diskonchip.c 46.9 KB
Newer Older
1
/*
L
Linus Torvalds 已提交
2 3 4 5 6 7 8
 * (C) 2003 Red Hat, Inc.
 * (C) 2004 Dan Brown <dan_brown@ieee.org>
 * (C) 2004 Kalev Lember <kalev@smartlink.ee>
 *
 * Author: David Woodhouse <dwmw2@infradead.org>
 * Additional Diskonchip 2000 and Millennium support by Dan Brown <dan_brown@ieee.org>
 * Diskonchip Millennium Plus support by Kalev Lember <kalev@smartlink.ee>
9
 *
L
Linus Torvalds 已提交
10
 * Error correction code lifted from the old docecc code
11
 * Author: Fabrice Bellard (fabrice.bellard@netgem.com)
L
Linus Torvalds 已提交
12 13
 * Copyright (C) 2000 Netgem S.A.
 * converted to the generic Reed-Solomon library by Thomas Gleixner <tglx@linutronix.de>
14
 *
L
Linus Torvalds 已提交
15 16 17 18 19 20 21 22 23
 * Interface to generic NAND code for M-Systems DiskOnChip devices
 */

#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/sched.h>
#include <linux/delay.h>
#include <linux/rslib.h>
#include <linux/moduleparam.h>
24
#include <linux/slab.h>
25
#include <linux/io.h>
L
Linus Torvalds 已提交
26 27

#include <linux/mtd/mtd.h>
28
#include <linux/mtd/rawnand.h>
L
Linus Torvalds 已提交
29 30 31
#include <linux/mtd/doc2000.h>
#include <linux/mtd/partitions.h>
#include <linux/mtd/inftl.h>
32
#include <linux/module.h>
L
Linus Torvalds 已提交
33 34

/* Where to look for the devices? */
35 36
#ifndef CONFIG_MTD_NAND_DISKONCHIP_PROBE_ADDRESS
#define CONFIG_MTD_NAND_DISKONCHIP_PROBE_ADDRESS 0
L
Linus Torvalds 已提交
37 38
#endif

39
static unsigned long doc_locations[] __initdata = {
L
Linus Torvalds 已提交
40
#if defined (__alpha__) || defined(__i386__) || defined(__x86_64__)
41
#ifdef CONFIG_MTD_NAND_DISKONCHIP_PROBE_HIGH
42
	0xfffc8000, 0xfffca000, 0xfffcc000, 0xfffce000,
L
Linus Torvalds 已提交
43
	0xfffd0000, 0xfffd2000, 0xfffd4000, 0xfffd6000,
44 45
	0xfffd8000, 0xfffda000, 0xfffdc000, 0xfffde000,
	0xfffe0000, 0xfffe2000, 0xfffe4000, 0xfffe6000,
L
Linus Torvalds 已提交
46
	0xfffe8000, 0xfffea000, 0xfffec000, 0xfffee000,
47
#else
48
	0xc8000, 0xca000, 0xcc000, 0xce000,
L
Linus Torvalds 已提交
49
	0xd0000, 0xd2000, 0xd4000, 0xd6000,
50 51
	0xd8000, 0xda000, 0xdc000, 0xde000,
	0xe0000, 0xe2000, 0xe4000, 0xe6000,
L
Linus Torvalds 已提交
52
	0xe8000, 0xea000, 0xec000, 0xee000,
53
#endif
L
Linus Torvalds 已提交
54 55 56 57 58 59 60 61 62 63
#endif
	0xffffffff };

static struct mtd_info *doclist = NULL;

struct doc_priv {
	void __iomem *virtadr;
	unsigned long physadr;
	u_char ChipID;
	u_char CDSNControl;
64
	int chips_per_floor;	/* The number of chips detected on each floor */
L
Linus Torvalds 已提交
65 66 67 68
	int curfloor;
	int curchip;
	int mh0_page;
	int mh1_page;
69
	struct rs_control *rs_decoder;
L
Linus Torvalds 已提交
70
	struct mtd_info *nextdoc;
71 72 73

	/* Handle the last stage of initialization (BBT scan, partitioning) */
	int (*late_init)(struct mtd_info *mtd);
L
Linus Torvalds 已提交
74 75 76 77 78 79 80 81 82 83 84 85
};

/* This is the ecc value computed by the HW ecc generator upon writing an empty
   page, one with all 0xff for data. */
static u_char empty_write_ecc[6] = { 0x4b, 0x00, 0xe2, 0x0e, 0x93, 0xf7 };

#define INFTL_BBT_RESERVED_BLOCKS 4

#define DoC_is_MillenniumPlus(doc) ((doc)->ChipID == DOC_ChipID_DocMilPlus16 || (doc)->ChipID == DOC_ChipID_DocMilPlus32)
#define DoC_is_Millennium(doc) ((doc)->ChipID == DOC_ChipID_DocMil)
#define DoC_is_2000(doc) ((doc)->ChipID == DOC_ChipID_Doc2k)

86 87
static void doc200x_hwcontrol(struct mtd_info *mtd, int cmd,
			      unsigned int bitmask);
L
Linus Torvalds 已提交
88 89
static void doc200x_select_chip(struct mtd_info *mtd, int chip);

90
static int debug = 0;
L
Linus Torvalds 已提交
91 92
module_param(debug, int, 0);

93
static int try_dword = 1;
L
Linus Torvalds 已提交
94 95
module_param(try_dword, int, 0);

96
static int no_ecc_failures = 0;
L
Linus Torvalds 已提交
97 98
module_param(no_ecc_failures, int, 0);

99
static int no_autopart = 0;
L
Linus Torvalds 已提交
100
module_param(no_autopart, int, 0);
101

102
static int show_firmware_partition = 0;
103
module_param(show_firmware_partition, int, 0);
L
Linus Torvalds 已提交
104

105
#ifdef CONFIG_MTD_NAND_DISKONCHIP_BBTWRITE
106
static int inftl_bbt_write = 1;
L
Linus Torvalds 已提交
107
#else
108
static int inftl_bbt_write = 0;
L
Linus Torvalds 已提交
109 110 111
#endif
module_param(inftl_bbt_write, int, 0);

112
static unsigned long doc_config_location = CONFIG_MTD_NAND_DISKONCHIP_PROBE_ADDRESS;
L
Linus Torvalds 已提交
113 114 115 116 117 118 119 120 121 122 123 124 125 126
module_param(doc_config_location, ulong, 0);
MODULE_PARM_DESC(doc_config_location, "Physical memory address at which to probe for DiskOnChip");

/* Sector size for HW ECC */
#define SECTOR_SIZE 512
/* The sector bytes are packed into NB_DATA 10 bit words */
#define NB_DATA (((SECTOR_SIZE + 1) * 8 + 6) / 10)
/* Number of roots */
#define NROOTS 4
/* First consective root */
#define FCR 510
/* Number of symbols */
#define NN 1023

127
/*
L
Linus Torvalds 已提交
128
 * The HW decoder in the DoC ASIC's provides us a error syndrome,
129
 * which we must convert to a standard syndrome usable by the generic
L
Linus Torvalds 已提交
130 131 132 133
 * Reed-Solomon library code.
 *
 * Fabrice Bellard figured this out in the old docecc code. I added
 * some comments, improved a minor bit and converted it to make use
L
Lucas De Marchi 已提交
134
 * of the generic Reed-Solomon library. tglx
L
Linus Torvalds 已提交
135
 */
136
static int doc_ecc_decode(struct rs_control *rs, uint8_t *data, uint8_t *ecc)
L
Linus Torvalds 已提交
137 138 139 140
{
	int i, j, nerr, errpos[8];
	uint8_t parity;
	uint16_t ds[4], s[5], tmp, errval[8], syn[4];
T
Thomas Gleixner 已提交
141
	struct rs_codec *cd = rs->codec;
L
Linus Torvalds 已提交
142

143
	memset(syn, 0, sizeof(syn));
L
Linus Torvalds 已提交
144 145 146 147 148 149 150
	/* Convert the ecc bytes into words */
	ds[0] = ((ecc[4] & 0xff) >> 0) | ((ecc[5] & 0x03) << 8);
	ds[1] = ((ecc[5] & 0xfc) >> 2) | ((ecc[2] & 0x0f) << 6);
	ds[2] = ((ecc[2] & 0xf0) >> 4) | ((ecc[3] & 0x3f) << 4);
	ds[3] = ((ecc[3] & 0xc0) >> 6) | ((ecc[0] & 0xff) << 2);
	parity = ecc[1];

151
	/* Initialize the syndrome buffer */
L
Linus Torvalds 已提交
152 153
	for (i = 0; i < NROOTS; i++)
		s[i] = ds[0];
154 155
	/*
	 *  Evaluate
L
Linus Torvalds 已提交
156 157 158
	 *  s[i] = ds[3]x^3 + ds[2]x^2 + ds[1]x^1 + ds[0]
	 *  where x = alpha^(FCR + i)
	 */
159 160
	for (j = 1; j < NROOTS; j++) {
		if (ds[j] == 0)
L
Linus Torvalds 已提交
161
			continue;
T
Thomas Gleixner 已提交
162
		tmp = cd->index_of[ds[j]];
163
		for (i = 0; i < NROOTS; i++)
T
Thomas Gleixner 已提交
164
			s[i] ^= cd->alpha_to[rs_modnn(cd, tmp + (FCR + i) * j)];
L
Linus Torvalds 已提交
165 166
	}

167
	/* Calc syn[i] = s[i] / alpha^(v + i) */
L
Linus Torvalds 已提交
168
	for (i = 0; i < NROOTS; i++) {
169
		if (s[i])
T
Thomas Gleixner 已提交
170
			syn[i] = rs_modnn(cd, cd->index_of[s[i]] + (NN - FCR - i));
L
Linus Torvalds 已提交
171 172 173 174 175 176 177 178
	}
	/* Call the decoder library */
	nerr = decode_rs16(rs, NULL, NULL, 1019, syn, 0, errpos, 0, errval);

	/* Incorrectable errors ? */
	if (nerr < 0)
		return nerr;

179
	/*
L
Linus Torvalds 已提交
180 181 182 183
	 * Correct the errors. The bitpositions are a bit of magic,
	 * but they are given by the design of the de/encoder circuit
	 * in the DoC ASIC's.
	 */
184
	for (i = 0; i < nerr; i++) {
L
Linus Torvalds 已提交
185 186 187 188 189 190 191 192 193 194 195
		int index, bitpos, pos = 1015 - errpos[i];
		uint8_t val;
		if (pos >= NB_DATA && pos < 1019)
			continue;
		if (pos < NB_DATA) {
			/* extract bit position (MSB first) */
			pos = 10 * (NB_DATA - 1 - pos) - 6;
			/* now correct the following 10 bits. At most two bytes
			   can be modified since pos is even */
			index = (pos >> 3) ^ 1;
			bitpos = pos & 7;
196
			if ((index >= 0 && index < SECTOR_SIZE) || index == (SECTOR_SIZE + 1)) {
L
Linus Torvalds 已提交
197 198 199 200 201 202 203 204 205
				val = (uint8_t) (errval[i] >> (2 + bitpos));
				parity ^= val;
				if (index < SECTOR_SIZE)
					data[index] ^= val;
			}
			index = ((pos >> 3) + 1) ^ 1;
			bitpos = (bitpos + 10) & 7;
			if (bitpos == 0)
				bitpos = 8;
206 207
			if ((index >= 0 && index < SECTOR_SIZE) || index == (SECTOR_SIZE + 1)) {
				val = (uint8_t) (errval[i] << (8 - bitpos));
L
Linus Torvalds 已提交
208 209 210 211 212 213 214
				parity ^= val;
				if (index < SECTOR_SIZE)
					data[index] ^= val;
			}
		}
	}
	/* If the parity is wrong, no rescue possible */
215
	return parity ? -EBADMSG : nerr;
L
Linus Torvalds 已提交
216 217 218 219 220 221
}

static void DoC_Delay(struct doc_priv *doc, unsigned short cycles)
{
	volatile char dummy;
	int i;
222

L
Linus Torvalds 已提交
223 224 225 226 227 228 229 230
	for (i = 0; i < cycles; i++) {
		if (DoC_is_Millennium(doc))
			dummy = ReadDOC(doc->virtadr, NOP);
		else if (DoC_is_MillenniumPlus(doc))
			dummy = ReadDOC(doc->virtadr, Mplus_NOP);
		else
			dummy = ReadDOC(doc->virtadr, DOCStatus);
	}
231

L
Linus Torvalds 已提交
232 233 234 235 236 237 238
}

#define CDSN_CTRL_FR_B_MASK	(CDSN_CTRL_FR_B0 | CDSN_CTRL_FR_B1)

/* DOC_WaitReady: Wait for RDY line to be asserted by the flash chip */
static int _DoC_WaitReady(struct doc_priv *doc)
{
239
	void __iomem *docptr = doc->virtadr;
L
Linus Torvalds 已提交
240 241
	unsigned long timeo = jiffies + (HZ * 10);

242 243
	if (debug)
		printk("_DoC_WaitReady...\n");
L
Linus Torvalds 已提交
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
	/* Out-of-line routine to wait for chip response */
	if (DoC_is_MillenniumPlus(doc)) {
		while ((ReadDOC(docptr, Mplus_FlashControl) & CDSN_CTRL_FR_B_MASK) != CDSN_CTRL_FR_B_MASK) {
			if (time_after(jiffies, timeo)) {
				printk("_DoC_WaitReady timed out.\n");
				return -EIO;
			}
			udelay(1);
			cond_resched();
		}
	} else {
		while (!(ReadDOC(docptr, CDSNControl) & CDSN_CTRL_FR_B)) {
			if (time_after(jiffies, timeo)) {
				printk("_DoC_WaitReady timed out.\n");
				return -EIO;
			}
			udelay(1);
			cond_resched();
		}
	}

	return 0;
}

static inline int DoC_WaitReady(struct doc_priv *doc)
{
270
	void __iomem *docptr = doc->virtadr;
L
Linus Torvalds 已提交
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
	int ret = 0;

	if (DoC_is_MillenniumPlus(doc)) {
		DoC_Delay(doc, 4);

		if ((ReadDOC(docptr, Mplus_FlashControl) & CDSN_CTRL_FR_B_MASK) != CDSN_CTRL_FR_B_MASK)
			/* Call the out-of-line routine to wait */
			ret = _DoC_WaitReady(doc);
	} else {
		DoC_Delay(doc, 4);

		if (!(ReadDOC(docptr, CDSNControl) & CDSN_CTRL_FR_B))
			/* Call the out-of-line routine to wait */
			ret = _DoC_WaitReady(doc);
		DoC_Delay(doc, 2);
	}

288 289
	if (debug)
		printk("DoC_WaitReady OK\n");
L
Linus Torvalds 已提交
290 291 292
	return ret;
}

293
static void doc2000_write_byte(struct nand_chip *this, u_char datum)
L
Linus Torvalds 已提交
294
{
295
	struct doc_priv *doc = nand_get_controller_data(this);
296
	void __iomem *docptr = doc->virtadr;
L
Linus Torvalds 已提交
297

298 299
	if (debug)
		printk("write_byte %02x\n", datum);
L
Linus Torvalds 已提交
300 301 302 303
	WriteDOC(datum, docptr, CDSNSlowIO);
	WriteDOC(datum, docptr, 2k_CDSN_IO);
}

304
static u_char doc2000_read_byte(struct nand_chip *this)
L
Linus Torvalds 已提交
305
{
306
	struct doc_priv *doc = nand_get_controller_data(this);
307
	void __iomem *docptr = doc->virtadr;
L
Linus Torvalds 已提交
308 309 310 311 312
	u_char ret;

	ReadDOC(docptr, CDSNSlowIO);
	DoC_Delay(doc, 2);
	ret = ReadDOC(docptr, 2k_CDSN_IO);
313 314
	if (debug)
		printk("read_byte returns %02x\n", ret);
L
Linus Torvalds 已提交
315 316 317
	return ret;
}

318 319
static void doc2000_writebuf(struct nand_chip *this, const u_char *buf,
			     int len)
L
Linus Torvalds 已提交
320
{
321
	struct doc_priv *doc = nand_get_controller_data(this);
322
	void __iomem *docptr = doc->virtadr;
L
Linus Torvalds 已提交
323
	int i;
324 325 326
	if (debug)
		printk("writebuf of %d bytes: ", len);
	for (i = 0; i < len; i++) {
L
Linus Torvalds 已提交
327 328 329 330
		WriteDOC_(buf[i], docptr, DoC_2k_CDSN_IO + i);
		if (debug && i < 16)
			printk("%02x ", buf[i]);
	}
331 332
	if (debug)
		printk("\n");
L
Linus Torvalds 已提交
333 334
}

335
static void doc2000_readbuf(struct nand_chip *this, u_char *buf, int len)
L
Linus Torvalds 已提交
336
{
337
	struct doc_priv *doc = nand_get_controller_data(this);
338 339
	void __iomem *docptr = doc->virtadr;
	int i;
L
Linus Torvalds 已提交
340

341 342
	if (debug)
		printk("readbuf of %d bytes: ", len);
L
Linus Torvalds 已提交
343

344
	for (i = 0; i < len; i++)
L
Linus Torvalds 已提交
345 346 347
		buf[i] = ReadDOC(docptr, 2k_CDSN_IO + i);
}

348
static void doc2000_readbuf_dword(struct nand_chip *this, u_char *buf, int len)
L
Linus Torvalds 已提交
349
{
350
	struct doc_priv *doc = nand_get_controller_data(this);
351 352
	void __iomem *docptr = doc->virtadr;
	int i;
L
Linus Torvalds 已提交
353

354 355
	if (debug)
		printk("readbuf_dword of %d bytes: ", len);
L
Linus Torvalds 已提交
356

357 358 359
	if (unlikely((((unsigned long)buf) | len) & 3)) {
		for (i = 0; i < len; i++) {
			*(uint8_t *) (&buf[i]) = ReadDOC(docptr, 2k_CDSN_IO + i);
L
Linus Torvalds 已提交
360 361
		}
	} else {
362 363
		for (i = 0; i < len; i += 4) {
			*(uint32_t *) (&buf[i]) = readl(docptr + DoC_2k_CDSN_IO + i);
L
Linus Torvalds 已提交
364 365 366 367 368 369
		}
	}
}

static uint16_t __init doc200x_ident_chip(struct mtd_info *mtd, int nr)
{
370
	struct nand_chip *this = mtd_to_nand(mtd);
371
	struct doc_priv *doc = nand_get_controller_data(this);
L
Linus Torvalds 已提交
372 373 374
	uint16_t ret;

	doc200x_select_chip(mtd, nr);
375 376 377 378
	doc200x_hwcontrol(mtd, NAND_CMD_READID,
			  NAND_CTRL_CLE | NAND_CTRL_CHANGE);
	doc200x_hwcontrol(mtd, 0, NAND_CTRL_ALE | NAND_CTRL_CHANGE);
	doc200x_hwcontrol(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
379

380
	/* We can't use dev_ready here, but at least we wait for the
381
	 * command to complete
382 383
	 */
	udelay(50);
384

385 386
	ret = this->read_byte(this) << 8;
	ret |= this->read_byte(this);
L
Linus Torvalds 已提交
387 388 389 390 391 392 393 394 395

	if (doc->ChipID == DOC_ChipID_Doc2k && try_dword && !nr) {
		/* First chip probe. See if we get same results by 32-bit access */
		union {
			uint32_t dword;
			uint8_t byte[4];
		} ident;
		void __iomem *docptr = doc->virtadr;

396 397 398 399 400
		doc200x_hwcontrol(mtd, NAND_CMD_READID,
				  NAND_CTRL_CLE | NAND_CTRL_CHANGE);
		doc200x_hwcontrol(mtd, 0, NAND_CTRL_ALE | NAND_CTRL_CHANGE);
		doc200x_hwcontrol(mtd, NAND_CMD_NONE,
				  NAND_NCE | NAND_CTRL_CHANGE);
L
Linus Torvalds 已提交
401

402 403
		udelay(50);

L
Linus Torvalds 已提交
404 405
		ident.dword = readl(docptr + DoC_2k_CDSN_IO);
		if (((ident.byte[0] << 8) | ident.byte[1]) == ret) {
406
			pr_info("DiskOnChip 2000 responds to DWORD access\n");
L
Linus Torvalds 已提交
407 408 409
			this->read_buf = &doc2000_readbuf_dword;
		}
	}
410

L
Linus Torvalds 已提交
411 412 413 414 415
	return ret;
}

static void __init doc2000_count_chips(struct mtd_info *mtd)
{
416
	struct nand_chip *this = mtd_to_nand(mtd);
417
	struct doc_priv *doc = nand_get_controller_data(this);
L
Linus Torvalds 已提交
418 419 420 421 422 423 424 425 426 427 428 429 430 431 432
	uint16_t mfrid;
	int i;

	/* Max 4 chips per floor on DiskOnChip 2000 */
	doc->chips_per_floor = 4;

	/* Find out what the first chip is */
	mfrid = doc200x_ident_chip(mtd, 0);

	/* Find how many chips in each floor. */
	for (i = 1; i < 4; i++) {
		if (doc200x_ident_chip(mtd, i) != mfrid)
			break;
	}
	doc->chips_per_floor = i;
433
	pr_debug("Detected %d chips per floor.\n", i);
L
Linus Torvalds 已提交
434 435
}

436
static int doc200x_wait(struct mtd_info *mtd, struct nand_chip *this)
L
Linus Torvalds 已提交
437
{
438
	struct doc_priv *doc = nand_get_controller_data(this);
L
Linus Torvalds 已提交
439 440

	int status;
441

L
Linus Torvalds 已提交
442
	DoC_WaitReady(doc);
443
	nand_status_op(this, NULL);
L
Linus Torvalds 已提交
444
	DoC_WaitReady(doc);
445
	status = (int)this->read_byte(this);
L
Linus Torvalds 已提交
446 447 448 449

	return status;
}

450
static void doc2001_write_byte(struct nand_chip *this, u_char datum)
L
Linus Torvalds 已提交
451
{
452
	struct doc_priv *doc = nand_get_controller_data(this);
453
	void __iomem *docptr = doc->virtadr;
L
Linus Torvalds 已提交
454 455 456 457 458 459

	WriteDOC(datum, docptr, CDSNSlowIO);
	WriteDOC(datum, docptr, Mil_CDSN_IO);
	WriteDOC(datum, docptr, WritePipeTerm);
}

460
static u_char doc2001_read_byte(struct nand_chip *this)
L
Linus Torvalds 已提交
461
{
462
	struct doc_priv *doc = nand_get_controller_data(this);
463
	void __iomem *docptr = doc->virtadr;
L
Linus Torvalds 已提交
464 465 466 467 468 469 470 471 472

	//ReadDOC(docptr, CDSNSlowIO);
	/* 11.4.5 -- delay twice to allow extended length cycle */
	DoC_Delay(doc, 2);
	ReadDOC(docptr, ReadPipeInit);
	//return ReadDOC(docptr, Mil_CDSN_IO);
	return ReadDOC(docptr, LastDataRead);
}

473
static void doc2001_writebuf(struct nand_chip *this, const u_char *buf, int len)
L
Linus Torvalds 已提交
474
{
475
	struct doc_priv *doc = nand_get_controller_data(this);
476
	void __iomem *docptr = doc->virtadr;
L
Linus Torvalds 已提交
477 478
	int i;

479
	for (i = 0; i < len; i++)
L
Linus Torvalds 已提交
480 481 482 483 484
		WriteDOC_(buf[i], docptr, DoC_Mil_CDSN_IO + i);
	/* Terminate write pipeline */
	WriteDOC(0x00, docptr, WritePipeTerm);
}

485
static void doc2001_readbuf(struct nand_chip *this, u_char *buf, int len)
L
Linus Torvalds 已提交
486
{
487
	struct doc_priv *doc = nand_get_controller_data(this);
488
	void __iomem *docptr = doc->virtadr;
L
Linus Torvalds 已提交
489 490 491 492 493
	int i;

	/* Start read pipeline */
	ReadDOC(docptr, ReadPipeInit);

494
	for (i = 0; i < len - 1; i++)
L
Linus Torvalds 已提交
495 496 497 498 499 500
		buf[i] = ReadDOC(docptr, Mil_CDSN_IO + (i & 0xff));

	/* Terminate read pipeline */
	buf[i] = ReadDOC(docptr, LastDataRead);
}

501
static u_char doc2001plus_read_byte(struct nand_chip *this)
L
Linus Torvalds 已提交
502
{
503
	struct doc_priv *doc = nand_get_controller_data(this);
504
	void __iomem *docptr = doc->virtadr;
L
Linus Torvalds 已提交
505 506
	u_char ret;

507 508 509 510 511
	ReadDOC(docptr, Mplus_ReadPipeInit);
	ReadDOC(docptr, Mplus_ReadPipeInit);
	ret = ReadDOC(docptr, Mplus_LastDataRead);
	if (debug)
		printk("read_byte returns %02x\n", ret);
L
Linus Torvalds 已提交
512 513 514
	return ret;
}

515
static void doc2001plus_writebuf(struct nand_chip *this, const u_char *buf, int len)
L
Linus Torvalds 已提交
516
{
517
	struct doc_priv *doc = nand_get_controller_data(this);
518
	void __iomem *docptr = doc->virtadr;
L
Linus Torvalds 已提交
519 520
	int i;

521 522 523
	if (debug)
		printk("writebuf of %d bytes: ", len);
	for (i = 0; i < len; i++) {
L
Linus Torvalds 已提交
524 525 526 527
		WriteDOC_(buf[i], docptr, DoC_Mil_CDSN_IO + i);
		if (debug && i < 16)
			printk("%02x ", buf[i]);
	}
528 529
	if (debug)
		printk("\n");
L
Linus Torvalds 已提交
530 531
}

532
static void doc2001plus_readbuf(struct nand_chip *this, u_char *buf, int len)
L
Linus Torvalds 已提交
533
{
534
	struct doc_priv *doc = nand_get_controller_data(this);
535
	void __iomem *docptr = doc->virtadr;
L
Linus Torvalds 已提交
536 537
	int i;

538 539
	if (debug)
		printk("readbuf of %d bytes: ", len);
L
Linus Torvalds 已提交
540 541 542 543 544

	/* Start read pipeline */
	ReadDOC(docptr, Mplus_ReadPipeInit);
	ReadDOC(docptr, Mplus_ReadPipeInit);

545
	for (i = 0; i < len - 2; i++) {
L
Linus Torvalds 已提交
546 547 548 549 550 551
		buf[i] = ReadDOC(docptr, Mil_CDSN_IO);
		if (debug && i < 16)
			printk("%02x ", buf[i]);
	}

	/* Terminate read pipeline */
552
	buf[len - 2] = ReadDOC(docptr, Mplus_LastDataRead);
L
Linus Torvalds 已提交
553
	if (debug && i < 16)
554 555
		printk("%02x ", buf[len - 2]);
	buf[len - 1] = ReadDOC(docptr, Mplus_LastDataRead);
L
Linus Torvalds 已提交
556
	if (debug && i < 16)
557 558 559
		printk("%02x ", buf[len - 1]);
	if (debug)
		printk("\n");
L
Linus Torvalds 已提交
560 561 562 563
}

static void doc2001plus_select_chip(struct mtd_info *mtd, int chip)
{
564
	struct nand_chip *this = mtd_to_nand(mtd);
565
	struct doc_priv *doc = nand_get_controller_data(this);
566
	void __iomem *docptr = doc->virtadr;
L
Linus Torvalds 已提交
567 568
	int floor = 0;

569 570
	if (debug)
		printk("select chip (%d)\n", chip);
L
Linus Torvalds 已提交
571 572 573 574 575 576 577 578

	if (chip == -1) {
		/* Disable flash internally */
		WriteDOC(0, docptr, Mplus_FlashSelect);
		return;
	}

	floor = chip / doc->chips_per_floor;
579
	chip -= (floor * doc->chips_per_floor);
L
Linus Torvalds 已提交
580 581 582

	/* Assert ChipEnable and deassert WriteProtect */
	WriteDOC((DOC_FLASH_CE), docptr, Mplus_FlashSelect);
583
	nand_reset_op(this);
L
Linus Torvalds 已提交
584 585 586 587 588 589 590

	doc->curchip = chip;
	doc->curfloor = floor;
}

static void doc200x_select_chip(struct mtd_info *mtd, int chip)
{
591
	struct nand_chip *this = mtd_to_nand(mtd);
592
	struct doc_priv *doc = nand_get_controller_data(this);
593
	void __iomem *docptr = doc->virtadr;
L
Linus Torvalds 已提交
594 595
	int floor = 0;

596 597
	if (debug)
		printk("select chip (%d)\n", chip);
L
Linus Torvalds 已提交
598 599 600 601 602

	if (chip == -1)
		return;

	floor = chip / doc->chips_per_floor;
603
	chip -= (floor * doc->chips_per_floor);
L
Linus Torvalds 已提交
604 605

	/* 11.4.4 -- deassert CE before changing chip */
606
	doc200x_hwcontrol(mtd, NAND_CMD_NONE, 0 | NAND_CTRL_CHANGE);
L
Linus Torvalds 已提交
607 608 609 610

	WriteDOC(floor, docptr, FloorSelect);
	WriteDOC(chip, docptr, CDSNDeviceSelect);

611
	doc200x_hwcontrol(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
L
Linus Torvalds 已提交
612 613 614 615 616

	doc->curchip = chip;
	doc->curfloor = floor;
}

617 618 619 620
#define CDSN_CTRL_MSK (CDSN_CTRL_CE | CDSN_CTRL_CLE | CDSN_CTRL_ALE)

static void doc200x_hwcontrol(struct mtd_info *mtd, int cmd,
			      unsigned int ctrl)
L
Linus Torvalds 已提交
621
{
622
	struct nand_chip *this = mtd_to_nand(mtd);
623
	struct doc_priv *doc = nand_get_controller_data(this);
624
	void __iomem *docptr = doc->virtadr;
L
Linus Torvalds 已提交
625

626 627 628 629 630 631 632 633
	if (ctrl & NAND_CTRL_CHANGE) {
		doc->CDSNControl &= ~CDSN_CTRL_MSK;
		doc->CDSNControl |= ctrl & CDSN_CTRL_MSK;
		if (debug)
			printk("hwcontrol(%d): %02x\n", cmd, doc->CDSNControl);
		WriteDOC(doc->CDSNControl, docptr, CDSNControl);
		/* 11.4.3 -- 4 NOPs after CSDNControl write */
		DoC_Delay(doc, 4);
L
Linus Torvalds 已提交
634
	}
635 636
	if (cmd != NAND_CMD_NONE) {
		if (DoC_is_2000(doc))
637
			doc2000_write_byte(this, cmd);
638
		else
639
			doc2001_write_byte(this, cmd);
640
	}
L
Linus Torvalds 已提交
641 642
}

643
static void doc2001plus_command(struct mtd_info *mtd, unsigned command, int column, int page_addr)
L
Linus Torvalds 已提交
644
{
645
	struct nand_chip *this = mtd_to_nand(mtd);
646
	struct doc_priv *doc = nand_get_controller_data(this);
647
	void __iomem *docptr = doc->virtadr;
L
Linus Torvalds 已提交
648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663

	/*
	 * Must terminate write pipeline before sending any commands
	 * to the device.
	 */
	if (command == NAND_CMD_PAGEPROG) {
		WriteDOC(0x00, docptr, Mplus_WritePipeTerm);
		WriteDOC(0x00, docptr, Mplus_WritePipeTerm);
	}

	/*
	 * Write out the command to the device.
	 */
	if (command == NAND_CMD_SEQIN) {
		int readcmd;

J
Joern Engel 已提交
664
		if (column >= mtd->writesize) {
L
Linus Torvalds 已提交
665
			/* OOB area */
J
Joern Engel 已提交
666
			column -= mtd->writesize;
L
Linus Torvalds 已提交
667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684
			readcmd = NAND_CMD_READOOB;
		} else if (column < 256) {
			/* First 256 bytes --> READ0 */
			readcmd = NAND_CMD_READ0;
		} else {
			column -= 256;
			readcmd = NAND_CMD_READ1;
		}
		WriteDOC(readcmd, docptr, Mplus_FlashCmd);
	}
	WriteDOC(command, docptr, Mplus_FlashCmd);
	WriteDOC(0, docptr, Mplus_WritePipeTerm);
	WriteDOC(0, docptr, Mplus_WritePipeTerm);

	if (column != -1 || page_addr != -1) {
		/* Serially input address */
		if (column != -1) {
			/* Adjust columns for 16 bit buswidth */
685 686
			if (this->options & NAND_BUSWIDTH_16 &&
					!nand_opcode_8bits(command))
L
Linus Torvalds 已提交
687 688 689 690
				column >>= 1;
			WriteDOC(column, docptr, Mplus_FlashAddress);
		}
		if (page_addr != -1) {
691 692
			WriteDOC((unsigned char)(page_addr & 0xff), docptr, Mplus_FlashAddress);
			WriteDOC((unsigned char)((page_addr >> 8) & 0xff), docptr, Mplus_FlashAddress);
693
			if (this->options & NAND_ROW_ADDR_3) {
694
				WriteDOC((unsigned char)((page_addr >> 16) & 0x0f), docptr, Mplus_FlashAddress);
L
Linus Torvalds 已提交
695 696 697 698 699 700
				printk("high density\n");
			}
		}
		WriteDOC(0, docptr, Mplus_WritePipeTerm);
		WriteDOC(0, docptr, Mplus_WritePipeTerm);
		/* deassert ALE */
701 702
		if (command == NAND_CMD_READ0 || command == NAND_CMD_READ1 ||
		    command == NAND_CMD_READOOB || command == NAND_CMD_READID)
L
Linus Torvalds 已提交
703 704 705
			WriteDOC(0, docptr, Mplus_FlashControl);
	}

706
	/*
L
Linus Torvalds 已提交
707 708
	 * program and erase have their own busy handlers
	 * status and sequential in needs no delay
709
	 */
L
Linus Torvalds 已提交
710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725
	switch (command) {

	case NAND_CMD_PAGEPROG:
	case NAND_CMD_ERASE1:
	case NAND_CMD_ERASE2:
	case NAND_CMD_SEQIN:
	case NAND_CMD_STATUS:
		return;

	case NAND_CMD_RESET:
		if (this->dev_ready)
			break;
		udelay(this->chip_delay);
		WriteDOC(NAND_CMD_STATUS, docptr, Mplus_FlashCmd);
		WriteDOC(0, docptr, Mplus_WritePipeTerm);
		WriteDOC(0, docptr, Mplus_WritePipeTerm);
726
		while (!(this->read_byte(this) & 0x40)) ;
L
Linus Torvalds 已提交
727 728
		return;

729
		/* This applies to read commands */
L
Linus Torvalds 已提交
730
	default:
731
		/*
L
Linus Torvalds 已提交
732 733
		 * If we don't have access to the busy pin, we apply the given
		 * command delay
734
		 */
L
Linus Torvalds 已提交
735
		if (!this->dev_ready) {
736
			udelay(this->chip_delay);
L
Linus Torvalds 已提交
737 738 739 740 741 742
			return;
		}
	}

	/* Apply this short delay always to ensure that we do wait tWB in
	 * any case on any machine. */
743
	ndelay(100);
L
Linus Torvalds 已提交
744
	/* wait until command is processed */
745
	while (!this->dev_ready(mtd)) ;
L
Linus Torvalds 已提交
746 747 748 749
}

static int doc200x_dev_ready(struct mtd_info *mtd)
{
750
	struct nand_chip *this = mtd_to_nand(mtd);
751
	struct doc_priv *doc = nand_get_controller_data(this);
752
	void __iomem *docptr = doc->virtadr;
L
Linus Torvalds 已提交
753 754 755 756 757

	if (DoC_is_MillenniumPlus(doc)) {
		/* 11.4.2 -- must NOP four times before checking FR/B# */
		DoC_Delay(doc, 4);
		if ((ReadDOC(docptr, Mplus_FlashControl) & CDSN_CTRL_FR_B_MASK) != CDSN_CTRL_FR_B_MASK) {
758
			if (debug)
L
Linus Torvalds 已提交
759 760 761
				printk("not ready\n");
			return 0;
		}
762 763
		if (debug)
			printk("was ready\n");
L
Linus Torvalds 已提交
764 765 766 767 768
		return 1;
	} else {
		/* 11.4.2 -- must NOP four times before checking FR/B# */
		DoC_Delay(doc, 4);
		if (!(ReadDOC(docptr, CDSNControl) & CDSN_CTRL_FR_B)) {
769
			if (debug)
L
Linus Torvalds 已提交
770 771 772 773 774
				printk("not ready\n");
			return 0;
		}
		/* 11.4.2 -- Must NOP twice if it's ready */
		DoC_Delay(doc, 2);
775 776
		if (debug)
			printk("was ready\n");
L
Linus Torvalds 已提交
777 778 779 780
		return 1;
	}
}

781
static int doc200x_block_bad(struct mtd_info *mtd, loff_t ofs)
L
Linus Torvalds 已提交
782 783 784 785 786 787
{
	/* This is our last resort if we couldn't find or create a BBT.  Just
	   pretend all blocks are good. */
	return 0;
}

788
static void doc200x_enable_hwecc(struct nand_chip *this, int mode)
L
Linus Torvalds 已提交
789
{
790
	struct doc_priv *doc = nand_get_controller_data(this);
791
	void __iomem *docptr = doc->virtadr;
L
Linus Torvalds 已提交
792 793

	/* Prime the ECC engine */
794
	switch (mode) {
L
Linus Torvalds 已提交
795 796 797 798 799 800 801 802 803 804 805
	case NAND_ECC_READ:
		WriteDOC(DOC_ECC_RESET, docptr, ECCConf);
		WriteDOC(DOC_ECC_EN, docptr, ECCConf);
		break;
	case NAND_ECC_WRITE:
		WriteDOC(DOC_ECC_RESET, docptr, ECCConf);
		WriteDOC(DOC_ECC_EN | DOC_ECC_RW, docptr, ECCConf);
		break;
	}
}

806
static void doc2001plus_enable_hwecc(struct nand_chip *this, int mode)
L
Linus Torvalds 已提交
807
{
808
	struct doc_priv *doc = nand_get_controller_data(this);
809
	void __iomem *docptr = doc->virtadr;
L
Linus Torvalds 已提交
810 811

	/* Prime the ECC engine */
812
	switch (mode) {
L
Linus Torvalds 已提交
813 814 815 816 817 818 819 820 821 822 823 824
	case NAND_ECC_READ:
		WriteDOC(DOC_ECC_RESET, docptr, Mplus_ECCConf);
		WriteDOC(DOC_ECC_EN, docptr, Mplus_ECCConf);
		break;
	case NAND_ECC_WRITE:
		WriteDOC(DOC_ECC_RESET, docptr, Mplus_ECCConf);
		WriteDOC(DOC_ECC_EN | DOC_ECC_RW, docptr, Mplus_ECCConf);
		break;
	}
}

/* This code is only called on write */
825 826
static int doc200x_calculate_ecc(struct nand_chip *this, const u_char *dat,
				 unsigned char *ecc_code)
L
Linus Torvalds 已提交
827
{
828
	struct doc_priv *doc = nand_get_controller_data(this);
829
	void __iomem *docptr = doc->virtadr;
L
Linus Torvalds 已提交
830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852
	int i;
	int emptymatch = 1;

	/* flush the pipeline */
	if (DoC_is_2000(doc)) {
		WriteDOC(doc->CDSNControl & ~CDSN_CTRL_FLASH_IO, docptr, CDSNControl);
		WriteDOC(0, docptr, 2k_CDSN_IO);
		WriteDOC(0, docptr, 2k_CDSN_IO);
		WriteDOC(0, docptr, 2k_CDSN_IO);
		WriteDOC(doc->CDSNControl, docptr, CDSNControl);
	} else if (DoC_is_MillenniumPlus(doc)) {
		WriteDOC(0, docptr, Mplus_NOP);
		WriteDOC(0, docptr, Mplus_NOP);
		WriteDOC(0, docptr, Mplus_NOP);
	} else {
		WriteDOC(0, docptr, NOP);
		WriteDOC(0, docptr, NOP);
		WriteDOC(0, docptr, NOP);
	}

	for (i = 0; i < 6; i++) {
		if (DoC_is_MillenniumPlus(doc))
			ecc_code[i] = ReadDOC_(docptr, DoC_Mplus_ECCSyndrome0 + i);
853
		else
L
Linus Torvalds 已提交
854 855 856 857 858 859 860 861 862 863 864 865 866 867 868
			ecc_code[i] = ReadDOC_(docptr, DoC_ECCSyndrome0 + i);
		if (ecc_code[i] != empty_write_ecc[i])
			emptymatch = 0;
	}
	if (DoC_is_MillenniumPlus(doc))
		WriteDOC(DOC_ECC_DIS, docptr, Mplus_ECCConf);
	else
		WriteDOC(DOC_ECC_DIS, docptr, ECCConf);
#if 0
	/* If emptymatch=1, we might have an all-0xff data buffer.  Check. */
	if (emptymatch) {
		/* Note: this somewhat expensive test should not be triggered
		   often.  It could be optimized away by examining the data in
		   the writebuf routine, and remembering the result. */
		for (i = 0; i < 512; i++) {
869 870
			if (dat[i] == 0xff)
				continue;
L
Linus Torvalds 已提交
871 872 873 874 875 876 877
			emptymatch = 0;
			break;
		}
	}
	/* If emptymatch still =1, we do have an all-0xff data buffer.
	   Return all-0xff ecc value instead of the computed one, so
	   it'll look just like a freshly-erased page. */
878 879
	if (emptymatch)
		memset(ecc_code, 0xff, 6);
L
Linus Torvalds 已提交
880 881 882 883
#endif
	return 0;
}

884
static int doc200x_correct_data(struct nand_chip *this, u_char *dat,
885
				u_char *read_ecc, u_char *isnull)
L
Linus Torvalds 已提交
886 887
{
	int i, ret = 0;
888
	struct doc_priv *doc = nand_get_controller_data(this);
889
	void __iomem *docptr = doc->virtadr;
890
	uint8_t calc_ecc[6];
L
Linus Torvalds 已提交
891
	volatile u_char dummy;
892

L
Linus Torvalds 已提交
893 894 895 896 897 898 899 900 901 902 903 904 905 906
	/* flush the pipeline */
	if (DoC_is_2000(doc)) {
		dummy = ReadDOC(docptr, 2k_ECCStatus);
		dummy = ReadDOC(docptr, 2k_ECCStatus);
		dummy = ReadDOC(docptr, 2k_ECCStatus);
	} else if (DoC_is_MillenniumPlus(doc)) {
		dummy = ReadDOC(docptr, Mplus_ECCConf);
		dummy = ReadDOC(docptr, Mplus_ECCConf);
		dummy = ReadDOC(docptr, Mplus_ECCConf);
	} else {
		dummy = ReadDOC(docptr, ECCConf);
		dummy = ReadDOC(docptr, ECCConf);
		dummy = ReadDOC(docptr, ECCConf);
	}
907

L
Lucas De Marchi 已提交
908
	/* Error occurred ? */
L
Linus Torvalds 已提交
909 910 911 912 913 914 915
	if (dummy & 0x80) {
		for (i = 0; i < 6; i++) {
			if (DoC_is_MillenniumPlus(doc))
				calc_ecc[i] = ReadDOC_(docptr, DoC_Mplus_ECCSyndrome0 + i);
			else
				calc_ecc[i] = ReadDOC_(docptr, DoC_ECCSyndrome0 + i);
		}
916

917
		ret = doc_ecc_decode(doc->rs_decoder, dat, calc_ecc);
L
Linus Torvalds 已提交
918
		if (ret > 0)
919 920
			pr_err("doc200x_correct_data corrected %d errors\n",
			       ret);
921
	}
L
Linus Torvalds 已提交
922 923 924 925
	if (DoC_is_MillenniumPlus(doc))
		WriteDOC(DOC_ECC_DIS, docptr, Mplus_ECCConf);
	else
		WriteDOC(DOC_ECC_DIS, docptr, ECCConf);
926
	if (no_ecc_failures && mtd_is_eccerr(ret)) {
927
		pr_err("suppressing ECC failure\n");
L
Linus Torvalds 已提交
928 929 930 931
		ret = 0;
	}
	return ret;
}
932

L
Linus Torvalds 已提交
933 934
//u_char mydatabuf[528];

935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978
static int doc200x_ooblayout_ecc(struct mtd_info *mtd, int section,
				 struct mtd_oob_region *oobregion)
{
	if (section)
		return -ERANGE;

	oobregion->offset = 0;
	oobregion->length = 6;

	return 0;
}

static int doc200x_ooblayout_free(struct mtd_info *mtd, int section,
				  struct mtd_oob_region *oobregion)
{
	if (section > 1)
		return -ERANGE;

	/*
	 * The strange out-of-order free bytes definition is a (possibly
	 * unneeded) attempt to retain compatibility.  It used to read:
	 *	.oobfree = { {8, 8} }
	 * Since that leaves two bytes unusable, it was changed.  But the
	 * following scheme might affect existing jffs2 installs by moving the
	 * cleanmarker:
	 *	.oobfree = { {6, 10} }
	 * jffs2 seems to handle the above gracefully, but the current scheme
	 * seems safer. The only problem with it is that any code retrieving
	 * free bytes position must be able to handle out-of-order segments.
	 */
	if (!section) {
		oobregion->offset = 8;
		oobregion->length = 8;
	} else {
		oobregion->offset = 6;
		oobregion->length = 2;
	}

	return 0;
}

static const struct mtd_ooblayout_ops doc200x_ooblayout_ops = {
	.ecc = doc200x_ooblayout_ecc,
	.free = doc200x_ooblayout_free,
L
Linus Torvalds 已提交
979
};
980

L
Linus Torvalds 已提交
981
/* Find the (I)NFTL Media Header, and optionally also the mirror media header.
982
   On successful return, buf will contain a copy of the media header for
L
Linus Torvalds 已提交
983 984 985 986
   further processing.  id is the string to scan for, and will presumably be
   either "ANAND" or "BNAND".  If findmirror=1, also look for the mirror media
   header.  The page #s of the found media headers are placed in mh0_page and
   mh1_page in the DOC private structure. */
987
static int __init find_media_headers(struct mtd_info *mtd, u_char *buf, const char *id, int findmirror)
L
Linus Torvalds 已提交
988
{
989
	struct nand_chip *this = mtd_to_nand(mtd);
990
	struct doc_priv *doc = nand_get_controller_data(this);
991
	unsigned offs;
L
Linus Torvalds 已提交
992 993 994
	int ret;
	size_t retlen;

995
	for (offs = 0; offs < mtd->size; offs += mtd->erasesize) {
996
		ret = mtd_read(mtd, offs, mtd->writesize, &retlen, buf);
J
Joern Engel 已提交
997
		if (retlen != mtd->writesize)
998
			continue;
L
Linus Torvalds 已提交
999
		if (ret) {
1000
			pr_warn("ECC error scanning DOC at 0x%x\n", offs);
L
Linus Torvalds 已提交
1001
		}
1002 1003
		if (memcmp(buf, id, 6))
			continue;
1004
		pr_info("Found DiskOnChip %s Media Header at 0x%x\n", id, offs);
L
Linus Torvalds 已提交
1005 1006
		if (doc->mh0_page == -1) {
			doc->mh0_page = offs >> this->page_shift;
1007 1008
			if (!findmirror)
				return 1;
L
Linus Torvalds 已提交
1009 1010 1011 1012 1013 1014
			continue;
		}
		doc->mh1_page = offs >> this->page_shift;
		return 2;
	}
	if (doc->mh0_page == -1) {
1015
		pr_warn("DiskOnChip %s Media Header not found.\n", id);
L
Linus Torvalds 已提交
1016 1017 1018 1019 1020
		return 0;
	}
	/* Only one mediaheader was found.  We want buf to contain a
	   mediaheader on return, so we'll have to re-read the one we found. */
	offs = doc->mh0_page << this->page_shift;
1021
	ret = mtd_read(mtd, offs, mtd->writesize, &retlen, buf);
J
Joern Engel 已提交
1022
	if (retlen != mtd->writesize) {
L
Linus Torvalds 已提交
1023
		/* Insanity.  Give up. */
1024
		pr_err("Read DiskOnChip Media Header once, but can't reread it???\n");
L
Linus Torvalds 已提交
1025 1026 1027 1028 1029
		return 0;
	}
	return 1;
}

1030
static inline int __init nftl_partscan(struct mtd_info *mtd, struct mtd_partition *parts)
L
Linus Torvalds 已提交
1031
{
1032
	struct nand_chip *this = mtd_to_nand(mtd);
1033
	struct doc_priv *doc = nand_get_controller_data(this);
L
Linus Torvalds 已提交
1034 1035 1036 1037
	int ret = 0;
	u_char *buf;
	struct NFTLMediaHeader *mh;
	const unsigned psize = 1 << this->page_shift;
1038
	int numparts = 0;
L
Linus Torvalds 已提交
1039 1040 1041
	unsigned blocks, maxblocks;
	int offs, numheaders;

J
Joern Engel 已提交
1042
	buf = kmalloc(mtd->writesize, GFP_KERNEL);
L
Linus Torvalds 已提交
1043 1044 1045
	if (!buf) {
		return 0;
	}
1046 1047 1048
	if (!(numheaders = find_media_headers(mtd, buf, "ANAND", 1)))
		goto out;
	mh = (struct NFTLMediaHeader *)buf;
L
Linus Torvalds 已提交
1049

1050 1051 1052
	le16_to_cpus(&mh->NumEraseUnits);
	le16_to_cpus(&mh->FirstPhysicalEUN);
	le32_to_cpus(&mh->FormattedSize);
1053

1054 1055 1056 1057 1058
	pr_info("    DataOrgID        = %s\n"
		"    NumEraseUnits    = %d\n"
		"    FirstPhysicalEUN = %d\n"
		"    FormattedSize    = %d\n"
		"    UnitSizeFactor   = %d\n",
L
Linus Torvalds 已提交
1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069
		mh->DataOrgID, mh->NumEraseUnits,
		mh->FirstPhysicalEUN, mh->FormattedSize,
		mh->UnitSizeFactor);

	blocks = mtd->size >> this->phys_erase_shift;
	maxblocks = min(32768U, mtd->erasesize - psize);

	if (mh->UnitSizeFactor == 0x00) {
		/* Auto-determine UnitSizeFactor.  The constraints are:
		   - There can be at most 32768 virtual blocks.
		   - There can be at most (virtual block size - page size)
1070 1071
		   virtual blocks (because MediaHeader+BBT must fit in 1).
		 */
L
Linus Torvalds 已提交
1072 1073 1074 1075 1076 1077
		mh->UnitSizeFactor = 0xff;
		while (blocks > maxblocks) {
			blocks >>= 1;
			maxblocks = min(32768U, (maxblocks << 1) + psize);
			mh->UnitSizeFactor--;
		}
1078
		pr_warn("UnitSizeFactor=0x00 detected.  Correct value is assumed to be 0x%02x.\n", mh->UnitSizeFactor);
L
Linus Torvalds 已提交
1079 1080 1081 1082 1083
	}

	/* NOTE: The lines below modify internal variables of the NAND and MTD
	   layers; variables with have already been configured by nand_scan.
	   Unfortunately, we didn't know before this point what these values
L
Lucas De Marchi 已提交
1084
	   should be.  Thus, this code is somewhat dependent on the exact
L
Linus Torvalds 已提交
1085 1086 1087 1088
	   implementation of the NAND layer.  */
	if (mh->UnitSizeFactor != 0xff) {
		this->bbt_erase_shift += (0xff - mh->UnitSizeFactor);
		mtd->erasesize <<= (0xff - mh->UnitSizeFactor);
1089
		pr_info("Setting virtual erase size to %d\n", mtd->erasesize);
L
Linus Torvalds 已提交
1090 1091 1092 1093 1094
		blocks = mtd->size >> this->bbt_erase_shift;
		maxblocks = min(32768U, mtd->erasesize - psize);
	}

	if (blocks > maxblocks) {
1095
		pr_err("UnitSizeFactor of 0x%02x is inconsistent with device size.  Aborting.\n", mh->UnitSizeFactor);
L
Linus Torvalds 已提交
1096 1097 1098 1099 1100 1101 1102 1103
		goto out;
	}

	/* Skip past the media headers. */
	offs = max(doc->mh0_page, doc->mh1_page);
	offs <<= this->page_shift;
	offs += mtd->erasesize;

1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116
	if (show_firmware_partition == 1) {
		parts[0].name = " DiskOnChip Firmware / Media Header partition";
		parts[0].offset = 0;
		parts[0].size = offs;
		numparts = 1;
	}

	parts[numparts].name = " DiskOnChip BDTL partition";
	parts[numparts].offset = offs;
	parts[numparts].size = (mh->NumEraseUnits - numheaders) << this->bbt_erase_shift;

	offs += parts[numparts].size;
	numparts++;
L
Linus Torvalds 已提交
1117 1118

	if (offs < mtd->size) {
1119 1120 1121 1122
		parts[numparts].name = " DiskOnChip Remainder partition";
		parts[numparts].offset = offs;
		parts[numparts].size = mtd->size - offs;
		numparts++;
L
Linus Torvalds 已提交
1123
	}
1124 1125

	ret = numparts;
1126
 out:
L
Linus Torvalds 已提交
1127 1128 1129 1130 1131
	kfree(buf);
	return ret;
}

/* This is a stripped-down copy of the code in inftlmount.c */
1132
static inline int __init inftl_partscan(struct mtd_info *mtd, struct mtd_partition *parts)
L
Linus Torvalds 已提交
1133
{
1134
	struct nand_chip *this = mtd_to_nand(mtd);
1135
	struct doc_priv *doc = nand_get_controller_data(this);
L
Linus Torvalds 已提交
1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148
	int ret = 0;
	u_char *buf;
	struct INFTLMediaHeader *mh;
	struct INFTLPartition *ip;
	int numparts = 0;
	int blocks;
	int vshift, lastvunit = 0;
	int i;
	int end = mtd->size;

	if (inftl_bbt_write)
		end -= (INFTL_BBT_RESERVED_BLOCKS << this->phys_erase_shift);

J
Joern Engel 已提交
1149
	buf = kmalloc(mtd->writesize, GFP_KERNEL);
L
Linus Torvalds 已提交
1150 1151 1152 1153
	if (!buf) {
		return 0;
	}

1154 1155
	if (!find_media_headers(mtd, buf, "BNAND", 0))
		goto out;
L
Linus Torvalds 已提交
1156
	doc->mh1_page = doc->mh0_page + (4096 >> this->page_shift);
1157
	mh = (struct INFTLMediaHeader *)buf;
L
Linus Torvalds 已提交
1158

1159 1160 1161 1162 1163 1164
	le32_to_cpus(&mh->NoOfBootImageBlocks);
	le32_to_cpus(&mh->NoOfBinaryPartitions);
	le32_to_cpus(&mh->NoOfBDTLPartitions);
	le32_to_cpus(&mh->BlockMultiplierBits);
	le32_to_cpus(&mh->FormatFlags);
	le32_to_cpus(&mh->PercentUsed);
1165

1166 1167 1168 1169 1170 1171 1172 1173
	pr_info("    bootRecordID          = %s\n"
		"    NoOfBootImageBlocks   = %d\n"
		"    NoOfBinaryPartitions  = %d\n"
		"    NoOfBDTLPartitions    = %d\n"
		"    BlockMultiplerBits    = %d\n"
		"    FormatFlgs            = %d\n"
		"    OsakVersion           = %d.%d.%d.%d\n"
		"    PercentUsed           = %d\n",
L
Linus Torvalds 已提交
1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187
		mh->bootRecordID, mh->NoOfBootImageBlocks,
		mh->NoOfBinaryPartitions,
		mh->NoOfBDTLPartitions,
		mh->BlockMultiplierBits, mh->FormatFlags,
		((unsigned char *) &mh->OsakVersion)[0] & 0xf,
		((unsigned char *) &mh->OsakVersion)[1] & 0xf,
		((unsigned char *) &mh->OsakVersion)[2] & 0xf,
		((unsigned char *) &mh->OsakVersion)[3] & 0xf,
		mh->PercentUsed);

	vshift = this->phys_erase_shift + mh->BlockMultiplierBits;

	blocks = mtd->size >> vshift;
	if (blocks > 32768) {
1188
		pr_err("BlockMultiplierBits=%d is inconsistent with device size.  Aborting.\n", mh->BlockMultiplierBits);
L
Linus Torvalds 已提交
1189 1190 1191 1192 1193
		goto out;
	}

	blocks = doc->chips_per_floor << (this->chip_shift - this->phys_erase_shift);
	if (inftl_bbt_write && (blocks > mtd->erasesize)) {
1194
		pr_err("Writeable BBTs spanning more than one erase block are not yet supported.  FIX ME!\n");
L
Linus Torvalds 已提交
1195 1196 1197 1198 1199 1200
		goto out;
	}

	/* Scan the partitions */
	for (i = 0; (i < 4); i++) {
		ip = &(mh->Partitions[i]);
1201 1202 1203 1204 1205 1206
		le32_to_cpus(&ip->virtualUnits);
		le32_to_cpus(&ip->firstUnit);
		le32_to_cpus(&ip->lastUnit);
		le32_to_cpus(&ip->flags);
		le32_to_cpus(&ip->spareUnits);
		le32_to_cpus(&ip->Reserved0);
L
Linus Torvalds 已提交
1207

1208
		pr_info("    PARTITION[%d] ->\n"
L
Linus Torvalds 已提交
1209 1210 1211 1212 1213 1214 1215 1216 1217
			"        virtualUnits    = %d\n"
			"        firstUnit       = %d\n"
			"        lastUnit        = %d\n"
			"        flags           = 0x%x\n"
			"        spareUnits      = %d\n",
			i, ip->virtualUnits, ip->firstUnit,
			ip->lastUnit, ip->flags,
			ip->spareUnits);

1218 1219
		if ((show_firmware_partition == 1) &&
		    (i == 0) && (ip->firstUnit > 0)) {
L
Linus Torvalds 已提交
1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232
			parts[0].name = " DiskOnChip IPL / Media Header partition";
			parts[0].offset = 0;
			parts[0].size = mtd->erasesize * ip->firstUnit;
			numparts = 1;
		}

		if (ip->flags & INFTL_BINARY)
			parts[numparts].name = " DiskOnChip BDK partition";
		else
			parts[numparts].name = " DiskOnChip BDTL partition";
		parts[numparts].offset = ip->firstUnit << vshift;
		parts[numparts].size = (1 + ip->lastUnit - ip->firstUnit) << vshift;
		numparts++;
1233 1234 1235 1236
		if (ip->lastUnit > lastvunit)
			lastvunit = ip->lastUnit;
		if (ip->flags & INFTL_LAST)
			break;
L
Linus Torvalds 已提交
1237 1238 1239 1240 1241 1242 1243 1244 1245
	}
	lastvunit++;
	if ((lastvunit << vshift) < end) {
		parts[numparts].name = " DiskOnChip Remainder partition";
		parts[numparts].offset = lastvunit << vshift;
		parts[numparts].size = end - parts[numparts].offset;
		numparts++;
	}
	ret = numparts;
1246
 out:
L
Linus Torvalds 已提交
1247 1248 1249 1250 1251 1252 1253
	kfree(buf);
	return ret;
}

static int __init nftl_scan_bbt(struct mtd_info *mtd)
{
	int ret, numparts;
1254
	struct nand_chip *this = mtd_to_nand(mtd);
1255
	struct doc_priv *doc = nand_get_controller_data(this);
L
Linus Torvalds 已提交
1256 1257
	struct mtd_partition parts[2];

1258
	memset((char *)parts, 0, sizeof(parts));
L
Linus Torvalds 已提交
1259 1260 1261
	/* On NFTL, we have to find the media headers before we can read the
	   BBTs, since they're stored in the media header eraseblocks. */
	numparts = nftl_partscan(mtd, parts);
1262 1263
	if (!numparts)
		return -EIO;
L
Linus Torvalds 已提交
1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278
	this->bbt_td->options = NAND_BBT_ABSPAGE | NAND_BBT_8BIT |
				NAND_BBT_SAVECONTENT | NAND_BBT_WRITE |
				NAND_BBT_VERSION;
	this->bbt_td->veroffs = 7;
	this->bbt_td->pages[0] = doc->mh0_page + 1;
	if (doc->mh1_page != -1) {
		this->bbt_md->options = NAND_BBT_ABSPAGE | NAND_BBT_8BIT |
					NAND_BBT_SAVECONTENT | NAND_BBT_WRITE |
					NAND_BBT_VERSION;
		this->bbt_md->veroffs = 7;
		this->bbt_md->pages[0] = doc->mh1_page + 1;
	} else {
		this->bbt_md = NULL;
	}

1279
	ret = nand_create_bbt(this);
1280
	if (ret)
L
Linus Torvalds 已提交
1281
		return ret;
1282

1283
	return mtd_device_register(mtd, parts, no_autopart ? 0 : numparts);
L
Linus Torvalds 已提交
1284 1285 1286 1287 1288
}

static int __init inftl_scan_bbt(struct mtd_info *mtd)
{
	int ret, numparts;
1289
	struct nand_chip *this = mtd_to_nand(mtd);
1290
	struct doc_priv *doc = nand_get_controller_data(this);
L
Linus Torvalds 已提交
1291 1292 1293
	struct mtd_partition parts[5];

	if (this->numchips > doc->chips_per_floor) {
1294
		pr_err("Multi-floor INFTL devices not yet supported.\n");
L
Linus Torvalds 已提交
1295 1296 1297 1298 1299 1300 1301 1302 1303 1304
		return -EIO;
	}

	if (DoC_is_MillenniumPlus(doc)) {
		this->bbt_td->options = NAND_BBT_2BIT | NAND_BBT_ABSPAGE;
		if (inftl_bbt_write)
			this->bbt_td->options |= NAND_BBT_WRITE;
		this->bbt_td->pages[0] = 2;
		this->bbt_md = NULL;
	} else {
1305
		this->bbt_td->options = NAND_BBT_LASTBLOCK | NAND_BBT_8BIT | NAND_BBT_VERSION;
L
Linus Torvalds 已提交
1306 1307 1308 1309 1310 1311 1312 1313 1314
		if (inftl_bbt_write)
			this->bbt_td->options |= NAND_BBT_WRITE;
		this->bbt_td->offs = 8;
		this->bbt_td->len = 8;
		this->bbt_td->veroffs = 7;
		this->bbt_td->maxblocks = INFTL_BBT_RESERVED_BLOCKS;
		this->bbt_td->reserved_block_code = 0x01;
		this->bbt_td->pattern = "MSYS_BBT";

1315
		this->bbt_md->options = NAND_BBT_LASTBLOCK | NAND_BBT_8BIT | NAND_BBT_VERSION;
L
Linus Torvalds 已提交
1316 1317 1318 1319 1320 1321 1322 1323 1324 1325
		if (inftl_bbt_write)
			this->bbt_md->options |= NAND_BBT_WRITE;
		this->bbt_md->offs = 8;
		this->bbt_md->len = 8;
		this->bbt_md->veroffs = 7;
		this->bbt_md->maxblocks = INFTL_BBT_RESERVED_BLOCKS;
		this->bbt_md->reserved_block_code = 0x01;
		this->bbt_md->pattern = "TBB_SYSM";
	}

1326
	ret = nand_create_bbt(this);
1327
	if (ret)
L
Linus Torvalds 已提交
1328
		return ret;
1329

1330
	memset((char *)parts, 0, sizeof(parts));
L
Linus Torvalds 已提交
1331 1332 1333 1334
	numparts = inftl_partscan(mtd, parts);
	/* At least for now, require the INFTL Media Header.  We could probably
	   do without it for non-INFTL use, since all it gives us is
	   autopartitioning, but I want to give it more thought. */
1335 1336
	if (!numparts)
		return -EIO;
1337
	return mtd_device_register(mtd, parts, no_autopart ? 0 : numparts);
L
Linus Torvalds 已提交
1338 1339 1340 1341
}

static inline int __init doc2000_init(struct mtd_info *mtd)
{
1342
	struct nand_chip *this = mtd_to_nand(mtd);
1343
	struct doc_priv *doc = nand_get_controller_data(this);
L
Linus Torvalds 已提交
1344 1345 1346 1347

	this->read_byte = doc2000_read_byte;
	this->write_buf = doc2000_writebuf;
	this->read_buf = doc2000_readbuf;
1348
	doc->late_init = nftl_scan_bbt;
L
Linus Torvalds 已提交
1349 1350 1351 1352 1353 1354 1355 1356 1357

	doc->CDSNControl = CDSN_CTRL_FLASH_IO | CDSN_CTRL_ECC_IO;
	doc2000_count_chips(mtd);
	mtd->name = "DiskOnChip 2000 (NFTL Model)";
	return (4 * doc->chips_per_floor);
}

static inline int __init doc2001_init(struct mtd_info *mtd)
{
1358
	struct nand_chip *this = mtd_to_nand(mtd);
1359
	struct doc_priv *doc = nand_get_controller_data(this);
L
Linus Torvalds 已提交
1360 1361 1362 1363 1364 1365 1366 1367 1368 1369

	this->read_byte = doc2001_read_byte;
	this->write_buf = doc2001_writebuf;
	this->read_buf = doc2001_readbuf;

	ReadDOC(doc->virtadr, ChipID);
	ReadDOC(doc->virtadr, ChipID);
	ReadDOC(doc->virtadr, ChipID);
	if (ReadDOC(doc->virtadr, ChipID) != DOC_ChipID_DocMil) {
		/* It's not a Millennium; it's one of the newer
1370
		   DiskOnChip 2000 units with a similar ASIC.
L
Linus Torvalds 已提交
1371 1372 1373 1374
		   Treat it like a Millennium, except that it
		   can have multiple chips. */
		doc2000_count_chips(mtd);
		mtd->name = "DiskOnChip 2000 (INFTL Model)";
1375
		doc->late_init = inftl_scan_bbt;
L
Linus Torvalds 已提交
1376 1377 1378 1379 1380
		return (4 * doc->chips_per_floor);
	} else {
		/* Bog-standard Millennium */
		doc->chips_per_floor = 1;
		mtd->name = "DiskOnChip Millennium";
1381
		doc->late_init = nftl_scan_bbt;
L
Linus Torvalds 已提交
1382 1383 1384 1385 1386 1387
		return 1;
	}
}

static inline int __init doc2001plus_init(struct mtd_info *mtd)
{
1388
	struct nand_chip *this = mtd_to_nand(mtd);
1389
	struct doc_priv *doc = nand_get_controller_data(this);
L
Linus Torvalds 已提交
1390 1391 1392 1393

	this->read_byte = doc2001plus_read_byte;
	this->write_buf = doc2001plus_writebuf;
	this->read_buf = doc2001plus_readbuf;
1394
	doc->late_init = inftl_scan_bbt;
1395
	this->cmd_ctrl = NULL;
L
Linus Torvalds 已提交
1396 1397
	this->select_chip = doc2001plus_select_chip;
	this->cmdfunc = doc2001plus_command;
1398
	this->ecc.hwctl = doc2001plus_enable_hwecc;
L
Linus Torvalds 已提交
1399 1400 1401 1402 1403 1404 1405

	doc->chips_per_floor = 1;
	mtd->name = "DiskOnChip Millennium Plus";

	return 1;
}

1406
static int __init doc_probe(unsigned long physadr)
L
Linus Torvalds 已提交
1407
{
1408 1409
	struct nand_chip *nand = NULL;
	struct doc_priv *doc = NULL;
L
Linus Torvalds 已提交
1410 1411 1412 1413 1414 1415 1416 1417
	unsigned char ChipID;
	struct mtd_info *mtd;
	void __iomem *virtadr;
	unsigned char save_control;
	unsigned char tmp, tmpb, tmpc;
	int reg, len, numchips;
	int ret = 0;

1418
	if (!request_mem_region(physadr, DOC_IOREMAP_LEN, "DiskOnChip"))
1419
		return -EBUSY;
L
Linus Torvalds 已提交
1420 1421
	virtadr = ioremap(physadr, DOC_IOREMAP_LEN);
	if (!virtadr) {
1422 1423
		pr_err("Diskonchip ioremap failed: 0x%x bytes at 0x%lx\n",
		       DOC_IOREMAP_LEN, physadr);
1424 1425
		ret = -EIO;
		goto error_ioremap;
L
Linus Torvalds 已提交
1426 1427 1428 1429 1430 1431 1432 1433
	}

	/* It's not possible to cleanly detect the DiskOnChip - the
	 * bootup procedure will put the device into reset mode, and
	 * it's not possible to talk to it without actually writing
	 * to the DOCControl register. So we store the current contents
	 * of the DOCControl register's location, in case we later decide
	 * that it's not a DiskOnChip, and want to put it back how we
1434
	 * found it.
L
Linus Torvalds 已提交
1435 1436 1437 1438
	 */
	save_control = ReadDOC(virtadr, DOCControl);

	/* Reset the DiskOnChip ASIC */
1439 1440
	WriteDOC(DOC_MODE_CLR_ERR | DOC_MODE_MDWREN | DOC_MODE_RESET, virtadr, DOCControl);
	WriteDOC(DOC_MODE_CLR_ERR | DOC_MODE_MDWREN | DOC_MODE_RESET, virtadr, DOCControl);
L
Linus Torvalds 已提交
1441 1442

	/* Enable the DiskOnChip ASIC */
1443 1444
	WriteDOC(DOC_MODE_CLR_ERR | DOC_MODE_MDWREN | DOC_MODE_NORMAL, virtadr, DOCControl);
	WriteDOC(DOC_MODE_CLR_ERR | DOC_MODE_MDWREN | DOC_MODE_NORMAL, virtadr, DOCControl);
L
Linus Torvalds 已提交
1445 1446 1447

	ChipID = ReadDOC(virtadr, ChipID);

1448
	switch (ChipID) {
L
Linus Torvalds 已提交
1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463
	case DOC_ChipID_Doc2k:
		reg = DoC_2k_ECCStatus;
		break;
	case DOC_ChipID_DocMil:
		reg = DoC_ECCConf;
		break;
	case DOC_ChipID_DocMilPlus16:
	case DOC_ChipID_DocMilPlus32:
	case 0:
		/* Possible Millennium Plus, need to do more checks */
		/* Possibly release from power down mode */
		for (tmp = 0; (tmp < 4); tmp++)
			ReadDOC(virtadr, Mplus_Power);

		/* Reset the Millennium Plus ASIC */
1464
		tmp = DOC_MODE_RESET | DOC_MODE_MDWREN | DOC_MODE_RST_LAT | DOC_MODE_BDECT;
L
Linus Torvalds 已提交
1465 1466 1467
		WriteDOC(tmp, virtadr, Mplus_DOCControl);
		WriteDOC(~tmp, virtadr, Mplus_CtrlConfirm);

1468
		usleep_range(1000, 2000);
L
Linus Torvalds 已提交
1469
		/* Enable the Millennium Plus ASIC */
1470
		tmp = DOC_MODE_NORMAL | DOC_MODE_MDWREN | DOC_MODE_RST_LAT | DOC_MODE_BDECT;
L
Linus Torvalds 已提交
1471 1472
		WriteDOC(tmp, virtadr, Mplus_DOCControl);
		WriteDOC(~tmp, virtadr, Mplus_CtrlConfirm);
1473
		usleep_range(1000, 2000);
L
Linus Torvalds 已提交
1474 1475 1476 1477 1478 1479 1480 1481

		ChipID = ReadDOC(virtadr, ChipID);

		switch (ChipID) {
		case DOC_ChipID_DocMilPlus16:
			reg = DoC_Mplus_Toggle;
			break;
		case DOC_ChipID_DocMilPlus32:
1482
			pr_err("DiskOnChip Millennium Plus 32MB is not supported, ignoring.\n");
L
Linus Torvalds 已提交
1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493
		default:
			ret = -ENODEV;
			goto notfound;
		}
		break;

	default:
		ret = -ENODEV;
		goto notfound;
	}
	/* Check the TOGGLE bit in the ECC register */
1494
	tmp = ReadDOC_(virtadr, reg) & DOC_TOGGLE_BIT;
L
Linus Torvalds 已提交
1495 1496 1497
	tmpb = ReadDOC_(virtadr, reg) & DOC_TOGGLE_BIT;
	tmpc = ReadDOC_(virtadr, reg) & DOC_TOGGLE_BIT;
	if ((tmp == tmpb) || (tmp != tmpc)) {
1498
		pr_warn("Possible DiskOnChip at 0x%lx failed TOGGLE test, dropping.\n", physadr);
L
Linus Torvalds 已提交
1499 1500 1501 1502 1503 1504 1505
		ret = -ENODEV;
		goto notfound;
	}

	for (mtd = doclist; mtd; mtd = doc->nextdoc) {
		unsigned char oldval;
		unsigned char newval;
1506
		nand = mtd_to_nand(mtd);
1507
		doc = nand_get_controller_data(nand);
L
Linus Torvalds 已提交
1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523
		/* Use the alias resolution register to determine if this is
		   in fact the same DOC aliased to a new address.  If writes
		   to one chip's alias resolution register change the value on
		   the other chip, they're the same chip. */
		if (ChipID == DOC_ChipID_DocMilPlus16) {
			oldval = ReadDOC(doc->virtadr, Mplus_AliasResolution);
			newval = ReadDOC(virtadr, Mplus_AliasResolution);
		} else {
			oldval = ReadDOC(doc->virtadr, AliasResolution);
			newval = ReadDOC(virtadr, AliasResolution);
		}
		if (oldval != newval)
			continue;
		if (ChipID == DOC_ChipID_DocMilPlus16) {
			WriteDOC(~newval, virtadr, Mplus_AliasResolution);
			oldval = ReadDOC(doc->virtadr, Mplus_AliasResolution);
1524
			WriteDOC(newval, virtadr, Mplus_AliasResolution);	// restore it
L
Linus Torvalds 已提交
1525 1526 1527
		} else {
			WriteDOC(~newval, virtadr, AliasResolution);
			oldval = ReadDOC(doc->virtadr, AliasResolution);
1528
			WriteDOC(newval, virtadr, AliasResolution);	// restore it
L
Linus Torvalds 已提交
1529 1530 1531
		}
		newval = ~newval;
		if (oldval == newval) {
1532 1533
			pr_debug("Found alias of DOC at 0x%lx to 0x%lx\n",
				 doc->physadr, physadr);
L
Linus Torvalds 已提交
1534 1535 1536 1537
			goto notfound;
		}
	}

1538
	pr_notice("DiskOnChip found at 0x%lx\n", physadr);
L
Linus Torvalds 已提交
1539

1540 1541 1542 1543
	len = sizeof(struct nand_chip) + sizeof(struct doc_priv) +
	      (2 * sizeof(struct nand_bbt_descr));
	nand = kzalloc(len, GFP_KERNEL);
	if (!nand) {
L
Linus Torvalds 已提交
1544 1545 1546 1547
		ret = -ENOMEM;
		goto fail;
	}

1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565

	/*
	 * Allocate a RS codec instance
	 *
	 * Symbolsize is 10 (bits)
	 * Primitve polynomial is x^10+x^3+1
	 * First consecutive root is 510
	 * Primitve element to generate roots = 1
	 * Generator polinomial degree = 4
	 */
	doc = (struct doc_priv *) (nand + 1);
	doc->rs_decoder = init_rs(10, 0x409, FCR, 1, NROOTS);
	if (!doc->rs_decoder) {
		pr_err("DiskOnChip: Could not create a RS codec\n");
		ret = -ENOMEM;
		goto fail;
	}

1566
	mtd			= nand_to_mtd(nand);
L
Linus Torvalds 已提交
1567 1568 1569 1570
	nand->bbt_td		= (struct nand_bbt_descr *) (doc + 1);
	nand->bbt_md		= nand->bbt_td + 1;

	mtd->owner		= THIS_MODULE;
1571
	mtd_set_ooblayout(mtd, &doc200x_ooblayout_ops);
L
Linus Torvalds 已提交
1572

1573
	nand_set_controller_data(nand, doc);
L
Linus Torvalds 已提交
1574
	nand->select_chip	= doc200x_select_chip;
1575
	nand->cmd_ctrl		= doc200x_hwcontrol;
L
Linus Torvalds 已提交
1576 1577 1578
	nand->dev_ready		= doc200x_dev_ready;
	nand->waitfunc		= doc200x_wait;
	nand->block_bad		= doc200x_block_bad;
T
Thomas Gleixner 已提交
1579 1580 1581
	nand->ecc.hwctl		= doc200x_enable_hwecc;
	nand->ecc.calculate	= doc200x_calculate_ecc;
	nand->ecc.correct	= doc200x_correct_data;
L
Linus Torvalds 已提交
1582

T
Thomas Gleixner 已提交
1583 1584 1585
	nand->ecc.mode		= NAND_ECC_HW_SYNDROME;
	nand->ecc.size		= 512;
	nand->ecc.bytes		= 6;
M
Mike Dunn 已提交
1586
	nand->ecc.strength	= 2;
1587
	nand->ecc.options	= NAND_ECC_GENERIC_ERASED_CHECK;
1588
	nand->bbt_options	= NAND_BBT_USE_FLASH;
1589 1590
	/* Skip the automatic BBT scan so we can run it manually */
	nand->options		|= NAND_SKIP_BBTSCAN;
L
Linus Torvalds 已提交
1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607

	doc->physadr		= physadr;
	doc->virtadr		= virtadr;
	doc->ChipID		= ChipID;
	doc->curfloor		= -1;
	doc->curchip		= -1;
	doc->mh0_page		= -1;
	doc->mh1_page		= -1;
	doc->nextdoc		= doclist;

	if (ChipID == DOC_ChipID_Doc2k)
		numchips = doc2000_init(mtd);
	else if (ChipID == DOC_ChipID_DocMilPlus16)
		numchips = doc2001plus_init(mtd);
	else
		numchips = doc2001_init(mtd);

1608
	if ((ret = nand_scan(nand, numchips)) || (ret = doc->late_init(mtd))) {
L
Linus Torvalds 已提交
1609 1610 1611
		/* DBB note: i believe nand_release is necessary here, as
		   buffers may have been allocated in nand_base.  Check with
		   Thomas. FIX ME! */
1612 1613 1614
		/* nand_release will call mtd_device_unregister, but we
		   haven't yet added it.  This is handled without incident by
		   mtd_device_unregister, as far as I can tell. */
1615
		nand_release(nand);
L
Linus Torvalds 已提交
1616 1617 1618 1619 1620 1621 1622
		goto fail;
	}

	/* Success! */
	doclist = mtd;
	return 0;

1623
 notfound:
L
Linus Torvalds 已提交
1624 1625 1626
	/* Put back the contents of the DOCControl register, in case it's not
	   actually a DiskOnChip.  */
	WriteDOC(save_control, virtadr, DOCControl);
1627
 fail:
1628 1629 1630
	if (doc)
		free_rs(doc->rs_decoder);
	kfree(nand);
L
Linus Torvalds 已提交
1631
	iounmap(virtadr);
1632 1633 1634 1635

error_ioremap:
	release_mem_region(physadr, DOC_IOREMAP_LEN);

L
Linus Torvalds 已提交
1636 1637 1638 1639 1640
	return ret;
}

static void release_nanddoc(void)
{
1641
	struct mtd_info *mtd, *nextmtd;
L
Linus Torvalds 已提交
1642 1643 1644 1645
	struct nand_chip *nand;
	struct doc_priv *doc;

	for (mtd = doclist; mtd; mtd = nextmtd) {
1646
		nand = mtd_to_nand(mtd);
1647
		doc = nand_get_controller_data(nand);
L
Linus Torvalds 已提交
1648 1649

		nextmtd = doc->nextdoc;
1650
		nand_release(nand);
L
Linus Torvalds 已提交
1651
		iounmap(doc->virtadr);
1652
		release_mem_region(doc->physadr, DOC_IOREMAP_LEN);
1653
		free_rs(doc->rs_decoder);
1654
		kfree(nand);
L
Linus Torvalds 已提交
1655 1656 1657 1658 1659 1660 1661 1662
	}
}

static int __init init_nanddoc(void)
{
	int i, ret = 0;

	if (doc_config_location) {
1663 1664
		pr_info("Using configured DiskOnChip probe address 0x%lx\n",
			doc_config_location);
L
Linus Torvalds 已提交
1665 1666
		ret = doc_probe(doc_config_location);
		if (ret < 0)
1667
			return ret;
L
Linus Torvalds 已提交
1668
	} else {
1669
		for (i = 0; (doc_locations[i] != 0xffffffff); i++) {
L
Linus Torvalds 已提交
1670 1671 1672 1673 1674 1675
			doc_probe(doc_locations[i]);
		}
	}
	/* No banner message any more. Print a message if no DiskOnChip
	   found, so the user knows we at least tried. */
	if (!doclist) {
1676
		pr_info("No valid DiskOnChip devices found\n");
L
Linus Torvalds 已提交
1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692
		ret = -ENODEV;
	}
	return ret;
}

static void __exit cleanup_nanddoc(void)
{
	/* Cleanup the nand/DoC resources */
	release_nanddoc();
}

module_init(init_nanddoc);
module_exit(cleanup_nanddoc);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
1693
MODULE_DESCRIPTION("M-Systems DiskOnChip 2000, Millennium and Millennium Plus device driver");