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_xfer(struct spi_slave *slave, unsigned int bitlen,
134 const void *dout, void *din, unsigned long flags)
136 return dm_spi_xfer(slave->dev, bitlen, dout, din, flags);
139 int spi_write_then_read(struct spi_slave *slave, const u8 *opcode,
140 size_t n_opcode, const u8 *txbuf, u8 *rxbuf,
143 unsigned long flags = SPI_XFER_BEGIN;
147 flags |= SPI_XFER_END;
149 ret = spi_xfer(slave, n_opcode * 8, opcode, NULL, flags);
152 "spi: failed to send command (%zu bytes): %d\n",
154 } else if (n_buf != 0) {
155 ret = spi_xfer(slave, n_buf * 8, txbuf, rxbuf, SPI_XFER_END);
158 "spi: failed to transfer %zu bytes of data: %d\n",
165 #if CONFIG_IS_ENABLED(OF_REAL)
166 static int spi_child_post_bind(struct udevice *dev)
168 struct dm_spi_slave_plat *plat = dev_get_parent_plat(dev);
170 if (!dev_has_ofnode(dev))
173 return spi_slave_of_to_plat(dev, plat);
177 static int spi_post_probe(struct udevice *bus)
179 if (CONFIG_IS_ENABLED(OF_REAL)) {
180 struct dm_spi_bus *spi = dev_get_uclass_priv(bus);
182 spi->max_hz = dev_read_u32_default(bus, "spi-max-frequency", 0);
184 #if defined(CONFIG_NEEDS_MANUAL_RELOC)
185 struct dm_spi_ops *ops = spi_get_ops(bus);
186 static int reloc_done;
190 ops->claim_bus += gd->reloc_off;
191 if (ops->release_bus)
192 ops->release_bus += gd->reloc_off;
193 if (ops->set_wordlen)
194 ops->set_wordlen += gd->reloc_off;
196 ops->xfer += gd->reloc_off;
198 ops->set_speed += gd->reloc_off;
200 ops->set_mode += gd->reloc_off;
202 ops->cs_info += gd->reloc_off;
204 struct spi_controller_mem_ops *mem_ops =
205 (struct spi_controller_mem_ops *)ops->mem_ops;
206 if (mem_ops->adjust_op_size)
207 mem_ops->adjust_op_size += gd->reloc_off;
208 if (mem_ops->supports_op)
209 mem_ops->supports_op += gd->reloc_off;
210 if (mem_ops->exec_op)
211 mem_ops->exec_op += gd->reloc_off;
220 static int spi_child_pre_probe(struct udevice *dev)
222 struct dm_spi_slave_plat *plat = dev_get_parent_plat(dev);
223 struct spi_slave *slave = dev_get_parent_priv(dev);
226 * This is needed because we pass struct spi_slave around the place
227 * instead slave->dev (a struct udevice). So we have to have some
228 * way to access the slave udevice given struct spi_slave. Once we
229 * change the SPI API to use udevice instead of spi_slave, we can
234 slave->max_hz = plat->max_hz;
235 slave->mode = plat->mode;
236 slave->wordlen = SPI_DEFAULT_WORDLEN;
241 int spi_chip_select(struct udevice *dev)
243 struct dm_spi_slave_plat *plat = dev_get_parent_plat(dev);
245 return plat ? plat->cs : -ENOENT;
248 int spi_find_chip_select(struct udevice *bus, int cs, struct udevice **devp)
250 struct dm_spi_ops *ops;
251 struct spi_cs_info info;
256 * Ask the driver. For the moment we don't have CS info.
257 * When we do we could provide the driver with a helper function
258 * to figure out what chip selects are valid, or just handle the
261 ops = spi_get_ops(bus);
263 ret = ops->cs_info(bus, cs, &info);
266 * We could assume there is at least one valid chip select.
267 * The driver didn't care enough to tell us.
273 dev_err(bus, "Invalid cs %d (err=%d)\n", cs, ret);
277 for (device_find_first_child(bus, &dev); dev;
278 device_find_next_child(&dev)) {
279 struct dm_spi_slave_plat *plat;
281 plat = dev_get_parent_plat(dev);
282 dev_dbg(bus, "%s: plat=%p, cs=%d\n", __func__, plat, plat->cs);
283 if (plat->cs == cs) {
292 int spi_cs_is_valid(unsigned int busnum, unsigned int cs)
294 struct spi_cs_info info;
298 ret = uclass_find_device_by_seq(UCLASS_SPI, busnum, &bus);
300 log_debug("%s: No bus %d\n", __func__, busnum);
304 return spi_cs_info(bus, cs, &info);
307 int spi_cs_info(struct udevice *bus, uint cs, struct spi_cs_info *info)
309 struct spi_cs_info local_info;
315 /* If there is a device attached, return it */
317 ret = spi_find_chip_select(bus, cs, &info->dev);
318 return ret == -ENODEV ? 0 : ret;
321 int spi_find_bus_and_cs(int busnum, int cs, struct udevice **busp,
322 struct udevice **devp)
324 struct udevice *bus, *dev;
327 ret = uclass_find_device_by_seq(UCLASS_SPI, busnum, &bus);
329 log_debug("%s: No bus %d\n", __func__, busnum);
332 ret = spi_find_chip_select(bus, cs, &dev);
334 dev_dbg(bus, "%s: No cs %d\n", __func__, cs);
343 int spi_get_bus_and_cs(int busnum, int cs, struct udevice **busp,
344 struct spi_slave **devp)
346 struct udevice *bus, *dev;
347 struct dm_spi_bus *bus_data;
348 struct spi_slave *slave;
351 #if CONFIG_IS_ENABLED(OF_PLATDATA)
352 ret = uclass_first_device_err(UCLASS_SPI, &bus);
354 ret = uclass_get_device_by_seq(UCLASS_SPI, busnum, &bus);
357 log_err("Invalid bus %d (err=%d)\n", busnum, ret);
360 ret = spi_find_chip_select(bus, cs, &dev);
362 dev_err(bus, "Invalid chip select %d:%d (err=%d)\n", busnum, cs, ret);
366 if (!device_active(dev)) {
367 struct spi_slave *slave;
369 ret = device_probe(dev);
372 slave = dev_get_parent_priv(dev);
376 slave = dev_get_parent_priv(dev);
377 bus_data = dev_get_uclass_priv(bus);
380 * In case the operation speed is not yet established by
381 * dm_spi_claim_bus() ensure the bus is configured properly.
383 if (!bus_data->speed) {
384 ret = spi_claim_bus(slave);
394 log_debug("%s: Error path, device '%s'\n", __func__, dev->name);
399 int _spi_get_bus_and_cs(int busnum, int cs, int speed, int mode,
400 const char *drv_name, const char *dev_name,
401 struct udevice **busp, struct spi_slave **devp)
403 struct udevice *bus, *dev;
404 struct dm_spi_slave_plat *plat;
405 struct dm_spi_bus *bus_data;
406 struct spi_slave *slave;
407 bool created = false;
410 #if CONFIG_IS_ENABLED(OF_PLATDATA)
411 ret = uclass_first_device_err(UCLASS_SPI, &bus);
413 ret = uclass_get_device_by_seq(UCLASS_SPI, busnum, &bus);
416 log_err("Invalid bus %d (err=%d)\n", busnum, ret);
419 ret = spi_find_chip_select(bus, cs, &dev);
422 * If there is no such device, create one automatically. This means
423 * that we don't need a device tree node or platform data for the
424 * SPI flash chip - we will bind to the correct driver.
426 if (ret == -ENODEV && drv_name) {
427 dev_dbg(bus, "%s: Binding new device '%s', busnum=%d, cs=%d, driver=%s\n",
428 __func__, dev_name, busnum, cs, drv_name);
429 ret = device_bind_driver(bus, drv_name, dev_name, &dev);
431 dev_dbg(bus, "%s: Unable to bind driver (ret=%d)\n",
435 plat = dev_get_parent_plat(dev);
438 plat->max_hz = speed;
441 "Warning: SPI speed fallback to %u kHz\n",
442 SPI_DEFAULT_SPEED_HZ / 1000);
443 plat->max_hz = SPI_DEFAULT_SPEED_HZ;
448 dev_err(bus, "Invalid chip select %d:%d (err=%d)\n", busnum, cs, ret);
451 plat = dev_get_parent_plat(dev);
454 if (!device_active(dev)) {
455 struct spi_slave *slave;
457 ret = device_probe(dev);
460 slave = dev_get_parent_priv(dev);
464 slave = dev_get_parent_priv(dev);
465 bus_data = dev_get_uclass_priv(bus);
468 * In case the operation speed is not yet established by
469 * dm_spi_claim_bus() ensure the bus is configured properly.
471 if (!bus_data->speed) {
472 ret = spi_claim_bus(slave);
477 /* In case bus frequency or mode changed, update it. */
478 if ((speed && bus_data->speed && bus_data->speed != speed) ||
479 (plat && plat->mode != mode)) {
480 ret = spi_set_speed_mode(bus, speed, mode);
487 log_debug("%s: bus=%p, slave=%p\n", __func__, bus, *devp);
492 spi_release_bus(slave);
494 log_debug("%s: Error path, created=%d, device '%s'\n", __func__,
497 device_remove(dev, DM_REMOVE_NORMAL);
504 /* Compatibility function - to be removed */
505 struct spi_slave *spi_setup_slave(unsigned int busnum, unsigned int cs,
506 unsigned int speed, unsigned int mode)
508 struct spi_slave *slave;
512 ret = _spi_get_bus_and_cs(busnum, cs, speed, mode, NULL, 0, &dev,
520 void spi_free_slave(struct spi_slave *slave)
522 device_remove(slave->dev, DM_REMOVE_NORMAL);
525 int spi_slave_of_to_plat(struct udevice *dev, struct dm_spi_slave_plat *plat)
530 plat->cs = dev_read_u32_default(dev, "reg", -1);
531 plat->max_hz = dev_read_u32_default(dev, "spi-max-frequency",
532 SPI_DEFAULT_SPEED_HZ);
533 if (dev_read_bool(dev, "spi-cpol"))
535 if (dev_read_bool(dev, "spi-cpha"))
537 if (dev_read_bool(dev, "spi-cs-high"))
539 if (dev_read_bool(dev, "spi-3wire"))
541 if (dev_read_bool(dev, "spi-half-duplex"))
542 mode |= SPI_PREAMBLE;
544 /* Device DUAL/QUAD mode */
545 value = dev_read_u32_default(dev, "spi-tx-bus-width", 1);
556 mode |= SPI_TX_OCTAL;
559 warn_non_spl("spi-tx-bus-width %d not supported\n", value);
563 value = dev_read_u32_default(dev, "spi-rx-bus-width", 1);
574 mode |= SPI_RX_OCTAL;
577 warn_non_spl("spi-rx-bus-width %d not supported\n", value);
586 UCLASS_DRIVER(spi) = {
589 .flags = DM_UC_FLAG_SEQ_ALIAS,
590 #if CONFIG_IS_ENABLED(OF_REAL)
591 .post_bind = dm_scan_fdt_dev,
593 .post_probe = spi_post_probe,
594 .child_pre_probe = spi_child_pre_probe,
595 .per_device_auto = sizeof(struct dm_spi_bus),
596 .per_child_auto = sizeof(struct spi_slave),
597 .per_child_plat_auto = sizeof(struct dm_spi_slave_plat),
598 #if CONFIG_IS_ENABLED(OF_REAL)
599 .child_post_bind = spi_child_post_bind,
603 UCLASS_DRIVER(spi_generic) = {
604 .id = UCLASS_SPI_GENERIC,
605 .name = "spi_generic",
608 U_BOOT_DRIVER(spi_generic_drv) = {
609 .name = "spi_generic_drv",
610 .id = UCLASS_SPI_GENERIC,