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