blob: 54b1e5e359b84f9686a3942001bc2896649ffa7a [file] [log] [blame]
Mike Dunn570469f2012-01-03 16:05:44 -08001/*
2 * Copyright © 2012 Mike Dunn <mikedunn@newsguy.com>
3 *
4 * mtd nand driver for M-Systems DiskOnChip G4
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
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#include <linux/kernel.h>
32#include <linux/slab.h>
33#include <linux/init.h>
34#include <linux/string.h>
35#include <linux/sched.h>
36#include <linux/delay.h>
37#include <linux/module.h>
38#include <linux/export.h>
39#include <linux/platform_device.h>
40#include <linux/io.h>
41#include <linux/bitops.h>
42#include <linux/mtd/partitions.h>
43#include <linux/mtd/mtd.h>
44#include <linux/mtd/nand.h>
45#include <linux/bch.h>
46#include <linux/bitrev.h>
47
48/*
Mike Dunn5a90d412012-12-07 12:07:21 -080049 * In "reliable mode" consecutive 2k pages are used in parallel (in some
50 * fashion) to store the same data. The data can be read back from the
51 * even-numbered pages in the normal manner; odd-numbered pages will appear to
52 * contain junk. Systems that boot from the docg4 typically write the secondary
53 * program loader (SPL) code in this mode. The SPL is loaded by the initial
54 * program loader (IPL, stored in the docg4's 2k NOR-like region that is mapped
55 * to the reset vector address). This module parameter enables you to use this
56 * driver to write the SPL. When in this mode, no more than 2k of data can be
57 * written at a time, because the addresses do not increment in the normal
58 * manner, and the starting offset must be within an even-numbered 2k region;
59 * i.e., invalid starting offsets are 0x800, 0xa00, 0xc00, 0xe00, 0x1800,
60 * 0x1a00, ... Reliable mode is a special case and should not be used unless
61 * you know what you're doing.
62 */
63static bool reliable_mode;
64module_param(reliable_mode, bool, 0);
65MODULE_PARM_DESC(reliable_mode, "pages are programmed in reliable mode");
66
67/*
Mike Dunn570469f2012-01-03 16:05:44 -080068 * You'll want to ignore badblocks if you're reading a partition that contains
69 * data written by the TrueFFS library (i.e., by PalmOS, Windows, etc), since
70 * it does not use mtd nand's method for marking bad blocks (using oob area).
71 * This will also skip the check of the "page written" flag.
72 */
73static bool ignore_badblocks;
74module_param(ignore_badblocks, bool, 0);
75MODULE_PARM_DESC(ignore_badblocks, "no badblock checking performed");
76
77struct docg4_priv {
78 struct mtd_info *mtd;
79 struct device *dev;
80 void __iomem *virtadr;
81 int status;
82 struct {
83 unsigned int command;
84 int column;
85 int page;
86 } last_command;
87 uint8_t oob_buf[16];
88 uint8_t ecc_buf[7];
89 int oob_page;
90 struct bch_control *bch;
91};
92
93/*
94 * Defines prefixed with DOCG4 are unique to the diskonchip G4. All others are
95 * shared with other diskonchip devices (P3, G3 at least).
96 *
97 * Functions with names prefixed with docg4_ are mtd / nand interface functions
98 * (though they may also be called internally). All others are internal.
99 */
100
101#define DOC_IOSPACE_DATA 0x0800
102
103/* register offsets */
104#define DOC_CHIPID 0x1000
105#define DOC_DEVICESELECT 0x100a
106#define DOC_ASICMODE 0x100c
107#define DOC_DATAEND 0x101e
108#define DOC_NOP 0x103e
109
110#define DOC_FLASHSEQUENCE 0x1032
111#define DOC_FLASHCOMMAND 0x1034
112#define DOC_FLASHADDRESS 0x1036
113#define DOC_FLASHCONTROL 0x1038
114#define DOC_ECCCONF0 0x1040
115#define DOC_ECCCONF1 0x1042
116#define DOC_HAMMINGPARITY 0x1046
117#define DOC_BCH_SYNDROM(idx) (0x1048 + idx)
118
119#define DOC_ASICMODECONFIRM 0x1072
120#define DOC_CHIPID_INV 0x1074
121#define DOC_POWERMODE 0x107c
122
123#define DOCG4_MYSTERY_REG 0x1050
124
125/* apparently used only to write oob bytes 6 and 7 */
126#define DOCG4_OOB_6_7 0x1052
127
128/* DOC_FLASHSEQUENCE register commands */
129#define DOC_SEQ_RESET 0x00
130#define DOCG4_SEQ_PAGE_READ 0x03
131#define DOCG4_SEQ_FLUSH 0x29
132#define DOCG4_SEQ_PAGEWRITE 0x16
133#define DOCG4_SEQ_PAGEPROG 0x1e
134#define DOCG4_SEQ_BLOCKERASE 0x24
Mike Dunn5a90d412012-12-07 12:07:21 -0800135#define DOCG4_SEQ_SETMODE 0x45
Mike Dunn570469f2012-01-03 16:05:44 -0800136
137/* DOC_FLASHCOMMAND register commands */
138#define DOCG4_CMD_PAGE_READ 0x00
139#define DOC_CMD_ERASECYCLE2 0xd0
140#define DOCG4_CMD_FLUSH 0x70
141#define DOCG4_CMD_READ2 0x30
142#define DOC_CMD_PROG_BLOCK_ADDR 0x60
143#define DOCG4_CMD_PAGEWRITE 0x80
144#define DOC_CMD_PROG_CYCLE2 0x10
Mike Dunn5a90d412012-12-07 12:07:21 -0800145#define DOCG4_CMD_FAST_MODE 0xa3 /* functionality guessed */
146#define DOC_CMD_RELIABLE_MODE 0x22
Mike Dunn570469f2012-01-03 16:05:44 -0800147#define DOC_CMD_RESET 0xff
148
149/* DOC_POWERMODE register bits */
150#define DOC_POWERDOWN_READY 0x80
151
152/* DOC_FLASHCONTROL register bits */
153#define DOC_CTRL_CE 0x10
154#define DOC_CTRL_UNKNOWN 0x40
155#define DOC_CTRL_FLASHREADY 0x01
156
157/* DOC_ECCCONF0 register bits */
158#define DOC_ECCCONF0_READ_MODE 0x8000
159#define DOC_ECCCONF0_UNKNOWN 0x2000
160#define DOC_ECCCONF0_ECC_ENABLE 0x1000
161#define DOC_ECCCONF0_DATA_BYTES_MASK 0x07ff
162
163/* DOC_ECCCONF1 register bits */
164#define DOC_ECCCONF1_BCH_SYNDROM_ERR 0x80
165#define DOC_ECCCONF1_ECC_ENABLE 0x07
166#define DOC_ECCCONF1_PAGE_IS_WRITTEN 0x20
167
168/* DOC_ASICMODE register bits */
169#define DOC_ASICMODE_RESET 0x00
170#define DOC_ASICMODE_NORMAL 0x01
171#define DOC_ASICMODE_POWERDOWN 0x02
172#define DOC_ASICMODE_MDWREN 0x04
173#define DOC_ASICMODE_BDETCT_RESET 0x08
174#define DOC_ASICMODE_RSTIN_RESET 0x10
175#define DOC_ASICMODE_RAM_WE 0x20
176
177/* good status values read after read/write/erase operations */
178#define DOCG4_PROGSTATUS_GOOD 0x51
179#define DOCG4_PROGSTATUS_GOOD_2 0xe0
180
181/*
182 * On read operations (page and oob-only), the first byte read from I/O reg is a
183 * status. On error, it reads 0x73; otherwise, it reads either 0x71 (first read
184 * after reset only) or 0x51, so bit 1 is presumed to be an error indicator.
185 */
186#define DOCG4_READ_ERROR 0x02 /* bit 1 indicates read error */
187
188/* anatomy of the device */
189#define DOCG4_CHIP_SIZE 0x8000000
190#define DOCG4_PAGE_SIZE 0x200
191#define DOCG4_PAGES_PER_BLOCK 0x200
192#define DOCG4_BLOCK_SIZE (DOCG4_PAGES_PER_BLOCK * DOCG4_PAGE_SIZE)
193#define DOCG4_NUMBLOCKS (DOCG4_CHIP_SIZE / DOCG4_BLOCK_SIZE)
194#define DOCG4_OOB_SIZE 0x10
195#define DOCG4_CHIP_SHIFT 27 /* log_2(DOCG4_CHIP_SIZE) */
196#define DOCG4_PAGE_SHIFT 9 /* log_2(DOCG4_PAGE_SIZE) */
197#define DOCG4_ERASE_SHIFT 18 /* log_2(DOCG4_BLOCK_SIZE) */
198
199/* all but the last byte is included in ecc calculation */
200#define DOCG4_BCH_SIZE (DOCG4_PAGE_SIZE + DOCG4_OOB_SIZE - 1)
201
202#define DOCG4_USERDATA_LEN 520 /* 512 byte page plus 8 oob avail to user */
203
204/* expected values from the ID registers */
205#define DOCG4_IDREG1_VALUE 0x0400
206#define DOCG4_IDREG2_VALUE 0xfbff
207
208/* primitive polynomial used to build the Galois field used by hw ecc gen */
209#define DOCG4_PRIMITIVE_POLY 0x4443
210
211#define DOCG4_M 14 /* Galois field is of order 2^14 */
212#define DOCG4_T 4 /* BCH alg corrects up to 4 bit errors */
213
214#define DOCG4_FACTORY_BBT_PAGE 16 /* page where read-only factory bbt lives */
215
216/*
217 * Oob bytes 0 - 6 are available to the user.
218 * Byte 7 is hamming ecc for first 7 bytes. Bytes 8 - 14 are hw-generated ecc.
219 * Byte 15 (the last) is used by the driver as a "page written" flag.
220 */
221static struct nand_ecclayout docg4_oobinfo = {
222 .eccbytes = 9,
223 .eccpos = {7, 8, 9, 10, 11, 12, 13, 14, 15},
224 .oobavail = 7,
225 .oobfree = { {0, 7} }
226};
227
228/*
229 * The device has a nop register which M-Sys claims is for the purpose of
230 * inserting precise delays. But beware; at least some operations fail if the
231 * nop writes are replaced with a generic delay!
232 */
233static inline void write_nop(void __iomem *docptr)
234{
235 writew(0, docptr + DOC_NOP);
236}
237
238static void docg4_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
239{
240 int i;
241 struct nand_chip *nand = mtd->priv;
242 uint16_t *p = (uint16_t *) buf;
243 len >>= 1;
244
245 for (i = 0; i < len; i++)
246 p[i] = readw(nand->IO_ADDR_R);
247}
248
249static void docg4_write_buf16(struct mtd_info *mtd, const uint8_t *buf, int len)
250{
251 int i;
252 struct nand_chip *nand = mtd->priv;
253 uint16_t *p = (uint16_t *) buf;
254 len >>= 1;
255
256 for (i = 0; i < len; i++)
257 writew(p[i], nand->IO_ADDR_W);
258}
259
260static int poll_status(struct docg4_priv *doc)
261{
262 /*
263 * Busy-wait for the FLASHREADY bit to be set in the FLASHCONTROL
264 * register. Operations known to take a long time (e.g., block erase)
265 * should sleep for a while before calling this.
266 */
267
268 uint16_t flash_status;
269 unsigned int timeo;
270 void __iomem *docptr = doc->virtadr;
271
272 dev_dbg(doc->dev, "%s...\n", __func__);
273
274 /* hardware quirk requires reading twice initially */
275 flash_status = readw(docptr + DOC_FLASHCONTROL);
276
277 timeo = 1000;
278 do {
279 cpu_relax();
280 flash_status = readb(docptr + DOC_FLASHCONTROL);
281 } while (!(flash_status & DOC_CTRL_FLASHREADY) && --timeo);
282
283
284 if (!timeo) {
285 dev_err(doc->dev, "%s: timed out!\n", __func__);
286 return NAND_STATUS_FAIL;
287 }
288
289 if (unlikely(timeo < 50))
290 dev_warn(doc->dev, "%s: nearly timed out; %d remaining\n",
291 __func__, timeo);
292
293 return 0;
294}
295
296
297static int docg4_wait(struct mtd_info *mtd, struct nand_chip *nand)
298{
299
300 struct docg4_priv *doc = nand->priv;
301 int status = NAND_STATUS_WP; /* inverse logic?? */
302 dev_dbg(doc->dev, "%s...\n", __func__);
303
304 /* report any previously unreported error */
305 if (doc->status) {
306 status |= doc->status;
307 doc->status = 0;
308 return status;
309 }
310
311 status |= poll_status(doc);
312 return status;
313}
314
315static void docg4_select_chip(struct mtd_info *mtd, int chip)
316{
317 /*
318 * Select among multiple cascaded chips ("floors"). Multiple floors are
319 * not yet supported, so the only valid non-negative value is 0.
320 */
321 struct nand_chip *nand = mtd->priv;
322 struct docg4_priv *doc = nand->priv;
323 void __iomem *docptr = doc->virtadr;
324
325 dev_dbg(doc->dev, "%s: chip %d\n", __func__, chip);
326
327 if (chip < 0)
328 return; /* deselected */
329
330 if (chip > 0)
331 dev_warn(doc->dev, "multiple floors currently unsupported\n");
332
333 writew(0, docptr + DOC_DEVICESELECT);
334}
335
336static void reset(struct mtd_info *mtd)
337{
338 /* full device reset */
339
340 struct nand_chip *nand = mtd->priv;
341 struct docg4_priv *doc = nand->priv;
342 void __iomem *docptr = doc->virtadr;
343
344 writew(DOC_ASICMODE_RESET | DOC_ASICMODE_MDWREN,
345 docptr + DOC_ASICMODE);
346 writew(~(DOC_ASICMODE_RESET | DOC_ASICMODE_MDWREN),
347 docptr + DOC_ASICMODECONFIRM);
348 write_nop(docptr);
349
350 writew(DOC_ASICMODE_NORMAL | DOC_ASICMODE_MDWREN,
351 docptr + DOC_ASICMODE);
352 writew(~(DOC_ASICMODE_NORMAL | DOC_ASICMODE_MDWREN),
353 docptr + DOC_ASICMODECONFIRM);
354
355 writew(DOC_ECCCONF1_ECC_ENABLE, docptr + DOC_ECCCONF1);
356
357 poll_status(doc);
358}
359
360static void read_hw_ecc(void __iomem *docptr, uint8_t *ecc_buf)
361{
362 /* read the 7 hw-generated ecc bytes */
363
364 int i;
365 for (i = 0; i < 7; i++) { /* hw quirk; read twice */
366 ecc_buf[i] = readb(docptr + DOC_BCH_SYNDROM(i));
367 ecc_buf[i] = readb(docptr + DOC_BCH_SYNDROM(i));
368 }
369}
370
371static int correct_data(struct mtd_info *mtd, uint8_t *buf, int page)
372{
373 /*
374 * Called after a page read when hardware reports bitflips.
375 * Up to four bitflips can be corrected.
376 */
377
378 struct nand_chip *nand = mtd->priv;
379 struct docg4_priv *doc = nand->priv;
380 void __iomem *docptr = doc->virtadr;
381 int i, numerrs, errpos[4];
382 const uint8_t blank_read_hwecc[8] = {
383 0xcf, 0x72, 0xfc, 0x1b, 0xa9, 0xc7, 0xb9, 0 };
384
385 read_hw_ecc(docptr, doc->ecc_buf); /* read 7 hw-generated ecc bytes */
386
387 /* check if read error is due to a blank page */
388 if (!memcmp(doc->ecc_buf, blank_read_hwecc, 7))
389 return 0; /* yes */
390
391 /* skip additional check of "written flag" if ignore_badblocks */
392 if (ignore_badblocks == false) {
393
394 /*
395 * If the hw ecc bytes are not those of a blank page, there's
396 * still a chance that the page is blank, but was read with
397 * errors. Check the "written flag" in last oob byte, which
398 * is set to zero when a page is written. If more than half
399 * the bits are set, assume a blank page. Unfortunately, the
400 * bit flips(s) are not reported in stats.
401 */
402
Mike Dunnaa6d01f2012-07-11 11:08:19 -0700403 if (nand->oob_poi[15]) {
Mike Dunn570469f2012-01-03 16:05:44 -0800404 int bit, numsetbits = 0;
Mike Dunnaa6d01f2012-07-11 11:08:19 -0700405 unsigned long written_flag = nand->oob_poi[15];
Mike Dunn570469f2012-01-03 16:05:44 -0800406 for_each_set_bit(bit, &written_flag, 8)
407 numsetbits++;
408 if (numsetbits > 4) { /* assume blank */
409 dev_warn(doc->dev,
410 "error(s) in blank page "
411 "at offset %08x\n",
412 page * DOCG4_PAGE_SIZE);
413 return 0;
414 }
415 }
416 }
417
418 /*
419 * The hardware ecc unit produces oob_ecc ^ calc_ecc. The kernel's bch
420 * algorithm is used to decode this. However the hw operates on page
421 * data in a bit order that is the reverse of that of the bch alg,
422 * requiring that the bits be reversed on the result. Thanks to Ivan
423 * Djelic for his analysis!
424 */
425 for (i = 0; i < 7; i++)
426 doc->ecc_buf[i] = bitrev8(doc->ecc_buf[i]);
427
428 numerrs = decode_bch(doc->bch, NULL, DOCG4_USERDATA_LEN, NULL,
429 doc->ecc_buf, NULL, errpos);
430
431 if (numerrs == -EBADMSG) {
432 dev_warn(doc->dev, "uncorrectable errors at offset %08x\n",
433 page * DOCG4_PAGE_SIZE);
434 return -EBADMSG;
435 }
436
437 BUG_ON(numerrs < 0); /* -EINVAL, or anything other than -EBADMSG */
438
439 /* undo last step in BCH alg (modulo mirroring not needed) */
440 for (i = 0; i < numerrs; i++)
441 errpos[i] = (errpos[i] & ~7)|(7-(errpos[i] & 7));
442
443 /* fix the errors */
444 for (i = 0; i < numerrs; i++) {
445
446 /* ignore if error within oob ecc bytes */
447 if (errpos[i] > DOCG4_USERDATA_LEN * 8)
448 continue;
449
450 /* if error within oob area preceeding ecc bytes... */
451 if (errpos[i] > DOCG4_PAGE_SIZE * 8)
452 change_bit(errpos[i] - DOCG4_PAGE_SIZE * 8,
Mike Dunnaa6d01f2012-07-11 11:08:19 -0700453 (unsigned long *)nand->oob_poi);
Mike Dunn570469f2012-01-03 16:05:44 -0800454
455 else /* error in page data */
456 change_bit(errpos[i], (unsigned long *)buf);
457 }
458
459 dev_notice(doc->dev, "%d error(s) corrected at offset %08x\n",
460 numerrs, page * DOCG4_PAGE_SIZE);
461
462 return numerrs;
463}
464
465static uint8_t docg4_read_byte(struct mtd_info *mtd)
466{
467 struct nand_chip *nand = mtd->priv;
468 struct docg4_priv *doc = nand->priv;
469
470 dev_dbg(doc->dev, "%s\n", __func__);
471
472 if (doc->last_command.command == NAND_CMD_STATUS) {
473 int status;
474
475 /*
476 * Previous nand command was status request, so nand
477 * infrastructure code expects to read the status here. If an
478 * error occurred in a previous operation, report it.
479 */
480 doc->last_command.command = 0;
481
482 if (doc->status) {
483 status = doc->status;
484 doc->status = 0;
485 }
486
487 /* why is NAND_STATUS_WP inverse logic?? */
488 else
489 status = NAND_STATUS_WP | NAND_STATUS_READY;
490
491 return status;
492 }
493
494 dev_warn(doc->dev, "unexpectd call to read_byte()\n");
495
496 return 0;
497}
498
499static void write_addr(struct docg4_priv *doc, uint32_t docg4_addr)
500{
501 /* write the four address bytes packed in docg4_addr to the device */
502
503 void __iomem *docptr = doc->virtadr;
504 writeb(docg4_addr & 0xff, docptr + DOC_FLASHADDRESS);
505 docg4_addr >>= 8;
506 writeb(docg4_addr & 0xff, docptr + DOC_FLASHADDRESS);
507 docg4_addr >>= 8;
508 writeb(docg4_addr & 0xff, docptr + DOC_FLASHADDRESS);
509 docg4_addr >>= 8;
510 writeb(docg4_addr & 0xff, docptr + DOC_FLASHADDRESS);
511}
512
513static int read_progstatus(struct docg4_priv *doc)
514{
515 /*
516 * This apparently checks the status of programming. Done after an
517 * erasure, and after page data is written. On error, the status is
518 * saved, to be later retrieved by the nand infrastructure code.
519 */
520 void __iomem *docptr = doc->virtadr;
521
522 /* status is read from the I/O reg */
523 uint16_t status1 = readw(docptr + DOC_IOSPACE_DATA);
524 uint16_t status2 = readw(docptr + DOC_IOSPACE_DATA);
525 uint16_t status3 = readw(docptr + DOCG4_MYSTERY_REG);
526
527 dev_dbg(doc->dev, "docg4: %s: %02x %02x %02x\n",
528 __func__, status1, status2, status3);
529
530 if (status1 != DOCG4_PROGSTATUS_GOOD
531 || status2 != DOCG4_PROGSTATUS_GOOD_2
532 || status3 != DOCG4_PROGSTATUS_GOOD_2) {
533 doc->status = NAND_STATUS_FAIL;
534 dev_warn(doc->dev, "read_progstatus failed: "
535 "%02x, %02x, %02x\n", status1, status2, status3);
536 return -EIO;
537 }
538 return 0;
539}
540
541static int pageprog(struct mtd_info *mtd)
542{
543 /*
544 * Final step in writing a page. Writes the contents of its
545 * internal buffer out to the flash array, or some such.
546 */
547
548 struct nand_chip *nand = mtd->priv;
549 struct docg4_priv *doc = nand->priv;
550 void __iomem *docptr = doc->virtadr;
551 int retval = 0;
552
553 dev_dbg(doc->dev, "docg4: %s\n", __func__);
554
555 writew(DOCG4_SEQ_PAGEPROG, docptr + DOC_FLASHSEQUENCE);
556 writew(DOC_CMD_PROG_CYCLE2, docptr + DOC_FLASHCOMMAND);
557 write_nop(docptr);
558 write_nop(docptr);
559
560 /* Just busy-wait; usleep_range() slows things down noticeably. */
561 poll_status(doc);
562
563 writew(DOCG4_SEQ_FLUSH, docptr + DOC_FLASHSEQUENCE);
564 writew(DOCG4_CMD_FLUSH, docptr + DOC_FLASHCOMMAND);
565 writew(DOC_ECCCONF0_READ_MODE | 4, docptr + DOC_ECCCONF0);
566 write_nop(docptr);
567 write_nop(docptr);
568 write_nop(docptr);
569 write_nop(docptr);
570 write_nop(docptr);
571
572 retval = read_progstatus(doc);
573 writew(0, docptr + DOC_DATAEND);
574 write_nop(docptr);
575 poll_status(doc);
576 write_nop(docptr);
577
578 return retval;
579}
580
581static void sequence_reset(struct mtd_info *mtd)
582{
583 /* common starting sequence for all operations */
584
585 struct nand_chip *nand = mtd->priv;
586 struct docg4_priv *doc = nand->priv;
587 void __iomem *docptr = doc->virtadr;
588
589 writew(DOC_CTRL_UNKNOWN | DOC_CTRL_CE, docptr + DOC_FLASHCONTROL);
590 writew(DOC_SEQ_RESET, docptr + DOC_FLASHSEQUENCE);
591 writew(DOC_CMD_RESET, docptr + DOC_FLASHCOMMAND);
592 write_nop(docptr);
593 write_nop(docptr);
594 poll_status(doc);
595 write_nop(docptr);
596}
597
598static void read_page_prologue(struct mtd_info *mtd, uint32_t docg4_addr)
599{
600 /* first step in reading a page */
601
602 struct nand_chip *nand = mtd->priv;
603 struct docg4_priv *doc = nand->priv;
604 void __iomem *docptr = doc->virtadr;
605
606 dev_dbg(doc->dev,
607 "docg4: %s: g4 page %08x\n", __func__, docg4_addr);
608
609 sequence_reset(mtd);
610
611 writew(DOCG4_SEQ_PAGE_READ, docptr + DOC_FLASHSEQUENCE);
612 writew(DOCG4_CMD_PAGE_READ, docptr + DOC_FLASHCOMMAND);
613 write_nop(docptr);
614
615 write_addr(doc, docg4_addr);
616
617 write_nop(docptr);
618 writew(DOCG4_CMD_READ2, docptr + DOC_FLASHCOMMAND);
619 write_nop(docptr);
620 write_nop(docptr);
621
622 poll_status(doc);
623}
624
625static void write_page_prologue(struct mtd_info *mtd, uint32_t docg4_addr)
626{
627 /* first step in writing a page */
628
629 struct nand_chip *nand = mtd->priv;
630 struct docg4_priv *doc = nand->priv;
631 void __iomem *docptr = doc->virtadr;
632
633 dev_dbg(doc->dev,
634 "docg4: %s: g4 addr: %x\n", __func__, docg4_addr);
635 sequence_reset(mtd);
Mike Dunn5a90d412012-12-07 12:07:21 -0800636
637 if (unlikely(reliable_mode)) {
638 writew(DOCG4_SEQ_SETMODE, docptr + DOC_FLASHSEQUENCE);
639 writew(DOCG4_CMD_FAST_MODE, docptr + DOC_FLASHCOMMAND);
640 writew(DOC_CMD_RELIABLE_MODE, docptr + DOC_FLASHCOMMAND);
641 write_nop(docptr);
642 }
643
Mike Dunn570469f2012-01-03 16:05:44 -0800644 writew(DOCG4_SEQ_PAGEWRITE, docptr + DOC_FLASHSEQUENCE);
645 writew(DOCG4_CMD_PAGEWRITE, docptr + DOC_FLASHCOMMAND);
646 write_nop(docptr);
647 write_addr(doc, docg4_addr);
648 write_nop(docptr);
649 write_nop(docptr);
650 poll_status(doc);
651}
652
653static uint32_t mtd_to_docg4_address(int page, int column)
654{
655 /*
656 * Convert mtd address to format used by the device, 32 bit packed.
657 *
658 * Some notes on G4 addressing... The M-Sys documentation on this device
659 * claims that pages are 2K in length, and indeed, the format of the
660 * address used by the device reflects that. But within each page are
661 * four 512 byte "sub-pages", each with its own oob data that is
662 * read/written immediately after the 512 bytes of page data. This oob
663 * data contains the ecc bytes for the preceeding 512 bytes.
664 *
665 * Rather than tell the mtd nand infrastructure that page size is 2k,
666 * with four sub-pages each, we engage in a little subterfuge and tell
667 * the infrastructure code that pages are 512 bytes in size. This is
668 * done because during the course of reverse-engineering the device, I
669 * never observed an instance where an entire 2K "page" was read or
670 * written as a unit. Each "sub-page" is always addressed individually,
671 * its data read/written, and ecc handled before the next "sub-page" is
672 * addressed.
673 *
674 * This requires us to convert addresses passed by the mtd nand
675 * infrastructure code to those used by the device.
676 *
677 * The address that is written to the device consists of four bytes: the
678 * first two are the 2k page number, and the second is the index into
679 * the page. The index is in terms of 16-bit half-words and includes
680 * the preceeding oob data, so e.g., the index into the second
681 * "sub-page" is 0x108, and the full device address of the start of mtd
682 * page 0x201 is 0x00800108.
683 */
684 int g4_page = page / 4; /* device's 2K page */
685 int g4_index = (page % 4) * 0x108 + column/2; /* offset into page */
686 return (g4_page << 16) | g4_index; /* pack */
687}
688
689static void docg4_command(struct mtd_info *mtd, unsigned command, int column,
690 int page_addr)
691{
692 /* handle standard nand commands */
693
694 struct nand_chip *nand = mtd->priv;
695 struct docg4_priv *doc = nand->priv;
696 uint32_t g4_addr = mtd_to_docg4_address(page_addr, column);
697
698 dev_dbg(doc->dev, "%s %x, page_addr=%x, column=%x\n",
699 __func__, command, page_addr, column);
700
701 /*
702 * Save the command and its arguments. This enables emulation of
703 * standard flash devices, and also some optimizations.
704 */
705 doc->last_command.command = command;
706 doc->last_command.column = column;
707 doc->last_command.page = page_addr;
708
709 switch (command) {
710
711 case NAND_CMD_RESET:
712 reset(mtd);
713 break;
714
715 case NAND_CMD_READ0:
716 read_page_prologue(mtd, g4_addr);
717 break;
718
719 case NAND_CMD_STATUS:
720 /* next call to read_byte() will expect a status */
721 break;
722
723 case NAND_CMD_SEQIN:
Mike Dunn5a90d412012-12-07 12:07:21 -0800724 if (unlikely(reliable_mode)) {
725 uint16_t g4_page = g4_addr >> 16;
726
727 /* writes to odd-numbered 2k pages are invalid */
728 if (g4_page & 0x01)
729 dev_warn(doc->dev,
730 "invalid reliable mode address\n");
731 }
732
Mike Dunn570469f2012-01-03 16:05:44 -0800733 write_page_prologue(mtd, g4_addr);
734
735 /* hack for deferred write of oob bytes */
736 if (doc->oob_page == page_addr)
737 memcpy(nand->oob_poi, doc->oob_buf, 16);
738 break;
739
740 case NAND_CMD_PAGEPROG:
741 pageprog(mtd);
742 break;
743
744 /* we don't expect these, based on review of nand_base.c */
745 case NAND_CMD_READOOB:
746 case NAND_CMD_READID:
747 case NAND_CMD_ERASE1:
748 case NAND_CMD_ERASE2:
749 dev_warn(doc->dev, "docg4_command: "
750 "unexpected nand command 0x%x\n", command);
751 break;
752
753 }
754}
755
756static int read_page(struct mtd_info *mtd, struct nand_chip *nand,
757 uint8_t *buf, int page, bool use_ecc)
758{
759 struct docg4_priv *doc = nand->priv;
760 void __iomem *docptr = doc->virtadr;
761 uint16_t status, edc_err, *buf16;
Mike Dunn3f91e942012-04-25 12:06:09 -0700762 int bits_corrected = 0;
Mike Dunn570469f2012-01-03 16:05:44 -0800763
764 dev_dbg(doc->dev, "%s: page %08x\n", __func__, page);
765
766 writew(DOC_ECCCONF0_READ_MODE |
767 DOC_ECCCONF0_ECC_ENABLE |
768 DOC_ECCCONF0_UNKNOWN |
769 DOCG4_BCH_SIZE,
770 docptr + DOC_ECCCONF0);
771 write_nop(docptr);
772 write_nop(docptr);
773 write_nop(docptr);
774 write_nop(docptr);
775 write_nop(docptr);
776
777 /* the 1st byte from the I/O reg is a status; the rest is page data */
778 status = readw(docptr + DOC_IOSPACE_DATA);
779 if (status & DOCG4_READ_ERROR) {
780 dev_err(doc->dev,
781 "docg4_read_page: bad status: 0x%02x\n", status);
782 writew(0, docptr + DOC_DATAEND);
783 return -EIO;
784 }
785
786 dev_dbg(doc->dev, "%s: status = 0x%x\n", __func__, status);
787
788 docg4_read_buf(mtd, buf, DOCG4_PAGE_SIZE); /* read the page data */
789
Mike Dunnaa6d01f2012-07-11 11:08:19 -0700790 /* this device always reads oob after page data */
Mike Dunn570469f2012-01-03 16:05:44 -0800791 /* first 14 oob bytes read from I/O reg */
Mike Dunnaa6d01f2012-07-11 11:08:19 -0700792 docg4_read_buf(mtd, nand->oob_poi, 14);
Mike Dunn570469f2012-01-03 16:05:44 -0800793
794 /* last 2 read from another reg */
Mike Dunnaa6d01f2012-07-11 11:08:19 -0700795 buf16 = (uint16_t *)(nand->oob_poi + 14);
Mike Dunn570469f2012-01-03 16:05:44 -0800796 *buf16 = readw(docptr + DOCG4_MYSTERY_REG);
797
798 write_nop(docptr);
799
800 if (likely(use_ecc == true)) {
801
802 /* read the register that tells us if bitflip(s) detected */
803 edc_err = readw(docptr + DOC_ECCCONF1);
804 edc_err = readw(docptr + DOC_ECCCONF1);
805 dev_dbg(doc->dev, "%s: edc_err = 0x%02x\n", __func__, edc_err);
806
807 /* If bitflips are reported, attempt to correct with ecc */
808 if (edc_err & DOC_ECCCONF1_BCH_SYNDROM_ERR) {
Mike Dunn3f91e942012-04-25 12:06:09 -0700809 bits_corrected = correct_data(mtd, buf, page);
Mike Dunn570469f2012-01-03 16:05:44 -0800810 if (bits_corrected == -EBADMSG)
811 mtd->ecc_stats.failed++;
812 else
813 mtd->ecc_stats.corrected += bits_corrected;
814 }
815 }
816
817 writew(0, docptr + DOC_DATAEND);
Mike Dunn5bf3d662012-09-11 08:50:50 -0700818 if (bits_corrected == -EBADMSG) /* uncorrectable errors */
819 return 0;
Mike Dunn3f91e942012-04-25 12:06:09 -0700820 return bits_corrected;
Mike Dunn570469f2012-01-03 16:05:44 -0800821}
822
823
824static int docg4_read_page_raw(struct mtd_info *mtd, struct nand_chip *nand,
Brian Norris1fbb9382012-05-02 10:14:55 -0700825 uint8_t *buf, int oob_required, int page)
Mike Dunn570469f2012-01-03 16:05:44 -0800826{
827 return read_page(mtd, nand, buf, page, false);
828}
829
830static int docg4_read_page(struct mtd_info *mtd, struct nand_chip *nand,
Brian Norris1fbb9382012-05-02 10:14:55 -0700831 uint8_t *buf, int oob_required, int page)
Mike Dunn570469f2012-01-03 16:05:44 -0800832{
833 return read_page(mtd, nand, buf, page, true);
834}
835
836static int docg4_read_oob(struct mtd_info *mtd, struct nand_chip *nand,
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +0300837 int page)
Mike Dunn570469f2012-01-03 16:05:44 -0800838{
839 struct docg4_priv *doc = nand->priv;
840 void __iomem *docptr = doc->virtadr;
841 uint16_t status;
842
843 dev_dbg(doc->dev, "%s: page %x\n", __func__, page);
844
Mike Dunn570469f2012-01-03 16:05:44 -0800845 docg4_command(mtd, NAND_CMD_READ0, nand->ecc.size, page);
846
847 writew(DOC_ECCCONF0_READ_MODE | DOCG4_OOB_SIZE, docptr + DOC_ECCCONF0);
848 write_nop(docptr);
849 write_nop(docptr);
850 write_nop(docptr);
851 write_nop(docptr);
852 write_nop(docptr);
853
854 /* the 1st byte from the I/O reg is a status; the rest is oob data */
855 status = readw(docptr + DOC_IOSPACE_DATA);
856 if (status & DOCG4_READ_ERROR) {
857 dev_warn(doc->dev,
858 "docg4_read_oob failed: status = 0x%02x\n", status);
859 return -EIO;
860 }
861
862 dev_dbg(doc->dev, "%s: status = 0x%x\n", __func__, status);
863
864 docg4_read_buf(mtd, nand->oob_poi, 16);
865
866 write_nop(docptr);
867 write_nop(docptr);
868 write_nop(docptr);
869 writew(0, docptr + DOC_DATAEND);
870 write_nop(docptr);
871
872 return 0;
873}
874
875static void docg4_erase_block(struct mtd_info *mtd, int page)
876{
877 struct nand_chip *nand = mtd->priv;
878 struct docg4_priv *doc = nand->priv;
879 void __iomem *docptr = doc->virtadr;
880 uint16_t g4_page;
881
882 dev_dbg(doc->dev, "%s: page %04x\n", __func__, page);
883
884 sequence_reset(mtd);
885
886 writew(DOCG4_SEQ_BLOCKERASE, docptr + DOC_FLASHSEQUENCE);
887 writew(DOC_CMD_PROG_BLOCK_ADDR, docptr + DOC_FLASHCOMMAND);
888 write_nop(docptr);
889
890 /* only 2 bytes of address are written to specify erase block */
891 g4_page = (uint16_t)(page / 4); /* to g4's 2k page addressing */
892 writeb(g4_page & 0xff, docptr + DOC_FLASHADDRESS);
893 g4_page >>= 8;
894 writeb(g4_page & 0xff, docptr + DOC_FLASHADDRESS);
895 write_nop(docptr);
896
897 /* start the erasure */
898 writew(DOC_CMD_ERASECYCLE2, docptr + DOC_FLASHCOMMAND);
899 write_nop(docptr);
900 write_nop(docptr);
901
902 usleep_range(500, 1000); /* erasure is long; take a snooze */
903 poll_status(doc);
904 writew(DOCG4_SEQ_FLUSH, docptr + DOC_FLASHSEQUENCE);
905 writew(DOCG4_CMD_FLUSH, docptr + DOC_FLASHCOMMAND);
906 writew(DOC_ECCCONF0_READ_MODE | 4, docptr + DOC_ECCCONF0);
907 write_nop(docptr);
908 write_nop(docptr);
909 write_nop(docptr);
910 write_nop(docptr);
911 write_nop(docptr);
912
913 read_progstatus(doc);
914
915 writew(0, docptr + DOC_DATAEND);
916 write_nop(docptr);
917 poll_status(doc);
918 write_nop(docptr);
919}
920
Josh Wufdbad98d2012-06-25 18:07:45 +0800921static int write_page(struct mtd_info *mtd, struct nand_chip *nand,
Mike Dunn570469f2012-01-03 16:05:44 -0800922 const uint8_t *buf, bool use_ecc)
923{
924 struct docg4_priv *doc = nand->priv;
925 void __iomem *docptr = doc->virtadr;
926 uint8_t ecc_buf[8];
927
928 dev_dbg(doc->dev, "%s...\n", __func__);
929
930 writew(DOC_ECCCONF0_ECC_ENABLE |
931 DOC_ECCCONF0_UNKNOWN |
932 DOCG4_BCH_SIZE,
933 docptr + DOC_ECCCONF0);
934 write_nop(docptr);
935
936 /* write the page data */
937 docg4_write_buf16(mtd, buf, DOCG4_PAGE_SIZE);
938
939 /* oob bytes 0 through 5 are written to I/O reg */
940 docg4_write_buf16(mtd, nand->oob_poi, 6);
941
942 /* oob byte 6 written to a separate reg */
943 writew(nand->oob_poi[6], docptr + DOCG4_OOB_6_7);
944
945 write_nop(docptr);
946 write_nop(docptr);
947
948 /* write hw-generated ecc bytes to oob */
949 if (likely(use_ecc == true)) {
950 /* oob byte 7 is hamming code */
951 uint8_t hamming = readb(docptr + DOC_HAMMINGPARITY);
952 hamming = readb(docptr + DOC_HAMMINGPARITY); /* 2nd read */
953 writew(hamming, docptr + DOCG4_OOB_6_7);
954 write_nop(docptr);
955
956 /* read the 7 bch bytes from ecc regs */
957 read_hw_ecc(docptr, ecc_buf);
958 ecc_buf[7] = 0; /* clear the "page written" flag */
959 }
960
961 /* write user-supplied bytes to oob */
962 else {
963 writew(nand->oob_poi[7], docptr + DOCG4_OOB_6_7);
964 write_nop(docptr);
965 memcpy(ecc_buf, &nand->oob_poi[8], 8);
966 }
967
968 docg4_write_buf16(mtd, ecc_buf, 8);
969 write_nop(docptr);
970 write_nop(docptr);
971 writew(0, docptr + DOC_DATAEND);
972 write_nop(docptr);
Josh Wufdbad98d2012-06-25 18:07:45 +0800973
974 return 0;
Mike Dunn570469f2012-01-03 16:05:44 -0800975}
976
Josh Wufdbad98d2012-06-25 18:07:45 +0800977static int docg4_write_page_raw(struct mtd_info *mtd, struct nand_chip *nand,
Brian Norris1fbb9382012-05-02 10:14:55 -0700978 const uint8_t *buf, int oob_required)
Mike Dunn570469f2012-01-03 16:05:44 -0800979{
980 return write_page(mtd, nand, buf, false);
981}
982
Josh Wufdbad98d2012-06-25 18:07:45 +0800983static int docg4_write_page(struct mtd_info *mtd, struct nand_chip *nand,
Brian Norris1fbb9382012-05-02 10:14:55 -0700984 const uint8_t *buf, int oob_required)
Mike Dunn570469f2012-01-03 16:05:44 -0800985{
986 return write_page(mtd, nand, buf, true);
987}
988
989static int docg4_write_oob(struct mtd_info *mtd, struct nand_chip *nand,
990 int page)
991{
992 /*
993 * Writing oob-only is not really supported, because MLC nand must write
994 * oob bytes at the same time as page data. Nonetheless, we save the
995 * oob buffer contents here, and then write it along with the page data
996 * if the same page is subsequently written. This allows user space
997 * utilities that write the oob data prior to the page data to work
998 * (e.g., nandwrite). The disdvantage is that, if the intention was to
999 * write oob only, the operation is quietly ignored. Also, oob can get
1000 * corrupted if two concurrent processes are running nandwrite.
1001 */
1002
1003 /* note that bytes 7..14 are hw generated hamming/ecc and overwritten */
1004 struct docg4_priv *doc = nand->priv;
1005 doc->oob_page = page;
1006 memcpy(doc->oob_buf, nand->oob_poi, 16);
1007 return 0;
1008}
1009
1010static int __init read_factory_bbt(struct mtd_info *mtd)
1011{
1012 /*
1013 * The device contains a read-only factory bad block table. Read it and
1014 * update the memory-based bbt accordingly.
1015 */
1016
1017 struct nand_chip *nand = mtd->priv;
1018 struct docg4_priv *doc = nand->priv;
1019 uint32_t g4_addr = mtd_to_docg4_address(DOCG4_FACTORY_BBT_PAGE, 0);
1020 uint8_t *buf;
1021 int i, block, status;
1022
1023 buf = kzalloc(DOCG4_PAGE_SIZE, GFP_KERNEL);
1024 if (buf == NULL)
1025 return -ENOMEM;
1026
1027 read_page_prologue(mtd, g4_addr);
Brian Norris1fbb9382012-05-02 10:14:55 -07001028 status = docg4_read_page(mtd, nand, buf, 0, DOCG4_FACTORY_BBT_PAGE);
Mike Dunn570469f2012-01-03 16:05:44 -08001029 if (status)
1030 goto exit;
1031
1032 /*
1033 * If no memory-based bbt was created, exit. This will happen if module
1034 * parameter ignore_badblocks is set. Then why even call this function?
1035 * For an unknown reason, block erase always fails if it's the first
1036 * operation after device power-up. The above read ensures it never is.
1037 * Ugly, I know.
1038 */
1039 if (nand->bbt == NULL) /* no memory-based bbt */
1040 goto exit;
1041
1042 /*
1043 * Parse factory bbt and update memory-based bbt. Factory bbt format is
1044 * simple: one bit per block, block numbers increase left to right (msb
1045 * to lsb). Bit clear means bad block.
1046 */
1047 for (i = block = 0; block < DOCG4_NUMBLOCKS; block += 8, i++) {
1048 int bitnum;
1049 unsigned long bits = ~buf[i];
1050 for_each_set_bit(bitnum, &bits, 8) {
1051 int badblock = block + 7 - bitnum;
1052 nand->bbt[badblock / 4] |=
1053 0x03 << ((badblock % 4) * 2);
1054 mtd->ecc_stats.badblocks++;
1055 dev_notice(doc->dev, "factory-marked bad block: %d\n",
1056 badblock);
1057 }
1058 }
1059 exit:
1060 kfree(buf);
1061 return status;
1062}
1063
1064static int docg4_block_markbad(struct mtd_info *mtd, loff_t ofs)
1065{
1066 /*
1067 * Mark a block as bad. Bad blocks are marked in the oob area of the
1068 * first page of the block. The default scan_bbt() in the nand
1069 * infrastructure code works fine for building the memory-based bbt
1070 * during initialization, as does the nand infrastructure function that
1071 * checks if a block is bad by reading the bbt. This function replaces
1072 * the nand default because writes to oob-only are not supported.
1073 */
1074
1075 int ret, i;
1076 uint8_t *buf;
1077 struct nand_chip *nand = mtd->priv;
1078 struct docg4_priv *doc = nand->priv;
1079 struct nand_bbt_descr *bbtd = nand->badblock_pattern;
1080 int block = (int)(ofs >> nand->bbt_erase_shift);
1081 int page = (int)(ofs >> nand->page_shift);
1082 uint32_t g4_addr = mtd_to_docg4_address(page, 0);
1083
1084 dev_dbg(doc->dev, "%s: %08llx\n", __func__, ofs);
1085
1086 if (unlikely(ofs & (DOCG4_BLOCK_SIZE - 1)))
1087 dev_warn(doc->dev, "%s: ofs %llx not start of block!\n",
1088 __func__, ofs);
1089
1090 /* allocate blank buffer for page data */
1091 buf = kzalloc(DOCG4_PAGE_SIZE, GFP_KERNEL);
1092 if (buf == NULL)
1093 return -ENOMEM;
1094
1095 /* update bbt in memory */
1096 nand->bbt[block / 4] |= 0x01 << ((block & 0x03) * 2);
1097
1098 /* write bit-wise negation of pattern to oob buffer */
1099 memset(nand->oob_poi, 0xff, mtd->oobsize);
1100 for (i = 0; i < bbtd->len; i++)
1101 nand->oob_poi[bbtd->offs + i] = ~bbtd->pattern[i];
1102
1103 /* write first page of block */
1104 write_page_prologue(mtd, g4_addr);
Brian Norris1fbb9382012-05-02 10:14:55 -07001105 docg4_write_page(mtd, nand, buf, 1);
Mike Dunn570469f2012-01-03 16:05:44 -08001106 ret = pageprog(mtd);
1107 if (!ret)
1108 mtd->ecc_stats.badblocks++;
1109
1110 kfree(buf);
1111
1112 return ret;
1113}
1114
1115static int docg4_block_neverbad(struct mtd_info *mtd, loff_t ofs, int getchip)
1116{
1117 /* only called when module_param ignore_badblocks is set */
1118 return 0;
1119}
1120
1121static int docg4_suspend(struct platform_device *pdev, pm_message_t state)
1122{
1123 /*
1124 * Put the device into "deep power-down" mode. Note that CE# must be
1125 * deasserted for this to take effect. The xscale, e.g., can be
1126 * configured to float this signal when the processor enters power-down,
1127 * and a suitable pull-up ensures its deassertion.
1128 */
1129
1130 int i;
1131 uint8_t pwr_down;
1132 struct docg4_priv *doc = platform_get_drvdata(pdev);
1133 void __iomem *docptr = doc->virtadr;
1134
1135 dev_dbg(doc->dev, "%s...\n", __func__);
1136
1137 /* poll the register that tells us we're ready to go to sleep */
1138 for (i = 0; i < 10; i++) {
1139 pwr_down = readb(docptr + DOC_POWERMODE);
1140 if (pwr_down & DOC_POWERDOWN_READY)
1141 break;
1142 usleep_range(1000, 4000);
1143 }
1144
1145 if (pwr_down & DOC_POWERDOWN_READY) {
1146 dev_err(doc->dev, "suspend failed; "
1147 "timeout polling DOC_POWERDOWN_READY\n");
1148 return -EIO;
1149 }
1150
1151 writew(DOC_ASICMODE_POWERDOWN | DOC_ASICMODE_MDWREN,
1152 docptr + DOC_ASICMODE);
1153 writew(~(DOC_ASICMODE_POWERDOWN | DOC_ASICMODE_MDWREN),
1154 docptr + DOC_ASICMODECONFIRM);
1155
1156 write_nop(docptr);
1157
1158 return 0;
1159}
1160
1161static int docg4_resume(struct platform_device *pdev)
1162{
1163
1164 /*
1165 * Exit power-down. Twelve consecutive reads of the address below
1166 * accomplishes this, assuming CE# has been asserted.
1167 */
1168
1169 struct docg4_priv *doc = platform_get_drvdata(pdev);
1170 void __iomem *docptr = doc->virtadr;
1171 int i;
1172
1173 dev_dbg(doc->dev, "%s...\n", __func__);
1174
1175 for (i = 0; i < 12; i++)
1176 readb(docptr + 0x1fff);
1177
1178 return 0;
1179}
1180
1181static void __init init_mtd_structs(struct mtd_info *mtd)
1182{
1183 /* initialize mtd and nand data structures */
1184
1185 /*
1186 * Note that some of the following initializations are not usually
1187 * required within a nand driver because they are performed by the nand
1188 * infrastructure code as part of nand_scan(). In this case they need
1189 * to be initialized here because we skip call to nand_scan_ident() (the
1190 * first half of nand_scan()). The call to nand_scan_ident() is skipped
1191 * because for this device the chip id is not read in the manner of a
1192 * standard nand device. Unfortunately, nand_scan_ident() does other
1193 * things as well, such as call nand_set_defaults().
1194 */
1195
1196 struct nand_chip *nand = mtd->priv;
1197 struct docg4_priv *doc = nand->priv;
1198
1199 mtd->size = DOCG4_CHIP_SIZE;
1200 mtd->name = "Msys_Diskonchip_G4";
1201 mtd->writesize = DOCG4_PAGE_SIZE;
1202 mtd->erasesize = DOCG4_BLOCK_SIZE;
1203 mtd->oobsize = DOCG4_OOB_SIZE;
1204 nand->chipsize = DOCG4_CHIP_SIZE;
1205 nand->chip_shift = DOCG4_CHIP_SHIFT;
1206 nand->bbt_erase_shift = nand->phys_erase_shift = DOCG4_ERASE_SHIFT;
1207 nand->chip_delay = 20;
1208 nand->page_shift = DOCG4_PAGE_SHIFT;
1209 nand->pagemask = 0x3ffff;
1210 nand->badblockpos = NAND_LARGE_BADBLOCK_POS;
1211 nand->badblockbits = 8;
1212 nand->ecc.layout = &docg4_oobinfo;
1213 nand->ecc.mode = NAND_ECC_HW_SYNDROME;
1214 nand->ecc.size = DOCG4_PAGE_SIZE;
1215 nand->ecc.prepad = 8;
1216 nand->ecc.bytes = 8;
Mike Dunn6a918ba2012-03-11 14:21:11 -07001217 nand->ecc.strength = DOCG4_T;
Brian Norris1826dbc2012-05-01 17:12:55 -07001218 nand->options = NAND_BUSWIDTH_16 | NAND_NO_SUBPAGE_WRITE;
Mike Dunn570469f2012-01-03 16:05:44 -08001219 nand->IO_ADDR_R = nand->IO_ADDR_W = doc->virtadr + DOC_IOSPACE_DATA;
1220 nand->controller = &nand->hwcontrol;
1221 spin_lock_init(&nand->controller->lock);
1222 init_waitqueue_head(&nand->controller->wq);
1223
1224 /* methods */
1225 nand->cmdfunc = docg4_command;
1226 nand->waitfunc = docg4_wait;
1227 nand->select_chip = docg4_select_chip;
1228 nand->read_byte = docg4_read_byte;
1229 nand->block_markbad = docg4_block_markbad;
1230 nand->read_buf = docg4_read_buf;
1231 nand->write_buf = docg4_write_buf16;
1232 nand->scan_bbt = nand_default_bbt;
1233 nand->erase_cmd = docg4_erase_block;
1234 nand->ecc.read_page = docg4_read_page;
1235 nand->ecc.write_page = docg4_write_page;
1236 nand->ecc.read_page_raw = docg4_read_page_raw;
1237 nand->ecc.write_page_raw = docg4_write_page_raw;
1238 nand->ecc.read_oob = docg4_read_oob;
1239 nand->ecc.write_oob = docg4_write_oob;
1240
1241 /*
1242 * The way the nand infrastructure code is written, a memory-based bbt
1243 * is not created if NAND_SKIP_BBTSCAN is set. With no memory bbt,
1244 * nand->block_bad() is used. So when ignoring bad blocks, we skip the
1245 * scan and define a dummy block_bad() which always returns 0.
1246 */
1247 if (ignore_badblocks) {
1248 nand->options |= NAND_SKIP_BBTSCAN;
1249 nand->block_bad = docg4_block_neverbad;
1250 }
1251
1252}
1253
1254static int __init read_id_reg(struct mtd_info *mtd)
1255{
1256 struct nand_chip *nand = mtd->priv;
1257 struct docg4_priv *doc = nand->priv;
1258 void __iomem *docptr = doc->virtadr;
1259 uint16_t id1, id2;
1260
1261 /* check for presence of g4 chip by reading id registers */
1262 id1 = readw(docptr + DOC_CHIPID);
1263 id1 = readw(docptr + DOCG4_MYSTERY_REG);
1264 id2 = readw(docptr + DOC_CHIPID_INV);
1265 id2 = readw(docptr + DOCG4_MYSTERY_REG);
1266
1267 if (id1 == DOCG4_IDREG1_VALUE && id2 == DOCG4_IDREG2_VALUE) {
1268 dev_info(doc->dev,
1269 "NAND device: 128MiB Diskonchip G4 detected\n");
1270 return 0;
1271 }
1272
1273 return -ENODEV;
1274}
1275
1276static char const *part_probes[] = { "cmdlinepart", "saftlpart", NULL };
1277
1278static int __init probe_docg4(struct platform_device *pdev)
1279{
1280 struct mtd_info *mtd;
1281 struct nand_chip *nand;
1282 void __iomem *virtadr;
1283 struct docg4_priv *doc;
1284 int len, retval;
1285 struct resource *r;
1286 struct device *dev = &pdev->dev;
1287
1288 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1289 if (r == NULL) {
1290 dev_err(dev, "no io memory resource defined!\n");
1291 return -ENODEV;
1292 }
1293
1294 virtadr = ioremap(r->start, resource_size(r));
1295 if (!virtadr) {
Dan Carpenter2c4ae272012-01-31 11:54:06 +03001296 dev_err(dev, "Diskonchip ioremap failed: %pR\n", r);
Mike Dunn570469f2012-01-03 16:05:44 -08001297 return -EIO;
1298 }
1299
1300 len = sizeof(struct mtd_info) + sizeof(struct nand_chip) +
1301 sizeof(struct docg4_priv);
1302 mtd = kzalloc(len, GFP_KERNEL);
1303 if (mtd == NULL) {
1304 retval = -ENOMEM;
1305 goto fail;
1306 }
1307 nand = (struct nand_chip *) (mtd + 1);
1308 doc = (struct docg4_priv *) (nand + 1);
1309 mtd->priv = nand;
1310 nand->priv = doc;
1311 mtd->owner = THIS_MODULE;
1312 doc->virtadr = virtadr;
1313 doc->dev = dev;
1314
1315 init_mtd_structs(mtd);
1316
1317 /* initialize kernel bch algorithm */
1318 doc->bch = init_bch(DOCG4_M, DOCG4_T, DOCG4_PRIMITIVE_POLY);
1319 if (doc->bch == NULL) {
1320 retval = -EINVAL;
1321 goto fail;
1322 }
1323
1324 platform_set_drvdata(pdev, doc);
1325
1326 reset(mtd);
1327 retval = read_id_reg(mtd);
1328 if (retval == -ENODEV) {
1329 dev_warn(dev, "No diskonchip G4 device found.\n");
1330 goto fail;
1331 }
1332
1333 retval = nand_scan_tail(mtd);
1334 if (retval)
1335 goto fail;
1336
1337 retval = read_factory_bbt(mtd);
1338 if (retval)
1339 goto fail;
1340
1341 retval = mtd_device_parse_register(mtd, part_probes, NULL, NULL, 0);
1342 if (retval)
1343 goto fail;
1344
1345 doc->mtd = mtd;
1346 return 0;
1347
1348 fail:
1349 iounmap(virtadr);
1350 if (mtd) {
1351 /* re-declarations avoid compiler warning */
1352 struct nand_chip *nand = mtd->priv;
1353 struct docg4_priv *doc = nand->priv;
1354 nand_release(mtd); /* deletes partitions and mtd devices */
1355 platform_set_drvdata(pdev, NULL);
1356 free_bch(doc->bch);
1357 kfree(mtd);
1358 }
1359
1360 return retval;
1361}
1362
1363static int __exit cleanup_docg4(struct platform_device *pdev)
1364{
1365 struct docg4_priv *doc = platform_get_drvdata(pdev);
1366 nand_release(doc->mtd);
1367 platform_set_drvdata(pdev, NULL);
1368 free_bch(doc->bch);
1369 kfree(doc->mtd);
1370 iounmap(doc->virtadr);
1371 return 0;
1372}
1373
1374static struct platform_driver docg4_driver = {
1375 .driver = {
1376 .name = "docg4",
1377 .owner = THIS_MODULE,
1378 },
1379 .suspend = docg4_suspend,
1380 .resume = docg4_resume,
1381 .remove = __exit_p(cleanup_docg4),
1382};
1383
1384static int __init docg4_init(void)
1385{
1386 return platform_driver_probe(&docg4_driver, probe_docg4);
1387}
1388
1389static void __exit docg4_exit(void)
1390{
1391 platform_driver_unregister(&docg4_driver);
1392}
1393
1394module_init(docg4_init);
1395module_exit(docg4_exit);
1396
1397MODULE_LICENSE("GPL");
1398MODULE_AUTHOR("Mike Dunn");
1399MODULE_DESCRIPTION("M-Systems DiskOnChip G4 device driver");