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