dfu_nand.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /*
  2. * dfu_nand.c -- DFU for NAND routines.
  3. *
  4. * Copyright (C) 2012-2013 Texas Instruments, Inc.
  5. *
  6. * Based on dfu_mmc.c which is:
  7. * Copyright (C) 2012 Samsung Electronics
  8. * author: Lukasz Majewski <l.majewski@samsung.com>
  9. *
  10. * SPDX-License-Identifier: GPL-2.0+
  11. */
  12. #include <common.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. 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. ret = nand_write_skip_bad(mtd, start, &count, &actual,
  59. lim, buf, WITH_WR_VERIFY);
  60. }
  61. if (ret != 0) {
  62. printf("%s: nand_%s_skip_bad call failed at %llx!\n",
  63. __func__, op == DFU_OP_READ ? "read" : "write",
  64. start);
  65. return ret;
  66. }
  67. /*
  68. * Find out where we stopped writing data. This can be deeper into
  69. * the NAND than we expected due to having to skip bad blocks. So
  70. * we must take this into account for the next write, if any.
  71. */
  72. if (actual > count)
  73. dfu->bad_skip += actual - count;
  74. return ret;
  75. }
  76. static inline int nand_block_write(struct dfu_entity *dfu,
  77. u64 offset, void *buf, long *len)
  78. {
  79. return nand_block_op(DFU_OP_WRITE, dfu, offset, buf, len);
  80. }
  81. static inline int nand_block_read(struct dfu_entity *dfu,
  82. u64 offset, void *buf, long *len)
  83. {
  84. return nand_block_op(DFU_OP_READ, dfu, offset, buf, len);
  85. }
  86. static int dfu_write_medium_nand(struct dfu_entity *dfu,
  87. u64 offset, void *buf, long *len)
  88. {
  89. int ret = -1;
  90. switch (dfu->layout) {
  91. case DFU_RAW_ADDR:
  92. ret = nand_block_write(dfu, offset, buf, len);
  93. break;
  94. default:
  95. printf("%s: Layout (%s) not (yet) supported!\n", __func__,
  96. dfu_get_layout(dfu->layout));
  97. }
  98. return ret;
  99. }
  100. long dfu_get_medium_size_nand(struct dfu_entity *dfu)
  101. {
  102. return dfu->data.nand.size;
  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. }