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 static int spi_set_speed_mode(struct udevice *bus, int speed, int mode)
20 struct dm_spi_ops *ops;
23 ops = spi_get_ops(bus);
25 ret = ops->set_speed(bus, speed);
29 printf("Cannot set speed (err=%d)\n", ret);
34 ret = ops->set_mode(bus, mode);
38 printf("Cannot set mode (err=%d)\n", ret);
45 int dm_spi_claim_bus(struct udevice *dev)
47 struct udevice *bus = dev->parent;
48 struct dm_spi_ops *ops = spi_get_ops(bus);
49 struct dm_spi_bus *spi = dev_get_uclass_priv(bus);
50 struct spi_slave *slave = dev_get_parent_priv(dev);
53 speed = slave->max_hz;
56 speed = min(speed, (int)spi->max_hz);
62 if (speed != slave->speed) {
63 int ret = spi_set_speed_mode(bus, speed, slave->mode);
70 return ops->claim_bus ? ops->claim_bus(dev) : 0;
73 void dm_spi_release_bus(struct udevice *dev)
75 struct udevice *bus = dev->parent;
76 struct dm_spi_ops *ops = spi_get_ops(bus);
79 ops->release_bus(dev);
82 int dm_spi_xfer(struct udevice *dev, unsigned int bitlen,
83 const void *dout, void *din, unsigned long flags)
85 struct udevice *bus = dev->parent;
87 if (bus->uclass->uc_drv->id != UCLASS_SPI)
90 return spi_get_ops(bus)->xfer(dev, bitlen, dout, din, flags);
93 int spi_claim_bus(struct spi_slave *slave)
95 return dm_spi_claim_bus(slave->dev);
98 void spi_release_bus(struct spi_slave *slave)
100 dm_spi_release_bus(slave->dev);
103 int spi_xfer(struct spi_slave *slave, unsigned int bitlen,
104 const void *dout, void *din, unsigned long flags)
106 return dm_spi_xfer(slave->dev, bitlen, dout, din, flags);
109 #if !CONFIG_IS_ENABLED(OF_PLATDATA)
110 static int spi_child_post_bind(struct udevice *dev)
112 struct dm_spi_slave_platdata *plat = dev_get_parent_platdata(dev);
114 if (!dev_of_valid(dev))
117 return spi_slave_ofdata_to_platdata(dev, plat);
121 static int spi_post_probe(struct udevice *bus)
123 #if !CONFIG_IS_ENABLED(OF_PLATDATA)
124 struct dm_spi_bus *spi = dev_get_uclass_priv(bus);
126 spi->max_hz = dev_read_u32_default(bus, "spi-max-frequency", 0);
128 #if defined(CONFIG_NEEDS_MANUAL_RELOC)
129 struct dm_spi_ops *ops = spi_get_ops(bus);
132 ops->claim_bus += gd->reloc_off;
133 if (ops->release_bus)
134 ops->release_bus += gd->reloc_off;
135 if (ops->set_wordlen)
136 ops->set_wordlen += gd->reloc_off;
138 ops->xfer += gd->reloc_off;
140 ops->set_speed += gd->reloc_off;
142 ops->set_mode += gd->reloc_off;
144 ops->cs_info += gd->reloc_off;
150 static int spi_child_pre_probe(struct udevice *dev)
152 struct dm_spi_slave_platdata *plat = dev_get_parent_platdata(dev);
153 struct spi_slave *slave = dev_get_parent_priv(dev);
156 * This is needed because we pass struct spi_slave around the place
157 * instead slave->dev (a struct udevice). So we have to have some
158 * way to access the slave udevice given struct spi_slave. Once we
159 * change the SPI API to use udevice instead of spi_slave, we can
164 slave->max_hz = plat->max_hz;
165 slave->mode = plat->mode;
166 slave->wordlen = SPI_DEFAULT_WORDLEN;
171 int spi_chip_select(struct udevice *dev)
173 struct dm_spi_slave_platdata *plat = dev_get_parent_platdata(dev);
175 return plat ? plat->cs : -ENOENT;
178 int spi_find_chip_select(struct udevice *bus, int cs, struct udevice **devp)
182 for (device_find_first_child(bus, &dev); dev;
183 device_find_next_child(&dev)) {
184 struct dm_spi_slave_platdata *plat;
186 plat = dev_get_parent_platdata(dev);
187 debug("%s: plat=%p, cs=%d\n", __func__, plat, plat->cs);
188 if (plat->cs == cs) {
197 int spi_cs_is_valid(unsigned int busnum, unsigned int cs)
199 struct spi_cs_info info;
203 ret = uclass_find_device_by_seq(UCLASS_SPI, busnum, false, &bus);
205 debug("%s: No bus %d\n", __func__, busnum);
209 return spi_cs_info(bus, cs, &info);
212 int spi_cs_info(struct udevice *bus, uint cs, struct spi_cs_info *info)
214 struct spi_cs_info local_info;
215 struct dm_spi_ops *ops;
221 /* If there is a device attached, return it */
223 ret = spi_find_chip_select(bus, cs, &info->dev);
228 * Otherwise ask the driver. For the moment we don't have CS info.
229 * When we do we could provide the driver with a helper function
230 * to figure out what chip selects are valid, or just handle the
233 ops = spi_get_ops(bus);
235 return ops->cs_info(bus, cs, info);
238 * We could assume there is at least one valid chip select, but best
239 * to be sure and return an error in this case. The driver didn't
240 * care enough to tell us.
245 int spi_find_bus_and_cs(int busnum, int cs, struct udevice **busp,
246 struct udevice **devp)
248 struct udevice *bus, *dev;
251 ret = uclass_find_device_by_seq(UCLASS_SPI, busnum, false, &bus);
253 debug("%s: No bus %d\n", __func__, busnum);
256 ret = spi_find_chip_select(bus, cs, &dev);
258 debug("%s: No cs %d\n", __func__, cs);
267 int spi_get_bus_and_cs(int busnum, int cs, int speed, int mode,
268 const char *drv_name, const char *dev_name,
269 struct udevice **busp, struct spi_slave **devp)
271 struct udevice *bus, *dev;
272 struct dm_spi_slave_platdata *plat;
273 bool created = false;
276 #if CONFIG_IS_ENABLED(OF_PLATDATA) || CONFIG_IS_ENABLED(OF_PRIOR_STAGE)
277 ret = uclass_first_device_err(UCLASS_SPI, &bus);
279 ret = uclass_get_device_by_seq(UCLASS_SPI, busnum, &bus);
282 printf("Invalid bus %d (err=%d)\n", busnum, ret);
285 ret = spi_find_chip_select(bus, cs, &dev);
288 * If there is no such device, create one automatically. This means
289 * that we don't need a device tree node or platform data for the
290 * SPI flash chip - we will bind to the correct driver.
292 if (ret == -ENODEV && drv_name) {
293 debug("%s: Binding new device '%s', busnum=%d, cs=%d, driver=%s\n",
294 __func__, dev_name, busnum, cs, drv_name);
295 ret = device_bind_driver(bus, drv_name, dev_name, &dev);
297 debug("%s: Unable to bind driver (ret=%d)\n", __func__,
301 plat = dev_get_parent_platdata(dev);
303 plat->max_hz = speed;
307 printf("Invalid chip select %d:%d (err=%d)\n", busnum, cs,
312 if (!device_active(dev)) {
313 struct spi_slave *slave;
315 ret = device_probe(dev);
318 slave = dev_get_parent_priv(dev);
322 plat = dev_get_parent_platdata(dev);
324 speed = plat->max_hz;
327 ret = spi_set_speed_mode(bus, speed, mode);
332 *devp = dev_get_parent_priv(dev);
333 debug("%s: bus=%p, slave=%p\n", __func__, bus, *devp);
338 debug("%s: Error path, created=%d, device '%s'\n", __func__,
341 device_remove(dev, DM_REMOVE_NORMAL);
348 /* Compatibility function - to be removed */
349 struct spi_slave *spi_setup_slave(unsigned int busnum, unsigned int cs,
350 unsigned int speed, unsigned int mode)
352 struct spi_slave *slave;
356 ret = spi_get_bus_and_cs(busnum, cs, speed, mode, NULL, 0, &dev,
364 void spi_free_slave(struct spi_slave *slave)
366 device_remove(slave->dev, DM_REMOVE_NORMAL);
370 int spi_slave_ofdata_to_platdata(struct udevice *dev,
371 struct dm_spi_slave_platdata *plat)
376 plat->cs = dev_read_u32_default(dev, "reg", -1);
377 plat->max_hz = dev_read_u32_default(dev, "spi-max-frequency", 0);
378 if (dev_read_bool(dev, "spi-cpol"))
380 if (dev_read_bool(dev, "spi-cpha"))
382 if (dev_read_bool(dev, "spi-cs-high"))
384 if (dev_read_bool(dev, "spi-3wire"))
386 if (dev_read_bool(dev, "spi-half-duplex"))
387 mode |= SPI_PREAMBLE;
389 /* Device DUAL/QUAD mode */
390 value = dev_read_u32_default(dev, "spi-tx-bus-width", 1);
401 warn_non_spl("spi-tx-bus-width %d not supported\n", value);
405 value = dev_read_u32_default(dev, "spi-rx-bus-width", 1);
416 warn_non_spl("spi-rx-bus-width %d not supported\n", value);
425 UCLASS_DRIVER(spi) = {
428 .flags = DM_UC_FLAG_SEQ_ALIAS,
429 #if !CONFIG_IS_ENABLED(OF_PLATDATA)
430 .post_bind = dm_scan_fdt_dev,
432 .post_probe = spi_post_probe,
433 .child_pre_probe = spi_child_pre_probe,
434 .per_device_auto_alloc_size = sizeof(struct dm_spi_bus),
435 .per_child_auto_alloc_size = sizeof(struct spi_slave),
436 .per_child_platdata_auto_alloc_size =
437 sizeof(struct dm_spi_slave_platdata),
438 #if !CONFIG_IS_ENABLED(OF_PLATDATA)
439 .child_post_bind = spi_child_post_bind,
443 UCLASS_DRIVER(spi_generic) = {
444 .id = UCLASS_SPI_GENERIC,
445 .name = "spi_generic",
448 U_BOOT_DRIVER(spi_generic_drv) = {
449 .name = "spi_generic_drv",
450 .id = UCLASS_SPI_GENERIC,