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>
26 #include <linux/platform_device.h>
27 #include <linux/string.h>
28 #include <linux/slab.h>
30 #include <linux/delay.h>
31 #include <linux/mtd/mtd.h>
32 #include <linux/mtd/partitions.h>
33 #include <linux/bitmap.h>
34 #include <linux/bitrev.h>
35 #include <linux/bch.h>
37 #include <linux/debugfs.h>
38 #include <linux/seq_file.h>
40 #define CREATE_TRACE_POINTS
44 * This driver handles the DiskOnChip G3 flash memory.
46 * As no specification is available from M-Systems/Sandisk, this drivers lacks
47 * several functions available on the chip, as :
50 * The bus data width (8bits versus 16bits) is not handled (if_cfg flag), and
51 * the driver assumes a 16bits data bus.
53 * DocG3 relies on 2 ECC algorithms, which are handled in hardware :
54 * - a 1 byte Hamming code stored in the OOB for each page
55 * - a 7 bytes BCH code stored in the OOB for each page
57 * - BCH is in GF(2^14)
58 * - BCH is over data of 520 bytes (512 page + 7 page_info bytes
60 * - BCH can correct up to 4 bits (t = 4)
61 * - BCH syndroms are calculated in hardware, and checked in hardware as well
65 static unsigned int reliable_mode
;
66 module_param(reliable_mode
, uint
, 0);
67 MODULE_PARM_DESC(reliable_mode
, "Set the docg3 mode (0=normal MLC, 1=fast, "
68 "2=reliable) : MLC normal operations are in normal mode");
70 static int docg3_ooblayout_ecc(struct mtd_info
*mtd
, int section
,
71 struct mtd_oob_region
*oobregion
)
76 /* byte 7 is Hamming ECC, byte 8-14 are BCH ECC */
77 oobregion
->offset
= 7;
78 oobregion
->length
= 8;
83 static int docg3_ooblayout_free(struct mtd_info
*mtd
, int section
,
84 struct mtd_oob_region
*oobregion
)
89 /* free bytes: byte 0 until byte 6, byte 15 */
91 oobregion
->offset
= 0;
92 oobregion
->length
= 7;
94 oobregion
->offset
= 15;
95 oobregion
->length
= 1;
101 static const struct mtd_ooblayout_ops nand_ooblayout_docg3_ops
= {
102 .ecc
= docg3_ooblayout_ecc
,
103 .free
= docg3_ooblayout_free
,
106 static inline u8
doc_readb(struct docg3
*docg3
, u16 reg
)
108 u8 val
= readb(docg3
->cascade
->base
+ reg
);
110 trace_docg3_io(0, 8, reg
, (int)val
);
114 static inline u16
doc_readw(struct docg3
*docg3
, u16 reg
)
116 u16 val
= readw(docg3
->cascade
->base
+ reg
);
118 trace_docg3_io(0, 16, reg
, (int)val
);
122 static inline void doc_writeb(struct docg3
*docg3
, u8 val
, u16 reg
)
124 writeb(val
, docg3
->cascade
->base
+ reg
);
125 trace_docg3_io(1, 8, reg
, val
);
128 static inline void doc_writew(struct docg3
*docg3
, u16 val
, u16 reg
)
130 writew(val
, docg3
->cascade
->base
+ reg
);
131 trace_docg3_io(1, 16, reg
, val
);
134 static inline void doc_flash_command(struct docg3
*docg3
, u8 cmd
)
136 doc_writeb(docg3
, cmd
, DOC_FLASHCOMMAND
);
139 static inline void doc_flash_sequence(struct docg3
*docg3
, u8 seq
)
141 doc_writeb(docg3
, seq
, DOC_FLASHSEQUENCE
);
144 static inline void doc_flash_address(struct docg3
*docg3
, u8 addr
)
146 doc_writeb(docg3
, addr
, DOC_FLASHADDRESS
);
149 static char const * const part_probes
[] = { "cmdlinepart", "saftlpart", NULL
};
151 static int doc_register_readb(struct docg3
*docg3
, int reg
)
155 doc_writew(docg3
, reg
, DOC_READADDRESS
);
156 val
= doc_readb(docg3
, reg
);
157 doc_vdbg("Read register %04x : %02x\n", reg
, val
);
161 static int doc_register_readw(struct docg3
*docg3
, int reg
)
165 doc_writew(docg3
, reg
, DOC_READADDRESS
);
166 val
= doc_readw(docg3
, reg
);
167 doc_vdbg("Read register %04x : %04x\n", reg
, val
);
172 * doc_delay - delay docg3 operations
174 * @nbNOPs: the number of NOPs to issue
176 * As no specification is available, the right timings between chip commands are
177 * unknown. The only available piece of information are the observed nops on a
178 * working docg3 chip.
179 * Therefore, doc_delay relies on a busy loop of NOPs, instead of scheduler
180 * friendlier msleep() functions or blocking mdelay().
182 static void doc_delay(struct docg3
*docg3
, int nbNOPs
)
186 doc_vdbg("NOP x %d\n", nbNOPs
);
187 for (i
= 0; i
< nbNOPs
; i
++)
188 doc_writeb(docg3
, 0, DOC_NOP
);
191 static int is_prot_seq_error(struct docg3
*docg3
)
195 ctrl
= doc_register_readb(docg3
, DOC_FLASHCONTROL
);
196 return ctrl
& (DOC_CTRL_PROTECTION_ERROR
| DOC_CTRL_SEQUENCE_ERROR
);
199 static int doc_is_ready(struct docg3
*docg3
)
203 ctrl
= doc_register_readb(docg3
, DOC_FLASHCONTROL
);
204 return ctrl
& DOC_CTRL_FLASHREADY
;
207 static int doc_wait_ready(struct docg3
*docg3
)
209 int maxWaitCycles
= 100;
214 } while (!doc_is_ready(docg3
) && maxWaitCycles
--);
216 if (maxWaitCycles
> 0)
222 static int doc_reset_seq(struct docg3
*docg3
)
226 doc_writeb(docg3
, 0x10, DOC_FLASHCONTROL
);
227 doc_flash_sequence(docg3
, DOC_SEQ_RESET
);
228 doc_flash_command(docg3
, DOC_CMD_RESET
);
230 ret
= doc_wait_ready(docg3
);
232 doc_dbg("doc_reset_seq() -> isReady=%s\n", ret
? "false" : "true");
237 * doc_read_data_area - Read data from data area
239 * @buf: the buffer to fill in (might be NULL is dummy reads)
240 * @len: the length to read
241 * @first: first time read, DOC_READADDRESS should be set
243 * Reads bytes from flash data. Handles the single byte / even bytes reads.
245 static void doc_read_data_area(struct docg3
*docg3
, void *buf
, int len
,
252 doc_dbg("doc_read_data_area(buf=%p, len=%d)\n", buf
, len
);
257 doc_writew(docg3
, DOC_IOSPACE_DATA
, DOC_READADDRESS
);
259 for (i
= 0; i
< len4
; i
+= 2) {
260 data16
= doc_readw(docg3
, DOC_IOSPACE_DATA
);
268 doc_writew(docg3
, DOC_IOSPACE_DATA
| DOC_READADDR_ONE_BYTE
,
272 for (i
= 0; i
< cdr
; i
++) {
273 data8
= doc_readb(docg3
, DOC_IOSPACE_DATA
);
283 * doc_write_data_area - Write data into data area
285 * @buf: the buffer to get input bytes from
286 * @len: the length to write
288 * Writes bytes into flash data. Handles the single byte / even bytes writes.
290 static void doc_write_data_area(struct docg3
*docg3
, const void *buf
, int len
)
296 doc_dbg("doc_write_data_area(buf=%p, len=%d)\n", buf
, len
);
300 doc_writew(docg3
, DOC_IOSPACE_DATA
, DOC_READADDRESS
);
302 for (i
= 0; i
< len4
; i
+= 2) {
303 doc_writew(docg3
, *src16
, DOC_IOSPACE_DATA
);
308 for (i
= 0; i
< cdr
; i
++) {
309 doc_writew(docg3
, DOC_IOSPACE_DATA
| DOC_READADDR_ONE_BYTE
,
311 doc_writeb(docg3
, *src8
, DOC_IOSPACE_DATA
);
317 * doc_set_data_mode - Sets the flash to normal or reliable data mode
320 * The reliable data mode is a bit slower than the fast mode, but less errors
321 * occur. Entering the reliable mode cannot be done without entering the fast
324 * In reliable mode, pages 2*n and 2*n+1 are clones. Writing to page 0 of blocks
325 * (4,5) make the hardware write also to page 1 of blocks blocks(4,5). Reading
326 * from page 0 of blocks (4,5) or from page 1 of blocks (4,5) gives the same
327 * result, which is a logical and between bytes from page 0 and page 1 (which is
328 * consistent with the fact that writing to a page is _clearing_ bits of that
331 static void doc_set_reliable_mode(struct docg3
*docg3
)
333 static char *strmode
[] = { "normal", "fast", "reliable", "invalid" };
335 doc_dbg("doc_set_reliable_mode(%s)\n", strmode
[docg3
->reliable
]);
336 switch (docg3
->reliable
) {
340 doc_flash_sequence(docg3
, DOC_SEQ_SET_FASTMODE
);
341 doc_flash_command(docg3
, DOC_CMD_FAST_MODE
);
344 doc_flash_sequence(docg3
, DOC_SEQ_SET_RELIABLEMODE
);
345 doc_flash_command(docg3
, DOC_CMD_FAST_MODE
);
346 doc_flash_command(docg3
, DOC_CMD_RELIABLE_MODE
);
349 doc_err("doc_set_reliable_mode(): invalid mode\n");
356 * doc_set_asic_mode - Set the ASIC mode
360 * The ASIC can work in 3 modes :
361 * - RESET: all registers are zeroed
362 * - NORMAL: receives and handles commands
363 * - POWERDOWN: minimal poweruse, flash parts shut off
365 static void doc_set_asic_mode(struct docg3
*docg3
, u8 mode
)
369 for (i
= 0; i
< 12; i
++)
370 doc_readb(docg3
, DOC_IOSPACE_IPL
);
372 mode
|= DOC_ASICMODE_MDWREN
;
373 doc_dbg("doc_set_asic_mode(%02x)\n", mode
);
374 doc_writeb(docg3
, mode
, DOC_ASICMODE
);
375 doc_writeb(docg3
, ~mode
, DOC_ASICMODECONFIRM
);
380 * doc_set_device_id - Sets the devices id for cascaded G3 chips
382 * @id: the chip to select (amongst 0, 1, 2, 3)
384 * There can be 4 cascaded G3 chips. This function selects the one which will
385 * should be the active one.
387 static void doc_set_device_id(struct docg3
*docg3
, int id
)
391 doc_dbg("doc_set_device_id(%d)\n", id
);
392 doc_writeb(docg3
, id
, DOC_DEVICESELECT
);
393 ctrl
= doc_register_readb(docg3
, DOC_FLASHCONTROL
);
395 ctrl
&= ~DOC_CTRL_VIOLATION
;
397 doc_writeb(docg3
, ctrl
, DOC_FLASHCONTROL
);
401 * doc_set_extra_page_mode - Change flash page layout
404 * Normally, the flash page is split into the data (512 bytes) and the out of
405 * band data (16 bytes). For each, 4 more bytes can be accessed, where the wear
406 * leveling counters are stored. To access this last area of 4 bytes, a special
407 * mode must be input to the flash ASIC.
409 * Returns 0 if no error occurred, -EIO else.
411 static int doc_set_extra_page_mode(struct docg3
*docg3
)
415 doc_dbg("doc_set_extra_page_mode()\n");
416 doc_flash_sequence(docg3
, DOC_SEQ_PAGE_SIZE_532
);
417 doc_flash_command(docg3
, DOC_CMD_PAGE_SIZE_532
);
420 fctrl
= doc_register_readb(docg3
, DOC_FLASHCONTROL
);
421 if (fctrl
& (DOC_CTRL_PROTECTION_ERROR
| DOC_CTRL_SEQUENCE_ERROR
))
428 * doc_setup_addr_sector - Setup blocks/page/ofs address for one plane
430 * @sector: the sector
432 static void doc_setup_addr_sector(struct docg3
*docg3
, int sector
)
435 doc_flash_address(docg3
, sector
& 0xff);
436 doc_flash_address(docg3
, (sector
>> 8) & 0xff);
437 doc_flash_address(docg3
, (sector
>> 16) & 0xff);
442 * doc_setup_writeaddr_sector - Setup blocks/page/ofs address for one plane
444 * @sector: the sector
445 * @ofs: the offset in the page, between 0 and (512 + 16 + 512)
447 static void doc_setup_writeaddr_sector(struct docg3
*docg3
, int sector
, int ofs
)
451 doc_flash_address(docg3
, ofs
& 0xff);
452 doc_flash_address(docg3
, sector
& 0xff);
453 doc_flash_address(docg3
, (sector
>> 8) & 0xff);
454 doc_flash_address(docg3
, (sector
>> 16) & 0xff);
459 * doc_seek - Set both flash planes to the specified block, page for reading
461 * @block0: the first plane block index
462 * @block1: the second plane block index
463 * @page: the page index within the block
464 * @wear: if true, read will occur on the 4 extra bytes of the wear area
465 * @ofs: offset in page to read
467 * Programs the flash even and odd planes to the specific block and page.
468 * Alternatively, programs the flash to the wear area of the specified page.
470 static int doc_read_seek(struct docg3
*docg3
, int block0
, int block1
, int page
,
475 doc_dbg("doc_seek(blocks=(%d,%d), page=%d, ofs=%d, wear=%d)\n",
476 block0
, block1
, page
, ofs
, wear
);
478 if (!wear
&& (ofs
< 2 * DOC_LAYOUT_PAGE_SIZE
)) {
479 doc_flash_sequence(docg3
, DOC_SEQ_SET_PLANE1
);
480 doc_flash_command(docg3
, DOC_CMD_READ_PLANE1
);
483 doc_flash_sequence(docg3
, DOC_SEQ_SET_PLANE2
);
484 doc_flash_command(docg3
, DOC_CMD_READ_PLANE2
);
488 doc_set_reliable_mode(docg3
);
490 ret
= doc_set_extra_page_mode(docg3
);
494 doc_flash_sequence(docg3
, DOC_SEQ_READ
);
495 sector
= (block0
<< DOC_ADDR_BLOCK_SHIFT
) + (page
& DOC_ADDR_PAGE_MASK
);
496 doc_flash_command(docg3
, DOC_CMD_PROG_BLOCK_ADDR
);
497 doc_setup_addr_sector(docg3
, sector
);
499 sector
= (block1
<< DOC_ADDR_BLOCK_SHIFT
) + (page
& DOC_ADDR_PAGE_MASK
);
500 doc_flash_command(docg3
, DOC_CMD_PROG_BLOCK_ADDR
);
501 doc_setup_addr_sector(docg3
, sector
);
509 * doc_write_seek - Set both flash planes to the specified block, page for writing
511 * @block0: the first plane block index
512 * @block1: the second plane block index
513 * @page: the page index within the block
514 * @ofs: offset in page to write
516 * Programs the flash even and odd planes to the specific block and page.
517 * Alternatively, programs the flash to the wear area of the specified page.
519 static int doc_write_seek(struct docg3
*docg3
, int block0
, int block1
, int page
,
524 doc_dbg("doc_write_seek(blocks=(%d,%d), page=%d, ofs=%d)\n",
525 block0
, block1
, page
, ofs
);
527 doc_set_reliable_mode(docg3
);
529 if (ofs
< 2 * DOC_LAYOUT_PAGE_SIZE
) {
530 doc_flash_sequence(docg3
, DOC_SEQ_SET_PLANE1
);
531 doc_flash_command(docg3
, DOC_CMD_READ_PLANE1
);
534 doc_flash_sequence(docg3
, DOC_SEQ_SET_PLANE2
);
535 doc_flash_command(docg3
, DOC_CMD_READ_PLANE2
);
539 doc_flash_sequence(docg3
, DOC_SEQ_PAGE_SETUP
);
540 doc_flash_command(docg3
, DOC_CMD_PROG_CYCLE1
);
542 sector
= (block0
<< DOC_ADDR_BLOCK_SHIFT
) + (page
& DOC_ADDR_PAGE_MASK
);
543 doc_setup_writeaddr_sector(docg3
, sector
, ofs
);
545 doc_flash_command(docg3
, DOC_CMD_PROG_CYCLE3
);
547 ret
= doc_wait_ready(docg3
);
551 doc_flash_command(docg3
, DOC_CMD_PROG_CYCLE1
);
552 sector
= (block1
<< DOC_ADDR_BLOCK_SHIFT
) + (page
& DOC_ADDR_PAGE_MASK
);
553 doc_setup_writeaddr_sector(docg3
, sector
, ofs
);
562 * doc_read_page_ecc_init - Initialize hardware ECC engine
564 * @len: the number of bytes covered by the ECC (BCH covered)
566 * The function does initialize the hardware ECC engine to compute the Hamming
567 * ECC (on 1 byte) and the BCH hardware ECC (on 7 bytes).
569 * Return 0 if succeeded, -EIO on error
571 static int doc_read_page_ecc_init(struct docg3
*docg3
, int len
)
573 doc_writew(docg3
, DOC_ECCCONF0_READ_MODE
574 | DOC_ECCCONF0_BCH_ENABLE
| DOC_ECCCONF0_HAMMING_ENABLE
575 | (len
& DOC_ECCCONF0_DATA_BYTES_MASK
),
578 doc_register_readb(docg3
, DOC_FLASHCONTROL
);
579 return doc_wait_ready(docg3
);
583 * doc_write_page_ecc_init - Initialize hardware BCH ECC engine
585 * @len: the number of bytes covered by the ECC (BCH covered)
587 * The function does initialize the hardware ECC engine to compute the Hamming
588 * ECC (on 1 byte) and the BCH hardware ECC (on 7 bytes).
590 * Return 0 if succeeded, -EIO on error
592 static int doc_write_page_ecc_init(struct docg3
*docg3
, int len
)
594 doc_writew(docg3
, DOC_ECCCONF0_WRITE_MODE
595 | DOC_ECCCONF0_BCH_ENABLE
| DOC_ECCCONF0_HAMMING_ENABLE
596 | (len
& DOC_ECCCONF0_DATA_BYTES_MASK
),
599 doc_register_readb(docg3
, DOC_FLASHCONTROL
);
600 return doc_wait_ready(docg3
);
604 * doc_ecc_disable - Disable Hamming and BCH ECC hardware calculator
607 * Disables the hardware ECC generator and checker, for unchecked reads (as when
608 * reading OOB only or write status byte).
610 static void doc_ecc_disable(struct docg3
*docg3
)
612 doc_writew(docg3
, DOC_ECCCONF0_READ_MODE
, DOC_ECCCONF0
);
617 * doc_hamming_ecc_init - Initialize hardware Hamming ECC engine
619 * @nb_bytes: the number of bytes covered by the ECC (Hamming covered)
621 * This function programs the ECC hardware to compute the hamming code on the
622 * last provided N bytes to the hardware generator.
624 static void doc_hamming_ecc_init(struct docg3
*docg3
, int nb_bytes
)
628 ecc_conf1
= doc_register_readb(docg3
, DOC_ECCCONF1
);
629 ecc_conf1
&= ~DOC_ECCCONF1_HAMMING_BITS_MASK
;
630 ecc_conf1
|= (nb_bytes
& DOC_ECCCONF1_HAMMING_BITS_MASK
);
631 doc_writeb(docg3
, ecc_conf1
, DOC_ECCCONF1
);
635 * doc_ecc_bch_fix_data - Fix if need be read data from flash
637 * @buf: the buffer of read data (512 + 7 + 1 bytes)
638 * @hwecc: the hardware calculated ECC.
639 * It's in fact recv_ecc ^ calc_ecc, where recv_ecc was read from OOB
640 * area data, and calc_ecc the ECC calculated by the hardware generator.
642 * Checks if the received data matches the ECC, and if an error is detected,
643 * tries to fix the bit flips (at most 4) in the buffer buf. As the docg3
644 * understands the (data, ecc, syndroms) in an inverted order in comparison to
645 * the BCH library, the function reverses the order of bits (ie. bit7 and bit0,
646 * bit6 and bit 1, ...) for all ECC data.
648 * The hardware ecc unit produces oob_ecc ^ calc_ecc. The kernel's bch
649 * algorithm is used to decode this. However the hw operates on page
650 * data in a bit order that is the reverse of that of the bch alg,
651 * requiring that the bits be reversed on the result. Thanks to Ivan
652 * Djelic for his analysis.
654 * Returns number of fixed bits (0, 1, 2, 3, 4) or -EBADMSG if too many bit
655 * errors were detected and cannot be fixed.
657 static int doc_ecc_bch_fix_data(struct docg3
*docg3
, void *buf
, u8
*hwecc
)
659 u8 ecc
[DOC_ECC_BCH_SIZE
];
660 int errorpos
[DOC_ECC_BCH_T
], i
, numerrs
;
662 for (i
= 0; i
< DOC_ECC_BCH_SIZE
; i
++)
663 ecc
[i
] = bitrev8(hwecc
[i
]);
664 numerrs
= decode_bch(docg3
->cascade
->bch
, NULL
,
665 DOC_ECC_BCH_COVERED_BYTES
,
666 NULL
, ecc
, NULL
, errorpos
);
667 BUG_ON(numerrs
== -EINVAL
);
671 for (i
= 0; i
< numerrs
; i
++)
672 errorpos
[i
] = (errorpos
[i
] & ~7) | (7 - (errorpos
[i
] & 7));
673 for (i
= 0; i
< numerrs
; i
++)
674 if (errorpos
[i
] < DOC_ECC_BCH_COVERED_BYTES
*8)
675 /* error is located in data, correct it */
676 change_bit(errorpos
[i
], buf
);
678 doc_dbg("doc_ecc_bch_fix_data: flipped %d bits\n", numerrs
);
684 * doc_read_page_prepare - Prepares reading data from a flash page
686 * @block0: the first plane block index on flash memory
687 * @block1: the second plane block index on flash memory
688 * @page: the page index in the block
689 * @offset: the offset in the page (must be a multiple of 4)
691 * Prepares the page to be read in the flash memory :
692 * - tell ASIC to map the flash pages
693 * - tell ASIC to be in read mode
695 * After a call to this method, a call to doc_read_page_finish is mandatory,
696 * to end the read cycle of the flash.
698 * Read data from a flash page. The length to be read must be between 0 and
699 * (page_size + oob_size + wear_size), ie. 532, and a multiple of 4 (because
700 * the extra bytes reading is not implemented).
702 * As pages are grouped by 2 (in 2 planes), reading from a page must be done
704 * - one read of 512 bytes at offset 0
705 * - one read of 512 bytes at offset 512 + 16
707 * Returns 0 if successful, -EIO if a read error occurred.
709 static int doc_read_page_prepare(struct docg3
*docg3
, int block0
, int block1
,
710 int page
, int offset
)
712 int wear_area
= 0, ret
= 0;
714 doc_dbg("doc_read_page_prepare(blocks=(%d,%d), page=%d, ofsInPage=%d)\n",
715 block0
, block1
, page
, offset
);
716 if (offset
>= DOC_LAYOUT_WEAR_OFFSET
)
718 if (!wear_area
&& offset
> (DOC_LAYOUT_PAGE_OOB_SIZE
* 2))
721 doc_set_device_id(docg3
, docg3
->device_id
);
722 ret
= doc_reset_seq(docg3
);
726 /* Program the flash address block and page */
727 ret
= doc_read_seek(docg3
, block0
, block1
, page
, wear_area
, offset
);
731 doc_flash_command(docg3
, DOC_CMD_READ_ALL_PLANES
);
733 doc_wait_ready(docg3
);
735 doc_flash_command(docg3
, DOC_CMD_SET_ADDR_READ
);
737 if (offset
>= DOC_LAYOUT_PAGE_SIZE
* 2)
738 offset
-= 2 * DOC_LAYOUT_PAGE_SIZE
;
739 doc_flash_address(docg3
, offset
>> 2);
741 doc_wait_ready(docg3
);
743 doc_flash_command(docg3
, DOC_CMD_READ_FLASH
);
747 doc_writeb(docg3
, 0, DOC_DATAEND
);
753 * doc_read_page_getbytes - Reads bytes from a prepared page
755 * @len: the number of bytes to be read (must be a multiple of 4)
756 * @buf: the buffer to be filled in (or NULL is forget bytes)
757 * @first: 1 if first time read, DOC_READADDRESS should be set
758 * @last_odd: 1 if last read ended up on an odd byte
760 * Reads bytes from a prepared page. There is a trickery here : if the last read
761 * ended up on an odd offset in the 1024 bytes double page, ie. between the 2
762 * planes, the first byte must be read apart. If a word (16bit) read was used,
763 * the read would return the byte of plane 2 as low *and* high endian, which
764 * will mess the read.
767 static int doc_read_page_getbytes(struct docg3
*docg3
, int len
, u_char
*buf
,
768 int first
, int last_odd
)
770 if (last_odd
&& len
> 0) {
771 doc_read_data_area(docg3
, buf
, 1, first
);
772 doc_read_data_area(docg3
, buf
? buf
+ 1 : buf
, len
- 1, 0);
774 doc_read_data_area(docg3
, buf
, len
, first
);
781 * doc_write_page_putbytes - Writes bytes into a prepared page
783 * @len: the number of bytes to be written
784 * @buf: the buffer of input bytes
787 static void doc_write_page_putbytes(struct docg3
*docg3
, int len
,
790 doc_write_data_area(docg3
, buf
, len
);
795 * doc_get_bch_hw_ecc - Get hardware calculated BCH ECC
797 * @hwecc: the array of 7 integers where the hardware ecc will be stored
799 static void doc_get_bch_hw_ecc(struct docg3
*docg3
, u8
*hwecc
)
803 for (i
= 0; i
< DOC_ECC_BCH_SIZE
; i
++)
804 hwecc
[i
] = doc_register_readb(docg3
, DOC_BCH_HW_ECC(i
));
808 * doc_page_finish - Ends reading/writing of a flash page
811 static void doc_page_finish(struct docg3
*docg3
)
813 doc_writeb(docg3
, 0, DOC_DATAEND
);
818 * doc_read_page_finish - Ends reading of a flash page
821 * As a side effect, resets the chip selector to 0. This ensures that after each
822 * read operation, the floor 0 is selected. Therefore, if the systems halts, the
823 * reboot will boot on floor 0, where the IPL is.
825 static void doc_read_page_finish(struct docg3
*docg3
)
827 doc_page_finish(docg3
);
828 doc_set_device_id(docg3
, 0);
832 * calc_block_sector - Calculate blocks, pages and ofs.
834 * @from: offset in flash
835 * @block0: first plane block index calculated
836 * @block1: second plane block index calculated
837 * @page: page calculated
838 * @ofs: offset in page
839 * @reliable: 0 if docg3 in normal mode, 1 if docg3 in fast mode, 2 if docg3 in
842 * The calculation is based on the reliable/normal mode. In normal mode, the 64
843 * pages of a block are available. In reliable mode, as pages 2*n and 2*n+1 are
844 * clones, only 32 pages per block are available.
846 static void calc_block_sector(loff_t from
, int *block0
, int *block1
, int *page
,
847 int *ofs
, int reliable
)
849 uint sector
, pages_biblock
;
851 pages_biblock
= DOC_LAYOUT_PAGES_PER_BLOCK
* DOC_LAYOUT_NBPLANES
;
852 if (reliable
== 1 || reliable
== 2)
855 sector
= from
/ DOC_LAYOUT_PAGE_SIZE
;
856 *block0
= sector
/ pages_biblock
* DOC_LAYOUT_NBPLANES
;
857 *block1
= *block0
+ 1;
858 *page
= sector
% pages_biblock
;
859 *page
/= DOC_LAYOUT_NBPLANES
;
860 if (reliable
== 1 || reliable
== 2)
863 *ofs
= DOC_LAYOUT_PAGE_OOB_SIZE
;
869 * doc_read_oob - Read out of band bytes from flash
871 * @from: the offset from first block and first page, in bytes, aligned on page
873 * @ops: the mtd oob structure
875 * Reads flash memory OOB area of pages.
877 * Returns 0 if read successful, of -EIO, -EINVAL if an error occurred
879 static int doc_read_oob(struct mtd_info
*mtd
, loff_t from
,
880 struct mtd_oob_ops
*ops
)
882 struct docg3
*docg3
= mtd
->priv
;
883 int block0
, block1
, page
, ret
, skip
, ofs
= 0;
884 u8
*oobbuf
= ops
->oobbuf
;
885 u8
*buf
= ops
->datbuf
;
886 size_t len
, ooblen
, nbdata
, nboob
;
887 u8 hwecc
[DOC_ECC_BCH_SIZE
], eccconf1
;
888 int max_bitflips
= 0;
895 ooblen
= ops
->ooblen
;
899 if (oobbuf
&& ops
->mode
== MTD_OPS_PLACE_OOB
)
900 oobbuf
+= ops
->ooboffs
;
902 doc_dbg("doc_read_oob(from=%lld, mode=%d, data=(%p:%zu), oob=(%p:%zu))\n",
903 from
, ops
->mode
, buf
, len
, oobbuf
, ooblen
);
904 if (ooblen
% DOC_LAYOUT_OOB_SIZE
)
910 skip
= from
% DOC_LAYOUT_PAGE_SIZE
;
911 mutex_lock(&docg3
->cascade
->lock
);
912 while (ret
>= 0 && (len
> 0 || ooblen
> 0)) {
913 calc_block_sector(from
- skip
, &block0
, &block1
, &page
, &ofs
,
915 nbdata
= min_t(size_t, len
, DOC_LAYOUT_PAGE_SIZE
- skip
);
916 nboob
= min_t(size_t, ooblen
, (size_t)DOC_LAYOUT_OOB_SIZE
);
917 ret
= doc_read_page_prepare(docg3
, block0
, block1
, page
, ofs
);
920 ret
= doc_read_page_ecc_init(docg3
, DOC_ECC_BCH_TOTAL_BYTES
);
923 ret
= doc_read_page_getbytes(docg3
, skip
, NULL
, 1, 0);
926 ret
= doc_read_page_getbytes(docg3
, nbdata
, buf
, 0, skip
% 2);
929 doc_read_page_getbytes(docg3
,
930 DOC_LAYOUT_PAGE_SIZE
- nbdata
- skip
,
931 NULL
, 0, (skip
+ nbdata
) % 2);
932 ret
= doc_read_page_getbytes(docg3
, nboob
, oobbuf
, 0, 0);
935 doc_read_page_getbytes(docg3
, DOC_LAYOUT_OOB_SIZE
- nboob
,
938 doc_get_bch_hw_ecc(docg3
, hwecc
);
939 eccconf1
= doc_register_readb(docg3
, DOC_ECCCONF1
);
941 if (nboob
>= DOC_LAYOUT_OOB_SIZE
) {
942 doc_dbg("OOB - INFO: %*phC\n", 7, oobbuf
);
943 doc_dbg("OOB - HAMMING: %02x\n", oobbuf
[7]);
944 doc_dbg("OOB - BCH_ECC: %*phC\n", 7, oobbuf
+ 8);
945 doc_dbg("OOB - UNUSED: %02x\n", oobbuf
[15]);
947 doc_dbg("ECC checks: ECCConf1=%x\n", eccconf1
);
948 doc_dbg("ECC HW_ECC: %*phC\n", 7, hwecc
);
951 if (is_prot_seq_error(docg3
))
954 if ((block0
>= DOC_LAYOUT_BLOCK_FIRST_DATA
) &&
955 (eccconf1
& DOC_ECCCONF1_BCH_SYNDROM_ERR
) &&
956 (eccconf1
& DOC_ECCCONF1_PAGE_IS_WRITTEN
) &&
957 (ops
->mode
!= MTD_OPS_RAW
) &&
958 (nbdata
== DOC_LAYOUT_PAGE_SIZE
)) {
959 ret
= doc_ecc_bch_fix_data(docg3
, buf
, hwecc
);
961 mtd
->ecc_stats
.failed
++;
965 mtd
->ecc_stats
.corrected
+= ret
;
966 max_bitflips
= max(max_bitflips
, ret
);
971 doc_read_page_finish(docg3
);
972 ops
->retlen
+= nbdata
;
973 ops
->oobretlen
+= nboob
;
978 from
+= DOC_LAYOUT_PAGE_SIZE
;
983 mutex_unlock(&docg3
->cascade
->lock
);
986 doc_read_page_finish(docg3
);
990 static int doc_reload_bbt(struct docg3
*docg3
)
992 int block
= DOC_LAYOUT_BLOCK_BBT
;
993 int ret
= 0, nbpages
, page
;
994 u_char
*buf
= docg3
->bbt
;
996 nbpages
= DIV_ROUND_UP(docg3
->max_block
+ 1, 8 * DOC_LAYOUT_PAGE_SIZE
);
997 for (page
= 0; !ret
&& (page
< nbpages
); page
++) {
998 ret
= doc_read_page_prepare(docg3
, block
, block
+ 1,
999 page
+ DOC_LAYOUT_PAGE_BBT
, 0);
1001 ret
= doc_read_page_ecc_init(docg3
,
1002 DOC_LAYOUT_PAGE_SIZE
);
1004 doc_read_page_getbytes(docg3
, DOC_LAYOUT_PAGE_SIZE
,
1006 buf
+= DOC_LAYOUT_PAGE_SIZE
;
1008 doc_read_page_finish(docg3
);
1013 * doc_block_isbad - Checks whether a block is good or not
1015 * @from: the offset to find the correct block
1017 * Returns 1 if block is bad, 0 if block is good
1019 static int doc_block_isbad(struct mtd_info
*mtd
, loff_t from
)
1021 struct docg3
*docg3
= mtd
->priv
;
1022 int block0
, block1
, page
, ofs
, is_good
;
1024 calc_block_sector(from
, &block0
, &block1
, &page
, &ofs
,
1026 doc_dbg("doc_block_isbad(from=%lld) => block=(%d,%d), page=%d, ofs=%d\n",
1027 from
, block0
, block1
, page
, ofs
);
1029 if (block0
< DOC_LAYOUT_BLOCK_FIRST_DATA
)
1031 if (block1
> docg3
->max_block
)
1034 is_good
= docg3
->bbt
[block0
>> 3] & (1 << (block0
& 0x7));
1040 * doc_get_erase_count - Get block erase count
1041 * @docg3: the device
1042 * @from: the offset in which the block is.
1044 * Get the number of times a block was erased. The number is the maximum of
1045 * erase times between first and second plane (which should be equal normally).
1047 * Returns The number of erases, or -EINVAL or -EIO on error.
1049 static int doc_get_erase_count(struct docg3
*docg3
, loff_t from
)
1051 u8 buf
[DOC_LAYOUT_WEAR_SIZE
];
1052 int ret
, plane1_erase_count
, plane2_erase_count
;
1053 int block0
, block1
, page
, ofs
;
1055 doc_dbg("doc_get_erase_count(from=%lld, buf=%p)\n", from
, buf
);
1056 if (from
% DOC_LAYOUT_PAGE_SIZE
)
1058 calc_block_sector(from
, &block0
, &block1
, &page
, &ofs
, docg3
->reliable
);
1059 if (block1
> docg3
->max_block
)
1062 ret
= doc_reset_seq(docg3
);
1064 ret
= doc_read_page_prepare(docg3
, block0
, block1
, page
,
1065 ofs
+ DOC_LAYOUT_WEAR_OFFSET
, 0);
1067 ret
= doc_read_page_getbytes(docg3
, DOC_LAYOUT_WEAR_SIZE
,
1069 doc_read_page_finish(docg3
);
1071 if (ret
|| (buf
[0] != DOC_ERASE_MARK
) || (buf
[2] != DOC_ERASE_MARK
))
1073 plane1_erase_count
= (u8
)(~buf
[1]) | ((u8
)(~buf
[4]) << 8)
1074 | ((u8
)(~buf
[5]) << 16);
1075 plane2_erase_count
= (u8
)(~buf
[3]) | ((u8
)(~buf
[6]) << 8)
1076 | ((u8
)(~buf
[7]) << 16);
1078 return max(plane1_erase_count
, plane2_erase_count
);
1083 * doc_get_op_status - get erase/write operation status
1084 * @docg3: the device
1086 * Queries the status from the chip, and returns it
1088 * Returns the status (bits DOC_PLANES_STATUS_*)
1090 static int doc_get_op_status(struct docg3
*docg3
)
1094 doc_flash_sequence(docg3
, DOC_SEQ_PLANES_STATUS
);
1095 doc_flash_command(docg3
, DOC_CMD_PLANES_STATUS
);
1096 doc_delay(docg3
, 5);
1098 doc_ecc_disable(docg3
);
1099 doc_read_data_area(docg3
, &status
, 1, 1);
1104 * doc_write_erase_wait_status - wait for write or erase completion
1105 * @docg3: the device
1107 * Wait for the chip to be ready again after erase or write operation, and check
1108 * erase/write status.
1110 * Returns 0 if erase successful, -EIO if erase/write issue, -ETIMEOUT if
1113 static int doc_write_erase_wait_status(struct docg3
*docg3
)
1115 int i
, status
, ret
= 0;
1117 for (i
= 0; !doc_is_ready(docg3
) && i
< 5; i
++)
1119 if (!doc_is_ready(docg3
)) {
1120 doc_dbg("Timeout reached and the chip is still not ready\n");
1125 status
= doc_get_op_status(docg3
);
1126 if (status
& DOC_PLANES_STATUS_FAIL
) {
1127 doc_dbg("Erase/Write failed on (a) plane(s), status = %x\n",
1133 doc_page_finish(docg3
);
1138 * doc_erase_block - Erase a couple of blocks
1139 * @docg3: the device
1140 * @block0: the first block to erase (leftmost plane)
1141 * @block1: the second block to erase (rightmost plane)
1143 * Erase both blocks, and return operation status
1145 * Returns 0 if erase successful, -EIO if erase issue, -ETIMEOUT if chip not
1146 * ready for too long
1148 static int doc_erase_block(struct docg3
*docg3
, int block0
, int block1
)
1152 doc_dbg("doc_erase_block(blocks=(%d,%d))\n", block0
, block1
);
1153 ret
= doc_reset_seq(docg3
);
1157 doc_set_reliable_mode(docg3
);
1158 doc_flash_sequence(docg3
, DOC_SEQ_ERASE
);
1160 sector
= block0
<< DOC_ADDR_BLOCK_SHIFT
;
1161 doc_flash_command(docg3
, DOC_CMD_PROG_BLOCK_ADDR
);
1162 doc_setup_addr_sector(docg3
, sector
);
1163 sector
= block1
<< DOC_ADDR_BLOCK_SHIFT
;
1164 doc_flash_command(docg3
, DOC_CMD_PROG_BLOCK_ADDR
);
1165 doc_setup_addr_sector(docg3
, sector
);
1166 doc_delay(docg3
, 1);
1168 doc_flash_command(docg3
, DOC_CMD_ERASECYCLE2
);
1169 doc_delay(docg3
, 2);
1171 if (is_prot_seq_error(docg3
)) {
1172 doc_err("Erase blocks %d,%d error\n", block0
, block1
);
1176 return doc_write_erase_wait_status(docg3
);
1180 * doc_erase - Erase a portion of the chip
1182 * @info: the erase info
1184 * Erase a bunch of contiguous blocks, by pairs, as a "mtd" page of 1024 is
1185 * split into 2 pages of 512 bytes on 2 contiguous blocks.
1187 * Returns 0 if erase successful, -EINVAL if addressing error, -EIO if erase
1190 static int doc_erase(struct mtd_info
*mtd
, struct erase_info
*info
)
1192 struct docg3
*docg3
= mtd
->priv
;
1194 int block0
, block1
, page
, ret
, ofs
= 0;
1196 doc_dbg("doc_erase(from=%lld, len=%lld\n", info
->addr
, info
->len
);
1198 info
->state
= MTD_ERASE_PENDING
;
1199 calc_block_sector(info
->addr
+ info
->len
, &block0
, &block1
, &page
,
1200 &ofs
, docg3
->reliable
);
1202 if (info
->addr
+ info
->len
> mtd
->size
|| page
|| ofs
)
1206 calc_block_sector(info
->addr
, &block0
, &block1
, &page
, &ofs
,
1208 mutex_lock(&docg3
->cascade
->lock
);
1209 doc_set_device_id(docg3
, docg3
->device_id
);
1210 doc_set_reliable_mode(docg3
);
1211 for (len
= info
->len
; !ret
&& len
> 0; len
-= mtd
->erasesize
) {
1212 info
->state
= MTD_ERASING
;
1213 ret
= doc_erase_block(docg3
, block0
, block1
);
1217 mutex_unlock(&docg3
->cascade
->lock
);
1222 info
->state
= MTD_ERASE_DONE
;
1226 info
->state
= MTD_ERASE_FAILED
;
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
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
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
);
1263 /* Program the flash address block and page */
1264 ret
= doc_write_seek(docg3
, block0
, block1
, page
, ofs
);
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
,
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
);
1305 doc_read_page_finish(docg3
);
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
)
1319 switch (ops
->mode
) {
1320 case MTD_OPS_PLACE_OOB
:
1321 case MTD_OPS_AUTO_OOB
:
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
)
1369 autoecc
= doc_guess_autoecc(ops
);
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
);
1379 memcpy(docg3
->oob_write_buf
, ops
->oobbuf
, DOC_LAYOUT_OOB_SIZE
);
1380 ops
->oobretlen
= DOC_LAYOUT_OOB_SIZE
;
1386 * doc_write_oob - Write out of band bytes to flash
1388 * @ofs: the offset from first block and first page, in bytes, aligned on page
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 successful, 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 ret
, autoecc
, oobdelta
;
1405 u8
*oobbuf
= ops
->oobbuf
;
1406 u8
*buf
= ops
->datbuf
;
1408 u8 oob
[DOC_LAYOUT_OOB_SIZE
];
1415 ooblen
= ops
->ooblen
;
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
:
1427 oobdelta
= mtd
->oobsize
;
1429 case MTD_OPS_AUTO_OOB
:
1430 oobdelta
= mtd
->oobavail
;
1435 if ((len
% DOC_LAYOUT_PAGE_SIZE
) || (ooblen
% oobdelta
) ||
1436 (ofs
% DOC_LAYOUT_PAGE_SIZE
))
1438 if (len
&& ooblen
&&
1439 (len
/ DOC_LAYOUT_PAGE_SIZE
) != (ooblen
/ oobdelta
))
1445 if (len
== 0 && ooblen
== 0)
1447 if (len
== 0 && ooblen
> 0)
1448 return doc_backup_oob(docg3
, ofs
, ops
);
1450 autoecc
= doc_guess_autoecc(ops
);
1454 mutex_lock(&docg3
->cascade
->lock
);
1455 while (!ret
&& len
> 0) {
1456 memset(oob
, 0, sizeof(oob
));
1457 if (ofs
== docg3
->oob_write_ofs
)
1458 memcpy(oob
, docg3
->oob_write_buf
, DOC_LAYOUT_OOB_SIZE
);
1459 else if (ooblen
> 0 && ops
->mode
== MTD_OPS_AUTO_OOB
)
1460 doc_fill_autooob(oob
, oobbuf
);
1461 else if (ooblen
> 0)
1462 memcpy(oob
, oobbuf
, DOC_LAYOUT_OOB_SIZE
);
1463 ret
= doc_write_page(docg3
, ofs
, buf
, oob
, autoecc
);
1465 ofs
+= DOC_LAYOUT_PAGE_SIZE
;
1466 len
-= DOC_LAYOUT_PAGE_SIZE
;
1467 buf
+= DOC_LAYOUT_PAGE_SIZE
;
1471 ops
->oobretlen
+= oobdelta
;
1473 ops
->retlen
+= DOC_LAYOUT_PAGE_SIZE
;
1476 doc_set_device_id(docg3
, 0);
1477 mutex_unlock(&docg3
->cascade
->lock
);
1481 static struct docg3
*sysfs_dev2docg3(struct device
*dev
,
1482 struct device_attribute
*attr
)
1485 struct platform_device
*pdev
= to_platform_device(dev
);
1486 struct mtd_info
**docg3_floors
= platform_get_drvdata(pdev
);
1488 floor
= attr
->attr
.name
[1] - '0';
1489 if (floor
< 0 || floor
>= DOC_MAX_NBFLOORS
)
1492 return docg3_floors
[floor
]->priv
;
1495 static ssize_t
dps0_is_key_locked(struct device
*dev
,
1496 struct device_attribute
*attr
, char *buf
)
1498 struct docg3
*docg3
= sysfs_dev2docg3(dev
, attr
);
1501 mutex_lock(&docg3
->cascade
->lock
);
1502 doc_set_device_id(docg3
, docg3
->device_id
);
1503 dps0
= doc_register_readb(docg3
, DOC_DPS0_STATUS
);
1504 doc_set_device_id(docg3
, 0);
1505 mutex_unlock(&docg3
->cascade
->lock
);
1507 return sprintf(buf
, "%d\n", !(dps0
& DOC_DPS_KEY_OK
));
1510 static ssize_t
dps1_is_key_locked(struct device
*dev
,
1511 struct device_attribute
*attr
, char *buf
)
1513 struct docg3
*docg3
= sysfs_dev2docg3(dev
, attr
);
1516 mutex_lock(&docg3
->cascade
->lock
);
1517 doc_set_device_id(docg3
, docg3
->device_id
);
1518 dps1
= doc_register_readb(docg3
, DOC_DPS1_STATUS
);
1519 doc_set_device_id(docg3
, 0);
1520 mutex_unlock(&docg3
->cascade
->lock
);
1522 return sprintf(buf
, "%d\n", !(dps1
& DOC_DPS_KEY_OK
));
1525 static ssize_t
dps0_insert_key(struct device
*dev
,
1526 struct device_attribute
*attr
,
1527 const char *buf
, size_t count
)
1529 struct docg3
*docg3
= sysfs_dev2docg3(dev
, attr
);
1532 if (count
!= DOC_LAYOUT_DPS_KEY_LENGTH
)
1535 mutex_lock(&docg3
->cascade
->lock
);
1536 doc_set_device_id(docg3
, docg3
->device_id
);
1537 for (i
= 0; i
< DOC_LAYOUT_DPS_KEY_LENGTH
; i
++)
1538 doc_writeb(docg3
, buf
[i
], DOC_DPS0_KEY
);
1539 doc_set_device_id(docg3
, 0);
1540 mutex_unlock(&docg3
->cascade
->lock
);
1544 static ssize_t
dps1_insert_key(struct device
*dev
,
1545 struct device_attribute
*attr
,
1546 const char *buf
, size_t count
)
1548 struct docg3
*docg3
= sysfs_dev2docg3(dev
, attr
);
1551 if (count
!= DOC_LAYOUT_DPS_KEY_LENGTH
)
1554 mutex_lock(&docg3
->cascade
->lock
);
1555 doc_set_device_id(docg3
, docg3
->device_id
);
1556 for (i
= 0; i
< DOC_LAYOUT_DPS_KEY_LENGTH
; i
++)
1557 doc_writeb(docg3
, buf
[i
], DOC_DPS1_KEY
);
1558 doc_set_device_id(docg3
, 0);
1559 mutex_unlock(&docg3
->cascade
->lock
);
1563 #define FLOOR_SYSFS(id) { \
1564 __ATTR(f##id##_dps0_is_keylocked, S_IRUGO, dps0_is_key_locked, NULL), \
1565 __ATTR(f##id##_dps1_is_keylocked, S_IRUGO, dps1_is_key_locked, NULL), \
1566 __ATTR(f##id##_dps0_protection_key, S_IWUSR|S_IWGRP, NULL, dps0_insert_key), \
1567 __ATTR(f##id##_dps1_protection_key, S_IWUSR|S_IWGRP, NULL, dps1_insert_key), \
1570 static struct device_attribute doc_sys_attrs
[DOC_MAX_NBFLOORS
][4] = {
1571 FLOOR_SYSFS(0), FLOOR_SYSFS(1), FLOOR_SYSFS(2), FLOOR_SYSFS(3)
1574 static int doc_register_sysfs(struct platform_device
*pdev
,
1575 struct docg3_cascade
*cascade
)
1577 struct device
*dev
= &pdev
->dev
;
1583 floor
< DOC_MAX_NBFLOORS
&& cascade
->floors
[floor
];
1585 for (i
= 0; i
< 4; i
++) {
1586 ret
= device_create_file(dev
, &doc_sys_attrs
[floor
][i
]);
1597 device_remove_file(dev
, &doc_sys_attrs
[floor
][i
]);
1599 } while (--floor
>= 0);
1604 static void doc_unregister_sysfs(struct platform_device
*pdev
,
1605 struct docg3_cascade
*cascade
)
1607 struct device
*dev
= &pdev
->dev
;
1610 for (floor
= 0; floor
< DOC_MAX_NBFLOORS
&& cascade
->floors
[floor
];
1612 for (i
= 0; i
< 4; i
++)
1613 device_remove_file(dev
, &doc_sys_attrs
[floor
][i
]);
1617 * Debug sysfs entries
1619 static int dbg_flashctrl_show(struct seq_file
*s
, void *p
)
1621 struct docg3
*docg3
= (struct docg3
*)s
->private;
1625 mutex_lock(&docg3
->cascade
->lock
);
1626 fctrl
= doc_register_readb(docg3
, DOC_FLASHCONTROL
);
1627 mutex_unlock(&docg3
->cascade
->lock
);
1629 seq_printf(s
, "FlashControl : 0x%02x (%s,CE# %s,%s,%s,flash %s)\n",
1631 fctrl
& DOC_CTRL_VIOLATION
? "protocol violation" : "-",
1632 fctrl
& DOC_CTRL_CE
? "active" : "inactive",
1633 fctrl
& DOC_CTRL_PROTECTION_ERROR
? "protection error" : "-",
1634 fctrl
& DOC_CTRL_SEQUENCE_ERROR
? "sequence error" : "-",
1635 fctrl
& DOC_CTRL_FLASHREADY
? "ready" : "not ready");
1639 DEBUGFS_RO_ATTR(flashcontrol
, dbg_flashctrl_show
);
1641 static int dbg_asicmode_show(struct seq_file
*s
, void *p
)
1643 struct docg3
*docg3
= (struct docg3
*)s
->private;
1647 mutex_lock(&docg3
->cascade
->lock
);
1648 pctrl
= doc_register_readb(docg3
, DOC_ASICMODE
);
1649 mode
= pctrl
& 0x03;
1650 mutex_unlock(&docg3
->cascade
->lock
);
1653 "%04x : RAM_WE=%d,RSTIN_RESET=%d,BDETCT_RESET=%d,WRITE_ENABLE=%d,POWERDOWN=%d,MODE=%d%d (",
1655 pctrl
& DOC_ASICMODE_RAM_WE
? 1 : 0,
1656 pctrl
& DOC_ASICMODE_RSTIN_RESET
? 1 : 0,
1657 pctrl
& DOC_ASICMODE_BDETCT_RESET
? 1 : 0,
1658 pctrl
& DOC_ASICMODE_MDWREN
? 1 : 0,
1659 pctrl
& DOC_ASICMODE_POWERDOWN
? 1 : 0,
1660 mode
>> 1, mode
& 0x1);
1663 case DOC_ASICMODE_RESET
:
1664 seq_puts(s
, "reset");
1666 case DOC_ASICMODE_NORMAL
:
1667 seq_puts(s
, "normal");
1669 case DOC_ASICMODE_POWERDOWN
:
1670 seq_puts(s
, "powerdown");
1676 DEBUGFS_RO_ATTR(asic_mode
, dbg_asicmode_show
);
1678 static int dbg_device_id_show(struct seq_file
*s
, void *p
)
1680 struct docg3
*docg3
= (struct docg3
*)s
->private;
1683 mutex_lock(&docg3
->cascade
->lock
);
1684 id
= doc_register_readb(docg3
, DOC_DEVICESELECT
);
1685 mutex_unlock(&docg3
->cascade
->lock
);
1687 seq_printf(s
, "DeviceId = %d\n", id
);
1690 DEBUGFS_RO_ATTR(device_id
, dbg_device_id_show
);
1692 static int dbg_protection_show(struct seq_file
*s
, void *p
)
1694 struct docg3
*docg3
= (struct docg3
*)s
->private;
1695 int protect
, dps0
, dps0_low
, dps0_high
, dps1
, dps1_low
, dps1_high
;
1697 mutex_lock(&docg3
->cascade
->lock
);
1698 protect
= doc_register_readb(docg3
, DOC_PROTECTION
);
1699 dps0
= doc_register_readb(docg3
, DOC_DPS0_STATUS
);
1700 dps0_low
= doc_register_readw(docg3
, DOC_DPS0_ADDRLOW
);
1701 dps0_high
= doc_register_readw(docg3
, DOC_DPS0_ADDRHIGH
);
1702 dps1
= doc_register_readb(docg3
, DOC_DPS1_STATUS
);
1703 dps1_low
= doc_register_readw(docg3
, DOC_DPS1_ADDRLOW
);
1704 dps1_high
= doc_register_readw(docg3
, DOC_DPS1_ADDRHIGH
);
1705 mutex_unlock(&docg3
->cascade
->lock
);
1707 seq_printf(s
, "Protection = 0x%02x (", protect
);
1708 if (protect
& DOC_PROTECT_FOUNDRY_OTP_LOCK
)
1709 seq_puts(s
, "FOUNDRY_OTP_LOCK,");
1710 if (protect
& DOC_PROTECT_CUSTOMER_OTP_LOCK
)
1711 seq_puts(s
, "CUSTOMER_OTP_LOCK,");
1712 if (protect
& DOC_PROTECT_LOCK_INPUT
)
1713 seq_puts(s
, "LOCK_INPUT,");
1714 if (protect
& DOC_PROTECT_STICKY_LOCK
)
1715 seq_puts(s
, "STICKY_LOCK,");
1716 if (protect
& DOC_PROTECT_PROTECTION_ENABLED
)
1717 seq_puts(s
, "PROTECTION ON,");
1718 if (protect
& DOC_PROTECT_IPL_DOWNLOAD_LOCK
)
1719 seq_puts(s
, "IPL_DOWNLOAD_LOCK,");
1720 if (protect
& DOC_PROTECT_PROTECTION_ERROR
)
1721 seq_puts(s
, "PROTECT_ERR,");
1723 seq_puts(s
, "NO_PROTECT_ERR");
1726 seq_printf(s
, "DPS0 = 0x%02x : Protected area [0x%x - 0x%x] : OTP=%d, READ=%d, WRITE=%d, HW_LOCK=%d, KEY_OK=%d\n",
1727 dps0
, dps0_low
, dps0_high
,
1728 !!(dps0
& DOC_DPS_OTP_PROTECTED
),
1729 !!(dps0
& DOC_DPS_READ_PROTECTED
),
1730 !!(dps0
& DOC_DPS_WRITE_PROTECTED
),
1731 !!(dps0
& DOC_DPS_HW_LOCK_ENABLED
),
1732 !!(dps0
& DOC_DPS_KEY_OK
));
1733 seq_printf(s
, "DPS1 = 0x%02x : Protected area [0x%x - 0x%x] : OTP=%d, READ=%d, WRITE=%d, HW_LOCK=%d, KEY_OK=%d\n",
1734 dps1
, dps1_low
, dps1_high
,
1735 !!(dps1
& DOC_DPS_OTP_PROTECTED
),
1736 !!(dps1
& DOC_DPS_READ_PROTECTED
),
1737 !!(dps1
& DOC_DPS_WRITE_PROTECTED
),
1738 !!(dps1
& DOC_DPS_HW_LOCK_ENABLED
),
1739 !!(dps1
& DOC_DPS_KEY_OK
));
1742 DEBUGFS_RO_ATTR(protection
, dbg_protection_show
);
1744 static void __init
doc_dbg_register(struct mtd_info
*floor
)
1746 struct dentry
*root
= floor
->dbg
.dfs_dir
;
1747 struct docg3
*docg3
= floor
->priv
;
1749 if (IS_ERR_OR_NULL(root
)) {
1750 if (IS_ENABLED(CONFIG_DEBUG_FS
) &&
1751 !IS_ENABLED(CONFIG_MTD_PARTITIONED_MASTER
))
1752 dev_warn(floor
->dev
.parent
,
1753 "CONFIG_MTD_PARTITIONED_MASTER must be enabled to expose debugfs stuff\n");
1757 debugfs_create_file("docg3_flashcontrol", S_IRUSR
, root
, docg3
,
1758 &flashcontrol_fops
);
1759 debugfs_create_file("docg3_asic_mode", S_IRUSR
, root
, docg3
,
1761 debugfs_create_file("docg3_device_id", S_IRUSR
, root
, docg3
,
1763 debugfs_create_file("docg3_protection", S_IRUSR
, root
, docg3
,
1768 * doc_set_driver_info - Fill the mtd_info structure and docg3 structure
1769 * @chip_id: The chip ID of the supported chip
1770 * @mtd: The structure to fill
1772 static int __init
doc_set_driver_info(int chip_id
, struct mtd_info
*mtd
)
1774 struct docg3
*docg3
= mtd
->priv
;
1777 cfg
= doc_register_readb(docg3
, DOC_CONFIGURATION
);
1778 docg3
->if_cfg
= (cfg
& DOC_CONF_IF_CFG
? 1 : 0);
1779 docg3
->reliable
= reliable_mode
;
1783 mtd
->name
= kasprintf(GFP_KERNEL
, "docg3.%d",
1787 docg3
->max_block
= 2047;
1790 mtd
->type
= MTD_NANDFLASH
;
1791 mtd
->flags
= MTD_CAP_NANDFLASH
;
1792 mtd
->size
= (docg3
->max_block
+ 1) * DOC_LAYOUT_BLOCK_SIZE
;
1793 if (docg3
->reliable
== 2)
1795 mtd
->erasesize
= DOC_LAYOUT_BLOCK_SIZE
* DOC_LAYOUT_NBPLANES
;
1796 if (docg3
->reliable
== 2)
1797 mtd
->erasesize
/= 2;
1798 mtd
->writebufsize
= mtd
->writesize
= DOC_LAYOUT_PAGE_SIZE
;
1799 mtd
->oobsize
= DOC_LAYOUT_OOB_SIZE
;
1800 mtd
->_erase
= doc_erase
;
1801 mtd
->_read_oob
= doc_read_oob
;
1802 mtd
->_write_oob
= doc_write_oob
;
1803 mtd
->_block_isbad
= doc_block_isbad
;
1804 mtd_set_ooblayout(mtd
, &nand_ooblayout_docg3_ops
);
1806 mtd
->ecc_strength
= DOC_ECC_BCH_T
;
1812 * doc_probe_device - Check if a device is available
1813 * @base: the io space where the device is probed
1814 * @floor: the floor of the probed device
1816 * @cascade: the cascade of chips this devices will belong to
1818 * Checks whether a device at the specified IO range, and floor is available.
1820 * Returns a mtd_info struct if there is a device, ENODEV if none found, ENOMEM
1821 * if a memory allocation failed. If floor 0 is checked, a reset of the ASIC is
1824 static struct mtd_info
* __init
1825 doc_probe_device(struct docg3_cascade
*cascade
, int floor
, struct device
*dev
)
1827 int ret
, bbt_nbpages
;
1828 u16 chip_id
, chip_id_inv
;
1829 struct docg3
*docg3
;
1830 struct mtd_info
*mtd
;
1833 docg3
= kzalloc(sizeof(struct docg3
), GFP_KERNEL
);
1836 mtd
= kzalloc(sizeof(struct mtd_info
), GFP_KERNEL
);
1840 mtd
->dev
.parent
= dev
;
1841 bbt_nbpages
= DIV_ROUND_UP(docg3
->max_block
+ 1,
1842 8 * DOC_LAYOUT_PAGE_SIZE
);
1843 docg3
->bbt
= kzalloc(bbt_nbpages
* DOC_LAYOUT_PAGE_SIZE
, GFP_KERNEL
);
1848 docg3
->device_id
= floor
;
1849 docg3
->cascade
= cascade
;
1850 doc_set_device_id(docg3
, docg3
->device_id
);
1852 doc_set_asic_mode(docg3
, DOC_ASICMODE_RESET
);
1853 doc_set_asic_mode(docg3
, DOC_ASICMODE_NORMAL
);
1855 chip_id
= doc_register_readw(docg3
, DOC_CHIPID
);
1856 chip_id_inv
= doc_register_readw(docg3
, DOC_CHIPID_INV
);
1859 if (chip_id
!= (u16
)(~chip_id_inv
)) {
1865 doc_info("Found a G3 DiskOnChip at addr %p, floor %d\n",
1866 docg3
->cascade
->base
, floor
);
1869 doc_err("Chip id %04x is not a DiskOnChip G3 chip\n", chip_id
);
1873 ret
= doc_set_driver_info(chip_id
, mtd
);
1877 doc_hamming_ecc_init(docg3
, DOC_LAYOUT_OOB_PAGEINFO_SZ
);
1878 doc_reload_bbt(docg3
);
1888 return ERR_PTR(ret
);
1892 * doc_release_device - Release a docg3 floor
1895 static void doc_release_device(struct mtd_info
*mtd
)
1897 struct docg3
*docg3
= mtd
->priv
;
1899 mtd_device_unregister(mtd
);
1907 * docg3_resume - Awakens docg3 floor
1908 * @pdev: platfrom device
1910 * Returns 0 (always successful)
1912 static int docg3_resume(struct platform_device
*pdev
)
1915 struct docg3_cascade
*cascade
;
1916 struct mtd_info
**docg3_floors
, *mtd
;
1917 struct docg3
*docg3
;
1919 cascade
= platform_get_drvdata(pdev
);
1920 docg3_floors
= cascade
->floors
;
1921 mtd
= docg3_floors
[0];
1924 doc_dbg("docg3_resume()\n");
1925 for (i
= 0; i
< 12; i
++)
1926 doc_readb(docg3
, DOC_IOSPACE_IPL
);
1931 * docg3_suspend - Put in low power mode the docg3 floor
1932 * @pdev: platform device
1933 * @state: power state
1935 * Shuts off most of docg3 circuitery to lower power consumption.
1937 * Returns 0 if suspend succeeded, -EIO if chip refused suspend
1939 static int docg3_suspend(struct platform_device
*pdev
, pm_message_t state
)
1942 struct docg3_cascade
*cascade
;
1943 struct mtd_info
**docg3_floors
, *mtd
;
1944 struct docg3
*docg3
;
1947 cascade
= platform_get_drvdata(pdev
);
1948 docg3_floors
= cascade
->floors
;
1949 for (floor
= 0; floor
< DOC_MAX_NBFLOORS
; floor
++) {
1950 mtd
= docg3_floors
[floor
];
1955 doc_writeb(docg3
, floor
, DOC_DEVICESELECT
);
1956 ctrl
= doc_register_readb(docg3
, DOC_FLASHCONTROL
);
1957 ctrl
&= ~DOC_CTRL_VIOLATION
& ~DOC_CTRL_CE
;
1958 doc_writeb(docg3
, ctrl
, DOC_FLASHCONTROL
);
1960 for (i
= 0; i
< 10; i
++) {
1961 usleep_range(3000, 4000);
1962 pwr_down
= doc_register_readb(docg3
, DOC_POWERMODE
);
1963 if (pwr_down
& DOC_POWERDOWN_READY
)
1966 if (pwr_down
& DOC_POWERDOWN_READY
) {
1967 doc_dbg("docg3_suspend(): floor %d powerdown ok\n",
1970 doc_err("docg3_suspend(): floor %d powerdown failed\n",
1976 mtd
= docg3_floors
[0];
1978 doc_set_asic_mode(docg3
, DOC_ASICMODE_POWERDOWN
);
1983 * doc_probe - Probe the IO space for a DiskOnChip G3 chip
1984 * @pdev: platform device
1986 * Probes for a G3 chip at the specified IO space in the platform data
1987 * ressources. The floor 0 must be available.
1989 * Returns 0 on success, -ENOMEM, -ENXIO on error
1991 static int __init
docg3_probe(struct platform_device
*pdev
)
1993 struct device
*dev
= &pdev
->dev
;
1994 struct mtd_info
*mtd
;
1995 struct resource
*ress
;
1998 struct docg3_cascade
*cascade
;
2001 ress
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
2003 dev_err(dev
, "No I/O memory resource defined\n");
2006 base
= devm_ioremap(dev
, ress
->start
, DOC_IOSPACE_SIZE
);
2009 cascade
= devm_kzalloc(dev
, sizeof(*cascade
) * DOC_MAX_NBFLOORS
,
2013 cascade
->base
= base
;
2014 mutex_init(&cascade
->lock
);
2015 cascade
->bch
= init_bch(DOC_ECC_BCH_M
, DOC_ECC_BCH_T
,
2016 DOC_ECC_BCH_PRIMPOLY
);
2020 for (floor
= 0; floor
< DOC_MAX_NBFLOORS
; floor
++) {
2021 mtd
= doc_probe_device(cascade
, floor
, dev
);
2032 cascade
->floors
[floor
] = mtd
;
2033 ret
= mtd_device_parse_register(mtd
, part_probes
, NULL
, NULL
,
2038 doc_dbg_register(cascade
->floors
[floor
]);
2041 ret
= doc_register_sysfs(pdev
, cascade
);
2045 platform_set_drvdata(pdev
, cascade
);
2050 dev_info(dev
, "No supported DiskOnChip found\n");
2052 free_bch(cascade
->bch
);
2053 for (floor
= 0; floor
< DOC_MAX_NBFLOORS
; floor
++)
2054 if (cascade
->floors
[floor
])
2055 doc_release_device(cascade
->floors
[floor
]);
2060 * docg3_release - Release the driver
2061 * @pdev: the platform device
2065 static int docg3_release(struct platform_device
*pdev
)
2067 struct docg3_cascade
*cascade
= platform_get_drvdata(pdev
);
2068 struct docg3
*docg3
= cascade
->floors
[0]->priv
;
2071 doc_unregister_sysfs(pdev
, cascade
);
2072 for (floor
= 0; floor
< DOC_MAX_NBFLOORS
; floor
++)
2073 if (cascade
->floors
[floor
])
2074 doc_release_device(cascade
->floors
[floor
]);
2076 free_bch(docg3
->cascade
->bch
);
2081 static const struct of_device_id docg3_dt_ids
[] = {
2082 { .compatible
= "m-systems,diskonchip-g3" },
2085 MODULE_DEVICE_TABLE(of
, docg3_dt_ids
);
2088 static struct platform_driver g3_driver
= {
2091 .of_match_table
= of_match_ptr(docg3_dt_ids
),
2093 .suspend
= docg3_suspend
,
2094 .resume
= docg3_resume
,
2095 .remove
= docg3_release
,
2098 module_platform_driver_probe(g3_driver
, docg3_probe
);
2100 MODULE_LICENSE("GPL");
2101 MODULE_AUTHOR("Robert Jarzmik <robert.jarzmik@free.fr>");
2102 MODULE_DESCRIPTION("MTD driver for DiskOnChip G3");