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>
12 #include <linux/kernel.h>
13 #include <linux/module.h>
14 #include <asm/errno.h>
16 #include <asm/uaccess.h>
17 #include <linux/delay.h>
18 #include <linux/slab.h>
19 #include <linux/init.h>
20 #include <linux/types.h>
21 #include <linux/bitops.h>
23 #include <linux/mtd/mtd.h>
24 #include <linux/mtd/nand.h>
25 #include <linux/mtd/doc2000.h>
27 /* #define ECC_DEBUG */
29 /* I have no idea why some DoC chips can not use memcop_form|to_io().
30 * This may be due to the different revisions of the ASIC controller built-in or
31 * simplily a QA/Bug issue. Who knows ?? If you have trouble, please uncomment
35 static int doc_read(struct mtd_info *mtd, loff_t from, size_t len,
36 size_t *retlen, u_char *buf);
37 static int doc_write(struct mtd_info *mtd, loff_t to, size_t len,
38 size_t *retlen, const u_char *buf);
39 static int doc_read_oob(struct mtd_info *mtd, loff_t ofs,
40 struct mtd_oob_ops *ops);
41 static int doc_write_oob(struct mtd_info *mtd, loff_t ofs,
42 struct mtd_oob_ops *ops);
43 static int doc_erase (struct mtd_info *mtd, struct erase_info *instr);
45 static struct mtd_info *docmilpluslist = NULL;
48 /* Perform the required delay cycles by writing to the NOP register */
49 static void DoC_Delay(void __iomem * docptr, int cycles)
53 for (i = 0; (i < cycles); i++)
54 WriteDOC(0, docptr, Mplus_NOP);
57 #define CDSN_CTRL_FR_B_MASK (CDSN_CTRL_FR_B0 | CDSN_CTRL_FR_B1)
59 /* DOC_WaitReady: Wait for RDY line to be asserted by the flash chip */
60 static int _DoC_WaitReady(void __iomem * docptr)
62 unsigned int c = 0xffff;
64 pr_debug("_DoC_WaitReady called for out-of-line wait\n");
66 /* Out-of-line routine to wait for chip response */
67 while (((ReadDOC(docptr, Mplus_FlashControl) & CDSN_CTRL_FR_B_MASK) != CDSN_CTRL_FR_B_MASK) && --c)
71 pr_debug("_DoC_WaitReady timed out.\n");
76 static inline int DoC_WaitReady(void __iomem * docptr)
78 /* This is inline, to optimise the common case, where it's ready instantly */
81 /* read form NOP register should be issued prior to the read from CDSNControl
82 see Software Requirement 11.4 item 2. */
85 if ((ReadDOC(docptr, Mplus_FlashControl) & CDSN_CTRL_FR_B_MASK) != CDSN_CTRL_FR_B_MASK)
86 /* Call the out-of-line routine to wait */
87 ret = _DoC_WaitReady(docptr);
92 /* For some reason the Millennium Plus seems to occasionally put itself
93 * into reset mode. For me this happens randomly, with no pattern that I
94 * can detect. M-systems suggest always check this on any block level
95 * operation and setting to normal mode if in reset mode.
97 static inline void DoC_CheckASIC(void __iomem * docptr)
99 /* Make sure the DoC is in normal mode */
100 if ((ReadDOC(docptr, Mplus_DOCControl) & DOC_MODE_NORMAL) == 0) {
101 WriteDOC((DOC_MODE_NORMAL | DOC_MODE_MDWREN), docptr, Mplus_DOCControl);
102 WriteDOC(~(DOC_MODE_NORMAL | DOC_MODE_MDWREN), docptr, Mplus_CtrlConfirm);
106 /* DoC_Command: Send a flash command to the flash chip through the Flash
107 * command register. Need 2 Write Pipeline Terminates to complete send.
109 static void DoC_Command(void __iomem * docptr, unsigned char command,
110 unsigned char xtraflags)
112 WriteDOC(command, docptr, Mplus_FlashCmd);
113 WriteDOC(command, docptr, Mplus_WritePipeTerm);
114 WriteDOC(command, docptr, Mplus_WritePipeTerm);
117 /* DoC_Address: Set the current address for the flash chip through the Flash
118 * Address register. Need 2 Write Pipeline Terminates to complete send.
120 static inline void DoC_Address(struct DiskOnChip *doc, int numbytes,
121 unsigned long ofs, unsigned char xtraflags1,
122 unsigned char xtraflags2)
124 void __iomem * docptr = doc->virtadr;
126 /* Allow for possible Mill Plus internal flash interleaving */
127 ofs >>= doc->interleave;
131 /* Send single byte, bits 0-7. */
132 WriteDOC(ofs & 0xff, docptr, Mplus_FlashAddress);
135 /* Send bits 9-16 followed by 17-23 */
136 WriteDOC((ofs >> 9) & 0xff, docptr, Mplus_FlashAddress);
137 WriteDOC((ofs >> 17) & 0xff, docptr, Mplus_FlashAddress);
140 /* Send 0-7, 9-16, then 17-23 */
141 WriteDOC(ofs & 0xff, docptr, Mplus_FlashAddress);
142 WriteDOC((ofs >> 9) & 0xff, docptr, Mplus_FlashAddress);
143 WriteDOC((ofs >> 17) & 0xff, docptr, Mplus_FlashAddress);
149 WriteDOC(0x00, docptr, Mplus_WritePipeTerm);
150 WriteDOC(0x00, docptr, Mplus_WritePipeTerm);
153 /* DoC_SelectChip: Select a given flash chip within the current floor */
154 static int DoC_SelectChip(void __iomem * docptr, int chip)
156 /* No choice for flash chip on Millennium Plus */
160 /* DoC_SelectFloor: Select a given floor (bank of flash chips) */
161 static int DoC_SelectFloor(void __iomem * docptr, int floor)
163 WriteDOC((floor & 0x3), docptr, Mplus_DeviceSelect);
168 * Translate the given offset into the appropriate command and offset.
169 * This does the mapping using the 16bit interleave layout defined by
170 * M-Systems, and looks like this for a sector pair:
171 * +-----------+-------+-------+-------+--------------+---------+-----------+
172 * | 0 --- 511 |512-517|518-519|520-521| 522 --- 1033 |1034-1039|1040 - 1055|
173 * +-----------+-------+-------+-------+--------------+---------+-----------+
174 * | Data 0 | ECC 0 |Flags0 |Flags1 | Data 1 |ECC 1 | OOB 1 + 2 |
175 * +-----------+-------+-------+-------+--------------+---------+-----------+
177 /* FIXME: This lives in INFTL not here. Other users of flash devices
179 static unsigned int DoC_GetDataOffset(struct mtd_info *mtd, loff_t *from)
181 struct DiskOnChip *this = mtd->priv;
183 if (this->interleave) {
184 unsigned int ofs = *from & 0x3ff;
188 cmd = NAND_CMD_READ0;
190 } else if (ofs < 1014) {
191 cmd = NAND_CMD_READ1;
192 ofs = (ofs & 0x1ff) + 10;
194 cmd = NAND_CMD_READOOB;
198 *from = (*from & ~0x3ff) | ofs;
203 return NAND_CMD_READ1;
204 return NAND_CMD_READ0;
208 static unsigned int DoC_GetECCOffset(struct mtd_info *mtd, loff_t *from)
210 unsigned int ofs, cmd;
213 cmd = NAND_CMD_READOOB;
214 ofs = 10 + (*from & 0xf);
216 cmd = NAND_CMD_READ1;
220 *from = (*from & ~0x3ff) | ofs;
224 static unsigned int DoC_GetFlagsOffset(struct mtd_info *mtd, loff_t *from)
226 unsigned int ofs, cmd;
228 cmd = NAND_CMD_READ1;
229 ofs = (*from & 0x200) ? 8 : 6;
230 *from = (*from & ~0x3ff) | ofs;
234 static unsigned int DoC_GetHdrOffset(struct mtd_info *mtd, loff_t *from)
236 unsigned int ofs, cmd;
238 cmd = NAND_CMD_READOOB;
239 ofs = (*from & 0x200) ? 24 : 16;
240 *from = (*from & ~0x3ff) | ofs;
244 static inline void MemReadDOC(void __iomem * docptr, unsigned char *buf, int len)
248 for (i = 0; i < len; i++)
249 buf[i] = ReadDOC(docptr, Mil_CDSN_IO + i);
251 memcpy_fromio(buf, docptr + DoC_Mil_CDSN_IO, len);
255 static inline void MemWriteDOC(void __iomem * docptr, unsigned char *buf, int len)
259 for (i = 0; i < len; i++)
260 WriteDOC(buf[i], docptr, Mil_CDSN_IO + i);
262 memcpy_toio(docptr + DoC_Mil_CDSN_IO, buf, len);
266 /* DoC_IdentChip: Identify a given NAND chip given {floor,chip} */
267 static int DoC_IdentChip(struct DiskOnChip *doc, int floor, int chip)
271 void __iomem * docptr = doc->virtadr;
273 /* Page in the required floor/chip */
274 DoC_SelectFloor(docptr, floor);
275 DoC_SelectChip(docptr, chip);
277 /* Millennium Plus bus cycle sequence as per figure 2, section 2.4 */
278 WriteDOC((DOC_FLASH_CE | DOC_FLASH_WP), docptr, Mplus_FlashSelect);
280 /* Reset the chip, see Software Requirement 11.4 item 1. */
281 DoC_Command(docptr, NAND_CMD_RESET, 0);
282 DoC_WaitReady(docptr);
284 /* Read the NAND chip ID: 1. Send ReadID command */
285 DoC_Command(docptr, NAND_CMD_READID, 0);
287 /* Read the NAND chip ID: 2. Send address byte zero */
288 DoC_Address(doc, 1, 0x00, 0, 0x00);
290 WriteDOC(0, docptr, Mplus_FlashControl);
291 DoC_WaitReady(docptr);
293 /* Read the manufacturer and device id codes of the flash device through
294 CDSN IO register see Software Requirement 11.4 item 5.*/
295 dummy = ReadDOC(docptr, Mplus_ReadPipeInit);
296 dummy = ReadDOC(docptr, Mplus_ReadPipeInit);
298 mfr = ReadDOC(docptr, Mil_CDSN_IO);
300 dummy = ReadDOC(docptr, Mil_CDSN_IO); /* 2 way interleave */
302 id = ReadDOC(docptr, Mil_CDSN_IO);
304 dummy = ReadDOC(docptr, Mil_CDSN_IO); /* 2 way interleave */
306 dummy = ReadDOC(docptr, Mplus_LastDataRead);
307 dummy = ReadDOC(docptr, Mplus_LastDataRead);
309 /* Disable flash internally */
310 WriteDOC(0, docptr, Mplus_FlashSelect);
312 /* No response - return failure */
313 if (mfr == 0xff || mfr == 0)
316 for (i = 0; nand_flash_ids[i].name != NULL; i++) {
317 if (id == nand_flash_ids[i].id) {
318 /* Try to identify manufacturer */
319 for (j = 0; nand_manuf_ids[j].id != 0x0; j++) {
320 if (nand_manuf_ids[j].id == mfr)
323 printk(KERN_INFO "Flash chip found: Manufacturer ID: %2.2X, "
324 "Chip ID: %2.2X (%s:%s)\n", mfr, id,
325 nand_manuf_ids[j].name, nand_flash_ids[i].name);
328 doc->chipshift = ffs((nand_flash_ids[i].chipsize << 20)) - 1;
329 doc->erasesize = nand_flash_ids[i].erasesize << doc->interleave;
334 if (nand_flash_ids[i].name == NULL)
339 /* DoC_ScanChips: Find all NAND chips present in a DiskOnChip, and identify them */
340 static void DoC_ScanChips(struct DiskOnChip *this)
343 int numchips[MAX_FLOORS_MPLUS];
350 /* Work out the intended interleave setting */
351 this->interleave = 0;
352 if (this->ChipID == DOC_ChipID_DocMilPlus32)
353 this->interleave = 1;
355 /* Check the ASIC agrees */
356 if ( (this->interleave << 2) !=
357 (ReadDOC(this->virtadr, Mplus_Configuration) & 4)) {
358 u_char conf = ReadDOC(this->virtadr, Mplus_Configuration);
359 printk(KERN_NOTICE "Setting DiskOnChip Millennium Plus interleave to %s\n",
360 this->interleave?"on (16-bit)":"off (8-bit)");
362 WriteDOC(conf, this->virtadr, Mplus_Configuration);
365 /* For each floor, find the number of valid chips it contains */
366 for (floor = 0,ret = 1; floor < MAX_FLOORS_MPLUS; floor++) {
368 for (chip = 0; chip < MAX_CHIPS_MPLUS && ret != 0; chip++) {
369 ret = DoC_IdentChip(this, floor, chip);
376 /* If there are none at all that we recognise, bail */
377 if (!this->numchips) {
378 printk("No flash chips recognised.\n");
382 /* Allocate an array to hold the information for each chip */
383 this->chips = kmalloc(sizeof(struct Nand) * this->numchips, GFP_KERNEL);
385 printk("MTD: No memory for allocating chip info structures\n");
389 /* Fill out the chip array with {floor, chipno} for each
390 * detected chip in the device. */
391 for (floor = 0, ret = 0; floor < MAX_FLOORS_MPLUS; floor++) {
392 for (chip = 0 ; chip < numchips[floor] ; chip++) {
393 this->chips[ret].floor = floor;
394 this->chips[ret].chip = chip;
395 this->chips[ret].curadr = 0;
396 this->chips[ret].curmode = 0x50;
401 /* Calculate and print the total size of the device */
402 this->totlen = this->numchips * (1 << this->chipshift);
403 printk(KERN_INFO "%d flash chips found. Total DiskOnChip size: %ld MiB\n",
404 this->numchips ,this->totlen >> 20);
407 static int DoCMilPlus_is_alias(struct DiskOnChip *doc1, struct DiskOnChip *doc2)
409 int tmp1, tmp2, retval;
411 if (doc1->physadr == doc2->physadr)
414 /* Use the alias resolution register which was set aside for this
415 * purpose. If it's value is the same on both chips, they might
416 * be the same chip, and we write to one and check for a change in
417 * the other. It's unclear if this register is usuable in the
418 * DoC 2000 (it's in the Millennium docs), but it seems to work. */
419 tmp1 = ReadDOC(doc1->virtadr, Mplus_AliasResolution);
420 tmp2 = ReadDOC(doc2->virtadr, Mplus_AliasResolution);
424 WriteDOC((tmp1+1) % 0xff, doc1->virtadr, Mplus_AliasResolution);
425 tmp2 = ReadDOC(doc2->virtadr, Mplus_AliasResolution);
426 if (tmp2 == (tmp1+1) % 0xff)
431 /* Restore register contents. May not be necessary, but do it just to
433 WriteDOC(tmp1, doc1->virtadr, Mplus_AliasResolution);
438 /* This routine is found from the docprobe code by symbol_get(),
439 * which will bump the use count of this module. */
440 void DoCMilPlus_init(struct mtd_info *mtd)
442 struct DiskOnChip *this = mtd->priv;
443 struct DiskOnChip *old = NULL;
445 /* We must avoid being called twice for the same device. */
447 old = docmilpluslist->priv;
450 if (DoCMilPlus_is_alias(this, old)) {
451 printk(KERN_NOTICE "Ignoring DiskOnChip Millennium "
452 "Plus at 0x%lX - already configured\n",
454 iounmap(this->virtadr);
459 old = old->nextdoc->priv;
464 mtd->name = "DiskOnChip Millennium Plus";
465 printk(KERN_NOTICE "DiskOnChip Millennium Plus found at "
466 "address 0x%lX\n", this->physadr);
468 mtd->type = MTD_NANDFLASH;
469 mtd->flags = MTD_CAP_NANDFLASH;
470 mtd->writebufsize = mtd->writesize = 512;
472 mtd->owner = THIS_MODULE;
473 mtd->_erase = doc_erase;
474 mtd->_read = doc_read;
475 mtd->_write = doc_write;
476 mtd->_read_oob = doc_read_oob;
477 mtd->_write_oob = doc_write_oob;
481 /* Ident all the chips present. */
486 iounmap(this->virtadr);
488 this->nextdoc = docmilpluslist;
489 docmilpluslist = mtd;
490 mtd->size = this->totlen;
491 mtd->erasesize = this->erasesize;
492 mtd_device_register(mtd, NULL, 0);
496 EXPORT_SYMBOL_GPL(DoCMilPlus_init);
499 static int doc_dumpblk(struct mtd_info *mtd, loff_t from)
503 struct DiskOnChip *this = mtd->priv;
504 void __iomem * docptr = this->virtadr;
505 struct Nand *mychip = &this->chips[from >> (this->chipshift)];
506 unsigned char *bp, buf[1056];
511 /* Don't allow read past end of device */
512 if (from >= this->totlen)
515 DoC_CheckASIC(docptr);
517 /* Find the chip which is to be used and select it */
518 if (this->curfloor != mychip->floor) {
519 DoC_SelectFloor(docptr, mychip->floor);
520 DoC_SelectChip(docptr, mychip->chip);
521 } else if (this->curchip != mychip->chip) {
522 DoC_SelectChip(docptr, mychip->chip);
524 this->curfloor = mychip->floor;
525 this->curchip = mychip->chip;
527 /* Millennium Plus bus cycle sequence as per figure 2, section 2.4 */
528 WriteDOC((DOC_FLASH_CE | DOC_FLASH_WP), docptr, Mplus_FlashSelect);
530 /* Reset the chip, see Software Requirement 11.4 item 1. */
531 DoC_Command(docptr, NAND_CMD_RESET, 0);
532 DoC_WaitReady(docptr);
535 DoC_Command(docptr, DoC_GetDataOffset(mtd, &fofs), 0);
536 DoC_Address(this, 3, fofs, 0, 0x00);
537 WriteDOC(0, docptr, Mplus_FlashControl);
538 DoC_WaitReady(docptr);
540 /* disable the ECC engine */
541 WriteDOC(DOC_ECC_RESET, docptr, Mplus_ECCConf);
543 ReadDOC(docptr, Mplus_ReadPipeInit);
544 ReadDOC(docptr, Mplus_ReadPipeInit);
546 /* Read the data via the internal pipeline through CDSN IO
547 register, see Pipelined Read Operations 11.3 */
548 MemReadDOC(docptr, buf, 1054);
549 buf[1054] = ReadDOC(docptr, Mplus_LastDataRead);
550 buf[1055] = ReadDOC(docptr, Mplus_LastDataRead);
552 memset(&c[0], 0, sizeof(c));
553 printk("DUMP OFFSET=%x:\n", (int)from);
555 for (i = 0, bp = &buf[0]; (i < 1056); i++) {
558 printk(" %02x", *bp);
559 c[(i & 0xf)] = ((*bp >= 0x20) && (*bp <= 0x7f)) ? *bp : '.';
561 if (((i + 1) % 16) == 0)
566 /* Disable flash internally */
567 WriteDOC(0, docptr, Mplus_FlashSelect);
573 static int doc_read(struct mtd_info *mtd, loff_t from, size_t len,
574 size_t *retlen, u_char *buf)
579 unsigned char syndrome[6], eccbuf[6];
580 struct DiskOnChip *this = mtd->priv;
581 void __iomem * docptr = this->virtadr;
582 struct Nand *mychip = &this->chips[from >> (this->chipshift)];
584 /* Don't allow a single read to cross a 512-byte block boundary */
585 if (from + len > ((from | 0x1ff) + 1))
586 len = ((from | 0x1ff) + 1) - from;
588 DoC_CheckASIC(docptr);
590 /* Find the chip which is to be used and select it */
591 if (this->curfloor != mychip->floor) {
592 DoC_SelectFloor(docptr, mychip->floor);
593 DoC_SelectChip(docptr, mychip->chip);
594 } else if (this->curchip != mychip->chip) {
595 DoC_SelectChip(docptr, mychip->chip);
597 this->curfloor = mychip->floor;
598 this->curchip = mychip->chip;
600 /* Millennium Plus bus cycle sequence as per figure 2, section 2.4 */
601 WriteDOC((DOC_FLASH_CE | DOC_FLASH_WP), docptr, Mplus_FlashSelect);
603 /* Reset the chip, see Software Requirement 11.4 item 1. */
604 DoC_Command(docptr, NAND_CMD_RESET, 0);
605 DoC_WaitReady(docptr);
608 DoC_Command(docptr, DoC_GetDataOffset(mtd, &fofs), 0);
609 DoC_Address(this, 3, fofs, 0, 0x00);
610 WriteDOC(0, docptr, Mplus_FlashControl);
611 DoC_WaitReady(docptr);
613 /* init the ECC engine, see Reed-Solomon EDC/ECC 11.1 .*/
614 WriteDOC(DOC_ECC_RESET, docptr, Mplus_ECCConf);
615 WriteDOC(DOC_ECC_EN, docptr, Mplus_ECCConf);
617 /* Let the caller know we completed it */
621 ReadDOC(docptr, Mplus_ReadPipeInit);
622 ReadDOC(docptr, Mplus_ReadPipeInit);
624 /* Read the data via the internal pipeline through CDSN IO
625 register, see Pipelined Read Operations 11.3 */
626 MemReadDOC(docptr, buf, len);
628 /* Read the ECC data following raw data */
629 MemReadDOC(docptr, eccbuf, 4);
630 eccbuf[4] = ReadDOC(docptr, Mplus_LastDataRead);
631 eccbuf[5] = ReadDOC(docptr, Mplus_LastDataRead);
633 /* Flush the pipeline */
634 dummy = ReadDOC(docptr, Mplus_ECCConf);
635 dummy = ReadDOC(docptr, Mplus_ECCConf);
637 /* Check the ECC Status */
638 if (ReadDOC(docptr, Mplus_ECCConf) & 0x80) {
640 /* There was an ECC error */
642 printk("DiskOnChip ECC Error: Read at %lx\n", (long)from);
644 /* Read the ECC syndrome through the DiskOnChip ECC logic.
645 These syndrome will be all ZERO when there is no error */
646 for (i = 0; i < 6; i++)
647 syndrome[i] = ReadDOC(docptr, Mplus_ECCSyndrome0 + i);
649 nb_errors = doc_decode_ecc(buf, syndrome);
651 printk("ECC Errors corrected: %x\n", nb_errors);
654 /* We return error, but have actually done the
655 read. Not that this can be told to user-space, via
656 sys_read(), but at least MTD-aware stuff can know
657 about it by checking *retlen */
659 printk("%s(%d): Millennium Plus ECC error (from=0x%x:\n",
660 __FILE__, __LINE__, (int)from);
661 printk(" syndrome= %02x:%02x:%02x:%02x:%02x:"
663 syndrome[0], syndrome[1], syndrome[2],
664 syndrome[3], syndrome[4], syndrome[5]);
665 printk(" eccbuf= %02x:%02x:%02x:%02x:%02x:"
667 eccbuf[0], eccbuf[1], eccbuf[2],
668 eccbuf[3], eccbuf[4], eccbuf[5]);
675 printk("ECC DATA at %lx: %2.2X %2.2X %2.2X %2.2X %2.2X %2.2X\n",
676 (long)from, eccbuf[0], eccbuf[1], eccbuf[2], eccbuf[3],
677 eccbuf[4], eccbuf[5]);
679 /* disable the ECC engine */
680 WriteDOC(DOC_ECC_DIS, docptr , Mplus_ECCConf);
682 /* Disable flash internally */
683 WriteDOC(0, docptr, Mplus_FlashSelect);
688 static int doc_write(struct mtd_info *mtd, loff_t to, size_t len,
689 size_t *retlen, const u_char *buf)
691 int i, before, ret = 0;
695 struct DiskOnChip *this = mtd->priv;
696 void __iomem * docptr = this->virtadr;
697 struct Nand *mychip = &this->chips[to >> (this->chipshift)];
699 /* Don't allow writes which aren't exactly one block (512 bytes) */
700 if ((to & 0x1ff) || (len != 0x200))
703 /* Determine position of OOB flags, before or after data */
704 before = (this->interleave && (to & 0x200));
706 DoC_CheckASIC(docptr);
708 /* Find the chip which is to be used and select it */
709 if (this->curfloor != mychip->floor) {
710 DoC_SelectFloor(docptr, mychip->floor);
711 DoC_SelectChip(docptr, mychip->chip);
712 } else if (this->curchip != mychip->chip) {
713 DoC_SelectChip(docptr, mychip->chip);
715 this->curfloor = mychip->floor;
716 this->curchip = mychip->chip;
718 /* Millennium Plus bus cycle sequence as per figure 2, section 2.4 */
719 WriteDOC(DOC_FLASH_CE, docptr, Mplus_FlashSelect);
721 /* Reset the chip, see Software Requirement 11.4 item 1. */
722 DoC_Command(docptr, NAND_CMD_RESET, 0);
723 DoC_WaitReady(docptr);
725 /* Set device to appropriate plane of flash */
727 WriteDOC(DoC_GetDataOffset(mtd, &fto), docptr, Mplus_FlashCmd);
729 /* On interleaved devices the flags for 2nd half 512 are before data */
733 /* issue the Serial Data In command to initial the Page Program process */
734 DoC_Command(docptr, NAND_CMD_SEQIN, 0x00);
735 DoC_Address(this, 3, fto, 0x00, 0x00);
737 /* Disable the ECC engine */
738 WriteDOC(DOC_ECC_RESET, docptr, Mplus_ECCConf);
741 /* Write the block status BLOCK_USED (0x5555) */
742 WriteDOC(0x55, docptr, Mil_CDSN_IO);
743 WriteDOC(0x55, docptr, Mil_CDSN_IO);
746 /* init the ECC engine, see Reed-Solomon EDC/ECC 11.1 .*/
747 WriteDOC(DOC_ECC_EN | DOC_ECC_RW, docptr, Mplus_ECCConf);
749 MemWriteDOC(docptr, (unsigned char *) buf, len);
751 /* Write ECC data to flash, the ECC info is generated by
752 the DiskOnChip ECC logic see Reed-Solomon EDC/ECC 11.1 */
753 DoC_Delay(docptr, 3);
755 /* Read the ECC data through the DiskOnChip ECC logic */
756 for (i = 0; i < 6; i++)
757 eccbuf[i] = ReadDOC(docptr, Mplus_ECCSyndrome0 + i);
759 /* disable the ECC engine */
760 WriteDOC(DOC_ECC_DIS, docptr, Mplus_ECCConf);
762 /* Write the ECC data to flash */
763 MemWriteDOC(docptr, eccbuf, 6);
766 /* Write the block status BLOCK_USED (0x5555) */
767 WriteDOC(0x55, docptr, Mil_CDSN_IO+6);
768 WriteDOC(0x55, docptr, Mil_CDSN_IO+7);
772 printk("OOB data at %lx is %2.2X %2.2X %2.2X %2.2X %2.2X %2.2X\n",
773 (long) to, eccbuf[0], eccbuf[1], eccbuf[2], eccbuf[3],
774 eccbuf[4], eccbuf[5]);
777 WriteDOC(0x00, docptr, Mplus_WritePipeTerm);
778 WriteDOC(0x00, docptr, Mplus_WritePipeTerm);
780 /* Commit the Page Program command and wait for ready
781 see Software Requirement 11.4 item 1.*/
782 DoC_Command(docptr, NAND_CMD_PAGEPROG, 0x00);
783 DoC_WaitReady(docptr);
785 /* Read the status of the flash device through CDSN IO register
786 see Software Requirement 11.4 item 5.*/
787 DoC_Command(docptr, NAND_CMD_STATUS, 0);
788 dummy = ReadDOC(docptr, Mplus_ReadPipeInit);
789 dummy = ReadDOC(docptr, Mplus_ReadPipeInit);
790 DoC_Delay(docptr, 2);
791 if ((dummy = ReadDOC(docptr, Mplus_LastDataRead)) & 1) {
792 printk("MTD: Error 0x%x programming at 0x%x\n", dummy, (int)to);
793 /* Error in programming
794 FIXME: implement Bad Block Replacement (in nftl.c ??) */
797 dummy = ReadDOC(docptr, Mplus_LastDataRead);
799 /* Disable flash internally */
800 WriteDOC(0, docptr, Mplus_FlashSelect);
802 /* Let the caller know we completed it */
808 static int doc_read_oob(struct mtd_info *mtd, loff_t ofs,
809 struct mtd_oob_ops *ops)
812 struct DiskOnChip *this = mtd->priv;
813 void __iomem * docptr = this->virtadr;
814 struct Nand *mychip = &this->chips[ofs >> this->chipshift];
815 size_t i, size, got, want;
816 uint8_t *buf = ops->oobbuf;
817 size_t len = ops->len;
819 BUG_ON(ops->mode != MTD_OPS_PLACE_OOB);
823 DoC_CheckASIC(docptr);
825 /* Find the chip which is to be used and select it */
826 if (this->curfloor != mychip->floor) {
827 DoC_SelectFloor(docptr, mychip->floor);
828 DoC_SelectChip(docptr, mychip->chip);
829 } else if (this->curchip != mychip->chip) {
830 DoC_SelectChip(docptr, mychip->chip);
832 this->curfloor = mychip->floor;
833 this->curchip = mychip->chip;
835 /* Millennium Plus bus cycle sequence as per figure 2, section 2.4 */
836 WriteDOC((DOC_FLASH_CE | DOC_FLASH_WP), docptr, Mplus_FlashSelect);
838 /* disable the ECC engine */
839 WriteDOC(DOC_ECC_RESET, docptr, Mplus_ECCConf);
840 DoC_WaitReady(docptr);
842 /* Maximum of 16 bytes in the OOB region, so limit read to that */
848 for (i = 0; ((i < 3) && (want > 0)); i++) {
849 /* Figure out which region we are accessing... */
852 if (!this->interleave) {
853 DoC_Command(docptr, NAND_CMD_READOOB, 0);
855 } else if (base < 6) {
856 DoC_Command(docptr, DoC_GetECCOffset(mtd, &fofs), 0);
858 } else if (base < 8) {
859 DoC_Command(docptr, DoC_GetFlagsOffset(mtd, &fofs), 0);
862 DoC_Command(docptr, DoC_GetHdrOffset(mtd, &fofs), 0);
868 /* Issue read command */
869 DoC_Address(this, 3, fofs, 0, 0x00);
870 WriteDOC(0, docptr, Mplus_FlashControl);
871 DoC_WaitReady(docptr);
873 ReadDOC(docptr, Mplus_ReadPipeInit);
874 ReadDOC(docptr, Mplus_ReadPipeInit);
875 MemReadDOC(docptr, &buf[got], size - 2);
876 buf[got + size - 2] = ReadDOC(docptr, Mplus_LastDataRead);
877 buf[got + size - 1] = ReadDOC(docptr, Mplus_LastDataRead);
884 /* Disable flash internally */
885 WriteDOC(0, docptr, Mplus_FlashSelect);
891 static int doc_write_oob(struct mtd_info *mtd, loff_t ofs,
892 struct mtd_oob_ops *ops)
896 struct DiskOnChip *this = mtd->priv;
897 void __iomem * docptr = this->virtadr;
898 struct Nand *mychip = &this->chips[ofs >> this->chipshift];
899 size_t i, size, got, want;
901 uint8_t *buf = ops->oobbuf;
902 size_t len = ops->len;
904 BUG_ON(ops->mode != MTD_OPS_PLACE_OOB);
908 DoC_CheckASIC(docptr);
910 /* Find the chip which is to be used and select it */
911 if (this->curfloor != mychip->floor) {
912 DoC_SelectFloor(docptr, mychip->floor);
913 DoC_SelectChip(docptr, mychip->chip);
914 } else if (this->curchip != mychip->chip) {
915 DoC_SelectChip(docptr, mychip->chip);
917 this->curfloor = mychip->floor;
918 this->curchip = mychip->chip;
920 /* Millennium Plus bus cycle sequence as per figure 2, section 2.4 */
921 WriteDOC(DOC_FLASH_CE, docptr, Mplus_FlashSelect);
924 /* Maximum of 16 bytes in the OOB region, so limit write to that */
930 for (i = 0; ((i < 3) && (want > 0)); i++) {
931 /* Reset the chip, see Software Requirement 11.4 item 1. */
932 DoC_Command(docptr, NAND_CMD_RESET, 0);
933 DoC_WaitReady(docptr);
935 /* Figure out which region we are accessing... */
938 if (!this->interleave) {
939 WriteDOC(NAND_CMD_READOOB, docptr, Mplus_FlashCmd);
941 } else if (base < 6) {
942 WriteDOC(DoC_GetECCOffset(mtd, &fofs), docptr, Mplus_FlashCmd);
944 } else if (base < 8) {
945 WriteDOC(DoC_GetFlagsOffset(mtd, &fofs), docptr, Mplus_FlashCmd);
948 WriteDOC(DoC_GetHdrOffset(mtd, &fofs), docptr, Mplus_FlashCmd);
954 /* Issue the Serial Data In command to initial the Page Program process */
955 DoC_Command(docptr, NAND_CMD_SEQIN, 0x00);
956 DoC_Address(this, 3, fofs, 0, 0x00);
958 /* Disable the ECC engine */
959 WriteDOC(DOC_ECC_RESET, docptr, Mplus_ECCConf);
961 /* Write the data via the internal pipeline through CDSN IO
962 register, see Pipelined Write Operations 11.2 */
963 MemWriteDOC(docptr, (unsigned char *) &buf[got], size);
964 WriteDOC(0x00, docptr, Mplus_WritePipeTerm);
965 WriteDOC(0x00, docptr, Mplus_WritePipeTerm);
967 /* Commit the Page Program command and wait for ready
968 see Software Requirement 11.4 item 1.*/
969 DoC_Command(docptr, NAND_CMD_PAGEPROG, 0x00);
970 DoC_WaitReady(docptr);
972 /* Read the status of the flash device through CDSN IO register
973 see Software Requirement 11.4 item 5.*/
974 DoC_Command(docptr, NAND_CMD_STATUS, 0x00);
975 dummy = ReadDOC(docptr, Mplus_ReadPipeInit);
976 dummy = ReadDOC(docptr, Mplus_ReadPipeInit);
977 DoC_Delay(docptr, 2);
978 if ((dummy = ReadDOC(docptr, Mplus_LastDataRead)) & 1) {
979 printk("MTD: Error 0x%x programming oob at 0x%x\n",
981 /* FIXME: implement Bad Block Replacement */
985 dummy = ReadDOC(docptr, Mplus_LastDataRead);
992 /* Disable flash internally */
993 WriteDOC(0, docptr, Mplus_FlashSelect);
999 int doc_erase(struct mtd_info *mtd, struct erase_info *instr)
1001 volatile char dummy;
1002 struct DiskOnChip *this = mtd->priv;
1003 __u32 ofs = instr->addr;
1004 __u32 len = instr->len;
1005 void __iomem * docptr = this->virtadr;
1006 struct Nand *mychip = &this->chips[ofs >> this->chipshift];
1008 DoC_CheckASIC(docptr);
1010 if (len != mtd->erasesize)
1011 printk(KERN_WARNING "MTD: Erase not right size (%x != %x)n",
1012 len, mtd->erasesize);
1014 /* Find the chip which is to be used and select it */
1015 if (this->curfloor != mychip->floor) {
1016 DoC_SelectFloor(docptr, mychip->floor);
1017 DoC_SelectChip(docptr, mychip->chip);
1018 } else if (this->curchip != mychip->chip) {
1019 DoC_SelectChip(docptr, mychip->chip);
1021 this->curfloor = mychip->floor;
1022 this->curchip = mychip->chip;
1024 instr->state = MTD_ERASE_PENDING;
1026 /* Millennium Plus bus cycle sequence as per figure 2, section 2.4 */
1027 WriteDOC(DOC_FLASH_CE, docptr, Mplus_FlashSelect);
1029 DoC_Command(docptr, NAND_CMD_RESET, 0x00);
1030 DoC_WaitReady(docptr);
1032 DoC_Command(docptr, NAND_CMD_ERASE1, 0);
1033 DoC_Address(this, 2, ofs, 0, 0x00);
1034 DoC_Command(docptr, NAND_CMD_ERASE2, 0);
1035 DoC_WaitReady(docptr);
1036 instr->state = MTD_ERASING;
1038 /* Read the status of the flash device through CDSN IO register
1039 see Software Requirement 11.4 item 5. */
1040 DoC_Command(docptr, NAND_CMD_STATUS, 0);
1041 dummy = ReadDOC(docptr, Mplus_ReadPipeInit);
1042 dummy = ReadDOC(docptr, Mplus_ReadPipeInit);
1043 if ((dummy = ReadDOC(docptr, Mplus_LastDataRead)) & 1) {
1044 printk("MTD: Error 0x%x erasing at 0x%x\n", dummy, ofs);
1045 /* FIXME: implement Bad Block Replacement (in nftl.c ??) */
1046 instr->state = MTD_ERASE_FAILED;
1048 instr->state = MTD_ERASE_DONE;
1050 dummy = ReadDOC(docptr, Mplus_LastDataRead);
1052 /* Disable flash internally */
1053 WriteDOC(0, docptr, Mplus_FlashSelect);
1055 mtd_erase_callback(instr);
1060 /****************************************************************************
1064 ****************************************************************************/
1066 static void __exit cleanup_doc2001plus(void)
1068 struct mtd_info *mtd;
1069 struct DiskOnChip *this;
1071 while ((mtd=docmilpluslist)) {
1073 docmilpluslist = this->nextdoc;
1075 mtd_device_unregister(mtd);
1077 iounmap(this->virtadr);
1083 module_exit(cleanup_doc2001plus);
1085 MODULE_LICENSE("GPL");
1086 MODULE_AUTHOR("Greg Ungerer <gerg@snapgear.com> et al.");
1087 MODULE_DESCRIPTION("Driver for DiskOnChip Millennium Plus");