2 * drivers/mtd/nand/docg4.c
4 * Copyright (C) 2013 Mike Dunn <mikedunn@newsguy.com>
6 * This file is released under the terms of GPL v2 and any later version.
7 * See the file COPYING in the root directory of the source tree for details.
9 * mtd nand driver for M-Systems DiskOnChip G4
11 * Tested on the Palm Treo 680. The G4 is also present on Toshiba Portege, Asus
12 * P526, some HTC smartphones (Wizard, Prophet, ...), O2 XDA Zinc, maybe others.
13 * Should work on these as well. Let me know!
17 * Mechanism for management of password-protected areas
19 * Hamming ecc when reading oob only
21 * According to the M-Sys documentation, this device is also available in a
22 * "dual-die" configuration having a 256MB capacity, but no mechanism for
23 * detecting this variant is documented. Currently this driver assumes 128MB
26 * Support for multiple cascaded devices ("floors"). Not sure which gadgets
27 * contain multiple G4s in a cascaded configuration, if any.
33 #include <asm/arch/hardware.h>
35 #include <asm/bitops.h>
36 #include <asm/errno.h>
39 #include <linux/bch.h>
40 #include <linux/bitrev.h>
41 #include <linux/mtd/docg4.h>
44 * The device has a nop register which M-Sys claims is for the purpose of
45 * inserting precise delays. But beware; at least some operations fail if the
46 * nop writes are replaced with a generic delay!
48 static inline void write_nop(void __iomem
*docptr
)
50 writew(0, docptr
+ DOC_NOP
);
54 static int poll_status(void __iomem
*docptr
)
57 * Busy-wait for the FLASHREADY bit to be set in the FLASHCONTROL
58 * register. Operations known to take a long time (e.g., block erase)
59 * should sleep for a while before calling this.
64 /* hardware quirk requires reading twice initially */
65 flash_status
= readb(docptr
+ DOC_FLASHCONTROL
);
68 flash_status
= readb(docptr
+ DOC_FLASHCONTROL
);
69 } while (!(flash_status
& DOC_CTRL_FLASHREADY
));
74 static void write_addr(void __iomem
*docptr
, uint32_t docg4_addr
)
76 /* write the four address bytes packed in docg4_addr to the device */
78 writeb(docg4_addr
& 0xff, docptr
+ DOC_FLASHADDRESS
);
80 writeb(docg4_addr
& 0xff, docptr
+ DOC_FLASHADDRESS
);
82 writeb(docg4_addr
& 0xff, docptr
+ DOC_FLASHADDRESS
);
84 writeb(docg4_addr
& 0xff, docptr
+ DOC_FLASHADDRESS
);
88 * This is a module parameter in the linux kernel version of this driver. It is
89 * hard-coded to 'off' for u-boot. This driver uses oob to mark bad blocks.
90 * This can be problematic when dealing with data not intended for the mtd/nand
91 * subsystem. For example, on boards that boot from the docg4 and use the IPL
92 * to load an spl + u-boot image, the blocks containing the image will be
93 * reported as "bad" because the oob of the first page of each block contains a
94 * magic number that the IPL looks for, which causes the badblock scan to
95 * erroneously add them to the bad block table. To erase such a block, use
96 * u-boot's 'nand scrub'. scrub is safe for the docg4. The device does have a
97 * factory bad block table, but it is read-only, and is used in conjunction with
98 * oob bad block markers that are written by mtd/nand when a block is deemed to
99 * be bad. To read data from "bad" blocks, use 'read.raw'. Unfortunately,
100 * read.raw does not use ecc, which would still work fine on such misidentified
101 * bad blocks. TODO: u-boot nand utilities need the ability to ignore bad
104 static const int ignore_badblocks
; /* remains false */
109 unsigned int command
;
116 struct bch_control
*bch
;
119 * Oob bytes 0 - 6 are available to the user.
120 * Byte 7 is hamming ecc for first 7 bytes. Bytes 8 - 14 are hw-generated ecc.
121 * Byte 15 (the last) is used by the driver as a "page written" flag.
123 static struct nand_ecclayout docg4_oobinfo
= {
125 .eccpos
= {7, 8, 9, 10, 11, 12, 13, 14, 15},
127 .oobfree
= { {0, 7} }
130 static void reset(void __iomem
*docptr
)
132 /* full device reset */
134 writew(DOC_ASICMODE_RESET
| DOC_ASICMODE_MDWREN
, docptr
+ DOC_ASICMODE
);
135 writew(~(DOC_ASICMODE_RESET
| DOC_ASICMODE_MDWREN
),
136 docptr
+ DOC_ASICMODECONFIRM
);
139 writew(DOC_ASICMODE_NORMAL
| DOC_ASICMODE_MDWREN
,
140 docptr
+ DOC_ASICMODE
);
141 writew(~(DOC_ASICMODE_NORMAL
| DOC_ASICMODE_MDWREN
),
142 docptr
+ DOC_ASICMODECONFIRM
);
144 writew(DOC_ECCCONF1_ECC_ENABLE
, docptr
+ DOC_ECCCONF1
);
149 static void docg4_select_chip(struct mtd_info
*mtd
, int chip
)
152 * Select among multiple cascaded chips ("floors"). Multiple floors are
153 * not yet supported, so the only valid non-negative value is 0.
155 void __iomem
*docptr
= CONFIG_SYS_NAND_BASE
;
158 return; /* deselected */
161 printf("multiple floors currently unsupported\n");
163 writew(0, docptr
+ DOC_DEVICESELECT
);
166 static void read_hw_ecc(void __iomem
*docptr
, uint8_t *ecc_buf
)
168 /* read the 7 hw-generated ecc bytes */
171 for (i
= 0; i
< 7; i
++) { /* hw quirk; read twice */
172 ecc_buf
[i
] = readb(docptr
+ DOC_BCH_SYNDROM(i
));
173 ecc_buf
[i
] = readb(docptr
+ DOC_BCH_SYNDROM(i
));
177 static int correct_data(struct mtd_info
*mtd
, uint8_t *buf
, int page
)
180 * Called after a page read when hardware reports bitflips.
181 * Up to four bitflips can be corrected.
184 struct nand_chip
*nand
= mtd
->priv
;
185 struct docg4_priv
*doc
= nand
->priv
;
186 void __iomem
*docptr
= CONFIG_SYS_NAND_BASE
;
188 unsigned int errpos
[4];
189 const uint8_t blank_read_hwecc
[8] = {
190 0xcf, 0x72, 0xfc, 0x1b, 0xa9, 0xc7, 0xb9, 0 };
192 read_hw_ecc(docptr
, doc
->ecc_buf
); /* read 7 hw-generated ecc bytes */
194 /* check if read error is due to a blank page */
195 if (!memcmp(doc
->ecc_buf
, blank_read_hwecc
, 7))
198 /* skip additional check of "written flag" if ignore_badblocks */
199 if (!ignore_badblocks
) {
201 * If the hw ecc bytes are not those of a blank page, there's
202 * still a chance that the page is blank, but was read with
203 * errors. Check the "written flag" in last oob byte, which
204 * is set to zero when a page is written. If more than half
205 * the bits are set, assume a blank page. Unfortunately, the
206 * bit flips(s) are not reported in stats.
209 if (doc
->oob_buf
[15]) {
210 int bit
, numsetbits
= 0;
211 unsigned long written_flag
= doc
->oob_buf
[15];
213 for (bit
= 0; bit
< 8; bit
++) {
214 if (written_flag
& 0x01)
218 if (numsetbits
> 4) { /* assume blank */
219 printf("errors in blank page at offset %08x\n",
220 page
* DOCG4_PAGE_SIZE
);
227 * The hardware ecc unit produces oob_ecc ^ calc_ecc. The kernel's bch
228 * algorithm is used to decode this. However the hw operates on page
229 * data in a bit order that is the reverse of that of the bch alg,
230 * requiring that the bits be reversed on the result. Thanks to Ivan
231 * Djelic for his analysis!
233 for (i
= 0; i
< 7; i
++)
234 doc
->ecc_buf
[i
] = bitrev8(doc
->ecc_buf
[i
]);
236 numerrs
= decode_bch(doc
->bch
, NULL
, DOCG4_USERDATA_LEN
, NULL
,
237 doc
->ecc_buf
, NULL
, errpos
);
239 if (numerrs
== -EBADMSG
) {
240 printf("uncorrectable errors at offset %08x\n",
241 page
* DOCG4_PAGE_SIZE
);
245 BUG_ON(numerrs
< 0); /* -EINVAL, or anything other than -EBADMSG */
247 /* undo last step in BCH alg (modulo mirroring not needed) */
248 for (i
= 0; i
< numerrs
; i
++)
249 errpos
[i
] = (errpos
[i
] & ~7)|(7-(errpos
[i
] & 7));
252 for (i
= 0; i
< numerrs
; i
++) {
253 /* ignore if error within oob ecc bytes */
254 if (errpos
[i
] > DOCG4_USERDATA_LEN
* 8)
257 /* if error within oob area preceeding ecc bytes... */
258 if (errpos
[i
] > DOCG4_PAGE_SIZE
* 8)
259 __change_bit(errpos
[i
] - DOCG4_PAGE_SIZE
* 8,
260 (unsigned long *)doc
->oob_buf
);
262 else /* error in page data */
263 __change_bit(errpos
[i
], (unsigned long *)buf
);
266 printf("%d error(s) corrected at offset %08x\n",
267 numerrs
, page
* DOCG4_PAGE_SIZE
);
272 static int read_progstatus(struct docg4_priv
*doc
, void __iomem
*docptr
)
275 * This apparently checks the status of programming. Done after an
276 * erasure, and after page data is written. On error, the status is
277 * saved, to be later retrieved by the nand infrastructure code.
280 /* status is read from the I/O reg */
281 uint16_t status1
= readw(docptr
+ DOC_IOSPACE_DATA
);
282 uint16_t status2
= readw(docptr
+ DOC_IOSPACE_DATA
);
283 uint16_t status3
= readw(docptr
+ DOCG4_MYSTERY_REG
);
285 MTDDEBUG(MTD_DEBUG_LEVEL3
, "docg4: %s: %02x %02x %02x\n",
286 __func__
, status1
, status2
, status3
);
288 if (status1
!= DOCG4_PROGSTATUS_GOOD
||
289 status2
!= DOCG4_PROGSTATUS_GOOD_2
||
290 status3
!= DOCG4_PROGSTATUS_GOOD_2
) {
291 doc
->status
= NAND_STATUS_FAIL
;
292 printf("read_progstatus failed: %02x, %02x, %02x\n",
293 status1
, status2
, status3
);
299 static int pageprog(struct mtd_info
*mtd
)
302 * Final step in writing a page. Writes the contents of its
303 * internal buffer out to the flash array, or some such.
306 struct nand_chip
*nand
= mtd
->priv
;
307 struct docg4_priv
*doc
= nand
->priv
;
308 void __iomem
*docptr
= CONFIG_SYS_NAND_BASE
;
311 MTDDEBUG(MTD_DEBUG_LEVEL3
, "docg4: %s\n", __func__
);
313 writew(DOCG4_SEQ_PAGEPROG
, docptr
+ DOC_FLASHSEQUENCE
);
314 writew(DOC_CMD_PROG_CYCLE2
, docptr
+ DOC_FLASHCOMMAND
);
318 /* Just busy-wait; usleep_range() slows things down noticeably. */
321 writew(DOCG4_SEQ_FLUSH
, docptr
+ DOC_FLASHSEQUENCE
);
322 writew(DOCG4_CMD_FLUSH
, docptr
+ DOC_FLASHCOMMAND
);
323 writew(DOC_ECCCONF0_READ_MODE
| 4, docptr
+ DOC_ECCCONF0
);
330 retval
= read_progstatus(doc
, docptr
);
331 writew(0, docptr
+ DOC_DATAEND
);
339 static void sequence_reset(void __iomem
*docptr
)
341 /* common starting sequence for all operations */
343 writew(DOC_CTRL_UNKNOWN
| DOC_CTRL_CE
, docptr
+ DOC_FLASHCONTROL
);
344 writew(DOC_SEQ_RESET
, docptr
+ DOC_FLASHSEQUENCE
);
345 writew(DOC_CMD_RESET
, docptr
+ DOC_FLASHCOMMAND
);
352 static void read_page_prologue(void __iomem
*docptr
, uint32_t docg4_addr
)
354 /* first step in reading a page */
356 sequence_reset(docptr
);
358 writew(DOCG4_SEQ_PAGE_READ
, docptr
+ DOC_FLASHSEQUENCE
);
359 writew(DOCG4_CMD_PAGE_READ
, docptr
+ DOC_FLASHCOMMAND
);
362 write_addr(docptr
, docg4_addr
);
365 writew(DOCG4_CMD_READ2
, docptr
+ DOC_FLASHCOMMAND
);
372 static void write_page_prologue(void __iomem
*docptr
, uint32_t docg4_addr
)
374 /* first step in writing a page */
376 sequence_reset(docptr
);
377 writew(DOCG4_SEQ_PAGEWRITE
, docptr
+ DOC_FLASHSEQUENCE
);
378 writew(DOCG4_CMD_PAGEWRITE
, docptr
+ DOC_FLASHCOMMAND
);
380 write_addr(docptr
, docg4_addr
);
386 static uint32_t mtd_to_docg4_address(int page
, int column
)
389 * Convert mtd address to format used by the device, 32 bit packed.
391 * Some notes on G4 addressing... The M-Sys documentation on this device
392 * claims that pages are 2K in length, and indeed, the format of the
393 * address used by the device reflects that. But within each page are
394 * four 512 byte "sub-pages", each with its own oob data that is
395 * read/written immediately after the 512 bytes of page data. This oob
396 * data contains the ecc bytes for the preceeding 512 bytes.
398 * Rather than tell the mtd nand infrastructure that page size is 2k,
399 * with four sub-pages each, we engage in a little subterfuge and tell
400 * the infrastructure code that pages are 512 bytes in size. This is
401 * done because during the course of reverse-engineering the device, I
402 * never observed an instance where an entire 2K "page" was read or
403 * written as a unit. Each "sub-page" is always addressed individually,
404 * its data read/written, and ecc handled before the next "sub-page" is
407 * This requires us to convert addresses passed by the mtd nand
408 * infrastructure code to those used by the device.
410 * The address that is written to the device consists of four bytes: the
411 * first two are the 2k page number, and the second is the index into
412 * the page. The index is in terms of 16-bit half-words and includes
413 * the preceeding oob data, so e.g., the index into the second
414 * "sub-page" is 0x108, and the full device address of the start of mtd
415 * page 0x201 is 0x00800108.
417 int g4_page
= page
/ 4; /* device's 2K page */
418 int g4_index
= (page
% 4) * 0x108 + column
/2; /* offset into page */
419 return (g4_page
<< 16) | g4_index
; /* pack */
422 static void docg4_command(struct mtd_info
*mtd
, unsigned command
, int column
,
425 /* handle standard nand commands */
427 struct nand_chip
*nand
= mtd
->priv
;
428 struct docg4_priv
*doc
= nand
->priv
;
429 uint32_t g4_addr
= mtd_to_docg4_address(page_addr
, column
);
431 MTDDEBUG(MTD_DEBUG_LEVEL3
, "%s %x, page_addr=%x, column=%x\n",
432 __func__
, command
, page_addr
, column
);
435 * Save the command and its arguments. This enables emulation of
436 * standard flash devices, and also some optimizations.
438 doc
->last_command
.command
= command
;
439 doc
->last_command
.column
= column
;
440 doc
->last_command
.page
= page_addr
;
444 reset(CONFIG_SYS_NAND_BASE
);
448 read_page_prologue(CONFIG_SYS_NAND_BASE
, g4_addr
);
451 case NAND_CMD_STATUS
:
452 /* next call to read_byte() will expect a status */
456 write_page_prologue(CONFIG_SYS_NAND_BASE
, g4_addr
);
458 /* hack for deferred write of oob bytes */
459 if (doc
->oob_page
== page_addr
)
460 memcpy(nand
->oob_poi
, doc
->oob_buf
, 16);
463 case NAND_CMD_PAGEPROG
:
467 /* we don't expect these, based on review of nand_base.c */
468 case NAND_CMD_READOOB
:
469 case NAND_CMD_READID
:
470 case NAND_CMD_ERASE1
:
471 case NAND_CMD_ERASE2
:
472 printf("docg4_command: unexpected nand command 0x%x\n",
478 static void docg4_read_buf(struct mtd_info
*mtd
, uint8_t *buf
, int len
)
481 struct nand_chip
*nand
= mtd
->priv
;
482 uint16_t *p
= (uint16_t *)buf
;
485 for (i
= 0; i
< len
; i
++)
486 p
[i
] = readw(nand
->IO_ADDR_R
);
489 static int docg4_read_oob(struct mtd_info
*mtd
, struct nand_chip
*nand
,
490 int page
, int sndcmd
)
492 struct docg4_priv
*doc
= nand
->priv
;
493 void __iomem
*docptr
= CONFIG_SYS_NAND_BASE
;
496 MTDDEBUG(MTD_DEBUG_LEVEL3
, "%s: page %x\n", __func__
, page
);
499 * Oob bytes are read as part of a normal page read. If the previous
500 * nand command was a read of the page whose oob is now being read, just
501 * copy the oob bytes that we saved in a local buffer and avoid a
504 if (doc
->last_command
.command
== NAND_CMD_READ0
&&
505 doc
->last_command
.page
== page
) {
506 memcpy(nand
->oob_poi
, doc
->oob_buf
, 16);
511 * Separate read of oob data only.
513 docg4_command(mtd
, NAND_CMD_READ0
, nand
->ecc
.size
, page
);
515 writew(DOC_ECCCONF0_READ_MODE
| DOCG4_OOB_SIZE
, docptr
+ DOC_ECCCONF0
);
522 /* the 1st byte from the I/O reg is a status; the rest is oob data */
523 status
= readw(docptr
+ DOC_IOSPACE_DATA
);
524 if (status
& DOCG4_READ_ERROR
) {
525 printf("docg4_read_oob failed: status = 0x%02x\n", status
);
529 MTDDEBUG(MTD_DEBUG_LEVEL3
, "%s: status = 0x%x\n", __func__
, status
);
531 docg4_read_buf(mtd
, nand
->oob_poi
, 16);
536 writew(0, docptr
+ DOC_DATAEND
);
542 static int docg4_write_oob(struct mtd_info
*mtd
, struct nand_chip
*nand
,
546 * Writing oob-only is not really supported, because MLC nand must write
547 * oob bytes at the same time as page data. Nonetheless, we save the
548 * oob buffer contents here, and then write it along with the page data
549 * if the same page is subsequently written. This allows user space
550 * utilities that write the oob data prior to the page data to work
551 * (e.g., nandwrite). The disdvantage is that, if the intention was to
552 * write oob only, the operation is quietly ignored. Also, oob can get
553 * corrupted if two concurrent processes are running nandwrite.
556 /* note that bytes 7..14 are hw generated hamming/ecc and overwritten */
557 struct docg4_priv
*doc
= nand
->priv
;
558 doc
->oob_page
= page
;
559 memcpy(doc
->oob_buf
, nand
->oob_poi
, 16);
563 static int docg4_block_neverbad(struct mtd_info
*mtd
, loff_t ofs
, int getchip
)
565 /* only called when module_param ignore_badblocks is set */
569 static void docg4_write_buf16(struct mtd_info
*mtd
, const uint8_t *buf
, int len
)
572 struct nand_chip
*nand
= mtd
->priv
;
573 uint16_t *p
= (uint16_t *)buf
;
576 for (i
= 0; i
< len
; i
++)
577 writew(p
[i
], nand
->IO_ADDR_W
);
580 static void write_page(struct mtd_info
*mtd
, struct nand_chip
*nand
,
581 const uint8_t *buf
, int use_ecc
)
583 void __iomem
*docptr
= CONFIG_SYS_NAND_BASE
;
586 writew(DOC_ECCCONF0_ECC_ENABLE
|
587 DOC_ECCCONF0_UNKNOWN
|
589 docptr
+ DOC_ECCCONF0
);
592 /* write the page data */
593 docg4_write_buf16(mtd
, buf
, DOCG4_PAGE_SIZE
);
595 /* oob bytes 0 through 5 are written to I/O reg */
596 docg4_write_buf16(mtd
, nand
->oob_poi
, 6);
598 /* oob byte 6 written to a separate reg */
599 writew(nand
->oob_poi
[6], docptr
+ DOCG4_OOB_6_7
);
604 /* write hw-generated ecc bytes to oob */
605 if (likely(use_ecc
)) {
606 /* oob byte 7 is hamming code */
607 uint8_t hamming
= readb(docptr
+ DOC_HAMMINGPARITY
);
608 hamming
= readb(docptr
+ DOC_HAMMINGPARITY
); /* 2nd read */
609 writew(hamming
, docptr
+ DOCG4_OOB_6_7
);
612 /* read the 7 bch bytes from ecc regs */
613 read_hw_ecc(docptr
, ecc_buf
);
614 ecc_buf
[7] = 0; /* clear the "page written" flag */
617 /* write user-supplied bytes to oob */
619 writew(nand
->oob_poi
[7], docptr
+ DOCG4_OOB_6_7
);
621 memcpy(ecc_buf
, &nand
->oob_poi
[8], 8);
624 docg4_write_buf16(mtd
, ecc_buf
, 8);
627 writew(0, docptr
+ DOC_DATAEND
);
631 static void docg4_write_page_raw(struct mtd_info
*mtd
, struct nand_chip
*nand
,
634 return write_page(mtd
, nand
, buf
, 0);
637 static void docg4_write_page(struct mtd_info
*mtd
, struct nand_chip
*nand
,
640 return write_page(mtd
, nand
, buf
, 1);
643 static int read_page(struct mtd_info
*mtd
, struct nand_chip
*nand
,
644 uint8_t *buf
, int page
, int use_ecc
)
646 struct docg4_priv
*doc
= nand
->priv
;
647 void __iomem
*docptr
= CONFIG_SYS_NAND_BASE
;
648 uint16_t status
, edc_err
, *buf16
;
650 writew(DOC_ECCCONF0_READ_MODE
|
651 DOC_ECCCONF0_ECC_ENABLE
|
652 DOC_ECCCONF0_UNKNOWN
|
654 docptr
+ DOC_ECCCONF0
);
661 /* the 1st byte from the I/O reg is a status; the rest is page data */
662 status
= readw(docptr
+ DOC_IOSPACE_DATA
);
663 if (status
& DOCG4_READ_ERROR
) {
664 printf("docg4_read_page: bad status: 0x%02x\n", status
);
665 writew(0, docptr
+ DOC_DATAEND
);
669 docg4_read_buf(mtd
, buf
, DOCG4_PAGE_SIZE
); /* read the page data */
671 /* first 14 oob bytes read from I/O reg */
672 docg4_read_buf(mtd
, nand
->oob_poi
, 14);
674 /* last 2 read from another reg */
675 buf16
= (uint16_t *)(nand
->oob_poi
+ 14);
676 *buf16
= readw(docptr
+ DOCG4_MYSTERY_REG
);
679 * Diskonchips read oob immediately after a page read. Mtd
680 * infrastructure issues a separate command for reading oob after the
681 * page is read. So we save the oob bytes in a local buffer and just
682 * copy it if the next command reads oob from the same page.
684 memcpy(doc
->oob_buf
, nand
->oob_poi
, 16);
688 if (likely(use_ecc
)) {
689 /* read the register that tells us if bitflip(s) detected */
690 edc_err
= readw(docptr
+ DOC_ECCCONF1
);
691 edc_err
= readw(docptr
+ DOC_ECCCONF1
);
693 /* If bitflips are reported, attempt to correct with ecc */
694 if (edc_err
& DOC_ECCCONF1_BCH_SYNDROM_ERR
) {
695 int bits_corrected
= correct_data(mtd
, buf
, page
);
696 if (bits_corrected
== -EBADMSG
)
697 mtd
->ecc_stats
.failed
++;
699 mtd
->ecc_stats
.corrected
+= bits_corrected
;
703 writew(0, docptr
+ DOC_DATAEND
);
708 static int docg4_read_page_raw(struct mtd_info
*mtd
, struct nand_chip
*nand
,
709 uint8_t *buf
, int page
)
711 return read_page(mtd
, nand
, buf
, page
, 0);
714 static int docg4_read_page(struct mtd_info
*mtd
, struct nand_chip
*nand
,
715 uint8_t *buf
, int page
)
717 return read_page(mtd
, nand
, buf
, page
, 1);
720 static void docg4_erase_block(struct mtd_info
*mtd
, int page
)
722 struct nand_chip
*nand
= mtd
->priv
;
723 struct docg4_priv
*doc
= nand
->priv
;
724 void __iomem
*docptr
= CONFIG_SYS_NAND_BASE
;
727 MTDDEBUG(MTD_DEBUG_LEVEL3
, "%s: page %04x\n", __func__
, page
);
729 sequence_reset(docptr
);
731 writew(DOCG4_SEQ_BLOCKERASE
, docptr
+ DOC_FLASHSEQUENCE
);
732 writew(DOC_CMD_PROG_BLOCK_ADDR
, docptr
+ DOC_FLASHCOMMAND
);
735 /* only 2 bytes of address are written to specify erase block */
736 g4_page
= (uint16_t)(page
/ 4); /* to g4's 2k page addressing */
737 writeb(g4_page
& 0xff, docptr
+ DOC_FLASHADDRESS
);
739 writeb(g4_page
& 0xff, docptr
+ DOC_FLASHADDRESS
);
742 /* start the erasure */
743 writew(DOC_CMD_ERASECYCLE2
, docptr
+ DOC_FLASHCOMMAND
);
748 writew(DOCG4_SEQ_FLUSH
, docptr
+ DOC_FLASHSEQUENCE
);
749 writew(DOCG4_CMD_FLUSH
, docptr
+ DOC_FLASHCOMMAND
);
750 writew(DOC_ECCCONF0_READ_MODE
| 4, docptr
+ DOC_ECCCONF0
);
757 read_progstatus(doc
, docptr
);
759 writew(0, docptr
+ DOC_DATAEND
);
765 static int read_factory_bbt(struct mtd_info
*mtd
)
768 * The device contains a read-only factory bad block table. Read it and
769 * update the memory-based bbt accordingly.
772 struct nand_chip
*nand
= mtd
->priv
;
773 uint32_t g4_addr
= mtd_to_docg4_address(DOCG4_FACTORY_BBT_PAGE
, 0);
775 int i
, block
, status
;
777 buf
= kzalloc(DOCG4_PAGE_SIZE
, GFP_KERNEL
);
781 read_page_prologue(CONFIG_SYS_NAND_BASE
, g4_addr
);
782 status
= docg4_read_page(mtd
, nand
, buf
, DOCG4_FACTORY_BBT_PAGE
);
787 * If no memory-based bbt was created, exit. This will happen if module
788 * parameter ignore_badblocks is set. Then why even call this function?
789 * For an unknown reason, block erase always fails if it's the first
790 * operation after device power-up. The above read ensures it never is.
793 if (nand
->bbt
== NULL
) /* no memory-based bbt */
797 * Parse factory bbt and update memory-based bbt. Factory bbt format is
798 * simple: one bit per block, block numbers increase left to right (msb
799 * to lsb). Bit clear means bad block.
801 for (i
= block
= 0; block
< DOCG4_NUMBLOCKS
; block
+= 8, i
++) {
804 for (bitnum
= 0, mask
= 0x80;
805 bitnum
< 8; bitnum
++, mask
>>= 1) {
806 if (!(buf
[i
] & mask
)) {
807 int badblock
= block
+ bitnum
;
808 nand
->bbt
[badblock
/ 4] |=
809 0x03 << ((badblock
% 4) * 2);
810 mtd
->ecc_stats
.badblocks
++;
811 printf("factory-marked bad block: %d\n",
821 static int docg4_block_markbad(struct mtd_info
*mtd
, loff_t ofs
)
824 * Mark a block as bad. Bad blocks are marked in the oob area of the
825 * first page of the block. The default scan_bbt() in the nand
826 * infrastructure code works fine for building the memory-based bbt
827 * during initialization, as does the nand infrastructure function that
828 * checks if a block is bad by reading the bbt. This function replaces
829 * the nand default because writes to oob-only are not supported.
834 struct nand_chip
*nand
= mtd
->priv
;
835 struct nand_bbt_descr
*bbtd
= nand
->badblock_pattern
;
836 int block
= (int)(ofs
>> nand
->bbt_erase_shift
);
837 int page
= (int)(ofs
>> nand
->page_shift
);
838 uint32_t g4_addr
= mtd_to_docg4_address(page
, 0);
840 MTDDEBUG(MTD_DEBUG_LEVEL3
, "%s: %08llx\n", __func__
, ofs
);
842 if (unlikely(ofs
& (DOCG4_BLOCK_SIZE
- 1)))
843 printf("%s: ofs %llx not start of block!\n",
846 /* allocate blank buffer for page data */
847 buf
= kzalloc(DOCG4_PAGE_SIZE
, GFP_KERNEL
);
851 /* update bbt in memory */
852 nand
->bbt
[block
/ 4] |= 0x01 << ((block
& 0x03) * 2);
854 /* write bit-wise negation of pattern to oob buffer */
855 memset(nand
->oob_poi
, 0xff, mtd
->oobsize
);
856 for (i
= 0; i
< bbtd
->len
; i
++)
857 nand
->oob_poi
[bbtd
->offs
+ i
] = ~bbtd
->pattern
[i
];
859 /* write first page of block */
860 write_page_prologue(CONFIG_SYS_NAND_BASE
, g4_addr
);
861 docg4_write_page(mtd
, nand
, buf
);
864 mtd
->ecc_stats
.badblocks
++;
871 static uint8_t docg4_read_byte(struct mtd_info
*mtd
)
873 struct nand_chip
*nand
= mtd
->priv
;
874 struct docg4_priv
*doc
= nand
->priv
;
876 MTDDEBUG(MTD_DEBUG_LEVEL3
, "%s\n", __func__
);
878 if (doc
->last_command
.command
== NAND_CMD_STATUS
) {
882 * Previous nand command was status request, so nand
883 * infrastructure code expects to read the status here. If an
884 * error occurred in a previous operation, report it.
886 doc
->last_command
.command
= 0;
889 status
= doc
->status
;
893 /* why is NAND_STATUS_WP inverse logic?? */
895 status
= NAND_STATUS_WP
| NAND_STATUS_READY
;
900 printf("unexpectd call to read_byte()\n");
905 static int docg4_wait(struct mtd_info
*mtd
, struct nand_chip
*nand
)
907 struct docg4_priv
*doc
= nand
->priv
;
908 int status
= NAND_STATUS_WP
; /* inverse logic?? */
909 MTDDEBUG(MTD_DEBUG_LEVEL3
, "%s...\n", __func__
);
911 /* report any previously unreported error */
913 status
|= doc
->status
;
918 status
|= poll_status(CONFIG_SYS_NAND_BASE
);
922 int docg4_nand_init(struct mtd_info
*mtd
, struct nand_chip
*nand
, int devnum
)
925 struct docg4_priv
*docg4
;
928 docg4
= kzalloc(sizeof(*docg4
), GFP_KERNEL
);
935 /* These must be initialized here because the docg4 is non-standard
936 * and doesn't produce an id that the nand code can use to look up
937 * these values (nand_scan_ident() not called).
939 mtd
->size
= DOCG4_CHIP_SIZE
;
940 mtd
->name
= "Msys_Diskonchip_G4";
941 mtd
->writesize
= DOCG4_PAGE_SIZE
;
942 mtd
->erasesize
= DOCG4_BLOCK_SIZE
;
943 mtd
->oobsize
= DOCG4_OOB_SIZE
;
946 (void __iomem
*)CONFIG_SYS_NAND_BASE
+ DOC_IOSPACE_DATA
;
947 nand
->IO_ADDR_W
= nand
->IO_ADDR_R
;
948 nand
->chipsize
= DOCG4_CHIP_SIZE
;
949 nand
->chip_shift
= DOCG4_CHIP_SHIFT
;
950 nand
->bbt_erase_shift
= DOCG4_ERASE_SHIFT
;
951 nand
->phys_erase_shift
= DOCG4_ERASE_SHIFT
;
952 nand
->chip_delay
= 20;
953 nand
->page_shift
= DOCG4_PAGE_SHIFT
;
954 nand
->pagemask
= 0x3ffff;
955 nand
->badblockpos
= NAND_LARGE_BADBLOCK_POS
;
956 nand
->badblockbits
= 8;
957 nand
->ecc
.layout
= &docg4_oobinfo
;
958 nand
->ecc
.mode
= NAND_ECC_HW_SYNDROME
;
959 nand
->ecc
.size
= DOCG4_PAGE_SIZE
;
960 nand
->ecc
.prepad
= 8;
963 NAND_BUSWIDTH_16
| NAND_NO_SUBPAGE_WRITE
| NAND_NO_AUTOINCR
;
964 nand
->controller
= &nand
->hwcontrol
;
967 nand
->cmdfunc
= docg4_command
;
968 nand
->waitfunc
= docg4_wait
;
969 nand
->select_chip
= docg4_select_chip
;
970 nand
->read_byte
= docg4_read_byte
;
971 nand
->block_markbad
= docg4_block_markbad
;
972 nand
->read_buf
= docg4_read_buf
;
973 nand
->write_buf
= docg4_write_buf16
;
974 nand
->scan_bbt
= nand_default_bbt
;
975 nand
->erase_cmd
= docg4_erase_block
;
976 nand
->ecc
.read_page
= docg4_read_page
;
977 nand
->ecc
.write_page
= docg4_write_page
;
978 nand
->ecc
.read_page_raw
= docg4_read_page_raw
;
979 nand
->ecc
.write_page_raw
= docg4_write_page_raw
;
980 nand
->ecc
.read_oob
= docg4_read_oob
;
981 nand
->ecc
.write_oob
= docg4_write_oob
;
984 * The way the nand infrastructure code is written, a memory-based bbt
985 * is not created if NAND_SKIP_BBTSCAN is set. With no memory bbt,
986 * nand->block_bad() is used. So when ignoring bad blocks, we skip the
987 * scan and define a dummy block_bad() which always returns 0.
989 if (ignore_badblocks
) {
990 nand
->options
|= NAND_SKIP_BBTSCAN
;
991 nand
->block_bad
= docg4_block_neverbad
;
994 reset(CONFIG_SYS_NAND_BASE
);
996 /* check for presence of g4 chip by reading id registers */
997 id1
= readw(CONFIG_SYS_NAND_BASE
+ DOC_CHIPID
);
998 id1
= readw(CONFIG_SYS_NAND_BASE
+ DOCG4_MYSTERY_REG
);
999 id2
= readw(CONFIG_SYS_NAND_BASE
+ DOC_CHIPID_INV
);
1000 id2
= readw(CONFIG_SYS_NAND_BASE
+ DOCG4_MYSTERY_REG
);
1001 if (id1
!= DOCG4_IDREG1_VALUE
|| id2
!= DOCG4_IDREG2_VALUE
)
1004 /* initialize bch algorithm */
1005 docg4
->bch
= init_bch(DOCG4_M
, DOCG4_T
, DOCG4_PRIMITIVE_POLY
);
1006 if (docg4
->bch
== NULL
)
1009 retval
= nand_scan_tail(mtd
);
1014 * Scan for bad blocks and create bbt here, then add the factory-marked
1015 * bad blocks to the bbt.
1017 nand
->scan_bbt(mtd
);
1018 nand
->options
|= NAND_BBT_SCANNED
;
1019 retval
= read_factory_bbt(mtd
);
1023 retval
= nand_register(devnum
);