blob: 36b7d383aa9afa42322d447bb07d29f83c2e7d81 [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 <dm.h>
9#include <errno.h>
Simon Glass0f2af882020-05-10 11:40:05 -060010#include <log.h>
Simon Glassdd82d442014-10-13 23:41:52 -060011#include <malloc.h>
12#include <spi.h>
T Karthik Reddy626a7342021-03-17 12:31:30 +010013#include <spi-mem.h>
Patrick Delaunay1d83e032020-10-15 17:18:17 +020014#include <dm/device_compat.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060015#include <asm/global_data.h>
Simon Glassdd82d442014-10-13 23:41:52 -060016#include <dm/device-internal.h>
17#include <dm/uclass-internal.h>
Simon Glassdd82d442014-10-13 23:41:52 -060018#include <dm/lists.h>
19#include <dm/util.h>
20
21DECLARE_GLOBAL_DATA_PTR;
22
Simon Goldschmidta942e1a2018-10-30 21:09:48 +010023#define SPI_DEFAULT_SPEED_HZ 100000
24
Simon Glassdd82d442014-10-13 23:41:52 -060025static int spi_set_speed_mode(struct udevice *bus, int speed, int mode)
26{
27 struct dm_spi_ops *ops;
28 int ret;
29
30 ops = spi_get_ops(bus);
31 if (ops->set_speed)
32 ret = ops->set_speed(bus, speed);
33 else
34 ret = -EINVAL;
35 if (ret) {
Patrick Delaunay1d83e032020-10-15 17:18:17 +020036 dev_err(bus, "Cannot set speed (err=%d)\n", ret);
Simon Glassdd82d442014-10-13 23:41:52 -060037 return ret;
38 }
39
40 if (ops->set_mode)
41 ret = ops->set_mode(bus, mode);
42 else
43 ret = -EINVAL;
44 if (ret) {
Patrick Delaunay1d83e032020-10-15 17:18:17 +020045 dev_err(bus, "Cannot set mode (err=%d)\n", ret);
Simon Glassdd82d442014-10-13 23:41:52 -060046 return ret;
47 }
48
49 return 0;
50}
51
Peng Fanfdd88a32016-05-03 10:02:22 +080052int dm_spi_claim_bus(struct udevice *dev)
Simon Glassdd82d442014-10-13 23:41:52 -060053{
Simon Glassdd82d442014-10-13 23:41:52 -060054 struct udevice *bus = dev->parent;
55 struct dm_spi_ops *ops = spi_get_ops(bus);
Simon Glassde0977b2015-03-05 12:25:20 -070056 struct dm_spi_bus *spi = dev_get_uclass_priv(bus);
Peng Fanfdd88a32016-05-03 10:02:22 +080057 struct spi_slave *slave = dev_get_parent_priv(dev);
Ovidiu Panait40dcee12020-12-14 19:06:50 +020058 uint speed, mode;
Simon Glassdd82d442014-10-13 23:41:52 -060059
60 speed = slave->max_hz;
Ovidiu Panait40dcee12020-12-14 19:06:50 +020061 mode = slave->mode;
62
Simon Glassdd82d442014-10-13 23:41:52 -060063 if (spi->max_hz) {
64 if (speed)
Ovidiu Panait40dcee12020-12-14 19:06:50 +020065 speed = min(speed, spi->max_hz);
Simon Glassdd82d442014-10-13 23:41:52 -060066 else
67 speed = spi->max_hz;
68 }
69 if (!speed)
Simon Goldschmidta942e1a2018-10-30 21:09:48 +010070 speed = SPI_DEFAULT_SPEED_HZ;
Ovidiu Panait40dcee12020-12-14 19:06:50 +020071
72 if (speed != spi->speed || mode != spi->mode) {
Mario Six2d844dd2018-01-15 11:08:41 +010073 int ret = spi_set_speed_mode(bus, speed, slave->mode);
74
Simon Glassb46cb632015-02-17 15:29:35 -070075 if (ret)
Simon Glass00d99902018-10-01 12:22:24 -060076 return log_ret(ret);
Ovidiu Panait40dcee12020-12-14 19:06:50 +020077
78 spi->speed = speed;
79 spi->mode = mode;
Simon Glassb46cb632015-02-17 15:29:35 -070080 }
Simon Glassdd82d442014-10-13 23:41:52 -060081
Simon Glass00d99902018-10-01 12:22:24 -060082 return log_ret(ops->claim_bus ? ops->claim_bus(dev) : 0);
Simon Glassdd82d442014-10-13 23:41:52 -060083}
84
Peng Fanfdd88a32016-05-03 10:02:22 +080085void dm_spi_release_bus(struct udevice *dev)
Simon Glassdd82d442014-10-13 23:41:52 -060086{
Simon Glassdd82d442014-10-13 23:41:52 -060087 struct udevice *bus = dev->parent;
88 struct dm_spi_ops *ops = spi_get_ops(bus);
89
90 if (ops->release_bus)
Simon Glass5c74fba2015-04-19 09:05:40 -060091 ops->release_bus(dev);
Simon Glassdd82d442014-10-13 23:41:52 -060092}
93
Peng Fanfdd88a32016-05-03 10:02:22 +080094int dm_spi_xfer(struct udevice *dev, unsigned int bitlen,
95 const void *dout, void *din, unsigned long flags)
Simon Glassdd82d442014-10-13 23:41:52 -060096{
Simon Glassdd82d442014-10-13 23:41:52 -060097 struct udevice *bus = dev->parent;
Simon Glass2d2e8602019-12-06 21:42:35 -070098 struct dm_spi_ops *ops = spi_get_ops(bus);
Simon Glassdd82d442014-10-13 23:41:52 -060099
100 if (bus->uclass->uc_drv->id != UCLASS_SPI)
101 return -EOPNOTSUPP;
Simon Glass2d2e8602019-12-06 21:42:35 -0700102 if (!ops->xfer)
103 return -ENOSYS;
Simon Glassdd82d442014-10-13 23:41:52 -0600104
Simon Glass2d2e8602019-12-06 21:42:35 -0700105 return ops->xfer(dev, bitlen, dout, din, flags);
Simon Glassdd82d442014-10-13 23:41:52 -0600106}
107
Simon Glass37ad0fe2019-10-20 21:31:47 -0600108int dm_spi_get_mmap(struct udevice *dev, ulong *map_basep, uint *map_sizep,
109 uint *offsetp)
110{
111 struct udevice *bus = dev->parent;
112 struct dm_spi_ops *ops = spi_get_ops(bus);
113
114 if (bus->uclass->uc_drv->id != UCLASS_SPI)
115 return -EOPNOTSUPP;
116 if (!ops->get_mmap)
117 return -ENOSYS;
118
119 return ops->get_mmap(dev, map_basep, map_sizep, offsetp);
120}
121
Peng Fanfdd88a32016-05-03 10:02:22 +0800122int spi_claim_bus(struct spi_slave *slave)
123{
Simon Glass00d99902018-10-01 12:22:24 -0600124 return log_ret(dm_spi_claim_bus(slave->dev));
Peng Fanfdd88a32016-05-03 10:02:22 +0800125}
126
127void spi_release_bus(struct spi_slave *slave)
128{
129 dm_spi_release_bus(slave->dev);
130}
131
Paul Barkerdbb689f2022-10-05 13:18:34 +0100132int spi_set_speed(struct spi_slave *slave, uint hz)
133{
134 struct dm_spi_ops *ops;
135 int ret;
136
137 ops = spi_get_ops(slave->dev->parent);
138 if (ops->set_speed)
139 ret = ops->set_speed(slave->dev->parent, hz);
140 else
141 ret = -EINVAL;
142 if (ret)
143 dev_err(slave->dev, "Cannot set speed (err=%d)\n", ret);
144 return ret;
145}
146
Peng Fanfdd88a32016-05-03 10:02:22 +0800147int spi_xfer(struct spi_slave *slave, unsigned int bitlen,
148 const void *dout, void *din, unsigned long flags)
149{
150 return dm_spi_xfer(slave->dev, bitlen, dout, din, flags);
151}
152
Jagan Teki7cc71fd2019-07-22 17:22:56 +0530153int spi_write_then_read(struct spi_slave *slave, const u8 *opcode,
154 size_t n_opcode, const u8 *txbuf, u8 *rxbuf,
155 size_t n_buf)
156{
157 unsigned long flags = SPI_XFER_BEGIN;
158 int ret;
159
160 if (n_buf == 0)
161 flags |= SPI_XFER_END;
162
163 ret = spi_xfer(slave, n_opcode * 8, opcode, NULL, flags);
164 if (ret) {
Patrick Delaunay1d83e032020-10-15 17:18:17 +0200165 dev_dbg(slave->dev,
166 "spi: failed to send command (%zu bytes): %d\n",
167 n_opcode, ret);
Jagan Teki7cc71fd2019-07-22 17:22:56 +0530168 } else if (n_buf != 0) {
169 ret = spi_xfer(slave, n_buf * 8, txbuf, rxbuf, SPI_XFER_END);
170 if (ret)
Patrick Delaunay1d83e032020-10-15 17:18:17 +0200171 dev_dbg(slave->dev,
172 "spi: failed to transfer %zu bytes of data: %d\n",
173 n_buf, ret);
Jagan Teki7cc71fd2019-07-22 17:22:56 +0530174 }
175
176 return ret;
177}
178
Simon Glass92882652021-08-07 07:24:04 -0600179#if CONFIG_IS_ENABLED(OF_REAL)
Simon Glassd941aaf2015-06-23 15:39:05 -0600180static int spi_child_post_bind(struct udevice *dev)
Simon Glassdd82d442014-10-13 23:41:52 -0600181{
Simon Glassb75b15b2020-12-03 16:55:23 -0700182 struct dm_spi_slave_plat *plat = dev_get_parent_plat(dev);
Simon Glassdd82d442014-10-13 23:41:52 -0600183
Simon Glassf1d50f72020-12-19 10:40:13 -0700184 if (!dev_has_ofnode(dev))
Simon Glass5d2ee052015-01-25 08:27:12 -0700185 return 0;
186
Simon Glassaad29ae2020-12-03 16:55:21 -0700187 return spi_slave_of_to_plat(dev, plat);
Simon Glass5d2ee052015-01-25 08:27:12 -0700188}
Simon Glass3fb33392016-11-13 14:22:01 -0700189#endif
Simon Glass5d2ee052015-01-25 08:27:12 -0700190
Simon Glassd941aaf2015-06-23 15:39:05 -0600191static int spi_post_probe(struct udevice *bus)
Simon Glass5d2ee052015-01-25 08:27:12 -0700192{
Simon Glass6d70ba02021-08-07 07:24:06 -0600193 if (CONFIG_IS_ENABLED(OF_REAL)) {
194 struct dm_spi_bus *spi = dev_get_uclass_priv(bus);
Simon Glass5d2ee052015-01-25 08:27:12 -0700195
Simon Glass6d70ba02021-08-07 07:24:06 -0600196 spi->max_hz = dev_read_u32_default(bus, "spi-max-frequency", 0);
197 }
Michal Simek28d7e4e2015-10-27 13:36:42 +0100198
Simon Glassdd82d442014-10-13 23:41:52 -0600199 return 0;
200}
201
Simon Glassd941aaf2015-06-23 15:39:05 -0600202static int spi_child_pre_probe(struct udevice *dev)
Simon Glass82c2f502015-01-25 08:27:11 -0700203{
Simon Glassb75b15b2020-12-03 16:55:23 -0700204 struct dm_spi_slave_plat *plat = dev_get_parent_plat(dev);
Simon Glassde44acf2015-09-28 23:32:01 -0600205 struct spi_slave *slave = dev_get_parent_priv(dev);
Simon Glass82c2f502015-01-25 08:27:11 -0700206
Simon Glass5d2ee052015-01-25 08:27:12 -0700207 /*
208 * This is needed because we pass struct spi_slave around the place
209 * instead slave->dev (a struct udevice). So we have to have some
210 * way to access the slave udevice given struct spi_slave. Once we
211 * change the SPI API to use udevice instead of spi_slave, we can
212 * drop this.
213 */
Simon Glass82c2f502015-01-25 08:27:11 -0700214 slave->dev = dev;
215
Simon Glass5d2ee052015-01-25 08:27:12 -0700216 slave->max_hz = plat->max_hz;
217 slave->mode = plat->mode;
Christophe Ricardfb0c53e2016-01-17 11:56:48 +0100218 slave->wordlen = SPI_DEFAULT_WORDLEN;
Simon Glass5d2ee052015-01-25 08:27:12 -0700219
Simon Glass82c2f502015-01-25 08:27:11 -0700220 return 0;
221}
222
Simon Glassdd82d442014-10-13 23:41:52 -0600223int spi_chip_select(struct udevice *dev)
224{
Simon Glassb75b15b2020-12-03 16:55:23 -0700225 struct dm_spi_slave_plat *plat = dev_get_parent_plat(dev);
Simon Glassdd82d442014-10-13 23:41:52 -0600226
Venkatesh Yadav Abbarapu91b9e372024-09-26 10:25:05 +0530227 return plat ? plat->cs[0] : -ENOENT;
Simon Glassdd82d442014-10-13 23:41:52 -0600228}
229
Simon Glass5ef36f22014-11-11 10:46:22 -0700230int spi_find_chip_select(struct udevice *bus, int cs, struct udevice **devp)
Simon Glassdd82d442014-10-13 23:41:52 -0600231{
Bin Meng9741e732019-09-09 06:00:02 -0700232 struct dm_spi_ops *ops;
233 struct spi_cs_info info;
Simon Glassdd82d442014-10-13 23:41:52 -0600234 struct udevice *dev;
Bin Meng9741e732019-09-09 06:00:02 -0700235 int ret;
236
237 /*
238 * Ask the driver. For the moment we don't have CS info.
239 * When we do we could provide the driver with a helper function
240 * to figure out what chip selects are valid, or just handle the
241 * request.
242 */
243 ops = spi_get_ops(bus);
244 if (ops->cs_info) {
245 ret = ops->cs_info(bus, cs, &info);
246 } else {
247 /*
248 * We could assume there is at least one valid chip select.
249 * The driver didn't care enough to tell us.
250 */
251 ret = 0;
252 }
253
254 if (ret) {
Patrick Delaunay1d83e032020-10-15 17:18:17 +0200255 dev_err(bus, "Invalid cs %d (err=%d)\n", cs, ret);
Bin Meng9741e732019-09-09 06:00:02 -0700256 return ret;
257 }
Simon Glassdd82d442014-10-13 23:41:52 -0600258
259 for (device_find_first_child(bus, &dev); dev;
260 device_find_next_child(&dev)) {
Simon Glassb75b15b2020-12-03 16:55:23 -0700261 struct dm_spi_slave_plat *plat;
Simon Glassdd82d442014-10-13 23:41:52 -0600262
Simon Glass71fa5b42020-12-03 16:55:18 -0700263 plat = dev_get_parent_plat(dev);
Venkatesh Yadav Abbarapu91b9e372024-09-26 10:25:05 +0530264 dev_dbg(bus, "%s: plat=%p, cs=%d\n", __func__, plat, plat->cs[0]);
265 if (plat->cs[0] == cs) {
Simon Glassdd82d442014-10-13 23:41:52 -0600266 *devp = dev;
267 return 0;
268 }
269 }
270
271 return -ENODEV;
272}
273
274int spi_cs_is_valid(unsigned int busnum, unsigned int cs)
275{
276 struct spi_cs_info info;
277 struct udevice *bus;
278 int ret;
279
Simon Glass07e13382020-12-16 21:20:29 -0700280 ret = uclass_find_device_by_seq(UCLASS_SPI, busnum, &bus);
Simon Glassdd82d442014-10-13 23:41:52 -0600281 if (ret) {
Patrick Delaunay1d83e032020-10-15 17:18:17 +0200282 log_debug("%s: No bus %d\n", __func__, busnum);
Simon Glassdd82d442014-10-13 23:41:52 -0600283 return ret;
284 }
285
286 return spi_cs_info(bus, cs, &info);
287}
288
289int spi_cs_info(struct udevice *bus, uint cs, struct spi_cs_info *info)
290{
291 struct spi_cs_info local_info;
Simon Glassdd82d442014-10-13 23:41:52 -0600292 int ret;
293
294 if (!info)
295 info = &local_info;
296
297 /* If there is a device attached, return it */
298 info->dev = NULL;
299 ret = spi_find_chip_select(bus, cs, &info->dev);
Bin Meng9741e732019-09-09 06:00:02 -0700300 return ret == -ENODEV ? 0 : ret;
Simon Glassdd82d442014-10-13 23:41:52 -0600301}
302
Simon Glassdd82d442014-10-13 23:41:52 -0600303int spi_find_bus_and_cs(int busnum, int cs, struct udevice **busp,
304 struct udevice **devp)
305{
306 struct udevice *bus, *dev;
307 int ret;
308
Simon Glass07e13382020-12-16 21:20:29 -0700309 ret = uclass_find_device_by_seq(UCLASS_SPI, busnum, &bus);
Simon Glassdd82d442014-10-13 23:41:52 -0600310 if (ret) {
Patrick Delaunay1d83e032020-10-15 17:18:17 +0200311 log_debug("%s: No bus %d\n", __func__, busnum);
Simon Glassdd82d442014-10-13 23:41:52 -0600312 return ret;
313 }
314 ret = spi_find_chip_select(bus, cs, &dev);
315 if (ret) {
Patrick Delaunay1d83e032020-10-15 17:18:17 +0200316 dev_dbg(bus, "%s: No cs %d\n", __func__, cs);
Simon Glassdd82d442014-10-13 23:41:52 -0600317 return ret;
318 }
319 *busp = bus;
320 *devp = dev;
321
322 return ret;
323}
324
Patrice Chotard86e06ae2022-03-30 09:33:13 +0200325int spi_get_bus_and_cs(int busnum, int cs, struct udevice **busp,
326 struct spi_slave **devp)
Simon Glassdd82d442014-10-13 23:41:52 -0600327{
328 struct udevice *bus, *dev;
Patrice Chotard86e06ae2022-03-30 09:33:13 +0200329 struct dm_spi_bus *bus_data;
330 struct spi_slave *slave;
331 int ret;
332
333#if CONFIG_IS_ENABLED(OF_PLATDATA)
334 ret = uclass_first_device_err(UCLASS_SPI, &bus);
335#else
336 ret = uclass_get_device_by_seq(UCLASS_SPI, busnum, &bus);
337#endif
338 if (ret) {
339 log_err("Invalid bus %d (err=%d)\n", busnum, ret);
340 return ret;
341 }
342 ret = spi_find_chip_select(bus, cs, &dev);
343 if (ret) {
344 dev_err(bus, "Invalid chip select %d:%d (err=%d)\n", busnum, cs, ret);
345 return ret;
346 }
347
348 if (!device_active(dev)) {
349 struct spi_slave *slave;
350
351 ret = device_probe(dev);
352 if (ret)
353 goto err;
354 slave = dev_get_parent_priv(dev);
355 slave->dev = dev;
356 }
357
358 slave = dev_get_parent_priv(dev);
359 bus_data = dev_get_uclass_priv(bus);
360
361 /*
362 * In case the operation speed is not yet established by
363 * dm_spi_claim_bus() ensure the bus is configured properly.
364 */
365 if (!bus_data->speed) {
366 ret = spi_claim_bus(slave);
367 if (ret)
368 goto err;
369 }
370 *busp = bus;
371 *devp = slave;
372
373 return 0;
374
375err:
376 log_debug("%s: Error path, device '%s'\n", __func__, dev->name);
377
378 return ret;
379}
380
381int _spi_get_bus_and_cs(int busnum, int cs, int speed, int mode,
382 const char *drv_name, const char *dev_name,
383 struct udevice **busp, struct spi_slave **devp)
384{
385 struct udevice *bus, *dev;
Simon Glassb75b15b2020-12-03 16:55:23 -0700386 struct dm_spi_slave_plat *plat;
Ovidiu Panait40dcee12020-12-14 19:06:50 +0200387 struct dm_spi_bus *bus_data;
Marcin Wojtas70d8f982019-11-21 05:38:47 +0100388 struct spi_slave *slave;
Simon Glassdd82d442014-10-13 23:41:52 -0600389 bool created = false;
390 int ret;
391
Thomas Fitzsimmons59c90f32019-09-06 07:51:19 -0400392#if CONFIG_IS_ENABLED(OF_PLATDATA)
Simon Glass3fb33392016-11-13 14:22:01 -0700393 ret = uclass_first_device_err(UCLASS_SPI, &bus);
394#else
Simon Glassdd82d442014-10-13 23:41:52 -0600395 ret = uclass_get_device_by_seq(UCLASS_SPI, busnum, &bus);
Simon Glass3fb33392016-11-13 14:22:01 -0700396#endif
Simon Glassdd82d442014-10-13 23:41:52 -0600397 if (ret) {
Patrick Delaunay1d83e032020-10-15 17:18:17 +0200398 log_err("Invalid bus %d (err=%d)\n", busnum, ret);
Simon Glassdd82d442014-10-13 23:41:52 -0600399 return ret;
400 }
401 ret = spi_find_chip_select(bus, cs, &dev);
402
403 /*
404 * If there is no such device, create one automatically. This means
405 * that we don't need a device tree node or platform data for the
406 * SPI flash chip - we will bind to the correct driver.
407 */
408 if (ret == -ENODEV && drv_name) {
Patrick Delaunay1d83e032020-10-15 17:18:17 +0200409 dev_dbg(bus, "%s: Binding new device '%s', busnum=%d, cs=%d, driver=%s\n",
410 __func__, dev_name, busnum, cs, drv_name);
Simon Glassd8a21f62014-11-11 10:46:23 -0700411 ret = device_bind_driver(bus, drv_name, dev_name, &dev);
Simon Glassf0307e12016-11-13 14:22:05 -0700412 if (ret) {
Patrick Delaunay1d83e032020-10-15 17:18:17 +0200413 dev_dbg(bus, "%s: Unable to bind driver (ret=%d)\n",
414 __func__, ret);
Simon Glassdd82d442014-10-13 23:41:52 -0600415 return ret;
Simon Glassf0307e12016-11-13 14:22:05 -0700416 }
Simon Glass71fa5b42020-12-03 16:55:18 -0700417 plat = dev_get_parent_plat(dev);
Venkatesh Yadav Abbarapu91b9e372024-09-26 10:25:05 +0530418 plat->cs[0] = cs;
Simon Goldschmidta942e1a2018-10-30 21:09:48 +0100419 if (speed) {
420 plat->max_hz = speed;
421 } else {
Patrick Delaunay1d83e032020-10-15 17:18:17 +0200422 dev_warn(bus,
423 "Warning: SPI speed fallback to %u kHz\n",
424 SPI_DEFAULT_SPEED_HZ / 1000);
Simon Goldschmidta942e1a2018-10-30 21:09:48 +0100425 plat->max_hz = SPI_DEFAULT_SPEED_HZ;
426 }
Simon Glass5d2ee052015-01-25 08:27:12 -0700427 plat->mode = mode;
Simon Glassdd82d442014-10-13 23:41:52 -0600428 created = true;
429 } else if (ret) {
Patrick Delaunay1d83e032020-10-15 17:18:17 +0200430 dev_err(bus, "Invalid chip select %d:%d (err=%d)\n", busnum, cs, ret);
Simon Glassdd82d442014-10-13 23:41:52 -0600431 return ret;
Marek Vasut170013f2021-06-10 14:00:00 +0200432 } else if (dev) {
433 plat = dev_get_parent_plat(dev);
Simon Glassdd82d442014-10-13 23:41:52 -0600434 }
435
436 if (!device_active(dev)) {
Simon Glass5d2ee052015-01-25 08:27:12 -0700437 struct spi_slave *slave;
Simon Glassdd82d442014-10-13 23:41:52 -0600438
Simon Glass5d2ee052015-01-25 08:27:12 -0700439 ret = device_probe(dev);
Simon Glassdd82d442014-10-13 23:41:52 -0600440 if (ret)
441 goto err;
Simon Glassde44acf2015-09-28 23:32:01 -0600442 slave = dev_get_parent_priv(dev);
Simon Glassdd82d442014-10-13 23:41:52 -0600443 slave->dev = dev;
Simon Glassdd82d442014-10-13 23:41:52 -0600444 }
445
Marcin Wojtas70d8f982019-11-21 05:38:47 +0100446 slave = dev_get_parent_priv(dev);
Ovidiu Panait40dcee12020-12-14 19:06:50 +0200447 bus_data = dev_get_uclass_priv(bus);
Patrick Delaunayfa19c652019-02-27 15:36:44 +0100448
Venkatesh Yadav Abbarapu91b9e372024-09-26 10:25:05 +0530449#if CONFIG_IS_ENABLED(SPI_ADVANCE)
450 if ((dev_read_bool(dev, "parallel-memories")) && !slave->multi_cs_cap) {
451 dev_err(dev, "controller doesn't support multi CS\n");
452 return -EINVAL;
453 }
454#endif
Marcin Wojtas70d8f982019-11-21 05:38:47 +0100455 /*
456 * In case the operation speed is not yet established by
457 * dm_spi_claim_bus() ensure the bus is configured properly.
458 */
Ovidiu Panait40dcee12020-12-14 19:06:50 +0200459 if (!bus_data->speed) {
Marcin Wojtas70d8f982019-11-21 05:38:47 +0100460 ret = spi_claim_bus(slave);
461 if (ret)
462 goto err;
Vignesh Rae569792016-07-06 10:04:28 +0530463 }
Simon Glassdd82d442014-10-13 23:41:52 -0600464
Marek Vasut170013f2021-06-10 14:00:00 +0200465 /* In case bus frequency or mode changed, update it. */
466 if ((speed && bus_data->speed && bus_data->speed != speed) ||
467 (plat && plat->mode != mode)) {
468 ret = spi_set_speed_mode(bus, speed, mode);
469 if (ret)
470 goto err_speed_mode;
471 }
472
Simon Glassdd82d442014-10-13 23:41:52 -0600473 *busp = bus;
Marcin Wojtas70d8f982019-11-21 05:38:47 +0100474 *devp = slave;
Patrick Delaunay1d83e032020-10-15 17:18:17 +0200475 log_debug("%s: bus=%p, slave=%p\n", __func__, bus, *devp);
Simon Glassdd82d442014-10-13 23:41:52 -0600476
477 return 0;
478
Marek Vasut170013f2021-06-10 14:00:00 +0200479err_speed_mode:
480 spi_release_bus(slave);
Simon Glassdd82d442014-10-13 23:41:52 -0600481err:
Patrick Delaunay1d83e032020-10-15 17:18:17 +0200482 log_debug("%s: Error path, created=%d, device '%s'\n", __func__,
483 created, dev->name);
Simon Glassdd82d442014-10-13 23:41:52 -0600484 if (created) {
Stefan Roese80b5bc92017-03-20 12:51:48 +0100485 device_remove(dev, DM_REMOVE_NORMAL);
Simon Glassdd82d442014-10-13 23:41:52 -0600486 device_unbind(dev);
487 }
488
489 return ret;
490}
491
492/* Compatibility function - to be removed */
Simon Glassdd82d442014-10-13 23:41:52 -0600493struct spi_slave *spi_setup_slave(unsigned int busnum, unsigned int cs,
494 unsigned int speed, unsigned int mode)
495{
496 struct spi_slave *slave;
497 struct udevice *dev;
498 int ret;
499
Patrice Chotard86e06ae2022-03-30 09:33:13 +0200500 ret = _spi_get_bus_and_cs(busnum, cs, speed, mode, NULL, 0, &dev,
501 &slave);
Simon Glassdd82d442014-10-13 23:41:52 -0600502 if (ret)
503 return NULL;
504
505 return slave;
506}
507
508void spi_free_slave(struct spi_slave *slave)
509{
Stefan Roese80b5bc92017-03-20 12:51:48 +0100510 device_remove(slave->dev, DM_REMOVE_NORMAL);
Simon Glassdd82d442014-10-13 23:41:52 -0600511}
512
Simon Glassb75b15b2020-12-03 16:55:23 -0700513int spi_slave_of_to_plat(struct udevice *dev, struct dm_spi_slave_plat *plat)
Simon Glassdd82d442014-10-13 23:41:52 -0600514{
Jagan Teki96536b12016-08-08 17:12:12 +0530515 int mode = 0;
Mugunthan V N4b0f40c2015-12-23 20:39:37 +0530516 int value;
Simon Glassdd82d442014-10-13 23:41:52 -0600517
Venkatesh Yadav Abbarapu91b9e372024-09-26 10:25:05 +0530518#if CONFIG_IS_ENABLED(SPI_ADVANCE)
519 int ret;
520
521 ret = dev_read_u32_array(dev, "reg", plat->cs, SPI_CS_CNT_MAX);
522
523 if (ret == -EOVERFLOW || ret == -FDT_ERR_BADLAYOUT) {
524 dev_read_u32(dev, "reg", &plat->cs[0]);
525 } else {
526 dev_err(dev, "has no valid 'reg' property (%d)\n", ret);
527 return ret;
528 }
529#else
530 plat->cs[0] = dev_read_u32_default(dev, "reg", -1);
531#endif
532
Simon Goldschmidta942e1a2018-10-30 21:09:48 +0100533 plat->max_hz = dev_read_u32_default(dev, "spi-max-frequency",
534 SPI_DEFAULT_SPEED_HZ);
Simon Glass4b280582017-05-18 20:09:54 -0600535 if (dev_read_bool(dev, "spi-cpol"))
Simon Glassdd82d442014-10-13 23:41:52 -0600536 mode |= SPI_CPOL;
Simon Glass4b280582017-05-18 20:09:54 -0600537 if (dev_read_bool(dev, "spi-cpha"))
Simon Glassdd82d442014-10-13 23:41:52 -0600538 mode |= SPI_CPHA;
Simon Glass4b280582017-05-18 20:09:54 -0600539 if (dev_read_bool(dev, "spi-cs-high"))
Simon Glassdd82d442014-10-13 23:41:52 -0600540 mode |= SPI_CS_HIGH;
Simon Glass4b280582017-05-18 20:09:54 -0600541 if (dev_read_bool(dev, "spi-3wire"))
Jagan Tekid3868fd2015-12-03 22:19:05 +0530542 mode |= SPI_3WIRE;
Simon Glass4b280582017-05-18 20:09:54 -0600543 if (dev_read_bool(dev, "spi-half-duplex"))
Simon Glassdd82d442014-10-13 23:41:52 -0600544 mode |= SPI_PREAMBLE;
Mugunthan V N4b0f40c2015-12-23 20:39:37 +0530545
546 /* Device DUAL/QUAD mode */
Simon Glass4b280582017-05-18 20:09:54 -0600547 value = dev_read_u32_default(dev, "spi-tx-bus-width", 1);
Mugunthan V N4b0f40c2015-12-23 20:39:37 +0530548 switch (value) {
549 case 1:
550 break;
551 case 2:
552 mode |= SPI_TX_DUAL;
553 break;
554 case 4:
555 mode |= SPI_TX_QUAD;
556 break;
Vignesh Raghavendrac063ee32019-12-05 15:46:05 +0530557 case 8:
558 mode |= SPI_TX_OCTAL;
559 break;
Mugunthan V N4b0f40c2015-12-23 20:39:37 +0530560 default:
Simon Glass5c1c9ea2024-09-29 19:49:41 -0600561 warn_non_xpl("spi-tx-bus-width %d not supported\n", value);
Mugunthan V N4b0f40c2015-12-23 20:39:37 +0530562 break;
563 }
564
Simon Glass4b280582017-05-18 20:09:54 -0600565 value = dev_read_u32_default(dev, "spi-rx-bus-width", 1);
Mugunthan V N4b0f40c2015-12-23 20:39:37 +0530566 switch (value) {
567 case 1:
568 break;
569 case 2:
Jagan Teki96536b12016-08-08 17:12:12 +0530570 mode |= SPI_RX_DUAL;
Mugunthan V N4b0f40c2015-12-23 20:39:37 +0530571 break;
572 case 4:
Jagan Teki96536b12016-08-08 17:12:12 +0530573 mode |= SPI_RX_QUAD;
Mugunthan V N4b0f40c2015-12-23 20:39:37 +0530574 break;
Vignesh Raghavendrac063ee32019-12-05 15:46:05 +0530575 case 8:
576 mode |= SPI_RX_OCTAL;
577 break;
Mugunthan V N4b0f40c2015-12-23 20:39:37 +0530578 default:
Simon Glass5c1c9ea2024-09-29 19:49:41 -0600579 warn_non_xpl("spi-rx-bus-width %d not supported\n", value);
Mugunthan V N4b0f40c2015-12-23 20:39:37 +0530580 break;
581 }
582
Jagan Teki96536b12016-08-08 17:12:12 +0530583 plat->mode = mode;
Mugunthan V N4b0f40c2015-12-23 20:39:37 +0530584
Simon Glassdd82d442014-10-13 23:41:52 -0600585 return 0;
586}
587
588UCLASS_DRIVER(spi) = {
589 .id = UCLASS_SPI,
590 .name = "spi",
Simon Glass0ccb0972015-01-25 08:27:05 -0700591 .flags = DM_UC_FLAG_SEQ_ALIAS,
Simon Glass3580f6d2021-08-07 07:24:03 -0600592#if CONFIG_IS_ENABLED(OF_REAL)
Simon Glass18230342016-07-05 17:10:10 -0600593 .post_bind = dm_scan_fdt_dev,
Simon Glass3fb33392016-11-13 14:22:01 -0700594#endif
Simon Glassdd82d442014-10-13 23:41:52 -0600595 .post_probe = spi_post_probe,
Simon Glass82c2f502015-01-25 08:27:11 -0700596 .child_pre_probe = spi_child_pre_probe,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700597 .per_device_auto = sizeof(struct dm_spi_bus),
598 .per_child_auto = sizeof(struct spi_slave),
Simon Glassb75b15b2020-12-03 16:55:23 -0700599 .per_child_plat_auto = sizeof(struct dm_spi_slave_plat),
Simon Glass92882652021-08-07 07:24:04 -0600600#if CONFIG_IS_ENABLED(OF_REAL)
Simon Glass5d2ee052015-01-25 08:27:12 -0700601 .child_post_bind = spi_child_post_bind,
Simon Glass3fb33392016-11-13 14:22:01 -0700602#endif
Simon Glassdd82d442014-10-13 23:41:52 -0600603};
604
605UCLASS_DRIVER(spi_generic) = {
606 .id = UCLASS_SPI_GENERIC,
607 .name = "spi_generic",
608};
609
610U_BOOT_DRIVER(spi_generic_drv) = {
611 .name = "spi_generic_drv",
612 .id = UCLASS_SPI_GENERIC,
613};