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.41 2003/06/11 09:45:19 dwmw2 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>
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_ecc(struct mtd_info *mtd, loff_t from, size_t len,
40 size_t *retlen, u_char *buf, u_char *eccbuf, int oobsel);
41 static int doc_write_ecc(struct mtd_info *mtd, loff_t to, size_t len,
42 size_t *retlen, const u_char *buf, u_char *eccbuf, int oobsel);
43 static int doc_read_oob(struct mtd_info *mtd, loff_t ofs, size_t len,
44 size_t *retlen, u_char *buf);
45 static int doc_write_oob(struct mtd_info *mtd, loff_t ofs, size_t len,
46 size_t *retlen, const u_char *buf);
47 static int doc_erase (struct mtd_info *mtd, struct erase_info *instr);
49 static struct mtd_info *docmillist = NULL;
51 /* Perform the required delay cycles by reading from the NOP register */
52 static void DoC_Delay(unsigned long docptr, unsigned short cycles)
57 for (i = 0; i < cycles; i++)
58 dummy = ReadDOC(docptr, NOP);
61 /* DOC_WaitReady: Wait for RDY line to be asserted by the flash chip */
62 static int _DoC_WaitReady(unsigned long docptr)
64 unsigned short c = 0xffff;
66 DEBUG(MTD_DEBUG_LEVEL3,
67 "_DoC_WaitReady called for out-of-line wait\n");
69 /* Out-of-line routine to wait for chip response */
70 while (!(ReadDOC(docptr, CDSNControl) & CDSN_CTRL_FR_B) && --c)
74 DEBUG(MTD_DEBUG_LEVEL2, "_DoC_WaitReady timed out.\n");
79 static inline int DoC_WaitReady(unsigned long docptr)
81 /* This is inline, to optimise the common case, where it's ready instantly */
84 /* 4 read form NOP register should be issued in prior to the read from CDSNControl
85 see Software Requirement 11.4 item 2. */
88 if (!(ReadDOC(docptr, CDSNControl) & CDSN_CTRL_FR_B))
89 /* Call the out-of-line routine to wait */
90 ret = _DoC_WaitReady(docptr);
92 /* issue 2 read from NOP register after reading from CDSNControl register
93 see Software Requirement 11.4 item 2. */
99 /* DoC_Command: Send a flash command to the flash chip through the CDSN IO register
100 with the internal pipeline. Each of 4 delay cycles (read from the NOP register) is
101 required after writing to CDSN Control register, see Software Requirement 11.4 item 3. */
103 static inline void DoC_Command(unsigned long docptr, unsigned char command,
104 unsigned char xtraflags)
106 /* Assert the CLE (Command Latch Enable) line to the flash chip */
107 WriteDOC(xtraflags | CDSN_CTRL_CLE | CDSN_CTRL_CE, docptr, CDSNControl);
108 DoC_Delay(docptr, 4);
110 /* Send the command */
111 WriteDOC(command, docptr, Mil_CDSN_IO);
112 WriteDOC(0x00, docptr, WritePipeTerm);
114 /* Lower the CLE line */
115 WriteDOC(xtraflags | CDSN_CTRL_CE, docptr, CDSNControl);
116 DoC_Delay(docptr, 4);
119 /* DoC_Address: Set the current address for the flash chip through the CDSN IO register
120 with the internal pipeline. Each of 4 delay cycles (read from the NOP register) is
121 required after writing to CDSN Control register, see Software Requirement 11.4 item 3. */
123 static inline void DoC_Address(unsigned long docptr, int numbytes, unsigned long ofs,
124 unsigned char xtraflags1, unsigned char xtraflags2)
126 /* Assert the ALE (Address Latch Enable) line to the flash chip */
127 WriteDOC(xtraflags1 | CDSN_CTRL_ALE | CDSN_CTRL_CE, docptr, CDSNControl);
128 DoC_Delay(docptr, 4);
130 /* Send the address */
134 /* Send single byte, bits 0-7. */
135 WriteDOC(ofs & 0xff, docptr, Mil_CDSN_IO);
136 WriteDOC(0x00, docptr, WritePipeTerm);
139 /* Send bits 9-16 followed by 17-23 */
140 WriteDOC((ofs >> 9) & 0xff, docptr, Mil_CDSN_IO);
141 WriteDOC((ofs >> 17) & 0xff, docptr, Mil_CDSN_IO);
142 WriteDOC(0x00, docptr, WritePipeTerm);
145 /* Send 0-7, 9-16, then 17-23 */
146 WriteDOC(ofs & 0xff, docptr, Mil_CDSN_IO);
147 WriteDOC((ofs >> 9) & 0xff, docptr, Mil_CDSN_IO);
148 WriteDOC((ofs >> 17) & 0xff, docptr, Mil_CDSN_IO);
149 WriteDOC(0x00, docptr, WritePipeTerm);
155 /* Lower the ALE line */
156 WriteDOC(xtraflags1 | xtraflags2 | CDSN_CTRL_CE, docptr, CDSNControl);
157 DoC_Delay(docptr, 4);
160 /* DoC_SelectChip: Select a given flash chip within the current floor */
161 static int DoC_SelectChip(unsigned long docptr, int chip)
163 /* Select the individual flash chip requested */
164 WriteDOC(chip, docptr, CDSNDeviceSelect);
165 DoC_Delay(docptr, 4);
167 /* Wait for it to be ready */
168 return DoC_WaitReady(docptr);
171 /* DoC_SelectFloor: Select a given floor (bank of flash chips) */
172 static int DoC_SelectFloor(unsigned long docptr, int floor)
174 /* Select the floor (bank) of chips required */
175 WriteDOC(floor, docptr, FloorSelect);
177 /* Wait for the chip to be ready */
178 return DoC_WaitReady(docptr);
181 /* DoC_IdentChip: Identify a given NAND chip given {floor,chip} */
182 static int DoC_IdentChip(struct DiskOnChip *doc, int floor, int chip)
187 /* Page in the required floor/chip
188 FIXME: is this supported by Millennium ?? */
189 DoC_SelectFloor(doc->virtadr, floor);
190 DoC_SelectChip(doc->virtadr, chip);
192 /* Reset the chip, see Software Requirement 11.4 item 1. */
193 DoC_Command(doc->virtadr, NAND_CMD_RESET, CDSN_CTRL_WP);
194 DoC_WaitReady(doc->virtadr);
196 /* Read the NAND chip ID: 1. Send ReadID command */
197 DoC_Command(doc->virtadr, NAND_CMD_READID, CDSN_CTRL_WP);
199 /* Read the NAND chip ID: 2. Send address byte zero */
200 DoC_Address(doc->virtadr, 1, 0x00, CDSN_CTRL_WP, 0x00);
202 /* Read the manufacturer and device id codes of the flash device through
203 CDSN IO register see Software Requirement 11.4 item 5.*/
204 dummy = ReadDOC(doc->virtadr, ReadPipeInit);
205 DoC_Delay(doc->virtadr, 2);
206 mfr = ReadDOC(doc->virtadr, Mil_CDSN_IO);
208 DoC_Delay(doc->virtadr, 2);
209 id = ReadDOC(doc->virtadr, Mil_CDSN_IO);
210 dummy = ReadDOC(doc->virtadr, LastDataRead);
212 /* No response - return failure */
213 if (mfr == 0xff || mfr == 0)
216 /* FIXME: to deal with multi-flash on multi-Millennium case more carefully */
217 for (i = 0; nand_flash_ids[i].name != NULL; i++) {
218 if ( id == nand_flash_ids[i].id) {
219 /* Try to identify manufacturer */
220 for (j = 0; nand_manuf_ids[j].id != 0x0; j++) {
221 if (nand_manuf_ids[j].id == mfr)
224 printk(KERN_INFO "Flash chip found: Manufacturer ID: %2.2X, "
225 "Chip ID: %2.2X (%s:%s)\n",
226 mfr, id, nand_manuf_ids[j].name, nand_flash_ids[i].name);
229 doc->chipshift = nand_flash_ids[i].chipshift;
234 if (nand_flash_ids[i].name == NULL)
240 /* DoC_ScanChips: Find all NAND chips present in a DiskOnChip, and identify them */
241 static void DoC_ScanChips(struct DiskOnChip *this)
244 int numchips[MAX_FLOORS_MIL];
251 /* For each floor, find the number of valid chips it contains */
252 for (floor = 0,ret = 1; floor < MAX_FLOORS_MIL; floor++) {
254 for (chip = 0; chip < MAX_CHIPS_MIL && ret != 0; chip++) {
255 ret = DoC_IdentChip(this, floor, chip);
262 /* If there are none at all that we recognise, bail */
263 if (!this->numchips) {
264 printk("No flash chips recognised.\n");
268 /* Allocate an array to hold the information for each chip */
269 this->chips = kmalloc(sizeof(struct Nand) * this->numchips, GFP_KERNEL);
271 printk("No memory for allocating chip info structures\n");
275 /* Fill out the chip array with {floor, chipno} for each
276 * detected chip in the device. */
277 for (floor = 0, ret = 0; floor < MAX_FLOORS_MIL; floor++) {
278 for (chip = 0 ; chip < numchips[floor] ; chip++) {
279 this->chips[ret].floor = floor;
280 this->chips[ret].chip = chip;
281 this->chips[ret].curadr = 0;
282 this->chips[ret].curmode = 0x50;
287 /* Calculate and print the total size of the device */
288 this->totlen = this->numchips * (1 << this->chipshift);
289 printk(KERN_INFO "%d flash chips found. Total DiskOnChip size: %ld MiB\n",
290 this->numchips ,this->totlen >> 20);
293 static int DoCMil_is_alias(struct DiskOnChip *doc1, struct DiskOnChip *doc2)
295 int tmp1, tmp2, retval;
297 if (doc1->physadr == doc2->physadr)
300 /* Use the alias resolution register which was set aside for this
301 * purpose. If it's value is the same on both chips, they might
302 * be the same chip, and we write to one and check for a change in
303 * the other. It's unclear if this register is usuable in the
304 * DoC 2000 (it's in the Millenium docs), but it seems to work. */
305 tmp1 = ReadDOC(doc1->virtadr, AliasResolution);
306 tmp2 = ReadDOC(doc2->virtadr, AliasResolution);
310 WriteDOC((tmp1+1) % 0xff, doc1->virtadr, AliasResolution);
311 tmp2 = ReadDOC(doc2->virtadr, AliasResolution);
312 if (tmp2 == (tmp1+1) % 0xff)
317 /* Restore register contents. May not be necessary, but do it just to
319 WriteDOC(tmp1, doc1->virtadr, AliasResolution);
324 static const char im_name[] = "DoCMil_init";
326 /* This routine is made available to other mtd code via
327 * inter_module_register. It must only be accessed through
328 * inter_module_get which will bump the use count of this module. The
329 * addresses passed back in mtd are valid as long as the use count of
330 * this module is non-zero, i.e. between inter_module_get and
331 * inter_module_put. Keith Owens <kaos@ocs.com.au> 29 Oct 2000.
333 static void DoCMil_init(struct mtd_info *mtd)
335 struct DiskOnChip *this = (struct DiskOnChip *)mtd->priv;
336 struct DiskOnChip *old = NULL;
338 /* We must avoid being called twice for the same device. */
340 old = (struct DiskOnChip *)docmillist->priv;
343 if (DoCMil_is_alias(this, old)) {
344 printk(KERN_NOTICE "Ignoring DiskOnChip Millennium at "
345 "0x%lX - already configured\n", this->physadr);
346 iounmap((void *)this->virtadr);
351 old = (struct DiskOnChip *)old->nextdoc->priv;
356 mtd->name = "DiskOnChip Millennium";
357 printk(KERN_NOTICE "DiskOnChip Millennium found at address 0x%lX\n",
360 mtd->type = MTD_NANDFLASH;
361 mtd->flags = MTD_CAP_NANDFLASH;
362 mtd->ecctype = MTD_ECC_RS_DiskOnChip;
365 /* FIXME: erase size is not always 8KiB */
366 mtd->erasesize = 0x2000;
370 mtd->owner = THIS_MODULE;
371 mtd->erase = doc_erase;
374 mtd->read = doc_read;
375 mtd->write = doc_write;
376 mtd->read_ecc = doc_read_ecc;
377 mtd->write_ecc = doc_write_ecc;
378 mtd->read_oob = doc_read_oob;
379 mtd->write_oob = doc_write_oob;
387 /* Ident all the chips present. */
392 iounmap((void *)this->virtadr);
394 this->nextdoc = docmillist;
396 mtd->size = this->totlen;
402 static int doc_read (struct mtd_info *mtd, loff_t from, size_t len,
403 size_t *retlen, u_char *buf)
405 /* Just a special case of doc_read_ecc */
406 return doc_read_ecc(mtd, from, len, retlen, buf, NULL, 0);
409 static int doc_read_ecc (struct mtd_info *mtd, loff_t from, size_t len,
410 size_t *retlen, u_char *buf, u_char *eccbuf, int oobsel)
414 unsigned char syndrome[6];
415 struct DiskOnChip *this = (struct DiskOnChip *)mtd->priv;
416 unsigned long docptr = this->virtadr;
417 struct Nand *mychip = &this->chips[from >> (this->chipshift)];
419 /* Don't allow read past end of device */
420 if (from >= this->totlen)
423 /* Don't allow a single read to cross a 512-byte block boundary */
424 if (from + len > ((from | 0x1ff) + 1))
425 len = ((from | 0x1ff) + 1) - from;
427 /* Find the chip which is to be used and select it */
428 if (this->curfloor != mychip->floor) {
429 DoC_SelectFloor(docptr, mychip->floor);
430 DoC_SelectChip(docptr, mychip->chip);
431 } else if (this->curchip != mychip->chip) {
432 DoC_SelectChip(docptr, mychip->chip);
434 this->curfloor = mychip->floor;
435 this->curchip = mychip->chip;
437 /* issue the Read0 or Read1 command depend on which half of the page
438 we are accessing. Polling the Flash Ready bit after issue 3 bytes
439 address in Sequence Read Mode, see Software Requirement 11.4 item 1.*/
440 DoC_Command(docptr, (from >> 8) & 1, CDSN_CTRL_WP);
441 DoC_Address(docptr, 3, from, CDSN_CTRL_WP, 0x00);
442 DoC_WaitReady(docptr);
445 /* init the ECC engine, see Reed-Solomon EDC/ECC 11.1 .*/
446 WriteDOC (DOC_ECC_RESET, docptr, ECCConf);
447 WriteDOC (DOC_ECC_EN, docptr, ECCConf);
449 /* disable the ECC engine */
450 WriteDOC (DOC_ECC_RESET, docptr, ECCConf);
451 WriteDOC (DOC_ECC_DIS, docptr, ECCConf);
454 /* Read the data via the internal pipeline through CDSN IO register,
455 see Pipelined Read Operations 11.3 */
456 dummy = ReadDOC(docptr, ReadPipeInit);
458 for (i = 0; i < len-1; i++) {
459 /* N.B. you have to increase the source address in this way or the
460 ECC logic will not work properly */
461 buf[i] = ReadDOC(docptr, Mil_CDSN_IO + (i & 0xff));
464 memcpy_fromio(buf, docptr + DoC_Mil_CDSN_IO, len - 1);
466 buf[len - 1] = ReadDOC(docptr, LastDataRead);
468 /* Let the caller know we completed it */
473 /* Read the ECC data from Spare Data Area,
474 see Reed-Solomon EDC/ECC 11.1 */
475 dummy = ReadDOC(docptr, ReadPipeInit);
477 for (i = 0; i < 5; i++) {
478 /* N.B. you have to increase the source address in this way or the
479 ECC logic will not work properly */
480 eccbuf[i] = ReadDOC(docptr, Mil_CDSN_IO + i);
483 memcpy_fromio(eccbuf, docptr + DoC_Mil_CDSN_IO, 5);
485 eccbuf[5] = ReadDOC(docptr, LastDataRead);
487 /* Flush the pipeline */
488 dummy = ReadDOC(docptr, ECCConf);
489 dummy = ReadDOC(docptr, ECCConf);
491 /* Check the ECC Status */
492 if (ReadDOC(docptr, ECCConf) & 0x80) {
494 /* There was an ECC error */
496 printk("DiskOnChip ECC Error: Read at %lx\n", (long)from);
498 /* Read the ECC syndrom through the DiskOnChip ECC logic.
499 These syndrome will be all ZERO when there is no error */
500 for (i = 0; i < 6; i++) {
501 syndrome[i] = ReadDOC(docptr, ECCSyndrome0 + i);
503 nb_errors = doc_decode_ecc(buf, syndrome);
505 printk("ECC Errors corrected: %x\n", nb_errors);
508 /* We return error, but have actually done the read. Not that
509 this can be told to user-space, via sys_read(), but at least
510 MTD-aware stuff can know about it by checking *retlen */
516 printk("ECC DATA at %lx: %2.2X %2.2X %2.2X %2.2X %2.2X %2.2X\n",
517 (long)from, eccbuf[0], eccbuf[1], eccbuf[2], eccbuf[3],
518 eccbuf[4], eccbuf[5]);
521 /* disable the ECC engine */
522 WriteDOC(DOC_ECC_DIS, docptr , ECCConf);
528 static int doc_write (struct mtd_info *mtd, loff_t to, size_t len,
529 size_t *retlen, const u_char *buf)
532 return doc_write_ecc(mtd, to, len, retlen, buf, eccbuf, 0);
535 static int doc_write_ecc (struct mtd_info *mtd, loff_t to, size_t len,
536 size_t *retlen, const u_char *buf, u_char *eccbuf, int oobsel)
540 struct DiskOnChip *this = (struct DiskOnChip *)mtd->priv;
541 unsigned long docptr = this->virtadr;
542 struct Nand *mychip = &this->chips[to >> (this->chipshift)];
544 /* Don't allow write past end of device */
545 if (to >= this->totlen)
549 /* Don't allow a single write to cross a 512-byte block boundary */
550 if (to + len > ( (to | 0x1ff) + 1))
551 len = ((to | 0x1ff) + 1) - to;
553 /* Don't allow writes which aren't exactly one block */
554 if (to & 0x1ff || len != 0x200)
558 /* Find the chip which is to be used and select it */
559 if (this->curfloor != mychip->floor) {
560 DoC_SelectFloor(docptr, mychip->floor);
561 DoC_SelectChip(docptr, mychip->chip);
562 } else if (this->curchip != mychip->chip) {
563 DoC_SelectChip(docptr, mychip->chip);
565 this->curfloor = mychip->floor;
566 this->curchip = mychip->chip;
568 /* Reset the chip, see Software Requirement 11.4 item 1. */
569 DoC_Command(docptr, NAND_CMD_RESET, 0x00);
570 DoC_WaitReady(docptr);
571 /* Set device to main plane of flash */
572 DoC_Command(docptr, NAND_CMD_READ0, 0x00);
574 /* issue the Serial Data In command to initial the Page Program process */
575 DoC_Command(docptr, NAND_CMD_SEQIN, 0x00);
576 DoC_Address(docptr, 3, to, 0x00, 0x00);
577 DoC_WaitReady(docptr);
580 /* init the ECC engine, see Reed-Solomon EDC/ECC 11.1 .*/
581 WriteDOC (DOC_ECC_RESET, docptr, ECCConf);
582 WriteDOC (DOC_ECC_EN | DOC_ECC_RW, docptr, ECCConf);
584 /* disable the ECC engine */
585 WriteDOC (DOC_ECC_RESET, docptr, ECCConf);
586 WriteDOC (DOC_ECC_DIS, docptr, ECCConf);
589 /* Write the data via the internal pipeline through CDSN IO register,
590 see Pipelined Write Operations 11.2 */
592 for (i = 0; i < len; i++) {
593 /* N.B. you have to increase the source address in this way or the
594 ECC logic will not work properly */
595 WriteDOC(buf[i], docptr, Mil_CDSN_IO + i);
598 memcpy_toio(docptr + DoC_Mil_CDSN_IO, buf, len);
600 WriteDOC(0x00, docptr, WritePipeTerm);
603 /* Write ECC data to flash, the ECC info is generated by the DiskOnChip ECC logic
604 see Reed-Solomon EDC/ECC 11.1 */
605 WriteDOC(0, docptr, NOP);
606 WriteDOC(0, docptr, NOP);
607 WriteDOC(0, docptr, NOP);
609 /* Read the ECC data through the DiskOnChip ECC logic */
610 for (i = 0; i < 6; i++) {
611 eccbuf[i] = ReadDOC(docptr, ECCSyndrome0 + i);
614 /* ignore the ECC engine */
615 WriteDOC(DOC_ECC_DIS, docptr , ECCConf);
618 /* Write the ECC data to flash */
619 for (i = 0; i < 6; i++) {
620 /* N.B. you have to increase the source address in this way or the
621 ECC logic will not work properly */
622 WriteDOC(eccbuf[i], docptr, Mil_CDSN_IO + i);
625 memcpy_toio(docptr + DoC_Mil_CDSN_IO, eccbuf, 6);
628 /* write the block status BLOCK_USED (0x5555) at the end of ECC data
629 FIXME: this is only a hack for programming the IPL area for LinuxBIOS
630 and should be replace with proper codes in user space utilities */
631 WriteDOC(0x55, docptr, Mil_CDSN_IO);
632 WriteDOC(0x55, docptr, Mil_CDSN_IO + 1);
634 WriteDOC(0x00, docptr, WritePipeTerm);
637 printk("OOB data at %lx is %2.2X %2.2X %2.2X %2.2X %2.2X %2.2X\n",
638 (long) to, eccbuf[0], eccbuf[1], eccbuf[2], eccbuf[3],
639 eccbuf[4], eccbuf[5]);
643 /* Commit the Page Program command and wait for ready
644 see Software Requirement 11.4 item 1.*/
645 DoC_Command(docptr, NAND_CMD_PAGEPROG, 0x00);
646 DoC_WaitReady(docptr);
648 /* Read the status of the flash device through CDSN IO register
649 see Software Requirement 11.4 item 5.*/
650 DoC_Command(docptr, NAND_CMD_STATUS, CDSN_CTRL_WP);
651 dummy = ReadDOC(docptr, ReadPipeInit);
652 DoC_Delay(docptr, 2);
653 if (ReadDOC(docptr, Mil_CDSN_IO) & 1) {
654 printk("Error programming flash\n");
655 /* Error in programming
656 FIXME: implement Bad Block Replacement (in nftl.c ??) */
660 dummy = ReadDOC(docptr, LastDataRead);
662 /* Let the caller know we completed it */
668 static int doc_read_oob(struct mtd_info *mtd, loff_t ofs, size_t len,
669 size_t *retlen, u_char *buf)
675 struct DiskOnChip *this = (struct DiskOnChip *)mtd->priv;
676 unsigned long docptr = this->virtadr;
677 struct Nand *mychip = &this->chips[ofs >> this->chipshift];
679 /* Find the chip which is to be used and select it */
680 if (this->curfloor != mychip->floor) {
681 DoC_SelectFloor(docptr, mychip->floor);
682 DoC_SelectChip(docptr, mychip->chip);
683 } else if (this->curchip != mychip->chip) {
684 DoC_SelectChip(docptr, mychip->chip);
686 this->curfloor = mychip->floor;
687 this->curchip = mychip->chip;
689 /* disable the ECC engine */
690 WriteDOC (DOC_ECC_RESET, docptr, ECCConf);
691 WriteDOC (DOC_ECC_DIS, docptr, ECCConf);
693 /* issue the Read2 command to set the pointer to the Spare Data Area.
694 Polling the Flash Ready bit after issue 3 bytes address in
695 Sequence Read Mode, see Software Requirement 11.4 item 1.*/
696 DoC_Command(docptr, NAND_CMD_READOOB, CDSN_CTRL_WP);
697 DoC_Address(docptr, 3, ofs, CDSN_CTRL_WP, 0x00);
698 DoC_WaitReady(docptr);
700 /* Read the data out via the internal pipeline through CDSN IO register,
701 see Pipelined Read Operations 11.3 */
702 dummy = ReadDOC(docptr, ReadPipeInit);
704 for (i = 0; i < len-1; i++) {
705 /* N.B. you have to increase the source address in this way or the
706 ECC logic will not work properly */
707 buf[i] = ReadDOC(docptr, Mil_CDSN_IO + i);
710 memcpy_fromio(buf, docptr + DoC_Mil_CDSN_IO, len - 1);
712 buf[len - 1] = ReadDOC(docptr, LastDataRead);
719 static int doc_write_oob(struct mtd_info *mtd, loff_t ofs, size_t len,
720 size_t *retlen, const u_char *buf)
727 struct DiskOnChip *this = (struct DiskOnChip *)mtd->priv;
728 unsigned long docptr = this->virtadr;
729 struct Nand *mychip = &this->chips[ofs >> this->chipshift];
731 /* Find the chip which is to be used and select it */
732 if (this->curfloor != mychip->floor) {
733 DoC_SelectFloor(docptr, mychip->floor);
734 DoC_SelectChip(docptr, mychip->chip);
735 } else if (this->curchip != mychip->chip) {
736 DoC_SelectChip(docptr, mychip->chip);
738 this->curfloor = mychip->floor;
739 this->curchip = mychip->chip;
741 /* disable the ECC engine */
742 WriteDOC (DOC_ECC_RESET, docptr, ECCConf);
743 WriteDOC (DOC_ECC_DIS, docptr, ECCConf);
745 /* Reset the chip, see Software Requirement 11.4 item 1. */
746 DoC_Command(docptr, NAND_CMD_RESET, CDSN_CTRL_WP);
747 DoC_WaitReady(docptr);
748 /* issue the Read2 command to set the pointer to the Spare Data Area. */
749 DoC_Command(docptr, NAND_CMD_READOOB, CDSN_CTRL_WP);
751 /* issue the Serial Data In command to initial the Page Program process */
752 DoC_Command(docptr, NAND_CMD_SEQIN, 0x00);
753 DoC_Address(docptr, 3, ofs, 0x00, 0x00);
755 /* Write the data via the internal pipeline through CDSN IO register,
756 see Pipelined Write Operations 11.2 */
758 for (i = 0; i < len; i++) {
759 /* N.B. you have to increase the source address in this way or the
760 ECC logic will not work properly */
761 WriteDOC(buf[i], docptr, Mil_CDSN_IO + i);
764 memcpy_toio(docptr + DoC_Mil_CDSN_IO, buf, len);
766 WriteDOC(0x00, docptr, WritePipeTerm);
768 /* Commit the Page Program command and wait for ready
769 see Software Requirement 11.4 item 1.*/
770 DoC_Command(docptr, NAND_CMD_PAGEPROG, 0x00);
771 DoC_WaitReady(docptr);
773 /* Read the status of the flash device through CDSN IO register
774 see Software Requirement 11.4 item 5.*/
775 DoC_Command(docptr, NAND_CMD_STATUS, 0x00);
776 dummy = ReadDOC(docptr, ReadPipeInit);
777 DoC_Delay(docptr, 2);
778 if (ReadDOC(docptr, Mil_CDSN_IO) & 1) {
779 printk("Error programming oob data\n");
780 /* FIXME: implement Bad Block Replacement (in nftl.c ??) */
784 dummy = ReadDOC(docptr, LastDataRead);
791 int doc_erase (struct mtd_info *mtd, struct erase_info *instr)
794 struct DiskOnChip *this = (struct DiskOnChip *)mtd->priv;
795 __u32 ofs = instr->addr;
796 __u32 len = instr->len;
797 unsigned long docptr = this->virtadr;
798 struct Nand *mychip = &this->chips[ofs >> this->chipshift];
800 if (len != mtd->erasesize)
801 printk(KERN_WARNING "Erase not right size (%x != %x)n",
802 len, mtd->erasesize);
804 /* Find the chip which is to be used and select it */
805 if (this->curfloor != mychip->floor) {
806 DoC_SelectFloor(docptr, mychip->floor);
807 DoC_SelectChip(docptr, mychip->chip);
808 } else if (this->curchip != mychip->chip) {
809 DoC_SelectChip(docptr, mychip->chip);
811 this->curfloor = mychip->floor;
812 this->curchip = mychip->chip;
814 instr->state = MTD_ERASE_PENDING;
816 /* issue the Erase Setup command */
817 DoC_Command(docptr, NAND_CMD_ERASE1, 0x00);
818 DoC_Address(docptr, 2, ofs, 0x00, 0x00);
820 /* Commit the Erase Start command and wait for ready
821 see Software Requirement 11.4 item 1.*/
822 DoC_Command(docptr, NAND_CMD_ERASE2, 0x00);
823 DoC_WaitReady(docptr);
825 instr->state = MTD_ERASING;
827 /* Read the status of the flash device through CDSN IO register
828 see Software Requirement 11.4 item 5.
829 FIXME: it seems that we are not wait long enough, some blocks are not
831 DoC_Command(docptr, NAND_CMD_STATUS, CDSN_CTRL_WP);
832 dummy = ReadDOC(docptr, ReadPipeInit);
833 DoC_Delay(docptr, 2);
834 if (ReadDOC(docptr, Mil_CDSN_IO) & 1) {
835 printk("Error Erasing at 0x%x\n", ofs);
836 /* There was an error
837 FIXME: implement Bad Block Replacement (in nftl.c ??) */
838 instr->state = MTD_ERASE_FAILED;
840 instr->state = MTD_ERASE_DONE;
841 dummy = ReadDOC(docptr, LastDataRead);
844 instr->callback(instr);
849 /****************************************************************************
853 ****************************************************************************/
855 int __init init_doc2001(void)
857 inter_module_register(im_name, THIS_MODULE, &DoCMil_init);
861 static void __exit cleanup_doc2001(void)
863 struct mtd_info *mtd;
864 struct DiskOnChip *this;
866 while ((mtd=docmillist)) {
867 this = (struct DiskOnChip *)mtd->priv;
868 docmillist = this->nextdoc;
872 iounmap((void *)this->virtadr);
876 inter_module_unregister(im_name);
879 module_exit(cleanup_doc2001);
880 module_init(init_doc2001);
882 MODULE_LICENSE("GPL");
883 MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org> et al.");
884 MODULE_DESCRIPTION("Alternative driver for DiskOnChip Millennium");