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.14 2005/11/07 11:14:24 gleixner Exp $
14 #include <linux/kernel.h>
15 #include <linux/module.h>
16 #include <asm/errno.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/init.h>
24 #include <linux/types.h>
25 #include <linux/bitops.h>
27 #include <linux/mtd/mtd.h>
28 #include <linux/mtd/nand.h>
29 #include <linux/mtd/doc2000.h>
31 /* #define ECC_DEBUG */
33 /* I have no idea why some DoC chips can not use memcop_form|to_io().
34 * This may be due to the different revisions of the ASIC controller built-in or
35 * simplily a QA/Bug issue. Who knows ?? If you have trouble, please uncomment
39 static int doc_read(struct mtd_info *mtd, loff_t from, size_t len,
40 size_t *retlen, u_char *buf);
41 static int doc_write(struct mtd_info *mtd, loff_t to, size_t len,
42 size_t *retlen, const u_char *buf);
43 static int doc_read_oob(struct mtd_info *mtd, loff_t ofs,
44 struct mtd_oob_ops *ops);
45 static int doc_write_oob(struct mtd_info *mtd, loff_t ofs,
46 struct mtd_oob_ops *ops);
47 static int doc_erase (struct mtd_info *mtd, struct erase_info *instr);
49 static struct mtd_info *docmilpluslist = NULL;
52 /* Perform the required delay cycles by writing to the NOP register */
53 static void DoC_Delay(void __iomem * docptr, int cycles)
57 for (i = 0; (i < cycles); i++)
58 WriteDOC(0, docptr, Mplus_NOP);
61 #define CDSN_CTRL_FR_B_MASK (CDSN_CTRL_FR_B0 | CDSN_CTRL_FR_B1)
63 /* DOC_WaitReady: Wait for RDY line to be asserted by the flash chip */
64 static int _DoC_WaitReady(void __iomem * docptr)
66 unsigned int c = 0xffff;
68 DEBUG(MTD_DEBUG_LEVEL3,
69 "_DoC_WaitReady called for out-of-line wait\n");
71 /* Out-of-line routine to wait for chip response */
72 while (((ReadDOC(docptr, Mplus_FlashControl) & CDSN_CTRL_FR_B_MASK) != CDSN_CTRL_FR_B_MASK) && --c)
76 DEBUG(MTD_DEBUG_LEVEL2, "_DoC_WaitReady timed out.\n");
81 static inline int DoC_WaitReady(void __iomem * docptr)
83 /* This is inline, to optimise the common case, where it's ready instantly */
86 /* read form NOP register should be issued prior to the read from CDSNControl
87 see Software Requirement 11.4 item 2. */
90 if ((ReadDOC(docptr, Mplus_FlashControl) & CDSN_CTRL_FR_B_MASK) != CDSN_CTRL_FR_B_MASK)
91 /* Call the out-of-line routine to wait */
92 ret = _DoC_WaitReady(docptr);
97 /* For some reason the Millennium Plus seems to occassionally put itself
98 * into reset mode. For me this happens randomly, with no pattern that I
99 * can detect. M-systems suggest always check this on any block level
100 * operation and setting to normal mode if in reset mode.
102 static inline void DoC_CheckASIC(void __iomem * docptr)
104 /* Make sure the DoC is in normal mode */
105 if ((ReadDOC(docptr, Mplus_DOCControl) & DOC_MODE_NORMAL) == 0) {
106 WriteDOC((DOC_MODE_NORMAL | DOC_MODE_MDWREN), docptr, Mplus_DOCControl);
107 WriteDOC(~(DOC_MODE_NORMAL | DOC_MODE_MDWREN), docptr, Mplus_CtrlConfirm);
111 /* DoC_Command: Send a flash command to the flash chip through the Flash
112 * command register. Need 2 Write Pipeline Terminates to complete send.
114 static void DoC_Command(void __iomem * docptr, unsigned char command,
115 unsigned char xtraflags)
117 WriteDOC(command, docptr, Mplus_FlashCmd);
118 WriteDOC(command, docptr, Mplus_WritePipeTerm);
119 WriteDOC(command, docptr, Mplus_WritePipeTerm);
122 /* DoC_Address: Set the current address for the flash chip through the Flash
123 * Address register. Need 2 Write Pipeline Terminates to complete send.
125 static inline void DoC_Address(struct DiskOnChip *doc, int numbytes,
126 unsigned long ofs, unsigned char xtraflags1,
127 unsigned char xtraflags2)
129 void __iomem * docptr = doc->virtadr;
131 /* Allow for possible Mill Plus internal flash interleaving */
132 ofs >>= doc->interleave;
136 /* Send single byte, bits 0-7. */
137 WriteDOC(ofs & 0xff, docptr, Mplus_FlashAddress);
140 /* Send bits 9-16 followed by 17-23 */
141 WriteDOC((ofs >> 9) & 0xff, docptr, Mplus_FlashAddress);
142 WriteDOC((ofs >> 17) & 0xff, docptr, Mplus_FlashAddress);
145 /* Send 0-7, 9-16, then 17-23 */
146 WriteDOC(ofs & 0xff, docptr, Mplus_FlashAddress);
147 WriteDOC((ofs >> 9) & 0xff, docptr, Mplus_FlashAddress);
148 WriteDOC((ofs >> 17) & 0xff, docptr, Mplus_FlashAddress);
154 WriteDOC(0x00, docptr, Mplus_WritePipeTerm);
155 WriteDOC(0x00, docptr, Mplus_WritePipeTerm);
158 /* DoC_SelectChip: Select a given flash chip within the current floor */
159 static int DoC_SelectChip(void __iomem * docptr, int chip)
161 /* No choice for flash chip on Millennium Plus */
165 /* DoC_SelectFloor: Select a given floor (bank of flash chips) */
166 static int DoC_SelectFloor(void __iomem * docptr, int floor)
168 WriteDOC((floor & 0x3), docptr, Mplus_DeviceSelect);
173 * Translate the given offset into the appropriate command and offset.
174 * This does the mapping using the 16bit interleave layout defined by
175 * M-Systems, and looks like this for a sector pair:
176 * +-----------+-------+-------+-------+--------------+---------+-----------+
177 * | 0 --- 511 |512-517|518-519|520-521| 522 --- 1033 |1034-1039|1040 - 1055|
178 * +-----------+-------+-------+-------+--------------+---------+-----------+
179 * | Data 0 | ECC 0 |Flags0 |Flags1 | Data 1 |ECC 1 | OOB 1 + 2 |
180 * +-----------+-------+-------+-------+--------------+---------+-----------+
182 /* FIXME: This lives in INFTL not here. Other users of flash devices
184 static unsigned int DoC_GetDataOffset(struct mtd_info *mtd, loff_t *from)
186 struct DiskOnChip *this = mtd->priv;
188 if (this->interleave) {
189 unsigned int ofs = *from & 0x3ff;
193 cmd = NAND_CMD_READ0;
195 } else if (ofs < 1014) {
196 cmd = NAND_CMD_READ1;
197 ofs = (ofs & 0x1ff) + 10;
199 cmd = NAND_CMD_READOOB;
203 *from = (*from & ~0x3ff) | ofs;
208 return NAND_CMD_READ1;
209 return NAND_CMD_READ0;
213 static unsigned int DoC_GetECCOffset(struct mtd_info *mtd, loff_t *from)
215 unsigned int ofs, cmd;
218 cmd = NAND_CMD_READOOB;
219 ofs = 10 + (*from & 0xf);
221 cmd = NAND_CMD_READ1;
225 *from = (*from & ~0x3ff) | ofs;
229 static unsigned int DoC_GetFlagsOffset(struct mtd_info *mtd, loff_t *from)
231 unsigned int ofs, cmd;
233 cmd = NAND_CMD_READ1;
234 ofs = (*from & 0x200) ? 8 : 6;
235 *from = (*from & ~0x3ff) | ofs;
239 static unsigned int DoC_GetHdrOffset(struct mtd_info *mtd, loff_t *from)
241 unsigned int ofs, cmd;
243 cmd = NAND_CMD_READOOB;
244 ofs = (*from & 0x200) ? 24 : 16;
245 *from = (*from & ~0x3ff) | ofs;
249 static inline void MemReadDOC(void __iomem * docptr, unsigned char *buf, int len)
253 for (i = 0; i < len; i++)
254 buf[i] = ReadDOC(docptr, Mil_CDSN_IO + i);
256 memcpy_fromio(buf, docptr + DoC_Mil_CDSN_IO, len);
260 static inline void MemWriteDOC(void __iomem * docptr, unsigned char *buf, int len)
264 for (i = 0; i < len; i++)
265 WriteDOC(buf[i], docptr, Mil_CDSN_IO + i);
267 memcpy_toio(docptr + DoC_Mil_CDSN_IO, buf, len);
271 /* DoC_IdentChip: Identify a given NAND chip given {floor,chip} */
272 static int DoC_IdentChip(struct DiskOnChip *doc, int floor, int chip)
276 void __iomem * docptr = doc->virtadr;
278 /* Page in the required floor/chip */
279 DoC_SelectFloor(docptr, floor);
280 DoC_SelectChip(docptr, chip);
282 /* Millennium Plus bus cycle sequence as per figure 2, section 2.4 */
283 WriteDOC((DOC_FLASH_CE | DOC_FLASH_WP), docptr, Mplus_FlashSelect);
285 /* Reset the chip, see Software Requirement 11.4 item 1. */
286 DoC_Command(docptr, NAND_CMD_RESET, 0);
287 DoC_WaitReady(docptr);
289 /* Read the NAND chip ID: 1. Send ReadID command */
290 DoC_Command(docptr, NAND_CMD_READID, 0);
292 /* Read the NAND chip ID: 2. Send address byte zero */
293 DoC_Address(doc, 1, 0x00, 0, 0x00);
295 WriteDOC(0, docptr, Mplus_FlashControl);
296 DoC_WaitReady(docptr);
298 /* Read the manufacturer and device id codes of the flash device through
299 CDSN IO register see Software Requirement 11.4 item 5.*/
300 dummy = ReadDOC(docptr, Mplus_ReadPipeInit);
301 dummy = ReadDOC(docptr, Mplus_ReadPipeInit);
303 mfr = ReadDOC(docptr, Mil_CDSN_IO);
305 dummy = ReadDOC(docptr, Mil_CDSN_IO); /* 2 way interleave */
307 id = ReadDOC(docptr, Mil_CDSN_IO);
309 dummy = ReadDOC(docptr, Mil_CDSN_IO); /* 2 way interleave */
311 dummy = ReadDOC(docptr, Mplus_LastDataRead);
312 dummy = ReadDOC(docptr, Mplus_LastDataRead);
314 /* Disable flash internally */
315 WriteDOC(0, docptr, Mplus_FlashSelect);
317 /* No response - return failure */
318 if (mfr == 0xff || mfr == 0)
321 for (i = 0; nand_flash_ids[i].name != NULL; i++) {
322 if (id == nand_flash_ids[i].id) {
323 /* Try to identify manufacturer */
324 for (j = 0; nand_manuf_ids[j].id != 0x0; j++) {
325 if (nand_manuf_ids[j].id == mfr)
328 printk(KERN_INFO "Flash chip found: Manufacturer ID: %2.2X, "
329 "Chip ID: %2.2X (%s:%s)\n", mfr, id,
330 nand_manuf_ids[j].name, nand_flash_ids[i].name);
333 doc->chipshift = ffs((nand_flash_ids[i].chipsize << 20)) - 1;
334 doc->erasesize = nand_flash_ids[i].erasesize << doc->interleave;
339 if (nand_flash_ids[i].name == NULL)
344 /* DoC_ScanChips: Find all NAND chips present in a DiskOnChip, and identify them */
345 static void DoC_ScanChips(struct DiskOnChip *this)
348 int numchips[MAX_FLOORS_MPLUS];
355 /* Work out the intended interleave setting */
356 this->interleave = 0;
357 if (this->ChipID == DOC_ChipID_DocMilPlus32)
358 this->interleave = 1;
360 /* Check the ASIC agrees */
361 if ( (this->interleave << 2) !=
362 (ReadDOC(this->virtadr, Mplus_Configuration) & 4)) {
363 u_char conf = ReadDOC(this->virtadr, Mplus_Configuration);
364 printk(KERN_NOTICE "Setting DiskOnChip Millennium Plus interleave to %s\n",
365 this->interleave?"on (16-bit)":"off (8-bit)");
367 WriteDOC(conf, this->virtadr, Mplus_Configuration);
370 /* For each floor, find the number of valid chips it contains */
371 for (floor = 0,ret = 1; floor < MAX_FLOORS_MPLUS; floor++) {
373 for (chip = 0; chip < MAX_CHIPS_MPLUS && ret != 0; chip++) {
374 ret = DoC_IdentChip(this, floor, chip);
381 /* If there are none at all that we recognise, bail */
382 if (!this->numchips) {
383 printk("No flash chips recognised.\n");
387 /* Allocate an array to hold the information for each chip */
388 this->chips = kmalloc(sizeof(struct Nand) * this->numchips, GFP_KERNEL);
390 printk("MTD: No memory for allocating chip info structures\n");
394 /* Fill out the chip array with {floor, chipno} for each
395 * detected chip in the device. */
396 for (floor = 0, ret = 0; floor < MAX_FLOORS_MPLUS; floor++) {
397 for (chip = 0 ; chip < numchips[floor] ; chip++) {
398 this->chips[ret].floor = floor;
399 this->chips[ret].chip = chip;
400 this->chips[ret].curadr = 0;
401 this->chips[ret].curmode = 0x50;
406 /* Calculate and print the total size of the device */
407 this->totlen = this->numchips * (1 << this->chipshift);
408 printk(KERN_INFO "%d flash chips found. Total DiskOnChip size: %ld MiB\n",
409 this->numchips ,this->totlen >> 20);
412 static int DoCMilPlus_is_alias(struct DiskOnChip *doc1, struct DiskOnChip *doc2)
414 int tmp1, tmp2, retval;
416 if (doc1->physadr == doc2->physadr)
419 /* Use the alias resolution register which was set aside for this
420 * purpose. If it's value is the same on both chips, they might
421 * be the same chip, and we write to one and check for a change in
422 * the other. It's unclear if this register is usuable in the
423 * DoC 2000 (it's in the Millennium docs), but it seems to work. */
424 tmp1 = ReadDOC(doc1->virtadr, Mplus_AliasResolution);
425 tmp2 = ReadDOC(doc2->virtadr, Mplus_AliasResolution);
429 WriteDOC((tmp1+1) % 0xff, doc1->virtadr, Mplus_AliasResolution);
430 tmp2 = ReadDOC(doc2->virtadr, Mplus_AliasResolution);
431 if (tmp2 == (tmp1+1) % 0xff)
436 /* Restore register contents. May not be necessary, but do it just to
438 WriteDOC(tmp1, doc1->virtadr, Mplus_AliasResolution);
443 /* This routine is found from the docprobe code by symbol_get(),
444 * which will bump the use count of this module. */
445 void DoCMilPlus_init(struct mtd_info *mtd)
447 struct DiskOnChip *this = mtd->priv;
448 struct DiskOnChip *old = NULL;
450 /* We must avoid being called twice for the same device. */
452 old = docmilpluslist->priv;
455 if (DoCMilPlus_is_alias(this, old)) {
456 printk(KERN_NOTICE "Ignoring DiskOnChip Millennium "
457 "Plus at 0x%lX - already configured\n",
459 iounmap(this->virtadr);
464 old = old->nextdoc->priv;
469 mtd->name = "DiskOnChip Millennium Plus";
470 printk(KERN_NOTICE "DiskOnChip Millennium Plus found at "
471 "address 0x%lX\n", this->physadr);
473 mtd->type = MTD_NANDFLASH;
474 mtd->flags = MTD_CAP_NANDFLASH;
478 mtd->writesize = 512;
480 mtd->owner = THIS_MODULE;
481 mtd->erase = doc_erase;
484 mtd->read = doc_read;
485 mtd->write = doc_write;
486 mtd->read_oob = doc_read_oob;
487 mtd->write_oob = doc_write_oob;
495 /* Ident all the chips present. */
500 iounmap(this->virtadr);
502 this->nextdoc = docmilpluslist;
503 docmilpluslist = mtd;
504 mtd->size = this->totlen;
505 mtd->erasesize = this->erasesize;
510 EXPORT_SYMBOL_GPL(DoCMilPlus_init);
513 static int doc_dumpblk(struct mtd_info *mtd, loff_t from)
517 struct DiskOnChip *this = mtd->priv;
518 void __iomem * docptr = this->virtadr;
519 struct Nand *mychip = &this->chips[from >> (this->chipshift)];
520 unsigned char *bp, buf[1056];
525 /* Don't allow read past end of device */
526 if (from >= this->totlen)
529 DoC_CheckASIC(docptr);
531 /* Find the chip which is to be used and select it */
532 if (this->curfloor != mychip->floor) {
533 DoC_SelectFloor(docptr, mychip->floor);
534 DoC_SelectChip(docptr, mychip->chip);
535 } else if (this->curchip != mychip->chip) {
536 DoC_SelectChip(docptr, mychip->chip);
538 this->curfloor = mychip->floor;
539 this->curchip = mychip->chip;
541 /* Millennium Plus bus cycle sequence as per figure 2, section 2.4 */
542 WriteDOC((DOC_FLASH_CE | DOC_FLASH_WP), docptr, Mplus_FlashSelect);
544 /* Reset the chip, see Software Requirement 11.4 item 1. */
545 DoC_Command(docptr, NAND_CMD_RESET, 0);
546 DoC_WaitReady(docptr);
549 DoC_Command(docptr, DoC_GetDataOffset(mtd, &fofs), 0);
550 DoC_Address(this, 3, fofs, 0, 0x00);
551 WriteDOC(0, docptr, Mplus_FlashControl);
552 DoC_WaitReady(docptr);
554 /* disable the ECC engine */
555 WriteDOC(DOC_ECC_RESET, docptr, Mplus_ECCConf);
557 ReadDOC(docptr, Mplus_ReadPipeInit);
558 ReadDOC(docptr, Mplus_ReadPipeInit);
560 /* Read the data via the internal pipeline through CDSN IO
561 register, see Pipelined Read Operations 11.3 */
562 MemReadDOC(docptr, buf, 1054);
563 buf[1054] = ReadDOC(docptr, Mplus_LastDataRead);
564 buf[1055] = ReadDOC(docptr, Mplus_LastDataRead);
566 memset(&c[0], 0, sizeof(c));
567 printk("DUMP OFFSET=%x:\n", (int)from);
569 for (i = 0, bp = &buf[0]; (i < 1056); i++) {
572 printk(" %02x", *bp);
573 c[(i & 0xf)] = ((*bp >= 0x20) && (*bp <= 0x7f)) ? *bp : '.';
575 if (((i + 1) % 16) == 0)
580 /* Disable flash internally */
581 WriteDOC(0, docptr, Mplus_FlashSelect);
587 static int doc_read(struct mtd_info *mtd, loff_t from, size_t len,
588 size_t *retlen, u_char *buf)
593 unsigned char syndrome[6], eccbuf[6];
594 struct DiskOnChip *this = mtd->priv;
595 void __iomem * docptr = this->virtadr;
596 struct Nand *mychip = &this->chips[from >> (this->chipshift)];
598 /* Don't allow read past end of device */
599 if (from >= this->totlen)
602 /* Don't allow a single read to cross a 512-byte block boundary */
603 if (from + len > ((from | 0x1ff) + 1))
604 len = ((from | 0x1ff) + 1) - from;
606 DoC_CheckASIC(docptr);
608 /* Find the chip which is to be used and select it */
609 if (this->curfloor != mychip->floor) {
610 DoC_SelectFloor(docptr, mychip->floor);
611 DoC_SelectChip(docptr, mychip->chip);
612 } else if (this->curchip != mychip->chip) {
613 DoC_SelectChip(docptr, mychip->chip);
615 this->curfloor = mychip->floor;
616 this->curchip = mychip->chip;
618 /* Millennium Plus bus cycle sequence as per figure 2, section 2.4 */
619 WriteDOC((DOC_FLASH_CE | DOC_FLASH_WP), docptr, Mplus_FlashSelect);
621 /* Reset the chip, see Software Requirement 11.4 item 1. */
622 DoC_Command(docptr, NAND_CMD_RESET, 0);
623 DoC_WaitReady(docptr);
626 DoC_Command(docptr, DoC_GetDataOffset(mtd, &fofs), 0);
627 DoC_Address(this, 3, fofs, 0, 0x00);
628 WriteDOC(0, docptr, Mplus_FlashControl);
629 DoC_WaitReady(docptr);
631 /* init the ECC engine, see Reed-Solomon EDC/ECC 11.1 .*/
632 WriteDOC(DOC_ECC_RESET, docptr, Mplus_ECCConf);
633 WriteDOC(DOC_ECC_EN, docptr, Mplus_ECCConf);
635 /* Let the caller know we completed it */
639 ReadDOC(docptr, Mplus_ReadPipeInit);
640 ReadDOC(docptr, Mplus_ReadPipeInit);
642 /* Read the data via the internal pipeline through CDSN IO
643 register, see Pipelined Read Operations 11.3 */
644 MemReadDOC(docptr, buf, len);
646 /* Read the ECC data following raw data */
647 MemReadDOC(docptr, eccbuf, 4);
648 eccbuf[4] = ReadDOC(docptr, Mplus_LastDataRead);
649 eccbuf[5] = ReadDOC(docptr, Mplus_LastDataRead);
651 /* Flush the pipeline */
652 dummy = ReadDOC(docptr, Mplus_ECCConf);
653 dummy = ReadDOC(docptr, Mplus_ECCConf);
655 /* Check the ECC Status */
656 if (ReadDOC(docptr, Mplus_ECCConf) & 0x80) {
658 /* There was an ECC error */
660 printk("DiskOnChip ECC Error: Read at %lx\n", (long)from);
662 /* Read the ECC syndrom through the DiskOnChip ECC logic.
663 These syndrome will be all ZERO when there is no error */
664 for (i = 0; i < 6; i++)
665 syndrome[i] = ReadDOC(docptr, Mplus_ECCSyndrome0 + i);
667 nb_errors = doc_decode_ecc(buf, syndrome);
669 printk("ECC Errors corrected: %x\n", nb_errors);
672 /* We return error, but have actually done the
673 read. Not that this can be told to user-space, via
674 sys_read(), but at least MTD-aware stuff can know
675 about it by checking *retlen */
677 printk("%s(%d): Millennium Plus ECC error (from=0x%x:\n",
678 __FILE__, __LINE__, (int)from);
679 printk(" syndrome= %02x:%02x:%02x:%02x:%02x:"
681 syndrome[0], syndrome[1], syndrome[2],
682 syndrome[3], syndrome[4], syndrome[5]);
683 printk(" eccbuf= %02x:%02x:%02x:%02x:%02x:"
685 eccbuf[0], eccbuf[1], eccbuf[2],
686 eccbuf[3], eccbuf[4], eccbuf[5]);
693 printk("ECC DATA at %lx: %2.2X %2.2X %2.2X %2.2X %2.2X %2.2X\n",
694 (long)from, eccbuf[0], eccbuf[1], eccbuf[2], eccbuf[3],
695 eccbuf[4], eccbuf[5]);
697 /* disable the ECC engine */
698 WriteDOC(DOC_ECC_DIS, docptr , Mplus_ECCConf);
700 /* Disable flash internally */
701 WriteDOC(0, docptr, Mplus_FlashSelect);
706 static int doc_write(struct mtd_info *mtd, loff_t to, size_t len,
707 size_t *retlen, const u_char *buf)
709 int i, before, ret = 0;
713 struct DiskOnChip *this = mtd->priv;
714 void __iomem * docptr = this->virtadr;
715 struct Nand *mychip = &this->chips[to >> (this->chipshift)];
717 /* Don't allow write past end of device */
718 if (to >= this->totlen)
721 /* Don't allow writes which aren't exactly one block (512 bytes) */
722 if ((to & 0x1ff) || (len != 0x200))
725 /* Determine position of OOB flags, before or after data */
726 before = (this->interleave && (to & 0x200));
728 DoC_CheckASIC(docptr);
730 /* Find the chip which is to be used and select it */
731 if (this->curfloor != mychip->floor) {
732 DoC_SelectFloor(docptr, mychip->floor);
733 DoC_SelectChip(docptr, mychip->chip);
734 } else if (this->curchip != mychip->chip) {
735 DoC_SelectChip(docptr, mychip->chip);
737 this->curfloor = mychip->floor;
738 this->curchip = mychip->chip;
740 /* Millennium Plus bus cycle sequence as per figure 2, section 2.4 */
741 WriteDOC(DOC_FLASH_CE, docptr, Mplus_FlashSelect);
743 /* Reset the chip, see Software Requirement 11.4 item 1. */
744 DoC_Command(docptr, NAND_CMD_RESET, 0);
745 DoC_WaitReady(docptr);
747 /* Set device to appropriate plane of flash */
749 WriteDOC(DoC_GetDataOffset(mtd, &fto), docptr, Mplus_FlashCmd);
751 /* On interleaved devices the flags for 2nd half 512 are before data */
752 if (eccbuf && before)
755 /* issue the Serial Data In command to initial the Page Program process */
756 DoC_Command(docptr, NAND_CMD_SEQIN, 0x00);
757 DoC_Address(this, 3, fto, 0x00, 0x00);
759 /* Disable the ECC engine */
760 WriteDOC(DOC_ECC_RESET, docptr, Mplus_ECCConf);
763 /* Write the block status BLOCK_USED (0x5555) */
764 WriteDOC(0x55, docptr, Mil_CDSN_IO);
765 WriteDOC(0x55, docptr, Mil_CDSN_IO);
768 /* init the ECC engine, see Reed-Solomon EDC/ECC 11.1 .*/
769 WriteDOC(DOC_ECC_EN | DOC_ECC_RW, docptr, Mplus_ECCConf);
771 MemWriteDOC(docptr, (unsigned char *) buf, len);
773 /* Write ECC data to flash, the ECC info is generated by
774 the DiskOnChip ECC logic see Reed-Solomon EDC/ECC 11.1 */
775 DoC_Delay(docptr, 3);
777 /* Read the ECC data through the DiskOnChip ECC logic */
778 for (i = 0; i < 6; i++)
779 eccbuf[i] = ReadDOC(docptr, Mplus_ECCSyndrome0 + i);
781 /* disable the ECC engine */
782 WriteDOC(DOC_ECC_DIS, docptr, Mplus_ECCConf);
784 /* Write the ECC data to flash */
785 MemWriteDOC(docptr, eccbuf, 6);
788 /* Write the block status BLOCK_USED (0x5555) */
789 WriteDOC(0x55, docptr, Mil_CDSN_IO+6);
790 WriteDOC(0x55, docptr, Mil_CDSN_IO+7);
794 printk("OOB data at %lx is %2.2X %2.2X %2.2X %2.2X %2.2X %2.2X\n",
795 (long) to, eccbuf[0], eccbuf[1], eccbuf[2], eccbuf[3],
796 eccbuf[4], eccbuf[5]);
799 WriteDOC(0x00, docptr, Mplus_WritePipeTerm);
800 WriteDOC(0x00, docptr, Mplus_WritePipeTerm);
802 /* Commit the Page Program command and wait for ready
803 see Software Requirement 11.4 item 1.*/
804 DoC_Command(docptr, NAND_CMD_PAGEPROG, 0x00);
805 DoC_WaitReady(docptr);
807 /* Read the status of the flash device through CDSN IO register
808 see Software Requirement 11.4 item 5.*/
809 DoC_Command(docptr, NAND_CMD_STATUS, 0);
810 dummy = ReadDOC(docptr, Mplus_ReadPipeInit);
811 dummy = ReadDOC(docptr, Mplus_ReadPipeInit);
812 DoC_Delay(docptr, 2);
813 if ((dummy = ReadDOC(docptr, Mplus_LastDataRead)) & 1) {
814 printk("MTD: Error 0x%x programming at 0x%x\n", dummy, (int)to);
815 /* Error in programming
816 FIXME: implement Bad Block Replacement (in nftl.c ??) */
820 dummy = ReadDOC(docptr, Mplus_LastDataRead);
822 /* Disable flash internally */
823 WriteDOC(0, docptr, Mplus_FlashSelect);
825 /* Let the caller know we completed it */
831 static int doc_read_oob(struct mtd_info *mtd, loff_t ofs,
832 struct mtd_oob_ops *ops)
835 struct DiskOnChip *this = mtd->priv;
836 void __iomem * docptr = this->virtadr;
837 struct Nand *mychip = &this->chips[ofs >> this->chipshift];
838 size_t i, size, got, want;
839 uint8_t *buf = ops->oobbuf;
840 size_t len = ops->len;
842 BUG_ON(ops->mode != MTD_OOB_PLACE);
846 DoC_CheckASIC(docptr);
848 /* Find the chip which is to be used and select it */
849 if (this->curfloor != mychip->floor) {
850 DoC_SelectFloor(docptr, mychip->floor);
851 DoC_SelectChip(docptr, mychip->chip);
852 } else if (this->curchip != mychip->chip) {
853 DoC_SelectChip(docptr, mychip->chip);
855 this->curfloor = mychip->floor;
856 this->curchip = mychip->chip;
858 /* Millennium Plus bus cycle sequence as per figure 2, section 2.4 */
859 WriteDOC((DOC_FLASH_CE | DOC_FLASH_WP), docptr, Mplus_FlashSelect);
861 /* disable the ECC engine */
862 WriteDOC(DOC_ECC_RESET, docptr, Mplus_ECCConf);
863 DoC_WaitReady(docptr);
865 /* Maximum of 16 bytes in the OOB region, so limit read to that */
871 for (i = 0; ((i < 3) && (want > 0)); i++) {
872 /* Figure out which region we are accessing... */
875 if (!this->interleave) {
876 DoC_Command(docptr, NAND_CMD_READOOB, 0);
878 } else if (base < 6) {
879 DoC_Command(docptr, DoC_GetECCOffset(mtd, &fofs), 0);
881 } else if (base < 8) {
882 DoC_Command(docptr, DoC_GetFlagsOffset(mtd, &fofs), 0);
885 DoC_Command(docptr, DoC_GetHdrOffset(mtd, &fofs), 0);
891 /* Issue read command */
892 DoC_Address(this, 3, fofs, 0, 0x00);
893 WriteDOC(0, docptr, Mplus_FlashControl);
894 DoC_WaitReady(docptr);
896 ReadDOC(docptr, Mplus_ReadPipeInit);
897 ReadDOC(docptr, Mplus_ReadPipeInit);
898 MemReadDOC(docptr, &buf[got], size - 2);
899 buf[got + size - 2] = ReadDOC(docptr, Mplus_LastDataRead);
900 buf[got + size - 1] = ReadDOC(docptr, Mplus_LastDataRead);
907 /* Disable flash internally */
908 WriteDOC(0, docptr, Mplus_FlashSelect);
914 static int doc_write_oob(struct mtd_info *mtd, loff_t ofs,
915 struct mtd_oob_ops *ops)
919 struct DiskOnChip *this = mtd->priv;
920 void __iomem * docptr = this->virtadr;
921 struct Nand *mychip = &this->chips[ofs >> this->chipshift];
922 size_t i, size, got, want;
924 uint8_t *buf = ops->oobbuf;
925 size_t len = ops->len;
927 BUG_ON(ops->mode != MTD_OOB_PLACE);
931 DoC_CheckASIC(docptr);
933 /* Find the chip which is to be used and select it */
934 if (this->curfloor != mychip->floor) {
935 DoC_SelectFloor(docptr, mychip->floor);
936 DoC_SelectChip(docptr, mychip->chip);
937 } else if (this->curchip != mychip->chip) {
938 DoC_SelectChip(docptr, mychip->chip);
940 this->curfloor = mychip->floor;
941 this->curchip = mychip->chip;
943 /* Millennium Plus bus cycle sequence as per figure 2, section 2.4 */
944 WriteDOC(DOC_FLASH_CE, docptr, Mplus_FlashSelect);
947 /* Maximum of 16 bytes in the OOB region, so limit write to that */
953 for (i = 0; ((i < 3) && (want > 0)); i++) {
954 /* Reset the chip, see Software Requirement 11.4 item 1. */
955 DoC_Command(docptr, NAND_CMD_RESET, 0);
956 DoC_WaitReady(docptr);
958 /* Figure out which region we are accessing... */
961 if (!this->interleave) {
962 WriteDOC(NAND_CMD_READOOB, docptr, Mplus_FlashCmd);
964 } else if (base < 6) {
965 WriteDOC(DoC_GetECCOffset(mtd, &fofs), docptr, Mplus_FlashCmd);
967 } else if (base < 8) {
968 WriteDOC(DoC_GetFlagsOffset(mtd, &fofs), docptr, Mplus_FlashCmd);
971 WriteDOC(DoC_GetHdrOffset(mtd, &fofs), docptr, Mplus_FlashCmd);
977 /* Issue the Serial Data In command to initial the Page Program process */
978 DoC_Command(docptr, NAND_CMD_SEQIN, 0x00);
979 DoC_Address(this, 3, fofs, 0, 0x00);
981 /* Disable the ECC engine */
982 WriteDOC(DOC_ECC_RESET, docptr, Mplus_ECCConf);
984 /* Write the data via the internal pipeline through CDSN IO
985 register, see Pipelined Write Operations 11.2 */
986 MemWriteDOC(docptr, (unsigned char *) &buf[got], size);
987 WriteDOC(0x00, docptr, Mplus_WritePipeTerm);
988 WriteDOC(0x00, docptr, Mplus_WritePipeTerm);
990 /* Commit the Page Program command and wait for ready
991 see Software Requirement 11.4 item 1.*/
992 DoC_Command(docptr, NAND_CMD_PAGEPROG, 0x00);
993 DoC_WaitReady(docptr);
995 /* Read the status of the flash device through CDSN IO register
996 see Software Requirement 11.4 item 5.*/
997 DoC_Command(docptr, NAND_CMD_STATUS, 0x00);
998 dummy = ReadDOC(docptr, Mplus_ReadPipeInit);
999 dummy = ReadDOC(docptr, Mplus_ReadPipeInit);
1000 DoC_Delay(docptr, 2);
1001 if ((dummy = ReadDOC(docptr, Mplus_LastDataRead)) & 1) {
1002 printk("MTD: Error 0x%x programming oob at 0x%x\n",
1004 /* FIXME: implement Bad Block Replacement */
1008 dummy = ReadDOC(docptr, Mplus_LastDataRead);
1015 /* Disable flash internally */
1016 WriteDOC(0, docptr, Mplus_FlashSelect);
1022 int doc_erase(struct mtd_info *mtd, struct erase_info *instr)
1024 volatile char dummy;
1025 struct DiskOnChip *this = mtd->priv;
1026 __u32 ofs = instr->addr;
1027 __u32 len = instr->len;
1028 void __iomem * docptr = this->virtadr;
1029 struct Nand *mychip = &this->chips[ofs >> this->chipshift];
1031 DoC_CheckASIC(docptr);
1033 if (len != mtd->erasesize)
1034 printk(KERN_WARNING "MTD: Erase not right size (%x != %x)n",
1035 len, mtd->erasesize);
1037 /* Find the chip which is to be used and select it */
1038 if (this->curfloor != mychip->floor) {
1039 DoC_SelectFloor(docptr, mychip->floor);
1040 DoC_SelectChip(docptr, mychip->chip);
1041 } else if (this->curchip != mychip->chip) {
1042 DoC_SelectChip(docptr, mychip->chip);
1044 this->curfloor = mychip->floor;
1045 this->curchip = mychip->chip;
1047 instr->state = MTD_ERASE_PENDING;
1049 /* Millennium Plus bus cycle sequence as per figure 2, section 2.4 */
1050 WriteDOC(DOC_FLASH_CE, docptr, Mplus_FlashSelect);
1052 DoC_Command(docptr, NAND_CMD_RESET, 0x00);
1053 DoC_WaitReady(docptr);
1055 DoC_Command(docptr, NAND_CMD_ERASE1, 0);
1056 DoC_Address(this, 2, ofs, 0, 0x00);
1057 DoC_Command(docptr, NAND_CMD_ERASE2, 0);
1058 DoC_WaitReady(docptr);
1059 instr->state = MTD_ERASING;
1061 /* Read the status of the flash device through CDSN IO register
1062 see Software Requirement 11.4 item 5. */
1063 DoC_Command(docptr, NAND_CMD_STATUS, 0);
1064 dummy = ReadDOC(docptr, Mplus_ReadPipeInit);
1065 dummy = ReadDOC(docptr, Mplus_ReadPipeInit);
1066 if ((dummy = ReadDOC(docptr, Mplus_LastDataRead)) & 1) {
1067 printk("MTD: Error 0x%x erasing at 0x%x\n", dummy, ofs);
1068 /* FIXME: implement Bad Block Replacement (in nftl.c ??) */
1069 instr->state = MTD_ERASE_FAILED;
1071 instr->state = MTD_ERASE_DONE;
1073 dummy = ReadDOC(docptr, Mplus_LastDataRead);
1075 /* Disable flash internally */
1076 WriteDOC(0, docptr, Mplus_FlashSelect);
1078 mtd_erase_callback(instr);
1083 /****************************************************************************
1087 ****************************************************************************/
1089 static void __exit cleanup_doc2001plus(void)
1091 struct mtd_info *mtd;
1092 struct DiskOnChip *this;
1094 while ((mtd=docmilpluslist)) {
1096 docmilpluslist = this->nextdoc;
1098 del_mtd_device(mtd);
1100 iounmap(this->virtadr);
1106 module_exit(cleanup_doc2001plus);
1108 MODULE_LICENSE("GPL");
1109 MODULE_AUTHOR("Greg Ungerer <gerg@snapgear.com> et al.");
1110 MODULE_DESCRIPTION("Driver for DiskOnChip Millennium Plus");