File: /usr/src/linux/drivers/mtd/devices/doc2001.c

1     
2     /*
3      * Linux driver for Disk-On-Chip Millennium
4      * (c) 1999 Machine Vision Holdings, Inc.
5      * (c) 1999, 2000 David Woodhouse <dwmw2@infradead.org>
6      *
7      * $Id: doc2001.c,v 1.34 2001/06/02 14:30:43 dwmw2 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 ECC_DEBUG */
29     
30     /* I have no idea why some DoC chips can not use memcop_form|to_io().
31      * This may be due to the different revisions of the ASIC controller built-in or
32      * simplily a QA/Bug issue. Who knows ?? If you have trouble, please uncomment
33      * this:*/
34     #undef USE_MEMCPY
35     
36     static int doc_read(struct mtd_info *mtd, loff_t from, size_t len,
37     		    size_t *retlen, u_char *buf);
38     static int doc_write(struct mtd_info *mtd, loff_t to, size_t len,
39     		     size_t *retlen, const u_char *buf);
40     static int doc_read_ecc(struct mtd_info *mtd, loff_t from, size_t len,
41     			size_t *retlen, u_char *buf, u_char *eccbuf);
42     static int doc_write_ecc(struct mtd_info *mtd, loff_t to, size_t len,
43     			 size_t *retlen, const u_char *buf, u_char *eccbuf);
44     static int doc_read_oob(struct mtd_info *mtd, loff_t ofs, size_t len,
45     			size_t *retlen, u_char *buf);
46     static int doc_write_oob(struct mtd_info *mtd, loff_t ofs, size_t len,
47     			 size_t *retlen, const u_char *buf);
48     static int doc_erase (struct mtd_info *mtd, struct erase_info *instr);
49     
50     static struct mtd_info *docmillist = NULL;
51     
52     /* Perform the required delay cycles by reading from the NOP register */
53     static void DoC_Delay(unsigned long docptr, unsigned short cycles)
54     {
55     	volatile char dummy;
56     	int i;
57     
58     	for (i = 0; i < cycles; i++)
59     		dummy = ReadDOC(docptr, NOP);
60     }
61     
62     /* DOC_WaitReady: Wait for RDY line to be asserted by the flash chip */
63     static int _DoC_WaitReady(unsigned long docptr)
64     {
65     	unsigned short c = 0xffff;
66     
67     	DEBUG(MTD_DEBUG_LEVEL3,
68     	      "_DoC_WaitReady called for out-of-line wait\n");
69     
70     	/* Out-of-line routine to wait for chip response */
71     	while (!(ReadDOC(docptr, CDSNControl) & CDSN_CTRL_FR_B) && --c)
72     		;
73     
74     	if (c == 0)
75     		DEBUG(MTD_DEBUG_LEVEL2, "_DoC_WaitReady timed out.\n");
76     
77     	return (c == 0);
78     }
79     
80     static inline int DoC_WaitReady(unsigned long docptr)
81     {
82     	/* This is inline, to optimise the common case, where it's ready instantly */
83     	int ret = 0;
84     
85     	/* 4 read form NOP register should be issued in prior to the read from CDSNControl
86     	   see Software Requirement 11.4 item 2. */
87     	DoC_Delay(docptr, 4);
88     
89     	if (!(ReadDOC(docptr, CDSNControl) & CDSN_CTRL_FR_B))
90     		/* Call the out-of-line routine to wait */
91     		ret = _DoC_WaitReady(docptr);
92     
93     	/* issue 2 read from NOP register after reading from CDSNControl register
94     	   see Software Requirement 11.4 item 2. */
95     	DoC_Delay(docptr, 2);
96     
97     	return ret;
98     }
99     
100     /* DoC_Command: Send a flash command to the flash chip through the CDSN IO register
101        with the internal pipeline. Each of 4 delay cycles (read from the NOP register) is
102        required after writing to CDSN Control register, see Software Requirement 11.4 item 3. */
103     
104     static inline void DoC_Command(unsigned long docptr, unsigned char command,
105     			       unsigned char xtraflags)
106     {
107     	/* Assert the CLE (Command Latch Enable) line to the flash chip */
108     	WriteDOC(xtraflags | CDSN_CTRL_CLE | CDSN_CTRL_CE, docptr, CDSNControl);
109     	DoC_Delay(docptr, 4);
110     
111     	/* Send the command */
112     	WriteDOC(command, docptr, Mil_CDSN_IO);
113     	WriteDOC(0x00, docptr, WritePipeTerm);
114     
115     	/* Lower the CLE line */
116     	WriteDOC(xtraflags | CDSN_CTRL_CE, docptr, CDSNControl);
117     	DoC_Delay(docptr, 4);
118     }
119     
120     /* DoC_Address: Set the current address for the flash chip through the CDSN IO register
121        with the internal pipeline. Each of 4 delay cycles (read from the NOP register) is
122        required after writing to CDSN Control register, see Software Requirement 11.4 item 3. */
123     
124     static inline void DoC_Address(unsigned long docptr, int numbytes, unsigned long ofs,
125     			       unsigned char xtraflags1, unsigned char xtraflags2)
126     {
127     	/* Assert the ALE (Address Latch Enable) line to the flash chip */
128     	WriteDOC(xtraflags1 | CDSN_CTRL_ALE | CDSN_CTRL_CE, docptr, CDSNControl);
129     	DoC_Delay(docptr, 4);
130     
131     	/* Send the address */
132     	switch (numbytes)
133     	    {
134     	    case 1:
135     		    /* Send single byte, bits 0-7. */
136     		    WriteDOC(ofs & 0xff, docptr, Mil_CDSN_IO);
137     		    WriteDOC(0x00, docptr, WritePipeTerm);
138     		    break;
139     	    case 2:
140     		    /* Send bits 9-16 followed by 17-23 */
141     		    WriteDOC((ofs >> 9)  & 0xff, docptr, Mil_CDSN_IO);
142     		    WriteDOC((ofs >> 17) & 0xff, docptr, Mil_CDSN_IO);
143     		    WriteDOC(0x00, docptr, WritePipeTerm);
144     		break;
145     	    case 3:
146     		    /* Send 0-7, 9-16, then 17-23 */
147     		    WriteDOC(ofs & 0xff, docptr, Mil_CDSN_IO);
148     		    WriteDOC((ofs >> 9)  & 0xff, docptr, Mil_CDSN_IO);
149     		    WriteDOC((ofs >> 17) & 0xff, docptr, Mil_CDSN_IO);
150     		    WriteDOC(0x00, docptr, WritePipeTerm);
151     		break;
152     	    default:
153     		return;
154     	    }
155     
156     	/* Lower the ALE line */
157     	WriteDOC(xtraflags1 | xtraflags2 | CDSN_CTRL_CE, docptr, CDSNControl);
158     	DoC_Delay(docptr, 4);
159     }
160     
161     /* DoC_SelectChip: Select a given flash chip within the current floor */
162     static int DoC_SelectChip(unsigned long docptr, int chip)
163     {
164     	/* Select the individual flash chip requested */
165     	WriteDOC(chip, docptr, CDSNDeviceSelect);
166     	DoC_Delay(docptr, 4);
167     
168     	/* Wait for it to be ready */
169     	return DoC_WaitReady(docptr);
170     }
171     
172     /* DoC_SelectFloor: Select a given floor (bank of flash chips) */
173     static int DoC_SelectFloor(unsigned long docptr, int floor)
174     {
175     	/* Select the floor (bank) of chips required */
176     	WriteDOC(floor, docptr, FloorSelect);
177     
178     	/* Wait for the chip to be ready */
179     	return DoC_WaitReady(docptr);
180     }
181     
182     /* DoC_IdentChip: Identify a given NAND chip given {floor,chip} */
183     static int DoC_IdentChip(struct DiskOnChip *doc, int floor, int chip)
184     {
185     	int mfr, id, i;
186     	volatile char dummy;
187     
188     	/* Page in the required floor/chip
189     	   FIXME: is this supported by Millennium ?? */
190     	DoC_SelectFloor(doc->virtadr, floor);
191     	DoC_SelectChip(doc->virtadr, chip);
192     
193     	/* Reset the chip, see Software Requirement 11.4 item 1. */
194     	DoC_Command(doc->virtadr, NAND_CMD_RESET, CDSN_CTRL_WP);
195     	DoC_WaitReady(doc->virtadr);
196     
197     	/* Read the NAND chip ID: 1. Send ReadID command */ 
198     	DoC_Command(doc->virtadr, NAND_CMD_READID, CDSN_CTRL_WP);
199     
200     	/* Read the NAND chip ID: 2. Send address byte zero */ 
201     	DoC_Address(doc->virtadr, 1, 0x00, CDSN_CTRL_WP, 0x00);
202     
203     	/* Read the manufacturer and device id codes of the flash device through
204     	   CDSN IO register see Software Requirement 11.4 item 5.*/
205     	dummy = ReadDOC(doc->virtadr, ReadPipeInit);
206     	DoC_Delay(doc->virtadr, 2);
207     	mfr = ReadDOC(doc->virtadr, Mil_CDSN_IO);
208     
209     	DoC_Delay(doc->virtadr, 2);
210     	id  = ReadDOC(doc->virtadr, Mil_CDSN_IO);
211     	dummy = ReadDOC(doc->virtadr, LastDataRead);
212     
213     	/* No response - return failure */
214     	if (mfr == 0xff || mfr == 0)
215     		return 0;
216     
217     	/* FIXME: to deal with multi-flash on multi-Millennium case more carefully */
218     	for (i = 0; nand_flash_ids[i].name != NULL; i++) {
219     		if (mfr == nand_flash_ids[i].manufacture_id &&
220     		    id == nand_flash_ids[i].model_id) {
221     			printk(KERN_INFO "Flash chip found: Manufacturer ID: %2.2X, "
222     			       "Chip ID: %2.2X (%s)\n",
223     			       mfr, id, nand_flash_ids[i].name);
224     			doc->mfr = mfr;
225     			doc->id = id;
226     			doc->chipshift = nand_flash_ids[i].chipshift;
227     			break;
228     		}
229     	}
230     
231     	if (nand_flash_ids[i].name == NULL)
232     		return 0;
233     	else
234     		return 1;
235     }
236     
237     /* DoC_ScanChips: Find all NAND chips present in a DiskOnChip, and identify them */
238     static void DoC_ScanChips(struct DiskOnChip *this)
239     {
240     	int floor, chip;
241     	int numchips[MAX_FLOORS_MIL];
242     	int ret;
243     
244     	this->numchips = 0;
245     	this->mfr = 0;
246     	this->id = 0;
247     
248     	/* For each floor, find the number of valid chips it contains */
249     	for (floor = 0,ret = 1; floor < MAX_FLOORS_MIL; floor++) {
250     		numchips[floor] = 0;
251     		for (chip = 0; chip < MAX_CHIPS_MIL && ret != 0; chip++) {
252     			ret = DoC_IdentChip(this, floor, chip);
253     			if (ret) {
254     				numchips[floor]++;
255     				this->numchips++;
256     			}
257     		}
258     	}
259     	/* If there are none at all that we recognise, bail */
260     	if (!this->numchips) {
261     		printk("No flash chips recognised.\n");
262     		return;
263     	}
264     
265     	/* Allocate an array to hold the information for each chip */
266     	this->chips = kmalloc(sizeof(struct Nand) * this->numchips, GFP_KERNEL);
267     	if (!this->chips){
268     		printk("No memory for allocating chip info structures\n");
269     		return;
270     	}
271     
272     	/* Fill out the chip array with {floor, chipno} for each 
273     	 * detected chip in the device. */
274     	for (floor = 0, ret = 0; floor < MAX_FLOORS_MIL; floor++) {
275     		for (chip = 0 ; chip < numchips[floor] ; chip++) {
276     			this->chips[ret].floor = floor;
277     			this->chips[ret].chip = chip;
278     			this->chips[ret].curadr = 0;
279     			this->chips[ret].curmode = 0x50;
280     			ret++;
281     		}
282     	}
283     
284     	/* Calculate and print the total size of the device */
285     	this->totlen = this->numchips * (1 << this->chipshift);
286     	printk(KERN_INFO "%d flash chips found. Total DiskOnChip size: %ld MiB\n",
287     	       this->numchips ,this->totlen >> 20);
288     }
289     
290     static int DoCMil_is_alias(struct DiskOnChip *doc1, struct DiskOnChip *doc2)
291     {
292     	int tmp1, tmp2, retval;
293     
294     	if (doc1->physadr == doc2->physadr)
295     		return 1;
296     
297     	/* Use the alias resolution register which was set aside for this
298     	 * purpose. If it's value is the same on both chips, they might
299     	 * be the same chip, and we write to one and check for a change in
300     	 * the other. It's unclear if this register is usuable in the
301     	 * DoC 2000 (it's in the Millenium docs), but it seems to work. */
302     	tmp1 = ReadDOC(doc1->virtadr, AliasResolution);
303     	tmp2 = ReadDOC(doc2->virtadr, AliasResolution);
304     	if (tmp1 != tmp2)
305     		return 0;
306     	
307     	WriteDOC((tmp1+1) % 0xff, doc1->virtadr, AliasResolution);
308     	tmp2 = ReadDOC(doc2->virtadr, AliasResolution);
309     	if (tmp2 == (tmp1+1) % 0xff)
310     		retval = 1;
311     	else
312     		retval = 0;
313     
314     	/* Restore register contents.  May not be necessary, but do it just to
315     	 * be safe. */
316     	WriteDOC(tmp1, doc1->virtadr, AliasResolution);
317     
318     	return retval;
319     }
320     
321     static const char im_name[] = "DoCMil_init";
322     
323     /* This routine is made available to other mtd code via
324      * inter_module_register.  It must only be accessed through
325      * inter_module_get which will bump the use count of this module.  The
326      * addresses passed back in mtd are valid as long as the use count of
327      * this module is non-zero, i.e. between inter_module_get and
328      * inter_module_put.  Keith Owens <kaos@ocs.com.au> 29 Oct 2000.
329      */
330     static void DoCMil_init(struct mtd_info *mtd)
331     {
332     	struct DiskOnChip *this = (struct DiskOnChip *)mtd->priv;
333     	struct DiskOnChip *old = NULL;
334     
335     	/* We must avoid being called twice for the same device. */
336     	if (docmillist)
337     		old = (struct DiskOnChip *)docmillist->priv;
338     
339     	while (old) {
340     		if (DoCMil_is_alias(this, old)) {
341     			printk(KERN_NOTICE "Ignoring DiskOnChip Millennium at "
342     			       "0x%lX - already configured\n", this->physadr);
343     			iounmap((void *)this->virtadr);
344     			kfree(mtd);
345     			return;
346     		}
347     		if (old->nextdoc)
348     			old = (struct DiskOnChip *)old->nextdoc->priv;
349     		else
350     			old = NULL;
351     	}
352     
353     	mtd->name = "DiskOnChip Millennium";
354     	printk(KERN_NOTICE "DiskOnChip Millennium found at address 0x%lX\n",
355     	       this->physadr);
356     
357     	mtd->type = MTD_NANDFLASH;
358     	mtd->flags = MTD_CAP_NANDFLASH;
359     	mtd->size = 0;
360     
361     	/* FIXME: erase size is not always 8kB */
362     	mtd->erasesize = 0x2000;
363     
364     	mtd->oobblock = 512;
365     	mtd->oobsize = 16;
366     	mtd->module = THIS_MODULE;
367     	mtd->erase = doc_erase;
368     	mtd->point = NULL;
369     	mtd->unpoint = NULL;
370     	mtd->read = doc_read;
371     	mtd->write = doc_write;
372     	mtd->read_ecc = doc_read_ecc;
373     	mtd->write_ecc = doc_write_ecc;
374     	mtd->read_oob = doc_read_oob;
375     	mtd->write_oob = doc_write_oob;
376     	mtd->sync = NULL;
377     
378     	this->totlen = 0;
379     	this->numchips = 0;
380     	this->curfloor = -1;
381     	this->curchip = -1;
382     
383     	/* Ident all the chips present. */
384     	DoC_ScanChips(this);
385     
386     	if (!this->totlen) {
387     		kfree(mtd);
388     		iounmap((void *)this->virtadr);
389     	} else {
390     		this->nextdoc = docmillist;
391     		docmillist = mtd;
392     		mtd->size  = this->totlen;
393     		add_mtd_device(mtd);
394     		return;
395     	}
396     }
397     
398     static int doc_read (struct mtd_info *mtd, loff_t from, size_t len,
399     		     size_t *retlen, u_char *buf)
400     {
401     	/* Just a special case of doc_read_ecc */
402     	return doc_read_ecc(mtd, from, len, retlen, buf, NULL);
403     }
404     
405     static int doc_read_ecc (struct mtd_info *mtd, loff_t from, size_t len,
406     			 size_t *retlen, u_char *buf, u_char *eccbuf)
407     {
408     	int i, ret;
409     	volatile char dummy;
410     	unsigned char syndrome[6];
411     	struct DiskOnChip *this = (struct DiskOnChip *)mtd->priv;
412     	unsigned long docptr = this->virtadr;
413     	struct Nand *mychip = &this->chips[from >> (this->chipshift)];
414     
415     	/* Don't allow read past end of device */
416     	if (from >= this->totlen)
417     		return -EINVAL;
418     
419     	/* Don't allow a single read to cross a 512-byte block boundary */
420     	if (from + len > ((from | 0x1ff) + 1)) 
421     		len = ((from | 0x1ff) + 1) - from;
422     
423     	/* Find the chip which is to be used and select it */
424     	if (this->curfloor != mychip->floor) {
425     		DoC_SelectFloor(docptr, mychip->floor);
426     		DoC_SelectChip(docptr, mychip->chip);
427     	} else if (this->curchip != mychip->chip) {
428     		DoC_SelectChip(docptr, mychip->chip);
429     	}
430     	this->curfloor = mychip->floor;
431     	this->curchip = mychip->chip;
432     
433     	/* issue the Read0 or Read1 command depend on which half of the page
434     	   we are accessing. Polling the Flash Ready bit after issue 3 bytes
435     	   address in Sequence Read Mode, see Software Requirement 11.4 item 1.*/
436     	DoC_Command(docptr, (from >> 8) & 1, CDSN_CTRL_WP);
437     	DoC_Address(docptr, 3, from, CDSN_CTRL_WP, 0x00);
438     	DoC_WaitReady(docptr);
439     
440     	if (eccbuf) {
441     		/* init the ECC engine, see Reed-Solomon EDC/ECC 11.1 .*/
442     		WriteDOC (DOC_ECC_RESET, docptr, ECCConf);
443     		WriteDOC (DOC_ECC_EN, docptr, ECCConf);
444     	} else {
445     		/* disable the ECC engine */
446     		WriteDOC (DOC_ECC_RESET, docptr, ECCConf);
447     		WriteDOC (DOC_ECC_DIS, docptr, ECCConf);
448     	}
449     
450     	/* Read the data via the internal pipeline through CDSN IO register,
451     	   see Pipelined Read Operations 11.3 */
452     	dummy = ReadDOC(docptr, ReadPipeInit);
453     #ifndef USE_MEMCPY
454     	for (i = 0; i < len-1; i++) {
455     		/* N.B. you have to increase the source address in this way or the
456     		   ECC logic will not work properly */
457     		buf[i] = ReadDOC(docptr, Mil_CDSN_IO + (i & 0xff));
458     	}
459     #else
460     	memcpy_fromio(buf, docptr + DoC_Mil_CDSN_IO, len - 1);
461     #endif
462     	buf[len - 1] = ReadDOC(docptr, LastDataRead);
463     
464     	/* Let the caller know we completed it */
465     	*retlen = len;
466             ret = 0;
467     
468     	if (eccbuf) {
469     		/* Read the ECC data from Spare Data Area,
470     		   see Reed-Solomon EDC/ECC 11.1 */
471     		dummy = ReadDOC(docptr, ReadPipeInit);
472     #ifndef USE_MEMCPY
473     		for (i = 0; i < 5; i++) {
474     			/* N.B. you have to increase the source address in this way or the
475     			   ECC logic will not work properly */
476     			eccbuf[i] = ReadDOC(docptr, Mil_CDSN_IO + i);
477     		}
478     #else
479     		memcpy_fromio(eccbuf, docptr + DoC_Mil_CDSN_IO, 5);
480     #endif
481     		eccbuf[5] = ReadDOC(docptr, LastDataRead);
482     
483     		/* Flush the pipeline */
484     		dummy = ReadDOC(docptr, ECCConf);
485     		dummy = ReadDOC(docptr, ECCConf);
486     
487     		/* Check the ECC Status */
488     		if (ReadDOC(docptr, ECCConf) & 0x80) {
489                             int nb_errors;
490     			/* There was an ECC error */
491     #ifdef ECC_DEBUG
492     			printk("DiskOnChip ECC Error: Read at %lx\n", (long)from);
493     #endif
494     			/* Read the ECC syndrom through the DiskOnChip ECC logic.
495     			   These syndrome will be all ZERO when there is no error */
496     			for (i = 0; i < 6; i++) {
497     				syndrome[i] = ReadDOC(docptr, ECCSyndrome0 + i);
498     			}
499                             nb_errors = doc_decode_ecc(buf, syndrome);
500     #ifdef ECC_DEBUG
501     			printk("ECC Errors corrected: %x\n", nb_errors);
502     #endif
503                             if (nb_errors < 0) {
504     				/* We return error, but have actually done the read. Not that
505     				   this can be told to user-space, via sys_read(), but at least
506     				   MTD-aware stuff can know about it by checking *retlen */
507     				ret = -EIO;
508                             }
509     		}
510     
511     #ifdef PSYCHO_DEBUG
512     		printk("ECC DATA at %lx: %2.2X %2.2X %2.2X %2.2X %2.2X %2.2X\n",
513     		       (long)from, eccbuf[0], eccbuf[1], eccbuf[2], eccbuf[3],
514     		       eccbuf[4], eccbuf[5]);
515     #endif
516     
517     		/* disable the ECC engine */
518     		WriteDOC(DOC_ECC_DIS, docptr , ECCConf);
519     	}
520     
521     	return ret;
522     }
523     
524     static int doc_write (struct mtd_info *mtd, loff_t to, size_t len,
525     		      size_t *retlen, const u_char *buf)
526     {
527     	char eccbuf[6];
528     	return doc_write_ecc(mtd, to, len, retlen, buf, eccbuf);
529     }
530     
531     static int doc_write_ecc (struct mtd_info *mtd, loff_t to, size_t len,
532     			  size_t *retlen, const u_char *buf, u_char *eccbuf)
533     {
534     	int i,ret = 0;
535     	volatile char dummy;
536     	struct DiskOnChip *this = (struct DiskOnChip *)mtd->priv;
537     	unsigned long docptr = this->virtadr;
538     	struct Nand *mychip = &this->chips[to >> (this->chipshift)];
539     
540     	/* Don't allow write past end of device */
541     	if (to >= this->totlen)
542     		return -EINVAL;
543     
544     #if 0
545     	/* Don't allow a single write to cross a 512-byte block boundary */
546     	if (to + len > ( (to | 0x1ff) + 1)) 
547     		len = ((to | 0x1ff) + 1) - to;
548     #else
549     	/* Don't allow writes which aren't exactly one block */
550     	if (to & 0x1ff || len != 0x200)
551     		return -EINVAL;
552     #endif
553     
554     	/* Find the chip which is to be used and select it */
555     	if (this->curfloor != mychip->floor) {
556     		DoC_SelectFloor(docptr, mychip->floor);
557     		DoC_SelectChip(docptr, mychip->chip);
558     	} else if (this->curchip != mychip->chip) {
559     		DoC_SelectChip(docptr, mychip->chip);
560     	}
561     	this->curfloor = mychip->floor;
562     	this->curchip = mychip->chip;
563     
564     	/* Reset the chip, see Software Requirement 11.4 item 1. */
565     	DoC_Command(docptr, NAND_CMD_RESET, 0x00);
566     	DoC_WaitReady(docptr);
567     	/* Set device to main plane of flash */
568     	DoC_Command(docptr, NAND_CMD_READ0, 0x00);
569     
570     	/* issue the Serial Data In command to initial the Page Program process */
571     	DoC_Command(docptr, NAND_CMD_SEQIN, 0x00);
572     	DoC_Address(docptr, 3, to, 0x00, 0x00);
573     	DoC_WaitReady(docptr);
574     
575     	if (eccbuf) {
576     		/* init the ECC engine, see Reed-Solomon EDC/ECC 11.1 .*/
577     		WriteDOC (DOC_ECC_RESET, docptr, ECCConf);
578     		WriteDOC (DOC_ECC_EN | DOC_ECC_RW, docptr, ECCConf);
579     	} else {
580     		/* disable the ECC engine */
581     		WriteDOC (DOC_ECC_RESET, docptr, ECCConf);
582     		WriteDOC (DOC_ECC_DIS, docptr, ECCConf);
583     	}
584     
585     	/* Write the data via the internal pipeline through CDSN IO register,
586     	   see Pipelined Write Operations 11.2 */
587     #ifndef USE_MEMCPY
588     	for (i = 0; i < len; i++) {
589     		/* N.B. you have to increase the source address in this way or the
590     		   ECC logic will not work properly */
591     		WriteDOC(buf[i], docptr, Mil_CDSN_IO + i);
592     	}
593     #else
594     	memcpy_toio(docptr + DoC_Mil_CDSN_IO, buf, len);
595     #endif
596     	WriteDOC(0x00, docptr, WritePipeTerm);
597     
598     	if (eccbuf) {
599     		/* Write ECC data to flash, the ECC info is generated by the DiskOnChip ECC logic
600     		   see Reed-Solomon EDC/ECC 11.1 */
601     		WriteDOC(0, docptr, NOP);
602     		WriteDOC(0, docptr, NOP);
603     		WriteDOC(0, docptr, NOP);
604     
605     		/* Read the ECC data through the DiskOnChip ECC logic */
606     		for (i = 0; i < 6; i++) {
607     			eccbuf[i] = ReadDOC(docptr, ECCSyndrome0 + i);
608     		}
609     
610     		/* ignore the ECC engine */
611     		WriteDOC(DOC_ECC_DIS, docptr , ECCConf);
612     
613     #ifndef USE_MEMCPY
614     		/* Write the ECC data to flash */
615     		for (i = 0; i < 6; i++) {
616     			/* N.B. you have to increase the source address in this way or the
617     			   ECC logic will not work properly */
618     			WriteDOC(eccbuf[i], docptr, Mil_CDSN_IO + i);
619     		}
620     #else
621     		memcpy_toio(docptr + DoC_Mil_CDSN_IO, eccbuf, 6);
622     #endif
623     
624     		/* write the block status BLOCK_USED (0x5555) at the end of ECC data
625     		   FIXME: this is only a hack for programming the IPL area for LinuxBIOS
626     		   and should be replace with proper codes in user space utilities */ 
627     		WriteDOC(0x55, docptr, Mil_CDSN_IO);
628     		WriteDOC(0x55, docptr, Mil_CDSN_IO + 1);
629     
630     		WriteDOC(0x00, docptr, WritePipeTerm);
631     
632     #ifdef PSYCHO_DEBUG
633     		printk("OOB data at %lx is %2.2X %2.2X %2.2X %2.2X %2.2X %2.2X\n",
634     		       (long) to, eccbuf[0], eccbuf[1], eccbuf[2], eccbuf[3],
635     		       eccbuf[4], eccbuf[5]);
636     #endif
637     	}
638     
639     	/* Commit the Page Program command and wait for ready
640     	   see Software Requirement 11.4 item 1.*/
641     	DoC_Command(docptr, NAND_CMD_PAGEPROG, 0x00);
642     	DoC_WaitReady(docptr);
643     
644     	/* Read the status of the flash device through CDSN IO register
645     	   see Software Requirement 11.4 item 5.*/
646     	DoC_Command(docptr, NAND_CMD_STATUS, CDSN_CTRL_WP);
647     	dummy = ReadDOC(docptr, ReadPipeInit);
648     	DoC_Delay(docptr, 2);
649     	if (ReadDOC(docptr, Mil_CDSN_IO) & 1) {
650     		printk("Error programming flash\n");
651     		/* Error in programming
652     		   FIXME: implement Bad Block Replacement (in nftl.c ??) */
653     		*retlen = 0;
654     		ret = -EIO;
655     	}
656     	dummy = ReadDOC(docptr, LastDataRead);
657     
658     	/* Let the caller know we completed it */
659     	*retlen = len;
660     
661     	return ret;
662     }
663     
664     static int doc_read_oob(struct mtd_info *mtd, loff_t ofs, size_t len,
665     			size_t *retlen, u_char *buf)
666     {
667     #ifndef USE_MEMCPY
668     	int i;
669     #endif
670     	volatile char dummy;
671     	struct DiskOnChip *this = (struct DiskOnChip *)mtd->priv;
672     	unsigned long docptr = this->virtadr;
673     	struct Nand *mychip = &this->chips[ofs >> this->chipshift];
674     
675     	/* Find the chip which is to be used and select it */
676     	if (this->curfloor != mychip->floor) {
677     		DoC_SelectFloor(docptr, mychip->floor);
678     		DoC_SelectChip(docptr, mychip->chip);
679     	} else if (this->curchip != mychip->chip) {
680     		DoC_SelectChip(docptr, mychip->chip);
681     	}
682     	this->curfloor = mychip->floor;
683     	this->curchip = mychip->chip;
684     
685     	/* disable the ECC engine */
686     	WriteDOC (DOC_ECC_RESET, docptr, ECCConf);
687     	WriteDOC (DOC_ECC_DIS, docptr, ECCConf);
688     
689     	/* issue the Read2 command to set the pointer to the Spare Data Area.
690     	   Polling the Flash Ready bit after issue 3 bytes address in
691     	   Sequence Read Mode, see Software Requirement 11.4 item 1.*/
692     	DoC_Command(docptr, NAND_CMD_READOOB, CDSN_CTRL_WP);
693     	DoC_Address(docptr, 3, ofs, CDSN_CTRL_WP, 0x00);
694     	DoC_WaitReady(docptr);
695     
696     	/* Read the data out via the internal pipeline through CDSN IO register,
697     	   see Pipelined Read Operations 11.3 */
698     	dummy = ReadDOC(docptr, ReadPipeInit);
699     #ifndef USE_MEMCPY
700     	for (i = 0; i < len-1; i++) {
701     		/* N.B. you have to increase the source address in this way or the
702     		   ECC logic will not work properly */
703     		buf[i] = ReadDOC(docptr, Mil_CDSN_IO + i);
704     	}
705     #else
706     	memcpy_fromio(buf, docptr + DoC_Mil_CDSN_IO, len - 1);
707     #endif
708     	buf[len - 1] = ReadDOC(docptr, LastDataRead);
709     
710     	*retlen = len;
711     
712     	return 0;
713     }
714     
715     static int doc_write_oob(struct mtd_info *mtd, loff_t ofs, size_t len,
716     			 size_t *retlen, const u_char *buf)
717     {
718     #ifndef USE_MEMCPY
719     	int i;
720     #endif
721     	volatile char dummy;
722     	int ret = 0;
723     	struct DiskOnChip *this = (struct DiskOnChip *)mtd->priv;
724     	unsigned long docptr = this->virtadr;
725     	struct Nand *mychip = &this->chips[ofs >> this->chipshift];
726     
727     	/* Find the chip which is to be used and select it */
728     	if (this->curfloor != mychip->floor) {
729     		DoC_SelectFloor(docptr, mychip->floor);
730     		DoC_SelectChip(docptr, mychip->chip);
731     	} else if (this->curchip != mychip->chip) {
732     		DoC_SelectChip(docptr, mychip->chip);
733     	}
734     	this->curfloor = mychip->floor;
735     	this->curchip = mychip->chip;
736     
737     	/* disable the ECC engine */
738     	WriteDOC (DOC_ECC_RESET, docptr, ECCConf);
739     	WriteDOC (DOC_ECC_DIS, docptr, ECCConf);
740     
741     	/* Reset the chip, see Software Requirement 11.4 item 1. */
742     	DoC_Command(docptr, NAND_CMD_RESET, CDSN_CTRL_WP);
743     	DoC_WaitReady(docptr);
744     	/* issue the Read2 command to set the pointer to the Spare Data Area. */
745     	DoC_Command(docptr, NAND_CMD_READOOB, CDSN_CTRL_WP);
746     
747     	/* issue the Serial Data In command to initial the Page Program process */
748     	DoC_Command(docptr, NAND_CMD_SEQIN, 0x00);
749     	DoC_Address(docptr, 3, ofs, 0x00, 0x00);
750     
751     	/* Write the data via the internal pipeline through CDSN IO register,
752     	   see Pipelined Write Operations 11.2 */
753     #ifndef USE_MEMCPY
754     	for (i = 0; i < len; i++) {
755     		/* N.B. you have to increase the source address in this way or the
756     		   ECC logic will not work properly */
757     		WriteDOC(buf[i], docptr, Mil_CDSN_IO + i);
758     	}
759     #else
760     	memcpy_toio(docptr + DoC_Mil_CDSN_IO, buf, len);
761     #endif
762     	WriteDOC(0x00, docptr, WritePipeTerm);
763     
764     	/* Commit the Page Program command and wait for ready
765     	   see Software Requirement 11.4 item 1.*/
766     	DoC_Command(docptr, NAND_CMD_PAGEPROG, 0x00);
767     	DoC_WaitReady(docptr);
768     
769     	/* Read the status of the flash device through CDSN IO register
770     	   see Software Requirement 11.4 item 5.*/
771     	DoC_Command(docptr, NAND_CMD_STATUS, 0x00);
772     	dummy = ReadDOC(docptr, ReadPipeInit);
773     	DoC_Delay(docptr, 2);
774     	if (ReadDOC(docptr, Mil_CDSN_IO) & 1) {
775     		printk("Error programming oob data\n");
776     		/* FIXME: implement Bad Block Replacement (in nftl.c ??) */
777     		*retlen = 0;
778     		ret = -EIO;
779     	}
780     	dummy = ReadDOC(docptr, LastDataRead);
781     
782     	*retlen = len;
783     
784     	return ret;
785     }
786     
787     int doc_erase (struct mtd_info *mtd, struct erase_info *instr)
788     {
789     	volatile char dummy;
790     	struct DiskOnChip *this = (struct DiskOnChip *)mtd->priv;
791     	__u32 ofs = instr->addr;
792     	__u32 len = instr->len;
793     	unsigned long docptr = this->virtadr;
794     	struct Nand *mychip = &this->chips[ofs >> this->chipshift];
795     
796     	if (len != mtd->erasesize) 
797     		printk(KERN_WARNING "Erase not right size (%x != %x)n",
798     		       len, mtd->erasesize);
799     
800     	/* Find the chip which is to be used and select it */
801     	if (this->curfloor != mychip->floor) {
802     		DoC_SelectFloor(docptr, mychip->floor);
803     		DoC_SelectChip(docptr, mychip->chip);
804     	} else if (this->curchip != mychip->chip) {
805     		DoC_SelectChip(docptr, mychip->chip);
806     	}
807     	this->curfloor = mychip->floor;
808     	this->curchip = mychip->chip;
809     
810     	instr->state = MTD_ERASE_PENDING;
811     
812     	/* issue the Erase Setup command */
813     	DoC_Command(docptr, NAND_CMD_ERASE1, 0x00);
814     	DoC_Address(docptr, 2, ofs, 0x00, 0x00);
815     
816     	/* Commit the Erase Start command and wait for ready
817     	   see Software Requirement 11.4 item 1.*/
818     	DoC_Command(docptr, NAND_CMD_ERASE2, 0x00);
819     	DoC_WaitReady(docptr);
820     
821     	instr->state = MTD_ERASING;
822     
823     	/* Read the status of the flash device through CDSN IO register
824     	   see Software Requirement 11.4 item 5.
825     	   FIXME: it seems that we are not wait long enough, some blocks are not
826     	   erased fully */
827     	DoC_Command(docptr, NAND_CMD_STATUS, CDSN_CTRL_WP);
828     	dummy = ReadDOC(docptr, ReadPipeInit);
829     	DoC_Delay(docptr, 2);
830     	if (ReadDOC(docptr, Mil_CDSN_IO) & 1) {
831     		printk("Error Erasing at 0x%x\n", ofs);
832     		/* There was an error
833     		   FIXME: implement Bad Block Replacement (in nftl.c ??) */
834     		instr->state = MTD_ERASE_FAILED;
835     	} else
836     		instr->state = MTD_ERASE_DONE;
837     	dummy = ReadDOC(docptr, LastDataRead);
838     
839     	if (instr->callback) 
840     		instr->callback(instr);
841     
842     	return 0;
843     }
844     
845     /****************************************************************************
846      *
847      * Module stuff
848      *
849      ****************************************************************************/
850     
851     #if LINUX_VERSION_CODE < 0x20212 && defined(MODULE)
852     #define cleanup_doc2001 cleanup_module
853     #define init_doc2001 init_module
854     #endif
855     
856     int __init init_doc2001(void)
857     {
858     	inter_module_register(im_name, THIS_MODULE, &DoCMil_init);
859     	return 0;
860     }
861     
862     static void __exit cleanup_doc2001(void)
863     {
864     	struct mtd_info *mtd;
865     	struct DiskOnChip *this;
866     
867     	while ((mtd=docmillist)) {
868     		this = (struct DiskOnChip *)mtd->priv;
869     		docmillist = this->nextdoc;
870     			
871     		del_mtd_device(mtd);
872     			
873     		iounmap((void *)this->virtadr);
874     		kfree(this->chips);
875     		kfree(mtd);
876     	}
877     	inter_module_unregister(im_name);
878     }
879     
880     module_exit(cleanup_doc2001);
881     module_init(init_doc2001);
882     
883     
884