Merge tag 'mtd/fixes-for-6.9-rc6' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux-2.6-block.git] / drivers / mtd / nand / raw / diskonchip.c
CommitLineData
09c434b8 1// SPDX-License-Identifier: GPL-2.0-only
61b03bd7 2/*
1da177e4
LT
3 * (C) 2003 Red Hat, Inc.
4 * (C) 2004 Dan Brown <dan_brown@ieee.org>
5 * (C) 2004 Kalev Lember <kalev@smartlink.ee>
6 *
7 * Author: David Woodhouse <dwmw2@infradead.org>
8 * Additional Diskonchip 2000 and Millennium support by Dan Brown <dan_brown@ieee.org>
9 * Diskonchip Millennium Plus support by Kalev Lember <kalev@smartlink.ee>
61b03bd7 10 *
1da177e4 11 * Error correction code lifted from the old docecc code
61b03bd7 12 * Author: Fabrice Bellard (fabrice.bellard@netgem.com)
1da177e4
LT
13 * Copyright (C) 2000 Netgem S.A.
14 * converted to the generic Reed-Solomon library by Thomas Gleixner <tglx@linutronix.de>
61b03bd7 15 *
1da177e4 16 * Interface to generic NAND code for M-Systems DiskOnChip devices
1da177e4
LT
17 */
18
19#include <linux/kernel.h>
20#include <linux/init.h>
21#include <linux/sched.h>
22#include <linux/delay.h>
23#include <linux/rslib.h>
24#include <linux/moduleparam.h>
5a0e3ad6 25#include <linux/slab.h>
2584cf83 26#include <linux/io.h>
1da177e4
LT
27
28#include <linux/mtd/mtd.h>
d4092d76 29#include <linux/mtd/rawnand.h>
1da177e4 30#include <linux/mtd/doc2000.h>
1da177e4
LT
31#include <linux/mtd/partitions.h>
32#include <linux/mtd/inftl.h>
a0e5cc58 33#include <linux/module.h>
1da177e4
LT
34
35/* Where to look for the devices? */
651078ba
TG
36#ifndef CONFIG_MTD_NAND_DISKONCHIP_PROBE_ADDRESS
37#define CONFIG_MTD_NAND_DISKONCHIP_PROBE_ADDRESS 0
1da177e4
LT
38#endif
39
14a95b8a 40static unsigned long doc_locations[] __initdata = {
1da177e4 41#if defined (__alpha__) || defined(__i386__) || defined(__x86_64__)
651078ba 42#ifdef CONFIG_MTD_NAND_DISKONCHIP_PROBE_HIGH
61b03bd7 43 0xfffc8000, 0xfffca000, 0xfffcc000, 0xfffce000,
1da177e4 44 0xfffd0000, 0xfffd2000, 0xfffd4000, 0xfffd6000,
61b03bd7
TG
45 0xfffd8000, 0xfffda000, 0xfffdc000, 0xfffde000,
46 0xfffe0000, 0xfffe2000, 0xfffe4000, 0xfffe6000,
1da177e4 47 0xfffe8000, 0xfffea000, 0xfffec000, 0xfffee000,
9d403496 48#else
61b03bd7 49 0xc8000, 0xca000, 0xcc000, 0xce000,
1da177e4 50 0xd0000, 0xd2000, 0xd4000, 0xd6000,
61b03bd7
TG
51 0xd8000, 0xda000, 0xdc000, 0xde000,
52 0xe0000, 0xe2000, 0xe4000, 0xe6000,
1da177e4 53 0xe8000, 0xea000, 0xec000, 0xee000,
9d403496 54#endif
1da177e4 55#endif
21c9fb61 56};
1da177e4
LT
57
58static struct mtd_info *doclist = NULL;
59
60struct doc_priv {
f37b1d3c 61 struct nand_controller base;
1da177e4
LT
62 void __iomem *virtadr;
63 unsigned long physadr;
64 u_char ChipID;
65 u_char CDSNControl;
e0c7d767 66 int chips_per_floor; /* The number of chips detected on each floor */
1da177e4
LT
67 int curfloor;
68 int curchip;
69 int mh0_page;
70 int mh1_page;
964dfce9 71 struct rs_control *rs_decoder;
1da177e4 72 struct mtd_info *nextdoc;
fddf5cec 73 bool supports_32b_reads;
d24fe0c3
BN
74
75 /* Handle the last stage of initialization (BBT scan, partitioning) */
76 int (*late_init)(struct mtd_info *mtd);
1da177e4
LT
77};
78
1da177e4
LT
79/* This is the ecc value computed by the HW ecc generator upon writing an empty
80 page, one with all 0xff for data. */
81static u_char empty_write_ecc[6] = { 0x4b, 0x00, 0xe2, 0x0e, 0x93, 0xf7 };
82
83#define INFTL_BBT_RESERVED_BLOCKS 4
84
85#define DoC_is_MillenniumPlus(doc) ((doc)->ChipID == DOC_ChipID_DocMilPlus16 || (doc)->ChipID == DOC_ChipID_DocMilPlus32)
86#define DoC_is_Millennium(doc) ((doc)->ChipID == DOC_ChipID_DocMil)
87#define DoC_is_2000(doc) ((doc)->ChipID == DOC_ChipID_Doc2k)
88
e0c7d767 89static int debug = 0;
1da177e4
LT
90module_param(debug, int, 0);
91
e0c7d767 92static int try_dword = 1;
1da177e4
LT
93module_param(try_dword, int, 0);
94
e0c7d767 95static int no_ecc_failures = 0;
1da177e4
LT
96module_param(no_ecc_failures, int, 0);
97
e0c7d767 98static int no_autopart = 0;
1da177e4 99module_param(no_autopart, int, 0);
1a78ff6b 100
e0c7d767 101static int show_firmware_partition = 0;
1a78ff6b 102module_param(show_firmware_partition, int, 0);
1da177e4 103
89e2bf61 104#ifdef CONFIG_MTD_NAND_DISKONCHIP_BBTWRITE
e0c7d767 105static int inftl_bbt_write = 1;
1da177e4 106#else
e0c7d767 107static int inftl_bbt_write = 0;
1da177e4
LT
108#endif
109module_param(inftl_bbt_write, int, 0);
110
651078ba 111static unsigned long doc_config_location = CONFIG_MTD_NAND_DISKONCHIP_PROBE_ADDRESS;
1da177e4
LT
112module_param(doc_config_location, ulong, 0);
113MODULE_PARM_DESC(doc_config_location, "Physical memory address at which to probe for DiskOnChip");
114
1da177e4
LT
115/* Sector size for HW ECC */
116#define SECTOR_SIZE 512
117/* The sector bytes are packed into NB_DATA 10 bit words */
118#define NB_DATA (((SECTOR_SIZE + 1) * 8 + 6) / 10)
119/* Number of roots */
120#define NROOTS 4
121/* First consective root */
122#define FCR 510
123/* Number of symbols */
124#define NN 1023
125
61b03bd7 126/*
1da177e4 127 * The HW decoder in the DoC ASIC's provides us a error syndrome,
7854d3f7 128 * which we must convert to a standard syndrome usable by the generic
1da177e4
LT
129 * Reed-Solomon library code.
130 *
131 * Fabrice Bellard figured this out in the old docecc code. I added
132 * some comments, improved a minor bit and converted it to make use
25985edc 133 * of the generic Reed-Solomon library. tglx
1da177e4 134 */
e0c7d767 135static int doc_ecc_decode(struct rs_control *rs, uint8_t *data, uint8_t *ecc)
1da177e4
LT
136{
137 int i, j, nerr, errpos[8];
138 uint8_t parity;
139 uint16_t ds[4], s[5], tmp, errval[8], syn[4];
21633981 140 struct rs_codec *cd = rs->codec;
1da177e4 141
c9fb6773 142 memset(syn, 0, sizeof(syn));
1da177e4
LT
143 /* Convert the ecc bytes into words */
144 ds[0] = ((ecc[4] & 0xff) >> 0) | ((ecc[5] & 0x03) << 8);
145 ds[1] = ((ecc[5] & 0xfc) >> 2) | ((ecc[2] & 0x0f) << 6);
146 ds[2] = ((ecc[2] & 0xf0) >> 4) | ((ecc[3] & 0x3f) << 4);
147 ds[3] = ((ecc[3] & 0xc0) >> 6) | ((ecc[0] & 0xff) << 2);
148 parity = ecc[1];
149
7854d3f7 150 /* Initialize the syndrome buffer */
1da177e4
LT
151 for (i = 0; i < NROOTS; i++)
152 s[i] = ds[0];
61b03bd7
TG
153 /*
154 * Evaluate
1da177e4
LT
155 * s[i] = ds[3]x^3 + ds[2]x^2 + ds[1]x^1 + ds[0]
156 * where x = alpha^(FCR + i)
157 */
e0c7d767
DW
158 for (j = 1; j < NROOTS; j++) {
159 if (ds[j] == 0)
1da177e4 160 continue;
21633981 161 tmp = cd->index_of[ds[j]];
e0c7d767 162 for (i = 0; i < NROOTS; i++)
21633981 163 s[i] ^= cd->alpha_to[rs_modnn(cd, tmp + (FCR + i) * j)];
1da177e4
LT
164 }
165
c9fb6773 166 /* Calc syn[i] = s[i] / alpha^(v + i) */
1da177e4 167 for (i = 0; i < NROOTS; i++) {
c9fb6773 168 if (s[i])
21633981 169 syn[i] = rs_modnn(cd, cd->index_of[s[i]] + (NN - FCR - i));
1da177e4
LT
170 }
171 /* Call the decoder library */
172 nerr = decode_rs16(rs, NULL, NULL, 1019, syn, 0, errpos, 0, errval);
173
174 /* Incorrectable errors ? */
175 if (nerr < 0)
176 return nerr;
177
61b03bd7 178 /*
1da177e4
LT
179 * Correct the errors. The bitpositions are a bit of magic,
180 * but they are given by the design of the de/encoder circuit
181 * in the DoC ASIC's.
182 */
e0c7d767 183 for (i = 0; i < nerr; i++) {
1da177e4
LT
184 int index, bitpos, pos = 1015 - errpos[i];
185 uint8_t val;
186 if (pos >= NB_DATA && pos < 1019)
187 continue;
188 if (pos < NB_DATA) {
189 /* extract bit position (MSB first) */
190 pos = 10 * (NB_DATA - 1 - pos) - 6;
191 /* now correct the following 10 bits. At most two bytes
192 can be modified since pos is even */
193 index = (pos >> 3) ^ 1;
194 bitpos = pos & 7;
e0c7d767 195 if ((index >= 0 && index < SECTOR_SIZE) || index == (SECTOR_SIZE + 1)) {
1da177e4
LT
196 val = (uint8_t) (errval[i] >> (2 + bitpos));
197 parity ^= val;
198 if (index < SECTOR_SIZE)
199 data[index] ^= val;
200 }
201 index = ((pos >> 3) + 1) ^ 1;
202 bitpos = (bitpos + 10) & 7;
203 if (bitpos == 0)
204 bitpos = 8;
e0c7d767
DW
205 if ((index >= 0 && index < SECTOR_SIZE) || index == (SECTOR_SIZE + 1)) {
206 val = (uint8_t) (errval[i] << (8 - bitpos));
1da177e4
LT
207 parity ^= val;
208 if (index < SECTOR_SIZE)
209 data[index] ^= val;
210 }
211 }
212 }
213 /* If the parity is wrong, no rescue possible */
eb684507 214 return parity ? -EBADMSG : nerr;
1da177e4
LT
215}
216
217static void DoC_Delay(struct doc_priv *doc, unsigned short cycles)
218{
63c34f21 219 volatile char __always_unused dummy;
1da177e4 220 int i;
61b03bd7 221
1da177e4
LT
222 for (i = 0; i < cycles; i++) {
223 if (DoC_is_Millennium(doc))
224 dummy = ReadDOC(doc->virtadr, NOP);
225 else if (DoC_is_MillenniumPlus(doc))
226 dummy = ReadDOC(doc->virtadr, Mplus_NOP);
227 else
228 dummy = ReadDOC(doc->virtadr, DOCStatus);
229 }
61b03bd7 230
1da177e4
LT
231}
232
233#define CDSN_CTRL_FR_B_MASK (CDSN_CTRL_FR_B0 | CDSN_CTRL_FR_B1)
234
235/* DOC_WaitReady: Wait for RDY line to be asserted by the flash chip */
236static int _DoC_WaitReady(struct doc_priv *doc)
237{
e0c7d767 238 void __iomem *docptr = doc->virtadr;
1da177e4
LT
239 unsigned long timeo = jiffies + (HZ * 10);
240
e0c7d767
DW
241 if (debug)
242 printk("_DoC_WaitReady...\n");
1da177e4
LT
243 /* Out-of-line routine to wait for chip response */
244 if (DoC_is_MillenniumPlus(doc)) {
245 while ((ReadDOC(docptr, Mplus_FlashControl) & CDSN_CTRL_FR_B_MASK) != CDSN_CTRL_FR_B_MASK) {
246 if (time_after(jiffies, timeo)) {
247 printk("_DoC_WaitReady timed out.\n");
248 return -EIO;
249 }
250 udelay(1);
251 cond_resched();
252 }
253 } else {
254 while (!(ReadDOC(docptr, CDSNControl) & CDSN_CTRL_FR_B)) {
255 if (time_after(jiffies, timeo)) {
256 printk("_DoC_WaitReady timed out.\n");
257 return -EIO;
258 }
259 udelay(1);
260 cond_resched();
261 }
262 }
263
264 return 0;
265}
266
267static inline int DoC_WaitReady(struct doc_priv *doc)
268{
e0c7d767 269 void __iomem *docptr = doc->virtadr;
1da177e4
LT
270 int ret = 0;
271
272 if (DoC_is_MillenniumPlus(doc)) {
273 DoC_Delay(doc, 4);
274
275 if ((ReadDOC(docptr, Mplus_FlashControl) & CDSN_CTRL_FR_B_MASK) != CDSN_CTRL_FR_B_MASK)
276 /* Call the out-of-line routine to wait */
277 ret = _DoC_WaitReady(doc);
278 } else {
279 DoC_Delay(doc, 4);
280
281 if (!(ReadDOC(docptr, CDSNControl) & CDSN_CTRL_FR_B))
282 /* Call the out-of-line routine to wait */
283 ret = _DoC_WaitReady(doc);
284 DoC_Delay(doc, 2);
285 }
286
e0c7d767
DW
287 if (debug)
288 printk("DoC_WaitReady OK\n");
1da177e4
LT
289 return ret;
290}
291
c0739d85 292static void doc2000_write_byte(struct nand_chip *this, u_char datum)
1da177e4 293{
d699ed25 294 struct doc_priv *doc = nand_get_controller_data(this);
e0c7d767 295 void __iomem *docptr = doc->virtadr;
1da177e4 296
e0c7d767
DW
297 if (debug)
298 printk("write_byte %02x\n", datum);
1da177e4
LT
299 WriteDOC(datum, docptr, CDSNSlowIO);
300 WriteDOC(datum, docptr, 2k_CDSN_IO);
301}
302
c0739d85
BB
303static void doc2000_writebuf(struct nand_chip *this, const u_char *buf,
304 int len)
1da177e4 305{
d699ed25 306 struct doc_priv *doc = nand_get_controller_data(this);
e0c7d767 307 void __iomem *docptr = doc->virtadr;
1da177e4 308 int i;
e0c7d767
DW
309 if (debug)
310 printk("writebuf of %d bytes: ", len);
311 for (i = 0; i < len; i++) {
1da177e4
LT
312 WriteDOC_(buf[i], docptr, DoC_2k_CDSN_IO + i);
313 if (debug && i < 16)
314 printk("%02x ", buf[i]);
315 }
e0c7d767
DW
316 if (debug)
317 printk("\n");
1da177e4
LT
318}
319
7e534323 320static void doc2000_readbuf(struct nand_chip *this, u_char *buf, int len)
1da177e4 321{
d699ed25 322 struct doc_priv *doc = nand_get_controller_data(this);
e0c7d767 323 void __iomem *docptr = doc->virtadr;
fddf5cec 324 u32 *buf32 = (u32 *)buf;
e0c7d767 325 int i;
1da177e4 326
e0c7d767
DW
327 if (debug)
328 printk("readbuf of %d bytes: ", len);
1da177e4 329
fddf5cec
BB
330 if (!doc->supports_32b_reads ||
331 ((((unsigned long)buf) | len) & 3)) {
332 for (i = 0; i < len; i++)
333 buf[i] = ReadDOC(docptr, 2k_CDSN_IO + i);
1da177e4 334 } else {
fddf5cec
BB
335 for (i = 0; i < len / 4; i++)
336 buf32[i] = readl(docptr + DoC_2k_CDSN_IO + i);
1da177e4
LT
337 }
338}
339
f46eb7af
BB
340/*
341 * We need our own readid() here because it's called before the NAND chip
342 * has been initialized, and calling nand_op_readid() would lead to a NULL
343 * pointer exception when dereferencing the NAND timings.
344 */
345static void doc200x_readid(struct nand_chip *this, unsigned int cs, u8 *id)
346{
347 u8 addr = 0;
348 struct nand_op_instr instrs[] = {
349 NAND_OP_CMD(NAND_CMD_READID, 0),
350 NAND_OP_ADDR(1, &addr, 50),
351 NAND_OP_8BIT_DATA_IN(2, id, 0),
352 };
353
354 struct nand_operation op = NAND_OPERATION(cs, instrs);
355
356 if (!id)
357 op.ninstrs--;
358
359 this->controller->ops->exec_op(this, &op, false);
360}
361
1da177e4
LT
362static uint16_t __init doc200x_ident_chip(struct mtd_info *mtd, int nr)
363{
4bd4ebcc 364 struct nand_chip *this = mtd_to_nand(mtd);
d699ed25 365 struct doc_priv *doc = nand_get_controller_data(this);
1da177e4 366 uint16_t ret;
f46eb7af 367 u8 id[2];
1da177e4 368
f46eb7af 369 doc200x_readid(this, nr, id);
61b03bd7 370
f46eb7af 371 ret = ((u16)id[0] << 8) | id[1];
1da177e4
LT
372
373 if (doc->ChipID == DOC_ChipID_Doc2k && try_dword && !nr) {
374 /* First chip probe. See if we get same results by 32-bit access */
375 union {
376 uint32_t dword;
377 uint8_t byte[4];
378 } ident;
379 void __iomem *docptr = doc->virtadr;
380
f46eb7af 381 doc200x_readid(this, nr, NULL);
dfd61294 382
1da177e4
LT
383 ident.dword = readl(docptr + DoC_2k_CDSN_IO);
384 if (((ident.byte[0] << 8) | ident.byte[1]) == ret) {
63fa37f0 385 pr_info("DiskOnChip 2000 responds to DWORD access\n");
fddf5cec 386 doc->supports_32b_reads = true;
1da177e4
LT
387 }
388 }
61b03bd7 389
1da177e4
LT
390 return ret;
391}
392
393static void __init doc2000_count_chips(struct mtd_info *mtd)
394{
4bd4ebcc 395 struct nand_chip *this = mtd_to_nand(mtd);
d699ed25 396 struct doc_priv *doc = nand_get_controller_data(this);
1da177e4
LT
397 uint16_t mfrid;
398 int i;
399
400 /* Max 4 chips per floor on DiskOnChip 2000 */
401 doc->chips_per_floor = 4;
402
403 /* Find out what the first chip is */
404 mfrid = doc200x_ident_chip(mtd, 0);
405
406 /* Find how many chips in each floor. */
407 for (i = 1; i < 4; i++) {
408 if (doc200x_ident_chip(mtd, i) != mfrid)
409 break;
410 }
411 doc->chips_per_floor = i;
63fa37f0 412 pr_debug("Detected %d chips per floor.\n", i);
1da177e4
LT
413}
414
c0739d85 415static void doc2001_write_byte(struct nand_chip *this, u_char datum)
1da177e4 416{
d699ed25 417 struct doc_priv *doc = nand_get_controller_data(this);
e0c7d767 418 void __iomem *docptr = doc->virtadr;
1da177e4
LT
419
420 WriteDOC(datum, docptr, CDSNSlowIO);
421 WriteDOC(datum, docptr, Mil_CDSN_IO);
422 WriteDOC(datum, docptr, WritePipeTerm);
423}
424
c0739d85 425static void doc2001_writebuf(struct nand_chip *this, const u_char *buf, int len)
1da177e4 426{
d699ed25 427 struct doc_priv *doc = nand_get_controller_data(this);
e0c7d767 428 void __iomem *docptr = doc->virtadr;
1da177e4
LT
429 int i;
430
e0c7d767 431 for (i = 0; i < len; i++)
1da177e4
LT
432 WriteDOC_(buf[i], docptr, DoC_Mil_CDSN_IO + i);
433 /* Terminate write pipeline */
434 WriteDOC(0x00, docptr, WritePipeTerm);
435}
436
7e534323 437static void doc2001_readbuf(struct nand_chip *this, u_char *buf, int len)
1da177e4 438{
d699ed25 439 struct doc_priv *doc = nand_get_controller_data(this);
e0c7d767 440 void __iomem *docptr = doc->virtadr;
1da177e4
LT
441 int i;
442
443 /* Start read pipeline */
444 ReadDOC(docptr, ReadPipeInit);
445
e0c7d767 446 for (i = 0; i < len - 1; i++)
1da177e4
LT
447 buf[i] = ReadDOC(docptr, Mil_CDSN_IO + (i & 0xff));
448
449 /* Terminate read pipeline */
450 buf[i] = ReadDOC(docptr, LastDataRead);
451}
452
c0739d85 453static void doc2001plus_writebuf(struct nand_chip *this, const u_char *buf, int len)
1da177e4 454{
d699ed25 455 struct doc_priv *doc = nand_get_controller_data(this);
e0c7d767 456 void __iomem *docptr = doc->virtadr;
1da177e4
LT
457 int i;
458
e0c7d767
DW
459 if (debug)
460 printk("writebuf of %d bytes: ", len);
461 for (i = 0; i < len; i++) {
1da177e4
LT
462 WriteDOC_(buf[i], docptr, DoC_Mil_CDSN_IO + i);
463 if (debug && i < 16)
464 printk("%02x ", buf[i]);
465 }
e0c7d767
DW
466 if (debug)
467 printk("\n");
1da177e4
LT
468}
469
7e534323 470static void doc2001plus_readbuf(struct nand_chip *this, u_char *buf, int len)
1da177e4 471{
d699ed25 472 struct doc_priv *doc = nand_get_controller_data(this);
e0c7d767 473 void __iomem *docptr = doc->virtadr;
1da177e4
LT
474 int i;
475
e0c7d767
DW
476 if (debug)
477 printk("readbuf of %d bytes: ", len);
1da177e4
LT
478
479 /* Start read pipeline */
480 ReadDOC(docptr, Mplus_ReadPipeInit);
481 ReadDOC(docptr, Mplus_ReadPipeInit);
482
e0c7d767 483 for (i = 0; i < len - 2; i++) {
1da177e4
LT
484 buf[i] = ReadDOC(docptr, Mil_CDSN_IO);
485 if (debug && i < 16)
486 printk("%02x ", buf[i]);
487 }
488
489 /* Terminate read pipeline */
a50b0c20
BB
490 if (len >= 2) {
491 buf[len - 2] = ReadDOC(docptr, Mplus_LastDataRead);
492 if (debug && i < 16)
493 printk("%02x ", buf[len - 2]);
494 }
495
e0c7d767 496 buf[len - 1] = ReadDOC(docptr, Mplus_LastDataRead);
1da177e4 497 if (debug && i < 16)
e0c7d767
DW
498 printk("%02x ", buf[len - 1]);
499 if (debug)
500 printk("\n");
1da177e4
LT
501}
502
f46eb7af
BB
503static void doc200x_write_control(struct doc_priv *doc, u8 value)
504{
505 WriteDOC(value, doc->virtadr, CDSNControl);
506 /* 11.4.3 -- 4 NOPs after CSDNControl write */
507 DoC_Delay(doc, 4);
508}
509
510static void doc200x_exec_instr(struct nand_chip *this,
511 const struct nand_op_instr *instr)
512{
513 struct doc_priv *doc = nand_get_controller_data(this);
514 unsigned int i;
515
516 switch (instr->type) {
517 case NAND_OP_CMD_INSTR:
518 doc200x_write_control(doc, CDSN_CTRL_CE | CDSN_CTRL_CLE);
519 doc2000_write_byte(this, instr->ctx.cmd.opcode);
520 break;
521
522 case NAND_OP_ADDR_INSTR:
523 doc200x_write_control(doc, CDSN_CTRL_CE | CDSN_CTRL_ALE);
524 for (i = 0; i < instr->ctx.addr.naddrs; i++) {
525 u8 addr = instr->ctx.addr.addrs[i];
526
527 if (DoC_is_2000(doc))
528 doc2000_write_byte(this, addr);
529 else
530 doc2001_write_byte(this, addr);
531 }
532 break;
533
534 case NAND_OP_DATA_IN_INSTR:
535 doc200x_write_control(doc, CDSN_CTRL_CE);
536 if (DoC_is_2000(doc))
537 doc2000_readbuf(this, instr->ctx.data.buf.in,
538 instr->ctx.data.len);
539 else
540 doc2001_readbuf(this, instr->ctx.data.buf.in,
541 instr->ctx.data.len);
542 break;
543
544 case NAND_OP_DATA_OUT_INSTR:
545 doc200x_write_control(doc, CDSN_CTRL_CE);
546 if (DoC_is_2000(doc))
547 doc2000_writebuf(this, instr->ctx.data.buf.out,
548 instr->ctx.data.len);
549 else
550 doc2001_writebuf(this, instr->ctx.data.buf.out,
551 instr->ctx.data.len);
552 break;
553
554 case NAND_OP_WAITRDY_INSTR:
555 DoC_WaitReady(doc);
556 break;
557 }
558
559 if (instr->delay_ns)
560 ndelay(instr->delay_ns);
561}
562
563static int doc200x_exec_op(struct nand_chip *this,
564 const struct nand_operation *op,
565 bool check_only)
566{
567 struct doc_priv *doc = nand_get_controller_data(this);
568 unsigned int i;
569
570 if (check_only)
571 return true;
572
573 doc->curchip = op->cs % doc->chips_per_floor;
574 doc->curfloor = op->cs / doc->chips_per_floor;
575
576 WriteDOC(doc->curfloor, doc->virtadr, FloorSelect);
577 WriteDOC(doc->curchip, doc->virtadr, CDSNDeviceSelect);
578
579 /* Assert CE pin */
580 doc200x_write_control(doc, CDSN_CTRL_CE);
581
582 for (i = 0; i < op->ninstrs; i++)
583 doc200x_exec_instr(this, &op->instrs[i]);
584
585 /* De-assert CE pin */
586 doc200x_write_control(doc, 0);
587
588 return 0;
589}
590
591static void doc2001plus_write_pipe_term(struct doc_priv *doc)
592{
593 WriteDOC(0x00, doc->virtadr, Mplus_WritePipeTerm);
594 WriteDOC(0x00, doc->virtadr, Mplus_WritePipeTerm);
595}
596
597static void doc2001plus_exec_instr(struct nand_chip *this,
598 const struct nand_op_instr *instr)
599{
600 struct doc_priv *doc = nand_get_controller_data(this);
601 unsigned int i;
602
603 switch (instr->type) {
604 case NAND_OP_CMD_INSTR:
605 WriteDOC(instr->ctx.cmd.opcode, doc->virtadr, Mplus_FlashCmd);
606 doc2001plus_write_pipe_term(doc);
607 break;
608
609 case NAND_OP_ADDR_INSTR:
610 for (i = 0; i < instr->ctx.addr.naddrs; i++) {
611 u8 addr = instr->ctx.addr.addrs[i];
612
613 WriteDOC(addr, doc->virtadr, Mplus_FlashAddress);
614 }
615 doc2001plus_write_pipe_term(doc);
616 /* deassert ALE */
617 WriteDOC(0, doc->virtadr, Mplus_FlashControl);
618 break;
619
620 case NAND_OP_DATA_IN_INSTR:
621 doc2001plus_readbuf(this, instr->ctx.data.buf.in,
622 instr->ctx.data.len);
623 break;
624 case NAND_OP_DATA_OUT_INSTR:
625 doc2001plus_writebuf(this, instr->ctx.data.buf.out,
626 instr->ctx.data.len);
627 doc2001plus_write_pipe_term(doc);
628 break;
629 case NAND_OP_WAITRDY_INSTR:
630 DoC_WaitReady(doc);
631 break;
632 }
633
634 if (instr->delay_ns)
635 ndelay(instr->delay_ns);
636}
637
638static int doc2001plus_exec_op(struct nand_chip *this,
639 const struct nand_operation *op,
640 bool check_only)
641{
642 struct doc_priv *doc = nand_get_controller_data(this);
643 unsigned int i;
644
645 if (check_only)
646 return true;
647
648 doc->curchip = op->cs % doc->chips_per_floor;
649 doc->curfloor = op->cs / doc->chips_per_floor;
650
651 /* Assert ChipEnable and deassert WriteProtect */
652 WriteDOC(DOC_FLASH_CE, doc->virtadr, Mplus_FlashSelect);
653
654 for (i = 0; i < op->ninstrs; i++)
655 doc2001plus_exec_instr(this, &op->instrs[i]);
656
657 /* De-assert ChipEnable */
658 WriteDOC(0, doc->virtadr, Mplus_FlashSelect);
659
660 return 0;
661}
662
ec47636c 663static void doc200x_enable_hwecc(struct nand_chip *this, int mode)
1da177e4 664{
d699ed25 665 struct doc_priv *doc = nand_get_controller_data(this);
e0c7d767 666 void __iomem *docptr = doc->virtadr;
1da177e4
LT
667
668 /* Prime the ECC engine */
e0c7d767 669 switch (mode) {
1da177e4
LT
670 case NAND_ECC_READ:
671 WriteDOC(DOC_ECC_RESET, docptr, ECCConf);
672 WriteDOC(DOC_ECC_EN, docptr, ECCConf);
673 break;
674 case NAND_ECC_WRITE:
675 WriteDOC(DOC_ECC_RESET, docptr, ECCConf);
676 WriteDOC(DOC_ECC_EN | DOC_ECC_RW, docptr, ECCConf);
677 break;
678 }
679}
680
ec47636c 681static void doc2001plus_enable_hwecc(struct nand_chip *this, int mode)
1da177e4 682{
d699ed25 683 struct doc_priv *doc = nand_get_controller_data(this);
e0c7d767 684 void __iomem *docptr = doc->virtadr;
1da177e4
LT
685
686 /* Prime the ECC engine */
e0c7d767 687 switch (mode) {
1da177e4
LT
688 case NAND_ECC_READ:
689 WriteDOC(DOC_ECC_RESET, docptr, Mplus_ECCConf);
690 WriteDOC(DOC_ECC_EN, docptr, Mplus_ECCConf);
691 break;
692 case NAND_ECC_WRITE:
693 WriteDOC(DOC_ECC_RESET, docptr, Mplus_ECCConf);
694 WriteDOC(DOC_ECC_EN | DOC_ECC_RW, docptr, Mplus_ECCConf);
695 break;
696 }
697}
698
699/* This code is only called on write */
af37d2c3
BB
700static int doc200x_calculate_ecc(struct nand_chip *this, const u_char *dat,
701 unsigned char *ecc_code)
1da177e4 702{
d699ed25 703 struct doc_priv *doc = nand_get_controller_data(this);
e0c7d767 704 void __iomem *docptr = doc->virtadr;
1da177e4 705 int i;
63c34f21 706 int __always_unused emptymatch = 1;
1da177e4
LT
707
708 /* flush the pipeline */
709 if (DoC_is_2000(doc)) {
710 WriteDOC(doc->CDSNControl & ~CDSN_CTRL_FLASH_IO, docptr, CDSNControl);
711 WriteDOC(0, docptr, 2k_CDSN_IO);
712 WriteDOC(0, docptr, 2k_CDSN_IO);
713 WriteDOC(0, docptr, 2k_CDSN_IO);
714 WriteDOC(doc->CDSNControl, docptr, CDSNControl);
715 } else if (DoC_is_MillenniumPlus(doc)) {
716 WriteDOC(0, docptr, Mplus_NOP);
717 WriteDOC(0, docptr, Mplus_NOP);
718 WriteDOC(0, docptr, Mplus_NOP);
719 } else {
720 WriteDOC(0, docptr, NOP);
721 WriteDOC(0, docptr, NOP);
722 WriteDOC(0, docptr, NOP);
723 }
724
725 for (i = 0; i < 6; i++) {
726 if (DoC_is_MillenniumPlus(doc))
727 ecc_code[i] = ReadDOC_(docptr, DoC_Mplus_ECCSyndrome0 + i);
61b03bd7 728 else
1da177e4
LT
729 ecc_code[i] = ReadDOC_(docptr, DoC_ECCSyndrome0 + i);
730 if (ecc_code[i] != empty_write_ecc[i])
731 emptymatch = 0;
732 }
733 if (DoC_is_MillenniumPlus(doc))
734 WriteDOC(DOC_ECC_DIS, docptr, Mplus_ECCConf);
735 else
736 WriteDOC(DOC_ECC_DIS, docptr, ECCConf);
737#if 0
738 /* If emptymatch=1, we might have an all-0xff data buffer. Check. */
739 if (emptymatch) {
740 /* Note: this somewhat expensive test should not be triggered
741 often. It could be optimized away by examining the data in
742 the writebuf routine, and remembering the result. */
743 for (i = 0; i < 512; i++) {
e0c7d767
DW
744 if (dat[i] == 0xff)
745 continue;
1da177e4
LT
746 emptymatch = 0;
747 break;
748 }
749 }
750 /* If emptymatch still =1, we do have an all-0xff data buffer.
751 Return all-0xff ecc value instead of the computed one, so
752 it'll look just like a freshly-erased page. */
e0c7d767
DW
753 if (emptymatch)
754 memset(ecc_code, 0xff, 6);
1da177e4
LT
755#endif
756 return 0;
757}
758
00da2ea9 759static int doc200x_correct_data(struct nand_chip *this, u_char *dat,
f5bbdacc 760 u_char *read_ecc, u_char *isnull)
1da177e4
LT
761{
762 int i, ret = 0;
d699ed25 763 struct doc_priv *doc = nand_get_controller_data(this);
e0c7d767 764 void __iomem *docptr = doc->virtadr;
f5bbdacc 765 uint8_t calc_ecc[6];
1da177e4 766 volatile u_char dummy;
61b03bd7 767
1da177e4
LT
768 /* flush the pipeline */
769 if (DoC_is_2000(doc)) {
770 dummy = ReadDOC(docptr, 2k_ECCStatus);
771 dummy = ReadDOC(docptr, 2k_ECCStatus);
772 dummy = ReadDOC(docptr, 2k_ECCStatus);
773 } else if (DoC_is_MillenniumPlus(doc)) {
774 dummy = ReadDOC(docptr, Mplus_ECCConf);
775 dummy = ReadDOC(docptr, Mplus_ECCConf);
776 dummy = ReadDOC(docptr, Mplus_ECCConf);
777 } else {
778 dummy = ReadDOC(docptr, ECCConf);
779 dummy = ReadDOC(docptr, ECCConf);
780 dummy = ReadDOC(docptr, ECCConf);
781 }
61b03bd7 782
25985edc 783 /* Error occurred ? */
1da177e4
LT
784 if (dummy & 0x80) {
785 for (i = 0; i < 6; i++) {
786 if (DoC_is_MillenniumPlus(doc))
787 calc_ecc[i] = ReadDOC_(docptr, DoC_Mplus_ECCSyndrome0 + i);
788 else
789 calc_ecc[i] = ReadDOC_(docptr, DoC_ECCSyndrome0 + i);
1da177e4 790 }
cc01e607 791
964dfce9 792 ret = doc_ecc_decode(doc->rs_decoder, dat, calc_ecc);
1da177e4 793 if (ret > 0)
63fa37f0
SP
794 pr_err("doc200x_correct_data corrected %d errors\n",
795 ret);
61b03bd7 796 }
1da177e4
LT
797 if (DoC_is_MillenniumPlus(doc))
798 WriteDOC(DOC_ECC_DIS, docptr, Mplus_ECCConf);
799 else
800 WriteDOC(DOC_ECC_DIS, docptr, ECCConf);
d57f4054 801 if (no_ecc_failures && mtd_is_eccerr(ret)) {
63fa37f0 802 pr_err("suppressing ECC failure\n");
1da177e4
LT
803 ret = 0;
804 }
805 return ret;
806}
61b03bd7 807
1da177e4
LT
808//u_char mydatabuf[528];
809
68c1b754
BB
810static int doc200x_ooblayout_ecc(struct mtd_info *mtd, int section,
811 struct mtd_oob_region *oobregion)
812{
813 if (section)
814 return -ERANGE;
815
816 oobregion->offset = 0;
817 oobregion->length = 6;
818
819 return 0;
820}
821
822static int doc200x_ooblayout_free(struct mtd_info *mtd, int section,
823 struct mtd_oob_region *oobregion)
824{
825 if (section > 1)
826 return -ERANGE;
827
828 /*
829 * The strange out-of-order free bytes definition is a (possibly
830 * unneeded) attempt to retain compatibility. It used to read:
831 * .oobfree = { {8, 8} }
832 * Since that leaves two bytes unusable, it was changed. But the
833 * following scheme might affect existing jffs2 installs by moving the
834 * cleanmarker:
835 * .oobfree = { {6, 10} }
836 * jffs2 seems to handle the above gracefully, but the current scheme
837 * seems safer. The only problem with it is that any code retrieving
838 * free bytes position must be able to handle out-of-order segments.
839 */
840 if (!section) {
841 oobregion->offset = 8;
842 oobregion->length = 8;
843 } else {
844 oobregion->offset = 6;
845 oobregion->length = 2;
846 }
847
848 return 0;
849}
850
851static const struct mtd_ooblayout_ops doc200x_ooblayout_ops = {
852 .ecc = doc200x_ooblayout_ecc,
853 .free = doc200x_ooblayout_free,
1da177e4 854};
61b03bd7 855
1da177e4 856/* Find the (I)NFTL Media Header, and optionally also the mirror media header.
af901ca1 857 On successful return, buf will contain a copy of the media header for
1da177e4
LT
858 further processing. id is the string to scan for, and will presumably be
859 either "ANAND" or "BNAND". If findmirror=1, also look for the mirror media
860 header. The page #s of the found media headers are placed in mh0_page and
861 mh1_page in the DOC private structure. */
e0c7d767 862static int __init find_media_headers(struct mtd_info *mtd, u_char *buf, const char *id, int findmirror)
1da177e4 863{
4bd4ebcc 864 struct nand_chip *this = mtd_to_nand(mtd);
d699ed25 865 struct doc_priv *doc = nand_get_controller_data(this);
1a78ff6b 866 unsigned offs;
1da177e4
LT
867 int ret;
868 size_t retlen;
869
1a78ff6b 870 for (offs = 0; offs < mtd->size; offs += mtd->erasesize) {
329ad399 871 ret = mtd_read(mtd, offs, mtd->writesize, &retlen, buf);
28318776 872 if (retlen != mtd->writesize)
e0c7d767 873 continue;
1da177e4 874 if (ret) {
63fa37f0 875 pr_warn("ECC error scanning DOC at 0x%x\n", offs);
1da177e4 876 }
e0c7d767
DW
877 if (memcmp(buf, id, 6))
878 continue;
63fa37f0 879 pr_info("Found DiskOnChip %s Media Header at 0x%x\n", id, offs);
1da177e4
LT
880 if (doc->mh0_page == -1) {
881 doc->mh0_page = offs >> this->page_shift;
e0c7d767
DW
882 if (!findmirror)
883 return 1;
1da177e4
LT
884 continue;
885 }
886 doc->mh1_page = offs >> this->page_shift;
887 return 2;
888 }
889 if (doc->mh0_page == -1) {
63fa37f0 890 pr_warn("DiskOnChip %s Media Header not found.\n", id);
1da177e4
LT
891 return 0;
892 }
893 /* Only one mediaheader was found. We want buf to contain a
894 mediaheader on return, so we'll have to re-read the one we found. */
895 offs = doc->mh0_page << this->page_shift;
329ad399 896 ret = mtd_read(mtd, offs, mtd->writesize, &retlen, buf);
28318776 897 if (retlen != mtd->writesize) {
1da177e4 898 /* Insanity. Give up. */
63fa37f0 899 pr_err("Read DiskOnChip Media Header once, but can't reread it???\n");
1da177e4
LT
900 return 0;
901 }
902 return 1;
903}
904
e0c7d767 905static inline int __init nftl_partscan(struct mtd_info *mtd, struct mtd_partition *parts)
1da177e4 906{
4bd4ebcc 907 struct nand_chip *this = mtd_to_nand(mtd);
d699ed25 908 struct doc_priv *doc = nand_get_controller_data(this);
629a442c 909 struct nand_memory_organization *memorg;
1da177e4
LT
910 int ret = 0;
911 u_char *buf;
912 struct NFTLMediaHeader *mh;
913 const unsigned psize = 1 << this->page_shift;
1a78ff6b 914 int numparts = 0;
1da177e4
LT
915 unsigned blocks, maxblocks;
916 int offs, numheaders;
917
629a442c
BB
918 memorg = nanddev_get_memorg(&this->base);
919
28318776 920 buf = kmalloc(mtd->writesize, GFP_KERNEL);
1da177e4 921 if (!buf) {
1da177e4
LT
922 return 0;
923 }
e0c7d767
DW
924 if (!(numheaders = find_media_headers(mtd, buf, "ANAND", 1)))
925 goto out;
926 mh = (struct NFTLMediaHeader *)buf;
1da177e4 927
96372446
HH
928 le16_to_cpus(&mh->NumEraseUnits);
929 le16_to_cpus(&mh->FirstPhysicalEUN);
930 le32_to_cpus(&mh->FormattedSize);
f29a4b86 931
63fa37f0
SP
932 pr_info(" DataOrgID = %s\n"
933 " NumEraseUnits = %d\n"
934 " FirstPhysicalEUN = %d\n"
935 " FormattedSize = %d\n"
936 " UnitSizeFactor = %d\n",
1da177e4
LT
937 mh->DataOrgID, mh->NumEraseUnits,
938 mh->FirstPhysicalEUN, mh->FormattedSize,
939 mh->UnitSizeFactor);
1da177e4
LT
940
941 blocks = mtd->size >> this->phys_erase_shift;
942 maxblocks = min(32768U, mtd->erasesize - psize);
943
944 if (mh->UnitSizeFactor == 0x00) {
945 /* Auto-determine UnitSizeFactor. The constraints are:
946 - There can be at most 32768 virtual blocks.
947 - There can be at most (virtual block size - page size)
e0c7d767
DW
948 virtual blocks (because MediaHeader+BBT must fit in 1).
949 */
1da177e4
LT
950 mh->UnitSizeFactor = 0xff;
951 while (blocks > maxblocks) {
952 blocks >>= 1;
953 maxblocks = min(32768U, (maxblocks << 1) + psize);
954 mh->UnitSizeFactor--;
955 }
63fa37f0 956 pr_warn("UnitSizeFactor=0x00 detected. Correct value is assumed to be 0x%02x.\n", mh->UnitSizeFactor);
1da177e4
LT
957 }
958
959 /* NOTE: The lines below modify internal variables of the NAND and MTD
960 layers; variables with have already been configured by nand_scan.
961 Unfortunately, we didn't know before this point what these values
25985edc 962 should be. Thus, this code is somewhat dependent on the exact
1da177e4
LT
963 implementation of the NAND layer. */
964 if (mh->UnitSizeFactor != 0xff) {
965 this->bbt_erase_shift += (0xff - mh->UnitSizeFactor);
629a442c 966 memorg->pages_per_eraseblock <<= (0xff - mh->UnitSizeFactor);
1da177e4 967 mtd->erasesize <<= (0xff - mh->UnitSizeFactor);
63fa37f0 968 pr_info("Setting virtual erase size to %d\n", mtd->erasesize);
1da177e4
LT
969 blocks = mtd->size >> this->bbt_erase_shift;
970 maxblocks = min(32768U, mtd->erasesize - psize);
971 }
972
973 if (blocks > maxblocks) {
63fa37f0 974 pr_err("UnitSizeFactor of 0x%02x is inconsistent with device size. Aborting.\n", mh->UnitSizeFactor);
1da177e4
LT
975 goto out;
976 }
977
978 /* Skip past the media headers. */
979 offs = max(doc->mh0_page, doc->mh1_page);
980 offs <<= this->page_shift;
981 offs += mtd->erasesize;
982
1a78ff6b
DB
983 if (show_firmware_partition == 1) {
984 parts[0].name = " DiskOnChip Firmware / Media Header partition";
985 parts[0].offset = 0;
986 parts[0].size = offs;
987 numparts = 1;
988 }
989
990 parts[numparts].name = " DiskOnChip BDTL partition";
991 parts[numparts].offset = offs;
992 parts[numparts].size = (mh->NumEraseUnits - numheaders) << this->bbt_erase_shift;
993
994 offs += parts[numparts].size;
995 numparts++;
1da177e4 996
1da177e4 997 if (offs < mtd->size) {
1a78ff6b
DB
998 parts[numparts].name = " DiskOnChip Remainder partition";
999 parts[numparts].offset = offs;
1000 parts[numparts].size = mtd->size - offs;
1001 numparts++;
1da177e4 1002 }
1a78ff6b
DB
1003
1004 ret = numparts;
e0c7d767 1005 out:
1da177e4
LT
1006 kfree(buf);
1007 return ret;
1008}
1009
1010/* This is a stripped-down copy of the code in inftlmount.c */
e0c7d767 1011static inline int __init inftl_partscan(struct mtd_info *mtd, struct mtd_partition *parts)
1da177e4 1012{
4bd4ebcc 1013 struct nand_chip *this = mtd_to_nand(mtd);
d699ed25 1014 struct doc_priv *doc = nand_get_controller_data(this);
1da177e4
LT
1015 int ret = 0;
1016 u_char *buf;
1017 struct INFTLMediaHeader *mh;
1018 struct INFTLPartition *ip;
1019 int numparts = 0;
1020 int blocks;
1021 int vshift, lastvunit = 0;
1022 int i;
1023 int end = mtd->size;
1024
1025 if (inftl_bbt_write)
1026 end -= (INFTL_BBT_RESERVED_BLOCKS << this->phys_erase_shift);
1027
28318776 1028 buf = kmalloc(mtd->writesize, GFP_KERNEL);
1da177e4 1029 if (!buf) {
1da177e4
LT
1030 return 0;
1031 }
1032
e0c7d767
DW
1033 if (!find_media_headers(mtd, buf, "BNAND", 0))
1034 goto out;
1da177e4 1035 doc->mh1_page = doc->mh0_page + (4096 >> this->page_shift);
e0c7d767 1036 mh = (struct INFTLMediaHeader *)buf;
1da177e4 1037
96372446
HH
1038 le32_to_cpus(&mh->NoOfBootImageBlocks);
1039 le32_to_cpus(&mh->NoOfBinaryPartitions);
1040 le32_to_cpus(&mh->NoOfBDTLPartitions);
1041 le32_to_cpus(&mh->BlockMultiplierBits);
1042 le32_to_cpus(&mh->FormatFlags);
1043 le32_to_cpus(&mh->PercentUsed);
61b03bd7 1044
63fa37f0
SP
1045 pr_info(" bootRecordID = %s\n"
1046 " NoOfBootImageBlocks = %d\n"
1047 " NoOfBinaryPartitions = %d\n"
1048 " NoOfBDTLPartitions = %d\n"
13a96466 1049 " BlockMultiplierBits = %d\n"
63fa37f0
SP
1050 " FormatFlgs = %d\n"
1051 " OsakVersion = %d.%d.%d.%d\n"
1052 " PercentUsed = %d\n",
1da177e4
LT
1053 mh->bootRecordID, mh->NoOfBootImageBlocks,
1054 mh->NoOfBinaryPartitions,
1055 mh->NoOfBDTLPartitions,
1056 mh->BlockMultiplierBits, mh->FormatFlags,
1057 ((unsigned char *) &mh->OsakVersion)[0] & 0xf,
1058 ((unsigned char *) &mh->OsakVersion)[1] & 0xf,
1059 ((unsigned char *) &mh->OsakVersion)[2] & 0xf,
1060 ((unsigned char *) &mh->OsakVersion)[3] & 0xf,
1061 mh->PercentUsed);
1da177e4
LT
1062
1063 vshift = this->phys_erase_shift + mh->BlockMultiplierBits;
1064
1065 blocks = mtd->size >> vshift;
1066 if (blocks > 32768) {
63fa37f0 1067 pr_err("BlockMultiplierBits=%d is inconsistent with device size. Aborting.\n", mh->BlockMultiplierBits);
1da177e4
LT
1068 goto out;
1069 }
1070
1071 blocks = doc->chips_per_floor << (this->chip_shift - this->phys_erase_shift);
1072 if (inftl_bbt_write && (blocks > mtd->erasesize)) {
63fa37f0 1073 pr_err("Writeable BBTs spanning more than one erase block are not yet supported. FIX ME!\n");
1da177e4
LT
1074 goto out;
1075 }
1076
1077 /* Scan the partitions */
1078 for (i = 0; (i < 4); i++) {
1079 ip = &(mh->Partitions[i]);
96372446
HH
1080 le32_to_cpus(&ip->virtualUnits);
1081 le32_to_cpus(&ip->firstUnit);
1082 le32_to_cpus(&ip->lastUnit);
1083 le32_to_cpus(&ip->flags);
1084 le32_to_cpus(&ip->spareUnits);
1085 le32_to_cpus(&ip->Reserved0);
1da177e4 1086
63fa37f0 1087 pr_info(" PARTITION[%d] ->\n"
1da177e4
LT
1088 " virtualUnits = %d\n"
1089 " firstUnit = %d\n"
1090 " lastUnit = %d\n"
1091 " flags = 0x%x\n"
1092 " spareUnits = %d\n",
1093 i, ip->virtualUnits, ip->firstUnit,
1094 ip->lastUnit, ip->flags,
1095 ip->spareUnits);
1da177e4 1096
1a78ff6b
DB
1097 if ((show_firmware_partition == 1) &&
1098 (i == 0) && (ip->firstUnit > 0)) {
1da177e4
LT
1099 parts[0].name = " DiskOnChip IPL / Media Header partition";
1100 parts[0].offset = 0;
1101 parts[0].size = mtd->erasesize * ip->firstUnit;
1102 numparts = 1;
1103 }
1da177e4
LT
1104
1105 if (ip->flags & INFTL_BINARY)
1106 parts[numparts].name = " DiskOnChip BDK partition";
1107 else
1108 parts[numparts].name = " DiskOnChip BDTL partition";
1109 parts[numparts].offset = ip->firstUnit << vshift;
1110 parts[numparts].size = (1 + ip->lastUnit - ip->firstUnit) << vshift;
1111 numparts++;
e0c7d767
DW
1112 if (ip->lastUnit > lastvunit)
1113 lastvunit = ip->lastUnit;
1114 if (ip->flags & INFTL_LAST)
1115 break;
1da177e4
LT
1116 }
1117 lastvunit++;
1118 if ((lastvunit << vshift) < end) {
1119 parts[numparts].name = " DiskOnChip Remainder partition";
1120 parts[numparts].offset = lastvunit << vshift;
1121 parts[numparts].size = end - parts[numparts].offset;
1122 numparts++;
1123 }
1124 ret = numparts;
e0c7d767 1125 out:
1da177e4
LT
1126 kfree(buf);
1127 return ret;
1128}
1129
1130static int __init nftl_scan_bbt(struct mtd_info *mtd)
1131{
1132 int ret, numparts;
4bd4ebcc 1133 struct nand_chip *this = mtd_to_nand(mtd);
d699ed25 1134 struct doc_priv *doc = nand_get_controller_data(this);
1da177e4
LT
1135 struct mtd_partition parts[2];
1136
e0c7d767 1137 memset((char *)parts, 0, sizeof(parts));
1da177e4
LT
1138 /* On NFTL, we have to find the media headers before we can read the
1139 BBTs, since they're stored in the media header eraseblocks. */
1140 numparts = nftl_partscan(mtd, parts);
e0c7d767
DW
1141 if (!numparts)
1142 return -EIO;
1da177e4
LT
1143 this->bbt_td->options = NAND_BBT_ABSPAGE | NAND_BBT_8BIT |
1144 NAND_BBT_SAVECONTENT | NAND_BBT_WRITE |
1145 NAND_BBT_VERSION;
1146 this->bbt_td->veroffs = 7;
1147 this->bbt_td->pages[0] = doc->mh0_page + 1;
1148 if (doc->mh1_page != -1) {
1149 this->bbt_md->options = NAND_BBT_ABSPAGE | NAND_BBT_8BIT |
1150 NAND_BBT_SAVECONTENT | NAND_BBT_WRITE |
1151 NAND_BBT_VERSION;
1152 this->bbt_md->veroffs = 7;
1153 this->bbt_md->pages[0] = doc->mh1_page + 1;
1154 } else {
1155 this->bbt_md = NULL;
1156 }
1157
e80eba75 1158 ret = nand_create_bbt(this);
d24fe0c3 1159 if (ret)
1da177e4 1160 return ret;
d24fe0c3 1161
6a7c7334 1162 return mtd_device_register(mtd, parts, no_autopart ? 0 : numparts);
1da177e4
LT
1163}
1164
1165static int __init inftl_scan_bbt(struct mtd_info *mtd)
1166{
1167 int ret, numparts;
4bd4ebcc 1168 struct nand_chip *this = mtd_to_nand(mtd);
d699ed25 1169 struct doc_priv *doc = nand_get_controller_data(this);
1da177e4
LT
1170 struct mtd_partition parts[5];
1171
32813e28 1172 if (nanddev_ntargets(&this->base) > doc->chips_per_floor) {
63fa37f0 1173 pr_err("Multi-floor INFTL devices not yet supported.\n");
1da177e4
LT
1174 return -EIO;
1175 }
1176
1177 if (DoC_is_MillenniumPlus(doc)) {
1178 this->bbt_td->options = NAND_BBT_2BIT | NAND_BBT_ABSPAGE;
1179 if (inftl_bbt_write)
1180 this->bbt_td->options |= NAND_BBT_WRITE;
1181 this->bbt_td->pages[0] = 2;
1182 this->bbt_md = NULL;
1183 } else {
e0c7d767 1184 this->bbt_td->options = NAND_BBT_LASTBLOCK | NAND_BBT_8BIT | NAND_BBT_VERSION;
1da177e4
LT
1185 if (inftl_bbt_write)
1186 this->bbt_td->options |= NAND_BBT_WRITE;
1187 this->bbt_td->offs = 8;
1188 this->bbt_td->len = 8;
1189 this->bbt_td->veroffs = 7;
1190 this->bbt_td->maxblocks = INFTL_BBT_RESERVED_BLOCKS;
1191 this->bbt_td->reserved_block_code = 0x01;
1192 this->bbt_td->pattern = "MSYS_BBT";
1193
e0c7d767 1194 this->bbt_md->options = NAND_BBT_LASTBLOCK | NAND_BBT_8BIT | NAND_BBT_VERSION;
1da177e4
LT
1195 if (inftl_bbt_write)
1196 this->bbt_md->options |= NAND_BBT_WRITE;
1197 this->bbt_md->offs = 8;
1198 this->bbt_md->len = 8;
1199 this->bbt_md->veroffs = 7;
1200 this->bbt_md->maxblocks = INFTL_BBT_RESERVED_BLOCKS;
1201 this->bbt_md->reserved_block_code = 0x01;
1202 this->bbt_md->pattern = "TBB_SYSM";
1203 }
1204
e80eba75 1205 ret = nand_create_bbt(this);
d24fe0c3 1206 if (ret)
1da177e4 1207 return ret;
d24fe0c3 1208
e0c7d767 1209 memset((char *)parts, 0, sizeof(parts));
1da177e4
LT
1210 numparts = inftl_partscan(mtd, parts);
1211 /* At least for now, require the INFTL Media Header. We could probably
1212 do without it for non-INFTL use, since all it gives us is
1213 autopartitioning, but I want to give it more thought. */
e0c7d767
DW
1214 if (!numparts)
1215 return -EIO;
6a7c7334 1216 return mtd_device_register(mtd, parts, no_autopart ? 0 : numparts);
1da177e4
LT
1217}
1218
1219static inline int __init doc2000_init(struct mtd_info *mtd)
1220{
4bd4ebcc 1221 struct nand_chip *this = mtd_to_nand(mtd);
d699ed25 1222 struct doc_priv *doc = nand_get_controller_data(this);
1da177e4 1223
d24fe0c3 1224 doc->late_init = nftl_scan_bbt;
1da177e4
LT
1225
1226 doc->CDSNControl = CDSN_CTRL_FLASH_IO | CDSN_CTRL_ECC_IO;
1227 doc2000_count_chips(mtd);
1228 mtd->name = "DiskOnChip 2000 (NFTL Model)";
1229 return (4 * doc->chips_per_floor);
1230}
1231
1232static inline int __init doc2001_init(struct mtd_info *mtd)
1233{
4bd4ebcc 1234 struct nand_chip *this = mtd_to_nand(mtd);
d699ed25 1235 struct doc_priv *doc = nand_get_controller_data(this);
1da177e4 1236
1da177e4
LT
1237 ReadDOC(doc->virtadr, ChipID);
1238 ReadDOC(doc->virtadr, ChipID);
1239 ReadDOC(doc->virtadr, ChipID);
1240 if (ReadDOC(doc->virtadr, ChipID) != DOC_ChipID_DocMil) {
1241 /* It's not a Millennium; it's one of the newer
61b03bd7 1242 DiskOnChip 2000 units with a similar ASIC.
1da177e4
LT
1243 Treat it like a Millennium, except that it
1244 can have multiple chips. */
1245 doc2000_count_chips(mtd);
1246 mtd->name = "DiskOnChip 2000 (INFTL Model)";
d24fe0c3 1247 doc->late_init = inftl_scan_bbt;
1da177e4
LT
1248 return (4 * doc->chips_per_floor);
1249 } else {
1250 /* Bog-standard Millennium */
1251 doc->chips_per_floor = 1;
1252 mtd->name = "DiskOnChip Millennium";
d24fe0c3 1253 doc->late_init = nftl_scan_bbt;
1da177e4
LT
1254 return 1;
1255 }
1256}
1257
1258static inline int __init doc2001plus_init(struct mtd_info *mtd)
1259{
4bd4ebcc 1260 struct nand_chip *this = mtd_to_nand(mtd);
d699ed25 1261 struct doc_priv *doc = nand_get_controller_data(this);
1da177e4 1262
d24fe0c3 1263 doc->late_init = inftl_scan_bbt;
0cddd6c2 1264 this->ecc.hwctl = doc2001plus_enable_hwecc;
1da177e4
LT
1265
1266 doc->chips_per_floor = 1;
1267 mtd->name = "DiskOnChip Millennium Plus";
1268
1269 return 1;
1270}
1271
7f4ea034
MR
1272static int doc200x_attach_chip(struct nand_chip *chip)
1273{
1274 if (chip->ecc.engine_type != NAND_ECC_ENGINE_TYPE_ON_HOST)
1275 return 0;
1276
1277 chip->ecc.placement = NAND_ECC_PLACEMENT_INTERLEAVED;
1278 chip->ecc.size = 512;
1279 chip->ecc.bytes = 6;
1280 chip->ecc.strength = 2;
1281 chip->ecc.options = NAND_ECC_GENERIC_ERASED_CHECK;
1282 chip->ecc.hwctl = doc200x_enable_hwecc;
1283 chip->ecc.calculate = doc200x_calculate_ecc;
1284 chip->ecc.correct = doc200x_correct_data;
1285
1286 return 0;
1287}
1288
f46eb7af
BB
1289static const struct nand_controller_ops doc200x_ops = {
1290 .exec_op = doc200x_exec_op,
7f4ea034 1291 .attach_chip = doc200x_attach_chip,
f46eb7af
BB
1292};
1293
1294static const struct nand_controller_ops doc2001plus_ops = {
1295 .exec_op = doc2001plus_exec_op,
7f4ea034 1296 .attach_chip = doc200x_attach_chip,
f46eb7af
BB
1297};
1298
858119e1 1299static int __init doc_probe(unsigned long physadr)
1da177e4 1300{
964dfce9
TG
1301 struct nand_chip *nand = NULL;
1302 struct doc_priv *doc = NULL;
1da177e4
LT
1303 unsigned char ChipID;
1304 struct mtd_info *mtd;
1da177e4
LT
1305 void __iomem *virtadr;
1306 unsigned char save_control;
1307 unsigned char tmp, tmpb, tmpc;
1308 int reg, len, numchips;
1309 int ret = 0;
1310
86e4bbc7 1311 if (!request_mem_region(physadr, DOC_IOREMAP_LEN, "DiskOnChip"))
4f0614a0 1312 return -EBUSY;
1da177e4
LT
1313 virtadr = ioremap(physadr, DOC_IOREMAP_LEN);
1314 if (!virtadr) {
63fa37f0
SP
1315 pr_err("Diskonchip ioremap failed: 0x%x bytes at 0x%lx\n",
1316 DOC_IOREMAP_LEN, physadr);
4f0614a0
AS
1317 ret = -EIO;
1318 goto error_ioremap;
1da177e4
LT
1319 }
1320
1321 /* It's not possible to cleanly detect the DiskOnChip - the
1322 * bootup procedure will put the device into reset mode, and
1323 * it's not possible to talk to it without actually writing
1324 * to the DOCControl register. So we store the current contents
1325 * of the DOCControl register's location, in case we later decide
1326 * that it's not a DiskOnChip, and want to put it back how we
61b03bd7 1327 * found it.
1da177e4
LT
1328 */
1329 save_control = ReadDOC(virtadr, DOCControl);
1330
1331 /* Reset the DiskOnChip ASIC */
e0c7d767
DW
1332 WriteDOC(DOC_MODE_CLR_ERR | DOC_MODE_MDWREN | DOC_MODE_RESET, virtadr, DOCControl);
1333 WriteDOC(DOC_MODE_CLR_ERR | DOC_MODE_MDWREN | DOC_MODE_RESET, virtadr, DOCControl);
1da177e4
LT
1334
1335 /* Enable the DiskOnChip ASIC */
e0c7d767
DW
1336 WriteDOC(DOC_MODE_CLR_ERR | DOC_MODE_MDWREN | DOC_MODE_NORMAL, virtadr, DOCControl);
1337 WriteDOC(DOC_MODE_CLR_ERR | DOC_MODE_MDWREN | DOC_MODE_NORMAL, virtadr, DOCControl);
1da177e4
LT
1338
1339 ChipID = ReadDOC(virtadr, ChipID);
1340
e0c7d767 1341 switch (ChipID) {
1da177e4
LT
1342 case DOC_ChipID_Doc2k:
1343 reg = DoC_2k_ECCStatus;
1344 break;
1345 case DOC_ChipID_DocMil:
1346 reg = DoC_ECCConf;
1347 break;
1348 case DOC_ChipID_DocMilPlus16:
1349 case DOC_ChipID_DocMilPlus32:
1350 case 0:
1351 /* Possible Millennium Plus, need to do more checks */
1352 /* Possibly release from power down mode */
1353 for (tmp = 0; (tmp < 4); tmp++)
1354 ReadDOC(virtadr, Mplus_Power);
1355
1356 /* Reset the Millennium Plus ASIC */
e0c7d767 1357 tmp = DOC_MODE_RESET | DOC_MODE_MDWREN | DOC_MODE_RST_LAT | DOC_MODE_BDECT;
1da177e4
LT
1358 WriteDOC(tmp, virtadr, Mplus_DOCControl);
1359 WriteDOC(~tmp, virtadr, Mplus_CtrlConfirm);
1360
7b4b1994 1361 usleep_range(1000, 2000);
1da177e4 1362 /* Enable the Millennium Plus ASIC */
e0c7d767 1363 tmp = DOC_MODE_NORMAL | DOC_MODE_MDWREN | DOC_MODE_RST_LAT | DOC_MODE_BDECT;
1da177e4
LT
1364 WriteDOC(tmp, virtadr, Mplus_DOCControl);
1365 WriteDOC(~tmp, virtadr, Mplus_CtrlConfirm);
7b4b1994 1366 usleep_range(1000, 2000);
1da177e4
LT
1367
1368 ChipID = ReadDOC(virtadr, ChipID);
1369
1370 switch (ChipID) {
1371 case DOC_ChipID_DocMilPlus16:
1372 reg = DoC_Mplus_Toggle;
1373 break;
1374 case DOC_ChipID_DocMilPlus32:
63fa37f0 1375 pr_err("DiskOnChip Millennium Plus 32MB is not supported, ignoring.\n");
025a06c1 1376 fallthrough;
1da177e4
LT
1377 default:
1378 ret = -ENODEV;
1379 goto notfound;
1380 }
1381 break;
1382
1383 default:
1384 ret = -ENODEV;
1385 goto notfound;
1386 }
1387 /* Check the TOGGLE bit in the ECC register */
e0c7d767 1388 tmp = ReadDOC_(virtadr, reg) & DOC_TOGGLE_BIT;
1da177e4
LT
1389 tmpb = ReadDOC_(virtadr, reg) & DOC_TOGGLE_BIT;
1390 tmpc = ReadDOC_(virtadr, reg) & DOC_TOGGLE_BIT;
1391 if ((tmp == tmpb) || (tmp != tmpc)) {
63fa37f0 1392 pr_warn("Possible DiskOnChip at 0x%lx failed TOGGLE test, dropping.\n", physadr);
1da177e4
LT
1393 ret = -ENODEV;
1394 goto notfound;
1395 }
1396
1397 for (mtd = doclist; mtd; mtd = doc->nextdoc) {
1398 unsigned char oldval;
1399 unsigned char newval;
4bd4ebcc 1400 nand = mtd_to_nand(mtd);
d699ed25 1401 doc = nand_get_controller_data(nand);
1da177e4
LT
1402 /* Use the alias resolution register to determine if this is
1403 in fact the same DOC aliased to a new address. If writes
1404 to one chip's alias resolution register change the value on
1405 the other chip, they're the same chip. */
1406 if (ChipID == DOC_ChipID_DocMilPlus16) {
1407 oldval = ReadDOC(doc->virtadr, Mplus_AliasResolution);
1408 newval = ReadDOC(virtadr, Mplus_AliasResolution);
1409 } else {
1410 oldval = ReadDOC(doc->virtadr, AliasResolution);
1411 newval = ReadDOC(virtadr, AliasResolution);
1412 }
1413 if (oldval != newval)
1414 continue;
1415 if (ChipID == DOC_ChipID_DocMilPlus16) {
1416 WriteDOC(~newval, virtadr, Mplus_AliasResolution);
1417 oldval = ReadDOC(doc->virtadr, Mplus_AliasResolution);
e0c7d767 1418 WriteDOC(newval, virtadr, Mplus_AliasResolution); // restore it
1da177e4
LT
1419 } else {
1420 WriteDOC(~newval, virtadr, AliasResolution);
1421 oldval = ReadDOC(doc->virtadr, AliasResolution);
e0c7d767 1422 WriteDOC(newval, virtadr, AliasResolution); // restore it
1da177e4
LT
1423 }
1424 newval = ~newval;
1425 if (oldval == newval) {
63fa37f0
SP
1426 pr_debug("Found alias of DOC at 0x%lx to 0x%lx\n",
1427 doc->physadr, physadr);
1da177e4
LT
1428 goto notfound;
1429 }
1430 }
1431
63fa37f0 1432 pr_notice("DiskOnChip found at 0x%lx\n", physadr);
1da177e4 1433
b0c423c7
BB
1434 len = sizeof(struct nand_chip) + sizeof(struct doc_priv) +
1435 (2 * sizeof(struct nand_bbt_descr));
1436 nand = kzalloc(len, GFP_KERNEL);
1437 if (!nand) {
1da177e4
LT
1438 ret = -ENOMEM;
1439 goto fail;
1440 }
1da177e4 1441
964dfce9
TG
1442 /*
1443 * Allocate a RS codec instance
1444 *
1445 * Symbolsize is 10 (bits)
1446 * Primitve polynomial is x^10+x^3+1
1447 * First consecutive root is 510
1448 * Primitve element to generate roots = 1
1449 * Generator polinomial degree = 4
1450 */
1451 doc = (struct doc_priv *) (nand + 1);
1452 doc->rs_decoder = init_rs(10, 0x409, FCR, 1, NROOTS);
1453 if (!doc->rs_decoder) {
1454 pr_err("DiskOnChip: Could not create a RS codec\n");
1455 ret = -ENOMEM;
1456 goto fail;
1457 }
1458
f37b1d3c 1459 nand_controller_init(&doc->base);
f46eb7af
BB
1460 if (ChipID == DOC_ChipID_DocMilPlus16)
1461 doc->base.ops = &doc2001plus_ops;
1462 else
1463 doc->base.ops = &doc200x_ops;
1464
b0c423c7 1465 mtd = nand_to_mtd(nand);
1da177e4
LT
1466 nand->bbt_td = (struct nand_bbt_descr *) (doc + 1);
1467 nand->bbt_md = nand->bbt_td + 1;
1468
1da177e4 1469 mtd->owner = THIS_MODULE;
68c1b754 1470 mtd_set_ooblayout(mtd, &doc200x_ooblayout_ops);
1da177e4 1471
f37b1d3c 1472 nand->controller = &doc->base;
d699ed25 1473 nand_set_controller_data(nand, doc);
bb9ebd4e 1474 nand->bbt_options = NAND_BBT_USE_FLASH;
d24fe0c3 1475 /* Skip the automatic BBT scan so we can run it manually */
dace12cc 1476 nand->options |= NAND_SKIP_BBTSCAN | NAND_NO_BBM_QUIRK;
1da177e4
LT
1477
1478 doc->physadr = physadr;
1479 doc->virtadr = virtadr;
1480 doc->ChipID = ChipID;
1481 doc->curfloor = -1;
1482 doc->curchip = -1;
1483 doc->mh0_page = -1;
1484 doc->mh1_page = -1;
1485 doc->nextdoc = doclist;
1486
1487 if (ChipID == DOC_ChipID_Doc2k)
1488 numchips = doc2000_init(mtd);
1489 else if (ChipID == DOC_ChipID_DocMilPlus16)
1490 numchips = doc2001plus_init(mtd);
1491 else
1492 numchips = doc2001_init(mtd);
1493
2b8aa4c3
DL
1494 ret = nand_scan(nand, numchips);
1495 if (ret)
1496 goto fail;
1497
1498 ret = doc->late_init(mtd);
1499 if (ret) {
c5be12e4 1500 nand_cleanup(nand);
1da177e4
LT
1501 goto fail;
1502 }
1503
1504 /* Success! */
1505 doclist = mtd;
1506 return 0;
1507
e0c7d767 1508 notfound:
1da177e4
LT
1509 /* Put back the contents of the DOCControl register, in case it's not
1510 actually a DiskOnChip. */
1511 WriteDOC(save_control, virtadr, DOCControl);
e0c7d767 1512 fail:
964dfce9
TG
1513 if (doc)
1514 free_rs(doc->rs_decoder);
1515 kfree(nand);
1da177e4 1516 iounmap(virtadr);
4f0614a0
AS
1517
1518error_ioremap:
1519 release_mem_region(physadr, DOC_IOREMAP_LEN);
1520
1da177e4
LT
1521 return ret;
1522}
1523
1524static void release_nanddoc(void)
1525{
e0c7d767 1526 struct mtd_info *mtd, *nextmtd;
1da177e4
LT
1527 struct nand_chip *nand;
1528 struct doc_priv *doc;
63a14607 1529 int ret;
1da177e4
LT
1530
1531 for (mtd = doclist; mtd; mtd = nextmtd) {
4bd4ebcc 1532 nand = mtd_to_nand(mtd);
d699ed25 1533 doc = nand_get_controller_data(nand);
1da177e4
LT
1534
1535 nextmtd = doc->nextdoc;
63a14607
MR
1536 ret = mtd_device_unregister(mtd);
1537 WARN_ON(ret);
1538 nand_cleanup(nand);
1da177e4 1539 iounmap(doc->virtadr);
4f0614a0 1540 release_mem_region(doc->physadr, DOC_IOREMAP_LEN);
964dfce9 1541 free_rs(doc->rs_decoder);
b0c423c7 1542 kfree(nand);
1da177e4
LT
1543 }
1544}
1545
1546static int __init init_nanddoc(void)
1547{
1548 int i, ret = 0;
1549
1da177e4 1550 if (doc_config_location) {
63fa37f0
SP
1551 pr_info("Using configured DiskOnChip probe address 0x%lx\n",
1552 doc_config_location);
1da177e4
LT
1553 ret = doc_probe(doc_config_location);
1554 if (ret < 0)
964dfce9 1555 return ret;
1da177e4 1556 } else {
21c9fb61 1557 for (i = 0; i < ARRAY_SIZE(doc_locations); i++) {
1da177e4
LT
1558 doc_probe(doc_locations[i]);
1559 }
1560 }
1561 /* No banner message any more. Print a message if no DiskOnChip
1562 found, so the user knows we at least tried. */
1563 if (!doclist) {
63fa37f0 1564 pr_info("No valid DiskOnChip devices found\n");
1da177e4 1565 ret = -ENODEV;
1da177e4 1566 }
1da177e4
LT
1567 return ret;
1568}
1569
1570static void __exit cleanup_nanddoc(void)
1571{
1572 /* Cleanup the nand/DoC resources */
1573 release_nanddoc();
1da177e4
LT
1574}
1575
1576module_init(init_nanddoc);
1577module_exit(cleanup_nanddoc);
1578
1579MODULE_LICENSE("GPL");
1580MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
2a7af8ca 1581MODULE_DESCRIPTION("M-Systems DiskOnChip 2000, Millennium and Millennium Plus device driver");