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