1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2014 Google, Inc
11 #include <dm/device-internal.h>
12 #include <dm/uclass-internal.h>
16 DECLARE_GLOBAL_DATA_PTR;
18 #define SPI_DEFAULT_SPEED_HZ 100000
20 static int spi_set_speed_mode(struct udevice *bus, int speed, int mode)
22 struct dm_spi_ops *ops;
25 ops = spi_get_ops(bus);
27 ret = ops->set_speed(bus, speed);
31 printf("Cannot set speed (err=%d)\n", ret);
36 ret = ops->set_mode(bus, mode);
40 printf("Cannot set mode (err=%d)\n", ret);
47 int dm_spi_claim_bus(struct udevice *dev)
49 struct udevice *bus = dev->parent;
50 struct dm_spi_ops *ops = spi_get_ops(bus);
51 struct dm_spi_bus *spi = dev_get_uclass_priv(bus);
52 struct spi_slave *slave = dev_get_parent_priv(dev);
55 speed = slave->max_hz;
58 speed = min(speed, (int)spi->max_hz);
63 speed = SPI_DEFAULT_SPEED_HZ;
64 if (speed != slave->speed) {
65 int ret = spi_set_speed_mode(bus, speed, slave->mode);
72 return log_ret(ops->claim_bus ? ops->claim_bus(dev) : 0);
75 void dm_spi_release_bus(struct udevice *dev)
77 struct udevice *bus = dev->parent;
78 struct dm_spi_ops *ops = spi_get_ops(bus);
81 ops->release_bus(dev);
84 int dm_spi_xfer(struct udevice *dev, unsigned int bitlen,
85 const void *dout, void *din, unsigned long flags)
87 struct udevice *bus = dev->parent;
89 if (bus->uclass->uc_drv->id != UCLASS_SPI)
92 return spi_get_ops(bus)->xfer(dev, bitlen, dout, din, flags);
95 int spi_claim_bus(struct spi_slave *slave)
97 return log_ret(dm_spi_claim_bus(slave->dev));
100 void spi_release_bus(struct spi_slave *slave)
102 dm_spi_release_bus(slave->dev);
105 int spi_xfer(struct spi_slave *slave, unsigned int bitlen,
106 const void *dout, void *din, unsigned long flags)
108 return dm_spi_xfer(slave->dev, bitlen, dout, din, flags);
111 int spi_write_then_read(struct spi_slave *slave, const u8 *opcode,
112 size_t n_opcode, const u8 *txbuf, u8 *rxbuf,
115 unsigned long flags = SPI_XFER_BEGIN;
119 flags |= SPI_XFER_END;
121 ret = spi_xfer(slave, n_opcode * 8, opcode, NULL, flags);
123 debug("spi: failed to send command (%zu bytes): %d\n",
125 } else if (n_buf != 0) {
126 ret = spi_xfer(slave, n_buf * 8, txbuf, rxbuf, SPI_XFER_END);
128 debug("spi: failed to transfer %zu bytes of data: %d\n",
135 #if !CONFIG_IS_ENABLED(OF_PLATDATA)
136 static int spi_child_post_bind(struct udevice *dev)
138 struct dm_spi_slave_platdata *plat = dev_get_parent_platdata(dev);
140 if (!dev_of_valid(dev))
143 return spi_slave_ofdata_to_platdata(dev, plat);
147 static int spi_post_probe(struct udevice *bus)
149 #if !CONFIG_IS_ENABLED(OF_PLATDATA)
150 struct dm_spi_bus *spi = dev_get_uclass_priv(bus);
152 spi->max_hz = dev_read_u32_default(bus, "spi-max-frequency", 0);
154 #if defined(CONFIG_NEEDS_MANUAL_RELOC)
155 struct dm_spi_ops *ops = spi_get_ops(bus);
158 ops->claim_bus += gd->reloc_off;
159 if (ops->release_bus)
160 ops->release_bus += gd->reloc_off;
161 if (ops->set_wordlen)
162 ops->set_wordlen += gd->reloc_off;
164 ops->xfer += gd->reloc_off;
166 ops->set_speed += gd->reloc_off;
168 ops->set_mode += gd->reloc_off;
170 ops->cs_info += gd->reloc_off;
176 static int spi_child_pre_probe(struct udevice *dev)
178 struct dm_spi_slave_platdata *plat = dev_get_parent_platdata(dev);
179 struct spi_slave *slave = dev_get_parent_priv(dev);
182 * This is needed because we pass struct spi_slave around the place
183 * instead slave->dev (a struct udevice). So we have to have some
184 * way to access the slave udevice given struct spi_slave. Once we
185 * change the SPI API to use udevice instead of spi_slave, we can
190 slave->max_hz = plat->max_hz;
191 slave->mode = plat->mode;
192 slave->wordlen = SPI_DEFAULT_WORDLEN;
197 int spi_chip_select(struct udevice *dev)
199 struct dm_spi_slave_platdata *plat = dev_get_parent_platdata(dev);
201 return plat ? plat->cs : -ENOENT;
204 int spi_find_chip_select(struct udevice *bus, int cs, struct udevice **devp)
208 for (device_find_first_child(bus, &dev); dev;
209 device_find_next_child(&dev)) {
210 struct dm_spi_slave_platdata *plat;
212 plat = dev_get_parent_platdata(dev);
213 debug("%s: plat=%p, cs=%d\n", __func__, plat, plat->cs);
214 if (plat->cs == cs) {
223 int spi_cs_is_valid(unsigned int busnum, unsigned int cs)
225 struct spi_cs_info info;
229 ret = uclass_find_device_by_seq(UCLASS_SPI, busnum, false, &bus);
231 debug("%s: No bus %d\n", __func__, busnum);
235 return spi_cs_info(bus, cs, &info);
238 int spi_cs_info(struct udevice *bus, uint cs, struct spi_cs_info *info)
240 struct spi_cs_info local_info;
241 struct dm_spi_ops *ops;
247 /* If there is a device attached, return it */
249 ret = spi_find_chip_select(bus, cs, &info->dev);
254 * Otherwise ask the driver. For the moment we don't have CS info.
255 * When we do we could provide the driver with a helper function
256 * to figure out what chip selects are valid, or just handle the
259 ops = spi_get_ops(bus);
261 return ops->cs_info(bus, cs, info);
264 * We could assume there is at least one valid chip select.
265 * The driver didn't care enough to tell us.
270 int spi_find_bus_and_cs(int busnum, int cs, struct udevice **busp,
271 struct udevice **devp)
273 struct udevice *bus, *dev;
276 ret = uclass_find_device_by_seq(UCLASS_SPI, busnum, false, &bus);
278 debug("%s: No bus %d\n", __func__, busnum);
281 ret = spi_find_chip_select(bus, cs, &dev);
283 debug("%s: No cs %d\n", __func__, cs);
292 int spi_get_bus_and_cs(int busnum, int cs, int speed, int mode,
293 const char *drv_name, const char *dev_name,
294 struct udevice **busp, struct spi_slave **devp)
296 struct udevice *bus, *dev;
297 struct dm_spi_slave_platdata *plat;
298 bool created = false;
301 #if CONFIG_IS_ENABLED(OF_PLATDATA)
302 ret = uclass_first_device_err(UCLASS_SPI, &bus);
304 ret = uclass_get_device_by_seq(UCLASS_SPI, busnum, &bus);
307 printf("Invalid bus %d (err=%d)\n", busnum, ret);
310 ret = spi_find_chip_select(bus, cs, &dev);
313 * If there is no such device, create one automatically. This means
314 * that we don't need a device tree node or platform data for the
315 * SPI flash chip - we will bind to the correct driver.
317 if (ret == -ENODEV && drv_name) {
318 debug("%s: Binding new device '%s', busnum=%d, cs=%d, driver=%s\n",
319 __func__, dev_name, busnum, cs, drv_name);
320 ret = device_bind_driver(bus, drv_name, dev_name, &dev);
322 debug("%s: Unable to bind driver (ret=%d)\n", __func__,
326 plat = dev_get_parent_platdata(dev);
329 plat->max_hz = speed;
331 printf("Warning: SPI speed fallback to %u kHz\n",
332 SPI_DEFAULT_SPEED_HZ / 1000);
333 plat->max_hz = SPI_DEFAULT_SPEED_HZ;
338 printf("Invalid chip select %d:%d (err=%d)\n", busnum, cs,
343 if (!device_active(dev)) {
344 struct spi_slave *slave;
346 ret = device_probe(dev);
349 slave = dev_get_parent_priv(dev);
353 plat = dev_get_parent_platdata(dev);
355 /* get speed and mode from platdata when available */
357 speed = plat->max_hz;
360 ret = spi_set_speed_mode(bus, speed, mode);
365 *devp = dev_get_parent_priv(dev);
366 debug("%s: bus=%p, slave=%p\n", __func__, bus, *devp);
371 debug("%s: Error path, created=%d, device '%s'\n", __func__,
374 device_remove(dev, DM_REMOVE_NORMAL);
381 /* Compatibility function - to be removed */
382 struct spi_slave *spi_setup_slave(unsigned int busnum, unsigned int cs,
383 unsigned int speed, unsigned int mode)
385 struct spi_slave *slave;
389 ret = spi_get_bus_and_cs(busnum, cs, speed, mode, NULL, 0, &dev,
397 void spi_free_slave(struct spi_slave *slave)
399 device_remove(slave->dev, DM_REMOVE_NORMAL);
403 int spi_slave_ofdata_to_platdata(struct udevice *dev,
404 struct dm_spi_slave_platdata *plat)
409 plat->cs = dev_read_u32_default(dev, "reg", -1);
410 plat->max_hz = dev_read_u32_default(dev, "spi-max-frequency",
411 SPI_DEFAULT_SPEED_HZ);
412 if (dev_read_bool(dev, "spi-cpol"))
414 if (dev_read_bool(dev, "spi-cpha"))
416 if (dev_read_bool(dev, "spi-cs-high"))
418 if (dev_read_bool(dev, "spi-3wire"))
420 if (dev_read_bool(dev, "spi-half-duplex"))
421 mode |= SPI_PREAMBLE;
423 /* Device DUAL/QUAD mode */
424 value = dev_read_u32_default(dev, "spi-tx-bus-width", 1);
435 warn_non_spl("spi-tx-bus-width %d not supported\n", value);
439 value = dev_read_u32_default(dev, "spi-rx-bus-width", 1);
450 warn_non_spl("spi-rx-bus-width %d not supported\n", value);
459 UCLASS_DRIVER(spi) = {
462 .flags = DM_UC_FLAG_SEQ_ALIAS,
463 #if !CONFIG_IS_ENABLED(OF_PLATDATA)
464 .post_bind = dm_scan_fdt_dev,
466 .post_probe = spi_post_probe,
467 .child_pre_probe = spi_child_pre_probe,
468 .per_device_auto_alloc_size = sizeof(struct dm_spi_bus),
469 .per_child_auto_alloc_size = sizeof(struct spi_slave),
470 .per_child_platdata_auto_alloc_size =
471 sizeof(struct dm_spi_slave_platdata),
472 #if !CONFIG_IS_ENABLED(OF_PLATDATA)
473 .child_post_bind = spi_child_post_bind,
477 UCLASS_DRIVER(spi_generic) = {
478 .id = UCLASS_SPI_GENERIC,
479 .name = "spi_generic",
482 U_BOOT_DRIVER(spi_generic_drv) = {
483 .name = "spi_generic_drv",
484 .id = UCLASS_SPI_GENERIC,