blob: be516d5aa62df35e9e9edac1454b63fb198d171f [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
Ovidiu Panait05f38812020-12-14 19:06:48 +020031/**
32 * struct sandbox_spi_priv - Sandbox SPI private data
33 *
34 * Helper struct to keep track of the sandbox SPI bus internal state. It is
35 * used in unit tests to verify that dm spi functions update the bus
36 * speed/mode properly (for instance, when jumping back and forth between spi
37 * slaves claiming the bus, we need to make sure that the bus speed is updated
38 * accordingly for each slave).
39 *
40 * @speed: Current bus speed.
41 * @mode: Current bus mode.
42 */
43struct sandbox_spi_priv {
44 uint speed;
45 uint mode;
46};
47
Simon Glass95429fe2014-10-13 23:41:57 -060048__weak int sandbox_spi_get_emul(struct sandbox_state *state,
49 struct udevice *bus, struct udevice *slave,
50 struct udevice **emulp)
Mike Frysinger1d8e57c2013-12-03 16:43:26 -070051{
Simon Glass95429fe2014-10-13 23:41:57 -060052 return -ENOENT;
Mike Frysinger1d8e57c2013-12-03 16:43:26 -070053}
54
Simon Glass95429fe2014-10-13 23:41:57 -060055static int sandbox_spi_xfer(struct udevice *slave, unsigned int bitlen,
56 const void *dout, void *din, unsigned long flags)
Mike Frysinger1d8e57c2013-12-03 16:43:26 -070057{
Simon Glass95429fe2014-10-13 23:41:57 -060058 struct udevice *bus = slave->parent;
Mike Frysinger1d8e57c2013-12-03 16:43:26 -070059 struct sandbox_state *state = state_get_current();
Simon Glass95429fe2014-10-13 23:41:57 -060060 struct dm_spi_emul_ops *ops;
61 struct udevice *emul;
Mike Frysinger1d8e57c2013-12-03 16:43:26 -070062 uint bytes = bitlen / 8, i;
Simon Glass95429fe2014-10-13 23:41:57 -060063 int ret;
Simon Glass95429fe2014-10-13 23:41:57 -060064 uint busnum, cs;
Mike Frysinger1d8e57c2013-12-03 16:43:26 -070065
66 if (bitlen == 0)
Simon Glass95429fe2014-10-13 23:41:57 -060067 return 0;
Mike Frysinger1d8e57c2013-12-03 16:43:26 -070068
69 /* we can only do 8 bit transfers */
70 if (bitlen % 8) {
71 printf("sandbox_spi: xfer: invalid bitlen size %u; needs to be 8bit\n",
72 bitlen);
Simon Glass95429fe2014-10-13 23:41:57 -060073 return -EINVAL;
Mike Frysinger1d8e57c2013-12-03 16:43:26 -070074 }
75
Simon Glass75e534b2020-12-16 21:20:07 -070076 busnum = dev_seq(bus);
Simon Glass95429fe2014-10-13 23:41:57 -060077 cs = spi_chip_select(slave);
78 if (busnum >= CONFIG_SANDBOX_SPI_MAX_BUS ||
79 cs >= CONFIG_SANDBOX_SPI_MAX_CS) {
80 printf("%s: busnum=%u, cs=%u: out of range\n", __func__,
81 busnum, cs);
82 return -ENOENT;
83 }
84 ret = sandbox_spi_get_emul(state, bus, slave, &emul);
85 if (ret) {
86 printf("%s: busnum=%u, cs=%u: no emulation available (err=%d)\n",
87 __func__, busnum, cs, ret);
88 return -ENOENT;
89 }
90 ret = device_probe(emul);
91 if (ret)
92 return ret;
Mike Frysinger1d8e57c2013-12-03 16:43:26 -070093
Simon Glass95429fe2014-10-13 23:41:57 -060094 ops = spi_emul_get_ops(emul);
95 ret = ops->xfer(emul, bitlen, dout, din, flags);
Mike Frysinger1d8e57c2013-12-03 16:43:26 -070096
Simon Glass3b4b3402018-10-01 11:55:13 -060097 log_content("sandbox_spi: xfer: got back %i (that's %s)\n rx:",
98 ret, ret ? "bad" : "good");
99 if (din) {
100 for (i = 0; i < bytes; ++i)
101 log_content(" %u:%02x", i, ((u8 *)din)[i]);
102 }
103 log_content("\n");
Mike Frysinger1d8e57c2013-12-03 16:43:26 -0700104
Mike Frysinger1d8e57c2013-12-03 16:43:26 -0700105 return ret;
106}
Simon Glassfe27ded2014-02-27 13:26:24 -0700107
Simon Glass95429fe2014-10-13 23:41:57 -0600108static int sandbox_spi_set_speed(struct udevice *bus, uint speed)
Simon Glassfe27ded2014-02-27 13:26:24 -0700109{
Ovidiu Panait05f38812020-12-14 19:06:48 +0200110 struct sandbox_spi_priv *priv = dev_get_priv(bus);
111
112 priv->speed = speed;
113
Simon Glass95429fe2014-10-13 23:41:57 -0600114 return 0;
Simon Glassfe27ded2014-02-27 13:26:24 -0700115}
Simon Glass95429fe2014-10-13 23:41:57 -0600116
117static int sandbox_spi_set_mode(struct udevice *bus, uint mode)
118{
Ovidiu Panait05f38812020-12-14 19:06:48 +0200119 struct sandbox_spi_priv *priv = dev_get_priv(bus);
120
121 priv->mode = mode;
122
Simon Glass95429fe2014-10-13 23:41:57 -0600123 return 0;
124}
125
126static int sandbox_cs_info(struct udevice *bus, uint cs,
127 struct spi_cs_info *info)
128{
Ovidiu Panaitae734732020-12-14 19:06:47 +0200129 /* Always allow activity on CS 0, CS 1 */
130 if (cs >= 2)
Bin Mengf8586f62019-09-09 06:00:01 -0700131 return -EINVAL;
Simon Glass95429fe2014-10-13 23:41:57 -0600132
133 return 0;
134}
135
Simon Glass37ad0fe2019-10-20 21:31:47 -0600136static int sandbox_spi_get_mmap(struct udevice *dev, ulong *map_basep,
137 uint *map_sizep, uint *offsetp)
138{
139 *map_basep = 0x1000;
140 *map_sizep = 0x2000;
141 *offsetp = 0x100;
142
143 return 0;
144}
145
Simon Glass95429fe2014-10-13 23:41:57 -0600146static const struct dm_spi_ops sandbox_spi_ops = {
147 .xfer = sandbox_spi_xfer,
148 .set_speed = sandbox_spi_set_speed,
149 .set_mode = sandbox_spi_set_mode,
150 .cs_info = sandbox_cs_info,
Simon Glass37ad0fe2019-10-20 21:31:47 -0600151 .get_mmap = sandbox_spi_get_mmap,
Simon Glass95429fe2014-10-13 23:41:57 -0600152};
153
154static const struct udevice_id sandbox_spi_ids[] = {
155 { .compatible = "sandbox,spi" },
156 { }
157};
158
Walter Lozano2901ac62020-06-25 01:10:04 -0300159U_BOOT_DRIVER(sandbox_spi) = {
160 .name = "sandbox_spi",
Simon Glass95429fe2014-10-13 23:41:57 -0600161 .id = UCLASS_SPI,
162 .of_match = sandbox_spi_ids,
Simon Glass95429fe2014-10-13 23:41:57 -0600163 .ops = &sandbox_spi_ops,
Ovidiu Panait05f38812020-12-14 19:06:48 +0200164 .priv_auto = sizeof(struct sandbox_spi_priv),
Simon Glass95429fe2014-10-13 23:41:57 -0600165};