blob: 0fdbfae43410b28d00775877ef6575b21981cb5d [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>
7#include <malloc.h>
8#include <errno.h>
9#include <div64.h>
10#include <dfu.h>
Simon Glassdef92152014-10-13 23:41:55 -060011#include <spi.h>
Stephen Warren0280af72014-06-11 16:03:36 -060012#include <spi_flash.h>
Patrick Delaunaybdc0f122019-10-14 09:28:00 +020013#include <jffs2/load_kernel.h>
14#include <linux/mtd/mtd.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{
Phil Edworthye01036c2017-05-24 15:09:49 +010026 return spi_flash_read(dfu->data.sf.dev, dfu->data.sf.start + offset,
27 *len, buf);
Stephen Warren0280af72014-06-11 16:03:36 -060028}
29
Fabio Estevam4556b352015-09-23 00:50:39 -030030static 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 Warren0280af72014-06-11 16:03:36 -060036static int dfu_write_medium_sf(struct dfu_entity *dfu,
Patrick Delaunayae224c32019-10-14 09:27:58 +020037 u64 offset, void *buf, long *len)
Stephen Warren0280af72014-06-11 16:03:36 -060038{
39 int ret;
40
Fabio Estevam4556b352015-09-23 00:50:39 -030041 ret = spi_flash_erase(dfu->data.sf.dev,
42 find_sector(dfu, dfu->data.sf.start, offset),
Fabio Estevameae41e92015-09-22 00:55:00 -030043 dfu->data.sf.dev->sector_size);
Stephen Warren0280af72014-06-11 16:03:36 -060044 if (ret)
45 return ret;
46
Fabio Estevam4556b352015-09-23 00:50:39 -030047 ret = spi_flash_write(dfu->data.sf.dev, dfu->data.sf.start + offset,
48 *len, buf);
Stephen Warren0280af72014-06-11 16:03:36 -060049 if (ret)
50 return ret;
51
52 return 0;
53}
54
55static int dfu_flush_medium_sf(struct dfu_entity *dfu)
56{
Patrick Delaunaybdc0f122019-10-14 09:28:00 +020057 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 Warren0280af72014-06-11 16:03:36 -060071 return 0;
72}
73
74static unsigned int dfu_polltimeout_sf(struct dfu_entity *dfu)
75{
Patrick Delaunaybdc0f122019-10-14 09:28:00 +020076 /*
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 Warren0280af72014-06-11 16:03:36 -060084 return DFU_DEFAULT_POLL_TIMEOUT;
85}
86
87static void dfu_free_entity_sf(struct dfu_entity *dfu)
88{
89 spi_flash_free(dfu->data.sf.dev);
90}
91
92static 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
141int dfu_fill_entity_sf(struct dfu_entity *dfu, char *devstr, char *s)
142{
143 char *st;
Vignesh R354e39e2015-10-20 15:22:00 +0530144 char *devstr_bkup = strdup(devstr);
Stephen Warren0280af72014-06-11 16:03:36 -0600145
Vignesh R354e39e2015-10-20 15:22:00 +0530146 dfu->data.sf.dev = parse_dev(devstr_bkup);
147 free(devstr_bkup);
Stephen Warren0280af72014-06-11 16:03:36 -0600148 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 Delaunaybdc0f122019-10-14 09:28:00 +0200160 } 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 Warren0280af72014-06-11 16:03:36 -0600188 } 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}