blob: 7fef754c6341c20a6d8919782a52887c79064048 [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
Simon Glass3b4b3402018-10-01 11:55:13 -060011#define LOG_CATEGORY UCLASS_SPI_FLASH
12
Mike Frysingerb375ad92013-12-03 16:43:27 -070013#include <common.h>
Simon Glass01862382014-10-13 23:42:08 -060014#include <dm.h>
Mike Frysingerb375ad92013-12-03 16:43:27 -070015#include <malloc.h>
16#include <spi.h>
17#include <os.h>
18
19#include <spi_flash.h>
20#include "sf_internal.h"
21
22#include <asm/getopt.h>
23#include <asm/spi.h>
24#include <asm/state.h>
Simon Glass01862382014-10-13 23:42:08 -060025#include <dm/device-internal.h>
26#include <dm/lists.h>
27#include <dm/uclass-internal.h>
28
Mike Frysingerb375ad92013-12-03 16:43:27 -070029/*
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 Glass01862382014-10-13 23:42:08 -060043 SF_WRITE_STATUS, /* write the flash's status register */
Mike Frysingerb375ad92013-12-03 16:43:27 -070044};
45
Simon Glass3b4b3402018-10-01 11:55:13 -060046#if CONFIG_IS_ENABLED(LOG)
Mike Frysingerb375ad92013-12-03 16:43:27 -070047static const char *sandbox_sf_state_name(enum sandbox_sf_state state)
48{
49 static const char * const states[] = {
50 "CMD", "ID", "ADDR", "READ", "WRITE", "ERASE", "READ_STATUS",
Simon Glass01862382014-10-13 23:42:08 -060051 "READ_STATUS1", "WRITE_STATUS",
Mike Frysingerb375ad92013-12-03 16:43:27 -070052 };
53 return states[state];
54}
Simon Glass3b4b3402018-10-01 11:55:13 -060055#endif /* LOG */
Mike Frysingerb375ad92013-12-03 16:43:27 -070056
57/* Bits for the status register */
58#define STAT_WIP (1 << 0)
59#define STAT_WEL (1 << 1)
60
61/* Assume all SPI flashes have 3 byte addresses since they do atm */
62#define SF_ADDR_LEN 3
63
Simon Glass5d8f0632014-09-15 06:33:19 -060064#define IDCODE_LEN 3
Mike Frysingerb375ad92013-12-03 16:43:27 -070065
66/* Used to quickly bulk erase backing store */
67static u8 sandbox_sf_0xff[0x1000];
68
69/* Internal state data for each SPI flash */
70struct sandbox_spi_flash {
Simon Glass01862382014-10-13 23:42:08 -060071 unsigned int cs; /* Chip select we are attached to */
Mike Frysingerb375ad92013-12-03 16:43:27 -070072 /*
73 * As we receive data over the SPI bus, our flash transitions
74 * between states. For example, we start off in the SF_CMD
75 * state where the first byte tells us what operation to perform
76 * (such as read or write the flash). But the operation itself
77 * can go through a few states such as first reading in the
78 * offset in the flash to perform the requested operation.
79 * Thus "state" stores the exact state that our machine is in
80 * while "cmd" stores the overall command we're processing.
81 */
82 enum sandbox_sf_state state;
83 uint cmd;
Simon Glass5d8f0632014-09-15 06:33:19 -060084 /* Erase size of current erase command */
85 uint erase_size;
Mike Frysingerb375ad92013-12-03 16:43:27 -070086 /* Current position in the flash; used when reading/writing/etc... */
87 uint off;
88 /* How many address bytes we've consumed */
89 uint addr_bytes, pad_addr_bytes;
90 /* The current flash status (see STAT_XXX defines above) */
91 u16 status;
92 /* Data describing the flash we're emulating */
Jagan Teki77ae47b2016-10-30 23:16:10 +053093 const struct spi_flash_info *data;
Mike Frysingerb375ad92013-12-03 16:43:27 -070094 /* The file on disk to serv up data from */
95 int fd;
96};
97
Simon Glass01862382014-10-13 23:42:08 -060098struct sandbox_spi_flash_plat_data {
99 const char *filename;
100 const char *device_name;
101 int bus;
102 int cs;
103};
104
105/**
106 * This is a very strange probe function. If it has platform data (which may
107 * have come from the device tree) then this function gets the filename and
Simon Glassfd5259a2018-10-01 11:55:09 -0600108 * device type from there.
Simon Glass01862382014-10-13 23:42:08 -0600109 */
110static int sandbox_sf_probe(struct udevice *dev)
Mike Frysingerb375ad92013-12-03 16:43:27 -0700111{
112 /* spec = idcode:file */
Simon Glass01862382014-10-13 23:42:08 -0600113 struct sandbox_spi_flash *sbsf = dev_get_priv(dev);
Simon Glass5d8f0632014-09-15 06:33:19 -0600114 size_t len, idname_len;
Jagan Teki77ae47b2016-10-30 23:16:10 +0530115 const struct spi_flash_info *data;
Simon Glass01862382014-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();
Simon Glass8e8963b2018-10-01 11:55:19 -0600118 struct dm_spi_slave_platdata *slave_plat;
Simon Glass01862382014-10-13 23:42:08 -0600119 struct udevice *bus = dev->parent;
120 const char *spec = NULL;
Simon Glass8e8963b2018-10-01 11:55:19 -0600121 struct udevice *emul;
Simon Glass01862382014-10-13 23:42:08 -0600122 int ret = 0;
123 int cs = -1;
Mike Frysingerb375ad92013-12-03 16:43:27 -0700124
Simon Glass01862382014-10-13 23:42:08 -0600125 debug("%s: bus %d, looking for emul=%p: ", __func__, bus->seq, dev);
Simon Glass8e8963b2018-10-01 11:55:19 -0600126 ret = sandbox_spi_get_emul(state, bus, dev, &emul);
127 if (ret) {
Simon Glassbb213e32015-05-04 11:31:10 -0600128 printf("Error: Unknown chip select for device '%s'\n",
Simon Glass8e8963b2018-10-01 11:55:19 -0600129 dev->name);
130 return ret;
Simon Glass01862382014-10-13 23:42:08 -0600131 }
Simon Glass8e8963b2018-10-01 11:55:19 -0600132 slave_plat = dev_get_parent_platdata(dev);
133 cs = slave_plat->cs;
Simon Glass01862382014-10-13 23:42:08 -0600134 debug("found at cs %d\n", cs);
135
136 if (!pdata->filename) {
Simon Glassfd5259a2018-10-01 11:55:09 -0600137 printf("Error: No filename available\n");
138 return -EINVAL;
Mike Frysingerb375ad92013-12-03 16:43:27 -0700139 }
Simon Glassfd5259a2018-10-01 11:55:09 -0600140 spec = strchr(pdata->device_name, ',');
141 if (spec)
142 spec++;
143 else
144 spec = pdata->device_name;
145 idname_len = strlen(spec);
Simon Glass01862382014-10-13 23:42:08 -0600146 debug("%s: device='%s'\n", __func__, spec);
Mike Frysingerb375ad92013-12-03 16:43:27 -0700147
Jagan Teki77ae47b2016-10-30 23:16:10 +0530148 for (data = spi_flash_ids; data->name; data++) {
Mike Frysingerb375ad92013-12-03 16:43:27 -0700149 len = strlen(data->name);
150 if (idname_len != len)
151 continue;
Simon Glass01862382014-10-13 23:42:08 -0600152 if (!strncasecmp(spec, data->name, len))
Mike Frysingerb375ad92013-12-03 16:43:27 -0700153 break;
154 }
Simon Glass5d8f0632014-09-15 06:33:19 -0600155 if (!data->name) {
Simon Glassdb45d9e2016-02-24 09:14:54 -0700156 printf("%s: unknown flash '%*s'\n", __func__, (int)idname_len,
Mike Frysingerb375ad92013-12-03 16:43:27 -0700157 spec);
Simon Glass01862382014-10-13 23:42:08 -0600158 ret = -EINVAL;
Mike Frysingerb375ad92013-12-03 16:43:27 -0700159 goto error;
160 }
161
162 if (sandbox_sf_0xff[0] == 0x00)
163 memset(sandbox_sf_0xff, 0xff, sizeof(sandbox_sf_0xff));
164
Simon Glass01862382014-10-13 23:42:08 -0600165 sbsf->fd = os_open(pdata->filename, 02);
Mike Frysingerb375ad92013-12-03 16:43:27 -0700166 if (sbsf->fd == -1) {
Simon Glassdb45d9e2016-02-24 09:14:54 -0700167 printf("%s: unable to open file '%s'\n", __func__,
Simon Glass01862382014-10-13 23:42:08 -0600168 pdata->filename);
169 ret = -EIO;
Mike Frysingerb375ad92013-12-03 16:43:27 -0700170 goto error;
171 }
172
173 sbsf->data = data;
Simon Glass01862382014-10-13 23:42:08 -0600174 sbsf->cs = cs;
Mike Frysingerb375ad92013-12-03 16:43:27 -0700175
Mike Frysingerb375ad92013-12-03 16:43:27 -0700176 return 0;
177
178 error:
Simon Glassef3493d2015-01-25 08:27:09 -0700179 debug("%s: Got error %d\n", __func__, ret);
Simon Glass01862382014-10-13 23:42:08 -0600180 return ret;
Mike Frysingerb375ad92013-12-03 16:43:27 -0700181}
182
Simon Glass01862382014-10-13 23:42:08 -0600183static int sandbox_sf_remove(struct udevice *dev)
Mike Frysingerb375ad92013-12-03 16:43:27 -0700184{
Simon Glass01862382014-10-13 23:42:08 -0600185 struct sandbox_spi_flash *sbsf = dev_get_priv(dev);
Mike Frysingerb375ad92013-12-03 16:43:27 -0700186
187 os_close(sbsf->fd);
Simon Glass01862382014-10-13 23:42:08 -0600188
189 return 0;
Mike Frysingerb375ad92013-12-03 16:43:27 -0700190}
191
Simon Glass01862382014-10-13 23:42:08 -0600192static void sandbox_sf_cs_activate(struct udevice *dev)
Mike Frysingerb375ad92013-12-03 16:43:27 -0700193{
Simon Glass01862382014-10-13 23:42:08 -0600194 struct sandbox_spi_flash *sbsf = dev_get_priv(dev);
Mike Frysingerb375ad92013-12-03 16:43:27 -0700195
Simon Glass3b4b3402018-10-01 11:55:13 -0600196 log_content("sandbox_sf: CS activated; state is fresh!\n");
Mike Frysingerb375ad92013-12-03 16:43:27 -0700197
198 /* CS is asserted, so reset state */
199 sbsf->off = 0;
200 sbsf->addr_bytes = 0;
201 sbsf->pad_addr_bytes = 0;
202 sbsf->state = SF_CMD;
203 sbsf->cmd = SF_CMD;
204}
205
Simon Glass01862382014-10-13 23:42:08 -0600206static void sandbox_sf_cs_deactivate(struct udevice *dev)
Mike Frysingerb375ad92013-12-03 16:43:27 -0700207{
Simon Glass3b4b3402018-10-01 11:55:13 -0600208 log_content("sandbox_sf: CS deactivated; cmd done processing!\n");
Mike Frysingerb375ad92013-12-03 16:43:27 -0700209}
210
Simon Glass01862382014-10-13 23:42:08 -0600211/*
212 * There are times when the data lines are allowed to tristate. What
213 * is actually sensed on the line depends on the hardware. It could
214 * always be 0xFF/0x00 (if there are pull ups/downs), or things could
215 * float and so we'd get garbage back. This func encapsulates that
216 * scenario so we can worry about the details here.
217 */
218static void sandbox_spi_tristate(u8 *buf, uint len)
219{
220 /* XXX: make this into a user config option ? */
221 memset(buf, 0xff, len);
222}
223
Mike Frysingerb375ad92013-12-03 16:43:27 -0700224/* Figure out what command this stream is telling us to do */
225static int sandbox_sf_process_cmd(struct sandbox_spi_flash *sbsf, const u8 *rx,
226 u8 *tx)
227{
228 enum sandbox_sf_state oldstate = sbsf->state;
229
230 /* We need to output a byte for the cmd byte we just ate */
Simon Glass01862382014-10-13 23:42:08 -0600231 if (tx)
232 sandbox_spi_tristate(tx, 1);
Mike Frysingerb375ad92013-12-03 16:43:27 -0700233
234 sbsf->cmd = rx[0];
235 switch (sbsf->cmd) {
236 case CMD_READ_ID:
237 sbsf->state = SF_ID;
238 sbsf->cmd = SF_ID;
239 break;
240 case CMD_READ_ARRAY_FAST:
241 sbsf->pad_addr_bytes = 1;
242 case CMD_READ_ARRAY_SLOW:
243 case CMD_PAGE_PROGRAM:
Mike Frysingerb375ad92013-12-03 16:43:27 -0700244 sbsf->state = SF_ADDR;
245 break;
246 case CMD_WRITE_DISABLE:
247 debug(" write disabled\n");
248 sbsf->status &= ~STAT_WEL;
249 break;
250 case CMD_READ_STATUS:
251 sbsf->state = SF_READ_STATUS;
252 break;
253 case CMD_READ_STATUS1:
254 sbsf->state = SF_READ_STATUS1;
255 break;
256 case CMD_WRITE_ENABLE:
257 debug(" write enabled\n");
258 sbsf->status |= STAT_WEL;
259 break;
Simon Glass01862382014-10-13 23:42:08 -0600260 case CMD_WRITE_STATUS:
261 sbsf->state = SF_WRITE_STATUS;
262 break;
Mike Frysingerb375ad92013-12-03 16:43:27 -0700263 default: {
Simon Glass5d8f0632014-09-15 06:33:19 -0600264 int flags = sbsf->data->flags;
Mike Frysingerb375ad92013-12-03 16:43:27 -0700265
Simon Glass5d8f0632014-09-15 06:33:19 -0600266 /* we only support erase here */
267 if (sbsf->cmd == CMD_ERASE_CHIP) {
268 sbsf->erase_size = sbsf->data->sector_size *
Jagan Teki49e65792016-10-30 23:16:15 +0530269 sbsf->data->n_sectors;
Simon Glass5d8f0632014-09-15 06:33:19 -0600270 } else if (sbsf->cmd == CMD_ERASE_4K && (flags & SECT_4K)) {
271 sbsf->erase_size = 4 << 10;
Jagan Tekifbf356b2016-08-08 17:19:08 +0530272 } else if (sbsf->cmd == CMD_ERASE_64K && !(flags & SECT_4K)) {
Simon Glass5d8f0632014-09-15 06:33:19 -0600273 sbsf->erase_size = 64 << 10;
274 } else {
275 debug(" cmd unknown: %#x\n", sbsf->cmd);
Simon Glass01862382014-10-13 23:42:08 -0600276 return -EIO;
Mike Frysingerb375ad92013-12-03 16:43:27 -0700277 }
Simon Glass5d8f0632014-09-15 06:33:19 -0600278 sbsf->state = SF_ADDR;
279 break;
Mike Frysingerb375ad92013-12-03 16:43:27 -0700280 }
281 }
282
283 if (oldstate != sbsf->state)
Simon Glass3b4b3402018-10-01 11:55:13 -0600284 log_content(" cmd: transition to %s state\n",
285 sandbox_sf_state_name(sbsf->state));
Mike Frysingerb375ad92013-12-03 16:43:27 -0700286
287 return 0;
288}
289
290int sandbox_erase_part(struct sandbox_spi_flash *sbsf, int size)
291{
292 int todo;
293 int ret;
294
295 while (size > 0) {
Masahiro Yamadadb204642014-11-07 03:03:31 +0900296 todo = min(size, (int)sizeof(sandbox_sf_0xff));
Mike Frysingerb375ad92013-12-03 16:43:27 -0700297 ret = os_write(sbsf->fd, sandbox_sf_0xff, todo);
298 if (ret != todo)
299 return ret;
300 size -= todo;
301 }
302
303 return 0;
304}
305
Simon Glass01862382014-10-13 23:42:08 -0600306static int sandbox_sf_xfer(struct udevice *dev, unsigned int bitlen,
307 const void *rxp, void *txp, unsigned long flags)
Mike Frysingerb375ad92013-12-03 16:43:27 -0700308{
Simon Glass01862382014-10-13 23:42:08 -0600309 struct sandbox_spi_flash *sbsf = dev_get_priv(dev);
310 const uint8_t *rx = rxp;
311 uint8_t *tx = txp;
Mike Frysingerb375ad92013-12-03 16:43:27 -0700312 uint cnt, pos = 0;
Simon Glass01862382014-10-13 23:42:08 -0600313 int bytes = bitlen / 8;
Mike Frysingerb375ad92013-12-03 16:43:27 -0700314 int ret;
315
Simon Glass3b4b3402018-10-01 11:55:13 -0600316 log_content("sandbox_sf: state:%x(%s) bytes:%u\n", sbsf->state,
317 sandbox_sf_state_name(sbsf->state), bytes);
Mike Frysingerb375ad92013-12-03 16:43:27 -0700318
Simon Glass01862382014-10-13 23:42:08 -0600319 if ((flags & SPI_XFER_BEGIN))
320 sandbox_sf_cs_activate(dev);
321
Mike Frysingerb375ad92013-12-03 16:43:27 -0700322 if (sbsf->state == SF_CMD) {
323 /* Figure out the initial state */
Simon Glass01862382014-10-13 23:42:08 -0600324 ret = sandbox_sf_process_cmd(sbsf, rx, tx);
325 if (ret)
326 return ret;
Mike Frysingerb375ad92013-12-03 16:43:27 -0700327 ++pos;
328 }
329
330 /* Process the remaining data */
331 while (pos < bytes) {
332 switch (sbsf->state) {
333 case SF_ID: {
334 u8 id;
335
Simon Glass3b4b3402018-10-01 11:55:13 -0600336 log_content(" id: off:%u tx:", sbsf->off);
Simon Glass5d8f0632014-09-15 06:33:19 -0600337 if (sbsf->off < IDCODE_LEN) {
338 /* Extract correct byte from ID 0x00aabbcc */
Jagan Tekib980b692016-10-30 23:16:12 +0530339 id = ((JEDEC_MFR(sbsf->data) << 16) |
340 JEDEC_ID(sbsf->data)) >>
Simon Glass5d8f0632014-09-15 06:33:19 -0600341 (8 * (IDCODE_LEN - 1 - sbsf->off));
342 } else {
Mike Frysingerb375ad92013-12-03 16:43:27 -0700343 id = 0;
Simon Glass5d8f0632014-09-15 06:33:19 -0600344 }
Simon Glass3b4b3402018-10-01 11:55:13 -0600345 log_content("%d %02x\n", sbsf->off, id);
Mike Frysingerb375ad92013-12-03 16:43:27 -0700346 tx[pos++] = id;
347 ++sbsf->off;
348 break;
349 }
350 case SF_ADDR:
Simon Glass3b4b3402018-10-01 11:55:13 -0600351 log_content(" addr: bytes:%u rx:%02x ",
352 sbsf->addr_bytes, rx[pos]);
Mike Frysingerb375ad92013-12-03 16:43:27 -0700353
354 if (sbsf->addr_bytes++ < SF_ADDR_LEN)
355 sbsf->off = (sbsf->off << 8) | rx[pos];
Simon Glass3b4b3402018-10-01 11:55:13 -0600356 log_content("addr:%06x\n", sbsf->off);
Mike Frysingerb375ad92013-12-03 16:43:27 -0700357
Simon Glass01862382014-10-13 23:42:08 -0600358 if (tx)
359 sandbox_spi_tristate(&tx[pos], 1);
360 pos++;
Mike Frysingerb375ad92013-12-03 16:43:27 -0700361
362 /* See if we're done processing */
363 if (sbsf->addr_bytes <
364 SF_ADDR_LEN + sbsf->pad_addr_bytes)
365 break;
366
367 /* Next state! */
368 if (os_lseek(sbsf->fd, sbsf->off, OS_SEEK_SET) < 0) {
369 puts("sandbox_sf: os_lseek() failed");
Simon Glass01862382014-10-13 23:42:08 -0600370 return -EIO;
Mike Frysingerb375ad92013-12-03 16:43:27 -0700371 }
372 switch (sbsf->cmd) {
373 case CMD_READ_ARRAY_FAST:
374 case CMD_READ_ARRAY_SLOW:
375 sbsf->state = SF_READ;
376 break;
377 case CMD_PAGE_PROGRAM:
378 sbsf->state = SF_WRITE;
379 break;
380 default:
381 /* assume erase state ... */
382 sbsf->state = SF_ERASE;
383 goto case_sf_erase;
384 }
Simon Glass3b4b3402018-10-01 11:55:13 -0600385 log_content(" cmd: transition to %s state\n",
386 sandbox_sf_state_name(sbsf->state));
Mike Frysingerb375ad92013-12-03 16:43:27 -0700387 break;
388 case SF_READ:
389 /*
390 * XXX: need to handle exotic behavior:
391 * - reading past end of device
392 */
393
394 cnt = bytes - pos;
Simon Glass3b4b3402018-10-01 11:55:13 -0600395 log_content(" tx: read(%u)\n", cnt);
Simon Glass01862382014-10-13 23:42:08 -0600396 assert(tx);
Mike Frysingerb375ad92013-12-03 16:43:27 -0700397 ret = os_read(sbsf->fd, tx + pos, cnt);
398 if (ret < 0) {
Simon Glass01862382014-10-13 23:42:08 -0600399 puts("sandbox_sf: os_read() failed\n");
400 return -EIO;
Mike Frysingerb375ad92013-12-03 16:43:27 -0700401 }
402 pos += ret;
403 break;
404 case SF_READ_STATUS:
Simon Glass3b4b3402018-10-01 11:55:13 -0600405 log_content(" read status: %#x\n", sbsf->status);
Mike Frysingerb375ad92013-12-03 16:43:27 -0700406 cnt = bytes - pos;
407 memset(tx + pos, sbsf->status, cnt);
408 pos += cnt;
409 break;
410 case SF_READ_STATUS1:
Simon Glass3b4b3402018-10-01 11:55:13 -0600411 log_content(" read status: %#x\n", sbsf->status);
Mike Frysingerb375ad92013-12-03 16:43:27 -0700412 cnt = bytes - pos;
413 memset(tx + pos, sbsf->status >> 8, cnt);
414 pos += cnt;
415 break;
Simon Glass01862382014-10-13 23:42:08 -0600416 case SF_WRITE_STATUS:
Simon Glass3b4b3402018-10-01 11:55:13 -0600417 log_content(" write status: %#x (ignored)\n", rx[pos]);
Simon Glass01862382014-10-13 23:42:08 -0600418 pos = bytes;
419 break;
Mike Frysingerb375ad92013-12-03 16:43:27 -0700420 case SF_WRITE:
421 /*
422 * XXX: need to handle exotic behavior:
423 * - unaligned addresses
424 * - more than a page (256) worth of data
425 * - reading past end of device
426 */
427 if (!(sbsf->status & STAT_WEL)) {
428 puts("sandbox_sf: write enable not set before write\n");
429 goto done;
430 }
431
432 cnt = bytes - pos;
Simon Glass3b4b3402018-10-01 11:55:13 -0600433 log_content(" rx: write(%u)\n", cnt);
Simon Glass01862382014-10-13 23:42:08 -0600434 if (tx)
435 sandbox_spi_tristate(&tx[pos], cnt);
Mike Frysingerb375ad92013-12-03 16:43:27 -0700436 ret = os_write(sbsf->fd, rx + pos, cnt);
437 if (ret < 0) {
438 puts("sandbox_spi: os_write() failed\n");
Simon Glass01862382014-10-13 23:42:08 -0600439 return -EIO;
Mike Frysingerb375ad92013-12-03 16:43:27 -0700440 }
441 pos += ret;
442 sbsf->status &= ~STAT_WEL;
443 break;
444 case SF_ERASE:
445 case_sf_erase: {
Mike Frysingerb375ad92013-12-03 16:43:27 -0700446 if (!(sbsf->status & STAT_WEL)) {
447 puts("sandbox_sf: write enable not set before erase\n");
448 goto done;
449 }
450
451 /* verify address is aligned */
Simon Glass5d8f0632014-09-15 06:33:19 -0600452 if (sbsf->off & (sbsf->erase_size - 1)) {
Simon Glass3b4b3402018-10-01 11:55:13 -0600453 log_content(" sector erase: cmd:%#x needs align:%#x, but we got %#x\n",
454 sbsf->cmd, sbsf->erase_size,
455 sbsf->off);
Mike Frysingerb375ad92013-12-03 16:43:27 -0700456 sbsf->status &= ~STAT_WEL;
457 goto done;
458 }
459
Simon Glass3b4b3402018-10-01 11:55:13 -0600460 log_content(" sector erase addr: %u, size: %u\n",
461 sbsf->off, sbsf->erase_size);
Mike Frysingerb375ad92013-12-03 16:43:27 -0700462
463 cnt = bytes - pos;
Simon Glass01862382014-10-13 23:42:08 -0600464 if (tx)
465 sandbox_spi_tristate(&tx[pos], cnt);
Mike Frysingerb375ad92013-12-03 16:43:27 -0700466 pos += cnt;
467
468 /*
469 * TODO(vapier@gentoo.org): latch WIP in status, and
470 * delay before clearing it ?
471 */
Simon Glass5d8f0632014-09-15 06:33:19 -0600472 ret = sandbox_erase_part(sbsf, sbsf->erase_size);
Mike Frysingerb375ad92013-12-03 16:43:27 -0700473 sbsf->status &= ~STAT_WEL;
474 if (ret) {
Simon Glass3b4b3402018-10-01 11:55:13 -0600475 log_content("sandbox_sf: Erase failed\n");
Mike Frysingerb375ad92013-12-03 16:43:27 -0700476 goto done;
477 }
478 goto done;
479 }
480 default:
Simon Glass3b4b3402018-10-01 11:55:13 -0600481 log_content(" ??? no idea what to do ???\n");
Mike Frysingerb375ad92013-12-03 16:43:27 -0700482 goto done;
483 }
484 }
485
486 done:
Simon Glass01862382014-10-13 23:42:08 -0600487 if (flags & SPI_XFER_END)
488 sandbox_sf_cs_deactivate(dev);
489 return pos == bytes ? 0 : -EIO;
490}
491
492int sandbox_sf_ofdata_to_platdata(struct udevice *dev)
493{
494 struct sandbox_spi_flash_plat_data *pdata = dev_get_platdata(dev);
Simon Glass01862382014-10-13 23:42:08 -0600495
Simon Glass11aa2af2017-05-18 20:09:58 -0600496 pdata->filename = dev_read_string(dev, "sandbox,filename");
497 pdata->device_name = dev_read_string(dev, "compatible");
Simon Glass01862382014-10-13 23:42:08 -0600498 if (!pdata->filename || !pdata->device_name) {
499 debug("%s: Missing properties, filename=%s, device_name=%s\n",
500 __func__, pdata->filename, pdata->device_name);
501 return -EINVAL;
502 }
503
504 return 0;
Mike Frysingerb375ad92013-12-03 16:43:27 -0700505}
506
Simon Glass01862382014-10-13 23:42:08 -0600507static const struct dm_spi_emul_ops sandbox_sf_emul_ops = {
Mike Frysingerb375ad92013-12-03 16:43:27 -0700508 .xfer = sandbox_sf_xfer,
509};
510
Simon Glass01862382014-10-13 23:42:08 -0600511#ifdef CONFIG_SPI_FLASH
Simon Glass01862382014-10-13 23:42:08 -0600512int sandbox_sf_bind_emul(struct sandbox_state *state, int busnum, int cs,
Simon Glassf3548632018-06-11 13:07:16 -0600513 struct udevice *bus, ofnode node, const char *spec)
Simon Glass01862382014-10-13 23:42:08 -0600514{
515 struct udevice *emul;
516 char name[20], *str;
517 struct driver *drv;
518 int ret;
519
520 /* now the emulator */
521 strncpy(name, spec, sizeof(name) - 6);
522 name[sizeof(name) - 6] = '\0';
523 strcat(name, "-emul");
Simon Glass01862382014-10-13 23:42:08 -0600524 drv = lists_driver_lookup_name("sandbox_sf_emul");
525 if (!drv) {
526 puts("Cannot find sandbox_sf_emul driver\n");
527 return -ENOENT;
528 }
Simon Glass18790eb2018-06-12 00:05:01 -0600529 str = strdup(name);
530 if (!str)
531 return -ENOMEM;
Simon Glassf3548632018-06-11 13:07:16 -0600532 ret = device_bind_ofnode(bus, drv, str, NULL, node, &emul);
Simon Glass01862382014-10-13 23:42:08 -0600533 if (ret) {
Simon Glass18790eb2018-06-12 00:05:01 -0600534 free(str);
Simon Glass01862382014-10-13 23:42:08 -0600535 printf("Cannot create emul device for spec '%s' (err=%d)\n",
536 spec, ret);
537 return ret;
538 }
539 state->spi[busnum][cs].emul = emul;
540
541 return 0;
542}
543
544void sandbox_sf_unbind_emul(struct sandbox_state *state, int busnum, int cs)
545{
Simon Glass5d2ee052015-01-25 08:27:12 -0700546 struct udevice *dev;
547
548 dev = state->spi[busnum][cs].emul;
Stefan Roese80b5bc92017-03-20 12:51:48 +0100549 device_remove(dev, DM_REMOVE_NORMAL);
Simon Glass5d2ee052015-01-25 08:27:12 -0700550 device_unbind(dev);
Simon Glass01862382014-10-13 23:42:08 -0600551 state->spi[busnum][cs].emul = NULL;
552}
553
Simon Glass01862382014-10-13 23:42:08 -0600554int sandbox_spi_get_emul(struct sandbox_state *state,
555 struct udevice *bus, struct udevice *slave,
556 struct udevice **emulp)
557{
558 struct sandbox_spi_info *info;
559 int busnum = bus->seq;
560 int cs = spi_chip_select(slave);
561 int ret;
562
563 info = &state->spi[busnum][cs];
564 if (!info->emul) {
565 /* Use the same device tree node as the SPI flash device */
566 debug("%s: busnum=%u, cs=%u: binding SPI flash emulation: ",
567 __func__, busnum, cs);
568 ret = sandbox_sf_bind_emul(state, busnum, cs, bus,
Simon Glassf3548632018-06-11 13:07:16 -0600569 dev_ofnode(slave), slave->name);
Simon Glass01862382014-10-13 23:42:08 -0600570 if (ret) {
571 debug("failed (err=%d)\n", ret);
572 return ret;
573 }
574 debug("OK\n");
575 }
576 *emulp = info->emul;
577
578 return 0;
579}
Simon Glass01862382014-10-13 23:42:08 -0600580#endif
581
582static const struct udevice_id sandbox_sf_ids[] = {
583 { .compatible = "sandbox,spi-flash" },
584 { }
585};
586
587U_BOOT_DRIVER(sandbox_sf_emul) = {
588 .name = "sandbox_sf_emul",
589 .id = UCLASS_SPI_EMUL,
590 .of_match = sandbox_sf_ids,
591 .ofdata_to_platdata = sandbox_sf_ofdata_to_platdata,
592 .probe = sandbox_sf_probe,
593 .remove = sandbox_sf_remove,
594 .priv_auto_alloc_size = sizeof(struct sandbox_spi_flash),
595 .platdata_auto_alloc_size = sizeof(struct sandbox_spi_flash_plat_data),
596 .ops = &sandbox_sf_emul_ops,
597};