uwb: Use kcalloc instead of kzalloc to allocate array
[zen-stable.git] / drivers / mtd / devices / doc2001plus.c
blob99351bc3e0ed32207ecf294ff03bad1b322ea74c
1 /*
2 * Linux driver for Disk-On-Chip Millennium Plus
4 * (c) 2002-2003 Greg Ungerer <gerg@snapgear.com>
5 * (c) 2002-2003 SnapGear Inc
6 * (c) 1999 Machine Vision Holdings, Inc.
7 * (c) 1999, 2000 David Woodhouse <dwmw2@infradead.org>
9 * Released under GPL
12 #include <linux/kernel.h>
13 #include <linux/module.h>
14 #include <asm/errno.h>
15 #include <asm/io.h>
16 #include <asm/uaccess.h>
17 #include <linux/delay.h>
18 #include <linux/slab.h>
19 #include <linux/init.h>
20 #include <linux/types.h>
21 #include <linux/bitops.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
32 * this:*/
33 #undef USE_MEMCPY
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_oob(struct mtd_info *mtd, loff_t ofs,
40 struct mtd_oob_ops *ops);
41 static int doc_write_oob(struct mtd_info *mtd, loff_t ofs,
42 struct mtd_oob_ops *ops);
43 static int doc_erase (struct mtd_info *mtd, struct erase_info *instr);
45 static struct mtd_info *docmilpluslist = NULL;
48 /* Perform the required delay cycles by writing to the NOP register */
49 static void DoC_Delay(void __iomem * docptr, int cycles)
51 int i;
53 for (i = 0; (i < cycles); i++)
54 WriteDOC(0, docptr, Mplus_NOP);
57 #define CDSN_CTRL_FR_B_MASK (CDSN_CTRL_FR_B0 | CDSN_CTRL_FR_B1)
59 /* DOC_WaitReady: Wait for RDY line to be asserted by the flash chip */
60 static int _DoC_WaitReady(void __iomem * docptr)
62 unsigned int c = 0xffff;
64 pr_debug("_DoC_WaitReady called for out-of-line wait\n");
66 /* Out-of-line routine to wait for chip response */
67 while (((ReadDOC(docptr, Mplus_FlashControl) & CDSN_CTRL_FR_B_MASK) != CDSN_CTRL_FR_B_MASK) && --c)
70 if (c == 0)
71 pr_debug("_DoC_WaitReady timed out.\n");
73 return (c == 0);
76 static inline int DoC_WaitReady(void __iomem * docptr)
78 /* This is inline, to optimise the common case, where it's ready instantly */
79 int ret = 0;
81 /* read form NOP register should be issued prior to the read from CDSNControl
82 see Software Requirement 11.4 item 2. */
83 DoC_Delay(docptr, 4);
85 if ((ReadDOC(docptr, Mplus_FlashControl) & CDSN_CTRL_FR_B_MASK) != CDSN_CTRL_FR_B_MASK)
86 /* Call the out-of-line routine to wait */
87 ret = _DoC_WaitReady(docptr);
89 return ret;
92 /* For some reason the Millennium Plus seems to occasionally put itself
93 * into reset mode. For me this happens randomly, with no pattern that I
94 * can detect. M-systems suggest always check this on any block level
95 * operation and setting to normal mode if in reset mode.
97 static inline void DoC_CheckASIC(void __iomem * docptr)
99 /* Make sure the DoC is in normal mode */
100 if ((ReadDOC(docptr, Mplus_DOCControl) & DOC_MODE_NORMAL) == 0) {
101 WriteDOC((DOC_MODE_NORMAL | DOC_MODE_MDWREN), docptr, Mplus_DOCControl);
102 WriteDOC(~(DOC_MODE_NORMAL | DOC_MODE_MDWREN), docptr, Mplus_CtrlConfirm);
106 /* DoC_Command: Send a flash command to the flash chip through the Flash
107 * command register. Need 2 Write Pipeline Terminates to complete send.
109 static void DoC_Command(void __iomem * docptr, unsigned char command,
110 unsigned char xtraflags)
112 WriteDOC(command, docptr, Mplus_FlashCmd);
113 WriteDOC(command, docptr, Mplus_WritePipeTerm);
114 WriteDOC(command, docptr, Mplus_WritePipeTerm);
117 /* DoC_Address: Set the current address for the flash chip through the Flash
118 * Address register. Need 2 Write Pipeline Terminates to complete send.
120 static inline void DoC_Address(struct DiskOnChip *doc, int numbytes,
121 unsigned long ofs, unsigned char xtraflags1,
122 unsigned char xtraflags2)
124 void __iomem * docptr = doc->virtadr;
126 /* Allow for possible Mill Plus internal flash interleaving */
127 ofs >>= doc->interleave;
129 switch (numbytes) {
130 case 1:
131 /* Send single byte, bits 0-7. */
132 WriteDOC(ofs & 0xff, docptr, Mplus_FlashAddress);
133 break;
134 case 2:
135 /* Send bits 9-16 followed by 17-23 */
136 WriteDOC((ofs >> 9) & 0xff, docptr, Mplus_FlashAddress);
137 WriteDOC((ofs >> 17) & 0xff, docptr, Mplus_FlashAddress);
138 break;
139 case 3:
140 /* Send 0-7, 9-16, then 17-23 */
141 WriteDOC(ofs & 0xff, docptr, Mplus_FlashAddress);
142 WriteDOC((ofs >> 9) & 0xff, docptr, Mplus_FlashAddress);
143 WriteDOC((ofs >> 17) & 0xff, docptr, Mplus_FlashAddress);
144 break;
145 default:
146 return;
149 WriteDOC(0x00, docptr, Mplus_WritePipeTerm);
150 WriteDOC(0x00, docptr, Mplus_WritePipeTerm);
153 /* DoC_SelectChip: Select a given flash chip within the current floor */
154 static int DoC_SelectChip(void __iomem * docptr, int chip)
156 /* No choice for flash chip on Millennium Plus */
157 return 0;
160 /* DoC_SelectFloor: Select a given floor (bank of flash chips) */
161 static int DoC_SelectFloor(void __iomem * docptr, int floor)
163 WriteDOC((floor & 0x3), docptr, Mplus_DeviceSelect);
164 return 0;
168 * Translate the given offset into the appropriate command and offset.
169 * This does the mapping using the 16bit interleave layout defined by
170 * M-Systems, and looks like this for a sector pair:
171 * +-----------+-------+-------+-------+--------------+---------+-----------+
172 * | 0 --- 511 |512-517|518-519|520-521| 522 --- 1033 |1034-1039|1040 - 1055|
173 * +-----------+-------+-------+-------+--------------+---------+-----------+
174 * | Data 0 | ECC 0 |Flags0 |Flags1 | Data 1 |ECC 1 | OOB 1 + 2 |
175 * +-----------+-------+-------+-------+--------------+---------+-----------+
177 /* FIXME: This lives in INFTL not here. Other users of flash devices
178 may not want it */
179 static unsigned int DoC_GetDataOffset(struct mtd_info *mtd, loff_t *from)
181 struct DiskOnChip *this = mtd->priv;
183 if (this->interleave) {
184 unsigned int ofs = *from & 0x3ff;
185 unsigned int cmd;
187 if (ofs < 512) {
188 cmd = NAND_CMD_READ0;
189 ofs &= 0x1ff;
190 } else if (ofs < 1014) {
191 cmd = NAND_CMD_READ1;
192 ofs = (ofs & 0x1ff) + 10;
193 } else {
194 cmd = NAND_CMD_READOOB;
195 ofs = ofs - 1014;
198 *from = (*from & ~0x3ff) | ofs;
199 return cmd;
200 } else {
201 /* No interleave */
202 if ((*from) & 0x100)
203 return NAND_CMD_READ1;
204 return NAND_CMD_READ0;
208 static unsigned int DoC_GetECCOffset(struct mtd_info *mtd, loff_t *from)
210 unsigned int ofs, cmd;
212 if (*from & 0x200) {
213 cmd = NAND_CMD_READOOB;
214 ofs = 10 + (*from & 0xf);
215 } else {
216 cmd = NAND_CMD_READ1;
217 ofs = (*from & 0xf);
220 *from = (*from & ~0x3ff) | ofs;
221 return cmd;
224 static unsigned int DoC_GetFlagsOffset(struct mtd_info *mtd, loff_t *from)
226 unsigned int ofs, cmd;
228 cmd = NAND_CMD_READ1;
229 ofs = (*from & 0x200) ? 8 : 6;
230 *from = (*from & ~0x3ff) | ofs;
231 return cmd;
234 static unsigned int DoC_GetHdrOffset(struct mtd_info *mtd, loff_t *from)
236 unsigned int ofs, cmd;
238 cmd = NAND_CMD_READOOB;
239 ofs = (*from & 0x200) ? 24 : 16;
240 *from = (*from & ~0x3ff) | ofs;
241 return cmd;
244 static inline void MemReadDOC(void __iomem * docptr, unsigned char *buf, int len)
246 #ifndef USE_MEMCPY
247 int i;
248 for (i = 0; i < len; i++)
249 buf[i] = ReadDOC(docptr, Mil_CDSN_IO + i);
250 #else
251 memcpy_fromio(buf, docptr + DoC_Mil_CDSN_IO, len);
252 #endif
255 static inline void MemWriteDOC(void __iomem * docptr, unsigned char *buf, int len)
257 #ifndef USE_MEMCPY
258 int i;
259 for (i = 0; i < len; i++)
260 WriteDOC(buf[i], docptr, Mil_CDSN_IO + i);
261 #else
262 memcpy_toio(docptr + DoC_Mil_CDSN_IO, buf, len);
263 #endif
266 /* DoC_IdentChip: Identify a given NAND chip given {floor,chip} */
267 static int DoC_IdentChip(struct DiskOnChip *doc, int floor, int chip)
269 int mfr, id, i, j;
270 volatile char dummy;
271 void __iomem * docptr = doc->virtadr;
273 /* Page in the required floor/chip */
274 DoC_SelectFloor(docptr, floor);
275 DoC_SelectChip(docptr, chip);
277 /* Millennium Plus bus cycle sequence as per figure 2, section 2.4 */
278 WriteDOC((DOC_FLASH_CE | DOC_FLASH_WP), docptr, Mplus_FlashSelect);
280 /* Reset the chip, see Software Requirement 11.4 item 1. */
281 DoC_Command(docptr, NAND_CMD_RESET, 0);
282 DoC_WaitReady(docptr);
284 /* Read the NAND chip ID: 1. Send ReadID command */
285 DoC_Command(docptr, NAND_CMD_READID, 0);
287 /* Read the NAND chip ID: 2. Send address byte zero */
288 DoC_Address(doc, 1, 0x00, 0, 0x00);
290 WriteDOC(0, docptr, Mplus_FlashControl);
291 DoC_WaitReady(docptr);
293 /* Read the manufacturer and device id codes of the flash device through
294 CDSN IO register see Software Requirement 11.4 item 5.*/
295 dummy = ReadDOC(docptr, Mplus_ReadPipeInit);
296 dummy = ReadDOC(docptr, Mplus_ReadPipeInit);
298 mfr = ReadDOC(docptr, Mil_CDSN_IO);
299 if (doc->interleave)
300 dummy = ReadDOC(docptr, Mil_CDSN_IO); /* 2 way interleave */
302 id = ReadDOC(docptr, Mil_CDSN_IO);
303 if (doc->interleave)
304 dummy = ReadDOC(docptr, Mil_CDSN_IO); /* 2 way interleave */
306 dummy = ReadDOC(docptr, Mplus_LastDataRead);
307 dummy = ReadDOC(docptr, Mplus_LastDataRead);
309 /* Disable flash internally */
310 WriteDOC(0, docptr, Mplus_FlashSelect);
312 /* No response - return failure */
313 if (mfr == 0xff || mfr == 0)
314 return 0;
316 for (i = 0; nand_flash_ids[i].name != NULL; i++) {
317 if (id == nand_flash_ids[i].id) {
318 /* Try to identify manufacturer */
319 for (j = 0; nand_manuf_ids[j].id != 0x0; j++) {
320 if (nand_manuf_ids[j].id == mfr)
321 break;
323 printk(KERN_INFO "Flash chip found: Manufacturer ID: %2.2X, "
324 "Chip ID: %2.2X (%s:%s)\n", mfr, id,
325 nand_manuf_ids[j].name, nand_flash_ids[i].name);
326 doc->mfr = mfr;
327 doc->id = id;
328 doc->chipshift = ffs((nand_flash_ids[i].chipsize << 20)) - 1;
329 doc->erasesize = nand_flash_ids[i].erasesize << doc->interleave;
330 break;
334 if (nand_flash_ids[i].name == NULL)
335 return 0;
336 return 1;
339 /* DoC_ScanChips: Find all NAND chips present in a DiskOnChip, and identify them */
340 static void DoC_ScanChips(struct DiskOnChip *this)
342 int floor, chip;
343 int numchips[MAX_FLOORS_MPLUS];
344 int ret;
346 this->numchips = 0;
347 this->mfr = 0;
348 this->id = 0;
350 /* Work out the intended interleave setting */
351 this->interleave = 0;
352 if (this->ChipID == DOC_ChipID_DocMilPlus32)
353 this->interleave = 1;
355 /* Check the ASIC agrees */
356 if ( (this->interleave << 2) !=
357 (ReadDOC(this->virtadr, Mplus_Configuration) & 4)) {
358 u_char conf = ReadDOC(this->virtadr, Mplus_Configuration);
359 printk(KERN_NOTICE "Setting DiskOnChip Millennium Plus interleave to %s\n",
360 this->interleave?"on (16-bit)":"off (8-bit)");
361 conf ^= 4;
362 WriteDOC(conf, this->virtadr, Mplus_Configuration);
365 /* For each floor, find the number of valid chips it contains */
366 for (floor = 0,ret = 1; floor < MAX_FLOORS_MPLUS; floor++) {
367 numchips[floor] = 0;
368 for (chip = 0; chip < MAX_CHIPS_MPLUS && ret != 0; chip++) {
369 ret = DoC_IdentChip(this, floor, chip);
370 if (ret) {
371 numchips[floor]++;
372 this->numchips++;
376 /* If there are none at all that we recognise, bail */
377 if (!this->numchips) {
378 printk("No flash chips recognised.\n");
379 return;
382 /* Allocate an array to hold the information for each chip */
383 this->chips = kmalloc(sizeof(struct Nand) * this->numchips, GFP_KERNEL);
384 if (!this->chips){
385 printk("MTD: No memory for allocating chip info structures\n");
386 return;
389 /* Fill out the chip array with {floor, chipno} for each
390 * detected chip in the device. */
391 for (floor = 0, ret = 0; floor < MAX_FLOORS_MPLUS; floor++) {
392 for (chip = 0 ; chip < numchips[floor] ; chip++) {
393 this->chips[ret].floor = floor;
394 this->chips[ret].chip = chip;
395 this->chips[ret].curadr = 0;
396 this->chips[ret].curmode = 0x50;
397 ret++;
401 /* Calculate and print the total size of the device */
402 this->totlen = this->numchips * (1 << this->chipshift);
403 printk(KERN_INFO "%d flash chips found. Total DiskOnChip size: %ld MiB\n",
404 this->numchips ,this->totlen >> 20);
407 static int DoCMilPlus_is_alias(struct DiskOnChip *doc1, struct DiskOnChip *doc2)
409 int tmp1, tmp2, retval;
411 if (doc1->physadr == doc2->physadr)
412 return 1;
414 /* Use the alias resolution register which was set aside for this
415 * purpose. If it's value is the same on both chips, they might
416 * be the same chip, and we write to one and check for a change in
417 * the other. It's unclear if this register is usuable in the
418 * DoC 2000 (it's in the Millennium docs), but it seems to work. */
419 tmp1 = ReadDOC(doc1->virtadr, Mplus_AliasResolution);
420 tmp2 = ReadDOC(doc2->virtadr, Mplus_AliasResolution);
421 if (tmp1 != tmp2)
422 return 0;
424 WriteDOC((tmp1+1) % 0xff, doc1->virtadr, Mplus_AliasResolution);
425 tmp2 = ReadDOC(doc2->virtadr, Mplus_AliasResolution);
426 if (tmp2 == (tmp1+1) % 0xff)
427 retval = 1;
428 else
429 retval = 0;
431 /* Restore register contents. May not be necessary, but do it just to
432 * be safe. */
433 WriteDOC(tmp1, doc1->virtadr, Mplus_AliasResolution);
435 return retval;
438 /* This routine is found from the docprobe code by symbol_get(),
439 * which will bump the use count of this module. */
440 void DoCMilPlus_init(struct mtd_info *mtd)
442 struct DiskOnChip *this = mtd->priv;
443 struct DiskOnChip *old = NULL;
445 /* We must avoid being called twice for the same device. */
446 if (docmilpluslist)
447 old = docmilpluslist->priv;
449 while (old) {
450 if (DoCMilPlus_is_alias(this, old)) {
451 printk(KERN_NOTICE "Ignoring DiskOnChip Millennium "
452 "Plus at 0x%lX - already configured\n",
453 this->physadr);
454 iounmap(this->virtadr);
455 kfree(mtd);
456 return;
458 if (old->nextdoc)
459 old = old->nextdoc->priv;
460 else
461 old = NULL;
464 mtd->name = "DiskOnChip Millennium Plus";
465 printk(KERN_NOTICE "DiskOnChip Millennium Plus found at "
466 "address 0x%lX\n", this->physadr);
468 mtd->type = MTD_NANDFLASH;
469 mtd->flags = MTD_CAP_NANDFLASH;
470 mtd->size = 0;
472 mtd->erasesize = 0;
473 mtd->writesize = 512;
474 mtd->oobsize = 16;
475 mtd->owner = THIS_MODULE;
476 mtd->erase = doc_erase;
477 mtd->point = NULL;
478 mtd->unpoint = NULL;
479 mtd->read = doc_read;
480 mtd->write = doc_write;
481 mtd->read_oob = doc_read_oob;
482 mtd->write_oob = doc_write_oob;
483 mtd->sync = NULL;
485 this->totlen = 0;
486 this->numchips = 0;
487 this->curfloor = -1;
488 this->curchip = -1;
490 /* Ident all the chips present. */
491 DoC_ScanChips(this);
493 if (!this->totlen) {
494 kfree(mtd);
495 iounmap(this->virtadr);
496 } else {
497 this->nextdoc = docmilpluslist;
498 docmilpluslist = mtd;
499 mtd->size = this->totlen;
500 mtd->erasesize = this->erasesize;
501 mtd_device_register(mtd, NULL, 0);
502 return;
505 EXPORT_SYMBOL_GPL(DoCMilPlus_init);
507 #if 0
508 static int doc_dumpblk(struct mtd_info *mtd, loff_t from)
510 int i;
511 loff_t fofs;
512 struct DiskOnChip *this = mtd->priv;
513 void __iomem * docptr = this->virtadr;
514 struct Nand *mychip = &this->chips[from >> (this->chipshift)];
515 unsigned char *bp, buf[1056];
516 char c[32];
518 from &= ~0x3ff;
520 /* Don't allow read past end of device */
521 if (from >= this->totlen)
522 return -EINVAL;
524 DoC_CheckASIC(docptr);
526 /* Find the chip which is to be used and select it */
527 if (this->curfloor != mychip->floor) {
528 DoC_SelectFloor(docptr, mychip->floor);
529 DoC_SelectChip(docptr, mychip->chip);
530 } else if (this->curchip != mychip->chip) {
531 DoC_SelectChip(docptr, mychip->chip);
533 this->curfloor = mychip->floor;
534 this->curchip = mychip->chip;
536 /* Millennium Plus bus cycle sequence as per figure 2, section 2.4 */
537 WriteDOC((DOC_FLASH_CE | DOC_FLASH_WP), docptr, Mplus_FlashSelect);
539 /* Reset the chip, see Software Requirement 11.4 item 1. */
540 DoC_Command(docptr, NAND_CMD_RESET, 0);
541 DoC_WaitReady(docptr);
543 fofs = from;
544 DoC_Command(docptr, DoC_GetDataOffset(mtd, &fofs), 0);
545 DoC_Address(this, 3, fofs, 0, 0x00);
546 WriteDOC(0, docptr, Mplus_FlashControl);
547 DoC_WaitReady(docptr);
549 /* disable the ECC engine */
550 WriteDOC(DOC_ECC_RESET, docptr, Mplus_ECCConf);
552 ReadDOC(docptr, Mplus_ReadPipeInit);
553 ReadDOC(docptr, Mplus_ReadPipeInit);
555 /* Read the data via the internal pipeline through CDSN IO
556 register, see Pipelined Read Operations 11.3 */
557 MemReadDOC(docptr, buf, 1054);
558 buf[1054] = ReadDOC(docptr, Mplus_LastDataRead);
559 buf[1055] = ReadDOC(docptr, Mplus_LastDataRead);
561 memset(&c[0], 0, sizeof(c));
562 printk("DUMP OFFSET=%x:\n", (int)from);
564 for (i = 0, bp = &buf[0]; (i < 1056); i++) {
565 if ((i % 16) == 0)
566 printk("%08x: ", i);
567 printk(" %02x", *bp);
568 c[(i & 0xf)] = ((*bp >= 0x20) && (*bp <= 0x7f)) ? *bp : '.';
569 bp++;
570 if (((i + 1) % 16) == 0)
571 printk(" %s\n", c);
573 printk("\n");
575 /* Disable flash internally */
576 WriteDOC(0, docptr, Mplus_FlashSelect);
578 return 0;
580 #endif
582 static int doc_read(struct mtd_info *mtd, loff_t from, size_t len,
583 size_t *retlen, u_char *buf)
585 int ret, i;
586 volatile char dummy;
587 loff_t fofs;
588 unsigned char syndrome[6], eccbuf[6];
589 struct DiskOnChip *this = mtd->priv;
590 void __iomem * docptr = this->virtadr;
591 struct Nand *mychip = &this->chips[from >> (this->chipshift)];
593 /* Don't allow read past end of device */
594 if (from >= this->totlen)
595 return -EINVAL;
597 /* Don't allow a single read to cross a 512-byte block boundary */
598 if (from + len > ((from | 0x1ff) + 1))
599 len = ((from | 0x1ff) + 1) - from;
601 DoC_CheckASIC(docptr);
603 /* Find the chip which is to be used and select it */
604 if (this->curfloor != mychip->floor) {
605 DoC_SelectFloor(docptr, mychip->floor);
606 DoC_SelectChip(docptr, mychip->chip);
607 } else if (this->curchip != mychip->chip) {
608 DoC_SelectChip(docptr, mychip->chip);
610 this->curfloor = mychip->floor;
611 this->curchip = mychip->chip;
613 /* Millennium Plus bus cycle sequence as per figure 2, section 2.4 */
614 WriteDOC((DOC_FLASH_CE | DOC_FLASH_WP), docptr, Mplus_FlashSelect);
616 /* Reset the chip, see Software Requirement 11.4 item 1. */
617 DoC_Command(docptr, NAND_CMD_RESET, 0);
618 DoC_WaitReady(docptr);
620 fofs = from;
621 DoC_Command(docptr, DoC_GetDataOffset(mtd, &fofs), 0);
622 DoC_Address(this, 3, fofs, 0, 0x00);
623 WriteDOC(0, docptr, Mplus_FlashControl);
624 DoC_WaitReady(docptr);
626 /* init the ECC engine, see Reed-Solomon EDC/ECC 11.1 .*/
627 WriteDOC(DOC_ECC_RESET, docptr, Mplus_ECCConf);
628 WriteDOC(DOC_ECC_EN, docptr, Mplus_ECCConf);
630 /* Let the caller know we completed it */
631 *retlen = len;
632 ret = 0;
634 ReadDOC(docptr, Mplus_ReadPipeInit);
635 ReadDOC(docptr, Mplus_ReadPipeInit);
637 /* Read the data via the internal pipeline through CDSN IO
638 register, see Pipelined Read Operations 11.3 */
639 MemReadDOC(docptr, buf, len);
641 /* Read the ECC data following raw data */
642 MemReadDOC(docptr, eccbuf, 4);
643 eccbuf[4] = ReadDOC(docptr, Mplus_LastDataRead);
644 eccbuf[5] = ReadDOC(docptr, Mplus_LastDataRead);
646 /* Flush the pipeline */
647 dummy = ReadDOC(docptr, Mplus_ECCConf);
648 dummy = ReadDOC(docptr, Mplus_ECCConf);
650 /* Check the ECC Status */
651 if (ReadDOC(docptr, Mplus_ECCConf) & 0x80) {
652 int nb_errors;
653 /* There was an ECC error */
654 #ifdef ECC_DEBUG
655 printk("DiskOnChip ECC Error: Read at %lx\n", (long)from);
656 #endif
657 /* Read the ECC syndrome through the DiskOnChip ECC logic.
658 These syndrome will be all ZERO when there is no error */
659 for (i = 0; i < 6; i++)
660 syndrome[i] = ReadDOC(docptr, Mplus_ECCSyndrome0 + i);
662 nb_errors = doc_decode_ecc(buf, syndrome);
663 #ifdef ECC_DEBUG
664 printk("ECC Errors corrected: %x\n", nb_errors);
665 #endif
666 if (nb_errors < 0) {
667 /* We return error, but have actually done the
668 read. Not that this can be told to user-space, via
669 sys_read(), but at least MTD-aware stuff can know
670 about it by checking *retlen */
671 #ifdef ECC_DEBUG
672 printk("%s(%d): Millennium Plus ECC error (from=0x%x:\n",
673 __FILE__, __LINE__, (int)from);
674 printk(" syndrome= %02x:%02x:%02x:%02x:%02x:"
675 "%02x\n",
676 syndrome[0], syndrome[1], syndrome[2],
677 syndrome[3], syndrome[4], syndrome[5]);
678 printk(" eccbuf= %02x:%02x:%02x:%02x:%02x:"
679 "%02x\n",
680 eccbuf[0], eccbuf[1], eccbuf[2],
681 eccbuf[3], eccbuf[4], eccbuf[5]);
682 #endif
683 ret = -EIO;
687 #ifdef PSYCHO_DEBUG
688 printk("ECC DATA at %lx: %2.2X %2.2X %2.2X %2.2X %2.2X %2.2X\n",
689 (long)from, eccbuf[0], eccbuf[1], eccbuf[2], eccbuf[3],
690 eccbuf[4], eccbuf[5]);
691 #endif
692 /* disable the ECC engine */
693 WriteDOC(DOC_ECC_DIS, docptr , Mplus_ECCConf);
695 /* Disable flash internally */
696 WriteDOC(0, docptr, Mplus_FlashSelect);
698 return ret;
701 static int doc_write(struct mtd_info *mtd, loff_t to, size_t len,
702 size_t *retlen, const u_char *buf)
704 int i, before, ret = 0;
705 loff_t fto;
706 volatile char dummy;
707 char eccbuf[6];
708 struct DiskOnChip *this = mtd->priv;
709 void __iomem * docptr = this->virtadr;
710 struct Nand *mychip = &this->chips[to >> (this->chipshift)];
712 /* Don't allow write past end of device */
713 if (to >= this->totlen)
714 return -EINVAL;
716 /* Don't allow writes which aren't exactly one block (512 bytes) */
717 if ((to & 0x1ff) || (len != 0x200))
718 return -EINVAL;
720 /* Determine position of OOB flags, before or after data */
721 before = (this->interleave && (to & 0x200));
723 DoC_CheckASIC(docptr);
725 /* Find the chip which is to be used and select it */
726 if (this->curfloor != mychip->floor) {
727 DoC_SelectFloor(docptr, mychip->floor);
728 DoC_SelectChip(docptr, mychip->chip);
729 } else if (this->curchip != mychip->chip) {
730 DoC_SelectChip(docptr, mychip->chip);
732 this->curfloor = mychip->floor;
733 this->curchip = mychip->chip;
735 /* Millennium Plus bus cycle sequence as per figure 2, section 2.4 */
736 WriteDOC(DOC_FLASH_CE, docptr, Mplus_FlashSelect);
738 /* Reset the chip, see Software Requirement 11.4 item 1. */
739 DoC_Command(docptr, NAND_CMD_RESET, 0);
740 DoC_WaitReady(docptr);
742 /* Set device to appropriate plane of flash */
743 fto = to;
744 WriteDOC(DoC_GetDataOffset(mtd, &fto), docptr, Mplus_FlashCmd);
746 /* On interleaved devices the flags for 2nd half 512 are before data */
747 if (before)
748 fto -= 2;
750 /* issue the Serial Data In command to initial the Page Program process */
751 DoC_Command(docptr, NAND_CMD_SEQIN, 0x00);
752 DoC_Address(this, 3, fto, 0x00, 0x00);
754 /* Disable the ECC engine */
755 WriteDOC(DOC_ECC_RESET, docptr, Mplus_ECCConf);
757 if (before) {
758 /* Write the block status BLOCK_USED (0x5555) */
759 WriteDOC(0x55, docptr, Mil_CDSN_IO);
760 WriteDOC(0x55, docptr, Mil_CDSN_IO);
763 /* init the ECC engine, see Reed-Solomon EDC/ECC 11.1 .*/
764 WriteDOC(DOC_ECC_EN | DOC_ECC_RW, docptr, Mplus_ECCConf);
766 MemWriteDOC(docptr, (unsigned char *) buf, len);
768 /* Write ECC data to flash, the ECC info is generated by
769 the DiskOnChip ECC logic see Reed-Solomon EDC/ECC 11.1 */
770 DoC_Delay(docptr, 3);
772 /* Read the ECC data through the DiskOnChip ECC logic */
773 for (i = 0; i < 6; i++)
774 eccbuf[i] = ReadDOC(docptr, Mplus_ECCSyndrome0 + i);
776 /* disable the ECC engine */
777 WriteDOC(DOC_ECC_DIS, docptr, Mplus_ECCConf);
779 /* Write the ECC data to flash */
780 MemWriteDOC(docptr, eccbuf, 6);
782 if (!before) {
783 /* Write the block status BLOCK_USED (0x5555) */
784 WriteDOC(0x55, docptr, Mil_CDSN_IO+6);
785 WriteDOC(0x55, docptr, Mil_CDSN_IO+7);
788 #ifdef PSYCHO_DEBUG
789 printk("OOB data at %lx is %2.2X %2.2X %2.2X %2.2X %2.2X %2.2X\n",
790 (long) to, eccbuf[0], eccbuf[1], eccbuf[2], eccbuf[3],
791 eccbuf[4], eccbuf[5]);
792 #endif
794 WriteDOC(0x00, docptr, Mplus_WritePipeTerm);
795 WriteDOC(0x00, docptr, Mplus_WritePipeTerm);
797 /* Commit the Page Program command and wait for ready
798 see Software Requirement 11.4 item 1.*/
799 DoC_Command(docptr, NAND_CMD_PAGEPROG, 0x00);
800 DoC_WaitReady(docptr);
802 /* Read the status of the flash device through CDSN IO register
803 see Software Requirement 11.4 item 5.*/
804 DoC_Command(docptr, NAND_CMD_STATUS, 0);
805 dummy = ReadDOC(docptr, Mplus_ReadPipeInit);
806 dummy = ReadDOC(docptr, Mplus_ReadPipeInit);
807 DoC_Delay(docptr, 2);
808 if ((dummy = ReadDOC(docptr, Mplus_LastDataRead)) & 1) {
809 printk("MTD: Error 0x%x programming at 0x%x\n", dummy, (int)to);
810 /* Error in programming
811 FIXME: implement Bad Block Replacement (in nftl.c ??) */
812 *retlen = 0;
813 ret = -EIO;
815 dummy = ReadDOC(docptr, Mplus_LastDataRead);
817 /* Disable flash internally */
818 WriteDOC(0, docptr, Mplus_FlashSelect);
820 /* Let the caller know we completed it */
821 *retlen = len;
823 return ret;
826 static int doc_read_oob(struct mtd_info *mtd, loff_t ofs,
827 struct mtd_oob_ops *ops)
829 loff_t fofs, base;
830 struct DiskOnChip *this = mtd->priv;
831 void __iomem * docptr = this->virtadr;
832 struct Nand *mychip = &this->chips[ofs >> this->chipshift];
833 size_t i, size, got, want;
834 uint8_t *buf = ops->oobbuf;
835 size_t len = ops->len;
837 BUG_ON(ops->mode != MTD_OPS_PLACE_OOB);
839 ofs += ops->ooboffs;
841 DoC_CheckASIC(docptr);
843 /* Find the chip which is to be used and select it */
844 if (this->curfloor != mychip->floor) {
845 DoC_SelectFloor(docptr, mychip->floor);
846 DoC_SelectChip(docptr, mychip->chip);
847 } else if (this->curchip != mychip->chip) {
848 DoC_SelectChip(docptr, mychip->chip);
850 this->curfloor = mychip->floor;
851 this->curchip = mychip->chip;
853 /* Millennium Plus bus cycle sequence as per figure 2, section 2.4 */
854 WriteDOC((DOC_FLASH_CE | DOC_FLASH_WP), docptr, Mplus_FlashSelect);
856 /* disable the ECC engine */
857 WriteDOC(DOC_ECC_RESET, docptr, Mplus_ECCConf);
858 DoC_WaitReady(docptr);
860 /* Maximum of 16 bytes in the OOB region, so limit read to that */
861 if (len > 16)
862 len = 16;
863 got = 0;
864 want = len;
866 for (i = 0; ((i < 3) && (want > 0)); i++) {
867 /* Figure out which region we are accessing... */
868 fofs = ofs;
869 base = ofs & 0xf;
870 if (!this->interleave) {
871 DoC_Command(docptr, NAND_CMD_READOOB, 0);
872 size = 16 - base;
873 } else if (base < 6) {
874 DoC_Command(docptr, DoC_GetECCOffset(mtd, &fofs), 0);
875 size = 6 - base;
876 } else if (base < 8) {
877 DoC_Command(docptr, DoC_GetFlagsOffset(mtd, &fofs), 0);
878 size = 8 - base;
879 } else {
880 DoC_Command(docptr, DoC_GetHdrOffset(mtd, &fofs), 0);
881 size = 16 - base;
883 if (size > want)
884 size = want;
886 /* Issue read command */
887 DoC_Address(this, 3, fofs, 0, 0x00);
888 WriteDOC(0, docptr, Mplus_FlashControl);
889 DoC_WaitReady(docptr);
891 ReadDOC(docptr, Mplus_ReadPipeInit);
892 ReadDOC(docptr, Mplus_ReadPipeInit);
893 MemReadDOC(docptr, &buf[got], size - 2);
894 buf[got + size - 2] = ReadDOC(docptr, Mplus_LastDataRead);
895 buf[got + size - 1] = ReadDOC(docptr, Mplus_LastDataRead);
897 ofs += size;
898 got += size;
899 want -= size;
902 /* Disable flash internally */
903 WriteDOC(0, docptr, Mplus_FlashSelect);
905 ops->retlen = len;
906 return 0;
909 static int doc_write_oob(struct mtd_info *mtd, loff_t ofs,
910 struct mtd_oob_ops *ops)
912 volatile char dummy;
913 loff_t fofs, base;
914 struct DiskOnChip *this = mtd->priv;
915 void __iomem * docptr = this->virtadr;
916 struct Nand *mychip = &this->chips[ofs >> this->chipshift];
917 size_t i, size, got, want;
918 int ret = 0;
919 uint8_t *buf = ops->oobbuf;
920 size_t len = ops->len;
922 BUG_ON(ops->mode != MTD_OPS_PLACE_OOB);
924 ofs += ops->ooboffs;
926 DoC_CheckASIC(docptr);
928 /* Find the chip which is to be used and select it */
929 if (this->curfloor != mychip->floor) {
930 DoC_SelectFloor(docptr, mychip->floor);
931 DoC_SelectChip(docptr, mychip->chip);
932 } else if (this->curchip != mychip->chip) {
933 DoC_SelectChip(docptr, mychip->chip);
935 this->curfloor = mychip->floor;
936 this->curchip = mychip->chip;
938 /* Millennium Plus bus cycle sequence as per figure 2, section 2.4 */
939 WriteDOC(DOC_FLASH_CE, docptr, Mplus_FlashSelect);
942 /* Maximum of 16 bytes in the OOB region, so limit write to that */
943 if (len > 16)
944 len = 16;
945 got = 0;
946 want = len;
948 for (i = 0; ((i < 3) && (want > 0)); i++) {
949 /* Reset the chip, see Software Requirement 11.4 item 1. */
950 DoC_Command(docptr, NAND_CMD_RESET, 0);
951 DoC_WaitReady(docptr);
953 /* Figure out which region we are accessing... */
954 fofs = ofs;
955 base = ofs & 0x0f;
956 if (!this->interleave) {
957 WriteDOC(NAND_CMD_READOOB, docptr, Mplus_FlashCmd);
958 size = 16 - base;
959 } else if (base < 6) {
960 WriteDOC(DoC_GetECCOffset(mtd, &fofs), docptr, Mplus_FlashCmd);
961 size = 6 - base;
962 } else if (base < 8) {
963 WriteDOC(DoC_GetFlagsOffset(mtd, &fofs), docptr, Mplus_FlashCmd);
964 size = 8 - base;
965 } else {
966 WriteDOC(DoC_GetHdrOffset(mtd, &fofs), docptr, Mplus_FlashCmd);
967 size = 16 - base;
969 if (size > want)
970 size = want;
972 /* Issue the Serial Data In command to initial the Page Program process */
973 DoC_Command(docptr, NAND_CMD_SEQIN, 0x00);
974 DoC_Address(this, 3, fofs, 0, 0x00);
976 /* Disable the ECC engine */
977 WriteDOC(DOC_ECC_RESET, docptr, Mplus_ECCConf);
979 /* Write the data via the internal pipeline through CDSN IO
980 register, see Pipelined Write Operations 11.2 */
981 MemWriteDOC(docptr, (unsigned char *) &buf[got], size);
982 WriteDOC(0x00, docptr, Mplus_WritePipeTerm);
983 WriteDOC(0x00, docptr, Mplus_WritePipeTerm);
985 /* Commit the Page Program command and wait for ready
986 see Software Requirement 11.4 item 1.*/
987 DoC_Command(docptr, NAND_CMD_PAGEPROG, 0x00);
988 DoC_WaitReady(docptr);
990 /* Read the status of the flash device through CDSN IO register
991 see Software Requirement 11.4 item 5.*/
992 DoC_Command(docptr, NAND_CMD_STATUS, 0x00);
993 dummy = ReadDOC(docptr, Mplus_ReadPipeInit);
994 dummy = ReadDOC(docptr, Mplus_ReadPipeInit);
995 DoC_Delay(docptr, 2);
996 if ((dummy = ReadDOC(docptr, Mplus_LastDataRead)) & 1) {
997 printk("MTD: Error 0x%x programming oob at 0x%x\n",
998 dummy, (int)ofs);
999 /* FIXME: implement Bad Block Replacement */
1000 ops->retlen = 0;
1001 ret = -EIO;
1003 dummy = ReadDOC(docptr, Mplus_LastDataRead);
1005 ofs += size;
1006 got += size;
1007 want -= size;
1010 /* Disable flash internally */
1011 WriteDOC(0, docptr, Mplus_FlashSelect);
1013 ops->retlen = len;
1014 return ret;
1017 int doc_erase(struct mtd_info *mtd, struct erase_info *instr)
1019 volatile char dummy;
1020 struct DiskOnChip *this = mtd->priv;
1021 __u32 ofs = instr->addr;
1022 __u32 len = instr->len;
1023 void __iomem * docptr = this->virtadr;
1024 struct Nand *mychip = &this->chips[ofs >> this->chipshift];
1026 DoC_CheckASIC(docptr);
1028 if (len != mtd->erasesize)
1029 printk(KERN_WARNING "MTD: Erase not right size (%x != %x)n",
1030 len, mtd->erasesize);
1032 /* Find the chip which is to be used and select it */
1033 if (this->curfloor != mychip->floor) {
1034 DoC_SelectFloor(docptr, mychip->floor);
1035 DoC_SelectChip(docptr, mychip->chip);
1036 } else if (this->curchip != mychip->chip) {
1037 DoC_SelectChip(docptr, mychip->chip);
1039 this->curfloor = mychip->floor;
1040 this->curchip = mychip->chip;
1042 instr->state = MTD_ERASE_PENDING;
1044 /* Millennium Plus bus cycle sequence as per figure 2, section 2.4 */
1045 WriteDOC(DOC_FLASH_CE, docptr, Mplus_FlashSelect);
1047 DoC_Command(docptr, NAND_CMD_RESET, 0x00);
1048 DoC_WaitReady(docptr);
1050 DoC_Command(docptr, NAND_CMD_ERASE1, 0);
1051 DoC_Address(this, 2, ofs, 0, 0x00);
1052 DoC_Command(docptr, NAND_CMD_ERASE2, 0);
1053 DoC_WaitReady(docptr);
1054 instr->state = MTD_ERASING;
1056 /* Read the status of the flash device through CDSN IO register
1057 see Software Requirement 11.4 item 5. */
1058 DoC_Command(docptr, NAND_CMD_STATUS, 0);
1059 dummy = ReadDOC(docptr, Mplus_ReadPipeInit);
1060 dummy = ReadDOC(docptr, Mplus_ReadPipeInit);
1061 if ((dummy = ReadDOC(docptr, Mplus_LastDataRead)) & 1) {
1062 printk("MTD: Error 0x%x erasing at 0x%x\n", dummy, ofs);
1063 /* FIXME: implement Bad Block Replacement (in nftl.c ??) */
1064 instr->state = MTD_ERASE_FAILED;
1065 } else {
1066 instr->state = MTD_ERASE_DONE;
1068 dummy = ReadDOC(docptr, Mplus_LastDataRead);
1070 /* Disable flash internally */
1071 WriteDOC(0, docptr, Mplus_FlashSelect);
1073 mtd_erase_callback(instr);
1075 return 0;
1078 /****************************************************************************
1080 * Module stuff
1082 ****************************************************************************/
1084 static void __exit cleanup_doc2001plus(void)
1086 struct mtd_info *mtd;
1087 struct DiskOnChip *this;
1089 while ((mtd=docmilpluslist)) {
1090 this = mtd->priv;
1091 docmilpluslist = this->nextdoc;
1093 mtd_device_unregister(mtd);
1095 iounmap(this->virtadr);
1096 kfree(this->chips);
1097 kfree(mtd);
1101 module_exit(cleanup_doc2001plus);
1103 MODULE_LICENSE("GPL");
1104 MODULE_AUTHOR("Greg Ungerer <gerg@snapgear.com> et al.");
1105 MODULE_DESCRIPTION("Driver for DiskOnChip Millennium Plus");