blob: 37935fd0402068d9a021beeae7e2245366e55e0c [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>
Boris Brezillond4092d72017-08-04 17:29:10 +020044#include <linux/mtd/rawnand.h>
Mike Dunn570469f2012-01-03 16:05:44 -080045#include <linux/bch.h>
46#include <linux/bitrev.h>
Mike Dunn9fee8402013-10-10 10:25:27 -070047#include <linux/jiffies.h>
Mike Dunn570469f2012-01-03 16:05:44 -080048
49/*
Mike Dunn5a90d412012-12-07 12:07:21 -080050 * In "reliable mode" consecutive 2k pages are used in parallel (in some
51 * fashion) to store the same data. The data can be read back from the
52 * even-numbered pages in the normal manner; odd-numbered pages will appear to
53 * contain junk. Systems that boot from the docg4 typically write the secondary
54 * program loader (SPL) code in this mode. The SPL is loaded by the initial
55 * program loader (IPL, stored in the docg4's 2k NOR-like region that is mapped
56 * to the reset vector address). This module parameter enables you to use this
57 * driver to write the SPL. When in this mode, no more than 2k of data can be
58 * written at a time, because the addresses do not increment in the normal
59 * manner, and the starting offset must be within an even-numbered 2k region;
60 * i.e., invalid starting offsets are 0x800, 0xa00, 0xc00, 0xe00, 0x1800,
61 * 0x1a00, ... Reliable mode is a special case and should not be used unless
62 * you know what you're doing.
63 */
64static bool reliable_mode;
65module_param(reliable_mode, bool, 0);
66MODULE_PARM_DESC(reliable_mode, "pages are programmed in reliable mode");
67
68/*
Mike Dunn570469f2012-01-03 16:05:44 -080069 * You'll want to ignore badblocks if you're reading a partition that contains
70 * data written by the TrueFFS library (i.e., by PalmOS, Windows, etc), since
71 * it does not use mtd nand's method for marking bad blocks (using oob area).
72 * This will also skip the check of the "page written" flag.
73 */
74static bool ignore_badblocks;
75module_param(ignore_badblocks, bool, 0);
76MODULE_PARM_DESC(ignore_badblocks, "no badblock checking performed");
77
78struct docg4_priv {
79 struct mtd_info *mtd;
80 struct device *dev;
81 void __iomem *virtadr;
82 int status;
83 struct {
84 unsigned int command;
85 int column;
86 int page;
87 } last_command;
88 uint8_t oob_buf[16];
89 uint8_t ecc_buf[7];
90 int oob_page;
91 struct bch_control *bch;
92};
93
94/*
95 * Defines prefixed with DOCG4 are unique to the diskonchip G4. All others are
96 * shared with other diskonchip devices (P3, G3 at least).
97 *
98 * Functions with names prefixed with docg4_ are mtd / nand interface functions
99 * (though they may also be called internally). All others are internal.
100 */
101
102#define DOC_IOSPACE_DATA 0x0800
103
104/* register offsets */
105#define DOC_CHIPID 0x1000
106#define DOC_DEVICESELECT 0x100a
107#define DOC_ASICMODE 0x100c
108#define DOC_DATAEND 0x101e
109#define DOC_NOP 0x103e
110
111#define DOC_FLASHSEQUENCE 0x1032
112#define DOC_FLASHCOMMAND 0x1034
113#define DOC_FLASHADDRESS 0x1036
114#define DOC_FLASHCONTROL 0x1038
115#define DOC_ECCCONF0 0x1040
116#define DOC_ECCCONF1 0x1042
117#define DOC_HAMMINGPARITY 0x1046
118#define DOC_BCH_SYNDROM(idx) (0x1048 + idx)
119
120#define DOC_ASICMODECONFIRM 0x1072
121#define DOC_CHIPID_INV 0x1074
122#define DOC_POWERMODE 0x107c
123
124#define DOCG4_MYSTERY_REG 0x1050
125
126/* apparently used only to write oob bytes 6 and 7 */
127#define DOCG4_OOB_6_7 0x1052
128
129/* DOC_FLASHSEQUENCE register commands */
130#define DOC_SEQ_RESET 0x00
131#define DOCG4_SEQ_PAGE_READ 0x03
132#define DOCG4_SEQ_FLUSH 0x29
133#define DOCG4_SEQ_PAGEWRITE 0x16
134#define DOCG4_SEQ_PAGEPROG 0x1e
135#define DOCG4_SEQ_BLOCKERASE 0x24
Mike Dunn5a90d412012-12-07 12:07:21 -0800136#define DOCG4_SEQ_SETMODE 0x45
Mike Dunn570469f2012-01-03 16:05:44 -0800137
138/* DOC_FLASHCOMMAND register commands */
139#define DOCG4_CMD_PAGE_READ 0x00
140#define DOC_CMD_ERASECYCLE2 0xd0
141#define DOCG4_CMD_FLUSH 0x70
142#define DOCG4_CMD_READ2 0x30
143#define DOC_CMD_PROG_BLOCK_ADDR 0x60
144#define DOCG4_CMD_PAGEWRITE 0x80
145#define DOC_CMD_PROG_CYCLE2 0x10
Mike Dunn5a90d412012-12-07 12:07:21 -0800146#define DOCG4_CMD_FAST_MODE 0xa3 /* functionality guessed */
147#define DOC_CMD_RELIABLE_MODE 0x22
Mike Dunn570469f2012-01-03 16:05:44 -0800148#define DOC_CMD_RESET 0xff
149
150/* DOC_POWERMODE register bits */
151#define DOC_POWERDOWN_READY 0x80
152
153/* DOC_FLASHCONTROL register bits */
154#define DOC_CTRL_CE 0x10
155#define DOC_CTRL_UNKNOWN 0x40
156#define DOC_CTRL_FLASHREADY 0x01
157
158/* DOC_ECCCONF0 register bits */
159#define DOC_ECCCONF0_READ_MODE 0x8000
160#define DOC_ECCCONF0_UNKNOWN 0x2000
161#define DOC_ECCCONF0_ECC_ENABLE 0x1000
162#define DOC_ECCCONF0_DATA_BYTES_MASK 0x07ff
163
164/* DOC_ECCCONF1 register bits */
165#define DOC_ECCCONF1_BCH_SYNDROM_ERR 0x80
166#define DOC_ECCCONF1_ECC_ENABLE 0x07
167#define DOC_ECCCONF1_PAGE_IS_WRITTEN 0x20
168
169/* DOC_ASICMODE register bits */
170#define DOC_ASICMODE_RESET 0x00
171#define DOC_ASICMODE_NORMAL 0x01
172#define DOC_ASICMODE_POWERDOWN 0x02
173#define DOC_ASICMODE_MDWREN 0x04
174#define DOC_ASICMODE_BDETCT_RESET 0x08
175#define DOC_ASICMODE_RSTIN_RESET 0x10
176#define DOC_ASICMODE_RAM_WE 0x20
177
178/* good status values read after read/write/erase operations */
179#define DOCG4_PROGSTATUS_GOOD 0x51
180#define DOCG4_PROGSTATUS_GOOD_2 0xe0
181
182/*
183 * On read operations (page and oob-only), the first byte read from I/O reg is a
184 * status. On error, it reads 0x73; otherwise, it reads either 0x71 (first read
185 * after reset only) or 0x51, so bit 1 is presumed to be an error indicator.
186 */
187#define DOCG4_READ_ERROR 0x02 /* bit 1 indicates read error */
188
189/* anatomy of the device */
190#define DOCG4_CHIP_SIZE 0x8000000
191#define DOCG4_PAGE_SIZE 0x200
192#define DOCG4_PAGES_PER_BLOCK 0x200
193#define DOCG4_BLOCK_SIZE (DOCG4_PAGES_PER_BLOCK * DOCG4_PAGE_SIZE)
194#define DOCG4_NUMBLOCKS (DOCG4_CHIP_SIZE / DOCG4_BLOCK_SIZE)
195#define DOCG4_OOB_SIZE 0x10
196#define DOCG4_CHIP_SHIFT 27 /* log_2(DOCG4_CHIP_SIZE) */
197#define DOCG4_PAGE_SHIFT 9 /* log_2(DOCG4_PAGE_SIZE) */
198#define DOCG4_ERASE_SHIFT 18 /* log_2(DOCG4_BLOCK_SIZE) */
199
200/* all but the last byte is included in ecc calculation */
201#define DOCG4_BCH_SIZE (DOCG4_PAGE_SIZE + DOCG4_OOB_SIZE - 1)
202
203#define DOCG4_USERDATA_LEN 520 /* 512 byte page plus 8 oob avail to user */
204
205/* expected values from the ID registers */
206#define DOCG4_IDREG1_VALUE 0x0400
207#define DOCG4_IDREG2_VALUE 0xfbff
208
209/* primitive polynomial used to build the Galois field used by hw ecc gen */
210#define DOCG4_PRIMITIVE_POLY 0x4443
211
212#define DOCG4_M 14 /* Galois field is of order 2^14 */
213#define DOCG4_T 4 /* BCH alg corrects up to 4 bit errors */
214
215#define DOCG4_FACTORY_BBT_PAGE 16 /* page where read-only factory bbt lives */
Mike Dunn3c9c6d62012-12-07 12:07:23 -0800216#define DOCG4_REDUNDANT_BBT_PAGE 24 /* page where redundant factory bbt lives */
Mike Dunn570469f2012-01-03 16:05:44 -0800217
218/*
Mike Dunn440b1d72012-12-07 12:07:22 -0800219 * Bytes 0, 1 are used as badblock marker.
220 * Bytes 2 - 6 are available to the user.
221 * Byte 7 is hamming ecc for first 7 oob bytes only.
222 * Bytes 8 - 14 are hw-generated ecc covering entire page + oob bytes 0 - 14.
Mike Dunn570469f2012-01-03 16:05:44 -0800223 * Byte 15 (the last) is used by the driver as a "page written" flag.
224 */
Boris Brezillon6b750652016-02-03 20:00:35 +0100225static int docg4_ooblayout_ecc(struct mtd_info *mtd, int section,
226 struct mtd_oob_region *oobregion)
227{
228 if (section)
229 return -ERANGE;
230
231 oobregion->offset = 7;
232 oobregion->length = 9;
233
234 return 0;
235}
236
237static int docg4_ooblayout_free(struct mtd_info *mtd, int section,
238 struct mtd_oob_region *oobregion)
239{
240 if (section)
241 return -ERANGE;
242
243 oobregion->offset = 2;
244 oobregion->length = 5;
245
246 return 0;
247}
248
249static const struct mtd_ooblayout_ops docg4_ooblayout_ops = {
250 .ecc = docg4_ooblayout_ecc,
251 .free = docg4_ooblayout_free,
Mike Dunn570469f2012-01-03 16:05:44 -0800252};
253
254/*
255 * The device has a nop register which M-Sys claims is for the purpose of
256 * inserting precise delays. But beware; at least some operations fail if the
257 * nop writes are replaced with a generic delay!
258 */
259static inline void write_nop(void __iomem *docptr)
260{
261 writew(0, docptr + DOC_NOP);
262}
263
264static void docg4_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
265{
266 int i;
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100267 struct nand_chip *nand = mtd_to_nand(mtd);
Mike Dunn570469f2012-01-03 16:05:44 -0800268 uint16_t *p = (uint16_t *) buf;
269 len >>= 1;
270
271 for (i = 0; i < len; i++)
272 p[i] = readw(nand->IO_ADDR_R);
273}
274
275static void docg4_write_buf16(struct mtd_info *mtd, const uint8_t *buf, int len)
276{
277 int i;
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100278 struct nand_chip *nand = mtd_to_nand(mtd);
Mike Dunn570469f2012-01-03 16:05:44 -0800279 uint16_t *p = (uint16_t *) buf;
280 len >>= 1;
281
282 for (i = 0; i < len; i++)
283 writew(p[i], nand->IO_ADDR_W);
284}
285
286static int poll_status(struct docg4_priv *doc)
287{
288 /*
289 * Busy-wait for the FLASHREADY bit to be set in the FLASHCONTROL
290 * register. Operations known to take a long time (e.g., block erase)
291 * should sleep for a while before calling this.
292 */
293
294 uint16_t flash_status;
Mike Dunn9fee8402013-10-10 10:25:27 -0700295 unsigned long timeo;
Mike Dunn570469f2012-01-03 16:05:44 -0800296 void __iomem *docptr = doc->virtadr;
297
298 dev_dbg(doc->dev, "%s...\n", __func__);
299
300 /* hardware quirk requires reading twice initially */
301 flash_status = readw(docptr + DOC_FLASHCONTROL);
302
Mike Dunn9fee8402013-10-10 10:25:27 -0700303 timeo = jiffies + msecs_to_jiffies(200); /* generous timeout */
Mike Dunn570469f2012-01-03 16:05:44 -0800304 do {
305 cpu_relax();
306 flash_status = readb(docptr + DOC_FLASHCONTROL);
Mike Dunn9fee8402013-10-10 10:25:27 -0700307 } while (!(flash_status & DOC_CTRL_FLASHREADY) &&
308 time_before(jiffies, timeo));
Mike Dunn570469f2012-01-03 16:05:44 -0800309
Mike Dunn9fee8402013-10-10 10:25:27 -0700310 if (unlikely(!(flash_status & DOC_CTRL_FLASHREADY))) {
Mike Dunn570469f2012-01-03 16:05:44 -0800311 dev_err(doc->dev, "%s: timed out!\n", __func__);
312 return NAND_STATUS_FAIL;
313 }
314
Mike Dunn570469f2012-01-03 16:05:44 -0800315 return 0;
316}
317
318
319static int docg4_wait(struct mtd_info *mtd, struct nand_chip *nand)
320{
321
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100322 struct docg4_priv *doc = nand_get_controller_data(nand);
Mike Dunn570469f2012-01-03 16:05:44 -0800323 int status = NAND_STATUS_WP; /* inverse logic?? */
324 dev_dbg(doc->dev, "%s...\n", __func__);
325
326 /* report any previously unreported error */
327 if (doc->status) {
328 status |= doc->status;
329 doc->status = 0;
330 return status;
331 }
332
333 status |= poll_status(doc);
334 return status;
335}
336
337static void docg4_select_chip(struct mtd_info *mtd, int chip)
338{
339 /*
340 * Select among multiple cascaded chips ("floors"). Multiple floors are
341 * not yet supported, so the only valid non-negative value is 0.
342 */
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100343 struct nand_chip *nand = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100344 struct docg4_priv *doc = nand_get_controller_data(nand);
Mike Dunn570469f2012-01-03 16:05:44 -0800345 void __iomem *docptr = doc->virtadr;
346
347 dev_dbg(doc->dev, "%s: chip %d\n", __func__, chip);
348
349 if (chip < 0)
350 return; /* deselected */
351
352 if (chip > 0)
353 dev_warn(doc->dev, "multiple floors currently unsupported\n");
354
355 writew(0, docptr + DOC_DEVICESELECT);
356}
357
358static void reset(struct mtd_info *mtd)
359{
360 /* full device reset */
361
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100362 struct nand_chip *nand = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100363 struct docg4_priv *doc = nand_get_controller_data(nand);
Mike Dunn570469f2012-01-03 16:05:44 -0800364 void __iomem *docptr = doc->virtadr;
365
366 writew(DOC_ASICMODE_RESET | DOC_ASICMODE_MDWREN,
367 docptr + DOC_ASICMODE);
368 writew(~(DOC_ASICMODE_RESET | DOC_ASICMODE_MDWREN),
369 docptr + DOC_ASICMODECONFIRM);
370 write_nop(docptr);
371
372 writew(DOC_ASICMODE_NORMAL | DOC_ASICMODE_MDWREN,
373 docptr + DOC_ASICMODE);
374 writew(~(DOC_ASICMODE_NORMAL | DOC_ASICMODE_MDWREN),
375 docptr + DOC_ASICMODECONFIRM);
376
377 writew(DOC_ECCCONF1_ECC_ENABLE, docptr + DOC_ECCCONF1);
378
379 poll_status(doc);
380}
381
382static void read_hw_ecc(void __iomem *docptr, uint8_t *ecc_buf)
383{
384 /* read the 7 hw-generated ecc bytes */
385
386 int i;
387 for (i = 0; i < 7; i++) { /* hw quirk; read twice */
388 ecc_buf[i] = readb(docptr + DOC_BCH_SYNDROM(i));
389 ecc_buf[i] = readb(docptr + DOC_BCH_SYNDROM(i));
390 }
391}
392
393static int correct_data(struct mtd_info *mtd, uint8_t *buf, int page)
394{
395 /*
396 * Called after a page read when hardware reports bitflips.
397 * Up to four bitflips can be corrected.
398 */
399
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100400 struct nand_chip *nand = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100401 struct docg4_priv *doc = nand_get_controller_data(nand);
Mike Dunn570469f2012-01-03 16:05:44 -0800402 void __iomem *docptr = doc->virtadr;
403 int i, numerrs, errpos[4];
404 const uint8_t blank_read_hwecc[8] = {
405 0xcf, 0x72, 0xfc, 0x1b, 0xa9, 0xc7, 0xb9, 0 };
406
407 read_hw_ecc(docptr, doc->ecc_buf); /* read 7 hw-generated ecc bytes */
408
409 /* check if read error is due to a blank page */
410 if (!memcmp(doc->ecc_buf, blank_read_hwecc, 7))
411 return 0; /* yes */
412
413 /* skip additional check of "written flag" if ignore_badblocks */
414 if (ignore_badblocks == false) {
415
416 /*
417 * If the hw ecc bytes are not those of a blank page, there's
418 * still a chance that the page is blank, but was read with
419 * errors. Check the "written flag" in last oob byte, which
420 * is set to zero when a page is written. If more than half
421 * the bits are set, assume a blank page. Unfortunately, the
422 * bit flips(s) are not reported in stats.
423 */
424
Mike Dunnaa6d01f2012-07-11 11:08:19 -0700425 if (nand->oob_poi[15]) {
Mike Dunn570469f2012-01-03 16:05:44 -0800426 int bit, numsetbits = 0;
Mike Dunnaa6d01f2012-07-11 11:08:19 -0700427 unsigned long written_flag = nand->oob_poi[15];
Mike Dunn570469f2012-01-03 16:05:44 -0800428 for_each_set_bit(bit, &written_flag, 8)
429 numsetbits++;
430 if (numsetbits > 4) { /* assume blank */
431 dev_warn(doc->dev,
432 "error(s) in blank page "
433 "at offset %08x\n",
434 page * DOCG4_PAGE_SIZE);
435 return 0;
436 }
437 }
438 }
439
440 /*
441 * The hardware ecc unit produces oob_ecc ^ calc_ecc. The kernel's bch
442 * algorithm is used to decode this. However the hw operates on page
443 * data in a bit order that is the reverse of that of the bch alg,
444 * requiring that the bits be reversed on the result. Thanks to Ivan
445 * Djelic for his analysis!
446 */
447 for (i = 0; i < 7; i++)
448 doc->ecc_buf[i] = bitrev8(doc->ecc_buf[i]);
449
450 numerrs = decode_bch(doc->bch, NULL, DOCG4_USERDATA_LEN, NULL,
451 doc->ecc_buf, NULL, errpos);
452
453 if (numerrs == -EBADMSG) {
454 dev_warn(doc->dev, "uncorrectable errors at offset %08x\n",
455 page * DOCG4_PAGE_SIZE);
456 return -EBADMSG;
457 }
458
459 BUG_ON(numerrs < 0); /* -EINVAL, or anything other than -EBADMSG */
460
461 /* undo last step in BCH alg (modulo mirroring not needed) */
462 for (i = 0; i < numerrs; i++)
463 errpos[i] = (errpos[i] & ~7)|(7-(errpos[i] & 7));
464
465 /* fix the errors */
466 for (i = 0; i < numerrs; i++) {
467
468 /* ignore if error within oob ecc bytes */
469 if (errpos[i] > DOCG4_USERDATA_LEN * 8)
470 continue;
471
472 /* if error within oob area preceeding ecc bytes... */
473 if (errpos[i] > DOCG4_PAGE_SIZE * 8)
474 change_bit(errpos[i] - DOCG4_PAGE_SIZE * 8,
Mike Dunnaa6d01f2012-07-11 11:08:19 -0700475 (unsigned long *)nand->oob_poi);
Mike Dunn570469f2012-01-03 16:05:44 -0800476
477 else /* error in page data */
478 change_bit(errpos[i], (unsigned long *)buf);
479 }
480
481 dev_notice(doc->dev, "%d error(s) corrected at offset %08x\n",
482 numerrs, page * DOCG4_PAGE_SIZE);
483
484 return numerrs;
485}
486
487static uint8_t docg4_read_byte(struct mtd_info *mtd)
488{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100489 struct nand_chip *nand = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100490 struct docg4_priv *doc = nand_get_controller_data(nand);
Mike Dunn570469f2012-01-03 16:05:44 -0800491
492 dev_dbg(doc->dev, "%s\n", __func__);
493
494 if (doc->last_command.command == NAND_CMD_STATUS) {
495 int status;
496
497 /*
498 * Previous nand command was status request, so nand
499 * infrastructure code expects to read the status here. If an
500 * error occurred in a previous operation, report it.
501 */
502 doc->last_command.command = 0;
503
504 if (doc->status) {
505 status = doc->status;
506 doc->status = 0;
507 }
508
509 /* why is NAND_STATUS_WP inverse logic?? */
510 else
511 status = NAND_STATUS_WP | NAND_STATUS_READY;
512
513 return status;
514 }
515
Masanari Iida6d3be302013-09-30 23:19:09 +0900516 dev_warn(doc->dev, "unexpected call to read_byte()\n");
Mike Dunn570469f2012-01-03 16:05:44 -0800517
518 return 0;
519}
520
521static void write_addr(struct docg4_priv *doc, uint32_t docg4_addr)
522{
523 /* write the four address bytes packed in docg4_addr to the device */
524
525 void __iomem *docptr = doc->virtadr;
526 writeb(docg4_addr & 0xff, docptr + DOC_FLASHADDRESS);
527 docg4_addr >>= 8;
528 writeb(docg4_addr & 0xff, docptr + DOC_FLASHADDRESS);
529 docg4_addr >>= 8;
530 writeb(docg4_addr & 0xff, docptr + DOC_FLASHADDRESS);
531 docg4_addr >>= 8;
532 writeb(docg4_addr & 0xff, docptr + DOC_FLASHADDRESS);
533}
534
535static int read_progstatus(struct docg4_priv *doc)
536{
537 /*
538 * This apparently checks the status of programming. Done after an
539 * erasure, and after page data is written. On error, the status is
540 * saved, to be later retrieved by the nand infrastructure code.
541 */
542 void __iomem *docptr = doc->virtadr;
543
544 /* status is read from the I/O reg */
545 uint16_t status1 = readw(docptr + DOC_IOSPACE_DATA);
546 uint16_t status2 = readw(docptr + DOC_IOSPACE_DATA);
547 uint16_t status3 = readw(docptr + DOCG4_MYSTERY_REG);
548
549 dev_dbg(doc->dev, "docg4: %s: %02x %02x %02x\n",
550 __func__, status1, status2, status3);
551
552 if (status1 != DOCG4_PROGSTATUS_GOOD
553 || status2 != DOCG4_PROGSTATUS_GOOD_2
554 || status3 != DOCG4_PROGSTATUS_GOOD_2) {
555 doc->status = NAND_STATUS_FAIL;
556 dev_warn(doc->dev, "read_progstatus failed: "
557 "%02x, %02x, %02x\n", status1, status2, status3);
558 return -EIO;
559 }
560 return 0;
561}
562
563static int pageprog(struct mtd_info *mtd)
564{
565 /*
566 * Final step in writing a page. Writes the contents of its
567 * internal buffer out to the flash array, or some such.
568 */
569
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100570 struct nand_chip *nand = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100571 struct docg4_priv *doc = nand_get_controller_data(nand);
Mike Dunn570469f2012-01-03 16:05:44 -0800572 void __iomem *docptr = doc->virtadr;
573 int retval = 0;
574
575 dev_dbg(doc->dev, "docg4: %s\n", __func__);
576
577 writew(DOCG4_SEQ_PAGEPROG, docptr + DOC_FLASHSEQUENCE);
578 writew(DOC_CMD_PROG_CYCLE2, docptr + DOC_FLASHCOMMAND);
579 write_nop(docptr);
580 write_nop(docptr);
581
582 /* Just busy-wait; usleep_range() slows things down noticeably. */
583 poll_status(doc);
584
585 writew(DOCG4_SEQ_FLUSH, docptr + DOC_FLASHSEQUENCE);
586 writew(DOCG4_CMD_FLUSH, docptr + DOC_FLASHCOMMAND);
587 writew(DOC_ECCCONF0_READ_MODE | 4, docptr + DOC_ECCCONF0);
588 write_nop(docptr);
589 write_nop(docptr);
590 write_nop(docptr);
591 write_nop(docptr);
592 write_nop(docptr);
593
594 retval = read_progstatus(doc);
595 writew(0, docptr + DOC_DATAEND);
596 write_nop(docptr);
597 poll_status(doc);
598 write_nop(docptr);
599
600 return retval;
601}
602
603static void sequence_reset(struct mtd_info *mtd)
604{
605 /* common starting sequence for all operations */
606
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100607 struct nand_chip *nand = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100608 struct docg4_priv *doc = nand_get_controller_data(nand);
Mike Dunn570469f2012-01-03 16:05:44 -0800609 void __iomem *docptr = doc->virtadr;
610
611 writew(DOC_CTRL_UNKNOWN | DOC_CTRL_CE, docptr + DOC_FLASHCONTROL);
612 writew(DOC_SEQ_RESET, docptr + DOC_FLASHSEQUENCE);
613 writew(DOC_CMD_RESET, docptr + DOC_FLASHCOMMAND);
614 write_nop(docptr);
615 write_nop(docptr);
616 poll_status(doc);
617 write_nop(docptr);
618}
619
620static void read_page_prologue(struct mtd_info *mtd, uint32_t docg4_addr)
621{
622 /* first step in reading a page */
623
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100624 struct nand_chip *nand = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100625 struct docg4_priv *doc = nand_get_controller_data(nand);
Mike Dunn570469f2012-01-03 16:05:44 -0800626 void __iomem *docptr = doc->virtadr;
627
628 dev_dbg(doc->dev,
629 "docg4: %s: g4 page %08x\n", __func__, docg4_addr);
630
631 sequence_reset(mtd);
632
633 writew(DOCG4_SEQ_PAGE_READ, docptr + DOC_FLASHSEQUENCE);
634 writew(DOCG4_CMD_PAGE_READ, docptr + DOC_FLASHCOMMAND);
635 write_nop(docptr);
636
637 write_addr(doc, docg4_addr);
638
639 write_nop(docptr);
640 writew(DOCG4_CMD_READ2, docptr + DOC_FLASHCOMMAND);
641 write_nop(docptr);
642 write_nop(docptr);
643
644 poll_status(doc);
645}
646
647static void write_page_prologue(struct mtd_info *mtd, uint32_t docg4_addr)
648{
649 /* first step in writing a page */
650
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100651 struct nand_chip *nand = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100652 struct docg4_priv *doc = nand_get_controller_data(nand);
Mike Dunn570469f2012-01-03 16:05:44 -0800653 void __iomem *docptr = doc->virtadr;
654
655 dev_dbg(doc->dev,
656 "docg4: %s: g4 addr: %x\n", __func__, docg4_addr);
657 sequence_reset(mtd);
Mike Dunn5a90d412012-12-07 12:07:21 -0800658
659 if (unlikely(reliable_mode)) {
660 writew(DOCG4_SEQ_SETMODE, docptr + DOC_FLASHSEQUENCE);
661 writew(DOCG4_CMD_FAST_MODE, docptr + DOC_FLASHCOMMAND);
662 writew(DOC_CMD_RELIABLE_MODE, docptr + DOC_FLASHCOMMAND);
663 write_nop(docptr);
664 }
665
Mike Dunn570469f2012-01-03 16:05:44 -0800666 writew(DOCG4_SEQ_PAGEWRITE, docptr + DOC_FLASHSEQUENCE);
667 writew(DOCG4_CMD_PAGEWRITE, docptr + DOC_FLASHCOMMAND);
668 write_nop(docptr);
669 write_addr(doc, docg4_addr);
670 write_nop(docptr);
671 write_nop(docptr);
672 poll_status(doc);
673}
674
675static uint32_t mtd_to_docg4_address(int page, int column)
676{
677 /*
678 * Convert mtd address to format used by the device, 32 bit packed.
679 *
680 * Some notes on G4 addressing... The M-Sys documentation on this device
681 * claims that pages are 2K in length, and indeed, the format of the
682 * address used by the device reflects that. But within each page are
683 * four 512 byte "sub-pages", each with its own oob data that is
684 * read/written immediately after the 512 bytes of page data. This oob
685 * data contains the ecc bytes for the preceeding 512 bytes.
686 *
687 * Rather than tell the mtd nand infrastructure that page size is 2k,
688 * with four sub-pages each, we engage in a little subterfuge and tell
689 * the infrastructure code that pages are 512 bytes in size. This is
690 * done because during the course of reverse-engineering the device, I
691 * never observed an instance where an entire 2K "page" was read or
692 * written as a unit. Each "sub-page" is always addressed individually,
693 * its data read/written, and ecc handled before the next "sub-page" is
694 * addressed.
695 *
696 * This requires us to convert addresses passed by the mtd nand
697 * infrastructure code to those used by the device.
698 *
699 * The address that is written to the device consists of four bytes: the
700 * first two are the 2k page number, and the second is the index into
701 * the page. The index is in terms of 16-bit half-words and includes
702 * the preceeding oob data, so e.g., the index into the second
703 * "sub-page" is 0x108, and the full device address of the start of mtd
704 * page 0x201 is 0x00800108.
705 */
706 int g4_page = page / 4; /* device's 2K page */
707 int g4_index = (page % 4) * 0x108 + column/2; /* offset into page */
708 return (g4_page << 16) | g4_index; /* pack */
709}
710
711static void docg4_command(struct mtd_info *mtd, unsigned command, int column,
712 int page_addr)
713{
714 /* handle standard nand commands */
715
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100716 struct nand_chip *nand = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100717 struct docg4_priv *doc = nand_get_controller_data(nand);
Mike Dunn570469f2012-01-03 16:05:44 -0800718 uint32_t g4_addr = mtd_to_docg4_address(page_addr, column);
719
720 dev_dbg(doc->dev, "%s %x, page_addr=%x, column=%x\n",
721 __func__, command, page_addr, column);
722
723 /*
724 * Save the command and its arguments. This enables emulation of
725 * standard flash devices, and also some optimizations.
726 */
727 doc->last_command.command = command;
728 doc->last_command.column = column;
729 doc->last_command.page = page_addr;
730
731 switch (command) {
732
733 case NAND_CMD_RESET:
734 reset(mtd);
735 break;
736
737 case NAND_CMD_READ0:
738 read_page_prologue(mtd, g4_addr);
739 break;
740
741 case NAND_CMD_STATUS:
742 /* next call to read_byte() will expect a status */
743 break;
744
745 case NAND_CMD_SEQIN:
Mike Dunn5a90d412012-12-07 12:07:21 -0800746 if (unlikely(reliable_mode)) {
747 uint16_t g4_page = g4_addr >> 16;
748
749 /* writes to odd-numbered 2k pages are invalid */
750 if (g4_page & 0x01)
751 dev_warn(doc->dev,
752 "invalid reliable mode address\n");
753 }
754
Mike Dunn570469f2012-01-03 16:05:44 -0800755 write_page_prologue(mtd, g4_addr);
756
757 /* hack for deferred write of oob bytes */
758 if (doc->oob_page == page_addr)
759 memcpy(nand->oob_poi, doc->oob_buf, 16);
760 break;
761
762 case NAND_CMD_PAGEPROG:
763 pageprog(mtd);
764 break;
765
766 /* we don't expect these, based on review of nand_base.c */
767 case NAND_CMD_READOOB:
768 case NAND_CMD_READID:
769 case NAND_CMD_ERASE1:
770 case NAND_CMD_ERASE2:
771 dev_warn(doc->dev, "docg4_command: "
772 "unexpected nand command 0x%x\n", command);
773 break;
774
775 }
776}
777
778static int read_page(struct mtd_info *mtd, struct nand_chip *nand,
779 uint8_t *buf, int page, bool use_ecc)
780{
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100781 struct docg4_priv *doc = nand_get_controller_data(nand);
Mike Dunn570469f2012-01-03 16:05:44 -0800782 void __iomem *docptr = doc->virtadr;
783 uint16_t status, edc_err, *buf16;
Mike Dunn3f91e942012-04-25 12:06:09 -0700784 int bits_corrected = 0;
Mike Dunn570469f2012-01-03 16:05:44 -0800785
786 dev_dbg(doc->dev, "%s: page %08x\n", __func__, page);
787
Boris Brezillon25f815f2017-11-30 18:01:30 +0100788 nand_read_page_op(nand, page, 0, NULL, 0);
789
Mike Dunn570469f2012-01-03 16:05:44 -0800790 writew(DOC_ECCCONF0_READ_MODE |
791 DOC_ECCCONF0_ECC_ENABLE |
792 DOC_ECCCONF0_UNKNOWN |
793 DOCG4_BCH_SIZE,
794 docptr + DOC_ECCCONF0);
795 write_nop(docptr);
796 write_nop(docptr);
797 write_nop(docptr);
798 write_nop(docptr);
799 write_nop(docptr);
800
801 /* the 1st byte from the I/O reg is a status; the rest is page data */
802 status = readw(docptr + DOC_IOSPACE_DATA);
803 if (status & DOCG4_READ_ERROR) {
804 dev_err(doc->dev,
805 "docg4_read_page: bad status: 0x%02x\n", status);
806 writew(0, docptr + DOC_DATAEND);
807 return -EIO;
808 }
809
810 dev_dbg(doc->dev, "%s: status = 0x%x\n", __func__, status);
811
812 docg4_read_buf(mtd, buf, DOCG4_PAGE_SIZE); /* read the page data */
813
Mike Dunnaa6d01f2012-07-11 11:08:19 -0700814 /* this device always reads oob after page data */
Mike Dunn570469f2012-01-03 16:05:44 -0800815 /* first 14 oob bytes read from I/O reg */
Mike Dunnaa6d01f2012-07-11 11:08:19 -0700816 docg4_read_buf(mtd, nand->oob_poi, 14);
Mike Dunn570469f2012-01-03 16:05:44 -0800817
818 /* last 2 read from another reg */
Mike Dunnaa6d01f2012-07-11 11:08:19 -0700819 buf16 = (uint16_t *)(nand->oob_poi + 14);
Mike Dunn570469f2012-01-03 16:05:44 -0800820 *buf16 = readw(docptr + DOCG4_MYSTERY_REG);
821
822 write_nop(docptr);
823
824 if (likely(use_ecc == true)) {
825
826 /* read the register that tells us if bitflip(s) detected */
827 edc_err = readw(docptr + DOC_ECCCONF1);
828 edc_err = readw(docptr + DOC_ECCCONF1);
829 dev_dbg(doc->dev, "%s: edc_err = 0x%02x\n", __func__, edc_err);
830
831 /* If bitflips are reported, attempt to correct with ecc */
832 if (edc_err & DOC_ECCCONF1_BCH_SYNDROM_ERR) {
Mike Dunn3f91e942012-04-25 12:06:09 -0700833 bits_corrected = correct_data(mtd, buf, page);
Mike Dunn570469f2012-01-03 16:05:44 -0800834 if (bits_corrected == -EBADMSG)
835 mtd->ecc_stats.failed++;
836 else
837 mtd->ecc_stats.corrected += bits_corrected;
838 }
839 }
840
841 writew(0, docptr + DOC_DATAEND);
Mike Dunn5bf3d662012-09-11 08:50:50 -0700842 if (bits_corrected == -EBADMSG) /* uncorrectable errors */
843 return 0;
Mike Dunn3f91e942012-04-25 12:06:09 -0700844 return bits_corrected;
Mike Dunn570469f2012-01-03 16:05:44 -0800845}
846
847
Boris Brezillonb9761682018-09-06 14:05:20 +0200848static int docg4_read_page_raw(struct nand_chip *nand, uint8_t *buf,
849 int oob_required, int page)
Mike Dunn570469f2012-01-03 16:05:44 -0800850{
Boris Brezillonb9761682018-09-06 14:05:20 +0200851 return read_page(nand_to_mtd(nand), nand, buf, page, false);
Mike Dunn570469f2012-01-03 16:05:44 -0800852}
853
Boris Brezillonb9761682018-09-06 14:05:20 +0200854static int docg4_read_page(struct nand_chip *nand, uint8_t *buf,
855 int oob_required, int page)
Mike Dunn570469f2012-01-03 16:05:44 -0800856{
Boris Brezillonb9761682018-09-06 14:05:20 +0200857 return read_page(nand_to_mtd(nand), nand, buf, page, true);
Mike Dunn570469f2012-01-03 16:05:44 -0800858}
859
Boris Brezillonb9761682018-09-06 14:05:20 +0200860static int docg4_read_oob(struct nand_chip *nand, int page)
Mike Dunn570469f2012-01-03 16:05:44 -0800861{
Boris Brezillonb9761682018-09-06 14:05:20 +0200862 struct mtd_info *mtd = nand_to_mtd(nand);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100863 struct docg4_priv *doc = nand_get_controller_data(nand);
Mike Dunn570469f2012-01-03 16:05:44 -0800864 void __iomem *docptr = doc->virtadr;
865 uint16_t status;
866
867 dev_dbg(doc->dev, "%s: page %x\n", __func__, page);
868
Boris Brezillon97d90da2017-11-30 18:01:29 +0100869 nand_read_page_op(nand, page, nand->ecc.size, NULL, 0);
Mike Dunn570469f2012-01-03 16:05:44 -0800870
871 writew(DOC_ECCCONF0_READ_MODE | DOCG4_OOB_SIZE, docptr + DOC_ECCCONF0);
872 write_nop(docptr);
873 write_nop(docptr);
874 write_nop(docptr);
875 write_nop(docptr);
876 write_nop(docptr);
877
878 /* the 1st byte from the I/O reg is a status; the rest is oob data */
879 status = readw(docptr + DOC_IOSPACE_DATA);
880 if (status & DOCG4_READ_ERROR) {
881 dev_warn(doc->dev,
882 "docg4_read_oob failed: status = 0x%02x\n", status);
883 return -EIO;
884 }
885
886 dev_dbg(doc->dev, "%s: status = 0x%x\n", __func__, status);
887
888 docg4_read_buf(mtd, nand->oob_poi, 16);
889
890 write_nop(docptr);
891 write_nop(docptr);
892 write_nop(docptr);
893 writew(0, docptr + DOC_DATAEND);
894 write_nop(docptr);
895
896 return 0;
897}
898
Brian Norris49c50b92014-05-06 16:02:19 -0700899static int docg4_erase_block(struct mtd_info *mtd, int page)
Mike Dunn570469f2012-01-03 16:05:44 -0800900{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100901 struct nand_chip *nand = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100902 struct docg4_priv *doc = nand_get_controller_data(nand);
Mike Dunn570469f2012-01-03 16:05:44 -0800903 void __iomem *docptr = doc->virtadr;
904 uint16_t g4_page;
Miquel Raynaleb945552017-11-30 18:01:28 +0100905 int status;
Mike Dunn570469f2012-01-03 16:05:44 -0800906
907 dev_dbg(doc->dev, "%s: page %04x\n", __func__, page);
908
909 sequence_reset(mtd);
910
911 writew(DOCG4_SEQ_BLOCKERASE, docptr + DOC_FLASHSEQUENCE);
912 writew(DOC_CMD_PROG_BLOCK_ADDR, docptr + DOC_FLASHCOMMAND);
913 write_nop(docptr);
914
915 /* only 2 bytes of address are written to specify erase block */
916 g4_page = (uint16_t)(page / 4); /* to g4's 2k page addressing */
917 writeb(g4_page & 0xff, docptr + DOC_FLASHADDRESS);
918 g4_page >>= 8;
919 writeb(g4_page & 0xff, docptr + DOC_FLASHADDRESS);
920 write_nop(docptr);
921
922 /* start the erasure */
923 writew(DOC_CMD_ERASECYCLE2, docptr + DOC_FLASHCOMMAND);
924 write_nop(docptr);
925 write_nop(docptr);
926
927 usleep_range(500, 1000); /* erasure is long; take a snooze */
928 poll_status(doc);
929 writew(DOCG4_SEQ_FLUSH, docptr + DOC_FLASHSEQUENCE);
930 writew(DOCG4_CMD_FLUSH, docptr + DOC_FLASHCOMMAND);
931 writew(DOC_ECCCONF0_READ_MODE | 4, docptr + DOC_ECCCONF0);
932 write_nop(docptr);
933 write_nop(docptr);
934 write_nop(docptr);
935 write_nop(docptr);
936 write_nop(docptr);
937
938 read_progstatus(doc);
939
940 writew(0, docptr + DOC_DATAEND);
941 write_nop(docptr);
942 poll_status(doc);
943 write_nop(docptr);
Brian Norris49c50b92014-05-06 16:02:19 -0700944
Miquel Raynaleb945552017-11-30 18:01:28 +0100945 status = nand->waitfunc(mtd, nand);
946 if (status < 0)
947 return status;
948
949 return status & NAND_STATUS_FAIL ? -EIO : 0;
Mike Dunn570469f2012-01-03 16:05:44 -0800950}
951
Josh Wufdbad98d2012-06-25 18:07:45 +0800952static int write_page(struct mtd_info *mtd, struct nand_chip *nand,
Boris Brezillon25f815f2017-11-30 18:01:30 +0100953 const uint8_t *buf, int page, bool use_ecc)
Mike Dunn570469f2012-01-03 16:05:44 -0800954{
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100955 struct docg4_priv *doc = nand_get_controller_data(nand);
Mike Dunn570469f2012-01-03 16:05:44 -0800956 void __iomem *docptr = doc->virtadr;
957 uint8_t ecc_buf[8];
958
959 dev_dbg(doc->dev, "%s...\n", __func__);
960
Boris Brezillon25f815f2017-11-30 18:01:30 +0100961 nand_prog_page_begin_op(nand, page, 0, NULL, 0);
962
Mike Dunn570469f2012-01-03 16:05:44 -0800963 writew(DOC_ECCCONF0_ECC_ENABLE |
964 DOC_ECCCONF0_UNKNOWN |
965 DOCG4_BCH_SIZE,
966 docptr + DOC_ECCCONF0);
967 write_nop(docptr);
968
969 /* write the page data */
970 docg4_write_buf16(mtd, buf, DOCG4_PAGE_SIZE);
971
972 /* oob bytes 0 through 5 are written to I/O reg */
973 docg4_write_buf16(mtd, nand->oob_poi, 6);
974
975 /* oob byte 6 written to a separate reg */
976 writew(nand->oob_poi[6], docptr + DOCG4_OOB_6_7);
977
978 write_nop(docptr);
979 write_nop(docptr);
980
981 /* write hw-generated ecc bytes to oob */
982 if (likely(use_ecc == true)) {
983 /* oob byte 7 is hamming code */
984 uint8_t hamming = readb(docptr + DOC_HAMMINGPARITY);
985 hamming = readb(docptr + DOC_HAMMINGPARITY); /* 2nd read */
986 writew(hamming, docptr + DOCG4_OOB_6_7);
987 write_nop(docptr);
988
989 /* read the 7 bch bytes from ecc regs */
990 read_hw_ecc(docptr, ecc_buf);
991 ecc_buf[7] = 0; /* clear the "page written" flag */
992 }
993
994 /* write user-supplied bytes to oob */
995 else {
996 writew(nand->oob_poi[7], docptr + DOCG4_OOB_6_7);
997 write_nop(docptr);
998 memcpy(ecc_buf, &nand->oob_poi[8], 8);
999 }
1000
1001 docg4_write_buf16(mtd, ecc_buf, 8);
1002 write_nop(docptr);
1003 write_nop(docptr);
1004 writew(0, docptr + DOC_DATAEND);
1005 write_nop(docptr);
Josh Wufdbad98d2012-06-25 18:07:45 +08001006
Boris Brezillon25f815f2017-11-30 18:01:30 +01001007 return nand_prog_page_end_op(nand);
Mike Dunn570469f2012-01-03 16:05:44 -08001008}
1009
Boris Brezillon767eb6f2018-09-06 14:05:21 +02001010static int docg4_write_page_raw(struct nand_chip *nand, const uint8_t *buf,
1011 int oob_required, int page)
Mike Dunn570469f2012-01-03 16:05:44 -08001012{
Boris Brezillon767eb6f2018-09-06 14:05:21 +02001013 return write_page(nand_to_mtd(nand), nand, buf, page, false);
Mike Dunn570469f2012-01-03 16:05:44 -08001014}
1015
Boris Brezillon767eb6f2018-09-06 14:05:21 +02001016static int docg4_write_page(struct nand_chip *nand, const uint8_t *buf,
1017 int oob_required, int page)
Mike Dunn570469f2012-01-03 16:05:44 -08001018{
Boris Brezillon767eb6f2018-09-06 14:05:21 +02001019 return write_page(nand_to_mtd(nand), nand, buf, page, true);
Mike Dunn570469f2012-01-03 16:05:44 -08001020}
1021
Boris Brezillon767eb6f2018-09-06 14:05:21 +02001022static int docg4_write_oob(struct nand_chip *nand, int page)
Mike Dunn570469f2012-01-03 16:05:44 -08001023{
1024 /*
1025 * Writing oob-only is not really supported, because MLC nand must write
1026 * oob bytes at the same time as page data. Nonetheless, we save the
1027 * oob buffer contents here, and then write it along with the page data
1028 * if the same page is subsequently written. This allows user space
1029 * utilities that write the oob data prior to the page data to work
1030 * (e.g., nandwrite). The disdvantage is that, if the intention was to
1031 * write oob only, the operation is quietly ignored. Also, oob can get
1032 * corrupted if two concurrent processes are running nandwrite.
1033 */
1034
1035 /* note that bytes 7..14 are hw generated hamming/ecc and overwritten */
Boris BREZILLONd699ed22015-12-10 09:00:41 +01001036 struct docg4_priv *doc = nand_get_controller_data(nand);
Mike Dunn570469f2012-01-03 16:05:44 -08001037 doc->oob_page = page;
1038 memcpy(doc->oob_buf, nand->oob_poi, 16);
1039 return 0;
1040}
1041
1042static int __init read_factory_bbt(struct mtd_info *mtd)
1043{
1044 /*
1045 * The device contains a read-only factory bad block table. Read it and
1046 * update the memory-based bbt accordingly.
1047 */
1048
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +01001049 struct nand_chip *nand = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +01001050 struct docg4_priv *doc = nand_get_controller_data(nand);
Mike Dunn570469f2012-01-03 16:05:44 -08001051 uint32_t g4_addr = mtd_to_docg4_address(DOCG4_FACTORY_BBT_PAGE, 0);
1052 uint8_t *buf;
Mike Dunn3c9c6d62012-12-07 12:07:23 -08001053 int i, block;
1054 __u32 eccfailed_stats = mtd->ecc_stats.failed;
Mike Dunn570469f2012-01-03 16:05:44 -08001055
1056 buf = kzalloc(DOCG4_PAGE_SIZE, GFP_KERNEL);
1057 if (buf == NULL)
1058 return -ENOMEM;
1059
1060 read_page_prologue(mtd, g4_addr);
Boris Brezillonb9761682018-09-06 14:05:20 +02001061 docg4_read_page(nand, buf, 0, DOCG4_FACTORY_BBT_PAGE);
Mike Dunn570469f2012-01-03 16:05:44 -08001062
1063 /*
1064 * If no memory-based bbt was created, exit. This will happen if module
1065 * parameter ignore_badblocks is set. Then why even call this function?
1066 * For an unknown reason, block erase always fails if it's the first
1067 * operation after device power-up. The above read ensures it never is.
1068 * Ugly, I know.
1069 */
1070 if (nand->bbt == NULL) /* no memory-based bbt */
1071 goto exit;
1072
Mike Dunn3c9c6d62012-12-07 12:07:23 -08001073 if (mtd->ecc_stats.failed > eccfailed_stats) {
1074 /*
1075 * Whoops, an ecc failure ocurred reading the factory bbt.
1076 * It is stored redundantly, so we get another chance.
1077 */
1078 eccfailed_stats = mtd->ecc_stats.failed;
Boris Brezillonb9761682018-09-06 14:05:20 +02001079 docg4_read_page(nand, buf, 0, DOCG4_REDUNDANT_BBT_PAGE);
Mike Dunn3c9c6d62012-12-07 12:07:23 -08001080 if (mtd->ecc_stats.failed > eccfailed_stats) {
1081 dev_warn(doc->dev,
1082 "The factory bbt could not be read!\n");
1083 goto exit;
1084 }
1085 }
1086
Mike Dunn570469f2012-01-03 16:05:44 -08001087 /*
1088 * Parse factory bbt and update memory-based bbt. Factory bbt format is
1089 * simple: one bit per block, block numbers increase left to right (msb
1090 * to lsb). Bit clear means bad block.
1091 */
1092 for (i = block = 0; block < DOCG4_NUMBLOCKS; block += 8, i++) {
1093 int bitnum;
1094 unsigned long bits = ~buf[i];
1095 for_each_set_bit(bitnum, &bits, 8) {
1096 int badblock = block + 7 - bitnum;
1097 nand->bbt[badblock / 4] |=
1098 0x03 << ((badblock % 4) * 2);
1099 mtd->ecc_stats.badblocks++;
1100 dev_notice(doc->dev, "factory-marked bad block: %d\n",
1101 badblock);
1102 }
1103 }
1104 exit:
1105 kfree(buf);
Mike Dunn3c9c6d62012-12-07 12:07:23 -08001106 return 0;
Mike Dunn570469f2012-01-03 16:05:44 -08001107}
1108
1109static int docg4_block_markbad(struct mtd_info *mtd, loff_t ofs)
1110{
1111 /*
1112 * Mark a block as bad. Bad blocks are marked in the oob area of the
1113 * first page of the block. The default scan_bbt() in the nand
1114 * infrastructure code works fine for building the memory-based bbt
1115 * during initialization, as does the nand infrastructure function that
1116 * checks if a block is bad by reading the bbt. This function replaces
1117 * the nand default because writes to oob-only are not supported.
1118 */
1119
1120 int ret, i;
1121 uint8_t *buf;
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +01001122 struct nand_chip *nand = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +01001123 struct docg4_priv *doc = nand_get_controller_data(nand);
Mike Dunn570469f2012-01-03 16:05:44 -08001124 struct nand_bbt_descr *bbtd = nand->badblock_pattern;
Mike Dunn570469f2012-01-03 16:05:44 -08001125 int page = (int)(ofs >> nand->page_shift);
1126 uint32_t g4_addr = mtd_to_docg4_address(page, 0);
1127
1128 dev_dbg(doc->dev, "%s: %08llx\n", __func__, ofs);
1129
1130 if (unlikely(ofs & (DOCG4_BLOCK_SIZE - 1)))
1131 dev_warn(doc->dev, "%s: ofs %llx not start of block!\n",
1132 __func__, ofs);
1133
1134 /* allocate blank buffer for page data */
1135 buf = kzalloc(DOCG4_PAGE_SIZE, GFP_KERNEL);
1136 if (buf == NULL)
1137 return -ENOMEM;
1138
Mike Dunn570469f2012-01-03 16:05:44 -08001139 /* write bit-wise negation of pattern to oob buffer */
1140 memset(nand->oob_poi, 0xff, mtd->oobsize);
1141 for (i = 0; i < bbtd->len; i++)
1142 nand->oob_poi[bbtd->offs + i] = ~bbtd->pattern[i];
1143
1144 /* write first page of block */
1145 write_page_prologue(mtd, g4_addr);
Boris Brezillon767eb6f2018-09-06 14:05:21 +02001146 docg4_write_page(nand, buf, 1, page);
Mike Dunn570469f2012-01-03 16:05:44 -08001147 ret = pageprog(mtd);
Mike Dunn570469f2012-01-03 16:05:44 -08001148
1149 kfree(buf);
1150
1151 return ret;
1152}
1153
Archit Taneja9f3e0422016-02-03 14:29:49 +05301154static int docg4_block_neverbad(struct mtd_info *mtd, loff_t ofs)
Mike Dunn570469f2012-01-03 16:05:44 -08001155{
1156 /* only called when module_param ignore_badblocks is set */
1157 return 0;
1158}
1159
1160static int docg4_suspend(struct platform_device *pdev, pm_message_t state)
1161{
1162 /*
1163 * Put the device into "deep power-down" mode. Note that CE# must be
1164 * deasserted for this to take effect. The xscale, e.g., can be
1165 * configured to float this signal when the processor enters power-down,
1166 * and a suitable pull-up ensures its deassertion.
1167 */
1168
1169 int i;
1170 uint8_t pwr_down;
1171 struct docg4_priv *doc = platform_get_drvdata(pdev);
1172 void __iomem *docptr = doc->virtadr;
1173
1174 dev_dbg(doc->dev, "%s...\n", __func__);
1175
1176 /* poll the register that tells us we're ready to go to sleep */
1177 for (i = 0; i < 10; i++) {
1178 pwr_down = readb(docptr + DOC_POWERMODE);
1179 if (pwr_down & DOC_POWERDOWN_READY)
1180 break;
1181 usleep_range(1000, 4000);
1182 }
1183
1184 if (pwr_down & DOC_POWERDOWN_READY) {
1185 dev_err(doc->dev, "suspend failed; "
1186 "timeout polling DOC_POWERDOWN_READY\n");
1187 return -EIO;
1188 }
1189
1190 writew(DOC_ASICMODE_POWERDOWN | DOC_ASICMODE_MDWREN,
1191 docptr + DOC_ASICMODE);
1192 writew(~(DOC_ASICMODE_POWERDOWN | DOC_ASICMODE_MDWREN),
1193 docptr + DOC_ASICMODECONFIRM);
1194
1195 write_nop(docptr);
1196
1197 return 0;
1198}
1199
1200static int docg4_resume(struct platform_device *pdev)
1201{
1202
1203 /*
1204 * Exit power-down. Twelve consecutive reads of the address below
1205 * accomplishes this, assuming CE# has been asserted.
1206 */
1207
1208 struct docg4_priv *doc = platform_get_drvdata(pdev);
1209 void __iomem *docptr = doc->virtadr;
1210 int i;
1211
1212 dev_dbg(doc->dev, "%s...\n", __func__);
1213
1214 for (i = 0; i < 12; i++)
1215 readb(docptr + 0x1fff);
1216
1217 return 0;
1218}
1219
Geert Uytterhoeven166cd442018-08-23 23:43:45 +02001220static void init_mtd_structs(struct mtd_info *mtd)
Mike Dunn570469f2012-01-03 16:05:44 -08001221{
1222 /* initialize mtd and nand data structures */
1223
1224 /*
1225 * Note that some of the following initializations are not usually
1226 * required within a nand driver because they are performed by the nand
1227 * infrastructure code as part of nand_scan(). In this case they need
1228 * to be initialized here because we skip call to nand_scan_ident() (the
Miquel Raynal66a38472018-07-25 15:31:43 +02001229 * first half of nand_scan()). The call to nand_scan_ident() could be
1230 * skipped because for this device the chip id is not read in the manner
1231 * of a standard nand device.
Mike Dunn570469f2012-01-03 16:05:44 -08001232 */
1233
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +01001234 struct nand_chip *nand = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +01001235 struct docg4_priv *doc = nand_get_controller_data(nand);
Mike Dunn570469f2012-01-03 16:05:44 -08001236
1237 mtd->size = DOCG4_CHIP_SIZE;
1238 mtd->name = "Msys_Diskonchip_G4";
1239 mtd->writesize = DOCG4_PAGE_SIZE;
1240 mtd->erasesize = DOCG4_BLOCK_SIZE;
1241 mtd->oobsize = DOCG4_OOB_SIZE;
Boris Brezillon6b750652016-02-03 20:00:35 +01001242 mtd_set_ooblayout(mtd, &docg4_ooblayout_ops);
Mike Dunn570469f2012-01-03 16:05:44 -08001243 nand->chipsize = DOCG4_CHIP_SIZE;
1244 nand->chip_shift = DOCG4_CHIP_SHIFT;
1245 nand->bbt_erase_shift = nand->phys_erase_shift = DOCG4_ERASE_SHIFT;
1246 nand->chip_delay = 20;
1247 nand->page_shift = DOCG4_PAGE_SHIFT;
1248 nand->pagemask = 0x3ffff;
1249 nand->badblockpos = NAND_LARGE_BADBLOCK_POS;
1250 nand->badblockbits = 8;
Mike Dunn570469f2012-01-03 16:05:44 -08001251 nand->ecc.mode = NAND_ECC_HW_SYNDROME;
1252 nand->ecc.size = DOCG4_PAGE_SIZE;
1253 nand->ecc.prepad = 8;
1254 nand->ecc.bytes = 8;
Mike Dunn6a918ba2012-03-11 14:21:11 -07001255 nand->ecc.strength = DOCG4_T;
Brian Norris1826dbc2012-05-01 17:12:55 -07001256 nand->options = NAND_BUSWIDTH_16 | NAND_NO_SUBPAGE_WRITE;
Mike Dunn570469f2012-01-03 16:05:44 -08001257 nand->IO_ADDR_R = nand->IO_ADDR_W = doc->virtadr + DOC_IOSPACE_DATA;
Miquel Raynal7da45132018-07-17 09:08:02 +02001258 nand->controller = &nand->dummy_controller;
1259 nand_controller_init(nand->controller);
Mike Dunn570469f2012-01-03 16:05:44 -08001260
1261 /* methods */
1262 nand->cmdfunc = docg4_command;
1263 nand->waitfunc = docg4_wait;
1264 nand->select_chip = docg4_select_chip;
1265 nand->read_byte = docg4_read_byte;
1266 nand->block_markbad = docg4_block_markbad;
1267 nand->read_buf = docg4_read_buf;
1268 nand->write_buf = docg4_write_buf16;
Brian Norris49c50b92014-05-06 16:02:19 -07001269 nand->erase = docg4_erase_block;
Miquel Raynalb9587582018-03-19 14:47:19 +01001270 nand->set_features = nand_get_set_features_notsupp;
1271 nand->get_features = nand_get_set_features_notsupp;
Mike Dunn570469f2012-01-03 16:05:44 -08001272 nand->ecc.read_page = docg4_read_page;
1273 nand->ecc.write_page = docg4_write_page;
1274 nand->ecc.read_page_raw = docg4_read_page_raw;
1275 nand->ecc.write_page_raw = docg4_write_page_raw;
1276 nand->ecc.read_oob = docg4_read_oob;
1277 nand->ecc.write_oob = docg4_write_oob;
1278
1279 /*
1280 * The way the nand infrastructure code is written, a memory-based bbt
1281 * is not created if NAND_SKIP_BBTSCAN is set. With no memory bbt,
1282 * nand->block_bad() is used. So when ignoring bad blocks, we skip the
1283 * scan and define a dummy block_bad() which always returns 0.
1284 */
1285 if (ignore_badblocks) {
1286 nand->options |= NAND_SKIP_BBTSCAN;
1287 nand->block_bad = docg4_block_neverbad;
1288 }
1289
1290}
1291
Geert Uytterhoeven166cd442018-08-23 23:43:45 +02001292static int read_id_reg(struct mtd_info *mtd)
Mike Dunn570469f2012-01-03 16:05:44 -08001293{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +01001294 struct nand_chip *nand = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +01001295 struct docg4_priv *doc = nand_get_controller_data(nand);
Mike Dunn570469f2012-01-03 16:05:44 -08001296 void __iomem *docptr = doc->virtadr;
1297 uint16_t id1, id2;
1298
1299 /* check for presence of g4 chip by reading id registers */
1300 id1 = readw(docptr + DOC_CHIPID);
1301 id1 = readw(docptr + DOCG4_MYSTERY_REG);
1302 id2 = readw(docptr + DOC_CHIPID_INV);
1303 id2 = readw(docptr + DOCG4_MYSTERY_REG);
1304
1305 if (id1 == DOCG4_IDREG1_VALUE && id2 == DOCG4_IDREG2_VALUE) {
1306 dev_info(doc->dev,
1307 "NAND device: 128MiB Diskonchip G4 detected\n");
1308 return 0;
1309 }
1310
1311 return -ENODEV;
1312}
1313
1314static char const *part_probes[] = { "cmdlinepart", "saftlpart", NULL };
1315
Miquel Raynal66a38472018-07-25 15:31:43 +02001316static int docg4_attach_chip(struct nand_chip *chip)
1317{
1318 struct mtd_info *mtd = nand_to_mtd(chip);
1319 struct docg4_priv *doc = (struct docg4_priv *)(chip + 1);
1320 int ret;
1321
1322 init_mtd_structs(mtd);
1323
1324 /* Initialize kernel BCH algorithm */
1325 doc->bch = init_bch(DOCG4_M, DOCG4_T, DOCG4_PRIMITIVE_POLY);
1326 if (!doc->bch)
1327 return -EINVAL;
1328
1329 reset(mtd);
1330
1331 ret = read_id_reg(mtd);
1332 if (ret)
1333 free_bch(doc->bch);
1334
1335 return ret;
1336}
1337
1338static void docg4_detach_chip(struct nand_chip *chip)
1339{
1340 struct docg4_priv *doc = (struct docg4_priv *)(chip + 1);
1341
1342 free_bch(doc->bch);
1343}
1344
1345static const struct nand_controller_ops docg4_controller_ops = {
1346 .attach_chip = docg4_attach_chip,
1347 .detach_chip = docg4_detach_chip,
1348};
1349
Mike Dunn570469f2012-01-03 16:05:44 -08001350static int __init probe_docg4(struct platform_device *pdev)
1351{
1352 struct mtd_info *mtd;
1353 struct nand_chip *nand;
1354 void __iomem *virtadr;
1355 struct docg4_priv *doc;
1356 int len, retval;
1357 struct resource *r;
1358 struct device *dev = &pdev->dev;
1359
1360 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1361 if (r == NULL) {
1362 dev_err(dev, "no io memory resource defined!\n");
1363 return -ENODEV;
1364 }
1365
1366 virtadr = ioremap(r->start, resource_size(r));
1367 if (!virtadr) {
Dan Carpenter2c4ae272012-01-31 11:54:06 +03001368 dev_err(dev, "Diskonchip ioremap failed: %pR\n", r);
Mike Dunn570469f2012-01-03 16:05:44 -08001369 return -EIO;
1370 }
1371
Boris BREZILLON5d073792015-12-10 09:00:01 +01001372 len = sizeof(struct nand_chip) + sizeof(struct docg4_priv);
1373 nand = kzalloc(len, GFP_KERNEL);
1374 if (nand == NULL) {
Mike Dunn570469f2012-01-03 16:05:44 -08001375 retval = -ENOMEM;
Miquel Raynal8604e632018-02-25 23:09:14 +01001376 goto unmap;
Mike Dunn570469f2012-01-03 16:05:44 -08001377 }
Boris BREZILLON5d073792015-12-10 09:00:01 +01001378
1379 mtd = nand_to_mtd(nand);
Mike Dunn570469f2012-01-03 16:05:44 -08001380 doc = (struct docg4_priv *) (nand + 1);
Boris BREZILLONd699ed22015-12-10 09:00:41 +01001381 nand_set_controller_data(nand, doc);
Frans Klaver0a1abe72015-06-10 22:38:43 +02001382 mtd->dev.parent = &pdev->dev;
Mike Dunn570469f2012-01-03 16:05:44 -08001383 doc->virtadr = virtadr;
1384 doc->dev = dev;
Mike Dunn570469f2012-01-03 16:05:44 -08001385 platform_set_drvdata(pdev, doc);
1386
Miquel Raynal66a38472018-07-25 15:31:43 +02001387 /*
1388 * Running nand_scan() with maxchips == 0 will skip nand_scan_ident(),
1389 * which is a specific operation with this driver and done in the
1390 * ->attach_chip callback.
1391 */
1392 nand->dummy_controller.ops = &docg4_controller_ops;
Boris Brezillon00ad3782018-09-06 14:05:14 +02001393 retval = nand_scan(nand, 0);
Mike Dunn570469f2012-01-03 16:05:44 -08001394 if (retval)
Miquel Raynal66a38472018-07-25 15:31:43 +02001395 goto free_nand;
Mike Dunn570469f2012-01-03 16:05:44 -08001396
1397 retval = read_factory_bbt(mtd);
1398 if (retval)
Miquel Raynal8604e632018-02-25 23:09:14 +01001399 goto cleanup_nand;
Mike Dunn570469f2012-01-03 16:05:44 -08001400
1401 retval = mtd_device_parse_register(mtd, part_probes, NULL, NULL, 0);
1402 if (retval)
Miquel Raynal8604e632018-02-25 23:09:14 +01001403 goto cleanup_nand;
Mike Dunn570469f2012-01-03 16:05:44 -08001404
1405 doc->mtd = mtd;
Miquel Raynal8604e632018-02-25 23:09:14 +01001406
Mike Dunn570469f2012-01-03 16:05:44 -08001407 return 0;
1408
Miquel Raynal8604e632018-02-25 23:09:14 +01001409cleanup_nand:
1410 nand_cleanup(nand);
Miquel Raynal8604e632018-02-25 23:09:14 +01001411free_nand:
Brian Norris2d374392015-12-18 11:39:53 -08001412 kfree(nand);
Miquel Raynal8604e632018-02-25 23:09:14 +01001413unmap:
Boris BREZILLON5d073792015-12-10 09:00:01 +01001414 iounmap(virtadr);
1415
Mike Dunn570469f2012-01-03 16:05:44 -08001416 return retval;
1417}
1418
1419static int __exit cleanup_docg4(struct platform_device *pdev)
1420{
1421 struct docg4_priv *doc = platform_get_drvdata(pdev);
Boris Brezillon59ac2762018-09-06 14:05:15 +02001422 nand_release(mtd_to_nand(doc->mtd));
Boris BREZILLON5d073792015-12-10 09:00:01 +01001423 kfree(mtd_to_nand(doc->mtd));
Mike Dunn570469f2012-01-03 16:05:44 -08001424 iounmap(doc->virtadr);
1425 return 0;
1426}
1427
1428static struct platform_driver docg4_driver = {
1429 .driver = {
1430 .name = "docg4",
Mike Dunn570469f2012-01-03 16:05:44 -08001431 },
1432 .suspend = docg4_suspend,
1433 .resume = docg4_resume,
1434 .remove = __exit_p(cleanup_docg4),
1435};
1436
Jingoo Han725a2272013-03-05 13:28:33 +09001437module_platform_driver_probe(docg4_driver, probe_docg4);
Mike Dunn570469f2012-01-03 16:05:44 -08001438
1439MODULE_LICENSE("GPL");
1440MODULE_AUTHOR("Mike Dunn");
1441MODULE_DESCRIPTION("M-Systems DiskOnChip G4 device driver");