blob: 75ba6a1ed7ef2000cf691dbd4c19ae7181798b50 [file] [log] [blame]
Mike Frysinger1d8e57c2013-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 Glass95429fe2014-10-13 23:41:57 -060012#include <dm.h>
Mike Frysinger1d8e57c2013-12-03 16:43:26 -070013#include <malloc.h>
14#include <spi.h>
Simon Glass95429fe2014-10-13 23:41:57 -060015#include <spi_flash.h>
Mike Frysinger1d8e57c2013-12-03 16:43:26 -070016#include <os.h>
17
Masahiro Yamada56a931c2016-09-21 11:28:55 +090018#include <linux/errno.h>
Mike Frysinger1d8e57c2013-12-03 16:43:26 -070019#include <asm/spi.h>
20#include <asm/state.h>
Simon Glass95429fe2014-10-13 23:41:57 -060021#include <dm/device-internal.h>
22
Mike Frysinger1d8e57c2013-12-03 16:43:26 -070023#ifndef CONFIG_SPI_IDLE_VAL
24# define CONFIG_SPI_IDLE_VAL 0xFF
25#endif
26
Mike Frysinger1d8e57c2013-12-03 16:43:26 -070027const char *sandbox_spi_parse_spec(const char *arg, unsigned long *bus,
28 unsigned long *cs)
29{
30 char *endp;
31
32 *bus = simple_strtoul(arg, &endp, 0);
33 if (*endp != ':' || *bus >= CONFIG_SANDBOX_SPI_MAX_BUS)
34 return NULL;
35
36 *cs = simple_strtoul(endp + 1, &endp, 0);
37 if (*endp != ':' || *cs >= CONFIG_SANDBOX_SPI_MAX_CS)
38 return NULL;
39
40 return endp + 1;
41}
42
Simon Glass95429fe2014-10-13 23:41:57 -060043__weak int sandbox_spi_get_emul(struct sandbox_state *state,
44 struct udevice *bus, struct udevice *slave,
45 struct udevice **emulp)
Mike Frysinger1d8e57c2013-12-03 16:43:26 -070046{
Simon Glass95429fe2014-10-13 23:41:57 -060047 return -ENOENT;
Mike Frysinger1d8e57c2013-12-03 16:43:26 -070048}
49
Simon Glass95429fe2014-10-13 23:41:57 -060050static int sandbox_spi_xfer(struct udevice *slave, unsigned int bitlen,
51 const void *dout, void *din, unsigned long flags)
Mike Frysinger1d8e57c2013-12-03 16:43:26 -070052{
Simon Glass95429fe2014-10-13 23:41:57 -060053 struct udevice *bus = slave->parent;
Mike Frysinger1d8e57c2013-12-03 16:43:26 -070054 struct sandbox_state *state = state_get_current();
Simon Glass95429fe2014-10-13 23:41:57 -060055 struct dm_spi_emul_ops *ops;
56 struct udevice *emul;
Mike Frysinger1d8e57c2013-12-03 16:43:26 -070057 uint bytes = bitlen / 8, i;
Simon Glass95429fe2014-10-13 23:41:57 -060058 int ret;
Mike Frysinger1d8e57c2013-12-03 16:43:26 -070059 u8 *tx = (void *)dout, *rx = din;
Simon Glass95429fe2014-10-13 23:41:57 -060060 uint busnum, cs;
Mike Frysinger1d8e57c2013-12-03 16:43:26 -070061
62 if (bitlen == 0)
Simon Glass95429fe2014-10-13 23:41:57 -060063 return 0;
Mike Frysinger1d8e57c2013-12-03 16:43:26 -070064
65 /* we can only do 8 bit transfers */
66 if (bitlen % 8) {
67 printf("sandbox_spi: xfer: invalid bitlen size %u; needs to be 8bit\n",
68 bitlen);
Simon Glass95429fe2014-10-13 23:41:57 -060069 return -EINVAL;
Mike Frysinger1d8e57c2013-12-03 16:43:26 -070070 }
71
Simon Glass95429fe2014-10-13 23:41:57 -060072 busnum = bus->seq;
73 cs = spi_chip_select(slave);
74 if (busnum >= CONFIG_SANDBOX_SPI_MAX_BUS ||
75 cs >= CONFIG_SANDBOX_SPI_MAX_CS) {
76 printf("%s: busnum=%u, cs=%u: out of range\n", __func__,
77 busnum, cs);
78 return -ENOENT;
79 }
80 ret = sandbox_spi_get_emul(state, bus, slave, &emul);
81 if (ret) {
82 printf("%s: busnum=%u, cs=%u: no emulation available (err=%d)\n",
83 __func__, busnum, cs, ret);
84 return -ENOENT;
85 }
86 ret = device_probe(emul);
87 if (ret)
88 return ret;
Mike Frysinger1d8e57c2013-12-03 16:43:26 -070089
90 /* make sure rx/tx buffers are full so clients can assume */
91 if (!tx) {
92 debug("sandbox_spi: xfer: auto-allocating tx scratch buffer\n");
93 tx = malloc(bytes);
94 if (!tx) {
95 debug("sandbox_spi: Out of memory\n");
96 return -ENOMEM;
97 }
98 }
99 if (!rx) {
100 debug("sandbox_spi: xfer: auto-allocating rx scratch buffer\n");
101 rx = malloc(bytes);
102 if (!rx) {
103 debug("sandbox_spi: Out of memory\n");
104 return -ENOMEM;
105 }
106 }
107
Simon Glass95429fe2014-10-13 23:41:57 -0600108 ops = spi_emul_get_ops(emul);
109 ret = ops->xfer(emul, bitlen, dout, din, flags);
Mike Frysinger1d8e57c2013-12-03 16:43:26 -0700110
111 debug("sandbox_spi: xfer: got back %i (that's %s)\n rx:",
112 ret, ret ? "bad" : "good");
113 for (i = 0; i < bytes; ++i)
114 debug(" %u:%02x", i, rx[i]);
115 debug("\n");
116
117 if (tx != dout)
118 free(tx);
119 if (rx != din)
120 free(rx);
121
Mike Frysinger1d8e57c2013-12-03 16:43:26 -0700122 return ret;
123}
Simon Glassfe27ded2014-02-27 13:26:24 -0700124
Simon Glass95429fe2014-10-13 23:41:57 -0600125static int sandbox_spi_set_speed(struct udevice *bus, uint speed)
Simon Glassfe27ded2014-02-27 13:26:24 -0700126{
Simon Glass95429fe2014-10-13 23:41:57 -0600127 return 0;
Simon Glassfe27ded2014-02-27 13:26:24 -0700128}
Simon Glass95429fe2014-10-13 23:41:57 -0600129
130static int sandbox_spi_set_mode(struct udevice *bus, uint mode)
131{
132 return 0;
133}
134
135static int sandbox_cs_info(struct udevice *bus, uint cs,
136 struct spi_cs_info *info)
137{
138 /* Always allow activity on CS 0 */
139 if (cs >= 1)
140 return -ENODEV;
141
142 return 0;
143}
144
145static const struct dm_spi_ops sandbox_spi_ops = {
146 .xfer = sandbox_spi_xfer,
147 .set_speed = sandbox_spi_set_speed,
148 .set_mode = sandbox_spi_set_mode,
149 .cs_info = sandbox_cs_info,
150};
151
152static const struct udevice_id sandbox_spi_ids[] = {
153 { .compatible = "sandbox,spi" },
154 { }
155};
156
157U_BOOT_DRIVER(spi_sandbox) = {
158 .name = "spi_sandbox",
159 .id = UCLASS_SPI,
160 .of_match = sandbox_spi_ids,
Simon Glass95429fe2014-10-13 23:41:57 -0600161 .ops = &sandbox_spi_ops,
162};