blob: 4cc016138b11e68d1dab64d23535282e4278e780 [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
Simon Glass95429fe2014-10-13 23:41:57 -060013#include <dm.h>
Simon Glass0f2af882020-05-10 11:40:05 -060014#include <log.h>
Mike Frysinger1d8e57c2013-12-03 16:43:26 -070015#include <malloc.h>
16#include <spi.h>
Simon Glass95429fe2014-10-13 23:41:57 -060017#include <spi_flash.h>
Mike Frysinger1d8e57c2013-12-03 16:43:26 -070018#include <os.h>
19
Masahiro Yamada56a931c2016-09-21 11:28:55 +090020#include <linux/errno.h>
Mike Frysinger1d8e57c2013-12-03 16:43:26 -070021#include <asm/spi.h>
22#include <asm/state.h>
Simon Glass711fd982020-07-07 13:11:49 -060023#include <dm/acpi.h>
Simon Glass95429fe2014-10-13 23:41:57 -060024#include <dm/device-internal.h>
25
Ovidiu Panait05f38812020-12-14 19:06:48 +020026/**
27 * struct sandbox_spi_priv - Sandbox SPI private data
28 *
29 * Helper struct to keep track of the sandbox SPI bus internal state. It is
30 * used in unit tests to verify that dm spi functions update the bus
31 * speed/mode properly (for instance, when jumping back and forth between spi
32 * slaves claiming the bus, we need to make sure that the bus speed is updated
33 * accordingly for each slave).
34 *
35 * @speed: Current bus speed.
36 * @mode: Current bus mode.
37 */
38struct sandbox_spi_priv {
39 uint speed;
40 uint mode;
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
Ovidiu Panaita2c9d012020-12-14 19:06:49 +020050uint sandbox_spi_get_speed(struct udevice *dev)
51{
52 struct sandbox_spi_priv *priv = dev_get_priv(dev);
53
54 return priv->speed;
55}
56
57uint sandbox_spi_get_mode(struct udevice *dev)
58{
59 struct sandbox_spi_priv *priv = dev_get_priv(dev);
60
61 return priv->mode;
62}
63
Simon Glass95429fe2014-10-13 23:41:57 -060064static int sandbox_spi_xfer(struct udevice *slave, unsigned int bitlen,
65 const void *dout, void *din, unsigned long flags)
Mike Frysinger1d8e57c2013-12-03 16:43:26 -070066{
Simon Glass95429fe2014-10-13 23:41:57 -060067 struct udevice *bus = slave->parent;
Mike Frysinger1d8e57c2013-12-03 16:43:26 -070068 struct sandbox_state *state = state_get_current();
Simon Glass95429fe2014-10-13 23:41:57 -060069 struct dm_spi_emul_ops *ops;
70 struct udevice *emul;
Mike Frysinger1d8e57c2013-12-03 16:43:26 -070071 uint bytes = bitlen / 8, i;
Simon Glass95429fe2014-10-13 23:41:57 -060072 int ret;
Simon Glass95429fe2014-10-13 23:41:57 -060073 uint busnum, cs;
Mike Frysinger1d8e57c2013-12-03 16:43:26 -070074
75 if (bitlen == 0)
Simon Glass95429fe2014-10-13 23:41:57 -060076 return 0;
Mike Frysinger1d8e57c2013-12-03 16:43:26 -070077
78 /* we can only do 8 bit transfers */
79 if (bitlen % 8) {
80 printf("sandbox_spi: xfer: invalid bitlen size %u; needs to be 8bit\n",
81 bitlen);
Simon Glass95429fe2014-10-13 23:41:57 -060082 return -EINVAL;
Mike Frysinger1d8e57c2013-12-03 16:43:26 -070083 }
84
Simon Glass75e534b2020-12-16 21:20:07 -070085 busnum = dev_seq(bus);
Simon Glass95429fe2014-10-13 23:41:57 -060086 cs = spi_chip_select(slave);
87 if (busnum >= CONFIG_SANDBOX_SPI_MAX_BUS ||
88 cs >= CONFIG_SANDBOX_SPI_MAX_CS) {
89 printf("%s: busnum=%u, cs=%u: out of range\n", __func__,
90 busnum, cs);
91 return -ENOENT;
92 }
93 ret = sandbox_spi_get_emul(state, bus, slave, &emul);
94 if (ret) {
95 printf("%s: busnum=%u, cs=%u: no emulation available (err=%d)\n",
96 __func__, busnum, cs, ret);
97 return -ENOENT;
98 }
99 ret = device_probe(emul);
100 if (ret)
101 return ret;
Mike Frysinger1d8e57c2013-12-03 16:43:26 -0700102
Simon Glass95429fe2014-10-13 23:41:57 -0600103 ops = spi_emul_get_ops(emul);
104 ret = ops->xfer(emul, bitlen, dout, din, flags);
Mike Frysinger1d8e57c2013-12-03 16:43:26 -0700105
Simon Glass3b4b3402018-10-01 11:55:13 -0600106 log_content("sandbox_spi: xfer: got back %i (that's %s)\n rx:",
107 ret, ret ? "bad" : "good");
108 if (din) {
109 for (i = 0; i < bytes; ++i)
110 log_content(" %u:%02x", i, ((u8 *)din)[i]);
111 }
112 log_content("\n");
Mike Frysinger1d8e57c2013-12-03 16:43:26 -0700113
Mike Frysinger1d8e57c2013-12-03 16:43:26 -0700114 return ret;
115}
Simon Glassfe27ded2014-02-27 13:26:24 -0700116
Simon Glass95429fe2014-10-13 23:41:57 -0600117static int sandbox_spi_set_speed(struct udevice *bus, uint speed)
Simon Glassfe27ded2014-02-27 13:26:24 -0700118{
Ovidiu Panait05f38812020-12-14 19:06:48 +0200119 struct sandbox_spi_priv *priv = dev_get_priv(bus);
120
121 priv->speed = speed;
122
Simon Glass95429fe2014-10-13 23:41:57 -0600123 return 0;
Simon Glassfe27ded2014-02-27 13:26:24 -0700124}
Simon Glass95429fe2014-10-13 23:41:57 -0600125
126static int sandbox_spi_set_mode(struct udevice *bus, uint mode)
127{
Ovidiu Panait05f38812020-12-14 19:06:48 +0200128 struct sandbox_spi_priv *priv = dev_get_priv(bus);
129
130 priv->mode = mode;
131
Simon Glass95429fe2014-10-13 23:41:57 -0600132 return 0;
133}
134
135static int sandbox_cs_info(struct udevice *bus, uint cs,
136 struct spi_cs_info *info)
137{
Ovidiu Panaitae734732020-12-14 19:06:47 +0200138 /* Always allow activity on CS 0, CS 1 */
139 if (cs >= 2)
Bin Mengf8586f62019-09-09 06:00:01 -0700140 return -EINVAL;
Simon Glass95429fe2014-10-13 23:41:57 -0600141
142 return 0;
143}
144
Simon Glass37ad0fe2019-10-20 21:31:47 -0600145static int sandbox_spi_get_mmap(struct udevice *dev, ulong *map_basep,
146 uint *map_sizep, uint *offsetp)
147{
148 *map_basep = 0x1000;
149 *map_sizep = 0x2000;
150 *offsetp = 0x100;
151
152 return 0;
153}
154
Simon Glass95429fe2014-10-13 23:41:57 -0600155static const struct dm_spi_ops sandbox_spi_ops = {
156 .xfer = sandbox_spi_xfer,
157 .set_speed = sandbox_spi_set_speed,
158 .set_mode = sandbox_spi_set_mode,
159 .cs_info = sandbox_cs_info,
Simon Glass37ad0fe2019-10-20 21:31:47 -0600160 .get_mmap = sandbox_spi_get_mmap,
Simon Glass95429fe2014-10-13 23:41:57 -0600161};
162
163static const struct udevice_id sandbox_spi_ids[] = {
164 { .compatible = "sandbox,spi" },
165 { }
166};
167
Walter Lozano2901ac62020-06-25 01:10:04 -0300168U_BOOT_DRIVER(sandbox_spi) = {
169 .name = "sandbox_spi",
Simon Glass95429fe2014-10-13 23:41:57 -0600170 .id = UCLASS_SPI,
171 .of_match = sandbox_spi_ids,
Simon Glass95429fe2014-10-13 23:41:57 -0600172 .ops = &sandbox_spi_ops,
Ovidiu Panait05f38812020-12-14 19:06:48 +0200173 .priv_auto = sizeof(struct sandbox_spi_priv),
Simon Glass95429fe2014-10-13 23:41:57 -0600174};