Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | d7af6a4 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2014 Google, Inc |
Simon Glass | d7af6a4 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 4 | */ |
| 5 | |
Patrick Delaunay | 46c5391 | 2020-10-15 17:18:17 +0200 | [diff] [blame] | 6 | #define LOG_CATEGORY UCLASS_SPI |
| 7 | |
Simon Glass | d7af6a4 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 8 | #include <common.h> |
| 9 | #include <dm.h> |
| 10 | #include <errno.h> |
Simon Glass | f7ae49f | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 11 | #include <log.h> |
Simon Glass | d7af6a4 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 12 | #include <malloc.h> |
| 13 | #include <spi.h> |
Patrick Delaunay | 46c5391 | 2020-10-15 17:18:17 +0200 | [diff] [blame] | 14 | #include <dm/device_compat.h> |
Simon Glass | 401d1c4 | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 15 | #include <asm/global_data.h> |
Simon Glass | d7af6a4 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 16 | #include <dm/device-internal.h> |
| 17 | #include <dm/uclass-internal.h> |
Simon Glass | d7af6a4 | 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 | 12bfb2e | 2018-10-30 21:09:48 +0100 | [diff] [blame] | 23 | #define SPI_DEFAULT_SPEED_HZ 100000 |
| 24 | |
Simon Glass | d7af6a4 | 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 | 46c5391 | 2020-10-15 17:18:17 +0200 | [diff] [blame] | 36 | dev_err(bus, "Cannot set speed (err=%d)\n", ret); |
Simon Glass | d7af6a4 | 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 | 46c5391 | 2020-10-15 17:18:17 +0200 | [diff] [blame] | 45 | dev_err(bus, "Cannot set mode (err=%d)\n", ret); |
Simon Glass | d7af6a4 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 46 | return ret; |
| 47 | } |
| 48 | |
| 49 | return 0; |
| 50 | } |
| 51 | |
Peng Fan | 7a3eff4 | 2016-05-03 10:02:22 +0800 | [diff] [blame] | 52 | int dm_spi_claim_bus(struct udevice *dev) |
Simon Glass | d7af6a4 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 53 | { |
Simon Glass | d7af6a4 | 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 | e564f05 | 2015-03-05 12:25:20 -0700 | [diff] [blame] | 56 | struct dm_spi_bus *spi = dev_get_uclass_priv(bus); |
Peng Fan | 7a3eff4 | 2016-05-03 10:02:22 +0800 | [diff] [blame] | 57 | struct spi_slave *slave = dev_get_parent_priv(dev); |
Ovidiu Panait | 741280e | 2020-12-14 19:06:50 +0200 | [diff] [blame] | 58 | uint speed, mode; |
Simon Glass | d7af6a4 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 59 | |
| 60 | speed = slave->max_hz; |
Ovidiu Panait | 741280e | 2020-12-14 19:06:50 +0200 | [diff] [blame] | 61 | mode = slave->mode; |
| 62 | |
Simon Glass | d7af6a4 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 63 | if (spi->max_hz) { |
| 64 | if (speed) |
Ovidiu Panait | 741280e | 2020-12-14 19:06:50 +0200 | [diff] [blame] | 65 | speed = min(speed, spi->max_hz); |
Simon Glass | d7af6a4 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 66 | else |
| 67 | speed = spi->max_hz; |
| 68 | } |
| 69 | if (!speed) |
Simon Goldschmidt | 12bfb2e | 2018-10-30 21:09:48 +0100 | [diff] [blame] | 70 | speed = SPI_DEFAULT_SPEED_HZ; |
Ovidiu Panait | 741280e | 2020-12-14 19:06:50 +0200 | [diff] [blame] | 71 | |
| 72 | if (speed != spi->speed || mode != spi->mode) { |
Mario Six | 24fc1ec | 2018-01-15 11:08:41 +0100 | [diff] [blame] | 73 | int ret = spi_set_speed_mode(bus, speed, slave->mode); |
| 74 | |
Simon Glass | 60e2809 | 2015-02-17 15:29:35 -0700 | [diff] [blame] | 75 | if (ret) |
Simon Glass | 5e24a2e | 2018-10-01 12:22:24 -0600 | [diff] [blame] | 76 | return log_ret(ret); |
Ovidiu Panait | 741280e | 2020-12-14 19:06:50 +0200 | [diff] [blame] | 77 | |
| 78 | spi->speed = speed; |
| 79 | spi->mode = mode; |
Simon Glass | 60e2809 | 2015-02-17 15:29:35 -0700 | [diff] [blame] | 80 | } |
Simon Glass | d7af6a4 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 81 | |
Simon Glass | 5e24a2e | 2018-10-01 12:22:24 -0600 | [diff] [blame] | 82 | return log_ret(ops->claim_bus ? ops->claim_bus(dev) : 0); |
Simon Glass | d7af6a4 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 83 | } |
| 84 | |
Peng Fan | 7a3eff4 | 2016-05-03 10:02:22 +0800 | [diff] [blame] | 85 | void dm_spi_release_bus(struct udevice *dev) |
Simon Glass | d7af6a4 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 86 | { |
Simon Glass | d7af6a4 | 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 | 9694b72 | 2015-04-19 09:05:40 -0600 | [diff] [blame] | 91 | ops->release_bus(dev); |
Simon Glass | d7af6a4 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 92 | } |
| 93 | |
Peng Fan | 7a3eff4 | 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 | d7af6a4 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 96 | { |
Simon Glass | d7af6a4 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 97 | struct udevice *bus = dev->parent; |
Simon Glass | ccdabd8 | 2019-12-06 21:42:35 -0700 | [diff] [blame] | 98 | struct dm_spi_ops *ops = spi_get_ops(bus); |
Simon Glass | d7af6a4 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 99 | |
| 100 | if (bus->uclass->uc_drv->id != UCLASS_SPI) |
| 101 | return -EOPNOTSUPP; |
Simon Glass | ccdabd8 | 2019-12-06 21:42:35 -0700 | [diff] [blame] | 102 | if (!ops->xfer) |
| 103 | return -ENOSYS; |
Simon Glass | d7af6a4 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 104 | |
Simon Glass | ccdabd8 | 2019-12-06 21:42:35 -0700 | [diff] [blame] | 105 | return ops->xfer(dev, bitlen, dout, din, flags); |
Simon Glass | d7af6a4 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 106 | } |
| 107 | |
Simon Glass | c53b318 | 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 | 7a3eff4 | 2016-05-03 10:02:22 +0800 | [diff] [blame] | 122 | int spi_claim_bus(struct spi_slave *slave) |
| 123 | { |
Simon Glass | 5e24a2e | 2018-10-01 12:22:24 -0600 | [diff] [blame] | 124 | return log_ret(dm_spi_claim_bus(slave->dev)); |
Peng Fan | 7a3eff4 | 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 | |
| 132 | int spi_xfer(struct spi_slave *slave, unsigned int bitlen, |
| 133 | const void *dout, void *din, unsigned long flags) |
| 134 | { |
| 135 | return dm_spi_xfer(slave->dev, bitlen, dout, din, flags); |
| 136 | } |
| 137 | |
Jagan Teki | 8473b32 | 2019-07-22 17:22:56 +0530 | [diff] [blame] | 138 | int spi_write_then_read(struct spi_slave *slave, const u8 *opcode, |
| 139 | size_t n_opcode, const u8 *txbuf, u8 *rxbuf, |
| 140 | size_t n_buf) |
| 141 | { |
| 142 | unsigned long flags = SPI_XFER_BEGIN; |
| 143 | int ret; |
| 144 | |
| 145 | if (n_buf == 0) |
| 146 | flags |= SPI_XFER_END; |
| 147 | |
| 148 | ret = spi_xfer(slave, n_opcode * 8, opcode, NULL, flags); |
| 149 | if (ret) { |
Patrick Delaunay | 46c5391 | 2020-10-15 17:18:17 +0200 | [diff] [blame] | 150 | dev_dbg(slave->dev, |
| 151 | "spi: failed to send command (%zu bytes): %d\n", |
| 152 | n_opcode, ret); |
Jagan Teki | 8473b32 | 2019-07-22 17:22:56 +0530 | [diff] [blame] | 153 | } else if (n_buf != 0) { |
| 154 | ret = spi_xfer(slave, n_buf * 8, txbuf, rxbuf, SPI_XFER_END); |
| 155 | if (ret) |
Patrick Delaunay | 46c5391 | 2020-10-15 17:18:17 +0200 | [diff] [blame] | 156 | dev_dbg(slave->dev, |
| 157 | "spi: failed to transfer %zu bytes of data: %d\n", |
| 158 | n_buf, ret); |
Jagan Teki | 8473b32 | 2019-07-22 17:22:56 +0530 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | return ret; |
| 162 | } |
| 163 | |
Simon Glass | 71634f2 | 2016-11-13 14:22:01 -0700 | [diff] [blame] | 164 | #if !CONFIG_IS_ENABLED(OF_PLATDATA) |
Simon Glass | 6f849c3 | 2015-06-23 15:39:05 -0600 | [diff] [blame] | 165 | static int spi_child_post_bind(struct udevice *dev) |
Simon Glass | d7af6a4 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 166 | { |
Simon Glass | 8a8d24b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 167 | struct dm_spi_slave_plat *plat = dev_get_parent_plat(dev); |
Simon Glass | d7af6a4 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 168 | |
Simon Glass | 7d14ee4 | 2020-12-19 10:40:13 -0700 | [diff] [blame] | 169 | if (!dev_has_ofnode(dev)) |
Simon Glass | d0cff03 | 2015-01-25 08:27:12 -0700 | [diff] [blame] | 170 | return 0; |
| 171 | |
Simon Glass | d1998a9 | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 172 | return spi_slave_of_to_plat(dev, plat); |
Simon Glass | d0cff03 | 2015-01-25 08:27:12 -0700 | [diff] [blame] | 173 | } |
Simon Glass | 71634f2 | 2016-11-13 14:22:01 -0700 | [diff] [blame] | 174 | #endif |
Simon Glass | d0cff03 | 2015-01-25 08:27:12 -0700 | [diff] [blame] | 175 | |
Simon Glass | 6f849c3 | 2015-06-23 15:39:05 -0600 | [diff] [blame] | 176 | static int spi_post_probe(struct udevice *bus) |
Simon Glass | d0cff03 | 2015-01-25 08:27:12 -0700 | [diff] [blame] | 177 | { |
Simon Glass | 71634f2 | 2016-11-13 14:22:01 -0700 | [diff] [blame] | 178 | #if !CONFIG_IS_ENABLED(OF_PLATDATA) |
Simon Glass | e564f05 | 2015-03-05 12:25:20 -0700 | [diff] [blame] | 179 | struct dm_spi_bus *spi = dev_get_uclass_priv(bus); |
Simon Glass | d0cff03 | 2015-01-25 08:27:12 -0700 | [diff] [blame] | 180 | |
Simon Glass | 279e26f | 2017-05-18 20:09:54 -0600 | [diff] [blame] | 181 | spi->max_hz = dev_read_u32_default(bus, "spi-max-frequency", 0); |
Simon Glass | 71634f2 | 2016-11-13 14:22:01 -0700 | [diff] [blame] | 182 | #endif |
Michal Simek | 281f156 | 2015-10-27 13:36:42 +0100 | [diff] [blame] | 183 | #if defined(CONFIG_NEEDS_MANUAL_RELOC) |
| 184 | struct dm_spi_ops *ops = spi_get_ops(bus); |
Ashok Reddy Soma | 4d9b1af | 2019-09-17 00:11:02 -0600 | [diff] [blame] | 185 | static int reloc_done; |
Michal Simek | 281f156 | 2015-10-27 13:36:42 +0100 | [diff] [blame] | 186 | |
Ashok Reddy Soma | 4d9b1af | 2019-09-17 00:11:02 -0600 | [diff] [blame] | 187 | if (!reloc_done) { |
| 188 | if (ops->claim_bus) |
| 189 | ops->claim_bus += gd->reloc_off; |
| 190 | if (ops->release_bus) |
| 191 | ops->release_bus += gd->reloc_off; |
| 192 | if (ops->set_wordlen) |
| 193 | ops->set_wordlen += gd->reloc_off; |
| 194 | if (ops->xfer) |
| 195 | ops->xfer += gd->reloc_off; |
| 196 | if (ops->set_speed) |
| 197 | ops->set_speed += gd->reloc_off; |
| 198 | if (ops->set_mode) |
| 199 | ops->set_mode += gd->reloc_off; |
| 200 | if (ops->cs_info) |
| 201 | ops->cs_info += gd->reloc_off; |
| 202 | reloc_done++; |
| 203 | } |
Michal Simek | 281f156 | 2015-10-27 13:36:42 +0100 | [diff] [blame] | 204 | #endif |
| 205 | |
Simon Glass | d7af6a4 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 206 | return 0; |
| 207 | } |
| 208 | |
Simon Glass | 6f849c3 | 2015-06-23 15:39:05 -0600 | [diff] [blame] | 209 | static int spi_child_pre_probe(struct udevice *dev) |
Simon Glass | 440714e | 2015-01-25 08:27:11 -0700 | [diff] [blame] | 210 | { |
Simon Glass | 8a8d24b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 211 | struct dm_spi_slave_plat *plat = dev_get_parent_plat(dev); |
Simon Glass | bcbe3d1 | 2015-09-28 23:32:01 -0600 | [diff] [blame] | 212 | struct spi_slave *slave = dev_get_parent_priv(dev); |
Simon Glass | 440714e | 2015-01-25 08:27:11 -0700 | [diff] [blame] | 213 | |
Simon Glass | d0cff03 | 2015-01-25 08:27:12 -0700 | [diff] [blame] | 214 | /* |
| 215 | * This is needed because we pass struct spi_slave around the place |
| 216 | * instead slave->dev (a struct udevice). So we have to have some |
| 217 | * way to access the slave udevice given struct spi_slave. Once we |
| 218 | * change the SPI API to use udevice instead of spi_slave, we can |
| 219 | * drop this. |
| 220 | */ |
Simon Glass | 440714e | 2015-01-25 08:27:11 -0700 | [diff] [blame] | 221 | slave->dev = dev; |
| 222 | |
Simon Glass | d0cff03 | 2015-01-25 08:27:12 -0700 | [diff] [blame] | 223 | slave->max_hz = plat->max_hz; |
| 224 | slave->mode = plat->mode; |
Christophe Ricard | 674f360 | 2016-01-17 11:56:48 +0100 | [diff] [blame] | 225 | slave->wordlen = SPI_DEFAULT_WORDLEN; |
Simon Glass | d0cff03 | 2015-01-25 08:27:12 -0700 | [diff] [blame] | 226 | |
Simon Glass | 440714e | 2015-01-25 08:27:11 -0700 | [diff] [blame] | 227 | return 0; |
| 228 | } |
| 229 | |
Simon Glass | d7af6a4 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 230 | int spi_chip_select(struct udevice *dev) |
| 231 | { |
Simon Glass | 8a8d24b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 232 | struct dm_spi_slave_plat *plat = dev_get_parent_plat(dev); |
Simon Glass | d7af6a4 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 233 | |
Simon Glass | d0cff03 | 2015-01-25 08:27:12 -0700 | [diff] [blame] | 234 | return plat ? plat->cs : -ENOENT; |
Simon Glass | d7af6a4 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 235 | } |
| 236 | |
Simon Glass | ff56bba | 2014-11-11 10:46:22 -0700 | [diff] [blame] | 237 | int spi_find_chip_select(struct udevice *bus, int cs, struct udevice **devp) |
Simon Glass | d7af6a4 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 238 | { |
Bin Meng | 7bacce5 | 2019-09-09 06:00:02 -0700 | [diff] [blame] | 239 | struct dm_spi_ops *ops; |
| 240 | struct spi_cs_info info; |
Simon Glass | d7af6a4 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 241 | struct udevice *dev; |
Bin Meng | 7bacce5 | 2019-09-09 06:00:02 -0700 | [diff] [blame] | 242 | int ret; |
| 243 | |
| 244 | /* |
| 245 | * Ask the driver. For the moment we don't have CS info. |
| 246 | * When we do we could provide the driver with a helper function |
| 247 | * to figure out what chip selects are valid, or just handle the |
| 248 | * request. |
| 249 | */ |
| 250 | ops = spi_get_ops(bus); |
| 251 | if (ops->cs_info) { |
| 252 | ret = ops->cs_info(bus, cs, &info); |
| 253 | } else { |
| 254 | /* |
| 255 | * We could assume there is at least one valid chip select. |
| 256 | * The driver didn't care enough to tell us. |
| 257 | */ |
| 258 | ret = 0; |
| 259 | } |
| 260 | |
| 261 | if (ret) { |
Patrick Delaunay | 46c5391 | 2020-10-15 17:18:17 +0200 | [diff] [blame] | 262 | dev_err(bus, "Invalid cs %d (err=%d)\n", cs, ret); |
Bin Meng | 7bacce5 | 2019-09-09 06:00:02 -0700 | [diff] [blame] | 263 | return ret; |
| 264 | } |
Simon Glass | d7af6a4 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 265 | |
| 266 | for (device_find_first_child(bus, &dev); dev; |
| 267 | device_find_next_child(&dev)) { |
Simon Glass | 8a8d24b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 268 | struct dm_spi_slave_plat *plat; |
Simon Glass | d7af6a4 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 269 | |
Simon Glass | caa4daa | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 270 | plat = dev_get_parent_plat(dev); |
Patrick Delaunay | 46c5391 | 2020-10-15 17:18:17 +0200 | [diff] [blame] | 271 | dev_dbg(bus, "%s: plat=%p, cs=%d\n", __func__, plat, plat->cs); |
Simon Glass | d0cff03 | 2015-01-25 08:27:12 -0700 | [diff] [blame] | 272 | if (plat->cs == cs) { |
Simon Glass | d7af6a4 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 273 | *devp = dev; |
| 274 | return 0; |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | return -ENODEV; |
| 279 | } |
| 280 | |
| 281 | int spi_cs_is_valid(unsigned int busnum, unsigned int cs) |
| 282 | { |
| 283 | struct spi_cs_info info; |
| 284 | struct udevice *bus; |
| 285 | int ret; |
| 286 | |
Simon Glass | 9917591 | 2020-12-16 21:20:29 -0700 | [diff] [blame] | 287 | ret = uclass_find_device_by_seq(UCLASS_SPI, busnum, &bus); |
Simon Glass | d7af6a4 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 288 | if (ret) { |
Patrick Delaunay | 46c5391 | 2020-10-15 17:18:17 +0200 | [diff] [blame] | 289 | log_debug("%s: No bus %d\n", __func__, busnum); |
Simon Glass | d7af6a4 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 290 | return ret; |
| 291 | } |
| 292 | |
| 293 | return spi_cs_info(bus, cs, &info); |
| 294 | } |
| 295 | |
| 296 | int spi_cs_info(struct udevice *bus, uint cs, struct spi_cs_info *info) |
| 297 | { |
| 298 | struct spi_cs_info local_info; |
Simon Glass | d7af6a4 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 299 | int ret; |
| 300 | |
| 301 | if (!info) |
| 302 | info = &local_info; |
| 303 | |
| 304 | /* If there is a device attached, return it */ |
| 305 | info->dev = NULL; |
| 306 | ret = spi_find_chip_select(bus, cs, &info->dev); |
Bin Meng | 7bacce5 | 2019-09-09 06:00:02 -0700 | [diff] [blame] | 307 | return ret == -ENODEV ? 0 : ret; |
Simon Glass | d7af6a4 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 308 | } |
| 309 | |
Simon Glass | d7af6a4 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 310 | int spi_find_bus_and_cs(int busnum, int cs, struct udevice **busp, |
| 311 | struct udevice **devp) |
| 312 | { |
| 313 | struct udevice *bus, *dev; |
| 314 | int ret; |
| 315 | |
Simon Glass | 9917591 | 2020-12-16 21:20:29 -0700 | [diff] [blame] | 316 | ret = uclass_find_device_by_seq(UCLASS_SPI, busnum, &bus); |
Simon Glass | d7af6a4 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 317 | if (ret) { |
Patrick Delaunay | 46c5391 | 2020-10-15 17:18:17 +0200 | [diff] [blame] | 318 | log_debug("%s: No bus %d\n", __func__, busnum); |
Simon Glass | d7af6a4 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 319 | return ret; |
| 320 | } |
| 321 | ret = spi_find_chip_select(bus, cs, &dev); |
| 322 | if (ret) { |
Patrick Delaunay | 46c5391 | 2020-10-15 17:18:17 +0200 | [diff] [blame] | 323 | dev_dbg(bus, "%s: No cs %d\n", __func__, cs); |
Simon Glass | d7af6a4 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 324 | return ret; |
| 325 | } |
| 326 | *busp = bus; |
| 327 | *devp = dev; |
| 328 | |
| 329 | return ret; |
| 330 | } |
| 331 | |
| 332 | int spi_get_bus_and_cs(int busnum, int cs, int speed, int mode, |
| 333 | const char *drv_name, const char *dev_name, |
| 334 | struct udevice **busp, struct spi_slave **devp) |
| 335 | { |
| 336 | struct udevice *bus, *dev; |
Simon Glass | 8a8d24b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 337 | struct dm_spi_slave_plat *plat; |
Ovidiu Panait | 741280e | 2020-12-14 19:06:50 +0200 | [diff] [blame] | 338 | struct dm_spi_bus *bus_data; |
Marcin Wojtas | f7dd537 | 2019-11-21 05:38:47 +0100 | [diff] [blame] | 339 | struct spi_slave *slave; |
Simon Glass | d7af6a4 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 340 | bool created = false; |
| 341 | int ret; |
| 342 | |
Thomas Fitzsimmons | 640abba | 2019-09-06 07:51:19 -0400 | [diff] [blame] | 343 | #if CONFIG_IS_ENABLED(OF_PLATDATA) |
Simon Glass | 71634f2 | 2016-11-13 14:22:01 -0700 | [diff] [blame] | 344 | ret = uclass_first_device_err(UCLASS_SPI, &bus); |
| 345 | #else |
Simon Glass | d7af6a4 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 346 | ret = uclass_get_device_by_seq(UCLASS_SPI, busnum, &bus); |
Simon Glass | 71634f2 | 2016-11-13 14:22:01 -0700 | [diff] [blame] | 347 | #endif |
Simon Glass | d7af6a4 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 348 | if (ret) { |
Patrick Delaunay | 46c5391 | 2020-10-15 17:18:17 +0200 | [diff] [blame] | 349 | log_err("Invalid bus %d (err=%d)\n", busnum, ret); |
Simon Glass | d7af6a4 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 350 | return ret; |
| 351 | } |
| 352 | ret = spi_find_chip_select(bus, cs, &dev); |
| 353 | |
| 354 | /* |
| 355 | * If there is no such device, create one automatically. This means |
| 356 | * that we don't need a device tree node or platform data for the |
| 357 | * SPI flash chip - we will bind to the correct driver. |
| 358 | */ |
| 359 | if (ret == -ENODEV && drv_name) { |
Patrick Delaunay | 46c5391 | 2020-10-15 17:18:17 +0200 | [diff] [blame] | 360 | dev_dbg(bus, "%s: Binding new device '%s', busnum=%d, cs=%d, driver=%s\n", |
| 361 | __func__, dev_name, busnum, cs, drv_name); |
Simon Glass | 6b18656 | 2014-11-11 10:46:23 -0700 | [diff] [blame] | 362 | ret = device_bind_driver(bus, drv_name, dev_name, &dev); |
Simon Glass | 28f9885 | 2016-11-13 14:22:05 -0700 | [diff] [blame] | 363 | if (ret) { |
Patrick Delaunay | 46c5391 | 2020-10-15 17:18:17 +0200 | [diff] [blame] | 364 | dev_dbg(bus, "%s: Unable to bind driver (ret=%d)\n", |
| 365 | __func__, ret); |
Simon Glass | d7af6a4 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 366 | return ret; |
Simon Glass | 28f9885 | 2016-11-13 14:22:05 -0700 | [diff] [blame] | 367 | } |
Simon Glass | caa4daa | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 368 | plat = dev_get_parent_plat(dev); |
Simon Glass | d0cff03 | 2015-01-25 08:27:12 -0700 | [diff] [blame] | 369 | plat->cs = cs; |
Simon Goldschmidt | 12bfb2e | 2018-10-30 21:09:48 +0100 | [diff] [blame] | 370 | if (speed) { |
| 371 | plat->max_hz = speed; |
| 372 | } else { |
Patrick Delaunay | 46c5391 | 2020-10-15 17:18:17 +0200 | [diff] [blame] | 373 | dev_warn(bus, |
| 374 | "Warning: SPI speed fallback to %u kHz\n", |
| 375 | SPI_DEFAULT_SPEED_HZ / 1000); |
Simon Goldschmidt | 12bfb2e | 2018-10-30 21:09:48 +0100 | [diff] [blame] | 376 | plat->max_hz = SPI_DEFAULT_SPEED_HZ; |
| 377 | } |
Simon Glass | d0cff03 | 2015-01-25 08:27:12 -0700 | [diff] [blame] | 378 | plat->mode = mode; |
Simon Glass | d7af6a4 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 379 | created = true; |
| 380 | } else if (ret) { |
Patrick Delaunay | 46c5391 | 2020-10-15 17:18:17 +0200 | [diff] [blame] | 381 | dev_err(bus, "Invalid chip select %d:%d (err=%d)\n", busnum, cs, ret); |
Simon Glass | d7af6a4 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 382 | return ret; |
| 383 | } |
| 384 | |
| 385 | if (!device_active(dev)) { |
Simon Glass | d0cff03 | 2015-01-25 08:27:12 -0700 | [diff] [blame] | 386 | struct spi_slave *slave; |
Simon Glass | d7af6a4 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 387 | |
Simon Glass | d0cff03 | 2015-01-25 08:27:12 -0700 | [diff] [blame] | 388 | ret = device_probe(dev); |
Simon Glass | d7af6a4 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 389 | if (ret) |
| 390 | goto err; |
Simon Glass | bcbe3d1 | 2015-09-28 23:32:01 -0600 | [diff] [blame] | 391 | slave = dev_get_parent_priv(dev); |
Simon Glass | d7af6a4 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 392 | slave->dev = dev; |
Simon Glass | d7af6a4 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 393 | } |
| 394 | |
Marcin Wojtas | f7dd537 | 2019-11-21 05:38:47 +0100 | [diff] [blame] | 395 | slave = dev_get_parent_priv(dev); |
Ovidiu Panait | 741280e | 2020-12-14 19:06:50 +0200 | [diff] [blame] | 396 | bus_data = dev_get_uclass_priv(bus); |
Patrick Delaunay | b0cc1b8 | 2019-02-27 15:36:44 +0100 | [diff] [blame] | 397 | |
Marcin Wojtas | f7dd537 | 2019-11-21 05:38:47 +0100 | [diff] [blame] | 398 | /* |
| 399 | * In case the operation speed is not yet established by |
| 400 | * dm_spi_claim_bus() ensure the bus is configured properly. |
| 401 | */ |
Ovidiu Panait | 741280e | 2020-12-14 19:06:50 +0200 | [diff] [blame] | 402 | if (!bus_data->speed) { |
Marcin Wojtas | f7dd537 | 2019-11-21 05:38:47 +0100 | [diff] [blame] | 403 | ret = spi_claim_bus(slave); |
| 404 | if (ret) |
| 405 | goto err; |
Vignesh R | 96907c0 | 2016-07-06 10:04:28 +0530 | [diff] [blame] | 406 | } |
Simon Glass | d7af6a4 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 407 | |
| 408 | *busp = bus; |
Marcin Wojtas | f7dd537 | 2019-11-21 05:38:47 +0100 | [diff] [blame] | 409 | *devp = slave; |
Patrick Delaunay | 46c5391 | 2020-10-15 17:18:17 +0200 | [diff] [blame] | 410 | log_debug("%s: bus=%p, slave=%p\n", __func__, bus, *devp); |
Simon Glass | d7af6a4 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 411 | |
| 412 | return 0; |
| 413 | |
| 414 | err: |
Patrick Delaunay | 46c5391 | 2020-10-15 17:18:17 +0200 | [diff] [blame] | 415 | log_debug("%s: Error path, created=%d, device '%s'\n", __func__, |
| 416 | created, dev->name); |
Simon Glass | d7af6a4 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 417 | if (created) { |
Stefan Roese | 706865a | 2017-03-20 12:51:48 +0100 | [diff] [blame] | 418 | device_remove(dev, DM_REMOVE_NORMAL); |
Simon Glass | d7af6a4 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 419 | device_unbind(dev); |
| 420 | } |
| 421 | |
| 422 | return ret; |
| 423 | } |
| 424 | |
| 425 | /* Compatibility function - to be removed */ |
Simon Glass | d7af6a4 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 426 | struct spi_slave *spi_setup_slave(unsigned int busnum, unsigned int cs, |
| 427 | unsigned int speed, unsigned int mode) |
| 428 | { |
| 429 | struct spi_slave *slave; |
| 430 | struct udevice *dev; |
| 431 | int ret; |
| 432 | |
| 433 | ret = spi_get_bus_and_cs(busnum, cs, speed, mode, NULL, 0, &dev, |
Simon Glass | 279e26f | 2017-05-18 20:09:54 -0600 | [diff] [blame] | 434 | &slave); |
Simon Glass | d7af6a4 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 435 | if (ret) |
| 436 | return NULL; |
| 437 | |
| 438 | return slave; |
| 439 | } |
| 440 | |
| 441 | void spi_free_slave(struct spi_slave *slave) |
| 442 | { |
Stefan Roese | 706865a | 2017-03-20 12:51:48 +0100 | [diff] [blame] | 443 | device_remove(slave->dev, DM_REMOVE_NORMAL); |
Simon Glass | d7af6a4 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 444 | } |
| 445 | |
Simon Glass | 8a8d24b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 446 | int spi_slave_of_to_plat(struct udevice *dev, struct dm_spi_slave_plat *plat) |
Simon Glass | d7af6a4 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 447 | { |
Jagan Teki | 08fe9c2 | 2016-08-08 17:12:12 +0530 | [diff] [blame] | 448 | int mode = 0; |
Mugunthan V N | f8e2f92 | 2015-12-23 20:39:37 +0530 | [diff] [blame] | 449 | int value; |
Simon Glass | d7af6a4 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 450 | |
Simon Glass | 279e26f | 2017-05-18 20:09:54 -0600 | [diff] [blame] | 451 | plat->cs = dev_read_u32_default(dev, "reg", -1); |
Simon Goldschmidt | 12bfb2e | 2018-10-30 21:09:48 +0100 | [diff] [blame] | 452 | plat->max_hz = dev_read_u32_default(dev, "spi-max-frequency", |
| 453 | SPI_DEFAULT_SPEED_HZ); |
Simon Glass | 279e26f | 2017-05-18 20:09:54 -0600 | [diff] [blame] | 454 | if (dev_read_bool(dev, "spi-cpol")) |
Simon Glass | d7af6a4 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 455 | mode |= SPI_CPOL; |
Simon Glass | 279e26f | 2017-05-18 20:09:54 -0600 | [diff] [blame] | 456 | if (dev_read_bool(dev, "spi-cpha")) |
Simon Glass | d7af6a4 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 457 | mode |= SPI_CPHA; |
Simon Glass | 279e26f | 2017-05-18 20:09:54 -0600 | [diff] [blame] | 458 | if (dev_read_bool(dev, "spi-cs-high")) |
Simon Glass | d7af6a4 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 459 | mode |= SPI_CS_HIGH; |
Simon Glass | 279e26f | 2017-05-18 20:09:54 -0600 | [diff] [blame] | 460 | if (dev_read_bool(dev, "spi-3wire")) |
Jagan Teki | 379b49d | 2015-12-03 22:19:05 +0530 | [diff] [blame] | 461 | mode |= SPI_3WIRE; |
Simon Glass | 279e26f | 2017-05-18 20:09:54 -0600 | [diff] [blame] | 462 | if (dev_read_bool(dev, "spi-half-duplex")) |
Simon Glass | d7af6a4 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 463 | mode |= SPI_PREAMBLE; |
Mugunthan V N | f8e2f92 | 2015-12-23 20:39:37 +0530 | [diff] [blame] | 464 | |
| 465 | /* Device DUAL/QUAD mode */ |
Simon Glass | 279e26f | 2017-05-18 20:09:54 -0600 | [diff] [blame] | 466 | value = dev_read_u32_default(dev, "spi-tx-bus-width", 1); |
Mugunthan V N | f8e2f92 | 2015-12-23 20:39:37 +0530 | [diff] [blame] | 467 | switch (value) { |
| 468 | case 1: |
| 469 | break; |
| 470 | case 2: |
| 471 | mode |= SPI_TX_DUAL; |
| 472 | break; |
| 473 | case 4: |
| 474 | mode |= SPI_TX_QUAD; |
| 475 | break; |
Vignesh Raghavendra | 658df8b | 2019-12-05 15:46:05 +0530 | [diff] [blame] | 476 | case 8: |
| 477 | mode |= SPI_TX_OCTAL; |
| 478 | break; |
Mugunthan V N | f8e2f92 | 2015-12-23 20:39:37 +0530 | [diff] [blame] | 479 | default: |
Simon Glass | 1b7c28f | 2016-11-29 20:00:13 -0700 | [diff] [blame] | 480 | warn_non_spl("spi-tx-bus-width %d not supported\n", value); |
Mugunthan V N | f8e2f92 | 2015-12-23 20:39:37 +0530 | [diff] [blame] | 481 | break; |
| 482 | } |
| 483 | |
Simon Glass | 279e26f | 2017-05-18 20:09:54 -0600 | [diff] [blame] | 484 | value = dev_read_u32_default(dev, "spi-rx-bus-width", 1); |
Mugunthan V N | f8e2f92 | 2015-12-23 20:39:37 +0530 | [diff] [blame] | 485 | switch (value) { |
| 486 | case 1: |
| 487 | break; |
| 488 | case 2: |
Jagan Teki | 08fe9c2 | 2016-08-08 17:12:12 +0530 | [diff] [blame] | 489 | mode |= SPI_RX_DUAL; |
Mugunthan V N | f8e2f92 | 2015-12-23 20:39:37 +0530 | [diff] [blame] | 490 | break; |
| 491 | case 4: |
Jagan Teki | 08fe9c2 | 2016-08-08 17:12:12 +0530 | [diff] [blame] | 492 | mode |= SPI_RX_QUAD; |
Mugunthan V N | f8e2f92 | 2015-12-23 20:39:37 +0530 | [diff] [blame] | 493 | break; |
Vignesh Raghavendra | 658df8b | 2019-12-05 15:46:05 +0530 | [diff] [blame] | 494 | case 8: |
| 495 | mode |= SPI_RX_OCTAL; |
| 496 | break; |
Mugunthan V N | f8e2f92 | 2015-12-23 20:39:37 +0530 | [diff] [blame] | 497 | default: |
Simon Glass | 1b7c28f | 2016-11-29 20:00:13 -0700 | [diff] [blame] | 498 | warn_non_spl("spi-rx-bus-width %d not supported\n", value); |
Mugunthan V N | f8e2f92 | 2015-12-23 20:39:37 +0530 | [diff] [blame] | 499 | break; |
| 500 | } |
| 501 | |
Jagan Teki | 08fe9c2 | 2016-08-08 17:12:12 +0530 | [diff] [blame] | 502 | plat->mode = mode; |
Mugunthan V N | f8e2f92 | 2015-12-23 20:39:37 +0530 | [diff] [blame] | 503 | |
Simon Glass | d7af6a4 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 504 | return 0; |
| 505 | } |
| 506 | |
| 507 | UCLASS_DRIVER(spi) = { |
| 508 | .id = UCLASS_SPI, |
| 509 | .name = "spi", |
Simon Glass | 9cc36a2 | 2015-01-25 08:27:05 -0700 | [diff] [blame] | 510 | .flags = DM_UC_FLAG_SEQ_ALIAS, |
Faiz Abbas | 280af01 | 2020-09-14 12:11:14 +0530 | [diff] [blame] | 511 | #if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA) |
Simon Glass | 9119548 | 2016-07-05 17:10:10 -0600 | [diff] [blame] | 512 | .post_bind = dm_scan_fdt_dev, |
Simon Glass | 71634f2 | 2016-11-13 14:22:01 -0700 | [diff] [blame] | 513 | #endif |
Simon Glass | d7af6a4 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 514 | .post_probe = spi_post_probe, |
Simon Glass | 440714e | 2015-01-25 08:27:11 -0700 | [diff] [blame] | 515 | .child_pre_probe = spi_child_pre_probe, |
Simon Glass | 41575d8 | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 516 | .per_device_auto = sizeof(struct dm_spi_bus), |
| 517 | .per_child_auto = sizeof(struct spi_slave), |
Simon Glass | 8a8d24b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 518 | .per_child_plat_auto = sizeof(struct dm_spi_slave_plat), |
Simon Glass | 71634f2 | 2016-11-13 14:22:01 -0700 | [diff] [blame] | 519 | #if !CONFIG_IS_ENABLED(OF_PLATDATA) |
Simon Glass | d0cff03 | 2015-01-25 08:27:12 -0700 | [diff] [blame] | 520 | .child_post_bind = spi_child_post_bind, |
Simon Glass | 71634f2 | 2016-11-13 14:22:01 -0700 | [diff] [blame] | 521 | #endif |
Simon Glass | d7af6a4 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 522 | }; |
| 523 | |
| 524 | UCLASS_DRIVER(spi_generic) = { |
| 525 | .id = UCLASS_SPI_GENERIC, |
| 526 | .name = "spi_generic", |
| 527 | }; |
| 528 | |
| 529 | U_BOOT_DRIVER(spi_generic_drv) = { |
| 530 | .name = "spi_generic_drv", |
| 531 | .id = UCLASS_SPI_GENERIC, |
| 532 | }; |