1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2014 Google, Inc
6 #define LOG_CATEGORY UCLASS_SPI
14 #include <dm/device_compat.h>
15 #include <dm/device-internal.h>
16 #include <dm/uclass-internal.h>
20 DECLARE_GLOBAL_DATA_PTR;
22 #define SPI_DEFAULT_SPEED_HZ 100000
24 static int spi_set_speed_mode(struct udevice *bus, int speed, int mode)
26 struct dm_spi_ops *ops;
29 ops = spi_get_ops(bus);
31 ret = ops->set_speed(bus, speed);
35 dev_err(bus, "Cannot set speed (err=%d)\n", ret);
40 ret = ops->set_mode(bus, mode);
44 dev_err(bus, "Cannot set mode (err=%d)\n", ret);
51 int dm_spi_claim_bus(struct udevice *dev)
53 struct udevice *bus = dev->parent;
54 struct dm_spi_ops *ops = spi_get_ops(bus);
55 struct dm_spi_bus *spi = dev_get_uclass_priv(bus);
56 struct spi_slave *slave = dev_get_parent_priv(dev);
59 speed = slave->max_hz;
64 speed = min(speed, spi->max_hz);
69 speed = SPI_DEFAULT_SPEED_HZ;
71 if (speed != spi->speed || mode != spi->mode) {
72 int ret = spi_set_speed_mode(bus, speed, slave->mode);
81 return log_ret(ops->claim_bus ? ops->claim_bus(dev) : 0);
84 void dm_spi_release_bus(struct udevice *dev)
86 struct udevice *bus = dev->parent;
87 struct dm_spi_ops *ops = spi_get_ops(bus);
90 ops->release_bus(dev);
93 int dm_spi_xfer(struct udevice *dev, unsigned int bitlen,
94 const void *dout, void *din, unsigned long flags)
96 struct udevice *bus = dev->parent;
97 struct dm_spi_ops *ops = spi_get_ops(bus);
99 if (bus->uclass->uc_drv->id != UCLASS_SPI)
104 return ops->xfer(dev, bitlen, dout, din, flags);
107 int dm_spi_get_mmap(struct udevice *dev, ulong *map_basep, uint *map_sizep,
110 struct udevice *bus = dev->parent;
111 struct dm_spi_ops *ops = spi_get_ops(bus);
113 if (bus->uclass->uc_drv->id != UCLASS_SPI)
118 return ops->get_mmap(dev, map_basep, map_sizep, offsetp);
121 int spi_claim_bus(struct spi_slave *slave)
123 return log_ret(dm_spi_claim_bus(slave->dev));
126 void spi_release_bus(struct spi_slave *slave)
128 dm_spi_release_bus(slave->dev);
131 int spi_xfer(struct spi_slave *slave, unsigned int bitlen,
132 const void *dout, void *din, unsigned long flags)
134 return dm_spi_xfer(slave->dev, bitlen, dout, din, flags);
137 int spi_write_then_read(struct spi_slave *slave, const u8 *opcode,
138 size_t n_opcode, const u8 *txbuf, u8 *rxbuf,
141 unsigned long flags = SPI_XFER_BEGIN;
145 flags |= SPI_XFER_END;
147 ret = spi_xfer(slave, n_opcode * 8, opcode, NULL, flags);
150 "spi: failed to send command (%zu bytes): %d\n",
152 } else if (n_buf != 0) {
153 ret = spi_xfer(slave, n_buf * 8, txbuf, rxbuf, SPI_XFER_END);
156 "spi: failed to transfer %zu bytes of data: %d\n",
163 #if !CONFIG_IS_ENABLED(OF_PLATDATA)
164 static int spi_child_post_bind(struct udevice *dev)
166 struct dm_spi_slave_plat *plat = dev_get_parent_plat(dev);
168 if (!dev_has_ofnode(dev))
171 return spi_slave_of_to_plat(dev, plat);
175 static int spi_post_probe(struct udevice *bus)
177 #if !CONFIG_IS_ENABLED(OF_PLATDATA)
178 struct dm_spi_bus *spi = dev_get_uclass_priv(bus);
180 spi->max_hz = dev_read_u32_default(bus, "spi-max-frequency", 0);
182 #if defined(CONFIG_NEEDS_MANUAL_RELOC)
183 struct dm_spi_ops *ops = spi_get_ops(bus);
184 static int reloc_done;
188 ops->claim_bus += gd->reloc_off;
189 if (ops->release_bus)
190 ops->release_bus += gd->reloc_off;
191 if (ops->set_wordlen)
192 ops->set_wordlen += gd->reloc_off;
194 ops->xfer += gd->reloc_off;
196 ops->set_speed += gd->reloc_off;
198 ops->set_mode += gd->reloc_off;
200 ops->cs_info += gd->reloc_off;
208 static int spi_child_pre_probe(struct udevice *dev)
210 struct dm_spi_slave_plat *plat = dev_get_parent_plat(dev);
211 struct spi_slave *slave = dev_get_parent_priv(dev);
214 * This is needed because we pass struct spi_slave around the place
215 * instead slave->dev (a struct udevice). So we have to have some
216 * way to access the slave udevice given struct spi_slave. Once we
217 * change the SPI API to use udevice instead of spi_slave, we can
222 slave->max_hz = plat->max_hz;
223 slave->mode = plat->mode;
224 slave->wordlen = SPI_DEFAULT_WORDLEN;
229 int spi_chip_select(struct udevice *dev)
231 struct dm_spi_slave_plat *plat = dev_get_parent_plat(dev);
233 return plat ? plat->cs : -ENOENT;
236 int spi_find_chip_select(struct udevice *bus, int cs, struct udevice **devp)
238 struct dm_spi_ops *ops;
239 struct spi_cs_info info;
244 * Ask the driver. For the moment we don't have CS info.
245 * When we do we could provide the driver with a helper function
246 * to figure out what chip selects are valid, or just handle the
249 ops = spi_get_ops(bus);
251 ret = ops->cs_info(bus, cs, &info);
254 * We could assume there is at least one valid chip select.
255 * The driver didn't care enough to tell us.
261 dev_err(bus, "Invalid cs %d (err=%d)\n", cs, ret);
265 for (device_find_first_child(bus, &dev); dev;
266 device_find_next_child(&dev)) {
267 struct dm_spi_slave_plat *plat;
269 plat = dev_get_parent_plat(dev);
270 dev_dbg(bus, "%s: plat=%p, cs=%d\n", __func__, plat, plat->cs);
271 if (plat->cs == cs) {
280 int spi_cs_is_valid(unsigned int busnum, unsigned int cs)
282 struct spi_cs_info info;
286 ret = uclass_find_device_by_seq(UCLASS_SPI, busnum, &bus);
288 log_debug("%s: No bus %d\n", __func__, busnum);
292 return spi_cs_info(bus, cs, &info);
295 int spi_cs_info(struct udevice *bus, uint cs, struct spi_cs_info *info)
297 struct spi_cs_info local_info;
303 /* If there is a device attached, return it */
305 ret = spi_find_chip_select(bus, cs, &info->dev);
306 return ret == -ENODEV ? 0 : ret;
309 int spi_find_bus_and_cs(int busnum, int cs, struct udevice **busp,
310 struct udevice **devp)
312 struct udevice *bus, *dev;
315 ret = uclass_find_device_by_seq(UCLASS_SPI, busnum, &bus);
317 log_debug("%s: No bus %d\n", __func__, busnum);
320 ret = spi_find_chip_select(bus, cs, &dev);
322 dev_dbg(bus, "%s: No cs %d\n", __func__, cs);
331 int spi_get_bus_and_cs(int busnum, int cs, int speed, int mode,
332 const char *drv_name, const char *dev_name,
333 struct udevice **busp, struct spi_slave **devp)
335 struct udevice *bus, *dev;
336 struct dm_spi_slave_plat *plat;
337 struct dm_spi_bus *bus_data;
338 struct spi_slave *slave;
339 bool created = false;
342 #if CONFIG_IS_ENABLED(OF_PLATDATA)
343 ret = uclass_first_device_err(UCLASS_SPI, &bus);
345 ret = uclass_get_device_by_seq(UCLASS_SPI, busnum, &bus);
348 log_err("Invalid bus %d (err=%d)\n", busnum, ret);
351 ret = spi_find_chip_select(bus, cs, &dev);
354 * If there is no such device, create one automatically. This means
355 * that we don't need a device tree node or platform data for the
356 * SPI flash chip - we will bind to the correct driver.
358 if (ret == -ENODEV && drv_name) {
359 dev_dbg(bus, "%s: Binding new device '%s', busnum=%d, cs=%d, driver=%s\n",
360 __func__, dev_name, busnum, cs, drv_name);
361 ret = device_bind_driver(bus, drv_name, dev_name, &dev);
363 dev_dbg(bus, "%s: Unable to bind driver (ret=%d)\n",
367 plat = dev_get_parent_plat(dev);
370 plat->max_hz = speed;
373 "Warning: SPI speed fallback to %u kHz\n",
374 SPI_DEFAULT_SPEED_HZ / 1000);
375 plat->max_hz = SPI_DEFAULT_SPEED_HZ;
380 dev_err(bus, "Invalid chip select %d:%d (err=%d)\n", busnum, cs, ret);
384 if (!device_active(dev)) {
385 struct spi_slave *slave;
387 ret = device_probe(dev);
390 slave = dev_get_parent_priv(dev);
394 slave = dev_get_parent_priv(dev);
395 bus_data = dev_get_uclass_priv(bus);
398 * In case the operation speed is not yet established by
399 * dm_spi_claim_bus() ensure the bus is configured properly.
401 if (!bus_data->speed) {
402 ret = spi_claim_bus(slave);
409 log_debug("%s: bus=%p, slave=%p\n", __func__, bus, *devp);
414 log_debug("%s: Error path, created=%d, device '%s'\n", __func__,
417 device_remove(dev, DM_REMOVE_NORMAL);
424 /* Compatibility function - to be removed */
425 struct spi_slave *spi_setup_slave(unsigned int busnum, unsigned int cs,
426 unsigned int speed, unsigned int mode)
428 struct spi_slave *slave;
432 ret = spi_get_bus_and_cs(busnum, cs, speed, mode, NULL, 0, &dev,
440 void spi_free_slave(struct spi_slave *slave)
442 device_remove(slave->dev, DM_REMOVE_NORMAL);
445 int spi_slave_of_to_plat(struct udevice *dev, struct dm_spi_slave_plat *plat)
450 plat->cs = dev_read_u32_default(dev, "reg", -1);
451 plat->max_hz = dev_read_u32_default(dev, "spi-max-frequency",
452 SPI_DEFAULT_SPEED_HZ);
453 if (dev_read_bool(dev, "spi-cpol"))
455 if (dev_read_bool(dev, "spi-cpha"))
457 if (dev_read_bool(dev, "spi-cs-high"))
459 if (dev_read_bool(dev, "spi-3wire"))
461 if (dev_read_bool(dev, "spi-half-duplex"))
462 mode |= SPI_PREAMBLE;
464 /* Device DUAL/QUAD mode */
465 value = dev_read_u32_default(dev, "spi-tx-bus-width", 1);
476 mode |= SPI_TX_OCTAL;
479 warn_non_spl("spi-tx-bus-width %d not supported\n", value);
483 value = dev_read_u32_default(dev, "spi-rx-bus-width", 1);
494 mode |= SPI_RX_OCTAL;
497 warn_non_spl("spi-rx-bus-width %d not supported\n", value);
506 UCLASS_DRIVER(spi) = {
509 .flags = DM_UC_FLAG_SEQ_ALIAS,
510 #if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
511 .post_bind = dm_scan_fdt_dev,
513 .post_probe = spi_post_probe,
514 .child_pre_probe = spi_child_pre_probe,
515 .per_device_auto = sizeof(struct dm_spi_bus),
516 .per_child_auto = sizeof(struct spi_slave),
517 .per_child_plat_auto = sizeof(struct dm_spi_slave_plat),
518 #if !CONFIG_IS_ENABLED(OF_PLATDATA)
519 .child_post_bind = spi_child_post_bind,
523 UCLASS_DRIVER(spi_generic) = {
524 .id = UCLASS_SPI_GENERIC,
525 .name = "spi_generic",
528 U_BOOT_DRIVER(spi_generic_drv) = {
529 .name = "spi_generic_drv",
530 .id = UCLASS_SPI_GENERIC,