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>
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
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 :
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
56 * - BCH is in GF(2^14)
57 * - BCH is over data of 520 bytes (512 page + 7 page_info bytes
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");
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
= {
78 .eccpos
= {7, 8, 9, 10, 11, 12, 13, 14},
79 .oobfree
= {{0, 7}, {15, 1} },
83 static inline u8
doc_readb(struct docg3
*docg3
, u16 reg
)
85 u8 val
= readb(docg3
->cascade
->base
+ reg
);
87 trace_docg3_io(0, 8, reg
, (int)val
);
91 static inline u16
doc_readw(struct docg3
*docg3
, u16 reg
)
93 u16 val
= readw(docg3
->cascade
->base
+ reg
);
95 trace_docg3_io(0, 16, reg
, (int)val
);
99 static inline void doc_writeb(struct docg3
*docg3
, u8 val
, u16 reg
)
101 writeb(val
, docg3
->cascade
->base
+ reg
);
102 trace_docg3_io(1, 8, reg
, val
);
105 static inline void doc_writew(struct docg3
*docg3
, u16 val
, u16 reg
)
107 writew(val
, docg3
->cascade
->base
+ reg
);
108 trace_docg3_io(1, 16, reg
, val
);
111 static inline void doc_flash_command(struct docg3
*docg3
, u8 cmd
)
113 doc_writeb(docg3
, cmd
, DOC_FLASHCOMMAND
);
116 static inline void doc_flash_sequence(struct docg3
*docg3
, u8 seq
)
118 doc_writeb(docg3
, seq
, DOC_FLASHSEQUENCE
);
121 static inline void doc_flash_address(struct docg3
*docg3
, u8 addr
)
123 doc_writeb(docg3
, addr
, DOC_FLASHADDRESS
);
126 static char const *part_probes
[] = { "cmdlinepart", "saftlpart", NULL
};
128 static int doc_register_readb(struct docg3
*docg3
, int reg
)
132 doc_writew(docg3
, reg
, DOC_READADDRESS
);
133 val
= doc_readb(docg3
, reg
);
134 doc_vdbg("Read register %04x : %02x\n", reg
, val
);
138 static int doc_register_readw(struct docg3
*docg3
, int reg
)
142 doc_writew(docg3
, reg
, DOC_READADDRESS
);
143 val
= doc_readw(docg3
, reg
);
144 doc_vdbg("Read register %04x : %04x\n", reg
, val
);
149 * doc_delay - delay docg3 operations
151 * @nbNOPs: the number of NOPs to issue
153 * As no specification is available, the right timings between chip commands are
154 * unknown. The only available piece of information are the observed nops on a
155 * working docg3 chip.
156 * Therefore, doc_delay relies on a busy loop of NOPs, instead of scheduler
157 * friendlier msleep() functions or blocking mdelay().
159 static void doc_delay(struct docg3
*docg3
, int nbNOPs
)
163 doc_vdbg("NOP x %d\n", nbNOPs
);
164 for (i
= 0; i
< nbNOPs
; i
++)
165 doc_writeb(docg3
, 0, DOC_NOP
);
168 static int is_prot_seq_error(struct docg3
*docg3
)
172 ctrl
= doc_register_readb(docg3
, DOC_FLASHCONTROL
);
173 return ctrl
& (DOC_CTRL_PROTECTION_ERROR
| DOC_CTRL_SEQUENCE_ERROR
);
176 static int doc_is_ready(struct docg3
*docg3
)
180 ctrl
= doc_register_readb(docg3
, DOC_FLASHCONTROL
);
181 return ctrl
& DOC_CTRL_FLASHREADY
;
184 static int doc_wait_ready(struct docg3
*docg3
)
186 int maxWaitCycles
= 100;
191 } while (!doc_is_ready(docg3
) && maxWaitCycles
--);
193 if (maxWaitCycles
> 0)
199 static int doc_reset_seq(struct docg3
*docg3
)
203 doc_writeb(docg3
, 0x10, DOC_FLASHCONTROL
);
204 doc_flash_sequence(docg3
, DOC_SEQ_RESET
);
205 doc_flash_command(docg3
, DOC_CMD_RESET
);
207 ret
= doc_wait_ready(docg3
);
209 doc_dbg("doc_reset_seq() -> isReady=%s\n", ret
? "false" : "true");
214 * doc_read_data_area - Read data from data area
216 * @buf: the buffer to fill in (might be NULL is dummy reads)
217 * @len: the length to read
218 * @first: first time read, DOC_READADDRESS should be set
220 * Reads bytes from flash data. Handles the single byte / even bytes reads.
222 static void doc_read_data_area(struct docg3
*docg3
, void *buf
, int len
,
229 doc_dbg("doc_read_data_area(buf=%p, len=%d)\n", buf
, len
);
234 doc_writew(docg3
, DOC_IOSPACE_DATA
, DOC_READADDRESS
);
236 for (i
= 0; i
< len4
; i
+= 2) {
237 data16
= doc_readw(docg3
, DOC_IOSPACE_DATA
);
245 doc_writew(docg3
, DOC_IOSPACE_DATA
| DOC_READADDR_ONE_BYTE
,
249 for (i
= 0; i
< cdr
; i
++) {
250 data8
= doc_readb(docg3
, DOC_IOSPACE_DATA
);
260 * doc_write_data_area - Write data into data area
262 * @buf: the buffer to get input bytes from
263 * @len: the length to write
265 * Writes bytes into flash data. Handles the single byte / even bytes writes.
267 static void doc_write_data_area(struct docg3
*docg3
, const void *buf
, int len
)
273 doc_dbg("doc_write_data_area(buf=%p, len=%d)\n", buf
, len
);
277 doc_writew(docg3
, DOC_IOSPACE_DATA
, DOC_READADDRESS
);
279 for (i
= 0; i
< len4
; i
+= 2) {
280 doc_writew(docg3
, *src16
, DOC_IOSPACE_DATA
);
285 for (i
= 0; i
< cdr
; i
++) {
286 doc_writew(docg3
, DOC_IOSPACE_DATA
| DOC_READADDR_ONE_BYTE
,
288 doc_writeb(docg3
, *src8
, DOC_IOSPACE_DATA
);
294 * doc_set_data_mode - Sets the flash to normal or reliable data mode
297 * The reliable data mode is a bit slower than the fast mode, but less errors
298 * occur. Entering the reliable mode cannot be done without entering the fast
301 * In reliable mode, pages 2*n and 2*n+1 are clones. Writing to page 0 of blocks
302 * (4,5) make the hardware write also to page 1 of blocks blocks(4,5). Reading
303 * from page 0 of blocks (4,5) or from page 1 of blocks (4,5) gives the same
304 * result, which is a logical and between bytes from page 0 and page 1 (which is
305 * consistent with the fact that writing to a page is _clearing_ bits of that
308 static void doc_set_reliable_mode(struct docg3
*docg3
)
310 static char *strmode
[] = { "normal", "fast", "reliable", "invalid" };
312 doc_dbg("doc_set_reliable_mode(%s)\n", strmode
[docg3
->reliable
]);
313 switch (docg3
->reliable
) {
317 doc_flash_sequence(docg3
, DOC_SEQ_SET_FASTMODE
);
318 doc_flash_command(docg3
, DOC_CMD_FAST_MODE
);
321 doc_flash_sequence(docg3
, DOC_SEQ_SET_RELIABLEMODE
);
322 doc_flash_command(docg3
, DOC_CMD_FAST_MODE
);
323 doc_flash_command(docg3
, DOC_CMD_RELIABLE_MODE
);
326 doc_err("doc_set_reliable_mode(): invalid mode\n");
333 * doc_set_asic_mode - Set the ASIC mode
337 * The ASIC can work in 3 modes :
338 * - RESET: all registers are zeroed
339 * - NORMAL: receives and handles commands
340 * - POWERDOWN: minimal poweruse, flash parts shut off
342 static void doc_set_asic_mode(struct docg3
*docg3
, u8 mode
)
346 for (i
= 0; i
< 12; i
++)
347 doc_readb(docg3
, DOC_IOSPACE_IPL
);
349 mode
|= DOC_ASICMODE_MDWREN
;
350 doc_dbg("doc_set_asic_mode(%02x)\n", mode
);
351 doc_writeb(docg3
, mode
, DOC_ASICMODE
);
352 doc_writeb(docg3
, ~mode
, DOC_ASICMODECONFIRM
);
357 * doc_set_device_id - Sets the devices id for cascaded G3 chips
359 * @id: the chip to select (amongst 0, 1, 2, 3)
361 * There can be 4 cascaded G3 chips. This function selects the one which will
362 * should be the active one.
364 static void doc_set_device_id(struct docg3
*docg3
, int id
)
368 doc_dbg("doc_set_device_id(%d)\n", id
);
369 doc_writeb(docg3
, id
, DOC_DEVICESELECT
);
370 ctrl
= doc_register_readb(docg3
, DOC_FLASHCONTROL
);
372 ctrl
&= ~DOC_CTRL_VIOLATION
;
374 doc_writeb(docg3
, ctrl
, DOC_FLASHCONTROL
);
378 * doc_set_extra_page_mode - Change flash page layout
381 * Normally, the flash page is split into the data (512 bytes) and the out of
382 * band data (16 bytes). For each, 4 more bytes can be accessed, where the wear
383 * leveling counters are stored. To access this last area of 4 bytes, a special
384 * mode must be input to the flash ASIC.
386 * Returns 0 if no error occurred, -EIO else.
388 static int doc_set_extra_page_mode(struct docg3
*docg3
)
392 doc_dbg("doc_set_extra_page_mode()\n");
393 doc_flash_sequence(docg3
, DOC_SEQ_PAGE_SIZE_532
);
394 doc_flash_command(docg3
, DOC_CMD_PAGE_SIZE_532
);
397 fctrl
= doc_register_readb(docg3
, DOC_FLASHCONTROL
);
398 if (fctrl
& (DOC_CTRL_PROTECTION_ERROR
| DOC_CTRL_SEQUENCE_ERROR
))
405 * doc_setup_addr_sector - Setup blocks/page/ofs address for one plane
407 * @sector: the sector
409 static void doc_setup_addr_sector(struct docg3
*docg3
, int sector
)
412 doc_flash_address(docg3
, sector
& 0xff);
413 doc_flash_address(docg3
, (sector
>> 8) & 0xff);
414 doc_flash_address(docg3
, (sector
>> 16) & 0xff);
419 * doc_setup_writeaddr_sector - Setup blocks/page/ofs address for one plane
421 * @sector: the sector
422 * @ofs: the offset in the page, between 0 and (512 + 16 + 512)
424 static void doc_setup_writeaddr_sector(struct docg3
*docg3
, int sector
, int ofs
)
428 doc_flash_address(docg3
, ofs
& 0xff);
429 doc_flash_address(docg3
, sector
& 0xff);
430 doc_flash_address(docg3
, (sector
>> 8) & 0xff);
431 doc_flash_address(docg3
, (sector
>> 16) & 0xff);
436 * doc_seek - Set both flash planes to the specified block, page for reading
438 * @block0: the first plane block index
439 * @block1: the second plane block index
440 * @page: the page index within the block
441 * @wear: if true, read will occur on the 4 extra bytes of the wear area
442 * @ofs: offset in page to read
444 * Programs the flash even and odd planes to the specific block and page.
445 * Alternatively, programs the flash to the wear area of the specified page.
447 static int doc_read_seek(struct docg3
*docg3
, int block0
, int block1
, int page
,
452 doc_dbg("doc_seek(blocks=(%d,%d), page=%d, ofs=%d, wear=%d)\n",
453 block0
, block1
, page
, ofs
, wear
);
455 if (!wear
&& (ofs
< 2 * DOC_LAYOUT_PAGE_SIZE
)) {
456 doc_flash_sequence(docg3
, DOC_SEQ_SET_PLANE1
);
457 doc_flash_command(docg3
, DOC_CMD_READ_PLANE1
);
460 doc_flash_sequence(docg3
, DOC_SEQ_SET_PLANE2
);
461 doc_flash_command(docg3
, DOC_CMD_READ_PLANE2
);
465 doc_set_reliable_mode(docg3
);
467 ret
= doc_set_extra_page_mode(docg3
);
471 doc_flash_sequence(docg3
, DOC_SEQ_READ
);
472 sector
= (block0
<< DOC_ADDR_BLOCK_SHIFT
) + (page
& DOC_ADDR_PAGE_MASK
);
473 doc_flash_command(docg3
, DOC_CMD_PROG_BLOCK_ADDR
);
474 doc_setup_addr_sector(docg3
, sector
);
476 sector
= (block1
<< DOC_ADDR_BLOCK_SHIFT
) + (page
& DOC_ADDR_PAGE_MASK
);
477 doc_flash_command(docg3
, DOC_CMD_PROG_BLOCK_ADDR
);
478 doc_setup_addr_sector(docg3
, sector
);
486 * doc_write_seek - Set both flash planes to the specified block, page for writing
488 * @block0: the first plane block index
489 * @block1: the second plane block index
490 * @page: the page index within the block
491 * @ofs: offset in page to write
493 * Programs the flash even and odd planes to the specific block and page.
494 * Alternatively, programs the flash to the wear area of the specified page.
496 static int doc_write_seek(struct docg3
*docg3
, int block0
, int block1
, int page
,
501 doc_dbg("doc_write_seek(blocks=(%d,%d), page=%d, ofs=%d)\n",
502 block0
, block1
, page
, ofs
);
504 doc_set_reliable_mode(docg3
);
506 if (ofs
< 2 * DOC_LAYOUT_PAGE_SIZE
) {
507 doc_flash_sequence(docg3
, DOC_SEQ_SET_PLANE1
);
508 doc_flash_command(docg3
, DOC_CMD_READ_PLANE1
);
511 doc_flash_sequence(docg3
, DOC_SEQ_SET_PLANE2
);
512 doc_flash_command(docg3
, DOC_CMD_READ_PLANE2
);
516 doc_flash_sequence(docg3
, DOC_SEQ_PAGE_SETUP
);
517 doc_flash_command(docg3
, DOC_CMD_PROG_CYCLE1
);
519 sector
= (block0
<< DOC_ADDR_BLOCK_SHIFT
) + (page
& DOC_ADDR_PAGE_MASK
);
520 doc_setup_writeaddr_sector(docg3
, sector
, ofs
);
522 doc_flash_command(docg3
, DOC_CMD_PROG_CYCLE3
);
524 ret
= doc_wait_ready(docg3
);
528 doc_flash_command(docg3
, DOC_CMD_PROG_CYCLE1
);
529 sector
= (block1
<< DOC_ADDR_BLOCK_SHIFT
) + (page
& DOC_ADDR_PAGE_MASK
);
530 doc_setup_writeaddr_sector(docg3
, sector
, ofs
);
539 * doc_read_page_ecc_init - Initialize hardware ECC engine
541 * @len: the number of bytes covered by the ECC (BCH covered)
543 * The function does initialize the hardware ECC engine to compute the Hamming
544 * ECC (on 1 byte) and the BCH hardware ECC (on 7 bytes).
546 * Return 0 if succeeded, -EIO on error
548 static int doc_read_page_ecc_init(struct docg3
*docg3
, int len
)
550 doc_writew(docg3
, DOC_ECCCONF0_READ_MODE
551 | DOC_ECCCONF0_BCH_ENABLE
| DOC_ECCCONF0_HAMMING_ENABLE
552 | (len
& DOC_ECCCONF0_DATA_BYTES_MASK
),
555 doc_register_readb(docg3
, DOC_FLASHCONTROL
);
556 return doc_wait_ready(docg3
);
560 * doc_write_page_ecc_init - Initialize hardware BCH ECC engine
562 * @len: the number of bytes covered by the ECC (BCH covered)
564 * The function does initialize the hardware ECC engine to compute the Hamming
565 * ECC (on 1 byte) and the BCH hardware ECC (on 7 bytes).
567 * Return 0 if succeeded, -EIO on error
569 static int doc_write_page_ecc_init(struct docg3
*docg3
, int len
)
571 doc_writew(docg3
, DOC_ECCCONF0_WRITE_MODE
572 | DOC_ECCCONF0_BCH_ENABLE
| DOC_ECCCONF0_HAMMING_ENABLE
573 | (len
& DOC_ECCCONF0_DATA_BYTES_MASK
),
576 doc_register_readb(docg3
, DOC_FLASHCONTROL
);
577 return doc_wait_ready(docg3
);
581 * doc_ecc_disable - Disable Hamming and BCH ECC hardware calculator
584 * Disables the hardware ECC generator and checker, for unchecked reads (as when
585 * reading OOB only or write status byte).
587 static void doc_ecc_disable(struct docg3
*docg3
)
589 doc_writew(docg3
, DOC_ECCCONF0_READ_MODE
, DOC_ECCCONF0
);
594 * doc_hamming_ecc_init - Initialize hardware Hamming ECC engine
596 * @nb_bytes: the number of bytes covered by the ECC (Hamming covered)
598 * This function programs the ECC hardware to compute the hamming code on the
599 * last provided N bytes to the hardware generator.
601 static void doc_hamming_ecc_init(struct docg3
*docg3
, int nb_bytes
)
605 ecc_conf1
= doc_register_readb(docg3
, DOC_ECCCONF1
);
606 ecc_conf1
&= ~DOC_ECCCONF1_HAMMING_BITS_MASK
;
607 ecc_conf1
|= (nb_bytes
& DOC_ECCCONF1_HAMMING_BITS_MASK
);
608 doc_writeb(docg3
, ecc_conf1
, DOC_ECCCONF1
);
612 * doc_ecc_bch_fix_data - Fix if need be read data from flash
614 * @buf: the buffer of read data (512 + 7 + 1 bytes)
615 * @hwecc: the hardware calculated ECC.
616 * It's in fact recv_ecc ^ calc_ecc, where recv_ecc was read from OOB
617 * area data, and calc_ecc the ECC calculated by the hardware generator.
619 * Checks if the received data matches the ECC, and if an error is detected,
620 * tries to fix the bit flips (at most 4) in the buffer buf. As the docg3
621 * understands the (data, ecc, syndroms) in an inverted order in comparison to
622 * the BCH library, the function reverses the order of bits (ie. bit7 and bit0,
623 * bit6 and bit 1, ...) for all ECC data.
625 * The hardware ecc unit produces oob_ecc ^ calc_ecc. The kernel's bch
626 * algorithm is used to decode this. However the hw operates on page
627 * data in a bit order that is the reverse of that of the bch alg,
628 * requiring that the bits be reversed on the result. Thanks to Ivan
629 * Djelic for his analysis.
631 * Returns number of fixed bits (0, 1, 2, 3, 4) or -EBADMSG if too many bit
632 * errors were detected and cannot be fixed.
634 static int doc_ecc_bch_fix_data(struct docg3
*docg3
, void *buf
, u8
*hwecc
)
636 u8 ecc
[DOC_ECC_BCH_SIZE
];
637 int errorpos
[DOC_ECC_BCH_T
], i
, numerrs
;
639 for (i
= 0; i
< DOC_ECC_BCH_SIZE
; i
++)
640 ecc
[i
] = bitrev8(hwecc
[i
]);
641 numerrs
= decode_bch(docg3
->cascade
->bch
, NULL
,
642 DOC_ECC_BCH_COVERED_BYTES
,
643 NULL
, ecc
, NULL
, errorpos
);
644 BUG_ON(numerrs
== -EINVAL
);
648 for (i
= 0; i
< numerrs
; i
++)
649 errorpos
[i
] = (errorpos
[i
] & ~7) | (7 - (errorpos
[i
] & 7));
650 for (i
= 0; i
< numerrs
; i
++)
651 if (errorpos
[i
] < DOC_ECC_BCH_COVERED_BYTES
*8)
652 /* error is located in data, correct it */
653 change_bit(errorpos
[i
], buf
);
655 doc_dbg("doc_ecc_bch_fix_data: flipped %d bits\n", numerrs
);
661 * doc_read_page_prepare - Prepares reading data from a flash page
663 * @block0: the first plane block index on flash memory
664 * @block1: the second plane block index on flash memory
665 * @page: the page index in the block
666 * @offset: the offset in the page (must be a multiple of 4)
668 * Prepares the page to be read in the flash memory :
669 * - tell ASIC to map the flash pages
670 * - tell ASIC to be in read mode
672 * After a call to this method, a call to doc_read_page_finish is mandatory,
673 * to end the read cycle of the flash.
675 * Read data from a flash page. The length to be read must be between 0 and
676 * (page_size + oob_size + wear_size), ie. 532, and a multiple of 4 (because
677 * the extra bytes reading is not implemented).
679 * As pages are grouped by 2 (in 2 planes), reading from a page must be done
681 * - one read of 512 bytes at offset 0
682 * - one read of 512 bytes at offset 512 + 16
684 * Returns 0 if successful, -EIO if a read error occurred.
686 static int doc_read_page_prepare(struct docg3
*docg3
, int block0
, int block1
,
687 int page
, int offset
)
689 int wear_area
= 0, ret
= 0;
691 doc_dbg("doc_read_page_prepare(blocks=(%d,%d), page=%d, ofsInPage=%d)\n",
692 block0
, block1
, page
, offset
);
693 if (offset
>= DOC_LAYOUT_WEAR_OFFSET
)
695 if (!wear_area
&& offset
> (DOC_LAYOUT_PAGE_OOB_SIZE
* 2))
698 doc_set_device_id(docg3
, docg3
->device_id
);
699 ret
= doc_reset_seq(docg3
);
703 /* Program the flash address block and page */
704 ret
= doc_read_seek(docg3
, block0
, block1
, page
, wear_area
, offset
);
708 doc_flash_command(docg3
, DOC_CMD_READ_ALL_PLANES
);
710 doc_wait_ready(docg3
);
712 doc_flash_command(docg3
, DOC_CMD_SET_ADDR_READ
);
714 if (offset
>= DOC_LAYOUT_PAGE_SIZE
* 2)
715 offset
-= 2 * DOC_LAYOUT_PAGE_SIZE
;
716 doc_flash_address(docg3
, offset
>> 2);
718 doc_wait_ready(docg3
);
720 doc_flash_command(docg3
, DOC_CMD_READ_FLASH
);
724 doc_writeb(docg3
, 0, DOC_DATAEND
);
730 * doc_read_page_getbytes - Reads bytes from a prepared page
732 * @len: the number of bytes to be read (must be a multiple of 4)
733 * @buf: the buffer to be filled in (or NULL is forget bytes)
734 * @first: 1 if first time read, DOC_READADDRESS should be set
735 * @last_odd: 1 if last read ended up on an odd byte
737 * Reads bytes from a prepared page. There is a trickery here : if the last read
738 * ended up on an odd offset in the 1024 bytes double page, ie. between the 2
739 * planes, the first byte must be read apart. If a word (16bit) read was used,
740 * the read would return the byte of plane 2 as low *and* high endian, which
741 * will mess the read.
744 static int doc_read_page_getbytes(struct docg3
*docg3
, int len
, u_char
*buf
,
745 int first
, int last_odd
)
747 if (last_odd
&& len
> 0) {
748 doc_read_data_area(docg3
, buf
, 1, first
);
749 doc_read_data_area(docg3
, buf
? buf
+ 1 : buf
, len
- 1, 0);
751 doc_read_data_area(docg3
, buf
, len
, first
);
758 * doc_write_page_putbytes - Writes bytes into a prepared page
760 * @len: the number of bytes to be written
761 * @buf: the buffer of input bytes
764 static void doc_write_page_putbytes(struct docg3
*docg3
, int len
,
767 doc_write_data_area(docg3
, buf
, len
);
772 * doc_get_bch_hw_ecc - Get hardware calculated BCH ECC
774 * @hwecc: the array of 7 integers where the hardware ecc will be stored
776 static void doc_get_bch_hw_ecc(struct docg3
*docg3
, u8
*hwecc
)
780 for (i
= 0; i
< DOC_ECC_BCH_SIZE
; i
++)
781 hwecc
[i
] = doc_register_readb(docg3
, DOC_BCH_HW_ECC(i
));
785 * doc_page_finish - Ends reading/writing of a flash page
788 static void doc_page_finish(struct docg3
*docg3
)
790 doc_writeb(docg3
, 0, DOC_DATAEND
);
795 * doc_read_page_finish - Ends reading of a flash page
798 * As a side effect, resets the chip selector to 0. This ensures that after each
799 * read operation, the floor 0 is selected. Therefore, if the systems halts, the
800 * reboot will boot on floor 0, where the IPL is.
802 static void doc_read_page_finish(struct docg3
*docg3
)
804 doc_page_finish(docg3
);
805 doc_set_device_id(docg3
, 0);
809 * calc_block_sector - Calculate blocks, pages and ofs.
811 * @from: offset in flash
812 * @block0: first plane block index calculated
813 * @block1: second plane block index calculated
814 * @page: page calculated
815 * @ofs: offset in page
816 * @reliable: 0 if docg3 in normal mode, 1 if docg3 in fast mode, 2 if docg3 in
819 * The calculation is based on the reliable/normal mode. In normal mode, the 64
820 * pages of a block are available. In reliable mode, as pages 2*n and 2*n+1 are
821 * clones, only 32 pages per block are available.
823 static void calc_block_sector(loff_t from
, int *block0
, int *block1
, int *page
,
824 int *ofs
, int reliable
)
826 uint sector
, pages_biblock
;
828 pages_biblock
= DOC_LAYOUT_PAGES_PER_BLOCK
* DOC_LAYOUT_NBPLANES
;
829 if (reliable
== 1 || reliable
== 2)
832 sector
= from
/ DOC_LAYOUT_PAGE_SIZE
;
833 *block0
= sector
/ pages_biblock
* DOC_LAYOUT_NBPLANES
;
834 *block1
= *block0
+ 1;
835 *page
= sector
% pages_biblock
;
836 *page
/= DOC_LAYOUT_NBPLANES
;
837 if (reliable
== 1 || reliable
== 2)
840 *ofs
= DOC_LAYOUT_PAGE_OOB_SIZE
;
846 * doc_read_oob - Read out of band bytes from flash
848 * @from: the offset from first block and first page, in bytes, aligned on page
850 * @ops: the mtd oob structure
852 * Reads flash memory OOB area of pages.
854 * Returns 0 if read successful, of -EIO, -EINVAL if an error occurred
856 static int doc_read_oob(struct mtd_info
*mtd
, loff_t from
,
857 struct mtd_oob_ops
*ops
)
859 struct docg3
*docg3
= mtd
->priv
;
860 int block0
, block1
, page
, ret
, skip
, ofs
= 0;
861 u8
*oobbuf
= ops
->oobbuf
;
862 u8
*buf
= ops
->datbuf
;
863 size_t len
, ooblen
, nbdata
, nboob
;
864 u8 hwecc
[DOC_ECC_BCH_SIZE
], eccconf1
;
865 int max_bitflips
= 0;
872 ooblen
= ops
->ooblen
;
876 if (oobbuf
&& ops
->mode
== MTD_OPS_PLACE_OOB
)
877 oobbuf
+= ops
->ooboffs
;
879 doc_dbg("doc_read_oob(from=%lld, mode=%d, data=(%p:%zu), oob=(%p:%zu))\n",
880 from
, ops
->mode
, buf
, len
, oobbuf
, ooblen
);
881 if (ooblen
% DOC_LAYOUT_OOB_SIZE
)
884 if (from
+ len
> mtd
->size
)
890 skip
= from
% DOC_LAYOUT_PAGE_SIZE
;
891 mutex_lock(&docg3
->cascade
->lock
);
892 while (ret
>= 0 && (len
> 0 || ooblen
> 0)) {
893 calc_block_sector(from
- skip
, &block0
, &block1
, &page
, &ofs
,
895 nbdata
= min_t(size_t, len
, DOC_LAYOUT_PAGE_SIZE
- skip
);
896 nboob
= min_t(size_t, ooblen
, (size_t)DOC_LAYOUT_OOB_SIZE
);
897 ret
= doc_read_page_prepare(docg3
, block0
, block1
, page
, ofs
);
900 ret
= doc_read_page_ecc_init(docg3
, DOC_ECC_BCH_TOTAL_BYTES
);
903 ret
= doc_read_page_getbytes(docg3
, skip
, NULL
, 1, 0);
906 ret
= doc_read_page_getbytes(docg3
, nbdata
, buf
, 0, skip
% 2);
909 doc_read_page_getbytes(docg3
,
910 DOC_LAYOUT_PAGE_SIZE
- nbdata
- skip
,
911 NULL
, 0, (skip
+ nbdata
) % 2);
912 ret
= doc_read_page_getbytes(docg3
, nboob
, oobbuf
, 0, 0);
915 doc_read_page_getbytes(docg3
, DOC_LAYOUT_OOB_SIZE
- nboob
,
918 doc_get_bch_hw_ecc(docg3
, hwecc
);
919 eccconf1
= doc_register_readb(docg3
, DOC_ECCCONF1
);
921 if (nboob
>= DOC_LAYOUT_OOB_SIZE
) {
922 doc_dbg("OOB - INFO: %02x:%02x:%02x:%02x:%02x:%02x:%02x\n",
923 oobbuf
[0], oobbuf
[1], oobbuf
[2], oobbuf
[3],
924 oobbuf
[4], oobbuf
[5], oobbuf
[6]);
925 doc_dbg("OOB - HAMMING: %02x\n", oobbuf
[7]);
926 doc_dbg("OOB - BCH_ECC: %02x:%02x:%02x:%02x:%02x:%02x:%02x\n",
927 oobbuf
[8], oobbuf
[9], oobbuf
[10], oobbuf
[11],
928 oobbuf
[12], oobbuf
[13], oobbuf
[14]);
929 doc_dbg("OOB - UNUSED: %02x\n", oobbuf
[15]);
931 doc_dbg("ECC checks: ECCConf1=%x\n", eccconf1
);
932 doc_dbg("ECC HW_ECC: %02x:%02x:%02x:%02x:%02x:%02x:%02x\n",
933 hwecc
[0], hwecc
[1], hwecc
[2], hwecc
[3], hwecc
[4],
937 if (is_prot_seq_error(docg3
))
940 if ((block0
>= DOC_LAYOUT_BLOCK_FIRST_DATA
) &&
941 (eccconf1
& DOC_ECCCONF1_BCH_SYNDROM_ERR
) &&
942 (eccconf1
& DOC_ECCCONF1_PAGE_IS_WRITTEN
) &&
943 (ops
->mode
!= MTD_OPS_RAW
) &&
944 (nbdata
== DOC_LAYOUT_PAGE_SIZE
)) {
945 ret
= doc_ecc_bch_fix_data(docg3
, buf
, hwecc
);
947 mtd
->ecc_stats
.failed
++;
951 mtd
->ecc_stats
.corrected
+= ret
;
952 max_bitflips
= max(max_bitflips
, ret
);
957 doc_read_page_finish(docg3
);
958 ops
->retlen
+= nbdata
;
959 ops
->oobretlen
+= nboob
;
964 from
+= DOC_LAYOUT_PAGE_SIZE
;
969 mutex_unlock(&docg3
->cascade
->lock
);
972 doc_read_page_finish(docg3
);
977 * doc_read - Read bytes from flash
979 * @from: the offset from first block and first page, in bytes, aligned on page
981 * @len: the number of bytes to read (must be a multiple of 4)
982 * @retlen: the number of bytes actually read
983 * @buf: the filled in buffer
985 * Reads flash memory pages. This function does not read the OOB chunk, but only
988 * Returns 0 if read successful, of -EIO, -EINVAL if an error occurred
990 static int doc_read(struct mtd_info
*mtd
, loff_t from
, size_t len
,
991 size_t *retlen
, u_char
*buf
)
993 struct mtd_oob_ops ops
;
996 memset(&ops
, 0, sizeof(ops
));
999 ops
.mode
= MTD_OPS_AUTO_OOB
;
1001 ret
= doc_read_oob(mtd
, from
, &ops
);
1002 *retlen
= ops
.retlen
;
1006 static int doc_reload_bbt(struct docg3
*docg3
)
1008 int block
= DOC_LAYOUT_BLOCK_BBT
;
1009 int ret
= 0, nbpages
, page
;
1010 u_char
*buf
= docg3
->bbt
;
1012 nbpages
= DIV_ROUND_UP(docg3
->max_block
+ 1, 8 * DOC_LAYOUT_PAGE_SIZE
);
1013 for (page
= 0; !ret
&& (page
< nbpages
); page
++) {
1014 ret
= doc_read_page_prepare(docg3
, block
, block
+ 1,
1015 page
+ DOC_LAYOUT_PAGE_BBT
, 0);
1017 ret
= doc_read_page_ecc_init(docg3
,
1018 DOC_LAYOUT_PAGE_SIZE
);
1020 doc_read_page_getbytes(docg3
, DOC_LAYOUT_PAGE_SIZE
,
1022 buf
+= DOC_LAYOUT_PAGE_SIZE
;
1024 doc_read_page_finish(docg3
);
1029 * doc_block_isbad - Checks whether a block is good or not
1031 * @from: the offset to find the correct block
1033 * Returns 1 if block is bad, 0 if block is good
1035 static int doc_block_isbad(struct mtd_info
*mtd
, loff_t from
)
1037 struct docg3
*docg3
= mtd
->priv
;
1038 int block0
, block1
, page
, ofs
, is_good
;
1040 calc_block_sector(from
, &block0
, &block1
, &page
, &ofs
,
1042 doc_dbg("doc_block_isbad(from=%lld) => block=(%d,%d), page=%d, ofs=%d\n",
1043 from
, block0
, block1
, page
, ofs
);
1045 if (block0
< DOC_LAYOUT_BLOCK_FIRST_DATA
)
1047 if (block1
> docg3
->max_block
)
1050 is_good
= docg3
->bbt
[block0
>> 3] & (1 << (block0
& 0x7));
1056 * doc_get_erase_count - Get block erase count
1057 * @docg3: the device
1058 * @from: the offset in which the block is.
1060 * Get the number of times a block was erased. The number is the maximum of
1061 * erase times between first and second plane (which should be equal normally).
1063 * Returns The number of erases, or -EINVAL or -EIO on error.
1065 static int doc_get_erase_count(struct docg3
*docg3
, loff_t from
)
1067 u8 buf
[DOC_LAYOUT_WEAR_SIZE
];
1068 int ret
, plane1_erase_count
, plane2_erase_count
;
1069 int block0
, block1
, page
, ofs
;
1071 doc_dbg("doc_get_erase_count(from=%lld, buf=%p)\n", from
, buf
);
1072 if (from
% DOC_LAYOUT_PAGE_SIZE
)
1074 calc_block_sector(from
, &block0
, &block1
, &page
, &ofs
, docg3
->reliable
);
1075 if (block1
> docg3
->max_block
)
1078 ret
= doc_reset_seq(docg3
);
1080 ret
= doc_read_page_prepare(docg3
, block0
, block1
, page
,
1081 ofs
+ DOC_LAYOUT_WEAR_OFFSET
, 0);
1083 ret
= doc_read_page_getbytes(docg3
, DOC_LAYOUT_WEAR_SIZE
,
1085 doc_read_page_finish(docg3
);
1087 if (ret
|| (buf
[0] != DOC_ERASE_MARK
) || (buf
[2] != DOC_ERASE_MARK
))
1089 plane1_erase_count
= (u8
)(~buf
[1]) | ((u8
)(~buf
[4]) << 8)
1090 | ((u8
)(~buf
[5]) << 16);
1091 plane2_erase_count
= (u8
)(~buf
[3]) | ((u8
)(~buf
[6]) << 8)
1092 | ((u8
)(~buf
[7]) << 16);
1094 return max(plane1_erase_count
, plane2_erase_count
);
1099 * doc_get_op_status - get erase/write operation status
1100 * @docg3: the device
1102 * Queries the status from the chip, and returns it
1104 * Returns the status (bits DOC_PLANES_STATUS_*)
1106 static int doc_get_op_status(struct docg3
*docg3
)
1110 doc_flash_sequence(docg3
, DOC_SEQ_PLANES_STATUS
);
1111 doc_flash_command(docg3
, DOC_CMD_PLANES_STATUS
);
1112 doc_delay(docg3
, 5);
1114 doc_ecc_disable(docg3
);
1115 doc_read_data_area(docg3
, &status
, 1, 1);
1120 * doc_write_erase_wait_status - wait for write or erase completion
1121 * @docg3: the device
1123 * Wait for the chip to be ready again after erase or write operation, and check
1124 * erase/write status.
1126 * Returns 0 if erase successful, -EIO if erase/write issue, -ETIMEOUT if
1129 static int doc_write_erase_wait_status(struct docg3
*docg3
)
1131 int i
, status
, ret
= 0;
1133 for (i
= 0; !doc_is_ready(docg3
) && i
< 5; i
++)
1135 if (!doc_is_ready(docg3
)) {
1136 doc_dbg("Timeout reached and the chip is still not ready\n");
1141 status
= doc_get_op_status(docg3
);
1142 if (status
& DOC_PLANES_STATUS_FAIL
) {
1143 doc_dbg("Erase/Write failed on (a) plane(s), status = %x\n",
1149 doc_page_finish(docg3
);
1154 * doc_erase_block - Erase a couple of blocks
1155 * @docg3: the device
1156 * @block0: the first block to erase (leftmost plane)
1157 * @block1: the second block to erase (rightmost plane)
1159 * Erase both blocks, and return operation status
1161 * Returns 0 if erase successful, -EIO if erase issue, -ETIMEOUT if chip not
1162 * ready for too long
1164 static int doc_erase_block(struct docg3
*docg3
, int block0
, int block1
)
1168 doc_dbg("doc_erase_block(blocks=(%d,%d))\n", block0
, block1
);
1169 ret
= doc_reset_seq(docg3
);
1173 doc_set_reliable_mode(docg3
);
1174 doc_flash_sequence(docg3
, DOC_SEQ_ERASE
);
1176 sector
= block0
<< DOC_ADDR_BLOCK_SHIFT
;
1177 doc_flash_command(docg3
, DOC_CMD_PROG_BLOCK_ADDR
);
1178 doc_setup_addr_sector(docg3
, sector
);
1179 sector
= block1
<< DOC_ADDR_BLOCK_SHIFT
;
1180 doc_flash_command(docg3
, DOC_CMD_PROG_BLOCK_ADDR
);
1181 doc_setup_addr_sector(docg3
, sector
);
1182 doc_delay(docg3
, 1);
1184 doc_flash_command(docg3
, DOC_CMD_ERASECYCLE2
);
1185 doc_delay(docg3
, 2);
1187 if (is_prot_seq_error(docg3
)) {
1188 doc_err("Erase blocks %d,%d error\n", block0
, block1
);
1192 return doc_write_erase_wait_status(docg3
);
1196 * doc_erase - Erase a portion of the chip
1198 * @info: the erase info
1200 * Erase a bunch of contiguous blocks, by pairs, as a "mtd" page of 1024 is
1201 * split into 2 pages of 512 bytes on 2 contiguous blocks.
1203 * Returns 0 if erase successful, -EINVAL if addressing error, -EIO if erase
1206 static int doc_erase(struct mtd_info
*mtd
, struct erase_info
*info
)
1208 struct docg3
*docg3
= mtd
->priv
;
1210 int block0
, block1
, page
, ret
, ofs
= 0;
1212 doc_dbg("doc_erase(from=%lld, len=%lld\n", info
->addr
, info
->len
);
1214 info
->state
= MTD_ERASE_PENDING
;
1215 calc_block_sector(info
->addr
+ info
->len
, &block0
, &block1
, &page
,
1216 &ofs
, docg3
->reliable
);
1218 if (info
->addr
+ info
->len
> mtd
->size
|| page
|| ofs
)
1222 calc_block_sector(info
->addr
, &block0
, &block1
, &page
, &ofs
,
1224 mutex_lock(&docg3
->cascade
->lock
);
1225 doc_set_device_id(docg3
, docg3
->device_id
);
1226 doc_set_reliable_mode(docg3
);
1227 for (len
= info
->len
; !ret
&& len
> 0; len
-= mtd
->erasesize
) {
1228 info
->state
= MTD_ERASING
;
1229 ret
= doc_erase_block(docg3
, block0
, block1
);
1233 mutex_unlock(&docg3
->cascade
->lock
);
1238 info
->state
= MTD_ERASE_DONE
;
1242 info
->state
= MTD_ERASE_FAILED
;
1247 * doc_write_page - Write a single page to the chip
1248 * @docg3: the device
1249 * @to: the offset from first block and first page, in bytes, aligned on page
1251 * @buf: buffer to get bytes from
1252 * @oob: buffer to get out of band bytes from (can be NULL if no OOB should be
1254 * @autoecc: if 0, all 16 bytes from OOB are taken, regardless of HW Hamming or
1255 * BCH computations. If 1, only bytes 0-7 and byte 15 are taken,
1256 * remaining ones are filled with hardware Hamming and BCH
1257 * computations. Its value is not meaningfull is oob == NULL.
1259 * Write one full page (ie. 1 page split on two planes), of 512 bytes, with the
1260 * OOB data. The OOB ECC is automatically computed by the hardware Hamming and
1261 * BCH generator if autoecc is not null.
1263 * Returns 0 if write successful, -EIO if write error, -EAGAIN if timeout
1265 static int doc_write_page(struct docg3
*docg3
, loff_t to
, const u_char
*buf
,
1266 const u_char
*oob
, int autoecc
)
1268 int block0
, block1
, page
, ret
, ofs
= 0;
1269 u8 hwecc
[DOC_ECC_BCH_SIZE
], hamming
;
1271 doc_dbg("doc_write_page(to=%lld)\n", to
);
1272 calc_block_sector(to
, &block0
, &block1
, &page
, &ofs
, docg3
->reliable
);
1274 doc_set_device_id(docg3
, docg3
->device_id
);
1275 ret
= doc_reset_seq(docg3
);
1279 /* Program the flash address block and page */
1280 ret
= doc_write_seek(docg3
, block0
, block1
, page
, ofs
);
1284 doc_write_page_ecc_init(docg3
, DOC_ECC_BCH_TOTAL_BYTES
);
1285 doc_delay(docg3
, 2);
1286 doc_write_page_putbytes(docg3
, DOC_LAYOUT_PAGE_SIZE
, buf
);
1288 if (oob
&& autoecc
) {
1289 doc_write_page_putbytes(docg3
, DOC_LAYOUT_OOB_PAGEINFO_SZ
, oob
);
1290 doc_delay(docg3
, 2);
1291 oob
+= DOC_LAYOUT_OOB_UNUSED_OFS
;
1293 hamming
= doc_register_readb(docg3
, DOC_HAMMINGPARITY
);
1294 doc_delay(docg3
, 2);
1295 doc_write_page_putbytes(docg3
, DOC_LAYOUT_OOB_HAMMING_SZ
,
1297 doc_delay(docg3
, 2);
1299 doc_get_bch_hw_ecc(docg3
, hwecc
);
1300 doc_write_page_putbytes(docg3
, DOC_LAYOUT_OOB_BCH_SZ
, hwecc
);
1301 doc_delay(docg3
, 2);
1303 doc_write_page_putbytes(docg3
, DOC_LAYOUT_OOB_UNUSED_SZ
, oob
);
1305 if (oob
&& !autoecc
)
1306 doc_write_page_putbytes(docg3
, DOC_LAYOUT_OOB_SIZE
, oob
);
1308 doc_delay(docg3
, 2);
1309 doc_page_finish(docg3
);
1310 doc_delay(docg3
, 2);
1311 doc_flash_command(docg3
, DOC_CMD_PROG_CYCLE2
);
1312 doc_delay(docg3
, 2);
1315 * The wait status will perform another doc_page_finish() call, but that
1316 * seems to please the docg3, so leave it.
1318 ret
= doc_write_erase_wait_status(docg3
);
1321 doc_read_page_finish(docg3
);
1326 * doc_guess_autoecc - Guess autoecc mode from mbd_oob_ops
1327 * @ops: the oob operations
1329 * Returns 0 or 1 if success, -EINVAL if invalid oob mode
1331 static int doc_guess_autoecc(struct mtd_oob_ops
*ops
)
1335 switch (ops
->mode
) {
1336 case MTD_OPS_PLACE_OOB
:
1337 case MTD_OPS_AUTO_OOB
:
1350 * doc_fill_autooob - Fill a 16 bytes OOB from 8 non-ECC bytes
1351 * @dst: the target 16 bytes OOB buffer
1352 * @oobsrc: the source 8 bytes non-ECC OOB buffer
1355 static void doc_fill_autooob(u8
*dst
, u8
*oobsrc
)
1357 memcpy(dst
, oobsrc
, DOC_LAYOUT_OOB_PAGEINFO_SZ
);
1358 dst
[DOC_LAYOUT_OOB_UNUSED_OFS
] = oobsrc
[DOC_LAYOUT_OOB_PAGEINFO_SZ
];
1362 * doc_backup_oob - Backup OOB into docg3 structure
1363 * @docg3: the device
1364 * @to: the page offset in the chip
1365 * @ops: the OOB size and buffer
1367 * As the docg3 should write a page with its OOB in one pass, and some userland
1368 * applications do write_oob() to setup the OOB and then write(), store the OOB
1369 * into a temporary storage. This is very dangerous, as 2 concurrent
1370 * applications could store an OOB, and then write their pages (which will
1371 * result into one having its OOB corrupted).
1373 * The only reliable way would be for userland to call doc_write_oob() with both
1374 * the page data _and_ the OOB area.
1376 * Returns 0 if success, -EINVAL if ops content invalid
1378 static int doc_backup_oob(struct docg3
*docg3
, loff_t to
,
1379 struct mtd_oob_ops
*ops
)
1381 int ooblen
= ops
->ooblen
, autoecc
;
1383 if (ooblen
!= DOC_LAYOUT_OOB_SIZE
)
1385 autoecc
= doc_guess_autoecc(ops
);
1389 docg3
->oob_write_ofs
= to
;
1390 docg3
->oob_autoecc
= autoecc
;
1391 if (ops
->mode
== MTD_OPS_AUTO_OOB
) {
1392 doc_fill_autooob(docg3
->oob_write_buf
, ops
->oobbuf
);
1395 memcpy(docg3
->oob_write_buf
, ops
->oobbuf
, DOC_LAYOUT_OOB_SIZE
);
1396 ops
->oobretlen
= DOC_LAYOUT_OOB_SIZE
;
1402 * doc_write_oob - Write out of band bytes to flash
1404 * @ofs: the offset from first block and first page, in bytes, aligned on page
1406 * @ops: the mtd oob structure
1408 * Either write OOB data into a temporary buffer, for the subsequent write
1409 * page. The provided OOB should be 16 bytes long. If a data buffer is provided
1410 * as well, issue the page write.
1411 * Or provide data without OOB, and then a all zeroed OOB will be used (ECC will
1412 * still be filled in if asked for).
1414 * Returns 0 is successful, EINVAL if length is not 14 bytes
1416 static int doc_write_oob(struct mtd_info
*mtd
, loff_t ofs
,
1417 struct mtd_oob_ops
*ops
)
1419 struct docg3
*docg3
= mtd
->priv
;
1420 int ret
, autoecc
, oobdelta
;
1421 u8
*oobbuf
= ops
->oobbuf
;
1422 u8
*buf
= ops
->datbuf
;
1424 u8 oob
[DOC_LAYOUT_OOB_SIZE
];
1431 ooblen
= ops
->ooblen
;
1435 if (oobbuf
&& ops
->mode
== MTD_OPS_PLACE_OOB
)
1436 oobbuf
+= ops
->ooboffs
;
1438 doc_dbg("doc_write_oob(from=%lld, mode=%d, data=(%p:%zu), oob=(%p:%zu))\n",
1439 ofs
, ops
->mode
, buf
, len
, oobbuf
, ooblen
);
1440 switch (ops
->mode
) {
1441 case MTD_OPS_PLACE_OOB
:
1443 oobdelta
= mtd
->oobsize
;
1445 case MTD_OPS_AUTO_OOB
:
1446 oobdelta
= mtd
->ecclayout
->oobavail
;
1451 if ((len
% DOC_LAYOUT_PAGE_SIZE
) || (ooblen
% oobdelta
) ||
1452 (ofs
% DOC_LAYOUT_PAGE_SIZE
))
1454 if (len
&& ooblen
&&
1455 (len
/ DOC_LAYOUT_PAGE_SIZE
) != (ooblen
/ oobdelta
))
1457 if (ofs
+ len
> mtd
->size
)
1463 if (len
== 0 && ooblen
== 0)
1465 if (len
== 0 && ooblen
> 0)
1466 return doc_backup_oob(docg3
, ofs
, ops
);
1468 autoecc
= doc_guess_autoecc(ops
);
1472 mutex_lock(&docg3
->cascade
->lock
);
1473 while (!ret
&& len
> 0) {
1474 memset(oob
, 0, sizeof(oob
));
1475 if (ofs
== docg3
->oob_write_ofs
)
1476 memcpy(oob
, docg3
->oob_write_buf
, DOC_LAYOUT_OOB_SIZE
);
1477 else if (ooblen
> 0 && ops
->mode
== MTD_OPS_AUTO_OOB
)
1478 doc_fill_autooob(oob
, oobbuf
);
1479 else if (ooblen
> 0)
1480 memcpy(oob
, oobbuf
, DOC_LAYOUT_OOB_SIZE
);
1481 ret
= doc_write_page(docg3
, ofs
, buf
, oob
, autoecc
);
1483 ofs
+= DOC_LAYOUT_PAGE_SIZE
;
1484 len
-= DOC_LAYOUT_PAGE_SIZE
;
1485 buf
+= DOC_LAYOUT_PAGE_SIZE
;
1489 ops
->oobretlen
+= oobdelta
;
1491 ops
->retlen
+= DOC_LAYOUT_PAGE_SIZE
;
1494 doc_set_device_id(docg3
, 0);
1495 mutex_unlock(&docg3
->cascade
->lock
);
1500 * doc_write - Write a buffer to the chip
1502 * @to: the offset from first block and first page, in bytes, aligned on page
1504 * @len: the number of bytes to write (must be a full page size, ie. 512)
1505 * @retlen: the number of bytes actually written (0 or 512)
1506 * @buf: the buffer to get bytes from
1508 * Writes data to the chip.
1510 * Returns 0 if write successful, -EIO if write error
1512 static int doc_write(struct mtd_info
*mtd
, loff_t to
, size_t len
,
1513 size_t *retlen
, const u_char
*buf
)
1515 struct docg3
*docg3
= mtd
->priv
;
1517 struct mtd_oob_ops ops
;
1519 doc_dbg("doc_write(to=%lld, len=%zu)\n", to
, len
);
1520 ops
.datbuf
= (char *)buf
;
1522 ops
.mode
= MTD_OPS_PLACE_OOB
;
1527 ret
= doc_write_oob(mtd
, to
, &ops
);
1528 *retlen
= ops
.retlen
;
1532 static struct docg3
*sysfs_dev2docg3(struct device
*dev
,
1533 struct device_attribute
*attr
)
1536 struct platform_device
*pdev
= to_platform_device(dev
);
1537 struct mtd_info
**docg3_floors
= platform_get_drvdata(pdev
);
1539 floor
= attr
->attr
.name
[1] - '0';
1540 if (floor
< 0 || floor
>= DOC_MAX_NBFLOORS
)
1543 return docg3_floors
[floor
]->priv
;
1546 static ssize_t
dps0_is_key_locked(struct device
*dev
,
1547 struct device_attribute
*attr
, char *buf
)
1549 struct docg3
*docg3
= sysfs_dev2docg3(dev
, attr
);
1552 mutex_lock(&docg3
->cascade
->lock
);
1553 doc_set_device_id(docg3
, docg3
->device_id
);
1554 dps0
= doc_register_readb(docg3
, DOC_DPS0_STATUS
);
1555 doc_set_device_id(docg3
, 0);
1556 mutex_unlock(&docg3
->cascade
->lock
);
1558 return sprintf(buf
, "%d\n", !(dps0
& DOC_DPS_KEY_OK
));
1561 static ssize_t
dps1_is_key_locked(struct device
*dev
,
1562 struct device_attribute
*attr
, char *buf
)
1564 struct docg3
*docg3
= sysfs_dev2docg3(dev
, attr
);
1567 mutex_lock(&docg3
->cascade
->lock
);
1568 doc_set_device_id(docg3
, docg3
->device_id
);
1569 dps1
= doc_register_readb(docg3
, DOC_DPS1_STATUS
);
1570 doc_set_device_id(docg3
, 0);
1571 mutex_unlock(&docg3
->cascade
->lock
);
1573 return sprintf(buf
, "%d\n", !(dps1
& DOC_DPS_KEY_OK
));
1576 static ssize_t
dps0_insert_key(struct device
*dev
,
1577 struct device_attribute
*attr
,
1578 const char *buf
, size_t count
)
1580 struct docg3
*docg3
= sysfs_dev2docg3(dev
, attr
);
1583 if (count
!= DOC_LAYOUT_DPS_KEY_LENGTH
)
1586 mutex_lock(&docg3
->cascade
->lock
);
1587 doc_set_device_id(docg3
, docg3
->device_id
);
1588 for (i
= 0; i
< DOC_LAYOUT_DPS_KEY_LENGTH
; i
++)
1589 doc_writeb(docg3
, buf
[i
], DOC_DPS0_KEY
);
1590 doc_set_device_id(docg3
, 0);
1591 mutex_unlock(&docg3
->cascade
->lock
);
1595 static ssize_t
dps1_insert_key(struct device
*dev
,
1596 struct device_attribute
*attr
,
1597 const char *buf
, size_t count
)
1599 struct docg3
*docg3
= sysfs_dev2docg3(dev
, attr
);
1602 if (count
!= DOC_LAYOUT_DPS_KEY_LENGTH
)
1605 mutex_lock(&docg3
->cascade
->lock
);
1606 doc_set_device_id(docg3
, docg3
->device_id
);
1607 for (i
= 0; i
< DOC_LAYOUT_DPS_KEY_LENGTH
; i
++)
1608 doc_writeb(docg3
, buf
[i
], DOC_DPS1_KEY
);
1609 doc_set_device_id(docg3
, 0);
1610 mutex_unlock(&docg3
->cascade
->lock
);
1614 #define FLOOR_SYSFS(id) { \
1615 __ATTR(f##id##_dps0_is_keylocked, S_IRUGO, dps0_is_key_locked, NULL), \
1616 __ATTR(f##id##_dps1_is_keylocked, S_IRUGO, dps1_is_key_locked, NULL), \
1617 __ATTR(f##id##_dps0_protection_key, S_IWUGO, NULL, dps0_insert_key), \
1618 __ATTR(f##id##_dps1_protection_key, S_IWUGO, NULL, dps1_insert_key), \
1621 static struct device_attribute doc_sys_attrs
[DOC_MAX_NBFLOORS
][4] = {
1622 FLOOR_SYSFS(0), FLOOR_SYSFS(1), FLOOR_SYSFS(2), FLOOR_SYSFS(3)
1625 static int doc_register_sysfs(struct platform_device
*pdev
,
1626 struct docg3_cascade
*cascade
)
1628 int ret
= 0, floor
, i
= 0;
1629 struct device
*dev
= &pdev
->dev
;
1631 for (floor
= 0; !ret
&& floor
< DOC_MAX_NBFLOORS
&&
1632 cascade
->floors
[floor
]; floor
++)
1633 for (i
= 0; !ret
&& i
< 4; i
++)
1634 ret
= device_create_file(dev
, &doc_sys_attrs
[floor
][i
]);
1639 device_remove_file(dev
, &doc_sys_attrs
[floor
][i
]);
1641 } while (--floor
>= 0);
1645 static void doc_unregister_sysfs(struct platform_device
*pdev
,
1646 struct docg3_cascade
*cascade
)
1648 struct device
*dev
= &pdev
->dev
;
1651 for (floor
= 0; floor
< DOC_MAX_NBFLOORS
&& cascade
->floors
[floor
];
1653 for (i
= 0; i
< 4; i
++)
1654 device_remove_file(dev
, &doc_sys_attrs
[floor
][i
]);
1658 * Debug sysfs entries
1660 static int dbg_flashctrl_show(struct seq_file
*s
, void *p
)
1662 struct docg3
*docg3
= (struct docg3
*)s
->private;
1667 mutex_lock(&docg3
->cascade
->lock
);
1668 fctrl
= doc_register_readb(docg3
, DOC_FLASHCONTROL
);
1669 mutex_unlock(&docg3
->cascade
->lock
);
1671 pos
+= seq_printf(s
,
1672 "FlashControl : 0x%02x (%s,CE# %s,%s,%s,flash %s)\n",
1674 fctrl
& DOC_CTRL_VIOLATION
? "protocol violation" : "-",
1675 fctrl
& DOC_CTRL_CE
? "active" : "inactive",
1676 fctrl
& DOC_CTRL_PROTECTION_ERROR
? "protection error" : "-",
1677 fctrl
& DOC_CTRL_SEQUENCE_ERROR
? "sequence error" : "-",
1678 fctrl
& DOC_CTRL_FLASHREADY
? "ready" : "not ready");
1681 DEBUGFS_RO_ATTR(flashcontrol
, dbg_flashctrl_show
);
1683 static int dbg_asicmode_show(struct seq_file
*s
, void *p
)
1685 struct docg3
*docg3
= (struct docg3
*)s
->private;
1687 int pos
= 0, pctrl
, mode
;
1689 mutex_lock(&docg3
->cascade
->lock
);
1690 pctrl
= doc_register_readb(docg3
, DOC_ASICMODE
);
1691 mode
= pctrl
& 0x03;
1692 mutex_unlock(&docg3
->cascade
->lock
);
1694 pos
+= seq_printf(s
,
1695 "%04x : RAM_WE=%d,RSTIN_RESET=%d,BDETCT_RESET=%d,WRITE_ENABLE=%d,POWERDOWN=%d,MODE=%d%d (",
1697 pctrl
& DOC_ASICMODE_RAM_WE
? 1 : 0,
1698 pctrl
& DOC_ASICMODE_RSTIN_RESET
? 1 : 0,
1699 pctrl
& DOC_ASICMODE_BDETCT_RESET
? 1 : 0,
1700 pctrl
& DOC_ASICMODE_MDWREN
? 1 : 0,
1701 pctrl
& DOC_ASICMODE_POWERDOWN
? 1 : 0,
1702 mode
>> 1, mode
& 0x1);
1705 case DOC_ASICMODE_RESET
:
1706 pos
+= seq_printf(s
, "reset");
1708 case DOC_ASICMODE_NORMAL
:
1709 pos
+= seq_printf(s
, "normal");
1711 case DOC_ASICMODE_POWERDOWN
:
1712 pos
+= seq_printf(s
, "powerdown");
1715 pos
+= seq_printf(s
, ")\n");
1718 DEBUGFS_RO_ATTR(asic_mode
, dbg_asicmode_show
);
1720 static int dbg_device_id_show(struct seq_file
*s
, void *p
)
1722 struct docg3
*docg3
= (struct docg3
*)s
->private;
1726 mutex_lock(&docg3
->cascade
->lock
);
1727 id
= doc_register_readb(docg3
, DOC_DEVICESELECT
);
1728 mutex_unlock(&docg3
->cascade
->lock
);
1730 pos
+= seq_printf(s
, "DeviceId = %d\n", id
);
1733 DEBUGFS_RO_ATTR(device_id
, dbg_device_id_show
);
1735 static int dbg_protection_show(struct seq_file
*s
, void *p
)
1737 struct docg3
*docg3
= (struct docg3
*)s
->private;
1739 int protect
, dps0
, dps0_low
, dps0_high
, dps1
, dps1_low
, dps1_high
;
1741 mutex_lock(&docg3
->cascade
->lock
);
1742 protect
= doc_register_readb(docg3
, DOC_PROTECTION
);
1743 dps0
= doc_register_readb(docg3
, DOC_DPS0_STATUS
);
1744 dps0_low
= doc_register_readw(docg3
, DOC_DPS0_ADDRLOW
);
1745 dps0_high
= doc_register_readw(docg3
, DOC_DPS0_ADDRHIGH
);
1746 dps1
= doc_register_readb(docg3
, DOC_DPS1_STATUS
);
1747 dps1_low
= doc_register_readw(docg3
, DOC_DPS1_ADDRLOW
);
1748 dps1_high
= doc_register_readw(docg3
, DOC_DPS1_ADDRHIGH
);
1749 mutex_unlock(&docg3
->cascade
->lock
);
1751 pos
+= seq_printf(s
, "Protection = 0x%02x (",
1753 if (protect
& DOC_PROTECT_FOUNDRY_OTP_LOCK
)
1754 pos
+= seq_printf(s
, "FOUNDRY_OTP_LOCK,");
1755 if (protect
& DOC_PROTECT_CUSTOMER_OTP_LOCK
)
1756 pos
+= seq_printf(s
, "CUSTOMER_OTP_LOCK,");
1757 if (protect
& DOC_PROTECT_LOCK_INPUT
)
1758 pos
+= seq_printf(s
, "LOCK_INPUT,");
1759 if (protect
& DOC_PROTECT_STICKY_LOCK
)
1760 pos
+= seq_printf(s
, "STICKY_LOCK,");
1761 if (protect
& DOC_PROTECT_PROTECTION_ENABLED
)
1762 pos
+= seq_printf(s
, "PROTECTION ON,");
1763 if (protect
& DOC_PROTECT_IPL_DOWNLOAD_LOCK
)
1764 pos
+= seq_printf(s
, "IPL_DOWNLOAD_LOCK,");
1765 if (protect
& DOC_PROTECT_PROTECTION_ERROR
)
1766 pos
+= seq_printf(s
, "PROTECT_ERR,");
1768 pos
+= seq_printf(s
, "NO_PROTECT_ERR");
1769 pos
+= seq_printf(s
, ")\n");
1771 pos
+= seq_printf(s
, "DPS0 = 0x%02x : "
1772 "Protected area [0x%x - 0x%x] : OTP=%d, READ=%d, "
1773 "WRITE=%d, HW_LOCK=%d, KEY_OK=%d\n",
1774 dps0
, dps0_low
, dps0_high
,
1775 !!(dps0
& DOC_DPS_OTP_PROTECTED
),
1776 !!(dps0
& DOC_DPS_READ_PROTECTED
),
1777 !!(dps0
& DOC_DPS_WRITE_PROTECTED
),
1778 !!(dps0
& DOC_DPS_HW_LOCK_ENABLED
),
1779 !!(dps0
& DOC_DPS_KEY_OK
));
1780 pos
+= seq_printf(s
, "DPS1 = 0x%02x : "
1781 "Protected area [0x%x - 0x%x] : OTP=%d, READ=%d, "
1782 "WRITE=%d, HW_LOCK=%d, KEY_OK=%d\n",
1783 dps1
, dps1_low
, dps1_high
,
1784 !!(dps1
& DOC_DPS_OTP_PROTECTED
),
1785 !!(dps1
& DOC_DPS_READ_PROTECTED
),
1786 !!(dps1
& DOC_DPS_WRITE_PROTECTED
),
1787 !!(dps1
& DOC_DPS_HW_LOCK_ENABLED
),
1788 !!(dps1
& DOC_DPS_KEY_OK
));
1791 DEBUGFS_RO_ATTR(protection
, dbg_protection_show
);
1793 static int __init
doc_dbg_register(struct docg3
*docg3
)
1795 struct dentry
*root
, *entry
;
1797 root
= debugfs_create_dir("docg3", NULL
);
1801 entry
= debugfs_create_file("flashcontrol", S_IRUSR
, root
, docg3
,
1802 &flashcontrol_fops
);
1804 entry
= debugfs_create_file("asic_mode", S_IRUSR
, root
,
1805 docg3
, &asic_mode_fops
);
1807 entry
= debugfs_create_file("device_id", S_IRUSR
, root
,
1808 docg3
, &device_id_fops
);
1810 entry
= debugfs_create_file("protection", S_IRUSR
, root
,
1811 docg3
, &protection_fops
);
1813 docg3
->debugfs_root
= root
;
1816 debugfs_remove_recursive(root
);
1821 static void __exit
doc_dbg_unregister(struct docg3
*docg3
)
1823 debugfs_remove_recursive(docg3
->debugfs_root
);
1827 * doc_set_driver_info - Fill the mtd_info structure and docg3 structure
1828 * @chip_id: The chip ID of the supported chip
1829 * @mtd: The structure to fill
1831 static void __init
doc_set_driver_info(int chip_id
, struct mtd_info
*mtd
)
1833 struct docg3
*docg3
= mtd
->priv
;
1836 cfg
= doc_register_readb(docg3
, DOC_CONFIGURATION
);
1837 docg3
->if_cfg
= (cfg
& DOC_CONF_IF_CFG
? 1 : 0);
1838 docg3
->reliable
= reliable_mode
;
1842 mtd
->name
= kasprintf(GFP_KERNEL
, "docg3.%d",
1844 docg3
->max_block
= 2047;
1847 mtd
->type
= MTD_NANDFLASH
;
1848 mtd
->flags
= MTD_CAP_NANDFLASH
;
1849 mtd
->size
= (docg3
->max_block
+ 1) * DOC_LAYOUT_BLOCK_SIZE
;
1850 if (docg3
->reliable
== 2)
1852 mtd
->erasesize
= DOC_LAYOUT_BLOCK_SIZE
* DOC_LAYOUT_NBPLANES
;
1853 if (docg3
->reliable
== 2)
1854 mtd
->erasesize
/= 2;
1855 mtd
->writebufsize
= mtd
->writesize
= DOC_LAYOUT_PAGE_SIZE
;
1856 mtd
->oobsize
= DOC_LAYOUT_OOB_SIZE
;
1857 mtd
->owner
= THIS_MODULE
;
1858 mtd
->_erase
= doc_erase
;
1859 mtd
->_read
= doc_read
;
1860 mtd
->_write
= doc_write
;
1861 mtd
->_read_oob
= doc_read_oob
;
1862 mtd
->_write_oob
= doc_write_oob
;
1863 mtd
->_block_isbad
= doc_block_isbad
;
1864 mtd
->ecclayout
= &docg3_oobinfo
;
1865 mtd
->ecc_strength
= DOC_ECC_BCH_T
;
1869 * doc_probe_device - Check if a device is available
1870 * @base: the io space where the device is probed
1871 * @floor: the floor of the probed device
1873 * @cascade: the cascade of chips this devices will belong to
1875 * Checks whether a device at the specified IO range, and floor is available.
1877 * Returns a mtd_info struct if there is a device, ENODEV if none found, ENOMEM
1878 * if a memory allocation failed. If floor 0 is checked, a reset of the ASIC is
1881 static struct mtd_info
* __init
1882 doc_probe_device(struct docg3_cascade
*cascade
, int floor
, struct device
*dev
)
1884 int ret
, bbt_nbpages
;
1885 u16 chip_id
, chip_id_inv
;
1886 struct docg3
*docg3
;
1887 struct mtd_info
*mtd
;
1890 docg3
= kzalloc(sizeof(struct docg3
), GFP_KERNEL
);
1893 mtd
= kzalloc(sizeof(struct mtd_info
), GFP_KERNEL
);
1897 bbt_nbpages
= DIV_ROUND_UP(docg3
->max_block
+ 1,
1898 8 * DOC_LAYOUT_PAGE_SIZE
);
1899 docg3
->bbt
= kzalloc(bbt_nbpages
* DOC_LAYOUT_PAGE_SIZE
, GFP_KERNEL
);
1904 docg3
->device_id
= floor
;
1905 docg3
->cascade
= cascade
;
1906 doc_set_device_id(docg3
, docg3
->device_id
);
1908 doc_set_asic_mode(docg3
, DOC_ASICMODE_RESET
);
1909 doc_set_asic_mode(docg3
, DOC_ASICMODE_NORMAL
);
1911 chip_id
= doc_register_readw(docg3
, DOC_CHIPID
);
1912 chip_id_inv
= doc_register_readw(docg3
, DOC_CHIPID_INV
);
1915 if (chip_id
!= (u16
)(~chip_id_inv
)) {
1921 doc_info("Found a G3 DiskOnChip at addr %p, floor %d\n",
1922 docg3
->cascade
->base
, floor
);
1925 doc_err("Chip id %04x is not a DiskOnChip G3 chip\n", chip_id
);
1929 doc_set_driver_info(chip_id
, mtd
);
1931 doc_hamming_ecc_init(docg3
, DOC_LAYOUT_OOB_PAGEINFO_SZ
);
1932 doc_reload_bbt(docg3
);
1940 return ERR_PTR(ret
);
1944 * doc_release_device - Release a docg3 floor
1947 static void doc_release_device(struct mtd_info
*mtd
)
1949 struct docg3
*docg3
= mtd
->priv
;
1951 mtd_device_unregister(mtd
);
1959 * docg3_resume - Awakens docg3 floor
1960 * @pdev: platfrom device
1962 * Returns 0 (always successful)
1964 static int docg3_resume(struct platform_device
*pdev
)
1967 struct docg3_cascade
*cascade
;
1968 struct mtd_info
**docg3_floors
, *mtd
;
1969 struct docg3
*docg3
;
1971 cascade
= platform_get_drvdata(pdev
);
1972 docg3_floors
= cascade
->floors
;
1973 mtd
= docg3_floors
[0];
1976 doc_dbg("docg3_resume()\n");
1977 for (i
= 0; i
< 12; i
++)
1978 doc_readb(docg3
, DOC_IOSPACE_IPL
);
1983 * docg3_suspend - Put in low power mode the docg3 floor
1984 * @pdev: platform device
1985 * @state: power state
1987 * Shuts off most of docg3 circuitery to lower power consumption.
1989 * Returns 0 if suspend succeeded, -EIO if chip refused suspend
1991 static int docg3_suspend(struct platform_device
*pdev
, pm_message_t state
)
1994 struct docg3_cascade
*cascade
;
1995 struct mtd_info
**docg3_floors
, *mtd
;
1996 struct docg3
*docg3
;
1999 cascade
= platform_get_drvdata(pdev
);
2000 docg3_floors
= cascade
->floors
;
2001 for (floor
= 0; floor
< DOC_MAX_NBFLOORS
; floor
++) {
2002 mtd
= docg3_floors
[floor
];
2007 doc_writeb(docg3
, floor
, DOC_DEVICESELECT
);
2008 ctrl
= doc_register_readb(docg3
, DOC_FLASHCONTROL
);
2009 ctrl
&= ~DOC_CTRL_VIOLATION
& ~DOC_CTRL_CE
;
2010 doc_writeb(docg3
, ctrl
, DOC_FLASHCONTROL
);
2012 for (i
= 0; i
< 10; i
++) {
2013 usleep_range(3000, 4000);
2014 pwr_down
= doc_register_readb(docg3
, DOC_POWERMODE
);
2015 if (pwr_down
& DOC_POWERDOWN_READY
)
2018 if (pwr_down
& DOC_POWERDOWN_READY
) {
2019 doc_dbg("docg3_suspend(): floor %d powerdown ok\n",
2022 doc_err("docg3_suspend(): floor %d powerdown failed\n",
2028 mtd
= docg3_floors
[0];
2030 doc_set_asic_mode(docg3
, DOC_ASICMODE_POWERDOWN
);
2035 * doc_probe - Probe the IO space for a DiskOnChip G3 chip
2036 * @pdev: platform device
2038 * Probes for a G3 chip at the specified IO space in the platform data
2039 * ressources. The floor 0 must be available.
2041 * Returns 0 on success, -ENOMEM, -ENXIO on error
2043 static int __init
docg3_probe(struct platform_device
*pdev
)
2045 struct device
*dev
= &pdev
->dev
;
2046 struct mtd_info
*mtd
;
2047 struct resource
*ress
;
2049 int ret
, floor
, found
= 0;
2050 struct docg3_cascade
*cascade
;
2053 ress
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
2055 dev_err(dev
, "No I/O memory resource defined\n");
2058 base
= ioremap(ress
->start
, DOC_IOSPACE_SIZE
);
2061 cascade
= kzalloc(sizeof(*cascade
) * DOC_MAX_NBFLOORS
,
2065 cascade
->base
= base
;
2066 mutex_init(&cascade
->lock
);
2067 cascade
->bch
= init_bch(DOC_ECC_BCH_M
, DOC_ECC_BCH_T
,
2068 DOC_ECC_BCH_PRIMPOLY
);
2072 for (floor
= 0; floor
< DOC_MAX_NBFLOORS
; floor
++) {
2073 mtd
= doc_probe_device(cascade
, floor
, dev
);
2084 cascade
->floors
[floor
] = mtd
;
2085 ret
= mtd_device_parse_register(mtd
, part_probes
, NULL
, NULL
,
2092 ret
= doc_register_sysfs(pdev
, cascade
);
2098 platform_set_drvdata(pdev
, cascade
);
2099 doc_dbg_register(cascade
->floors
[0]->priv
);
2104 dev_info(dev
, "No supported DiskOnChip found\n");
2106 kfree(cascade
->bch
);
2107 for (floor
= 0; floor
< DOC_MAX_NBFLOORS
; floor
++)
2108 if (cascade
->floors
[floor
])
2109 doc_release_device(cascade
->floors
[floor
]);
2119 * docg3_release - Release the driver
2120 * @pdev: the platform device
2124 static int __exit
docg3_release(struct platform_device
*pdev
)
2126 struct docg3_cascade
*cascade
= platform_get_drvdata(pdev
);
2127 struct docg3
*docg3
= cascade
->floors
[0]->priv
;
2128 void __iomem
*base
= cascade
->base
;
2131 doc_unregister_sysfs(pdev
, cascade
);
2132 doc_dbg_unregister(docg3
);
2133 for (floor
= 0; floor
< DOC_MAX_NBFLOORS
; floor
++)
2134 if (cascade
->floors
[floor
])
2135 doc_release_device(cascade
->floors
[floor
]);
2137 free_bch(docg3
->cascade
->bch
);
2143 static struct platform_driver g3_driver
= {
2146 .owner
= THIS_MODULE
,
2148 .suspend
= docg3_suspend
,
2149 .resume
= docg3_resume
,
2150 .remove
= __exit_p(docg3_release
),
2153 static int __init
docg3_init(void)
2155 return platform_driver_probe(&g3_driver
, docg3_probe
);
2157 module_init(docg3_init
);
2160 static void __exit
docg3_exit(void)
2162 platform_driver_unregister(&g3_driver
);
2164 module_exit(docg3_exit
);
2166 MODULE_LICENSE("GPL");
2167 MODULE_AUTHOR("Robert Jarzmik <robert.jarzmik@free.fr>");
2168 MODULE_DESCRIPTION("MTD driver for DiskOnChip G3");