blob: fe71a12c26279b53dffeb5e9e13aeb91a02cc3da [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/init.h>
20#include <linux/types.h>
21#include <linux/bitops.h>
22
23#include <linux/mtd/mtd.h>
24#include <linux/mtd/nand.h>
25#include <linux/mtd/doc2000.h>
26
27/* #define ECC_DEBUG */
28
29/* I have no idea why some DoC chips can not use memcop_form|to_io().
30 * This may be due to the different revisions of the ASIC controller built-in or
31 * simplily a QA/Bug issue. Who knows ?? If you have trouble, please uncomment
32 * this:*/
33#undef USE_MEMCPY
34
35static int doc_read(struct mtd_info *mtd, loff_t from, size_t len,
36 size_t *retlen, u_char *buf);
37static int doc_write(struct mtd_info *mtd, loff_t to, size_t len,
38 size_t *retlen, const u_char *buf);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +020039static int doc_read_oob(struct mtd_info *mtd, loff_t ofs,
40 struct mtd_oob_ops *ops);
41static int doc_write_oob(struct mtd_info *mtd, loff_t ofs,
42 struct mtd_oob_ops *ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -070043static int doc_erase (struct mtd_info *mtd, struct erase_info *instr);
44
45static struct mtd_info *docmillist = NULL;
46
47/* Perform the required delay cycles by reading from the NOP register */
48static void DoC_Delay(void __iomem * docptr, unsigned short cycles)
49{
50 volatile char dummy;
51 int i;
52
53 for (i = 0; i < cycles; i++)
54 dummy = ReadDOC(docptr, NOP);
55}
56
57/* DOC_WaitReady: Wait for RDY line to be asserted by the flash chip */
58static int _DoC_WaitReady(void __iomem * docptr)
59{
60 unsigned short c = 0xffff;
61
62 DEBUG(MTD_DEBUG_LEVEL3,
63 "_DoC_WaitReady called for out-of-line wait\n");
64
65 /* Out-of-line routine to wait for chip response */
66 while (!(ReadDOC(docptr, CDSNControl) & CDSN_CTRL_FR_B) && --c)
67 ;
68
69 if (c == 0)
70 DEBUG(MTD_DEBUG_LEVEL2, "_DoC_WaitReady timed out.\n");
71
72 return (c == 0);
73}
74
75static inline int DoC_WaitReady(void __iomem * docptr)
76{
77 /* This is inline, to optimise the common case, where it's ready instantly */
78 int ret = 0;
79
80 /* 4 read form NOP register should be issued in prior to the read from CDSNControl
81 see Software Requirement 11.4 item 2. */
82 DoC_Delay(docptr, 4);
83
84 if (!(ReadDOC(docptr, CDSNControl) & CDSN_CTRL_FR_B))
85 /* Call the out-of-line routine to wait */
86 ret = _DoC_WaitReady(docptr);
87
88 /* issue 2 read from NOP register after reading from CDSNControl register
89 see Software Requirement 11.4 item 2. */
90 DoC_Delay(docptr, 2);
91
92 return ret;
93}
94
95/* DoC_Command: Send a flash command to the flash chip through the CDSN IO register
96 with the internal pipeline. Each of 4 delay cycles (read from the NOP register) is
97 required after writing to CDSN Control register, see Software Requirement 11.4 item 3. */
98
Arjan van de Ven858119e2006-01-14 13:20:43 -080099static void DoC_Command(void __iomem * docptr, unsigned char command,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 unsigned char xtraflags)
101{
102 /* Assert the CLE (Command Latch Enable) line to the flash chip */
103 WriteDOC(xtraflags | CDSN_CTRL_CLE | CDSN_CTRL_CE, docptr, CDSNControl);
104 DoC_Delay(docptr, 4);
105
106 /* Send the command */
107 WriteDOC(command, docptr, Mil_CDSN_IO);
108 WriteDOC(0x00, docptr, WritePipeTerm);
109
110 /* Lower the CLE line */
111 WriteDOC(xtraflags | CDSN_CTRL_CE, docptr, CDSNControl);
112 DoC_Delay(docptr, 4);
113}
114
115/* DoC_Address: Set the current address for the flash chip through the CDSN IO register
116 with the internal pipeline. Each of 4 delay cycles (read from the NOP register) is
117 required after writing to CDSN Control register, see Software Requirement 11.4 item 3. */
118
119static inline void DoC_Address(void __iomem * docptr, int numbytes, unsigned long ofs,
120 unsigned char xtraflags1, unsigned char xtraflags2)
121{
122 /* Assert the ALE (Address Latch Enable) line to the flash chip */
123 WriteDOC(xtraflags1 | CDSN_CTRL_ALE | CDSN_CTRL_CE, docptr, CDSNControl);
124 DoC_Delay(docptr, 4);
125
126 /* Send the address */
127 switch (numbytes)
128 {
129 case 1:
130 /* Send single byte, bits 0-7. */
131 WriteDOC(ofs & 0xff, docptr, Mil_CDSN_IO);
132 WriteDOC(0x00, docptr, WritePipeTerm);
133 break;
134 case 2:
135 /* Send bits 9-16 followed by 17-23 */
136 WriteDOC((ofs >> 9) & 0xff, docptr, Mil_CDSN_IO);
137 WriteDOC((ofs >> 17) & 0xff, docptr, Mil_CDSN_IO);
138 WriteDOC(0x00, docptr, WritePipeTerm);
139 break;
140 case 3:
141 /* Send 0-7, 9-16, then 17-23 */
142 WriteDOC(ofs & 0xff, docptr, Mil_CDSN_IO);
143 WriteDOC((ofs >> 9) & 0xff, docptr, Mil_CDSN_IO);
144 WriteDOC((ofs >> 17) & 0xff, docptr, Mil_CDSN_IO);
145 WriteDOC(0x00, docptr, WritePipeTerm);
146 break;
147 default:
148 return;
149 }
150
151 /* Lower the ALE line */
152 WriteDOC(xtraflags1 | xtraflags2 | CDSN_CTRL_CE, docptr, CDSNControl);
153 DoC_Delay(docptr, 4);
154}
155
156/* DoC_SelectChip: Select a given flash chip within the current floor */
157static int DoC_SelectChip(void __iomem * docptr, int chip)
158{
159 /* Select the individual flash chip requested */
160 WriteDOC(chip, docptr, CDSNDeviceSelect);
161 DoC_Delay(docptr, 4);
162
163 /* Wait for it to be ready */
164 return DoC_WaitReady(docptr);
165}
166
167/* DoC_SelectFloor: Select a given floor (bank of flash chips) */
168static int DoC_SelectFloor(void __iomem * docptr, int floor)
169{
170 /* Select the floor (bank) of chips required */
171 WriteDOC(floor, docptr, FloorSelect);
172
173 /* Wait for the chip to be ready */
174 return DoC_WaitReady(docptr);
175}
176
177/* DoC_IdentChip: Identify a given NAND chip given {floor,chip} */
178static int DoC_IdentChip(struct DiskOnChip *doc, int floor, int chip)
179{
180 int mfr, id, i, j;
181 volatile char dummy;
182
183 /* Page in the required floor/chip
184 FIXME: is this supported by Millennium ?? */
185 DoC_SelectFloor(doc->virtadr, floor);
186 DoC_SelectChip(doc->virtadr, chip);
187
188 /* Reset the chip, see Software Requirement 11.4 item 1. */
189 DoC_Command(doc->virtadr, NAND_CMD_RESET, CDSN_CTRL_WP);
190 DoC_WaitReady(doc->virtadr);
191
Thomas Gleixnere5580fb2005-11-07 11:15:40 +0000192 /* Read the NAND chip ID: 1. Send ReadID command */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 DoC_Command(doc->virtadr, NAND_CMD_READID, CDSN_CTRL_WP);
194
Thomas Gleixnere5580fb2005-11-07 11:15:40 +0000195 /* Read the NAND chip ID: 2. Send address byte zero */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 DoC_Address(doc->virtadr, 1, 0x00, CDSN_CTRL_WP, 0x00);
197
198 /* Read the manufacturer and device id codes of the flash device through
199 CDSN IO register see Software Requirement 11.4 item 5.*/
200 dummy = ReadDOC(doc->virtadr, ReadPipeInit);
201 DoC_Delay(doc->virtadr, 2);
202 mfr = ReadDOC(doc->virtadr, Mil_CDSN_IO);
203
204 DoC_Delay(doc->virtadr, 2);
205 id = ReadDOC(doc->virtadr, Mil_CDSN_IO);
206 dummy = ReadDOC(doc->virtadr, LastDataRead);
207
208 /* No response - return failure */
209 if (mfr == 0xff || mfr == 0)
210 return 0;
211
212 /* FIXME: to deal with multi-flash on multi-Millennium case more carefully */
213 for (i = 0; nand_flash_ids[i].name != NULL; i++) {
214 if ( id == nand_flash_ids[i].id) {
215 /* Try to identify manufacturer */
216 for (j = 0; nand_manuf_ids[j].id != 0x0; j++) {
217 if (nand_manuf_ids[j].id == mfr)
218 break;
Thomas Gleixnere5580fb2005-11-07 11:15:40 +0000219 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 printk(KERN_INFO "Flash chip found: Manufacturer ID: %2.2X, "
221 "Chip ID: %2.2X (%s:%s)\n",
222 mfr, id, nand_manuf_ids[j].name, nand_flash_ids[i].name);
223 doc->mfr = mfr;
224 doc->id = id;
225 doc->chipshift = ffs((nand_flash_ids[i].chipsize << 20)) - 1;
226 break;
227 }
228 }
229
230 if (nand_flash_ids[i].name == NULL)
231 return 0;
232 else
233 return 1;
234}
235
236/* DoC_ScanChips: Find all NAND chips present in a DiskOnChip, and identify them */
237static void DoC_ScanChips(struct DiskOnChip *this)
238{
239 int floor, chip;
240 int numchips[MAX_FLOORS_MIL];
241 int ret;
242
243 this->numchips = 0;
244 this->mfr = 0;
245 this->id = 0;
246
247 /* For each floor, find the number of valid chips it contains */
248 for (floor = 0,ret = 1; floor < MAX_FLOORS_MIL; floor++) {
249 numchips[floor] = 0;
250 for (chip = 0; chip < MAX_CHIPS_MIL && ret != 0; chip++) {
251 ret = DoC_IdentChip(this, floor, chip);
252 if (ret) {
253 numchips[floor]++;
254 this->numchips++;
255 }
256 }
257 }
258 /* If there are none at all that we recognise, bail */
259 if (!this->numchips) {
260 printk("No flash chips recognised.\n");
261 return;
262 }
263
264 /* Allocate an array to hold the information for each chip */
265 this->chips = kmalloc(sizeof(struct Nand) * this->numchips, GFP_KERNEL);
266 if (!this->chips){
267 printk("No memory for allocating chip info structures\n");
268 return;
269 }
270
Thomas Gleixnere5580fb2005-11-07 11:15:40 +0000271 /* Fill out the chip array with {floor, chipno} for each
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 * detected chip in the device. */
273 for (floor = 0, ret = 0; floor < MAX_FLOORS_MIL; floor++) {
274 for (chip = 0 ; chip < numchips[floor] ; chip++) {
275 this->chips[ret].floor = floor;
276 this->chips[ret].chip = chip;
277 this->chips[ret].curadr = 0;
278 this->chips[ret].curmode = 0x50;
279 ret++;
280 }
281 }
282
283 /* Calculate and print the total size of the device */
284 this->totlen = this->numchips * (1 << this->chipshift);
285 printk(KERN_INFO "%d flash chips found. Total DiskOnChip size: %ld MiB\n",
286 this->numchips ,this->totlen >> 20);
287}
288
289static int DoCMil_is_alias(struct DiskOnChip *doc1, struct DiskOnChip *doc2)
290{
291 int tmp1, tmp2, retval;
292
293 if (doc1->physadr == doc2->physadr)
294 return 1;
295
296 /* Use the alias resolution register which was set aside for this
297 * purpose. If it's value is the same on both chips, they might
298 * be the same chip, and we write to one and check for a change in
299 * the other. It's unclear if this register is usuable in the
300 * DoC 2000 (it's in the Millenium docs), but it seems to work. */
301 tmp1 = ReadDOC(doc1->virtadr, AliasResolution);
302 tmp2 = ReadDOC(doc2->virtadr, AliasResolution);
303 if (tmp1 != tmp2)
304 return 0;
Thomas Gleixnere5580fb2005-11-07 11:15:40 +0000305
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 WriteDOC((tmp1+1) % 0xff, doc1->virtadr, AliasResolution);
307 tmp2 = ReadDOC(doc2->virtadr, AliasResolution);
308 if (tmp2 == (tmp1+1) % 0xff)
309 retval = 1;
310 else
311 retval = 0;
312
313 /* Restore register contents. May not be necessary, but do it just to
314 * be safe. */
315 WriteDOC(tmp1, doc1->virtadr, AliasResolution);
316
317 return retval;
318}
319
David Woodhouse5e535422006-05-08 14:05:05 +0100320/* This routine is found from the docprobe code by symbol_get(),
321 * which will bump the use count of this module. */
322void DoCMil_init(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323{
324 struct DiskOnChip *this = mtd->priv;
325 struct DiskOnChip *old = NULL;
326
327 /* We must avoid being called twice for the same device. */
328 if (docmillist)
329 old = docmillist->priv;
330
331 while (old) {
332 if (DoCMil_is_alias(this, old)) {
333 printk(KERN_NOTICE "Ignoring DiskOnChip Millennium at "
334 "0x%lX - already configured\n", this->physadr);
335 iounmap(this->virtadr);
336 kfree(mtd);
337 return;
338 }
339 if (old->nextdoc)
340 old = old->nextdoc->priv;
341 else
342 old = NULL;
343 }
344
345 mtd->name = "DiskOnChip Millennium";
346 printk(KERN_NOTICE "DiskOnChip Millennium found at address 0x%lX\n",
347 this->physadr);
348
349 mtd->type = MTD_NANDFLASH;
350 mtd->flags = MTD_CAP_NANDFLASH;
351 mtd->ecctype = MTD_ECC_RS_DiskOnChip;
352 mtd->size = 0;
353
354 /* FIXME: erase size is not always 8KiB */
355 mtd->erasesize = 0x2000;
356
Joern Engel28318772006-05-22 23:18:05 +0200357 mtd->writesize = 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 mtd->oobsize = 16;
359 mtd->owner = THIS_MODULE;
360 mtd->erase = doc_erase;
361 mtd->point = NULL;
362 mtd->unpoint = NULL;
363 mtd->read = doc_read;
364 mtd->write = doc_write;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 mtd->read_oob = doc_read_oob;
366 mtd->write_oob = doc_write_oob;
367 mtd->sync = NULL;
368
369 this->totlen = 0;
370 this->numchips = 0;
371 this->curfloor = -1;
372 this->curchip = -1;
373
374 /* Ident all the chips present. */
375 DoC_ScanChips(this);
376
377 if (!this->totlen) {
378 kfree(mtd);
379 iounmap(this->virtadr);
380 } else {
381 this->nextdoc = docmillist;
382 docmillist = mtd;
383 mtd->size = this->totlen;
384 add_mtd_device(mtd);
385 return;
386 }
387}
David Woodhousef0ad11d2006-05-12 11:40:13 +0100388EXPORT_SYMBOL_GPL(DoCMil_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389
390static int doc_read (struct mtd_info *mtd, loff_t from, size_t len,
391 size_t *retlen, u_char *buf)
392{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 int i, ret;
394 volatile char dummy;
Thomas Gleixner7f8a8942006-06-28 10:11:33 +0200395 unsigned char syndrome[6], eccbuf[6];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 struct DiskOnChip *this = mtd->priv;
397 void __iomem *docptr = this->virtadr;
398 struct Nand *mychip = &this->chips[from >> (this->chipshift)];
399
400 /* Don't allow read past end of device */
401 if (from >= this->totlen)
402 return -EINVAL;
403
404 /* Don't allow a single read to cross a 512-byte block boundary */
Thomas Gleixnere5580fb2005-11-07 11:15:40 +0000405 if (from + len > ((from | 0x1ff) + 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 len = ((from | 0x1ff) + 1) - from;
407
408 /* Find the chip which is to be used and select it */
409 if (this->curfloor != mychip->floor) {
410 DoC_SelectFloor(docptr, mychip->floor);
411 DoC_SelectChip(docptr, mychip->chip);
412 } else if (this->curchip != mychip->chip) {
413 DoC_SelectChip(docptr, mychip->chip);
414 }
415 this->curfloor = mychip->floor;
416 this->curchip = mychip->chip;
417
418 /* issue the Read0 or Read1 command depend on which half of the page
419 we are accessing. Polling the Flash Ready bit after issue 3 bytes
420 address in Sequence Read Mode, see Software Requirement 11.4 item 1.*/
421 DoC_Command(docptr, (from >> 8) & 1, CDSN_CTRL_WP);
422 DoC_Address(docptr, 3, from, CDSN_CTRL_WP, 0x00);
423 DoC_WaitReady(docptr);
424
Thomas Gleixner7f8a8942006-06-28 10:11:33 +0200425 /* init the ECC engine, see Reed-Solomon EDC/ECC 11.1 .*/
426 WriteDOC (DOC_ECC_RESET, docptr, ECCConf);
427 WriteDOC (DOC_ECC_EN, docptr, ECCConf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
429 /* Read the data via the internal pipeline through CDSN IO register,
430 see Pipelined Read Operations 11.3 */
431 dummy = ReadDOC(docptr, ReadPipeInit);
432#ifndef USE_MEMCPY
433 for (i = 0; i < len-1; i++) {
434 /* N.B. you have to increase the source address in this way or the
435 ECC logic will not work properly */
436 buf[i] = ReadDOC(docptr, Mil_CDSN_IO + (i & 0xff));
437 }
438#else
439 memcpy_fromio(buf, docptr + DoC_Mil_CDSN_IO, len - 1);
440#endif
441 buf[len - 1] = ReadDOC(docptr, LastDataRead);
442
443 /* Let the caller know we completed it */
444 *retlen = len;
445 ret = 0;
446
Thomas Gleixner7f8a8942006-06-28 10:11:33 +0200447 /* Read the ECC data from Spare Data Area,
448 see Reed-Solomon EDC/ECC 11.1 */
449 dummy = ReadDOC(docptr, ReadPipeInit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450#ifndef USE_MEMCPY
Thomas Gleixner7f8a8942006-06-28 10:11:33 +0200451 for (i = 0; i < 5; i++) {
452 /* N.B. you have to increase the source address in this way or the
453 ECC logic will not work properly */
454 eccbuf[i] = ReadDOC(docptr, Mil_CDSN_IO + i);
455 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456#else
Thomas Gleixner7f8a8942006-06-28 10:11:33 +0200457 memcpy_fromio(eccbuf, docptr + DoC_Mil_CDSN_IO, 5);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458#endif
Thomas Gleixner7f8a8942006-06-28 10:11:33 +0200459 eccbuf[5] = ReadDOC(docptr, LastDataRead);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460
Thomas Gleixner7f8a8942006-06-28 10:11:33 +0200461 /* Flush the pipeline */
462 dummy = ReadDOC(docptr, ECCConf);
463 dummy = ReadDOC(docptr, ECCConf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464
Thomas Gleixner7f8a8942006-06-28 10:11:33 +0200465 /* Check the ECC Status */
466 if (ReadDOC(docptr, ECCConf) & 0x80) {
467 int nb_errors;
468 /* There was an ECC error */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469#ifdef ECC_DEBUG
Thomas Gleixner7f8a8942006-06-28 10:11:33 +0200470 printk("DiskOnChip ECC Error: Read at %lx\n", (long)from);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471#endif
Thomas Gleixner7f8a8942006-06-28 10:11:33 +0200472 /* Read the ECC syndrom through the DiskOnChip ECC logic.
473 These syndrome will be all ZERO when there is no error */
474 for (i = 0; i < 6; i++) {
475 syndrome[i] = ReadDOC(docptr, ECCSyndrome0 + i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 }
Thomas Gleixner7f8a8942006-06-28 10:11:33 +0200477 nb_errors = doc_decode_ecc(buf, syndrome);
478#ifdef ECC_DEBUG
479 printk("ECC Errors corrected: %x\n", nb_errors);
480#endif
481 if (nb_errors < 0) {
482 /* We return error, but have actually done the read. Not that
483 this can be told to user-space, via sys_read(), but at least
484 MTD-aware stuff can know about it by checking *retlen */
485 ret = -EIO;
486 }
487 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
489#ifdef PSYCHO_DEBUG
Thomas Gleixner7f8a8942006-06-28 10:11:33 +0200490 printk("ECC DATA at %lx: %2.2X %2.2X %2.2X %2.2X %2.2X %2.2X\n",
491 (long)from, eccbuf[0], eccbuf[1], eccbuf[2], eccbuf[3],
492 eccbuf[4], eccbuf[5]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493#endif
494
Thomas Gleixner7f8a8942006-06-28 10:11:33 +0200495 /* disable the ECC engine */
496 WriteDOC(DOC_ECC_DIS, docptr , ECCConf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497
498 return ret;
499}
500
501static int doc_write (struct mtd_info *mtd, loff_t to, size_t len,
502 size_t *retlen, const u_char *buf)
503{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 int i,ret = 0;
Thomas Gleixner7f8a8942006-06-28 10:11:33 +0200505 char eccbuf[6];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 volatile char dummy;
507 struct DiskOnChip *this = mtd->priv;
508 void __iomem *docptr = this->virtadr;
509 struct Nand *mychip = &this->chips[to >> (this->chipshift)];
510
511 /* Don't allow write past end of device */
512 if (to >= this->totlen)
513 return -EINVAL;
514
515#if 0
516 /* Don't allow a single write to cross a 512-byte block boundary */
Thomas Gleixnere5580fb2005-11-07 11:15:40 +0000517 if (to + len > ( (to | 0x1ff) + 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 len = ((to | 0x1ff) + 1) - to;
519#else
520 /* Don't allow writes which aren't exactly one block */
521 if (to & 0x1ff || len != 0x200)
522 return -EINVAL;
523#endif
524
525 /* Find the chip which is to be used and select it */
526 if (this->curfloor != mychip->floor) {
527 DoC_SelectFloor(docptr, mychip->floor);
528 DoC_SelectChip(docptr, mychip->chip);
529 } else if (this->curchip != mychip->chip) {
530 DoC_SelectChip(docptr, mychip->chip);
531 }
532 this->curfloor = mychip->floor;
533 this->curchip = mychip->chip;
534
535 /* Reset the chip, see Software Requirement 11.4 item 1. */
536 DoC_Command(docptr, NAND_CMD_RESET, 0x00);
537 DoC_WaitReady(docptr);
538 /* Set device to main plane of flash */
539 DoC_Command(docptr, NAND_CMD_READ0, 0x00);
540
541 /* issue the Serial Data In command to initial the Page Program process */
542 DoC_Command(docptr, NAND_CMD_SEQIN, 0x00);
543 DoC_Address(docptr, 3, to, 0x00, 0x00);
544 DoC_WaitReady(docptr);
545
Thomas Gleixner7f8a8942006-06-28 10:11:33 +0200546 /* init the ECC engine, see Reed-Solomon EDC/ECC 11.1 .*/
547 WriteDOC (DOC_ECC_RESET, docptr, ECCConf);
548 WriteDOC (DOC_ECC_EN | DOC_ECC_RW, docptr, ECCConf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
550 /* Write the data via the internal pipeline through CDSN IO register,
551 see Pipelined Write Operations 11.2 */
552#ifndef USE_MEMCPY
553 for (i = 0; i < len; i++) {
554 /* N.B. you have to increase the source address in this way or the
555 ECC logic will not work properly */
556 WriteDOC(buf[i], docptr, Mil_CDSN_IO + i);
557 }
558#else
559 memcpy_toio(docptr + DoC_Mil_CDSN_IO, buf, len);
560#endif
561 WriteDOC(0x00, docptr, WritePipeTerm);
562
Thomas Gleixner7f8a8942006-06-28 10:11:33 +0200563 /* Write ECC data to flash, the ECC info is generated by the DiskOnChip ECC logic
564 see Reed-Solomon EDC/ECC 11.1 */
565 WriteDOC(0, docptr, NOP);
566 WriteDOC(0, docptr, NOP);
567 WriteDOC(0, docptr, NOP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568
Thomas Gleixner7f8a8942006-06-28 10:11:33 +0200569 /* Read the ECC data through the DiskOnChip ECC logic */
570 for (i = 0; i < 6; i++) {
571 eccbuf[i] = ReadDOC(docptr, ECCSyndrome0 + i);
572 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573
Thomas Gleixner7f8a8942006-06-28 10:11:33 +0200574 /* ignore the ECC engine */
575 WriteDOC(DOC_ECC_DIS, docptr , ECCConf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576
577#ifndef USE_MEMCPY
Thomas Gleixner7f8a8942006-06-28 10:11:33 +0200578 /* Write the ECC data to flash */
579 for (i = 0; i < 6; i++) {
580 /* N.B. you have to increase the source address in this way or the
581 ECC logic will not work properly */
582 WriteDOC(eccbuf[i], docptr, Mil_CDSN_IO + i);
583 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584#else
Thomas Gleixner7f8a8942006-06-28 10:11:33 +0200585 memcpy_toio(docptr + DoC_Mil_CDSN_IO, eccbuf, 6);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586#endif
587
Thomas Gleixner7f8a8942006-06-28 10:11:33 +0200588 /* write the block status BLOCK_USED (0x5555) at the end of ECC data
589 FIXME: this is only a hack for programming the IPL area for LinuxBIOS
590 and should be replace with proper codes in user space utilities */
591 WriteDOC(0x55, docptr, Mil_CDSN_IO);
592 WriteDOC(0x55, docptr, Mil_CDSN_IO + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
Thomas Gleixner7f8a8942006-06-28 10:11:33 +0200594 WriteDOC(0x00, docptr, WritePipeTerm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595
596#ifdef PSYCHO_DEBUG
Thomas Gleixner7f8a8942006-06-28 10:11:33 +0200597 printk("OOB data at %lx is %2.2X %2.2X %2.2X %2.2X %2.2X %2.2X\n",
598 (long) to, eccbuf[0], eccbuf[1], eccbuf[2], eccbuf[3],
599 eccbuf[4], eccbuf[5]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601
602 /* Commit the Page Program command and wait for ready
603 see Software Requirement 11.4 item 1.*/
604 DoC_Command(docptr, NAND_CMD_PAGEPROG, 0x00);
605 DoC_WaitReady(docptr);
606
607 /* Read the status of the flash device through CDSN IO register
608 see Software Requirement 11.4 item 5.*/
609 DoC_Command(docptr, NAND_CMD_STATUS, CDSN_CTRL_WP);
610 dummy = ReadDOC(docptr, ReadPipeInit);
611 DoC_Delay(docptr, 2);
612 if (ReadDOC(docptr, Mil_CDSN_IO) & 1) {
613 printk("Error programming flash\n");
614 /* Error in programming
615 FIXME: implement Bad Block Replacement (in nftl.c ??) */
616 *retlen = 0;
617 ret = -EIO;
618 }
619 dummy = ReadDOC(docptr, LastDataRead);
620
621 /* Let the caller know we completed it */
622 *retlen = len;
623
624 return ret;
625}
626
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200627static int doc_read_oob(struct mtd_info *mtd, loff_t ofs,
628 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629{
630#ifndef USE_MEMCPY
631 int i;
632#endif
633 volatile char dummy;
634 struct DiskOnChip *this = mtd->priv;
635 void __iomem *docptr = this->virtadr;
636 struct Nand *mychip = &this->chips[ofs >> this->chipshift];
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200637 uint8_t *buf = ops->oobbuf;
638 size_t len = ops->len;
639
640 BUG_ON(ops->mode != MTD_OOB_PLACE);
641
642 ofs += ops->ooboffs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643
644 /* Find the chip which is to be used and select it */
645 if (this->curfloor != mychip->floor) {
646 DoC_SelectFloor(docptr, mychip->floor);
647 DoC_SelectChip(docptr, mychip->chip);
648 } else if (this->curchip != mychip->chip) {
649 DoC_SelectChip(docptr, mychip->chip);
650 }
651 this->curfloor = mychip->floor;
652 this->curchip = mychip->chip;
653
654 /* disable the ECC engine */
655 WriteDOC (DOC_ECC_RESET, docptr, ECCConf);
656 WriteDOC (DOC_ECC_DIS, docptr, ECCConf);
657
658 /* issue the Read2 command to set the pointer to the Spare Data Area.
659 Polling the Flash Ready bit after issue 3 bytes address in
660 Sequence Read Mode, see Software Requirement 11.4 item 1.*/
661 DoC_Command(docptr, NAND_CMD_READOOB, CDSN_CTRL_WP);
662 DoC_Address(docptr, 3, ofs, CDSN_CTRL_WP, 0x00);
663 DoC_WaitReady(docptr);
664
665 /* Read the data out via the internal pipeline through CDSN IO register,
666 see Pipelined Read Operations 11.3 */
667 dummy = ReadDOC(docptr, ReadPipeInit);
668#ifndef USE_MEMCPY
669 for (i = 0; i < len-1; i++) {
670 /* N.B. you have to increase the source address in this way or the
671 ECC logic will not work properly */
672 buf[i] = ReadDOC(docptr, Mil_CDSN_IO + i);
673 }
674#else
675 memcpy_fromio(buf, docptr + DoC_Mil_CDSN_IO, len - 1);
676#endif
677 buf[len - 1] = ReadDOC(docptr, LastDataRead);
678
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200679 ops->retlen = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680
681 return 0;
682}
683
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200684static int doc_write_oob(struct mtd_info *mtd, loff_t ofs,
685 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686{
687#ifndef USE_MEMCPY
688 int i;
689#endif
690 volatile char dummy;
691 int ret = 0;
692 struct DiskOnChip *this = mtd->priv;
693 void __iomem *docptr = this->virtadr;
694 struct Nand *mychip = &this->chips[ofs >> this->chipshift];
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200695 uint8_t *buf = ops->oobbuf;
696 size_t len = ops->len;
697
698 BUG_ON(ops->mode != MTD_OOB_PLACE);
699
700 ofs += ops->ooboffs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701
702 /* Find the chip which is to be used and select it */
703 if (this->curfloor != mychip->floor) {
704 DoC_SelectFloor(docptr, mychip->floor);
705 DoC_SelectChip(docptr, mychip->chip);
706 } else if (this->curchip != mychip->chip) {
707 DoC_SelectChip(docptr, mychip->chip);
708 }
709 this->curfloor = mychip->floor;
710 this->curchip = mychip->chip;
711
712 /* disable the ECC engine */
713 WriteDOC (DOC_ECC_RESET, docptr, ECCConf);
714 WriteDOC (DOC_ECC_DIS, docptr, ECCConf);
715
716 /* Reset the chip, see Software Requirement 11.4 item 1. */
717 DoC_Command(docptr, NAND_CMD_RESET, CDSN_CTRL_WP);
718 DoC_WaitReady(docptr);
719 /* issue the Read2 command to set the pointer to the Spare Data Area. */
720 DoC_Command(docptr, NAND_CMD_READOOB, CDSN_CTRL_WP);
721
722 /* issue the Serial Data In command to initial the Page Program process */
723 DoC_Command(docptr, NAND_CMD_SEQIN, 0x00);
724 DoC_Address(docptr, 3, ofs, 0x00, 0x00);
725
726 /* Write the data via the internal pipeline through CDSN IO register,
727 see Pipelined Write Operations 11.2 */
728#ifndef USE_MEMCPY
729 for (i = 0; i < len; i++) {
730 /* N.B. you have to increase the source address in this way or the
731 ECC logic will not work properly */
732 WriteDOC(buf[i], docptr, Mil_CDSN_IO + i);
733 }
734#else
735 memcpy_toio(docptr + DoC_Mil_CDSN_IO, buf, len);
736#endif
737 WriteDOC(0x00, docptr, WritePipeTerm);
738
739 /* Commit the Page Program command and wait for ready
740 see Software Requirement 11.4 item 1.*/
741 DoC_Command(docptr, NAND_CMD_PAGEPROG, 0x00);
742 DoC_WaitReady(docptr);
743
744 /* Read the status of the flash device through CDSN IO register
745 see Software Requirement 11.4 item 5.*/
746 DoC_Command(docptr, NAND_CMD_STATUS, 0x00);
747 dummy = ReadDOC(docptr, ReadPipeInit);
748 DoC_Delay(docptr, 2);
749 if (ReadDOC(docptr, Mil_CDSN_IO) & 1) {
750 printk("Error programming oob data\n");
751 /* FIXME: implement Bad Block Replacement (in nftl.c ??) */
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200752 ops->retlen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 ret = -EIO;
754 }
755 dummy = ReadDOC(docptr, LastDataRead);
756
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200757 ops->retlen = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758
759 return ret;
760}
761
762int doc_erase (struct mtd_info *mtd, struct erase_info *instr)
763{
764 volatile char dummy;
765 struct DiskOnChip *this = mtd->priv;
766 __u32 ofs = instr->addr;
767 __u32 len = instr->len;
768 void __iomem *docptr = this->virtadr;
769 struct Nand *mychip = &this->chips[ofs >> this->chipshift];
770
Thomas Gleixnere5580fb2005-11-07 11:15:40 +0000771 if (len != mtd->erasesize)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 printk(KERN_WARNING "Erase not right size (%x != %x)n",
773 len, mtd->erasesize);
774
775 /* Find the chip which is to be used and select it */
776 if (this->curfloor != mychip->floor) {
777 DoC_SelectFloor(docptr, mychip->floor);
778 DoC_SelectChip(docptr, mychip->chip);
779 } else if (this->curchip != mychip->chip) {
780 DoC_SelectChip(docptr, mychip->chip);
781 }
782 this->curfloor = mychip->floor;
783 this->curchip = mychip->chip;
784
785 instr->state = MTD_ERASE_PENDING;
786
787 /* issue the Erase Setup command */
788 DoC_Command(docptr, NAND_CMD_ERASE1, 0x00);
789 DoC_Address(docptr, 2, ofs, 0x00, 0x00);
790
791 /* Commit the Erase Start command and wait for ready
792 see Software Requirement 11.4 item 1.*/
793 DoC_Command(docptr, NAND_CMD_ERASE2, 0x00);
794 DoC_WaitReady(docptr);
795
796 instr->state = MTD_ERASING;
797
798 /* Read the status of the flash device through CDSN IO register
799 see Software Requirement 11.4 item 5.
800 FIXME: it seems that we are not wait long enough, some blocks are not
801 erased fully */
802 DoC_Command(docptr, NAND_CMD_STATUS, CDSN_CTRL_WP);
803 dummy = ReadDOC(docptr, ReadPipeInit);
804 DoC_Delay(docptr, 2);
805 if (ReadDOC(docptr, Mil_CDSN_IO) & 1) {
806 printk("Error Erasing at 0x%x\n", ofs);
807 /* There was an error
808 FIXME: implement Bad Block Replacement (in nftl.c ??) */
809 instr->state = MTD_ERASE_FAILED;
810 } else
811 instr->state = MTD_ERASE_DONE;
812 dummy = ReadDOC(docptr, LastDataRead);
813
814 mtd_erase_callback(instr);
815
816 return 0;
817}
818
819/****************************************************************************
820 *
821 * Module stuff
822 *
823 ****************************************************************************/
824
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825static void __exit cleanup_doc2001(void)
826{
827 struct mtd_info *mtd;
828 struct DiskOnChip *this;
829
830 while ((mtd=docmillist)) {
831 this = mtd->priv;
832 docmillist = this->nextdoc;
Thomas Gleixnere5580fb2005-11-07 11:15:40 +0000833
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 del_mtd_device(mtd);
Thomas Gleixnere5580fb2005-11-07 11:15:40 +0000835
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 iounmap(this->virtadr);
837 kfree(this->chips);
838 kfree(mtd);
839 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840}
841
842module_exit(cleanup_doc2001);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843
844MODULE_LICENSE("GPL");
845MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org> et al.");
846MODULE_DESCRIPTION("Alternative driver for DiskOnChip Millennium");