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/delay.h>
21 #include <linux/slab.h>
22 #include <linux/init.h>
23 #include <linux/types.h>
24 #include <linux/bitops.h>
26 #include <linux/mtd/mtd.h>
27 #include <linux/mtd/nand.h>
28 #include <linux/mtd/doc2000.h>
30 /* #define ECC_DEBUG */
32 /* I have no idea why some DoC chips can not use memcop_form|to_io().
33 * This may be due to the different revisions of the ASIC controller built-in or
34 * simplily a QA/Bug issue. Who knows ?? If you have trouble, please uncomment
38 static int doc_read(struct mtd_info *mtd, loff_t from, size_t len,
39 size_t *retlen, u_char *buf);
40 static int doc_write(struct mtd_info *mtd, loff_t to, size_t len,
41 size_t *retlen, const u_char *buf);
42 static int doc_read_oob(struct mtd_info *mtd, loff_t ofs,
43 struct mtd_oob_ops *ops);
44 static int doc_write_oob(struct mtd_info *mtd, loff_t ofs,
45 struct mtd_oob_ops *ops);
46 static int doc_erase (struct mtd_info *mtd, struct erase_info *instr);
48 static struct mtd_info *docmilpluslist = NULL;
51 /* Perform the required delay cycles by writing to the NOP register */
52 static void DoC_Delay(void __iomem * docptr, int cycles)
56 for (i = 0; (i < cycles); i++)
57 WriteDOC(0, docptr, Mplus_NOP);
60 #define CDSN_CTRL_FR_B_MASK (CDSN_CTRL_FR_B0 | CDSN_CTRL_FR_B1)
62 /* DOC_WaitReady: Wait for RDY line to be asserted by the flash chip */
63 static int _DoC_WaitReady(void __iomem * docptr)
65 unsigned int c = 0xffff;
67 DEBUG(MTD_DEBUG_LEVEL3,
68 "_DoC_WaitReady called for out-of-line wait\n");
70 /* Out-of-line routine to wait for chip response */
71 while (((ReadDOC(docptr, Mplus_FlashControl) & CDSN_CTRL_FR_B_MASK) != CDSN_CTRL_FR_B_MASK) && --c)
75 DEBUG(MTD_DEBUG_LEVEL2, "_DoC_WaitReady timed out.\n");
80 static inline int DoC_WaitReady(void __iomem * docptr)
82 /* This is inline, to optimise the common case, where it's ready instantly */
85 /* read form NOP register should be issued prior to the read from CDSNControl
86 see Software Requirement 11.4 item 2. */
89 if ((ReadDOC(docptr, Mplus_FlashControl) & CDSN_CTRL_FR_B_MASK) != CDSN_CTRL_FR_B_MASK)
90 /* Call the out-of-line routine to wait */
91 ret = _DoC_WaitReady(docptr);
96 /* For some reason the Millennium Plus seems to occassionally put itself
97 * into reset mode. For me this happens randomly, with no pattern that I
98 * can detect. M-systems suggest always check this on any block level
99 * operation and setting to normal mode if in reset mode.
101 static inline void DoC_CheckASIC(void __iomem * docptr)
103 /* Make sure the DoC is in normal mode */
104 if ((ReadDOC(docptr, Mplus_DOCControl) & DOC_MODE_NORMAL) == 0) {
105 WriteDOC((DOC_MODE_NORMAL | DOC_MODE_MDWREN), docptr, Mplus_DOCControl);
106 WriteDOC(~(DOC_MODE_NORMAL | DOC_MODE_MDWREN), docptr, Mplus_CtrlConfirm);
110 /* DoC_Command: Send a flash command to the flash chip through the Flash
111 * command register. Need 2 Write Pipeline Terminates to complete send.
113 static void DoC_Command(void __iomem * docptr, unsigned char command,
114 unsigned char xtraflags)
116 WriteDOC(command, docptr, Mplus_FlashCmd);
117 WriteDOC(command, docptr, Mplus_WritePipeTerm);
118 WriteDOC(command, docptr, Mplus_WritePipeTerm);
121 /* DoC_Address: Set the current address for the flash chip through the Flash
122 * Address register. Need 2 Write Pipeline Terminates to complete send.
124 static inline void DoC_Address(struct DiskOnChip *doc, int numbytes,
125 unsigned long ofs, unsigned char xtraflags1,
126 unsigned char xtraflags2)
128 void __iomem * docptr = doc->virtadr;
130 /* Allow for possible Mill Plus internal flash interleaving */
131 ofs >>= doc->interleave;
135 /* Send single byte, bits 0-7. */
136 WriteDOC(ofs & 0xff, docptr, Mplus_FlashAddress);
139 /* Send bits 9-16 followed by 17-23 */
140 WriteDOC((ofs >> 9) & 0xff, docptr, Mplus_FlashAddress);
141 WriteDOC((ofs >> 17) & 0xff, docptr, Mplus_FlashAddress);
144 /* Send 0-7, 9-16, then 17-23 */
145 WriteDOC(ofs & 0xff, docptr, Mplus_FlashAddress);
146 WriteDOC((ofs >> 9) & 0xff, docptr, Mplus_FlashAddress);
147 WriteDOC((ofs >> 17) & 0xff, docptr, Mplus_FlashAddress);
153 WriteDOC(0x00, docptr, Mplus_WritePipeTerm);
154 WriteDOC(0x00, docptr, Mplus_WritePipeTerm);
157 /* DoC_SelectChip: Select a given flash chip within the current floor */
158 static int DoC_SelectChip(void __iomem * docptr, int chip)
160 /* No choice for flash chip on Millennium Plus */
164 /* DoC_SelectFloor: Select a given floor (bank of flash chips) */
165 static int DoC_SelectFloor(void __iomem * docptr, int floor)
167 WriteDOC((floor & 0x3), docptr, Mplus_DeviceSelect);
172 * Translate the given offset into the appropriate command and offset.
173 * This does the mapping using the 16bit interleave layout defined by
174 * M-Systems, and looks like this for a sector pair:
175 * +-----------+-------+-------+-------+--------------+---------+-----------+
176 * | 0 --- 511 |512-517|518-519|520-521| 522 --- 1033 |1034-1039|1040 - 1055|
177 * +-----------+-------+-------+-------+--------------+---------+-----------+
178 * | Data 0 | ECC 0 |Flags0 |Flags1 | Data 1 |ECC 1 | OOB 1 + 2 |
179 * +-----------+-------+-------+-------+--------------+---------+-----------+
181 /* FIXME: This lives in INFTL not here. Other users of flash devices
183 static unsigned int DoC_GetDataOffset(struct mtd_info *mtd, loff_t *from)
185 struct DiskOnChip *this = mtd->priv;
187 if (this->interleave) {
188 unsigned int ofs = *from & 0x3ff;
192 cmd = NAND_CMD_READ0;
194 } else if (ofs < 1014) {
195 cmd = NAND_CMD_READ1;
196 ofs = (ofs & 0x1ff) + 10;
198 cmd = NAND_CMD_READOOB;
202 *from = (*from & ~0x3ff) | ofs;
207 return NAND_CMD_READ1;
208 return NAND_CMD_READ0;
212 static unsigned int DoC_GetECCOffset(struct mtd_info *mtd, loff_t *from)
214 unsigned int ofs, cmd;
217 cmd = NAND_CMD_READOOB;
218 ofs = 10 + (*from & 0xf);
220 cmd = NAND_CMD_READ1;
224 *from = (*from & ~0x3ff) | ofs;
228 static unsigned int DoC_GetFlagsOffset(struct mtd_info *mtd, loff_t *from)
230 unsigned int ofs, cmd;
232 cmd = NAND_CMD_READ1;
233 ofs = (*from & 0x200) ? 8 : 6;
234 *from = (*from & ~0x3ff) | ofs;
238 static unsigned int DoC_GetHdrOffset(struct mtd_info *mtd, loff_t *from)
240 unsigned int ofs, cmd;
242 cmd = NAND_CMD_READOOB;
243 ofs = (*from & 0x200) ? 24 : 16;
244 *from = (*from & ~0x3ff) | ofs;
248 static inline void MemReadDOC(void __iomem * docptr, unsigned char *buf, int len)
252 for (i = 0; i < len; i++)
253 buf[i] = ReadDOC(docptr, Mil_CDSN_IO + i);
255 memcpy_fromio(buf, docptr + DoC_Mil_CDSN_IO, len);
259 static inline void MemWriteDOC(void __iomem * docptr, unsigned char *buf, int len)
263 for (i = 0; i < len; i++)
264 WriteDOC(buf[i], docptr, Mil_CDSN_IO + i);
266 memcpy_toio(docptr + DoC_Mil_CDSN_IO, buf, len);
270 /* DoC_IdentChip: Identify a given NAND chip given {floor,chip} */
271 static int DoC_IdentChip(struct DiskOnChip *doc, int floor, int chip)
275 void __iomem * docptr = doc->virtadr;
277 /* Page in the required floor/chip */
278 DoC_SelectFloor(docptr, floor);
279 DoC_SelectChip(docptr, chip);
281 /* Millennium Plus bus cycle sequence as per figure 2, section 2.4 */
282 WriteDOC((DOC_FLASH_CE | DOC_FLASH_WP), docptr, Mplus_FlashSelect);
284 /* Reset the chip, see Software Requirement 11.4 item 1. */
285 DoC_Command(docptr, NAND_CMD_RESET, 0);
286 DoC_WaitReady(docptr);
288 /* Read the NAND chip ID: 1. Send ReadID command */
289 DoC_Command(docptr, NAND_CMD_READID, 0);
291 /* Read the NAND chip ID: 2. Send address byte zero */
292 DoC_Address(doc, 1, 0x00, 0, 0x00);
294 WriteDOC(0, docptr, Mplus_FlashControl);
295 DoC_WaitReady(docptr);
297 /* Read the manufacturer and device id codes of the flash device through
298 CDSN IO register see Software Requirement 11.4 item 5.*/
299 dummy = ReadDOC(docptr, Mplus_ReadPipeInit);
300 dummy = ReadDOC(docptr, Mplus_ReadPipeInit);
302 mfr = ReadDOC(docptr, Mil_CDSN_IO);
304 dummy = ReadDOC(docptr, Mil_CDSN_IO); /* 2 way interleave */
306 id = ReadDOC(docptr, Mil_CDSN_IO);
308 dummy = ReadDOC(docptr, Mil_CDSN_IO); /* 2 way interleave */
310 dummy = ReadDOC(docptr, Mplus_LastDataRead);
311 dummy = ReadDOC(docptr, Mplus_LastDataRead);
313 /* Disable flash internally */
314 WriteDOC(0, docptr, Mplus_FlashSelect);
316 /* No response - return failure */
317 if (mfr == 0xff || mfr == 0)
320 for (i = 0; nand_flash_ids[i].name != NULL; i++) {
321 if (id == nand_flash_ids[i].id) {
322 /* Try to identify manufacturer */
323 for (j = 0; nand_manuf_ids[j].id != 0x0; j++) {
324 if (nand_manuf_ids[j].id == mfr)
327 printk(KERN_INFO "Flash chip found: Manufacturer ID: %2.2X, "
328 "Chip ID: %2.2X (%s:%s)\n", mfr, id,
329 nand_manuf_ids[j].name, nand_flash_ids[i].name);
332 doc->chipshift = ffs((nand_flash_ids[i].chipsize << 20)) - 1;
333 doc->erasesize = nand_flash_ids[i].erasesize << doc->interleave;
338 if (nand_flash_ids[i].name == NULL)
343 /* DoC_ScanChips: Find all NAND chips present in a DiskOnChip, and identify them */
344 static void DoC_ScanChips(struct DiskOnChip *this)
347 int numchips[MAX_FLOORS_MPLUS];
354 /* Work out the intended interleave setting */
355 this->interleave = 0;
356 if (this->ChipID == DOC_ChipID_DocMilPlus32)
357 this->interleave = 1;
359 /* Check the ASIC agrees */
360 if ( (this->interleave << 2) !=
361 (ReadDOC(this->virtadr, Mplus_Configuration) & 4)) {
362 u_char conf = ReadDOC(this->virtadr, Mplus_Configuration);
363 printk(KERN_NOTICE "Setting DiskOnChip Millennium Plus interleave to %s\n",
364 this->interleave?"on (16-bit)":"off (8-bit)");
366 WriteDOC(conf, this->virtadr, Mplus_Configuration);
369 /* For each floor, find the number of valid chips it contains */
370 for (floor = 0,ret = 1; floor < MAX_FLOORS_MPLUS; floor++) {
372 for (chip = 0; chip < MAX_CHIPS_MPLUS && ret != 0; chip++) {
373 ret = DoC_IdentChip(this, floor, chip);
380 /* If there are none at all that we recognise, bail */
381 if (!this->numchips) {
382 printk("No flash chips recognised.\n");
386 /* Allocate an array to hold the information for each chip */
387 this->chips = kmalloc(sizeof(struct Nand) * this->numchips, GFP_KERNEL);
389 printk("MTD: No memory for allocating chip info structures\n");
393 /* Fill out the chip array with {floor, chipno} for each
394 * detected chip in the device. */
395 for (floor = 0, ret = 0; floor < MAX_FLOORS_MPLUS; floor++) {
396 for (chip = 0 ; chip < numchips[floor] ; chip++) {
397 this->chips[ret].floor = floor;
398 this->chips[ret].chip = chip;
399 this->chips[ret].curadr = 0;
400 this->chips[ret].curmode = 0x50;
405 /* Calculate and print the total size of the device */
406 this->totlen = this->numchips * (1 << this->chipshift);
407 printk(KERN_INFO "%d flash chips found. Total DiskOnChip size: %ld MiB\n",
408 this->numchips ,this->totlen >> 20);
411 static int DoCMilPlus_is_alias(struct DiskOnChip *doc1, struct DiskOnChip *doc2)
413 int tmp1, tmp2, retval;
415 if (doc1->physadr == doc2->physadr)
418 /* Use the alias resolution register which was set aside for this
419 * purpose. If it's value is the same on both chips, they might
420 * be the same chip, and we write to one and check for a change in
421 * the other. It's unclear if this register is usuable in the
422 * DoC 2000 (it's in the Millennium docs), but it seems to work. */
423 tmp1 = ReadDOC(doc1->virtadr, Mplus_AliasResolution);
424 tmp2 = ReadDOC(doc2->virtadr, Mplus_AliasResolution);
428 WriteDOC((tmp1+1) % 0xff, doc1->virtadr, Mplus_AliasResolution);
429 tmp2 = ReadDOC(doc2->virtadr, Mplus_AliasResolution);
430 if (tmp2 == (tmp1+1) % 0xff)
435 /* Restore register contents. May not be necessary, but do it just to
437 WriteDOC(tmp1, doc1->virtadr, Mplus_AliasResolution);
442 /* This routine is found from the docprobe code by symbol_get(),
443 * which will bump the use count of this module. */
444 void DoCMilPlus_init(struct mtd_info *mtd)
446 struct DiskOnChip *this = mtd->priv;
447 struct DiskOnChip *old = NULL;
449 /* We must avoid being called twice for the same device. */
451 old = docmilpluslist->priv;
454 if (DoCMilPlus_is_alias(this, old)) {
455 printk(KERN_NOTICE "Ignoring DiskOnChip Millennium "
456 "Plus at 0x%lX - already configured\n",
458 iounmap(this->virtadr);
463 old = old->nextdoc->priv;
468 mtd->name = "DiskOnChip Millennium Plus";
469 printk(KERN_NOTICE "DiskOnChip Millennium Plus found at "
470 "address 0x%lX\n", this->physadr);
472 mtd->type = MTD_NANDFLASH;
473 mtd->flags = MTD_CAP_NANDFLASH;
477 mtd->writesize = 512;
479 mtd->owner = THIS_MODULE;
480 mtd->erase = doc_erase;
483 mtd->read = doc_read;
484 mtd->write = doc_write;
485 mtd->read_oob = doc_read_oob;
486 mtd->write_oob = doc_write_oob;
494 /* Ident all the chips present. */
499 iounmap(this->virtadr);
501 this->nextdoc = docmilpluslist;
502 docmilpluslist = mtd;
503 mtd->size = this->totlen;
504 mtd->erasesize = this->erasesize;
509 EXPORT_SYMBOL_GPL(DoCMilPlus_init);
512 static int doc_dumpblk(struct mtd_info *mtd, loff_t from)
516 struct DiskOnChip *this = mtd->priv;
517 void __iomem * docptr = this->virtadr;
518 struct Nand *mychip = &this->chips[from >> (this->chipshift)];
519 unsigned char *bp, buf[1056];
524 /* Don't allow read past end of device */
525 if (from >= this->totlen)
528 DoC_CheckASIC(docptr);
530 /* Find the chip which is to be used and select it */
531 if (this->curfloor != mychip->floor) {
532 DoC_SelectFloor(docptr, mychip->floor);
533 DoC_SelectChip(docptr, mychip->chip);
534 } else if (this->curchip != mychip->chip) {
535 DoC_SelectChip(docptr, mychip->chip);
537 this->curfloor = mychip->floor;
538 this->curchip = mychip->chip;
540 /* Millennium Plus bus cycle sequence as per figure 2, section 2.4 */
541 WriteDOC((DOC_FLASH_CE | DOC_FLASH_WP), docptr, Mplus_FlashSelect);
543 /* Reset the chip, see Software Requirement 11.4 item 1. */
544 DoC_Command(docptr, NAND_CMD_RESET, 0);
545 DoC_WaitReady(docptr);
548 DoC_Command(docptr, DoC_GetDataOffset(mtd, &fofs), 0);
549 DoC_Address(this, 3, fofs, 0, 0x00);
550 WriteDOC(0, docptr, Mplus_FlashControl);
551 DoC_WaitReady(docptr);
553 /* disable the ECC engine */
554 WriteDOC(DOC_ECC_RESET, docptr, Mplus_ECCConf);
556 ReadDOC(docptr, Mplus_ReadPipeInit);
557 ReadDOC(docptr, Mplus_ReadPipeInit);
559 /* Read the data via the internal pipeline through CDSN IO
560 register, see Pipelined Read Operations 11.3 */
561 MemReadDOC(docptr, buf, 1054);
562 buf[1054] = ReadDOC(docptr, Mplus_LastDataRead);
563 buf[1055] = ReadDOC(docptr, Mplus_LastDataRead);
565 memset(&c[0], 0, sizeof(c));
566 printk("DUMP OFFSET=%x:\n", (int)from);
568 for (i = 0, bp = &buf[0]; (i < 1056); i++) {
571 printk(" %02x", *bp);
572 c[(i & 0xf)] = ((*bp >= 0x20) && (*bp <= 0x7f)) ? *bp : '.';
574 if (((i + 1) % 16) == 0)
579 /* Disable flash internally */
580 WriteDOC(0, docptr, Mplus_FlashSelect);
586 static int doc_read(struct mtd_info *mtd, loff_t from, size_t len,
587 size_t *retlen, u_char *buf)
592 unsigned char syndrome[6], eccbuf[6];
593 struct DiskOnChip *this = mtd->priv;
594 void __iomem * docptr = this->virtadr;
595 struct Nand *mychip = &this->chips[from >> (this->chipshift)];
597 /* Don't allow read past end of device */
598 if (from >= this->totlen)
601 /* Don't allow a single read to cross a 512-byte block boundary */
602 if (from + len > ((from | 0x1ff) + 1))
603 len = ((from | 0x1ff) + 1) - from;
605 DoC_CheckASIC(docptr);
607 /* Find the chip which is to be used and select it */
608 if (this->curfloor != mychip->floor) {
609 DoC_SelectFloor(docptr, mychip->floor);
610 DoC_SelectChip(docptr, mychip->chip);
611 } else if (this->curchip != mychip->chip) {
612 DoC_SelectChip(docptr, mychip->chip);
614 this->curfloor = mychip->floor;
615 this->curchip = mychip->chip;
617 /* Millennium Plus bus cycle sequence as per figure 2, section 2.4 */
618 WriteDOC((DOC_FLASH_CE | DOC_FLASH_WP), docptr, Mplus_FlashSelect);
620 /* Reset the chip, see Software Requirement 11.4 item 1. */
621 DoC_Command(docptr, NAND_CMD_RESET, 0);
622 DoC_WaitReady(docptr);
625 DoC_Command(docptr, DoC_GetDataOffset(mtd, &fofs), 0);
626 DoC_Address(this, 3, fofs, 0, 0x00);
627 WriteDOC(0, docptr, Mplus_FlashControl);
628 DoC_WaitReady(docptr);
630 /* init the ECC engine, see Reed-Solomon EDC/ECC 11.1 .*/
631 WriteDOC(DOC_ECC_RESET, docptr, Mplus_ECCConf);
632 WriteDOC(DOC_ECC_EN, docptr, Mplus_ECCConf);
634 /* Let the caller know we completed it */
638 ReadDOC(docptr, Mplus_ReadPipeInit);
639 ReadDOC(docptr, Mplus_ReadPipeInit);
641 /* Read the data via the internal pipeline through CDSN IO
642 register, see Pipelined Read Operations 11.3 */
643 MemReadDOC(docptr, buf, len);
645 /* Read the ECC data following raw data */
646 MemReadDOC(docptr, eccbuf, 4);
647 eccbuf[4] = ReadDOC(docptr, Mplus_LastDataRead);
648 eccbuf[5] = ReadDOC(docptr, Mplus_LastDataRead);
650 /* Flush the pipeline */
651 dummy = ReadDOC(docptr, Mplus_ECCConf);
652 dummy = ReadDOC(docptr, Mplus_ECCConf);
654 /* Check the ECC Status */
655 if (ReadDOC(docptr, Mplus_ECCConf) & 0x80) {
657 /* There was an ECC error */
659 printk("DiskOnChip ECC Error: Read at %lx\n", (long)from);
661 /* Read the ECC syndrom through the DiskOnChip ECC logic.
662 These syndrome will be all ZERO when there is no error */
663 for (i = 0; i < 6; i++)
664 syndrome[i] = ReadDOC(docptr, Mplus_ECCSyndrome0 + i);
666 nb_errors = doc_decode_ecc(buf, syndrome);
668 printk("ECC Errors corrected: %x\n", nb_errors);
671 /* We return error, but have actually done the
672 read. Not that this can be told to user-space, via
673 sys_read(), but at least MTD-aware stuff can know
674 about it by checking *retlen */
676 printk("%s(%d): Millennium Plus ECC error (from=0x%x:\n",
677 __FILE__, __LINE__, (int)from);
678 printk(" syndrome= %02x:%02x:%02x:%02x:%02x:"
680 syndrome[0], syndrome[1], syndrome[2],
681 syndrome[3], syndrome[4], syndrome[5]);
682 printk(" eccbuf= %02x:%02x:%02x:%02x:%02x:"
684 eccbuf[0], eccbuf[1], eccbuf[2],
685 eccbuf[3], eccbuf[4], eccbuf[5]);
692 printk("ECC DATA at %lx: %2.2X %2.2X %2.2X %2.2X %2.2X %2.2X\n",
693 (long)from, eccbuf[0], eccbuf[1], eccbuf[2], eccbuf[3],
694 eccbuf[4], eccbuf[5]);
696 /* disable the ECC engine */
697 WriteDOC(DOC_ECC_DIS, docptr , Mplus_ECCConf);
699 /* Disable flash internally */
700 WriteDOC(0, docptr, Mplus_FlashSelect);
705 static int doc_write(struct mtd_info *mtd, loff_t to, size_t len,
706 size_t *retlen, const u_char *buf)
708 int i, before, ret = 0;
712 struct DiskOnChip *this = mtd->priv;
713 void __iomem * docptr = this->virtadr;
714 struct Nand *mychip = &this->chips[to >> (this->chipshift)];
716 /* Don't allow write past end of device */
717 if (to >= this->totlen)
720 /* Don't allow writes which aren't exactly one block (512 bytes) */
721 if ((to & 0x1ff) || (len != 0x200))
724 /* Determine position of OOB flags, before or after data */
725 before = (this->interleave && (to & 0x200));
727 DoC_CheckASIC(docptr);
729 /* Find the chip which is to be used and select it */
730 if (this->curfloor != mychip->floor) {
731 DoC_SelectFloor(docptr, mychip->floor);
732 DoC_SelectChip(docptr, mychip->chip);
733 } else if (this->curchip != mychip->chip) {
734 DoC_SelectChip(docptr, mychip->chip);
736 this->curfloor = mychip->floor;
737 this->curchip = mychip->chip;
739 /* Millennium Plus bus cycle sequence as per figure 2, section 2.4 */
740 WriteDOC(DOC_FLASH_CE, docptr, Mplus_FlashSelect);
742 /* Reset the chip, see Software Requirement 11.4 item 1. */
743 DoC_Command(docptr, NAND_CMD_RESET, 0);
744 DoC_WaitReady(docptr);
746 /* Set device to appropriate plane of flash */
748 WriteDOC(DoC_GetDataOffset(mtd, &fto), docptr, Mplus_FlashCmd);
750 /* On interleaved devices the flags for 2nd half 512 are before data */
751 if (eccbuf && before)
754 /* issue the Serial Data In command to initial the Page Program process */
755 DoC_Command(docptr, NAND_CMD_SEQIN, 0x00);
756 DoC_Address(this, 3, fto, 0x00, 0x00);
758 /* Disable the ECC engine */
759 WriteDOC(DOC_ECC_RESET, docptr, Mplus_ECCConf);
762 /* Write the block status BLOCK_USED (0x5555) */
763 WriteDOC(0x55, docptr, Mil_CDSN_IO);
764 WriteDOC(0x55, docptr, Mil_CDSN_IO);
767 /* init the ECC engine, see Reed-Solomon EDC/ECC 11.1 .*/
768 WriteDOC(DOC_ECC_EN | DOC_ECC_RW, docptr, Mplus_ECCConf);
770 MemWriteDOC(docptr, (unsigned char *) buf, len);
772 /* Write ECC data to flash, the ECC info is generated by
773 the DiskOnChip ECC logic see Reed-Solomon EDC/ECC 11.1 */
774 DoC_Delay(docptr, 3);
776 /* Read the ECC data through the DiskOnChip ECC logic */
777 for (i = 0; i < 6; i++)
778 eccbuf[i] = ReadDOC(docptr, Mplus_ECCSyndrome0 + i);
780 /* disable the ECC engine */
781 WriteDOC(DOC_ECC_DIS, docptr, Mplus_ECCConf);
783 /* Write the ECC data to flash */
784 MemWriteDOC(docptr, eccbuf, 6);
787 /* Write the block status BLOCK_USED (0x5555) */
788 WriteDOC(0x55, docptr, Mil_CDSN_IO+6);
789 WriteDOC(0x55, docptr, Mil_CDSN_IO+7);
793 printk("OOB data at %lx is %2.2X %2.2X %2.2X %2.2X %2.2X %2.2X\n",
794 (long) to, eccbuf[0], eccbuf[1], eccbuf[2], eccbuf[3],
795 eccbuf[4], eccbuf[5]);
798 WriteDOC(0x00, docptr, Mplus_WritePipeTerm);
799 WriteDOC(0x00, docptr, Mplus_WritePipeTerm);
801 /* Commit the Page Program command and wait for ready
802 see Software Requirement 11.4 item 1.*/
803 DoC_Command(docptr, NAND_CMD_PAGEPROG, 0x00);
804 DoC_WaitReady(docptr);
806 /* Read the status of the flash device through CDSN IO register
807 see Software Requirement 11.4 item 5.*/
808 DoC_Command(docptr, NAND_CMD_STATUS, 0);
809 dummy = ReadDOC(docptr, Mplus_ReadPipeInit);
810 dummy = ReadDOC(docptr, Mplus_ReadPipeInit);
811 DoC_Delay(docptr, 2);
812 if ((dummy = ReadDOC(docptr, Mplus_LastDataRead)) & 1) {
813 printk("MTD: Error 0x%x programming at 0x%x\n", dummy, (int)to);
814 /* Error in programming
815 FIXME: implement Bad Block Replacement (in nftl.c ??) */
819 dummy = ReadDOC(docptr, Mplus_LastDataRead);
821 /* Disable flash internally */
822 WriteDOC(0, docptr, Mplus_FlashSelect);
824 /* Let the caller know we completed it */
830 static int doc_read_oob(struct mtd_info *mtd, loff_t ofs,
831 struct mtd_oob_ops *ops)
834 struct DiskOnChip *this = mtd->priv;
835 void __iomem * docptr = this->virtadr;
836 struct Nand *mychip = &this->chips[ofs >> this->chipshift];
837 size_t i, size, got, want;
838 uint8_t *buf = ops->oobbuf;
839 size_t len = ops->len;
841 BUG_ON(ops->mode != MTD_OOB_PLACE);
845 DoC_CheckASIC(docptr);
847 /* Find the chip which is to be used and select it */
848 if (this->curfloor != mychip->floor) {
849 DoC_SelectFloor(docptr, mychip->floor);
850 DoC_SelectChip(docptr, mychip->chip);
851 } else if (this->curchip != mychip->chip) {
852 DoC_SelectChip(docptr, mychip->chip);
854 this->curfloor = mychip->floor;
855 this->curchip = mychip->chip;
857 /* Millennium Plus bus cycle sequence as per figure 2, section 2.4 */
858 WriteDOC((DOC_FLASH_CE | DOC_FLASH_WP), docptr, Mplus_FlashSelect);
860 /* disable the ECC engine */
861 WriteDOC(DOC_ECC_RESET, docptr, Mplus_ECCConf);
862 DoC_WaitReady(docptr);
864 /* Maximum of 16 bytes in the OOB region, so limit read to that */
870 for (i = 0; ((i < 3) && (want > 0)); i++) {
871 /* Figure out which region we are accessing... */
874 if (!this->interleave) {
875 DoC_Command(docptr, NAND_CMD_READOOB, 0);
877 } else if (base < 6) {
878 DoC_Command(docptr, DoC_GetECCOffset(mtd, &fofs), 0);
880 } else if (base < 8) {
881 DoC_Command(docptr, DoC_GetFlagsOffset(mtd, &fofs), 0);
884 DoC_Command(docptr, DoC_GetHdrOffset(mtd, &fofs), 0);
890 /* Issue read command */
891 DoC_Address(this, 3, fofs, 0, 0x00);
892 WriteDOC(0, docptr, Mplus_FlashControl);
893 DoC_WaitReady(docptr);
895 ReadDOC(docptr, Mplus_ReadPipeInit);
896 ReadDOC(docptr, Mplus_ReadPipeInit);
897 MemReadDOC(docptr, &buf[got], size - 2);
898 buf[got + size - 2] = ReadDOC(docptr, Mplus_LastDataRead);
899 buf[got + size - 1] = ReadDOC(docptr, Mplus_LastDataRead);
906 /* Disable flash internally */
907 WriteDOC(0, docptr, Mplus_FlashSelect);
913 static int doc_write_oob(struct mtd_info *mtd, loff_t ofs,
914 struct mtd_oob_ops *ops)
918 struct DiskOnChip *this = mtd->priv;
919 void __iomem * docptr = this->virtadr;
920 struct Nand *mychip = &this->chips[ofs >> this->chipshift];
921 size_t i, size, got, want;
923 uint8_t *buf = ops->oobbuf;
924 size_t len = ops->len;
926 BUG_ON(ops->mode != MTD_OOB_PLACE);
930 DoC_CheckASIC(docptr);
932 /* Find the chip which is to be used and select it */
933 if (this->curfloor != mychip->floor) {
934 DoC_SelectFloor(docptr, mychip->floor);
935 DoC_SelectChip(docptr, mychip->chip);
936 } else if (this->curchip != mychip->chip) {
937 DoC_SelectChip(docptr, mychip->chip);
939 this->curfloor = mychip->floor;
940 this->curchip = mychip->chip;
942 /* Millennium Plus bus cycle sequence as per figure 2, section 2.4 */
943 WriteDOC(DOC_FLASH_CE, docptr, Mplus_FlashSelect);
946 /* Maximum of 16 bytes in the OOB region, so limit write to that */
952 for (i = 0; ((i < 3) && (want > 0)); i++) {
953 /* Reset the chip, see Software Requirement 11.4 item 1. */
954 DoC_Command(docptr, NAND_CMD_RESET, 0);
955 DoC_WaitReady(docptr);
957 /* Figure out which region we are accessing... */
960 if (!this->interleave) {
961 WriteDOC(NAND_CMD_READOOB, docptr, Mplus_FlashCmd);
963 } else if (base < 6) {
964 WriteDOC(DoC_GetECCOffset(mtd, &fofs), docptr, Mplus_FlashCmd);
966 } else if (base < 8) {
967 WriteDOC(DoC_GetFlagsOffset(mtd, &fofs), docptr, Mplus_FlashCmd);
970 WriteDOC(DoC_GetHdrOffset(mtd, &fofs), docptr, Mplus_FlashCmd);
976 /* Issue the Serial Data In command to initial the Page Program process */
977 DoC_Command(docptr, NAND_CMD_SEQIN, 0x00);
978 DoC_Address(this, 3, fofs, 0, 0x00);
980 /* Disable the ECC engine */
981 WriteDOC(DOC_ECC_RESET, docptr, Mplus_ECCConf);
983 /* Write the data via the internal pipeline through CDSN IO
984 register, see Pipelined Write Operations 11.2 */
985 MemWriteDOC(docptr, (unsigned char *) &buf[got], size);
986 WriteDOC(0x00, docptr, Mplus_WritePipeTerm);
987 WriteDOC(0x00, docptr, Mplus_WritePipeTerm);
989 /* Commit the Page Program command and wait for ready
990 see Software Requirement 11.4 item 1.*/
991 DoC_Command(docptr, NAND_CMD_PAGEPROG, 0x00);
992 DoC_WaitReady(docptr);
994 /* Read the status of the flash device through CDSN IO register
995 see Software Requirement 11.4 item 5.*/
996 DoC_Command(docptr, NAND_CMD_STATUS, 0x00);
997 dummy = ReadDOC(docptr, Mplus_ReadPipeInit);
998 dummy = ReadDOC(docptr, Mplus_ReadPipeInit);
999 DoC_Delay(docptr, 2);
1000 if ((dummy = ReadDOC(docptr, Mplus_LastDataRead)) & 1) {
1001 printk("MTD: Error 0x%x programming oob at 0x%x\n",
1003 /* FIXME: implement Bad Block Replacement */
1007 dummy = ReadDOC(docptr, Mplus_LastDataRead);
1014 /* Disable flash internally */
1015 WriteDOC(0, docptr, Mplus_FlashSelect);
1021 int doc_erase(struct mtd_info *mtd, struct erase_info *instr)
1023 volatile char dummy;
1024 struct DiskOnChip *this = mtd->priv;
1025 __u32 ofs = instr->addr;
1026 __u32 len = instr->len;
1027 void __iomem * docptr = this->virtadr;
1028 struct Nand *mychip = &this->chips[ofs >> this->chipshift];
1030 DoC_CheckASIC(docptr);
1032 if (len != mtd->erasesize)
1033 printk(KERN_WARNING "MTD: Erase not right size (%x != %x)n",
1034 len, mtd->erasesize);
1036 /* Find the chip which is to be used and select it */
1037 if (this->curfloor != mychip->floor) {
1038 DoC_SelectFloor(docptr, mychip->floor);
1039 DoC_SelectChip(docptr, mychip->chip);
1040 } else if (this->curchip != mychip->chip) {
1041 DoC_SelectChip(docptr, mychip->chip);
1043 this->curfloor = mychip->floor;
1044 this->curchip = mychip->chip;
1046 instr->state = MTD_ERASE_PENDING;
1048 /* Millennium Plus bus cycle sequence as per figure 2, section 2.4 */
1049 WriteDOC(DOC_FLASH_CE, docptr, Mplus_FlashSelect);
1051 DoC_Command(docptr, NAND_CMD_RESET, 0x00);
1052 DoC_WaitReady(docptr);
1054 DoC_Command(docptr, NAND_CMD_ERASE1, 0);
1055 DoC_Address(this, 2, ofs, 0, 0x00);
1056 DoC_Command(docptr, NAND_CMD_ERASE2, 0);
1057 DoC_WaitReady(docptr);
1058 instr->state = MTD_ERASING;
1060 /* Read the status of the flash device through CDSN IO register
1061 see Software Requirement 11.4 item 5. */
1062 DoC_Command(docptr, NAND_CMD_STATUS, 0);
1063 dummy = ReadDOC(docptr, Mplus_ReadPipeInit);
1064 dummy = ReadDOC(docptr, Mplus_ReadPipeInit);
1065 if ((dummy = ReadDOC(docptr, Mplus_LastDataRead)) & 1) {
1066 printk("MTD: Error 0x%x erasing at 0x%x\n", dummy, ofs);
1067 /* FIXME: implement Bad Block Replacement (in nftl.c ??) */
1068 instr->state = MTD_ERASE_FAILED;
1070 instr->state = MTD_ERASE_DONE;
1072 dummy = ReadDOC(docptr, Mplus_LastDataRead);
1074 /* Disable flash internally */
1075 WriteDOC(0, docptr, Mplus_FlashSelect);
1077 mtd_erase_callback(instr);
1082 /****************************************************************************
1086 ****************************************************************************/
1088 static void __exit cleanup_doc2001plus(void)
1090 struct mtd_info *mtd;
1091 struct DiskOnChip *this;
1093 while ((mtd=docmilpluslist)) {
1095 docmilpluslist = this->nextdoc;
1097 del_mtd_device(mtd);
1099 iounmap(this->virtadr);
1105 module_exit(cleanup_doc2001plus);
1107 MODULE_LICENSE("GPL");
1108 MODULE_AUTHOR("Greg Ungerer <gerg@snapgear.com> et al.");
1109 MODULE_DESCRIPTION("Driver for DiskOnChip Millennium Plus");