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.5 2003/06/11 09:45:19 dwmw2 Exp $
12 #include <linux/kernel.h>
13 #include <linux/module.h>
14 #include <asm/errno.h>
16 #include <asm/uaccess.h>
17 #include <linux/miscdevice.h>
18 #include <linux/pci.h>
19 #include <linux/delay.h>
20 #include <linux/slab.h>
21 #include <linux/sched.h>
22 #include <linux/init.h>
23 #include <linux/types.h>
25 #include <linux/mtd/mtd.h>
26 #include <linux/mtd/nand.h>
27 #include <linux/mtd/doc2000.h>
29 /* #define ECC_DEBUG */
31 /* I have no idea why some DoC chips can not use memcop_form|to_io().
32 * This may be due to the different revisions of the ASIC controller built-in or
33 * simplily a QA/Bug issue. Who knows ?? If you have trouble, please uncomment
37 static int doc_read(struct mtd_info *mtd, loff_t from, size_t len,
38 size_t *retlen, u_char *buf);
39 static int doc_write(struct mtd_info *mtd, loff_t to, size_t len,
40 size_t *retlen, const u_char *buf);
41 static int doc_read_ecc(struct mtd_info *mtd, loff_t from, size_t len,
42 size_t *retlen, u_char *buf, u_char *eccbuf,
43 struct nand_oobinfo *oobsel);
44 static int doc_write_ecc(struct mtd_info *mtd, loff_t to, size_t len,
45 size_t *retlen, const u_char *buf, u_char *eccbuf,
46 struct nand_oobinfo *oobsel);
47 static int doc_read_oob(struct mtd_info *mtd, loff_t ofs, size_t len,
48 size_t *retlen, u_char *buf);
49 static int doc_write_oob(struct mtd_info *mtd, loff_t ofs, size_t len,
50 size_t *retlen, const u_char *buf);
51 static int doc_erase (struct mtd_info *mtd, struct erase_info *instr);
53 static struct mtd_info *docmilpluslist = NULL;
56 /* Perform the required delay cycles by writing to the NOP register */
57 static void DoC_Delay(unsigned long docptr, int cycles)
61 for (i = 0; (i < cycles); i++)
62 WriteDOC(0, docptr, Mplus_NOP);
65 #define CDSN_CTRL_FR_B_MASK (CDSN_CTRL_FR_B0 | CDSN_CTRL_FR_B1)
67 /* DOC_WaitReady: Wait for RDY line to be asserted by the flash chip */
68 static int _DoC_WaitReady(unsigned long docptr)
70 unsigned int c = 0xffff;
72 DEBUG(MTD_DEBUG_LEVEL3,
73 "_DoC_WaitReady called for out-of-line wait\n");
75 /* Out-of-line routine to wait for chip response */
76 while (((ReadDOC(docptr, Mplus_FlashControl) & CDSN_CTRL_FR_B_MASK) != CDSN_CTRL_FR_B_MASK) && --c)
80 DEBUG(MTD_DEBUG_LEVEL2, "_DoC_WaitReady timed out.\n");
85 static inline int DoC_WaitReady(unsigned long docptr)
87 /* This is inline, to optimise the common case, where it's ready instantly */
90 /* read form NOP register should be issued prior to the read from CDSNControl
91 see Software Requirement 11.4 item 2. */
94 if ((ReadDOC(docptr, Mplus_FlashControl) & CDSN_CTRL_FR_B_MASK) != CDSN_CTRL_FR_B_MASK)
95 /* Call the out-of-line routine to wait */
96 ret = _DoC_WaitReady(docptr);
101 /* For some reason the Millennium Plus seems to occassionally put itself
102 * into reset mode. For me this happens randomly, with no pattern that I
103 * can detect. M-systems suggest always check this on any block level
104 * operation and setting to normal mode if in reset mode.
106 static inline void DoC_CheckASIC(unsigned long docptr)
108 /* Make sure the DoC is in normal mode */
109 if ((ReadDOC(docptr, Mplus_DOCControl) & DOC_MODE_NORMAL) == 0) {
110 WriteDOC((DOC_MODE_NORMAL | DOC_MODE_MDWREN), docptr, Mplus_DOCControl);
111 WriteDOC(~(DOC_MODE_NORMAL | DOC_MODE_MDWREN), docptr, Mplus_CtrlConfirm);
115 /* DoC_Command: Send a flash command to the flash chip through the Flash
116 * command register. Need 2 Write Pipeline Terminates to complete send.
118 static inline void DoC_Command(unsigned long docptr, unsigned char command,
119 unsigned char xtraflags)
121 WriteDOC(command, docptr, Mplus_FlashCmd);
122 WriteDOC(command, docptr, Mplus_WritePipeTerm);
123 WriteDOC(command, docptr, Mplus_WritePipeTerm);
126 /* DoC_Address: Set the current address for the flash chip through the Flash
127 * Address register. Need 2 Write Pipeline Terminates to complete send.
129 static inline void DoC_Address(struct DiskOnChip *doc, int numbytes,
130 unsigned long ofs, unsigned char xtraflags1,
131 unsigned char xtraflags2)
133 unsigned long docptr = doc->virtadr;
135 /* Allow for possible Mill Plus internal flash interleaving */
136 ofs >>= doc->interleave;
140 /* Send single byte, bits 0-7. */
141 WriteDOC(ofs & 0xff, docptr, Mplus_FlashAddress);
144 /* Send bits 9-16 followed by 17-23 */
145 WriteDOC((ofs >> 9) & 0xff, docptr, Mplus_FlashAddress);
146 WriteDOC((ofs >> 17) & 0xff, docptr, Mplus_FlashAddress);
149 /* Send 0-7, 9-16, then 17-23 */
150 WriteDOC(ofs & 0xff, docptr, Mplus_FlashAddress);
151 WriteDOC((ofs >> 9) & 0xff, docptr, Mplus_FlashAddress);
152 WriteDOC((ofs >> 17) & 0xff, docptr, Mplus_FlashAddress);
158 WriteDOC(0x00, docptr, Mplus_WritePipeTerm);
159 WriteDOC(0x00, docptr, Mplus_WritePipeTerm);
162 /* DoC_SelectChip: Select a given flash chip within the current floor */
163 static int DoC_SelectChip(unsigned long docptr, int chip)
165 /* No choice for flash chip on Millennium Plus */
169 /* DoC_SelectFloor: Select a given floor (bank of flash chips) */
170 static int DoC_SelectFloor(unsigned long docptr, int floor)
172 WriteDOC((floor & 0x3), docptr, Mplus_DeviceSelect);
177 * Translate the given offset into the appropriate command and offset.
178 * This does the mapping using the 16bit interleave layout defined by
179 * M-Systems, and looks like this for a sector pair:
180 * +-----------+-------+-------+-------+--------------+---------+-----------+
181 * | 0 --- 511 |512-517|518-519|520-521| 522 --- 1033 |1034-1039|1040 - 1055|
182 * +-----------+-------+-------+-------+--------------+---------+-----------+
183 * | Data 0 | ECC 0 |Flags0 |Flags1 | Data 1 |ECC 1 | OOB 1 + 2 |
184 * +-----------+-------+-------+-------+--------------+---------+-----------+
186 static unsigned int DoC_GetDataOffset(struct mtd_info *mtd, loff_t *from)
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;
206 static unsigned int DoC_GetECCOffset(struct mtd_info *mtd, loff_t *from)
208 unsigned int ofs, cmd;
211 cmd = NAND_CMD_READOOB;
212 ofs = 10 + (*from & 0xf);
214 cmd = NAND_CMD_READ1;
218 *from = (*from & ~0x3ff) | ofs;
222 static unsigned int DoC_GetFlagsOffset(struct mtd_info *mtd, loff_t *from)
224 unsigned int ofs, cmd;
226 cmd = NAND_CMD_READ1;
227 ofs = (*from & 0x200) ? 8 : 6;
228 *from = (*from & ~0x3ff) | ofs;
232 static unsigned int DoC_GetHdrOffset(struct mtd_info *mtd, loff_t *from)
234 unsigned int ofs, cmd;
236 cmd = NAND_CMD_READOOB;
237 ofs = (*from & 0x200) ? 24 : 16;
238 *from = (*from & ~0x3ff) | ofs;
242 static inline void MemReadDOC(unsigned long docptr, unsigned char *buf, int len)
246 for (i = 0; i < len; i++)
247 buf[i] = ReadDOC(docptr, Mil_CDSN_IO + i);
249 memcpy_fromio(buf, docptr + DoC_Mil_CDSN_IO, len);
253 static inline void MemWriteDOC(unsigned long docptr, unsigned char *buf, int len)
257 for (i = 0; i < len; i++)
258 WriteDOC(buf[i], docptr, Mil_CDSN_IO + i);
260 memcpy_toio(docptr + DoC_Mil_CDSN_IO, buf, len);
264 /* DoC_IdentChip: Identify a given NAND chip given {floor,chip} */
265 static int DoC_IdentChip(struct DiskOnChip *doc, int floor, int chip)
269 unsigned long docptr = doc->virtadr;
271 /* Page in the required floor/chip */
272 DoC_SelectFloor(docptr, floor);
273 DoC_SelectChip(docptr, chip);
275 /* Millennium Plus bus cycle sequence as per figure 2, section 2.4 */
276 WriteDOC((DOC_FLASH_CE | DOC_FLASH_WP), docptr, Mplus_FlashSelect);
278 /* Reset the chip, see Software Requirement 11.4 item 1. */
279 DoC_Command(docptr, NAND_CMD_RESET, 0);
280 DoC_WaitReady(docptr);
282 /* Read the NAND chip ID: 1. Send ReadID command */
283 DoC_Command(docptr, NAND_CMD_READID, 0);
285 /* Read the NAND chip ID: 2. Send address byte zero */
286 DoC_Address(doc, 1, 0x00, 0, 0x00);
288 WriteDOC(0, docptr, Mplus_FlashControl);
289 DoC_WaitReady(docptr);
291 /* Read the manufacturer and device id codes of the flash device through
292 CDSN IO register see Software Requirement 11.4 item 5.*/
293 dummy = ReadDOC(docptr, Mplus_ReadPipeInit);
294 dummy = ReadDOC(docptr, Mplus_ReadPipeInit);
296 mfr = ReadDOC(docptr, Mil_CDSN_IO);
297 dummy = ReadDOC(docptr, Mil_CDSN_IO); /* 2 way interleave */
299 id = ReadDOC(docptr, Mil_CDSN_IO);
300 dummy = ReadDOC(docptr, Mil_CDSN_IO); /* 2 way interleave */
302 dummy = ReadDOC(docptr, Mplus_LastDataRead);
303 dummy = ReadDOC(docptr, Mplus_LastDataRead);
305 /* Disable flash internally */
306 WriteDOC(0, docptr, Mplus_FlashSelect);
308 /* No response - return failure */
309 if (mfr == 0xff || mfr == 0)
312 for (i = 0; nand_flash_ids[i].name != NULL; i++) {
313 if (id == nand_flash_ids[i].id) {
314 /* Try to identify manufacturer */
315 for (j = 0; nand_manuf_ids[j].id != 0x0; j++) {
316 if (nand_manuf_ids[j].id == mfr)
319 printk(KERN_INFO "Flash chip found: Manufacturer ID: %2.2X, "
320 "Chip ID: %2.2X (%s:%s)\n", mfr, id,
321 nand_manuf_ids[j].name, nand_flash_ids[i].name);
325 if (doc->ChipID == DOC_ChipID_DocMilPlus32)
327 doc->chipshift = nand_flash_ids[i].chipshift;
328 doc->erasesize = nand_flash_ids[i].erasesize << doc->interleave;
333 if (nand_flash_ids[i].name == NULL)
338 /* DoC_ScanChips: Find all NAND chips present in a DiskOnChip, and identify them */
339 static void DoC_ScanChips(struct DiskOnChip *this)
342 int numchips[MAX_FLOORS_MPLUS];
349 /* For each floor, find the number of valid chips it contains */
350 for (floor = 0,ret = 1; floor < MAX_FLOORS_MPLUS; floor++) {
352 for (chip = 0; chip < MAX_CHIPS_MPLUS && ret != 0; chip++) {
353 ret = DoC_IdentChip(this, floor, chip);
360 /* If there are none at all that we recognise, bail */
361 if (!this->numchips) {
362 printk("No flash chips recognised.\n");
366 /* Allocate an array to hold the information for each chip */
367 this->chips = kmalloc(sizeof(struct Nand) * this->numchips, GFP_KERNEL);
369 printk("MTD: No memory for allocating chip info structures\n");
373 /* Fill out the chip array with {floor, chipno} for each
374 * detected chip in the device. */
375 for (floor = 0, ret = 0; floor < MAX_FLOORS_MPLUS; floor++) {
376 for (chip = 0 ; chip < numchips[floor] ; chip++) {
377 this->chips[ret].floor = floor;
378 this->chips[ret].chip = chip;
379 this->chips[ret].curadr = 0;
380 this->chips[ret].curmode = 0x50;
385 /* Calculate and print the total size of the device */
386 this->totlen = this->numchips * (1 << this->chipshift);
387 printk(KERN_INFO "%d flash chips found. Total DiskOnChip size: %ld MiB\n",
388 this->numchips ,this->totlen >> 20);
391 static int DoCMilPlus_is_alias(struct DiskOnChip *doc1, struct DiskOnChip *doc2)
393 int tmp1, tmp2, retval;
395 if (doc1->physadr == doc2->physadr)
398 /* Use the alias resolution register which was set aside for this
399 * purpose. If it's value is the same on both chips, they might
400 * be the same chip, and we write to one and check for a change in
401 * the other. It's unclear if this register is usuable in the
402 * DoC 2000 (it's in the Millennium docs), but it seems to work. */
403 tmp1 = ReadDOC(doc1->virtadr, Mplus_AliasResolution);
404 tmp2 = ReadDOC(doc2->virtadr, Mplus_AliasResolution);
408 WriteDOC((tmp1+1) % 0xff, doc1->virtadr, Mplus_AliasResolution);
409 tmp2 = ReadDOC(doc2->virtadr, Mplus_AliasResolution);
410 if (tmp2 == (tmp1+1) % 0xff)
415 /* Restore register contents. May not be necessary, but do it just to
417 WriteDOC(tmp1, doc1->virtadr, Mplus_AliasResolution);
422 static const char im_name[] = "DoCMilPlus_init";
424 /* This routine is made available to other mtd code via
425 * inter_module_register. It must only be accessed through
426 * inter_module_get which will bump the use count of this module. The
427 * addresses passed back in mtd are valid as long as the use count of
428 * this module is non-zero, i.e. between inter_module_get and
429 * inter_module_put. Keith Owens <kaos@ocs.com.au> 29 Oct 2000.
431 static void DoCMilPlus_init(struct mtd_info *mtd)
433 struct DiskOnChip *this = (struct DiskOnChip *)mtd->priv;
434 struct DiskOnChip *old = NULL;
436 /* We must avoid being called twice for the same device. */
438 old = (struct DiskOnChip *)docmilpluslist->priv;
441 if (DoCMilPlus_is_alias(this, old)) {
442 printk(KERN_NOTICE "Ignoring DiskOnChip Millennium "
443 "Plus at 0x%lX - already configured\n",
445 iounmap((void *)this->virtadr);
450 old = (struct DiskOnChip *)old->nextdoc->priv;
455 mtd->name = "DiskOnChip Millennium Plus";
456 printk(KERN_NOTICE "DiskOnChip Millennium Plus found at "
457 "address 0x%lX\n", this->physadr);
459 mtd->type = MTD_NANDFLASH;
460 mtd->flags = MTD_CAP_NANDFLASH;
461 mtd->ecctype = MTD_ECC_RS_DiskOnChip;
467 mtd->owner = THIS_MODULE;
468 mtd->erase = doc_erase;
471 mtd->read = doc_read;
472 mtd->write = doc_write;
473 mtd->read_ecc = doc_read_ecc;
474 mtd->write_ecc = doc_write_ecc;
475 mtd->read_oob = doc_read_oob;
476 mtd->write_oob = doc_write_oob;
484 /* Ident all the chips present. */
489 iounmap((void *)this->virtadr);
491 this->nextdoc = docmilpluslist;
492 docmilpluslist = mtd;
493 mtd->size = this->totlen;
494 mtd->erasesize = this->erasesize;
501 static int doc_dumpblk(struct mtd_info *mtd, loff_t from)
505 struct DiskOnChip *this = (struct DiskOnChip *)mtd->priv;
506 unsigned long docptr = this->virtadr;
507 struct Nand *mychip = &this->chips[from >> (this->chipshift)];
508 unsigned char *bp, buf[1056];
513 /* Don't allow read past end of device */
514 if (from >= this->totlen)
517 DoC_CheckASIC(docptr);
519 /* Find the chip which is to be used and select it */
520 if (this->curfloor != mychip->floor) {
521 DoC_SelectFloor(docptr, mychip->floor);
522 DoC_SelectChip(docptr, mychip->chip);
523 } else if (this->curchip != mychip->chip) {
524 DoC_SelectChip(docptr, mychip->chip);
526 this->curfloor = mychip->floor;
527 this->curchip = mychip->chip;
529 /* Millennium Plus bus cycle sequence as per figure 2, section 2.4 */
530 WriteDOC((DOC_FLASH_CE | DOC_FLASH_WP), docptr, Mplus_FlashSelect);
532 /* Reset the chip, see Software Requirement 11.4 item 1. */
533 DoC_Command(docptr, NAND_CMD_RESET, 0);
534 DoC_WaitReady(docptr);
537 DoC_Command(docptr, DoC_GetDataOffset(mtd, &fofs), 0);
538 DoC_Address(this, 3, fofs, 0, 0x00);
539 WriteDOC(0, docptr, Mplus_FlashControl);
540 DoC_WaitReady(docptr);
542 /* disable the ECC engine */
543 WriteDOC(DOC_ECC_RESET, docptr, Mplus_ECCConf);
545 ReadDOC(docptr, Mplus_ReadPipeInit);
546 ReadDOC(docptr, Mplus_ReadPipeInit);
548 /* Read the data via the internal pipeline through CDSN IO
549 register, see Pipelined Read Operations 11.3 */
550 MemReadDOC(docptr, buf, 1054);
551 buf[1054] = ReadDOC(docptr, Mplus_LastDataRead);
552 buf[1055] = ReadDOC(docptr, Mplus_LastDataRead);
554 memset(&c[0], 0, sizeof(c));
555 printk("DUMP OFFSET=%x:\n", (int)from);
557 for (i = 0, bp = &buf[0]; (i < 1056); i++) {
560 printk(" %02x", *bp);
561 c[(i & 0xf)] = ((*bp >= 0x20) && (*bp <= 0x7f)) ? *bp : '.';
563 if (((i + 1) % 16) == 0)
568 /* Disable flash internally */
569 WriteDOC(0, docptr, Mplus_FlashSelect);
575 static int doc_read(struct mtd_info *mtd, loff_t from, size_t len,
576 size_t *retlen, u_char *buf)
578 /* Just a special case of doc_read_ecc */
579 return doc_read_ecc(mtd, from, len, retlen, buf, NULL, NULL);
582 static int doc_read_ecc(struct mtd_info *mtd, loff_t from, size_t len,
583 size_t *retlen, u_char *buf, u_char *eccbuf,
584 struct nand_oobinfo *oobsel)
589 unsigned char syndrome[6];
590 struct DiskOnChip *this = (struct DiskOnChip *)mtd->priv;
591 unsigned long docptr = this->virtadr;
592 struct Nand *mychip = &this->chips[from >> (this->chipshift)];
594 /* Don't allow read past end of device */
595 if (from >= this->totlen)
598 /* Don't allow a single read to cross a 512-byte block boundary */
599 if (from + len > ((from | 0x1ff) + 1))
600 len = ((from | 0x1ff) + 1) - from;
602 DoC_CheckASIC(docptr);
604 /* Find the chip which is to be used and select it */
605 if (this->curfloor != mychip->floor) {
606 DoC_SelectFloor(docptr, mychip->floor);
607 DoC_SelectChip(docptr, mychip->chip);
608 } else if (this->curchip != mychip->chip) {
609 DoC_SelectChip(docptr, mychip->chip);
611 this->curfloor = mychip->floor;
612 this->curchip = mychip->chip;
614 /* Millennium Plus bus cycle sequence as per figure 2, section 2.4 */
615 WriteDOC((DOC_FLASH_CE | DOC_FLASH_WP), docptr, Mplus_FlashSelect);
617 /* Reset the chip, see Software Requirement 11.4 item 1. */
618 DoC_Command(docptr, NAND_CMD_RESET, 0);
619 DoC_WaitReady(docptr);
622 DoC_Command(docptr, DoC_GetDataOffset(mtd, &fofs), 0);
623 DoC_Address(this, 3, fofs, 0, 0x00);
624 WriteDOC(0, docptr, Mplus_FlashControl);
625 DoC_WaitReady(docptr);
628 /* init the ECC engine, see Reed-Solomon EDC/ECC 11.1 .*/
629 WriteDOC(DOC_ECC_RESET, docptr, Mplus_ECCConf);
630 WriteDOC(DOC_ECC_EN, docptr, Mplus_ECCConf);
632 /* disable the ECC engine */
633 WriteDOC(DOC_ECC_RESET, docptr, Mplus_ECCConf);
636 /* Let the caller know we completed it */
640 ReadDOC(docptr, Mplus_ReadPipeInit);
641 ReadDOC(docptr, Mplus_ReadPipeInit);
644 /* Read the data via the internal pipeline through CDSN IO
645 register, see Pipelined Read Operations 11.3 */
646 MemReadDOC(docptr, buf, len);
648 /* Read the ECC data following raw data */
649 MemReadDOC(docptr, eccbuf, 4);
650 eccbuf[4] = ReadDOC(docptr, Mplus_LastDataRead);
651 eccbuf[5] = ReadDOC(docptr, Mplus_LastDataRead);
653 /* Flush the pipeline */
654 dummy = ReadDOC(docptr, Mplus_ECCConf);
655 dummy = ReadDOC(docptr, Mplus_ECCConf);
657 /* Check the ECC Status */
658 if (ReadDOC(docptr, Mplus_ECCConf) & 0x80) {
660 /* There was an ECC error */
662 printk("DiskOnChip ECC Error: Read at %lx\n", (long)from);
664 /* Read the ECC syndrom through the DiskOnChip ECC logic.
665 These syndrome will be all ZERO when there is no error */
666 for (i = 0; i < 6; i++)
667 syndrome[i] = ReadDOC(docptr, Mplus_ECCSyndrome0 + i);
669 nb_errors = doc_decode_ecc(buf, syndrome);
671 printk("ECC Errors corrected: %x\n", nb_errors);
674 /* We return error, but have actually done the read. Not that
675 this can be told to user-space, via sys_read(), but at least
676 MTD-aware stuff can know 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]);
699 /* disable the ECC engine */
700 WriteDOC(DOC_ECC_DIS, docptr , Mplus_ECCConf);
702 /* Read the data via the internal pipeline through CDSN IO
703 register, see Pipelined Read Operations 11.3 */
704 MemReadDOC(docptr, buf, len-2);
705 buf[len-2] = ReadDOC(docptr, Mplus_LastDataRead);
706 buf[len-1] = ReadDOC(docptr, Mplus_LastDataRead);
709 /* Disable flash internally */
710 WriteDOC(0, docptr, Mplus_FlashSelect);
715 static int doc_write(struct mtd_info *mtd, loff_t to, size_t len,
716 size_t *retlen, const u_char *buf)
719 return doc_write_ecc(mtd, to, len, retlen, buf, eccbuf, NULL);
722 static int doc_write_ecc(struct mtd_info *mtd, loff_t to, size_t len,
723 size_t *retlen, const u_char *buf, u_char *eccbuf,
724 struct nand_oobinfo *oobsel)
726 int i, before, ret = 0;
729 struct DiskOnChip *this = (struct DiskOnChip *)mtd->priv;
730 unsigned long docptr = this->virtadr;
731 struct Nand *mychip = &this->chips[to >> (this->chipshift)];
733 /* Don't allow write past end of device */
734 if (to >= this->totlen)
737 /* Don't allow writes which aren't exactly one block (512 bytes) */
738 if ((to & 0x1ff) || (len != 0x200))
741 /* Determine position of OOB flags, before or after data */
744 DoC_CheckASIC(docptr);
746 /* Find the chip which is to be used and select it */
747 if (this->curfloor != mychip->floor) {
748 DoC_SelectFloor(docptr, mychip->floor);
749 DoC_SelectChip(docptr, mychip->chip);
750 } else if (this->curchip != mychip->chip) {
751 DoC_SelectChip(docptr, mychip->chip);
753 this->curfloor = mychip->floor;
754 this->curchip = mychip->chip;
756 /* Millennium Plus bus cycle sequence as per figure 2, section 2.4 */
757 WriteDOC(DOC_FLASH_CE, docptr, Mplus_FlashSelect);
759 /* Reset the chip, see Software Requirement 11.4 item 1. */
760 DoC_Command(docptr, NAND_CMD_RESET, 0);
761 DoC_WaitReady(docptr);
763 /* Set device to appropriate plane of flash */
765 WriteDOC(DoC_GetDataOffset(mtd, &fto), docptr, Mplus_FlashCmd);
767 /* On interleaved devices the flags for 2nd half 512 are before data */
768 if (eccbuf && before)
771 /* issue the Serial Data In command to initial the Page Program process */
772 DoC_Command(docptr, NAND_CMD_SEQIN, 0x00);
773 DoC_Address(this, 3, fto, 0x00, 0x00);
775 /* Disable the ECC engine */
776 WriteDOC(DOC_ECC_RESET, docptr, Mplus_ECCConf);
780 /* Write the block status BLOCK_USED (0x5555) */
781 WriteDOC(0x55, docptr, Mil_CDSN_IO);
782 WriteDOC(0x55, docptr, Mil_CDSN_IO);
785 /* init the ECC engine, see Reed-Solomon EDC/ECC 11.1 .*/
786 WriteDOC(DOC_ECC_EN | DOC_ECC_RW, docptr, Mplus_ECCConf);
789 MemWriteDOC(docptr, (unsigned char *) buf, len);
792 /* Write ECC data to flash, the ECC info is generated by
793 the DiskOnChip ECC logic see Reed-Solomon EDC/ECC 11.1 */
794 DoC_Delay(docptr, 3);
796 /* Read the ECC data through the DiskOnChip ECC logic */
797 for (i = 0; i < 6; i++)
798 eccbuf[i] = ReadDOC(docptr, Mplus_ECCSyndrome0 + i);
800 /* disable the ECC engine */
801 WriteDOC(DOC_ECC_DIS, docptr, Mplus_ECCConf);
803 /* Write the ECC data to flash */
804 MemWriteDOC(docptr, eccbuf, 6);
807 /* Write the block status BLOCK_USED (0x5555) */
808 WriteDOC(0x55, docptr, Mil_CDSN_IO+6);
809 WriteDOC(0x55, docptr, Mil_CDSN_IO+7);
813 printk("OOB data at %lx is %2.2X %2.2X %2.2X %2.2X %2.2X %2.2X\n",
814 (long) to, eccbuf[0], eccbuf[1], eccbuf[2], eccbuf[3],
815 eccbuf[4], eccbuf[5]);
819 WriteDOC(0x00, docptr, Mplus_WritePipeTerm);
820 WriteDOC(0x00, docptr, Mplus_WritePipeTerm);
822 /* Commit the Page Program command and wait for ready
823 see Software Requirement 11.4 item 1.*/
824 DoC_Command(docptr, NAND_CMD_PAGEPROG, 0x00);
825 DoC_WaitReady(docptr);
827 /* Read the status of the flash device through CDSN IO register
828 see Software Requirement 11.4 item 5.*/
829 DoC_Command(docptr, NAND_CMD_STATUS, 0);
830 dummy = ReadDOC(docptr, Mplus_ReadPipeInit);
831 dummy = ReadDOC(docptr, Mplus_ReadPipeInit);
832 DoC_Delay(docptr, 2);
833 if ((dummy = ReadDOC(docptr, Mplus_LastDataRead)) & 1) {
834 printk("MTD: Error 0x%x programming at 0x%x\n", dummy, (int)to);
835 /* Error in programming
836 FIXME: implement Bad Block Replacement (in nftl.c ??) */
840 dummy = ReadDOC(docptr, Mplus_LastDataRead);
842 /* Disable flash internally */
843 WriteDOC(0, docptr, Mplus_FlashSelect);
845 /* Let the caller know we completed it */
851 static int doc_read_oob(struct mtd_info *mtd, loff_t ofs, size_t len,
852 size_t *retlen, u_char *buf)
855 struct DiskOnChip *this = (struct DiskOnChip *)mtd->priv;
856 unsigned long docptr = this->virtadr;
857 struct Nand *mychip = &this->chips[ofs >> this->chipshift];
858 size_t i, size, got, want;
860 DoC_CheckASIC(docptr);
862 /* Find the chip which is to be used and select it */
863 if (this->curfloor != mychip->floor) {
864 DoC_SelectFloor(docptr, mychip->floor);
865 DoC_SelectChip(docptr, mychip->chip);
866 } else if (this->curchip != mychip->chip) {
867 DoC_SelectChip(docptr, mychip->chip);
869 this->curfloor = mychip->floor;
870 this->curchip = mychip->chip;
872 /* Millennium Plus bus cycle sequence as per figure 2, section 2.4 */
873 WriteDOC((DOC_FLASH_CE | DOC_FLASH_WP), docptr, Mplus_FlashSelect);
875 /* disable the ECC engine */
876 WriteDOC(DOC_ECC_RESET, docptr, Mplus_ECCConf);
877 DoC_WaitReady(docptr);
879 /* Maximum of 16 bytes in the OOB region, so limit read to that */
885 for (i = 0; ((i < 3) && (want > 0)); i++) {
886 /* Figure out which region we are accessing... */
890 DoC_Command(docptr, DoC_GetECCOffset(mtd, &fofs), 0);
892 } else if (base < 8) {
893 DoC_Command(docptr, DoC_GetFlagsOffset(mtd, &fofs), 0);
896 DoC_Command(docptr, DoC_GetHdrOffset(mtd, &fofs), 0);
902 /* Issue read command */
903 DoC_Address(this, 3, fofs, 0, 0x00);
904 WriteDOC(0, docptr, Mplus_FlashControl);
905 DoC_WaitReady(docptr);
907 ReadDOC(docptr, Mplus_ReadPipeInit);
908 ReadDOC(docptr, Mplus_ReadPipeInit);
909 MemReadDOC(docptr, &buf[got], size - 2);
910 buf[got + size - 2] = ReadDOC(docptr, Mplus_LastDataRead);
911 buf[got + size - 1] = ReadDOC(docptr, Mplus_LastDataRead);
918 /* Disable flash internally */
919 WriteDOC(0, docptr, Mplus_FlashSelect);
925 static int doc_write_oob(struct mtd_info *mtd, loff_t ofs, size_t len,
926 size_t *retlen, const u_char *buf)
930 struct DiskOnChip *this = (struct DiskOnChip *)mtd->priv;
931 unsigned long docptr = this->virtadr;
932 struct Nand *mychip = &this->chips[ofs >> this->chipshift];
933 size_t i, size, got, want;
936 DoC_CheckASIC(docptr);
938 /* Find the chip which is to be used and select it */
939 if (this->curfloor != mychip->floor) {
940 DoC_SelectFloor(docptr, mychip->floor);
941 DoC_SelectChip(docptr, mychip->chip);
942 } else if (this->curchip != mychip->chip) {
943 DoC_SelectChip(docptr, mychip->chip);
945 this->curfloor = mychip->floor;
946 this->curchip = mychip->chip;
948 /* Millennium Plus bus cycle sequence as per figure 2, section 2.4 */
949 WriteDOC(DOC_FLASH_CE, docptr, Mplus_FlashSelect);
952 /* Maximum of 16 bytes in the OOB region, so limit write to that */
958 for (i = 0; ((i < 3) && (want > 0)); i++) {
959 /* Reset the chip, see Software Requirement 11.4 item 1. */
960 DoC_Command(docptr, NAND_CMD_RESET, 0);
961 DoC_WaitReady(docptr);
963 /* Figure out which region we are accessing... */
967 WriteDOC(DoC_GetECCOffset(mtd, &fofs), docptr, Mplus_FlashCmd);
969 } else if (base < 8) {
970 WriteDOC(DoC_GetFlagsOffset(mtd, &fofs), docptr, Mplus_FlashCmd);
973 WriteDOC(DoC_GetHdrOffset(mtd, &fofs), docptr, Mplus_FlashCmd);
979 /* Issue the Serial Data In command to initial the Page Program process */
980 DoC_Command(docptr, NAND_CMD_SEQIN, 0x00);
981 DoC_Address(this, 3, fofs, 0, 0x00);
983 /* Disable the ECC engine */
984 WriteDOC(DOC_ECC_RESET, docptr, Mplus_ECCConf);
986 /* Write the data via the internal pipeline through CDSN IO
987 register, see Pipelined Write Operations 11.2 */
988 MemWriteDOC(docptr, (unsigned char *) &buf[got], size);
989 WriteDOC(0x00, docptr, Mplus_WritePipeTerm);
990 WriteDOC(0x00, docptr, Mplus_WritePipeTerm);
992 /* Commit the Page Program command and wait for ready
993 see Software Requirement 11.4 item 1.*/
994 DoC_Command(docptr, NAND_CMD_PAGEPROG, 0x00);
995 DoC_WaitReady(docptr);
997 /* Read the status of the flash device through CDSN IO register
998 see Software Requirement 11.4 item 5.*/
999 DoC_Command(docptr, NAND_CMD_STATUS, 0x00);
1000 dummy = ReadDOC(docptr, Mplus_ReadPipeInit);
1001 dummy = ReadDOC(docptr, Mplus_ReadPipeInit);
1002 DoC_Delay(docptr, 2);
1003 if ((dummy = ReadDOC(docptr, Mplus_LastDataRead)) & 1) {
1004 printk("MTD: Error 0x%x programming oob at 0x%x\n",
1006 /* FIXME: implement Bad Block Replacement */
1010 dummy = ReadDOC(docptr, Mplus_LastDataRead);
1017 /* Disable flash internally */
1018 WriteDOC(0, docptr, Mplus_FlashSelect);
1024 int doc_erase(struct mtd_info *mtd, struct erase_info *instr)
1026 volatile char dummy;
1027 struct DiskOnChip *this = (struct DiskOnChip *)mtd->priv;
1028 __u32 ofs = instr->addr;
1029 __u32 len = instr->len;
1030 unsigned long docptr = this->virtadr;
1031 struct Nand *mychip = &this->chips[ofs >> this->chipshift];
1033 DoC_CheckASIC(docptr);
1035 if (len != mtd->erasesize)
1036 printk(KERN_WARNING "MTD: Erase not right size (%x != %x)n",
1037 len, mtd->erasesize);
1039 /* Find the chip which is to be used and select it */
1040 if (this->curfloor != mychip->floor) {
1041 DoC_SelectFloor(docptr, mychip->floor);
1042 DoC_SelectChip(docptr, mychip->chip);
1043 } else if (this->curchip != mychip->chip) {
1044 DoC_SelectChip(docptr, mychip->chip);
1046 this->curfloor = mychip->floor;
1047 this->curchip = mychip->chip;
1049 instr->state = MTD_ERASE_PENDING;
1051 /* Millennium Plus bus cycle sequence as per figure 2, section 2.4 */
1052 WriteDOC(DOC_FLASH_CE, docptr, Mplus_FlashSelect);
1054 DoC_Command(docptr, NAND_CMD_RESET, 0x00);
1055 DoC_WaitReady(docptr);
1057 DoC_Command(docptr, NAND_CMD_ERASE1, 0);
1058 DoC_Address(this, 2, ofs, 0, 0x00);
1059 DoC_Command(docptr, NAND_CMD_ERASE2, 0);
1060 DoC_WaitReady(docptr);
1061 instr->state = MTD_ERASING;
1063 /* Read the status of the flash device through CDSN IO register
1064 see Software Requirement 11.4 item 5. */
1065 DoC_Command(docptr, NAND_CMD_STATUS, 0);
1066 dummy = ReadDOC(docptr, Mplus_ReadPipeInit);
1067 dummy = ReadDOC(docptr, Mplus_ReadPipeInit);
1068 if ((dummy = ReadDOC(docptr, Mplus_LastDataRead)) & 1) {
1069 printk("MTD: Error 0x%x erasing at 0x%x\n", dummy, ofs);
1070 /* FIXME: implement Bad Block Replacement (in nftl.c ??) */
1071 instr->state = MTD_ERASE_FAILED;
1073 instr->state = MTD_ERASE_DONE;
1075 dummy = ReadDOC(docptr, Mplus_LastDataRead);
1077 /* Disable flash internally */
1078 WriteDOC(0, docptr, Mplus_FlashSelect);
1080 if (instr->callback)
1081 instr->callback(instr);
1086 /****************************************************************************
1090 ****************************************************************************/
1092 int __init init_doc2001plus(void)
1094 inter_module_register(im_name, THIS_MODULE, &DoCMilPlus_init);
1098 static void __exit cleanup_doc2001plus(void)
1100 struct mtd_info *mtd;
1101 struct DiskOnChip *this;
1103 while ((mtd=docmilpluslist)) {
1104 this = (struct DiskOnChip *)mtd->priv;
1105 docmilpluslist = this->nextdoc;
1107 del_mtd_device(mtd);
1109 iounmap((void *)this->virtadr);
1113 inter_module_unregister(im_name);
1116 module_exit(cleanup_doc2001plus);
1117 module_init(init_doc2001plus);
1119 MODULE_LICENSE("GPL");
1120 MODULE_AUTHOR("Greg Ungerer <gerg@snapgear.com> et al.");
1121 MODULE_DESCRIPTION("Driver for DiskOnChip Millennium Plus");