blob: 6057dd1a2a317d5989e25aae0e8573917519c1bd [file] [log] [blame]
wdenkdc7c9a12003-03-26 06:55:25 +00001/*
2 * Driver for NAND support, Rick Bronson
3 * borrowed heavily from:
4 * (c) 1999 Machine Vision Holdings, Inc.
5 * (c) 1999, 2000 David Woodhouse <dwmw2@infradead.org>
wdenk384cc682005-04-03 22:35:21 +00006 *
7 * Added 16-bit nand support
8 * (C) 2004 Texas Instruments
wdenkdc7c9a12003-03-26 06:55:25 +00009 */
10
11#include <common.h>
wdenkdc7c9a12003-03-26 06:55:25 +000012#include <command.h>
13#include <malloc.h>
14#include <asm/io.h>
wdenka3d991b2004-04-15 21:48:45 +000015#include <watchdog.h>
wdenkdc7c9a12003-03-26 06:55:25 +000016
17#ifdef CONFIG_SHOW_BOOT_PROGRESS
18# include <status_led.h>
19# define SHOW_BOOT_PROGRESS(arg) show_boot_progress(arg)
20#else
21# define SHOW_BOOT_PROGRESS(arg)
22#endif
23
24#if (CONFIG_COMMANDS & CFG_CMD_NAND)
25
wdenkdc7c9a12003-03-26 06:55:25 +000026#include <linux/mtd/nand.h>
27#include <linux/mtd/nand_ids.h>
wdenk7a8e9bed2003-05-31 18:35:21 +000028#include <jffs2/jffs2.h>
wdenkdc7c9a12003-03-26 06:55:25 +000029
wdenk1f4bb372003-07-27 00:21:01 +000030#ifdef CONFIG_OMAP1510
31void archflashwp(void *archdata, int wp);
32#endif
33
34#define ROUND_DOWN(value,boundary) ((value) & (~((boundary)-1)))
35
wdenkdc7c9a12003-03-26 06:55:25 +000036/*
37 * Definition of the out of band configuration structure
38 */
39struct nand_oob_config {
40 int ecc_pos[6]; /* position of ECC bytes inside oob */
41 int badblock_pos; /* position of bad block flag inside oob -1 = inactive */
42 int eccvalid_pos; /* position of ECC valid flag inside oob -1 = inactive */
43} oob_config = { {0}, 0, 0};
44
wdenka43278a2003-09-11 19:48:06 +000045#undef NAND_DEBUG
wdenkdc7c9a12003-03-26 06:55:25 +000046#undef PSYCHO_DEBUG
wdenk7a8e9bed2003-05-31 18:35:21 +000047
48/* ****************** WARNING *********************
49 * When ALLOW_ERASE_BAD_DEBUG is non-zero the erase command will
50 * erase (or at least attempt to erase) blocks that are marked
51 * bad. This can be very handy if you are _sure_ that the block
52 * is OK, say because you marked a good block bad to test bad
53 * block handling and you are done testing, or if you have
54 * accidentally marked blocks bad.
55 *
56 * Erasing factory marked bad blocks is a _bad_ idea. If the
57 * erase succeeds there is no reliable way to find them again,
58 * and attempting to program or erase bad blocks can affect
59 * the data in _other_ (good) blocks.
60 */
61#define ALLOW_ERASE_BAD_DEBUG 0
wdenkdc7c9a12003-03-26 06:55:25 +000062
63#define CONFIG_MTD_NAND_ECC /* enable ECC */
wdenk1f4bb372003-07-27 00:21:01 +000064#define CONFIG_MTD_NAND_ECC_JFFS2
wdenkdc7c9a12003-03-26 06:55:25 +000065
wdenk7a8e9bed2003-05-31 18:35:21 +000066/* bits for nand_rw() `cmd'; or together as needed */
67#define NANDRW_READ 0x01
68#define NANDRW_WRITE 0x00
69#define NANDRW_JFFS2 0x02
wdenka3d991b2004-04-15 21:48:45 +000070#define NANDRW_JFFS2_SKIP 0x04
wdenk7a8e9bed2003-05-31 18:35:21 +000071
wdenkdc7c9a12003-03-26 06:55:25 +000072/*
73 * Function Prototypes
74 */
75static void nand_print(struct nand_chip *nand);
wdenk13a56952004-06-09 14:58:14 +000076int nand_rw (struct nand_chip* nand, int cmd,
wdenkdc7c9a12003-03-26 06:55:25 +000077 size_t start, size_t len,
78 size_t * retlen, u_char * buf);
wdenk13a56952004-06-09 14:58:14 +000079int nand_erase(struct nand_chip* nand, size_t ofs, size_t len, int clean);
wdenkdc7c9a12003-03-26 06:55:25 +000080static int nand_read_ecc(struct nand_chip *nand, size_t start, size_t len,
81 size_t * retlen, u_char *buf, u_char *ecc_code);
82static int nand_write_ecc (struct nand_chip* nand, size_t to, size_t len,
83 size_t * retlen, const u_char * buf, u_char * ecc_code);
wdenk7a8e9bed2003-05-31 18:35:21 +000084static void nand_print_bad(struct nand_chip *nand);
85static int nand_read_oob(struct nand_chip* nand, size_t ofs, size_t len,
86 size_t * retlen, u_char * buf);
87static int nand_write_oob(struct nand_chip* nand, size_t ofs, size_t len,
88 size_t * retlen, const u_char * buf);
wdenk1f4bb372003-07-27 00:21:01 +000089static int NanD_WaitReady(struct nand_chip *nand, int ale_wait);
wdenkdc7c9a12003-03-26 06:55:25 +000090#ifdef CONFIG_MTD_NAND_ECC
91static int nand_correct_data (u_char *dat, u_char *read_ecc, u_char *calc_ecc);
92static void nand_calculate_ecc (const u_char *dat, u_char *ecc_code);
93#endif
94
wdenk7a8e9bed2003-05-31 18:35:21 +000095struct nand_chip nand_dev_desc[CFG_MAX_NAND_DEVICE] = {{0}};
wdenkdc7c9a12003-03-26 06:55:25 +000096
97/* Current NAND Device */
98static int curr_device = -1;
99
100/* ------------------------------------------------------------------------- */
101
102int do_nand (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
103{
104 int rcode = 0;
105
106 switch (argc) {
107 case 0:
108 case 1:
109 printf ("Usage:\n%s\n", cmdtp->usage);
110 return 1;
111 case 2:
wdenk8bde7f72003-06-27 21:31:46 +0000112 if (strcmp(argv[1],"info") == 0) {
wdenkdc7c9a12003-03-26 06:55:25 +0000113 int i;
114
115 putc ('\n');
116
117 for (i=0; i<CFG_MAX_NAND_DEVICE; ++i) {
118 if(nand_dev_desc[i].ChipID == NAND_ChipID_UNKNOWN)
119 continue; /* list only known devices */
120 printf ("Device %d: ", i);
121 nand_print(&nand_dev_desc[i]);
122 }
123 return 0;
124
125 } else if (strcmp(argv[1],"device") == 0) {
126 if ((curr_device < 0) || (curr_device >= CFG_MAX_NAND_DEVICE)) {
127 puts ("\nno devices available\n");
128 return 1;
129 }
130 printf ("\nDevice %d: ", curr_device);
131 nand_print(&nand_dev_desc[curr_device]);
132 return 0;
wdenk7a8e9bed2003-05-31 18:35:21 +0000133
134 } else if (strcmp(argv[1],"bad") == 0) {
135 if ((curr_device < 0) || (curr_device >= CFG_MAX_NAND_DEVICE)) {
136 puts ("\nno devices available\n");
137 return 1;
138 }
139 printf ("\nDevice %d bad blocks:\n", curr_device);
140 nand_print_bad(&nand_dev_desc[curr_device]);
141 return 0;
142
wdenkdc7c9a12003-03-26 06:55:25 +0000143 }
144 printf ("Usage:\n%s\n", cmdtp->usage);
145 return 1;
146 case 3:
147 if (strcmp(argv[1],"device") == 0) {
148 int dev = (int)simple_strtoul(argv[2], NULL, 10);
149
150 printf ("\nDevice %d: ", dev);
151 if (dev >= CFG_MAX_NAND_DEVICE) {
152 puts ("unknown device\n");
153 return 1;
154 }
155 nand_print(&nand_dev_desc[dev]);
156 /*nand_print (dev);*/
157
158 if (nand_dev_desc[dev].ChipID == NAND_ChipID_UNKNOWN) {
159 return 1;
160 }
161
162 curr_device = dev;
163
164 puts ("... is now current device\n");
165
166 return 0;
167 }
wdenk7a8e9bed2003-05-31 18:35:21 +0000168 else if (strcmp(argv[1],"erase") == 0 && strcmp(argv[2], "clean") == 0) {
169 struct nand_chip* nand = &nand_dev_desc[curr_device];
170 ulong off = 0;
171 ulong size = nand->totlen;
172 int ret;
173
174 printf ("\nNAND erase: device %d offset %ld, size %ld ... ",
175 curr_device, off, size);
176
177 ret = nand_erase (nand, off, size, 1);
178
179 printf("%s\n", ret ? "ERROR" : "OK");
180
181 return ret;
182 }
wdenkdc7c9a12003-03-26 06:55:25 +0000183
184 printf ("Usage:\n%s\n", cmdtp->usage);
185 return 1;
186 default:
187 /* at least 4 args */
188
wdenk7a8e9bed2003-05-31 18:35:21 +0000189 if (strncmp(argv[1], "read", 4) == 0 ||
190 strncmp(argv[1], "write", 5) == 0) {
wdenkdc7c9a12003-03-26 06:55:25 +0000191 ulong addr = simple_strtoul(argv[2], NULL, 16);
192 ulong off = simple_strtoul(argv[3], NULL, 16);
193 ulong size = simple_strtoul(argv[4], NULL, 16);
wdenk7a8e9bed2003-05-31 18:35:21 +0000194 int cmd = (strncmp(argv[1], "read", 4) == 0) ?
195 NANDRW_READ : NANDRW_WRITE;
wdenkdc7c9a12003-03-26 06:55:25 +0000196 int ret, total;
wdenk7a8e9bed2003-05-31 18:35:21 +0000197 char* cmdtail = strchr(argv[1], '.');
198
199 if (cmdtail && !strncmp(cmdtail, ".oob", 2)) {
200 /* read out-of-band data */
201 if (cmd & NANDRW_READ) {
202 ret = nand_read_oob(nand_dev_desc + curr_device,
203 off, size, &total,
204 (u_char*)addr);
205 }
206 else {
207 ret = nand_write_oob(nand_dev_desc + curr_device,
208 off, size, &total,
209 (u_char*)addr);
210 }
211 return ret;
212 }
213 else if (cmdtail && !strncmp(cmdtail, ".jffs2", 2))
214 cmd |= NANDRW_JFFS2; /* skip bad blocks */
wdenka3d991b2004-04-15 21:48:45 +0000215 else if (cmdtail && !strncmp(cmdtail, ".jffs2s", 2)) {
216 cmd |= NANDRW_JFFS2; /* skip bad blocks (on read too) */
217 if (cmd & NANDRW_READ)
218 cmd |= NANDRW_JFFS2_SKIP; /* skip bad blocks (on read too) */
219 }
wdenk7a8e9bed2003-05-31 18:35:21 +0000220#ifdef SXNI855T
221 /* need ".e" same as ".j" for compatibility with older units */
222 else if (cmdtail && !strcmp(cmdtail, ".e"))
223 cmd |= NANDRW_JFFS2; /* skip bad blocks */
224#endif
stroesea842a6d2004-12-16 17:45:46 +0000225#ifdef CFG_NAND_SKIP_BAD_DOT_I
226 /* need ".i" same as ".jffs2s" for compatibility with older units (esd) */
227 /* ".i" for image -> read skips bad block (no 0xff) */
228 else if (cmdtail && !strcmp(cmdtail, ".i"))
229 cmd |= NANDRW_JFFS2; /* skip bad blocks (on read too) */
230 if (cmd & NANDRW_READ)
231 cmd |= NANDRW_JFFS2_SKIP; /* skip bad blocks (on read too) */
232#endif /* CFG_NAND_SKIP_BAD_DOT_I */
wdenk7a8e9bed2003-05-31 18:35:21 +0000233 else if (cmdtail) {
234 printf ("Usage:\n%s\n", cmdtp->usage);
235 return 1;
236 }
wdenkdc7c9a12003-03-26 06:55:25 +0000237
238 printf ("\nNAND %s: device %d offset %ld, size %ld ... ",
wdenk7a8e9bed2003-05-31 18:35:21 +0000239 (cmd & NANDRW_READ) ? "read" : "write",
240 curr_device, off, size);
wdenkdc7c9a12003-03-26 06:55:25 +0000241
242 ret = nand_rw(nand_dev_desc + curr_device, cmd, off, size,
243 &total, (u_char*)addr);
244
wdenk1f4bb372003-07-27 00:21:01 +0000245 printf (" %d bytes %s: %s\n", total,
wdenk998eaae2004-04-18 19:43:36 +0000246 (cmd & NANDRW_READ) ? "read" : "written",
wdenkdc7c9a12003-03-26 06:55:25 +0000247 ret ? "ERROR" : "OK");
248
249 return ret;
wdenk7a8e9bed2003-05-31 18:35:21 +0000250 } else if (strcmp(argv[1],"erase") == 0 &&
251 (argc == 4 || strcmp("clean", argv[2]) == 0)) {
252 int clean = argc == 5;
253 ulong off = simple_strtoul(argv[2 + clean], NULL, 16);
254 ulong size = simple_strtoul(argv[3 + clean], NULL, 16);
wdenkdc7c9a12003-03-26 06:55:25 +0000255 int ret;
256
257 printf ("\nNAND erase: device %d offset %ld, size %ld ... ",
258 curr_device, off, size);
259
wdenk7a8e9bed2003-05-31 18:35:21 +0000260 ret = nand_erase (nand_dev_desc + curr_device, off, size, clean);
wdenkdc7c9a12003-03-26 06:55:25 +0000261
262 printf("%s\n", ret ? "ERROR" : "OK");
263
264 return ret;
265 } else {
266 printf ("Usage:\n%s\n", cmdtp->usage);
267 rcode = 1;
268 }
269
270 return rcode;
271 }
272}
273
wdenk0d498392003-07-01 21:06:45 +0000274U_BOOT_CMD(
275 nand, 5, 1, do_nand,
wdenkb0fce992003-06-29 21:03:46 +0000276 "nand - NAND sub-system\n",
277 "info - show available NAND devices\n"
278 "nand device [dev] - show or set current device\n"
wdenka3d991b2004-04-15 21:48:45 +0000279 "nand read[.jffs2[s]] addr off size\n"
wdenkb0fce992003-06-29 21:03:46 +0000280 "nand write[.jffs2] addr off size - read/write `size' bytes starting\n"
281 " at offset `off' to/from memory address `addr'\n"
282 "nand erase [clean] [off size] - erase `size' bytes from\n"
283 " offset `off' (entire device if not specified)\n"
284 "nand bad - show bad blocks\n"
285 "nand read.oob addr off size - read out-of-band data\n"
286 "nand write.oob addr off size - read out-of-band data\n"
287);
288
wdenkdc7c9a12003-03-26 06:55:25 +0000289int do_nandboot (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
290{
291 char *boot_device = NULL;
292 char *ep;
293 int dev;
294 ulong cnt;
295 ulong addr;
296 ulong offset = 0;
297 image_header_t *hdr;
298 int rcode = 0;
299 switch (argc) {
300 case 1:
301 addr = CFG_LOAD_ADDR;
302 boot_device = getenv ("bootdevice");
303 break;
304 case 2:
305 addr = simple_strtoul(argv[1], NULL, 16);
306 boot_device = getenv ("bootdevice");
307 break;
308 case 3:
309 addr = simple_strtoul(argv[1], NULL, 16);
310 boot_device = argv[2];
311 break;
312 case 4:
313 addr = simple_strtoul(argv[1], NULL, 16);
314 boot_device = argv[2];
315 offset = simple_strtoul(argv[3], NULL, 16);
316 break;
317 default:
318 printf ("Usage:\n%s\n", cmdtp->usage);
319 SHOW_BOOT_PROGRESS (-1);
320 return 1;
321 }
322
323 if (!boot_device) {
324 puts ("\n** No boot device **\n");
325 SHOW_BOOT_PROGRESS (-1);
326 return 1;
327 }
328
329 dev = simple_strtoul(boot_device, &ep, 16);
330
331 if ((dev >= CFG_MAX_NAND_DEVICE) ||
332 (nand_dev_desc[dev].ChipID == NAND_ChipID_UNKNOWN)) {
333 printf ("\n** Device %d not available\n", dev);
334 SHOW_BOOT_PROGRESS (-1);
335 return 1;
336 }
337
wdenk7a8e9bed2003-05-31 18:35:21 +0000338 printf ("\nLoading from device %d: %s at 0x%lx (offset 0x%lx)\n",
wdenkdc7c9a12003-03-26 06:55:25 +0000339 dev, nand_dev_desc[dev].name, nand_dev_desc[dev].IO_ADDR,
340 offset);
341
wdenk7a8e9bed2003-05-31 18:35:21 +0000342 if (nand_rw (nand_dev_desc + dev, NANDRW_READ, offset,
wdenkdc7c9a12003-03-26 06:55:25 +0000343 SECTORSIZE, NULL, (u_char *)addr)) {
344 printf ("** Read error on %d\n", dev);
345 SHOW_BOOT_PROGRESS (-1);
346 return 1;
347 }
348
349 hdr = (image_header_t *)addr;
350
351 if (ntohl(hdr->ih_magic) == IH_MAGIC) {
352
353 print_image_hdr (hdr);
354
355 cnt = (ntohl(hdr->ih_size) + sizeof(image_header_t));
356 cnt -= SECTORSIZE;
357 } else {
358 printf ("\n** Bad Magic Number 0x%x **\n", hdr->ih_magic);
359 SHOW_BOOT_PROGRESS (-1);
360 return 1;
361 }
362
wdenk7a8e9bed2003-05-31 18:35:21 +0000363 if (nand_rw (nand_dev_desc + dev, NANDRW_READ, offset + SECTORSIZE, cnt,
wdenkdc7c9a12003-03-26 06:55:25 +0000364 NULL, (u_char *)(addr+SECTORSIZE))) {
365 printf ("** Read error on %d\n", dev);
366 SHOW_BOOT_PROGRESS (-1);
367 return 1;
368 }
369
370 /* Loading ok, update default load address */
371
372 load_addr = addr;
373
374 /* Check if we should attempt an auto-start */
375 if (((ep = getenv("autostart")) != NULL) && (strcmp(ep,"yes") == 0)) {
376 char *local_args[2];
377 extern int do_bootm (cmd_tbl_t *, int, int, char *[]);
378
379 local_args[0] = argv[0];
380 local_args[1] = NULL;
381
wdenk7a8e9bed2003-05-31 18:35:21 +0000382 printf ("Automatic boot of image at addr 0x%08lx ...\n", addr);
wdenkdc7c9a12003-03-26 06:55:25 +0000383
384 do_bootm (cmdtp, 0, 1, local_args);
385 rcode = 1;
386 }
387 return rcode;
388}
389
wdenk0d498392003-07-01 21:06:45 +0000390U_BOOT_CMD(
391 nboot, 4, 1, do_nandboot,
wdenkb0fce992003-06-29 21:03:46 +0000392 "nboot - boot from NAND device\n",
393 "loadAddr dev\n"
394);
395
wdenk7a8e9bed2003-05-31 18:35:21 +0000396/* returns 0 if block containing pos is OK:
397 * valid erase block and
398 * not marked bad, or no bad mark position is specified
399 * returns 1 if marked bad or otherwise invalid
400 */
wdenk384cc682005-04-03 22:35:21 +0000401int check_block (struct nand_chip *nand, unsigned long pos)
wdenk7a8e9bed2003-05-31 18:35:21 +0000402{
403 int retlen;
404 uint8_t oob_data;
wdenk384cc682005-04-03 22:35:21 +0000405 uint16_t oob_data16[6];
wdenk7a8e9bed2003-05-31 18:35:21 +0000406 int page0 = pos & (-nand->erasesize);
407 int page1 = page0 + nand->oobblock;
408 int badpos = oob_config.badblock_pos;
409
410 if (pos >= nand->totlen)
411 return 1;
412
413 if (badpos < 0)
414 return 0; /* no way to check, assume OK */
415
wdenk384cc682005-04-03 22:35:21 +0000416 if (nand->bus16) {
417 if (nand_read_oob(nand, (page0 + 0), 12, &retlen, (uint8_t *)oob_data16)
418 || (oob_data16[2] & 0xff00) != 0xff00)
419 return 1;
420 if (nand_read_oob(nand, (page1 + 0), 12, &retlen, (uint8_t *)oob_data16)
421 || (oob_data16[2] & 0xff00) != 0xff00)
422 return 1;
423 } else {
424 /* Note - bad block marker can be on first or second page */
425 if (nand_read_oob(nand, page0 + badpos, 1, &retlen, &oob_data)
426 || oob_data != 0xff
427 || nand_read_oob (nand, page1 + badpos, 1, &retlen, &oob_data)
428 || oob_data != 0xff)
429 return 1;
430 }
wdenk7a8e9bed2003-05-31 18:35:21 +0000431
432 return 0;
433}
wdenk8bde7f72003-06-27 21:31:46 +0000434
wdenk7a8e9bed2003-05-31 18:35:21 +0000435/* print bad blocks in NAND flash */
436static void nand_print_bad(struct nand_chip* nand)
437{
438 unsigned long pos;
439
440 for (pos = 0; pos < nand->totlen; pos += nand->erasesize) {
441 if (check_block(nand, pos))
442 printf(" 0x%8.8lx\n", pos);
443 }
444 puts("\n");
445}
446
447/* cmd: 0: NANDRW_WRITE write, fail on bad block
448 * 1: NANDRW_READ read, fail on bad block
449 * 2: NANDRW_WRITE | NANDRW_JFFS2 write, skip bad blocks
450 * 3: NANDRW_READ | NANDRW_JFFS2 read, data all 0xff for bad blocks
wdenka3d991b2004-04-15 21:48:45 +0000451 * 7: NANDRW_READ | NANDRW_JFFS2 | NANDRW_JFFS2_SKIP read, skip bad blocks
wdenk7a8e9bed2003-05-31 18:35:21 +0000452 */
wdenk13a56952004-06-09 14:58:14 +0000453int nand_rw (struct nand_chip* nand, int cmd,
wdenkdc7c9a12003-03-26 06:55:25 +0000454 size_t start, size_t len,
455 size_t * retlen, u_char * buf)
456{
wdenk1f4bb372003-07-27 00:21:01 +0000457 int ret = 0, n, total = 0;
wdenkdc7c9a12003-03-26 06:55:25 +0000458 char eccbuf[6];
wdenk7a8e9bed2003-05-31 18:35:21 +0000459 /* eblk (once set) is the start of the erase block containing the
460 * data being processed.
461 */
462 unsigned long eblk = ~0; /* force mismatch on first pass */
463 unsigned long erasesize = nand->erasesize;
wdenkdc7c9a12003-03-26 06:55:25 +0000464
wdenk7a8e9bed2003-05-31 18:35:21 +0000465 while (len) {
466 if ((start & (-erasesize)) != eblk) {
467 /* have crossed into new erase block, deal with
468 * it if it is sure marked bad.
469 */
470 eblk = start & (-erasesize); /* start of block */
471 if (check_block(nand, eblk)) {
472 if (cmd == (NANDRW_READ | NANDRW_JFFS2)) {
473 while (len > 0 &&
474 start - eblk < erasesize) {
475 *(buf++) = 0xff;
476 ++start;
477 ++total;
478 --len;
479 }
480 continue;
wdenk384cc682005-04-03 22:35:21 +0000481 } else if (cmd == (NANDRW_READ | NANDRW_JFFS2 | NANDRW_JFFS2_SKIP)) {
wdenka3d991b2004-04-15 21:48:45 +0000482 start += erasesize;
483 continue;
wdenk384cc682005-04-03 22:35:21 +0000484 } else if (cmd == (NANDRW_WRITE | NANDRW_JFFS2)) {
wdenk7a8e9bed2003-05-31 18:35:21 +0000485 /* skip bad block */
486 start += erasesize;
487 continue;
wdenk384cc682005-04-03 22:35:21 +0000488 } else {
wdenk7a8e9bed2003-05-31 18:35:21 +0000489 ret = 1;
490 break;
491 }
492 }
493 }
wdenkdc7c9a12003-03-26 06:55:25 +0000494 /* The ECC will not be calculated correctly if
495 less than 512 is written or read */
wdenk1f4bb372003-07-27 00:21:01 +0000496 /* Is request at least 512 bytes AND it starts on a proper boundry */
497 if((start != ROUND_DOWN(start, 0x200)) || (len < 0x200))
498 printf("Warning block writes should be at least 512 bytes and start on a 512 byte boundry\n");
499
wdenk384cc682005-04-03 22:35:21 +0000500 if (cmd & NANDRW_READ) {
wdenk7a8e9bed2003-05-31 18:35:21 +0000501 ret = nand_read_ecc(nand, start,
502 min(len, eblk + erasesize - start),
wdenk1f4bb372003-07-27 00:21:01 +0000503 &n, (u_char*)buf, eccbuf);
wdenk384cc682005-04-03 22:35:21 +0000504 } else {
wdenk7a8e9bed2003-05-31 18:35:21 +0000505 ret = nand_write_ecc(nand, start,
506 min(len, eblk + erasesize - start),
wdenk1f4bb372003-07-27 00:21:01 +0000507 &n, (u_char*)buf, eccbuf);
wdenk384cc682005-04-03 22:35:21 +0000508 }
wdenkdc7c9a12003-03-26 06:55:25 +0000509
510 if (ret)
511 break;
512
513 start += n;
514 buf += n;
515 total += n;
516 len -= n;
517 }
518 if (retlen)
519 *retlen = total;
520
521 return ret;
522}
523
524static void nand_print(struct nand_chip *nand)
wdenk0db5bca2003-03-31 17:27:09 +0000525{
wdenk7a8e9bed2003-05-31 18:35:21 +0000526 if (nand->numchips > 1) {
527 printf("%s at 0x%lx,\n"
528 "\t %d chips %s, size %d MB, \n"
529 "\t total size %ld MB, sector size %ld kB\n",
530 nand->name, nand->IO_ADDR, nand->numchips,
531 nand->chips_name, 1 << (nand->chipshift - 20),
532 nand->totlen >> 20, nand->erasesize >> 10);
533 }
534 else {
wdenk8bde7f72003-06-27 21:31:46 +0000535 printf("%s at 0x%lx (", nand->chips_name, nand->IO_ADDR);
wdenk7a8e9bed2003-05-31 18:35:21 +0000536 print_size(nand->totlen, ", ");
537 print_size(nand->erasesize, " sector)\n");
wdenkdc7c9a12003-03-26 06:55:25 +0000538 }
539}
540
541/* ------------------------------------------------------------------------- */
542
wdenk1f4bb372003-07-27 00:21:01 +0000543static int NanD_WaitReady(struct nand_chip *nand, int ale_wait)
wdenkdc7c9a12003-03-26 06:55:25 +0000544{
545 /* This is inline, to optimise the common case, where it's ready instantly */
546 int ret = 0;
wdenkdc7c9a12003-03-26 06:55:25 +0000547
wdenk1f4bb372003-07-27 00:21:01 +0000548#ifdef NAND_NO_RB /* in config file, shorter delays currently wrap accesses */
549 if(ale_wait)
550 NAND_WAIT_READY(nand); /* do the worst case 25us wait */
551 else
552 udelay(10);
553#else /* has functional r/b signal */
wdenk12f34242003-09-02 22:48:03 +0000554 NAND_WAIT_READY(nand);
wdenk1f4bb372003-07-27 00:21:01 +0000555#endif
wdenkdc7c9a12003-03-26 06:55:25 +0000556 return ret;
557}
558
559/* NanD_Command: Send a flash command to the flash chip */
560
561static inline int NanD_Command(struct nand_chip *nand, unsigned char command)
562{
563 unsigned long nandptr = nand->IO_ADDR;
564
565 /* Assert the CLE (Command Latch Enable) line to the flash chip */
566 NAND_CTL_SETCLE(nandptr);
567
568 /* Send the command */
569 WRITE_NAND_COMMAND(command, nandptr);
570
571 /* Lower the CLE line */
572 NAND_CTL_CLRCLE(nandptr);
573
wdenk1f4bb372003-07-27 00:21:01 +0000574#ifdef NAND_NO_RB
575 if(command == NAND_CMD_RESET){
576 u_char ret_val;
577 NanD_Command(nand, NAND_CMD_STATUS);
wdenk384cc682005-04-03 22:35:21 +0000578 do {
wdenk1f4bb372003-07-27 00:21:01 +0000579 ret_val = READ_NAND(nandptr);/* wait till ready */
580 } while((ret_val & 0x40) != 0x40);
581 }
582#endif
583 return NanD_WaitReady(nand, 0);
wdenkdc7c9a12003-03-26 06:55:25 +0000584}
585
586/* NanD_Address: Set the current address for the flash chip */
587
588static int NanD_Address(struct nand_chip *nand, int numbytes, unsigned long ofs)
wdenk0db5bca2003-03-31 17:27:09 +0000589{
590 unsigned long nandptr;
591 int i;
wdenkdc7c9a12003-03-26 06:55:25 +0000592
wdenk0db5bca2003-03-31 17:27:09 +0000593 nandptr = nand->IO_ADDR;
wdenkdc7c9a12003-03-26 06:55:25 +0000594
595 /* Assert the ALE (Address Latch Enable) line to the flash chip */
wdenk0db5bca2003-03-31 17:27:09 +0000596 NAND_CTL_SETALE(nandptr);
wdenkdc7c9a12003-03-26 06:55:25 +0000597
wdenk0db5bca2003-03-31 17:27:09 +0000598 /* Send the address */
599 /* Devices with 256-byte page are addressed as:
600 * Column (bits 0-7), Page (bits 8-15, 16-23, 24-31)
601 * there is no device on the market with page256
602 * and more than 24 bits.
603 * Devices with 512-byte page are addressed as:
604 * Column (bits 0-7), Page (bits 9-16, 17-24, 25-31)
605 * 25-31 is sent only if the chip support it.
606 * bit 8 changes the read command to be sent
607 * (NAND_CMD_READ0 or NAND_CMD_READ1).
wdenkdc7c9a12003-03-26 06:55:25 +0000608 */
609
wdenk0db5bca2003-03-31 17:27:09 +0000610 if (numbytes == ADDR_COLUMN || numbytes == ADDR_COLUMN_PAGE)
611 WRITE_NAND_ADDRESS(ofs, nandptr);
wdenkdc7c9a12003-03-26 06:55:25 +0000612
wdenk0db5bca2003-03-31 17:27:09 +0000613 ofs = ofs >> nand->page_shift;
wdenkdc7c9a12003-03-26 06:55:25 +0000614
wdenk384cc682005-04-03 22:35:21 +0000615 if (numbytes == ADDR_PAGE || numbytes == ADDR_COLUMN_PAGE) {
616 for (i = 0; i < nand->pageadrlen; i++, ofs = ofs >> 8) {
wdenk0db5bca2003-03-31 17:27:09 +0000617 WRITE_NAND_ADDRESS(ofs, nandptr);
wdenk384cc682005-04-03 22:35:21 +0000618 }
619 }
wdenkdc7c9a12003-03-26 06:55:25 +0000620
wdenk0db5bca2003-03-31 17:27:09 +0000621 /* Lower the ALE line */
622 NAND_CTL_CLRALE(nandptr);
wdenkdc7c9a12003-03-26 06:55:25 +0000623
wdenk0db5bca2003-03-31 17:27:09 +0000624 /* Wait for the chip to respond */
wdenk1f4bb372003-07-27 00:21:01 +0000625 return NanD_WaitReady(nand, 1);
wdenk0db5bca2003-03-31 17:27:09 +0000626}
wdenkdc7c9a12003-03-26 06:55:25 +0000627
628/* NanD_SelectChip: Select a given flash chip within the current floor */
629
630static inline int NanD_SelectChip(struct nand_chip *nand, int chip)
631{
632 /* Wait for it to be ready */
wdenk1f4bb372003-07-27 00:21:01 +0000633 return NanD_WaitReady(nand, 0);
wdenkdc7c9a12003-03-26 06:55:25 +0000634}
635
636/* NanD_IdentChip: Identify a given NAND chip given {floor,chip} */
637
638static int NanD_IdentChip(struct nand_chip *nand, int floor, int chip)
639{
640 int mfr, id, i;
641
wdenk0db5bca2003-03-31 17:27:09 +0000642 NAND_ENABLE_CE(nand); /* set pin low */
wdenkdc7c9a12003-03-26 06:55:25 +0000643 /* Reset the chip */
644 if (NanD_Command(nand, NAND_CMD_RESET)) {
645#ifdef NAND_DEBUG
646 printf("NanD_Command (reset) for %d,%d returned true\n",
647 floor, chip);
648#endif
wdenk0db5bca2003-03-31 17:27:09 +0000649 NAND_DISABLE_CE(nand); /* set pin high */
wdenkdc7c9a12003-03-26 06:55:25 +0000650 return 0;
651 }
652
653 /* Read the NAND chip ID: 1. Send ReadID command */
654 if (NanD_Command(nand, NAND_CMD_READID)) {
655#ifdef NAND_DEBUG
656 printf("NanD_Command (ReadID) for %d,%d returned true\n",
657 floor, chip);
658#endif
wdenk0db5bca2003-03-31 17:27:09 +0000659 NAND_DISABLE_CE(nand); /* set pin high */
wdenkdc7c9a12003-03-26 06:55:25 +0000660 return 0;
661 }
662
663 /* Read the NAND chip ID: 2. Send address byte zero */
664 NanD_Address(nand, ADDR_COLUMN, 0);
665
666 /* Read the manufacturer and device id codes from the device */
667
668 mfr = READ_NAND(nand->IO_ADDR);
669
670 id = READ_NAND(nand->IO_ADDR);
671
wdenk8bde7f72003-06-27 21:31:46 +0000672 NAND_DISABLE_CE(nand); /* set pin high */
wdenkdc7c9a12003-03-26 06:55:25 +0000673 /* No response - return failure */
wdenk0db5bca2003-03-31 17:27:09 +0000674 if (mfr == 0xff || mfr == 0) {
wdenk4d816772003-09-03 14:03:26 +0000675#ifdef NAND_DEBUG
wdenk0db5bca2003-03-31 17:27:09 +0000676 printf("NanD_Command (ReadID) got %d %d\n", mfr, id);
wdenk4d816772003-09-03 14:03:26 +0000677#endif
wdenk0db5bca2003-03-31 17:27:09 +0000678 return 0;
679 }
wdenkdc7c9a12003-03-26 06:55:25 +0000680
681 /* Check it's the same as the first chip we identified.
682 * M-Systems say that any given nand_chip device should only
683 * contain _one_ type of flash part, although that's not a
684 * hardware restriction. */
685 if (nand->mfr) {
wdenk384cc682005-04-03 22:35:21 +0000686 if (nand->mfr == mfr && nand->id == id) {
wdenkdc7c9a12003-03-26 06:55:25 +0000687 return 1; /* This is another the same the first */
wdenk384cc682005-04-03 22:35:21 +0000688 } else {
wdenkdc7c9a12003-03-26 06:55:25 +0000689 printf("Flash chip at floor %d, chip %d is different:\n",
690 floor, chip);
wdenk384cc682005-04-03 22:35:21 +0000691 }
wdenkdc7c9a12003-03-26 06:55:25 +0000692 }
693
694 /* Print and store the manufacturer and ID codes. */
695 for (i = 0; nand_flash_ids[i].name != NULL; i++) {
696 if (mfr == nand_flash_ids[i].manufacture_id &&
697 id == nand_flash_ids[i].model_id) {
698#ifdef NAND_DEBUG
699 printf("Flash chip found:\n\t Manufacturer ID: 0x%2.2X, "
700 "Chip ID: 0x%2.2X (%s)\n", mfr, id,
701 nand_flash_ids[i].name);
702#endif
703 if (!nand->mfr) {
704 nand->mfr = mfr;
705 nand->id = id;
706 nand->chipshift =
707 nand_flash_ids[i].chipshift;
708 nand->page256 = nand_flash_ids[i].page256;
wdenk7a8e9bed2003-05-31 18:35:21 +0000709 nand->eccsize = 256;
wdenkdc7c9a12003-03-26 06:55:25 +0000710 if (nand->page256) {
711 nand->oobblock = 256;
712 nand->oobsize = 8;
713 nand->page_shift = 8;
714 } else {
715 nand->oobblock = 512;
716 nand->oobsize = 16;
717 nand->page_shift = 9;
718 }
wdenk384cc682005-04-03 22:35:21 +0000719 nand->pageadrlen = nand_flash_ids[i].pageadrlen;
720 nand->erasesize = nand_flash_ids[i].erasesize;
721 nand->chips_name = nand_flash_ids[i].name;
722 nand->bus16 = nand_flash_ids[i].bus16;
723 return 1;
wdenkdc7c9a12003-03-26 06:55:25 +0000724 }
725 return 0;
726 }
727 }
728
729
730#ifdef NAND_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/* NanD_ScanChips: Find all NAND chips present in a nand_chip, and identify them */
740
741static void NanD_ScanChips(struct nand_chip *nand)
742{
743 int floor, chip;
744 int numchips[NAND_MAX_FLOORS];
745 int maxchips = NAND_MAX_CHIPS;
746 int ret = 1;
747
748 nand->numchips = 0;
749 nand->mfr = 0;
750 nand->id = 0;
751
752
753 /* For each floor, find the number of valid chips it contains */
754 for (floor = 0; floor < NAND_MAX_FLOORS; floor++) {
755 ret = 1;
756 numchips[floor] = 0;
757 for (chip = 0; chip < maxchips && ret != 0; chip++) {
758
759 ret = NanD_IdentChip(nand, floor, chip);
760 if (ret) {
761 numchips[floor]++;
762 nand->numchips++;
763 }
764 }
765 }
766
767 /* If there are none at all that we recognise, bail */
768 if (!nand->numchips) {
wdenka43278a2003-09-11 19:48:06 +0000769#ifdef NAND_DEBUG
wdenk4d816772003-09-03 14:03:26 +0000770 puts ("No NAND flash chips recognised.\n");
wdenka43278a2003-09-11 19:48:06 +0000771#endif
wdenkdc7c9a12003-03-26 06:55:25 +0000772 return;
773 }
774
775 /* Allocate an array to hold the information for each chip */
776 nand->chips = malloc(sizeof(struct Nand) * nand->numchips);
777 if (!nand->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 < NAND_MAX_FLOORS; floor++) {
787 for (chip = 0; chip < numchips[floor]; chip++) {
788 nand->chips[ret].floor = floor;
789 nand->chips[ret].chip = chip;
790 nand->chips[ret].curadr = 0;
791 nand->chips[ret].curmode = 0x50;
792 ret++;
793 }
794 }
795
796 /* Calculate and print the total size of the device */
797 nand->totlen = nand->numchips * (1 << nand->chipshift);
798
799#ifdef NAND_DEBUG
800 printf("%d flash chips found. Total nand_chip size: %ld MB\n",
801 nand->numchips, nand->totlen >> 20);
802#endif
803}
wdenk0db5bca2003-03-31 17:27:09 +0000804
wdenkdc7c9a12003-03-26 06:55:25 +0000805/* we need to be fast here, 1 us per read translates to 1 second per meg */
wdenk384cc682005-04-03 22:35:21 +0000806static void NanD_ReadBuf (struct nand_chip *nand, u_char * data_buf, int cntr)
wdenk0db5bca2003-03-31 17:27:09 +0000807{
wdenk7a8e9bed2003-05-31 18:35:21 +0000808 unsigned long nandptr = nand->IO_ADDR;
wdenk0db5bca2003-03-31 17:27:09 +0000809
wdenk384cc682005-04-03 22:35:21 +0000810 NanD_Command (nand, NAND_CMD_READ0);
wdenk0db5bca2003-03-31 17:27:09 +0000811
wdenk384cc682005-04-03 22:35:21 +0000812 if (nand->bus16) {
813 u16 val;
814
815 while (cntr >= 16) {
816 val = READ_NAND (nandptr);
817 *data_buf++ = val & 0xff;
818 *data_buf++ = val >> 8;
819 val = READ_NAND (nandptr);
820 *data_buf++ = val & 0xff;
821 *data_buf++ = val >> 8;
822 val = READ_NAND (nandptr);
823 *data_buf++ = val & 0xff;
824 *data_buf++ = val >> 8;
825 val = READ_NAND (nandptr);
826 *data_buf++ = val & 0xff;
827 *data_buf++ = val >> 8;
828 val = READ_NAND (nandptr);
829 *data_buf++ = val & 0xff;
830 *data_buf++ = val >> 8;
831 val = READ_NAND (nandptr);
832 *data_buf++ = val & 0xff;
833 *data_buf++ = val >> 8;
834 val = READ_NAND (nandptr);
835 *data_buf++ = val & 0xff;
836 *data_buf++ = val >> 8;
837 val = READ_NAND (nandptr);
838 *data_buf++ = val & 0xff;
839 *data_buf++ = val >> 8;
840 cntr -= 16;
841 }
842
843 while (cntr > 0) {
844 val = READ_NAND (nandptr);
845 *data_buf++ = val & 0xff;
846 *data_buf++ = val >> 8;
847 cntr -= 2;
848 }
849 } else {
850 while (cntr >= 16) {
851 *data_buf++ = READ_NAND (nandptr);
852 *data_buf++ = READ_NAND (nandptr);
853 *data_buf++ = READ_NAND (nandptr);
854 *data_buf++ = READ_NAND (nandptr);
855 *data_buf++ = READ_NAND (nandptr);
856 *data_buf++ = READ_NAND (nandptr);
857 *data_buf++ = READ_NAND (nandptr);
858 *data_buf++ = READ_NAND (nandptr);
859 *data_buf++ = READ_NAND (nandptr);
860 *data_buf++ = READ_NAND (nandptr);
861 *data_buf++ = READ_NAND (nandptr);
862 *data_buf++ = READ_NAND (nandptr);
863 *data_buf++ = READ_NAND (nandptr);
864 *data_buf++ = READ_NAND (nandptr);
865 *data_buf++ = READ_NAND (nandptr);
866 *data_buf++ = READ_NAND (nandptr);
867 cntr -= 16;
868 }
869
870 while (cntr > 0) {
871 *data_buf++ = READ_NAND (nandptr);
872 cntr--;
873 }
wdenk0db5bca2003-03-31 17:27:09 +0000874 }
875}
wdenkdc7c9a12003-03-26 06:55:25 +0000876
wdenkdc7c9a12003-03-26 06:55:25 +0000877/*
878 * NAND read with ECC
879 */
880static int nand_read_ecc(struct nand_chip *nand, size_t start, size_t len,
881 size_t * retlen, u_char *buf, u_char *ecc_code)
882{
883 int col, page;
884 int ecc_status = 0;
885#ifdef CONFIG_MTD_NAND_ECC
886 int j;
887 int ecc_failed = 0;
888 u_char *data_poi;
889 u_char ecc_calc[6];
890#endif
wdenkdc7c9a12003-03-26 06:55:25 +0000891
892 /* Do not allow reads past end of device */
893 if ((start + len) > nand->totlen) {
wdenk384cc682005-04-03 22:35:21 +0000894 printf ("%s: Attempt read beyond end of device %x %x %x\n",
895 __FUNCTION__, (uint) start, (uint) len, (uint) nand->totlen);
wdenkdc7c9a12003-03-26 06:55:25 +0000896 *retlen = 0;
897 return -1;
898 }
899
900 /* First we calculate the starting page */
wdenk0db5bca2003-03-31 17:27:09 +0000901 /*page = shr(start, nand->page_shift);*/
902 page = start >> nand->page_shift;
wdenkdc7c9a12003-03-26 06:55:25 +0000903
904 /* Get raw starting column */
905 col = start & (nand->oobblock - 1);
906
907 /* Initialize return value */
908 *retlen = 0;
909
910 /* Select the NAND device */
911 NAND_ENABLE_CE(nand); /* set pin low */
912
913 /* Loop until all data read */
914 while (*retlen < len) {
915
wdenkdc7c9a12003-03-26 06:55:25 +0000916#ifdef CONFIG_MTD_NAND_ECC
wdenkdc7c9a12003-03-26 06:55:25 +0000917 /* Do we have this page in cache ? */
918 if (nand->cache_page == page)
919 goto readdata;
920 /* Send the read command */
921 NanD_Command(nand, NAND_CMD_READ0);
wdenk384cc682005-04-03 22:35:21 +0000922 if (nand->bus16) {
923 NanD_Address(nand, ADDR_COLUMN_PAGE,
924 (page << nand->page_shift) + (col >> 1));
925 } else {
926 NanD_Address(nand, ADDR_COLUMN_PAGE,
927 (page << nand->page_shift) + col);
928 }
929
wdenkdc7c9a12003-03-26 06:55:25 +0000930 /* Read in a page + oob data */
wdenk7a8e9bed2003-05-31 18:35:21 +0000931 NanD_ReadBuf(nand, nand->data_buf, nand->oobblock + nand->oobsize);
wdenkdc7c9a12003-03-26 06:55:25 +0000932
933 /* copy data into cache, for read out of cache and if ecc fails */
wdenk384cc682005-04-03 22:35:21 +0000934 if (nand->data_cache) {
935 memcpy (nand->data_cache, nand->data_buf,
936 nand->oobblock + nand->oobsize);
937 }
wdenkdc7c9a12003-03-26 06:55:25 +0000938
939 /* Pick the ECC bytes out of the oob data */
wdenk384cc682005-04-03 22:35:21 +0000940 for (j = 0; j < 6; j++) {
wdenkdc7c9a12003-03-26 06:55:25 +0000941 ecc_code[j] = nand->data_buf[(nand->oobblock + oob_config.ecc_pos[j])];
wdenk384cc682005-04-03 22:35:21 +0000942 }
wdenkdc7c9a12003-03-26 06:55:25 +0000943
944 /* Calculate the ECC and verify it */
945 /* If block was not written with ECC, skip ECC */
946 if (oob_config.eccvalid_pos != -1 &&
947 (nand->data_buf[nand->oobblock + oob_config.eccvalid_pos] & 0x0f) != 0x0f) {
948
949 nand_calculate_ecc (&nand->data_buf[0], &ecc_calc[0]);
950 switch (nand_correct_data (&nand->data_buf[0], &ecc_code[0], &ecc_calc[0])) {
951 case -1:
wdenk0db5bca2003-03-31 17:27:09 +0000952 printf ("%s: Failed ECC read, page 0x%08x\n", __FUNCTION__, page);
wdenkdc7c9a12003-03-26 06:55:25 +0000953 ecc_failed++;
954 break;
955 case 1:
956 case 2: /* transfer ECC corrected data to cache */
wdenk7a8e9bed2003-05-31 18:35:21 +0000957 if (nand->data_cache)
958 memcpy (nand->data_cache, nand->data_buf, 256);
wdenkdc7c9a12003-03-26 06:55:25 +0000959 break;
960 }
961 }
962
963 if (oob_config.eccvalid_pos != -1 &&
964 nand->oobblock == 512 && (nand->data_buf[nand->oobblock + oob_config.eccvalid_pos] & 0xf0) != 0xf0) {
965
966 nand_calculate_ecc (&nand->data_buf[256], &ecc_calc[3]);
967 switch (nand_correct_data (&nand->data_buf[256], &ecc_code[3], &ecc_calc[3])) {
968 case -1:
wdenk0db5bca2003-03-31 17:27:09 +0000969 printf ("%s: Failed ECC read, page 0x%08x\n", __FUNCTION__, page);
wdenkdc7c9a12003-03-26 06:55:25 +0000970 ecc_failed++;
971 break;
972 case 1:
973 case 2: /* transfer ECC corrected data to cache */
974 if (nand->data_cache)
975 memcpy (&nand->data_cache[256], &nand->data_buf[256], 256);
976 break;
977 }
978 }
979readdata:
980 /* Read the data from ECC data buffer into return buffer */
981 data_poi = (nand->data_cache) ? nand->data_cache : nand->data_buf;
982 data_poi += col;
983 if ((*retlen + (nand->oobblock - col)) >= len) {
wdenk7a8e9bed2003-05-31 18:35:21 +0000984 memcpy (buf + *retlen, data_poi, len - *retlen);
wdenkdc7c9a12003-03-26 06:55:25 +0000985 *retlen = len;
986 } else {
wdenk7a8e9bed2003-05-31 18:35:21 +0000987 memcpy (buf + *retlen, data_poi, nand->oobblock - col);
wdenkdc7c9a12003-03-26 06:55:25 +0000988 *retlen += nand->oobblock - col;
989 }
990 /* Set cache page address, invalidate, if ecc_failed */
991 nand->cache_page = (nand->data_cache && !ecc_failed) ? page : -1;
992
993 ecc_status += ecc_failed;
994 ecc_failed = 0;
995
996#else
997 /* Send the read command */
998 NanD_Command(nand, NAND_CMD_READ0);
wdenk384cc682005-04-03 22:35:21 +0000999 if (nand->bus16) {
1000 NanD_Address(nand, ADDR_COLUMN_PAGE,
1001 (page << nand->page_shift) + (col >> 1));
1002 } else {
1003 NanD_Address(nand, ADDR_COLUMN_PAGE,
1004 (page << nand->page_shift) + col);
1005 }
1006
wdenkdc7c9a12003-03-26 06:55:25 +00001007 /* Read the data directly into the return buffer */
1008 if ((*retlen + (nand->oobblock - col)) >= len) {
wdenk7a8e9bed2003-05-31 18:35:21 +00001009 NanD_ReadBuf(nand, buf + *retlen, len - *retlen);
wdenkdc7c9a12003-03-26 06:55:25 +00001010 *retlen = len;
1011 /* We're done */
1012 continue;
1013 } else {
wdenk7a8e9bed2003-05-31 18:35:21 +00001014 NanD_ReadBuf(nand, buf + *retlen, nand->oobblock - col);
wdenkdc7c9a12003-03-26 06:55:25 +00001015 *retlen += nand->oobblock - col;
1016 }
1017#endif
1018 /* For subsequent reads align to page boundary. */
1019 col = 0;
1020 /* Increment page address */
1021 page++;
1022 }
1023
1024 /* De-select the NAND device */
wdenk0db5bca2003-03-31 17:27:09 +00001025 NAND_DISABLE_CE(nand); /* set pin high */
wdenkdc7c9a12003-03-26 06:55:25 +00001026
1027 /*
1028 * Return success, if no ECC failures, else -EIO
1029 * fs driver will take care of that, because
1030 * retlen == desired len and result == -EIO
1031 */
1032 return ecc_status ? -1 : 0;
1033}
1034
wdenkdc7c9a12003-03-26 06:55:25 +00001035/*
1036 * Nand_page_program function is used for write and writev !
1037 */
1038static int nand_write_page (struct nand_chip *nand,
1039 int page, int col, int last, u_char * ecc_code)
1040{
1041
1042 int i;
wdenkdc7c9a12003-03-26 06:55:25 +00001043 unsigned long nandptr = nand->IO_ADDR;
wdenk384cc682005-04-03 22:35:21 +00001044
wdenk1f4bb372003-07-27 00:21:01 +00001045#ifdef CONFIG_MTD_NAND_ECC
wdenkdc7c9a12003-03-26 06:55:25 +00001046#ifdef CONFIG_MTD_NAND_VERIFY_WRITE
1047 int ecc_bytes = (nand->oobblock == 512) ? 6 : 3;
1048#endif
1049#endif
1050 /* pad oob area */
1051 for (i = nand->oobblock; i < nand->oobblock + nand->oobsize; i++)
1052 nand->data_buf[i] = 0xff;
1053
1054#ifdef CONFIG_MTD_NAND_ECC
1055 /* Zero out the ECC array */
1056 for (i = 0; i < 6; i++)
1057 ecc_code[i] = 0x00;
1058
1059 /* Read back previous written data, if col > 0 */
1060 if (col) {
wdenk384cc682005-04-03 22:35:21 +00001061 NanD_Command (nand, NAND_CMD_READ0);
1062 if (nand->bus16) {
1063 NanD_Address (nand, ADDR_COLUMN_PAGE,
1064 (page << nand->page_shift) + (col >> 1));
1065 } else {
1066 NanD_Address (nand, ADDR_COLUMN_PAGE,
1067 (page << nand->page_shift) + col);
1068 }
1069
1070 if (nand->bus16) {
1071 u16 val;
1072
1073 for (i = 0; i < col; i += 2) {
1074 val = READ_NAND (nandptr);
1075 nand->data_buf[i] = val & 0xff;
1076 nand->data_buf[i + 1] = val >> 8;
1077 }
1078 } else {
1079 for (i = 0; i < col; i++)
1080 nand->data_buf[i] = READ_NAND (nandptr);
1081 }
wdenkdc7c9a12003-03-26 06:55:25 +00001082 }
1083
1084 /* Calculate and write the ECC if we have enough data */
1085 if ((col < nand->eccsize) && (last >= nand->eccsize)) {
1086 nand_calculate_ecc (&nand->data_buf[0], &(ecc_code[0]));
wdenk384cc682005-04-03 22:35:21 +00001087 for (i = 0; i < 3; i++) {
1088 nand->data_buf[(nand->oobblock +
1089 oob_config.ecc_pos[i])] = ecc_code[i];
1090 }
1091 if (oob_config.eccvalid_pos != -1) {
1092 nand->data_buf[nand->oobblock +
1093 oob_config.eccvalid_pos] = 0xf0;
1094 }
wdenkdc7c9a12003-03-26 06:55:25 +00001095 }
1096
1097 /* Calculate and write the second ECC if we have enough data */
1098 if ((nand->oobblock == 512) && (last == nand->oobblock)) {
1099 nand_calculate_ecc (&nand->data_buf[256], &(ecc_code[3]));
wdenk384cc682005-04-03 22:35:21 +00001100 for (i = 3; i < 6; i++) {
1101 nand->data_buf[(nand->oobblock +
1102 oob_config.ecc_pos[i])] = ecc_code[i];
1103 }
1104 if (oob_config.eccvalid_pos != -1) {
1105 nand->data_buf[nand->oobblock +
1106 oob_config.eccvalid_pos] &= 0x0f;
1107 }
wdenkdc7c9a12003-03-26 06:55:25 +00001108 }
1109#endif
1110 /* Prepad for partial page programming !!! */
1111 for (i = 0; i < col; i++)
1112 nand->data_buf[i] = 0xff;
1113
1114 /* Postpad for partial page programming !!! oob is already padded */
1115 for (i = last; i < nand->oobblock; i++)
1116 nand->data_buf[i] = 0xff;
1117
1118 /* Send command to begin auto page programming */
wdenk384cc682005-04-03 22:35:21 +00001119 NanD_Command (nand, NAND_CMD_READ0);
1120 NanD_Command (nand, NAND_CMD_SEQIN);
1121 if (nand->bus16) {
1122 NanD_Address (nand, ADDR_COLUMN_PAGE,
1123 (page << nand->page_shift) + (col >> 1));
1124 } else {
1125 NanD_Address (nand, ADDR_COLUMN_PAGE,
1126 (page << nand->page_shift) + col);
1127 }
wdenkdc7c9a12003-03-26 06:55:25 +00001128
1129 /* Write out complete page of data */
wdenk384cc682005-04-03 22:35:21 +00001130 if (nand->bus16) {
1131 for (i = 0; i < (nand->oobblock + nand->oobsize); i += 2) {
1132 WRITE_NAND (nand->data_buf[i] +
1133 (nand->data_buf[i + 1] << 8),
1134 nand->IO_ADDR);
1135 }
1136 } else {
1137 for (i = 0; i < (nand->oobblock + nand->oobsize); i++)
1138 WRITE_NAND (nand->data_buf[i], nand->IO_ADDR);
1139 }
wdenkdc7c9a12003-03-26 06:55:25 +00001140
1141 /* Send command to actually program the data */
wdenk384cc682005-04-03 22:35:21 +00001142 NanD_Command (nand, NAND_CMD_PAGEPROG);
1143 NanD_Command (nand, NAND_CMD_STATUS);
wdenk1f4bb372003-07-27 00:21:01 +00001144#ifdef NAND_NO_RB
wdenk384cc682005-04-03 22:35:21 +00001145 {
1146 u_char ret_val;
wdenkdc7c9a12003-03-26 06:55:25 +00001147
wdenk384cc682005-04-03 22:35:21 +00001148 do {
1149 ret_val = READ_NAND (nandptr); /* wait till ready */
1150 } while ((ret_val & 0x40) != 0x40);
wdenk1f4bb372003-07-27 00:21:01 +00001151 }
1152#endif
wdenkdc7c9a12003-03-26 06:55:25 +00001153 /* See if device thinks it succeeded */
wdenk384cc682005-04-03 22:35:21 +00001154 if (READ_NAND (nand->IO_ADDR) & 0x01) {
1155 printf ("%s: Failed write, page 0x%08x, ", __FUNCTION__,
1156 page);
wdenkdc7c9a12003-03-26 06:55:25 +00001157 return -1;
1158 }
1159#ifdef CONFIG_MTD_NAND_VERIFY_WRITE
1160 /*
1161 * The NAND device assumes that it is always writing to
1162 * a cleanly erased page. Hence, it performs its internal
1163 * write verification only on bits that transitioned from
1164 * 1 to 0. The device does NOT verify the whole page on a
1165 * byte by byte basis. It is possible that the page was
1166 * not completely erased or the page is becoming unusable
1167 * due to wear. The read with ECC would catch the error
1168 * later when the ECC page check fails, but we would rather
1169 * catch it early in the page write stage. Better to write
1170 * no data than invalid data.
1171 */
1172
1173 /* Send command to read back the page */
1174 if (col < nand->eccsize)
wdenk384cc682005-04-03 22:35:21 +00001175 NanD_Command (nand, NAND_CMD_READ0);
wdenkdc7c9a12003-03-26 06:55:25 +00001176 else
wdenk384cc682005-04-03 22:35:21 +00001177 NanD_Command (nand, NAND_CMD_READ1);
1178 if (nand->bus16) {
1179 NanD_Address (nand, ADDR_COLUMN_PAGE,
1180 (page << nand->page_shift) + (col >> 1));
1181 } else {
1182 NanD_Address (nand, ADDR_COLUMN_PAGE,
1183 (page << nand->page_shift) + col);
1184 }
wdenkdc7c9a12003-03-26 06:55:25 +00001185
1186 /* Loop through and verify the data */
wdenk384cc682005-04-03 22:35:21 +00001187 if (nand->bus16) {
1188 for (i = col; i < last; i = +2) {
1189 if ((nand->data_buf[i] +
1190 (nand->data_buf[i + 1] << 8)) != READ_NAND (nand->IO_ADDR)) {
1191 printf ("%s: Failed write verify, page 0x%08x ",
1192 __FUNCTION__, page);
1193 return -1;
1194 }
1195 }
1196 } else {
1197 for (i = col; i < last; i++) {
1198 if (nand->data_buf[i] != READ_NAND (nand->IO_ADDR)) {
1199 printf ("%s: Failed write verify, page 0x%08x ",
1200 __FUNCTION__, page);
1201 return -1;
1202 }
wdenkdc7c9a12003-03-26 06:55:25 +00001203 }
1204 }
1205
1206#ifdef CONFIG_MTD_NAND_ECC
1207 /*
1208 * We also want to check that the ECC bytes wrote
1209 * correctly for the same reasons stated above.
1210 */
wdenk384cc682005-04-03 22:35:21 +00001211 NanD_Command (nand, NAND_CMD_READOOB);
1212 if (nand->bus16) {
1213 NanD_Address (nand, ADDR_COLUMN_PAGE,
1214 (page << nand->page_shift) + (col >> 1));
1215 } else {
1216 NanD_Address (nand, ADDR_COLUMN_PAGE,
1217 (page << nand->page_shift) + col);
1218 }
1219 if (nand->bus16) {
1220 for (i = 0; i < nand->oobsize; i += 2) {
1221 val = READ_NAND (nand->IO_ADDR);
1222 nand->data_buf[i] = val & 0xff;
1223 nand->data_buf[i + 1] = val >> 8;
1224 }
1225 } else {
1226 for (i = 0; i < nand->oobsize; i++) {
1227 nand->data_buf[i] = READ_NAND (nand->IO_ADDR);
1228 }
1229 }
wdenkdc7c9a12003-03-26 06:55:25 +00001230 for (i = 0; i < ecc_bytes; i++) {
1231 if ((nand->data_buf[(oob_config.ecc_pos[i])] != ecc_code[i]) && ecc_code[i]) {
wdenk0db5bca2003-03-31 17:27:09 +00001232 printf ("%s: Failed ECC write "
wdenk384cc682005-04-03 22:35:21 +00001233 "verify, page 0x%08x, "
1234 "%6i bytes were succesful\n",
1235 __FUNCTION__, page, i);
wdenkdc7c9a12003-03-26 06:55:25 +00001236 return -1;
1237 }
1238 }
wdenk384cc682005-04-03 22:35:21 +00001239#endif /* CONFIG_MTD_NAND_ECC */
1240#endif /* CONFIG_MTD_NAND_VERIFY_WRITE */
wdenkdc7c9a12003-03-26 06:55:25 +00001241 return 0;
1242}
wdenk0db5bca2003-03-31 17:27:09 +00001243
wdenkdc7c9a12003-03-26 06:55:25 +00001244static int nand_write_ecc (struct nand_chip* nand, size_t to, size_t len,
1245 size_t * retlen, const u_char * buf, u_char * ecc_code)
1246{
1247 int i, page, col, cnt, ret = 0;
1248
1249 /* Do not allow write past end of device */
1250 if ((to + len) > nand->totlen) {
wdenk0db5bca2003-03-31 17:27:09 +00001251 printf ("%s: Attempt to write past end of page\n", __FUNCTION__);
wdenkdc7c9a12003-03-26 06:55:25 +00001252 return -1;
1253 }
1254
1255 /* Shift to get page */
1256 page = ((int) to) >> nand->page_shift;
1257
1258 /* Get the starting column */
1259 col = to & (nand->oobblock - 1);
1260
1261 /* Initialize return length value */
1262 *retlen = 0;
1263
1264 /* Select the NAND device */
wdenk1f4bb372003-07-27 00:21:01 +00001265#ifdef CONFIG_OMAP1510
1266 archflashwp(0,0);
1267#endif
wdenk384cc682005-04-03 22:35:21 +00001268#ifdef CFG_NAND_WP
1269 NAND_WP_OFF();
1270#endif
1271
wdenk1f4bb372003-07-27 00:21:01 +00001272 NAND_ENABLE_CE(nand); /* set pin low */
wdenkdc7c9a12003-03-26 06:55:25 +00001273
1274 /* Check the WP bit */
wdenk0db5bca2003-03-31 17:27:09 +00001275 NanD_Command(nand, NAND_CMD_STATUS);
wdenkdc7c9a12003-03-26 06:55:25 +00001276 if (!(READ_NAND(nand->IO_ADDR) & 0x80)) {
wdenk0db5bca2003-03-31 17:27:09 +00001277 printf ("%s: Device is write protected!!!\n", __FUNCTION__);
wdenkdc7c9a12003-03-26 06:55:25 +00001278 ret = -1;
1279 goto out;
1280 }
1281
1282 /* Loop until all data is written */
1283 while (*retlen < len) {
1284 /* Invalidate cache, if we write to this page */
1285 if (nand->cache_page == page)
1286 nand->cache_page = -1;
1287
1288 /* Write data into buffer */
wdenk384cc682005-04-03 22:35:21 +00001289 if ((col + len) >= nand->oobblock) {
1290 for (i = col, cnt = 0; i < nand->oobblock; i++, cnt++) {
wdenkdc7c9a12003-03-26 06:55:25 +00001291 nand->data_buf[i] = buf[(*retlen + cnt)];
wdenk384cc682005-04-03 22:35:21 +00001292 }
1293 } else {
1294 for (i = col, cnt = 0; cnt < (len - *retlen); i++, cnt++) {
wdenkdc7c9a12003-03-26 06:55:25 +00001295 nand->data_buf[i] = buf[(*retlen + cnt)];
wdenk384cc682005-04-03 22:35:21 +00001296 }
1297 }
wdenkdc7c9a12003-03-26 06:55:25 +00001298 /* We use the same function for write and writev !) */
1299 ret = nand_write_page (nand, page, col, i, ecc_code);
1300 if (ret)
1301 goto out;
1302
1303 /* Next data start at page boundary */
1304 col = 0;
1305
1306 /* Update written bytes count */
1307 *retlen += cnt;
1308
1309 /* Increment page address */
1310 page++;
1311 }
1312
1313 /* Return happy */
1314 *retlen = len;
1315
1316out:
1317 /* De-select the NAND device */
wdenk0db5bca2003-03-31 17:27:09 +00001318 NAND_DISABLE_CE(nand); /* set pin high */
wdenk1f4bb372003-07-27 00:21:01 +00001319#ifdef CONFIG_OMAP1510
1320 archflashwp(0,1);
1321#endif
wdenk384cc682005-04-03 22:35:21 +00001322#ifdef CFG_NAND_WP
1323 NAND_WP_ON();
1324#endif
1325
wdenkdc7c9a12003-03-26 06:55:25 +00001326 return ret;
1327}
1328
wdenk7a8e9bed2003-05-31 18:35:21 +00001329/* read from the 16 bytes of oob data that correspond to a 512 byte
1330 * page or 2 256-byte pages.
wdenkdc7c9a12003-03-26 06:55:25 +00001331 */
wdenkdc7c9a12003-03-26 06:55:25 +00001332static int nand_read_oob(struct nand_chip* nand, size_t ofs, size_t len,
wdenk7a8e9bed2003-05-31 18:35:21 +00001333 size_t * retlen, u_char * buf)
wdenkdc7c9a12003-03-26 06:55:25 +00001334{
wdenk7a8e9bed2003-05-31 18:35:21 +00001335 int len256 = 0;
wdenkdc7c9a12003-03-26 06:55:25 +00001336 struct Nand *mychip;
wdenk0db5bca2003-03-31 17:27:09 +00001337 int ret = 0;
wdenkdc7c9a12003-03-26 06:55:25 +00001338
wdenk7a8e9bed2003-05-31 18:35:21 +00001339 mychip = &nand->chips[ofs >> nand->chipshift];
wdenkdc7c9a12003-03-26 06:55:25 +00001340
1341 /* update address for 2M x 8bit devices. OOB starts on the second */
1342 /* page to maintain compatibility with nand_read_ecc. */
1343 if (nand->page256) {
1344 if (!(ofs & 0x8))
1345 ofs += 0x100;
1346 else
1347 ofs -= 0x8;
1348 }
1349
wdenk7a8e9bed2003-05-31 18:35:21 +00001350 NAND_ENABLE_CE(nand); /* set pin low */
wdenkdc7c9a12003-03-26 06:55:25 +00001351 NanD_Command(nand, NAND_CMD_READOOB);
wdenk384cc682005-04-03 22:35:21 +00001352 if (nand->bus16) {
1353 NanD_Address(nand, ADDR_COLUMN_PAGE,
1354 ((ofs >> nand->page_shift) << nand->page_shift) +
1355 ((ofs & (nand->oobblock - 1)) >> 1));
1356 } else {
1357 NanD_Address(nand, ADDR_COLUMN_PAGE, ofs);
1358 }
wdenkdc7c9a12003-03-26 06:55:25 +00001359
1360 /* treat crossing 8-byte OOB data for 2M x 8bit devices */
1361 /* Note: datasheet says it should automaticaly wrap to the */
1362 /* next OOB block, but it didn't work here. mf. */
1363 if (nand->page256 && ofs + len > (ofs | 0x7) + 1) {
1364 len256 = (ofs | 0x7) + 1 - ofs;
1365 NanD_ReadBuf(nand, buf, len256);
1366
1367 NanD_Command(nand, NAND_CMD_READOOB);
1368 NanD_Address(nand, ADDR_COLUMN_PAGE, ofs & (~0x1ff));
1369 }
1370
1371 NanD_ReadBuf(nand, &buf[len256], len - len256);
1372
1373 *retlen = len;
1374 /* Reading the full OOB data drops us off of the end of the page,
wdenk8bde7f72003-06-27 21:31:46 +00001375 * causing the flash device to go into busy mode, so we need
1376 * to wait until ready 11.4.1 and Toshiba TC58256FT nands */
wdenkdc7c9a12003-03-26 06:55:25 +00001377
wdenk1f4bb372003-07-27 00:21:01 +00001378 ret = NanD_WaitReady(nand, 1);
wdenk8bde7f72003-06-27 21:31:46 +00001379 NAND_DISABLE_CE(nand); /* set pin high */
wdenkdc7c9a12003-03-26 06:55:25 +00001380
1381 return ret;
1382
1383}
wdenk7a8e9bed2003-05-31 18:35:21 +00001384
1385/* write to the 16 bytes of oob data that correspond to a 512 byte
1386 * page or 2 256-byte pages.
1387 */
wdenkdc7c9a12003-03-26 06:55:25 +00001388static int nand_write_oob(struct nand_chip* nand, size_t ofs, size_t len,
1389 size_t * retlen, const u_char * buf)
1390{
1391 int len256 = 0;
wdenk7a8e9bed2003-05-31 18:35:21 +00001392 int i;
wdenkdc7c9a12003-03-26 06:55:25 +00001393 unsigned long nandptr = nand->IO_ADDR;
1394
1395#ifdef PSYCHO_DEBUG
1396 printf("nand_write_oob(%lx, %d): %2.2X %2.2X %2.2X %2.2X ... %2.2X %2.2X .. %2.2X %2.2X\n",
1397 (long)ofs, len, buf[0], buf[1], buf[2], buf[3],
1398 buf[8], buf[9], buf[14],buf[15]);
1399#endif
1400
wdenk7a8e9bed2003-05-31 18:35:21 +00001401 NAND_ENABLE_CE(nand); /* set pin low to enable chip */
1402
wdenkdc7c9a12003-03-26 06:55:25 +00001403 /* Reset the chip */
1404 NanD_Command(nand, NAND_CMD_RESET);
1405
1406 /* issue the Read2 command to set the pointer to the Spare Data Area. */
1407 NanD_Command(nand, NAND_CMD_READOOB);
wdenk384cc682005-04-03 22:35:21 +00001408 if (nand->bus16) {
1409 NanD_Address(nand, ADDR_COLUMN_PAGE,
1410 ((ofs >> nand->page_shift) << nand->page_shift) +
1411 ((ofs & (nand->oobblock - 1)) >> 1));
1412 } else {
1413 NanD_Address(nand, ADDR_COLUMN_PAGE, ofs);
1414 }
wdenkdc7c9a12003-03-26 06:55:25 +00001415
1416 /* update address for 2M x 8bit devices. OOB starts on the second */
1417 /* page to maintain compatibility with nand_read_ecc. */
1418 if (nand->page256) {
1419 if (!(ofs & 0x8))
1420 ofs += 0x100;
1421 else
1422 ofs -= 0x8;
1423 }
1424
1425 /* issue the Serial Data In command to initial the Page Program process */
1426 NanD_Command(nand, NAND_CMD_SEQIN);
wdenk384cc682005-04-03 22:35:21 +00001427 if (nand->bus16) {
1428 NanD_Address(nand, ADDR_COLUMN_PAGE,
1429 ((ofs >> nand->page_shift) << nand->page_shift) +
1430 ((ofs & (nand->oobblock - 1)) >> 1));
1431 } else {
1432 NanD_Address(nand, ADDR_COLUMN_PAGE, ofs);
1433 }
wdenkdc7c9a12003-03-26 06:55:25 +00001434
1435 /* treat crossing 8-byte OOB data for 2M x 8bit devices */
1436 /* Note: datasheet says it should automaticaly wrap to the */
1437 /* next OOB block, but it didn't work here. mf. */
1438 if (nand->page256 && ofs + len > (ofs | 0x7) + 1) {
1439 len256 = (ofs | 0x7) + 1 - ofs;
wdenk7a8e9bed2003-05-31 18:35:21 +00001440 for (i = 0; i < len256; i++)
1441 WRITE_NAND(buf[i], nandptr);
wdenkdc7c9a12003-03-26 06:55:25 +00001442
1443 NanD_Command(nand, NAND_CMD_PAGEPROG);
1444 NanD_Command(nand, NAND_CMD_STATUS);
wdenk1f4bb372003-07-27 00:21:01 +00001445#ifdef NAND_NO_RB
1446 { u_char ret_val;
wdenk384cc682005-04-03 22:35:21 +00001447 do {
1448 ret_val = READ_NAND(nandptr); /* wait till ready */
1449 } while ((ret_val & 0x40) != 0x40);
wdenk1f4bb372003-07-27 00:21:01 +00001450 }
1451#endif
wdenkdc7c9a12003-03-26 06:55:25 +00001452 if (READ_NAND(nandptr) & 1) {
1453 puts ("Error programming oob data\n");
1454 /* There was an error */
wdenk7a8e9bed2003-05-31 18:35:21 +00001455 NAND_DISABLE_CE(nand); /* set pin high */
wdenkdc7c9a12003-03-26 06:55:25 +00001456 *retlen = 0;
1457 return -1;
1458 }
1459 NanD_Command(nand, NAND_CMD_SEQIN);
1460 NanD_Address(nand, ADDR_COLUMN_PAGE, ofs & (~0x1ff));
1461 }
1462
wdenk384cc682005-04-03 22:35:21 +00001463 if (nand->bus16) {
1464 for (i = len256; i < len; i += 2) {
1465 WRITE_NAND(buf[i] + (buf[i+1] << 8), nandptr);
1466 }
1467 } else {
1468 for (i = len256; i < len; i++)
1469 WRITE_NAND(buf[i], nandptr);
1470 }
wdenkdc7c9a12003-03-26 06:55:25 +00001471
1472 NanD_Command(nand, NAND_CMD_PAGEPROG);
1473 NanD_Command(nand, NAND_CMD_STATUS);
wdenk1f4bb372003-07-27 00:21:01 +00001474#ifdef NAND_NO_RB
wdenk384cc682005-04-03 22:35:21 +00001475 { u_char ret_val;
1476 do {
1477 ret_val = READ_NAND(nandptr); /* wait till ready */
1478 } while ((ret_val & 0x40) != 0x40);
wdenk1f4bb372003-07-27 00:21:01 +00001479 }
1480#endif
wdenkdc7c9a12003-03-26 06:55:25 +00001481 if (READ_NAND(nandptr) & 1) {
1482 puts ("Error programming oob data\n");
1483 /* There was an error */
wdenk7a8e9bed2003-05-31 18:35:21 +00001484 NAND_DISABLE_CE(nand); /* set pin high */
wdenkdc7c9a12003-03-26 06:55:25 +00001485 *retlen = 0;
1486 return -1;
1487 }
1488
wdenk7a8e9bed2003-05-31 18:35:21 +00001489 NAND_DISABLE_CE(nand); /* set pin high */
wdenkdc7c9a12003-03-26 06:55:25 +00001490 *retlen = len;
1491 return 0;
1492
1493}
wdenkdc7c9a12003-03-26 06:55:25 +00001494
wdenk13a56952004-06-09 14:58:14 +00001495int nand_erase(struct nand_chip* nand, size_t ofs, size_t len, int clean)
wdenkdc7c9a12003-03-26 06:55:25 +00001496{
wdenk7a8e9bed2003-05-31 18:35:21 +00001497 /* This is defined as a structure so it will work on any system
1498 * using native endian jffs2 (the default).
1499 */
1500 static struct jffs2_unknown_node clean_marker = {
1501 JFFS2_MAGIC_BITMASK,
1502 JFFS2_NODETYPE_CLEANMARKER,
1503 8 /* 8 bytes in this node */
1504 };
wdenkdc7c9a12003-03-26 06:55:25 +00001505 unsigned long nandptr;
1506 struct Nand *mychip;
wdenk85ec0bc2003-03-31 16:34:49 +00001507 int ret = 0;
wdenkdc7c9a12003-03-26 06:55:25 +00001508
1509 if (ofs & (nand->erasesize-1) || len & (nand->erasesize-1)) {
1510 printf ("Offset and size must be sector aligned, erasesize = %d\n",
wdenk8bde7f72003-06-27 21:31:46 +00001511 (int) nand->erasesize);
wdenkdc7c9a12003-03-26 06:55:25 +00001512 return -1;
1513 }
1514
1515 nandptr = nand->IO_ADDR;
1516
wdenk85ec0bc2003-03-31 16:34:49 +00001517 /* Select the NAND device */
wdenk1f4bb372003-07-27 00:21:01 +00001518#ifdef CONFIG_OMAP1510
1519 archflashwp(0,0);
1520#endif
wdenk384cc682005-04-03 22:35:21 +00001521#ifdef CFG_NAND_WP
1522 NAND_WP_OFF();
1523#endif
wdenk1f4bb372003-07-27 00:21:01 +00001524 NAND_ENABLE_CE(nand); /* set pin low */
wdenk85ec0bc2003-03-31 16:34:49 +00001525
1526 /* Check the WP bit */
1527 NanD_Command(nand, NAND_CMD_STATUS);
1528 if (!(READ_NAND(nand->IO_ADDR) & 0x80)) {
1529 printf ("nand_write_ecc: Device is write protected!!!\n");
1530 ret = -1;
1531 goto out;
1532 }
1533
wdenk0db5bca2003-03-31 17:27:09 +00001534 /* Check the WP bit */
1535 NanD_Command(nand, NAND_CMD_STATUS);
1536 if (!(READ_NAND(nand->IO_ADDR) & 0x80)) {
1537 printf ("%s: Device is write protected!!!\n", __FUNCTION__);
1538 ret = -1;
1539 goto out;
1540 }
1541
wdenkdc7c9a12003-03-26 06:55:25 +00001542 /* FIXME: Do nand in the background. Use timers or schedule_task() */
1543 while(len) {
wdenk0db5bca2003-03-31 17:27:09 +00001544 /*mychip = &nand->chips[shr(ofs, nand->chipshift)];*/
1545 mychip = &nand->chips[ofs >> nand->chipshift];
wdenkdc7c9a12003-03-26 06:55:25 +00001546
wdenk7a8e9bed2003-05-31 18:35:21 +00001547 /* always check for bad block first, genuine bad blocks
1548 * should _never_ be erased.
1549 */
1550 if (ALLOW_ERASE_BAD_DEBUG || !check_block(nand, ofs)) {
1551 /* Select the NAND device */
1552 NAND_ENABLE_CE(nand); /* set pin low */
wdenkdc7c9a12003-03-26 06:55:25 +00001553
wdenk7a8e9bed2003-05-31 18:35:21 +00001554 NanD_Command(nand, NAND_CMD_ERASE1);
1555 NanD_Address(nand, ADDR_PAGE, ofs);
1556 NanD_Command(nand, NAND_CMD_ERASE2);
wdenkdc7c9a12003-03-26 06:55:25 +00001557
wdenk7a8e9bed2003-05-31 18:35:21 +00001558 NanD_Command(nand, NAND_CMD_STATUS);
1559
wdenk1f4bb372003-07-27 00:21:01 +00001560#ifdef NAND_NO_RB
wdenk384cc682005-04-03 22:35:21 +00001561 { u_char ret_val;
1562 do {
1563 ret_val = READ_NAND(nandptr); /* wait till ready */
1564 } while ((ret_val & 0x40) != 0x40);
wdenk1f4bb372003-07-27 00:21:01 +00001565 }
1566#endif
wdenk7a8e9bed2003-05-31 18:35:21 +00001567 if (READ_NAND(nandptr) & 1) {
1568 printf ("%s: Error erasing at 0x%lx\n",
1569 __FUNCTION__, (long)ofs);
1570 /* There was an error */
1571 ret = -1;
1572 goto out;
1573 }
1574 if (clean) {
1575 int n; /* return value not used */
1576 int p, l;
1577
1578 /* clean marker position and size depend
1579 * on the page size, since 256 byte pages
1580 * only have 8 bytes of oob data
1581 */
1582 if (nand->page256) {
1583 p = NAND_JFFS2_OOB8_FSDAPOS;
1584 l = NAND_JFFS2_OOB8_FSDALEN;
wdenk384cc682005-04-03 22:35:21 +00001585 } else {
wdenk7a8e9bed2003-05-31 18:35:21 +00001586 p = NAND_JFFS2_OOB16_FSDAPOS;
1587 l = NAND_JFFS2_OOB16_FSDALEN;
1588 }
1589
1590 ret = nand_write_oob(nand, ofs + p, l, &n,
1591 (u_char *)&clean_marker);
1592 /* quit here if write failed */
1593 if (ret)
1594 goto out;
1595 }
wdenkdc7c9a12003-03-26 06:55:25 +00001596 }
1597 ofs += nand->erasesize;
1598 len -= nand->erasesize;
1599 }
1600
wdenk85ec0bc2003-03-31 16:34:49 +00001601out:
1602 /* De-select the NAND device */
1603 NAND_DISABLE_CE(nand); /* set pin high */
wdenk1f4bb372003-07-27 00:21:01 +00001604#ifdef CONFIG_OMAP1510
1605 archflashwp(0,1);
1606#endif
wdenk384cc682005-04-03 22:35:21 +00001607#ifdef CFG_NAND_WP
1608 NAND_WP_ON();
1609#endif
1610
wdenk85ec0bc2003-03-31 16:34:49 +00001611 return ret;
wdenkdc7c9a12003-03-26 06:55:25 +00001612}
1613
1614static inline int nandcheck(unsigned long potential, unsigned long physadr)
1615{
wdenkdc7c9a12003-03-26 06:55:25 +00001616 return 0;
1617}
1618
wdenka43278a2003-09-11 19:48:06 +00001619unsigned long nand_probe(unsigned long physadr)
wdenkdc7c9a12003-03-26 06:55:25 +00001620{
1621 struct nand_chip *nand = NULL;
1622 int i = 0, ChipID = 1;
1623
1624#ifdef CONFIG_MTD_NAND_ECC_JFFS2
1625 oob_config.ecc_pos[0] = NAND_JFFS2_OOB_ECCPOS0;
1626 oob_config.ecc_pos[1] = NAND_JFFS2_OOB_ECCPOS1;
1627 oob_config.ecc_pos[2] = NAND_JFFS2_OOB_ECCPOS2;
1628 oob_config.ecc_pos[3] = NAND_JFFS2_OOB_ECCPOS3;
1629 oob_config.ecc_pos[4] = NAND_JFFS2_OOB_ECCPOS4;
1630 oob_config.ecc_pos[5] = NAND_JFFS2_OOB_ECCPOS5;
wdenkdc7c9a12003-03-26 06:55:25 +00001631 oob_config.eccvalid_pos = 4;
1632#else
1633 oob_config.ecc_pos[0] = NAND_NOOB_ECCPOS0;
1634 oob_config.ecc_pos[1] = NAND_NOOB_ECCPOS1;
1635 oob_config.ecc_pos[2] = NAND_NOOB_ECCPOS2;
1636 oob_config.ecc_pos[3] = NAND_NOOB_ECCPOS3;
1637 oob_config.ecc_pos[4] = NAND_NOOB_ECCPOS4;
1638 oob_config.ecc_pos[5] = NAND_NOOB_ECCPOS5;
wdenkdc7c9a12003-03-26 06:55:25 +00001639 oob_config.eccvalid_pos = NAND_NOOB_ECCVPOS;
1640#endif
wdenk7a8e9bed2003-05-31 18:35:21 +00001641 oob_config.badblock_pos = 5;
wdenkdc7c9a12003-03-26 06:55:25 +00001642
1643 for (i=0; i<CFG_MAX_NAND_DEVICE; i++) {
1644 if (nand_dev_desc[i].ChipID == NAND_ChipID_UNKNOWN) {
wdenka43278a2003-09-11 19:48:06 +00001645 nand = &nand_dev_desc[i];
wdenkdc7c9a12003-03-26 06:55:25 +00001646 break;
1647 }
1648 }
wdenka43278a2003-09-11 19:48:06 +00001649 if (!nand)
1650 return (0);
wdenkdc7c9a12003-03-26 06:55:25 +00001651
wdenk7a8e9bed2003-05-31 18:35:21 +00001652 memset((char *)nand, 0, sizeof(struct nand_chip));
1653
1654 nand->IO_ADDR = physadr;
1655 nand->cache_page = -1; /* init the cache page */
1656 NanD_ScanChips(nand);
1657
1658 if (nand->totlen == 0) {
1659 /* no chips found, clean up and quit */
1660 memset((char *)nand, 0, sizeof(struct nand_chip));
1661 nand->ChipID = NAND_ChipID_UNKNOWN;
wdenka43278a2003-09-11 19:48:06 +00001662 return (0);
wdenk7a8e9bed2003-05-31 18:35:21 +00001663 }
1664
1665 nand->ChipID = ChipID;
wdenk0db5bca2003-03-31 17:27:09 +00001666 if (curr_device == -1)
1667 curr_device = i;
wdenkdc7c9a12003-03-26 06:55:25 +00001668
wdenk0db5bca2003-03-31 17:27:09 +00001669 nand->data_buf = malloc (nand->oobblock + nand->oobsize);
1670 if (!nand->data_buf) {
1671 puts ("Cannot allocate memory for data structures.\n");
wdenka43278a2003-09-11 19:48:06 +00001672 return (0);
wdenk0db5bca2003-03-31 17:27:09 +00001673 }
wdenka43278a2003-09-11 19:48:06 +00001674
1675 return (nand->totlen);
wdenkdc7c9a12003-03-26 06:55:25 +00001676}
1677
1678#ifdef CONFIG_MTD_NAND_ECC
1679/*
1680 * Pre-calculated 256-way 1 byte column parity
1681 */
1682static const u_char nand_ecc_precalc_table[] = {
wdenk384cc682005-04-03 22:35:21 +00001683 0x00, 0x55, 0x56, 0x03, 0x59, 0x0c, 0x0f, 0x5a,
1684 0x5a, 0x0f, 0x0c, 0x59, 0x03, 0x56, 0x55, 0x00,
1685 0x65, 0x30, 0x33, 0x66, 0x3c, 0x69, 0x6a, 0x3f,
1686 0x3f, 0x6a, 0x69, 0x3c, 0x66, 0x33, 0x30, 0x65,
1687 0x66, 0x33, 0x30, 0x65, 0x3f, 0x6a, 0x69, 0x3c,
1688 0x3c, 0x69, 0x6a, 0x3f, 0x65, 0x30, 0x33, 0x66,
1689 0x03, 0x56, 0x55, 0x00, 0x5a, 0x0f, 0x0c, 0x59,
1690 0x59, 0x0c, 0x0f, 0x5a, 0x00, 0x55, 0x56, 0x03,
1691 0x69, 0x3c, 0x3f, 0x6a, 0x30, 0x65, 0x66, 0x33,
1692 0x33, 0x66, 0x65, 0x30, 0x6a, 0x3f, 0x3c, 0x69,
1693 0x0c, 0x59, 0x5a, 0x0f, 0x55, 0x00, 0x03, 0x56,
1694 0x56, 0x03, 0x00, 0x55, 0x0f, 0x5a, 0x59, 0x0c,
1695 0x0f, 0x5a, 0x59, 0x0c, 0x56, 0x03, 0x00, 0x55,
1696 0x55, 0x00, 0x03, 0x56, 0x0c, 0x59, 0x5a, 0x0f,
1697 0x6a, 0x3f, 0x3c, 0x69, 0x33, 0x66, 0x65, 0x30,
1698 0x30, 0x65, 0x66, 0x33, 0x69, 0x3c, 0x3f, 0x6a,
1699 0x6a, 0x3f, 0x3c, 0x69, 0x33, 0x66, 0x65, 0x30,
1700 0x30, 0x65, 0x66, 0x33, 0x69, 0x3c, 0x3f, 0x6a,
1701 0x0f, 0x5a, 0x59, 0x0c, 0x56, 0x03, 0x00, 0x55,
1702 0x55, 0x00, 0x03, 0x56, 0x0c, 0x59, 0x5a, 0x0f,
1703 0x0c, 0x59, 0x5a, 0x0f, 0x55, 0x00, 0x03, 0x56,
1704 0x56, 0x03, 0x00, 0x55, 0x0f, 0x5a, 0x59, 0x0c,
1705 0x69, 0x3c, 0x3f, 0x6a, 0x30, 0x65, 0x66, 0x33,
1706 0x33, 0x66, 0x65, 0x30, 0x6a, 0x3f, 0x3c, 0x69,
1707 0x03, 0x56, 0x55, 0x00, 0x5a, 0x0f, 0x0c, 0x59,
1708 0x59, 0x0c, 0x0f, 0x5a, 0x00, 0x55, 0x56, 0x03,
1709 0x66, 0x33, 0x30, 0x65, 0x3f, 0x6a, 0x69, 0x3c,
1710 0x3c, 0x69, 0x6a, 0x3f, 0x65, 0x30, 0x33, 0x66,
1711 0x65, 0x30, 0x33, 0x66, 0x3c, 0x69, 0x6a, 0x3f,
1712 0x3f, 0x6a, 0x69, 0x3c, 0x66, 0x33, 0x30, 0x65,
1713 0x00, 0x55, 0x56, 0x03, 0x59, 0x0c, 0x0f, 0x5a,
1714 0x5a, 0x0f, 0x0c, 0x59, 0x03, 0x56, 0x55, 0x00
wdenkdc7c9a12003-03-26 06:55:25 +00001715};
1716
1717
1718/*
1719 * Creates non-inverted ECC code from line parity
1720 */
1721static void nand_trans_result(u_char reg2, u_char reg3,
1722 u_char *ecc_code)
1723{
1724 u_char a, b, i, tmp1, tmp2;
1725
1726 /* Initialize variables */
1727 a = b = 0x80;
1728 tmp1 = tmp2 = 0;
1729
1730 /* Calculate first ECC byte */
1731 for (i = 0; i < 4; i++) {
1732 if (reg3 & a) /* LP15,13,11,9 --> ecc_code[0] */
1733 tmp1 |= b;
1734 b >>= 1;
1735 if (reg2 & a) /* LP14,12,10,8 --> ecc_code[0] */
1736 tmp1 |= b;
1737 b >>= 1;
1738 a >>= 1;
1739 }
1740
1741 /* Calculate second ECC byte */
1742 b = 0x80;
1743 for (i = 0; i < 4; i++) {
1744 if (reg3 & a) /* LP7,5,3,1 --> ecc_code[1] */
1745 tmp2 |= b;
1746 b >>= 1;
1747 if (reg2 & a) /* LP6,4,2,0 --> ecc_code[1] */
1748 tmp2 |= b;
1749 b >>= 1;
1750 a >>= 1;
1751 }
1752
1753 /* Store two of the ECC bytes */
1754 ecc_code[0] = tmp1;
1755 ecc_code[1] = tmp2;
1756}
1757
1758/*
1759 * Calculate 3 byte ECC code for 256 byte block
1760 */
1761static void nand_calculate_ecc (const u_char *dat, u_char *ecc_code)
1762{
wdenk7a8e9bed2003-05-31 18:35:21 +00001763 u_char idx, reg1, reg3;
wdenkdc7c9a12003-03-26 06:55:25 +00001764 int j;
1765
1766 /* Initialize variables */
wdenk7a8e9bed2003-05-31 18:35:21 +00001767 reg1 = reg3 = 0;
wdenkdc7c9a12003-03-26 06:55:25 +00001768 ecc_code[0] = ecc_code[1] = ecc_code[2] = 0;
1769
1770 /* Build up column parity */
1771 for(j = 0; j < 256; j++) {
1772
1773 /* Get CP0 - CP5 from table */
1774 idx = nand_ecc_precalc_table[dat[j]];
wdenk7a8e9bed2003-05-31 18:35:21 +00001775 reg1 ^= idx;
wdenkdc7c9a12003-03-26 06:55:25 +00001776
1777 /* All bit XOR = 1 ? */
1778 if (idx & 0x40) {
1779 reg3 ^= (u_char) j;
wdenkdc7c9a12003-03-26 06:55:25 +00001780 }
1781 }
1782
1783 /* Create non-inverted ECC code from line parity */
wdenk7a8e9bed2003-05-31 18:35:21 +00001784 nand_trans_result((reg1 & 0x40) ? ~reg3 : reg3, reg3, ecc_code);
wdenkdc7c9a12003-03-26 06:55:25 +00001785
1786 /* Calculate final ECC code */
1787 ecc_code[0] = ~ecc_code[0];
1788 ecc_code[1] = ~ecc_code[1];
1789 ecc_code[2] = ((~reg1) << 2) | 0x03;
1790}
1791
1792/*
1793 * Detect and correct a 1 bit error for 256 byte block
1794 */
1795static int nand_correct_data (u_char *dat, u_char *read_ecc, u_char *calc_ecc)
1796{
1797 u_char a, b, c, d1, d2, d3, add, bit, i;
1798
1799 /* Do error detection */
1800 d1 = calc_ecc[0] ^ read_ecc[0];
1801 d2 = calc_ecc[1] ^ read_ecc[1];
1802 d3 = calc_ecc[2] ^ read_ecc[2];
1803
1804 if ((d1 | d2 | d3) == 0) {
1805 /* No errors */
1806 return 0;
wdenk384cc682005-04-03 22:35:21 +00001807 } else {
wdenkdc7c9a12003-03-26 06:55:25 +00001808 a = (d1 ^ (d1 >> 1)) & 0x55;
1809 b = (d2 ^ (d2 >> 1)) & 0x55;
1810 c = (d3 ^ (d3 >> 1)) & 0x54;
1811
1812 /* Found and will correct single bit error in the data */
1813 if ((a == 0x55) && (b == 0x55) && (c == 0x54)) {
1814 c = 0x80;
1815 add = 0;
1816 a = 0x80;
1817 for (i=0; i<4; i++) {
1818 if (d1 & c)
1819 add |= a;
1820 c >>= 2;
1821 a >>= 1;
1822 }
1823 c = 0x80;
1824 for (i=0; i<4; i++) {
1825 if (d2 & c)
1826 add |= a;
1827 c >>= 2;
1828 a >>= 1;
1829 }
1830 bit = 0;
1831 b = 0x04;
1832 c = 0x80;
1833 for (i=0; i<3; i++) {
1834 if (d3 & c)
1835 bit |= b;
1836 c >>= 2;
1837 b >>= 1;
1838 }
1839 b = 0x01;
1840 a = dat[add];
1841 a ^= (b << bit);
1842 dat[add] = a;
1843 return 1;
1844 }
1845 else {
1846 i = 0;
1847 while (d1) {
1848 if (d1 & 0x01)
1849 ++i;
1850 d1 >>= 1;
1851 }
1852 while (d2) {
1853 if (d2 & 0x01)
1854 ++i;
1855 d2 >>= 1;
1856 }
1857 while (d3) {
1858 if (d3 & 0x01)
1859 ++i;
1860 d3 >>= 1;
1861 }
1862 if (i == 1) {
1863 /* ECC Code Error Correction */
1864 read_ecc[0] = calc_ecc[0];
1865 read_ecc[1] = calc_ecc[1];
1866 read_ecc[2] = calc_ecc[2];
1867 return 2;
1868 }
1869 else {
1870 /* Uncorrectable Error */
1871 return -1;
1872 }
1873 }
1874 }
1875
1876 /* Should never happen */
1877 return -1;
1878}
wdenk1f4bb372003-07-27 00:21:01 +00001879
wdenkdc7c9a12003-03-26 06:55:25 +00001880#endif
wdenk998eaae2004-04-18 19:43:36 +00001881
1882#ifdef CONFIG_JFFS2_NAND
1883
1884int read_jffs2_nand(size_t start, size_t len,
1885 size_t * retlen, u_char * buf, int nanddev)
1886{
1887 return nand_rw(nand_dev_desc + nanddev, NANDRW_READ | NANDRW_JFFS2,
1888 start, len, retlen, buf);
1889}
1890
1891#endif /* CONFIG_JFFS2_NAND */
1892
1893
wdenkdc7c9a12003-03-26 06:55:25 +00001894#endif /* (CONFIG_COMMANDS & CFG_CMD_NAND) */