1
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 *
7 * $Id: doc2001.c,v 1.38 2002/12/10 15:05:42 gleixner Exp $
8 */
9
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <asm/errno.h>
13 #include <asm/io.h>
14 #include <asm/uaccess.h>
15 #include <linux/miscdevice.h>
16 #include <linux/pci.h>
17 #include <linux/delay.h>
18 #include <linux/slab.h>
19 #include <linux/sched.h>
20 #include <linux/init.h>
21 #include <linux/types.h>
22
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
35 static int doc_read(struct mtd_info *mtd, loff_t from, size_t len,
36 size_t *retlen, u_char *buf);
37 static int doc_write(struct mtd_info *mtd, loff_t to, size_t len,
38 size_t *retlen, const u_char *buf);
39 static int doc_read_ecc(struct mtd_info *mtd, loff_t from, size_t len,
40 size_t *retlen, u_char *buf, u_char *eccbuf, int oobsel);
41 static int doc_write_ecc(struct mtd_info *mtd, loff_t to, size_t len,
42 size_t *retlen, const u_char *buf, u_char *eccbuf, int oobsel);
43 static int doc_read_oob(struct mtd_info *mtd, loff_t ofs, size_t len,
44 size_t *retlen, u_char *buf);
45 static int doc_write_oob(struct mtd_info *mtd, loff_t ofs, size_t len,
46 size_t *retlen, const u_char *buf);
47 static int doc_erase (struct mtd_info *mtd, struct erase_info *instr);
48
49 static struct mtd_info *docmillist = NULL;
50
51 /* Perform the required delay cycles by reading from the NOP register */
DoC_Delay(unsigned long docptr,unsigned short cycles)52 static void DoC_Delay(unsigned long docptr, unsigned short cycles)
53 {
54 volatile char dummy;
55 int i;
56
57 for (i = 0; i < cycles; i++)
58 dummy = ReadDOC(docptr, NOP);
59 }
60
61 /* DOC_WaitReady: Wait for RDY line to be asserted by the flash chip */
_DoC_WaitReady(unsigned long docptr)62 static int _DoC_WaitReady(unsigned long docptr)
63 {
64 unsigned short c = 0xffff;
65
66 DEBUG(MTD_DEBUG_LEVEL3,
67 "_DoC_WaitReady called for out-of-line wait\n");
68
69 /* Out-of-line routine to wait for chip response */
70 while (!(ReadDOC(docptr, CDSNControl) & CDSN_CTRL_FR_B) && --c)
71 ;
72
73 if (c == 0)
74 DEBUG(MTD_DEBUG_LEVEL2, "_DoC_WaitReady timed out.\n");
75
76 return (c == 0);
77 }
78
DoC_WaitReady(unsigned long docptr)79 static inline int DoC_WaitReady(unsigned long docptr)
80 {
81 /* This is inline, to optimise the common case, where it's ready instantly */
82 int ret = 0;
83
84 /* 4 read form NOP register should be issued in prior to the read from CDSNControl
85 see Software Requirement 11.4 item 2. */
86 DoC_Delay(docptr, 4);
87
88 if (!(ReadDOC(docptr, CDSNControl) & CDSN_CTRL_FR_B))
89 /* Call the out-of-line routine to wait */
90 ret = _DoC_WaitReady(docptr);
91
92 /* issue 2 read from NOP register after reading from CDSNControl register
93 see Software Requirement 11.4 item 2. */
94 DoC_Delay(docptr, 2);
95
96 return ret;
97 }
98
99 /* DoC_Command: Send a flash command to the flash chip through the CDSN IO register
100 with the internal pipeline. Each of 4 delay cycles (read from the NOP register) is
101 required after writing to CDSN Control register, see Software Requirement 11.4 item 3. */
102
DoC_Command(unsigned long docptr,unsigned char command,unsigned char xtraflags)103 static inline void DoC_Command(unsigned long docptr, unsigned char command,
104 unsigned char xtraflags)
105 {
106 /* Assert the CLE (Command Latch Enable) line to the flash chip */
107 WriteDOC(xtraflags | CDSN_CTRL_CLE | CDSN_CTRL_CE, docptr, CDSNControl);
108 DoC_Delay(docptr, 4);
109
110 /* Send the command */
111 WriteDOC(command, docptr, Mil_CDSN_IO);
112 WriteDOC(0x00, docptr, WritePipeTerm);
113
114 /* Lower the CLE line */
115 WriteDOC(xtraflags | CDSN_CTRL_CE, docptr, CDSNControl);
116 DoC_Delay(docptr, 4);
117 }
118
119 /* DoC_Address: Set the current address for the flash chip through the CDSN IO register
120 with the internal pipeline. Each of 4 delay cycles (read from the NOP register) is
121 required after writing to CDSN Control register, see Software Requirement 11.4 item 3. */
122
DoC_Address(unsigned long docptr,int numbytes,unsigned long ofs,unsigned char xtraflags1,unsigned char xtraflags2)123 static inline void DoC_Address(unsigned long docptr, int numbytes, unsigned long ofs,
124 unsigned char xtraflags1, unsigned char xtraflags2)
125 {
126 /* Assert the ALE (Address Latch Enable) line to the flash chip */
127 WriteDOC(xtraflags1 | CDSN_CTRL_ALE | CDSN_CTRL_CE, docptr, CDSNControl);
128 DoC_Delay(docptr, 4);
129
130 /* Send the address */
131 switch (numbytes)
132 {
133 case 1:
134 /* Send single byte, bits 0-7. */
135 WriteDOC(ofs & 0xff, docptr, Mil_CDSN_IO);
136 WriteDOC(0x00, docptr, WritePipeTerm);
137 break;
138 case 2:
139 /* Send bits 9-16 followed by 17-23 */
140 WriteDOC((ofs >> 9) & 0xff, docptr, Mil_CDSN_IO);
141 WriteDOC((ofs >> 17) & 0xff, docptr, Mil_CDSN_IO);
142 WriteDOC(0x00, docptr, WritePipeTerm);
143 break;
144 case 3:
145 /* Send 0-7, 9-16, then 17-23 */
146 WriteDOC(ofs & 0xff, docptr, Mil_CDSN_IO);
147 WriteDOC((ofs >> 9) & 0xff, docptr, Mil_CDSN_IO);
148 WriteDOC((ofs >> 17) & 0xff, docptr, Mil_CDSN_IO);
149 WriteDOC(0x00, docptr, WritePipeTerm);
150 break;
151 default:
152 return;
153 }
154
155 /* Lower the ALE line */
156 WriteDOC(xtraflags1 | xtraflags2 | CDSN_CTRL_CE, docptr, CDSNControl);
157 DoC_Delay(docptr, 4);
158 }
159
160 /* DoC_SelectChip: Select a given flash chip within the current floor */
DoC_SelectChip(unsigned long docptr,int chip)161 static int DoC_SelectChip(unsigned long docptr, int chip)
162 {
163 /* Select the individual flash chip requested */
164 WriteDOC(chip, docptr, CDSNDeviceSelect);
165 DoC_Delay(docptr, 4);
166
167 /* Wait for it to be ready */
168 return DoC_WaitReady(docptr);
169 }
170
171 /* DoC_SelectFloor: Select a given floor (bank of flash chips) */
DoC_SelectFloor(unsigned long docptr,int floor)172 static int DoC_SelectFloor(unsigned long docptr, int floor)
173 {
174 /* Select the floor (bank) of chips required */
175 WriteDOC(floor, docptr, FloorSelect);
176
177 /* Wait for the chip to be ready */
178 return DoC_WaitReady(docptr);
179 }
180
181 /* DoC_IdentChip: Identify a given NAND chip given {floor,chip} */
DoC_IdentChip(struct DiskOnChip * doc,int floor,int chip)182 static int DoC_IdentChip(struct DiskOnChip *doc, int floor, int chip)
183 {
184 int mfr, id, i, j;
185 volatile char dummy;
186
187 /* Page in the required floor/chip
188 FIXME: is this supported by Millennium ?? */
189 DoC_SelectFloor(doc->virtadr, floor);
190 DoC_SelectChip(doc->virtadr, chip);
191
192 /* Reset the chip, see Software Requirement 11.4 item 1. */
193 DoC_Command(doc->virtadr, NAND_CMD_RESET, CDSN_CTRL_WP);
194 DoC_WaitReady(doc->virtadr);
195
196 /* Read the NAND chip ID: 1. Send ReadID command */
197 DoC_Command(doc->virtadr, NAND_CMD_READID, CDSN_CTRL_WP);
198
199 /* Read the NAND chip ID: 2. Send address byte zero */
200 DoC_Address(doc->virtadr, 1, 0x00, CDSN_CTRL_WP, 0x00);
201
202 /* Read the manufacturer and device id codes of the flash device through
203 CDSN IO register see Software Requirement 11.4 item 5.*/
204 dummy = ReadDOC(doc->virtadr, ReadPipeInit);
205 DoC_Delay(doc->virtadr, 2);
206 mfr = ReadDOC(doc->virtadr, Mil_CDSN_IO);
207
208 DoC_Delay(doc->virtadr, 2);
209 id = ReadDOC(doc->virtadr, Mil_CDSN_IO);
210 dummy = ReadDOC(doc->virtadr, LastDataRead);
211
212 /* No response - return failure */
213 if (mfr == 0xff || mfr == 0)
214 return 0;
215
216 /* FIXME: to deal with multi-flash on multi-Millennium case more carefully */
217 for (i = 0; nand_flash_ids[i].name != NULL; i++) {
218 if ( id == nand_flash_ids[i].id) {
219 /* Try to identify manufacturer */
220 for (j = 0; nand_manuf_ids[j].id != 0x0; j++) {
221 if (nand_manuf_ids[j].id == mfr)
222 break;
223 }
224 printk(KERN_INFO "Flash chip found: Manufacturer ID: %2.2X, "
225 "Chip ID: %2.2X (%s:%s)\n",
226 mfr, id, nand_manuf_ids[j].name, nand_flash_ids[i].name);
227 doc->mfr = mfr;
228 doc->id = id;
229 doc->chipshift = nand_flash_ids[i].chipshift;
230 break;
231 }
232 }
233
234 if (nand_flash_ids[i].name == NULL)
235 return 0;
236 else
237 return 1;
238 }
239
240 /* DoC_ScanChips: Find all NAND chips present in a DiskOnChip, and identify them */
DoC_ScanChips(struct DiskOnChip * this)241 static void DoC_ScanChips(struct DiskOnChip *this)
242 {
243 int floor, chip;
244 int numchips[MAX_FLOORS_MIL];
245 int ret;
246
247 this->numchips = 0;
248 this->mfr = 0;
249 this->id = 0;
250
251 /* For each floor, find the number of valid chips it contains */
252 for (floor = 0,ret = 1; floor < MAX_FLOORS_MIL; floor++) {
253 numchips[floor] = 0;
254 for (chip = 0; chip < MAX_CHIPS_MIL && ret != 0; chip++) {
255 ret = DoC_IdentChip(this, floor, chip);
256 if (ret) {
257 numchips[floor]++;
258 this->numchips++;
259 }
260 }
261 }
262 /* If there are none at all that we recognise, bail */
263 if (!this->numchips) {
264 printk("No flash chips recognised.\n");
265 return;
266 }
267
268 /* Allocate an array to hold the information for each chip */
269 this->chips = kmalloc(sizeof(struct Nand) * this->numchips, GFP_KERNEL);
270 if (!this->chips){
271 printk("No memory for allocating chip info structures\n");
272 return;
273 }
274
275 /* Fill out the chip array with {floor, chipno} for each
276 * detected chip in the device. */
277 for (floor = 0, ret = 0; floor < MAX_FLOORS_MIL; floor++) {
278 for (chip = 0 ; chip < numchips[floor] ; chip++) {
279 this->chips[ret].floor = floor;
280 this->chips[ret].chip = chip;
281 this->chips[ret].curadr = 0;
282 this->chips[ret].curmode = 0x50;
283 ret++;
284 }
285 }
286
287 /* Calculate and print the total size of the device */
288 this->totlen = this->numchips * (1 << this->chipshift);
289 printk(KERN_INFO "%d flash chips found. Total DiskOnChip size: %ld MiB\n",
290 this->numchips ,this->totlen >> 20);
291 }
292
DoCMil_is_alias(struct DiskOnChip * doc1,struct DiskOnChip * doc2)293 static int DoCMil_is_alias(struct DiskOnChip *doc1, struct DiskOnChip *doc2)
294 {
295 int tmp1, tmp2, retval;
296
297 if (doc1->physadr == doc2->physadr)
298 return 1;
299
300 /* Use the alias resolution register which was set aside for this
301 * purpose. If it's value is the same on both chips, they might
302 * be the same chip, and we write to one and check for a change in
303 * the other. It's unclear if this register is usuable in the
304 * DoC 2000 (it's in the Millenium docs), but it seems to work. */
305 tmp1 = ReadDOC(doc1->virtadr, AliasResolution);
306 tmp2 = ReadDOC(doc2->virtadr, AliasResolution);
307 if (tmp1 != tmp2)
308 return 0;
309
310 WriteDOC((tmp1+1) % 0xff, doc1->virtadr, AliasResolution);
311 tmp2 = ReadDOC(doc2->virtadr, AliasResolution);
312 if (tmp2 == (tmp1+1) % 0xff)
313 retval = 1;
314 else
315 retval = 0;
316
317 /* Restore register contents. May not be necessary, but do it just to
318 * be safe. */
319 WriteDOC(tmp1, doc1->virtadr, AliasResolution);
320
321 return retval;
322 }
323
324 static const char im_name[] = "DoCMil_init";
325
326 /* This routine is made available to other mtd code via
327 * inter_module_register. It must only be accessed through
328 * inter_module_get which will bump the use count of this module. The
329 * addresses passed back in mtd are valid as long as the use count of
330 * this module is non-zero, i.e. between inter_module_get and
331 * inter_module_put. Keith Owens <kaos@ocs.com.au> 29 Oct 2000.
332 */
DoCMil_init(struct mtd_info * mtd)333 static void DoCMil_init(struct mtd_info *mtd)
334 {
335 struct DiskOnChip *this = (struct DiskOnChip *)mtd->priv;
336 struct DiskOnChip *old = NULL;
337
338 /* We must avoid being called twice for the same device. */
339 if (docmillist)
340 old = (struct DiskOnChip *)docmillist->priv;
341
342 while (old) {
343 if (DoCMil_is_alias(this, old)) {
344 printk(KERN_NOTICE "Ignoring DiskOnChip Millennium at "
345 "0x%lX - already configured\n", this->physadr);
346 iounmap((void *)this->virtadr);
347 kfree(mtd);
348 return;
349 }
350 if (old->nextdoc)
351 old = (struct DiskOnChip *)old->nextdoc->priv;
352 else
353 old = NULL;
354 }
355
356 mtd->name = "DiskOnChip Millennium";
357 printk(KERN_NOTICE "DiskOnChip Millennium found at address 0x%lX\n",
358 this->physadr);
359
360 mtd->type = MTD_NANDFLASH;
361 mtd->flags = MTD_CAP_NANDFLASH;
362 mtd->size = 0;
363
364 /* FIXME: erase size is not always 8kB */
365 mtd->erasesize = 0x2000;
366
367 mtd->oobblock = 512;
368 mtd->oobsize = 16;
369 mtd->module = THIS_MODULE;
370 mtd->erase = doc_erase;
371 mtd->point = NULL;
372 mtd->unpoint = NULL;
373 mtd->read = doc_read;
374 mtd->write = doc_write;
375 mtd->read_ecc = doc_read_ecc;
376 mtd->write_ecc = doc_write_ecc;
377 mtd->read_oob = doc_read_oob;
378 mtd->write_oob = doc_write_oob;
379 mtd->sync = NULL;
380
381 this->totlen = 0;
382 this->numchips = 0;
383 this->curfloor = -1;
384 this->curchip = -1;
385
386 /* Ident all the chips present. */
387 DoC_ScanChips(this);
388
389 if (!this->totlen) {
390 kfree(mtd);
391 iounmap((void *)this->virtadr);
392 } else {
393 this->nextdoc = docmillist;
394 docmillist = mtd;
395 mtd->size = this->totlen;
396 add_mtd_device(mtd);
397 return;
398 }
399 }
400
doc_read(struct mtd_info * mtd,loff_t from,size_t len,size_t * retlen,u_char * buf)401 static int doc_read (struct mtd_info *mtd, loff_t from, size_t len,
402 size_t *retlen, u_char *buf)
403 {
404 /* Just a special case of doc_read_ecc */
405 return doc_read_ecc(mtd, from, len, retlen, buf, NULL, 0);
406 }
407
doc_read_ecc(struct mtd_info * mtd,loff_t from,size_t len,size_t * retlen,u_char * buf,u_char * eccbuf,int oobsel)408 static int doc_read_ecc (struct mtd_info *mtd, loff_t from, size_t len,
409 size_t *retlen, u_char *buf, u_char *eccbuf, int oobsel)
410 {
411 int i, ret;
412 volatile char dummy;
413 unsigned char syndrome[6];
414 struct DiskOnChip *this = (struct DiskOnChip *)mtd->priv;
415 unsigned long docptr = this->virtadr;
416 struct Nand *mychip = &this->chips[from >> (this->chipshift)];
417
418 /* Don't allow read past end of device */
419 if (from >= this->totlen)
420 return -EINVAL;
421
422 /* Don't allow a single read to cross a 512-byte block boundary */
423 if (from + len > ((from | 0x1ff) + 1))
424 len = ((from | 0x1ff) + 1) - from;
425
426 /* Find the chip which is to be used and select it */
427 if (this->curfloor != mychip->floor) {
428 DoC_SelectFloor(docptr, mychip->floor);
429 DoC_SelectChip(docptr, mychip->chip);
430 } else if (this->curchip != mychip->chip) {
431 DoC_SelectChip(docptr, mychip->chip);
432 }
433 this->curfloor = mychip->floor;
434 this->curchip = mychip->chip;
435
436 /* issue the Read0 or Read1 command depend on which half of the page
437 we are accessing. Polling the Flash Ready bit after issue 3 bytes
438 address in Sequence Read Mode, see Software Requirement 11.4 item 1.*/
439 DoC_Command(docptr, (from >> 8) & 1, CDSN_CTRL_WP);
440 DoC_Address(docptr, 3, from, CDSN_CTRL_WP, 0x00);
441 DoC_WaitReady(docptr);
442
443 if (eccbuf) {
444 /* init the ECC engine, see Reed-Solomon EDC/ECC 11.1 .*/
445 WriteDOC (DOC_ECC_RESET, docptr, ECCConf);
446 WriteDOC (DOC_ECC_EN, docptr, ECCConf);
447 } else {
448 /* disable the ECC engine */
449 WriteDOC (DOC_ECC_RESET, docptr, ECCConf);
450 WriteDOC (DOC_ECC_DIS, docptr, ECCConf);
451 }
452
453 /* Read the data via the internal pipeline through CDSN IO register,
454 see Pipelined Read Operations 11.3 */
455 dummy = ReadDOC(docptr, ReadPipeInit);
456 #ifndef USE_MEMCPY
457 for (i = 0; i < len-1; i++) {
458 /* N.B. you have to increase the source address in this way or the
459 ECC logic will not work properly */
460 buf[i] = ReadDOC(docptr, Mil_CDSN_IO + (i & 0xff));
461 }
462 #else
463 memcpy_fromio(buf, docptr + DoC_Mil_CDSN_IO, len - 1);
464 #endif
465 buf[len - 1] = ReadDOC(docptr, LastDataRead);
466
467 /* Let the caller know we completed it */
468 *retlen = len;
469 ret = 0;
470
471 if (eccbuf) {
472 /* Read the ECC data from Spare Data Area,
473 see Reed-Solomon EDC/ECC 11.1 */
474 dummy = ReadDOC(docptr, ReadPipeInit);
475 #ifndef USE_MEMCPY
476 for (i = 0; i < 5; i++) {
477 /* N.B. you have to increase the source address in this way or the
478 ECC logic will not work properly */
479 eccbuf[i] = ReadDOC(docptr, Mil_CDSN_IO + i);
480 }
481 #else
482 memcpy_fromio(eccbuf, docptr + DoC_Mil_CDSN_IO, 5);
483 #endif
484 eccbuf[5] = ReadDOC(docptr, LastDataRead);
485
486 /* Flush the pipeline */
487 dummy = ReadDOC(docptr, ECCConf);
488 dummy = ReadDOC(docptr, ECCConf);
489
490 /* Check the ECC Status */
491 if (ReadDOC(docptr, ECCConf) & 0x80) {
492 int nb_errors;
493 /* There was an ECC error */
494 #ifdef ECC_DEBUG
495 printk("DiskOnChip ECC Error: Read at %lx\n", (long)from);
496 #endif
497 /* Read the ECC syndrom through the DiskOnChip ECC logic.
498 These syndrome will be all ZERO when there is no error */
499 for (i = 0; i < 6; i++) {
500 syndrome[i] = ReadDOC(docptr, ECCSyndrome0 + i);
501 }
502 nb_errors = doc_decode_ecc(buf, syndrome);
503 #ifdef ECC_DEBUG
504 printk("ECC Errors corrected: %x\n", nb_errors);
505 #endif
506 if (nb_errors < 0) {
507 /* We return error, but have actually done the read. Not that
508 this can be told to user-space, via sys_read(), but at least
509 MTD-aware stuff can know about it by checking *retlen */
510 ret = -EIO;
511 }
512 }
513
514 #ifdef PSYCHO_DEBUG
515 printk("ECC DATA at %lx: %2.2X %2.2X %2.2X %2.2X %2.2X %2.2X\n",
516 (long)from, eccbuf[0], eccbuf[1], eccbuf[2], eccbuf[3],
517 eccbuf[4], eccbuf[5]);
518 #endif
519
520 /* disable the ECC engine */
521 WriteDOC(DOC_ECC_DIS, docptr , ECCConf);
522 }
523
524 return ret;
525 }
526
doc_write(struct mtd_info * mtd,loff_t to,size_t len,size_t * retlen,const u_char * buf)527 static int doc_write (struct mtd_info *mtd, loff_t to, size_t len,
528 size_t *retlen, const u_char *buf)
529 {
530 char eccbuf[6];
531 return doc_write_ecc(mtd, to, len, retlen, buf, eccbuf, 0);
532 }
533
doc_write_ecc(struct mtd_info * mtd,loff_t to,size_t len,size_t * retlen,const u_char * buf,u_char * eccbuf,int oobsel)534 static int doc_write_ecc (struct mtd_info *mtd, loff_t to, size_t len,
535 size_t *retlen, const u_char *buf, u_char *eccbuf, int oobsel)
536 {
537 int i,ret = 0;
538 volatile char dummy;
539 struct DiskOnChip *this = (struct DiskOnChip *)mtd->priv;
540 unsigned long docptr = this->virtadr;
541 struct Nand *mychip = &this->chips[to >> (this->chipshift)];
542
543 /* Don't allow write past end of device */
544 if (to >= this->totlen)
545 return -EINVAL;
546
547 #if 0
548 /* Don't allow a single write to cross a 512-byte block boundary */
549 if (to + len > ( (to | 0x1ff) + 1))
550 len = ((to | 0x1ff) + 1) - to;
551 #else
552 /* Don't allow writes which aren't exactly one block */
553 if (to & 0x1ff || len != 0x200)
554 return -EINVAL;
555 #endif
556
557 /* Find the chip which is to be used and select it */
558 if (this->curfloor != mychip->floor) {
559 DoC_SelectFloor(docptr, mychip->floor);
560 DoC_SelectChip(docptr, mychip->chip);
561 } else if (this->curchip != mychip->chip) {
562 DoC_SelectChip(docptr, mychip->chip);
563 }
564 this->curfloor = mychip->floor;
565 this->curchip = mychip->chip;
566
567 /* Reset the chip, see Software Requirement 11.4 item 1. */
568 DoC_Command(docptr, NAND_CMD_RESET, 0x00);
569 DoC_WaitReady(docptr);
570 /* Set device to main plane of flash */
571 DoC_Command(docptr, NAND_CMD_READ0, 0x00);
572
573 /* issue the Serial Data In command to initial the Page Program process */
574 DoC_Command(docptr, NAND_CMD_SEQIN, 0x00);
575 DoC_Address(docptr, 3, to, 0x00, 0x00);
576 DoC_WaitReady(docptr);
577
578 if (eccbuf) {
579 /* init the ECC engine, see Reed-Solomon EDC/ECC 11.1 .*/
580 WriteDOC (DOC_ECC_RESET, docptr, ECCConf);
581 WriteDOC (DOC_ECC_EN | DOC_ECC_RW, docptr, ECCConf);
582 } else {
583 /* disable the ECC engine */
584 WriteDOC (DOC_ECC_RESET, docptr, ECCConf);
585 WriteDOC (DOC_ECC_DIS, docptr, ECCConf);
586 }
587
588 /* Write the data via the internal pipeline through CDSN IO register,
589 see Pipelined Write Operations 11.2 */
590 #ifndef USE_MEMCPY
591 for (i = 0; i < len; i++) {
592 /* N.B. you have to increase the source address in this way or the
593 ECC logic will not work properly */
594 WriteDOC(buf[i], docptr, Mil_CDSN_IO + i);
595 }
596 #else
597 memcpy_toio(docptr + DoC_Mil_CDSN_IO, buf, len);
598 #endif
599 WriteDOC(0x00, docptr, WritePipeTerm);
600
601 if (eccbuf) {
602 /* Write ECC data to flash, the ECC info is generated by the DiskOnChip ECC logic
603 see Reed-Solomon EDC/ECC 11.1 */
604 WriteDOC(0, docptr, NOP);
605 WriteDOC(0, docptr, NOP);
606 WriteDOC(0, docptr, NOP);
607
608 /* Read the ECC data through the DiskOnChip ECC logic */
609 for (i = 0; i < 6; i++) {
610 eccbuf[i] = ReadDOC(docptr, ECCSyndrome0 + i);
611 }
612
613 /* ignore the ECC engine */
614 WriteDOC(DOC_ECC_DIS, docptr , ECCConf);
615
616 #ifndef USE_MEMCPY
617 /* Write the ECC data to flash */
618 for (i = 0; i < 6; i++) {
619 /* N.B. you have to increase the source address in this way or the
620 ECC logic will not work properly */
621 WriteDOC(eccbuf[i], docptr, Mil_CDSN_IO + i);
622 }
623 #else
624 memcpy_toio(docptr + DoC_Mil_CDSN_IO, eccbuf, 6);
625 #endif
626
627 /* write the block status BLOCK_USED (0x5555) at the end of ECC data
628 FIXME: this is only a hack for programming the IPL area for LinuxBIOS
629 and should be replace with proper codes in user space utilities */
630 WriteDOC(0x55, docptr, Mil_CDSN_IO);
631 WriteDOC(0x55, docptr, Mil_CDSN_IO + 1);
632
633 WriteDOC(0x00, docptr, WritePipeTerm);
634
635 #ifdef PSYCHO_DEBUG
636 printk("OOB data at %lx is %2.2X %2.2X %2.2X %2.2X %2.2X %2.2X\n",
637 (long) to, eccbuf[0], eccbuf[1], eccbuf[2], eccbuf[3],
638 eccbuf[4], eccbuf[5]);
639 #endif
640 }
641
642 /* Commit the Page Program command and wait for ready
643 see Software Requirement 11.4 item 1.*/
644 DoC_Command(docptr, NAND_CMD_PAGEPROG, 0x00);
645 DoC_WaitReady(docptr);
646
647 /* Read the status of the flash device through CDSN IO register
648 see Software Requirement 11.4 item 5.*/
649 DoC_Command(docptr, NAND_CMD_STATUS, CDSN_CTRL_WP);
650 dummy = ReadDOC(docptr, ReadPipeInit);
651 DoC_Delay(docptr, 2);
652 if (ReadDOC(docptr, Mil_CDSN_IO) & 1) {
653 printk("Error programming flash\n");
654 /* Error in programming
655 FIXME: implement Bad Block Replacement (in nftl.c ??) */
656 *retlen = 0;
657 ret = -EIO;
658 }
659 dummy = ReadDOC(docptr, LastDataRead);
660
661 /* Let the caller know we completed it */
662 *retlen = len;
663
664 return ret;
665 }
666
doc_read_oob(struct mtd_info * mtd,loff_t ofs,size_t len,size_t * retlen,u_char * buf)667 static int doc_read_oob(struct mtd_info *mtd, loff_t ofs, size_t len,
668 size_t *retlen, u_char *buf)
669 {
670 #ifndef USE_MEMCPY
671 int i;
672 #endif
673 volatile char dummy;
674 struct DiskOnChip *this = (struct DiskOnChip *)mtd->priv;
675 unsigned long docptr = this->virtadr;
676 struct Nand *mychip = &this->chips[ofs >> this->chipshift];
677
678 /* Find the chip which is to be used and select it */
679 if (this->curfloor != mychip->floor) {
680 DoC_SelectFloor(docptr, mychip->floor);
681 DoC_SelectChip(docptr, mychip->chip);
682 } else if (this->curchip != mychip->chip) {
683 DoC_SelectChip(docptr, mychip->chip);
684 }
685 this->curfloor = mychip->floor;
686 this->curchip = mychip->chip;
687
688 /* disable the ECC engine */
689 WriteDOC (DOC_ECC_RESET, docptr, ECCConf);
690 WriteDOC (DOC_ECC_DIS, docptr, ECCConf);
691
692 /* issue the Read2 command to set the pointer to the Spare Data Area.
693 Polling the Flash Ready bit after issue 3 bytes address in
694 Sequence Read Mode, see Software Requirement 11.4 item 1.*/
695 DoC_Command(docptr, NAND_CMD_READOOB, CDSN_CTRL_WP);
696 DoC_Address(docptr, 3, ofs, CDSN_CTRL_WP, 0x00);
697 DoC_WaitReady(docptr);
698
699 /* Read the data out via the internal pipeline through CDSN IO register,
700 see Pipelined Read Operations 11.3 */
701 dummy = ReadDOC(docptr, ReadPipeInit);
702 #ifndef USE_MEMCPY
703 for (i = 0; i < len-1; i++) {
704 /* N.B. you have to increase the source address in this way or the
705 ECC logic will not work properly */
706 buf[i] = ReadDOC(docptr, Mil_CDSN_IO + i);
707 }
708 #else
709 memcpy_fromio(buf, docptr + DoC_Mil_CDSN_IO, len - 1);
710 #endif
711 buf[len - 1] = ReadDOC(docptr, LastDataRead);
712
713 *retlen = len;
714
715 return 0;
716 }
717
doc_write_oob(struct mtd_info * mtd,loff_t ofs,size_t len,size_t * retlen,const u_char * buf)718 static int doc_write_oob(struct mtd_info *mtd, loff_t ofs, size_t len,
719 size_t *retlen, const u_char *buf)
720 {
721 #ifndef USE_MEMCPY
722 int i;
723 #endif
724 volatile char dummy;
725 int ret = 0;
726 struct DiskOnChip *this = (struct DiskOnChip *)mtd->priv;
727 unsigned long docptr = this->virtadr;
728 struct Nand *mychip = &this->chips[ofs >> this->chipshift];
729
730 /* Find the chip which is to be used and select it */
731 if (this->curfloor != mychip->floor) {
732 DoC_SelectFloor(docptr, mychip->floor);
733 DoC_SelectChip(docptr, mychip->chip);
734 } else if (this->curchip != mychip->chip) {
735 DoC_SelectChip(docptr, mychip->chip);
736 }
737 this->curfloor = mychip->floor;
738 this->curchip = mychip->chip;
739
740 /* disable the ECC engine */
741 WriteDOC (DOC_ECC_RESET, docptr, ECCConf);
742 WriteDOC (DOC_ECC_DIS, docptr, ECCConf);
743
744 /* Reset the chip, see Software Requirement 11.4 item 1. */
745 DoC_Command(docptr, NAND_CMD_RESET, CDSN_CTRL_WP);
746 DoC_WaitReady(docptr);
747 /* issue the Read2 command to set the pointer to the Spare Data Area. */
748 DoC_Command(docptr, NAND_CMD_READOOB, CDSN_CTRL_WP);
749
750 /* issue the Serial Data In command to initial the Page Program process */
751 DoC_Command(docptr, NAND_CMD_SEQIN, 0x00);
752 DoC_Address(docptr, 3, ofs, 0x00, 0x00);
753
754 /* Write the data via the internal pipeline through CDSN IO register,
755 see Pipelined Write Operations 11.2 */
756 #ifndef USE_MEMCPY
757 for (i = 0; i < len; i++) {
758 /* N.B. you have to increase the source address in this way or the
759 ECC logic will not work properly */
760 WriteDOC(buf[i], docptr, Mil_CDSN_IO + i);
761 }
762 #else
763 memcpy_toio(docptr + DoC_Mil_CDSN_IO, buf, len);
764 #endif
765 WriteDOC(0x00, docptr, WritePipeTerm);
766
767 /* Commit the Page Program command and wait for ready
768 see Software Requirement 11.4 item 1.*/
769 DoC_Command(docptr, NAND_CMD_PAGEPROG, 0x00);
770 DoC_WaitReady(docptr);
771
772 /* Read the status of the flash device through CDSN IO register
773 see Software Requirement 11.4 item 5.*/
774 DoC_Command(docptr, NAND_CMD_STATUS, 0x00);
775 dummy = ReadDOC(docptr, ReadPipeInit);
776 DoC_Delay(docptr, 2);
777 if (ReadDOC(docptr, Mil_CDSN_IO) & 1) {
778 printk("Error programming oob data\n");
779 /* FIXME: implement Bad Block Replacement (in nftl.c ??) */
780 *retlen = 0;
781 ret = -EIO;
782 }
783 dummy = ReadDOC(docptr, LastDataRead);
784
785 *retlen = len;
786
787 return ret;
788 }
789
doc_erase(struct mtd_info * mtd,struct erase_info * instr)790 int doc_erase (struct mtd_info *mtd, struct erase_info *instr)
791 {
792 volatile char dummy;
793 struct DiskOnChip *this = (struct DiskOnChip *)mtd->priv;
794 __u32 ofs = instr->addr;
795 __u32 len = instr->len;
796 unsigned long docptr = this->virtadr;
797 struct Nand *mychip = &this->chips[ofs >> this->chipshift];
798
799 if (len != mtd->erasesize)
800 printk(KERN_WARNING "Erase not right size (%x != %x)n",
801 len, mtd->erasesize);
802
803 /* Find the chip which is to be used and select it */
804 if (this->curfloor != mychip->floor) {
805 DoC_SelectFloor(docptr, mychip->floor);
806 DoC_SelectChip(docptr, mychip->chip);
807 } else if (this->curchip != mychip->chip) {
808 DoC_SelectChip(docptr, mychip->chip);
809 }
810 this->curfloor = mychip->floor;
811 this->curchip = mychip->chip;
812
813 instr->state = MTD_ERASE_PENDING;
814
815 /* issue the Erase Setup command */
816 DoC_Command(docptr, NAND_CMD_ERASE1, 0x00);
817 DoC_Address(docptr, 2, ofs, 0x00, 0x00);
818
819 /* Commit the Erase Start command and wait for ready
820 see Software Requirement 11.4 item 1.*/
821 DoC_Command(docptr, NAND_CMD_ERASE2, 0x00);
822 DoC_WaitReady(docptr);
823
824 instr->state = MTD_ERASING;
825
826 /* Read the status of the flash device through CDSN IO register
827 see Software Requirement 11.4 item 5.
828 FIXME: it seems that we are not wait long enough, some blocks are not
829 erased fully */
830 DoC_Command(docptr, NAND_CMD_STATUS, CDSN_CTRL_WP);
831 dummy = ReadDOC(docptr, ReadPipeInit);
832 DoC_Delay(docptr, 2);
833 if (ReadDOC(docptr, Mil_CDSN_IO) & 1) {
834 printk("Error Erasing at 0x%x\n", ofs);
835 /* There was an error
836 FIXME: implement Bad Block Replacement (in nftl.c ??) */
837 instr->state = MTD_ERASE_FAILED;
838 } else
839 instr->state = MTD_ERASE_DONE;
840 dummy = ReadDOC(docptr, LastDataRead);
841
842 if (instr->callback)
843 instr->callback(instr);
844
845 return 0;
846 }
847
848 /****************************************************************************
849 *
850 * Module stuff
851 *
852 ****************************************************************************/
853
init_doc2001(void)854 int __init init_doc2001(void)
855 {
856 inter_module_register(im_name, THIS_MODULE, &DoCMil_init);
857 return 0;
858 }
859
cleanup_doc2001(void)860 static void __exit cleanup_doc2001(void)
861 {
862 struct mtd_info *mtd;
863 struct DiskOnChip *this;
864
865 while ((mtd=docmillist)) {
866 this = (struct DiskOnChip *)mtd->priv;
867 docmillist = this->nextdoc;
868
869 del_mtd_device(mtd);
870
871 iounmap((void *)this->virtadr);
872 kfree(this->chips);
873 kfree(mtd);
874 }
875 inter_module_unregister(im_name);
876 }
877
878 module_exit(cleanup_doc2001);
879 module_init(init_doc2001);
880
881 MODULE_LICENSE("GPL");
882 MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org> et al.");
883 MODULE_DESCRIPTION("Alternative driver for DiskOnChip Millennium");
884