blob: 7155d4aebd6d20971963d82e7fb3e753434ebc86 [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>
Patrick Delaunay46c53912020-10-15 17:18:17 +020014#include <dm/device_compat.h>
Simon Glass401d1c42020-10-30 21:38:53 -060015#include <asm/global_data.h>
Simon Glassd7af6a42014-10-13 23:41:52 -060016#include <dm/device-internal.h>
17#include <dm/uclass-internal.h>
Simon Glassd7af6a42014-10-13 23:41:52 -060018#include <dm/lists.h>
19#include <dm/util.h>
20
21DECLARE_GLOBAL_DATA_PTR;
22
Simon Goldschmidt12bfb2e2018-10-30 21:09:48 +010023#define SPI_DEFAULT_SPEED_HZ 100000
24
Simon Glassd7af6a42014-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 Delaunay46c53912020-10-15 17:18:17 +020036 dev_err(bus, "Cannot set speed (err=%d)\n", ret);
Simon Glassd7af6a42014-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 Delaunay46c53912020-10-15 17:18:17 +020045 dev_err(bus, "Cannot set mode (err=%d)\n", ret);
Simon Glassd7af6a42014-10-13 23:41:52 -060046 return ret;
47 }
48
49 return 0;
50}
51
Peng Fan7a3eff42016-05-03 10:02:22 +080052int dm_spi_claim_bus(struct udevice *dev)
Simon Glassd7af6a42014-10-13 23:41:52 -060053{
Simon Glassd7af6a42014-10-13 23:41:52 -060054 struct udevice *bus = dev->parent;
55 struct dm_spi_ops *ops = spi_get_ops(bus);
Simon Glasse564f052015-03-05 12:25:20 -070056 struct dm_spi_bus *spi = dev_get_uclass_priv(bus);
Peng Fan7a3eff42016-05-03 10:02:22 +080057 struct spi_slave *slave = dev_get_parent_priv(dev);
Ovidiu Panait741280e2020-12-14 19:06:50 +020058 uint speed, mode;
Simon Glassd7af6a42014-10-13 23:41:52 -060059
60 speed = slave->max_hz;
Ovidiu Panait741280e2020-12-14 19:06:50 +020061 mode = slave->mode;
62
Simon Glassd7af6a42014-10-13 23:41:52 -060063 if (spi->max_hz) {
64 if (speed)
Ovidiu Panait741280e2020-12-14 19:06:50 +020065 speed = min(speed, spi->max_hz);
Simon Glassd7af6a42014-10-13 23:41:52 -060066 else
67 speed = spi->max_hz;
68 }
69 if (!speed)
Simon Goldschmidt12bfb2e2018-10-30 21:09:48 +010070 speed = SPI_DEFAULT_SPEED_HZ;
Ovidiu Panait741280e2020-12-14 19:06:50 +020071
72 if (speed != spi->speed || mode != spi->mode) {
Mario Six24fc1ec2018-01-15 11:08:41 +010073 int ret = spi_set_speed_mode(bus, speed, slave->mode);
74
Simon Glass60e28092015-02-17 15:29:35 -070075 if (ret)
Simon Glass5e24a2e2018-10-01 12:22:24 -060076 return log_ret(ret);
Ovidiu Panait741280e2020-12-14 19:06:50 +020077
78 spi->speed = speed;
79 spi->mode = mode;
Simon Glass60e28092015-02-17 15:29:35 -070080 }
Simon Glassd7af6a42014-10-13 23:41:52 -060081
Simon Glass5e24a2e2018-10-01 12:22:24 -060082 return log_ret(ops->claim_bus ? ops->claim_bus(dev) : 0);
Simon Glassd7af6a42014-10-13 23:41:52 -060083}
84
Peng Fan7a3eff42016-05-03 10:02:22 +080085void dm_spi_release_bus(struct udevice *dev)
Simon Glassd7af6a42014-10-13 23:41:52 -060086{
Simon Glassd7af6a42014-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 Glass9694b722015-04-19 09:05:40 -060091 ops->release_bus(dev);
Simon Glassd7af6a42014-10-13 23:41:52 -060092}
93
Peng Fan7a3eff42016-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 Glassd7af6a42014-10-13 23:41:52 -060096{
Simon Glassd7af6a42014-10-13 23:41:52 -060097 struct udevice *bus = dev->parent;
Simon Glassccdabd82019-12-06 21:42:35 -070098 struct dm_spi_ops *ops = spi_get_ops(bus);
Simon Glassd7af6a42014-10-13 23:41:52 -060099
100 if (bus->uclass->uc_drv->id != UCLASS_SPI)
101 return -EOPNOTSUPP;
Simon Glassccdabd82019-12-06 21:42:35 -0700102 if (!ops->xfer)
103 return -ENOSYS;
Simon Glassd7af6a42014-10-13 23:41:52 -0600104
Simon Glassccdabd82019-12-06 21:42:35 -0700105 return ops->xfer(dev, bitlen, dout, din, flags);
Simon Glassd7af6a42014-10-13 23:41:52 -0600106}
107
Simon Glassc53b3182019-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 Fan7a3eff42016-05-03 10:02:22 +0800122int spi_claim_bus(struct spi_slave *slave)
123{
Simon Glass5e24a2e2018-10-01 12:22:24 -0600124 return log_ret(dm_spi_claim_bus(slave->dev));
Peng Fan7a3eff42016-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
132int spi_xfer(struct spi_slave *slave, unsigned int bitlen,
133 const void *dout, void *din, unsigned long flags)
134{
135 return dm_spi_xfer(slave->dev, bitlen, dout, din, flags);
136}
137
Jagan Teki8473b322019-07-22 17:22:56 +0530138int spi_write_then_read(struct spi_slave *slave, const u8 *opcode,
139 size_t n_opcode, const u8 *txbuf, u8 *rxbuf,
140 size_t n_buf)
141{
142 unsigned long flags = SPI_XFER_BEGIN;
143 int ret;
144
145 if (n_buf == 0)
146 flags |= SPI_XFER_END;
147
148 ret = spi_xfer(slave, n_opcode * 8, opcode, NULL, flags);
149 if (ret) {
Patrick Delaunay46c53912020-10-15 17:18:17 +0200150 dev_dbg(slave->dev,
151 "spi: failed to send command (%zu bytes): %d\n",
152 n_opcode, ret);
Jagan Teki8473b322019-07-22 17:22:56 +0530153 } else if (n_buf != 0) {
154 ret = spi_xfer(slave, n_buf * 8, txbuf, rxbuf, SPI_XFER_END);
155 if (ret)
Patrick Delaunay46c53912020-10-15 17:18:17 +0200156 dev_dbg(slave->dev,
157 "spi: failed to transfer %zu bytes of data: %d\n",
158 n_buf, ret);
Jagan Teki8473b322019-07-22 17:22:56 +0530159 }
160
161 return ret;
162}
163
Simon Glass71634f22016-11-13 14:22:01 -0700164#if !CONFIG_IS_ENABLED(OF_PLATDATA)
Simon Glass6f849c32015-06-23 15:39:05 -0600165static int spi_child_post_bind(struct udevice *dev)
Simon Glassd7af6a42014-10-13 23:41:52 -0600166{
Simon Glass8a8d24b2020-12-03 16:55:23 -0700167 struct dm_spi_slave_plat *plat = dev_get_parent_plat(dev);
Simon Glassd7af6a42014-10-13 23:41:52 -0600168
Simon Glass7d14ee42020-12-19 10:40:13 -0700169 if (!dev_has_ofnode(dev))
Simon Glassd0cff032015-01-25 08:27:12 -0700170 return 0;
171
Simon Glassd1998a92020-12-03 16:55:21 -0700172 return spi_slave_of_to_plat(dev, plat);
Simon Glassd0cff032015-01-25 08:27:12 -0700173}
Simon Glass71634f22016-11-13 14:22:01 -0700174#endif
Simon Glassd0cff032015-01-25 08:27:12 -0700175
Simon Glass6f849c32015-06-23 15:39:05 -0600176static int spi_post_probe(struct udevice *bus)
Simon Glassd0cff032015-01-25 08:27:12 -0700177{
Simon Glass71634f22016-11-13 14:22:01 -0700178#if !CONFIG_IS_ENABLED(OF_PLATDATA)
Simon Glasse564f052015-03-05 12:25:20 -0700179 struct dm_spi_bus *spi = dev_get_uclass_priv(bus);
Simon Glassd0cff032015-01-25 08:27:12 -0700180
Simon Glass279e26f2017-05-18 20:09:54 -0600181 spi->max_hz = dev_read_u32_default(bus, "spi-max-frequency", 0);
Simon Glass71634f22016-11-13 14:22:01 -0700182#endif
Michal Simek281f1562015-10-27 13:36:42 +0100183#if defined(CONFIG_NEEDS_MANUAL_RELOC)
184 struct dm_spi_ops *ops = spi_get_ops(bus);
Ashok Reddy Soma4d9b1af2019-09-17 00:11:02 -0600185 static int reloc_done;
Michal Simek281f1562015-10-27 13:36:42 +0100186
Ashok Reddy Soma4d9b1af2019-09-17 00:11:02 -0600187 if (!reloc_done) {
188 if (ops->claim_bus)
189 ops->claim_bus += gd->reloc_off;
190 if (ops->release_bus)
191 ops->release_bus += gd->reloc_off;
192 if (ops->set_wordlen)
193 ops->set_wordlen += gd->reloc_off;
194 if (ops->xfer)
195 ops->xfer += gd->reloc_off;
196 if (ops->set_speed)
197 ops->set_speed += gd->reloc_off;
198 if (ops->set_mode)
199 ops->set_mode += gd->reloc_off;
200 if (ops->cs_info)
201 ops->cs_info += gd->reloc_off;
202 reloc_done++;
203 }
Michal Simek281f1562015-10-27 13:36:42 +0100204#endif
205
Simon Glassd7af6a42014-10-13 23:41:52 -0600206 return 0;
207}
208
Simon Glass6f849c32015-06-23 15:39:05 -0600209static int spi_child_pre_probe(struct udevice *dev)
Simon Glass440714e2015-01-25 08:27:11 -0700210{
Simon Glass8a8d24b2020-12-03 16:55:23 -0700211 struct dm_spi_slave_plat *plat = dev_get_parent_plat(dev);
Simon Glassbcbe3d12015-09-28 23:32:01 -0600212 struct spi_slave *slave = dev_get_parent_priv(dev);
Simon Glass440714e2015-01-25 08:27:11 -0700213
Simon Glassd0cff032015-01-25 08:27:12 -0700214 /*
215 * This is needed because we pass struct spi_slave around the place
216 * instead slave->dev (a struct udevice). So we have to have some
217 * way to access the slave udevice given struct spi_slave. Once we
218 * change the SPI API to use udevice instead of spi_slave, we can
219 * drop this.
220 */
Simon Glass440714e2015-01-25 08:27:11 -0700221 slave->dev = dev;
222
Simon Glassd0cff032015-01-25 08:27:12 -0700223 slave->max_hz = plat->max_hz;
224 slave->mode = plat->mode;
Christophe Ricard674f3602016-01-17 11:56:48 +0100225 slave->wordlen = SPI_DEFAULT_WORDLEN;
Simon Glassd0cff032015-01-25 08:27:12 -0700226
Simon Glass440714e2015-01-25 08:27:11 -0700227 return 0;
228}
229
Simon Glassd7af6a42014-10-13 23:41:52 -0600230int spi_chip_select(struct udevice *dev)
231{
Simon Glass8a8d24b2020-12-03 16:55:23 -0700232 struct dm_spi_slave_plat *plat = dev_get_parent_plat(dev);
Simon Glassd7af6a42014-10-13 23:41:52 -0600233
Simon Glassd0cff032015-01-25 08:27:12 -0700234 return plat ? plat->cs : -ENOENT;
Simon Glassd7af6a42014-10-13 23:41:52 -0600235}
236
Simon Glassff56bba2014-11-11 10:46:22 -0700237int spi_find_chip_select(struct udevice *bus, int cs, struct udevice **devp)
Simon Glassd7af6a42014-10-13 23:41:52 -0600238{
Bin Meng7bacce52019-09-09 06:00:02 -0700239 struct dm_spi_ops *ops;
240 struct spi_cs_info info;
Simon Glassd7af6a42014-10-13 23:41:52 -0600241 struct udevice *dev;
Bin Meng7bacce52019-09-09 06:00:02 -0700242 int ret;
243
244 /*
245 * Ask the driver. For the moment we don't have CS info.
246 * When we do we could provide the driver with a helper function
247 * to figure out what chip selects are valid, or just handle the
248 * request.
249 */
250 ops = spi_get_ops(bus);
251 if (ops->cs_info) {
252 ret = ops->cs_info(bus, cs, &info);
253 } else {
254 /*
255 * We could assume there is at least one valid chip select.
256 * The driver didn't care enough to tell us.
257 */
258 ret = 0;
259 }
260
261 if (ret) {
Patrick Delaunay46c53912020-10-15 17:18:17 +0200262 dev_err(bus, "Invalid cs %d (err=%d)\n", cs, ret);
Bin Meng7bacce52019-09-09 06:00:02 -0700263 return ret;
264 }
Simon Glassd7af6a42014-10-13 23:41:52 -0600265
266 for (device_find_first_child(bus, &dev); dev;
267 device_find_next_child(&dev)) {
Simon Glass8a8d24b2020-12-03 16:55:23 -0700268 struct dm_spi_slave_plat *plat;
Simon Glassd7af6a42014-10-13 23:41:52 -0600269
Simon Glasscaa4daa2020-12-03 16:55:18 -0700270 plat = dev_get_parent_plat(dev);
Patrick Delaunay46c53912020-10-15 17:18:17 +0200271 dev_dbg(bus, "%s: plat=%p, cs=%d\n", __func__, plat, plat->cs);
Simon Glassd0cff032015-01-25 08:27:12 -0700272 if (plat->cs == cs) {
Simon Glassd7af6a42014-10-13 23:41:52 -0600273 *devp = dev;
274 return 0;
275 }
276 }
277
278 return -ENODEV;
279}
280
281int spi_cs_is_valid(unsigned int busnum, unsigned int cs)
282{
283 struct spi_cs_info info;
284 struct udevice *bus;
285 int ret;
286
Simon Glass99175912020-12-16 21:20:29 -0700287 ret = uclass_find_device_by_seq(UCLASS_SPI, busnum, &bus);
Simon Glassd7af6a42014-10-13 23:41:52 -0600288 if (ret) {
Patrick Delaunay46c53912020-10-15 17:18:17 +0200289 log_debug("%s: No bus %d\n", __func__, busnum);
Simon Glassd7af6a42014-10-13 23:41:52 -0600290 return ret;
291 }
292
293 return spi_cs_info(bus, cs, &info);
294}
295
296int spi_cs_info(struct udevice *bus, uint cs, struct spi_cs_info *info)
297{
298 struct spi_cs_info local_info;
Simon Glassd7af6a42014-10-13 23:41:52 -0600299 int ret;
300
301 if (!info)
302 info = &local_info;
303
304 /* If there is a device attached, return it */
305 info->dev = NULL;
306 ret = spi_find_chip_select(bus, cs, &info->dev);
Bin Meng7bacce52019-09-09 06:00:02 -0700307 return ret == -ENODEV ? 0 : ret;
Simon Glassd7af6a42014-10-13 23:41:52 -0600308}
309
Simon Glassd7af6a42014-10-13 23:41:52 -0600310int spi_find_bus_and_cs(int busnum, int cs, struct udevice **busp,
311 struct udevice **devp)
312{
313 struct udevice *bus, *dev;
314 int ret;
315
Simon Glass99175912020-12-16 21:20:29 -0700316 ret = uclass_find_device_by_seq(UCLASS_SPI, busnum, &bus);
Simon Glassd7af6a42014-10-13 23:41:52 -0600317 if (ret) {
Patrick Delaunay46c53912020-10-15 17:18:17 +0200318 log_debug("%s: No bus %d\n", __func__, busnum);
Simon Glassd7af6a42014-10-13 23:41:52 -0600319 return ret;
320 }
321 ret = spi_find_chip_select(bus, cs, &dev);
322 if (ret) {
Patrick Delaunay46c53912020-10-15 17:18:17 +0200323 dev_dbg(bus, "%s: No cs %d\n", __func__, cs);
Simon Glassd7af6a42014-10-13 23:41:52 -0600324 return ret;
325 }
326 *busp = bus;
327 *devp = dev;
328
329 return ret;
330}
331
332int spi_get_bus_and_cs(int busnum, int cs, int speed, int mode,
333 const char *drv_name, const char *dev_name,
334 struct udevice **busp, struct spi_slave **devp)
335{
336 struct udevice *bus, *dev;
Simon Glass8a8d24b2020-12-03 16:55:23 -0700337 struct dm_spi_slave_plat *plat;
Ovidiu Panait741280e2020-12-14 19:06:50 +0200338 struct dm_spi_bus *bus_data;
Marcin Wojtasf7dd5372019-11-21 05:38:47 +0100339 struct spi_slave *slave;
Simon Glassd7af6a42014-10-13 23:41:52 -0600340 bool created = false;
341 int ret;
342
Thomas Fitzsimmons640abba2019-09-06 07:51:19 -0400343#if CONFIG_IS_ENABLED(OF_PLATDATA)
Simon Glass71634f22016-11-13 14:22:01 -0700344 ret = uclass_first_device_err(UCLASS_SPI, &bus);
345#else
Simon Glassd7af6a42014-10-13 23:41:52 -0600346 ret = uclass_get_device_by_seq(UCLASS_SPI, busnum, &bus);
Simon Glass71634f22016-11-13 14:22:01 -0700347#endif
Simon Glassd7af6a42014-10-13 23:41:52 -0600348 if (ret) {
Patrick Delaunay46c53912020-10-15 17:18:17 +0200349 log_err("Invalid bus %d (err=%d)\n", busnum, ret);
Simon Glassd7af6a42014-10-13 23:41:52 -0600350 return ret;
351 }
352 ret = spi_find_chip_select(bus, cs, &dev);
353
354 /*
355 * If there is no such device, create one automatically. This means
356 * that we don't need a device tree node or platform data for the
357 * SPI flash chip - we will bind to the correct driver.
358 */
359 if (ret == -ENODEV && drv_name) {
Patrick Delaunay46c53912020-10-15 17:18:17 +0200360 dev_dbg(bus, "%s: Binding new device '%s', busnum=%d, cs=%d, driver=%s\n",
361 __func__, dev_name, busnum, cs, drv_name);
Simon Glass6b186562014-11-11 10:46:23 -0700362 ret = device_bind_driver(bus, drv_name, dev_name, &dev);
Simon Glass28f98852016-11-13 14:22:05 -0700363 if (ret) {
Patrick Delaunay46c53912020-10-15 17:18:17 +0200364 dev_dbg(bus, "%s: Unable to bind driver (ret=%d)\n",
365 __func__, ret);
Simon Glassd7af6a42014-10-13 23:41:52 -0600366 return ret;
Simon Glass28f98852016-11-13 14:22:05 -0700367 }
Simon Glasscaa4daa2020-12-03 16:55:18 -0700368 plat = dev_get_parent_plat(dev);
Simon Glassd0cff032015-01-25 08:27:12 -0700369 plat->cs = cs;
Simon Goldschmidt12bfb2e2018-10-30 21:09:48 +0100370 if (speed) {
371 plat->max_hz = speed;
372 } else {
Patrick Delaunay46c53912020-10-15 17:18:17 +0200373 dev_warn(bus,
374 "Warning: SPI speed fallback to %u kHz\n",
375 SPI_DEFAULT_SPEED_HZ / 1000);
Simon Goldschmidt12bfb2e2018-10-30 21:09:48 +0100376 plat->max_hz = SPI_DEFAULT_SPEED_HZ;
377 }
Simon Glassd0cff032015-01-25 08:27:12 -0700378 plat->mode = mode;
Simon Glassd7af6a42014-10-13 23:41:52 -0600379 created = true;
380 } else if (ret) {
Patrick Delaunay46c53912020-10-15 17:18:17 +0200381 dev_err(bus, "Invalid chip select %d:%d (err=%d)\n", busnum, cs, ret);
Simon Glassd7af6a42014-10-13 23:41:52 -0600382 return ret;
383 }
384
385 if (!device_active(dev)) {
Simon Glassd0cff032015-01-25 08:27:12 -0700386 struct spi_slave *slave;
Simon Glassd7af6a42014-10-13 23:41:52 -0600387
Simon Glassd0cff032015-01-25 08:27:12 -0700388 ret = device_probe(dev);
Simon Glassd7af6a42014-10-13 23:41:52 -0600389 if (ret)
390 goto err;
Simon Glassbcbe3d12015-09-28 23:32:01 -0600391 slave = dev_get_parent_priv(dev);
Simon Glassd7af6a42014-10-13 23:41:52 -0600392 slave->dev = dev;
Simon Glassd7af6a42014-10-13 23:41:52 -0600393 }
394
Marcin Wojtasf7dd5372019-11-21 05:38:47 +0100395 slave = dev_get_parent_priv(dev);
Ovidiu Panait741280e2020-12-14 19:06:50 +0200396 bus_data = dev_get_uclass_priv(bus);
Patrick Delaunayb0cc1b82019-02-27 15:36:44 +0100397
Marcin Wojtasf7dd5372019-11-21 05:38:47 +0100398 /*
399 * In case the operation speed is not yet established by
400 * dm_spi_claim_bus() ensure the bus is configured properly.
401 */
Ovidiu Panait741280e2020-12-14 19:06:50 +0200402 if (!bus_data->speed) {
Marcin Wojtasf7dd5372019-11-21 05:38:47 +0100403 ret = spi_claim_bus(slave);
404 if (ret)
405 goto err;
Vignesh R96907c02016-07-06 10:04:28 +0530406 }
Simon Glassd7af6a42014-10-13 23:41:52 -0600407
408 *busp = bus;
Marcin Wojtasf7dd5372019-11-21 05:38:47 +0100409 *devp = slave;
Patrick Delaunay46c53912020-10-15 17:18:17 +0200410 log_debug("%s: bus=%p, slave=%p\n", __func__, bus, *devp);
Simon Glassd7af6a42014-10-13 23:41:52 -0600411
412 return 0;
413
414err:
Patrick Delaunay46c53912020-10-15 17:18:17 +0200415 log_debug("%s: Error path, created=%d, device '%s'\n", __func__,
416 created, dev->name);
Simon Glassd7af6a42014-10-13 23:41:52 -0600417 if (created) {
Stefan Roese706865a2017-03-20 12:51:48 +0100418 device_remove(dev, DM_REMOVE_NORMAL);
Simon Glassd7af6a42014-10-13 23:41:52 -0600419 device_unbind(dev);
420 }
421
422 return ret;
423}
424
425/* Compatibility function - to be removed */
Simon Glassd7af6a42014-10-13 23:41:52 -0600426struct spi_slave *spi_setup_slave(unsigned int busnum, unsigned int cs,
427 unsigned int speed, unsigned int mode)
428{
429 struct spi_slave *slave;
430 struct udevice *dev;
431 int ret;
432
433 ret = spi_get_bus_and_cs(busnum, cs, speed, mode, NULL, 0, &dev,
Simon Glass279e26f2017-05-18 20:09:54 -0600434 &slave);
Simon Glassd7af6a42014-10-13 23:41:52 -0600435 if (ret)
436 return NULL;
437
438 return slave;
439}
440
441void spi_free_slave(struct spi_slave *slave)
442{
Stefan Roese706865a2017-03-20 12:51:48 +0100443 device_remove(slave->dev, DM_REMOVE_NORMAL);
Simon Glassd7af6a42014-10-13 23:41:52 -0600444}
445
Simon Glass8a8d24b2020-12-03 16:55:23 -0700446int spi_slave_of_to_plat(struct udevice *dev, struct dm_spi_slave_plat *plat)
Simon Glassd7af6a42014-10-13 23:41:52 -0600447{
Jagan Teki08fe9c22016-08-08 17:12:12 +0530448 int mode = 0;
Mugunthan V Nf8e2f922015-12-23 20:39:37 +0530449 int value;
Simon Glassd7af6a42014-10-13 23:41:52 -0600450
Simon Glass279e26f2017-05-18 20:09:54 -0600451 plat->cs = dev_read_u32_default(dev, "reg", -1);
Simon Goldschmidt12bfb2e2018-10-30 21:09:48 +0100452 plat->max_hz = dev_read_u32_default(dev, "spi-max-frequency",
453 SPI_DEFAULT_SPEED_HZ);
Simon Glass279e26f2017-05-18 20:09:54 -0600454 if (dev_read_bool(dev, "spi-cpol"))
Simon Glassd7af6a42014-10-13 23:41:52 -0600455 mode |= SPI_CPOL;
Simon Glass279e26f2017-05-18 20:09:54 -0600456 if (dev_read_bool(dev, "spi-cpha"))
Simon Glassd7af6a42014-10-13 23:41:52 -0600457 mode |= SPI_CPHA;
Simon Glass279e26f2017-05-18 20:09:54 -0600458 if (dev_read_bool(dev, "spi-cs-high"))
Simon Glassd7af6a42014-10-13 23:41:52 -0600459 mode |= SPI_CS_HIGH;
Simon Glass279e26f2017-05-18 20:09:54 -0600460 if (dev_read_bool(dev, "spi-3wire"))
Jagan Teki379b49d2015-12-03 22:19:05 +0530461 mode |= SPI_3WIRE;
Simon Glass279e26f2017-05-18 20:09:54 -0600462 if (dev_read_bool(dev, "spi-half-duplex"))
Simon Glassd7af6a42014-10-13 23:41:52 -0600463 mode |= SPI_PREAMBLE;
Mugunthan V Nf8e2f922015-12-23 20:39:37 +0530464
465 /* Device DUAL/QUAD mode */
Simon Glass279e26f2017-05-18 20:09:54 -0600466 value = dev_read_u32_default(dev, "spi-tx-bus-width", 1);
Mugunthan V Nf8e2f922015-12-23 20:39:37 +0530467 switch (value) {
468 case 1:
469 break;
470 case 2:
471 mode |= SPI_TX_DUAL;
472 break;
473 case 4:
474 mode |= SPI_TX_QUAD;
475 break;
Vignesh Raghavendra658df8b2019-12-05 15:46:05 +0530476 case 8:
477 mode |= SPI_TX_OCTAL;
478 break;
Mugunthan V Nf8e2f922015-12-23 20:39:37 +0530479 default:
Simon Glass1b7c28f2016-11-29 20:00:13 -0700480 warn_non_spl("spi-tx-bus-width %d not supported\n", value);
Mugunthan V Nf8e2f922015-12-23 20:39:37 +0530481 break;
482 }
483
Simon Glass279e26f2017-05-18 20:09:54 -0600484 value = dev_read_u32_default(dev, "spi-rx-bus-width", 1);
Mugunthan V Nf8e2f922015-12-23 20:39:37 +0530485 switch (value) {
486 case 1:
487 break;
488 case 2:
Jagan Teki08fe9c22016-08-08 17:12:12 +0530489 mode |= SPI_RX_DUAL;
Mugunthan V Nf8e2f922015-12-23 20:39:37 +0530490 break;
491 case 4:
Jagan Teki08fe9c22016-08-08 17:12:12 +0530492 mode |= SPI_RX_QUAD;
Mugunthan V Nf8e2f922015-12-23 20:39:37 +0530493 break;
Vignesh Raghavendra658df8b2019-12-05 15:46:05 +0530494 case 8:
495 mode |= SPI_RX_OCTAL;
496 break;
Mugunthan V Nf8e2f922015-12-23 20:39:37 +0530497 default:
Simon Glass1b7c28f2016-11-29 20:00:13 -0700498 warn_non_spl("spi-rx-bus-width %d not supported\n", value);
Mugunthan V Nf8e2f922015-12-23 20:39:37 +0530499 break;
500 }
501
Jagan Teki08fe9c22016-08-08 17:12:12 +0530502 plat->mode = mode;
Mugunthan V Nf8e2f922015-12-23 20:39:37 +0530503
Simon Glassd7af6a42014-10-13 23:41:52 -0600504 return 0;
505}
506
507UCLASS_DRIVER(spi) = {
508 .id = UCLASS_SPI,
509 .name = "spi",
Simon Glass9cc36a22015-01-25 08:27:05 -0700510 .flags = DM_UC_FLAG_SEQ_ALIAS,
Faiz Abbas280af012020-09-14 12:11:14 +0530511#if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
Simon Glass91195482016-07-05 17:10:10 -0600512 .post_bind = dm_scan_fdt_dev,
Simon Glass71634f22016-11-13 14:22:01 -0700513#endif
Simon Glassd7af6a42014-10-13 23:41:52 -0600514 .post_probe = spi_post_probe,
Simon Glass440714e2015-01-25 08:27:11 -0700515 .child_pre_probe = spi_child_pre_probe,
Simon Glass41575d82020-12-03 16:55:17 -0700516 .per_device_auto = sizeof(struct dm_spi_bus),
517 .per_child_auto = sizeof(struct spi_slave),
Simon Glass8a8d24b2020-12-03 16:55:23 -0700518 .per_child_plat_auto = sizeof(struct dm_spi_slave_plat),
Simon Glass71634f22016-11-13 14:22:01 -0700519#if !CONFIG_IS_ENABLED(OF_PLATDATA)
Simon Glassd0cff032015-01-25 08:27:12 -0700520 .child_post_bind = spi_child_post_bind,
Simon Glass71634f22016-11-13 14:22:01 -0700521#endif
Simon Glassd7af6a42014-10-13 23:41:52 -0600522};
523
524UCLASS_DRIVER(spi_generic) = {
525 .id = UCLASS_SPI_GENERIC,
526 .name = "spi_generic",
527};
528
529U_BOOT_DRIVER(spi_generic_drv) = {
530 .name = "spi_generic_drv",
531 .id = UCLASS_SPI_GENERIC,
532};