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