blob: f844597d04cf8287e6606c23cca60afedb8b4358 [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
Ovidiu Panait05f38812020-12-14 19:06:48 +020027/**
28 * struct sandbox_spi_priv - Sandbox SPI private data
29 *
30 * Helper struct to keep track of the sandbox SPI bus internal state. It is
31 * used in unit tests to verify that dm spi functions update the bus
32 * speed/mode properly (for instance, when jumping back and forth between spi
33 * slaves claiming the bus, we need to make sure that the bus speed is updated
34 * accordingly for each slave).
35 *
36 * @speed: Current bus speed.
37 * @mode: Current bus mode.
38 */
39struct sandbox_spi_priv {
40 uint speed;
41 uint mode;
42};
43
Simon Glass95429fe2014-10-13 23:41:57 -060044__weak int sandbox_spi_get_emul(struct sandbox_state *state,
45 struct udevice *bus, struct udevice *slave,
46 struct udevice **emulp)
Mike Frysinger1d8e57c2013-12-03 16:43:26 -070047{
Simon Glass95429fe2014-10-13 23:41:57 -060048 return -ENOENT;
Mike Frysinger1d8e57c2013-12-03 16:43:26 -070049}
50
Ovidiu Panaita2c9d012020-12-14 19:06:49 +020051uint sandbox_spi_get_speed(struct udevice *dev)
52{
53 struct sandbox_spi_priv *priv = dev_get_priv(dev);
54
55 return priv->speed;
56}
57
58uint sandbox_spi_get_mode(struct udevice *dev)
59{
60 struct sandbox_spi_priv *priv = dev_get_priv(dev);
61
62 return priv->mode;
63}
64
Simon Glass95429fe2014-10-13 23:41:57 -060065static int sandbox_spi_xfer(struct udevice *slave, unsigned int bitlen,
66 const void *dout, void *din, unsigned long flags)
Mike Frysinger1d8e57c2013-12-03 16:43:26 -070067{
Simon Glass95429fe2014-10-13 23:41:57 -060068 struct udevice *bus = slave->parent;
Mike Frysinger1d8e57c2013-12-03 16:43:26 -070069 struct sandbox_state *state = state_get_current();
Simon Glass95429fe2014-10-13 23:41:57 -060070 struct dm_spi_emul_ops *ops;
71 struct udevice *emul;
Mike Frysinger1d8e57c2013-12-03 16:43:26 -070072 uint bytes = bitlen / 8, i;
Simon Glass95429fe2014-10-13 23:41:57 -060073 int ret;
Simon Glass95429fe2014-10-13 23:41:57 -060074 uint busnum, cs;
Mike Frysinger1d8e57c2013-12-03 16:43:26 -070075
76 if (bitlen == 0)
Simon Glass95429fe2014-10-13 23:41:57 -060077 return 0;
Mike Frysinger1d8e57c2013-12-03 16:43:26 -070078
79 /* we can only do 8 bit transfers */
80 if (bitlen % 8) {
81 printf("sandbox_spi: xfer: invalid bitlen size %u; needs to be 8bit\n",
82 bitlen);
Simon Glass95429fe2014-10-13 23:41:57 -060083 return -EINVAL;
Mike Frysinger1d8e57c2013-12-03 16:43:26 -070084 }
85
Simon Glass75e534b2020-12-16 21:20:07 -070086 busnum = dev_seq(bus);
Simon Glass95429fe2014-10-13 23:41:57 -060087 cs = spi_chip_select(slave);
88 if (busnum >= CONFIG_SANDBOX_SPI_MAX_BUS ||
89 cs >= CONFIG_SANDBOX_SPI_MAX_CS) {
90 printf("%s: busnum=%u, cs=%u: out of range\n", __func__,
91 busnum, cs);
92 return -ENOENT;
93 }
94 ret = sandbox_spi_get_emul(state, bus, slave, &emul);
95 if (ret) {
96 printf("%s: busnum=%u, cs=%u: no emulation available (err=%d)\n",
97 __func__, busnum, cs, ret);
98 return -ENOENT;
99 }
100 ret = device_probe(emul);
101 if (ret)
102 return ret;
Mike Frysinger1d8e57c2013-12-03 16:43:26 -0700103
Simon Glass95429fe2014-10-13 23:41:57 -0600104 ops = spi_emul_get_ops(emul);
105 ret = ops->xfer(emul, bitlen, dout, din, flags);
Mike Frysinger1d8e57c2013-12-03 16:43:26 -0700106
Simon Glass3b4b3402018-10-01 11:55:13 -0600107 log_content("sandbox_spi: xfer: got back %i (that's %s)\n rx:",
108 ret, ret ? "bad" : "good");
109 if (din) {
110 for (i = 0; i < bytes; ++i)
111 log_content(" %u:%02x", i, ((u8 *)din)[i]);
112 }
113 log_content("\n");
Mike Frysinger1d8e57c2013-12-03 16:43:26 -0700114
Mike Frysinger1d8e57c2013-12-03 16:43:26 -0700115 return ret;
116}
Simon Glassfe27ded2014-02-27 13:26:24 -0700117
Simon Glass95429fe2014-10-13 23:41:57 -0600118static int sandbox_spi_set_speed(struct udevice *bus, uint speed)
Simon Glassfe27ded2014-02-27 13:26:24 -0700119{
Ovidiu Panait05f38812020-12-14 19:06:48 +0200120 struct sandbox_spi_priv *priv = dev_get_priv(bus);
121
122 priv->speed = speed;
123
Simon Glass95429fe2014-10-13 23:41:57 -0600124 return 0;
Simon Glassfe27ded2014-02-27 13:26:24 -0700125}
Simon Glass95429fe2014-10-13 23:41:57 -0600126
127static int sandbox_spi_set_mode(struct udevice *bus, uint mode)
128{
Ovidiu Panait05f38812020-12-14 19:06:48 +0200129 struct sandbox_spi_priv *priv = dev_get_priv(bus);
130
131 priv->mode = mode;
132
Simon Glass95429fe2014-10-13 23:41:57 -0600133 return 0;
134}
135
136static int sandbox_cs_info(struct udevice *bus, uint cs,
137 struct spi_cs_info *info)
138{
Ovidiu Panaitae734732020-12-14 19:06:47 +0200139 /* Always allow activity on CS 0, CS 1 */
140 if (cs >= 2)
Bin Mengf8586f62019-09-09 06:00:01 -0700141 return -EINVAL;
Simon Glass95429fe2014-10-13 23:41:57 -0600142
143 return 0;
144}
145
Simon Glass37ad0fe2019-10-20 21:31:47 -0600146static int sandbox_spi_get_mmap(struct udevice *dev, ulong *map_basep,
147 uint *map_sizep, uint *offsetp)
148{
149 *map_basep = 0x1000;
150 *map_sizep = 0x2000;
151 *offsetp = 0x100;
152
153 return 0;
154}
155
Simon Glass95429fe2014-10-13 23:41:57 -0600156static const struct dm_spi_ops sandbox_spi_ops = {
157 .xfer = sandbox_spi_xfer,
158 .set_speed = sandbox_spi_set_speed,
159 .set_mode = sandbox_spi_set_mode,
160 .cs_info = sandbox_cs_info,
Simon Glass37ad0fe2019-10-20 21:31:47 -0600161 .get_mmap = sandbox_spi_get_mmap,
Simon Glass95429fe2014-10-13 23:41:57 -0600162};
163
164static const struct udevice_id sandbox_spi_ids[] = {
165 { .compatible = "sandbox,spi" },
166 { }
167};
168
Walter Lozano2901ac62020-06-25 01:10:04 -0300169U_BOOT_DRIVER(sandbox_spi) = {
170 .name = "sandbox_spi",
Simon Glass95429fe2014-10-13 23:41:57 -0600171 .id = UCLASS_SPI,
172 .of_match = sandbox_spi_ids,
Simon Glass95429fe2014-10-13 23:41:57 -0600173 .ops = &sandbox_spi_ops,
Ovidiu Panait05f38812020-12-14 19:06:48 +0200174 .priv_auto = sizeof(struct sandbox_spi_priv),
Simon Glass95429fe2014-10-13 23:41:57 -0600175};