blob: 8cbe97ee2075a5acfa05f5bf656f2f8b3670e73f [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
Simon Glassc3aed5d2018-10-01 11:55:13 -060011#define LOG_CATEGORY UCLASS_SPI_FLASH
12
Mike Frysingerffdb20b2013-12-03 16:43:27 -070013#include <common.h>
Simon Glassb6c29562014-10-13 23:42:08 -060014#include <dm.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060015#include <log.h>
Mike Frysingerffdb20b2013-12-03 16:43:27 -070016#include <malloc.h>
17#include <spi.h>
18#include <os.h>
19
20#include <spi_flash.h>
21#include "sf_internal.h"
22
23#include <asm/getopt.h>
24#include <asm/spi.h>
25#include <asm/state.h>
Simon Glassb6c29562014-10-13 23:42:08 -060026#include <dm/device-internal.h>
27#include <dm/lists.h>
28#include <dm/uclass-internal.h>
29
Mike Frysingerffdb20b2013-12-03 16:43:27 -070030/*
31 * The different states that our SPI flash transitions between.
32 * We need to keep track of this across multiple xfer calls since
33 * the SPI bus could possibly call down into us multiple times.
34 */
35enum sandbox_sf_state {
36 SF_CMD, /* default state -- we're awaiting a command */
37 SF_ID, /* read the flash's (jedec) ID code */
38 SF_ADDR, /* processing the offset in the flash to read/etc... */
39 SF_READ, /* reading data from the flash */
40 SF_WRITE, /* writing data to the flash, i.e. page programming */
41 SF_ERASE, /* erase the flash */
42 SF_READ_STATUS, /* read the flash's status register */
43 SF_READ_STATUS1, /* read the flash's status register upper 8 bits*/
Simon Glassb6c29562014-10-13 23:42:08 -060044 SF_WRITE_STATUS, /* write the flash's status register */
Mike Frysingerffdb20b2013-12-03 16:43:27 -070045};
46
Simon Glassc3aed5d2018-10-01 11:55:13 -060047#if CONFIG_IS_ENABLED(LOG)
Mike Frysingerffdb20b2013-12-03 16:43:27 -070048static const char *sandbox_sf_state_name(enum sandbox_sf_state state)
49{
50 static const char * const states[] = {
51 "CMD", "ID", "ADDR", "READ", "WRITE", "ERASE", "READ_STATUS",
Simon Glassb6c29562014-10-13 23:42:08 -060052 "READ_STATUS1", "WRITE_STATUS",
Mike Frysingerffdb20b2013-12-03 16:43:27 -070053 };
54 return states[state];
55}
Simon Glassc3aed5d2018-10-01 11:55:13 -060056#endif /* LOG */
Mike Frysingerffdb20b2013-12-03 16:43:27 -070057
58/* Bits for the status register */
59#define STAT_WIP (1 << 0)
60#define STAT_WEL (1 << 1)
Simon Glassa58986c2018-11-06 15:21:41 -070061#define STAT_BP_SHIFT 2
62#define STAT_BP_MASK (7 << STAT_BP_SHIFT)
Mike Frysingerffdb20b2013-12-03 16:43:27 -070063
64/* Assume all SPI flashes have 3 byte addresses since they do atm */
65#define SF_ADDR_LEN 3
66
Simon Glass110bdee2014-09-15 06:33:19 -060067#define IDCODE_LEN 3
Mike Frysingerffdb20b2013-12-03 16:43:27 -070068
69/* Used to quickly bulk erase backing store */
70static u8 sandbox_sf_0xff[0x1000];
71
72/* Internal state data for each SPI flash */
73struct sandbox_spi_flash {
Simon Glassb6c29562014-10-13 23:42:08 -060074 unsigned int cs; /* Chip select we are attached to */
Mike Frysingerffdb20b2013-12-03 16:43:27 -070075 /*
76 * As we receive data over the SPI bus, our flash transitions
77 * between states. For example, we start off in the SF_CMD
78 * state where the first byte tells us what operation to perform
79 * (such as read or write the flash). But the operation itself
80 * can go through a few states such as first reading in the
81 * offset in the flash to perform the requested operation.
82 * Thus "state" stores the exact state that our machine is in
83 * while "cmd" stores the overall command we're processing.
84 */
85 enum sandbox_sf_state state;
86 uint cmd;
Simon Glass110bdee2014-09-15 06:33:19 -060087 /* Erase size of current erase command */
88 uint erase_size;
Mike Frysingerffdb20b2013-12-03 16:43:27 -070089 /* Current position in the flash; used when reading/writing/etc... */
90 uint off;
91 /* How many address bytes we've consumed */
92 uint addr_bytes, pad_addr_bytes;
93 /* The current flash status (see STAT_XXX defines above) */
94 u16 status;
95 /* Data describing the flash we're emulating */
Vignesh Rc4e88622019-02-05 11:29:23 +053096 const struct flash_info *data;
Mike Frysingerffdb20b2013-12-03 16:43:27 -070097 /* The file on disk to serv up data from */
98 int fd;
99};
100
Simon Glassb6c29562014-10-13 23:42:08 -0600101struct sandbox_spi_flash_plat_data {
102 const char *filename;
103 const char *device_name;
104 int bus;
105 int cs;
106};
107
Simon Glassa58986c2018-11-06 15:21:41 -0700108void sandbox_sf_set_block_protect(struct udevice *dev, int bp_mask)
109{
110 struct sandbox_spi_flash *sbsf = dev_get_priv(dev);
111
112 sbsf->status &= ~STAT_BP_MASK;
113 sbsf->status |= bp_mask << STAT_BP_SHIFT;
114}
115
Simon Glassb6c29562014-10-13 23:42:08 -0600116/**
117 * This is a very strange probe function. If it has platform data (which may
118 * have come from the device tree) then this function gets the filename and
Simon Glass1289e962018-10-01 11:55:09 -0600119 * device type from there.
Simon Glassb6c29562014-10-13 23:42:08 -0600120 */
121static int sandbox_sf_probe(struct udevice *dev)
Mike Frysingerffdb20b2013-12-03 16:43:27 -0700122{
123 /* spec = idcode:file */
Simon Glassb6c29562014-10-13 23:42:08 -0600124 struct sandbox_spi_flash *sbsf = dev_get_priv(dev);
Simon Glass110bdee2014-09-15 06:33:19 -0600125 size_t len, idname_len;
Vignesh Rc4e88622019-02-05 11:29:23 +0530126 const struct flash_info *data;
Simon Glassb6c29562014-10-13 23:42:08 -0600127 struct sandbox_spi_flash_plat_data *pdata = dev_get_platdata(dev);
128 struct sandbox_state *state = state_get_current();
Simon Glass66613f52018-10-01 11:55:19 -0600129 struct dm_spi_slave_platdata *slave_plat;
Simon Glassb6c29562014-10-13 23:42:08 -0600130 struct udevice *bus = dev->parent;
131 const char *spec = NULL;
Simon Glass66613f52018-10-01 11:55:19 -0600132 struct udevice *emul;
Simon Glassb6c29562014-10-13 23:42:08 -0600133 int ret = 0;
134 int cs = -1;
Mike Frysingerffdb20b2013-12-03 16:43:27 -0700135
Simon Glassb6c29562014-10-13 23:42:08 -0600136 debug("%s: bus %d, looking for emul=%p: ", __func__, bus->seq, dev);
Simon Glass66613f52018-10-01 11:55:19 -0600137 ret = sandbox_spi_get_emul(state, bus, dev, &emul);
138 if (ret) {
Simon Glass832adb22015-05-04 11:31:10 -0600139 printf("Error: Unknown chip select for device '%s'\n",
Simon Glass66613f52018-10-01 11:55:19 -0600140 dev->name);
141 return ret;
Simon Glassb6c29562014-10-13 23:42:08 -0600142 }
Simon Glass66613f52018-10-01 11:55:19 -0600143 slave_plat = dev_get_parent_platdata(dev);
144 cs = slave_plat->cs;
Simon Glassb6c29562014-10-13 23:42:08 -0600145 debug("found at cs %d\n", cs);
146
147 if (!pdata->filename) {
Simon Glass1289e962018-10-01 11:55:09 -0600148 printf("Error: No filename available\n");
149 return -EINVAL;
Simon Glassb6c29562014-10-13 23:42:08 -0600150 }
Simon Glass1289e962018-10-01 11:55:09 -0600151 spec = strchr(pdata->device_name, ',');
152 if (spec)
153 spec++;
154 else
155 spec = pdata->device_name;
156 idname_len = strlen(spec);
Simon Glassb6c29562014-10-13 23:42:08 -0600157 debug("%s: device='%s'\n", __func__, spec);
Mike Frysingerffdb20b2013-12-03 16:43:27 -0700158
Vignesh Rc4e88622019-02-05 11:29:23 +0530159 for (data = spi_nor_ids; data->name; data++) {
Mike Frysingerffdb20b2013-12-03 16:43:27 -0700160 len = strlen(data->name);
161 if (idname_len != len)
162 continue;
Simon Glassb6c29562014-10-13 23:42:08 -0600163 if (!strncasecmp(spec, data->name, len))
Mike Frysingerffdb20b2013-12-03 16:43:27 -0700164 break;
165 }
Simon Glass110bdee2014-09-15 06:33:19 -0600166 if (!data->name) {
Simon Glass20f655d2016-02-24 09:14:54 -0700167 printf("%s: unknown flash '%*s'\n", __func__, (int)idname_len,
Mike Frysingerffdb20b2013-12-03 16:43:27 -0700168 spec);
Simon Glassb6c29562014-10-13 23:42:08 -0600169 ret = -EINVAL;
Mike Frysingerffdb20b2013-12-03 16:43:27 -0700170 goto error;
171 }
172
173 if (sandbox_sf_0xff[0] == 0x00)
174 memset(sandbox_sf_0xff, 0xff, sizeof(sandbox_sf_0xff));
175
Simon Glassb6c29562014-10-13 23:42:08 -0600176 sbsf->fd = os_open(pdata->filename, 02);
Mike Frysingerffdb20b2013-12-03 16:43:27 -0700177 if (sbsf->fd == -1) {
Simon Glass20f655d2016-02-24 09:14:54 -0700178 printf("%s: unable to open file '%s'\n", __func__,
Simon Glassb6c29562014-10-13 23:42:08 -0600179 pdata->filename);
180 ret = -EIO;
Mike Frysingerffdb20b2013-12-03 16:43:27 -0700181 goto error;
182 }
183
184 sbsf->data = data;
Simon Glassb6c29562014-10-13 23:42:08 -0600185 sbsf->cs = cs;
Mike Frysingerffdb20b2013-12-03 16:43:27 -0700186
Mike Frysingerffdb20b2013-12-03 16:43:27 -0700187 return 0;
188
189 error:
Simon Glass1603bf32015-01-25 08:27:09 -0700190 debug("%s: Got error %d\n", __func__, ret);
Simon Glassb6c29562014-10-13 23:42:08 -0600191 return ret;
Mike Frysingerffdb20b2013-12-03 16:43:27 -0700192}
193
Simon Glassb6c29562014-10-13 23:42:08 -0600194static int sandbox_sf_remove(struct udevice *dev)
Mike Frysingerffdb20b2013-12-03 16:43:27 -0700195{
Simon Glassb6c29562014-10-13 23:42:08 -0600196 struct sandbox_spi_flash *sbsf = dev_get_priv(dev);
Mike Frysingerffdb20b2013-12-03 16:43:27 -0700197
198 os_close(sbsf->fd);
Simon Glassb6c29562014-10-13 23:42:08 -0600199
200 return 0;
Mike Frysingerffdb20b2013-12-03 16:43:27 -0700201}
202
Simon Glassb6c29562014-10-13 23:42:08 -0600203static void sandbox_sf_cs_activate(struct udevice *dev)
Mike Frysingerffdb20b2013-12-03 16:43:27 -0700204{
Simon Glassb6c29562014-10-13 23:42:08 -0600205 struct sandbox_spi_flash *sbsf = dev_get_priv(dev);
Mike Frysingerffdb20b2013-12-03 16:43:27 -0700206
Simon Glassc3aed5d2018-10-01 11:55:13 -0600207 log_content("sandbox_sf: CS activated; state is fresh!\n");
Mike Frysingerffdb20b2013-12-03 16:43:27 -0700208
209 /* CS is asserted, so reset state */
210 sbsf->off = 0;
211 sbsf->addr_bytes = 0;
212 sbsf->pad_addr_bytes = 0;
213 sbsf->state = SF_CMD;
214 sbsf->cmd = SF_CMD;
215}
216
Simon Glassb6c29562014-10-13 23:42:08 -0600217static void sandbox_sf_cs_deactivate(struct udevice *dev)
Mike Frysingerffdb20b2013-12-03 16:43:27 -0700218{
Simon Glassc3aed5d2018-10-01 11:55:13 -0600219 log_content("sandbox_sf: CS deactivated; cmd done processing!\n");
Mike Frysingerffdb20b2013-12-03 16:43:27 -0700220}
221
Simon Glassb6c29562014-10-13 23:42:08 -0600222/*
223 * There are times when the data lines are allowed to tristate. What
224 * is actually sensed on the line depends on the hardware. It could
225 * always be 0xFF/0x00 (if there are pull ups/downs), or things could
226 * float and so we'd get garbage back. This func encapsulates that
227 * scenario so we can worry about the details here.
228 */
229static void sandbox_spi_tristate(u8 *buf, uint len)
230{
231 /* XXX: make this into a user config option ? */
232 memset(buf, 0xff, len);
233}
234
Mike Frysingerffdb20b2013-12-03 16:43:27 -0700235/* Figure out what command this stream is telling us to do */
236static int sandbox_sf_process_cmd(struct sandbox_spi_flash *sbsf, const u8 *rx,
237 u8 *tx)
238{
239 enum sandbox_sf_state oldstate = sbsf->state;
240
241 /* We need to output a byte for the cmd byte we just ate */
Simon Glassb6c29562014-10-13 23:42:08 -0600242 if (tx)
243 sandbox_spi_tristate(tx, 1);
Mike Frysingerffdb20b2013-12-03 16:43:27 -0700244
245 sbsf->cmd = rx[0];
246 switch (sbsf->cmd) {
Vignesh Rc4e88622019-02-05 11:29:23 +0530247 case SPINOR_OP_RDID:
Mike Frysingerffdb20b2013-12-03 16:43:27 -0700248 sbsf->state = SF_ID;
249 sbsf->cmd = SF_ID;
250 break;
Vignesh Rc4e88622019-02-05 11:29:23 +0530251 case SPINOR_OP_READ_FAST:
Mike Frysingerffdb20b2013-12-03 16:43:27 -0700252 sbsf->pad_addr_bytes = 1;
Vignesh Rc4e88622019-02-05 11:29:23 +0530253 case SPINOR_OP_READ:
254 case SPINOR_OP_PP:
Mike Frysingerffdb20b2013-12-03 16:43:27 -0700255 sbsf->state = SF_ADDR;
256 break;
Vignesh Rc4e88622019-02-05 11:29:23 +0530257 case SPINOR_OP_WRDI:
Mike Frysingerffdb20b2013-12-03 16:43:27 -0700258 debug(" write disabled\n");
259 sbsf->status &= ~STAT_WEL;
260 break;
Vignesh Rc4e88622019-02-05 11:29:23 +0530261 case SPINOR_OP_RDSR:
Mike Frysingerffdb20b2013-12-03 16:43:27 -0700262 sbsf->state = SF_READ_STATUS;
263 break;
Vignesh Rc4e88622019-02-05 11:29:23 +0530264 case SPINOR_OP_RDSR2:
Mike Frysingerffdb20b2013-12-03 16:43:27 -0700265 sbsf->state = SF_READ_STATUS1;
266 break;
Vignesh Rc4e88622019-02-05 11:29:23 +0530267 case SPINOR_OP_WREN:
Mike Frysingerffdb20b2013-12-03 16:43:27 -0700268 debug(" write enabled\n");
269 sbsf->status |= STAT_WEL;
270 break;
Vignesh Rc4e88622019-02-05 11:29:23 +0530271 case SPINOR_OP_WRSR:
Simon Glassb6c29562014-10-13 23:42:08 -0600272 sbsf->state = SF_WRITE_STATUS;
273 break;
Mike Frysingerffdb20b2013-12-03 16:43:27 -0700274 default: {
Simon Glass110bdee2014-09-15 06:33:19 -0600275 int flags = sbsf->data->flags;
Mike Frysingerffdb20b2013-12-03 16:43:27 -0700276
Simon Glass110bdee2014-09-15 06:33:19 -0600277 /* we only support erase here */
Vignesh Rc4e88622019-02-05 11:29:23 +0530278 if (sbsf->cmd == SPINOR_OP_CHIP_ERASE) {
Simon Glass110bdee2014-09-15 06:33:19 -0600279 sbsf->erase_size = sbsf->data->sector_size *
Jagan Tekieccb6be2016-10-30 23:16:15 +0530280 sbsf->data->n_sectors;
Vignesh Rc4e88622019-02-05 11:29:23 +0530281 } else if (sbsf->cmd == SPINOR_OP_BE_4K && (flags & SECT_4K)) {
Simon Glass110bdee2014-09-15 06:33:19 -0600282 sbsf->erase_size = 4 << 10;
Vignesh Rc4e88622019-02-05 11:29:23 +0530283 } else if (sbsf->cmd == SPINOR_OP_SE && !(flags & SECT_4K)) {
Simon Glass110bdee2014-09-15 06:33:19 -0600284 sbsf->erase_size = 64 << 10;
285 } else {
286 debug(" cmd unknown: %#x\n", sbsf->cmd);
Simon Glassb6c29562014-10-13 23:42:08 -0600287 return -EIO;
Mike Frysingerffdb20b2013-12-03 16:43:27 -0700288 }
Simon Glass110bdee2014-09-15 06:33:19 -0600289 sbsf->state = SF_ADDR;
290 break;
Mike Frysingerffdb20b2013-12-03 16:43:27 -0700291 }
292 }
293
294 if (oldstate != sbsf->state)
Simon Glassc3aed5d2018-10-01 11:55:13 -0600295 log_content(" cmd: transition to %s state\n",
296 sandbox_sf_state_name(sbsf->state));
Mike Frysingerffdb20b2013-12-03 16:43:27 -0700297
298 return 0;
299}
300
301int sandbox_erase_part(struct sandbox_spi_flash *sbsf, int size)
302{
303 int todo;
304 int ret;
305
306 while (size > 0) {
Masahiro Yamadab4141192014-11-07 03:03:31 +0900307 todo = min(size, (int)sizeof(sandbox_sf_0xff));
Mike Frysingerffdb20b2013-12-03 16:43:27 -0700308 ret = os_write(sbsf->fd, sandbox_sf_0xff, todo);
309 if (ret != todo)
310 return ret;
311 size -= todo;
312 }
313
314 return 0;
315}
316
Simon Glassb6c29562014-10-13 23:42:08 -0600317static int sandbox_sf_xfer(struct udevice *dev, unsigned int bitlen,
318 const void *rxp, void *txp, unsigned long flags)
Mike Frysingerffdb20b2013-12-03 16:43:27 -0700319{
Simon Glassb6c29562014-10-13 23:42:08 -0600320 struct sandbox_spi_flash *sbsf = dev_get_priv(dev);
321 const uint8_t *rx = rxp;
322 uint8_t *tx = txp;
Mike Frysingerffdb20b2013-12-03 16:43:27 -0700323 uint cnt, pos = 0;
Simon Glassb6c29562014-10-13 23:42:08 -0600324 int bytes = bitlen / 8;
Mike Frysingerffdb20b2013-12-03 16:43:27 -0700325 int ret;
326
Simon Glassc3aed5d2018-10-01 11:55:13 -0600327 log_content("sandbox_sf: state:%x(%s) bytes:%u\n", sbsf->state,
328 sandbox_sf_state_name(sbsf->state), bytes);
Mike Frysingerffdb20b2013-12-03 16:43:27 -0700329
Simon Glassb6c29562014-10-13 23:42:08 -0600330 if ((flags & SPI_XFER_BEGIN))
331 sandbox_sf_cs_activate(dev);
332
Mike Frysingerffdb20b2013-12-03 16:43:27 -0700333 if (sbsf->state == SF_CMD) {
334 /* Figure out the initial state */
Simon Glassb6c29562014-10-13 23:42:08 -0600335 ret = sandbox_sf_process_cmd(sbsf, rx, tx);
336 if (ret)
337 return ret;
Mike Frysingerffdb20b2013-12-03 16:43:27 -0700338 ++pos;
339 }
340
341 /* Process the remaining data */
342 while (pos < bytes) {
343 switch (sbsf->state) {
344 case SF_ID: {
345 u8 id;
346
Simon Glassc3aed5d2018-10-01 11:55:13 -0600347 log_content(" id: off:%u tx:", sbsf->off);
Simon Glass110bdee2014-09-15 06:33:19 -0600348 if (sbsf->off < IDCODE_LEN) {
349 /* Extract correct byte from ID 0x00aabbcc */
Jagan Teki523b4e32016-10-30 23:16:12 +0530350 id = ((JEDEC_MFR(sbsf->data) << 16) |
351 JEDEC_ID(sbsf->data)) >>
Simon Glass110bdee2014-09-15 06:33:19 -0600352 (8 * (IDCODE_LEN - 1 - sbsf->off));
353 } else {
Mike Frysingerffdb20b2013-12-03 16:43:27 -0700354 id = 0;
Simon Glass110bdee2014-09-15 06:33:19 -0600355 }
Simon Glassc3aed5d2018-10-01 11:55:13 -0600356 log_content("%d %02x\n", sbsf->off, id);
Mike Frysingerffdb20b2013-12-03 16:43:27 -0700357 tx[pos++] = id;
358 ++sbsf->off;
359 break;
360 }
361 case SF_ADDR:
Simon Glassc3aed5d2018-10-01 11:55:13 -0600362 log_content(" addr: bytes:%u rx:%02x ",
363 sbsf->addr_bytes, rx[pos]);
Mike Frysingerffdb20b2013-12-03 16:43:27 -0700364
365 if (sbsf->addr_bytes++ < SF_ADDR_LEN)
366 sbsf->off = (sbsf->off << 8) | rx[pos];
Simon Glassc3aed5d2018-10-01 11:55:13 -0600367 log_content("addr:%06x\n", sbsf->off);
Mike Frysingerffdb20b2013-12-03 16:43:27 -0700368
Simon Glassb6c29562014-10-13 23:42:08 -0600369 if (tx)
370 sandbox_spi_tristate(&tx[pos], 1);
371 pos++;
Mike Frysingerffdb20b2013-12-03 16:43:27 -0700372
373 /* See if we're done processing */
374 if (sbsf->addr_bytes <
375 SF_ADDR_LEN + sbsf->pad_addr_bytes)
376 break;
377
378 /* Next state! */
379 if (os_lseek(sbsf->fd, sbsf->off, OS_SEEK_SET) < 0) {
380 puts("sandbox_sf: os_lseek() failed");
Simon Glassb6c29562014-10-13 23:42:08 -0600381 return -EIO;
Mike Frysingerffdb20b2013-12-03 16:43:27 -0700382 }
383 switch (sbsf->cmd) {
Vignesh Rc4e88622019-02-05 11:29:23 +0530384 case SPINOR_OP_READ_FAST:
385 case SPINOR_OP_READ:
Mike Frysingerffdb20b2013-12-03 16:43:27 -0700386 sbsf->state = SF_READ;
387 break;
Vignesh Rc4e88622019-02-05 11:29:23 +0530388 case SPINOR_OP_PP:
Mike Frysingerffdb20b2013-12-03 16:43:27 -0700389 sbsf->state = SF_WRITE;
390 break;
391 default:
392 /* assume erase state ... */
393 sbsf->state = SF_ERASE;
394 goto case_sf_erase;
395 }
Simon Glassc3aed5d2018-10-01 11:55:13 -0600396 log_content(" cmd: transition to %s state\n",
397 sandbox_sf_state_name(sbsf->state));
Mike Frysingerffdb20b2013-12-03 16:43:27 -0700398 break;
399 case SF_READ:
400 /*
401 * XXX: need to handle exotic behavior:
402 * - reading past end of device
403 */
404
405 cnt = bytes - pos;
Simon Glassc3aed5d2018-10-01 11:55:13 -0600406 log_content(" tx: read(%u)\n", cnt);
Simon Glassb6c29562014-10-13 23:42:08 -0600407 assert(tx);
Mike Frysingerffdb20b2013-12-03 16:43:27 -0700408 ret = os_read(sbsf->fd, tx + pos, cnt);
409 if (ret < 0) {
Simon Glassb6c29562014-10-13 23:42:08 -0600410 puts("sandbox_sf: os_read() failed\n");
411 return -EIO;
Mike Frysingerffdb20b2013-12-03 16:43:27 -0700412 }
413 pos += ret;
414 break;
415 case SF_READ_STATUS:
Simon Glassc3aed5d2018-10-01 11:55:13 -0600416 log_content(" read status: %#x\n", sbsf->status);
Mike Frysingerffdb20b2013-12-03 16:43:27 -0700417 cnt = bytes - pos;
418 memset(tx + pos, sbsf->status, cnt);
419 pos += cnt;
420 break;
421 case SF_READ_STATUS1:
Simon Glassc3aed5d2018-10-01 11:55:13 -0600422 log_content(" read status: %#x\n", sbsf->status);
Mike Frysingerffdb20b2013-12-03 16:43:27 -0700423 cnt = bytes - pos;
424 memset(tx + pos, sbsf->status >> 8, cnt);
425 pos += cnt;
426 break;
Simon Glassb6c29562014-10-13 23:42:08 -0600427 case SF_WRITE_STATUS:
Simon Glassc3aed5d2018-10-01 11:55:13 -0600428 log_content(" write status: %#x (ignored)\n", rx[pos]);
Simon Glassb6c29562014-10-13 23:42:08 -0600429 pos = bytes;
430 break;
Mike Frysingerffdb20b2013-12-03 16:43:27 -0700431 case SF_WRITE:
432 /*
433 * XXX: need to handle exotic behavior:
434 * - unaligned addresses
435 * - more than a page (256) worth of data
436 * - reading past end of device
437 */
438 if (!(sbsf->status & STAT_WEL)) {
439 puts("sandbox_sf: write enable not set before write\n");
440 goto done;
441 }
442
443 cnt = bytes - pos;
Simon Glassc3aed5d2018-10-01 11:55:13 -0600444 log_content(" rx: write(%u)\n", cnt);
Simon Glassb6c29562014-10-13 23:42:08 -0600445 if (tx)
446 sandbox_spi_tristate(&tx[pos], cnt);
Mike Frysingerffdb20b2013-12-03 16:43:27 -0700447 ret = os_write(sbsf->fd, rx + pos, cnt);
448 if (ret < 0) {
449 puts("sandbox_spi: os_write() failed\n");
Simon Glassb6c29562014-10-13 23:42:08 -0600450 return -EIO;
Mike Frysingerffdb20b2013-12-03 16:43:27 -0700451 }
452 pos += ret;
453 sbsf->status &= ~STAT_WEL;
454 break;
455 case SF_ERASE:
456 case_sf_erase: {
Mike Frysingerffdb20b2013-12-03 16:43:27 -0700457 if (!(sbsf->status & STAT_WEL)) {
458 puts("sandbox_sf: write enable not set before erase\n");
459 goto done;
460 }
461
462 /* verify address is aligned */
Simon Glass110bdee2014-09-15 06:33:19 -0600463 if (sbsf->off & (sbsf->erase_size - 1)) {
Simon Glassc3aed5d2018-10-01 11:55:13 -0600464 log_content(" sector erase: cmd:%#x needs align:%#x, but we got %#x\n",
465 sbsf->cmd, sbsf->erase_size,
466 sbsf->off);
Mike Frysingerffdb20b2013-12-03 16:43:27 -0700467 sbsf->status &= ~STAT_WEL;
468 goto done;
469 }
470
Simon Glassc3aed5d2018-10-01 11:55:13 -0600471 log_content(" sector erase addr: %u, size: %u\n",
472 sbsf->off, sbsf->erase_size);
Mike Frysingerffdb20b2013-12-03 16:43:27 -0700473
474 cnt = bytes - pos;
Simon Glassb6c29562014-10-13 23:42:08 -0600475 if (tx)
476 sandbox_spi_tristate(&tx[pos], cnt);
Mike Frysingerffdb20b2013-12-03 16:43:27 -0700477 pos += cnt;
478
479 /*
480 * TODO(vapier@gentoo.org): latch WIP in status, and
481 * delay before clearing it ?
482 */
Simon Glass110bdee2014-09-15 06:33:19 -0600483 ret = sandbox_erase_part(sbsf, sbsf->erase_size);
Mike Frysingerffdb20b2013-12-03 16:43:27 -0700484 sbsf->status &= ~STAT_WEL;
485 if (ret) {
Simon Glassc3aed5d2018-10-01 11:55:13 -0600486 log_content("sandbox_sf: Erase failed\n");
Mike Frysingerffdb20b2013-12-03 16:43:27 -0700487 goto done;
488 }
489 goto done;
490 }
491 default:
Simon Glassc3aed5d2018-10-01 11:55:13 -0600492 log_content(" ??? no idea what to do ???\n");
Mike Frysingerffdb20b2013-12-03 16:43:27 -0700493 goto done;
494 }
495 }
496
497 done:
Simon Glassb6c29562014-10-13 23:42:08 -0600498 if (flags & SPI_XFER_END)
499 sandbox_sf_cs_deactivate(dev);
500 return pos == bytes ? 0 : -EIO;
Mike Frysingerffdb20b2013-12-03 16:43:27 -0700501}
502
Simon Glassb6c29562014-10-13 23:42:08 -0600503int sandbox_sf_ofdata_to_platdata(struct udevice *dev)
504{
505 struct sandbox_spi_flash_plat_data *pdata = dev_get_platdata(dev);
Simon Glassb6c29562014-10-13 23:42:08 -0600506
Simon Glassa1181792017-05-18 20:09:58 -0600507 pdata->filename = dev_read_string(dev, "sandbox,filename");
508 pdata->device_name = dev_read_string(dev, "compatible");
Simon Glassb6c29562014-10-13 23:42:08 -0600509 if (!pdata->filename || !pdata->device_name) {
510 debug("%s: Missing properties, filename=%s, device_name=%s\n",
511 __func__, pdata->filename, pdata->device_name);
512 return -EINVAL;
513 }
514
515 return 0;
516}
517
518static const struct dm_spi_emul_ops sandbox_sf_emul_ops = {
Mike Frysingerffdb20b2013-12-03 16:43:27 -0700519 .xfer = sandbox_sf_xfer,
520};
521
Simon Glassb6c29562014-10-13 23:42:08 -0600522#ifdef CONFIG_SPI_FLASH
Simon Glassb6c29562014-10-13 23:42:08 -0600523int sandbox_sf_bind_emul(struct sandbox_state *state, int busnum, int cs,
Simon Glass008dcdd2018-06-11 13:07:16 -0600524 struct udevice *bus, ofnode node, const char *spec)
Simon Glassb6c29562014-10-13 23:42:08 -0600525{
526 struct udevice *emul;
527 char name[20], *str;
528 struct driver *drv;
529 int ret;
530
531 /* now the emulator */
532 strncpy(name, spec, sizeof(name) - 6);
533 name[sizeof(name) - 6] = '\0';
534 strcat(name, "-emul");
Simon Glassb6c29562014-10-13 23:42:08 -0600535 drv = lists_driver_lookup_name("sandbox_sf_emul");
536 if (!drv) {
537 puts("Cannot find sandbox_sf_emul driver\n");
538 return -ENOENT;
539 }
Simon Glassfb952832018-06-12 00:05:01 -0600540 str = strdup(name);
541 if (!str)
542 return -ENOMEM;
Simon Glass008dcdd2018-06-11 13:07:16 -0600543 ret = device_bind_ofnode(bus, drv, str, NULL, node, &emul);
Simon Glassb6c29562014-10-13 23:42:08 -0600544 if (ret) {
Simon Glassfb952832018-06-12 00:05:01 -0600545 free(str);
Simon Glassb6c29562014-10-13 23:42:08 -0600546 printf("Cannot create emul device for spec '%s' (err=%d)\n",
547 spec, ret);
548 return ret;
549 }
550 state->spi[busnum][cs].emul = emul;
551
552 return 0;
553}
554
555void sandbox_sf_unbind_emul(struct sandbox_state *state, int busnum, int cs)
556{
Simon Glassd0cff032015-01-25 08:27:12 -0700557 struct udevice *dev;
558
559 dev = state->spi[busnum][cs].emul;
Stefan Roese706865a2017-03-20 12:51:48 +0100560 device_remove(dev, DM_REMOVE_NORMAL);
Simon Glassd0cff032015-01-25 08:27:12 -0700561 device_unbind(dev);
Simon Glassb6c29562014-10-13 23:42:08 -0600562 state->spi[busnum][cs].emul = NULL;
563}
564
Simon Glassb6c29562014-10-13 23:42:08 -0600565int sandbox_spi_get_emul(struct sandbox_state *state,
566 struct udevice *bus, struct udevice *slave,
567 struct udevice **emulp)
568{
569 struct sandbox_spi_info *info;
570 int busnum = bus->seq;
571 int cs = spi_chip_select(slave);
572 int ret;
573
574 info = &state->spi[busnum][cs];
575 if (!info->emul) {
576 /* Use the same device tree node as the SPI flash device */
577 debug("%s: busnum=%u, cs=%u: binding SPI flash emulation: ",
578 __func__, busnum, cs);
579 ret = sandbox_sf_bind_emul(state, busnum, cs, bus,
Simon Glass008dcdd2018-06-11 13:07:16 -0600580 dev_ofnode(slave), slave->name);
Simon Glassb6c29562014-10-13 23:42:08 -0600581 if (ret) {
582 debug("failed (err=%d)\n", ret);
583 return ret;
584 }
585 debug("OK\n");
586 }
587 *emulp = info->emul;
588
589 return 0;
590}
Simon Glassb6c29562014-10-13 23:42:08 -0600591#endif
592
593static const struct udevice_id sandbox_sf_ids[] = {
594 { .compatible = "sandbox,spi-flash" },
595 { }
596};
597
598U_BOOT_DRIVER(sandbox_sf_emul) = {
599 .name = "sandbox_sf_emul",
600 .id = UCLASS_SPI_EMUL,
601 .of_match = sandbox_sf_ids,
602 .ofdata_to_platdata = sandbox_sf_ofdata_to_platdata,
603 .probe = sandbox_sf_probe,
604 .remove = sandbox_sf_remove,
605 .priv_auto_alloc_size = sizeof(struct sandbox_spi_flash),
606 .platdata_auto_alloc_size = sizeof(struct sandbox_spi_flash_plat_data),
607 .ops = &sandbox_sf_emul_ops,
608};