blob: f59134ff9260dbd38b4dcec07bc29b74e7b09ca7 [file] [log] [blame]
Mike Frysingerffdb20b2013-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 Glassb6c29562014-10-13 23:42:08 -060012#include <dm.h>
Mike Frysingerffdb20b2013-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 Glassb6c29562014-10-13 23:42:08 -060023#include <dm/device-internal.h>
24#include <dm/lists.h>
25#include <dm/uclass-internal.h>
26
27DECLARE_GLOBAL_DATA_PTR;
Mike Frysingerffdb20b2013-12-03 16:43:27 -070028
29/*
30 * The different states that our SPI flash transitions between.
31 * We need to keep track of this across multiple xfer calls since
32 * the SPI bus could possibly call down into us multiple times.
33 */
34enum sandbox_sf_state {
35 SF_CMD, /* default state -- we're awaiting a command */
36 SF_ID, /* read the flash's (jedec) ID code */
37 SF_ADDR, /* processing the offset in the flash to read/etc... */
38 SF_READ, /* reading data from the flash */
39 SF_WRITE, /* writing data to the flash, i.e. page programming */
40 SF_ERASE, /* erase the flash */
41 SF_READ_STATUS, /* read the flash's status register */
42 SF_READ_STATUS1, /* read the flash's status register upper 8 bits*/
Simon Glassb6c29562014-10-13 23:42:08 -060043 SF_WRITE_STATUS, /* write the flash's status register */
Mike Frysingerffdb20b2013-12-03 16:43:27 -070044};
45
46static const char *sandbox_sf_state_name(enum sandbox_sf_state state)
47{
48 static const char * const states[] = {
49 "CMD", "ID", "ADDR", "READ", "WRITE", "ERASE", "READ_STATUS",
Simon Glassb6c29562014-10-13 23:42:08 -060050 "READ_STATUS1", "WRITE_STATUS",
Mike Frysingerffdb20b2013-12-03 16:43:27 -070051 };
52 return states[state];
53}
54
55/* Bits for the status register */
56#define STAT_WIP (1 << 0)
57#define STAT_WEL (1 << 1)
58
59/* Assume all SPI flashes have 3 byte addresses since they do atm */
60#define SF_ADDR_LEN 3
61
Simon Glass110bdee2014-09-15 06:33:19 -060062#define IDCODE_LEN 3
Mike Frysingerffdb20b2013-12-03 16:43:27 -070063
64/* Used to quickly bulk erase backing store */
65static u8 sandbox_sf_0xff[0x1000];
66
67/* Internal state data for each SPI flash */
68struct sandbox_spi_flash {
Simon Glassb6c29562014-10-13 23:42:08 -060069 unsigned int cs; /* Chip select we are attached to */
Mike Frysingerffdb20b2013-12-03 16:43:27 -070070 /*
71 * As we receive data over the SPI bus, our flash transitions
72 * between states. For example, we start off in the SF_CMD
73 * state where the first byte tells us what operation to perform
74 * (such as read or write the flash). But the operation itself
75 * can go through a few states such as first reading in the
76 * offset in the flash to perform the requested operation.
77 * Thus "state" stores the exact state that our machine is in
78 * while "cmd" stores the overall command we're processing.
79 */
80 enum sandbox_sf_state state;
81 uint cmd;
Simon Glass110bdee2014-09-15 06:33:19 -060082 /* Erase size of current erase command */
83 uint erase_size;
Mike Frysingerffdb20b2013-12-03 16:43:27 -070084 /* Current position in the flash; used when reading/writing/etc... */
85 uint off;
86 /* How many address bytes we've consumed */
87 uint addr_bytes, pad_addr_bytes;
88 /* The current flash status (see STAT_XXX defines above) */
89 u16 status;
90 /* Data describing the flash we're emulating */
Simon Glass110bdee2014-09-15 06:33:19 -060091 const struct spi_flash_params *data;
Mike Frysingerffdb20b2013-12-03 16:43:27 -070092 /* The file on disk to serv up data from */
93 int fd;
94};
95
Simon Glassb6c29562014-10-13 23:42:08 -060096struct sandbox_spi_flash_plat_data {
97 const char *filename;
98 const char *device_name;
99 int bus;
100 int cs;
101};
102
103/**
104 * This is a very strange probe function. If it has platform data (which may
105 * have come from the device tree) then this function gets the filename and
106 * device type from there. Failing that it looks at the command line
107 * parameter.
108 */
109static int sandbox_sf_probe(struct udevice *dev)
Mike Frysingerffdb20b2013-12-03 16:43:27 -0700110{
111 /* spec = idcode:file */
Simon Glassb6c29562014-10-13 23:42:08 -0600112 struct sandbox_spi_flash *sbsf = dev_get_priv(dev);
Mike Frysingerffdb20b2013-12-03 16:43:27 -0700113 const char *file;
Simon Glass110bdee2014-09-15 06:33:19 -0600114 size_t len, idname_len;
115 const struct spi_flash_params *data;
Simon Glassb6c29562014-10-13 23:42:08 -0600116 struct sandbox_spi_flash_plat_data *pdata = dev_get_platdata(dev);
117 struct sandbox_state *state = state_get_current();
118 struct udevice *bus = dev->parent;
119 const char *spec = NULL;
120 int ret = 0;
121 int cs = -1;
122 int i;
Mike Frysingerffdb20b2013-12-03 16:43:27 -0700123
Simon Glassb6c29562014-10-13 23:42:08 -0600124 debug("%s: bus %d, looking for emul=%p: ", __func__, bus->seq, dev);
125 if (bus->seq >= 0 && bus->seq < CONFIG_SANDBOX_SPI_MAX_BUS) {
126 for (i = 0; i < CONFIG_SANDBOX_SPI_MAX_CS; i++) {
127 if (state->spi[bus->seq][i].emul == dev)
128 cs = i;
129 }
Mike Frysingerffdb20b2013-12-03 16:43:27 -0700130 }
Simon Glassb6c29562014-10-13 23:42:08 -0600131 if (cs == -1) {
Simon Glass832adb22015-05-04 11:31:10 -0600132 printf("Error: Unknown chip select for device '%s'\n",
Simon Glassb6c29562014-10-13 23:42:08 -0600133 dev->name);
134 return -EINVAL;
135 }
136 debug("found at cs %d\n", cs);
137
138 if (!pdata->filename) {
139 struct sandbox_state *state = state_get_current();
140
141 assert(bus->seq != -1);
142 if (bus->seq < CONFIG_SANDBOX_SPI_MAX_BUS)
143 spec = state->spi[bus->seq][cs].spec;
Simon Glass1603bf32015-01-25 08:27:09 -0700144 if (!spec) {
Simon Glass20f655d2016-02-24 09:14:54 -0700145 debug("%s: No spec found for bus %d, cs %d\n",
146 __func__, bus->seq, cs);
Simon Glass1603bf32015-01-25 08:27:09 -0700147 ret = -ENOENT;
148 goto error;
149 }
Simon Glassb6c29562014-10-13 23:42:08 -0600150
151 file = strchr(spec, ':');
152 if (!file) {
Simon Glass20f655d2016-02-24 09:14:54 -0700153 printf("%s: unable to parse file\n", __func__);
Simon Glassb6c29562014-10-13 23:42:08 -0600154 ret = -EINVAL;
155 goto error;
156 }
157 idname_len = file - spec;
158 pdata->filename = file + 1;
159 pdata->device_name = spec;
160 ++file;
161 } else {
162 spec = strchr(pdata->device_name, ',');
163 if (spec)
164 spec++;
165 else
166 spec = pdata->device_name;
167 idname_len = strlen(spec);
168 }
169 debug("%s: device='%s'\n", __func__, spec);
Mike Frysingerffdb20b2013-12-03 16:43:27 -0700170
Simon Glass110bdee2014-09-15 06:33:19 -0600171 for (data = spi_flash_params_table; data->name; data++) {
Mike Frysingerffdb20b2013-12-03 16:43:27 -0700172 len = strlen(data->name);
173 if (idname_len != len)
174 continue;
Simon Glassb6c29562014-10-13 23:42:08 -0600175 if (!strncasecmp(spec, data->name, len))
Mike Frysingerffdb20b2013-12-03 16:43:27 -0700176 break;
177 }
Simon Glass110bdee2014-09-15 06:33:19 -0600178 if (!data->name) {
Simon Glass20f655d2016-02-24 09:14:54 -0700179 printf("%s: unknown flash '%*s'\n", __func__, (int)idname_len,
Mike Frysingerffdb20b2013-12-03 16:43:27 -0700180 spec);
Simon Glassb6c29562014-10-13 23:42:08 -0600181 ret = -EINVAL;
Mike Frysingerffdb20b2013-12-03 16:43:27 -0700182 goto error;
183 }
184
185 if (sandbox_sf_0xff[0] == 0x00)
186 memset(sandbox_sf_0xff, 0xff, sizeof(sandbox_sf_0xff));
187
Simon Glassb6c29562014-10-13 23:42:08 -0600188 sbsf->fd = os_open(pdata->filename, 02);
Mike Frysingerffdb20b2013-12-03 16:43:27 -0700189 if (sbsf->fd == -1) {
Simon Glass20f655d2016-02-24 09:14:54 -0700190 printf("%s: unable to open file '%s'\n", __func__,
Simon Glassb6c29562014-10-13 23:42:08 -0600191 pdata->filename);
192 ret = -EIO;
Mike Frysingerffdb20b2013-12-03 16:43:27 -0700193 goto error;
194 }
195
196 sbsf->data = data;
Simon Glassb6c29562014-10-13 23:42:08 -0600197 sbsf->cs = cs;
Mike Frysingerffdb20b2013-12-03 16:43:27 -0700198
Mike Frysingerffdb20b2013-12-03 16:43:27 -0700199 return 0;
200
201 error:
Simon Glass1603bf32015-01-25 08:27:09 -0700202 debug("%s: Got error %d\n", __func__, ret);
Simon Glassb6c29562014-10-13 23:42:08 -0600203 return ret;
Mike Frysingerffdb20b2013-12-03 16:43:27 -0700204}
205
Simon Glassb6c29562014-10-13 23:42:08 -0600206static int sandbox_sf_remove(struct udevice *dev)
Mike Frysingerffdb20b2013-12-03 16:43:27 -0700207{
Simon Glassb6c29562014-10-13 23:42:08 -0600208 struct sandbox_spi_flash *sbsf = dev_get_priv(dev);
Mike Frysingerffdb20b2013-12-03 16:43:27 -0700209
210 os_close(sbsf->fd);
Simon Glassb6c29562014-10-13 23:42:08 -0600211
212 return 0;
Mike Frysingerffdb20b2013-12-03 16:43:27 -0700213}
214
Simon Glassb6c29562014-10-13 23:42:08 -0600215static void sandbox_sf_cs_activate(struct udevice *dev)
Mike Frysingerffdb20b2013-12-03 16:43:27 -0700216{
Simon Glassb6c29562014-10-13 23:42:08 -0600217 struct sandbox_spi_flash *sbsf = dev_get_priv(dev);
Mike Frysingerffdb20b2013-12-03 16:43:27 -0700218
219 debug("sandbox_sf: CS activated; state is fresh!\n");
220
221 /* CS is asserted, so reset state */
222 sbsf->off = 0;
223 sbsf->addr_bytes = 0;
224 sbsf->pad_addr_bytes = 0;
225 sbsf->state = SF_CMD;
226 sbsf->cmd = SF_CMD;
227}
228
Simon Glassb6c29562014-10-13 23:42:08 -0600229static void sandbox_sf_cs_deactivate(struct udevice *dev)
Mike Frysingerffdb20b2013-12-03 16:43:27 -0700230{
231 debug("sandbox_sf: CS deactivated; cmd done processing!\n");
232}
233
Simon Glassb6c29562014-10-13 23:42:08 -0600234/*
235 * There are times when the data lines are allowed to tristate. What
236 * is actually sensed on the line depends on the hardware. It could
237 * always be 0xFF/0x00 (if there are pull ups/downs), or things could
238 * float and so we'd get garbage back. This func encapsulates that
239 * scenario so we can worry about the details here.
240 */
241static void sandbox_spi_tristate(u8 *buf, uint len)
242{
243 /* XXX: make this into a user config option ? */
244 memset(buf, 0xff, len);
245}
246
Mike Frysingerffdb20b2013-12-03 16:43:27 -0700247/* Figure out what command this stream is telling us to do */
248static int sandbox_sf_process_cmd(struct sandbox_spi_flash *sbsf, const u8 *rx,
249 u8 *tx)
250{
251 enum sandbox_sf_state oldstate = sbsf->state;
252
253 /* We need to output a byte for the cmd byte we just ate */
Simon Glassb6c29562014-10-13 23:42:08 -0600254 if (tx)
255 sandbox_spi_tristate(tx, 1);
Mike Frysingerffdb20b2013-12-03 16:43:27 -0700256
257 sbsf->cmd = rx[0];
258 switch (sbsf->cmd) {
259 case CMD_READ_ID:
260 sbsf->state = SF_ID;
261 sbsf->cmd = SF_ID;
262 break;
263 case CMD_READ_ARRAY_FAST:
264 sbsf->pad_addr_bytes = 1;
265 case CMD_READ_ARRAY_SLOW:
266 case CMD_PAGE_PROGRAM:
Mike Frysingerffdb20b2013-12-03 16:43:27 -0700267 sbsf->state = SF_ADDR;
268 break;
269 case CMD_WRITE_DISABLE:
270 debug(" write disabled\n");
271 sbsf->status &= ~STAT_WEL;
272 break;
273 case CMD_READ_STATUS:
274 sbsf->state = SF_READ_STATUS;
275 break;
276 case CMD_READ_STATUS1:
277 sbsf->state = SF_READ_STATUS1;
278 break;
279 case CMD_WRITE_ENABLE:
280 debug(" write enabled\n");
281 sbsf->status |= STAT_WEL;
282 break;
Simon Glassb6c29562014-10-13 23:42:08 -0600283 case CMD_WRITE_STATUS:
284 sbsf->state = SF_WRITE_STATUS;
285 break;
Mike Frysingerffdb20b2013-12-03 16:43:27 -0700286 default: {
Simon Glass110bdee2014-09-15 06:33:19 -0600287 int flags = sbsf->data->flags;
Mike Frysingerffdb20b2013-12-03 16:43:27 -0700288
Simon Glass110bdee2014-09-15 06:33:19 -0600289 /* we only support erase here */
290 if (sbsf->cmd == CMD_ERASE_CHIP) {
291 sbsf->erase_size = sbsf->data->sector_size *
292 sbsf->data->nr_sectors;
293 } else if (sbsf->cmd == CMD_ERASE_4K && (flags & SECT_4K)) {
294 sbsf->erase_size = 4 << 10;
Jagan Tekiddc2dfb2016-08-08 17:19:08 +0530295 } else if (sbsf->cmd == CMD_ERASE_64K && !(flags & SECT_4K)) {
Simon Glass110bdee2014-09-15 06:33:19 -0600296 sbsf->erase_size = 64 << 10;
297 } else {
298 debug(" cmd unknown: %#x\n", sbsf->cmd);
Simon Glassb6c29562014-10-13 23:42:08 -0600299 return -EIO;
Mike Frysingerffdb20b2013-12-03 16:43:27 -0700300 }
Simon Glass110bdee2014-09-15 06:33:19 -0600301 sbsf->state = SF_ADDR;
302 break;
Mike Frysingerffdb20b2013-12-03 16:43:27 -0700303 }
304 }
305
306 if (oldstate != sbsf->state)
307 debug(" cmd: transition to %s state\n",
308 sandbox_sf_state_name(sbsf->state));
309
310 return 0;
311}
312
313int sandbox_erase_part(struct sandbox_spi_flash *sbsf, int size)
314{
315 int todo;
316 int ret;
317
318 while (size > 0) {
Masahiro Yamadab4141192014-11-07 03:03:31 +0900319 todo = min(size, (int)sizeof(sandbox_sf_0xff));
Mike Frysingerffdb20b2013-12-03 16:43:27 -0700320 ret = os_write(sbsf->fd, sandbox_sf_0xff, todo);
321 if (ret != todo)
322 return ret;
323 size -= todo;
324 }
325
326 return 0;
327}
328
Simon Glassb6c29562014-10-13 23:42:08 -0600329static int sandbox_sf_xfer(struct udevice *dev, unsigned int bitlen,
330 const void *rxp, void *txp, unsigned long flags)
Mike Frysingerffdb20b2013-12-03 16:43:27 -0700331{
Simon Glassb6c29562014-10-13 23:42:08 -0600332 struct sandbox_spi_flash *sbsf = dev_get_priv(dev);
333 const uint8_t *rx = rxp;
334 uint8_t *tx = txp;
Mike Frysingerffdb20b2013-12-03 16:43:27 -0700335 uint cnt, pos = 0;
Simon Glassb6c29562014-10-13 23:42:08 -0600336 int bytes = bitlen / 8;
Mike Frysingerffdb20b2013-12-03 16:43:27 -0700337 int ret;
338
339 debug("sandbox_sf: state:%x(%s) bytes:%u\n", sbsf->state,
340 sandbox_sf_state_name(sbsf->state), bytes);
341
Simon Glassb6c29562014-10-13 23:42:08 -0600342 if ((flags & SPI_XFER_BEGIN))
343 sandbox_sf_cs_activate(dev);
344
Mike Frysingerffdb20b2013-12-03 16:43:27 -0700345 if (sbsf->state == SF_CMD) {
346 /* Figure out the initial state */
Simon Glassb6c29562014-10-13 23:42:08 -0600347 ret = sandbox_sf_process_cmd(sbsf, rx, tx);
348 if (ret)
349 return ret;
Mike Frysingerffdb20b2013-12-03 16:43:27 -0700350 ++pos;
351 }
352
353 /* Process the remaining data */
354 while (pos < bytes) {
355 switch (sbsf->state) {
356 case SF_ID: {
357 u8 id;
358
359 debug(" id: off:%u tx:", sbsf->off);
Simon Glass110bdee2014-09-15 06:33:19 -0600360 if (sbsf->off < IDCODE_LEN) {
361 /* Extract correct byte from ID 0x00aabbcc */
362 id = sbsf->data->jedec >>
363 (8 * (IDCODE_LEN - 1 - sbsf->off));
364 } else {
Mike Frysingerffdb20b2013-12-03 16:43:27 -0700365 id = 0;
Simon Glass110bdee2014-09-15 06:33:19 -0600366 }
367 debug("%d %02x\n", sbsf->off, id);
Mike Frysingerffdb20b2013-12-03 16:43:27 -0700368 tx[pos++] = id;
369 ++sbsf->off;
370 break;
371 }
372 case SF_ADDR:
373 debug(" addr: bytes:%u rx:%02x ", sbsf->addr_bytes,
374 rx[pos]);
375
376 if (sbsf->addr_bytes++ < SF_ADDR_LEN)
377 sbsf->off = (sbsf->off << 8) | rx[pos];
378 debug("addr:%06x\n", sbsf->off);
379
Simon Glassb6c29562014-10-13 23:42:08 -0600380 if (tx)
381 sandbox_spi_tristate(&tx[pos], 1);
382 pos++;
Mike Frysingerffdb20b2013-12-03 16:43:27 -0700383
384 /* See if we're done processing */
385 if (sbsf->addr_bytes <
386 SF_ADDR_LEN + sbsf->pad_addr_bytes)
387 break;
388
389 /* Next state! */
390 if (os_lseek(sbsf->fd, sbsf->off, OS_SEEK_SET) < 0) {
391 puts("sandbox_sf: os_lseek() failed");
Simon Glassb6c29562014-10-13 23:42:08 -0600392 return -EIO;
Mike Frysingerffdb20b2013-12-03 16:43:27 -0700393 }
394 switch (sbsf->cmd) {
395 case CMD_READ_ARRAY_FAST:
396 case CMD_READ_ARRAY_SLOW:
397 sbsf->state = SF_READ;
398 break;
399 case CMD_PAGE_PROGRAM:
400 sbsf->state = SF_WRITE;
401 break;
402 default:
403 /* assume erase state ... */
404 sbsf->state = SF_ERASE;
405 goto case_sf_erase;
406 }
407 debug(" cmd: transition to %s state\n",
408 sandbox_sf_state_name(sbsf->state));
409 break;
410 case SF_READ:
411 /*
412 * XXX: need to handle exotic behavior:
413 * - reading past end of device
414 */
415
416 cnt = bytes - pos;
417 debug(" tx: read(%u)\n", cnt);
Simon Glassb6c29562014-10-13 23:42:08 -0600418 assert(tx);
Mike Frysingerffdb20b2013-12-03 16:43:27 -0700419 ret = os_read(sbsf->fd, tx + pos, cnt);
420 if (ret < 0) {
Simon Glassb6c29562014-10-13 23:42:08 -0600421 puts("sandbox_sf: os_read() failed\n");
422 return -EIO;
Mike Frysingerffdb20b2013-12-03 16:43:27 -0700423 }
424 pos += ret;
425 break;
426 case SF_READ_STATUS:
427 debug(" read status: %#x\n", sbsf->status);
428 cnt = bytes - pos;
429 memset(tx + pos, sbsf->status, cnt);
430 pos += cnt;
431 break;
432 case SF_READ_STATUS1:
433 debug(" read status: %#x\n", sbsf->status);
434 cnt = bytes - pos;
435 memset(tx + pos, sbsf->status >> 8, cnt);
436 pos += cnt;
437 break;
Simon Glassb6c29562014-10-13 23:42:08 -0600438 case SF_WRITE_STATUS:
439 debug(" write status: %#x (ignored)\n", rx[pos]);
440 pos = bytes;
441 break;
Mike Frysingerffdb20b2013-12-03 16:43:27 -0700442 case SF_WRITE:
443 /*
444 * XXX: need to handle exotic behavior:
445 * - unaligned addresses
446 * - more than a page (256) worth of data
447 * - reading past end of device
448 */
449 if (!(sbsf->status & STAT_WEL)) {
450 puts("sandbox_sf: write enable not set before write\n");
451 goto done;
452 }
453
454 cnt = bytes - pos;
455 debug(" rx: write(%u)\n", cnt);
Simon Glassb6c29562014-10-13 23:42:08 -0600456 if (tx)
457 sandbox_spi_tristate(&tx[pos], cnt);
Mike Frysingerffdb20b2013-12-03 16:43:27 -0700458 ret = os_write(sbsf->fd, rx + pos, cnt);
459 if (ret < 0) {
460 puts("sandbox_spi: os_write() failed\n");
Simon Glassb6c29562014-10-13 23:42:08 -0600461 return -EIO;
Mike Frysingerffdb20b2013-12-03 16:43:27 -0700462 }
463 pos += ret;
464 sbsf->status &= ~STAT_WEL;
465 break;
466 case SF_ERASE:
467 case_sf_erase: {
Mike Frysingerffdb20b2013-12-03 16:43:27 -0700468 if (!(sbsf->status & STAT_WEL)) {
469 puts("sandbox_sf: write enable not set before erase\n");
470 goto done;
471 }
472
473 /* verify address is aligned */
Simon Glass110bdee2014-09-15 06:33:19 -0600474 if (sbsf->off & (sbsf->erase_size - 1)) {
Mike Frysingerffdb20b2013-12-03 16:43:27 -0700475 debug(" sector erase: cmd:%#x needs align:%#x, but we got %#x\n",
Simon Glass110bdee2014-09-15 06:33:19 -0600476 sbsf->cmd, sbsf->erase_size,
Mike Frysingerffdb20b2013-12-03 16:43:27 -0700477 sbsf->off);
478 sbsf->status &= ~STAT_WEL;
479 goto done;
480 }
481
Simon Glass110bdee2014-09-15 06:33:19 -0600482 debug(" sector erase addr: %u, size: %u\n", sbsf->off,
483 sbsf->erase_size);
Mike Frysingerffdb20b2013-12-03 16:43:27 -0700484
485 cnt = bytes - pos;
Simon Glassb6c29562014-10-13 23:42:08 -0600486 if (tx)
487 sandbox_spi_tristate(&tx[pos], cnt);
Mike Frysingerffdb20b2013-12-03 16:43:27 -0700488 pos += cnt;
489
490 /*
491 * TODO(vapier@gentoo.org): latch WIP in status, and
492 * delay before clearing it ?
493 */
Simon Glass110bdee2014-09-15 06:33:19 -0600494 ret = sandbox_erase_part(sbsf, sbsf->erase_size);
Mike Frysingerffdb20b2013-12-03 16:43:27 -0700495 sbsf->status &= ~STAT_WEL;
496 if (ret) {
497 debug("sandbox_sf: Erase failed\n");
498 goto done;
499 }
500 goto done;
501 }
502 default:
503 debug(" ??? no idea what to do ???\n");
504 goto done;
505 }
506 }
507
508 done:
Simon Glassb6c29562014-10-13 23:42:08 -0600509 if (flags & SPI_XFER_END)
510 sandbox_sf_cs_deactivate(dev);
511 return pos == bytes ? 0 : -EIO;
Mike Frysingerffdb20b2013-12-03 16:43:27 -0700512}
513
Simon Glassb6c29562014-10-13 23:42:08 -0600514int sandbox_sf_ofdata_to_platdata(struct udevice *dev)
515{
516 struct sandbox_spi_flash_plat_data *pdata = dev_get_platdata(dev);
517 const void *blob = gd->fdt_blob;
518 int node = dev->of_offset;
519
520 pdata->filename = fdt_getprop(blob, node, "sandbox,filename", NULL);
521 pdata->device_name = fdt_getprop(blob, node, "compatible", NULL);
522 if (!pdata->filename || !pdata->device_name) {
523 debug("%s: Missing properties, filename=%s, device_name=%s\n",
524 __func__, pdata->filename, pdata->device_name);
525 return -EINVAL;
526 }
527
528 return 0;
529}
530
531static const struct dm_spi_emul_ops sandbox_sf_emul_ops = {
Mike Frysingerffdb20b2013-12-03 16:43:27 -0700532 .xfer = sandbox_sf_xfer,
533};
534
Simon Glassb6c29562014-10-13 23:42:08 -0600535#ifdef CONFIG_SPI_FLASH
Mike Frysingerffdb20b2013-12-03 16:43:27 -0700536static int sandbox_cmdline_cb_spi_sf(struct sandbox_state *state,
537 const char *arg)
538{
539 unsigned long bus, cs;
540 const char *spec = sandbox_spi_parse_spec(arg, &bus, &cs);
541
542 if (!spec)
543 return 1;
544
545 /*
546 * It is safe to not make a copy of 'spec' because it comes from the
547 * command line.
548 *
549 * TODO(sjg@chromium.org): It would be nice if we could parse the
550 * spec here, but the problem is that no U-Boot init has been done
551 * yet. Perhaps we can figure something out.
552 */
Mike Frysingerffdb20b2013-12-03 16:43:27 -0700553 state->spi[bus][cs].spec = spec;
Simon Glass20f655d2016-02-24 09:14:54 -0700554 debug("%s: Setting up spec '%s' for bus %ld, cs %ld\n", __func__,
555 spec, bus, cs);
556
Mike Frysingerffdb20b2013-12-03 16:43:27 -0700557 return 0;
558}
559SANDBOX_CMDLINE_OPT(spi_sf, 1, "connect a SPI flash: <bus>:<cs>:<id>:<file>");
Simon Glassb6c29562014-10-13 23:42:08 -0600560
561int sandbox_sf_bind_emul(struct sandbox_state *state, int busnum, int cs,
562 struct udevice *bus, int of_offset, const char *spec)
563{
564 struct udevice *emul;
565 char name[20], *str;
566 struct driver *drv;
567 int ret;
568
569 /* now the emulator */
570 strncpy(name, spec, sizeof(name) - 6);
571 name[sizeof(name) - 6] = '\0';
572 strcat(name, "-emul");
573 str = strdup(name);
574 if (!str)
575 return -ENOMEM;
576 drv = lists_driver_lookup_name("sandbox_sf_emul");
577 if (!drv) {
578 puts("Cannot find sandbox_sf_emul driver\n");
579 return -ENOENT;
580 }
581 ret = device_bind(bus, drv, str, NULL, of_offset, &emul);
582 if (ret) {
583 printf("Cannot create emul device for spec '%s' (err=%d)\n",
584 spec, ret);
585 return ret;
586 }
587 state->spi[busnum][cs].emul = emul;
588
589 return 0;
590}
591
592void sandbox_sf_unbind_emul(struct sandbox_state *state, int busnum, int cs)
593{
Simon Glassd0cff032015-01-25 08:27:12 -0700594 struct udevice *dev;
595
596 dev = state->spi[busnum][cs].emul;
597 device_remove(dev);
598 device_unbind(dev);
Simon Glassb6c29562014-10-13 23:42:08 -0600599 state->spi[busnum][cs].emul = NULL;
600}
601
602static int sandbox_sf_bind_bus_cs(struct sandbox_state *state, int busnum,
603 int cs, const char *spec)
604{
605 struct udevice *bus, *slave;
606 int ret;
607
608 ret = uclass_find_device_by_seq(UCLASS_SPI, busnum, true, &bus);
609 if (ret) {
610 printf("Invalid bus %d for spec '%s' (err=%d)\n", busnum,
611 spec, ret);
612 return ret;
613 }
Simon Glassff56bba2014-11-11 10:46:22 -0700614 ret = spi_find_chip_select(bus, cs, &slave);
Simon Glassb6c29562014-10-13 23:42:08 -0600615 if (!ret) {
616 printf("Chip select %d already exists for spec '%s'\n", cs,
617 spec);
618 return -EEXIST;
619 }
620
Simon Glass6b186562014-11-11 10:46:23 -0700621 ret = device_bind_driver(bus, "spi_flash_std", spec, &slave);
Simon Glassb6c29562014-10-13 23:42:08 -0600622 if (ret)
623 return ret;
624
625 return sandbox_sf_bind_emul(state, busnum, cs, bus, -1, spec);
626}
627
628int sandbox_spi_get_emul(struct sandbox_state *state,
629 struct udevice *bus, struct udevice *slave,
630 struct udevice **emulp)
631{
632 struct sandbox_spi_info *info;
633 int busnum = bus->seq;
634 int cs = spi_chip_select(slave);
635 int ret;
636
637 info = &state->spi[busnum][cs];
638 if (!info->emul) {
639 /* Use the same device tree node as the SPI flash device */
640 debug("%s: busnum=%u, cs=%u: binding SPI flash emulation: ",
641 __func__, busnum, cs);
642 ret = sandbox_sf_bind_emul(state, busnum, cs, bus,
643 slave->of_offset, slave->name);
644 if (ret) {
645 debug("failed (err=%d)\n", ret);
646 return ret;
647 }
648 debug("OK\n");
649 }
650 *emulp = info->emul;
651
652 return 0;
653}
654
655int dm_scan_other(bool pre_reloc_only)
656{
657 struct sandbox_state *state = state_get_current();
658 int busnum, cs;
659
660 if (pre_reloc_only)
661 return 0;
662 for (busnum = 0; busnum < CONFIG_SANDBOX_SPI_MAX_BUS; busnum++) {
663 for (cs = 0; cs < CONFIG_SANDBOX_SPI_MAX_CS; cs++) {
664 const char *spec = state->spi[busnum][cs].spec;
665 int ret;
666
667 if (spec) {
668 ret = sandbox_sf_bind_bus_cs(state, busnum,
669 cs, spec);
670 if (ret) {
671 debug("%s: Bind failed for bus %d, cs %d\n",
672 __func__, busnum, cs);
673 return ret;
674 }
Simon Glass20f655d2016-02-24 09:14:54 -0700675 debug("%s: Setting up spec '%s' for bus %d, cs %d\n",
676 __func__, spec, busnum, cs);
Simon Glassb6c29562014-10-13 23:42:08 -0600677 }
678 }
679 }
680
681 return 0;
682}
683#endif
684
685static const struct udevice_id sandbox_sf_ids[] = {
686 { .compatible = "sandbox,spi-flash" },
687 { }
688};
689
690U_BOOT_DRIVER(sandbox_sf_emul) = {
691 .name = "sandbox_sf_emul",
692 .id = UCLASS_SPI_EMUL,
693 .of_match = sandbox_sf_ids,
694 .ofdata_to_platdata = sandbox_sf_ofdata_to_platdata,
695 .probe = sandbox_sf_probe,
696 .remove = sandbox_sf_remove,
697 .priv_auto_alloc_size = sizeof(struct sandbox_spi_flash),
698 .platdata_auto_alloc_size = sizeof(struct sandbox_spi_flash_plat_data),
699 .ops = &sandbox_sf_emul_ops,
700};