Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Pantelis Antoniou | cf14d0d | 2013-03-14 05:32:52 +0000 | [diff] [blame] | 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> |
Pantelis Antoniou | cf14d0d | 2013-03-14 05:32:52 +0000 | [diff] [blame] | 10 | */ |
| 11 | |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 12 | #include <log.h> |
Pantelis Antoniou | cf14d0d | 2013-03-14 05:32:52 +0000 | [diff] [blame] | 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 | |
Afzal Mohammed | b9a4a6b | 2013-09-18 01:14:50 +0530 | [diff] [blame] | 21 | static int nand_block_op(enum dfu_op op, struct dfu_entity *dfu, |
Pantelis Antoniou | cf14d0d | 2013-03-14 05:32:52 +0000 | [diff] [blame] | 22 | u64 offset, void *buf, long *len) |
| 23 | { |
| 24 | loff_t start, lim; |
| 25 | size_t count, actual; |
| 26 | int ret; |
Scott Wood | 08364d9 | 2016-05-30 13:57:54 -0500 | [diff] [blame] | 27 | struct mtd_info *mtd; |
Pantelis Antoniou | cf14d0d | 2013-03-14 05:32:52 +0000 | [diff] [blame] | 28 | |
| 29 | /* if buf == NULL return total size of the area */ |
| 30 | if (buf == NULL) { |
| 31 | *len = dfu->data.nand.size; |
| 32 | return 0; |
| 33 | } |
| 34 | |
| 35 | start = dfu->data.nand.start + offset + dfu->bad_skip; |
| 36 | lim = dfu->data.nand.start + dfu->data.nand.size - start; |
| 37 | count = *len; |
| 38 | |
Grygorii Strashko | ae1e9c7 | 2017-06-26 19:12:53 -0500 | [diff] [blame] | 39 | mtd = get_nand_dev_by_index(nand_curr_device); |
| 40 | |
Pantelis Antoniou | cf14d0d | 2013-03-14 05:32:52 +0000 | [diff] [blame] | 41 | if (nand_curr_device < 0 || |
| 42 | nand_curr_device >= CONFIG_SYS_MAX_NAND_DEVICE || |
Grygorii Strashko | ae1e9c7 | 2017-06-26 19:12:53 -0500 | [diff] [blame] | 43 | !mtd) { |
Pantelis Antoniou | cf14d0d | 2013-03-14 05:32:52 +0000 | [diff] [blame] | 44 | printf("%s: invalid nand device\n", __func__); |
| 45 | return -1; |
| 46 | } |
| 47 | |
Heiko Schocher | 0ae98b4 | 2013-06-24 18:50:40 +0200 | [diff] [blame] | 48 | if (op == DFU_OP_READ) { |
Scott Wood | 08364d9 | 2016-05-30 13:57:54 -0500 | [diff] [blame] | 49 | ret = nand_read_skip_bad(mtd, start, &count, &actual, |
| 50 | lim, buf); |
Heiko Schocher | 0ae98b4 | 2013-06-24 18:50:40 +0200 | [diff] [blame] | 51 | } else { |
| 52 | nand_erase_options_t opts; |
Guillermo Rodríguez | 5378fb1 | 2019-12-16 16:27:57 +0100 | [diff] [blame] | 53 | int write_flags = WITH_WR_VERIFY; |
Heiko Schocher | 0ae98b4 | 2013-06-24 18:50:40 +0200 | [diff] [blame] | 54 | |
| 55 | memset(&opts, 0, sizeof(opts)); |
| 56 | opts.offset = start; |
| 57 | opts.length = count; |
| 58 | opts.spread = 1; |
| 59 | opts.quiet = 1; |
| 60 | opts.lim = lim; |
| 61 | /* first erase */ |
Scott Wood | 08364d9 | 2016-05-30 13:57:54 -0500 | [diff] [blame] | 62 | ret = nand_erase_opts(mtd, &opts); |
Heiko Schocher | 0ae98b4 | 2013-06-24 18:50:40 +0200 | [diff] [blame] | 63 | if (ret) |
| 64 | return ret; |
| 65 | /* then write */ |
Guillermo Rodríguez | 5378fb1 | 2019-12-16 16:27:57 +0100 | [diff] [blame] | 66 | #ifdef CONFIG_DFU_NAND_TRIMFFS |
| 67 | if (dfu->data.nand.ubi) |
| 68 | write_flags |= WITH_DROP_FFS; |
| 69 | #endif |
Scott Wood | 08364d9 | 2016-05-30 13:57:54 -0500 | [diff] [blame] | 70 | ret = nand_write_skip_bad(mtd, start, &count, &actual, |
Guillermo Rodríguez | 5378fb1 | 2019-12-16 16:27:57 +0100 | [diff] [blame] | 71 | lim, buf, write_flags); |
Heiko Schocher | 0ae98b4 | 2013-06-24 18:50:40 +0200 | [diff] [blame] | 72 | } |
Pantelis Antoniou | cf14d0d | 2013-03-14 05:32:52 +0000 | [diff] [blame] | 73 | |
| 74 | if (ret != 0) { |
| 75 | printf("%s: nand_%s_skip_bad call failed at %llx!\n", |
| 76 | __func__, op == DFU_OP_READ ? "read" : "write", |
| 77 | start); |
| 78 | return ret; |
| 79 | } |
| 80 | |
| 81 | /* |
| 82 | * Find out where we stopped writing data. This can be deeper into |
| 83 | * the NAND than we expected due to having to skip bad blocks. So |
| 84 | * we must take this into account for the next write, if any. |
| 85 | */ |
| 86 | if (actual > count) |
| 87 | dfu->bad_skip += actual - count; |
| 88 | |
| 89 | return ret; |
| 90 | } |
| 91 | |
| 92 | static inline int nand_block_write(struct dfu_entity *dfu, |
| 93 | u64 offset, void *buf, long *len) |
| 94 | { |
| 95 | return nand_block_op(DFU_OP_WRITE, dfu, offset, buf, len); |
| 96 | } |
| 97 | |
| 98 | static inline int nand_block_read(struct dfu_entity *dfu, |
| 99 | u64 offset, void *buf, long *len) |
| 100 | { |
| 101 | return nand_block_op(DFU_OP_READ, dfu, offset, buf, len); |
| 102 | } |
| 103 | |
| 104 | static int dfu_write_medium_nand(struct dfu_entity *dfu, |
| 105 | u64 offset, void *buf, long *len) |
| 106 | { |
| 107 | int ret = -1; |
| 108 | |
| 109 | switch (dfu->layout) { |
| 110 | case DFU_RAW_ADDR: |
| 111 | ret = nand_block_write(dfu, offset, buf, len); |
| 112 | break; |
| 113 | default: |
| 114 | printf("%s: Layout (%s) not (yet) supported!\n", __func__, |
| 115 | dfu_get_layout(dfu->layout)); |
| 116 | } |
| 117 | |
| 118 | return ret; |
| 119 | } |
| 120 | |
Patrick Delaunay | f4e934f | 2017-07-19 16:39:23 +0200 | [diff] [blame] | 121 | int dfu_get_medium_size_nand(struct dfu_entity *dfu, u64 *size) |
Stephen Warren | 020e6f3 | 2014-06-11 12:47:27 -0600 | [diff] [blame] | 122 | { |
Patrick Delaunay | 5ea9b89 | 2017-07-19 16:39:22 +0200 | [diff] [blame] | 123 | *size = dfu->data.nand.size; |
| 124 | |
| 125 | return 0; |
Stephen Warren | 020e6f3 | 2014-06-11 12:47:27 -0600 | [diff] [blame] | 126 | } |
| 127 | |
Pantelis Antoniou | cf14d0d | 2013-03-14 05:32:52 +0000 | [diff] [blame] | 128 | static int dfu_read_medium_nand(struct dfu_entity *dfu, u64 offset, void *buf, |
| 129 | long *len) |
| 130 | { |
| 131 | int ret = -1; |
| 132 | |
| 133 | switch (dfu->layout) { |
| 134 | case DFU_RAW_ADDR: |
| 135 | ret = nand_block_read(dfu, offset, buf, len); |
| 136 | break; |
| 137 | default: |
| 138 | printf("%s: Layout (%s) not (yet) supported!\n", __func__, |
| 139 | dfu_get_layout(dfu->layout)); |
| 140 | } |
| 141 | |
| 142 | return ret; |
| 143 | } |
| 144 | |
Heiko Schocher | ad40139 | 2013-07-25 06:43:11 +0200 | [diff] [blame] | 145 | static int dfu_flush_medium_nand(struct dfu_entity *dfu) |
| 146 | { |
| 147 | int ret = 0; |
Heiko Schocher | 0ed9da5 | 2016-06-07 08:55:44 +0200 | [diff] [blame] | 148 | u64 off; |
Heiko Schocher | ad40139 | 2013-07-25 06:43:11 +0200 | [diff] [blame] | 149 | |
| 150 | /* in case of ubi partition, erase rest of the partition */ |
| 151 | if (dfu->data.nand.ubi) { |
Grygorii Strashko | ae1e9c7 | 2017-06-26 19:12:53 -0500 | [diff] [blame] | 152 | struct mtd_info *mtd = get_nand_dev_by_index(nand_curr_device); |
Heiko Schocher | ad40139 | 2013-07-25 06:43:11 +0200 | [diff] [blame] | 153 | nand_erase_options_t opts; |
| 154 | |
| 155 | if (nand_curr_device < 0 || |
| 156 | nand_curr_device >= CONFIG_SYS_MAX_NAND_DEVICE || |
Grygorii Strashko | ae1e9c7 | 2017-06-26 19:12:53 -0500 | [diff] [blame] | 157 | !mtd) { |
Heiko Schocher | ad40139 | 2013-07-25 06:43:11 +0200 | [diff] [blame] | 158 | printf("%s: invalid nand device\n", __func__); |
| 159 | return -1; |
| 160 | } |
| 161 | |
Heiko Schocher | ad40139 | 2013-07-25 06:43:11 +0200 | [diff] [blame] | 162 | memset(&opts, 0, sizeof(opts)); |
Heiko Schocher | 0ed9da5 | 2016-06-07 08:55:44 +0200 | [diff] [blame] | 163 | off = dfu->offset; |
| 164 | if ((off & (mtd->erasesize - 1)) != 0) { |
| 165 | /* |
| 166 | * last write ended with unaligned length |
| 167 | * sector is erased, jump to next |
| 168 | */ |
| 169 | off = off & ~((mtd->erasesize - 1)); |
| 170 | off += mtd->erasesize; |
| 171 | } |
| 172 | opts.offset = dfu->data.nand.start + off + |
Heiko Schocher | ad40139 | 2013-07-25 06:43:11 +0200 | [diff] [blame] | 173 | dfu->bad_skip; |
| 174 | opts.length = dfu->data.nand.start + |
| 175 | dfu->data.nand.size - opts.offset; |
Scott Wood | 08364d9 | 2016-05-30 13:57:54 -0500 | [diff] [blame] | 176 | ret = nand_erase_opts(mtd, &opts); |
Heiko Schocher | ad40139 | 2013-07-25 06:43:11 +0200 | [diff] [blame] | 177 | if (ret != 0) |
| 178 | printf("Failure erase: %d\n", ret); |
| 179 | } |
| 180 | |
| 181 | return ret; |
| 182 | } |
| 183 | |
Heiko Schocher | b948810 | 2014-04-11 07:59:47 +0200 | [diff] [blame] | 184 | unsigned int dfu_polltimeout_nand(struct dfu_entity *dfu) |
| 185 | { |
| 186 | /* |
| 187 | * Currently, Poll Timeout != 0 is only needed on nand |
| 188 | * ubi partition, as the not used sectors need an erase |
| 189 | */ |
| 190 | if (dfu->data.nand.ubi) |
| 191 | return DFU_MANIFEST_POLL_TIMEOUT; |
| 192 | |
| 193 | return DFU_DEFAULT_POLL_TIMEOUT; |
| 194 | } |
| 195 | |
Masami Hiramatsu | fa4282a | 2022-01-31 11:52:37 +0900 | [diff] [blame] | 196 | int dfu_fill_entity_nand(struct dfu_entity *dfu, char *devstr, char **argv, int argc) |
Pantelis Antoniou | cf14d0d | 2013-03-14 05:32:52 +0000 | [diff] [blame] | 197 | { |
Masami Hiramatsu | fa4282a | 2022-01-31 11:52:37 +0900 | [diff] [blame] | 198 | char *s; |
Pantelis Antoniou | cf14d0d | 2013-03-14 05:32:52 +0000 | [diff] [blame] | 199 | int ret, dev, part; |
| 200 | |
Heiko Schocher | ad40139 | 2013-07-25 06:43:11 +0200 | [diff] [blame] | 201 | dfu->data.nand.ubi = 0; |
Pantelis Antoniou | cf14d0d | 2013-03-14 05:32:52 +0000 | [diff] [blame] | 202 | dfu->dev_type = DFU_DEV_NAND; |
Masami Hiramatsu | fa4282a | 2022-01-31 11:52:37 +0900 | [diff] [blame] | 203 | if (argc != 3) |
| 204 | return -EINVAL; |
| 205 | |
| 206 | if (!strcmp(argv[0], "raw")) { |
Pantelis Antoniou | cf14d0d | 2013-03-14 05:32:52 +0000 | [diff] [blame] | 207 | dfu->layout = DFU_RAW_ADDR; |
Masami Hiramatsu | fa4282a | 2022-01-31 11:52:37 +0900 | [diff] [blame] | 208 | dfu->data.nand.start = hextoul(argv[1], &s); |
| 209 | if (*s) |
| 210 | return -EINVAL; |
| 211 | dfu->data.nand.size = hextoul(argv[2], &s); |
| 212 | if (*s) |
| 213 | return -EINVAL; |
| 214 | } else if ((!strcmp(argv[0], "part")) || (!strcmp(argv[0], "partubi"))) { |
Pantelis Antoniou | cf14d0d | 2013-03-14 05:32:52 +0000 | [diff] [blame] | 215 | char mtd_id[32]; |
| 216 | struct mtd_device *mtd_dev; |
| 217 | u8 part_num; |
| 218 | struct part_info *pi; |
| 219 | |
| 220 | dfu->layout = DFU_RAW_ADDR; |
| 221 | |
Masami Hiramatsu | fa4282a | 2022-01-31 11:52:37 +0900 | [diff] [blame] | 222 | dev = dectoul(argv[1], &s); |
| 223 | if (*s) |
| 224 | return -EINVAL; |
| 225 | part = dectoul(argv[2], &s); |
| 226 | if (*s) |
| 227 | return -EINVAL; |
Pantelis Antoniou | cf14d0d | 2013-03-14 05:32:52 +0000 | [diff] [blame] | 228 | |
| 229 | sprintf(mtd_id, "%s%d,%d", "nand", dev, part - 1); |
Ralph Siemsen | 756d9e1 | 2019-08-27 14:28:19 -0400 | [diff] [blame] | 230 | debug("using id '%s'\n", mtd_id); |
Pantelis Antoniou | cf14d0d | 2013-03-14 05:32:52 +0000 | [diff] [blame] | 231 | |
| 232 | mtdparts_init(); |
| 233 | |
| 234 | ret = find_dev_and_part(mtd_id, &mtd_dev, &part_num, &pi); |
| 235 | if (ret != 0) { |
| 236 | printf("Could not locate '%s'\n", mtd_id); |
| 237 | return -1; |
| 238 | } |
| 239 | |
| 240 | dfu->data.nand.start = pi->offset; |
| 241 | dfu->data.nand.size = pi->size; |
Masami Hiramatsu | fa4282a | 2022-01-31 11:52:37 +0900 | [diff] [blame] | 242 | if (!strcmp(argv[0], "partubi")) |
Heiko Schocher | ad40139 | 2013-07-25 06:43:11 +0200 | [diff] [blame] | 243 | dfu->data.nand.ubi = 1; |
Pantelis Antoniou | cf14d0d | 2013-03-14 05:32:52 +0000 | [diff] [blame] | 244 | } else { |
Masami Hiramatsu | fa4282a | 2022-01-31 11:52:37 +0900 | [diff] [blame] | 245 | printf("%s: Memory layout (%s) not supported!\n", __func__, argv[0]); |
Pantelis Antoniou | cf14d0d | 2013-03-14 05:32:52 +0000 | [diff] [blame] | 246 | return -1; |
| 247 | } |
| 248 | |
Stephen Warren | 020e6f3 | 2014-06-11 12:47:27 -0600 | [diff] [blame] | 249 | dfu->get_medium_size = dfu_get_medium_size_nand; |
Pantelis Antoniou | cf14d0d | 2013-03-14 05:32:52 +0000 | [diff] [blame] | 250 | dfu->read_medium = dfu_read_medium_nand; |
| 251 | dfu->write_medium = dfu_write_medium_nand; |
Heiko Schocher | ad40139 | 2013-07-25 06:43:11 +0200 | [diff] [blame] | 252 | dfu->flush_medium = dfu_flush_medium_nand; |
Heiko Schocher | b948810 | 2014-04-11 07:59:47 +0200 | [diff] [blame] | 253 | dfu->poll_timeout = dfu_polltimeout_nand; |
Pantelis Antoniou | cf14d0d | 2013-03-14 05:32:52 +0000 | [diff] [blame] | 254 | |
| 255 | /* initial state */ |
| 256 | dfu->inited = 0; |
| 257 | |
| 258 | return 0; |
| 259 | } |