You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

1653 lines
42 KiB

  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. #include "nand.h"
  27. #include "time_support.h"
  28. #include "fileio.h"
  29. static int handle_nand_list_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  30. static int handle_nand_probe_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  31. static int handle_nand_check_bad_blocks_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  32. static int handle_nand_info_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  33. static int handle_nand_write_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  34. static int handle_nand_dump_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  35. static int handle_nand_erase_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  36. static int handle_nand_raw_access_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  37. static int nand_read_page(struct nand_device_s *device, uint32_t page, uint8_t *data, uint32_t data_size, uint8_t *oob, uint32_t oob_size);
  38. //static int nand_read_plain(struct nand_device_s *device, uint32_t address, uint8_t *data, uint32_t data_size);
  39. static int nand_write_page(struct nand_device_s *device, uint32_t page, uint8_t *data, uint32_t data_size, uint8_t *oob, uint32_t oob_size);
  40. /* NAND flash controller
  41. */
  42. extern nand_flash_controller_t davinci_nand_controller;
  43. extern nand_flash_controller_t lpc3180_nand_controller;
  44. extern nand_flash_controller_t orion_nand_controller;
  45. extern nand_flash_controller_t s3c2410_nand_controller;
  46. extern nand_flash_controller_t s3c2412_nand_controller;
  47. extern nand_flash_controller_t s3c2440_nand_controller;
  48. extern nand_flash_controller_t s3c2443_nand_controller;
  49. /* extern nand_flash_controller_t boundary_scan_nand_controller; */
  50. static nand_flash_controller_t *nand_flash_controllers[] =
  51. {
  52. &davinci_nand_controller,
  53. &lpc3180_nand_controller,
  54. &orion_nand_controller,
  55. &s3c2410_nand_controller,
  56. &s3c2412_nand_controller,
  57. &s3c2440_nand_controller,
  58. &s3c2443_nand_controller,
  59. /* &boundary_scan_nand_controller, */
  60. NULL
  61. };
  62. /* configured NAND devices and NAND Flash command handler */
  63. static nand_device_t *nand_devices = NULL;
  64. static command_t *nand_cmd;
  65. /* Chip ID list
  66. *
  67. * Name, ID code, pagesize, chipsize in MegaByte, eraseblock size,
  68. * options
  69. *
  70. * Pagesize; 0, 256, 512
  71. * 0 get this information from the extended chip ID
  72. * 256 256 Byte page size
  73. * 512 512 Byte page size
  74. */
  75. static nand_info_t nand_flash_ids[] =
  76. {
  77. /* start "museum" IDs */
  78. {"NAND 1MiB 5V 8-bit", 0x6e, 256, 1, 0x1000, 0},
  79. {"NAND 2MiB 5V 8-bit", 0x64, 256, 2, 0x1000, 0},
  80. {"NAND 4MiB 5V 8-bit", 0x6b, 512, 4, 0x2000, 0},
  81. {"NAND 1MiB 3,3V 8-bit", 0xe8, 256, 1, 0x1000, 0},
  82. {"NAND 1MiB 3,3V 8-bit", 0xec, 256, 1, 0x1000, 0},
  83. {"NAND 2MiB 3,3V 8-bit", 0xea, 256, 2, 0x1000, 0},
  84. {"NAND 4MiB 3,3V 8-bit", 0xd5, 512, 4, 0x2000, 0},
  85. {"NAND 4MiB 3,3V 8-bit", 0xe3, 512, 4, 0x2000, 0},
  86. {"NAND 4MiB 3,3V 8-bit", 0xe5, 512, 4, 0x2000, 0},
  87. {"NAND 8MiB 3,3V 8-bit", 0xd6, 512, 8, 0x2000, 0},
  88. {"NAND 8MiB 1,8V 8-bit", 0x39, 512, 8, 0x2000, 0},
  89. {"NAND 8MiB 3,3V 8-bit", 0xe6, 512, 8, 0x2000, 0},
  90. {"NAND 8MiB 1,8V 16-bit", 0x49, 512, 8, 0x2000, NAND_BUSWIDTH_16},
  91. {"NAND 8MiB 3,3V 16-bit", 0x59, 512, 8, 0x2000, NAND_BUSWIDTH_16},
  92. /* end "museum" IDs */
  93. {"NAND 16MiB 1,8V 8-bit", 0x33, 512, 16, 0x4000, 0},
  94. {"NAND 16MiB 3,3V 8-bit", 0x73, 512, 16, 0x4000, 0},
  95. {"NAND 16MiB 1,8V 16-bit", 0x43, 512, 16, 0x4000, NAND_BUSWIDTH_16},
  96. {"NAND 16MiB 3,3V 16-bit", 0x53, 512, 16, 0x4000, NAND_BUSWIDTH_16},
  97. {"NAND 32MiB 1,8V 8-bit", 0x35, 512, 32, 0x4000, 0},
  98. {"NAND 32MiB 3,3V 8-bit", 0x75, 512, 32, 0x4000, 0},
  99. {"NAND 32MiB 1,8V 16-bit", 0x45, 512, 32, 0x4000, NAND_BUSWIDTH_16},
  100. {"NAND 32MiB 3,3V 16-bit", 0x55, 512, 32, 0x4000, NAND_BUSWIDTH_16},
  101. {"NAND 64MiB 1,8V 8-bit", 0x36, 512, 64, 0x4000, 0},
  102. {"NAND 64MiB 3,3V 8-bit", 0x76, 512, 64, 0x4000, 0},
  103. {"NAND 64MiB 1,8V 16-bit", 0x46, 512, 64, 0x4000, NAND_BUSWIDTH_16},
  104. {"NAND 64MiB 3,3V 16-bit", 0x56, 512, 64, 0x4000, NAND_BUSWIDTH_16},
  105. {"NAND 128MiB 1,8V 8-bit", 0x78, 512, 128, 0x4000, 0},
  106. {"NAND 128MiB 1,8V 8-bit", 0x39, 512, 128, 0x4000, 0},
  107. {"NAND 128MiB 3,3V 8-bit", 0x79, 512, 128, 0x4000, 0},
  108. {"NAND 128MiB 1,8V 16-bit", 0x72, 512, 128, 0x4000, NAND_BUSWIDTH_16},
  109. {"NAND 128MiB 1,8V 16-bit", 0x49, 512, 128, 0x4000, NAND_BUSWIDTH_16},
  110. {"NAND 128MiB 3,3V 16-bit", 0x74, 512, 128, 0x4000, NAND_BUSWIDTH_16},
  111. {"NAND 128MiB 3,3V 16-bit", 0x59, 512, 128, 0x4000, NAND_BUSWIDTH_16},
  112. {"NAND 256MiB 3,3V 8-bit", 0x71, 512, 256, 0x4000, 0},
  113. {"NAND 64MiB 1,8V 8-bit", 0xA2, 0, 64, 0, LP_OPTIONS},
  114. {"NAND 64MiB 3,3V 8-bit", 0xF2, 0, 64, 0, LP_OPTIONS},
  115. {"NAND 64MiB 1,8V 16-bit", 0xB2, 0, 64, 0, LP_OPTIONS16},
  116. {"NAND 64MiB 3,3V 16-bit", 0xC2, 0, 64, 0, LP_OPTIONS16},
  117. {"NAND 128MiB 1,8V 8-bit", 0xA1, 0, 128, 0, LP_OPTIONS},
  118. {"NAND 128MiB 3,3V 8-bit", 0xF1, 0, 128, 0, LP_OPTIONS},
  119. {"NAND 128MiB 1,8V 16-bit", 0xB1, 0, 128, 0, LP_OPTIONS16},
  120. {"NAND 128MiB 3,3V 16-bit", 0xC1, 0, 128, 0, LP_OPTIONS16},
  121. {"NAND 256MiB 1,8V 8-bit", 0xAA, 0, 256, 0, LP_OPTIONS},
  122. {"NAND 256MiB 3,3V 8-bit", 0xDA, 0, 256, 0, LP_OPTIONS},
  123. {"NAND 256MiB 1,8V 16-bit", 0xBA, 0, 256, 0, LP_OPTIONS16},
  124. {"NAND 256MiB 3,3V 16-bit", 0xCA, 0, 256, 0, LP_OPTIONS16},
  125. {"NAND 512MiB 1,8V 8-bit", 0xAC, 0, 512, 0, LP_OPTIONS},
  126. {"NAND 512MiB 3,3V 8-bit", 0xDC, 0, 512, 0, LP_OPTIONS},
  127. {"NAND 512MiB 1,8V 16-bit", 0xBC, 0, 512, 0, LP_OPTIONS16},
  128. {"NAND 512MiB 3,3V 16-bit", 0xCC, 0, 512, 0, LP_OPTIONS16},
  129. {"NAND 1GiB 1,8V 8-bit", 0xA3, 0, 1024, 0, LP_OPTIONS},
  130. {"NAND 1GiB 3,3V 8-bit", 0xD3, 0, 1024, 0, LP_OPTIONS},
  131. {"NAND 1GiB 1,8V 16-bit", 0xB3, 0, 1024, 0, LP_OPTIONS16},
  132. {"NAND 1GiB 3,3V 16-bit", 0xC3, 0, 1024, 0, LP_OPTIONS16},
  133. {"NAND 2GiB 1,8V 8-bit", 0xA5, 0, 2048, 0, LP_OPTIONS},
  134. {"NAND 2GiB 3,3V 8-bit", 0xD5, 0, 2048, 0, LP_OPTIONS},
  135. {"NAND 2GiB 1,8V 16-bit", 0xB5, 0, 2048, 0, LP_OPTIONS16},
  136. {"NAND 2GiB 3,3V 16-bit", 0xC5, 0, 2048, 0, LP_OPTIONS16},
  137. {NULL, 0, 0, 0, 0, 0 }
  138. };
  139. /* Manufacturer ID list
  140. */
  141. static nand_manufacturer_t nand_manuf_ids[] =
  142. {
  143. {0x0, "unknown"},
  144. {NAND_MFR_TOSHIBA, "Toshiba"},
  145. {NAND_MFR_SAMSUNG, "Samsung"},
  146. {NAND_MFR_FUJITSU, "Fujitsu"},
  147. {NAND_MFR_NATIONAL, "National"},
  148. {NAND_MFR_RENESAS, "Renesas"},
  149. {NAND_MFR_STMICRO, "ST Micro"},
  150. {NAND_MFR_HYNIX, "Hynix"},
  151. {NAND_MFR_MICRON, "Micron"},
  152. {0x0, NULL},
  153. };
  154. /*
  155. * Define default oob placement schemes for large and small page devices
  156. */
  157. #if 0
  158. static nand_ecclayout_t nand_oob_8 = {
  159. .eccbytes = 3,
  160. .eccpos = {0, 1, 2},
  161. .oobfree = {
  162. {.offset = 3,
  163. .length = 2},
  164. {.offset = 6,
  165. .length = 2}}
  166. };
  167. #endif
  168. static nand_ecclayout_t nand_oob_16 = {
  169. .eccbytes = 6,
  170. .eccpos = {0, 1, 2, 3, 6, 7},
  171. .oobfree = {
  172. {.offset = 8,
  173. . length = 8}}
  174. };
  175. static nand_ecclayout_t nand_oob_64 = {
  176. .eccbytes = 24,
  177. .eccpos = {
  178. 40, 41, 42, 43, 44, 45, 46, 47,
  179. 48, 49, 50, 51, 52, 53, 54, 55,
  180. 56, 57, 58, 59, 60, 61, 62, 63},
  181. .oobfree = {
  182. {.offset = 2,
  183. .length = 38}}
  184. };
  185. /* nand device <nand_controller> [controller options]
  186. */
  187. static int handle_nand_device_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  188. {
  189. int i;
  190. int retval;
  191. if (argc < 1)
  192. {
  193. LOG_WARNING("incomplete flash device nand configuration");
  194. return ERROR_FLASH_BANK_INVALID;
  195. }
  196. for (i = 0; nand_flash_controllers[i]; i++)
  197. {
  198. nand_device_t *p, *c;
  199. if (strcmp(args[0], nand_flash_controllers[i]->name) == 0)
  200. {
  201. /* register flash specific commands */
  202. if ((retval = nand_flash_controllers[i]->register_commands(cmd_ctx)) != ERROR_OK)
  203. {
  204. LOG_ERROR("couldn't register '%s' commands", args[0]);
  205. return retval;
  206. }
  207. c = malloc(sizeof(nand_device_t));
  208. c->controller = nand_flash_controllers[i];
  209. c->controller_priv = NULL;
  210. c->manufacturer = NULL;
  211. c->device = NULL;
  212. c->bus_width = 0;
  213. c->address_cycles = 0;
  214. c->page_size = 0;
  215. c->use_raw = 0;
  216. c->next = NULL;
  217. if ((retval = nand_flash_controllers[i]->nand_device_command(cmd_ctx, cmd, args, argc, c)) != ERROR_OK)
  218. {
  219. LOG_ERROR("'%s' driver rejected nand flash", c->controller->name);
  220. free(c);
  221. return ERROR_OK;
  222. }
  223. /* put NAND device in linked list */
  224. if (nand_devices)
  225. {
  226. /* find last flash device */
  227. for (p = nand_devices; p && p->next; p = p->next);
  228. if (p)
  229. p->next = c;
  230. }
  231. else
  232. {
  233. nand_devices = c;
  234. }
  235. return ERROR_OK;
  236. }
  237. }
  238. /* no valid NAND controller was found (i.e. the configuration option,
  239. * didn't match one of the compiled-in controllers)
  240. */
  241. LOG_ERROR("No valid NAND flash controller found (%s)", args[0]);
  242. LOG_ERROR("compiled-in NAND flash controllers:");
  243. for (i = 0; nand_flash_controllers[i]; i++)
  244. {
  245. LOG_ERROR("%i: %s", i, nand_flash_controllers[i]->name);
  246. }
  247. return ERROR_OK;
  248. }
  249. int nand_register_commands(struct command_context_s *cmd_ctx)
  250. {
  251. nand_cmd = register_command(cmd_ctx, NULL, "nand", NULL, COMMAND_ANY, "NAND specific commands");
  252. register_command(cmd_ctx, nand_cmd, "device", handle_nand_device_command, COMMAND_CONFIG, NULL);
  253. return ERROR_OK;
  254. }
  255. int nand_init(struct command_context_s *cmd_ctx)
  256. {
  257. if (nand_devices)
  258. {
  259. register_command(cmd_ctx, nand_cmd, "list", handle_nand_list_command, COMMAND_EXEC,
  260. "list configured NAND flash devices");
  261. register_command(cmd_ctx, nand_cmd, "info", handle_nand_info_command, COMMAND_EXEC,
  262. "print info about NAND flash device <num>");
  263. register_command(cmd_ctx, nand_cmd, "probe", handle_nand_probe_command, COMMAND_EXEC,
  264. "identify NAND flash device <num>");
  265. register_command(cmd_ctx, nand_cmd, "check_bad_blocks", handle_nand_check_bad_blocks_command, COMMAND_EXEC,
  266. "check NAND flash device <num> for bad blocks [<offset> <length>]");
  267. register_command(cmd_ctx, nand_cmd, "erase", handle_nand_erase_command, COMMAND_EXEC,
  268. "erase blocks on NAND flash device <num> <offset> <length>");
  269. register_command(cmd_ctx, nand_cmd, "dump", handle_nand_dump_command, COMMAND_EXEC,
  270. "dump from NAND flash device <num> <filename> "
  271. "<offset> <length> [oob_raw | oob_only]");
  272. register_command(cmd_ctx, nand_cmd, "write", handle_nand_write_command, COMMAND_EXEC,
  273. "write to NAND flash device <num> <filename> <offset> [oob_raw | oob_only | oob_softecc | oob_softecc_kw]");
  274. register_command(cmd_ctx, nand_cmd, "raw_access", handle_nand_raw_access_command, COMMAND_EXEC,
  275. "raw access to NAND flash device <num> ['enable'|'disable']");
  276. }
  277. return ERROR_OK;
  278. }
  279. nand_device_t *get_nand_device_by_num(int num)
  280. {
  281. nand_device_t *p;
  282. int i = 0;
  283. for (p = nand_devices; p; p = p->next)
  284. {
  285. if (i++ == num)
  286. {
  287. return p;
  288. }
  289. }
  290. return NULL;
  291. }
  292. static int nand_build_bbt(struct nand_device_s *device, int first, int last)
  293. {
  294. uint32_t page = 0x0;
  295. int i;
  296. uint8_t oob[6];
  297. if ((first < 0) || (first >= device->num_blocks))
  298. first = 0;
  299. if ((last >= device->num_blocks) || (last == -1))
  300. last = device->num_blocks - 1;
  301. for (i = first; i < last; i++)
  302. {
  303. nand_read_page(device, page, NULL, 0, oob, 6);
  304. if (((device->device->options & NAND_BUSWIDTH_16) && ((oob[0] & oob[1]) != 0xff))
  305. || (((device->page_size == 512) && (oob[5] != 0xff)) ||
  306. ((device->page_size == 2048) && (oob[0] != 0xff))))
  307. {
  308. LOG_WARNING("bad block: %i", i);
  309. device->blocks[i].is_bad = 1;
  310. }
  311. else
  312. {
  313. device->blocks[i].is_bad = 0;
  314. }
  315. page += (device->erase_size / device->page_size);
  316. }
  317. return ERROR_OK;
  318. }
  319. int nand_read_status(struct nand_device_s *device, uint8_t *status)
  320. {
  321. if (!device->device)
  322. return ERROR_NAND_DEVICE_NOT_PROBED;
  323. /* Send read status command */
  324. device->controller->command(device, NAND_CMD_STATUS);
  325. alive_sleep(1);
  326. /* read status */
  327. if (device->device->options & NAND_BUSWIDTH_16)
  328. {
  329. uint16_t data;
  330. device->controller->read_data(device, &data);
  331. *status = data & 0xff;
  332. }
  333. else
  334. {
  335. device->controller->read_data(device, status);
  336. }
  337. return ERROR_OK;
  338. }
  339. static int nand_poll_ready(struct nand_device_s *device, int timeout)
  340. {
  341. uint8_t status;
  342. device->controller->command(device, NAND_CMD_STATUS);
  343. do {
  344. if (device->device->options & NAND_BUSWIDTH_16) {
  345. uint16_t data;
  346. device->controller->read_data(device, &data);
  347. status = data & 0xff;
  348. } else {
  349. device->controller->read_data(device, &status);
  350. }
  351. if (status & NAND_STATUS_READY)
  352. break;
  353. alive_sleep(1);
  354. } while (timeout--);
  355. return (status & NAND_STATUS_READY) != 0;
  356. }
  357. int nand_probe(struct nand_device_s *device)
  358. {
  359. uint8_t manufacturer_id, device_id;
  360. uint8_t id_buff[6];
  361. int retval;
  362. int i;
  363. /* clear device data */
  364. device->device = NULL;
  365. device->manufacturer = NULL;
  366. /* clear device parameters */
  367. device->bus_width = 0;
  368. device->address_cycles = 0;
  369. device->page_size = 0;
  370. device->erase_size = 0;
  371. /* initialize controller (device parameters are zero, use controller default) */
  372. if ((retval = device->controller->init(device) != ERROR_OK))
  373. {
  374. switch (retval)
  375. {
  376. case ERROR_NAND_OPERATION_FAILED:
  377. LOG_DEBUG("controller initialization failed");
  378. return ERROR_NAND_OPERATION_FAILED;
  379. case ERROR_NAND_OPERATION_NOT_SUPPORTED:
  380. LOG_ERROR("BUG: controller reported that it doesn't support default parameters");
  381. return ERROR_NAND_OPERATION_FAILED;
  382. default:
  383. LOG_ERROR("BUG: unknown controller initialization failure");
  384. return ERROR_NAND_OPERATION_FAILED;
  385. }
  386. }
  387. device->controller->command(device, NAND_CMD_RESET);
  388. device->controller->reset(device);
  389. device->controller->command(device, NAND_CMD_READID);
  390. device->controller->address(device, 0x0);
  391. if (device->bus_width == 8)
  392. {
  393. device->controller->read_data(device, &manufacturer_id);
  394. device->controller->read_data(device, &device_id);
  395. }
  396. else
  397. {
  398. uint16_t data_buf;
  399. device->controller->read_data(device, &data_buf);
  400. manufacturer_id = data_buf & 0xff;
  401. device->controller->read_data(device, &data_buf);
  402. device_id = data_buf & 0xff;
  403. }
  404. for (i = 0; nand_flash_ids[i].name; i++)
  405. {
  406. if (nand_flash_ids[i].id == device_id)
  407. {
  408. device->device = &nand_flash_ids[i];
  409. break;
  410. }
  411. }
  412. for (i = 0; nand_manuf_ids[i].name; i++)
  413. {
  414. if (nand_manuf_ids[i].id == manufacturer_id)
  415. {
  416. device->manufacturer = &nand_manuf_ids[i];
  417. break;
  418. }
  419. }
  420. if (!device->manufacturer)
  421. {
  422. device->manufacturer = &nand_manuf_ids[0];
  423. device->manufacturer->id = manufacturer_id;
  424. }
  425. if (!device->device)
  426. {
  427. LOG_ERROR("unknown NAND flash device found, manufacturer id: 0x%2.2x device id: 0x%2.2x",
  428. manufacturer_id, device_id);
  429. return ERROR_NAND_OPERATION_FAILED;
  430. }
  431. LOG_DEBUG("found %s (%s)", device->device->name, device->manufacturer->name);
  432. /* initialize device parameters */
  433. /* bus width */
  434. if (device->device->options & NAND_BUSWIDTH_16)
  435. device->bus_width = 16;
  436. else
  437. device->bus_width = 8;
  438. /* Do we need extended device probe information? */
  439. if (device->device->page_size == 0 ||
  440. device->device->erase_size == 0)
  441. {
  442. if (device->bus_width == 8)
  443. {
  444. device->controller->read_data(device, id_buff + 3);
  445. device->controller->read_data(device, id_buff + 4);
  446. device->controller->read_data(device, id_buff + 5);
  447. }
  448. else
  449. {
  450. uint16_t data_buf;
  451. device->controller->read_data(device, &data_buf);
  452. id_buff[3] = data_buf;
  453. device->controller->read_data(device, &data_buf);
  454. id_buff[4] = data_buf;
  455. device->controller->read_data(device, &data_buf);
  456. id_buff[5] = data_buf >> 8;
  457. }
  458. }
  459. /* page size */
  460. if (device->device->page_size == 0)
  461. {
  462. device->page_size = 1 << (10 + (id_buff[4] & 3));
  463. }
  464. else if (device->device->page_size == 256)
  465. {
  466. LOG_ERROR("NAND flashes with 256 byte pagesize are not supported");
  467. return ERROR_NAND_OPERATION_FAILED;
  468. }
  469. else
  470. {
  471. device->page_size = device->device->page_size;
  472. }
  473. /* number of address cycles */
  474. if (device->page_size <= 512)
  475. {
  476. /* small page devices */
  477. if (device->device->chip_size <= 32)
  478. device->address_cycles = 3;
  479. else if (device->device->chip_size <= 8*1024)
  480. device->address_cycles = 4;
  481. else
  482. {
  483. LOG_ERROR("BUG: small page NAND device with more than 8 GiB encountered");
  484. device->address_cycles = 5;
  485. }
  486. }
  487. else
  488. {
  489. /* large page devices */
  490. if (device->device->chip_size <= 128)
  491. device->address_cycles = 4;
  492. else if (device->device->chip_size <= 32*1024)
  493. device->address_cycles = 5;
  494. else
  495. {
  496. LOG_ERROR("BUG: large page NAND device with more than 32 GiB encountered");
  497. device->address_cycles = 6;
  498. }
  499. }
  500. /* erase size */
  501. if (device->device->erase_size == 0)
  502. {
  503. switch ((id_buff[4] >> 4) & 3) {
  504. case 0:
  505. device->erase_size = 64 << 10;
  506. break;
  507. case 1:
  508. device->erase_size = 128 << 10;
  509. break;
  510. case 2:
  511. device->erase_size = 256 << 10;
  512. break;
  513. case 3:
  514. device->erase_size =512 << 10;
  515. break;
  516. }
  517. }
  518. else
  519. {
  520. device->erase_size = device->device->erase_size;
  521. }
  522. /* initialize controller, but leave parameters at the controllers default */
  523. if ((retval = device->controller->init(device) != ERROR_OK))
  524. {
  525. switch (retval)
  526. {
  527. case ERROR_NAND_OPERATION_FAILED:
  528. LOG_DEBUG("controller initialization failed");
  529. return ERROR_NAND_OPERATION_FAILED;
  530. case ERROR_NAND_OPERATION_NOT_SUPPORTED:
  531. LOG_ERROR("controller doesn't support requested parameters (buswidth: %i, address cycles: %i, page size: %i)",
  532. device->bus_width, device->address_cycles, device->page_size);
  533. return ERROR_NAND_OPERATION_FAILED;
  534. default:
  535. LOG_ERROR("BUG: unknown controller initialization failure");
  536. return ERROR_NAND_OPERATION_FAILED;
  537. }
  538. }
  539. device->num_blocks = (device->device->chip_size * 1024) / (device->erase_size / 1024);
  540. device->blocks = malloc(sizeof(nand_block_t) * device->num_blocks);
  541. for (i = 0; i < device->num_blocks; i++)
  542. {
  543. device->blocks[i].size = device->erase_size;
  544. device->blocks[i].offset = i * device->erase_size;
  545. device->blocks[i].is_erased = -1;
  546. device->blocks[i].is_bad = -1;
  547. }
  548. return ERROR_OK;
  549. }
  550. int nand_erase(struct nand_device_s *device, int first_block, int last_block)
  551. {
  552. int i;
  553. uint32_t page;
  554. uint8_t status;
  555. int retval;
  556. if (!device->device)
  557. return ERROR_NAND_DEVICE_NOT_PROBED;
  558. if ((first_block < 0) || (last_block > device->num_blocks))
  559. return ERROR_INVALID_ARGUMENTS;
  560. /* make sure we know if a block is bad before erasing it */
  561. for (i = first_block; i <= last_block; i++)
  562. {
  563. if (device->blocks[i].is_bad == -1)
  564. {
  565. nand_build_bbt(device, i, last_block);
  566. break;
  567. }
  568. }
  569. for (i = first_block; i <= last_block; i++)
  570. {
  571. /* Send erase setup command */
  572. device->controller->command(device, NAND_CMD_ERASE1);
  573. page = i * (device->erase_size / device->page_size);
  574. /* Send page address */
  575. if (device->page_size <= 512)
  576. {
  577. /* row */
  578. device->controller->address(device, page & 0xff);
  579. device->controller->address(device, (page >> 8) & 0xff);
  580. /* 3rd cycle only on devices with more than 32 MiB */
  581. if (device->address_cycles >= 4)
  582. device->controller->address(device, (page >> 16) & 0xff);
  583. /* 4th cycle only on devices with more than 8 GiB */
  584. if (device->address_cycles >= 5)
  585. device->controller->address(device, (page >> 24) & 0xff);
  586. }
  587. else
  588. {
  589. /* row */
  590. device->controller->address(device, page & 0xff);
  591. device->controller->address(device, (page >> 8) & 0xff);
  592. /* 3rd cycle only on devices with more than 128 MiB */
  593. if (device->address_cycles >= 5)
  594. device->controller->address(device, (page >> 16) & 0xff);
  595. }
  596. /* Send erase confirm command */
  597. device->controller->command(device, NAND_CMD_ERASE2);
  598. retval = device->controller->nand_ready ?
  599. device->controller->nand_ready(device, 1000) :
  600. nand_poll_ready(device, 1000);
  601. if (!retval) {
  602. LOG_ERROR("timeout waiting for NAND flash block erase to complete");
  603. return ERROR_NAND_OPERATION_TIMEOUT;
  604. }
  605. if ((retval = nand_read_status(device, &status)) != ERROR_OK)
  606. {
  607. LOG_ERROR("couldn't read status");
  608. return ERROR_NAND_OPERATION_FAILED;
  609. }
  610. if (status & 0x1)
  611. {
  612. LOG_ERROR("erase operation didn't pass, status: 0x%2.2x", status);
  613. return ERROR_NAND_OPERATION_FAILED;
  614. }
  615. device->blocks[i].is_erased = 1;
  616. }
  617. return ERROR_OK;
  618. }
  619. #if 0
  620. static int nand_read_plain(struct nand_device_s *device, uint32_t address, uint8_t *data, uint32_t data_size)
  621. {
  622. uint8_t *page;
  623. if (!device->device)
  624. return ERROR_NAND_DEVICE_NOT_PROBED;
  625. if (address % device->page_size)
  626. {
  627. LOG_ERROR("reads need to be page aligned");
  628. return ERROR_NAND_OPERATION_FAILED;
  629. }
  630. page = malloc(device->page_size);
  631. while (data_size > 0)
  632. {
  633. uint32_t thisrun_size = (data_size > device->page_size) ? device->page_size : data_size;
  634. uint32_t page_address;
  635. page_address = address / device->page_size;
  636. nand_read_page(device, page_address, page, device->page_size, NULL, 0);
  637. memcpy(data, page, thisrun_size);
  638. address += thisrun_size;
  639. data += thisrun_size;
  640. data_size -= thisrun_size;
  641. }
  642. free(page);
  643. return ERROR_OK;
  644. }
  645. static int nand_write_plain(struct nand_device_s *device, uint32_t address, uint8_t *data, uint32_t data_size)
  646. {
  647. uint8_t *page;
  648. if (!device->device)
  649. return ERROR_NAND_DEVICE_NOT_PROBED;
  650. if (address % device->page_size)
  651. {
  652. LOG_ERROR("writes need to be page aligned");
  653. return ERROR_NAND_OPERATION_FAILED;
  654. }
  655. page = malloc(device->page_size);
  656. while (data_size > 0)
  657. {
  658. uint32_t thisrun_size = (data_size > device->page_size) ? device->page_size : data_size;
  659. uint32_t page_address;
  660. memset(page, 0xff, device->page_size);
  661. memcpy(page, data, thisrun_size);
  662. page_address = address / device->page_size;
  663. nand_write_page(device, page_address, page, device->page_size, NULL, 0);
  664. address += thisrun_size;
  665. data += thisrun_size;
  666. data_size -= thisrun_size;
  667. }
  668. free(page);
  669. return ERROR_OK;
  670. }
  671. #endif
  672. int nand_write_page(struct nand_device_s *device, uint32_t page, uint8_t *data, uint32_t data_size, uint8_t *oob, uint32_t oob_size)
  673. {
  674. uint32_t block;
  675. if (!device->device)
  676. return ERROR_NAND_DEVICE_NOT_PROBED;
  677. block = page / (device->erase_size / device->page_size);
  678. if (device->blocks[block].is_erased == 1)
  679. device->blocks[block].is_erased = 0;
  680. if (device->use_raw || device->controller->write_page == NULL)
  681. return nand_write_page_raw(device, page, data, data_size, oob, oob_size);
  682. else
  683. return device->controller->write_page(device, page, data, data_size, oob, oob_size);
  684. }
  685. static int nand_read_page(struct nand_device_s *device, uint32_t page, uint8_t *data, uint32_t data_size, uint8_t *oob, uint32_t oob_size)
  686. {
  687. if (!device->device)
  688. return ERROR_NAND_DEVICE_NOT_PROBED;
  689. if (device->use_raw || device->controller->read_page == NULL)
  690. return nand_read_page_raw(device, page, data, data_size, oob, oob_size);
  691. else
  692. return device->controller->read_page(device, page, data, data_size, oob, oob_size);
  693. }
  694. int nand_read_page_raw(struct nand_device_s *device, uint32_t page, uint8_t *data, uint32_t data_size, uint8_t *oob, uint32_t oob_size)
  695. {
  696. uint32_t i;
  697. if (!device->device)
  698. return ERROR_NAND_DEVICE_NOT_PROBED;
  699. if (device->page_size <= 512)
  700. {
  701. /* small page device */
  702. if (data)
  703. device->controller->command(device, NAND_CMD_READ0);
  704. else
  705. device->controller->command(device, NAND_CMD_READOOB);
  706. /* column (always 0, we start at the beginning of a page/OOB area) */
  707. device->controller->address(device, 0x0);
  708. /* row */
  709. device->controller->address(device, page & 0xff);
  710. device->controller->address(device, (page >> 8) & 0xff);
  711. /* 4th cycle only on devices with more than 32 MiB */
  712. if (device->address_cycles >= 4)
  713. device->controller->address(device, (page >> 16) & 0xff);
  714. /* 5th cycle only on devices with more than 8 GiB */
  715. if (device->address_cycles >= 5)
  716. device->controller->address(device, (page >> 24) & 0xff);
  717. }
  718. else
  719. {
  720. /* large page device */
  721. device->controller->command(device, NAND_CMD_READ0);
  722. /* column (0 when we start at the beginning of a page,
  723. * or 2048 for the beginning of OOB area)
  724. */
  725. device->controller->address(device, 0x0);
  726. if (data)
  727. device->controller->address(device, 0x0);
  728. else
  729. device->controller->address(device, 0x8);
  730. /* row */
  731. device->controller->address(device, page & 0xff);
  732. device->controller->address(device, (page >> 8) & 0xff);
  733. /* 5th cycle only on devices with more than 128 MiB */
  734. if (device->address_cycles >= 5)
  735. device->controller->address(device, (page >> 16) & 0xff);
  736. /* large page devices need a start command */
  737. device->controller->command(device, NAND_CMD_READSTART);
  738. }
  739. if (device->controller->nand_ready) {
  740. if (!device->controller->nand_ready(device, 100))
  741. return ERROR_NAND_OPERATION_TIMEOUT;
  742. } else {
  743. alive_sleep(1);
  744. }
  745. if (data)
  746. {
  747. if (device->controller->read_block_data != NULL)
  748. (device->controller->read_block_data)(device, data, data_size);
  749. else
  750. {
  751. for (i = 0; i < data_size;)
  752. {
  753. if (device->device->options & NAND_BUSWIDTH_16)
  754. {
  755. device->controller->read_data(device, data);
  756. data += 2;
  757. i += 2;
  758. }
  759. else
  760. {
  761. device->controller->read_data(device, data);
  762. data += 1;
  763. i += 1;
  764. }
  765. }
  766. }
  767. }
  768. if (oob)
  769. {
  770. if (device->controller->read_block_data != NULL)
  771. (device->controller->read_block_data)(device, oob, oob_size);
  772. else
  773. {
  774. for (i = 0; i < oob_size;)
  775. {
  776. if (device->device->options & NAND_BUSWIDTH_16)
  777. {
  778. device->controller->read_data(device, oob);
  779. oob += 2;
  780. i += 2;
  781. }
  782. else
  783. {
  784. device->controller->read_data(device, oob);
  785. oob += 1;
  786. i += 1;
  787. }
  788. }
  789. }
  790. }
  791. return ERROR_OK;
  792. }
  793. int nand_write_page_raw(struct nand_device_s *device, uint32_t page, uint8_t *data, uint32_t data_size, uint8_t *oob, uint32_t oob_size)
  794. {
  795. uint32_t i;
  796. int retval;
  797. uint8_t status;
  798. if (!device->device)
  799. return ERROR_NAND_DEVICE_NOT_PROBED;
  800. device->controller->command(device, NAND_CMD_SEQIN);
  801. if (device->page_size <= 512)
  802. {
  803. /* column (always 0, we start at the beginning of a page/OOB area) */
  804. device->controller->address(device, 0x0);
  805. /* row */
  806. device->controller->address(device, page & 0xff);
  807. device->controller->address(device, (page >> 8) & 0xff);
  808. /* 4th cycle only on devices with more than 32 MiB */
  809. if (device->address_cycles >= 4)
  810. device->controller->address(device, (page >> 16) & 0xff);
  811. /* 5th cycle only on devices with more than 8 GiB */
  812. if (device->address_cycles >= 5)
  813. device->controller->address(device, (page >> 24) & 0xff);
  814. }
  815. else
  816. {
  817. /* column (0 when we start at the beginning of a page,
  818. * or 2048 for the beginning of OOB area)
  819. */
  820. device->controller->address(device, 0x0);
  821. if (data)
  822. device->controller->address(device, 0x0);
  823. else
  824. device->controller->address(device, 0x8);
  825. /* row */
  826. device->controller->address(device, page & 0xff);
  827. device->controller->address(device, (page >> 8) & 0xff);
  828. /* 5th cycle only on devices with more than 128 MiB */
  829. if (device->address_cycles >= 5)
  830. device->controller->address(device, (page >> 16) & 0xff);
  831. }
  832. if (data)
  833. {
  834. if (device->controller->write_block_data != NULL)
  835. (device->controller->write_block_data)(device, data, data_size);
  836. else
  837. {
  838. for (i = 0; i < data_size;)
  839. {
  840. if (device->device->options & NAND_BUSWIDTH_16)
  841. {
  842. uint16_t data_buf = le_to_h_u16(data);
  843. device->controller->write_data(device, data_buf);
  844. data += 2;
  845. i += 2;
  846. }
  847. else
  848. {
  849. device->controller->write_data(device, *data);
  850. data += 1;
  851. i += 1;
  852. }
  853. }
  854. }
  855. }
  856. if (oob)
  857. {
  858. if (device->controller->write_block_data != NULL)
  859. (device->controller->write_block_data)(device, oob, oob_size);
  860. else
  861. {
  862. for (i = 0; i < oob_size;)
  863. {
  864. if (device->device->options & NAND_BUSWIDTH_16)
  865. {
  866. uint16_t oob_buf = le_to_h_u16(data);
  867. device->controller->write_data(device, oob_buf);
  868. oob += 2;
  869. i += 2;
  870. }
  871. else
  872. {
  873. device->controller->write_data(device, *oob);
  874. oob += 1;
  875. i += 1;
  876. }
  877. }
  878. }
  879. }
  880. device->controller->command(device, NAND_CMD_PAGEPROG);
  881. retval = device->controller->nand_ready ?
  882. device->controller->nand_ready(device, 100) :
  883. nand_poll_ready(device, 100);
  884. if (!retval)
  885. return ERROR_NAND_OPERATION_TIMEOUT;
  886. if ((retval = nand_read_status(device, &status)) != ERROR_OK)
  887. {
  888. LOG_ERROR("couldn't read status");
  889. return ERROR_NAND_OPERATION_FAILED;
  890. }
  891. if (status & NAND_STATUS_FAIL)
  892. {
  893. LOG_ERROR("write operation didn't pass, status: 0x%2.2x", status);
  894. return ERROR_NAND_OPERATION_FAILED;
  895. }
  896. return ERROR_OK;
  897. }
  898. int handle_nand_list_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  899. {
  900. nand_device_t *p;
  901. int i;
  902. if (!nand_devices)
  903. {
  904. command_print(cmd_ctx, "no NAND flash devices configured");
  905. return ERROR_OK;
  906. }
  907. for (p = nand_devices, i = 0; p; p = p->next, i++)
  908. {
  909. if (p->device)
  910. command_print(cmd_ctx, "#%i: %s (%s) pagesize: %i, buswidth: %i, erasesize: %i",
  911. i, p->device->name, p->manufacturer->name, p->page_size, p->bus_width, p->erase_size);
  912. else
  913. command_print(cmd_ctx, "#%i: not probed", i);
  914. }
  915. return ERROR_OK;
  916. }
  917. static int handle_nand_info_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  918. {
  919. nand_device_t *p;
  920. int i = 0;
  921. int j = 0;
  922. int first = -1;
  923. int last = -1;
  924. switch (argc) {
  925. default:
  926. return ERROR_COMMAND_SYNTAX_ERROR;
  927. case 1:
  928. first = 0;
  929. last = INT32_MAX;
  930. break;
  931. case 2:
  932. first = last = strtoul(args[1], NULL, 0);
  933. break;
  934. case 3:
  935. first = strtoul(args[1], NULL, 0);
  936. last = strtoul(args[2], NULL, 0);
  937. break;
  938. }
  939. p = get_nand_device_by_num(strtoul(args[0], NULL, 0));
  940. if (p)
  941. {
  942. if (p->device)
  943. {
  944. if (first >= p->num_blocks)
  945. first = p->num_blocks - 1;
  946. if (last >= p->num_blocks)
  947. last = p->num_blocks - 1;
  948. command_print(cmd_ctx, "#%i: %s (%s) pagesize: %i, buswidth: %i, erasesize: %i",
  949. i++, p->device->name, p->manufacturer->name, p->page_size, p->bus_width, p->erase_size);
  950. for (j = first; j <= last; j++)
  951. {
  952. char *erase_state, *bad_state;
  953. if (p->blocks[j].is_erased == 0)
  954. erase_state = "not erased";
  955. else if (p->blocks[j].is_erased == 1)
  956. erase_state = "erased";
  957. else
  958. erase_state = "erase state unknown";
  959. if (p->blocks[j].is_bad == 0)
  960. bad_state = "";
  961. else if (p->blocks[j].is_bad == 1)
  962. bad_state = " (marked bad)";
  963. else
  964. bad_state = " (block condition unknown)";
  965. command_print(cmd_ctx,
  966. "\t#%i: 0x%8.8" PRIx32 " (%" PRId32 "kB) %s%s",
  967. j,
  968. p->blocks[j].offset,
  969. p->blocks[j].size / 1024,
  970. erase_state,
  971. bad_state);
  972. }
  973. }
  974. else
  975. {
  976. command_print(cmd_ctx, "#%s: not probed", args[0]);
  977. }
  978. }
  979. return ERROR_OK;
  980. }
  981. static int handle_nand_probe_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  982. {
  983. nand_device_t *p;
  984. int retval;
  985. if (argc != 1)
  986. {
  987. return ERROR_COMMAND_SYNTAX_ERROR;
  988. }
  989. p = get_nand_device_by_num(strtoul(args[0], NULL, 0));
  990. if (p)
  991. {
  992. if ((retval = nand_probe(p)) == ERROR_OK)
  993. {
  994. command_print(cmd_ctx, "NAND flash device '%s' found", p->device->name);
  995. }
  996. else if (retval == ERROR_NAND_OPERATION_FAILED)
  997. {
  998. command_print(cmd_ctx, "probing failed for NAND flash device");
  999. }
  1000. else
  1001. {
  1002. command_print(cmd_ctx, "unknown error when probing NAND flash device");
  1003. }
  1004. }
  1005. else
  1006. {
  1007. command_print(cmd_ctx, "NAND flash device '#%s' is out of bounds", args[0]);
  1008. }
  1009. return ERROR_OK;
  1010. }
  1011. static int handle_nand_erase_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  1012. {
  1013. nand_device_t *p;
  1014. int retval;
  1015. if (argc != 3)
  1016. {
  1017. return ERROR_COMMAND_SYNTAX_ERROR;
  1018. }
  1019. p = get_nand_device_by_num(strtoul(args[0], NULL, 0));
  1020. if (p)
  1021. {
  1022. char *cp;
  1023. unsigned long offset;
  1024. unsigned long length;
  1025. offset = strtoul(args[1], &cp, 0);
  1026. if (*cp || offset == ULONG_MAX || offset % p->erase_size)
  1027. {
  1028. return ERROR_INVALID_ARGUMENTS;
  1029. }
  1030. offset /= p->erase_size;
  1031. length = strtoul(args[2], &cp, 0);
  1032. if (*cp || length == ULONG_MAX || length % p->erase_size)
  1033. {
  1034. return ERROR_INVALID_ARGUMENTS;
  1035. }
  1036. length -= 1;
  1037. length /= p->erase_size;
  1038. retval = nand_erase(p, offset, offset + length);
  1039. if (retval == ERROR_OK)
  1040. {
  1041. command_print(cmd_ctx, "successfully erased blocks "
  1042. "%lu to %lu on NAND flash device '%s'",
  1043. offset, offset + length, p->device->name);
  1044. }
  1045. else if (retval == ERROR_NAND_OPERATION_FAILED)
  1046. {
  1047. command_print(cmd_ctx, "erase failed");
  1048. }
  1049. else
  1050. {
  1051. command_print(cmd_ctx, "unknown error when erasing NAND flash device");
  1052. }
  1053. }
  1054. else
  1055. {
  1056. command_print(cmd_ctx, "NAND flash device '#%s' is out of bounds", args[0]);
  1057. }
  1058. return ERROR_OK;
  1059. }
  1060. int handle_nand_check_bad_blocks_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  1061. {
  1062. nand_device_t *p;
  1063. int retval;
  1064. int first = -1;
  1065. int last = -1;
  1066. if ((argc < 1) || (argc > 3) || (argc == 2))
  1067. {
  1068. return ERROR_COMMAND_SYNTAX_ERROR;
  1069. }
  1070. p = get_nand_device_by_num(strtoul(args[0], NULL, 0));
  1071. if (!p) {
  1072. command_print(cmd_ctx, "NAND flash device '#%s' is out of bounds",
  1073. args[0]);
  1074. return ERROR_INVALID_ARGUMENTS;
  1075. }
  1076. if (argc == 3)
  1077. {
  1078. char *cp;
  1079. unsigned long offset;
  1080. unsigned long length;
  1081. offset = strtoul(args[1], &cp, 0);
  1082. if (*cp || offset == ULONG_MAX || offset % p->erase_size)
  1083. {
  1084. return ERROR_INVALID_ARGUMENTS;
  1085. }
  1086. offset /= p->erase_size;
  1087. length = strtoul(args[2], &cp, 0);
  1088. if (*cp || length == ULONG_MAX || length % p->erase_size)
  1089. {
  1090. return ERROR_INVALID_ARGUMENTS;
  1091. }
  1092. length -= 1;
  1093. length /= p->erase_size;
  1094. first = offset;
  1095. last = offset + length;
  1096. }
  1097. retval = nand_build_bbt(p, first, last);
  1098. if (retval == ERROR_OK)
  1099. {
  1100. command_print(cmd_ctx, "checked NAND flash device for bad blocks, "
  1101. "use \"nand info\" command to list blocks");
  1102. }
  1103. else if (retval == ERROR_NAND_OPERATION_FAILED)
  1104. {
  1105. command_print(cmd_ctx, "error when checking for bad blocks on "
  1106. "NAND flash device");
  1107. }
  1108. else
  1109. {
  1110. command_print(cmd_ctx, "unknown error when checking for bad "
  1111. "blocks on NAND flash device");
  1112. }
  1113. return ERROR_OK;
  1114. }
  1115. static int handle_nand_write_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  1116. {
  1117. uint32_t offset;
  1118. uint32_t binary_size;
  1119. uint32_t buf_cnt;
  1120. enum oob_formats oob_format = NAND_OOB_NONE;
  1121. fileio_t fileio;
  1122. duration_t duration;
  1123. char *duration_text;
  1124. nand_device_t *p;
  1125. if (argc < 3)
  1126. {
  1127. return ERROR_COMMAND_SYNTAX_ERROR;
  1128. }
  1129. p = get_nand_device_by_num(strtoul(args[0], NULL, 0));
  1130. if (p)
  1131. {
  1132. uint8_t *page = NULL;
  1133. uint32_t page_size = 0;
  1134. uint8_t *oob = NULL;
  1135. uint32_t oob_size = 0;
  1136. const int *eccpos = NULL;
  1137. offset = strtoul(args[2], NULL, 0);
  1138. if (argc > 3)
  1139. {
  1140. int i;
  1141. for (i = 3; i < argc; i++)
  1142. {
  1143. if (!strcmp(args[i], "oob_raw"))
  1144. oob_format |= NAND_OOB_RAW;
  1145. else if (!strcmp(args[i], "oob_only"))
  1146. oob_format |= NAND_OOB_RAW | NAND_OOB_ONLY;
  1147. else if (!strcmp(args[i], "oob_softecc"))
  1148. oob_format |= NAND_OOB_SW_ECC;
  1149. else if (!strcmp(args[i], "oob_softecc_kw"))
  1150. oob_format |= NAND_OOB_SW_ECC_KW;
  1151. else
  1152. {
  1153. command_print(cmd_ctx, "unknown option: %s", args[i]);
  1154. return ERROR_COMMAND_SYNTAX_ERROR;
  1155. }
  1156. }
  1157. }
  1158. duration_start_measure(&duration);
  1159. if (fileio_open(&fileio, args[1], FILEIO_READ, FILEIO_BINARY) != ERROR_OK)
  1160. {
  1161. return ERROR_OK;
  1162. }
  1163. buf_cnt = binary_size = fileio.size;
  1164. if (!(oob_format & NAND_OOB_ONLY))
  1165. {
  1166. page_size = p->page_size;
  1167. page = malloc(p->page_size);
  1168. }
  1169. if (oob_format & (NAND_OOB_RAW | NAND_OOB_SW_ECC | NAND_OOB_SW_ECC_KW))
  1170. {
  1171. if (p->page_size == 512) {
  1172. oob_size = 16;
  1173. eccpos = nand_oob_16.eccpos;
  1174. } else if (p->page_size == 2048) {
  1175. oob_size = 64;
  1176. eccpos = nand_oob_64.eccpos;
  1177. }
  1178. oob = malloc(oob_size);
  1179. }
  1180. if (offset % p->page_size)
  1181. {
  1182. command_print(cmd_ctx, "only page size aligned offsets and sizes are supported");
  1183. fileio_close(&fileio);
  1184. free(oob);
  1185. free(page);
  1186. return ERROR_OK;
  1187. }
  1188. while (buf_cnt > 0)
  1189. {
  1190. uint32_t size_read;
  1191. if (NULL != page)
  1192. {
  1193. fileio_read(&fileio, page_size, page, &size_read);
  1194. buf_cnt -= size_read;
  1195. if (size_read < page_size)
  1196. {
  1197. memset(page + size_read, 0xff, page_size - size_read);
  1198. }
  1199. }
  1200. if (oob_format & NAND_OOB_SW_ECC)
  1201. {
  1202. uint32_t i, j;
  1203. uint8_t ecc[3];
  1204. memset(oob, 0xff, oob_size);
  1205. for (i = 0, j = 0; i < page_size; i += 256) {
  1206. nand_calculate_ecc(p, page + i, ecc);
  1207. oob[eccpos[j++]] = ecc[0];
  1208. oob[eccpos[j++]] = ecc[1];
  1209. oob[eccpos[j++]] = ecc[2];
  1210. }
  1211. } else if (oob_format & NAND_OOB_SW_ECC_KW)
  1212. {
  1213. /*
  1214. * In this case eccpos is not used as
  1215. * the ECC data is always stored contigously
  1216. * at the end of the OOB area. It consists
  1217. * of 10 bytes per 512-byte data block.
  1218. */
  1219. uint32_t i;
  1220. uint8_t *ecc = oob + oob_size - page_size/512 * 10;
  1221. memset(oob, 0xff, oob_size);
  1222. for (i = 0; i < page_size; i += 512) {
  1223. nand_calculate_ecc_kw(p, page + i, ecc);
  1224. ecc += 10;
  1225. }
  1226. }
  1227. else if (NULL != oob)
  1228. {
  1229. fileio_read(&fileio, oob_size, oob, &size_read);
  1230. buf_cnt -= size_read;
  1231. if (size_read < oob_size)
  1232. {
  1233. memset(oob + size_read, 0xff, oob_size - size_read);
  1234. }
  1235. }
  1236. if (nand_write_page(p, offset / p->page_size, page, page_size, oob, oob_size) != ERROR_OK)
  1237. {
  1238. command_print(cmd_ctx, "failed writing file %s to NAND flash %s at offset 0x%8.8" PRIx32 "",
  1239. args[1], args[0], offset);
  1240. fileio_close(&fileio);
  1241. free(oob);
  1242. free(page);
  1243. return ERROR_OK;
  1244. }
  1245. offset += page_size;
  1246. }
  1247. fileio_close(&fileio);
  1248. free(oob);
  1249. free(page);
  1250. oob = NULL;
  1251. page = NULL;
  1252. duration_stop_measure(&duration, &duration_text);
  1253. command_print(cmd_ctx, "wrote file %s to NAND flash %s up to offset 0x%8.8" PRIx32 " in %s",
  1254. args[1], args[0], offset, duration_text);
  1255. free(duration_text);
  1256. duration_text = NULL;
  1257. }
  1258. else
  1259. {
  1260. command_print(cmd_ctx, "NAND flash device '#%s' is out of bounds", args[0]);
  1261. }
  1262. return ERROR_OK;
  1263. }
  1264. static int handle_nand_dump_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  1265. {
  1266. nand_device_t *p;
  1267. if (argc < 4)
  1268. {
  1269. return ERROR_COMMAND_SYNTAX_ERROR;
  1270. }
  1271. p = get_nand_device_by_num(strtoul(args[0], NULL, 0));
  1272. if (p)
  1273. {
  1274. if (p->device)
  1275. {
  1276. fileio_t fileio;
  1277. duration_t duration;
  1278. char *duration_text;
  1279. int retval;
  1280. uint8_t *page = NULL;
  1281. uint32_t page_size = 0;
  1282. uint8_t *oob = NULL;
  1283. uint32_t oob_size = 0;
  1284. uint32_t address = strtoul(args[2], NULL, 0);
  1285. uint32_t size = strtoul(args[3], NULL, 0);
  1286. uint32_t bytes_done = 0;
  1287. enum oob_formats oob_format = NAND_OOB_NONE;
  1288. if (argc > 4)
  1289. {
  1290. int i;
  1291. for (i = 4; i < argc; i++)
  1292. {
  1293. if (!strcmp(args[i], "oob_raw"))
  1294. oob_format |= NAND_OOB_RAW;
  1295. else if (!strcmp(args[i], "oob_only"))
  1296. oob_format |= NAND_OOB_RAW | NAND_OOB_ONLY;
  1297. else
  1298. command_print(cmd_ctx, "unknown option: '%s'", args[i]);
  1299. }
  1300. }
  1301. if ((address % p->page_size) || (size % p->page_size))
  1302. {
  1303. command_print(cmd_ctx, "only page size aligned addresses and sizes are supported");
  1304. return ERROR_OK;
  1305. }
  1306. if (!(oob_format & NAND_OOB_ONLY))
  1307. {
  1308. page_size = p->page_size;
  1309. page = malloc(p->page_size);
  1310. }
  1311. if (oob_format & NAND_OOB_RAW)
  1312. {
  1313. if (p->page_size == 512)
  1314. oob_size = 16;
  1315. else if (p->page_size == 2048)
  1316. oob_size = 64;
  1317. oob = malloc(oob_size);
  1318. }
  1319. if (fileio_open(&fileio, args[1], FILEIO_WRITE, FILEIO_BINARY) != ERROR_OK)
  1320. {
  1321. return ERROR_OK;
  1322. }
  1323. duration_start_measure(&duration);
  1324. while (size > 0)
  1325. {
  1326. uint32_t size_written;
  1327. if ((retval = nand_read_page(p, address / p->page_size, page, page_size, oob, oob_size)) != ERROR_OK)
  1328. {
  1329. command_print(cmd_ctx, "reading NAND flash page failed");
  1330. free(page);
  1331. free(oob);
  1332. fileio_close(&fileio);
  1333. return ERROR_OK;
  1334. }
  1335. if (NULL != page)
  1336. {
  1337. fileio_write(&fileio, page_size, page, &size_written);
  1338. bytes_done += page_size;
  1339. }
  1340. if (NULL != oob)
  1341. {
  1342. fileio_write(&fileio, oob_size, oob, &size_written);
  1343. bytes_done += oob_size;
  1344. }
  1345. size -= p->page_size;
  1346. address += p->page_size;
  1347. }
  1348. free(page);
  1349. page = NULL;
  1350. free(oob);
  1351. oob = NULL;
  1352. fileio_close(&fileio);
  1353. duration_stop_measure(&duration, &duration_text);
  1354. command_print(cmd_ctx, "dumped %lld byte in %s", fileio.size, duration_text);
  1355. free(duration_text);
  1356. duration_text = NULL;
  1357. }
  1358. else
  1359. {
  1360. command_print(cmd_ctx, "#%s: not probed", args[0]);
  1361. }
  1362. }
  1363. else
  1364. {
  1365. command_print(cmd_ctx, "NAND flash device '#%s' is out of bounds", args[0]);
  1366. }
  1367. return ERROR_OK;
  1368. }
  1369. static int handle_nand_raw_access_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  1370. {
  1371. nand_device_t *p;
  1372. if ((argc < 1) || (argc > 2))
  1373. {
  1374. return ERROR_COMMAND_SYNTAX_ERROR;
  1375. }
  1376. p = get_nand_device_by_num(strtoul(args[0], NULL, 0));
  1377. if (p)
  1378. {
  1379. if (p->device)
  1380. {
  1381. if (argc == 2)
  1382. {
  1383. if (strcmp("enable", args[1]) == 0)
  1384. {
  1385. p->use_raw = 1;
  1386. }
  1387. else if (strcmp("disable", args[1]) == 0)
  1388. {
  1389. p->use_raw = 0;
  1390. }
  1391. else
  1392. {
  1393. return ERROR_COMMAND_SYNTAX_ERROR;
  1394. }
  1395. }
  1396. command_print(cmd_ctx, "raw access is %s", (p->use_raw) ? "enabled" : "disabled");
  1397. }
  1398. else
  1399. {
  1400. command_print(cmd_ctx, "#%s: not probed", args[0]);
  1401. }
  1402. }
  1403. else
  1404. {
  1405. command_print(cmd_ctx, "NAND flash device '#%s' is out of bounds", args[0]);
  1406. }
  1407. return ERROR_OK;
  1408. }