blob: 740bd7541cb98b29b7995ce2e2a43005c3536705 [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>
Simon Glass0f2af882020-05-10 11:40:05 -060015#include <log.h>
Mike Frysingerb375ad92013-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 Glass01862382014-10-13 23:42:08 -060026#include <dm/device-internal.h>
27#include <dm/lists.h>
28#include <dm/uclass-internal.h>
29
Mike Frysingerb375ad92013-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 Glass01862382014-10-13 23:42:08 -060044 SF_WRITE_STATUS, /* write the flash's status register */
Mike Frysingerb375ad92013-12-03 16:43:27 -070045};
46
47static 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}
55
56/* Bits for the status register */
57#define STAT_WIP (1 << 0)
58#define STAT_WEL (1 << 1)
Simon Glass36eee8c2018-11-06 15:21:41 -070059#define STAT_BP_SHIFT 2
60#define STAT_BP_MASK (7 << STAT_BP_SHIFT)
Mike Frysingerb375ad92013-12-03 16:43:27 -070061
62/* Assume all SPI flashes have 3 byte addresses since they do atm */
63#define SF_ADDR_LEN 3
64
Simon Glass5d8f0632014-09-15 06:33:19 -060065#define IDCODE_LEN 3
Mike Frysingerb375ad92013-12-03 16:43:27 -070066
67/* Used to quickly bulk erase backing store */
68static u8 sandbox_sf_0xff[0x1000];
69
70/* Internal state data for each SPI flash */
71struct sandbox_spi_flash {
Simon Glass01862382014-10-13 23:42:08 -060072 unsigned int cs; /* Chip select we are attached to */
Mike Frysingerb375ad92013-12-03 16:43:27 -070073 /*
74 * As we receive data over the SPI bus, our flash transitions
75 * between states. For example, we start off in the SF_CMD
76 * state where the first byte tells us what operation to perform
77 * (such as read or write the flash). But the operation itself
78 * can go through a few states such as first reading in the
79 * offset in the flash to perform the requested operation.
80 * Thus "state" stores the exact state that our machine is in
81 * while "cmd" stores the overall command we're processing.
82 */
83 enum sandbox_sf_state state;
84 uint cmd;
Simon Glass5d8f0632014-09-15 06:33:19 -060085 /* Erase size of current erase command */
86 uint erase_size;
Mike Frysingerb375ad92013-12-03 16:43:27 -070087 /* Current position in the flash; used when reading/writing/etc... */
88 uint off;
89 /* How many address bytes we've consumed */
90 uint addr_bytes, pad_addr_bytes;
91 /* The current flash status (see STAT_XXX defines above) */
92 u16 status;
93 /* Data describing the flash we're emulating */
Vignesh R14510412019-02-05 11:29:23 +053094 const struct flash_info *data;
Mike Frysingerb375ad92013-12-03 16:43:27 -070095 /* The file on disk to serv up data from */
96 int fd;
97};
98
Simon Glass01862382014-10-13 23:42:08 -060099struct sandbox_spi_flash_plat_data {
100 const char *filename;
101 const char *device_name;
102 int bus;
103 int cs;
104};
105
Simon Glass36eee8c2018-11-06 15:21:41 -0700106void sandbox_sf_set_block_protect(struct udevice *dev, int bp_mask)
107{
108 struct sandbox_spi_flash *sbsf = dev_get_priv(dev);
109
110 sbsf->status &= ~STAT_BP_MASK;
111 sbsf->status |= bp_mask << STAT_BP_SHIFT;
112}
113
Simon Glass01862382014-10-13 23:42:08 -0600114/**
115 * This is a very strange probe function. If it has platform data (which may
116 * have come from the device tree) then this function gets the filename and
Simon Glassfd5259a2018-10-01 11:55:09 -0600117 * device type from there.
Simon Glass01862382014-10-13 23:42:08 -0600118 */
119static int sandbox_sf_probe(struct udevice *dev)
Mike Frysingerb375ad92013-12-03 16:43:27 -0700120{
121 /* spec = idcode:file */
Simon Glass01862382014-10-13 23:42:08 -0600122 struct sandbox_spi_flash *sbsf = dev_get_priv(dev);
Simon Glass5d8f0632014-09-15 06:33:19 -0600123 size_t len, idname_len;
Vignesh R14510412019-02-05 11:29:23 +0530124 const struct flash_info *data;
Simon Glassfa20e932020-12-03 16:55:20 -0700125 struct sandbox_spi_flash_plat_data *pdata = dev_get_plat(dev);
Simon Glass01862382014-10-13 23:42:08 -0600126 struct sandbox_state *state = state_get_current();
Simon Glassb75b15b2020-12-03 16:55:23 -0700127 struct dm_spi_slave_plat *slave_plat;
Simon Glass01862382014-10-13 23:42:08 -0600128 struct udevice *bus = dev->parent;
129 const char *spec = NULL;
Simon Glass8e8963b2018-10-01 11:55:19 -0600130 struct udevice *emul;
Simon Glass01862382014-10-13 23:42:08 -0600131 int ret = 0;
132 int cs = -1;
Mike Frysingerb375ad92013-12-03 16:43:27 -0700133
Simon Glass01862382014-10-13 23:42:08 -0600134 debug("%s: bus %d, looking for emul=%p: ", __func__, bus->seq, dev);
Simon Glass8e8963b2018-10-01 11:55:19 -0600135 ret = sandbox_spi_get_emul(state, bus, dev, &emul);
136 if (ret) {
Simon Glassbb213e32015-05-04 11:31:10 -0600137 printf("Error: Unknown chip select for device '%s'\n",
Simon Glass8e8963b2018-10-01 11:55:19 -0600138 dev->name);
139 return ret;
Simon Glass01862382014-10-13 23:42:08 -0600140 }
Simon Glass71fa5b42020-12-03 16:55:18 -0700141 slave_plat = dev_get_parent_plat(dev);
Simon Glass8e8963b2018-10-01 11:55:19 -0600142 cs = slave_plat->cs;
Simon Glass01862382014-10-13 23:42:08 -0600143 debug("found at cs %d\n", cs);
144
145 if (!pdata->filename) {
Simon Glassfd5259a2018-10-01 11:55:09 -0600146 printf("Error: No filename available\n");
147 return -EINVAL;
Mike Frysingerb375ad92013-12-03 16:43:27 -0700148 }
Simon Glassfd5259a2018-10-01 11:55:09 -0600149 spec = strchr(pdata->device_name, ',');
150 if (spec)
151 spec++;
152 else
153 spec = pdata->device_name;
154 idname_len = strlen(spec);
Simon Glass01862382014-10-13 23:42:08 -0600155 debug("%s: device='%s'\n", __func__, spec);
Mike Frysingerb375ad92013-12-03 16:43:27 -0700156
Vignesh R14510412019-02-05 11:29:23 +0530157 for (data = spi_nor_ids; data->name; data++) {
Mike Frysingerb375ad92013-12-03 16:43:27 -0700158 len = strlen(data->name);
159 if (idname_len != len)
160 continue;
Simon Glass01862382014-10-13 23:42:08 -0600161 if (!strncasecmp(spec, data->name, len))
Mike Frysingerb375ad92013-12-03 16:43:27 -0700162 break;
163 }
Simon Glass5d8f0632014-09-15 06:33:19 -0600164 if (!data->name) {
Simon Glassdb45d9e2016-02-24 09:14:54 -0700165 printf("%s: unknown flash '%*s'\n", __func__, (int)idname_len,
Mike Frysingerb375ad92013-12-03 16:43:27 -0700166 spec);
Simon Glass01862382014-10-13 23:42:08 -0600167 ret = -EINVAL;
Mike Frysingerb375ad92013-12-03 16:43:27 -0700168 goto error;
169 }
170
171 if (sandbox_sf_0xff[0] == 0x00)
172 memset(sandbox_sf_0xff, 0xff, sizeof(sandbox_sf_0xff));
173
Simon Glass01862382014-10-13 23:42:08 -0600174 sbsf->fd = os_open(pdata->filename, 02);
Mike Frysingerb375ad92013-12-03 16:43:27 -0700175 if (sbsf->fd == -1) {
Simon Glassdb45d9e2016-02-24 09:14:54 -0700176 printf("%s: unable to open file '%s'\n", __func__,
Simon Glass01862382014-10-13 23:42:08 -0600177 pdata->filename);
178 ret = -EIO;
Mike Frysingerb375ad92013-12-03 16:43:27 -0700179 goto error;
180 }
181
182 sbsf->data = data;
Simon Glass01862382014-10-13 23:42:08 -0600183 sbsf->cs = cs;
Mike Frysingerb375ad92013-12-03 16:43:27 -0700184
Mike Frysingerb375ad92013-12-03 16:43:27 -0700185 return 0;
186
187 error:
Simon Glassef3493d2015-01-25 08:27:09 -0700188 debug("%s: Got error %d\n", __func__, ret);
Simon Glass01862382014-10-13 23:42:08 -0600189 return ret;
Mike Frysingerb375ad92013-12-03 16:43:27 -0700190}
191
Simon Glass01862382014-10-13 23:42:08 -0600192static int sandbox_sf_remove(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
196 os_close(sbsf->fd);
Simon Glass01862382014-10-13 23:42:08 -0600197
198 return 0;
Mike Frysingerb375ad92013-12-03 16:43:27 -0700199}
200
Simon Glass01862382014-10-13 23:42:08 -0600201static void sandbox_sf_cs_activate(struct udevice *dev)
Mike Frysingerb375ad92013-12-03 16:43:27 -0700202{
Simon Glass01862382014-10-13 23:42:08 -0600203 struct sandbox_spi_flash *sbsf = dev_get_priv(dev);
Mike Frysingerb375ad92013-12-03 16:43:27 -0700204
Simon Glass3b4b3402018-10-01 11:55:13 -0600205 log_content("sandbox_sf: CS activated; state is fresh!\n");
Mike Frysingerb375ad92013-12-03 16:43:27 -0700206
207 /* CS is asserted, so reset state */
208 sbsf->off = 0;
209 sbsf->addr_bytes = 0;
210 sbsf->pad_addr_bytes = 0;
211 sbsf->state = SF_CMD;
212 sbsf->cmd = SF_CMD;
213}
214
Simon Glass01862382014-10-13 23:42:08 -0600215static void sandbox_sf_cs_deactivate(struct udevice *dev)
Mike Frysingerb375ad92013-12-03 16:43:27 -0700216{
Simon Glass3b4b3402018-10-01 11:55:13 -0600217 log_content("sandbox_sf: CS deactivated; cmd done processing!\n");
Mike Frysingerb375ad92013-12-03 16:43:27 -0700218}
219
Simon Glass01862382014-10-13 23:42:08 -0600220/*
221 * There are times when the data lines are allowed to tristate. What
222 * is actually sensed on the line depends on the hardware. It could
223 * always be 0xFF/0x00 (if there are pull ups/downs), or things could
224 * float and so we'd get garbage back. This func encapsulates that
225 * scenario so we can worry about the details here.
226 */
227static void sandbox_spi_tristate(u8 *buf, uint len)
228{
229 /* XXX: make this into a user config option ? */
230 memset(buf, 0xff, len);
231}
232
Mike Frysingerb375ad92013-12-03 16:43:27 -0700233/* Figure out what command this stream is telling us to do */
234static int sandbox_sf_process_cmd(struct sandbox_spi_flash *sbsf, const u8 *rx,
235 u8 *tx)
236{
237 enum sandbox_sf_state oldstate = sbsf->state;
238
239 /* We need to output a byte for the cmd byte we just ate */
Simon Glass01862382014-10-13 23:42:08 -0600240 if (tx)
241 sandbox_spi_tristate(tx, 1);
Mike Frysingerb375ad92013-12-03 16:43:27 -0700242
243 sbsf->cmd = rx[0];
244 switch (sbsf->cmd) {
Vignesh R14510412019-02-05 11:29:23 +0530245 case SPINOR_OP_RDID:
Mike Frysingerb375ad92013-12-03 16:43:27 -0700246 sbsf->state = SF_ID;
247 sbsf->cmd = SF_ID;
248 break;
Vignesh R14510412019-02-05 11:29:23 +0530249 case SPINOR_OP_READ_FAST:
Mike Frysingerb375ad92013-12-03 16:43:27 -0700250 sbsf->pad_addr_bytes = 1;
Vignesh R14510412019-02-05 11:29:23 +0530251 case SPINOR_OP_READ:
252 case SPINOR_OP_PP:
Mike Frysingerb375ad92013-12-03 16:43:27 -0700253 sbsf->state = SF_ADDR;
254 break;
Vignesh R14510412019-02-05 11:29:23 +0530255 case SPINOR_OP_WRDI:
Mike Frysingerb375ad92013-12-03 16:43:27 -0700256 debug(" write disabled\n");
257 sbsf->status &= ~STAT_WEL;
258 break;
Vignesh R14510412019-02-05 11:29:23 +0530259 case SPINOR_OP_RDSR:
Mike Frysingerb375ad92013-12-03 16:43:27 -0700260 sbsf->state = SF_READ_STATUS;
261 break;
Vignesh R14510412019-02-05 11:29:23 +0530262 case SPINOR_OP_RDSR2:
Mike Frysingerb375ad92013-12-03 16:43:27 -0700263 sbsf->state = SF_READ_STATUS1;
264 break;
Vignesh R14510412019-02-05 11:29:23 +0530265 case SPINOR_OP_WREN:
Mike Frysingerb375ad92013-12-03 16:43:27 -0700266 debug(" write enabled\n");
267 sbsf->status |= STAT_WEL;
268 break;
Vignesh R14510412019-02-05 11:29:23 +0530269 case SPINOR_OP_WRSR:
Simon Glass01862382014-10-13 23:42:08 -0600270 sbsf->state = SF_WRITE_STATUS;
271 break;
Mike Frysingerb375ad92013-12-03 16:43:27 -0700272 default: {
Simon Glass5d8f0632014-09-15 06:33:19 -0600273 int flags = sbsf->data->flags;
Mike Frysingerb375ad92013-12-03 16:43:27 -0700274
Simon Glass5d8f0632014-09-15 06:33:19 -0600275 /* we only support erase here */
Vignesh R14510412019-02-05 11:29:23 +0530276 if (sbsf->cmd == SPINOR_OP_CHIP_ERASE) {
Simon Glass5d8f0632014-09-15 06:33:19 -0600277 sbsf->erase_size = sbsf->data->sector_size *
Jagan Teki49e65792016-10-30 23:16:15 +0530278 sbsf->data->n_sectors;
Vignesh R14510412019-02-05 11:29:23 +0530279 } else if (sbsf->cmd == SPINOR_OP_BE_4K && (flags & SECT_4K)) {
Simon Glass5d8f0632014-09-15 06:33:19 -0600280 sbsf->erase_size = 4 << 10;
Vignesh R14510412019-02-05 11:29:23 +0530281 } else if (sbsf->cmd == SPINOR_OP_SE && !(flags & SECT_4K)) {
Simon Glass5d8f0632014-09-15 06:33:19 -0600282 sbsf->erase_size = 64 << 10;
283 } else {
284 debug(" cmd unknown: %#x\n", sbsf->cmd);
Simon Glass01862382014-10-13 23:42:08 -0600285 return -EIO;
Mike Frysingerb375ad92013-12-03 16:43:27 -0700286 }
Simon Glass5d8f0632014-09-15 06:33:19 -0600287 sbsf->state = SF_ADDR;
288 break;
Mike Frysingerb375ad92013-12-03 16:43:27 -0700289 }
290 }
291
292 if (oldstate != sbsf->state)
Simon Glass3b4b3402018-10-01 11:55:13 -0600293 log_content(" cmd: transition to %s state\n",
294 sandbox_sf_state_name(sbsf->state));
Mike Frysingerb375ad92013-12-03 16:43:27 -0700295
296 return 0;
297}
298
299int sandbox_erase_part(struct sandbox_spi_flash *sbsf, int size)
300{
301 int todo;
302 int ret;
303
304 while (size > 0) {
Masahiro Yamadadb204642014-11-07 03:03:31 +0900305 todo = min(size, (int)sizeof(sandbox_sf_0xff));
Mike Frysingerb375ad92013-12-03 16:43:27 -0700306 ret = os_write(sbsf->fd, sandbox_sf_0xff, todo);
307 if (ret != todo)
308 return ret;
309 size -= todo;
310 }
311
312 return 0;
313}
314
Simon Glass01862382014-10-13 23:42:08 -0600315static int sandbox_sf_xfer(struct udevice *dev, unsigned int bitlen,
316 const void *rxp, void *txp, unsigned long flags)
Mike Frysingerb375ad92013-12-03 16:43:27 -0700317{
Simon Glass01862382014-10-13 23:42:08 -0600318 struct sandbox_spi_flash *sbsf = dev_get_priv(dev);
319 const uint8_t *rx = rxp;
320 uint8_t *tx = txp;
Mike Frysingerb375ad92013-12-03 16:43:27 -0700321 uint cnt, pos = 0;
Simon Glass01862382014-10-13 23:42:08 -0600322 int bytes = bitlen / 8;
Mike Frysingerb375ad92013-12-03 16:43:27 -0700323 int ret;
324
Simon Glass3b4b3402018-10-01 11:55:13 -0600325 log_content("sandbox_sf: state:%x(%s) bytes:%u\n", sbsf->state,
326 sandbox_sf_state_name(sbsf->state), bytes);
Mike Frysingerb375ad92013-12-03 16:43:27 -0700327
Simon Glass01862382014-10-13 23:42:08 -0600328 if ((flags & SPI_XFER_BEGIN))
329 sandbox_sf_cs_activate(dev);
330
Mike Frysingerb375ad92013-12-03 16:43:27 -0700331 if (sbsf->state == SF_CMD) {
332 /* Figure out the initial state */
Simon Glass01862382014-10-13 23:42:08 -0600333 ret = sandbox_sf_process_cmd(sbsf, rx, tx);
334 if (ret)
335 return ret;
Mike Frysingerb375ad92013-12-03 16:43:27 -0700336 ++pos;
337 }
338
339 /* Process the remaining data */
340 while (pos < bytes) {
341 switch (sbsf->state) {
342 case SF_ID: {
343 u8 id;
344
Simon Glass3b4b3402018-10-01 11:55:13 -0600345 log_content(" id: off:%u tx:", sbsf->off);
Simon Glass5d8f0632014-09-15 06:33:19 -0600346 if (sbsf->off < IDCODE_LEN) {
347 /* Extract correct byte from ID 0x00aabbcc */
Jagan Tekib980b692016-10-30 23:16:12 +0530348 id = ((JEDEC_MFR(sbsf->data) << 16) |
349 JEDEC_ID(sbsf->data)) >>
Simon Glass5d8f0632014-09-15 06:33:19 -0600350 (8 * (IDCODE_LEN - 1 - sbsf->off));
351 } else {
Mike Frysingerb375ad92013-12-03 16:43:27 -0700352 id = 0;
Simon Glass5d8f0632014-09-15 06:33:19 -0600353 }
Simon Glass3b4b3402018-10-01 11:55:13 -0600354 log_content("%d %02x\n", sbsf->off, id);
Mike Frysingerb375ad92013-12-03 16:43:27 -0700355 tx[pos++] = id;
356 ++sbsf->off;
357 break;
358 }
359 case SF_ADDR:
Simon Glass3b4b3402018-10-01 11:55:13 -0600360 log_content(" addr: bytes:%u rx:%02x ",
361 sbsf->addr_bytes, rx[pos]);
Mike Frysingerb375ad92013-12-03 16:43:27 -0700362
363 if (sbsf->addr_bytes++ < SF_ADDR_LEN)
364 sbsf->off = (sbsf->off << 8) | rx[pos];
Simon Glass3b4b3402018-10-01 11:55:13 -0600365 log_content("addr:%06x\n", sbsf->off);
Mike Frysingerb375ad92013-12-03 16:43:27 -0700366
Simon Glass01862382014-10-13 23:42:08 -0600367 if (tx)
368 sandbox_spi_tristate(&tx[pos], 1);
369 pos++;
Mike Frysingerb375ad92013-12-03 16:43:27 -0700370
371 /* See if we're done processing */
372 if (sbsf->addr_bytes <
373 SF_ADDR_LEN + sbsf->pad_addr_bytes)
374 break;
375
376 /* Next state! */
377 if (os_lseek(sbsf->fd, sbsf->off, OS_SEEK_SET) < 0) {
378 puts("sandbox_sf: os_lseek() failed");
Simon Glass01862382014-10-13 23:42:08 -0600379 return -EIO;
Mike Frysingerb375ad92013-12-03 16:43:27 -0700380 }
381 switch (sbsf->cmd) {
Vignesh R14510412019-02-05 11:29:23 +0530382 case SPINOR_OP_READ_FAST:
383 case SPINOR_OP_READ:
Mike Frysingerb375ad92013-12-03 16:43:27 -0700384 sbsf->state = SF_READ;
385 break;
Vignesh R14510412019-02-05 11:29:23 +0530386 case SPINOR_OP_PP:
Mike Frysingerb375ad92013-12-03 16:43:27 -0700387 sbsf->state = SF_WRITE;
388 break;
389 default:
390 /* assume erase state ... */
391 sbsf->state = SF_ERASE;
392 goto case_sf_erase;
393 }
Simon Glass3b4b3402018-10-01 11:55:13 -0600394 log_content(" cmd: transition to %s state\n",
395 sandbox_sf_state_name(sbsf->state));
Mike Frysingerb375ad92013-12-03 16:43:27 -0700396 break;
397 case SF_READ:
398 /*
399 * XXX: need to handle exotic behavior:
400 * - reading past end of device
401 */
402
403 cnt = bytes - pos;
Simon Glass3b4b3402018-10-01 11:55:13 -0600404 log_content(" tx: read(%u)\n", cnt);
Simon Glass01862382014-10-13 23:42:08 -0600405 assert(tx);
Mike Frysingerb375ad92013-12-03 16:43:27 -0700406 ret = os_read(sbsf->fd, tx + pos, cnt);
407 if (ret < 0) {
Simon Glass01862382014-10-13 23:42:08 -0600408 puts("sandbox_sf: os_read() failed\n");
409 return -EIO;
Mike Frysingerb375ad92013-12-03 16:43:27 -0700410 }
411 pos += ret;
412 break;
413 case SF_READ_STATUS:
Simon Glass3b4b3402018-10-01 11:55:13 -0600414 log_content(" read status: %#x\n", sbsf->status);
Mike Frysingerb375ad92013-12-03 16:43:27 -0700415 cnt = bytes - pos;
416 memset(tx + pos, sbsf->status, cnt);
417 pos += cnt;
418 break;
419 case SF_READ_STATUS1:
Simon Glass3b4b3402018-10-01 11:55:13 -0600420 log_content(" read status: %#x\n", sbsf->status);
Mike Frysingerb375ad92013-12-03 16:43:27 -0700421 cnt = bytes - pos;
422 memset(tx + pos, sbsf->status >> 8, cnt);
423 pos += cnt;
424 break;
Simon Glass01862382014-10-13 23:42:08 -0600425 case SF_WRITE_STATUS:
Simon Glass3b4b3402018-10-01 11:55:13 -0600426 log_content(" write status: %#x (ignored)\n", rx[pos]);
Simon Glass01862382014-10-13 23:42:08 -0600427 pos = bytes;
428 break;
Mike Frysingerb375ad92013-12-03 16:43:27 -0700429 case SF_WRITE:
430 /*
431 * XXX: need to handle exotic behavior:
432 * - unaligned addresses
433 * - more than a page (256) worth of data
434 * - reading past end of device
435 */
436 if (!(sbsf->status & STAT_WEL)) {
437 puts("sandbox_sf: write enable not set before write\n");
438 goto done;
439 }
440
441 cnt = bytes - pos;
Simon Glass3b4b3402018-10-01 11:55:13 -0600442 log_content(" rx: write(%u)\n", cnt);
Simon Glass01862382014-10-13 23:42:08 -0600443 if (tx)
444 sandbox_spi_tristate(&tx[pos], cnt);
Mike Frysingerb375ad92013-12-03 16:43:27 -0700445 ret = os_write(sbsf->fd, rx + pos, cnt);
446 if (ret < 0) {
447 puts("sandbox_spi: os_write() failed\n");
Simon Glass01862382014-10-13 23:42:08 -0600448 return -EIO;
Mike Frysingerb375ad92013-12-03 16:43:27 -0700449 }
450 pos += ret;
451 sbsf->status &= ~STAT_WEL;
452 break;
453 case SF_ERASE:
454 case_sf_erase: {
Mike Frysingerb375ad92013-12-03 16:43:27 -0700455 if (!(sbsf->status & STAT_WEL)) {
456 puts("sandbox_sf: write enable not set before erase\n");
457 goto done;
458 }
459
460 /* verify address is aligned */
Simon Glass5d8f0632014-09-15 06:33:19 -0600461 if (sbsf->off & (sbsf->erase_size - 1)) {
Simon Glass3b4b3402018-10-01 11:55:13 -0600462 log_content(" sector erase: cmd:%#x needs align:%#x, but we got %#x\n",
463 sbsf->cmd, sbsf->erase_size,
464 sbsf->off);
Mike Frysingerb375ad92013-12-03 16:43:27 -0700465 sbsf->status &= ~STAT_WEL;
466 goto done;
467 }
468
Simon Glass3b4b3402018-10-01 11:55:13 -0600469 log_content(" sector erase addr: %u, size: %u\n",
470 sbsf->off, sbsf->erase_size);
Mike Frysingerb375ad92013-12-03 16:43:27 -0700471
472 cnt = bytes - pos;
Simon Glass01862382014-10-13 23:42:08 -0600473 if (tx)
474 sandbox_spi_tristate(&tx[pos], cnt);
Mike Frysingerb375ad92013-12-03 16:43:27 -0700475 pos += cnt;
476
477 /*
478 * TODO(vapier@gentoo.org): latch WIP in status, and
479 * delay before clearing it ?
480 */
Simon Glass5d8f0632014-09-15 06:33:19 -0600481 ret = sandbox_erase_part(sbsf, sbsf->erase_size);
Mike Frysingerb375ad92013-12-03 16:43:27 -0700482 sbsf->status &= ~STAT_WEL;
483 if (ret) {
Simon Glass3b4b3402018-10-01 11:55:13 -0600484 log_content("sandbox_sf: Erase failed\n");
Mike Frysingerb375ad92013-12-03 16:43:27 -0700485 goto done;
486 }
487 goto done;
488 }
489 default:
Simon Glass3b4b3402018-10-01 11:55:13 -0600490 log_content(" ??? no idea what to do ???\n");
Mike Frysingerb375ad92013-12-03 16:43:27 -0700491 goto done;
492 }
493 }
494
495 done:
Simon Glass01862382014-10-13 23:42:08 -0600496 if (flags & SPI_XFER_END)
497 sandbox_sf_cs_deactivate(dev);
498 return pos == bytes ? 0 : -EIO;
499}
500
Simon Glassaad29ae2020-12-03 16:55:21 -0700501int sandbox_sf_of_to_plat(struct udevice *dev)
Simon Glass01862382014-10-13 23:42:08 -0600502{
Simon Glassfa20e932020-12-03 16:55:20 -0700503 struct sandbox_spi_flash_plat_data *pdata = dev_get_plat(dev);
Simon Glass01862382014-10-13 23:42:08 -0600504
Simon Glass11aa2af2017-05-18 20:09:58 -0600505 pdata->filename = dev_read_string(dev, "sandbox,filename");
506 pdata->device_name = dev_read_string(dev, "compatible");
Simon Glass01862382014-10-13 23:42:08 -0600507 if (!pdata->filename || !pdata->device_name) {
508 debug("%s: Missing properties, filename=%s, device_name=%s\n",
509 __func__, pdata->filename, pdata->device_name);
510 return -EINVAL;
511 }
512
513 return 0;
Mike Frysingerb375ad92013-12-03 16:43:27 -0700514}
515
Simon Glass01862382014-10-13 23:42:08 -0600516static const struct dm_spi_emul_ops sandbox_sf_emul_ops = {
Mike Frysingerb375ad92013-12-03 16:43:27 -0700517 .xfer = sandbox_sf_xfer,
518};
519
Simon Glass01862382014-10-13 23:42:08 -0600520#ifdef CONFIG_SPI_FLASH
Simon Glass01862382014-10-13 23:42:08 -0600521int sandbox_sf_bind_emul(struct sandbox_state *state, int busnum, int cs,
Simon Glassf3548632018-06-11 13:07:16 -0600522 struct udevice *bus, ofnode node, const char *spec)
Simon Glass01862382014-10-13 23:42:08 -0600523{
524 struct udevice *emul;
525 char name[20], *str;
526 struct driver *drv;
527 int ret;
528
529 /* now the emulator */
530 strncpy(name, spec, sizeof(name) - 6);
531 name[sizeof(name) - 6] = '\0';
532 strcat(name, "-emul");
Simon Glass01862382014-10-13 23:42:08 -0600533 drv = lists_driver_lookup_name("sandbox_sf_emul");
534 if (!drv) {
535 puts("Cannot find sandbox_sf_emul driver\n");
536 return -ENOENT;
537 }
Simon Glass18790eb2018-06-12 00:05:01 -0600538 str = strdup(name);
539 if (!str)
540 return -ENOMEM;
Simon Glass884870f2020-11-28 17:50:01 -0700541 ret = device_bind(bus, drv, str, NULL, node, &emul);
Simon Glass01862382014-10-13 23:42:08 -0600542 if (ret) {
Simon Glass18790eb2018-06-12 00:05:01 -0600543 free(str);
Simon Glass01862382014-10-13 23:42:08 -0600544 printf("Cannot create emul device for spec '%s' (err=%d)\n",
545 spec, ret);
546 return ret;
547 }
548 state->spi[busnum][cs].emul = emul;
549
550 return 0;
551}
552
553void sandbox_sf_unbind_emul(struct sandbox_state *state, int busnum, int cs)
554{
Simon Glass5d2ee052015-01-25 08:27:12 -0700555 struct udevice *dev;
556
557 dev = state->spi[busnum][cs].emul;
Stefan Roese80b5bc92017-03-20 12:51:48 +0100558 device_remove(dev, DM_REMOVE_NORMAL);
Simon Glass5d2ee052015-01-25 08:27:12 -0700559 device_unbind(dev);
Simon Glass01862382014-10-13 23:42:08 -0600560 state->spi[busnum][cs].emul = NULL;
561}
562
Simon Glass01862382014-10-13 23:42:08 -0600563int sandbox_spi_get_emul(struct sandbox_state *state,
564 struct udevice *bus, struct udevice *slave,
565 struct udevice **emulp)
566{
567 struct sandbox_spi_info *info;
568 int busnum = bus->seq;
569 int cs = spi_chip_select(slave);
570 int ret;
571
572 info = &state->spi[busnum][cs];
573 if (!info->emul) {
574 /* Use the same device tree node as the SPI flash device */
575 debug("%s: busnum=%u, cs=%u: binding SPI flash emulation: ",
576 __func__, busnum, cs);
577 ret = sandbox_sf_bind_emul(state, busnum, cs, bus,
Simon Glassf3548632018-06-11 13:07:16 -0600578 dev_ofnode(slave), slave->name);
Simon Glass01862382014-10-13 23:42:08 -0600579 if (ret) {
580 debug("failed (err=%d)\n", ret);
581 return ret;
582 }
583 debug("OK\n");
584 }
585 *emulp = info->emul;
586
587 return 0;
588}
Simon Glass01862382014-10-13 23:42:08 -0600589#endif
590
591static const struct udevice_id sandbox_sf_ids[] = {
592 { .compatible = "sandbox,spi-flash" },
593 { }
594};
595
596U_BOOT_DRIVER(sandbox_sf_emul) = {
597 .name = "sandbox_sf_emul",
598 .id = UCLASS_SPI_EMUL,
599 .of_match = sandbox_sf_ids,
Simon Glassaad29ae2020-12-03 16:55:21 -0700600 .of_to_plat = sandbox_sf_of_to_plat,
Simon Glass01862382014-10-13 23:42:08 -0600601 .probe = sandbox_sf_probe,
602 .remove = sandbox_sf_remove,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700603 .priv_auto = sizeof(struct sandbox_spi_flash),
Simon Glass71fa5b42020-12-03 16:55:18 -0700604 .plat_auto = sizeof(struct sandbox_spi_flash_plat_data),
Simon Glass01862382014-10-13 23:42:08 -0600605 .ops = &sandbox_sf_emul_ops,
606};