blob: d5a1e3a6762e4bf3b5eb5ba9db8ffec5c5969be3 [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 Glassd7af6a42014-10-13 23:41:52 -060015#include <dm/device-internal.h>
16#include <dm/uclass-internal.h>
Simon Glassd7af6a42014-10-13 23:41:52 -060017#include <dm/lists.h>
18#include <dm/util.h>
19
20DECLARE_GLOBAL_DATA_PTR;
21
Simon Goldschmidt12bfb2e2018-10-30 21:09:48 +010022#define SPI_DEFAULT_SPEED_HZ 100000
23
Simon Glassd7af6a42014-10-13 23:41:52 -060024static int spi_set_speed_mode(struct udevice *bus, int speed, int mode)
25{
26 struct dm_spi_ops *ops;
27 int ret;
28
29 ops = spi_get_ops(bus);
30 if (ops->set_speed)
31 ret = ops->set_speed(bus, speed);
32 else
33 ret = -EINVAL;
34 if (ret) {
Patrick Delaunay46c53912020-10-15 17:18:17 +020035 dev_err(bus, "Cannot set speed (err=%d)\n", ret);
Simon Glassd7af6a42014-10-13 23:41:52 -060036 return ret;
37 }
38
39 if (ops->set_mode)
40 ret = ops->set_mode(bus, mode);
41 else
42 ret = -EINVAL;
43 if (ret) {
Patrick Delaunay46c53912020-10-15 17:18:17 +020044 dev_err(bus, "Cannot set mode (err=%d)\n", ret);
Simon Glassd7af6a42014-10-13 23:41:52 -060045 return ret;
46 }
47
48 return 0;
49}
50
Peng Fan7a3eff42016-05-03 10:02:22 +080051int dm_spi_claim_bus(struct udevice *dev)
Simon Glassd7af6a42014-10-13 23:41:52 -060052{
Simon Glassd7af6a42014-10-13 23:41:52 -060053 struct udevice *bus = dev->parent;
54 struct dm_spi_ops *ops = spi_get_ops(bus);
Simon Glasse564f052015-03-05 12:25:20 -070055 struct dm_spi_bus *spi = dev_get_uclass_priv(bus);
Peng Fan7a3eff42016-05-03 10:02:22 +080056 struct spi_slave *slave = dev_get_parent_priv(dev);
Simon Glassd7af6a42014-10-13 23:41:52 -060057 int speed;
Simon Glassd7af6a42014-10-13 23:41:52 -060058
59 speed = slave->max_hz;
60 if (spi->max_hz) {
61 if (speed)
Masahiro Yamadab4141192014-11-07 03:03:31 +090062 speed = min(speed, (int)spi->max_hz);
Simon Glassd7af6a42014-10-13 23:41:52 -060063 else
64 speed = spi->max_hz;
65 }
66 if (!speed)
Simon Goldschmidt12bfb2e2018-10-30 21:09:48 +010067 speed = SPI_DEFAULT_SPEED_HZ;
Simon Glass60e28092015-02-17 15:29:35 -070068 if (speed != slave->speed) {
Mario Six24fc1ec2018-01-15 11:08:41 +010069 int ret = spi_set_speed_mode(bus, speed, slave->mode);
70
Simon Glass60e28092015-02-17 15:29:35 -070071 if (ret)
Simon Glass5e24a2e2018-10-01 12:22:24 -060072 return log_ret(ret);
Simon Glass60e28092015-02-17 15:29:35 -070073 slave->speed = speed;
74 }
Simon Glassd7af6a42014-10-13 23:41:52 -060075
Simon Glass5e24a2e2018-10-01 12:22:24 -060076 return log_ret(ops->claim_bus ? ops->claim_bus(dev) : 0);
Simon Glassd7af6a42014-10-13 23:41:52 -060077}
78
Peng Fan7a3eff42016-05-03 10:02:22 +080079void dm_spi_release_bus(struct udevice *dev)
Simon Glassd7af6a42014-10-13 23:41:52 -060080{
Simon Glassd7af6a42014-10-13 23:41:52 -060081 struct udevice *bus = dev->parent;
82 struct dm_spi_ops *ops = spi_get_ops(bus);
83
84 if (ops->release_bus)
Simon Glass9694b722015-04-19 09:05:40 -060085 ops->release_bus(dev);
Simon Glassd7af6a42014-10-13 23:41:52 -060086}
87
Peng Fan7a3eff42016-05-03 10:02:22 +080088int dm_spi_xfer(struct udevice *dev, unsigned int bitlen,
89 const void *dout, void *din, unsigned long flags)
Simon Glassd7af6a42014-10-13 23:41:52 -060090{
Simon Glassd7af6a42014-10-13 23:41:52 -060091 struct udevice *bus = dev->parent;
Simon Glassccdabd82019-12-06 21:42:35 -070092 struct dm_spi_ops *ops = spi_get_ops(bus);
Simon Glassd7af6a42014-10-13 23:41:52 -060093
94 if (bus->uclass->uc_drv->id != UCLASS_SPI)
95 return -EOPNOTSUPP;
Simon Glassccdabd82019-12-06 21:42:35 -070096 if (!ops->xfer)
97 return -ENOSYS;
Simon Glassd7af6a42014-10-13 23:41:52 -060098
Simon Glassccdabd82019-12-06 21:42:35 -070099 return ops->xfer(dev, bitlen, dout, din, flags);
Simon Glassd7af6a42014-10-13 23:41:52 -0600100}
101
Simon Glassc53b3182019-10-20 21:31:47 -0600102int dm_spi_get_mmap(struct udevice *dev, ulong *map_basep, uint *map_sizep,
103 uint *offsetp)
104{
105 struct udevice *bus = dev->parent;
106 struct dm_spi_ops *ops = spi_get_ops(bus);
107
108 if (bus->uclass->uc_drv->id != UCLASS_SPI)
109 return -EOPNOTSUPP;
110 if (!ops->get_mmap)
111 return -ENOSYS;
112
113 return ops->get_mmap(dev, map_basep, map_sizep, offsetp);
114}
115
Peng Fan7a3eff42016-05-03 10:02:22 +0800116int spi_claim_bus(struct spi_slave *slave)
117{
Simon Glass5e24a2e2018-10-01 12:22:24 -0600118 return log_ret(dm_spi_claim_bus(slave->dev));
Peng Fan7a3eff42016-05-03 10:02:22 +0800119}
120
121void spi_release_bus(struct spi_slave *slave)
122{
123 dm_spi_release_bus(slave->dev);
124}
125
126int spi_xfer(struct spi_slave *slave, unsigned int bitlen,
127 const void *dout, void *din, unsigned long flags)
128{
129 return dm_spi_xfer(slave->dev, bitlen, dout, din, flags);
130}
131
Jagan Teki8473b322019-07-22 17:22:56 +0530132int spi_write_then_read(struct spi_slave *slave, const u8 *opcode,
133 size_t n_opcode, const u8 *txbuf, u8 *rxbuf,
134 size_t n_buf)
135{
136 unsigned long flags = SPI_XFER_BEGIN;
137 int ret;
138
139 if (n_buf == 0)
140 flags |= SPI_XFER_END;
141
142 ret = spi_xfer(slave, n_opcode * 8, opcode, NULL, flags);
143 if (ret) {
Patrick Delaunay46c53912020-10-15 17:18:17 +0200144 dev_dbg(slave->dev,
145 "spi: failed to send command (%zu bytes): %d\n",
146 n_opcode, ret);
Jagan Teki8473b322019-07-22 17:22:56 +0530147 } else if (n_buf != 0) {
148 ret = spi_xfer(slave, n_buf * 8, txbuf, rxbuf, SPI_XFER_END);
149 if (ret)
Patrick Delaunay46c53912020-10-15 17:18:17 +0200150 dev_dbg(slave->dev,
151 "spi: failed to transfer %zu bytes of data: %d\n",
152 n_buf, ret);
Jagan Teki8473b322019-07-22 17:22:56 +0530153 }
154
155 return ret;
156}
157
Simon Glass71634f22016-11-13 14:22:01 -0700158#if !CONFIG_IS_ENABLED(OF_PLATDATA)
Simon Glass6f849c32015-06-23 15:39:05 -0600159static int spi_child_post_bind(struct udevice *dev)
Simon Glassd7af6a42014-10-13 23:41:52 -0600160{
Simon Glassd0cff032015-01-25 08:27:12 -0700161 struct dm_spi_slave_platdata *plat = dev_get_parent_platdata(dev);
Simon Glassd7af6a42014-10-13 23:41:52 -0600162
Simon Glass279e26f2017-05-18 20:09:54 -0600163 if (!dev_of_valid(dev))
Simon Glassd0cff032015-01-25 08:27:12 -0700164 return 0;
165
Simon Glass279e26f2017-05-18 20:09:54 -0600166 return spi_slave_ofdata_to_platdata(dev, plat);
Simon Glassd0cff032015-01-25 08:27:12 -0700167}
Simon Glass71634f22016-11-13 14:22:01 -0700168#endif
Simon Glassd0cff032015-01-25 08:27:12 -0700169
Simon Glass6f849c32015-06-23 15:39:05 -0600170static int spi_post_probe(struct udevice *bus)
Simon Glassd0cff032015-01-25 08:27:12 -0700171{
Simon Glass71634f22016-11-13 14:22:01 -0700172#if !CONFIG_IS_ENABLED(OF_PLATDATA)
Simon Glasse564f052015-03-05 12:25:20 -0700173 struct dm_spi_bus *spi = dev_get_uclass_priv(bus);
Simon Glassd0cff032015-01-25 08:27:12 -0700174
Simon Glass279e26f2017-05-18 20:09:54 -0600175 spi->max_hz = dev_read_u32_default(bus, "spi-max-frequency", 0);
Simon Glass71634f22016-11-13 14:22:01 -0700176#endif
Michal Simek281f1562015-10-27 13:36:42 +0100177#if defined(CONFIG_NEEDS_MANUAL_RELOC)
178 struct dm_spi_ops *ops = spi_get_ops(bus);
Ashok Reddy Soma4d9b1af2019-09-17 00:11:02 -0600179 static int reloc_done;
Michal Simek281f1562015-10-27 13:36:42 +0100180
Ashok Reddy Soma4d9b1af2019-09-17 00:11:02 -0600181 if (!reloc_done) {
182 if (ops->claim_bus)
183 ops->claim_bus += gd->reloc_off;
184 if (ops->release_bus)
185 ops->release_bus += gd->reloc_off;
186 if (ops->set_wordlen)
187 ops->set_wordlen += gd->reloc_off;
188 if (ops->xfer)
189 ops->xfer += gd->reloc_off;
190 if (ops->set_speed)
191 ops->set_speed += gd->reloc_off;
192 if (ops->set_mode)
193 ops->set_mode += gd->reloc_off;
194 if (ops->cs_info)
195 ops->cs_info += gd->reloc_off;
196 reloc_done++;
197 }
Michal Simek281f1562015-10-27 13:36:42 +0100198#endif
199
Simon Glassd7af6a42014-10-13 23:41:52 -0600200 return 0;
201}
202
Simon Glass6f849c32015-06-23 15:39:05 -0600203static int spi_child_pre_probe(struct udevice *dev)
Simon Glass440714e2015-01-25 08:27:11 -0700204{
Simon Glassd0cff032015-01-25 08:27:12 -0700205 struct dm_spi_slave_platdata *plat = dev_get_parent_platdata(dev);
Simon Glassbcbe3d12015-09-28 23:32:01 -0600206 struct spi_slave *slave = dev_get_parent_priv(dev);
Simon Glass440714e2015-01-25 08:27:11 -0700207
Simon Glassd0cff032015-01-25 08:27:12 -0700208 /*
209 * This is needed because we pass struct spi_slave around the place
210 * instead slave->dev (a struct udevice). So we have to have some
211 * way to access the slave udevice given struct spi_slave. Once we
212 * change the SPI API to use udevice instead of spi_slave, we can
213 * drop this.
214 */
Simon Glass440714e2015-01-25 08:27:11 -0700215 slave->dev = dev;
216
Simon Glassd0cff032015-01-25 08:27:12 -0700217 slave->max_hz = plat->max_hz;
218 slave->mode = plat->mode;
Christophe Ricard674f3602016-01-17 11:56:48 +0100219 slave->wordlen = SPI_DEFAULT_WORDLEN;
Simon Glassd0cff032015-01-25 08:27:12 -0700220
Simon Glass440714e2015-01-25 08:27:11 -0700221 return 0;
222}
223
Simon Glassd7af6a42014-10-13 23:41:52 -0600224int spi_chip_select(struct udevice *dev)
225{
Simon Glassd0cff032015-01-25 08:27:12 -0700226 struct dm_spi_slave_platdata *plat = dev_get_parent_platdata(dev);
Simon Glassd7af6a42014-10-13 23:41:52 -0600227
Simon Glassd0cff032015-01-25 08:27:12 -0700228 return plat ? plat->cs : -ENOENT;
Simon Glassd7af6a42014-10-13 23:41:52 -0600229}
230
Simon Glassff56bba2014-11-11 10:46:22 -0700231int spi_find_chip_select(struct udevice *bus, int cs, struct udevice **devp)
Simon Glassd7af6a42014-10-13 23:41:52 -0600232{
Bin Meng7bacce52019-09-09 06:00:02 -0700233 struct dm_spi_ops *ops;
234 struct spi_cs_info info;
Simon Glassd7af6a42014-10-13 23:41:52 -0600235 struct udevice *dev;
Bin Meng7bacce52019-09-09 06:00:02 -0700236 int ret;
237
238 /*
239 * Ask the driver. For the moment we don't have CS info.
240 * When we do we could provide the driver with a helper function
241 * to figure out what chip selects are valid, or just handle the
242 * request.
243 */
244 ops = spi_get_ops(bus);
245 if (ops->cs_info) {
246 ret = ops->cs_info(bus, cs, &info);
247 } else {
248 /*
249 * We could assume there is at least one valid chip select.
250 * The driver didn't care enough to tell us.
251 */
252 ret = 0;
253 }
254
255 if (ret) {
Patrick Delaunay46c53912020-10-15 17:18:17 +0200256 dev_err(bus, "Invalid cs %d (err=%d)\n", cs, ret);
Bin Meng7bacce52019-09-09 06:00:02 -0700257 return ret;
258 }
Simon Glassd7af6a42014-10-13 23:41:52 -0600259
260 for (device_find_first_child(bus, &dev); dev;
261 device_find_next_child(&dev)) {
Simon Glassd0cff032015-01-25 08:27:12 -0700262 struct dm_spi_slave_platdata *plat;
Simon Glassd7af6a42014-10-13 23:41:52 -0600263
Simon Glassd0cff032015-01-25 08:27:12 -0700264 plat = dev_get_parent_platdata(dev);
Patrick Delaunay46c53912020-10-15 17:18:17 +0200265 dev_dbg(bus, "%s: plat=%p, cs=%d\n", __func__, plat, plat->cs);
Simon Glassd0cff032015-01-25 08:27:12 -0700266 if (plat->cs == cs) {
Simon Glassd7af6a42014-10-13 23:41:52 -0600267 *devp = dev;
268 return 0;
269 }
270 }
271
272 return -ENODEV;
273}
274
275int spi_cs_is_valid(unsigned int busnum, unsigned int cs)
276{
277 struct spi_cs_info info;
278 struct udevice *bus;
279 int ret;
280
281 ret = uclass_find_device_by_seq(UCLASS_SPI, busnum, false, &bus);
282 if (ret) {
Patrick Delaunay46c53912020-10-15 17:18:17 +0200283 log_debug("%s: No bus %d\n", __func__, busnum);
Simon Glassd7af6a42014-10-13 23:41:52 -0600284 return ret;
285 }
286
287 return spi_cs_info(bus, cs, &info);
288}
289
290int spi_cs_info(struct udevice *bus, uint cs, struct spi_cs_info *info)
291{
292 struct spi_cs_info local_info;
Simon Glassd7af6a42014-10-13 23:41:52 -0600293 int ret;
294
295 if (!info)
296 info = &local_info;
297
298 /* If there is a device attached, return it */
299 info->dev = NULL;
300 ret = spi_find_chip_select(bus, cs, &info->dev);
Bin Meng7bacce52019-09-09 06:00:02 -0700301 return ret == -ENODEV ? 0 : ret;
Simon Glassd7af6a42014-10-13 23:41:52 -0600302}
303
Simon Glassd7af6a42014-10-13 23:41:52 -0600304int spi_find_bus_and_cs(int busnum, int cs, struct udevice **busp,
305 struct udevice **devp)
306{
307 struct udevice *bus, *dev;
308 int ret;
309
310 ret = uclass_find_device_by_seq(UCLASS_SPI, busnum, false, &bus);
311 if (ret) {
Patrick Delaunay46c53912020-10-15 17:18:17 +0200312 log_debug("%s: No bus %d\n", __func__, busnum);
Simon Glassd7af6a42014-10-13 23:41:52 -0600313 return ret;
314 }
315 ret = spi_find_chip_select(bus, cs, &dev);
316 if (ret) {
Patrick Delaunay46c53912020-10-15 17:18:17 +0200317 dev_dbg(bus, "%s: No cs %d\n", __func__, cs);
Simon Glassd7af6a42014-10-13 23:41:52 -0600318 return ret;
319 }
320 *busp = bus;
321 *devp = dev;
322
323 return ret;
324}
325
326int spi_get_bus_and_cs(int busnum, int cs, int speed, int mode,
327 const char *drv_name, const char *dev_name,
328 struct udevice **busp, struct spi_slave **devp)
329{
330 struct udevice *bus, *dev;
Vignesh R96907c02016-07-06 10:04:28 +0530331 struct dm_spi_slave_platdata *plat;
Marcin Wojtasf7dd5372019-11-21 05:38:47 +0100332 struct spi_slave *slave;
Simon Glassd7af6a42014-10-13 23:41:52 -0600333 bool created = false;
334 int ret;
335
Thomas Fitzsimmons640abba2019-09-06 07:51:19 -0400336#if CONFIG_IS_ENABLED(OF_PLATDATA)
Simon Glass71634f22016-11-13 14:22:01 -0700337 ret = uclass_first_device_err(UCLASS_SPI, &bus);
338#else
Simon Glassd7af6a42014-10-13 23:41:52 -0600339 ret = uclass_get_device_by_seq(UCLASS_SPI, busnum, &bus);
Simon Glass71634f22016-11-13 14:22:01 -0700340#endif
Simon Glassd7af6a42014-10-13 23:41:52 -0600341 if (ret) {
Patrick Delaunay46c53912020-10-15 17:18:17 +0200342 log_err("Invalid bus %d (err=%d)\n", busnum, ret);
Simon Glassd7af6a42014-10-13 23:41:52 -0600343 return ret;
344 }
345 ret = spi_find_chip_select(bus, cs, &dev);
346
347 /*
348 * If there is no such device, create one automatically. This means
349 * that we don't need a device tree node or platform data for the
350 * SPI flash chip - we will bind to the correct driver.
351 */
352 if (ret == -ENODEV && drv_name) {
Patrick Delaunay46c53912020-10-15 17:18:17 +0200353 dev_dbg(bus, "%s: Binding new device '%s', busnum=%d, cs=%d, driver=%s\n",
354 __func__, dev_name, busnum, cs, drv_name);
Simon Glass6b186562014-11-11 10:46:23 -0700355 ret = device_bind_driver(bus, drv_name, dev_name, &dev);
Simon Glass28f98852016-11-13 14:22:05 -0700356 if (ret) {
Patrick Delaunay46c53912020-10-15 17:18:17 +0200357 dev_dbg(bus, "%s: Unable to bind driver (ret=%d)\n",
358 __func__, ret);
Simon Glassd7af6a42014-10-13 23:41:52 -0600359 return ret;
Simon Glass28f98852016-11-13 14:22:05 -0700360 }
Simon Glassd0cff032015-01-25 08:27:12 -0700361 plat = dev_get_parent_platdata(dev);
362 plat->cs = cs;
Simon Goldschmidt12bfb2e2018-10-30 21:09:48 +0100363 if (speed) {
364 plat->max_hz = speed;
365 } else {
Patrick Delaunay46c53912020-10-15 17:18:17 +0200366 dev_warn(bus,
367 "Warning: SPI speed fallback to %u kHz\n",
368 SPI_DEFAULT_SPEED_HZ / 1000);
Simon Goldschmidt12bfb2e2018-10-30 21:09:48 +0100369 plat->max_hz = SPI_DEFAULT_SPEED_HZ;
370 }
Simon Glassd0cff032015-01-25 08:27:12 -0700371 plat->mode = mode;
Simon Glassd7af6a42014-10-13 23:41:52 -0600372 created = true;
373 } else if (ret) {
Patrick Delaunay46c53912020-10-15 17:18:17 +0200374 dev_err(bus, "Invalid chip select %d:%d (err=%d)\n", busnum, cs, ret);
Simon Glassd7af6a42014-10-13 23:41:52 -0600375 return ret;
376 }
377
378 if (!device_active(dev)) {
Simon Glassd0cff032015-01-25 08:27:12 -0700379 struct spi_slave *slave;
Simon Glassd7af6a42014-10-13 23:41:52 -0600380
Simon Glassd0cff032015-01-25 08:27:12 -0700381 ret = device_probe(dev);
Simon Glassd7af6a42014-10-13 23:41:52 -0600382 if (ret)
383 goto err;
Simon Glassbcbe3d12015-09-28 23:32:01 -0600384 slave = dev_get_parent_priv(dev);
Simon Glassd7af6a42014-10-13 23:41:52 -0600385 slave->dev = dev;
Simon Glassd7af6a42014-10-13 23:41:52 -0600386 }
387
Marcin Wojtasf7dd5372019-11-21 05:38:47 +0100388 slave = dev_get_parent_priv(dev);
Patrick Delaunayb0cc1b82019-02-27 15:36:44 +0100389
Marcin Wojtasf7dd5372019-11-21 05:38:47 +0100390 /*
391 * In case the operation speed is not yet established by
392 * dm_spi_claim_bus() ensure the bus is configured properly.
393 */
394 if (!slave->speed) {
395 ret = spi_claim_bus(slave);
396 if (ret)
397 goto err;
Vignesh R96907c02016-07-06 10:04:28 +0530398 }
Simon Glassd7af6a42014-10-13 23:41:52 -0600399
400 *busp = bus;
Marcin Wojtasf7dd5372019-11-21 05:38:47 +0100401 *devp = slave;
Patrick Delaunay46c53912020-10-15 17:18:17 +0200402 log_debug("%s: bus=%p, slave=%p\n", __func__, bus, *devp);
Simon Glassd7af6a42014-10-13 23:41:52 -0600403
404 return 0;
405
406err:
Patrick Delaunay46c53912020-10-15 17:18:17 +0200407 log_debug("%s: Error path, created=%d, device '%s'\n", __func__,
408 created, dev->name);
Simon Glassd7af6a42014-10-13 23:41:52 -0600409 if (created) {
Stefan Roese706865a2017-03-20 12:51:48 +0100410 device_remove(dev, DM_REMOVE_NORMAL);
Simon Glassd7af6a42014-10-13 23:41:52 -0600411 device_unbind(dev);
412 }
413
414 return ret;
415}
416
417/* Compatibility function - to be removed */
Simon Glassd7af6a42014-10-13 23:41:52 -0600418struct spi_slave *spi_setup_slave(unsigned int busnum, unsigned int cs,
419 unsigned int speed, unsigned int mode)
420{
421 struct spi_slave *slave;
422 struct udevice *dev;
423 int ret;
424
425 ret = spi_get_bus_and_cs(busnum, cs, speed, mode, NULL, 0, &dev,
Simon Glass279e26f2017-05-18 20:09:54 -0600426 &slave);
Simon Glassd7af6a42014-10-13 23:41:52 -0600427 if (ret)
428 return NULL;
429
430 return slave;
431}
432
433void spi_free_slave(struct spi_slave *slave)
434{
Stefan Roese706865a2017-03-20 12:51:48 +0100435 device_remove(slave->dev, DM_REMOVE_NORMAL);
Simon Glassd7af6a42014-10-13 23:41:52 -0600436 slave->dev = NULL;
437}
438
Simon Glass279e26f2017-05-18 20:09:54 -0600439int spi_slave_ofdata_to_platdata(struct udevice *dev,
Simon Glassd0cff032015-01-25 08:27:12 -0700440 struct dm_spi_slave_platdata *plat)
Simon Glassd7af6a42014-10-13 23:41:52 -0600441{
Jagan Teki08fe9c22016-08-08 17:12:12 +0530442 int mode = 0;
Mugunthan V Nf8e2f922015-12-23 20:39:37 +0530443 int value;
Simon Glassd7af6a42014-10-13 23:41:52 -0600444
Simon Glass279e26f2017-05-18 20:09:54 -0600445 plat->cs = dev_read_u32_default(dev, "reg", -1);
Simon Goldschmidt12bfb2e2018-10-30 21:09:48 +0100446 plat->max_hz = dev_read_u32_default(dev, "spi-max-frequency",
447 SPI_DEFAULT_SPEED_HZ);
Simon Glass279e26f2017-05-18 20:09:54 -0600448 if (dev_read_bool(dev, "spi-cpol"))
Simon Glassd7af6a42014-10-13 23:41:52 -0600449 mode |= SPI_CPOL;
Simon Glass279e26f2017-05-18 20:09:54 -0600450 if (dev_read_bool(dev, "spi-cpha"))
Simon Glassd7af6a42014-10-13 23:41:52 -0600451 mode |= SPI_CPHA;
Simon Glass279e26f2017-05-18 20:09:54 -0600452 if (dev_read_bool(dev, "spi-cs-high"))
Simon Glassd7af6a42014-10-13 23:41:52 -0600453 mode |= SPI_CS_HIGH;
Simon Glass279e26f2017-05-18 20:09:54 -0600454 if (dev_read_bool(dev, "spi-3wire"))
Jagan Teki379b49d2015-12-03 22:19:05 +0530455 mode |= SPI_3WIRE;
Simon Glass279e26f2017-05-18 20:09:54 -0600456 if (dev_read_bool(dev, "spi-half-duplex"))
Simon Glassd7af6a42014-10-13 23:41:52 -0600457 mode |= SPI_PREAMBLE;
Mugunthan V Nf8e2f922015-12-23 20:39:37 +0530458
459 /* Device DUAL/QUAD mode */
Simon Glass279e26f2017-05-18 20:09:54 -0600460 value = dev_read_u32_default(dev, "spi-tx-bus-width", 1);
Mugunthan V Nf8e2f922015-12-23 20:39:37 +0530461 switch (value) {
462 case 1:
463 break;
464 case 2:
465 mode |= SPI_TX_DUAL;
466 break;
467 case 4:
468 mode |= SPI_TX_QUAD;
469 break;
Vignesh Raghavendra658df8b2019-12-05 15:46:05 +0530470 case 8:
471 mode |= SPI_TX_OCTAL;
472 break;
Mugunthan V Nf8e2f922015-12-23 20:39:37 +0530473 default:
Simon Glass1b7c28f2016-11-29 20:00:13 -0700474 warn_non_spl("spi-tx-bus-width %d not supported\n", value);
Mugunthan V Nf8e2f922015-12-23 20:39:37 +0530475 break;
476 }
477
Simon Glass279e26f2017-05-18 20:09:54 -0600478 value = dev_read_u32_default(dev, "spi-rx-bus-width", 1);
Mugunthan V Nf8e2f922015-12-23 20:39:37 +0530479 switch (value) {
480 case 1:
481 break;
482 case 2:
Jagan Teki08fe9c22016-08-08 17:12:12 +0530483 mode |= SPI_RX_DUAL;
Mugunthan V Nf8e2f922015-12-23 20:39:37 +0530484 break;
485 case 4:
Jagan Teki08fe9c22016-08-08 17:12:12 +0530486 mode |= SPI_RX_QUAD;
Mugunthan V Nf8e2f922015-12-23 20:39:37 +0530487 break;
Vignesh Raghavendra658df8b2019-12-05 15:46:05 +0530488 case 8:
489 mode |= SPI_RX_OCTAL;
490 break;
Mugunthan V Nf8e2f922015-12-23 20:39:37 +0530491 default:
Simon Glass1b7c28f2016-11-29 20:00:13 -0700492 warn_non_spl("spi-rx-bus-width %d not supported\n", value);
Mugunthan V Nf8e2f922015-12-23 20:39:37 +0530493 break;
494 }
495
Jagan Teki08fe9c22016-08-08 17:12:12 +0530496 plat->mode = mode;
Mugunthan V Nf8e2f922015-12-23 20:39:37 +0530497
Simon Glassd7af6a42014-10-13 23:41:52 -0600498 return 0;
499}
500
501UCLASS_DRIVER(spi) = {
502 .id = UCLASS_SPI,
503 .name = "spi",
Simon Glass9cc36a22015-01-25 08:27:05 -0700504 .flags = DM_UC_FLAG_SEQ_ALIAS,
Faiz Abbas280af012020-09-14 12:11:14 +0530505#if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
Simon Glass91195482016-07-05 17:10:10 -0600506 .post_bind = dm_scan_fdt_dev,
Simon Glass71634f22016-11-13 14:22:01 -0700507#endif
Simon Glassd7af6a42014-10-13 23:41:52 -0600508 .post_probe = spi_post_probe,
Simon Glass440714e2015-01-25 08:27:11 -0700509 .child_pre_probe = spi_child_pre_probe,
Simon Glassd7af6a42014-10-13 23:41:52 -0600510 .per_device_auto_alloc_size = sizeof(struct dm_spi_bus),
Simon Glass19a25f62015-01-25 08:27:07 -0700511 .per_child_auto_alloc_size = sizeof(struct spi_slave),
Simon Glassd0cff032015-01-25 08:27:12 -0700512 .per_child_platdata_auto_alloc_size =
513 sizeof(struct dm_spi_slave_platdata),
Simon Glass71634f22016-11-13 14:22:01 -0700514#if !CONFIG_IS_ENABLED(OF_PLATDATA)
Simon Glassd0cff032015-01-25 08:27:12 -0700515 .child_post_bind = spi_child_post_bind,
Simon Glass71634f22016-11-13 14:22:01 -0700516#endif
Simon Glassd7af6a42014-10-13 23:41:52 -0600517};
518
519UCLASS_DRIVER(spi_generic) = {
520 .id = UCLASS_SPI_GENERIC,
521 .name = "spi_generic",
522};
523
524U_BOOT_DRIVER(spi_generic_drv) = {
525 .name = "spi_generic_drv",
526 .id = UCLASS_SPI_GENERIC,
527};