Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | dd82d44 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2014 Google, Inc |
Simon Glass | dd82d44 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 4 | */ |
| 5 | |
Patrick Delaunay | 1d83e03 | 2020-10-15 17:18:17 +0200 | [diff] [blame] | 6 | #define LOG_CATEGORY UCLASS_SPI |
| 7 | |
Simon Glass | dd82d44 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 8 | #include <dm.h> |
| 9 | #include <errno.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 10 | #include <log.h> |
Simon Glass | dd82d44 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 11 | #include <malloc.h> |
| 12 | #include <spi.h> |
T Karthik Reddy | 626a734 | 2021-03-17 12:31:30 +0100 | [diff] [blame] | 13 | #include <spi-mem.h> |
Patrick Delaunay | 1d83e03 | 2020-10-15 17:18:17 +0200 | [diff] [blame] | 14 | #include <dm/device_compat.h> |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 15 | #include <asm/global_data.h> |
Simon Glass | dd82d44 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 16 | #include <dm/device-internal.h> |
| 17 | #include <dm/uclass-internal.h> |
Simon Glass | dd82d44 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 18 | #include <dm/lists.h> |
| 19 | #include <dm/util.h> |
| 20 | |
| 21 | DECLARE_GLOBAL_DATA_PTR; |
| 22 | |
Simon Goldschmidt | a942e1a | 2018-10-30 21:09:48 +0100 | [diff] [blame] | 23 | #define SPI_DEFAULT_SPEED_HZ 100000 |
| 24 | |
Simon Glass | dd82d44 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 25 | static int spi_set_speed_mode(struct udevice *bus, int speed, int mode) |
| 26 | { |
| 27 | struct dm_spi_ops *ops; |
| 28 | int ret; |
| 29 | |
| 30 | ops = spi_get_ops(bus); |
| 31 | if (ops->set_speed) |
| 32 | ret = ops->set_speed(bus, speed); |
| 33 | else |
| 34 | ret = -EINVAL; |
| 35 | if (ret) { |
Patrick Delaunay | 1d83e03 | 2020-10-15 17:18:17 +0200 | [diff] [blame] | 36 | dev_err(bus, "Cannot set speed (err=%d)\n", ret); |
Simon Glass | dd82d44 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 37 | return ret; |
| 38 | } |
| 39 | |
| 40 | if (ops->set_mode) |
| 41 | ret = ops->set_mode(bus, mode); |
| 42 | else |
| 43 | ret = -EINVAL; |
| 44 | if (ret) { |
Patrick Delaunay | 1d83e03 | 2020-10-15 17:18:17 +0200 | [diff] [blame] | 45 | dev_err(bus, "Cannot set mode (err=%d)\n", ret); |
Simon Glass | dd82d44 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 46 | return ret; |
| 47 | } |
| 48 | |
| 49 | return 0; |
| 50 | } |
| 51 | |
Peng Fan | fdd88a3 | 2016-05-03 10:02:22 +0800 | [diff] [blame] | 52 | int dm_spi_claim_bus(struct udevice *dev) |
Simon Glass | dd82d44 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 53 | { |
Simon Glass | dd82d44 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 54 | struct udevice *bus = dev->parent; |
| 55 | struct dm_spi_ops *ops = spi_get_ops(bus); |
Simon Glass | de0977b | 2015-03-05 12:25:20 -0700 | [diff] [blame] | 56 | struct dm_spi_bus *spi = dev_get_uclass_priv(bus); |
Peng Fan | fdd88a3 | 2016-05-03 10:02:22 +0800 | [diff] [blame] | 57 | struct spi_slave *slave = dev_get_parent_priv(dev); |
Ovidiu Panait | 40dcee1 | 2020-12-14 19:06:50 +0200 | [diff] [blame] | 58 | uint speed, mode; |
Simon Glass | dd82d44 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 59 | |
| 60 | speed = slave->max_hz; |
Ovidiu Panait | 40dcee1 | 2020-12-14 19:06:50 +0200 | [diff] [blame] | 61 | mode = slave->mode; |
| 62 | |
Simon Glass | dd82d44 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 63 | if (spi->max_hz) { |
| 64 | if (speed) |
Ovidiu Panait | 40dcee1 | 2020-12-14 19:06:50 +0200 | [diff] [blame] | 65 | speed = min(speed, spi->max_hz); |
Simon Glass | dd82d44 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 66 | else |
| 67 | speed = spi->max_hz; |
| 68 | } |
| 69 | if (!speed) |
Simon Goldschmidt | a942e1a | 2018-10-30 21:09:48 +0100 | [diff] [blame] | 70 | speed = SPI_DEFAULT_SPEED_HZ; |
Ovidiu Panait | 40dcee1 | 2020-12-14 19:06:50 +0200 | [diff] [blame] | 71 | |
| 72 | if (speed != spi->speed || mode != spi->mode) { |
Mario Six | 2d844dd | 2018-01-15 11:08:41 +0100 | [diff] [blame] | 73 | int ret = spi_set_speed_mode(bus, speed, slave->mode); |
| 74 | |
Simon Glass | b46cb63 | 2015-02-17 15:29:35 -0700 | [diff] [blame] | 75 | if (ret) |
Simon Glass | 00d9990 | 2018-10-01 12:22:24 -0600 | [diff] [blame] | 76 | return log_ret(ret); |
Ovidiu Panait | 40dcee1 | 2020-12-14 19:06:50 +0200 | [diff] [blame] | 77 | |
| 78 | spi->speed = speed; |
| 79 | spi->mode = mode; |
Simon Glass | b46cb63 | 2015-02-17 15:29:35 -0700 | [diff] [blame] | 80 | } |
Simon Glass | dd82d44 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 81 | |
Simon Glass | 00d9990 | 2018-10-01 12:22:24 -0600 | [diff] [blame] | 82 | return log_ret(ops->claim_bus ? ops->claim_bus(dev) : 0); |
Simon Glass | dd82d44 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 83 | } |
| 84 | |
Peng Fan | fdd88a3 | 2016-05-03 10:02:22 +0800 | [diff] [blame] | 85 | void dm_spi_release_bus(struct udevice *dev) |
Simon Glass | dd82d44 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 86 | { |
Simon Glass | dd82d44 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 87 | struct udevice *bus = dev->parent; |
| 88 | struct dm_spi_ops *ops = spi_get_ops(bus); |
| 89 | |
| 90 | if (ops->release_bus) |
Simon Glass | 5c74fba | 2015-04-19 09:05:40 -0600 | [diff] [blame] | 91 | ops->release_bus(dev); |
Simon Glass | dd82d44 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 92 | } |
| 93 | |
Peng Fan | fdd88a3 | 2016-05-03 10:02:22 +0800 | [diff] [blame] | 94 | int dm_spi_xfer(struct udevice *dev, unsigned int bitlen, |
| 95 | const void *dout, void *din, unsigned long flags) |
Simon Glass | dd82d44 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 96 | { |
Simon Glass | dd82d44 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 97 | struct udevice *bus = dev->parent; |
Simon Glass | 2d2e860 | 2019-12-06 21:42:35 -0700 | [diff] [blame] | 98 | struct dm_spi_ops *ops = spi_get_ops(bus); |
Simon Glass | dd82d44 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 99 | |
| 100 | if (bus->uclass->uc_drv->id != UCLASS_SPI) |
| 101 | return -EOPNOTSUPP; |
Simon Glass | 2d2e860 | 2019-12-06 21:42:35 -0700 | [diff] [blame] | 102 | if (!ops->xfer) |
| 103 | return -ENOSYS; |
Simon Glass | dd82d44 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 104 | |
Simon Glass | 2d2e860 | 2019-12-06 21:42:35 -0700 | [diff] [blame] | 105 | return ops->xfer(dev, bitlen, dout, din, flags); |
Simon Glass | dd82d44 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 106 | } |
| 107 | |
Simon Glass | 37ad0fe | 2019-10-20 21:31:47 -0600 | [diff] [blame] | 108 | int dm_spi_get_mmap(struct udevice *dev, ulong *map_basep, uint *map_sizep, |
| 109 | uint *offsetp) |
| 110 | { |
| 111 | struct udevice *bus = dev->parent; |
| 112 | struct dm_spi_ops *ops = spi_get_ops(bus); |
| 113 | |
| 114 | if (bus->uclass->uc_drv->id != UCLASS_SPI) |
| 115 | return -EOPNOTSUPP; |
| 116 | if (!ops->get_mmap) |
| 117 | return -ENOSYS; |
| 118 | |
| 119 | return ops->get_mmap(dev, map_basep, map_sizep, offsetp); |
| 120 | } |
| 121 | |
Peng Fan | fdd88a3 | 2016-05-03 10:02:22 +0800 | [diff] [blame] | 122 | int spi_claim_bus(struct spi_slave *slave) |
| 123 | { |
Simon Glass | 00d9990 | 2018-10-01 12:22:24 -0600 | [diff] [blame] | 124 | return log_ret(dm_spi_claim_bus(slave->dev)); |
Peng Fan | fdd88a3 | 2016-05-03 10:02:22 +0800 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | void spi_release_bus(struct spi_slave *slave) |
| 128 | { |
| 129 | dm_spi_release_bus(slave->dev); |
| 130 | } |
| 131 | |
Paul Barker | dbb689f | 2022-10-05 13:18:34 +0100 | [diff] [blame] | 132 | int spi_set_speed(struct spi_slave *slave, uint hz) |
| 133 | { |
| 134 | struct dm_spi_ops *ops; |
| 135 | int ret; |
| 136 | |
| 137 | ops = spi_get_ops(slave->dev->parent); |
| 138 | if (ops->set_speed) |
| 139 | ret = ops->set_speed(slave->dev->parent, hz); |
| 140 | else |
| 141 | ret = -EINVAL; |
| 142 | if (ret) |
| 143 | dev_err(slave->dev, "Cannot set speed (err=%d)\n", ret); |
| 144 | return ret; |
| 145 | } |
| 146 | |
Peng Fan | fdd88a3 | 2016-05-03 10:02:22 +0800 | [diff] [blame] | 147 | int spi_xfer(struct spi_slave *slave, unsigned int bitlen, |
| 148 | const void *dout, void *din, unsigned long flags) |
| 149 | { |
| 150 | return dm_spi_xfer(slave->dev, bitlen, dout, din, flags); |
| 151 | } |
| 152 | |
Jagan Teki | 7cc71fd | 2019-07-22 17:22:56 +0530 | [diff] [blame] | 153 | int spi_write_then_read(struct spi_slave *slave, const u8 *opcode, |
| 154 | size_t n_opcode, const u8 *txbuf, u8 *rxbuf, |
| 155 | size_t n_buf) |
| 156 | { |
| 157 | unsigned long flags = SPI_XFER_BEGIN; |
| 158 | int ret; |
| 159 | |
| 160 | if (n_buf == 0) |
| 161 | flags |= SPI_XFER_END; |
| 162 | |
| 163 | ret = spi_xfer(slave, n_opcode * 8, opcode, NULL, flags); |
| 164 | if (ret) { |
Patrick Delaunay | 1d83e03 | 2020-10-15 17:18:17 +0200 | [diff] [blame] | 165 | dev_dbg(slave->dev, |
| 166 | "spi: failed to send command (%zu bytes): %d\n", |
| 167 | n_opcode, ret); |
Jagan Teki | 7cc71fd | 2019-07-22 17:22:56 +0530 | [diff] [blame] | 168 | } else if (n_buf != 0) { |
| 169 | ret = spi_xfer(slave, n_buf * 8, txbuf, rxbuf, SPI_XFER_END); |
| 170 | if (ret) |
Patrick Delaunay | 1d83e03 | 2020-10-15 17:18:17 +0200 | [diff] [blame] | 171 | dev_dbg(slave->dev, |
| 172 | "spi: failed to transfer %zu bytes of data: %d\n", |
| 173 | n_buf, ret); |
Jagan Teki | 7cc71fd | 2019-07-22 17:22:56 +0530 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | return ret; |
| 177 | } |
| 178 | |
Simon Glass | 9288265 | 2021-08-07 07:24:04 -0600 | [diff] [blame] | 179 | #if CONFIG_IS_ENABLED(OF_REAL) |
Simon Glass | d941aaf | 2015-06-23 15:39:05 -0600 | [diff] [blame] | 180 | static int spi_child_post_bind(struct udevice *dev) |
Simon Glass | dd82d44 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 181 | { |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 182 | struct dm_spi_slave_plat *plat = dev_get_parent_plat(dev); |
Simon Glass | dd82d44 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 183 | |
Simon Glass | f1d50f7 | 2020-12-19 10:40:13 -0700 | [diff] [blame] | 184 | if (!dev_has_ofnode(dev)) |
Simon Glass | 5d2ee05 | 2015-01-25 08:27:12 -0700 | [diff] [blame] | 185 | return 0; |
| 186 | |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 187 | return spi_slave_of_to_plat(dev, plat); |
Simon Glass | 5d2ee05 | 2015-01-25 08:27:12 -0700 | [diff] [blame] | 188 | } |
Simon Glass | 3fb3339 | 2016-11-13 14:22:01 -0700 | [diff] [blame] | 189 | #endif |
Simon Glass | 5d2ee05 | 2015-01-25 08:27:12 -0700 | [diff] [blame] | 190 | |
Simon Glass | d941aaf | 2015-06-23 15:39:05 -0600 | [diff] [blame] | 191 | static int spi_post_probe(struct udevice *bus) |
Simon Glass | 5d2ee05 | 2015-01-25 08:27:12 -0700 | [diff] [blame] | 192 | { |
Simon Glass | 6d70ba0 | 2021-08-07 07:24:06 -0600 | [diff] [blame] | 193 | if (CONFIG_IS_ENABLED(OF_REAL)) { |
| 194 | struct dm_spi_bus *spi = dev_get_uclass_priv(bus); |
Simon Glass | 5d2ee05 | 2015-01-25 08:27:12 -0700 | [diff] [blame] | 195 | |
Simon Glass | 6d70ba0 | 2021-08-07 07:24:06 -0600 | [diff] [blame] | 196 | spi->max_hz = dev_read_u32_default(bus, "spi-max-frequency", 0); |
| 197 | } |
Michal Simek | 28d7e4e | 2015-10-27 13:36:42 +0100 | [diff] [blame] | 198 | |
Simon Glass | dd82d44 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 199 | return 0; |
| 200 | } |
| 201 | |
Simon Glass | d941aaf | 2015-06-23 15:39:05 -0600 | [diff] [blame] | 202 | static int spi_child_pre_probe(struct udevice *dev) |
Simon Glass | 82c2f50 | 2015-01-25 08:27:11 -0700 | [diff] [blame] | 203 | { |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 204 | struct dm_spi_slave_plat *plat = dev_get_parent_plat(dev); |
Simon Glass | de44acf | 2015-09-28 23:32:01 -0600 | [diff] [blame] | 205 | struct spi_slave *slave = dev_get_parent_priv(dev); |
Simon Glass | 82c2f50 | 2015-01-25 08:27:11 -0700 | [diff] [blame] | 206 | |
Simon Glass | 5d2ee05 | 2015-01-25 08:27:12 -0700 | [diff] [blame] | 207 | /* |
| 208 | * This is needed because we pass struct spi_slave around the place |
| 209 | * instead slave->dev (a struct udevice). So we have to have some |
| 210 | * way to access the slave udevice given struct spi_slave. Once we |
| 211 | * change the SPI API to use udevice instead of spi_slave, we can |
| 212 | * drop this. |
| 213 | */ |
Simon Glass | 82c2f50 | 2015-01-25 08:27:11 -0700 | [diff] [blame] | 214 | slave->dev = dev; |
| 215 | |
Simon Glass | 5d2ee05 | 2015-01-25 08:27:12 -0700 | [diff] [blame] | 216 | slave->max_hz = plat->max_hz; |
| 217 | slave->mode = plat->mode; |
Christophe Ricard | fb0c53e | 2016-01-17 11:56:48 +0100 | [diff] [blame] | 218 | slave->wordlen = SPI_DEFAULT_WORDLEN; |
Simon Glass | 5d2ee05 | 2015-01-25 08:27:12 -0700 | [diff] [blame] | 219 | |
Simon Glass | 82c2f50 | 2015-01-25 08:27:11 -0700 | [diff] [blame] | 220 | return 0; |
| 221 | } |
| 222 | |
Simon Glass | dd82d44 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 223 | int spi_chip_select(struct udevice *dev) |
| 224 | { |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 225 | struct dm_spi_slave_plat *plat = dev_get_parent_plat(dev); |
Simon Glass | dd82d44 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 226 | |
Simon Glass | 5d2ee05 | 2015-01-25 08:27:12 -0700 | [diff] [blame] | 227 | return plat ? plat->cs : -ENOENT; |
Simon Glass | dd82d44 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 228 | } |
| 229 | |
Simon Glass | 5ef36f2 | 2014-11-11 10:46:22 -0700 | [diff] [blame] | 230 | int spi_find_chip_select(struct udevice *bus, int cs, struct udevice **devp) |
Simon Glass | dd82d44 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 231 | { |
Bin Meng | 9741e73 | 2019-09-09 06:00:02 -0700 | [diff] [blame] | 232 | struct dm_spi_ops *ops; |
| 233 | struct spi_cs_info info; |
Simon Glass | dd82d44 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 234 | struct udevice *dev; |
Bin Meng | 9741e73 | 2019-09-09 06:00:02 -0700 | [diff] [blame] | 235 | int ret; |
| 236 | |
| 237 | /* |
| 238 | * Ask the driver. For the moment we don't have CS info. |
| 239 | * When we do we could provide the driver with a helper function |
| 240 | * to figure out what chip selects are valid, or just handle the |
| 241 | * request. |
| 242 | */ |
| 243 | ops = spi_get_ops(bus); |
| 244 | if (ops->cs_info) { |
| 245 | ret = ops->cs_info(bus, cs, &info); |
| 246 | } else { |
| 247 | /* |
| 248 | * We could assume there is at least one valid chip select. |
| 249 | * The driver didn't care enough to tell us. |
| 250 | */ |
| 251 | ret = 0; |
| 252 | } |
| 253 | |
| 254 | if (ret) { |
Patrick Delaunay | 1d83e03 | 2020-10-15 17:18:17 +0200 | [diff] [blame] | 255 | dev_err(bus, "Invalid cs %d (err=%d)\n", cs, ret); |
Bin Meng | 9741e73 | 2019-09-09 06:00:02 -0700 | [diff] [blame] | 256 | return ret; |
| 257 | } |
Simon Glass | dd82d44 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 258 | |
| 259 | for (device_find_first_child(bus, &dev); dev; |
| 260 | device_find_next_child(&dev)) { |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 261 | struct dm_spi_slave_plat *plat; |
Simon Glass | dd82d44 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 262 | |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 263 | plat = dev_get_parent_plat(dev); |
Patrick Delaunay | 1d83e03 | 2020-10-15 17:18:17 +0200 | [diff] [blame] | 264 | dev_dbg(bus, "%s: plat=%p, cs=%d\n", __func__, plat, plat->cs); |
Simon Glass | 5d2ee05 | 2015-01-25 08:27:12 -0700 | [diff] [blame] | 265 | if (plat->cs == cs) { |
Simon Glass | dd82d44 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 266 | *devp = dev; |
| 267 | return 0; |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | return -ENODEV; |
| 272 | } |
| 273 | |
| 274 | int spi_cs_is_valid(unsigned int busnum, unsigned int cs) |
| 275 | { |
| 276 | struct spi_cs_info info; |
| 277 | struct udevice *bus; |
| 278 | int ret; |
| 279 | |
Simon Glass | 07e1338 | 2020-12-16 21:20:29 -0700 | [diff] [blame] | 280 | ret = uclass_find_device_by_seq(UCLASS_SPI, busnum, &bus); |
Simon Glass | dd82d44 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 281 | if (ret) { |
Patrick Delaunay | 1d83e03 | 2020-10-15 17:18:17 +0200 | [diff] [blame] | 282 | log_debug("%s: No bus %d\n", __func__, busnum); |
Simon Glass | dd82d44 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 283 | return ret; |
| 284 | } |
| 285 | |
| 286 | return spi_cs_info(bus, cs, &info); |
| 287 | } |
| 288 | |
| 289 | int spi_cs_info(struct udevice *bus, uint cs, struct spi_cs_info *info) |
| 290 | { |
| 291 | struct spi_cs_info local_info; |
Simon Glass | dd82d44 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 292 | int ret; |
| 293 | |
| 294 | if (!info) |
| 295 | info = &local_info; |
| 296 | |
| 297 | /* If there is a device attached, return it */ |
| 298 | info->dev = NULL; |
| 299 | ret = spi_find_chip_select(bus, cs, &info->dev); |
Bin Meng | 9741e73 | 2019-09-09 06:00:02 -0700 | [diff] [blame] | 300 | return ret == -ENODEV ? 0 : ret; |
Simon Glass | dd82d44 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 301 | } |
| 302 | |
Simon Glass | dd82d44 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 303 | int spi_find_bus_and_cs(int busnum, int cs, struct udevice **busp, |
| 304 | struct udevice **devp) |
| 305 | { |
| 306 | struct udevice *bus, *dev; |
| 307 | int ret; |
| 308 | |
Simon Glass | 07e1338 | 2020-12-16 21:20:29 -0700 | [diff] [blame] | 309 | ret = uclass_find_device_by_seq(UCLASS_SPI, busnum, &bus); |
Simon Glass | dd82d44 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 310 | if (ret) { |
Patrick Delaunay | 1d83e03 | 2020-10-15 17:18:17 +0200 | [diff] [blame] | 311 | log_debug("%s: No bus %d\n", __func__, busnum); |
Simon Glass | dd82d44 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 312 | return ret; |
| 313 | } |
| 314 | ret = spi_find_chip_select(bus, cs, &dev); |
| 315 | if (ret) { |
Patrick Delaunay | 1d83e03 | 2020-10-15 17:18:17 +0200 | [diff] [blame] | 316 | dev_dbg(bus, "%s: No cs %d\n", __func__, cs); |
Simon Glass | dd82d44 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 317 | return ret; |
| 318 | } |
| 319 | *busp = bus; |
| 320 | *devp = dev; |
| 321 | |
| 322 | return ret; |
| 323 | } |
| 324 | |
Patrice Chotard | 86e06ae | 2022-03-30 09:33:13 +0200 | [diff] [blame] | 325 | int spi_get_bus_and_cs(int busnum, int cs, struct udevice **busp, |
| 326 | struct spi_slave **devp) |
Simon Glass | dd82d44 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 327 | { |
| 328 | struct udevice *bus, *dev; |
Patrice Chotard | 86e06ae | 2022-03-30 09:33:13 +0200 | [diff] [blame] | 329 | struct dm_spi_bus *bus_data; |
| 330 | struct spi_slave *slave; |
| 331 | int ret; |
| 332 | |
| 333 | #if CONFIG_IS_ENABLED(OF_PLATDATA) |
| 334 | ret = uclass_first_device_err(UCLASS_SPI, &bus); |
| 335 | #else |
| 336 | ret = uclass_get_device_by_seq(UCLASS_SPI, busnum, &bus); |
| 337 | #endif |
| 338 | if (ret) { |
| 339 | log_err("Invalid bus %d (err=%d)\n", busnum, ret); |
| 340 | return ret; |
| 341 | } |
| 342 | ret = spi_find_chip_select(bus, cs, &dev); |
| 343 | if (ret) { |
| 344 | dev_err(bus, "Invalid chip select %d:%d (err=%d)\n", busnum, cs, ret); |
| 345 | return ret; |
| 346 | } |
| 347 | |
| 348 | if (!device_active(dev)) { |
| 349 | struct spi_slave *slave; |
| 350 | |
| 351 | ret = device_probe(dev); |
| 352 | if (ret) |
| 353 | goto err; |
| 354 | slave = dev_get_parent_priv(dev); |
| 355 | slave->dev = dev; |
| 356 | } |
| 357 | |
| 358 | slave = dev_get_parent_priv(dev); |
| 359 | bus_data = dev_get_uclass_priv(bus); |
| 360 | |
| 361 | /* |
| 362 | * In case the operation speed is not yet established by |
| 363 | * dm_spi_claim_bus() ensure the bus is configured properly. |
| 364 | */ |
| 365 | if (!bus_data->speed) { |
| 366 | ret = spi_claim_bus(slave); |
| 367 | if (ret) |
| 368 | goto err; |
| 369 | } |
| 370 | *busp = bus; |
| 371 | *devp = slave; |
| 372 | |
| 373 | return 0; |
| 374 | |
| 375 | err: |
| 376 | log_debug("%s: Error path, device '%s'\n", __func__, dev->name); |
| 377 | |
| 378 | return ret; |
| 379 | } |
| 380 | |
| 381 | int _spi_get_bus_and_cs(int busnum, int cs, int speed, int mode, |
| 382 | const char *drv_name, const char *dev_name, |
| 383 | struct udevice **busp, struct spi_slave **devp) |
| 384 | { |
| 385 | struct udevice *bus, *dev; |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 386 | struct dm_spi_slave_plat *plat; |
Ovidiu Panait | 40dcee1 | 2020-12-14 19:06:50 +0200 | [diff] [blame] | 387 | struct dm_spi_bus *bus_data; |
Marcin Wojtas | 70d8f98 | 2019-11-21 05:38:47 +0100 | [diff] [blame] | 388 | struct spi_slave *slave; |
Simon Glass | dd82d44 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 389 | bool created = false; |
| 390 | int ret; |
| 391 | |
Thomas Fitzsimmons | 59c90f3 | 2019-09-06 07:51:19 -0400 | [diff] [blame] | 392 | #if CONFIG_IS_ENABLED(OF_PLATDATA) |
Simon Glass | 3fb3339 | 2016-11-13 14:22:01 -0700 | [diff] [blame] | 393 | ret = uclass_first_device_err(UCLASS_SPI, &bus); |
| 394 | #else |
Simon Glass | dd82d44 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 395 | ret = uclass_get_device_by_seq(UCLASS_SPI, busnum, &bus); |
Simon Glass | 3fb3339 | 2016-11-13 14:22:01 -0700 | [diff] [blame] | 396 | #endif |
Simon Glass | dd82d44 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 397 | if (ret) { |
Patrick Delaunay | 1d83e03 | 2020-10-15 17:18:17 +0200 | [diff] [blame] | 398 | log_err("Invalid bus %d (err=%d)\n", busnum, ret); |
Simon Glass | dd82d44 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 399 | return ret; |
| 400 | } |
| 401 | ret = spi_find_chip_select(bus, cs, &dev); |
| 402 | |
| 403 | /* |
| 404 | * If there is no such device, create one automatically. This means |
| 405 | * that we don't need a device tree node or platform data for the |
| 406 | * SPI flash chip - we will bind to the correct driver. |
| 407 | */ |
| 408 | if (ret == -ENODEV && drv_name) { |
Patrick Delaunay | 1d83e03 | 2020-10-15 17:18:17 +0200 | [diff] [blame] | 409 | dev_dbg(bus, "%s: Binding new device '%s', busnum=%d, cs=%d, driver=%s\n", |
| 410 | __func__, dev_name, busnum, cs, drv_name); |
Simon Glass | d8a21f6 | 2014-11-11 10:46:23 -0700 | [diff] [blame] | 411 | ret = device_bind_driver(bus, drv_name, dev_name, &dev); |
Simon Glass | f0307e1 | 2016-11-13 14:22:05 -0700 | [diff] [blame] | 412 | if (ret) { |
Patrick Delaunay | 1d83e03 | 2020-10-15 17:18:17 +0200 | [diff] [blame] | 413 | dev_dbg(bus, "%s: Unable to bind driver (ret=%d)\n", |
| 414 | __func__, ret); |
Simon Glass | dd82d44 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 415 | return ret; |
Simon Glass | f0307e1 | 2016-11-13 14:22:05 -0700 | [diff] [blame] | 416 | } |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 417 | plat = dev_get_parent_plat(dev); |
Simon Glass | 5d2ee05 | 2015-01-25 08:27:12 -0700 | [diff] [blame] | 418 | plat->cs = cs; |
Simon Goldschmidt | a942e1a | 2018-10-30 21:09:48 +0100 | [diff] [blame] | 419 | if (speed) { |
| 420 | plat->max_hz = speed; |
| 421 | } else { |
Patrick Delaunay | 1d83e03 | 2020-10-15 17:18:17 +0200 | [diff] [blame] | 422 | dev_warn(bus, |
| 423 | "Warning: SPI speed fallback to %u kHz\n", |
| 424 | SPI_DEFAULT_SPEED_HZ / 1000); |
Simon Goldschmidt | a942e1a | 2018-10-30 21:09:48 +0100 | [diff] [blame] | 425 | plat->max_hz = SPI_DEFAULT_SPEED_HZ; |
| 426 | } |
Simon Glass | 5d2ee05 | 2015-01-25 08:27:12 -0700 | [diff] [blame] | 427 | plat->mode = mode; |
Simon Glass | dd82d44 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 428 | created = true; |
| 429 | } else if (ret) { |
Patrick Delaunay | 1d83e03 | 2020-10-15 17:18:17 +0200 | [diff] [blame] | 430 | dev_err(bus, "Invalid chip select %d:%d (err=%d)\n", busnum, cs, ret); |
Simon Glass | dd82d44 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 431 | return ret; |
Marek Vasut | 170013f | 2021-06-10 14:00:00 +0200 | [diff] [blame] | 432 | } else if (dev) { |
| 433 | plat = dev_get_parent_plat(dev); |
Simon Glass | dd82d44 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 434 | } |
| 435 | |
| 436 | if (!device_active(dev)) { |
Simon Glass | 5d2ee05 | 2015-01-25 08:27:12 -0700 | [diff] [blame] | 437 | struct spi_slave *slave; |
Simon Glass | dd82d44 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 438 | |
Simon Glass | 5d2ee05 | 2015-01-25 08:27:12 -0700 | [diff] [blame] | 439 | ret = device_probe(dev); |
Simon Glass | dd82d44 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 440 | if (ret) |
| 441 | goto err; |
Simon Glass | de44acf | 2015-09-28 23:32:01 -0600 | [diff] [blame] | 442 | slave = dev_get_parent_priv(dev); |
Simon Glass | dd82d44 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 443 | slave->dev = dev; |
Simon Glass | dd82d44 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 444 | } |
| 445 | |
Marcin Wojtas | 70d8f98 | 2019-11-21 05:38:47 +0100 | [diff] [blame] | 446 | slave = dev_get_parent_priv(dev); |
Ovidiu Panait | 40dcee1 | 2020-12-14 19:06:50 +0200 | [diff] [blame] | 447 | bus_data = dev_get_uclass_priv(bus); |
Patrick Delaunay | fa19c65 | 2019-02-27 15:36:44 +0100 | [diff] [blame] | 448 | |
Marcin Wojtas | 70d8f98 | 2019-11-21 05:38:47 +0100 | [diff] [blame] | 449 | /* |
| 450 | * In case the operation speed is not yet established by |
| 451 | * dm_spi_claim_bus() ensure the bus is configured properly. |
| 452 | */ |
Ovidiu Panait | 40dcee1 | 2020-12-14 19:06:50 +0200 | [diff] [blame] | 453 | if (!bus_data->speed) { |
Marcin Wojtas | 70d8f98 | 2019-11-21 05:38:47 +0100 | [diff] [blame] | 454 | ret = spi_claim_bus(slave); |
| 455 | if (ret) |
| 456 | goto err; |
Vignesh R | ae56979 | 2016-07-06 10:04:28 +0530 | [diff] [blame] | 457 | } |
Simon Glass | dd82d44 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 458 | |
Marek Vasut | 170013f | 2021-06-10 14:00:00 +0200 | [diff] [blame] | 459 | /* In case bus frequency or mode changed, update it. */ |
| 460 | if ((speed && bus_data->speed && bus_data->speed != speed) || |
| 461 | (plat && plat->mode != mode)) { |
| 462 | ret = spi_set_speed_mode(bus, speed, mode); |
| 463 | if (ret) |
| 464 | goto err_speed_mode; |
| 465 | } |
| 466 | |
Simon Glass | dd82d44 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 467 | *busp = bus; |
Marcin Wojtas | 70d8f98 | 2019-11-21 05:38:47 +0100 | [diff] [blame] | 468 | *devp = slave; |
Patrick Delaunay | 1d83e03 | 2020-10-15 17:18:17 +0200 | [diff] [blame] | 469 | log_debug("%s: bus=%p, slave=%p\n", __func__, bus, *devp); |
Simon Glass | dd82d44 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 470 | |
| 471 | return 0; |
| 472 | |
Marek Vasut | 170013f | 2021-06-10 14:00:00 +0200 | [diff] [blame] | 473 | err_speed_mode: |
| 474 | spi_release_bus(slave); |
Simon Glass | dd82d44 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 475 | err: |
Patrick Delaunay | 1d83e03 | 2020-10-15 17:18:17 +0200 | [diff] [blame] | 476 | log_debug("%s: Error path, created=%d, device '%s'\n", __func__, |
| 477 | created, dev->name); |
Simon Glass | dd82d44 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 478 | if (created) { |
Stefan Roese | 80b5bc9 | 2017-03-20 12:51:48 +0100 | [diff] [blame] | 479 | device_remove(dev, DM_REMOVE_NORMAL); |
Simon Glass | dd82d44 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 480 | device_unbind(dev); |
| 481 | } |
| 482 | |
| 483 | return ret; |
| 484 | } |
| 485 | |
| 486 | /* Compatibility function - to be removed */ |
Simon Glass | dd82d44 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 487 | struct spi_slave *spi_setup_slave(unsigned int busnum, unsigned int cs, |
| 488 | unsigned int speed, unsigned int mode) |
| 489 | { |
| 490 | struct spi_slave *slave; |
| 491 | struct udevice *dev; |
| 492 | int ret; |
| 493 | |
Patrice Chotard | 86e06ae | 2022-03-30 09:33:13 +0200 | [diff] [blame] | 494 | ret = _spi_get_bus_and_cs(busnum, cs, speed, mode, NULL, 0, &dev, |
| 495 | &slave); |
Simon Glass | dd82d44 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 496 | if (ret) |
| 497 | return NULL; |
| 498 | |
| 499 | return slave; |
| 500 | } |
| 501 | |
| 502 | void spi_free_slave(struct spi_slave *slave) |
| 503 | { |
Stefan Roese | 80b5bc9 | 2017-03-20 12:51:48 +0100 | [diff] [blame] | 504 | device_remove(slave->dev, DM_REMOVE_NORMAL); |
Simon Glass | dd82d44 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 505 | } |
| 506 | |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 507 | int spi_slave_of_to_plat(struct udevice *dev, struct dm_spi_slave_plat *plat) |
Simon Glass | dd82d44 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 508 | { |
Jagan Teki | 96536b1 | 2016-08-08 17:12:12 +0530 | [diff] [blame] | 509 | int mode = 0; |
Mugunthan V N | 4b0f40c | 2015-12-23 20:39:37 +0530 | [diff] [blame] | 510 | int value; |
Simon Glass | dd82d44 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 511 | |
Simon Glass | 4b28058 | 2017-05-18 20:09:54 -0600 | [diff] [blame] | 512 | plat->cs = dev_read_u32_default(dev, "reg", -1); |
Simon Goldschmidt | a942e1a | 2018-10-30 21:09:48 +0100 | [diff] [blame] | 513 | plat->max_hz = dev_read_u32_default(dev, "spi-max-frequency", |
| 514 | SPI_DEFAULT_SPEED_HZ); |
Simon Glass | 4b28058 | 2017-05-18 20:09:54 -0600 | [diff] [blame] | 515 | if (dev_read_bool(dev, "spi-cpol")) |
Simon Glass | dd82d44 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 516 | mode |= SPI_CPOL; |
Simon Glass | 4b28058 | 2017-05-18 20:09:54 -0600 | [diff] [blame] | 517 | if (dev_read_bool(dev, "spi-cpha")) |
Simon Glass | dd82d44 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 518 | mode |= SPI_CPHA; |
Simon Glass | 4b28058 | 2017-05-18 20:09:54 -0600 | [diff] [blame] | 519 | if (dev_read_bool(dev, "spi-cs-high")) |
Simon Glass | dd82d44 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 520 | mode |= SPI_CS_HIGH; |
Simon Glass | 4b28058 | 2017-05-18 20:09:54 -0600 | [diff] [blame] | 521 | if (dev_read_bool(dev, "spi-3wire")) |
Jagan Teki | d3868fd | 2015-12-03 22:19:05 +0530 | [diff] [blame] | 522 | mode |= SPI_3WIRE; |
Simon Glass | 4b28058 | 2017-05-18 20:09:54 -0600 | [diff] [blame] | 523 | if (dev_read_bool(dev, "spi-half-duplex")) |
Simon Glass | dd82d44 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 524 | mode |= SPI_PREAMBLE; |
Mugunthan V N | 4b0f40c | 2015-12-23 20:39:37 +0530 | [diff] [blame] | 525 | |
| 526 | /* Device DUAL/QUAD mode */ |
Simon Glass | 4b28058 | 2017-05-18 20:09:54 -0600 | [diff] [blame] | 527 | value = dev_read_u32_default(dev, "spi-tx-bus-width", 1); |
Mugunthan V N | 4b0f40c | 2015-12-23 20:39:37 +0530 | [diff] [blame] | 528 | switch (value) { |
| 529 | case 1: |
| 530 | break; |
| 531 | case 2: |
| 532 | mode |= SPI_TX_DUAL; |
| 533 | break; |
| 534 | case 4: |
| 535 | mode |= SPI_TX_QUAD; |
| 536 | break; |
Vignesh Raghavendra | c063ee3 | 2019-12-05 15:46:05 +0530 | [diff] [blame] | 537 | case 8: |
| 538 | mode |= SPI_TX_OCTAL; |
| 539 | break; |
Mugunthan V N | 4b0f40c | 2015-12-23 20:39:37 +0530 | [diff] [blame] | 540 | default: |
Simon Glass | 6f5bed0 | 2016-11-29 20:00:13 -0700 | [diff] [blame] | 541 | warn_non_spl("spi-tx-bus-width %d not supported\n", value); |
Mugunthan V N | 4b0f40c | 2015-12-23 20:39:37 +0530 | [diff] [blame] | 542 | break; |
| 543 | } |
| 544 | |
Simon Glass | 4b28058 | 2017-05-18 20:09:54 -0600 | [diff] [blame] | 545 | value = dev_read_u32_default(dev, "spi-rx-bus-width", 1); |
Mugunthan V N | 4b0f40c | 2015-12-23 20:39:37 +0530 | [diff] [blame] | 546 | switch (value) { |
| 547 | case 1: |
| 548 | break; |
| 549 | case 2: |
Jagan Teki | 96536b1 | 2016-08-08 17:12:12 +0530 | [diff] [blame] | 550 | mode |= SPI_RX_DUAL; |
Mugunthan V N | 4b0f40c | 2015-12-23 20:39:37 +0530 | [diff] [blame] | 551 | break; |
| 552 | case 4: |
Jagan Teki | 96536b1 | 2016-08-08 17:12:12 +0530 | [diff] [blame] | 553 | mode |= SPI_RX_QUAD; |
Mugunthan V N | 4b0f40c | 2015-12-23 20:39:37 +0530 | [diff] [blame] | 554 | break; |
Vignesh Raghavendra | c063ee3 | 2019-12-05 15:46:05 +0530 | [diff] [blame] | 555 | case 8: |
| 556 | mode |= SPI_RX_OCTAL; |
| 557 | break; |
Mugunthan V N | 4b0f40c | 2015-12-23 20:39:37 +0530 | [diff] [blame] | 558 | default: |
Simon Glass | 6f5bed0 | 2016-11-29 20:00:13 -0700 | [diff] [blame] | 559 | warn_non_spl("spi-rx-bus-width %d not supported\n", value); |
Mugunthan V N | 4b0f40c | 2015-12-23 20:39:37 +0530 | [diff] [blame] | 560 | break; |
| 561 | } |
| 562 | |
Jagan Teki | 96536b1 | 2016-08-08 17:12:12 +0530 | [diff] [blame] | 563 | plat->mode = mode; |
Mugunthan V N | 4b0f40c | 2015-12-23 20:39:37 +0530 | [diff] [blame] | 564 | |
Simon Glass | dd82d44 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 565 | return 0; |
| 566 | } |
| 567 | |
| 568 | UCLASS_DRIVER(spi) = { |
| 569 | .id = UCLASS_SPI, |
| 570 | .name = "spi", |
Simon Glass | 0ccb097 | 2015-01-25 08:27:05 -0700 | [diff] [blame] | 571 | .flags = DM_UC_FLAG_SEQ_ALIAS, |
Simon Glass | 3580f6d | 2021-08-07 07:24:03 -0600 | [diff] [blame] | 572 | #if CONFIG_IS_ENABLED(OF_REAL) |
Simon Glass | 1823034 | 2016-07-05 17:10:10 -0600 | [diff] [blame] | 573 | .post_bind = dm_scan_fdt_dev, |
Simon Glass | 3fb3339 | 2016-11-13 14:22:01 -0700 | [diff] [blame] | 574 | #endif |
Simon Glass | dd82d44 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 575 | .post_probe = spi_post_probe, |
Simon Glass | 82c2f50 | 2015-01-25 08:27:11 -0700 | [diff] [blame] | 576 | .child_pre_probe = spi_child_pre_probe, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 577 | .per_device_auto = sizeof(struct dm_spi_bus), |
| 578 | .per_child_auto = sizeof(struct spi_slave), |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 579 | .per_child_plat_auto = sizeof(struct dm_spi_slave_plat), |
Simon Glass | 9288265 | 2021-08-07 07:24:04 -0600 | [diff] [blame] | 580 | #if CONFIG_IS_ENABLED(OF_REAL) |
Simon Glass | 5d2ee05 | 2015-01-25 08:27:12 -0700 | [diff] [blame] | 581 | .child_post_bind = spi_child_post_bind, |
Simon Glass | 3fb3339 | 2016-11-13 14:22:01 -0700 | [diff] [blame] | 582 | #endif |
Simon Glass | dd82d44 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 583 | }; |
| 584 | |
| 585 | UCLASS_DRIVER(spi_generic) = { |
| 586 | .id = UCLASS_SPI_GENERIC, |
| 587 | .name = "spi_generic", |
| 588 | }; |
| 589 | |
| 590 | U_BOOT_DRIVER(spi_generic_drv) = { |
| 591 | .name = "spi_generic_drv", |
| 592 | .id = UCLASS_SPI_GENERIC, |
| 593 | }; |