2 * Copyright (c) 2014 Google, Inc
4 * SPDX-License-Identifier: GPL-2.0+
12 #include <dm/device-internal.h>
13 #include <dm/uclass-internal.h>
17 DECLARE_GLOBAL_DATA_PTR;
19 static int spi_set_speed_mode(struct udevice *bus, int speed, int mode)
21 struct dm_spi_ops *ops;
24 ops = spi_get_ops(bus);
26 ret = ops->set_speed(bus, speed);
30 printf("Cannot set speed (err=%d)\n", ret);
35 ret = ops->set_mode(bus, mode);
39 printf("Cannot set mode (err=%d)\n", ret);
46 int dm_spi_claim_bus(struct udevice *dev)
48 struct udevice *bus = dev->parent;
49 struct dm_spi_ops *ops = spi_get_ops(bus);
50 struct dm_spi_bus *spi = dev_get_uclass_priv(bus);
51 struct spi_slave *slave = dev_get_parent_priv(dev);
55 speed = slave->max_hz;
58 speed = min(speed, (int)spi->max_hz);
64 if (speed != slave->speed) {
65 ret = spi_set_speed_mode(bus, speed, slave->mode);
71 return ops->claim_bus ? ops->claim_bus(dev) : 0;
74 void dm_spi_release_bus(struct udevice *dev)
76 struct udevice *bus = dev->parent;
77 struct dm_spi_ops *ops = spi_get_ops(bus);
80 ops->release_bus(dev);
83 int dm_spi_xfer(struct udevice *dev, unsigned int bitlen,
84 const void *dout, void *din, unsigned long flags)
86 struct udevice *bus = dev->parent;
88 if (bus->uclass->uc_drv->id != UCLASS_SPI)
91 return spi_get_ops(bus)->xfer(dev, bitlen, dout, din, flags);
94 int spi_claim_bus(struct spi_slave *slave)
96 return dm_spi_claim_bus(slave->dev);
99 void spi_release_bus(struct spi_slave *slave)
101 dm_spi_release_bus(slave->dev);
104 int spi_xfer(struct spi_slave *slave, unsigned int bitlen,
105 const void *dout, void *din, unsigned long flags)
107 return dm_spi_xfer(slave->dev, bitlen, dout, din, flags);
110 #if !CONFIG_IS_ENABLED(OF_PLATDATA)
111 static int spi_child_post_bind(struct udevice *dev)
113 struct dm_spi_slave_platdata *plat = dev_get_parent_platdata(dev);
115 if (!dev_of_valid(dev))
118 return spi_slave_ofdata_to_platdata(dev, plat);
122 static int spi_post_probe(struct udevice *bus)
124 #if !CONFIG_IS_ENABLED(OF_PLATDATA)
125 struct dm_spi_bus *spi = dev_get_uclass_priv(bus);
127 spi->max_hz = dev_read_u32_default(bus, "spi-max-frequency", 0);
129 #if defined(CONFIG_NEEDS_MANUAL_RELOC)
130 struct dm_spi_ops *ops = spi_get_ops(bus);
134 ops->claim_bus += gd->reloc_off;
135 if (ops->release_bus)
136 ops->release_bus += gd->reloc_off;
137 if (ops->set_wordlen)
138 ops->set_wordlen += gd->reloc_off;
140 ops->xfer += gd->reloc_off;
142 ops->set_speed += gd->reloc_off;
144 ops->set_mode += gd->reloc_off;
146 ops->cs_info += gd->reloc_off;
152 static int spi_child_pre_probe(struct udevice *dev)
154 struct dm_spi_slave_platdata *plat = dev_get_parent_platdata(dev);
155 struct spi_slave *slave = dev_get_parent_priv(dev);
158 * This is needed because we pass struct spi_slave around the place
159 * instead slave->dev (a struct udevice). So we have to have some
160 * way to access the slave udevice given struct spi_slave. Once we
161 * change the SPI API to use udevice instead of spi_slave, we can
166 slave->max_hz = plat->max_hz;
167 slave->mode = plat->mode;
168 slave->wordlen = SPI_DEFAULT_WORDLEN;
173 int spi_chip_select(struct udevice *dev)
175 struct dm_spi_slave_platdata *plat = dev_get_parent_platdata(dev);
177 return plat ? plat->cs : -ENOENT;
180 int spi_find_chip_select(struct udevice *bus, int cs, struct udevice **devp)
184 for (device_find_first_child(bus, &dev); dev;
185 device_find_next_child(&dev)) {
186 struct dm_spi_slave_platdata *plat;
188 plat = dev_get_parent_platdata(dev);
189 debug("%s: plat=%p, cs=%d\n", __func__, plat, plat->cs);
190 if (plat->cs == cs) {
199 int spi_cs_is_valid(unsigned int busnum, unsigned int cs)
201 struct spi_cs_info info;
205 ret = uclass_find_device_by_seq(UCLASS_SPI, busnum, false, &bus);
207 debug("%s: No bus %d\n", __func__, busnum);
211 return spi_cs_info(bus, cs, &info);
214 int spi_cs_info(struct udevice *bus, uint cs, struct spi_cs_info *info)
216 struct spi_cs_info local_info;
217 struct dm_spi_ops *ops;
223 /* If there is a device attached, return it */
225 ret = spi_find_chip_select(bus, cs, &info->dev);
230 * Otherwise ask the driver. For the moment we don't have CS info.
231 * When we do we could provide the driver with a helper function
232 * to figure out what chip selects are valid, or just handle the
235 ops = spi_get_ops(bus);
237 return ops->cs_info(bus, cs, info);
240 * We could assume there is at least one valid chip select, but best
241 * to be sure and return an error in this case. The driver didn't
242 * care enough to tell us.
247 int spi_find_bus_and_cs(int busnum, int cs, struct udevice **busp,
248 struct udevice **devp)
250 struct udevice *bus, *dev;
253 ret = uclass_find_device_by_seq(UCLASS_SPI, busnum, false, &bus);
255 debug("%s: No bus %d\n", __func__, busnum);
258 ret = spi_find_chip_select(bus, cs, &dev);
260 debug("%s: No cs %d\n", __func__, cs);
269 int spi_get_bus_and_cs(int busnum, int cs, int speed, int mode,
270 const char *drv_name, const char *dev_name,
271 struct udevice **busp, struct spi_slave **devp)
273 struct udevice *bus, *dev;
274 struct dm_spi_slave_platdata *plat;
275 bool created = false;
278 #if CONFIG_IS_ENABLED(OF_PLATDATA)
279 ret = uclass_first_device_err(UCLASS_SPI, &bus);
281 ret = uclass_get_device_by_seq(UCLASS_SPI, busnum, &bus);
284 printf("Invalid bus %d (err=%d)\n", busnum, ret);
287 ret = spi_find_chip_select(bus, cs, &dev);
290 * If there is no such device, create one automatically. This means
291 * that we don't need a device tree node or platform data for the
292 * SPI flash chip - we will bind to the correct driver.
294 if (ret == -ENODEV && drv_name) {
295 debug("%s: Binding new device '%s', busnum=%d, cs=%d, driver=%s\n",
296 __func__, dev_name, busnum, cs, drv_name);
297 ret = device_bind_driver(bus, drv_name, dev_name, &dev);
299 debug("%s: Unable to bind driver (ret=%d)\n", __func__,
303 plat = dev_get_parent_platdata(dev);
305 plat->max_hz = speed;
309 printf("Invalid chip select %d:%d (err=%d)\n", busnum, cs,
314 if (!device_active(dev)) {
315 struct spi_slave *slave;
317 ret = device_probe(dev);
320 slave = dev_get_parent_priv(dev);
324 plat = dev_get_parent_platdata(dev);
326 speed = plat->max_hz;
329 ret = spi_set_speed_mode(bus, speed, mode);
334 *devp = dev_get_parent_priv(dev);
335 debug("%s: bus=%p, slave=%p\n", __func__, bus, *devp);
340 debug("%s: Error path, created=%d, device '%s'\n", __func__,
343 device_remove(dev, DM_REMOVE_NORMAL);
350 /* Compatibility function - to be removed */
351 struct spi_slave *spi_setup_slave_fdt(const void *blob, int node,
354 struct udevice *bus, *dev;
357 ret = uclass_get_device_by_of_offset(UCLASS_SPI, bus_node, &bus);
360 ret = device_get_child_by_of_offset(bus, node, &dev);
363 return dev_get_parent_priv(dev);
366 /* Compatibility function - to be removed */
367 struct spi_slave *spi_setup_slave(unsigned int busnum, unsigned int cs,
368 unsigned int speed, unsigned int mode)
370 struct spi_slave *slave;
374 ret = spi_get_bus_and_cs(busnum, cs, speed, mode, NULL, 0, &dev,
382 void spi_free_slave(struct spi_slave *slave)
384 device_remove(slave->dev, DM_REMOVE_NORMAL);
388 int spi_slave_ofdata_to_platdata(struct udevice *dev,
389 struct dm_spi_slave_platdata *plat)
394 plat->cs = dev_read_u32_default(dev, "reg", -1);
395 plat->max_hz = dev_read_u32_default(dev, "spi-max-frequency", 0);
396 if (dev_read_bool(dev, "spi-cpol"))
398 if (dev_read_bool(dev, "spi-cpha"))
400 if (dev_read_bool(dev, "spi-cs-high"))
402 if (dev_read_bool(dev, "spi-3wire"))
404 if (dev_read_bool(dev, "spi-half-duplex"))
405 mode |= SPI_PREAMBLE;
407 /* Device DUAL/QUAD mode */
408 value = dev_read_u32_default(dev, "spi-tx-bus-width", 1);
419 warn_non_spl("spi-tx-bus-width %d not supported\n", value);
423 value = dev_read_u32_default(dev, "spi-rx-bus-width", 1);
434 warn_non_spl("spi-rx-bus-width %d not supported\n", value);
443 UCLASS_DRIVER(spi) = {
446 .flags = DM_UC_FLAG_SEQ_ALIAS,
447 #if !CONFIG_IS_ENABLED(OF_PLATDATA)
448 .post_bind = dm_scan_fdt_dev,
450 .post_probe = spi_post_probe,
451 .child_pre_probe = spi_child_pre_probe,
452 .per_device_auto_alloc_size = sizeof(struct dm_spi_bus),
453 .per_child_auto_alloc_size = sizeof(struct spi_slave),
454 .per_child_platdata_auto_alloc_size =
455 sizeof(struct dm_spi_slave_platdata),
456 #if !CONFIG_IS_ENABLED(OF_PLATDATA)
457 .child_post_bind = spi_child_post_bind,
461 UCLASS_DRIVER(spi_generic) = {
462 .id = UCLASS_SPI_GENERIC,
463 .name = "spi_generic",
466 U_BOOT_DRIVER(spi_generic_drv) = {
467 .name = "spi_generic_drv",
468 .id = UCLASS_SPI_GENERIC,