blob: 8f7296adec6db1ceb64af7395627cc8ec74e5d73 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Stephen Warren0280af72014-06-11 16:03:36 -06002/*
3 * Copyright (c) 2014, NVIDIA CORPORATION. All rights reserved.
Stephen Warren0280af72014-06-11 16:03:36 -06004 */
5
Stephen Warren0280af72014-06-11 16:03:36 -06006#include <malloc.h>
7#include <errno.h>
8#include <div64.h>
9#include <dfu.h>
Simon Glassdef92152014-10-13 23:41:55 -060010#include <spi.h>
Stephen Warren0280af72014-06-11 16:03:36 -060011#include <spi_flash.h>
Patrick Delaunaybdc0f122019-10-14 09:28:00 +020012#include <jffs2/load_kernel.h>
13#include <linux/mtd/mtd.h>
Masami Hiramatsuf815c332022-01-31 11:52:29 +090014#include <linux/ctype.h>
Stephen Warren0280af72014-06-11 16:03:36 -060015
Patrick Delaunayf4e934f2017-07-19 16:39:23 +020016static int dfu_get_medium_size_sf(struct dfu_entity *dfu, u64 *size)
Stephen Warren0280af72014-06-11 16:03:36 -060017{
Patrick Delaunay5ea9b892017-07-19 16:39:22 +020018 *size = dfu->data.sf.size;
19
20 return 0;
Stephen Warren0280af72014-06-11 16:03:36 -060021}
22
23static int dfu_read_medium_sf(struct dfu_entity *dfu, u64 offset, void *buf,
Patrick Delaunayae224c32019-10-14 09:27:58 +020024 long *len)
Stephen Warren0280af72014-06-11 16:03:36 -060025{
Marek Vasuta5fe7802021-09-14 05:26:51 +020026 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 Warren0280af72014-06-11 16:03:36 -060038}
39
Fabio Estevam4556b352015-09-23 00:50:39 -030040static 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 Warren0280af72014-06-11 16:03:36 -060046static int dfu_write_medium_sf(struct dfu_entity *dfu,
Patrick Delaunayae224c32019-10-14 09:27:58 +020047 u64 offset, void *buf, long *len)
Stephen Warren0280af72014-06-11 16:03:36 -060048{
49 int ret;
50
Fabio Estevam4556b352015-09-23 00:50:39 -030051 ret = spi_flash_erase(dfu->data.sf.dev,
52 find_sector(dfu, dfu->data.sf.start, offset),
Fabio Estevameae41e92015-09-22 00:55:00 -030053 dfu->data.sf.dev->sector_size);
Stephen Warren0280af72014-06-11 16:03:36 -060054 if (ret)
55 return ret;
56
Fabio Estevam4556b352015-09-23 00:50:39 -030057 ret = spi_flash_write(dfu->data.sf.dev, dfu->data.sf.start + offset,
58 *len, buf);
Stephen Warren0280af72014-06-11 16:03:36 -060059 if (ret)
60 return ret;
61
62 return 0;
63}
64
65static int dfu_flush_medium_sf(struct dfu_entity *dfu)
66{
Patrick Delaunaybdc0f122019-10-14 09:28:00 +020067 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 Warren0280af72014-06-11 16:03:36 -060081 return 0;
82}
83
84static unsigned int dfu_polltimeout_sf(struct dfu_entity *dfu)
85{
Patrick Delaunaybdc0f122019-10-14 09:28:00 +020086 /*
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 Warren0280af72014-06-11 16:03:36 -060094 return DFU_DEFAULT_POLL_TIMEOUT;
95}
96
97static void dfu_free_entity_sf(struct dfu_entity *dfu)
98{
Heinrich Schuchardt81f910e2021-03-18 07:46:07 +010099 /*
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 Warren0280af72014-06-11 16:03:36 -0600116}
117
118static 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;
Neil Armstrong2e3e0c92024-10-01 18:06:12 +0200126 bool use_dt = true;
Stephen Warren0280af72014-06-11 16:03:36 -0600127
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 }
Neil Armstrong2e3e0c92024-10-01 18:06:12 +0200147 if (IS_ENABLED(CONFIG_DM_SPI_FLASH))
148 use_dt = false;
Stephen Warren0280af72014-06-11 16:03:36 -0600149 }
150
151 s = strsep(&devstr, ":");
152 if (s && *s) {
153 mode = simple_strtoul(s, &endp, 0);
154 if (*endp || mode > 3) {
155 printf("Invalid SPI mode %s\n", s);
156 return NULL;
157 }
Neil Armstrong2e3e0c92024-10-01 18:06:12 +0200158 if (IS_ENABLED(CONFIG_DM_SPI_FLASH))
159 use_dt = false;
Stephen Warren0280af72014-06-11 16:03:36 -0600160 }
161
Neil Armstrong2e3e0c92024-10-01 18:06:12 +0200162 if (IS_ENABLED(CONFIG_DM_SPI_FLASH) && use_dt) {
163 struct udevice *new;
164
165 if (!spi_flash_probe_bus_cs(bus, cs, &new))
166 dev = dev_get_uclass_priv(new);
167 else
168 dev = NULL;
169 } else {
170 dev = spi_flash_probe(bus, cs, speed, mode);
171 }
Stephen Warren0280af72014-06-11 16:03:36 -0600172 if (!dev) {
Heinrich Schuchardt34db4d72020-12-27 09:58:05 +0100173 printf("Failed to create SPI flash at %u:%u:%u:%u\n",
Stephen Warren0280af72014-06-11 16:03:36 -0600174 bus, cs, speed, mode);
175 return NULL;
176 }
177
178 return dev;
179}
180
Masami Hiramatsufa4282a2022-01-31 11:52:37 +0900181int dfu_fill_entity_sf(struct dfu_entity *dfu, char *devstr, char **argv, int argc)
Stephen Warren0280af72014-06-11 16:03:36 -0600182{
Masami Hiramatsufa4282a2022-01-31 11:52:37 +0900183 char *s;
Vignesh R354e39e2015-10-20 15:22:00 +0530184 char *devstr_bkup = strdup(devstr);
Stephen Warren0280af72014-06-11 16:03:36 -0600185
Vignesh R354e39e2015-10-20 15:22:00 +0530186 dfu->data.sf.dev = parse_dev(devstr_bkup);
187 free(devstr_bkup);
Stephen Warren0280af72014-06-11 16:03:36 -0600188 if (!dfu->data.sf.dev)
189 return -ENODEV;
190
191 dfu->dev_type = DFU_DEV_SF;
192 dfu->max_buf_size = dfu->data.sf.dev->sector_size;
193
Masami Hiramatsufa4282a2022-01-31 11:52:37 +0900194 if (argc != 3)
195 return -EINVAL;
196 if (!strcmp(argv[0], "raw")) {
Stephen Warren0280af72014-06-11 16:03:36 -0600197 dfu->layout = DFU_RAW_ADDR;
Masami Hiramatsufa4282a2022-01-31 11:52:37 +0900198 dfu->data.sf.start = hextoul(argv[1], &s);
199 if (*s)
200 return -EINVAL;
201 dfu->data.sf.size = hextoul(argv[2], &s);
202 if (*s)
203 return -EINVAL;
Patrick Delaunaybdc0f122019-10-14 09:28:00 +0200204 } else if (CONFIG_IS_ENABLED(DFU_SF_PART) &&
Masami Hiramatsufa4282a2022-01-31 11:52:37 +0900205 (!strcmp(argv[0], "part") || !strcmp(argv[0], "partubi"))) {
Patrick Delaunaybdc0f122019-10-14 09:28:00 +0200206 char mtd_id[32];
207 struct mtd_device *mtd_dev;
208 u8 part_num;
209 struct part_info *pi;
210 int ret, dev, part;
211
212 dfu->layout = DFU_RAW_ADDR;
213
Masami Hiramatsufa4282a2022-01-31 11:52:37 +0900214 dev = dectoul(argv[1], &s);
215 if (*s)
216 return -EINVAL;
217 part = dectoul(argv[2], &s);
218 if (*s)
219 return -EINVAL;
Patrick Delaunaybdc0f122019-10-14 09:28:00 +0200220
221 sprintf(mtd_id, "%s%d,%d", "nor", dev, part - 1);
222 printf("using id '%s'\n", mtd_id);
223
224 mtdparts_init();
225
226 ret = find_dev_and_part(mtd_id, &mtd_dev, &part_num, &pi);
227 if (ret != 0) {
228 printf("Could not locate '%s'\n", mtd_id);
229 return -1;
230 }
231 dfu->data.sf.start = pi->offset;
232 dfu->data.sf.size = pi->size;
Masami Hiramatsufa4282a2022-01-31 11:52:37 +0900233 if (!strcmp(argv[0], "partubi"))
Patrick Delaunaybdc0f122019-10-14 09:28:00 +0200234 dfu->data.sf.ubi = 1;
Stephen Warren0280af72014-06-11 16:03:36 -0600235 } else {
Masami Hiramatsufa4282a2022-01-31 11:52:37 +0900236 printf("%s: Memory layout (%s) not supported!\n", __func__, argv[0]);
Stephen Warren0280af72014-06-11 16:03:36 -0600237 spi_flash_free(dfu->data.sf.dev);
238 return -1;
239 }
240
241 dfu->get_medium_size = dfu_get_medium_size_sf;
242 dfu->read_medium = dfu_read_medium_sf;
243 dfu->write_medium = dfu_write_medium_sf;
244 dfu->flush_medium = dfu_flush_medium_sf;
245 dfu->poll_timeout = dfu_polltimeout_sf;
246 dfu->free_entity = dfu_free_entity_sf;
247
248 /* initial state */
249 dfu->inited = 0;
250
251 return 0;
252}