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