3 * Linux driver for Disk-On-Chip Millennium
4 * (c) 1999 Machine Vision Holdings, Inc.
5 * (c) 1999, 2000 David Woodhouse <dwmw2@infradead.org>
7 * $Id: doc2001.c,v 1.42 2004/04/04 12:36:45 gleixner Exp $
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <asm/errno.h>
14 #include <asm/uaccess.h>
15 #include <linux/miscdevice.h>
16 #include <linux/pci.h>
17 #include <linux/delay.h>
18 #include <linux/slab.h>
19 #include <linux/sched.h>
20 #include <linux/init.h>
21 #include <linux/types.h>
22 #include <linux/bitops.h>
24 #include <linux/mtd/mtd.h>
25 #include <linux/mtd/nand.h>
26 #include <linux/mtd/doc2000.h>
28 /* #define ECC_DEBUG */
30 /* I have no idea why some DoC chips can not use memcop_form|to_io().
31 * This may be due to the different revisions of the ASIC controller built-in or
32 * simplily a QA/Bug issue. Who knows ?? If you have trouble, please uncomment
36 static int doc_read(struct mtd_info *mtd, loff_t from, size_t len,
37 size_t *retlen, u_char *buf);
38 static int doc_write(struct mtd_info *mtd, loff_t to, size_t len,
39 size_t *retlen, const u_char *buf);
40 static int doc_read_ecc(struct mtd_info *mtd, loff_t from, size_t len,
41 size_t *retlen, u_char *buf, u_char *eccbuf, int oobsel);
42 static int doc_write_ecc(struct mtd_info *mtd, loff_t to, size_t len,
43 size_t *retlen, const u_char *buf, u_char *eccbuf, int oobsel);
44 static int doc_read_oob(struct mtd_info *mtd, loff_t ofs, size_t len,
45 size_t *retlen, u_char *buf);
46 static int doc_write_oob(struct mtd_info *mtd, loff_t ofs, size_t len,
47 size_t *retlen, const u_char *buf);
48 static int doc_erase (struct mtd_info *mtd, struct erase_info *instr);
50 static struct mtd_info *docmillist = NULL;
52 /* Perform the required delay cycles by reading from the NOP register */
53 static void DoC_Delay(unsigned long docptr, unsigned short cycles)
58 for (i = 0; i < cycles; i++)
59 dummy = ReadDOC(docptr, NOP);
62 /* DOC_WaitReady: Wait for RDY line to be asserted by the flash chip */
63 static int _DoC_WaitReady(unsigned long docptr)
65 unsigned short 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, CDSNControl) & CDSN_CTRL_FR_B) && --c)
75 DEBUG(MTD_DEBUG_LEVEL2, "_DoC_WaitReady timed out.\n");
80 static inline int DoC_WaitReady(unsigned long docptr)
82 /* This is inline, to optimise the common case, where it's ready instantly */
85 /* 4 read form NOP register should be issued in prior to the read from CDSNControl
86 see Software Requirement 11.4 item 2. */
89 if (!(ReadDOC(docptr, CDSNControl) & CDSN_CTRL_FR_B))
90 /* Call the out-of-line routine to wait */
91 ret = _DoC_WaitReady(docptr);
93 /* issue 2 read from NOP register after reading from CDSNControl register
94 see Software Requirement 11.4 item 2. */
100 /* DoC_Command: Send a flash command to the flash chip through the CDSN IO register
101 with the internal pipeline. Each of 4 delay cycles (read from the NOP register) is
102 required after writing to CDSN Control register, see Software Requirement 11.4 item 3. */
104 static inline void DoC_Command(unsigned long docptr, unsigned char command,
105 unsigned char xtraflags)
107 /* Assert the CLE (Command Latch Enable) line to the flash chip */
108 WriteDOC(xtraflags | CDSN_CTRL_CLE | CDSN_CTRL_CE, docptr, CDSNControl);
109 DoC_Delay(docptr, 4);
111 /* Send the command */
112 WriteDOC(command, docptr, Mil_CDSN_IO);
113 WriteDOC(0x00, docptr, WritePipeTerm);
115 /* Lower the CLE line */
116 WriteDOC(xtraflags | CDSN_CTRL_CE, docptr, CDSNControl);
117 DoC_Delay(docptr, 4);
120 /* DoC_Address: Set the current address for the flash chip through the CDSN IO register
121 with the internal pipeline. Each of 4 delay cycles (read from the NOP register) is
122 required after writing to CDSN Control register, see Software Requirement 11.4 item 3. */
124 static inline void DoC_Address(unsigned long docptr, int numbytes, unsigned long ofs,
125 unsigned char xtraflags1, unsigned char xtraflags2)
127 /* Assert the ALE (Address Latch Enable) line to the flash chip */
128 WriteDOC(xtraflags1 | CDSN_CTRL_ALE | CDSN_CTRL_CE, docptr, CDSNControl);
129 DoC_Delay(docptr, 4);
131 /* Send the address */
135 /* Send single byte, bits 0-7. */
136 WriteDOC(ofs & 0xff, docptr, Mil_CDSN_IO);
137 WriteDOC(0x00, docptr, WritePipeTerm);
140 /* Send bits 9-16 followed by 17-23 */
141 WriteDOC((ofs >> 9) & 0xff, docptr, Mil_CDSN_IO);
142 WriteDOC((ofs >> 17) & 0xff, docptr, Mil_CDSN_IO);
143 WriteDOC(0x00, docptr, WritePipeTerm);
146 /* Send 0-7, 9-16, then 17-23 */
147 WriteDOC(ofs & 0xff, docptr, Mil_CDSN_IO);
148 WriteDOC((ofs >> 9) & 0xff, docptr, Mil_CDSN_IO);
149 WriteDOC((ofs >> 17) & 0xff, docptr, Mil_CDSN_IO);
150 WriteDOC(0x00, docptr, WritePipeTerm);
156 /* Lower the ALE line */
157 WriteDOC(xtraflags1 | xtraflags2 | CDSN_CTRL_CE, docptr, CDSNControl);
158 DoC_Delay(docptr, 4);
161 /* DoC_SelectChip: Select a given flash chip within the current floor */
162 static int DoC_SelectChip(unsigned long docptr, int chip)
164 /* Select the individual flash chip requested */
165 WriteDOC(chip, docptr, CDSNDeviceSelect);
166 DoC_Delay(docptr, 4);
168 /* Wait for it to be ready */
169 return DoC_WaitReady(docptr);
172 /* DoC_SelectFloor: Select a given floor (bank of flash chips) */
173 static int DoC_SelectFloor(unsigned long docptr, int floor)
175 /* Select the floor (bank) of chips required */
176 WriteDOC(floor, docptr, FloorSelect);
178 /* Wait for the chip to be ready */
179 return DoC_WaitReady(docptr);
182 /* DoC_IdentChip: Identify a given NAND chip given {floor,chip} */
183 static int DoC_IdentChip(struct DiskOnChip *doc, int floor, int chip)
188 /* Page in the required floor/chip
189 FIXME: is this supported by Millennium ?? */
190 DoC_SelectFloor(doc->virtadr, floor);
191 DoC_SelectChip(doc->virtadr, chip);
193 /* Reset the chip, see Software Requirement 11.4 item 1. */
194 DoC_Command(doc->virtadr, NAND_CMD_RESET, CDSN_CTRL_WP);
195 DoC_WaitReady(doc->virtadr);
197 /* Read the NAND chip ID: 1. Send ReadID command */
198 DoC_Command(doc->virtadr, NAND_CMD_READID, CDSN_CTRL_WP);
200 /* Read the NAND chip ID: 2. Send address byte zero */
201 DoC_Address(doc->virtadr, 1, 0x00, CDSN_CTRL_WP, 0x00);
203 /* Read the manufacturer and device id codes of the flash device through
204 CDSN IO register see Software Requirement 11.4 item 5.*/
205 dummy = ReadDOC(doc->virtadr, ReadPipeInit);
206 DoC_Delay(doc->virtadr, 2);
207 mfr = ReadDOC(doc->virtadr, Mil_CDSN_IO);
209 DoC_Delay(doc->virtadr, 2);
210 id = ReadDOC(doc->virtadr, Mil_CDSN_IO);
211 dummy = ReadDOC(doc->virtadr, LastDataRead);
213 /* No response - return failure */
214 if (mfr == 0xff || mfr == 0)
217 /* FIXME: to deal with multi-flash on multi-Millennium case more carefully */
218 for (i = 0; nand_flash_ids[i].name != NULL; i++) {
219 if ( id == nand_flash_ids[i].id) {
220 /* Try to identify manufacturer */
221 for (j = 0; nand_manuf_ids[j].id != 0x0; j++) {
222 if (nand_manuf_ids[j].id == mfr)
225 printk(KERN_INFO "Flash chip found: Manufacturer ID: %2.2X, "
226 "Chip ID: %2.2X (%s:%s)\n",
227 mfr, id, nand_manuf_ids[j].name, nand_flash_ids[i].name);
230 doc->chipshift = ffs((nand_flash_ids[i].chipsize << 20)) - 1;
235 if (nand_flash_ids[i].name == NULL)
241 /* DoC_ScanChips: Find all NAND chips present in a DiskOnChip, and identify them */
242 static void DoC_ScanChips(struct DiskOnChip *this)
245 int numchips[MAX_FLOORS_MIL];
252 /* For each floor, find the number of valid chips it contains */
253 for (floor = 0,ret = 1; floor < MAX_FLOORS_MIL; floor++) {
255 for (chip = 0; chip < MAX_CHIPS_MIL && ret != 0; chip++) {
256 ret = DoC_IdentChip(this, floor, chip);
263 /* If there are none at all that we recognise, bail */
264 if (!this->numchips) {
265 printk("No flash chips recognised.\n");
269 /* Allocate an array to hold the information for each chip */
270 this->chips = kmalloc(sizeof(struct Nand) * this->numchips, GFP_KERNEL);
272 printk("No memory for allocating chip info structures\n");
276 /* Fill out the chip array with {floor, chipno} for each
277 * detected chip in the device. */
278 for (floor = 0, ret = 0; floor < MAX_FLOORS_MIL; floor++) {
279 for (chip = 0 ; chip < numchips[floor] ; chip++) {
280 this->chips[ret].floor = floor;
281 this->chips[ret].chip = chip;
282 this->chips[ret].curadr = 0;
283 this->chips[ret].curmode = 0x50;
288 /* Calculate and print the total size of the device */
289 this->totlen = this->numchips * (1 << this->chipshift);
290 printk(KERN_INFO "%d flash chips found. Total DiskOnChip size: %ld MiB\n",
291 this->numchips ,this->totlen >> 20);
294 static int DoCMil_is_alias(struct DiskOnChip *doc1, struct DiskOnChip *doc2)
296 int tmp1, tmp2, retval;
298 if (doc1->physadr == doc2->physadr)
301 /* Use the alias resolution register which was set aside for this
302 * purpose. If it's value is the same on both chips, they might
303 * be the same chip, and we write to one and check for a change in
304 * the other. It's unclear if this register is usuable in the
305 * DoC 2000 (it's in the Millenium docs), but it seems to work. */
306 tmp1 = ReadDOC(doc1->virtadr, AliasResolution);
307 tmp2 = ReadDOC(doc2->virtadr, AliasResolution);
311 WriteDOC((tmp1+1) % 0xff, doc1->virtadr, AliasResolution);
312 tmp2 = ReadDOC(doc2->virtadr, AliasResolution);
313 if (tmp2 == (tmp1+1) % 0xff)
318 /* Restore register contents. May not be necessary, but do it just to
320 WriteDOC(tmp1, doc1->virtadr, AliasResolution);
325 static const char im_name[] = "DoCMil_init";
327 /* This routine is made available to other mtd code via
328 * inter_module_register. It must only be accessed through
329 * inter_module_get which will bump the use count of this module. The
330 * addresses passed back in mtd are valid as long as the use count of
331 * this module is non-zero, i.e. between inter_module_get and
332 * inter_module_put. Keith Owens <kaos@ocs.com.au> 29 Oct 2000.
334 static void DoCMil_init(struct mtd_info *mtd)
336 struct DiskOnChip *this = (struct DiskOnChip *)mtd->priv;
337 struct DiskOnChip *old = NULL;
339 /* We must avoid being called twice for the same device. */
341 old = (struct DiskOnChip *)docmillist->priv;
344 if (DoCMil_is_alias(this, old)) {
345 printk(KERN_NOTICE "Ignoring DiskOnChip Millennium at "
346 "0x%lX - already configured\n", this->physadr);
347 iounmap((void *)this->virtadr);
352 old = (struct DiskOnChip *)old->nextdoc->priv;
357 mtd->name = "DiskOnChip Millennium";
358 printk(KERN_NOTICE "DiskOnChip Millennium found at address 0x%lX\n",
361 mtd->type = MTD_NANDFLASH;
362 mtd->flags = MTD_CAP_NANDFLASH;
363 mtd->ecctype = MTD_ECC_RS_DiskOnChip;
366 /* FIXME: erase size is not always 8KiB */
367 mtd->erasesize = 0x2000;
371 mtd->owner = THIS_MODULE;
372 mtd->erase = doc_erase;
375 mtd->read = doc_read;
376 mtd->write = doc_write;
377 mtd->read_ecc = doc_read_ecc;
378 mtd->write_ecc = doc_write_ecc;
379 mtd->read_oob = doc_read_oob;
380 mtd->write_oob = doc_write_oob;
388 /* Ident all the chips present. */
393 iounmap((void *)this->virtadr);
395 this->nextdoc = docmillist;
397 mtd->size = this->totlen;
403 static int doc_read (struct mtd_info *mtd, loff_t from, size_t len,
404 size_t *retlen, u_char *buf)
406 /* Just a special case of doc_read_ecc */
407 return doc_read_ecc(mtd, from, len, retlen, buf, NULL, 0);
410 static int doc_read_ecc (struct mtd_info *mtd, loff_t from, size_t len,
411 size_t *retlen, u_char *buf, u_char *eccbuf, int oobsel)
415 unsigned char syndrome[6];
416 struct DiskOnChip *this = (struct DiskOnChip *)mtd->priv;
417 unsigned long docptr = this->virtadr;
418 struct Nand *mychip = &this->chips[from >> (this->chipshift)];
420 /* Don't allow read past end of device */
421 if (from >= this->totlen)
424 /* Don't allow a single read to cross a 512-byte block boundary */
425 if (from + len > ((from | 0x1ff) + 1))
426 len = ((from | 0x1ff) + 1) - from;
428 /* Find the chip which is to be used and select it */
429 if (this->curfloor != mychip->floor) {
430 DoC_SelectFloor(docptr, mychip->floor);
431 DoC_SelectChip(docptr, mychip->chip);
432 } else if (this->curchip != mychip->chip) {
433 DoC_SelectChip(docptr, mychip->chip);
435 this->curfloor = mychip->floor;
436 this->curchip = mychip->chip;
438 /* issue the Read0 or Read1 command depend on which half of the page
439 we are accessing. Polling the Flash Ready bit after issue 3 bytes
440 address in Sequence Read Mode, see Software Requirement 11.4 item 1.*/
441 DoC_Command(docptr, (from >> 8) & 1, CDSN_CTRL_WP);
442 DoC_Address(docptr, 3, from, CDSN_CTRL_WP, 0x00);
443 DoC_WaitReady(docptr);
446 /* init the ECC engine, see Reed-Solomon EDC/ECC 11.1 .*/
447 WriteDOC (DOC_ECC_RESET, docptr, ECCConf);
448 WriteDOC (DOC_ECC_EN, docptr, ECCConf);
450 /* disable the ECC engine */
451 WriteDOC (DOC_ECC_RESET, docptr, ECCConf);
452 WriteDOC (DOC_ECC_DIS, docptr, ECCConf);
455 /* Read the data via the internal pipeline through CDSN IO register,
456 see Pipelined Read Operations 11.3 */
457 dummy = ReadDOC(docptr, ReadPipeInit);
459 for (i = 0; i < len-1; i++) {
460 /* N.B. you have to increase the source address in this way or the
461 ECC logic will not work properly */
462 buf[i] = ReadDOC(docptr, Mil_CDSN_IO + (i & 0xff));
465 memcpy_fromio(buf, docptr + DoC_Mil_CDSN_IO, len - 1);
467 buf[len - 1] = ReadDOC(docptr, LastDataRead);
469 /* Let the caller know we completed it */
474 /* Read the ECC data from Spare Data Area,
475 see Reed-Solomon EDC/ECC 11.1 */
476 dummy = ReadDOC(docptr, ReadPipeInit);
478 for (i = 0; i < 5; i++) {
479 /* N.B. you have to increase the source address in this way or the
480 ECC logic will not work properly */
481 eccbuf[i] = ReadDOC(docptr, Mil_CDSN_IO + i);
484 memcpy_fromio(eccbuf, docptr + DoC_Mil_CDSN_IO, 5);
486 eccbuf[5] = ReadDOC(docptr, LastDataRead);
488 /* Flush the pipeline */
489 dummy = ReadDOC(docptr, ECCConf);
490 dummy = ReadDOC(docptr, ECCConf);
492 /* Check the ECC Status */
493 if (ReadDOC(docptr, ECCConf) & 0x80) {
495 /* There was an ECC error */
497 printk("DiskOnChip ECC Error: Read at %lx\n", (long)from);
499 /* Read the ECC syndrom through the DiskOnChip ECC logic.
500 These syndrome will be all ZERO when there is no error */
501 for (i = 0; i < 6; i++) {
502 syndrome[i] = ReadDOC(docptr, ECCSyndrome0 + i);
504 nb_errors = doc_decode_ecc(buf, syndrome);
506 printk("ECC Errors corrected: %x\n", nb_errors);
509 /* We return error, but have actually done the read. Not that
510 this can be told to user-space, via sys_read(), but at least
511 MTD-aware stuff can know about it by checking *retlen */
517 printk("ECC DATA at %lx: %2.2X %2.2X %2.2X %2.2X %2.2X %2.2X\n",
518 (long)from, eccbuf[0], eccbuf[1], eccbuf[2], eccbuf[3],
519 eccbuf[4], eccbuf[5]);
522 /* disable the ECC engine */
523 WriteDOC(DOC_ECC_DIS, docptr , ECCConf);
529 static int doc_write (struct mtd_info *mtd, loff_t to, size_t len,
530 size_t *retlen, const u_char *buf)
533 return doc_write_ecc(mtd, to, len, retlen, buf, eccbuf, 0);
536 static int doc_write_ecc (struct mtd_info *mtd, loff_t to, size_t len,
537 size_t *retlen, const u_char *buf, u_char *eccbuf, int oobsel)
541 struct DiskOnChip *this = (struct DiskOnChip *)mtd->priv;
542 unsigned long docptr = this->virtadr;
543 struct Nand *mychip = &this->chips[to >> (this->chipshift)];
545 /* Don't allow write past end of device */
546 if (to >= this->totlen)
550 /* Don't allow a single write to cross a 512-byte block boundary */
551 if (to + len > ( (to | 0x1ff) + 1))
552 len = ((to | 0x1ff) + 1) - to;
554 /* Don't allow writes which aren't exactly one block */
555 if (to & 0x1ff || len != 0x200)
559 /* Find the chip which is to be used and select it */
560 if (this->curfloor != mychip->floor) {
561 DoC_SelectFloor(docptr, mychip->floor);
562 DoC_SelectChip(docptr, mychip->chip);
563 } else if (this->curchip != mychip->chip) {
564 DoC_SelectChip(docptr, mychip->chip);
566 this->curfloor = mychip->floor;
567 this->curchip = mychip->chip;
569 /* Reset the chip, see Software Requirement 11.4 item 1. */
570 DoC_Command(docptr, NAND_CMD_RESET, 0x00);
571 DoC_WaitReady(docptr);
572 /* Set device to main plane of flash */
573 DoC_Command(docptr, NAND_CMD_READ0, 0x00);
575 /* issue the Serial Data In command to initial the Page Program process */
576 DoC_Command(docptr, NAND_CMD_SEQIN, 0x00);
577 DoC_Address(docptr, 3, to, 0x00, 0x00);
578 DoC_WaitReady(docptr);
581 /* init the ECC engine, see Reed-Solomon EDC/ECC 11.1 .*/
582 WriteDOC (DOC_ECC_RESET, docptr, ECCConf);
583 WriteDOC (DOC_ECC_EN | DOC_ECC_RW, docptr, ECCConf);
585 /* disable the ECC engine */
586 WriteDOC (DOC_ECC_RESET, docptr, ECCConf);
587 WriteDOC (DOC_ECC_DIS, docptr, ECCConf);
590 /* Write the data via the internal pipeline through CDSN IO register,
591 see Pipelined Write Operations 11.2 */
593 for (i = 0; i < len; i++) {
594 /* N.B. you have to increase the source address in this way or the
595 ECC logic will not work properly */
596 WriteDOC(buf[i], docptr, Mil_CDSN_IO + i);
599 memcpy_toio(docptr + DoC_Mil_CDSN_IO, buf, len);
601 WriteDOC(0x00, docptr, WritePipeTerm);
604 /* Write ECC data to flash, the ECC info is generated by the DiskOnChip ECC logic
605 see Reed-Solomon EDC/ECC 11.1 */
606 WriteDOC(0, docptr, NOP);
607 WriteDOC(0, docptr, NOP);
608 WriteDOC(0, docptr, NOP);
610 /* Read the ECC data through the DiskOnChip ECC logic */
611 for (i = 0; i < 6; i++) {
612 eccbuf[i] = ReadDOC(docptr, ECCSyndrome0 + i);
615 /* ignore the ECC engine */
616 WriteDOC(DOC_ECC_DIS, docptr , ECCConf);
619 /* Write the ECC data to flash */
620 for (i = 0; i < 6; i++) {
621 /* N.B. you have to increase the source address in this way or the
622 ECC logic will not work properly */
623 WriteDOC(eccbuf[i], docptr, Mil_CDSN_IO + i);
626 memcpy_toio(docptr + DoC_Mil_CDSN_IO, eccbuf, 6);
629 /* write the block status BLOCK_USED (0x5555) at the end of ECC data
630 FIXME: this is only a hack for programming the IPL area for LinuxBIOS
631 and should be replace with proper codes in user space utilities */
632 WriteDOC(0x55, docptr, Mil_CDSN_IO);
633 WriteDOC(0x55, docptr, Mil_CDSN_IO + 1);
635 WriteDOC(0x00, docptr, WritePipeTerm);
638 printk("OOB data at %lx is %2.2X %2.2X %2.2X %2.2X %2.2X %2.2X\n",
639 (long) to, eccbuf[0], eccbuf[1], eccbuf[2], eccbuf[3],
640 eccbuf[4], eccbuf[5]);
644 /* Commit the Page Program command and wait for ready
645 see Software Requirement 11.4 item 1.*/
646 DoC_Command(docptr, NAND_CMD_PAGEPROG, 0x00);
647 DoC_WaitReady(docptr);
649 /* Read the status of the flash device through CDSN IO register
650 see Software Requirement 11.4 item 5.*/
651 DoC_Command(docptr, NAND_CMD_STATUS, CDSN_CTRL_WP);
652 dummy = ReadDOC(docptr, ReadPipeInit);
653 DoC_Delay(docptr, 2);
654 if (ReadDOC(docptr, Mil_CDSN_IO) & 1) {
655 printk("Error programming flash\n");
656 /* Error in programming
657 FIXME: implement Bad Block Replacement (in nftl.c ??) */
661 dummy = ReadDOC(docptr, LastDataRead);
663 /* Let the caller know we completed it */
669 static int doc_read_oob(struct mtd_info *mtd, loff_t ofs, size_t len,
670 size_t *retlen, u_char *buf)
676 struct DiskOnChip *this = (struct DiskOnChip *)mtd->priv;
677 unsigned long docptr = this->virtadr;
678 struct Nand *mychip = &this->chips[ofs >> this->chipshift];
680 /* Find the chip which is to be used and select it */
681 if (this->curfloor != mychip->floor) {
682 DoC_SelectFloor(docptr, mychip->floor);
683 DoC_SelectChip(docptr, mychip->chip);
684 } else if (this->curchip != mychip->chip) {
685 DoC_SelectChip(docptr, mychip->chip);
687 this->curfloor = mychip->floor;
688 this->curchip = mychip->chip;
690 /* disable the ECC engine */
691 WriteDOC (DOC_ECC_RESET, docptr, ECCConf);
692 WriteDOC (DOC_ECC_DIS, docptr, ECCConf);
694 /* issue the Read2 command to set the pointer to the Spare Data Area.
695 Polling the Flash Ready bit after issue 3 bytes address in
696 Sequence Read Mode, see Software Requirement 11.4 item 1.*/
697 DoC_Command(docptr, NAND_CMD_READOOB, CDSN_CTRL_WP);
698 DoC_Address(docptr, 3, ofs, CDSN_CTRL_WP, 0x00);
699 DoC_WaitReady(docptr);
701 /* Read the data out via the internal pipeline through CDSN IO register,
702 see Pipelined Read Operations 11.3 */
703 dummy = ReadDOC(docptr, ReadPipeInit);
705 for (i = 0; i < len-1; i++) {
706 /* N.B. you have to increase the source address in this way or the
707 ECC logic will not work properly */
708 buf[i] = ReadDOC(docptr, Mil_CDSN_IO + i);
711 memcpy_fromio(buf, docptr + DoC_Mil_CDSN_IO, len - 1);
713 buf[len - 1] = ReadDOC(docptr, LastDataRead);
720 static int doc_write_oob(struct mtd_info *mtd, loff_t ofs, size_t len,
721 size_t *retlen, const u_char *buf)
728 struct DiskOnChip *this = (struct DiskOnChip *)mtd->priv;
729 unsigned long docptr = this->virtadr;
730 struct Nand *mychip = &this->chips[ofs >> this->chipshift];
732 /* Find the chip which is to be used and select it */
733 if (this->curfloor != mychip->floor) {
734 DoC_SelectFloor(docptr, mychip->floor);
735 DoC_SelectChip(docptr, mychip->chip);
736 } else if (this->curchip != mychip->chip) {
737 DoC_SelectChip(docptr, mychip->chip);
739 this->curfloor = mychip->floor;
740 this->curchip = mychip->chip;
742 /* disable the ECC engine */
743 WriteDOC (DOC_ECC_RESET, docptr, ECCConf);
744 WriteDOC (DOC_ECC_DIS, docptr, ECCConf);
746 /* Reset the chip, see Software Requirement 11.4 item 1. */
747 DoC_Command(docptr, NAND_CMD_RESET, CDSN_CTRL_WP);
748 DoC_WaitReady(docptr);
749 /* issue the Read2 command to set the pointer to the Spare Data Area. */
750 DoC_Command(docptr, NAND_CMD_READOOB, CDSN_CTRL_WP);
752 /* issue the Serial Data In command to initial the Page Program process */
753 DoC_Command(docptr, NAND_CMD_SEQIN, 0x00);
754 DoC_Address(docptr, 3, ofs, 0x00, 0x00);
756 /* Write the data via the internal pipeline through CDSN IO register,
757 see Pipelined Write Operations 11.2 */
759 for (i = 0; i < len; i++) {
760 /* N.B. you have to increase the source address in this way or the
761 ECC logic will not work properly */
762 WriteDOC(buf[i], docptr, Mil_CDSN_IO + i);
765 memcpy_toio(docptr + DoC_Mil_CDSN_IO, buf, len);
767 WriteDOC(0x00, docptr, WritePipeTerm);
769 /* Commit the Page Program command and wait for ready
770 see Software Requirement 11.4 item 1.*/
771 DoC_Command(docptr, NAND_CMD_PAGEPROG, 0x00);
772 DoC_WaitReady(docptr);
774 /* Read the status of the flash device through CDSN IO register
775 see Software Requirement 11.4 item 5.*/
776 DoC_Command(docptr, NAND_CMD_STATUS, 0x00);
777 dummy = ReadDOC(docptr, ReadPipeInit);
778 DoC_Delay(docptr, 2);
779 if (ReadDOC(docptr, Mil_CDSN_IO) & 1) {
780 printk("Error programming oob data\n");
781 /* FIXME: implement Bad Block Replacement (in nftl.c ??) */
785 dummy = ReadDOC(docptr, LastDataRead);
792 int doc_erase (struct mtd_info *mtd, struct erase_info *instr)
795 struct DiskOnChip *this = (struct DiskOnChip *)mtd->priv;
796 __u32 ofs = instr->addr;
797 __u32 len = instr->len;
798 unsigned long docptr = this->virtadr;
799 struct Nand *mychip = &this->chips[ofs >> this->chipshift];
801 if (len != mtd->erasesize)
802 printk(KERN_WARNING "Erase not right size (%x != %x)n",
803 len, mtd->erasesize);
805 /* Find the chip which is to be used and select it */
806 if (this->curfloor != mychip->floor) {
807 DoC_SelectFloor(docptr, mychip->floor);
808 DoC_SelectChip(docptr, mychip->chip);
809 } else if (this->curchip != mychip->chip) {
810 DoC_SelectChip(docptr, mychip->chip);
812 this->curfloor = mychip->floor;
813 this->curchip = mychip->chip;
815 instr->state = MTD_ERASE_PENDING;
817 /* issue the Erase Setup command */
818 DoC_Command(docptr, NAND_CMD_ERASE1, 0x00);
819 DoC_Address(docptr, 2, ofs, 0x00, 0x00);
821 /* Commit the Erase Start command and wait for ready
822 see Software Requirement 11.4 item 1.*/
823 DoC_Command(docptr, NAND_CMD_ERASE2, 0x00);
824 DoC_WaitReady(docptr);
826 instr->state = MTD_ERASING;
828 /* Read the status of the flash device through CDSN IO register
829 see Software Requirement 11.4 item 5.
830 FIXME: it seems that we are not wait long enough, some blocks are not
832 DoC_Command(docptr, NAND_CMD_STATUS, CDSN_CTRL_WP);
833 dummy = ReadDOC(docptr, ReadPipeInit);
834 DoC_Delay(docptr, 2);
835 if (ReadDOC(docptr, Mil_CDSN_IO) & 1) {
836 printk("Error Erasing at 0x%x\n", ofs);
837 /* There was an error
838 FIXME: implement Bad Block Replacement (in nftl.c ??) */
839 instr->state = MTD_ERASE_FAILED;
841 instr->state = MTD_ERASE_DONE;
842 dummy = ReadDOC(docptr, LastDataRead);
845 instr->callback(instr);
850 /****************************************************************************
854 ****************************************************************************/
856 int __init init_doc2001(void)
858 inter_module_register(im_name, THIS_MODULE, &DoCMil_init);
862 static void __exit cleanup_doc2001(void)
864 struct mtd_info *mtd;
865 struct DiskOnChip *this;
867 while ((mtd=docmillist)) {
868 this = (struct DiskOnChip *)mtd->priv;
869 docmillist = this->nextdoc;
873 iounmap((void *)this->virtadr);
877 inter_module_unregister(im_name);
880 module_exit(cleanup_doc2001);
881 module_init(init_doc2001);
883 MODULE_LICENSE("GPL");
884 MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org> et al.");
885 MODULE_DESCRIPTION("Alternative driver for DiskOnChip Millennium");