blob: 1ee0c0dcb53be58ddf8930b5801b0b4ba4079c4d [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Linux driver for Disk-On-Chip Millennium Plus
3 *
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>
8 *
Thomas Gleixnere5580fb2005-11-07 11:15:40 +00009 * $Id: doc2001plus.c,v 1.14 2005/11/07 11:14:24 gleixner Exp $
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 *
11 * Released under GPL
12 */
13
14#include <linux/kernel.h>
15#include <linux/module.h>
16#include <asm/errno.h>
17#include <asm/io.h>
18#include <asm/uaccess.h>
19#include <linux/miscdevice.h>
20#include <linux/pci.h>
21#include <linux/delay.h>
22#include <linux/slab.h>
23#include <linux/sched.h>
24#include <linux/init.h>
25#include <linux/types.h>
26#include <linux/bitops.h>
27
28#include <linux/mtd/mtd.h>
29#include <linux/mtd/nand.h>
30#include <linux/mtd/doc2000.h>
31
32/* #define ECC_DEBUG */
33
34/* I have no idea why some DoC chips can not use memcop_form|to_io().
35 * This may be due to the different revisions of the ASIC controller built-in or
36 * simplily a QA/Bug issue. Who knows ?? If you have trouble, please uncomment
37 * this:*/
38#undef USE_MEMCPY
39
40static int doc_read(struct mtd_info *mtd, loff_t from, size_t len,
41 size_t *retlen, u_char *buf);
42static int doc_write(struct mtd_info *mtd, loff_t to, size_t len,
43 size_t *retlen, const u_char *buf);
44static int doc_read_ecc(struct mtd_info *mtd, loff_t from, size_t len,
45 size_t *retlen, u_char *buf, u_char *eccbuf,
46 struct nand_oobinfo *oobsel);
47static int doc_write_ecc(struct mtd_info *mtd, loff_t to, size_t len,
48 size_t *retlen, const u_char *buf, u_char *eccbuf,
49 struct nand_oobinfo *oobsel);
50static int doc_read_oob(struct mtd_info *mtd, loff_t ofs, size_t len,
51 size_t *retlen, u_char *buf);
52static int doc_write_oob(struct mtd_info *mtd, loff_t ofs, size_t len,
53 size_t *retlen, const u_char *buf);
54static int doc_erase (struct mtd_info *mtd, struct erase_info *instr);
55
56static struct mtd_info *docmilpluslist = NULL;
57
58
59/* Perform the required delay cycles by writing to the NOP register */
60static void DoC_Delay(void __iomem * docptr, int cycles)
61{
62 int i;
63
64 for (i = 0; (i < cycles); i++)
65 WriteDOC(0, docptr, Mplus_NOP);
66}
67
68#define CDSN_CTRL_FR_B_MASK (CDSN_CTRL_FR_B0 | CDSN_CTRL_FR_B1)
69
70/* DOC_WaitReady: Wait for RDY line to be asserted by the flash chip */
71static int _DoC_WaitReady(void __iomem * docptr)
72{
73 unsigned int c = 0xffff;
74
75 DEBUG(MTD_DEBUG_LEVEL3,
76 "_DoC_WaitReady called for out-of-line wait\n");
77
78 /* Out-of-line routine to wait for chip response */
79 while (((ReadDOC(docptr, Mplus_FlashControl) & CDSN_CTRL_FR_B_MASK) != CDSN_CTRL_FR_B_MASK) && --c)
80 ;
81
82 if (c == 0)
83 DEBUG(MTD_DEBUG_LEVEL2, "_DoC_WaitReady timed out.\n");
84
85 return (c == 0);
86}
87
88static inline int DoC_WaitReady(void __iomem * docptr)
89{
90 /* This is inline, to optimise the common case, where it's ready instantly */
91 int ret = 0;
92
93 /* read form NOP register should be issued prior to the read from CDSNControl
94 see Software Requirement 11.4 item 2. */
95 DoC_Delay(docptr, 4);
96
97 if ((ReadDOC(docptr, Mplus_FlashControl) & CDSN_CTRL_FR_B_MASK) != CDSN_CTRL_FR_B_MASK)
98 /* Call the out-of-line routine to wait */
99 ret = _DoC_WaitReady(docptr);
100
101 return ret;
102}
103
104/* For some reason the Millennium Plus seems to occassionally put itself
105 * into reset mode. For me this happens randomly, with no pattern that I
106 * can detect. M-systems suggest always check this on any block level
107 * operation and setting to normal mode if in reset mode.
108 */
109static inline void DoC_CheckASIC(void __iomem * docptr)
110{
111 /* Make sure the DoC is in normal mode */
112 if ((ReadDOC(docptr, Mplus_DOCControl) & DOC_MODE_NORMAL) == 0) {
113 WriteDOC((DOC_MODE_NORMAL | DOC_MODE_MDWREN), docptr, Mplus_DOCControl);
114 WriteDOC(~(DOC_MODE_NORMAL | DOC_MODE_MDWREN), docptr, Mplus_CtrlConfirm);
115 }
116}
117
118/* DoC_Command: Send a flash command to the flash chip through the Flash
119 * command register. Need 2 Write Pipeline Terminates to complete send.
120 */
Arjan van de Ven858119e2006-01-14 13:20:43 -0800121static void DoC_Command(void __iomem * docptr, unsigned char command,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 unsigned char xtraflags)
123{
124 WriteDOC(command, docptr, Mplus_FlashCmd);
125 WriteDOC(command, docptr, Mplus_WritePipeTerm);
126 WriteDOC(command, docptr, Mplus_WritePipeTerm);
127}
128
129/* DoC_Address: Set the current address for the flash chip through the Flash
130 * Address register. Need 2 Write Pipeline Terminates to complete send.
131 */
132static inline void DoC_Address(struct DiskOnChip *doc, int numbytes,
133 unsigned long ofs, unsigned char xtraflags1,
134 unsigned char xtraflags2)
135{
136 void __iomem * docptr = doc->virtadr;
137
138 /* Allow for possible Mill Plus internal flash interleaving */
139 ofs >>= doc->interleave;
140
141 switch (numbytes) {
142 case 1:
143 /* Send single byte, bits 0-7. */
144 WriteDOC(ofs & 0xff, docptr, Mplus_FlashAddress);
145 break;
146 case 2:
147 /* Send bits 9-16 followed by 17-23 */
148 WriteDOC((ofs >> 9) & 0xff, docptr, Mplus_FlashAddress);
149 WriteDOC((ofs >> 17) & 0xff, docptr, Mplus_FlashAddress);
150 break;
151 case 3:
152 /* Send 0-7, 9-16, then 17-23 */
153 WriteDOC(ofs & 0xff, docptr, Mplus_FlashAddress);
154 WriteDOC((ofs >> 9) & 0xff, docptr, Mplus_FlashAddress);
155 WriteDOC((ofs >> 17) & 0xff, docptr, Mplus_FlashAddress);
156 break;
157 default:
158 return;
159 }
160
161 WriteDOC(0x00, docptr, Mplus_WritePipeTerm);
162 WriteDOC(0x00, docptr, Mplus_WritePipeTerm);
163}
164
165/* DoC_SelectChip: Select a given flash chip within the current floor */
166static int DoC_SelectChip(void __iomem * docptr, int chip)
167{
168 /* No choice for flash chip on Millennium Plus */
169 return 0;
170}
171
172/* DoC_SelectFloor: Select a given floor (bank of flash chips) */
173static int DoC_SelectFloor(void __iomem * docptr, int floor)
174{
175 WriteDOC((floor & 0x3), docptr, Mplus_DeviceSelect);
176 return 0;
177}
178
179/*
180 * Translate the given offset into the appropriate command and offset.
181 * This does the mapping using the 16bit interleave layout defined by
182 * M-Systems, and looks like this for a sector pair:
183 * +-----------+-------+-------+-------+--------------+---------+-----------+
184 * | 0 --- 511 |512-517|518-519|520-521| 522 --- 1033 |1034-1039|1040 - 1055|
185 * +-----------+-------+-------+-------+--------------+---------+-----------+
186 * | Data 0 | ECC 0 |Flags0 |Flags1 | Data 1 |ECC 1 | OOB 1 + 2 |
187 * +-----------+-------+-------+-------+--------------+---------+-----------+
188 */
189/* FIXME: This lives in INFTL not here. Other users of flash devices
190 may not want it */
191static unsigned int DoC_GetDataOffset(struct mtd_info *mtd, loff_t *from)
192{
193 struct DiskOnChip *this = mtd->priv;
194
195 if (this->interleave) {
196 unsigned int ofs = *from & 0x3ff;
197 unsigned int cmd;
198
199 if (ofs < 512) {
200 cmd = NAND_CMD_READ0;
201 ofs &= 0x1ff;
202 } else if (ofs < 1014) {
203 cmd = NAND_CMD_READ1;
204 ofs = (ofs & 0x1ff) + 10;
205 } else {
206 cmd = NAND_CMD_READOOB;
207 ofs = ofs - 1014;
208 }
209
210 *from = (*from & ~0x3ff) | ofs;
211 return cmd;
212 } else {
213 /* No interleave */
214 if ((*from) & 0x100)
215 return NAND_CMD_READ1;
216 return NAND_CMD_READ0;
217 }
218}
219
220static unsigned int DoC_GetECCOffset(struct mtd_info *mtd, loff_t *from)
221{
222 unsigned int ofs, cmd;
223
224 if (*from & 0x200) {
225 cmd = NAND_CMD_READOOB;
226 ofs = 10 + (*from & 0xf);
227 } else {
228 cmd = NAND_CMD_READ1;
229 ofs = (*from & 0xf);
230 }
231
232 *from = (*from & ~0x3ff) | ofs;
233 return cmd;
234}
235
236static unsigned int DoC_GetFlagsOffset(struct mtd_info *mtd, loff_t *from)
237{
238 unsigned int ofs, cmd;
239
240 cmd = NAND_CMD_READ1;
241 ofs = (*from & 0x200) ? 8 : 6;
242 *from = (*from & ~0x3ff) | ofs;
243 return cmd;
244}
245
246static unsigned int DoC_GetHdrOffset(struct mtd_info *mtd, loff_t *from)
247{
248 unsigned int ofs, cmd;
249
250 cmd = NAND_CMD_READOOB;
251 ofs = (*from & 0x200) ? 24 : 16;
252 *from = (*from & ~0x3ff) | ofs;
253 return cmd;
254}
255
256static inline void MemReadDOC(void __iomem * docptr, unsigned char *buf, int len)
257{
258#ifndef USE_MEMCPY
259 int i;
260 for (i = 0; i < len; i++)
261 buf[i] = ReadDOC(docptr, Mil_CDSN_IO + i);
262#else
263 memcpy_fromio(buf, docptr + DoC_Mil_CDSN_IO, len);
264#endif
265}
266
267static inline void MemWriteDOC(void __iomem * docptr, unsigned char *buf, int len)
268{
269#ifndef USE_MEMCPY
270 int i;
271 for (i = 0; i < len; i++)
272 WriteDOC(buf[i], docptr, Mil_CDSN_IO + i);
273#else
274 memcpy_toio(docptr + DoC_Mil_CDSN_IO, buf, len);
275#endif
276}
277
278/* DoC_IdentChip: Identify a given NAND chip given {floor,chip} */
279static int DoC_IdentChip(struct DiskOnChip *doc, int floor, int chip)
280{
281 int mfr, id, i, j;
282 volatile char dummy;
283 void __iomem * docptr = doc->virtadr;
284
285 /* Page in the required floor/chip */
286 DoC_SelectFloor(docptr, floor);
287 DoC_SelectChip(docptr, chip);
288
289 /* Millennium Plus bus cycle sequence as per figure 2, section 2.4 */
290 WriteDOC((DOC_FLASH_CE | DOC_FLASH_WP), docptr, Mplus_FlashSelect);
291
292 /* Reset the chip, see Software Requirement 11.4 item 1. */
293 DoC_Command(docptr, NAND_CMD_RESET, 0);
294 DoC_WaitReady(docptr);
295
Thomas Gleixnere5580fb2005-11-07 11:15:40 +0000296 /* Read the NAND chip ID: 1. Send ReadID command */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 DoC_Command(docptr, NAND_CMD_READID, 0);
298
Thomas Gleixnere5580fb2005-11-07 11:15:40 +0000299 /* Read the NAND chip ID: 2. Send address byte zero */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 DoC_Address(doc, 1, 0x00, 0, 0x00);
301
302 WriteDOC(0, docptr, Mplus_FlashControl);
303 DoC_WaitReady(docptr);
304
305 /* Read the manufacturer and device id codes of the flash device through
306 CDSN IO register see Software Requirement 11.4 item 5.*/
307 dummy = ReadDOC(docptr, Mplus_ReadPipeInit);
308 dummy = ReadDOC(docptr, Mplus_ReadPipeInit);
309
310 mfr = ReadDOC(docptr, Mil_CDSN_IO);
311 if (doc->interleave)
312 dummy = ReadDOC(docptr, Mil_CDSN_IO); /* 2 way interleave */
313
314 id = ReadDOC(docptr, Mil_CDSN_IO);
315 if (doc->interleave)
316 dummy = ReadDOC(docptr, Mil_CDSN_IO); /* 2 way interleave */
317
318 dummy = ReadDOC(docptr, Mplus_LastDataRead);
319 dummy = ReadDOC(docptr, Mplus_LastDataRead);
320
321 /* Disable flash internally */
322 WriteDOC(0, docptr, Mplus_FlashSelect);
323
324 /* No response - return failure */
325 if (mfr == 0xff || mfr == 0)
326 return 0;
327
328 for (i = 0; nand_flash_ids[i].name != NULL; i++) {
329 if (id == nand_flash_ids[i].id) {
330 /* Try to identify manufacturer */
331 for (j = 0; nand_manuf_ids[j].id != 0x0; j++) {
332 if (nand_manuf_ids[j].id == mfr)
333 break;
334 }
335 printk(KERN_INFO "Flash chip found: Manufacturer ID: %2.2X, "
336 "Chip ID: %2.2X (%s:%s)\n", mfr, id,
337 nand_manuf_ids[j].name, nand_flash_ids[i].name);
338 doc->mfr = mfr;
339 doc->id = id;
340 doc->chipshift = ffs((nand_flash_ids[i].chipsize << 20)) - 1;
341 doc->erasesize = nand_flash_ids[i].erasesize << doc->interleave;
342 break;
343 }
344 }
345
346 if (nand_flash_ids[i].name == NULL)
347 return 0;
348 return 1;
349}
350
351/* DoC_ScanChips: Find all NAND chips present in a DiskOnChip, and identify them */
352static void DoC_ScanChips(struct DiskOnChip *this)
353{
354 int floor, chip;
355 int numchips[MAX_FLOORS_MPLUS];
356 int ret;
357
358 this->numchips = 0;
359 this->mfr = 0;
360 this->id = 0;
361
362 /* Work out the intended interleave setting */
363 this->interleave = 0;
364 if (this->ChipID == DOC_ChipID_DocMilPlus32)
365 this->interleave = 1;
366
367 /* Check the ASIC agrees */
Thomas Gleixnere5580fb2005-11-07 11:15:40 +0000368 if ( (this->interleave << 2) !=
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 (ReadDOC(this->virtadr, Mplus_Configuration) & 4)) {
370 u_char conf = ReadDOC(this->virtadr, Mplus_Configuration);
371 printk(KERN_NOTICE "Setting DiskOnChip Millennium Plus interleave to %s\n",
372 this->interleave?"on (16-bit)":"off (8-bit)");
373 conf ^= 4;
374 WriteDOC(conf, this->virtadr, Mplus_Configuration);
375 }
376
377 /* For each floor, find the number of valid chips it contains */
378 for (floor = 0,ret = 1; floor < MAX_FLOORS_MPLUS; floor++) {
379 numchips[floor] = 0;
380 for (chip = 0; chip < MAX_CHIPS_MPLUS && ret != 0; chip++) {
381 ret = DoC_IdentChip(this, floor, chip);
382 if (ret) {
383 numchips[floor]++;
384 this->numchips++;
385 }
386 }
387 }
388 /* If there are none at all that we recognise, bail */
389 if (!this->numchips) {
390 printk("No flash chips recognised.\n");
391 return;
392 }
393
394 /* Allocate an array to hold the information for each chip */
395 this->chips = kmalloc(sizeof(struct Nand) * this->numchips, GFP_KERNEL);
396 if (!this->chips){
397 printk("MTD: No memory for allocating chip info structures\n");
398 return;
399 }
400
Thomas Gleixnere5580fb2005-11-07 11:15:40 +0000401 /* Fill out the chip array with {floor, chipno} for each
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 * detected chip in the device. */
403 for (floor = 0, ret = 0; floor < MAX_FLOORS_MPLUS; floor++) {
404 for (chip = 0 ; chip < numchips[floor] ; chip++) {
405 this->chips[ret].floor = floor;
406 this->chips[ret].chip = chip;
407 this->chips[ret].curadr = 0;
408 this->chips[ret].curmode = 0x50;
409 ret++;
410 }
411 }
412
413 /* Calculate and print the total size of the device */
414 this->totlen = this->numchips * (1 << this->chipshift);
415 printk(KERN_INFO "%d flash chips found. Total DiskOnChip size: %ld MiB\n",
416 this->numchips ,this->totlen >> 20);
417}
418
419static int DoCMilPlus_is_alias(struct DiskOnChip *doc1, struct DiskOnChip *doc2)
420{
421 int tmp1, tmp2, retval;
422
423 if (doc1->physadr == doc2->physadr)
424 return 1;
425
426 /* Use the alias resolution register which was set aside for this
427 * purpose. If it's value is the same on both chips, they might
428 * be the same chip, and we write to one and check for a change in
429 * the other. It's unclear if this register is usuable in the
430 * DoC 2000 (it's in the Millennium docs), but it seems to work. */
431 tmp1 = ReadDOC(doc1->virtadr, Mplus_AliasResolution);
432 tmp2 = ReadDOC(doc2->virtadr, Mplus_AliasResolution);
433 if (tmp1 != tmp2)
434 return 0;
Thomas Gleixnere5580fb2005-11-07 11:15:40 +0000435
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 WriteDOC((tmp1+1) % 0xff, doc1->virtadr, Mplus_AliasResolution);
437 tmp2 = ReadDOC(doc2->virtadr, Mplus_AliasResolution);
438 if (tmp2 == (tmp1+1) % 0xff)
439 retval = 1;
440 else
441 retval = 0;
442
443 /* Restore register contents. May not be necessary, but do it just to
444 * be safe. */
445 WriteDOC(tmp1, doc1->virtadr, Mplus_AliasResolution);
446
447 return retval;
448}
449
David Woodhouse5e535422006-05-08 14:05:05 +0100450/* This routine is found from the docprobe code by symbol_get(),
451 * which will bump the use count of this module. */
452void DoCMilPlus_init(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453{
454 struct DiskOnChip *this = mtd->priv;
455 struct DiskOnChip *old = NULL;
456
457 /* We must avoid being called twice for the same device. */
458 if (docmilpluslist)
459 old = docmilpluslist->priv;
460
461 while (old) {
462 if (DoCMilPlus_is_alias(this, old)) {
463 printk(KERN_NOTICE "Ignoring DiskOnChip Millennium "
464 "Plus at 0x%lX - already configured\n",
465 this->physadr);
466 iounmap(this->virtadr);
467 kfree(mtd);
468 return;
469 }
470 if (old->nextdoc)
471 old = old->nextdoc->priv;
472 else
473 old = NULL;
474 }
475
476 mtd->name = "DiskOnChip Millennium Plus";
477 printk(KERN_NOTICE "DiskOnChip Millennium Plus found at "
478 "address 0x%lX\n", this->physadr);
479
480 mtd->type = MTD_NANDFLASH;
481 mtd->flags = MTD_CAP_NANDFLASH;
482 mtd->ecctype = MTD_ECC_RS_DiskOnChip;
483 mtd->size = 0;
484
485 mtd->erasesize = 0;
Joern Engel28318772006-05-22 23:18:05 +0200486 mtd->writesize = 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 mtd->oobsize = 16;
488 mtd->owner = THIS_MODULE;
489 mtd->erase = doc_erase;
490 mtd->point = NULL;
491 mtd->unpoint = NULL;
492 mtd->read = doc_read;
493 mtd->write = doc_write;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 mtd->read_oob = doc_read_oob;
495 mtd->write_oob = doc_write_oob;
496 mtd->sync = NULL;
497
498 this->totlen = 0;
499 this->numchips = 0;
500 this->curfloor = -1;
501 this->curchip = -1;
502
503 /* Ident all the chips present. */
504 DoC_ScanChips(this);
505
506 if (!this->totlen) {
507 kfree(mtd);
508 iounmap(this->virtadr);
509 } else {
510 this->nextdoc = docmilpluslist;
511 docmilpluslist = mtd;
512 mtd->size = this->totlen;
513 mtd->erasesize = this->erasesize;
514 add_mtd_device(mtd);
515 return;
516 }
517}
David Woodhouseb04ecae2006-05-10 16:16:13 +0100518EXPORT_SYMBOL_GPL(DoCMilPlus_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519
520#if 0
521static int doc_dumpblk(struct mtd_info *mtd, loff_t from)
522{
523 int i;
524 loff_t fofs;
525 struct DiskOnChip *this = mtd->priv;
526 void __iomem * docptr = this->virtadr;
527 struct Nand *mychip = &this->chips[from >> (this->chipshift)];
528 unsigned char *bp, buf[1056];
529 char c[32];
530
531 from &= ~0x3ff;
532
533 /* Don't allow read past end of device */
534 if (from >= this->totlen)
535 return -EINVAL;
536
537 DoC_CheckASIC(docptr);
538
539 /* Find the chip which is to be used and select it */
540 if (this->curfloor != mychip->floor) {
541 DoC_SelectFloor(docptr, mychip->floor);
542 DoC_SelectChip(docptr, mychip->chip);
543 } else if (this->curchip != mychip->chip) {
544 DoC_SelectChip(docptr, mychip->chip);
545 }
546 this->curfloor = mychip->floor;
547 this->curchip = mychip->chip;
548
549 /* Millennium Plus bus cycle sequence as per figure 2, section 2.4 */
550 WriteDOC((DOC_FLASH_CE | DOC_FLASH_WP), docptr, Mplus_FlashSelect);
551
552 /* Reset the chip, see Software Requirement 11.4 item 1. */
553 DoC_Command(docptr, NAND_CMD_RESET, 0);
554 DoC_WaitReady(docptr);
555
556 fofs = from;
557 DoC_Command(docptr, DoC_GetDataOffset(mtd, &fofs), 0);
558 DoC_Address(this, 3, fofs, 0, 0x00);
559 WriteDOC(0, docptr, Mplus_FlashControl);
560 DoC_WaitReady(docptr);
561
562 /* disable the ECC engine */
563 WriteDOC(DOC_ECC_RESET, docptr, Mplus_ECCConf);
564
565 ReadDOC(docptr, Mplus_ReadPipeInit);
566 ReadDOC(docptr, Mplus_ReadPipeInit);
567
568 /* Read the data via the internal pipeline through CDSN IO
569 register, see Pipelined Read Operations 11.3 */
570 MemReadDOC(docptr, buf, 1054);
571 buf[1054] = ReadDOC(docptr, Mplus_LastDataRead);
572 buf[1055] = ReadDOC(docptr, Mplus_LastDataRead);
573
574 memset(&c[0], 0, sizeof(c));
575 printk("DUMP OFFSET=%x:\n", (int)from);
576
577 for (i = 0, bp = &buf[0]; (i < 1056); i++) {
578 if ((i % 16) == 0)
579 printk("%08x: ", i);
580 printk(" %02x", *bp);
581 c[(i & 0xf)] = ((*bp >= 0x20) && (*bp <= 0x7f)) ? *bp : '.';
582 bp++;
583 if (((i + 1) % 16) == 0)
584 printk(" %s\n", c);
585 }
586 printk("\n");
587
588 /* Disable flash internally */
589 WriteDOC(0, docptr, Mplus_FlashSelect);
590
591 return 0;
592}
593#endif
594
595static int doc_read(struct mtd_info *mtd, loff_t from, size_t len,
596 size_t *retlen, u_char *buf)
597{
598 /* Just a special case of doc_read_ecc */
599 return doc_read_ecc(mtd, from, len, retlen, buf, NULL, NULL);
600}
601
602static int doc_read_ecc(struct mtd_info *mtd, loff_t from, size_t len,
603 size_t *retlen, u_char *buf, u_char *eccbuf,
604 struct nand_oobinfo *oobsel)
605{
606 int ret, i;
607 volatile char dummy;
608 loff_t fofs;
609 unsigned char syndrome[6];
610 struct DiskOnChip *this = mtd->priv;
611 void __iomem * docptr = this->virtadr;
612 struct Nand *mychip = &this->chips[from >> (this->chipshift)];
613
614 /* Don't allow read past end of device */
615 if (from >= this->totlen)
616 return -EINVAL;
617
618 /* Don't allow a single read to cross a 512-byte block boundary */
Thomas Gleixnere5580fb2005-11-07 11:15:40 +0000619 if (from + len > ((from | 0x1ff) + 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 len = ((from | 0x1ff) + 1) - from;
621
622 DoC_CheckASIC(docptr);
623
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);
630 }
631 this->curfloor = mychip->floor;
632 this->curchip = mychip->chip;
633
634 /* Millennium Plus bus cycle sequence as per figure 2, section 2.4 */
635 WriteDOC((DOC_FLASH_CE | DOC_FLASH_WP), docptr, Mplus_FlashSelect);
636
637 /* Reset the chip, see Software Requirement 11.4 item 1. */
638 DoC_Command(docptr, NAND_CMD_RESET, 0);
639 DoC_WaitReady(docptr);
640
641 fofs = from;
642 DoC_Command(docptr, DoC_GetDataOffset(mtd, &fofs), 0);
643 DoC_Address(this, 3, fofs, 0, 0x00);
644 WriteDOC(0, docptr, Mplus_FlashControl);
645 DoC_WaitReady(docptr);
646
647 if (eccbuf) {
648 /* init the ECC engine, see Reed-Solomon EDC/ECC 11.1 .*/
649 WriteDOC(DOC_ECC_RESET, docptr, Mplus_ECCConf);
650 WriteDOC(DOC_ECC_EN, docptr, Mplus_ECCConf);
651 } else {
652 /* disable the ECC engine */
653 WriteDOC(DOC_ECC_RESET, docptr, Mplus_ECCConf);
654 }
655
656 /* Let the caller know we completed it */
657 *retlen = len;
658 ret = 0;
659
660 ReadDOC(docptr, Mplus_ReadPipeInit);
661 ReadDOC(docptr, Mplus_ReadPipeInit);
662
663 if (eccbuf) {
664 /* Read the data via the internal pipeline through CDSN IO
665 register, see Pipelined Read Operations 11.3 */
666 MemReadDOC(docptr, buf, len);
667
668 /* Read the ECC data following raw data */
669 MemReadDOC(docptr, eccbuf, 4);
670 eccbuf[4] = ReadDOC(docptr, Mplus_LastDataRead);
671 eccbuf[5] = ReadDOC(docptr, Mplus_LastDataRead);
672
673 /* Flush the pipeline */
674 dummy = ReadDOC(docptr, Mplus_ECCConf);
675 dummy = ReadDOC(docptr, Mplus_ECCConf);
676
677 /* Check the ECC Status */
678 if (ReadDOC(docptr, Mplus_ECCConf) & 0x80) {
679 int nb_errors;
680 /* There was an ECC error */
681#ifdef ECC_DEBUG
682 printk("DiskOnChip ECC Error: Read at %lx\n", (long)from);
683#endif
684 /* Read the ECC syndrom through the DiskOnChip ECC logic.
685 These syndrome will be all ZERO when there is no error */
686 for (i = 0; i < 6; i++)
687 syndrome[i] = ReadDOC(docptr, Mplus_ECCSyndrome0 + i);
688
689 nb_errors = doc_decode_ecc(buf, syndrome);
690#ifdef ECC_DEBUG
691 printk("ECC Errors corrected: %x\n", nb_errors);
692#endif
693 if (nb_errors < 0) {
694 /* We return error, but have actually done the read. Not that
695 this can be told to user-space, via sys_read(), but at least
696 MTD-aware stuff can know about it by checking *retlen */
697#ifdef ECC_DEBUG
698 printk("%s(%d): Millennium Plus ECC error (from=0x%x:\n",
699 __FILE__, __LINE__, (int)from);
700 printk(" syndrome= %02x:%02x:%02x:%02x:%02x:"
701 "%02x\n",
702 syndrome[0], syndrome[1], syndrome[2],
703 syndrome[3], syndrome[4], syndrome[5]);
704 printk(" eccbuf= %02x:%02x:%02x:%02x:%02x:"
705 "%02x\n",
706 eccbuf[0], eccbuf[1], eccbuf[2],
707 eccbuf[3], eccbuf[4], eccbuf[5]);
708#endif
709 ret = -EIO;
710 }
711 }
712
713#ifdef PSYCHO_DEBUG
714 printk("ECC DATA at %lx: %2.2X %2.2X %2.2X %2.2X %2.2X %2.2X\n",
715 (long)from, eccbuf[0], eccbuf[1], eccbuf[2], eccbuf[3],
716 eccbuf[4], eccbuf[5]);
717#endif
718
719 /* disable the ECC engine */
720 WriteDOC(DOC_ECC_DIS, docptr , Mplus_ECCConf);
721 } else {
722 /* Read the data via the internal pipeline through CDSN IO
723 register, see Pipelined Read Operations 11.3 */
724 MemReadDOC(docptr, buf, len-2);
725 buf[len-2] = ReadDOC(docptr, Mplus_LastDataRead);
726 buf[len-1] = ReadDOC(docptr, Mplus_LastDataRead);
727 }
728
729 /* Disable flash internally */
730 WriteDOC(0, docptr, Mplus_FlashSelect);
731
732 return ret;
733}
734
735static int doc_write(struct mtd_info *mtd, loff_t to, size_t len,
736 size_t *retlen, const u_char *buf)
737{
738 char eccbuf[6];
739 return doc_write_ecc(mtd, to, len, retlen, buf, eccbuf, NULL);
740}
741
742static int doc_write_ecc(struct mtd_info *mtd, loff_t to, size_t len,
743 size_t *retlen, const u_char *buf, u_char *eccbuf,
744 struct nand_oobinfo *oobsel)
745{
746 int i, before, ret = 0;
747 loff_t fto;
748 volatile char dummy;
749 struct DiskOnChip *this = mtd->priv;
750 void __iomem * docptr = this->virtadr;
751 struct Nand *mychip = &this->chips[to >> (this->chipshift)];
752
753 /* Don't allow write past end of device */
754 if (to >= this->totlen)
755 return -EINVAL;
756
757 /* Don't allow writes which aren't exactly one block (512 bytes) */
758 if ((to & 0x1ff) || (len != 0x200))
759 return -EINVAL;
760
761 /* Determine position of OOB flags, before or after data */
762 before = (this->interleave && (to & 0x200));
763
764 DoC_CheckASIC(docptr);
765
766 /* Find the chip which is to be used and select it */
767 if (this->curfloor != mychip->floor) {
768 DoC_SelectFloor(docptr, mychip->floor);
769 DoC_SelectChip(docptr, mychip->chip);
770 } else if (this->curchip != mychip->chip) {
771 DoC_SelectChip(docptr, mychip->chip);
772 }
773 this->curfloor = mychip->floor;
774 this->curchip = mychip->chip;
775
776 /* Millennium Plus bus cycle sequence as per figure 2, section 2.4 */
777 WriteDOC(DOC_FLASH_CE, docptr, Mplus_FlashSelect);
778
779 /* Reset the chip, see Software Requirement 11.4 item 1. */
780 DoC_Command(docptr, NAND_CMD_RESET, 0);
781 DoC_WaitReady(docptr);
782
783 /* Set device to appropriate plane of flash */
784 fto = to;
785 WriteDOC(DoC_GetDataOffset(mtd, &fto), docptr, Mplus_FlashCmd);
786
787 /* On interleaved devices the flags for 2nd half 512 are before data */
788 if (eccbuf && before)
789 fto -= 2;
790
791 /* issue the Serial Data In command to initial the Page Program process */
792 DoC_Command(docptr, NAND_CMD_SEQIN, 0x00);
793 DoC_Address(this, 3, fto, 0x00, 0x00);
794
795 /* Disable the ECC engine */
796 WriteDOC(DOC_ECC_RESET, docptr, Mplus_ECCConf);
797
798 if (eccbuf) {
799 if (before) {
800 /* Write the block status BLOCK_USED (0x5555) */
801 WriteDOC(0x55, docptr, Mil_CDSN_IO);
802 WriteDOC(0x55, docptr, Mil_CDSN_IO);
803 }
804
805 /* init the ECC engine, see Reed-Solomon EDC/ECC 11.1 .*/
806 WriteDOC(DOC_ECC_EN | DOC_ECC_RW, docptr, Mplus_ECCConf);
807 }
808
809 MemWriteDOC(docptr, (unsigned char *) buf, len);
810
811 if (eccbuf) {
812 /* Write ECC data to flash, the ECC info is generated by
813 the DiskOnChip ECC logic see Reed-Solomon EDC/ECC 11.1 */
814 DoC_Delay(docptr, 3);
815
816 /* Read the ECC data through the DiskOnChip ECC logic */
817 for (i = 0; i < 6; i++)
818 eccbuf[i] = ReadDOC(docptr, Mplus_ECCSyndrome0 + i);
819
820 /* disable the ECC engine */
821 WriteDOC(DOC_ECC_DIS, docptr, Mplus_ECCConf);
822
823 /* Write the ECC data to flash */
824 MemWriteDOC(docptr, eccbuf, 6);
825
826 if (!before) {
827 /* Write the block status BLOCK_USED (0x5555) */
828 WriteDOC(0x55, docptr, Mil_CDSN_IO+6);
829 WriteDOC(0x55, docptr, Mil_CDSN_IO+7);
830 }
831
832#ifdef PSYCHO_DEBUG
833 printk("OOB data at %lx is %2.2X %2.2X %2.2X %2.2X %2.2X %2.2X\n",
834 (long) to, eccbuf[0], eccbuf[1], eccbuf[2], eccbuf[3],
835 eccbuf[4], eccbuf[5]);
836#endif
837 }
838
839 WriteDOC(0x00, docptr, Mplus_WritePipeTerm);
840 WriteDOC(0x00, docptr, Mplus_WritePipeTerm);
841
842 /* Commit the Page Program command and wait for ready
843 see Software Requirement 11.4 item 1.*/
844 DoC_Command(docptr, NAND_CMD_PAGEPROG, 0x00);
845 DoC_WaitReady(docptr);
846
847 /* Read the status of the flash device through CDSN IO register
848 see Software Requirement 11.4 item 5.*/
849 DoC_Command(docptr, NAND_CMD_STATUS, 0);
850 dummy = ReadDOC(docptr, Mplus_ReadPipeInit);
851 dummy = ReadDOC(docptr, Mplus_ReadPipeInit);
852 DoC_Delay(docptr, 2);
853 if ((dummy = ReadDOC(docptr, Mplus_LastDataRead)) & 1) {
854 printk("MTD: Error 0x%x programming at 0x%x\n", dummy, (int)to);
855 /* Error in programming
856 FIXME: implement Bad Block Replacement (in nftl.c ??) */
857 *retlen = 0;
858 ret = -EIO;
859 }
860 dummy = ReadDOC(docptr, Mplus_LastDataRead);
861
862 /* Disable flash internally */
863 WriteDOC(0, docptr, Mplus_FlashSelect);
864
865 /* Let the caller know we completed it */
866 *retlen = len;
867
868 return ret;
869}
870
871static int doc_read_oob(struct mtd_info *mtd, loff_t ofs, size_t len,
872 size_t *retlen, u_char *buf)
873{
874 loff_t fofs, base;
875 struct DiskOnChip *this = mtd->priv;
876 void __iomem * docptr = this->virtadr;
877 struct Nand *mychip = &this->chips[ofs >> this->chipshift];
878 size_t i, size, got, want;
879
880 DoC_CheckASIC(docptr);
881
882 /* Find the chip which is to be used and select it */
883 if (this->curfloor != mychip->floor) {
884 DoC_SelectFloor(docptr, mychip->floor);
885 DoC_SelectChip(docptr, mychip->chip);
886 } else if (this->curchip != mychip->chip) {
887 DoC_SelectChip(docptr, mychip->chip);
888 }
889 this->curfloor = mychip->floor;
890 this->curchip = mychip->chip;
891
892 /* Millennium Plus bus cycle sequence as per figure 2, section 2.4 */
893 WriteDOC((DOC_FLASH_CE | DOC_FLASH_WP), docptr, Mplus_FlashSelect);
894
895 /* disable the ECC engine */
896 WriteDOC(DOC_ECC_RESET, docptr, Mplus_ECCConf);
897 DoC_WaitReady(docptr);
898
899 /* Maximum of 16 bytes in the OOB region, so limit read to that */
900 if (len > 16)
901 len = 16;
902 got = 0;
903 want = len;
904
905 for (i = 0; ((i < 3) && (want > 0)); i++) {
906 /* Figure out which region we are accessing... */
907 fofs = ofs;
908 base = ofs & 0xf;
909 if (!this->interleave) {
910 DoC_Command(docptr, NAND_CMD_READOOB, 0);
911 size = 16 - base;
912 } else if (base < 6) {
913 DoC_Command(docptr, DoC_GetECCOffset(mtd, &fofs), 0);
914 size = 6 - base;
915 } else if (base < 8) {
916 DoC_Command(docptr, DoC_GetFlagsOffset(mtd, &fofs), 0);
917 size = 8 - base;
918 } else {
919 DoC_Command(docptr, DoC_GetHdrOffset(mtd, &fofs), 0);
920 size = 16 - base;
921 }
922 if (size > want)
923 size = want;
924
925 /* Issue read command */
926 DoC_Address(this, 3, fofs, 0, 0x00);
927 WriteDOC(0, docptr, Mplus_FlashControl);
928 DoC_WaitReady(docptr);
929
930 ReadDOC(docptr, Mplus_ReadPipeInit);
931 ReadDOC(docptr, Mplus_ReadPipeInit);
932 MemReadDOC(docptr, &buf[got], size - 2);
933 buf[got + size - 2] = ReadDOC(docptr, Mplus_LastDataRead);
934 buf[got + size - 1] = ReadDOC(docptr, Mplus_LastDataRead);
935
936 ofs += size;
937 got += size;
938 want -= size;
939 }
940
941 /* Disable flash internally */
942 WriteDOC(0, docptr, Mplus_FlashSelect);
943
944 *retlen = len;
945 return 0;
946}
947
948static int doc_write_oob(struct mtd_info *mtd, loff_t ofs, size_t len,
949 size_t *retlen, const u_char *buf)
950{
951 volatile char dummy;
952 loff_t fofs, base;
953 struct DiskOnChip *this = mtd->priv;
954 void __iomem * docptr = this->virtadr;
955 struct Nand *mychip = &this->chips[ofs >> this->chipshift];
956 size_t i, size, got, want;
957 int ret = 0;
958
959 DoC_CheckASIC(docptr);
960
961 /* Find the chip which is to be used and select it */
962 if (this->curfloor != mychip->floor) {
963 DoC_SelectFloor(docptr, mychip->floor);
964 DoC_SelectChip(docptr, mychip->chip);
965 } else if (this->curchip != mychip->chip) {
966 DoC_SelectChip(docptr, mychip->chip);
967 }
968 this->curfloor = mychip->floor;
969 this->curchip = mychip->chip;
970
971 /* Millennium Plus bus cycle sequence as per figure 2, section 2.4 */
972 WriteDOC(DOC_FLASH_CE, docptr, Mplus_FlashSelect);
973
974
975 /* Maximum of 16 bytes in the OOB region, so limit write to that */
976 if (len > 16)
977 len = 16;
978 got = 0;
979 want = len;
980
981 for (i = 0; ((i < 3) && (want > 0)); i++) {
982 /* Reset the chip, see Software Requirement 11.4 item 1. */
983 DoC_Command(docptr, NAND_CMD_RESET, 0);
984 DoC_WaitReady(docptr);
985
986 /* Figure out which region we are accessing... */
987 fofs = ofs;
988 base = ofs & 0x0f;
989 if (!this->interleave) {
990 WriteDOC(NAND_CMD_READOOB, docptr, Mplus_FlashCmd);
991 size = 16 - base;
992 } else if (base < 6) {
993 WriteDOC(DoC_GetECCOffset(mtd, &fofs), docptr, Mplus_FlashCmd);
994 size = 6 - base;
995 } else if (base < 8) {
996 WriteDOC(DoC_GetFlagsOffset(mtd, &fofs), docptr, Mplus_FlashCmd);
997 size = 8 - base;
998 } else {
999 WriteDOC(DoC_GetHdrOffset(mtd, &fofs), docptr, Mplus_FlashCmd);
1000 size = 16 - base;
1001 }
1002 if (size > want)
1003 size = want;
1004
1005 /* Issue the Serial Data In command to initial the Page Program process */
1006 DoC_Command(docptr, NAND_CMD_SEQIN, 0x00);
1007 DoC_Address(this, 3, fofs, 0, 0x00);
1008
1009 /* Disable the ECC engine */
1010 WriteDOC(DOC_ECC_RESET, docptr, Mplus_ECCConf);
1011
1012 /* Write the data via the internal pipeline through CDSN IO
1013 register, see Pipelined Write Operations 11.2 */
1014 MemWriteDOC(docptr, (unsigned char *) &buf[got], size);
1015 WriteDOC(0x00, docptr, Mplus_WritePipeTerm);
1016 WriteDOC(0x00, docptr, Mplus_WritePipeTerm);
1017
1018 /* Commit the Page Program command and wait for ready
1019 see Software Requirement 11.4 item 1.*/
1020 DoC_Command(docptr, NAND_CMD_PAGEPROG, 0x00);
1021 DoC_WaitReady(docptr);
1022
1023 /* Read the status of the flash device through CDSN IO register
1024 see Software Requirement 11.4 item 5.*/
1025 DoC_Command(docptr, NAND_CMD_STATUS, 0x00);
1026 dummy = ReadDOC(docptr, Mplus_ReadPipeInit);
1027 dummy = ReadDOC(docptr, Mplus_ReadPipeInit);
1028 DoC_Delay(docptr, 2);
1029 if ((dummy = ReadDOC(docptr, Mplus_LastDataRead)) & 1) {
1030 printk("MTD: Error 0x%x programming oob at 0x%x\n",
1031 dummy, (int)ofs);
1032 /* FIXME: implement Bad Block Replacement */
1033 *retlen = 0;
1034 ret = -EIO;
1035 }
1036 dummy = ReadDOC(docptr, Mplus_LastDataRead);
1037
1038 ofs += size;
1039 got += size;
1040 want -= size;
1041 }
1042
1043 /* Disable flash internally */
1044 WriteDOC(0, docptr, Mplus_FlashSelect);
1045
1046 *retlen = len;
1047 return ret;
1048}
1049
1050int doc_erase(struct mtd_info *mtd, struct erase_info *instr)
1051{
1052 volatile char dummy;
1053 struct DiskOnChip *this = mtd->priv;
1054 __u32 ofs = instr->addr;
1055 __u32 len = instr->len;
1056 void __iomem * docptr = this->virtadr;
1057 struct Nand *mychip = &this->chips[ofs >> this->chipshift];
1058
1059 DoC_CheckASIC(docptr);
1060
Thomas Gleixnere5580fb2005-11-07 11:15:40 +00001061 if (len != mtd->erasesize)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 printk(KERN_WARNING "MTD: Erase not right size (%x != %x)n",
1063 len, mtd->erasesize);
1064
1065 /* Find the chip which is to be used and select it */
1066 if (this->curfloor != mychip->floor) {
1067 DoC_SelectFloor(docptr, mychip->floor);
1068 DoC_SelectChip(docptr, mychip->chip);
1069 } else if (this->curchip != mychip->chip) {
1070 DoC_SelectChip(docptr, mychip->chip);
1071 }
1072 this->curfloor = mychip->floor;
1073 this->curchip = mychip->chip;
1074
1075 instr->state = MTD_ERASE_PENDING;
1076
1077 /* Millennium Plus bus cycle sequence as per figure 2, section 2.4 */
1078 WriteDOC(DOC_FLASH_CE, docptr, Mplus_FlashSelect);
1079
1080 DoC_Command(docptr, NAND_CMD_RESET, 0x00);
1081 DoC_WaitReady(docptr);
1082
1083 DoC_Command(docptr, NAND_CMD_ERASE1, 0);
1084 DoC_Address(this, 2, ofs, 0, 0x00);
1085 DoC_Command(docptr, NAND_CMD_ERASE2, 0);
1086 DoC_WaitReady(docptr);
1087 instr->state = MTD_ERASING;
1088
1089 /* Read the status of the flash device through CDSN IO register
1090 see Software Requirement 11.4 item 5. */
1091 DoC_Command(docptr, NAND_CMD_STATUS, 0);
1092 dummy = ReadDOC(docptr, Mplus_ReadPipeInit);
1093 dummy = ReadDOC(docptr, Mplus_ReadPipeInit);
1094 if ((dummy = ReadDOC(docptr, Mplus_LastDataRead)) & 1) {
1095 printk("MTD: Error 0x%x erasing at 0x%x\n", dummy, ofs);
1096 /* FIXME: implement Bad Block Replacement (in nftl.c ??) */
1097 instr->state = MTD_ERASE_FAILED;
1098 } else {
1099 instr->state = MTD_ERASE_DONE;
1100 }
1101 dummy = ReadDOC(docptr, Mplus_LastDataRead);
1102
1103 /* Disable flash internally */
1104 WriteDOC(0, docptr, Mplus_FlashSelect);
1105
1106 mtd_erase_callback(instr);
1107
1108 return 0;
1109}
1110
1111/****************************************************************************
1112 *
1113 * Module stuff
1114 *
1115 ****************************************************************************/
1116
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117static void __exit cleanup_doc2001plus(void)
1118{
1119 struct mtd_info *mtd;
1120 struct DiskOnChip *this;
1121
1122 while ((mtd=docmilpluslist)) {
1123 this = mtd->priv;
1124 docmilpluslist = this->nextdoc;
Thomas Gleixnere5580fb2005-11-07 11:15:40 +00001125
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 del_mtd_device(mtd);
Thomas Gleixnere5580fb2005-11-07 11:15:40 +00001127
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 iounmap(this->virtadr);
1129 kfree(this->chips);
1130 kfree(mtd);
1131 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132}
1133
1134module_exit(cleanup_doc2001plus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135
1136MODULE_LICENSE("GPL");
1137MODULE_AUTHOR("Greg Ungerer <gerg@snapgear.com> et al.");
1138MODULE_DESCRIPTION("Driver for DiskOnChip Millennium Plus");