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.
 
 
 
 
 
 

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