Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Stephen Warren | 0280af7 | 2014-06-11 16:03:36 -0600 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2014, NVIDIA CORPORATION. All rights reserved. |
Stephen Warren | 0280af7 | 2014-06-11 16:03:36 -0600 | [diff] [blame] | 4 | */ |
| 5 | |
Stephen Warren | 0280af7 | 2014-06-11 16:03:36 -0600 | [diff] [blame] | 6 | #include <malloc.h> |
| 7 | #include <errno.h> |
| 8 | #include <div64.h> |
| 9 | #include <dfu.h> |
Simon Glass | def9215 | 2014-10-13 23:41:55 -0600 | [diff] [blame] | 10 | #include <spi.h> |
Stephen Warren | 0280af7 | 2014-06-11 16:03:36 -0600 | [diff] [blame] | 11 | #include <spi_flash.h> |
Patrick Delaunay | bdc0f12 | 2019-10-14 09:28:00 +0200 | [diff] [blame] | 12 | #include <jffs2/load_kernel.h> |
| 13 | #include <linux/mtd/mtd.h> |
Masami Hiramatsu | f815c33 | 2022-01-31 11:52:29 +0900 | [diff] [blame] | 14 | #include <linux/ctype.h> |
Stephen Warren | 0280af7 | 2014-06-11 16:03:36 -0600 | [diff] [blame] | 15 | |
Patrick Delaunay | f4e934f | 2017-07-19 16:39:23 +0200 | [diff] [blame] | 16 | static int dfu_get_medium_size_sf(struct dfu_entity *dfu, u64 *size) |
Stephen Warren | 0280af7 | 2014-06-11 16:03:36 -0600 | [diff] [blame] | 17 | { |
Patrick Delaunay | 5ea9b89 | 2017-07-19 16:39:22 +0200 | [diff] [blame] | 18 | *size = dfu->data.sf.size; |
| 19 | |
| 20 | return 0; |
Stephen Warren | 0280af7 | 2014-06-11 16:03:36 -0600 | [diff] [blame] | 21 | } |
| 22 | |
| 23 | static int dfu_read_medium_sf(struct dfu_entity *dfu, u64 offset, void *buf, |
Patrick Delaunay | ae224c3 | 2019-10-14 09:27:58 +0200 | [diff] [blame] | 24 | long *len) |
Stephen Warren | 0280af7 | 2014-06-11 16:03:36 -0600 | [diff] [blame] | 25 | { |
Marek Vasut | a5fe780 | 2021-09-14 05:26:51 +0200 | [diff] [blame] | 26 | long seglen = *len; |
| 27 | int ret; |
| 28 | |
| 29 | if (seglen > (16 << 20)) |
| 30 | seglen = (16 << 20); |
| 31 | |
| 32 | ret = spi_flash_read(dfu->data.sf.dev, dfu->data.sf.start + offset, |
| 33 | seglen, buf); |
| 34 | if (!ret) |
| 35 | *len = seglen; |
| 36 | |
| 37 | return ret; |
Stephen Warren | 0280af7 | 2014-06-11 16:03:36 -0600 | [diff] [blame] | 38 | } |
| 39 | |
Fabio Estevam | 4556b35 | 2015-09-23 00:50:39 -0300 | [diff] [blame] | 40 | static u64 find_sector(struct dfu_entity *dfu, u64 start, u64 offset) |
| 41 | { |
| 42 | return (lldiv((start + offset), dfu->data.sf.dev->sector_size)) * |
| 43 | dfu->data.sf.dev->sector_size; |
| 44 | } |
| 45 | |
Stephen Warren | 0280af7 | 2014-06-11 16:03:36 -0600 | [diff] [blame] | 46 | static int dfu_write_medium_sf(struct dfu_entity *dfu, |
Patrick Delaunay | ae224c3 | 2019-10-14 09:27:58 +0200 | [diff] [blame] | 47 | u64 offset, void *buf, long *len) |
Stephen Warren | 0280af7 | 2014-06-11 16:03:36 -0600 | [diff] [blame] | 48 | { |
| 49 | int ret; |
| 50 | |
Fabio Estevam | 4556b35 | 2015-09-23 00:50:39 -0300 | [diff] [blame] | 51 | ret = spi_flash_erase(dfu->data.sf.dev, |
| 52 | find_sector(dfu, dfu->data.sf.start, offset), |
Fabio Estevam | eae41e9 | 2015-09-22 00:55:00 -0300 | [diff] [blame] | 53 | dfu->data.sf.dev->sector_size); |
Stephen Warren | 0280af7 | 2014-06-11 16:03:36 -0600 | [diff] [blame] | 54 | if (ret) |
| 55 | return ret; |
| 56 | |
Fabio Estevam | 4556b35 | 2015-09-23 00:50:39 -0300 | [diff] [blame] | 57 | ret = spi_flash_write(dfu->data.sf.dev, dfu->data.sf.start + offset, |
| 58 | *len, buf); |
Stephen Warren | 0280af7 | 2014-06-11 16:03:36 -0600 | [diff] [blame] | 59 | if (ret) |
| 60 | return ret; |
| 61 | |
| 62 | return 0; |
| 63 | } |
| 64 | |
| 65 | static int dfu_flush_medium_sf(struct dfu_entity *dfu) |
| 66 | { |
Patrick Delaunay | bdc0f12 | 2019-10-14 09:28:00 +0200 | [diff] [blame] | 67 | u64 off, length; |
| 68 | |
| 69 | if (!CONFIG_IS_ENABLED(DFU_SF_PART) || !dfu->data.sf.ubi) |
| 70 | return 0; |
| 71 | |
| 72 | /* in case of ubi partition, erase rest of the partition */ |
| 73 | off = find_sector(dfu, dfu->data.sf.start, dfu->offset); |
| 74 | /* last write ended with unaligned length jump to next */ |
| 75 | if (off != dfu->data.sf.start + dfu->offset) |
| 76 | off += dfu->data.sf.dev->sector_size; |
| 77 | length = dfu->data.sf.start + dfu->data.sf.size - off; |
| 78 | if (length) |
| 79 | return spi_flash_erase(dfu->data.sf.dev, off, length); |
| 80 | |
Stephen Warren | 0280af7 | 2014-06-11 16:03:36 -0600 | [diff] [blame] | 81 | return 0; |
| 82 | } |
| 83 | |
| 84 | static unsigned int dfu_polltimeout_sf(struct dfu_entity *dfu) |
| 85 | { |
Patrick Delaunay | bdc0f12 | 2019-10-14 09:28:00 +0200 | [diff] [blame] | 86 | /* |
| 87 | * Currently, Poll Timeout != 0 is only needed on nand |
| 88 | * ubi partition, as sectors which are not used need |
| 89 | * to be erased |
| 90 | */ |
| 91 | if (CONFIG_IS_ENABLED(DFU_SF_PART) && dfu->data.sf.ubi) |
| 92 | return DFU_MANIFEST_POLL_TIMEOUT; |
| 93 | |
Stephen Warren | 0280af7 | 2014-06-11 16:03:36 -0600 | [diff] [blame] | 94 | return DFU_DEFAULT_POLL_TIMEOUT; |
| 95 | } |
| 96 | |
| 97 | static void dfu_free_entity_sf(struct dfu_entity *dfu) |
| 98 | { |
Heinrich Schuchardt | 81f910e | 2021-03-18 07:46:07 +0100 | [diff] [blame] | 99 | /* |
| 100 | * In the DM case it is not necessary to free the SPI device. |
| 101 | * For the non-DM case we must ensure that the the SPI device is only |
| 102 | * freed once. |
| 103 | */ |
| 104 | if (!CONFIG_IS_ENABLED(DM_SPI_FLASH)) { |
| 105 | struct spi_flash *dev = dfu->data.sf.dev; |
| 106 | |
| 107 | if (!dev) |
| 108 | return; |
| 109 | dfu->data.sf.dev = NULL; |
| 110 | list_for_each_entry(dfu, &dfu_list, list) { |
| 111 | if (dfu->data.sf.dev == dev) |
| 112 | dfu->data.sf.dev = NULL; |
| 113 | } |
| 114 | spi_flash_free(dev); |
| 115 | } |
Stephen Warren | 0280af7 | 2014-06-11 16:03:36 -0600 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | static struct spi_flash *parse_dev(char *devstr) |
| 119 | { |
| 120 | unsigned int bus; |
| 121 | unsigned int cs; |
| 122 | unsigned int speed = CONFIG_SF_DEFAULT_SPEED; |
| 123 | unsigned int mode = CONFIG_SF_DEFAULT_MODE; |
| 124 | char *s, *endp; |
| 125 | struct spi_flash *dev; |
| 126 | |
| 127 | s = strsep(&devstr, ":"); |
| 128 | if (!s || !*s || (bus = simple_strtoul(s, &endp, 0), *endp)) { |
| 129 | printf("Invalid SPI bus %s\n", s); |
| 130 | return NULL; |
| 131 | } |
| 132 | |
| 133 | s = strsep(&devstr, ":"); |
| 134 | if (!s || !*s || (cs = simple_strtoul(s, &endp, 0), *endp)) { |
| 135 | printf("Invalid SPI chip-select %s\n", s); |
| 136 | return NULL; |
| 137 | } |
| 138 | |
| 139 | s = strsep(&devstr, ":"); |
| 140 | if (s && *s) { |
| 141 | speed = simple_strtoul(s, &endp, 0); |
| 142 | if (*endp || !speed) { |
| 143 | printf("Invalid SPI speed %s\n", s); |
| 144 | return NULL; |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | s = strsep(&devstr, ":"); |
| 149 | if (s && *s) { |
| 150 | mode = simple_strtoul(s, &endp, 0); |
| 151 | if (*endp || mode > 3) { |
| 152 | printf("Invalid SPI mode %s\n", s); |
| 153 | return NULL; |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | dev = spi_flash_probe(bus, cs, speed, mode); |
| 158 | if (!dev) { |
Heinrich Schuchardt | 34db4d7 | 2020-12-27 09:58:05 +0100 | [diff] [blame] | 159 | printf("Failed to create SPI flash at %u:%u:%u:%u\n", |
Stephen Warren | 0280af7 | 2014-06-11 16:03:36 -0600 | [diff] [blame] | 160 | bus, cs, speed, mode); |
| 161 | return NULL; |
| 162 | } |
| 163 | |
| 164 | return dev; |
| 165 | } |
| 166 | |
Masami Hiramatsu | fa4282a | 2022-01-31 11:52:37 +0900 | [diff] [blame] | 167 | int dfu_fill_entity_sf(struct dfu_entity *dfu, char *devstr, char **argv, int argc) |
Stephen Warren | 0280af7 | 2014-06-11 16:03:36 -0600 | [diff] [blame] | 168 | { |
Masami Hiramatsu | fa4282a | 2022-01-31 11:52:37 +0900 | [diff] [blame] | 169 | char *s; |
Vignesh R | 354e39e | 2015-10-20 15:22:00 +0530 | [diff] [blame] | 170 | char *devstr_bkup = strdup(devstr); |
Stephen Warren | 0280af7 | 2014-06-11 16:03:36 -0600 | [diff] [blame] | 171 | |
Vignesh R | 354e39e | 2015-10-20 15:22:00 +0530 | [diff] [blame] | 172 | dfu->data.sf.dev = parse_dev(devstr_bkup); |
| 173 | free(devstr_bkup); |
Stephen Warren | 0280af7 | 2014-06-11 16:03:36 -0600 | [diff] [blame] | 174 | if (!dfu->data.sf.dev) |
| 175 | return -ENODEV; |
| 176 | |
| 177 | dfu->dev_type = DFU_DEV_SF; |
| 178 | dfu->max_buf_size = dfu->data.sf.dev->sector_size; |
| 179 | |
Masami Hiramatsu | fa4282a | 2022-01-31 11:52:37 +0900 | [diff] [blame] | 180 | if (argc != 3) |
| 181 | return -EINVAL; |
| 182 | if (!strcmp(argv[0], "raw")) { |
Stephen Warren | 0280af7 | 2014-06-11 16:03:36 -0600 | [diff] [blame] | 183 | dfu->layout = DFU_RAW_ADDR; |
Masami Hiramatsu | fa4282a | 2022-01-31 11:52:37 +0900 | [diff] [blame] | 184 | dfu->data.sf.start = hextoul(argv[1], &s); |
| 185 | if (*s) |
| 186 | return -EINVAL; |
| 187 | dfu->data.sf.size = hextoul(argv[2], &s); |
| 188 | if (*s) |
| 189 | return -EINVAL; |
Patrick Delaunay | bdc0f12 | 2019-10-14 09:28:00 +0200 | [diff] [blame] | 190 | } else if (CONFIG_IS_ENABLED(DFU_SF_PART) && |
Masami Hiramatsu | fa4282a | 2022-01-31 11:52:37 +0900 | [diff] [blame] | 191 | (!strcmp(argv[0], "part") || !strcmp(argv[0], "partubi"))) { |
Patrick Delaunay | bdc0f12 | 2019-10-14 09:28:00 +0200 | [diff] [blame] | 192 | char mtd_id[32]; |
| 193 | struct mtd_device *mtd_dev; |
| 194 | u8 part_num; |
| 195 | struct part_info *pi; |
| 196 | int ret, dev, part; |
| 197 | |
| 198 | dfu->layout = DFU_RAW_ADDR; |
| 199 | |
Masami Hiramatsu | fa4282a | 2022-01-31 11:52:37 +0900 | [diff] [blame] | 200 | dev = dectoul(argv[1], &s); |
| 201 | if (*s) |
| 202 | return -EINVAL; |
| 203 | part = dectoul(argv[2], &s); |
| 204 | if (*s) |
| 205 | return -EINVAL; |
Patrick Delaunay | bdc0f12 | 2019-10-14 09:28:00 +0200 | [diff] [blame] | 206 | |
| 207 | sprintf(mtd_id, "%s%d,%d", "nor", dev, part - 1); |
| 208 | printf("using id '%s'\n", mtd_id); |
| 209 | |
| 210 | mtdparts_init(); |
| 211 | |
| 212 | ret = find_dev_and_part(mtd_id, &mtd_dev, &part_num, &pi); |
| 213 | if (ret != 0) { |
| 214 | printf("Could not locate '%s'\n", mtd_id); |
| 215 | return -1; |
| 216 | } |
| 217 | dfu->data.sf.start = pi->offset; |
| 218 | dfu->data.sf.size = pi->size; |
Masami Hiramatsu | fa4282a | 2022-01-31 11:52:37 +0900 | [diff] [blame] | 219 | if (!strcmp(argv[0], "partubi")) |
Patrick Delaunay | bdc0f12 | 2019-10-14 09:28:00 +0200 | [diff] [blame] | 220 | dfu->data.sf.ubi = 1; |
Stephen Warren | 0280af7 | 2014-06-11 16:03:36 -0600 | [diff] [blame] | 221 | } else { |
Masami Hiramatsu | fa4282a | 2022-01-31 11:52:37 +0900 | [diff] [blame] | 222 | printf("%s: Memory layout (%s) not supported!\n", __func__, argv[0]); |
Stephen Warren | 0280af7 | 2014-06-11 16:03:36 -0600 | [diff] [blame] | 223 | spi_flash_free(dfu->data.sf.dev); |
| 224 | return -1; |
| 225 | } |
| 226 | |
| 227 | dfu->get_medium_size = dfu_get_medium_size_sf; |
| 228 | dfu->read_medium = dfu_read_medium_sf; |
| 229 | dfu->write_medium = dfu_write_medium_sf; |
| 230 | dfu->flush_medium = dfu_flush_medium_sf; |
| 231 | dfu->poll_timeout = dfu_polltimeout_sf; |
| 232 | dfu->free_entity = dfu_free_entity_sf; |
| 233 | |
| 234 | /* initial state */ |
| 235 | dfu->inited = 0; |
| 236 | |
| 237 | return 0; |
| 238 | } |