GUI: Fix Tomato RAF theme for all builds. Compilation typo.
[tomato.git] / release / src-rt-6.x.4708 / linux / linux-2.6.36 / drivers / mtd / devices / doc2001.c
blob7173ff0daa7eeb4d2b5036052623d14981a5cb19
2 /*
3 * Linux driver for Disk-On-Chip Millennium
4 * (c) 1999 Machine Vision Holdings, Inc.
5 * (c) 1999, 2000 David Woodhouse <dwmw2@infradead.org>
6 */
8 #include <linux/kernel.h>
9 #include <linux/module.h>
10 #include <asm/errno.h>
11 #include <asm/io.h>
12 #include <asm/uaccess.h>
13 #include <linux/delay.h>
14 #include <linux/slab.h>
15 #include <linux/init.h>
16 #include <linux/types.h>
17 #include <linux/bitops.h>
19 #include <linux/mtd/mtd.h>
20 #include <linux/mtd/nand.h>
21 #include <linux/mtd/doc2000.h>
23 /* #define ECC_DEBUG */
25 /* I have no idea why some DoC chips can not use memcop_form|to_io().
26 * This may be due to the different revisions of the ASIC controller built-in or
27 * simplily a QA/Bug issue. Who knows ?? If you have trouble, please uncomment
28 * this:*/
29 #undef USE_MEMCPY
31 static int doc_read(struct mtd_info *mtd, loff_t from, size_t len,
32 size_t *retlen, u_char *buf);
33 static int doc_write(struct mtd_info *mtd, loff_t to, size_t len,
34 size_t *retlen, const u_char *buf);
35 static int doc_read_oob(struct mtd_info *mtd, loff_t ofs,
36 struct mtd_oob_ops *ops);
37 static int doc_write_oob(struct mtd_info *mtd, loff_t ofs,
38 struct mtd_oob_ops *ops);
39 static int doc_erase (struct mtd_info *mtd, struct erase_info *instr);
41 static struct mtd_info *docmillist = NULL;
43 /* Perform the required delay cycles by reading from the NOP register */
44 static void DoC_Delay(void __iomem * docptr, unsigned short cycles)
46 volatile char dummy;
47 int i;
49 for (i = 0; i < cycles; i++)
50 dummy = ReadDOC(docptr, NOP);
53 /* DOC_WaitReady: Wait for RDY line to be asserted by the flash chip */
54 static int _DoC_WaitReady(void __iomem * docptr)
56 unsigned short c = 0xffff;
58 DEBUG(MTD_DEBUG_LEVEL3,
59 "_DoC_WaitReady called for out-of-line wait\n");
61 /* Out-of-line routine to wait for chip response */
62 while (!(ReadDOC(docptr, CDSNControl) & CDSN_CTRL_FR_B) && --c)
65 if (c == 0)
66 DEBUG(MTD_DEBUG_LEVEL2, "_DoC_WaitReady timed out.\n");
68 return (c == 0);
71 static inline int DoC_WaitReady(void __iomem * docptr)
73 /* This is inline, to optimise the common case, where it's ready instantly */
74 int ret = 0;
76 /* 4 read form NOP register should be issued in prior to the read from CDSNControl
77 see Software Requirement 11.4 item 2. */
78 DoC_Delay(docptr, 4);
80 if (!(ReadDOC(docptr, CDSNControl) & CDSN_CTRL_FR_B))
81 /* Call the out-of-line routine to wait */
82 ret = _DoC_WaitReady(docptr);
84 /* issue 2 read from NOP register after reading from CDSNControl register
85 see Software Requirement 11.4 item 2. */
86 DoC_Delay(docptr, 2);
88 return ret;
91 /* DoC_Command: Send a flash command to the flash chip through the CDSN IO register
92 with the internal pipeline. Each of 4 delay cycles (read from the NOP register) is
93 required after writing to CDSN Control register, see Software Requirement 11.4 item 3. */
95 static void DoC_Command(void __iomem * docptr, unsigned char command,
96 unsigned char xtraflags)
98 /* Assert the CLE (Command Latch Enable) line to the flash chip */
99 WriteDOC(xtraflags | CDSN_CTRL_CLE | CDSN_CTRL_CE, docptr, CDSNControl);
100 DoC_Delay(docptr, 4);
102 /* Send the command */
103 WriteDOC(command, docptr, Mil_CDSN_IO);
104 WriteDOC(0x00, docptr, WritePipeTerm);
106 /* Lower the CLE line */
107 WriteDOC(xtraflags | CDSN_CTRL_CE, docptr, CDSNControl);
108 DoC_Delay(docptr, 4);
111 /* DoC_Address: Set the current address for the flash chip through the CDSN IO register
112 with the internal pipeline. Each of 4 delay cycles (read from the NOP register) is
113 required after writing to CDSN Control register, see Software Requirement 11.4 item 3. */
115 static inline void DoC_Address(void __iomem * docptr, int numbytes, unsigned long ofs,
116 unsigned char xtraflags1, unsigned char xtraflags2)
118 /* Assert the ALE (Address Latch Enable) line to the flash chip */
119 WriteDOC(xtraflags1 | CDSN_CTRL_ALE | CDSN_CTRL_CE, docptr, CDSNControl);
120 DoC_Delay(docptr, 4);
122 /* Send the address */
123 switch (numbytes)
125 case 1:
126 /* Send single byte, bits 0-7. */
127 WriteDOC(ofs & 0xff, docptr, Mil_CDSN_IO);
128 WriteDOC(0x00, docptr, WritePipeTerm);
129 break;
130 case 2:
131 /* Send bits 9-16 followed by 17-23 */
132 WriteDOC((ofs >> 9) & 0xff, docptr, Mil_CDSN_IO);
133 WriteDOC((ofs >> 17) & 0xff, docptr, Mil_CDSN_IO);
134 WriteDOC(0x00, docptr, WritePipeTerm);
135 break;
136 case 3:
137 /* Send 0-7, 9-16, then 17-23 */
138 WriteDOC(ofs & 0xff, docptr, Mil_CDSN_IO);
139 WriteDOC((ofs >> 9) & 0xff, docptr, Mil_CDSN_IO);
140 WriteDOC((ofs >> 17) & 0xff, docptr, Mil_CDSN_IO);
141 WriteDOC(0x00, docptr, WritePipeTerm);
142 break;
143 default:
144 return;
147 /* Lower the ALE line */
148 WriteDOC(xtraflags1 | xtraflags2 | CDSN_CTRL_CE, docptr, CDSNControl);
149 DoC_Delay(docptr, 4);
152 /* DoC_SelectChip: Select a given flash chip within the current floor */
153 static int DoC_SelectChip(void __iomem * docptr, int chip)
155 /* Select the individual flash chip requested */
156 WriteDOC(chip, docptr, CDSNDeviceSelect);
157 DoC_Delay(docptr, 4);
159 /* Wait for it to be ready */
160 return DoC_WaitReady(docptr);
163 /* DoC_SelectFloor: Select a given floor (bank of flash chips) */
164 static int DoC_SelectFloor(void __iomem * docptr, int floor)
166 /* Select the floor (bank) of chips required */
167 WriteDOC(floor, docptr, FloorSelect);
169 /* Wait for the chip to be ready */
170 return DoC_WaitReady(docptr);
173 /* DoC_IdentChip: Identify a given NAND chip given {floor,chip} */
174 static int DoC_IdentChip(struct DiskOnChip *doc, int floor, int chip)
176 int mfr, id, i, j;
177 volatile char dummy;
179 DoC_SelectFloor(doc->virtadr, floor);
180 DoC_SelectChip(doc->virtadr, chip);
182 /* Reset the chip, see Software Requirement 11.4 item 1. */
183 DoC_Command(doc->virtadr, NAND_CMD_RESET, CDSN_CTRL_WP);
184 DoC_WaitReady(doc->virtadr);
186 /* Read the NAND chip ID: 1. Send ReadID command */
187 DoC_Command(doc->virtadr, NAND_CMD_READID, CDSN_CTRL_WP);
189 /* Read the NAND chip ID: 2. Send address byte zero */
190 DoC_Address(doc->virtadr, 1, 0x00, CDSN_CTRL_WP, 0x00);
192 /* Read the manufacturer and device id codes of the flash device through
193 CDSN IO register see Software Requirement 11.4 item 5.*/
194 dummy = ReadDOC(doc->virtadr, ReadPipeInit);
195 DoC_Delay(doc->virtadr, 2);
196 mfr = ReadDOC(doc->virtadr, Mil_CDSN_IO);
198 DoC_Delay(doc->virtadr, 2);
199 id = ReadDOC(doc->virtadr, Mil_CDSN_IO);
200 dummy = ReadDOC(doc->virtadr, LastDataRead);
202 /* No response - return failure */
203 if (mfr == 0xff || mfr == 0)
204 return 0;
206 for (i = 0; nand_flash_ids[i].name != NULL; i++) {
207 if ( id == nand_flash_ids[i].id) {
208 /* Try to identify manufacturer */
209 for (j = 0; nand_manuf_ids[j].id != 0x0; j++) {
210 if (nand_manuf_ids[j].id == mfr)
211 break;
213 printk(KERN_INFO "Flash chip found: Manufacturer ID: %2.2X, "
214 "Chip ID: %2.2X (%s:%s)\n",
215 mfr, id, nand_manuf_ids[j].name, nand_flash_ids[i].name);
216 doc->mfr = mfr;
217 doc->id = id;
218 doc->chipshift = ffs((nand_flash_ids[i].chipsize << 20)) - 1;
219 break;
223 if (nand_flash_ids[i].name == NULL)
224 return 0;
225 else
226 return 1;
229 /* DoC_ScanChips: Find all NAND chips present in a DiskOnChip, and identify them */
230 static void DoC_ScanChips(struct DiskOnChip *this)
232 int floor, chip;
233 int numchips[MAX_FLOORS_MIL];
234 int ret;
236 this->numchips = 0;
237 this->mfr = 0;
238 this->id = 0;
240 /* For each floor, find the number of valid chips it contains */
241 for (floor = 0,ret = 1; floor < MAX_FLOORS_MIL; floor++) {
242 numchips[floor] = 0;
243 for (chip = 0; chip < MAX_CHIPS_MIL && ret != 0; chip++) {
244 ret = DoC_IdentChip(this, floor, chip);
245 if (ret) {
246 numchips[floor]++;
247 this->numchips++;
251 /* If there are none at all that we recognise, bail */
252 if (!this->numchips) {
253 printk("No flash chips recognised.\n");
254 return;
257 /* Allocate an array to hold the information for each chip */
258 this->chips = kmalloc(sizeof(struct Nand) * this->numchips, GFP_KERNEL);
259 if (!this->chips){
260 printk("No memory for allocating chip info structures\n");
261 return;
264 /* Fill out the chip array with {floor, chipno} for each
265 * detected chip in the device. */
266 for (floor = 0, ret = 0; floor < MAX_FLOORS_MIL; floor++) {
267 for (chip = 0 ; chip < numchips[floor] ; chip++) {
268 this->chips[ret].floor = floor;
269 this->chips[ret].chip = chip;
270 this->chips[ret].curadr = 0;
271 this->chips[ret].curmode = 0x50;
272 ret++;
276 /* Calculate and print the total size of the device */
277 this->totlen = this->numchips * (1 << this->chipshift);
278 printk(KERN_INFO "%d flash chips found. Total DiskOnChip size: %ld MiB\n",
279 this->numchips ,this->totlen >> 20);
282 static int DoCMil_is_alias(struct DiskOnChip *doc1, struct DiskOnChip *doc2)
284 int tmp1, tmp2, retval;
286 if (doc1->physadr == doc2->physadr)
287 return 1;
289 /* Use the alias resolution register which was set aside for this
290 * purpose. If it's value is the same on both chips, they might
291 * be the same chip, and we write to one and check for a change in
292 * the other. It's unclear if this register is usuable in the
293 * DoC 2000 (it's in the Millenium docs), but it seems to work. */
294 tmp1 = ReadDOC(doc1->virtadr, AliasResolution);
295 tmp2 = ReadDOC(doc2->virtadr, AliasResolution);
296 if (tmp1 != tmp2)
297 return 0;
299 WriteDOC((tmp1+1) % 0xff, doc1->virtadr, AliasResolution);
300 tmp2 = ReadDOC(doc2->virtadr, AliasResolution);
301 if (tmp2 == (tmp1+1) % 0xff)
302 retval = 1;
303 else
304 retval = 0;
306 /* Restore register contents. May not be necessary, but do it just to
307 * be safe. */
308 WriteDOC(tmp1, doc1->virtadr, AliasResolution);
310 return retval;
313 /* This routine is found from the docprobe code by symbol_get(),
314 * which will bump the use count of this module. */
315 void DoCMil_init(struct mtd_info *mtd)
317 struct DiskOnChip *this = mtd->priv;
318 struct DiskOnChip *old = NULL;
320 /* We must avoid being called twice for the same device. */
321 if (docmillist)
322 old = docmillist->priv;
324 while (old) {
325 if (DoCMil_is_alias(this, old)) {
326 printk(KERN_NOTICE "Ignoring DiskOnChip Millennium at "
327 "0x%lX - already configured\n", this->physadr);
328 iounmap(this->virtadr);
329 kfree(mtd);
330 return;
332 if (old->nextdoc)
333 old = old->nextdoc->priv;
334 else
335 old = NULL;
338 mtd->name = "DiskOnChip Millennium";
339 printk(KERN_NOTICE "DiskOnChip Millennium found at address 0x%lX\n",
340 this->physadr);
342 mtd->type = MTD_NANDFLASH;
343 mtd->flags = MTD_CAP_NANDFLASH;
344 mtd->size = 0;
346 mtd->erasesize = 0x2000;
348 mtd->writesize = 512;
349 mtd->oobsize = 16;
350 mtd->owner = THIS_MODULE;
351 mtd->erase = doc_erase;
352 mtd->point = NULL;
353 mtd->unpoint = NULL;
354 mtd->read = doc_read;
355 mtd->write = doc_write;
356 mtd->read_oob = doc_read_oob;
357 mtd->write_oob = doc_write_oob;
358 mtd->sync = NULL;
360 this->totlen = 0;
361 this->numchips = 0;
362 this->curfloor = -1;
363 this->curchip = -1;
365 /* Ident all the chips present. */
366 DoC_ScanChips(this);
368 if (!this->totlen) {
369 kfree(mtd);
370 iounmap(this->virtadr);
371 } else {
372 this->nextdoc = docmillist;
373 docmillist = mtd;
374 mtd->size = this->totlen;
375 add_mtd_device(mtd);
376 return;
379 EXPORT_SYMBOL_GPL(DoCMil_init);
381 static int doc_read (struct mtd_info *mtd, loff_t from, size_t len,
382 size_t *retlen, u_char *buf)
384 int i, ret;
385 volatile char dummy;
386 unsigned char syndrome[6], eccbuf[6];
387 struct DiskOnChip *this = mtd->priv;
388 void __iomem *docptr = this->virtadr;
389 struct Nand *mychip = &this->chips[from >> (this->chipshift)];
391 /* Don't allow read past end of device */
392 if (from >= this->totlen)
393 return -EINVAL;
395 /* Don't allow a single read to cross a 512-byte block boundary */
396 if (from + len > ((from | 0x1ff) + 1))
397 len = ((from | 0x1ff) + 1) - from;
399 /* Find the chip which is to be used and select it */
400 if (this->curfloor != mychip->floor) {
401 DoC_SelectFloor(docptr, mychip->floor);
402 DoC_SelectChip(docptr, mychip->chip);
403 } else if (this->curchip != mychip->chip) {
404 DoC_SelectChip(docptr, mychip->chip);
406 this->curfloor = mychip->floor;
407 this->curchip = mychip->chip;
409 /* issue the Read0 or Read1 command depend on which half of the page
410 we are accessing. Polling the Flash Ready bit after issue 3 bytes
411 address in Sequence Read Mode, see Software Requirement 11.4 item 1.*/
412 DoC_Command(docptr, (from >> 8) & 1, CDSN_CTRL_WP);
413 DoC_Address(docptr, 3, from, CDSN_CTRL_WP, 0x00);
414 DoC_WaitReady(docptr);
416 /* init the ECC engine, see Reed-Solomon EDC/ECC 11.1 .*/
417 WriteDOC (DOC_ECC_RESET, docptr, ECCConf);
418 WriteDOC (DOC_ECC_EN, docptr, ECCConf);
420 /* Read the data via the internal pipeline through CDSN IO register,
421 see Pipelined Read Operations 11.3 */
422 dummy = ReadDOC(docptr, ReadPipeInit);
423 #ifndef USE_MEMCPY
424 for (i = 0; i < len-1; i++) {
425 /* N.B. you have to increase the source address in this way or the
426 ECC logic will not work properly */
427 buf[i] = ReadDOC(docptr, Mil_CDSN_IO + (i & 0xff));
429 #else
430 memcpy_fromio(buf, docptr + DoC_Mil_CDSN_IO, len - 1);
431 #endif
432 buf[len - 1] = ReadDOC(docptr, LastDataRead);
434 /* Let the caller know we completed it */
435 *retlen = len;
436 ret = 0;
438 /* Read the ECC data from Spare Data Area,
439 see Reed-Solomon EDC/ECC 11.1 */
440 dummy = ReadDOC(docptr, ReadPipeInit);
441 #ifndef USE_MEMCPY
442 for (i = 0; i < 5; i++) {
443 /* N.B. you have to increase the source address in this way or the
444 ECC logic will not work properly */
445 eccbuf[i] = ReadDOC(docptr, Mil_CDSN_IO + i);
447 #else
448 memcpy_fromio(eccbuf, docptr + DoC_Mil_CDSN_IO, 5);
449 #endif
450 eccbuf[5] = ReadDOC(docptr, LastDataRead);
452 /* Flush the pipeline */
453 dummy = ReadDOC(docptr, ECCConf);
454 dummy = ReadDOC(docptr, ECCConf);
456 /* Check the ECC Status */
457 if (ReadDOC(docptr, ECCConf) & 0x80) {
458 int nb_errors;
459 /* There was an ECC error */
460 #ifdef ECC_DEBUG
461 printk("DiskOnChip ECC Error: Read at %lx\n", (long)from);
462 #endif
463 /* Read the ECC syndrom through the DiskOnChip ECC logic.
464 These syndrome will be all ZERO when there is no error */
465 for (i = 0; i < 6; i++) {
466 syndrome[i] = ReadDOC(docptr, ECCSyndrome0 + i);
468 nb_errors = doc_decode_ecc(buf, syndrome);
469 #ifdef ECC_DEBUG
470 printk("ECC Errors corrected: %x\n", nb_errors);
471 #endif
472 if (nb_errors < 0) {
473 /* We return error, but have actually done the read. Not that
474 this can be told to user-space, via sys_read(), but at least
475 MTD-aware stuff can know about it by checking *retlen */
476 ret = -EIO;
480 #ifdef PSYCHO_DEBUG
481 printk("ECC DATA at %lx: %2.2X %2.2X %2.2X %2.2X %2.2X %2.2X\n",
482 (long)from, eccbuf[0], eccbuf[1], eccbuf[2], eccbuf[3],
483 eccbuf[4], eccbuf[5]);
484 #endif
486 /* disable the ECC engine */
487 WriteDOC(DOC_ECC_DIS, docptr , ECCConf);
489 return ret;
492 static int doc_write (struct mtd_info *mtd, loff_t to, size_t len,
493 size_t *retlen, const u_char *buf)
495 int i,ret = 0;
496 char eccbuf[6];
497 volatile char dummy;
498 struct DiskOnChip *this = mtd->priv;
499 void __iomem *docptr = this->virtadr;
500 struct Nand *mychip = &this->chips[to >> (this->chipshift)];
502 /* Don't allow write past end of device */
503 if (to >= this->totlen)
504 return -EINVAL;
506 /* Don't allow writes which aren't exactly one block */
507 if (to & 0x1ff || len != 0x200)
508 return -EINVAL;
510 /* Find the chip which is to be used and select it */
511 if (this->curfloor != mychip->floor) {
512 DoC_SelectFloor(docptr, mychip->floor);
513 DoC_SelectChip(docptr, mychip->chip);
514 } else if (this->curchip != mychip->chip) {
515 DoC_SelectChip(docptr, mychip->chip);
517 this->curfloor = mychip->floor;
518 this->curchip = mychip->chip;
520 /* Reset the chip, see Software Requirement 11.4 item 1. */
521 DoC_Command(docptr, NAND_CMD_RESET, 0x00);
522 DoC_WaitReady(docptr);
523 /* Set device to main plane of flash */
524 DoC_Command(docptr, NAND_CMD_READ0, 0x00);
526 /* issue the Serial Data In command to initial the Page Program process */
527 DoC_Command(docptr, NAND_CMD_SEQIN, 0x00);
528 DoC_Address(docptr, 3, to, 0x00, 0x00);
529 DoC_WaitReady(docptr);
531 /* init the ECC engine, see Reed-Solomon EDC/ECC 11.1 .*/
532 WriteDOC (DOC_ECC_RESET, docptr, ECCConf);
533 WriteDOC (DOC_ECC_EN | DOC_ECC_RW, docptr, ECCConf);
535 /* Write the data via the internal pipeline through CDSN IO register,
536 see Pipelined Write Operations 11.2 */
537 #ifndef USE_MEMCPY
538 for (i = 0; i < len; i++) {
539 /* N.B. you have to increase the source address in this way or the
540 ECC logic will not work properly */
541 WriteDOC(buf[i], docptr, Mil_CDSN_IO + i);
543 #else
544 memcpy_toio(docptr + DoC_Mil_CDSN_IO, buf, len);
545 #endif
546 WriteDOC(0x00, docptr, WritePipeTerm);
548 /* Write ECC data to flash, the ECC info is generated by the DiskOnChip ECC logic
549 see Reed-Solomon EDC/ECC 11.1 */
550 WriteDOC(0, docptr, NOP);
551 WriteDOC(0, docptr, NOP);
552 WriteDOC(0, docptr, NOP);
554 /* Read the ECC data through the DiskOnChip ECC logic */
555 for (i = 0; i < 6; i++) {
556 eccbuf[i] = ReadDOC(docptr, ECCSyndrome0 + i);
559 /* ignore the ECC engine */
560 WriteDOC(DOC_ECC_DIS, docptr , ECCConf);
562 #ifndef USE_MEMCPY
563 /* Write the ECC data to flash */
564 for (i = 0; i < 6; i++) {
565 /* N.B. you have to increase the source address in this way or the
566 ECC logic will not work properly */
567 WriteDOC(eccbuf[i], docptr, Mil_CDSN_IO + i);
569 #else
570 memcpy_toio(docptr + DoC_Mil_CDSN_IO, eccbuf, 6);
571 #endif
573 WriteDOC(0x55, docptr, Mil_CDSN_IO);
574 WriteDOC(0x55, docptr, Mil_CDSN_IO + 1);
576 WriteDOC(0x00, docptr, WritePipeTerm);
578 #ifdef PSYCHO_DEBUG
579 printk("OOB data at %lx is %2.2X %2.2X %2.2X %2.2X %2.2X %2.2X\n",
580 (long) to, eccbuf[0], eccbuf[1], eccbuf[2], eccbuf[3],
581 eccbuf[4], eccbuf[5]);
582 #endif
584 /* Commit the Page Program command and wait for ready
585 see Software Requirement 11.4 item 1.*/
586 DoC_Command(docptr, NAND_CMD_PAGEPROG, 0x00);
587 DoC_WaitReady(docptr);
589 /* Read the status of the flash device through CDSN IO register
590 see Software Requirement 11.4 item 5.*/
591 DoC_Command(docptr, NAND_CMD_STATUS, CDSN_CTRL_WP);
592 dummy = ReadDOC(docptr, ReadPipeInit);
593 DoC_Delay(docptr, 2);
594 if (ReadDOC(docptr, Mil_CDSN_IO) & 1) {
595 printk("Error programming flash\n");
596 *retlen = 0;
597 ret = -EIO;
599 dummy = ReadDOC(docptr, LastDataRead);
601 /* Let the caller know we completed it */
602 *retlen = len;
604 return ret;
607 static int doc_read_oob(struct mtd_info *mtd, loff_t ofs,
608 struct mtd_oob_ops *ops)
610 #ifndef USE_MEMCPY
611 int i;
612 #endif
613 volatile char dummy;
614 struct DiskOnChip *this = mtd->priv;
615 void __iomem *docptr = this->virtadr;
616 struct Nand *mychip = &this->chips[ofs >> this->chipshift];
617 uint8_t *buf = ops->oobbuf;
618 size_t len = ops->len;
620 BUG_ON(ops->mode != MTD_OOB_PLACE);
622 ofs += ops->ooboffs;
624 /* Find the chip which is to be used and select it */
625 if (this->curfloor != mychip->floor) {
626 DoC_SelectFloor(docptr, mychip->floor);
627 DoC_SelectChip(docptr, mychip->chip);
628 } else if (this->curchip != mychip->chip) {
629 DoC_SelectChip(docptr, mychip->chip);
631 this->curfloor = mychip->floor;
632 this->curchip = mychip->chip;
634 /* disable the ECC engine */
635 WriteDOC (DOC_ECC_RESET, docptr, ECCConf);
636 WriteDOC (DOC_ECC_DIS, docptr, ECCConf);
638 /* issue the Read2 command to set the pointer to the Spare Data Area.
639 Polling the Flash Ready bit after issue 3 bytes address in
640 Sequence Read Mode, see Software Requirement 11.4 item 1.*/
641 DoC_Command(docptr, NAND_CMD_READOOB, CDSN_CTRL_WP);
642 DoC_Address(docptr, 3, ofs, CDSN_CTRL_WP, 0x00);
643 DoC_WaitReady(docptr);
645 /* Read the data out via the internal pipeline through CDSN IO register,
646 see Pipelined Read Operations 11.3 */
647 dummy = ReadDOC(docptr, ReadPipeInit);
648 #ifndef USE_MEMCPY
649 for (i = 0; i < len-1; i++) {
650 /* N.B. you have to increase the source address in this way or the
651 ECC logic will not work properly */
652 buf[i] = ReadDOC(docptr, Mil_CDSN_IO + i);
654 #else
655 memcpy_fromio(buf, docptr + DoC_Mil_CDSN_IO, len - 1);
656 #endif
657 buf[len - 1] = ReadDOC(docptr, LastDataRead);
659 ops->retlen = len;
661 return 0;
664 static int doc_write_oob(struct mtd_info *mtd, loff_t ofs,
665 struct mtd_oob_ops *ops)
667 #ifndef USE_MEMCPY
668 int i;
669 #endif
670 volatile char dummy;
671 int ret = 0;
672 struct DiskOnChip *this = mtd->priv;
673 void __iomem *docptr = this->virtadr;
674 struct Nand *mychip = &this->chips[ofs >> this->chipshift];
675 uint8_t *buf = ops->oobbuf;
676 size_t len = ops->len;
678 BUG_ON(ops->mode != MTD_OOB_PLACE);
680 ofs += ops->ooboffs;
682 /* Find the chip which is to be used and select it */
683 if (this->curfloor != mychip->floor) {
684 DoC_SelectFloor(docptr, mychip->floor);
685 DoC_SelectChip(docptr, mychip->chip);
686 } else if (this->curchip != mychip->chip) {
687 DoC_SelectChip(docptr, mychip->chip);
689 this->curfloor = mychip->floor;
690 this->curchip = mychip->chip;
692 /* disable the ECC engine */
693 WriteDOC (DOC_ECC_RESET, docptr, ECCConf);
694 WriteDOC (DOC_ECC_DIS, docptr, ECCConf);
696 /* Reset the chip, see Software Requirement 11.4 item 1. */
697 DoC_Command(docptr, NAND_CMD_RESET, CDSN_CTRL_WP);
698 DoC_WaitReady(docptr);
699 /* issue the Read2 command to set the pointer to the Spare Data Area. */
700 DoC_Command(docptr, NAND_CMD_READOOB, CDSN_CTRL_WP);
702 /* issue the Serial Data In command to initial the Page Program process */
703 DoC_Command(docptr, NAND_CMD_SEQIN, 0x00);
704 DoC_Address(docptr, 3, ofs, 0x00, 0x00);
706 /* Write the data via the internal pipeline through CDSN IO register,
707 see Pipelined Write Operations 11.2 */
708 #ifndef USE_MEMCPY
709 for (i = 0; i < len; i++) {
710 /* N.B. you have to increase the source address in this way or the
711 ECC logic will not work properly */
712 WriteDOC(buf[i], docptr, Mil_CDSN_IO + i);
714 #else
715 memcpy_toio(docptr + DoC_Mil_CDSN_IO, buf, len);
716 #endif
717 WriteDOC(0x00, docptr, WritePipeTerm);
719 /* Commit the Page Program command and wait for ready
720 see Software Requirement 11.4 item 1.*/
721 DoC_Command(docptr, NAND_CMD_PAGEPROG, 0x00);
722 DoC_WaitReady(docptr);
724 /* Read the status of the flash device through CDSN IO register
725 see Software Requirement 11.4 item 5.*/
726 DoC_Command(docptr, NAND_CMD_STATUS, 0x00);
727 dummy = ReadDOC(docptr, ReadPipeInit);
728 DoC_Delay(docptr, 2);
729 if (ReadDOC(docptr, Mil_CDSN_IO) & 1) {
730 printk("Error programming oob data\n");
731 ops->retlen = 0;
732 ret = -EIO;
734 dummy = ReadDOC(docptr, LastDataRead);
736 ops->retlen = len;
738 return ret;
741 int doc_erase (struct mtd_info *mtd, struct erase_info *instr)
743 volatile char dummy;
744 struct DiskOnChip *this = mtd->priv;
745 __u32 ofs = instr->addr;
746 __u32 len = instr->len;
747 void __iomem *docptr = this->virtadr;
748 struct Nand *mychip = &this->chips[ofs >> this->chipshift];
750 if (len != mtd->erasesize)
751 printk(KERN_WARNING "Erase not right size (%x != %x)n",
752 len, mtd->erasesize);
754 /* Find the chip which is to be used and select it */
755 if (this->curfloor != mychip->floor) {
756 DoC_SelectFloor(docptr, mychip->floor);
757 DoC_SelectChip(docptr, mychip->chip);
758 } else if (this->curchip != mychip->chip) {
759 DoC_SelectChip(docptr, mychip->chip);
761 this->curfloor = mychip->floor;
762 this->curchip = mychip->chip;
764 instr->state = MTD_ERASE_PENDING;
766 /* issue the Erase Setup command */
767 DoC_Command(docptr, NAND_CMD_ERASE1, 0x00);
768 DoC_Address(docptr, 2, ofs, 0x00, 0x00);
770 /* Commit the Erase Start command and wait for ready
771 see Software Requirement 11.4 item 1.*/
772 DoC_Command(docptr, NAND_CMD_ERASE2, 0x00);
773 DoC_WaitReady(docptr);
775 instr->state = MTD_ERASING;
777 DoC_Command(docptr, NAND_CMD_STATUS, CDSN_CTRL_WP);
778 dummy = ReadDOC(docptr, ReadPipeInit);
779 DoC_Delay(docptr, 2);
780 if (ReadDOC(docptr, Mil_CDSN_IO) & 1) {
781 printk("Error Erasing at 0x%x\n", ofs);
782 instr->state = MTD_ERASE_FAILED;
783 } else
784 instr->state = MTD_ERASE_DONE;
785 dummy = ReadDOC(docptr, LastDataRead);
787 mtd_erase_callback(instr);
789 return 0;
792 /****************************************************************************
794 * Module stuff
796 ****************************************************************************/
798 static void __exit cleanup_doc2001(void)
800 struct mtd_info *mtd;
801 struct DiskOnChip *this;
803 while ((mtd=docmillist)) {
804 this = mtd->priv;
805 docmillist = this->nextdoc;
807 del_mtd_device(mtd);
809 iounmap(this->virtadr);
810 kfree(this->chips);
811 kfree(mtd);
815 module_exit(cleanup_doc2001);
817 MODULE_LICENSE("GPL");
818 MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org> et al.");
819 MODULE_DESCRIPTION("Alternative driver for DiskOnChip Millennium");