1
2
3
4
5
6
7
8
9
10
11#include <common.h>
12#include <dm.h>
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>
23#include <dm/device-internal.h>
24#include <dm/lists.h>
25#include <dm/uclass-internal.h>
26
27DECLARE_GLOBAL_DATA_PTR;
28
29
30
31
32
33
34enum sandbox_sf_state {
35 SF_CMD,
36 SF_ID,
37 SF_ADDR,
38 SF_READ,
39 SF_WRITE,
40 SF_ERASE,
41 SF_READ_STATUS,
42 SF_READ_STATUS1,
43 SF_WRITE_STATUS,
44};
45
46static 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",
50 "READ_STATUS1", "WRITE_STATUS",
51 };
52 return states[state];
53}
54
55
56#define STAT_WIP (1 << 0)
57#define STAT_WEL (1 << 1)
58
59
60#define SF_ADDR_LEN 3
61
62#define IDCODE_LEN 3
63
64
65static u8 sandbox_sf_0xff[0x1000];
66
67
68struct sandbox_spi_flash {
69 unsigned int cs;
70
71
72
73
74
75
76
77
78
79
80 enum sandbox_sf_state state;
81 uint cmd;
82
83 uint erase_size;
84
85 uint off;
86
87 uint addr_bytes, pad_addr_bytes;
88
89 u16 status;
90
91 const struct spi_flash_info *data;
92
93 int fd;
94};
95
96struct sandbox_spi_flash_plat_data {
97 const char *filename;
98 const char *device_name;
99 int bus;
100 int cs;
101};
102
103
104
105
106
107
108
109static int sandbox_sf_probe(struct udevice *dev)
110{
111
112 struct sandbox_spi_flash *sbsf = dev_get_priv(dev);
113 const char *file;
114 size_t len, idname_len;
115 const struct spi_flash_info *data;
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;
123
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 }
130 }
131 if (cs == -1) {
132 printf("Error: Unknown chip select for device '%s'\n",
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;
144 if (!spec) {
145 debug("%s: No spec found for bus %d, cs %d\n",
146 __func__, bus->seq, cs);
147 ret = -ENOENT;
148 goto error;
149 }
150
151 file = strchr(spec, ':');
152 if (!file) {
153 printf("%s: unable to parse file\n", __func__);
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);
170
171 for (data = spi_flash_ids; data->name; data++) {
172 len = strlen(data->name);
173 if (idname_len != len)
174 continue;
175 if (!strncasecmp(spec, data->name, len))
176 break;
177 }
178 if (!data->name) {
179 printf("%s: unknown flash '%*s'\n", __func__, (int)idname_len,
180 spec);
181 ret = -EINVAL;
182 goto error;
183 }
184
185 if (sandbox_sf_0xff[0] == 0x00)
186 memset(sandbox_sf_0xff, 0xff, sizeof(sandbox_sf_0xff));
187
188 sbsf->fd = os_open(pdata->filename, 02);
189 if (sbsf->fd == -1) {
190 printf("%s: unable to open file '%s'\n", __func__,
191 pdata->filename);
192 ret = -EIO;
193 goto error;
194 }
195
196 sbsf->data = data;
197 sbsf->cs = cs;
198
199 return 0;
200
201 error:
202 debug("%s: Got error %d\n", __func__, ret);
203 return ret;
204}
205
206static int sandbox_sf_remove(struct udevice *dev)
207{
208 struct sandbox_spi_flash *sbsf = dev_get_priv(dev);
209
210 os_close(sbsf->fd);
211
212 return 0;
213}
214
215static void sandbox_sf_cs_activate(struct udevice *dev)
216{
217 struct sandbox_spi_flash *sbsf = dev_get_priv(dev);
218
219 debug("sandbox_sf: CS activated; state is fresh!\n");
220
221
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
229static void sandbox_sf_cs_deactivate(struct udevice *dev)
230{
231 debug("sandbox_sf: CS deactivated; cmd done processing!\n");
232}
233
234
235
236
237
238
239
240
241static void sandbox_spi_tristate(u8 *buf, uint len)
242{
243
244 memset(buf, 0xff, len);
245}
246
247
248static 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
254 if (tx)
255 sandbox_spi_tristate(tx, 1);
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:
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;
283 case CMD_WRITE_STATUS:
284 sbsf->state = SF_WRITE_STATUS;
285 break;
286 default: {
287 int flags = sbsf->data->flags;
288
289
290 if (sbsf->cmd == CMD_ERASE_CHIP) {
291 sbsf->erase_size = sbsf->data->sector_size *
292 sbsf->data->n_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_64K && !(flags & SECT_4K)) {
296 sbsf->erase_size = 64 << 10;
297 } else {
298 debug(" cmd unknown: %#x\n", sbsf->cmd);
299 return -EIO;
300 }
301 sbsf->state = SF_ADDR;
302 break;
303 }
304 }
305
306 if (oldstate != sbsf->state)
307 debug(" cmd: transition to %s state\n",
308 sandbox_sf_state_name(sbsf->state));
309
310 return 0;
311}
312
313int sandbox_erase_part(struct sandbox_spi_flash *sbsf, int size)
314{
315 int todo;
316 int ret;
317
318 while (size > 0) {
319 todo = min(size, (int)sizeof(sandbox_sf_0xff));
320 ret = os_write(sbsf->fd, sandbox_sf_0xff, todo);
321 if (ret != todo)
322 return ret;
323 size -= todo;
324 }
325
326 return 0;
327}
328
329static int sandbox_sf_xfer(struct udevice *dev, unsigned int bitlen,
330 const void *rxp, void *txp, unsigned long flags)
331{
332 struct sandbox_spi_flash *sbsf = dev_get_priv(dev);
333 const uint8_t *rx = rxp;
334 uint8_t *tx = txp;
335 uint cnt, pos = 0;
336 int bytes = bitlen / 8;
337 int ret;
338
339 debug("sandbox_sf: state:%x(%s) bytes:%u\n", sbsf->state,
340 sandbox_sf_state_name(sbsf->state), bytes);
341
342 if ((flags & SPI_XFER_BEGIN))
343 sandbox_sf_cs_activate(dev);
344
345 if (sbsf->state == SF_CMD) {
346
347 ret = sandbox_sf_process_cmd(sbsf, rx, tx);
348 if (ret)
349 return ret;
350 ++pos;
351 }
352
353
354 while (pos < bytes) {
355 switch (sbsf->state) {
356 case SF_ID: {
357 u8 id;
358
359 debug(" id: off:%u tx:", sbsf->off);
360 if (sbsf->off < IDCODE_LEN) {
361
362 id = ((JEDEC_MFR(sbsf->data) << 16) |
363 JEDEC_ID(sbsf->data)) >>
364 (8 * (IDCODE_LEN - 1 - sbsf->off));
365 } else {
366 id = 0;
367 }
368 debug("%d %02x\n", sbsf->off, id);
369 tx[pos++] = id;
370 ++sbsf->off;
371 break;
372 }
373 case SF_ADDR:
374 debug(" addr: bytes:%u rx:%02x ", sbsf->addr_bytes,
375 rx[pos]);
376
377 if (sbsf->addr_bytes++ < SF_ADDR_LEN)
378 sbsf->off = (sbsf->off << 8) | rx[pos];
379 debug("addr:%06x\n", sbsf->off);
380
381 if (tx)
382 sandbox_spi_tristate(&tx[pos], 1);
383 pos++;
384
385
386 if (sbsf->addr_bytes <
387 SF_ADDR_LEN + sbsf->pad_addr_bytes)
388 break;
389
390
391 if (os_lseek(sbsf->fd, sbsf->off, OS_SEEK_SET) < 0) {
392 puts("sandbox_sf: os_lseek() failed");
393 return -EIO;
394 }
395 switch (sbsf->cmd) {
396 case CMD_READ_ARRAY_FAST:
397 case CMD_READ_ARRAY_SLOW:
398 sbsf->state = SF_READ;
399 break;
400 case CMD_PAGE_PROGRAM:
401 sbsf->state = SF_WRITE;
402 break;
403 default:
404
405 sbsf->state = SF_ERASE;
406 goto case_sf_erase;
407 }
408 debug(" cmd: transition to %s state\n",
409 sandbox_sf_state_name(sbsf->state));
410 break;
411 case SF_READ:
412
413
414
415
416
417 cnt = bytes - pos;
418 debug(" tx: read(%u)\n", cnt);
419 assert(tx);
420 ret = os_read(sbsf->fd, tx + pos, cnt);
421 if (ret < 0) {
422 puts("sandbox_sf: os_read() failed\n");
423 return -EIO;
424 }
425 pos += ret;
426 break;
427 case SF_READ_STATUS:
428 debug(" read status: %#x\n", sbsf->status);
429 cnt = bytes - pos;
430 memset(tx + pos, sbsf->status, cnt);
431 pos += cnt;
432 break;
433 case SF_READ_STATUS1:
434 debug(" read status: %#x\n", sbsf->status);
435 cnt = bytes - pos;
436 memset(tx + pos, sbsf->status >> 8, cnt);
437 pos += cnt;
438 break;
439 case SF_WRITE_STATUS:
440 debug(" write status: %#x (ignored)\n", rx[pos]);
441 pos = bytes;
442 break;
443 case SF_WRITE:
444
445
446
447
448
449
450 if (!(sbsf->status & STAT_WEL)) {
451 puts("sandbox_sf: write enable not set before write\n");
452 goto done;
453 }
454
455 cnt = bytes - pos;
456 debug(" rx: write(%u)\n", cnt);
457 if (tx)
458 sandbox_spi_tristate(&tx[pos], cnt);
459 ret = os_write(sbsf->fd, rx + pos, cnt);
460 if (ret < 0) {
461 puts("sandbox_spi: os_write() failed\n");
462 return -EIO;
463 }
464 pos += ret;
465 sbsf->status &= ~STAT_WEL;
466 break;
467 case SF_ERASE:
468 case_sf_erase: {
469 if (!(sbsf->status & STAT_WEL)) {
470 puts("sandbox_sf: write enable not set before erase\n");
471 goto done;
472 }
473
474
475 if (sbsf->off & (sbsf->erase_size - 1)) {
476 debug(" sector erase: cmd:%#x needs align:%#x, but we got %#x\n",
477 sbsf->cmd, sbsf->erase_size,
478 sbsf->off);
479 sbsf->status &= ~STAT_WEL;
480 goto done;
481 }
482
483 debug(" sector erase addr: %u, size: %u\n", sbsf->off,
484 sbsf->erase_size);
485
486 cnt = bytes - pos;
487 if (tx)
488 sandbox_spi_tristate(&tx[pos], cnt);
489 pos += cnt;
490
491
492
493
494
495 ret = sandbox_erase_part(sbsf, sbsf->erase_size);
496 sbsf->status &= ~STAT_WEL;
497 if (ret) {
498 debug("sandbox_sf: Erase failed\n");
499 goto done;
500 }
501 goto done;
502 }
503 default:
504 debug(" ??? no idea what to do ???\n");
505 goto done;
506 }
507 }
508
509 done:
510 if (flags & SPI_XFER_END)
511 sandbox_sf_cs_deactivate(dev);
512 return pos == bytes ? 0 : -EIO;
513}
514
515int sandbox_sf_ofdata_to_platdata(struct udevice *dev)
516{
517 struct sandbox_spi_flash_plat_data *pdata = dev_get_platdata(dev);
518
519 pdata->filename = dev_read_string(dev, "sandbox,filename");
520 pdata->device_name = dev_read_string(dev, "compatible");
521 if (!pdata->filename || !pdata->device_name) {
522 debug("%s: Missing properties, filename=%s, device_name=%s\n",
523 __func__, pdata->filename, pdata->device_name);
524 return -EINVAL;
525 }
526
527 return 0;
528}
529
530static const struct dm_spi_emul_ops sandbox_sf_emul_ops = {
531 .xfer = sandbox_sf_xfer,
532};
533
534#ifdef CONFIG_SPI_FLASH
535static int sandbox_cmdline_cb_spi_sf(struct sandbox_state *state,
536 const char *arg)
537{
538 unsigned long bus, cs;
539 const char *spec = sandbox_spi_parse_spec(arg, &bus, &cs);
540
541 if (!spec)
542 return 1;
543
544
545
546
547
548
549
550
551
552 state->spi[bus][cs].spec = spec;
553 debug("%s: Setting up spec '%s' for bus %ld, cs %ld\n", __func__,
554 spec, bus, cs);
555
556 return 0;
557}
558SANDBOX_CMDLINE_OPT(spi_sf, 1, "connect a SPI flash: <bus>:<cs>:<id>:<file>");
559
560int sandbox_sf_bind_emul(struct sandbox_state *state, int busnum, int cs,
561 struct udevice *bus, int of_offset, const char *spec)
562{
563 struct udevice *emul;
564 char name[20], *str;
565 struct driver *drv;
566 int ret;
567
568
569 strncpy(name, spec, sizeof(name) - 6);
570 name[sizeof(name) - 6] = '\0';
571 strcat(name, "-emul");
572 str = strdup(name);
573 if (!str)
574 return -ENOMEM;
575 drv = lists_driver_lookup_name("sandbox_sf_emul");
576 if (!drv) {
577 puts("Cannot find sandbox_sf_emul driver\n");
578 return -ENOENT;
579 }
580 ret = device_bind(bus, drv, str, NULL, of_offset, &emul);
581 if (ret) {
582 printf("Cannot create emul device for spec '%s' (err=%d)\n",
583 spec, ret);
584 return ret;
585 }
586 state->spi[busnum][cs].emul = emul;
587
588 return 0;
589}
590
591void sandbox_sf_unbind_emul(struct sandbox_state *state, int busnum, int cs)
592{
593 struct udevice *dev;
594
595 dev = state->spi[busnum][cs].emul;
596 device_remove(dev, DM_REMOVE_NORMAL);
597 device_unbind(dev);
598 state->spi[busnum][cs].emul = NULL;
599}
600
601static int sandbox_sf_bind_bus_cs(struct sandbox_state *state, int busnum,
602 int cs, const char *spec)
603{
604 struct udevice *bus, *slave;
605 int ret;
606
607 ret = uclass_find_device_by_seq(UCLASS_SPI, busnum, true, &bus);
608 if (ret) {
609 printf("Invalid bus %d for spec '%s' (err=%d)\n", busnum,
610 spec, ret);
611 return ret;
612 }
613 ret = spi_find_chip_select(bus, cs, &slave);
614 if (!ret) {
615 printf("Chip select %d already exists for spec '%s'\n", cs,
616 spec);
617 return -EEXIST;
618 }
619
620 ret = device_bind_driver(bus, "spi_flash_std", spec, &slave);
621 if (ret)
622 return ret;
623
624 return sandbox_sf_bind_emul(state, busnum, cs, bus, -1, spec);
625}
626
627int sandbox_spi_get_emul(struct sandbox_state *state,
628 struct udevice *bus, struct udevice *slave,
629 struct udevice **emulp)
630{
631 struct sandbox_spi_info *info;
632 int busnum = bus->seq;
633 int cs = spi_chip_select(slave);
634 int ret;
635
636 info = &state->spi[busnum][cs];
637 if (!info->emul) {
638
639 debug("%s: busnum=%u, cs=%u: binding SPI flash emulation: ",
640 __func__, busnum, cs);
641 ret = sandbox_sf_bind_emul(state, busnum, cs, bus,
642 dev_of_offset(slave), slave->name);
643 if (ret) {
644 debug("failed (err=%d)\n", ret);
645 return ret;
646 }
647 debug("OK\n");
648 }
649 *emulp = info->emul;
650
651 return 0;
652}
653
654int dm_scan_other(bool pre_reloc_only)
655{
656 struct sandbox_state *state = state_get_current();
657 int busnum, cs;
658
659 if (pre_reloc_only)
660 return 0;
661 for (busnum = 0; busnum < CONFIG_SANDBOX_SPI_MAX_BUS; busnum++) {
662 for (cs = 0; cs < CONFIG_SANDBOX_SPI_MAX_CS; cs++) {
663 const char *spec = state->spi[busnum][cs].spec;
664 int ret;
665
666 if (spec) {
667 ret = sandbox_sf_bind_bus_cs(state, busnum,
668 cs, spec);
669 if (ret) {
670 debug("%s: Bind failed for bus %d, cs %d\n",
671 __func__, busnum, cs);
672 return ret;
673 }
674 debug("%s: Setting up spec '%s' for bus %d, cs %d\n",
675 __func__, spec, busnum, cs);
676 }
677 }
678 }
679
680 return 0;
681}
682#endif
683
684static const struct udevice_id sandbox_sf_ids[] = {
685 { .compatible = "sandbox,spi-flash" },
686 { }
687};
688
689U_BOOT_DRIVER(sandbox_sf_emul) = {
690 .name = "sandbox_sf_emul",
691 .id = UCLASS_SPI_EMUL,
692 .of_match = sandbox_sf_ids,
693 .ofdata_to_platdata = sandbox_sf_ofdata_to_platdata,
694 .probe = sandbox_sf_probe,
695 .remove = sandbox_sf_remove,
696 .priv_auto_alloc_size = sizeof(struct sandbox_spi_flash),
697 .platdata_auto_alloc_size = sizeof(struct sandbox_spi_flash_plat_data),
698 .ops = &sandbox_sf_emul_ops,
699};
700