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;
475 mtd->ecctype = MTD_ECC_RS_DiskOnChip;
479 mtd->writesize = 512;
481 mtd->owner = THIS_MODULE;
482 mtd->erase = doc_erase;
485 mtd->read = doc_read;
486 mtd->write = doc_write;
487 mtd->read_oob = doc_read_oob;
488 mtd->write_oob = doc_write_oob;
496 /* Ident all the chips present. */
501 iounmap(this->virtadr);
503 this->nextdoc = docmilpluslist;
504 docmilpluslist = mtd;
505 mtd->size = this->totlen;
506 mtd->erasesize = this->erasesize;
511 EXPORT_SYMBOL_GPL(DoCMilPlus_init);
514 static int doc_dumpblk(struct mtd_info *mtd, loff_t from)
518 struct DiskOnChip *this = mtd->priv;
519 void __iomem * docptr = this->virtadr;
520 struct Nand *mychip = &this->chips[from >> (this->chipshift)];
521 unsigned char *bp, buf[1056];
526 /* Don't allow read past end of device */
527 if (from >= this->totlen)
530 DoC_CheckASIC(docptr);
532 /* Find the chip which is to be used and select it */
533 if (this->curfloor != mychip->floor) {
534 DoC_SelectFloor(docptr, mychip->floor);
535 DoC_SelectChip(docptr, mychip->chip);
536 } else if (this->curchip != mychip->chip) {
537 DoC_SelectChip(docptr, mychip->chip);
539 this->curfloor = mychip->floor;
540 this->curchip = mychip->chip;
542 /* Millennium Plus bus cycle sequence as per figure 2, section 2.4 */
543 WriteDOC((DOC_FLASH_CE | DOC_FLASH_WP), docptr, Mplus_FlashSelect);
545 /* Reset the chip, see Software Requirement 11.4 item 1. */
546 DoC_Command(docptr, NAND_CMD_RESET, 0);
547 DoC_WaitReady(docptr);
550 DoC_Command(docptr, DoC_GetDataOffset(mtd, &fofs), 0);
551 DoC_Address(this, 3, fofs, 0, 0x00);
552 WriteDOC(0, docptr, Mplus_FlashControl);
553 DoC_WaitReady(docptr);
555 /* disable the ECC engine */
556 WriteDOC(DOC_ECC_RESET, docptr, Mplus_ECCConf);
558 ReadDOC(docptr, Mplus_ReadPipeInit);
559 ReadDOC(docptr, Mplus_ReadPipeInit);
561 /* Read the data via the internal pipeline through CDSN IO
562 register, see Pipelined Read Operations 11.3 */
563 MemReadDOC(docptr, buf, 1054);
564 buf[1054] = ReadDOC(docptr, Mplus_LastDataRead);
565 buf[1055] = ReadDOC(docptr, Mplus_LastDataRead);
567 memset(&c[0], 0, sizeof(c));
568 printk("DUMP OFFSET=%x:\n", (int)from);
570 for (i = 0, bp = &buf[0]; (i < 1056); i++) {
573 printk(" %02x", *bp);
574 c[(i & 0xf)] = ((*bp >= 0x20) && (*bp <= 0x7f)) ? *bp : '.';
576 if (((i + 1) % 16) == 0)
581 /* Disable flash internally */
582 WriteDOC(0, docptr, Mplus_FlashSelect);
588 static int doc_read(struct mtd_info *mtd, loff_t from, size_t len,
589 size_t *retlen, u_char *buf)
594 unsigned char syndrome[6], eccbuf[6];
595 struct DiskOnChip *this = mtd->priv;
596 void __iomem * docptr = this->virtadr;
597 struct Nand *mychip = &this->chips[from >> (this->chipshift)];
599 /* Don't allow read past end of device */
600 if (from >= this->totlen)
603 /* Don't allow a single read to cross a 512-byte block boundary */
604 if (from + len > ((from | 0x1ff) + 1))
605 len = ((from | 0x1ff) + 1) - from;
607 DoC_CheckASIC(docptr);
609 /* Find the chip which is to be used and select it */
610 if (this->curfloor != mychip->floor) {
611 DoC_SelectFloor(docptr, mychip->floor);
612 DoC_SelectChip(docptr, mychip->chip);
613 } else if (this->curchip != mychip->chip) {
614 DoC_SelectChip(docptr, mychip->chip);
616 this->curfloor = mychip->floor;
617 this->curchip = mychip->chip;
619 /* Millennium Plus bus cycle sequence as per figure 2, section 2.4 */
620 WriteDOC((DOC_FLASH_CE | DOC_FLASH_WP), docptr, Mplus_FlashSelect);
622 /* Reset the chip, see Software Requirement 11.4 item 1. */
623 DoC_Command(docptr, NAND_CMD_RESET, 0);
624 DoC_WaitReady(docptr);
627 DoC_Command(docptr, DoC_GetDataOffset(mtd, &fofs), 0);
628 DoC_Address(this, 3, fofs, 0, 0x00);
629 WriteDOC(0, docptr, Mplus_FlashControl);
630 DoC_WaitReady(docptr);
632 /* init the ECC engine, see Reed-Solomon EDC/ECC 11.1 .*/
633 WriteDOC(DOC_ECC_RESET, docptr, Mplus_ECCConf);
634 WriteDOC(DOC_ECC_EN, docptr, Mplus_ECCConf);
636 /* Let the caller know we completed it */
640 ReadDOC(docptr, Mplus_ReadPipeInit);
641 ReadDOC(docptr, Mplus_ReadPipeInit);
643 /* Read the data via the internal pipeline through CDSN IO
644 register, see Pipelined Read Operations 11.3 */
645 MemReadDOC(docptr, buf, len);
647 /* Read the ECC data following raw data */
648 MemReadDOC(docptr, eccbuf, 4);
649 eccbuf[4] = ReadDOC(docptr, Mplus_LastDataRead);
650 eccbuf[5] = ReadDOC(docptr, Mplus_LastDataRead);
652 /* Flush the pipeline */
653 dummy = ReadDOC(docptr, Mplus_ECCConf);
654 dummy = ReadDOC(docptr, Mplus_ECCConf);
656 /* Check the ECC Status */
657 if (ReadDOC(docptr, Mplus_ECCConf) & 0x80) {
659 /* There was an ECC error */
661 printk("DiskOnChip ECC Error: Read at %lx\n", (long)from);
663 /* Read the ECC syndrom through the DiskOnChip ECC logic.
664 These syndrome will be all ZERO when there is no error */
665 for (i = 0; i < 6; i++)
666 syndrome[i] = ReadDOC(docptr, Mplus_ECCSyndrome0 + i);
668 nb_errors = doc_decode_ecc(buf, syndrome);
670 printk("ECC Errors corrected: %x\n", nb_errors);
673 /* We return error, but have actually done the
674 read. Not that this can be told to user-space, via
675 sys_read(), but at least MTD-aware stuff can know
676 about it by checking *retlen */
678 printk("%s(%d): Millennium Plus ECC error (from=0x%x:\n",
679 __FILE__, __LINE__, (int)from);
680 printk(" syndrome= %02x:%02x:%02x:%02x:%02x:"
682 syndrome[0], syndrome[1], syndrome[2],
683 syndrome[3], syndrome[4], syndrome[5]);
684 printk(" eccbuf= %02x:%02x:%02x:%02x:%02x:"
686 eccbuf[0], eccbuf[1], eccbuf[2],
687 eccbuf[3], eccbuf[4], eccbuf[5]);
694 printk("ECC DATA at %lx: %2.2X %2.2X %2.2X %2.2X %2.2X %2.2X\n",
695 (long)from, eccbuf[0], eccbuf[1], eccbuf[2], eccbuf[3],
696 eccbuf[4], eccbuf[5]);
698 /* disable the ECC engine */
699 WriteDOC(DOC_ECC_DIS, docptr , Mplus_ECCConf);
701 /* Disable flash internally */
702 WriteDOC(0, docptr, Mplus_FlashSelect);
707 static int doc_write(struct mtd_info *mtd, loff_t to, size_t len,
708 size_t *retlen, const u_char *buf)
710 int i, before, ret = 0;
714 struct DiskOnChip *this = mtd->priv;
715 void __iomem * docptr = this->virtadr;
716 struct Nand *mychip = &this->chips[to >> (this->chipshift)];
718 /* Don't allow write past end of device */
719 if (to >= this->totlen)
722 /* Don't allow writes which aren't exactly one block (512 bytes) */
723 if ((to & 0x1ff) || (len != 0x200))
726 /* Determine position of OOB flags, before or after data */
727 before = (this->interleave && (to & 0x200));
729 DoC_CheckASIC(docptr);
731 /* Find the chip which is to be used and select it */
732 if (this->curfloor != mychip->floor) {
733 DoC_SelectFloor(docptr, mychip->floor);
734 DoC_SelectChip(docptr, mychip->chip);
735 } else if (this->curchip != mychip->chip) {
736 DoC_SelectChip(docptr, mychip->chip);
738 this->curfloor = mychip->floor;
739 this->curchip = mychip->chip;
741 /* Millennium Plus bus cycle sequence as per figure 2, section 2.4 */
742 WriteDOC(DOC_FLASH_CE, docptr, Mplus_FlashSelect);
744 /* Reset the chip, see Software Requirement 11.4 item 1. */
745 DoC_Command(docptr, NAND_CMD_RESET, 0);
746 DoC_WaitReady(docptr);
748 /* Set device to appropriate plane of flash */
750 WriteDOC(DoC_GetDataOffset(mtd, &fto), docptr, Mplus_FlashCmd);
752 /* On interleaved devices the flags for 2nd half 512 are before data */
753 if (eccbuf && before)
756 /* issue the Serial Data In command to initial the Page Program process */
757 DoC_Command(docptr, NAND_CMD_SEQIN, 0x00);
758 DoC_Address(this, 3, fto, 0x00, 0x00);
760 /* Disable the ECC engine */
761 WriteDOC(DOC_ECC_RESET, docptr, Mplus_ECCConf);
764 /* Write the block status BLOCK_USED (0x5555) */
765 WriteDOC(0x55, docptr, Mil_CDSN_IO);
766 WriteDOC(0x55, docptr, Mil_CDSN_IO);
769 /* init the ECC engine, see Reed-Solomon EDC/ECC 11.1 .*/
770 WriteDOC(DOC_ECC_EN | DOC_ECC_RW, docptr, Mplus_ECCConf);
772 MemWriteDOC(docptr, (unsigned char *) buf, len);
774 /* Write ECC data to flash, the ECC info is generated by
775 the DiskOnChip ECC logic see Reed-Solomon EDC/ECC 11.1 */
776 DoC_Delay(docptr, 3);
778 /* Read the ECC data through the DiskOnChip ECC logic */
779 for (i = 0; i < 6; i++)
780 eccbuf[i] = ReadDOC(docptr, Mplus_ECCSyndrome0 + i);
782 /* disable the ECC engine */
783 WriteDOC(DOC_ECC_DIS, docptr, Mplus_ECCConf);
785 /* Write the ECC data to flash */
786 MemWriteDOC(docptr, eccbuf, 6);
789 /* Write the block status BLOCK_USED (0x5555) */
790 WriteDOC(0x55, docptr, Mil_CDSN_IO+6);
791 WriteDOC(0x55, docptr, Mil_CDSN_IO+7);
795 printk("OOB data at %lx is %2.2X %2.2X %2.2X %2.2X %2.2X %2.2X\n",
796 (long) to, eccbuf[0], eccbuf[1], eccbuf[2], eccbuf[3],
797 eccbuf[4], eccbuf[5]);
800 WriteDOC(0x00, docptr, Mplus_WritePipeTerm);
801 WriteDOC(0x00, docptr, Mplus_WritePipeTerm);
803 /* Commit the Page Program command and wait for ready
804 see Software Requirement 11.4 item 1.*/
805 DoC_Command(docptr, NAND_CMD_PAGEPROG, 0x00);
806 DoC_WaitReady(docptr);
808 /* Read the status of the flash device through CDSN IO register
809 see Software Requirement 11.4 item 5.*/
810 DoC_Command(docptr, NAND_CMD_STATUS, 0);
811 dummy = ReadDOC(docptr, Mplus_ReadPipeInit);
812 dummy = ReadDOC(docptr, Mplus_ReadPipeInit);
813 DoC_Delay(docptr, 2);
814 if ((dummy = ReadDOC(docptr, Mplus_LastDataRead)) & 1) {
815 printk("MTD: Error 0x%x programming at 0x%x\n", dummy, (int)to);
816 /* Error in programming
817 FIXME: implement Bad Block Replacement (in nftl.c ??) */
821 dummy = ReadDOC(docptr, Mplus_LastDataRead);
823 /* Disable flash internally */
824 WriteDOC(0, docptr, Mplus_FlashSelect);
826 /* Let the caller know we completed it */
832 static int doc_read_oob(struct mtd_info *mtd, loff_t ofs,
833 struct mtd_oob_ops *ops)
836 struct DiskOnChip *this = mtd->priv;
837 void __iomem * docptr = this->virtadr;
838 struct Nand *mychip = &this->chips[ofs >> this->chipshift];
839 size_t i, size, got, want;
840 uint8_t *buf = ops->oobbuf;
841 size_t len = ops->len;
843 BUG_ON(ops->mode != MTD_OOB_PLACE);
847 DoC_CheckASIC(docptr);
849 /* Find the chip which is to be used and select it */
850 if (this->curfloor != mychip->floor) {
851 DoC_SelectFloor(docptr, mychip->floor);
852 DoC_SelectChip(docptr, mychip->chip);
853 } else if (this->curchip != mychip->chip) {
854 DoC_SelectChip(docptr, mychip->chip);
856 this->curfloor = mychip->floor;
857 this->curchip = mychip->chip;
859 /* Millennium Plus bus cycle sequence as per figure 2, section 2.4 */
860 WriteDOC((DOC_FLASH_CE | DOC_FLASH_WP), docptr, Mplus_FlashSelect);
862 /* disable the ECC engine */
863 WriteDOC(DOC_ECC_RESET, docptr, Mplus_ECCConf);
864 DoC_WaitReady(docptr);
866 /* Maximum of 16 bytes in the OOB region, so limit read to that */
872 for (i = 0; ((i < 3) && (want > 0)); i++) {
873 /* Figure out which region we are accessing... */
876 if (!this->interleave) {
877 DoC_Command(docptr, NAND_CMD_READOOB, 0);
879 } else if (base < 6) {
880 DoC_Command(docptr, DoC_GetECCOffset(mtd, &fofs), 0);
882 } else if (base < 8) {
883 DoC_Command(docptr, DoC_GetFlagsOffset(mtd, &fofs), 0);
886 DoC_Command(docptr, DoC_GetHdrOffset(mtd, &fofs), 0);
892 /* Issue read command */
893 DoC_Address(this, 3, fofs, 0, 0x00);
894 WriteDOC(0, docptr, Mplus_FlashControl);
895 DoC_WaitReady(docptr);
897 ReadDOC(docptr, Mplus_ReadPipeInit);
898 ReadDOC(docptr, Mplus_ReadPipeInit);
899 MemReadDOC(docptr, &buf[got], size - 2);
900 buf[got + size - 2] = ReadDOC(docptr, Mplus_LastDataRead);
901 buf[got + size - 1] = ReadDOC(docptr, Mplus_LastDataRead);
908 /* Disable flash internally */
909 WriteDOC(0, docptr, Mplus_FlashSelect);
915 static int doc_write_oob(struct mtd_info *mtd, loff_t ofs,
916 struct mtd_oob_ops *ops)
920 struct DiskOnChip *this = mtd->priv;
921 void __iomem * docptr = this->virtadr;
922 struct Nand *mychip = &this->chips[ofs >> this->chipshift];
923 size_t i, size, got, want;
925 uint8_t *buf = ops->oobbuf;
926 size_t len = ops->len;
928 BUG_ON(ops->mode != MTD_OOB_PLACE);
932 DoC_CheckASIC(docptr);
934 /* Find the chip which is to be used and select it */
935 if (this->curfloor != mychip->floor) {
936 DoC_SelectFloor(docptr, mychip->floor);
937 DoC_SelectChip(docptr, mychip->chip);
938 } else if (this->curchip != mychip->chip) {
939 DoC_SelectChip(docptr, mychip->chip);
941 this->curfloor = mychip->floor;
942 this->curchip = mychip->chip;
944 /* Millennium Plus bus cycle sequence as per figure 2, section 2.4 */
945 WriteDOC(DOC_FLASH_CE, docptr, Mplus_FlashSelect);
948 /* Maximum of 16 bytes in the OOB region, so limit write to that */
954 for (i = 0; ((i < 3) && (want > 0)); i++) {
955 /* Reset the chip, see Software Requirement 11.4 item 1. */
956 DoC_Command(docptr, NAND_CMD_RESET, 0);
957 DoC_WaitReady(docptr);
959 /* Figure out which region we are accessing... */
962 if (!this->interleave) {
963 WriteDOC(NAND_CMD_READOOB, docptr, Mplus_FlashCmd);
965 } else if (base < 6) {
966 WriteDOC(DoC_GetECCOffset(mtd, &fofs), docptr, Mplus_FlashCmd);
968 } else if (base < 8) {
969 WriteDOC(DoC_GetFlagsOffset(mtd, &fofs), docptr, Mplus_FlashCmd);
972 WriteDOC(DoC_GetHdrOffset(mtd, &fofs), docptr, Mplus_FlashCmd);
978 /* Issue the Serial Data In command to initial the Page Program process */
979 DoC_Command(docptr, NAND_CMD_SEQIN, 0x00);
980 DoC_Address(this, 3, fofs, 0, 0x00);
982 /* Disable the ECC engine */
983 WriteDOC(DOC_ECC_RESET, docptr, Mplus_ECCConf);
985 /* Write the data via the internal pipeline through CDSN IO
986 register, see Pipelined Write Operations 11.2 */
987 MemWriteDOC(docptr, (unsigned char *) &buf[got], size);
988 WriteDOC(0x00, docptr, Mplus_WritePipeTerm);
989 WriteDOC(0x00, docptr, Mplus_WritePipeTerm);
991 /* Commit the Page Program command and wait for ready
992 see Software Requirement 11.4 item 1.*/
993 DoC_Command(docptr, NAND_CMD_PAGEPROG, 0x00);
994 DoC_WaitReady(docptr);
996 /* Read the status of the flash device through CDSN IO register
997 see Software Requirement 11.4 item 5.*/
998 DoC_Command(docptr, NAND_CMD_STATUS, 0x00);
999 dummy = ReadDOC(docptr, Mplus_ReadPipeInit);
1000 dummy = ReadDOC(docptr, Mplus_ReadPipeInit);
1001 DoC_Delay(docptr, 2);
1002 if ((dummy = ReadDOC(docptr, Mplus_LastDataRead)) & 1) {
1003 printk("MTD: Error 0x%x programming oob at 0x%x\n",
1005 /* FIXME: implement Bad Block Replacement */
1009 dummy = ReadDOC(docptr, Mplus_LastDataRead);
1016 /* Disable flash internally */
1017 WriteDOC(0, docptr, Mplus_FlashSelect);
1023 int doc_erase(struct mtd_info *mtd, struct erase_info *instr)
1025 volatile char dummy;
1026 struct DiskOnChip *this = mtd->priv;
1027 __u32 ofs = instr->addr;
1028 __u32 len = instr->len;
1029 void __iomem * docptr = this->virtadr;
1030 struct Nand *mychip = &this->chips[ofs >> this->chipshift];
1032 DoC_CheckASIC(docptr);
1034 if (len != mtd->erasesize)
1035 printk(KERN_WARNING "MTD: Erase not right size (%x != %x)n",
1036 len, mtd->erasesize);
1038 /* Find the chip which is to be used and select it */
1039 if (this->curfloor != mychip->floor) {
1040 DoC_SelectFloor(docptr, mychip->floor);
1041 DoC_SelectChip(docptr, mychip->chip);
1042 } else if (this->curchip != mychip->chip) {
1043 DoC_SelectChip(docptr, mychip->chip);
1045 this->curfloor = mychip->floor;
1046 this->curchip = mychip->chip;
1048 instr->state = MTD_ERASE_PENDING;
1050 /* Millennium Plus bus cycle sequence as per figure 2, section 2.4 */
1051 WriteDOC(DOC_FLASH_CE, docptr, Mplus_FlashSelect);
1053 DoC_Command(docptr, NAND_CMD_RESET, 0x00);
1054 DoC_WaitReady(docptr);
1056 DoC_Command(docptr, NAND_CMD_ERASE1, 0);
1057 DoC_Address(this, 2, ofs, 0, 0x00);
1058 DoC_Command(docptr, NAND_CMD_ERASE2, 0);
1059 DoC_WaitReady(docptr);
1060 instr->state = MTD_ERASING;
1062 /* Read the status of the flash device through CDSN IO register
1063 see Software Requirement 11.4 item 5. */
1064 DoC_Command(docptr, NAND_CMD_STATUS, 0);
1065 dummy = ReadDOC(docptr, Mplus_ReadPipeInit);
1066 dummy = ReadDOC(docptr, Mplus_ReadPipeInit);
1067 if ((dummy = ReadDOC(docptr, Mplus_LastDataRead)) & 1) {
1068 printk("MTD: Error 0x%x erasing at 0x%x\n", dummy, ofs);
1069 /* FIXME: implement Bad Block Replacement (in nftl.c ??) */
1070 instr->state = MTD_ERASE_FAILED;
1072 instr->state = MTD_ERASE_DONE;
1074 dummy = ReadDOC(docptr, Mplus_LastDataRead);
1076 /* Disable flash internally */
1077 WriteDOC(0, docptr, Mplus_FlashSelect);
1079 mtd_erase_callback(instr);
1084 /****************************************************************************
1088 ****************************************************************************/
1090 static void __exit cleanup_doc2001plus(void)
1092 struct mtd_info *mtd;
1093 struct DiskOnChip *this;
1095 while ((mtd=docmilpluslist)) {
1097 docmilpluslist = this->nextdoc;
1099 del_mtd_device(mtd);
1101 iounmap(this->virtadr);
1107 module_exit(cleanup_doc2001plus);
1109 MODULE_LICENSE("GPL");
1110 MODULE_AUTHOR("Greg Ungerer <gerg@snapgear.com> et al.");
1111 MODULE_DESCRIPTION("Driver for DiskOnChip Millennium Plus");