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