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