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.
 
 
 
 
 
 

894 lines
26 KiB

  1. /***************************************************************************
  2. * Copyright (C) 2007 by Dominic Rath <Dominic.Rath@gmx.de> *
  3. * Copyright (C) 2002 Thomas Gleixner <tglx@linutronix.de> *
  4. * Copyright (C) 2009 Zachary T Welch <zw@superlucidity.net> *
  5. * *
  6. * Partially based on drivers/mtd/nand_ids.c from Linux. *
  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, see <http://www.gnu.org/licenses/>. *
  20. ***************************************************************************/
  21. #ifdef HAVE_CONFIG_H
  22. #include "config.h"
  23. #endif
  24. #include "imp.h"
  25. /* configured NAND devices and NAND Flash command handler */
  26. struct nand_device *nand_devices;
  27. void nand_device_add(struct nand_device *c)
  28. {
  29. if (nand_devices) {
  30. struct nand_device *p = nand_devices;
  31. while (p && p->next)
  32. p = p->next;
  33. p->next = c;
  34. } else
  35. nand_devices = c;
  36. }
  37. /* Chip ID list
  38. *
  39. * Manufacturer, ID code, pagesize, chipsize in MegaByte, eraseblock size,
  40. * options, name
  41. *
  42. * Pagesize; 0, 256, 512
  43. * 0 get this information from the extended chip ID
  44. * 256 256 Byte page size
  45. * 512 512 Byte page size
  46. */
  47. static struct nand_info nand_flash_ids[] = {
  48. /* Vendor Specific Entries */
  49. { NAND_MFR_SAMSUNG, 0xD5, 8192, 2048, 0x100000, LP_OPTIONS,
  50. "K9GAG08 2GB NAND 3.3V x8 MLC 2b/cell"},
  51. { NAND_MFR_SAMSUNG, 0xD7, 8192, 4096, 0x100000, LP_OPTIONS,
  52. "K9LBG08 4GB NAND 3.3V x8 MLC 2b/cell"},
  53. /* start "museum" IDs */
  54. { 0x0, 0x6e, 256, 1, 0x1000, 0, "NAND 1MiB 5V 8-bit"},
  55. { 0x0, 0x64, 256, 2, 0x1000, 0, "NAND 2MiB 5V 8-bit"},
  56. { 0x0, 0x6b, 512, 4, 0x2000, 0, "NAND 4MiB 5V 8-bit"},
  57. { 0x0, 0xe8, 256, 1, 0x1000, 0, "NAND 1MiB 3.3V 8-bit"},
  58. { 0x0, 0xec, 256, 1, 0x1000, 0, "NAND 1MiB 3.3V 8-bit"},
  59. { 0x0, 0xea, 256, 2, 0x1000, 0, "NAND 2MiB 3.3V 8-bit"},
  60. { 0x0, 0xd5, 512, 4, 0x2000, 0, "NAND 4MiB 3.3V 8-bit"},
  61. { 0x0, 0xe3, 512, 4, 0x2000, 0, "NAND 4MiB 3.3V 8-bit"},
  62. { 0x0, 0xe5, 512, 4, 0x2000, 0, "NAND 4MiB 3.3V 8-bit"},
  63. { 0x0, 0xd6, 512, 8, 0x2000, 0, "NAND 8MiB 3.3V 8-bit"},
  64. { 0x0, 0x39, 512, 8, 0x2000, 0, "NAND 8MiB 1.8V 8-bit"},
  65. { 0x0, 0xe6, 512, 8, 0x2000, 0, "NAND 8MiB 3.3V 8-bit"},
  66. { 0x0, 0x49, 512, 8, 0x2000, NAND_BUSWIDTH_16, "NAND 8MiB 1.8V 16-bit"},
  67. { 0x0, 0x59, 512, 8, 0x2000, NAND_BUSWIDTH_16, "NAND 8MiB 3.3V 16-bit"},
  68. /* end "museum" IDs */
  69. { 0x0, 0x33, 512, 16, 0x4000, 0, "NAND 16MiB 1.8V 8-bit"},
  70. { 0x0, 0x73, 512, 16, 0x4000, 0, "NAND 16MiB 3.3V 8-bit"},
  71. { 0x0, 0x43, 512, 16, 0x4000, NAND_BUSWIDTH_16, "NAND 16MiB 1.8V 16-bit"},
  72. { 0x0, 0x53, 512, 16, 0x4000, NAND_BUSWIDTH_16, "NAND 16MiB 3.3V 16-bit"},
  73. { 0x0, 0x35, 512, 32, 0x4000, 0, "NAND 32MiB 1.8V 8-bit"},
  74. { 0x0, 0x75, 512, 32, 0x4000, 0, "NAND 32MiB 3.3V 8-bit"},
  75. { 0x0, 0x45, 512, 32, 0x4000, NAND_BUSWIDTH_16, "NAND 32MiB 1.8V 16-bit"},
  76. { 0x0, 0x55, 512, 32, 0x4000, NAND_BUSWIDTH_16, "NAND 32MiB 3.3V 16-bit"},
  77. { 0x0, 0x36, 512, 64, 0x4000, 0, "NAND 64MiB 1.8V 8-bit"},
  78. { 0x0, 0x76, 512, 64, 0x4000, 0, "NAND 64MiB 3.3V 8-bit"},
  79. { 0x0, 0x46, 512, 64, 0x4000, NAND_BUSWIDTH_16, "NAND 64MiB 1.8V 16-bit"},
  80. { 0x0, 0x56, 512, 64, 0x4000, NAND_BUSWIDTH_16, "NAND 64MiB 3.3V 16-bit"},
  81. { 0x0, 0x78, 512, 128, 0x4000, 0, "NAND 128MiB 1.8V 8-bit"},
  82. { 0x0, 0x39, 512, 128, 0x4000, 0, "NAND 128MiB 1.8V 8-bit"},
  83. { 0x0, 0x79, 512, 128, 0x4000, 0, "NAND 128MiB 3.3V 8-bit"},
  84. { 0x0, 0x72, 512, 128, 0x4000, NAND_BUSWIDTH_16, "NAND 128MiB 1.8V 16-bit"},
  85. { 0x0, 0x49, 512, 128, 0x4000, NAND_BUSWIDTH_16, "NAND 128MiB 1.8V 16-bit"},
  86. { 0x0, 0x74, 512, 128, 0x4000, NAND_BUSWIDTH_16, "NAND 128MiB 3.3V 16-bit"},
  87. { 0x0, 0x59, 512, 128, 0x4000, NAND_BUSWIDTH_16, "NAND 128MiB 3.3V 16-bit"},
  88. { 0x0, 0x71, 512, 256, 0x4000, 0, "NAND 256MiB 3.3V 8-bit"},
  89. { 0x0, 0xA2, 0, 64, 0, LP_OPTIONS, "NAND 64MiB 1.8V 8-bit"},
  90. { 0x0, 0xF2, 0, 64, 0, LP_OPTIONS, "NAND 64MiB 3.3V 8-bit"},
  91. { 0x0, 0xB2, 0, 64, 0, LP_OPTIONS16, "NAND 64MiB 1.8V 16-bit"},
  92. { 0x0, 0xC2, 0, 64, 0, LP_OPTIONS16, "NAND 64MiB 3.3V 16-bit"},
  93. { 0x0, 0xA1, 0, 128, 0, LP_OPTIONS, "NAND 128MiB 1.8V 8-bit"},
  94. { 0x0, 0xF1, 0, 128, 0, LP_OPTIONS, "NAND 128MiB 3.3V 8-bit"},
  95. { 0x0, 0xB1, 0, 128, 0, LP_OPTIONS16, "NAND 128MiB 1.8V 16-bit"},
  96. { 0x0, 0xC1, 0, 128, 0, LP_OPTIONS16, "NAND 128MiB 3.3V 16-bit"},
  97. { 0x0, 0xAA, 0, 256, 0, LP_OPTIONS, "NAND 256MiB 1.8V 8-bit"},
  98. { 0x0, 0xDA, 0, 256, 0, LP_OPTIONS, "NAND 256MiB 3.3V 8-bit"},
  99. { 0x0, 0xBA, 0, 256, 0, LP_OPTIONS16, "NAND 256MiB 1.8V 16-bit"},
  100. { 0x0, 0xCA, 0, 256, 0, LP_OPTIONS16, "NAND 256MiB 3.3V 16-bit"},
  101. { 0x0, 0xAC, 0, 512, 0, LP_OPTIONS, "NAND 512MiB 1.8V 8-bit"},
  102. { 0x0, 0xDC, 0, 512, 0, LP_OPTIONS, "NAND 512MiB 3.3V 8-bit"},
  103. { 0x0, 0xBC, 0, 512, 0, LP_OPTIONS16, "NAND 512MiB 1.8V 16-bit"},
  104. { 0x0, 0xCC, 0, 512, 0, LP_OPTIONS16, "NAND 512MiB 3.3V 16-bit"},
  105. { 0x0, 0xA3, 0, 1024, 0, LP_OPTIONS, "NAND 1GiB 1.8V 8-bit"},
  106. { 0x0, 0xD3, 0, 1024, 0, LP_OPTIONS, "NAND 1GiB 3.3V 8-bit"},
  107. { 0x0, 0xB3, 0, 1024, 0, LP_OPTIONS16, "NAND 1GiB 1.8V 16-bit"},
  108. { 0x0, 0xC3, 0, 1024, 0, LP_OPTIONS16, "NAND 1GiB 3.3V 16-bit"},
  109. { 0x0, 0xA5, 0, 2048, 0, LP_OPTIONS, "NAND 2GiB 1.8V 8-bit"},
  110. { 0x0, 0xD5, 0, 8192, 0, LP_OPTIONS, "NAND 2GiB 3.3V 8-bit"},
  111. { 0x0, 0xB5, 0, 2048, 0, LP_OPTIONS16, "NAND 2GiB 1.8V 16-bit"},
  112. { 0x0, 0xC5, 0, 2048, 0, LP_OPTIONS16, "NAND 2GiB 3.3V 16-bit"},
  113. { 0x0, 0x48, 0, 2048, 0, LP_OPTIONS, "NAND 2GiB 3.3V 8-bit"},
  114. {0, 0, 0, 0, 0, 0, NULL}
  115. };
  116. /* Manufacturer ID list
  117. */
  118. static struct nand_manufacturer nand_manuf_ids[] = {
  119. {0x0, "unknown"},
  120. {NAND_MFR_TOSHIBA, "Toshiba"},
  121. {NAND_MFR_SAMSUNG, "Samsung"},
  122. {NAND_MFR_FUJITSU, "Fujitsu"},
  123. {NAND_MFR_NATIONAL, "National"},
  124. {NAND_MFR_RENESAS, "Renesas"},
  125. {NAND_MFR_STMICRO, "ST Micro"},
  126. {NAND_MFR_HYNIX, "Hynix"},
  127. {NAND_MFR_MICRON, "Micron"},
  128. {0x0, NULL},
  129. };
  130. /*
  131. * Define default oob placement schemes for large and small page devices
  132. */
  133. #if 0
  134. static struct nand_ecclayout nand_oob_8 = {
  135. .eccbytes = 3,
  136. .eccpos = {0, 1, 2},
  137. .oobfree = {
  138. {.offset = 3,
  139. .length = 2},
  140. {.offset = 6,
  141. .length = 2}
  142. }
  143. };
  144. #endif
  145. /**
  146. * Returns the flash bank specified by @a name, which matches the
  147. * driver name and a suffix (option) specify the driver-specific
  148. * bank number. The suffix consists of the '.' and the driver-specific
  149. * bank number: when two davinci banks are defined, then 'davinci.1' refers
  150. * to the second (e.g. DM355EVM).
  151. */
  152. static struct nand_device *get_nand_device_by_name(const char *name)
  153. {
  154. unsigned requested = get_flash_name_index(name);
  155. unsigned found = 0;
  156. struct nand_device *nand;
  157. for (nand = nand_devices; NULL != nand; nand = nand->next) {
  158. if (strcmp(nand->name, name) == 0)
  159. return nand;
  160. if (!flash_driver_name_matches(nand->controller->name, name))
  161. continue;
  162. if (++found < requested)
  163. continue;
  164. return nand;
  165. }
  166. return NULL;
  167. }
  168. struct nand_device *get_nand_device_by_num(int num)
  169. {
  170. struct nand_device *p;
  171. int i = 0;
  172. for (p = nand_devices; p; p = p->next) {
  173. if (i++ == num)
  174. return p;
  175. }
  176. return NULL;
  177. }
  178. COMMAND_HELPER(nand_command_get_device, unsigned name_index,
  179. struct nand_device **nand)
  180. {
  181. const char *str = CMD_ARGV[name_index];
  182. *nand = get_nand_device_by_name(str);
  183. if (*nand)
  184. return ERROR_OK;
  185. unsigned num;
  186. COMMAND_PARSE_NUMBER(uint, str, num);
  187. *nand = get_nand_device_by_num(num);
  188. if (!*nand) {
  189. command_print(CMD, "NAND flash device '%s' not found", str);
  190. return ERROR_COMMAND_SYNTAX_ERROR;
  191. }
  192. return ERROR_OK;
  193. }
  194. int nand_build_bbt(struct nand_device *nand, int first, int last)
  195. {
  196. uint32_t page;
  197. int i;
  198. int pages_per_block = (nand->erase_size / nand->page_size);
  199. uint8_t oob[6];
  200. int ret;
  201. if ((first < 0) || (first >= nand->num_blocks))
  202. first = 0;
  203. if ((last >= nand->num_blocks) || (last == -1))
  204. last = nand->num_blocks - 1;
  205. page = first * pages_per_block;
  206. for (i = first; i <= last; i++) {
  207. ret = nand_read_page(nand, page, NULL, 0, oob, 6);
  208. if (ret != ERROR_OK)
  209. return ret;
  210. if (((nand->device->options & NAND_BUSWIDTH_16) && ((oob[0] & oob[1]) != 0xff))
  211. || (((nand->page_size == 512) && (oob[5] != 0xff)) ||
  212. ((nand->page_size == 2048) && (oob[0] != 0xff)))) {
  213. LOG_WARNING("bad block: %i", i);
  214. nand->blocks[i].is_bad = 1;
  215. } else
  216. nand->blocks[i].is_bad = 0;
  217. page += pages_per_block;
  218. }
  219. return ERROR_OK;
  220. }
  221. int nand_read_status(struct nand_device *nand, uint8_t *status)
  222. {
  223. if (!nand->device)
  224. return ERROR_NAND_DEVICE_NOT_PROBED;
  225. /* Send read status command */
  226. /* FIXME: errors returned from nand->controller are mostly ignored! */
  227. nand->controller->command(nand, NAND_CMD_STATUS);
  228. alive_sleep(1);
  229. /* read status */
  230. if (nand->device->options & NAND_BUSWIDTH_16) {
  231. uint16_t data;
  232. nand->controller->read_data(nand, &data);
  233. *status = data & 0xff;
  234. } else
  235. nand->controller->read_data(nand, status);
  236. return ERROR_OK;
  237. }
  238. static int nand_poll_ready(struct nand_device *nand, int timeout)
  239. {
  240. uint8_t status;
  241. nand->controller->command(nand, NAND_CMD_STATUS);
  242. do {
  243. if (nand->device->options & NAND_BUSWIDTH_16) {
  244. uint16_t data;
  245. nand->controller->read_data(nand, &data);
  246. status = data & 0xff;
  247. } else
  248. nand->controller->read_data(nand, &status);
  249. if (status & NAND_STATUS_READY)
  250. break;
  251. alive_sleep(1);
  252. } while (timeout--);
  253. return (status & NAND_STATUS_READY) != 0;
  254. }
  255. int nand_probe(struct nand_device *nand)
  256. {
  257. uint8_t manufacturer_id, device_id;
  258. uint8_t id_buff[6] = { 0 }; /* zero buff to silence false warning
  259. * from clang static analyzer */
  260. int retval;
  261. int i;
  262. /* clear device data */
  263. nand->device = NULL;
  264. nand->manufacturer = NULL;
  265. /* clear device parameters */
  266. nand->bus_width = 0;
  267. nand->address_cycles = 0;
  268. nand->page_size = 0;
  269. nand->erase_size = 0;
  270. /* initialize controller (device parameters are zero, use controller default) */
  271. retval = nand->controller->init(nand);
  272. if (retval != ERROR_OK) {
  273. switch (retval) {
  274. case ERROR_NAND_OPERATION_FAILED:
  275. LOG_DEBUG("controller initialization failed");
  276. return ERROR_NAND_OPERATION_FAILED;
  277. case ERROR_NAND_OPERATION_NOT_SUPPORTED:
  278. LOG_ERROR(
  279. "BUG: controller reported that it doesn't support default parameters");
  280. return ERROR_NAND_OPERATION_FAILED;
  281. default:
  282. LOG_ERROR("BUG: unknown controller initialization failure");
  283. return ERROR_NAND_OPERATION_FAILED;
  284. }
  285. }
  286. nand->controller->command(nand, NAND_CMD_RESET);
  287. nand->controller->reset(nand);
  288. nand->controller->command(nand, NAND_CMD_READID);
  289. nand->controller->address(nand, 0x0);
  290. if (nand->bus_width == 8) {
  291. nand->controller->read_data(nand, &manufacturer_id);
  292. nand->controller->read_data(nand, &device_id);
  293. } else {
  294. uint16_t data_buf;
  295. nand->controller->read_data(nand, &data_buf);
  296. manufacturer_id = data_buf & 0xff;
  297. nand->controller->read_data(nand, &data_buf);
  298. device_id = data_buf & 0xff;
  299. }
  300. for (i = 0; nand_flash_ids[i].name; i++) {
  301. if (nand_flash_ids[i].id == device_id &&
  302. (nand_flash_ids[i].mfr_id == manufacturer_id ||
  303. nand_flash_ids[i].mfr_id == 0)) {
  304. nand->device = &nand_flash_ids[i];
  305. break;
  306. }
  307. }
  308. for (i = 0; nand_manuf_ids[i].name; i++) {
  309. if (nand_manuf_ids[i].id == manufacturer_id) {
  310. nand->manufacturer = &nand_manuf_ids[i];
  311. break;
  312. }
  313. }
  314. if (!nand->manufacturer) {
  315. nand->manufacturer = &nand_manuf_ids[0];
  316. nand->manufacturer->id = manufacturer_id;
  317. }
  318. if (!nand->device) {
  319. LOG_ERROR(
  320. "unknown NAND flash device found, manufacturer id: 0x%2.2x device id: 0x%2.2x",
  321. manufacturer_id,
  322. device_id);
  323. return ERROR_NAND_OPERATION_FAILED;
  324. }
  325. LOG_DEBUG("found %s (%s)", nand->device->name, nand->manufacturer->name);
  326. /* initialize device parameters */
  327. /* bus width */
  328. if (nand->device->options & NAND_BUSWIDTH_16)
  329. nand->bus_width = 16;
  330. else
  331. nand->bus_width = 8;
  332. /* Do we need extended device probe information? */
  333. if (nand->device->page_size == 0 ||
  334. nand->device->erase_size == 0) {
  335. if (nand->bus_width == 8) {
  336. retval = nand->controller->read_data(nand, id_buff + 3);
  337. if (retval != ERROR_OK)
  338. return retval;
  339. retval = nand->controller->read_data(nand, id_buff + 4);
  340. if (retval != ERROR_OK)
  341. return retval;
  342. retval = nand->controller->read_data(nand, id_buff + 5);
  343. if (retval != ERROR_OK)
  344. return retval;
  345. } else {
  346. uint16_t data_buf;
  347. retval = nand->controller->read_data(nand, &data_buf);
  348. if (retval != ERROR_OK)
  349. return retval;
  350. id_buff[3] = data_buf;
  351. retval = nand->controller->read_data(nand, &data_buf);
  352. if (retval != ERROR_OK)
  353. return retval;
  354. id_buff[4] = data_buf;
  355. retval = nand->controller->read_data(nand, &data_buf);
  356. if (retval != ERROR_OK)
  357. return retval;
  358. id_buff[5] = data_buf >> 8;
  359. }
  360. }
  361. /* page size */
  362. if (nand->device->page_size == 0)
  363. nand->page_size = 1 << (10 + (id_buff[4] & 3));
  364. else if (nand->device->page_size == 256) {
  365. LOG_ERROR("NAND flashes with 256 byte pagesize are not supported");
  366. return ERROR_NAND_OPERATION_FAILED;
  367. } else
  368. nand->page_size = nand->device->page_size;
  369. /* number of address cycles */
  370. if (nand->page_size <= 512) {
  371. /* small page devices */
  372. if (nand->device->chip_size <= 32)
  373. nand->address_cycles = 3;
  374. else if (nand->device->chip_size <= 8*1024)
  375. nand->address_cycles = 4;
  376. else {
  377. LOG_ERROR("BUG: small page NAND device with more than 8 GiB encountered");
  378. nand->address_cycles = 5;
  379. }
  380. } else {
  381. /* large page devices */
  382. if (nand->device->chip_size <= 128)
  383. nand->address_cycles = 4;
  384. else if (nand->device->chip_size <= 32*1024)
  385. nand->address_cycles = 5;
  386. else {
  387. LOG_ERROR("BUG: large page NAND device with more than 32 GiB encountered");
  388. nand->address_cycles = 6;
  389. }
  390. }
  391. /* erase size */
  392. if (nand->device->erase_size == 0) {
  393. switch ((id_buff[4] >> 4) & 3) {
  394. case 0:
  395. nand->erase_size = 64 << 10;
  396. break;
  397. case 1:
  398. nand->erase_size = 128 << 10;
  399. break;
  400. case 2:
  401. nand->erase_size = 256 << 10;
  402. break;
  403. case 3:
  404. nand->erase_size = 512 << 10;
  405. break;
  406. }
  407. } else
  408. nand->erase_size = nand->device->erase_size;
  409. /* initialize controller, but leave parameters at the controllers default */
  410. retval = nand->controller->init(nand);
  411. if (retval != ERROR_OK) {
  412. switch (retval) {
  413. case ERROR_NAND_OPERATION_FAILED:
  414. LOG_DEBUG("controller initialization failed");
  415. return ERROR_NAND_OPERATION_FAILED;
  416. case ERROR_NAND_OPERATION_NOT_SUPPORTED:
  417. LOG_ERROR(
  418. "controller doesn't support requested parameters (buswidth: %i, address cycles: %i, page size: %i)",
  419. nand->bus_width,
  420. nand->address_cycles,
  421. nand->page_size);
  422. return ERROR_NAND_OPERATION_FAILED;
  423. default:
  424. LOG_ERROR("BUG: unknown controller initialization failure");
  425. return ERROR_NAND_OPERATION_FAILED;
  426. }
  427. }
  428. nand->num_blocks = (nand->device->chip_size * 1024) / (nand->erase_size / 1024);
  429. nand->blocks = malloc(sizeof(struct nand_block) * nand->num_blocks);
  430. for (i = 0; i < nand->num_blocks; i++) {
  431. nand->blocks[i].size = nand->erase_size;
  432. nand->blocks[i].offset = i * nand->erase_size;
  433. nand->blocks[i].is_erased = -1;
  434. nand->blocks[i].is_bad = -1;
  435. }
  436. return ERROR_OK;
  437. }
  438. int nand_erase(struct nand_device *nand, int first_block, int last_block)
  439. {
  440. int i;
  441. uint32_t page;
  442. uint8_t status;
  443. int retval;
  444. if (!nand->device)
  445. return ERROR_NAND_DEVICE_NOT_PROBED;
  446. if ((first_block < 0) || (last_block >= nand->num_blocks))
  447. return ERROR_COMMAND_SYNTAX_ERROR;
  448. /* make sure we know if a block is bad before erasing it */
  449. for (i = first_block; i <= last_block; i++) {
  450. if (nand->blocks[i].is_bad == -1) {
  451. nand_build_bbt(nand, i, last_block);
  452. break;
  453. }
  454. }
  455. for (i = first_block; i <= last_block; i++) {
  456. /* Send erase setup command */
  457. nand->controller->command(nand, NAND_CMD_ERASE1);
  458. page = i * (nand->erase_size / nand->page_size);
  459. /* Send page address */
  460. if (nand->page_size <= 512) {
  461. /* row */
  462. nand->controller->address(nand, page & 0xff);
  463. nand->controller->address(nand, (page >> 8) & 0xff);
  464. /* 3rd cycle only on devices with more than 32 MiB */
  465. if (nand->address_cycles >= 4)
  466. nand->controller->address(nand, (page >> 16) & 0xff);
  467. /* 4th cycle only on devices with more than 8 GiB */
  468. if (nand->address_cycles >= 5)
  469. nand->controller->address(nand, (page >> 24) & 0xff);
  470. } else {
  471. /* row */
  472. nand->controller->address(nand, page & 0xff);
  473. nand->controller->address(nand, (page >> 8) & 0xff);
  474. /* 3rd cycle only on devices with more than 128 MiB */
  475. if (nand->address_cycles >= 5)
  476. nand->controller->address(nand, (page >> 16) & 0xff);
  477. }
  478. /* Send erase confirm command */
  479. nand->controller->command(nand, NAND_CMD_ERASE2);
  480. retval = nand->controller->nand_ready ?
  481. nand->controller->nand_ready(nand, 1000) :
  482. nand_poll_ready(nand, 1000);
  483. if (!retval) {
  484. LOG_ERROR("timeout waiting for NAND flash block erase to complete");
  485. return ERROR_NAND_OPERATION_TIMEOUT;
  486. }
  487. retval = nand_read_status(nand, &status);
  488. if (retval != ERROR_OK) {
  489. LOG_ERROR("couldn't read status");
  490. return ERROR_NAND_OPERATION_FAILED;
  491. }
  492. if (status & 0x1) {
  493. LOG_ERROR("didn't erase %sblock %d; status: 0x%2.2x",
  494. (nand->blocks[i].is_bad == 1)
  495. ? "bad " : "",
  496. i, status);
  497. /* continue; other blocks might still be erasable */
  498. }
  499. nand->blocks[i].is_erased = 1;
  500. }
  501. return ERROR_OK;
  502. }
  503. #if 0
  504. static int nand_read_plain(struct nand_device *nand,
  505. uint32_t address,
  506. uint8_t *data,
  507. uint32_t data_size)
  508. {
  509. uint8_t *page;
  510. if (!nand->device)
  511. return ERROR_NAND_DEVICE_NOT_PROBED;
  512. if (address % nand->page_size) {
  513. LOG_ERROR("reads need to be page aligned");
  514. return ERROR_NAND_OPERATION_FAILED;
  515. }
  516. page = malloc(nand->page_size);
  517. while (data_size > 0) {
  518. uint32_t thisrun_size = (data_size > nand->page_size) ? nand->page_size : data_size;
  519. uint32_t page_address;
  520. page_address = address / nand->page_size;
  521. nand_read_page(nand, page_address, page, nand->page_size, NULL, 0);
  522. memcpy(data, page, thisrun_size);
  523. address += thisrun_size;
  524. data += thisrun_size;
  525. data_size -= thisrun_size;
  526. }
  527. free(page);
  528. return ERROR_OK;
  529. }
  530. static int nand_write_plain(struct nand_device *nand,
  531. uint32_t address,
  532. uint8_t *data,
  533. uint32_t data_size)
  534. {
  535. uint8_t *page;
  536. if (!nand->device)
  537. return ERROR_NAND_DEVICE_NOT_PROBED;
  538. if (address % nand->page_size) {
  539. LOG_ERROR("writes need to be page aligned");
  540. return ERROR_NAND_OPERATION_FAILED;
  541. }
  542. page = malloc(nand->page_size);
  543. while (data_size > 0) {
  544. uint32_t thisrun_size = (data_size > nand->page_size) ? nand->page_size : data_size;
  545. uint32_t page_address;
  546. memset(page, 0xff, nand->page_size);
  547. memcpy(page, data, thisrun_size);
  548. page_address = address / nand->page_size;
  549. nand_write_page(nand, page_address, page, nand->page_size, NULL, 0);
  550. address += thisrun_size;
  551. data += thisrun_size;
  552. data_size -= thisrun_size;
  553. }
  554. free(page);
  555. return ERROR_OK;
  556. }
  557. #endif
  558. int nand_write_page(struct nand_device *nand, uint32_t page,
  559. uint8_t *data, uint32_t data_size,
  560. uint8_t *oob, uint32_t oob_size)
  561. {
  562. uint32_t block;
  563. if (!nand->device)
  564. return ERROR_NAND_DEVICE_NOT_PROBED;
  565. block = page / (nand->erase_size / nand->page_size);
  566. if (nand->blocks[block].is_erased == 1)
  567. nand->blocks[block].is_erased = 0;
  568. if (nand->use_raw || !nand->controller->write_page)
  569. return nand_write_page_raw(nand, page, data, data_size, oob, oob_size);
  570. else
  571. return nand->controller->write_page(nand, page, data, data_size, oob, oob_size);
  572. }
  573. int nand_read_page(struct nand_device *nand, uint32_t page,
  574. uint8_t *data, uint32_t data_size,
  575. uint8_t *oob, uint32_t oob_size)
  576. {
  577. if (!nand->device)
  578. return ERROR_NAND_DEVICE_NOT_PROBED;
  579. if (nand->use_raw || !nand->controller->read_page)
  580. return nand_read_page_raw(nand, page, data, data_size, oob, oob_size);
  581. else
  582. return nand->controller->read_page(nand, page, data, data_size, oob, oob_size);
  583. }
  584. int nand_page_command(struct nand_device *nand, uint32_t page,
  585. uint8_t cmd, bool oob_only)
  586. {
  587. if (!nand->device)
  588. return ERROR_NAND_DEVICE_NOT_PROBED;
  589. if (oob_only && NAND_CMD_READ0 == cmd && nand->page_size <= 512)
  590. cmd = NAND_CMD_READOOB;
  591. nand->controller->command(nand, cmd);
  592. if (nand->page_size <= 512) {
  593. /* small page device */
  594. /* column (always 0, we start at the beginning of a page/OOB area) */
  595. nand->controller->address(nand, 0x0);
  596. /* row */
  597. nand->controller->address(nand, page & 0xff);
  598. nand->controller->address(nand, (page >> 8) & 0xff);
  599. /* 4th cycle only on devices with more than 32 MiB */
  600. if (nand->address_cycles >= 4)
  601. nand->controller->address(nand, (page >> 16) & 0xff);
  602. /* 5th cycle only on devices with more than 8 GiB */
  603. if (nand->address_cycles >= 5)
  604. nand->controller->address(nand, (page >> 24) & 0xff);
  605. } else {
  606. /* large page device */
  607. /* column (0 when we start at the beginning of a page,
  608. * or 2048 for the beginning of OOB area)
  609. */
  610. nand->controller->address(nand, 0x0);
  611. if (oob_only)
  612. nand->controller->address(nand, 0x8);
  613. else
  614. nand->controller->address(nand, 0x0);
  615. /* row */
  616. nand->controller->address(nand, page & 0xff);
  617. nand->controller->address(nand, (page >> 8) & 0xff);
  618. /* 5th cycle only on devices with more than 128 MiB */
  619. if (nand->address_cycles >= 5)
  620. nand->controller->address(nand, (page >> 16) & 0xff);
  621. /* large page devices need a start command if reading */
  622. if (cmd == NAND_CMD_READ0)
  623. nand->controller->command(nand, NAND_CMD_READSTART);
  624. }
  625. if (nand->controller->nand_ready) {
  626. if (!nand->controller->nand_ready(nand, 100))
  627. return ERROR_NAND_OPERATION_TIMEOUT;
  628. } else {
  629. /* nand_poll_read() cannot be used during nand read */
  630. alive_sleep(1);
  631. }
  632. return ERROR_OK;
  633. }
  634. int nand_read_data_page(struct nand_device *nand, uint8_t *data, uint32_t size)
  635. {
  636. int retval = ERROR_NAND_NO_BUFFER;
  637. if (nand->controller->read_block_data)
  638. retval = (nand->controller->read_block_data)(nand, data, size);
  639. if (retval == ERROR_NAND_NO_BUFFER) {
  640. uint32_t i;
  641. int incr = (nand->device->options & NAND_BUSWIDTH_16) ? 2 : 1;
  642. retval = ERROR_OK;
  643. for (i = 0; retval == ERROR_OK && i < size; i += incr) {
  644. retval = nand->controller->read_data(nand, data);
  645. data += incr;
  646. }
  647. }
  648. return retval;
  649. }
  650. int nand_read_page_raw(struct nand_device *nand, uint32_t page,
  651. uint8_t *data, uint32_t data_size,
  652. uint8_t *oob, uint32_t oob_size)
  653. {
  654. int retval;
  655. retval = nand_page_command(nand, page, NAND_CMD_READ0, !data);
  656. if (retval != ERROR_OK)
  657. return retval;
  658. if (data)
  659. nand_read_data_page(nand, data, data_size);
  660. if (oob)
  661. nand_read_data_page(nand, oob, oob_size);
  662. return ERROR_OK;
  663. }
  664. int nand_write_data_page(struct nand_device *nand, uint8_t *data, uint32_t size)
  665. {
  666. int retval = ERROR_NAND_NO_BUFFER;
  667. if (nand->controller->write_block_data)
  668. retval = (nand->controller->write_block_data)(nand, data, size);
  669. if (retval == ERROR_NAND_NO_BUFFER) {
  670. bool is16bit = nand->device->options & NAND_BUSWIDTH_16;
  671. uint32_t incr = is16bit ? 2 : 1;
  672. uint16_t write_data;
  673. uint32_t i;
  674. for (i = 0; i < size; i += incr) {
  675. if (is16bit)
  676. write_data = le_to_h_u16(data);
  677. else
  678. write_data = *data;
  679. retval = nand->controller->write_data(nand, write_data);
  680. if (retval != ERROR_OK)
  681. break;
  682. data += incr;
  683. }
  684. }
  685. return retval;
  686. }
  687. int nand_write_finish(struct nand_device *nand)
  688. {
  689. int retval;
  690. uint8_t status;
  691. nand->controller->command(nand, NAND_CMD_PAGEPROG);
  692. retval = nand->controller->nand_ready ?
  693. nand->controller->nand_ready(nand, 100) :
  694. nand_poll_ready(nand, 100);
  695. if (!retval)
  696. return ERROR_NAND_OPERATION_TIMEOUT;
  697. retval = nand_read_status(nand, &status);
  698. if (retval != ERROR_OK) {
  699. LOG_ERROR("couldn't read status");
  700. return ERROR_NAND_OPERATION_FAILED;
  701. }
  702. if (status & NAND_STATUS_FAIL) {
  703. LOG_ERROR("write operation didn't pass, status: 0x%2.2x",
  704. status);
  705. return ERROR_NAND_OPERATION_FAILED;
  706. }
  707. return ERROR_OK;
  708. }
  709. int nand_write_page_raw(struct nand_device *nand, uint32_t page,
  710. uint8_t *data, uint32_t data_size,
  711. uint8_t *oob, uint32_t oob_size)
  712. {
  713. int retval;
  714. retval = nand_page_command(nand, page, NAND_CMD_SEQIN, !data);
  715. if (retval != ERROR_OK)
  716. return retval;
  717. if (data) {
  718. retval = nand_write_data_page(nand, data, data_size);
  719. if (retval != ERROR_OK) {
  720. LOG_ERROR("Unable to write data to NAND device");
  721. return retval;
  722. }
  723. }
  724. if (oob) {
  725. retval = nand_write_data_page(nand, oob, oob_size);
  726. if (retval != ERROR_OK) {
  727. LOG_ERROR("Unable to write OOB data to NAND device");
  728. return retval;
  729. }
  730. }
  731. return nand_write_finish(nand);
  732. }