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