File: /usr/src/linux/drivers/mtd/devices/doc2000.c
1
2 /*
3 * Linux driver for Disk-On-Chip 2000 and Millennium
4 * (c) 1999 Machine Vision Holdings, Inc.
5 * (c) 1999, 2000 David Woodhouse <dwmw2@infradead.org>
6 *
7 * $Id: doc2000.c,v 1.43 2001/06/02 14:30:43 dwmw2 Exp $
8 */
9
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <asm/errno.h>
13 #include <asm/io.h>
14 #include <asm/uaccess.h>
15 #include <linux/miscdevice.h>
16 #include <linux/pci.h>
17 #include <linux/delay.h>
18 #include <linux/slab.h>
19 #include <linux/sched.h>
20 #include <linux/init.h>
21 #include <linux/types.h>
22
23 #include <linux/mtd/mtd.h>
24 #include <linux/mtd/nand.h>
25 #include <linux/mtd/nand_ids.h>
26 #include <linux/mtd/doc2000.h>
27
28 #define DOC_SUPPORT_2000
29 #define DOC_SUPPORT_MILLENNIUM
30
31 #ifdef DOC_SUPPORT_2000
32 #define DoC_is_2000(doc) (doc->ChipID == DOC_ChipID_Doc2k)
33 #else
34 #define DoC_is_2000(doc) (0)
35 #endif
36
37 #ifdef DOC_SUPPORT_MILLENNIUM
38 #define DoC_is_Millennium(doc) (doc->ChipID == DOC_ChipID_DocMil)
39 #else
40 #define DoC_is_Millennium(doc) (0)
41 #endif
42
43 /* #define ECC_DEBUG */
44
45 /* I have no idea why some DoC chips can not use memcpy_from|to_io().
46 * This may be due to the different revisions of the ASIC controller built-in or
47 * simplily a QA/Bug issue. Who knows ?? If you have trouble, please uncomment
48 * this:
49 #undef USE_MEMCPY
50 */
51
52 static int doc_read(struct mtd_info *mtd, loff_t from, size_t len,
53 size_t *retlen, u_char *buf);
54 static int doc_write(struct mtd_info *mtd, loff_t to, size_t len,
55 size_t *retlen, const u_char *buf);
56 static int doc_read_ecc(struct mtd_info *mtd, loff_t from, size_t len,
57 size_t *retlen, u_char *buf, u_char *eccbuf);
58 static int doc_write_ecc(struct mtd_info *mtd, loff_t to, size_t len,
59 size_t *retlen, const u_char *buf, u_char *eccbuf);
60 static int doc_read_oob(struct mtd_info *mtd, loff_t ofs, size_t len,
61 size_t *retlen, u_char *buf);
62 static int doc_write_oob(struct mtd_info *mtd, loff_t ofs, size_t len,
63 size_t *retlen, const u_char *buf);
64 static int doc_erase (struct mtd_info *mtd, struct erase_info *instr);
65
66 static struct mtd_info *doc2klist = NULL;
67
68 /* Perform the required delay cycles by reading from the appropriate register */
69 static void DoC_Delay(struct DiskOnChip *doc, unsigned short cycles)
70 {
71 volatile char dummy;
72 int i;
73
74 for (i = 0; i < cycles; i++) {
75 if (DoC_is_Millennium(doc))
76 dummy = ReadDOC(doc->virtadr, NOP);
77 else
78 dummy = ReadDOC(doc->virtadr, DOCStatus);
79 }
80
81 }
82
83 /* DOC_WaitReady: Wait for RDY line to be asserted by the flash chip */
84 static int _DoC_WaitReady(struct DiskOnChip *doc)
85 {
86 unsigned long docptr = doc->virtadr;
87 unsigned short c = 0xffff;
88
89 DEBUG(MTD_DEBUG_LEVEL3,
90 "_DoC_WaitReady called for out-of-line wait\n");
91
92 /* Out-of-line routine to wait for chip response */
93 while (!(ReadDOC(docptr, CDSNControl) & CDSN_CTRL_FR_B) && --c)
94 ;
95
96 if (c == 0)
97 DEBUG(MTD_DEBUG_LEVEL2, "_DoC_WaitReady timed out.\n");
98
99 return (c == 0);
100 }
101
102 static inline int DoC_WaitReady(struct DiskOnChip *doc)
103 {
104 unsigned long docptr = doc->virtadr;
105 /* This is inline, to optimise the common case, where it's ready instantly */
106 int ret = 0;
107
108 /* 4 read form NOP register should be issued in prior to the read from CDSNControl
109 see Software Requirement 11.4 item 2. */
110 DoC_Delay(doc, 4);
111
112 if (!(ReadDOC(docptr, CDSNControl) & CDSN_CTRL_FR_B))
113 /* Call the out-of-line routine to wait */
114 ret = _DoC_WaitReady(doc);
115
116 /* issue 2 read from NOP register after reading from CDSNControl register
117 see Software Requirement 11.4 item 2. */
118 DoC_Delay(doc, 2);
119
120 return ret;
121 }
122
123 /* DoC_Command: Send a flash command to the flash chip through the CDSN Slow IO register to
124 bypass the internal pipeline. Each of 4 delay cycles (read from the NOP register) is
125 required after writing to CDSN Control register, see Software Requirement 11.4 item 3. */
126
127 static inline int DoC_Command(struct DiskOnChip *doc, unsigned char command,
128 unsigned char xtraflags)
129 {
130 unsigned long docptr = doc->virtadr;
131
132 if (DoC_is_2000(doc))
133 xtraflags |= CDSN_CTRL_FLASH_IO;
134
135 /* Assert the CLE (Command Latch Enable) line to the flash chip */
136 WriteDOC(xtraflags | CDSN_CTRL_CLE | CDSN_CTRL_CE, docptr, CDSNControl);
137 DoC_Delay(doc, 4); /* Software requirement 11.4.3 for Millennium */
138
139 if (DoC_is_Millennium(doc))
140 WriteDOC(command, docptr, CDSNSlowIO);
141
142 /* Send the command */
143 WriteDOC_(command, docptr, doc->ioreg);
144
145 /* Lower the CLE line */
146 WriteDOC(xtraflags | CDSN_CTRL_CE, docptr, CDSNControl);
147 DoC_Delay(doc, 4); /* Software requirement 11.4.3 for Millennium */
148
149 /* Wait for the chip to respond - Software requirement 11.4.1 (extended for any command) */
150 return DoC_WaitReady(doc);
151 }
152
153 /* DoC_Address: Set the current address for the flash chip through the CDSN Slow IO register to
154 bypass the internal pipeline. Each of 4 delay cycles (read from the NOP register) is
155 required after writing to CDSN Control register, see Software Requirement 11.4 item 3. */
156
157 static int DoC_Address(struct DiskOnChip *doc, int numbytes, unsigned long ofs,
158 unsigned char xtraflags1, unsigned char xtraflags2)
159 {
160 unsigned long docptr;
161 int i;
162
163 docptr = doc->virtadr;
164
165 if (DoC_is_2000(doc))
166 xtraflags1 |= CDSN_CTRL_FLASH_IO;
167
168 /* Assert the ALE (Address Latch Enable) line to the flash chip */
169 WriteDOC(xtraflags1 | CDSN_CTRL_ALE | CDSN_CTRL_CE, docptr, CDSNControl);
170
171 DoC_Delay(doc, 4); /* Software requirement 11.4.3 for Millennium */
172
173 /* Send the address */
174 /* Devices with 256-byte page are addressed as:
175 Column (bits 0-7), Page (bits 8-15, 16-23, 24-31)
176 * there is no device on the market with page256
177 and more than 24 bits.
178 Devices with 512-byte page are addressed as:
179 Column (bits 0-7), Page (bits 9-16, 17-24, 25-31)
180 * 25-31 is sent only if the chip support it.
181 * bit 8 changes the read command to be sent
182 (NAND_CMD_READ0 or NAND_CMD_READ1).
183 */
184
185 if (numbytes == ADDR_COLUMN || numbytes == ADDR_COLUMN_PAGE) {
186 if (DoC_is_Millennium(doc))
187 WriteDOC(ofs & 0xff, docptr, CDSNSlowIO);
188 WriteDOC_(ofs & 0xff, docptr, doc->ioreg);
189 }
190
191 if (doc->page256) {
192 ofs = ofs >> 8;
193 } else {
194 ofs = ofs >> 9;
195 }
196
197 if (numbytes == ADDR_PAGE || numbytes == ADDR_COLUMN_PAGE) {
198 for (i = 0; i < doc->pageadrlen; i++, ofs = ofs >> 8) {
199 if (DoC_is_Millennium(doc))
200 WriteDOC(ofs & 0xff, docptr, CDSNSlowIO);
201 WriteDOC_(ofs & 0xff, docptr, doc->ioreg);
202 }
203 }
204
205 DoC_Delay(doc, 2); /* Needed for some slow flash chips. mf. */
206
207 /* FIXME: The SlowIO's for millennium could be replaced by
208 a single WritePipeTerm here. mf. */
209
210 /* Lower the ALE line */
211 WriteDOC(xtraflags1 | xtraflags2 | CDSN_CTRL_CE, docptr,
212 CDSNControl);
213
214 DoC_Delay(doc, 4); /* Software requirement 11.4.3 for Millennium */
215
216 /* Wait for the chip to respond - Software requirement 11.4.1 */
217 return DoC_WaitReady(doc);
218 }
219
220 /* Read a buffer from DoC, taking care of Millennium odditys */
221 static void DoC_ReadBuf(struct DiskOnChip *doc, u_char * buf, int len)
222 {
223 int dummy;
224 int modulus = 0xffff;
225 unsigned long docptr;
226 int i;
227
228 docptr = doc->virtadr;
229
230 if (len <= 0)
231 return;
232
233 if (DoC_is_Millennium(doc)) {
234 /* Read the data via the internal pipeline through CDSN IO register,
235 see Pipelined Read Operations 11.3 */
236 dummy = ReadDOC(docptr, ReadPipeInit);
237
238 /* Millennium should use the LastDataRead register - Pipeline Reads */
239 len--;
240
241 /* This is needed for correctly ECC calculation */
242 modulus = 0xff;
243 }
244
245 for (i = 0; i < len; i++)
246 buf[i] = ReadDOC_(docptr, doc->ioreg + (i & modulus));
247
248 if (DoC_is_Millennium(doc)) {
249 buf[i] = ReadDOC(docptr, LastDataRead);
250 }
251 }
252
253 /* Write a buffer to DoC, taking care of Millennium odditys */
254 static void DoC_WriteBuf(struct DiskOnChip *doc, const u_char * buf, int len)
255 {
256 unsigned long docptr;
257 int i;
258
259 docptr = doc->virtadr;
260
261 if (len <= 0)
262 return;
263
264 for (i = 0; i < len; i++)
265 WriteDOC_(buf[i], docptr, doc->ioreg + i);
266
267 if (DoC_is_Millennium(doc)) {
268 WriteDOC(0x00, docptr, WritePipeTerm);
269 }
270 }
271
272
273 /* DoC_SelectChip: Select a given flash chip within the current floor */
274
275 static inline int DoC_SelectChip(struct DiskOnChip *doc, int chip)
276 {
277 unsigned long docptr = doc->virtadr;
278
279 /* Software requirement 11.4.4 before writing DeviceSelect */
280 /* Deassert the CE line to eliminate glitches on the FCE# outputs */
281 WriteDOC(CDSN_CTRL_WP, docptr, CDSNControl);
282 DoC_Delay(doc, 4); /* Software requirement 11.4.3 for Millennium */
283
284 /* Select the individual flash chip requested */
285 WriteDOC(chip, docptr, CDSNDeviceSelect);
286 DoC_Delay(doc, 4);
287
288 /* Reassert the CE line */
289 WriteDOC(CDSN_CTRL_CE | CDSN_CTRL_FLASH_IO | CDSN_CTRL_WP, docptr,
290 CDSNControl);
291 DoC_Delay(doc, 4); /* Software requirement 11.4.3 for Millennium */
292
293 /* Wait for it to be ready */
294 return DoC_WaitReady(doc);
295 }
296
297 /* DoC_SelectFloor: Select a given floor (bank of flash chips) */
298
299 static inline int DoC_SelectFloor(struct DiskOnChip *doc, int floor)
300 {
301 unsigned long docptr = doc->virtadr;
302
303 /* Select the floor (bank) of chips required */
304 WriteDOC(floor, docptr, FloorSelect);
305
306 /* Wait for the chip to be ready */
307 return DoC_WaitReady(doc);
308 }
309
310 /* DoC_IdentChip: Identify a given NAND chip given {floor,chip} */
311
312 static int DoC_IdentChip(struct DiskOnChip *doc, int floor, int chip)
313 {
314 int mfr, id, i;
315 volatile char dummy;
316
317 /* Page in the required floor/chip */
318 DoC_SelectFloor(doc, floor);
319 DoC_SelectChip(doc, chip);
320
321 /* Reset the chip */
322 if (DoC_Command(doc, NAND_CMD_RESET, CDSN_CTRL_WP)) {
323 DEBUG(MTD_DEBUG_LEVEL2,
324 "DoC_Command (reset) for %d,%d returned true\n",
325 floor, chip);
326 return 0;
327 }
328
329
330 /* Read the NAND chip ID: 1. Send ReadID command */
331 if (DoC_Command(doc, NAND_CMD_READID, CDSN_CTRL_WP)) {
332 DEBUG(MTD_DEBUG_LEVEL2,
333 "DoC_Command (ReadID) for %d,%d returned true\n",
334 floor, chip);
335 return 0;
336 }
337
338 /* Read the NAND chip ID: 2. Send address byte zero */
339 DoC_Address(doc, ADDR_COLUMN, 0, CDSN_CTRL_WP, 0);
340
341 /* Read the manufacturer and device id codes from the device */
342
343 /* CDSN Slow IO register see Software Requirement 11.4 item 5. */
344 dummy = ReadDOC(doc->virtadr, CDSNSlowIO);
345 DoC_Delay(doc, 2);
346 mfr = ReadDOC_(doc->virtadr, doc->ioreg);
347
348 /* CDSN Slow IO register see Software Requirement 11.4 item 5. */
349 dummy = ReadDOC(doc->virtadr, CDSNSlowIO);
350 DoC_Delay(doc, 2);
351 id = ReadDOC_(doc->virtadr, doc->ioreg);
352
353 /* No response - return failure */
354 if (mfr == 0xff || mfr == 0)
355 return 0;
356
357 /* Check it's the same as the first chip we identified.
358 * M-Systems say that any given DiskOnChip device should only
359 * contain _one_ type of flash part, although that's not a
360 * hardware restriction. */
361 if (doc->mfr) {
362 if (doc->mfr == mfr && doc->id == id)
363 return 1; /* This is another the same the first */
364 else
365 printk(KERN_WARNING
366 "Flash chip at floor %d, chip %d is different:\n",
367 floor, chip);
368 }
369
370 /* Print and store the manufacturer and ID codes. */
371 for (i = 0; nand_flash_ids[i].name != NULL; i++) {
372 if (mfr == nand_flash_ids[i].manufacture_id &&
373 id == nand_flash_ids[i].model_id) {
374 printk(KERN_INFO
375 "Flash chip found: Manufacturer ID: %2.2X, "
376 "Chip ID: %2.2X (%s)\n", mfr, id,
377 nand_flash_ids[i].name);
378 if (!doc->mfr) {
379 doc->mfr = mfr;
380 doc->id = id;
381 doc->chipshift =
382 nand_flash_ids[i].chipshift;
383 doc->page256 = nand_flash_ids[i].page256;
384 doc->pageadrlen =
385 nand_flash_ids[i].pageadrlen;
386 doc->erasesize =
387 nand_flash_ids[i].erasesize;
388 return 1;
389 }
390 return 0;
391 }
392 }
393
394
395 /* We haven't fully identified the chip. Print as much as we know. */
396 printk(KERN_WARNING "Unknown flash chip found: %2.2X %2.2X\n",
397 id, mfr);
398
399 printk(KERN_WARNING "Please report to dwmw2@infradead.org\n");
400 return 0;
401 }
402
403 /* DoC_ScanChips: Find all NAND chips present in a DiskOnChip, and identify them */
404
405 static void DoC_ScanChips(struct DiskOnChip *this)
406 {
407 int floor, chip;
408 int numchips[MAX_FLOORS];
409 int maxchips = MAX_CHIPS;
410 int ret = 1;
411
412 this->numchips = 0;
413 this->mfr = 0;
414 this->id = 0;
415
416 if (DoC_is_Millennium(this))
417 maxchips = MAX_CHIPS_MIL;
418
419 /* For each floor, find the number of valid chips it contains */
420 for (floor = 0; floor < MAX_FLOORS; floor++) {
421 ret = 1;
422 numchips[floor] = 0;
423 for (chip = 0; chip < maxchips && ret != 0; chip++) {
424
425 ret = DoC_IdentChip(this, floor, chip);
426 if (ret) {
427 numchips[floor]++;
428 this->numchips++;
429 }
430 }
431 }
432
433 /* If there are none at all that we recognise, bail */
434 if (!this->numchips) {
435 printk("No flash chips recognised.\n");
436 return;
437 }
438
439 /* Allocate an array to hold the information for each chip */
440 this->chips = kmalloc(sizeof(struct Nand) * this->numchips, GFP_KERNEL);
441 if (!this->chips) {
442 printk("No memory for allocating chip info structures\n");
443 return;
444 }
445
446 ret = 0;
447
448 /* Fill out the chip array with {floor, chipno} for each
449 * detected chip in the device. */
450 for (floor = 0; floor < MAX_FLOORS; floor++) {
451 for (chip = 0; chip < numchips[floor]; chip++) {
452 this->chips[ret].floor = floor;
453 this->chips[ret].chip = chip;
454 this->chips[ret].curadr = 0;
455 this->chips[ret].curmode = 0x50;
456 ret++;
457 }
458 }
459
460 /* Calculate and print the total size of the device */
461 this->totlen = this->numchips * (1 << this->chipshift);
462
463 printk(KERN_INFO "%d flash chips found. Total DiskOnChip size: %ld MiB\n",
464 this->numchips, this->totlen >> 20);
465 }
466
467 static int DoC2k_is_alias(struct DiskOnChip *doc1, struct DiskOnChip *doc2)
468 {
469 int tmp1, tmp2, retval;
470 if (doc1->physadr == doc2->physadr)
471 return 1;
472
473 /* Use the alias resolution register which was set aside for this
474 * purpose. If it's value is the same on both chips, they might
475 * be the same chip, and we write to one and check for a change in
476 * the other. It's unclear if this register is usuable in the
477 * DoC 2000 (it's in the Millennium docs), but it seems to work. */
478 tmp1 = ReadDOC(doc1->virtadr, AliasResolution);
479 tmp2 = ReadDOC(doc2->virtadr, AliasResolution);
480 if (tmp1 != tmp2)
481 return 0;
482
483 WriteDOC((tmp1 + 1) % 0xff, doc1->virtadr, AliasResolution);
484 tmp2 = ReadDOC(doc2->virtadr, AliasResolution);
485 if (tmp2 == (tmp1 + 1) % 0xff)
486 retval = 1;
487 else
488 retval = 0;
489
490 /* Restore register contents. May not be necessary, but do it just to
491 * be safe. */
492 WriteDOC(tmp1, doc1->virtadr, AliasResolution);
493
494 return retval;
495 }
496
497 static const char im_name[] = "DoC2k_init";
498
499 /* This routine is made available to other mtd code via
500 * inter_module_register. It must only be accessed through
501 * inter_module_get which will bump the use count of this module. The
502 * addresses passed back in mtd are valid as long as the use count of
503 * this module is non-zero, i.e. between inter_module_get and
504 * inter_module_put. Keith Owens <kaos@ocs.com.au> 29 Oct 2000.
505 */
506 static void DoC2k_init(struct mtd_info *mtd)
507 {
508 struct DiskOnChip *this = (struct DiskOnChip *) mtd->priv;
509 struct DiskOnChip *old = NULL;
510
511 /* We must avoid being called twice for the same device. */
512
513 if (doc2klist)
514 old = (struct DiskOnChip *) doc2klist->priv;
515
516 while (old) {
517 if (DoC2k_is_alias(old, this)) {
518 printk(KERN_NOTICE
519 "Ignoring DiskOnChip 2000 at 0x%lX - already configured\n",
520 this->physadr);
521 iounmap((void *) this->virtadr);
522 kfree(mtd);
523 return;
524 }
525 if (old->nextdoc)
526 old = (struct DiskOnChip *) old->nextdoc->priv;
527 else
528 old = NULL;
529 }
530
531
532 switch (this->ChipID) {
533 case DOC_ChipID_Doc2k:
534 mtd->name = "DiskOnChip 2000";
535 this->ioreg = DoC_2k_CDSN_IO;
536 break;
537 case DOC_ChipID_DocMil:
538 mtd->name = "DiskOnChip Millennium";
539 this->ioreg = DoC_Mil_CDSN_IO;
540 break;
541 }
542
543 printk(KERN_NOTICE "%s found at address 0x%lX\n", mtd->name,
544 this->physadr);
545
546 mtd->type = MTD_NANDFLASH;
547 mtd->flags = MTD_CAP_NANDFLASH;
548 mtd->size = 0;
549 mtd->erasesize = 0;
550 mtd->oobblock = 512;
551 mtd->oobsize = 16;
552 mtd->module = THIS_MODULE;
553 mtd->erase = doc_erase;
554 mtd->point = NULL;
555 mtd->unpoint = NULL;
556 mtd->read = doc_read;
557 mtd->write = doc_write;
558 mtd->read_ecc = doc_read_ecc;
559 mtd->write_ecc = doc_write_ecc;
560 mtd->read_oob = doc_read_oob;
561 mtd->write_oob = doc_write_oob;
562 mtd->sync = NULL;
563
564 this->totlen = 0;
565 this->numchips = 0;
566
567 this->curfloor = -1;
568 this->curchip = -1;
569
570 /* Ident all the chips present. */
571 DoC_ScanChips(this);
572
573 if (!this->totlen) {
574 kfree(mtd);
575 iounmap((void *) this->virtadr);
576 } else {
577 this->nextdoc = doc2klist;
578 doc2klist = mtd;
579 mtd->size = this->totlen;
580 mtd->erasesize = this->erasesize;
581 add_mtd_device(mtd);
582 return;
583 }
584 }
585
586 static int doc_read(struct mtd_info *mtd, loff_t from, size_t len,
587 size_t * retlen, u_char * buf)
588 {
589 /* Just a special case of doc_read_ecc */
590 return doc_read_ecc(mtd, from, len, retlen, buf, NULL);
591 }
592
593 static int doc_read_ecc(struct mtd_info *mtd, loff_t from, size_t len,
594 size_t * retlen, u_char * buf, u_char * eccbuf)
595 {
596 struct DiskOnChip *this = (struct DiskOnChip *) mtd->priv;
597 unsigned long docptr;
598 struct Nand *mychip;
599 unsigned char syndrome[6];
600 volatile char dummy;
601 int i, len256 = 0, ret=0;
602
603 docptr = this->virtadr;
604
605 /* Don't allow read past end of device */
606 if (from >= this->totlen)
607 return -EINVAL;
608
609 /* Don't allow a single read to cross a 512-byte block boundary */
610 if (from + len > ((from | 0x1ff) + 1))
611 len = ((from | 0x1ff) + 1) - from;
612
613 /* The ECC will not be calculated correctly if less than 512 is read */
614 if (len != 0x200 && eccbuf)
615 printk(KERN_WARNING
616 "ECC needs a full sector read (adr: %lx size %lx)\n",
617 (long) from, (long) len);
618
619 /* printk("DoC_Read (adr: %lx size %lx)\n", (long) from, (long) len); */
620
621
622 /* Find the chip which is to be used and select it */
623 mychip = &this->chips[from >> (this->chipshift)];
624
625 if (this->curfloor != mychip->floor) {
626 DoC_SelectFloor(this, mychip->floor);
627 DoC_SelectChip(this, mychip->chip);
628 } else if (this->curchip != mychip->chip) {
629 DoC_SelectChip(this, mychip->chip);
630 }
631
632 this->curfloor = mychip->floor;
633 this->curchip = mychip->chip;
634
635 DoC_Command(this,
636 (!this->page256
637 && (from & 0x100)) ? NAND_CMD_READ1 : NAND_CMD_READ0,
638 CDSN_CTRL_WP);
639 DoC_Address(this, ADDR_COLUMN_PAGE, from, CDSN_CTRL_WP,
640 CDSN_CTRL_ECC_IO);
641
642 if (eccbuf) {
643 /* Prime the ECC engine */
644 WriteDOC(DOC_ECC_RESET, docptr, ECCConf);
645 WriteDOC(DOC_ECC_EN, docptr, ECCConf);
646 } else {
647 /* disable the ECC engine */
648 WriteDOC(DOC_ECC_RESET, docptr, ECCConf);
649 WriteDOC(DOC_ECC_DIS, docptr, ECCConf);
650 }
651
652 /* treat crossing 256-byte sector for 2M x 8bits devices */
653 if (this->page256 && from + len > (from | 0xff) + 1) {
654 len256 = (from | 0xff) + 1 - from;
655 DoC_ReadBuf(this, buf, len256);
656
657 DoC_Command(this, NAND_CMD_READ0, CDSN_CTRL_WP);
658 DoC_Address(this, ADDR_COLUMN_PAGE, from + len256,
659 CDSN_CTRL_WP, CDSN_CTRL_ECC_IO);
660 }
661
662 DoC_ReadBuf(this, &buf[len256], len - len256);
663
664 /* Let the caller know we completed it */
665 *retlen = len;
666
667 if (eccbuf) {
668 /* Read the ECC data through the DiskOnChip ECC logic */
669 /* Note: this will work even with 2M x 8bit devices as */
670 /* they have 8 bytes of OOB per 256 page. mf. */
671 DoC_ReadBuf(this, eccbuf, 6);
672
673 /* Flush the pipeline */
674 if (DoC_is_Millennium(this)) {
675 dummy = ReadDOC(docptr, ECCConf);
676 dummy = ReadDOC(docptr, ECCConf);
677 i = ReadDOC(docptr, ECCConf);
678 } else {
679 dummy = ReadDOC(docptr, 2k_ECCStatus);
680 dummy = ReadDOC(docptr, 2k_ECCStatus);
681 i = ReadDOC(docptr, 2k_ECCStatus);
682 }
683
684 /* Check the ECC Status */
685 if (i & 0x80) {
686 int nb_errors;
687 /* There was an ECC error */
688 #ifdef ECC_DEBUG
689 printk("DiskOnChip ECC Error: Read at %lx\n", (long)from);
690 #endif
691 /* Read the ECC syndrom through the DiskOnChip ECC logic.
692 These syndrome will be all ZERO when there is no error */
693 for (i = 0; i < 6; i++) {
694 syndrome[i] =
695 ReadDOC(docptr, ECCSyndrome0 + i);
696 }
697 nb_errors = doc_decode_ecc(buf, syndrome);
698
699 #ifdef ECC_DEBUG
700 printk("Errors corrected: %x\n", nb_errors);
701 #endif
702 if (nb_errors < 0) {
703 /* We return error, but have actually done the read. Not that
704 this can be told to user-space, via sys_read(), but at least
705 MTD-aware stuff can know about it by checking *retlen */
706 ret = -EIO;
707 }
708 }
709
710 #ifdef PSYCHO_DEBUG
711 printk("ECC DATA at %lxB: %2.2X %2.2X %2.2X %2.2X %2.2X %2.2X\n",
712 (long)from, eccbuf[0], eccbuf[1], eccbuf[2],
713 eccbuf[3], eccbuf[4], eccbuf[5]);
714 #endif
715
716 /* disable the ECC engine */
717 WriteDOC(DOC_ECC_DIS, docptr , ECCConf);
718 }
719
720 /* according to 11.4.1, we need to wait for the busy line
721 * drop if we read to the end of the page. */
722 if(0 == ((from + *retlen) & 0x1ff))
723 {
724 DoC_WaitReady(this);
725 }
726
727 return ret;
728 }
729
730 static int doc_write(struct mtd_info *mtd, loff_t to, size_t len,
731 size_t * retlen, const u_char * buf)
732 {
733 char eccbuf[6];
734 return doc_write_ecc(mtd, to, len, retlen, buf, eccbuf);
735 }
736
737 static int doc_write_ecc(struct mtd_info *mtd, loff_t to, size_t len,
738 size_t * retlen, const u_char * buf,
739 u_char * eccbuf)
740 {
741 struct DiskOnChip *this = (struct DiskOnChip *) mtd->priv;
742 int di; /* Yes, DI is a hangover from when I was disassembling the binary driver */
743 unsigned long docptr;
744 volatile char dummy;
745 int len256 = 0;
746 struct Nand *mychip;
747
748 docptr = this->virtadr;
749
750 /* Don't allow write past end of device */
751 if (to >= this->totlen)
752 return -EINVAL;
753
754 /* Don't allow a single write to cross a 512-byte block boundary */
755 if (to + len > ((to | 0x1ff) + 1))
756 len = ((to | 0x1ff) + 1) - to;
757
758 /* The ECC will not be calculated correctly if less than 512 is written */
759 if (len != 0x200 && eccbuf)
760 printk(KERN_WARNING
761 "ECC needs a full sector write (adr: %lx size %lx)\n",
762 (long) to, (long) len);
763
764 /* printk("DoC_Write (adr: %lx size %lx)\n", (long) to, (long) len); */
765
766 /* Find the chip which is to be used and select it */
767 mychip = &this->chips[to >> (this->chipshift)];
768
769 if (this->curfloor != mychip->floor) {
770 DoC_SelectFloor(this, mychip->floor);
771 DoC_SelectChip(this, mychip->chip);
772 } else if (this->curchip != mychip->chip) {
773 DoC_SelectChip(this, mychip->chip);
774 }
775
776 this->curfloor = mychip->floor;
777 this->curchip = mychip->chip;
778
779 /* Set device to main plane of flash */
780 DoC_Command(this, NAND_CMD_RESET, CDSN_CTRL_WP);
781 DoC_Command(this,
782 (!this->page256
783 && (to & 0x100)) ? NAND_CMD_READ1 : NAND_CMD_READ0,
784 CDSN_CTRL_WP);
785
786 DoC_Command(this, NAND_CMD_SEQIN, 0);
787 DoC_Address(this, ADDR_COLUMN_PAGE, to, 0, CDSN_CTRL_ECC_IO);
788
789 if (eccbuf) {
790 /* Prime the ECC engine */
791 WriteDOC(DOC_ECC_RESET, docptr, ECCConf);
792 WriteDOC(DOC_ECC_EN | DOC_ECC_RW, docptr, ECCConf);
793 } else {
794 /* disable the ECC engine */
795 WriteDOC(DOC_ECC_RESET, docptr, ECCConf);
796 WriteDOC(DOC_ECC_DIS, docptr, ECCConf);
797 }
798
799 /* treat crossing 256-byte sector for 2M x 8bits devices */
800 if (this->page256 && to + len > (to | 0xff) + 1) {
801 len256 = (to | 0xff) + 1 - to;
802 DoC_WriteBuf(this, buf, len256);
803
804 DoC_Command(this, NAND_CMD_PAGEPROG, 0);
805
806 DoC_Command(this, NAND_CMD_STATUS, CDSN_CTRL_WP);
807 /* There's an implicit DoC_WaitReady() in DoC_Command */
808
809 dummy = ReadDOC(docptr, CDSNSlowIO);
810 DoC_Delay(this, 2);
811
812 if (ReadDOC_(docptr, this->ioreg) & 1) {
813 printk("Error programming flash\n");
814 /* Error in programming */
815 *retlen = 0;
816 return -EIO;
817 }
818
819 DoC_Command(this, NAND_CMD_SEQIN, 0);
820 DoC_Address(this, ADDR_COLUMN_PAGE, to + len256, 0,
821 CDSN_CTRL_ECC_IO);
822 }
823
824 DoC_WriteBuf(this, &buf[len256], len - len256);
825
826 if (eccbuf) {
827 WriteDOC(CDSN_CTRL_ECC_IO | CDSN_CTRL_CE, docptr,
828 CDSNControl);
829
830 if (DoC_is_Millennium(this)) {
831 WriteDOC(0, docptr, NOP);
832 WriteDOC(0, docptr, NOP);
833 WriteDOC(0, docptr, NOP);
834 } else {
835 WriteDOC_(0, docptr, this->ioreg);
836 WriteDOC_(0, docptr, this->ioreg);
837 WriteDOC_(0, docptr, this->ioreg);
838 }
839
840 /* Read the ECC data through the DiskOnChip ECC logic */
841 for (di = 0; di < 6; di++) {
842 eccbuf[di] = ReadDOC(docptr, ECCSyndrome0 + di);
843 }
844
845 /* Reset the ECC engine */
846 WriteDOC(DOC_ECC_DIS, docptr, ECCConf);
847
848 #ifdef PSYCHO_DEBUG
849 printk
850 ("OOB data at %lx is %2.2X %2.2X %2.2X %2.2X %2.2X %2.2X\n",
851 (long) to, eccbuf[0], eccbuf[1], eccbuf[2], eccbuf[3],
852 eccbuf[4], eccbuf[5]);
853 #endif
854 }
855
856 DoC_Command(this, NAND_CMD_PAGEPROG, 0);
857
858 DoC_Command(this, NAND_CMD_STATUS, CDSN_CTRL_WP);
859 /* There's an implicit DoC_WaitReady() in DoC_Command */
860
861 dummy = ReadDOC(docptr, CDSNSlowIO);
862 DoC_Delay(this, 2);
863
864 if (ReadDOC_(docptr, this->ioreg) & 1) {
865 printk("Error programming flash\n");
866 /* Error in programming */
867 *retlen = 0;
868 return -EIO;
869 }
870
871 /* Let the caller know we completed it */
872 *retlen = len;
873
874 if (eccbuf) {
875 unsigned char x[8];
876 size_t dummy;
877
878 /* Write the ECC data to flash */
879 for (di=0; di<6; di++)
880 x[di] = eccbuf[di];
881
882 x[6]=0x55;
883 x[7]=0x55;
884
885 return doc_write_oob(mtd, to, 8, &dummy, x);
886 }
887
888 return 0;
889 }
890
891 static int doc_read_oob(struct mtd_info *mtd, loff_t ofs, size_t len,
892 size_t * retlen, u_char * buf)
893 {
894 struct DiskOnChip *this = (struct DiskOnChip *) mtd->priv;
895 int len256 = 0;
896 unsigned long docptr;
897 struct Nand *mychip;
898
899 docptr = this->virtadr;
900
901 mychip = &this->chips[ofs >> this->chipshift];
902
903 if (this->curfloor != mychip->floor) {
904 DoC_SelectFloor(this, mychip->floor);
905 DoC_SelectChip(this, mychip->chip);
906 } else if (this->curchip != mychip->chip) {
907 DoC_SelectChip(this, mychip->chip);
908 }
909 this->curfloor = mychip->floor;
910 this->curchip = mychip->chip;
911
912 /* update address for 2M x 8bit devices. OOB starts on the second */
913 /* page to maintain compatibility with doc_read_ecc. */
914 if (this->page256) {
915 if (!(ofs & 0x8))
916 ofs += 0x100;
917 else
918 ofs -= 0x8;
919 }
920
921 DoC_Command(this, NAND_CMD_READOOB, CDSN_CTRL_WP);
922 DoC_Address(this, ADDR_COLUMN_PAGE, ofs, CDSN_CTRL_WP, 0);
923
924 /* treat crossing 8-byte OOB data for 2M x 8bit devices */
925 /* Note: datasheet says it should automaticaly wrap to the */
926 /* next OOB block, but it didn't work here. mf. */
927 if (this->page256 && ofs + len > (ofs | 0x7) + 1) {
928 len256 = (ofs | 0x7) + 1 - ofs;
929 DoC_ReadBuf(this, buf, len256);
930
931 DoC_Command(this, NAND_CMD_READOOB, CDSN_CTRL_WP);
932 DoC_Address(this, ADDR_COLUMN_PAGE, ofs & (~0x1ff),
933 CDSN_CTRL_WP, 0);
934 }
935
936 DoC_ReadBuf(this, &buf[len256], len - len256);
937
938 *retlen = len;
939 /* Reading the full OOB data drops us off of the end of the page,
940 * causing the flash device to go into busy mode, so we need
941 * to wait until ready 11.4.1 and Toshiba TC58256FT docs */
942 return DoC_WaitReady(this);
943
944 }
945
946 static int doc_write_oob(struct mtd_info *mtd, loff_t ofs, size_t len,
947 size_t * retlen, const u_char * buf)
948 {
949 struct DiskOnChip *this = (struct DiskOnChip *) mtd->priv;
950 int len256 = 0;
951 unsigned long docptr = this->virtadr;
952 struct Nand *mychip = &this->chips[ofs >> this->chipshift];
953 int dummy;
954
955 // printk("doc_write_oob(%lx, %d): %2.2X %2.2X %2.2X %2.2X ... %2.2X %2.2X .. %2.2X %2.2X\n",(long)ofs, len,
956 // buf[0], buf[1], buf[2], buf[3], buf[8], buf[9], buf[14],buf[15]);
957
958 /* Find the chip which is to be used and select it */
959 if (this->curfloor != mychip->floor) {
960 DoC_SelectFloor(this, mychip->floor);
961 DoC_SelectChip(this, mychip->chip);
962 } else if (this->curchip != mychip->chip) {
963 DoC_SelectChip(this, mychip->chip);
964 }
965 this->curfloor = mychip->floor;
966 this->curchip = mychip->chip;
967
968 /* disable the ECC engine */
969 WriteDOC (DOC_ECC_RESET, docptr, ECCConf);
970 WriteDOC (DOC_ECC_DIS, docptr, ECCConf);
971
972 /* Reset the chip, see Software Requirement 11.4 item 1. */
973 DoC_Command(this, NAND_CMD_RESET, CDSN_CTRL_WP);
974
975 /* issue the Read2 command to set the pointer to the Spare Data Area. */
976 DoC_Command(this, NAND_CMD_READOOB, CDSN_CTRL_WP);
977
978 /* update address for 2M x 8bit devices. OOB starts on the second */
979 /* page to maintain compatibility with doc_read_ecc. */
980 if (this->page256) {
981 if (!(ofs & 0x8))
982 ofs += 0x100;
983 else
984 ofs -= 0x8;
985 }
986
987 /* issue the Serial Data In command to initial the Page Program process */
988 DoC_Command(this, NAND_CMD_SEQIN, 0);
989 DoC_Address(this, ADDR_COLUMN_PAGE, ofs, 0, 0);
990
991 /* treat crossing 8-byte OOB data for 2M x 8bit devices */
992 /* Note: datasheet says it should automaticaly wrap to the */
993 /* next OOB block, but it didn't work here. mf. */
994 if (this->page256 && ofs + len > (ofs | 0x7) + 1) {
995 len256 = (ofs | 0x7) + 1 - ofs;
996 DoC_WriteBuf(this, buf, len256);
997
998 DoC_Command(this, NAND_CMD_PAGEPROG, 0);
999 DoC_Command(this, NAND_CMD_STATUS, 0);
1000 /* DoC_WaitReady() is implicit in DoC_Command */
1001
1002 dummy = ReadDOC(docptr, CDSNSlowIO);
1003 DoC_Delay(this, 2);
1004
1005 if (ReadDOC_(docptr, this->ioreg) & 1) {
1006 printk("Error programming oob data\n");
1007 /* There was an error */
1008 *retlen = 0;
1009 return -EIO;
1010 }
1011 DoC_Command(this, NAND_CMD_SEQIN, 0);
1012 DoC_Address(this, ADDR_COLUMN_PAGE, ofs & (~0x1ff), 0, 0);
1013 }
1014
1015 DoC_WriteBuf(this, &buf[len256], len - len256);
1016
1017 DoC_Command(this, NAND_CMD_PAGEPROG, 0);
1018 DoC_Command(this, NAND_CMD_STATUS, 0);
1019 /* DoC_WaitReady() is implicit in DoC_Command */
1020
1021 dummy = ReadDOC(docptr, CDSNSlowIO);
1022 DoC_Delay(this, 2);
1023
1024 if (ReadDOC_(docptr, this->ioreg) & 1) {
1025 printk("Error programming oob data\n");
1026 /* There was an error */
1027 *retlen = 0;
1028 return -EIO;
1029 }
1030
1031 *retlen = len;
1032 return 0;
1033
1034 }
1035
1036 int doc_erase(struct mtd_info *mtd, struct erase_info *instr)
1037 {
1038 struct DiskOnChip *this = (struct DiskOnChip *) mtd->priv;
1039 __u32 ofs = instr->addr;
1040 __u32 len = instr->len;
1041 unsigned long docptr;
1042 struct Nand *mychip;
1043
1044 if (len != mtd->erasesize)
1045 printk(KERN_WARNING "Erase not right size (%x != %x)n",
1046 len, mtd->erasesize);
1047
1048 docptr = this->virtadr;
1049
1050 mychip = &this->chips[ofs >> this->chipshift];
1051
1052 if (this->curfloor != mychip->floor) {
1053 DoC_SelectFloor(this, mychip->floor);
1054 DoC_SelectChip(this, mychip->chip);
1055 } else if (this->curchip != mychip->chip) {
1056 DoC_SelectChip(this, mychip->chip);
1057 }
1058 this->curfloor = mychip->floor;
1059 this->curchip = mychip->chip;
1060
1061 instr->state = MTD_ERASE_PENDING;
1062
1063 DoC_Command(this, NAND_CMD_ERASE1, 0);
1064 DoC_Address(this, ADDR_PAGE, ofs, 0, 0);
1065 DoC_Command(this, NAND_CMD_ERASE2, 0);
1066
1067 instr->state = MTD_ERASING;
1068
1069 DoC_Command(this, NAND_CMD_STATUS, CDSN_CTRL_WP);
1070
1071 if (ReadDOC_(docptr, this->ioreg) & 1) {
1072 printk("Error writing\n");
1073 /* There was an error */
1074 instr->state = MTD_ERASE_FAILED;
1075 } else
1076 instr->state = MTD_ERASE_DONE;
1077
1078 if (instr->callback)
1079 instr->callback(instr);
1080
1081 return 0;
1082 }
1083
1084
1085 /****************************************************************************
1086 *
1087 * Module stuff
1088 *
1089 ****************************************************************************/
1090
1091 #if LINUX_VERSION_CODE < 0x20212 && defined(MODULE)
1092 #define cleanup_doc2000 cleanup_module
1093 #define init_doc2000 init_module
1094 #endif
1095
1096 int __init init_doc2000(void)
1097 {
1098 inter_module_register(im_name, THIS_MODULE, &DoC2k_init);
1099 return 0;
1100 }
1101
1102 static void __exit cleanup_doc2000(void)
1103 {
1104 struct mtd_info *mtd;
1105 struct DiskOnChip *this;
1106
1107 while ((mtd = doc2klist)) {
1108 this = (struct DiskOnChip *) mtd->priv;
1109 doc2klist = this->nextdoc;
1110
1111 del_mtd_device(mtd);
1112
1113 iounmap((void *) this->virtadr);
1114 kfree(this->chips);
1115 kfree(mtd);
1116 }
1117 inter_module_unregister(im_name);
1118 }
1119
1120 module_exit(cleanup_doc2000);
1121 module_init(init_doc2000);
1122