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>
33 #include <linux/debugfs.h>
34 #include <linux/seq_file.h>
36 #define CREATE_TRACE_POINTS
40 * This driver handles the DiskOnChip G3 flash memory.
42 * As no specification is available from M-Systems/Sandisk, this drivers lacks
43 * several functions available on the chip, as :
47 * - ECC fixing (lack of BCH algorith understanding)
48 * - powerdown / powerup
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
56 * The BCH part is only used for check purpose, no correction is available as
57 * some information is missing. What is known is that :
58 * - BCH is in GF(2^14)
59 * - BCH is over data of 520 bytes (512 page + 7 page_info bytes
61 * - BCH can correct up to 4 bits (t = 4)
62 * - BCH syndroms are calculated in hardware, and checked in hardware as well
67 * struct docg3_oobinfo - DiskOnChip G3 OOB layout
68 * @eccbytes: 8 bytes are used (1 for Hamming ECC, 7 for BCH ECC)
69 * @eccpos: ecc positions (byte 7 is Hamming ECC, byte 8-14 are BCH ECC)
70 * @oobfree: free pageinfo bytes (byte 0 until byte 6, byte 15
71 * @oobavail: 8 available bytes remaining after ECC toll
73 static struct nand_ecclayout docg3_oobinfo
= {
75 .eccpos
= {7, 8, 9, 10, 11, 12, 13, 14},
76 .oobfree
= {{0, 7}, {15, 1} },
80 static inline u8
doc_readb(struct docg3
*docg3
, u16 reg
)
82 u8 val
= readb(docg3
->base
+ reg
);
84 trace_docg3_io(0, 8, reg
, (int)val
);
88 static inline u16
doc_readw(struct docg3
*docg3
, u16 reg
)
90 u16 val
= readw(docg3
->base
+ reg
);
92 trace_docg3_io(0, 16, reg
, (int)val
);
96 static inline void doc_writeb(struct docg3
*docg3
, u8 val
, u16 reg
)
98 writeb(val
, docg3
->base
+ reg
);
99 trace_docg3_io(1, 8, reg
, val
);
102 static inline void doc_writew(struct docg3
*docg3
, u16 val
, u16 reg
)
104 writew(val
, docg3
->base
+ reg
);
105 trace_docg3_io(1, 16, reg
, val
);
108 static inline void doc_flash_command(struct docg3
*docg3
, u8 cmd
)
110 doc_writeb(docg3
, cmd
, DOC_FLASHCOMMAND
);
113 static inline void doc_flash_sequence(struct docg3
*docg3
, u8 seq
)
115 doc_writeb(docg3
, seq
, DOC_FLASHSEQUENCE
);
118 static inline void doc_flash_address(struct docg3
*docg3
, u8 addr
)
120 doc_writeb(docg3
, addr
, DOC_FLASHADDRESS
);
123 static char const *part_probes
[] = { "cmdlinepart", "saftlpart", NULL
};
125 static int doc_register_readb(struct docg3
*docg3
, int reg
)
129 doc_writew(docg3
, reg
, DOC_READADDRESS
);
130 val
= doc_readb(docg3
, reg
);
131 doc_vdbg("Read register %04x : %02x\n", reg
, val
);
135 static int doc_register_readw(struct docg3
*docg3
, int reg
)
139 doc_writew(docg3
, reg
, DOC_READADDRESS
);
140 val
= doc_readw(docg3
, reg
);
141 doc_vdbg("Read register %04x : %04x\n", reg
, val
);
146 * doc_delay - delay docg3 operations
148 * @nbNOPs: the number of NOPs to issue
150 * As no specification is available, the right timings between chip commands are
151 * unknown. The only available piece of information are the observed nops on a
152 * working docg3 chip.
153 * Therefore, doc_delay relies on a busy loop of NOPs, instead of scheduler
154 * friendlier msleep() functions or blocking mdelay().
156 static void doc_delay(struct docg3
*docg3
, int nbNOPs
)
160 doc_vdbg("NOP x %d\n", nbNOPs
);
161 for (i
= 0; i
< nbNOPs
; i
++)
162 doc_writeb(docg3
, 0, DOC_NOP
);
165 static int is_prot_seq_error(struct docg3
*docg3
)
169 ctrl
= doc_register_readb(docg3
, DOC_FLASHCONTROL
);
170 return ctrl
& (DOC_CTRL_PROTECTION_ERROR
| DOC_CTRL_SEQUENCE_ERROR
);
173 static int doc_is_ready(struct docg3
*docg3
)
177 ctrl
= doc_register_readb(docg3
, DOC_FLASHCONTROL
);
178 return ctrl
& DOC_CTRL_FLASHREADY
;
181 static int doc_wait_ready(struct docg3
*docg3
)
183 int maxWaitCycles
= 100;
188 } while (!doc_is_ready(docg3
) && maxWaitCycles
--);
190 if (maxWaitCycles
> 0)
196 static int doc_reset_seq(struct docg3
*docg3
)
200 doc_writeb(docg3
, 0x10, DOC_FLASHCONTROL
);
201 doc_flash_sequence(docg3
, DOC_SEQ_RESET
);
202 doc_flash_command(docg3
, DOC_CMD_RESET
);
204 ret
= doc_wait_ready(docg3
);
206 doc_dbg("doc_reset_seq() -> isReady=%s\n", ret
? "false" : "true");
211 * doc_read_data_area - Read data from data area
213 * @buf: the buffer to fill in (might be NULL is dummy reads)
214 * @len: the length to read
215 * @first: first time read, DOC_READADDRESS should be set
217 * Reads bytes from flash data. Handles the single byte / even bytes reads.
219 static void doc_read_data_area(struct docg3
*docg3
, void *buf
, int len
,
226 doc_dbg("doc_read_data_area(buf=%p, len=%d)\n", buf
, len
);
231 doc_writew(docg3
, DOC_IOSPACE_DATA
, DOC_READADDRESS
);
233 for (i
= 0; i
< len4
; i
+= 2) {
234 data16
= doc_readw(docg3
, DOC_IOSPACE_DATA
);
242 doc_writew(docg3
, DOC_IOSPACE_DATA
| DOC_READADDR_ONE_BYTE
,
246 for (i
= 0; i
< cdr
; i
++) {
247 data8
= doc_readb(docg3
, DOC_IOSPACE_DATA
);
257 * doc_set_data_mode - Sets the flash to reliable data mode
260 * The reliable data mode is a bit slower than the fast mode, but less errors
261 * occur. Entering the reliable mode cannot be done without entering the fast
264 static void doc_set_reliable_mode(struct docg3
*docg3
)
266 doc_dbg("doc_set_reliable_mode()\n");
267 doc_flash_sequence(docg3
, DOC_SEQ_SET_MODE
);
268 doc_flash_command(docg3
, DOC_CMD_FAST_MODE
);
269 doc_flash_command(docg3
, DOC_CMD_RELIABLE_MODE
);
274 * doc_set_asic_mode - Set the ASIC mode
278 * The ASIC can work in 3 modes :
279 * - RESET: all registers are zeroed
280 * - NORMAL: receives and handles commands
281 * - POWERDOWN: minimal poweruse, flash parts shut off
283 static void doc_set_asic_mode(struct docg3
*docg3
, u8 mode
)
287 for (i
= 0; i
< 12; i
++)
288 doc_readb(docg3
, DOC_IOSPACE_IPL
);
290 mode
|= DOC_ASICMODE_MDWREN
;
291 doc_dbg("doc_set_asic_mode(%02x)\n", mode
);
292 doc_writeb(docg3
, mode
, DOC_ASICMODE
);
293 doc_writeb(docg3
, ~mode
, DOC_ASICMODECONFIRM
);
298 * doc_set_device_id - Sets the devices id for cascaded G3 chips
300 * @id: the chip to select (amongst 0, 1, 2, 3)
302 * There can be 4 cascaded G3 chips. This function selects the one which will
303 * should be the active one.
305 static void doc_set_device_id(struct docg3
*docg3
, int id
)
309 doc_dbg("doc_set_device_id(%d)\n", id
);
310 doc_writeb(docg3
, id
, DOC_DEVICESELECT
);
311 ctrl
= doc_register_readb(docg3
, DOC_FLASHCONTROL
);
313 ctrl
&= ~DOC_CTRL_VIOLATION
;
315 doc_writeb(docg3
, ctrl
, DOC_FLASHCONTROL
);
319 * doc_set_extra_page_mode - Change flash page layout
322 * Normally, the flash page is split into the data (512 bytes) and the out of
323 * band data (16 bytes). For each, 4 more bytes can be accessed, where the wear
324 * leveling counters are stored. To access this last area of 4 bytes, a special
325 * mode must be input to the flash ASIC.
327 * Returns 0 if no error occured, -EIO else.
329 static int doc_set_extra_page_mode(struct docg3
*docg3
)
333 doc_dbg("doc_set_extra_page_mode()\n");
334 doc_flash_sequence(docg3
, DOC_SEQ_PAGE_SIZE_532
);
335 doc_flash_command(docg3
, DOC_CMD_PAGE_SIZE_532
);
338 fctrl
= doc_register_readb(docg3
, DOC_FLASHCONTROL
);
339 if (fctrl
& (DOC_CTRL_PROTECTION_ERROR
| DOC_CTRL_SEQUENCE_ERROR
))
346 * doc_seek - Set both flash planes to the specified block, page for reading
348 * @block0: the first plane block index
349 * @block1: the second plane block index
350 * @page: the page index within the block
351 * @wear: if true, read will occur on the 4 extra bytes of the wear area
352 * @ofs: offset in page to read
354 * Programs the flash even and odd planes to the specific block and page.
355 * Alternatively, programs the flash to the wear area of the specified page.
357 static int doc_read_seek(struct docg3
*docg3
, int block0
, int block1
, int page
,
362 doc_dbg("doc_seek(blocks=(%d,%d), page=%d, ofs=%d, wear=%d)\n",
363 block0
, block1
, page
, ofs
, wear
);
365 if (!wear
&& (ofs
< 2 * DOC_LAYOUT_PAGE_SIZE
)) {
366 doc_flash_sequence(docg3
, DOC_SEQ_SET_PLANE1
);
367 doc_flash_command(docg3
, DOC_CMD_READ_PLANE1
);
370 doc_flash_sequence(docg3
, DOC_SEQ_SET_PLANE2
);
371 doc_flash_command(docg3
, DOC_CMD_READ_PLANE2
);
375 doc_set_reliable_mode(docg3
);
377 ret
= doc_set_extra_page_mode(docg3
);
381 sector
= (block0
<< DOC_ADDR_BLOCK_SHIFT
) + (page
& DOC_ADDR_PAGE_MASK
);
382 doc_flash_sequence(docg3
, DOC_SEQ_READ
);
383 doc_flash_command(docg3
, DOC_CMD_PROG_BLOCK_ADDR
);
385 doc_flash_address(docg3
, sector
& 0xff);
386 doc_flash_address(docg3
, (sector
>> 8) & 0xff);
387 doc_flash_address(docg3
, (sector
>> 16) & 0xff);
390 sector
= (block1
<< DOC_ADDR_BLOCK_SHIFT
) + (page
& DOC_ADDR_PAGE_MASK
);
391 doc_flash_command(docg3
, DOC_CMD_PROG_BLOCK_ADDR
);
393 doc_flash_address(docg3
, sector
& 0xff);
394 doc_flash_address(docg3
, (sector
>> 8) & 0xff);
395 doc_flash_address(docg3
, (sector
>> 16) & 0xff);
403 * doc_read_page_ecc_init - Initialize hardware ECC engine
405 * @len: the number of bytes covered by the ECC (BCH covered)
407 * The function does initialize the hardware ECC engine to compute the Hamming
408 * ECC (on 1 byte) and the BCH Syndroms (on 7 bytes).
410 * Return 0 if succeeded, -EIO on error
412 static int doc_read_page_ecc_init(struct docg3
*docg3
, int len
)
414 doc_writew(docg3
, DOC_ECCCONF0_READ_MODE
415 | DOC_ECCCONF0_BCH_ENABLE
| DOC_ECCCONF0_HAMMING_ENABLE
416 | (len
& DOC_ECCCONF0_DATA_BYTES_MASK
),
419 doc_register_readb(docg3
, DOC_FLASHCONTROL
);
420 return doc_wait_ready(docg3
);
424 * doc_read_page_prepare - Prepares reading data from a flash page
426 * @block0: the first plane block index on flash memory
427 * @block1: the second plane block index on flash memory
428 * @page: the page index in the block
429 * @offset: the offset in the page (must be a multiple of 4)
431 * Prepares the page to be read in the flash memory :
432 * - tell ASIC to map the flash pages
433 * - tell ASIC to be in read mode
435 * After a call to this method, a call to doc_read_page_finish is mandatory,
436 * to end the read cycle of the flash.
438 * Read data from a flash page. The length to be read must be between 0 and
439 * (page_size + oob_size + wear_size), ie. 532, and a multiple of 4 (because
440 * the extra bytes reading is not implemented).
442 * As pages are grouped by 2 (in 2 planes), reading from a page must be done
444 * - one read of 512 bytes at offset 0
445 * - one read of 512 bytes at offset 512 + 16
447 * Returns 0 if successful, -EIO if a read error occured.
449 static int doc_read_page_prepare(struct docg3
*docg3
, int block0
, int block1
,
450 int page
, int offset
)
452 int wear_area
= 0, ret
= 0;
454 doc_dbg("doc_read_page_prepare(blocks=(%d,%d), page=%d, ofsInPage=%d)\n",
455 block0
, block1
, page
, offset
);
456 if (offset
>= DOC_LAYOUT_WEAR_OFFSET
)
458 if (!wear_area
&& offset
> (DOC_LAYOUT_PAGE_OOB_SIZE
* 2))
461 doc_set_device_id(docg3
, docg3
->device_id
);
462 ret
= doc_reset_seq(docg3
);
466 /* Program the flash address block and page */
467 ret
= doc_read_seek(docg3
, block0
, block1
, page
, wear_area
, offset
);
471 doc_flash_command(docg3
, DOC_CMD_READ_ALL_PLANES
);
473 doc_wait_ready(docg3
);
475 doc_flash_command(docg3
, DOC_CMD_SET_ADDR_READ
);
477 if (offset
>= DOC_LAYOUT_PAGE_SIZE
* 2)
478 offset
-= 2 * DOC_LAYOUT_PAGE_SIZE
;
479 doc_flash_address(docg3
, offset
>> 2);
481 doc_wait_ready(docg3
);
483 doc_flash_command(docg3
, DOC_CMD_READ_FLASH
);
487 doc_writeb(docg3
, 0, DOC_DATAEND
);
493 * doc_read_page_getbytes - Reads bytes from a prepared page
495 * @len: the number of bytes to be read (must be a multiple of 4)
496 * @buf: the buffer to be filled in
497 * @first: 1 if first time read, DOC_READADDRESS should be set
500 static int doc_read_page_getbytes(struct docg3
*docg3
, int len
, u_char
*buf
,
503 doc_read_data_area(docg3
, buf
, len
, first
);
509 * doc_get_hw_bch_syndroms - Get hardware calculated BCH syndroms
511 * @syns: the array of 7 integers where the syndroms will be stored
513 static void doc_get_hw_bch_syndroms(struct docg3
*docg3
, int *syns
)
517 for (i
= 0; i
< DOC_ECC_BCH_SIZE
; i
++)
518 syns
[i
] = doc_register_readb(docg3
, DOC_BCH_SYNDROM(i
));
522 * doc_read_page_finish - Ends reading of a flash page
525 * As a side effect, resets the chip selector to 0. This ensures that after each
526 * read operation, the floor 0 is selected. Therefore, if the systems halts, the
527 * reboot will boot on floor 0, where the IPL is.
529 static void doc_read_page_finish(struct docg3
*docg3
)
531 doc_writeb(docg3
, 0, DOC_DATAEND
);
533 doc_set_device_id(docg3
, 0);
537 * calc_block_sector - Calculate blocks, pages and ofs.
539 * @from: offset in flash
540 * @block0: first plane block index calculated
541 * @block1: second plane block index calculated
542 * @page: page calculated
543 * @ofs: offset in page
545 static void calc_block_sector(loff_t from
, int *block0
, int *block1
, int *page
,
550 sector
= from
/ DOC_LAYOUT_PAGE_SIZE
;
551 *block0
= sector
/ (DOC_LAYOUT_PAGES_PER_BLOCK
* DOC_LAYOUT_NBPLANES
)
552 * DOC_LAYOUT_NBPLANES
;
553 *block1
= *block0
+ 1;
554 *page
= sector
% (DOC_LAYOUT_PAGES_PER_BLOCK
* DOC_LAYOUT_NBPLANES
);
555 *page
/= DOC_LAYOUT_NBPLANES
;
557 *ofs
= DOC_LAYOUT_PAGE_OOB_SIZE
;
563 * doc_read_oob - Read out of band bytes from flash
565 * @from: the offset from first block and first page, in bytes, aligned on page
567 * @ops: the mtd oob structure
569 * Reads flash memory OOB area of pages.
571 * Returns 0 if read successfull, of -EIO, -EINVAL if an error occured
573 static int doc_read_oob(struct mtd_info
*mtd
, loff_t from
,
574 struct mtd_oob_ops
*ops
)
576 struct docg3
*docg3
= mtd
->priv
;
577 int block0
, block1
, page
, ret
, ofs
= 0;
578 u8
*oobbuf
= ops
->oobbuf
;
579 u8
*buf
= ops
->datbuf
;
580 size_t len
, ooblen
, nbdata
, nboob
;
581 u8 calc_ecc
[DOC_ECC_BCH_SIZE
], eccconf1
;
588 ooblen
= ops
->ooblen
;
592 if (oobbuf
&& ops
->mode
== MTD_OPS_PLACE_OOB
)
593 oobbuf
+= ops
->ooboffs
;
595 doc_dbg("doc_read_oob(from=%lld, mode=%d, data=(%p:%zu), oob=(%p:%zu))\n",
596 from
, ops
->mode
, buf
, len
, oobbuf
, ooblen
);
597 if ((len
% DOC_LAYOUT_PAGE_SIZE
) || (ooblen
% DOC_LAYOUT_OOB_SIZE
) ||
598 (from
% DOC_LAYOUT_PAGE_SIZE
))
602 calc_block_sector(from
+ len
, &block0
, &block1
, &page
, &ofs
);
603 if (block1
> docg3
->max_block
)
609 while (!ret
&& (len
> 0 || ooblen
> 0)) {
610 calc_block_sector(from
, &block0
, &block1
, &page
, &ofs
);
611 nbdata
= min_t(size_t, len
, (size_t)DOC_LAYOUT_PAGE_SIZE
);
612 nboob
= min_t(size_t, ooblen
, (size_t)DOC_LAYOUT_OOB_SIZE
);
613 ret
= doc_read_page_prepare(docg3
, block0
, block1
, page
, ofs
);
616 ret
= doc_read_page_ecc_init(docg3
, DOC_ECC_BCH_COVERED_BYTES
);
619 ret
= doc_read_page_getbytes(docg3
, nbdata
, buf
, 1);
622 doc_read_page_getbytes(docg3
, DOC_LAYOUT_PAGE_SIZE
- nbdata
,
624 ret
= doc_read_page_getbytes(docg3
, nboob
, oobbuf
, 0);
627 doc_read_page_getbytes(docg3
, DOC_LAYOUT_OOB_SIZE
- nboob
,
630 doc_get_hw_bch_syndroms(docg3
, calc_ecc
);
631 eccconf1
= doc_register_readb(docg3
, DOC_ECCCONF1
);
633 if (nboob
>= DOC_LAYOUT_OOB_SIZE
) {
634 doc_dbg("OOB - INFO: %02x:%02x:%02x:%02x:%02x:%02x:%02x\n",
635 oobbuf
[0], oobbuf
[1], oobbuf
[2], oobbuf
[3],
636 oobbuf
[4], oobbuf
[5], oobbuf
[6]);
637 doc_dbg("OOB - HAMMING: %02x\n", oobbuf
[7]);
638 doc_dbg("OOB - BCH_ECC: %02x:%02x:%02x:%02x:%02x:%02x:%02x\n",
639 oobbuf
[8], oobbuf
[9], oobbuf
[10], oobbuf
[11],
640 oobbuf
[12], oobbuf
[13], oobbuf
[14]);
641 doc_dbg("OOB - UNUSED: %02x\n", oobbuf
[15]);
643 doc_dbg("ECC checks: ECCConf1=%x\n", eccconf1
);
644 doc_dbg("ECC CALC_ECC: %02x:%02x:%02x:%02x:%02x:%02x:%02x\n",
645 calc_ecc
[0], calc_ecc
[1], calc_ecc
[2],
646 calc_ecc
[3], calc_ecc
[4], calc_ecc
[5],
650 if (block0
>= DOC_LAYOUT_BLOCK_FIRST_DATA
) {
651 if ((eccconf1
& DOC_ECCCONF1_BCH_SYNDROM_ERR
) &&
652 (eccconf1
& DOC_ECCCONF1_PAGE_IS_WRITTEN
))
654 if (is_prot_seq_error(docg3
))
658 doc_read_page_finish(docg3
);
659 ops
->retlen
+= nbdata
;
660 ops
->oobretlen
+= nboob
;
665 from
+= DOC_LAYOUT_PAGE_SIZE
;
670 doc_read_page_finish(docg3
);
676 * doc_read - Read bytes from flash
678 * @from: the offset from first block and first page, in bytes, aligned on page
680 * @len: the number of bytes to read (must be a multiple of 4)
681 * @retlen: the number of bytes actually read
682 * @buf: the filled in buffer
684 * Reads flash memory pages. This function does not read the OOB chunk, but only
687 * Returns 0 if read successfull, of -EIO, -EINVAL if an error occured
689 static int doc_read(struct mtd_info
*mtd
, loff_t from
, size_t len
,
690 size_t *retlen
, u_char
*buf
)
692 struct mtd_oob_ops ops
;
695 memset(&ops
, 0, sizeof(ops
));
698 ops
.mode
= MTD_OPS_AUTO_OOB
;
700 ret
= doc_read_oob(mtd
, from
, &ops
);
701 *retlen
= ops
.retlen
;
705 static int doc_reload_bbt(struct docg3
*docg3
)
707 int block
= DOC_LAYOUT_BLOCK_BBT
;
708 int ret
= 0, nbpages
, page
;
709 u_char
*buf
= docg3
->bbt
;
711 nbpages
= DIV_ROUND_UP(docg3
->max_block
+ 1, 8 * DOC_LAYOUT_PAGE_SIZE
);
712 for (page
= 0; !ret
&& (page
< nbpages
); page
++) {
713 ret
= doc_read_page_prepare(docg3
, block
, block
+ 1,
714 page
+ DOC_LAYOUT_PAGE_BBT
, 0);
716 ret
= doc_read_page_ecc_init(docg3
,
717 DOC_LAYOUT_PAGE_SIZE
);
719 doc_read_page_getbytes(docg3
, DOC_LAYOUT_PAGE_SIZE
,
721 buf
+= DOC_LAYOUT_PAGE_SIZE
;
723 doc_read_page_finish(docg3
);
728 * doc_block_isbad - Checks whether a block is good or not
730 * @from: the offset to find the correct block
732 * Returns 1 if block is bad, 0 if block is good
734 static int doc_block_isbad(struct mtd_info
*mtd
, loff_t from
)
736 struct docg3
*docg3
= mtd
->priv
;
737 int block0
, block1
, page
, ofs
, is_good
;
739 calc_block_sector(from
, &block0
, &block1
, &page
, &ofs
);
740 doc_dbg("doc_block_isbad(from=%lld) => block=(%d,%d), page=%d, ofs=%d\n",
741 from
, block0
, block1
, page
, ofs
);
743 if (block0
< DOC_LAYOUT_BLOCK_FIRST_DATA
)
745 if (block1
> docg3
->max_block
)
748 is_good
= docg3
->bbt
[block0
>> 3] & (1 << (block0
& 0x7));
753 * doc_get_erase_count - Get block erase count
755 * @from: the offset in which the block is.
757 * Get the number of times a block was erased. The number is the maximum of
758 * erase times between first and second plane (which should be equal normally).
760 * Returns The number of erases, or -EINVAL or -EIO on error.
762 static int doc_get_erase_count(struct docg3
*docg3
, loff_t from
)
764 u8 buf
[DOC_LAYOUT_WEAR_SIZE
];
765 int ret
, plane1_erase_count
, plane2_erase_count
;
766 int block0
, block1
, page
, ofs
;
768 doc_dbg("doc_get_erase_count(from=%lld, buf=%p)\n", from
, buf
);
769 if (from
% DOC_LAYOUT_PAGE_SIZE
)
771 calc_block_sector(from
, &block0
, &block1
, &page
, &ofs
);
772 if (block1
> docg3
->max_block
)
775 ret
= doc_reset_seq(docg3
);
777 ret
= doc_read_page_prepare(docg3
, block0
, block1
, page
,
778 ofs
+ DOC_LAYOUT_WEAR_OFFSET
);
780 ret
= doc_read_page_getbytes(docg3
, DOC_LAYOUT_WEAR_SIZE
,
782 doc_read_page_finish(docg3
);
784 if (ret
|| (buf
[0] != DOC_ERASE_MARK
) || (buf
[2] != DOC_ERASE_MARK
))
786 plane1_erase_count
= (u8
)(~buf
[1]) | ((u8
)(~buf
[4]) << 8)
787 | ((u8
)(~buf
[5]) << 16);
788 plane2_erase_count
= (u8
)(~buf
[3]) | ((u8
)(~buf
[6]) << 8)
789 | ((u8
)(~buf
[7]) << 16);
791 return max(plane1_erase_count
, plane2_erase_count
);
795 * Debug sysfs entries
797 static int dbg_flashctrl_show(struct seq_file
*s
, void *p
)
799 struct docg3
*docg3
= (struct docg3
*)s
->private;
802 u8 fctrl
= doc_register_readb(docg3
, DOC_FLASHCONTROL
);
805 "FlashControl : 0x%02x (%s,CE# %s,%s,%s,flash %s)\n",
807 fctrl
& DOC_CTRL_VIOLATION
? "protocol violation" : "-",
808 fctrl
& DOC_CTRL_CE
? "active" : "inactive",
809 fctrl
& DOC_CTRL_PROTECTION_ERROR
? "protection error" : "-",
810 fctrl
& DOC_CTRL_SEQUENCE_ERROR
? "sequence error" : "-",
811 fctrl
& DOC_CTRL_FLASHREADY
? "ready" : "not ready");
814 DEBUGFS_RO_ATTR(flashcontrol
, dbg_flashctrl_show
);
816 static int dbg_asicmode_show(struct seq_file
*s
, void *p
)
818 struct docg3
*docg3
= (struct docg3
*)s
->private;
821 int pctrl
= doc_register_readb(docg3
, DOC_ASICMODE
);
822 int mode
= pctrl
& 0x03;
825 "%04x : RAM_WE=%d,RSTIN_RESET=%d,BDETCT_RESET=%d,WRITE_ENABLE=%d,POWERDOWN=%d,MODE=%d%d (",
827 pctrl
& DOC_ASICMODE_RAM_WE
? 1 : 0,
828 pctrl
& DOC_ASICMODE_RSTIN_RESET
? 1 : 0,
829 pctrl
& DOC_ASICMODE_BDETCT_RESET
? 1 : 0,
830 pctrl
& DOC_ASICMODE_MDWREN
? 1 : 0,
831 pctrl
& DOC_ASICMODE_POWERDOWN
? 1 : 0,
832 mode
>> 1, mode
& 0x1);
835 case DOC_ASICMODE_RESET
:
836 pos
+= seq_printf(s
, "reset");
838 case DOC_ASICMODE_NORMAL
:
839 pos
+= seq_printf(s
, "normal");
841 case DOC_ASICMODE_POWERDOWN
:
842 pos
+= seq_printf(s
, "powerdown");
845 pos
+= seq_printf(s
, ")\n");
848 DEBUGFS_RO_ATTR(asic_mode
, dbg_asicmode_show
);
850 static int dbg_device_id_show(struct seq_file
*s
, void *p
)
852 struct docg3
*docg3
= (struct docg3
*)s
->private;
854 int id
= doc_register_readb(docg3
, DOC_DEVICESELECT
);
856 pos
+= seq_printf(s
, "DeviceId = %d\n", id
);
859 DEBUGFS_RO_ATTR(device_id
, dbg_device_id_show
);
861 static int dbg_protection_show(struct seq_file
*s
, void *p
)
863 struct docg3
*docg3
= (struct docg3
*)s
->private;
865 int protect
, dps0
, dps0_low
, dps0_high
, dps1
, dps1_low
, dps1_high
;
867 protect
= doc_register_readb(docg3
, DOC_PROTECTION
);
868 dps0
= doc_register_readb(docg3
, DOC_DPS0_STATUS
);
869 dps0_low
= doc_register_readw(docg3
, DOC_DPS0_ADDRLOW
);
870 dps0_high
= doc_register_readw(docg3
, DOC_DPS0_ADDRHIGH
);
871 dps1
= doc_register_readb(docg3
, DOC_DPS1_STATUS
);
872 dps1_low
= doc_register_readw(docg3
, DOC_DPS1_ADDRLOW
);
873 dps1_high
= doc_register_readw(docg3
, DOC_DPS1_ADDRHIGH
);
875 pos
+= seq_printf(s
, "Protection = 0x%02x (",
877 if (protect
& DOC_PROTECT_FOUNDRY_OTP_LOCK
)
878 pos
+= seq_printf(s
, "FOUNDRY_OTP_LOCK,");
879 if (protect
& DOC_PROTECT_CUSTOMER_OTP_LOCK
)
880 pos
+= seq_printf(s
, "CUSTOMER_OTP_LOCK,");
881 if (protect
& DOC_PROTECT_LOCK_INPUT
)
882 pos
+= seq_printf(s
, "LOCK_INPUT,");
883 if (protect
& DOC_PROTECT_STICKY_LOCK
)
884 pos
+= seq_printf(s
, "STICKY_LOCK,");
885 if (protect
& DOC_PROTECT_PROTECTION_ENABLED
)
886 pos
+= seq_printf(s
, "PROTECTION ON,");
887 if (protect
& DOC_PROTECT_IPL_DOWNLOAD_LOCK
)
888 pos
+= seq_printf(s
, "IPL_DOWNLOAD_LOCK,");
889 if (protect
& DOC_PROTECT_PROTECTION_ERROR
)
890 pos
+= seq_printf(s
, "PROTECT_ERR,");
892 pos
+= seq_printf(s
, "NO_PROTECT_ERR");
893 pos
+= seq_printf(s
, ")\n");
895 pos
+= seq_printf(s
, "DPS0 = 0x%02x : "
896 "Protected area [0x%x - 0x%x] : OTP=%d, READ=%d, "
897 "WRITE=%d, HW_LOCK=%d, KEY_OK=%d\n",
898 dps0
, dps0_low
, dps0_high
,
899 !!(dps0
& DOC_DPS_OTP_PROTECTED
),
900 !!(dps0
& DOC_DPS_READ_PROTECTED
),
901 !!(dps0
& DOC_DPS_WRITE_PROTECTED
),
902 !!(dps0
& DOC_DPS_HW_LOCK_ENABLED
),
903 !!(dps0
& DOC_DPS_KEY_OK
));
904 pos
+= seq_printf(s
, "DPS1 = 0x%02x : "
905 "Protected area [0x%x - 0x%x] : OTP=%d, READ=%d, "
906 "WRITE=%d, HW_LOCK=%d, KEY_OK=%d\n",
907 dps1
, dps1_low
, dps1_high
,
908 !!(dps1
& DOC_DPS_OTP_PROTECTED
),
909 !!(dps1
& DOC_DPS_READ_PROTECTED
),
910 !!(dps1
& DOC_DPS_WRITE_PROTECTED
),
911 !!(dps1
& DOC_DPS_HW_LOCK_ENABLED
),
912 !!(dps1
& DOC_DPS_KEY_OK
));
915 DEBUGFS_RO_ATTR(protection
, dbg_protection_show
);
917 static int __init
doc_dbg_register(struct docg3
*docg3
)
919 struct dentry
*root
, *entry
;
921 root
= debugfs_create_dir("docg3", NULL
);
925 entry
= debugfs_create_file("flashcontrol", S_IRUSR
, root
, docg3
,
928 entry
= debugfs_create_file("asic_mode", S_IRUSR
, root
,
929 docg3
, &asic_mode_fops
);
931 entry
= debugfs_create_file("device_id", S_IRUSR
, root
,
932 docg3
, &device_id_fops
);
934 entry
= debugfs_create_file("protection", S_IRUSR
, root
,
935 docg3
, &protection_fops
);
937 docg3
->debugfs_root
= root
;
940 debugfs_remove_recursive(root
);
945 static void __exit
doc_dbg_unregister(struct docg3
*docg3
)
947 debugfs_remove_recursive(docg3
->debugfs_root
);
951 * doc_set_driver_info - Fill the mtd_info structure and docg3 structure
952 * @chip_id: The chip ID of the supported chip
953 * @mtd: The structure to fill
955 static void __init
doc_set_driver_info(int chip_id
, struct mtd_info
*mtd
)
957 struct docg3
*docg3
= mtd
->priv
;
960 cfg
= doc_register_readb(docg3
, DOC_CONFIGURATION
);
961 docg3
->if_cfg
= (cfg
& DOC_CONF_IF_CFG
? 1 : 0);
965 mtd
->name
= kasprintf(GFP_KERNEL
, "DiskOnChip G3 floor %d",
967 docg3
->max_block
= 2047;
970 mtd
->type
= MTD_NANDFLASH
;
972 * Once write methods are added, the correct flags will be set.
973 * mtd->flags = MTD_CAP_NANDFLASH;
975 mtd
->flags
= MTD_CAP_ROM
;
976 mtd
->size
= (docg3
->max_block
+ 1) * DOC_LAYOUT_BLOCK_SIZE
;
977 mtd
->erasesize
= DOC_LAYOUT_BLOCK_SIZE
* DOC_LAYOUT_NBPLANES
;
978 mtd
->writesize
= DOC_LAYOUT_PAGE_SIZE
;
979 mtd
->oobsize
= DOC_LAYOUT_OOB_SIZE
;
980 mtd
->owner
= THIS_MODULE
;
984 mtd
->read
= doc_read
;
986 mtd
->read_oob
= doc_read_oob
;
987 mtd
->write_oob
= NULL
;
989 mtd
->block_isbad
= doc_block_isbad
;
990 mtd
->ecclayout
= &docg3_oobinfo
;
994 * doc_probe_device - Check if a device is available
995 * @base: the io space where the device is probed
996 * @floor: the floor of the probed device
999 * Checks whether a device at the specified IO range, and floor is available.
1001 * Returns a mtd_info struct if there is a device, ENODEV if none found, ENOMEM
1002 * if a memory allocation failed. If floor 0 is checked, a reset of the ASIC is
1005 static struct mtd_info
*doc_probe_device(void __iomem
*base
, int floor
,
1008 int ret
, bbt_nbpages
;
1009 u16 chip_id
, chip_id_inv
;
1010 struct docg3
*docg3
;
1011 struct mtd_info
*mtd
;
1014 docg3
= kzalloc(sizeof(struct docg3
), GFP_KERNEL
);
1017 mtd
= kzalloc(sizeof(struct mtd_info
), GFP_KERNEL
);
1021 bbt_nbpages
= DIV_ROUND_UP(docg3
->max_block
+ 1,
1022 8 * DOC_LAYOUT_PAGE_SIZE
);
1023 docg3
->bbt
= kzalloc(bbt_nbpages
* DOC_LAYOUT_PAGE_SIZE
, GFP_KERNEL
);
1028 docg3
->device_id
= floor
;
1030 doc_set_device_id(docg3
, docg3
->device_id
);
1032 doc_set_asic_mode(docg3
, DOC_ASICMODE_RESET
);
1033 doc_set_asic_mode(docg3
, DOC_ASICMODE_NORMAL
);
1035 chip_id
= doc_register_readw(docg3
, DOC_CHIPID
);
1036 chip_id_inv
= doc_register_readw(docg3
, DOC_CHIPID_INV
);
1039 if (chip_id
!= (u16
)(~chip_id_inv
)) {
1045 doc_info("Found a G3 DiskOnChip at addr %p, floor %d\n",
1049 doc_err("Chip id %04x is not a DiskOnChip G3 chip\n", chip_id
);
1053 doc_set_driver_info(chip_id
, mtd
);
1055 doc_reload_bbt(docg3
);
1063 return ERR_PTR(ret
);
1067 * doc_release_device - Release a docg3 floor
1070 static void doc_release_device(struct mtd_info
*mtd
)
1072 struct docg3
*docg3
= mtd
->priv
;
1074 mtd_device_unregister(mtd
);
1082 * doc_probe - Probe the IO space for a DiskOnChip G3 chip
1083 * @pdev: platform device
1085 * Probes for a G3 chip at the specified IO space in the platform data
1086 * ressources. The floor 0 must be available.
1088 * Returns 0 on success, -ENOMEM, -ENXIO on error
1090 static int __init
docg3_probe(struct platform_device
*pdev
)
1092 struct device
*dev
= &pdev
->dev
;
1093 struct mtd_info
*mtd
;
1094 struct resource
*ress
;
1096 int ret
, floor
, found
= 0;
1097 struct mtd_info
**docg3_floors
;
1100 ress
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1102 dev_err(dev
, "No I/O memory resource defined\n");
1105 base
= ioremap(ress
->start
, DOC_IOSPACE_SIZE
);
1108 docg3_floors
= kzalloc(sizeof(*docg3_floors
) * DOC_MAX_NBFLOORS
,
1114 for (floor
= 0; floor
< DOC_MAX_NBFLOORS
; floor
++) {
1115 mtd
= doc_probe_device(base
, floor
, dev
);
1116 if (floor
== 0 && !mtd
)
1118 if (!IS_ERR_OR_NULL(mtd
))
1119 ret
= mtd_device_parse_register(mtd
, part_probes
,
1123 docg3_floors
[floor
] = mtd
;
1133 platform_set_drvdata(pdev
, docg3_floors
);
1134 doc_dbg_register(docg3_floors
[0]->priv
);
1139 dev_info(dev
, "No supported DiskOnChip found\n");
1141 for (floor
= 0; floor
< DOC_MAX_NBFLOORS
; floor
++)
1142 if (docg3_floors
[floor
])
1143 doc_release_device(docg3_floors
[floor
]);
1151 * docg3_release - Release the driver
1152 * @pdev: the platform device
1156 static int __exit
docg3_release(struct platform_device
*pdev
)
1158 struct mtd_info
**docg3_floors
= platform_get_drvdata(pdev
);
1159 struct docg3
*docg3
= docg3_floors
[0]->priv
;
1160 void __iomem
*base
= docg3
->base
;
1163 doc_dbg_unregister(docg3
);
1164 for (floor
= 0; floor
< DOC_MAX_NBFLOORS
; floor
++)
1165 if (docg3_floors
[floor
])
1166 doc_release_device(docg3_floors
[floor
]);
1168 kfree(docg3_floors
);
1173 static struct platform_driver g3_driver
= {
1176 .owner
= THIS_MODULE
,
1178 .remove
= __exit_p(docg3_release
),
1181 static int __init
docg3_init(void)
1183 return platform_driver_probe(&g3_driver
, docg3_probe
);
1185 module_init(docg3_init
);
1188 static void __exit
docg3_exit(void)
1190 platform_driver_unregister(&g3_driver
);
1192 module_exit(docg3_exit
);
1194 MODULE_LICENSE("GPL");
1195 MODULE_AUTHOR("Robert Jarzmik <robert.jarzmik@free.fr>");
1196 MODULE_DESCRIPTION("MTD driver for DiskOnChip G3");