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.
 
 
 
 
 
 

1480 lines
38 KiB

  1. /***************************************************************************
  2. * Copyright (C) 2007 by Dominic Rath *
  3. * Dominic.Rath@gmx.de *
  4. * *
  5. * partially based on *
  6. * drivers/mtd/nand_ids.c *
  7. * *
  8. * Copyright (C) 2002 Thomas Gleixner (tglx@linutronix.de) *
  9. * *
  10. * This program is free software; you can redistribute it and/or modify *
  11. * it under the terms of the GNU General Public License as published by *
  12. * the Free Software Foundation; either version 2 of the License, or *
  13. * (at your option) any later version. *
  14. * *
  15. * This program is distributed in the hope that it will be useful, *
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  18. * GNU General Public License for more details. *
  19. * *
  20. * You should have received a copy of the GNU General Public License *
  21. * along with this program; if not, write to the *
  22. * Free Software Foundation, Inc., *
  23. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
  24. ***************************************************************************/
  25. #ifdef HAVE_CONFIG_H
  26. #include "config.h"
  27. #endif
  28. #include "replacements.h"
  29. #include "log.h"
  30. #include <stdlib.h>
  31. #include <string.h>
  32. #include <inttypes.h>
  33. #include <errno.h>
  34. #include "nand.h"
  35. #include "flash.h"
  36. #include "time_support.h"
  37. #include "fileio.h"
  38. #include "image.h"
  39. int nand_register_commands(struct command_context_s *cmd_ctx);
  40. int handle_nand_list_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  41. int handle_nand_probe_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  42. int handle_nand_check_bad_blocks_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  43. int handle_nand_info_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  44. int handle_nand_copy_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  45. int handle_nand_write_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  46. int handle_nand_dump_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  47. int handle_nand_erase_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  48. int handle_nand_raw_access_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  49. int nand_read_page_raw(struct nand_device_s *device, u32 page, u8 *data, u32 data_size, u8 *oob, u32 oob_size);
  50. int nand_read_page(struct nand_device_s *device, u32 page, u8 *data, u32 data_size, u8 *oob, u32 oob_size);
  51. int nand_read_plain(struct nand_device_s *device, u32 address, u8 *data, u32 data_size);
  52. int nand_write_page_raw(struct nand_device_s *device, u32 page, u8 *data, u32 data_size, u8 *oob, u32 oob_size);
  53. int nand_write_page(struct nand_device_s *device, u32 page, u8 *data, u32 data_size, u8 *oob, u32 oob_size);
  54. /* NAND flash controller
  55. */
  56. extern nand_flash_controller_t lpc3180_nand_controller;
  57. extern nand_flash_controller_t s3c2410_nand_controller;
  58. extern nand_flash_controller_t s3c2412_nand_controller;
  59. extern nand_flash_controller_t s3c2440_nand_controller;
  60. extern nand_flash_controller_t s3c2443_nand_controller;
  61. /* extern nand_flash_controller_t boundary_scan_nand_controller; */
  62. nand_flash_controller_t *nand_flash_controllers[] =
  63. {
  64. &lpc3180_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. 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 (nand_flash_controllers[i]->register_commands(cmd_ctx) != ERROR_OK)
  179. {
  180. ERROR("couldn't register '%s' commands", args[0]);
  181. exit(-1);
  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. 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. ERROR("No valid NAND flash controller found (%s)", args[0]);
  218. ERROR("compiled-in NAND flash controllers:");
  219. for (i = 0; nand_flash_controllers[i]; i++)
  220. {
  221. 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> [options]");
  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;
  274. oob = malloc(6);
  275. if ((first < 0) || (first >= device->num_blocks))
  276. first = 0;
  277. if ((last >= device->num_blocks) || (last == -1))
  278. last = device->num_blocks - 1;
  279. for (i = first; i < last; i++)
  280. {
  281. nand_read_page(device, page, NULL, 0, oob, 6);
  282. if (((device->device->options & NAND_BUSWIDTH_16) && ((oob[0] & oob[1]) != 0xff))
  283. || (((device->page_size == 512) && (oob[5] != 0xff)) ||
  284. ((device->page_size == 2048) && (oob[0] != 0xff))))
  285. {
  286. WARNING("invalid block: %i", i);
  287. device->blocks[i].is_bad = 1;
  288. }
  289. else
  290. {
  291. device->blocks[i].is_bad = 0;
  292. }
  293. page += (device->erase_size / device->page_size);
  294. }
  295. return ERROR_OK;
  296. }
  297. int nand_read_status(struct nand_device_s *device, u8 *status)
  298. {
  299. if (!device->device)
  300. return ERROR_NAND_DEVICE_NOT_PROBED;
  301. /* Send read status command */
  302. device->controller->command(device, NAND_CMD_STATUS);
  303. usleep(1000);
  304. /* read status */
  305. if (device->device->options & NAND_BUSWIDTH_16)
  306. {
  307. u16 data;
  308. device->controller->read_data(device, &data);
  309. *status = data & 0xff;
  310. }
  311. else
  312. {
  313. device->controller->read_data(device, status);
  314. }
  315. return ERROR_OK;
  316. }
  317. int nand_probe(struct nand_device_s *device)
  318. {
  319. u8 manufacturer_id, device_id;
  320. int retval;
  321. int i;
  322. /* clear device data */
  323. device->device = NULL;
  324. device->manufacturer = NULL;
  325. /* clear device parameters */
  326. device->bus_width = 0;
  327. device->address_cycles = 0;
  328. device->page_size = 0;
  329. device->erase_size = 0;
  330. /* initialize controller (device parameters are zero, use controller default) */
  331. if ((retval = device->controller->init(device) != ERROR_OK))
  332. {
  333. switch (retval)
  334. {
  335. case ERROR_NAND_OPERATION_FAILED:
  336. DEBUG("controller initialization failed");
  337. return ERROR_NAND_OPERATION_FAILED;
  338. case ERROR_NAND_OPERATION_NOT_SUPPORTED:
  339. ERROR("BUG: controller reported that it doesn't support default parameters");
  340. return ERROR_NAND_OPERATION_FAILED;
  341. default:
  342. ERROR("BUG: unknown controller initialization failure");
  343. return ERROR_NAND_OPERATION_FAILED;
  344. }
  345. }
  346. device->controller->command(device, NAND_CMD_RESET);
  347. device->controller->reset(device);
  348. device->controller->command(device, NAND_CMD_READID);
  349. device->controller->address(device, 0x0);
  350. if (device->bus_width == 8)
  351. {
  352. device->controller->read_data(device, &manufacturer_id);
  353. device->controller->read_data(device, &device_id);
  354. }
  355. else
  356. {
  357. u16 data_buf;
  358. device->controller->read_data(device, &data_buf);
  359. manufacturer_id = data_buf & 0xff;
  360. device->controller->read_data(device, &data_buf);
  361. device_id = data_buf & 0xff;
  362. }
  363. for (i = 0; nand_flash_ids[i].name; i++)
  364. {
  365. if (nand_flash_ids[i].id == device_id)
  366. {
  367. device->device = &nand_flash_ids[i];
  368. break;
  369. }
  370. }
  371. for (i = 0; nand_manuf_ids[i].name; i++)
  372. {
  373. if (nand_manuf_ids[i].id == manufacturer_id)
  374. {
  375. device->manufacturer = &nand_manuf_ids[i];
  376. break;
  377. }
  378. }
  379. if (!device->manufacturer)
  380. {
  381. device->manufacturer = &nand_manuf_ids[0];
  382. device->manufacturer->id = manufacturer_id;
  383. }
  384. if (!device->device)
  385. {
  386. ERROR("unknown NAND flash device found, manufacturer id: 0x%2.2x device id: 0x%2.2x",
  387. manufacturer_id, device_id);
  388. return ERROR_NAND_OPERATION_FAILED;
  389. }
  390. DEBUG("found %s (%s)", device->device->name, device->manufacturer->name);
  391. /* initialize device parameters */
  392. /* bus width */
  393. if (device->device->options & NAND_BUSWIDTH_16)
  394. device->bus_width = 16;
  395. else
  396. device->bus_width = 8;
  397. /* page size */
  398. if (device->device->page_size == 0)
  399. {
  400. /* TODO: support reading extended chip id to determine page size */
  401. return ERROR_NAND_OPERATION_FAILED;
  402. }
  403. else if (device->device->page_size == 256)
  404. {
  405. ERROR("NAND flashes with 256 byte pagesize are not supported");
  406. return ERROR_NAND_OPERATION_FAILED;
  407. }
  408. else
  409. {
  410. device->page_size = device->device->page_size;
  411. }
  412. /* number of address cycles */
  413. if (device->page_size <= 512)
  414. {
  415. /* small page devices */
  416. if (device->device->chip_size <= 32)
  417. device->address_cycles = 3;
  418. else if (device->device->chip_size <= 8*1024)
  419. device->address_cycles = 4;
  420. else
  421. {
  422. ERROR("BUG: small page NAND device with more than 8 GiB encountered");
  423. device->address_cycles = 5;
  424. }
  425. }
  426. else
  427. {
  428. /* large page devices */
  429. if (device->device->chip_size <= 128)
  430. device->address_cycles = 4;
  431. else if (device->device->chip_size <= 32*1024)
  432. device->address_cycles = 5;
  433. else
  434. {
  435. ERROR("BUG: small page NAND device with more than 32 GiB encountered");
  436. device->address_cycles = 6;
  437. }
  438. }
  439. /* erase size */
  440. if (device->device->erase_size == 0)
  441. {
  442. /* TODO: support reading extended chip id to determine erase size */
  443. }
  444. else
  445. {
  446. device->erase_size = device->device->erase_size;
  447. }
  448. /* initialize controller, but leave parameters at the controllers default */
  449. if ((retval = device->controller->init(device) != ERROR_OK))
  450. {
  451. switch (retval)
  452. {
  453. case ERROR_NAND_OPERATION_FAILED:
  454. DEBUG("controller initialization failed");
  455. return ERROR_NAND_OPERATION_FAILED;
  456. case ERROR_NAND_OPERATION_NOT_SUPPORTED:
  457. ERROR("controller doesn't support requested parameters (buswidth: %i, address cycles: %i, page size: %i)",
  458. device->bus_width, device->address_cycles, device->page_size);
  459. return ERROR_NAND_OPERATION_FAILED;
  460. default:
  461. ERROR("BUG: unknown controller initialization failure");
  462. return ERROR_NAND_OPERATION_FAILED;
  463. }
  464. }
  465. device->num_blocks = (device->device->chip_size * 1024) / (device->erase_size / 1024);
  466. device->blocks = malloc(sizeof(nand_block_t) * device->num_blocks);
  467. for (i = 0; i < device->num_blocks; i++)
  468. {
  469. device->blocks[i].size = device->erase_size;
  470. device->blocks[i].offset = i * device->erase_size;
  471. device->blocks[i].is_erased = -1;
  472. device->blocks[i].is_bad = -1;
  473. }
  474. return ERROR_OK;
  475. }
  476. int nand_erase(struct nand_device_s *device, int first_block, int last_block)
  477. {
  478. int i;
  479. u32 page;
  480. u8 status;
  481. int retval;
  482. if (!device->device)
  483. return ERROR_NAND_DEVICE_NOT_PROBED;
  484. if ((first_block < 0) || (last_block > device->num_blocks))
  485. return ERROR_INVALID_ARGUMENTS;
  486. /* make sure we know if a block is bad before erasing it */
  487. for (i = first_block; i <= last_block; i++)
  488. {
  489. if (device->blocks[i].is_bad == -1)
  490. {
  491. nand_build_bbt(device, i, last_block);
  492. break;
  493. }
  494. }
  495. for (i = first_block; i <= last_block; i++)
  496. {
  497. /* Send erase setup command */
  498. device->controller->command(device, NAND_CMD_ERASE1);
  499. page = i * (device->erase_size / device->page_size);
  500. /* Send page address */
  501. if (device->page_size <= 512)
  502. {
  503. /* row */
  504. device->controller->address(device, page & 0xff);
  505. device->controller->address(device, (page >> 8) & 0xff);
  506. /* 3rd cycle only on devices with more than 32 MiB */
  507. if (device->address_cycles >= 4)
  508. device->controller->address(device, (page >> 16) & 0xff);
  509. /* 4th cycle only on devices with more than 8 GiB */
  510. if (device->address_cycles >= 5)
  511. device->controller->address(device, (page >> 24) & 0xff);
  512. }
  513. else
  514. {
  515. /* row */
  516. device->controller->address(device, page & 0xff);
  517. device->controller->address(device, (page >> 8) & 0xff);
  518. /* 3rd cycle only on devices with more than 128 MiB */
  519. if (device->address_cycles >= 5)
  520. device->controller->address(device, (page >> 16) & 0xff);
  521. }
  522. /* Send erase confirm command */
  523. device->controller->command(device, NAND_CMD_ERASE2);
  524. if (!device->controller->nand_ready(device, 1000))
  525. {
  526. ERROR("timeout waiting for NAND flash block erase to complete");
  527. return ERROR_NAND_OPERATION_TIMEOUT;
  528. }
  529. if ((retval = nand_read_status(device, &status)) != ERROR_OK)
  530. {
  531. ERROR("couldn't read status");
  532. return ERROR_NAND_OPERATION_FAILED;
  533. }
  534. if (status & 0x1)
  535. {
  536. ERROR("erase operation didn't pass, status: 0x%2.2x", status);
  537. return ERROR_NAND_OPERATION_FAILED;
  538. }
  539. }
  540. return ERROR_OK;
  541. }
  542. int nand_read_plain(struct nand_device_s *device, u32 address, u8 *data, u32 data_size)
  543. {
  544. u8 *page;
  545. if (!device->device)
  546. return ERROR_NAND_DEVICE_NOT_PROBED;
  547. if (address % device->page_size)
  548. {
  549. ERROR("reads need to be page aligned");
  550. return ERROR_NAND_OPERATION_FAILED;
  551. }
  552. page = malloc(device->page_size);
  553. while (data_size > 0 )
  554. {
  555. u32 thisrun_size = (data_size > device->page_size) ? device->page_size : data_size;
  556. u32 page_address;
  557. page_address = address / device->page_size;
  558. nand_read_page(device, page_address, page, device->page_size, NULL, 0);
  559. memcpy(data, page, thisrun_size);
  560. address += thisrun_size;
  561. data += thisrun_size;
  562. data_size -= thisrun_size;
  563. }
  564. free(page);
  565. return ERROR_OK;
  566. }
  567. int nand_write_plain(struct nand_device_s *device, u32 address, u8 *data, u32 data_size)
  568. {
  569. u8 *page;
  570. if (!device->device)
  571. return ERROR_NAND_DEVICE_NOT_PROBED;
  572. if (address % device->page_size)
  573. {
  574. ERROR("writes need to be page aligned");
  575. return ERROR_NAND_OPERATION_FAILED;
  576. }
  577. page = malloc(device->page_size);
  578. while (data_size > 0 )
  579. {
  580. u32 thisrun_size = (data_size > device->page_size) ? device->page_size : data_size;
  581. u32 page_address;
  582. memset(page, 0xff, device->page_size);
  583. memcpy(page, data, thisrun_size);
  584. page_address = address / device->page_size;
  585. nand_write_page(device, page_address, page, device->page_size, NULL, 0);
  586. address += thisrun_size;
  587. data += thisrun_size;
  588. data_size -= thisrun_size;
  589. }
  590. free(page);
  591. return ERROR_OK;
  592. }
  593. int nand_write_page(struct nand_device_s *device, u32 page, u8 *data, u32 data_size, u8 *oob, u32 oob_size)
  594. {
  595. if (!device->device)
  596. return ERROR_NAND_DEVICE_NOT_PROBED;
  597. if (device->use_raw || device->controller->write_page == NULL)
  598. return nand_write_page_raw(device, page, data, data_size, oob, oob_size);
  599. else
  600. return device->controller->write_page(device, page, data, data_size, oob, oob_size);
  601. }
  602. int nand_read_page(struct nand_device_s *device, u32 page, u8 *data, u32 data_size, u8 *oob, u32 oob_size)
  603. {
  604. if (!device->device)
  605. return ERROR_NAND_DEVICE_NOT_PROBED;
  606. if (device->use_raw || device->controller->read_page == NULL)
  607. return nand_read_page_raw(device, page, data, data_size, oob, oob_size);
  608. else
  609. return device->controller->read_page(device, page, data, data_size, oob, oob_size);
  610. }
  611. int nand_read_page_raw(struct nand_device_s *device, u32 page, u8 *data, u32 data_size, u8 *oob, u32 oob_size)
  612. {
  613. int i;
  614. if (!device->device)
  615. return ERROR_NAND_DEVICE_NOT_PROBED;
  616. if (device->page_size <= 512)
  617. {
  618. /* small page device */
  619. if (data)
  620. device->controller->command(device, NAND_CMD_READ0);
  621. else
  622. device->controller->command(device, NAND_CMD_READOOB);
  623. /* column (always 0, we start at the beginning of a page/OOB area) */
  624. device->controller->address(device, 0x0);
  625. /* row */
  626. device->controller->address(device, page & 0xff);
  627. device->controller->address(device, (page >> 8) & 0xff);
  628. /* 4th cycle only on devices with more than 32 MiB */
  629. if (device->address_cycles >= 4)
  630. device->controller->address(device, (page >> 16) & 0xff);
  631. /* 5th cycle only on devices with more than 8 GiB */
  632. if (device->address_cycles >= 5)
  633. device->controller->address(device, (page >> 24) & 0xff);
  634. }
  635. else
  636. {
  637. /* large page device */
  638. device->controller->command(device, NAND_CMD_READ0);
  639. /* column (0 when we start at the beginning of a page,
  640. * or 2048 for the beginning of OOB area)
  641. */
  642. device->controller->address(device, 0x0);
  643. device->controller->address(device, 0x8);
  644. /* row */
  645. device->controller->address(device, page & 0xff);
  646. device->controller->address(device, (page >> 8) & 0xff);
  647. /* 5th cycle only on devices with more than 128 MiB */
  648. if (device->address_cycles >= 5)
  649. device->controller->address(device, (page >> 16) & 0xff);
  650. /* large page devices need a start command */
  651. device->controller->command(device, NAND_CMD_READSTART);
  652. }
  653. if (!device->controller->nand_ready(device, 100))
  654. return ERROR_NAND_OPERATION_TIMEOUT;
  655. if (data)
  656. {
  657. if (device->controller->read_block_data != NULL)
  658. (device->controller->read_block_data)(device, data, data_size);
  659. else
  660. {
  661. for (i = 0; i < data_size;)
  662. {
  663. if (device->device->options & NAND_BUSWIDTH_16)
  664. {
  665. device->controller->read_data(device, data);
  666. data += 2;
  667. i += 2;
  668. }
  669. else
  670. {
  671. device->controller->read_data(device, data);
  672. data += 1;
  673. i += 1;
  674. }
  675. }
  676. }
  677. }
  678. if (oob)
  679. {
  680. if (device->controller->read_block_data != NULL)
  681. (device->controller->read_block_data)(device, oob, oob_size);
  682. else
  683. {
  684. for (i = 0; i < oob_size;)
  685. {
  686. if (device->device->options & NAND_BUSWIDTH_16)
  687. {
  688. device->controller->read_data(device, oob);
  689. oob += 2;
  690. i += 2;
  691. }
  692. else
  693. {
  694. device->controller->read_data(device, oob);
  695. oob += 1;
  696. i += 1;
  697. }
  698. }
  699. }
  700. }
  701. return ERROR_OK;
  702. }
  703. int nand_write_page_raw(struct nand_device_s *device, u32 page, u8 *data, u32 data_size, u8 *oob, u32 oob_size)
  704. {
  705. int i;
  706. int retval;
  707. u8 status;
  708. if (!device->device)
  709. return ERROR_NAND_DEVICE_NOT_PROBED;
  710. device->controller->command(device, NAND_CMD_SEQIN);
  711. if (device->page_size <= 512)
  712. {
  713. /* column (always 0, we start at the beginning of a page/OOB area) */
  714. device->controller->address(device, 0x0);
  715. /* row */
  716. device->controller->address(device, page & 0xff);
  717. device->controller->address(device, (page >> 8) & 0xff);
  718. /* 4th cycle only on devices with more than 32 MiB */
  719. if (device->address_cycles >= 4)
  720. device->controller->address(device, (page >> 16) & 0xff);
  721. /* 5th cycle only on devices with more than 8 GiB */
  722. if (device->address_cycles >= 5)
  723. device->controller->address(device, (page >> 24) & 0xff);
  724. }
  725. else
  726. {
  727. /* column (0 when we start at the beginning of a page,
  728. * or 2048 for the beginning of OOB area)
  729. */
  730. device->controller->address(device, 0x0);
  731. device->controller->address(device, 0x8);
  732. /* row */
  733. device->controller->address(device, page & 0xff);
  734. device->controller->address(device, (page >> 8) & 0xff);
  735. /* 5th cycle only on devices with more than 128 MiB */
  736. if (device->address_cycles >= 5)
  737. device->controller->address(device, (page >> 16) & 0xff);
  738. }
  739. if (data)
  740. {
  741. if (device->controller->write_block_data != NULL)
  742. (device->controller->write_block_data)(device, data, data_size);
  743. else
  744. {
  745. for (i = 0; i < data_size;)
  746. {
  747. if (device->device->options & NAND_BUSWIDTH_16)
  748. {
  749. u16 data_buf = le_to_h_u16(data);
  750. device->controller->write_data(device, data_buf);
  751. data += 2;
  752. i += 2;
  753. }
  754. else
  755. {
  756. device->controller->write_data(device, *data);
  757. data += 1;
  758. i += 1;
  759. }
  760. }
  761. }
  762. }
  763. if (oob)
  764. {
  765. if (device->controller->write_block_data != NULL)
  766. (device->controller->write_block_data)(device, oob, oob_size);
  767. else
  768. {
  769. for (i = 0; i < oob_size;)
  770. {
  771. if (device->device->options & NAND_BUSWIDTH_16)
  772. {
  773. u16 oob_buf = le_to_h_u16(data);
  774. device->controller->write_data(device, oob_buf);
  775. oob += 2;
  776. i += 2;
  777. }
  778. else
  779. {
  780. device->controller->write_data(device, *oob);
  781. oob += 1;
  782. i += 1;
  783. }
  784. }
  785. }
  786. }
  787. device->controller->command(device, NAND_CMD_PAGEPROG);
  788. if (!device->controller->nand_ready(device, 100))
  789. return ERROR_NAND_OPERATION_TIMEOUT;
  790. if ((retval = nand_read_status(device, &status)) != ERROR_OK)
  791. {
  792. ERROR("couldn't read status");
  793. return ERROR_NAND_OPERATION_FAILED;
  794. }
  795. if (status & NAND_STATUS_FAIL)
  796. {
  797. ERROR("write operation didn't pass, status: 0x%2.2x", status);
  798. return ERROR_NAND_OPERATION_FAILED;
  799. }
  800. return ERROR_OK;
  801. }
  802. int handle_nand_list_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  803. {
  804. nand_device_t *p;
  805. int i = 0;
  806. if (!nand_devices)
  807. {
  808. command_print(cmd_ctx, "no NAND flash devices configured");
  809. return ERROR_OK;
  810. }
  811. for (p = nand_devices; p; p = p->next)
  812. {
  813. if (p->device)
  814. command_print(cmd_ctx, "#%i: %s (%s) pagesize: %i, buswidth: %i, erasesize: %i",
  815. i++, p->device->name, p->manufacturer->name, p->page_size, p->bus_width, p->erase_size);
  816. else
  817. command_print(cmd_ctx, "#%i: not probed");
  818. }
  819. return ERROR_OK;
  820. }
  821. int handle_nand_info_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  822. {
  823. nand_device_t *p;
  824. int i = 0;
  825. int j = 0;
  826. int first = -1;
  827. int last = -1;
  828. if ((argc < 1) || (argc > 3))
  829. {
  830. command_print(cmd_ctx, "usage: nand info <num> [<first> <last>]");
  831. return ERROR_OK;
  832. }
  833. if (argc == 2)
  834. {
  835. first = last = strtoul(args[1], NULL, 0);
  836. }
  837. else if (argc == 3)
  838. {
  839. first = strtoul(args[1], NULL, 0);
  840. last = strtoul(args[2], NULL, 0);
  841. }
  842. p = get_nand_device_by_num(strtoul(args[0], NULL, 0));
  843. if (p)
  844. {
  845. if (p->device)
  846. {
  847. if (first >= p->num_blocks)
  848. first = p->num_blocks - 1;
  849. if (last >= p->num_blocks)
  850. last = p->num_blocks - 1;
  851. command_print(cmd_ctx, "#%i: %s (%s) pagesize: %i, buswidth: %i, erasesize: %i",
  852. i++, p->device->name, p->manufacturer->name, p->page_size, p->bus_width, p->erase_size);
  853. for (j = first; j <= last; j++)
  854. {
  855. char *erase_state, *bad_state;
  856. if (p->blocks[j].is_erased == 0)
  857. erase_state = "not erased";
  858. else if (p->blocks[j].is_erased == 1)
  859. erase_state = "erased";
  860. else
  861. erase_state = "erase state unknown";
  862. if (p->blocks[j].is_bad == 0)
  863. bad_state = "";
  864. else if (p->blocks[j].is_bad == 1)
  865. bad_state = " (marked bad)";
  866. else
  867. bad_state = " (block condition unknown)";
  868. command_print(cmd_ctx, "\t#%i: 0x%8.8x (0x%xkB) %s%s",
  869. j, p->blocks[j].offset, p->blocks[j].size / 1024,
  870. erase_state, bad_state);
  871. }
  872. }
  873. else
  874. {
  875. command_print(cmd_ctx, "#%i: not probed");
  876. }
  877. }
  878. return ERROR_OK;
  879. }
  880. int handle_nand_probe_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  881. {
  882. nand_device_t *p;
  883. int retval;
  884. if (argc != 1)
  885. {
  886. command_print(cmd_ctx, "usage: nand probe <num>");
  887. return ERROR_OK;
  888. }
  889. p = get_nand_device_by_num(strtoul(args[0], NULL, 0));
  890. if (p)
  891. {
  892. if ((retval = nand_probe(p)) == ERROR_OK)
  893. {
  894. command_print(cmd_ctx, "NAND flash device '%s' found", p->device->name);
  895. }
  896. else if (retval == ERROR_NAND_OPERATION_FAILED)
  897. {
  898. command_print(cmd_ctx, "probing failed for NAND flash device");
  899. }
  900. else
  901. {
  902. command_print(cmd_ctx, "unknown error when probing NAND flash device");
  903. }
  904. }
  905. else
  906. {
  907. command_print(cmd_ctx, "NAND flash device '#%s' is out of bounds", args[0]);
  908. }
  909. return ERROR_OK;
  910. }
  911. int handle_nand_erase_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  912. {
  913. nand_device_t *p;
  914. int retval;
  915. if (argc != 3)
  916. {
  917. command_print(cmd_ctx, "usage: nand erase <num> <first> <last>");
  918. return ERROR_OK;
  919. }
  920. p = get_nand_device_by_num(strtoul(args[0], NULL, 0));
  921. if (p)
  922. {
  923. int first = strtoul(args[1], NULL, 0);
  924. int last = strtoul(args[2], NULL, 0);
  925. if ((retval = nand_erase(p, first, last)) == ERROR_OK)
  926. {
  927. command_print(cmd_ctx, "successfully erased blocks %i to %i on NAND flash device '%s'", first, last, p->device->name);
  928. }
  929. else if (retval == ERROR_NAND_OPERATION_FAILED)
  930. {
  931. command_print(cmd_ctx, "erase failed");
  932. }
  933. else
  934. {
  935. command_print(cmd_ctx, "unknown error when erasing NAND flash device");
  936. }
  937. }
  938. else
  939. {
  940. command_print(cmd_ctx, "NAND flash device '#%s' is out of bounds", args[0]);
  941. }
  942. return ERROR_OK;
  943. }
  944. int handle_nand_check_bad_blocks_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  945. {
  946. nand_device_t *p;
  947. int retval;
  948. int first = -1;
  949. int last = -1;
  950. if ((argc < 1) || (argc > 3) || (argc == 2))
  951. {
  952. command_print(cmd_ctx, "usage: nand check_bad_blocks <num> [<first> <last>]");
  953. return ERROR_OK;
  954. }
  955. if (argc == 3)
  956. {
  957. first = strtoul(args[1], NULL, 0);
  958. last = strtoul(args[2], NULL, 0);
  959. }
  960. p = get_nand_device_by_num(strtoul(args[0], NULL, 0));
  961. if (p)
  962. {
  963. if ((retval = nand_build_bbt(p, first, last)) == ERROR_OK)
  964. {
  965. command_print(cmd_ctx, "checked NAND flash device for bad blocks, use \"nand info\" command to list blocks", p->device->name);
  966. }
  967. else if (retval == ERROR_NAND_OPERATION_FAILED)
  968. {
  969. command_print(cmd_ctx, "error when checking for bad blocks on NAND flash device");
  970. }
  971. else
  972. {
  973. command_print(cmd_ctx, "unknown error when checking for bad blocks on NAND flash device");
  974. }
  975. }
  976. else
  977. {
  978. command_print(cmd_ctx, "NAND flash device '#%s' is out of bounds", args[0]);
  979. }
  980. return ERROR_OK;
  981. }
  982. int handle_nand_copy_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  983. {
  984. nand_device_t *p;
  985. if (argc != 4)
  986. {
  987. command_print(cmd_ctx, "usage: nand copy <num> <offset> <length> <ram-address>");
  988. return ERROR_OK;
  989. }
  990. p = get_nand_device_by_num(strtoul(args[0], NULL, 0));
  991. if (p)
  992. {
  993. }
  994. else
  995. {
  996. command_print(cmd_ctx, "NAND flash device '#%s' is out of bounds", args[0]);
  997. }
  998. return ERROR_OK;
  999. }
  1000. int handle_nand_write_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  1001. {
  1002. u32 offset;
  1003. u32 binary_size;
  1004. u32 buf_cnt;
  1005. enum oob_formats oob_format = NAND_OOB_NONE;
  1006. fileio_t fileio;
  1007. duration_t duration;
  1008. char *duration_text;
  1009. nand_device_t *p;
  1010. if (argc < 3)
  1011. {
  1012. command_print(cmd_ctx, "usage: nand write <num> <file> <offset> [options]");
  1013. return ERROR_OK;
  1014. }
  1015. p = get_nand_device_by_num(strtoul(args[0], NULL, 0));
  1016. if (p)
  1017. {
  1018. u8 *page = NULL;
  1019. u32 page_size = 0;
  1020. u8 *oob = NULL;
  1021. u32 oob_size = 0;
  1022. duration_start_measure(&duration);
  1023. offset = strtoul(args[2], NULL, 0);
  1024. if (argc > 3)
  1025. {
  1026. int i;
  1027. for (i = 3; i < argc; i++)
  1028. {
  1029. if (!strcmp(args[i], "oob_raw"))
  1030. oob_format |= NAND_OOB_RAW;
  1031. else if (!strcmp(args[i], "oob_only"))
  1032. oob_format |= NAND_OOB_RAW | NAND_OOB_ONLY;
  1033. else
  1034. {
  1035. command_print(cmd_ctx, "unknown option: %s", args[i]);
  1036. }
  1037. }
  1038. }
  1039. if (fileio_open(&fileio, args[1], FILEIO_READ, FILEIO_BINARY) != ERROR_OK)
  1040. {
  1041. command_print(cmd_ctx, "file open error: %s", fileio.error_str);
  1042. return ERROR_OK;
  1043. }
  1044. buf_cnt = binary_size = fileio.size;
  1045. if (!(oob_format & NAND_OOB_ONLY))
  1046. {
  1047. page_size = p->page_size;
  1048. page = malloc(p->page_size);
  1049. }
  1050. if (oob_format & NAND_OOB_RAW)
  1051. {
  1052. if (p->page_size == 512)
  1053. oob_size = 16;
  1054. else if (p->page_size == 2048)
  1055. oob_size = 64;
  1056. oob = malloc(oob_size);
  1057. }
  1058. if (offset % p->page_size)
  1059. {
  1060. command_print(cmd_ctx, "only page size aligned offsets and sizes are supported");
  1061. return ERROR_OK;
  1062. }
  1063. while (buf_cnt > 0)
  1064. {
  1065. u32 size_read;
  1066. if (page)
  1067. {
  1068. fileio_read(&fileio, page_size, page, &size_read);
  1069. buf_cnt -= size_read;
  1070. if (size_read < page_size)
  1071. {
  1072. memset(page + size_read, 0xff, page_size - size_read);
  1073. }
  1074. }
  1075. if (oob)
  1076. {
  1077. fileio_read(&fileio, oob_size, oob, &size_read);
  1078. buf_cnt -= size_read;
  1079. if (size_read < oob_size)
  1080. {
  1081. memset(oob + size_read, 0xff, oob_size - size_read);
  1082. }
  1083. }
  1084. if (nand_write_page(p, offset / p->page_size, page, page_size, oob, oob_size) != ERROR_OK)
  1085. {
  1086. command_print(cmd_ctx, "failed writing file %s to NAND flash %s at offset 0x%8.8x",
  1087. args[1], args[0], offset);
  1088. return ERROR_OK;
  1089. }
  1090. offset += page_size;
  1091. }
  1092. fileio_close(&fileio);
  1093. duration_stop_measure(&duration, &duration_text);
  1094. command_print(cmd_ctx, "wrote file %s to NAND flash %s at offset 0x%8.8x in %s",
  1095. args[1], args[0], offset, duration_text);
  1096. free(duration_text);
  1097. }
  1098. else
  1099. {
  1100. command_print(cmd_ctx, "NAND flash device '#%s' is out of bounds", args[0]);
  1101. }
  1102. return ERROR_OK;
  1103. }
  1104. int handle_nand_dump_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  1105. {
  1106. nand_device_t *p;
  1107. if (argc < 4)
  1108. {
  1109. command_print(cmd_ctx, "usage: nand dump <num> <filename> <address> <size> [options]");
  1110. return ERROR_OK;
  1111. }
  1112. p = get_nand_device_by_num(strtoul(args[0], NULL, 0));
  1113. if (p)
  1114. {
  1115. if (p->device)
  1116. {
  1117. fileio_t fileio;
  1118. duration_t duration;
  1119. char *duration_text;
  1120. int retval;
  1121. u8 *page = NULL;
  1122. u32 page_size = 0;
  1123. u8 *oob = NULL;
  1124. u32 oob_size = 0;
  1125. u32 address = strtoul(args[2], NULL, 0);
  1126. u32 size = strtoul(args[3], NULL, 0);
  1127. u32 bytes_done = 0;
  1128. enum oob_formats oob_format = NAND_OOB_NONE;
  1129. if (argc > 4)
  1130. {
  1131. int i;
  1132. for (i = 4; i < argc; i++)
  1133. {
  1134. if (!strcmp(args[i], "oob_raw"))
  1135. oob_format |= NAND_OOB_RAW;
  1136. else if (!strcmp(args[i], "oob_only"))
  1137. oob_format |= NAND_OOB_RAW | NAND_OOB_ONLY;
  1138. else
  1139. command_print(cmd_ctx, "unknown option: '%s'", args[i]);
  1140. }
  1141. }
  1142. if ((address % p->page_size) || (size % p->page_size))
  1143. {
  1144. command_print(cmd_ctx, "only page size aligned addresses and sizes are supported");
  1145. return ERROR_OK;
  1146. }
  1147. if (!(oob_format & NAND_OOB_ONLY))
  1148. {
  1149. page_size = p->page_size;
  1150. page = malloc(p->page_size);
  1151. }
  1152. if (oob_format & NAND_OOB_RAW)
  1153. {
  1154. if (p->page_size == 512)
  1155. oob_size = 16;
  1156. else if (p->page_size == 2048)
  1157. oob_size = 64;
  1158. oob = malloc(oob_size);
  1159. }
  1160. if (fileio_open(&fileio, args[1], FILEIO_WRITE, FILEIO_BINARY) != ERROR_OK)
  1161. {
  1162. command_print(cmd_ctx, "dump_image error: %s", fileio.error_str);
  1163. return ERROR_OK;
  1164. }
  1165. duration_start_measure(&duration);
  1166. while (size > 0)
  1167. {
  1168. u32 size_written;
  1169. if ((retval = nand_read_page(p, address / p->page_size, page, page_size, oob, oob_size)) != ERROR_OK)
  1170. {
  1171. command_print(cmd_ctx, "reading NAND flash page failed");
  1172. return ERROR_OK;
  1173. }
  1174. if (page)
  1175. {
  1176. fileio_write(&fileio, page_size, page, &size_written);
  1177. bytes_done += page_size;
  1178. }
  1179. if (oob)
  1180. {
  1181. fileio_write(&fileio, oob_size, oob, &size_written);
  1182. bytes_done += oob_size;
  1183. }
  1184. size -= p->page_size;
  1185. address += p->page_size;
  1186. }
  1187. if (page)
  1188. free(page);
  1189. if (oob)
  1190. free(oob);
  1191. fileio_close(&fileio);
  1192. duration_stop_measure(&duration, &duration_text);
  1193. command_print(cmd_ctx, "dumped %"PRIi64" byte in %s", fileio.size, duration_text);
  1194. free(duration_text);
  1195. }
  1196. else
  1197. {
  1198. command_print(cmd_ctx, "#%i: not probed");
  1199. }
  1200. }
  1201. else
  1202. {
  1203. command_print(cmd_ctx, "NAND flash device '#%s' is out of bounds", args[0]);
  1204. }
  1205. return ERROR_OK;
  1206. }
  1207. int handle_nand_raw_access_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  1208. {
  1209. nand_device_t *p;
  1210. if ((argc < 1) || (argc > 2))
  1211. {
  1212. command_print(cmd_ctx, "usage: nand raw_access <num> ['enable'|'disable']");
  1213. return ERROR_OK;
  1214. }
  1215. p = get_nand_device_by_num(strtoul(args[0], NULL, 0));
  1216. if (p)
  1217. {
  1218. if (p->device)
  1219. {
  1220. if (argc == 2)
  1221. {
  1222. if (strcmp("enable", args[1]) == 0)
  1223. {
  1224. p->use_raw = 1;
  1225. }
  1226. else if (strcmp("disable", args[1]) == 0)
  1227. {
  1228. p->use_raw = 0;
  1229. }
  1230. else
  1231. {
  1232. command_print(cmd_ctx, "usage: nand raw_access ['enable'|disable']");
  1233. }
  1234. }
  1235. command_print(cmd_ctx, "raw access is %s", (p->use_raw) ? "enabled" : "disabled");
  1236. }
  1237. else
  1238. {
  1239. command_print(cmd_ctx, "#%i: not probed");
  1240. }
  1241. }
  1242. else
  1243. {
  1244. command_print(cmd_ctx, "NAND flash device '#%s' is out of bounds", args[0]);
  1245. }
  1246. return ERROR_OK;
  1247. }