1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2014 Google, Inc
6 #define LOG_CATEGORY UCLASS_SPI
15 #include <dm/device_compat.h>
16 #include <asm/global_data.h>
17 #include <dm/device-internal.h>
18 #include <dm/uclass-internal.h>
22 DECLARE_GLOBAL_DATA_PTR;
24 #define SPI_DEFAULT_SPEED_HZ 100000
26 static int spi_set_speed_mode(struct udevice *bus, int speed, int mode)
28 struct dm_spi_ops *ops;
31 ops = spi_get_ops(bus);
33 ret = ops->set_speed(bus, speed);
37 dev_err(bus, "Cannot set speed (err=%d)\n", ret);
42 ret = ops->set_mode(bus, mode);
46 dev_err(bus, "Cannot set mode (err=%d)\n", ret);
53 int dm_spi_claim_bus(struct udevice *dev)
55 struct udevice *bus = dev->parent;
56 struct dm_spi_ops *ops = spi_get_ops(bus);
57 struct dm_spi_bus *spi = dev_get_uclass_priv(bus);
58 struct spi_slave *slave = dev_get_parent_priv(dev);
61 speed = slave->max_hz;
66 speed = min(speed, spi->max_hz);
71 speed = SPI_DEFAULT_SPEED_HZ;
73 if (speed != spi->speed || mode != spi->mode) {
74 int ret = spi_set_speed_mode(bus, speed, slave->mode);
83 return log_ret(ops->claim_bus ? ops->claim_bus(dev) : 0);
86 void dm_spi_release_bus(struct udevice *dev)
88 struct udevice *bus = dev->parent;
89 struct dm_spi_ops *ops = spi_get_ops(bus);
92 ops->release_bus(dev);
95 int dm_spi_xfer(struct udevice *dev, unsigned int bitlen,
96 const void *dout, void *din, unsigned long flags)
98 struct udevice *bus = dev->parent;
99 struct dm_spi_ops *ops = spi_get_ops(bus);
101 if (bus->uclass->uc_drv->id != UCLASS_SPI)
106 return ops->xfer(dev, bitlen, dout, din, flags);
109 int dm_spi_get_mmap(struct udevice *dev, ulong *map_basep, uint *map_sizep,
112 struct udevice *bus = dev->parent;
113 struct dm_spi_ops *ops = spi_get_ops(bus);
115 if (bus->uclass->uc_drv->id != UCLASS_SPI)
120 return ops->get_mmap(dev, map_basep, map_sizep, offsetp);
123 int spi_claim_bus(struct spi_slave *slave)
125 return log_ret(dm_spi_claim_bus(slave->dev));
128 void spi_release_bus(struct spi_slave *slave)
130 dm_spi_release_bus(slave->dev);
133 int spi_set_speed(struct spi_slave *slave, uint hz)
135 struct dm_spi_ops *ops;
138 ops = spi_get_ops(slave->dev->parent);
140 ret = ops->set_speed(slave->dev->parent, hz);
144 dev_err(slave->dev, "Cannot set speed (err=%d)\n", ret);
148 int spi_xfer(struct spi_slave *slave, unsigned int bitlen,
149 const void *dout, void *din, unsigned long flags)
151 return dm_spi_xfer(slave->dev, bitlen, dout, din, flags);
154 int spi_write_then_read(struct spi_slave *slave, const u8 *opcode,
155 size_t n_opcode, const u8 *txbuf, u8 *rxbuf,
158 unsigned long flags = SPI_XFER_BEGIN;
162 flags |= SPI_XFER_END;
164 ret = spi_xfer(slave, n_opcode * 8, opcode, NULL, flags);
167 "spi: failed to send command (%zu bytes): %d\n",
169 } else if (n_buf != 0) {
170 ret = spi_xfer(slave, n_buf * 8, txbuf, rxbuf, SPI_XFER_END);
173 "spi: failed to transfer %zu bytes of data: %d\n",
180 #if CONFIG_IS_ENABLED(OF_REAL)
181 static int spi_child_post_bind(struct udevice *dev)
183 struct dm_spi_slave_plat *plat = dev_get_parent_plat(dev);
185 if (!dev_has_ofnode(dev))
188 return spi_slave_of_to_plat(dev, plat);
192 static int spi_post_probe(struct udevice *bus)
194 if (CONFIG_IS_ENABLED(OF_REAL)) {
195 struct dm_spi_bus *spi = dev_get_uclass_priv(bus);
197 spi->max_hz = dev_read_u32_default(bus, "spi-max-frequency", 0);
199 #if defined(CONFIG_NEEDS_MANUAL_RELOC)
200 struct dm_spi_ops *ops = spi_get_ops(bus);
201 static int reloc_done;
205 ops->claim_bus += gd->reloc_off;
206 if (ops->release_bus)
207 ops->release_bus += gd->reloc_off;
208 if (ops->set_wordlen)
209 ops->set_wordlen += gd->reloc_off;
211 ops->xfer += gd->reloc_off;
213 ops->set_speed += gd->reloc_off;
215 ops->set_mode += gd->reloc_off;
217 ops->cs_info += gd->reloc_off;
219 struct spi_controller_mem_ops *mem_ops =
220 (struct spi_controller_mem_ops *)ops->mem_ops;
221 if (mem_ops->adjust_op_size)
222 mem_ops->adjust_op_size += gd->reloc_off;
223 if (mem_ops->supports_op)
224 mem_ops->supports_op += gd->reloc_off;
225 if (mem_ops->exec_op)
226 mem_ops->exec_op += gd->reloc_off;
235 static int spi_child_pre_probe(struct udevice *dev)
237 struct dm_spi_slave_plat *plat = dev_get_parent_plat(dev);
238 struct spi_slave *slave = dev_get_parent_priv(dev);
241 * This is needed because we pass struct spi_slave around the place
242 * instead slave->dev (a struct udevice). So we have to have some
243 * way to access the slave udevice given struct spi_slave. Once we
244 * change the SPI API to use udevice instead of spi_slave, we can
249 slave->max_hz = plat->max_hz;
250 slave->mode = plat->mode;
251 slave->wordlen = SPI_DEFAULT_WORDLEN;
256 int spi_chip_select(struct udevice *dev)
258 struct dm_spi_slave_plat *plat = dev_get_parent_plat(dev);
260 return plat ? plat->cs : -ENOENT;
263 int spi_find_chip_select(struct udevice *bus, int cs, struct udevice **devp)
265 struct dm_spi_ops *ops;
266 struct spi_cs_info info;
271 * Ask the driver. For the moment we don't have CS info.
272 * When we do we could provide the driver with a helper function
273 * to figure out what chip selects are valid, or just handle the
276 ops = spi_get_ops(bus);
278 ret = ops->cs_info(bus, cs, &info);
281 * We could assume there is at least one valid chip select.
282 * The driver didn't care enough to tell us.
288 dev_err(bus, "Invalid cs %d (err=%d)\n", cs, ret);
292 for (device_find_first_child(bus, &dev); dev;
293 device_find_next_child(&dev)) {
294 struct dm_spi_slave_plat *plat;
296 plat = dev_get_parent_plat(dev);
297 dev_dbg(bus, "%s: plat=%p, cs=%d\n", __func__, plat, plat->cs);
298 if (plat->cs == cs) {
307 int spi_cs_is_valid(unsigned int busnum, unsigned int cs)
309 struct spi_cs_info info;
313 ret = uclass_find_device_by_seq(UCLASS_SPI, busnum, &bus);
315 log_debug("%s: No bus %d\n", __func__, busnum);
319 return spi_cs_info(bus, cs, &info);
322 int spi_cs_info(struct udevice *bus, uint cs, struct spi_cs_info *info)
324 struct spi_cs_info local_info;
330 /* If there is a device attached, return it */
332 ret = spi_find_chip_select(bus, cs, &info->dev);
333 return ret == -ENODEV ? 0 : ret;
336 int spi_find_bus_and_cs(int busnum, int cs, struct udevice **busp,
337 struct udevice **devp)
339 struct udevice *bus, *dev;
342 ret = uclass_find_device_by_seq(UCLASS_SPI, busnum, &bus);
344 log_debug("%s: No bus %d\n", __func__, busnum);
347 ret = spi_find_chip_select(bus, cs, &dev);
349 dev_dbg(bus, "%s: No cs %d\n", __func__, cs);
358 int spi_get_bus_and_cs(int busnum, int cs, struct udevice **busp,
359 struct spi_slave **devp)
361 struct udevice *bus, *dev;
362 struct dm_spi_bus *bus_data;
363 struct spi_slave *slave;
366 #if CONFIG_IS_ENABLED(OF_PLATDATA)
367 ret = uclass_first_device_err(UCLASS_SPI, &bus);
369 ret = uclass_get_device_by_seq(UCLASS_SPI, busnum, &bus);
372 log_err("Invalid bus %d (err=%d)\n", busnum, ret);
375 ret = spi_find_chip_select(bus, cs, &dev);
377 dev_err(bus, "Invalid chip select %d:%d (err=%d)\n", busnum, cs, ret);
381 if (!device_active(dev)) {
382 struct spi_slave *slave;
384 ret = device_probe(dev);
387 slave = dev_get_parent_priv(dev);
391 slave = dev_get_parent_priv(dev);
392 bus_data = dev_get_uclass_priv(bus);
395 * In case the operation speed is not yet established by
396 * dm_spi_claim_bus() ensure the bus is configured properly.
398 if (!bus_data->speed) {
399 ret = spi_claim_bus(slave);
409 log_debug("%s: Error path, device '%s'\n", __func__, dev->name);
414 int _spi_get_bus_and_cs(int busnum, int cs, int speed, int mode,
415 const char *drv_name, const char *dev_name,
416 struct udevice **busp, struct spi_slave **devp)
418 struct udevice *bus, *dev;
419 struct dm_spi_slave_plat *plat;
420 struct dm_spi_bus *bus_data;
421 struct spi_slave *slave;
422 bool created = false;
425 #if CONFIG_IS_ENABLED(OF_PLATDATA)
426 ret = uclass_first_device_err(UCLASS_SPI, &bus);
428 ret = uclass_get_device_by_seq(UCLASS_SPI, busnum, &bus);
431 log_err("Invalid bus %d (err=%d)\n", busnum, ret);
434 ret = spi_find_chip_select(bus, cs, &dev);
437 * If there is no such device, create one automatically. This means
438 * that we don't need a device tree node or platform data for the
439 * SPI flash chip - we will bind to the correct driver.
441 if (ret == -ENODEV && drv_name) {
442 dev_dbg(bus, "%s: Binding new device '%s', busnum=%d, cs=%d, driver=%s\n",
443 __func__, dev_name, busnum, cs, drv_name);
444 ret = device_bind_driver(bus, drv_name, dev_name, &dev);
446 dev_dbg(bus, "%s: Unable to bind driver (ret=%d)\n",
450 plat = dev_get_parent_plat(dev);
453 plat->max_hz = speed;
456 "Warning: SPI speed fallback to %u kHz\n",
457 SPI_DEFAULT_SPEED_HZ / 1000);
458 plat->max_hz = SPI_DEFAULT_SPEED_HZ;
463 dev_err(bus, "Invalid chip select %d:%d (err=%d)\n", busnum, cs, ret);
466 plat = dev_get_parent_plat(dev);
469 if (!device_active(dev)) {
470 struct spi_slave *slave;
472 ret = device_probe(dev);
475 slave = dev_get_parent_priv(dev);
479 slave = dev_get_parent_priv(dev);
480 bus_data = dev_get_uclass_priv(bus);
483 * In case the operation speed is not yet established by
484 * dm_spi_claim_bus() ensure the bus is configured properly.
486 if (!bus_data->speed) {
487 ret = spi_claim_bus(slave);
492 /* In case bus frequency or mode changed, update it. */
493 if ((speed && bus_data->speed && bus_data->speed != speed) ||
494 (plat && plat->mode != mode)) {
495 ret = spi_set_speed_mode(bus, speed, mode);
502 log_debug("%s: bus=%p, slave=%p\n", __func__, bus, *devp);
507 spi_release_bus(slave);
509 log_debug("%s: Error path, created=%d, device '%s'\n", __func__,
512 device_remove(dev, DM_REMOVE_NORMAL);
519 /* Compatibility function - to be removed */
520 struct spi_slave *spi_setup_slave(unsigned int busnum, unsigned int cs,
521 unsigned int speed, unsigned int mode)
523 struct spi_slave *slave;
527 ret = _spi_get_bus_and_cs(busnum, cs, speed, mode, NULL, 0, &dev,
535 void spi_free_slave(struct spi_slave *slave)
537 device_remove(slave->dev, DM_REMOVE_NORMAL);
540 int spi_slave_of_to_plat(struct udevice *dev, struct dm_spi_slave_plat *plat)
545 plat->cs = dev_read_u32_default(dev, "reg", -1);
546 plat->max_hz = dev_read_u32_default(dev, "spi-max-frequency",
547 SPI_DEFAULT_SPEED_HZ);
548 if (dev_read_bool(dev, "spi-cpol"))
550 if (dev_read_bool(dev, "spi-cpha"))
552 if (dev_read_bool(dev, "spi-cs-high"))
554 if (dev_read_bool(dev, "spi-3wire"))
556 if (dev_read_bool(dev, "spi-half-duplex"))
557 mode |= SPI_PREAMBLE;
559 /* Device DUAL/QUAD mode */
560 value = dev_read_u32_default(dev, "spi-tx-bus-width", 1);
571 mode |= SPI_TX_OCTAL;
574 warn_non_spl("spi-tx-bus-width %d not supported\n", value);
578 value = dev_read_u32_default(dev, "spi-rx-bus-width", 1);
589 mode |= SPI_RX_OCTAL;
592 warn_non_spl("spi-rx-bus-width %d not supported\n", value);
601 UCLASS_DRIVER(spi) = {
604 .flags = DM_UC_FLAG_SEQ_ALIAS,
605 #if CONFIG_IS_ENABLED(OF_REAL)
606 .post_bind = dm_scan_fdt_dev,
608 .post_probe = spi_post_probe,
609 .child_pre_probe = spi_child_pre_probe,
610 .per_device_auto = sizeof(struct dm_spi_bus),
611 .per_child_auto = sizeof(struct spi_slave),
612 .per_child_plat_auto = sizeof(struct dm_spi_slave_plat),
613 #if CONFIG_IS_ENABLED(OF_REAL)
614 .child_post_bind = spi_child_post_bind,
618 UCLASS_DRIVER(spi_generic) = {
619 .id = UCLASS_SPI_GENERIC,
620 .name = "spi_generic",
623 U_BOOT_DRIVER(spi_generic_drv) = {
624 .name = "spi_generic_drv",
625 .id = UCLASS_SPI_GENERIC,