dfu_nand.c 5.7 KB

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