blob: 8d0fb2daad7124ec34bb9650d0f9a83be892fbcf [file] [log] [blame]
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * (C) 2003 Red Hat, Inc.
3 * (C) 2004 Dan Brown <dan_brown@ieee.org>
4 * (C) 2004 Kalev Lember <kalev@smartlink.ee>
5 *
6 * Author: David Woodhouse <dwmw2@infradead.org>
7 * Additional Diskonchip 2000 and Millennium support by Dan Brown <dan_brown@ieee.org>
8 * Diskonchip Millennium Plus support by Kalev Lember <kalev@smartlink.ee>
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00009 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 * Error correction code lifted from the old docecc code
Thomas Gleixner61b03bd2005-11-07 11:15:49 +000011 * Author: Fabrice Bellard (fabrice.bellard@netgem.com)
Linus Torvalds1da177e2005-04-16 15:20:36 -070012 * Copyright (C) 2000 Netgem S.A.
13 * converted to the generic Reed-Solomon library by Thomas Gleixner <tglx@linutronix.de>
Thomas Gleixner61b03bd2005-11-07 11:15:49 +000014 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070015 * Interface to generic NAND code for M-Systems DiskOnChip devices
Linus Torvalds1da177e2005-04-16 15:20:36 -070016 */
17
18#include <linux/kernel.h>
19#include <linux/init.h>
20#include <linux/sched.h>
21#include <linux/delay.h>
22#include <linux/rslib.h>
23#include <linux/moduleparam.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090024#include <linux/slab.h>
Dan Williams2584cf82015-08-10 23:07:05 -040025#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
27#include <linux/mtd/mtd.h>
Boris Brezillond4092d72017-08-04 17:29:10 +020028#include <linux/mtd/rawnand.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/mtd/doc2000.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/mtd/partitions.h>
31#include <linux/mtd/inftl.h>
Paul Gortmakera0e5cc52011-07-03 15:17:31 -040032#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
34/* Where to look for the devices? */
Thomas Gleixner651078b2005-01-31 20:36:46 +000035#ifndef CONFIG_MTD_NAND_DISKONCHIP_PROBE_ADDRESS
36#define CONFIG_MTD_NAND_DISKONCHIP_PROBE_ADDRESS 0
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#endif
38
Sachin Kamat14a95b8a2013-08-12 15:10:47 +053039static unsigned long doc_locations[] __initdata = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#if defined (__alpha__) || defined(__i386__) || defined(__x86_64__)
Thomas Gleixner651078b2005-01-31 20:36:46 +000041#ifdef CONFIG_MTD_NAND_DISKONCHIP_PROBE_HIGH
Thomas Gleixner61b03bd2005-11-07 11:15:49 +000042 0xfffc8000, 0xfffca000, 0xfffcc000, 0xfffce000,
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 0xfffd0000, 0xfffd2000, 0xfffd4000, 0xfffd6000,
Thomas Gleixner61b03bd2005-11-07 11:15:49 +000044 0xfffd8000, 0xfffda000, 0xfffdc000, 0xfffde000,
45 0xfffe0000, 0xfffe2000, 0xfffe4000, 0xfffe6000,
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 0xfffe8000, 0xfffea000, 0xfffec000, 0xfffee000,
Michael Opdenacker9d403492013-07-08 06:39:20 +020047#else
Thomas Gleixner61b03bd2005-11-07 11:15:49 +000048 0xc8000, 0xca000, 0xcc000, 0xce000,
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 0xd0000, 0xd2000, 0xd4000, 0xd6000,
Thomas Gleixner61b03bd2005-11-07 11:15:49 +000050 0xd8000, 0xda000, 0xdc000, 0xde000,
51 0xe0000, 0xe2000, 0xe4000, 0xe6000,
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 0xe8000, 0xea000, 0xec000, 0xee000,
Michael Opdenacker9d403492013-07-08 06:39:20 +020053#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070054#endif
55 0xffffffff };
56
57static struct mtd_info *doclist = NULL;
58
59struct doc_priv {
60 void __iomem *virtadr;
61 unsigned long physadr;
62 u_char ChipID;
63 u_char CDSNControl;
David Woodhousee0c7d762006-05-13 18:07:53 +010064 int chips_per_floor; /* The number of chips detected on each floor */
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 int curfloor;
66 int curchip;
67 int mh0_page;
68 int mh1_page;
69 struct mtd_info *nextdoc;
Brian Norrisd24fe0c2015-02-28 02:13:10 -080070
71 /* Handle the last stage of initialization (BBT scan, partitioning) */
72 int (*late_init)(struct mtd_info *mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073};
74
Linus Torvalds1da177e2005-04-16 15:20:36 -070075/* This is the ecc value computed by the HW ecc generator upon writing an empty
76 page, one with all 0xff for data. */
77static u_char empty_write_ecc[6] = { 0x4b, 0x00, 0xe2, 0x0e, 0x93, 0xf7 };
78
79#define INFTL_BBT_RESERVED_BLOCKS 4
80
81#define DoC_is_MillenniumPlus(doc) ((doc)->ChipID == DOC_ChipID_DocMilPlus16 || (doc)->ChipID == DOC_ChipID_DocMilPlus32)
82#define DoC_is_Millennium(doc) ((doc)->ChipID == DOC_ChipID_DocMil)
83#define DoC_is_2000(doc) ((doc)->ChipID == DOC_ChipID_Doc2k)
84
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +020085static void doc200x_hwcontrol(struct mtd_info *mtd, int cmd,
86 unsigned int bitmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087static void doc200x_select_chip(struct mtd_info *mtd, int chip);
88
David Woodhousee0c7d762006-05-13 18:07:53 +010089static int debug = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090module_param(debug, int, 0);
91
David Woodhousee0c7d762006-05-13 18:07:53 +010092static int try_dword = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093module_param(try_dword, int, 0);
94
David Woodhousee0c7d762006-05-13 18:07:53 +010095static int no_ecc_failures = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096module_param(no_ecc_failures, int, 0);
97
David Woodhousee0c7d762006-05-13 18:07:53 +010098static int no_autopart = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099module_param(no_autopart, int, 0);
Dan Brown1a78ff62005-03-29 21:57:48 +0100100
David Woodhousee0c7d762006-05-13 18:07:53 +0100101static int show_firmware_partition = 0;
Dan Brown1a78ff62005-03-29 21:57:48 +0100102module_param(show_firmware_partition, int, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
Robert P. J. Day89e2bf62007-03-07 18:33:25 -0500104#ifdef CONFIG_MTD_NAND_DISKONCHIP_BBTWRITE
David Woodhousee0c7d762006-05-13 18:07:53 +0100105static int inftl_bbt_write = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106#else
David Woodhousee0c7d762006-05-13 18:07:53 +0100107static int inftl_bbt_write = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108#endif
109module_param(inftl_bbt_write, int, 0);
110
Thomas Gleixner651078b2005-01-31 20:36:46 +0000111static unsigned long doc_config_location = CONFIG_MTD_NAND_DISKONCHIP_PROBE_ADDRESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112module_param(doc_config_location, ulong, 0);
113MODULE_PARM_DESC(doc_config_location, "Physical memory address at which to probe for DiskOnChip");
114
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115/* 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
126/* the Reed Solomon control structure */
127static struct rs_control *rs_decoder;
128
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000129/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 * The HW decoder in the DoC ASIC's provides us a error syndrome,
Brian Norris7854d3f2011-06-23 14:12:08 -0700131 * which we must convert to a standard syndrome usable by the generic
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 * Reed-Solomon library code.
133 *
134 * Fabrice Bellard figured this out in the old docecc code. I added
135 * some comments, improved a minor bit and converted it to make use
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300136 * of the generic Reed-Solomon library. tglx
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100138static int doc_ecc_decode(struct rs_control *rs, uint8_t *data, uint8_t *ecc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139{
140 int i, j, nerr, errpos[8];
141 uint8_t parity;
142 uint16_t ds[4], s[5], tmp, errval[8], syn[4];
143
Mark Warec9fb6772010-05-27 11:45:39 +1000144 memset(syn, 0, sizeof(syn));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 /* Convert the ecc bytes into words */
146 ds[0] = ((ecc[4] & 0xff) >> 0) | ((ecc[5] & 0x03) << 8);
147 ds[1] = ((ecc[5] & 0xfc) >> 2) | ((ecc[2] & 0x0f) << 6);
148 ds[2] = ((ecc[2] & 0xf0) >> 4) | ((ecc[3] & 0x3f) << 4);
149 ds[3] = ((ecc[3] & 0xc0) >> 6) | ((ecc[0] & 0xff) << 2);
150 parity = ecc[1];
151
Brian Norris7854d3f2011-06-23 14:12:08 -0700152 /* Initialize the syndrome buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 for (i = 0; i < NROOTS; i++)
154 s[i] = ds[0];
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000155 /*
156 * Evaluate
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 * s[i] = ds[3]x^3 + ds[2]x^2 + ds[1]x^1 + ds[0]
158 * where x = alpha^(FCR + i)
159 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100160 for (j = 1; j < NROOTS; j++) {
161 if (ds[j] == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 continue;
163 tmp = rs->index_of[ds[j]];
David Woodhousee0c7d762006-05-13 18:07:53 +0100164 for (i = 0; i < NROOTS; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 s[i] ^= rs->alpha_to[rs_modnn(rs, tmp + (FCR + i) * j)];
166 }
167
Mark Warec9fb6772010-05-27 11:45:39 +1000168 /* Calc syn[i] = s[i] / alpha^(v + i) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 for (i = 0; i < NROOTS; i++) {
Mark Warec9fb6772010-05-27 11:45:39 +1000170 if (s[i])
David Woodhousee0c7d762006-05-13 18:07:53 +0100171 syn[i] = rs_modnn(rs, rs->index_of[s[i]] + (NN - FCR - i));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 }
173 /* Call the decoder library */
174 nerr = decode_rs16(rs, NULL, NULL, 1019, syn, 0, errpos, 0, errval);
175
176 /* Incorrectable errors ? */
177 if (nerr < 0)
178 return nerr;
179
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000180 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 * Correct the errors. The bitpositions are a bit of magic,
182 * but they are given by the design of the de/encoder circuit
183 * in the DoC ASIC's.
184 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100185 for (i = 0; i < nerr; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 int index, bitpos, pos = 1015 - errpos[i];
187 uint8_t val;
188 if (pos >= NB_DATA && pos < 1019)
189 continue;
190 if (pos < NB_DATA) {
191 /* extract bit position (MSB first) */
192 pos = 10 * (NB_DATA - 1 - pos) - 6;
193 /* now correct the following 10 bits. At most two bytes
194 can be modified since pos is even */
195 index = (pos >> 3) ^ 1;
196 bitpos = pos & 7;
David Woodhousee0c7d762006-05-13 18:07:53 +0100197 if ((index >= 0 && index < SECTOR_SIZE) || index == (SECTOR_SIZE + 1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 val = (uint8_t) (errval[i] >> (2 + bitpos));
199 parity ^= val;
200 if (index < SECTOR_SIZE)
201 data[index] ^= val;
202 }
203 index = ((pos >> 3) + 1) ^ 1;
204 bitpos = (bitpos + 10) & 7;
205 if (bitpos == 0)
206 bitpos = 8;
David Woodhousee0c7d762006-05-13 18:07:53 +0100207 if ((index >= 0 && index < SECTOR_SIZE) || index == (SECTOR_SIZE + 1)) {
208 val = (uint8_t) (errval[i] << (8 - bitpos));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 parity ^= val;
210 if (index < SECTOR_SIZE)
211 data[index] ^= val;
212 }
213 }
214 }
215 /* If the parity is wrong, no rescue possible */
Jörn Engeleb684502007-10-20 23:16:32 +0200216 return parity ? -EBADMSG : nerr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217}
218
219static void DoC_Delay(struct doc_priv *doc, unsigned short cycles)
220{
221 volatile char dummy;
222 int i;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000223
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 for (i = 0; i < cycles; i++) {
225 if (DoC_is_Millennium(doc))
226 dummy = ReadDOC(doc->virtadr, NOP);
227 else if (DoC_is_MillenniumPlus(doc))
228 dummy = ReadDOC(doc->virtadr, Mplus_NOP);
229 else
230 dummy = ReadDOC(doc->virtadr, DOCStatus);
231 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000232
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233}
234
235#define CDSN_CTRL_FR_B_MASK (CDSN_CTRL_FR_B0 | CDSN_CTRL_FR_B1)
236
237/* DOC_WaitReady: Wait for RDY line to be asserted by the flash chip */
238static int _DoC_WaitReady(struct doc_priv *doc)
239{
David Woodhousee0c7d762006-05-13 18:07:53 +0100240 void __iomem *docptr = doc->virtadr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 unsigned long timeo = jiffies + (HZ * 10);
242
David Woodhousee0c7d762006-05-13 18:07:53 +0100243 if (debug)
244 printk("_DoC_WaitReady...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 /* Out-of-line routine to wait for chip response */
246 if (DoC_is_MillenniumPlus(doc)) {
247 while ((ReadDOC(docptr, Mplus_FlashControl) & CDSN_CTRL_FR_B_MASK) != CDSN_CTRL_FR_B_MASK) {
248 if (time_after(jiffies, timeo)) {
249 printk("_DoC_WaitReady timed out.\n");
250 return -EIO;
251 }
252 udelay(1);
253 cond_resched();
254 }
255 } else {
256 while (!(ReadDOC(docptr, CDSNControl) & CDSN_CTRL_FR_B)) {
257 if (time_after(jiffies, timeo)) {
258 printk("_DoC_WaitReady timed out.\n");
259 return -EIO;
260 }
261 udelay(1);
262 cond_resched();
263 }
264 }
265
266 return 0;
267}
268
269static inline int DoC_WaitReady(struct doc_priv *doc)
270{
David Woodhousee0c7d762006-05-13 18:07:53 +0100271 void __iomem *docptr = doc->virtadr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 int ret = 0;
273
274 if (DoC_is_MillenniumPlus(doc)) {
275 DoC_Delay(doc, 4);
276
277 if ((ReadDOC(docptr, Mplus_FlashControl) & CDSN_CTRL_FR_B_MASK) != CDSN_CTRL_FR_B_MASK)
278 /* Call the out-of-line routine to wait */
279 ret = _DoC_WaitReady(doc);
280 } else {
281 DoC_Delay(doc, 4);
282
283 if (!(ReadDOC(docptr, CDSNControl) & CDSN_CTRL_FR_B))
284 /* Call the out-of-line routine to wait */
285 ret = _DoC_WaitReady(doc);
286 DoC_Delay(doc, 2);
287 }
288
David Woodhousee0c7d762006-05-13 18:07:53 +0100289 if (debug)
290 printk("DoC_WaitReady OK\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 return ret;
292}
293
294static void doc2000_write_byte(struct mtd_info *mtd, u_char datum)
295{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100296 struct nand_chip *this = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100297 struct doc_priv *doc = nand_get_controller_data(this);
David Woodhousee0c7d762006-05-13 18:07:53 +0100298 void __iomem *docptr = doc->virtadr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
David Woodhousee0c7d762006-05-13 18:07:53 +0100300 if (debug)
301 printk("write_byte %02x\n", datum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 WriteDOC(datum, docptr, CDSNSlowIO);
303 WriteDOC(datum, docptr, 2k_CDSN_IO);
304}
305
306static u_char doc2000_read_byte(struct mtd_info *mtd)
307{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100308 struct nand_chip *this = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100309 struct doc_priv *doc = nand_get_controller_data(this);
David Woodhousee0c7d762006-05-13 18:07:53 +0100310 void __iomem *docptr = doc->virtadr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 u_char ret;
312
313 ReadDOC(docptr, CDSNSlowIO);
314 DoC_Delay(doc, 2);
315 ret = ReadDOC(docptr, 2k_CDSN_IO);
David Woodhousee0c7d762006-05-13 18:07:53 +0100316 if (debug)
317 printk("read_byte returns %02x\n", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 return ret;
319}
320
David Woodhousee0c7d762006-05-13 18:07:53 +0100321static void doc2000_writebuf(struct mtd_info *mtd, const u_char *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100323 struct nand_chip *this = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100324 struct doc_priv *doc = nand_get_controller_data(this);
David Woodhousee0c7d762006-05-13 18:07:53 +0100325 void __iomem *docptr = doc->virtadr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 int i;
David Woodhousee0c7d762006-05-13 18:07:53 +0100327 if (debug)
328 printk("writebuf of %d bytes: ", len);
329 for (i = 0; i < len; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 WriteDOC_(buf[i], docptr, DoC_2k_CDSN_IO + i);
331 if (debug && i < 16)
332 printk("%02x ", buf[i]);
333 }
David Woodhousee0c7d762006-05-13 18:07:53 +0100334 if (debug)
335 printk("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336}
337
David Woodhousee0c7d762006-05-13 18:07:53 +0100338static void doc2000_readbuf(struct mtd_info *mtd, u_char *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100340 struct nand_chip *this = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100341 struct doc_priv *doc = nand_get_controller_data(this);
David Woodhousee0c7d762006-05-13 18:07:53 +0100342 void __iomem *docptr = doc->virtadr;
343 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344
David Woodhousee0c7d762006-05-13 18:07:53 +0100345 if (debug)
346 printk("readbuf of %d bytes: ", len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347
David Woodhousee0c7d762006-05-13 18:07:53 +0100348 for (i = 0; i < len; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 buf[i] = ReadDOC(docptr, 2k_CDSN_IO + i);
350 }
351}
352
David Woodhousee0c7d762006-05-13 18:07:53 +0100353static void doc2000_readbuf_dword(struct mtd_info *mtd, u_char *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100355 struct nand_chip *this = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100356 struct doc_priv *doc = nand_get_controller_data(this);
David Woodhousee0c7d762006-05-13 18:07:53 +0100357 void __iomem *docptr = doc->virtadr;
358 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
David Woodhousee0c7d762006-05-13 18:07:53 +0100360 if (debug)
361 printk("readbuf_dword of %d bytes: ", len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
David Woodhousee0c7d762006-05-13 18:07:53 +0100363 if (unlikely((((unsigned long)buf) | len) & 3)) {
364 for (i = 0; i < len; i++) {
365 *(uint8_t *) (&buf[i]) = ReadDOC(docptr, 2k_CDSN_IO + i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 }
367 } else {
David Woodhousee0c7d762006-05-13 18:07:53 +0100368 for (i = 0; i < len; i += 4) {
369 *(uint32_t *) (&buf[i]) = readl(docptr + DoC_2k_CDSN_IO + i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 }
371 }
372}
373
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374static uint16_t __init doc200x_ident_chip(struct mtd_info *mtd, int nr)
375{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100376 struct nand_chip *this = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100377 struct doc_priv *doc = nand_get_controller_data(this);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 uint16_t ret;
379
380 doc200x_select_chip(mtd, nr);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200381 doc200x_hwcontrol(mtd, NAND_CMD_READID,
382 NAND_CTRL_CLE | NAND_CTRL_CHANGE);
383 doc200x_hwcontrol(mtd, 0, NAND_CTRL_ALE | NAND_CTRL_CHANGE);
384 doc200x_hwcontrol(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000385
Lucas De Marchie9c54992011-04-26 23:28:26 -0700386 /* We can't use dev_ready here, but at least we wait for the
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000387 * command to complete
Thomas Gleixnerdfd61292005-02-22 21:48:25 +0000388 */
389 udelay(50);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000390
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 ret = this->read_byte(mtd) << 8;
392 ret |= this->read_byte(mtd);
393
394 if (doc->ChipID == DOC_ChipID_Doc2k && try_dword && !nr) {
395 /* First chip probe. See if we get same results by 32-bit access */
396 union {
397 uint32_t dword;
398 uint8_t byte[4];
399 } ident;
400 void __iomem *docptr = doc->virtadr;
401
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200402 doc200x_hwcontrol(mtd, NAND_CMD_READID,
403 NAND_CTRL_CLE | NAND_CTRL_CHANGE);
404 doc200x_hwcontrol(mtd, 0, NAND_CTRL_ALE | NAND_CTRL_CHANGE);
405 doc200x_hwcontrol(mtd, NAND_CMD_NONE,
406 NAND_NCE | NAND_CTRL_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407
Thomas Gleixnerdfd61292005-02-22 21:48:25 +0000408 udelay(50);
409
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 ident.dword = readl(docptr + DoC_2k_CDSN_IO);
411 if (((ident.byte[0] << 8) | ident.byte[1]) == ret) {
Shreeya Patel63fa37f2018-02-22 22:01:22 +0530412 pr_info("DiskOnChip 2000 responds to DWORD access\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 this->read_buf = &doc2000_readbuf_dword;
414 }
415 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000416
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 return ret;
418}
419
420static void __init doc2000_count_chips(struct mtd_info *mtd)
421{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100422 struct nand_chip *this = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100423 struct doc_priv *doc = nand_get_controller_data(this);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 uint16_t mfrid;
425 int i;
426
427 /* Max 4 chips per floor on DiskOnChip 2000 */
428 doc->chips_per_floor = 4;
429
430 /* Find out what the first chip is */
431 mfrid = doc200x_ident_chip(mtd, 0);
432
433 /* Find how many chips in each floor. */
434 for (i = 1; i < 4; i++) {
435 if (doc200x_ident_chip(mtd, i) != mfrid)
436 break;
437 }
438 doc->chips_per_floor = i;
Shreeya Patel63fa37f2018-02-22 22:01:22 +0530439 pr_debug("Detected %d chips per floor.\n", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440}
441
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200442static int doc200x_wait(struct mtd_info *mtd, struct nand_chip *this)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443{
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100444 struct doc_priv *doc = nand_get_controller_data(this);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
446 int status;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000447
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 DoC_WaitReady(doc);
Boris Brezillon97d90da2017-11-30 18:01:29 +0100449 nand_status_op(this, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 DoC_WaitReady(doc);
451 status = (int)this->read_byte(mtd);
452
453 return status;
454}
455
456static void doc2001_write_byte(struct mtd_info *mtd, u_char datum)
457{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100458 struct nand_chip *this = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100459 struct doc_priv *doc = nand_get_controller_data(this);
David Woodhousee0c7d762006-05-13 18:07:53 +0100460 void __iomem *docptr = doc->virtadr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461
462 WriteDOC(datum, docptr, CDSNSlowIO);
463 WriteDOC(datum, docptr, Mil_CDSN_IO);
464 WriteDOC(datum, docptr, WritePipeTerm);
465}
466
467static u_char doc2001_read_byte(struct mtd_info *mtd)
468{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100469 struct nand_chip *this = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100470 struct doc_priv *doc = nand_get_controller_data(this);
David Woodhousee0c7d762006-05-13 18:07:53 +0100471 void __iomem *docptr = doc->virtadr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472
473 //ReadDOC(docptr, CDSNSlowIO);
474 /* 11.4.5 -- delay twice to allow extended length cycle */
475 DoC_Delay(doc, 2);
476 ReadDOC(docptr, ReadPipeInit);
477 //return ReadDOC(docptr, Mil_CDSN_IO);
478 return ReadDOC(docptr, LastDataRead);
479}
480
David Woodhousee0c7d762006-05-13 18:07:53 +0100481static void doc2001_writebuf(struct mtd_info *mtd, const u_char *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100483 struct nand_chip *this = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100484 struct doc_priv *doc = nand_get_controller_data(this);
David Woodhousee0c7d762006-05-13 18:07:53 +0100485 void __iomem *docptr = doc->virtadr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 int i;
487
David Woodhousee0c7d762006-05-13 18:07:53 +0100488 for (i = 0; i < len; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 WriteDOC_(buf[i], docptr, DoC_Mil_CDSN_IO + i);
490 /* Terminate write pipeline */
491 WriteDOC(0x00, docptr, WritePipeTerm);
492}
493
David Woodhousee0c7d762006-05-13 18:07:53 +0100494static void doc2001_readbuf(struct mtd_info *mtd, u_char *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100496 struct nand_chip *this = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100497 struct doc_priv *doc = nand_get_controller_data(this);
David Woodhousee0c7d762006-05-13 18:07:53 +0100498 void __iomem *docptr = doc->virtadr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 int i;
500
501 /* Start read pipeline */
502 ReadDOC(docptr, ReadPipeInit);
503
David Woodhousee0c7d762006-05-13 18:07:53 +0100504 for (i = 0; i < len - 1; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 buf[i] = ReadDOC(docptr, Mil_CDSN_IO + (i & 0xff));
506
507 /* Terminate read pipeline */
508 buf[i] = ReadDOC(docptr, LastDataRead);
509}
510
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511static u_char doc2001plus_read_byte(struct mtd_info *mtd)
512{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100513 struct nand_chip *this = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100514 struct doc_priv *doc = nand_get_controller_data(this);
David Woodhousee0c7d762006-05-13 18:07:53 +0100515 void __iomem *docptr = doc->virtadr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 u_char ret;
517
David Woodhousee0c7d762006-05-13 18:07:53 +0100518 ReadDOC(docptr, Mplus_ReadPipeInit);
519 ReadDOC(docptr, Mplus_ReadPipeInit);
520 ret = ReadDOC(docptr, Mplus_LastDataRead);
521 if (debug)
522 printk("read_byte returns %02x\n", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 return ret;
524}
525
David Woodhousee0c7d762006-05-13 18:07:53 +0100526static void doc2001plus_writebuf(struct mtd_info *mtd, const u_char *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100528 struct nand_chip *this = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100529 struct doc_priv *doc = nand_get_controller_data(this);
David Woodhousee0c7d762006-05-13 18:07:53 +0100530 void __iomem *docptr = doc->virtadr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 int i;
532
David Woodhousee0c7d762006-05-13 18:07:53 +0100533 if (debug)
534 printk("writebuf of %d bytes: ", len);
535 for (i = 0; i < len; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 WriteDOC_(buf[i], docptr, DoC_Mil_CDSN_IO + i);
537 if (debug && i < 16)
538 printk("%02x ", buf[i]);
539 }
David Woodhousee0c7d762006-05-13 18:07:53 +0100540 if (debug)
541 printk("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542}
543
David Woodhousee0c7d762006-05-13 18:07:53 +0100544static void doc2001plus_readbuf(struct mtd_info *mtd, u_char *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100546 struct nand_chip *this = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100547 struct doc_priv *doc = nand_get_controller_data(this);
David Woodhousee0c7d762006-05-13 18:07:53 +0100548 void __iomem *docptr = doc->virtadr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 int i;
550
David Woodhousee0c7d762006-05-13 18:07:53 +0100551 if (debug)
552 printk("readbuf of %d bytes: ", len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553
554 /* Start read pipeline */
555 ReadDOC(docptr, Mplus_ReadPipeInit);
556 ReadDOC(docptr, Mplus_ReadPipeInit);
557
David Woodhousee0c7d762006-05-13 18:07:53 +0100558 for (i = 0; i < len - 2; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 buf[i] = ReadDOC(docptr, Mil_CDSN_IO);
560 if (debug && i < 16)
561 printk("%02x ", buf[i]);
562 }
563
564 /* Terminate read pipeline */
David Woodhousee0c7d762006-05-13 18:07:53 +0100565 buf[len - 2] = ReadDOC(docptr, Mplus_LastDataRead);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 if (debug && i < 16)
David Woodhousee0c7d762006-05-13 18:07:53 +0100567 printk("%02x ", buf[len - 2]);
568 buf[len - 1] = ReadDOC(docptr, Mplus_LastDataRead);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 if (debug && i < 16)
David Woodhousee0c7d762006-05-13 18:07:53 +0100570 printk("%02x ", buf[len - 1]);
571 if (debug)
572 printk("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573}
574
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575static void doc2001plus_select_chip(struct mtd_info *mtd, int chip)
576{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100577 struct nand_chip *this = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100578 struct doc_priv *doc = nand_get_controller_data(this);
David Woodhousee0c7d762006-05-13 18:07:53 +0100579 void __iomem *docptr = doc->virtadr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 int floor = 0;
581
David Woodhousee0c7d762006-05-13 18:07:53 +0100582 if (debug)
583 printk("select chip (%d)\n", chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584
585 if (chip == -1) {
586 /* Disable flash internally */
587 WriteDOC(0, docptr, Mplus_FlashSelect);
588 return;
589 }
590
591 floor = chip / doc->chips_per_floor;
David Woodhousee0c7d762006-05-13 18:07:53 +0100592 chip -= (floor * doc->chips_per_floor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
594 /* Assert ChipEnable and deassert WriteProtect */
595 WriteDOC((DOC_FLASH_CE), docptr, Mplus_FlashSelect);
Boris Brezillon97d90da2017-11-30 18:01:29 +0100596 nand_reset_op(this);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
598 doc->curchip = chip;
599 doc->curfloor = floor;
600}
601
602static void doc200x_select_chip(struct mtd_info *mtd, int chip)
603{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100604 struct nand_chip *this = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100605 struct doc_priv *doc = nand_get_controller_data(this);
David Woodhousee0c7d762006-05-13 18:07:53 +0100606 void __iomem *docptr = doc->virtadr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 int floor = 0;
608
David Woodhousee0c7d762006-05-13 18:07:53 +0100609 if (debug)
610 printk("select chip (%d)\n", chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611
612 if (chip == -1)
613 return;
614
615 floor = chip / doc->chips_per_floor;
David Woodhousee0c7d762006-05-13 18:07:53 +0100616 chip -= (floor * doc->chips_per_floor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617
618 /* 11.4.4 -- deassert CE before changing chip */
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200619 doc200x_hwcontrol(mtd, NAND_CMD_NONE, 0 | NAND_CTRL_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620
621 WriteDOC(floor, docptr, FloorSelect);
622 WriteDOC(chip, docptr, CDSNDeviceSelect);
623
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200624 doc200x_hwcontrol(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625
626 doc->curchip = chip;
627 doc->curfloor = floor;
628}
629
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200630#define CDSN_CTRL_MSK (CDSN_CTRL_CE | CDSN_CTRL_CLE | CDSN_CTRL_ALE)
631
632static void doc200x_hwcontrol(struct mtd_info *mtd, int cmd,
633 unsigned int ctrl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100635 struct nand_chip *this = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100636 struct doc_priv *doc = nand_get_controller_data(this);
David Woodhousee0c7d762006-05-13 18:07:53 +0100637 void __iomem *docptr = doc->virtadr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200639 if (ctrl & NAND_CTRL_CHANGE) {
640 doc->CDSNControl &= ~CDSN_CTRL_MSK;
641 doc->CDSNControl |= ctrl & CDSN_CTRL_MSK;
642 if (debug)
643 printk("hwcontrol(%d): %02x\n", cmd, doc->CDSNControl);
644 WriteDOC(doc->CDSNControl, docptr, CDSNControl);
645 /* 11.4.3 -- 4 NOPs after CSDNControl write */
646 DoC_Delay(doc, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 }
Thomas Gleixnercad74f22006-05-23 23:28:48 +0200648 if (cmd != NAND_CMD_NONE) {
649 if (DoC_is_2000(doc))
650 doc2000_write_byte(mtd, cmd);
651 else
652 doc2001_write_byte(mtd, cmd);
653 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654}
655
David Woodhousee0c7d762006-05-13 18:07:53 +0100656static void doc2001plus_command(struct mtd_info *mtd, unsigned command, int column, int page_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100658 struct nand_chip *this = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100659 struct doc_priv *doc = nand_get_controller_data(this);
David Woodhousee0c7d762006-05-13 18:07:53 +0100660 void __iomem *docptr = doc->virtadr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661
662 /*
663 * Must terminate write pipeline before sending any commands
664 * to the device.
665 */
666 if (command == NAND_CMD_PAGEPROG) {
667 WriteDOC(0x00, docptr, Mplus_WritePipeTerm);
668 WriteDOC(0x00, docptr, Mplus_WritePipeTerm);
669 }
670
671 /*
672 * Write out the command to the device.
673 */
674 if (command == NAND_CMD_SEQIN) {
675 int readcmd;
676
Joern Engel28318772006-05-22 23:18:05 +0200677 if (column >= mtd->writesize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 /* OOB area */
Joern Engel28318772006-05-22 23:18:05 +0200679 column -= mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 readcmd = NAND_CMD_READOOB;
681 } else if (column < 256) {
682 /* First 256 bytes --> READ0 */
683 readcmd = NAND_CMD_READ0;
684 } else {
685 column -= 256;
686 readcmd = NAND_CMD_READ1;
687 }
688 WriteDOC(readcmd, docptr, Mplus_FlashCmd);
689 }
690 WriteDOC(command, docptr, Mplus_FlashCmd);
691 WriteDOC(0, docptr, Mplus_WritePipeTerm);
692 WriteDOC(0, docptr, Mplus_WritePipeTerm);
693
694 if (column != -1 || page_addr != -1) {
695 /* Serially input address */
696 if (column != -1) {
697 /* Adjust columns for 16 bit buswidth */
Brian Norris3dad2342014-01-29 14:08:12 -0800698 if (this->options & NAND_BUSWIDTH_16 &&
699 !nand_opcode_8bits(command))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 column >>= 1;
701 WriteDOC(column, docptr, Mplus_FlashAddress);
702 }
703 if (page_addr != -1) {
David Woodhousee0c7d762006-05-13 18:07:53 +0100704 WriteDOC((unsigned char)(page_addr & 0xff), docptr, Mplus_FlashAddress);
705 WriteDOC((unsigned char)((page_addr >> 8) & 0xff), docptr, Mplus_FlashAddress);
Masahiro Yamada14157f82017-09-13 11:05:50 +0900706 if (this->options & NAND_ROW_ADDR_3) {
David Woodhousee0c7d762006-05-13 18:07:53 +0100707 WriteDOC((unsigned char)((page_addr >> 16) & 0x0f), docptr, Mplus_FlashAddress);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 printk("high density\n");
709 }
710 }
711 WriteDOC(0, docptr, Mplus_WritePipeTerm);
712 WriteDOC(0, docptr, Mplus_WritePipeTerm);
713 /* deassert ALE */
David Woodhousee0c7d762006-05-13 18:07:53 +0100714 if (command == NAND_CMD_READ0 || command == NAND_CMD_READ1 ||
715 command == NAND_CMD_READOOB || command == NAND_CMD_READID)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 WriteDOC(0, docptr, Mplus_FlashControl);
717 }
718
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000719 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 * program and erase have their own busy handlers
721 * status and sequential in needs no delay
David Woodhousee0c7d762006-05-13 18:07:53 +0100722 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 switch (command) {
724
725 case NAND_CMD_PAGEPROG:
726 case NAND_CMD_ERASE1:
727 case NAND_CMD_ERASE2:
728 case NAND_CMD_SEQIN:
729 case NAND_CMD_STATUS:
730 return;
731
732 case NAND_CMD_RESET:
733 if (this->dev_ready)
734 break;
735 udelay(this->chip_delay);
736 WriteDOC(NAND_CMD_STATUS, docptr, Mplus_FlashCmd);
737 WriteDOC(0, docptr, Mplus_WritePipeTerm);
738 WriteDOC(0, docptr, Mplus_WritePipeTerm);
David Woodhousee0c7d762006-05-13 18:07:53 +0100739 while (!(this->read_byte(mtd) & 0x40)) ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 return;
741
David Woodhousee0c7d762006-05-13 18:07:53 +0100742 /* This applies to read commands */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 default:
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000744 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 * If we don't have access to the busy pin, we apply the given
746 * command delay
David Woodhousee0c7d762006-05-13 18:07:53 +0100747 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 if (!this->dev_ready) {
David Woodhousee0c7d762006-05-13 18:07:53 +0100749 udelay(this->chip_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 return;
751 }
752 }
753
754 /* Apply this short delay always to ensure that we do wait tWB in
755 * any case on any machine. */
David Woodhousee0c7d762006-05-13 18:07:53 +0100756 ndelay(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 /* wait until command is processed */
David Woodhousee0c7d762006-05-13 18:07:53 +0100758 while (!this->dev_ready(mtd)) ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759}
760
761static int doc200x_dev_ready(struct mtd_info *mtd)
762{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100763 struct nand_chip *this = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100764 struct doc_priv *doc = nand_get_controller_data(this);
David Woodhousee0c7d762006-05-13 18:07:53 +0100765 void __iomem *docptr = doc->virtadr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766
767 if (DoC_is_MillenniumPlus(doc)) {
768 /* 11.4.2 -- must NOP four times before checking FR/B# */
769 DoC_Delay(doc, 4);
770 if ((ReadDOC(docptr, Mplus_FlashControl) & CDSN_CTRL_FR_B_MASK) != CDSN_CTRL_FR_B_MASK) {
David Woodhousee0c7d762006-05-13 18:07:53 +0100771 if (debug)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 printk("not ready\n");
773 return 0;
774 }
David Woodhousee0c7d762006-05-13 18:07:53 +0100775 if (debug)
776 printk("was ready\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 return 1;
778 } else {
779 /* 11.4.2 -- must NOP four times before checking FR/B# */
780 DoC_Delay(doc, 4);
781 if (!(ReadDOC(docptr, CDSNControl) & CDSN_CTRL_FR_B)) {
David Woodhousee0c7d762006-05-13 18:07:53 +0100782 if (debug)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 printk("not ready\n");
784 return 0;
785 }
786 /* 11.4.2 -- Must NOP twice if it's ready */
787 DoC_Delay(doc, 2);
David Woodhousee0c7d762006-05-13 18:07:53 +0100788 if (debug)
789 printk("was ready\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 return 1;
791 }
792}
793
Archit Taneja9f3e0422016-02-03 14:29:49 +0530794static int doc200x_block_bad(struct mtd_info *mtd, loff_t ofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795{
796 /* This is our last resort if we couldn't find or create a BBT. Just
797 pretend all blocks are good. */
798 return 0;
799}
800
801static void doc200x_enable_hwecc(struct mtd_info *mtd, int mode)
802{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100803 struct nand_chip *this = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100804 struct doc_priv *doc = nand_get_controller_data(this);
David Woodhousee0c7d762006-05-13 18:07:53 +0100805 void __iomem *docptr = doc->virtadr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806
807 /* Prime the ECC engine */
David Woodhousee0c7d762006-05-13 18:07:53 +0100808 switch (mode) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 case NAND_ECC_READ:
810 WriteDOC(DOC_ECC_RESET, docptr, ECCConf);
811 WriteDOC(DOC_ECC_EN, docptr, ECCConf);
812 break;
813 case NAND_ECC_WRITE:
814 WriteDOC(DOC_ECC_RESET, docptr, ECCConf);
815 WriteDOC(DOC_ECC_EN | DOC_ECC_RW, docptr, ECCConf);
816 break;
817 }
818}
819
820static void doc2001plus_enable_hwecc(struct mtd_info *mtd, int mode)
821{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100822 struct nand_chip *this = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100823 struct doc_priv *doc = nand_get_controller_data(this);
David Woodhousee0c7d762006-05-13 18:07:53 +0100824 void __iomem *docptr = doc->virtadr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825
826 /* Prime the ECC engine */
David Woodhousee0c7d762006-05-13 18:07:53 +0100827 switch (mode) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 case NAND_ECC_READ:
829 WriteDOC(DOC_ECC_RESET, docptr, Mplus_ECCConf);
830 WriteDOC(DOC_ECC_EN, docptr, Mplus_ECCConf);
831 break;
832 case NAND_ECC_WRITE:
833 WriteDOC(DOC_ECC_RESET, docptr, Mplus_ECCConf);
834 WriteDOC(DOC_ECC_EN | DOC_ECC_RW, docptr, Mplus_ECCConf);
835 break;
836 }
837}
838
839/* This code is only called on write */
David Woodhousee0c7d762006-05-13 18:07:53 +0100840static int doc200x_calculate_ecc(struct mtd_info *mtd, const u_char *dat, unsigned char *ecc_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100842 struct nand_chip *this = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100843 struct doc_priv *doc = nand_get_controller_data(this);
David Woodhousee0c7d762006-05-13 18:07:53 +0100844 void __iomem *docptr = doc->virtadr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 int i;
846 int emptymatch = 1;
847
848 /* flush the pipeline */
849 if (DoC_is_2000(doc)) {
850 WriteDOC(doc->CDSNControl & ~CDSN_CTRL_FLASH_IO, docptr, CDSNControl);
851 WriteDOC(0, docptr, 2k_CDSN_IO);
852 WriteDOC(0, docptr, 2k_CDSN_IO);
853 WriteDOC(0, docptr, 2k_CDSN_IO);
854 WriteDOC(doc->CDSNControl, docptr, CDSNControl);
855 } else if (DoC_is_MillenniumPlus(doc)) {
856 WriteDOC(0, docptr, Mplus_NOP);
857 WriteDOC(0, docptr, Mplus_NOP);
858 WriteDOC(0, docptr, Mplus_NOP);
859 } else {
860 WriteDOC(0, docptr, NOP);
861 WriteDOC(0, docptr, NOP);
862 WriteDOC(0, docptr, NOP);
863 }
864
865 for (i = 0; i < 6; i++) {
866 if (DoC_is_MillenniumPlus(doc))
867 ecc_code[i] = ReadDOC_(docptr, DoC_Mplus_ECCSyndrome0 + i);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000868 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 ecc_code[i] = ReadDOC_(docptr, DoC_ECCSyndrome0 + i);
870 if (ecc_code[i] != empty_write_ecc[i])
871 emptymatch = 0;
872 }
873 if (DoC_is_MillenniumPlus(doc))
874 WriteDOC(DOC_ECC_DIS, docptr, Mplus_ECCConf);
875 else
876 WriteDOC(DOC_ECC_DIS, docptr, ECCConf);
877#if 0
878 /* If emptymatch=1, we might have an all-0xff data buffer. Check. */
879 if (emptymatch) {
880 /* Note: this somewhat expensive test should not be triggered
881 often. It could be optimized away by examining the data in
882 the writebuf routine, and remembering the result. */
883 for (i = 0; i < 512; i++) {
David Woodhousee0c7d762006-05-13 18:07:53 +0100884 if (dat[i] == 0xff)
885 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 emptymatch = 0;
887 break;
888 }
889 }
890 /* If emptymatch still =1, we do have an all-0xff data buffer.
891 Return all-0xff ecc value instead of the computed one, so
892 it'll look just like a freshly-erased page. */
David Woodhousee0c7d762006-05-13 18:07:53 +0100893 if (emptymatch)
894 memset(ecc_code, 0xff, 6);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895#endif
896 return 0;
897}
898
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200899static int doc200x_correct_data(struct mtd_info *mtd, u_char *dat,
900 u_char *read_ecc, u_char *isnull)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901{
902 int i, ret = 0;
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100903 struct nand_chip *this = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100904 struct doc_priv *doc = nand_get_controller_data(this);
David Woodhousee0c7d762006-05-13 18:07:53 +0100905 void __iomem *docptr = doc->virtadr;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200906 uint8_t calc_ecc[6];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 volatile u_char dummy;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000908
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 /* flush the pipeline */
910 if (DoC_is_2000(doc)) {
911 dummy = ReadDOC(docptr, 2k_ECCStatus);
912 dummy = ReadDOC(docptr, 2k_ECCStatus);
913 dummy = ReadDOC(docptr, 2k_ECCStatus);
914 } else if (DoC_is_MillenniumPlus(doc)) {
915 dummy = ReadDOC(docptr, Mplus_ECCConf);
916 dummy = ReadDOC(docptr, Mplus_ECCConf);
917 dummy = ReadDOC(docptr, Mplus_ECCConf);
918 } else {
919 dummy = ReadDOC(docptr, ECCConf);
920 dummy = ReadDOC(docptr, ECCConf);
921 dummy = ReadDOC(docptr, ECCConf);
922 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000923
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300924 /* Error occurred ? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 if (dummy & 0x80) {
926 for (i = 0; i < 6; i++) {
927 if (DoC_is_MillenniumPlus(doc))
928 calc_ecc[i] = ReadDOC_(docptr, DoC_Mplus_ECCSyndrome0 + i);
929 else
930 calc_ecc[i] = ReadDOC_(docptr, DoC_ECCSyndrome0 + i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 }
Boris BREZILLONcc01e602015-12-30 20:39:52 +0100932
933 ret = doc_ecc_decode(rs_decoder, dat, calc_ecc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 if (ret > 0)
Shreeya Patel63fa37f2018-02-22 22:01:22 +0530935 pr_err("doc200x_correct_data corrected %d errors\n",
936 ret);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000937 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 if (DoC_is_MillenniumPlus(doc))
939 WriteDOC(DOC_ECC_DIS, docptr, Mplus_ECCConf);
940 else
941 WriteDOC(DOC_ECC_DIS, docptr, ECCConf);
Brian Norrisd57f40542011-09-20 18:34:25 -0700942 if (no_ecc_failures && mtd_is_eccerr(ret)) {
Shreeya Patel63fa37f2018-02-22 22:01:22 +0530943 pr_err("suppressing ECC failure\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 ret = 0;
945 }
946 return ret;
947}
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000948
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949//u_char mydatabuf[528];
950
Boris Brezillon68c1b752016-02-03 20:00:23 +0100951static int doc200x_ooblayout_ecc(struct mtd_info *mtd, int section,
952 struct mtd_oob_region *oobregion)
953{
954 if (section)
955 return -ERANGE;
956
957 oobregion->offset = 0;
958 oobregion->length = 6;
959
960 return 0;
961}
962
963static int doc200x_ooblayout_free(struct mtd_info *mtd, int section,
964 struct mtd_oob_region *oobregion)
965{
966 if (section > 1)
967 return -ERANGE;
968
969 /*
970 * The strange out-of-order free bytes definition is a (possibly
971 * unneeded) attempt to retain compatibility. It used to read:
972 * .oobfree = { {8, 8} }
973 * Since that leaves two bytes unusable, it was changed. But the
974 * following scheme might affect existing jffs2 installs by moving the
975 * cleanmarker:
976 * .oobfree = { {6, 10} }
977 * jffs2 seems to handle the above gracefully, but the current scheme
978 * seems safer. The only problem with it is that any code retrieving
979 * free bytes position must be able to handle out-of-order segments.
980 */
981 if (!section) {
982 oobregion->offset = 8;
983 oobregion->length = 8;
984 } else {
985 oobregion->offset = 6;
986 oobregion->length = 2;
987 }
988
989 return 0;
990}
991
992static const struct mtd_ooblayout_ops doc200x_ooblayout_ops = {
993 .ecc = doc200x_ooblayout_ecc,
994 .free = doc200x_ooblayout_free,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995};
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000996
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997/* Find the (I)NFTL Media Header, and optionally also the mirror media header.
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200998 On successful return, buf will contain a copy of the media header for
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 further processing. id is the string to scan for, and will presumably be
1000 either "ANAND" or "BNAND". If findmirror=1, also look for the mirror media
1001 header. The page #s of the found media headers are placed in mh0_page and
1002 mh1_page in the DOC private structure. */
David Woodhousee0c7d762006-05-13 18:07:53 +01001003static int __init find_media_headers(struct mtd_info *mtd, u_char *buf, const char *id, int findmirror)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +01001005 struct nand_chip *this = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +01001006 struct doc_priv *doc = nand_get_controller_data(this);
Dan Brown1a78ff62005-03-29 21:57:48 +01001007 unsigned offs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 int ret;
1009 size_t retlen;
1010
Dan Brown1a78ff62005-03-29 21:57:48 +01001011 for (offs = 0; offs < mtd->size; offs += mtd->erasesize) {
Artem Bityutskiy329ad392011-12-23 17:30:16 +02001012 ret = mtd_read(mtd, offs, mtd->writesize, &retlen, buf);
Joern Engel28318772006-05-22 23:18:05 +02001013 if (retlen != mtd->writesize)
David Woodhousee0c7d762006-05-13 18:07:53 +01001014 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 if (ret) {
Shreeya Patel63fa37f2018-02-22 22:01:22 +05301016 pr_warn("ECC error scanning DOC at 0x%x\n", offs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 }
David Woodhousee0c7d762006-05-13 18:07:53 +01001018 if (memcmp(buf, id, 6))
1019 continue;
Shreeya Patel63fa37f2018-02-22 22:01:22 +05301020 pr_info("Found DiskOnChip %s Media Header at 0x%x\n", id, offs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 if (doc->mh0_page == -1) {
1022 doc->mh0_page = offs >> this->page_shift;
David Woodhousee0c7d762006-05-13 18:07:53 +01001023 if (!findmirror)
1024 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 continue;
1026 }
1027 doc->mh1_page = offs >> this->page_shift;
1028 return 2;
1029 }
1030 if (doc->mh0_page == -1) {
Shreeya Patel63fa37f2018-02-22 22:01:22 +05301031 pr_warn("DiskOnChip %s Media Header not found.\n", id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 return 0;
1033 }
1034 /* Only one mediaheader was found. We want buf to contain a
1035 mediaheader on return, so we'll have to re-read the one we found. */
1036 offs = doc->mh0_page << this->page_shift;
Artem Bityutskiy329ad392011-12-23 17:30:16 +02001037 ret = mtd_read(mtd, offs, mtd->writesize, &retlen, buf);
Joern Engel28318772006-05-22 23:18:05 +02001038 if (retlen != mtd->writesize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 /* Insanity. Give up. */
Shreeya Patel63fa37f2018-02-22 22:01:22 +05301040 pr_err("Read DiskOnChip Media Header once, but can't reread it???\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 return 0;
1042 }
1043 return 1;
1044}
1045
David Woodhousee0c7d762006-05-13 18:07:53 +01001046static inline int __init nftl_partscan(struct mtd_info *mtd, struct mtd_partition *parts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +01001048 struct nand_chip *this = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +01001049 struct doc_priv *doc = nand_get_controller_data(this);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 int ret = 0;
1051 u_char *buf;
1052 struct NFTLMediaHeader *mh;
1053 const unsigned psize = 1 << this->page_shift;
Dan Brown1a78ff62005-03-29 21:57:48 +01001054 int numparts = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 unsigned blocks, maxblocks;
1056 int offs, numheaders;
1057
Joern Engel28318772006-05-22 23:18:05 +02001058 buf = kmalloc(mtd->writesize, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 if (!buf) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 return 0;
1061 }
David Woodhousee0c7d762006-05-13 18:07:53 +01001062 if (!(numheaders = find_media_headers(mtd, buf, "ANAND", 1)))
1063 goto out;
1064 mh = (struct NFTLMediaHeader *)buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065
Harvey Harrison96372442008-07-30 12:34:57 -07001066 le16_to_cpus(&mh->NumEraseUnits);
1067 le16_to_cpus(&mh->FirstPhysicalEUN);
1068 le32_to_cpus(&mh->FormattedSize);
Thomas Gleixnerf29a4b82005-01-31 22:22:24 +00001069
Shreeya Patel63fa37f2018-02-22 22:01:22 +05301070 pr_info(" DataOrgID = %s\n"
1071 " NumEraseUnits = %d\n"
1072 " FirstPhysicalEUN = %d\n"
1073 " FormattedSize = %d\n"
1074 " UnitSizeFactor = %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 mh->DataOrgID, mh->NumEraseUnits,
1076 mh->FirstPhysicalEUN, mh->FormattedSize,
1077 mh->UnitSizeFactor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078
1079 blocks = mtd->size >> this->phys_erase_shift;
1080 maxblocks = min(32768U, mtd->erasesize - psize);
1081
1082 if (mh->UnitSizeFactor == 0x00) {
1083 /* Auto-determine UnitSizeFactor. The constraints are:
1084 - There can be at most 32768 virtual blocks.
1085 - There can be at most (virtual block size - page size)
David Woodhousee0c7d762006-05-13 18:07:53 +01001086 virtual blocks (because MediaHeader+BBT must fit in 1).
1087 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 mh->UnitSizeFactor = 0xff;
1089 while (blocks > maxblocks) {
1090 blocks >>= 1;
1091 maxblocks = min(32768U, (maxblocks << 1) + psize);
1092 mh->UnitSizeFactor--;
1093 }
Shreeya Patel63fa37f2018-02-22 22:01:22 +05301094 pr_warn("UnitSizeFactor=0x00 detected. Correct value is assumed to be 0x%02x.\n", mh->UnitSizeFactor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 }
1096
1097 /* NOTE: The lines below modify internal variables of the NAND and MTD
1098 layers; variables with have already been configured by nand_scan.
1099 Unfortunately, we didn't know before this point what these values
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001100 should be. Thus, this code is somewhat dependent on the exact
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 implementation of the NAND layer. */
1102 if (mh->UnitSizeFactor != 0xff) {
1103 this->bbt_erase_shift += (0xff - mh->UnitSizeFactor);
1104 mtd->erasesize <<= (0xff - mh->UnitSizeFactor);
Shreeya Patel63fa37f2018-02-22 22:01:22 +05301105 pr_info("Setting virtual erase size to %d\n", mtd->erasesize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 blocks = mtd->size >> this->bbt_erase_shift;
1107 maxblocks = min(32768U, mtd->erasesize - psize);
1108 }
1109
1110 if (blocks > maxblocks) {
Shreeya Patel63fa37f2018-02-22 22:01:22 +05301111 pr_err("UnitSizeFactor of 0x%02x is inconsistent with device size. Aborting.\n", mh->UnitSizeFactor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 goto out;
1113 }
1114
1115 /* Skip past the media headers. */
1116 offs = max(doc->mh0_page, doc->mh1_page);
1117 offs <<= this->page_shift;
1118 offs += mtd->erasesize;
1119
Dan Brown1a78ff62005-03-29 21:57:48 +01001120 if (show_firmware_partition == 1) {
1121 parts[0].name = " DiskOnChip Firmware / Media Header partition";
1122 parts[0].offset = 0;
1123 parts[0].size = offs;
1124 numparts = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 }
Dan Brown1a78ff62005-03-29 21:57:48 +01001126
1127 parts[numparts].name = " DiskOnChip BDTL partition";
1128 parts[numparts].offset = offs;
1129 parts[numparts].size = (mh->NumEraseUnits - numheaders) << this->bbt_erase_shift;
1130
1131 offs += parts[numparts].size;
1132 numparts++;
1133
1134 if (offs < mtd->size) {
1135 parts[numparts].name = " DiskOnChip Remainder partition";
1136 parts[numparts].offset = offs;
1137 parts[numparts].size = mtd->size - offs;
1138 numparts++;
1139 }
1140
1141 ret = numparts;
David Woodhousee0c7d762006-05-13 18:07:53 +01001142 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 kfree(buf);
1144 return ret;
1145}
1146
1147/* This is a stripped-down copy of the code in inftlmount.c */
David Woodhousee0c7d762006-05-13 18:07:53 +01001148static inline int __init inftl_partscan(struct mtd_info *mtd, struct mtd_partition *parts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +01001150 struct nand_chip *this = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +01001151 struct doc_priv *doc = nand_get_controller_data(this);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 int ret = 0;
1153 u_char *buf;
1154 struct INFTLMediaHeader *mh;
1155 struct INFTLPartition *ip;
1156 int numparts = 0;
1157 int blocks;
1158 int vshift, lastvunit = 0;
1159 int i;
1160 int end = mtd->size;
1161
1162 if (inftl_bbt_write)
1163 end -= (INFTL_BBT_RESERVED_BLOCKS << this->phys_erase_shift);
1164
Joern Engel28318772006-05-22 23:18:05 +02001165 buf = kmalloc(mtd->writesize, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 if (!buf) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 return 0;
1168 }
1169
David Woodhousee0c7d762006-05-13 18:07:53 +01001170 if (!find_media_headers(mtd, buf, "BNAND", 0))
1171 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 doc->mh1_page = doc->mh0_page + (4096 >> this->page_shift);
David Woodhousee0c7d762006-05-13 18:07:53 +01001173 mh = (struct INFTLMediaHeader *)buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174
Harvey Harrison96372442008-07-30 12:34:57 -07001175 le32_to_cpus(&mh->NoOfBootImageBlocks);
1176 le32_to_cpus(&mh->NoOfBinaryPartitions);
1177 le32_to_cpus(&mh->NoOfBDTLPartitions);
1178 le32_to_cpus(&mh->BlockMultiplierBits);
1179 le32_to_cpus(&mh->FormatFlags);
1180 le32_to_cpus(&mh->PercentUsed);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001181
Shreeya Patel63fa37f2018-02-22 22:01:22 +05301182 pr_info(" bootRecordID = %s\n"
1183 " NoOfBootImageBlocks = %d\n"
1184 " NoOfBinaryPartitions = %d\n"
1185 " NoOfBDTLPartitions = %d\n"
1186 " BlockMultiplerBits = %d\n"
1187 " FormatFlgs = %d\n"
1188 " OsakVersion = %d.%d.%d.%d\n"
1189 " PercentUsed = %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 mh->bootRecordID, mh->NoOfBootImageBlocks,
1191 mh->NoOfBinaryPartitions,
1192 mh->NoOfBDTLPartitions,
1193 mh->BlockMultiplierBits, mh->FormatFlags,
1194 ((unsigned char *) &mh->OsakVersion)[0] & 0xf,
1195 ((unsigned char *) &mh->OsakVersion)[1] & 0xf,
1196 ((unsigned char *) &mh->OsakVersion)[2] & 0xf,
1197 ((unsigned char *) &mh->OsakVersion)[3] & 0xf,
1198 mh->PercentUsed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199
1200 vshift = this->phys_erase_shift + mh->BlockMultiplierBits;
1201
1202 blocks = mtd->size >> vshift;
1203 if (blocks > 32768) {
Shreeya Patel63fa37f2018-02-22 22:01:22 +05301204 pr_err("BlockMultiplierBits=%d is inconsistent with device size. Aborting.\n", mh->BlockMultiplierBits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 goto out;
1206 }
1207
1208 blocks = doc->chips_per_floor << (this->chip_shift - this->phys_erase_shift);
1209 if (inftl_bbt_write && (blocks > mtd->erasesize)) {
Shreeya Patel63fa37f2018-02-22 22:01:22 +05301210 pr_err("Writeable BBTs spanning more than one erase block are not yet supported. FIX ME!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 goto out;
1212 }
1213
1214 /* Scan the partitions */
1215 for (i = 0; (i < 4); i++) {
1216 ip = &(mh->Partitions[i]);
Harvey Harrison96372442008-07-30 12:34:57 -07001217 le32_to_cpus(&ip->virtualUnits);
1218 le32_to_cpus(&ip->firstUnit);
1219 le32_to_cpus(&ip->lastUnit);
1220 le32_to_cpus(&ip->flags);
1221 le32_to_cpus(&ip->spareUnits);
1222 le32_to_cpus(&ip->Reserved0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223
Shreeya Patel63fa37f2018-02-22 22:01:22 +05301224 pr_info(" PARTITION[%d] ->\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 " virtualUnits = %d\n"
1226 " firstUnit = %d\n"
1227 " lastUnit = %d\n"
1228 " flags = 0x%x\n"
1229 " spareUnits = %d\n",
1230 i, ip->virtualUnits, ip->firstUnit,
1231 ip->lastUnit, ip->flags,
1232 ip->spareUnits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233
Dan Brown1a78ff62005-03-29 21:57:48 +01001234 if ((show_firmware_partition == 1) &&
1235 (i == 0) && (ip->firstUnit > 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 parts[0].name = " DiskOnChip IPL / Media Header partition";
1237 parts[0].offset = 0;
1238 parts[0].size = mtd->erasesize * ip->firstUnit;
1239 numparts = 1;
1240 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241
1242 if (ip->flags & INFTL_BINARY)
1243 parts[numparts].name = " DiskOnChip BDK partition";
1244 else
1245 parts[numparts].name = " DiskOnChip BDTL partition";
1246 parts[numparts].offset = ip->firstUnit << vshift;
1247 parts[numparts].size = (1 + ip->lastUnit - ip->firstUnit) << vshift;
1248 numparts++;
David Woodhousee0c7d762006-05-13 18:07:53 +01001249 if (ip->lastUnit > lastvunit)
1250 lastvunit = ip->lastUnit;
1251 if (ip->flags & INFTL_LAST)
1252 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 }
1254 lastvunit++;
1255 if ((lastvunit << vshift) < end) {
1256 parts[numparts].name = " DiskOnChip Remainder partition";
1257 parts[numparts].offset = lastvunit << vshift;
1258 parts[numparts].size = end - parts[numparts].offset;
1259 numparts++;
1260 }
1261 ret = numparts;
David Woodhousee0c7d762006-05-13 18:07:53 +01001262 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 kfree(buf);
1264 return ret;
1265}
1266
1267static int __init nftl_scan_bbt(struct mtd_info *mtd)
1268{
1269 int ret, numparts;
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +01001270 struct nand_chip *this = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +01001271 struct doc_priv *doc = nand_get_controller_data(this);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 struct mtd_partition parts[2];
1273
David Woodhousee0c7d762006-05-13 18:07:53 +01001274 memset((char *)parts, 0, sizeof(parts));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 /* On NFTL, we have to find the media headers before we can read the
1276 BBTs, since they're stored in the media header eraseblocks. */
1277 numparts = nftl_partscan(mtd, parts);
David Woodhousee0c7d762006-05-13 18:07:53 +01001278 if (!numparts)
1279 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 this->bbt_td->options = NAND_BBT_ABSPAGE | NAND_BBT_8BIT |
1281 NAND_BBT_SAVECONTENT | NAND_BBT_WRITE |
1282 NAND_BBT_VERSION;
1283 this->bbt_td->veroffs = 7;
1284 this->bbt_td->pages[0] = doc->mh0_page + 1;
1285 if (doc->mh1_page != -1) {
1286 this->bbt_md->options = NAND_BBT_ABSPAGE | NAND_BBT_8BIT |
1287 NAND_BBT_SAVECONTENT | NAND_BBT_WRITE |
1288 NAND_BBT_VERSION;
1289 this->bbt_md->veroffs = 7;
1290 this->bbt_md->pages[0] = doc->mh1_page + 1;
1291 } else {
1292 this->bbt_md = NULL;
1293 }
1294
Brian Norrisd24fe0c2015-02-28 02:13:10 -08001295 ret = this->scan_bbt(mtd);
1296 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 return ret;
Brian Norrisd24fe0c2015-02-28 02:13:10 -08001298
Brian Norris6a7c7332015-06-01 16:17:17 -07001299 return mtd_device_register(mtd, parts, no_autopart ? 0 : numparts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300}
1301
1302static int __init inftl_scan_bbt(struct mtd_info *mtd)
1303{
1304 int ret, numparts;
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +01001305 struct nand_chip *this = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +01001306 struct doc_priv *doc = nand_get_controller_data(this);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 struct mtd_partition parts[5];
1308
1309 if (this->numchips > doc->chips_per_floor) {
Shreeya Patel63fa37f2018-02-22 22:01:22 +05301310 pr_err("Multi-floor INFTL devices not yet supported.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 return -EIO;
1312 }
1313
1314 if (DoC_is_MillenniumPlus(doc)) {
1315 this->bbt_td->options = NAND_BBT_2BIT | NAND_BBT_ABSPAGE;
1316 if (inftl_bbt_write)
1317 this->bbt_td->options |= NAND_BBT_WRITE;
1318 this->bbt_td->pages[0] = 2;
1319 this->bbt_md = NULL;
1320 } else {
David Woodhousee0c7d762006-05-13 18:07:53 +01001321 this->bbt_td->options = NAND_BBT_LASTBLOCK | NAND_BBT_8BIT | NAND_BBT_VERSION;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 if (inftl_bbt_write)
1323 this->bbt_td->options |= NAND_BBT_WRITE;
1324 this->bbt_td->offs = 8;
1325 this->bbt_td->len = 8;
1326 this->bbt_td->veroffs = 7;
1327 this->bbt_td->maxblocks = INFTL_BBT_RESERVED_BLOCKS;
1328 this->bbt_td->reserved_block_code = 0x01;
1329 this->bbt_td->pattern = "MSYS_BBT";
1330
David Woodhousee0c7d762006-05-13 18:07:53 +01001331 this->bbt_md->options = NAND_BBT_LASTBLOCK | NAND_BBT_8BIT | NAND_BBT_VERSION;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 if (inftl_bbt_write)
1333 this->bbt_md->options |= NAND_BBT_WRITE;
1334 this->bbt_md->offs = 8;
1335 this->bbt_md->len = 8;
1336 this->bbt_md->veroffs = 7;
1337 this->bbt_md->maxblocks = INFTL_BBT_RESERVED_BLOCKS;
1338 this->bbt_md->reserved_block_code = 0x01;
1339 this->bbt_md->pattern = "TBB_SYSM";
1340 }
1341
Brian Norrisd24fe0c2015-02-28 02:13:10 -08001342 ret = this->scan_bbt(mtd);
1343 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 return ret;
Brian Norrisd24fe0c2015-02-28 02:13:10 -08001345
David Woodhousee0c7d762006-05-13 18:07:53 +01001346 memset((char *)parts, 0, sizeof(parts));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 numparts = inftl_partscan(mtd, parts);
1348 /* At least for now, require the INFTL Media Header. We could probably
1349 do without it for non-INFTL use, since all it gives us is
1350 autopartitioning, but I want to give it more thought. */
David Woodhousee0c7d762006-05-13 18:07:53 +01001351 if (!numparts)
1352 return -EIO;
Brian Norris6a7c7332015-06-01 16:17:17 -07001353 return mtd_device_register(mtd, parts, no_autopart ? 0 : numparts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354}
1355
1356static inline int __init doc2000_init(struct mtd_info *mtd)
1357{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +01001358 struct nand_chip *this = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +01001359 struct doc_priv *doc = nand_get_controller_data(this);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 this->read_byte = doc2000_read_byte;
1362 this->write_buf = doc2000_writebuf;
1363 this->read_buf = doc2000_readbuf;
Brian Norrisd24fe0c2015-02-28 02:13:10 -08001364 doc->late_init = nftl_scan_bbt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365
1366 doc->CDSNControl = CDSN_CTRL_FLASH_IO | CDSN_CTRL_ECC_IO;
1367 doc2000_count_chips(mtd);
1368 mtd->name = "DiskOnChip 2000 (NFTL Model)";
1369 return (4 * doc->chips_per_floor);
1370}
1371
1372static inline int __init doc2001_init(struct mtd_info *mtd)
1373{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +01001374 struct nand_chip *this = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +01001375 struct doc_priv *doc = nand_get_controller_data(this);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 this->read_byte = doc2001_read_byte;
1378 this->write_buf = doc2001_writebuf;
1379 this->read_buf = doc2001_readbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380
1381 ReadDOC(doc->virtadr, ChipID);
1382 ReadDOC(doc->virtadr, ChipID);
1383 ReadDOC(doc->virtadr, ChipID);
1384 if (ReadDOC(doc->virtadr, ChipID) != DOC_ChipID_DocMil) {
1385 /* It's not a Millennium; it's one of the newer
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001386 DiskOnChip 2000 units with a similar ASIC.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 Treat it like a Millennium, except that it
1388 can have multiple chips. */
1389 doc2000_count_chips(mtd);
1390 mtd->name = "DiskOnChip 2000 (INFTL Model)";
Brian Norrisd24fe0c2015-02-28 02:13:10 -08001391 doc->late_init = inftl_scan_bbt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 return (4 * doc->chips_per_floor);
1393 } else {
1394 /* Bog-standard Millennium */
1395 doc->chips_per_floor = 1;
1396 mtd->name = "DiskOnChip Millennium";
Brian Norrisd24fe0c2015-02-28 02:13:10 -08001397 doc->late_init = nftl_scan_bbt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 return 1;
1399 }
1400}
1401
1402static inline int __init doc2001plus_init(struct mtd_info *mtd)
1403{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +01001404 struct nand_chip *this = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +01001405 struct doc_priv *doc = nand_get_controller_data(this);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 this->read_byte = doc2001plus_read_byte;
1408 this->write_buf = doc2001plus_writebuf;
1409 this->read_buf = doc2001plus_readbuf;
Brian Norrisd24fe0c2015-02-28 02:13:10 -08001410 doc->late_init = inftl_scan_bbt;
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +02001411 this->cmd_ctrl = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 this->select_chip = doc2001plus_select_chip;
1413 this->cmdfunc = doc2001plus_command;
Thomas Gleixner0cddd6c2006-05-23 15:59:58 +02001414 this->ecc.hwctl = doc2001plus_enable_hwecc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415
1416 doc->chips_per_floor = 1;
1417 mtd->name = "DiskOnChip Millennium Plus";
1418
1419 return 1;
1420}
1421
Arjan van de Ven858119e2006-01-14 13:20:43 -08001422static int __init doc_probe(unsigned long physadr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423{
1424 unsigned char ChipID;
1425 struct mtd_info *mtd;
1426 struct nand_chip *nand;
1427 struct doc_priv *doc;
1428 void __iomem *virtadr;
1429 unsigned char save_control;
1430 unsigned char tmp, tmpb, tmpc;
1431 int reg, len, numchips;
1432 int ret = 0;
1433
Sasha Levin86e4bbc2014-03-19 18:24:37 -04001434 if (!request_mem_region(physadr, DOC_IOREMAP_LEN, "DiskOnChip"))
Alexander Shiyan4f0614a2013-11-13 15:59:25 +04001435 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 virtadr = ioremap(physadr, DOC_IOREMAP_LEN);
1437 if (!virtadr) {
Shreeya Patel63fa37f2018-02-22 22:01:22 +05301438 pr_err("Diskonchip ioremap failed: 0x%x bytes at 0x%lx\n",
1439 DOC_IOREMAP_LEN, physadr);
Alexander Shiyan4f0614a2013-11-13 15:59:25 +04001440 ret = -EIO;
1441 goto error_ioremap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 }
1443
1444 /* It's not possible to cleanly detect the DiskOnChip - the
1445 * bootup procedure will put the device into reset mode, and
1446 * it's not possible to talk to it without actually writing
1447 * to the DOCControl register. So we store the current contents
1448 * of the DOCControl register's location, in case we later decide
1449 * that it's not a DiskOnChip, and want to put it back how we
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001450 * found it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 */
1452 save_control = ReadDOC(virtadr, DOCControl);
1453
1454 /* Reset the DiskOnChip ASIC */
David Woodhousee0c7d762006-05-13 18:07:53 +01001455 WriteDOC(DOC_MODE_CLR_ERR | DOC_MODE_MDWREN | DOC_MODE_RESET, virtadr, DOCControl);
1456 WriteDOC(DOC_MODE_CLR_ERR | DOC_MODE_MDWREN | DOC_MODE_RESET, virtadr, DOCControl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457
1458 /* Enable the DiskOnChip ASIC */
David Woodhousee0c7d762006-05-13 18:07:53 +01001459 WriteDOC(DOC_MODE_CLR_ERR | DOC_MODE_MDWREN | DOC_MODE_NORMAL, virtadr, DOCControl);
1460 WriteDOC(DOC_MODE_CLR_ERR | DOC_MODE_MDWREN | DOC_MODE_NORMAL, virtadr, DOCControl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461
1462 ChipID = ReadDOC(virtadr, ChipID);
1463
David Woodhousee0c7d762006-05-13 18:07:53 +01001464 switch (ChipID) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 case DOC_ChipID_Doc2k:
1466 reg = DoC_2k_ECCStatus;
1467 break;
1468 case DOC_ChipID_DocMil:
1469 reg = DoC_ECCConf;
1470 break;
1471 case DOC_ChipID_DocMilPlus16:
1472 case DOC_ChipID_DocMilPlus32:
1473 case 0:
1474 /* Possible Millennium Plus, need to do more checks */
1475 /* Possibly release from power down mode */
1476 for (tmp = 0; (tmp < 4); tmp++)
1477 ReadDOC(virtadr, Mplus_Power);
1478
1479 /* Reset the Millennium Plus ASIC */
David Woodhousee0c7d762006-05-13 18:07:53 +01001480 tmp = DOC_MODE_RESET | DOC_MODE_MDWREN | DOC_MODE_RST_LAT | DOC_MODE_BDECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 WriteDOC(tmp, virtadr, Mplus_DOCControl);
1482 WriteDOC(~tmp, virtadr, Mplus_CtrlConfirm);
1483
Jia-Ju Bai7b4b1992018-04-11 10:57:57 +08001484 usleep_range(1000, 2000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 /* Enable the Millennium Plus ASIC */
David Woodhousee0c7d762006-05-13 18:07:53 +01001486 tmp = DOC_MODE_NORMAL | DOC_MODE_MDWREN | DOC_MODE_RST_LAT | DOC_MODE_BDECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 WriteDOC(tmp, virtadr, Mplus_DOCControl);
1488 WriteDOC(~tmp, virtadr, Mplus_CtrlConfirm);
Jia-Ju Bai7b4b1992018-04-11 10:57:57 +08001489 usleep_range(1000, 2000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490
1491 ChipID = ReadDOC(virtadr, ChipID);
1492
1493 switch (ChipID) {
1494 case DOC_ChipID_DocMilPlus16:
1495 reg = DoC_Mplus_Toggle;
1496 break;
1497 case DOC_ChipID_DocMilPlus32:
Shreeya Patel63fa37f2018-02-22 22:01:22 +05301498 pr_err("DiskOnChip Millennium Plus 32MB is not supported, ignoring.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 default:
1500 ret = -ENODEV;
1501 goto notfound;
1502 }
1503 break;
1504
1505 default:
1506 ret = -ENODEV;
1507 goto notfound;
1508 }
1509 /* Check the TOGGLE bit in the ECC register */
David Woodhousee0c7d762006-05-13 18:07:53 +01001510 tmp = ReadDOC_(virtadr, reg) & DOC_TOGGLE_BIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 tmpb = ReadDOC_(virtadr, reg) & DOC_TOGGLE_BIT;
1512 tmpc = ReadDOC_(virtadr, reg) & DOC_TOGGLE_BIT;
1513 if ((tmp == tmpb) || (tmp != tmpc)) {
Shreeya Patel63fa37f2018-02-22 22:01:22 +05301514 pr_warn("Possible DiskOnChip at 0x%lx failed TOGGLE test, dropping.\n", physadr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 ret = -ENODEV;
1516 goto notfound;
1517 }
1518
1519 for (mtd = doclist; mtd; mtd = doc->nextdoc) {
1520 unsigned char oldval;
1521 unsigned char newval;
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +01001522 nand = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +01001523 doc = nand_get_controller_data(nand);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 /* Use the alias resolution register to determine if this is
1525 in fact the same DOC aliased to a new address. If writes
1526 to one chip's alias resolution register change the value on
1527 the other chip, they're the same chip. */
1528 if (ChipID == DOC_ChipID_DocMilPlus16) {
1529 oldval = ReadDOC(doc->virtadr, Mplus_AliasResolution);
1530 newval = ReadDOC(virtadr, Mplus_AliasResolution);
1531 } else {
1532 oldval = ReadDOC(doc->virtadr, AliasResolution);
1533 newval = ReadDOC(virtadr, AliasResolution);
1534 }
1535 if (oldval != newval)
1536 continue;
1537 if (ChipID == DOC_ChipID_DocMilPlus16) {
1538 WriteDOC(~newval, virtadr, Mplus_AliasResolution);
1539 oldval = ReadDOC(doc->virtadr, Mplus_AliasResolution);
David Woodhousee0c7d762006-05-13 18:07:53 +01001540 WriteDOC(newval, virtadr, Mplus_AliasResolution); // restore it
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 } else {
1542 WriteDOC(~newval, virtadr, AliasResolution);
1543 oldval = ReadDOC(doc->virtadr, AliasResolution);
David Woodhousee0c7d762006-05-13 18:07:53 +01001544 WriteDOC(newval, virtadr, AliasResolution); // restore it
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 }
1546 newval = ~newval;
1547 if (oldval == newval) {
Shreeya Patel63fa37f2018-02-22 22:01:22 +05301548 pr_debug("Found alias of DOC at 0x%lx to 0x%lx\n",
1549 doc->physadr, physadr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 goto notfound;
1551 }
1552 }
1553
Shreeya Patel63fa37f2018-02-22 22:01:22 +05301554 pr_notice("DiskOnChip found at 0x%lx\n", physadr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555
Boris BREZILLONb0c423c2015-12-10 09:00:00 +01001556 len = sizeof(struct nand_chip) + sizeof(struct doc_priv) +
1557 (2 * sizeof(struct nand_bbt_descr));
1558 nand = kzalloc(len, GFP_KERNEL);
1559 if (!nand) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 ret = -ENOMEM;
1561 goto fail;
1562 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563
Boris BREZILLONb0c423c2015-12-10 09:00:00 +01001564 mtd = nand_to_mtd(nand);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 doc = (struct doc_priv *) (nand + 1);
1566 nand->bbt_td = (struct nand_bbt_descr *) (doc + 1);
1567 nand->bbt_md = nand->bbt_td + 1;
1568
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 mtd->owner = THIS_MODULE;
Boris Brezillon68c1b752016-02-03 20:00:23 +01001570 mtd_set_ooblayout(mtd, &doc200x_ooblayout_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571
Boris BREZILLONd699ed22015-12-10 09:00:41 +01001572 nand_set_controller_data(nand, doc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 nand->select_chip = doc200x_select_chip;
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +02001574 nand->cmd_ctrl = doc200x_hwcontrol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 nand->dev_ready = doc200x_dev_ready;
1576 nand->waitfunc = doc200x_wait;
1577 nand->block_bad = doc200x_block_bad;
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02001578 nand->ecc.hwctl = doc200x_enable_hwecc;
1579 nand->ecc.calculate = doc200x_calculate_ecc;
1580 nand->ecc.correct = doc200x_correct_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02001582 nand->ecc.mode = NAND_ECC_HW_SYNDROME;
1583 nand->ecc.size = 512;
1584 nand->ecc.bytes = 6;
Mike Dunn6a918ba2012-03-11 14:21:11 -07001585 nand->ecc.strength = 2;
Boris BREZILLONcc01e602015-12-30 20:39:52 +01001586 nand->ecc.options = NAND_ECC_GENERIC_ERASED_CHECK;
Brian Norrisbb9ebd42011-05-31 16:31:23 -07001587 nand->bbt_options = NAND_BBT_USE_FLASH;
Brian Norrisd24fe0c2015-02-28 02:13:10 -08001588 /* Skip the automatic BBT scan so we can run it manually */
1589 nand->options |= NAND_SKIP_BBTSCAN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590
1591 doc->physadr = physadr;
1592 doc->virtadr = virtadr;
1593 doc->ChipID = ChipID;
1594 doc->curfloor = -1;
1595 doc->curchip = -1;
1596 doc->mh0_page = -1;
1597 doc->mh1_page = -1;
1598 doc->nextdoc = doclist;
1599
1600 if (ChipID == DOC_ChipID_Doc2k)
1601 numchips = doc2000_init(mtd);
1602 else if (ChipID == DOC_ChipID_DocMilPlus16)
1603 numchips = doc2001plus_init(mtd);
1604 else
1605 numchips = doc2001_init(mtd);
1606
Brian Norrisd24fe0c2015-02-28 02:13:10 -08001607 if ((ret = nand_scan(mtd, numchips)) || (ret = doc->late_init(mtd))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 /* DBB note: i believe nand_release is necessary here, as
1609 buffers may have been allocated in nand_base. Check with
1610 Thomas. FIX ME! */
Jamie Iles0f47e952011-05-23 10:23:19 +01001611 /* nand_release will call mtd_device_unregister, but we
1612 haven't yet added it. This is handled without incident by
1613 mtd_device_unregister, as far as I can tell. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 nand_release(mtd);
Boris BREZILLONb0c423c2015-12-10 09:00:00 +01001615 kfree(nand);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 goto fail;
1617 }
1618
1619 /* Success! */
1620 doclist = mtd;
1621 return 0;
1622
David Woodhousee0c7d762006-05-13 18:07:53 +01001623 notfound:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 /* Put back the contents of the DOCControl register, in case it's not
1625 actually a DiskOnChip. */
1626 WriteDOC(save_control, virtadr, DOCControl);
David Woodhousee0c7d762006-05-13 18:07:53 +01001627 fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 iounmap(virtadr);
Alexander Shiyan4f0614a2013-11-13 15:59:25 +04001629
1630error_ioremap:
1631 release_mem_region(physadr, DOC_IOREMAP_LEN);
1632
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 return ret;
1634}
1635
1636static void release_nanddoc(void)
1637{
David Woodhousee0c7d762006-05-13 18:07:53 +01001638 struct mtd_info *mtd, *nextmtd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 struct nand_chip *nand;
1640 struct doc_priv *doc;
1641
1642 for (mtd = doclist; mtd; mtd = nextmtd) {
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +01001643 nand = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +01001644 doc = nand_get_controller_data(nand);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645
1646 nextmtd = doc->nextdoc;
1647 nand_release(mtd);
1648 iounmap(doc->virtadr);
Alexander Shiyan4f0614a2013-11-13 15:59:25 +04001649 release_mem_region(doc->physadr, DOC_IOREMAP_LEN);
Boris BREZILLONb0c423c2015-12-10 09:00:00 +01001650 kfree(nand);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 }
1652}
1653
1654static int __init init_nanddoc(void)
1655{
1656 int i, ret = 0;
1657
1658 /* We could create the decoder on demand, if memory is a concern.
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001659 * This way we have it handy, if an error happens
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 *
1661 * Symbolsize is 10 (bits)
1662 * Primitve polynomial is x^10+x^3+1
1663 * first consecutive root is 510
1664 * primitve element to generate roots = 1
1665 * generator polinomial degree = 4
1666 */
1667 rs_decoder = init_rs(10, 0x409, FCR, 1, NROOTS);
David Woodhousee0c7d762006-05-13 18:07:53 +01001668 if (!rs_decoder) {
Shreeya Patel63fa37f2018-02-22 22:01:22 +05301669 pr_err("DiskOnChip: Could not create a RS decoder\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 return -ENOMEM;
1671 }
1672
1673 if (doc_config_location) {
Shreeya Patel63fa37f2018-02-22 22:01:22 +05301674 pr_info("Using configured DiskOnChip probe address 0x%lx\n",
1675 doc_config_location);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 ret = doc_probe(doc_config_location);
1677 if (ret < 0)
1678 goto outerr;
1679 } else {
David Woodhousee0c7d762006-05-13 18:07:53 +01001680 for (i = 0; (doc_locations[i] != 0xffffffff); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 doc_probe(doc_locations[i]);
1682 }
1683 }
1684 /* No banner message any more. Print a message if no DiskOnChip
1685 found, so the user knows we at least tried. */
1686 if (!doclist) {
Shreeya Patel63fa37f2018-02-22 22:01:22 +05301687 pr_info("No valid DiskOnChip devices found\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 ret = -ENODEV;
1689 goto outerr;
1690 }
1691 return 0;
David Woodhousee0c7d762006-05-13 18:07:53 +01001692 outerr:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 free_rs(rs_decoder);
1694 return ret;
1695}
1696
1697static void __exit cleanup_nanddoc(void)
1698{
1699 /* Cleanup the nand/DoC resources */
1700 release_nanddoc();
1701
1702 /* Free the reed solomon resources */
1703 if (rs_decoder) {
1704 free_rs(rs_decoder);
1705 }
1706}
1707
1708module_init(init_nanddoc);
1709module_exit(cleanup_nanddoc);
1710
1711MODULE_LICENSE("GPL");
1712MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
Niels de Vos2a7af8c2009-01-30 11:08:35 +01001713MODULE_DESCRIPTION("M-Systems DiskOnChip 2000, Millennium and Millennium Plus device driver");