blob: 1b6c0282513171342799d50d0794cf7c70b60c2b [file] [log] [blame]
Mike Frysingerb375ad92013-12-03 16:43:27 -07001/*
2 * Simulate a SPI flash
3 *
4 * Copyright (c) 2011-2013 The Chromium OS Authors.
5 * See file CREDITS for list of people who contributed to this
6 * project.
7 *
8 * Licensed under the GPL-2 or later.
9 */
10
11#include <common.h>
Simon Glass01862382014-10-13 23:42:08 -060012#include <dm.h>
Mike Frysingerb375ad92013-12-03 16:43:27 -070013#include <malloc.h>
14#include <spi.h>
15#include <os.h>
16
17#include <spi_flash.h>
18#include "sf_internal.h"
19
20#include <asm/getopt.h>
21#include <asm/spi.h>
22#include <asm/state.h>
Simon Glass01862382014-10-13 23:42:08 -060023#include <dm/device-internal.h>
24#include <dm/lists.h>
25#include <dm/uclass-internal.h>
26
Mike Frysingerb375ad92013-12-03 16:43:27 -070027/*
28 * The different states that our SPI flash transitions between.
29 * We need to keep track of this across multiple xfer calls since
30 * the SPI bus could possibly call down into us multiple times.
31 */
32enum sandbox_sf_state {
33 SF_CMD, /* default state -- we're awaiting a command */
34 SF_ID, /* read the flash's (jedec) ID code */
35 SF_ADDR, /* processing the offset in the flash to read/etc... */
36 SF_READ, /* reading data from the flash */
37 SF_WRITE, /* writing data to the flash, i.e. page programming */
38 SF_ERASE, /* erase the flash */
39 SF_READ_STATUS, /* read the flash's status register */
40 SF_READ_STATUS1, /* read the flash's status register upper 8 bits*/
Simon Glass01862382014-10-13 23:42:08 -060041 SF_WRITE_STATUS, /* write the flash's status register */
Mike Frysingerb375ad92013-12-03 16:43:27 -070042};
43
44static const char *sandbox_sf_state_name(enum sandbox_sf_state state)
45{
46 static const char * const states[] = {
47 "CMD", "ID", "ADDR", "READ", "WRITE", "ERASE", "READ_STATUS",
Simon Glass01862382014-10-13 23:42:08 -060048 "READ_STATUS1", "WRITE_STATUS",
Mike Frysingerb375ad92013-12-03 16:43:27 -070049 };
50 return states[state];
51}
52
53/* Bits for the status register */
54#define STAT_WIP (1 << 0)
55#define STAT_WEL (1 << 1)
56
57/* Assume all SPI flashes have 3 byte addresses since they do atm */
58#define SF_ADDR_LEN 3
59
Simon Glass5d8f0632014-09-15 06:33:19 -060060#define IDCODE_LEN 3
Mike Frysingerb375ad92013-12-03 16:43:27 -070061
62/* Used to quickly bulk erase backing store */
63static u8 sandbox_sf_0xff[0x1000];
64
65/* Internal state data for each SPI flash */
66struct sandbox_spi_flash {
Simon Glass01862382014-10-13 23:42:08 -060067 unsigned int cs; /* Chip select we are attached to */
Mike Frysingerb375ad92013-12-03 16:43:27 -070068 /*
69 * As we receive data over the SPI bus, our flash transitions
70 * between states. For example, we start off in the SF_CMD
71 * state where the first byte tells us what operation to perform
72 * (such as read or write the flash). But the operation itself
73 * can go through a few states such as first reading in the
74 * offset in the flash to perform the requested operation.
75 * Thus "state" stores the exact state that our machine is in
76 * while "cmd" stores the overall command we're processing.
77 */
78 enum sandbox_sf_state state;
79 uint cmd;
Simon Glass5d8f0632014-09-15 06:33:19 -060080 /* Erase size of current erase command */
81 uint erase_size;
Mike Frysingerb375ad92013-12-03 16:43:27 -070082 /* Current position in the flash; used when reading/writing/etc... */
83 uint off;
84 /* How many address bytes we've consumed */
85 uint addr_bytes, pad_addr_bytes;
86 /* The current flash status (see STAT_XXX defines above) */
87 u16 status;
88 /* Data describing the flash we're emulating */
Jagan Teki77ae47b2016-10-30 23:16:10 +053089 const struct spi_flash_info *data;
Mike Frysingerb375ad92013-12-03 16:43:27 -070090 /* The file on disk to serv up data from */
91 int fd;
92};
93
Simon Glass01862382014-10-13 23:42:08 -060094struct sandbox_spi_flash_plat_data {
95 const char *filename;
96 const char *device_name;
97 int bus;
98 int cs;
99};
100
101/**
102 * This is a very strange probe function. If it has platform data (which may
103 * have come from the device tree) then this function gets the filename and
104 * device type from there. Failing that it looks at the command line
105 * parameter.
106 */
107static int sandbox_sf_probe(struct udevice *dev)
Mike Frysingerb375ad92013-12-03 16:43:27 -0700108{
109 /* spec = idcode:file */
Simon Glass01862382014-10-13 23:42:08 -0600110 struct sandbox_spi_flash *sbsf = dev_get_priv(dev);
Mike Frysingerb375ad92013-12-03 16:43:27 -0700111 const char *file;
Simon Glass5d8f0632014-09-15 06:33:19 -0600112 size_t len, idname_len;
Jagan Teki77ae47b2016-10-30 23:16:10 +0530113 const struct spi_flash_info *data;
Simon Glass01862382014-10-13 23:42:08 -0600114 struct sandbox_spi_flash_plat_data *pdata = dev_get_platdata(dev);
115 struct sandbox_state *state = state_get_current();
116 struct udevice *bus = dev->parent;
117 const char *spec = NULL;
118 int ret = 0;
119 int cs = -1;
120 int i;
Mike Frysingerb375ad92013-12-03 16:43:27 -0700121
Simon Glass01862382014-10-13 23:42:08 -0600122 debug("%s: bus %d, looking for emul=%p: ", __func__, bus->seq, dev);
123 if (bus->seq >= 0 && bus->seq < CONFIG_SANDBOX_SPI_MAX_BUS) {
124 for (i = 0; i < CONFIG_SANDBOX_SPI_MAX_CS; i++) {
125 if (state->spi[bus->seq][i].emul == dev)
126 cs = i;
127 }
128 }
129 if (cs == -1) {
Simon Glassbb213e32015-05-04 11:31:10 -0600130 printf("Error: Unknown chip select for device '%s'\n",
Simon Glass01862382014-10-13 23:42:08 -0600131 dev->name);
132 return -EINVAL;
133 }
134 debug("found at cs %d\n", cs);
135
136 if (!pdata->filename) {
137 struct sandbox_state *state = state_get_current();
138
139 assert(bus->seq != -1);
140 if (bus->seq < CONFIG_SANDBOX_SPI_MAX_BUS)
141 spec = state->spi[bus->seq][cs].spec;
Simon Glassef3493d2015-01-25 08:27:09 -0700142 if (!spec) {
Simon Glassdb45d9e2016-02-24 09:14:54 -0700143 debug("%s: No spec found for bus %d, cs %d\n",
144 __func__, bus->seq, cs);
Simon Glassef3493d2015-01-25 08:27:09 -0700145 ret = -ENOENT;
146 goto error;
147 }
Simon Glass01862382014-10-13 23:42:08 -0600148
149 file = strchr(spec, ':');
150 if (!file) {
Simon Glassdb45d9e2016-02-24 09:14:54 -0700151 printf("%s: unable to parse file\n", __func__);
Simon Glass01862382014-10-13 23:42:08 -0600152 ret = -EINVAL;
153 goto error;
154 }
155 idname_len = file - spec;
156 pdata->filename = file + 1;
157 pdata->device_name = spec;
158 ++file;
159 } else {
160 spec = strchr(pdata->device_name, ',');
161 if (spec)
162 spec++;
163 else
164 spec = pdata->device_name;
165 idname_len = strlen(spec);
Mike Frysingerb375ad92013-12-03 16:43:27 -0700166 }
Simon Glass01862382014-10-13 23:42:08 -0600167 debug("%s: device='%s'\n", __func__, spec);
Mike Frysingerb375ad92013-12-03 16:43:27 -0700168
Jagan Teki77ae47b2016-10-30 23:16:10 +0530169 for (data = spi_flash_ids; data->name; data++) {
Mike Frysingerb375ad92013-12-03 16:43:27 -0700170 len = strlen(data->name);
171 if (idname_len != len)
172 continue;
Simon Glass01862382014-10-13 23:42:08 -0600173 if (!strncasecmp(spec, data->name, len))
Mike Frysingerb375ad92013-12-03 16:43:27 -0700174 break;
175 }
Simon Glass5d8f0632014-09-15 06:33:19 -0600176 if (!data->name) {
Simon Glassdb45d9e2016-02-24 09:14:54 -0700177 printf("%s: unknown flash '%*s'\n", __func__, (int)idname_len,
Mike Frysingerb375ad92013-12-03 16:43:27 -0700178 spec);
Simon Glass01862382014-10-13 23:42:08 -0600179 ret = -EINVAL;
Mike Frysingerb375ad92013-12-03 16:43:27 -0700180 goto error;
181 }
182
183 if (sandbox_sf_0xff[0] == 0x00)
184 memset(sandbox_sf_0xff, 0xff, sizeof(sandbox_sf_0xff));
185
Simon Glass01862382014-10-13 23:42:08 -0600186 sbsf->fd = os_open(pdata->filename, 02);
Mike Frysingerb375ad92013-12-03 16:43:27 -0700187 if (sbsf->fd == -1) {
Simon Glassdb45d9e2016-02-24 09:14:54 -0700188 printf("%s: unable to open file '%s'\n", __func__,
Simon Glass01862382014-10-13 23:42:08 -0600189 pdata->filename);
190 ret = -EIO;
Mike Frysingerb375ad92013-12-03 16:43:27 -0700191 goto error;
192 }
193
194 sbsf->data = data;
Simon Glass01862382014-10-13 23:42:08 -0600195 sbsf->cs = cs;
Mike Frysingerb375ad92013-12-03 16:43:27 -0700196
Mike Frysingerb375ad92013-12-03 16:43:27 -0700197 return 0;
198
199 error:
Simon Glassef3493d2015-01-25 08:27:09 -0700200 debug("%s: Got error %d\n", __func__, ret);
Simon Glass01862382014-10-13 23:42:08 -0600201 return ret;
Mike Frysingerb375ad92013-12-03 16:43:27 -0700202}
203
Simon Glass01862382014-10-13 23:42:08 -0600204static int sandbox_sf_remove(struct udevice *dev)
Mike Frysingerb375ad92013-12-03 16:43:27 -0700205{
Simon Glass01862382014-10-13 23:42:08 -0600206 struct sandbox_spi_flash *sbsf = dev_get_priv(dev);
Mike Frysingerb375ad92013-12-03 16:43:27 -0700207
208 os_close(sbsf->fd);
Simon Glass01862382014-10-13 23:42:08 -0600209
210 return 0;
Mike Frysingerb375ad92013-12-03 16:43:27 -0700211}
212
Simon Glass01862382014-10-13 23:42:08 -0600213static void sandbox_sf_cs_activate(struct udevice *dev)
Mike Frysingerb375ad92013-12-03 16:43:27 -0700214{
Simon Glass01862382014-10-13 23:42:08 -0600215 struct sandbox_spi_flash *sbsf = dev_get_priv(dev);
Mike Frysingerb375ad92013-12-03 16:43:27 -0700216
217 debug("sandbox_sf: CS activated; state is fresh!\n");
218
219 /* CS is asserted, so reset state */
220 sbsf->off = 0;
221 sbsf->addr_bytes = 0;
222 sbsf->pad_addr_bytes = 0;
223 sbsf->state = SF_CMD;
224 sbsf->cmd = SF_CMD;
225}
226
Simon Glass01862382014-10-13 23:42:08 -0600227static void sandbox_sf_cs_deactivate(struct udevice *dev)
Mike Frysingerb375ad92013-12-03 16:43:27 -0700228{
229 debug("sandbox_sf: CS deactivated; cmd done processing!\n");
230}
231
Simon Glass01862382014-10-13 23:42:08 -0600232/*
233 * There are times when the data lines are allowed to tristate. What
234 * is actually sensed on the line depends on the hardware. It could
235 * always be 0xFF/0x00 (if there are pull ups/downs), or things could
236 * float and so we'd get garbage back. This func encapsulates that
237 * scenario so we can worry about the details here.
238 */
239static void sandbox_spi_tristate(u8 *buf, uint len)
240{
241 /* XXX: make this into a user config option ? */
242 memset(buf, 0xff, len);
243}
244
Mike Frysingerb375ad92013-12-03 16:43:27 -0700245/* Figure out what command this stream is telling us to do */
246static int sandbox_sf_process_cmd(struct sandbox_spi_flash *sbsf, const u8 *rx,
247 u8 *tx)
248{
249 enum sandbox_sf_state oldstate = sbsf->state;
250
251 /* We need to output a byte for the cmd byte we just ate */
Simon Glass01862382014-10-13 23:42:08 -0600252 if (tx)
253 sandbox_spi_tristate(tx, 1);
Mike Frysingerb375ad92013-12-03 16:43:27 -0700254
255 sbsf->cmd = rx[0];
256 switch (sbsf->cmd) {
257 case CMD_READ_ID:
258 sbsf->state = SF_ID;
259 sbsf->cmd = SF_ID;
260 break;
261 case CMD_READ_ARRAY_FAST:
262 sbsf->pad_addr_bytes = 1;
263 case CMD_READ_ARRAY_SLOW:
264 case CMD_PAGE_PROGRAM:
Mike Frysingerb375ad92013-12-03 16:43:27 -0700265 sbsf->state = SF_ADDR;
266 break;
267 case CMD_WRITE_DISABLE:
268 debug(" write disabled\n");
269 sbsf->status &= ~STAT_WEL;
270 break;
271 case CMD_READ_STATUS:
272 sbsf->state = SF_READ_STATUS;
273 break;
274 case CMD_READ_STATUS1:
275 sbsf->state = SF_READ_STATUS1;
276 break;
277 case CMD_WRITE_ENABLE:
278 debug(" write enabled\n");
279 sbsf->status |= STAT_WEL;
280 break;
Simon Glass01862382014-10-13 23:42:08 -0600281 case CMD_WRITE_STATUS:
282 sbsf->state = SF_WRITE_STATUS;
283 break;
Mike Frysingerb375ad92013-12-03 16:43:27 -0700284 default: {
Simon Glass5d8f0632014-09-15 06:33:19 -0600285 int flags = sbsf->data->flags;
Mike Frysingerb375ad92013-12-03 16:43:27 -0700286
Simon Glass5d8f0632014-09-15 06:33:19 -0600287 /* we only support erase here */
288 if (sbsf->cmd == CMD_ERASE_CHIP) {
289 sbsf->erase_size = sbsf->data->sector_size *
Jagan Teki49e65792016-10-30 23:16:15 +0530290 sbsf->data->n_sectors;
Simon Glass5d8f0632014-09-15 06:33:19 -0600291 } else if (sbsf->cmd == CMD_ERASE_4K && (flags & SECT_4K)) {
292 sbsf->erase_size = 4 << 10;
Jagan Tekifbf356b2016-08-08 17:19:08 +0530293 } else if (sbsf->cmd == CMD_ERASE_64K && !(flags & SECT_4K)) {
Simon Glass5d8f0632014-09-15 06:33:19 -0600294 sbsf->erase_size = 64 << 10;
295 } else {
296 debug(" cmd unknown: %#x\n", sbsf->cmd);
Simon Glass01862382014-10-13 23:42:08 -0600297 return -EIO;
Mike Frysingerb375ad92013-12-03 16:43:27 -0700298 }
Simon Glass5d8f0632014-09-15 06:33:19 -0600299 sbsf->state = SF_ADDR;
300 break;
Mike Frysingerb375ad92013-12-03 16:43:27 -0700301 }
302 }
303
304 if (oldstate != sbsf->state)
305 debug(" cmd: transition to %s state\n",
306 sandbox_sf_state_name(sbsf->state));
307
308 return 0;
309}
310
311int sandbox_erase_part(struct sandbox_spi_flash *sbsf, int size)
312{
313 int todo;
314 int ret;
315
316 while (size > 0) {
Masahiro Yamadadb204642014-11-07 03:03:31 +0900317 todo = min(size, (int)sizeof(sandbox_sf_0xff));
Mike Frysingerb375ad92013-12-03 16:43:27 -0700318 ret = os_write(sbsf->fd, sandbox_sf_0xff, todo);
319 if (ret != todo)
320 return ret;
321 size -= todo;
322 }
323
324 return 0;
325}
326
Simon Glass01862382014-10-13 23:42:08 -0600327static int sandbox_sf_xfer(struct udevice *dev, unsigned int bitlen,
328 const void *rxp, void *txp, unsigned long flags)
Mike Frysingerb375ad92013-12-03 16:43:27 -0700329{
Simon Glass01862382014-10-13 23:42:08 -0600330 struct sandbox_spi_flash *sbsf = dev_get_priv(dev);
331 const uint8_t *rx = rxp;
332 uint8_t *tx = txp;
Mike Frysingerb375ad92013-12-03 16:43:27 -0700333 uint cnt, pos = 0;
Simon Glass01862382014-10-13 23:42:08 -0600334 int bytes = bitlen / 8;
Mike Frysingerb375ad92013-12-03 16:43:27 -0700335 int ret;
336
337 debug("sandbox_sf: state:%x(%s) bytes:%u\n", sbsf->state,
338 sandbox_sf_state_name(sbsf->state), bytes);
339
Simon Glass01862382014-10-13 23:42:08 -0600340 if ((flags & SPI_XFER_BEGIN))
341 sandbox_sf_cs_activate(dev);
342
Mike Frysingerb375ad92013-12-03 16:43:27 -0700343 if (sbsf->state == SF_CMD) {
344 /* Figure out the initial state */
Simon Glass01862382014-10-13 23:42:08 -0600345 ret = sandbox_sf_process_cmd(sbsf, rx, tx);
346 if (ret)
347 return ret;
Mike Frysingerb375ad92013-12-03 16:43:27 -0700348 ++pos;
349 }
350
351 /* Process the remaining data */
352 while (pos < bytes) {
353 switch (sbsf->state) {
354 case SF_ID: {
355 u8 id;
356
357 debug(" id: off:%u tx:", sbsf->off);
Simon Glass5d8f0632014-09-15 06:33:19 -0600358 if (sbsf->off < IDCODE_LEN) {
359 /* Extract correct byte from ID 0x00aabbcc */
Jagan Tekib980b692016-10-30 23:16:12 +0530360 id = ((JEDEC_MFR(sbsf->data) << 16) |
361 JEDEC_ID(sbsf->data)) >>
Simon Glass5d8f0632014-09-15 06:33:19 -0600362 (8 * (IDCODE_LEN - 1 - sbsf->off));
363 } else {
Mike Frysingerb375ad92013-12-03 16:43:27 -0700364 id = 0;
Simon Glass5d8f0632014-09-15 06:33:19 -0600365 }
366 debug("%d %02x\n", sbsf->off, id);
Mike Frysingerb375ad92013-12-03 16:43:27 -0700367 tx[pos++] = id;
368 ++sbsf->off;
369 break;
370 }
371 case SF_ADDR:
372 debug(" addr: bytes:%u rx:%02x ", sbsf->addr_bytes,
373 rx[pos]);
374
375 if (sbsf->addr_bytes++ < SF_ADDR_LEN)
376 sbsf->off = (sbsf->off << 8) | rx[pos];
377 debug("addr:%06x\n", sbsf->off);
378
Simon Glass01862382014-10-13 23:42:08 -0600379 if (tx)
380 sandbox_spi_tristate(&tx[pos], 1);
381 pos++;
Mike Frysingerb375ad92013-12-03 16:43:27 -0700382
383 /* See if we're done processing */
384 if (sbsf->addr_bytes <
385 SF_ADDR_LEN + sbsf->pad_addr_bytes)
386 break;
387
388 /* Next state! */
389 if (os_lseek(sbsf->fd, sbsf->off, OS_SEEK_SET) < 0) {
390 puts("sandbox_sf: os_lseek() failed");
Simon Glass01862382014-10-13 23:42:08 -0600391 return -EIO;
Mike Frysingerb375ad92013-12-03 16:43:27 -0700392 }
393 switch (sbsf->cmd) {
394 case CMD_READ_ARRAY_FAST:
395 case CMD_READ_ARRAY_SLOW:
396 sbsf->state = SF_READ;
397 break;
398 case CMD_PAGE_PROGRAM:
399 sbsf->state = SF_WRITE;
400 break;
401 default:
402 /* assume erase state ... */
403 sbsf->state = SF_ERASE;
404 goto case_sf_erase;
405 }
406 debug(" cmd: transition to %s state\n",
407 sandbox_sf_state_name(sbsf->state));
408 break;
409 case SF_READ:
410 /*
411 * XXX: need to handle exotic behavior:
412 * - reading past end of device
413 */
414
415 cnt = bytes - pos;
416 debug(" tx: read(%u)\n", cnt);
Simon Glass01862382014-10-13 23:42:08 -0600417 assert(tx);
Mike Frysingerb375ad92013-12-03 16:43:27 -0700418 ret = os_read(sbsf->fd, tx + pos, cnt);
419 if (ret < 0) {
Simon Glass01862382014-10-13 23:42:08 -0600420 puts("sandbox_sf: os_read() failed\n");
421 return -EIO;
Mike Frysingerb375ad92013-12-03 16:43:27 -0700422 }
423 pos += ret;
424 break;
425 case SF_READ_STATUS:
426 debug(" read status: %#x\n", sbsf->status);
427 cnt = bytes - pos;
428 memset(tx + pos, sbsf->status, cnt);
429 pos += cnt;
430 break;
431 case SF_READ_STATUS1:
432 debug(" read status: %#x\n", sbsf->status);
433 cnt = bytes - pos;
434 memset(tx + pos, sbsf->status >> 8, cnt);
435 pos += cnt;
436 break;
Simon Glass01862382014-10-13 23:42:08 -0600437 case SF_WRITE_STATUS:
438 debug(" write status: %#x (ignored)\n", rx[pos]);
439 pos = bytes;
440 break;
Mike Frysingerb375ad92013-12-03 16:43:27 -0700441 case SF_WRITE:
442 /*
443 * XXX: need to handle exotic behavior:
444 * - unaligned addresses
445 * - more than a page (256) worth of data
446 * - reading past end of device
447 */
448 if (!(sbsf->status & STAT_WEL)) {
449 puts("sandbox_sf: write enable not set before write\n");
450 goto done;
451 }
452
453 cnt = bytes - pos;
454 debug(" rx: write(%u)\n", cnt);
Simon Glass01862382014-10-13 23:42:08 -0600455 if (tx)
456 sandbox_spi_tristate(&tx[pos], cnt);
Mike Frysingerb375ad92013-12-03 16:43:27 -0700457 ret = os_write(sbsf->fd, rx + pos, cnt);
458 if (ret < 0) {
459 puts("sandbox_spi: os_write() failed\n");
Simon Glass01862382014-10-13 23:42:08 -0600460 return -EIO;
Mike Frysingerb375ad92013-12-03 16:43:27 -0700461 }
462 pos += ret;
463 sbsf->status &= ~STAT_WEL;
464 break;
465 case SF_ERASE:
466 case_sf_erase: {
Mike Frysingerb375ad92013-12-03 16:43:27 -0700467 if (!(sbsf->status & STAT_WEL)) {
468 puts("sandbox_sf: write enable not set before erase\n");
469 goto done;
470 }
471
472 /* verify address is aligned */
Simon Glass5d8f0632014-09-15 06:33:19 -0600473 if (sbsf->off & (sbsf->erase_size - 1)) {
Mike Frysingerb375ad92013-12-03 16:43:27 -0700474 debug(" sector erase: cmd:%#x needs align:%#x, but we got %#x\n",
Simon Glass5d8f0632014-09-15 06:33:19 -0600475 sbsf->cmd, sbsf->erase_size,
Mike Frysingerb375ad92013-12-03 16:43:27 -0700476 sbsf->off);
477 sbsf->status &= ~STAT_WEL;
478 goto done;
479 }
480
Simon Glass5d8f0632014-09-15 06:33:19 -0600481 debug(" sector erase addr: %u, size: %u\n", sbsf->off,
482 sbsf->erase_size);
Mike Frysingerb375ad92013-12-03 16:43:27 -0700483
484 cnt = bytes - pos;
Simon Glass01862382014-10-13 23:42:08 -0600485 if (tx)
486 sandbox_spi_tristate(&tx[pos], cnt);
Mike Frysingerb375ad92013-12-03 16:43:27 -0700487 pos += cnt;
488
489 /*
490 * TODO(vapier@gentoo.org): latch WIP in status, and
491 * delay before clearing it ?
492 */
Simon Glass5d8f0632014-09-15 06:33:19 -0600493 ret = sandbox_erase_part(sbsf, sbsf->erase_size);
Mike Frysingerb375ad92013-12-03 16:43:27 -0700494 sbsf->status &= ~STAT_WEL;
495 if (ret) {
496 debug("sandbox_sf: Erase failed\n");
497 goto done;
498 }
499 goto done;
500 }
501 default:
502 debug(" ??? no idea what to do ???\n");
503 goto done;
504 }
505 }
506
507 done:
Simon Glass01862382014-10-13 23:42:08 -0600508 if (flags & SPI_XFER_END)
509 sandbox_sf_cs_deactivate(dev);
510 return pos == bytes ? 0 : -EIO;
511}
512
513int sandbox_sf_ofdata_to_platdata(struct udevice *dev)
514{
515 struct sandbox_spi_flash_plat_data *pdata = dev_get_platdata(dev);
Simon Glass01862382014-10-13 23:42:08 -0600516
Simon Glass11aa2af2017-05-18 20:09:58 -0600517 pdata->filename = dev_read_string(dev, "sandbox,filename");
518 pdata->device_name = dev_read_string(dev, "compatible");
Simon Glass01862382014-10-13 23:42:08 -0600519 if (!pdata->filename || !pdata->device_name) {
520 debug("%s: Missing properties, filename=%s, device_name=%s\n",
521 __func__, pdata->filename, pdata->device_name);
522 return -EINVAL;
523 }
524
525 return 0;
Mike Frysingerb375ad92013-12-03 16:43:27 -0700526}
527
Simon Glass01862382014-10-13 23:42:08 -0600528static const struct dm_spi_emul_ops sandbox_sf_emul_ops = {
Mike Frysingerb375ad92013-12-03 16:43:27 -0700529 .xfer = sandbox_sf_xfer,
530};
531
Simon Glass01862382014-10-13 23:42:08 -0600532#ifdef CONFIG_SPI_FLASH
Mike Frysingerb375ad92013-12-03 16:43:27 -0700533static int sandbox_cmdline_cb_spi_sf(struct sandbox_state *state,
534 const char *arg)
535{
536 unsigned long bus, cs;
537 const char *spec = sandbox_spi_parse_spec(arg, &bus, &cs);
538
539 if (!spec)
540 return 1;
541
542 /*
543 * It is safe to not make a copy of 'spec' because it comes from the
544 * command line.
545 *
546 * TODO(sjg@chromium.org): It would be nice if we could parse the
547 * spec here, but the problem is that no U-Boot init has been done
548 * yet. Perhaps we can figure something out.
549 */
Mike Frysingerb375ad92013-12-03 16:43:27 -0700550 state->spi[bus][cs].spec = spec;
Simon Glassdb45d9e2016-02-24 09:14:54 -0700551 debug("%s: Setting up spec '%s' for bus %ld, cs %ld\n", __func__,
552 spec, bus, cs);
553
Mike Frysingerb375ad92013-12-03 16:43:27 -0700554 return 0;
555}
556SANDBOX_CMDLINE_OPT(spi_sf, 1, "connect a SPI flash: <bus>:<cs>:<id>:<file>");
Simon Glass01862382014-10-13 23:42:08 -0600557
558int sandbox_sf_bind_emul(struct sandbox_state *state, int busnum, int cs,
Simon Glassf3548632018-06-11 13:07:16 -0600559 struct udevice *bus, ofnode node, const char *spec)
Simon Glass01862382014-10-13 23:42:08 -0600560{
561 struct udevice *emul;
562 char name[20], *str;
563 struct driver *drv;
564 int ret;
565
566 /* now the emulator */
567 strncpy(name, spec, sizeof(name) - 6);
568 name[sizeof(name) - 6] = '\0';
569 strcat(name, "-emul");
Simon Glass01862382014-10-13 23:42:08 -0600570 drv = lists_driver_lookup_name("sandbox_sf_emul");
571 if (!drv) {
572 puts("Cannot find sandbox_sf_emul driver\n");
573 return -ENOENT;
574 }
Simon Glass18790eb2018-06-12 00:05:01 -0600575 str = strdup(name);
576 if (!str)
577 return -ENOMEM;
Simon Glassf3548632018-06-11 13:07:16 -0600578 ret = device_bind_ofnode(bus, drv, str, NULL, node, &emul);
Simon Glass01862382014-10-13 23:42:08 -0600579 if (ret) {
Simon Glass18790eb2018-06-12 00:05:01 -0600580 free(str);
Simon Glass01862382014-10-13 23:42:08 -0600581 printf("Cannot create emul device for spec '%s' (err=%d)\n",
582 spec, ret);
583 return ret;
584 }
585 state->spi[busnum][cs].emul = emul;
586
587 return 0;
588}
589
590void sandbox_sf_unbind_emul(struct sandbox_state *state, int busnum, int cs)
591{
Simon Glass5d2ee052015-01-25 08:27:12 -0700592 struct udevice *dev;
593
594 dev = state->spi[busnum][cs].emul;
Stefan Roese80b5bc92017-03-20 12:51:48 +0100595 device_remove(dev, DM_REMOVE_NORMAL);
Simon Glass5d2ee052015-01-25 08:27:12 -0700596 device_unbind(dev);
Simon Glass01862382014-10-13 23:42:08 -0600597 state->spi[busnum][cs].emul = NULL;
598}
599
600static int sandbox_sf_bind_bus_cs(struct sandbox_state *state, int busnum,
601 int cs, const char *spec)
602{
603 struct udevice *bus, *slave;
604 int ret;
605
606 ret = uclass_find_device_by_seq(UCLASS_SPI, busnum, true, &bus);
607 if (ret) {
608 printf("Invalid bus %d for spec '%s' (err=%d)\n", busnum,
609 spec, ret);
610 return ret;
611 }
Simon Glass5ef36f22014-11-11 10:46:22 -0700612 ret = spi_find_chip_select(bus, cs, &slave);
Simon Glass01862382014-10-13 23:42:08 -0600613 if (!ret) {
614 printf("Chip select %d already exists for spec '%s'\n", cs,
615 spec);
616 return -EEXIST;
617 }
618
Simon Glassd8a21f62014-11-11 10:46:23 -0700619 ret = device_bind_driver(bus, "spi_flash_std", spec, &slave);
Simon Glass01862382014-10-13 23:42:08 -0600620 if (ret)
621 return ret;
622
Simon Glassf3548632018-06-11 13:07:16 -0600623 return sandbox_sf_bind_emul(state, busnum, cs, bus, ofnode_null(),
624 spec);
Simon Glass01862382014-10-13 23:42:08 -0600625}
626
627int sandbox_spi_get_emul(struct sandbox_state *state,
628 struct udevice *bus, struct udevice *slave,
629 struct udevice **emulp)
630{
631 struct sandbox_spi_info *info;
632 int busnum = bus->seq;
633 int cs = spi_chip_select(slave);
634 int ret;
635
636 info = &state->spi[busnum][cs];
637 if (!info->emul) {
638 /* Use the same device tree node as the SPI flash device */
639 debug("%s: busnum=%u, cs=%u: binding SPI flash emulation: ",
640 __func__, busnum, cs);
641 ret = sandbox_sf_bind_emul(state, busnum, cs, bus,
Simon Glassf3548632018-06-11 13:07:16 -0600642 dev_ofnode(slave), slave->name);
Simon Glass01862382014-10-13 23:42:08 -0600643 if (ret) {
644 debug("failed (err=%d)\n", ret);
645 return ret;
646 }
647 debug("OK\n");
648 }
649 *emulp = info->emul;
650
651 return 0;
652}
653
654int dm_scan_other(bool pre_reloc_only)
655{
656 struct sandbox_state *state = state_get_current();
657 int busnum, cs;
658
659 if (pre_reloc_only)
660 return 0;
661 for (busnum = 0; busnum < CONFIG_SANDBOX_SPI_MAX_BUS; busnum++) {
662 for (cs = 0; cs < CONFIG_SANDBOX_SPI_MAX_CS; cs++) {
663 const char *spec = state->spi[busnum][cs].spec;
664 int ret;
665
666 if (spec) {
667 ret = sandbox_sf_bind_bus_cs(state, busnum,
668 cs, spec);
669 if (ret) {
670 debug("%s: Bind failed for bus %d, cs %d\n",
671 __func__, busnum, cs);
672 return ret;
673 }
Simon Glassdb45d9e2016-02-24 09:14:54 -0700674 debug("%s: Setting up spec '%s' for bus %d, cs %d\n",
675 __func__, spec, busnum, cs);
Simon Glass01862382014-10-13 23:42:08 -0600676 }
677 }
678 }
679
680 return 0;
681}
682#endif
683
684static const struct udevice_id sandbox_sf_ids[] = {
685 { .compatible = "sandbox,spi-flash" },
686 { }
687};
688
689U_BOOT_DRIVER(sandbox_sf_emul) = {
690 .name = "sandbox_sf_emul",
691 .id = UCLASS_SPI_EMUL,
692 .of_match = sandbox_sf_ids,
693 .ofdata_to_platdata = sandbox_sf_ofdata_to_platdata,
694 .probe = sandbox_sf_probe,
695 .remove = sandbox_sf_remove,
696 .priv_auto_alloc_size = sizeof(struct sandbox_spi_flash),
697 .platdata_auto_alloc_size = sizeof(struct sandbox_spi_flash_plat_data),
698 .ops = &sandbox_sf_emul_ops,
699};