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
66 static inline u8
doc_readb(struct docg3
*docg3
, u16 reg
)
68 u8 val
= readb(docg3
->base
+ reg
);
70 trace_docg3_io(0, 8, reg
, (int)val
);
74 static inline u16
doc_readw(struct docg3
*docg3
, u16 reg
)
76 u16 val
= readw(docg3
->base
+ reg
);
78 trace_docg3_io(0, 16, reg
, (int)val
);
82 static inline void doc_writeb(struct docg3
*docg3
, u8 val
, u16 reg
)
84 writeb(val
, docg3
->base
+ reg
);
85 trace_docg3_io(1, 16, reg
, val
);
88 static inline void doc_writew(struct docg3
*docg3
, u16 val
, u16 reg
)
90 writew(val
, docg3
->base
+ reg
);
91 trace_docg3_io(1, 16, reg
, val
);
94 static inline void doc_flash_command(struct docg3
*docg3
, u8 cmd
)
96 doc_writeb(docg3
, cmd
, DOC_FLASHCOMMAND
);
99 static inline void doc_flash_sequence(struct docg3
*docg3
, u8 seq
)
101 doc_writeb(docg3
, seq
, DOC_FLASHSEQUENCE
);
104 static inline void doc_flash_address(struct docg3
*docg3
, u8 addr
)
106 doc_writeb(docg3
, addr
, DOC_FLASHADDRESS
);
109 static char const *part_probes
[] = { "cmdlinepart", "saftlpart", NULL
};
111 static int doc_register_readb(struct docg3
*docg3
, int reg
)
115 doc_writew(docg3
, reg
, DOC_READADDRESS
);
116 val
= doc_readb(docg3
, reg
);
117 doc_vdbg("Read register %04x : %02x\n", reg
, val
);
121 static int doc_register_readw(struct docg3
*docg3
, int reg
)
125 doc_writew(docg3
, reg
, DOC_READADDRESS
);
126 val
= doc_readw(docg3
, reg
);
127 doc_vdbg("Read register %04x : %04x\n", reg
, val
);
132 * doc_delay - delay docg3 operations
134 * @nbNOPs: the number of NOPs to issue
136 * As no specification is available, the right timings between chip commands are
137 * unknown. The only available piece of information are the observed nops on a
138 * working docg3 chip.
139 * Therefore, doc_delay relies on a busy loop of NOPs, instead of scheduler
140 * friendlier msleep() functions or blocking mdelay().
142 static void doc_delay(struct docg3
*docg3
, int nbNOPs
)
146 doc_dbg("NOP x %d\n", nbNOPs
);
147 for (i
= 0; i
< nbNOPs
; i
++)
148 doc_writeb(docg3
, 0, DOC_NOP
);
151 static int is_prot_seq_error(struct docg3
*docg3
)
155 ctrl
= doc_register_readb(docg3
, DOC_FLASHCONTROL
);
156 return ctrl
& (DOC_CTRL_PROTECTION_ERROR
| DOC_CTRL_SEQUENCE_ERROR
);
159 static int doc_is_ready(struct docg3
*docg3
)
163 ctrl
= doc_register_readb(docg3
, DOC_FLASHCONTROL
);
164 return ctrl
& DOC_CTRL_FLASHREADY
;
167 static int doc_wait_ready(struct docg3
*docg3
)
169 int maxWaitCycles
= 100;
174 } while (!doc_is_ready(docg3
) && maxWaitCycles
--);
176 if (maxWaitCycles
> 0)
182 static int doc_reset_seq(struct docg3
*docg3
)
186 doc_writeb(docg3
, 0x10, DOC_FLASHCONTROL
);
187 doc_flash_sequence(docg3
, DOC_SEQ_RESET
);
188 doc_flash_command(docg3
, DOC_CMD_RESET
);
190 ret
= doc_wait_ready(docg3
);
192 doc_dbg("doc_reset_seq() -> isReady=%s\n", ret
? "false" : "true");
197 * doc_read_data_area - Read data from data area
199 * @buf: the buffer to fill in
200 * @len: the lenght to read
201 * @first: first time read, DOC_READADDRESS should be set
203 * Reads bytes from flash data. Handles the single byte / even bytes reads.
205 static void doc_read_data_area(struct docg3
*docg3
, void *buf
, int len
,
212 doc_dbg("doc_read_data_area(buf=%p, len=%d)\n", buf
, len
);
217 doc_writew(docg3
, DOC_IOSPACE_DATA
, DOC_READADDRESS
);
219 for (i
= 0; i
< len4
; i
+= 2) {
220 data16
= doc_readw(docg3
, DOC_IOSPACE_DATA
);
226 doc_writew(docg3
, DOC_IOSPACE_DATA
| DOC_READADDR_ONE_BYTE
,
230 for (i
= 0; i
< cdr
; i
++) {
231 data8
= doc_readb(docg3
, DOC_IOSPACE_DATA
);
239 * doc_set_data_mode - Sets the flash to reliable data mode
242 * The reliable data mode is a bit slower than the fast mode, but less errors
243 * occur. Entering the reliable mode cannot be done without entering the fast
246 static void doc_set_reliable_mode(struct docg3
*docg3
)
248 doc_dbg("doc_set_reliable_mode()\n");
249 doc_flash_sequence(docg3
, DOC_SEQ_SET_MODE
);
250 doc_flash_command(docg3
, DOC_CMD_FAST_MODE
);
251 doc_flash_command(docg3
, DOC_CMD_RELIABLE_MODE
);
256 * doc_set_asic_mode - Set the ASIC mode
260 * The ASIC can work in 3 modes :
261 * - RESET: all registers are zeroed
262 * - NORMAL: receives and handles commands
263 * - POWERDOWN: minimal poweruse, flash parts shut off
265 static void doc_set_asic_mode(struct docg3
*docg3
, u8 mode
)
269 for (i
= 0; i
< 12; i
++)
270 doc_readb(docg3
, DOC_IOSPACE_IPL
);
272 mode
|= DOC_ASICMODE_MDWREN
;
273 doc_dbg("doc_set_asic_mode(%02x)\n", mode
);
274 doc_writeb(docg3
, mode
, DOC_ASICMODE
);
275 doc_writeb(docg3
, ~mode
, DOC_ASICMODECONFIRM
);
280 * doc_set_device_id - Sets the devices id for cascaded G3 chips
282 * @id: the chip to select (amongst 0, 1, 2, 3)
284 * There can be 4 cascaded G3 chips. This function selects the one which will
285 * should be the active one.
287 static void doc_set_device_id(struct docg3
*docg3
, int id
)
291 doc_dbg("doc_set_device_id(%d)\n", id
);
292 doc_writeb(docg3
, id
, DOC_DEVICESELECT
);
293 ctrl
= doc_register_readb(docg3
, DOC_FLASHCONTROL
);
295 ctrl
&= ~DOC_CTRL_VIOLATION
;
297 doc_writeb(docg3
, ctrl
, DOC_FLASHCONTROL
);
301 * doc_set_extra_page_mode - Change flash page layout
304 * Normally, the flash page is split into the data (512 bytes) and the out of
305 * band data (16 bytes). For each, 4 more bytes can be accessed, where the wear
306 * leveling counters are stored. To access this last area of 4 bytes, a special
307 * mode must be input to the flash ASIC.
309 * Returns 0 if no error occured, -EIO else.
311 static int doc_set_extra_page_mode(struct docg3
*docg3
)
315 doc_dbg("doc_set_extra_page_mode()\n");
316 doc_flash_sequence(docg3
, DOC_SEQ_PAGE_SIZE_532
);
317 doc_flash_command(docg3
, DOC_CMD_PAGE_SIZE_532
);
320 fctrl
= doc_register_readb(docg3
, DOC_FLASHCONTROL
);
321 if (fctrl
& (DOC_CTRL_PROTECTION_ERROR
| DOC_CTRL_SEQUENCE_ERROR
))
328 * doc_seek - Set both flash planes to the specified block, page for reading
330 * @block0: the first plane block index
331 * @block1: the second plane block index
332 * @page: the page index within the block
333 * @wear: if true, read will occur on the 4 extra bytes of the wear area
334 * @ofs: offset in page to read
336 * Programs the flash even and odd planes to the specific block and page.
337 * Alternatively, programs the flash to the wear area of the specified page.
339 static int doc_read_seek(struct docg3
*docg3
, int block0
, int block1
, int page
,
344 doc_dbg("doc_seek(blocks=(%d,%d), page=%d, ofs=%d, wear=%d)\n",
345 block0
, block1
, page
, ofs
, wear
);
347 if (!wear
&& (ofs
< 2 * DOC_LAYOUT_PAGE_SIZE
)) {
348 doc_flash_sequence(docg3
, DOC_SEQ_SET_PLANE1
);
349 doc_flash_command(docg3
, DOC_CMD_READ_PLANE1
);
352 doc_flash_sequence(docg3
, DOC_SEQ_SET_PLANE2
);
353 doc_flash_command(docg3
, DOC_CMD_READ_PLANE2
);
357 doc_set_reliable_mode(docg3
);
359 ret
= doc_set_extra_page_mode(docg3
);
363 sector
= (block0
<< DOC_ADDR_BLOCK_SHIFT
) + (page
& DOC_ADDR_PAGE_MASK
);
364 doc_flash_sequence(docg3
, DOC_SEQ_READ
);
365 doc_flash_command(docg3
, DOC_CMD_PROG_BLOCK_ADDR
);
367 doc_flash_address(docg3
, sector
& 0xff);
368 doc_flash_address(docg3
, (sector
>> 8) & 0xff);
369 doc_flash_address(docg3
, (sector
>> 16) & 0xff);
372 sector
= (block1
<< DOC_ADDR_BLOCK_SHIFT
) + (page
& DOC_ADDR_PAGE_MASK
);
373 doc_flash_command(docg3
, DOC_CMD_PROG_BLOCK_ADDR
);
375 doc_flash_address(docg3
, sector
& 0xff);
376 doc_flash_address(docg3
, (sector
>> 8) & 0xff);
377 doc_flash_address(docg3
, (sector
>> 16) & 0xff);
385 * doc_read_page_ecc_init - Initialize hardware ECC engine
387 * @len: the number of bytes covered by the ECC (BCH covered)
389 * The function does initialize the hardware ECC engine to compute the Hamming
390 * ECC (on 1 byte) and the BCH Syndroms (on 7 bytes).
392 * Return 0 if succeeded, -EIO on error
394 static int doc_read_page_ecc_init(struct docg3
*docg3
, int len
)
396 doc_writew(docg3
, DOC_ECCCONF0_READ_MODE
397 | DOC_ECCCONF0_BCH_ENABLE
| DOC_ECCCONF0_HAMMING_ENABLE
398 | (len
& DOC_ECCCONF0_DATA_BYTES_MASK
),
401 doc_register_readb(docg3
, DOC_FLASHCONTROL
);
402 return doc_wait_ready(docg3
);
406 * doc_read_page_prepare - Prepares reading data from a flash page
408 * @block0: the first plane block index on flash memory
409 * @block1: the second plane block index on flash memory
410 * @page: the page index in the block
411 * @offset: the offset in the page (must be a multiple of 4)
413 * Prepares the page to be read in the flash memory :
414 * - tell ASIC to map the flash pages
415 * - tell ASIC to be in read mode
417 * After a call to this method, a call to doc_read_page_finish is mandatory,
418 * to end the read cycle of the flash.
420 * Read data from a flash page. The length to be read must be between 0 and
421 * (page_size + oob_size + wear_size), ie. 532, and a multiple of 4 (because
422 * the extra bytes reading is not implemented).
424 * As pages are grouped by 2 (in 2 planes), reading from a page must be done
426 * - one read of 512 bytes at offset 0
427 * - one read of 512 bytes at offset 512 + 16
429 * Returns 0 if successful, -EIO if a read error occured.
431 static int doc_read_page_prepare(struct docg3
*docg3
, int block0
, int block1
,
432 int page
, int offset
)
434 int wear_area
= 0, ret
= 0;
436 doc_dbg("doc_read_page_prepare(blocks=(%d,%d), page=%d, ofsInPage=%d)\n",
437 block0
, block1
, page
, offset
);
438 if (offset
>= DOC_LAYOUT_WEAR_OFFSET
)
440 if (!wear_area
&& offset
> (DOC_LAYOUT_PAGE_OOB_SIZE
* 2))
443 doc_set_device_id(docg3
, docg3
->device_id
);
444 ret
= doc_reset_seq(docg3
);
448 /* Program the flash address block and page */
449 ret
= doc_read_seek(docg3
, block0
, block1
, page
, wear_area
, offset
);
453 doc_flash_command(docg3
, DOC_CMD_READ_ALL_PLANES
);
455 doc_wait_ready(docg3
);
457 doc_flash_command(docg3
, DOC_CMD_SET_ADDR_READ
);
459 if (offset
>= DOC_LAYOUT_PAGE_SIZE
* 2)
460 offset
-= 2 * DOC_LAYOUT_PAGE_SIZE
;
461 doc_flash_address(docg3
, offset
>> 2);
463 doc_wait_ready(docg3
);
465 doc_flash_command(docg3
, DOC_CMD_READ_FLASH
);
469 doc_writeb(docg3
, 0, DOC_DATAEND
);
475 * doc_read_page_getbytes - Reads bytes from a prepared page
477 * @len: the number of bytes to be read (must be a multiple of 4)
478 * @buf: the buffer to be filled in
479 * @first: 1 if first time read, DOC_READADDRESS should be set
482 static int doc_read_page_getbytes(struct docg3
*docg3
, int len
, u_char
*buf
,
485 doc_read_data_area(docg3
, buf
, len
, first
);
491 * doc_get_hw_bch_syndroms - Get hardware calculated BCH syndroms
493 * @syns: the array of 7 integers where the syndroms will be stored
495 static void doc_get_hw_bch_syndroms(struct docg3
*docg3
, int *syns
)
499 for (i
= 0; i
< DOC_ECC_BCH_SIZE
; i
++)
500 syns
[i
] = doc_register_readb(docg3
, DOC_BCH_SYNDROM(i
));
504 * doc_read_page_finish - Ends reading of a flash page
507 * As a side effect, resets the chip selector to 0. This ensures that after each
508 * read operation, the floor 0 is selected. Therefore, if the systems halts, the
509 * reboot will boot on floor 0, where the IPL is.
511 static void doc_read_page_finish(struct docg3
*docg3
)
513 doc_writeb(docg3
, 0, DOC_DATAEND
);
515 doc_set_device_id(docg3
, 0);
519 * calc_block_sector - Calculate blocks, pages and ofs.
521 * @from: offset in flash
522 * @block0: first plane block index calculated
523 * @block1: second plane block index calculated
524 * @page: page calculated
525 * @ofs: offset in page
527 static void calc_block_sector(loff_t from
, int *block0
, int *block1
, int *page
,
532 sector
= from
/ DOC_LAYOUT_PAGE_SIZE
;
533 *block0
= sector
/ (DOC_LAYOUT_PAGES_PER_BLOCK
* DOC_LAYOUT_NBPLANES
)
534 * DOC_LAYOUT_NBPLANES
;
535 *block1
= *block0
+ 1;
536 *page
= sector
% (DOC_LAYOUT_PAGES_PER_BLOCK
* DOC_LAYOUT_NBPLANES
);
537 *page
/= DOC_LAYOUT_NBPLANES
;
539 *ofs
= DOC_LAYOUT_PAGE_OOB_SIZE
;
545 * doc_read - Read bytes from flash
547 * @from: the offset from first block and first page, in bytes, aligned on page
549 * @len: the number of bytes to read (must be a multiple of 4)
550 * @retlen: the number of bytes actually read
551 * @buf: the filled in buffer
553 * Reads flash memory pages. This function does not read the OOB chunk, but only
556 * Returns 0 if read successfull, of -EIO, -EINVAL if an error occured
558 static int doc_read(struct mtd_info
*mtd
, loff_t from
, size_t len
,
559 size_t *retlen
, u_char
*buf
)
561 struct docg3
*docg3
= mtd
->priv
;
562 int block0
, block1
, page
, readlen
, ret
, ofs
= 0;
563 int syn
[DOC_ECC_BCH_SIZE
], eccconf1
;
564 u8 oob
[DOC_LAYOUT_OOB_SIZE
];
567 doc_dbg("doc_read(from=%lld, len=%zu, buf=%p)\n", from
, len
, buf
);
568 if (from
% DOC_LAYOUT_PAGE_SIZE
)
572 calc_block_sector(from
, &block0
, &block1
, &page
, &ofs
);
573 if (block1
> docg3
->max_block
)
578 readlen
= min_t(size_t, len
, (size_t)DOC_LAYOUT_PAGE_SIZE
);
579 while (!ret
&& len
> 0) {
580 readlen
= min_t(size_t, len
, (size_t)DOC_LAYOUT_PAGE_SIZE
);
581 ret
= doc_read_page_prepare(docg3
, block0
, block1
, page
, ofs
);
584 ret
= doc_read_page_ecc_init(docg3
, DOC_ECC_BCH_COVERED_BYTES
);
587 ret
= doc_read_page_getbytes(docg3
, readlen
, buf
, 1);
590 ret
= doc_read_page_getbytes(docg3
, DOC_LAYOUT_OOB_SIZE
,
592 if (ret
< DOC_LAYOUT_OOB_SIZE
)
599 ofs
^= DOC_LAYOUT_PAGE_OOB_SIZE
;
602 if (page
> DOC_ADDR_PAGE_MASK
) {
609 * There should be a BCH bitstream fixing algorithm here ...
610 * By now, a page read failure is triggered by BCH error
612 doc_get_hw_bch_syndroms(docg3
, syn
);
613 eccconf1
= doc_register_readb(docg3
, DOC_ECCCONF1
);
615 doc_dbg("OOB - INFO: %02x:%02x:%02x:%02x:%02x:%02x:%02x\n",
616 oob
[0], oob
[1], oob
[2], oob
[3], oob
[4],
618 doc_dbg("OOB - HAMMING: %02x\n", oob
[7]);
619 doc_dbg("OOB - BCH_ECC: %02x:%02x:%02x:%02x:%02x:%02x:%02x\n",
620 oob
[8], oob
[9], oob
[10], oob
[11], oob
[12],
622 doc_dbg("OOB - UNUSED: %02x\n", oob
[15]);
623 doc_dbg("ECC checks: ECCConf1=%x\n", eccconf1
);
624 doc_dbg("ECC BCH syndrom: %02x:%02x:%02x:%02x:%02x:%02x:%02x\n",
625 syn
[0], syn
[1], syn
[2], syn
[3], syn
[4], syn
[5], syn
[6]);
628 if (block0
>= DOC_LAYOUT_BLOCK_FIRST_DATA
) {
629 if (eccconf1
& DOC_ECCCONF1_BCH_SYNDROM_ERR
)
631 if (is_prot_seq_error(docg3
))
634 doc_read_page_finish(docg3
);
639 doc_read_page_finish(docg3
);
645 * doc_read_oob - Read out of band bytes from flash
647 * @from: the offset from first block and first page, in bytes, aligned on page
649 * @ops: the mtd oob structure
651 * Reads flash memory OOB area of pages.
653 * Returns 0 if read successfull, of -EIO, -EINVAL if an error occured
655 static int doc_read_oob(struct mtd_info
*mtd
, loff_t from
,
656 struct mtd_oob_ops
*ops
)
658 struct docg3
*docg3
= mtd
->priv
;
659 int block0
, block1
, page
, ofs
, ret
;
660 u8
*buf
= ops
->oobbuf
;
661 size_t len
= ops
->ooblen
;
663 doc_dbg("doc_read_oob(from=%lld, buf=%p, len=%zu)\n", from
, buf
, len
);
664 if (len
!= DOC_LAYOUT_OOB_SIZE
)
668 case MTD_OPS_PLACE_OOB
:
675 calc_block_sector(from
, &block0
, &block1
, &page
, &ofs
);
676 if (block1
> docg3
->max_block
)
679 ret
= doc_read_page_prepare(docg3
, block0
, block1
, page
,
680 ofs
+ DOC_LAYOUT_PAGE_SIZE
);
682 ret
= doc_read_page_ecc_init(docg3
, DOC_LAYOUT_OOB_SIZE
);
684 ret
= doc_read_page_getbytes(docg3
, DOC_LAYOUT_OOB_SIZE
,
686 doc_read_page_finish(docg3
);
689 ops
->oobretlen
= ret
;
692 return (ret
> 0) ? 0 : ret
;
695 static int doc_reload_bbt(struct docg3
*docg3
)
697 int block
= DOC_LAYOUT_BLOCK_BBT
;
698 int ret
= 0, nbpages
, page
;
699 u_char
*buf
= docg3
->bbt
;
701 nbpages
= DIV_ROUND_UP(docg3
->max_block
+ 1, 8 * DOC_LAYOUT_PAGE_SIZE
);
702 for (page
= 0; !ret
&& (page
< nbpages
); page
++) {
703 ret
= doc_read_page_prepare(docg3
, block
, block
+ 1,
704 page
+ DOC_LAYOUT_PAGE_BBT
, 0);
706 ret
= doc_read_page_ecc_init(docg3
,
707 DOC_LAYOUT_PAGE_SIZE
);
709 doc_read_page_getbytes(docg3
, DOC_LAYOUT_PAGE_SIZE
,
711 buf
+= DOC_LAYOUT_PAGE_SIZE
;
713 doc_read_page_finish(docg3
);
718 * doc_block_isbad - Checks whether a block is good or not
720 * @from: the offset to find the correct block
722 * Returns 1 if block is bad, 0 if block is good
724 static int doc_block_isbad(struct mtd_info
*mtd
, loff_t from
)
726 struct docg3
*docg3
= mtd
->priv
;
727 int block0
, block1
, page
, ofs
, is_good
;
729 calc_block_sector(from
, &block0
, &block1
, &page
, &ofs
);
730 doc_dbg("doc_block_isbad(from=%lld) => block=(%d,%d), page=%d, ofs=%d\n",
731 from
, block0
, block1
, page
, ofs
);
733 if (block0
< DOC_LAYOUT_BLOCK_FIRST_DATA
)
735 if (block1
> docg3
->max_block
)
738 is_good
= docg3
->bbt
[block0
>> 3] & (1 << (block0
& 0x7));
743 * doc_get_erase_count - Get block erase count
745 * @from: the offset in which the block is.
747 * Get the number of times a block was erased. The number is the maximum of
748 * erase times between first and second plane (which should be equal normally).
750 * Returns The number of erases, or -EINVAL or -EIO on error.
752 static int doc_get_erase_count(struct docg3
*docg3
, loff_t from
)
754 u8 buf
[DOC_LAYOUT_WEAR_SIZE
];
755 int ret
, plane1_erase_count
, plane2_erase_count
;
756 int block0
, block1
, page
, ofs
;
758 doc_dbg("doc_get_erase_count(from=%lld, buf=%p)\n", from
, buf
);
759 if (from
% DOC_LAYOUT_PAGE_SIZE
)
761 calc_block_sector(from
, &block0
, &block1
, &page
, &ofs
);
762 if (block1
> docg3
->max_block
)
765 ret
= doc_reset_seq(docg3
);
767 ret
= doc_read_page_prepare(docg3
, block0
, block1
, page
,
768 ofs
+ DOC_LAYOUT_WEAR_OFFSET
);
770 ret
= doc_read_page_getbytes(docg3
, DOC_LAYOUT_WEAR_SIZE
,
772 doc_read_page_finish(docg3
);
774 if (ret
|| (buf
[0] != DOC_ERASE_MARK
) || (buf
[2] != DOC_ERASE_MARK
))
776 plane1_erase_count
= (u8
)(~buf
[1]) | ((u8
)(~buf
[4]) << 8)
777 | ((u8
)(~buf
[5]) << 16);
778 plane2_erase_count
= (u8
)(~buf
[3]) | ((u8
)(~buf
[6]) << 8)
779 | ((u8
)(~buf
[7]) << 16);
781 return max(plane1_erase_count
, plane2_erase_count
);
785 * Debug sysfs entries
787 static int dbg_flashctrl_show(struct seq_file
*s
, void *p
)
789 struct docg3
*docg3
= (struct docg3
*)s
->private;
792 u8 fctrl
= doc_register_readb(docg3
, DOC_FLASHCONTROL
);
795 "FlashControl : 0x%02x (%s,CE# %s,%s,%s,flash %s)\n",
797 fctrl
& DOC_CTRL_VIOLATION
? "protocol violation" : "-",
798 fctrl
& DOC_CTRL_CE
? "active" : "inactive",
799 fctrl
& DOC_CTRL_PROTECTION_ERROR
? "protection error" : "-",
800 fctrl
& DOC_CTRL_SEQUENCE_ERROR
? "sequence error" : "-",
801 fctrl
& DOC_CTRL_FLASHREADY
? "ready" : "not ready");
804 DEBUGFS_RO_ATTR(flashcontrol
, dbg_flashctrl_show
);
806 static int dbg_asicmode_show(struct seq_file
*s
, void *p
)
808 struct docg3
*docg3
= (struct docg3
*)s
->private;
811 int pctrl
= doc_register_readb(docg3
, DOC_ASICMODE
);
812 int mode
= pctrl
& 0x03;
815 "%04x : RAM_WE=%d,RSTIN_RESET=%d,BDETCT_RESET=%d,WRITE_ENABLE=%d,POWERDOWN=%d,MODE=%d%d (",
817 pctrl
& DOC_ASICMODE_RAM_WE
? 1 : 0,
818 pctrl
& DOC_ASICMODE_RSTIN_RESET
? 1 : 0,
819 pctrl
& DOC_ASICMODE_BDETCT_RESET
? 1 : 0,
820 pctrl
& DOC_ASICMODE_MDWREN
? 1 : 0,
821 pctrl
& DOC_ASICMODE_POWERDOWN
? 1 : 0,
822 mode
>> 1, mode
& 0x1);
825 case DOC_ASICMODE_RESET
:
826 pos
+= seq_printf(s
, "reset");
828 case DOC_ASICMODE_NORMAL
:
829 pos
+= seq_printf(s
, "normal");
831 case DOC_ASICMODE_POWERDOWN
:
832 pos
+= seq_printf(s
, "powerdown");
835 pos
+= seq_printf(s
, ")\n");
838 DEBUGFS_RO_ATTR(asic_mode
, dbg_asicmode_show
);
840 static int dbg_device_id_show(struct seq_file
*s
, void *p
)
842 struct docg3
*docg3
= (struct docg3
*)s
->private;
844 int id
= doc_register_readb(docg3
, DOC_DEVICESELECT
);
846 pos
+= seq_printf(s
, "DeviceId = %d\n", id
);
849 DEBUGFS_RO_ATTR(device_id
, dbg_device_id_show
);
851 static int dbg_protection_show(struct seq_file
*s
, void *p
)
853 struct docg3
*docg3
= (struct docg3
*)s
->private;
855 int protect
= doc_register_readb(docg3
, DOC_PROTECTION
);
856 int dps0
= doc_register_readb(docg3
, DOC_DPS0_STATUS
);
857 int dps0_low
= doc_register_readb(docg3
, DOC_DPS0_ADDRLOW
);
858 int dps0_high
= doc_register_readb(docg3
, DOC_DPS0_ADDRHIGH
);
859 int dps1
= doc_register_readb(docg3
, DOC_DPS1_STATUS
);
860 int dps1_low
= doc_register_readb(docg3
, DOC_DPS1_ADDRLOW
);
861 int dps1_high
= doc_register_readb(docg3
, DOC_DPS1_ADDRHIGH
);
863 pos
+= seq_printf(s
, "Protection = 0x%02x (",
865 if (protect
& DOC_PROTECT_FOUNDRY_OTP_LOCK
)
866 pos
+= seq_printf(s
, "FOUNDRY_OTP_LOCK,");
867 if (protect
& DOC_PROTECT_CUSTOMER_OTP_LOCK
)
868 pos
+= seq_printf(s
, "CUSTOMER_OTP_LOCK,");
869 if (protect
& DOC_PROTECT_LOCK_INPUT
)
870 pos
+= seq_printf(s
, "LOCK_INPUT,");
871 if (protect
& DOC_PROTECT_STICKY_LOCK
)
872 pos
+= seq_printf(s
, "STICKY_LOCK,");
873 if (protect
& DOC_PROTECT_PROTECTION_ENABLED
)
874 pos
+= seq_printf(s
, "PROTECTION ON,");
875 if (protect
& DOC_PROTECT_IPL_DOWNLOAD_LOCK
)
876 pos
+= seq_printf(s
, "IPL_DOWNLOAD_LOCK,");
877 if (protect
& DOC_PROTECT_PROTECTION_ERROR
)
878 pos
+= seq_printf(s
, "PROTECT_ERR,");
880 pos
+= seq_printf(s
, "NO_PROTECT_ERR");
881 pos
+= seq_printf(s
, ")\n");
883 pos
+= seq_printf(s
, "DPS0 = 0x%02x : "
884 "Protected area [0x%x - 0x%x] : OTP=%d, READ=%d, "
885 "WRITE=%d, HW_LOCK=%d, KEY_OK=%d\n",
886 dps0
, dps0_low
, dps0_high
,
887 !!(dps0
& DOC_DPS_OTP_PROTECTED
),
888 !!(dps0
& DOC_DPS_READ_PROTECTED
),
889 !!(dps0
& DOC_DPS_WRITE_PROTECTED
),
890 !!(dps0
& DOC_DPS_HW_LOCK_ENABLED
),
891 !!(dps0
& DOC_DPS_KEY_OK
));
892 pos
+= seq_printf(s
, "DPS1 = 0x%02x : "
893 "Protected area [0x%x - 0x%x] : OTP=%d, READ=%d, "
894 "WRITE=%d, HW_LOCK=%d, KEY_OK=%d\n",
895 dps1
, dps1_low
, dps1_high
,
896 !!(dps1
& DOC_DPS_OTP_PROTECTED
),
897 !!(dps1
& DOC_DPS_READ_PROTECTED
),
898 !!(dps1
& DOC_DPS_WRITE_PROTECTED
),
899 !!(dps1
& DOC_DPS_HW_LOCK_ENABLED
),
900 !!(dps1
& DOC_DPS_KEY_OK
));
903 DEBUGFS_RO_ATTR(protection
, dbg_protection_show
);
905 static int __init
doc_dbg_register(struct docg3
*docg3
)
907 struct dentry
*root
, *entry
;
909 root
= debugfs_create_dir("docg3", NULL
);
913 entry
= debugfs_create_file("flashcontrol", S_IRUSR
, root
, docg3
,
916 entry
= debugfs_create_file("asic_mode", S_IRUSR
, root
,
917 docg3
, &asic_mode_fops
);
919 entry
= debugfs_create_file("device_id", S_IRUSR
, root
,
920 docg3
, &device_id_fops
);
922 entry
= debugfs_create_file("protection", S_IRUSR
, root
,
923 docg3
, &protection_fops
);
925 docg3
->debugfs_root
= root
;
928 debugfs_remove_recursive(root
);
933 static void __exit
doc_dbg_unregister(struct docg3
*docg3
)
935 debugfs_remove_recursive(docg3
->debugfs_root
);
939 * doc_set_driver_info - Fill the mtd_info structure and docg3 structure
940 * @chip_id: The chip ID of the supported chip
941 * @mtd: The structure to fill
943 static void __init
doc_set_driver_info(int chip_id
, struct mtd_info
*mtd
)
945 struct docg3
*docg3
= mtd
->priv
;
948 cfg
= doc_register_readb(docg3
, DOC_CONFIGURATION
);
949 docg3
->if_cfg
= (cfg
& DOC_CONF_IF_CFG
? 1 : 0);
953 mtd
->name
= "DiskOnChip G3";
954 docg3
->max_block
= 2047;
957 mtd
->type
= MTD_NANDFLASH
;
959 * Once write methods are added, the correct flags will be set.
960 * mtd->flags = MTD_CAP_NANDFLASH;
962 mtd
->flags
= MTD_CAP_ROM
;
963 mtd
->size
= (docg3
->max_block
+ 1) * DOC_LAYOUT_BLOCK_SIZE
;
964 mtd
->erasesize
= DOC_LAYOUT_BLOCK_SIZE
* DOC_LAYOUT_NBPLANES
;
965 mtd
->writesize
= DOC_LAYOUT_PAGE_SIZE
;
966 mtd
->oobsize
= DOC_LAYOUT_OOB_SIZE
;
967 mtd
->owner
= THIS_MODULE
;
971 mtd
->read
= doc_read
;
973 mtd
->read_oob
= doc_read_oob
;
974 mtd
->write_oob
= NULL
;
976 mtd
->block_isbad
= doc_block_isbad
;
980 * doc_probe - Probe the IO space for a DiskOnChip G3 chip
981 * @pdev: platform device
983 * Probes for a G3 chip at the specified IO space in the platform data
986 * Returns 0 on success, -ENOMEM, -ENXIO on error
988 static int __init
docg3_probe(struct platform_device
*pdev
)
990 struct device
*dev
= &pdev
->dev
;
992 struct mtd_info
*mtd
;
993 struct resource
*ress
;
994 int ret
, bbt_nbpages
;
995 u16 chip_id
, chip_id_inv
;
998 docg3
= kzalloc(sizeof(struct docg3
), GFP_KERNEL
);
1001 mtd
= kzalloc(sizeof(struct mtd_info
), GFP_KERNEL
);
1007 ress
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1009 dev_err(dev
, "No I/O memory resource defined\n");
1012 docg3
->base
= ioremap(ress
->start
, DOC_IOSPACE_SIZE
);
1014 docg3
->dev
= &pdev
->dev
;
1015 docg3
->device_id
= 0;
1016 doc_set_device_id(docg3
, docg3
->device_id
);
1017 doc_set_asic_mode(docg3
, DOC_ASICMODE_RESET
);
1018 doc_set_asic_mode(docg3
, DOC_ASICMODE_NORMAL
);
1020 chip_id
= doc_register_readw(docg3
, DOC_CHIPID
);
1021 chip_id_inv
= doc_register_readw(docg3
, DOC_CHIPID_INV
);
1024 if (chip_id
!= (u16
)(~chip_id_inv
)) {
1025 doc_info("No device found at IO addr %p\n",
1026 (void *)ress
->start
);
1032 doc_info("Found a G3 DiskOnChip at addr %p\n",
1033 (void *)ress
->start
);
1036 doc_err("Chip id %04x is not a DiskOnChip G3 chip\n", chip_id
);
1040 doc_set_driver_info(chip_id
, mtd
);
1041 platform_set_drvdata(pdev
, mtd
);
1044 bbt_nbpages
= DIV_ROUND_UP(docg3
->max_block
+ 1,
1045 8 * DOC_LAYOUT_PAGE_SIZE
);
1046 docg3
->bbt
= kzalloc(bbt_nbpages
* DOC_LAYOUT_PAGE_SIZE
, GFP_KERNEL
);
1049 doc_reload_bbt(docg3
);
1051 ret
= mtd_device_parse_register(mtd
, part_probes
,
1054 goto register_error
;
1056 doc_dbg_register(docg3
);
1062 iounmap(docg3
->base
);
1072 * docg3_release - Release the driver
1073 * @pdev: the platform device
1077 static int __exit
docg3_release(struct platform_device
*pdev
)
1079 struct mtd_info
*mtd
= platform_get_drvdata(pdev
);
1080 struct docg3
*docg3
= mtd
->priv
;
1082 doc_dbg_unregister(docg3
);
1083 mtd_device_unregister(mtd
);
1084 iounmap(docg3
->base
);
1091 static struct platform_driver g3_driver
= {
1094 .owner
= THIS_MODULE
,
1096 .remove
= __exit_p(docg3_release
),
1099 static int __init
docg3_init(void)
1101 return platform_driver_probe(&g3_driver
, docg3_probe
);
1103 module_init(docg3_init
);
1106 static void __exit
docg3_exit(void)
1108 platform_driver_unregister(&g3_driver
);
1110 module_exit(docg3_exit
);
1112 MODULE_LICENSE("GPL");
1113 MODULE_AUTHOR("Robert Jarzmik <robert.jarzmik@free.fr>");
1114 MODULE_DESCRIPTION("MTD driver for DiskOnChip G3");