spi-topcliff-pch: add recovery processing in case wait-event timeout
[zen-stable.git] / drivers / mtd / devices / docg3.c
blob690198fc807d004746823456f16c32250cbcc976
1 /*
2 * Handles the M-Systems DiskOnChip G3 chip
4 * Copyright (C) 2011 Robert Jarzmik
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.
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.
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
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 #include <linux/bitmap.h>
33 #include <linux/bitrev.h>
34 #include <linux/bch.h>
36 #include <linux/debugfs.h>
37 #include <linux/seq_file.h>
39 #define CREATE_TRACE_POINTS
40 #include "docg3.h"
43 * This driver handles the DiskOnChip G3 flash memory.
45 * As no specification is available from M-Systems/Sandisk, this drivers lacks
46 * several functions available on the chip, as :
47 * - IPL write
49 * The bus data width (8bits versus 16bits) is not handled (if_cfg flag), and
50 * the driver assumes a 16bits data bus.
52 * DocG3 relies on 2 ECC algorithms, which are handled in hardware :
53 * - a 1 byte Hamming code stored in the OOB for each page
54 * - a 7 bytes BCH code stored in the OOB for each page
55 * The BCH ECC is :
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
64 static unsigned int reliable_mode;
65 module_param(reliable_mode, uint, 0);
66 MODULE_PARM_DESC(reliable_mode, "Set the docg3 mode (0=normal MLC, 1=fast, "
67 "2=reliable) : MLC normal operations are in normal mode");
69 /**
70 * struct docg3_oobinfo - DiskOnChip G3 OOB layout
71 * @eccbytes: 8 bytes are used (1 for Hamming ECC, 7 for BCH ECC)
72 * @eccpos: ecc positions (byte 7 is Hamming ECC, byte 8-14 are BCH ECC)
73 * @oobfree: free pageinfo bytes (byte 0 until byte 6, byte 15
74 * @oobavail: 8 available bytes remaining after ECC toll
76 static struct nand_ecclayout docg3_oobinfo = {
77 .eccbytes = 8,
78 .eccpos = {7, 8, 9, 10, 11, 12, 13, 14},
79 .oobfree = {{0, 7}, {15, 1} },
80 .oobavail = 8,
83 /**
84 * struct docg3_bch - BCH engine
86 static struct bch_control *docg3_bch;
88 static inline u8 doc_readb(struct docg3 *docg3, u16 reg)
90 u8 val = readb(docg3->base + reg);
92 trace_docg3_io(0, 8, reg, (int)val);
93 return val;
96 static inline u16 doc_readw(struct docg3 *docg3, u16 reg)
98 u16 val = readw(docg3->base + reg);
100 trace_docg3_io(0, 16, reg, (int)val);
101 return val;
104 static inline void doc_writeb(struct docg3 *docg3, u8 val, u16 reg)
106 writeb(val, docg3->base + reg);
107 trace_docg3_io(1, 8, reg, val);
110 static inline void doc_writew(struct docg3 *docg3, u16 val, u16 reg)
112 writew(val, docg3->base + reg);
113 trace_docg3_io(1, 16, reg, val);
116 static inline void doc_flash_command(struct docg3 *docg3, u8 cmd)
118 doc_writeb(docg3, cmd, DOC_FLASHCOMMAND);
121 static inline void doc_flash_sequence(struct docg3 *docg3, u8 seq)
123 doc_writeb(docg3, seq, DOC_FLASHSEQUENCE);
126 static inline void doc_flash_address(struct docg3 *docg3, u8 addr)
128 doc_writeb(docg3, addr, DOC_FLASHADDRESS);
131 static char const *part_probes[] = { "cmdlinepart", "saftlpart", NULL };
133 static int doc_register_readb(struct docg3 *docg3, int reg)
135 u8 val;
137 doc_writew(docg3, reg, DOC_READADDRESS);
138 val = doc_readb(docg3, reg);
139 doc_vdbg("Read register %04x : %02x\n", reg, val);
140 return val;
143 static int doc_register_readw(struct docg3 *docg3, int reg)
145 u16 val;
147 doc_writew(docg3, reg, DOC_READADDRESS);
148 val = doc_readw(docg3, reg);
149 doc_vdbg("Read register %04x : %04x\n", reg, val);
150 return val;
154 * doc_delay - delay docg3 operations
155 * @docg3: the device
156 * @nbNOPs: the number of NOPs to issue
158 * As no specification is available, the right timings between chip commands are
159 * unknown. The only available piece of information are the observed nops on a
160 * working docg3 chip.
161 * Therefore, doc_delay relies on a busy loop of NOPs, instead of scheduler
162 * friendlier msleep() functions or blocking mdelay().
164 static void doc_delay(struct docg3 *docg3, int nbNOPs)
166 int i;
168 doc_vdbg("NOP x %d\n", nbNOPs);
169 for (i = 0; i < nbNOPs; i++)
170 doc_writeb(docg3, 0, DOC_NOP);
173 static int is_prot_seq_error(struct docg3 *docg3)
175 int ctrl;
177 ctrl = doc_register_readb(docg3, DOC_FLASHCONTROL);
178 return ctrl & (DOC_CTRL_PROTECTION_ERROR | DOC_CTRL_SEQUENCE_ERROR);
181 static int doc_is_ready(struct docg3 *docg3)
183 int ctrl;
185 ctrl = doc_register_readb(docg3, DOC_FLASHCONTROL);
186 return ctrl & DOC_CTRL_FLASHREADY;
189 static int doc_wait_ready(struct docg3 *docg3)
191 int maxWaitCycles = 100;
193 do {
194 doc_delay(docg3, 4);
195 cpu_relax();
196 } while (!doc_is_ready(docg3) && maxWaitCycles--);
197 doc_delay(docg3, 2);
198 if (maxWaitCycles > 0)
199 return 0;
200 else
201 return -EIO;
204 static int doc_reset_seq(struct docg3 *docg3)
206 int ret;
208 doc_writeb(docg3, 0x10, DOC_FLASHCONTROL);
209 doc_flash_sequence(docg3, DOC_SEQ_RESET);
210 doc_flash_command(docg3, DOC_CMD_RESET);
211 doc_delay(docg3, 2);
212 ret = doc_wait_ready(docg3);
214 doc_dbg("doc_reset_seq() -> isReady=%s\n", ret ? "false" : "true");
215 return ret;
219 * doc_read_data_area - Read data from data area
220 * @docg3: the device
221 * @buf: the buffer to fill in (might be NULL is dummy reads)
222 * @len: the length to read
223 * @first: first time read, DOC_READADDRESS should be set
225 * Reads bytes from flash data. Handles the single byte / even bytes reads.
227 static void doc_read_data_area(struct docg3 *docg3, void *buf, int len,
228 int first)
230 int i, cdr, len4;
231 u16 data16, *dst16;
232 u8 data8, *dst8;
234 doc_dbg("doc_read_data_area(buf=%p, len=%d)\n", buf, len);
235 cdr = len & 0x3;
236 len4 = len - cdr;
238 if (first)
239 doc_writew(docg3, DOC_IOSPACE_DATA, DOC_READADDRESS);
240 dst16 = buf;
241 for (i = 0; i < len4; i += 2) {
242 data16 = doc_readw(docg3, DOC_IOSPACE_DATA);
243 if (dst16) {
244 *dst16 = data16;
245 dst16++;
249 if (cdr) {
250 doc_writew(docg3, DOC_IOSPACE_DATA | DOC_READADDR_ONE_BYTE,
251 DOC_READADDRESS);
252 doc_delay(docg3, 1);
253 dst8 = (u8 *)dst16;
254 for (i = 0; i < cdr; i++) {
255 data8 = doc_readb(docg3, DOC_IOSPACE_DATA);
256 if (dst8) {
257 *dst8 = data8;
258 dst8++;
265 * doc_write_data_area - Write data into data area
266 * @docg3: the device
267 * @buf: the buffer to get input bytes from
268 * @len: the length to write
270 * Writes bytes into flash data. Handles the single byte / even bytes writes.
272 static void doc_write_data_area(struct docg3 *docg3, const void *buf, int len)
274 int i, cdr, len4;
275 u16 *src16;
276 u8 *src8;
278 doc_dbg("doc_write_data_area(buf=%p, len=%d)\n", buf, len);
279 cdr = len & 0x3;
280 len4 = len - cdr;
282 doc_writew(docg3, DOC_IOSPACE_DATA, DOC_READADDRESS);
283 src16 = (u16 *)buf;
284 for (i = 0; i < len4; i += 2) {
285 doc_writew(docg3, *src16, DOC_IOSPACE_DATA);
286 src16++;
289 src8 = (u8 *)src16;
290 for (i = 0; i < cdr; i++) {
291 doc_writew(docg3, DOC_IOSPACE_DATA | DOC_READADDR_ONE_BYTE,
292 DOC_READADDRESS);
293 doc_writeb(docg3, *src8, DOC_IOSPACE_DATA);
294 src8++;
299 * doc_set_data_mode - Sets the flash to normal or reliable data mode
300 * @docg3: the device
302 * The reliable data mode is a bit slower than the fast mode, but less errors
303 * occur. Entering the reliable mode cannot be done without entering the fast
304 * mode first.
306 * In reliable mode, pages 2*n and 2*n+1 are clones. Writing to page 0 of blocks
307 * (4,5) make the hardware write also to page 1 of blocks blocks(4,5). Reading
308 * from page 0 of blocks (4,5) or from page 1 of blocks (4,5) gives the same
309 * result, which is a logical and between bytes from page 0 and page 1 (which is
310 * consistent with the fact that writing to a page is _clearing_ bits of that
311 * page).
313 static void doc_set_reliable_mode(struct docg3 *docg3)
315 static char *strmode[] = { "normal", "fast", "reliable", "invalid" };
317 doc_dbg("doc_set_reliable_mode(%s)\n", strmode[docg3->reliable]);
318 switch (docg3->reliable) {
319 case 0:
320 break;
321 case 1:
322 doc_flash_sequence(docg3, DOC_SEQ_SET_FASTMODE);
323 doc_flash_command(docg3, DOC_CMD_FAST_MODE);
324 break;
325 case 2:
326 doc_flash_sequence(docg3, DOC_SEQ_SET_RELIABLEMODE);
327 doc_flash_command(docg3, DOC_CMD_FAST_MODE);
328 doc_flash_command(docg3, DOC_CMD_RELIABLE_MODE);
329 break;
330 default:
331 doc_err("doc_set_reliable_mode(): invalid mode\n");
332 break;
334 doc_delay(docg3, 2);
338 * doc_set_asic_mode - Set the ASIC mode
339 * @docg3: the device
340 * @mode: the mode
342 * The ASIC can work in 3 modes :
343 * - RESET: all registers are zeroed
344 * - NORMAL: receives and handles commands
345 * - POWERDOWN: minimal poweruse, flash parts shut off
347 static void doc_set_asic_mode(struct docg3 *docg3, u8 mode)
349 int i;
351 for (i = 0; i < 12; i++)
352 doc_readb(docg3, DOC_IOSPACE_IPL);
354 mode |= DOC_ASICMODE_MDWREN;
355 doc_dbg("doc_set_asic_mode(%02x)\n", mode);
356 doc_writeb(docg3, mode, DOC_ASICMODE);
357 doc_writeb(docg3, ~mode, DOC_ASICMODECONFIRM);
358 doc_delay(docg3, 1);
362 * doc_set_device_id - Sets the devices id for cascaded G3 chips
363 * @docg3: the device
364 * @id: the chip to select (amongst 0, 1, 2, 3)
366 * There can be 4 cascaded G3 chips. This function selects the one which will
367 * should be the active one.
369 static void doc_set_device_id(struct docg3 *docg3, int id)
371 u8 ctrl;
373 doc_dbg("doc_set_device_id(%d)\n", id);
374 doc_writeb(docg3, id, DOC_DEVICESELECT);
375 ctrl = doc_register_readb(docg3, DOC_FLASHCONTROL);
377 ctrl &= ~DOC_CTRL_VIOLATION;
378 ctrl |= DOC_CTRL_CE;
379 doc_writeb(docg3, ctrl, DOC_FLASHCONTROL);
383 * doc_set_extra_page_mode - Change flash page layout
384 * @docg3: the device
386 * Normally, the flash page is split into the data (512 bytes) and the out of
387 * band data (16 bytes). For each, 4 more bytes can be accessed, where the wear
388 * leveling counters are stored. To access this last area of 4 bytes, a special
389 * mode must be input to the flash ASIC.
391 * Returns 0 if no error occured, -EIO else.
393 static int doc_set_extra_page_mode(struct docg3 *docg3)
395 int fctrl;
397 doc_dbg("doc_set_extra_page_mode()\n");
398 doc_flash_sequence(docg3, DOC_SEQ_PAGE_SIZE_532);
399 doc_flash_command(docg3, DOC_CMD_PAGE_SIZE_532);
400 doc_delay(docg3, 2);
402 fctrl = doc_register_readb(docg3, DOC_FLASHCONTROL);
403 if (fctrl & (DOC_CTRL_PROTECTION_ERROR | DOC_CTRL_SEQUENCE_ERROR))
404 return -EIO;
405 else
406 return 0;
410 * doc_setup_addr_sector - Setup blocks/page/ofs address for one plane
411 * @docg3: the device
412 * @sector: the sector
414 static void doc_setup_addr_sector(struct docg3 *docg3, int sector)
416 doc_delay(docg3, 1);
417 doc_flash_address(docg3, sector & 0xff);
418 doc_flash_address(docg3, (sector >> 8) & 0xff);
419 doc_flash_address(docg3, (sector >> 16) & 0xff);
420 doc_delay(docg3, 1);
424 * doc_setup_writeaddr_sector - Setup blocks/page/ofs address for one plane
425 * @docg3: the device
426 * @sector: the sector
427 * @ofs: the offset in the page, between 0 and (512 + 16 + 512)
429 static void doc_setup_writeaddr_sector(struct docg3 *docg3, int sector, int ofs)
431 ofs = ofs >> 2;
432 doc_delay(docg3, 1);
433 doc_flash_address(docg3, ofs & 0xff);
434 doc_flash_address(docg3, sector & 0xff);
435 doc_flash_address(docg3, (sector >> 8) & 0xff);
436 doc_flash_address(docg3, (sector >> 16) & 0xff);
437 doc_delay(docg3, 1);
441 * doc_seek - Set both flash planes to the specified block, page for reading
442 * @docg3: the device
443 * @block0: the first plane block index
444 * @block1: the second plane block index
445 * @page: the page index within the block
446 * @wear: if true, read will occur on the 4 extra bytes of the wear area
447 * @ofs: offset in page to read
449 * Programs the flash even and odd planes to the specific block and page.
450 * Alternatively, programs the flash to the wear area of the specified page.
452 static int doc_read_seek(struct docg3 *docg3, int block0, int block1, int page,
453 int wear, int ofs)
455 int sector, ret = 0;
457 doc_dbg("doc_seek(blocks=(%d,%d), page=%d, ofs=%d, wear=%d)\n",
458 block0, block1, page, ofs, wear);
460 if (!wear && (ofs < 2 * DOC_LAYOUT_PAGE_SIZE)) {
461 doc_flash_sequence(docg3, DOC_SEQ_SET_PLANE1);
462 doc_flash_command(docg3, DOC_CMD_READ_PLANE1);
463 doc_delay(docg3, 2);
464 } else {
465 doc_flash_sequence(docg3, DOC_SEQ_SET_PLANE2);
466 doc_flash_command(docg3, DOC_CMD_READ_PLANE2);
467 doc_delay(docg3, 2);
470 doc_set_reliable_mode(docg3);
471 if (wear)
472 ret = doc_set_extra_page_mode(docg3);
473 if (ret)
474 goto out;
476 doc_flash_sequence(docg3, DOC_SEQ_READ);
477 sector = (block0 << DOC_ADDR_BLOCK_SHIFT) + (page & DOC_ADDR_PAGE_MASK);
478 doc_flash_command(docg3, DOC_CMD_PROG_BLOCK_ADDR);
479 doc_setup_addr_sector(docg3, sector);
481 sector = (block1 << DOC_ADDR_BLOCK_SHIFT) + (page & DOC_ADDR_PAGE_MASK);
482 doc_flash_command(docg3, DOC_CMD_PROG_BLOCK_ADDR);
483 doc_setup_addr_sector(docg3, sector);
484 doc_delay(docg3, 1);
486 out:
487 return ret;
491 * doc_write_seek - Set both flash planes to the specified block, page for writing
492 * @docg3: the device
493 * @block0: the first plane block index
494 * @block1: the second plane block index
495 * @page: the page index within the block
496 * @ofs: offset in page to write
498 * Programs the flash even and odd planes to the specific block and page.
499 * Alternatively, programs the flash to the wear area of the specified page.
501 static int doc_write_seek(struct docg3 *docg3, int block0, int block1, int page,
502 int ofs)
504 int ret = 0, sector;
506 doc_dbg("doc_write_seek(blocks=(%d,%d), page=%d, ofs=%d)\n",
507 block0, block1, page, ofs);
509 doc_set_reliable_mode(docg3);
511 if (ofs < 2 * DOC_LAYOUT_PAGE_SIZE) {
512 doc_flash_sequence(docg3, DOC_SEQ_SET_PLANE1);
513 doc_flash_command(docg3, DOC_CMD_READ_PLANE1);
514 doc_delay(docg3, 2);
515 } else {
516 doc_flash_sequence(docg3, DOC_SEQ_SET_PLANE2);
517 doc_flash_command(docg3, DOC_CMD_READ_PLANE2);
518 doc_delay(docg3, 2);
521 doc_flash_sequence(docg3, DOC_SEQ_PAGE_SETUP);
522 doc_flash_command(docg3, DOC_CMD_PROG_CYCLE1);
524 sector = (block0 << DOC_ADDR_BLOCK_SHIFT) + (page & DOC_ADDR_PAGE_MASK);
525 doc_setup_writeaddr_sector(docg3, sector, ofs);
527 doc_flash_command(docg3, DOC_CMD_PROG_CYCLE3);
528 doc_delay(docg3, 2);
529 ret = doc_wait_ready(docg3);
530 if (ret)
531 goto out;
533 doc_flash_command(docg3, DOC_CMD_PROG_CYCLE1);
534 sector = (block1 << DOC_ADDR_BLOCK_SHIFT) + (page & DOC_ADDR_PAGE_MASK);
535 doc_setup_writeaddr_sector(docg3, sector, ofs);
536 doc_delay(docg3, 1);
538 out:
539 return ret;
544 * doc_read_page_ecc_init - Initialize hardware ECC engine
545 * @docg3: the device
546 * @len: the number of bytes covered by the ECC (BCH covered)
548 * The function does initialize the hardware ECC engine to compute the Hamming
549 * ECC (on 1 byte) and the BCH hardware ECC (on 7 bytes).
551 * Return 0 if succeeded, -EIO on error
553 static int doc_read_page_ecc_init(struct docg3 *docg3, int len)
555 doc_writew(docg3, DOC_ECCCONF0_READ_MODE
556 | DOC_ECCCONF0_BCH_ENABLE | DOC_ECCCONF0_HAMMING_ENABLE
557 | (len & DOC_ECCCONF0_DATA_BYTES_MASK),
558 DOC_ECCCONF0);
559 doc_delay(docg3, 4);
560 doc_register_readb(docg3, DOC_FLASHCONTROL);
561 return doc_wait_ready(docg3);
565 * doc_write_page_ecc_init - Initialize hardware BCH ECC engine
566 * @docg3: the device
567 * @len: the number of bytes covered by the ECC (BCH covered)
569 * The function does initialize the hardware ECC engine to compute the Hamming
570 * ECC (on 1 byte) and the BCH hardware ECC (on 7 bytes).
572 * Return 0 if succeeded, -EIO on error
574 static int doc_write_page_ecc_init(struct docg3 *docg3, int len)
576 doc_writew(docg3, DOC_ECCCONF0_WRITE_MODE
577 | DOC_ECCCONF0_BCH_ENABLE | DOC_ECCCONF0_HAMMING_ENABLE
578 | (len & DOC_ECCCONF0_DATA_BYTES_MASK),
579 DOC_ECCCONF0);
580 doc_delay(docg3, 4);
581 doc_register_readb(docg3, DOC_FLASHCONTROL);
582 return doc_wait_ready(docg3);
586 * doc_ecc_disable - Disable Hamming and BCH ECC hardware calculator
587 * @docg3: the device
589 * Disables the hardware ECC generator and checker, for unchecked reads (as when
590 * reading OOB only or write status byte).
592 static void doc_ecc_disable(struct docg3 *docg3)
594 doc_writew(docg3, DOC_ECCCONF0_READ_MODE, DOC_ECCCONF0);
595 doc_delay(docg3, 4);
599 * doc_hamming_ecc_init - Initialize hardware Hamming ECC engine
600 * @docg3: the device
601 * @nb_bytes: the number of bytes covered by the ECC (Hamming covered)
603 * This function programs the ECC hardware to compute the hamming code on the
604 * last provided N bytes to the hardware generator.
606 static void doc_hamming_ecc_init(struct docg3 *docg3, int nb_bytes)
608 u8 ecc_conf1;
610 ecc_conf1 = doc_register_readb(docg3, DOC_ECCCONF1);
611 ecc_conf1 &= ~DOC_ECCCONF1_HAMMING_BITS_MASK;
612 ecc_conf1 |= (nb_bytes & DOC_ECCCONF1_HAMMING_BITS_MASK);
613 doc_writeb(docg3, ecc_conf1, DOC_ECCCONF1);
617 * doc_ecc_bch_fix_data - Fix if need be read data from flash
618 * @docg3: the device
619 * @buf: the buffer of read data (512 + 7 + 1 bytes)
620 * @hwecc: the hardware calculated ECC.
621 * It's in fact recv_ecc ^ calc_ecc, where recv_ecc was read from OOB
622 * area data, and calc_ecc the ECC calculated by the hardware generator.
624 * Checks if the received data matches the ECC, and if an error is detected,
625 * tries to fix the bit flips (at most 4) in the buffer buf. As the docg3
626 * understands the (data, ecc, syndroms) in an inverted order in comparison to
627 * the BCH library, the function reverses the order of bits (ie. bit7 and bit0,
628 * bit6 and bit 1, ...) for all ECC data.
630 * The hardware ecc unit produces oob_ecc ^ calc_ecc. The kernel's bch
631 * algorithm is used to decode this. However the hw operates on page
632 * data in a bit order that is the reverse of that of the bch alg,
633 * requiring that the bits be reversed on the result. Thanks to Ivan
634 * Djelic for his analysis.
636 * Returns number of fixed bits (0, 1, 2, 3, 4) or -EBADMSG if too many bit
637 * errors were detected and cannot be fixed.
639 static int doc_ecc_bch_fix_data(struct docg3 *docg3, void *buf, u8 *hwecc)
641 u8 ecc[DOC_ECC_BCH_SIZE];
642 int errorpos[DOC_ECC_BCH_T], i, numerrs;
644 for (i = 0; i < DOC_ECC_BCH_SIZE; i++)
645 ecc[i] = bitrev8(hwecc[i]);
646 numerrs = decode_bch(docg3_bch, NULL, DOC_ECC_BCH_COVERED_BYTES,
647 NULL, ecc, NULL, errorpos);
648 BUG_ON(numerrs == -EINVAL);
649 if (numerrs < 0)
650 goto out;
652 for (i = 0; i < numerrs; i++)
653 errorpos[i] = (errorpos[i] & ~7) | (7 - (errorpos[i] & 7));
654 for (i = 0; i < numerrs; i++)
655 if (errorpos[i] < DOC_ECC_BCH_COVERED_BYTES*8)
656 /* error is located in data, correct it */
657 change_bit(errorpos[i], buf);
658 out:
659 doc_dbg("doc_ecc_bch_fix_data: flipped %d bits\n", numerrs);
660 return numerrs;
665 * doc_read_page_prepare - Prepares reading data from a flash page
666 * @docg3: the device
667 * @block0: the first plane block index on flash memory
668 * @block1: the second plane block index on flash memory
669 * @page: the page index in the block
670 * @offset: the offset in the page (must be a multiple of 4)
672 * Prepares the page to be read in the flash memory :
673 * - tell ASIC to map the flash pages
674 * - tell ASIC to be in read mode
676 * After a call to this method, a call to doc_read_page_finish is mandatory,
677 * to end the read cycle of the flash.
679 * Read data from a flash page. The length to be read must be between 0 and
680 * (page_size + oob_size + wear_size), ie. 532, and a multiple of 4 (because
681 * the extra bytes reading is not implemented).
683 * As pages are grouped by 2 (in 2 planes), reading from a page must be done
684 * in two steps:
685 * - one read of 512 bytes at offset 0
686 * - one read of 512 bytes at offset 512 + 16
688 * Returns 0 if successful, -EIO if a read error occured.
690 static int doc_read_page_prepare(struct docg3 *docg3, int block0, int block1,
691 int page, int offset)
693 int wear_area = 0, ret = 0;
695 doc_dbg("doc_read_page_prepare(blocks=(%d,%d), page=%d, ofsInPage=%d)\n",
696 block0, block1, page, offset);
697 if (offset >= DOC_LAYOUT_WEAR_OFFSET)
698 wear_area = 1;
699 if (!wear_area && offset > (DOC_LAYOUT_PAGE_OOB_SIZE * 2))
700 return -EINVAL;
702 doc_set_device_id(docg3, docg3->device_id);
703 ret = doc_reset_seq(docg3);
704 if (ret)
705 goto err;
707 /* Program the flash address block and page */
708 ret = doc_read_seek(docg3, block0, block1, page, wear_area, offset);
709 if (ret)
710 goto err;
712 doc_flash_command(docg3, DOC_CMD_READ_ALL_PLANES);
713 doc_delay(docg3, 2);
714 doc_wait_ready(docg3);
716 doc_flash_command(docg3, DOC_CMD_SET_ADDR_READ);
717 doc_delay(docg3, 1);
718 if (offset >= DOC_LAYOUT_PAGE_SIZE * 2)
719 offset -= 2 * DOC_LAYOUT_PAGE_SIZE;
720 doc_flash_address(docg3, offset >> 2);
721 doc_delay(docg3, 1);
722 doc_wait_ready(docg3);
724 doc_flash_command(docg3, DOC_CMD_READ_FLASH);
726 return 0;
727 err:
728 doc_writeb(docg3, 0, DOC_DATAEND);
729 doc_delay(docg3, 2);
730 return -EIO;
734 * doc_read_page_getbytes - Reads bytes from a prepared page
735 * @docg3: the device
736 * @len: the number of bytes to be read (must be a multiple of 4)
737 * @buf: the buffer to be filled in
738 * @first: 1 if first time read, DOC_READADDRESS should be set
741 static int doc_read_page_getbytes(struct docg3 *docg3, int len, u_char *buf,
742 int first)
744 doc_read_data_area(docg3, buf, len, first);
745 doc_delay(docg3, 2);
746 return len;
750 * doc_write_page_putbytes - Writes bytes into a prepared page
751 * @docg3: the device
752 * @len: the number of bytes to be written
753 * @buf: the buffer of input bytes
756 static void doc_write_page_putbytes(struct docg3 *docg3, int len,
757 const u_char *buf)
759 doc_write_data_area(docg3, buf, len);
760 doc_delay(docg3, 2);
764 * doc_get_bch_hw_ecc - Get hardware calculated BCH ECC
765 * @docg3: the device
766 * @hwecc: the array of 7 integers where the hardware ecc will be stored
768 static void doc_get_bch_hw_ecc(struct docg3 *docg3, u8 *hwecc)
770 int i;
772 for (i = 0; i < DOC_ECC_BCH_SIZE; i++)
773 hwecc[i] = doc_register_readb(docg3, DOC_BCH_HW_ECC(i));
777 * doc_page_finish - Ends reading/writing of a flash page
778 * @docg3: the device
780 static void doc_page_finish(struct docg3 *docg3)
782 doc_writeb(docg3, 0, DOC_DATAEND);
783 doc_delay(docg3, 2);
787 * doc_read_page_finish - Ends reading of a flash page
788 * @docg3: the device
790 * As a side effect, resets the chip selector to 0. This ensures that after each
791 * read operation, the floor 0 is selected. Therefore, if the systems halts, the
792 * reboot will boot on floor 0, where the IPL is.
794 static void doc_read_page_finish(struct docg3 *docg3)
796 doc_page_finish(docg3);
797 doc_set_device_id(docg3, 0);
801 * calc_block_sector - Calculate blocks, pages and ofs.
803 * @from: offset in flash
804 * @block0: first plane block index calculated
805 * @block1: second plane block index calculated
806 * @page: page calculated
807 * @ofs: offset in page
808 * @reliable: 0 if docg3 in normal mode, 1 if docg3 in fast mode, 2 if docg3 in
809 * reliable mode.
811 * The calculation is based on the reliable/normal mode. In normal mode, the 64
812 * pages of a block are available. In reliable mode, as pages 2*n and 2*n+1 are
813 * clones, only 32 pages per block are available.
815 static void calc_block_sector(loff_t from, int *block0, int *block1, int *page,
816 int *ofs, int reliable)
818 uint sector, pages_biblock;
820 pages_biblock = DOC_LAYOUT_PAGES_PER_BLOCK * DOC_LAYOUT_NBPLANES;
821 if (reliable == 1 || reliable == 2)
822 pages_biblock /= 2;
824 sector = from / DOC_LAYOUT_PAGE_SIZE;
825 *block0 = sector / pages_biblock * DOC_LAYOUT_NBPLANES;
826 *block1 = *block0 + 1;
827 *page = sector % pages_biblock;
828 *page /= DOC_LAYOUT_NBPLANES;
829 if (reliable == 1 || reliable == 2)
830 *page *= 2;
831 if (sector % 2)
832 *ofs = DOC_LAYOUT_PAGE_OOB_SIZE;
833 else
834 *ofs = 0;
838 * doc_read_oob - Read out of band bytes from flash
839 * @mtd: the device
840 * @from: the offset from first block and first page, in bytes, aligned on page
841 * size
842 * @ops: the mtd oob structure
844 * Reads flash memory OOB area of pages.
846 * Returns 0 if read successfull, of -EIO, -EINVAL if an error occured
848 static int doc_read_oob(struct mtd_info *mtd, loff_t from,
849 struct mtd_oob_ops *ops)
851 struct docg3 *docg3 = mtd->priv;
852 int block0, block1, page, ret, ofs = 0;
853 u8 *oobbuf = ops->oobbuf;
854 u8 *buf = ops->datbuf;
855 size_t len, ooblen, nbdata, nboob;
856 u8 hwecc[DOC_ECC_BCH_SIZE], eccconf1;
858 if (buf)
859 len = ops->len;
860 else
861 len = 0;
862 if (oobbuf)
863 ooblen = ops->ooblen;
864 else
865 ooblen = 0;
867 if (oobbuf && ops->mode == MTD_OPS_PLACE_OOB)
868 oobbuf += ops->ooboffs;
870 doc_dbg("doc_read_oob(from=%lld, mode=%d, data=(%p:%zu), oob=(%p:%zu))\n",
871 from, ops->mode, buf, len, oobbuf, ooblen);
872 if ((len % DOC_LAYOUT_PAGE_SIZE) || (ooblen % DOC_LAYOUT_OOB_SIZE) ||
873 (from % DOC_LAYOUT_PAGE_SIZE))
874 return -EINVAL;
876 ret = -EINVAL;
877 calc_block_sector(from + len, &block0, &block1, &page, &ofs,
878 docg3->reliable);
879 if (block1 > docg3->max_block)
880 goto err;
882 ops->oobretlen = 0;
883 ops->retlen = 0;
884 ret = 0;
885 while (!ret && (len > 0 || ooblen > 0)) {
886 calc_block_sector(from, &block0, &block1, &page, &ofs,
887 docg3->reliable);
888 nbdata = min_t(size_t, len, (size_t)DOC_LAYOUT_PAGE_SIZE);
889 nboob = min_t(size_t, ooblen, (size_t)DOC_LAYOUT_OOB_SIZE);
890 ret = doc_read_page_prepare(docg3, block0, block1, page, ofs);
891 if (ret < 0)
892 goto err;
893 ret = doc_read_page_ecc_init(docg3, DOC_ECC_BCH_TOTAL_BYTES);
894 if (ret < 0)
895 goto err_in_read;
896 ret = doc_read_page_getbytes(docg3, nbdata, buf, 1);
897 if (ret < nbdata)
898 goto err_in_read;
899 doc_read_page_getbytes(docg3, DOC_LAYOUT_PAGE_SIZE - nbdata,
900 NULL, 0);
901 ret = doc_read_page_getbytes(docg3, nboob, oobbuf, 0);
902 if (ret < nboob)
903 goto err_in_read;
904 doc_read_page_getbytes(docg3, DOC_LAYOUT_OOB_SIZE - nboob,
905 NULL, 0);
907 doc_get_bch_hw_ecc(docg3, hwecc);
908 eccconf1 = doc_register_readb(docg3, DOC_ECCCONF1);
910 if (nboob >= DOC_LAYOUT_OOB_SIZE) {
911 doc_dbg("OOB - INFO: %02x:%02x:%02x:%02x:%02x:%02x:%02x\n",
912 oobbuf[0], oobbuf[1], oobbuf[2], oobbuf[3],
913 oobbuf[4], oobbuf[5], oobbuf[6]);
914 doc_dbg("OOB - HAMMING: %02x\n", oobbuf[7]);
915 doc_dbg("OOB - BCH_ECC: %02x:%02x:%02x:%02x:%02x:%02x:%02x\n",
916 oobbuf[8], oobbuf[9], oobbuf[10], oobbuf[11],
917 oobbuf[12], oobbuf[13], oobbuf[14]);
918 doc_dbg("OOB - UNUSED: %02x\n", oobbuf[15]);
920 doc_dbg("ECC checks: ECCConf1=%x\n", eccconf1);
921 doc_dbg("ECC HW_ECC: %02x:%02x:%02x:%02x:%02x:%02x:%02x\n",
922 hwecc[0], hwecc[1], hwecc[2], hwecc[3], hwecc[4],
923 hwecc[5], hwecc[6]);
925 ret = -EIO;
926 if (is_prot_seq_error(docg3))
927 goto err_in_read;
928 ret = 0;
929 if ((block0 >= DOC_LAYOUT_BLOCK_FIRST_DATA) &&
930 (eccconf1 & DOC_ECCCONF1_BCH_SYNDROM_ERR) &&
931 (eccconf1 & DOC_ECCCONF1_PAGE_IS_WRITTEN) &&
932 (ops->mode != MTD_OPS_RAW) &&
933 (nbdata == DOC_LAYOUT_PAGE_SIZE)) {
934 ret = doc_ecc_bch_fix_data(docg3, buf, hwecc);
935 if (ret < 0) {
936 mtd->ecc_stats.failed++;
937 ret = -EBADMSG;
939 if (ret > 0) {
940 mtd->ecc_stats.corrected += ret;
941 ret = -EUCLEAN;
945 doc_read_page_finish(docg3);
946 ops->retlen += nbdata;
947 ops->oobretlen += nboob;
948 buf += nbdata;
949 oobbuf += nboob;
950 len -= nbdata;
951 ooblen -= nboob;
952 from += DOC_LAYOUT_PAGE_SIZE;
955 return ret;
956 err_in_read:
957 doc_read_page_finish(docg3);
958 err:
959 return ret;
963 * doc_read - Read bytes from flash
964 * @mtd: the device
965 * @from: the offset from first block and first page, in bytes, aligned on page
966 * size
967 * @len: the number of bytes to read (must be a multiple of 4)
968 * @retlen: the number of bytes actually read
969 * @buf: the filled in buffer
971 * Reads flash memory pages. This function does not read the OOB chunk, but only
972 * the page data.
974 * Returns 0 if read successfull, of -EIO, -EINVAL if an error occured
976 static int doc_read(struct mtd_info *mtd, loff_t from, size_t len,
977 size_t *retlen, u_char *buf)
979 struct mtd_oob_ops ops;
980 size_t ret;
982 memset(&ops, 0, sizeof(ops));
983 ops.datbuf = buf;
984 ops.len = len;
985 ops.mode = MTD_OPS_AUTO_OOB;
987 ret = doc_read_oob(mtd, from, &ops);
988 *retlen = ops.retlen;
989 return ret;
992 static int doc_reload_bbt(struct docg3 *docg3)
994 int block = DOC_LAYOUT_BLOCK_BBT;
995 int ret = 0, nbpages, page;
996 u_char *buf = docg3->bbt;
998 nbpages = DIV_ROUND_UP(docg3->max_block + 1, 8 * DOC_LAYOUT_PAGE_SIZE);
999 for (page = 0; !ret && (page < nbpages); page++) {
1000 ret = doc_read_page_prepare(docg3, block, block + 1,
1001 page + DOC_LAYOUT_PAGE_BBT, 0);
1002 if (!ret)
1003 ret = doc_read_page_ecc_init(docg3,
1004 DOC_LAYOUT_PAGE_SIZE);
1005 if (!ret)
1006 doc_read_page_getbytes(docg3, DOC_LAYOUT_PAGE_SIZE,
1007 buf, 1);
1008 buf += DOC_LAYOUT_PAGE_SIZE;
1010 doc_read_page_finish(docg3);
1011 return ret;
1015 * doc_block_isbad - Checks whether a block is good or not
1016 * @mtd: the device
1017 * @from: the offset to find the correct block
1019 * Returns 1 if block is bad, 0 if block is good
1021 static int doc_block_isbad(struct mtd_info *mtd, loff_t from)
1023 struct docg3 *docg3 = mtd->priv;
1024 int block0, block1, page, ofs, is_good;
1026 calc_block_sector(from, &block0, &block1, &page, &ofs,
1027 docg3->reliable);
1028 doc_dbg("doc_block_isbad(from=%lld) => block=(%d,%d), page=%d, ofs=%d\n",
1029 from, block0, block1, page, ofs);
1031 if (block0 < DOC_LAYOUT_BLOCK_FIRST_DATA)
1032 return 0;
1033 if (block1 > docg3->max_block)
1034 return -EINVAL;
1036 is_good = docg3->bbt[block0 >> 3] & (1 << (block0 & 0x7));
1037 return !is_good;
1040 #if 0
1042 * doc_get_erase_count - Get block erase count
1043 * @docg3: the device
1044 * @from: the offset in which the block is.
1046 * Get the number of times a block was erased. The number is the maximum of
1047 * erase times between first and second plane (which should be equal normally).
1049 * Returns The number of erases, or -EINVAL or -EIO on error.
1051 static int doc_get_erase_count(struct docg3 *docg3, loff_t from)
1053 u8 buf[DOC_LAYOUT_WEAR_SIZE];
1054 int ret, plane1_erase_count, plane2_erase_count;
1055 int block0, block1, page, ofs;
1057 doc_dbg("doc_get_erase_count(from=%lld, buf=%p)\n", from, buf);
1058 if (from % DOC_LAYOUT_PAGE_SIZE)
1059 return -EINVAL;
1060 calc_block_sector(from, &block0, &block1, &page, &ofs, docg3->reliable);
1061 if (block1 > docg3->max_block)
1062 return -EINVAL;
1064 ret = doc_reset_seq(docg3);
1065 if (!ret)
1066 ret = doc_read_page_prepare(docg3, block0, block1, page,
1067 ofs + DOC_LAYOUT_WEAR_OFFSET);
1068 if (!ret)
1069 ret = doc_read_page_getbytes(docg3, DOC_LAYOUT_WEAR_SIZE,
1070 buf, 1);
1071 doc_read_page_finish(docg3);
1073 if (ret || (buf[0] != DOC_ERASE_MARK) || (buf[2] != DOC_ERASE_MARK))
1074 return -EIO;
1075 plane1_erase_count = (u8)(~buf[1]) | ((u8)(~buf[4]) << 8)
1076 | ((u8)(~buf[5]) << 16);
1077 plane2_erase_count = (u8)(~buf[3]) | ((u8)(~buf[6]) << 8)
1078 | ((u8)(~buf[7]) << 16);
1080 return max(plane1_erase_count, plane2_erase_count);
1082 #endif
1085 * doc_get_op_status - get erase/write operation status
1086 * @docg3: the device
1088 * Queries the status from the chip, and returns it
1090 * Returns the status (bits DOC_PLANES_STATUS_*)
1092 static int doc_get_op_status(struct docg3 *docg3)
1094 u8 status;
1096 doc_flash_sequence(docg3, DOC_SEQ_PLANES_STATUS);
1097 doc_flash_command(docg3, DOC_CMD_PLANES_STATUS);
1098 doc_delay(docg3, 5);
1100 doc_ecc_disable(docg3);
1101 doc_read_data_area(docg3, &status, 1, 1);
1102 return status;
1106 * doc_write_erase_wait_status - wait for write or erase completion
1107 * @docg3: the device
1109 * Wait for the chip to be ready again after erase or write operation, and check
1110 * erase/write status.
1112 * Returns 0 if erase successfull, -EIO if erase/write issue, -ETIMEOUT if
1113 * timeout
1115 static int doc_write_erase_wait_status(struct docg3 *docg3)
1117 int status, ret = 0;
1119 if (!doc_is_ready(docg3))
1120 usleep_range(3000, 3000);
1121 if (!doc_is_ready(docg3)) {
1122 doc_dbg("Timeout reached and the chip is still not ready\n");
1123 ret = -EAGAIN;
1124 goto out;
1127 status = doc_get_op_status(docg3);
1128 if (status & DOC_PLANES_STATUS_FAIL) {
1129 doc_dbg("Erase/Write failed on (a) plane(s), status = %x\n",
1130 status);
1131 ret = -EIO;
1134 out:
1135 doc_page_finish(docg3);
1136 return ret;
1140 * doc_erase_block - Erase a couple of blocks
1141 * @docg3: the device
1142 * @block0: the first block to erase (leftmost plane)
1143 * @block1: the second block to erase (rightmost plane)
1145 * Erase both blocks, and return operation status
1147 * Returns 0 if erase successful, -EIO if erase issue, -ETIMEOUT if chip not
1148 * ready for too long
1150 static int doc_erase_block(struct docg3 *docg3, int block0, int block1)
1152 int ret, sector;
1154 doc_dbg("doc_erase_block(blocks=(%d,%d))\n", block0, block1);
1155 ret = doc_reset_seq(docg3);
1156 if (ret)
1157 return -EIO;
1159 doc_set_reliable_mode(docg3);
1160 doc_flash_sequence(docg3, DOC_SEQ_ERASE);
1162 sector = block0 << DOC_ADDR_BLOCK_SHIFT;
1163 doc_flash_command(docg3, DOC_CMD_PROG_BLOCK_ADDR);
1164 doc_setup_addr_sector(docg3, sector);
1165 sector = block1 << DOC_ADDR_BLOCK_SHIFT;
1166 doc_flash_command(docg3, DOC_CMD_PROG_BLOCK_ADDR);
1167 doc_setup_addr_sector(docg3, sector);
1168 doc_delay(docg3, 1);
1170 doc_flash_command(docg3, DOC_CMD_ERASECYCLE2);
1171 doc_delay(docg3, 2);
1173 if (is_prot_seq_error(docg3)) {
1174 doc_err("Erase blocks %d,%d error\n", block0, block1);
1175 return -EIO;
1178 return doc_write_erase_wait_status(docg3);
1182 * doc_erase - Erase a portion of the chip
1183 * @mtd: the device
1184 * @info: the erase info
1186 * Erase a bunch of contiguous blocks, by pairs, as a "mtd" page of 1024 is
1187 * split into 2 pages of 512 bytes on 2 contiguous blocks.
1189 * Returns 0 if erase successful, -EINVAL if adressing error, -EIO if erase
1190 * issue
1192 static int doc_erase(struct mtd_info *mtd, struct erase_info *info)
1194 struct docg3 *docg3 = mtd->priv;
1195 uint64_t len;
1196 int block0, block1, page, ret, ofs = 0;
1198 doc_dbg("doc_erase(from=%lld, len=%lld\n", info->addr, info->len);
1199 doc_set_device_id(docg3, docg3->device_id);
1201 info->state = MTD_ERASE_PENDING;
1202 calc_block_sector(info->addr + info->len, &block0, &block1, &page,
1203 &ofs, docg3->reliable);
1204 ret = -EINVAL;
1205 if (block1 > docg3->max_block || page || ofs)
1206 goto reset_err;
1208 ret = 0;
1209 calc_block_sector(info->addr, &block0, &block1, &page, &ofs,
1210 docg3->reliable);
1211 doc_set_reliable_mode(docg3);
1212 for (len = info->len; !ret && len > 0; len -= mtd->erasesize) {
1213 info->state = MTD_ERASING;
1214 ret = doc_erase_block(docg3, block0, block1);
1215 block0 += 2;
1216 block1 += 2;
1219 if (ret)
1220 goto reset_err;
1222 info->state = MTD_ERASE_DONE;
1223 return 0;
1225 reset_err:
1226 info->state = MTD_ERASE_FAILED;
1227 return ret;
1231 * doc_write_page - Write a single page to the chip
1232 * @docg3: the device
1233 * @to: the offset from first block and first page, in bytes, aligned on page
1234 * size
1235 * @buf: buffer to get bytes from
1236 * @oob: buffer to get out of band bytes from (can be NULL if no OOB should be
1237 * written)
1238 * @autoecc: if 0, all 16 bytes from OOB are taken, regardless of HW Hamming or
1239 * BCH computations. If 1, only bytes 0-7 and byte 15 are taken,
1240 * remaining ones are filled with hardware Hamming and BCH
1241 * computations. Its value is not meaningfull is oob == NULL.
1243 * Write one full page (ie. 1 page split on two planes), of 512 bytes, with the
1244 * OOB data. The OOB ECC is automatically computed by the hardware Hamming and
1245 * BCH generator if autoecc is not null.
1247 * Returns 0 if write successful, -EIO if write error, -EAGAIN if timeout
1249 static int doc_write_page(struct docg3 *docg3, loff_t to, const u_char *buf,
1250 const u_char *oob, int autoecc)
1252 int block0, block1, page, ret, ofs = 0;
1253 u8 hwecc[DOC_ECC_BCH_SIZE], hamming;
1255 doc_dbg("doc_write_page(to=%lld)\n", to);
1256 calc_block_sector(to, &block0, &block1, &page, &ofs, docg3->reliable);
1258 doc_set_device_id(docg3, docg3->device_id);
1259 ret = doc_reset_seq(docg3);
1260 if (ret)
1261 goto err;
1263 /* Program the flash address block and page */
1264 ret = doc_write_seek(docg3, block0, block1, page, ofs);
1265 if (ret)
1266 goto err;
1268 doc_write_page_ecc_init(docg3, DOC_ECC_BCH_TOTAL_BYTES);
1269 doc_delay(docg3, 2);
1270 doc_write_page_putbytes(docg3, DOC_LAYOUT_PAGE_SIZE, buf);
1272 if (oob && autoecc) {
1273 doc_write_page_putbytes(docg3, DOC_LAYOUT_OOB_PAGEINFO_SZ, oob);
1274 doc_delay(docg3, 2);
1275 oob += DOC_LAYOUT_OOB_UNUSED_OFS;
1277 hamming = doc_register_readb(docg3, DOC_HAMMINGPARITY);
1278 doc_delay(docg3, 2);
1279 doc_write_page_putbytes(docg3, DOC_LAYOUT_OOB_HAMMING_SZ,
1280 &hamming);
1281 doc_delay(docg3, 2);
1283 doc_get_bch_hw_ecc(docg3, hwecc);
1284 doc_write_page_putbytes(docg3, DOC_LAYOUT_OOB_BCH_SZ, hwecc);
1285 doc_delay(docg3, 2);
1287 doc_write_page_putbytes(docg3, DOC_LAYOUT_OOB_UNUSED_SZ, oob);
1289 if (oob && !autoecc)
1290 doc_write_page_putbytes(docg3, DOC_LAYOUT_OOB_SIZE, oob);
1292 doc_delay(docg3, 2);
1293 doc_page_finish(docg3);
1294 doc_delay(docg3, 2);
1295 doc_flash_command(docg3, DOC_CMD_PROG_CYCLE2);
1296 doc_delay(docg3, 2);
1299 * The wait status will perform another doc_page_finish() call, but that
1300 * seems to please the docg3, so leave it.
1302 ret = doc_write_erase_wait_status(docg3);
1303 return ret;
1304 err:
1305 doc_read_page_finish(docg3);
1306 return ret;
1310 * doc_guess_autoecc - Guess autoecc mode from mbd_oob_ops
1311 * @ops: the oob operations
1313 * Returns 0 or 1 if success, -EINVAL if invalid oob mode
1315 static int doc_guess_autoecc(struct mtd_oob_ops *ops)
1317 int autoecc;
1319 switch (ops->mode) {
1320 case MTD_OPS_PLACE_OOB:
1321 case MTD_OPS_AUTO_OOB:
1322 autoecc = 1;
1323 break;
1324 case MTD_OPS_RAW:
1325 autoecc = 0;
1326 break;
1327 default:
1328 autoecc = -EINVAL;
1330 return autoecc;
1334 * doc_fill_autooob - Fill a 16 bytes OOB from 8 non-ECC bytes
1335 * @dst: the target 16 bytes OOB buffer
1336 * @oobsrc: the source 8 bytes non-ECC OOB buffer
1339 static void doc_fill_autooob(u8 *dst, u8 *oobsrc)
1341 memcpy(dst, oobsrc, DOC_LAYOUT_OOB_PAGEINFO_SZ);
1342 dst[DOC_LAYOUT_OOB_UNUSED_OFS] = oobsrc[DOC_LAYOUT_OOB_PAGEINFO_SZ];
1346 * doc_backup_oob - Backup OOB into docg3 structure
1347 * @docg3: the device
1348 * @to: the page offset in the chip
1349 * @ops: the OOB size and buffer
1351 * As the docg3 should write a page with its OOB in one pass, and some userland
1352 * applications do write_oob() to setup the OOB and then write(), store the OOB
1353 * into a temporary storage. This is very dangerous, as 2 concurrent
1354 * applications could store an OOB, and then write their pages (which will
1355 * result into one having its OOB corrupted).
1357 * The only reliable way would be for userland to call doc_write_oob() with both
1358 * the page data _and_ the OOB area.
1360 * Returns 0 if success, -EINVAL if ops content invalid
1362 static int doc_backup_oob(struct docg3 *docg3, loff_t to,
1363 struct mtd_oob_ops *ops)
1365 int ooblen = ops->ooblen, autoecc;
1367 if (ooblen != DOC_LAYOUT_OOB_SIZE)
1368 return -EINVAL;
1369 autoecc = doc_guess_autoecc(ops);
1370 if (autoecc < 0)
1371 return autoecc;
1373 docg3->oob_write_ofs = to;
1374 docg3->oob_autoecc = autoecc;
1375 if (ops->mode == MTD_OPS_AUTO_OOB) {
1376 doc_fill_autooob(docg3->oob_write_buf, ops->oobbuf);
1377 ops->oobretlen = 8;
1378 } else {
1379 memcpy(docg3->oob_write_buf, ops->oobbuf, DOC_LAYOUT_OOB_SIZE);
1380 ops->oobretlen = DOC_LAYOUT_OOB_SIZE;
1382 return 0;
1386 * doc_write_oob - Write out of band bytes to flash
1387 * @mtd: the device
1388 * @ofs: the offset from first block and first page, in bytes, aligned on page
1389 * size
1390 * @ops: the mtd oob structure
1392 * Either write OOB data into a temporary buffer, for the subsequent write
1393 * page. The provided OOB should be 16 bytes long. If a data buffer is provided
1394 * as well, issue the page write.
1395 * Or provide data without OOB, and then a all zeroed OOB will be used (ECC will
1396 * still be filled in if asked for).
1398 * Returns 0 is successfull, EINVAL if length is not 14 bytes
1400 static int doc_write_oob(struct mtd_info *mtd, loff_t ofs,
1401 struct mtd_oob_ops *ops)
1403 struct docg3 *docg3 = mtd->priv;
1404 int block0, block1, page, ret, pofs = 0, autoecc, oobdelta;
1405 u8 *oobbuf = ops->oobbuf;
1406 u8 *buf = ops->datbuf;
1407 size_t len, ooblen;
1408 u8 oob[DOC_LAYOUT_OOB_SIZE];
1410 if (buf)
1411 len = ops->len;
1412 else
1413 len = 0;
1414 if (oobbuf)
1415 ooblen = ops->ooblen;
1416 else
1417 ooblen = 0;
1419 if (oobbuf && ops->mode == MTD_OPS_PLACE_OOB)
1420 oobbuf += ops->ooboffs;
1422 doc_dbg("doc_write_oob(from=%lld, mode=%d, data=(%p:%zu), oob=(%p:%zu))\n",
1423 ofs, ops->mode, buf, len, oobbuf, ooblen);
1424 switch (ops->mode) {
1425 case MTD_OPS_PLACE_OOB:
1426 case MTD_OPS_RAW:
1427 oobdelta = mtd->oobsize;
1428 break;
1429 case MTD_OPS_AUTO_OOB:
1430 oobdelta = mtd->ecclayout->oobavail;
1431 break;
1432 default:
1433 oobdelta = 0;
1435 if ((len % DOC_LAYOUT_PAGE_SIZE) || (ooblen % oobdelta) ||
1436 (ofs % DOC_LAYOUT_PAGE_SIZE))
1437 return -EINVAL;
1438 if (len && ooblen &&
1439 (len / DOC_LAYOUT_PAGE_SIZE) != (ooblen / oobdelta))
1440 return -EINVAL;
1442 ret = -EINVAL;
1443 calc_block_sector(ofs + len, &block0, &block1, &page, &pofs,
1444 docg3->reliable);
1445 if (block1 > docg3->max_block)
1446 goto err;
1448 ops->oobretlen = 0;
1449 ops->retlen = 0;
1450 ret = 0;
1451 if (len == 0 && ooblen == 0)
1452 return -EINVAL;
1453 if (len == 0 && ooblen > 0)
1454 return doc_backup_oob(docg3, ofs, ops);
1456 autoecc = doc_guess_autoecc(ops);
1457 if (autoecc < 0)
1458 return autoecc;
1460 while (!ret && len > 0) {
1461 memset(oob, 0, sizeof(oob));
1462 if (ofs == docg3->oob_write_ofs)
1463 memcpy(oob, docg3->oob_write_buf, DOC_LAYOUT_OOB_SIZE);
1464 else if (ooblen > 0 && ops->mode == MTD_OPS_AUTO_OOB)
1465 doc_fill_autooob(oob, oobbuf);
1466 else if (ooblen > 0)
1467 memcpy(oob, oobbuf, DOC_LAYOUT_OOB_SIZE);
1468 ret = doc_write_page(docg3, ofs, buf, oob, autoecc);
1470 ofs += DOC_LAYOUT_PAGE_SIZE;
1471 len -= DOC_LAYOUT_PAGE_SIZE;
1472 buf += DOC_LAYOUT_PAGE_SIZE;
1473 if (ooblen) {
1474 oobbuf += oobdelta;
1475 ooblen -= oobdelta;
1476 ops->oobretlen += oobdelta;
1478 ops->retlen += DOC_LAYOUT_PAGE_SIZE;
1480 err:
1481 doc_set_device_id(docg3, 0);
1482 return ret;
1486 * doc_write - Write a buffer to the chip
1487 * @mtd: the device
1488 * @to: the offset from first block and first page, in bytes, aligned on page
1489 * size
1490 * @len: the number of bytes to write (must be a full page size, ie. 512)
1491 * @retlen: the number of bytes actually written (0 or 512)
1492 * @buf: the buffer to get bytes from
1494 * Writes data to the chip.
1496 * Returns 0 if write successful, -EIO if write error
1498 static int doc_write(struct mtd_info *mtd, loff_t to, size_t len,
1499 size_t *retlen, const u_char *buf)
1501 struct docg3 *docg3 = mtd->priv;
1502 int ret;
1503 struct mtd_oob_ops ops;
1505 doc_dbg("doc_write(to=%lld, len=%zu)\n", to, len);
1506 ops.datbuf = (char *)buf;
1507 ops.len = len;
1508 ops.mode = MTD_OPS_PLACE_OOB;
1509 ops.oobbuf = NULL;
1510 ops.ooblen = 0;
1511 ops.ooboffs = 0;
1513 ret = doc_write_oob(mtd, to, &ops);
1514 *retlen = ops.retlen;
1515 return ret;
1518 static struct docg3 *sysfs_dev2docg3(struct device *dev,
1519 struct device_attribute *attr)
1521 int floor;
1522 struct platform_device *pdev = to_platform_device(dev);
1523 struct mtd_info **docg3_floors = platform_get_drvdata(pdev);
1525 floor = attr->attr.name[1] - '0';
1526 if (floor < 0 || floor >= DOC_MAX_NBFLOORS)
1527 return NULL;
1528 else
1529 return docg3_floors[floor]->priv;
1532 static ssize_t dps0_is_key_locked(struct device *dev,
1533 struct device_attribute *attr, char *buf)
1535 struct docg3 *docg3 = sysfs_dev2docg3(dev, attr);
1536 int dps0;
1538 doc_set_device_id(docg3, docg3->device_id);
1539 dps0 = doc_register_readb(docg3, DOC_DPS0_STATUS);
1540 doc_set_device_id(docg3, 0);
1542 return sprintf(buf, "%d\n", !(dps0 & DOC_DPS_KEY_OK));
1545 static ssize_t dps1_is_key_locked(struct device *dev,
1546 struct device_attribute *attr, char *buf)
1548 struct docg3 *docg3 = sysfs_dev2docg3(dev, attr);
1549 int dps1;
1551 doc_set_device_id(docg3, docg3->device_id);
1552 dps1 = doc_register_readb(docg3, DOC_DPS1_STATUS);
1553 doc_set_device_id(docg3, 0);
1555 return sprintf(buf, "%d\n", !(dps1 & DOC_DPS_KEY_OK));
1558 static ssize_t dps0_insert_key(struct device *dev,
1559 struct device_attribute *attr,
1560 const char *buf, size_t count)
1562 struct docg3 *docg3 = sysfs_dev2docg3(dev, attr);
1563 int i;
1565 if (count != DOC_LAYOUT_DPS_KEY_LENGTH)
1566 return -EINVAL;
1568 doc_set_device_id(docg3, docg3->device_id);
1569 for (i = 0; i < DOC_LAYOUT_DPS_KEY_LENGTH; i++)
1570 doc_writeb(docg3, buf[i], DOC_DPS0_KEY);
1571 doc_set_device_id(docg3, 0);
1572 return count;
1575 static ssize_t dps1_insert_key(struct device *dev,
1576 struct device_attribute *attr,
1577 const char *buf, size_t count)
1579 struct docg3 *docg3 = sysfs_dev2docg3(dev, attr);
1580 int i;
1582 if (count != DOC_LAYOUT_DPS_KEY_LENGTH)
1583 return -EINVAL;
1585 doc_set_device_id(docg3, docg3->device_id);
1586 for (i = 0; i < DOC_LAYOUT_DPS_KEY_LENGTH; i++)
1587 doc_writeb(docg3, buf[i], DOC_DPS1_KEY);
1588 doc_set_device_id(docg3, 0);
1589 return count;
1592 #define FLOOR_SYSFS(id) { \
1593 __ATTR(f##id##_dps0_is_keylocked, S_IRUGO, dps0_is_key_locked, NULL), \
1594 __ATTR(f##id##_dps1_is_keylocked, S_IRUGO, dps1_is_key_locked, NULL), \
1595 __ATTR(f##id##_dps0_protection_key, S_IWUGO, NULL, dps0_insert_key), \
1596 __ATTR(f##id##_dps1_protection_key, S_IWUGO, NULL, dps1_insert_key), \
1599 static struct device_attribute doc_sys_attrs[DOC_MAX_NBFLOORS][4] = {
1600 FLOOR_SYSFS(0), FLOOR_SYSFS(1), FLOOR_SYSFS(2), FLOOR_SYSFS(3)
1603 static int doc_register_sysfs(struct platform_device *pdev,
1604 struct mtd_info **floors)
1606 int ret = 0, floor, i = 0;
1607 struct device *dev = &pdev->dev;
1609 for (floor = 0; !ret && floor < DOC_MAX_NBFLOORS && floors[floor];
1610 floor++)
1611 for (i = 0; !ret && i < 4; i++)
1612 ret = device_create_file(dev, &doc_sys_attrs[floor][i]);
1613 if (!ret)
1614 return 0;
1615 do {
1616 while (--i >= 0)
1617 device_remove_file(dev, &doc_sys_attrs[floor][i]);
1618 i = 4;
1619 } while (--floor >= 0);
1620 return ret;
1623 static void doc_unregister_sysfs(struct platform_device *pdev,
1624 struct mtd_info **floors)
1626 struct device *dev = &pdev->dev;
1627 int floor, i;
1629 for (floor = 0; floor < DOC_MAX_NBFLOORS && floors[floor];
1630 floor++)
1631 for (i = 0; i < 4; i++)
1632 device_remove_file(dev, &doc_sys_attrs[floor][i]);
1636 * Debug sysfs entries
1638 static int dbg_flashctrl_show(struct seq_file *s, void *p)
1640 struct docg3 *docg3 = (struct docg3 *)s->private;
1642 int pos = 0;
1643 u8 fctrl = doc_register_readb(docg3, DOC_FLASHCONTROL);
1645 pos += seq_printf(s,
1646 "FlashControl : 0x%02x (%s,CE# %s,%s,%s,flash %s)\n",
1647 fctrl,
1648 fctrl & DOC_CTRL_VIOLATION ? "protocol violation" : "-",
1649 fctrl & DOC_CTRL_CE ? "active" : "inactive",
1650 fctrl & DOC_CTRL_PROTECTION_ERROR ? "protection error" : "-",
1651 fctrl & DOC_CTRL_SEQUENCE_ERROR ? "sequence error" : "-",
1652 fctrl & DOC_CTRL_FLASHREADY ? "ready" : "not ready");
1653 return pos;
1655 DEBUGFS_RO_ATTR(flashcontrol, dbg_flashctrl_show);
1657 static int dbg_asicmode_show(struct seq_file *s, void *p)
1659 struct docg3 *docg3 = (struct docg3 *)s->private;
1661 int pos = 0;
1662 int pctrl = doc_register_readb(docg3, DOC_ASICMODE);
1663 int mode = pctrl & 0x03;
1665 pos += seq_printf(s,
1666 "%04x : RAM_WE=%d,RSTIN_RESET=%d,BDETCT_RESET=%d,WRITE_ENABLE=%d,POWERDOWN=%d,MODE=%d%d (",
1667 pctrl,
1668 pctrl & DOC_ASICMODE_RAM_WE ? 1 : 0,
1669 pctrl & DOC_ASICMODE_RSTIN_RESET ? 1 : 0,
1670 pctrl & DOC_ASICMODE_BDETCT_RESET ? 1 : 0,
1671 pctrl & DOC_ASICMODE_MDWREN ? 1 : 0,
1672 pctrl & DOC_ASICMODE_POWERDOWN ? 1 : 0,
1673 mode >> 1, mode & 0x1);
1675 switch (mode) {
1676 case DOC_ASICMODE_RESET:
1677 pos += seq_printf(s, "reset");
1678 break;
1679 case DOC_ASICMODE_NORMAL:
1680 pos += seq_printf(s, "normal");
1681 break;
1682 case DOC_ASICMODE_POWERDOWN:
1683 pos += seq_printf(s, "powerdown");
1684 break;
1686 pos += seq_printf(s, ")\n");
1687 return pos;
1689 DEBUGFS_RO_ATTR(asic_mode, dbg_asicmode_show);
1691 static int dbg_device_id_show(struct seq_file *s, void *p)
1693 struct docg3 *docg3 = (struct docg3 *)s->private;
1694 int pos = 0;
1695 int id = doc_register_readb(docg3, DOC_DEVICESELECT);
1697 pos += seq_printf(s, "DeviceId = %d\n", id);
1698 return pos;
1700 DEBUGFS_RO_ATTR(device_id, dbg_device_id_show);
1702 static int dbg_protection_show(struct seq_file *s, void *p)
1704 struct docg3 *docg3 = (struct docg3 *)s->private;
1705 int pos = 0;
1706 int protect, dps0, dps0_low, dps0_high, dps1, dps1_low, dps1_high;
1708 protect = doc_register_readb(docg3, DOC_PROTECTION);
1709 dps0 = doc_register_readb(docg3, DOC_DPS0_STATUS);
1710 dps0_low = doc_register_readw(docg3, DOC_DPS0_ADDRLOW);
1711 dps0_high = doc_register_readw(docg3, DOC_DPS0_ADDRHIGH);
1712 dps1 = doc_register_readb(docg3, DOC_DPS1_STATUS);
1713 dps1_low = doc_register_readw(docg3, DOC_DPS1_ADDRLOW);
1714 dps1_high = doc_register_readw(docg3, DOC_DPS1_ADDRHIGH);
1716 pos += seq_printf(s, "Protection = 0x%02x (",
1717 protect);
1718 if (protect & DOC_PROTECT_FOUNDRY_OTP_LOCK)
1719 pos += seq_printf(s, "FOUNDRY_OTP_LOCK,");
1720 if (protect & DOC_PROTECT_CUSTOMER_OTP_LOCK)
1721 pos += seq_printf(s, "CUSTOMER_OTP_LOCK,");
1722 if (protect & DOC_PROTECT_LOCK_INPUT)
1723 pos += seq_printf(s, "LOCK_INPUT,");
1724 if (protect & DOC_PROTECT_STICKY_LOCK)
1725 pos += seq_printf(s, "STICKY_LOCK,");
1726 if (protect & DOC_PROTECT_PROTECTION_ENABLED)
1727 pos += seq_printf(s, "PROTECTION ON,");
1728 if (protect & DOC_PROTECT_IPL_DOWNLOAD_LOCK)
1729 pos += seq_printf(s, "IPL_DOWNLOAD_LOCK,");
1730 if (protect & DOC_PROTECT_PROTECTION_ERROR)
1731 pos += seq_printf(s, "PROTECT_ERR,");
1732 else
1733 pos += seq_printf(s, "NO_PROTECT_ERR");
1734 pos += seq_printf(s, ")\n");
1736 pos += seq_printf(s, "DPS0 = 0x%02x : "
1737 "Protected area [0x%x - 0x%x] : OTP=%d, READ=%d, "
1738 "WRITE=%d, HW_LOCK=%d, KEY_OK=%d\n",
1739 dps0, dps0_low, dps0_high,
1740 !!(dps0 & DOC_DPS_OTP_PROTECTED),
1741 !!(dps0 & DOC_DPS_READ_PROTECTED),
1742 !!(dps0 & DOC_DPS_WRITE_PROTECTED),
1743 !!(dps0 & DOC_DPS_HW_LOCK_ENABLED),
1744 !!(dps0 & DOC_DPS_KEY_OK));
1745 pos += seq_printf(s, "DPS1 = 0x%02x : "
1746 "Protected area [0x%x - 0x%x] : OTP=%d, READ=%d, "
1747 "WRITE=%d, HW_LOCK=%d, KEY_OK=%d\n",
1748 dps1, dps1_low, dps1_high,
1749 !!(dps1 & DOC_DPS_OTP_PROTECTED),
1750 !!(dps1 & DOC_DPS_READ_PROTECTED),
1751 !!(dps1 & DOC_DPS_WRITE_PROTECTED),
1752 !!(dps1 & DOC_DPS_HW_LOCK_ENABLED),
1753 !!(dps1 & DOC_DPS_KEY_OK));
1754 return pos;
1756 DEBUGFS_RO_ATTR(protection, dbg_protection_show);
1758 static int __init doc_dbg_register(struct docg3 *docg3)
1760 struct dentry *root, *entry;
1762 root = debugfs_create_dir("docg3", NULL);
1763 if (!root)
1764 return -ENOMEM;
1766 entry = debugfs_create_file("flashcontrol", S_IRUSR, root, docg3,
1767 &flashcontrol_fops);
1768 if (entry)
1769 entry = debugfs_create_file("asic_mode", S_IRUSR, root,
1770 docg3, &asic_mode_fops);
1771 if (entry)
1772 entry = debugfs_create_file("device_id", S_IRUSR, root,
1773 docg3, &device_id_fops);
1774 if (entry)
1775 entry = debugfs_create_file("protection", S_IRUSR, root,
1776 docg3, &protection_fops);
1777 if (entry) {
1778 docg3->debugfs_root = root;
1779 return 0;
1780 } else {
1781 debugfs_remove_recursive(root);
1782 return -ENOMEM;
1786 static void __exit doc_dbg_unregister(struct docg3 *docg3)
1788 debugfs_remove_recursive(docg3->debugfs_root);
1792 * doc_set_driver_info - Fill the mtd_info structure and docg3 structure
1793 * @chip_id: The chip ID of the supported chip
1794 * @mtd: The structure to fill
1796 static void __init doc_set_driver_info(int chip_id, struct mtd_info *mtd)
1798 struct docg3 *docg3 = mtd->priv;
1799 int cfg;
1801 cfg = doc_register_readb(docg3, DOC_CONFIGURATION);
1802 docg3->if_cfg = (cfg & DOC_CONF_IF_CFG ? 1 : 0);
1803 docg3->reliable = reliable_mode;
1805 switch (chip_id) {
1806 case DOC_CHIPID_G3:
1807 mtd->name = kasprintf(GFP_KERNEL, "DiskOnChip G3 floor %d",
1808 docg3->device_id);
1809 docg3->max_block = 2047;
1810 break;
1812 mtd->type = MTD_NANDFLASH;
1813 mtd->flags = MTD_CAP_NANDFLASH;
1814 mtd->size = (docg3->max_block + 1) * DOC_LAYOUT_BLOCK_SIZE;
1815 if (docg3->reliable == 2)
1816 mtd->size /= 2;
1817 mtd->erasesize = DOC_LAYOUT_BLOCK_SIZE * DOC_LAYOUT_NBPLANES;
1818 if (docg3->reliable == 2)
1819 mtd->erasesize /= 2;
1820 mtd->writebufsize = mtd->writesize = DOC_LAYOUT_PAGE_SIZE;
1821 mtd->oobsize = DOC_LAYOUT_OOB_SIZE;
1822 mtd->owner = THIS_MODULE;
1823 mtd->erase = doc_erase;
1824 mtd->read = doc_read;
1825 mtd->write = doc_write;
1826 mtd->read_oob = doc_read_oob;
1827 mtd->write_oob = doc_write_oob;
1828 mtd->block_isbad = doc_block_isbad;
1829 mtd->ecclayout = &docg3_oobinfo;
1833 * doc_probe_device - Check if a device is available
1834 * @base: the io space where the device is probed
1835 * @floor: the floor of the probed device
1836 * @dev: the device
1838 * Checks whether a device at the specified IO range, and floor is available.
1840 * Returns a mtd_info struct if there is a device, ENODEV if none found, ENOMEM
1841 * if a memory allocation failed. If floor 0 is checked, a reset of the ASIC is
1842 * launched.
1844 static struct mtd_info *doc_probe_device(void __iomem *base, int floor,
1845 struct device *dev)
1847 int ret, bbt_nbpages;
1848 u16 chip_id, chip_id_inv;
1849 struct docg3 *docg3;
1850 struct mtd_info *mtd;
1852 ret = -ENOMEM;
1853 docg3 = kzalloc(sizeof(struct docg3), GFP_KERNEL);
1854 if (!docg3)
1855 goto nomem1;
1856 mtd = kzalloc(sizeof(struct mtd_info), GFP_KERNEL);
1857 if (!mtd)
1858 goto nomem2;
1859 mtd->priv = docg3;
1860 bbt_nbpages = DIV_ROUND_UP(docg3->max_block + 1,
1861 8 * DOC_LAYOUT_PAGE_SIZE);
1862 docg3->bbt = kzalloc(bbt_nbpages * DOC_LAYOUT_PAGE_SIZE, GFP_KERNEL);
1863 if (!docg3->bbt)
1864 goto nomem3;
1866 docg3->dev = dev;
1867 docg3->device_id = floor;
1868 docg3->base = base;
1869 doc_set_device_id(docg3, docg3->device_id);
1870 if (!floor)
1871 doc_set_asic_mode(docg3, DOC_ASICMODE_RESET);
1872 doc_set_asic_mode(docg3, DOC_ASICMODE_NORMAL);
1874 chip_id = doc_register_readw(docg3, DOC_CHIPID);
1875 chip_id_inv = doc_register_readw(docg3, DOC_CHIPID_INV);
1877 ret = 0;
1878 if (chip_id != (u16)(~chip_id_inv)) {
1879 goto nomem3;
1882 switch (chip_id) {
1883 case DOC_CHIPID_G3:
1884 doc_info("Found a G3 DiskOnChip at addr %p, floor %d\n",
1885 base, floor);
1886 break;
1887 default:
1888 doc_err("Chip id %04x is not a DiskOnChip G3 chip\n", chip_id);
1889 goto nomem3;
1892 doc_set_driver_info(chip_id, mtd);
1894 doc_hamming_ecc_init(docg3, DOC_LAYOUT_OOB_PAGEINFO_SZ);
1895 doc_reload_bbt(docg3);
1896 return mtd;
1898 nomem3:
1899 kfree(mtd);
1900 nomem2:
1901 kfree(docg3);
1902 nomem1:
1903 return ERR_PTR(ret);
1907 * doc_release_device - Release a docg3 floor
1908 * @mtd: the device
1910 static void doc_release_device(struct mtd_info *mtd)
1912 struct docg3 *docg3 = mtd->priv;
1914 mtd_device_unregister(mtd);
1915 kfree(docg3->bbt);
1916 kfree(docg3);
1917 kfree(mtd->name);
1918 kfree(mtd);
1922 * docg3_resume - Awakens docg3 floor
1923 * @pdev: platfrom device
1925 * Returns 0 (always successfull)
1927 static int docg3_resume(struct platform_device *pdev)
1929 int i;
1930 struct mtd_info **docg3_floors, *mtd;
1931 struct docg3 *docg3;
1933 docg3_floors = platform_get_drvdata(pdev);
1934 mtd = docg3_floors[0];
1935 docg3 = mtd->priv;
1937 doc_dbg("docg3_resume()\n");
1938 for (i = 0; i < 12; i++)
1939 doc_readb(docg3, DOC_IOSPACE_IPL);
1940 return 0;
1944 * docg3_suspend - Put in low power mode the docg3 floor
1945 * @pdev: platform device
1946 * @state: power state
1948 * Shuts off most of docg3 circuitery to lower power consumption.
1950 * Returns 0 if suspend succeeded, -EIO if chip refused suspend
1952 static int docg3_suspend(struct platform_device *pdev, pm_message_t state)
1954 int floor, i;
1955 struct mtd_info **docg3_floors, *mtd;
1956 struct docg3 *docg3;
1957 u8 ctrl, pwr_down;
1959 docg3_floors = platform_get_drvdata(pdev);
1960 for (floor = 0; floor < DOC_MAX_NBFLOORS; floor++) {
1961 mtd = docg3_floors[floor];
1962 if (!mtd)
1963 continue;
1964 docg3 = mtd->priv;
1966 doc_writeb(docg3, floor, DOC_DEVICESELECT);
1967 ctrl = doc_register_readb(docg3, DOC_FLASHCONTROL);
1968 ctrl &= ~DOC_CTRL_VIOLATION & ~DOC_CTRL_CE;
1969 doc_writeb(docg3, ctrl, DOC_FLASHCONTROL);
1971 for (i = 0; i < 10; i++) {
1972 usleep_range(3000, 4000);
1973 pwr_down = doc_register_readb(docg3, DOC_POWERMODE);
1974 if (pwr_down & DOC_POWERDOWN_READY)
1975 break;
1977 if (pwr_down & DOC_POWERDOWN_READY) {
1978 doc_dbg("docg3_suspend(): floor %d powerdown ok\n",
1979 floor);
1980 } else {
1981 doc_err("docg3_suspend(): floor %d powerdown failed\n",
1982 floor);
1983 return -EIO;
1987 mtd = docg3_floors[0];
1988 docg3 = mtd->priv;
1989 doc_set_asic_mode(docg3, DOC_ASICMODE_POWERDOWN);
1990 return 0;
1994 * doc_probe - Probe the IO space for a DiskOnChip G3 chip
1995 * @pdev: platform device
1997 * Probes for a G3 chip at the specified IO space in the platform data
1998 * ressources. The floor 0 must be available.
2000 * Returns 0 on success, -ENOMEM, -ENXIO on error
2002 static int __init docg3_probe(struct platform_device *pdev)
2004 struct device *dev = &pdev->dev;
2005 struct mtd_info *mtd;
2006 struct resource *ress;
2007 void __iomem *base;
2008 int ret, floor, found = 0;
2009 struct mtd_info **docg3_floors;
2011 ret = -ENXIO;
2012 ress = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2013 if (!ress) {
2014 dev_err(dev, "No I/O memory resource defined\n");
2015 goto noress;
2017 base = ioremap(ress->start, DOC_IOSPACE_SIZE);
2019 ret = -ENOMEM;
2020 docg3_floors = kzalloc(sizeof(*docg3_floors) * DOC_MAX_NBFLOORS,
2021 GFP_KERNEL);
2022 if (!docg3_floors)
2023 goto nomem1;
2024 docg3_bch = init_bch(DOC_ECC_BCH_M, DOC_ECC_BCH_T,
2025 DOC_ECC_BCH_PRIMPOLY);
2026 if (!docg3_bch)
2027 goto nomem2;
2029 for (floor = 0; floor < DOC_MAX_NBFLOORS; floor++) {
2030 mtd = doc_probe_device(base, floor, dev);
2031 if (IS_ERR(mtd)) {
2032 ret = PTR_ERR(mtd);
2033 goto err_probe;
2035 if (!mtd) {
2036 if (floor == 0)
2037 goto notfound;
2038 else
2039 continue;
2041 docg3_floors[floor] = mtd;
2042 ret = mtd_device_parse_register(mtd, part_probes, NULL, NULL,
2044 if (ret)
2045 goto err_probe;
2046 found++;
2049 ret = doc_register_sysfs(pdev, docg3_floors);
2050 if (ret)
2051 goto err_probe;
2052 if (!found)
2053 goto notfound;
2055 platform_set_drvdata(pdev, docg3_floors);
2056 doc_dbg_register(docg3_floors[0]->priv);
2057 return 0;
2059 notfound:
2060 ret = -ENODEV;
2061 dev_info(dev, "No supported DiskOnChip found\n");
2062 err_probe:
2063 free_bch(docg3_bch);
2064 for (floor = 0; floor < DOC_MAX_NBFLOORS; floor++)
2065 if (docg3_floors[floor])
2066 doc_release_device(docg3_floors[floor]);
2067 nomem2:
2068 kfree(docg3_floors);
2069 nomem1:
2070 iounmap(base);
2071 noress:
2072 return ret;
2076 * docg3_release - Release the driver
2077 * @pdev: the platform device
2079 * Returns 0
2081 static int __exit docg3_release(struct platform_device *pdev)
2083 struct mtd_info **docg3_floors = platform_get_drvdata(pdev);
2084 struct docg3 *docg3 = docg3_floors[0]->priv;
2085 void __iomem *base = docg3->base;
2086 int floor;
2088 doc_unregister_sysfs(pdev, docg3_floors);
2089 doc_dbg_unregister(docg3);
2090 for (floor = 0; floor < DOC_MAX_NBFLOORS; floor++)
2091 if (docg3_floors[floor])
2092 doc_release_device(docg3_floors[floor]);
2094 kfree(docg3_floors);
2095 free_bch(docg3_bch);
2096 iounmap(base);
2097 return 0;
2100 static struct platform_driver g3_driver = {
2101 .driver = {
2102 .name = "docg3",
2103 .owner = THIS_MODULE,
2105 .suspend = docg3_suspend,
2106 .resume = docg3_resume,
2107 .remove = __exit_p(docg3_release),
2110 static int __init docg3_init(void)
2112 return platform_driver_probe(&g3_driver, docg3_probe);
2114 module_init(docg3_init);
2117 static void __exit docg3_exit(void)
2119 platform_driver_unregister(&g3_driver);
2121 module_exit(docg3_exit);
2123 MODULE_LICENSE("GPL");
2124 MODULE_AUTHOR("Robert Jarzmik <robert.jarzmik@free.fr>");
2125 MODULE_DESCRIPTION("MTD driver for DiskOnChip G3");