cipso: unsigned buf_len cannot be negative
[linux-2.6/next.git] / drivers / mtd / devices / doc2001.c
blobe32c568c11450e1c8041f4e28604ed1b72654d00
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/miscdevice.h>
14 #include <linux/delay.h>
15 #include <linux/slab.h>
16 #include <linux/init.h>
17 #include <linux/types.h>
18 #include <linux/bitops.h>
20 #include <linux/mtd/mtd.h>
21 #include <linux/mtd/nand.h>
22 #include <linux/mtd/doc2000.h>
24 /* #define ECC_DEBUG */
26 /* I have no idea why some DoC chips can not use memcop_form|to_io().
27 * This may be due to the different revisions of the ASIC controller built-in or
28 * simplily a QA/Bug issue. Who knows ?? If you have trouble, please uncomment
29 * this:*/
30 #undef USE_MEMCPY
32 static int doc_read(struct mtd_info *mtd, loff_t from, size_t len,
33 size_t *retlen, u_char *buf);
34 static int doc_write(struct mtd_info *mtd, loff_t to, size_t len,
35 size_t *retlen, const u_char *buf);
36 static int doc_read_oob(struct mtd_info *mtd, loff_t ofs,
37 struct mtd_oob_ops *ops);
38 static int doc_write_oob(struct mtd_info *mtd, loff_t ofs,
39 struct mtd_oob_ops *ops);
40 static int doc_erase (struct mtd_info *mtd, struct erase_info *instr);
42 static struct mtd_info *docmillist = NULL;
44 /* Perform the required delay cycles by reading from the NOP register */
45 static void DoC_Delay(void __iomem * docptr, unsigned short cycles)
47 volatile char dummy;
48 int i;
50 for (i = 0; i < cycles; i++)
51 dummy = ReadDOC(docptr, NOP);
54 /* DOC_WaitReady: Wait for RDY line to be asserted by the flash chip */
55 static int _DoC_WaitReady(void __iomem * docptr)
57 unsigned short c = 0xffff;
59 DEBUG(MTD_DEBUG_LEVEL3,
60 "_DoC_WaitReady called for out-of-line wait\n");
62 /* Out-of-line routine to wait for chip response */
63 while (!(ReadDOC(docptr, CDSNControl) & CDSN_CTRL_FR_B) && --c)
66 if (c == 0)
67 DEBUG(MTD_DEBUG_LEVEL2, "_DoC_WaitReady timed out.\n");
69 return (c == 0);
72 static inline int DoC_WaitReady(void __iomem * docptr)
74 /* This is inline, to optimise the common case, where it's ready instantly */
75 int ret = 0;
77 /* 4 read form NOP register should be issued in prior to the read from CDSNControl
78 see Software Requirement 11.4 item 2. */
79 DoC_Delay(docptr, 4);
81 if (!(ReadDOC(docptr, CDSNControl) & CDSN_CTRL_FR_B))
82 /* Call the out-of-line routine to wait */
83 ret = _DoC_WaitReady(docptr);
85 /* issue 2 read from NOP register after reading from CDSNControl register
86 see Software Requirement 11.4 item 2. */
87 DoC_Delay(docptr, 2);
89 return ret;
92 /* DoC_Command: Send a flash command to the flash chip through the CDSN IO register
93 with the internal pipeline. Each of 4 delay cycles (read from the NOP register) is
94 required after writing to CDSN Control register, see Software Requirement 11.4 item 3. */
96 static void DoC_Command(void __iomem * docptr, unsigned char command,
97 unsigned char xtraflags)
99 /* Assert the CLE (Command Latch Enable) line to the flash chip */
100 WriteDOC(xtraflags | CDSN_CTRL_CLE | CDSN_CTRL_CE, docptr, CDSNControl);
101 DoC_Delay(docptr, 4);
103 /* Send the command */
104 WriteDOC(command, docptr, Mil_CDSN_IO);
105 WriteDOC(0x00, docptr, WritePipeTerm);
107 /* Lower the CLE line */
108 WriteDOC(xtraflags | CDSN_CTRL_CE, docptr, CDSNControl);
109 DoC_Delay(docptr, 4);
112 /* DoC_Address: Set the current address for the flash chip through the CDSN IO register
113 with the internal pipeline. Each of 4 delay cycles (read from the NOP register) is
114 required after writing to CDSN Control register, see Software Requirement 11.4 item 3. */
116 static inline void DoC_Address(void __iomem * docptr, int numbytes, unsigned long ofs,
117 unsigned char xtraflags1, unsigned char xtraflags2)
119 /* Assert the ALE (Address Latch Enable) line to the flash chip */
120 WriteDOC(xtraflags1 | CDSN_CTRL_ALE | CDSN_CTRL_CE, docptr, CDSNControl);
121 DoC_Delay(docptr, 4);
123 /* Send the address */
124 switch (numbytes)
126 case 1:
127 /* Send single byte, bits 0-7. */
128 WriteDOC(ofs & 0xff, docptr, Mil_CDSN_IO);
129 WriteDOC(0x00, docptr, WritePipeTerm);
130 break;
131 case 2:
132 /* Send bits 9-16 followed by 17-23 */
133 WriteDOC((ofs >> 9) & 0xff, docptr, Mil_CDSN_IO);
134 WriteDOC((ofs >> 17) & 0xff, docptr, Mil_CDSN_IO);
135 WriteDOC(0x00, docptr, WritePipeTerm);
136 break;
137 case 3:
138 /* Send 0-7, 9-16, then 17-23 */
139 WriteDOC(ofs & 0xff, docptr, Mil_CDSN_IO);
140 WriteDOC((ofs >> 9) & 0xff, docptr, Mil_CDSN_IO);
141 WriteDOC((ofs >> 17) & 0xff, docptr, Mil_CDSN_IO);
142 WriteDOC(0x00, docptr, WritePipeTerm);
143 break;
144 default:
145 return;
148 /* Lower the ALE line */
149 WriteDOC(xtraflags1 | xtraflags2 | CDSN_CTRL_CE, docptr, CDSNControl);
150 DoC_Delay(docptr, 4);
153 /* DoC_SelectChip: Select a given flash chip within the current floor */
154 static int DoC_SelectChip(void __iomem * docptr, int chip)
156 /* Select the individual flash chip requested */
157 WriteDOC(chip, docptr, CDSNDeviceSelect);
158 DoC_Delay(docptr, 4);
160 /* Wait for it to be ready */
161 return DoC_WaitReady(docptr);
164 /* DoC_SelectFloor: Select a given floor (bank of flash chips) */
165 static int DoC_SelectFloor(void __iomem * docptr, int floor)
167 /* Select the floor (bank) of chips required */
168 WriteDOC(floor, docptr, FloorSelect);
170 /* Wait for the chip to be ready */
171 return DoC_WaitReady(docptr);
174 /* DoC_IdentChip: Identify a given NAND chip given {floor,chip} */
175 static int DoC_IdentChip(struct DiskOnChip *doc, int floor, int chip)
177 int mfr, id, i, j;
178 volatile char dummy;
180 /* Page in the required floor/chip
181 FIXME: is this supported by Millennium ?? */
182 DoC_SelectFloor(doc->virtadr, floor);
183 DoC_SelectChip(doc->virtadr, chip);
185 /* Reset the chip, see Software Requirement 11.4 item 1. */
186 DoC_Command(doc->virtadr, NAND_CMD_RESET, CDSN_CTRL_WP);
187 DoC_WaitReady(doc->virtadr);
189 /* Read the NAND chip ID: 1. Send ReadID command */
190 DoC_Command(doc->virtadr, NAND_CMD_READID, CDSN_CTRL_WP);
192 /* Read the NAND chip ID: 2. Send address byte zero */
193 DoC_Address(doc->virtadr, 1, 0x00, CDSN_CTRL_WP, 0x00);
195 /* Read the manufacturer and device id codes of the flash device through
196 CDSN IO register see Software Requirement 11.4 item 5.*/
197 dummy = ReadDOC(doc->virtadr, ReadPipeInit);
198 DoC_Delay(doc->virtadr, 2);
199 mfr = ReadDOC(doc->virtadr, Mil_CDSN_IO);
201 DoC_Delay(doc->virtadr, 2);
202 id = ReadDOC(doc->virtadr, Mil_CDSN_IO);
203 dummy = ReadDOC(doc->virtadr, LastDataRead);
205 /* No response - return failure */
206 if (mfr == 0xff || mfr == 0)
207 return 0;
209 /* FIXME: to deal with multi-flash on multi-Millennium case more carefully */
210 for (i = 0; nand_flash_ids[i].name != NULL; i++) {
211 if ( id == nand_flash_ids[i].id) {
212 /* Try to identify manufacturer */
213 for (j = 0; nand_manuf_ids[j].id != 0x0; j++) {
214 if (nand_manuf_ids[j].id == mfr)
215 break;
217 printk(KERN_INFO "Flash chip found: Manufacturer ID: %2.2X, "
218 "Chip ID: %2.2X (%s:%s)\n",
219 mfr, id, nand_manuf_ids[j].name, nand_flash_ids[i].name);
220 doc->mfr = mfr;
221 doc->id = id;
222 doc->chipshift = ffs((nand_flash_ids[i].chipsize << 20)) - 1;
223 break;
227 if (nand_flash_ids[i].name == NULL)
228 return 0;
229 else
230 return 1;
233 /* DoC_ScanChips: Find all NAND chips present in a DiskOnChip, and identify them */
234 static void DoC_ScanChips(struct DiskOnChip *this)
236 int floor, chip;
237 int numchips[MAX_FLOORS_MIL];
238 int ret;
240 this->numchips = 0;
241 this->mfr = 0;
242 this->id = 0;
244 /* For each floor, find the number of valid chips it contains */
245 for (floor = 0,ret = 1; floor < MAX_FLOORS_MIL; floor++) {
246 numchips[floor] = 0;
247 for (chip = 0; chip < MAX_CHIPS_MIL && ret != 0; chip++) {
248 ret = DoC_IdentChip(this, floor, chip);
249 if (ret) {
250 numchips[floor]++;
251 this->numchips++;
255 /* If there are none at all that we recognise, bail */
256 if (!this->numchips) {
257 printk("No flash chips recognised.\n");
258 return;
261 /* Allocate an array to hold the information for each chip */
262 this->chips = kmalloc(sizeof(struct Nand) * this->numchips, GFP_KERNEL);
263 if (!this->chips){
264 printk("No memory for allocating chip info structures\n");
265 return;
268 /* Fill out the chip array with {floor, chipno} for each
269 * detected chip in the device. */
270 for (floor = 0, ret = 0; floor < MAX_FLOORS_MIL; floor++) {
271 for (chip = 0 ; chip < numchips[floor] ; chip++) {
272 this->chips[ret].floor = floor;
273 this->chips[ret].chip = chip;
274 this->chips[ret].curadr = 0;
275 this->chips[ret].curmode = 0x50;
276 ret++;
280 /* Calculate and print the total size of the device */
281 this->totlen = this->numchips * (1 << this->chipshift);
282 printk(KERN_INFO "%d flash chips found. Total DiskOnChip size: %ld MiB\n",
283 this->numchips ,this->totlen >> 20);
286 static int DoCMil_is_alias(struct DiskOnChip *doc1, struct DiskOnChip *doc2)
288 int tmp1, tmp2, retval;
290 if (doc1->physadr == doc2->physadr)
291 return 1;
293 /* Use the alias resolution register which was set aside for this
294 * purpose. If it's value is the same on both chips, they might
295 * be the same chip, and we write to one and check for a change in
296 * the other. It's unclear if this register is usuable in the
297 * DoC 2000 (it's in the Millenium docs), but it seems to work. */
298 tmp1 = ReadDOC(doc1->virtadr, AliasResolution);
299 tmp2 = ReadDOC(doc2->virtadr, AliasResolution);
300 if (tmp1 != tmp2)
301 return 0;
303 WriteDOC((tmp1+1) % 0xff, doc1->virtadr, AliasResolution);
304 tmp2 = ReadDOC(doc2->virtadr, AliasResolution);
305 if (tmp2 == (tmp1+1) % 0xff)
306 retval = 1;
307 else
308 retval = 0;
310 /* Restore register contents. May not be necessary, but do it just to
311 * be safe. */
312 WriteDOC(tmp1, doc1->virtadr, AliasResolution);
314 return retval;
317 /* This routine is found from the docprobe code by symbol_get(),
318 * which will bump the use count of this module. */
319 void DoCMil_init(struct mtd_info *mtd)
321 struct DiskOnChip *this = mtd->priv;
322 struct DiskOnChip *old = NULL;
324 /* We must avoid being called twice for the same device. */
325 if (docmillist)
326 old = docmillist->priv;
328 while (old) {
329 if (DoCMil_is_alias(this, old)) {
330 printk(KERN_NOTICE "Ignoring DiskOnChip Millennium at "
331 "0x%lX - already configured\n", this->physadr);
332 iounmap(this->virtadr);
333 kfree(mtd);
334 return;
336 if (old->nextdoc)
337 old = old->nextdoc->priv;
338 else
339 old = NULL;
342 mtd->name = "DiskOnChip Millennium";
343 printk(KERN_NOTICE "DiskOnChip Millennium found at address 0x%lX\n",
344 this->physadr);
346 mtd->type = MTD_NANDFLASH;
347 mtd->flags = MTD_CAP_NANDFLASH;
348 mtd->size = 0;
350 /* FIXME: erase size is not always 8KiB */
351 mtd->erasesize = 0x2000;
353 mtd->writesize = 512;
354 mtd->oobsize = 16;
355 mtd->owner = THIS_MODULE;
356 mtd->erase = doc_erase;
357 mtd->point = NULL;
358 mtd->unpoint = NULL;
359 mtd->read = doc_read;
360 mtd->write = doc_write;
361 mtd->read_oob = doc_read_oob;
362 mtd->write_oob = doc_write_oob;
363 mtd->sync = NULL;
365 this->totlen = 0;
366 this->numchips = 0;
367 this->curfloor = -1;
368 this->curchip = -1;
370 /* Ident all the chips present. */
371 DoC_ScanChips(this);
373 if (!this->totlen) {
374 kfree(mtd);
375 iounmap(this->virtadr);
376 } else {
377 this->nextdoc = docmillist;
378 docmillist = mtd;
379 mtd->size = this->totlen;
380 add_mtd_device(mtd);
381 return;
384 EXPORT_SYMBOL_GPL(DoCMil_init);
386 static int doc_read (struct mtd_info *mtd, loff_t from, size_t len,
387 size_t *retlen, u_char *buf)
389 int i, ret;
390 volatile char dummy;
391 unsigned char syndrome[6], eccbuf[6];
392 struct DiskOnChip *this = mtd->priv;
393 void __iomem *docptr = this->virtadr;
394 struct Nand *mychip = &this->chips[from >> (this->chipshift)];
396 /* Don't allow read past end of device */
397 if (from >= this->totlen)
398 return -EINVAL;
400 /* Don't allow a single read to cross a 512-byte block boundary */
401 if (from + len > ((from | 0x1ff) + 1))
402 len = ((from | 0x1ff) + 1) - from;
404 /* Find the chip which is to be used and select it */
405 if (this->curfloor != mychip->floor) {
406 DoC_SelectFloor(docptr, mychip->floor);
407 DoC_SelectChip(docptr, mychip->chip);
408 } else if (this->curchip != mychip->chip) {
409 DoC_SelectChip(docptr, mychip->chip);
411 this->curfloor = mychip->floor;
412 this->curchip = mychip->chip;
414 /* issue the Read0 or Read1 command depend on which half of the page
415 we are accessing. Polling the Flash Ready bit after issue 3 bytes
416 address in Sequence Read Mode, see Software Requirement 11.4 item 1.*/
417 DoC_Command(docptr, (from >> 8) & 1, CDSN_CTRL_WP);
418 DoC_Address(docptr, 3, from, CDSN_CTRL_WP, 0x00);
419 DoC_WaitReady(docptr);
421 /* init the ECC engine, see Reed-Solomon EDC/ECC 11.1 .*/
422 WriteDOC (DOC_ECC_RESET, docptr, ECCConf);
423 WriteDOC (DOC_ECC_EN, docptr, ECCConf);
425 /* Read the data via the internal pipeline through CDSN IO register,
426 see Pipelined Read Operations 11.3 */
427 dummy = ReadDOC(docptr, ReadPipeInit);
428 #ifndef USE_MEMCPY
429 for (i = 0; i < len-1; i++) {
430 /* N.B. you have to increase the source address in this way or the
431 ECC logic will not work properly */
432 buf[i] = ReadDOC(docptr, Mil_CDSN_IO + (i & 0xff));
434 #else
435 memcpy_fromio(buf, docptr + DoC_Mil_CDSN_IO, len - 1);
436 #endif
437 buf[len - 1] = ReadDOC(docptr, LastDataRead);
439 /* Let the caller know we completed it */
440 *retlen = len;
441 ret = 0;
443 /* Read the ECC data from Spare Data Area,
444 see Reed-Solomon EDC/ECC 11.1 */
445 dummy = ReadDOC(docptr, ReadPipeInit);
446 #ifndef USE_MEMCPY
447 for (i = 0; i < 5; i++) {
448 /* N.B. you have to increase the source address in this way or the
449 ECC logic will not work properly */
450 eccbuf[i] = ReadDOC(docptr, Mil_CDSN_IO + i);
452 #else
453 memcpy_fromio(eccbuf, docptr + DoC_Mil_CDSN_IO, 5);
454 #endif
455 eccbuf[5] = ReadDOC(docptr, LastDataRead);
457 /* Flush the pipeline */
458 dummy = ReadDOC(docptr, ECCConf);
459 dummy = ReadDOC(docptr, ECCConf);
461 /* Check the ECC Status */
462 if (ReadDOC(docptr, ECCConf) & 0x80) {
463 int nb_errors;
464 /* There was an ECC error */
465 #ifdef ECC_DEBUG
466 printk("DiskOnChip ECC Error: Read at %lx\n", (long)from);
467 #endif
468 /* Read the ECC syndrom through the DiskOnChip ECC logic.
469 These syndrome will be all ZERO when there is no error */
470 for (i = 0; i < 6; i++) {
471 syndrome[i] = ReadDOC(docptr, ECCSyndrome0 + i);
473 nb_errors = doc_decode_ecc(buf, syndrome);
474 #ifdef ECC_DEBUG
475 printk("ECC Errors corrected: %x\n", nb_errors);
476 #endif
477 if (nb_errors < 0) {
478 /* We return error, but have actually done the read. Not that
479 this can be told to user-space, via sys_read(), but at least
480 MTD-aware stuff can know about it by checking *retlen */
481 ret = -EIO;
485 #ifdef PSYCHO_DEBUG
486 printk("ECC DATA at %lx: %2.2X %2.2X %2.2X %2.2X %2.2X %2.2X\n",
487 (long)from, eccbuf[0], eccbuf[1], eccbuf[2], eccbuf[3],
488 eccbuf[4], eccbuf[5]);
489 #endif
491 /* disable the ECC engine */
492 WriteDOC(DOC_ECC_DIS, docptr , ECCConf);
494 return ret;
497 static int doc_write (struct mtd_info *mtd, loff_t to, size_t len,
498 size_t *retlen, const u_char *buf)
500 int i,ret = 0;
501 char eccbuf[6];
502 volatile char dummy;
503 struct DiskOnChip *this = mtd->priv;
504 void __iomem *docptr = this->virtadr;
505 struct Nand *mychip = &this->chips[to >> (this->chipshift)];
507 /* Don't allow write past end of device */
508 if (to >= this->totlen)
509 return -EINVAL;
511 #if 0
512 /* Don't allow a single write to cross a 512-byte block boundary */
513 if (to + len > ( (to | 0x1ff) + 1))
514 len = ((to | 0x1ff) + 1) - to;
515 #else
516 /* Don't allow writes which aren't exactly one block */
517 if (to & 0x1ff || len != 0x200)
518 return -EINVAL;
519 #endif
521 /* Find the chip which is to be used and select it */
522 if (this->curfloor != mychip->floor) {
523 DoC_SelectFloor(docptr, mychip->floor);
524 DoC_SelectChip(docptr, mychip->chip);
525 } else if (this->curchip != mychip->chip) {
526 DoC_SelectChip(docptr, mychip->chip);
528 this->curfloor = mychip->floor;
529 this->curchip = mychip->chip;
531 /* Reset the chip, see Software Requirement 11.4 item 1. */
532 DoC_Command(docptr, NAND_CMD_RESET, 0x00);
533 DoC_WaitReady(docptr);
534 /* Set device to main plane of flash */
535 DoC_Command(docptr, NAND_CMD_READ0, 0x00);
537 /* issue the Serial Data In command to initial the Page Program process */
538 DoC_Command(docptr, NAND_CMD_SEQIN, 0x00);
539 DoC_Address(docptr, 3, to, 0x00, 0x00);
540 DoC_WaitReady(docptr);
542 /* init the ECC engine, see Reed-Solomon EDC/ECC 11.1 .*/
543 WriteDOC (DOC_ECC_RESET, docptr, ECCConf);
544 WriteDOC (DOC_ECC_EN | DOC_ECC_RW, docptr, ECCConf);
546 /* Write the data via the internal pipeline through CDSN IO register,
547 see Pipelined Write Operations 11.2 */
548 #ifndef USE_MEMCPY
549 for (i = 0; i < len; i++) {
550 /* N.B. you have to increase the source address in this way or the
551 ECC logic will not work properly */
552 WriteDOC(buf[i], docptr, Mil_CDSN_IO + i);
554 #else
555 memcpy_toio(docptr + DoC_Mil_CDSN_IO, buf, len);
556 #endif
557 WriteDOC(0x00, docptr, WritePipeTerm);
559 /* Write ECC data to flash, the ECC info is generated by the DiskOnChip ECC logic
560 see Reed-Solomon EDC/ECC 11.1 */
561 WriteDOC(0, docptr, NOP);
562 WriteDOC(0, docptr, NOP);
563 WriteDOC(0, docptr, NOP);
565 /* Read the ECC data through the DiskOnChip ECC logic */
566 for (i = 0; i < 6; i++) {
567 eccbuf[i] = ReadDOC(docptr, ECCSyndrome0 + i);
570 /* ignore the ECC engine */
571 WriteDOC(DOC_ECC_DIS, docptr , ECCConf);
573 #ifndef USE_MEMCPY
574 /* Write the ECC data to flash */
575 for (i = 0; i < 6; i++) {
576 /* N.B. you have to increase the source address in this way or the
577 ECC logic will not work properly */
578 WriteDOC(eccbuf[i], docptr, Mil_CDSN_IO + i);
580 #else
581 memcpy_toio(docptr + DoC_Mil_CDSN_IO, eccbuf, 6);
582 #endif
584 /* write the block status BLOCK_USED (0x5555) at the end of ECC data
585 FIXME: this is only a hack for programming the IPL area for LinuxBIOS
586 and should be replace with proper codes in user space utilities */
587 WriteDOC(0x55, docptr, Mil_CDSN_IO);
588 WriteDOC(0x55, docptr, Mil_CDSN_IO + 1);
590 WriteDOC(0x00, docptr, WritePipeTerm);
592 #ifdef PSYCHO_DEBUG
593 printk("OOB data at %lx is %2.2X %2.2X %2.2X %2.2X %2.2X %2.2X\n",
594 (long) to, eccbuf[0], eccbuf[1], eccbuf[2], eccbuf[3],
595 eccbuf[4], eccbuf[5]);
596 #endif
598 /* Commit the Page Program command and wait for ready
599 see Software Requirement 11.4 item 1.*/
600 DoC_Command(docptr, NAND_CMD_PAGEPROG, 0x00);
601 DoC_WaitReady(docptr);
603 /* Read the status of the flash device through CDSN IO register
604 see Software Requirement 11.4 item 5.*/
605 DoC_Command(docptr, NAND_CMD_STATUS, CDSN_CTRL_WP);
606 dummy = ReadDOC(docptr, ReadPipeInit);
607 DoC_Delay(docptr, 2);
608 if (ReadDOC(docptr, Mil_CDSN_IO) & 1) {
609 printk("Error programming flash\n");
610 /* Error in programming
611 FIXME: implement Bad Block Replacement (in nftl.c ??) */
612 *retlen = 0;
613 ret = -EIO;
615 dummy = ReadDOC(docptr, LastDataRead);
617 /* Let the caller know we completed it */
618 *retlen = len;
620 return ret;
623 static int doc_read_oob(struct mtd_info *mtd, loff_t ofs,
624 struct mtd_oob_ops *ops)
626 #ifndef USE_MEMCPY
627 int i;
628 #endif
629 volatile char dummy;
630 struct DiskOnChip *this = mtd->priv;
631 void __iomem *docptr = this->virtadr;
632 struct Nand *mychip = &this->chips[ofs >> this->chipshift];
633 uint8_t *buf = ops->oobbuf;
634 size_t len = ops->len;
636 BUG_ON(ops->mode != MTD_OOB_PLACE);
638 ofs += ops->ooboffs;
640 /* Find the chip which is to be used and select it */
641 if (this->curfloor != mychip->floor) {
642 DoC_SelectFloor(docptr, mychip->floor);
643 DoC_SelectChip(docptr, mychip->chip);
644 } else if (this->curchip != mychip->chip) {
645 DoC_SelectChip(docptr, mychip->chip);
647 this->curfloor = mychip->floor;
648 this->curchip = mychip->chip;
650 /* disable the ECC engine */
651 WriteDOC (DOC_ECC_RESET, docptr, ECCConf);
652 WriteDOC (DOC_ECC_DIS, docptr, ECCConf);
654 /* issue the Read2 command to set the pointer to the Spare Data Area.
655 Polling the Flash Ready bit after issue 3 bytes address in
656 Sequence Read Mode, see Software Requirement 11.4 item 1.*/
657 DoC_Command(docptr, NAND_CMD_READOOB, CDSN_CTRL_WP);
658 DoC_Address(docptr, 3, ofs, CDSN_CTRL_WP, 0x00);
659 DoC_WaitReady(docptr);
661 /* Read the data out via the internal pipeline through CDSN IO register,
662 see Pipelined Read Operations 11.3 */
663 dummy = ReadDOC(docptr, ReadPipeInit);
664 #ifndef USE_MEMCPY
665 for (i = 0; i < len-1; i++) {
666 /* N.B. you have to increase the source address in this way or the
667 ECC logic will not work properly */
668 buf[i] = ReadDOC(docptr, Mil_CDSN_IO + i);
670 #else
671 memcpy_fromio(buf, docptr + DoC_Mil_CDSN_IO, len - 1);
672 #endif
673 buf[len - 1] = ReadDOC(docptr, LastDataRead);
675 ops->retlen = len;
677 return 0;
680 static int doc_write_oob(struct mtd_info *mtd, loff_t ofs,
681 struct mtd_oob_ops *ops)
683 #ifndef USE_MEMCPY
684 int i;
685 #endif
686 volatile char dummy;
687 int ret = 0;
688 struct DiskOnChip *this = mtd->priv;
689 void __iomem *docptr = this->virtadr;
690 struct Nand *mychip = &this->chips[ofs >> this->chipshift];
691 uint8_t *buf = ops->oobbuf;
692 size_t len = ops->len;
694 BUG_ON(ops->mode != MTD_OOB_PLACE);
696 ofs += ops->ooboffs;
698 /* Find the chip which is to be used and select it */
699 if (this->curfloor != mychip->floor) {
700 DoC_SelectFloor(docptr, mychip->floor);
701 DoC_SelectChip(docptr, mychip->chip);
702 } else if (this->curchip != mychip->chip) {
703 DoC_SelectChip(docptr, mychip->chip);
705 this->curfloor = mychip->floor;
706 this->curchip = mychip->chip;
708 /* disable the ECC engine */
709 WriteDOC (DOC_ECC_RESET, docptr, ECCConf);
710 WriteDOC (DOC_ECC_DIS, docptr, ECCConf);
712 /* Reset the chip, see Software Requirement 11.4 item 1. */
713 DoC_Command(docptr, NAND_CMD_RESET, CDSN_CTRL_WP);
714 DoC_WaitReady(docptr);
715 /* issue the Read2 command to set the pointer to the Spare Data Area. */
716 DoC_Command(docptr, NAND_CMD_READOOB, CDSN_CTRL_WP);
718 /* issue the Serial Data In command to initial the Page Program process */
719 DoC_Command(docptr, NAND_CMD_SEQIN, 0x00);
720 DoC_Address(docptr, 3, ofs, 0x00, 0x00);
722 /* Write the data via the internal pipeline through CDSN IO register,
723 see Pipelined Write Operations 11.2 */
724 #ifndef USE_MEMCPY
725 for (i = 0; i < len; i++) {
726 /* N.B. you have to increase the source address in this way or the
727 ECC logic will not work properly */
728 WriteDOC(buf[i], docptr, Mil_CDSN_IO + i);
730 #else
731 memcpy_toio(docptr + DoC_Mil_CDSN_IO, buf, len);
732 #endif
733 WriteDOC(0x00, docptr, WritePipeTerm);
735 /* Commit the Page Program command and wait for ready
736 see Software Requirement 11.4 item 1.*/
737 DoC_Command(docptr, NAND_CMD_PAGEPROG, 0x00);
738 DoC_WaitReady(docptr);
740 /* Read the status of the flash device through CDSN IO register
741 see Software Requirement 11.4 item 5.*/
742 DoC_Command(docptr, NAND_CMD_STATUS, 0x00);
743 dummy = ReadDOC(docptr, ReadPipeInit);
744 DoC_Delay(docptr, 2);
745 if (ReadDOC(docptr, Mil_CDSN_IO) & 1) {
746 printk("Error programming oob data\n");
747 /* FIXME: implement Bad Block Replacement (in nftl.c ??) */
748 ops->retlen = 0;
749 ret = -EIO;
751 dummy = ReadDOC(docptr, LastDataRead);
753 ops->retlen = len;
755 return ret;
758 int doc_erase (struct mtd_info *mtd, struct erase_info *instr)
760 volatile char dummy;
761 struct DiskOnChip *this = mtd->priv;
762 __u32 ofs = instr->addr;
763 __u32 len = instr->len;
764 void __iomem *docptr = this->virtadr;
765 struct Nand *mychip = &this->chips[ofs >> this->chipshift];
767 if (len != mtd->erasesize)
768 printk(KERN_WARNING "Erase not right size (%x != %x)n",
769 len, mtd->erasesize);
771 /* Find the chip which is to be used and select it */
772 if (this->curfloor != mychip->floor) {
773 DoC_SelectFloor(docptr, mychip->floor);
774 DoC_SelectChip(docptr, mychip->chip);
775 } else if (this->curchip != mychip->chip) {
776 DoC_SelectChip(docptr, mychip->chip);
778 this->curfloor = mychip->floor;
779 this->curchip = mychip->chip;
781 instr->state = MTD_ERASE_PENDING;
783 /* issue the Erase Setup command */
784 DoC_Command(docptr, NAND_CMD_ERASE1, 0x00);
785 DoC_Address(docptr, 2, ofs, 0x00, 0x00);
787 /* Commit the Erase Start command and wait for ready
788 see Software Requirement 11.4 item 1.*/
789 DoC_Command(docptr, NAND_CMD_ERASE2, 0x00);
790 DoC_WaitReady(docptr);
792 instr->state = MTD_ERASING;
794 /* Read the status of the flash device through CDSN IO register
795 see Software Requirement 11.4 item 5.
796 FIXME: it seems that we are not wait long enough, some blocks are not
797 erased fully */
798 DoC_Command(docptr, NAND_CMD_STATUS, CDSN_CTRL_WP);
799 dummy = ReadDOC(docptr, ReadPipeInit);
800 DoC_Delay(docptr, 2);
801 if (ReadDOC(docptr, Mil_CDSN_IO) & 1) {
802 printk("Error Erasing at 0x%x\n", ofs);
803 /* There was an error
804 FIXME: implement Bad Block Replacement (in nftl.c ??) */
805 instr->state = MTD_ERASE_FAILED;
806 } else
807 instr->state = MTD_ERASE_DONE;
808 dummy = ReadDOC(docptr, LastDataRead);
810 mtd_erase_callback(instr);
812 return 0;
815 /****************************************************************************
817 * Module stuff
819 ****************************************************************************/
821 static void __exit cleanup_doc2001(void)
823 struct mtd_info *mtd;
824 struct DiskOnChip *this;
826 while ((mtd=docmillist)) {
827 this = mtd->priv;
828 docmillist = this->nextdoc;
830 del_mtd_device(mtd);
832 iounmap(this->virtadr);
833 kfree(this->chips);
834 kfree(mtd);
838 module_exit(cleanup_doc2001);
840 MODULE_LICENSE("GPL");
841 MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org> et al.");
842 MODULE_DESCRIPTION("Alternative driver for DiskOnChip Millennium");