blob: 04e5fa93735e32a2e2581da8576235918ce40453 [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/*
Mike Dunn440b1d72012-12-07 12:07:22 -0800217 * Bytes 0, 1 are used as badblock marker.
218 * Bytes 2 - 6 are available to the user.
219 * Byte 7 is hamming ecc for first 7 oob bytes only.
220 * Bytes 8 - 14 are hw-generated ecc covering entire page + oob bytes 0 - 14.
Mike Dunn570469f2012-01-03 16:05:44 -0800221 * Byte 15 (the last) is used by the driver as a "page written" flag.
222 */
223static struct nand_ecclayout docg4_oobinfo = {
224 .eccbytes = 9,
225 .eccpos = {7, 8, 9, 10, 11, 12, 13, 14, 15},
Mike Dunn440b1d72012-12-07 12:07:22 -0800226 .oobavail = 5,
227 .oobfree = { {.offset = 2, .length = 5} }
Mike Dunn570469f2012-01-03 16:05:44 -0800228};
229
230/*
231 * The device has a nop register which M-Sys claims is for the purpose of
232 * inserting precise delays. But beware; at least some operations fail if the
233 * nop writes are replaced with a generic delay!
234 */
235static inline void write_nop(void __iomem *docptr)
236{
237 writew(0, docptr + DOC_NOP);
238}
239
240static void docg4_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
241{
242 int i;
243 struct nand_chip *nand = mtd->priv;
244 uint16_t *p = (uint16_t *) buf;
245 len >>= 1;
246
247 for (i = 0; i < len; i++)
248 p[i] = readw(nand->IO_ADDR_R);
249}
250
251static void docg4_write_buf16(struct mtd_info *mtd, const uint8_t *buf, int len)
252{
253 int i;
254 struct nand_chip *nand = mtd->priv;
255 uint16_t *p = (uint16_t *) buf;
256 len >>= 1;
257
258 for (i = 0; i < len; i++)
259 writew(p[i], nand->IO_ADDR_W);
260}
261
262static int poll_status(struct docg4_priv *doc)
263{
264 /*
265 * Busy-wait for the FLASHREADY bit to be set in the FLASHCONTROL
266 * register. Operations known to take a long time (e.g., block erase)
267 * should sleep for a while before calling this.
268 */
269
270 uint16_t flash_status;
271 unsigned int timeo;
272 void __iomem *docptr = doc->virtadr;
273
274 dev_dbg(doc->dev, "%s...\n", __func__);
275
276 /* hardware quirk requires reading twice initially */
277 flash_status = readw(docptr + DOC_FLASHCONTROL);
278
279 timeo = 1000;
280 do {
281 cpu_relax();
282 flash_status = readb(docptr + DOC_FLASHCONTROL);
283 } while (!(flash_status & DOC_CTRL_FLASHREADY) && --timeo);
284
285
286 if (!timeo) {
287 dev_err(doc->dev, "%s: timed out!\n", __func__);
288 return NAND_STATUS_FAIL;
289 }
290
291 if (unlikely(timeo < 50))
292 dev_warn(doc->dev, "%s: nearly timed out; %d remaining\n",
293 __func__, timeo);
294
295 return 0;
296}
297
298
299static int docg4_wait(struct mtd_info *mtd, struct nand_chip *nand)
300{
301
302 struct docg4_priv *doc = nand->priv;
303 int status = NAND_STATUS_WP; /* inverse logic?? */
304 dev_dbg(doc->dev, "%s...\n", __func__);
305
306 /* report any previously unreported error */
307 if (doc->status) {
308 status |= doc->status;
309 doc->status = 0;
310 return status;
311 }
312
313 status |= poll_status(doc);
314 return status;
315}
316
317static void docg4_select_chip(struct mtd_info *mtd, int chip)
318{
319 /*
320 * Select among multiple cascaded chips ("floors"). Multiple floors are
321 * not yet supported, so the only valid non-negative value is 0.
322 */
323 struct nand_chip *nand = mtd->priv;
324 struct docg4_priv *doc = nand->priv;
325 void __iomem *docptr = doc->virtadr;
326
327 dev_dbg(doc->dev, "%s: chip %d\n", __func__, chip);
328
329 if (chip < 0)
330 return; /* deselected */
331
332 if (chip > 0)
333 dev_warn(doc->dev, "multiple floors currently unsupported\n");
334
335 writew(0, docptr + DOC_DEVICESELECT);
336}
337
338static void reset(struct mtd_info *mtd)
339{
340 /* full device reset */
341
342 struct nand_chip *nand = mtd->priv;
343 struct docg4_priv *doc = nand->priv;
344 void __iomem *docptr = doc->virtadr;
345
346 writew(DOC_ASICMODE_RESET | DOC_ASICMODE_MDWREN,
347 docptr + DOC_ASICMODE);
348 writew(~(DOC_ASICMODE_RESET | DOC_ASICMODE_MDWREN),
349 docptr + DOC_ASICMODECONFIRM);
350 write_nop(docptr);
351
352 writew(DOC_ASICMODE_NORMAL | DOC_ASICMODE_MDWREN,
353 docptr + DOC_ASICMODE);
354 writew(~(DOC_ASICMODE_NORMAL | DOC_ASICMODE_MDWREN),
355 docptr + DOC_ASICMODECONFIRM);
356
357 writew(DOC_ECCCONF1_ECC_ENABLE, docptr + DOC_ECCCONF1);
358
359 poll_status(doc);
360}
361
362static void read_hw_ecc(void __iomem *docptr, uint8_t *ecc_buf)
363{
364 /* read the 7 hw-generated ecc bytes */
365
366 int i;
367 for (i = 0; i < 7; i++) { /* hw quirk; read twice */
368 ecc_buf[i] = readb(docptr + DOC_BCH_SYNDROM(i));
369 ecc_buf[i] = readb(docptr + DOC_BCH_SYNDROM(i));
370 }
371}
372
373static int correct_data(struct mtd_info *mtd, uint8_t *buf, int page)
374{
375 /*
376 * Called after a page read when hardware reports bitflips.
377 * Up to four bitflips can be corrected.
378 */
379
380 struct nand_chip *nand = mtd->priv;
381 struct docg4_priv *doc = nand->priv;
382 void __iomem *docptr = doc->virtadr;
383 int i, numerrs, errpos[4];
384 const uint8_t blank_read_hwecc[8] = {
385 0xcf, 0x72, 0xfc, 0x1b, 0xa9, 0xc7, 0xb9, 0 };
386
387 read_hw_ecc(docptr, doc->ecc_buf); /* read 7 hw-generated ecc bytes */
388
389 /* check if read error is due to a blank page */
390 if (!memcmp(doc->ecc_buf, blank_read_hwecc, 7))
391 return 0; /* yes */
392
393 /* skip additional check of "written flag" if ignore_badblocks */
394 if (ignore_badblocks == false) {
395
396 /*
397 * If the hw ecc bytes are not those of a blank page, there's
398 * still a chance that the page is blank, but was read with
399 * errors. Check the "written flag" in last oob byte, which
400 * is set to zero when a page is written. If more than half
401 * the bits are set, assume a blank page. Unfortunately, the
402 * bit flips(s) are not reported in stats.
403 */
404
Mike Dunnaa6d01f2012-07-11 11:08:19 -0700405 if (nand->oob_poi[15]) {
Mike Dunn570469f2012-01-03 16:05:44 -0800406 int bit, numsetbits = 0;
Mike Dunnaa6d01f2012-07-11 11:08:19 -0700407 unsigned long written_flag = nand->oob_poi[15];
Mike Dunn570469f2012-01-03 16:05:44 -0800408 for_each_set_bit(bit, &written_flag, 8)
409 numsetbits++;
410 if (numsetbits > 4) { /* assume blank */
411 dev_warn(doc->dev,
412 "error(s) in blank page "
413 "at offset %08x\n",
414 page * DOCG4_PAGE_SIZE);
415 return 0;
416 }
417 }
418 }
419
420 /*
421 * The hardware ecc unit produces oob_ecc ^ calc_ecc. The kernel's bch
422 * algorithm is used to decode this. However the hw operates on page
423 * data in a bit order that is the reverse of that of the bch alg,
424 * requiring that the bits be reversed on the result. Thanks to Ivan
425 * Djelic for his analysis!
426 */
427 for (i = 0; i < 7; i++)
428 doc->ecc_buf[i] = bitrev8(doc->ecc_buf[i]);
429
430 numerrs = decode_bch(doc->bch, NULL, DOCG4_USERDATA_LEN, NULL,
431 doc->ecc_buf, NULL, errpos);
432
433 if (numerrs == -EBADMSG) {
434 dev_warn(doc->dev, "uncorrectable errors at offset %08x\n",
435 page * DOCG4_PAGE_SIZE);
436 return -EBADMSG;
437 }
438
439 BUG_ON(numerrs < 0); /* -EINVAL, or anything other than -EBADMSG */
440
441 /* undo last step in BCH alg (modulo mirroring not needed) */
442 for (i = 0; i < numerrs; i++)
443 errpos[i] = (errpos[i] & ~7)|(7-(errpos[i] & 7));
444
445 /* fix the errors */
446 for (i = 0; i < numerrs; i++) {
447
448 /* ignore if error within oob ecc bytes */
449 if (errpos[i] > DOCG4_USERDATA_LEN * 8)
450 continue;
451
452 /* if error within oob area preceeding ecc bytes... */
453 if (errpos[i] > DOCG4_PAGE_SIZE * 8)
454 change_bit(errpos[i] - DOCG4_PAGE_SIZE * 8,
Mike Dunnaa6d01f2012-07-11 11:08:19 -0700455 (unsigned long *)nand->oob_poi);
Mike Dunn570469f2012-01-03 16:05:44 -0800456
457 else /* error in page data */
458 change_bit(errpos[i], (unsigned long *)buf);
459 }
460
461 dev_notice(doc->dev, "%d error(s) corrected at offset %08x\n",
462 numerrs, page * DOCG4_PAGE_SIZE);
463
464 return numerrs;
465}
466
467static uint8_t docg4_read_byte(struct mtd_info *mtd)
468{
469 struct nand_chip *nand = mtd->priv;
470 struct docg4_priv *doc = nand->priv;
471
472 dev_dbg(doc->dev, "%s\n", __func__);
473
474 if (doc->last_command.command == NAND_CMD_STATUS) {
475 int status;
476
477 /*
478 * Previous nand command was status request, so nand
479 * infrastructure code expects to read the status here. If an
480 * error occurred in a previous operation, report it.
481 */
482 doc->last_command.command = 0;
483
484 if (doc->status) {
485 status = doc->status;
486 doc->status = 0;
487 }
488
489 /* why is NAND_STATUS_WP inverse logic?? */
490 else
491 status = NAND_STATUS_WP | NAND_STATUS_READY;
492
493 return status;
494 }
495
496 dev_warn(doc->dev, "unexpectd call to read_byte()\n");
497
498 return 0;
499}
500
501static void write_addr(struct docg4_priv *doc, uint32_t docg4_addr)
502{
503 /* write the four address bytes packed in docg4_addr to the device */
504
505 void __iomem *docptr = doc->virtadr;
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 docg4_addr >>= 8;
512 writeb(docg4_addr & 0xff, docptr + DOC_FLASHADDRESS);
513}
514
515static int read_progstatus(struct docg4_priv *doc)
516{
517 /*
518 * This apparently checks the status of programming. Done after an
519 * erasure, and after page data is written. On error, the status is
520 * saved, to be later retrieved by the nand infrastructure code.
521 */
522 void __iomem *docptr = doc->virtadr;
523
524 /* status is read from the I/O reg */
525 uint16_t status1 = readw(docptr + DOC_IOSPACE_DATA);
526 uint16_t status2 = readw(docptr + DOC_IOSPACE_DATA);
527 uint16_t status3 = readw(docptr + DOCG4_MYSTERY_REG);
528
529 dev_dbg(doc->dev, "docg4: %s: %02x %02x %02x\n",
530 __func__, status1, status2, status3);
531
532 if (status1 != DOCG4_PROGSTATUS_GOOD
533 || status2 != DOCG4_PROGSTATUS_GOOD_2
534 || status3 != DOCG4_PROGSTATUS_GOOD_2) {
535 doc->status = NAND_STATUS_FAIL;
536 dev_warn(doc->dev, "read_progstatus failed: "
537 "%02x, %02x, %02x\n", status1, status2, status3);
538 return -EIO;
539 }
540 return 0;
541}
542
543static int pageprog(struct mtd_info *mtd)
544{
545 /*
546 * Final step in writing a page. Writes the contents of its
547 * internal buffer out to the flash array, or some such.
548 */
549
550 struct nand_chip *nand = mtd->priv;
551 struct docg4_priv *doc = nand->priv;
552 void __iomem *docptr = doc->virtadr;
553 int retval = 0;
554
555 dev_dbg(doc->dev, "docg4: %s\n", __func__);
556
557 writew(DOCG4_SEQ_PAGEPROG, docptr + DOC_FLASHSEQUENCE);
558 writew(DOC_CMD_PROG_CYCLE2, docptr + DOC_FLASHCOMMAND);
559 write_nop(docptr);
560 write_nop(docptr);
561
562 /* Just busy-wait; usleep_range() slows things down noticeably. */
563 poll_status(doc);
564
565 writew(DOCG4_SEQ_FLUSH, docptr + DOC_FLASHSEQUENCE);
566 writew(DOCG4_CMD_FLUSH, docptr + DOC_FLASHCOMMAND);
567 writew(DOC_ECCCONF0_READ_MODE | 4, docptr + DOC_ECCCONF0);
568 write_nop(docptr);
569 write_nop(docptr);
570 write_nop(docptr);
571 write_nop(docptr);
572 write_nop(docptr);
573
574 retval = read_progstatus(doc);
575 writew(0, docptr + DOC_DATAEND);
576 write_nop(docptr);
577 poll_status(doc);
578 write_nop(docptr);
579
580 return retval;
581}
582
583static void sequence_reset(struct mtd_info *mtd)
584{
585 /* common starting sequence for all operations */
586
587 struct nand_chip *nand = mtd->priv;
588 struct docg4_priv *doc = nand->priv;
589 void __iomem *docptr = doc->virtadr;
590
591 writew(DOC_CTRL_UNKNOWN | DOC_CTRL_CE, docptr + DOC_FLASHCONTROL);
592 writew(DOC_SEQ_RESET, docptr + DOC_FLASHSEQUENCE);
593 writew(DOC_CMD_RESET, docptr + DOC_FLASHCOMMAND);
594 write_nop(docptr);
595 write_nop(docptr);
596 poll_status(doc);
597 write_nop(docptr);
598}
599
600static void read_page_prologue(struct mtd_info *mtd, uint32_t docg4_addr)
601{
602 /* first step in reading a page */
603
604 struct nand_chip *nand = mtd->priv;
605 struct docg4_priv *doc = nand->priv;
606 void __iomem *docptr = doc->virtadr;
607
608 dev_dbg(doc->dev,
609 "docg4: %s: g4 page %08x\n", __func__, docg4_addr);
610
611 sequence_reset(mtd);
612
613 writew(DOCG4_SEQ_PAGE_READ, docptr + DOC_FLASHSEQUENCE);
614 writew(DOCG4_CMD_PAGE_READ, docptr + DOC_FLASHCOMMAND);
615 write_nop(docptr);
616
617 write_addr(doc, docg4_addr);
618
619 write_nop(docptr);
620 writew(DOCG4_CMD_READ2, docptr + DOC_FLASHCOMMAND);
621 write_nop(docptr);
622 write_nop(docptr);
623
624 poll_status(doc);
625}
626
627static void write_page_prologue(struct mtd_info *mtd, uint32_t docg4_addr)
628{
629 /* first step in writing a page */
630
631 struct nand_chip *nand = mtd->priv;
632 struct docg4_priv *doc = nand->priv;
633 void __iomem *docptr = doc->virtadr;
634
635 dev_dbg(doc->dev,
636 "docg4: %s: g4 addr: %x\n", __func__, docg4_addr);
637 sequence_reset(mtd);
Mike Dunn5a90d412012-12-07 12:07:21 -0800638
639 if (unlikely(reliable_mode)) {
640 writew(DOCG4_SEQ_SETMODE, docptr + DOC_FLASHSEQUENCE);
641 writew(DOCG4_CMD_FAST_MODE, docptr + DOC_FLASHCOMMAND);
642 writew(DOC_CMD_RELIABLE_MODE, docptr + DOC_FLASHCOMMAND);
643 write_nop(docptr);
644 }
645
Mike Dunn570469f2012-01-03 16:05:44 -0800646 writew(DOCG4_SEQ_PAGEWRITE, docptr + DOC_FLASHSEQUENCE);
647 writew(DOCG4_CMD_PAGEWRITE, docptr + DOC_FLASHCOMMAND);
648 write_nop(docptr);
649 write_addr(doc, docg4_addr);
650 write_nop(docptr);
651 write_nop(docptr);
652 poll_status(doc);
653}
654
655static uint32_t mtd_to_docg4_address(int page, int column)
656{
657 /*
658 * Convert mtd address to format used by the device, 32 bit packed.
659 *
660 * Some notes on G4 addressing... The M-Sys documentation on this device
661 * claims that pages are 2K in length, and indeed, the format of the
662 * address used by the device reflects that. But within each page are
663 * four 512 byte "sub-pages", each with its own oob data that is
664 * read/written immediately after the 512 bytes of page data. This oob
665 * data contains the ecc bytes for the preceeding 512 bytes.
666 *
667 * Rather than tell the mtd nand infrastructure that page size is 2k,
668 * with four sub-pages each, we engage in a little subterfuge and tell
669 * the infrastructure code that pages are 512 bytes in size. This is
670 * done because during the course of reverse-engineering the device, I
671 * never observed an instance where an entire 2K "page" was read or
672 * written as a unit. Each "sub-page" is always addressed individually,
673 * its data read/written, and ecc handled before the next "sub-page" is
674 * addressed.
675 *
676 * This requires us to convert addresses passed by the mtd nand
677 * infrastructure code to those used by the device.
678 *
679 * The address that is written to the device consists of four bytes: the
680 * first two are the 2k page number, and the second is the index into
681 * the page. The index is in terms of 16-bit half-words and includes
682 * the preceeding oob data, so e.g., the index into the second
683 * "sub-page" is 0x108, and the full device address of the start of mtd
684 * page 0x201 is 0x00800108.
685 */
686 int g4_page = page / 4; /* device's 2K page */
687 int g4_index = (page % 4) * 0x108 + column/2; /* offset into page */
688 return (g4_page << 16) | g4_index; /* pack */
689}
690
691static void docg4_command(struct mtd_info *mtd, unsigned command, int column,
692 int page_addr)
693{
694 /* handle standard nand commands */
695
696 struct nand_chip *nand = mtd->priv;
697 struct docg4_priv *doc = nand->priv;
698 uint32_t g4_addr = mtd_to_docg4_address(page_addr, column);
699
700 dev_dbg(doc->dev, "%s %x, page_addr=%x, column=%x\n",
701 __func__, command, page_addr, column);
702
703 /*
704 * Save the command and its arguments. This enables emulation of
705 * standard flash devices, and also some optimizations.
706 */
707 doc->last_command.command = command;
708 doc->last_command.column = column;
709 doc->last_command.page = page_addr;
710
711 switch (command) {
712
713 case NAND_CMD_RESET:
714 reset(mtd);
715 break;
716
717 case NAND_CMD_READ0:
718 read_page_prologue(mtd, g4_addr);
719 break;
720
721 case NAND_CMD_STATUS:
722 /* next call to read_byte() will expect a status */
723 break;
724
725 case NAND_CMD_SEQIN:
Mike Dunn5a90d412012-12-07 12:07:21 -0800726 if (unlikely(reliable_mode)) {
727 uint16_t g4_page = g4_addr >> 16;
728
729 /* writes to odd-numbered 2k pages are invalid */
730 if (g4_page & 0x01)
731 dev_warn(doc->dev,
732 "invalid reliable mode address\n");
733 }
734
Mike Dunn570469f2012-01-03 16:05:44 -0800735 write_page_prologue(mtd, g4_addr);
736
737 /* hack for deferred write of oob bytes */
738 if (doc->oob_page == page_addr)
739 memcpy(nand->oob_poi, doc->oob_buf, 16);
740 break;
741
742 case NAND_CMD_PAGEPROG:
743 pageprog(mtd);
744 break;
745
746 /* we don't expect these, based on review of nand_base.c */
747 case NAND_CMD_READOOB:
748 case NAND_CMD_READID:
749 case NAND_CMD_ERASE1:
750 case NAND_CMD_ERASE2:
751 dev_warn(doc->dev, "docg4_command: "
752 "unexpected nand command 0x%x\n", command);
753 break;
754
755 }
756}
757
758static int read_page(struct mtd_info *mtd, struct nand_chip *nand,
759 uint8_t *buf, int page, bool use_ecc)
760{
761 struct docg4_priv *doc = nand->priv;
762 void __iomem *docptr = doc->virtadr;
763 uint16_t status, edc_err, *buf16;
Mike Dunn3f91e942012-04-25 12:06:09 -0700764 int bits_corrected = 0;
Mike Dunn570469f2012-01-03 16:05:44 -0800765
766 dev_dbg(doc->dev, "%s: page %08x\n", __func__, page);
767
768 writew(DOC_ECCCONF0_READ_MODE |
769 DOC_ECCCONF0_ECC_ENABLE |
770 DOC_ECCCONF0_UNKNOWN |
771 DOCG4_BCH_SIZE,
772 docptr + DOC_ECCCONF0);
773 write_nop(docptr);
774 write_nop(docptr);
775 write_nop(docptr);
776 write_nop(docptr);
777 write_nop(docptr);
778
779 /* the 1st byte from the I/O reg is a status; the rest is page data */
780 status = readw(docptr + DOC_IOSPACE_DATA);
781 if (status & DOCG4_READ_ERROR) {
782 dev_err(doc->dev,
783 "docg4_read_page: bad status: 0x%02x\n", status);
784 writew(0, docptr + DOC_DATAEND);
785 return -EIO;
786 }
787
788 dev_dbg(doc->dev, "%s: status = 0x%x\n", __func__, status);
789
790 docg4_read_buf(mtd, buf, DOCG4_PAGE_SIZE); /* read the page data */
791
Mike Dunnaa6d01f2012-07-11 11:08:19 -0700792 /* this device always reads oob after page data */
Mike Dunn570469f2012-01-03 16:05:44 -0800793 /* first 14 oob bytes read from I/O reg */
Mike Dunnaa6d01f2012-07-11 11:08:19 -0700794 docg4_read_buf(mtd, nand->oob_poi, 14);
Mike Dunn570469f2012-01-03 16:05:44 -0800795
796 /* last 2 read from another reg */
Mike Dunnaa6d01f2012-07-11 11:08:19 -0700797 buf16 = (uint16_t *)(nand->oob_poi + 14);
Mike Dunn570469f2012-01-03 16:05:44 -0800798 *buf16 = readw(docptr + DOCG4_MYSTERY_REG);
799
800 write_nop(docptr);
801
802 if (likely(use_ecc == true)) {
803
804 /* read the register that tells us if bitflip(s) detected */
805 edc_err = readw(docptr + DOC_ECCCONF1);
806 edc_err = readw(docptr + DOC_ECCCONF1);
807 dev_dbg(doc->dev, "%s: edc_err = 0x%02x\n", __func__, edc_err);
808
809 /* If bitflips are reported, attempt to correct with ecc */
810 if (edc_err & DOC_ECCCONF1_BCH_SYNDROM_ERR) {
Mike Dunn3f91e942012-04-25 12:06:09 -0700811 bits_corrected = correct_data(mtd, buf, page);
Mike Dunn570469f2012-01-03 16:05:44 -0800812 if (bits_corrected == -EBADMSG)
813 mtd->ecc_stats.failed++;
814 else
815 mtd->ecc_stats.corrected += bits_corrected;
816 }
817 }
818
819 writew(0, docptr + DOC_DATAEND);
Mike Dunn5bf3d662012-09-11 08:50:50 -0700820 if (bits_corrected == -EBADMSG) /* uncorrectable errors */
821 return 0;
Mike Dunn3f91e942012-04-25 12:06:09 -0700822 return bits_corrected;
Mike Dunn570469f2012-01-03 16:05:44 -0800823}
824
825
826static int docg4_read_page_raw(struct mtd_info *mtd, struct nand_chip *nand,
Brian Norris1fbb9382012-05-02 10:14:55 -0700827 uint8_t *buf, int oob_required, int page)
Mike Dunn570469f2012-01-03 16:05:44 -0800828{
829 return read_page(mtd, nand, buf, page, false);
830}
831
832static int docg4_read_page(struct mtd_info *mtd, struct nand_chip *nand,
Brian Norris1fbb9382012-05-02 10:14:55 -0700833 uint8_t *buf, int oob_required, int page)
Mike Dunn570469f2012-01-03 16:05:44 -0800834{
835 return read_page(mtd, nand, buf, page, true);
836}
837
838static int docg4_read_oob(struct mtd_info *mtd, struct nand_chip *nand,
Shmulik Ladkani5c2ffb12012-05-09 13:06:35 +0300839 int page)
Mike Dunn570469f2012-01-03 16:05:44 -0800840{
841 struct docg4_priv *doc = nand->priv;
842 void __iomem *docptr = doc->virtadr;
843 uint16_t status;
844
845 dev_dbg(doc->dev, "%s: page %x\n", __func__, page);
846
Mike Dunn570469f2012-01-03 16:05:44 -0800847 docg4_command(mtd, NAND_CMD_READ0, nand->ecc.size, page);
848
849 writew(DOC_ECCCONF0_READ_MODE | DOCG4_OOB_SIZE, docptr + DOC_ECCCONF0);
850 write_nop(docptr);
851 write_nop(docptr);
852 write_nop(docptr);
853 write_nop(docptr);
854 write_nop(docptr);
855
856 /* the 1st byte from the I/O reg is a status; the rest is oob data */
857 status = readw(docptr + DOC_IOSPACE_DATA);
858 if (status & DOCG4_READ_ERROR) {
859 dev_warn(doc->dev,
860 "docg4_read_oob failed: status = 0x%02x\n", status);
861 return -EIO;
862 }
863
864 dev_dbg(doc->dev, "%s: status = 0x%x\n", __func__, status);
865
866 docg4_read_buf(mtd, nand->oob_poi, 16);
867
868 write_nop(docptr);
869 write_nop(docptr);
870 write_nop(docptr);
871 writew(0, docptr + DOC_DATAEND);
872 write_nop(docptr);
873
874 return 0;
875}
876
877static void docg4_erase_block(struct mtd_info *mtd, int page)
878{
879 struct nand_chip *nand = mtd->priv;
880 struct docg4_priv *doc = nand->priv;
881 void __iomem *docptr = doc->virtadr;
882 uint16_t g4_page;
883
884 dev_dbg(doc->dev, "%s: page %04x\n", __func__, page);
885
886 sequence_reset(mtd);
887
888 writew(DOCG4_SEQ_BLOCKERASE, docptr + DOC_FLASHSEQUENCE);
889 writew(DOC_CMD_PROG_BLOCK_ADDR, docptr + DOC_FLASHCOMMAND);
890 write_nop(docptr);
891
892 /* only 2 bytes of address are written to specify erase block */
893 g4_page = (uint16_t)(page / 4); /* to g4's 2k page addressing */
894 writeb(g4_page & 0xff, docptr + DOC_FLASHADDRESS);
895 g4_page >>= 8;
896 writeb(g4_page & 0xff, docptr + DOC_FLASHADDRESS);
897 write_nop(docptr);
898
899 /* start the erasure */
900 writew(DOC_CMD_ERASECYCLE2, docptr + DOC_FLASHCOMMAND);
901 write_nop(docptr);
902 write_nop(docptr);
903
904 usleep_range(500, 1000); /* erasure is long; take a snooze */
905 poll_status(doc);
906 writew(DOCG4_SEQ_FLUSH, docptr + DOC_FLASHSEQUENCE);
907 writew(DOCG4_CMD_FLUSH, docptr + DOC_FLASHCOMMAND);
908 writew(DOC_ECCCONF0_READ_MODE | 4, docptr + DOC_ECCCONF0);
909 write_nop(docptr);
910 write_nop(docptr);
911 write_nop(docptr);
912 write_nop(docptr);
913 write_nop(docptr);
914
915 read_progstatus(doc);
916
917 writew(0, docptr + DOC_DATAEND);
918 write_nop(docptr);
919 poll_status(doc);
920 write_nop(docptr);
921}
922
Josh Wufdbad98d2012-06-25 18:07:45 +0800923static int write_page(struct mtd_info *mtd, struct nand_chip *nand,
Mike Dunn570469f2012-01-03 16:05:44 -0800924 const uint8_t *buf, bool use_ecc)
925{
926 struct docg4_priv *doc = nand->priv;
927 void __iomem *docptr = doc->virtadr;
928 uint8_t ecc_buf[8];
929
930 dev_dbg(doc->dev, "%s...\n", __func__);
931
932 writew(DOC_ECCCONF0_ECC_ENABLE |
933 DOC_ECCCONF0_UNKNOWN |
934 DOCG4_BCH_SIZE,
935 docptr + DOC_ECCCONF0);
936 write_nop(docptr);
937
938 /* write the page data */
939 docg4_write_buf16(mtd, buf, DOCG4_PAGE_SIZE);
940
941 /* oob bytes 0 through 5 are written to I/O reg */
942 docg4_write_buf16(mtd, nand->oob_poi, 6);
943
944 /* oob byte 6 written to a separate reg */
945 writew(nand->oob_poi[6], docptr + DOCG4_OOB_6_7);
946
947 write_nop(docptr);
948 write_nop(docptr);
949
950 /* write hw-generated ecc bytes to oob */
951 if (likely(use_ecc == true)) {
952 /* oob byte 7 is hamming code */
953 uint8_t hamming = readb(docptr + DOC_HAMMINGPARITY);
954 hamming = readb(docptr + DOC_HAMMINGPARITY); /* 2nd read */
955 writew(hamming, docptr + DOCG4_OOB_6_7);
956 write_nop(docptr);
957
958 /* read the 7 bch bytes from ecc regs */
959 read_hw_ecc(docptr, ecc_buf);
960 ecc_buf[7] = 0; /* clear the "page written" flag */
961 }
962
963 /* write user-supplied bytes to oob */
964 else {
965 writew(nand->oob_poi[7], docptr + DOCG4_OOB_6_7);
966 write_nop(docptr);
967 memcpy(ecc_buf, &nand->oob_poi[8], 8);
968 }
969
970 docg4_write_buf16(mtd, ecc_buf, 8);
971 write_nop(docptr);
972 write_nop(docptr);
973 writew(0, docptr + DOC_DATAEND);
974 write_nop(docptr);
Josh Wufdbad98d2012-06-25 18:07:45 +0800975
976 return 0;
Mike Dunn570469f2012-01-03 16:05:44 -0800977}
978
Josh Wufdbad98d2012-06-25 18:07:45 +0800979static int docg4_write_page_raw(struct mtd_info *mtd, struct nand_chip *nand,
Brian Norris1fbb9382012-05-02 10:14:55 -0700980 const uint8_t *buf, int oob_required)
Mike Dunn570469f2012-01-03 16:05:44 -0800981{
982 return write_page(mtd, nand, buf, false);
983}
984
Josh Wufdbad98d2012-06-25 18:07:45 +0800985static int docg4_write_page(struct mtd_info *mtd, struct nand_chip *nand,
Brian Norris1fbb9382012-05-02 10:14:55 -0700986 const uint8_t *buf, int oob_required)
Mike Dunn570469f2012-01-03 16:05:44 -0800987{
988 return write_page(mtd, nand, buf, true);
989}
990
991static int docg4_write_oob(struct mtd_info *mtd, struct nand_chip *nand,
992 int page)
993{
994 /*
995 * Writing oob-only is not really supported, because MLC nand must write
996 * oob bytes at the same time as page data. Nonetheless, we save the
997 * oob buffer contents here, and then write it along with the page data
998 * if the same page is subsequently written. This allows user space
999 * utilities that write the oob data prior to the page data to work
1000 * (e.g., nandwrite). The disdvantage is that, if the intention was to
1001 * write oob only, the operation is quietly ignored. Also, oob can get
1002 * corrupted if two concurrent processes are running nandwrite.
1003 */
1004
1005 /* note that bytes 7..14 are hw generated hamming/ecc and overwritten */
1006 struct docg4_priv *doc = nand->priv;
1007 doc->oob_page = page;
1008 memcpy(doc->oob_buf, nand->oob_poi, 16);
1009 return 0;
1010}
1011
1012static int __init read_factory_bbt(struct mtd_info *mtd)
1013{
1014 /*
1015 * The device contains a read-only factory bad block table. Read it and
1016 * update the memory-based bbt accordingly.
1017 */
1018
1019 struct nand_chip *nand = mtd->priv;
1020 struct docg4_priv *doc = nand->priv;
1021 uint32_t g4_addr = mtd_to_docg4_address(DOCG4_FACTORY_BBT_PAGE, 0);
1022 uint8_t *buf;
1023 int i, block, status;
1024
1025 buf = kzalloc(DOCG4_PAGE_SIZE, GFP_KERNEL);
1026 if (buf == NULL)
1027 return -ENOMEM;
1028
1029 read_page_prologue(mtd, g4_addr);
Brian Norris1fbb9382012-05-02 10:14:55 -07001030 status = docg4_read_page(mtd, nand, buf, 0, DOCG4_FACTORY_BBT_PAGE);
Mike Dunn570469f2012-01-03 16:05:44 -08001031 if (status)
1032 goto exit;
1033
1034 /*
1035 * If no memory-based bbt was created, exit. This will happen if module
1036 * parameter ignore_badblocks is set. Then why even call this function?
1037 * For an unknown reason, block erase always fails if it's the first
1038 * operation after device power-up. The above read ensures it never is.
1039 * Ugly, I know.
1040 */
1041 if (nand->bbt == NULL) /* no memory-based bbt */
1042 goto exit;
1043
1044 /*
1045 * Parse factory bbt and update memory-based bbt. Factory bbt format is
1046 * simple: one bit per block, block numbers increase left to right (msb
1047 * to lsb). Bit clear means bad block.
1048 */
1049 for (i = block = 0; block < DOCG4_NUMBLOCKS; block += 8, i++) {
1050 int bitnum;
1051 unsigned long bits = ~buf[i];
1052 for_each_set_bit(bitnum, &bits, 8) {
1053 int badblock = block + 7 - bitnum;
1054 nand->bbt[badblock / 4] |=
1055 0x03 << ((badblock % 4) * 2);
1056 mtd->ecc_stats.badblocks++;
1057 dev_notice(doc->dev, "factory-marked bad block: %d\n",
1058 badblock);
1059 }
1060 }
1061 exit:
1062 kfree(buf);
1063 return status;
1064}
1065
1066static int docg4_block_markbad(struct mtd_info *mtd, loff_t ofs)
1067{
1068 /*
1069 * Mark a block as bad. Bad blocks are marked in the oob area of the
1070 * first page of the block. The default scan_bbt() in the nand
1071 * infrastructure code works fine for building the memory-based bbt
1072 * during initialization, as does the nand infrastructure function that
1073 * checks if a block is bad by reading the bbt. This function replaces
1074 * the nand default because writes to oob-only are not supported.
1075 */
1076
1077 int ret, i;
1078 uint8_t *buf;
1079 struct nand_chip *nand = mtd->priv;
1080 struct docg4_priv *doc = nand->priv;
1081 struct nand_bbt_descr *bbtd = nand->badblock_pattern;
1082 int block = (int)(ofs >> nand->bbt_erase_shift);
1083 int page = (int)(ofs >> nand->page_shift);
1084 uint32_t g4_addr = mtd_to_docg4_address(page, 0);
1085
1086 dev_dbg(doc->dev, "%s: %08llx\n", __func__, ofs);
1087
1088 if (unlikely(ofs & (DOCG4_BLOCK_SIZE - 1)))
1089 dev_warn(doc->dev, "%s: ofs %llx not start of block!\n",
1090 __func__, ofs);
1091
1092 /* allocate blank buffer for page data */
1093 buf = kzalloc(DOCG4_PAGE_SIZE, GFP_KERNEL);
1094 if (buf == NULL)
1095 return -ENOMEM;
1096
1097 /* update bbt in memory */
1098 nand->bbt[block / 4] |= 0x01 << ((block & 0x03) * 2);
1099
1100 /* write bit-wise negation of pattern to oob buffer */
1101 memset(nand->oob_poi, 0xff, mtd->oobsize);
1102 for (i = 0; i < bbtd->len; i++)
1103 nand->oob_poi[bbtd->offs + i] = ~bbtd->pattern[i];
1104
1105 /* write first page of block */
1106 write_page_prologue(mtd, g4_addr);
Brian Norris1fbb9382012-05-02 10:14:55 -07001107 docg4_write_page(mtd, nand, buf, 1);
Mike Dunn570469f2012-01-03 16:05:44 -08001108 ret = pageprog(mtd);
1109 if (!ret)
1110 mtd->ecc_stats.badblocks++;
1111
1112 kfree(buf);
1113
1114 return ret;
1115}
1116
1117static int docg4_block_neverbad(struct mtd_info *mtd, loff_t ofs, int getchip)
1118{
1119 /* only called when module_param ignore_badblocks is set */
1120 return 0;
1121}
1122
1123static int docg4_suspend(struct platform_device *pdev, pm_message_t state)
1124{
1125 /*
1126 * Put the device into "deep power-down" mode. Note that CE# must be
1127 * deasserted for this to take effect. The xscale, e.g., can be
1128 * configured to float this signal when the processor enters power-down,
1129 * and a suitable pull-up ensures its deassertion.
1130 */
1131
1132 int i;
1133 uint8_t pwr_down;
1134 struct docg4_priv *doc = platform_get_drvdata(pdev);
1135 void __iomem *docptr = doc->virtadr;
1136
1137 dev_dbg(doc->dev, "%s...\n", __func__);
1138
1139 /* poll the register that tells us we're ready to go to sleep */
1140 for (i = 0; i < 10; i++) {
1141 pwr_down = readb(docptr + DOC_POWERMODE);
1142 if (pwr_down & DOC_POWERDOWN_READY)
1143 break;
1144 usleep_range(1000, 4000);
1145 }
1146
1147 if (pwr_down & DOC_POWERDOWN_READY) {
1148 dev_err(doc->dev, "suspend failed; "
1149 "timeout polling DOC_POWERDOWN_READY\n");
1150 return -EIO;
1151 }
1152
1153 writew(DOC_ASICMODE_POWERDOWN | DOC_ASICMODE_MDWREN,
1154 docptr + DOC_ASICMODE);
1155 writew(~(DOC_ASICMODE_POWERDOWN | DOC_ASICMODE_MDWREN),
1156 docptr + DOC_ASICMODECONFIRM);
1157
1158 write_nop(docptr);
1159
1160 return 0;
1161}
1162
1163static int docg4_resume(struct platform_device *pdev)
1164{
1165
1166 /*
1167 * Exit power-down. Twelve consecutive reads of the address below
1168 * accomplishes this, assuming CE# has been asserted.
1169 */
1170
1171 struct docg4_priv *doc = platform_get_drvdata(pdev);
1172 void __iomem *docptr = doc->virtadr;
1173 int i;
1174
1175 dev_dbg(doc->dev, "%s...\n", __func__);
1176
1177 for (i = 0; i < 12; i++)
1178 readb(docptr + 0x1fff);
1179
1180 return 0;
1181}
1182
1183static void __init init_mtd_structs(struct mtd_info *mtd)
1184{
1185 /* initialize mtd and nand data structures */
1186
1187 /*
1188 * Note that some of the following initializations are not usually
1189 * required within a nand driver because they are performed by the nand
1190 * infrastructure code as part of nand_scan(). In this case they need
1191 * to be initialized here because we skip call to nand_scan_ident() (the
1192 * first half of nand_scan()). The call to nand_scan_ident() is skipped
1193 * because for this device the chip id is not read in the manner of a
1194 * standard nand device. Unfortunately, nand_scan_ident() does other
1195 * things as well, such as call nand_set_defaults().
1196 */
1197
1198 struct nand_chip *nand = mtd->priv;
1199 struct docg4_priv *doc = nand->priv;
1200
1201 mtd->size = DOCG4_CHIP_SIZE;
1202 mtd->name = "Msys_Diskonchip_G4";
1203 mtd->writesize = DOCG4_PAGE_SIZE;
1204 mtd->erasesize = DOCG4_BLOCK_SIZE;
1205 mtd->oobsize = DOCG4_OOB_SIZE;
1206 nand->chipsize = DOCG4_CHIP_SIZE;
1207 nand->chip_shift = DOCG4_CHIP_SHIFT;
1208 nand->bbt_erase_shift = nand->phys_erase_shift = DOCG4_ERASE_SHIFT;
1209 nand->chip_delay = 20;
1210 nand->page_shift = DOCG4_PAGE_SHIFT;
1211 nand->pagemask = 0x3ffff;
1212 nand->badblockpos = NAND_LARGE_BADBLOCK_POS;
1213 nand->badblockbits = 8;
1214 nand->ecc.layout = &docg4_oobinfo;
1215 nand->ecc.mode = NAND_ECC_HW_SYNDROME;
1216 nand->ecc.size = DOCG4_PAGE_SIZE;
1217 nand->ecc.prepad = 8;
1218 nand->ecc.bytes = 8;
Mike Dunn6a918ba2012-03-11 14:21:11 -07001219 nand->ecc.strength = DOCG4_T;
Brian Norris1826dbc2012-05-01 17:12:55 -07001220 nand->options = NAND_BUSWIDTH_16 | NAND_NO_SUBPAGE_WRITE;
Mike Dunn570469f2012-01-03 16:05:44 -08001221 nand->IO_ADDR_R = nand->IO_ADDR_W = doc->virtadr + DOC_IOSPACE_DATA;
1222 nand->controller = &nand->hwcontrol;
1223 spin_lock_init(&nand->controller->lock);
1224 init_waitqueue_head(&nand->controller->wq);
1225
1226 /* methods */
1227 nand->cmdfunc = docg4_command;
1228 nand->waitfunc = docg4_wait;
1229 nand->select_chip = docg4_select_chip;
1230 nand->read_byte = docg4_read_byte;
1231 nand->block_markbad = docg4_block_markbad;
1232 nand->read_buf = docg4_read_buf;
1233 nand->write_buf = docg4_write_buf16;
1234 nand->scan_bbt = nand_default_bbt;
1235 nand->erase_cmd = docg4_erase_block;
1236 nand->ecc.read_page = docg4_read_page;
1237 nand->ecc.write_page = docg4_write_page;
1238 nand->ecc.read_page_raw = docg4_read_page_raw;
1239 nand->ecc.write_page_raw = docg4_write_page_raw;
1240 nand->ecc.read_oob = docg4_read_oob;
1241 nand->ecc.write_oob = docg4_write_oob;
1242
1243 /*
1244 * The way the nand infrastructure code is written, a memory-based bbt
1245 * is not created if NAND_SKIP_BBTSCAN is set. With no memory bbt,
1246 * nand->block_bad() is used. So when ignoring bad blocks, we skip the
1247 * scan and define a dummy block_bad() which always returns 0.
1248 */
1249 if (ignore_badblocks) {
1250 nand->options |= NAND_SKIP_BBTSCAN;
1251 nand->block_bad = docg4_block_neverbad;
1252 }
1253
1254}
1255
1256static int __init read_id_reg(struct mtd_info *mtd)
1257{
1258 struct nand_chip *nand = mtd->priv;
1259 struct docg4_priv *doc = nand->priv;
1260 void __iomem *docptr = doc->virtadr;
1261 uint16_t id1, id2;
1262
1263 /* check for presence of g4 chip by reading id registers */
1264 id1 = readw(docptr + DOC_CHIPID);
1265 id1 = readw(docptr + DOCG4_MYSTERY_REG);
1266 id2 = readw(docptr + DOC_CHIPID_INV);
1267 id2 = readw(docptr + DOCG4_MYSTERY_REG);
1268
1269 if (id1 == DOCG4_IDREG1_VALUE && id2 == DOCG4_IDREG2_VALUE) {
1270 dev_info(doc->dev,
1271 "NAND device: 128MiB Diskonchip G4 detected\n");
1272 return 0;
1273 }
1274
1275 return -ENODEV;
1276}
1277
1278static char const *part_probes[] = { "cmdlinepart", "saftlpart", NULL };
1279
1280static int __init probe_docg4(struct platform_device *pdev)
1281{
1282 struct mtd_info *mtd;
1283 struct nand_chip *nand;
1284 void __iomem *virtadr;
1285 struct docg4_priv *doc;
1286 int len, retval;
1287 struct resource *r;
1288 struct device *dev = &pdev->dev;
1289
1290 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1291 if (r == NULL) {
1292 dev_err(dev, "no io memory resource defined!\n");
1293 return -ENODEV;
1294 }
1295
1296 virtadr = ioremap(r->start, resource_size(r));
1297 if (!virtadr) {
Dan Carpenter2c4ae272012-01-31 11:54:06 +03001298 dev_err(dev, "Diskonchip ioremap failed: %pR\n", r);
Mike Dunn570469f2012-01-03 16:05:44 -08001299 return -EIO;
1300 }
1301
1302 len = sizeof(struct mtd_info) + sizeof(struct nand_chip) +
1303 sizeof(struct docg4_priv);
1304 mtd = kzalloc(len, GFP_KERNEL);
1305 if (mtd == NULL) {
1306 retval = -ENOMEM;
1307 goto fail;
1308 }
1309 nand = (struct nand_chip *) (mtd + 1);
1310 doc = (struct docg4_priv *) (nand + 1);
1311 mtd->priv = nand;
1312 nand->priv = doc;
1313 mtd->owner = THIS_MODULE;
1314 doc->virtadr = virtadr;
1315 doc->dev = dev;
1316
1317 init_mtd_structs(mtd);
1318
1319 /* initialize kernel bch algorithm */
1320 doc->bch = init_bch(DOCG4_M, DOCG4_T, DOCG4_PRIMITIVE_POLY);
1321 if (doc->bch == NULL) {
1322 retval = -EINVAL;
1323 goto fail;
1324 }
1325
1326 platform_set_drvdata(pdev, doc);
1327
1328 reset(mtd);
1329 retval = read_id_reg(mtd);
1330 if (retval == -ENODEV) {
1331 dev_warn(dev, "No diskonchip G4 device found.\n");
1332 goto fail;
1333 }
1334
1335 retval = nand_scan_tail(mtd);
1336 if (retval)
1337 goto fail;
1338
1339 retval = read_factory_bbt(mtd);
1340 if (retval)
1341 goto fail;
1342
1343 retval = mtd_device_parse_register(mtd, part_probes, NULL, NULL, 0);
1344 if (retval)
1345 goto fail;
1346
1347 doc->mtd = mtd;
1348 return 0;
1349
1350 fail:
1351 iounmap(virtadr);
1352 if (mtd) {
1353 /* re-declarations avoid compiler warning */
1354 struct nand_chip *nand = mtd->priv;
1355 struct docg4_priv *doc = nand->priv;
1356 nand_release(mtd); /* deletes partitions and mtd devices */
1357 platform_set_drvdata(pdev, NULL);
1358 free_bch(doc->bch);
1359 kfree(mtd);
1360 }
1361
1362 return retval;
1363}
1364
1365static int __exit cleanup_docg4(struct platform_device *pdev)
1366{
1367 struct docg4_priv *doc = platform_get_drvdata(pdev);
1368 nand_release(doc->mtd);
1369 platform_set_drvdata(pdev, NULL);
1370 free_bch(doc->bch);
1371 kfree(doc->mtd);
1372 iounmap(doc->virtadr);
1373 return 0;
1374}
1375
1376static struct platform_driver docg4_driver = {
1377 .driver = {
1378 .name = "docg4",
1379 .owner = THIS_MODULE,
1380 },
1381 .suspend = docg4_suspend,
1382 .resume = docg4_resume,
1383 .remove = __exit_p(cleanup_docg4),
1384};
1385
1386static int __init docg4_init(void)
1387{
1388 return platform_driver_probe(&docg4_driver, probe_docg4);
1389}
1390
1391static void __exit docg4_exit(void)
1392{
1393 platform_driver_unregister(&docg4_driver);
1394}
1395
1396module_init(docg4_init);
1397module_exit(docg4_exit);
1398
1399MODULE_LICENSE("GPL");
1400MODULE_AUTHOR("Mike Dunn");
1401MODULE_DESCRIPTION("M-Systems DiskOnChip G4 device driver");