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