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 | |
| 6 | #include <common.h> |
| 7 | #include <dm.h> |
| 8 | #include <errno.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 9 | #include <log.h> |
Simon Glass | dd82d44 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 10 | #include <malloc.h> |
| 11 | #include <spi.h> |
| 12 | #include <dm/device-internal.h> |
| 13 | #include <dm/uclass-internal.h> |
Simon Glass | dd82d44 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 14 | #include <dm/lists.h> |
| 15 | #include <dm/util.h> |
| 16 | |
| 17 | DECLARE_GLOBAL_DATA_PTR; |
| 18 | |
Simon Goldschmidt | a942e1a | 2018-10-30 21:09:48 +0100 | [diff] [blame] | 19 | #define SPI_DEFAULT_SPEED_HZ 100000 |
| 20 | |
Simon Glass | dd82d44 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 21 | static int spi_set_speed_mode(struct udevice *bus, int speed, int mode) |
| 22 | { |
| 23 | struct dm_spi_ops *ops; |
| 24 | int ret; |
| 25 | |
| 26 | ops = spi_get_ops(bus); |
| 27 | if (ops->set_speed) |
| 28 | ret = ops->set_speed(bus, speed); |
| 29 | else |
| 30 | ret = -EINVAL; |
| 31 | if (ret) { |
| 32 | printf("Cannot set speed (err=%d)\n", ret); |
| 33 | return ret; |
| 34 | } |
| 35 | |
| 36 | if (ops->set_mode) |
| 37 | ret = ops->set_mode(bus, mode); |
| 38 | else |
| 39 | ret = -EINVAL; |
| 40 | if (ret) { |
| 41 | printf("Cannot set mode (err=%d)\n", ret); |
| 42 | return ret; |
| 43 | } |
| 44 | |
| 45 | return 0; |
| 46 | } |
| 47 | |
Peng Fan | fdd88a3 | 2016-05-03 10:02:22 +0800 | [diff] [blame] | 48 | int dm_spi_claim_bus(struct udevice *dev) |
Simon Glass | dd82d44 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 49 | { |
Simon Glass | dd82d44 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 50 | struct udevice *bus = dev->parent; |
| 51 | struct dm_spi_ops *ops = spi_get_ops(bus); |
Simon Glass | de0977b | 2015-03-05 12:25:20 -0700 | [diff] [blame] | 52 | struct dm_spi_bus *spi = dev_get_uclass_priv(bus); |
Peng Fan | fdd88a3 | 2016-05-03 10:02:22 +0800 | [diff] [blame] | 53 | struct spi_slave *slave = dev_get_parent_priv(dev); |
Ovidiu Panait | 40dcee1 | 2020-12-14 19:06:50 +0200 | [diff] [blame] | 54 | uint speed, mode; |
Simon Glass | dd82d44 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 55 | |
| 56 | speed = slave->max_hz; |
Ovidiu Panait | 40dcee1 | 2020-12-14 19:06:50 +0200 | [diff] [blame] | 57 | mode = slave->mode; |
| 58 | |
Simon Glass | dd82d44 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 59 | if (spi->max_hz) { |
| 60 | if (speed) |
Ovidiu Panait | 40dcee1 | 2020-12-14 19:06:50 +0200 | [diff] [blame] | 61 | speed = min(speed, spi->max_hz); |
Simon Glass | dd82d44 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 62 | else |
| 63 | speed = spi->max_hz; |
| 64 | } |
| 65 | if (!speed) |
Simon Goldschmidt | a942e1a | 2018-10-30 21:09:48 +0100 | [diff] [blame] | 66 | speed = SPI_DEFAULT_SPEED_HZ; |
Ovidiu Panait | 40dcee1 | 2020-12-14 19:06:50 +0200 | [diff] [blame] | 67 | |
| 68 | if (speed != spi->speed || mode != spi->mode) { |
Mario Six | 2d844dd | 2018-01-15 11:08:41 +0100 | [diff] [blame] | 69 | int ret = spi_set_speed_mode(bus, speed, slave->mode); |
| 70 | |
Simon Glass | b46cb63 | 2015-02-17 15:29:35 -0700 | [diff] [blame] | 71 | if (ret) |
Simon Glass | 00d9990 | 2018-10-01 12:22:24 -0600 | [diff] [blame] | 72 | return log_ret(ret); |
Ovidiu Panait | 40dcee1 | 2020-12-14 19:06:50 +0200 | [diff] [blame] | 73 | |
| 74 | spi->speed = speed; |
| 75 | spi->mode = mode; |
Simon Glass | b46cb63 | 2015-02-17 15:29:35 -0700 | [diff] [blame] | 76 | } |
Simon Glass | dd82d44 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 77 | |
Simon Glass | 00d9990 | 2018-10-01 12:22:24 -0600 | [diff] [blame] | 78 | return log_ret(ops->claim_bus ? ops->claim_bus(dev) : 0); |
Simon Glass | dd82d44 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 79 | } |
| 80 | |
Peng Fan | fdd88a3 | 2016-05-03 10:02:22 +0800 | [diff] [blame] | 81 | void dm_spi_release_bus(struct udevice *dev) |
Simon Glass | dd82d44 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 82 | { |
Simon Glass | dd82d44 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 83 | struct udevice *bus = dev->parent; |
| 84 | struct dm_spi_ops *ops = spi_get_ops(bus); |
| 85 | |
| 86 | if (ops->release_bus) |
Simon Glass | 5c74fba | 2015-04-19 09:05:40 -0600 | [diff] [blame] | 87 | ops->release_bus(dev); |
Simon Glass | dd82d44 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 88 | } |
| 89 | |
Peng Fan | fdd88a3 | 2016-05-03 10:02:22 +0800 | [diff] [blame] | 90 | int dm_spi_xfer(struct udevice *dev, unsigned int bitlen, |
| 91 | const void *dout, void *din, unsigned long flags) |
Simon Glass | dd82d44 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 92 | { |
Simon Glass | dd82d44 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 93 | struct udevice *bus = dev->parent; |
Simon Glass | 2d2e860 | 2019-12-06 21:42:35 -0700 | [diff] [blame] | 94 | struct dm_spi_ops *ops = spi_get_ops(bus); |
Simon Glass | dd82d44 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 95 | |
| 96 | if (bus->uclass->uc_drv->id != UCLASS_SPI) |
| 97 | return -EOPNOTSUPP; |
Simon Glass | 2d2e860 | 2019-12-06 21:42:35 -0700 | [diff] [blame] | 98 | if (!ops->xfer) |
| 99 | return -ENOSYS; |
Simon Glass | dd82d44 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 100 | |
Simon Glass | 2d2e860 | 2019-12-06 21:42:35 -0700 | [diff] [blame] | 101 | return ops->xfer(dev, bitlen, dout, din, flags); |
Simon Glass | dd82d44 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 102 | } |
| 103 | |
Simon Glass | 37ad0fe | 2019-10-20 21:31:47 -0600 | [diff] [blame] | 104 | int dm_spi_get_mmap(struct udevice *dev, ulong *map_basep, uint *map_sizep, |
| 105 | uint *offsetp) |
| 106 | { |
| 107 | struct udevice *bus = dev->parent; |
| 108 | struct dm_spi_ops *ops = spi_get_ops(bus); |
| 109 | |
| 110 | if (bus->uclass->uc_drv->id != UCLASS_SPI) |
| 111 | return -EOPNOTSUPP; |
| 112 | if (!ops->get_mmap) |
| 113 | return -ENOSYS; |
| 114 | |
| 115 | return ops->get_mmap(dev, map_basep, map_sizep, offsetp); |
| 116 | } |
| 117 | |
Peng Fan | fdd88a3 | 2016-05-03 10:02:22 +0800 | [diff] [blame] | 118 | int spi_claim_bus(struct spi_slave *slave) |
| 119 | { |
Simon Glass | 00d9990 | 2018-10-01 12:22:24 -0600 | [diff] [blame] | 120 | return log_ret(dm_spi_claim_bus(slave->dev)); |
Peng Fan | fdd88a3 | 2016-05-03 10:02:22 +0800 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | void spi_release_bus(struct spi_slave *slave) |
| 124 | { |
| 125 | dm_spi_release_bus(slave->dev); |
| 126 | } |
| 127 | |
| 128 | int spi_xfer(struct spi_slave *slave, unsigned int bitlen, |
| 129 | const void *dout, void *din, unsigned long flags) |
| 130 | { |
| 131 | return dm_spi_xfer(slave->dev, bitlen, dout, din, flags); |
| 132 | } |
| 133 | |
Jagan Teki | 7cc71fd | 2019-07-22 17:22:56 +0530 | [diff] [blame] | 134 | int spi_write_then_read(struct spi_slave *slave, const u8 *opcode, |
| 135 | size_t n_opcode, const u8 *txbuf, u8 *rxbuf, |
| 136 | size_t n_buf) |
| 137 | { |
| 138 | unsigned long flags = SPI_XFER_BEGIN; |
| 139 | int ret; |
| 140 | |
| 141 | if (n_buf == 0) |
| 142 | flags |= SPI_XFER_END; |
| 143 | |
| 144 | ret = spi_xfer(slave, n_opcode * 8, opcode, NULL, flags); |
| 145 | if (ret) { |
| 146 | debug("spi: failed to send command (%zu bytes): %d\n", |
| 147 | n_opcode, ret); |
| 148 | } else if (n_buf != 0) { |
| 149 | ret = spi_xfer(slave, n_buf * 8, txbuf, rxbuf, SPI_XFER_END); |
| 150 | if (ret) |
| 151 | debug("spi: failed to transfer %zu bytes of data: %d\n", |
| 152 | n_buf, ret); |
| 153 | } |
| 154 | |
| 155 | return ret; |
| 156 | } |
| 157 | |
Simon Glass | 3fb3339 | 2016-11-13 14:22:01 -0700 | [diff] [blame] | 158 | #if !CONFIG_IS_ENABLED(OF_PLATDATA) |
Simon Glass | d941aaf | 2015-06-23 15:39:05 -0600 | [diff] [blame] | 159 | static int spi_child_post_bind(struct udevice *dev) |
Simon Glass | dd82d44 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 160 | { |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 161 | struct dm_spi_slave_plat *plat = dev_get_parent_plat(dev); |
Simon Glass | dd82d44 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 162 | |
Simon Glass | 4b28058 | 2017-05-18 20:09:54 -0600 | [diff] [blame] | 163 | if (!dev_of_valid(dev)) |
Simon Glass | 5d2ee05 | 2015-01-25 08:27:12 -0700 | [diff] [blame] | 164 | return 0; |
| 165 | |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 166 | return spi_slave_of_to_plat(dev, plat); |
Simon Glass | 5d2ee05 | 2015-01-25 08:27:12 -0700 | [diff] [blame] | 167 | } |
Simon Glass | 3fb3339 | 2016-11-13 14:22:01 -0700 | [diff] [blame] | 168 | #endif |
Simon Glass | 5d2ee05 | 2015-01-25 08:27:12 -0700 | [diff] [blame] | 169 | |
Simon Glass | d941aaf | 2015-06-23 15:39:05 -0600 | [diff] [blame] | 170 | static int spi_post_probe(struct udevice *bus) |
Simon Glass | 5d2ee05 | 2015-01-25 08:27:12 -0700 | [diff] [blame] | 171 | { |
Simon Glass | 3fb3339 | 2016-11-13 14:22:01 -0700 | [diff] [blame] | 172 | #if !CONFIG_IS_ENABLED(OF_PLATDATA) |
Simon Glass | de0977b | 2015-03-05 12:25:20 -0700 | [diff] [blame] | 173 | struct dm_spi_bus *spi = dev_get_uclass_priv(bus); |
Simon Glass | 5d2ee05 | 2015-01-25 08:27:12 -0700 | [diff] [blame] | 174 | |
Simon Glass | 4b28058 | 2017-05-18 20:09:54 -0600 | [diff] [blame] | 175 | spi->max_hz = dev_read_u32_default(bus, "spi-max-frequency", 0); |
Simon Glass | 3fb3339 | 2016-11-13 14:22:01 -0700 | [diff] [blame] | 176 | #endif |
Michal Simek | 28d7e4e | 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 | 1d89d8c | 2019-09-17 00:11:02 -0600 | [diff] [blame] | 179 | static int reloc_done; |
Michal Simek | 28d7e4e | 2015-10-27 13:36:42 +0100 | [diff] [blame] | 180 | |
Ashok Reddy Soma | 1d89d8c | 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 | 28d7e4e | 2015-10-27 13:36:42 +0100 | [diff] [blame] | 198 | #endif |
| 199 | |
Simon Glass | dd82d44 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 200 | return 0; |
| 201 | } |
| 202 | |
Simon Glass | d941aaf | 2015-06-23 15:39:05 -0600 | [diff] [blame] | 203 | static int spi_child_pre_probe(struct udevice *dev) |
Simon Glass | 82c2f50 | 2015-01-25 08:27:11 -0700 | [diff] [blame] | 204 | { |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 205 | struct dm_spi_slave_plat *plat = dev_get_parent_plat(dev); |
Simon Glass | de44acf | 2015-09-28 23:32:01 -0600 | [diff] [blame] | 206 | struct spi_slave *slave = dev_get_parent_priv(dev); |
Simon Glass | 82c2f50 | 2015-01-25 08:27:11 -0700 | [diff] [blame] | 207 | |
Simon Glass | 5d2ee05 | 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 | 82c2f50 | 2015-01-25 08:27:11 -0700 | [diff] [blame] | 215 | slave->dev = dev; |
| 216 | |
Simon Glass | 5d2ee05 | 2015-01-25 08:27:12 -0700 | [diff] [blame] | 217 | slave->max_hz = plat->max_hz; |
| 218 | slave->mode = plat->mode; |
Christophe Ricard | fb0c53e | 2016-01-17 11:56:48 +0100 | [diff] [blame] | 219 | slave->wordlen = SPI_DEFAULT_WORDLEN; |
Simon Glass | 5d2ee05 | 2015-01-25 08:27:12 -0700 | [diff] [blame] | 220 | |
Simon Glass | 82c2f50 | 2015-01-25 08:27:11 -0700 | [diff] [blame] | 221 | return 0; |
| 222 | } |
| 223 | |
Simon Glass | dd82d44 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 224 | int spi_chip_select(struct udevice *dev) |
| 225 | { |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 226 | struct dm_spi_slave_plat *plat = dev_get_parent_plat(dev); |
Simon Glass | dd82d44 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 227 | |
Simon Glass | 5d2ee05 | 2015-01-25 08:27:12 -0700 | [diff] [blame] | 228 | return plat ? plat->cs : -ENOENT; |
Simon Glass | dd82d44 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 229 | } |
| 230 | |
Simon Glass | 5ef36f2 | 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 | dd82d44 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 232 | { |
Bin Meng | 9741e73 | 2019-09-09 06:00:02 -0700 | [diff] [blame] | 233 | struct dm_spi_ops *ops; |
| 234 | struct spi_cs_info info; |
Simon Glass | dd82d44 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 235 | struct udevice *dev; |
Bin Meng | 9741e73 | 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) { |
| 256 | printf("Invalid cs %d (err=%d)\n", cs, ret); |
| 257 | return ret; |
| 258 | } |
Simon Glass | dd82d44 | 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 | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 262 | struct dm_spi_slave_plat *plat; |
Simon Glass | dd82d44 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 263 | |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 264 | plat = dev_get_parent_plat(dev); |
Simon Glass | 5d2ee05 | 2015-01-25 08:27:12 -0700 | [diff] [blame] | 265 | debug("%s: plat=%p, cs=%d\n", __func__, plat, plat->cs); |
| 266 | if (plat->cs == cs) { |
Simon Glass | dd82d44 | 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 | |
Simon Glass | 07e1338 | 2020-12-16 21:20:29 -0700 | [diff] [blame] | 281 | ret = uclass_find_device_by_seq(UCLASS_SPI, busnum, &bus); |
Simon Glass | dd82d44 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 282 | if (ret) { |
| 283 | debug("%s: No bus %d\n", __func__, busnum); |
| 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 | dd82d44 | 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 | 9741e73 | 2019-09-09 06:00:02 -0700 | [diff] [blame] | 301 | return ret == -ENODEV ? 0 : ret; |
Simon Glass | dd82d44 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 302 | } |
| 303 | |
Simon Glass | dd82d44 | 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 | |
Simon Glass | 07e1338 | 2020-12-16 21:20:29 -0700 | [diff] [blame] | 310 | ret = uclass_find_device_by_seq(UCLASS_SPI, busnum, &bus); |
Simon Glass | dd82d44 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 311 | if (ret) { |
| 312 | debug("%s: No bus %d\n", __func__, busnum); |
| 313 | return ret; |
| 314 | } |
| 315 | ret = spi_find_chip_select(bus, cs, &dev); |
| 316 | if (ret) { |
| 317 | debug("%s: No cs %d\n", __func__, cs); |
| 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; |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 331 | struct dm_spi_slave_plat *plat; |
Ovidiu Panait | 40dcee1 | 2020-12-14 19:06:50 +0200 | [diff] [blame] | 332 | struct dm_spi_bus *bus_data; |
Marcin Wojtas | 70d8f98 | 2019-11-21 05:38:47 +0100 | [diff] [blame] | 333 | struct spi_slave *slave; |
Simon Glass | dd82d44 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 334 | bool created = false; |
| 335 | int ret; |
| 336 | |
Thomas Fitzsimmons | 59c90f3 | 2019-09-06 07:51:19 -0400 | [diff] [blame] | 337 | #if CONFIG_IS_ENABLED(OF_PLATDATA) |
Simon Glass | 3fb3339 | 2016-11-13 14:22:01 -0700 | [diff] [blame] | 338 | ret = uclass_first_device_err(UCLASS_SPI, &bus); |
| 339 | #else |
Simon Glass | dd82d44 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 340 | ret = uclass_get_device_by_seq(UCLASS_SPI, busnum, &bus); |
Simon Glass | 3fb3339 | 2016-11-13 14:22:01 -0700 | [diff] [blame] | 341 | #endif |
Simon Glass | dd82d44 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 342 | if (ret) { |
| 343 | printf("Invalid bus %d (err=%d)\n", busnum, ret); |
| 344 | return ret; |
| 345 | } |
| 346 | ret = spi_find_chip_select(bus, cs, &dev); |
| 347 | |
| 348 | /* |
| 349 | * If there is no such device, create one automatically. This means |
| 350 | * that we don't need a device tree node or platform data for the |
| 351 | * SPI flash chip - we will bind to the correct driver. |
| 352 | */ |
| 353 | if (ret == -ENODEV && drv_name) { |
| 354 | debug("%s: Binding new device '%s', busnum=%d, cs=%d, driver=%s\n", |
| 355 | __func__, dev_name, busnum, cs, drv_name); |
Simon Glass | d8a21f6 | 2014-11-11 10:46:23 -0700 | [diff] [blame] | 356 | ret = device_bind_driver(bus, drv_name, dev_name, &dev); |
Simon Glass | f0307e1 | 2016-11-13 14:22:05 -0700 | [diff] [blame] | 357 | if (ret) { |
| 358 | debug("%s: Unable to bind driver (ret=%d)\n", __func__, |
| 359 | ret); |
Simon Glass | dd82d44 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 360 | return ret; |
Simon Glass | f0307e1 | 2016-11-13 14:22:05 -0700 | [diff] [blame] | 361 | } |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 362 | plat = dev_get_parent_plat(dev); |
Simon Glass | 5d2ee05 | 2015-01-25 08:27:12 -0700 | [diff] [blame] | 363 | plat->cs = cs; |
Simon Goldschmidt | a942e1a | 2018-10-30 21:09:48 +0100 | [diff] [blame] | 364 | if (speed) { |
| 365 | plat->max_hz = speed; |
| 366 | } else { |
| 367 | printf("Warning: SPI speed fallback to %u kHz\n", |
| 368 | SPI_DEFAULT_SPEED_HZ / 1000); |
| 369 | plat->max_hz = SPI_DEFAULT_SPEED_HZ; |
| 370 | } |
Simon Glass | 5d2ee05 | 2015-01-25 08:27:12 -0700 | [diff] [blame] | 371 | plat->mode = mode; |
Simon Glass | dd82d44 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 372 | created = true; |
| 373 | } else if (ret) { |
| 374 | printf("Invalid chip select %d:%d (err=%d)\n", busnum, cs, |
| 375 | ret); |
| 376 | return ret; |
| 377 | } |
| 378 | |
| 379 | if (!device_active(dev)) { |
Simon Glass | 5d2ee05 | 2015-01-25 08:27:12 -0700 | [diff] [blame] | 380 | struct spi_slave *slave; |
Simon Glass | dd82d44 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 381 | |
Simon Glass | 5d2ee05 | 2015-01-25 08:27:12 -0700 | [diff] [blame] | 382 | ret = device_probe(dev); |
Simon Glass | dd82d44 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 383 | if (ret) |
| 384 | goto err; |
Simon Glass | de44acf | 2015-09-28 23:32:01 -0600 | [diff] [blame] | 385 | slave = dev_get_parent_priv(dev); |
Simon Glass | dd82d44 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 386 | slave->dev = dev; |
Simon Glass | dd82d44 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 387 | } |
| 388 | |
Marcin Wojtas | 70d8f98 | 2019-11-21 05:38:47 +0100 | [diff] [blame] | 389 | slave = dev_get_parent_priv(dev); |
Ovidiu Panait | 40dcee1 | 2020-12-14 19:06:50 +0200 | [diff] [blame] | 390 | bus_data = dev_get_uclass_priv(bus); |
Patrick Delaunay | fa19c65 | 2019-02-27 15:36:44 +0100 | [diff] [blame] | 391 | |
Marcin Wojtas | 70d8f98 | 2019-11-21 05:38:47 +0100 | [diff] [blame] | 392 | /* |
| 393 | * In case the operation speed is not yet established by |
| 394 | * dm_spi_claim_bus() ensure the bus is configured properly. |
| 395 | */ |
Ovidiu Panait | 40dcee1 | 2020-12-14 19:06:50 +0200 | [diff] [blame] | 396 | if (!bus_data->speed) { |
Marcin Wojtas | 70d8f98 | 2019-11-21 05:38:47 +0100 | [diff] [blame] | 397 | ret = spi_claim_bus(slave); |
| 398 | if (ret) |
| 399 | goto err; |
Vignesh R | ae56979 | 2016-07-06 10:04:28 +0530 | [diff] [blame] | 400 | } |
Simon Glass | dd82d44 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 401 | |
| 402 | *busp = bus; |
Marcin Wojtas | 70d8f98 | 2019-11-21 05:38:47 +0100 | [diff] [blame] | 403 | *devp = slave; |
Simon Glass | dd82d44 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 404 | debug("%s: bus=%p, slave=%p\n", __func__, bus, *devp); |
| 405 | |
| 406 | return 0; |
| 407 | |
| 408 | err: |
Anatolij Gustschin | 264359e | 2016-04-21 09:28:02 +0200 | [diff] [blame] | 409 | debug("%s: Error path, created=%d, device '%s'\n", __func__, |
Simon Glass | 5d2ee05 | 2015-01-25 08:27:12 -0700 | [diff] [blame] | 410 | created, dev->name); |
Simon Glass | dd82d44 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 411 | if (created) { |
Stefan Roese | 80b5bc9 | 2017-03-20 12:51:48 +0100 | [diff] [blame] | 412 | device_remove(dev, DM_REMOVE_NORMAL); |
Simon Glass | dd82d44 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 413 | device_unbind(dev); |
| 414 | } |
| 415 | |
| 416 | return ret; |
| 417 | } |
| 418 | |
| 419 | /* Compatibility function - to be removed */ |
Simon Glass | dd82d44 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 420 | struct spi_slave *spi_setup_slave(unsigned int busnum, unsigned int cs, |
| 421 | unsigned int speed, unsigned int mode) |
| 422 | { |
| 423 | struct spi_slave *slave; |
| 424 | struct udevice *dev; |
| 425 | int ret; |
| 426 | |
| 427 | ret = spi_get_bus_and_cs(busnum, cs, speed, mode, NULL, 0, &dev, |
Simon Glass | 4b28058 | 2017-05-18 20:09:54 -0600 | [diff] [blame] | 428 | &slave); |
Simon Glass | dd82d44 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 429 | if (ret) |
| 430 | return NULL; |
| 431 | |
| 432 | return slave; |
| 433 | } |
| 434 | |
| 435 | void spi_free_slave(struct spi_slave *slave) |
| 436 | { |
Stefan Roese | 80b5bc9 | 2017-03-20 12:51:48 +0100 | [diff] [blame] | 437 | device_remove(slave->dev, DM_REMOVE_NORMAL); |
Simon Glass | dd82d44 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 438 | } |
| 439 | |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 440 | 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] | 441 | { |
Jagan Teki | 96536b1 | 2016-08-08 17:12:12 +0530 | [diff] [blame] | 442 | int mode = 0; |
Mugunthan V N | 4b0f40c | 2015-12-23 20:39:37 +0530 | [diff] [blame] | 443 | int value; |
Simon Glass | dd82d44 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 444 | |
Simon Glass | 4b28058 | 2017-05-18 20:09:54 -0600 | [diff] [blame] | 445 | plat->cs = dev_read_u32_default(dev, "reg", -1); |
Simon Goldschmidt | a942e1a | 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 | 4b28058 | 2017-05-18 20:09:54 -0600 | [diff] [blame] | 448 | if (dev_read_bool(dev, "spi-cpol")) |
Simon Glass | dd82d44 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 449 | mode |= SPI_CPOL; |
Simon Glass | 4b28058 | 2017-05-18 20:09:54 -0600 | [diff] [blame] | 450 | if (dev_read_bool(dev, "spi-cpha")) |
Simon Glass | dd82d44 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 451 | mode |= SPI_CPHA; |
Simon Glass | 4b28058 | 2017-05-18 20:09:54 -0600 | [diff] [blame] | 452 | if (dev_read_bool(dev, "spi-cs-high")) |
Simon Glass | dd82d44 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 453 | mode |= SPI_CS_HIGH; |
Simon Glass | 4b28058 | 2017-05-18 20:09:54 -0600 | [diff] [blame] | 454 | if (dev_read_bool(dev, "spi-3wire")) |
Jagan Teki | d3868fd | 2015-12-03 22:19:05 +0530 | [diff] [blame] | 455 | mode |= SPI_3WIRE; |
Simon Glass | 4b28058 | 2017-05-18 20:09:54 -0600 | [diff] [blame] | 456 | if (dev_read_bool(dev, "spi-half-duplex")) |
Simon Glass | dd82d44 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 457 | mode |= SPI_PREAMBLE; |
Mugunthan V N | 4b0f40c | 2015-12-23 20:39:37 +0530 | [diff] [blame] | 458 | |
| 459 | /* Device DUAL/QUAD mode */ |
Simon Glass | 4b28058 | 2017-05-18 20:09:54 -0600 | [diff] [blame] | 460 | value = dev_read_u32_default(dev, "spi-tx-bus-width", 1); |
Mugunthan V N | 4b0f40c | 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 | c063ee3 | 2019-12-05 15:46:05 +0530 | [diff] [blame] | 470 | case 8: |
| 471 | mode |= SPI_TX_OCTAL; |
| 472 | break; |
Mugunthan V N | 4b0f40c | 2015-12-23 20:39:37 +0530 | [diff] [blame] | 473 | default: |
Simon Glass | 6f5bed0 | 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 | 4b0f40c | 2015-12-23 20:39:37 +0530 | [diff] [blame] | 475 | break; |
| 476 | } |
| 477 | |
Simon Glass | 4b28058 | 2017-05-18 20:09:54 -0600 | [diff] [blame] | 478 | value = dev_read_u32_default(dev, "spi-rx-bus-width", 1); |
Mugunthan V N | 4b0f40c | 2015-12-23 20:39:37 +0530 | [diff] [blame] | 479 | switch (value) { |
| 480 | case 1: |
| 481 | break; |
| 482 | case 2: |
Jagan Teki | 96536b1 | 2016-08-08 17:12:12 +0530 | [diff] [blame] | 483 | mode |= SPI_RX_DUAL; |
Mugunthan V N | 4b0f40c | 2015-12-23 20:39:37 +0530 | [diff] [blame] | 484 | break; |
| 485 | case 4: |
Jagan Teki | 96536b1 | 2016-08-08 17:12:12 +0530 | [diff] [blame] | 486 | mode |= SPI_RX_QUAD; |
Mugunthan V N | 4b0f40c | 2015-12-23 20:39:37 +0530 | [diff] [blame] | 487 | break; |
Vignesh Raghavendra | c063ee3 | 2019-12-05 15:46:05 +0530 | [diff] [blame] | 488 | case 8: |
| 489 | mode |= SPI_RX_OCTAL; |
| 490 | break; |
Mugunthan V N | 4b0f40c | 2015-12-23 20:39:37 +0530 | [diff] [blame] | 491 | default: |
Simon Glass | 6f5bed0 | 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 | 4b0f40c | 2015-12-23 20:39:37 +0530 | [diff] [blame] | 493 | break; |
| 494 | } |
| 495 | |
Jagan Teki | 96536b1 | 2016-08-08 17:12:12 +0530 | [diff] [blame] | 496 | plat->mode = mode; |
Mugunthan V N | 4b0f40c | 2015-12-23 20:39:37 +0530 | [diff] [blame] | 497 | |
Simon Glass | dd82d44 | 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 | 0ccb097 | 2015-01-25 08:27:05 -0700 | [diff] [blame] | 504 | .flags = DM_UC_FLAG_SEQ_ALIAS, |
Faiz Abbas | 8598c34 | 2020-09-14 12:11:14 +0530 | [diff] [blame] | 505 | #if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA) |
Simon Glass | 1823034 | 2016-07-05 17:10:10 -0600 | [diff] [blame] | 506 | .post_bind = dm_scan_fdt_dev, |
Simon Glass | 3fb3339 | 2016-11-13 14:22:01 -0700 | [diff] [blame] | 507 | #endif |
Simon Glass | dd82d44 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 508 | .post_probe = spi_post_probe, |
Simon Glass | 82c2f50 | 2015-01-25 08:27:11 -0700 | [diff] [blame] | 509 | .child_pre_probe = spi_child_pre_probe, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 510 | .per_device_auto = sizeof(struct dm_spi_bus), |
| 511 | .per_child_auto = sizeof(struct spi_slave), |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 512 | .per_child_plat_auto = sizeof(struct dm_spi_slave_plat), |
Simon Glass | 3fb3339 | 2016-11-13 14:22:01 -0700 | [diff] [blame] | 513 | #if !CONFIG_IS_ENABLED(OF_PLATDATA) |
Simon Glass | 5d2ee05 | 2015-01-25 08:27:12 -0700 | [diff] [blame] | 514 | .child_post_bind = spi_child_post_bind, |
Simon Glass | 3fb3339 | 2016-11-13 14:22:01 -0700 | [diff] [blame] | 515 | #endif |
Simon Glass | dd82d44 | 2014-10-13 23:41:52 -0600 | [diff] [blame] | 516 | }; |
| 517 | |
| 518 | UCLASS_DRIVER(spi_generic) = { |
| 519 | .id = UCLASS_SPI_GENERIC, |
| 520 | .name = "spi_generic", |
| 521 | }; |
| 522 | |
| 523 | U_BOOT_DRIVER(spi_generic_drv) = { |
| 524 | .name = "spi_generic_drv", |
| 525 | .id = UCLASS_SPI_GENERIC, |
| 526 | }; |