1d7af6a48SSimon Glass /* 2d7af6a48SSimon Glass * Copyright (c) 2014 Google, Inc 3d7af6a48SSimon Glass * 4d7af6a48SSimon Glass * SPDX-License-Identifier: GPL-2.0+ 5d7af6a48SSimon Glass */ 6d7af6a48SSimon Glass 7d7af6a48SSimon Glass #include <common.h> 8d7af6a48SSimon Glass #include <dm.h> 9d7af6a48SSimon Glass #include <errno.h> 10d7af6a48SSimon Glass #include <malloc.h> 11d7af6a48SSimon Glass #include <spi.h> 12d7af6a48SSimon Glass #include <dm/device-internal.h> 13d7af6a48SSimon Glass #include <dm/uclass-internal.h> 14d7af6a48SSimon Glass #include <dm/lists.h> 15d7af6a48SSimon Glass #include <dm/util.h> 16d7af6a48SSimon Glass 17d7af6a48SSimon Glass DECLARE_GLOBAL_DATA_PTR; 18d7af6a48SSimon Glass 19448ceaf5SSimon Goldschmidt #define SPI_DEFAULT_SPEED_HZ 100000 20448ceaf5SSimon Goldschmidt 21d7af6a48SSimon Glass static int spi_set_speed_mode(struct udevice *bus, int speed, int mode) 22d7af6a48SSimon Glass { 23d7af6a48SSimon Glass struct dm_spi_ops *ops; 24d7af6a48SSimon Glass int ret; 25d7af6a48SSimon Glass 26d7af6a48SSimon Glass ops = spi_get_ops(bus); 27d7af6a48SSimon Glass if (ops->set_speed) 28d7af6a48SSimon Glass ret = ops->set_speed(bus, speed); 29d7af6a48SSimon Glass else 30d7af6a48SSimon Glass ret = -EINVAL; 31d7af6a48SSimon Glass if (ret) { 32d7af6a48SSimon Glass printf("Cannot set speed (err=%d)\n", ret); 33d7af6a48SSimon Glass return ret; 34d7af6a48SSimon Glass } 35d7af6a48SSimon Glass 36d7af6a48SSimon Glass if (ops->set_mode) 37d7af6a48SSimon Glass ret = ops->set_mode(bus, mode); 38d7af6a48SSimon Glass else 39d7af6a48SSimon Glass ret = -EINVAL; 40d7af6a48SSimon Glass if (ret) { 41d7af6a48SSimon Glass printf("Cannot set mode (err=%d)\n", ret); 42d7af6a48SSimon Glass return ret; 43d7af6a48SSimon Glass } 44d7af6a48SSimon Glass 45d7af6a48SSimon Glass return 0; 46d7af6a48SSimon Glass } 47d7af6a48SSimon Glass 487a3eff4cSPeng Fan int dm_spi_claim_bus(struct udevice *dev) 49d7af6a48SSimon Glass { 50d7af6a48SSimon Glass struct udevice *bus = dev->parent; 51d7af6a48SSimon Glass struct dm_spi_ops *ops = spi_get_ops(bus); 52e564f054SSimon Glass struct dm_spi_bus *spi = dev_get_uclass_priv(bus); 537a3eff4cSPeng Fan struct spi_slave *slave = dev_get_parent_priv(dev); 54d7af6a48SSimon Glass int speed; 55d7af6a48SSimon Glass 56d7af6a48SSimon Glass speed = slave->max_hz; 57d7af6a48SSimon Glass if (spi->max_hz) { 58d7af6a48SSimon Glass if (speed) 59b4141195SMasahiro Yamada speed = min(speed, (int)spi->max_hz); 60d7af6a48SSimon Glass else 61d7af6a48SSimon Glass speed = spi->max_hz; 62d7af6a48SSimon Glass } 63d7af6a48SSimon Glass if (!speed) 64448ceaf5SSimon Goldschmidt speed = SPI_DEFAULT_SPEED_HZ; 6560e2809aSSimon Glass if (speed != slave->speed) { 668849a3beSMario Six int ret = spi_set_speed_mode(bus, speed, slave->mode); 678849a3beSMario Six 68d7af6a48SSimon Glass if (ret) 69519ddfffSSimon Glass return log_ret(ret); 7060e2809aSSimon Glass slave->speed = speed; 7160e2809aSSimon Glass } 72d7af6a48SSimon Glass 73519ddfffSSimon Glass return log_ret(ops->claim_bus ? ops->claim_bus(dev) : 0); 74d7af6a48SSimon Glass } 75d7af6a48SSimon Glass 767a3eff4cSPeng Fan void dm_spi_release_bus(struct udevice *dev) 77d7af6a48SSimon Glass { 78d7af6a48SSimon Glass struct udevice *bus = dev->parent; 79d7af6a48SSimon Glass struct dm_spi_ops *ops = spi_get_ops(bus); 80d7af6a48SSimon Glass 81d7af6a48SSimon Glass if (ops->release_bus) 829694b724SSimon Glass ops->release_bus(dev); 83d7af6a48SSimon Glass } 84d7af6a48SSimon Glass 857a3eff4cSPeng Fan int dm_spi_xfer(struct udevice *dev, unsigned int bitlen, 86d7af6a48SSimon Glass const void *dout, void *din, unsigned long flags) 87d7af6a48SSimon Glass { 88d7af6a48SSimon Glass struct udevice *bus = dev->parent; 89f75b9e50SSimon Glass struct dm_spi_ops *ops = spi_get_ops(bus); 90d7af6a48SSimon Glass 91d7af6a48SSimon Glass if (bus->uclass->uc_drv->id != UCLASS_SPI) 92d7af6a48SSimon Glass return -EOPNOTSUPP; 93f75b9e50SSimon Glass if (!ops->xfer) 94f75b9e50SSimon Glass return -ENOSYS; 95d7af6a48SSimon Glass 96f75b9e50SSimon Glass return ops->xfer(dev, bitlen, dout, din, flags); 97d7af6a48SSimon Glass } 98d7af6a48SSimon Glass 997ac3b0edSSimon Glass int dm_spi_get_mmap(struct udevice *dev, ulong *map_basep, uint *map_sizep, 1007ac3b0edSSimon Glass uint *offsetp) 1017ac3b0edSSimon Glass { 1027ac3b0edSSimon Glass struct udevice *bus = dev->parent; 1037ac3b0edSSimon Glass struct dm_spi_ops *ops = spi_get_ops(bus); 1047ac3b0edSSimon Glass 1057ac3b0edSSimon Glass if (bus->uclass->uc_drv->id != UCLASS_SPI) 1067ac3b0edSSimon Glass return -EOPNOTSUPP; 1077ac3b0edSSimon Glass if (!ops->get_mmap) 1087ac3b0edSSimon Glass return -ENOSYS; 1097ac3b0edSSimon Glass 1107ac3b0edSSimon Glass return ops->get_mmap(dev, map_basep, map_sizep, offsetp); 1117ac3b0edSSimon Glass } 1127ac3b0edSSimon Glass 1137a3eff4cSPeng Fan int spi_claim_bus(struct spi_slave *slave) 1147a3eff4cSPeng Fan { 115519ddfffSSimon Glass return log_ret(dm_spi_claim_bus(slave->dev)); 1167a3eff4cSPeng Fan } 1177a3eff4cSPeng Fan 1187a3eff4cSPeng Fan void spi_release_bus(struct spi_slave *slave) 1197a3eff4cSPeng Fan { 1207a3eff4cSPeng Fan dm_spi_release_bus(slave->dev); 1217a3eff4cSPeng Fan } 1227a3eff4cSPeng Fan 1237a3eff4cSPeng Fan int spi_xfer(struct spi_slave *slave, unsigned int bitlen, 1247a3eff4cSPeng Fan const void *dout, void *din, unsigned long flags) 1257a3eff4cSPeng Fan { 1267a3eff4cSPeng Fan return dm_spi_xfer(slave->dev, bitlen, dout, din, flags); 1277a3eff4cSPeng Fan } 1287a3eff4cSPeng Fan 129a3171fa3SJagan Teki int spi_write_then_read(struct spi_slave *slave, const u8 *opcode, 130a3171fa3SJagan Teki size_t n_opcode, const u8 *txbuf, u8 *rxbuf, 131a3171fa3SJagan Teki size_t n_buf) 132a3171fa3SJagan Teki { 133a3171fa3SJagan Teki unsigned long flags = SPI_XFER_BEGIN; 134a3171fa3SJagan Teki int ret; 135a3171fa3SJagan Teki 136a3171fa3SJagan Teki if (n_buf == 0) 137a3171fa3SJagan Teki flags |= SPI_XFER_END; 138a3171fa3SJagan Teki 139a3171fa3SJagan Teki ret = spi_xfer(slave, n_opcode * 8, opcode, NULL, flags); 140a3171fa3SJagan Teki if (ret) { 141a3171fa3SJagan Teki debug("spi: failed to send command (%zu bytes): %d\n", 142a3171fa3SJagan Teki n_opcode, ret); 143a3171fa3SJagan Teki } else if (n_buf != 0) { 144a3171fa3SJagan Teki ret = spi_xfer(slave, n_buf * 8, txbuf, rxbuf, SPI_XFER_END); 145a3171fa3SJagan Teki if (ret) 146a3171fa3SJagan Teki debug("spi: failed to transfer %zu bytes of data: %d\n", 147a3171fa3SJagan Teki n_buf, ret); 148a3171fa3SJagan Teki } 149a3171fa3SJagan Teki 150a3171fa3SJagan Teki return ret; 151a3171fa3SJagan Teki } 152a3171fa3SJagan Teki 15371634f28SSimon Glass #if !CONFIG_IS_ENABLED(OF_PLATDATA) 1546f849c30SSimon Glass static int spi_child_post_bind(struct udevice *dev) 155d7af6a48SSimon Glass { 156d0cff03eSSimon Glass struct dm_spi_slave_platdata *plat = dev_get_parent_platdata(dev); 157d7af6a48SSimon Glass 158279e26f5SSimon Glass if (!dev_of_valid(dev)) 159d0cff03eSSimon Glass return 0; 160d0cff03eSSimon Glass 161279e26f5SSimon Glass return spi_slave_ofdata_to_platdata(dev, plat); 162d0cff03eSSimon Glass } 16371634f28SSimon Glass #endif 164d0cff03eSSimon Glass 1656f849c30SSimon Glass static int spi_post_probe(struct udevice *bus) 166d0cff03eSSimon Glass { 16771634f28SSimon Glass #if !CONFIG_IS_ENABLED(OF_PLATDATA) 168e564f054SSimon Glass struct dm_spi_bus *spi = dev_get_uclass_priv(bus); 169d0cff03eSSimon Glass 170279e26f5SSimon Glass spi->max_hz = dev_read_u32_default(bus, "spi-max-frequency", 0); 17171634f28SSimon Glass #endif 172281f1566SMichal Simek #if defined(CONFIG_NEEDS_MANUAL_RELOC) 173281f1566SMichal Simek struct dm_spi_ops *ops = spi_get_ops(bus); 1742c7db7c3SAshok Reddy Soma static int reloc_done; 175281f1566SMichal Simek 1762c7db7c3SAshok Reddy Soma if (!reloc_done) { 177281f1566SMichal Simek if (ops->claim_bus) 178281f1566SMichal Simek ops->claim_bus += gd->reloc_off; 179281f1566SMichal Simek if (ops->release_bus) 180281f1566SMichal Simek ops->release_bus += gd->reloc_off; 181281f1566SMichal Simek if (ops->set_wordlen) 182281f1566SMichal Simek ops->set_wordlen += gd->reloc_off; 183281f1566SMichal Simek if (ops->xfer) 184281f1566SMichal Simek ops->xfer += gd->reloc_off; 185281f1566SMichal Simek if (ops->set_speed) 186281f1566SMichal Simek ops->set_speed += gd->reloc_off; 187281f1566SMichal Simek if (ops->set_mode) 188281f1566SMichal Simek ops->set_mode += gd->reloc_off; 189281f1566SMichal Simek if (ops->cs_info) 190281f1566SMichal Simek ops->cs_info += gd->reloc_off; 1912c7db7c3SAshok Reddy Soma reloc_done++; 1922c7db7c3SAshok Reddy Soma } 193281f1566SMichal Simek #endif 194281f1566SMichal Simek 195d7af6a48SSimon Glass return 0; 196d7af6a48SSimon Glass } 197d7af6a48SSimon Glass 1986f849c30SSimon Glass static int spi_child_pre_probe(struct udevice *dev) 199440714eeSSimon Glass { 200d0cff03eSSimon Glass struct dm_spi_slave_platdata *plat = dev_get_parent_platdata(dev); 201bcbe3d15SSimon Glass struct spi_slave *slave = dev_get_parent_priv(dev); 202440714eeSSimon Glass 203d0cff03eSSimon Glass /* 204d0cff03eSSimon Glass * This is needed because we pass struct spi_slave around the place 205d0cff03eSSimon Glass * instead slave->dev (a struct udevice). So we have to have some 206d0cff03eSSimon Glass * way to access the slave udevice given struct spi_slave. Once we 207d0cff03eSSimon Glass * change the SPI API to use udevice instead of spi_slave, we can 208d0cff03eSSimon Glass * drop this. 209d0cff03eSSimon Glass */ 210440714eeSSimon Glass slave->dev = dev; 211440714eeSSimon Glass 212d0cff03eSSimon Glass slave->max_hz = plat->max_hz; 213d0cff03eSSimon Glass slave->mode = plat->mode; 214674f3609SChristophe Ricard slave->wordlen = SPI_DEFAULT_WORDLEN; 215d0cff03eSSimon Glass 216440714eeSSimon Glass return 0; 217440714eeSSimon Glass } 218440714eeSSimon Glass 219d7af6a48SSimon Glass int spi_chip_select(struct udevice *dev) 220d7af6a48SSimon Glass { 221d0cff03eSSimon Glass struct dm_spi_slave_platdata *plat = dev_get_parent_platdata(dev); 222d7af6a48SSimon Glass 223d0cff03eSSimon Glass return plat ? plat->cs : -ENOENT; 224d7af6a48SSimon Glass } 225d7af6a48SSimon Glass 226ff56bba2SSimon Glass int spi_find_chip_select(struct udevice *bus, int cs, struct udevice **devp) 227d7af6a48SSimon Glass { 2285a1c4376SBin Meng struct dm_spi_ops *ops; 2295a1c4376SBin Meng struct spi_cs_info info; 230d7af6a48SSimon Glass struct udevice *dev; 2315a1c4376SBin Meng int ret; 2325a1c4376SBin Meng 2335a1c4376SBin Meng /* 2345a1c4376SBin Meng * Ask the driver. For the moment we don't have CS info. 2355a1c4376SBin Meng * When we do we could provide the driver with a helper function 2365a1c4376SBin Meng * to figure out what chip selects are valid, or just handle the 2375a1c4376SBin Meng * request. 2385a1c4376SBin Meng */ 2395a1c4376SBin Meng ops = spi_get_ops(bus); 2405a1c4376SBin Meng if (ops->cs_info) { 2415a1c4376SBin Meng ret = ops->cs_info(bus, cs, &info); 2425a1c4376SBin Meng } else { 2435a1c4376SBin Meng /* 2445a1c4376SBin Meng * We could assume there is at least one valid chip select. 2455a1c4376SBin Meng * The driver didn't care enough to tell us. 2465a1c4376SBin Meng */ 2475a1c4376SBin Meng ret = 0; 2485a1c4376SBin Meng } 2495a1c4376SBin Meng 2505a1c4376SBin Meng if (ret) { 2515a1c4376SBin Meng printf("Invalid cs %d (err=%d)\n", cs, ret); 2525a1c4376SBin Meng return ret; 2535a1c4376SBin Meng } 254d7af6a48SSimon Glass 255d7af6a48SSimon Glass for (device_find_first_child(bus, &dev); dev; 256d7af6a48SSimon Glass device_find_next_child(&dev)) { 257d0cff03eSSimon Glass struct dm_spi_slave_platdata *plat; 258d7af6a48SSimon Glass 259d0cff03eSSimon Glass plat = dev_get_parent_platdata(dev); 260d0cff03eSSimon Glass debug("%s: plat=%p, cs=%d\n", __func__, plat, plat->cs); 261d0cff03eSSimon Glass if (plat->cs == cs) { 262d7af6a48SSimon Glass *devp = dev; 263d7af6a48SSimon Glass return 0; 264d7af6a48SSimon Glass } 265d7af6a48SSimon Glass } 266d7af6a48SSimon Glass 267d7af6a48SSimon Glass return -ENODEV; 268d7af6a48SSimon Glass } 269d7af6a48SSimon Glass 270d7af6a48SSimon Glass int spi_cs_is_valid(unsigned int busnum, unsigned int cs) 271d7af6a48SSimon Glass { 272d7af6a48SSimon Glass struct spi_cs_info info; 273d7af6a48SSimon Glass struct udevice *bus; 274d7af6a48SSimon Glass int ret; 275d7af6a48SSimon Glass 276d7af6a48SSimon Glass ret = uclass_find_device_by_seq(UCLASS_SPI, busnum, false, &bus); 277d7af6a48SSimon Glass if (ret) { 278d7af6a48SSimon Glass debug("%s: No bus %d\n", __func__, busnum); 279d7af6a48SSimon Glass return ret; 280d7af6a48SSimon Glass } 281d7af6a48SSimon Glass 282d7af6a48SSimon Glass return spi_cs_info(bus, cs, &info); 283d7af6a48SSimon Glass } 284d7af6a48SSimon Glass 285d7af6a48SSimon Glass int spi_cs_info(struct udevice *bus, uint cs, struct spi_cs_info *info) 286d7af6a48SSimon Glass { 287d7af6a48SSimon Glass struct spi_cs_info local_info; 288d7af6a48SSimon Glass int ret; 289d7af6a48SSimon Glass 290d7af6a48SSimon Glass if (!info) 291d7af6a48SSimon Glass info = &local_info; 292d7af6a48SSimon Glass 293d7af6a48SSimon Glass /* If there is a device attached, return it */ 294d7af6a48SSimon Glass info->dev = NULL; 295d7af6a48SSimon Glass ret = spi_find_chip_select(bus, cs, &info->dev); 2965a1c4376SBin Meng return ret == -ENODEV ? 0 : ret; 297d7af6a48SSimon Glass } 298d7af6a48SSimon Glass 299d7af6a48SSimon Glass int spi_find_bus_and_cs(int busnum, int cs, struct udevice **busp, 300d7af6a48SSimon Glass struct udevice **devp) 301d7af6a48SSimon Glass { 302d7af6a48SSimon Glass struct udevice *bus, *dev; 303d7af6a48SSimon Glass int ret; 304d7af6a48SSimon Glass 305d7af6a48SSimon Glass ret = uclass_find_device_by_seq(UCLASS_SPI, busnum, false, &bus); 306d7af6a48SSimon Glass if (ret) { 307d7af6a48SSimon Glass debug("%s: No bus %d\n", __func__, busnum); 308d7af6a48SSimon Glass return ret; 309d7af6a48SSimon Glass } 310d7af6a48SSimon Glass ret = spi_find_chip_select(bus, cs, &dev); 311d7af6a48SSimon Glass if (ret) { 312d7af6a48SSimon Glass debug("%s: No cs %d\n", __func__, cs); 313d7af6a48SSimon Glass return ret; 314d7af6a48SSimon Glass } 315d7af6a48SSimon Glass *busp = bus; 316d7af6a48SSimon Glass *devp = dev; 317d7af6a48SSimon Glass 318d7af6a48SSimon Glass return ret; 319d7af6a48SSimon Glass } 320d7af6a48SSimon Glass 321d7af6a48SSimon Glass int spi_get_bus_and_cs(int busnum, int cs, int speed, int mode, 322d7af6a48SSimon Glass const char *drv_name, const char *dev_name, 323d7af6a48SSimon Glass struct udevice **busp, struct spi_slave **devp) 324d7af6a48SSimon Glass { 325d7af6a48SSimon Glass struct udevice *bus, *dev; 32696907c0fSVignesh R struct dm_spi_slave_platdata *plat; 327*0962b023SMarcin Wojtas struct spi_slave *slave; 328d7af6a48SSimon Glass bool created = false; 329d7af6a48SSimon Glass int ret; 330d7af6a48SSimon Glass 3310e760d45SThomas Fitzsimmons #if CONFIG_IS_ENABLED(OF_PLATDATA) 33271634f28SSimon Glass ret = uclass_first_device_err(UCLASS_SPI, &bus); 33371634f28SSimon Glass #else 334d7af6a48SSimon Glass ret = uclass_get_device_by_seq(UCLASS_SPI, busnum, &bus); 33571634f28SSimon Glass #endif 336d7af6a48SSimon Glass if (ret) { 337d7af6a48SSimon Glass printf("Invalid bus %d (err=%d)\n", busnum, ret); 338d7af6a48SSimon Glass return ret; 339d7af6a48SSimon Glass } 340d7af6a48SSimon Glass ret = spi_find_chip_select(bus, cs, &dev); 341d7af6a48SSimon Glass 342d7af6a48SSimon Glass /* 343d7af6a48SSimon Glass * If there is no such device, create one automatically. This means 344d7af6a48SSimon Glass * that we don't need a device tree node or platform data for the 345d7af6a48SSimon Glass * SPI flash chip - we will bind to the correct driver. 346d7af6a48SSimon Glass */ 347d7af6a48SSimon Glass if (ret == -ENODEV && drv_name) { 348d7af6a48SSimon Glass debug("%s: Binding new device '%s', busnum=%d, cs=%d, driver=%s\n", 349d7af6a48SSimon Glass __func__, dev_name, busnum, cs, drv_name); 3506b18656aSSimon Glass ret = device_bind_driver(bus, drv_name, dev_name, &dev); 35128f98858SSimon Glass if (ret) { 35228f98858SSimon Glass debug("%s: Unable to bind driver (ret=%d)\n", __func__, 35328f98858SSimon Glass ret); 354d7af6a48SSimon Glass return ret; 35528f98858SSimon Glass } 356d0cff03eSSimon Glass plat = dev_get_parent_platdata(dev); 357d0cff03eSSimon Glass plat->cs = cs; 358448ceaf5SSimon Goldschmidt if (speed) { 359d0cff03eSSimon Glass plat->max_hz = speed; 360448ceaf5SSimon Goldschmidt } else { 361448ceaf5SSimon Goldschmidt printf("Warning: SPI speed fallback to %u kHz\n", 362448ceaf5SSimon Goldschmidt SPI_DEFAULT_SPEED_HZ / 1000); 363448ceaf5SSimon Goldschmidt plat->max_hz = SPI_DEFAULT_SPEED_HZ; 364448ceaf5SSimon Goldschmidt } 365d0cff03eSSimon Glass plat->mode = mode; 366d7af6a48SSimon Glass created = true; 367d7af6a48SSimon Glass } else if (ret) { 368d7af6a48SSimon Glass printf("Invalid chip select %d:%d (err=%d)\n", busnum, cs, 369d7af6a48SSimon Glass ret); 370d7af6a48SSimon Glass return ret; 371d7af6a48SSimon Glass } 372d7af6a48SSimon Glass 373d7af6a48SSimon Glass if (!device_active(dev)) { 374d0cff03eSSimon Glass struct spi_slave *slave; 375d7af6a48SSimon Glass 376d0cff03eSSimon Glass ret = device_probe(dev); 377d7af6a48SSimon Glass if (ret) 378d7af6a48SSimon Glass goto err; 379bcbe3d15SSimon Glass slave = dev_get_parent_priv(dev); 380d7af6a48SSimon Glass slave->dev = dev; 381d7af6a48SSimon Glass } 382d7af6a48SSimon Glass 383*0962b023SMarcin Wojtas slave = dev_get_parent_priv(dev); 384ee9b3572SPatrick Delaunay 385*0962b023SMarcin Wojtas /* 386*0962b023SMarcin Wojtas * In case the operation speed is not yet established by 387*0962b023SMarcin Wojtas * dm_spi_claim_bus() ensure the bus is configured properly. 388*0962b023SMarcin Wojtas */ 389*0962b023SMarcin Wojtas if (!slave->speed) { 390*0962b023SMarcin Wojtas ret = spi_claim_bus(slave); 391d7af6a48SSimon Glass if (ret) 392d7af6a48SSimon Glass goto err; 393*0962b023SMarcin Wojtas } 394d7af6a48SSimon Glass 395d7af6a48SSimon Glass *busp = bus; 396*0962b023SMarcin Wojtas *devp = slave; 397d7af6a48SSimon Glass debug("%s: bus=%p, slave=%p\n", __func__, bus, *devp); 398d7af6a48SSimon Glass 399d7af6a48SSimon Glass return 0; 400d7af6a48SSimon Glass 401d7af6a48SSimon Glass err: 402c8864d72SAnatolij Gustschin debug("%s: Error path, created=%d, device '%s'\n", __func__, 403d0cff03eSSimon Glass created, dev->name); 404d7af6a48SSimon Glass if (created) { 405706865afSStefan Roese device_remove(dev, DM_REMOVE_NORMAL); 406d7af6a48SSimon Glass device_unbind(dev); 407d7af6a48SSimon Glass } 408d7af6a48SSimon Glass 409d7af6a48SSimon Glass return ret; 410d7af6a48SSimon Glass } 411d7af6a48SSimon Glass 412d7af6a48SSimon Glass /* Compatibility function - to be removed */ 413d7af6a48SSimon Glass struct spi_slave *spi_setup_slave(unsigned int busnum, unsigned int cs, 414d7af6a48SSimon Glass unsigned int speed, unsigned int mode) 415d7af6a48SSimon Glass { 416d7af6a48SSimon Glass struct spi_slave *slave; 417d7af6a48SSimon Glass struct udevice *dev; 418d7af6a48SSimon Glass int ret; 419d7af6a48SSimon Glass 420d7af6a48SSimon Glass ret = spi_get_bus_and_cs(busnum, cs, speed, mode, NULL, 0, &dev, 421d7af6a48SSimon Glass &slave); 422d7af6a48SSimon Glass if (ret) 423d7af6a48SSimon Glass return NULL; 424d7af6a48SSimon Glass 425d7af6a48SSimon Glass return slave; 426d7af6a48SSimon Glass } 427d7af6a48SSimon Glass 428d7af6a48SSimon Glass void spi_free_slave(struct spi_slave *slave) 429d7af6a48SSimon Glass { 430706865afSStefan Roese device_remove(slave->dev, DM_REMOVE_NORMAL); 431d7af6a48SSimon Glass slave->dev = NULL; 432d7af6a48SSimon Glass } 433d7af6a48SSimon Glass 434279e26f5SSimon Glass int spi_slave_ofdata_to_platdata(struct udevice *dev, 435d0cff03eSSimon Glass struct dm_spi_slave_platdata *plat) 436d7af6a48SSimon Glass { 43708fe9c29SJagan Teki int mode = 0; 438f8e2f92dSMugunthan V N int value; 439d7af6a48SSimon Glass 440279e26f5SSimon Glass plat->cs = dev_read_u32_default(dev, "reg", -1); 441448ceaf5SSimon Goldschmidt plat->max_hz = dev_read_u32_default(dev, "spi-max-frequency", 442448ceaf5SSimon Goldschmidt SPI_DEFAULT_SPEED_HZ); 443279e26f5SSimon Glass if (dev_read_bool(dev, "spi-cpol")) 444d7af6a48SSimon Glass mode |= SPI_CPOL; 445279e26f5SSimon Glass if (dev_read_bool(dev, "spi-cpha")) 446d7af6a48SSimon Glass mode |= SPI_CPHA; 447279e26f5SSimon Glass if (dev_read_bool(dev, "spi-cs-high")) 448d7af6a48SSimon Glass mode |= SPI_CS_HIGH; 449279e26f5SSimon Glass if (dev_read_bool(dev, "spi-3wire")) 450379b49d8SJagan Teki mode |= SPI_3WIRE; 451279e26f5SSimon Glass if (dev_read_bool(dev, "spi-half-duplex")) 452d7af6a48SSimon Glass mode |= SPI_PREAMBLE; 453f8e2f92dSMugunthan V N 454f8e2f92dSMugunthan V N /* Device DUAL/QUAD mode */ 455279e26f5SSimon Glass value = dev_read_u32_default(dev, "spi-tx-bus-width", 1); 456f8e2f92dSMugunthan V N switch (value) { 457f8e2f92dSMugunthan V N case 1: 458f8e2f92dSMugunthan V N break; 459f8e2f92dSMugunthan V N case 2: 460f8e2f92dSMugunthan V N mode |= SPI_TX_DUAL; 461f8e2f92dSMugunthan V N break; 462f8e2f92dSMugunthan V N case 4: 463f8e2f92dSMugunthan V N mode |= SPI_TX_QUAD; 464f8e2f92dSMugunthan V N break; 465f8e2f92dSMugunthan V N default: 4661b7c28f5SSimon Glass warn_non_spl("spi-tx-bus-width %d not supported\n", value); 467f8e2f92dSMugunthan V N break; 468f8e2f92dSMugunthan V N } 469f8e2f92dSMugunthan V N 470279e26f5SSimon Glass value = dev_read_u32_default(dev, "spi-rx-bus-width", 1); 471f8e2f92dSMugunthan V N switch (value) { 472f8e2f92dSMugunthan V N case 1: 473f8e2f92dSMugunthan V N break; 474f8e2f92dSMugunthan V N case 2: 47508fe9c29SJagan Teki mode |= SPI_RX_DUAL; 476f8e2f92dSMugunthan V N break; 477f8e2f92dSMugunthan V N case 4: 47808fe9c29SJagan Teki mode |= SPI_RX_QUAD; 479f8e2f92dSMugunthan V N break; 480f8e2f92dSMugunthan V N default: 4811b7c28f5SSimon Glass warn_non_spl("spi-rx-bus-width %d not supported\n", value); 482f8e2f92dSMugunthan V N break; 483f8e2f92dSMugunthan V N } 484f8e2f92dSMugunthan V N 48508fe9c29SJagan Teki plat->mode = mode; 486f8e2f92dSMugunthan V N 487d7af6a48SSimon Glass return 0; 488d7af6a48SSimon Glass } 489d7af6a48SSimon Glass 490d7af6a48SSimon Glass UCLASS_DRIVER(spi) = { 491d7af6a48SSimon Glass .id = UCLASS_SPI, 492d7af6a48SSimon Glass .name = "spi", 4939cc36a2bSSimon Glass .flags = DM_UC_FLAG_SEQ_ALIAS, 49471634f28SSimon Glass #if !CONFIG_IS_ENABLED(OF_PLATDATA) 49591195485SSimon Glass .post_bind = dm_scan_fdt_dev, 49671634f28SSimon Glass #endif 497d7af6a48SSimon Glass .post_probe = spi_post_probe, 498440714eeSSimon Glass .child_pre_probe = spi_child_pre_probe, 499d7af6a48SSimon Glass .per_device_auto_alloc_size = sizeof(struct dm_spi_bus), 50019a25f67SSimon Glass .per_child_auto_alloc_size = sizeof(struct spi_slave), 501d0cff03eSSimon Glass .per_child_platdata_auto_alloc_size = 502d0cff03eSSimon Glass sizeof(struct dm_spi_slave_platdata), 50371634f28SSimon Glass #if !CONFIG_IS_ENABLED(OF_PLATDATA) 504d0cff03eSSimon Glass .child_post_bind = spi_child_post_bind, 50571634f28SSimon Glass #endif 506d7af6a48SSimon Glass }; 507d7af6a48SSimon Glass 508d7af6a48SSimon Glass UCLASS_DRIVER(spi_generic) = { 509d7af6a48SSimon Glass .id = UCLASS_SPI_GENERIC, 510d7af6a48SSimon Glass .name = "spi_generic", 511d7af6a48SSimon Glass }; 512d7af6a48SSimon Glass 513d7af6a48SSimon Glass U_BOOT_DRIVER(spi_generic_drv) = { 514d7af6a48SSimon Glass .name = "spi_generic_drv", 515d7af6a48SSimon Glass .id = UCLASS_SPI_GENERIC, 516d7af6a48SSimon Glass }; 517