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