blob: ec3336f071e7e11156ce0cfd3593a1552630bedc [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;
Thomas Gleixner964dfce2018-04-22 18:23:54 +020069 struct rs_control *rs_decoder;
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 struct mtd_info *nextdoc;
Brian Norrisd24fe0c2015-02-28 02:13:10 -080071
72 /* Handle the last stage of initialization (BBT scan, partitioning) */
73 int (*late_init)(struct mtd_info *mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -070074};
75
Linus Torvalds1da177e2005-04-16 15:20:36 -070076/* This is the ecc value computed by the HW ecc generator upon writing an empty
77 page, one with all 0xff for data. */
78static u_char empty_write_ecc[6] = { 0x4b, 0x00, 0xe2, 0x0e, 0x93, 0xf7 };
79
80#define INFTL_BBT_RESERVED_BLOCKS 4
81
82#define DoC_is_MillenniumPlus(doc) ((doc)->ChipID == DOC_ChipID_DocMilPlus16 || (doc)->ChipID == DOC_ChipID_DocMilPlus32)
83#define DoC_is_Millennium(doc) ((doc)->ChipID == DOC_ChipID_DocMil)
84#define DoC_is_2000(doc) ((doc)->ChipID == DOC_ChipID_Doc2k)
85
Boris Brezillon0f808c12018-09-06 14:05:26 +020086static void doc200x_hwcontrol(struct nand_chip *this, int cmd,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +020087 unsigned int bitmask);
Boris Brezillon758b56f2018-09-06 14:05:24 +020088static void doc200x_select_chip(struct nand_chip *this, int chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
David Woodhousee0c7d762006-05-13 18:07:53 +010090static int debug = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091module_param(debug, int, 0);
92
David Woodhousee0c7d762006-05-13 18:07:53 +010093static int try_dword = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070094module_param(try_dword, int, 0);
95
David Woodhousee0c7d762006-05-13 18:07:53 +010096static int no_ecc_failures = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097module_param(no_ecc_failures, int, 0);
98
David Woodhousee0c7d762006-05-13 18:07:53 +010099static int no_autopart = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100module_param(no_autopart, int, 0);
Dan Brown1a78ff62005-03-29 21:57:48 +0100101
David Woodhousee0c7d762006-05-13 18:07:53 +0100102static int show_firmware_partition = 0;
Dan Brown1a78ff62005-03-29 21:57:48 +0100103module_param(show_firmware_partition, int, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
Robert P. J. Day89e2bf62007-03-07 18:33:25 -0500105#ifdef CONFIG_MTD_NAND_DISKONCHIP_BBTWRITE
David Woodhousee0c7d762006-05-13 18:07:53 +0100106static int inftl_bbt_write = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107#else
David Woodhousee0c7d762006-05-13 18:07:53 +0100108static int inftl_bbt_write = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109#endif
110module_param(inftl_bbt_write, int, 0);
111
Thomas Gleixner651078b2005-01-31 20:36:46 +0000112static unsigned long doc_config_location = CONFIG_MTD_NAND_DISKONCHIP_PROBE_ADDRESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113module_param(doc_config_location, ulong, 0);
114MODULE_PARM_DESC(doc_config_location, "Physical memory address at which to probe for DiskOnChip");
115
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116/* Sector size for HW ECC */
117#define SECTOR_SIZE 512
118/* The sector bytes are packed into NB_DATA 10 bit words */
119#define NB_DATA (((SECTOR_SIZE + 1) * 8 + 6) / 10)
120/* Number of roots */
121#define NROOTS 4
122/* First consective root */
123#define FCR 510
124/* Number of symbols */
125#define NN 1023
126
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000127/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 * The HW decoder in the DoC ASIC's provides us a error syndrome,
Brian Norris7854d3f2011-06-23 14:12:08 -0700129 * which we must convert to a standard syndrome usable by the generic
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 * Reed-Solomon library code.
131 *
132 * Fabrice Bellard figured this out in the old docecc code. I added
133 * some comments, improved a minor bit and converted it to make use
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300134 * of the generic Reed-Solomon library. tglx
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100136static int doc_ecc_decode(struct rs_control *rs, uint8_t *data, uint8_t *ecc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137{
138 int i, j, nerr, errpos[8];
139 uint8_t parity;
140 uint16_t ds[4], s[5], tmp, errval[8], syn[4];
Thomas Gleixner21633982018-04-22 18:23:53 +0200141 struct rs_codec *cd = rs->codec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
Mark Warec9fb6772010-05-27 11:45:39 +1000143 memset(syn, 0, sizeof(syn));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 /* Convert the ecc bytes into words */
145 ds[0] = ((ecc[4] & 0xff) >> 0) | ((ecc[5] & 0x03) << 8);
146 ds[1] = ((ecc[5] & 0xfc) >> 2) | ((ecc[2] & 0x0f) << 6);
147 ds[2] = ((ecc[2] & 0xf0) >> 4) | ((ecc[3] & 0x3f) << 4);
148 ds[3] = ((ecc[3] & 0xc0) >> 6) | ((ecc[0] & 0xff) << 2);
149 parity = ecc[1];
150
Brian Norris7854d3f2011-06-23 14:12:08 -0700151 /* Initialize the syndrome buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 for (i = 0; i < NROOTS; i++)
153 s[i] = ds[0];
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000154 /*
155 * Evaluate
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 * s[i] = ds[3]x^3 + ds[2]x^2 + ds[1]x^1 + ds[0]
157 * where x = alpha^(FCR + i)
158 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100159 for (j = 1; j < NROOTS; j++) {
160 if (ds[j] == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 continue;
Thomas Gleixner21633982018-04-22 18:23:53 +0200162 tmp = cd->index_of[ds[j]];
David Woodhousee0c7d762006-05-13 18:07:53 +0100163 for (i = 0; i < NROOTS; i++)
Thomas Gleixner21633982018-04-22 18:23:53 +0200164 s[i] ^= cd->alpha_to[rs_modnn(cd, tmp + (FCR + i) * j)];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 }
166
Mark Warec9fb6772010-05-27 11:45:39 +1000167 /* Calc syn[i] = s[i] / alpha^(v + i) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 for (i = 0; i < NROOTS; i++) {
Mark Warec9fb6772010-05-27 11:45:39 +1000169 if (s[i])
Thomas Gleixner21633982018-04-22 18:23:53 +0200170 syn[i] = rs_modnn(cd, cd->index_of[s[i]] + (NN - FCR - i));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 }
172 /* Call the decoder library */
173 nerr = decode_rs16(rs, NULL, NULL, 1019, syn, 0, errpos, 0, errval);
174
175 /* Incorrectable errors ? */
176 if (nerr < 0)
177 return nerr;
178
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000179 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 * Correct the errors. The bitpositions are a bit of magic,
181 * but they are given by the design of the de/encoder circuit
182 * in the DoC ASIC's.
183 */
David Woodhousee0c7d762006-05-13 18:07:53 +0100184 for (i = 0; i < nerr; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 int index, bitpos, pos = 1015 - errpos[i];
186 uint8_t val;
187 if (pos >= NB_DATA && pos < 1019)
188 continue;
189 if (pos < NB_DATA) {
190 /* extract bit position (MSB first) */
191 pos = 10 * (NB_DATA - 1 - pos) - 6;
192 /* now correct the following 10 bits. At most two bytes
193 can be modified since pos is even */
194 index = (pos >> 3) ^ 1;
195 bitpos = pos & 7;
David Woodhousee0c7d762006-05-13 18:07:53 +0100196 if ((index >= 0 && index < SECTOR_SIZE) || index == (SECTOR_SIZE + 1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 val = (uint8_t) (errval[i] >> (2 + bitpos));
198 parity ^= val;
199 if (index < SECTOR_SIZE)
200 data[index] ^= val;
201 }
202 index = ((pos >> 3) + 1) ^ 1;
203 bitpos = (bitpos + 10) & 7;
204 if (bitpos == 0)
205 bitpos = 8;
David Woodhousee0c7d762006-05-13 18:07:53 +0100206 if ((index >= 0 && index < SECTOR_SIZE) || index == (SECTOR_SIZE + 1)) {
207 val = (uint8_t) (errval[i] << (8 - bitpos));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 parity ^= val;
209 if (index < SECTOR_SIZE)
210 data[index] ^= val;
211 }
212 }
213 }
214 /* If the parity is wrong, no rescue possible */
Jörn Engeleb684502007-10-20 23:16:32 +0200215 return parity ? -EBADMSG : nerr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216}
217
218static void DoC_Delay(struct doc_priv *doc, unsigned short cycles)
219{
220 volatile char dummy;
221 int i;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000222
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 for (i = 0; i < cycles; i++) {
224 if (DoC_is_Millennium(doc))
225 dummy = ReadDOC(doc->virtadr, NOP);
226 else if (DoC_is_MillenniumPlus(doc))
227 dummy = ReadDOC(doc->virtadr, Mplus_NOP);
228 else
229 dummy = ReadDOC(doc->virtadr, DOCStatus);
230 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000231
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232}
233
234#define CDSN_CTRL_FR_B_MASK (CDSN_CTRL_FR_B0 | CDSN_CTRL_FR_B1)
235
236/* DOC_WaitReady: Wait for RDY line to be asserted by the flash chip */
237static int _DoC_WaitReady(struct doc_priv *doc)
238{
David Woodhousee0c7d762006-05-13 18:07:53 +0100239 void __iomem *docptr = doc->virtadr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 unsigned long timeo = jiffies + (HZ * 10);
241
David Woodhousee0c7d762006-05-13 18:07:53 +0100242 if (debug)
243 printk("_DoC_WaitReady...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 /* Out-of-line routine to wait for chip response */
245 if (DoC_is_MillenniumPlus(doc)) {
246 while ((ReadDOC(docptr, Mplus_FlashControl) & CDSN_CTRL_FR_B_MASK) != CDSN_CTRL_FR_B_MASK) {
247 if (time_after(jiffies, timeo)) {
248 printk("_DoC_WaitReady timed out.\n");
249 return -EIO;
250 }
251 udelay(1);
252 cond_resched();
253 }
254 } else {
255 while (!(ReadDOC(docptr, CDSNControl) & CDSN_CTRL_FR_B)) {
256 if (time_after(jiffies, timeo)) {
257 printk("_DoC_WaitReady timed out.\n");
258 return -EIO;
259 }
260 udelay(1);
261 cond_resched();
262 }
263 }
264
265 return 0;
266}
267
268static inline int DoC_WaitReady(struct doc_priv *doc)
269{
David Woodhousee0c7d762006-05-13 18:07:53 +0100270 void __iomem *docptr = doc->virtadr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 int ret = 0;
272
273 if (DoC_is_MillenniumPlus(doc)) {
274 DoC_Delay(doc, 4);
275
276 if ((ReadDOC(docptr, Mplus_FlashControl) & CDSN_CTRL_FR_B_MASK) != CDSN_CTRL_FR_B_MASK)
277 /* Call the out-of-line routine to wait */
278 ret = _DoC_WaitReady(doc);
279 } else {
280 DoC_Delay(doc, 4);
281
282 if (!(ReadDOC(docptr, CDSNControl) & CDSN_CTRL_FR_B))
283 /* Call the out-of-line routine to wait */
284 ret = _DoC_WaitReady(doc);
285 DoC_Delay(doc, 2);
286 }
287
David Woodhousee0c7d762006-05-13 18:07:53 +0100288 if (debug)
289 printk("DoC_WaitReady OK\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 return ret;
291}
292
Boris Brezillonc0739d82018-09-06 14:05:23 +0200293static void doc2000_write_byte(struct nand_chip *this, u_char datum)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294{
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100295 struct doc_priv *doc = nand_get_controller_data(this);
David Woodhousee0c7d762006-05-13 18:07:53 +0100296 void __iomem *docptr = doc->virtadr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297
David Woodhousee0c7d762006-05-13 18:07:53 +0100298 if (debug)
299 printk("write_byte %02x\n", datum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 WriteDOC(datum, docptr, CDSNSlowIO);
301 WriteDOC(datum, docptr, 2k_CDSN_IO);
302}
303
Boris Brezillon7e534322018-09-06 14:05:22 +0200304static u_char doc2000_read_byte(struct nand_chip *this)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305{
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100306 struct doc_priv *doc = nand_get_controller_data(this);
David Woodhousee0c7d762006-05-13 18:07:53 +0100307 void __iomem *docptr = doc->virtadr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 u_char ret;
309
310 ReadDOC(docptr, CDSNSlowIO);
311 DoC_Delay(doc, 2);
312 ret = ReadDOC(docptr, 2k_CDSN_IO);
David Woodhousee0c7d762006-05-13 18:07:53 +0100313 if (debug)
314 printk("read_byte returns %02x\n", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 return ret;
316}
317
Boris Brezillonc0739d82018-09-06 14:05:23 +0200318static void doc2000_writebuf(struct nand_chip *this, const u_char *buf,
319 int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320{
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100321 struct doc_priv *doc = nand_get_controller_data(this);
David Woodhousee0c7d762006-05-13 18:07:53 +0100322 void __iomem *docptr = doc->virtadr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 int i;
David Woodhousee0c7d762006-05-13 18:07:53 +0100324 if (debug)
325 printk("writebuf of %d bytes: ", len);
326 for (i = 0; i < len; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 WriteDOC_(buf[i], docptr, DoC_2k_CDSN_IO + i);
328 if (debug && i < 16)
329 printk("%02x ", buf[i]);
330 }
David Woodhousee0c7d762006-05-13 18:07:53 +0100331 if (debug)
332 printk("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333}
334
Boris Brezillon7e534322018-09-06 14:05:22 +0200335static void doc2000_readbuf(struct nand_chip *this, u_char *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336{
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100337 struct doc_priv *doc = nand_get_controller_data(this);
David Woodhousee0c7d762006-05-13 18:07:53 +0100338 void __iomem *docptr = doc->virtadr;
339 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
David Woodhousee0c7d762006-05-13 18:07:53 +0100341 if (debug)
342 printk("readbuf of %d bytes: ", len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
Boris Brezillon7e534322018-09-06 14:05:22 +0200344 for (i = 0; i < len; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 buf[i] = ReadDOC(docptr, 2k_CDSN_IO + i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346}
347
Boris Brezillon7e534322018-09-06 14:05:22 +0200348static void doc2000_readbuf_dword(struct nand_chip *this, u_char *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349{
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100350 struct doc_priv *doc = nand_get_controller_data(this);
David Woodhousee0c7d762006-05-13 18:07:53 +0100351 void __iomem *docptr = doc->virtadr;
352 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
David Woodhousee0c7d762006-05-13 18:07:53 +0100354 if (debug)
355 printk("readbuf_dword of %d bytes: ", len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
David Woodhousee0c7d762006-05-13 18:07:53 +0100357 if (unlikely((((unsigned long)buf) | len) & 3)) {
358 for (i = 0; i < len; i++) {
359 *(uint8_t *) (&buf[i]) = ReadDOC(docptr, 2k_CDSN_IO + i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 }
361 } else {
David Woodhousee0c7d762006-05-13 18:07:53 +0100362 for (i = 0; i < len; i += 4) {
363 *(uint32_t *) (&buf[i]) = readl(docptr + DoC_2k_CDSN_IO + i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 }
365 }
366}
367
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368static uint16_t __init doc200x_ident_chip(struct mtd_info *mtd, int nr)
369{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100370 struct nand_chip *this = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100371 struct doc_priv *doc = nand_get_controller_data(this);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 uint16_t ret;
373
Boris Brezillon758b56f2018-09-06 14:05:24 +0200374 doc200x_select_chip(this, nr);
Boris Brezillon0f808c12018-09-06 14:05:26 +0200375 doc200x_hwcontrol(this, NAND_CMD_READID,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200376 NAND_CTRL_CLE | NAND_CTRL_CHANGE);
Boris Brezillon0f808c12018-09-06 14:05:26 +0200377 doc200x_hwcontrol(this, 0, NAND_CTRL_ALE | NAND_CTRL_CHANGE);
378 doc200x_hwcontrol(this, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000379
Lucas De Marchie9c54992011-04-26 23:28:26 -0700380 /* We can't use dev_ready here, but at least we wait for the
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000381 * command to complete
Thomas Gleixnerdfd61292005-02-22 21:48:25 +0000382 */
383 udelay(50);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000384
Boris Brezillon716bbba2018-09-07 00:38:35 +0200385 ret = this->legacy.read_byte(this) << 8;
386 ret |= this->legacy.read_byte(this);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
388 if (doc->ChipID == DOC_ChipID_Doc2k && try_dword && !nr) {
389 /* First chip probe. See if we get same results by 32-bit access */
390 union {
391 uint32_t dword;
392 uint8_t byte[4];
393 } ident;
394 void __iomem *docptr = doc->virtadr;
395
Boris Brezillon0f808c12018-09-06 14:05:26 +0200396 doc200x_hwcontrol(this, NAND_CMD_READID,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200397 NAND_CTRL_CLE | NAND_CTRL_CHANGE);
Boris Brezillon0f808c12018-09-06 14:05:26 +0200398 doc200x_hwcontrol(this, 0, NAND_CTRL_ALE | NAND_CTRL_CHANGE);
399 doc200x_hwcontrol(this, NAND_CMD_NONE,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200400 NAND_NCE | NAND_CTRL_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401
Thomas Gleixnerdfd61292005-02-22 21:48:25 +0000402 udelay(50);
403
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 ident.dword = readl(docptr + DoC_2k_CDSN_IO);
405 if (((ident.byte[0] << 8) | ident.byte[1]) == ret) {
Shreeya Patel63fa37f2018-02-22 22:01:22 +0530406 pr_info("DiskOnChip 2000 responds to DWORD access\n");
Boris Brezillon716bbba2018-09-07 00:38:35 +0200407 this->legacy.read_buf = &doc2000_readbuf_dword;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 }
409 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000410
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 return ret;
412}
413
414static void __init doc2000_count_chips(struct mtd_info *mtd)
415{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100416 struct nand_chip *this = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100417 struct doc_priv *doc = nand_get_controller_data(this);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 uint16_t mfrid;
419 int i;
420
421 /* Max 4 chips per floor on DiskOnChip 2000 */
422 doc->chips_per_floor = 4;
423
424 /* Find out what the first chip is */
425 mfrid = doc200x_ident_chip(mtd, 0);
426
427 /* Find how many chips in each floor. */
428 for (i = 1; i < 4; i++) {
429 if (doc200x_ident_chip(mtd, i) != mfrid)
430 break;
431 }
432 doc->chips_per_floor = i;
Shreeya Patel63fa37f2018-02-22 22:01:22 +0530433 pr_debug("Detected %d chips per floor.\n", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434}
435
Boris Brezillonf1d46942018-09-06 14:05:29 +0200436static int doc200x_wait(struct nand_chip *this)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437{
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100438 struct doc_priv *doc = nand_get_controller_data(this);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
440 int status;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000441
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 DoC_WaitReady(doc);
Boris Brezillon97d90da2017-11-30 18:01:29 +0100443 nand_status_op(this, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 DoC_WaitReady(doc);
Boris Brezillon716bbba2018-09-07 00:38:35 +0200445 status = (int)this->legacy.read_byte(this);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
447 return status;
448}
449
Boris Brezillonc0739d82018-09-06 14:05:23 +0200450static void doc2001_write_byte(struct nand_chip *this, u_char datum)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451{
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100452 struct doc_priv *doc = nand_get_controller_data(this);
David Woodhousee0c7d762006-05-13 18:07:53 +0100453 void __iomem *docptr = doc->virtadr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454
455 WriteDOC(datum, docptr, CDSNSlowIO);
456 WriteDOC(datum, docptr, Mil_CDSN_IO);
457 WriteDOC(datum, docptr, WritePipeTerm);
458}
459
Boris Brezillon7e534322018-09-06 14:05:22 +0200460static u_char doc2001_read_byte(struct nand_chip *this)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461{
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100462 struct doc_priv *doc = nand_get_controller_data(this);
David Woodhousee0c7d762006-05-13 18:07:53 +0100463 void __iomem *docptr = doc->virtadr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464
465 //ReadDOC(docptr, CDSNSlowIO);
466 /* 11.4.5 -- delay twice to allow extended length cycle */
467 DoC_Delay(doc, 2);
468 ReadDOC(docptr, ReadPipeInit);
469 //return ReadDOC(docptr, Mil_CDSN_IO);
470 return ReadDOC(docptr, LastDataRead);
471}
472
Boris Brezillonc0739d82018-09-06 14:05:23 +0200473static void doc2001_writebuf(struct nand_chip *this, const u_char *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474{
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100475 struct doc_priv *doc = nand_get_controller_data(this);
David Woodhousee0c7d762006-05-13 18:07:53 +0100476 void __iomem *docptr = doc->virtadr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 int i;
478
David Woodhousee0c7d762006-05-13 18:07:53 +0100479 for (i = 0; i < len; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 WriteDOC_(buf[i], docptr, DoC_Mil_CDSN_IO + i);
481 /* Terminate write pipeline */
482 WriteDOC(0x00, docptr, WritePipeTerm);
483}
484
Boris Brezillon7e534322018-09-06 14:05:22 +0200485static void doc2001_readbuf(struct nand_chip *this, u_char *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486{
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100487 struct doc_priv *doc = nand_get_controller_data(this);
David Woodhousee0c7d762006-05-13 18:07:53 +0100488 void __iomem *docptr = doc->virtadr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 int i;
490
491 /* Start read pipeline */
492 ReadDOC(docptr, ReadPipeInit);
493
David Woodhousee0c7d762006-05-13 18:07:53 +0100494 for (i = 0; i < len - 1; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 buf[i] = ReadDOC(docptr, Mil_CDSN_IO + (i & 0xff));
496
497 /* Terminate read pipeline */
498 buf[i] = ReadDOC(docptr, LastDataRead);
499}
500
Boris Brezillon7e534322018-09-06 14:05:22 +0200501static u_char doc2001plus_read_byte(struct nand_chip *this)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502{
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100503 struct doc_priv *doc = nand_get_controller_data(this);
David Woodhousee0c7d762006-05-13 18:07:53 +0100504 void __iomem *docptr = doc->virtadr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 u_char ret;
506
David Woodhousee0c7d762006-05-13 18:07:53 +0100507 ReadDOC(docptr, Mplus_ReadPipeInit);
508 ReadDOC(docptr, Mplus_ReadPipeInit);
509 ret = ReadDOC(docptr, Mplus_LastDataRead);
510 if (debug)
511 printk("read_byte returns %02x\n", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 return ret;
513}
514
Boris Brezillonc0739d82018-09-06 14:05:23 +0200515static void doc2001plus_writebuf(struct nand_chip *this, const u_char *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516{
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100517 struct doc_priv *doc = nand_get_controller_data(this);
David Woodhousee0c7d762006-05-13 18:07:53 +0100518 void __iomem *docptr = doc->virtadr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 int i;
520
David Woodhousee0c7d762006-05-13 18:07:53 +0100521 if (debug)
522 printk("writebuf of %d bytes: ", len);
523 for (i = 0; i < len; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 WriteDOC_(buf[i], docptr, DoC_Mil_CDSN_IO + i);
525 if (debug && i < 16)
526 printk("%02x ", buf[i]);
527 }
David Woodhousee0c7d762006-05-13 18:07:53 +0100528 if (debug)
529 printk("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530}
531
Boris Brezillon7e534322018-09-06 14:05:22 +0200532static void doc2001plus_readbuf(struct nand_chip *this, u_char *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533{
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100534 struct doc_priv *doc = nand_get_controller_data(this);
David Woodhousee0c7d762006-05-13 18:07:53 +0100535 void __iomem *docptr = doc->virtadr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 int i;
537
David Woodhousee0c7d762006-05-13 18:07:53 +0100538 if (debug)
539 printk("readbuf of %d bytes: ", len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540
541 /* Start read pipeline */
542 ReadDOC(docptr, Mplus_ReadPipeInit);
543 ReadDOC(docptr, Mplus_ReadPipeInit);
544
David Woodhousee0c7d762006-05-13 18:07:53 +0100545 for (i = 0; i < len - 2; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 buf[i] = ReadDOC(docptr, Mil_CDSN_IO);
547 if (debug && i < 16)
548 printk("%02x ", buf[i]);
549 }
550
551 /* Terminate read pipeline */
David Woodhousee0c7d762006-05-13 18:07:53 +0100552 buf[len - 2] = ReadDOC(docptr, Mplus_LastDataRead);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 if (debug && i < 16)
David Woodhousee0c7d762006-05-13 18:07:53 +0100554 printk("%02x ", buf[len - 2]);
555 buf[len - 1] = ReadDOC(docptr, Mplus_LastDataRead);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 if (debug && i < 16)
David Woodhousee0c7d762006-05-13 18:07:53 +0100557 printk("%02x ", buf[len - 1]);
558 if (debug)
559 printk("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560}
561
Boris Brezillon758b56f2018-09-06 14:05:24 +0200562static void doc2001plus_select_chip(struct nand_chip *this, int chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563{
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100564 struct doc_priv *doc = nand_get_controller_data(this);
David Woodhousee0c7d762006-05-13 18:07:53 +0100565 void __iomem *docptr = doc->virtadr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 int floor = 0;
567
David Woodhousee0c7d762006-05-13 18:07:53 +0100568 if (debug)
569 printk("select chip (%d)\n", chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570
571 if (chip == -1) {
572 /* Disable flash internally */
573 WriteDOC(0, docptr, Mplus_FlashSelect);
574 return;
575 }
576
577 floor = chip / doc->chips_per_floor;
David Woodhousee0c7d762006-05-13 18:07:53 +0100578 chip -= (floor * doc->chips_per_floor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579
580 /* Assert ChipEnable and deassert WriteProtect */
581 WriteDOC((DOC_FLASH_CE), docptr, Mplus_FlashSelect);
Boris Brezillon97d90da2017-11-30 18:01:29 +0100582 nand_reset_op(this);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583
584 doc->curchip = chip;
585 doc->curfloor = floor;
586}
587
Boris Brezillon758b56f2018-09-06 14:05:24 +0200588static void doc200x_select_chip(struct nand_chip *this, int chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589{
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100590 struct doc_priv *doc = nand_get_controller_data(this);
David Woodhousee0c7d762006-05-13 18:07:53 +0100591 void __iomem *docptr = doc->virtadr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 int floor = 0;
593
David Woodhousee0c7d762006-05-13 18:07:53 +0100594 if (debug)
595 printk("select chip (%d)\n", chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596
597 if (chip == -1)
598 return;
599
600 floor = chip / doc->chips_per_floor;
David Woodhousee0c7d762006-05-13 18:07:53 +0100601 chip -= (floor * doc->chips_per_floor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602
603 /* 11.4.4 -- deassert CE before changing chip */
Boris Brezillon0f808c12018-09-06 14:05:26 +0200604 doc200x_hwcontrol(this, NAND_CMD_NONE, 0 | NAND_CTRL_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
606 WriteDOC(floor, docptr, FloorSelect);
607 WriteDOC(chip, docptr, CDSNDeviceSelect);
608
Boris Brezillon0f808c12018-09-06 14:05:26 +0200609 doc200x_hwcontrol(this, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610
611 doc->curchip = chip;
612 doc->curfloor = floor;
613}
614
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200615#define CDSN_CTRL_MSK (CDSN_CTRL_CE | CDSN_CTRL_CLE | CDSN_CTRL_ALE)
616
Boris Brezillon0f808c12018-09-06 14:05:26 +0200617static void doc200x_hwcontrol(struct nand_chip *this, int cmd,
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200618 unsigned int ctrl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619{
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100620 struct doc_priv *doc = nand_get_controller_data(this);
David Woodhousee0c7d762006-05-13 18:07:53 +0100621 void __iomem *docptr = doc->virtadr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200623 if (ctrl & NAND_CTRL_CHANGE) {
624 doc->CDSNControl &= ~CDSN_CTRL_MSK;
625 doc->CDSNControl |= ctrl & CDSN_CTRL_MSK;
626 if (debug)
627 printk("hwcontrol(%d): %02x\n", cmd, doc->CDSNControl);
628 WriteDOC(doc->CDSNControl, docptr, CDSNControl);
629 /* 11.4.3 -- 4 NOPs after CSDNControl write */
630 DoC_Delay(doc, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 }
Thomas Gleixnercad74f22006-05-23 23:28:48 +0200632 if (cmd != NAND_CMD_NONE) {
633 if (DoC_is_2000(doc))
Boris Brezillonc0739d82018-09-06 14:05:23 +0200634 doc2000_write_byte(this, cmd);
Thomas Gleixnercad74f22006-05-23 23:28:48 +0200635 else
Boris Brezillonc0739d82018-09-06 14:05:23 +0200636 doc2001_write_byte(this, cmd);
Thomas Gleixnercad74f22006-05-23 23:28:48 +0200637 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638}
639
Boris Brezillon5295cf22018-09-06 14:05:28 +0200640static void doc2001plus_command(struct nand_chip *this, unsigned command,
641 int column, int page_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642{
Boris Brezillon5295cf22018-09-06 14:05:28 +0200643 struct mtd_info *mtd = nand_to_mtd(this);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100644 struct doc_priv *doc = nand_get_controller_data(this);
David Woodhousee0c7d762006-05-13 18:07:53 +0100645 void __iomem *docptr = doc->virtadr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646
647 /*
648 * Must terminate write pipeline before sending any commands
649 * to the device.
650 */
651 if (command == NAND_CMD_PAGEPROG) {
652 WriteDOC(0x00, docptr, Mplus_WritePipeTerm);
653 WriteDOC(0x00, docptr, Mplus_WritePipeTerm);
654 }
655
656 /*
657 * Write out the command to the device.
658 */
659 if (command == NAND_CMD_SEQIN) {
660 int readcmd;
661
Joern Engel28318772006-05-22 23:18:05 +0200662 if (column >= mtd->writesize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 /* OOB area */
Joern Engel28318772006-05-22 23:18:05 +0200664 column -= mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 readcmd = NAND_CMD_READOOB;
666 } else if (column < 256) {
667 /* First 256 bytes --> READ0 */
668 readcmd = NAND_CMD_READ0;
669 } else {
670 column -= 256;
671 readcmd = NAND_CMD_READ1;
672 }
673 WriteDOC(readcmd, docptr, Mplus_FlashCmd);
674 }
675 WriteDOC(command, docptr, Mplus_FlashCmd);
676 WriteDOC(0, docptr, Mplus_WritePipeTerm);
677 WriteDOC(0, docptr, Mplus_WritePipeTerm);
678
679 if (column != -1 || page_addr != -1) {
680 /* Serially input address */
681 if (column != -1) {
682 /* Adjust columns for 16 bit buswidth */
Brian Norris3dad2342014-01-29 14:08:12 -0800683 if (this->options & NAND_BUSWIDTH_16 &&
684 !nand_opcode_8bits(command))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 column >>= 1;
686 WriteDOC(column, docptr, Mplus_FlashAddress);
687 }
688 if (page_addr != -1) {
David Woodhousee0c7d762006-05-13 18:07:53 +0100689 WriteDOC((unsigned char)(page_addr & 0xff), docptr, Mplus_FlashAddress);
690 WriteDOC((unsigned char)((page_addr >> 8) & 0xff), docptr, Mplus_FlashAddress);
Masahiro Yamada14157f82017-09-13 11:05:50 +0900691 if (this->options & NAND_ROW_ADDR_3) {
David Woodhousee0c7d762006-05-13 18:07:53 +0100692 WriteDOC((unsigned char)((page_addr >> 16) & 0x0f), docptr, Mplus_FlashAddress);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 printk("high density\n");
694 }
695 }
696 WriteDOC(0, docptr, Mplus_WritePipeTerm);
697 WriteDOC(0, docptr, Mplus_WritePipeTerm);
698 /* deassert ALE */
David Woodhousee0c7d762006-05-13 18:07:53 +0100699 if (command == NAND_CMD_READ0 || command == NAND_CMD_READ1 ||
700 command == NAND_CMD_READOOB || command == NAND_CMD_READID)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 WriteDOC(0, docptr, Mplus_FlashControl);
702 }
703
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000704 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 * program and erase have their own busy handlers
706 * status and sequential in needs no delay
David Woodhousee0c7d762006-05-13 18:07:53 +0100707 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 switch (command) {
709
710 case NAND_CMD_PAGEPROG:
711 case NAND_CMD_ERASE1:
712 case NAND_CMD_ERASE2:
713 case NAND_CMD_SEQIN:
714 case NAND_CMD_STATUS:
715 return;
716
717 case NAND_CMD_RESET:
718 if (this->dev_ready)
719 break;
720 udelay(this->chip_delay);
721 WriteDOC(NAND_CMD_STATUS, docptr, Mplus_FlashCmd);
722 WriteDOC(0, docptr, Mplus_WritePipeTerm);
723 WriteDOC(0, docptr, Mplus_WritePipeTerm);
Boris Brezillon716bbba2018-09-07 00:38:35 +0200724 while (!(this->legacy.read_byte(this) & 0x40)) ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 return;
726
David Woodhousee0c7d762006-05-13 18:07:53 +0100727 /* This applies to read commands */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 default:
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000729 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 * If we don't have access to the busy pin, we apply the given
731 * command delay
David Woodhousee0c7d762006-05-13 18:07:53 +0100732 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 if (!this->dev_ready) {
David Woodhousee0c7d762006-05-13 18:07:53 +0100734 udelay(this->chip_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 return;
736 }
737 }
738
739 /* Apply this short delay always to ensure that we do wait tWB in
740 * any case on any machine. */
David Woodhousee0c7d762006-05-13 18:07:53 +0100741 ndelay(100);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 /* wait until command is processed */
Boris Brezillon50a487e2018-09-06 14:05:27 +0200743 while (!this->dev_ready(this)) ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744}
745
Boris Brezillon50a487e2018-09-06 14:05:27 +0200746static int doc200x_dev_ready(struct nand_chip *this)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747{
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100748 struct doc_priv *doc = nand_get_controller_data(this);
David Woodhousee0c7d762006-05-13 18:07:53 +0100749 void __iomem *docptr = doc->virtadr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750
751 if (DoC_is_MillenniumPlus(doc)) {
752 /* 11.4.2 -- must NOP four times before checking FR/B# */
753 DoC_Delay(doc, 4);
754 if ((ReadDOC(docptr, Mplus_FlashControl) & CDSN_CTRL_FR_B_MASK) != CDSN_CTRL_FR_B_MASK) {
David Woodhousee0c7d762006-05-13 18:07:53 +0100755 if (debug)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 printk("not ready\n");
757 return 0;
758 }
David Woodhousee0c7d762006-05-13 18:07:53 +0100759 if (debug)
760 printk("was ready\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 return 1;
762 } else {
763 /* 11.4.2 -- must NOP four times before checking FR/B# */
764 DoC_Delay(doc, 4);
765 if (!(ReadDOC(docptr, CDSNControl) & CDSN_CTRL_FR_B)) {
David Woodhousee0c7d762006-05-13 18:07:53 +0100766 if (debug)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 printk("not ready\n");
768 return 0;
769 }
770 /* 11.4.2 -- Must NOP twice if it's ready */
771 DoC_Delay(doc, 2);
David Woodhousee0c7d762006-05-13 18:07:53 +0100772 if (debug)
773 printk("was ready\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 return 1;
775 }
776}
777
Boris Brezillonc17556f2018-09-06 14:05:25 +0200778static int doc200x_block_bad(struct nand_chip *this, loff_t ofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779{
780 /* This is our last resort if we couldn't find or create a BBT. Just
781 pretend all blocks are good. */
782 return 0;
783}
784
Boris Brezillonec476362018-09-06 14:05:17 +0200785static void doc200x_enable_hwecc(struct nand_chip *this, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786{
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100787 struct doc_priv *doc = nand_get_controller_data(this);
David Woodhousee0c7d762006-05-13 18:07:53 +0100788 void __iomem *docptr = doc->virtadr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789
790 /* Prime the ECC engine */
David Woodhousee0c7d762006-05-13 18:07:53 +0100791 switch (mode) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 case NAND_ECC_READ:
793 WriteDOC(DOC_ECC_RESET, docptr, ECCConf);
794 WriteDOC(DOC_ECC_EN, docptr, ECCConf);
795 break;
796 case NAND_ECC_WRITE:
797 WriteDOC(DOC_ECC_RESET, docptr, ECCConf);
798 WriteDOC(DOC_ECC_EN | DOC_ECC_RW, docptr, ECCConf);
799 break;
800 }
801}
802
Boris Brezillonec476362018-09-06 14:05:17 +0200803static void doc2001plus_enable_hwecc(struct nand_chip *this, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804{
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100805 struct doc_priv *doc = nand_get_controller_data(this);
David Woodhousee0c7d762006-05-13 18:07:53 +0100806 void __iomem *docptr = doc->virtadr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807
808 /* Prime the ECC engine */
David Woodhousee0c7d762006-05-13 18:07:53 +0100809 switch (mode) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 case NAND_ECC_READ:
811 WriteDOC(DOC_ECC_RESET, docptr, Mplus_ECCConf);
812 WriteDOC(DOC_ECC_EN, docptr, Mplus_ECCConf);
813 break;
814 case NAND_ECC_WRITE:
815 WriteDOC(DOC_ECC_RESET, docptr, Mplus_ECCConf);
816 WriteDOC(DOC_ECC_EN | DOC_ECC_RW, docptr, Mplus_ECCConf);
817 break;
818 }
819}
820
821/* This code is only called on write */
Boris Brezillonaf37d2c2018-09-06 14:05:18 +0200822static int doc200x_calculate_ecc(struct nand_chip *this, const u_char *dat,
823 unsigned char *ecc_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824{
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100825 struct doc_priv *doc = nand_get_controller_data(this);
David Woodhousee0c7d762006-05-13 18:07:53 +0100826 void __iomem *docptr = doc->virtadr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 int i;
828 int emptymatch = 1;
829
830 /* flush the pipeline */
831 if (DoC_is_2000(doc)) {
832 WriteDOC(doc->CDSNControl & ~CDSN_CTRL_FLASH_IO, docptr, CDSNControl);
833 WriteDOC(0, docptr, 2k_CDSN_IO);
834 WriteDOC(0, docptr, 2k_CDSN_IO);
835 WriteDOC(0, docptr, 2k_CDSN_IO);
836 WriteDOC(doc->CDSNControl, docptr, CDSNControl);
837 } else if (DoC_is_MillenniumPlus(doc)) {
838 WriteDOC(0, docptr, Mplus_NOP);
839 WriteDOC(0, docptr, Mplus_NOP);
840 WriteDOC(0, docptr, Mplus_NOP);
841 } else {
842 WriteDOC(0, docptr, NOP);
843 WriteDOC(0, docptr, NOP);
844 WriteDOC(0, docptr, NOP);
845 }
846
847 for (i = 0; i < 6; i++) {
848 if (DoC_is_MillenniumPlus(doc))
849 ecc_code[i] = ReadDOC_(docptr, DoC_Mplus_ECCSyndrome0 + i);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000850 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 ecc_code[i] = ReadDOC_(docptr, DoC_ECCSyndrome0 + i);
852 if (ecc_code[i] != empty_write_ecc[i])
853 emptymatch = 0;
854 }
855 if (DoC_is_MillenniumPlus(doc))
856 WriteDOC(DOC_ECC_DIS, docptr, Mplus_ECCConf);
857 else
858 WriteDOC(DOC_ECC_DIS, docptr, ECCConf);
859#if 0
860 /* If emptymatch=1, we might have an all-0xff data buffer. Check. */
861 if (emptymatch) {
862 /* Note: this somewhat expensive test should not be triggered
863 often. It could be optimized away by examining the data in
864 the writebuf routine, and remembering the result. */
865 for (i = 0; i < 512; i++) {
David Woodhousee0c7d762006-05-13 18:07:53 +0100866 if (dat[i] == 0xff)
867 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 emptymatch = 0;
869 break;
870 }
871 }
872 /* If emptymatch still =1, we do have an all-0xff data buffer.
873 Return all-0xff ecc value instead of the computed one, so
874 it'll look just like a freshly-erased page. */
David Woodhousee0c7d762006-05-13 18:07:53 +0100875 if (emptymatch)
876 memset(ecc_code, 0xff, 6);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877#endif
878 return 0;
879}
880
Boris Brezillon00da2ea2018-09-06 14:05:19 +0200881static int doc200x_correct_data(struct nand_chip *this, u_char *dat,
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200882 u_char *read_ecc, u_char *isnull)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883{
884 int i, ret = 0;
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100885 struct doc_priv *doc = nand_get_controller_data(this);
David Woodhousee0c7d762006-05-13 18:07:53 +0100886 void __iomem *docptr = doc->virtadr;
Thomas Gleixnerf5bbdac2006-05-25 10:07:16 +0200887 uint8_t calc_ecc[6];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 volatile u_char dummy;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000889
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 /* flush the pipeline */
891 if (DoC_is_2000(doc)) {
892 dummy = ReadDOC(docptr, 2k_ECCStatus);
893 dummy = ReadDOC(docptr, 2k_ECCStatus);
894 dummy = ReadDOC(docptr, 2k_ECCStatus);
895 } else if (DoC_is_MillenniumPlus(doc)) {
896 dummy = ReadDOC(docptr, Mplus_ECCConf);
897 dummy = ReadDOC(docptr, Mplus_ECCConf);
898 dummy = ReadDOC(docptr, Mplus_ECCConf);
899 } else {
900 dummy = ReadDOC(docptr, ECCConf);
901 dummy = ReadDOC(docptr, ECCConf);
902 dummy = ReadDOC(docptr, ECCConf);
903 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000904
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300905 /* Error occurred ? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 if (dummy & 0x80) {
907 for (i = 0; i < 6; i++) {
908 if (DoC_is_MillenniumPlus(doc))
909 calc_ecc[i] = ReadDOC_(docptr, DoC_Mplus_ECCSyndrome0 + i);
910 else
911 calc_ecc[i] = ReadDOC_(docptr, DoC_ECCSyndrome0 + i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 }
Boris BREZILLONcc01e602015-12-30 20:39:52 +0100913
Thomas Gleixner964dfce2018-04-22 18:23:54 +0200914 ret = doc_ecc_decode(doc->rs_decoder, dat, calc_ecc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 if (ret > 0)
Shreeya Patel63fa37f2018-02-22 22:01:22 +0530916 pr_err("doc200x_correct_data corrected %d errors\n",
917 ret);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000918 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 if (DoC_is_MillenniumPlus(doc))
920 WriteDOC(DOC_ECC_DIS, docptr, Mplus_ECCConf);
921 else
922 WriteDOC(DOC_ECC_DIS, docptr, ECCConf);
Brian Norrisd57f40542011-09-20 18:34:25 -0700923 if (no_ecc_failures && mtd_is_eccerr(ret)) {
Shreeya Patel63fa37f2018-02-22 22:01:22 +0530924 pr_err("suppressing ECC failure\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 ret = 0;
926 }
927 return ret;
928}
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000929
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930//u_char mydatabuf[528];
931
Boris Brezillon68c1b752016-02-03 20:00:23 +0100932static int doc200x_ooblayout_ecc(struct mtd_info *mtd, int section,
933 struct mtd_oob_region *oobregion)
934{
935 if (section)
936 return -ERANGE;
937
938 oobregion->offset = 0;
939 oobregion->length = 6;
940
941 return 0;
942}
943
944static int doc200x_ooblayout_free(struct mtd_info *mtd, int section,
945 struct mtd_oob_region *oobregion)
946{
947 if (section > 1)
948 return -ERANGE;
949
950 /*
951 * The strange out-of-order free bytes definition is a (possibly
952 * unneeded) attempt to retain compatibility. It used to read:
953 * .oobfree = { {8, 8} }
954 * Since that leaves two bytes unusable, it was changed. But the
955 * following scheme might affect existing jffs2 installs by moving the
956 * cleanmarker:
957 * .oobfree = { {6, 10} }
958 * jffs2 seems to handle the above gracefully, but the current scheme
959 * seems safer. The only problem with it is that any code retrieving
960 * free bytes position must be able to handle out-of-order segments.
961 */
962 if (!section) {
963 oobregion->offset = 8;
964 oobregion->length = 8;
965 } else {
966 oobregion->offset = 6;
967 oobregion->length = 2;
968 }
969
970 return 0;
971}
972
973static const struct mtd_ooblayout_ops doc200x_ooblayout_ops = {
974 .ecc = doc200x_ooblayout_ecc,
975 .free = doc200x_ooblayout_free,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976};
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000977
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978/* Find the (I)NFTL Media Header, and optionally also the mirror media header.
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200979 On successful return, buf will contain a copy of the media header for
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 further processing. id is the string to scan for, and will presumably be
981 either "ANAND" or "BNAND". If findmirror=1, also look for the mirror media
982 header. The page #s of the found media headers are placed in mh0_page and
983 mh1_page in the DOC private structure. */
David Woodhousee0c7d762006-05-13 18:07:53 +0100984static int __init find_media_headers(struct mtd_info *mtd, u_char *buf, const char *id, int findmirror)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +0100986 struct nand_chip *this = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100987 struct doc_priv *doc = nand_get_controller_data(this);
Dan Brown1a78ff62005-03-29 21:57:48 +0100988 unsigned offs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 int ret;
990 size_t retlen;
991
Dan Brown1a78ff62005-03-29 21:57:48 +0100992 for (offs = 0; offs < mtd->size; offs += mtd->erasesize) {
Artem Bityutskiy329ad392011-12-23 17:30:16 +0200993 ret = mtd_read(mtd, offs, mtd->writesize, &retlen, buf);
Joern Engel28318772006-05-22 23:18:05 +0200994 if (retlen != mtd->writesize)
David Woodhousee0c7d762006-05-13 18:07:53 +0100995 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 if (ret) {
Shreeya Patel63fa37f2018-02-22 22:01:22 +0530997 pr_warn("ECC error scanning DOC at 0x%x\n", offs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 }
David Woodhousee0c7d762006-05-13 18:07:53 +0100999 if (memcmp(buf, id, 6))
1000 continue;
Shreeya Patel63fa37f2018-02-22 22:01:22 +05301001 pr_info("Found DiskOnChip %s Media Header at 0x%x\n", id, offs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 if (doc->mh0_page == -1) {
1003 doc->mh0_page = offs >> this->page_shift;
David Woodhousee0c7d762006-05-13 18:07:53 +01001004 if (!findmirror)
1005 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 continue;
1007 }
1008 doc->mh1_page = offs >> this->page_shift;
1009 return 2;
1010 }
1011 if (doc->mh0_page == -1) {
Shreeya Patel63fa37f2018-02-22 22:01:22 +05301012 pr_warn("DiskOnChip %s Media Header not found.\n", id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 return 0;
1014 }
1015 /* Only one mediaheader was found. We want buf to contain a
1016 mediaheader on return, so we'll have to re-read the one we found. */
1017 offs = doc->mh0_page << this->page_shift;
Artem Bityutskiy329ad392011-12-23 17:30:16 +02001018 ret = mtd_read(mtd, offs, mtd->writesize, &retlen, buf);
Joern Engel28318772006-05-22 23:18:05 +02001019 if (retlen != mtd->writesize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 /* Insanity. Give up. */
Shreeya Patel63fa37f2018-02-22 22:01:22 +05301021 pr_err("Read DiskOnChip Media Header once, but can't reread it???\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 return 0;
1023 }
1024 return 1;
1025}
1026
David Woodhousee0c7d762006-05-13 18:07:53 +01001027static inline int __init nftl_partscan(struct mtd_info *mtd, struct mtd_partition *parts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +01001029 struct nand_chip *this = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +01001030 struct doc_priv *doc = nand_get_controller_data(this);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 int ret = 0;
1032 u_char *buf;
1033 struct NFTLMediaHeader *mh;
1034 const unsigned psize = 1 << this->page_shift;
Dan Brown1a78ff62005-03-29 21:57:48 +01001035 int numparts = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 unsigned blocks, maxblocks;
1037 int offs, numheaders;
1038
Joern Engel28318772006-05-22 23:18:05 +02001039 buf = kmalloc(mtd->writesize, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 if (!buf) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 return 0;
1042 }
David Woodhousee0c7d762006-05-13 18:07:53 +01001043 if (!(numheaders = find_media_headers(mtd, buf, "ANAND", 1)))
1044 goto out;
1045 mh = (struct NFTLMediaHeader *)buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046
Harvey Harrison96372442008-07-30 12:34:57 -07001047 le16_to_cpus(&mh->NumEraseUnits);
1048 le16_to_cpus(&mh->FirstPhysicalEUN);
1049 le32_to_cpus(&mh->FormattedSize);
Thomas Gleixnerf29a4b82005-01-31 22:22:24 +00001050
Shreeya Patel63fa37f2018-02-22 22:01:22 +05301051 pr_info(" DataOrgID = %s\n"
1052 " NumEraseUnits = %d\n"
1053 " FirstPhysicalEUN = %d\n"
1054 " FormattedSize = %d\n"
1055 " UnitSizeFactor = %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 mh->DataOrgID, mh->NumEraseUnits,
1057 mh->FirstPhysicalEUN, mh->FormattedSize,
1058 mh->UnitSizeFactor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059
1060 blocks = mtd->size >> this->phys_erase_shift;
1061 maxblocks = min(32768U, mtd->erasesize - psize);
1062
1063 if (mh->UnitSizeFactor == 0x00) {
1064 /* Auto-determine UnitSizeFactor. The constraints are:
1065 - There can be at most 32768 virtual blocks.
1066 - There can be at most (virtual block size - page size)
David Woodhousee0c7d762006-05-13 18:07:53 +01001067 virtual blocks (because MediaHeader+BBT must fit in 1).
1068 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 mh->UnitSizeFactor = 0xff;
1070 while (blocks > maxblocks) {
1071 blocks >>= 1;
1072 maxblocks = min(32768U, (maxblocks << 1) + psize);
1073 mh->UnitSizeFactor--;
1074 }
Shreeya Patel63fa37f2018-02-22 22:01:22 +05301075 pr_warn("UnitSizeFactor=0x00 detected. Correct value is assumed to be 0x%02x.\n", mh->UnitSizeFactor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 }
1077
1078 /* NOTE: The lines below modify internal variables of the NAND and MTD
1079 layers; variables with have already been configured by nand_scan.
1080 Unfortunately, we didn't know before this point what these values
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001081 should be. Thus, this code is somewhat dependent on the exact
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 implementation of the NAND layer. */
1083 if (mh->UnitSizeFactor != 0xff) {
1084 this->bbt_erase_shift += (0xff - mh->UnitSizeFactor);
1085 mtd->erasesize <<= (0xff - mh->UnitSizeFactor);
Shreeya Patel63fa37f2018-02-22 22:01:22 +05301086 pr_info("Setting virtual erase size to %d\n", mtd->erasesize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 blocks = mtd->size >> this->bbt_erase_shift;
1088 maxblocks = min(32768U, mtd->erasesize - psize);
1089 }
1090
1091 if (blocks > maxblocks) {
Shreeya Patel63fa37f2018-02-22 22:01:22 +05301092 pr_err("UnitSizeFactor of 0x%02x is inconsistent with device size. Aborting.\n", mh->UnitSizeFactor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 goto out;
1094 }
1095
1096 /* Skip past the media headers. */
1097 offs = max(doc->mh0_page, doc->mh1_page);
1098 offs <<= this->page_shift;
1099 offs += mtd->erasesize;
1100
Dan Brown1a78ff62005-03-29 21:57:48 +01001101 if (show_firmware_partition == 1) {
1102 parts[0].name = " DiskOnChip Firmware / Media Header partition";
1103 parts[0].offset = 0;
1104 parts[0].size = offs;
1105 numparts = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 }
Dan Brown1a78ff62005-03-29 21:57:48 +01001107
1108 parts[numparts].name = " DiskOnChip BDTL partition";
1109 parts[numparts].offset = offs;
1110 parts[numparts].size = (mh->NumEraseUnits - numheaders) << this->bbt_erase_shift;
1111
1112 offs += parts[numparts].size;
1113 numparts++;
1114
1115 if (offs < mtd->size) {
1116 parts[numparts].name = " DiskOnChip Remainder partition";
1117 parts[numparts].offset = offs;
1118 parts[numparts].size = mtd->size - offs;
1119 numparts++;
1120 }
1121
1122 ret = numparts;
David Woodhousee0c7d762006-05-13 18:07:53 +01001123 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 kfree(buf);
1125 return ret;
1126}
1127
1128/* This is a stripped-down copy of the code in inftlmount.c */
David Woodhousee0c7d762006-05-13 18:07:53 +01001129static inline int __init inftl_partscan(struct mtd_info *mtd, struct mtd_partition *parts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +01001131 struct nand_chip *this = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +01001132 struct doc_priv *doc = nand_get_controller_data(this);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 int ret = 0;
1134 u_char *buf;
1135 struct INFTLMediaHeader *mh;
1136 struct INFTLPartition *ip;
1137 int numparts = 0;
1138 int blocks;
1139 int vshift, lastvunit = 0;
1140 int i;
1141 int end = mtd->size;
1142
1143 if (inftl_bbt_write)
1144 end -= (INFTL_BBT_RESERVED_BLOCKS << this->phys_erase_shift);
1145
Joern Engel28318772006-05-22 23:18:05 +02001146 buf = kmalloc(mtd->writesize, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 if (!buf) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 return 0;
1149 }
1150
David Woodhousee0c7d762006-05-13 18:07:53 +01001151 if (!find_media_headers(mtd, buf, "BNAND", 0))
1152 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 doc->mh1_page = doc->mh0_page + (4096 >> this->page_shift);
David Woodhousee0c7d762006-05-13 18:07:53 +01001154 mh = (struct INFTLMediaHeader *)buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155
Harvey Harrison96372442008-07-30 12:34:57 -07001156 le32_to_cpus(&mh->NoOfBootImageBlocks);
1157 le32_to_cpus(&mh->NoOfBinaryPartitions);
1158 le32_to_cpus(&mh->NoOfBDTLPartitions);
1159 le32_to_cpus(&mh->BlockMultiplierBits);
1160 le32_to_cpus(&mh->FormatFlags);
1161 le32_to_cpus(&mh->PercentUsed);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001162
Shreeya Patel63fa37f2018-02-22 22:01:22 +05301163 pr_info(" bootRecordID = %s\n"
1164 " NoOfBootImageBlocks = %d\n"
1165 " NoOfBinaryPartitions = %d\n"
1166 " NoOfBDTLPartitions = %d\n"
1167 " BlockMultiplerBits = %d\n"
1168 " FormatFlgs = %d\n"
1169 " OsakVersion = %d.%d.%d.%d\n"
1170 " PercentUsed = %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 mh->bootRecordID, mh->NoOfBootImageBlocks,
1172 mh->NoOfBinaryPartitions,
1173 mh->NoOfBDTLPartitions,
1174 mh->BlockMultiplierBits, mh->FormatFlags,
1175 ((unsigned char *) &mh->OsakVersion)[0] & 0xf,
1176 ((unsigned char *) &mh->OsakVersion)[1] & 0xf,
1177 ((unsigned char *) &mh->OsakVersion)[2] & 0xf,
1178 ((unsigned char *) &mh->OsakVersion)[3] & 0xf,
1179 mh->PercentUsed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180
1181 vshift = this->phys_erase_shift + mh->BlockMultiplierBits;
1182
1183 blocks = mtd->size >> vshift;
1184 if (blocks > 32768) {
Shreeya Patel63fa37f2018-02-22 22:01:22 +05301185 pr_err("BlockMultiplierBits=%d is inconsistent with device size. Aborting.\n", mh->BlockMultiplierBits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 goto out;
1187 }
1188
1189 blocks = doc->chips_per_floor << (this->chip_shift - this->phys_erase_shift);
1190 if (inftl_bbt_write && (blocks > mtd->erasesize)) {
Shreeya Patel63fa37f2018-02-22 22:01:22 +05301191 pr_err("Writeable BBTs spanning more than one erase block are not yet supported. FIX ME!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 goto out;
1193 }
1194
1195 /* Scan the partitions */
1196 for (i = 0; (i < 4); i++) {
1197 ip = &(mh->Partitions[i]);
Harvey Harrison96372442008-07-30 12:34:57 -07001198 le32_to_cpus(&ip->virtualUnits);
1199 le32_to_cpus(&ip->firstUnit);
1200 le32_to_cpus(&ip->lastUnit);
1201 le32_to_cpus(&ip->flags);
1202 le32_to_cpus(&ip->spareUnits);
1203 le32_to_cpus(&ip->Reserved0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204
Shreeya Patel63fa37f2018-02-22 22:01:22 +05301205 pr_info(" PARTITION[%d] ->\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 " virtualUnits = %d\n"
1207 " firstUnit = %d\n"
1208 " lastUnit = %d\n"
1209 " flags = 0x%x\n"
1210 " spareUnits = %d\n",
1211 i, ip->virtualUnits, ip->firstUnit,
1212 ip->lastUnit, ip->flags,
1213 ip->spareUnits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214
Dan Brown1a78ff62005-03-29 21:57:48 +01001215 if ((show_firmware_partition == 1) &&
1216 (i == 0) && (ip->firstUnit > 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 parts[0].name = " DiskOnChip IPL / Media Header partition";
1218 parts[0].offset = 0;
1219 parts[0].size = mtd->erasesize * ip->firstUnit;
1220 numparts = 1;
1221 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222
1223 if (ip->flags & INFTL_BINARY)
1224 parts[numparts].name = " DiskOnChip BDK partition";
1225 else
1226 parts[numparts].name = " DiskOnChip BDTL partition";
1227 parts[numparts].offset = ip->firstUnit << vshift;
1228 parts[numparts].size = (1 + ip->lastUnit - ip->firstUnit) << vshift;
1229 numparts++;
David Woodhousee0c7d762006-05-13 18:07:53 +01001230 if (ip->lastUnit > lastvunit)
1231 lastvunit = ip->lastUnit;
1232 if (ip->flags & INFTL_LAST)
1233 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 }
1235 lastvunit++;
1236 if ((lastvunit << vshift) < end) {
1237 parts[numparts].name = " DiskOnChip Remainder partition";
1238 parts[numparts].offset = lastvunit << vshift;
1239 parts[numparts].size = end - parts[numparts].offset;
1240 numparts++;
1241 }
1242 ret = numparts;
David Woodhousee0c7d762006-05-13 18:07:53 +01001243 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 kfree(buf);
1245 return ret;
1246}
1247
1248static int __init nftl_scan_bbt(struct mtd_info *mtd)
1249{
1250 int ret, numparts;
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +01001251 struct nand_chip *this = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +01001252 struct doc_priv *doc = nand_get_controller_data(this);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 struct mtd_partition parts[2];
1254
David Woodhousee0c7d762006-05-13 18:07:53 +01001255 memset((char *)parts, 0, sizeof(parts));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 /* On NFTL, we have to find the media headers before we can read the
1257 BBTs, since they're stored in the media header eraseblocks. */
1258 numparts = nftl_partscan(mtd, parts);
David Woodhousee0c7d762006-05-13 18:07:53 +01001259 if (!numparts)
1260 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 this->bbt_td->options = NAND_BBT_ABSPAGE | NAND_BBT_8BIT |
1262 NAND_BBT_SAVECONTENT | NAND_BBT_WRITE |
1263 NAND_BBT_VERSION;
1264 this->bbt_td->veroffs = 7;
1265 this->bbt_td->pages[0] = doc->mh0_page + 1;
1266 if (doc->mh1_page != -1) {
1267 this->bbt_md->options = NAND_BBT_ABSPAGE | NAND_BBT_8BIT |
1268 NAND_BBT_SAVECONTENT | NAND_BBT_WRITE |
1269 NAND_BBT_VERSION;
1270 this->bbt_md->veroffs = 7;
1271 this->bbt_md->pages[0] = doc->mh1_page + 1;
1272 } else {
1273 this->bbt_md = NULL;
1274 }
1275
Boris Brezillone80eba72018-07-05 12:27:31 +02001276 ret = nand_create_bbt(this);
Brian Norrisd24fe0c2015-02-28 02:13:10 -08001277 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 return ret;
Brian Norrisd24fe0c2015-02-28 02:13:10 -08001279
Brian Norris6a7c7332015-06-01 16:17:17 -07001280 return mtd_device_register(mtd, parts, no_autopart ? 0 : numparts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281}
1282
1283static int __init inftl_scan_bbt(struct mtd_info *mtd)
1284{
1285 int ret, numparts;
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +01001286 struct nand_chip *this = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +01001287 struct doc_priv *doc = nand_get_controller_data(this);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 struct mtd_partition parts[5];
1289
1290 if (this->numchips > doc->chips_per_floor) {
Shreeya Patel63fa37f2018-02-22 22:01:22 +05301291 pr_err("Multi-floor INFTL devices not yet supported.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 return -EIO;
1293 }
1294
1295 if (DoC_is_MillenniumPlus(doc)) {
1296 this->bbt_td->options = NAND_BBT_2BIT | NAND_BBT_ABSPAGE;
1297 if (inftl_bbt_write)
1298 this->bbt_td->options |= NAND_BBT_WRITE;
1299 this->bbt_td->pages[0] = 2;
1300 this->bbt_md = NULL;
1301 } else {
David Woodhousee0c7d762006-05-13 18:07:53 +01001302 this->bbt_td->options = NAND_BBT_LASTBLOCK | NAND_BBT_8BIT | NAND_BBT_VERSION;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 if (inftl_bbt_write)
1304 this->bbt_td->options |= NAND_BBT_WRITE;
1305 this->bbt_td->offs = 8;
1306 this->bbt_td->len = 8;
1307 this->bbt_td->veroffs = 7;
1308 this->bbt_td->maxblocks = INFTL_BBT_RESERVED_BLOCKS;
1309 this->bbt_td->reserved_block_code = 0x01;
1310 this->bbt_td->pattern = "MSYS_BBT";
1311
David Woodhousee0c7d762006-05-13 18:07:53 +01001312 this->bbt_md->options = NAND_BBT_LASTBLOCK | NAND_BBT_8BIT | NAND_BBT_VERSION;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 if (inftl_bbt_write)
1314 this->bbt_md->options |= NAND_BBT_WRITE;
1315 this->bbt_md->offs = 8;
1316 this->bbt_md->len = 8;
1317 this->bbt_md->veroffs = 7;
1318 this->bbt_md->maxblocks = INFTL_BBT_RESERVED_BLOCKS;
1319 this->bbt_md->reserved_block_code = 0x01;
1320 this->bbt_md->pattern = "TBB_SYSM";
1321 }
1322
Boris Brezillone80eba72018-07-05 12:27:31 +02001323 ret = nand_create_bbt(this);
Brian Norrisd24fe0c2015-02-28 02:13:10 -08001324 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 return ret;
Brian Norrisd24fe0c2015-02-28 02:13:10 -08001326
David Woodhousee0c7d762006-05-13 18:07:53 +01001327 memset((char *)parts, 0, sizeof(parts));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 numparts = inftl_partscan(mtd, parts);
1329 /* At least for now, require the INFTL Media Header. We could probably
1330 do without it for non-INFTL use, since all it gives us is
1331 autopartitioning, but I want to give it more thought. */
David Woodhousee0c7d762006-05-13 18:07:53 +01001332 if (!numparts)
1333 return -EIO;
Brian Norris6a7c7332015-06-01 16:17:17 -07001334 return mtd_device_register(mtd, parts, no_autopart ? 0 : numparts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335}
1336
1337static inline int __init doc2000_init(struct mtd_info *mtd)
1338{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +01001339 struct nand_chip *this = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +01001340 struct doc_priv *doc = nand_get_controller_data(this);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341
Boris Brezillon716bbba2018-09-07 00:38:35 +02001342 this->legacy.read_byte = doc2000_read_byte;
1343 this->legacy.write_buf = doc2000_writebuf;
1344 this->legacy.read_buf = doc2000_readbuf;
Brian Norrisd24fe0c2015-02-28 02:13:10 -08001345 doc->late_init = nftl_scan_bbt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346
1347 doc->CDSNControl = CDSN_CTRL_FLASH_IO | CDSN_CTRL_ECC_IO;
1348 doc2000_count_chips(mtd);
1349 mtd->name = "DiskOnChip 2000 (NFTL Model)";
1350 return (4 * doc->chips_per_floor);
1351}
1352
1353static inline int __init doc2001_init(struct mtd_info *mtd)
1354{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +01001355 struct nand_chip *this = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +01001356 struct doc_priv *doc = nand_get_controller_data(this);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357
Boris Brezillon716bbba2018-09-07 00:38:35 +02001358 this->legacy.read_byte = doc2001_read_byte;
1359 this->legacy.write_buf = doc2001_writebuf;
1360 this->legacy.read_buf = doc2001_readbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361
1362 ReadDOC(doc->virtadr, ChipID);
1363 ReadDOC(doc->virtadr, ChipID);
1364 ReadDOC(doc->virtadr, ChipID);
1365 if (ReadDOC(doc->virtadr, ChipID) != DOC_ChipID_DocMil) {
1366 /* It's not a Millennium; it's one of the newer
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001367 DiskOnChip 2000 units with a similar ASIC.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 Treat it like a Millennium, except that it
1369 can have multiple chips. */
1370 doc2000_count_chips(mtd);
1371 mtd->name = "DiskOnChip 2000 (INFTL Model)";
Brian Norrisd24fe0c2015-02-28 02:13:10 -08001372 doc->late_init = inftl_scan_bbt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 return (4 * doc->chips_per_floor);
1374 } else {
1375 /* Bog-standard Millennium */
1376 doc->chips_per_floor = 1;
1377 mtd->name = "DiskOnChip Millennium";
Brian Norrisd24fe0c2015-02-28 02:13:10 -08001378 doc->late_init = nftl_scan_bbt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 return 1;
1380 }
1381}
1382
1383static inline int __init doc2001plus_init(struct mtd_info *mtd)
1384{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +01001385 struct nand_chip *this = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +01001386 struct doc_priv *doc = nand_get_controller_data(this);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387
Boris Brezillon716bbba2018-09-07 00:38:35 +02001388 this->legacy.read_byte = doc2001plus_read_byte;
1389 this->legacy.write_buf = doc2001plus_writebuf;
1390 this->legacy.read_buf = doc2001plus_readbuf;
Brian Norrisd24fe0c2015-02-28 02:13:10 -08001391 doc->late_init = inftl_scan_bbt;
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +02001392 this->cmd_ctrl = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 this->select_chip = doc2001plus_select_chip;
1394 this->cmdfunc = doc2001plus_command;
Thomas Gleixner0cddd6c2006-05-23 15:59:58 +02001395 this->ecc.hwctl = doc2001plus_enable_hwecc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396
1397 doc->chips_per_floor = 1;
1398 mtd->name = "DiskOnChip Millennium Plus";
1399
1400 return 1;
1401}
1402
Arjan van de Ven858119e2006-01-14 13:20:43 -08001403static int __init doc_probe(unsigned long physadr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404{
Thomas Gleixner964dfce2018-04-22 18:23:54 +02001405 struct nand_chip *nand = NULL;
1406 struct doc_priv *doc = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 unsigned char ChipID;
1408 struct mtd_info *mtd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 void __iomem *virtadr;
1410 unsigned char save_control;
1411 unsigned char tmp, tmpb, tmpc;
1412 int reg, len, numchips;
1413 int ret = 0;
1414
Sasha Levin86e4bbc2014-03-19 18:24:37 -04001415 if (!request_mem_region(physadr, DOC_IOREMAP_LEN, "DiskOnChip"))
Alexander Shiyan4f0614a2013-11-13 15:59:25 +04001416 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 virtadr = ioremap(physadr, DOC_IOREMAP_LEN);
1418 if (!virtadr) {
Shreeya Patel63fa37f2018-02-22 22:01:22 +05301419 pr_err("Diskonchip ioremap failed: 0x%x bytes at 0x%lx\n",
1420 DOC_IOREMAP_LEN, physadr);
Alexander Shiyan4f0614a2013-11-13 15:59:25 +04001421 ret = -EIO;
1422 goto error_ioremap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 }
1424
1425 /* It's not possible to cleanly detect the DiskOnChip - the
1426 * bootup procedure will put the device into reset mode, and
1427 * it's not possible to talk to it without actually writing
1428 * to the DOCControl register. So we store the current contents
1429 * of the DOCControl register's location, in case we later decide
1430 * that it's not a DiskOnChip, and want to put it back how we
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001431 * found it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 */
1433 save_control = ReadDOC(virtadr, DOCControl);
1434
1435 /* Reset the DiskOnChip ASIC */
David Woodhousee0c7d762006-05-13 18:07:53 +01001436 WriteDOC(DOC_MODE_CLR_ERR | DOC_MODE_MDWREN | DOC_MODE_RESET, virtadr, DOCControl);
1437 WriteDOC(DOC_MODE_CLR_ERR | DOC_MODE_MDWREN | DOC_MODE_RESET, virtadr, DOCControl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438
1439 /* Enable the DiskOnChip ASIC */
David Woodhousee0c7d762006-05-13 18:07:53 +01001440 WriteDOC(DOC_MODE_CLR_ERR | DOC_MODE_MDWREN | DOC_MODE_NORMAL, virtadr, DOCControl);
1441 WriteDOC(DOC_MODE_CLR_ERR | DOC_MODE_MDWREN | DOC_MODE_NORMAL, virtadr, DOCControl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442
1443 ChipID = ReadDOC(virtadr, ChipID);
1444
David Woodhousee0c7d762006-05-13 18:07:53 +01001445 switch (ChipID) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 case DOC_ChipID_Doc2k:
1447 reg = DoC_2k_ECCStatus;
1448 break;
1449 case DOC_ChipID_DocMil:
1450 reg = DoC_ECCConf;
1451 break;
1452 case DOC_ChipID_DocMilPlus16:
1453 case DOC_ChipID_DocMilPlus32:
1454 case 0:
1455 /* Possible Millennium Plus, need to do more checks */
1456 /* Possibly release from power down mode */
1457 for (tmp = 0; (tmp < 4); tmp++)
1458 ReadDOC(virtadr, Mplus_Power);
1459
1460 /* Reset the Millennium Plus ASIC */
David Woodhousee0c7d762006-05-13 18:07:53 +01001461 tmp = DOC_MODE_RESET | DOC_MODE_MDWREN | DOC_MODE_RST_LAT | DOC_MODE_BDECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 WriteDOC(tmp, virtadr, Mplus_DOCControl);
1463 WriteDOC(~tmp, virtadr, Mplus_CtrlConfirm);
1464
Jia-Ju Bai7b4b1992018-04-11 10:57:57 +08001465 usleep_range(1000, 2000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 /* Enable the Millennium Plus ASIC */
David Woodhousee0c7d762006-05-13 18:07:53 +01001467 tmp = DOC_MODE_NORMAL | DOC_MODE_MDWREN | DOC_MODE_RST_LAT | DOC_MODE_BDECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 WriteDOC(tmp, virtadr, Mplus_DOCControl);
1469 WriteDOC(~tmp, virtadr, Mplus_CtrlConfirm);
Jia-Ju Bai7b4b1992018-04-11 10:57:57 +08001470 usleep_range(1000, 2000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471
1472 ChipID = ReadDOC(virtadr, ChipID);
1473
1474 switch (ChipID) {
1475 case DOC_ChipID_DocMilPlus16:
1476 reg = DoC_Mplus_Toggle;
1477 break;
1478 case DOC_ChipID_DocMilPlus32:
Shreeya Patel63fa37f2018-02-22 22:01:22 +05301479 pr_err("DiskOnChip Millennium Plus 32MB is not supported, ignoring.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 default:
1481 ret = -ENODEV;
1482 goto notfound;
1483 }
1484 break;
1485
1486 default:
1487 ret = -ENODEV;
1488 goto notfound;
1489 }
1490 /* Check the TOGGLE bit in the ECC register */
David Woodhousee0c7d762006-05-13 18:07:53 +01001491 tmp = ReadDOC_(virtadr, reg) & DOC_TOGGLE_BIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 tmpb = ReadDOC_(virtadr, reg) & DOC_TOGGLE_BIT;
1493 tmpc = ReadDOC_(virtadr, reg) & DOC_TOGGLE_BIT;
1494 if ((tmp == tmpb) || (tmp != tmpc)) {
Shreeya Patel63fa37f2018-02-22 22:01:22 +05301495 pr_warn("Possible DiskOnChip at 0x%lx failed TOGGLE test, dropping.\n", physadr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 ret = -ENODEV;
1497 goto notfound;
1498 }
1499
1500 for (mtd = doclist; mtd; mtd = doc->nextdoc) {
1501 unsigned char oldval;
1502 unsigned char newval;
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +01001503 nand = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +01001504 doc = nand_get_controller_data(nand);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 /* Use the alias resolution register to determine if this is
1506 in fact the same DOC aliased to a new address. If writes
1507 to one chip's alias resolution register change the value on
1508 the other chip, they're the same chip. */
1509 if (ChipID == DOC_ChipID_DocMilPlus16) {
1510 oldval = ReadDOC(doc->virtadr, Mplus_AliasResolution);
1511 newval = ReadDOC(virtadr, Mplus_AliasResolution);
1512 } else {
1513 oldval = ReadDOC(doc->virtadr, AliasResolution);
1514 newval = ReadDOC(virtadr, AliasResolution);
1515 }
1516 if (oldval != newval)
1517 continue;
1518 if (ChipID == DOC_ChipID_DocMilPlus16) {
1519 WriteDOC(~newval, virtadr, Mplus_AliasResolution);
1520 oldval = ReadDOC(doc->virtadr, Mplus_AliasResolution);
David Woodhousee0c7d762006-05-13 18:07:53 +01001521 WriteDOC(newval, virtadr, Mplus_AliasResolution); // restore it
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 } else {
1523 WriteDOC(~newval, virtadr, AliasResolution);
1524 oldval = ReadDOC(doc->virtadr, AliasResolution);
David Woodhousee0c7d762006-05-13 18:07:53 +01001525 WriteDOC(newval, virtadr, AliasResolution); // restore it
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 }
1527 newval = ~newval;
1528 if (oldval == newval) {
Shreeya Patel63fa37f2018-02-22 22:01:22 +05301529 pr_debug("Found alias of DOC at 0x%lx to 0x%lx\n",
1530 doc->physadr, physadr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 goto notfound;
1532 }
1533 }
1534
Shreeya Patel63fa37f2018-02-22 22:01:22 +05301535 pr_notice("DiskOnChip found at 0x%lx\n", physadr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536
Boris BREZILLONb0c423c2015-12-10 09:00:00 +01001537 len = sizeof(struct nand_chip) + sizeof(struct doc_priv) +
1538 (2 * sizeof(struct nand_bbt_descr));
1539 nand = kzalloc(len, GFP_KERNEL);
1540 if (!nand) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 ret = -ENOMEM;
1542 goto fail;
1543 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544
Thomas Gleixner964dfce2018-04-22 18:23:54 +02001545
1546 /*
1547 * Allocate a RS codec instance
1548 *
1549 * Symbolsize is 10 (bits)
1550 * Primitve polynomial is x^10+x^3+1
1551 * First consecutive root is 510
1552 * Primitve element to generate roots = 1
1553 * Generator polinomial degree = 4
1554 */
1555 doc = (struct doc_priv *) (nand + 1);
1556 doc->rs_decoder = init_rs(10, 0x409, FCR, 1, NROOTS);
1557 if (!doc->rs_decoder) {
1558 pr_err("DiskOnChip: Could not create a RS codec\n");
1559 ret = -ENOMEM;
1560 goto fail;
1561 }
1562
Boris BREZILLONb0c423c2015-12-10 09:00:00 +01001563 mtd = nand_to_mtd(nand);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 nand->bbt_td = (struct nand_bbt_descr *) (doc + 1);
1565 nand->bbt_md = nand->bbt_td + 1;
1566
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 mtd->owner = THIS_MODULE;
Boris Brezillon68c1b752016-02-03 20:00:23 +01001568 mtd_set_ooblayout(mtd, &doc200x_ooblayout_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569
Boris BREZILLONd699ed22015-12-10 09:00:41 +01001570 nand_set_controller_data(nand, doc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 nand->select_chip = doc200x_select_chip;
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +02001572 nand->cmd_ctrl = doc200x_hwcontrol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 nand->dev_ready = doc200x_dev_ready;
1574 nand->waitfunc = doc200x_wait;
1575 nand->block_bad = doc200x_block_bad;
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02001576 nand->ecc.hwctl = doc200x_enable_hwecc;
1577 nand->ecc.calculate = doc200x_calculate_ecc;
1578 nand->ecc.correct = doc200x_correct_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02001580 nand->ecc.mode = NAND_ECC_HW_SYNDROME;
1581 nand->ecc.size = 512;
1582 nand->ecc.bytes = 6;
Mike Dunn6a918ba2012-03-11 14:21:11 -07001583 nand->ecc.strength = 2;
Boris BREZILLONcc01e602015-12-30 20:39:52 +01001584 nand->ecc.options = NAND_ECC_GENERIC_ERASED_CHECK;
Brian Norrisbb9ebd42011-05-31 16:31:23 -07001585 nand->bbt_options = NAND_BBT_USE_FLASH;
Brian Norrisd24fe0c2015-02-28 02:13:10 -08001586 /* Skip the automatic BBT scan so we can run it manually */
1587 nand->options |= NAND_SKIP_BBTSCAN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588
1589 doc->physadr = physadr;
1590 doc->virtadr = virtadr;
1591 doc->ChipID = ChipID;
1592 doc->curfloor = -1;
1593 doc->curchip = -1;
1594 doc->mh0_page = -1;
1595 doc->mh1_page = -1;
1596 doc->nextdoc = doclist;
1597
1598 if (ChipID == DOC_ChipID_Doc2k)
1599 numchips = doc2000_init(mtd);
1600 else if (ChipID == DOC_ChipID_DocMilPlus16)
1601 numchips = doc2001plus_init(mtd);
1602 else
1603 numchips = doc2001_init(mtd);
1604
Boris Brezillon00ad3782018-09-06 14:05:14 +02001605 if ((ret = nand_scan(nand, numchips)) || (ret = doc->late_init(mtd))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 /* DBB note: i believe nand_release is necessary here, as
1607 buffers may have been allocated in nand_base. Check with
1608 Thomas. FIX ME! */
Jamie Iles0f47e952011-05-23 10:23:19 +01001609 /* nand_release will call mtd_device_unregister, but we
1610 haven't yet added it. This is handled without incident by
1611 mtd_device_unregister, as far as I can tell. */
Boris Brezillon59ac2762018-09-06 14:05:15 +02001612 nand_release(nand);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 goto fail;
1614 }
1615
1616 /* Success! */
1617 doclist = mtd;
1618 return 0;
1619
David Woodhousee0c7d762006-05-13 18:07:53 +01001620 notfound:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 /* Put back the contents of the DOCControl register, in case it's not
1622 actually a DiskOnChip. */
1623 WriteDOC(save_control, virtadr, DOCControl);
David Woodhousee0c7d762006-05-13 18:07:53 +01001624 fail:
Thomas Gleixner964dfce2018-04-22 18:23:54 +02001625 if (doc)
1626 free_rs(doc->rs_decoder);
1627 kfree(nand);
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;
Boris Brezillon59ac2762018-09-06 14:05:15 +02001647 nand_release(nand);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 iounmap(doc->virtadr);
Alexander Shiyan4f0614a2013-11-13 15:59:25 +04001649 release_mem_region(doc->physadr, DOC_IOREMAP_LEN);
Thomas Gleixner964dfce2018-04-22 18:23:54 +02001650 free_rs(doc->rs_decoder);
Boris BREZILLONb0c423c2015-12-10 09:00:00 +01001651 kfree(nand);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 }
1653}
1654
1655static int __init init_nanddoc(void)
1656{
1657 int i, ret = 0;
1658
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 if (doc_config_location) {
Shreeya Patel63fa37f2018-02-22 22:01:22 +05301660 pr_info("Using configured DiskOnChip probe address 0x%lx\n",
1661 doc_config_location);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 ret = doc_probe(doc_config_location);
1663 if (ret < 0)
Thomas Gleixner964dfce2018-04-22 18:23:54 +02001664 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 } else {
David Woodhousee0c7d762006-05-13 18:07:53 +01001666 for (i = 0; (doc_locations[i] != 0xffffffff); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 doc_probe(doc_locations[i]);
1668 }
1669 }
1670 /* No banner message any more. Print a message if no DiskOnChip
1671 found, so the user knows we at least tried. */
1672 if (!doclist) {
Shreeya Patel63fa37f2018-02-22 22:01:22 +05301673 pr_info("No valid DiskOnChip devices found\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 ret = -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 return ret;
1677}
1678
1679static void __exit cleanup_nanddoc(void)
1680{
1681 /* Cleanup the nand/DoC resources */
1682 release_nanddoc();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683}
1684
1685module_init(init_nanddoc);
1686module_exit(cleanup_nanddoc);
1687
1688MODULE_LICENSE("GPL");
1689MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
Niels de Vos2a7af8c2009-01-30 11:08:35 +01001690MODULE_DESCRIPTION("M-Systems DiskOnChip 2000, Millennium and Millennium Plus device driver");