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