blob: b72493ced86dab8ddfc15eddbf873bee7f849da1 [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
6#include <common.h>
Simon Glass8e201882020-05-10 11:39:54 -06007#include <flash.h>
Stephen Warren0280af72014-06-11 16:03:36 -06008#include <malloc.h>
9#include <errno.h>
10#include <div64.h>
11#include <dfu.h>
Simon Glassdef92152014-10-13 23:41:55 -060012#include <spi.h>
Stephen Warren0280af72014-06-11 16:03:36 -060013#include <spi_flash.h>
Patrick Delaunaybdc0f122019-10-14 09:28:00 +020014#include <jffs2/load_kernel.h>
15#include <linux/mtd/mtd.h>
Stephen Warren0280af72014-06-11 16:03:36 -060016
Patrick Delaunayf4e934f2017-07-19 16:39:23 +020017static int dfu_get_medium_size_sf(struct dfu_entity *dfu, u64 *size)
Stephen Warren0280af72014-06-11 16:03:36 -060018{
Patrick Delaunay5ea9b892017-07-19 16:39:22 +020019 *size = dfu->data.sf.size;
20
21 return 0;
Stephen Warren0280af72014-06-11 16:03:36 -060022}
23
24static int dfu_read_medium_sf(struct dfu_entity *dfu, u64 offset, void *buf,
Patrick Delaunayae224c32019-10-14 09:27:58 +020025 long *len)
Stephen Warren0280af72014-06-11 16:03:36 -060026{
Marek Vasuta5fe7802021-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 Warren0280af72014-06-11 16:03:36 -060039}
40
Fabio Estevam4556b352015-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 Warren0280af72014-06-11 16:03:36 -060047static int dfu_write_medium_sf(struct dfu_entity *dfu,
Patrick Delaunayae224c32019-10-14 09:27:58 +020048 u64 offset, void *buf, long *len)
Stephen Warren0280af72014-06-11 16:03:36 -060049{
50 int ret;
51
Fabio Estevam4556b352015-09-23 00:50:39 -030052 ret = spi_flash_erase(dfu->data.sf.dev,
53 find_sector(dfu, dfu->data.sf.start, offset),
Fabio Estevameae41e92015-09-22 00:55:00 -030054 dfu->data.sf.dev->sector_size);
Stephen Warren0280af72014-06-11 16:03:36 -060055 if (ret)
56 return ret;
57
Fabio Estevam4556b352015-09-23 00:50:39 -030058 ret = spi_flash_write(dfu->data.sf.dev, dfu->data.sf.start + offset,
59 *len, buf);
Stephen Warren0280af72014-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 Delaunaybdc0f122019-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 Warren0280af72014-06-11 16:03:36 -060082 return 0;
83}
84
85static unsigned int dfu_polltimeout_sf(struct dfu_entity *dfu)
86{
Patrick Delaunaybdc0f122019-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 Warren0280af72014-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 Schuchardt81f910e2021-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 Warren0280af72014-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 Schuchardt34db4d72020-12-27 09:58:05 +0100160 printf("Failed to create SPI flash at %u:%u:%u:%u\n",
Stephen Warren0280af72014-06-11 16:03:36 -0600161 bus, cs, speed, mode);
162 return NULL;
163 }
164
165 return dev;
166}
167
168int dfu_fill_entity_sf(struct dfu_entity *dfu, char *devstr, char *s)
169{
170 char *st;
Vignesh R354e39e2015-10-20 15:22:00 +0530171 char *devstr_bkup = strdup(devstr);
Stephen Warren0280af72014-06-11 16:03:36 -0600172
Vignesh R354e39e2015-10-20 15:22:00 +0530173 dfu->data.sf.dev = parse_dev(devstr_bkup);
174 free(devstr_bkup);
Stephen Warren0280af72014-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
181 st = strsep(&s, " ");
182 if (!strcmp(st, "raw")) {
183 dfu->layout = DFU_RAW_ADDR;
Simon Glass3ff49ec2021-07-24 09:03:29 -0600184 dfu->data.sf.start = hextoul(s, &s);
Stephen Warren0280af72014-06-11 16:03:36 -0600185 s++;
Simon Glass3ff49ec2021-07-24 09:03:29 -0600186 dfu->data.sf.size = hextoul(s, &s);
Patrick Delaunaybdc0f122019-10-14 09:28:00 +0200187 } else if (CONFIG_IS_ENABLED(DFU_SF_PART) &&
188 (!strcmp(st, "part") || !strcmp(st, "partubi"))) {
189 char mtd_id[32];
190 struct mtd_device *mtd_dev;
191 u8 part_num;
192 struct part_info *pi;
193 int ret, dev, part;
194
195 dfu->layout = DFU_RAW_ADDR;
196
Simon Glassff9b9032021-07-24 09:03:30 -0600197 dev = dectoul(s, &s);
Patrick Delaunaybdc0f122019-10-14 09:28:00 +0200198 s++;
Simon Glassff9b9032021-07-24 09:03:30 -0600199 part = dectoul(s, &s);
Patrick Delaunaybdc0f122019-10-14 09:28:00 +0200200
201 sprintf(mtd_id, "%s%d,%d", "nor", dev, part - 1);
202 printf("using id '%s'\n", mtd_id);
203
204 mtdparts_init();
205
206 ret = find_dev_and_part(mtd_id, &mtd_dev, &part_num, &pi);
207 if (ret != 0) {
208 printf("Could not locate '%s'\n", mtd_id);
209 return -1;
210 }
211 dfu->data.sf.start = pi->offset;
212 dfu->data.sf.size = pi->size;
213 if (!strcmp(st, "partubi"))
214 dfu->data.sf.ubi = 1;
Stephen Warren0280af72014-06-11 16:03:36 -0600215 } else {
216 printf("%s: Memory layout (%s) not supported!\n", __func__, st);
217 spi_flash_free(dfu->data.sf.dev);
218 return -1;
219 }
220
221 dfu->get_medium_size = dfu_get_medium_size_sf;
222 dfu->read_medium = dfu_read_medium_sf;
223 dfu->write_medium = dfu_write_medium_sf;
224 dfu->flush_medium = dfu_flush_medium_sf;
225 dfu->poll_timeout = dfu_polltimeout_sf;
226 dfu->free_entity = dfu_free_entity_sf;
227
228 /* initial state */
229 dfu->inited = 0;
230
231 return 0;
232}