dfu_nand.c 5.7 KB

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