blob: 0e2a9326f717da9f18213b2032195e0529b7c132 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001
2/*
3 * Linux driver for Disk-On-Chip Millennium
4 * (c) 1999 Machine Vision Holdings, Inc.
5 * (c) 1999, 2000 David Woodhouse <dwmw2@infradead.org>
6 *
Thomas Gleixnere5580fb2005-11-07 11:15:40 +00007 * $Id: doc2001.c,v 1.49 2005/11/07 11:14:24 gleixner Exp $
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 */
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>
23
24#include <linux/mtd/mtd.h>
25#include <linux/mtd/nand.h>
26#include <linux/mtd/doc2000.h>
27
28/* #define ECC_DEBUG */
29
30/* I have no idea why some DoC chips can not use memcop_form|to_io().
31 * This may be due to the different revisions of the ASIC controller built-in or
32 * simplily a QA/Bug issue. Who knows ?? If you have trouble, please uncomment
33 * this:*/
34#undef USE_MEMCPY
35
36static int doc_read(struct mtd_info *mtd, loff_t from, size_t len,
37 size_t *retlen, u_char *buf);
38static int doc_write(struct mtd_info *mtd, loff_t to, size_t len,
39 size_t *retlen, const u_char *buf);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +020040static int doc_read_oob(struct mtd_info *mtd, loff_t ofs,
41 struct mtd_oob_ops *ops);
42static int doc_write_oob(struct mtd_info *mtd, loff_t ofs,
43 struct mtd_oob_ops *ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -070044static int doc_erase (struct mtd_info *mtd, struct erase_info *instr);
45
46static struct mtd_info *docmillist = NULL;
47
48/* Perform the required delay cycles by reading from the NOP register */
49static void DoC_Delay(void __iomem * docptr, unsigned short cycles)
50{
51 volatile char dummy;
52 int i;
53
54 for (i = 0; i < cycles; i++)
55 dummy = ReadDOC(docptr, NOP);
56}
57
58/* DOC_WaitReady: Wait for RDY line to be asserted by the flash chip */
59static int _DoC_WaitReady(void __iomem * docptr)
60{
61 unsigned short c = 0xffff;
62
63 DEBUG(MTD_DEBUG_LEVEL3,
64 "_DoC_WaitReady called for out-of-line wait\n");
65
66 /* Out-of-line routine to wait for chip response */
67 while (!(ReadDOC(docptr, CDSNControl) & CDSN_CTRL_FR_B) && --c)
68 ;
69
70 if (c == 0)
71 DEBUG(MTD_DEBUG_LEVEL2, "_DoC_WaitReady timed out.\n");
72
73 return (c == 0);
74}
75
76static inline int DoC_WaitReady(void __iomem * docptr)
77{
78 /* This is inline, to optimise the common case, where it's ready instantly */
79 int ret = 0;
80
81 /* 4 read form NOP register should be issued in prior to the read from CDSNControl
82 see Software Requirement 11.4 item 2. */
83 DoC_Delay(docptr, 4);
84
85 if (!(ReadDOC(docptr, CDSNControl) & CDSN_CTRL_FR_B))
86 /* Call the out-of-line routine to wait */
87 ret = _DoC_WaitReady(docptr);
88
89 /* issue 2 read from NOP register after reading from CDSNControl register
90 see Software Requirement 11.4 item 2. */
91 DoC_Delay(docptr, 2);
92
93 return ret;
94}
95
96/* DoC_Command: Send a flash command to the flash chip through the CDSN IO register
97 with the internal pipeline. Each of 4 delay cycles (read from the NOP register) is
98 required after writing to CDSN Control register, see Software Requirement 11.4 item 3. */
99
Arjan van de Ven858119e2006-01-14 13:20:43 -0800100static void DoC_Command(void __iomem * docptr, unsigned char command,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 unsigned char xtraflags)
102{
103 /* Assert the CLE (Command Latch Enable) line to the flash chip */
104 WriteDOC(xtraflags | CDSN_CTRL_CLE | CDSN_CTRL_CE, docptr, CDSNControl);
105 DoC_Delay(docptr, 4);
106
107 /* Send the command */
108 WriteDOC(command, docptr, Mil_CDSN_IO);
109 WriteDOC(0x00, docptr, WritePipeTerm);
110
111 /* Lower the CLE line */
112 WriteDOC(xtraflags | CDSN_CTRL_CE, docptr, CDSNControl);
113 DoC_Delay(docptr, 4);
114}
115
116/* DoC_Address: Set the current address for the flash chip through the CDSN IO register
117 with the internal pipeline. Each of 4 delay cycles (read from the NOP register) is
118 required after writing to CDSN Control register, see Software Requirement 11.4 item 3. */
119
120static inline void DoC_Address(void __iomem * docptr, int numbytes, unsigned long ofs,
121 unsigned char xtraflags1, unsigned char xtraflags2)
122{
123 /* Assert the ALE (Address Latch Enable) line to the flash chip */
124 WriteDOC(xtraflags1 | CDSN_CTRL_ALE | CDSN_CTRL_CE, docptr, CDSNControl);
125 DoC_Delay(docptr, 4);
126
127 /* Send the address */
128 switch (numbytes)
129 {
130 case 1:
131 /* Send single byte, bits 0-7. */
132 WriteDOC(ofs & 0xff, docptr, Mil_CDSN_IO);
133 WriteDOC(0x00, docptr, WritePipeTerm);
134 break;
135 case 2:
136 /* Send bits 9-16 followed by 17-23 */
137 WriteDOC((ofs >> 9) & 0xff, docptr, Mil_CDSN_IO);
138 WriteDOC((ofs >> 17) & 0xff, docptr, Mil_CDSN_IO);
139 WriteDOC(0x00, docptr, WritePipeTerm);
140 break;
141 case 3:
142 /* Send 0-7, 9-16, then 17-23 */
143 WriteDOC(ofs & 0xff, docptr, Mil_CDSN_IO);
144 WriteDOC((ofs >> 9) & 0xff, docptr, Mil_CDSN_IO);
145 WriteDOC((ofs >> 17) & 0xff, docptr, Mil_CDSN_IO);
146 WriteDOC(0x00, docptr, WritePipeTerm);
147 break;
148 default:
149 return;
150 }
151
152 /* Lower the ALE line */
153 WriteDOC(xtraflags1 | xtraflags2 | CDSN_CTRL_CE, docptr, CDSNControl);
154 DoC_Delay(docptr, 4);
155}
156
157/* DoC_SelectChip: Select a given flash chip within the current floor */
158static int DoC_SelectChip(void __iomem * docptr, int chip)
159{
160 /* Select the individual flash chip requested */
161 WriteDOC(chip, docptr, CDSNDeviceSelect);
162 DoC_Delay(docptr, 4);
163
164 /* Wait for it to be ready */
165 return DoC_WaitReady(docptr);
166}
167
168/* DoC_SelectFloor: Select a given floor (bank of flash chips) */
169static int DoC_SelectFloor(void __iomem * docptr, int floor)
170{
171 /* Select the floor (bank) of chips required */
172 WriteDOC(floor, docptr, FloorSelect);
173
174 /* Wait for the chip to be ready */
175 return DoC_WaitReady(docptr);
176}
177
178/* DoC_IdentChip: Identify a given NAND chip given {floor,chip} */
179static int DoC_IdentChip(struct DiskOnChip *doc, int floor, int chip)
180{
181 int mfr, id, i, j;
182 volatile char dummy;
183
184 /* Page in the required floor/chip
185 FIXME: is this supported by Millennium ?? */
186 DoC_SelectFloor(doc->virtadr, floor);
187 DoC_SelectChip(doc->virtadr, chip);
188
189 /* Reset the chip, see Software Requirement 11.4 item 1. */
190 DoC_Command(doc->virtadr, NAND_CMD_RESET, CDSN_CTRL_WP);
191 DoC_WaitReady(doc->virtadr);
192
Thomas Gleixnere5580fb2005-11-07 11:15:40 +0000193 /* Read the NAND chip ID: 1. Send ReadID command */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 DoC_Command(doc->virtadr, NAND_CMD_READID, CDSN_CTRL_WP);
195
Thomas Gleixnere5580fb2005-11-07 11:15:40 +0000196 /* Read the NAND chip ID: 2. Send address byte zero */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 DoC_Address(doc->virtadr, 1, 0x00, CDSN_CTRL_WP, 0x00);
198
199 /* Read the manufacturer and device id codes of the flash device through
200 CDSN IO register see Software Requirement 11.4 item 5.*/
201 dummy = ReadDOC(doc->virtadr, ReadPipeInit);
202 DoC_Delay(doc->virtadr, 2);
203 mfr = ReadDOC(doc->virtadr, Mil_CDSN_IO);
204
205 DoC_Delay(doc->virtadr, 2);
206 id = ReadDOC(doc->virtadr, Mil_CDSN_IO);
207 dummy = ReadDOC(doc->virtadr, LastDataRead);
208
209 /* No response - return failure */
210 if (mfr == 0xff || mfr == 0)
211 return 0;
212
213 /* FIXME: to deal with multi-flash on multi-Millennium case more carefully */
214 for (i = 0; nand_flash_ids[i].name != NULL; i++) {
215 if ( id == nand_flash_ids[i].id) {
216 /* Try to identify manufacturer */
217 for (j = 0; nand_manuf_ids[j].id != 0x0; j++) {
218 if (nand_manuf_ids[j].id == mfr)
219 break;
Thomas Gleixnere5580fb2005-11-07 11:15:40 +0000220 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 printk(KERN_INFO "Flash chip found: Manufacturer ID: %2.2X, "
222 "Chip ID: %2.2X (%s:%s)\n",
223 mfr, id, nand_manuf_ids[j].name, nand_flash_ids[i].name);
224 doc->mfr = mfr;
225 doc->id = id;
226 doc->chipshift = ffs((nand_flash_ids[i].chipsize << 20)) - 1;
227 break;
228 }
229 }
230
231 if (nand_flash_ids[i].name == NULL)
232 return 0;
233 else
234 return 1;
235}
236
237/* DoC_ScanChips: Find all NAND chips present in a DiskOnChip, and identify them */
238static void DoC_ScanChips(struct DiskOnChip *this)
239{
240 int floor, chip;
241 int numchips[MAX_FLOORS_MIL];
242 int ret;
243
244 this->numchips = 0;
245 this->mfr = 0;
246 this->id = 0;
247
248 /* For each floor, find the number of valid chips it contains */
249 for (floor = 0,ret = 1; floor < MAX_FLOORS_MIL; floor++) {
250 numchips[floor] = 0;
251 for (chip = 0; chip < MAX_CHIPS_MIL && ret != 0; chip++) {
252 ret = DoC_IdentChip(this, floor, chip);
253 if (ret) {
254 numchips[floor]++;
255 this->numchips++;
256 }
257 }
258 }
259 /* If there are none at all that we recognise, bail */
260 if (!this->numchips) {
261 printk("No flash chips recognised.\n");
262 return;
263 }
264
265 /* Allocate an array to hold the information for each chip */
266 this->chips = kmalloc(sizeof(struct Nand) * this->numchips, GFP_KERNEL);
267 if (!this->chips){
268 printk("No memory for allocating chip info structures\n");
269 return;
270 }
271
Thomas Gleixnere5580fb2005-11-07 11:15:40 +0000272 /* Fill out the chip array with {floor, chipno} for each
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 * detected chip in the device. */
274 for (floor = 0, ret = 0; floor < MAX_FLOORS_MIL; floor++) {
275 for (chip = 0 ; chip < numchips[floor] ; chip++) {
276 this->chips[ret].floor = floor;
277 this->chips[ret].chip = chip;
278 this->chips[ret].curadr = 0;
279 this->chips[ret].curmode = 0x50;
280 ret++;
281 }
282 }
283
284 /* Calculate and print the total size of the device */
285 this->totlen = this->numchips * (1 << this->chipshift);
286 printk(KERN_INFO "%d flash chips found. Total DiskOnChip size: %ld MiB\n",
287 this->numchips ,this->totlen >> 20);
288}
289
290static int DoCMil_is_alias(struct DiskOnChip *doc1, struct DiskOnChip *doc2)
291{
292 int tmp1, tmp2, retval;
293
294 if (doc1->physadr == doc2->physadr)
295 return 1;
296
297 /* Use the alias resolution register which was set aside for this
298 * purpose. If it's value is the same on both chips, they might
299 * be the same chip, and we write to one and check for a change in
300 * the other. It's unclear if this register is usuable in the
301 * DoC 2000 (it's in the Millenium docs), but it seems to work. */
302 tmp1 = ReadDOC(doc1->virtadr, AliasResolution);
303 tmp2 = ReadDOC(doc2->virtadr, AliasResolution);
304 if (tmp1 != tmp2)
305 return 0;
Thomas Gleixnere5580fb2005-11-07 11:15:40 +0000306
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 WriteDOC((tmp1+1) % 0xff, doc1->virtadr, AliasResolution);
308 tmp2 = ReadDOC(doc2->virtadr, AliasResolution);
309 if (tmp2 == (tmp1+1) % 0xff)
310 retval = 1;
311 else
312 retval = 0;
313
314 /* Restore register contents. May not be necessary, but do it just to
315 * be safe. */
316 WriteDOC(tmp1, doc1->virtadr, AliasResolution);
317
318 return retval;
319}
320
David Woodhouse5e535422006-05-08 14:05:05 +0100321/* This routine is found from the docprobe code by symbol_get(),
322 * which will bump the use count of this module. */
323void DoCMil_init(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324{
325 struct DiskOnChip *this = mtd->priv;
326 struct DiskOnChip *old = NULL;
327
328 /* We must avoid being called twice for the same device. */
329 if (docmillist)
330 old = docmillist->priv;
331
332 while (old) {
333 if (DoCMil_is_alias(this, old)) {
334 printk(KERN_NOTICE "Ignoring DiskOnChip Millennium at "
335 "0x%lX - already configured\n", this->physadr);
336 iounmap(this->virtadr);
337 kfree(mtd);
338 return;
339 }
340 if (old->nextdoc)
341 old = old->nextdoc->priv;
342 else
343 old = NULL;
344 }
345
346 mtd->name = "DiskOnChip Millennium";
347 printk(KERN_NOTICE "DiskOnChip Millennium found at address 0x%lX\n",
348 this->physadr);
349
350 mtd->type = MTD_NANDFLASH;
351 mtd->flags = MTD_CAP_NANDFLASH;
352 mtd->ecctype = MTD_ECC_RS_DiskOnChip;
353 mtd->size = 0;
354
355 /* FIXME: erase size is not always 8KiB */
356 mtd->erasesize = 0x2000;
357
Joern Engel28318772006-05-22 23:18:05 +0200358 mtd->writesize = 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 mtd->oobsize = 16;
360 mtd->owner = THIS_MODULE;
361 mtd->erase = doc_erase;
362 mtd->point = NULL;
363 mtd->unpoint = NULL;
364 mtd->read = doc_read;
365 mtd->write = doc_write;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 mtd->read_oob = doc_read_oob;
367 mtd->write_oob = doc_write_oob;
368 mtd->sync = NULL;
369
370 this->totlen = 0;
371 this->numchips = 0;
372 this->curfloor = -1;
373 this->curchip = -1;
374
375 /* Ident all the chips present. */
376 DoC_ScanChips(this);
377
378 if (!this->totlen) {
379 kfree(mtd);
380 iounmap(this->virtadr);
381 } else {
382 this->nextdoc = docmillist;
383 docmillist = mtd;
384 mtd->size = this->totlen;
385 add_mtd_device(mtd);
386 return;
387 }
388}
David Woodhousef0ad11d2006-05-12 11:40:13 +0100389EXPORT_SYMBOL_GPL(DoCMil_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390
391static int doc_read (struct mtd_info *mtd, loff_t from, size_t len,
392 size_t *retlen, u_char *buf)
393{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 int i, ret;
395 volatile char dummy;
Thomas Gleixner7f8a8942006-06-28 10:11:33 +0200396 unsigned char syndrome[6], eccbuf[6];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 struct DiskOnChip *this = mtd->priv;
398 void __iomem *docptr = this->virtadr;
399 struct Nand *mychip = &this->chips[from >> (this->chipshift)];
400
401 /* Don't allow read past end of device */
402 if (from >= this->totlen)
403 return -EINVAL;
404
405 /* Don't allow a single read to cross a 512-byte block boundary */
Thomas Gleixnere5580fb2005-11-07 11:15:40 +0000406 if (from + len > ((from | 0x1ff) + 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 len = ((from | 0x1ff) + 1) - from;
408
409 /* Find the chip which is to be used and select it */
410 if (this->curfloor != mychip->floor) {
411 DoC_SelectFloor(docptr, mychip->floor);
412 DoC_SelectChip(docptr, mychip->chip);
413 } else if (this->curchip != mychip->chip) {
414 DoC_SelectChip(docptr, mychip->chip);
415 }
416 this->curfloor = mychip->floor;
417 this->curchip = mychip->chip;
418
419 /* issue the Read0 or Read1 command depend on which half of the page
420 we are accessing. Polling the Flash Ready bit after issue 3 bytes
421 address in Sequence Read Mode, see Software Requirement 11.4 item 1.*/
422 DoC_Command(docptr, (from >> 8) & 1, CDSN_CTRL_WP);
423 DoC_Address(docptr, 3, from, CDSN_CTRL_WP, 0x00);
424 DoC_WaitReady(docptr);
425
Thomas Gleixner7f8a8942006-06-28 10:11:33 +0200426 /* init the ECC engine, see Reed-Solomon EDC/ECC 11.1 .*/
427 WriteDOC (DOC_ECC_RESET, docptr, ECCConf);
428 WriteDOC (DOC_ECC_EN, docptr, ECCConf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429
430 /* Read the data via the internal pipeline through CDSN IO register,
431 see Pipelined Read Operations 11.3 */
432 dummy = ReadDOC(docptr, ReadPipeInit);
433#ifndef USE_MEMCPY
434 for (i = 0; i < len-1; i++) {
435 /* N.B. you have to increase the source address in this way or the
436 ECC logic will not work properly */
437 buf[i] = ReadDOC(docptr, Mil_CDSN_IO + (i & 0xff));
438 }
439#else
440 memcpy_fromio(buf, docptr + DoC_Mil_CDSN_IO, len - 1);
441#endif
442 buf[len - 1] = ReadDOC(docptr, LastDataRead);
443
444 /* Let the caller know we completed it */
445 *retlen = len;
446 ret = 0;
447
Thomas Gleixner7f8a8942006-06-28 10:11:33 +0200448 /* Read the ECC data from Spare Data Area,
449 see Reed-Solomon EDC/ECC 11.1 */
450 dummy = ReadDOC(docptr, ReadPipeInit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451#ifndef USE_MEMCPY
Thomas Gleixner7f8a8942006-06-28 10:11:33 +0200452 for (i = 0; i < 5; i++) {
453 /* N.B. you have to increase the source address in this way or the
454 ECC logic will not work properly */
455 eccbuf[i] = ReadDOC(docptr, Mil_CDSN_IO + i);
456 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457#else
Thomas Gleixner7f8a8942006-06-28 10:11:33 +0200458 memcpy_fromio(eccbuf, docptr + DoC_Mil_CDSN_IO, 5);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459#endif
Thomas Gleixner7f8a8942006-06-28 10:11:33 +0200460 eccbuf[5] = ReadDOC(docptr, LastDataRead);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461
Thomas Gleixner7f8a8942006-06-28 10:11:33 +0200462 /* Flush the pipeline */
463 dummy = ReadDOC(docptr, ECCConf);
464 dummy = ReadDOC(docptr, ECCConf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465
Thomas Gleixner7f8a8942006-06-28 10:11:33 +0200466 /* Check the ECC Status */
467 if (ReadDOC(docptr, ECCConf) & 0x80) {
468 int nb_errors;
469 /* There was an ECC error */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470#ifdef ECC_DEBUG
Thomas Gleixner7f8a8942006-06-28 10:11:33 +0200471 printk("DiskOnChip ECC Error: Read at %lx\n", (long)from);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472#endif
Thomas Gleixner7f8a8942006-06-28 10:11:33 +0200473 /* Read the ECC syndrom through the DiskOnChip ECC logic.
474 These syndrome will be all ZERO when there is no error */
475 for (i = 0; i < 6; i++) {
476 syndrome[i] = ReadDOC(docptr, ECCSyndrome0 + i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 }
Thomas Gleixner7f8a8942006-06-28 10:11:33 +0200478 nb_errors = doc_decode_ecc(buf, syndrome);
479#ifdef ECC_DEBUG
480 printk("ECC Errors corrected: %x\n", nb_errors);
481#endif
482 if (nb_errors < 0) {
483 /* We return error, but have actually done the read. Not that
484 this can be told to user-space, via sys_read(), but at least
485 MTD-aware stuff can know about it by checking *retlen */
486 ret = -EIO;
487 }
488 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
490#ifdef PSYCHO_DEBUG
Thomas Gleixner7f8a8942006-06-28 10:11:33 +0200491 printk("ECC DATA at %lx: %2.2X %2.2X %2.2X %2.2X %2.2X %2.2X\n",
492 (long)from, eccbuf[0], eccbuf[1], eccbuf[2], eccbuf[3],
493 eccbuf[4], eccbuf[5]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494#endif
495
Thomas Gleixner7f8a8942006-06-28 10:11:33 +0200496 /* disable the ECC engine */
497 WriteDOC(DOC_ECC_DIS, docptr , ECCConf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498
499 return ret;
500}
501
502static int doc_write (struct mtd_info *mtd, loff_t to, size_t len,
503 size_t *retlen, const u_char *buf)
504{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 int i,ret = 0;
Thomas Gleixner7f8a8942006-06-28 10:11:33 +0200506 char eccbuf[6];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 volatile char dummy;
508 struct DiskOnChip *this = mtd->priv;
509 void __iomem *docptr = this->virtadr;
510 struct Nand *mychip = &this->chips[to >> (this->chipshift)];
511
512 /* Don't allow write past end of device */
513 if (to >= this->totlen)
514 return -EINVAL;
515
516#if 0
517 /* Don't allow a single write to cross a 512-byte block boundary */
Thomas Gleixnere5580fb2005-11-07 11:15:40 +0000518 if (to + len > ( (to | 0x1ff) + 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 len = ((to | 0x1ff) + 1) - to;
520#else
521 /* Don't allow writes which aren't exactly one block */
522 if (to & 0x1ff || len != 0x200)
523 return -EINVAL;
524#endif
525
526 /* Find the chip which is to be used and select it */
527 if (this->curfloor != mychip->floor) {
528 DoC_SelectFloor(docptr, mychip->floor);
529 DoC_SelectChip(docptr, mychip->chip);
530 } else if (this->curchip != mychip->chip) {
531 DoC_SelectChip(docptr, mychip->chip);
532 }
533 this->curfloor = mychip->floor;
534 this->curchip = mychip->chip;
535
536 /* Reset the chip, see Software Requirement 11.4 item 1. */
537 DoC_Command(docptr, NAND_CMD_RESET, 0x00);
538 DoC_WaitReady(docptr);
539 /* Set device to main plane of flash */
540 DoC_Command(docptr, NAND_CMD_READ0, 0x00);
541
542 /* issue the Serial Data In command to initial the Page Program process */
543 DoC_Command(docptr, NAND_CMD_SEQIN, 0x00);
544 DoC_Address(docptr, 3, to, 0x00, 0x00);
545 DoC_WaitReady(docptr);
546
Thomas Gleixner7f8a8942006-06-28 10:11:33 +0200547 /* init the ECC engine, see Reed-Solomon EDC/ECC 11.1 .*/
548 WriteDOC (DOC_ECC_RESET, docptr, ECCConf);
549 WriteDOC (DOC_ECC_EN | DOC_ECC_RW, docptr, ECCConf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
551 /* Write the data via the internal pipeline through CDSN IO register,
552 see Pipelined Write Operations 11.2 */
553#ifndef USE_MEMCPY
554 for (i = 0; i < len; i++) {
555 /* N.B. you have to increase the source address in this way or the
556 ECC logic will not work properly */
557 WriteDOC(buf[i], docptr, Mil_CDSN_IO + i);
558 }
559#else
560 memcpy_toio(docptr + DoC_Mil_CDSN_IO, buf, len);
561#endif
562 WriteDOC(0x00, docptr, WritePipeTerm);
563
Thomas Gleixner7f8a8942006-06-28 10:11:33 +0200564 /* Write ECC data to flash, the ECC info is generated by the DiskOnChip ECC logic
565 see Reed-Solomon EDC/ECC 11.1 */
566 WriteDOC(0, docptr, NOP);
567 WriteDOC(0, docptr, NOP);
568 WriteDOC(0, docptr, NOP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
Thomas Gleixner7f8a8942006-06-28 10:11:33 +0200570 /* Read the ECC data through the DiskOnChip ECC logic */
571 for (i = 0; i < 6; i++) {
572 eccbuf[i] = ReadDOC(docptr, ECCSyndrome0 + i);
573 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574
Thomas Gleixner7f8a8942006-06-28 10:11:33 +0200575 /* ignore the ECC engine */
576 WriteDOC(DOC_ECC_DIS, docptr , ECCConf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577
578#ifndef USE_MEMCPY
Thomas Gleixner7f8a8942006-06-28 10:11:33 +0200579 /* Write the ECC data to flash */
580 for (i = 0; i < 6; i++) {
581 /* N.B. you have to increase the source address in this way or the
582 ECC logic will not work properly */
583 WriteDOC(eccbuf[i], docptr, Mil_CDSN_IO + i);
584 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585#else
Thomas Gleixner7f8a8942006-06-28 10:11:33 +0200586 memcpy_toio(docptr + DoC_Mil_CDSN_IO, eccbuf, 6);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587#endif
588
Thomas Gleixner7f8a8942006-06-28 10:11:33 +0200589 /* write the block status BLOCK_USED (0x5555) at the end of ECC data
590 FIXME: this is only a hack for programming the IPL area for LinuxBIOS
591 and should be replace with proper codes in user space utilities */
592 WriteDOC(0x55, docptr, Mil_CDSN_IO);
593 WriteDOC(0x55, docptr, Mil_CDSN_IO + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594
Thomas Gleixner7f8a8942006-06-28 10:11:33 +0200595 WriteDOC(0x00, docptr, WritePipeTerm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596
597#ifdef PSYCHO_DEBUG
Thomas Gleixner7f8a8942006-06-28 10:11:33 +0200598 printk("OOB data at %lx is %2.2X %2.2X %2.2X %2.2X %2.2X %2.2X\n",
599 (long) to, eccbuf[0], eccbuf[1], eccbuf[2], eccbuf[3],
600 eccbuf[4], eccbuf[5]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602
603 /* Commit the Page Program command and wait for ready
604 see Software Requirement 11.4 item 1.*/
605 DoC_Command(docptr, NAND_CMD_PAGEPROG, 0x00);
606 DoC_WaitReady(docptr);
607
608 /* Read the status of the flash device through CDSN IO register
609 see Software Requirement 11.4 item 5.*/
610 DoC_Command(docptr, NAND_CMD_STATUS, CDSN_CTRL_WP);
611 dummy = ReadDOC(docptr, ReadPipeInit);
612 DoC_Delay(docptr, 2);
613 if (ReadDOC(docptr, Mil_CDSN_IO) & 1) {
614 printk("Error programming flash\n");
615 /* Error in programming
616 FIXME: implement Bad Block Replacement (in nftl.c ??) */
617 *retlen = 0;
618 ret = -EIO;
619 }
620 dummy = ReadDOC(docptr, LastDataRead);
621
622 /* Let the caller know we completed it */
623 *retlen = len;
624
625 return ret;
626}
627
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200628static int doc_read_oob(struct mtd_info *mtd, loff_t ofs,
629 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630{
631#ifndef USE_MEMCPY
632 int i;
633#endif
634 volatile char dummy;
635 struct DiskOnChip *this = mtd->priv;
636 void __iomem *docptr = this->virtadr;
637 struct Nand *mychip = &this->chips[ofs >> this->chipshift];
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200638 uint8_t *buf = ops->oobbuf;
639 size_t len = ops->len;
640
641 BUG_ON(ops->mode != MTD_OOB_PLACE);
642
643 ofs += ops->ooboffs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644
645 /* Find the chip which is to be used and select it */
646 if (this->curfloor != mychip->floor) {
647 DoC_SelectFloor(docptr, mychip->floor);
648 DoC_SelectChip(docptr, mychip->chip);
649 } else if (this->curchip != mychip->chip) {
650 DoC_SelectChip(docptr, mychip->chip);
651 }
652 this->curfloor = mychip->floor;
653 this->curchip = mychip->chip;
654
655 /* disable the ECC engine */
656 WriteDOC (DOC_ECC_RESET, docptr, ECCConf);
657 WriteDOC (DOC_ECC_DIS, docptr, ECCConf);
658
659 /* issue the Read2 command to set the pointer to the Spare Data Area.
660 Polling the Flash Ready bit after issue 3 bytes address in
661 Sequence Read Mode, see Software Requirement 11.4 item 1.*/
662 DoC_Command(docptr, NAND_CMD_READOOB, CDSN_CTRL_WP);
663 DoC_Address(docptr, 3, ofs, CDSN_CTRL_WP, 0x00);
664 DoC_WaitReady(docptr);
665
666 /* Read the data out via the internal pipeline through CDSN IO register,
667 see Pipelined Read Operations 11.3 */
668 dummy = ReadDOC(docptr, ReadPipeInit);
669#ifndef USE_MEMCPY
670 for (i = 0; i < len-1; i++) {
671 /* N.B. you have to increase the source address in this way or the
672 ECC logic will not work properly */
673 buf[i] = ReadDOC(docptr, Mil_CDSN_IO + i);
674 }
675#else
676 memcpy_fromio(buf, docptr + DoC_Mil_CDSN_IO, len - 1);
677#endif
678 buf[len - 1] = ReadDOC(docptr, LastDataRead);
679
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200680 ops->retlen = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681
682 return 0;
683}
684
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200685static int doc_write_oob(struct mtd_info *mtd, loff_t ofs,
686 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687{
688#ifndef USE_MEMCPY
689 int i;
690#endif
691 volatile char dummy;
692 int ret = 0;
693 struct DiskOnChip *this = mtd->priv;
694 void __iomem *docptr = this->virtadr;
695 struct Nand *mychip = &this->chips[ofs >> this->chipshift];
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200696 uint8_t *buf = ops->oobbuf;
697 size_t len = ops->len;
698
699 BUG_ON(ops->mode != MTD_OOB_PLACE);
700
701 ofs += ops->ooboffs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
703 /* Find the chip which is to be used and select it */
704 if (this->curfloor != mychip->floor) {
705 DoC_SelectFloor(docptr, mychip->floor);
706 DoC_SelectChip(docptr, mychip->chip);
707 } else if (this->curchip != mychip->chip) {
708 DoC_SelectChip(docptr, mychip->chip);
709 }
710 this->curfloor = mychip->floor;
711 this->curchip = mychip->chip;
712
713 /* disable the ECC engine */
714 WriteDOC (DOC_ECC_RESET, docptr, ECCConf);
715 WriteDOC (DOC_ECC_DIS, docptr, ECCConf);
716
717 /* Reset the chip, see Software Requirement 11.4 item 1. */
718 DoC_Command(docptr, NAND_CMD_RESET, CDSN_CTRL_WP);
719 DoC_WaitReady(docptr);
720 /* issue the Read2 command to set the pointer to the Spare Data Area. */
721 DoC_Command(docptr, NAND_CMD_READOOB, CDSN_CTRL_WP);
722
723 /* issue the Serial Data In command to initial the Page Program process */
724 DoC_Command(docptr, NAND_CMD_SEQIN, 0x00);
725 DoC_Address(docptr, 3, ofs, 0x00, 0x00);
726
727 /* Write the data via the internal pipeline through CDSN IO register,
728 see Pipelined Write Operations 11.2 */
729#ifndef USE_MEMCPY
730 for (i = 0; i < len; i++) {
731 /* N.B. you have to increase the source address in this way or the
732 ECC logic will not work properly */
733 WriteDOC(buf[i], docptr, Mil_CDSN_IO + i);
734 }
735#else
736 memcpy_toio(docptr + DoC_Mil_CDSN_IO, buf, len);
737#endif
738 WriteDOC(0x00, docptr, WritePipeTerm);
739
740 /* Commit the Page Program command and wait for ready
741 see Software Requirement 11.4 item 1.*/
742 DoC_Command(docptr, NAND_CMD_PAGEPROG, 0x00);
743 DoC_WaitReady(docptr);
744
745 /* Read the status of the flash device through CDSN IO register
746 see Software Requirement 11.4 item 5.*/
747 DoC_Command(docptr, NAND_CMD_STATUS, 0x00);
748 dummy = ReadDOC(docptr, ReadPipeInit);
749 DoC_Delay(docptr, 2);
750 if (ReadDOC(docptr, Mil_CDSN_IO) & 1) {
751 printk("Error programming oob data\n");
752 /* FIXME: implement Bad Block Replacement (in nftl.c ??) */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200753 ops->retlen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 ret = -EIO;
755 }
756 dummy = ReadDOC(docptr, LastDataRead);
757
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200758 ops->retlen = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759
760 return ret;
761}
762
763int doc_erase (struct mtd_info *mtd, struct erase_info *instr)
764{
765 volatile char dummy;
766 struct DiskOnChip *this = mtd->priv;
767 __u32 ofs = instr->addr;
768 __u32 len = instr->len;
769 void __iomem *docptr = this->virtadr;
770 struct Nand *mychip = &this->chips[ofs >> this->chipshift];
771
Thomas Gleixnere5580fb2005-11-07 11:15:40 +0000772 if (len != mtd->erasesize)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 printk(KERN_WARNING "Erase not right size (%x != %x)n",
774 len, mtd->erasesize);
775
776 /* Find the chip which is to be used and select it */
777 if (this->curfloor != mychip->floor) {
778 DoC_SelectFloor(docptr, mychip->floor);
779 DoC_SelectChip(docptr, mychip->chip);
780 } else if (this->curchip != mychip->chip) {
781 DoC_SelectChip(docptr, mychip->chip);
782 }
783 this->curfloor = mychip->floor;
784 this->curchip = mychip->chip;
785
786 instr->state = MTD_ERASE_PENDING;
787
788 /* issue the Erase Setup command */
789 DoC_Command(docptr, NAND_CMD_ERASE1, 0x00);
790 DoC_Address(docptr, 2, ofs, 0x00, 0x00);
791
792 /* Commit the Erase Start command and wait for ready
793 see Software Requirement 11.4 item 1.*/
794 DoC_Command(docptr, NAND_CMD_ERASE2, 0x00);
795 DoC_WaitReady(docptr);
796
797 instr->state = MTD_ERASING;
798
799 /* Read the status of the flash device through CDSN IO register
800 see Software Requirement 11.4 item 5.
801 FIXME: it seems that we are not wait long enough, some blocks are not
802 erased fully */
803 DoC_Command(docptr, NAND_CMD_STATUS, CDSN_CTRL_WP);
804 dummy = ReadDOC(docptr, ReadPipeInit);
805 DoC_Delay(docptr, 2);
806 if (ReadDOC(docptr, Mil_CDSN_IO) & 1) {
807 printk("Error Erasing at 0x%x\n", ofs);
808 /* There was an error
809 FIXME: implement Bad Block Replacement (in nftl.c ??) */
810 instr->state = MTD_ERASE_FAILED;
811 } else
812 instr->state = MTD_ERASE_DONE;
813 dummy = ReadDOC(docptr, LastDataRead);
814
815 mtd_erase_callback(instr);
816
817 return 0;
818}
819
820/****************************************************************************
821 *
822 * Module stuff
823 *
824 ****************************************************************************/
825
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826static void __exit cleanup_doc2001(void)
827{
828 struct mtd_info *mtd;
829 struct DiskOnChip *this;
830
831 while ((mtd=docmillist)) {
832 this = mtd->priv;
833 docmillist = this->nextdoc;
Thomas Gleixnere5580fb2005-11-07 11:15:40 +0000834
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 del_mtd_device(mtd);
Thomas Gleixnere5580fb2005-11-07 11:15:40 +0000836
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 iounmap(this->virtadr);
838 kfree(this->chips);
839 kfree(mtd);
840 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841}
842
843module_exit(cleanup_doc2001);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844
845MODULE_LICENSE("GPL");
846MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org> et al.");
847MODULE_DESCRIPTION("Alternative driver for DiskOnChip Millennium");