blob: 514484eba06639628687ee66fc9c9dfcbe75e57b [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();
118 struct udevice *bus = dev->parent;
119 const char *spec = NULL;
120 int ret = 0;
121 int cs = -1;
122 int i;
Mike Frysingerb375ad92013-12-03 16:43:27 -0700123
Simon Glass01862382014-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 }
130 }
131 if (cs == -1) {
Simon Glassbb213e32015-05-04 11:31:10 -0600132 printf("Error: Unknown chip select for device '%s'\n",
Simon Glass01862382014-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) {
Simon Glassfd5259a2018-10-01 11:55:09 -0600139 printf("Error: No filename available\n");
140 return -EINVAL;
Mike Frysingerb375ad92013-12-03 16:43:27 -0700141 }
Simon Glassfd5259a2018-10-01 11:55:09 -0600142 spec = strchr(pdata->device_name, ',');
143 if (spec)
144 spec++;
145 else
146 spec = pdata->device_name;
147 idname_len = strlen(spec);
Simon Glass01862382014-10-13 23:42:08 -0600148 debug("%s: device='%s'\n", __func__, spec);
Mike Frysingerb375ad92013-12-03 16:43:27 -0700149
Jagan Teki77ae47b2016-10-30 23:16:10 +0530150 for (data = spi_flash_ids; data->name; data++) {
Mike Frysingerb375ad92013-12-03 16:43:27 -0700151 len = strlen(data->name);
152 if (idname_len != len)
153 continue;
Simon Glass01862382014-10-13 23:42:08 -0600154 if (!strncasecmp(spec, data->name, len))
Mike Frysingerb375ad92013-12-03 16:43:27 -0700155 break;
156 }
Simon Glass5d8f0632014-09-15 06:33:19 -0600157 if (!data->name) {
Simon Glassdb45d9e2016-02-24 09:14:54 -0700158 printf("%s: unknown flash '%*s'\n", __func__, (int)idname_len,
Mike Frysingerb375ad92013-12-03 16:43:27 -0700159 spec);
Simon Glass01862382014-10-13 23:42:08 -0600160 ret = -EINVAL;
Mike Frysingerb375ad92013-12-03 16:43:27 -0700161 goto error;
162 }
163
164 if (sandbox_sf_0xff[0] == 0x00)
165 memset(sandbox_sf_0xff, 0xff, sizeof(sandbox_sf_0xff));
166
Simon Glass01862382014-10-13 23:42:08 -0600167 sbsf->fd = os_open(pdata->filename, 02);
Mike Frysingerb375ad92013-12-03 16:43:27 -0700168 if (sbsf->fd == -1) {
Simon Glassdb45d9e2016-02-24 09:14:54 -0700169 printf("%s: unable to open file '%s'\n", __func__,
Simon Glass01862382014-10-13 23:42:08 -0600170 pdata->filename);
171 ret = -EIO;
Mike Frysingerb375ad92013-12-03 16:43:27 -0700172 goto error;
173 }
174
175 sbsf->data = data;
Simon Glass01862382014-10-13 23:42:08 -0600176 sbsf->cs = cs;
Mike Frysingerb375ad92013-12-03 16:43:27 -0700177
Mike Frysingerb375ad92013-12-03 16:43:27 -0700178 return 0;
179
180 error:
Simon Glassef3493d2015-01-25 08:27:09 -0700181 debug("%s: Got error %d\n", __func__, ret);
Simon Glass01862382014-10-13 23:42:08 -0600182 return ret;
Mike Frysingerb375ad92013-12-03 16:43:27 -0700183}
184
Simon Glass01862382014-10-13 23:42:08 -0600185static int sandbox_sf_remove(struct udevice *dev)
Mike Frysingerb375ad92013-12-03 16:43:27 -0700186{
Simon Glass01862382014-10-13 23:42:08 -0600187 struct sandbox_spi_flash *sbsf = dev_get_priv(dev);
Mike Frysingerb375ad92013-12-03 16:43:27 -0700188
189 os_close(sbsf->fd);
Simon Glass01862382014-10-13 23:42:08 -0600190
191 return 0;
Mike Frysingerb375ad92013-12-03 16:43:27 -0700192}
193
Simon Glass01862382014-10-13 23:42:08 -0600194static void sandbox_sf_cs_activate(struct udevice *dev)
Mike Frysingerb375ad92013-12-03 16:43:27 -0700195{
Simon Glass01862382014-10-13 23:42:08 -0600196 struct sandbox_spi_flash *sbsf = dev_get_priv(dev);
Mike Frysingerb375ad92013-12-03 16:43:27 -0700197
Simon Glass3b4b3402018-10-01 11:55:13 -0600198 log_content("sandbox_sf: CS activated; state is fresh!\n");
Mike Frysingerb375ad92013-12-03 16:43:27 -0700199
200 /* CS is asserted, so reset state */
201 sbsf->off = 0;
202 sbsf->addr_bytes = 0;
203 sbsf->pad_addr_bytes = 0;
204 sbsf->state = SF_CMD;
205 sbsf->cmd = SF_CMD;
206}
207
Simon Glass01862382014-10-13 23:42:08 -0600208static void sandbox_sf_cs_deactivate(struct udevice *dev)
Mike Frysingerb375ad92013-12-03 16:43:27 -0700209{
Simon Glass3b4b3402018-10-01 11:55:13 -0600210 log_content("sandbox_sf: CS deactivated; cmd done processing!\n");
Mike Frysingerb375ad92013-12-03 16:43:27 -0700211}
212
Simon Glass01862382014-10-13 23:42:08 -0600213/*
214 * There are times when the data lines are allowed to tristate. What
215 * is actually sensed on the line depends on the hardware. It could
216 * always be 0xFF/0x00 (if there are pull ups/downs), or things could
217 * float and so we'd get garbage back. This func encapsulates that
218 * scenario so we can worry about the details here.
219 */
220static void sandbox_spi_tristate(u8 *buf, uint len)
221{
222 /* XXX: make this into a user config option ? */
223 memset(buf, 0xff, len);
224}
225
Mike Frysingerb375ad92013-12-03 16:43:27 -0700226/* Figure out what command this stream is telling us to do */
227static int sandbox_sf_process_cmd(struct sandbox_spi_flash *sbsf, const u8 *rx,
228 u8 *tx)
229{
230 enum sandbox_sf_state oldstate = sbsf->state;
231
232 /* We need to output a byte for the cmd byte we just ate */
Simon Glass01862382014-10-13 23:42:08 -0600233 if (tx)
234 sandbox_spi_tristate(tx, 1);
Mike Frysingerb375ad92013-12-03 16:43:27 -0700235
236 sbsf->cmd = rx[0];
237 switch (sbsf->cmd) {
238 case CMD_READ_ID:
239 sbsf->state = SF_ID;
240 sbsf->cmd = SF_ID;
241 break;
242 case CMD_READ_ARRAY_FAST:
243 sbsf->pad_addr_bytes = 1;
244 case CMD_READ_ARRAY_SLOW:
245 case CMD_PAGE_PROGRAM:
Mike Frysingerb375ad92013-12-03 16:43:27 -0700246 sbsf->state = SF_ADDR;
247 break;
248 case CMD_WRITE_DISABLE:
249 debug(" write disabled\n");
250 sbsf->status &= ~STAT_WEL;
251 break;
252 case CMD_READ_STATUS:
253 sbsf->state = SF_READ_STATUS;
254 break;
255 case CMD_READ_STATUS1:
256 sbsf->state = SF_READ_STATUS1;
257 break;
258 case CMD_WRITE_ENABLE:
259 debug(" write enabled\n");
260 sbsf->status |= STAT_WEL;
261 break;
Simon Glass01862382014-10-13 23:42:08 -0600262 case CMD_WRITE_STATUS:
263 sbsf->state = SF_WRITE_STATUS;
264 break;
Mike Frysingerb375ad92013-12-03 16:43:27 -0700265 default: {
Simon Glass5d8f0632014-09-15 06:33:19 -0600266 int flags = sbsf->data->flags;
Mike Frysingerb375ad92013-12-03 16:43:27 -0700267
Simon Glass5d8f0632014-09-15 06:33:19 -0600268 /* we only support erase here */
269 if (sbsf->cmd == CMD_ERASE_CHIP) {
270 sbsf->erase_size = sbsf->data->sector_size *
Jagan Teki49e65792016-10-30 23:16:15 +0530271 sbsf->data->n_sectors;
Simon Glass5d8f0632014-09-15 06:33:19 -0600272 } else if (sbsf->cmd == CMD_ERASE_4K && (flags & SECT_4K)) {
273 sbsf->erase_size = 4 << 10;
Jagan Tekifbf356b2016-08-08 17:19:08 +0530274 } else if (sbsf->cmd == CMD_ERASE_64K && !(flags & SECT_4K)) {
Simon Glass5d8f0632014-09-15 06:33:19 -0600275 sbsf->erase_size = 64 << 10;
276 } else {
277 debug(" cmd unknown: %#x\n", sbsf->cmd);
Simon Glass01862382014-10-13 23:42:08 -0600278 return -EIO;
Mike Frysingerb375ad92013-12-03 16:43:27 -0700279 }
Simon Glass5d8f0632014-09-15 06:33:19 -0600280 sbsf->state = SF_ADDR;
281 break;
Mike Frysingerb375ad92013-12-03 16:43:27 -0700282 }
283 }
284
285 if (oldstate != sbsf->state)
Simon Glass3b4b3402018-10-01 11:55:13 -0600286 log_content(" cmd: transition to %s state\n",
287 sandbox_sf_state_name(sbsf->state));
Mike Frysingerb375ad92013-12-03 16:43:27 -0700288
289 return 0;
290}
291
292int sandbox_erase_part(struct sandbox_spi_flash *sbsf, int size)
293{
294 int todo;
295 int ret;
296
297 while (size > 0) {
Masahiro Yamadadb204642014-11-07 03:03:31 +0900298 todo = min(size, (int)sizeof(sandbox_sf_0xff));
Mike Frysingerb375ad92013-12-03 16:43:27 -0700299 ret = os_write(sbsf->fd, sandbox_sf_0xff, todo);
300 if (ret != todo)
301 return ret;
302 size -= todo;
303 }
304
305 return 0;
306}
307
Simon Glass01862382014-10-13 23:42:08 -0600308static int sandbox_sf_xfer(struct udevice *dev, unsigned int bitlen,
309 const void *rxp, void *txp, unsigned long flags)
Mike Frysingerb375ad92013-12-03 16:43:27 -0700310{
Simon Glass01862382014-10-13 23:42:08 -0600311 struct sandbox_spi_flash *sbsf = dev_get_priv(dev);
312 const uint8_t *rx = rxp;
313 uint8_t *tx = txp;
Mike Frysingerb375ad92013-12-03 16:43:27 -0700314 uint cnt, pos = 0;
Simon Glass01862382014-10-13 23:42:08 -0600315 int bytes = bitlen / 8;
Mike Frysingerb375ad92013-12-03 16:43:27 -0700316 int ret;
317
Simon Glass3b4b3402018-10-01 11:55:13 -0600318 log_content("sandbox_sf: state:%x(%s) bytes:%u\n", sbsf->state,
319 sandbox_sf_state_name(sbsf->state), bytes);
Mike Frysingerb375ad92013-12-03 16:43:27 -0700320
Simon Glass01862382014-10-13 23:42:08 -0600321 if ((flags & SPI_XFER_BEGIN))
322 sandbox_sf_cs_activate(dev);
323
Mike Frysingerb375ad92013-12-03 16:43:27 -0700324 if (sbsf->state == SF_CMD) {
325 /* Figure out the initial state */
Simon Glass01862382014-10-13 23:42:08 -0600326 ret = sandbox_sf_process_cmd(sbsf, rx, tx);
327 if (ret)
328 return ret;
Mike Frysingerb375ad92013-12-03 16:43:27 -0700329 ++pos;
330 }
331
332 /* Process the remaining data */
333 while (pos < bytes) {
334 switch (sbsf->state) {
335 case SF_ID: {
336 u8 id;
337
Simon Glass3b4b3402018-10-01 11:55:13 -0600338 log_content(" id: off:%u tx:", sbsf->off);
Simon Glass5d8f0632014-09-15 06:33:19 -0600339 if (sbsf->off < IDCODE_LEN) {
340 /* Extract correct byte from ID 0x00aabbcc */
Jagan Tekib980b692016-10-30 23:16:12 +0530341 id = ((JEDEC_MFR(sbsf->data) << 16) |
342 JEDEC_ID(sbsf->data)) >>
Simon Glass5d8f0632014-09-15 06:33:19 -0600343 (8 * (IDCODE_LEN - 1 - sbsf->off));
344 } else {
Mike Frysingerb375ad92013-12-03 16:43:27 -0700345 id = 0;
Simon Glass5d8f0632014-09-15 06:33:19 -0600346 }
Simon Glass3b4b3402018-10-01 11:55:13 -0600347 log_content("%d %02x\n", sbsf->off, id);
Mike Frysingerb375ad92013-12-03 16:43:27 -0700348 tx[pos++] = id;
349 ++sbsf->off;
350 break;
351 }
352 case SF_ADDR:
Simon Glass3b4b3402018-10-01 11:55:13 -0600353 log_content(" addr: bytes:%u rx:%02x ",
354 sbsf->addr_bytes, rx[pos]);
Mike Frysingerb375ad92013-12-03 16:43:27 -0700355
356 if (sbsf->addr_bytes++ < SF_ADDR_LEN)
357 sbsf->off = (sbsf->off << 8) | rx[pos];
Simon Glass3b4b3402018-10-01 11:55:13 -0600358 log_content("addr:%06x\n", sbsf->off);
Mike Frysingerb375ad92013-12-03 16:43:27 -0700359
Simon Glass01862382014-10-13 23:42:08 -0600360 if (tx)
361 sandbox_spi_tristate(&tx[pos], 1);
362 pos++;
Mike Frysingerb375ad92013-12-03 16:43:27 -0700363
364 /* See if we're done processing */
365 if (sbsf->addr_bytes <
366 SF_ADDR_LEN + sbsf->pad_addr_bytes)
367 break;
368
369 /* Next state! */
370 if (os_lseek(sbsf->fd, sbsf->off, OS_SEEK_SET) < 0) {
371 puts("sandbox_sf: os_lseek() failed");
Simon Glass01862382014-10-13 23:42:08 -0600372 return -EIO;
Mike Frysingerb375ad92013-12-03 16:43:27 -0700373 }
374 switch (sbsf->cmd) {
375 case CMD_READ_ARRAY_FAST:
376 case CMD_READ_ARRAY_SLOW:
377 sbsf->state = SF_READ;
378 break;
379 case CMD_PAGE_PROGRAM:
380 sbsf->state = SF_WRITE;
381 break;
382 default:
383 /* assume erase state ... */
384 sbsf->state = SF_ERASE;
385 goto case_sf_erase;
386 }
Simon Glass3b4b3402018-10-01 11:55:13 -0600387 log_content(" cmd: transition to %s state\n",
388 sandbox_sf_state_name(sbsf->state));
Mike Frysingerb375ad92013-12-03 16:43:27 -0700389 break;
390 case SF_READ:
391 /*
392 * XXX: need to handle exotic behavior:
393 * - reading past end of device
394 */
395
396 cnt = bytes - pos;
Simon Glass3b4b3402018-10-01 11:55:13 -0600397 log_content(" tx: read(%u)\n", cnt);
Simon Glass01862382014-10-13 23:42:08 -0600398 assert(tx);
Mike Frysingerb375ad92013-12-03 16:43:27 -0700399 ret = os_read(sbsf->fd, tx + pos, cnt);
400 if (ret < 0) {
Simon Glass01862382014-10-13 23:42:08 -0600401 puts("sandbox_sf: os_read() failed\n");
402 return -EIO;
Mike Frysingerb375ad92013-12-03 16:43:27 -0700403 }
404 pos += ret;
405 break;
406 case SF_READ_STATUS:
Simon Glass3b4b3402018-10-01 11:55:13 -0600407 log_content(" read status: %#x\n", sbsf->status);
Mike Frysingerb375ad92013-12-03 16:43:27 -0700408 cnt = bytes - pos;
409 memset(tx + pos, sbsf->status, cnt);
410 pos += cnt;
411 break;
412 case SF_READ_STATUS1:
Simon Glass3b4b3402018-10-01 11:55:13 -0600413 log_content(" read status: %#x\n", sbsf->status);
Mike Frysingerb375ad92013-12-03 16:43:27 -0700414 cnt = bytes - pos;
415 memset(tx + pos, sbsf->status >> 8, cnt);
416 pos += cnt;
417 break;
Simon Glass01862382014-10-13 23:42:08 -0600418 case SF_WRITE_STATUS:
Simon Glass3b4b3402018-10-01 11:55:13 -0600419 log_content(" write status: %#x (ignored)\n", rx[pos]);
Simon Glass01862382014-10-13 23:42:08 -0600420 pos = bytes;
421 break;
Mike Frysingerb375ad92013-12-03 16:43:27 -0700422 case SF_WRITE:
423 /*
424 * XXX: need to handle exotic behavior:
425 * - unaligned addresses
426 * - more than a page (256) worth of data
427 * - reading past end of device
428 */
429 if (!(sbsf->status & STAT_WEL)) {
430 puts("sandbox_sf: write enable not set before write\n");
431 goto done;
432 }
433
434 cnt = bytes - pos;
Simon Glass3b4b3402018-10-01 11:55:13 -0600435 log_content(" rx: write(%u)\n", cnt);
Simon Glass01862382014-10-13 23:42:08 -0600436 if (tx)
437 sandbox_spi_tristate(&tx[pos], cnt);
Mike Frysingerb375ad92013-12-03 16:43:27 -0700438 ret = os_write(sbsf->fd, rx + pos, cnt);
439 if (ret < 0) {
440 puts("sandbox_spi: os_write() failed\n");
Simon Glass01862382014-10-13 23:42:08 -0600441 return -EIO;
Mike Frysingerb375ad92013-12-03 16:43:27 -0700442 }
443 pos += ret;
444 sbsf->status &= ~STAT_WEL;
445 break;
446 case SF_ERASE:
447 case_sf_erase: {
Mike Frysingerb375ad92013-12-03 16:43:27 -0700448 if (!(sbsf->status & STAT_WEL)) {
449 puts("sandbox_sf: write enable not set before erase\n");
450 goto done;
451 }
452
453 /* verify address is aligned */
Simon Glass5d8f0632014-09-15 06:33:19 -0600454 if (sbsf->off & (sbsf->erase_size - 1)) {
Simon Glass3b4b3402018-10-01 11:55:13 -0600455 log_content(" sector erase: cmd:%#x needs align:%#x, but we got %#x\n",
456 sbsf->cmd, sbsf->erase_size,
457 sbsf->off);
Mike Frysingerb375ad92013-12-03 16:43:27 -0700458 sbsf->status &= ~STAT_WEL;
459 goto done;
460 }
461
Simon Glass3b4b3402018-10-01 11:55:13 -0600462 log_content(" sector erase addr: %u, size: %u\n",
463 sbsf->off, sbsf->erase_size);
Mike Frysingerb375ad92013-12-03 16:43:27 -0700464
465 cnt = bytes - pos;
Simon Glass01862382014-10-13 23:42:08 -0600466 if (tx)
467 sandbox_spi_tristate(&tx[pos], cnt);
Mike Frysingerb375ad92013-12-03 16:43:27 -0700468 pos += cnt;
469
470 /*
471 * TODO(vapier@gentoo.org): latch WIP in status, and
472 * delay before clearing it ?
473 */
Simon Glass5d8f0632014-09-15 06:33:19 -0600474 ret = sandbox_erase_part(sbsf, sbsf->erase_size);
Mike Frysingerb375ad92013-12-03 16:43:27 -0700475 sbsf->status &= ~STAT_WEL;
476 if (ret) {
Simon Glass3b4b3402018-10-01 11:55:13 -0600477 log_content("sandbox_sf: Erase failed\n");
Mike Frysingerb375ad92013-12-03 16:43:27 -0700478 goto done;
479 }
480 goto done;
481 }
482 default:
Simon Glass3b4b3402018-10-01 11:55:13 -0600483 log_content(" ??? no idea what to do ???\n");
Mike Frysingerb375ad92013-12-03 16:43:27 -0700484 goto done;
485 }
486 }
487
488 done:
Simon Glass01862382014-10-13 23:42:08 -0600489 if (flags & SPI_XFER_END)
490 sandbox_sf_cs_deactivate(dev);
491 return pos == bytes ? 0 : -EIO;
492}
493
494int sandbox_sf_ofdata_to_platdata(struct udevice *dev)
495{
496 struct sandbox_spi_flash_plat_data *pdata = dev_get_platdata(dev);
Simon Glass01862382014-10-13 23:42:08 -0600497
Simon Glass11aa2af2017-05-18 20:09:58 -0600498 pdata->filename = dev_read_string(dev, "sandbox,filename");
499 pdata->device_name = dev_read_string(dev, "compatible");
Simon Glass01862382014-10-13 23:42:08 -0600500 if (!pdata->filename || !pdata->device_name) {
501 debug("%s: Missing properties, filename=%s, device_name=%s\n",
502 __func__, pdata->filename, pdata->device_name);
503 return -EINVAL;
504 }
505
506 return 0;
Mike Frysingerb375ad92013-12-03 16:43:27 -0700507}
508
Simon Glass01862382014-10-13 23:42:08 -0600509static const struct dm_spi_emul_ops sandbox_sf_emul_ops = {
Mike Frysingerb375ad92013-12-03 16:43:27 -0700510 .xfer = sandbox_sf_xfer,
511};
512
Simon Glass01862382014-10-13 23:42:08 -0600513#ifdef CONFIG_SPI_FLASH
Simon Glass01862382014-10-13 23:42:08 -0600514int sandbox_sf_bind_emul(struct sandbox_state *state, int busnum, int cs,
Simon Glassf3548632018-06-11 13:07:16 -0600515 struct udevice *bus, ofnode node, const char *spec)
Simon Glass01862382014-10-13 23:42:08 -0600516{
517 struct udevice *emul;
518 char name[20], *str;
519 struct driver *drv;
520 int ret;
521
522 /* now the emulator */
523 strncpy(name, spec, sizeof(name) - 6);
524 name[sizeof(name) - 6] = '\0';
525 strcat(name, "-emul");
Simon Glass01862382014-10-13 23:42:08 -0600526 drv = lists_driver_lookup_name("sandbox_sf_emul");
527 if (!drv) {
528 puts("Cannot find sandbox_sf_emul driver\n");
529 return -ENOENT;
530 }
Simon Glass18790eb2018-06-12 00:05:01 -0600531 str = strdup(name);
532 if (!str)
533 return -ENOMEM;
Simon Glassf3548632018-06-11 13:07:16 -0600534 ret = device_bind_ofnode(bus, drv, str, NULL, node, &emul);
Simon Glass01862382014-10-13 23:42:08 -0600535 if (ret) {
Simon Glass18790eb2018-06-12 00:05:01 -0600536 free(str);
Simon Glass01862382014-10-13 23:42:08 -0600537 printf("Cannot create emul device for spec '%s' (err=%d)\n",
538 spec, ret);
539 return ret;
540 }
541 state->spi[busnum][cs].emul = emul;
542
543 return 0;
544}
545
546void sandbox_sf_unbind_emul(struct sandbox_state *state, int busnum, int cs)
547{
Simon Glass5d2ee052015-01-25 08:27:12 -0700548 struct udevice *dev;
549
550 dev = state->spi[busnum][cs].emul;
Stefan Roese80b5bc92017-03-20 12:51:48 +0100551 device_remove(dev, DM_REMOVE_NORMAL);
Simon Glass5d2ee052015-01-25 08:27:12 -0700552 device_unbind(dev);
Simon Glass01862382014-10-13 23:42:08 -0600553 state->spi[busnum][cs].emul = NULL;
554}
555
Simon Glass01862382014-10-13 23:42:08 -0600556int sandbox_spi_get_emul(struct sandbox_state *state,
557 struct udevice *bus, struct udevice *slave,
558 struct udevice **emulp)
559{
560 struct sandbox_spi_info *info;
561 int busnum = bus->seq;
562 int cs = spi_chip_select(slave);
563 int ret;
564
565 info = &state->spi[busnum][cs];
566 if (!info->emul) {
567 /* Use the same device tree node as the SPI flash device */
568 debug("%s: busnum=%u, cs=%u: binding SPI flash emulation: ",
569 __func__, busnum, cs);
570 ret = sandbox_sf_bind_emul(state, busnum, cs, bus,
Simon Glassf3548632018-06-11 13:07:16 -0600571 dev_ofnode(slave), slave->name);
Simon Glass01862382014-10-13 23:42:08 -0600572 if (ret) {
573 debug("failed (err=%d)\n", ret);
574 return ret;
575 }
576 debug("OK\n");
577 }
578 *emulp = info->emul;
579
580 return 0;
581}
Simon Glass01862382014-10-13 23:42:08 -0600582#endif
583
584static const struct udevice_id sandbox_sf_ids[] = {
585 { .compatible = "sandbox,spi-flash" },
586 { }
587};
588
589U_BOOT_DRIVER(sandbox_sf_emul) = {
590 .name = "sandbox_sf_emul",
591 .id = UCLASS_SPI_EMUL,
592 .of_match = sandbox_sf_ids,
593 .ofdata_to_platdata = sandbox_sf_ofdata_to_platdata,
594 .probe = sandbox_sf_probe,
595 .remove = sandbox_sf_remove,
596 .priv_auto_alloc_size = sizeof(struct sandbox_spi_flash),
597 .platdata_auto_alloc_size = sizeof(struct sandbox_spi_flash_plat_data),
598 .ops = &sandbox_sf_emul_ops,
599};