blob: 7dd9953bed4e87d4425d3201a8fe903b758fe295 [file] [log] [blame]
Mike Dunn956b03e2013-04-12 11:59:18 -07001/*
2 * drivers/mtd/nand/docg4.c
3 *
4 * Copyright (C) 2013 Mike Dunn <mikedunn@newsguy.com>
5 *
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.
8 *
9 * mtd nand driver for M-Systems DiskOnChip G4
10 *
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!
14 *
15 * TODO:
16 *
17 * Mechanism for management of password-protected areas
18 *
19 * Hamming ecc when reading oob only
20 *
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
24 * capacity.
25 *
26 * Support for multiple cascaded devices ("floors"). Not sure which gadgets
27 * contain multiple G4s in a cascaded configuration, if any.
28 *
29 */
30
31
32#include <common.h>
33#include <asm/arch/hardware.h>
34#include <asm/io.h>
35#include <asm/bitops.h>
36#include <asm/errno.h>
37#include <malloc.h>
38#include <nand.h>
39#include <linux/bch.h>
40#include <linux/bitrev.h>
41#include <linux/mtd/docg4.h>
42
43/*
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!
47 */
48static inline void write_nop(void __iomem *docptr)
49{
50 writew(0, docptr + DOC_NOP);
51}
52
53
54static int poll_status(void __iomem *docptr)
55{
56 /*
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.
60 */
61
62 uint8_t flash_status;
63
64 /* hardware quirk requires reading twice initially */
65 flash_status = readb(docptr + DOC_FLASHCONTROL);
66
67 do {
68 flash_status = readb(docptr + DOC_FLASHCONTROL);
69 } while (!(flash_status & DOC_CTRL_FLASHREADY));
70
71 return 0;
72}
73
74static void write_addr(void __iomem *docptr, uint32_t docg4_addr)
75{
76 /* write the four address bytes packed in docg4_addr to the device */
77
78 writeb(docg4_addr & 0xff, docptr + DOC_FLASHADDRESS);
79 docg4_addr >>= 8;
80 writeb(docg4_addr & 0xff, docptr + DOC_FLASHADDRESS);
81 docg4_addr >>= 8;
82 writeb(docg4_addr & 0xff, docptr + DOC_FLASHADDRESS);
83 docg4_addr >>= 8;
84 writeb(docg4_addr & 0xff, docptr + DOC_FLASHADDRESS);
85}
86
87/*
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
102 * blocks.
103 */
104static const int ignore_badblocks; /* remains false */
105
106struct docg4_priv {
107 int status;
108 struct {
109 unsigned int command;
110 int column;
111 int page;
112 } last_command;
113 uint8_t oob_buf[16];
114 uint8_t ecc_buf[7];
115 int oob_page;
116 struct bch_control *bch;
117};
118/*
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.
122 */
123static struct nand_ecclayout docg4_oobinfo = {
124 .eccbytes = 9,
125 .eccpos = {7, 8, 9, 10, 11, 12, 13, 14, 15},
126 .oobavail = 7,
127 .oobfree = { {0, 7} }
128};
129
130static void reset(void __iomem *docptr)
131{
132 /* full device reset */
133
134 writew(DOC_ASICMODE_RESET | DOC_ASICMODE_MDWREN, docptr + DOC_ASICMODE);
135 writew(~(DOC_ASICMODE_RESET | DOC_ASICMODE_MDWREN),
136 docptr + DOC_ASICMODECONFIRM);
137 write_nop(docptr);
138
139 writew(DOC_ASICMODE_NORMAL | DOC_ASICMODE_MDWREN,
140 docptr + DOC_ASICMODE);
141 writew(~(DOC_ASICMODE_NORMAL | DOC_ASICMODE_MDWREN),
142 docptr + DOC_ASICMODECONFIRM);
143
144 writew(DOC_ECCCONF1_ECC_ENABLE, docptr + DOC_ECCCONF1);
145
146 poll_status(docptr);
147}
148
149static void docg4_select_chip(struct mtd_info *mtd, int chip)
150{
151 /*
152 * Select among multiple cascaded chips ("floors"). Multiple floors are
153 * not yet supported, so the only valid non-negative value is 0.
154 */
155 void __iomem *docptr = CONFIG_SYS_NAND_BASE;
156
157 if (chip < 0)
158 return; /* deselected */
159
160 if (chip > 0)
161 printf("multiple floors currently unsupported\n");
162
163 writew(0, docptr + DOC_DEVICESELECT);
164}
165
166static void read_hw_ecc(void __iomem *docptr, uint8_t *ecc_buf)
167{
168 /* read the 7 hw-generated ecc bytes */
169
170 int i;
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));
174 }
175}
176
177static int correct_data(struct mtd_info *mtd, uint8_t *buf, int page)
178{
179 /*
180 * Called after a page read when hardware reports bitflips.
181 * Up to four bitflips can be corrected.
182 */
183
184 struct nand_chip *nand = mtd->priv;
185 struct docg4_priv *doc = nand->priv;
186 void __iomem *docptr = CONFIG_SYS_NAND_BASE;
187 int i, numerrs;
188 unsigned int errpos[4];
189 const uint8_t blank_read_hwecc[8] = {
190 0xcf, 0x72, 0xfc, 0x1b, 0xa9, 0xc7, 0xb9, 0 };
191
192 read_hw_ecc(docptr, doc->ecc_buf); /* read 7 hw-generated ecc bytes */
193
194 /* check if read error is due to a blank page */
195 if (!memcmp(doc->ecc_buf, blank_read_hwecc, 7))
196 return 0; /* yes */
197
198 /* skip additional check of "written flag" if ignore_badblocks */
199 if (!ignore_badblocks) {
200 /*
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.
207 */
208
209 if (doc->oob_buf[15]) {
210 int bit, numsetbits = 0;
211 unsigned long written_flag = doc->oob_buf[15];
212
213 for (bit = 0; bit < 8; bit++) {
214 if (written_flag & 0x01)
215 numsetbits++;
216 written_flag >>= 1;
217 }
218 if (numsetbits > 4) { /* assume blank */
219 printf("errors in blank page at offset %08x\n",
220 page * DOCG4_PAGE_SIZE);
221 return 0;
222 }
223 }
224 }
225
226 /*
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!
232 */
233 for (i = 0; i < 7; i++)
234 doc->ecc_buf[i] = bitrev8(doc->ecc_buf[i]);
235
236 numerrs = decode_bch(doc->bch, NULL, DOCG4_USERDATA_LEN, NULL,
237 doc->ecc_buf, NULL, errpos);
238
239 if (numerrs == -EBADMSG) {
240 printf("uncorrectable errors at offset %08x\n",
241 page * DOCG4_PAGE_SIZE);
242 return -EBADMSG;
243 }
244
245 BUG_ON(numerrs < 0); /* -EINVAL, or anything other than -EBADMSG */
246
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));
250
251 /* fix the errors */
252 for (i = 0; i < numerrs; i++) {
253 /* ignore if error within oob ecc bytes */
254 if (errpos[i] > DOCG4_USERDATA_LEN * 8)
255 continue;
256
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);
261
262 else /* error in page data */
263 __change_bit(errpos[i], (unsigned long *)buf);
264 }
265
266 printf("%d error(s) corrected at offset %08x\n",
267 numerrs, page * DOCG4_PAGE_SIZE);
268
269 return numerrs;
270}
271
272static int read_progstatus(struct docg4_priv *doc, void __iomem *docptr)
273{
274 /*
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.
278 */
279
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);
284
285 MTDDEBUG(MTD_DEBUG_LEVEL3, "docg4: %s: %02x %02x %02x\n",
286 __func__, status1, status2, status3);
287
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);
294 return -EIO;
295 }
296 return 0;
297}
298
299static int pageprog(struct mtd_info *mtd)
300{
301 /*
302 * Final step in writing a page. Writes the contents of its
303 * internal buffer out to the flash array, or some such.
304 */
305
306 struct nand_chip *nand = mtd->priv;
307 struct docg4_priv *doc = nand->priv;
308 void __iomem *docptr = CONFIG_SYS_NAND_BASE;
309 int retval = 0;
310
311 MTDDEBUG(MTD_DEBUG_LEVEL3, "docg4: %s\n", __func__);
312
313 writew(DOCG4_SEQ_PAGEPROG, docptr + DOC_FLASHSEQUENCE);
314 writew(DOC_CMD_PROG_CYCLE2, docptr + DOC_FLASHCOMMAND);
315 write_nop(docptr);
316 write_nop(docptr);
317
318 /* Just busy-wait; usleep_range() slows things down noticeably. */
319 poll_status(docptr);
320
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);
324 write_nop(docptr);
325 write_nop(docptr);
326 write_nop(docptr);
327 write_nop(docptr);
328 write_nop(docptr);
329
330 retval = read_progstatus(doc, docptr);
331 writew(0, docptr + DOC_DATAEND);
332 write_nop(docptr);
333 poll_status(docptr);
334 write_nop(docptr);
335
336 return retval;
337}
338
339static void sequence_reset(void __iomem *docptr)
340{
341 /* common starting sequence for all operations */
342
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);
346 write_nop(docptr);
347 write_nop(docptr);
348 poll_status(docptr);
349 write_nop(docptr);
350}
351
352static void read_page_prologue(void __iomem *docptr, uint32_t docg4_addr)
353{
354 /* first step in reading a page */
355
356 sequence_reset(docptr);
357
358 writew(DOCG4_SEQ_PAGE_READ, docptr + DOC_FLASHSEQUENCE);
359 writew(DOCG4_CMD_PAGE_READ, docptr + DOC_FLASHCOMMAND);
360 write_nop(docptr);
361
362 write_addr(docptr, docg4_addr);
363
364 write_nop(docptr);
365 writew(DOCG4_CMD_READ2, docptr + DOC_FLASHCOMMAND);
366 write_nop(docptr);
367 write_nop(docptr);
368
369 poll_status(docptr);
370}
371
372static void write_page_prologue(void __iomem *docptr, uint32_t docg4_addr)
373{
374 /* first step in writing a page */
375
376 sequence_reset(docptr);
377 writew(DOCG4_SEQ_PAGEWRITE, docptr + DOC_FLASHSEQUENCE);
378 writew(DOCG4_CMD_PAGEWRITE, docptr + DOC_FLASHCOMMAND);
379 write_nop(docptr);
380 write_addr(docptr, docg4_addr);
381 write_nop(docptr);
382 write_nop(docptr);
383 poll_status(docptr);
384}
385
386static uint32_t mtd_to_docg4_address(int page, int column)
387{
388 /*
389 * Convert mtd address to format used by the device, 32 bit packed.
390 *
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.
397 *
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
405 * addressed.
406 *
407 * This requires us to convert addresses passed by the mtd nand
408 * infrastructure code to those used by the device.
409 *
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.
416 */
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 */
420}
421
422static void docg4_command(struct mtd_info *mtd, unsigned command, int column,
423 int page_addr)
424{
425 /* handle standard nand commands */
426
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);
430
431 MTDDEBUG(MTD_DEBUG_LEVEL3, "%s %x, page_addr=%x, column=%x\n",
432 __func__, command, page_addr, column);
433
434 /*
435 * Save the command and its arguments. This enables emulation of
436 * standard flash devices, and also some optimizations.
437 */
438 doc->last_command.command = command;
439 doc->last_command.column = column;
440 doc->last_command.page = page_addr;
441
442 switch (command) {
443 case NAND_CMD_RESET:
444 reset(CONFIG_SYS_NAND_BASE);
445 break;
446
447 case NAND_CMD_READ0:
448 read_page_prologue(CONFIG_SYS_NAND_BASE, g4_addr);
449 break;
450
451 case NAND_CMD_STATUS:
452 /* next call to read_byte() will expect a status */
453 break;
454
455 case NAND_CMD_SEQIN:
456 write_page_prologue(CONFIG_SYS_NAND_BASE, g4_addr);
457
458 /* hack for deferred write of oob bytes */
459 if (doc->oob_page == page_addr)
460 memcpy(nand->oob_poi, doc->oob_buf, 16);
461 break;
462
463 case NAND_CMD_PAGEPROG:
464 pageprog(mtd);
465 break;
466
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",
473 command);
474 break;
475 }
476}
477
478static void docg4_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
479{
480 int i;
481 struct nand_chip *nand = mtd->priv;
482 uint16_t *p = (uint16_t *)buf;
483 len >>= 1;
484
485 for (i = 0; i < len; i++)
486 p[i] = readw(nand->IO_ADDR_R);
487}
488
489static int docg4_read_oob(struct mtd_info *mtd, struct nand_chip *nand,
490 int page, int sndcmd)
491{
492 struct docg4_priv *doc = nand->priv;
493 void __iomem *docptr = CONFIG_SYS_NAND_BASE;
494 uint16_t status;
495
496 MTDDEBUG(MTD_DEBUG_LEVEL3, "%s: page %x\n", __func__, page);
497
498 /*
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
502 * separate oob read.
503 */
504 if (doc->last_command.command == NAND_CMD_READ0 &&
505 doc->last_command.page == page) {
506 memcpy(nand->oob_poi, doc->oob_buf, 16);
507 return 0;
508 }
509
510 /*
511 * Separate read of oob data only.
512 */
513 docg4_command(mtd, NAND_CMD_READ0, nand->ecc.size, page);
514
515 writew(DOC_ECCCONF0_READ_MODE | DOCG4_OOB_SIZE, docptr + DOC_ECCCONF0);
516 write_nop(docptr);
517 write_nop(docptr);
518 write_nop(docptr);
519 write_nop(docptr);
520 write_nop(docptr);
521
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);
526 return -EIO;
527 }
528
529 MTDDEBUG(MTD_DEBUG_LEVEL3, "%s: status = 0x%x\n", __func__, status);
530
531 docg4_read_buf(mtd, nand->oob_poi, 16);
532
533 write_nop(docptr);
534 write_nop(docptr);
535 write_nop(docptr);
536 writew(0, docptr + DOC_DATAEND);
537 write_nop(docptr);
538
539 return 0;
540}
541
542static int docg4_write_oob(struct mtd_info *mtd, struct nand_chip *nand,
543 int page)
544{
545 /*
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.
554 */
555
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);
560 return 0;
561}
562
563static int docg4_block_neverbad(struct mtd_info *mtd, loff_t ofs, int getchip)
564{
565 /* only called when module_param ignore_badblocks is set */
566 return 0;
567}
568
569static void docg4_write_buf16(struct mtd_info *mtd, const uint8_t *buf, int len)
570{
571 int i;
572 struct nand_chip *nand = mtd->priv;
573 uint16_t *p = (uint16_t *)buf;
574 len >>= 1;
575
576 for (i = 0; i < len; i++)
577 writew(p[i], nand->IO_ADDR_W);
578}
579
580static void write_page(struct mtd_info *mtd, struct nand_chip *nand,
581 const uint8_t *buf, int use_ecc)
582{
583 void __iomem *docptr = CONFIG_SYS_NAND_BASE;
584 uint8_t ecc_buf[8];
585
586 writew(DOC_ECCCONF0_ECC_ENABLE |
587 DOC_ECCCONF0_UNKNOWN |
588 DOCG4_BCH_SIZE,
589 docptr + DOC_ECCCONF0);
590 write_nop(docptr);
591
592 /* write the page data */
593 docg4_write_buf16(mtd, buf, DOCG4_PAGE_SIZE);
594
595 /* oob bytes 0 through 5 are written to I/O reg */
596 docg4_write_buf16(mtd, nand->oob_poi, 6);
597
598 /* oob byte 6 written to a separate reg */
599 writew(nand->oob_poi[6], docptr + DOCG4_OOB_6_7);
600
601 write_nop(docptr);
602 write_nop(docptr);
603
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);
610 write_nop(docptr);
611
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 */
615 }
616
617 /* write user-supplied bytes to oob */
618 else {
619 writew(nand->oob_poi[7], docptr + DOCG4_OOB_6_7);
620 write_nop(docptr);
621 memcpy(ecc_buf, &nand->oob_poi[8], 8);
622 }
623
624 docg4_write_buf16(mtd, ecc_buf, 8);
625 write_nop(docptr);
626 write_nop(docptr);
627 writew(0, docptr + DOC_DATAEND);
628 write_nop(docptr);
629}
630
631static void docg4_write_page_raw(struct mtd_info *mtd, struct nand_chip *nand,
632 const uint8_t *buf)
633{
634 return write_page(mtd, nand, buf, 0);
635}
636
637static void docg4_write_page(struct mtd_info *mtd, struct nand_chip *nand,
638 const uint8_t *buf)
639{
640 return write_page(mtd, nand, buf, 1);
641}
642
643static int read_page(struct mtd_info *mtd, struct nand_chip *nand,
644 uint8_t *buf, int page, int use_ecc)
645{
646 struct docg4_priv *doc = nand->priv;
647 void __iomem *docptr = CONFIG_SYS_NAND_BASE;
648 uint16_t status, edc_err, *buf16;
649
650 writew(DOC_ECCCONF0_READ_MODE |
651 DOC_ECCCONF0_ECC_ENABLE |
652 DOC_ECCCONF0_UNKNOWN |
653 DOCG4_BCH_SIZE,
654 docptr + DOC_ECCCONF0);
655 write_nop(docptr);
656 write_nop(docptr);
657 write_nop(docptr);
658 write_nop(docptr);
659 write_nop(docptr);
660
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);
666 return -EIO;
667 }
668
669 docg4_read_buf(mtd, buf, DOCG4_PAGE_SIZE); /* read the page data */
670
671 /* first 14 oob bytes read from I/O reg */
672 docg4_read_buf(mtd, nand->oob_poi, 14);
673
674 /* last 2 read from another reg */
675 buf16 = (uint16_t *)(nand->oob_poi + 14);
676 *buf16 = readw(docptr + DOCG4_MYSTERY_REG);
677
678 /*
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.
683 */
684 memcpy(doc->oob_buf, nand->oob_poi, 16);
685
686 write_nop(docptr);
687
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);
692
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++;
698 else
699 mtd->ecc_stats.corrected += bits_corrected;
700 }
701 }
702
703 writew(0, docptr + DOC_DATAEND);
704 return 0;
705}
706
707
708static int docg4_read_page_raw(struct mtd_info *mtd, struct nand_chip *nand,
709 uint8_t *buf, int page)
710{
711 return read_page(mtd, nand, buf, page, 0);
712}
713
714static int docg4_read_page(struct mtd_info *mtd, struct nand_chip *nand,
715 uint8_t *buf, int page)
716{
717 return read_page(mtd, nand, buf, page, 1);
718}
719
720static void docg4_erase_block(struct mtd_info *mtd, int page)
721{
722 struct nand_chip *nand = mtd->priv;
723 struct docg4_priv *doc = nand->priv;
724 void __iomem *docptr = CONFIG_SYS_NAND_BASE;
725 uint16_t g4_page;
726
727 MTDDEBUG(MTD_DEBUG_LEVEL3, "%s: page %04x\n", __func__, page);
728
729 sequence_reset(docptr);
730
731 writew(DOCG4_SEQ_BLOCKERASE, docptr + DOC_FLASHSEQUENCE);
732 writew(DOC_CMD_PROG_BLOCK_ADDR, docptr + DOC_FLASHCOMMAND);
733 write_nop(docptr);
734
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);
738 g4_page >>= 8;
739 writeb(g4_page & 0xff, docptr + DOC_FLASHADDRESS);
740 write_nop(docptr);
741
742 /* start the erasure */
743 writew(DOC_CMD_ERASECYCLE2, docptr + DOC_FLASHCOMMAND);
744 write_nop(docptr);
745 write_nop(docptr);
746
747 poll_status(docptr);
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);
751 write_nop(docptr);
752 write_nop(docptr);
753 write_nop(docptr);
754 write_nop(docptr);
755 write_nop(docptr);
756
757 read_progstatus(doc, docptr);
758
759 writew(0, docptr + DOC_DATAEND);
760 write_nop(docptr);
761 poll_status(docptr);
762 write_nop(docptr);
763}
764
765static int read_factory_bbt(struct mtd_info *mtd)
766{
767 /*
768 * The device contains a read-only factory bad block table. Read it and
769 * update the memory-based bbt accordingly.
770 */
771
772 struct nand_chip *nand = mtd->priv;
773 uint32_t g4_addr = mtd_to_docg4_address(DOCG4_FACTORY_BBT_PAGE, 0);
774 uint8_t *buf;
775 int i, block, status;
776
777 buf = kzalloc(DOCG4_PAGE_SIZE, GFP_KERNEL);
778 if (buf == NULL)
779 return -ENOMEM;
780
781 read_page_prologue(CONFIG_SYS_NAND_BASE, g4_addr);
782 status = docg4_read_page(mtd, nand, buf, DOCG4_FACTORY_BBT_PAGE);
783 if (status)
784 goto exit;
785
786 /*
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.
791 * Ugly, I know.
792 */
793 if (nand->bbt == NULL) /* no memory-based bbt */
794 goto exit;
795
796 /*
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.
800 */
801 for (i = block = 0; block < DOCG4_NUMBLOCKS; block += 8, i++) {
802 int bitnum;
803 uint8_t mask;
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",
812 badblock);
813 }
814 }
815 }
816 exit:
817 kfree(buf);
818 return status;
819}
820
821static int docg4_block_markbad(struct mtd_info *mtd, loff_t ofs)
822{
823 /*
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.
830 */
831
832 int ret, i;
833 uint8_t *buf;
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);
839
840 MTDDEBUG(MTD_DEBUG_LEVEL3, "%s: %08llx\n", __func__, ofs);
841
842 if (unlikely(ofs & (DOCG4_BLOCK_SIZE - 1)))
843 printf("%s: ofs %llx not start of block!\n",
844 __func__, ofs);
845
846 /* allocate blank buffer for page data */
847 buf = kzalloc(DOCG4_PAGE_SIZE, GFP_KERNEL);
848 if (buf == NULL)
849 return -ENOMEM;
850
851 /* update bbt in memory */
852 nand->bbt[block / 4] |= 0x01 << ((block & 0x03) * 2);
853
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];
858
859 /* write first page of block */
860 write_page_prologue(CONFIG_SYS_NAND_BASE, g4_addr);
861 docg4_write_page(mtd, nand, buf);
862 ret = pageprog(mtd);
863 if (!ret)
864 mtd->ecc_stats.badblocks++;
865
866 kfree(buf);
867
868 return ret;
869}
870
871static uint8_t docg4_read_byte(struct mtd_info *mtd)
872{
873 struct nand_chip *nand = mtd->priv;
874 struct docg4_priv *doc = nand->priv;
875
876 MTDDEBUG(MTD_DEBUG_LEVEL3, "%s\n", __func__);
877
878 if (doc->last_command.command == NAND_CMD_STATUS) {
879 int status;
880
881 /*
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.
885 */
886 doc->last_command.command = 0;
887
888 if (doc->status) {
889 status = doc->status;
890 doc->status = 0;
891 }
892
893 /* why is NAND_STATUS_WP inverse logic?? */
894 else
895 status = NAND_STATUS_WP | NAND_STATUS_READY;
896
897 return status;
898 }
899
900 printf("unexpectd call to read_byte()\n");
901
902 return 0;
903}
904
905static int docg4_wait(struct mtd_info *mtd, struct nand_chip *nand)
906{
907 struct docg4_priv *doc = nand->priv;
908 int status = NAND_STATUS_WP; /* inverse logic?? */
909 MTDDEBUG(MTD_DEBUG_LEVEL3, "%s...\n", __func__);
910
911 /* report any previously unreported error */
912 if (doc->status) {
913 status |= doc->status;
914 doc->status = 0;
915 return status;
916 }
917
918 status |= poll_status(CONFIG_SYS_NAND_BASE);
919 return status;
920}
921
922int docg4_nand_init(struct mtd_info *mtd, struct nand_chip *nand, int devnum)
923{
924 uint16_t id1, id2;
925 struct docg4_priv *docg4;
926 int retval;
927
928 docg4 = kzalloc(sizeof(*docg4), GFP_KERNEL);
929 if (!docg4)
930 return -1;
931
932 mtd->priv = nand;
933 nand->priv = docg4;
934
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).
938 */
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;
944
945 nand->IO_ADDR_R =
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;
961 nand->ecc.bytes = 8;
962 nand->options =
963 NAND_BUSWIDTH_16 | NAND_NO_SUBPAGE_WRITE | NAND_NO_AUTOINCR;
964 nand->controller = &nand->hwcontrol;
965
966 /* methods */
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;
982
983 /*
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.
988 */
989 if (ignore_badblocks) {
990 nand->options |= NAND_SKIP_BBTSCAN;
991 nand->block_bad = docg4_block_neverbad;
992 }
993
994 reset(CONFIG_SYS_NAND_BASE);
995
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)
1002 return -1;
1003
1004 /* initialize bch algorithm */
1005 docg4->bch = init_bch(DOCG4_M, DOCG4_T, DOCG4_PRIMITIVE_POLY);
1006 if (docg4->bch == NULL)
1007 return -1;
1008
1009 retval = nand_scan_tail(mtd);
1010 if (retval)
1011 return -1;
1012
1013 /*
1014 * Scan for bad blocks and create bbt here, then add the factory-marked
1015 * bad blocks to the bbt.
1016 */
1017 nand->scan_bbt(mtd);
1018 nand->options |= NAND_BBT_SCANNED;
1019 retval = read_factory_bbt(mtd);
1020 if (retval)
1021 return -1;
1022
1023 retval = nand_register(devnum);
1024 if (retval)
1025 return -1;
1026
1027 return 0;
1028}