Linux-2.6.12-rc2
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / mtd / devices / doc2001plus.c
blobed47bafb2ce2f9380d4e2bf9f516022e0735936d
1 /*
2 * Linux driver for Disk-On-Chip Millennium Plus
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>
9 * $Id: doc2001plus.c,v 1.13 2005/01/05 18:05:12 dwmw2 Exp $
11 * Released under GPL
14 #include <linux/kernel.h>
15 #include <linux/module.h>
16 #include <asm/errno.h>
17 #include <asm/io.h>
18 #include <asm/uaccess.h>
19 #include <linux/miscdevice.h>
20 #include <linux/pci.h>
21 #include <linux/delay.h>
22 #include <linux/slab.h>
23 #include <linux/sched.h>
24 #include <linux/init.h>
25 #include <linux/types.h>
26 #include <linux/bitops.h>
28 #include <linux/mtd/mtd.h>
29 #include <linux/mtd/nand.h>
30 #include <linux/mtd/doc2000.h>
32 /* #define ECC_DEBUG */
34 /* I have no idea why some DoC chips can not use memcop_form|to_io().
35 * This may be due to the different revisions of the ASIC controller built-in or
36 * simplily a QA/Bug issue. Who knows ?? If you have trouble, please uncomment
37 * this:*/
38 #undef USE_MEMCPY
40 static int doc_read(struct mtd_info *mtd, loff_t from, size_t len,
41 size_t *retlen, u_char *buf);
42 static int doc_write(struct mtd_info *mtd, loff_t to, size_t len,
43 size_t *retlen, const u_char *buf);
44 static int doc_read_ecc(struct mtd_info *mtd, loff_t from, size_t len,
45 size_t *retlen, u_char *buf, u_char *eccbuf,
46 struct nand_oobinfo *oobsel);
47 static int doc_write_ecc(struct mtd_info *mtd, loff_t to, size_t len,
48 size_t *retlen, const u_char *buf, u_char *eccbuf,
49 struct nand_oobinfo *oobsel);
50 static int doc_read_oob(struct mtd_info *mtd, loff_t ofs, size_t len,
51 size_t *retlen, u_char *buf);
52 static int doc_write_oob(struct mtd_info *mtd, loff_t ofs, size_t len,
53 size_t *retlen, const u_char *buf);
54 static int doc_erase (struct mtd_info *mtd, struct erase_info *instr);
56 static struct mtd_info *docmilpluslist = NULL;
59 /* Perform the required delay cycles by writing to the NOP register */
60 static void DoC_Delay(void __iomem * docptr, int cycles)
62 int i;
64 for (i = 0; (i < cycles); i++)
65 WriteDOC(0, docptr, Mplus_NOP);
68 #define CDSN_CTRL_FR_B_MASK (CDSN_CTRL_FR_B0 | CDSN_CTRL_FR_B1)
70 /* DOC_WaitReady: Wait for RDY line to be asserted by the flash chip */
71 static int _DoC_WaitReady(void __iomem * docptr)
73 unsigned int c = 0xffff;
75 DEBUG(MTD_DEBUG_LEVEL3,
76 "_DoC_WaitReady called for out-of-line wait\n");
78 /* Out-of-line routine to wait for chip response */
79 while (((ReadDOC(docptr, Mplus_FlashControl) & CDSN_CTRL_FR_B_MASK) != CDSN_CTRL_FR_B_MASK) && --c)
82 if (c == 0)
83 DEBUG(MTD_DEBUG_LEVEL2, "_DoC_WaitReady timed out.\n");
85 return (c == 0);
88 static inline int DoC_WaitReady(void __iomem * docptr)
90 /* This is inline, to optimise the common case, where it's ready instantly */
91 int ret = 0;
93 /* read form NOP register should be issued prior to the read from CDSNControl
94 see Software Requirement 11.4 item 2. */
95 DoC_Delay(docptr, 4);
97 if ((ReadDOC(docptr, Mplus_FlashControl) & CDSN_CTRL_FR_B_MASK) != CDSN_CTRL_FR_B_MASK)
98 /* Call the out-of-line routine to wait */
99 ret = _DoC_WaitReady(docptr);
101 return ret;
104 /* For some reason the Millennium Plus seems to occassionally put itself
105 * into reset mode. For me this happens randomly, with no pattern that I
106 * can detect. M-systems suggest always check this on any block level
107 * operation and setting to normal mode if in reset mode.
109 static inline void DoC_CheckASIC(void __iomem * docptr)
111 /* Make sure the DoC is in normal mode */
112 if ((ReadDOC(docptr, Mplus_DOCControl) & DOC_MODE_NORMAL) == 0) {
113 WriteDOC((DOC_MODE_NORMAL | DOC_MODE_MDWREN), docptr, Mplus_DOCControl);
114 WriteDOC(~(DOC_MODE_NORMAL | DOC_MODE_MDWREN), docptr, Mplus_CtrlConfirm);
118 /* DoC_Command: Send a flash command to the flash chip through the Flash
119 * command register. Need 2 Write Pipeline Terminates to complete send.
121 static inline void DoC_Command(void __iomem * docptr, unsigned char command,
122 unsigned char xtraflags)
124 WriteDOC(command, docptr, Mplus_FlashCmd);
125 WriteDOC(command, docptr, Mplus_WritePipeTerm);
126 WriteDOC(command, docptr, Mplus_WritePipeTerm);
129 /* DoC_Address: Set the current address for the flash chip through the Flash
130 * Address register. Need 2 Write Pipeline Terminates to complete send.
132 static inline void DoC_Address(struct DiskOnChip *doc, int numbytes,
133 unsigned long ofs, unsigned char xtraflags1,
134 unsigned char xtraflags2)
136 void __iomem * docptr = doc->virtadr;
138 /* Allow for possible Mill Plus internal flash interleaving */
139 ofs >>= doc->interleave;
141 switch (numbytes) {
142 case 1:
143 /* Send single byte, bits 0-7. */
144 WriteDOC(ofs & 0xff, docptr, Mplus_FlashAddress);
145 break;
146 case 2:
147 /* Send bits 9-16 followed by 17-23 */
148 WriteDOC((ofs >> 9) & 0xff, docptr, Mplus_FlashAddress);
149 WriteDOC((ofs >> 17) & 0xff, docptr, Mplus_FlashAddress);
150 break;
151 case 3:
152 /* Send 0-7, 9-16, then 17-23 */
153 WriteDOC(ofs & 0xff, docptr, Mplus_FlashAddress);
154 WriteDOC((ofs >> 9) & 0xff, docptr, Mplus_FlashAddress);
155 WriteDOC((ofs >> 17) & 0xff, docptr, Mplus_FlashAddress);
156 break;
157 default:
158 return;
161 WriteDOC(0x00, docptr, Mplus_WritePipeTerm);
162 WriteDOC(0x00, docptr, Mplus_WritePipeTerm);
165 /* DoC_SelectChip: Select a given flash chip within the current floor */
166 static int DoC_SelectChip(void __iomem * docptr, int chip)
168 /* No choice for flash chip on Millennium Plus */
169 return 0;
172 /* DoC_SelectFloor: Select a given floor (bank of flash chips) */
173 static int DoC_SelectFloor(void __iomem * docptr, int floor)
175 WriteDOC((floor & 0x3), docptr, Mplus_DeviceSelect);
176 return 0;
180 * Translate the given offset into the appropriate command and offset.
181 * This does the mapping using the 16bit interleave layout defined by
182 * M-Systems, and looks like this for a sector pair:
183 * +-----------+-------+-------+-------+--------------+---------+-----------+
184 * | 0 --- 511 |512-517|518-519|520-521| 522 --- 1033 |1034-1039|1040 - 1055|
185 * +-----------+-------+-------+-------+--------------+---------+-----------+
186 * | Data 0 | ECC 0 |Flags0 |Flags1 | Data 1 |ECC 1 | OOB 1 + 2 |
187 * +-----------+-------+-------+-------+--------------+---------+-----------+
189 /* FIXME: This lives in INFTL not here. Other users of flash devices
190 may not want it */
191 static unsigned int DoC_GetDataOffset(struct mtd_info *mtd, loff_t *from)
193 struct DiskOnChip *this = mtd->priv;
195 if (this->interleave) {
196 unsigned int ofs = *from & 0x3ff;
197 unsigned int cmd;
199 if (ofs < 512) {
200 cmd = NAND_CMD_READ0;
201 ofs &= 0x1ff;
202 } else if (ofs < 1014) {
203 cmd = NAND_CMD_READ1;
204 ofs = (ofs & 0x1ff) + 10;
205 } else {
206 cmd = NAND_CMD_READOOB;
207 ofs = ofs - 1014;
210 *from = (*from & ~0x3ff) | ofs;
211 return cmd;
212 } else {
213 /* No interleave */
214 if ((*from) & 0x100)
215 return NAND_CMD_READ1;
216 return NAND_CMD_READ0;
220 static unsigned int DoC_GetECCOffset(struct mtd_info *mtd, loff_t *from)
222 unsigned int ofs, cmd;
224 if (*from & 0x200) {
225 cmd = NAND_CMD_READOOB;
226 ofs = 10 + (*from & 0xf);
227 } else {
228 cmd = NAND_CMD_READ1;
229 ofs = (*from & 0xf);
232 *from = (*from & ~0x3ff) | ofs;
233 return cmd;
236 static unsigned int DoC_GetFlagsOffset(struct mtd_info *mtd, loff_t *from)
238 unsigned int ofs, cmd;
240 cmd = NAND_CMD_READ1;
241 ofs = (*from & 0x200) ? 8 : 6;
242 *from = (*from & ~0x3ff) | ofs;
243 return cmd;
246 static unsigned int DoC_GetHdrOffset(struct mtd_info *mtd, loff_t *from)
248 unsigned int ofs, cmd;
250 cmd = NAND_CMD_READOOB;
251 ofs = (*from & 0x200) ? 24 : 16;
252 *from = (*from & ~0x3ff) | ofs;
253 return cmd;
256 static inline void MemReadDOC(void __iomem * docptr, unsigned char *buf, int len)
258 #ifndef USE_MEMCPY
259 int i;
260 for (i = 0; i < len; i++)
261 buf[i] = ReadDOC(docptr, Mil_CDSN_IO + i);
262 #else
263 memcpy_fromio(buf, docptr + DoC_Mil_CDSN_IO, len);
264 #endif
267 static inline void MemWriteDOC(void __iomem * docptr, unsigned char *buf, int len)
269 #ifndef USE_MEMCPY
270 int i;
271 for (i = 0; i < len; i++)
272 WriteDOC(buf[i], docptr, Mil_CDSN_IO + i);
273 #else
274 memcpy_toio(docptr + DoC_Mil_CDSN_IO, buf, len);
275 #endif
278 /* DoC_IdentChip: Identify a given NAND chip given {floor,chip} */
279 static int DoC_IdentChip(struct DiskOnChip *doc, int floor, int chip)
281 int mfr, id, i, j;
282 volatile char dummy;
283 void __iomem * docptr = doc->virtadr;
285 /* Page in the required floor/chip */
286 DoC_SelectFloor(docptr, floor);
287 DoC_SelectChip(docptr, chip);
289 /* Millennium Plus bus cycle sequence as per figure 2, section 2.4 */
290 WriteDOC((DOC_FLASH_CE | DOC_FLASH_WP), docptr, Mplus_FlashSelect);
292 /* Reset the chip, see Software Requirement 11.4 item 1. */
293 DoC_Command(docptr, NAND_CMD_RESET, 0);
294 DoC_WaitReady(docptr);
296 /* Read the NAND chip ID: 1. Send ReadID command */
297 DoC_Command(docptr, NAND_CMD_READID, 0);
299 /* Read the NAND chip ID: 2. Send address byte zero */
300 DoC_Address(doc, 1, 0x00, 0, 0x00);
302 WriteDOC(0, docptr, Mplus_FlashControl);
303 DoC_WaitReady(docptr);
305 /* Read the manufacturer and device id codes of the flash device through
306 CDSN IO register see Software Requirement 11.4 item 5.*/
307 dummy = ReadDOC(docptr, Mplus_ReadPipeInit);
308 dummy = ReadDOC(docptr, Mplus_ReadPipeInit);
310 mfr = ReadDOC(docptr, Mil_CDSN_IO);
311 if (doc->interleave)
312 dummy = ReadDOC(docptr, Mil_CDSN_IO); /* 2 way interleave */
314 id = ReadDOC(docptr, Mil_CDSN_IO);
315 if (doc->interleave)
316 dummy = ReadDOC(docptr, Mil_CDSN_IO); /* 2 way interleave */
318 dummy = ReadDOC(docptr, Mplus_LastDataRead);
319 dummy = ReadDOC(docptr, Mplus_LastDataRead);
321 /* Disable flash internally */
322 WriteDOC(0, docptr, Mplus_FlashSelect);
324 /* No response - return failure */
325 if (mfr == 0xff || mfr == 0)
326 return 0;
328 for (i = 0; nand_flash_ids[i].name != NULL; i++) {
329 if (id == nand_flash_ids[i].id) {
330 /* Try to identify manufacturer */
331 for (j = 0; nand_manuf_ids[j].id != 0x0; j++) {
332 if (nand_manuf_ids[j].id == mfr)
333 break;
335 printk(KERN_INFO "Flash chip found: Manufacturer ID: %2.2X, "
336 "Chip ID: %2.2X (%s:%s)\n", mfr, id,
337 nand_manuf_ids[j].name, nand_flash_ids[i].name);
338 doc->mfr = mfr;
339 doc->id = id;
340 doc->chipshift = ffs((nand_flash_ids[i].chipsize << 20)) - 1;
341 doc->erasesize = nand_flash_ids[i].erasesize << doc->interleave;
342 break;
346 if (nand_flash_ids[i].name == NULL)
347 return 0;
348 return 1;
351 /* DoC_ScanChips: Find all NAND chips present in a DiskOnChip, and identify them */
352 static void DoC_ScanChips(struct DiskOnChip *this)
354 int floor, chip;
355 int numchips[MAX_FLOORS_MPLUS];
356 int ret;
358 this->numchips = 0;
359 this->mfr = 0;
360 this->id = 0;
362 /* Work out the intended interleave setting */
363 this->interleave = 0;
364 if (this->ChipID == DOC_ChipID_DocMilPlus32)
365 this->interleave = 1;
367 /* Check the ASIC agrees */
368 if ( (this->interleave << 2) !=
369 (ReadDOC(this->virtadr, Mplus_Configuration) & 4)) {
370 u_char conf = ReadDOC(this->virtadr, Mplus_Configuration);
371 printk(KERN_NOTICE "Setting DiskOnChip Millennium Plus interleave to %s\n",
372 this->interleave?"on (16-bit)":"off (8-bit)");
373 conf ^= 4;
374 WriteDOC(conf, this->virtadr, Mplus_Configuration);
377 /* For each floor, find the number of valid chips it contains */
378 for (floor = 0,ret = 1; floor < MAX_FLOORS_MPLUS; floor++) {
379 numchips[floor] = 0;
380 for (chip = 0; chip < MAX_CHIPS_MPLUS && ret != 0; chip++) {
381 ret = DoC_IdentChip(this, floor, chip);
382 if (ret) {
383 numchips[floor]++;
384 this->numchips++;
388 /* If there are none at all that we recognise, bail */
389 if (!this->numchips) {
390 printk("No flash chips recognised.\n");
391 return;
394 /* Allocate an array to hold the information for each chip */
395 this->chips = kmalloc(sizeof(struct Nand) * this->numchips, GFP_KERNEL);
396 if (!this->chips){
397 printk("MTD: No memory for allocating chip info structures\n");
398 return;
401 /* Fill out the chip array with {floor, chipno} for each
402 * detected chip in the device. */
403 for (floor = 0, ret = 0; floor < MAX_FLOORS_MPLUS; floor++) {
404 for (chip = 0 ; chip < numchips[floor] ; chip++) {
405 this->chips[ret].floor = floor;
406 this->chips[ret].chip = chip;
407 this->chips[ret].curadr = 0;
408 this->chips[ret].curmode = 0x50;
409 ret++;
413 /* Calculate and print the total size of the device */
414 this->totlen = this->numchips * (1 << this->chipshift);
415 printk(KERN_INFO "%d flash chips found. Total DiskOnChip size: %ld MiB\n",
416 this->numchips ,this->totlen >> 20);
419 static int DoCMilPlus_is_alias(struct DiskOnChip *doc1, struct DiskOnChip *doc2)
421 int tmp1, tmp2, retval;
423 if (doc1->physadr == doc2->physadr)
424 return 1;
426 /* Use the alias resolution register which was set aside for this
427 * purpose. If it's value is the same on both chips, they might
428 * be the same chip, and we write to one and check for a change in
429 * the other. It's unclear if this register is usuable in the
430 * DoC 2000 (it's in the Millennium docs), but it seems to work. */
431 tmp1 = ReadDOC(doc1->virtadr, Mplus_AliasResolution);
432 tmp2 = ReadDOC(doc2->virtadr, Mplus_AliasResolution);
433 if (tmp1 != tmp2)
434 return 0;
436 WriteDOC((tmp1+1) % 0xff, doc1->virtadr, Mplus_AliasResolution);
437 tmp2 = ReadDOC(doc2->virtadr, Mplus_AliasResolution);
438 if (tmp2 == (tmp1+1) % 0xff)
439 retval = 1;
440 else
441 retval = 0;
443 /* Restore register contents. May not be necessary, but do it just to
444 * be safe. */
445 WriteDOC(tmp1, doc1->virtadr, Mplus_AliasResolution);
447 return retval;
450 static const char im_name[] = "DoCMilPlus_init";
452 /* This routine is made available to other mtd code via
453 * inter_module_register. It must only be accessed through
454 * inter_module_get which will bump the use count of this module. The
455 * addresses passed back in mtd are valid as long as the use count of
456 * this module is non-zero, i.e. between inter_module_get and
457 * inter_module_put. Keith Owens <kaos@ocs.com.au> 29 Oct 2000.
459 static void DoCMilPlus_init(struct mtd_info *mtd)
461 struct DiskOnChip *this = mtd->priv;
462 struct DiskOnChip *old = NULL;
464 /* We must avoid being called twice for the same device. */
465 if (docmilpluslist)
466 old = docmilpluslist->priv;
468 while (old) {
469 if (DoCMilPlus_is_alias(this, old)) {
470 printk(KERN_NOTICE "Ignoring DiskOnChip Millennium "
471 "Plus at 0x%lX - already configured\n",
472 this->physadr);
473 iounmap(this->virtadr);
474 kfree(mtd);
475 return;
477 if (old->nextdoc)
478 old = old->nextdoc->priv;
479 else
480 old = NULL;
483 mtd->name = "DiskOnChip Millennium Plus";
484 printk(KERN_NOTICE "DiskOnChip Millennium Plus found at "
485 "address 0x%lX\n", this->physadr);
487 mtd->type = MTD_NANDFLASH;
488 mtd->flags = MTD_CAP_NANDFLASH;
489 mtd->ecctype = MTD_ECC_RS_DiskOnChip;
490 mtd->size = 0;
492 mtd->erasesize = 0;
493 mtd->oobblock = 512;
494 mtd->oobsize = 16;
495 mtd->owner = THIS_MODULE;
496 mtd->erase = doc_erase;
497 mtd->point = NULL;
498 mtd->unpoint = NULL;
499 mtd->read = doc_read;
500 mtd->write = doc_write;
501 mtd->read_ecc = doc_read_ecc;
502 mtd->write_ecc = doc_write_ecc;
503 mtd->read_oob = doc_read_oob;
504 mtd->write_oob = doc_write_oob;
505 mtd->sync = NULL;
507 this->totlen = 0;
508 this->numchips = 0;
509 this->curfloor = -1;
510 this->curchip = -1;
512 /* Ident all the chips present. */
513 DoC_ScanChips(this);
515 if (!this->totlen) {
516 kfree(mtd);
517 iounmap(this->virtadr);
518 } else {
519 this->nextdoc = docmilpluslist;
520 docmilpluslist = mtd;
521 mtd->size = this->totlen;
522 mtd->erasesize = this->erasesize;
523 add_mtd_device(mtd);
524 return;
528 #if 0
529 static int doc_dumpblk(struct mtd_info *mtd, loff_t from)
531 int i;
532 loff_t fofs;
533 struct DiskOnChip *this = mtd->priv;
534 void __iomem * docptr = this->virtadr;
535 struct Nand *mychip = &this->chips[from >> (this->chipshift)];
536 unsigned char *bp, buf[1056];
537 char c[32];
539 from &= ~0x3ff;
541 /* Don't allow read past end of device */
542 if (from >= this->totlen)
543 return -EINVAL;
545 DoC_CheckASIC(docptr);
547 /* Find the chip which is to be used and select it */
548 if (this->curfloor != mychip->floor) {
549 DoC_SelectFloor(docptr, mychip->floor);
550 DoC_SelectChip(docptr, mychip->chip);
551 } else if (this->curchip != mychip->chip) {
552 DoC_SelectChip(docptr, mychip->chip);
554 this->curfloor = mychip->floor;
555 this->curchip = mychip->chip;
557 /* Millennium Plus bus cycle sequence as per figure 2, section 2.4 */
558 WriteDOC((DOC_FLASH_CE | DOC_FLASH_WP), docptr, Mplus_FlashSelect);
560 /* Reset the chip, see Software Requirement 11.4 item 1. */
561 DoC_Command(docptr, NAND_CMD_RESET, 0);
562 DoC_WaitReady(docptr);
564 fofs = from;
565 DoC_Command(docptr, DoC_GetDataOffset(mtd, &fofs), 0);
566 DoC_Address(this, 3, fofs, 0, 0x00);
567 WriteDOC(0, docptr, Mplus_FlashControl);
568 DoC_WaitReady(docptr);
570 /* disable the ECC engine */
571 WriteDOC(DOC_ECC_RESET, docptr, Mplus_ECCConf);
573 ReadDOC(docptr, Mplus_ReadPipeInit);
574 ReadDOC(docptr, Mplus_ReadPipeInit);
576 /* Read the data via the internal pipeline through CDSN IO
577 register, see Pipelined Read Operations 11.3 */
578 MemReadDOC(docptr, buf, 1054);
579 buf[1054] = ReadDOC(docptr, Mplus_LastDataRead);
580 buf[1055] = ReadDOC(docptr, Mplus_LastDataRead);
582 memset(&c[0], 0, sizeof(c));
583 printk("DUMP OFFSET=%x:\n", (int)from);
585 for (i = 0, bp = &buf[0]; (i < 1056); i++) {
586 if ((i % 16) == 0)
587 printk("%08x: ", i);
588 printk(" %02x", *bp);
589 c[(i & 0xf)] = ((*bp >= 0x20) && (*bp <= 0x7f)) ? *bp : '.';
590 bp++;
591 if (((i + 1) % 16) == 0)
592 printk(" %s\n", c);
594 printk("\n");
596 /* Disable flash internally */
597 WriteDOC(0, docptr, Mplus_FlashSelect);
599 return 0;
601 #endif
603 static int doc_read(struct mtd_info *mtd, loff_t from, size_t len,
604 size_t *retlen, u_char *buf)
606 /* Just a special case of doc_read_ecc */
607 return doc_read_ecc(mtd, from, len, retlen, buf, NULL, NULL);
610 static int doc_read_ecc(struct mtd_info *mtd, loff_t from, size_t len,
611 size_t *retlen, u_char *buf, u_char *eccbuf,
612 struct nand_oobinfo *oobsel)
614 int ret, i;
615 volatile char dummy;
616 loff_t fofs;
617 unsigned char syndrome[6];
618 struct DiskOnChip *this = mtd->priv;
619 void __iomem * docptr = this->virtadr;
620 struct Nand *mychip = &this->chips[from >> (this->chipshift)];
622 /* Don't allow read past end of device */
623 if (from >= this->totlen)
624 return -EINVAL;
626 /* Don't allow a single read to cross a 512-byte block boundary */
627 if (from + len > ((from | 0x1ff) + 1))
628 len = ((from | 0x1ff) + 1) - from;
630 DoC_CheckASIC(docptr);
632 /* Find the chip which is to be used and select it */
633 if (this->curfloor != mychip->floor) {
634 DoC_SelectFloor(docptr, mychip->floor);
635 DoC_SelectChip(docptr, mychip->chip);
636 } else if (this->curchip != mychip->chip) {
637 DoC_SelectChip(docptr, mychip->chip);
639 this->curfloor = mychip->floor;
640 this->curchip = mychip->chip;
642 /* Millennium Plus bus cycle sequence as per figure 2, section 2.4 */
643 WriteDOC((DOC_FLASH_CE | DOC_FLASH_WP), docptr, Mplus_FlashSelect);
645 /* Reset the chip, see Software Requirement 11.4 item 1. */
646 DoC_Command(docptr, NAND_CMD_RESET, 0);
647 DoC_WaitReady(docptr);
649 fofs = from;
650 DoC_Command(docptr, DoC_GetDataOffset(mtd, &fofs), 0);
651 DoC_Address(this, 3, fofs, 0, 0x00);
652 WriteDOC(0, docptr, Mplus_FlashControl);
653 DoC_WaitReady(docptr);
655 if (eccbuf) {
656 /* init the ECC engine, see Reed-Solomon EDC/ECC 11.1 .*/
657 WriteDOC(DOC_ECC_RESET, docptr, Mplus_ECCConf);
658 WriteDOC(DOC_ECC_EN, docptr, Mplus_ECCConf);
659 } else {
660 /* disable the ECC engine */
661 WriteDOC(DOC_ECC_RESET, docptr, Mplus_ECCConf);
664 /* Let the caller know we completed it */
665 *retlen = len;
666 ret = 0;
668 ReadDOC(docptr, Mplus_ReadPipeInit);
669 ReadDOC(docptr, Mplus_ReadPipeInit);
671 if (eccbuf) {
672 /* Read the data via the internal pipeline through CDSN IO
673 register, see Pipelined Read Operations 11.3 */
674 MemReadDOC(docptr, buf, len);
676 /* Read the ECC data following raw data */
677 MemReadDOC(docptr, eccbuf, 4);
678 eccbuf[4] = ReadDOC(docptr, Mplus_LastDataRead);
679 eccbuf[5] = ReadDOC(docptr, Mplus_LastDataRead);
681 /* Flush the pipeline */
682 dummy = ReadDOC(docptr, Mplus_ECCConf);
683 dummy = ReadDOC(docptr, Mplus_ECCConf);
685 /* Check the ECC Status */
686 if (ReadDOC(docptr, Mplus_ECCConf) & 0x80) {
687 int nb_errors;
688 /* There was an ECC error */
689 #ifdef ECC_DEBUG
690 printk("DiskOnChip ECC Error: Read at %lx\n", (long)from);
691 #endif
692 /* Read the ECC syndrom through the DiskOnChip ECC logic.
693 These syndrome will be all ZERO when there is no error */
694 for (i = 0; i < 6; i++)
695 syndrome[i] = ReadDOC(docptr, Mplus_ECCSyndrome0 + i);
697 nb_errors = doc_decode_ecc(buf, syndrome);
698 #ifdef ECC_DEBUG
699 printk("ECC Errors corrected: %x\n", nb_errors);
700 #endif
701 if (nb_errors < 0) {
702 /* We return error, but have actually done the read. Not that
703 this can be told to user-space, via sys_read(), but at least
704 MTD-aware stuff can know about it by checking *retlen */
705 #ifdef ECC_DEBUG
706 printk("%s(%d): Millennium Plus ECC error (from=0x%x:\n",
707 __FILE__, __LINE__, (int)from);
708 printk(" syndrome= %02x:%02x:%02x:%02x:%02x:"
709 "%02x\n",
710 syndrome[0], syndrome[1], syndrome[2],
711 syndrome[3], syndrome[4], syndrome[5]);
712 printk(" eccbuf= %02x:%02x:%02x:%02x:%02x:"
713 "%02x\n",
714 eccbuf[0], eccbuf[1], eccbuf[2],
715 eccbuf[3], eccbuf[4], eccbuf[5]);
716 #endif
717 ret = -EIO;
721 #ifdef PSYCHO_DEBUG
722 printk("ECC DATA at %lx: %2.2X %2.2X %2.2X %2.2X %2.2X %2.2X\n",
723 (long)from, eccbuf[0], eccbuf[1], eccbuf[2], eccbuf[3],
724 eccbuf[4], eccbuf[5]);
725 #endif
727 /* disable the ECC engine */
728 WriteDOC(DOC_ECC_DIS, docptr , Mplus_ECCConf);
729 } else {
730 /* Read the data via the internal pipeline through CDSN IO
731 register, see Pipelined Read Operations 11.3 */
732 MemReadDOC(docptr, buf, len-2);
733 buf[len-2] = ReadDOC(docptr, Mplus_LastDataRead);
734 buf[len-1] = ReadDOC(docptr, Mplus_LastDataRead);
737 /* Disable flash internally */
738 WriteDOC(0, docptr, Mplus_FlashSelect);
740 return ret;
743 static int doc_write(struct mtd_info *mtd, loff_t to, size_t len,
744 size_t *retlen, const u_char *buf)
746 char eccbuf[6];
747 return doc_write_ecc(mtd, to, len, retlen, buf, eccbuf, NULL);
750 static int doc_write_ecc(struct mtd_info *mtd, loff_t to, size_t len,
751 size_t *retlen, const u_char *buf, u_char *eccbuf,
752 struct nand_oobinfo *oobsel)
754 int i, before, ret = 0;
755 loff_t fto;
756 volatile char dummy;
757 struct DiskOnChip *this = mtd->priv;
758 void __iomem * docptr = this->virtadr;
759 struct Nand *mychip = &this->chips[to >> (this->chipshift)];
761 /* Don't allow write past end of device */
762 if (to >= this->totlen)
763 return -EINVAL;
765 /* Don't allow writes which aren't exactly one block (512 bytes) */
766 if ((to & 0x1ff) || (len != 0x200))
767 return -EINVAL;
769 /* Determine position of OOB flags, before or after data */
770 before = (this->interleave && (to & 0x200));
772 DoC_CheckASIC(docptr);
774 /* Find the chip which is to be used and select it */
775 if (this->curfloor != mychip->floor) {
776 DoC_SelectFloor(docptr, mychip->floor);
777 DoC_SelectChip(docptr, mychip->chip);
778 } else if (this->curchip != mychip->chip) {
779 DoC_SelectChip(docptr, mychip->chip);
781 this->curfloor = mychip->floor;
782 this->curchip = mychip->chip;
784 /* Millennium Plus bus cycle sequence as per figure 2, section 2.4 */
785 WriteDOC(DOC_FLASH_CE, docptr, Mplus_FlashSelect);
787 /* Reset the chip, see Software Requirement 11.4 item 1. */
788 DoC_Command(docptr, NAND_CMD_RESET, 0);
789 DoC_WaitReady(docptr);
791 /* Set device to appropriate plane of flash */
792 fto = to;
793 WriteDOC(DoC_GetDataOffset(mtd, &fto), docptr, Mplus_FlashCmd);
795 /* On interleaved devices the flags for 2nd half 512 are before data */
796 if (eccbuf && before)
797 fto -= 2;
799 /* issue the Serial Data In command to initial the Page Program process */
800 DoC_Command(docptr, NAND_CMD_SEQIN, 0x00);
801 DoC_Address(this, 3, fto, 0x00, 0x00);
803 /* Disable the ECC engine */
804 WriteDOC(DOC_ECC_RESET, docptr, Mplus_ECCConf);
806 if (eccbuf) {
807 if (before) {
808 /* Write the block status BLOCK_USED (0x5555) */
809 WriteDOC(0x55, docptr, Mil_CDSN_IO);
810 WriteDOC(0x55, docptr, Mil_CDSN_IO);
813 /* init the ECC engine, see Reed-Solomon EDC/ECC 11.1 .*/
814 WriteDOC(DOC_ECC_EN | DOC_ECC_RW, docptr, Mplus_ECCConf);
817 MemWriteDOC(docptr, (unsigned char *) buf, len);
819 if (eccbuf) {
820 /* Write ECC data to flash, the ECC info is generated by
821 the DiskOnChip ECC logic see Reed-Solomon EDC/ECC 11.1 */
822 DoC_Delay(docptr, 3);
824 /* Read the ECC data through the DiskOnChip ECC logic */
825 for (i = 0; i < 6; i++)
826 eccbuf[i] = ReadDOC(docptr, Mplus_ECCSyndrome0 + i);
828 /* disable the ECC engine */
829 WriteDOC(DOC_ECC_DIS, docptr, Mplus_ECCConf);
831 /* Write the ECC data to flash */
832 MemWriteDOC(docptr, eccbuf, 6);
834 if (!before) {
835 /* Write the block status BLOCK_USED (0x5555) */
836 WriteDOC(0x55, docptr, Mil_CDSN_IO+6);
837 WriteDOC(0x55, docptr, Mil_CDSN_IO+7);
840 #ifdef PSYCHO_DEBUG
841 printk("OOB data at %lx is %2.2X %2.2X %2.2X %2.2X %2.2X %2.2X\n",
842 (long) to, eccbuf[0], eccbuf[1], eccbuf[2], eccbuf[3],
843 eccbuf[4], eccbuf[5]);
844 #endif
847 WriteDOC(0x00, docptr, Mplus_WritePipeTerm);
848 WriteDOC(0x00, docptr, Mplus_WritePipeTerm);
850 /* Commit the Page Program command and wait for ready
851 see Software Requirement 11.4 item 1.*/
852 DoC_Command(docptr, NAND_CMD_PAGEPROG, 0x00);
853 DoC_WaitReady(docptr);
855 /* Read the status of the flash device through CDSN IO register
856 see Software Requirement 11.4 item 5.*/
857 DoC_Command(docptr, NAND_CMD_STATUS, 0);
858 dummy = ReadDOC(docptr, Mplus_ReadPipeInit);
859 dummy = ReadDOC(docptr, Mplus_ReadPipeInit);
860 DoC_Delay(docptr, 2);
861 if ((dummy = ReadDOC(docptr, Mplus_LastDataRead)) & 1) {
862 printk("MTD: Error 0x%x programming at 0x%x\n", dummy, (int)to);
863 /* Error in programming
864 FIXME: implement Bad Block Replacement (in nftl.c ??) */
865 *retlen = 0;
866 ret = -EIO;
868 dummy = ReadDOC(docptr, Mplus_LastDataRead);
870 /* Disable flash internally */
871 WriteDOC(0, docptr, Mplus_FlashSelect);
873 /* Let the caller know we completed it */
874 *retlen = len;
876 return ret;
879 static int doc_read_oob(struct mtd_info *mtd, loff_t ofs, size_t len,
880 size_t *retlen, u_char *buf)
882 loff_t fofs, base;
883 struct DiskOnChip *this = mtd->priv;
884 void __iomem * docptr = this->virtadr;
885 struct Nand *mychip = &this->chips[ofs >> this->chipshift];
886 size_t i, size, got, want;
888 DoC_CheckASIC(docptr);
890 /* Find the chip which is to be used and select it */
891 if (this->curfloor != mychip->floor) {
892 DoC_SelectFloor(docptr, mychip->floor);
893 DoC_SelectChip(docptr, mychip->chip);
894 } else if (this->curchip != mychip->chip) {
895 DoC_SelectChip(docptr, mychip->chip);
897 this->curfloor = mychip->floor;
898 this->curchip = mychip->chip;
900 /* Millennium Plus bus cycle sequence as per figure 2, section 2.4 */
901 WriteDOC((DOC_FLASH_CE | DOC_FLASH_WP), docptr, Mplus_FlashSelect);
903 /* disable the ECC engine */
904 WriteDOC(DOC_ECC_RESET, docptr, Mplus_ECCConf);
905 DoC_WaitReady(docptr);
907 /* Maximum of 16 bytes in the OOB region, so limit read to that */
908 if (len > 16)
909 len = 16;
910 got = 0;
911 want = len;
913 for (i = 0; ((i < 3) && (want > 0)); i++) {
914 /* Figure out which region we are accessing... */
915 fofs = ofs;
916 base = ofs & 0xf;
917 if (!this->interleave) {
918 DoC_Command(docptr, NAND_CMD_READOOB, 0);
919 size = 16 - base;
920 } else if (base < 6) {
921 DoC_Command(docptr, DoC_GetECCOffset(mtd, &fofs), 0);
922 size = 6 - base;
923 } else if (base < 8) {
924 DoC_Command(docptr, DoC_GetFlagsOffset(mtd, &fofs), 0);
925 size = 8 - base;
926 } else {
927 DoC_Command(docptr, DoC_GetHdrOffset(mtd, &fofs), 0);
928 size = 16 - base;
930 if (size > want)
931 size = want;
933 /* Issue read command */
934 DoC_Address(this, 3, fofs, 0, 0x00);
935 WriteDOC(0, docptr, Mplus_FlashControl);
936 DoC_WaitReady(docptr);
938 ReadDOC(docptr, Mplus_ReadPipeInit);
939 ReadDOC(docptr, Mplus_ReadPipeInit);
940 MemReadDOC(docptr, &buf[got], size - 2);
941 buf[got + size - 2] = ReadDOC(docptr, Mplus_LastDataRead);
942 buf[got + size - 1] = ReadDOC(docptr, Mplus_LastDataRead);
944 ofs += size;
945 got += size;
946 want -= size;
949 /* Disable flash internally */
950 WriteDOC(0, docptr, Mplus_FlashSelect);
952 *retlen = len;
953 return 0;
956 static int doc_write_oob(struct mtd_info *mtd, loff_t ofs, size_t len,
957 size_t *retlen, const u_char *buf)
959 volatile char dummy;
960 loff_t fofs, base;
961 struct DiskOnChip *this = mtd->priv;
962 void __iomem * docptr = this->virtadr;
963 struct Nand *mychip = &this->chips[ofs >> this->chipshift];
964 size_t i, size, got, want;
965 int ret = 0;
967 DoC_CheckASIC(docptr);
969 /* Find the chip which is to be used and select it */
970 if (this->curfloor != mychip->floor) {
971 DoC_SelectFloor(docptr, mychip->floor);
972 DoC_SelectChip(docptr, mychip->chip);
973 } else if (this->curchip != mychip->chip) {
974 DoC_SelectChip(docptr, mychip->chip);
976 this->curfloor = mychip->floor;
977 this->curchip = mychip->chip;
979 /* Millennium Plus bus cycle sequence as per figure 2, section 2.4 */
980 WriteDOC(DOC_FLASH_CE, docptr, Mplus_FlashSelect);
983 /* Maximum of 16 bytes in the OOB region, so limit write to that */
984 if (len > 16)
985 len = 16;
986 got = 0;
987 want = len;
989 for (i = 0; ((i < 3) && (want > 0)); i++) {
990 /* Reset the chip, see Software Requirement 11.4 item 1. */
991 DoC_Command(docptr, NAND_CMD_RESET, 0);
992 DoC_WaitReady(docptr);
994 /* Figure out which region we are accessing... */
995 fofs = ofs;
996 base = ofs & 0x0f;
997 if (!this->interleave) {
998 WriteDOC(NAND_CMD_READOOB, docptr, Mplus_FlashCmd);
999 size = 16 - base;
1000 } else if (base < 6) {
1001 WriteDOC(DoC_GetECCOffset(mtd, &fofs), docptr, Mplus_FlashCmd);
1002 size = 6 - base;
1003 } else if (base < 8) {
1004 WriteDOC(DoC_GetFlagsOffset(mtd, &fofs), docptr, Mplus_FlashCmd);
1005 size = 8 - base;
1006 } else {
1007 WriteDOC(DoC_GetHdrOffset(mtd, &fofs), docptr, Mplus_FlashCmd);
1008 size = 16 - base;
1010 if (size > want)
1011 size = want;
1013 /* Issue the Serial Data In command to initial the Page Program process */
1014 DoC_Command(docptr, NAND_CMD_SEQIN, 0x00);
1015 DoC_Address(this, 3, fofs, 0, 0x00);
1017 /* Disable the ECC engine */
1018 WriteDOC(DOC_ECC_RESET, docptr, Mplus_ECCConf);
1020 /* Write the data via the internal pipeline through CDSN IO
1021 register, see Pipelined Write Operations 11.2 */
1022 MemWriteDOC(docptr, (unsigned char *) &buf[got], size);
1023 WriteDOC(0x00, docptr, Mplus_WritePipeTerm);
1024 WriteDOC(0x00, docptr, Mplus_WritePipeTerm);
1026 /* Commit the Page Program command and wait for ready
1027 see Software Requirement 11.4 item 1.*/
1028 DoC_Command(docptr, NAND_CMD_PAGEPROG, 0x00);
1029 DoC_WaitReady(docptr);
1031 /* Read the status of the flash device through CDSN IO register
1032 see Software Requirement 11.4 item 5.*/
1033 DoC_Command(docptr, NAND_CMD_STATUS, 0x00);
1034 dummy = ReadDOC(docptr, Mplus_ReadPipeInit);
1035 dummy = ReadDOC(docptr, Mplus_ReadPipeInit);
1036 DoC_Delay(docptr, 2);
1037 if ((dummy = ReadDOC(docptr, Mplus_LastDataRead)) & 1) {
1038 printk("MTD: Error 0x%x programming oob at 0x%x\n",
1039 dummy, (int)ofs);
1040 /* FIXME: implement Bad Block Replacement */
1041 *retlen = 0;
1042 ret = -EIO;
1044 dummy = ReadDOC(docptr, Mplus_LastDataRead);
1046 ofs += size;
1047 got += size;
1048 want -= size;
1051 /* Disable flash internally */
1052 WriteDOC(0, docptr, Mplus_FlashSelect);
1054 *retlen = len;
1055 return ret;
1058 int doc_erase(struct mtd_info *mtd, struct erase_info *instr)
1060 volatile char dummy;
1061 struct DiskOnChip *this = mtd->priv;
1062 __u32 ofs = instr->addr;
1063 __u32 len = instr->len;
1064 void __iomem * docptr = this->virtadr;
1065 struct Nand *mychip = &this->chips[ofs >> this->chipshift];
1067 DoC_CheckASIC(docptr);
1069 if (len != mtd->erasesize)
1070 printk(KERN_WARNING "MTD: Erase not right size (%x != %x)n",
1071 len, mtd->erasesize);
1073 /* Find the chip which is to be used and select it */
1074 if (this->curfloor != mychip->floor) {
1075 DoC_SelectFloor(docptr, mychip->floor);
1076 DoC_SelectChip(docptr, mychip->chip);
1077 } else if (this->curchip != mychip->chip) {
1078 DoC_SelectChip(docptr, mychip->chip);
1080 this->curfloor = mychip->floor;
1081 this->curchip = mychip->chip;
1083 instr->state = MTD_ERASE_PENDING;
1085 /* Millennium Plus bus cycle sequence as per figure 2, section 2.4 */
1086 WriteDOC(DOC_FLASH_CE, docptr, Mplus_FlashSelect);
1088 DoC_Command(docptr, NAND_CMD_RESET, 0x00);
1089 DoC_WaitReady(docptr);
1091 DoC_Command(docptr, NAND_CMD_ERASE1, 0);
1092 DoC_Address(this, 2, ofs, 0, 0x00);
1093 DoC_Command(docptr, NAND_CMD_ERASE2, 0);
1094 DoC_WaitReady(docptr);
1095 instr->state = MTD_ERASING;
1097 /* Read the status of the flash device through CDSN IO register
1098 see Software Requirement 11.4 item 5. */
1099 DoC_Command(docptr, NAND_CMD_STATUS, 0);
1100 dummy = ReadDOC(docptr, Mplus_ReadPipeInit);
1101 dummy = ReadDOC(docptr, Mplus_ReadPipeInit);
1102 if ((dummy = ReadDOC(docptr, Mplus_LastDataRead)) & 1) {
1103 printk("MTD: Error 0x%x erasing at 0x%x\n", dummy, ofs);
1104 /* FIXME: implement Bad Block Replacement (in nftl.c ??) */
1105 instr->state = MTD_ERASE_FAILED;
1106 } else {
1107 instr->state = MTD_ERASE_DONE;
1109 dummy = ReadDOC(docptr, Mplus_LastDataRead);
1111 /* Disable flash internally */
1112 WriteDOC(0, docptr, Mplus_FlashSelect);
1114 mtd_erase_callback(instr);
1116 return 0;
1119 /****************************************************************************
1121 * Module stuff
1123 ****************************************************************************/
1125 static int __init init_doc2001plus(void)
1127 inter_module_register(im_name, THIS_MODULE, &DoCMilPlus_init);
1128 return 0;
1131 static void __exit cleanup_doc2001plus(void)
1133 struct mtd_info *mtd;
1134 struct DiskOnChip *this;
1136 while ((mtd=docmilpluslist)) {
1137 this = mtd->priv;
1138 docmilpluslist = this->nextdoc;
1140 del_mtd_device(mtd);
1142 iounmap(this->virtadr);
1143 kfree(this->chips);
1144 kfree(mtd);
1146 inter_module_unregister(im_name);
1149 module_exit(cleanup_doc2001plus);
1150 module_init(init_doc2001plus);
1152 MODULE_LICENSE("GPL");
1153 MODULE_AUTHOR("Greg Ungerer <gerg@snapgear.com> et al.");
1154 MODULE_DESCRIPTION("Driver for DiskOnChip Millennium Plus");