1
2/*
3 * Linux driver for Disk-On-Chip 2000 and Millennium
4 * (c) 1999 Machine Vision Holdings, Inc.
5 * (c) 1999, 2000 David Woodhouse <dwmw2@infradead.org>
6 *
7 * $Id: doc2000.c,v 1.1.1.1 2008/10/15 03:26:35 james26_jang Exp $
8 */
9
10#include <linux/kernel.h>
11#include <linux/module.h>
12#include <asm/errno.h>
13#include <asm/io.h>
14#include <asm/uaccess.h>
15#include <linux/miscdevice.h>
16#include <linux/pci.h>
17#include <linux/delay.h>
18#include <linux/slab.h>
19#include <linux/sched.h>
20#include <linux/init.h>
21#include <linux/types.h>
22
23#include <linux/mtd/mtd.h>
24#include <linux/mtd/nand.h>
25#include <linux/mtd/nand_ids.h>
26#include <linux/mtd/doc2000.h>
27
28#define DOC_SUPPORT_2000
29#define DOC_SUPPORT_MILLENNIUM
30
31#ifdef DOC_SUPPORT_2000
32#define DoC_is_2000(doc) (doc->ChipID == DOC_ChipID_Doc2k)
33#else
34#define DoC_is_2000(doc) (0)
35#endif
36
37#ifdef DOC_SUPPORT_MILLENNIUM
38#define DoC_is_Millennium(doc) (doc->ChipID == DOC_ChipID_DocMil)
39#else
40#define DoC_is_Millennium(doc) (0)
41#endif
42
43/* #define ECC_DEBUG */
44
45/* I have no idea why some DoC chips can not use memcpy_from|to_io().
46 * This may be due to the different revisions of the ASIC controller built-in or
47 * simplily a QA/Bug issue. Who knows ?? If you have trouble, please uncomment
48 * this:
49 #undef USE_MEMCPY
50*/
51
52static int doc_read(struct mtd_info *mtd, loff_t from, size_t len,
53		    size_t *retlen, u_char *buf);
54static int doc_write(struct mtd_info *mtd, loff_t to, size_t len,
55		     size_t *retlen, const u_char *buf);
56static int doc_read_ecc(struct mtd_info *mtd, loff_t from, size_t len,
57			size_t *retlen, u_char *buf, u_char *eccbuf);
58static int doc_write_ecc(struct mtd_info *mtd, loff_t to, size_t len,
59			 size_t *retlen, const u_char *buf, u_char *eccbuf);
60static int doc_read_oob(struct mtd_info *mtd, loff_t ofs, size_t len,
61			size_t *retlen, u_char *buf);
62static int doc_write_oob(struct mtd_info *mtd, loff_t ofs, size_t len,
63			 size_t *retlen, const u_char *buf);
64static int doc_write_oob_nolock(struct mtd_info *mtd, loff_t ofs, size_t len,
65			 size_t *retlen, const u_char *buf);
66static int doc_erase (struct mtd_info *mtd, struct erase_info *instr);
67
68static struct mtd_info *doc2klist = NULL;
69
70/* Perform the required delay cycles by reading from the appropriate register */
71static void DoC_Delay(struct DiskOnChip *doc, unsigned short cycles)
72{
73	volatile char dummy;
74	int i;
75
76	for (i = 0; i < cycles; i++) {
77		if (DoC_is_Millennium(doc))
78			dummy = ReadDOC(doc->virtadr, NOP);
79		else
80			dummy = ReadDOC(doc->virtadr, DOCStatus);
81	}
82
83}
84
85/* DOC_WaitReady: Wait for RDY line to be asserted by the flash chip */
86static int _DoC_WaitReady(struct DiskOnChip *doc)
87{
88	unsigned long docptr = doc->virtadr;
89	unsigned long timeo = jiffies + (HZ * 10);
90
91	DEBUG(MTD_DEBUG_LEVEL3,
92	      "_DoC_WaitReady called for out-of-line wait\n");
93
94	/* Out-of-line routine to wait for chip response */
95	while (!(ReadDOC(docptr, CDSNControl) & CDSN_CTRL_FR_B)) {
96		if (time_after(jiffies, timeo)) {
97			DEBUG(MTD_DEBUG_LEVEL2, "_DoC_WaitReady timed out.\n");
98			return -EIO;
99		}
100		if (current->need_resched) {
101			set_current_state(TASK_UNINTERRUPTIBLE);
102			schedule_timeout(1);
103		}
104		else
105			udelay(1);
106	}
107
108	return 0;
109}
110
111static inline int DoC_WaitReady(struct DiskOnChip *doc)
112{
113	unsigned long docptr = doc->virtadr;
114	/* This is inline, to optimise the common case, where it's ready instantly */
115	int ret = 0;
116
117	/* 4 read form NOP register should be issued in prior to the read from CDSNControl
118	   see Software Requirement 11.4 item 2. */
119	DoC_Delay(doc, 4);
120
121	if (!(ReadDOC(docptr, CDSNControl) & CDSN_CTRL_FR_B))
122		/* Call the out-of-line routine to wait */
123		ret = _DoC_WaitReady(doc);
124
125	/* issue 2 read from NOP register after reading from CDSNControl register
126	   see Software Requirement 11.4 item 2. */
127	DoC_Delay(doc, 2);
128
129	return ret;
130}
131
132/* DoC_Command: Send a flash command to the flash chip through the CDSN Slow IO register to
133   bypass the internal pipeline. Each of 4 delay cycles (read from the NOP register) is
134   required after writing to CDSN Control register, see Software Requirement 11.4 item 3. */
135
136static inline int DoC_Command(struct DiskOnChip *doc, unsigned char command,
137			      unsigned char xtraflags)
138{
139	unsigned long docptr = doc->virtadr;
140
141	if (DoC_is_2000(doc))
142		xtraflags |= CDSN_CTRL_FLASH_IO;
143
144	/* Assert the CLE (Command Latch Enable) line to the flash chip */
145	WriteDOC(xtraflags | CDSN_CTRL_CLE | CDSN_CTRL_CE, docptr, CDSNControl);
146	DoC_Delay(doc, 4);	/* Software requirement 11.4.3 for Millennium */
147
148	if (DoC_is_Millennium(doc))
149		WriteDOC(command, docptr, CDSNSlowIO);
150
151	/* Send the command */
152	WriteDOC_(command, docptr, doc->ioreg);
153
154	/* Lower the CLE line */
155	WriteDOC(xtraflags | CDSN_CTRL_CE, docptr, CDSNControl);
156	DoC_Delay(doc, 4);	/* Software requirement 11.4.3 for Millennium */
157
158	/* Wait for the chip to respond - Software requirement 11.4.1 (extended for any command) */
159	return DoC_WaitReady(doc);
160}
161
162/* DoC_Address: Set the current address for the flash chip through the CDSN Slow IO register to
163   bypass the internal pipeline. Each of 4 delay cycles (read from the NOP register) is
164   required after writing to CDSN Control register, see Software Requirement 11.4 item 3. */
165
166static int DoC_Address(struct DiskOnChip *doc, int numbytes, unsigned long ofs,
167		       unsigned char xtraflags1, unsigned char xtraflags2)
168{
169	unsigned long docptr;
170	int i;
171
172	docptr = doc->virtadr;
173
174	if (DoC_is_2000(doc))
175		xtraflags1 |= CDSN_CTRL_FLASH_IO;
176
177	/* Assert the ALE (Address Latch Enable) line to the flash chip */
178	WriteDOC(xtraflags1 | CDSN_CTRL_ALE | CDSN_CTRL_CE, docptr, CDSNControl);
179
180	DoC_Delay(doc, 4);	/* Software requirement 11.4.3 for Millennium */
181
182	/* Send the address */
183	/* Devices with 256-byte page are addressed as:
184	   Column (bits 0-7), Page (bits 8-15, 16-23, 24-31)
185	   * there is no device on the market with page256
186	   and more than 24 bits.
187	   Devices with 512-byte page are addressed as:
188	   Column (bits 0-7), Page (bits 9-16, 17-24, 25-31)
189	   * 25-31 is sent only if the chip support it.
190	   * bit 8 changes the read command to be sent
191	   (NAND_CMD_READ0 or NAND_CMD_READ1).
192	 */
193
194	if (numbytes == ADDR_COLUMN || numbytes == ADDR_COLUMN_PAGE) {
195		if (DoC_is_Millennium(doc))
196			WriteDOC(ofs & 0xff, docptr, CDSNSlowIO);
197		WriteDOC_(ofs & 0xff, docptr, doc->ioreg);
198	}
199
200	if (doc->page256) {
201		ofs = ofs >> 8;
202	} else {
203		ofs = ofs >> 9;
204	}
205
206	if (numbytes == ADDR_PAGE || numbytes == ADDR_COLUMN_PAGE) {
207		for (i = 0; i < doc->pageadrlen; i++, ofs = ofs >> 8) {
208			if (DoC_is_Millennium(doc))
209				WriteDOC(ofs & 0xff, docptr, CDSNSlowIO);
210			WriteDOC_(ofs & 0xff, docptr, doc->ioreg);
211		}
212	}
213
214	DoC_Delay(doc, 2);	/* Needed for some slow flash chips. mf. */
215
216
217	/* Lower the ALE line */
218	WriteDOC(xtraflags1 | xtraflags2 | CDSN_CTRL_CE, docptr,
219		 CDSNControl);
220
221	DoC_Delay(doc, 4);	/* Software requirement 11.4.3 for Millennium */
222
223	/* Wait for the chip to respond - Software requirement 11.4.1 */
224	return DoC_WaitReady(doc);
225}
226
227/* Read a buffer from DoC, taking care of Millennium odditys */
228static void DoC_ReadBuf(struct DiskOnChip *doc, u_char * buf, int len)
229{
230	volatile int dummy;
231	int modulus = 0xffff;
232	unsigned long docptr;
233	int i;
234
235	docptr = doc->virtadr;
236
237	if (len <= 0)
238		return;
239
240	if (DoC_is_Millennium(doc)) {
241		/* Read the data via the internal pipeline through CDSN IO register,
242		   see Pipelined Read Operations 11.3 */
243		dummy = ReadDOC(docptr, ReadPipeInit);
244
245		/* Millennium should use the LastDataRead register - Pipeline Reads */
246		len--;
247
248		/* This is needed for correctly ECC calculation */
249		modulus = 0xff;
250	}
251
252	for (i = 0; i < len; i++)
253		buf[i] = ReadDOC_(docptr, doc->ioreg + (i & modulus));
254
255	if (DoC_is_Millennium(doc)) {
256		buf[i] = ReadDOC(docptr, LastDataRead);
257	}
258}
259
260/* Write a buffer to DoC, taking care of Millennium odditys */
261static void DoC_WriteBuf(struct DiskOnChip *doc, const u_char * buf, int len)
262{
263	unsigned long docptr;
264	int i;
265
266	docptr = doc->virtadr;
267
268	if (len <= 0)
269		return;
270
271	for (i = 0; i < len; i++)
272		WriteDOC_(buf[i], docptr, doc->ioreg + i);
273
274	if (DoC_is_Millennium(doc)) {
275		WriteDOC(0x00, docptr, WritePipeTerm);
276	}
277}
278
279
280/* DoC_SelectChip: Select a given flash chip within the current floor */
281
282static inline int DoC_SelectChip(struct DiskOnChip *doc, int chip)
283{
284	unsigned long docptr = doc->virtadr;
285
286	/* Software requirement 11.4.4 before writing DeviceSelect */
287	/* Deassert the CE line to eliminate glitches on the FCE# outputs */
288	WriteDOC(CDSN_CTRL_WP, docptr, CDSNControl);
289	DoC_Delay(doc, 4);	/* Software requirement 11.4.3 for Millennium */
290
291	/* Select the individual flash chip requested */
292	WriteDOC(chip, docptr, CDSNDeviceSelect);
293	DoC_Delay(doc, 4);
294
295	/* Reassert the CE line */
296	WriteDOC(CDSN_CTRL_CE | CDSN_CTRL_FLASH_IO | CDSN_CTRL_WP, docptr,
297		 CDSNControl);
298	DoC_Delay(doc, 4);	/* Software requirement 11.4.3 for Millennium */
299
300	/* Wait for it to be ready */
301	return DoC_WaitReady(doc);
302}
303
304/* DoC_SelectFloor: Select a given floor (bank of flash chips) */
305
306static inline int DoC_SelectFloor(struct DiskOnChip *doc, int floor)
307{
308	unsigned long docptr = doc->virtadr;
309
310	/* Select the floor (bank) of chips required */
311	WriteDOC(floor, docptr, FloorSelect);
312
313	/* Wait for the chip to be ready */
314	return DoC_WaitReady(doc);
315}
316
317/* DoC_IdentChip: Identify a given NAND chip given {floor,chip} */
318
319static int DoC_IdentChip(struct DiskOnChip *doc, int floor, int chip)
320{
321	int mfr, id, i;
322	volatile char dummy;
323
324	/* Page in the required floor/chip */
325	DoC_SelectFloor(doc, floor);
326	DoC_SelectChip(doc, chip);
327
328	/* Reset the chip */
329	if (DoC_Command(doc, NAND_CMD_RESET, CDSN_CTRL_WP)) {
330		DEBUG(MTD_DEBUG_LEVEL2,
331		      "DoC_Command (reset) for %d,%d returned true\n",
332		      floor, chip);
333		return 0;
334	}
335
336
337	/* Read the NAND chip ID: 1. Send ReadID command */
338	if (DoC_Command(doc, NAND_CMD_READID, CDSN_CTRL_WP)) {
339		DEBUG(MTD_DEBUG_LEVEL2,
340		      "DoC_Command (ReadID) for %d,%d returned true\n",
341		      floor, chip);
342		return 0;
343	}
344
345	/* Read the NAND chip ID: 2. Send address byte zero */
346	DoC_Address(doc, ADDR_COLUMN, 0, CDSN_CTRL_WP, 0);
347
348	/* Read the manufacturer and device id codes from the device */
349
350	/* CDSN Slow IO register see Software Requirement 11.4 item 5. */
351	dummy = ReadDOC(doc->virtadr, CDSNSlowIO);
352	DoC_Delay(doc, 2);
353	mfr = ReadDOC_(doc->virtadr, doc->ioreg);
354
355	/* CDSN Slow IO register see Software Requirement 11.4 item 5. */
356	dummy = ReadDOC(doc->virtadr, CDSNSlowIO);
357	DoC_Delay(doc, 2);
358	id = ReadDOC_(doc->virtadr, doc->ioreg);
359
360	/* No response - return failure */
361	if (mfr == 0xff || mfr == 0)
362		return 0;
363
364	/* Check it's the same as the first chip we identified.
365	 * M-Systems say that any given DiskOnChip device should only
366	 * contain _one_ type of flash part, although that's not a
367	 * hardware restriction. */
368	if (doc->mfr) {
369		if (doc->mfr == mfr && doc->id == id)
370			return 1;	/* This is another the same the first */
371		else
372			printk(KERN_WARNING
373			       "Flash chip at floor %d, chip %d is different:\n",
374			       floor, chip);
375	}
376
377	/* Print and store the manufacturer and ID codes. */
378	for (i = 0; nand_flash_ids[i].name != NULL; i++) {
379		if (mfr == nand_flash_ids[i].manufacture_id &&
380		    id == nand_flash_ids[i].model_id) {
381			printk(KERN_INFO
382			       "Flash chip found: Manufacturer ID: %2.2X, "
383			       "Chip ID: %2.2X (%s)\n", mfr, id,
384			       nand_flash_ids[i].name);
385			if (!doc->mfr) {
386				doc->mfr = mfr;
387				doc->id = id;
388				doc->chipshift =
389				    nand_flash_ids[i].chipshift;
390				doc->page256 = nand_flash_ids[i].page256;
391				doc->pageadrlen =
392				    nand_flash_ids[i].pageadrlen;
393				doc->erasesize =
394				    nand_flash_ids[i].erasesize;
395				return 1;
396			}
397			return 0;
398		}
399	}
400
401
402	/* We haven't fully identified the chip. Print as much as we know. */
403	printk(KERN_WARNING "Unknown flash chip found: %2.2X %2.2X\n",
404	       id, mfr);
405
406	printk(KERN_WARNING "Please report to dwmw2@infradead.org\n");
407	return 0;
408}
409
410/* DoC_ScanChips: Find all NAND chips present in a DiskOnChip, and identify them */
411
412static void DoC_ScanChips(struct DiskOnChip *this)
413{
414	int floor, chip;
415	int numchips[MAX_FLOORS];
416	int maxchips = MAX_CHIPS;
417	int ret = 1;
418
419	this->numchips = 0;
420	this->mfr = 0;
421	this->id = 0;
422
423	if (DoC_is_Millennium(this))
424		maxchips = MAX_CHIPS_MIL;
425
426	/* For each floor, find the number of valid chips it contains */
427	for (floor = 0; floor < MAX_FLOORS; floor++) {
428		ret = 1;
429		numchips[floor] = 0;
430		for (chip = 0; chip < maxchips && ret != 0; chip++) {
431
432			ret = DoC_IdentChip(this, floor, chip);
433			if (ret) {
434				numchips[floor]++;
435				this->numchips++;
436			}
437		}
438	}
439
440	/* If there are none at all that we recognise, bail */
441	if (!this->numchips) {
442		printk(KERN_NOTICE "No flash chips recognised.\n");
443		return;
444	}
445
446	/* Allocate an array to hold the information for each chip */
447	this->chips = kmalloc(sizeof(struct Nand) * this->numchips, GFP_KERNEL);
448	if (!this->chips) {
449		printk(KERN_NOTICE "No memory for allocating chip info structures\n");
450		return;
451	}
452
453	ret = 0;
454
455	/* Fill out the chip array with {floor, chipno} for each
456	 * detected chip in the device. */
457	for (floor = 0; floor < MAX_FLOORS; floor++) {
458		for (chip = 0; chip < numchips[floor]; chip++) {
459			this->chips[ret].floor = floor;
460			this->chips[ret].chip = chip;
461			this->chips[ret].curadr = 0;
462			this->chips[ret].curmode = 0x50;
463			ret++;
464		}
465	}
466
467	/* Calculate and print the total size of the device */
468	this->totlen = this->numchips * (1 << this->chipshift);
469
470	printk(KERN_INFO "%d flash chips found. Total DiskOnChip size: %ld MiB\n",
471	       this->numchips, this->totlen >> 20);
472}
473
474static int DoC2k_is_alias(struct DiskOnChip *doc1, struct DiskOnChip *doc2)
475{
476	int tmp1, tmp2, retval;
477	if (doc1->physadr == doc2->physadr)
478		return 1;
479
480	/* Use the alias resolution register which was set aside for this
481	 * purpose. If it's value is the same on both chips, they might
482	 * be the same chip, and we write to one and check for a change in
483	 * the other. It's unclear if this register is usuable in the
484	 * DoC 2000 (it's in the Millennium docs), but it seems to work. */
485	tmp1 = ReadDOC(doc1->virtadr, AliasResolution);
486	tmp2 = ReadDOC(doc2->virtadr, AliasResolution);
487	if (tmp1 != tmp2)
488		return 0;
489
490	WriteDOC((tmp1 + 1) % 0xff, doc1->virtadr, AliasResolution);
491	tmp2 = ReadDOC(doc2->virtadr, AliasResolution);
492	if (tmp2 == (tmp1 + 1) % 0xff)
493		retval = 1;
494	else
495		retval = 0;
496
497	/* Restore register contents.  May not be necessary, but do it just to
498	 * be safe. */
499	WriteDOC(tmp1, doc1->virtadr, AliasResolution);
500
501	return retval;
502}
503
504static const char im_name[] = "DoC2k_init";
505
506/* This routine is made available to other mtd code via
507 * inter_module_register.  It must only be accessed through
508 * inter_module_get which will bump the use count of this module.  The
509 * addresses passed back in mtd are valid as long as the use count of
510 * this module is non-zero, i.e. between inter_module_get and
511 * inter_module_put.  Keith Owens <kaos@ocs.com.au> 29 Oct 2000.
512 */
513static void DoC2k_init(struct mtd_info *mtd)
514{
515	struct DiskOnChip *this = (struct DiskOnChip *) mtd->priv;
516	struct DiskOnChip *old = NULL;
517
518	/* We must avoid being called twice for the same device. */
519
520	if (doc2klist)
521		old = (struct DiskOnChip *) doc2klist->priv;
522
523	while (old) {
524		if (DoC2k_is_alias(old, this)) {
525			printk(KERN_NOTICE
526			       "Ignoring DiskOnChip 2000 at 0x%lX - already configured\n",
527			       this->physadr);
528			iounmap((void *) this->virtadr);
529			kfree(mtd);
530			return;
531		}
532		if (old->nextdoc)
533			old = (struct DiskOnChip *) old->nextdoc->priv;
534		else
535			old = NULL;
536	}
537
538
539	switch (this->ChipID) {
540	case DOC_ChipID_Doc2k:
541		mtd->name = "DiskOnChip 2000";
542		this->ioreg = DoC_2k_CDSN_IO;
543		break;
544	case DOC_ChipID_DocMil:
545		mtd->name = "DiskOnChip Millennium";
546		this->ioreg = DoC_Mil_CDSN_IO;
547		break;
548	}
549
550	printk(KERN_NOTICE "%s found at address 0x%lX\n", mtd->name,
551	       this->physadr);
552
553	mtd->type = MTD_NANDFLASH;
554	mtd->flags = MTD_CAP_NANDFLASH;
555	mtd->size = 0;
556	mtd->erasesize = 0;
557	mtd->oobblock = 512;
558	mtd->oobsize = 16;
559	mtd->module = THIS_MODULE;
560	mtd->erase = doc_erase;
561	mtd->point = NULL;
562	mtd->unpoint = NULL;
563	mtd->read = doc_read;
564	mtd->write = doc_write;
565	mtd->read_ecc = doc_read_ecc;
566	mtd->write_ecc = doc_write_ecc;
567	mtd->read_oob = doc_read_oob;
568	mtd->write_oob = doc_write_oob;
569	mtd->sync = NULL;
570
571	this->totlen = 0;
572	this->numchips = 0;
573
574	this->curfloor = -1;
575	this->curchip = -1;
576	init_MUTEX(&this->lock);
577
578	/* Ident all the chips present. */
579	DoC_ScanChips(this);
580
581	if (!this->totlen) {
582		kfree(mtd);
583		iounmap((void *) this->virtadr);
584	} else {
585		this->nextdoc = doc2klist;
586		doc2klist = mtd;
587		mtd->size = this->totlen;
588		mtd->erasesize = this->erasesize;
589		add_mtd_device(mtd);
590		return;
591	}
592}
593
594static int doc_read(struct mtd_info *mtd, loff_t from, size_t len,
595		    size_t * retlen, u_char * buf)
596{
597	/* Just a special case of doc_read_ecc */
598	return doc_read_ecc(mtd, from, len, retlen, buf, NULL);
599}
600
601static int doc_read_ecc(struct mtd_info *mtd, loff_t from, size_t len,
602			size_t * retlen, u_char * buf, u_char * eccbuf)
603{
604	struct DiskOnChip *this = (struct DiskOnChip *) mtd->priv;
605	unsigned long docptr;
606	struct Nand *mychip;
607	unsigned char syndrome[6];
608	volatile char dummy;
609	int i, len256 = 0, ret=0;
610
611	docptr = this->virtadr;
612
613	/* Don't allow read past end of device */
614	if (from >= this->totlen)
615		return -EINVAL;
616
617	down(&this->lock);
618
619	/* Don't allow a single read to cross a 512-byte block boundary */
620	if (from + len > ((from | 0x1ff) + 1))
621		len = ((from | 0x1ff) + 1) - from;
622
623	/* The ECC will not be calculated correctly if less than 512 is read */
624	if (len != 0x200 && eccbuf)
625		printk(KERN_WARNING
626		       "ECC needs a full sector read (adr: %lx size %lx)\n",
627		       (long) from, (long) len);
628
629	/* printk("DoC_Read (adr: %lx size %lx)\n", (long) from, (long) len); */
630
631
632	/* Find the chip which is to be used and select it */
633	mychip = &this->chips[from >> (this->chipshift)];
634
635	if (this->curfloor != mychip->floor) {
636		DoC_SelectFloor(this, mychip->floor);
637		DoC_SelectChip(this, mychip->chip);
638	} else if (this->curchip != mychip->chip) {
639		DoC_SelectChip(this, mychip->chip);
640	}
641
642	this->curfloor = mychip->floor;
643	this->curchip = mychip->chip;
644
645	DoC_Command(this,
646		    (!this->page256
647		     && (from & 0x100)) ? NAND_CMD_READ1 : NAND_CMD_READ0,
648		    CDSN_CTRL_WP);
649	DoC_Address(this, ADDR_COLUMN_PAGE, from, CDSN_CTRL_WP,
650		    CDSN_CTRL_ECC_IO);
651
652	if (eccbuf) {
653		/* Prime the ECC engine */
654		WriteDOC(DOC_ECC_RESET, docptr, ECCConf);
655		WriteDOC(DOC_ECC_EN, docptr, ECCConf);
656	} else {
657		/* disable the ECC engine */
658		WriteDOC(DOC_ECC_RESET, docptr, ECCConf);
659		WriteDOC(DOC_ECC_DIS, docptr, ECCConf);
660	}
661
662	/* treat crossing 256-byte sector for 2M x 8bits devices */
663	if (this->page256 && from + len > (from | 0xff) + 1) {
664		len256 = (from | 0xff) + 1 - from;
665		DoC_ReadBuf(this, buf, len256);
666
667		DoC_Command(this, NAND_CMD_READ0, CDSN_CTRL_WP);
668		DoC_Address(this, ADDR_COLUMN_PAGE, from + len256,
669			    CDSN_CTRL_WP, CDSN_CTRL_ECC_IO);
670	}
671
672	DoC_ReadBuf(this, &buf[len256], len - len256);
673
674	/* Let the caller know we completed it */
675	*retlen = len;
676
677	if (eccbuf) {
678		/* Read the ECC data through the DiskOnChip ECC logic */
679		/* Note: this will work even with 2M x 8bit devices as   */
680		/*       they have 8 bytes of OOB per 256 page. mf.      */
681		DoC_ReadBuf(this, eccbuf, 6);
682
683		/* Flush the pipeline */
684		if (DoC_is_Millennium(this)) {
685			dummy = ReadDOC(docptr, ECCConf);
686			dummy = ReadDOC(docptr, ECCConf);
687			i = ReadDOC(docptr, ECCConf);
688		} else {
689			dummy = ReadDOC(docptr, 2k_ECCStatus);
690			dummy = ReadDOC(docptr, 2k_ECCStatus);
691			i = ReadDOC(docptr, 2k_ECCStatus);
692		}
693
694		/* Check the ECC Status */
695		if (i & 0x80) {
696			int nb_errors;
697			/* There was an ECC error */
698#ifdef ECC_DEBUG
699			printk(KERN_ERR "DiskOnChip ECC Error: Read at %lx\n", (long)from);
700#endif
701			/* Read the ECC syndrom through the DiskOnChip ECC logic.
702			   These syndrome will be all ZERO when there is no error */
703			for (i = 0; i < 6; i++) {
704				syndrome[i] =
705				    ReadDOC(docptr, ECCSyndrome0 + i);
706			}
707                        nb_errors = doc_decode_ecc(buf, syndrome);
708
709#ifdef ECC_DEBUG
710			printk(KERN_ERR "Errors corrected: %x\n", nb_errors);
711#endif
712                        if (nb_errors < 0) {
713				/* We return error, but have actually done the read. Not that
714				   this can be told to user-space, via sys_read(), but at least
715				   MTD-aware stuff can know about it by checking *retlen */
716				ret = -EIO;
717                        }
718		}
719
720#ifdef PSYCHO_DEBUG
721		printk(KERN_DEBUG "ECC DATA at %lxB: %2.2X %2.2X %2.2X %2.2X %2.2X %2.2X\n",
722			     (long)from, eccbuf[0], eccbuf[1], eccbuf[2],
723			     eccbuf[3], eccbuf[4], eccbuf[5]);
724#endif
725
726		/* disable the ECC engine */
727		WriteDOC(DOC_ECC_DIS, docptr , ECCConf);
728	}
729
730	/* according to 11.4.1, we need to wait for the busy line
731         * drop if we read to the end of the page.  */
732	if(0 == ((from + *retlen) & 0x1ff))
733	{
734	    DoC_WaitReady(this);
735	}
736
737	up(&this->lock);
738
739	return ret;
740}
741
742static int doc_write(struct mtd_info *mtd, loff_t to, size_t len,
743		     size_t * retlen, const u_char * buf)
744{
745	char eccbuf[6];
746	return doc_write_ecc(mtd, to, len, retlen, buf, eccbuf);
747}
748
749static int doc_write_ecc(struct mtd_info *mtd, loff_t to, size_t len,
750			 size_t * retlen, const u_char * buf,
751			 u_char * eccbuf)
752{
753	struct DiskOnChip *this = (struct DiskOnChip *) mtd->priv;
754	int di; /* Yes, DI is a hangover from when I was disassembling the binary driver */
755	unsigned long docptr;
756	volatile char dummy;
757	int len256 = 0;
758	struct Nand *mychip;
759
760	docptr = this->virtadr;
761
762	/* Don't allow write past end of device */
763	if (to >= this->totlen)
764		return -EINVAL;
765
766	down(&this->lock);
767
768	/* Don't allow a single write to cross a 512-byte block boundary */
769	if (to + len > ((to | 0x1ff) + 1))
770		len = ((to | 0x1ff) + 1) - to;
771
772	/* The ECC will not be calculated correctly if less than 512 is written */
773	if (len != 0x200 && eccbuf)
774		printk(KERN_WARNING
775		       "ECC needs a full sector write (adr: %lx size %lx)\n",
776		       (long) to, (long) len);
777
778	/* printk("DoC_Write (adr: %lx size %lx)\n", (long) to, (long) len); */
779
780	/* Find the chip which is to be used and select it */
781	mychip = &this->chips[to >> (this->chipshift)];
782
783	if (this->curfloor != mychip->floor) {
784		DoC_SelectFloor(this, mychip->floor);
785		DoC_SelectChip(this, mychip->chip);
786	} else if (this->curchip != mychip->chip) {
787		DoC_SelectChip(this, mychip->chip);
788	}
789
790	this->curfloor = mychip->floor;
791	this->curchip = mychip->chip;
792
793	/* Set device to main plane of flash */
794	DoC_Command(this, NAND_CMD_RESET, CDSN_CTRL_WP);
795	DoC_Command(this,
796		    (!this->page256
797		     && (to & 0x100)) ? NAND_CMD_READ1 : NAND_CMD_READ0,
798		    CDSN_CTRL_WP);
799
800	DoC_Command(this, NAND_CMD_SEQIN, 0);
801	DoC_Address(this, ADDR_COLUMN_PAGE, to, 0, CDSN_CTRL_ECC_IO);
802
803	if (eccbuf) {
804		/* Prime the ECC engine */
805		WriteDOC(DOC_ECC_RESET, docptr, ECCConf);
806		WriteDOC(DOC_ECC_EN | DOC_ECC_RW, docptr, ECCConf);
807	} else {
808		/* disable the ECC engine */
809		WriteDOC(DOC_ECC_RESET, docptr, ECCConf);
810		WriteDOC(DOC_ECC_DIS, docptr, ECCConf);
811	}
812
813	/* treat crossing 256-byte sector for 2M x 8bits devices */
814	if (this->page256 && to + len > (to | 0xff) + 1) {
815		len256 = (to | 0xff) + 1 - to;
816		DoC_WriteBuf(this, buf, len256);
817
818		DoC_Command(this, NAND_CMD_PAGEPROG, 0);
819
820		DoC_Command(this, NAND_CMD_STATUS, CDSN_CTRL_WP);
821		/* There's an implicit DoC_WaitReady() in DoC_Command */
822
823		dummy = ReadDOC(docptr, CDSNSlowIO);
824		DoC_Delay(this, 2);
825
826		if (ReadDOC_(docptr, this->ioreg) & 1) {
827			printk(KERN_ERR "Error programming flash\n");
828			/* Error in programming */
829			*retlen = 0;
830			up(&this->lock);
831			return -EIO;
832		}
833
834		DoC_Command(this, NAND_CMD_SEQIN, 0);
835		DoC_Address(this, ADDR_COLUMN_PAGE, to + len256, 0,
836			    CDSN_CTRL_ECC_IO);
837	}
838
839	DoC_WriteBuf(this, &buf[len256], len - len256);
840
841	if (eccbuf) {
842		WriteDOC(CDSN_CTRL_ECC_IO | CDSN_CTRL_CE, docptr,
843			 CDSNControl);
844
845		if (DoC_is_Millennium(this)) {
846			WriteDOC(0, docptr, NOP);
847			WriteDOC(0, docptr, NOP);
848			WriteDOC(0, docptr, NOP);
849		} else {
850			WriteDOC_(0, docptr, this->ioreg);
851			WriteDOC_(0, docptr, this->ioreg);
852			WriteDOC_(0, docptr, this->ioreg);
853		}
854
855		/* Read the ECC data through the DiskOnChip ECC logic */
856		for (di = 0; di < 6; di++) {
857			eccbuf[di] = ReadDOC(docptr, ECCSyndrome0 + di);
858		}
859
860		/* Reset the ECC engine */
861		WriteDOC(DOC_ECC_DIS, docptr, ECCConf);
862
863#ifdef PSYCHO_DEBUG
864		printk
865		    ("OOB data at %lx is %2.2X %2.2X %2.2X %2.2X %2.2X %2.2X\n",
866		     (long) to, eccbuf[0], eccbuf[1], eccbuf[2], eccbuf[3],
867		     eccbuf[4], eccbuf[5]);
868#endif
869	}
870
871	DoC_Command(this, NAND_CMD_PAGEPROG, 0);
872
873	DoC_Command(this, NAND_CMD_STATUS, CDSN_CTRL_WP);
874	/* There's an implicit DoC_WaitReady() in DoC_Command */
875
876	dummy = ReadDOC(docptr, CDSNSlowIO);
877	DoC_Delay(this, 2);
878
879	if (ReadDOC_(docptr, this->ioreg) & 1) {
880		printk(KERN_ERR "Error programming flash\n");
881		/* Error in programming */
882		*retlen = 0;
883		up(&this->lock);
884		return -EIO;
885	}
886
887	/* Let the caller know we completed it */
888	*retlen = len;
889
890	if (eccbuf) {
891		unsigned char x[8];
892		size_t dummy;
893		int ret;
894
895		/* Write the ECC data to flash */
896		for (di=0; di<6; di++)
897			x[di] = eccbuf[di];
898
899		x[6]=0x55;
900		x[7]=0x55;
901
902		ret = doc_write_oob_nolock(mtd, to, 8, &dummy, x);
903		up(&this->lock);
904		return ret;
905	}
906	up(&this->lock);
907	return 0;
908}
909
910static int doc_read_oob(struct mtd_info *mtd, loff_t ofs, size_t len,
911			size_t * retlen, u_char * buf)
912{
913	struct DiskOnChip *this = (struct DiskOnChip *) mtd->priv;
914	int len256 = 0, ret;
915	unsigned long docptr;
916	struct Nand *mychip;
917
918	down(&this->lock);
919
920	docptr = this->virtadr;
921
922	mychip = &this->chips[ofs >> this->chipshift];
923
924	if (this->curfloor != mychip->floor) {
925		DoC_SelectFloor(this, mychip->floor);
926		DoC_SelectChip(this, mychip->chip);
927	} else if (this->curchip != mychip->chip) {
928		DoC_SelectChip(this, mychip->chip);
929	}
930	this->curfloor = mychip->floor;
931	this->curchip = mychip->chip;
932
933	/* update address for 2M x 8bit devices. OOB starts on the second */
934	/* page to maintain compatibility with doc_read_ecc. */
935	if (this->page256) {
936		if (!(ofs & 0x8))
937			ofs += 0x100;
938		else
939			ofs -= 0x8;
940	}
941
942	DoC_Command(this, NAND_CMD_READOOB, CDSN_CTRL_WP);
943	DoC_Address(this, ADDR_COLUMN_PAGE, ofs, CDSN_CTRL_WP, 0);
944
945	/* treat crossing 8-byte OOB data for 2M x 8bit devices */
946	/* Note: datasheet says it should automaticaly wrap to the */
947	/*       next OOB block, but it didn't work here. mf.      */
948	if (this->page256 && ofs + len > (ofs | 0x7) + 1) {
949		len256 = (ofs | 0x7) + 1 - ofs;
950		DoC_ReadBuf(this, buf, len256);
951
952		DoC_Command(this, NAND_CMD_READOOB, CDSN_CTRL_WP);
953		DoC_Address(this, ADDR_COLUMN_PAGE, ofs & (~0x1ff),
954			    CDSN_CTRL_WP, 0);
955	}
956
957	DoC_ReadBuf(this, &buf[len256], len - len256);
958
959	*retlen = len;
960	/* Reading the full OOB data drops us off of the end of the page,
961         * causing the flash device to go into busy mode, so we need
962         * to wait until ready 11.4.1 and Toshiba TC58256FT docs */
963
964	ret = DoC_WaitReady(this);
965
966	up(&this->lock);
967	return ret;
968
969}
970
971static int doc_write_oob_nolock(struct mtd_info *mtd, loff_t ofs, size_t len,
972				size_t * retlen, const u_char * buf)
973{
974	struct DiskOnChip *this = (struct DiskOnChip *) mtd->priv;
975	int len256 = 0;
976	unsigned long docptr = this->virtadr;
977	struct Nand *mychip = &this->chips[ofs >> this->chipshift];
978	volatile int dummy;
979
980	//      printk("doc_write_oob(%lx, %d): %2.2X %2.2X %2.2X %2.2X ... %2.2X %2.2X .. %2.2X %2.2X\n",(long)ofs, len,
981	//   buf[0], buf[1], buf[2], buf[3], buf[8], buf[9], buf[14],buf[15]);
982
983	/* Find the chip which is to be used and select it */
984	if (this->curfloor != mychip->floor) {
985		DoC_SelectFloor(this, mychip->floor);
986		DoC_SelectChip(this, mychip->chip);
987	} else if (this->curchip != mychip->chip) {
988		DoC_SelectChip(this, mychip->chip);
989	}
990	this->curfloor = mychip->floor;
991	this->curchip = mychip->chip;
992
993	/* disable the ECC engine */
994	WriteDOC (DOC_ECC_RESET, docptr, ECCConf);
995	WriteDOC (DOC_ECC_DIS, docptr, ECCConf);
996
997	/* Reset the chip, see Software Requirement 11.4 item 1. */
998	DoC_Command(this, NAND_CMD_RESET, CDSN_CTRL_WP);
999
1000	/* issue the Read2 command to set the pointer to the Spare Data Area. */
1001	DoC_Command(this, NAND_CMD_READOOB, CDSN_CTRL_WP);
1002
1003	/* update address for 2M x 8bit devices. OOB starts on the second */
1004	/* page to maintain compatibility with doc_read_ecc. */
1005	if (this->page256) {
1006		if (!(ofs & 0x8))
1007			ofs += 0x100;
1008		else
1009			ofs -= 0x8;
1010	}
1011
1012	/* issue the Serial Data In command to initial the Page Program process */
1013	DoC_Command(this, NAND_CMD_SEQIN, 0);
1014	DoC_Address(this, ADDR_COLUMN_PAGE, ofs, 0, 0);
1015
1016	/* treat crossing 8-byte OOB data for 2M x 8bit devices */
1017	/* Note: datasheet says it should automaticaly wrap to the */
1018	/*       next OOB block, but it didn't work here. mf.      */
1019	if (this->page256 && ofs + len > (ofs | 0x7) + 1) {
1020		len256 = (ofs | 0x7) + 1 - ofs;
1021		DoC_WriteBuf(this, buf, len256);
1022
1023		DoC_Command(this, NAND_CMD_PAGEPROG, 0);
1024		DoC_Command(this, NAND_CMD_STATUS, 0);
1025		/* DoC_WaitReady() is implicit in DoC_Command */
1026
1027		dummy = ReadDOC(docptr, CDSNSlowIO);
1028		DoC_Delay(this, 2);
1029
1030		if (ReadDOC_(docptr, this->ioreg) & 1) {
1031			printk(KERN_ERR "Error programming oob data\n");
1032			/* There was an error */
1033			*retlen = 0;
1034			return -EIO;
1035		}
1036		DoC_Command(this, NAND_CMD_SEQIN, 0);
1037		DoC_Address(this, ADDR_COLUMN_PAGE, ofs & (~0x1ff), 0, 0);
1038	}
1039
1040	DoC_WriteBuf(this, &buf[len256], len - len256);
1041
1042	DoC_Command(this, NAND_CMD_PAGEPROG, 0);
1043	DoC_Command(this, NAND_CMD_STATUS, 0);
1044	/* DoC_WaitReady() is implicit in DoC_Command */
1045
1046	dummy = ReadDOC(docptr, CDSNSlowIO);
1047	DoC_Delay(this, 2);
1048
1049	if (ReadDOC_(docptr, this->ioreg) & 1) {
1050		printk(KERN_ERR "Error programming oob data\n");
1051		/* There was an error */
1052		*retlen = 0;
1053		return -EIO;
1054	}
1055
1056	*retlen = len;
1057	return 0;
1058
1059}
1060
1061static int doc_write_oob(struct mtd_info *mtd, loff_t ofs, size_t len,
1062 			 size_t * retlen, const u_char * buf)
1063{
1064 	struct DiskOnChip *this = (struct DiskOnChip *) mtd->priv;
1065 	int ret;
1066
1067 	down(&this->lock);
1068 	ret = doc_write_oob_nolock(mtd, ofs, len, retlen, buf);
1069
1070 	up(&this->lock);
1071 	return ret;
1072}
1073
1074static int doc_erase(struct mtd_info *mtd, struct erase_info *instr)
1075{
1076	struct DiskOnChip *this = (struct DiskOnChip *) mtd->priv;
1077	__u32 ofs = instr->addr;
1078	__u32 len = instr->len;
1079	volatile int dummy;
1080	unsigned long docptr;
1081	struct Nand *mychip;
1082
1083 	down(&this->lock);
1084
1085	if (ofs & (mtd->erasesize-1) || len & (mtd->erasesize-1)) {
1086		up(&this->lock);
1087		return -EINVAL;
1088	}
1089
1090	instr->state = MTD_ERASING;
1091
1092	docptr = this->virtadr;
1093
1094	while(len) {
1095		mychip = &this->chips[ofs >> this->chipshift];
1096
1097		if (this->curfloor != mychip->floor) {
1098			DoC_SelectFloor(this, mychip->floor);
1099			DoC_SelectChip(this, mychip->chip);
1100		} else if (this->curchip != mychip->chip) {
1101			DoC_SelectChip(this, mychip->chip);
1102		}
1103		this->curfloor = mychip->floor;
1104		this->curchip = mychip->chip;
1105
1106		DoC_Command(this, NAND_CMD_ERASE1, 0);
1107		DoC_Address(this, ADDR_PAGE, ofs, 0, 0);
1108		DoC_Command(this, NAND_CMD_ERASE2, 0);
1109
1110		DoC_Command(this, NAND_CMD_STATUS, CDSN_CTRL_WP);
1111
1112		dummy = ReadDOC(docptr, CDSNSlowIO);
1113		DoC_Delay(this, 2);
1114
1115		if (ReadDOC_(docptr, this->ioreg) & 1) {
1116			printk(KERN_ERR "Error erasing at 0x%x\n", ofs);
1117			/* There was an error */
1118			instr->state = MTD_ERASE_FAILED;
1119			goto callback;
1120		}
1121		ofs += mtd->erasesize;
1122		len -= mtd->erasesize;
1123	}
1124	instr->state = MTD_ERASE_DONE;
1125
1126 callback:
1127	if (instr->callback)
1128		instr->callback(instr);
1129
1130	up(&this->lock);
1131	return 0;
1132}
1133
1134
1135/****************************************************************************
1136 *
1137 * Module stuff
1138 *
1139 ****************************************************************************/
1140
1141int __init init_doc2000(void)
1142{
1143       inter_module_register(im_name, THIS_MODULE, &DoC2k_init);
1144       return 0;
1145}
1146
1147static void __exit cleanup_doc2000(void)
1148{
1149	struct mtd_info *mtd;
1150	struct DiskOnChip *this;
1151
1152	while ((mtd = doc2klist)) {
1153		this = (struct DiskOnChip *) mtd->priv;
1154		doc2klist = this->nextdoc;
1155
1156		del_mtd_device(mtd);
1157
1158		iounmap((void *) this->virtadr);
1159		kfree(this->chips);
1160		kfree(mtd);
1161	}
1162	inter_module_unregister(im_name);
1163}
1164
1165module_exit(cleanup_doc2000);
1166module_init(init_doc2000);
1167
1168MODULE_LICENSE("GPL");
1169MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org> et al.");
1170MODULE_DESCRIPTION("MTD driver for DiskOnChip 2000 and Millennium");
1171
1172