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 #define LOG_CATEGORY UCLASS_SPI_FLASH
12
13 #include <common.h>
14 #include <dm.h>
15 #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>
25 #include <dm/device-internal.h>
26 #include <dm/lists.h>
27 #include <dm/uclass-internal.h>
28
29 DECLARE_GLOBAL_DATA_PTR;
30
31 /*
32 * The different states that our SPI flash transitions between.
33 * We need to keep track of this across multiple xfer calls since
34 * the SPI bus could possibly call down into us multiple times.
35 */
36 enum sandbox_sf_state {
37 SF_CMD, /* default state -- we're awaiting a command */
38 SF_ID, /* read the flash's (jedec) ID code */
39 SF_ADDR, /* processing the offset in the flash to read/etc... */
40 SF_READ, /* reading data from the flash */
41 SF_WRITE, /* writing data to the flash, i.e. page programming */
42 SF_ERASE, /* erase the flash */
43 SF_READ_STATUS, /* read the flash's status register */
44 SF_READ_STATUS1, /* read the flash's status register upper 8 bits*/
45 SF_WRITE_STATUS, /* write the flash's status register */
46 };
47
48 #if CONFIG_IS_ENABLED(LOG)
sandbox_sf_state_name(enum sandbox_sf_state state)49 static const char *sandbox_sf_state_name(enum sandbox_sf_state state)
50 {
51 static const char * const states[] = {
52 "CMD", "ID", "ADDR", "READ", "WRITE", "ERASE", "READ_STATUS",
53 "READ_STATUS1", "WRITE_STATUS",
54 };
55 return states[state];
56 }
57 #endif /* LOG */
58
59 /* Bits for the status register */
60 #define STAT_WIP (1 << 0)
61 #define STAT_WEL (1 << 1)
62 #define STAT_BP_SHIFT 2
63 #define STAT_BP_MASK (7 << STAT_BP_SHIFT)
64
65 /* Assume all SPI flashes have 3 byte addresses since they do atm */
66 #define SF_ADDR_LEN 3
67
68 #define IDCODE_LEN 3
69
70 /* Used to quickly bulk erase backing store */
71 static u8 sandbox_sf_0xff[0x1000];
72
73 /* Internal state data for each SPI flash */
74 struct sandbox_spi_flash {
75 unsigned int cs; /* Chip select we are attached to */
76 /*
77 * As we receive data over the SPI bus, our flash transitions
78 * between states. For example, we start off in the SF_CMD
79 * state where the first byte tells us what operation to perform
80 * (such as read or write the flash). But the operation itself
81 * can go through a few states such as first reading in the
82 * offset in the flash to perform the requested operation.
83 * Thus "state" stores the exact state that our machine is in
84 * while "cmd" stores the overall command we're processing.
85 */
86 enum sandbox_sf_state state;
87 uint cmd;
88 /* Erase size of current erase command */
89 uint erase_size;
90 /* Current position in the flash; used when reading/writing/etc... */
91 uint off;
92 /* How many address bytes we've consumed */
93 uint addr_bytes, pad_addr_bytes;
94 /* The current flash status (see STAT_XXX defines above) */
95 u16 status;
96 /* Data describing the flash we're emulating */
97 const struct flash_info *data;
98 /* The file on disk to serv up data from */
99 int fd;
100 };
101
102 struct sandbox_spi_flash_plat_data {
103 const char *filename;
104 const char *device_name;
105 int bus;
106 int cs;
107 };
108
sandbox_sf_set_block_protect(struct udevice * dev,int bp_mask)109 void sandbox_sf_set_block_protect(struct udevice *dev, int bp_mask)
110 {
111 struct sandbox_spi_flash *sbsf = dev_get_priv(dev);
112
113 sbsf->status &= ~STAT_BP_MASK;
114 sbsf->status |= bp_mask << STAT_BP_SHIFT;
115 }
116
117 /**
118 * This is a very strange probe function. If it has platform data (which may
119 * have come from the device tree) then this function gets the filename and
120 * device type from there.
121 */
sandbox_sf_probe(struct udevice * dev)122 static int sandbox_sf_probe(struct udevice *dev)
123 {
124 /* spec = idcode:file */
125 struct sandbox_spi_flash *sbsf = dev_get_priv(dev);
126 size_t len, idname_len;
127 const struct flash_info *data;
128 struct sandbox_spi_flash_plat_data *pdata = dev_get_platdata(dev);
129 struct sandbox_state *state = state_get_current();
130 struct dm_spi_slave_platdata *slave_plat;
131 struct udevice *bus = dev->parent;
132 const char *spec = NULL;
133 struct udevice *emul;
134 int ret = 0;
135 int cs = -1;
136
137 debug("%s: bus %d, looking for emul=%p: ", __func__, bus->seq, dev);
138 ret = sandbox_spi_get_emul(state, bus, dev, &emul);
139 if (ret) {
140 printf("Error: Unknown chip select for device '%s'\n",
141 dev->name);
142 return ret;
143 }
144 slave_plat = dev_get_parent_platdata(dev);
145 cs = slave_plat->cs;
146 debug("found at cs %d\n", cs);
147
148 if (!pdata->filename) {
149 printf("Error: No filename available\n");
150 return -EINVAL;
151 }
152 spec = strchr(pdata->device_name, ',');
153 if (spec)
154 spec++;
155 else
156 spec = pdata->device_name;
157 idname_len = strlen(spec);
158 debug("%s: device='%s'\n", __func__, spec);
159
160 for (data = spi_nor_ids; data->name; data++) {
161 len = strlen(data->name);
162 if (idname_len != len)
163 continue;
164 if (!strncasecmp(spec, data->name, len))
165 break;
166 }
167 if (!data->name) {
168 printf("%s: unknown flash '%*s'\n", __func__, (int)idname_len,
169 spec);
170 ret = -EINVAL;
171 goto error;
172 }
173
174 if (sandbox_sf_0xff[0] == 0x00)
175 memset(sandbox_sf_0xff, 0xff, sizeof(sandbox_sf_0xff));
176
177 sbsf->fd = os_open(pdata->filename, 02);
178 if (sbsf->fd == -1) {
179 printf("%s: unable to open file '%s'\n", __func__,
180 pdata->filename);
181 ret = -EIO;
182 goto error;
183 }
184
185 sbsf->data = data;
186 sbsf->cs = cs;
187
188 return 0;
189
190 error:
191 debug("%s: Got error %d\n", __func__, ret);
192 return ret;
193 }
194
sandbox_sf_remove(struct udevice * dev)195 static int sandbox_sf_remove(struct udevice *dev)
196 {
197 struct sandbox_spi_flash *sbsf = dev_get_priv(dev);
198
199 os_close(sbsf->fd);
200
201 return 0;
202 }
203
sandbox_sf_cs_activate(struct udevice * dev)204 static void sandbox_sf_cs_activate(struct udevice *dev)
205 {
206 struct sandbox_spi_flash *sbsf = dev_get_priv(dev);
207
208 log_content("sandbox_sf: CS activated; state is fresh!\n");
209
210 /* CS is asserted, so reset state */
211 sbsf->off = 0;
212 sbsf->addr_bytes = 0;
213 sbsf->pad_addr_bytes = 0;
214 sbsf->state = SF_CMD;
215 sbsf->cmd = SF_CMD;
216 }
217
sandbox_sf_cs_deactivate(struct udevice * dev)218 static void sandbox_sf_cs_deactivate(struct udevice *dev)
219 {
220 log_content("sandbox_sf: CS deactivated; cmd done processing!\n");
221 }
222
223 /*
224 * There are times when the data lines are allowed to tristate. What
225 * is actually sensed on the line depends on the hardware. It could
226 * always be 0xFF/0x00 (if there are pull ups/downs), or things could
227 * float and so we'd get garbage back. This func encapsulates that
228 * scenario so we can worry about the details here.
229 */
sandbox_spi_tristate(u8 * buf,uint len)230 static void sandbox_spi_tristate(u8 *buf, uint len)
231 {
232 /* XXX: make this into a user config option ? */
233 memset(buf, 0xff, len);
234 }
235
236 /* Figure out what command this stream is telling us to do */
sandbox_sf_process_cmd(struct sandbox_spi_flash * sbsf,const u8 * rx,u8 * tx)237 static int sandbox_sf_process_cmd(struct sandbox_spi_flash *sbsf, const u8 *rx,
238 u8 *tx)
239 {
240 enum sandbox_sf_state oldstate = sbsf->state;
241
242 /* We need to output a byte for the cmd byte we just ate */
243 if (tx)
244 sandbox_spi_tristate(tx, 1);
245
246 sbsf->cmd = rx[0];
247 switch (sbsf->cmd) {
248 case SPINOR_OP_RDID:
249 sbsf->state = SF_ID;
250 sbsf->cmd = SF_ID;
251 break;
252 case SPINOR_OP_READ_FAST:
253 sbsf->pad_addr_bytes = 1;
254 case SPINOR_OP_READ:
255 case SPINOR_OP_PP:
256 sbsf->state = SF_ADDR;
257 break;
258 case SPINOR_OP_WRDI:
259 debug(" write disabled\n");
260 sbsf->status &= ~STAT_WEL;
261 break;
262 case SPINOR_OP_RDSR:
263 sbsf->state = SF_READ_STATUS;
264 break;
265 case SPINOR_OP_RDSR2:
266 sbsf->state = SF_READ_STATUS1;
267 break;
268 case SPINOR_OP_WREN:
269 debug(" write enabled\n");
270 sbsf->status |= STAT_WEL;
271 break;
272 case SPINOR_OP_WRSR:
273 sbsf->state = SF_WRITE_STATUS;
274 break;
275 default: {
276 int flags = sbsf->data->flags;
277
278 /* we only support erase here */
279 if (sbsf->cmd == SPINOR_OP_CHIP_ERASE) {
280 sbsf->erase_size = sbsf->data->sector_size *
281 sbsf->data->n_sectors;
282 } else if (sbsf->cmd == SPINOR_OP_BE_4K && (flags & SECT_4K)) {
283 sbsf->erase_size = 4 << 10;
284 } else if (sbsf->cmd == SPINOR_OP_SE && !(flags & SECT_4K)) {
285 sbsf->erase_size = 64 << 10;
286 } else {
287 debug(" cmd unknown: %#x\n", sbsf->cmd);
288 return -EIO;
289 }
290 sbsf->state = SF_ADDR;
291 break;
292 }
293 }
294
295 if (oldstate != sbsf->state)
296 log_content(" cmd: transition to %s state\n",
297 sandbox_sf_state_name(sbsf->state));
298
299 return 0;
300 }
301
sandbox_erase_part(struct sandbox_spi_flash * sbsf,int size)302 int sandbox_erase_part(struct sandbox_spi_flash *sbsf, int size)
303 {
304 int todo;
305 int ret;
306
307 while (size > 0) {
308 todo = min(size, (int)sizeof(sandbox_sf_0xff));
309 ret = os_write(sbsf->fd, sandbox_sf_0xff, todo);
310 if (ret != todo)
311 return ret;
312 size -= todo;
313 }
314
315 return 0;
316 }
317
sandbox_sf_xfer(struct udevice * dev,unsigned int bitlen,const void * rxp,void * txp,unsigned long flags)318 static int sandbox_sf_xfer(struct udevice *dev, unsigned int bitlen,
319 const void *rxp, void *txp, unsigned long flags)
320 {
321 struct sandbox_spi_flash *sbsf = dev_get_priv(dev);
322 const uint8_t *rx = rxp;
323 uint8_t *tx = txp;
324 uint cnt, pos = 0;
325 int bytes = bitlen / 8;
326 int ret;
327
328 log_content("sandbox_sf: state:%x(%s) bytes:%u\n", sbsf->state,
329 sandbox_sf_state_name(sbsf->state), bytes);
330
331 if ((flags & SPI_XFER_BEGIN))
332 sandbox_sf_cs_activate(dev);
333
334 if (sbsf->state == SF_CMD) {
335 /* Figure out the initial state */
336 ret = sandbox_sf_process_cmd(sbsf, rx, tx);
337 if (ret)
338 return ret;
339 ++pos;
340 }
341
342 /* Process the remaining data */
343 while (pos < bytes) {
344 switch (sbsf->state) {
345 case SF_ID: {
346 u8 id;
347
348 log_content(" id: off:%u tx:", sbsf->off);
349 if (sbsf->off < IDCODE_LEN) {
350 /* Extract correct byte from ID 0x00aabbcc */
351 id = ((JEDEC_MFR(sbsf->data) << 16) |
352 JEDEC_ID(sbsf->data)) >>
353 (8 * (IDCODE_LEN - 1 - sbsf->off));
354 } else {
355 id = 0;
356 }
357 log_content("%d %02x\n", sbsf->off, id);
358 tx[pos++] = id;
359 ++sbsf->off;
360 break;
361 }
362 case SF_ADDR:
363 log_content(" addr: bytes:%u rx:%02x ",
364 sbsf->addr_bytes, rx[pos]);
365
366 if (sbsf->addr_bytes++ < SF_ADDR_LEN)
367 sbsf->off = (sbsf->off << 8) | rx[pos];
368 log_content("addr:%06x\n", sbsf->off);
369
370 if (tx)
371 sandbox_spi_tristate(&tx[pos], 1);
372 pos++;
373
374 /* See if we're done processing */
375 if (sbsf->addr_bytes <
376 SF_ADDR_LEN + sbsf->pad_addr_bytes)
377 break;
378
379 /* Next state! */
380 if (os_lseek(sbsf->fd, sbsf->off, OS_SEEK_SET) < 0) {
381 puts("sandbox_sf: os_lseek() failed");
382 return -EIO;
383 }
384 switch (sbsf->cmd) {
385 case SPINOR_OP_READ_FAST:
386 case SPINOR_OP_READ:
387 sbsf->state = SF_READ;
388 break;
389 case SPINOR_OP_PP:
390 sbsf->state = SF_WRITE;
391 break;
392 default:
393 /* assume erase state ... */
394 sbsf->state = SF_ERASE;
395 goto case_sf_erase;
396 }
397 log_content(" cmd: transition to %s state\n",
398 sandbox_sf_state_name(sbsf->state));
399 break;
400 case SF_READ:
401 /*
402 * XXX: need to handle exotic behavior:
403 * - reading past end of device
404 */
405
406 cnt = bytes - pos;
407 log_content(" tx: read(%u)\n", cnt);
408 assert(tx);
409 ret = os_read(sbsf->fd, tx + pos, cnt);
410 if (ret < 0) {
411 puts("sandbox_sf: os_read() failed\n");
412 return -EIO;
413 }
414 pos += ret;
415 break;
416 case SF_READ_STATUS:
417 log_content(" read status: %#x\n", sbsf->status);
418 cnt = bytes - pos;
419 memset(tx + pos, sbsf->status, cnt);
420 pos += cnt;
421 break;
422 case SF_READ_STATUS1:
423 log_content(" read status: %#x\n", sbsf->status);
424 cnt = bytes - pos;
425 memset(tx + pos, sbsf->status >> 8, cnt);
426 pos += cnt;
427 break;
428 case SF_WRITE_STATUS:
429 log_content(" write status: %#x (ignored)\n", rx[pos]);
430 pos = bytes;
431 break;
432 case SF_WRITE:
433 /*
434 * XXX: need to handle exotic behavior:
435 * - unaligned addresses
436 * - more than a page (256) worth of data
437 * - reading past end of device
438 */
439 if (!(sbsf->status & STAT_WEL)) {
440 puts("sandbox_sf: write enable not set before write\n");
441 goto done;
442 }
443
444 cnt = bytes - pos;
445 log_content(" rx: write(%u)\n", cnt);
446 if (tx)
447 sandbox_spi_tristate(&tx[pos], cnt);
448 ret = os_write(sbsf->fd, rx + pos, cnt);
449 if (ret < 0) {
450 puts("sandbox_spi: os_write() failed\n");
451 return -EIO;
452 }
453 pos += ret;
454 sbsf->status &= ~STAT_WEL;
455 break;
456 case SF_ERASE:
457 case_sf_erase: {
458 if (!(sbsf->status & STAT_WEL)) {
459 puts("sandbox_sf: write enable not set before erase\n");
460 goto done;
461 }
462
463 /* verify address is aligned */
464 if (sbsf->off & (sbsf->erase_size - 1)) {
465 log_content(" sector erase: cmd:%#x needs align:%#x, but we got %#x\n",
466 sbsf->cmd, sbsf->erase_size,
467 sbsf->off);
468 sbsf->status &= ~STAT_WEL;
469 goto done;
470 }
471
472 log_content(" sector erase addr: %u, size: %u\n",
473 sbsf->off, sbsf->erase_size);
474
475 cnt = bytes - pos;
476 if (tx)
477 sandbox_spi_tristate(&tx[pos], cnt);
478 pos += cnt;
479
480 /*
481 * TODO(vapier@gentoo.org): latch WIP in status, and
482 * delay before clearing it ?
483 */
484 ret = sandbox_erase_part(sbsf, sbsf->erase_size);
485 sbsf->status &= ~STAT_WEL;
486 if (ret) {
487 log_content("sandbox_sf: Erase failed\n");
488 goto done;
489 }
490 goto done;
491 }
492 default:
493 log_content(" ??? no idea what to do ???\n");
494 goto done;
495 }
496 }
497
498 done:
499 if (flags & SPI_XFER_END)
500 sandbox_sf_cs_deactivate(dev);
501 return pos == bytes ? 0 : -EIO;
502 }
503
sandbox_sf_ofdata_to_platdata(struct udevice * dev)504 int sandbox_sf_ofdata_to_platdata(struct udevice *dev)
505 {
506 struct sandbox_spi_flash_plat_data *pdata = dev_get_platdata(dev);
507
508 pdata->filename = dev_read_string(dev, "sandbox,filename");
509 pdata->device_name = dev_read_string(dev, "compatible");
510 if (!pdata->filename || !pdata->device_name) {
511 debug("%s: Missing properties, filename=%s, device_name=%s\n",
512 __func__, pdata->filename, pdata->device_name);
513 return -EINVAL;
514 }
515
516 return 0;
517 }
518
519 static const struct dm_spi_emul_ops sandbox_sf_emul_ops = {
520 .xfer = sandbox_sf_xfer,
521 };
522
523 #ifdef CONFIG_SPI_FLASH
sandbox_sf_bind_emul(struct sandbox_state * state,int busnum,int cs,struct udevice * bus,ofnode node,const char * spec)524 int sandbox_sf_bind_emul(struct sandbox_state *state, int busnum, int cs,
525 struct udevice *bus, ofnode node, const char *spec)
526 {
527 struct udevice *emul;
528 char name[20], *str;
529 struct driver *drv;
530 int ret;
531
532 /* now the emulator */
533 strncpy(name, spec, sizeof(name) - 6);
534 name[sizeof(name) - 6] = '\0';
535 strcat(name, "-emul");
536 drv = lists_driver_lookup_name("sandbox_sf_emul");
537 if (!drv) {
538 puts("Cannot find sandbox_sf_emul driver\n");
539 return -ENOENT;
540 }
541 str = strdup(name);
542 if (!str)
543 return -ENOMEM;
544 ret = device_bind_ofnode(bus, drv, str, NULL, node, &emul);
545 if (ret) {
546 free(str);
547 printf("Cannot create emul device for spec '%s' (err=%d)\n",
548 spec, ret);
549 return ret;
550 }
551 state->spi[busnum][cs].emul = emul;
552
553 return 0;
554 }
555
sandbox_sf_unbind_emul(struct sandbox_state * state,int busnum,int cs)556 void sandbox_sf_unbind_emul(struct sandbox_state *state, int busnum, int cs)
557 {
558 struct udevice *dev;
559
560 dev = state->spi[busnum][cs].emul;
561 device_remove(dev, DM_REMOVE_NORMAL);
562 device_unbind(dev);
563 state->spi[busnum][cs].emul = NULL;
564 }
565
sandbox_spi_get_emul(struct sandbox_state * state,struct udevice * bus,struct udevice * slave,struct udevice ** emulp)566 int sandbox_spi_get_emul(struct sandbox_state *state,
567 struct udevice *bus, struct udevice *slave,
568 struct udevice **emulp)
569 {
570 struct sandbox_spi_info *info;
571 int busnum = bus->seq;
572 int cs = spi_chip_select(slave);
573 int ret;
574
575 info = &state->spi[busnum][cs];
576 if (!info->emul) {
577 /* Use the same device tree node as the SPI flash device */
578 debug("%s: busnum=%u, cs=%u: binding SPI flash emulation: ",
579 __func__, busnum, cs);
580 ret = sandbox_sf_bind_emul(state, busnum, cs, bus,
581 dev_ofnode(slave), slave->name);
582 if (ret) {
583 debug("failed (err=%d)\n", ret);
584 return ret;
585 }
586 debug("OK\n");
587 }
588 *emulp = info->emul;
589
590 return 0;
591 }
592 #endif
593
594 static const struct udevice_id sandbox_sf_ids[] = {
595 { .compatible = "sandbox,spi-flash" },
596 { }
597 };
598
599 U_BOOT_DRIVER(sandbox_sf_emul) = {
600 .name = "sandbox_sf_emul",
601 .id = UCLASS_SPI_EMUL,
602 .of_match = sandbox_sf_ids,
603 .ofdata_to_platdata = sandbox_sf_ofdata_to_platdata,
604 .probe = sandbox_sf_probe,
605 .remove = sandbox_sf_remove,
606 .priv_auto_alloc_size = sizeof(struct sandbox_spi_flash),
607 .platdata_auto_alloc_size = sizeof(struct sandbox_spi_flash_plat_data),
608 .ops = &sandbox_sf_emul_ops,
609 };
610