dfu_nand.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * dfu_nand.c -- DFU for NAND routines.
  4. *
  5. * Copyright (C) 2012-2013 Texas Instruments, Inc.
  6. *
  7. * Based on dfu_mmc.c which is:
  8. * Copyright (C) 2012 Samsung Electronics
  9. * author: Lukasz Majewski <l.majewski@samsung.com>
  10. */
  11. #include <common.h>
  12. #include <malloc.h>
  13. #include <errno.h>
  14. #include <div64.h>
  15. #include <dfu.h>
  16. #include <linux/mtd/mtd.h>
  17. #include <jffs2/load_kernel.h>
  18. #include <nand.h>
  19. static int nand_block_op(enum dfu_op op, struct dfu_entity *dfu,
  20. u64 offset, void *buf, long *len)
  21. {
  22. loff_t start, lim;
  23. size_t count, actual;
  24. int ret;
  25. struct mtd_info *mtd;
  26. /* if buf == NULL return total size of the area */
  27. if (buf == NULL) {
  28. *len = dfu->data.nand.size;
  29. return 0;
  30. }
  31. start = dfu->data.nand.start + offset + dfu->bad_skip;
  32. lim = dfu->data.nand.start + dfu->data.nand.size - start;
  33. count = *len;
  34. mtd = get_nand_dev_by_index(nand_curr_device);
  35. if (nand_curr_device < 0 ||
  36. nand_curr_device >= CONFIG_SYS_MAX_NAND_DEVICE ||
  37. !mtd) {
  38. printf("%s: invalid nand device\n", __func__);
  39. return -1;
  40. }
  41. if (op == DFU_OP_READ) {
  42. ret = nand_read_skip_bad(mtd, start, &count, &actual,
  43. lim, buf);
  44. } else {
  45. nand_erase_options_t opts;
  46. memset(&opts, 0, sizeof(opts));
  47. opts.offset = start;
  48. opts.length = count;
  49. opts.spread = 1;
  50. opts.quiet = 1;
  51. opts.lim = lim;
  52. /* first erase */
  53. ret = nand_erase_opts(mtd, &opts);
  54. if (ret)
  55. return ret;
  56. /* then write */
  57. ret = nand_write_skip_bad(mtd, start, &count, &actual,
  58. lim, buf, WITH_WR_VERIFY);
  59. }
  60. if (ret != 0) {
  61. printf("%s: nand_%s_skip_bad call failed at %llx!\n",
  62. __func__, op == DFU_OP_READ ? "read" : "write",
  63. start);
  64. return ret;
  65. }
  66. /*
  67. * Find out where we stopped writing data. This can be deeper into
  68. * the NAND than we expected due to having to skip bad blocks. So
  69. * we must take this into account for the next write, if any.
  70. */
  71. if (actual > count)
  72. dfu->bad_skip += actual - count;
  73. return ret;
  74. }
  75. static inline int nand_block_write(struct dfu_entity *dfu,
  76. u64 offset, void *buf, long *len)
  77. {
  78. return nand_block_op(DFU_OP_WRITE, dfu, offset, buf, len);
  79. }
  80. static inline int nand_block_read(struct dfu_entity *dfu,
  81. u64 offset, void *buf, long *len)
  82. {
  83. return nand_block_op(DFU_OP_READ, dfu, offset, buf, len);
  84. }
  85. static int dfu_write_medium_nand(struct dfu_entity *dfu,
  86. u64 offset, void *buf, long *len)
  87. {
  88. int ret = -1;
  89. switch (dfu->layout) {
  90. case DFU_RAW_ADDR:
  91. ret = nand_block_write(dfu, offset, buf, len);
  92. break;
  93. default:
  94. printf("%s: Layout (%s) not (yet) supported!\n", __func__,
  95. dfu_get_layout(dfu->layout));
  96. }
  97. return ret;
  98. }
  99. int dfu_get_medium_size_nand(struct dfu_entity *dfu, u64 *size)
  100. {
  101. *size = dfu->data.nand.size;
  102. return 0;
  103. }
  104. static int dfu_read_medium_nand(struct dfu_entity *dfu, u64 offset, void *buf,
  105. long *len)
  106. {
  107. int ret = -1;
  108. switch (dfu->layout) {
  109. case DFU_RAW_ADDR:
  110. ret = nand_block_read(dfu, offset, buf, len);
  111. break;
  112. default:
  113. printf("%s: Layout (%s) not (yet) supported!\n", __func__,
  114. dfu_get_layout(dfu->layout));
  115. }
  116. return ret;
  117. }
  118. static int dfu_flush_medium_nand(struct dfu_entity *dfu)
  119. {
  120. int ret = 0;
  121. u64 off;
  122. /* in case of ubi partition, erase rest of the partition */
  123. if (dfu->data.nand.ubi) {
  124. struct mtd_info *mtd = get_nand_dev_by_index(nand_curr_device);
  125. nand_erase_options_t opts;
  126. if (nand_curr_device < 0 ||
  127. nand_curr_device >= CONFIG_SYS_MAX_NAND_DEVICE ||
  128. !mtd) {
  129. printf("%s: invalid nand device\n", __func__);
  130. return -1;
  131. }
  132. memset(&opts, 0, sizeof(opts));
  133. off = dfu->offset;
  134. if ((off & (mtd->erasesize - 1)) != 0) {
  135. /*
  136. * last write ended with unaligned length
  137. * sector is erased, jump to next
  138. */
  139. off = off & ~((mtd->erasesize - 1));
  140. off += mtd->erasesize;
  141. }
  142. opts.offset = dfu->data.nand.start + off +
  143. dfu->bad_skip;
  144. opts.length = dfu->data.nand.start +
  145. dfu->data.nand.size - opts.offset;
  146. ret = nand_erase_opts(mtd, &opts);
  147. if (ret != 0)
  148. printf("Failure erase: %d\n", ret);
  149. }
  150. return ret;
  151. }
  152. unsigned int dfu_polltimeout_nand(struct dfu_entity *dfu)
  153. {
  154. /*
  155. * Currently, Poll Timeout != 0 is only needed on nand
  156. * ubi partition, as the not used sectors need an erase
  157. */
  158. if (dfu->data.nand.ubi)
  159. return DFU_MANIFEST_POLL_TIMEOUT;
  160. return DFU_DEFAULT_POLL_TIMEOUT;
  161. }
  162. int dfu_fill_entity_nand(struct dfu_entity *dfu, char *devstr, char *s)
  163. {
  164. char *st;
  165. int ret, dev, part;
  166. dfu->data.nand.ubi = 0;
  167. dfu->dev_type = DFU_DEV_NAND;
  168. st = strsep(&s, " ");
  169. if (!strcmp(st, "raw")) {
  170. dfu->layout = DFU_RAW_ADDR;
  171. dfu->data.nand.start = simple_strtoul(s, &s, 16);
  172. s++;
  173. dfu->data.nand.size = simple_strtoul(s, &s, 16);
  174. } else if ((!strcmp(st, "part")) || (!strcmp(st, "partubi"))) {
  175. char mtd_id[32];
  176. struct mtd_device *mtd_dev;
  177. u8 part_num;
  178. struct part_info *pi;
  179. dfu->layout = DFU_RAW_ADDR;
  180. dev = simple_strtoul(s, &s, 10);
  181. s++;
  182. part = simple_strtoul(s, &s, 10);
  183. sprintf(mtd_id, "%s%d,%d", "nand", dev, part - 1);
  184. printf("using id '%s'\n", mtd_id);
  185. mtdparts_init();
  186. ret = find_dev_and_part(mtd_id, &mtd_dev, &part_num, &pi);
  187. if (ret != 0) {
  188. printf("Could not locate '%s'\n", mtd_id);
  189. return -1;
  190. }
  191. dfu->data.nand.start = pi->offset;
  192. dfu->data.nand.size = pi->size;
  193. if (!strcmp(st, "partubi"))
  194. dfu->data.nand.ubi = 1;
  195. } else {
  196. printf("%s: Memory layout (%s) not supported!\n", __func__, st);
  197. return -1;
  198. }
  199. dfu->get_medium_size = dfu_get_medium_size_nand;
  200. dfu->read_medium = dfu_read_medium_nand;
  201. dfu->write_medium = dfu_write_medium_nand;
  202. dfu->flush_medium = dfu_flush_medium_nand;
  203. dfu->poll_timeout = dfu_polltimeout_nand;
  204. /* initial state */
  205. dfu->inited = 0;
  206. return 0;
  207. }