]> git.ipfire.org Git - people/ms/u-boot.git/blob - common/cmd_doc.c
USB Storage, add meaningful return value
[people/ms/u-boot.git] / common / cmd_doc.c
1 /*
2 * Driver for Disk-On-Chip 2000 and Millennium
3 * (c) 1999 Machine Vision Holdings, Inc.
4 * (c) 1999, 2000 David Woodhouse <dwmw2@infradead.org>
5 *
6 * $Id: doc2000.c,v 1.46 2001/10/02 15:05:13 dwmw2 Exp $
7 */
8
9 #include <common.h>
10 #include <config.h>
11 #include <command.h>
12 #include <malloc.h>
13 #include <asm/io.h>
14 #include <linux/mtd/nftl.h>
15 #include <linux/mtd/doc2000.h>
16
17 #ifdef CFG_DOC_SUPPORT_2000
18 #define DoC_is_2000(doc) (doc->ChipID == DOC_ChipID_Doc2k)
19 #else
20 #define DoC_is_2000(doc) (0)
21 #endif
22
23 #ifdef CFG_DOC_SUPPORT_MILLENNIUM
24 #define DoC_is_Millennium(doc) (doc->ChipID == DOC_ChipID_DocMil)
25 #else
26 #define DoC_is_Millennium(doc) (0)
27 #endif
28
29 /* CFG_DOC_PASSIVE_PROBE:
30 In order to ensure that the BIOS checksum is correct at boot time, and
31 hence that the onboard BIOS extension gets executed, the DiskOnChip
32 goes into reset mode when it is read sequentially: all registers
33 return 0xff until the chip is woken up again by writing to the
34 DOCControl register.
35
36 Unfortunately, this means that the probe for the DiskOnChip is unsafe,
37 because one of the first things it does is write to where it thinks
38 the DOCControl register should be - which may well be shared memory
39 for another device. I've had machines which lock up when this is
40 attempted. Hence the possibility to do a passive probe, which will fail
41 to detect a chip in reset mode, but is at least guaranteed not to lock
42 the machine.
43
44 If you have this problem, uncomment the following line:
45 #define CFG_DOC_PASSIVE_PROBE
46 */
47
48 #undef DOC_DEBUG
49 #undef ECC_DEBUG
50 #undef PSYCHO_DEBUG
51 #undef NFTL_DEBUG
52
53 static struct DiskOnChip doc_dev_desc[CFG_MAX_DOC_DEVICE];
54
55 /* Current DOC Device */
56 static int curr_device = -1;
57
58 /* Supported NAND flash devices */
59 static struct nand_flash_dev nand_flash_ids[] = {
60 {"Toshiba TC5816BDC", NAND_MFR_TOSHIBA, 0x64, 21, 1, 2, 0x1000, 0},
61 {"Toshiba TC5832DC", NAND_MFR_TOSHIBA, 0x6b, 22, 0, 2, 0x2000, 0},
62 {"Toshiba TH58V128DC", NAND_MFR_TOSHIBA, 0x73, 24, 0, 2, 0x4000, 0},
63 {"Toshiba TC58256FT/DC", NAND_MFR_TOSHIBA, 0x75, 25, 0, 2, 0x4000, 0},
64 {"Toshiba TH58512FT", NAND_MFR_TOSHIBA, 0x76, 26, 0, 3, 0x4000, 0},
65 {"Toshiba TC58V32DC", NAND_MFR_TOSHIBA, 0xe5, 22, 0, 2, 0x2000, 0},
66 {"Toshiba TC58V64AFT/DC", NAND_MFR_TOSHIBA, 0xe6, 23, 0, 2, 0x2000, 0},
67 {"Toshiba TC58V16BDC", NAND_MFR_TOSHIBA, 0xea, 21, 1, 2, 0x1000, 0},
68 {"Toshiba TH58100FT", NAND_MFR_TOSHIBA, 0x79, 27, 0, 3, 0x4000, 0},
69 {"Samsung KM29N16000", NAND_MFR_SAMSUNG, 0x64, 21, 1, 2, 0x1000, 0},
70 {"Samsung unknown 4Mb", NAND_MFR_SAMSUNG, 0x6b, 22, 0, 2, 0x2000, 0},
71 {"Samsung KM29U128T", NAND_MFR_SAMSUNG, 0x73, 24, 0, 2, 0x4000, 0},
72 {"Samsung KM29U256T", NAND_MFR_SAMSUNG, 0x75, 25, 0, 2, 0x4000, 0},
73 {"Samsung unknown 64Mb", NAND_MFR_SAMSUNG, 0x76, 26, 0, 3, 0x4000, 0},
74 {"Samsung KM29W32000", NAND_MFR_SAMSUNG, 0xe3, 22, 0, 2, 0x2000, 0},
75 {"Samsung unknown 4Mb", NAND_MFR_SAMSUNG, 0xe5, 22, 0, 2, 0x2000, 0},
76 {"Samsung KM29U64000", NAND_MFR_SAMSUNG, 0xe6, 23, 0, 2, 0x2000, 0},
77 {"Samsung KM29W16000", NAND_MFR_SAMSUNG, 0xea, 21, 1, 2, 0x1000, 0},
78 {"Samsung K9F5616Q0C", NAND_MFR_SAMSUNG, 0x45, 25, 0, 2, 0x4000, 1},
79 {"Samsung K9K1216Q0C", NAND_MFR_SAMSUNG, 0x46, 26, 0, 3, 0x4000, 1},
80 {"Samsung K9F1G08U0M", NAND_MFR_SAMSUNG, 0xf1, 27, 0, 2, 0, 0},
81 {NULL,}
82 };
83
84 /* ------------------------------------------------------------------------- */
85
86 int do_doc (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
87 {
88 int rcode = 0;
89
90 switch (argc) {
91 case 0:
92 case 1:
93 printf ("Usage:\n%s\n", cmdtp->usage);
94 return 1;
95 case 2:
96 if (strcmp(argv[1],"info") == 0) {
97 int i;
98
99 putc ('\n');
100
101 for (i=0; i<CFG_MAX_DOC_DEVICE; ++i) {
102 if(doc_dev_desc[i].ChipID == DOC_ChipID_UNKNOWN)
103 continue; /* list only known devices */
104 printf ("Device %d: ", i);
105 doc_print(&doc_dev_desc[i]);
106 }
107 return 0;
108
109 } else if (strcmp(argv[1],"device") == 0) {
110 if ((curr_device < 0) || (curr_device >= CFG_MAX_DOC_DEVICE)) {
111 puts ("\nno devices available\n");
112 return 1;
113 }
114 printf ("\nDevice %d: ", curr_device);
115 doc_print(&doc_dev_desc[curr_device]);
116 return 0;
117 }
118 printf ("Usage:\n%s\n", cmdtp->usage);
119 return 1;
120 case 3:
121 if (strcmp(argv[1],"device") == 0) {
122 int dev = (int)simple_strtoul(argv[2], NULL, 10);
123
124 printf ("\nDevice %d: ", dev);
125 if (dev >= CFG_MAX_DOC_DEVICE) {
126 puts ("unknown device\n");
127 return 1;
128 }
129 doc_print(&doc_dev_desc[dev]);
130 /*doc_print (dev);*/
131
132 if (doc_dev_desc[dev].ChipID == DOC_ChipID_UNKNOWN) {
133 return 1;
134 }
135
136 curr_device = dev;
137
138 puts ("... is now current device\n");
139
140 return 0;
141 }
142
143 printf ("Usage:\n%s\n", cmdtp->usage);
144 return 1;
145 default:
146 /* at least 4 args */
147
148 if (strcmp(argv[1],"read") == 0 || strcmp(argv[1],"write") == 0) {
149 ulong addr = simple_strtoul(argv[2], NULL, 16);
150 ulong off = simple_strtoul(argv[3], NULL, 16);
151 ulong size = simple_strtoul(argv[4], NULL, 16);
152 int cmd = (strcmp(argv[1],"read") == 0);
153 int ret, total;
154
155 printf ("\nDOC %s: device %d offset %ld, size %ld ... ",
156 cmd ? "read" : "write", curr_device, off, size);
157
158 ret = doc_rw(doc_dev_desc + curr_device, cmd, off, size,
159 (size_t *)&total, (u_char*)addr);
160
161 printf ("%d bytes %s: %s\n", total, cmd ? "read" : "write",
162 ret ? "ERROR" : "OK");
163
164 return ret;
165 } else if (strcmp(argv[1],"erase") == 0) {
166 ulong off = simple_strtoul(argv[2], NULL, 16);
167 ulong size = simple_strtoul(argv[3], NULL, 16);
168 int ret;
169
170 printf ("\nDOC erase: device %d offset %ld, size %ld ... ",
171 curr_device, off, size);
172
173 ret = doc_erase (doc_dev_desc + curr_device, off, size);
174
175 printf("%s\n", ret ? "ERROR" : "OK");
176
177 return ret;
178 } else {
179 printf ("Usage:\n%s\n", cmdtp->usage);
180 rcode = 1;
181 }
182
183 return rcode;
184 }
185 }
186 U_BOOT_CMD(
187 doc, 5, 1, do_doc,
188 "doc - Disk-On-Chip sub-system\n",
189 "info - show available DOC devices\n"
190 "doc device [dev] - show or set current device\n"
191 "doc read addr off size\n"
192 "doc write addr off size - read/write `size'"
193 " bytes starting at offset `off'\n"
194 " to/from memory address `addr'\n"
195 "doc erase off size - erase `size' bytes of DOC from offset `off'\n"
196 );
197
198 int do_docboot (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
199 {
200 char *boot_device = NULL;
201 char *ep;
202 int dev;
203 ulong cnt;
204 ulong addr;
205 ulong offset = 0;
206 image_header_t *hdr;
207 int rcode = 0;
208
209 show_boot_progress (34);
210 switch (argc) {
211 case 1:
212 addr = CFG_LOAD_ADDR;
213 boot_device = getenv ("bootdevice");
214 break;
215 case 2:
216 addr = simple_strtoul(argv[1], NULL, 16);
217 boot_device = getenv ("bootdevice");
218 break;
219 case 3:
220 addr = simple_strtoul(argv[1], NULL, 16);
221 boot_device = argv[2];
222 break;
223 case 4:
224 addr = simple_strtoul(argv[1], NULL, 16);
225 boot_device = argv[2];
226 offset = simple_strtoul(argv[3], NULL, 16);
227 break;
228 default:
229 printf ("Usage:\n%s\n", cmdtp->usage);
230 show_boot_progress (-35);
231 return 1;
232 }
233
234 show_boot_progress (35);
235 if (!boot_device) {
236 puts ("\n** No boot device **\n");
237 show_boot_progress (-36);
238 return 1;
239 }
240 show_boot_progress (36);
241
242 dev = simple_strtoul(boot_device, &ep, 16);
243
244 if ((dev >= CFG_MAX_DOC_DEVICE) ||
245 (doc_dev_desc[dev].ChipID == DOC_ChipID_UNKNOWN)) {
246 printf ("\n** Device %d not available\n", dev);
247 show_boot_progress (-37);
248 return 1;
249 }
250 show_boot_progress (37);
251
252 printf ("\nLoading from device %d: %s at 0x%lX (offset 0x%lX)\n",
253 dev, doc_dev_desc[dev].name, doc_dev_desc[dev].physadr,
254 offset);
255
256 if (doc_rw (doc_dev_desc + dev, 1, offset,
257 SECTORSIZE, NULL, (u_char *)addr)) {
258 printf ("** Read error on %d\n", dev);
259 show_boot_progress (-38);
260 return 1;
261 }
262 show_boot_progress (38);
263
264 hdr = (image_header_t *)addr;
265
266 if (hdr->ih_magic == IH_MAGIC) {
267
268 print_image_hdr (hdr);
269
270 cnt = (ntohl(hdr->ih_size) + sizeof(image_header_t));
271 cnt -= SECTORSIZE;
272 } else {
273 puts ("\n** Bad Magic Number **\n");
274 show_boot_progress (-39);
275 return 1;
276 }
277 show_boot_progress (39);
278
279 if (doc_rw (doc_dev_desc + dev, 1, offset + SECTORSIZE, cnt,
280 NULL, (u_char *)(addr+SECTORSIZE))) {
281 printf ("** Read error on %d\n", dev);
282 show_boot_progress (-40);
283 return 1;
284 }
285 show_boot_progress (40);
286
287 /* Loading ok, update default load address */
288
289 load_addr = addr;
290
291 /* Check if we should attempt an auto-start */
292 if (((ep = getenv("autostart")) != NULL) && (strcmp(ep,"yes") == 0)) {
293 char *local_args[2];
294 extern int do_bootm (cmd_tbl_t *, int, int, char *[]);
295
296 local_args[0] = argv[0];
297 local_args[1] = NULL;
298
299 printf ("Automatic boot of image at addr 0x%08lX ...\n", addr);
300
301 do_bootm (cmdtp, 0, 1, local_args);
302 rcode = 1;
303 }
304 return rcode;
305 }
306
307 U_BOOT_CMD(
308 docboot, 4, 1, do_docboot,
309 "docboot - boot from DOC device\n",
310 "loadAddr dev\n"
311 );
312
313 int doc_rw (struct DiskOnChip* this, int cmd,
314 loff_t from, size_t len,
315 size_t * retlen, u_char * buf)
316 {
317 int noecc, ret = 0, n, total = 0;
318 char eccbuf[6];
319
320 while(len) {
321 /* The ECC will not be calculated correctly if
322 less than 512 is written or read */
323 noecc = (from != (from | 0x1ff) + 1) || (len < 0x200);
324
325 if (cmd)
326 ret = doc_read_ecc(this, from, len,
327 (size_t *)&n, (u_char*)buf,
328 noecc ? (uchar *)NULL : (uchar *)eccbuf);
329 else
330 ret = doc_write_ecc(this, from, len,
331 (size_t *)&n, (u_char*)buf,
332 noecc ? (uchar *)NULL : (uchar *)eccbuf);
333
334 if (ret)
335 break;
336
337 from += n;
338 buf += n;
339 total += n;
340 len -= n;
341 }
342
343 if (retlen)
344 *retlen = total;
345
346 return ret;
347 }
348
349 void doc_print(struct DiskOnChip *this) {
350 printf("%s at 0x%lX,\n"
351 "\t %d chip%s %s, size %d MB, \n"
352 "\t total size %ld MB, sector size %ld kB\n",
353 this->name, this->physadr, this->numchips,
354 this->numchips>1 ? "s" : "", this->chips_name,
355 1 << (this->chipshift - 20),
356 this->totlen >> 20, this->erasesize >> 10);
357
358 if (this->nftl_found) {
359 struct NFTLrecord *nftl = &this->nftl;
360 unsigned long bin_size, flash_size;
361
362 bin_size = nftl->nb_boot_blocks * this->erasesize;
363 flash_size = (nftl->nb_blocks - nftl->nb_boot_blocks) * this->erasesize;
364
365 printf("\t NFTL boot record:\n"
366 "\t Binary partition: size %ld%s\n"
367 "\t Flash disk partition: size %ld%s, offset 0x%lx\n",
368 bin_size > (1 << 20) ? bin_size >> 20 : bin_size >> 10,
369 bin_size > (1 << 20) ? "MB" : "kB",
370 flash_size > (1 << 20) ? flash_size >> 20 : flash_size >> 10,
371 flash_size > (1 << 20) ? "MB" : "kB", bin_size);
372 } else {
373 puts ("\t No NFTL boot record found.\n");
374 }
375 }
376
377 /* ------------------------------------------------------------------------- */
378
379 /* This function is needed to avoid calls of the __ashrdi3 function. */
380 static int shr(int val, int shift) {
381 return val >> shift;
382 }
383
384 /* Perform the required delay cycles by reading from the appropriate register */
385 static void DoC_Delay(struct DiskOnChip *doc, unsigned short cycles)
386 {
387 volatile char dummy;
388 int i;
389
390 for (i = 0; i < cycles; i++) {
391 if (DoC_is_Millennium(doc))
392 dummy = ReadDOC(doc->virtadr, NOP);
393 else
394 dummy = ReadDOC(doc->virtadr, DOCStatus);
395 }
396
397 }
398
399 /* DOC_WaitReady: Wait for RDY line to be asserted by the flash chip */
400 static int _DoC_WaitReady(struct DiskOnChip *doc)
401 {
402 unsigned long docptr = doc->virtadr;
403 unsigned long start = get_timer(0);
404
405 #ifdef PSYCHO_DEBUG
406 puts ("_DoC_WaitReady called for out-of-line wait\n");
407 #endif
408
409 /* Out-of-line routine to wait for chip response */
410 while (!(ReadDOC(docptr, CDSNControl) & CDSN_CTRL_FR_B)) {
411 #ifdef CFG_DOC_SHORT_TIMEOUT
412 /* it seems that after a certain time the DoC deasserts
413 * the CDSN_CTRL_FR_B although it is not ready...
414 * using a short timout solve this (timer increments every ms) */
415 if (get_timer(start) > 10) {
416 return DOC_ETIMEOUT;
417 }
418 #else
419 if (get_timer(start) > 10 * 1000) {
420 puts ("_DoC_WaitReady timed out.\n");
421 return DOC_ETIMEOUT;
422 }
423 #endif
424 udelay(1);
425 }
426
427 return 0;
428 }
429
430 static int DoC_WaitReady(struct DiskOnChip *doc)
431 {
432 unsigned long docptr = doc->virtadr;
433 /* This is inline, to optimise the common case, where it's ready instantly */
434 int ret = 0;
435
436 /* 4 read form NOP register should be issued in prior to the read from CDSNControl
437 see Software Requirement 11.4 item 2. */
438 DoC_Delay(doc, 4);
439
440 if (!(ReadDOC(docptr, CDSNControl) & CDSN_CTRL_FR_B))
441 /* Call the out-of-line routine to wait */
442 ret = _DoC_WaitReady(doc);
443
444 /* issue 2 read from NOP register after reading from CDSNControl register
445 see Software Requirement 11.4 item 2. */
446 DoC_Delay(doc, 2);
447
448 return ret;
449 }
450
451 /* DoC_Command: Send a flash command to the flash chip through the CDSN Slow IO register to
452 bypass the internal pipeline. Each of 4 delay cycles (read from the NOP register) is
453 required after writing to CDSN Control register, see Software Requirement 11.4 item 3. */
454
455 static inline int DoC_Command(struct DiskOnChip *doc, unsigned char command,
456 unsigned char xtraflags)
457 {
458 unsigned long docptr = doc->virtadr;
459
460 if (DoC_is_2000(doc))
461 xtraflags |= CDSN_CTRL_FLASH_IO;
462
463 /* Assert the CLE (Command Latch Enable) line to the flash chip */
464 WriteDOC(xtraflags | CDSN_CTRL_CLE | CDSN_CTRL_CE, docptr, CDSNControl);
465 DoC_Delay(doc, 4); /* Software requirement 11.4.3 for Millennium */
466
467 if (DoC_is_Millennium(doc))
468 WriteDOC(command, docptr, CDSNSlowIO);
469
470 /* Send the command */
471 WriteDOC_(command, docptr, doc->ioreg);
472
473 /* Lower the CLE line */
474 WriteDOC(xtraflags | CDSN_CTRL_CE, docptr, CDSNControl);
475 DoC_Delay(doc, 4); /* Software requirement 11.4.3 for Millennium */
476
477 /* Wait for the chip to respond - Software requirement 11.4.1 (extended for any command) */
478 return DoC_WaitReady(doc);
479 }
480
481 /* DoC_Address: Set the current address for the flash chip through the CDSN Slow IO register to
482 bypass the internal pipeline. Each of 4 delay cycles (read from the NOP register) is
483 required after writing to CDSN Control register, see Software Requirement 11.4 item 3. */
484
485 static int DoC_Address(struct DiskOnChip *doc, int numbytes, unsigned long ofs,
486 unsigned char xtraflags1, unsigned char xtraflags2)
487 {
488 unsigned long docptr;
489 int i;
490
491 docptr = doc->virtadr;
492
493 if (DoC_is_2000(doc))
494 xtraflags1 |= CDSN_CTRL_FLASH_IO;
495
496 /* Assert the ALE (Address Latch Enable) line to the flash chip */
497 WriteDOC(xtraflags1 | CDSN_CTRL_ALE | CDSN_CTRL_CE, docptr, CDSNControl);
498
499 DoC_Delay(doc, 4); /* Software requirement 11.4.3 for Millennium */
500
501 /* Send the address */
502 /* Devices with 256-byte page are addressed as:
503 Column (bits 0-7), Page (bits 8-15, 16-23, 24-31)
504 * there is no device on the market with page256
505 and more than 24 bits.
506 Devices with 512-byte page are addressed as:
507 Column (bits 0-7), Page (bits 9-16, 17-24, 25-31)
508 * 25-31 is sent only if the chip support it.
509 * bit 8 changes the read command to be sent
510 (NAND_CMD_READ0 or NAND_CMD_READ1).
511 */
512
513 if (numbytes == ADDR_COLUMN || numbytes == ADDR_COLUMN_PAGE) {
514 if (DoC_is_Millennium(doc))
515 WriteDOC(ofs & 0xff, docptr, CDSNSlowIO);
516 WriteDOC_(ofs & 0xff, docptr, doc->ioreg);
517 }
518
519 if (doc->page256) {
520 ofs = ofs >> 8;
521 } else {
522 ofs = ofs >> 9;
523 }
524
525 if (numbytes == ADDR_PAGE || numbytes == ADDR_COLUMN_PAGE) {
526 for (i = 0; i < doc->pageadrlen; i++, ofs = ofs >> 8) {
527 if (DoC_is_Millennium(doc))
528 WriteDOC(ofs & 0xff, docptr, CDSNSlowIO);
529 WriteDOC_(ofs & 0xff, docptr, doc->ioreg);
530 }
531 }
532
533 DoC_Delay(doc, 2); /* Needed for some slow flash chips. mf. */
534
535 /* FIXME: The SlowIO's for millennium could be replaced by
536 a single WritePipeTerm here. mf. */
537
538 /* Lower the ALE line */
539 WriteDOC(xtraflags1 | xtraflags2 | CDSN_CTRL_CE, docptr,
540 CDSNControl);
541
542 DoC_Delay(doc, 4); /* Software requirement 11.4.3 for Millennium */
543
544 /* Wait for the chip to respond - Software requirement 11.4.1 */
545 return DoC_WaitReady(doc);
546 }
547
548 /* Read a buffer from DoC, taking care of Millennium oddities */
549 static void DoC_ReadBuf(struct DiskOnChip *doc, u_char * buf, int len)
550 {
551 volatile int dummy;
552 int modulus = 0xffff;
553 unsigned long docptr;
554 int i;
555
556 docptr = doc->virtadr;
557
558 if (len <= 0)
559 return;
560
561 if (DoC_is_Millennium(doc)) {
562 /* Read the data via the internal pipeline through CDSN IO register,
563 see Pipelined Read Operations 11.3 */
564 dummy = ReadDOC(docptr, ReadPipeInit);
565
566 /* Millennium should use the LastDataRead register - Pipeline Reads */
567 len--;
568
569 /* This is needed for correctly ECC calculation */
570 modulus = 0xff;
571 }
572
573 for (i = 0; i < len; i++)
574 buf[i] = ReadDOC_(docptr, doc->ioreg + (i & modulus));
575
576 if (DoC_is_Millennium(doc)) {
577 buf[i] = ReadDOC(docptr, LastDataRead);
578 }
579 }
580
581 /* Write a buffer to DoC, taking care of Millennium oddities */
582 static void DoC_WriteBuf(struct DiskOnChip *doc, const u_char * buf, int len)
583 {
584 unsigned long docptr;
585 int i;
586
587 docptr = doc->virtadr;
588
589 if (len <= 0)
590 return;
591
592 for (i = 0; i < len; i++)
593 WriteDOC_(buf[i], docptr, doc->ioreg + i);
594
595 if (DoC_is_Millennium(doc)) {
596 WriteDOC(0x00, docptr, WritePipeTerm);
597 }
598 }
599
600
601 /* DoC_SelectChip: Select a given flash chip within the current floor */
602
603 static inline int DoC_SelectChip(struct DiskOnChip *doc, int chip)
604 {
605 unsigned long docptr = doc->virtadr;
606
607 /* Software requirement 11.4.4 before writing DeviceSelect */
608 /* Deassert the CE line to eliminate glitches on the FCE# outputs */
609 WriteDOC(CDSN_CTRL_WP, docptr, CDSNControl);
610 DoC_Delay(doc, 4); /* Software requirement 11.4.3 for Millennium */
611
612 /* Select the individual flash chip requested */
613 WriteDOC(chip, docptr, CDSNDeviceSelect);
614 DoC_Delay(doc, 4);
615
616 /* Reassert the CE line */
617 WriteDOC(CDSN_CTRL_CE | CDSN_CTRL_FLASH_IO | CDSN_CTRL_WP, docptr,
618 CDSNControl);
619 DoC_Delay(doc, 4); /* Software requirement 11.4.3 for Millennium */
620
621 /* Wait for it to be ready */
622 return DoC_WaitReady(doc);
623 }
624
625 /* DoC_SelectFloor: Select a given floor (bank of flash chips) */
626
627 static inline int DoC_SelectFloor(struct DiskOnChip *doc, int floor)
628 {
629 unsigned long docptr = doc->virtadr;
630
631 /* Select the floor (bank) of chips required */
632 WriteDOC(floor, docptr, FloorSelect);
633
634 /* Wait for the chip to be ready */
635 return DoC_WaitReady(doc);
636 }
637
638 /* DoC_IdentChip: Identify a given NAND chip given {floor,chip} */
639
640 static int DoC_IdentChip(struct DiskOnChip *doc, int floor, int chip)
641 {
642 int mfr, id, i;
643 volatile char dummy;
644
645 /* Page in the required floor/chip */
646 DoC_SelectFloor(doc, floor);
647 DoC_SelectChip(doc, chip);
648
649 /* Reset the chip */
650 if (DoC_Command(doc, NAND_CMD_RESET, CDSN_CTRL_WP)) {
651 #ifdef DOC_DEBUG
652 printf("DoC_Command (reset) for %d,%d returned true\n",
653 floor, chip);
654 #endif
655 return 0;
656 }
657
658
659 /* Read the NAND chip ID: 1. Send ReadID command */
660 if (DoC_Command(doc, NAND_CMD_READID, CDSN_CTRL_WP)) {
661 #ifdef DOC_DEBUG
662 printf("DoC_Command (ReadID) for %d,%d returned true\n",
663 floor, chip);
664 #endif
665 return 0;
666 }
667
668 /* Read the NAND chip ID: 2. Send address byte zero */
669 DoC_Address(doc, ADDR_COLUMN, 0, CDSN_CTRL_WP, 0);
670
671 /* Read the manufacturer and device id codes from the device */
672
673 /* CDSN Slow IO register see Software Requirement 11.4 item 5. */
674 dummy = ReadDOC(doc->virtadr, CDSNSlowIO);
675 DoC_Delay(doc, 2);
676 mfr = ReadDOC_(doc->virtadr, doc->ioreg);
677
678 /* CDSN Slow IO register see Software Requirement 11.4 item 5. */
679 dummy = ReadDOC(doc->virtadr, CDSNSlowIO);
680 DoC_Delay(doc, 2);
681 id = ReadDOC_(doc->virtadr, doc->ioreg);
682
683 /* No response - return failure */
684 if (mfr == 0xff || mfr == 0)
685 return 0;
686
687 /* Check it's the same as the first chip we identified.
688 * M-Systems say that any given DiskOnChip device should only
689 * contain _one_ type of flash part, although that's not a
690 * hardware restriction. */
691 if (doc->mfr) {
692 if (doc->mfr == mfr && doc->id == id)
693 return 1; /* This is another the same the first */
694 else
695 printf("Flash chip at floor %d, chip %d is different:\n",
696 floor, chip);
697 }
698
699 /* Print and store the manufacturer and ID codes. */
700 for (i = 0; nand_flash_ids[i].name != NULL; i++) {
701 if (mfr == nand_flash_ids[i].manufacture_id &&
702 id == nand_flash_ids[i].model_id) {
703 #ifdef DOC_DEBUG
704 printf("Flash chip found: Manufacturer ID: %2.2X, "
705 "Chip ID: %2.2X (%s)\n", mfr, id,
706 nand_flash_ids[i].name);
707 #endif
708 if (!doc->mfr) {
709 doc->mfr = mfr;
710 doc->id = id;
711 doc->chipshift =
712 nand_flash_ids[i].chipshift;
713 doc->page256 = nand_flash_ids[i].page256;
714 doc->pageadrlen =
715 nand_flash_ids[i].pageadrlen;
716 doc->erasesize =
717 nand_flash_ids[i].erasesize;
718 doc->chips_name =
719 nand_flash_ids[i].name;
720 return 1;
721 }
722 return 0;
723 }
724 }
725
726
727 #ifdef DOC_DEBUG
728 /* We haven't fully identified the chip. Print as much as we know. */
729 printf("Unknown flash chip found: %2.2X %2.2X\n",
730 id, mfr);
731 #endif
732
733 return 0;
734 }
735
736 /* DoC_ScanChips: Find all NAND chips present in a DiskOnChip, and identify them */
737
738 static void DoC_ScanChips(struct DiskOnChip *this)
739 {
740 int floor, chip;
741 int numchips[MAX_FLOORS];
742 int maxchips = MAX_CHIPS;
743 int ret = 1;
744
745 this->numchips = 0;
746 this->mfr = 0;
747 this->id = 0;
748
749 if (DoC_is_Millennium(this))
750 maxchips = MAX_CHIPS_MIL;
751
752 /* For each floor, find the number of valid chips it contains */
753 for (floor = 0; floor < MAX_FLOORS; floor++) {
754 ret = 1;
755 numchips[floor] = 0;
756 for (chip = 0; chip < maxchips && ret != 0; chip++) {
757
758 ret = DoC_IdentChip(this, floor, chip);
759 if (ret) {
760 numchips[floor]++;
761 this->numchips++;
762 }
763 }
764 }
765
766 /* If there are none at all that we recognise, bail */
767 if (!this->numchips) {
768 puts ("No flash chips recognised.\n");
769 return;
770 }
771
772 /* Allocate an array to hold the information for each chip */
773 this->chips = malloc(sizeof(struct Nand) * this->numchips);
774 if (!this->chips) {
775 puts ("No memory for allocating chip info structures\n");
776 return;
777 }
778
779 ret = 0;
780
781 /* Fill out the chip array with {floor, chipno} for each
782 * detected chip in the device. */
783 for (floor = 0; floor < MAX_FLOORS; floor++) {
784 for (chip = 0; chip < numchips[floor]; chip++) {
785 this->chips[ret].floor = floor;
786 this->chips[ret].chip = chip;
787 this->chips[ret].curadr = 0;
788 this->chips[ret].curmode = 0x50;
789 ret++;
790 }
791 }
792
793 /* Calculate and print the total size of the device */
794 this->totlen = this->numchips * (1 << this->chipshift);
795
796 #ifdef DOC_DEBUG
797 printf("%d flash chips found. Total DiskOnChip size: %ld MB\n",
798 this->numchips, this->totlen >> 20);
799 #endif
800 }
801
802 /* find_boot_record: Find the NFTL Media Header and its Spare copy which contains the
803 * various device information of the NFTL partition and Bad Unit Table. Update
804 * the ReplUnitTable[] table accroding to the Bad Unit Table. ReplUnitTable[]
805 * is used for management of Erase Unit in other routines in nftl.c and nftlmount.c
806 */
807 static int find_boot_record(struct NFTLrecord *nftl)
808 {
809 struct nftl_uci1 h1;
810 struct nftl_oob oob;
811 unsigned int block, boot_record_count = 0;
812 int retlen;
813 u8 buf[SECTORSIZE];
814 struct NFTLMediaHeader *mh = &nftl->MediaHdr;
815 unsigned int i;
816
817 nftl->MediaUnit = BLOCK_NIL;
818 nftl->SpareMediaUnit = BLOCK_NIL;
819
820 /* search for a valid boot record */
821 for (block = 0; block < nftl->nb_blocks; block++) {
822 int ret;
823
824 /* Check for ANAND header first. Then can whinge if it's found but later
825 checks fail */
826 if ((ret = doc_read_ecc(nftl->mtd, block * nftl->EraseSize, SECTORSIZE,
827 (size_t *)&retlen, buf, NULL))) {
828 static int warncount = 5;
829
830 if (warncount) {
831 printf("Block read at 0x%x failed\n", block * nftl->EraseSize);
832 if (!--warncount)
833 puts ("Further failures for this block will not be printed\n");
834 }
835 continue;
836 }
837
838 if (retlen < 6 || memcmp(buf, "ANAND", 6)) {
839 /* ANAND\0 not found. Continue */
840 #ifdef PSYCHO_DEBUG
841 printf("ANAND header not found at 0x%x\n", block * nftl->EraseSize);
842 #endif
843 continue;
844 }
845
846 #ifdef NFTL_DEBUG
847 printf("ANAND header found at 0x%x\n", block * nftl->EraseSize);
848 #endif
849
850 /* To be safer with BIOS, also use erase mark as discriminant */
851 if ((ret = doc_read_oob(nftl->mtd, block * nftl->EraseSize + SECTORSIZE + 8,
852 8, (size_t *)&retlen, (uchar *)&h1) < 0)) {
853 #ifdef NFTL_DEBUG
854 printf("ANAND header found at 0x%x, but OOB data read failed\n",
855 block * nftl->EraseSize);
856 #endif
857 continue;
858 }
859
860 /* OK, we like it. */
861
862 if (boot_record_count) {
863 /* We've already processed one. So we just check if
864 this one is the same as the first one we found */
865 if (memcmp(mh, buf, sizeof(struct NFTLMediaHeader))) {
866 #ifdef NFTL_DEBUG
867 printf("NFTL Media Headers at 0x%x and 0x%x disagree.\n",
868 nftl->MediaUnit * nftl->EraseSize, block * nftl->EraseSize);
869 #endif
870 /* if (debug) Print both side by side */
871 return -1;
872 }
873 if (boot_record_count == 1)
874 nftl->SpareMediaUnit = block;
875
876 boot_record_count++;
877 continue;
878 }
879
880 /* This is the first we've seen. Copy the media header structure into place */
881 memcpy(mh, buf, sizeof(struct NFTLMediaHeader));
882
883 /* Do some sanity checks on it */
884 if (mh->UnitSizeFactor == 0) {
885 #ifdef NFTL_DEBUG
886 puts ("UnitSizeFactor 0x00 detected.\n"
887 "This violates the spec but we think we know what it means...\n");
888 #endif
889 } else if (mh->UnitSizeFactor != 0xff) {
890 printf ("Sorry, we don't support UnitSizeFactor "
891 "of != 1 yet.\n");
892 return -1;
893 }
894
895 nftl->nb_boot_blocks = le16_to_cpu(mh->FirstPhysicalEUN);
896 if ((nftl->nb_boot_blocks + 2) >= nftl->nb_blocks) {
897 printf ("NFTL Media Header sanity check failed:\n"
898 "nb_boot_blocks (%d) + 2 > nb_blocks (%d)\n",
899 nftl->nb_boot_blocks, nftl->nb_blocks);
900 return -1;
901 }
902
903 nftl->numvunits = le32_to_cpu(mh->FormattedSize) / nftl->EraseSize;
904 if (nftl->numvunits > (nftl->nb_blocks - nftl->nb_boot_blocks - 2)) {
905 printf ("NFTL Media Header sanity check failed:\n"
906 "numvunits (%d) > nb_blocks (%d) - nb_boot_blocks(%d) - 2\n",
907 nftl->numvunits,
908 nftl->nb_blocks,
909 nftl->nb_boot_blocks);
910 return -1;
911 }
912
913 nftl->nr_sects = nftl->numvunits * (nftl->EraseSize / SECTORSIZE);
914
915 /* If we're not using the last sectors in the device for some reason,
916 reduce nb_blocks accordingly so we forget they're there */
917 nftl->nb_blocks = le16_to_cpu(mh->NumEraseUnits) + le16_to_cpu(mh->FirstPhysicalEUN);
918
919 /* read the Bad Erase Unit Table and modify ReplUnitTable[] accordingly */
920 for (i = 0; i < nftl->nb_blocks; i++) {
921 if ((i & (SECTORSIZE - 1)) == 0) {
922 /* read one sector for every SECTORSIZE of blocks */
923 if ((ret = doc_read_ecc(nftl->mtd, block * nftl->EraseSize +
924 i + SECTORSIZE, SECTORSIZE,
925 (size_t *)&retlen, buf, (uchar *)&oob)) < 0) {
926 puts ("Read of bad sector table failed\n");
927 return -1;
928 }
929 }
930 /* mark the Bad Erase Unit as RESERVED in ReplUnitTable */
931 if (buf[i & (SECTORSIZE - 1)] != 0xff)
932 nftl->ReplUnitTable[i] = BLOCK_RESERVED;
933 }
934
935 nftl->MediaUnit = block;
936 boot_record_count++;
937
938 } /* foreach (block) */
939
940 return boot_record_count?0:-1;
941 }
942
943 /* This routine is made available to other mtd code via
944 * inter_module_register. It must only be accessed through
945 * inter_module_get which will bump the use count of this module. The
946 * addresses passed back in mtd are valid as long as the use count of
947 * this module is non-zero, i.e. between inter_module_get and
948 * inter_module_put. Keith Owens <kaos@ocs.com.au> 29 Oct 2000.
949 */
950 static void DoC2k_init(struct DiskOnChip* this)
951 {
952 struct NFTLrecord *nftl;
953
954 switch (this->ChipID) {
955 case DOC_ChipID_Doc2k:
956 this->name = "DiskOnChip 2000";
957 this->ioreg = DoC_2k_CDSN_IO;
958 break;
959 case DOC_ChipID_DocMil:
960 this->name = "DiskOnChip Millennium";
961 this->ioreg = DoC_Mil_CDSN_IO;
962 break;
963 }
964
965 #ifdef DOC_DEBUG
966 printf("%s found at address 0x%lX\n", this->name,
967 this->physadr);
968 #endif
969
970 this->totlen = 0;
971 this->numchips = 0;
972
973 this->curfloor = -1;
974 this->curchip = -1;
975
976 /* Ident all the chips present. */
977 DoC_ScanChips(this);
978 if ((!this->numchips) || (!this->chips))
979 return;
980
981 nftl = &this->nftl;
982
983 /* Get physical parameters */
984 nftl->EraseSize = this->erasesize;
985 nftl->nb_blocks = this->totlen / this->erasesize;
986 nftl->mtd = this;
987
988 if (find_boot_record(nftl) != 0)
989 this->nftl_found = 0;
990 else
991 this->nftl_found = 1;
992
993 printf("%s @ 0x%lX, %ld MB\n", this->name, this->physadr, this->totlen >> 20);
994 }
995
996 int doc_read_ecc(struct DiskOnChip* this, loff_t from, size_t len,
997 size_t * retlen, u_char * buf, u_char * eccbuf)
998 {
999 unsigned long docptr;
1000 struct Nand *mychip;
1001 unsigned char syndrome[6];
1002 volatile char dummy;
1003 int i, len256 = 0, ret=0;
1004
1005 docptr = this->virtadr;
1006
1007 /* Don't allow read past end of device */
1008 if (from >= this->totlen) {
1009 puts ("Out of flash\n");
1010 return DOC_EINVAL;
1011 }
1012
1013 /* Don't allow a single read to cross a 512-byte block boundary */
1014 if (from + len > ((from | 0x1ff) + 1))
1015 len = ((from | 0x1ff) + 1) - from;
1016
1017 /* The ECC will not be calculated correctly if less than 512 is read */
1018 if (len != 0x200 && eccbuf)
1019 printf("ECC needs a full sector read (adr: %lx size %lx)\n",
1020 (long) from, (long) len);
1021
1022 #ifdef PSYCHO_DEBUG
1023 printf("DoC_Read (adr: %lx size %lx)\n", (long) from, (long) len);
1024 #endif
1025
1026 /* Find the chip which is to be used and select it */
1027 mychip = &this->chips[shr(from, this->chipshift)];
1028
1029 if (this->curfloor != mychip->floor) {
1030 DoC_SelectFloor(this, mychip->floor);
1031 DoC_SelectChip(this, mychip->chip);
1032 } else if (this->curchip != mychip->chip) {
1033 DoC_SelectChip(this, mychip->chip);
1034 }
1035
1036 this->curfloor = mychip->floor;
1037 this->curchip = mychip->chip;
1038
1039 DoC_Command(this,
1040 (!this->page256
1041 && (from & 0x100)) ? NAND_CMD_READ1 : NAND_CMD_READ0,
1042 CDSN_CTRL_WP);
1043 DoC_Address(this, ADDR_COLUMN_PAGE, from, CDSN_CTRL_WP,
1044 CDSN_CTRL_ECC_IO);
1045
1046 if (eccbuf) {
1047 /* Prime the ECC engine */
1048 WriteDOC(DOC_ECC_RESET, docptr, ECCConf);
1049 WriteDOC(DOC_ECC_EN, docptr, ECCConf);
1050 } else {
1051 /* disable the ECC engine */
1052 WriteDOC(DOC_ECC_RESET, docptr, ECCConf);
1053 WriteDOC(DOC_ECC_DIS, docptr, ECCConf);
1054 }
1055
1056 /* treat crossing 256-byte sector for 2M x 8bits devices */
1057 if (this->page256 && from + len > (from | 0xff) + 1) {
1058 len256 = (from | 0xff) + 1 - from;
1059 DoC_ReadBuf(this, buf, len256);
1060
1061 DoC_Command(this, NAND_CMD_READ0, CDSN_CTRL_WP);
1062 DoC_Address(this, ADDR_COLUMN_PAGE, from + len256,
1063 CDSN_CTRL_WP, CDSN_CTRL_ECC_IO);
1064 }
1065
1066 DoC_ReadBuf(this, &buf[len256], len - len256);
1067
1068 /* Let the caller know we completed it */
1069 *retlen = len;
1070
1071 if (eccbuf) {
1072 /* Read the ECC data through the DiskOnChip ECC logic */
1073 /* Note: this will work even with 2M x 8bit devices as */
1074 /* they have 8 bytes of OOB per 256 page. mf. */
1075 DoC_ReadBuf(this, eccbuf, 6);
1076
1077 /* Flush the pipeline */
1078 if (DoC_is_Millennium(this)) {
1079 dummy = ReadDOC(docptr, ECCConf);
1080 dummy = ReadDOC(docptr, ECCConf);
1081 i = ReadDOC(docptr, ECCConf);
1082 } else {
1083 dummy = ReadDOC(docptr, 2k_ECCStatus);
1084 dummy = ReadDOC(docptr, 2k_ECCStatus);
1085 i = ReadDOC(docptr, 2k_ECCStatus);
1086 }
1087
1088 /* Check the ECC Status */
1089 if (i & 0x80) {
1090 int nb_errors;
1091 /* There was an ECC error */
1092 #ifdef ECC_DEBUG
1093 printf("DiskOnChip ECC Error: Read at %lx\n", (long)from);
1094 #endif
1095 /* Read the ECC syndrom through the DiskOnChip ECC logic.
1096 These syndrome will be all ZERO when there is no error */
1097 for (i = 0; i < 6; i++) {
1098 syndrome[i] =
1099 ReadDOC(docptr, ECCSyndrome0 + i);
1100 }
1101 nb_errors = doc_decode_ecc(buf, syndrome);
1102
1103 #ifdef ECC_DEBUG
1104 printf("Errors corrected: %x\n", nb_errors);
1105 #endif
1106 if (nb_errors < 0) {
1107 /* We return error, but have actually done the read. Not that
1108 this can be told to user-space, via sys_read(), but at least
1109 MTD-aware stuff can know about it by checking *retlen */
1110 printf("ECC Errors at %lx\n", (long)from);
1111 ret = DOC_EECC;
1112 }
1113 }
1114
1115 #ifdef PSYCHO_DEBUG
1116 printf("ECC DATA at %lxB: %2.2X %2.2X %2.2X %2.2X %2.2X %2.2X\n",
1117 (long)from, eccbuf[0], eccbuf[1], eccbuf[2],
1118 eccbuf[3], eccbuf[4], eccbuf[5]);
1119 #endif
1120
1121 /* disable the ECC engine */
1122 WriteDOC(DOC_ECC_DIS, docptr , ECCConf);
1123 }
1124
1125 /* according to 11.4.1, we need to wait for the busy line
1126 * drop if we read to the end of the page. */
1127 if(0 == ((from + *retlen) & 0x1ff))
1128 {
1129 DoC_WaitReady(this);
1130 }
1131
1132 return ret;
1133 }
1134
1135 int doc_write_ecc(struct DiskOnChip* this, loff_t to, size_t len,
1136 size_t * retlen, const u_char * buf,
1137 u_char * eccbuf)
1138 {
1139 int di; /* Yes, DI is a hangover from when I was disassembling the binary driver */
1140 unsigned long docptr;
1141 volatile char dummy;
1142 int len256 = 0;
1143 struct Nand *mychip;
1144
1145 docptr = this->virtadr;
1146
1147 /* Don't allow write past end of device */
1148 if (to >= this->totlen) {
1149 puts ("Out of flash\n");
1150 return DOC_EINVAL;
1151 }
1152
1153 /* Don't allow a single write to cross a 512-byte block boundary */
1154 if (to + len > ((to | 0x1ff) + 1))
1155 len = ((to | 0x1ff) + 1) - to;
1156
1157 /* The ECC will not be calculated correctly if less than 512 is written */
1158 if (len != 0x200 && eccbuf)
1159 printf("ECC needs a full sector write (adr: %lx size %lx)\n",
1160 (long) to, (long) len);
1161
1162 /* printf("DoC_Write (adr: %lx size %lx)\n", (long) to, (long) len); */
1163
1164 /* Find the chip which is to be used and select it */
1165 mychip = &this->chips[shr(to, this->chipshift)];
1166
1167 if (this->curfloor != mychip->floor) {
1168 DoC_SelectFloor(this, mychip->floor);
1169 DoC_SelectChip(this, mychip->chip);
1170 } else if (this->curchip != mychip->chip) {
1171 DoC_SelectChip(this, mychip->chip);
1172 }
1173
1174 this->curfloor = mychip->floor;
1175 this->curchip = mychip->chip;
1176
1177 /* Set device to main plane of flash */
1178 DoC_Command(this, NAND_CMD_RESET, CDSN_CTRL_WP);
1179 DoC_Command(this,
1180 (!this->page256
1181 && (to & 0x100)) ? NAND_CMD_READ1 : NAND_CMD_READ0,
1182 CDSN_CTRL_WP);
1183
1184 DoC_Command(this, NAND_CMD_SEQIN, 0);
1185 DoC_Address(this, ADDR_COLUMN_PAGE, to, 0, CDSN_CTRL_ECC_IO);
1186
1187 if (eccbuf) {
1188 /* Prime the ECC engine */
1189 WriteDOC(DOC_ECC_RESET, docptr, ECCConf);
1190 WriteDOC(DOC_ECC_EN | DOC_ECC_RW, docptr, ECCConf);
1191 } else {
1192 /* disable the ECC engine */
1193 WriteDOC(DOC_ECC_RESET, docptr, ECCConf);
1194 WriteDOC(DOC_ECC_DIS, docptr, ECCConf);
1195 }
1196
1197 /* treat crossing 256-byte sector for 2M x 8bits devices */
1198 if (this->page256 && to + len > (to | 0xff) + 1) {
1199 len256 = (to | 0xff) + 1 - to;
1200 DoC_WriteBuf(this, buf, len256);
1201
1202 DoC_Command(this, NAND_CMD_PAGEPROG, 0);
1203
1204 DoC_Command(this, NAND_CMD_STATUS, CDSN_CTRL_WP);
1205 /* There's an implicit DoC_WaitReady() in DoC_Command */
1206
1207 dummy = ReadDOC(docptr, CDSNSlowIO);
1208 DoC_Delay(this, 2);
1209
1210 if (ReadDOC_(docptr, this->ioreg) & 1) {
1211 puts ("Error programming flash\n");
1212 /* Error in programming */
1213 *retlen = 0;
1214 return DOC_EIO;
1215 }
1216
1217 DoC_Command(this, NAND_CMD_SEQIN, 0);
1218 DoC_Address(this, ADDR_COLUMN_PAGE, to + len256, 0,
1219 CDSN_CTRL_ECC_IO);
1220 }
1221
1222 DoC_WriteBuf(this, &buf[len256], len - len256);
1223
1224 if (eccbuf) {
1225 WriteDOC(CDSN_CTRL_ECC_IO | CDSN_CTRL_CE, docptr,
1226 CDSNControl);
1227
1228 if (DoC_is_Millennium(this)) {
1229 WriteDOC(0, docptr, NOP);
1230 WriteDOC(0, docptr, NOP);
1231 WriteDOC(0, docptr, NOP);
1232 } else {
1233 WriteDOC_(0, docptr, this->ioreg);
1234 WriteDOC_(0, docptr, this->ioreg);
1235 WriteDOC_(0, docptr, this->ioreg);
1236 }
1237
1238 /* Read the ECC data through the DiskOnChip ECC logic */
1239 for (di = 0; di < 6; di++) {
1240 eccbuf[di] = ReadDOC(docptr, ECCSyndrome0 + di);
1241 }
1242
1243 /* Reset the ECC engine */
1244 WriteDOC(DOC_ECC_DIS, docptr, ECCConf);
1245
1246 #ifdef PSYCHO_DEBUG
1247 printf
1248 ("OOB data at %lx is %2.2X %2.2X %2.2X %2.2X %2.2X %2.2X\n",
1249 (long) to, eccbuf[0], eccbuf[1], eccbuf[2], eccbuf[3],
1250 eccbuf[4], eccbuf[5]);
1251 #endif
1252 }
1253
1254 DoC_Command(this, NAND_CMD_PAGEPROG, 0);
1255
1256 DoC_Command(this, NAND_CMD_STATUS, CDSN_CTRL_WP);
1257 /* There's an implicit DoC_WaitReady() in DoC_Command */
1258
1259 dummy = ReadDOC(docptr, CDSNSlowIO);
1260 DoC_Delay(this, 2);
1261
1262 if (ReadDOC_(docptr, this->ioreg) & 1) {
1263 puts ("Error programming flash\n");
1264 /* Error in programming */
1265 *retlen = 0;
1266 return DOC_EIO;
1267 }
1268
1269 /* Let the caller know we completed it */
1270 *retlen = len;
1271
1272 if (eccbuf) {
1273 unsigned char x[8];
1274 size_t dummy;
1275 int ret;
1276
1277 /* Write the ECC data to flash */
1278 for (di=0; di<6; di++)
1279 x[di] = eccbuf[di];
1280
1281 x[6]=0x55;
1282 x[7]=0x55;
1283
1284 ret = doc_write_oob(this, to, 8, &dummy, x);
1285 return ret;
1286 }
1287 return 0;
1288 }
1289
1290 int doc_read_oob(struct DiskOnChip* this, loff_t ofs, size_t len,
1291 size_t * retlen, u_char * buf)
1292 {
1293 int len256 = 0, ret;
1294 unsigned long docptr;
1295 struct Nand *mychip;
1296
1297 docptr = this->virtadr;
1298
1299 mychip = &this->chips[shr(ofs, this->chipshift)];
1300
1301 if (this->curfloor != mychip->floor) {
1302 DoC_SelectFloor(this, mychip->floor);
1303 DoC_SelectChip(this, mychip->chip);
1304 } else if (this->curchip != mychip->chip) {
1305 DoC_SelectChip(this, mychip->chip);
1306 }
1307 this->curfloor = mychip->floor;
1308 this->curchip = mychip->chip;
1309
1310 /* update address for 2M x 8bit devices. OOB starts on the second */
1311 /* page to maintain compatibility with doc_read_ecc. */
1312 if (this->page256) {
1313 if (!(ofs & 0x8))
1314 ofs += 0x100;
1315 else
1316 ofs -= 0x8;
1317 }
1318
1319 DoC_Command(this, NAND_CMD_READOOB, CDSN_CTRL_WP);
1320 DoC_Address(this, ADDR_COLUMN_PAGE, ofs, CDSN_CTRL_WP, 0);
1321
1322 /* treat crossing 8-byte OOB data for 2M x 8bit devices */
1323 /* Note: datasheet says it should automaticaly wrap to the */
1324 /* next OOB block, but it didn't work here. mf. */
1325 if (this->page256 && ofs + len > (ofs | 0x7) + 1) {
1326 len256 = (ofs | 0x7) + 1 - ofs;
1327 DoC_ReadBuf(this, buf, len256);
1328
1329 DoC_Command(this, NAND_CMD_READOOB, CDSN_CTRL_WP);
1330 DoC_Address(this, ADDR_COLUMN_PAGE, ofs & (~0x1ff),
1331 CDSN_CTRL_WP, 0);
1332 }
1333
1334 DoC_ReadBuf(this, &buf[len256], len - len256);
1335
1336 *retlen = len;
1337 /* Reading the full OOB data drops us off of the end of the page,
1338 * causing the flash device to go into busy mode, so we need
1339 * to wait until ready 11.4.1 and Toshiba TC58256FT docs */
1340
1341 ret = DoC_WaitReady(this);
1342
1343 return ret;
1344
1345 }
1346
1347 int doc_write_oob(struct DiskOnChip* this, loff_t ofs, size_t len,
1348 size_t * retlen, const u_char * buf)
1349 {
1350 int len256 = 0;
1351 unsigned long docptr = this->virtadr;
1352 struct Nand *mychip = &this->chips[shr(ofs, this->chipshift)];
1353 volatile int dummy;
1354
1355 #ifdef PSYCHO_DEBUG
1356 printf("doc_write_oob(%lx, %d): %2.2X %2.2X %2.2X %2.2X ... %2.2X %2.2X .. %2.2X %2.2X\n",
1357 (long)ofs, len, buf[0], buf[1], buf[2], buf[3],
1358 buf[8], buf[9], buf[14],buf[15]);
1359 #endif
1360
1361 /* Find the chip which is to be used and select it */
1362 if (this->curfloor != mychip->floor) {
1363 DoC_SelectFloor(this, mychip->floor);
1364 DoC_SelectChip(this, mychip->chip);
1365 } else if (this->curchip != mychip->chip) {
1366 DoC_SelectChip(this, mychip->chip);
1367 }
1368 this->curfloor = mychip->floor;
1369 this->curchip = mychip->chip;
1370
1371 /* disable the ECC engine */
1372 WriteDOC (DOC_ECC_RESET, docptr, ECCConf);
1373 WriteDOC (DOC_ECC_DIS, docptr, ECCConf);
1374
1375 /* Reset the chip, see Software Requirement 11.4 item 1. */
1376 DoC_Command(this, NAND_CMD_RESET, CDSN_CTRL_WP);
1377
1378 /* issue the Read2 command to set the pointer to the Spare Data Area. */
1379 DoC_Command(this, NAND_CMD_READOOB, CDSN_CTRL_WP);
1380
1381 /* update address for 2M x 8bit devices. OOB starts on the second */
1382 /* page to maintain compatibility with doc_read_ecc. */
1383 if (this->page256) {
1384 if (!(ofs & 0x8))
1385 ofs += 0x100;
1386 else
1387 ofs -= 0x8;
1388 }
1389
1390 /* issue the Serial Data In command to initial the Page Program process */
1391 DoC_Command(this, NAND_CMD_SEQIN, 0);
1392 DoC_Address(this, ADDR_COLUMN_PAGE, ofs, 0, 0);
1393
1394 /* treat crossing 8-byte OOB data for 2M x 8bit devices */
1395 /* Note: datasheet says it should automaticaly wrap to the */
1396 /* next OOB block, but it didn't work here. mf. */
1397 if (this->page256 && ofs + len > (ofs | 0x7) + 1) {
1398 len256 = (ofs | 0x7) + 1 - ofs;
1399 DoC_WriteBuf(this, buf, len256);
1400
1401 DoC_Command(this, NAND_CMD_PAGEPROG, 0);
1402 DoC_Command(this, NAND_CMD_STATUS, 0);
1403 /* DoC_WaitReady() is implicit in DoC_Command */
1404
1405 dummy = ReadDOC(docptr, CDSNSlowIO);
1406 DoC_Delay(this, 2);
1407
1408 if (ReadDOC_(docptr, this->ioreg) & 1) {
1409 puts ("Error programming oob data\n");
1410 /* There was an error */
1411 *retlen = 0;
1412 return DOC_EIO;
1413 }
1414 DoC_Command(this, NAND_CMD_SEQIN, 0);
1415 DoC_Address(this, ADDR_COLUMN_PAGE, ofs & (~0x1ff), 0, 0);
1416 }
1417
1418 DoC_WriteBuf(this, &buf[len256], len - len256);
1419
1420 DoC_Command(this, NAND_CMD_PAGEPROG, 0);
1421 DoC_Command(this, NAND_CMD_STATUS, 0);
1422 /* DoC_WaitReady() is implicit in DoC_Command */
1423
1424 dummy = ReadDOC(docptr, CDSNSlowIO);
1425 DoC_Delay(this, 2);
1426
1427 if (ReadDOC_(docptr, this->ioreg) & 1) {
1428 puts ("Error programming oob data\n");
1429 /* There was an error */
1430 *retlen = 0;
1431 return DOC_EIO;
1432 }
1433
1434 *retlen = len;
1435 return 0;
1436
1437 }
1438
1439 int doc_erase(struct DiskOnChip* this, loff_t ofs, size_t len)
1440 {
1441 volatile int dummy;
1442 unsigned long docptr;
1443 struct Nand *mychip;
1444
1445 if (ofs & (this->erasesize-1) || len & (this->erasesize-1)) {
1446 puts ("Offset and size must be sector aligned\n");
1447 return DOC_EINVAL;
1448 }
1449
1450 docptr = this->virtadr;
1451
1452 /* FIXME: Do this in the background. Use timers or schedule_task() */
1453 while(len) {
1454 mychip = &this->chips[shr(ofs, this->chipshift)];
1455
1456 if (this->curfloor != mychip->floor) {
1457 DoC_SelectFloor(this, mychip->floor);
1458 DoC_SelectChip(this, mychip->chip);
1459 } else if (this->curchip != mychip->chip) {
1460 DoC_SelectChip(this, mychip->chip);
1461 }
1462 this->curfloor = mychip->floor;
1463 this->curchip = mychip->chip;
1464
1465 DoC_Command(this, NAND_CMD_ERASE1, 0);
1466 DoC_Address(this, ADDR_PAGE, ofs, 0, 0);
1467 DoC_Command(this, NAND_CMD_ERASE2, 0);
1468
1469 DoC_Command(this, NAND_CMD_STATUS, CDSN_CTRL_WP);
1470
1471 dummy = ReadDOC(docptr, CDSNSlowIO);
1472 DoC_Delay(this, 2);
1473
1474 if (ReadDOC_(docptr, this->ioreg) & 1) {
1475 printf("Error erasing at 0x%lx\n", (long)ofs);
1476 /* There was an error */
1477 goto callback;
1478 }
1479 ofs += this->erasesize;
1480 len -= this->erasesize;
1481 }
1482
1483 callback:
1484 return 0;
1485 }
1486
1487 static inline int doccheck(unsigned long potential, unsigned long physadr)
1488 {
1489 unsigned long window=potential;
1490 unsigned char tmp, ChipID;
1491 #ifndef DOC_PASSIVE_PROBE
1492 unsigned char tmp2;
1493 #endif
1494
1495 /* Routine copied from the Linux DOC driver */
1496
1497 #ifdef CFG_DOCPROBE_55AA
1498 /* Check for 0x55 0xAA signature at beginning of window,
1499 this is no longer true once we remove the IPL (for Millennium */
1500 if (ReadDOC(window, Sig1) != 0x55 || ReadDOC(window, Sig2) != 0xaa)
1501 return 0;
1502 #endif /* CFG_DOCPROBE_55AA */
1503
1504 #ifndef DOC_PASSIVE_PROBE
1505 /* It's not possible to cleanly detect the DiskOnChip - the
1506 * bootup procedure will put the device into reset mode, and
1507 * it's not possible to talk to it without actually writing
1508 * to the DOCControl register. So we store the current contents
1509 * of the DOCControl register's location, in case we later decide
1510 * that it's not a DiskOnChip, and want to put it back how we
1511 * found it.
1512 */
1513 tmp2 = ReadDOC(window, DOCControl);
1514
1515 /* Reset the DiskOnChip ASIC */
1516 WriteDOC(DOC_MODE_CLR_ERR | DOC_MODE_MDWREN | DOC_MODE_RESET,
1517 window, DOCControl);
1518 WriteDOC(DOC_MODE_CLR_ERR | DOC_MODE_MDWREN | DOC_MODE_RESET,
1519 window, DOCControl);
1520
1521 /* Enable the DiskOnChip ASIC */
1522 WriteDOC(DOC_MODE_CLR_ERR | DOC_MODE_MDWREN | DOC_MODE_NORMAL,
1523 window, DOCControl);
1524 WriteDOC(DOC_MODE_CLR_ERR | DOC_MODE_MDWREN | DOC_MODE_NORMAL,
1525 window, DOCControl);
1526 #endif /* !DOC_PASSIVE_PROBE */
1527
1528 ChipID = ReadDOC(window, ChipID);
1529
1530 switch (ChipID) {
1531 case DOC_ChipID_Doc2k:
1532 /* Check the TOGGLE bit in the ECC register */
1533 tmp = ReadDOC(window, 2k_ECCStatus) & DOC_TOGGLE_BIT;
1534 if ((ReadDOC(window, 2k_ECCStatus) & DOC_TOGGLE_BIT) != tmp)
1535 return ChipID;
1536 break;
1537
1538 case DOC_ChipID_DocMil:
1539 /* Check the TOGGLE bit in the ECC register */
1540 tmp = ReadDOC(window, ECCConf) & DOC_TOGGLE_BIT;
1541 if ((ReadDOC(window, ECCConf) & DOC_TOGGLE_BIT) != tmp)
1542 return ChipID;
1543 break;
1544
1545 default:
1546 #ifndef CFG_DOCPROBE_55AA
1547 /*
1548 * if the ID isn't the DoC2000 or DoCMillenium ID, so we can assume
1549 * the DOC is missing
1550 */
1551 # if 0
1552 printf("Possible DiskOnChip with unknown ChipID %2.2X found at 0x%lx\n",
1553 ChipID, physadr);
1554 # endif
1555 #endif
1556 #ifndef DOC_PASSIVE_PROBE
1557 /* Put back the contents of the DOCControl register, in case it's not
1558 * actually a DiskOnChip.
1559 */
1560 WriteDOC(tmp2, window, DOCControl);
1561 #endif
1562 return 0;
1563 }
1564
1565 puts ("DiskOnChip failed TOGGLE test, dropping.\n");
1566
1567 #ifndef DOC_PASSIVE_PROBE
1568 /* Put back the contents of the DOCControl register: it's not a DiskOnChip */
1569 WriteDOC(tmp2, window, DOCControl);
1570 #endif
1571 return 0;
1572 }
1573
1574 void doc_probe(unsigned long physadr)
1575 {
1576 struct DiskOnChip *this = NULL;
1577 int i=0, ChipID;
1578
1579 if ((ChipID = doccheck(physadr, physadr))) {
1580
1581 for (i=0; i<CFG_MAX_DOC_DEVICE; i++) {
1582 if (doc_dev_desc[i].ChipID == DOC_ChipID_UNKNOWN) {
1583 this = doc_dev_desc + i;
1584 break;
1585 }
1586 }
1587
1588 if (!this) {
1589 puts ("Cannot allocate memory for data structures.\n");
1590 return;
1591 }
1592
1593 if (curr_device == -1)
1594 curr_device = i;
1595
1596 memset((char *)this, 0, sizeof(struct DiskOnChip));
1597
1598 this->virtadr = physadr;
1599 this->physadr = physadr;
1600 this->ChipID = ChipID;
1601
1602 DoC2k_init(this);
1603 } else {
1604 puts ("No DiskOnChip found\n");
1605 }
1606 }