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/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
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_oob(struct mtd_info *mtd, loff_t ofs,
45 struct mtd_oob_ops *ops);
46 static int doc_write_oob(struct mtd_info *mtd, loff_t ofs,
47 struct mtd_oob_ops *ops);
48 static int doc_erase (struct mtd_info *mtd, struct erase_info *instr);
50 static struct mtd_info *docmilpluslist = NULL;
53 /* Perform the required delay cycles by writing to the NOP register */
54 static void DoC_Delay(void __iomem * docptr, int cycles)
58 for (i = 0; (i < cycles); i++)
59 WriteDOC(0, docptr, Mplus_NOP);
62 #define CDSN_CTRL_FR_B_MASK (CDSN_CTRL_FR_B0 | CDSN_CTRL_FR_B1)
64 /* DOC_WaitReady: Wait for RDY line to be asserted by the flash chip */
65 static int _DoC_WaitReady(void __iomem * docptr)
67 unsigned int c = 0xffff;
69 DEBUG(MTD_DEBUG_LEVEL3,
70 "_DoC_WaitReady called for out-of-line wait\n");
72 /* Out-of-line routine to wait for chip response */
73 while (((ReadDOC(docptr, Mplus_FlashControl) & CDSN_CTRL_FR_B_MASK) != CDSN_CTRL_FR_B_MASK) && --c)
77 DEBUG(MTD_DEBUG_LEVEL2, "_DoC_WaitReady timed out.\n");
82 static inline int DoC_WaitReady(void __iomem * docptr)
84 /* This is inline, to optimise the common case, where it's ready instantly */
87 /* read form NOP register should be issued prior to the read from CDSNControl
88 see Software Requirement 11.4 item 2. */
91 if ((ReadDOC(docptr, Mplus_FlashControl) & CDSN_CTRL_FR_B_MASK) != CDSN_CTRL_FR_B_MASK)
92 /* Call the out-of-line routine to wait */
93 ret = _DoC_WaitReady(docptr);
98 /* For some reason the Millennium Plus seems to occassionally put itself
99 * into reset mode. For me this happens randomly, with no pattern that I
100 * can detect. M-systems suggest always check this on any block level
101 * operation and setting to normal mode if in reset mode.
103 static inline void DoC_CheckASIC(void __iomem * docptr)
105 /* Make sure the DoC is in normal mode */
106 if ((ReadDOC(docptr, Mplus_DOCControl) & DOC_MODE_NORMAL) == 0) {
107 WriteDOC((DOC_MODE_NORMAL | DOC_MODE_MDWREN), docptr, Mplus_DOCControl);
108 WriteDOC(~(DOC_MODE_NORMAL | DOC_MODE_MDWREN), docptr, Mplus_CtrlConfirm);
112 /* DoC_Command: Send a flash command to the flash chip through the Flash
113 * command register. Need 2 Write Pipeline Terminates to complete send.
115 static void DoC_Command(void __iomem * docptr, unsigned char command,
116 unsigned char xtraflags)
118 WriteDOC(command, docptr, Mplus_FlashCmd);
119 WriteDOC(command, docptr, Mplus_WritePipeTerm);
120 WriteDOC(command, docptr, Mplus_WritePipeTerm);
123 /* DoC_Address: Set the current address for the flash chip through the Flash
124 * Address register. Need 2 Write Pipeline Terminates to complete send.
126 static inline void DoC_Address(struct DiskOnChip *doc, int numbytes,
127 unsigned long ofs, unsigned char xtraflags1,
128 unsigned char xtraflags2)
130 void __iomem * docptr = doc->virtadr;
132 /* Allow for possible Mill Plus internal flash interleaving */
133 ofs >>= doc->interleave;
137 /* Send single byte, bits 0-7. */
138 WriteDOC(ofs & 0xff, docptr, Mplus_FlashAddress);
141 /* Send bits 9-16 followed by 17-23 */
142 WriteDOC((ofs >> 9) & 0xff, docptr, Mplus_FlashAddress);
143 WriteDOC((ofs >> 17) & 0xff, docptr, Mplus_FlashAddress);
146 /* Send 0-7, 9-16, then 17-23 */
147 WriteDOC(ofs & 0xff, docptr, Mplus_FlashAddress);
148 WriteDOC((ofs >> 9) & 0xff, docptr, Mplus_FlashAddress);
149 WriteDOC((ofs >> 17) & 0xff, docptr, Mplus_FlashAddress);
155 WriteDOC(0x00, docptr, Mplus_WritePipeTerm);
156 WriteDOC(0x00, docptr, Mplus_WritePipeTerm);
159 /* DoC_SelectChip: Select a given flash chip within the current floor */
160 static int DoC_SelectChip(void __iomem * docptr, int chip)
162 /* No choice for flash chip on Millennium Plus */
166 /* DoC_SelectFloor: Select a given floor (bank of flash chips) */
167 static int DoC_SelectFloor(void __iomem * docptr, int floor)
169 WriteDOC((floor & 0x3), docptr, Mplus_DeviceSelect);
174 * Translate the given offset into the appropriate command and offset.
175 * This does the mapping using the 16bit interleave layout defined by
176 * M-Systems, and looks like this for a sector pair:
177 * +-----------+-------+-------+-------+--------------+---------+-----------+
178 * | 0 --- 511 |512-517|518-519|520-521| 522 --- 1033 |1034-1039|1040 - 1055|
179 * +-----------+-------+-------+-------+--------------+---------+-----------+
180 * | Data 0 | ECC 0 |Flags0 |Flags1 | Data 1 |ECC 1 | OOB 1 + 2 |
181 * +-----------+-------+-------+-------+--------------+---------+-----------+
183 /* FIXME: This lives in INFTL not here. Other users of flash devices
185 static unsigned int DoC_GetDataOffset(struct mtd_info *mtd, loff_t *from)
187 struct DiskOnChip *this = mtd->priv;
189 if (this->interleave) {
190 unsigned int ofs = *from & 0x3ff;
194 cmd = NAND_CMD_READ0;
196 } else if (ofs < 1014) {
197 cmd = NAND_CMD_READ1;
198 ofs = (ofs & 0x1ff) + 10;
200 cmd = NAND_CMD_READOOB;
204 *from = (*from & ~0x3ff) | ofs;
209 return NAND_CMD_READ1;
210 return NAND_CMD_READ0;
214 static unsigned int DoC_GetECCOffset(struct mtd_info *mtd, loff_t *from)
216 unsigned int ofs, cmd;
219 cmd = NAND_CMD_READOOB;
220 ofs = 10 + (*from & 0xf);
222 cmd = NAND_CMD_READ1;
226 *from = (*from & ~0x3ff) | ofs;
230 static unsigned int DoC_GetFlagsOffset(struct mtd_info *mtd, loff_t *from)
232 unsigned int ofs, cmd;
234 cmd = NAND_CMD_READ1;
235 ofs = (*from & 0x200) ? 8 : 6;
236 *from = (*from & ~0x3ff) | ofs;
240 static unsigned int DoC_GetHdrOffset(struct mtd_info *mtd, loff_t *from)
242 unsigned int ofs, cmd;
244 cmd = NAND_CMD_READOOB;
245 ofs = (*from & 0x200) ? 24 : 16;
246 *from = (*from & ~0x3ff) | ofs;
250 static inline void MemReadDOC(void __iomem * docptr, unsigned char *buf, int len)
254 for (i = 0; i < len; i++)
255 buf[i] = ReadDOC(docptr, Mil_CDSN_IO + i);
257 memcpy_fromio(buf, docptr + DoC_Mil_CDSN_IO, len);
261 static inline void MemWriteDOC(void __iomem * docptr, unsigned char *buf, int len)
265 for (i = 0; i < len; i++)
266 WriteDOC(buf[i], docptr, Mil_CDSN_IO + i);
268 memcpy_toio(docptr + DoC_Mil_CDSN_IO, buf, len);
272 /* DoC_IdentChip: Identify a given NAND chip given {floor,chip} */
273 static int DoC_IdentChip(struct DiskOnChip *doc, int floor, int chip)
277 void __iomem * docptr = doc->virtadr;
279 /* Page in the required floor/chip */
280 DoC_SelectFloor(docptr, floor);
281 DoC_SelectChip(docptr, chip);
283 /* Millennium Plus bus cycle sequence as per figure 2, section 2.4 */
284 WriteDOC((DOC_FLASH_CE | DOC_FLASH_WP), docptr, Mplus_FlashSelect);
286 /* Reset the chip, see Software Requirement 11.4 item 1. */
287 DoC_Command(docptr, NAND_CMD_RESET, 0);
288 DoC_WaitReady(docptr);
290 /* Read the NAND chip ID: 1. Send ReadID command */
291 DoC_Command(docptr, NAND_CMD_READID, 0);
293 /* Read the NAND chip ID: 2. Send address byte zero */
294 DoC_Address(doc, 1, 0x00, 0, 0x00);
296 WriteDOC(0, docptr, Mplus_FlashControl);
297 DoC_WaitReady(docptr);
299 /* Read the manufacturer and device id codes of the flash device through
300 CDSN IO register see Software Requirement 11.4 item 5.*/
301 dummy = ReadDOC(docptr, Mplus_ReadPipeInit);
302 dummy = ReadDOC(docptr, Mplus_ReadPipeInit);
304 mfr = ReadDOC(docptr, Mil_CDSN_IO);
306 dummy = ReadDOC(docptr, Mil_CDSN_IO); /* 2 way interleave */
308 id = ReadDOC(docptr, Mil_CDSN_IO);
310 dummy = ReadDOC(docptr, Mil_CDSN_IO); /* 2 way interleave */
312 dummy = ReadDOC(docptr, Mplus_LastDataRead);
313 dummy = ReadDOC(docptr, Mplus_LastDataRead);
315 /* Disable flash internally */
316 WriteDOC(0, docptr, Mplus_FlashSelect);
318 /* No response - return failure */
319 if (mfr == 0xff || mfr == 0)
322 for (i = 0; nand_flash_ids[i].name != NULL; i++) {
323 if (id == nand_flash_ids[i].id) {
324 /* Try to identify manufacturer */
325 for (j = 0; nand_manuf_ids[j].id != 0x0; j++) {
326 if (nand_manuf_ids[j].id == mfr)
329 printk(KERN_INFO "Flash chip found: Manufacturer ID: %2.2X, "
330 "Chip ID: %2.2X (%s:%s)\n", mfr, id,
331 nand_manuf_ids[j].name, nand_flash_ids[i].name);
334 doc->chipshift = ffs((nand_flash_ids[i].chipsize << 20)) - 1;
335 doc->erasesize = nand_flash_ids[i].erasesize << doc->interleave;
340 if (nand_flash_ids[i].name == NULL)
345 /* DoC_ScanChips: Find all NAND chips present in a DiskOnChip, and identify them */
346 static void DoC_ScanChips(struct DiskOnChip *this)
349 int numchips[MAX_FLOORS_MPLUS];
356 /* Work out the intended interleave setting */
357 this->interleave = 0;
358 if (this->ChipID == DOC_ChipID_DocMilPlus32)
359 this->interleave = 1;
361 /* Check the ASIC agrees */
362 if ( (this->interleave << 2) !=
363 (ReadDOC(this->virtadr, Mplus_Configuration) & 4)) {
364 u_char conf = ReadDOC(this->virtadr, Mplus_Configuration);
365 printk(KERN_NOTICE "Setting DiskOnChip Millennium Plus interleave to %s\n",
366 this->interleave?"on (16-bit)":"off (8-bit)");
368 WriteDOC(conf, this->virtadr, Mplus_Configuration);
371 /* For each floor, find the number of valid chips it contains */
372 for (floor = 0,ret = 1; floor < MAX_FLOORS_MPLUS; floor++) {
374 for (chip = 0; chip < MAX_CHIPS_MPLUS && ret != 0; chip++) {
375 ret = DoC_IdentChip(this, floor, chip);
382 /* If there are none at all that we recognise, bail */
383 if (!this->numchips) {
384 printk("No flash chips recognised.\n");
388 /* Allocate an array to hold the information for each chip */
389 this->chips = kmalloc(sizeof(struct Nand) * this->numchips, GFP_KERNEL);
391 printk("MTD: No memory for allocating chip info structures\n");
395 /* Fill out the chip array with {floor, chipno} for each
396 * detected chip in the device. */
397 for (floor = 0, ret = 0; floor < MAX_FLOORS_MPLUS; floor++) {
398 for (chip = 0 ; chip < numchips[floor] ; chip++) {
399 this->chips[ret].floor = floor;
400 this->chips[ret].chip = chip;
401 this->chips[ret].curadr = 0;
402 this->chips[ret].curmode = 0x50;
407 /* Calculate and print the total size of the device */
408 this->totlen = this->numchips * (1 << this->chipshift);
409 printk(KERN_INFO "%d flash chips found. Total DiskOnChip size: %ld MiB\n",
410 this->numchips ,this->totlen >> 20);
413 static int DoCMilPlus_is_alias(struct DiskOnChip *doc1, struct DiskOnChip *doc2)
415 int tmp1, tmp2, retval;
417 if (doc1->physadr == doc2->physadr)
420 /* Use the alias resolution register which was set aside for this
421 * purpose. If it's value is the same on both chips, they might
422 * be the same chip, and we write to one and check for a change in
423 * the other. It's unclear if this register is usuable in the
424 * DoC 2000 (it's in the Millennium docs), but it seems to work. */
425 tmp1 = ReadDOC(doc1->virtadr, Mplus_AliasResolution);
426 tmp2 = ReadDOC(doc2->virtadr, Mplus_AliasResolution);
430 WriteDOC((tmp1+1) % 0xff, doc1->virtadr, Mplus_AliasResolution);
431 tmp2 = ReadDOC(doc2->virtadr, Mplus_AliasResolution);
432 if (tmp2 == (tmp1+1) % 0xff)
437 /* Restore register contents. May not be necessary, but do it just to
439 WriteDOC(tmp1, doc1->virtadr, Mplus_AliasResolution);
444 /* This routine is found from the docprobe code by symbol_get(),
445 * which will bump the use count of this module. */
446 void DoCMilPlus_init(struct mtd_info *mtd)
448 struct DiskOnChip *this = mtd->priv;
449 struct DiskOnChip *old = NULL;
451 /* We must avoid being called twice for the same device. */
453 old = docmilpluslist->priv;
456 if (DoCMilPlus_is_alias(this, old)) {
457 printk(KERN_NOTICE "Ignoring DiskOnChip Millennium "
458 "Plus at 0x%lX - already configured\n",
460 iounmap(this->virtadr);
465 old = old->nextdoc->priv;
470 mtd->name = "DiskOnChip Millennium Plus";
471 printk(KERN_NOTICE "DiskOnChip Millennium Plus found at "
472 "address 0x%lX\n", this->physadr);
474 mtd->type = MTD_NANDFLASH;
475 mtd->flags = MTD_CAP_NANDFLASH;
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");