Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Simulate a SPI flash |
| 3 | * |
| 4 | * Copyright (c) 2011-2013 The Chromium OS Authors. |
| 5 | * See file CREDITS for list of people who contributed to this |
| 6 | * project. |
| 7 | * |
| 8 | * Licensed under the GPL-2 or later. |
| 9 | */ |
| 10 | |
| 11 | #include <common.h> |
Simon Glass | b6c2956 | 2014-10-13 23:42:08 -0600 | [diff] [blame] | 12 | #include <dm.h> |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 13 | #include <malloc.h> |
| 14 | #include <spi.h> |
| 15 | #include <os.h> |
| 16 | |
| 17 | #include <spi_flash.h> |
| 18 | #include "sf_internal.h" |
| 19 | |
| 20 | #include <asm/getopt.h> |
| 21 | #include <asm/spi.h> |
| 22 | #include <asm/state.h> |
Simon Glass | b6c2956 | 2014-10-13 23:42:08 -0600 | [diff] [blame] | 23 | #include <dm/device-internal.h> |
| 24 | #include <dm/lists.h> |
| 25 | #include <dm/uclass-internal.h> |
| 26 | |
| 27 | DECLARE_GLOBAL_DATA_PTR; |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 28 | |
| 29 | /* |
| 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 | */ |
| 34 | enum 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 Glass | b6c2956 | 2014-10-13 23:42:08 -0600 | [diff] [blame] | 43 | SF_WRITE_STATUS, /* write the flash's status register */ |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 44 | }; |
| 45 | |
| 46 | static const char *sandbox_sf_state_name(enum sandbox_sf_state state) |
| 47 | { |
| 48 | static const char * const states[] = { |
| 49 | "CMD", "ID", "ADDR", "READ", "WRITE", "ERASE", "READ_STATUS", |
Simon Glass | b6c2956 | 2014-10-13 23:42:08 -0600 | [diff] [blame] | 50 | "READ_STATUS1", "WRITE_STATUS", |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 51 | }; |
| 52 | return states[state]; |
| 53 | } |
| 54 | |
| 55 | /* Bits for the status register */ |
| 56 | #define STAT_WIP (1 << 0) |
| 57 | #define STAT_WEL (1 << 1) |
| 58 | |
| 59 | /* Assume all SPI flashes have 3 byte addresses since they do atm */ |
| 60 | #define SF_ADDR_LEN 3 |
| 61 | |
Simon Glass | 110bdee | 2014-09-15 06:33:19 -0600 | [diff] [blame] | 62 | #define IDCODE_LEN 3 |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 63 | |
| 64 | /* Used to quickly bulk erase backing store */ |
| 65 | static u8 sandbox_sf_0xff[0x1000]; |
| 66 | |
| 67 | /* Internal state data for each SPI flash */ |
| 68 | struct sandbox_spi_flash { |
Simon Glass | b6c2956 | 2014-10-13 23:42:08 -0600 | [diff] [blame] | 69 | unsigned int cs; /* Chip select we are attached to */ |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 70 | /* |
| 71 | * As we receive data over the SPI bus, our flash transitions |
| 72 | * between states. For example, we start off in the SF_CMD |
| 73 | * state where the first byte tells us what operation to perform |
| 74 | * (such as read or write the flash). But the operation itself |
| 75 | * can go through a few states such as first reading in the |
| 76 | * offset in the flash to perform the requested operation. |
| 77 | * Thus "state" stores the exact state that our machine is in |
| 78 | * while "cmd" stores the overall command we're processing. |
| 79 | */ |
| 80 | enum sandbox_sf_state state; |
| 81 | uint cmd; |
Simon Glass | 110bdee | 2014-09-15 06:33:19 -0600 | [diff] [blame] | 82 | /* Erase size of current erase command */ |
| 83 | uint erase_size; |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 84 | /* Current position in the flash; used when reading/writing/etc... */ |
| 85 | uint off; |
| 86 | /* How many address bytes we've consumed */ |
| 87 | uint addr_bytes, pad_addr_bytes; |
| 88 | /* The current flash status (see STAT_XXX defines above) */ |
| 89 | u16 status; |
| 90 | /* Data describing the flash we're emulating */ |
Simon Glass | 110bdee | 2014-09-15 06:33:19 -0600 | [diff] [blame] | 91 | const struct spi_flash_params *data; |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 92 | /* The file on disk to serv up data from */ |
| 93 | int fd; |
| 94 | }; |
| 95 | |
Simon Glass | b6c2956 | 2014-10-13 23:42:08 -0600 | [diff] [blame] | 96 | struct sandbox_spi_flash_plat_data { |
| 97 | const char *filename; |
| 98 | const char *device_name; |
| 99 | int bus; |
| 100 | int cs; |
| 101 | }; |
| 102 | |
| 103 | /** |
| 104 | * This is a very strange probe function. If it has platform data (which may |
| 105 | * have come from the device tree) then this function gets the filename and |
| 106 | * device type from there. Failing that it looks at the command line |
| 107 | * parameter. |
| 108 | */ |
| 109 | static int sandbox_sf_probe(struct udevice *dev) |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 110 | { |
| 111 | /* spec = idcode:file */ |
Simon Glass | b6c2956 | 2014-10-13 23:42:08 -0600 | [diff] [blame] | 112 | struct sandbox_spi_flash *sbsf = dev_get_priv(dev); |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 113 | const char *file; |
Simon Glass | 110bdee | 2014-09-15 06:33:19 -0600 | [diff] [blame] | 114 | size_t len, idname_len; |
| 115 | const struct spi_flash_params *data; |
Simon Glass | b6c2956 | 2014-10-13 23:42:08 -0600 | [diff] [blame] | 116 | 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 Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 123 | |
Simon Glass | b6c2956 | 2014-10-13 23:42:08 -0600 | [diff] [blame] | 124 | 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 | } |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 130 | } |
Simon Glass | b6c2956 | 2014-10-13 23:42:08 -0600 | [diff] [blame] | 131 | if (cs == -1) { |
Simon Glass | 832adb2 | 2015-05-04 11:31:10 -0600 | [diff] [blame] | 132 | printf("Error: Unknown chip select for device '%s'\n", |
Simon Glass | b6c2956 | 2014-10-13 23:42:08 -0600 | [diff] [blame] | 133 | dev->name); |
| 134 | return -EINVAL; |
| 135 | } |
| 136 | debug("found at cs %d\n", cs); |
| 137 | |
| 138 | if (!pdata->filename) { |
| 139 | struct sandbox_state *state = state_get_current(); |
| 140 | |
| 141 | assert(bus->seq != -1); |
| 142 | if (bus->seq < CONFIG_SANDBOX_SPI_MAX_BUS) |
| 143 | spec = state->spi[bus->seq][cs].spec; |
Simon Glass | 1603bf3 | 2015-01-25 08:27:09 -0700 | [diff] [blame] | 144 | if (!spec) { |
Simon Glass | 20f655d | 2016-02-24 09:14:54 -0700 | [diff] [blame] | 145 | debug("%s: No spec found for bus %d, cs %d\n", |
| 146 | __func__, bus->seq, cs); |
Simon Glass | 1603bf3 | 2015-01-25 08:27:09 -0700 | [diff] [blame] | 147 | ret = -ENOENT; |
| 148 | goto error; |
| 149 | } |
Simon Glass | b6c2956 | 2014-10-13 23:42:08 -0600 | [diff] [blame] | 150 | |
| 151 | file = strchr(spec, ':'); |
| 152 | if (!file) { |
Simon Glass | 20f655d | 2016-02-24 09:14:54 -0700 | [diff] [blame] | 153 | printf("%s: unable to parse file\n", __func__); |
Simon Glass | b6c2956 | 2014-10-13 23:42:08 -0600 | [diff] [blame] | 154 | ret = -EINVAL; |
| 155 | goto error; |
| 156 | } |
| 157 | idname_len = file - spec; |
| 158 | pdata->filename = file + 1; |
| 159 | pdata->device_name = spec; |
| 160 | ++file; |
| 161 | } else { |
| 162 | spec = strchr(pdata->device_name, ','); |
| 163 | if (spec) |
| 164 | spec++; |
| 165 | else |
| 166 | spec = pdata->device_name; |
| 167 | idname_len = strlen(spec); |
| 168 | } |
| 169 | debug("%s: device='%s'\n", __func__, spec); |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 170 | |
Simon Glass | 110bdee | 2014-09-15 06:33:19 -0600 | [diff] [blame] | 171 | for (data = spi_flash_params_table; data->name; data++) { |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 172 | len = strlen(data->name); |
| 173 | if (idname_len != len) |
| 174 | continue; |
Simon Glass | b6c2956 | 2014-10-13 23:42:08 -0600 | [diff] [blame] | 175 | if (!strncasecmp(spec, data->name, len)) |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 176 | break; |
| 177 | } |
Simon Glass | 110bdee | 2014-09-15 06:33:19 -0600 | [diff] [blame] | 178 | if (!data->name) { |
Simon Glass | 20f655d | 2016-02-24 09:14:54 -0700 | [diff] [blame] | 179 | printf("%s: unknown flash '%*s'\n", __func__, (int)idname_len, |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 180 | spec); |
Simon Glass | b6c2956 | 2014-10-13 23:42:08 -0600 | [diff] [blame] | 181 | ret = -EINVAL; |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 182 | goto error; |
| 183 | } |
| 184 | |
| 185 | if (sandbox_sf_0xff[0] == 0x00) |
| 186 | memset(sandbox_sf_0xff, 0xff, sizeof(sandbox_sf_0xff)); |
| 187 | |
Simon Glass | b6c2956 | 2014-10-13 23:42:08 -0600 | [diff] [blame] | 188 | sbsf->fd = os_open(pdata->filename, 02); |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 189 | if (sbsf->fd == -1) { |
Simon Glass | 20f655d | 2016-02-24 09:14:54 -0700 | [diff] [blame] | 190 | printf("%s: unable to open file '%s'\n", __func__, |
Simon Glass | b6c2956 | 2014-10-13 23:42:08 -0600 | [diff] [blame] | 191 | pdata->filename); |
| 192 | ret = -EIO; |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 193 | goto error; |
| 194 | } |
| 195 | |
| 196 | sbsf->data = data; |
Simon Glass | b6c2956 | 2014-10-13 23:42:08 -0600 | [diff] [blame] | 197 | sbsf->cs = cs; |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 198 | |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 199 | return 0; |
| 200 | |
| 201 | error: |
Simon Glass | 1603bf3 | 2015-01-25 08:27:09 -0700 | [diff] [blame] | 202 | debug("%s: Got error %d\n", __func__, ret); |
Simon Glass | b6c2956 | 2014-10-13 23:42:08 -0600 | [diff] [blame] | 203 | return ret; |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 204 | } |
| 205 | |
Simon Glass | b6c2956 | 2014-10-13 23:42:08 -0600 | [diff] [blame] | 206 | static int sandbox_sf_remove(struct udevice *dev) |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 207 | { |
Simon Glass | b6c2956 | 2014-10-13 23:42:08 -0600 | [diff] [blame] | 208 | struct sandbox_spi_flash *sbsf = dev_get_priv(dev); |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 209 | |
| 210 | os_close(sbsf->fd); |
Simon Glass | b6c2956 | 2014-10-13 23:42:08 -0600 | [diff] [blame] | 211 | |
| 212 | return 0; |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 213 | } |
| 214 | |
Simon Glass | b6c2956 | 2014-10-13 23:42:08 -0600 | [diff] [blame] | 215 | static void sandbox_sf_cs_activate(struct udevice *dev) |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 216 | { |
Simon Glass | b6c2956 | 2014-10-13 23:42:08 -0600 | [diff] [blame] | 217 | struct sandbox_spi_flash *sbsf = dev_get_priv(dev); |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 218 | |
| 219 | debug("sandbox_sf: CS activated; state is fresh!\n"); |
| 220 | |
| 221 | /* CS is asserted, so reset state */ |
| 222 | sbsf->off = 0; |
| 223 | sbsf->addr_bytes = 0; |
| 224 | sbsf->pad_addr_bytes = 0; |
| 225 | sbsf->state = SF_CMD; |
| 226 | sbsf->cmd = SF_CMD; |
| 227 | } |
| 228 | |
Simon Glass | b6c2956 | 2014-10-13 23:42:08 -0600 | [diff] [blame] | 229 | static void sandbox_sf_cs_deactivate(struct udevice *dev) |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 230 | { |
| 231 | debug("sandbox_sf: CS deactivated; cmd done processing!\n"); |
| 232 | } |
| 233 | |
Simon Glass | b6c2956 | 2014-10-13 23:42:08 -0600 | [diff] [blame] | 234 | /* |
| 235 | * There are times when the data lines are allowed to tristate. What |
| 236 | * is actually sensed on the line depends on the hardware. It could |
| 237 | * always be 0xFF/0x00 (if there are pull ups/downs), or things could |
| 238 | * float and so we'd get garbage back. This func encapsulates that |
| 239 | * scenario so we can worry about the details here. |
| 240 | */ |
| 241 | static void sandbox_spi_tristate(u8 *buf, uint len) |
| 242 | { |
| 243 | /* XXX: make this into a user config option ? */ |
| 244 | memset(buf, 0xff, len); |
| 245 | } |
| 246 | |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 247 | /* Figure out what command this stream is telling us to do */ |
| 248 | static int sandbox_sf_process_cmd(struct sandbox_spi_flash *sbsf, const u8 *rx, |
| 249 | u8 *tx) |
| 250 | { |
| 251 | enum sandbox_sf_state oldstate = sbsf->state; |
| 252 | |
| 253 | /* We need to output a byte for the cmd byte we just ate */ |
Simon Glass | b6c2956 | 2014-10-13 23:42:08 -0600 | [diff] [blame] | 254 | if (tx) |
| 255 | sandbox_spi_tristate(tx, 1); |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 256 | |
| 257 | sbsf->cmd = rx[0]; |
| 258 | switch (sbsf->cmd) { |
| 259 | case CMD_READ_ID: |
| 260 | sbsf->state = SF_ID; |
| 261 | sbsf->cmd = SF_ID; |
| 262 | break; |
| 263 | case CMD_READ_ARRAY_FAST: |
| 264 | sbsf->pad_addr_bytes = 1; |
| 265 | case CMD_READ_ARRAY_SLOW: |
| 266 | case CMD_PAGE_PROGRAM: |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 267 | sbsf->state = SF_ADDR; |
| 268 | break; |
| 269 | case CMD_WRITE_DISABLE: |
| 270 | debug(" write disabled\n"); |
| 271 | sbsf->status &= ~STAT_WEL; |
| 272 | break; |
| 273 | case CMD_READ_STATUS: |
| 274 | sbsf->state = SF_READ_STATUS; |
| 275 | break; |
| 276 | case CMD_READ_STATUS1: |
| 277 | sbsf->state = SF_READ_STATUS1; |
| 278 | break; |
| 279 | case CMD_WRITE_ENABLE: |
| 280 | debug(" write enabled\n"); |
| 281 | sbsf->status |= STAT_WEL; |
| 282 | break; |
Simon Glass | b6c2956 | 2014-10-13 23:42:08 -0600 | [diff] [blame] | 283 | case CMD_WRITE_STATUS: |
| 284 | sbsf->state = SF_WRITE_STATUS; |
| 285 | break; |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 286 | default: { |
Simon Glass | 110bdee | 2014-09-15 06:33:19 -0600 | [diff] [blame] | 287 | int flags = sbsf->data->flags; |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 288 | |
Simon Glass | 110bdee | 2014-09-15 06:33:19 -0600 | [diff] [blame] | 289 | /* we only support erase here */ |
| 290 | if (sbsf->cmd == CMD_ERASE_CHIP) { |
| 291 | sbsf->erase_size = sbsf->data->sector_size * |
| 292 | sbsf->data->nr_sectors; |
| 293 | } else if (sbsf->cmd == CMD_ERASE_4K && (flags & SECT_4K)) { |
| 294 | sbsf->erase_size = 4 << 10; |
| 295 | } else if (sbsf->cmd == CMD_ERASE_32K && (flags & SECT_32K)) { |
| 296 | sbsf->erase_size = 32 << 10; |
| 297 | } else if (sbsf->cmd == CMD_ERASE_64K && |
| 298 | !(flags & (SECT_4K | SECT_32K))) { |
| 299 | sbsf->erase_size = 64 << 10; |
| 300 | } else { |
| 301 | debug(" cmd unknown: %#x\n", sbsf->cmd); |
Simon Glass | b6c2956 | 2014-10-13 23:42:08 -0600 | [diff] [blame] | 302 | return -EIO; |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 303 | } |
Simon Glass | 110bdee | 2014-09-15 06:33:19 -0600 | [diff] [blame] | 304 | sbsf->state = SF_ADDR; |
| 305 | break; |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 306 | } |
| 307 | } |
| 308 | |
| 309 | if (oldstate != sbsf->state) |
| 310 | debug(" cmd: transition to %s state\n", |
| 311 | sandbox_sf_state_name(sbsf->state)); |
| 312 | |
| 313 | return 0; |
| 314 | } |
| 315 | |
| 316 | int sandbox_erase_part(struct sandbox_spi_flash *sbsf, int size) |
| 317 | { |
| 318 | int todo; |
| 319 | int ret; |
| 320 | |
| 321 | while (size > 0) { |
Masahiro Yamada | b414119 | 2014-11-07 03:03:31 +0900 | [diff] [blame] | 322 | todo = min(size, (int)sizeof(sandbox_sf_0xff)); |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 323 | ret = os_write(sbsf->fd, sandbox_sf_0xff, todo); |
| 324 | if (ret != todo) |
| 325 | return ret; |
| 326 | size -= todo; |
| 327 | } |
| 328 | |
| 329 | return 0; |
| 330 | } |
| 331 | |
Simon Glass | b6c2956 | 2014-10-13 23:42:08 -0600 | [diff] [blame] | 332 | static int sandbox_sf_xfer(struct udevice *dev, unsigned int bitlen, |
| 333 | const void *rxp, void *txp, unsigned long flags) |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 334 | { |
Simon Glass | b6c2956 | 2014-10-13 23:42:08 -0600 | [diff] [blame] | 335 | struct sandbox_spi_flash *sbsf = dev_get_priv(dev); |
| 336 | const uint8_t *rx = rxp; |
| 337 | uint8_t *tx = txp; |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 338 | uint cnt, pos = 0; |
Simon Glass | b6c2956 | 2014-10-13 23:42:08 -0600 | [diff] [blame] | 339 | int bytes = bitlen / 8; |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 340 | int ret; |
| 341 | |
| 342 | debug("sandbox_sf: state:%x(%s) bytes:%u\n", sbsf->state, |
| 343 | sandbox_sf_state_name(sbsf->state), bytes); |
| 344 | |
Simon Glass | b6c2956 | 2014-10-13 23:42:08 -0600 | [diff] [blame] | 345 | if ((flags & SPI_XFER_BEGIN)) |
| 346 | sandbox_sf_cs_activate(dev); |
| 347 | |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 348 | if (sbsf->state == SF_CMD) { |
| 349 | /* Figure out the initial state */ |
Simon Glass | b6c2956 | 2014-10-13 23:42:08 -0600 | [diff] [blame] | 350 | ret = sandbox_sf_process_cmd(sbsf, rx, tx); |
| 351 | if (ret) |
| 352 | return ret; |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 353 | ++pos; |
| 354 | } |
| 355 | |
| 356 | /* Process the remaining data */ |
| 357 | while (pos < bytes) { |
| 358 | switch (sbsf->state) { |
| 359 | case SF_ID: { |
| 360 | u8 id; |
| 361 | |
| 362 | debug(" id: off:%u tx:", sbsf->off); |
Simon Glass | 110bdee | 2014-09-15 06:33:19 -0600 | [diff] [blame] | 363 | if (sbsf->off < IDCODE_LEN) { |
| 364 | /* Extract correct byte from ID 0x00aabbcc */ |
| 365 | id = sbsf->data->jedec >> |
| 366 | (8 * (IDCODE_LEN - 1 - sbsf->off)); |
| 367 | } else { |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 368 | id = 0; |
Simon Glass | 110bdee | 2014-09-15 06:33:19 -0600 | [diff] [blame] | 369 | } |
| 370 | debug("%d %02x\n", sbsf->off, id); |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 371 | tx[pos++] = id; |
| 372 | ++sbsf->off; |
| 373 | break; |
| 374 | } |
| 375 | case SF_ADDR: |
| 376 | debug(" addr: bytes:%u rx:%02x ", sbsf->addr_bytes, |
| 377 | rx[pos]); |
| 378 | |
| 379 | if (sbsf->addr_bytes++ < SF_ADDR_LEN) |
| 380 | sbsf->off = (sbsf->off << 8) | rx[pos]; |
| 381 | debug("addr:%06x\n", sbsf->off); |
| 382 | |
Simon Glass | b6c2956 | 2014-10-13 23:42:08 -0600 | [diff] [blame] | 383 | if (tx) |
| 384 | sandbox_spi_tristate(&tx[pos], 1); |
| 385 | pos++; |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 386 | |
| 387 | /* See if we're done processing */ |
| 388 | if (sbsf->addr_bytes < |
| 389 | SF_ADDR_LEN + sbsf->pad_addr_bytes) |
| 390 | break; |
| 391 | |
| 392 | /* Next state! */ |
| 393 | if (os_lseek(sbsf->fd, sbsf->off, OS_SEEK_SET) < 0) { |
| 394 | puts("sandbox_sf: os_lseek() failed"); |
Simon Glass | b6c2956 | 2014-10-13 23:42:08 -0600 | [diff] [blame] | 395 | return -EIO; |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 396 | } |
| 397 | switch (sbsf->cmd) { |
| 398 | case CMD_READ_ARRAY_FAST: |
| 399 | case CMD_READ_ARRAY_SLOW: |
| 400 | sbsf->state = SF_READ; |
| 401 | break; |
| 402 | case CMD_PAGE_PROGRAM: |
| 403 | sbsf->state = SF_WRITE; |
| 404 | break; |
| 405 | default: |
| 406 | /* assume erase state ... */ |
| 407 | sbsf->state = SF_ERASE; |
| 408 | goto case_sf_erase; |
| 409 | } |
| 410 | debug(" cmd: transition to %s state\n", |
| 411 | sandbox_sf_state_name(sbsf->state)); |
| 412 | break; |
| 413 | case SF_READ: |
| 414 | /* |
| 415 | * XXX: need to handle exotic behavior: |
| 416 | * - reading past end of device |
| 417 | */ |
| 418 | |
| 419 | cnt = bytes - pos; |
| 420 | debug(" tx: read(%u)\n", cnt); |
Simon Glass | b6c2956 | 2014-10-13 23:42:08 -0600 | [diff] [blame] | 421 | assert(tx); |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 422 | ret = os_read(sbsf->fd, tx + pos, cnt); |
| 423 | if (ret < 0) { |
Simon Glass | b6c2956 | 2014-10-13 23:42:08 -0600 | [diff] [blame] | 424 | puts("sandbox_sf: os_read() failed\n"); |
| 425 | return -EIO; |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 426 | } |
| 427 | pos += ret; |
| 428 | break; |
| 429 | case SF_READ_STATUS: |
| 430 | debug(" read status: %#x\n", sbsf->status); |
| 431 | cnt = bytes - pos; |
| 432 | memset(tx + pos, sbsf->status, cnt); |
| 433 | pos += cnt; |
| 434 | break; |
| 435 | case SF_READ_STATUS1: |
| 436 | debug(" read status: %#x\n", sbsf->status); |
| 437 | cnt = bytes - pos; |
| 438 | memset(tx + pos, sbsf->status >> 8, cnt); |
| 439 | pos += cnt; |
| 440 | break; |
Simon Glass | b6c2956 | 2014-10-13 23:42:08 -0600 | [diff] [blame] | 441 | case SF_WRITE_STATUS: |
| 442 | debug(" write status: %#x (ignored)\n", rx[pos]); |
| 443 | pos = bytes; |
| 444 | break; |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 445 | case SF_WRITE: |
| 446 | /* |
| 447 | * XXX: need to handle exotic behavior: |
| 448 | * - unaligned addresses |
| 449 | * - more than a page (256) worth of data |
| 450 | * - reading past end of device |
| 451 | */ |
| 452 | if (!(sbsf->status & STAT_WEL)) { |
| 453 | puts("sandbox_sf: write enable not set before write\n"); |
| 454 | goto done; |
| 455 | } |
| 456 | |
| 457 | cnt = bytes - pos; |
| 458 | debug(" rx: write(%u)\n", cnt); |
Simon Glass | b6c2956 | 2014-10-13 23:42:08 -0600 | [diff] [blame] | 459 | if (tx) |
| 460 | sandbox_spi_tristate(&tx[pos], cnt); |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 461 | ret = os_write(sbsf->fd, rx + pos, cnt); |
| 462 | if (ret < 0) { |
| 463 | puts("sandbox_spi: os_write() failed\n"); |
Simon Glass | b6c2956 | 2014-10-13 23:42:08 -0600 | [diff] [blame] | 464 | return -EIO; |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 465 | } |
| 466 | pos += ret; |
| 467 | sbsf->status &= ~STAT_WEL; |
| 468 | break; |
| 469 | case SF_ERASE: |
| 470 | case_sf_erase: { |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 471 | if (!(sbsf->status & STAT_WEL)) { |
| 472 | puts("sandbox_sf: write enable not set before erase\n"); |
| 473 | goto done; |
| 474 | } |
| 475 | |
| 476 | /* verify address is aligned */ |
Simon Glass | 110bdee | 2014-09-15 06:33:19 -0600 | [diff] [blame] | 477 | if (sbsf->off & (sbsf->erase_size - 1)) { |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 478 | debug(" sector erase: cmd:%#x needs align:%#x, but we got %#x\n", |
Simon Glass | 110bdee | 2014-09-15 06:33:19 -0600 | [diff] [blame] | 479 | sbsf->cmd, sbsf->erase_size, |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 480 | sbsf->off); |
| 481 | sbsf->status &= ~STAT_WEL; |
| 482 | goto done; |
| 483 | } |
| 484 | |
Simon Glass | 110bdee | 2014-09-15 06:33:19 -0600 | [diff] [blame] | 485 | debug(" sector erase addr: %u, size: %u\n", sbsf->off, |
| 486 | sbsf->erase_size); |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 487 | |
| 488 | cnt = bytes - pos; |
Simon Glass | b6c2956 | 2014-10-13 23:42:08 -0600 | [diff] [blame] | 489 | if (tx) |
| 490 | sandbox_spi_tristate(&tx[pos], cnt); |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 491 | pos += cnt; |
| 492 | |
| 493 | /* |
| 494 | * TODO(vapier@gentoo.org): latch WIP in status, and |
| 495 | * delay before clearing it ? |
| 496 | */ |
Simon Glass | 110bdee | 2014-09-15 06:33:19 -0600 | [diff] [blame] | 497 | ret = sandbox_erase_part(sbsf, sbsf->erase_size); |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 498 | sbsf->status &= ~STAT_WEL; |
| 499 | if (ret) { |
| 500 | debug("sandbox_sf: Erase failed\n"); |
| 501 | goto done; |
| 502 | } |
| 503 | goto done; |
| 504 | } |
| 505 | default: |
| 506 | debug(" ??? no idea what to do ???\n"); |
| 507 | goto done; |
| 508 | } |
| 509 | } |
| 510 | |
| 511 | done: |
Simon Glass | b6c2956 | 2014-10-13 23:42:08 -0600 | [diff] [blame] | 512 | if (flags & SPI_XFER_END) |
| 513 | sandbox_sf_cs_deactivate(dev); |
| 514 | return pos == bytes ? 0 : -EIO; |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 515 | } |
| 516 | |
Simon Glass | b6c2956 | 2014-10-13 23:42:08 -0600 | [diff] [blame] | 517 | int sandbox_sf_ofdata_to_platdata(struct udevice *dev) |
| 518 | { |
| 519 | struct sandbox_spi_flash_plat_data *pdata = dev_get_platdata(dev); |
| 520 | const void *blob = gd->fdt_blob; |
| 521 | int node = dev->of_offset; |
| 522 | |
| 523 | pdata->filename = fdt_getprop(blob, node, "sandbox,filename", NULL); |
| 524 | pdata->device_name = fdt_getprop(blob, node, "compatible", NULL); |
| 525 | if (!pdata->filename || !pdata->device_name) { |
| 526 | debug("%s: Missing properties, filename=%s, device_name=%s\n", |
| 527 | __func__, pdata->filename, pdata->device_name); |
| 528 | return -EINVAL; |
| 529 | } |
| 530 | |
| 531 | return 0; |
| 532 | } |
| 533 | |
| 534 | static const struct dm_spi_emul_ops sandbox_sf_emul_ops = { |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 535 | .xfer = sandbox_sf_xfer, |
| 536 | }; |
| 537 | |
Simon Glass | b6c2956 | 2014-10-13 23:42:08 -0600 | [diff] [blame] | 538 | #ifdef CONFIG_SPI_FLASH |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 539 | static int sandbox_cmdline_cb_spi_sf(struct sandbox_state *state, |
| 540 | const char *arg) |
| 541 | { |
| 542 | unsigned long bus, cs; |
| 543 | const char *spec = sandbox_spi_parse_spec(arg, &bus, &cs); |
| 544 | |
| 545 | if (!spec) |
| 546 | return 1; |
| 547 | |
| 548 | /* |
| 549 | * It is safe to not make a copy of 'spec' because it comes from the |
| 550 | * command line. |
| 551 | * |
| 552 | * TODO(sjg@chromium.org): It would be nice if we could parse the |
| 553 | * spec here, but the problem is that no U-Boot init has been done |
| 554 | * yet. Perhaps we can figure something out. |
| 555 | */ |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 556 | state->spi[bus][cs].spec = spec; |
Simon Glass | 20f655d | 2016-02-24 09:14:54 -0700 | [diff] [blame] | 557 | debug("%s: Setting up spec '%s' for bus %ld, cs %ld\n", __func__, |
| 558 | spec, bus, cs); |
| 559 | |
Mike Frysinger | ffdb20b | 2013-12-03 16:43:27 -0700 | [diff] [blame] | 560 | return 0; |
| 561 | } |
| 562 | SANDBOX_CMDLINE_OPT(spi_sf, 1, "connect a SPI flash: <bus>:<cs>:<id>:<file>"); |
Simon Glass | b6c2956 | 2014-10-13 23:42:08 -0600 | [diff] [blame] | 563 | |
| 564 | int sandbox_sf_bind_emul(struct sandbox_state *state, int busnum, int cs, |
| 565 | struct udevice *bus, int of_offset, const char *spec) |
| 566 | { |
| 567 | struct udevice *emul; |
| 568 | char name[20], *str; |
| 569 | struct driver *drv; |
| 570 | int ret; |
| 571 | |
| 572 | /* now the emulator */ |
| 573 | strncpy(name, spec, sizeof(name) - 6); |
| 574 | name[sizeof(name) - 6] = '\0'; |
| 575 | strcat(name, "-emul"); |
| 576 | str = strdup(name); |
| 577 | if (!str) |
| 578 | return -ENOMEM; |
| 579 | drv = lists_driver_lookup_name("sandbox_sf_emul"); |
| 580 | if (!drv) { |
| 581 | puts("Cannot find sandbox_sf_emul driver\n"); |
| 582 | return -ENOENT; |
| 583 | } |
| 584 | ret = device_bind(bus, drv, str, NULL, of_offset, &emul); |
| 585 | if (ret) { |
| 586 | printf("Cannot create emul device for spec '%s' (err=%d)\n", |
| 587 | spec, ret); |
| 588 | return ret; |
| 589 | } |
| 590 | state->spi[busnum][cs].emul = emul; |
| 591 | |
| 592 | return 0; |
| 593 | } |
| 594 | |
| 595 | void sandbox_sf_unbind_emul(struct sandbox_state *state, int busnum, int cs) |
| 596 | { |
Simon Glass | d0cff03 | 2015-01-25 08:27:12 -0700 | [diff] [blame] | 597 | struct udevice *dev; |
| 598 | |
| 599 | dev = state->spi[busnum][cs].emul; |
| 600 | device_remove(dev); |
| 601 | device_unbind(dev); |
Simon Glass | b6c2956 | 2014-10-13 23:42:08 -0600 | [diff] [blame] | 602 | state->spi[busnum][cs].emul = NULL; |
| 603 | } |
| 604 | |
| 605 | static int sandbox_sf_bind_bus_cs(struct sandbox_state *state, int busnum, |
| 606 | int cs, const char *spec) |
| 607 | { |
| 608 | struct udevice *bus, *slave; |
| 609 | int ret; |
| 610 | |
| 611 | ret = uclass_find_device_by_seq(UCLASS_SPI, busnum, true, &bus); |
| 612 | if (ret) { |
| 613 | printf("Invalid bus %d for spec '%s' (err=%d)\n", busnum, |
| 614 | spec, ret); |
| 615 | return ret; |
| 616 | } |
Simon Glass | ff56bba | 2014-11-11 10:46:22 -0700 | [diff] [blame] | 617 | ret = spi_find_chip_select(bus, cs, &slave); |
Simon Glass | b6c2956 | 2014-10-13 23:42:08 -0600 | [diff] [blame] | 618 | if (!ret) { |
| 619 | printf("Chip select %d already exists for spec '%s'\n", cs, |
| 620 | spec); |
| 621 | return -EEXIST; |
| 622 | } |
| 623 | |
Simon Glass | 6b18656 | 2014-11-11 10:46:23 -0700 | [diff] [blame] | 624 | ret = device_bind_driver(bus, "spi_flash_std", spec, &slave); |
Simon Glass | b6c2956 | 2014-10-13 23:42:08 -0600 | [diff] [blame] | 625 | if (ret) |
| 626 | return ret; |
| 627 | |
| 628 | return sandbox_sf_bind_emul(state, busnum, cs, bus, -1, spec); |
| 629 | } |
| 630 | |
| 631 | int sandbox_spi_get_emul(struct sandbox_state *state, |
| 632 | struct udevice *bus, struct udevice *slave, |
| 633 | struct udevice **emulp) |
| 634 | { |
| 635 | struct sandbox_spi_info *info; |
| 636 | int busnum = bus->seq; |
| 637 | int cs = spi_chip_select(slave); |
| 638 | int ret; |
| 639 | |
| 640 | info = &state->spi[busnum][cs]; |
| 641 | if (!info->emul) { |
| 642 | /* Use the same device tree node as the SPI flash device */ |
| 643 | debug("%s: busnum=%u, cs=%u: binding SPI flash emulation: ", |
| 644 | __func__, busnum, cs); |
| 645 | ret = sandbox_sf_bind_emul(state, busnum, cs, bus, |
| 646 | slave->of_offset, slave->name); |
| 647 | if (ret) { |
| 648 | debug("failed (err=%d)\n", ret); |
| 649 | return ret; |
| 650 | } |
| 651 | debug("OK\n"); |
| 652 | } |
| 653 | *emulp = info->emul; |
| 654 | |
| 655 | return 0; |
| 656 | } |
| 657 | |
| 658 | int dm_scan_other(bool pre_reloc_only) |
| 659 | { |
| 660 | struct sandbox_state *state = state_get_current(); |
| 661 | int busnum, cs; |
| 662 | |
| 663 | if (pre_reloc_only) |
| 664 | return 0; |
| 665 | for (busnum = 0; busnum < CONFIG_SANDBOX_SPI_MAX_BUS; busnum++) { |
| 666 | for (cs = 0; cs < CONFIG_SANDBOX_SPI_MAX_CS; cs++) { |
| 667 | const char *spec = state->spi[busnum][cs].spec; |
| 668 | int ret; |
| 669 | |
| 670 | if (spec) { |
| 671 | ret = sandbox_sf_bind_bus_cs(state, busnum, |
| 672 | cs, spec); |
| 673 | if (ret) { |
| 674 | debug("%s: Bind failed for bus %d, cs %d\n", |
| 675 | __func__, busnum, cs); |
| 676 | return ret; |
| 677 | } |
Simon Glass | 20f655d | 2016-02-24 09:14:54 -0700 | [diff] [blame] | 678 | debug("%s: Setting up spec '%s' for bus %d, cs %d\n", |
| 679 | __func__, spec, busnum, cs); |
Simon Glass | b6c2956 | 2014-10-13 23:42:08 -0600 | [diff] [blame] | 680 | } |
| 681 | } |
| 682 | } |
| 683 | |
| 684 | return 0; |
| 685 | } |
| 686 | #endif |
| 687 | |
| 688 | static const struct udevice_id sandbox_sf_ids[] = { |
| 689 | { .compatible = "sandbox,spi-flash" }, |
| 690 | { } |
| 691 | }; |
| 692 | |
| 693 | U_BOOT_DRIVER(sandbox_sf_emul) = { |
| 694 | .name = "sandbox_sf_emul", |
| 695 | .id = UCLASS_SPI_EMUL, |
| 696 | .of_match = sandbox_sf_ids, |
| 697 | .ofdata_to_platdata = sandbox_sf_ofdata_to_platdata, |
| 698 | .probe = sandbox_sf_probe, |
| 699 | .remove = sandbox_sf_remove, |
| 700 | .priv_auto_alloc_size = sizeof(struct sandbox_spi_flash), |
| 701 | .platdata_auto_alloc_size = sizeof(struct sandbox_spi_flash_plat_data), |
| 702 | .ops = &sandbox_sf_emul_ops, |
| 703 | }; |