]> git.sur5r.net Git - openocd/blob - src/flash/nand.c
Add nand_fileio_* helper APIs.
[openocd] / src / flash / nand.c
1 /***************************************************************************
2  *   Copyright (C) 2007 by Dominic Rath                                    *
3  *   Dominic.Rath@gmx.de                                                   *
4  *                                                                         *
5  *   Partially based on drivers/mtd/nand_ids.c from Linux.                 *
6  *   Copyright (C) 2002 Thomas Gleixner <tglx@linutronix.de>               *
7  *                                                                         *
8  *   This program is free software; you can redistribute it and/or modify  *
9  *   it under the terms of the GNU General Public License as published by  *
10  *   the Free Software Foundation; either version 2 of the License, or     *
11  *   (at your option) any later version.                                   *
12  *                                                                         *
13  *   This program is distributed in the hope that it will be useful,       *
14  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
15  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
16  *   GNU General Public License for more details.                          *
17  *                                                                         *
18  *   You should have received a copy of the GNU General Public License     *
19  *   along with this program; if not, write to the                         *
20  *   Free Software Foundation, Inc.,                                       *
21  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
22  ***************************************************************************/
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26
27 #include "nand.h"
28 #include "time_support.h"
29 #include "fileio.h"
30
31 static int nand_read_page(struct nand_device *nand, uint32_t page, uint8_t *data, uint32_t data_size, uint8_t *oob, uint32_t oob_size);
32 //static int nand_read_plain(struct nand_device *nand, uint32_t address, uint8_t *data, uint32_t data_size);
33
34 static int nand_write_page(struct nand_device *nand, uint32_t page, uint8_t *data, uint32_t data_size, uint8_t *oob, uint32_t oob_size);
35
36 /* NAND flash controller
37  */
38 extern struct nand_flash_controller davinci_nand_controller;
39 extern struct nand_flash_controller lpc3180_nand_controller;
40 extern struct nand_flash_controller orion_nand_controller;
41 extern struct nand_flash_controller s3c2410_nand_controller;
42 extern struct nand_flash_controller s3c2412_nand_controller;
43 extern struct nand_flash_controller s3c2440_nand_controller;
44 extern struct nand_flash_controller s3c2443_nand_controller;
45 extern struct nand_flash_controller imx31_nand_flash_controller;
46
47 /* extern struct nand_flash_controller boundary_scan_nand_controller; */
48
49 static struct nand_flash_controller *nand_flash_controllers[] =
50 {
51         &davinci_nand_controller,
52         &lpc3180_nand_controller,
53         &orion_nand_controller,
54         &s3c2410_nand_controller,
55         &s3c2412_nand_controller,
56         &s3c2440_nand_controller,
57         &s3c2443_nand_controller,
58         &imx31_nand_flash_controller,
59 /*      &boundary_scan_nand_controller, */
60         NULL
61 };
62
63 /* configured NAND devices and NAND Flash command handler */
64 static struct nand_device *nand_devices = NULL;
65 static struct command *nand_cmd;
66
67 /*      Chip ID list
68  *
69  *      Name, ID code, pagesize, chipsize in MegaByte, eraseblock size,
70  *      options
71  *
72  *      Pagesize; 0, 256, 512
73  *      0       get this information from the extended chip ID
74  *      256     256 Byte page size
75  *      512     512 Byte page size
76  */
77 static struct nand_info nand_flash_ids[] =
78 {
79         /* start "museum" IDs */
80         {"NAND 1MiB 5V 8-bit",          0x6e, 256, 1, 0x1000, 0},
81         {"NAND 2MiB 5V 8-bit",          0x64, 256, 2, 0x1000, 0},
82         {"NAND 4MiB 5V 8-bit",          0x6b, 512, 4, 0x2000, 0},
83         {"NAND 1MiB 3,3V 8-bit",        0xe8, 256, 1, 0x1000, 0},
84         {"NAND 1MiB 3,3V 8-bit",        0xec, 256, 1, 0x1000, 0},
85         {"NAND 2MiB 3,3V 8-bit",        0xea, 256, 2, 0x1000, 0},
86         {"NAND 4MiB 3,3V 8-bit",        0xd5, 512, 4, 0x2000, 0},
87         {"NAND 4MiB 3,3V 8-bit",        0xe3, 512, 4, 0x2000, 0},
88         {"NAND 4MiB 3,3V 8-bit",        0xe5, 512, 4, 0x2000, 0},
89         {"NAND 8MiB 3,3V 8-bit",        0xd6, 512, 8, 0x2000, 0},
90
91         {"NAND 8MiB 1,8V 8-bit",        0x39, 512, 8, 0x2000, 0},
92         {"NAND 8MiB 3,3V 8-bit",        0xe6, 512, 8, 0x2000, 0},
93         {"NAND 8MiB 1,8V 16-bit",       0x49, 512, 8, 0x2000, NAND_BUSWIDTH_16},
94         {"NAND 8MiB 3,3V 16-bit",       0x59, 512, 8, 0x2000, NAND_BUSWIDTH_16},
95         /* end "museum" IDs */
96
97         {"NAND 16MiB 1,8V 8-bit",       0x33, 512, 16, 0x4000, 0},
98         {"NAND 16MiB 3,3V 8-bit",       0x73, 512, 16, 0x4000, 0},
99         {"NAND 16MiB 1,8V 16-bit",      0x43, 512, 16, 0x4000, NAND_BUSWIDTH_16},
100         {"NAND 16MiB 3,3V 16-bit",      0x53, 512, 16, 0x4000, NAND_BUSWIDTH_16},
101
102         {"NAND 32MiB 1,8V 8-bit",       0x35, 512, 32, 0x4000, 0},
103         {"NAND 32MiB 3,3V 8-bit",       0x75, 512, 32, 0x4000, 0},
104         {"NAND 32MiB 1,8V 16-bit",      0x45, 512, 32, 0x4000, NAND_BUSWIDTH_16},
105         {"NAND 32MiB 3,3V 16-bit",      0x55, 512, 32, 0x4000, NAND_BUSWIDTH_16},
106
107         {"NAND 64MiB 1,8V 8-bit",       0x36, 512, 64, 0x4000, 0},
108         {"NAND 64MiB 3,3V 8-bit",       0x76, 512, 64, 0x4000, 0},
109         {"NAND 64MiB 1,8V 16-bit",      0x46, 512, 64, 0x4000, NAND_BUSWIDTH_16},
110         {"NAND 64MiB 3,3V 16-bit",      0x56, 512, 64, 0x4000, NAND_BUSWIDTH_16},
111
112         {"NAND 128MiB 1,8V 8-bit",      0x78, 512, 128, 0x4000, 0},
113         {"NAND 128MiB 1,8V 8-bit",      0x39, 512, 128, 0x4000, 0},
114         {"NAND 128MiB 3,3V 8-bit",      0x79, 512, 128, 0x4000, 0},
115         {"NAND 128MiB 1,8V 16-bit",     0x72, 512, 128, 0x4000, NAND_BUSWIDTH_16},
116         {"NAND 128MiB 1,8V 16-bit",     0x49, 512, 128, 0x4000, NAND_BUSWIDTH_16},
117         {"NAND 128MiB 3,3V 16-bit",     0x74, 512, 128, 0x4000, NAND_BUSWIDTH_16},
118         {"NAND 128MiB 3,3V 16-bit",     0x59, 512, 128, 0x4000, NAND_BUSWIDTH_16},
119
120         {"NAND 256MiB 3,3V 8-bit",      0x71, 512, 256, 0x4000, 0},
121
122         {"NAND 64MiB 1,8V 8-bit",       0xA2, 0,  64, 0, LP_OPTIONS},
123         {"NAND 64MiB 3,3V 8-bit",       0xF2, 0,  64, 0, LP_OPTIONS},
124         {"NAND 64MiB 1,8V 16-bit",      0xB2, 0,  64, 0, LP_OPTIONS16},
125         {"NAND 64MiB 3,3V 16-bit",      0xC2, 0,  64, 0, LP_OPTIONS16},
126
127         {"NAND 128MiB 1,8V 8-bit",      0xA1, 0, 128, 0, LP_OPTIONS},
128         {"NAND 128MiB 3,3V 8-bit",      0xF1, 0, 128, 0, LP_OPTIONS},
129         {"NAND 128MiB 1,8V 16-bit",     0xB1, 0, 128, 0, LP_OPTIONS16},
130         {"NAND 128MiB 3,3V 16-bit",     0xC1, 0, 128, 0, LP_OPTIONS16},
131
132         {"NAND 256MiB 1,8V 8-bit",      0xAA, 0, 256, 0, LP_OPTIONS},
133         {"NAND 256MiB 3,3V 8-bit",      0xDA, 0, 256, 0, LP_OPTIONS},
134         {"NAND 256MiB 1,8V 16-bit",     0xBA, 0, 256, 0, LP_OPTIONS16},
135         {"NAND 256MiB 3,3V 16-bit",     0xCA, 0, 256, 0, LP_OPTIONS16},
136
137         {"NAND 512MiB 1,8V 8-bit",      0xAC, 0, 512, 0, LP_OPTIONS},
138         {"NAND 512MiB 3,3V 8-bit",      0xDC, 0, 512, 0, LP_OPTIONS},
139         {"NAND 512MiB 1,8V 16-bit",     0xBC, 0, 512, 0, LP_OPTIONS16},
140         {"NAND 512MiB 3,3V 16-bit",     0xCC, 0, 512, 0, LP_OPTIONS16},
141
142         {"NAND 1GiB 1,8V 8-bit",        0xA3, 0, 1024, 0, LP_OPTIONS},
143         {"NAND 1GiB 3,3V 8-bit",        0xD3, 0, 1024, 0, LP_OPTIONS},
144         {"NAND 1GiB 1,8V 16-bit",       0xB3, 0, 1024, 0, LP_OPTIONS16},
145         {"NAND 1GiB 3,3V 16-bit",       0xC3, 0, 1024, 0, LP_OPTIONS16},
146
147         {"NAND 2GiB 1,8V 8-bit",        0xA5, 0, 2048, 0, LP_OPTIONS},
148         {"NAND 2GiB 3,3V 8-bit",        0xD5, 0, 2048, 0, LP_OPTIONS},
149         {"NAND 2GiB 1,8V 16-bit",       0xB5, 0, 2048, 0, LP_OPTIONS16},
150         {"NAND 2GiB 3,3V 16-bit",       0xC5, 0, 2048, 0, LP_OPTIONS16},
151
152         {NULL, 0, 0, 0, 0, 0 }
153 };
154
155 /* Manufacturer ID list
156  */
157 static struct nand_manufacturer nand_manuf_ids[] =
158 {
159         {0x0, "unknown"},
160         {NAND_MFR_TOSHIBA, "Toshiba"},
161         {NAND_MFR_SAMSUNG, "Samsung"},
162         {NAND_MFR_FUJITSU, "Fujitsu"},
163         {NAND_MFR_NATIONAL, "National"},
164         {NAND_MFR_RENESAS, "Renesas"},
165         {NAND_MFR_STMICRO, "ST Micro"},
166         {NAND_MFR_HYNIX, "Hynix"},
167         {NAND_MFR_MICRON, "Micron"},
168         {0x0, NULL},
169 };
170
171 /*
172  * Define default oob placement schemes for large and small page devices
173  */
174
175 #if 0
176 static struct nand_ecclayout nand_oob_8 = {
177         .eccbytes = 3,
178         .eccpos = {0, 1, 2},
179         .oobfree = {
180                 {.offset = 3,
181                  .length = 2},
182                 {.offset = 6,
183                  .length = 2}}
184 };
185 #endif
186
187 static struct nand_ecclayout nand_oob_16 = {
188         .eccbytes = 6,
189         .eccpos = {0, 1, 2, 3, 6, 7},
190         .oobfree = {
191                 {.offset = 8,
192                  . length = 8}}
193 };
194
195 static struct nand_ecclayout nand_oob_64 = {
196         .eccbytes = 24,
197         .eccpos = {
198                    40, 41, 42, 43, 44, 45, 46, 47,
199                    48, 49, 50, 51, 52, 53, 54, 55,
200                    56, 57, 58, 59, 60, 61, 62, 63},
201         .oobfree = {
202                 {.offset = 2,
203                  .length = 38}}
204 };
205
206 /* nand device <nand_controller> [controller options]
207  */
208 COMMAND_HANDLER(handle_nand_device_command)
209 {
210         int i;
211         int retval;
212
213         if (argc < 1)
214         {
215                 LOG_WARNING("incomplete flash device nand configuration");
216                 return ERROR_FLASH_BANK_INVALID;
217         }
218
219         for (i = 0; nand_flash_controllers[i]; i++)
220         {
221                 struct nand_device *p, *c;
222
223                 if (strcmp(args[0], nand_flash_controllers[i]->name) == 0)
224                 {
225                         /* register flash specific commands */
226                         if ((retval = nand_flash_controllers[i]->register_commands(cmd_ctx)) != ERROR_OK)
227                         {
228                                 LOG_ERROR("couldn't register '%s' commands", args[0]);
229                                 return retval;
230                         }
231
232                         c = malloc(sizeof(struct nand_device));
233
234                         c->controller = nand_flash_controllers[i];
235                         c->controller_priv = NULL;
236                         c->manufacturer = NULL;
237                         c->device = NULL;
238                         c->bus_width = 0;
239                         c->address_cycles = 0;
240                         c->page_size = 0;
241                         c->use_raw = 0;
242                         c->next = NULL;
243
244                         retval = CALL_COMMAND_HANDLER(nand_flash_controllers[i]->nand_device_command, c);
245                         if (ERROR_OK != retval)
246                         {
247                                 LOG_ERROR("'%s' driver rejected nand flash", c->controller->name);
248                                 free(c);
249                                 return ERROR_OK;
250                         }
251
252                         /* put NAND device in linked list */
253                         if (nand_devices)
254                         {
255                                 /* find last flash device */
256                                 for (p = nand_devices; p && p->next; p = p->next);
257                                 if (p)
258                                         p->next = c;
259                         }
260                         else
261                         {
262                                 nand_devices = c;
263                         }
264
265                         return ERROR_OK;
266                 }
267         }
268
269         /* no valid NAND controller was found (i.e. the configuration option,
270          * didn't match one of the compiled-in controllers)
271          */
272         LOG_ERROR("No valid NAND flash controller found (%s)", args[0]);
273         LOG_ERROR("compiled-in NAND flash controllers:");
274         for (i = 0; nand_flash_controllers[i]; i++)
275         {
276                 LOG_ERROR("%i: %s", i, nand_flash_controllers[i]->name);
277         }
278
279         return ERROR_OK;
280 }
281
282 int nand_register_commands(struct command_context *cmd_ctx)
283 {
284         nand_cmd = register_command(cmd_ctx, NULL, "nand", NULL, COMMAND_ANY, "NAND specific commands");
285
286         register_command(cmd_ctx, nand_cmd, "device", handle_nand_device_command, COMMAND_CONFIG, NULL);
287
288         return ERROR_OK;
289 }
290
291 struct nand_device *get_nand_device_by_num(int num)
292 {
293         struct nand_device *p;
294         int i = 0;
295
296         for (p = nand_devices; p; p = p->next)
297         {
298                 if (i++ == num)
299                 {
300                         return p;
301                 }
302         }
303
304         return NULL;
305 }
306
307 int nand_command_get_device_by_num(struct command_context *cmd_ctx,
308                 const char *str, struct nand_device **nand)
309 {
310         unsigned num;
311         COMMAND_PARSE_NUMBER(uint, str, num);
312         *nand = get_nand_device_by_num(num);
313         if (!*nand) {
314                 command_print(cmd_ctx, "NAND flash device '#%s' is out of bounds", str);
315                 return ERROR_INVALID_ARGUMENTS;
316         }
317         return ERROR_OK;
318 }
319
320 static int nand_build_bbt(struct nand_device *nand, int first, int last)
321 {
322         uint32_t page = 0x0;
323         int i;
324         uint8_t oob[6];
325
326         if ((first < 0) || (first >= nand->num_blocks))
327                 first = 0;
328
329         if ((last >= nand->num_blocks) || (last == -1))
330                 last = nand->num_blocks - 1;
331
332         for (i = first; i < last; i++)
333         {
334                 nand_read_page(nand, page, NULL, 0, oob, 6);
335
336                 if (((nand->device->options & NAND_BUSWIDTH_16) && ((oob[0] & oob[1]) != 0xff))
337                         || (((nand->page_size == 512) && (oob[5] != 0xff)) ||
338                                 ((nand->page_size == 2048) && (oob[0] != 0xff))))
339                 {
340                         LOG_WARNING("bad block: %i", i);
341                         nand->blocks[i].is_bad = 1;
342                 }
343                 else
344                 {
345                         nand->blocks[i].is_bad = 0;
346                 }
347
348                 page += (nand->erase_size / nand->page_size);
349         }
350
351         return ERROR_OK;
352 }
353
354 int nand_read_status(struct nand_device *nand, uint8_t *status)
355 {
356         if (!nand->device)
357                 return ERROR_NAND_DEVICE_NOT_PROBED;
358
359         /* Send read status command */
360         nand->controller->command(nand, NAND_CMD_STATUS);
361
362         alive_sleep(1);
363
364         /* read status */
365         if (nand->device->options & NAND_BUSWIDTH_16)
366         {
367                 uint16_t data;
368                 nand->controller->read_data(nand, &data);
369                 *status = data & 0xff;
370         }
371         else
372         {
373                 nand->controller->read_data(nand, status);
374         }
375
376         return ERROR_OK;
377 }
378
379 static int nand_poll_ready(struct nand_device *nand, int timeout)
380 {
381         uint8_t status;
382
383         nand->controller->command(nand, NAND_CMD_STATUS);
384         do {
385                 if (nand->device->options & NAND_BUSWIDTH_16) {
386                         uint16_t data;
387                         nand->controller->read_data(nand, &data);
388                         status = data & 0xff;
389                 } else {
390                         nand->controller->read_data(nand, &status);
391                 }
392                 if (status & NAND_STATUS_READY)
393                         break;
394                 alive_sleep(1);
395         } while (timeout--);
396
397         return (status & NAND_STATUS_READY) != 0;
398 }
399
400 int nand_probe(struct nand_device *nand)
401 {
402         uint8_t manufacturer_id, device_id;
403         uint8_t id_buff[6];
404         int retval;
405         int i;
406
407         /* clear device data */
408         nand->device = NULL;
409         nand->manufacturer = NULL;
410
411         /* clear device parameters */
412         nand->bus_width = 0;
413         nand->address_cycles = 0;
414         nand->page_size = 0;
415         nand->erase_size = 0;
416
417         /* initialize controller (device parameters are zero, use controller default) */
418         if ((retval = nand->controller->init(nand) != ERROR_OK))
419         {
420                 switch (retval)
421                 {
422                         case ERROR_NAND_OPERATION_FAILED:
423                                 LOG_DEBUG("controller initialization failed");
424                                 return ERROR_NAND_OPERATION_FAILED;
425                         case ERROR_NAND_OPERATION_NOT_SUPPORTED:
426                                 LOG_ERROR("BUG: controller reported that it doesn't support default parameters");
427                                 return ERROR_NAND_OPERATION_FAILED;
428                         default:
429                                 LOG_ERROR("BUG: unknown controller initialization failure");
430                                 return ERROR_NAND_OPERATION_FAILED;
431                 }
432         }
433
434         nand->controller->command(nand, NAND_CMD_RESET);
435         nand->controller->reset(nand);
436
437         nand->controller->command(nand, NAND_CMD_READID);
438         nand->controller->address(nand, 0x0);
439
440         if (nand->bus_width == 8)
441         {
442                 nand->controller->read_data(nand, &manufacturer_id);
443                 nand->controller->read_data(nand, &device_id);
444         }
445         else
446         {
447                 uint16_t data_buf;
448                 nand->controller->read_data(nand, &data_buf);
449                 manufacturer_id = data_buf & 0xff;
450                 nand->controller->read_data(nand, &data_buf);
451                 device_id = data_buf & 0xff;
452         }
453
454         for (i = 0; nand_flash_ids[i].name; i++)
455         {
456                 if (nand_flash_ids[i].id == device_id)
457                 {
458                         nand->device = &nand_flash_ids[i];
459                         break;
460                 }
461         }
462
463         for (i = 0; nand_manuf_ids[i].name; i++)
464         {
465                 if (nand_manuf_ids[i].id == manufacturer_id)
466                 {
467                         nand->manufacturer = &nand_manuf_ids[i];
468                         break;
469                 }
470         }
471
472         if (!nand->manufacturer)
473         {
474                 nand->manufacturer = &nand_manuf_ids[0];
475                 nand->manufacturer->id = manufacturer_id;
476         }
477
478         if (!nand->device)
479         {
480                 LOG_ERROR("unknown NAND flash device found, manufacturer id: 0x%2.2x device id: 0x%2.2x",
481                         manufacturer_id, device_id);
482                 return ERROR_NAND_OPERATION_FAILED;
483         }
484
485         LOG_DEBUG("found %s (%s)", nand->device->name, nand->manufacturer->name);
486
487         /* initialize device parameters */
488
489         /* bus width */
490         if (nand->device->options & NAND_BUSWIDTH_16)
491                 nand->bus_width = 16;
492         else
493                 nand->bus_width = 8;
494
495         /* Do we need extended device probe information? */
496         if (nand->device->page_size == 0 ||
497             nand->device->erase_size == 0)
498         {
499                 if (nand->bus_width == 8)
500                 {
501                         nand->controller->read_data(nand, id_buff + 3);
502                         nand->controller->read_data(nand, id_buff + 4);
503                         nand->controller->read_data(nand, id_buff + 5);
504                 }
505                 else
506                 {
507                         uint16_t data_buf;
508
509                         nand->controller->read_data(nand, &data_buf);
510                         id_buff[3] = data_buf;
511
512                         nand->controller->read_data(nand, &data_buf);
513                         id_buff[4] = data_buf;
514
515                         nand->controller->read_data(nand, &data_buf);
516                         id_buff[5] = data_buf >> 8;
517                 }
518         }
519
520         /* page size */
521         if (nand->device->page_size == 0)
522         {
523                 nand->page_size = 1 << (10 + (id_buff[4] & 3));
524         }
525         else if (nand->device->page_size == 256)
526         {
527                 LOG_ERROR("NAND flashes with 256 byte pagesize are not supported");
528                 return ERROR_NAND_OPERATION_FAILED;
529         }
530         else
531         {
532                 nand->page_size = nand->device->page_size;
533         }
534
535         /* number of address cycles */
536         if (nand->page_size <= 512)
537         {
538                 /* small page devices */
539                 if (nand->device->chip_size <= 32)
540                         nand->address_cycles = 3;
541                 else if (nand->device->chip_size <= 8*1024)
542                         nand->address_cycles = 4;
543                 else
544                 {
545                         LOG_ERROR("BUG: small page NAND device with more than 8 GiB encountered");
546                         nand->address_cycles = 5;
547                 }
548         }
549         else
550         {
551                 /* large page devices */
552                 if (nand->device->chip_size <= 128)
553                         nand->address_cycles = 4;
554                 else if (nand->device->chip_size <= 32*1024)
555                         nand->address_cycles = 5;
556                 else
557                 {
558                         LOG_ERROR("BUG: large page NAND device with more than 32 GiB encountered");
559                         nand->address_cycles = 6;
560                 }
561         }
562
563         /* erase size */
564         if (nand->device->erase_size == 0)
565         {
566                 switch ((id_buff[4] >> 4) & 3) {
567                 case 0:
568                         nand->erase_size = 64 << 10;
569                         break;
570                 case 1:
571                         nand->erase_size = 128 << 10;
572                         break;
573                 case 2:
574                         nand->erase_size = 256 << 10;
575                         break;
576                 case 3:
577                         nand->erase_size =512 << 10;
578                         break;
579                 }
580         }
581         else
582         {
583                 nand->erase_size = nand->device->erase_size;
584         }
585
586         /* initialize controller, but leave parameters at the controllers default */
587         if ((retval = nand->controller->init(nand) != ERROR_OK))
588         {
589                 switch (retval)
590                 {
591                         case ERROR_NAND_OPERATION_FAILED:
592                                 LOG_DEBUG("controller initialization failed");
593                                 return ERROR_NAND_OPERATION_FAILED;
594                         case ERROR_NAND_OPERATION_NOT_SUPPORTED:
595                                 LOG_ERROR("controller doesn't support requested parameters (buswidth: %i, address cycles: %i, page size: %i)",
596                                         nand->bus_width, nand->address_cycles, nand->page_size);
597                                 return ERROR_NAND_OPERATION_FAILED;
598                         default:
599                                 LOG_ERROR("BUG: unknown controller initialization failure");
600                                 return ERROR_NAND_OPERATION_FAILED;
601                 }
602         }
603
604         nand->num_blocks = (nand->device->chip_size * 1024) / (nand->erase_size / 1024);
605         nand->blocks = malloc(sizeof(struct nand_block) * nand->num_blocks);
606
607         for (i = 0; i < nand->num_blocks; i++)
608         {
609                 nand->blocks[i].size = nand->erase_size;
610                 nand->blocks[i].offset = i * nand->erase_size;
611                 nand->blocks[i].is_erased = -1;
612                 nand->blocks[i].is_bad = -1;
613         }
614
615         return ERROR_OK;
616 }
617
618 static int nand_erase(struct nand_device *nand, int first_block, int last_block)
619 {
620         int i;
621         uint32_t page;
622         uint8_t status;
623         int retval;
624
625         if (!nand->device)
626                 return ERROR_NAND_DEVICE_NOT_PROBED;
627
628         if ((first_block < 0) || (last_block > nand->num_blocks))
629                 return ERROR_INVALID_ARGUMENTS;
630
631         /* make sure we know if a block is bad before erasing it */
632         for (i = first_block; i <= last_block; i++)
633         {
634                 if (nand->blocks[i].is_bad == -1)
635                 {
636                         nand_build_bbt(nand, i, last_block);
637                         break;
638                 }
639         }
640
641         for (i = first_block; i <= last_block; i++)
642         {
643                 /* Send erase setup command */
644                 nand->controller->command(nand, NAND_CMD_ERASE1);
645
646                 page = i * (nand->erase_size / nand->page_size);
647
648                 /* Send page address */
649                 if (nand->page_size <= 512)
650                 {
651                         /* row */
652                         nand->controller->address(nand, page & 0xff);
653                         nand->controller->address(nand, (page >> 8) & 0xff);
654
655                         /* 3rd cycle only on devices with more than 32 MiB */
656                         if (nand->address_cycles >= 4)
657                                 nand->controller->address(nand, (page >> 16) & 0xff);
658
659                         /* 4th cycle only on devices with more than 8 GiB */
660                         if (nand->address_cycles >= 5)
661                                 nand->controller->address(nand, (page >> 24) & 0xff);
662                 }
663                 else
664                 {
665                         /* row */
666                         nand->controller->address(nand, page & 0xff);
667                         nand->controller->address(nand, (page >> 8) & 0xff);
668
669                         /* 3rd cycle only on devices with more than 128 MiB */
670                         if (nand->address_cycles >= 5)
671                                 nand->controller->address(nand, (page >> 16) & 0xff);
672                 }
673
674                 /* Send erase confirm command */
675                 nand->controller->command(nand, NAND_CMD_ERASE2);
676
677                 retval = nand->controller->nand_ready ?
678                                 nand->controller->nand_ready(nand, 1000) :
679                                 nand_poll_ready(nand, 1000);
680                 if (!retval) {
681                         LOG_ERROR("timeout waiting for NAND flash block erase to complete");
682                         return ERROR_NAND_OPERATION_TIMEOUT;
683                 }
684
685                 if ((retval = nand_read_status(nand, &status)) != ERROR_OK)
686                 {
687                         LOG_ERROR("couldn't read status");
688                         return ERROR_NAND_OPERATION_FAILED;
689                 }
690
691                 if (status & 0x1)
692                 {
693                         LOG_ERROR("didn't erase %sblock %d; status: 0x%2.2x",
694                                         (nand->blocks[i].is_bad == 1)
695                                                 ? "bad " : "",
696                                         i, status);
697                         /* continue; other blocks might still be erasable */
698                 }
699
700                 nand->blocks[i].is_erased = 1;
701         }
702
703         return ERROR_OK;
704 }
705
706 #if 0
707 static int nand_read_plain(struct nand_device *nand, uint32_t address, uint8_t *data, uint32_t data_size)
708 {
709         uint8_t *page;
710
711         if (!nand->device)
712                 return ERROR_NAND_DEVICE_NOT_PROBED;
713
714         if (address % nand->page_size)
715         {
716                 LOG_ERROR("reads need to be page aligned");
717                 return ERROR_NAND_OPERATION_FAILED;
718         }
719
720         page = malloc(nand->page_size);
721
722         while (data_size > 0)
723         {
724                 uint32_t thisrun_size = (data_size > nand->page_size) ? nand->page_size : data_size;
725                 uint32_t page_address;
726
727
728                 page_address = address / nand->page_size;
729
730                 nand_read_page(nand, page_address, page, nand->page_size, NULL, 0);
731
732                 memcpy(data, page, thisrun_size);
733
734                 address += thisrun_size;
735                 data += thisrun_size;
736                 data_size -= thisrun_size;
737         }
738
739         free(page);
740
741         return ERROR_OK;
742 }
743
744 static int nand_write_plain(struct nand_device *nand, uint32_t address, uint8_t *data, uint32_t data_size)
745 {
746         uint8_t *page;
747
748         if (!nand->device)
749                 return ERROR_NAND_DEVICE_NOT_PROBED;
750
751         if (address % nand->page_size)
752         {
753                 LOG_ERROR("writes need to be page aligned");
754                 return ERROR_NAND_OPERATION_FAILED;
755         }
756
757         page = malloc(nand->page_size);
758
759         while (data_size > 0)
760         {
761                 uint32_t thisrun_size = (data_size > nand->page_size) ? nand->page_size : data_size;
762                 uint32_t page_address;
763
764                 memset(page, 0xff, nand->page_size);
765                 memcpy(page, data, thisrun_size);
766
767                 page_address = address / nand->page_size;
768
769                 nand_write_page(nand, page_address, page, nand->page_size, NULL, 0);
770
771                 address += thisrun_size;
772                 data += thisrun_size;
773                 data_size -= thisrun_size;
774         }
775
776         free(page);
777
778         return ERROR_OK;
779 }
780 #endif
781
782 int nand_write_page(struct nand_device *nand, uint32_t page, uint8_t *data, uint32_t data_size, uint8_t *oob, uint32_t oob_size)
783 {
784         uint32_t block;
785
786         if (!nand->device)
787                 return ERROR_NAND_DEVICE_NOT_PROBED;
788
789         block = page / (nand->erase_size / nand->page_size);
790         if (nand->blocks[block].is_erased == 1)
791                 nand->blocks[block].is_erased = 0;
792
793         if (nand->use_raw || nand->controller->write_page == NULL)
794                 return nand_write_page_raw(nand, page, data, data_size, oob, oob_size);
795         else
796                 return nand->controller->write_page(nand, page, data, data_size, oob, oob_size);
797 }
798
799 static int nand_read_page(struct nand_device *nand, uint32_t page, uint8_t *data, uint32_t data_size, uint8_t *oob, uint32_t oob_size)
800 {
801         if (!nand->device)
802                 return ERROR_NAND_DEVICE_NOT_PROBED;
803
804         if (nand->use_raw || nand->controller->read_page == NULL)
805                 return nand_read_page_raw(nand, page, data, data_size, oob, oob_size);
806         else
807                 return nand->controller->read_page(nand, page, data, data_size, oob, oob_size);
808 }
809
810 int nand_read_page_raw(struct nand_device *nand, uint32_t page, uint8_t *data, uint32_t data_size, uint8_t *oob, uint32_t oob_size)
811 {
812         uint32_t i;
813
814         if (!nand->device)
815                 return ERROR_NAND_DEVICE_NOT_PROBED;
816
817         if (nand->page_size <= 512)
818         {
819                 /* small page device */
820                 if (data)
821                         nand->controller->command(nand, NAND_CMD_READ0);
822                 else
823                         nand->controller->command(nand, NAND_CMD_READOOB);
824
825                 /* column (always 0, we start at the beginning of a page/OOB area) */
826                 nand->controller->address(nand, 0x0);
827
828                 /* row */
829                 nand->controller->address(nand, page & 0xff);
830                 nand->controller->address(nand, (page >> 8) & 0xff);
831
832                 /* 4th cycle only on devices with more than 32 MiB */
833                 if (nand->address_cycles >= 4)
834                         nand->controller->address(nand, (page >> 16) & 0xff);
835
836                 /* 5th cycle only on devices with more than 8 GiB */
837                 if (nand->address_cycles >= 5)
838                         nand->controller->address(nand, (page >> 24) & 0xff);
839         }
840         else
841         {
842                 /* large page device */
843                 nand->controller->command(nand, NAND_CMD_READ0);
844
845                 /* column (0 when we start at the beginning of a page,
846                  * or 2048 for the beginning of OOB area)
847                  */
848                 nand->controller->address(nand, 0x0);
849                 if (data)
850                         nand->controller->address(nand, 0x0);
851                 else
852                         nand->controller->address(nand, 0x8);
853
854                 /* row */
855                 nand->controller->address(nand, page & 0xff);
856                 nand->controller->address(nand, (page >> 8) & 0xff);
857
858                 /* 5th cycle only on devices with more than 128 MiB */
859                 if (nand->address_cycles >= 5)
860                         nand->controller->address(nand, (page >> 16) & 0xff);
861
862                 /* large page devices need a start command */
863                 nand->controller->command(nand, NAND_CMD_READSTART);
864         }
865
866         if (nand->controller->nand_ready) {
867                 if (!nand->controller->nand_ready(nand, 100))
868                         return ERROR_NAND_OPERATION_TIMEOUT;
869         } else {
870                 alive_sleep(1);
871         }
872
873         if (data)
874         {
875                 if (nand->controller->read_block_data != NULL)
876                         (nand->controller->read_block_data)(nand, data, data_size);
877                 else
878                 {
879                         for (i = 0; i < data_size;)
880                         {
881                                 if (nand->device->options & NAND_BUSWIDTH_16)
882                                 {
883                                         nand->controller->read_data(nand, data);
884                                         data += 2;
885                                         i += 2;
886                                 }
887                                 else
888                                 {
889                                         nand->controller->read_data(nand, data);
890                                         data += 1;
891                                         i += 1;
892                                 }
893                         }
894                 }
895         }
896
897         if (oob)
898         {
899                 if (nand->controller->read_block_data != NULL)
900                         (nand->controller->read_block_data)(nand, oob, oob_size);
901                 else
902                 {
903                         for (i = 0; i < oob_size;)
904                         {
905                                 if (nand->device->options & NAND_BUSWIDTH_16)
906                                 {
907                                         nand->controller->read_data(nand, oob);
908                                         oob += 2;
909                                         i += 2;
910                                 }
911                                 else
912                                 {
913                                         nand->controller->read_data(nand, oob);
914                                         oob += 1;
915                                         i += 1;
916                                 }
917                         }
918                 }
919         }
920
921         return ERROR_OK;
922 }
923
924 int nand_write_page_raw(struct nand_device *nand, uint32_t page, uint8_t *data, uint32_t data_size, uint8_t *oob, uint32_t oob_size)
925 {
926         uint32_t i;
927         int retval;
928         uint8_t status;
929
930         if (!nand->device)
931                 return ERROR_NAND_DEVICE_NOT_PROBED;
932
933         nand->controller->command(nand, NAND_CMD_SEQIN);
934
935         if (nand->page_size <= 512)
936         {
937                 /* column (always 0, we start at the beginning of a page/OOB area) */
938                 nand->controller->address(nand, 0x0);
939
940                 /* row */
941                 nand->controller->address(nand, page & 0xff);
942                 nand->controller->address(nand, (page >> 8) & 0xff);
943
944                 /* 4th cycle only on devices with more than 32 MiB */
945                 if (nand->address_cycles >= 4)
946                         nand->controller->address(nand, (page >> 16) & 0xff);
947
948                 /* 5th cycle only on devices with more than 8 GiB */
949                 if (nand->address_cycles >= 5)
950                         nand->controller->address(nand, (page >> 24) & 0xff);
951         }
952         else
953         {
954                 /* column (0 when we start at the beginning of a page,
955                  * or 2048 for the beginning of OOB area)
956                  */
957                 nand->controller->address(nand, 0x0);
958                 if (data)
959                         nand->controller->address(nand, 0x0);
960                 else
961                         nand->controller->address(nand, 0x8);
962
963                 /* row */
964                 nand->controller->address(nand, page & 0xff);
965                 nand->controller->address(nand, (page >> 8) & 0xff);
966
967                 /* 5th cycle only on devices with more than 128 MiB */
968                 if (nand->address_cycles >= 5)
969                         nand->controller->address(nand, (page >> 16) & 0xff);
970         }
971
972         if (data)
973         {
974                 if (nand->controller->write_block_data != NULL)
975                         (nand->controller->write_block_data)(nand, data, data_size);
976                 else
977                 {
978                         for (i = 0; i < data_size;)
979                         {
980                                 if (nand->device->options & NAND_BUSWIDTH_16)
981                                 {
982                                         uint16_t data_buf = le_to_h_u16(data);
983                                         nand->controller->write_data(nand, data_buf);
984                                         data += 2;
985                                         i += 2;
986                                 }
987                                 else
988                                 {
989                                         nand->controller->write_data(nand, *data);
990                                         data += 1;
991                                         i += 1;
992                                 }
993                         }
994                 }
995         }
996
997         if (oob)
998         {
999                 if (nand->controller->write_block_data != NULL)
1000                         (nand->controller->write_block_data)(nand, oob, oob_size);
1001                 else
1002                 {
1003                         for (i = 0; i < oob_size;)
1004                         {
1005                                 if (nand->device->options & NAND_BUSWIDTH_16)
1006                                 {
1007                                         uint16_t oob_buf = le_to_h_u16(data);
1008                                         nand->controller->write_data(nand, oob_buf);
1009                                         oob += 2;
1010                                         i += 2;
1011                                 }
1012                                 else
1013                                 {
1014                                         nand->controller->write_data(nand, *oob);
1015                                         oob += 1;
1016                                         i += 1;
1017                                 }
1018                         }
1019                 }
1020         }
1021
1022         nand->controller->command(nand, NAND_CMD_PAGEPROG);
1023
1024         retval = nand->controller->nand_ready ?
1025                         nand->controller->nand_ready(nand, 100) :
1026                         nand_poll_ready(nand, 100);
1027         if (!retval)
1028                 return ERROR_NAND_OPERATION_TIMEOUT;
1029
1030         if ((retval = nand_read_status(nand, &status)) != ERROR_OK)
1031         {
1032                 LOG_ERROR("couldn't read status");
1033                 return ERROR_NAND_OPERATION_FAILED;
1034         }
1035
1036         if (status & NAND_STATUS_FAIL)
1037         {
1038                 LOG_ERROR("write operation didn't pass, status: 0x%2.2x", status);
1039                 return ERROR_NAND_OPERATION_FAILED;
1040         }
1041
1042         return ERROR_OK;
1043 }
1044
1045 COMMAND_HANDLER(handle_nand_list_command)
1046 {
1047         struct nand_device *p;
1048         int i;
1049
1050         if (!nand_devices)
1051         {
1052                 command_print(cmd_ctx, "no NAND flash devices configured");
1053                 return ERROR_OK;
1054         }
1055
1056         for (p = nand_devices, i = 0; p; p = p->next, i++)
1057         {
1058                 if (p->device)
1059                         command_print(cmd_ctx, "#%i: %s (%s) "
1060                                 "pagesize: %i, buswidth: %i,\n\t"
1061                                 "blocksize: %i, blocks: %i",
1062                                 i, p->device->name, p->manufacturer->name,
1063                                 p->page_size, p->bus_width,
1064                                 p->erase_size, p->num_blocks);
1065                 else
1066                         command_print(cmd_ctx, "#%i: not probed", i);
1067         }
1068
1069         return ERROR_OK;
1070 }
1071
1072 COMMAND_HANDLER(handle_nand_info_command)
1073 {
1074         int i = 0;
1075         int j = 0;
1076         int first = -1;
1077         int last = -1;
1078
1079         struct nand_device *p;
1080         int retval = nand_command_get_device_by_num(cmd_ctx, args[0], &p);
1081         if (ERROR_OK != retval)
1082                 return retval;
1083
1084         switch (argc) {
1085         default:
1086                 return ERROR_COMMAND_SYNTAX_ERROR;
1087         case 1:
1088                 first = 0;
1089                 last = INT32_MAX;
1090                 break;
1091         case 2:
1092                 COMMAND_PARSE_NUMBER(int, args[1], i);
1093                 first = last = i;
1094                 i = 0;
1095                 break;
1096         case 3:
1097                 COMMAND_PARSE_NUMBER(int, args[1], first);
1098                 COMMAND_PARSE_NUMBER(int, args[2], last);
1099                 break;
1100         }
1101
1102         if (NULL == p->device)
1103         {
1104                 command_print(cmd_ctx, "#%s: not probed", args[0]);
1105                 return ERROR_OK;
1106         }
1107
1108         if (first >= p->num_blocks)
1109                 first = p->num_blocks - 1;
1110
1111         if (last >= p->num_blocks)
1112                 last = p->num_blocks - 1;
1113
1114         command_print(cmd_ctx, "#%i: %s (%s) pagesize: %i, buswidth: %i, erasesize: %i",
1115                 i++, p->device->name, p->manufacturer->name, p->page_size, p->bus_width, p->erase_size);
1116
1117         for (j = first; j <= last; j++)
1118         {
1119                 char *erase_state, *bad_state;
1120
1121                 if (p->blocks[j].is_erased == 0)
1122                         erase_state = "not erased";
1123                 else if (p->blocks[j].is_erased == 1)
1124                         erase_state = "erased";
1125                 else
1126                         erase_state = "erase state unknown";
1127
1128                 if (p->blocks[j].is_bad == 0)
1129                         bad_state = "";
1130                 else if (p->blocks[j].is_bad == 1)
1131                         bad_state = " (marked bad)";
1132                 else
1133                         bad_state = " (block condition unknown)";
1134
1135                 command_print(cmd_ctx,
1136                               "\t#%i: 0x%8.8" PRIx32 " (%" PRId32 "kB) %s%s",
1137                               j,
1138                               p->blocks[j].offset,
1139                               p->blocks[j].size / 1024,
1140                               erase_state,
1141                               bad_state);
1142         }
1143
1144         return ERROR_OK;
1145 }
1146
1147 COMMAND_HANDLER(handle_nand_probe_command)
1148 {
1149         if (argc != 1)
1150         {
1151                 return ERROR_COMMAND_SYNTAX_ERROR;
1152         }
1153
1154         struct nand_device *p;
1155         int retval = nand_command_get_device_by_num(cmd_ctx, args[0], &p);
1156         if (ERROR_OK != retval)
1157                 return retval;
1158
1159         if ((retval = nand_probe(p)) == ERROR_OK)
1160         {
1161                 command_print(cmd_ctx, "NAND flash device '%s' found", p->device->name);
1162         }
1163         else if (retval == ERROR_NAND_OPERATION_FAILED)
1164         {
1165                 command_print(cmd_ctx, "probing failed for NAND flash device");
1166         }
1167         else
1168         {
1169                 command_print(cmd_ctx, "unknown error when probing NAND flash device");
1170         }
1171
1172         return ERROR_OK;
1173 }
1174
1175 COMMAND_HANDLER(handle_nand_erase_command)
1176 {
1177         if (argc != 1 && argc != 3)
1178         {
1179                 return ERROR_COMMAND_SYNTAX_ERROR;
1180
1181         }
1182
1183         struct nand_device *p;
1184         int retval = nand_command_get_device_by_num(cmd_ctx, args[0], &p);
1185         if (ERROR_OK != retval)
1186                 return retval;
1187
1188         unsigned long offset;
1189         unsigned long length;
1190
1191         /* erase specified part of the chip; or else everything */
1192         if (argc == 3) {
1193                 unsigned long size = p->erase_size * p->num_blocks;
1194
1195                 COMMAND_PARSE_NUMBER(ulong, args[1], offset);
1196                 if ((offset % p->erase_size) != 0 || offset >= size)
1197                         return ERROR_INVALID_ARGUMENTS;
1198
1199                 COMMAND_PARSE_NUMBER(ulong, args[2], length);
1200                 if ((length == 0) || (length % p->erase_size) != 0
1201                                 || (length + offset) > size)
1202                         return ERROR_INVALID_ARGUMENTS;
1203
1204                 offset /= p->erase_size;
1205                 length /= p->erase_size;
1206         } else {
1207                 offset = 0;
1208                 length = p->num_blocks;
1209         }
1210
1211         retval = nand_erase(p, offset, offset + length - 1);
1212         if (retval == ERROR_OK)
1213         {
1214                 command_print(cmd_ctx, "erased blocks %lu to %lu "
1215                                 "on NAND flash device #%s '%s'",
1216                                 offset, offset + length,
1217                                 args[0], p->device->name);
1218         }
1219         else if (retval == ERROR_NAND_OPERATION_FAILED)
1220         {
1221                 command_print(cmd_ctx, "erase failed");
1222         }
1223         else
1224         {
1225                 command_print(cmd_ctx, "unknown error when erasing NAND flash device");
1226         }
1227
1228         return ERROR_OK;
1229 }
1230
1231 COMMAND_HANDLER(handle_nand_check_bad_blocks_command)
1232 {
1233         int first = -1;
1234         int last = -1;
1235
1236         if ((argc < 1) || (argc > 3) || (argc == 2))
1237         {
1238                 return ERROR_COMMAND_SYNTAX_ERROR;
1239
1240         }
1241
1242         struct nand_device *p;
1243         int retval = nand_command_get_device_by_num(cmd_ctx, args[0], &p);
1244         if (ERROR_OK != retval)
1245                 return retval;
1246
1247         if (argc == 3)
1248         {
1249                 unsigned long offset;
1250                 unsigned long length;
1251
1252                 COMMAND_PARSE_NUMBER(ulong, args[1], offset);
1253                 if (offset % p->erase_size)
1254                         return ERROR_INVALID_ARGUMENTS;
1255                 offset /= p->erase_size;
1256
1257                 COMMAND_PARSE_NUMBER(ulong, args[2], length);
1258                 if (length % p->erase_size)
1259                         return ERROR_INVALID_ARGUMENTS;
1260
1261                 length -= 1;
1262                 length /= p->erase_size;
1263
1264                 first = offset;
1265                 last = offset + length;
1266         }
1267
1268         retval = nand_build_bbt(p, first, last);
1269         if (retval == ERROR_OK)
1270         {
1271                 command_print(cmd_ctx, "checked NAND flash device for bad blocks, "
1272                                 "use \"nand info\" command to list blocks");
1273         }
1274         else if (retval == ERROR_NAND_OPERATION_FAILED)
1275         {
1276                 command_print(cmd_ctx, "error when checking for bad blocks on "
1277                                 "NAND flash device");
1278         }
1279         else
1280         {
1281                 command_print(cmd_ctx, "unknown error when checking for bad "
1282                                 "blocks on NAND flash device");
1283         }
1284
1285         return ERROR_OK;
1286 }
1287
1288 struct nand_fileio_state {
1289         uint32_t address;
1290         uint32_t size;
1291
1292         uint8_t *page;
1293         uint32_t page_size;
1294
1295         enum oob_formats oob_format;
1296         uint8_t *oob;
1297         uint32_t oob_size;
1298
1299         const int *eccpos;
1300
1301         bool file_opened;
1302         struct fileio fileio;
1303
1304         struct duration bench;
1305 };
1306
1307 static void nand_fileio_init(struct nand_fileio_state *state)
1308 {
1309         memset(state, 0, sizeof(*state));
1310         state->oob_format = NAND_OOB_NONE;
1311 }
1312
1313 static int nand_fileio_start(struct command_context *cmd_ctx,
1314                 struct nand_device *nand, const char *filename, int filemode,
1315                 struct nand_fileio_state *state)
1316 {
1317         if (state->address % nand->page_size)
1318         {
1319                 command_print(cmd_ctx, "only page-aligned addresses are supported");
1320                 return ERROR_COMMAND_SYNTAX_ERROR;
1321         }
1322
1323         duration_start(&state->bench);
1324
1325         if (NULL != filename)
1326         {
1327                 int retval = fileio_open(&state->fileio, filename, filemode, FILEIO_BINARY);
1328                 if (ERROR_OK != retval)
1329                 {
1330                         const char *msg = (FILEIO_READ == filemode) ? "read" : "write";
1331                         command_print(cmd_ctx, "failed to open '%s' for %s access",
1332                                         filename, msg);
1333                         return retval;
1334                 }
1335                 state->file_opened = true;
1336         }
1337
1338         if (!(state->oob_format & NAND_OOB_ONLY))
1339         {
1340                 state->page_size = nand->page_size;
1341                 state->page = malloc(nand->page_size);
1342         }
1343
1344         if (state->oob_format & (NAND_OOB_RAW | NAND_OOB_SW_ECC | NAND_OOB_SW_ECC_KW))
1345         {
1346                 if (nand->page_size == 512)
1347                 {
1348                         state->oob_size = 16;
1349                         state->eccpos = nand_oob_16.eccpos;
1350                 }
1351                 else if (nand->page_size == 2048)
1352                 {
1353                         state->oob_size = 64;
1354                         state->eccpos = nand_oob_64.eccpos;
1355                 }
1356                 state->oob = malloc(state->oob_size);
1357         }
1358
1359         return ERROR_OK;
1360 }
1361 static int nand_fileio_cleanup(struct nand_fileio_state *state)
1362 {
1363         if (state->file_opened)
1364                 fileio_close(&state->fileio);
1365
1366         if (state->oob)
1367         {
1368                 free(state->oob);
1369                 state->oob = NULL;
1370         }
1371         if (state->page)
1372         {
1373                 free(state->page);
1374                 state->page = NULL;
1375         }
1376         return ERROR_OK;
1377 }
1378 int nand_fileio_finish(struct nand_fileio_state *state)
1379 {
1380         nand_fileio_cleanup(state);
1381         return duration_measure(&state->bench);
1382 }
1383
1384 COMMAND_HELPER(nand_fileio_parse_args, struct nand_fileio_state *state,
1385                 struct nand_device **dev, enum fileio_access filemode,
1386                 bool need_size, bool sw_ecc)
1387 {
1388         nand_fileio_init(state);
1389
1390         unsigned minargs = need_size ? 4 : 3;
1391         if (argc < minargs)
1392                 return ERROR_COMMAND_SYNTAX_ERROR;
1393
1394         struct nand_device *nand;
1395         int retval = nand_command_get_device_by_num(cmd_ctx, args[0], &nand);
1396         if (ERROR_OK != retval)
1397                 return retval;
1398
1399         if (NULL == nand->device)
1400         {
1401                 command_print(cmd_ctx, "#%s: not probed", args[0]);
1402                 return ERROR_OK;
1403         }
1404
1405         COMMAND_PARSE_NUMBER(u32, args[2], state->address);
1406         if (need_size)
1407         {
1408                         COMMAND_PARSE_NUMBER(u32, args[2], state->size);
1409                         if (state->size % nand->page_size)
1410                         {
1411                                 command_print(cmd_ctx, "only page-aligned sizes are supported");
1412                                 return ERROR_COMMAND_SYNTAX_ERROR;
1413                         }
1414         }
1415
1416         if (argc > minargs)
1417         {
1418                 for (unsigned i = minargs; i < argc; i++)
1419                 {
1420                         if (!strcmp(args[i], "oob_raw"))
1421                                 state->oob_format |= NAND_OOB_RAW;
1422                         else if (!strcmp(args[i], "oob_only"))
1423                                 state->oob_format |= NAND_OOB_RAW | NAND_OOB_ONLY;
1424                         else if (sw_ecc && !strcmp(args[i], "oob_softecc"))
1425                                 state->oob_format |= NAND_OOB_SW_ECC;
1426                         else if (sw_ecc && !strcmp(args[i], "oob_softecc_kw"))
1427                                 state->oob_format |= NAND_OOB_SW_ECC_KW;
1428                         else
1429                         {
1430                                 command_print(cmd_ctx, "unknown option: %s", args[i]);
1431                                 return ERROR_COMMAND_SYNTAX_ERROR;
1432                         }
1433                 }
1434         }
1435
1436         retval = nand_fileio_start(cmd_ctx, nand, args[1], filemode, state);
1437         if (ERROR_OK != retval)
1438                 return retval;
1439
1440         if (!need_size)
1441                 state->size = state->fileio.size;
1442
1443         *dev = nand;
1444
1445         return ERROR_OK;
1446 }
1447
1448 /**
1449  * @returns If no error occurred, returns number of bytes consumed;
1450  * otherwise, returns a negative error code.)
1451  */
1452 int nand_fileio_read(struct nand_device *nand,
1453                 struct nand_fileio_state *s)
1454 {
1455         uint32_t total_read = 0;
1456         uint32_t one_read;
1457
1458         if (NULL != s->page)
1459         {
1460                 fileio_read(&s->fileio, s->page_size, s->page, &one_read);
1461                 if (one_read < s->page_size)
1462                         memset(s->page + one_read, 0xff, s->page_size - one_read);
1463                 total_read += one_read;
1464         }
1465
1466         if (s->oob_format & NAND_OOB_SW_ECC)
1467         {
1468                 uint8_t ecc[3];
1469                 memset(s->oob, 0xff, s->oob_size);
1470                 for (uint32_t i = 0, j = 0; i < s->page_size; i += 256)
1471                 {
1472                         nand_calculate_ecc(nand, s->page + i, ecc);
1473                         s->oob[s->eccpos[j++]] = ecc[0];
1474                         s->oob[s->eccpos[j++]] = ecc[1];
1475                         s->oob[s->eccpos[j++]] = ecc[2];
1476                 }
1477         }
1478         else if (s->oob_format & NAND_OOB_SW_ECC_KW)
1479         {
1480                 /*
1481                  * In this case eccpos is not used as
1482                  * the ECC data is always stored contigously
1483                  * at the end of the OOB area.  It consists
1484                  * of 10 bytes per 512-byte data block.
1485                  */
1486                 uint8_t *ecc = s->oob + s->oob_size - s->page_size / 512 * 10;
1487                 memset(s->oob, 0xff, s->oob_size);
1488                 for (uint32_t i = 0; i < s->page_size; i += 512)
1489                 {
1490                         nand_calculate_ecc_kw(nand, s->page + i, ecc);
1491                         ecc += 10;
1492                 }
1493         }
1494         else if (NULL != s->oob)
1495         {
1496                 fileio_read(&s->fileio, s->oob_size, s->oob, &one_read);
1497                 if (one_read < s->oob_size)
1498                         memset(s->oob + one_read, 0xff, s->oob_size - one_read);
1499                 total_read += one_read;
1500         }
1501         return total_read;
1502 }
1503
1504 COMMAND_HANDLER(handle_nand_write_command)
1505 {
1506         uint32_t offset;
1507         uint32_t binary_size;
1508         uint32_t buf_cnt;
1509         enum oob_formats oob_format = NAND_OOB_NONE;
1510
1511         struct fileio fileio;
1512
1513
1514         if (argc < 3)
1515         {
1516                 return ERROR_COMMAND_SYNTAX_ERROR;
1517         }
1518
1519         struct nand_device *p;
1520         int retval = nand_command_get_device_by_num(cmd_ctx, args[0], &p);
1521         if (ERROR_OK != retval)
1522                 return retval;
1523
1524         uint8_t *page = NULL;
1525         uint32_t page_size = 0;
1526         uint8_t *oob = NULL;
1527         uint32_t oob_size = 0;
1528         const int *eccpos = NULL;
1529
1530         COMMAND_PARSE_NUMBER(u32, args[2], offset);
1531
1532         if (argc > 3)
1533         {
1534                 for (unsigned i = 3; i < argc; i++)
1535                 {
1536                         if (!strcmp(args[i], "oob_raw"))
1537                                 oob_format |= NAND_OOB_RAW;
1538                         else if (!strcmp(args[i], "oob_only"))
1539                                 oob_format |= NAND_OOB_RAW | NAND_OOB_ONLY;
1540                         else if (!strcmp(args[i], "oob_softecc"))
1541                                 oob_format |= NAND_OOB_SW_ECC;
1542                         else if (!strcmp(args[i], "oob_softecc_kw"))
1543                                 oob_format |= NAND_OOB_SW_ECC_KW;
1544                         else
1545                         {
1546                                 command_print(cmd_ctx, "unknown option: %s", args[i]);
1547                                 return ERROR_COMMAND_SYNTAX_ERROR;
1548                         }
1549                 }
1550         }
1551
1552         struct duration bench;
1553         duration_start(&bench);
1554
1555         if (fileio_open(&fileio, args[1], FILEIO_READ, FILEIO_BINARY) != ERROR_OK)
1556         {
1557                 return ERROR_OK;
1558         }
1559
1560         buf_cnt = binary_size = fileio.size;
1561
1562         if (!(oob_format & NAND_OOB_ONLY))
1563         {
1564                 page_size = p->page_size;
1565                 page = malloc(p->page_size);
1566         }
1567
1568         if (oob_format & (NAND_OOB_RAW | NAND_OOB_SW_ECC | NAND_OOB_SW_ECC_KW))
1569         {
1570                 if (p->page_size == 512) {
1571                         oob_size = 16;
1572                         eccpos = nand_oob_16.eccpos;
1573                 } else if (p->page_size == 2048) {
1574                         oob_size = 64;
1575                         eccpos = nand_oob_64.eccpos;
1576                 }
1577                 oob = malloc(oob_size);
1578         }
1579
1580         if (offset % p->page_size)
1581         {
1582                 command_print(cmd_ctx, "only page size aligned offsets and sizes are supported");
1583                 fileio_close(&fileio);
1584                 free(oob);
1585                 free(page);
1586                 return ERROR_OK;
1587         }
1588
1589         while (buf_cnt > 0)
1590         {
1591                 uint32_t size_read;
1592
1593                 if (NULL != page)
1594                 {
1595                         fileio_read(&fileio, page_size, page, &size_read);
1596                         buf_cnt -= size_read;
1597                         if (size_read < page_size)
1598                         {
1599                                 memset(page + size_read, 0xff, page_size - size_read);
1600                         }
1601                 }
1602
1603                 if (oob_format & NAND_OOB_SW_ECC)
1604                 {
1605                         uint32_t i, j;
1606                         uint8_t ecc[3];
1607                         memset(oob, 0xff, oob_size);
1608                         for (i = 0, j = 0; i < page_size; i += 256) {
1609                                 nand_calculate_ecc(p, page + i, ecc);
1610                                 oob[eccpos[j++]] = ecc[0];
1611                                 oob[eccpos[j++]] = ecc[1];
1612                                 oob[eccpos[j++]] = ecc[2];
1613                         }
1614                 } else if (oob_format & NAND_OOB_SW_ECC_KW)
1615                 {
1616                         /*
1617                          * In this case eccpos is not used as
1618                          * the ECC data is always stored contigously
1619                          * at the end of the OOB area.  It consists
1620                          * of 10 bytes per 512-byte data block.
1621                          */
1622                         uint32_t i;
1623                         uint8_t *ecc = oob + oob_size - page_size/512 * 10;
1624                         memset(oob, 0xff, oob_size);
1625                         for (i = 0; i < page_size; i += 512) {
1626                                 nand_calculate_ecc_kw(p, page + i, ecc);
1627                                 ecc += 10;
1628                         }
1629                 }
1630                 else if (NULL != oob)
1631                 {
1632                         fileio_read(&fileio, oob_size, oob, &size_read);
1633                         buf_cnt -= size_read;
1634                         if (size_read < oob_size)
1635                         {
1636                                 memset(oob + size_read, 0xff, oob_size - size_read);
1637                         }
1638                 }
1639
1640                 if (nand_write_page(p, offset / p->page_size, page, page_size, oob, oob_size) != ERROR_OK)
1641                 {
1642                         command_print(cmd_ctx, "failed writing file %s to NAND flash %s at offset 0x%8.8" PRIx32 "",
1643                                 args[1], args[0], offset);
1644
1645                         fileio_close(&fileio);
1646                         free(oob);
1647                         free(page);
1648
1649                         return ERROR_OK;
1650                 }
1651                 offset += page_size;
1652         }
1653
1654         fileio_close(&fileio);
1655         free(oob);
1656         free(page);
1657         oob = NULL;
1658         page = NULL;
1659         if (duration_measure(&bench) == ERROR_OK)
1660         {
1661                 command_print(cmd_ctx, "wrote file %s to NAND flash %s "
1662                         "up to offset 0x%8.8" PRIx32 " in %fs (%0.3f kb/s)",
1663                         args[1], args[0], offset, duration_elapsed(&bench),
1664                         duration_kbps(&bench, fileio.size));
1665         }
1666
1667         return ERROR_OK;
1668 }
1669
1670 COMMAND_HANDLER(handle_nand_dump_command)
1671 {
1672         if (argc < 4)
1673         {
1674                 return ERROR_COMMAND_SYNTAX_ERROR;
1675         }
1676
1677         struct nand_device *p;
1678         int retval = nand_command_get_device_by_num(cmd_ctx, args[0], &p);
1679         if (ERROR_OK != retval)
1680                 return retval;
1681
1682         if (NULL == p->device)
1683         {
1684                 command_print(cmd_ctx, "#%s: not probed", args[0]);
1685                 return ERROR_OK;
1686         }
1687
1688         struct fileio fileio;
1689
1690         uint8_t *page = NULL;
1691         uint32_t page_size = 0;
1692         uint8_t *oob = NULL;
1693         uint32_t oob_size = 0;
1694         uint32_t address;
1695         COMMAND_PARSE_NUMBER(u32, args[2], address);
1696         uint32_t size;
1697         COMMAND_PARSE_NUMBER(u32, args[3], size);
1698         uint32_t bytes_done = 0;
1699         enum oob_formats oob_format = NAND_OOB_NONE;
1700
1701         if (argc > 4)
1702         {
1703                 for (unsigned i = 4; i < argc; i++)
1704                 {
1705                         if (!strcmp(args[i], "oob_raw"))
1706                                 oob_format |= NAND_OOB_RAW;
1707                         else if (!strcmp(args[i], "oob_only"))
1708                                 oob_format |= NAND_OOB_RAW | NAND_OOB_ONLY;
1709                         else
1710                                 command_print(cmd_ctx, "unknown option: '%s'", args[i]);
1711                 }
1712         }
1713
1714         if ((address % p->page_size) || (size % p->page_size))
1715         {
1716                 command_print(cmd_ctx, "only page size aligned addresses and sizes are supported");
1717                 return ERROR_OK;
1718         }
1719
1720         if (!(oob_format & NAND_OOB_ONLY))
1721         {
1722                 page_size = p->page_size;
1723                 page = malloc(p->page_size);
1724         }
1725
1726         if (oob_format & NAND_OOB_RAW)
1727         {
1728                 if (p->page_size == 512)
1729                         oob_size = 16;
1730                 else if (p->page_size == 2048)
1731                         oob_size = 64;
1732                 oob = malloc(oob_size);
1733         }
1734
1735         if (fileio_open(&fileio, args[1], FILEIO_WRITE, FILEIO_BINARY) != ERROR_OK)
1736         {
1737                 return ERROR_OK;
1738         }
1739
1740         struct duration bench;
1741         duration_start(&bench);
1742
1743         while (size > 0)
1744         {
1745                 uint32_t size_written;
1746                 if ((retval = nand_read_page(p, address / p->page_size, page, page_size, oob, oob_size)) != ERROR_OK)
1747                 {
1748                         command_print(cmd_ctx, "reading NAND flash page failed");
1749                         free(page);
1750                         free(oob);
1751                         fileio_close(&fileio);
1752                         return ERROR_OK;
1753                 }
1754
1755                 if (NULL != page)
1756                 {
1757                         fileio_write(&fileio, page_size, page, &size_written);
1758                         bytes_done += page_size;
1759                 }
1760
1761                 if (NULL != oob)
1762                 {
1763                         fileio_write(&fileio, oob_size, oob, &size_written);
1764                         bytes_done += oob_size;
1765                 }
1766
1767                 size -= p->page_size;
1768                 address += p->page_size;
1769         }
1770
1771         free(page);
1772         page = NULL;
1773         free(oob);
1774         oob = NULL;
1775         fileio_close(&fileio);
1776
1777         if (duration_measure(&bench) == ERROR_OK)
1778         {
1779                 command_print(cmd_ctx, "dumped %lld byte in %fs (%0.3f kb/s)",
1780                         fileio.size, duration_elapsed(&bench),
1781                         duration_kbps(&bench, fileio.size));
1782         }
1783
1784         return ERROR_OK;
1785 }
1786
1787 COMMAND_HANDLER(handle_nand_raw_access_command)
1788 {
1789         if ((argc < 1) || (argc > 2))
1790         {
1791                 return ERROR_COMMAND_SYNTAX_ERROR;
1792         }
1793
1794         struct nand_device *p;
1795         int retval = nand_command_get_device_by_num(cmd_ctx, args[0], &p);
1796         if (ERROR_OK != retval)
1797                 return retval;
1798
1799         if (NULL == p->device)
1800         {
1801                 command_print(cmd_ctx, "#%s: not probed", args[0]);
1802                 return ERROR_OK;
1803         }
1804
1805         if (argc == 2)
1806         {
1807                 if (strcmp("enable", args[1]) == 0)
1808                         p->use_raw = 1;
1809                 else if (strcmp("disable", args[1]) == 0)
1810                         p->use_raw = 0;
1811                 else
1812                         return ERROR_COMMAND_SYNTAX_ERROR;
1813         }
1814
1815         const char *msg = p->use_raw ? "enabled" : "disabled";
1816         command_print(cmd_ctx, "raw access is %s", msg);
1817
1818         return ERROR_OK;
1819 }
1820
1821 int nand_init(struct command_context *cmd_ctx)
1822 {
1823         if (!nand_devices)
1824                 return ERROR_OK;
1825
1826         register_command(cmd_ctx, nand_cmd, "list",
1827                         handle_nand_list_command, COMMAND_EXEC,
1828                         "list configured NAND flash devices");
1829         register_command(cmd_ctx, nand_cmd, "info",
1830                         handle_nand_info_command, COMMAND_EXEC,
1831                         "print info about NAND flash device <num>");
1832         register_command(cmd_ctx, nand_cmd, "probe",
1833                         handle_nand_probe_command, COMMAND_EXEC,
1834                         "identify NAND flash device <num>");
1835
1836         register_command(cmd_ctx, nand_cmd, "check_bad_blocks",
1837                         handle_nand_check_bad_blocks_command, COMMAND_EXEC,
1838                         "check NAND flash device <num> for bad blocks [<offset> <length>]");
1839         register_command(cmd_ctx, nand_cmd, "erase",
1840                         handle_nand_erase_command, COMMAND_EXEC,
1841                         "erase blocks on NAND flash device <num> [<offset> <length>]");
1842         register_command(cmd_ctx, nand_cmd, "dump",
1843                         handle_nand_dump_command, COMMAND_EXEC,
1844                         "dump from NAND flash device <num> <filename> "
1845                          "<offset> <length> [oob_raw | oob_only]");
1846         register_command(cmd_ctx, nand_cmd, "write",
1847                         handle_nand_write_command, COMMAND_EXEC,
1848                         "write to NAND flash device <num> <filename> <offset> "
1849                         "[oob_raw | oob_only | oob_softecc | oob_softecc_kw]");
1850
1851         register_command(cmd_ctx, nand_cmd, "raw_access",
1852                         handle_nand_raw_access_command, COMMAND_EXEC,
1853                         "raw access to NAND flash device <num> ['enable'|'disable']");
1854
1855         return ERROR_OK;
1856 }