blob: 092b13b00bf65e8153b7cc8394da2d757057cd69 [file] [log] [blame]
Mike Frysinger61228132013-12-03 16:43:26 -07001/*
2 * Simulate a SPI port
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 Glass49b5d6e2014-10-13 23:41:57 -060012#include <dm.h>
Mike Frysinger61228132013-12-03 16:43:26 -070013#include <malloc.h>
14#include <spi.h>
Simon Glass49b5d6e2014-10-13 23:41:57 -060015#include <spi_flash.h>
Mike Frysinger61228132013-12-03 16:43:26 -070016#include <os.h>
17
Masahiro Yamada1221ce42016-09-21 11:28:55 +090018#include <linux/errno.h>
Mike Frysinger61228132013-12-03 16:43:26 -070019#include <asm/spi.h>
20#include <asm/state.h>
Simon Glass49b5d6e2014-10-13 23:41:57 -060021#include <dm/device-internal.h>
22
23DECLARE_GLOBAL_DATA_PTR;
Mike Frysinger61228132013-12-03 16:43:26 -070024
25#ifndef CONFIG_SPI_IDLE_VAL
26# define CONFIG_SPI_IDLE_VAL 0xFF
27#endif
28
Mike Frysinger61228132013-12-03 16:43:26 -070029const char *sandbox_spi_parse_spec(const char *arg, unsigned long *bus,
30 unsigned long *cs)
31{
32 char *endp;
33
34 *bus = simple_strtoul(arg, &endp, 0);
35 if (*endp != ':' || *bus >= CONFIG_SANDBOX_SPI_MAX_BUS)
36 return NULL;
37
38 *cs = simple_strtoul(endp + 1, &endp, 0);
39 if (*endp != ':' || *cs >= CONFIG_SANDBOX_SPI_MAX_CS)
40 return NULL;
41
42 return endp + 1;
43}
44
Simon Glass49b5d6e2014-10-13 23:41:57 -060045__weak int sandbox_spi_get_emul(struct sandbox_state *state,
46 struct udevice *bus, struct udevice *slave,
47 struct udevice **emulp)
Mike Frysinger61228132013-12-03 16:43:26 -070048{
Simon Glass49b5d6e2014-10-13 23:41:57 -060049 return -ENOENT;
Mike Frysinger61228132013-12-03 16:43:26 -070050}
51
Simon Glass49b5d6e2014-10-13 23:41:57 -060052static int sandbox_spi_xfer(struct udevice *slave, unsigned int bitlen,
53 const void *dout, void *din, unsigned long flags)
Mike Frysinger61228132013-12-03 16:43:26 -070054{
Simon Glass49b5d6e2014-10-13 23:41:57 -060055 struct udevice *bus = slave->parent;
Mike Frysinger61228132013-12-03 16:43:26 -070056 struct sandbox_state *state = state_get_current();
Simon Glass49b5d6e2014-10-13 23:41:57 -060057 struct dm_spi_emul_ops *ops;
58 struct udevice *emul;
Mike Frysinger61228132013-12-03 16:43:26 -070059 uint bytes = bitlen / 8, i;
Simon Glass49b5d6e2014-10-13 23:41:57 -060060 int ret;
Mike Frysinger61228132013-12-03 16:43:26 -070061 u8 *tx = (void *)dout, *rx = din;
Simon Glass49b5d6e2014-10-13 23:41:57 -060062 uint busnum, cs;
Mike Frysinger61228132013-12-03 16:43:26 -070063
64 if (bitlen == 0)
Simon Glass49b5d6e2014-10-13 23:41:57 -060065 return 0;
Mike Frysinger61228132013-12-03 16:43:26 -070066
67 /* we can only do 8 bit transfers */
68 if (bitlen % 8) {
69 printf("sandbox_spi: xfer: invalid bitlen size %u; needs to be 8bit\n",
70 bitlen);
Simon Glass49b5d6e2014-10-13 23:41:57 -060071 return -EINVAL;
Mike Frysinger61228132013-12-03 16:43:26 -070072 }
73
Simon Glass49b5d6e2014-10-13 23:41:57 -060074 busnum = bus->seq;
75 cs = spi_chip_select(slave);
76 if (busnum >= CONFIG_SANDBOX_SPI_MAX_BUS ||
77 cs >= CONFIG_SANDBOX_SPI_MAX_CS) {
78 printf("%s: busnum=%u, cs=%u: out of range\n", __func__,
79 busnum, cs);
80 return -ENOENT;
81 }
82 ret = sandbox_spi_get_emul(state, bus, slave, &emul);
83 if (ret) {
84 printf("%s: busnum=%u, cs=%u: no emulation available (err=%d)\n",
85 __func__, busnum, cs, ret);
86 return -ENOENT;
87 }
88 ret = device_probe(emul);
89 if (ret)
90 return ret;
Mike Frysinger61228132013-12-03 16:43:26 -070091
92 /* make sure rx/tx buffers are full so clients can assume */
93 if (!tx) {
94 debug("sandbox_spi: xfer: auto-allocating tx scratch buffer\n");
95 tx = malloc(bytes);
96 if (!tx) {
97 debug("sandbox_spi: Out of memory\n");
98 return -ENOMEM;
99 }
100 }
101 if (!rx) {
102 debug("sandbox_spi: xfer: auto-allocating rx scratch buffer\n");
103 rx = malloc(bytes);
104 if (!rx) {
105 debug("sandbox_spi: Out of memory\n");
106 return -ENOMEM;
107 }
108 }
109
Simon Glass49b5d6e2014-10-13 23:41:57 -0600110 ops = spi_emul_get_ops(emul);
111 ret = ops->xfer(emul, bitlen, dout, din, flags);
Mike Frysinger61228132013-12-03 16:43:26 -0700112
113 debug("sandbox_spi: xfer: got back %i (that's %s)\n rx:",
114 ret, ret ? "bad" : "good");
115 for (i = 0; i < bytes; ++i)
116 debug(" %u:%02x", i, rx[i]);
117 debug("\n");
118
119 if (tx != dout)
120 free(tx);
121 if (rx != din)
122 free(rx);
123
Mike Frysinger61228132013-12-03 16:43:26 -0700124 return ret;
125}
Simon Glass6e16d902014-02-27 13:26:24 -0700126
Simon Glass49b5d6e2014-10-13 23:41:57 -0600127static int sandbox_spi_set_speed(struct udevice *bus, uint speed)
Simon Glass6e16d902014-02-27 13:26:24 -0700128{
Simon Glass49b5d6e2014-10-13 23:41:57 -0600129 return 0;
Simon Glass6e16d902014-02-27 13:26:24 -0700130}
Simon Glass49b5d6e2014-10-13 23:41:57 -0600131
132static int sandbox_spi_set_mode(struct udevice *bus, uint mode)
133{
134 return 0;
135}
136
137static int sandbox_cs_info(struct udevice *bus, uint cs,
138 struct spi_cs_info *info)
139{
140 /* Always allow activity on CS 0 */
141 if (cs >= 1)
142 return -ENODEV;
143
144 return 0;
145}
146
147static const struct dm_spi_ops sandbox_spi_ops = {
148 .xfer = sandbox_spi_xfer,
149 .set_speed = sandbox_spi_set_speed,
150 .set_mode = sandbox_spi_set_mode,
151 .cs_info = sandbox_cs_info,
152};
153
154static const struct udevice_id sandbox_spi_ids[] = {
155 { .compatible = "sandbox,spi" },
156 { }
157};
158
159U_BOOT_DRIVER(spi_sandbox) = {
160 .name = "spi_sandbox",
161 .id = UCLASS_SPI,
162 .of_match = sandbox_spi_ids,
Simon Glass49b5d6e2014-10-13 23:41:57 -0600163 .ops = &sandbox_spi_ops,
164};