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