2 * drivers/mtd/nand/docg4.c
4 * Copyright (C) 2013 Mike Dunn <mikedunn@newsguy.com>
6 * SPDX-License-Identifier: GPL-2.0+
8 * mtd nand driver for M-Systems DiskOnChip G4
10 * Tested on the Palm Treo 680. The G4 is also present on Toshiba Portege, Asus
11 * P526, some HTC smartphones (Wizard, Prophet, ...), O2 XDA Zinc, maybe others.
12 * Should work on these as well. Let me know!
16 * Mechanism for management of password-protected areas
18 * Hamming ecc when reading oob only
20 * According to the M-Sys documentation, this device is also available in a
21 * "dual-die" configuration having a 256MB capacity, but no mechanism for
22 * detecting this variant is documented. Currently this driver assumes 128MB
25 * Support for multiple cascaded devices ("floors"). Not sure which gadgets
26 * contain multiple G4s in a cascaded configuration, if any.
31 #include <asm/arch/hardware.h>
33 #include <asm/bitops.h>
34 #include <asm/errno.h>
37 #include <linux/bch.h>
38 #include <linux/bitrev.h>
39 #include <linux/mtd/docg4.h>
42 * The device has a nop register which M-Sys claims is for the purpose of
43 * inserting precise delays. But beware; at least some operations fail if the
44 * nop writes are replaced with a generic delay!
46 static inline void write_nop(void __iomem *docptr)
48 writew(0, docptr + DOC_NOP);
52 static int poll_status(void __iomem *docptr)
55 * Busy-wait for the FLASHREADY bit to be set in the FLASHCONTROL
56 * register. Operations known to take a long time (e.g., block erase)
57 * should sleep for a while before calling this.
62 /* hardware quirk requires reading twice initially */
63 flash_status = readb(docptr + DOC_FLASHCONTROL);
66 flash_status = readb(docptr + DOC_FLASHCONTROL);
67 } while (!(flash_status & DOC_CTRL_FLASHREADY));
72 static void write_addr(void __iomem *docptr, uint32_t docg4_addr)
74 /* write the four address bytes packed in docg4_addr to the device */
76 writeb(docg4_addr & 0xff, docptr + DOC_FLASHADDRESS);
78 writeb(docg4_addr & 0xff, docptr + DOC_FLASHADDRESS);
80 writeb(docg4_addr & 0xff, docptr + DOC_FLASHADDRESS);
82 writeb(docg4_addr & 0xff, docptr + DOC_FLASHADDRESS);
86 * This is a module parameter in the linux kernel version of this driver. It is
87 * hard-coded to 'off' for u-boot. This driver uses oob to mark bad blocks.
88 * This can be problematic when dealing with data not intended for the mtd/nand
89 * subsystem. For example, on boards that boot from the docg4 and use the IPL
90 * to load an spl + u-boot image, the blocks containing the image will be
91 * reported as "bad" because the oob of the first page of each block contains a
92 * magic number that the IPL looks for, which causes the badblock scan to
93 * erroneously add them to the bad block table. To erase such a block, use
94 * u-boot's 'nand scrub'. scrub is safe for the docg4. The device does have a
95 * factory bad block table, but it is read-only, and is used in conjunction with
96 * oob bad block markers that are written by mtd/nand when a block is deemed to
97 * be bad. To read data from "bad" blocks, use 'read.raw'. Unfortunately,
98 * read.raw does not use ecc, which would still work fine on such misidentified
99 * bad blocks. TODO: u-boot nand utilities need the ability to ignore bad
102 static const int ignore_badblocks; /* remains false */
107 unsigned int command;
114 struct bch_control *bch;
117 * Oob bytes 0 - 6 are available to the user.
118 * Byte 7 is hamming ecc for first 7 bytes. Bytes 8 - 14 are hw-generated ecc.
119 * Byte 15 (the last) is used by the driver as a "page written" flag.
121 static struct nand_ecclayout docg4_oobinfo = {
123 .eccpos = {7, 8, 9, 10, 11, 12, 13, 14, 15},
125 .oobfree = { {0, 7} }
128 static void reset(void __iomem *docptr)
130 /* full device reset */
132 writew(DOC_ASICMODE_RESET | DOC_ASICMODE_MDWREN, docptr + DOC_ASICMODE);
133 writew(~(DOC_ASICMODE_RESET | DOC_ASICMODE_MDWREN),
134 docptr + DOC_ASICMODECONFIRM);
137 writew(DOC_ASICMODE_NORMAL | DOC_ASICMODE_MDWREN,
138 docptr + DOC_ASICMODE);
139 writew(~(DOC_ASICMODE_NORMAL | DOC_ASICMODE_MDWREN),
140 docptr + DOC_ASICMODECONFIRM);
142 writew(DOC_ECCCONF1_ECC_ENABLE, docptr + DOC_ECCCONF1);
147 static void docg4_select_chip(struct mtd_info *mtd, int chip)
150 * Select among multiple cascaded chips ("floors"). Multiple floors are
151 * not yet supported, so the only valid non-negative value is 0.
153 void __iomem *docptr = CONFIG_SYS_NAND_BASE;
156 return; /* deselected */
159 printf("multiple floors currently unsupported\n");
161 writew(0, docptr + DOC_DEVICESELECT);
164 static void read_hw_ecc(void __iomem *docptr, uint8_t *ecc_buf)
166 /* read the 7 hw-generated ecc bytes */
169 for (i = 0; i < 7; i++) { /* hw quirk; read twice */
170 ecc_buf[i] = readb(docptr + DOC_BCH_SYNDROM(i));
171 ecc_buf[i] = readb(docptr + DOC_BCH_SYNDROM(i));
175 static int correct_data(struct mtd_info *mtd, uint8_t *buf, int page)
178 * Called after a page read when hardware reports bitflips.
179 * Up to four bitflips can be corrected.
182 struct nand_chip *nand = mtd->priv;
183 struct docg4_priv *doc = nand->priv;
184 void __iomem *docptr = CONFIG_SYS_NAND_BASE;
186 unsigned int errpos[4];
187 const uint8_t blank_read_hwecc[8] = {
188 0xcf, 0x72, 0xfc, 0x1b, 0xa9, 0xc7, 0xb9, 0 };
190 read_hw_ecc(docptr, doc->ecc_buf); /* read 7 hw-generated ecc bytes */
192 /* check if read error is due to a blank page */
193 if (!memcmp(doc->ecc_buf, blank_read_hwecc, 7))
196 /* skip additional check of "written flag" if ignore_badblocks */
197 if (!ignore_badblocks) {
199 * If the hw ecc bytes are not those of a blank page, there's
200 * still a chance that the page is blank, but was read with
201 * errors. Check the "written flag" in last oob byte, which
202 * is set to zero when a page is written. If more than half
203 * the bits are set, assume a blank page. Unfortunately, the
204 * bit flips(s) are not reported in stats.
207 if (doc->oob_buf[15]) {
208 int bit, numsetbits = 0;
209 unsigned long written_flag = doc->oob_buf[15];
211 for (bit = 0; bit < 8; bit++) {
212 if (written_flag & 0x01)
216 if (numsetbits > 4) { /* assume blank */
217 printf("errors in blank page at offset %08x\n",
218 page * DOCG4_PAGE_SIZE);
225 * The hardware ecc unit produces oob_ecc ^ calc_ecc. The kernel's bch
226 * algorithm is used to decode this. However the hw operates on page
227 * data in a bit order that is the reverse of that of the bch alg,
228 * requiring that the bits be reversed on the result. Thanks to Ivan
229 * Djelic for his analysis!
231 for (i = 0; i < 7; i++)
232 doc->ecc_buf[i] = bitrev8(doc->ecc_buf[i]);
234 numerrs = decode_bch(doc->bch, NULL, DOCG4_USERDATA_LEN, NULL,
235 doc->ecc_buf, NULL, errpos);
237 if (numerrs == -EBADMSG) {
238 printf("uncorrectable errors at offset %08x\n",
239 page * DOCG4_PAGE_SIZE);
243 BUG_ON(numerrs < 0); /* -EINVAL, or anything other than -EBADMSG */
245 /* undo last step in BCH alg (modulo mirroring not needed) */
246 for (i = 0; i < numerrs; i++)
247 errpos[i] = (errpos[i] & ~7)|(7-(errpos[i] & 7));
250 for (i = 0; i < numerrs; i++) {
251 /* ignore if error within oob ecc bytes */
252 if (errpos[i] > DOCG4_USERDATA_LEN * 8)
255 /* if error within oob area preceeding ecc bytes... */
256 if (errpos[i] > DOCG4_PAGE_SIZE * 8)
257 __change_bit(errpos[i] - DOCG4_PAGE_SIZE * 8,
258 (unsigned long *)doc->oob_buf);
260 else /* error in page data */
261 __change_bit(errpos[i], (unsigned long *)buf);
264 printf("%d error(s) corrected at offset %08x\n",
265 numerrs, page * DOCG4_PAGE_SIZE);
270 static int read_progstatus(struct docg4_priv *doc, void __iomem *docptr)
273 * This apparently checks the status of programming. Done after an
274 * erasure, and after page data is written. On error, the status is
275 * saved, to be later retrieved by the nand infrastructure code.
278 /* status is read from the I/O reg */
279 uint16_t status1 = readw(docptr + DOC_IOSPACE_DATA);
280 uint16_t status2 = readw(docptr + DOC_IOSPACE_DATA);
281 uint16_t status3 = readw(docptr + DOCG4_MYSTERY_REG);
283 MTDDEBUG(MTD_DEBUG_LEVEL3, "docg4: %s: %02x %02x %02x\n",
284 __func__, status1, status2, status3);
286 if (status1 != DOCG4_PROGSTATUS_GOOD ||
287 status2 != DOCG4_PROGSTATUS_GOOD_2 ||
288 status3 != DOCG4_PROGSTATUS_GOOD_2) {
289 doc->status = NAND_STATUS_FAIL;
290 printf("read_progstatus failed: %02x, %02x, %02x\n",
291 status1, status2, status3);
297 static int pageprog(struct mtd_info *mtd)
300 * Final step in writing a page. Writes the contents of its
301 * internal buffer out to the flash array, or some such.
304 struct nand_chip *nand = mtd->priv;
305 struct docg4_priv *doc = nand->priv;
306 void __iomem *docptr = CONFIG_SYS_NAND_BASE;
309 MTDDEBUG(MTD_DEBUG_LEVEL3, "docg4: %s\n", __func__);
311 writew(DOCG4_SEQ_PAGEPROG, docptr + DOC_FLASHSEQUENCE);
312 writew(DOC_CMD_PROG_CYCLE2, docptr + DOC_FLASHCOMMAND);
316 /* Just busy-wait; usleep_range() slows things down noticeably. */
319 writew(DOCG4_SEQ_FLUSH, docptr + DOC_FLASHSEQUENCE);
320 writew(DOCG4_CMD_FLUSH, docptr + DOC_FLASHCOMMAND);
321 writew(DOC_ECCCONF0_READ_MODE | 4, docptr + DOC_ECCCONF0);
328 retval = read_progstatus(doc, docptr);
329 writew(0, docptr + DOC_DATAEND);
337 static void sequence_reset(void __iomem *docptr)
339 /* common starting sequence for all operations */
341 writew(DOC_CTRL_UNKNOWN | DOC_CTRL_CE, docptr + DOC_FLASHCONTROL);
342 writew(DOC_SEQ_RESET, docptr + DOC_FLASHSEQUENCE);
343 writew(DOC_CMD_RESET, docptr + DOC_FLASHCOMMAND);
350 static void read_page_prologue(void __iomem *docptr, uint32_t docg4_addr)
352 /* first step in reading a page */
354 sequence_reset(docptr);
356 writew(DOCG4_SEQ_PAGE_READ, docptr + DOC_FLASHSEQUENCE);
357 writew(DOCG4_CMD_PAGE_READ, docptr + DOC_FLASHCOMMAND);
360 write_addr(docptr, docg4_addr);
363 writew(DOCG4_CMD_READ2, docptr + DOC_FLASHCOMMAND);
370 static void write_page_prologue(void __iomem *docptr, uint32_t docg4_addr)
372 /* first step in writing a page */
374 sequence_reset(docptr);
375 writew(DOCG4_SEQ_PAGEWRITE, docptr + DOC_FLASHSEQUENCE);
376 writew(DOCG4_CMD_PAGEWRITE, docptr + DOC_FLASHCOMMAND);
378 write_addr(docptr, docg4_addr);
384 static uint32_t mtd_to_docg4_address(int page, int column)
387 * Convert mtd address to format used by the device, 32 bit packed.
389 * Some notes on G4 addressing... The M-Sys documentation on this device
390 * claims that pages are 2K in length, and indeed, the format of the
391 * address used by the device reflects that. But within each page are
392 * four 512 byte "sub-pages", each with its own oob data that is
393 * read/written immediately after the 512 bytes of page data. This oob
394 * data contains the ecc bytes for the preceeding 512 bytes.
396 * Rather than tell the mtd nand infrastructure that page size is 2k,
397 * with four sub-pages each, we engage in a little subterfuge and tell
398 * the infrastructure code that pages are 512 bytes in size. This is
399 * done because during the course of reverse-engineering the device, I
400 * never observed an instance where an entire 2K "page" was read or
401 * written as a unit. Each "sub-page" is always addressed individually,
402 * its data read/written, and ecc handled before the next "sub-page" is
405 * This requires us to convert addresses passed by the mtd nand
406 * infrastructure code to those used by the device.
408 * The address that is written to the device consists of four bytes: the
409 * first two are the 2k page number, and the second is the index into
410 * the page. The index is in terms of 16-bit half-words and includes
411 * the preceeding oob data, so e.g., the index into the second
412 * "sub-page" is 0x108, and the full device address of the start of mtd
413 * page 0x201 is 0x00800108.
415 int g4_page = page / 4; /* device's 2K page */
416 int g4_index = (page % 4) * 0x108 + column/2; /* offset into page */
417 return (g4_page << 16) | g4_index; /* pack */
420 static void docg4_command(struct mtd_info *mtd, unsigned command, int column,
423 /* handle standard nand commands */
425 struct nand_chip *nand = mtd->priv;
426 struct docg4_priv *doc = nand->priv;
427 uint32_t g4_addr = mtd_to_docg4_address(page_addr, column);
429 MTDDEBUG(MTD_DEBUG_LEVEL3, "%s %x, page_addr=%x, column=%x\n",
430 __func__, command, page_addr, column);
433 * Save the command and its arguments. This enables emulation of
434 * standard flash devices, and also some optimizations.
436 doc->last_command.command = command;
437 doc->last_command.column = column;
438 doc->last_command.page = page_addr;
442 reset(CONFIG_SYS_NAND_BASE);
446 read_page_prologue(CONFIG_SYS_NAND_BASE, g4_addr);
449 case NAND_CMD_STATUS:
450 /* next call to read_byte() will expect a status */
454 write_page_prologue(CONFIG_SYS_NAND_BASE, g4_addr);
456 /* hack for deferred write of oob bytes */
457 if (doc->oob_page == page_addr)
458 memcpy(nand->oob_poi, doc->oob_buf, 16);
461 case NAND_CMD_PAGEPROG:
465 /* we don't expect these, based on review of nand_base.c */
466 case NAND_CMD_READOOB:
467 case NAND_CMD_READID:
468 case NAND_CMD_ERASE1:
469 case NAND_CMD_ERASE2:
470 printf("docg4_command: unexpected nand command 0x%x\n",
476 static void docg4_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
479 struct nand_chip *nand = mtd->priv;
480 uint16_t *p = (uint16_t *)buf;
483 for (i = 0; i < len; i++)
484 p[i] = readw(nand->IO_ADDR_R);
487 static int docg4_read_oob(struct mtd_info *mtd, struct nand_chip *nand,
490 struct docg4_priv *doc = nand->priv;
491 void __iomem *docptr = CONFIG_SYS_NAND_BASE;
494 MTDDEBUG(MTD_DEBUG_LEVEL3, "%s: page %x\n", __func__, page);
497 * Oob bytes are read as part of a normal page read. If the previous
498 * nand command was a read of the page whose oob is now being read, just
499 * copy the oob bytes that we saved in a local buffer and avoid a
502 if (doc->last_command.command == NAND_CMD_READ0 &&
503 doc->last_command.page == page) {
504 memcpy(nand->oob_poi, doc->oob_buf, 16);
509 * Separate read of oob data only.
511 docg4_command(mtd, NAND_CMD_READ0, nand->ecc.size, page);
513 writew(DOC_ECCCONF0_READ_MODE | DOCG4_OOB_SIZE, docptr + DOC_ECCCONF0);
520 /* the 1st byte from the I/O reg is a status; the rest is oob data */
521 status = readw(docptr + DOC_IOSPACE_DATA);
522 if (status & DOCG4_READ_ERROR) {
523 printf("docg4_read_oob failed: status = 0x%02x\n", status);
527 MTDDEBUG(MTD_DEBUG_LEVEL3, "%s: status = 0x%x\n", __func__, status);
529 docg4_read_buf(mtd, nand->oob_poi, 16);
534 writew(0, docptr + DOC_DATAEND);
540 static int docg4_write_oob(struct mtd_info *mtd, struct nand_chip *nand,
544 * Writing oob-only is not really supported, because MLC nand must write
545 * oob bytes at the same time as page data. Nonetheless, we save the
546 * oob buffer contents here, and then write it along with the page data
547 * if the same page is subsequently written. This allows user space
548 * utilities that write the oob data prior to the page data to work
549 * (e.g., nandwrite). The disdvantage is that, if the intention was to
550 * write oob only, the operation is quietly ignored. Also, oob can get
551 * corrupted if two concurrent processes are running nandwrite.
554 /* note that bytes 7..14 are hw generated hamming/ecc and overwritten */
555 struct docg4_priv *doc = nand->priv;
556 doc->oob_page = page;
557 memcpy(doc->oob_buf, nand->oob_poi, 16);
561 static int docg4_block_neverbad(struct mtd_info *mtd, loff_t ofs, int getchip)
563 /* only called when module_param ignore_badblocks is set */
567 static void docg4_write_buf16(struct mtd_info *mtd, const uint8_t *buf, int len)
570 struct nand_chip *nand = mtd->priv;
571 uint16_t *p = (uint16_t *)buf;
574 for (i = 0; i < len; i++)
575 writew(p[i], nand->IO_ADDR_W);
578 static int write_page(struct mtd_info *mtd, struct nand_chip *nand,
579 const uint8_t *buf, int use_ecc)
581 void __iomem *docptr = CONFIG_SYS_NAND_BASE;
584 writew(DOC_ECCCONF0_ECC_ENABLE |
585 DOC_ECCCONF0_UNKNOWN |
587 docptr + DOC_ECCCONF0);
590 /* write the page data */
591 docg4_write_buf16(mtd, buf, DOCG4_PAGE_SIZE);
593 /* oob bytes 0 through 5 are written to I/O reg */
594 docg4_write_buf16(mtd, nand->oob_poi, 6);
596 /* oob byte 6 written to a separate reg */
597 writew(nand->oob_poi[6], docptr + DOCG4_OOB_6_7);
602 /* write hw-generated ecc bytes to oob */
603 if (likely(use_ecc)) {
604 /* oob byte 7 is hamming code */
605 uint8_t hamming = readb(docptr + DOC_HAMMINGPARITY);
606 hamming = readb(docptr + DOC_HAMMINGPARITY); /* 2nd read */
607 writew(hamming, docptr + DOCG4_OOB_6_7);
610 /* read the 7 bch bytes from ecc regs */
611 read_hw_ecc(docptr, ecc_buf);
612 ecc_buf[7] = 0; /* clear the "page written" flag */
615 /* write user-supplied bytes to oob */
617 writew(nand->oob_poi[7], docptr + DOCG4_OOB_6_7);
619 memcpy(ecc_buf, &nand->oob_poi[8], 8);
622 docg4_write_buf16(mtd, ecc_buf, 8);
625 writew(0, docptr + DOC_DATAEND);
631 static int docg4_write_page_raw(struct mtd_info *mtd, struct nand_chip *nand,
632 const uint8_t *buf, int oob_required)
634 return write_page(mtd, nand, buf, 0);
637 static int docg4_write_page(struct mtd_info *mtd, struct nand_chip *nand,
638 const uint8_t *buf, int oob_required)
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 oob_required, 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 oob_required, 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, 0, 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, 1);
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;
962 nand->ecc.strength = DOCG4_T;
963 nand->options = NAND_BUSWIDTH_16 | NAND_NO_SUBPAGE_WRITE;
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);