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