blob: 2dae159370644c7b8f04468e0f6e460844029c9e [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Stephen Warren6f12ebf2014-06-11 16:03:36 -06002/*
3 * Copyright (c) 2014, NVIDIA CORPORATION. All rights reserved.
Stephen Warren6f12ebf2014-06-11 16:03:36 -06004 */
5
6#include <common.h>
7#include <malloc.h>
8#include <errno.h>
9#include <div64.h>
10#include <dfu.h>
Simon Glass843c9e82014-10-13 23:41:55 -060011#include <spi.h>
Stephen Warren6f12ebf2014-06-11 16:03:36 -060012#include <spi_flash.h>
Patrick Delaunaycb986ba2019-10-14 09:28:00 +020013#include <jffs2/load_kernel.h>
14#include <linux/mtd/mtd.h>
Masami Hiramatsu8db74c12022-01-31 11:52:29 +090015#include <linux/ctype.h>
Stephen Warren6f12ebf2014-06-11 16:03:36 -060016
Patrick Delaunay15970d82017-07-19 16:39:23 +020017static int dfu_get_medium_size_sf(struct dfu_entity *dfu, u64 *size)
Stephen Warren6f12ebf2014-06-11 16:03:36 -060018{
Patrick Delaunay4de51202017-07-19 16:39:22 +020019 *size = dfu->data.sf.size;
20
21 return 0;
Stephen Warren6f12ebf2014-06-11 16:03:36 -060022}
23
24static int dfu_read_medium_sf(struct dfu_entity *dfu, u64 offset, void *buf,
Patrick Delaunay909b6902019-10-14 09:27:58 +020025 long *len)
Stephen Warren6f12ebf2014-06-11 16:03:36 -060026{
Marek Vasutca341e92021-09-14 05:26:51 +020027 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 Warren6f12ebf2014-06-11 16:03:36 -060039}
40
Fabio Estevam2727f3b2015-09-23 00:50:39 -030041static 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 Warren6f12ebf2014-06-11 16:03:36 -060047static int dfu_write_medium_sf(struct dfu_entity *dfu,
Patrick Delaunay909b6902019-10-14 09:27:58 +020048 u64 offset, void *buf, long *len)
Stephen Warren6f12ebf2014-06-11 16:03:36 -060049{
50 int ret;
51
Fabio Estevam2727f3b2015-09-23 00:50:39 -030052 ret = spi_flash_erase(dfu->data.sf.dev,
53 find_sector(dfu, dfu->data.sf.start, offset),
Fabio Estevamf4c92582015-09-22 00:55:00 -030054 dfu->data.sf.dev->sector_size);
Stephen Warren6f12ebf2014-06-11 16:03:36 -060055 if (ret)
56 return ret;
57
Fabio Estevam2727f3b2015-09-23 00:50:39 -030058 ret = spi_flash_write(dfu->data.sf.dev, dfu->data.sf.start + offset,
59 *len, buf);
Stephen Warren6f12ebf2014-06-11 16:03:36 -060060 if (ret)
61 return ret;
62
63 return 0;
64}
65
66static int dfu_flush_medium_sf(struct dfu_entity *dfu)
67{
Patrick Delaunaycb986ba2019-10-14 09:28:00 +020068 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 Warren6f12ebf2014-06-11 16:03:36 -060082 return 0;
83}
84
85static unsigned int dfu_polltimeout_sf(struct dfu_entity *dfu)
86{
Patrick Delaunaycb986ba2019-10-14 09:28:00 +020087 /*
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 Warren6f12ebf2014-06-11 16:03:36 -060095 return DFU_DEFAULT_POLL_TIMEOUT;
96}
97
98static void dfu_free_entity_sf(struct dfu_entity *dfu)
99{
Heinrich Schuchardt57cba222021-03-18 07:46:07 +0100100 /*
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 Warren6f12ebf2014-06-11 16:03:36 -0600117}
118
119static 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 Schuchardta045bc12020-12-27 09:58:05 +0100160 printf("Failed to create SPI flash at %u:%u:%u:%u\n",
Stephen Warren6f12ebf2014-06-11 16:03:36 -0600161 bus, cs, speed, mode);
162 return NULL;
163 }
164
165 return dev;
166}
167
Masami Hiramatsu53b40632022-01-31 11:52:37 +0900168int dfu_fill_entity_sf(struct dfu_entity *dfu, char *devstr, char **argv, int argc)
Stephen Warren6f12ebf2014-06-11 16:03:36 -0600169{
Masami Hiramatsu53b40632022-01-31 11:52:37 +0900170 char *s;
Vignesh R30e3ea42015-10-20 15:22:00 +0530171 char *devstr_bkup = strdup(devstr);
Stephen Warren6f12ebf2014-06-11 16:03:36 -0600172
Vignesh R30e3ea42015-10-20 15:22:00 +0530173 dfu->data.sf.dev = parse_dev(devstr_bkup);
174 free(devstr_bkup);
Stephen Warren6f12ebf2014-06-11 16:03:36 -0600175 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
Masami Hiramatsu53b40632022-01-31 11:52:37 +0900181 if (argc != 3)
182 return -EINVAL;
183 if (!strcmp(argv[0], "raw")) {
Stephen Warren6f12ebf2014-06-11 16:03:36 -0600184 dfu->layout = DFU_RAW_ADDR;
Masami Hiramatsu53b40632022-01-31 11:52:37 +0900185 dfu->data.sf.start = hextoul(argv[1], &s);
186 if (*s)
187 return -EINVAL;
188 dfu->data.sf.size = hextoul(argv[2], &s);
189 if (*s)
190 return -EINVAL;
Patrick Delaunaycb986ba2019-10-14 09:28:00 +0200191 } else if (CONFIG_IS_ENABLED(DFU_SF_PART) &&
Masami Hiramatsu53b40632022-01-31 11:52:37 +0900192 (!strcmp(argv[0], "part") || !strcmp(argv[0], "partubi"))) {
Patrick Delaunaycb986ba2019-10-14 09:28:00 +0200193 char mtd_id[32];
194 struct mtd_device *mtd_dev;
195 u8 part_num;
196 struct part_info *pi;
197 int ret, dev, part;
198
199 dfu->layout = DFU_RAW_ADDR;
200
Masami Hiramatsu53b40632022-01-31 11:52:37 +0900201 dev = dectoul(argv[1], &s);
202 if (*s)
203 return -EINVAL;
204 part = dectoul(argv[2], &s);
205 if (*s)
206 return -EINVAL;
Patrick Delaunaycb986ba2019-10-14 09:28:00 +0200207
208 sprintf(mtd_id, "%s%d,%d", "nor", dev, part - 1);
209 printf("using id '%s'\n", mtd_id);
210
211 mtdparts_init();
212
213 ret = find_dev_and_part(mtd_id, &mtd_dev, &part_num, &pi);
214 if (ret != 0) {
215 printf("Could not locate '%s'\n", mtd_id);
216 return -1;
217 }
218 dfu->data.sf.start = pi->offset;
219 dfu->data.sf.size = pi->size;
Masami Hiramatsu53b40632022-01-31 11:52:37 +0900220 if (!strcmp(argv[0], "partubi"))
Patrick Delaunaycb986ba2019-10-14 09:28:00 +0200221 dfu->data.sf.ubi = 1;
Stephen Warren6f12ebf2014-06-11 16:03:36 -0600222 } else {
Masami Hiramatsu53b40632022-01-31 11:52:37 +0900223 printf("%s: Memory layout (%s) not supported!\n", __func__, argv[0]);
Stephen Warren6f12ebf2014-06-11 16:03:36 -0600224 spi_flash_free(dfu->data.sf.dev);
225 return -1;
226 }
227
228 dfu->get_medium_size = dfu_get_medium_size_sf;
229 dfu->read_medium = dfu_read_medium_sf;
230 dfu->write_medium = dfu_write_medium_sf;
231 dfu->flush_medium = dfu_flush_medium_sf;
232 dfu->poll_timeout = dfu_polltimeout_sf;
233 dfu->free_entity = dfu_free_entity_sf;
234
235 /* initial state */
236 dfu->inited = 0;
237
238 return 0;
239}