blob: f4795e68672fedbdc1613409d025c6edddb79355 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glassdd82d442014-10-13 23:41:52 -06002/*
3 * Copyright (c) 2014 Google, Inc
Simon Glassdd82d442014-10-13 23:41:52 -06004 */
5
Patrick Delaunay1d83e032020-10-15 17:18:17 +02006#define LOG_CATEGORY UCLASS_SPI
7
Simon Glassdd82d442014-10-13 23:41:52 -06008#include <common.h>
9#include <dm.h>
10#include <errno.h>
Simon Glass0f2af882020-05-10 11:40:05 -060011#include <log.h>
Simon Glassdd82d442014-10-13 23:41:52 -060012#include <malloc.h>
13#include <spi.h>
T Karthik Reddy626a7342021-03-17 12:31:30 +010014#include <spi-mem.h>
Patrick Delaunay1d83e032020-10-15 17:18:17 +020015#include <dm/device_compat.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060016#include <asm/global_data.h>
Simon Glassdd82d442014-10-13 23:41:52 -060017#include <dm/device-internal.h>
18#include <dm/uclass-internal.h>
Simon Glassdd82d442014-10-13 23:41:52 -060019#include <dm/lists.h>
20#include <dm/util.h>
21
22DECLARE_GLOBAL_DATA_PTR;
23
Simon Goldschmidta942e1a2018-10-30 21:09:48 +010024#define SPI_DEFAULT_SPEED_HZ 100000
25
Simon Glassdd82d442014-10-13 23:41:52 -060026static int spi_set_speed_mode(struct udevice *bus, int speed, int mode)
27{
28 struct dm_spi_ops *ops;
29 int ret;
30
31 ops = spi_get_ops(bus);
32 if (ops->set_speed)
33 ret = ops->set_speed(bus, speed);
34 else
35 ret = -EINVAL;
36 if (ret) {
Patrick Delaunay1d83e032020-10-15 17:18:17 +020037 dev_err(bus, "Cannot set speed (err=%d)\n", ret);
Simon Glassdd82d442014-10-13 23:41:52 -060038 return ret;
39 }
40
41 if (ops->set_mode)
42 ret = ops->set_mode(bus, mode);
43 else
44 ret = -EINVAL;
45 if (ret) {
Patrick Delaunay1d83e032020-10-15 17:18:17 +020046 dev_err(bus, "Cannot set mode (err=%d)\n", ret);
Simon Glassdd82d442014-10-13 23:41:52 -060047 return ret;
48 }
49
50 return 0;
51}
52
Peng Fanfdd88a32016-05-03 10:02:22 +080053int dm_spi_claim_bus(struct udevice *dev)
Simon Glassdd82d442014-10-13 23:41:52 -060054{
Simon Glassdd82d442014-10-13 23:41:52 -060055 struct udevice *bus = dev->parent;
56 struct dm_spi_ops *ops = spi_get_ops(bus);
Simon Glassde0977b2015-03-05 12:25:20 -070057 struct dm_spi_bus *spi = dev_get_uclass_priv(bus);
Peng Fanfdd88a32016-05-03 10:02:22 +080058 struct spi_slave *slave = dev_get_parent_priv(dev);
Ovidiu Panait40dcee12020-12-14 19:06:50 +020059 uint speed, mode;
Simon Glassdd82d442014-10-13 23:41:52 -060060
61 speed = slave->max_hz;
Ovidiu Panait40dcee12020-12-14 19:06:50 +020062 mode = slave->mode;
63
Simon Glassdd82d442014-10-13 23:41:52 -060064 if (spi->max_hz) {
65 if (speed)
Ovidiu Panait40dcee12020-12-14 19:06:50 +020066 speed = min(speed, spi->max_hz);
Simon Glassdd82d442014-10-13 23:41:52 -060067 else
68 speed = spi->max_hz;
69 }
70 if (!speed)
Simon Goldschmidta942e1a2018-10-30 21:09:48 +010071 speed = SPI_DEFAULT_SPEED_HZ;
Ovidiu Panait40dcee12020-12-14 19:06:50 +020072
73 if (speed != spi->speed || mode != spi->mode) {
Mario Six2d844dd2018-01-15 11:08:41 +010074 int ret = spi_set_speed_mode(bus, speed, slave->mode);
75
Simon Glassb46cb632015-02-17 15:29:35 -070076 if (ret)
Simon Glass00d99902018-10-01 12:22:24 -060077 return log_ret(ret);
Ovidiu Panait40dcee12020-12-14 19:06:50 +020078
79 spi->speed = speed;
80 spi->mode = mode;
Simon Glassb46cb632015-02-17 15:29:35 -070081 }
Simon Glassdd82d442014-10-13 23:41:52 -060082
Simon Glass00d99902018-10-01 12:22:24 -060083 return log_ret(ops->claim_bus ? ops->claim_bus(dev) : 0);
Simon Glassdd82d442014-10-13 23:41:52 -060084}
85
Peng Fanfdd88a32016-05-03 10:02:22 +080086void dm_spi_release_bus(struct udevice *dev)
Simon Glassdd82d442014-10-13 23:41:52 -060087{
Simon Glassdd82d442014-10-13 23:41:52 -060088 struct udevice *bus = dev->parent;
89 struct dm_spi_ops *ops = spi_get_ops(bus);
90
91 if (ops->release_bus)
Simon Glass5c74fba2015-04-19 09:05:40 -060092 ops->release_bus(dev);
Simon Glassdd82d442014-10-13 23:41:52 -060093}
94
Peng Fanfdd88a32016-05-03 10:02:22 +080095int dm_spi_xfer(struct udevice *dev, unsigned int bitlen,
96 const void *dout, void *din, unsigned long flags)
Simon Glassdd82d442014-10-13 23:41:52 -060097{
Simon Glassdd82d442014-10-13 23:41:52 -060098 struct udevice *bus = dev->parent;
Simon Glass2d2e8602019-12-06 21:42:35 -070099 struct dm_spi_ops *ops = spi_get_ops(bus);
Simon Glassdd82d442014-10-13 23:41:52 -0600100
101 if (bus->uclass->uc_drv->id != UCLASS_SPI)
102 return -EOPNOTSUPP;
Simon Glass2d2e8602019-12-06 21:42:35 -0700103 if (!ops->xfer)
104 return -ENOSYS;
Simon Glassdd82d442014-10-13 23:41:52 -0600105
Simon Glass2d2e8602019-12-06 21:42:35 -0700106 return ops->xfer(dev, bitlen, dout, din, flags);
Simon Glassdd82d442014-10-13 23:41:52 -0600107}
108
Simon Glass37ad0fe2019-10-20 21:31:47 -0600109int dm_spi_get_mmap(struct udevice *dev, ulong *map_basep, uint *map_sizep,
110 uint *offsetp)
111{
112 struct udevice *bus = dev->parent;
113 struct dm_spi_ops *ops = spi_get_ops(bus);
114
115 if (bus->uclass->uc_drv->id != UCLASS_SPI)
116 return -EOPNOTSUPP;
117 if (!ops->get_mmap)
118 return -ENOSYS;
119
120 return ops->get_mmap(dev, map_basep, map_sizep, offsetp);
121}
122
Peng Fanfdd88a32016-05-03 10:02:22 +0800123int spi_claim_bus(struct spi_slave *slave)
124{
Simon Glass00d99902018-10-01 12:22:24 -0600125 return log_ret(dm_spi_claim_bus(slave->dev));
Peng Fanfdd88a32016-05-03 10:02:22 +0800126}
127
128void spi_release_bus(struct spi_slave *slave)
129{
130 dm_spi_release_bus(slave->dev);
131}
132
Paul Barkerdbb689f2022-10-05 13:18:34 +0100133int spi_set_speed(struct spi_slave *slave, uint hz)
134{
135 struct dm_spi_ops *ops;
136 int ret;
137
138 ops = spi_get_ops(slave->dev->parent);
139 if (ops->set_speed)
140 ret = ops->set_speed(slave->dev->parent, hz);
141 else
142 ret = -EINVAL;
143 if (ret)
144 dev_err(slave->dev, "Cannot set speed (err=%d)\n", ret);
145 return ret;
146}
147
Peng Fanfdd88a32016-05-03 10:02:22 +0800148int spi_xfer(struct spi_slave *slave, unsigned int bitlen,
149 const void *dout, void *din, unsigned long flags)
150{
151 return dm_spi_xfer(slave->dev, bitlen, dout, din, flags);
152}
153
Jagan Teki7cc71fd2019-07-22 17:22:56 +0530154int spi_write_then_read(struct spi_slave *slave, const u8 *opcode,
155 size_t n_opcode, const u8 *txbuf, u8 *rxbuf,
156 size_t n_buf)
157{
158 unsigned long flags = SPI_XFER_BEGIN;
159 int ret;
160
161 if (n_buf == 0)
162 flags |= SPI_XFER_END;
163
164 ret = spi_xfer(slave, n_opcode * 8, opcode, NULL, flags);
165 if (ret) {
Patrick Delaunay1d83e032020-10-15 17:18:17 +0200166 dev_dbg(slave->dev,
167 "spi: failed to send command (%zu bytes): %d\n",
168 n_opcode, ret);
Jagan Teki7cc71fd2019-07-22 17:22:56 +0530169 } else if (n_buf != 0) {
170 ret = spi_xfer(slave, n_buf * 8, txbuf, rxbuf, SPI_XFER_END);
171 if (ret)
Patrick Delaunay1d83e032020-10-15 17:18:17 +0200172 dev_dbg(slave->dev,
173 "spi: failed to transfer %zu bytes of data: %d\n",
174 n_buf, ret);
Jagan Teki7cc71fd2019-07-22 17:22:56 +0530175 }
176
177 return ret;
178}
179
Simon Glass92882652021-08-07 07:24:04 -0600180#if CONFIG_IS_ENABLED(OF_REAL)
Simon Glassd941aaf2015-06-23 15:39:05 -0600181static int spi_child_post_bind(struct udevice *dev)
Simon Glassdd82d442014-10-13 23:41:52 -0600182{
Simon Glassb75b15b2020-12-03 16:55:23 -0700183 struct dm_spi_slave_plat *plat = dev_get_parent_plat(dev);
Simon Glassdd82d442014-10-13 23:41:52 -0600184
Simon Glassf1d50f72020-12-19 10:40:13 -0700185 if (!dev_has_ofnode(dev))
Simon Glass5d2ee052015-01-25 08:27:12 -0700186 return 0;
187
Simon Glassaad29ae2020-12-03 16:55:21 -0700188 return spi_slave_of_to_plat(dev, plat);
Simon Glass5d2ee052015-01-25 08:27:12 -0700189}
Simon Glass3fb33392016-11-13 14:22:01 -0700190#endif
Simon Glass5d2ee052015-01-25 08:27:12 -0700191
Simon Glassd941aaf2015-06-23 15:39:05 -0600192static int spi_post_probe(struct udevice *bus)
Simon Glass5d2ee052015-01-25 08:27:12 -0700193{
Simon Glass6d70ba02021-08-07 07:24:06 -0600194 if (CONFIG_IS_ENABLED(OF_REAL)) {
195 struct dm_spi_bus *spi = dev_get_uclass_priv(bus);
Simon Glass5d2ee052015-01-25 08:27:12 -0700196
Simon Glass6d70ba02021-08-07 07:24:06 -0600197 spi->max_hz = dev_read_u32_default(bus, "spi-max-frequency", 0);
198 }
Michal Simek28d7e4e2015-10-27 13:36:42 +0100199
Simon Glassdd82d442014-10-13 23:41:52 -0600200 return 0;
201}
202
Simon Glassd941aaf2015-06-23 15:39:05 -0600203static int spi_child_pre_probe(struct udevice *dev)
Simon Glass82c2f502015-01-25 08:27:11 -0700204{
Simon Glassb75b15b2020-12-03 16:55:23 -0700205 struct dm_spi_slave_plat *plat = dev_get_parent_plat(dev);
Simon Glassde44acf2015-09-28 23:32:01 -0600206 struct spi_slave *slave = dev_get_parent_priv(dev);
Simon Glass82c2f502015-01-25 08:27:11 -0700207
Simon Glass5d2ee052015-01-25 08:27:12 -0700208 /*
209 * This is needed because we pass struct spi_slave around the place
210 * instead slave->dev (a struct udevice). So we have to have some
211 * way to access the slave udevice given struct spi_slave. Once we
212 * change the SPI API to use udevice instead of spi_slave, we can
213 * drop this.
214 */
Simon Glass82c2f502015-01-25 08:27:11 -0700215 slave->dev = dev;
216
Simon Glass5d2ee052015-01-25 08:27:12 -0700217 slave->max_hz = plat->max_hz;
218 slave->mode = plat->mode;
Christophe Ricardfb0c53e2016-01-17 11:56:48 +0100219 slave->wordlen = SPI_DEFAULT_WORDLEN;
Simon Glass5d2ee052015-01-25 08:27:12 -0700220
Simon Glass82c2f502015-01-25 08:27:11 -0700221 return 0;
222}
223
Simon Glassdd82d442014-10-13 23:41:52 -0600224int spi_chip_select(struct udevice *dev)
225{
Simon Glassb75b15b2020-12-03 16:55:23 -0700226 struct dm_spi_slave_plat *plat = dev_get_parent_plat(dev);
Simon Glassdd82d442014-10-13 23:41:52 -0600227
Simon Glass5d2ee052015-01-25 08:27:12 -0700228 return plat ? plat->cs : -ENOENT;
Simon Glassdd82d442014-10-13 23:41:52 -0600229}
230
Simon Glass5ef36f22014-11-11 10:46:22 -0700231int spi_find_chip_select(struct udevice *bus, int cs, struct udevice **devp)
Simon Glassdd82d442014-10-13 23:41:52 -0600232{
Bin Meng9741e732019-09-09 06:00:02 -0700233 struct dm_spi_ops *ops;
234 struct spi_cs_info info;
Simon Glassdd82d442014-10-13 23:41:52 -0600235 struct udevice *dev;
Bin Meng9741e732019-09-09 06:00:02 -0700236 int ret;
237
238 /*
239 * Ask the driver. For the moment we don't have CS info.
240 * When we do we could provide the driver with a helper function
241 * to figure out what chip selects are valid, or just handle the
242 * request.
243 */
244 ops = spi_get_ops(bus);
245 if (ops->cs_info) {
246 ret = ops->cs_info(bus, cs, &info);
247 } else {
248 /*
249 * We could assume there is at least one valid chip select.
250 * The driver didn't care enough to tell us.
251 */
252 ret = 0;
253 }
254
255 if (ret) {
Patrick Delaunay1d83e032020-10-15 17:18:17 +0200256 dev_err(bus, "Invalid cs %d (err=%d)\n", cs, ret);
Bin Meng9741e732019-09-09 06:00:02 -0700257 return ret;
258 }
Simon Glassdd82d442014-10-13 23:41:52 -0600259
260 for (device_find_first_child(bus, &dev); dev;
261 device_find_next_child(&dev)) {
Simon Glassb75b15b2020-12-03 16:55:23 -0700262 struct dm_spi_slave_plat *plat;
Simon Glassdd82d442014-10-13 23:41:52 -0600263
Simon Glass71fa5b42020-12-03 16:55:18 -0700264 plat = dev_get_parent_plat(dev);
Patrick Delaunay1d83e032020-10-15 17:18:17 +0200265 dev_dbg(bus, "%s: plat=%p, cs=%d\n", __func__, plat, plat->cs);
Simon Glass5d2ee052015-01-25 08:27:12 -0700266 if (plat->cs == cs) {
Simon Glassdd82d442014-10-13 23:41:52 -0600267 *devp = dev;
268 return 0;
269 }
270 }
271
272 return -ENODEV;
273}
274
275int spi_cs_is_valid(unsigned int busnum, unsigned int cs)
276{
277 struct spi_cs_info info;
278 struct udevice *bus;
279 int ret;
280
Simon Glass07e13382020-12-16 21:20:29 -0700281 ret = uclass_find_device_by_seq(UCLASS_SPI, busnum, &bus);
Simon Glassdd82d442014-10-13 23:41:52 -0600282 if (ret) {
Patrick Delaunay1d83e032020-10-15 17:18:17 +0200283 log_debug("%s: No bus %d\n", __func__, busnum);
Simon Glassdd82d442014-10-13 23:41:52 -0600284 return ret;
285 }
286
287 return spi_cs_info(bus, cs, &info);
288}
289
290int spi_cs_info(struct udevice *bus, uint cs, struct spi_cs_info *info)
291{
292 struct spi_cs_info local_info;
Simon Glassdd82d442014-10-13 23:41:52 -0600293 int ret;
294
295 if (!info)
296 info = &local_info;
297
298 /* If there is a device attached, return it */
299 info->dev = NULL;
300 ret = spi_find_chip_select(bus, cs, &info->dev);
Bin Meng9741e732019-09-09 06:00:02 -0700301 return ret == -ENODEV ? 0 : ret;
Simon Glassdd82d442014-10-13 23:41:52 -0600302}
303
Simon Glassdd82d442014-10-13 23:41:52 -0600304int spi_find_bus_and_cs(int busnum, int cs, struct udevice **busp,
305 struct udevice **devp)
306{
307 struct udevice *bus, *dev;
308 int ret;
309
Simon Glass07e13382020-12-16 21:20:29 -0700310 ret = uclass_find_device_by_seq(UCLASS_SPI, busnum, &bus);
Simon Glassdd82d442014-10-13 23:41:52 -0600311 if (ret) {
Patrick Delaunay1d83e032020-10-15 17:18:17 +0200312 log_debug("%s: No bus %d\n", __func__, busnum);
Simon Glassdd82d442014-10-13 23:41:52 -0600313 return ret;
314 }
315 ret = spi_find_chip_select(bus, cs, &dev);
316 if (ret) {
Patrick Delaunay1d83e032020-10-15 17:18:17 +0200317 dev_dbg(bus, "%s: No cs %d\n", __func__, cs);
Simon Glassdd82d442014-10-13 23:41:52 -0600318 return ret;
319 }
320 *busp = bus;
321 *devp = dev;
322
323 return ret;
324}
325
Patrice Chotard86e06ae2022-03-30 09:33:13 +0200326int spi_get_bus_and_cs(int busnum, int cs, struct udevice **busp,
327 struct spi_slave **devp)
Simon Glassdd82d442014-10-13 23:41:52 -0600328{
329 struct udevice *bus, *dev;
Patrice Chotard86e06ae2022-03-30 09:33:13 +0200330 struct dm_spi_bus *bus_data;
331 struct spi_slave *slave;
332 int ret;
333
334#if CONFIG_IS_ENABLED(OF_PLATDATA)
335 ret = uclass_first_device_err(UCLASS_SPI, &bus);
336#else
337 ret = uclass_get_device_by_seq(UCLASS_SPI, busnum, &bus);
338#endif
339 if (ret) {
340 log_err("Invalid bus %d (err=%d)\n", busnum, ret);
341 return ret;
342 }
343 ret = spi_find_chip_select(bus, cs, &dev);
344 if (ret) {
345 dev_err(bus, "Invalid chip select %d:%d (err=%d)\n", busnum, cs, ret);
346 return ret;
347 }
348
349 if (!device_active(dev)) {
350 struct spi_slave *slave;
351
352 ret = device_probe(dev);
353 if (ret)
354 goto err;
355 slave = dev_get_parent_priv(dev);
356 slave->dev = dev;
357 }
358
359 slave = dev_get_parent_priv(dev);
360 bus_data = dev_get_uclass_priv(bus);
361
362 /*
363 * In case the operation speed is not yet established by
364 * dm_spi_claim_bus() ensure the bus is configured properly.
365 */
366 if (!bus_data->speed) {
367 ret = spi_claim_bus(slave);
368 if (ret)
369 goto err;
370 }
371 *busp = bus;
372 *devp = slave;
373
374 return 0;
375
376err:
377 log_debug("%s: Error path, device '%s'\n", __func__, dev->name);
378
379 return ret;
380}
381
382int _spi_get_bus_and_cs(int busnum, int cs, int speed, int mode,
383 const char *drv_name, const char *dev_name,
384 struct udevice **busp, struct spi_slave **devp)
385{
386 struct udevice *bus, *dev;
Simon Glassb75b15b2020-12-03 16:55:23 -0700387 struct dm_spi_slave_plat *plat;
Ovidiu Panait40dcee12020-12-14 19:06:50 +0200388 struct dm_spi_bus *bus_data;
Marcin Wojtas70d8f982019-11-21 05:38:47 +0100389 struct spi_slave *slave;
Simon Glassdd82d442014-10-13 23:41:52 -0600390 bool created = false;
391 int ret;
392
Thomas Fitzsimmons59c90f32019-09-06 07:51:19 -0400393#if CONFIG_IS_ENABLED(OF_PLATDATA)
Simon Glass3fb33392016-11-13 14:22:01 -0700394 ret = uclass_first_device_err(UCLASS_SPI, &bus);
395#else
Simon Glassdd82d442014-10-13 23:41:52 -0600396 ret = uclass_get_device_by_seq(UCLASS_SPI, busnum, &bus);
Simon Glass3fb33392016-11-13 14:22:01 -0700397#endif
Simon Glassdd82d442014-10-13 23:41:52 -0600398 if (ret) {
Patrick Delaunay1d83e032020-10-15 17:18:17 +0200399 log_err("Invalid bus %d (err=%d)\n", busnum, ret);
Simon Glassdd82d442014-10-13 23:41:52 -0600400 return ret;
401 }
402 ret = spi_find_chip_select(bus, cs, &dev);
403
404 /*
405 * If there is no such device, create one automatically. This means
406 * that we don't need a device tree node or platform data for the
407 * SPI flash chip - we will bind to the correct driver.
408 */
409 if (ret == -ENODEV && drv_name) {
Patrick Delaunay1d83e032020-10-15 17:18:17 +0200410 dev_dbg(bus, "%s: Binding new device '%s', busnum=%d, cs=%d, driver=%s\n",
411 __func__, dev_name, busnum, cs, drv_name);
Simon Glassd8a21f62014-11-11 10:46:23 -0700412 ret = device_bind_driver(bus, drv_name, dev_name, &dev);
Simon Glassf0307e12016-11-13 14:22:05 -0700413 if (ret) {
Patrick Delaunay1d83e032020-10-15 17:18:17 +0200414 dev_dbg(bus, "%s: Unable to bind driver (ret=%d)\n",
415 __func__, ret);
Simon Glassdd82d442014-10-13 23:41:52 -0600416 return ret;
Simon Glassf0307e12016-11-13 14:22:05 -0700417 }
Simon Glass71fa5b42020-12-03 16:55:18 -0700418 plat = dev_get_parent_plat(dev);
Simon Glass5d2ee052015-01-25 08:27:12 -0700419 plat->cs = cs;
Simon Goldschmidta942e1a2018-10-30 21:09:48 +0100420 if (speed) {
421 plat->max_hz = speed;
422 } else {
Patrick Delaunay1d83e032020-10-15 17:18:17 +0200423 dev_warn(bus,
424 "Warning: SPI speed fallback to %u kHz\n",
425 SPI_DEFAULT_SPEED_HZ / 1000);
Simon Goldschmidta942e1a2018-10-30 21:09:48 +0100426 plat->max_hz = SPI_DEFAULT_SPEED_HZ;
427 }
Simon Glass5d2ee052015-01-25 08:27:12 -0700428 plat->mode = mode;
Simon Glassdd82d442014-10-13 23:41:52 -0600429 created = true;
430 } else if (ret) {
Patrick Delaunay1d83e032020-10-15 17:18:17 +0200431 dev_err(bus, "Invalid chip select %d:%d (err=%d)\n", busnum, cs, ret);
Simon Glassdd82d442014-10-13 23:41:52 -0600432 return ret;
Marek Vasut170013f2021-06-10 14:00:00 +0200433 } else if (dev) {
434 plat = dev_get_parent_plat(dev);
Simon Glassdd82d442014-10-13 23:41:52 -0600435 }
436
437 if (!device_active(dev)) {
Simon Glass5d2ee052015-01-25 08:27:12 -0700438 struct spi_slave *slave;
Simon Glassdd82d442014-10-13 23:41:52 -0600439
Simon Glass5d2ee052015-01-25 08:27:12 -0700440 ret = device_probe(dev);
Simon Glassdd82d442014-10-13 23:41:52 -0600441 if (ret)
442 goto err;
Simon Glassde44acf2015-09-28 23:32:01 -0600443 slave = dev_get_parent_priv(dev);
Simon Glassdd82d442014-10-13 23:41:52 -0600444 slave->dev = dev;
Simon Glassdd82d442014-10-13 23:41:52 -0600445 }
446
Marcin Wojtas70d8f982019-11-21 05:38:47 +0100447 slave = dev_get_parent_priv(dev);
Ovidiu Panait40dcee12020-12-14 19:06:50 +0200448 bus_data = dev_get_uclass_priv(bus);
Patrick Delaunayfa19c652019-02-27 15:36:44 +0100449
Marcin Wojtas70d8f982019-11-21 05:38:47 +0100450 /*
451 * In case the operation speed is not yet established by
452 * dm_spi_claim_bus() ensure the bus is configured properly.
453 */
Ovidiu Panait40dcee12020-12-14 19:06:50 +0200454 if (!bus_data->speed) {
Marcin Wojtas70d8f982019-11-21 05:38:47 +0100455 ret = spi_claim_bus(slave);
456 if (ret)
457 goto err;
Vignesh Rae569792016-07-06 10:04:28 +0530458 }
Simon Glassdd82d442014-10-13 23:41:52 -0600459
Marek Vasut170013f2021-06-10 14:00:00 +0200460 /* In case bus frequency or mode changed, update it. */
461 if ((speed && bus_data->speed && bus_data->speed != speed) ||
462 (plat && plat->mode != mode)) {
463 ret = spi_set_speed_mode(bus, speed, mode);
464 if (ret)
465 goto err_speed_mode;
466 }
467
Simon Glassdd82d442014-10-13 23:41:52 -0600468 *busp = bus;
Marcin Wojtas70d8f982019-11-21 05:38:47 +0100469 *devp = slave;
Patrick Delaunay1d83e032020-10-15 17:18:17 +0200470 log_debug("%s: bus=%p, slave=%p\n", __func__, bus, *devp);
Simon Glassdd82d442014-10-13 23:41:52 -0600471
472 return 0;
473
Marek Vasut170013f2021-06-10 14:00:00 +0200474err_speed_mode:
475 spi_release_bus(slave);
Simon Glassdd82d442014-10-13 23:41:52 -0600476err:
Patrick Delaunay1d83e032020-10-15 17:18:17 +0200477 log_debug("%s: Error path, created=%d, device '%s'\n", __func__,
478 created, dev->name);
Simon Glassdd82d442014-10-13 23:41:52 -0600479 if (created) {
Stefan Roese80b5bc92017-03-20 12:51:48 +0100480 device_remove(dev, DM_REMOVE_NORMAL);
Simon Glassdd82d442014-10-13 23:41:52 -0600481 device_unbind(dev);
482 }
483
484 return ret;
485}
486
487/* Compatibility function - to be removed */
Simon Glassdd82d442014-10-13 23:41:52 -0600488struct spi_slave *spi_setup_slave(unsigned int busnum, unsigned int cs,
489 unsigned int speed, unsigned int mode)
490{
491 struct spi_slave *slave;
492 struct udevice *dev;
493 int ret;
494
Patrice Chotard86e06ae2022-03-30 09:33:13 +0200495 ret = _spi_get_bus_and_cs(busnum, cs, speed, mode, NULL, 0, &dev,
496 &slave);
Simon Glassdd82d442014-10-13 23:41:52 -0600497 if (ret)
498 return NULL;
499
500 return slave;
501}
502
503void spi_free_slave(struct spi_slave *slave)
504{
Stefan Roese80b5bc92017-03-20 12:51:48 +0100505 device_remove(slave->dev, DM_REMOVE_NORMAL);
Simon Glassdd82d442014-10-13 23:41:52 -0600506}
507
Simon Glassb75b15b2020-12-03 16:55:23 -0700508int spi_slave_of_to_plat(struct udevice *dev, struct dm_spi_slave_plat *plat)
Simon Glassdd82d442014-10-13 23:41:52 -0600509{
Jagan Teki96536b12016-08-08 17:12:12 +0530510 int mode = 0;
Mugunthan V N4b0f40c2015-12-23 20:39:37 +0530511 int value;
Simon Glassdd82d442014-10-13 23:41:52 -0600512
Simon Glass4b280582017-05-18 20:09:54 -0600513 plat->cs = dev_read_u32_default(dev, "reg", -1);
Simon Goldschmidta942e1a2018-10-30 21:09:48 +0100514 plat->max_hz = dev_read_u32_default(dev, "spi-max-frequency",
515 SPI_DEFAULT_SPEED_HZ);
Simon Glass4b280582017-05-18 20:09:54 -0600516 if (dev_read_bool(dev, "spi-cpol"))
Simon Glassdd82d442014-10-13 23:41:52 -0600517 mode |= SPI_CPOL;
Simon Glass4b280582017-05-18 20:09:54 -0600518 if (dev_read_bool(dev, "spi-cpha"))
Simon Glassdd82d442014-10-13 23:41:52 -0600519 mode |= SPI_CPHA;
Simon Glass4b280582017-05-18 20:09:54 -0600520 if (dev_read_bool(dev, "spi-cs-high"))
Simon Glassdd82d442014-10-13 23:41:52 -0600521 mode |= SPI_CS_HIGH;
Simon Glass4b280582017-05-18 20:09:54 -0600522 if (dev_read_bool(dev, "spi-3wire"))
Jagan Tekid3868fd2015-12-03 22:19:05 +0530523 mode |= SPI_3WIRE;
Simon Glass4b280582017-05-18 20:09:54 -0600524 if (dev_read_bool(dev, "spi-half-duplex"))
Simon Glassdd82d442014-10-13 23:41:52 -0600525 mode |= SPI_PREAMBLE;
Mugunthan V N4b0f40c2015-12-23 20:39:37 +0530526
527 /* Device DUAL/QUAD mode */
Simon Glass4b280582017-05-18 20:09:54 -0600528 value = dev_read_u32_default(dev, "spi-tx-bus-width", 1);
Mugunthan V N4b0f40c2015-12-23 20:39:37 +0530529 switch (value) {
530 case 1:
531 break;
532 case 2:
533 mode |= SPI_TX_DUAL;
534 break;
535 case 4:
536 mode |= SPI_TX_QUAD;
537 break;
Vignesh Raghavendrac063ee32019-12-05 15:46:05 +0530538 case 8:
539 mode |= SPI_TX_OCTAL;
540 break;
Mugunthan V N4b0f40c2015-12-23 20:39:37 +0530541 default:
Simon Glass6f5bed02016-11-29 20:00:13 -0700542 warn_non_spl("spi-tx-bus-width %d not supported\n", value);
Mugunthan V N4b0f40c2015-12-23 20:39:37 +0530543 break;
544 }
545
Simon Glass4b280582017-05-18 20:09:54 -0600546 value = dev_read_u32_default(dev, "spi-rx-bus-width", 1);
Mugunthan V N4b0f40c2015-12-23 20:39:37 +0530547 switch (value) {
548 case 1:
549 break;
550 case 2:
Jagan Teki96536b12016-08-08 17:12:12 +0530551 mode |= SPI_RX_DUAL;
Mugunthan V N4b0f40c2015-12-23 20:39:37 +0530552 break;
553 case 4:
Jagan Teki96536b12016-08-08 17:12:12 +0530554 mode |= SPI_RX_QUAD;
Mugunthan V N4b0f40c2015-12-23 20:39:37 +0530555 break;
Vignesh Raghavendrac063ee32019-12-05 15:46:05 +0530556 case 8:
557 mode |= SPI_RX_OCTAL;
558 break;
Mugunthan V N4b0f40c2015-12-23 20:39:37 +0530559 default:
Simon Glass6f5bed02016-11-29 20:00:13 -0700560 warn_non_spl("spi-rx-bus-width %d not supported\n", value);
Mugunthan V N4b0f40c2015-12-23 20:39:37 +0530561 break;
562 }
563
Jagan Teki96536b12016-08-08 17:12:12 +0530564 plat->mode = mode;
Mugunthan V N4b0f40c2015-12-23 20:39:37 +0530565
Simon Glassdd82d442014-10-13 23:41:52 -0600566 return 0;
567}
568
569UCLASS_DRIVER(spi) = {
570 .id = UCLASS_SPI,
571 .name = "spi",
Simon Glass0ccb0972015-01-25 08:27:05 -0700572 .flags = DM_UC_FLAG_SEQ_ALIAS,
Simon Glass3580f6d2021-08-07 07:24:03 -0600573#if CONFIG_IS_ENABLED(OF_REAL)
Simon Glass18230342016-07-05 17:10:10 -0600574 .post_bind = dm_scan_fdt_dev,
Simon Glass3fb33392016-11-13 14:22:01 -0700575#endif
Simon Glassdd82d442014-10-13 23:41:52 -0600576 .post_probe = spi_post_probe,
Simon Glass82c2f502015-01-25 08:27:11 -0700577 .child_pre_probe = spi_child_pre_probe,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700578 .per_device_auto = sizeof(struct dm_spi_bus),
579 .per_child_auto = sizeof(struct spi_slave),
Simon Glassb75b15b2020-12-03 16:55:23 -0700580 .per_child_plat_auto = sizeof(struct dm_spi_slave_plat),
Simon Glass92882652021-08-07 07:24:04 -0600581#if CONFIG_IS_ENABLED(OF_REAL)
Simon Glass5d2ee052015-01-25 08:27:12 -0700582 .child_post_bind = spi_child_post_bind,
Simon Glass3fb33392016-11-13 14:22:01 -0700583#endif
Simon Glassdd82d442014-10-13 23:41:52 -0600584};
585
586UCLASS_DRIVER(spi_generic) = {
587 .id = UCLASS_SPI_GENERIC,
588 .name = "spi_generic",
589};
590
591U_BOOT_DRIVER(spi_generic_drv) = {
592 .name = "spi_generic_drv",
593 .id = UCLASS_SPI_GENERIC,
594};