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