blob: c929e7c1d0e9aafdc252e90f85236d0ebdb2f3ad [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glassd7af6a42014-10-13 23:41:52 -06002/*
3 * Copyright (c) 2014 Google, Inc
Simon Glassd7af6a42014-10-13 23:41:52 -06004 */
5
Patrick Delaunay46c53912020-10-15 17:18:17 +02006#define LOG_CATEGORY UCLASS_SPI
7
Simon Glassd7af6a42014-10-13 23:41:52 -06008#include <common.h>
9#include <dm.h>
10#include <errno.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060011#include <log.h>
Simon Glassd7af6a42014-10-13 23:41:52 -060012#include <malloc.h>
13#include <spi.h>
T Karthik Reddy346df7d2021-03-17 12:31:30 +010014#include <spi-mem.h>
Patrick Delaunay46c53912020-10-15 17:18:17 +020015#include <dm/device_compat.h>
Simon Glass401d1c42020-10-30 21:38:53 -060016#include <asm/global_data.h>
Simon Glassd7af6a42014-10-13 23:41:52 -060017#include <dm/device-internal.h>
18#include <dm/uclass-internal.h>
Simon Glassd7af6a42014-10-13 23:41:52 -060019#include <dm/lists.h>
20#include <dm/util.h>
21
22DECLARE_GLOBAL_DATA_PTR;
23
Simon Goldschmidt12bfb2e2018-10-30 21:09:48 +010024#define SPI_DEFAULT_SPEED_HZ 100000
25
Simon Glassd7af6a42014-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 Delaunay46c53912020-10-15 17:18:17 +020037 dev_err(bus, "Cannot set speed (err=%d)\n", ret);
Simon Glassd7af6a42014-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 Delaunay46c53912020-10-15 17:18:17 +020046 dev_err(bus, "Cannot set mode (err=%d)\n", ret);
Simon Glassd7af6a42014-10-13 23:41:52 -060047 return ret;
48 }
49
50 return 0;
51}
52
Peng Fan7a3eff42016-05-03 10:02:22 +080053int dm_spi_claim_bus(struct udevice *dev)
Simon Glassd7af6a42014-10-13 23:41:52 -060054{
Simon Glassd7af6a42014-10-13 23:41:52 -060055 struct udevice *bus = dev->parent;
56 struct dm_spi_ops *ops = spi_get_ops(bus);
Simon Glasse564f052015-03-05 12:25:20 -070057 struct dm_spi_bus *spi = dev_get_uclass_priv(bus);
Peng Fan7a3eff42016-05-03 10:02:22 +080058 struct spi_slave *slave = dev_get_parent_priv(dev);
Ovidiu Panait741280e2020-12-14 19:06:50 +020059 uint speed, mode;
Simon Glassd7af6a42014-10-13 23:41:52 -060060
61 speed = slave->max_hz;
Ovidiu Panait741280e2020-12-14 19:06:50 +020062 mode = slave->mode;
63
Simon Glassd7af6a42014-10-13 23:41:52 -060064 if (spi->max_hz) {
65 if (speed)
Ovidiu Panait741280e2020-12-14 19:06:50 +020066 speed = min(speed, spi->max_hz);
Simon Glassd7af6a42014-10-13 23:41:52 -060067 else
68 speed = spi->max_hz;
69 }
70 if (!speed)
Simon Goldschmidt12bfb2e2018-10-30 21:09:48 +010071 speed = SPI_DEFAULT_SPEED_HZ;
Ovidiu Panait741280e2020-12-14 19:06:50 +020072
73 if (speed != spi->speed || mode != spi->mode) {
Mario Six24fc1ec2018-01-15 11:08:41 +010074 int ret = spi_set_speed_mode(bus, speed, slave->mode);
75
Simon Glass60e28092015-02-17 15:29:35 -070076 if (ret)
Simon Glass5e24a2e2018-10-01 12:22:24 -060077 return log_ret(ret);
Ovidiu Panait741280e2020-12-14 19:06:50 +020078
79 spi->speed = speed;
80 spi->mode = mode;
Simon Glass60e28092015-02-17 15:29:35 -070081 }
Simon Glassd7af6a42014-10-13 23:41:52 -060082
Simon Glass5e24a2e2018-10-01 12:22:24 -060083 return log_ret(ops->claim_bus ? ops->claim_bus(dev) : 0);
Simon Glassd7af6a42014-10-13 23:41:52 -060084}
85
Peng Fan7a3eff42016-05-03 10:02:22 +080086void dm_spi_release_bus(struct udevice *dev)
Simon Glassd7af6a42014-10-13 23:41:52 -060087{
Simon Glassd7af6a42014-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 Glass9694b722015-04-19 09:05:40 -060092 ops->release_bus(dev);
Simon Glassd7af6a42014-10-13 23:41:52 -060093}
94
Peng Fan7a3eff42016-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 Glassd7af6a42014-10-13 23:41:52 -060097{
Simon Glassd7af6a42014-10-13 23:41:52 -060098 struct udevice *bus = dev->parent;
Simon Glassccdabd82019-12-06 21:42:35 -070099 struct dm_spi_ops *ops = spi_get_ops(bus);
Simon Glassd7af6a42014-10-13 23:41:52 -0600100
101 if (bus->uclass->uc_drv->id != UCLASS_SPI)
102 return -EOPNOTSUPP;
Simon Glassccdabd82019-12-06 21:42:35 -0700103 if (!ops->xfer)
104 return -ENOSYS;
Simon Glassd7af6a42014-10-13 23:41:52 -0600105
Simon Glassccdabd82019-12-06 21:42:35 -0700106 return ops->xfer(dev, bitlen, dout, din, flags);
Simon Glassd7af6a42014-10-13 23:41:52 -0600107}
108
Simon Glassc53b3182019-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 Fan7a3eff42016-05-03 10:02:22 +0800123int spi_claim_bus(struct spi_slave *slave)
124{
Simon Glass5e24a2e2018-10-01 12:22:24 -0600125 return log_ret(dm_spi_claim_bus(slave->dev));
Peng Fan7a3eff42016-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 Barkerc98f6fe2022-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 Fan7a3eff42016-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 Teki8473b322019-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 Delaunay46c53912020-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 Teki8473b322019-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 Delaunay46c53912020-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 Teki8473b322019-07-22 17:22:56 +0530175 }
176
177 return ret;
178}
179
Simon Glass95397382021-08-07 07:24:04 -0600180#if CONFIG_IS_ENABLED(OF_REAL)
Simon Glass6f849c32015-06-23 15:39:05 -0600181static int spi_child_post_bind(struct udevice *dev)
Simon Glassd7af6a42014-10-13 23:41:52 -0600182{
Simon Glass8a8d24b2020-12-03 16:55:23 -0700183 struct dm_spi_slave_plat *plat = dev_get_parent_plat(dev);
Simon Glassd7af6a42014-10-13 23:41:52 -0600184
Simon Glass7d14ee42020-12-19 10:40:13 -0700185 if (!dev_has_ofnode(dev))
Simon Glassd0cff032015-01-25 08:27:12 -0700186 return 0;
187
Simon Glassd1998a92020-12-03 16:55:21 -0700188 return spi_slave_of_to_plat(dev, plat);
Simon Glassd0cff032015-01-25 08:27:12 -0700189}
Simon Glass71634f22016-11-13 14:22:01 -0700190#endif
Simon Glassd0cff032015-01-25 08:27:12 -0700191
Simon Glass6f849c32015-06-23 15:39:05 -0600192static int spi_post_probe(struct udevice *bus)
Simon Glassd0cff032015-01-25 08:27:12 -0700193{
Simon Glassdcfc42b2021-08-07 07:24:06 -0600194 if (CONFIG_IS_ENABLED(OF_REAL)) {
195 struct dm_spi_bus *spi = dev_get_uclass_priv(bus);
Simon Glassd0cff032015-01-25 08:27:12 -0700196
Simon Glassdcfc42b2021-08-07 07:24:06 -0600197 spi->max_hz = dev_read_u32_default(bus, "spi-max-frequency", 0);
198 }
Michal Simek281f1562015-10-27 13:36:42 +0100199#if defined(CONFIG_NEEDS_MANUAL_RELOC)
200 struct dm_spi_ops *ops = spi_get_ops(bus);
Ashok Reddy Soma4d9b1af2019-09-17 00:11:02 -0600201 static int reloc_done;
Michal Simek281f1562015-10-27 13:36:42 +0100202
Ashok Reddy Soma4d9b1af2019-09-17 00:11:02 -0600203 if (!reloc_done) {
204 if (ops->claim_bus)
205 ops->claim_bus += gd->reloc_off;
206 if (ops->release_bus)
207 ops->release_bus += gd->reloc_off;
208 if (ops->set_wordlen)
209 ops->set_wordlen += gd->reloc_off;
210 if (ops->xfer)
211 ops->xfer += gd->reloc_off;
212 if (ops->set_speed)
213 ops->set_speed += gd->reloc_off;
214 if (ops->set_mode)
215 ops->set_mode += gd->reloc_off;
216 if (ops->cs_info)
217 ops->cs_info += gd->reloc_off;
T Karthik Reddy346df7d2021-03-17 12:31:30 +0100218 if (ops->mem_ops) {
219 struct spi_controller_mem_ops *mem_ops =
220 (struct spi_controller_mem_ops *)ops->mem_ops;
221 if (mem_ops->adjust_op_size)
222 mem_ops->adjust_op_size += gd->reloc_off;
223 if (mem_ops->supports_op)
224 mem_ops->supports_op += gd->reloc_off;
225 if (mem_ops->exec_op)
226 mem_ops->exec_op += gd->reloc_off;
227 }
Ashok Reddy Soma4d9b1af2019-09-17 00:11:02 -0600228 reloc_done++;
229 }
Michal Simek281f1562015-10-27 13:36:42 +0100230#endif
231
Simon Glassd7af6a42014-10-13 23:41:52 -0600232 return 0;
233}
234
Simon Glass6f849c32015-06-23 15:39:05 -0600235static int spi_child_pre_probe(struct udevice *dev)
Simon Glass440714e2015-01-25 08:27:11 -0700236{
Simon Glass8a8d24b2020-12-03 16:55:23 -0700237 struct dm_spi_slave_plat *plat = dev_get_parent_plat(dev);
Simon Glassbcbe3d12015-09-28 23:32:01 -0600238 struct spi_slave *slave = dev_get_parent_priv(dev);
Simon Glass440714e2015-01-25 08:27:11 -0700239
Simon Glassd0cff032015-01-25 08:27:12 -0700240 /*
241 * This is needed because we pass struct spi_slave around the place
242 * instead slave->dev (a struct udevice). So we have to have some
243 * way to access the slave udevice given struct spi_slave. Once we
244 * change the SPI API to use udevice instead of spi_slave, we can
245 * drop this.
246 */
Simon Glass440714e2015-01-25 08:27:11 -0700247 slave->dev = dev;
248
Simon Glassd0cff032015-01-25 08:27:12 -0700249 slave->max_hz = plat->max_hz;
250 slave->mode = plat->mode;
Christophe Ricard674f3602016-01-17 11:56:48 +0100251 slave->wordlen = SPI_DEFAULT_WORDLEN;
Simon Glassd0cff032015-01-25 08:27:12 -0700252
Simon Glass440714e2015-01-25 08:27:11 -0700253 return 0;
254}
255
Simon Glassd7af6a42014-10-13 23:41:52 -0600256int spi_chip_select(struct udevice *dev)
257{
Simon Glass8a8d24b2020-12-03 16:55:23 -0700258 struct dm_spi_slave_plat *plat = dev_get_parent_plat(dev);
Simon Glassd7af6a42014-10-13 23:41:52 -0600259
Simon Glassd0cff032015-01-25 08:27:12 -0700260 return plat ? plat->cs : -ENOENT;
Simon Glassd7af6a42014-10-13 23:41:52 -0600261}
262
Simon Glassff56bba2014-11-11 10:46:22 -0700263int spi_find_chip_select(struct udevice *bus, int cs, struct udevice **devp)
Simon Glassd7af6a42014-10-13 23:41:52 -0600264{
Bin Meng7bacce52019-09-09 06:00:02 -0700265 struct dm_spi_ops *ops;
266 struct spi_cs_info info;
Simon Glassd7af6a42014-10-13 23:41:52 -0600267 struct udevice *dev;
Bin Meng7bacce52019-09-09 06:00:02 -0700268 int ret;
269
270 /*
271 * Ask the driver. For the moment we don't have CS info.
272 * When we do we could provide the driver with a helper function
273 * to figure out what chip selects are valid, or just handle the
274 * request.
275 */
276 ops = spi_get_ops(bus);
277 if (ops->cs_info) {
278 ret = ops->cs_info(bus, cs, &info);
279 } else {
280 /*
281 * We could assume there is at least one valid chip select.
282 * The driver didn't care enough to tell us.
283 */
284 ret = 0;
285 }
286
287 if (ret) {
Patrick Delaunay46c53912020-10-15 17:18:17 +0200288 dev_err(bus, "Invalid cs %d (err=%d)\n", cs, ret);
Bin Meng7bacce52019-09-09 06:00:02 -0700289 return ret;
290 }
Simon Glassd7af6a42014-10-13 23:41:52 -0600291
292 for (device_find_first_child(bus, &dev); dev;
293 device_find_next_child(&dev)) {
Simon Glass8a8d24b2020-12-03 16:55:23 -0700294 struct dm_spi_slave_plat *plat;
Simon Glassd7af6a42014-10-13 23:41:52 -0600295
Simon Glasscaa4daa2020-12-03 16:55:18 -0700296 plat = dev_get_parent_plat(dev);
Patrick Delaunay46c53912020-10-15 17:18:17 +0200297 dev_dbg(bus, "%s: plat=%p, cs=%d\n", __func__, plat, plat->cs);
Simon Glassd0cff032015-01-25 08:27:12 -0700298 if (plat->cs == cs) {
Simon Glassd7af6a42014-10-13 23:41:52 -0600299 *devp = dev;
300 return 0;
301 }
302 }
303
304 return -ENODEV;
305}
306
307int spi_cs_is_valid(unsigned int busnum, unsigned int cs)
308{
309 struct spi_cs_info info;
310 struct udevice *bus;
311 int ret;
312
Simon Glass99175912020-12-16 21:20:29 -0700313 ret = uclass_find_device_by_seq(UCLASS_SPI, busnum, &bus);
Simon Glassd7af6a42014-10-13 23:41:52 -0600314 if (ret) {
Patrick Delaunay46c53912020-10-15 17:18:17 +0200315 log_debug("%s: No bus %d\n", __func__, busnum);
Simon Glassd7af6a42014-10-13 23:41:52 -0600316 return ret;
317 }
318
319 return spi_cs_info(bus, cs, &info);
320}
321
322int spi_cs_info(struct udevice *bus, uint cs, struct spi_cs_info *info)
323{
324 struct spi_cs_info local_info;
Simon Glassd7af6a42014-10-13 23:41:52 -0600325 int ret;
326
327 if (!info)
328 info = &local_info;
329
330 /* If there is a device attached, return it */
331 info->dev = NULL;
332 ret = spi_find_chip_select(bus, cs, &info->dev);
Bin Meng7bacce52019-09-09 06:00:02 -0700333 return ret == -ENODEV ? 0 : ret;
Simon Glassd7af6a42014-10-13 23:41:52 -0600334}
335
Simon Glassd7af6a42014-10-13 23:41:52 -0600336int spi_find_bus_and_cs(int busnum, int cs, struct udevice **busp,
337 struct udevice **devp)
338{
339 struct udevice *bus, *dev;
340 int ret;
341
Simon Glass99175912020-12-16 21:20:29 -0700342 ret = uclass_find_device_by_seq(UCLASS_SPI, busnum, &bus);
Simon Glassd7af6a42014-10-13 23:41:52 -0600343 if (ret) {
Patrick Delaunay46c53912020-10-15 17:18:17 +0200344 log_debug("%s: No bus %d\n", __func__, busnum);
Simon Glassd7af6a42014-10-13 23:41:52 -0600345 return ret;
346 }
347 ret = spi_find_chip_select(bus, cs, &dev);
348 if (ret) {
Patrick Delaunay46c53912020-10-15 17:18:17 +0200349 dev_dbg(bus, "%s: No cs %d\n", __func__, cs);
Simon Glassd7af6a42014-10-13 23:41:52 -0600350 return ret;
351 }
352 *busp = bus;
353 *devp = dev;
354
355 return ret;
356}
357
Patrice Chotard61708bb2022-03-30 09:33:13 +0200358int spi_get_bus_and_cs(int busnum, int cs, struct udevice **busp,
359 struct spi_slave **devp)
360{
361 struct udevice *bus, *dev;
362 struct dm_spi_bus *bus_data;
363 struct spi_slave *slave;
364 int ret;
365
366#if CONFIG_IS_ENABLED(OF_PLATDATA)
367 ret = uclass_first_device_err(UCLASS_SPI, &bus);
368#else
369 ret = uclass_get_device_by_seq(UCLASS_SPI, busnum, &bus);
370#endif
371 if (ret) {
372 log_err("Invalid bus %d (err=%d)\n", busnum, ret);
373 return ret;
374 }
375 ret = spi_find_chip_select(bus, cs, &dev);
376 if (ret) {
377 dev_err(bus, "Invalid chip select %d:%d (err=%d)\n", busnum, cs, ret);
378 return ret;
379 }
380
381 if (!device_active(dev)) {
382 struct spi_slave *slave;
383
384 ret = device_probe(dev);
385 if (ret)
386 goto err;
387 slave = dev_get_parent_priv(dev);
388 slave->dev = dev;
389 }
390
391 slave = dev_get_parent_priv(dev);
392 bus_data = dev_get_uclass_priv(bus);
393
394 /*
395 * In case the operation speed is not yet established by
396 * dm_spi_claim_bus() ensure the bus is configured properly.
397 */
398 if (!bus_data->speed) {
399 ret = spi_claim_bus(slave);
400 if (ret)
401 goto err;
402 }
403 *busp = bus;
404 *devp = slave;
405
406 return 0;
407
408err:
409 log_debug("%s: Error path, device '%s'\n", __func__, dev->name);
410
411 return ret;
412}
413
414int _spi_get_bus_and_cs(int busnum, int cs, int speed, int mode,
415 const char *drv_name, const char *dev_name,
416 struct udevice **busp, struct spi_slave **devp)
Simon Glassd7af6a42014-10-13 23:41:52 -0600417{
418 struct udevice *bus, *dev;
Simon Glass8a8d24b2020-12-03 16:55:23 -0700419 struct dm_spi_slave_plat *plat;
Ovidiu Panait741280e2020-12-14 19:06:50 +0200420 struct dm_spi_bus *bus_data;
Marcin Wojtasf7dd5372019-11-21 05:38:47 +0100421 struct spi_slave *slave;
Simon Glassd7af6a42014-10-13 23:41:52 -0600422 bool created = false;
423 int ret;
424
Thomas Fitzsimmons640abba2019-09-06 07:51:19 -0400425#if CONFIG_IS_ENABLED(OF_PLATDATA)
Simon Glass71634f22016-11-13 14:22:01 -0700426 ret = uclass_first_device_err(UCLASS_SPI, &bus);
427#else
Simon Glassd7af6a42014-10-13 23:41:52 -0600428 ret = uclass_get_device_by_seq(UCLASS_SPI, busnum, &bus);
Simon Glass71634f22016-11-13 14:22:01 -0700429#endif
Simon Glassd7af6a42014-10-13 23:41:52 -0600430 if (ret) {
Patrick Delaunay46c53912020-10-15 17:18:17 +0200431 log_err("Invalid bus %d (err=%d)\n", busnum, ret);
Simon Glassd7af6a42014-10-13 23:41:52 -0600432 return ret;
433 }
434 ret = spi_find_chip_select(bus, cs, &dev);
435
436 /*
437 * If there is no such device, create one automatically. This means
438 * that we don't need a device tree node or platform data for the
439 * SPI flash chip - we will bind to the correct driver.
440 */
441 if (ret == -ENODEV && drv_name) {
Patrick Delaunay46c53912020-10-15 17:18:17 +0200442 dev_dbg(bus, "%s: Binding new device '%s', busnum=%d, cs=%d, driver=%s\n",
443 __func__, dev_name, busnum, cs, drv_name);
Simon Glass6b186562014-11-11 10:46:23 -0700444 ret = device_bind_driver(bus, drv_name, dev_name, &dev);
Simon Glass28f98852016-11-13 14:22:05 -0700445 if (ret) {
Patrick Delaunay46c53912020-10-15 17:18:17 +0200446 dev_dbg(bus, "%s: Unable to bind driver (ret=%d)\n",
447 __func__, ret);
Simon Glassd7af6a42014-10-13 23:41:52 -0600448 return ret;
Simon Glass28f98852016-11-13 14:22:05 -0700449 }
Simon Glasscaa4daa2020-12-03 16:55:18 -0700450 plat = dev_get_parent_plat(dev);
Simon Glassd0cff032015-01-25 08:27:12 -0700451 plat->cs = cs;
Simon Goldschmidt12bfb2e2018-10-30 21:09:48 +0100452 if (speed) {
453 plat->max_hz = speed;
454 } else {
Patrick Delaunay46c53912020-10-15 17:18:17 +0200455 dev_warn(bus,
456 "Warning: SPI speed fallback to %u kHz\n",
457 SPI_DEFAULT_SPEED_HZ / 1000);
Simon Goldschmidt12bfb2e2018-10-30 21:09:48 +0100458 plat->max_hz = SPI_DEFAULT_SPEED_HZ;
459 }
Simon Glassd0cff032015-01-25 08:27:12 -0700460 plat->mode = mode;
Simon Glassd7af6a42014-10-13 23:41:52 -0600461 created = true;
462 } else if (ret) {
Patrick Delaunay46c53912020-10-15 17:18:17 +0200463 dev_err(bus, "Invalid chip select %d:%d (err=%d)\n", busnum, cs, ret);
Simon Glassd7af6a42014-10-13 23:41:52 -0600464 return ret;
Marek Vasute2e95e52021-06-10 14:00:00 +0200465 } else if (dev) {
466 plat = dev_get_parent_plat(dev);
Simon Glassd7af6a42014-10-13 23:41:52 -0600467 }
468
469 if (!device_active(dev)) {
Simon Glassd0cff032015-01-25 08:27:12 -0700470 struct spi_slave *slave;
Simon Glassd7af6a42014-10-13 23:41:52 -0600471
Simon Glassd0cff032015-01-25 08:27:12 -0700472 ret = device_probe(dev);
Simon Glassd7af6a42014-10-13 23:41:52 -0600473 if (ret)
474 goto err;
Simon Glassbcbe3d12015-09-28 23:32:01 -0600475 slave = dev_get_parent_priv(dev);
Simon Glassd7af6a42014-10-13 23:41:52 -0600476 slave->dev = dev;
Simon Glassd7af6a42014-10-13 23:41:52 -0600477 }
478
Marcin Wojtasf7dd5372019-11-21 05:38:47 +0100479 slave = dev_get_parent_priv(dev);
Ovidiu Panait741280e2020-12-14 19:06:50 +0200480 bus_data = dev_get_uclass_priv(bus);
Patrick Delaunayb0cc1b82019-02-27 15:36:44 +0100481
Marcin Wojtasf7dd5372019-11-21 05:38:47 +0100482 /*
483 * In case the operation speed is not yet established by
484 * dm_spi_claim_bus() ensure the bus is configured properly.
485 */
Ovidiu Panait741280e2020-12-14 19:06:50 +0200486 if (!bus_data->speed) {
Marcin Wojtasf7dd5372019-11-21 05:38:47 +0100487 ret = spi_claim_bus(slave);
488 if (ret)
489 goto err;
Vignesh R96907c02016-07-06 10:04:28 +0530490 }
Simon Glassd7af6a42014-10-13 23:41:52 -0600491
Marek Vasute2e95e52021-06-10 14:00:00 +0200492 /* In case bus frequency or mode changed, update it. */
493 if ((speed && bus_data->speed && bus_data->speed != speed) ||
494 (plat && plat->mode != mode)) {
495 ret = spi_set_speed_mode(bus, speed, mode);
496 if (ret)
497 goto err_speed_mode;
498 }
499
Simon Glassd7af6a42014-10-13 23:41:52 -0600500 *busp = bus;
Marcin Wojtasf7dd5372019-11-21 05:38:47 +0100501 *devp = slave;
Patrick Delaunay46c53912020-10-15 17:18:17 +0200502 log_debug("%s: bus=%p, slave=%p\n", __func__, bus, *devp);
Simon Glassd7af6a42014-10-13 23:41:52 -0600503
504 return 0;
505
Marek Vasute2e95e52021-06-10 14:00:00 +0200506err_speed_mode:
507 spi_release_bus(slave);
Simon Glassd7af6a42014-10-13 23:41:52 -0600508err:
Patrick Delaunay46c53912020-10-15 17:18:17 +0200509 log_debug("%s: Error path, created=%d, device '%s'\n", __func__,
510 created, dev->name);
Simon Glassd7af6a42014-10-13 23:41:52 -0600511 if (created) {
Stefan Roese706865a2017-03-20 12:51:48 +0100512 device_remove(dev, DM_REMOVE_NORMAL);
Simon Glassd7af6a42014-10-13 23:41:52 -0600513 device_unbind(dev);
514 }
515
516 return ret;
517}
518
519/* Compatibility function - to be removed */
Simon Glassd7af6a42014-10-13 23:41:52 -0600520struct spi_slave *spi_setup_slave(unsigned int busnum, unsigned int cs,
521 unsigned int speed, unsigned int mode)
522{
523 struct spi_slave *slave;
524 struct udevice *dev;
525 int ret;
526
Patrice Chotard61708bb2022-03-30 09:33:13 +0200527 ret = _spi_get_bus_and_cs(busnum, cs, speed, mode, NULL, 0, &dev,
528 &slave);
Simon Glassd7af6a42014-10-13 23:41:52 -0600529 if (ret)
530 return NULL;
531
532 return slave;
533}
534
535void spi_free_slave(struct spi_slave *slave)
536{
Stefan Roese706865a2017-03-20 12:51:48 +0100537 device_remove(slave->dev, DM_REMOVE_NORMAL);
Simon Glassd7af6a42014-10-13 23:41:52 -0600538}
539
Simon Glass8a8d24b2020-12-03 16:55:23 -0700540int spi_slave_of_to_plat(struct udevice *dev, struct dm_spi_slave_plat *plat)
Simon Glassd7af6a42014-10-13 23:41:52 -0600541{
Jagan Teki08fe9c22016-08-08 17:12:12 +0530542 int mode = 0;
Mugunthan V Nf8e2f922015-12-23 20:39:37 +0530543 int value;
Simon Glassd7af6a42014-10-13 23:41:52 -0600544
Simon Glass279e26f2017-05-18 20:09:54 -0600545 plat->cs = dev_read_u32_default(dev, "reg", -1);
Simon Goldschmidt12bfb2e2018-10-30 21:09:48 +0100546 plat->max_hz = dev_read_u32_default(dev, "spi-max-frequency",
547 SPI_DEFAULT_SPEED_HZ);
Simon Glass279e26f2017-05-18 20:09:54 -0600548 if (dev_read_bool(dev, "spi-cpol"))
Simon Glassd7af6a42014-10-13 23:41:52 -0600549 mode |= SPI_CPOL;
Simon Glass279e26f2017-05-18 20:09:54 -0600550 if (dev_read_bool(dev, "spi-cpha"))
Simon Glassd7af6a42014-10-13 23:41:52 -0600551 mode |= SPI_CPHA;
Simon Glass279e26f2017-05-18 20:09:54 -0600552 if (dev_read_bool(dev, "spi-cs-high"))
Simon Glassd7af6a42014-10-13 23:41:52 -0600553 mode |= SPI_CS_HIGH;
Simon Glass279e26f2017-05-18 20:09:54 -0600554 if (dev_read_bool(dev, "spi-3wire"))
Jagan Teki379b49d2015-12-03 22:19:05 +0530555 mode |= SPI_3WIRE;
Simon Glass279e26f2017-05-18 20:09:54 -0600556 if (dev_read_bool(dev, "spi-half-duplex"))
Simon Glassd7af6a42014-10-13 23:41:52 -0600557 mode |= SPI_PREAMBLE;
Mugunthan V Nf8e2f922015-12-23 20:39:37 +0530558
559 /* Device DUAL/QUAD mode */
Simon Glass279e26f2017-05-18 20:09:54 -0600560 value = dev_read_u32_default(dev, "spi-tx-bus-width", 1);
Mugunthan V Nf8e2f922015-12-23 20:39:37 +0530561 switch (value) {
562 case 1:
563 break;
564 case 2:
565 mode |= SPI_TX_DUAL;
566 break;
567 case 4:
568 mode |= SPI_TX_QUAD;
569 break;
Vignesh Raghavendra658df8b2019-12-05 15:46:05 +0530570 case 8:
571 mode |= SPI_TX_OCTAL;
572 break;
Mugunthan V Nf8e2f922015-12-23 20:39:37 +0530573 default:
Simon Glass1b7c28f2016-11-29 20:00:13 -0700574 warn_non_spl("spi-tx-bus-width %d not supported\n", value);
Mugunthan V Nf8e2f922015-12-23 20:39:37 +0530575 break;
576 }
577
Simon Glass279e26f2017-05-18 20:09:54 -0600578 value = dev_read_u32_default(dev, "spi-rx-bus-width", 1);
Mugunthan V Nf8e2f922015-12-23 20:39:37 +0530579 switch (value) {
580 case 1:
581 break;
582 case 2:
Jagan Teki08fe9c22016-08-08 17:12:12 +0530583 mode |= SPI_RX_DUAL;
Mugunthan V Nf8e2f922015-12-23 20:39:37 +0530584 break;
585 case 4:
Jagan Teki08fe9c22016-08-08 17:12:12 +0530586 mode |= SPI_RX_QUAD;
Mugunthan V Nf8e2f922015-12-23 20:39:37 +0530587 break;
Vignesh Raghavendra658df8b2019-12-05 15:46:05 +0530588 case 8:
589 mode |= SPI_RX_OCTAL;
590 break;
Mugunthan V Nf8e2f922015-12-23 20:39:37 +0530591 default:
Simon Glass1b7c28f2016-11-29 20:00:13 -0700592 warn_non_spl("spi-rx-bus-width %d not supported\n", value);
Mugunthan V Nf8e2f922015-12-23 20:39:37 +0530593 break;
594 }
595
Jagan Teki08fe9c22016-08-08 17:12:12 +0530596 plat->mode = mode;
Mugunthan V Nf8e2f922015-12-23 20:39:37 +0530597
Simon Glassd7af6a42014-10-13 23:41:52 -0600598 return 0;
599}
600
601UCLASS_DRIVER(spi) = {
602 .id = UCLASS_SPI,
603 .name = "spi",
Simon Glass9cc36a22015-01-25 08:27:05 -0700604 .flags = DM_UC_FLAG_SEQ_ALIAS,
Simon Glass414cc152021-08-07 07:24:03 -0600605#if CONFIG_IS_ENABLED(OF_REAL)
Simon Glass91195482016-07-05 17:10:10 -0600606 .post_bind = dm_scan_fdt_dev,
Simon Glass71634f22016-11-13 14:22:01 -0700607#endif
Simon Glassd7af6a42014-10-13 23:41:52 -0600608 .post_probe = spi_post_probe,
Simon Glass440714e2015-01-25 08:27:11 -0700609 .child_pre_probe = spi_child_pre_probe,
Simon Glass41575d82020-12-03 16:55:17 -0700610 .per_device_auto = sizeof(struct dm_spi_bus),
611 .per_child_auto = sizeof(struct spi_slave),
Simon Glass8a8d24b2020-12-03 16:55:23 -0700612 .per_child_plat_auto = sizeof(struct dm_spi_slave_plat),
Simon Glass95397382021-08-07 07:24:04 -0600613#if CONFIG_IS_ENABLED(OF_REAL)
Simon Glassd0cff032015-01-25 08:27:12 -0700614 .child_post_bind = spi_child_post_bind,
Simon Glass71634f22016-11-13 14:22:01 -0700615#endif
Simon Glassd7af6a42014-10-13 23:41:52 -0600616};
617
618UCLASS_DRIVER(spi_generic) = {
619 .id = UCLASS_SPI_GENERIC,
620 .name = "spi_generic",
621};
622
623U_BOOT_DRIVER(spi_generic_drv) = {
624 .name = "spi_generic_drv",
625 .id = UCLASS_SPI_GENERIC,
626};