blob: 7c1c0f9e2dc7db296fb5a614503ff9d05651c061 [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;
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 Schuchardt34db4d72020-12-27 09:58:05 +0100159 printf("Failed to create SPI flash at %u:%u:%u:%u\n",
Stephen Warren0280af72014-06-11 16:03:36 -0600160 bus, cs, speed, mode);
161 return NULL;
162 }
163
164 return dev;
165}
166
Masami Hiramatsufa4282a2022-01-31 11:52:37 +0900167int dfu_fill_entity_sf(struct dfu_entity *dfu, char *devstr, char **argv, int argc)
Stephen Warren0280af72014-06-11 16:03:36 -0600168{
Masami Hiramatsufa4282a2022-01-31 11:52:37 +0900169 char *s;
Vignesh R354e39e2015-10-20 15:22:00 +0530170 char *devstr_bkup = strdup(devstr);
Stephen Warren0280af72014-06-11 16:03:36 -0600171
Vignesh R354e39e2015-10-20 15:22:00 +0530172 dfu->data.sf.dev = parse_dev(devstr_bkup);
173 free(devstr_bkup);
Stephen Warren0280af72014-06-11 16:03:36 -0600174 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 Hiramatsufa4282a2022-01-31 11:52:37 +0900180 if (argc != 3)
181 return -EINVAL;
182 if (!strcmp(argv[0], "raw")) {
Stephen Warren0280af72014-06-11 16:03:36 -0600183 dfu->layout = DFU_RAW_ADDR;
Masami Hiramatsufa4282a2022-01-31 11:52:37 +0900184 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 Delaunaybdc0f122019-10-14 09:28:00 +0200190 } else if (CONFIG_IS_ENABLED(DFU_SF_PART) &&
Masami Hiramatsufa4282a2022-01-31 11:52:37 +0900191 (!strcmp(argv[0], "part") || !strcmp(argv[0], "partubi"))) {
Patrick Delaunaybdc0f122019-10-14 09:28:00 +0200192 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 Hiramatsufa4282a2022-01-31 11:52:37 +0900200 dev = dectoul(argv[1], &s);
201 if (*s)
202 return -EINVAL;
203 part = dectoul(argv[2], &s);
204 if (*s)
205 return -EINVAL;
Patrick Delaunaybdc0f122019-10-14 09:28:00 +0200206
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 Hiramatsufa4282a2022-01-31 11:52:37 +0900219 if (!strcmp(argv[0], "partubi"))
Patrick Delaunaybdc0f122019-10-14 09:28:00 +0200220 dfu->data.sf.ubi = 1;
Stephen Warren0280af72014-06-11 16:03:36 -0600221 } else {
Masami Hiramatsufa4282a2022-01-31 11:52:37 +0900222 printf("%s: Memory layout (%s) not supported!\n", __func__, argv[0]);
Stephen Warren0280af72014-06-11 16:03:36 -0600223 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}