blob: b9121c397e1ce6e06d414aa2215b98812d966560 [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 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02006 * SPDX-License-Identifier: GPL-2.0+
Mike Dunn956b03e2013-04-12 11:59:18 -07007 *
8 * mtd nand driver for M-Systems DiskOnChip G4
9 *
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!
13 *
14 * TODO:
15 *
16 * Mechanism for management of password-protected areas
17 *
18 * Hamming ecc when reading oob only
19 *
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
23 * capacity.
24 *
25 * Support for multiple cascaded devices ("floors"). Not sure which gadgets
26 * contain multiple G4s in a cascaded configuration, if any.
Mike Dunn956b03e2013-04-12 11:59:18 -070027 */
28
29
30#include <common.h>
31#include <asm/arch/hardware.h>
32#include <asm/io.h>
33#include <asm/bitops.h>
34#include <asm/errno.h>
35#include <malloc.h>
36#include <nand.h>
37#include <linux/bch.h>
38#include <linux/bitrev.h>
39#include <linux/mtd/docg4.h>
40
41/*
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!
45 */
46static inline void write_nop(void __iomem *docptr)
47{
48 writew(0, docptr + DOC_NOP);
49}
50
51
52static int poll_status(void __iomem *docptr)
53{
54 /*
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.
58 */
59
60 uint8_t flash_status;
61
62 /* hardware quirk requires reading twice initially */
63 flash_status = readb(docptr + DOC_FLASHCONTROL);
64
65 do {
66 flash_status = readb(docptr + DOC_FLASHCONTROL);
67 } while (!(flash_status & DOC_CTRL_FLASHREADY));
68
69 return 0;
70}
71
72static void write_addr(void __iomem *docptr, uint32_t docg4_addr)
73{
74 /* write the four address bytes packed in docg4_addr to the device */
75
76 writeb(docg4_addr & 0xff, docptr + DOC_FLASHADDRESS);
77 docg4_addr >>= 8;
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}
84
85/*
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
100 * blocks.
101 */
102static const int ignore_badblocks; /* remains false */
103
104struct docg4_priv {
105 int status;
106 struct {
107 unsigned int command;
108 int column;
109 int page;
110 } last_command;
111 uint8_t oob_buf[16];
112 uint8_t ecc_buf[7];
113 int oob_page;
114 struct bch_control *bch;
115};
116/*
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.
120 */
121static struct nand_ecclayout docg4_oobinfo = {
122 .eccbytes = 9,
123 .eccpos = {7, 8, 9, 10, 11, 12, 13, 14, 15},
124 .oobavail = 7,
125 .oobfree = { {0, 7} }
126};
127
128static void reset(void __iomem *docptr)
129{
130 /* full device reset */
131
132 writew(DOC_ASICMODE_RESET | DOC_ASICMODE_MDWREN, docptr + DOC_ASICMODE);
133 writew(~(DOC_ASICMODE_RESET | DOC_ASICMODE_MDWREN),
134 docptr + DOC_ASICMODECONFIRM);
135 write_nop(docptr);
136
137 writew(DOC_ASICMODE_NORMAL | DOC_ASICMODE_MDWREN,
138 docptr + DOC_ASICMODE);
139 writew(~(DOC_ASICMODE_NORMAL | DOC_ASICMODE_MDWREN),
140 docptr + DOC_ASICMODECONFIRM);
141
142 writew(DOC_ECCCONF1_ECC_ENABLE, docptr + DOC_ECCCONF1);
143
144 poll_status(docptr);
145}
146
147static void docg4_select_chip(struct mtd_info *mtd, int chip)
148{
149 /*
150 * Select among multiple cascaded chips ("floors"). Multiple floors are
151 * not yet supported, so the only valid non-negative value is 0.
152 */
153 void __iomem *docptr = CONFIG_SYS_NAND_BASE;
154
155 if (chip < 0)
156 return; /* deselected */
157
158 if (chip > 0)
159 printf("multiple floors currently unsupported\n");
160
161 writew(0, docptr + DOC_DEVICESELECT);
162}
163
164static void read_hw_ecc(void __iomem *docptr, uint8_t *ecc_buf)
165{
166 /* read the 7 hw-generated ecc bytes */
167
168 int i;
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));
172 }
173}
174
175static int correct_data(struct mtd_info *mtd, uint8_t *buf, int page)
176{
177 /*
178 * Called after a page read when hardware reports bitflips.
179 * Up to four bitflips can be corrected.
180 */
181
182 struct nand_chip *nand = mtd->priv;
183 struct docg4_priv *doc = nand->priv;
184 void __iomem *docptr = CONFIG_SYS_NAND_BASE;
185 int i, numerrs;
186 unsigned int errpos[4];
187 const uint8_t blank_read_hwecc[8] = {
188 0xcf, 0x72, 0xfc, 0x1b, 0xa9, 0xc7, 0xb9, 0 };
189
190 read_hw_ecc(docptr, doc->ecc_buf); /* read 7 hw-generated ecc bytes */
191
192 /* check if read error is due to a blank page */
193 if (!memcmp(doc->ecc_buf, blank_read_hwecc, 7))
194 return 0; /* yes */
195
196 /* skip additional check of "written flag" if ignore_badblocks */
197 if (!ignore_badblocks) {
198 /*
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.
205 */
206
207 if (doc->oob_buf[15]) {
208 int bit, numsetbits = 0;
209 unsigned long written_flag = doc->oob_buf[15];
210
211 for (bit = 0; bit < 8; bit++) {
212 if (written_flag & 0x01)
213 numsetbits++;
214 written_flag >>= 1;
215 }
216 if (numsetbits > 4) { /* assume blank */
217 printf("errors in blank page at offset %08x\n",
218 page * DOCG4_PAGE_SIZE);
219 return 0;
220 }
221 }
222 }
223
224 /*
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!
230 */
231 for (i = 0; i < 7; i++)
232 doc->ecc_buf[i] = bitrev8(doc->ecc_buf[i]);
233
234 numerrs = decode_bch(doc->bch, NULL, DOCG4_USERDATA_LEN, NULL,
235 doc->ecc_buf, NULL, errpos);
236
237 if (numerrs == -EBADMSG) {
238 printf("uncorrectable errors at offset %08x\n",
239 page * DOCG4_PAGE_SIZE);
240 return -EBADMSG;
241 }
242
243 BUG_ON(numerrs < 0); /* -EINVAL, or anything other than -EBADMSG */
244
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));
248
249 /* fix the errors */
250 for (i = 0; i < numerrs; i++) {
251 /* ignore if error within oob ecc bytes */
252 if (errpos[i] > DOCG4_USERDATA_LEN * 8)
253 continue;
254
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);
259
260 else /* error in page data */
261 __change_bit(errpos[i], (unsigned long *)buf);
262 }
263
264 printf("%d error(s) corrected at offset %08x\n",
265 numerrs, page * DOCG4_PAGE_SIZE);
266
267 return numerrs;
268}
269
270static int read_progstatus(struct docg4_priv *doc, void __iomem *docptr)
271{
272 /*
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.
276 */
277
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);
282
283 MTDDEBUG(MTD_DEBUG_LEVEL3, "docg4: %s: %02x %02x %02x\n",
284 __func__, status1, status2, status3);
285
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);
292 return -EIO;
293 }
294 return 0;
295}
296
297static int pageprog(struct mtd_info *mtd)
298{
299 /*
300 * Final step in writing a page. Writes the contents of its
301 * internal buffer out to the flash array, or some such.
302 */
303
304 struct nand_chip *nand = mtd->priv;
305 struct docg4_priv *doc = nand->priv;
306 void __iomem *docptr = CONFIG_SYS_NAND_BASE;
307 int retval = 0;
308
309 MTDDEBUG(MTD_DEBUG_LEVEL3, "docg4: %s\n", __func__);
310
311 writew(DOCG4_SEQ_PAGEPROG, docptr + DOC_FLASHSEQUENCE);
312 writew(DOC_CMD_PROG_CYCLE2, docptr + DOC_FLASHCOMMAND);
313 write_nop(docptr);
314 write_nop(docptr);
315
316 /* Just busy-wait; usleep_range() slows things down noticeably. */
317 poll_status(docptr);
318
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);
322 write_nop(docptr);
323 write_nop(docptr);
324 write_nop(docptr);
325 write_nop(docptr);
326 write_nop(docptr);
327
328 retval = read_progstatus(doc, docptr);
329 writew(0, docptr + DOC_DATAEND);
330 write_nop(docptr);
331 poll_status(docptr);
332 write_nop(docptr);
333
334 return retval;
335}
336
337static void sequence_reset(void __iomem *docptr)
338{
339 /* common starting sequence for all operations */
340
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);
344 write_nop(docptr);
345 write_nop(docptr);
346 poll_status(docptr);
347 write_nop(docptr);
348}
349
350static void read_page_prologue(void __iomem *docptr, uint32_t docg4_addr)
351{
352 /* first step in reading a page */
353
354 sequence_reset(docptr);
355
356 writew(DOCG4_SEQ_PAGE_READ, docptr + DOC_FLASHSEQUENCE);
357 writew(DOCG4_CMD_PAGE_READ, docptr + DOC_FLASHCOMMAND);
358 write_nop(docptr);
359
360 write_addr(docptr, docg4_addr);
361
362 write_nop(docptr);
363 writew(DOCG4_CMD_READ2, docptr + DOC_FLASHCOMMAND);
364 write_nop(docptr);
365 write_nop(docptr);
366
367 poll_status(docptr);
368}
369
370static void write_page_prologue(void __iomem *docptr, uint32_t docg4_addr)
371{
372 /* first step in writing a page */
373
374 sequence_reset(docptr);
375 writew(DOCG4_SEQ_PAGEWRITE, docptr + DOC_FLASHSEQUENCE);
376 writew(DOCG4_CMD_PAGEWRITE, docptr + DOC_FLASHCOMMAND);
377 write_nop(docptr);
378 write_addr(docptr, docg4_addr);
379 write_nop(docptr);
380 write_nop(docptr);
381 poll_status(docptr);
382}
383
384static uint32_t mtd_to_docg4_address(int page, int column)
385{
386 /*
387 * Convert mtd address to format used by the device, 32 bit packed.
388 *
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.
395 *
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
403 * addressed.
404 *
405 * This requires us to convert addresses passed by the mtd nand
406 * infrastructure code to those used by the device.
407 *
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.
414 */
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 */
418}
419
420static void docg4_command(struct mtd_info *mtd, unsigned command, int column,
421 int page_addr)
422{
423 /* handle standard nand commands */
424
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);
428
429 MTDDEBUG(MTD_DEBUG_LEVEL3, "%s %x, page_addr=%x, column=%x\n",
430 __func__, command, page_addr, column);
431
432 /*
433 * Save the command and its arguments. This enables emulation of
434 * standard flash devices, and also some optimizations.
435 */
436 doc->last_command.command = command;
437 doc->last_command.column = column;
438 doc->last_command.page = page_addr;
439
440 switch (command) {
441 case NAND_CMD_RESET:
442 reset(CONFIG_SYS_NAND_BASE);
443 break;
444
445 case NAND_CMD_READ0:
446 read_page_prologue(CONFIG_SYS_NAND_BASE, g4_addr);
447 break;
448
449 case NAND_CMD_STATUS:
450 /* next call to read_byte() will expect a status */
451 break;
452
453 case NAND_CMD_SEQIN:
454 write_page_prologue(CONFIG_SYS_NAND_BASE, g4_addr);
455
456 /* hack for deferred write of oob bytes */
457 if (doc->oob_page == page_addr)
458 memcpy(nand->oob_poi, doc->oob_buf, 16);
459 break;
460
461 case NAND_CMD_PAGEPROG:
462 pageprog(mtd);
463 break;
464
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",
471 command);
472 break;
473 }
474}
475
476static void docg4_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
477{
478 int i;
479 struct nand_chip *nand = mtd->priv;
480 uint16_t *p = (uint16_t *)buf;
481 len >>= 1;
482
483 for (i = 0; i < len; i++)
484 p[i] = readw(nand->IO_ADDR_R);
485}
486
487static int docg4_read_oob(struct mtd_info *mtd, struct nand_chip *nand,
Mike Dunn211bf202013-06-17 10:44:55 -0700488 int page)
Mike Dunn956b03e2013-04-12 11:59:18 -0700489{
490 struct docg4_priv *doc = nand->priv;
491 void __iomem *docptr = CONFIG_SYS_NAND_BASE;
492 uint16_t status;
493
494 MTDDEBUG(MTD_DEBUG_LEVEL3, "%s: page %x\n", __func__, page);
495
496 /*
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
500 * separate oob read.
501 */
502 if (doc->last_command.command == NAND_CMD_READ0 &&
503 doc->last_command.page == page) {
504 memcpy(nand->oob_poi, doc->oob_buf, 16);
505 return 0;
506 }
507
508 /*
509 * Separate read of oob data only.
510 */
511 docg4_command(mtd, NAND_CMD_READ0, nand->ecc.size, page);
512
513 writew(DOC_ECCCONF0_READ_MODE | DOCG4_OOB_SIZE, docptr + DOC_ECCCONF0);
514 write_nop(docptr);
515 write_nop(docptr);
516 write_nop(docptr);
517 write_nop(docptr);
518 write_nop(docptr);
519
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);
524 return -EIO;
525 }
526
527 MTDDEBUG(MTD_DEBUG_LEVEL3, "%s: status = 0x%x\n", __func__, status);
528
529 docg4_read_buf(mtd, nand->oob_poi, 16);
530
531 write_nop(docptr);
532 write_nop(docptr);
533 write_nop(docptr);
534 writew(0, docptr + DOC_DATAEND);
535 write_nop(docptr);
536
537 return 0;
538}
539
540static int docg4_write_oob(struct mtd_info *mtd, struct nand_chip *nand,
541 int page)
542{
543 /*
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.
552 */
553
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);
558 return 0;
559}
560
561static int docg4_block_neverbad(struct mtd_info *mtd, loff_t ofs, int getchip)
562{
563 /* only called when module_param ignore_badblocks is set */
564 return 0;
565}
566
567static void docg4_write_buf16(struct mtd_info *mtd, const uint8_t *buf, int len)
568{
569 int i;
570 struct nand_chip *nand = mtd->priv;
571 uint16_t *p = (uint16_t *)buf;
572 len >>= 1;
573
574 for (i = 0; i < len; i++)
575 writew(p[i], nand->IO_ADDR_W);
576}
577
Mike Dunn211bf202013-06-17 10:44:55 -0700578static int write_page(struct mtd_info *mtd, struct nand_chip *nand,
Mike Dunn956b03e2013-04-12 11:59:18 -0700579 const uint8_t *buf, int use_ecc)
580{
581 void __iomem *docptr = CONFIG_SYS_NAND_BASE;
582 uint8_t ecc_buf[8];
583
584 writew(DOC_ECCCONF0_ECC_ENABLE |
585 DOC_ECCCONF0_UNKNOWN |
586 DOCG4_BCH_SIZE,
587 docptr + DOC_ECCCONF0);
588 write_nop(docptr);
589
590 /* write the page data */
591 docg4_write_buf16(mtd, buf, DOCG4_PAGE_SIZE);
592
593 /* oob bytes 0 through 5 are written to I/O reg */
594 docg4_write_buf16(mtd, nand->oob_poi, 6);
595
596 /* oob byte 6 written to a separate reg */
597 writew(nand->oob_poi[6], docptr + DOCG4_OOB_6_7);
598
599 write_nop(docptr);
600 write_nop(docptr);
601
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);
608 write_nop(docptr);
609
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 */
613 }
614
615 /* write user-supplied bytes to oob */
616 else {
617 writew(nand->oob_poi[7], docptr + DOCG4_OOB_6_7);
618 write_nop(docptr);
619 memcpy(ecc_buf, &nand->oob_poi[8], 8);
620 }
621
622 docg4_write_buf16(mtd, ecc_buf, 8);
623 write_nop(docptr);
624 write_nop(docptr);
625 writew(0, docptr + DOC_DATAEND);
626 write_nop(docptr);
Mike Dunn211bf202013-06-17 10:44:55 -0700627
628 return 0;
Mike Dunn956b03e2013-04-12 11:59:18 -0700629}
630
Mike Dunn211bf202013-06-17 10:44:55 -0700631static int docg4_write_page_raw(struct mtd_info *mtd, struct nand_chip *nand,
632 const uint8_t *buf, int oob_required)
Mike Dunn956b03e2013-04-12 11:59:18 -0700633{
634 return write_page(mtd, nand, buf, 0);
635}
636
Mike Dunn211bf202013-06-17 10:44:55 -0700637static int docg4_write_page(struct mtd_info *mtd, struct nand_chip *nand,
638 const uint8_t *buf, int oob_required)
Mike Dunn956b03e2013-04-12 11:59:18 -0700639{
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,
Mike Dunn211bf202013-06-17 10:44:55 -0700709 uint8_t *buf, int oob_required, int page)
Mike Dunn956b03e2013-04-12 11:59:18 -0700710{
711 return read_page(mtd, nand, buf, page, 0);
712}
713
714static int docg4_read_page(struct mtd_info *mtd, struct nand_chip *nand,
Mike Dunn211bf202013-06-17 10:44:55 -0700715 uint8_t *buf, int oob_required, int page)
Mike Dunn956b03e2013-04-12 11:59:18 -0700716{
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);
Mike Dunn211bf202013-06-17 10:44:55 -0700782 status = docg4_read_page(mtd, nand, buf, 0, DOCG4_FACTORY_BBT_PAGE);
Mike Dunn956b03e2013-04-12 11:59:18 -0700783 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);
Mike Dunn211bf202013-06-17 10:44:55 -0700861 docg4_write_page(mtd, nand, buf, 1);
Mike Dunn956b03e2013-04-12 11:59:18 -0700862 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;
Mike Dunn211bf202013-06-17 10:44:55 -0700962 nand->ecc.strength = DOCG4_T;
963 nand->options = NAND_BUSWIDTH_16 | NAND_NO_SUBPAGE_WRITE;
Mike Dunn956b03e2013-04-12 11:59:18 -0700964 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}