blob: 987b05dd9393812b74e833812b9ac32ee72ae1a9 [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
Simon Glassfd5259a2018-10-01 11:55:09 -0600104 * device type from there.
Simon Glass01862382014-10-13 23:42:08 -0600105 */
106static int sandbox_sf_probe(struct udevice *dev)
Mike Frysingerb375ad92013-12-03 16:43:27 -0700107{
108 /* spec = idcode:file */
Simon Glass01862382014-10-13 23:42:08 -0600109 struct sandbox_spi_flash *sbsf = dev_get_priv(dev);
Simon Glass5d8f0632014-09-15 06:33:19 -0600110 size_t len, idname_len;
Jagan Teki77ae47b2016-10-30 23:16:10 +0530111 const struct spi_flash_info *data;
Simon Glass01862382014-10-13 23:42:08 -0600112 struct sandbox_spi_flash_plat_data *pdata = dev_get_platdata(dev);
113 struct sandbox_state *state = state_get_current();
114 struct udevice *bus = dev->parent;
115 const char *spec = NULL;
116 int ret = 0;
117 int cs = -1;
118 int i;
Mike Frysingerb375ad92013-12-03 16:43:27 -0700119
Simon Glass01862382014-10-13 23:42:08 -0600120 debug("%s: bus %d, looking for emul=%p: ", __func__, bus->seq, dev);
121 if (bus->seq >= 0 && bus->seq < CONFIG_SANDBOX_SPI_MAX_BUS) {
122 for (i = 0; i < CONFIG_SANDBOX_SPI_MAX_CS; i++) {
123 if (state->spi[bus->seq][i].emul == dev)
124 cs = i;
125 }
126 }
127 if (cs == -1) {
Simon Glassbb213e32015-05-04 11:31:10 -0600128 printf("Error: Unknown chip select for device '%s'\n",
Simon Glass01862382014-10-13 23:42:08 -0600129 dev->name);
130 return -EINVAL;
131 }
132 debug("found at cs %d\n", cs);
133
134 if (!pdata->filename) {
Simon Glassfd5259a2018-10-01 11:55:09 -0600135 printf("Error: No filename available\n");
136 return -EINVAL;
Mike Frysingerb375ad92013-12-03 16:43:27 -0700137 }
Simon Glassfd5259a2018-10-01 11:55:09 -0600138 spec = strchr(pdata->device_name, ',');
139 if (spec)
140 spec++;
141 else
142 spec = pdata->device_name;
143 idname_len = strlen(spec);
Simon Glass01862382014-10-13 23:42:08 -0600144 debug("%s: device='%s'\n", __func__, spec);
Mike Frysingerb375ad92013-12-03 16:43:27 -0700145
Jagan Teki77ae47b2016-10-30 23:16:10 +0530146 for (data = spi_flash_ids; data->name; data++) {
Mike Frysingerb375ad92013-12-03 16:43:27 -0700147 len = strlen(data->name);
148 if (idname_len != len)
149 continue;
Simon Glass01862382014-10-13 23:42:08 -0600150 if (!strncasecmp(spec, data->name, len))
Mike Frysingerb375ad92013-12-03 16:43:27 -0700151 break;
152 }
Simon Glass5d8f0632014-09-15 06:33:19 -0600153 if (!data->name) {
Simon Glassdb45d9e2016-02-24 09:14:54 -0700154 printf("%s: unknown flash '%*s'\n", __func__, (int)idname_len,
Mike Frysingerb375ad92013-12-03 16:43:27 -0700155 spec);
Simon Glass01862382014-10-13 23:42:08 -0600156 ret = -EINVAL;
Mike Frysingerb375ad92013-12-03 16:43:27 -0700157 goto error;
158 }
159
160 if (sandbox_sf_0xff[0] == 0x00)
161 memset(sandbox_sf_0xff, 0xff, sizeof(sandbox_sf_0xff));
162
Simon Glass01862382014-10-13 23:42:08 -0600163 sbsf->fd = os_open(pdata->filename, 02);
Mike Frysingerb375ad92013-12-03 16:43:27 -0700164 if (sbsf->fd == -1) {
Simon Glassdb45d9e2016-02-24 09:14:54 -0700165 printf("%s: unable to open file '%s'\n", __func__,
Simon Glass01862382014-10-13 23:42:08 -0600166 pdata->filename);
167 ret = -EIO;
Mike Frysingerb375ad92013-12-03 16:43:27 -0700168 goto error;
169 }
170
171 sbsf->data = data;
Simon Glass01862382014-10-13 23:42:08 -0600172 sbsf->cs = cs;
Mike Frysingerb375ad92013-12-03 16:43:27 -0700173
Mike Frysingerb375ad92013-12-03 16:43:27 -0700174 return 0;
175
176 error:
Simon Glassef3493d2015-01-25 08:27:09 -0700177 debug("%s: Got error %d\n", __func__, ret);
Simon Glass01862382014-10-13 23:42:08 -0600178 return ret;
Mike Frysingerb375ad92013-12-03 16:43:27 -0700179}
180
Simon Glass01862382014-10-13 23:42:08 -0600181static int sandbox_sf_remove(struct udevice *dev)
Mike Frysingerb375ad92013-12-03 16:43:27 -0700182{
Simon Glass01862382014-10-13 23:42:08 -0600183 struct sandbox_spi_flash *sbsf = dev_get_priv(dev);
Mike Frysingerb375ad92013-12-03 16:43:27 -0700184
185 os_close(sbsf->fd);
Simon Glass01862382014-10-13 23:42:08 -0600186
187 return 0;
Mike Frysingerb375ad92013-12-03 16:43:27 -0700188}
189
Simon Glass01862382014-10-13 23:42:08 -0600190static void sandbox_sf_cs_activate(struct udevice *dev)
Mike Frysingerb375ad92013-12-03 16:43:27 -0700191{
Simon Glass01862382014-10-13 23:42:08 -0600192 struct sandbox_spi_flash *sbsf = dev_get_priv(dev);
Mike Frysingerb375ad92013-12-03 16:43:27 -0700193
194 debug("sandbox_sf: CS activated; state is fresh!\n");
195
196 /* CS is asserted, so reset state */
197 sbsf->off = 0;
198 sbsf->addr_bytes = 0;
199 sbsf->pad_addr_bytes = 0;
200 sbsf->state = SF_CMD;
201 sbsf->cmd = SF_CMD;
202}
203
Simon Glass01862382014-10-13 23:42:08 -0600204static void sandbox_sf_cs_deactivate(struct udevice *dev)
Mike Frysingerb375ad92013-12-03 16:43:27 -0700205{
206 debug("sandbox_sf: CS deactivated; cmd done processing!\n");
207}
208
Simon Glass01862382014-10-13 23:42:08 -0600209/*
210 * There are times when the data lines are allowed to tristate. What
211 * is actually sensed on the line depends on the hardware. It could
212 * always be 0xFF/0x00 (if there are pull ups/downs), or things could
213 * float and so we'd get garbage back. This func encapsulates that
214 * scenario so we can worry about the details here.
215 */
216static void sandbox_spi_tristate(u8 *buf, uint len)
217{
218 /* XXX: make this into a user config option ? */
219 memset(buf, 0xff, len);
220}
221
Mike Frysingerb375ad92013-12-03 16:43:27 -0700222/* Figure out what command this stream is telling us to do */
223static int sandbox_sf_process_cmd(struct sandbox_spi_flash *sbsf, const u8 *rx,
224 u8 *tx)
225{
226 enum sandbox_sf_state oldstate = sbsf->state;
227
228 /* We need to output a byte for the cmd byte we just ate */
Simon Glass01862382014-10-13 23:42:08 -0600229 if (tx)
230 sandbox_spi_tristate(tx, 1);
Mike Frysingerb375ad92013-12-03 16:43:27 -0700231
232 sbsf->cmd = rx[0];
233 switch (sbsf->cmd) {
234 case CMD_READ_ID:
235 sbsf->state = SF_ID;
236 sbsf->cmd = SF_ID;
237 break;
238 case CMD_READ_ARRAY_FAST:
239 sbsf->pad_addr_bytes = 1;
240 case CMD_READ_ARRAY_SLOW:
241 case CMD_PAGE_PROGRAM:
Mike Frysingerb375ad92013-12-03 16:43:27 -0700242 sbsf->state = SF_ADDR;
243 break;
244 case CMD_WRITE_DISABLE:
245 debug(" write disabled\n");
246 sbsf->status &= ~STAT_WEL;
247 break;
248 case CMD_READ_STATUS:
249 sbsf->state = SF_READ_STATUS;
250 break;
251 case CMD_READ_STATUS1:
252 sbsf->state = SF_READ_STATUS1;
253 break;
254 case CMD_WRITE_ENABLE:
255 debug(" write enabled\n");
256 sbsf->status |= STAT_WEL;
257 break;
Simon Glass01862382014-10-13 23:42:08 -0600258 case CMD_WRITE_STATUS:
259 sbsf->state = SF_WRITE_STATUS;
260 break;
Mike Frysingerb375ad92013-12-03 16:43:27 -0700261 default: {
Simon Glass5d8f0632014-09-15 06:33:19 -0600262 int flags = sbsf->data->flags;
Mike Frysingerb375ad92013-12-03 16:43:27 -0700263
Simon Glass5d8f0632014-09-15 06:33:19 -0600264 /* we only support erase here */
265 if (sbsf->cmd == CMD_ERASE_CHIP) {
266 sbsf->erase_size = sbsf->data->sector_size *
Jagan Teki49e65792016-10-30 23:16:15 +0530267 sbsf->data->n_sectors;
Simon Glass5d8f0632014-09-15 06:33:19 -0600268 } else if (sbsf->cmd == CMD_ERASE_4K && (flags & SECT_4K)) {
269 sbsf->erase_size = 4 << 10;
Jagan Tekifbf356b2016-08-08 17:19:08 +0530270 } else if (sbsf->cmd == CMD_ERASE_64K && !(flags & SECT_4K)) {
Simon Glass5d8f0632014-09-15 06:33:19 -0600271 sbsf->erase_size = 64 << 10;
272 } else {
273 debug(" cmd unknown: %#x\n", sbsf->cmd);
Simon Glass01862382014-10-13 23:42:08 -0600274 return -EIO;
Mike Frysingerb375ad92013-12-03 16:43:27 -0700275 }
Simon Glass5d8f0632014-09-15 06:33:19 -0600276 sbsf->state = SF_ADDR;
277 break;
Mike Frysingerb375ad92013-12-03 16:43:27 -0700278 }
279 }
280
281 if (oldstate != sbsf->state)
282 debug(" cmd: transition to %s state\n",
283 sandbox_sf_state_name(sbsf->state));
284
285 return 0;
286}
287
288int sandbox_erase_part(struct sandbox_spi_flash *sbsf, int size)
289{
290 int todo;
291 int ret;
292
293 while (size > 0) {
Masahiro Yamadadb204642014-11-07 03:03:31 +0900294 todo = min(size, (int)sizeof(sandbox_sf_0xff));
Mike Frysingerb375ad92013-12-03 16:43:27 -0700295 ret = os_write(sbsf->fd, sandbox_sf_0xff, todo);
296 if (ret != todo)
297 return ret;
298 size -= todo;
299 }
300
301 return 0;
302}
303
Simon Glass01862382014-10-13 23:42:08 -0600304static int sandbox_sf_xfer(struct udevice *dev, unsigned int bitlen,
305 const void *rxp, void *txp, unsigned long flags)
Mike Frysingerb375ad92013-12-03 16:43:27 -0700306{
Simon Glass01862382014-10-13 23:42:08 -0600307 struct sandbox_spi_flash *sbsf = dev_get_priv(dev);
308 const uint8_t *rx = rxp;
309 uint8_t *tx = txp;
Mike Frysingerb375ad92013-12-03 16:43:27 -0700310 uint cnt, pos = 0;
Simon Glass01862382014-10-13 23:42:08 -0600311 int bytes = bitlen / 8;
Mike Frysingerb375ad92013-12-03 16:43:27 -0700312 int ret;
313
314 debug("sandbox_sf: state:%x(%s) bytes:%u\n", sbsf->state,
315 sandbox_sf_state_name(sbsf->state), bytes);
316
Simon Glass01862382014-10-13 23:42:08 -0600317 if ((flags & SPI_XFER_BEGIN))
318 sandbox_sf_cs_activate(dev);
319
Mike Frysingerb375ad92013-12-03 16:43:27 -0700320 if (sbsf->state == SF_CMD) {
321 /* Figure out the initial state */
Simon Glass01862382014-10-13 23:42:08 -0600322 ret = sandbox_sf_process_cmd(sbsf, rx, tx);
323 if (ret)
324 return ret;
Mike Frysingerb375ad92013-12-03 16:43:27 -0700325 ++pos;
326 }
327
328 /* Process the remaining data */
329 while (pos < bytes) {
330 switch (sbsf->state) {
331 case SF_ID: {
332 u8 id;
333
334 debug(" id: off:%u tx:", sbsf->off);
Simon Glass5d8f0632014-09-15 06:33:19 -0600335 if (sbsf->off < IDCODE_LEN) {
336 /* Extract correct byte from ID 0x00aabbcc */
Jagan Tekib980b692016-10-30 23:16:12 +0530337 id = ((JEDEC_MFR(sbsf->data) << 16) |
338 JEDEC_ID(sbsf->data)) >>
Simon Glass5d8f0632014-09-15 06:33:19 -0600339 (8 * (IDCODE_LEN - 1 - sbsf->off));
340 } else {
Mike Frysingerb375ad92013-12-03 16:43:27 -0700341 id = 0;
Simon Glass5d8f0632014-09-15 06:33:19 -0600342 }
343 debug("%d %02x\n", sbsf->off, id);
Mike Frysingerb375ad92013-12-03 16:43:27 -0700344 tx[pos++] = id;
345 ++sbsf->off;
346 break;
347 }
348 case SF_ADDR:
349 debug(" addr: bytes:%u rx:%02x ", sbsf->addr_bytes,
350 rx[pos]);
351
352 if (sbsf->addr_bytes++ < SF_ADDR_LEN)
353 sbsf->off = (sbsf->off << 8) | rx[pos];
354 debug("addr:%06x\n", sbsf->off);
355
Simon Glass01862382014-10-13 23:42:08 -0600356 if (tx)
357 sandbox_spi_tristate(&tx[pos], 1);
358 pos++;
Mike Frysingerb375ad92013-12-03 16:43:27 -0700359
360 /* See if we're done processing */
361 if (sbsf->addr_bytes <
362 SF_ADDR_LEN + sbsf->pad_addr_bytes)
363 break;
364
365 /* Next state! */
366 if (os_lseek(sbsf->fd, sbsf->off, OS_SEEK_SET) < 0) {
367 puts("sandbox_sf: os_lseek() failed");
Simon Glass01862382014-10-13 23:42:08 -0600368 return -EIO;
Mike Frysingerb375ad92013-12-03 16:43:27 -0700369 }
370 switch (sbsf->cmd) {
371 case CMD_READ_ARRAY_FAST:
372 case CMD_READ_ARRAY_SLOW:
373 sbsf->state = SF_READ;
374 break;
375 case CMD_PAGE_PROGRAM:
376 sbsf->state = SF_WRITE;
377 break;
378 default:
379 /* assume erase state ... */
380 sbsf->state = SF_ERASE;
381 goto case_sf_erase;
382 }
383 debug(" cmd: transition to %s state\n",
384 sandbox_sf_state_name(sbsf->state));
385 break;
386 case SF_READ:
387 /*
388 * XXX: need to handle exotic behavior:
389 * - reading past end of device
390 */
391
392 cnt = bytes - pos;
393 debug(" tx: read(%u)\n", cnt);
Simon Glass01862382014-10-13 23:42:08 -0600394 assert(tx);
Mike Frysingerb375ad92013-12-03 16:43:27 -0700395 ret = os_read(sbsf->fd, tx + pos, cnt);
396 if (ret < 0) {
Simon Glass01862382014-10-13 23:42:08 -0600397 puts("sandbox_sf: os_read() failed\n");
398 return -EIO;
Mike Frysingerb375ad92013-12-03 16:43:27 -0700399 }
400 pos += ret;
401 break;
402 case SF_READ_STATUS:
403 debug(" read status: %#x\n", sbsf->status);
404 cnt = bytes - pos;
405 memset(tx + pos, sbsf->status, cnt);
406 pos += cnt;
407 break;
408 case SF_READ_STATUS1:
409 debug(" read status: %#x\n", sbsf->status);
410 cnt = bytes - pos;
411 memset(tx + pos, sbsf->status >> 8, cnt);
412 pos += cnt;
413 break;
Simon Glass01862382014-10-13 23:42:08 -0600414 case SF_WRITE_STATUS:
415 debug(" write status: %#x (ignored)\n", rx[pos]);
416 pos = bytes;
417 break;
Mike Frysingerb375ad92013-12-03 16:43:27 -0700418 case SF_WRITE:
419 /*
420 * XXX: need to handle exotic behavior:
421 * - unaligned addresses
422 * - more than a page (256) worth of data
423 * - reading past end of device
424 */
425 if (!(sbsf->status & STAT_WEL)) {
426 puts("sandbox_sf: write enable not set before write\n");
427 goto done;
428 }
429
430 cnt = bytes - pos;
431 debug(" rx: write(%u)\n", cnt);
Simon Glass01862382014-10-13 23:42:08 -0600432 if (tx)
433 sandbox_spi_tristate(&tx[pos], cnt);
Mike Frysingerb375ad92013-12-03 16:43:27 -0700434 ret = os_write(sbsf->fd, rx + pos, cnt);
435 if (ret < 0) {
436 puts("sandbox_spi: os_write() failed\n");
Simon Glass01862382014-10-13 23:42:08 -0600437 return -EIO;
Mike Frysingerb375ad92013-12-03 16:43:27 -0700438 }
439 pos += ret;
440 sbsf->status &= ~STAT_WEL;
441 break;
442 case SF_ERASE:
443 case_sf_erase: {
Mike Frysingerb375ad92013-12-03 16:43:27 -0700444 if (!(sbsf->status & STAT_WEL)) {
445 puts("sandbox_sf: write enable not set before erase\n");
446 goto done;
447 }
448
449 /* verify address is aligned */
Simon Glass5d8f0632014-09-15 06:33:19 -0600450 if (sbsf->off & (sbsf->erase_size - 1)) {
Mike Frysingerb375ad92013-12-03 16:43:27 -0700451 debug(" sector erase: cmd:%#x needs align:%#x, but we got %#x\n",
Simon Glass5d8f0632014-09-15 06:33:19 -0600452 sbsf->cmd, sbsf->erase_size,
Mike Frysingerb375ad92013-12-03 16:43:27 -0700453 sbsf->off);
454 sbsf->status &= ~STAT_WEL;
455 goto done;
456 }
457
Simon Glass5d8f0632014-09-15 06:33:19 -0600458 debug(" sector erase addr: %u, size: %u\n", sbsf->off,
459 sbsf->erase_size);
Mike Frysingerb375ad92013-12-03 16:43:27 -0700460
461 cnt = bytes - pos;
Simon Glass01862382014-10-13 23:42:08 -0600462 if (tx)
463 sandbox_spi_tristate(&tx[pos], cnt);
Mike Frysingerb375ad92013-12-03 16:43:27 -0700464 pos += cnt;
465
466 /*
467 * TODO(vapier@gentoo.org): latch WIP in status, and
468 * delay before clearing it ?
469 */
Simon Glass5d8f0632014-09-15 06:33:19 -0600470 ret = sandbox_erase_part(sbsf, sbsf->erase_size);
Mike Frysingerb375ad92013-12-03 16:43:27 -0700471 sbsf->status &= ~STAT_WEL;
472 if (ret) {
473 debug("sandbox_sf: Erase failed\n");
474 goto done;
475 }
476 goto done;
477 }
478 default:
479 debug(" ??? no idea what to do ???\n");
480 goto done;
481 }
482 }
483
484 done:
Simon Glass01862382014-10-13 23:42:08 -0600485 if (flags & SPI_XFER_END)
486 sandbox_sf_cs_deactivate(dev);
487 return pos == bytes ? 0 : -EIO;
488}
489
490int sandbox_sf_ofdata_to_platdata(struct udevice *dev)
491{
492 struct sandbox_spi_flash_plat_data *pdata = dev_get_platdata(dev);
Simon Glass01862382014-10-13 23:42:08 -0600493
Simon Glass11aa2af2017-05-18 20:09:58 -0600494 pdata->filename = dev_read_string(dev, "sandbox,filename");
495 pdata->device_name = dev_read_string(dev, "compatible");
Simon Glass01862382014-10-13 23:42:08 -0600496 if (!pdata->filename || !pdata->device_name) {
497 debug("%s: Missing properties, filename=%s, device_name=%s\n",
498 __func__, pdata->filename, pdata->device_name);
499 return -EINVAL;
500 }
501
502 return 0;
Mike Frysingerb375ad92013-12-03 16:43:27 -0700503}
504
Simon Glass01862382014-10-13 23:42:08 -0600505static const struct dm_spi_emul_ops sandbox_sf_emul_ops = {
Mike Frysingerb375ad92013-12-03 16:43:27 -0700506 .xfer = sandbox_sf_xfer,
507};
508
Simon Glass01862382014-10-13 23:42:08 -0600509#ifdef CONFIG_SPI_FLASH
Simon Glass01862382014-10-13 23:42:08 -0600510int sandbox_sf_bind_emul(struct sandbox_state *state, int busnum, int cs,
Simon Glassf3548632018-06-11 13:07:16 -0600511 struct udevice *bus, ofnode node, const char *spec)
Simon Glass01862382014-10-13 23:42:08 -0600512{
513 struct udevice *emul;
514 char name[20], *str;
515 struct driver *drv;
516 int ret;
517
518 /* now the emulator */
519 strncpy(name, spec, sizeof(name) - 6);
520 name[sizeof(name) - 6] = '\0';
521 strcat(name, "-emul");
Simon Glass01862382014-10-13 23:42:08 -0600522 drv = lists_driver_lookup_name("sandbox_sf_emul");
523 if (!drv) {
524 puts("Cannot find sandbox_sf_emul driver\n");
525 return -ENOENT;
526 }
Simon Glass18790eb2018-06-12 00:05:01 -0600527 str = strdup(name);
528 if (!str)
529 return -ENOMEM;
Simon Glassf3548632018-06-11 13:07:16 -0600530 ret = device_bind_ofnode(bus, drv, str, NULL, node, &emul);
Simon Glass01862382014-10-13 23:42:08 -0600531 if (ret) {
Simon Glass18790eb2018-06-12 00:05:01 -0600532 free(str);
Simon Glass01862382014-10-13 23:42:08 -0600533 printf("Cannot create emul device for spec '%s' (err=%d)\n",
534 spec, ret);
535 return ret;
536 }
537 state->spi[busnum][cs].emul = emul;
538
539 return 0;
540}
541
542void sandbox_sf_unbind_emul(struct sandbox_state *state, int busnum, int cs)
543{
Simon Glass5d2ee052015-01-25 08:27:12 -0700544 struct udevice *dev;
545
546 dev = state->spi[busnum][cs].emul;
Stefan Roese80b5bc92017-03-20 12:51:48 +0100547 device_remove(dev, DM_REMOVE_NORMAL);
Simon Glass5d2ee052015-01-25 08:27:12 -0700548 device_unbind(dev);
Simon Glass01862382014-10-13 23:42:08 -0600549 state->spi[busnum][cs].emul = NULL;
550}
551
Simon Glass01862382014-10-13 23:42:08 -0600552int sandbox_spi_get_emul(struct sandbox_state *state,
553 struct udevice *bus, struct udevice *slave,
554 struct udevice **emulp)
555{
556 struct sandbox_spi_info *info;
557 int busnum = bus->seq;
558 int cs = spi_chip_select(slave);
559 int ret;
560
561 info = &state->spi[busnum][cs];
562 if (!info->emul) {
563 /* Use the same device tree node as the SPI flash device */
564 debug("%s: busnum=%u, cs=%u: binding SPI flash emulation: ",
565 __func__, busnum, cs);
566 ret = sandbox_sf_bind_emul(state, busnum, cs, bus,
Simon Glassf3548632018-06-11 13:07:16 -0600567 dev_ofnode(slave), slave->name);
Simon Glass01862382014-10-13 23:42:08 -0600568 if (ret) {
569 debug("failed (err=%d)\n", ret);
570 return ret;
571 }
572 debug("OK\n");
573 }
574 *emulp = info->emul;
575
576 return 0;
577}
Simon Glass01862382014-10-13 23:42:08 -0600578#endif
579
580static const struct udevice_id sandbox_sf_ids[] = {
581 { .compatible = "sandbox,spi-flash" },
582 { }
583};
584
585U_BOOT_DRIVER(sandbox_sf_emul) = {
586 .name = "sandbox_sf_emul",
587 .id = UCLASS_SPI_EMUL,
588 .of_match = sandbox_sf_ids,
589 .ofdata_to_platdata = sandbox_sf_ofdata_to_platdata,
590 .probe = sandbox_sf_probe,
591 .remove = sandbox_sf_remove,
592 .priv_auto_alloc_size = sizeof(struct sandbox_spi_flash),
593 .platdata_auto_alloc_size = sizeof(struct sandbox_spi_flash_plat_data),
594 .ops = &sandbox_sf_emul_ops,
595};