blob: 1f949eac6351c32c1ce1d9b2de6ea44069550bac [file] [log] [blame]
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02001/*
2 * Handles the M-Systems DiskOnChip G3 chip
3 *
4 * Copyright (C) 2011 Robert Jarzmik
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 */
21
22#include <linux/kernel.h>
23#include <linux/module.h>
24#include <linux/errno.h>
25#include <linux/platform_device.h>
26#include <linux/string.h>
27#include <linux/slab.h>
28#include <linux/io.h>
29#include <linux/delay.h>
30#include <linux/mtd/mtd.h>
31#include <linux/mtd/partitions.h>
32
33#include <linux/debugfs.h>
34#include <linux/seq_file.h>
35
36#define CREATE_TRACE_POINTS
37#include "docg3.h"
38
39/*
40 * This driver handles the DiskOnChip G3 flash memory.
41 *
42 * As no specification is available from M-Systems/Sandisk, this drivers lacks
43 * several functions available on the chip, as :
Robert Jarzmikefa2ca72011-10-05 15:22:34 +020044 * - IPL write
45 * - ECC fixing (lack of BCH algorith understanding)
46 * - powerdown / powerup
47 *
48 * The bus data width (8bits versus 16bits) is not handled (if_cfg flag), and
49 * the driver assumes a 16bits data bus.
50 *
51 * DocG3 relies on 2 ECC algorithms, which are handled in hardware :
52 * - a 1 byte Hamming code stored in the OOB for each page
53 * - a 7 bytes BCH code stored in the OOB for each page
54 * The BCH part is only used for check purpose, no correction is available as
55 * some information is missing. What is known is that :
56 * - BCH is in GF(2^14)
57 * - BCH is over data of 520 bytes (512 page + 7 page_info bytes
58 * + 1 hamming byte)
59 * - BCH can correct up to 4 bits (t = 4)
60 * - BCH syndroms are calculated in hardware, and checked in hardware as well
61 *
62 */
63
Robert Jarzmik732b63b2011-11-19 16:02:49 +010064/**
65 * struct docg3_oobinfo - DiskOnChip G3 OOB layout
66 * @eccbytes: 8 bytes are used (1 for Hamming ECC, 7 for BCH ECC)
67 * @eccpos: ecc positions (byte 7 is Hamming ECC, byte 8-14 are BCH ECC)
68 * @oobfree: free pageinfo bytes (byte 0 until byte 6, byte 15
69 * @oobavail: 8 available bytes remaining after ECC toll
70 */
71static struct nand_ecclayout docg3_oobinfo = {
72 .eccbytes = 8,
73 .eccpos = {7, 8, 9, 10, 11, 12, 13, 14},
74 .oobfree = {{0, 7}, {15, 1} },
75 .oobavail = 8,
76};
77
Robert Jarzmikefa2ca72011-10-05 15:22:34 +020078static inline u8 doc_readb(struct docg3 *docg3, u16 reg)
79{
80 u8 val = readb(docg3->base + reg);
81
82 trace_docg3_io(0, 8, reg, (int)val);
83 return val;
84}
85
86static inline u16 doc_readw(struct docg3 *docg3, u16 reg)
87{
88 u16 val = readw(docg3->base + reg);
89
90 trace_docg3_io(0, 16, reg, (int)val);
91 return val;
92}
93
94static inline void doc_writeb(struct docg3 *docg3, u8 val, u16 reg)
95{
96 writeb(val, docg3->base + reg);
Robert Jarzmik84a93052011-11-19 16:02:44 +010097 trace_docg3_io(1, 8, reg, val);
Robert Jarzmikefa2ca72011-10-05 15:22:34 +020098}
99
100static inline void doc_writew(struct docg3 *docg3, u16 val, u16 reg)
101{
102 writew(val, docg3->base + reg);
103 trace_docg3_io(1, 16, reg, val);
104}
105
106static inline void doc_flash_command(struct docg3 *docg3, u8 cmd)
107{
108 doc_writeb(docg3, cmd, DOC_FLASHCOMMAND);
109}
110
111static inline void doc_flash_sequence(struct docg3 *docg3, u8 seq)
112{
113 doc_writeb(docg3, seq, DOC_FLASHSEQUENCE);
114}
115
116static inline void doc_flash_address(struct docg3 *docg3, u8 addr)
117{
118 doc_writeb(docg3, addr, DOC_FLASHADDRESS);
119}
120
121static char const *part_probes[] = { "cmdlinepart", "saftlpart", NULL };
122
123static int doc_register_readb(struct docg3 *docg3, int reg)
124{
125 u8 val;
126
127 doc_writew(docg3, reg, DOC_READADDRESS);
128 val = doc_readb(docg3, reg);
129 doc_vdbg("Read register %04x : %02x\n", reg, val);
130 return val;
131}
132
133static int doc_register_readw(struct docg3 *docg3, int reg)
134{
135 u16 val;
136
137 doc_writew(docg3, reg, DOC_READADDRESS);
138 val = doc_readw(docg3, reg);
139 doc_vdbg("Read register %04x : %04x\n", reg, val);
140 return val;
141}
142
143/**
144 * doc_delay - delay docg3 operations
145 * @docg3: the device
146 * @nbNOPs: the number of NOPs to issue
147 *
148 * As no specification is available, the right timings between chip commands are
149 * unknown. The only available piece of information are the observed nops on a
150 * working docg3 chip.
151 * Therefore, doc_delay relies on a busy loop of NOPs, instead of scheduler
152 * friendlier msleep() functions or blocking mdelay().
153 */
154static void doc_delay(struct docg3 *docg3, int nbNOPs)
155{
156 int i;
157
Robert Jarzmikac48e802011-11-19 16:02:43 +0100158 doc_vdbg("NOP x %d\n", nbNOPs);
Robert Jarzmikefa2ca72011-10-05 15:22:34 +0200159 for (i = 0; i < nbNOPs; i++)
160 doc_writeb(docg3, 0, DOC_NOP);
161}
162
163static int is_prot_seq_error(struct docg3 *docg3)
164{
165 int ctrl;
166
167 ctrl = doc_register_readb(docg3, DOC_FLASHCONTROL);
168 return ctrl & (DOC_CTRL_PROTECTION_ERROR | DOC_CTRL_SEQUENCE_ERROR);
169}
170
171static int doc_is_ready(struct docg3 *docg3)
172{
173 int ctrl;
174
175 ctrl = doc_register_readb(docg3, DOC_FLASHCONTROL);
176 return ctrl & DOC_CTRL_FLASHREADY;
177}
178
179static int doc_wait_ready(struct docg3 *docg3)
180{
181 int maxWaitCycles = 100;
182
183 do {
184 doc_delay(docg3, 4);
185 cpu_relax();
186 } while (!doc_is_ready(docg3) && maxWaitCycles--);
187 doc_delay(docg3, 2);
188 if (maxWaitCycles > 0)
189 return 0;
190 else
191 return -EIO;
192}
193
194static int doc_reset_seq(struct docg3 *docg3)
195{
196 int ret;
197
198 doc_writeb(docg3, 0x10, DOC_FLASHCONTROL);
199 doc_flash_sequence(docg3, DOC_SEQ_RESET);
200 doc_flash_command(docg3, DOC_CMD_RESET);
201 doc_delay(docg3, 2);
202 ret = doc_wait_ready(docg3);
203
204 doc_dbg("doc_reset_seq() -> isReady=%s\n", ret ? "false" : "true");
205 return ret;
206}
207
208/**
209 * doc_read_data_area - Read data from data area
210 * @docg3: the device
Robert Jarzmik32a50b32011-11-19 16:02:47 +0100211 * @buf: the buffer to fill in (might be NULL is dummy reads)
212 * @len: the length to read
Robert Jarzmikefa2ca72011-10-05 15:22:34 +0200213 * @first: first time read, DOC_READADDRESS should be set
214 *
215 * Reads bytes from flash data. Handles the single byte / even bytes reads.
216 */
217static void doc_read_data_area(struct docg3 *docg3, void *buf, int len,
218 int first)
219{
220 int i, cdr, len4;
221 u16 data16, *dst16;
222 u8 data8, *dst8;
223
224 doc_dbg("doc_read_data_area(buf=%p, len=%d)\n", buf, len);
225 cdr = len & 0x3;
226 len4 = len - cdr;
227
228 if (first)
229 doc_writew(docg3, DOC_IOSPACE_DATA, DOC_READADDRESS);
230 dst16 = buf;
231 for (i = 0; i < len4; i += 2) {
232 data16 = doc_readw(docg3, DOC_IOSPACE_DATA);
Robert Jarzmik32a50b32011-11-19 16:02:47 +0100233 if (dst16) {
234 *dst16 = data16;
235 dst16++;
236 }
Robert Jarzmikefa2ca72011-10-05 15:22:34 +0200237 }
238
239 if (cdr) {
240 doc_writew(docg3, DOC_IOSPACE_DATA | DOC_READADDR_ONE_BYTE,
241 DOC_READADDRESS);
242 doc_delay(docg3, 1);
243 dst8 = (u8 *)dst16;
244 for (i = 0; i < cdr; i++) {
245 data8 = doc_readb(docg3, DOC_IOSPACE_DATA);
Robert Jarzmik32a50b32011-11-19 16:02:47 +0100246 if (dst8) {
247 *dst8 = data8;
248 dst8++;
249 }
Robert Jarzmikefa2ca72011-10-05 15:22:34 +0200250 }
251 }
252}
253
254/**
Robert Jarzmikfb50b582011-11-19 16:02:52 +0100255 * doc_write_data_area - Write data into data area
256 * @docg3: the device
257 * @buf: the buffer to get input bytes from
258 * @len: the length to write
259 *
260 * Writes bytes into flash data. Handles the single byte / even bytes writes.
261 */
262static void doc_write_data_area(struct docg3 *docg3, const void *buf, int len)
263{
264 int i, cdr, len4;
265 u16 *src16;
266 u8 *src8;
267
268 doc_dbg("doc_write_data_area(buf=%p, len=%d)\n", buf, len);
269 cdr = len & 0x3;
270 len4 = len - cdr;
271
272 doc_writew(docg3, DOC_IOSPACE_DATA, DOC_READADDRESS);
273 src16 = (u16 *)buf;
274 for (i = 0; i < len4; i += 2) {
275 doc_writew(docg3, *src16, DOC_IOSPACE_DATA);
276 src16++;
277 }
278
279 src8 = (u8 *)src16;
280 for (i = 0; i < cdr; i++) {
281 doc_writew(docg3, DOC_IOSPACE_DATA | DOC_READADDR_ONE_BYTE,
282 DOC_READADDRESS);
283 doc_writeb(docg3, *src8, DOC_IOSPACE_DATA);
284 src8++;
285 }
286}
287
288/**
Robert Jarzmikefa2ca72011-10-05 15:22:34 +0200289 * doc_set_data_mode - Sets the flash to reliable data mode
290 * @docg3: the device
291 *
292 * The reliable data mode is a bit slower than the fast mode, but less errors
293 * occur. Entering the reliable mode cannot be done without entering the fast
294 * mode first.
295 */
296static void doc_set_reliable_mode(struct docg3 *docg3)
297{
298 doc_dbg("doc_set_reliable_mode()\n");
299 doc_flash_sequence(docg3, DOC_SEQ_SET_MODE);
300 doc_flash_command(docg3, DOC_CMD_FAST_MODE);
301 doc_flash_command(docg3, DOC_CMD_RELIABLE_MODE);
302 doc_delay(docg3, 2);
303}
304
305/**
306 * doc_set_asic_mode - Set the ASIC mode
307 * @docg3: the device
308 * @mode: the mode
309 *
310 * The ASIC can work in 3 modes :
311 * - RESET: all registers are zeroed
312 * - NORMAL: receives and handles commands
313 * - POWERDOWN: minimal poweruse, flash parts shut off
314 */
315static void doc_set_asic_mode(struct docg3 *docg3, u8 mode)
316{
317 int i;
318
319 for (i = 0; i < 12; i++)
320 doc_readb(docg3, DOC_IOSPACE_IPL);
321
322 mode |= DOC_ASICMODE_MDWREN;
323 doc_dbg("doc_set_asic_mode(%02x)\n", mode);
324 doc_writeb(docg3, mode, DOC_ASICMODE);
325 doc_writeb(docg3, ~mode, DOC_ASICMODECONFIRM);
326 doc_delay(docg3, 1);
327}
328
329/**
330 * doc_set_device_id - Sets the devices id for cascaded G3 chips
331 * @docg3: the device
332 * @id: the chip to select (amongst 0, 1, 2, 3)
333 *
334 * There can be 4 cascaded G3 chips. This function selects the one which will
335 * should be the active one.
336 */
337static void doc_set_device_id(struct docg3 *docg3, int id)
338{
339 u8 ctrl;
340
341 doc_dbg("doc_set_device_id(%d)\n", id);
342 doc_writeb(docg3, id, DOC_DEVICESELECT);
343 ctrl = doc_register_readb(docg3, DOC_FLASHCONTROL);
344
345 ctrl &= ~DOC_CTRL_VIOLATION;
346 ctrl |= DOC_CTRL_CE;
347 doc_writeb(docg3, ctrl, DOC_FLASHCONTROL);
348}
349
350/**
351 * doc_set_extra_page_mode - Change flash page layout
352 * @docg3: the device
353 *
354 * Normally, the flash page is split into the data (512 bytes) and the out of
355 * band data (16 bytes). For each, 4 more bytes can be accessed, where the wear
356 * leveling counters are stored. To access this last area of 4 bytes, a special
357 * mode must be input to the flash ASIC.
358 *
359 * Returns 0 if no error occured, -EIO else.
360 */
361static int doc_set_extra_page_mode(struct docg3 *docg3)
362{
363 int fctrl;
364
365 doc_dbg("doc_set_extra_page_mode()\n");
366 doc_flash_sequence(docg3, DOC_SEQ_PAGE_SIZE_532);
367 doc_flash_command(docg3, DOC_CMD_PAGE_SIZE_532);
368 doc_delay(docg3, 2);
369
370 fctrl = doc_register_readb(docg3, DOC_FLASHCONTROL);
371 if (fctrl & (DOC_CTRL_PROTECTION_ERROR | DOC_CTRL_SEQUENCE_ERROR))
372 return -EIO;
373 else
374 return 0;
375}
376
377/**
Robert Jarzmikfb50b582011-11-19 16:02:52 +0100378 * doc_setup_addr_sector - Setup blocks/page/ofs address for one plane
379 * @docg3: the device
380 * @sector: the sector
381 */
382static void doc_setup_addr_sector(struct docg3 *docg3, int sector)
383{
384 doc_delay(docg3, 1);
385 doc_flash_address(docg3, sector & 0xff);
386 doc_flash_address(docg3, (sector >> 8) & 0xff);
387 doc_flash_address(docg3, (sector >> 16) & 0xff);
388 doc_delay(docg3, 1);
389}
390
391/**
392 * doc_setup_writeaddr_sector - Setup blocks/page/ofs address for one plane
393 * @docg3: the device
394 * @sector: the sector
395 * @ofs: the offset in the page, between 0 and (512 + 16 + 512)
396 */
397static void doc_setup_writeaddr_sector(struct docg3 *docg3, int sector, int ofs)
398{
399 ofs = ofs >> 2;
400 doc_delay(docg3, 1);
401 doc_flash_address(docg3, ofs & 0xff);
402 doc_flash_address(docg3, sector & 0xff);
403 doc_flash_address(docg3, (sector >> 8) & 0xff);
404 doc_flash_address(docg3, (sector >> 16) & 0xff);
405 doc_delay(docg3, 1);
406}
407
408/**
Robert Jarzmikefa2ca72011-10-05 15:22:34 +0200409 * doc_seek - Set both flash planes to the specified block, page for reading
410 * @docg3: the device
411 * @block0: the first plane block index
412 * @block1: the second plane block index
413 * @page: the page index within the block
414 * @wear: if true, read will occur on the 4 extra bytes of the wear area
415 * @ofs: offset in page to read
416 *
417 * Programs the flash even and odd planes to the specific block and page.
418 * Alternatively, programs the flash to the wear area of the specified page.
419 */
420static int doc_read_seek(struct docg3 *docg3, int block0, int block1, int page,
421 int wear, int ofs)
422{
423 int sector, ret = 0;
424
425 doc_dbg("doc_seek(blocks=(%d,%d), page=%d, ofs=%d, wear=%d)\n",
426 block0, block1, page, ofs, wear);
427
428 if (!wear && (ofs < 2 * DOC_LAYOUT_PAGE_SIZE)) {
429 doc_flash_sequence(docg3, DOC_SEQ_SET_PLANE1);
430 doc_flash_command(docg3, DOC_CMD_READ_PLANE1);
431 doc_delay(docg3, 2);
432 } else {
433 doc_flash_sequence(docg3, DOC_SEQ_SET_PLANE2);
434 doc_flash_command(docg3, DOC_CMD_READ_PLANE2);
435 doc_delay(docg3, 2);
436 }
437
438 doc_set_reliable_mode(docg3);
439 if (wear)
440 ret = doc_set_extra_page_mode(docg3);
441 if (ret)
442 goto out;
443
Robert Jarzmikefa2ca72011-10-05 15:22:34 +0200444 doc_flash_sequence(docg3, DOC_SEQ_READ);
Robert Jarzmikfb50b582011-11-19 16:02:52 +0100445 sector = (block0 << DOC_ADDR_BLOCK_SHIFT) + (page & DOC_ADDR_PAGE_MASK);
Robert Jarzmikefa2ca72011-10-05 15:22:34 +0200446 doc_flash_command(docg3, DOC_CMD_PROG_BLOCK_ADDR);
Robert Jarzmikfb50b582011-11-19 16:02:52 +0100447 doc_setup_addr_sector(docg3, sector);
Robert Jarzmikefa2ca72011-10-05 15:22:34 +0200448
449 sector = (block1 << DOC_ADDR_BLOCK_SHIFT) + (page & DOC_ADDR_PAGE_MASK);
450 doc_flash_command(docg3, DOC_CMD_PROG_BLOCK_ADDR);
Robert Jarzmikfb50b582011-11-19 16:02:52 +0100451 doc_setup_addr_sector(docg3, sector);
Robert Jarzmikefa2ca72011-10-05 15:22:34 +0200452 doc_delay(docg3, 1);
Robert Jarzmikefa2ca72011-10-05 15:22:34 +0200453
454out:
455 return ret;
456}
457
458/**
Robert Jarzmikfb50b582011-11-19 16:02:52 +0100459 * doc_write_seek - Set both flash planes to the specified block, page for writing
460 * @docg3: the device
461 * @block0: the first plane block index
462 * @block1: the second plane block index
463 * @page: the page index within the block
464 * @ofs: offset in page to write
465 *
466 * Programs the flash even and odd planes to the specific block and page.
467 * Alternatively, programs the flash to the wear area of the specified page.
468 */
469static int doc_write_seek(struct docg3 *docg3, int block0, int block1, int page,
470 int ofs)
471{
472 int ret = 0, sector;
473
474 doc_dbg("doc_write_seek(blocks=(%d,%d), page=%d, ofs=%d)\n",
475 block0, block1, page, ofs);
476
477 doc_set_reliable_mode(docg3);
478
479 if (ofs < 2 * DOC_LAYOUT_PAGE_SIZE) {
480 doc_flash_sequence(docg3, DOC_SEQ_SET_PLANE1);
481 doc_flash_command(docg3, DOC_CMD_READ_PLANE1);
482 doc_delay(docg3, 2);
483 } else {
484 doc_flash_sequence(docg3, DOC_SEQ_SET_PLANE2);
485 doc_flash_command(docg3, DOC_CMD_READ_PLANE2);
486 doc_delay(docg3, 2);
487 }
488
489 doc_flash_sequence(docg3, DOC_SEQ_PAGE_SETUP);
490 doc_flash_command(docg3, DOC_CMD_PROG_CYCLE1);
491
492 sector = (block0 << DOC_ADDR_BLOCK_SHIFT) + (page & DOC_ADDR_PAGE_MASK);
493 doc_setup_writeaddr_sector(docg3, sector, ofs);
494
495 doc_flash_command(docg3, DOC_CMD_PROG_CYCLE3);
496 doc_delay(docg3, 2);
497 ret = doc_wait_ready(docg3);
498 if (ret)
499 goto out;
500
501 doc_flash_command(docg3, DOC_CMD_PROG_CYCLE1);
502 sector = (block1 << DOC_ADDR_BLOCK_SHIFT) + (page & DOC_ADDR_PAGE_MASK);
503 doc_setup_writeaddr_sector(docg3, sector, ofs);
504 doc_delay(docg3, 1);
505
506out:
507 return ret;
508}
509
510
511/**
Robert Jarzmikefa2ca72011-10-05 15:22:34 +0200512 * doc_read_page_ecc_init - Initialize hardware ECC engine
513 * @docg3: the device
514 * @len: the number of bytes covered by the ECC (BCH covered)
515 *
516 * The function does initialize the hardware ECC engine to compute the Hamming
517 * ECC (on 1 byte) and the BCH Syndroms (on 7 bytes).
518 *
519 * Return 0 if succeeded, -EIO on error
520 */
521static int doc_read_page_ecc_init(struct docg3 *docg3, int len)
522{
523 doc_writew(docg3, DOC_ECCCONF0_READ_MODE
524 | DOC_ECCCONF0_BCH_ENABLE | DOC_ECCCONF0_HAMMING_ENABLE
525 | (len & DOC_ECCCONF0_DATA_BYTES_MASK),
526 DOC_ECCCONF0);
527 doc_delay(docg3, 4);
528 doc_register_readb(docg3, DOC_FLASHCONTROL);
529 return doc_wait_ready(docg3);
530}
531
532/**
Robert Jarzmikfb50b582011-11-19 16:02:52 +0100533 * doc_write_page_ecc_init - Initialize hardware BCH ECC engine
534 * @docg3: the device
535 * @len: the number of bytes covered by the ECC (BCH covered)
536 *
537 * The function does initialize the hardware ECC engine to compute the Hamming
538 * ECC (on 1 byte) and the BCH Syndroms (on 7 bytes).
539 *
540 * Return 0 if succeeded, -EIO on error
541 */
542static int doc_write_page_ecc_init(struct docg3 *docg3, int len)
543{
544 doc_writew(docg3, !DOC_ECCCONF0_READ_MODE
545 | DOC_ECCCONF0_BCH_ENABLE | DOC_ECCCONF0_HAMMING_ENABLE
546 | (len & DOC_ECCCONF0_DATA_BYTES_MASK),
547 DOC_ECCCONF0);
548 doc_delay(docg3, 4);
549 doc_register_readb(docg3, DOC_FLASHCONTROL);
550 return doc_wait_ready(docg3);
551}
552
553/**
554 * doc_ecc_disable - Disable Hamming and BCH ECC hardware calculator
555 * @docg3: the device
556 *
557 * Disables the hardware ECC generator and checker, for unchecked reads (as when
558 * reading OOB only or write status byte).
559 */
560static void doc_ecc_disable(struct docg3 *docg3)
561{
562 doc_writew(docg3, DOC_ECCCONF0_READ_MODE, DOC_ECCCONF0);
563 doc_delay(docg3, 4);
564}
565
566/**
567 * doc_hamming_ecc_init - Initialize hardware Hamming ECC engine
568 * @docg3: the device
569 * @nb_bytes: the number of bytes covered by the ECC (Hamming covered)
570 *
571 * This function programs the ECC hardware to compute the hamming code on the
572 * last provided N bytes to the hardware generator.
573 */
574static void doc_hamming_ecc_init(struct docg3 *docg3, int nb_bytes)
575{
576 u8 ecc_conf1;
577
578 ecc_conf1 = doc_register_readb(docg3, DOC_ECCCONF1);
579 ecc_conf1 &= ~DOC_ECCCONF1_HAMMING_BITS_MASK;
580 ecc_conf1 |= (nb_bytes & DOC_ECCCONF1_HAMMING_BITS_MASK);
581 doc_writeb(docg3, ecc_conf1, DOC_ECCCONF1);
582}
583
584/**
Robert Jarzmikefa2ca72011-10-05 15:22:34 +0200585 * doc_read_page_prepare - Prepares reading data from a flash page
586 * @docg3: the device
587 * @block0: the first plane block index on flash memory
588 * @block1: the second plane block index on flash memory
589 * @page: the page index in the block
590 * @offset: the offset in the page (must be a multiple of 4)
591 *
592 * Prepares the page to be read in the flash memory :
593 * - tell ASIC to map the flash pages
594 * - tell ASIC to be in read mode
595 *
596 * After a call to this method, a call to doc_read_page_finish is mandatory,
597 * to end the read cycle of the flash.
598 *
599 * Read data from a flash page. The length to be read must be between 0 and
600 * (page_size + oob_size + wear_size), ie. 532, and a multiple of 4 (because
601 * the extra bytes reading is not implemented).
602 *
603 * As pages are grouped by 2 (in 2 planes), reading from a page must be done
604 * in two steps:
605 * - one read of 512 bytes at offset 0
606 * - one read of 512 bytes at offset 512 + 16
607 *
608 * Returns 0 if successful, -EIO if a read error occured.
609 */
610static int doc_read_page_prepare(struct docg3 *docg3, int block0, int block1,
611 int page, int offset)
612{
613 int wear_area = 0, ret = 0;
614
615 doc_dbg("doc_read_page_prepare(blocks=(%d,%d), page=%d, ofsInPage=%d)\n",
616 block0, block1, page, offset);
617 if (offset >= DOC_LAYOUT_WEAR_OFFSET)
618 wear_area = 1;
619 if (!wear_area && offset > (DOC_LAYOUT_PAGE_OOB_SIZE * 2))
620 return -EINVAL;
621
622 doc_set_device_id(docg3, docg3->device_id);
623 ret = doc_reset_seq(docg3);
624 if (ret)
625 goto err;
626
627 /* Program the flash address block and page */
628 ret = doc_read_seek(docg3, block0, block1, page, wear_area, offset);
629 if (ret)
630 goto err;
631
632 doc_flash_command(docg3, DOC_CMD_READ_ALL_PLANES);
633 doc_delay(docg3, 2);
634 doc_wait_ready(docg3);
635
636 doc_flash_command(docg3, DOC_CMD_SET_ADDR_READ);
637 doc_delay(docg3, 1);
638 if (offset >= DOC_LAYOUT_PAGE_SIZE * 2)
639 offset -= 2 * DOC_LAYOUT_PAGE_SIZE;
640 doc_flash_address(docg3, offset >> 2);
641 doc_delay(docg3, 1);
642 doc_wait_ready(docg3);
643
644 doc_flash_command(docg3, DOC_CMD_READ_FLASH);
645
646 return 0;
647err:
648 doc_writeb(docg3, 0, DOC_DATAEND);
649 doc_delay(docg3, 2);
650 return -EIO;
651}
652
653/**
654 * doc_read_page_getbytes - Reads bytes from a prepared page
655 * @docg3: the device
656 * @len: the number of bytes to be read (must be a multiple of 4)
657 * @buf: the buffer to be filled in
658 * @first: 1 if first time read, DOC_READADDRESS should be set
659 *
660 */
661static int doc_read_page_getbytes(struct docg3 *docg3, int len, u_char *buf,
662 int first)
663{
664 doc_read_data_area(docg3, buf, len, first);
665 doc_delay(docg3, 2);
666 return len;
667}
668
669/**
Robert Jarzmikfb50b582011-11-19 16:02:52 +0100670 * doc_write_page_putbytes - Writes bytes into a prepared page
671 * @docg3: the device
672 * @len: the number of bytes to be written
673 * @buf: the buffer of input bytes
674 *
675 */
676static void doc_write_page_putbytes(struct docg3 *docg3, int len,
677 const u_char *buf)
678{
679 doc_write_data_area(docg3, buf, len);
680 doc_delay(docg3, 2);
681}
682
683/**
Robert Jarzmikefa2ca72011-10-05 15:22:34 +0200684 * doc_get_hw_bch_syndroms - Get hardware calculated BCH syndroms
685 * @docg3: the device
686 * @syns: the array of 7 integers where the syndroms will be stored
687 */
Robert Jarzmikfb50b582011-11-19 16:02:52 +0100688static void doc_get_hw_bch_syndroms(struct docg3 *docg3, u8 *syns)
Robert Jarzmikefa2ca72011-10-05 15:22:34 +0200689{
690 int i;
691
692 for (i = 0; i < DOC_ECC_BCH_SIZE; i++)
693 syns[i] = doc_register_readb(docg3, DOC_BCH_SYNDROM(i));
694}
695
696/**
Robert Jarzmikfb50b582011-11-19 16:02:52 +0100697 * doc_page_finish - Ends reading/writing of a flash page
698 * @docg3: the device
699 */
700static void doc_page_finish(struct docg3 *docg3)
701{
702 doc_writeb(docg3, 0, DOC_DATAEND);
703 doc_delay(docg3, 2);
704}
705
706/**
Robert Jarzmikefa2ca72011-10-05 15:22:34 +0200707 * doc_read_page_finish - Ends reading of a flash page
708 * @docg3: the device
709 *
710 * As a side effect, resets the chip selector to 0. This ensures that after each
711 * read operation, the floor 0 is selected. Therefore, if the systems halts, the
712 * reboot will boot on floor 0, where the IPL is.
713 */
714static void doc_read_page_finish(struct docg3 *docg3)
715{
Robert Jarzmikfb50b582011-11-19 16:02:52 +0100716 doc_page_finish(docg3);
Robert Jarzmikefa2ca72011-10-05 15:22:34 +0200717 doc_set_device_id(docg3, 0);
718}
719
720/**
721 * calc_block_sector - Calculate blocks, pages and ofs.
722
723 * @from: offset in flash
724 * @block0: first plane block index calculated
725 * @block1: second plane block index calculated
726 * @page: page calculated
727 * @ofs: offset in page
728 */
729static void calc_block_sector(loff_t from, int *block0, int *block1, int *page,
730 int *ofs)
731{
732 uint sector;
733
734 sector = from / DOC_LAYOUT_PAGE_SIZE;
735 *block0 = sector / (DOC_LAYOUT_PAGES_PER_BLOCK * DOC_LAYOUT_NBPLANES)
736 * DOC_LAYOUT_NBPLANES;
737 *block1 = *block0 + 1;
738 *page = sector % (DOC_LAYOUT_PAGES_PER_BLOCK * DOC_LAYOUT_NBPLANES);
739 *page /= DOC_LAYOUT_NBPLANES;
740 if (sector % 2)
741 *ofs = DOC_LAYOUT_PAGE_OOB_SIZE;
742 else
743 *ofs = 0;
744}
745
746/**
Robert Jarzmik32a50b32011-11-19 16:02:47 +0100747 * doc_read_oob - Read out of band bytes from flash
748 * @mtd: the device
749 * @from: the offset from first block and first page, in bytes, aligned on page
750 * size
751 * @ops: the mtd oob structure
752 *
753 * Reads flash memory OOB area of pages.
754 *
755 * Returns 0 if read successfull, of -EIO, -EINVAL if an error occured
756 */
757static int doc_read_oob(struct mtd_info *mtd, loff_t from,
758 struct mtd_oob_ops *ops)
759{
760 struct docg3 *docg3 = mtd->priv;
761 int block0, block1, page, ret, ofs = 0;
762 u8 *oobbuf = ops->oobbuf;
763 u8 *buf = ops->datbuf;
764 size_t len, ooblen, nbdata, nboob;
765 u8 calc_ecc[DOC_ECC_BCH_SIZE], eccconf1;
766
767 if (buf)
768 len = ops->len;
769 else
770 len = 0;
771 if (oobbuf)
772 ooblen = ops->ooblen;
773 else
774 ooblen = 0;
775
776 if (oobbuf && ops->mode == MTD_OPS_PLACE_OOB)
777 oobbuf += ops->ooboffs;
778
779 doc_dbg("doc_read_oob(from=%lld, mode=%d, data=(%p:%zu), oob=(%p:%zu))\n",
780 from, ops->mode, buf, len, oobbuf, ooblen);
781 if ((len % DOC_LAYOUT_PAGE_SIZE) || (ooblen % DOC_LAYOUT_OOB_SIZE) ||
782 (from % DOC_LAYOUT_PAGE_SIZE))
783 return -EINVAL;
784
785 ret = -EINVAL;
786 calc_block_sector(from + len, &block0, &block1, &page, &ofs);
787 if (block1 > docg3->max_block)
788 goto err;
789
790 ops->oobretlen = 0;
791 ops->retlen = 0;
792 ret = 0;
793 while (!ret && (len > 0 || ooblen > 0)) {
794 calc_block_sector(from, &block0, &block1, &page, &ofs);
795 nbdata = min_t(size_t, len, (size_t)DOC_LAYOUT_PAGE_SIZE);
796 nboob = min_t(size_t, ooblen, (size_t)DOC_LAYOUT_OOB_SIZE);
797 ret = doc_read_page_prepare(docg3, block0, block1, page, ofs);
798 if (ret < 0)
799 goto err;
800 ret = doc_read_page_ecc_init(docg3, DOC_ECC_BCH_COVERED_BYTES);
801 if (ret < 0)
802 goto err_in_read;
803 ret = doc_read_page_getbytes(docg3, nbdata, buf, 1);
804 if (ret < nbdata)
805 goto err_in_read;
806 doc_read_page_getbytes(docg3, DOC_LAYOUT_PAGE_SIZE - nbdata,
807 NULL, 0);
808 ret = doc_read_page_getbytes(docg3, nboob, oobbuf, 0);
809 if (ret < nboob)
810 goto err_in_read;
811 doc_read_page_getbytes(docg3, DOC_LAYOUT_OOB_SIZE - nboob,
812 NULL, 0);
813
814 doc_get_hw_bch_syndroms(docg3, calc_ecc);
815 eccconf1 = doc_register_readb(docg3, DOC_ECCCONF1);
816
817 if (nboob >= DOC_LAYOUT_OOB_SIZE) {
818 doc_dbg("OOB - INFO: %02x:%02x:%02x:%02x:%02x:%02x:%02x\n",
819 oobbuf[0], oobbuf[1], oobbuf[2], oobbuf[3],
820 oobbuf[4], oobbuf[5], oobbuf[6]);
821 doc_dbg("OOB - HAMMING: %02x\n", oobbuf[7]);
822 doc_dbg("OOB - BCH_ECC: %02x:%02x:%02x:%02x:%02x:%02x:%02x\n",
823 oobbuf[8], oobbuf[9], oobbuf[10], oobbuf[11],
824 oobbuf[12], oobbuf[13], oobbuf[14]);
825 doc_dbg("OOB - UNUSED: %02x\n", oobbuf[15]);
826 }
827 doc_dbg("ECC checks: ECCConf1=%x\n", eccconf1);
828 doc_dbg("ECC CALC_ECC: %02x:%02x:%02x:%02x:%02x:%02x:%02x\n",
829 calc_ecc[0], calc_ecc[1], calc_ecc[2],
830 calc_ecc[3], calc_ecc[4], calc_ecc[5],
831 calc_ecc[6]);
832
833 ret = -EBADMSG;
834 if (block0 >= DOC_LAYOUT_BLOCK_FIRST_DATA) {
835 if ((eccconf1 & DOC_ECCCONF1_BCH_SYNDROM_ERR) &&
836 (eccconf1 & DOC_ECCCONF1_PAGE_IS_WRITTEN))
837 goto err_in_read;
838 if (is_prot_seq_error(docg3))
839 goto err_in_read;
840 }
841
842 doc_read_page_finish(docg3);
843 ops->retlen += nbdata;
844 ops->oobretlen += nboob;
845 buf += nbdata;
846 oobbuf += nboob;
847 len -= nbdata;
848 ooblen -= nboob;
849 from += DOC_LAYOUT_PAGE_SIZE;
850 }
851
852 return 0;
853err_in_read:
854 doc_read_page_finish(docg3);
855err:
856 return ret;
857}
858
859/**
Robert Jarzmikefa2ca72011-10-05 15:22:34 +0200860 * doc_read - Read bytes from flash
861 * @mtd: the device
862 * @from: the offset from first block and first page, in bytes, aligned on page
863 * size
864 * @len: the number of bytes to read (must be a multiple of 4)
865 * @retlen: the number of bytes actually read
866 * @buf: the filled in buffer
867 *
868 * Reads flash memory pages. This function does not read the OOB chunk, but only
869 * the page data.
870 *
871 * Returns 0 if read successfull, of -EIO, -EINVAL if an error occured
872 */
873static int doc_read(struct mtd_info *mtd, loff_t from, size_t len,
874 size_t *retlen, u_char *buf)
875{
Robert Jarzmik32a50b32011-11-19 16:02:47 +0100876 struct mtd_oob_ops ops;
877 size_t ret;
Robert Jarzmikefa2ca72011-10-05 15:22:34 +0200878
Robert Jarzmik32a50b32011-11-19 16:02:47 +0100879 memset(&ops, 0, sizeof(ops));
880 ops.datbuf = buf;
881 ops.len = len;
882 ops.mode = MTD_OPS_AUTO_OOB;
Robert Jarzmikefa2ca72011-10-05 15:22:34 +0200883
Robert Jarzmik32a50b32011-11-19 16:02:47 +0100884 ret = doc_read_oob(mtd, from, &ops);
885 *retlen = ops.retlen;
Robert Jarzmikefa2ca72011-10-05 15:22:34 +0200886 return ret;
887}
888
Robert Jarzmikefa2ca72011-10-05 15:22:34 +0200889static int doc_reload_bbt(struct docg3 *docg3)
890{
891 int block = DOC_LAYOUT_BLOCK_BBT;
892 int ret = 0, nbpages, page;
893 u_char *buf = docg3->bbt;
894
895 nbpages = DIV_ROUND_UP(docg3->max_block + 1, 8 * DOC_LAYOUT_PAGE_SIZE);
896 for (page = 0; !ret && (page < nbpages); page++) {
897 ret = doc_read_page_prepare(docg3, block, block + 1,
898 page + DOC_LAYOUT_PAGE_BBT, 0);
899 if (!ret)
900 ret = doc_read_page_ecc_init(docg3,
901 DOC_LAYOUT_PAGE_SIZE);
902 if (!ret)
903 doc_read_page_getbytes(docg3, DOC_LAYOUT_PAGE_SIZE,
904 buf, 1);
905 buf += DOC_LAYOUT_PAGE_SIZE;
906 }
907 doc_read_page_finish(docg3);
908 return ret;
909}
910
911/**
912 * doc_block_isbad - Checks whether a block is good or not
913 * @mtd: the device
914 * @from: the offset to find the correct block
915 *
916 * Returns 1 if block is bad, 0 if block is good
917 */
918static int doc_block_isbad(struct mtd_info *mtd, loff_t from)
919{
920 struct docg3 *docg3 = mtd->priv;
921 int block0, block1, page, ofs, is_good;
922
923 calc_block_sector(from, &block0, &block1, &page, &ofs);
924 doc_dbg("doc_block_isbad(from=%lld) => block=(%d,%d), page=%d, ofs=%d\n",
925 from, block0, block1, page, ofs);
926
927 if (block0 < DOC_LAYOUT_BLOCK_FIRST_DATA)
928 return 0;
929 if (block1 > docg3->max_block)
930 return -EINVAL;
931
932 is_good = docg3->bbt[block0 >> 3] & (1 << (block0 & 0x7));
933 return !is_good;
934}
935
936/**
937 * doc_get_erase_count - Get block erase count
938 * @docg3: the device
939 * @from: the offset in which the block is.
940 *
941 * Get the number of times a block was erased. The number is the maximum of
942 * erase times between first and second plane (which should be equal normally).
943 *
944 * Returns The number of erases, or -EINVAL or -EIO on error.
945 */
946static int doc_get_erase_count(struct docg3 *docg3, loff_t from)
947{
948 u8 buf[DOC_LAYOUT_WEAR_SIZE];
949 int ret, plane1_erase_count, plane2_erase_count;
950 int block0, block1, page, ofs;
951
952 doc_dbg("doc_get_erase_count(from=%lld, buf=%p)\n", from, buf);
953 if (from % DOC_LAYOUT_PAGE_SIZE)
954 return -EINVAL;
955 calc_block_sector(from, &block0, &block1, &page, &ofs);
956 if (block1 > docg3->max_block)
957 return -EINVAL;
958
959 ret = doc_reset_seq(docg3);
960 if (!ret)
961 ret = doc_read_page_prepare(docg3, block0, block1, page,
962 ofs + DOC_LAYOUT_WEAR_OFFSET);
963 if (!ret)
964 ret = doc_read_page_getbytes(docg3, DOC_LAYOUT_WEAR_SIZE,
965 buf, 1);
966 doc_read_page_finish(docg3);
967
968 if (ret || (buf[0] != DOC_ERASE_MARK) || (buf[2] != DOC_ERASE_MARK))
969 return -EIO;
970 plane1_erase_count = (u8)(~buf[1]) | ((u8)(~buf[4]) << 8)
971 | ((u8)(~buf[5]) << 16);
972 plane2_erase_count = (u8)(~buf[3]) | ((u8)(~buf[6]) << 8)
973 | ((u8)(~buf[7]) << 16);
974
975 return max(plane1_erase_count, plane2_erase_count);
976}
977
Robert Jarzmikfb50b582011-11-19 16:02:52 +0100978/**
979 * doc_get_op_status - get erase/write operation status
980 * @docg3: the device
981 *
982 * Queries the status from the chip, and returns it
983 *
984 * Returns the status (bits DOC_PLANES_STATUS_*)
985 */
986static int doc_get_op_status(struct docg3 *docg3)
987{
988 u8 status;
989
990 doc_flash_sequence(docg3, DOC_SEQ_PLANES_STATUS);
991 doc_flash_command(docg3, DOC_CMD_PLANES_STATUS);
992 doc_delay(docg3, 5);
993
994 doc_ecc_disable(docg3);
995 doc_read_data_area(docg3, &status, 1, 1);
996 return status;
997}
998
999/**
1000 * doc_write_erase_wait_status - wait for write or erase completion
1001 * @docg3: the device
1002 *
1003 * Wait for the chip to be ready again after erase or write operation, and check
1004 * erase/write status.
1005 *
1006 * Returns 0 if erase successfull, -EIO if erase/write issue, -ETIMEOUT if
1007 * timeout
1008 */
1009static int doc_write_erase_wait_status(struct docg3 *docg3)
1010{
1011 int status, ret = 0;
1012
1013 if (!doc_is_ready(docg3))
1014 usleep_range(3000, 3000);
1015 if (!doc_is_ready(docg3)) {
1016 doc_dbg("Timeout reached and the chip is still not ready\n");
1017 ret = -EAGAIN;
1018 goto out;
1019 }
1020
1021 status = doc_get_op_status(docg3);
1022 if (status & DOC_PLANES_STATUS_FAIL) {
1023 doc_dbg("Erase/Write failed on (a) plane(s), status = %x\n",
1024 status);
1025 ret = -EIO;
1026 }
1027
1028out:
1029 doc_page_finish(docg3);
1030 return ret;
1031}
1032
1033/**
Robert Jarzmikde03cd72011-11-19 16:02:53 +01001034 * doc_erase_block - Erase a couple of blocks
1035 * @docg3: the device
1036 * @block0: the first block to erase (leftmost plane)
1037 * @block1: the second block to erase (rightmost plane)
1038 *
1039 * Erase both blocks, and return operation status
1040 *
1041 * Returns 0 if erase successful, -EIO if erase issue, -ETIMEOUT if chip not
1042 * ready for too long
1043 */
1044static int doc_erase_block(struct docg3 *docg3, int block0, int block1)
1045{
1046 int ret, sector;
1047
1048 doc_dbg("doc_erase_block(blocks=(%d,%d))\n", block0, block1);
1049 ret = doc_reset_seq(docg3);
1050 if (ret)
1051 return -EIO;
1052
1053 doc_set_reliable_mode(docg3);
1054 doc_flash_sequence(docg3, DOC_SEQ_ERASE);
1055
1056 sector = block0 << DOC_ADDR_BLOCK_SHIFT;
1057 doc_flash_command(docg3, DOC_CMD_PROG_BLOCK_ADDR);
1058 doc_setup_addr_sector(docg3, sector);
1059 sector = block1 << DOC_ADDR_BLOCK_SHIFT;
1060 doc_flash_command(docg3, DOC_CMD_PROG_BLOCK_ADDR);
1061 doc_setup_addr_sector(docg3, sector);
1062 doc_delay(docg3, 1);
1063
1064 doc_flash_command(docg3, DOC_CMD_ERASECYCLE2);
1065 doc_delay(docg3, 2);
1066
1067 if (is_prot_seq_error(docg3)) {
1068 doc_err("Erase blocks %d,%d error\n", block0, block1);
1069 return -EIO;
1070 }
1071
1072 return doc_write_erase_wait_status(docg3);
1073}
1074
1075/**
1076 * doc_erase - Erase a portion of the chip
1077 * @mtd: the device
1078 * @info: the erase info
1079 *
1080 * Erase a bunch of contiguous blocks, by pairs, as a "mtd" page of 1024 is
1081 * split into 2 pages of 512 bytes on 2 contiguous blocks.
1082 *
1083 * Returns 0 if erase successful, -EINVAL if adressing error, -EIO if erase
1084 * issue
1085 */
1086static int doc_erase(struct mtd_info *mtd, struct erase_info *info)
1087{
1088 struct docg3 *docg3 = mtd->priv;
1089 uint64_t len;
1090 int block0, block1, page, ret, ofs = 0;
1091
1092 doc_dbg("doc_erase(from=%lld, len=%lld\n", info->addr, info->len);
1093 doc_set_device_id(docg3, docg3->device_id);
1094
1095 info->state = MTD_ERASE_PENDING;
1096 calc_block_sector(info->addr + info->len,
1097 &block0, &block1, &page, &ofs);
1098 ret = -EINVAL;
1099 if (block1 > docg3->max_block || page || ofs)
1100 goto reset_err;
1101
1102 ret = 0;
1103 calc_block_sector(info->addr, &block0, &block1, &page, &ofs);
1104 doc_set_reliable_mode(docg3);
1105 for (len = info->len; !ret && len > 0; len -= mtd->erasesize) {
1106 info->state = MTD_ERASING;
1107 ret = doc_erase_block(docg3, block0, block1);
1108 block0 += 2;
1109 block1 += 2;
1110 }
1111
1112 if (ret)
1113 goto reset_err;
1114
1115 info->state = MTD_ERASE_DONE;
1116 return 0;
1117
1118reset_err:
1119 info->state = MTD_ERASE_FAILED;
1120 return ret;
1121}
1122
1123/**
Robert Jarzmikfb50b582011-11-19 16:02:52 +01001124 * doc_write_page - Write a single page to the chip
1125 * @docg3: the device
1126 * @to: the offset from first block and first page, in bytes, aligned on page
1127 * size
1128 * @buf: buffer to get bytes from
1129 * @oob: buffer to get out of band bytes from (can be NULL if no OOB should be
1130 * written)
1131 * @autoecc: if 0, all 16 bytes from OOB are taken, regardless of HW Hamming or
1132 * BCH computations. If 1, only bytes 0-7 and byte 15 are taken,
1133 * remaining ones are filled with hardware Hamming and BCH
1134 * computations. Its value is not meaningfull is oob == NULL.
1135 *
1136 * Write one full page (ie. 1 page split on two planes), of 512 bytes, with the
1137 * OOB data. The OOB ECC is automatically computed by the hardware Hamming and
1138 * BCH generator if autoecc is not null.
1139 *
1140 * Returns 0 if write successful, -EIO if write error, -EAGAIN if timeout
1141 */
1142static int doc_write_page(struct docg3 *docg3, loff_t to, const u_char *buf,
1143 const u_char *oob, int autoecc)
1144{
1145 int block0, block1, page, ret, ofs = 0;
1146 u8 syn[DOC_ECC_BCH_SIZE], hamming;
1147
1148 doc_dbg("doc_write_page(to=%lld)\n", to);
1149 calc_block_sector(to, &block0, &block1, &page, &ofs);
1150
1151 doc_set_device_id(docg3, docg3->device_id);
1152 ret = doc_reset_seq(docg3);
1153 if (ret)
1154 goto err;
1155
1156 /* Program the flash address block and page */
1157 ret = doc_write_seek(docg3, block0, block1, page, ofs);
1158 if (ret)
1159 goto err;
1160
1161 doc_write_page_ecc_init(docg3, DOC_ECC_BCH_COVERED_BYTES);
1162 doc_delay(docg3, 2);
1163 doc_write_page_putbytes(docg3, DOC_LAYOUT_PAGE_SIZE, buf);
1164
1165 if (oob && autoecc) {
1166 doc_write_page_putbytes(docg3, DOC_LAYOUT_OOB_PAGEINFO_SZ, oob);
1167 doc_delay(docg3, 2);
1168 oob += DOC_LAYOUT_OOB_UNUSED_OFS;
1169
1170 hamming = doc_register_readb(docg3, DOC_HAMMINGPARITY);
1171 doc_delay(docg3, 2);
1172 doc_write_page_putbytes(docg3, DOC_LAYOUT_OOB_HAMMING_SZ,
1173 &hamming);
1174 doc_delay(docg3, 2);
1175
1176 doc_get_hw_bch_syndroms(docg3, syn);
1177 doc_write_page_putbytes(docg3, DOC_LAYOUT_OOB_BCH_SZ, syn);
1178 doc_delay(docg3, 2);
1179
1180 doc_write_page_putbytes(docg3, DOC_LAYOUT_OOB_UNUSED_SZ, oob);
1181 }
1182 if (oob && !autoecc)
1183 doc_write_page_putbytes(docg3, DOC_LAYOUT_OOB_SIZE, oob);
1184
1185 doc_delay(docg3, 2);
1186 doc_page_finish(docg3);
1187 doc_delay(docg3, 2);
1188 doc_flash_command(docg3, DOC_CMD_PROG_CYCLE2);
1189 doc_delay(docg3, 2);
1190
1191 /*
1192 * The wait status will perform another doc_page_finish() call, but that
1193 * seems to please the docg3, so leave it.
1194 */
1195 ret = doc_write_erase_wait_status(docg3);
1196 return ret;
1197err:
1198 doc_read_page_finish(docg3);
1199 return ret;
1200}
1201
1202/**
1203 * doc_guess_autoecc - Guess autoecc mode from mbd_oob_ops
1204 * @ops: the oob operations
1205 *
1206 * Returns 0 or 1 if success, -EINVAL if invalid oob mode
1207 */
1208static int doc_guess_autoecc(struct mtd_oob_ops *ops)
1209{
1210 int autoecc;
1211
1212 switch (ops->mode) {
1213 case MTD_OPS_PLACE_OOB:
1214 case MTD_OPS_AUTO_OOB:
1215 autoecc = 1;
1216 break;
1217 case MTD_OPS_RAW:
1218 autoecc = 0;
1219 break;
1220 default:
1221 autoecc = -EINVAL;
1222 }
1223 return autoecc;
1224}
1225
1226/**
1227 * doc_fill_autooob - Fill a 16 bytes OOB from 8 non-ECC bytes
1228 * @dst: the target 16 bytes OOB buffer
1229 * @oobsrc: the source 8 bytes non-ECC OOB buffer
1230 *
1231 */
1232static void doc_fill_autooob(u8 *dst, u8 *oobsrc)
1233{
1234 memcpy(dst, oobsrc, DOC_LAYOUT_OOB_PAGEINFO_SZ);
1235 dst[DOC_LAYOUT_OOB_UNUSED_OFS] = oobsrc[DOC_LAYOUT_OOB_PAGEINFO_SZ];
1236}
1237
1238/**
1239 * doc_backup_oob - Backup OOB into docg3 structure
1240 * @docg3: the device
1241 * @to: the page offset in the chip
1242 * @ops: the OOB size and buffer
1243 *
1244 * As the docg3 should write a page with its OOB in one pass, and some userland
1245 * applications do write_oob() to setup the OOB and then write(), store the OOB
1246 * into a temporary storage. This is very dangerous, as 2 concurrent
1247 * applications could store an OOB, and then write their pages (which will
1248 * result into one having its OOB corrupted).
1249 *
1250 * The only reliable way would be for userland to call doc_write_oob() with both
1251 * the page data _and_ the OOB area.
1252 *
1253 * Returns 0 if success, -EINVAL if ops content invalid
1254 */
1255static int doc_backup_oob(struct docg3 *docg3, loff_t to,
1256 struct mtd_oob_ops *ops)
1257{
1258 int ooblen = ops->ooblen, autoecc;
1259
1260 if (ooblen != DOC_LAYOUT_OOB_SIZE)
1261 return -EINVAL;
1262 autoecc = doc_guess_autoecc(ops);
1263 if (autoecc < 0)
1264 return autoecc;
1265
1266 docg3->oob_write_ofs = to;
1267 docg3->oob_autoecc = autoecc;
1268 if (ops->mode == MTD_OPS_AUTO_OOB) {
1269 doc_fill_autooob(docg3->oob_write_buf, ops->oobbuf);
1270 ops->oobretlen = 8;
1271 } else {
1272 memcpy(docg3->oob_write_buf, ops->oobbuf, DOC_LAYOUT_OOB_SIZE);
1273 ops->oobretlen = DOC_LAYOUT_OOB_SIZE;
1274 }
1275 return 0;
1276}
1277
1278/**
1279 * doc_write_oob - Write out of band bytes to flash
1280 * @mtd: the device
1281 * @ofs: the offset from first block and first page, in bytes, aligned on page
1282 * size
1283 * @ops: the mtd oob structure
1284 *
1285 * Either write OOB data into a temporary buffer, for the subsequent write
1286 * page. The provided OOB should be 16 bytes long. If a data buffer is provided
1287 * as well, issue the page write.
1288 * Or provide data without OOB, and then a all zeroed OOB will be used (ECC will
1289 * still be filled in if asked for).
1290 *
1291 * Returns 0 is successfull, EINVAL if length is not 14 bytes
1292 */
1293static int doc_write_oob(struct mtd_info *mtd, loff_t ofs,
1294 struct mtd_oob_ops *ops)
1295{
1296 struct docg3 *docg3 = mtd->priv;
1297 int block0, block1, page, ret, pofs = 0, autoecc, oobdelta;
1298 u8 *oobbuf = ops->oobbuf;
1299 u8 *buf = ops->datbuf;
1300 size_t len, ooblen;
1301 u8 oob[DOC_LAYOUT_OOB_SIZE];
1302
1303 if (buf)
1304 len = ops->len;
1305 else
1306 len = 0;
1307 if (oobbuf)
1308 ooblen = ops->ooblen;
1309 else
1310 ooblen = 0;
1311
1312 if (oobbuf && ops->mode == MTD_OPS_PLACE_OOB)
1313 oobbuf += ops->ooboffs;
1314
1315 doc_dbg("doc_write_oob(from=%lld, mode=%d, data=(%p:%zu), oob=(%p:%zu))\n",
1316 ofs, ops->mode, buf, len, oobbuf, ooblen);
1317 switch (ops->mode) {
1318 case MTD_OPS_PLACE_OOB:
1319 case MTD_OPS_RAW:
1320 oobdelta = mtd->oobsize;
1321 break;
1322 case MTD_OPS_AUTO_OOB:
1323 oobdelta = mtd->ecclayout->oobavail;
1324 break;
1325 default:
1326 oobdelta = 0;
1327 }
1328 if ((len % DOC_LAYOUT_PAGE_SIZE) || (ooblen % oobdelta) ||
1329 (ofs % DOC_LAYOUT_PAGE_SIZE))
1330 return -EINVAL;
1331 if (len && ooblen &&
1332 (len / DOC_LAYOUT_PAGE_SIZE) != (ooblen / oobdelta))
1333 return -EINVAL;
1334
1335 ret = -EINVAL;
1336 calc_block_sector(ofs + len, &block0, &block1, &page, &pofs);
1337 if (block1 > docg3->max_block)
1338 goto err;
1339
1340 ops->oobretlen = 0;
1341 ops->retlen = 0;
1342 ret = 0;
1343 if (len == 0 && ooblen == 0)
1344 return -EINVAL;
1345 if (len == 0 && ooblen > 0)
1346 return doc_backup_oob(docg3, ofs, ops);
1347
1348 autoecc = doc_guess_autoecc(ops);
1349 if (autoecc < 0)
1350 return autoecc;
1351
1352 while (!ret && len > 0) {
1353 memset(oob, 0, sizeof(oob));
1354 if (ofs == docg3->oob_write_ofs)
1355 memcpy(oob, docg3->oob_write_buf, DOC_LAYOUT_OOB_SIZE);
1356 else if (ooblen > 0 && ops->mode == MTD_OPS_AUTO_OOB)
1357 doc_fill_autooob(oob, oobbuf);
1358 else if (ooblen > 0)
1359 memcpy(oob, oobbuf, DOC_LAYOUT_OOB_SIZE);
1360 ret = doc_write_page(docg3, ofs, buf, oob, autoecc);
1361
1362 ofs += DOC_LAYOUT_PAGE_SIZE;
1363 len -= DOC_LAYOUT_PAGE_SIZE;
1364 buf += DOC_LAYOUT_PAGE_SIZE;
1365 if (ooblen) {
1366 oobbuf += oobdelta;
1367 ooblen -= oobdelta;
1368 ops->oobretlen += oobdelta;
1369 }
1370 ops->retlen += DOC_LAYOUT_PAGE_SIZE;
1371 }
1372err:
1373 doc_set_device_id(docg3, 0);
1374 return ret;
1375}
1376
1377/**
1378 * doc_write - Write a buffer to the chip
1379 * @mtd: the device
1380 * @to: the offset from first block and first page, in bytes, aligned on page
1381 * size
1382 * @len: the number of bytes to write (must be a full page size, ie. 512)
1383 * @retlen: the number of bytes actually written (0 or 512)
1384 * @buf: the buffer to get bytes from
1385 *
1386 * Writes data to the chip.
1387 *
1388 * Returns 0 if write successful, -EIO if write error
1389 */
1390static int doc_write(struct mtd_info *mtd, loff_t to, size_t len,
1391 size_t *retlen, const u_char *buf)
1392{
1393 struct docg3 *docg3 = mtd->priv;
1394 int ret;
1395 struct mtd_oob_ops ops;
1396
1397 doc_dbg("doc_write(to=%lld, len=%zu)\n", to, len);
1398 ops.datbuf = (char *)buf;
1399 ops.len = len;
1400 ops.mode = MTD_OPS_PLACE_OOB;
1401 ops.oobbuf = NULL;
1402 ops.ooblen = 0;
1403 ops.ooboffs = 0;
1404
1405 ret = doc_write_oob(mtd, to, &ops);
1406 *retlen = ops.retlen;
1407 return ret;
1408}
1409
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02001410/*
1411 * Debug sysfs entries
1412 */
1413static int dbg_flashctrl_show(struct seq_file *s, void *p)
1414{
1415 struct docg3 *docg3 = (struct docg3 *)s->private;
1416
1417 int pos = 0;
1418 u8 fctrl = doc_register_readb(docg3, DOC_FLASHCONTROL);
1419
1420 pos += seq_printf(s,
1421 "FlashControl : 0x%02x (%s,CE# %s,%s,%s,flash %s)\n",
1422 fctrl,
1423 fctrl & DOC_CTRL_VIOLATION ? "protocol violation" : "-",
1424 fctrl & DOC_CTRL_CE ? "active" : "inactive",
1425 fctrl & DOC_CTRL_PROTECTION_ERROR ? "protection error" : "-",
1426 fctrl & DOC_CTRL_SEQUENCE_ERROR ? "sequence error" : "-",
1427 fctrl & DOC_CTRL_FLASHREADY ? "ready" : "not ready");
1428 return pos;
1429}
1430DEBUGFS_RO_ATTR(flashcontrol, dbg_flashctrl_show);
1431
1432static int dbg_asicmode_show(struct seq_file *s, void *p)
1433{
1434 struct docg3 *docg3 = (struct docg3 *)s->private;
1435
1436 int pos = 0;
1437 int pctrl = doc_register_readb(docg3, DOC_ASICMODE);
1438 int mode = pctrl & 0x03;
1439
1440 pos += seq_printf(s,
1441 "%04x : RAM_WE=%d,RSTIN_RESET=%d,BDETCT_RESET=%d,WRITE_ENABLE=%d,POWERDOWN=%d,MODE=%d%d (",
1442 pctrl,
1443 pctrl & DOC_ASICMODE_RAM_WE ? 1 : 0,
1444 pctrl & DOC_ASICMODE_RSTIN_RESET ? 1 : 0,
1445 pctrl & DOC_ASICMODE_BDETCT_RESET ? 1 : 0,
1446 pctrl & DOC_ASICMODE_MDWREN ? 1 : 0,
1447 pctrl & DOC_ASICMODE_POWERDOWN ? 1 : 0,
1448 mode >> 1, mode & 0x1);
1449
1450 switch (mode) {
1451 case DOC_ASICMODE_RESET:
1452 pos += seq_printf(s, "reset");
1453 break;
1454 case DOC_ASICMODE_NORMAL:
1455 pos += seq_printf(s, "normal");
1456 break;
1457 case DOC_ASICMODE_POWERDOWN:
1458 pos += seq_printf(s, "powerdown");
1459 break;
1460 }
1461 pos += seq_printf(s, ")\n");
1462 return pos;
1463}
1464DEBUGFS_RO_ATTR(asic_mode, dbg_asicmode_show);
1465
1466static int dbg_device_id_show(struct seq_file *s, void *p)
1467{
1468 struct docg3 *docg3 = (struct docg3 *)s->private;
1469 int pos = 0;
1470 int id = doc_register_readb(docg3, DOC_DEVICESELECT);
1471
1472 pos += seq_printf(s, "DeviceId = %d\n", id);
1473 return pos;
1474}
1475DEBUGFS_RO_ATTR(device_id, dbg_device_id_show);
1476
1477static int dbg_protection_show(struct seq_file *s, void *p)
1478{
1479 struct docg3 *docg3 = (struct docg3 *)s->private;
1480 int pos = 0;
Robert Jarzmikdbc26d92011-11-19 16:02:45 +01001481 int protect, dps0, dps0_low, dps0_high, dps1, dps1_low, dps1_high;
1482
1483 protect = doc_register_readb(docg3, DOC_PROTECTION);
1484 dps0 = doc_register_readb(docg3, DOC_DPS0_STATUS);
1485 dps0_low = doc_register_readw(docg3, DOC_DPS0_ADDRLOW);
1486 dps0_high = doc_register_readw(docg3, DOC_DPS0_ADDRHIGH);
1487 dps1 = doc_register_readb(docg3, DOC_DPS1_STATUS);
1488 dps1_low = doc_register_readw(docg3, DOC_DPS1_ADDRLOW);
1489 dps1_high = doc_register_readw(docg3, DOC_DPS1_ADDRHIGH);
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02001490
1491 pos += seq_printf(s, "Protection = 0x%02x (",
1492 protect);
1493 if (protect & DOC_PROTECT_FOUNDRY_OTP_LOCK)
1494 pos += seq_printf(s, "FOUNDRY_OTP_LOCK,");
1495 if (protect & DOC_PROTECT_CUSTOMER_OTP_LOCK)
1496 pos += seq_printf(s, "CUSTOMER_OTP_LOCK,");
1497 if (protect & DOC_PROTECT_LOCK_INPUT)
1498 pos += seq_printf(s, "LOCK_INPUT,");
1499 if (protect & DOC_PROTECT_STICKY_LOCK)
1500 pos += seq_printf(s, "STICKY_LOCK,");
1501 if (protect & DOC_PROTECT_PROTECTION_ENABLED)
1502 pos += seq_printf(s, "PROTECTION ON,");
1503 if (protect & DOC_PROTECT_IPL_DOWNLOAD_LOCK)
1504 pos += seq_printf(s, "IPL_DOWNLOAD_LOCK,");
1505 if (protect & DOC_PROTECT_PROTECTION_ERROR)
1506 pos += seq_printf(s, "PROTECT_ERR,");
1507 else
1508 pos += seq_printf(s, "NO_PROTECT_ERR");
1509 pos += seq_printf(s, ")\n");
1510
1511 pos += seq_printf(s, "DPS0 = 0x%02x : "
1512 "Protected area [0x%x - 0x%x] : OTP=%d, READ=%d, "
1513 "WRITE=%d, HW_LOCK=%d, KEY_OK=%d\n",
1514 dps0, dps0_low, dps0_high,
1515 !!(dps0 & DOC_DPS_OTP_PROTECTED),
1516 !!(dps0 & DOC_DPS_READ_PROTECTED),
1517 !!(dps0 & DOC_DPS_WRITE_PROTECTED),
1518 !!(dps0 & DOC_DPS_HW_LOCK_ENABLED),
1519 !!(dps0 & DOC_DPS_KEY_OK));
1520 pos += seq_printf(s, "DPS1 = 0x%02x : "
1521 "Protected area [0x%x - 0x%x] : OTP=%d, READ=%d, "
1522 "WRITE=%d, HW_LOCK=%d, KEY_OK=%d\n",
1523 dps1, dps1_low, dps1_high,
1524 !!(dps1 & DOC_DPS_OTP_PROTECTED),
1525 !!(dps1 & DOC_DPS_READ_PROTECTED),
1526 !!(dps1 & DOC_DPS_WRITE_PROTECTED),
1527 !!(dps1 & DOC_DPS_HW_LOCK_ENABLED),
1528 !!(dps1 & DOC_DPS_KEY_OK));
1529 return pos;
1530}
1531DEBUGFS_RO_ATTR(protection, dbg_protection_show);
1532
1533static int __init doc_dbg_register(struct docg3 *docg3)
1534{
1535 struct dentry *root, *entry;
1536
1537 root = debugfs_create_dir("docg3", NULL);
1538 if (!root)
1539 return -ENOMEM;
1540
1541 entry = debugfs_create_file("flashcontrol", S_IRUSR, root, docg3,
1542 &flashcontrol_fops);
1543 if (entry)
1544 entry = debugfs_create_file("asic_mode", S_IRUSR, root,
1545 docg3, &asic_mode_fops);
1546 if (entry)
1547 entry = debugfs_create_file("device_id", S_IRUSR, root,
1548 docg3, &device_id_fops);
1549 if (entry)
1550 entry = debugfs_create_file("protection", S_IRUSR, root,
1551 docg3, &protection_fops);
1552 if (entry) {
1553 docg3->debugfs_root = root;
1554 return 0;
1555 } else {
1556 debugfs_remove_recursive(root);
1557 return -ENOMEM;
1558 }
1559}
1560
1561static void __exit doc_dbg_unregister(struct docg3 *docg3)
1562{
1563 debugfs_remove_recursive(docg3->debugfs_root);
1564}
1565
1566/**
1567 * doc_set_driver_info - Fill the mtd_info structure and docg3 structure
1568 * @chip_id: The chip ID of the supported chip
1569 * @mtd: The structure to fill
1570 */
1571static void __init doc_set_driver_info(int chip_id, struct mtd_info *mtd)
1572{
1573 struct docg3 *docg3 = mtd->priv;
1574 int cfg;
1575
1576 cfg = doc_register_readb(docg3, DOC_CONFIGURATION);
1577 docg3->if_cfg = (cfg & DOC_CONF_IF_CFG ? 1 : 0);
1578
1579 switch (chip_id) {
1580 case DOC_CHIPID_G3:
Robert Jarzmikae9d4932011-11-19 16:02:48 +01001581 mtd->name = kasprintf(GFP_KERNEL, "DiskOnChip G3 floor %d",
1582 docg3->device_id);
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02001583 docg3->max_block = 2047;
1584 break;
1585 }
1586 mtd->type = MTD_NANDFLASH;
Robert Jarzmik7a7fcf12011-11-19 16:02:54 +01001587 mtd->flags = MTD_CAP_NANDFLASH;
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02001588 mtd->size = (docg3->max_block + 1) * DOC_LAYOUT_BLOCK_SIZE;
1589 mtd->erasesize = DOC_LAYOUT_BLOCK_SIZE * DOC_LAYOUT_NBPLANES;
1590 mtd->writesize = DOC_LAYOUT_PAGE_SIZE;
1591 mtd->oobsize = DOC_LAYOUT_OOB_SIZE;
1592 mtd->owner = THIS_MODULE;
Robert Jarzmik7a7fcf12011-11-19 16:02:54 +01001593 mtd->erase = doc_erase;
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02001594 mtd->point = NULL;
1595 mtd->unpoint = NULL;
1596 mtd->read = doc_read;
Robert Jarzmik7a7fcf12011-11-19 16:02:54 +01001597 mtd->write = doc_write;
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02001598 mtd->read_oob = doc_read_oob;
Robert Jarzmik7a7fcf12011-11-19 16:02:54 +01001599 mtd->write_oob = doc_write_oob;
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02001600 mtd->sync = NULL;
1601 mtd->block_isbad = doc_block_isbad;
Robert Jarzmik732b63b2011-11-19 16:02:49 +01001602 mtd->ecclayout = &docg3_oobinfo;
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02001603}
1604
1605/**
Robert Jarzmikae9d4932011-11-19 16:02:48 +01001606 * doc_probe_device - Check if a device is available
1607 * @base: the io space where the device is probed
1608 * @floor: the floor of the probed device
1609 * @dev: the device
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02001610 *
Robert Jarzmikae9d4932011-11-19 16:02:48 +01001611 * Checks whether a device at the specified IO range, and floor is available.
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02001612 *
Robert Jarzmikae9d4932011-11-19 16:02:48 +01001613 * Returns a mtd_info struct if there is a device, ENODEV if none found, ENOMEM
1614 * if a memory allocation failed. If floor 0 is checked, a reset of the ASIC is
1615 * launched.
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02001616 */
Robert Jarzmikae9d4932011-11-19 16:02:48 +01001617static struct mtd_info *doc_probe_device(void __iomem *base, int floor,
1618 struct device *dev)
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02001619{
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02001620 int ret, bbt_nbpages;
1621 u16 chip_id, chip_id_inv;
Robert Jarzmikae9d4932011-11-19 16:02:48 +01001622 struct docg3 *docg3;
1623 struct mtd_info *mtd;
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02001624
1625 ret = -ENOMEM;
1626 docg3 = kzalloc(sizeof(struct docg3), GFP_KERNEL);
1627 if (!docg3)
1628 goto nomem1;
1629 mtd = kzalloc(sizeof(struct mtd_info), GFP_KERNEL);
1630 if (!mtd)
1631 goto nomem2;
1632 mtd->priv = docg3;
Robert Jarzmikae9d4932011-11-19 16:02:48 +01001633 bbt_nbpages = DIV_ROUND_UP(docg3->max_block + 1,
1634 8 * DOC_LAYOUT_PAGE_SIZE);
1635 docg3->bbt = kzalloc(bbt_nbpages * DOC_LAYOUT_PAGE_SIZE, GFP_KERNEL);
1636 if (!docg3->bbt)
1637 goto nomem3;
1638
1639 docg3->dev = dev;
1640 docg3->device_id = floor;
1641 docg3->base = base;
1642 doc_set_device_id(docg3, docg3->device_id);
1643 if (!floor)
1644 doc_set_asic_mode(docg3, DOC_ASICMODE_RESET);
1645 doc_set_asic_mode(docg3, DOC_ASICMODE_NORMAL);
1646
1647 chip_id = doc_register_readw(docg3, DOC_CHIPID);
1648 chip_id_inv = doc_register_readw(docg3, DOC_CHIPID_INV);
1649
1650 ret = 0;
1651 if (chip_id != (u16)(~chip_id_inv)) {
1652 goto nomem3;
1653 }
1654
1655 switch (chip_id) {
1656 case DOC_CHIPID_G3:
1657 doc_info("Found a G3 DiskOnChip at addr %p, floor %d\n",
1658 base, floor);
1659 break;
1660 default:
1661 doc_err("Chip id %04x is not a DiskOnChip G3 chip\n", chip_id);
1662 goto nomem3;
1663 }
1664
1665 doc_set_driver_info(chip_id, mtd);
1666
Robert Jarzmikfb50b582011-11-19 16:02:52 +01001667 doc_hamming_ecc_init(docg3, DOC_LAYOUT_OOB_PAGEINFO_SZ);
Robert Jarzmikae9d4932011-11-19 16:02:48 +01001668 doc_reload_bbt(docg3);
1669 return mtd;
1670
1671nomem3:
1672 kfree(mtd);
1673nomem2:
1674 kfree(docg3);
1675nomem1:
1676 return ERR_PTR(ret);
1677}
1678
1679/**
1680 * doc_release_device - Release a docg3 floor
1681 * @mtd: the device
1682 */
1683static void doc_release_device(struct mtd_info *mtd)
1684{
1685 struct docg3 *docg3 = mtd->priv;
1686
1687 mtd_device_unregister(mtd);
1688 kfree(docg3->bbt);
1689 kfree(docg3);
1690 kfree(mtd->name);
1691 kfree(mtd);
1692}
1693
1694/**
1695 * doc_probe - Probe the IO space for a DiskOnChip G3 chip
1696 * @pdev: platform device
1697 *
1698 * Probes for a G3 chip at the specified IO space in the platform data
1699 * ressources. The floor 0 must be available.
1700 *
1701 * Returns 0 on success, -ENOMEM, -ENXIO on error
1702 */
1703static int __init docg3_probe(struct platform_device *pdev)
1704{
1705 struct device *dev = &pdev->dev;
1706 struct mtd_info *mtd;
1707 struct resource *ress;
1708 void __iomem *base;
1709 int ret, floor, found = 0;
1710 struct mtd_info **docg3_floors;
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02001711
1712 ret = -ENXIO;
1713 ress = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1714 if (!ress) {
1715 dev_err(dev, "No I/O memory resource defined\n");
1716 goto noress;
1717 }
Robert Jarzmikae9d4932011-11-19 16:02:48 +01001718 base = ioremap(ress->start, DOC_IOSPACE_SIZE);
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02001719
1720 ret = -ENOMEM;
Robert Jarzmikae9d4932011-11-19 16:02:48 +01001721 docg3_floors = kzalloc(sizeof(*docg3_floors) * DOC_MAX_NBFLOORS,
1722 GFP_KERNEL);
1723 if (!docg3_floors)
1724 goto nomem;
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02001725
Robert Jarzmikae9d4932011-11-19 16:02:48 +01001726 ret = 0;
1727 for (floor = 0; floor < DOC_MAX_NBFLOORS; floor++) {
1728 mtd = doc_probe_device(base, floor, dev);
1729 if (floor == 0 && !mtd)
1730 goto notfound;
1731 if (!IS_ERR_OR_NULL(mtd))
1732 ret = mtd_device_parse_register(mtd, part_probes,
1733 NULL, NULL, 0);
1734 else
1735 ret = PTR_ERR(mtd);
1736 docg3_floors[floor] = mtd;
1737 if (ret)
1738 goto err_probe;
1739 if (mtd)
1740 found++;
1741 }
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02001742
Robert Jarzmikae9d4932011-11-19 16:02:48 +01001743 if (!found)
1744 goto notfound;
1745
1746 platform_set_drvdata(pdev, docg3_floors);
1747 doc_dbg_register(docg3_floors[0]->priv);
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02001748 return 0;
1749
Robert Jarzmikae9d4932011-11-19 16:02:48 +01001750notfound:
1751 ret = -ENODEV;
1752 dev_info(dev, "No supported DiskOnChip found\n");
1753err_probe:
1754 for (floor = 0; floor < DOC_MAX_NBFLOORS; floor++)
1755 if (docg3_floors[floor])
1756 doc_release_device(docg3_floors[floor]);
1757nomem:
1758 iounmap(base);
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02001759noress:
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02001760 return ret;
1761}
1762
1763/**
1764 * docg3_release - Release the driver
1765 * @pdev: the platform device
1766 *
1767 * Returns 0
1768 */
1769static int __exit docg3_release(struct platform_device *pdev)
1770{
Robert Jarzmikae9d4932011-11-19 16:02:48 +01001771 struct mtd_info **docg3_floors = platform_get_drvdata(pdev);
1772 struct docg3 *docg3 = docg3_floors[0]->priv;
1773 void __iomem *base = docg3->base;
1774 int floor;
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02001775
1776 doc_dbg_unregister(docg3);
Robert Jarzmikae9d4932011-11-19 16:02:48 +01001777 for (floor = 0; floor < DOC_MAX_NBFLOORS; floor++)
1778 if (docg3_floors[floor])
1779 doc_release_device(docg3_floors[floor]);
1780
1781 kfree(docg3_floors);
1782 iounmap(base);
Robert Jarzmikefa2ca72011-10-05 15:22:34 +02001783 return 0;
1784}
1785
1786static struct platform_driver g3_driver = {
1787 .driver = {
1788 .name = "docg3",
1789 .owner = THIS_MODULE,
1790 },
1791 .remove = __exit_p(docg3_release),
1792};
1793
1794static int __init docg3_init(void)
1795{
1796 return platform_driver_probe(&g3_driver, docg3_probe);
1797}
1798module_init(docg3_init);
1799
1800
1801static void __exit docg3_exit(void)
1802{
1803 platform_driver_unregister(&g3_driver);
1804}
1805module_exit(docg3_exit);
1806
1807MODULE_LICENSE("GPL");
1808MODULE_AUTHOR("Robert Jarzmik <robert.jarzmik@free.fr>");
1809MODULE_DESCRIPTION("MTD driver for DiskOnChip G3");