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