blob: a4eb8b5b85ecb9b7db0c253622fc97720022f4f1 [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
Brian Norris289c0522011-07-19 10:06:09 -070085 pr_debug("_DoC_WaitReady called for out-of-line wait\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
87 /* Out-of-line routine to wait for chip response */
88 while (!(ReadDOC(docptr, CDSNControl) & CDSN_CTRL_FR_B)) {
89 /* issue 2 read from NOP register after reading from CDSNControl register
90 see Software Requirement 11.4 item 2. */
91 DoC_Delay(doc, 2);
92
93 if (time_after(jiffies, timeo)) {
Brian Norris289c0522011-07-19 10:06:09 -070094 pr_debug("_DoC_WaitReady timed out.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 return -EIO;
96 }
97 udelay(1);
98 cond_resched();
99 }
100
101 return 0;
102}
103
104static inline int DoC_WaitReady(struct DiskOnChip *doc)
105{
106 void __iomem *docptr = doc->virtadr;
107
108 /* This is inline, to optimise the common case, where it's ready instantly */
109 int ret = 0;
110
111 /* 4 read form NOP register should be issued in prior to the read from CDSNControl
112 see Software Requirement 11.4 item 2. */
113 DoC_Delay(doc, 4);
114
115 if (!(ReadDOC(docptr, CDSNControl) & CDSN_CTRL_FR_B))
116 /* Call the out-of-line routine to wait */
117 ret = _DoC_WaitReady(doc);
118
119 /* issue 2 read from NOP register after reading from CDSNControl register
120 see Software Requirement 11.4 item 2. */
121 DoC_Delay(doc, 2);
122
123 return ret;
124}
125
126/* DoC_Command: Send a flash command to the flash chip through the CDSN Slow IO register to
127 bypass the internal pipeline. Each of 4 delay cycles (read from the NOP register) is
128 required after writing to CDSN Control register, see Software Requirement 11.4 item 3. */
129
Arjan van de Ven858119e2006-01-14 13:20:43 -0800130static int DoC_Command(struct DiskOnChip *doc, unsigned char command,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 unsigned char xtraflags)
132{
133 void __iomem *docptr = doc->virtadr;
134
135 if (DoC_is_2000(doc))
136 xtraflags |= CDSN_CTRL_FLASH_IO;
137
138 /* Assert the CLE (Command Latch Enable) line to the flash chip */
139 WriteDOC(xtraflags | CDSN_CTRL_CLE | CDSN_CTRL_CE, docptr, CDSNControl);
140 DoC_Delay(doc, 4); /* Software requirement 11.4.3 for Millennium */
141
142 if (DoC_is_Millennium(doc))
143 WriteDOC(command, docptr, CDSNSlowIO);
144
145 /* Send the command */
146 WriteDOC_(command, docptr, doc->ioreg);
147 if (DoC_is_Millennium(doc))
148 WriteDOC(command, docptr, WritePipeTerm);
149
150 /* Lower the CLE line */
151 WriteDOC(xtraflags | CDSN_CTRL_CE, docptr, CDSNControl);
152 DoC_Delay(doc, 4); /* Software requirement 11.4.3 for Millennium */
153
154 /* Wait for the chip to respond - Software requirement 11.4.1 (extended for any command) */
155 return DoC_WaitReady(doc);
156}
157
158/* DoC_Address: Set the current address for the flash chip through the CDSN Slow IO register to
159 bypass the internal pipeline. Each of 4 delay cycles (read from the NOP register) is
160 required after writing to CDSN Control register, see Software Requirement 11.4 item 3. */
161
162static int DoC_Address(struct DiskOnChip *doc, int numbytes, unsigned long ofs,
163 unsigned char xtraflags1, unsigned char xtraflags2)
164{
165 int i;
166 void __iomem *docptr = doc->virtadr;
167
168 if (DoC_is_2000(doc))
169 xtraflags1 |= CDSN_CTRL_FLASH_IO;
170
171 /* Assert the ALE (Address Latch Enable) line to the flash chip */
172 WriteDOC(xtraflags1 | CDSN_CTRL_ALE | CDSN_CTRL_CE, docptr, CDSNControl);
173
174 DoC_Delay(doc, 4); /* Software requirement 11.4.3 for Millennium */
175
176 /* Send the address */
177 /* Devices with 256-byte page are addressed as:
178 Column (bits 0-7), Page (bits 8-15, 16-23, 24-31)
179 * there is no device on the market with page256
180 and more than 24 bits.
181 Devices with 512-byte page are addressed as:
182 Column (bits 0-7), Page (bits 9-16, 17-24, 25-31)
183 * 25-31 is sent only if the chip support it.
184 * bit 8 changes the read command to be sent
185 (NAND_CMD_READ0 or NAND_CMD_READ1).
186 */
187
188 if (numbytes == ADDR_COLUMN || numbytes == ADDR_COLUMN_PAGE) {
189 if (DoC_is_Millennium(doc))
190 WriteDOC(ofs & 0xff, docptr, CDSNSlowIO);
191 WriteDOC_(ofs & 0xff, docptr, doc->ioreg);
192 }
193
194 if (doc->page256) {
195 ofs = ofs >> 8;
196 } else {
197 ofs = ofs >> 9;
198 }
199
200 if (numbytes == ADDR_PAGE || numbytes == ADDR_COLUMN_PAGE) {
201 for (i = 0; i < doc->pageadrlen; i++, ofs = ofs >> 8) {
202 if (DoC_is_Millennium(doc))
203 WriteDOC(ofs & 0xff, docptr, CDSNSlowIO);
204 WriteDOC_(ofs & 0xff, docptr, doc->ioreg);
205 }
206 }
207
208 if (DoC_is_Millennium(doc))
209 WriteDOC(ofs & 0xff, docptr, WritePipeTerm);
210
211 DoC_Delay(doc, 2); /* Needed for some slow flash chips. mf. */
Thomas Gleixnere5580fb2005-11-07 11:15:40 +0000212
213 /* FIXME: The SlowIO's for millennium could be replaced by
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 a single WritePipeTerm here. mf. */
215
216 /* Lower the ALE line */
217 WriteDOC(xtraflags1 | xtraflags2 | CDSN_CTRL_CE, docptr,
218 CDSNControl);
219
220 DoC_Delay(doc, 4); /* Software requirement 11.4.3 for Millennium */
221
222 /* Wait for the chip to respond - Software requirement 11.4.1 */
223 return DoC_WaitReady(doc);
224}
225
226/* Read a buffer from DoC, taking care of Millennium odditys */
227static void DoC_ReadBuf(struct DiskOnChip *doc, u_char * buf, int len)
228{
229 volatile int dummy;
230 int modulus = 0xffff;
231 void __iomem *docptr = doc->virtadr;
232 int i;
233
234 if (len <= 0)
235 return;
236
237 if (DoC_is_Millennium(doc)) {
238 /* Read the data via the internal pipeline through CDSN IO register,
239 see Pipelined Read Operations 11.3 */
240 dummy = ReadDOC(docptr, ReadPipeInit);
241
242 /* Millennium should use the LastDataRead register - Pipeline Reads */
243 len--;
244
245 /* This is needed for correctly ECC calculation */
246 modulus = 0xff;
247 }
248
249 for (i = 0; i < len; i++)
250 buf[i] = ReadDOC_(docptr, doc->ioreg + (i & modulus));
251
252 if (DoC_is_Millennium(doc)) {
253 buf[i] = ReadDOC(docptr, LastDataRead);
254 }
255}
256
257/* Write a buffer to DoC, taking care of Millennium odditys */
258static void DoC_WriteBuf(struct DiskOnChip *doc, const u_char * buf, int len)
259{
260 void __iomem *docptr = doc->virtadr;
261 int i;
262
263 if (len <= 0)
264 return;
265
266 for (i = 0; i < len; i++)
267 WriteDOC_(buf[i], docptr, doc->ioreg + i);
268
269 if (DoC_is_Millennium(doc)) {
270 WriteDOC(0x00, docptr, WritePipeTerm);
271 }
272}
273
274
275/* DoC_SelectChip: Select a given flash chip within the current floor */
276
277static inline int DoC_SelectChip(struct DiskOnChip *doc, int chip)
278{
279 void __iomem *docptr = doc->virtadr;
280
281 /* Software requirement 11.4.4 before writing DeviceSelect */
282 /* Deassert the CE line to eliminate glitches on the FCE# outputs */
283 WriteDOC(CDSN_CTRL_WP, docptr, CDSNControl);
284 DoC_Delay(doc, 4); /* Software requirement 11.4.3 for Millennium */
285
286 /* Select the individual flash chip requested */
287 WriteDOC(chip, docptr, CDSNDeviceSelect);
288 DoC_Delay(doc, 4);
289
290 /* Reassert the CE line */
291 WriteDOC(CDSN_CTRL_CE | CDSN_CTRL_FLASH_IO | CDSN_CTRL_WP, docptr,
292 CDSNControl);
293 DoC_Delay(doc, 4); /* Software requirement 11.4.3 for Millennium */
294
295 /* Wait for it to be ready */
296 return DoC_WaitReady(doc);
297}
298
299/* DoC_SelectFloor: Select a given floor (bank of flash chips) */
300
301static inline int DoC_SelectFloor(struct DiskOnChip *doc, int floor)
302{
303 void __iomem *docptr = doc->virtadr;
304
305 /* Select the floor (bank) of chips required */
306 WriteDOC(floor, docptr, FloorSelect);
307
308 /* Wait for the chip to be ready */
309 return DoC_WaitReady(doc);
310}
311
312/* DoC_IdentChip: Identify a given NAND chip given {floor,chip} */
313
314static int DoC_IdentChip(struct DiskOnChip *doc, int floor, int chip)
315{
316 int mfr, id, i, j;
317 volatile char dummy;
318
319 /* Page in the required floor/chip */
320 DoC_SelectFloor(doc, floor);
321 DoC_SelectChip(doc, chip);
322
323 /* Reset the chip */
324 if (DoC_Command(doc, NAND_CMD_RESET, CDSN_CTRL_WP)) {
Brian Norris289c0522011-07-19 10:06:09 -0700325 pr_debug("DoC_Command (reset) for %d,%d returned true\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 floor, chip);
327 return 0;
328 }
329
330
331 /* Read the NAND chip ID: 1. Send ReadID command */
332 if (DoC_Command(doc, NAND_CMD_READID, CDSN_CTRL_WP)) {
Brian Norris289c0522011-07-19 10:06:09 -0700333 pr_debug("DoC_Command (ReadID) for %d,%d returned true\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 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 if (DoC_is_Millennium(doc)) {
344 DoC_Delay(doc, 2);
345 dummy = ReadDOC(doc->virtadr, ReadPipeInit);
346 mfr = ReadDOC(doc->virtadr, LastDataRead);
347
348 DoC_Delay(doc, 2);
349 dummy = ReadDOC(doc->virtadr, ReadPipeInit);
350 id = ReadDOC(doc->virtadr, LastDataRead);
351 } else {
352 /* CDSN Slow IO register see Software Req 11.4 item 5. */
353 dummy = ReadDOC(doc->virtadr, CDSNSlowIO);
354 DoC_Delay(doc, 2);
355 mfr = ReadDOC_(doc->virtadr, doc->ioreg);
356
357 /* CDSN Slow IO register see Software Req 11.4 item 5. */
358 dummy = ReadDOC(doc->virtadr, CDSNSlowIO);
359 DoC_Delay(doc, 2);
360 id = ReadDOC_(doc->virtadr, doc->ioreg);
361 }
362
363 /* No response - return failure */
364 if (mfr == 0xff || mfr == 0)
365 return 0;
366
Thomas Gleixnere5580fb2005-11-07 11:15:40 +0000367 /* Check it's the same as the first chip we identified.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 * M-Systems say that any given DiskOnChip device should only
Thomas Gleixnere5580fb2005-11-07 11:15:40 +0000369 * contain _one_ type of flash part, although that's not a
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 * hardware restriction. */
371 if (doc->mfr) {
372 if (doc->mfr == mfr && doc->id == id)
Uwe Kleine-Königa247b5d2008-02-03 14:59:11 +0200373 return 1; /* This is the same as the first */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 else
375 printk(KERN_WARNING
376 "Flash chip at floor %d, chip %d is different:\n",
377 floor, chip);
378 }
379
380 /* Print and store the manufacturer and ID codes. */
381 for (i = 0; nand_flash_ids[i].name != NULL; i++) {
382 if (id == nand_flash_ids[i].id) {
383 /* Try to identify manufacturer */
384 for (j = 0; nand_manuf_ids[j].id != 0x0; j++) {
385 if (nand_manuf_ids[j].id == mfr)
386 break;
Thomas Gleixnere5580fb2005-11-07 11:15:40 +0000387 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 printk(KERN_INFO
389 "Flash chip found: Manufacturer ID: %2.2X, "
390 "Chip ID: %2.2X (%s:%s)\n", mfr, id,
391 nand_manuf_ids[j].name, nand_flash_ids[i].name);
392 if (!doc->mfr) {
393 doc->mfr = mfr;
394 doc->id = id;
Thomas Gleixnere5580fb2005-11-07 11:15:40 +0000395 doc->chipshift =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 ffs((nand_flash_ids[i].chipsize << 20)) - 1;
397 doc->page256 = (nand_flash_ids[i].pagesize == 256) ? 1 : 0;
398 doc->pageadrlen = doc->chipshift > 25 ? 3 : 2;
399 doc->erasesize =
400 nand_flash_ids[i].erasesize;
401 return 1;
402 }
403 return 0;
404 }
405 }
406
407
408 /* We haven't fully identified the chip. Print as much as we know. */
409 printk(KERN_WARNING "Unknown flash chip found: %2.2X %2.2X\n",
410 id, mfr);
411
412 printk(KERN_WARNING "Please report to dwmw2@infradead.org\n");
413 return 0;
414}
415
416/* DoC_ScanChips: Find all NAND chips present in a DiskOnChip, and identify them */
417
418static void DoC_ScanChips(struct DiskOnChip *this, int maxchips)
419{
420 int floor, chip;
421 int numchips[MAX_FLOORS];
422 int ret = 1;
423
424 this->numchips = 0;
425 this->mfr = 0;
426 this->id = 0;
427
428 /* For each floor, find the number of valid chips it contains */
429 for (floor = 0; floor < MAX_FLOORS; floor++) {
430 ret = 1;
431 numchips[floor] = 0;
432 for (chip = 0; chip < maxchips && ret != 0; chip++) {
433
434 ret = DoC_IdentChip(this, floor, chip);
435 if (ret) {
436 numchips[floor]++;
437 this->numchips++;
438 }
439 }
440 }
441
442 /* If there are none at all that we recognise, bail */
443 if (!this->numchips) {
444 printk(KERN_NOTICE "No flash chips recognised.\n");
445 return;
446 }
447
448 /* Allocate an array to hold the information for each chip */
449 this->chips = kmalloc(sizeof(struct Nand) * this->numchips, GFP_KERNEL);
450 if (!this->chips) {
451 printk(KERN_NOTICE "No memory for allocating chip info structures\n");
452 return;
453 }
454
455 ret = 0;
456
Thomas Gleixnere5580fb2005-11-07 11:15:40 +0000457 /* Fill out the chip array with {floor, chipno} for each
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 * detected chip in the device. */
459 for (floor = 0; floor < MAX_FLOORS; floor++) {
460 for (chip = 0; chip < numchips[floor]; chip++) {
461 this->chips[ret].floor = floor;
462 this->chips[ret].chip = chip;
463 this->chips[ret].curadr = 0;
464 this->chips[ret].curmode = 0x50;
465 ret++;
466 }
467 }
468
469 /* Calculate and print the total size of the device */
470 this->totlen = this->numchips * (1 << this->chipshift);
471
472 printk(KERN_INFO "%d flash chips found. Total DiskOnChip size: %ld MiB\n",
473 this->numchips, this->totlen >> 20);
474}
475
476static int DoC2k_is_alias(struct DiskOnChip *doc1, struct DiskOnChip *doc2)
477{
478 int tmp1, tmp2, retval;
479 if (doc1->physadr == doc2->physadr)
480 return 1;
481
482 /* Use the alias resolution register which was set aside for this
483 * purpose. If it's value is the same on both chips, they might
484 * be the same chip, and we write to one and check for a change in
485 * the other. It's unclear if this register is usuable in the
486 * DoC 2000 (it's in the Millennium docs), but it seems to work. */
487 tmp1 = ReadDOC(doc1->virtadr, AliasResolution);
488 tmp2 = ReadDOC(doc2->virtadr, AliasResolution);
489 if (tmp1 != tmp2)
490 return 0;
491
492 WriteDOC((tmp1 + 1) % 0xff, doc1->virtadr, AliasResolution);
493 tmp2 = ReadDOC(doc2->virtadr, AliasResolution);
494 if (tmp2 == (tmp1 + 1) % 0xff)
495 retval = 1;
496 else
497 retval = 0;
498
499 /* Restore register contents. May not be necessary, but do it just to
500 * be safe. */
501 WriteDOC(tmp1, doc1->virtadr, AliasResolution);
502
503 return retval;
504}
505
David Woodhouse5e535422006-05-08 14:05:05 +0100506/* This routine is found from the docprobe code by symbol_get(),
507 * which will bump the use count of this module. */
508void DoC2k_init(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509{
510 struct DiskOnChip *this = mtd->priv;
511 struct DiskOnChip *old = NULL;
512 int maxchips;
513
514 /* We must avoid being called twice for the same device. */
515
516 if (doc2klist)
517 old = doc2klist->priv;
518
519 while (old) {
520 if (DoC2k_is_alias(old, this)) {
521 printk(KERN_NOTICE
522 "Ignoring DiskOnChip 2000 at 0x%lX - already configured\n",
523 this->physadr);
524 iounmap(this->virtadr);
525 kfree(mtd);
526 return;
527 }
528 if (old->nextdoc)
529 old = old->nextdoc->priv;
530 else
531 old = NULL;
532 }
533
534
535 switch (this->ChipID) {
536 case DOC_ChipID_Doc2kTSOP:
537 mtd->name = "DiskOnChip 2000 TSOP";
538 this->ioreg = DoC_Mil_CDSN_IO;
539 /* Pretend it's a Millennium */
540 this->ChipID = DOC_ChipID_DocMil;
541 maxchips = MAX_CHIPS;
542 break;
543 case DOC_ChipID_Doc2k:
544 mtd->name = "DiskOnChip 2000";
545 this->ioreg = DoC_2k_CDSN_IO;
546 maxchips = MAX_CHIPS;
547 break;
548 case DOC_ChipID_DocMil:
549 mtd->name = "DiskOnChip Millennium";
550 this->ioreg = DoC_Mil_CDSN_IO;
551 maxchips = MAX_CHIPS_MIL;
552 break;
553 default:
554 printk("Unknown ChipID 0x%02x\n", this->ChipID);
555 kfree(mtd);
556 iounmap(this->virtadr);
557 return;
558 }
559
560 printk(KERN_NOTICE "%s found at address 0x%lX\n", mtd->name,
561 this->physadr);
562
563 mtd->type = MTD_NANDFLASH;
564 mtd->flags = MTD_CAP_NANDFLASH;
Artem Bityutskiycd1986a2012-02-03 09:38:43 +0200565 mtd->writebufsize = mtd->writesize = 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 mtd->oobsize = 16;
Mike Dunn6a918ba2012-03-11 14:21:11 -0700567 mtd->ecc_strength = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 mtd->owner = THIS_MODULE;
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +0200569 mtd->_erase = doc_erase;
570 mtd->_read = doc_read;
571 mtd->_write = doc_write;
572 mtd->_read_oob = doc_read_oob;
573 mtd->_write_oob = doc_write_oob;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 this->curfloor = -1;
575 this->curchip = -1;
Ingo Molnar48b19262006-03-31 02:29:41 -0800576 mutex_init(&this->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577
578 /* Ident all the chips present. */
579 DoC_ScanChips(this, maxchips);
580
581 if (!this->totlen) {
582 kfree(mtd);
583 iounmap(this->virtadr);
584 } else {
585 this->nextdoc = doc2klist;
586 doc2klist = mtd;
587 mtd->size = this->totlen;
588 mtd->erasesize = this->erasesize;
Jamie Ilesee0e87b2011-05-23 10:23:40 +0100589 mtd_device_register(mtd, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 return;
591 }
592}
David Woodhouse5e535422006-05-08 14:05:05 +0100593EXPORT_SYMBOL_GPL(DoC2k_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594
595static int doc_read(struct mtd_info *mtd, loff_t from, size_t len,
596 size_t * retlen, u_char * buf)
597{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 struct DiskOnChip *this = mtd->priv;
599 void __iomem *docptr = this->virtadr;
600 struct Nand *mychip;
Thomas Gleixner7f8a8942006-06-28 10:11:33 +0200601 unsigned char syndrome[6], eccbuf[6];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 volatile char dummy;
603 int i, len256 = 0, ret=0;
604 size_t left = len;
605
Ingo Molnar48b19262006-03-31 02:29:41 -0800606 mutex_lock(&this->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 while (left) {
608 len = left;
609
610 /* Don't allow a single read to cross a 512-byte block boundary */
611 if (from + len > ((from | 0x1ff) + 1))
612 len = ((from | 0x1ff) + 1) - from;
613
614 /* The ECC will not be calculated correctly if less than 512 is read */
Samuel Tardieu53fb84a2008-02-04 23:44:47 -0800615 if (len != 0x200)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 printk(KERN_WARNING
617 "ECC needs a full sector read (adr: %lx size %lx)\n",
618 (long) from, (long) len);
619
620 /* printk("DoC_Read (adr: %lx size %lx)\n", (long) from, (long) len); */
621
622
623 /* Find the chip which is to be used and select it */
624 mychip = &this->chips[from >> (this->chipshift)];
625
626 if (this->curfloor != mychip->floor) {
627 DoC_SelectFloor(this, mychip->floor);
628 DoC_SelectChip(this, mychip->chip);
629 } else if (this->curchip != mychip->chip) {
630 DoC_SelectChip(this, mychip->chip);
631 }
632
633 this->curfloor = mychip->floor;
634 this->curchip = mychip->chip;
635
636 DoC_Command(this,
637 (!this->page256
638 && (from & 0x100)) ? NAND_CMD_READ1 : NAND_CMD_READ0,
639 CDSN_CTRL_WP);
640 DoC_Address(this, ADDR_COLUMN_PAGE, from, CDSN_CTRL_WP,
641 CDSN_CTRL_ECC_IO);
642
Thomas Gleixner7f8a8942006-06-28 10:11:33 +0200643 /* Prime the ECC engine */
644 WriteDOC(DOC_ECC_RESET, docptr, ECCConf);
645 WriteDOC(DOC_ECC_EN, docptr, ECCConf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646
647 /* treat crossing 256-byte sector for 2M x 8bits devices */
648 if (this->page256 && from + len > (from | 0xff) + 1) {
649 len256 = (from | 0xff) + 1 - from;
650 DoC_ReadBuf(this, buf, len256);
651
652 DoC_Command(this, NAND_CMD_READ0, CDSN_CTRL_WP);
653 DoC_Address(this, ADDR_COLUMN_PAGE, from + len256,
654 CDSN_CTRL_WP, CDSN_CTRL_ECC_IO);
655 }
656
657 DoC_ReadBuf(this, &buf[len256], len - len256);
658
659 /* Let the caller know we completed it */
660 *retlen += len;
661
Thomas Gleixner7f8a8942006-06-28 10:11:33 +0200662 /* Read the ECC data through the DiskOnChip ECC logic */
663 /* Note: this will work even with 2M x 8bit devices as */
664 /* they have 8 bytes of OOB per 256 page. mf. */
665 DoC_ReadBuf(this, eccbuf, 6);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666
Thomas Gleixner7f8a8942006-06-28 10:11:33 +0200667 /* Flush the pipeline */
668 if (DoC_is_Millennium(this)) {
669 dummy = ReadDOC(docptr, ECCConf);
670 dummy = ReadDOC(docptr, ECCConf);
671 i = ReadDOC(docptr, ECCConf);
672 } else {
673 dummy = ReadDOC(docptr, 2k_ECCStatus);
674 dummy = ReadDOC(docptr, 2k_ECCStatus);
675 i = ReadDOC(docptr, 2k_ECCStatus);
676 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677
Thomas Gleixner7f8a8942006-06-28 10:11:33 +0200678 /* Check the ECC Status */
679 if (i & 0x80) {
680 int nb_errors;
681 /* There was an ECC error */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682#ifdef ECC_DEBUG
Thomas Gleixner7f8a8942006-06-28 10:11:33 +0200683 printk(KERN_ERR "DiskOnChip ECC Error: Read at %lx\n", (long)from);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684#endif
Brian Norris7854d3f2011-06-23 14:12:08 -0700685 /* Read the ECC syndrome through the DiskOnChip ECC
Thomas Gleixner7f8a8942006-06-28 10:11:33 +0200686 logic. These syndrome will be all ZERO when there
687 is no error */
688 for (i = 0; i < 6; i++) {
689 syndrome[i] =
690 ReadDOC(docptr, ECCSyndrome0 + i);
691 }
692 nb_errors = doc_decode_ecc(buf, syndrome);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693
694#ifdef ECC_DEBUG
Thomas Gleixner7f8a8942006-06-28 10:11:33 +0200695 printk(KERN_ERR "Errors corrected: %x\n", nb_errors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696#endif
Thomas Gleixner7f8a8942006-06-28 10:11:33 +0200697 if (nb_errors < 0) {
698 /* We return error, but have actually done the
699 read. Not that this can be told to
700 user-space, via sys_read(), but at least
701 MTD-aware stuff can know about it by
702 checking *retlen */
703 ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 }
Thomas Gleixner7f8a8942006-06-28 10:11:33 +0200705 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706
707#ifdef PSYCHO_DEBUG
Thomas Gleixner7f8a8942006-06-28 10:11:33 +0200708 printk(KERN_DEBUG "ECC DATA at %lxB: %2.2X %2.2X %2.2X %2.2X %2.2X %2.2X\n",
709 (long)from, eccbuf[0], eccbuf[1], eccbuf[2],
710 eccbuf[3], eccbuf[4], eccbuf[5]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711#endif
Thomas Gleixnere5580fb2005-11-07 11:15:40 +0000712
Thomas Gleixner7f8a8942006-06-28 10:11:33 +0200713 /* disable the ECC engine */
714 WriteDOC(DOC_ECC_DIS, docptr , ECCConf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715
Thomas Gleixnere5580fb2005-11-07 11:15:40 +0000716 /* according to 11.4.1, we need to wait for the busy line
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 * drop if we read to the end of the page. */
718 if(0 == ((from + len) & 0x1ff))
719 {
720 DoC_WaitReady(this);
721 }
722
723 from += len;
724 left -= len;
725 buf += len;
726 }
727
Ingo Molnar48b19262006-03-31 02:29:41 -0800728 mutex_unlock(&this->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729
730 return ret;
731}
732
733static int doc_write(struct mtd_info *mtd, loff_t to, size_t len,
734 size_t * retlen, const u_char * buf)
735{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 struct DiskOnChip *this = mtd->priv;
737 int di; /* Yes, DI is a hangover from when I was disassembling the binary driver */
738 void __iomem *docptr = this->virtadr;
Thomas Gleixner7f8a8942006-06-28 10:11:33 +0200739 unsigned char eccbuf[6];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 volatile char dummy;
741 int len256 = 0;
742 struct Nand *mychip;
743 size_t left = len;
744 int status;
745
Ingo Molnar48b19262006-03-31 02:29:41 -0800746 mutex_lock(&this->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 while (left) {
748 len = left;
749
750 /* Don't allow a single write to cross a 512-byte block boundary */
751 if (to + len > ((to | 0x1ff) + 1))
752 len = ((to | 0x1ff) + 1) - to;
753
754 /* The ECC will not be calculated correctly if less than 512 is written */
755/* DBB-
756 if (len != 0x200 && eccbuf)
757 printk(KERN_WARNING
758 "ECC needs a full sector write (adr: %lx size %lx)\n",
759 (long) to, (long) len);
760 -DBB */
761
762 /* printk("DoC_Write (adr: %lx size %lx)\n", (long) to, (long) len); */
763
764 /* Find the chip which is to be used and select it */
765 mychip = &this->chips[to >> (this->chipshift)];
766
767 if (this->curfloor != mychip->floor) {
768 DoC_SelectFloor(this, mychip->floor);
769 DoC_SelectChip(this, mychip->chip);
770 } else if (this->curchip != mychip->chip) {
771 DoC_SelectChip(this, mychip->chip);
772 }
773
774 this->curfloor = mychip->floor;
775 this->curchip = mychip->chip;
776
777 /* Set device to main plane of flash */
778 DoC_Command(this, NAND_CMD_RESET, CDSN_CTRL_WP);
779 DoC_Command(this,
780 (!this->page256
781 && (to & 0x100)) ? NAND_CMD_READ1 : NAND_CMD_READ0,
782 CDSN_CTRL_WP);
783
784 DoC_Command(this, NAND_CMD_SEQIN, 0);
785 DoC_Address(this, ADDR_COLUMN_PAGE, to, 0, CDSN_CTRL_ECC_IO);
786
Thomas Gleixner7f8a8942006-06-28 10:11:33 +0200787 /* Prime the ECC engine */
788 WriteDOC(DOC_ECC_RESET, docptr, ECCConf);
789 WriteDOC(DOC_ECC_EN | DOC_ECC_RW, docptr, ECCConf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790
791 /* treat crossing 256-byte sector for 2M x 8bits devices */
792 if (this->page256 && to + len > (to | 0xff) + 1) {
793 len256 = (to | 0xff) + 1 - to;
794 DoC_WriteBuf(this, buf, len256);
795
796 DoC_Command(this, NAND_CMD_PAGEPROG, 0);
797
798 DoC_Command(this, NAND_CMD_STATUS, CDSN_CTRL_WP);
799 /* There's an implicit DoC_WaitReady() in DoC_Command */
800
801 dummy = ReadDOC(docptr, CDSNSlowIO);
802 DoC_Delay(this, 2);
803
804 if (ReadDOC_(docptr, this->ioreg) & 1) {
805 printk(KERN_ERR "Error programming flash\n");
806 /* Error in programming */
807 *retlen = 0;
Ingo Molnar48b19262006-03-31 02:29:41 -0800808 mutex_unlock(&this->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 return -EIO;
810 }
811
812 DoC_Command(this, NAND_CMD_SEQIN, 0);
813 DoC_Address(this, ADDR_COLUMN_PAGE, to + len256, 0,
814 CDSN_CTRL_ECC_IO);
815 }
816
817 DoC_WriteBuf(this, &buf[len256], len - len256);
818
Thomas Gleixner7f8a8942006-06-28 10:11:33 +0200819 WriteDOC(CDSN_CTRL_ECC_IO | CDSN_CTRL_CE, docptr, CDSNControl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820
Thomas Gleixner7f8a8942006-06-28 10:11:33 +0200821 if (DoC_is_Millennium(this)) {
822 WriteDOC(0, docptr, NOP);
823 WriteDOC(0, docptr, NOP);
824 WriteDOC(0, docptr, NOP);
825 } else {
826 WriteDOC_(0, docptr, this->ioreg);
827 WriteDOC_(0, docptr, this->ioreg);
828 WriteDOC_(0, docptr, this->ioreg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 }
830
Thomas Gleixner7f8a8942006-06-28 10:11:33 +0200831 WriteDOC(CDSN_CTRL_ECC_IO | CDSN_CTRL_FLASH_IO | CDSN_CTRL_CE, docptr,
832 CDSNControl);
833
834 /* Read the ECC data through the DiskOnChip ECC logic */
835 for (di = 0; di < 6; di++) {
836 eccbuf[di] = ReadDOC(docptr, ECCSyndrome0 + di);
837 }
838
839 /* Reset the ECC engine */
840 WriteDOC(DOC_ECC_DIS, docptr, ECCConf);
841
842#ifdef PSYCHO_DEBUG
843 printk
844 ("OOB data at %lx is %2.2X %2.2X %2.2X %2.2X %2.2X %2.2X\n",
845 (long) to, eccbuf[0], eccbuf[1], eccbuf[2], eccbuf[3],
846 eccbuf[4], eccbuf[5]);
847#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 DoC_Command(this, NAND_CMD_PAGEPROG, 0);
849
850 DoC_Command(this, NAND_CMD_STATUS, CDSN_CTRL_WP);
851 /* There's an implicit DoC_WaitReady() in DoC_Command */
852
853 if (DoC_is_Millennium(this)) {
854 ReadDOC(docptr, ReadPipeInit);
855 status = ReadDOC(docptr, LastDataRead);
856 } else {
857 dummy = ReadDOC(docptr, CDSNSlowIO);
858 DoC_Delay(this, 2);
859 status = ReadDOC_(docptr, this->ioreg);
860 }
861
862 if (status & 1) {
863 printk(KERN_ERR "Error programming flash\n");
864 /* Error in programming */
865 *retlen = 0;
Ingo Molnar48b19262006-03-31 02:29:41 -0800866 mutex_unlock(&this->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 return -EIO;
868 }
869
870 /* Let the caller know we completed it */
871 *retlen += len;
Thomas Gleixnere5580fb2005-11-07 11:15:40 +0000872
Samuel Tardieu53fb84a2008-02-04 23:44:47 -0800873 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 unsigned char x[8];
875 size_t dummy;
876 int ret;
877
878 /* Write the ECC data to flash */
879 for (di=0; di<6; di++)
880 x[di] = eccbuf[di];
Thomas Gleixnere5580fb2005-11-07 11:15:40 +0000881
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 x[6]=0x55;
883 x[7]=0x55;
Thomas Gleixnere5580fb2005-11-07 11:15:40 +0000884
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 ret = doc_write_oob_nolock(mtd, to, 8, &dummy, x);
886 if (ret) {
Ingo Molnar48b19262006-03-31 02:29:41 -0800887 mutex_unlock(&this->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 return ret;
889 }
890 }
891
892 to += len;
893 left -= len;
894 buf += len;
895 }
896
Ingo Molnar48b19262006-03-31 02:29:41 -0800897 mutex_unlock(&this->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 return 0;
899}
900
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200901static int doc_read_oob(struct mtd_info *mtd, loff_t ofs,
902 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903{
904 struct DiskOnChip *this = mtd->priv;
905 int len256 = 0, ret;
906 struct Nand *mychip;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200907 uint8_t *buf = ops->oobbuf;
908 size_t len = ops->len;
909
Brian Norris0612b9d2011-08-30 18:45:40 -0700910 BUG_ON(ops->mode != MTD_OPS_PLACE_OOB);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200911
912 ofs += ops->ooboffs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913
Ingo Molnar48b19262006-03-31 02:29:41 -0800914 mutex_lock(&this->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915
916 mychip = &this->chips[ofs >> this->chipshift];
917
918 if (this->curfloor != mychip->floor) {
919 DoC_SelectFloor(this, mychip->floor);
920 DoC_SelectChip(this, mychip->chip);
921 } else if (this->curchip != mychip->chip) {
922 DoC_SelectChip(this, mychip->chip);
923 }
924 this->curfloor = mychip->floor;
925 this->curchip = mychip->chip;
926
927 /* update address for 2M x 8bit devices. OOB starts on the second */
928 /* page to maintain compatibility with doc_read_ecc. */
929 if (this->page256) {
930 if (!(ofs & 0x8))
931 ofs += 0x100;
932 else
933 ofs -= 0x8;
934 }
935
936 DoC_Command(this, NAND_CMD_READOOB, CDSN_CTRL_WP);
937 DoC_Address(this, ADDR_COLUMN_PAGE, ofs, CDSN_CTRL_WP, 0);
938
939 /* treat crossing 8-byte OOB data for 2M x 8bit devices */
940 /* Note: datasheet says it should automaticaly wrap to the */
941 /* next OOB block, but it didn't work here. mf. */
942 if (this->page256 && ofs + len > (ofs | 0x7) + 1) {
943 len256 = (ofs | 0x7) + 1 - ofs;
944 DoC_ReadBuf(this, buf, len256);
945
946 DoC_Command(this, NAND_CMD_READOOB, CDSN_CTRL_WP);
947 DoC_Address(this, ADDR_COLUMN_PAGE, ofs & (~0x1ff),
948 CDSN_CTRL_WP, 0);
949 }
950
951 DoC_ReadBuf(this, &buf[len256], len - len256);
952
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200953 ops->retlen = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 /* Reading the full OOB data drops us off of the end of the page,
955 * causing the flash device to go into busy mode, so we need
956 * to wait until ready 11.4.1 and Toshiba TC58256FT docs */
Thomas Gleixnere5580fb2005-11-07 11:15:40 +0000957
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 ret = DoC_WaitReady(this);
959
Ingo Molnar48b19262006-03-31 02:29:41 -0800960 mutex_unlock(&this->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 return ret;
962
963}
964
965static int doc_write_oob_nolock(struct mtd_info *mtd, loff_t ofs, size_t len,
966 size_t * retlen, const u_char * buf)
967{
968 struct DiskOnChip *this = mtd->priv;
969 int len256 = 0;
970 void __iomem *docptr = this->virtadr;
971 struct Nand *mychip = &this->chips[ofs >> this->chipshift];
972 volatile int dummy;
973 int status;
974
975 // 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,
976 // buf[0], buf[1], buf[2], buf[3], buf[8], buf[9], buf[14],buf[15]);
977
978 /* Find the chip which is to be used and select it */
979 if (this->curfloor != mychip->floor) {
980 DoC_SelectFloor(this, mychip->floor);
981 DoC_SelectChip(this, mychip->chip);
982 } else if (this->curchip != mychip->chip) {
983 DoC_SelectChip(this, mychip->chip);
984 }
985 this->curfloor = mychip->floor;
986 this->curchip = mychip->chip;
987
988 /* disable the ECC engine */
989 WriteDOC (DOC_ECC_RESET, docptr, ECCConf);
990 WriteDOC (DOC_ECC_DIS, docptr, ECCConf);
991
992 /* Reset the chip, see Software Requirement 11.4 item 1. */
993 DoC_Command(this, NAND_CMD_RESET, CDSN_CTRL_WP);
994
995 /* issue the Read2 command to set the pointer to the Spare Data Area. */
996 DoC_Command(this, NAND_CMD_READOOB, CDSN_CTRL_WP);
997
998 /* update address for 2M x 8bit devices. OOB starts on the second */
999 /* page to maintain compatibility with doc_read_ecc. */
1000 if (this->page256) {
1001 if (!(ofs & 0x8))
1002 ofs += 0x100;
1003 else
1004 ofs -= 0x8;
1005 }
1006
1007 /* issue the Serial Data In command to initial the Page Program process */
1008 DoC_Command(this, NAND_CMD_SEQIN, 0);
1009 DoC_Address(this, ADDR_COLUMN_PAGE, ofs, 0, 0);
1010
1011 /* treat crossing 8-byte OOB data for 2M x 8bit devices */
1012 /* Note: datasheet says it should automaticaly wrap to the */
1013 /* next OOB block, but it didn't work here. mf. */
1014 if (this->page256 && ofs + len > (ofs | 0x7) + 1) {
1015 len256 = (ofs | 0x7) + 1 - ofs;
1016 DoC_WriteBuf(this, buf, len256);
1017
1018 DoC_Command(this, NAND_CMD_PAGEPROG, 0);
1019 DoC_Command(this, NAND_CMD_STATUS, 0);
1020 /* DoC_WaitReady() is implicit in DoC_Command */
1021
1022 if (DoC_is_Millennium(this)) {
1023 ReadDOC(docptr, ReadPipeInit);
1024 status = ReadDOC(docptr, LastDataRead);
1025 } else {
1026 dummy = ReadDOC(docptr, CDSNSlowIO);
1027 DoC_Delay(this, 2);
1028 status = ReadDOC_(docptr, this->ioreg);
1029 }
1030
1031 if (status & 1) {
1032 printk(KERN_ERR "Error programming oob data\n");
1033 /* There was an error */
1034 *retlen = 0;
1035 return -EIO;
1036 }
1037 DoC_Command(this, NAND_CMD_SEQIN, 0);
1038 DoC_Address(this, ADDR_COLUMN_PAGE, ofs & (~0x1ff), 0, 0);
1039 }
1040
1041 DoC_WriteBuf(this, &buf[len256], len - len256);
1042
1043 DoC_Command(this, NAND_CMD_PAGEPROG, 0);
1044 DoC_Command(this, NAND_CMD_STATUS, 0);
1045 /* DoC_WaitReady() is implicit in DoC_Command */
1046
1047 if (DoC_is_Millennium(this)) {
1048 ReadDOC(docptr, ReadPipeInit);
1049 status = ReadDOC(docptr, LastDataRead);
1050 } else {
1051 dummy = ReadDOC(docptr, CDSNSlowIO);
1052 DoC_Delay(this, 2);
1053 status = ReadDOC_(docptr, this->ioreg);
1054 }
1055
1056 if (status & 1) {
1057 printk(KERN_ERR "Error programming oob data\n");
1058 /* There was an error */
1059 *retlen = 0;
1060 return -EIO;
1061 }
1062
1063 *retlen = len;
1064 return 0;
1065
1066}
Thomas Gleixnere5580fb2005-11-07 11:15:40 +00001067
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001068static int doc_write_oob(struct mtd_info *mtd, loff_t ofs,
1069 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070{
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001071 struct DiskOnChip *this = mtd->priv;
1072 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073
Brian Norris0612b9d2011-08-30 18:45:40 -07001074 BUG_ON(ops->mode != MTD_OPS_PLACE_OOB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075
Thomas Gleixner8593fbc2006-05-29 03:26:58 +02001076 mutex_lock(&this->lock);
1077 ret = doc_write_oob_nolock(mtd, ofs + ops->ooboffs, ops->len,
1078 &ops->retlen, ops->oobbuf);
1079
1080 mutex_unlock(&this->lock);
1081 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082}
1083
1084static int doc_erase(struct mtd_info *mtd, struct erase_info *instr)
1085{
1086 struct DiskOnChip *this = mtd->priv;
1087 __u32 ofs = instr->addr;
1088 __u32 len = instr->len;
1089 volatile int dummy;
1090 void __iomem *docptr = this->virtadr;
1091 struct Nand *mychip;
1092 int status;
1093
Ingo Molnar48b19262006-03-31 02:29:41 -08001094 mutex_lock(&this->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095
1096 if (ofs & (mtd->erasesize-1) || len & (mtd->erasesize-1)) {
Ingo Molnar48b19262006-03-31 02:29:41 -08001097 mutex_unlock(&this->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 return -EINVAL;
1099 }
1100
1101 instr->state = MTD_ERASING;
Thomas Gleixnere5580fb2005-11-07 11:15:40 +00001102
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 /* FIXME: Do this in the background. Use timers or schedule_task() */
1104 while(len) {
1105 mychip = &this->chips[ofs >> this->chipshift];
1106
1107 if (this->curfloor != mychip->floor) {
1108 DoC_SelectFloor(this, mychip->floor);
1109 DoC_SelectChip(this, mychip->chip);
1110 } else if (this->curchip != mychip->chip) {
1111 DoC_SelectChip(this, mychip->chip);
1112 }
1113 this->curfloor = mychip->floor;
1114 this->curchip = mychip->chip;
1115
1116 DoC_Command(this, NAND_CMD_ERASE1, 0);
1117 DoC_Address(this, ADDR_PAGE, ofs, 0, 0);
1118 DoC_Command(this, NAND_CMD_ERASE2, 0);
1119
1120 DoC_Command(this, NAND_CMD_STATUS, CDSN_CTRL_WP);
1121
1122 if (DoC_is_Millennium(this)) {
1123 ReadDOC(docptr, ReadPipeInit);
1124 status = ReadDOC(docptr, LastDataRead);
1125 } else {
1126 dummy = ReadDOC(docptr, CDSNSlowIO);
1127 DoC_Delay(this, 2);
1128 status = ReadDOC_(docptr, this->ioreg);
1129 }
1130
1131 if (status & 1) {
1132 printk(KERN_ERR "Error erasing at 0x%x\n", ofs);
1133 /* There was an error */
1134 instr->state = MTD_ERASE_FAILED;
1135 goto callback;
1136 }
1137 ofs += mtd->erasesize;
1138 len -= mtd->erasesize;
1139 }
1140 instr->state = MTD_ERASE_DONE;
1141
1142 callback:
1143 mtd_erase_callback(instr);
1144
Ingo Molnar48b19262006-03-31 02:29:41 -08001145 mutex_unlock(&this->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 return 0;
1147}
1148
1149
1150/****************************************************************************
1151 *
1152 * Module stuff
1153 *
1154 ****************************************************************************/
1155
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156static void __exit cleanup_doc2000(void)
1157{
1158 struct mtd_info *mtd;
1159 struct DiskOnChip *this;
1160
1161 while ((mtd = doc2klist)) {
1162 this = mtd->priv;
1163 doc2klist = this->nextdoc;
1164
Jamie Ilesee0e87b2011-05-23 10:23:40 +01001165 mtd_device_unregister(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166
1167 iounmap(this->virtadr);
1168 kfree(this->chips);
1169 kfree(mtd);
1170 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171}
1172
1173module_exit(cleanup_doc2000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174
1175MODULE_LICENSE("GPL");
1176MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org> et al.");
1177MODULE_DESCRIPTION("MTD driver for DiskOnChip 2000 and Millennium");
1178