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