1 /*
2  * Linux driver for Disk-On-Chip Millennium Plus
3  *
4  * (c) 2002-2003 Greg Ungerer <gerg@snapgear.com>
5  * (c) 2002-2003 SnapGear Inc
6  * (c) 1999 Machine Vision Holdings, Inc.
7  * (c) 1999, 2000 David Woodhouse <dwmw2@infradead.org>
8  *
9  * Released under GPL
10  */
11 
12 #include <linux/kernel.h>
13 #include <linux/module.h>
14 #include <asm/errno.h>
15 #include <asm/io.h>
16 #include <asm/uaccess.h>
17 #include <linux/delay.h>
18 #include <linux/slab.h>
19 #include <linux/init.h>
20 #include <linux/types.h>
21 #include <linux/bitops.h>
22 
23 #include <linux/mtd/mtd.h>
24 #include <linux/mtd/nand.h>
25 #include <linux/mtd/doc2000.h>
26 
27 /* #define ECC_DEBUG */
28 
29 /* I have no idea why some DoC chips can not use memcop_form|to_io().
30  * This may be due to the different revisions of the ASIC controller built-in or
31  * simplily a QA/Bug issue. Who knows ?? If you have trouble, please uncomment
32  * this:*/
33 #undef USE_MEMCPY
34 
35 static int doc_read(struct mtd_info *mtd, loff_t from, size_t len,
36 		size_t *retlen, u_char *buf);
37 static int doc_write(struct mtd_info *mtd, loff_t to, size_t len,
38 		size_t *retlen, const u_char *buf);
39 static int doc_read_oob(struct mtd_info *mtd, loff_t ofs,
40 			struct mtd_oob_ops *ops);
41 static int doc_write_oob(struct mtd_info *mtd, loff_t ofs,
42 			 struct mtd_oob_ops *ops);
43 static int doc_erase (struct mtd_info *mtd, struct erase_info *instr);
44 
45 static struct mtd_info *docmilpluslist = NULL;
46 
47 
48 /* Perform the required delay cycles by writing to the NOP register */
DoC_Delay(void __iomem * docptr,int cycles)49 static void DoC_Delay(void __iomem * docptr, int cycles)
50 {
51 	int i;
52 
53 	for (i = 0; (i < cycles); i++)
54 		WriteDOC(0, docptr, Mplus_NOP);
55 }
56 
57 #define	CDSN_CTRL_FR_B_MASK	(CDSN_CTRL_FR_B0 | CDSN_CTRL_FR_B1)
58 
59 /* DOC_WaitReady: Wait for RDY line to be asserted by the flash chip */
_DoC_WaitReady(void __iomem * docptr)60 static int _DoC_WaitReady(void __iomem * docptr)
61 {
62 	unsigned int c = 0xffff;
63 
64 	pr_debug("_DoC_WaitReady called for out-of-line wait\n");
65 
66 	/* Out-of-line routine to wait for chip response */
67 	while (((ReadDOC(docptr, Mplus_FlashControl) & CDSN_CTRL_FR_B_MASK) != CDSN_CTRL_FR_B_MASK) && --c)
68 		;
69 
70 	if (c == 0)
71 		pr_debug("_DoC_WaitReady timed out.\n");
72 
73 	return (c == 0);
74 }
75 
DoC_WaitReady(void __iomem * docptr)76 static inline int DoC_WaitReady(void __iomem * docptr)
77 {
78 	/* This is inline, to optimise the common case, where it's ready instantly */
79 	int ret = 0;
80 
81 	/* read form NOP register should be issued prior to the read from CDSNControl
82 	   see Software Requirement 11.4 item 2. */
83 	DoC_Delay(docptr, 4);
84 
85 	if ((ReadDOC(docptr, Mplus_FlashControl) & CDSN_CTRL_FR_B_MASK) != CDSN_CTRL_FR_B_MASK)
86 		/* Call the out-of-line routine to wait */
87 		ret = _DoC_WaitReady(docptr);
88 
89 	return ret;
90 }
91 
92 /* For some reason the Millennium Plus seems to occasionally put itself
93  * into reset mode. For me this happens randomly, with no pattern that I
94  * can detect. M-systems suggest always check this on any block level
95  * operation and setting to normal mode if in reset mode.
96  */
DoC_CheckASIC(void __iomem * docptr)97 static inline void DoC_CheckASIC(void __iomem * docptr)
98 {
99 	/* Make sure the DoC is in normal mode */
100 	if ((ReadDOC(docptr, Mplus_DOCControl) & DOC_MODE_NORMAL) == 0) {
101 		WriteDOC((DOC_MODE_NORMAL | DOC_MODE_MDWREN), docptr, Mplus_DOCControl);
102 		WriteDOC(~(DOC_MODE_NORMAL | DOC_MODE_MDWREN), docptr, Mplus_CtrlConfirm);
103 	}
104 }
105 
106 /* DoC_Command: Send a flash command to the flash chip through the Flash
107  * command register. Need 2 Write Pipeline Terminates to complete send.
108  */
DoC_Command(void __iomem * docptr,unsigned char command,unsigned char xtraflags)109 static void DoC_Command(void __iomem * docptr, unsigned char command,
110 			       unsigned char xtraflags)
111 {
112 	WriteDOC(command, docptr, Mplus_FlashCmd);
113 	WriteDOC(command, docptr, Mplus_WritePipeTerm);
114 	WriteDOC(command, docptr, Mplus_WritePipeTerm);
115 }
116 
117 /* DoC_Address: Set the current address for the flash chip through the Flash
118  * Address register. Need 2 Write Pipeline Terminates to complete send.
119  */
DoC_Address(struct DiskOnChip * doc,int numbytes,unsigned long ofs,unsigned char xtraflags1,unsigned char xtraflags2)120 static inline void DoC_Address(struct DiskOnChip *doc, int numbytes,
121 			       unsigned long ofs, unsigned char xtraflags1,
122 			       unsigned char xtraflags2)
123 {
124 	void __iomem * docptr = doc->virtadr;
125 
126 	/* Allow for possible Mill Plus internal flash interleaving */
127 	ofs >>= doc->interleave;
128 
129 	switch (numbytes) {
130 	case 1:
131 		/* Send single byte, bits 0-7. */
132 		WriteDOC(ofs & 0xff, docptr, Mplus_FlashAddress);
133 		break;
134 	case 2:
135 		/* Send bits 9-16 followed by 17-23 */
136 		WriteDOC((ofs >> 9)  & 0xff, docptr, Mplus_FlashAddress);
137 		WriteDOC((ofs >> 17) & 0xff, docptr, Mplus_FlashAddress);
138 		break;
139 	case 3:
140 		/* Send 0-7, 9-16, then 17-23 */
141 		WriteDOC(ofs & 0xff, docptr, Mplus_FlashAddress);
142 		WriteDOC((ofs >> 9)  & 0xff, docptr, Mplus_FlashAddress);
143 		WriteDOC((ofs >> 17) & 0xff, docptr, Mplus_FlashAddress);
144 		break;
145 	default:
146 		return;
147 	}
148 
149 	WriteDOC(0x00, docptr, Mplus_WritePipeTerm);
150 	WriteDOC(0x00, docptr, Mplus_WritePipeTerm);
151 }
152 
153 /* DoC_SelectChip: Select a given flash chip within the current floor */
DoC_SelectChip(void __iomem * docptr,int chip)154 static int DoC_SelectChip(void __iomem * docptr, int chip)
155 {
156 	/* No choice for flash chip on Millennium Plus */
157 	return 0;
158 }
159 
160 /* DoC_SelectFloor: Select a given floor (bank of flash chips) */
DoC_SelectFloor(void __iomem * docptr,int floor)161 static int DoC_SelectFloor(void __iomem * docptr, int floor)
162 {
163 	WriteDOC((floor & 0x3), docptr, Mplus_DeviceSelect);
164 	return 0;
165 }
166 
167 /*
168  * Translate the given offset into the appropriate command and offset.
169  * This does the mapping using the 16bit interleave layout defined by
170  * M-Systems, and looks like this for a sector pair:
171  *  +-----------+-------+-------+-------+--------------+---------+-----------+
172  *  | 0 --- 511 |512-517|518-519|520-521| 522 --- 1033 |1034-1039|1040 - 1055|
173  *  +-----------+-------+-------+-------+--------------+---------+-----------+
174  *  | Data 0    | ECC 0 |Flags0 |Flags1 | Data 1       |ECC 1    | OOB 1 + 2 |
175  *  +-----------+-------+-------+-------+--------------+---------+-----------+
176  */
177 /* FIXME: This lives in INFTL not here. Other users of flash devices
178    may not want it */
DoC_GetDataOffset(struct mtd_info * mtd,loff_t * from)179 static unsigned int DoC_GetDataOffset(struct mtd_info *mtd, loff_t *from)
180 {
181 	struct DiskOnChip *this = mtd->priv;
182 
183 	if (this->interleave) {
184 		unsigned int ofs = *from & 0x3ff;
185 		unsigned int cmd;
186 
187 		if (ofs < 512) {
188 			cmd = NAND_CMD_READ0;
189 			ofs &= 0x1ff;
190 		} else if (ofs < 1014) {
191 			cmd = NAND_CMD_READ1;
192 			ofs = (ofs & 0x1ff) + 10;
193 		} else {
194 			cmd = NAND_CMD_READOOB;
195 			ofs = ofs - 1014;
196 		}
197 
198 		*from = (*from & ~0x3ff) | ofs;
199 		return cmd;
200 	} else {
201 		/* No interleave */
202 		if ((*from) & 0x100)
203 			return NAND_CMD_READ1;
204 		return NAND_CMD_READ0;
205 	}
206 }
207 
DoC_GetECCOffset(struct mtd_info * mtd,loff_t * from)208 static unsigned int DoC_GetECCOffset(struct mtd_info *mtd, loff_t *from)
209 {
210 	unsigned int ofs, cmd;
211 
212 	if (*from & 0x200) {
213 		cmd = NAND_CMD_READOOB;
214 		ofs = 10 + (*from & 0xf);
215 	} else {
216 		cmd = NAND_CMD_READ1;
217 		ofs = (*from & 0xf);
218 	}
219 
220 	*from = (*from & ~0x3ff) | ofs;
221 	return cmd;
222 }
223 
DoC_GetFlagsOffset(struct mtd_info * mtd,loff_t * from)224 static unsigned int DoC_GetFlagsOffset(struct mtd_info *mtd, loff_t *from)
225 {
226 	unsigned int ofs, cmd;
227 
228 	cmd = NAND_CMD_READ1;
229 	ofs = (*from & 0x200) ? 8 : 6;
230 	*from = (*from & ~0x3ff) | ofs;
231 	return cmd;
232 }
233 
DoC_GetHdrOffset(struct mtd_info * mtd,loff_t * from)234 static unsigned int DoC_GetHdrOffset(struct mtd_info *mtd, loff_t *from)
235 {
236 	unsigned int ofs, cmd;
237 
238 	cmd = NAND_CMD_READOOB;
239 	ofs = (*from & 0x200) ? 24 : 16;
240 	*from = (*from & ~0x3ff) | ofs;
241 	return cmd;
242 }
243 
MemReadDOC(void __iomem * docptr,unsigned char * buf,int len)244 static inline void MemReadDOC(void __iomem * docptr, unsigned char *buf, int len)
245 {
246 #ifndef USE_MEMCPY
247 	int i;
248 	for (i = 0; i < len; i++)
249 		buf[i] = ReadDOC(docptr, Mil_CDSN_IO + i);
250 #else
251 	memcpy_fromio(buf, docptr + DoC_Mil_CDSN_IO, len);
252 #endif
253 }
254 
MemWriteDOC(void __iomem * docptr,unsigned char * buf,int len)255 static inline void MemWriteDOC(void __iomem * docptr, unsigned char *buf, int len)
256 {
257 #ifndef USE_MEMCPY
258 	int i;
259 	for (i = 0; i < len; i++)
260 		WriteDOC(buf[i], docptr, Mil_CDSN_IO + i);
261 #else
262 	memcpy_toio(docptr + DoC_Mil_CDSN_IO, buf, len);
263 #endif
264 }
265 
266 /* DoC_IdentChip: Identify a given NAND chip given {floor,chip} */
DoC_IdentChip(struct DiskOnChip * doc,int floor,int chip)267 static int DoC_IdentChip(struct DiskOnChip *doc, int floor, int chip)
268 {
269 	int mfr, id, i, j;
270 	volatile char dummy;
271 	void __iomem * docptr = doc->virtadr;
272 
273 	/* Page in the required floor/chip */
274 	DoC_SelectFloor(docptr, floor);
275 	DoC_SelectChip(docptr, chip);
276 
277 	/* Millennium Plus bus cycle sequence as per figure 2, section 2.4 */
278 	WriteDOC((DOC_FLASH_CE | DOC_FLASH_WP), docptr, Mplus_FlashSelect);
279 
280 	/* Reset the chip, see Software Requirement 11.4 item 1. */
281 	DoC_Command(docptr, NAND_CMD_RESET, 0);
282 	DoC_WaitReady(docptr);
283 
284 	/* Read the NAND chip ID: 1. Send ReadID command */
285 	DoC_Command(docptr, NAND_CMD_READID, 0);
286 
287 	/* Read the NAND chip ID: 2. Send address byte zero */
288 	DoC_Address(doc, 1, 0x00, 0, 0x00);
289 
290 	WriteDOC(0, docptr, Mplus_FlashControl);
291 	DoC_WaitReady(docptr);
292 
293 	/* Read the manufacturer and device id codes of the flash device through
294 	   CDSN IO register see Software Requirement 11.4 item 5.*/
295 	dummy = ReadDOC(docptr, Mplus_ReadPipeInit);
296 	dummy = ReadDOC(docptr, Mplus_ReadPipeInit);
297 
298 	mfr = ReadDOC(docptr, Mil_CDSN_IO);
299 	if (doc->interleave)
300 		dummy = ReadDOC(docptr, Mil_CDSN_IO); /* 2 way interleave */
301 
302 	id  = ReadDOC(docptr, Mil_CDSN_IO);
303 	if (doc->interleave)
304 		dummy = ReadDOC(docptr, Mil_CDSN_IO); /* 2 way interleave */
305 
306 	dummy = ReadDOC(docptr, Mplus_LastDataRead);
307 	dummy = ReadDOC(docptr, Mplus_LastDataRead);
308 
309 	/* Disable flash internally */
310 	WriteDOC(0, docptr, Mplus_FlashSelect);
311 
312 	/* No response - return failure */
313 	if (mfr == 0xff || mfr == 0)
314 		return 0;
315 
316 	for (i = 0; nand_flash_ids[i].name != NULL; i++) {
317 		if (id == nand_flash_ids[i].id) {
318 			/* Try to identify manufacturer */
319 			for (j = 0; nand_manuf_ids[j].id != 0x0; j++) {
320 				if (nand_manuf_ids[j].id == mfr)
321 					break;
322 			}
323 			printk(KERN_INFO "Flash chip found: Manufacturer ID: %2.2X, "
324 			       "Chip ID: %2.2X (%s:%s)\n", mfr, id,
325 			       nand_manuf_ids[j].name, nand_flash_ids[i].name);
326 			doc->mfr = mfr;
327 			doc->id = id;
328 			doc->chipshift = ffs((nand_flash_ids[i].chipsize << 20)) - 1;
329 			doc->erasesize = nand_flash_ids[i].erasesize << doc->interleave;
330 			break;
331 		}
332 	}
333 
334 	if (nand_flash_ids[i].name == NULL)
335 		return 0;
336 	return 1;
337 }
338 
339 /* DoC_ScanChips: Find all NAND chips present in a DiskOnChip, and identify them */
DoC_ScanChips(struct DiskOnChip * this)340 static void DoC_ScanChips(struct DiskOnChip *this)
341 {
342 	int floor, chip;
343 	int numchips[MAX_FLOORS_MPLUS];
344 	int ret;
345 
346 	this->numchips = 0;
347 	this->mfr = 0;
348 	this->id = 0;
349 
350 	/* Work out the intended interleave setting */
351 	this->interleave = 0;
352 	if (this->ChipID == DOC_ChipID_DocMilPlus32)
353 		this->interleave = 1;
354 
355 	/* Check the ASIC agrees */
356 	if ( (this->interleave << 2) !=
357 	     (ReadDOC(this->virtadr, Mplus_Configuration) & 4)) {
358 		u_char conf = ReadDOC(this->virtadr, Mplus_Configuration);
359 		printk(KERN_NOTICE "Setting DiskOnChip Millennium Plus interleave to %s\n",
360 		       this->interleave?"on (16-bit)":"off (8-bit)");
361 		conf ^= 4;
362 		WriteDOC(conf, this->virtadr, Mplus_Configuration);
363 	}
364 
365 	/* For each floor, find the number of valid chips it contains */
366 	for (floor = 0,ret = 1; floor < MAX_FLOORS_MPLUS; floor++) {
367 		numchips[floor] = 0;
368 		for (chip = 0; chip < MAX_CHIPS_MPLUS && ret != 0; chip++) {
369 			ret = DoC_IdentChip(this, floor, chip);
370 			if (ret) {
371 				numchips[floor]++;
372 				this->numchips++;
373 			}
374 		}
375 	}
376 	/* If there are none at all that we recognise, bail */
377 	if (!this->numchips) {
378 		printk("No flash chips recognised.\n");
379 		return;
380 	}
381 
382 	/* Allocate an array to hold the information for each chip */
383 	this->chips = kmalloc(sizeof(struct Nand) * this->numchips, GFP_KERNEL);
384 	if (!this->chips){
385 		printk("MTD: No memory for allocating chip info structures\n");
386 		return;
387 	}
388 
389 	/* Fill out the chip array with {floor, chipno} for each
390 	 * detected chip in the device. */
391 	for (floor = 0, ret = 0; floor < MAX_FLOORS_MPLUS; floor++) {
392 		for (chip = 0 ; chip < numchips[floor] ; chip++) {
393 			this->chips[ret].floor = floor;
394 			this->chips[ret].chip = chip;
395 			this->chips[ret].curadr = 0;
396 			this->chips[ret].curmode = 0x50;
397 			ret++;
398 		}
399 	}
400 
401 	/* Calculate and print the total size of the device */
402 	this->totlen = this->numchips * (1 << this->chipshift);
403 	printk(KERN_INFO "%d flash chips found. Total DiskOnChip size: %ld MiB\n",
404 	       this->numchips ,this->totlen >> 20);
405 }
406 
DoCMilPlus_is_alias(struct DiskOnChip * doc1,struct DiskOnChip * doc2)407 static int DoCMilPlus_is_alias(struct DiskOnChip *doc1, struct DiskOnChip *doc2)
408 {
409 	int tmp1, tmp2, retval;
410 
411 	if (doc1->physadr == doc2->physadr)
412 		return 1;
413 
414 	/* Use the alias resolution register which was set aside for this
415 	 * purpose. If it's value is the same on both chips, they might
416 	 * be the same chip, and we write to one and check for a change in
417 	 * the other. It's unclear if this register is usuable in the
418 	 * DoC 2000 (it's in the Millennium docs), but it seems to work. */
419 	tmp1 = ReadDOC(doc1->virtadr, Mplus_AliasResolution);
420 	tmp2 = ReadDOC(doc2->virtadr, Mplus_AliasResolution);
421 	if (tmp1 != tmp2)
422 		return 0;
423 
424 	WriteDOC((tmp1+1) % 0xff, doc1->virtadr, Mplus_AliasResolution);
425 	tmp2 = ReadDOC(doc2->virtadr, Mplus_AliasResolution);
426 	if (tmp2 == (tmp1+1) % 0xff)
427 		retval = 1;
428 	else
429 		retval = 0;
430 
431 	/* Restore register contents.  May not be necessary, but do it just to
432 	 * be safe. */
433 	WriteDOC(tmp1, doc1->virtadr, Mplus_AliasResolution);
434 
435 	return retval;
436 }
437 
438 /* This routine is found from the docprobe code by symbol_get(),
439  * which will bump the use count of this module. */
DoCMilPlus_init(struct mtd_info * mtd)440 void DoCMilPlus_init(struct mtd_info *mtd)
441 {
442 	struct DiskOnChip *this = mtd->priv;
443 	struct DiskOnChip *old = NULL;
444 
445 	/* We must avoid being called twice for the same device. */
446 	if (docmilpluslist)
447 		old = docmilpluslist->priv;
448 
449 	while (old) {
450 		if (DoCMilPlus_is_alias(this, old)) {
451 			printk(KERN_NOTICE "Ignoring DiskOnChip Millennium "
452 				"Plus at 0x%lX - already configured\n",
453 				this->physadr);
454 			iounmap(this->virtadr);
455 			kfree(mtd);
456 			return;
457 		}
458 		if (old->nextdoc)
459 			old = old->nextdoc->priv;
460 		else
461 			old = NULL;
462 	}
463 
464 	mtd->name = "DiskOnChip Millennium Plus";
465 	printk(KERN_NOTICE "DiskOnChip Millennium Plus found at "
466 		"address 0x%lX\n", this->physadr);
467 
468 	mtd->type = MTD_NANDFLASH;
469 	mtd->flags = MTD_CAP_NANDFLASH;
470 	mtd->writesize = 512;
471 	mtd->oobsize = 16;
472 	mtd->owner = THIS_MODULE;
473 	mtd->erase = doc_erase;
474 	mtd->read = doc_read;
475 	mtd->write = doc_write;
476 	mtd->read_oob = doc_read_oob;
477 	mtd->write_oob = doc_write_oob;
478 	this->curfloor = -1;
479 	this->curchip = -1;
480 
481 	/* Ident all the chips present. */
482 	DoC_ScanChips(this);
483 
484 	if (!this->totlen) {
485 		kfree(mtd);
486 		iounmap(this->virtadr);
487 	} else {
488 		this->nextdoc = docmilpluslist;
489 		docmilpluslist = mtd;
490 		mtd->size  = this->totlen;
491 		mtd->erasesize = this->erasesize;
492 		mtd_device_register(mtd, NULL, 0);
493 		return;
494 	}
495 }
496 EXPORT_SYMBOL_GPL(DoCMilPlus_init);
497 
498 #if 0
499 static int doc_dumpblk(struct mtd_info *mtd, loff_t from)
500 {
501 	int i;
502 	loff_t fofs;
503 	struct DiskOnChip *this = mtd->priv;
504 	void __iomem * docptr = this->virtadr;
505 	struct Nand *mychip = &this->chips[from >> (this->chipshift)];
506 	unsigned char *bp, buf[1056];
507 	char c[32];
508 
509 	from &= ~0x3ff;
510 
511 	/* Don't allow read past end of device */
512 	if (from >= this->totlen)
513 		return -EINVAL;
514 
515 	DoC_CheckASIC(docptr);
516 
517 	/* Find the chip which is to be used and select it */
518 	if (this->curfloor != mychip->floor) {
519 		DoC_SelectFloor(docptr, mychip->floor);
520 		DoC_SelectChip(docptr, mychip->chip);
521 	} else if (this->curchip != mychip->chip) {
522 		DoC_SelectChip(docptr, mychip->chip);
523 	}
524 	this->curfloor = mychip->floor;
525 	this->curchip = mychip->chip;
526 
527 	/* Millennium Plus bus cycle sequence as per figure 2, section 2.4 */
528 	WriteDOC((DOC_FLASH_CE | DOC_FLASH_WP), docptr, Mplus_FlashSelect);
529 
530 	/* Reset the chip, see Software Requirement 11.4 item 1. */
531 	DoC_Command(docptr, NAND_CMD_RESET, 0);
532 	DoC_WaitReady(docptr);
533 
534 	fofs = from;
535 	DoC_Command(docptr, DoC_GetDataOffset(mtd, &fofs), 0);
536 	DoC_Address(this, 3, fofs, 0, 0x00);
537 	WriteDOC(0, docptr, Mplus_FlashControl);
538 	DoC_WaitReady(docptr);
539 
540 	/* disable the ECC engine */
541 	WriteDOC(DOC_ECC_RESET, docptr, Mplus_ECCConf);
542 
543 	ReadDOC(docptr, Mplus_ReadPipeInit);
544 	ReadDOC(docptr, Mplus_ReadPipeInit);
545 
546 	/* Read the data via the internal pipeline through CDSN IO
547 	   register, see Pipelined Read Operations 11.3 */
548 	MemReadDOC(docptr, buf, 1054);
549 	buf[1054] = ReadDOC(docptr, Mplus_LastDataRead);
550 	buf[1055] = ReadDOC(docptr, Mplus_LastDataRead);
551 
552 	memset(&c[0], 0, sizeof(c));
553 	printk("DUMP OFFSET=%x:\n", (int)from);
554 
555         for (i = 0, bp = &buf[0]; (i < 1056); i++) {
556                 if ((i % 16) == 0)
557                         printk("%08x: ", i);
558                 printk(" %02x", *bp);
559                 c[(i & 0xf)] = ((*bp >= 0x20) && (*bp <= 0x7f)) ? *bp : '.';
560                 bp++;
561                 if (((i + 1) % 16) == 0)
562                         printk("    %s\n", c);
563         }
564 	printk("\n");
565 
566 	/* Disable flash internally */
567 	WriteDOC(0, docptr, Mplus_FlashSelect);
568 
569 	return 0;
570 }
571 #endif
572 
doc_read(struct mtd_info * mtd,loff_t from,size_t len,size_t * retlen,u_char * buf)573 static int doc_read(struct mtd_info *mtd, loff_t from, size_t len,
574 		    size_t *retlen, u_char *buf)
575 {
576 	int ret, i;
577 	volatile char dummy;
578 	loff_t fofs;
579 	unsigned char syndrome[6], eccbuf[6];
580 	struct DiskOnChip *this = mtd->priv;
581 	void __iomem * docptr = this->virtadr;
582 	struct Nand *mychip = &this->chips[from >> (this->chipshift)];
583 
584 	/* Don't allow read past end of device */
585 	if (from >= this->totlen)
586 		return -EINVAL;
587 
588 	/* Don't allow a single read to cross a 512-byte block boundary */
589 	if (from + len > ((from | 0x1ff) + 1))
590 		len = ((from | 0x1ff) + 1) - from;
591 
592 	DoC_CheckASIC(docptr);
593 
594 	/* Find the chip which is to be used and select it */
595 	if (this->curfloor != mychip->floor) {
596 		DoC_SelectFloor(docptr, mychip->floor);
597 		DoC_SelectChip(docptr, mychip->chip);
598 	} else if (this->curchip != mychip->chip) {
599 		DoC_SelectChip(docptr, mychip->chip);
600 	}
601 	this->curfloor = mychip->floor;
602 	this->curchip = mychip->chip;
603 
604 	/* Millennium Plus bus cycle sequence as per figure 2, section 2.4 */
605 	WriteDOC((DOC_FLASH_CE | DOC_FLASH_WP), docptr, Mplus_FlashSelect);
606 
607 	/* Reset the chip, see Software Requirement 11.4 item 1. */
608 	DoC_Command(docptr, NAND_CMD_RESET, 0);
609 	DoC_WaitReady(docptr);
610 
611 	fofs = from;
612 	DoC_Command(docptr, DoC_GetDataOffset(mtd, &fofs), 0);
613 	DoC_Address(this, 3, fofs, 0, 0x00);
614 	WriteDOC(0, docptr, Mplus_FlashControl);
615 	DoC_WaitReady(docptr);
616 
617 	/* init the ECC engine, see Reed-Solomon EDC/ECC 11.1 .*/
618 	WriteDOC(DOC_ECC_RESET, docptr, Mplus_ECCConf);
619 	WriteDOC(DOC_ECC_EN, docptr, Mplus_ECCConf);
620 
621 	/* Let the caller know we completed it */
622 	*retlen = len;
623 	ret = 0;
624 
625 	ReadDOC(docptr, Mplus_ReadPipeInit);
626 	ReadDOC(docptr, Mplus_ReadPipeInit);
627 
628 	/* Read the data via the internal pipeline through CDSN IO
629 	   register, see Pipelined Read Operations 11.3 */
630 	MemReadDOC(docptr, buf, len);
631 
632 	/* Read the ECC data following raw data */
633 	MemReadDOC(docptr, eccbuf, 4);
634 	eccbuf[4] = ReadDOC(docptr, Mplus_LastDataRead);
635 	eccbuf[5] = ReadDOC(docptr, Mplus_LastDataRead);
636 
637 	/* Flush the pipeline */
638 	dummy = ReadDOC(docptr, Mplus_ECCConf);
639 	dummy = ReadDOC(docptr, Mplus_ECCConf);
640 
641 	/* Check the ECC Status */
642 	if (ReadDOC(docptr, Mplus_ECCConf) & 0x80) {
643 		int nb_errors;
644 		/* There was an ECC error */
645 #ifdef ECC_DEBUG
646 		printk("DiskOnChip ECC Error: Read at %lx\n", (long)from);
647 #endif
648 		/* Read the ECC syndrome through the DiskOnChip ECC logic.
649 		   These syndrome will be all ZERO when there is no error */
650 		for (i = 0; i < 6; i++)
651 			syndrome[i] = ReadDOC(docptr, Mplus_ECCSyndrome0 + i);
652 
653 		nb_errors = doc_decode_ecc(buf, syndrome);
654 #ifdef ECC_DEBUG
655 		printk("ECC Errors corrected: %x\n", nb_errors);
656 #endif
657 		if (nb_errors < 0) {
658 			/* We return error, but have actually done the
659 			   read. Not that this can be told to user-space, via
660 			   sys_read(), but at least MTD-aware stuff can know
661 			   about it by checking *retlen */
662 #ifdef ECC_DEBUG
663 			printk("%s(%d): Millennium Plus ECC error (from=0x%x:\n",
664 				__FILE__, __LINE__, (int)from);
665 			printk("        syndrome= %02x:%02x:%02x:%02x:%02x:"
666 				"%02x\n",
667 				syndrome[0], syndrome[1], syndrome[2],
668 				syndrome[3], syndrome[4], syndrome[5]);
669 			printk("          eccbuf= %02x:%02x:%02x:%02x:%02x:"
670 				"%02x\n",
671 				eccbuf[0], eccbuf[1], eccbuf[2],
672 				eccbuf[3], eccbuf[4], eccbuf[5]);
673 #endif
674 				ret = -EIO;
675 		}
676 	}
677 
678 #ifdef PSYCHO_DEBUG
679 	printk("ECC DATA at %lx: %2.2X %2.2X %2.2X %2.2X %2.2X %2.2X\n",
680 	       (long)from, eccbuf[0], eccbuf[1], eccbuf[2], eccbuf[3],
681 	       eccbuf[4], eccbuf[5]);
682 #endif
683 	/* disable the ECC engine */
684 	WriteDOC(DOC_ECC_DIS, docptr , Mplus_ECCConf);
685 
686 	/* Disable flash internally */
687 	WriteDOC(0, docptr, Mplus_FlashSelect);
688 
689 	return ret;
690 }
691 
doc_write(struct mtd_info * mtd,loff_t to,size_t len,size_t * retlen,const u_char * buf)692 static int doc_write(struct mtd_info *mtd, loff_t to, size_t len,
693 		     size_t *retlen, const u_char *buf)
694 {
695 	int i, before, ret = 0;
696 	loff_t fto;
697 	volatile char dummy;
698 	char eccbuf[6];
699 	struct DiskOnChip *this = mtd->priv;
700 	void __iomem * docptr = this->virtadr;
701 	struct Nand *mychip = &this->chips[to >> (this->chipshift)];
702 
703 	/* Don't allow write past end of device */
704 	if (to >= this->totlen)
705 		return -EINVAL;
706 
707 	/* Don't allow writes which aren't exactly one block (512 bytes) */
708 	if ((to & 0x1ff) || (len != 0x200))
709 		return -EINVAL;
710 
711 	/* Determine position of OOB flags, before or after data */
712 	before = (this->interleave && (to & 0x200));
713 
714 	DoC_CheckASIC(docptr);
715 
716 	/* Find the chip which is to be used and select it */
717 	if (this->curfloor != mychip->floor) {
718 		DoC_SelectFloor(docptr, mychip->floor);
719 		DoC_SelectChip(docptr, mychip->chip);
720 	} else if (this->curchip != mychip->chip) {
721 		DoC_SelectChip(docptr, mychip->chip);
722 	}
723 	this->curfloor = mychip->floor;
724 	this->curchip = mychip->chip;
725 
726 	/* Millennium Plus bus cycle sequence as per figure 2, section 2.4 */
727 	WriteDOC(DOC_FLASH_CE, docptr, Mplus_FlashSelect);
728 
729 	/* Reset the chip, see Software Requirement 11.4 item 1. */
730 	DoC_Command(docptr, NAND_CMD_RESET, 0);
731 	DoC_WaitReady(docptr);
732 
733 	/* Set device to appropriate plane of flash */
734 	fto = to;
735 	WriteDOC(DoC_GetDataOffset(mtd, &fto), docptr, Mplus_FlashCmd);
736 
737 	/* On interleaved devices the flags for 2nd half 512 are before data */
738 	if (before)
739 		fto -= 2;
740 
741 	/* issue the Serial Data In command to initial the Page Program process */
742 	DoC_Command(docptr, NAND_CMD_SEQIN, 0x00);
743 	DoC_Address(this, 3, fto, 0x00, 0x00);
744 
745 	/* Disable the ECC engine */
746 	WriteDOC(DOC_ECC_RESET, docptr, Mplus_ECCConf);
747 
748 	if (before) {
749 		/* Write the block status BLOCK_USED (0x5555) */
750 		WriteDOC(0x55, docptr, Mil_CDSN_IO);
751 		WriteDOC(0x55, docptr, Mil_CDSN_IO);
752 	}
753 
754 	/* init the ECC engine, see Reed-Solomon EDC/ECC 11.1 .*/
755 	WriteDOC(DOC_ECC_EN | DOC_ECC_RW, docptr, Mplus_ECCConf);
756 
757 	MemWriteDOC(docptr, (unsigned char *) buf, len);
758 
759 	/* Write ECC data to flash, the ECC info is generated by
760 	   the DiskOnChip ECC logic see Reed-Solomon EDC/ECC 11.1 */
761 	DoC_Delay(docptr, 3);
762 
763 	/* Read the ECC data through the DiskOnChip ECC logic */
764 	for (i = 0; i < 6; i++)
765 		eccbuf[i] = ReadDOC(docptr, Mplus_ECCSyndrome0 + i);
766 
767 	/* disable the ECC engine */
768 	WriteDOC(DOC_ECC_DIS, docptr, Mplus_ECCConf);
769 
770 	/* Write the ECC data to flash */
771 	MemWriteDOC(docptr, eccbuf, 6);
772 
773 	if (!before) {
774 		/* Write the block status BLOCK_USED (0x5555) */
775 		WriteDOC(0x55, docptr, Mil_CDSN_IO+6);
776 		WriteDOC(0x55, docptr, Mil_CDSN_IO+7);
777 	}
778 
779 #ifdef PSYCHO_DEBUG
780 	printk("OOB data at %lx is %2.2X %2.2X %2.2X %2.2X %2.2X %2.2X\n",
781 	       (long) to, eccbuf[0], eccbuf[1], eccbuf[2], eccbuf[3],
782 	       eccbuf[4], eccbuf[5]);
783 #endif
784 
785 	WriteDOC(0x00, docptr, Mplus_WritePipeTerm);
786 	WriteDOC(0x00, docptr, Mplus_WritePipeTerm);
787 
788 	/* Commit the Page Program command and wait for ready
789 	   see Software Requirement 11.4 item 1.*/
790 	DoC_Command(docptr, NAND_CMD_PAGEPROG, 0x00);
791 	DoC_WaitReady(docptr);
792 
793 	/* Read the status of the flash device through CDSN IO register
794 	   see Software Requirement 11.4 item 5.*/
795 	DoC_Command(docptr, NAND_CMD_STATUS, 0);
796 	dummy = ReadDOC(docptr, Mplus_ReadPipeInit);
797 	dummy = ReadDOC(docptr, Mplus_ReadPipeInit);
798 	DoC_Delay(docptr, 2);
799 	if ((dummy = ReadDOC(docptr, Mplus_LastDataRead)) & 1) {
800 		printk("MTD: Error 0x%x programming at 0x%x\n", dummy, (int)to);
801 		/* Error in programming
802 		   FIXME: implement Bad Block Replacement (in nftl.c ??) */
803 		*retlen = 0;
804 		ret = -EIO;
805 	}
806 	dummy = ReadDOC(docptr, Mplus_LastDataRead);
807 
808 	/* Disable flash internally */
809 	WriteDOC(0, docptr, Mplus_FlashSelect);
810 
811 	/* Let the caller know we completed it */
812 	*retlen = len;
813 
814 	return ret;
815 }
816 
doc_read_oob(struct mtd_info * mtd,loff_t ofs,struct mtd_oob_ops * ops)817 static int doc_read_oob(struct mtd_info *mtd, loff_t ofs,
818 			struct mtd_oob_ops *ops)
819 {
820 	loff_t fofs, base;
821 	struct DiskOnChip *this = mtd->priv;
822 	void __iomem * docptr = this->virtadr;
823 	struct Nand *mychip = &this->chips[ofs >> this->chipshift];
824 	size_t i, size, got, want;
825 	uint8_t *buf = ops->oobbuf;
826 	size_t len = ops->len;
827 
828 	BUG_ON(ops->mode != MTD_OPS_PLACE_OOB);
829 
830 	ofs += ops->ooboffs;
831 
832 	DoC_CheckASIC(docptr);
833 
834 	/* Find the chip which is to be used and select it */
835 	if (this->curfloor != mychip->floor) {
836 		DoC_SelectFloor(docptr, mychip->floor);
837 		DoC_SelectChip(docptr, mychip->chip);
838 	} else if (this->curchip != mychip->chip) {
839 		DoC_SelectChip(docptr, mychip->chip);
840 	}
841 	this->curfloor = mychip->floor;
842 	this->curchip = mychip->chip;
843 
844 	/* Millennium Plus bus cycle sequence as per figure 2, section 2.4 */
845 	WriteDOC((DOC_FLASH_CE | DOC_FLASH_WP), docptr, Mplus_FlashSelect);
846 
847 	/* disable the ECC engine */
848 	WriteDOC(DOC_ECC_RESET, docptr, Mplus_ECCConf);
849 	DoC_WaitReady(docptr);
850 
851 	/* Maximum of 16 bytes in the OOB region, so limit read to that */
852 	if (len > 16)
853 		len = 16;
854 	got = 0;
855 	want = len;
856 
857 	for (i = 0; ((i < 3) && (want > 0)); i++) {
858 		/* Figure out which region we are accessing... */
859 		fofs = ofs;
860 		base = ofs & 0xf;
861 		if (!this->interleave) {
862 			DoC_Command(docptr, NAND_CMD_READOOB, 0);
863 			size = 16 - base;
864 		} else if (base < 6) {
865 			DoC_Command(docptr, DoC_GetECCOffset(mtd, &fofs), 0);
866 			size = 6 - base;
867 		} else if (base < 8) {
868 			DoC_Command(docptr, DoC_GetFlagsOffset(mtd, &fofs), 0);
869 			size = 8 - base;
870 		} else {
871 			DoC_Command(docptr, DoC_GetHdrOffset(mtd, &fofs), 0);
872 			size = 16 - base;
873 		}
874 		if (size > want)
875 			size = want;
876 
877 		/* Issue read command */
878 		DoC_Address(this, 3, fofs, 0, 0x00);
879 		WriteDOC(0, docptr, Mplus_FlashControl);
880 		DoC_WaitReady(docptr);
881 
882 		ReadDOC(docptr, Mplus_ReadPipeInit);
883 		ReadDOC(docptr, Mplus_ReadPipeInit);
884 		MemReadDOC(docptr, &buf[got], size - 2);
885 		buf[got + size - 2] = ReadDOC(docptr, Mplus_LastDataRead);
886 		buf[got + size - 1] = ReadDOC(docptr, Mplus_LastDataRead);
887 
888 		ofs += size;
889 		got += size;
890 		want -= size;
891 	}
892 
893 	/* Disable flash internally */
894 	WriteDOC(0, docptr, Mplus_FlashSelect);
895 
896 	ops->retlen = len;
897 	return 0;
898 }
899 
doc_write_oob(struct mtd_info * mtd,loff_t ofs,struct mtd_oob_ops * ops)900 static int doc_write_oob(struct mtd_info *mtd, loff_t ofs,
901 			 struct mtd_oob_ops *ops)
902 {
903 	volatile char dummy;
904 	loff_t fofs, base;
905 	struct DiskOnChip *this = mtd->priv;
906 	void __iomem * docptr = this->virtadr;
907 	struct Nand *mychip = &this->chips[ofs >> this->chipshift];
908 	size_t i, size, got, want;
909 	int ret = 0;
910 	uint8_t *buf = ops->oobbuf;
911 	size_t len = ops->len;
912 
913 	BUG_ON(ops->mode != MTD_OPS_PLACE_OOB);
914 
915 	ofs += ops->ooboffs;
916 
917 	DoC_CheckASIC(docptr);
918 
919 	/* Find the chip which is to be used and select it */
920 	if (this->curfloor != mychip->floor) {
921 		DoC_SelectFloor(docptr, mychip->floor);
922 		DoC_SelectChip(docptr, mychip->chip);
923 	} else if (this->curchip != mychip->chip) {
924 		DoC_SelectChip(docptr, mychip->chip);
925 	}
926 	this->curfloor = mychip->floor;
927 	this->curchip = mychip->chip;
928 
929 	/* Millennium Plus bus cycle sequence as per figure 2, section 2.4 */
930 	WriteDOC(DOC_FLASH_CE, docptr, Mplus_FlashSelect);
931 
932 
933 	/* Maximum of 16 bytes in the OOB region, so limit write to that */
934 	if (len > 16)
935 		len = 16;
936 	got = 0;
937 	want = len;
938 
939 	for (i = 0; ((i < 3) && (want > 0)); i++) {
940 		/* Reset the chip, see Software Requirement 11.4 item 1. */
941 		DoC_Command(docptr, NAND_CMD_RESET, 0);
942 		DoC_WaitReady(docptr);
943 
944 		/* Figure out which region we are accessing... */
945 		fofs = ofs;
946 		base = ofs & 0x0f;
947 		if (!this->interleave) {
948 			WriteDOC(NAND_CMD_READOOB, docptr, Mplus_FlashCmd);
949 			size = 16 - base;
950 		} else if (base < 6) {
951 			WriteDOC(DoC_GetECCOffset(mtd, &fofs), docptr, Mplus_FlashCmd);
952 			size = 6 - base;
953 		} else if (base < 8) {
954 			WriteDOC(DoC_GetFlagsOffset(mtd, &fofs), docptr, Mplus_FlashCmd);
955 			size = 8 - base;
956 		} else {
957 			WriteDOC(DoC_GetHdrOffset(mtd, &fofs), docptr, Mplus_FlashCmd);
958 			size = 16 - base;
959 		}
960 		if (size > want)
961 			size = want;
962 
963 		/* Issue the Serial Data In command to initial the Page Program process */
964 		DoC_Command(docptr, NAND_CMD_SEQIN, 0x00);
965 		DoC_Address(this, 3, fofs, 0, 0x00);
966 
967 		/* Disable the ECC engine */
968 		WriteDOC(DOC_ECC_RESET, docptr, Mplus_ECCConf);
969 
970 		/* Write the data via the internal pipeline through CDSN IO
971 		   register, see Pipelined Write Operations 11.2 */
972 		MemWriteDOC(docptr, (unsigned char *) &buf[got], size);
973 		WriteDOC(0x00, docptr, Mplus_WritePipeTerm);
974 		WriteDOC(0x00, docptr, Mplus_WritePipeTerm);
975 
976 		/* Commit the Page Program command and wait for ready
977 	 	   see Software Requirement 11.4 item 1.*/
978 		DoC_Command(docptr, NAND_CMD_PAGEPROG, 0x00);
979 		DoC_WaitReady(docptr);
980 
981 		/* Read the status of the flash device through CDSN IO register
982 		   see Software Requirement 11.4 item 5.*/
983 		DoC_Command(docptr, NAND_CMD_STATUS, 0x00);
984 		dummy = ReadDOC(docptr, Mplus_ReadPipeInit);
985 		dummy = ReadDOC(docptr, Mplus_ReadPipeInit);
986 		DoC_Delay(docptr, 2);
987 		if ((dummy = ReadDOC(docptr, Mplus_LastDataRead)) & 1) {
988 			printk("MTD: Error 0x%x programming oob at 0x%x\n",
989 				dummy, (int)ofs);
990 			/* FIXME: implement Bad Block Replacement */
991 			ops->retlen = 0;
992 			ret = -EIO;
993 		}
994 		dummy = ReadDOC(docptr, Mplus_LastDataRead);
995 
996 		ofs += size;
997 		got += size;
998 		want -= size;
999 	}
1000 
1001 	/* Disable flash internally */
1002 	WriteDOC(0, docptr, Mplus_FlashSelect);
1003 
1004 	ops->retlen = len;
1005 	return ret;
1006 }
1007 
doc_erase(struct mtd_info * mtd,struct erase_info * instr)1008 int doc_erase(struct mtd_info *mtd, struct erase_info *instr)
1009 {
1010 	volatile char dummy;
1011 	struct DiskOnChip *this = mtd->priv;
1012 	__u32 ofs = instr->addr;
1013 	__u32 len = instr->len;
1014 	void __iomem * docptr = this->virtadr;
1015 	struct Nand *mychip = &this->chips[ofs >> this->chipshift];
1016 
1017 	DoC_CheckASIC(docptr);
1018 
1019 	if (len != mtd->erasesize)
1020 		printk(KERN_WARNING "MTD: Erase not right size (%x != %x)n",
1021 		       len, mtd->erasesize);
1022 
1023 	/* Find the chip which is to be used and select it */
1024 	if (this->curfloor != mychip->floor) {
1025 		DoC_SelectFloor(docptr, mychip->floor);
1026 		DoC_SelectChip(docptr, mychip->chip);
1027 	} else if (this->curchip != mychip->chip) {
1028 		DoC_SelectChip(docptr, mychip->chip);
1029 	}
1030 	this->curfloor = mychip->floor;
1031 	this->curchip = mychip->chip;
1032 
1033 	instr->state = MTD_ERASE_PENDING;
1034 
1035 	/* Millennium Plus bus cycle sequence as per figure 2, section 2.4 */
1036 	WriteDOC(DOC_FLASH_CE, docptr, Mplus_FlashSelect);
1037 
1038 	DoC_Command(docptr, NAND_CMD_RESET, 0x00);
1039 	DoC_WaitReady(docptr);
1040 
1041 	DoC_Command(docptr, NAND_CMD_ERASE1, 0);
1042 	DoC_Address(this, 2, ofs, 0, 0x00);
1043 	DoC_Command(docptr, NAND_CMD_ERASE2, 0);
1044 	DoC_WaitReady(docptr);
1045 	instr->state = MTD_ERASING;
1046 
1047 	/* Read the status of the flash device through CDSN IO register
1048 	   see Software Requirement 11.4 item 5. */
1049 	DoC_Command(docptr, NAND_CMD_STATUS, 0);
1050 	dummy = ReadDOC(docptr, Mplus_ReadPipeInit);
1051 	dummy = ReadDOC(docptr, Mplus_ReadPipeInit);
1052 	if ((dummy = ReadDOC(docptr, Mplus_LastDataRead)) & 1) {
1053 		printk("MTD: Error 0x%x erasing at 0x%x\n", dummy, ofs);
1054 		/* FIXME: implement Bad Block Replacement (in nftl.c ??) */
1055 		instr->state = MTD_ERASE_FAILED;
1056 	} else {
1057 		instr->state = MTD_ERASE_DONE;
1058 	}
1059 	dummy = ReadDOC(docptr, Mplus_LastDataRead);
1060 
1061 	/* Disable flash internally */
1062 	WriteDOC(0, docptr, Mplus_FlashSelect);
1063 
1064 	mtd_erase_callback(instr);
1065 
1066 	return 0;
1067 }
1068 
1069 /****************************************************************************
1070  *
1071  * Module stuff
1072  *
1073  ****************************************************************************/
1074 
cleanup_doc2001plus(void)1075 static void __exit cleanup_doc2001plus(void)
1076 {
1077 	struct mtd_info *mtd;
1078 	struct DiskOnChip *this;
1079 
1080 	while ((mtd=docmilpluslist)) {
1081 		this = mtd->priv;
1082 		docmilpluslist = this->nextdoc;
1083 
1084 		mtd_device_unregister(mtd);
1085 
1086 		iounmap(this->virtadr);
1087 		kfree(this->chips);
1088 		kfree(mtd);
1089 	}
1090 }
1091 
1092 module_exit(cleanup_doc2001plus);
1093 
1094 MODULE_LICENSE("GPL");
1095 MODULE_AUTHOR("Greg Ungerer <gerg@snapgear.com> et al.");
1096 MODULE_DESCRIPTION("Driver for DiskOnChip Millennium Plus");
1097