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