blob: 755f176861490e5cadb428a7f1256406eb3ae5d9 [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
Simon Glass3b4b3402018-10-01 11:55:13 -060011#define LOG_CATEGORY UCLASS_SPI
12
Mike Frysinger1d8e57c2013-12-03 16:43:26 -070013#include <common.h>
Simon Glass95429fe2014-10-13 23:41:57 -060014#include <dm.h>
Simon Glass0f2af882020-05-10 11:40:05 -060015#include <log.h>
Mike Frysinger1d8e57c2013-12-03 16:43:26 -070016#include <malloc.h>
17#include <spi.h>
Simon Glass95429fe2014-10-13 23:41:57 -060018#include <spi_flash.h>
Mike Frysinger1d8e57c2013-12-03 16:43:26 -070019#include <os.h>
20
Masahiro Yamada56a931c2016-09-21 11:28:55 +090021#include <linux/errno.h>
Mike Frysinger1d8e57c2013-12-03 16:43:26 -070022#include <asm/spi.h>
23#include <asm/state.h>
Simon Glass711fd982020-07-07 13:11:49 -060024#include <dm/acpi.h>
Simon Glass95429fe2014-10-13 23:41:57 -060025#include <dm/device-internal.h>
26
Mike Frysinger1d8e57c2013-12-03 16:43:26 -070027#ifndef CONFIG_SPI_IDLE_VAL
28# define CONFIG_SPI_IDLE_VAL 0xFF
29#endif
30
Mike Frysinger1d8e57c2013-12-03 16:43:26 -070031const char *sandbox_spi_parse_spec(const char *arg, unsigned long *bus,
32 unsigned long *cs)
33{
34 char *endp;
35
36 *bus = simple_strtoul(arg, &endp, 0);
37 if (*endp != ':' || *bus >= CONFIG_SANDBOX_SPI_MAX_BUS)
38 return NULL;
39
40 *cs = simple_strtoul(endp + 1, &endp, 0);
41 if (*endp != ':' || *cs >= CONFIG_SANDBOX_SPI_MAX_CS)
42 return NULL;
43
44 return endp + 1;
45}
46
Simon Glass95429fe2014-10-13 23:41:57 -060047__weak int sandbox_spi_get_emul(struct sandbox_state *state,
48 struct udevice *bus, struct udevice *slave,
49 struct udevice **emulp)
Mike Frysinger1d8e57c2013-12-03 16:43:26 -070050{
Simon Glass95429fe2014-10-13 23:41:57 -060051 return -ENOENT;
Mike Frysinger1d8e57c2013-12-03 16:43:26 -070052}
53
Simon Glass95429fe2014-10-13 23:41:57 -060054static int sandbox_spi_xfer(struct udevice *slave, unsigned int bitlen,
55 const void *dout, void *din, unsigned long flags)
Mike Frysinger1d8e57c2013-12-03 16:43:26 -070056{
Simon Glass95429fe2014-10-13 23:41:57 -060057 struct udevice *bus = slave->parent;
Mike Frysinger1d8e57c2013-12-03 16:43:26 -070058 struct sandbox_state *state = state_get_current();
Simon Glass95429fe2014-10-13 23:41:57 -060059 struct dm_spi_emul_ops *ops;
60 struct udevice *emul;
Mike Frysinger1d8e57c2013-12-03 16:43:26 -070061 uint bytes = bitlen / 8, i;
Simon Glass95429fe2014-10-13 23:41:57 -060062 int ret;
Simon Glass95429fe2014-10-13 23:41:57 -060063 uint busnum, cs;
Mike Frysinger1d8e57c2013-12-03 16:43:26 -070064
65 if (bitlen == 0)
Simon Glass95429fe2014-10-13 23:41:57 -060066 return 0;
Mike Frysinger1d8e57c2013-12-03 16:43:26 -070067
68 /* we can only do 8 bit transfers */
69 if (bitlen % 8) {
70 printf("sandbox_spi: xfer: invalid bitlen size %u; needs to be 8bit\n",
71 bitlen);
Simon Glass95429fe2014-10-13 23:41:57 -060072 return -EINVAL;
Mike Frysinger1d8e57c2013-12-03 16:43:26 -070073 }
74
Simon Glass95429fe2014-10-13 23:41:57 -060075 busnum = bus->seq;
76 cs = spi_chip_select(slave);
77 if (busnum >= CONFIG_SANDBOX_SPI_MAX_BUS ||
78 cs >= CONFIG_SANDBOX_SPI_MAX_CS) {
79 printf("%s: busnum=%u, cs=%u: out of range\n", __func__,
80 busnum, cs);
81 return -ENOENT;
82 }
83 ret = sandbox_spi_get_emul(state, bus, slave, &emul);
84 if (ret) {
85 printf("%s: busnum=%u, cs=%u: no emulation available (err=%d)\n",
86 __func__, busnum, cs, ret);
87 return -ENOENT;
88 }
89 ret = device_probe(emul);
90 if (ret)
91 return ret;
Mike Frysinger1d8e57c2013-12-03 16:43:26 -070092
Simon Glass95429fe2014-10-13 23:41:57 -060093 ops = spi_emul_get_ops(emul);
94 ret = ops->xfer(emul, bitlen, dout, din, flags);
Mike Frysinger1d8e57c2013-12-03 16:43:26 -070095
Simon Glass3b4b3402018-10-01 11:55:13 -060096 log_content("sandbox_spi: xfer: got back %i (that's %s)\n rx:",
97 ret, ret ? "bad" : "good");
98 if (din) {
99 for (i = 0; i < bytes; ++i)
100 log_content(" %u:%02x", i, ((u8 *)din)[i]);
101 }
102 log_content("\n");
Mike Frysinger1d8e57c2013-12-03 16:43:26 -0700103
Mike Frysinger1d8e57c2013-12-03 16:43:26 -0700104 return ret;
105}
Simon Glassfe27ded2014-02-27 13:26:24 -0700106
Simon Glass95429fe2014-10-13 23:41:57 -0600107static int sandbox_spi_set_speed(struct udevice *bus, uint speed)
Simon Glassfe27ded2014-02-27 13:26:24 -0700108{
Simon Glass95429fe2014-10-13 23:41:57 -0600109 return 0;
Simon Glassfe27ded2014-02-27 13:26:24 -0700110}
Simon Glass95429fe2014-10-13 23:41:57 -0600111
112static int sandbox_spi_set_mode(struct udevice *bus, uint mode)
113{
114 return 0;
115}
116
117static int sandbox_cs_info(struct udevice *bus, uint cs,
118 struct spi_cs_info *info)
119{
120 /* Always allow activity on CS 0 */
121 if (cs >= 1)
Bin Mengf8586f62019-09-09 06:00:01 -0700122 return -EINVAL;
Simon Glass95429fe2014-10-13 23:41:57 -0600123
124 return 0;
125}
126
Simon Glass37ad0fe2019-10-20 21:31:47 -0600127static int sandbox_spi_get_mmap(struct udevice *dev, ulong *map_basep,
128 uint *map_sizep, uint *offsetp)
129{
130 *map_basep = 0x1000;
131 *map_sizep = 0x2000;
132 *offsetp = 0x100;
133
134 return 0;
135}
136
Simon Glass95429fe2014-10-13 23:41:57 -0600137static const struct dm_spi_ops sandbox_spi_ops = {
138 .xfer = sandbox_spi_xfer,
139 .set_speed = sandbox_spi_set_speed,
140 .set_mode = sandbox_spi_set_mode,
141 .cs_info = sandbox_cs_info,
Simon Glass37ad0fe2019-10-20 21:31:47 -0600142 .get_mmap = sandbox_spi_get_mmap,
Simon Glass95429fe2014-10-13 23:41:57 -0600143};
144
145static const struct udevice_id sandbox_spi_ids[] = {
146 { .compatible = "sandbox,spi" },
147 { }
148};
149
Walter Lozano2901ac62020-06-25 01:10:04 -0300150U_BOOT_DRIVER(sandbox_spi) = {
151 .name = "sandbox_spi",
Simon Glass95429fe2014-10-13 23:41:57 -0600152 .id = UCLASS_SPI,
153 .of_match = sandbox_spi_ids,
Simon Glass95429fe2014-10-13 23:41:57 -0600154 .ops = &sandbox_spi_ops,
155};