blob: d867b278064f3f0dbaca04fd77cd8f0ab69016a2 [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
133int spi_xfer(struct spi_slave *slave, unsigned int bitlen,
134 const void *dout, void *din, unsigned long flags)
135{
136 return dm_spi_xfer(slave->dev, bitlen, dout, din, flags);
137}
138
Jagan Teki8473b322019-07-22 17:22:56 +0530139int spi_write_then_read(struct spi_slave *slave, const u8 *opcode,
140 size_t n_opcode, const u8 *txbuf, u8 *rxbuf,
141 size_t n_buf)
142{
143 unsigned long flags = SPI_XFER_BEGIN;
144 int ret;
145
146 if (n_buf == 0)
147 flags |= SPI_XFER_END;
148
149 ret = spi_xfer(slave, n_opcode * 8, opcode, NULL, flags);
150 if (ret) {
Patrick Delaunay46c53912020-10-15 17:18:17 +0200151 dev_dbg(slave->dev,
152 "spi: failed to send command (%zu bytes): %d\n",
153 n_opcode, ret);
Jagan Teki8473b322019-07-22 17:22:56 +0530154 } else if (n_buf != 0) {
155 ret = spi_xfer(slave, n_buf * 8, txbuf, rxbuf, SPI_XFER_END);
156 if (ret)
Patrick Delaunay46c53912020-10-15 17:18:17 +0200157 dev_dbg(slave->dev,
158 "spi: failed to transfer %zu bytes of data: %d\n",
159 n_buf, ret);
Jagan Teki8473b322019-07-22 17:22:56 +0530160 }
161
162 return ret;
163}
164
Simon Glass71634f22016-11-13 14:22:01 -0700165#if !CONFIG_IS_ENABLED(OF_PLATDATA)
Simon Glass6f849c32015-06-23 15:39:05 -0600166static int spi_child_post_bind(struct udevice *dev)
Simon Glassd7af6a42014-10-13 23:41:52 -0600167{
Simon Glass8a8d24b2020-12-03 16:55:23 -0700168 struct dm_spi_slave_plat *plat = dev_get_parent_plat(dev);
Simon Glassd7af6a42014-10-13 23:41:52 -0600169
Simon Glass7d14ee42020-12-19 10:40:13 -0700170 if (!dev_has_ofnode(dev))
Simon Glassd0cff032015-01-25 08:27:12 -0700171 return 0;
172
Simon Glassd1998a92020-12-03 16:55:21 -0700173 return spi_slave_of_to_plat(dev, plat);
Simon Glassd0cff032015-01-25 08:27:12 -0700174}
Simon Glass71634f22016-11-13 14:22:01 -0700175#endif
Simon Glassd0cff032015-01-25 08:27:12 -0700176
Simon Glass6f849c32015-06-23 15:39:05 -0600177static int spi_post_probe(struct udevice *bus)
Simon Glassd0cff032015-01-25 08:27:12 -0700178{
Simon Glass71634f22016-11-13 14:22:01 -0700179#if !CONFIG_IS_ENABLED(OF_PLATDATA)
Simon Glasse564f052015-03-05 12:25:20 -0700180 struct dm_spi_bus *spi = dev_get_uclass_priv(bus);
Simon Glassd0cff032015-01-25 08:27:12 -0700181
Simon Glass279e26f2017-05-18 20:09:54 -0600182 spi->max_hz = dev_read_u32_default(bus, "spi-max-frequency", 0);
Simon Glass71634f22016-11-13 14:22:01 -0700183#endif
Michal Simek281f1562015-10-27 13:36:42 +0100184#if defined(CONFIG_NEEDS_MANUAL_RELOC)
185 struct dm_spi_ops *ops = spi_get_ops(bus);
Ashok Reddy Soma4d9b1af2019-09-17 00:11:02 -0600186 static int reloc_done;
Michal Simek281f1562015-10-27 13:36:42 +0100187
Ashok Reddy Soma4d9b1af2019-09-17 00:11:02 -0600188 if (!reloc_done) {
189 if (ops->claim_bus)
190 ops->claim_bus += gd->reloc_off;
191 if (ops->release_bus)
192 ops->release_bus += gd->reloc_off;
193 if (ops->set_wordlen)
194 ops->set_wordlen += gd->reloc_off;
195 if (ops->xfer)
196 ops->xfer += gd->reloc_off;
197 if (ops->set_speed)
198 ops->set_speed += gd->reloc_off;
199 if (ops->set_mode)
200 ops->set_mode += gd->reloc_off;
201 if (ops->cs_info)
202 ops->cs_info += gd->reloc_off;
T Karthik Reddy346df7d2021-03-17 12:31:30 +0100203 if (ops->mem_ops) {
204 struct spi_controller_mem_ops *mem_ops =
205 (struct spi_controller_mem_ops *)ops->mem_ops;
206 if (mem_ops->adjust_op_size)
207 mem_ops->adjust_op_size += gd->reloc_off;
208 if (mem_ops->supports_op)
209 mem_ops->supports_op += gd->reloc_off;
210 if (mem_ops->exec_op)
211 mem_ops->exec_op += gd->reloc_off;
212 }
Ashok Reddy Soma4d9b1af2019-09-17 00:11:02 -0600213 reloc_done++;
214 }
Michal Simek281f1562015-10-27 13:36:42 +0100215#endif
216
Simon Glassd7af6a42014-10-13 23:41:52 -0600217 return 0;
218}
219
Simon Glass6f849c32015-06-23 15:39:05 -0600220static int spi_child_pre_probe(struct udevice *dev)
Simon Glass440714e2015-01-25 08:27:11 -0700221{
Simon Glass8a8d24b2020-12-03 16:55:23 -0700222 struct dm_spi_slave_plat *plat = dev_get_parent_plat(dev);
Simon Glassbcbe3d12015-09-28 23:32:01 -0600223 struct spi_slave *slave = dev_get_parent_priv(dev);
Simon Glass440714e2015-01-25 08:27:11 -0700224
Simon Glassd0cff032015-01-25 08:27:12 -0700225 /*
226 * This is needed because we pass struct spi_slave around the place
227 * instead slave->dev (a struct udevice). So we have to have some
228 * way to access the slave udevice given struct spi_slave. Once we
229 * change the SPI API to use udevice instead of spi_slave, we can
230 * drop this.
231 */
Simon Glass440714e2015-01-25 08:27:11 -0700232 slave->dev = dev;
233
Simon Glassd0cff032015-01-25 08:27:12 -0700234 slave->max_hz = plat->max_hz;
235 slave->mode = plat->mode;
Christophe Ricard674f3602016-01-17 11:56:48 +0100236 slave->wordlen = SPI_DEFAULT_WORDLEN;
Simon Glassd0cff032015-01-25 08:27:12 -0700237
Simon Glass440714e2015-01-25 08:27:11 -0700238 return 0;
239}
240
Simon Glassd7af6a42014-10-13 23:41:52 -0600241int spi_chip_select(struct udevice *dev)
242{
Simon Glass8a8d24b2020-12-03 16:55:23 -0700243 struct dm_spi_slave_plat *plat = dev_get_parent_plat(dev);
Simon Glassd7af6a42014-10-13 23:41:52 -0600244
Simon Glassd0cff032015-01-25 08:27:12 -0700245 return plat ? plat->cs : -ENOENT;
Simon Glassd7af6a42014-10-13 23:41:52 -0600246}
247
Simon Glassff56bba2014-11-11 10:46:22 -0700248int spi_find_chip_select(struct udevice *bus, int cs, struct udevice **devp)
Simon Glassd7af6a42014-10-13 23:41:52 -0600249{
Bin Meng7bacce52019-09-09 06:00:02 -0700250 struct dm_spi_ops *ops;
251 struct spi_cs_info info;
Simon Glassd7af6a42014-10-13 23:41:52 -0600252 struct udevice *dev;
Bin Meng7bacce52019-09-09 06:00:02 -0700253 int ret;
254
255 /*
256 * Ask the driver. For the moment we don't have CS info.
257 * When we do we could provide the driver with a helper function
258 * to figure out what chip selects are valid, or just handle the
259 * request.
260 */
261 ops = spi_get_ops(bus);
262 if (ops->cs_info) {
263 ret = ops->cs_info(bus, cs, &info);
264 } else {
265 /*
266 * We could assume there is at least one valid chip select.
267 * The driver didn't care enough to tell us.
268 */
269 ret = 0;
270 }
271
272 if (ret) {
Patrick Delaunay46c53912020-10-15 17:18:17 +0200273 dev_err(bus, "Invalid cs %d (err=%d)\n", cs, ret);
Bin Meng7bacce52019-09-09 06:00:02 -0700274 return ret;
275 }
Simon Glassd7af6a42014-10-13 23:41:52 -0600276
277 for (device_find_first_child(bus, &dev); dev;
278 device_find_next_child(&dev)) {
Simon Glass8a8d24b2020-12-03 16:55:23 -0700279 struct dm_spi_slave_plat *plat;
Simon Glassd7af6a42014-10-13 23:41:52 -0600280
Simon Glasscaa4daa2020-12-03 16:55:18 -0700281 plat = dev_get_parent_plat(dev);
Patrick Delaunay46c53912020-10-15 17:18:17 +0200282 dev_dbg(bus, "%s: plat=%p, cs=%d\n", __func__, plat, plat->cs);
Simon Glassd0cff032015-01-25 08:27:12 -0700283 if (plat->cs == cs) {
Simon Glassd7af6a42014-10-13 23:41:52 -0600284 *devp = dev;
285 return 0;
286 }
287 }
288
289 return -ENODEV;
290}
291
292int spi_cs_is_valid(unsigned int busnum, unsigned int cs)
293{
294 struct spi_cs_info info;
295 struct udevice *bus;
296 int ret;
297
Simon Glass99175912020-12-16 21:20:29 -0700298 ret = uclass_find_device_by_seq(UCLASS_SPI, busnum, &bus);
Simon Glassd7af6a42014-10-13 23:41:52 -0600299 if (ret) {
Patrick Delaunay46c53912020-10-15 17:18:17 +0200300 log_debug("%s: No bus %d\n", __func__, busnum);
Simon Glassd7af6a42014-10-13 23:41:52 -0600301 return ret;
302 }
303
304 return spi_cs_info(bus, cs, &info);
305}
306
307int spi_cs_info(struct udevice *bus, uint cs, struct spi_cs_info *info)
308{
309 struct spi_cs_info local_info;
Simon Glassd7af6a42014-10-13 23:41:52 -0600310 int ret;
311
312 if (!info)
313 info = &local_info;
314
315 /* If there is a device attached, return it */
316 info->dev = NULL;
317 ret = spi_find_chip_select(bus, cs, &info->dev);
Bin Meng7bacce52019-09-09 06:00:02 -0700318 return ret == -ENODEV ? 0 : ret;
Simon Glassd7af6a42014-10-13 23:41:52 -0600319}
320
Simon Glassd7af6a42014-10-13 23:41:52 -0600321int spi_find_bus_and_cs(int busnum, int cs, struct udevice **busp,
322 struct udevice **devp)
323{
324 struct udevice *bus, *dev;
325 int ret;
326
Simon Glass99175912020-12-16 21:20:29 -0700327 ret = uclass_find_device_by_seq(UCLASS_SPI, busnum, &bus);
Simon Glassd7af6a42014-10-13 23:41:52 -0600328 if (ret) {
Patrick Delaunay46c53912020-10-15 17:18:17 +0200329 log_debug("%s: No bus %d\n", __func__, busnum);
Simon Glassd7af6a42014-10-13 23:41:52 -0600330 return ret;
331 }
332 ret = spi_find_chip_select(bus, cs, &dev);
333 if (ret) {
Patrick Delaunay46c53912020-10-15 17:18:17 +0200334 dev_dbg(bus, "%s: No cs %d\n", __func__, cs);
Simon Glassd7af6a42014-10-13 23:41:52 -0600335 return ret;
336 }
337 *busp = bus;
338 *devp = dev;
339
340 return ret;
341}
342
343int spi_get_bus_and_cs(int busnum, int cs, int speed, int mode,
344 const char *drv_name, const char *dev_name,
345 struct udevice **busp, struct spi_slave **devp)
346{
347 struct udevice *bus, *dev;
Simon Glass8a8d24b2020-12-03 16:55:23 -0700348 struct dm_spi_slave_plat *plat;
Ovidiu Panait741280e2020-12-14 19:06:50 +0200349 struct dm_spi_bus *bus_data;
Marcin Wojtasf7dd5372019-11-21 05:38:47 +0100350 struct spi_slave *slave;
Simon Glassd7af6a42014-10-13 23:41:52 -0600351 bool created = false;
352 int ret;
353
Thomas Fitzsimmons640abba2019-09-06 07:51:19 -0400354#if CONFIG_IS_ENABLED(OF_PLATDATA)
Simon Glass71634f22016-11-13 14:22:01 -0700355 ret = uclass_first_device_err(UCLASS_SPI, &bus);
356#else
Simon Glassd7af6a42014-10-13 23:41:52 -0600357 ret = uclass_get_device_by_seq(UCLASS_SPI, busnum, &bus);
Simon Glass71634f22016-11-13 14:22:01 -0700358#endif
Simon Glassd7af6a42014-10-13 23:41:52 -0600359 if (ret) {
Patrick Delaunay46c53912020-10-15 17:18:17 +0200360 log_err("Invalid bus %d (err=%d)\n", busnum, ret);
Simon Glassd7af6a42014-10-13 23:41:52 -0600361 return ret;
362 }
363 ret = spi_find_chip_select(bus, cs, &dev);
364
365 /*
366 * If there is no such device, create one automatically. This means
367 * that we don't need a device tree node or platform data for the
368 * SPI flash chip - we will bind to the correct driver.
369 */
370 if (ret == -ENODEV && drv_name) {
Patrick Delaunay46c53912020-10-15 17:18:17 +0200371 dev_dbg(bus, "%s: Binding new device '%s', busnum=%d, cs=%d, driver=%s\n",
372 __func__, dev_name, busnum, cs, drv_name);
Simon Glass6b186562014-11-11 10:46:23 -0700373 ret = device_bind_driver(bus, drv_name, dev_name, &dev);
Simon Glass28f98852016-11-13 14:22:05 -0700374 if (ret) {
Patrick Delaunay46c53912020-10-15 17:18:17 +0200375 dev_dbg(bus, "%s: Unable to bind driver (ret=%d)\n",
376 __func__, ret);
Simon Glassd7af6a42014-10-13 23:41:52 -0600377 return ret;
Simon Glass28f98852016-11-13 14:22:05 -0700378 }
Simon Glasscaa4daa2020-12-03 16:55:18 -0700379 plat = dev_get_parent_plat(dev);
Simon Glassd0cff032015-01-25 08:27:12 -0700380 plat->cs = cs;
Simon Goldschmidt12bfb2e2018-10-30 21:09:48 +0100381 if (speed) {
382 plat->max_hz = speed;
383 } else {
Patrick Delaunay46c53912020-10-15 17:18:17 +0200384 dev_warn(bus,
385 "Warning: SPI speed fallback to %u kHz\n",
386 SPI_DEFAULT_SPEED_HZ / 1000);
Simon Goldschmidt12bfb2e2018-10-30 21:09:48 +0100387 plat->max_hz = SPI_DEFAULT_SPEED_HZ;
388 }
Simon Glassd0cff032015-01-25 08:27:12 -0700389 plat->mode = mode;
Simon Glassd7af6a42014-10-13 23:41:52 -0600390 created = true;
391 } else if (ret) {
Patrick Delaunay46c53912020-10-15 17:18:17 +0200392 dev_err(bus, "Invalid chip select %d:%d (err=%d)\n", busnum, cs, ret);
Simon Glassd7af6a42014-10-13 23:41:52 -0600393 return ret;
Marek Vasute2e95e52021-06-10 14:00:00 +0200394 } else if (dev) {
395 plat = dev_get_parent_plat(dev);
Simon Glassd7af6a42014-10-13 23:41:52 -0600396 }
397
398 if (!device_active(dev)) {
Simon Glassd0cff032015-01-25 08:27:12 -0700399 struct spi_slave *slave;
Simon Glassd7af6a42014-10-13 23:41:52 -0600400
Simon Glassd0cff032015-01-25 08:27:12 -0700401 ret = device_probe(dev);
Simon Glassd7af6a42014-10-13 23:41:52 -0600402 if (ret)
403 goto err;
Simon Glassbcbe3d12015-09-28 23:32:01 -0600404 slave = dev_get_parent_priv(dev);
Simon Glassd7af6a42014-10-13 23:41:52 -0600405 slave->dev = dev;
Simon Glassd7af6a42014-10-13 23:41:52 -0600406 }
407
Marcin Wojtasf7dd5372019-11-21 05:38:47 +0100408 slave = dev_get_parent_priv(dev);
Ovidiu Panait741280e2020-12-14 19:06:50 +0200409 bus_data = dev_get_uclass_priv(bus);
Patrick Delaunayb0cc1b82019-02-27 15:36:44 +0100410
Marcin Wojtasf7dd5372019-11-21 05:38:47 +0100411 /*
412 * In case the operation speed is not yet established by
413 * dm_spi_claim_bus() ensure the bus is configured properly.
414 */
Ovidiu Panait741280e2020-12-14 19:06:50 +0200415 if (!bus_data->speed) {
Marcin Wojtasf7dd5372019-11-21 05:38:47 +0100416 ret = spi_claim_bus(slave);
417 if (ret)
418 goto err;
Vignesh R96907c02016-07-06 10:04:28 +0530419 }
Simon Glassd7af6a42014-10-13 23:41:52 -0600420
Marek Vasute2e95e52021-06-10 14:00:00 +0200421 /* In case bus frequency or mode changed, update it. */
422 if ((speed && bus_data->speed && bus_data->speed != speed) ||
423 (plat && plat->mode != mode)) {
424 ret = spi_set_speed_mode(bus, speed, mode);
425 if (ret)
426 goto err_speed_mode;
427 }
428
Simon Glassd7af6a42014-10-13 23:41:52 -0600429 *busp = bus;
Marcin Wojtasf7dd5372019-11-21 05:38:47 +0100430 *devp = slave;
Patrick Delaunay46c53912020-10-15 17:18:17 +0200431 log_debug("%s: bus=%p, slave=%p\n", __func__, bus, *devp);
Simon Glassd7af6a42014-10-13 23:41:52 -0600432
433 return 0;
434
Marek Vasute2e95e52021-06-10 14:00:00 +0200435err_speed_mode:
436 spi_release_bus(slave);
Simon Glassd7af6a42014-10-13 23:41:52 -0600437err:
Patrick Delaunay46c53912020-10-15 17:18:17 +0200438 log_debug("%s: Error path, created=%d, device '%s'\n", __func__,
439 created, dev->name);
Simon Glassd7af6a42014-10-13 23:41:52 -0600440 if (created) {
Stefan Roese706865a2017-03-20 12:51:48 +0100441 device_remove(dev, DM_REMOVE_NORMAL);
Simon Glassd7af6a42014-10-13 23:41:52 -0600442 device_unbind(dev);
443 }
444
445 return ret;
446}
447
448/* Compatibility function - to be removed */
Simon Glassd7af6a42014-10-13 23:41:52 -0600449struct spi_slave *spi_setup_slave(unsigned int busnum, unsigned int cs,
450 unsigned int speed, unsigned int mode)
451{
452 struct spi_slave *slave;
453 struct udevice *dev;
454 int ret;
455
456 ret = spi_get_bus_and_cs(busnum, cs, speed, mode, NULL, 0, &dev,
Simon Glass279e26f2017-05-18 20:09:54 -0600457 &slave);
Simon Glassd7af6a42014-10-13 23:41:52 -0600458 if (ret)
459 return NULL;
460
461 return slave;
462}
463
464void spi_free_slave(struct spi_slave *slave)
465{
Stefan Roese706865a2017-03-20 12:51:48 +0100466 device_remove(slave->dev, DM_REMOVE_NORMAL);
Simon Glassd7af6a42014-10-13 23:41:52 -0600467}
468
Simon Glass8a8d24b2020-12-03 16:55:23 -0700469int spi_slave_of_to_plat(struct udevice *dev, struct dm_spi_slave_plat *plat)
Simon Glassd7af6a42014-10-13 23:41:52 -0600470{
Jagan Teki08fe9c22016-08-08 17:12:12 +0530471 int mode = 0;
Mugunthan V Nf8e2f922015-12-23 20:39:37 +0530472 int value;
Simon Glassd7af6a42014-10-13 23:41:52 -0600473
Simon Glass279e26f2017-05-18 20:09:54 -0600474 plat->cs = dev_read_u32_default(dev, "reg", -1);
Simon Goldschmidt12bfb2e2018-10-30 21:09:48 +0100475 plat->max_hz = dev_read_u32_default(dev, "spi-max-frequency",
476 SPI_DEFAULT_SPEED_HZ);
Simon Glass279e26f2017-05-18 20:09:54 -0600477 if (dev_read_bool(dev, "spi-cpol"))
Simon Glassd7af6a42014-10-13 23:41:52 -0600478 mode |= SPI_CPOL;
Simon Glass279e26f2017-05-18 20:09:54 -0600479 if (dev_read_bool(dev, "spi-cpha"))
Simon Glassd7af6a42014-10-13 23:41:52 -0600480 mode |= SPI_CPHA;
Simon Glass279e26f2017-05-18 20:09:54 -0600481 if (dev_read_bool(dev, "spi-cs-high"))
Simon Glassd7af6a42014-10-13 23:41:52 -0600482 mode |= SPI_CS_HIGH;
Simon Glass279e26f2017-05-18 20:09:54 -0600483 if (dev_read_bool(dev, "spi-3wire"))
Jagan Teki379b49d2015-12-03 22:19:05 +0530484 mode |= SPI_3WIRE;
Simon Glass279e26f2017-05-18 20:09:54 -0600485 if (dev_read_bool(dev, "spi-half-duplex"))
Simon Glassd7af6a42014-10-13 23:41:52 -0600486 mode |= SPI_PREAMBLE;
Mugunthan V Nf8e2f922015-12-23 20:39:37 +0530487
488 /* Device DUAL/QUAD mode */
Simon Glass279e26f2017-05-18 20:09:54 -0600489 value = dev_read_u32_default(dev, "spi-tx-bus-width", 1);
Mugunthan V Nf8e2f922015-12-23 20:39:37 +0530490 switch (value) {
491 case 1:
492 break;
493 case 2:
494 mode |= SPI_TX_DUAL;
495 break;
496 case 4:
497 mode |= SPI_TX_QUAD;
498 break;
Vignesh Raghavendra658df8b2019-12-05 15:46:05 +0530499 case 8:
500 mode |= SPI_TX_OCTAL;
501 break;
Mugunthan V Nf8e2f922015-12-23 20:39:37 +0530502 default:
Simon Glass1b7c28f2016-11-29 20:00:13 -0700503 warn_non_spl("spi-tx-bus-width %d not supported\n", value);
Mugunthan V Nf8e2f922015-12-23 20:39:37 +0530504 break;
505 }
506
Simon Glass279e26f2017-05-18 20:09:54 -0600507 value = dev_read_u32_default(dev, "spi-rx-bus-width", 1);
Mugunthan V Nf8e2f922015-12-23 20:39:37 +0530508 switch (value) {
509 case 1:
510 break;
511 case 2:
Jagan Teki08fe9c22016-08-08 17:12:12 +0530512 mode |= SPI_RX_DUAL;
Mugunthan V Nf8e2f922015-12-23 20:39:37 +0530513 break;
514 case 4:
Jagan Teki08fe9c22016-08-08 17:12:12 +0530515 mode |= SPI_RX_QUAD;
Mugunthan V Nf8e2f922015-12-23 20:39:37 +0530516 break;
Vignesh Raghavendra658df8b2019-12-05 15:46:05 +0530517 case 8:
518 mode |= SPI_RX_OCTAL;
519 break;
Mugunthan V Nf8e2f922015-12-23 20:39:37 +0530520 default:
Simon Glass1b7c28f2016-11-29 20:00:13 -0700521 warn_non_spl("spi-rx-bus-width %d not supported\n", value);
Mugunthan V Nf8e2f922015-12-23 20:39:37 +0530522 break;
523 }
524
Jagan Teki08fe9c22016-08-08 17:12:12 +0530525 plat->mode = mode;
Mugunthan V Nf8e2f922015-12-23 20:39:37 +0530526
Simon Glassd7af6a42014-10-13 23:41:52 -0600527 return 0;
528}
529
530UCLASS_DRIVER(spi) = {
531 .id = UCLASS_SPI,
532 .name = "spi",
Simon Glass9cc36a22015-01-25 08:27:05 -0700533 .flags = DM_UC_FLAG_SEQ_ALIAS,
Faiz Abbas280af012020-09-14 12:11:14 +0530534#if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
Simon Glass91195482016-07-05 17:10:10 -0600535 .post_bind = dm_scan_fdt_dev,
Simon Glass71634f22016-11-13 14:22:01 -0700536#endif
Simon Glassd7af6a42014-10-13 23:41:52 -0600537 .post_probe = spi_post_probe,
Simon Glass440714e2015-01-25 08:27:11 -0700538 .child_pre_probe = spi_child_pre_probe,
Simon Glass41575d82020-12-03 16:55:17 -0700539 .per_device_auto = sizeof(struct dm_spi_bus),
540 .per_child_auto = sizeof(struct spi_slave),
Simon Glass8a8d24b2020-12-03 16:55:23 -0700541 .per_child_plat_auto = sizeof(struct dm_spi_slave_plat),
Simon Glass71634f22016-11-13 14:22:01 -0700542#if !CONFIG_IS_ENABLED(OF_PLATDATA)
Simon Glassd0cff032015-01-25 08:27:12 -0700543 .child_post_bind = spi_child_post_bind,
Simon Glass71634f22016-11-13 14:22:01 -0700544#endif
Simon Glassd7af6a42014-10-13 23:41:52 -0600545};
546
547UCLASS_DRIVER(spi_generic) = {
548 .id = UCLASS_SPI_GENERIC,
549 .name = "spi_generic",
550};
551
552U_BOOT_DRIVER(spi_generic_drv) = {
553 .name = "spi_generic_drv",
554 .id = UCLASS_SPI_GENERIC,
555};