blob: 3fe351f8a7064bfc2f9fcf6b884523e4dea1d98c [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 <common.h>
9#include <dm.h>
10#include <errno.h>
Simon Glass0f2af882020-05-10 11:40:05 -060011#include <log.h>
Simon Glassdd82d442014-10-13 23:41:52 -060012#include <malloc.h>
13#include <spi.h>
Patrick Delaunay1d83e032020-10-15 17:18:17 +020014#include <dm/device_compat.h>
Simon Glassdd82d442014-10-13 23:41:52 -060015#include <dm/device-internal.h>
16#include <dm/uclass-internal.h>
Simon Glassdd82d442014-10-13 23:41:52 -060017#include <dm/lists.h>
18#include <dm/util.h>
19
20DECLARE_GLOBAL_DATA_PTR;
21
Simon Goldschmidta942e1a2018-10-30 21:09:48 +010022#define SPI_DEFAULT_SPEED_HZ 100000
23
Simon Glassdd82d442014-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 Delaunay1d83e032020-10-15 17:18:17 +020035 dev_err(bus, "Cannot set speed (err=%d)\n", ret);
Simon Glassdd82d442014-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 Delaunay1d83e032020-10-15 17:18:17 +020044 dev_err(bus, "Cannot set mode (err=%d)\n", ret);
Simon Glassdd82d442014-10-13 23:41:52 -060045 return ret;
46 }
47
48 return 0;
49}
50
Peng Fanfdd88a32016-05-03 10:02:22 +080051int dm_spi_claim_bus(struct udevice *dev)
Simon Glassdd82d442014-10-13 23:41:52 -060052{
Simon Glassdd82d442014-10-13 23:41:52 -060053 struct udevice *bus = dev->parent;
54 struct dm_spi_ops *ops = spi_get_ops(bus);
Simon Glassde0977b2015-03-05 12:25:20 -070055 struct dm_spi_bus *spi = dev_get_uclass_priv(bus);
Peng Fanfdd88a32016-05-03 10:02:22 +080056 struct spi_slave *slave = dev_get_parent_priv(dev);
Ovidiu Panait40dcee12020-12-14 19:06:50 +020057 uint speed, mode;
Simon Glassdd82d442014-10-13 23:41:52 -060058
59 speed = slave->max_hz;
Ovidiu Panait40dcee12020-12-14 19:06:50 +020060 mode = slave->mode;
61
Simon Glassdd82d442014-10-13 23:41:52 -060062 if (spi->max_hz) {
63 if (speed)
Ovidiu Panait40dcee12020-12-14 19:06:50 +020064 speed = min(speed, spi->max_hz);
Simon Glassdd82d442014-10-13 23:41:52 -060065 else
66 speed = spi->max_hz;
67 }
68 if (!speed)
Simon Goldschmidta942e1a2018-10-30 21:09:48 +010069 speed = SPI_DEFAULT_SPEED_HZ;
Ovidiu Panait40dcee12020-12-14 19:06:50 +020070
71 if (speed != spi->speed || mode != spi->mode) {
Mario Six2d844dd2018-01-15 11:08:41 +010072 int ret = spi_set_speed_mode(bus, speed, slave->mode);
73
Simon Glassb46cb632015-02-17 15:29:35 -070074 if (ret)
Simon Glass00d99902018-10-01 12:22:24 -060075 return log_ret(ret);
Ovidiu Panait40dcee12020-12-14 19:06:50 +020076
77 spi->speed = speed;
78 spi->mode = mode;
Simon Glassb46cb632015-02-17 15:29:35 -070079 }
Simon Glassdd82d442014-10-13 23:41:52 -060080
Simon Glass00d99902018-10-01 12:22:24 -060081 return log_ret(ops->claim_bus ? ops->claim_bus(dev) : 0);
Simon Glassdd82d442014-10-13 23:41:52 -060082}
83
Peng Fanfdd88a32016-05-03 10:02:22 +080084void dm_spi_release_bus(struct udevice *dev)
Simon Glassdd82d442014-10-13 23:41:52 -060085{
Simon Glassdd82d442014-10-13 23:41:52 -060086 struct udevice *bus = dev->parent;
87 struct dm_spi_ops *ops = spi_get_ops(bus);
88
89 if (ops->release_bus)
Simon Glass5c74fba2015-04-19 09:05:40 -060090 ops->release_bus(dev);
Simon Glassdd82d442014-10-13 23:41:52 -060091}
92
Peng Fanfdd88a32016-05-03 10:02:22 +080093int dm_spi_xfer(struct udevice *dev, unsigned int bitlen,
94 const void *dout, void *din, unsigned long flags)
Simon Glassdd82d442014-10-13 23:41:52 -060095{
Simon Glassdd82d442014-10-13 23:41:52 -060096 struct udevice *bus = dev->parent;
Simon Glass2d2e8602019-12-06 21:42:35 -070097 struct dm_spi_ops *ops = spi_get_ops(bus);
Simon Glassdd82d442014-10-13 23:41:52 -060098
99 if (bus->uclass->uc_drv->id != UCLASS_SPI)
100 return -EOPNOTSUPP;
Simon Glass2d2e8602019-12-06 21:42:35 -0700101 if (!ops->xfer)
102 return -ENOSYS;
Simon Glassdd82d442014-10-13 23:41:52 -0600103
Simon Glass2d2e8602019-12-06 21:42:35 -0700104 return ops->xfer(dev, bitlen, dout, din, flags);
Simon Glassdd82d442014-10-13 23:41:52 -0600105}
106
Simon Glass37ad0fe2019-10-20 21:31:47 -0600107int dm_spi_get_mmap(struct udevice *dev, ulong *map_basep, uint *map_sizep,
108 uint *offsetp)
109{
110 struct udevice *bus = dev->parent;
111 struct dm_spi_ops *ops = spi_get_ops(bus);
112
113 if (bus->uclass->uc_drv->id != UCLASS_SPI)
114 return -EOPNOTSUPP;
115 if (!ops->get_mmap)
116 return -ENOSYS;
117
118 return ops->get_mmap(dev, map_basep, map_sizep, offsetp);
119}
120
Peng Fanfdd88a32016-05-03 10:02:22 +0800121int spi_claim_bus(struct spi_slave *slave)
122{
Simon Glass00d99902018-10-01 12:22:24 -0600123 return log_ret(dm_spi_claim_bus(slave->dev));
Peng Fanfdd88a32016-05-03 10:02:22 +0800124}
125
126void spi_release_bus(struct spi_slave *slave)
127{
128 dm_spi_release_bus(slave->dev);
129}
130
131int spi_xfer(struct spi_slave *slave, unsigned int bitlen,
132 const void *dout, void *din, unsigned long flags)
133{
134 return dm_spi_xfer(slave->dev, bitlen, dout, din, flags);
135}
136
Jagan Teki7cc71fd2019-07-22 17:22:56 +0530137int spi_write_then_read(struct spi_slave *slave, const u8 *opcode,
138 size_t n_opcode, const u8 *txbuf, u8 *rxbuf,
139 size_t n_buf)
140{
141 unsigned long flags = SPI_XFER_BEGIN;
142 int ret;
143
144 if (n_buf == 0)
145 flags |= SPI_XFER_END;
146
147 ret = spi_xfer(slave, n_opcode * 8, opcode, NULL, flags);
148 if (ret) {
Patrick Delaunay1d83e032020-10-15 17:18:17 +0200149 dev_dbg(slave->dev,
150 "spi: failed to send command (%zu bytes): %d\n",
151 n_opcode, ret);
Jagan Teki7cc71fd2019-07-22 17:22:56 +0530152 } else if (n_buf != 0) {
153 ret = spi_xfer(slave, n_buf * 8, txbuf, rxbuf, SPI_XFER_END);
154 if (ret)
Patrick Delaunay1d83e032020-10-15 17:18:17 +0200155 dev_dbg(slave->dev,
156 "spi: failed to transfer %zu bytes of data: %d\n",
157 n_buf, ret);
Jagan Teki7cc71fd2019-07-22 17:22:56 +0530158 }
159
160 return ret;
161}
162
Simon Glass3fb33392016-11-13 14:22:01 -0700163#if !CONFIG_IS_ENABLED(OF_PLATDATA)
Simon Glassd941aaf2015-06-23 15:39:05 -0600164static int spi_child_post_bind(struct udevice *dev)
Simon Glassdd82d442014-10-13 23:41:52 -0600165{
Simon Glassb75b15b2020-12-03 16:55:23 -0700166 struct dm_spi_slave_plat *plat = dev_get_parent_plat(dev);
Simon Glassdd82d442014-10-13 23:41:52 -0600167
Simon Glassf1d50f72020-12-19 10:40:13 -0700168 if (!dev_has_ofnode(dev))
Simon Glass5d2ee052015-01-25 08:27:12 -0700169 return 0;
170
Simon Glassaad29ae2020-12-03 16:55:21 -0700171 return spi_slave_of_to_plat(dev, plat);
Simon Glass5d2ee052015-01-25 08:27:12 -0700172}
Simon Glass3fb33392016-11-13 14:22:01 -0700173#endif
Simon Glass5d2ee052015-01-25 08:27:12 -0700174
Simon Glassd941aaf2015-06-23 15:39:05 -0600175static int spi_post_probe(struct udevice *bus)
Simon Glass5d2ee052015-01-25 08:27:12 -0700176{
Simon Glass3fb33392016-11-13 14:22:01 -0700177#if !CONFIG_IS_ENABLED(OF_PLATDATA)
Simon Glassde0977b2015-03-05 12:25:20 -0700178 struct dm_spi_bus *spi = dev_get_uclass_priv(bus);
Simon Glass5d2ee052015-01-25 08:27:12 -0700179
Simon Glass4b280582017-05-18 20:09:54 -0600180 spi->max_hz = dev_read_u32_default(bus, "spi-max-frequency", 0);
Simon Glass3fb33392016-11-13 14:22:01 -0700181#endif
Michal Simek28d7e4e2015-10-27 13:36:42 +0100182#if defined(CONFIG_NEEDS_MANUAL_RELOC)
183 struct dm_spi_ops *ops = spi_get_ops(bus);
Ashok Reddy Soma1d89d8c2019-09-17 00:11:02 -0600184 static int reloc_done;
Michal Simek28d7e4e2015-10-27 13:36:42 +0100185
Ashok Reddy Soma1d89d8c2019-09-17 00:11:02 -0600186 if (!reloc_done) {
187 if (ops->claim_bus)
188 ops->claim_bus += gd->reloc_off;
189 if (ops->release_bus)
190 ops->release_bus += gd->reloc_off;
191 if (ops->set_wordlen)
192 ops->set_wordlen += gd->reloc_off;
193 if (ops->xfer)
194 ops->xfer += gd->reloc_off;
195 if (ops->set_speed)
196 ops->set_speed += gd->reloc_off;
197 if (ops->set_mode)
198 ops->set_mode += gd->reloc_off;
199 if (ops->cs_info)
200 ops->cs_info += gd->reloc_off;
201 reloc_done++;
202 }
Michal Simek28d7e4e2015-10-27 13:36:42 +0100203#endif
204
Simon Glassdd82d442014-10-13 23:41:52 -0600205 return 0;
206}
207
Simon Glassd941aaf2015-06-23 15:39:05 -0600208static int spi_child_pre_probe(struct udevice *dev)
Simon Glass82c2f502015-01-25 08:27:11 -0700209{
Simon Glassb75b15b2020-12-03 16:55:23 -0700210 struct dm_spi_slave_plat *plat = dev_get_parent_plat(dev);
Simon Glassde44acf2015-09-28 23:32:01 -0600211 struct spi_slave *slave = dev_get_parent_priv(dev);
Simon Glass82c2f502015-01-25 08:27:11 -0700212
Simon Glass5d2ee052015-01-25 08:27:12 -0700213 /*
214 * This is needed because we pass struct spi_slave around the place
215 * instead slave->dev (a struct udevice). So we have to have some
216 * way to access the slave udevice given struct spi_slave. Once we
217 * change the SPI API to use udevice instead of spi_slave, we can
218 * drop this.
219 */
Simon Glass82c2f502015-01-25 08:27:11 -0700220 slave->dev = dev;
221
Simon Glass5d2ee052015-01-25 08:27:12 -0700222 slave->max_hz = plat->max_hz;
223 slave->mode = plat->mode;
Christophe Ricardfb0c53e2016-01-17 11:56:48 +0100224 slave->wordlen = SPI_DEFAULT_WORDLEN;
Simon Glass5d2ee052015-01-25 08:27:12 -0700225
Simon Glass82c2f502015-01-25 08:27:11 -0700226 return 0;
227}
228
Simon Glassdd82d442014-10-13 23:41:52 -0600229int spi_chip_select(struct udevice *dev)
230{
Simon Glassb75b15b2020-12-03 16:55:23 -0700231 struct dm_spi_slave_plat *plat = dev_get_parent_plat(dev);
Simon Glassdd82d442014-10-13 23:41:52 -0600232
Simon Glass5d2ee052015-01-25 08:27:12 -0700233 return plat ? plat->cs : -ENOENT;
Simon Glassdd82d442014-10-13 23:41:52 -0600234}
235
Simon Glass5ef36f22014-11-11 10:46:22 -0700236int spi_find_chip_select(struct udevice *bus, int cs, struct udevice **devp)
Simon Glassdd82d442014-10-13 23:41:52 -0600237{
Bin Meng9741e732019-09-09 06:00:02 -0700238 struct dm_spi_ops *ops;
239 struct spi_cs_info info;
Simon Glassdd82d442014-10-13 23:41:52 -0600240 struct udevice *dev;
Bin Meng9741e732019-09-09 06:00:02 -0700241 int ret;
242
243 /*
244 * Ask the driver. For the moment we don't have CS info.
245 * When we do we could provide the driver with a helper function
246 * to figure out what chip selects are valid, or just handle the
247 * request.
248 */
249 ops = spi_get_ops(bus);
250 if (ops->cs_info) {
251 ret = ops->cs_info(bus, cs, &info);
252 } else {
253 /*
254 * We could assume there is at least one valid chip select.
255 * The driver didn't care enough to tell us.
256 */
257 ret = 0;
258 }
259
260 if (ret) {
Patrick Delaunay1d83e032020-10-15 17:18:17 +0200261 dev_err(bus, "Invalid cs %d (err=%d)\n", cs, ret);
Bin Meng9741e732019-09-09 06:00:02 -0700262 return ret;
263 }
Simon Glassdd82d442014-10-13 23:41:52 -0600264
265 for (device_find_first_child(bus, &dev); dev;
266 device_find_next_child(&dev)) {
Simon Glassb75b15b2020-12-03 16:55:23 -0700267 struct dm_spi_slave_plat *plat;
Simon Glassdd82d442014-10-13 23:41:52 -0600268
Simon Glass71fa5b42020-12-03 16:55:18 -0700269 plat = dev_get_parent_plat(dev);
Patrick Delaunay1d83e032020-10-15 17:18:17 +0200270 dev_dbg(bus, "%s: plat=%p, cs=%d\n", __func__, plat, plat->cs);
Simon Glass5d2ee052015-01-25 08:27:12 -0700271 if (plat->cs == cs) {
Simon Glassdd82d442014-10-13 23:41:52 -0600272 *devp = dev;
273 return 0;
274 }
275 }
276
277 return -ENODEV;
278}
279
280int spi_cs_is_valid(unsigned int busnum, unsigned int cs)
281{
282 struct spi_cs_info info;
283 struct udevice *bus;
284 int ret;
285
Simon Glass07e13382020-12-16 21:20:29 -0700286 ret = uclass_find_device_by_seq(UCLASS_SPI, busnum, &bus);
Simon Glassdd82d442014-10-13 23:41:52 -0600287 if (ret) {
Patrick Delaunay1d83e032020-10-15 17:18:17 +0200288 log_debug("%s: No bus %d\n", __func__, busnum);
Simon Glassdd82d442014-10-13 23:41:52 -0600289 return ret;
290 }
291
292 return spi_cs_info(bus, cs, &info);
293}
294
295int spi_cs_info(struct udevice *bus, uint cs, struct spi_cs_info *info)
296{
297 struct spi_cs_info local_info;
Simon Glassdd82d442014-10-13 23:41:52 -0600298 int ret;
299
300 if (!info)
301 info = &local_info;
302
303 /* If there is a device attached, return it */
304 info->dev = NULL;
305 ret = spi_find_chip_select(bus, cs, &info->dev);
Bin Meng9741e732019-09-09 06:00:02 -0700306 return ret == -ENODEV ? 0 : ret;
Simon Glassdd82d442014-10-13 23:41:52 -0600307}
308
Simon Glassdd82d442014-10-13 23:41:52 -0600309int spi_find_bus_and_cs(int busnum, int cs, struct udevice **busp,
310 struct udevice **devp)
311{
312 struct udevice *bus, *dev;
313 int ret;
314
Simon Glass07e13382020-12-16 21:20:29 -0700315 ret = uclass_find_device_by_seq(UCLASS_SPI, busnum, &bus);
Simon Glassdd82d442014-10-13 23:41:52 -0600316 if (ret) {
Patrick Delaunay1d83e032020-10-15 17:18:17 +0200317 log_debug("%s: No bus %d\n", __func__, busnum);
Simon Glassdd82d442014-10-13 23:41:52 -0600318 return ret;
319 }
320 ret = spi_find_chip_select(bus, cs, &dev);
321 if (ret) {
Patrick Delaunay1d83e032020-10-15 17:18:17 +0200322 dev_dbg(bus, "%s: No cs %d\n", __func__, cs);
Simon Glassdd82d442014-10-13 23:41:52 -0600323 return ret;
324 }
325 *busp = bus;
326 *devp = dev;
327
328 return ret;
329}
330
331int spi_get_bus_and_cs(int busnum, int cs, int speed, int mode,
332 const char *drv_name, const char *dev_name,
333 struct udevice **busp, struct spi_slave **devp)
334{
335 struct udevice *bus, *dev;
Simon Glassb75b15b2020-12-03 16:55:23 -0700336 struct dm_spi_slave_plat *plat;
Ovidiu Panait40dcee12020-12-14 19:06:50 +0200337 struct dm_spi_bus *bus_data;
Marcin Wojtas70d8f982019-11-21 05:38:47 +0100338 struct spi_slave *slave;
Simon Glassdd82d442014-10-13 23:41:52 -0600339 bool created = false;
340 int ret;
341
Thomas Fitzsimmons59c90f32019-09-06 07:51:19 -0400342#if CONFIG_IS_ENABLED(OF_PLATDATA)
Simon Glass3fb33392016-11-13 14:22:01 -0700343 ret = uclass_first_device_err(UCLASS_SPI, &bus);
344#else
Simon Glassdd82d442014-10-13 23:41:52 -0600345 ret = uclass_get_device_by_seq(UCLASS_SPI, busnum, &bus);
Simon Glass3fb33392016-11-13 14:22:01 -0700346#endif
Simon Glassdd82d442014-10-13 23:41:52 -0600347 if (ret) {
Patrick Delaunay1d83e032020-10-15 17:18:17 +0200348 log_err("Invalid bus %d (err=%d)\n", busnum, ret);
Simon Glassdd82d442014-10-13 23:41:52 -0600349 return ret;
350 }
351 ret = spi_find_chip_select(bus, cs, &dev);
352
353 /*
354 * If there is no such device, create one automatically. This means
355 * that we don't need a device tree node or platform data for the
356 * SPI flash chip - we will bind to the correct driver.
357 */
358 if (ret == -ENODEV && drv_name) {
Patrick Delaunay1d83e032020-10-15 17:18:17 +0200359 dev_dbg(bus, "%s: Binding new device '%s', busnum=%d, cs=%d, driver=%s\n",
360 __func__, dev_name, busnum, cs, drv_name);
Simon Glassd8a21f62014-11-11 10:46:23 -0700361 ret = device_bind_driver(bus, drv_name, dev_name, &dev);
Simon Glassf0307e12016-11-13 14:22:05 -0700362 if (ret) {
Patrick Delaunay1d83e032020-10-15 17:18:17 +0200363 dev_dbg(bus, "%s: Unable to bind driver (ret=%d)\n",
364 __func__, ret);
Simon Glassdd82d442014-10-13 23:41:52 -0600365 return ret;
Simon Glassf0307e12016-11-13 14:22:05 -0700366 }
Simon Glass71fa5b42020-12-03 16:55:18 -0700367 plat = dev_get_parent_plat(dev);
Simon Glass5d2ee052015-01-25 08:27:12 -0700368 plat->cs = cs;
Simon Goldschmidta942e1a2018-10-30 21:09:48 +0100369 if (speed) {
370 plat->max_hz = speed;
371 } else {
Patrick Delaunay1d83e032020-10-15 17:18:17 +0200372 dev_warn(bus,
373 "Warning: SPI speed fallback to %u kHz\n",
374 SPI_DEFAULT_SPEED_HZ / 1000);
Simon Goldschmidta942e1a2018-10-30 21:09:48 +0100375 plat->max_hz = SPI_DEFAULT_SPEED_HZ;
376 }
Simon Glass5d2ee052015-01-25 08:27:12 -0700377 plat->mode = mode;
Simon Glassdd82d442014-10-13 23:41:52 -0600378 created = true;
379 } else if (ret) {
Patrick Delaunay1d83e032020-10-15 17:18:17 +0200380 dev_err(bus, "Invalid chip select %d:%d (err=%d)\n", busnum, cs, ret);
Simon Glassdd82d442014-10-13 23:41:52 -0600381 return ret;
382 }
383
384 if (!device_active(dev)) {
Simon Glass5d2ee052015-01-25 08:27:12 -0700385 struct spi_slave *slave;
Simon Glassdd82d442014-10-13 23:41:52 -0600386
Simon Glass5d2ee052015-01-25 08:27:12 -0700387 ret = device_probe(dev);
Simon Glassdd82d442014-10-13 23:41:52 -0600388 if (ret)
389 goto err;
Simon Glassde44acf2015-09-28 23:32:01 -0600390 slave = dev_get_parent_priv(dev);
Simon Glassdd82d442014-10-13 23:41:52 -0600391 slave->dev = dev;
Simon Glassdd82d442014-10-13 23:41:52 -0600392 }
393
Marcin Wojtas70d8f982019-11-21 05:38:47 +0100394 slave = dev_get_parent_priv(dev);
Ovidiu Panait40dcee12020-12-14 19:06:50 +0200395 bus_data = dev_get_uclass_priv(bus);
Patrick Delaunayfa19c652019-02-27 15:36:44 +0100396
Marcin Wojtas70d8f982019-11-21 05:38:47 +0100397 /*
398 * In case the operation speed is not yet established by
399 * dm_spi_claim_bus() ensure the bus is configured properly.
400 */
Ovidiu Panait40dcee12020-12-14 19:06:50 +0200401 if (!bus_data->speed) {
Marcin Wojtas70d8f982019-11-21 05:38:47 +0100402 ret = spi_claim_bus(slave);
403 if (ret)
404 goto err;
Vignesh Rae569792016-07-06 10:04:28 +0530405 }
Simon Glassdd82d442014-10-13 23:41:52 -0600406
407 *busp = bus;
Marcin Wojtas70d8f982019-11-21 05:38:47 +0100408 *devp = slave;
Patrick Delaunay1d83e032020-10-15 17:18:17 +0200409 log_debug("%s: bus=%p, slave=%p\n", __func__, bus, *devp);
Simon Glassdd82d442014-10-13 23:41:52 -0600410
411 return 0;
412
413err:
Patrick Delaunay1d83e032020-10-15 17:18:17 +0200414 log_debug("%s: Error path, created=%d, device '%s'\n", __func__,
415 created, dev->name);
Simon Glassdd82d442014-10-13 23:41:52 -0600416 if (created) {
Stefan Roese80b5bc92017-03-20 12:51:48 +0100417 device_remove(dev, DM_REMOVE_NORMAL);
Simon Glassdd82d442014-10-13 23:41:52 -0600418 device_unbind(dev);
419 }
420
421 return ret;
422}
423
424/* Compatibility function - to be removed */
Simon Glassdd82d442014-10-13 23:41:52 -0600425struct spi_slave *spi_setup_slave(unsigned int busnum, unsigned int cs,
426 unsigned int speed, unsigned int mode)
427{
428 struct spi_slave *slave;
429 struct udevice *dev;
430 int ret;
431
432 ret = spi_get_bus_and_cs(busnum, cs, speed, mode, NULL, 0, &dev,
Simon Glass4b280582017-05-18 20:09:54 -0600433 &slave);
Simon Glassdd82d442014-10-13 23:41:52 -0600434 if (ret)
435 return NULL;
436
437 return slave;
438}
439
440void spi_free_slave(struct spi_slave *slave)
441{
Stefan Roese80b5bc92017-03-20 12:51:48 +0100442 device_remove(slave->dev, DM_REMOVE_NORMAL);
Simon Glassdd82d442014-10-13 23:41:52 -0600443}
444
Simon Glassb75b15b2020-12-03 16:55:23 -0700445int spi_slave_of_to_plat(struct udevice *dev, struct dm_spi_slave_plat *plat)
Simon Glassdd82d442014-10-13 23:41:52 -0600446{
Jagan Teki96536b12016-08-08 17:12:12 +0530447 int mode = 0;
Mugunthan V N4b0f40c2015-12-23 20:39:37 +0530448 int value;
Simon Glassdd82d442014-10-13 23:41:52 -0600449
Simon Glass4b280582017-05-18 20:09:54 -0600450 plat->cs = dev_read_u32_default(dev, "reg", -1);
Simon Goldschmidta942e1a2018-10-30 21:09:48 +0100451 plat->max_hz = dev_read_u32_default(dev, "spi-max-frequency",
452 SPI_DEFAULT_SPEED_HZ);
Simon Glass4b280582017-05-18 20:09:54 -0600453 if (dev_read_bool(dev, "spi-cpol"))
Simon Glassdd82d442014-10-13 23:41:52 -0600454 mode |= SPI_CPOL;
Simon Glass4b280582017-05-18 20:09:54 -0600455 if (dev_read_bool(dev, "spi-cpha"))
Simon Glassdd82d442014-10-13 23:41:52 -0600456 mode |= SPI_CPHA;
Simon Glass4b280582017-05-18 20:09:54 -0600457 if (dev_read_bool(dev, "spi-cs-high"))
Simon Glassdd82d442014-10-13 23:41:52 -0600458 mode |= SPI_CS_HIGH;
Simon Glass4b280582017-05-18 20:09:54 -0600459 if (dev_read_bool(dev, "spi-3wire"))
Jagan Tekid3868fd2015-12-03 22:19:05 +0530460 mode |= SPI_3WIRE;
Simon Glass4b280582017-05-18 20:09:54 -0600461 if (dev_read_bool(dev, "spi-half-duplex"))
Simon Glassdd82d442014-10-13 23:41:52 -0600462 mode |= SPI_PREAMBLE;
Mugunthan V N4b0f40c2015-12-23 20:39:37 +0530463
464 /* Device DUAL/QUAD mode */
Simon Glass4b280582017-05-18 20:09:54 -0600465 value = dev_read_u32_default(dev, "spi-tx-bus-width", 1);
Mugunthan V N4b0f40c2015-12-23 20:39:37 +0530466 switch (value) {
467 case 1:
468 break;
469 case 2:
470 mode |= SPI_TX_DUAL;
471 break;
472 case 4:
473 mode |= SPI_TX_QUAD;
474 break;
Vignesh Raghavendrac063ee32019-12-05 15:46:05 +0530475 case 8:
476 mode |= SPI_TX_OCTAL;
477 break;
Mugunthan V N4b0f40c2015-12-23 20:39:37 +0530478 default:
Simon Glass6f5bed02016-11-29 20:00:13 -0700479 warn_non_spl("spi-tx-bus-width %d not supported\n", value);
Mugunthan V N4b0f40c2015-12-23 20:39:37 +0530480 break;
481 }
482
Simon Glass4b280582017-05-18 20:09:54 -0600483 value = dev_read_u32_default(dev, "spi-rx-bus-width", 1);
Mugunthan V N4b0f40c2015-12-23 20:39:37 +0530484 switch (value) {
485 case 1:
486 break;
487 case 2:
Jagan Teki96536b12016-08-08 17:12:12 +0530488 mode |= SPI_RX_DUAL;
Mugunthan V N4b0f40c2015-12-23 20:39:37 +0530489 break;
490 case 4:
Jagan Teki96536b12016-08-08 17:12:12 +0530491 mode |= SPI_RX_QUAD;
Mugunthan V N4b0f40c2015-12-23 20:39:37 +0530492 break;
Vignesh Raghavendrac063ee32019-12-05 15:46:05 +0530493 case 8:
494 mode |= SPI_RX_OCTAL;
495 break;
Mugunthan V N4b0f40c2015-12-23 20:39:37 +0530496 default:
Simon Glass6f5bed02016-11-29 20:00:13 -0700497 warn_non_spl("spi-rx-bus-width %d not supported\n", value);
Mugunthan V N4b0f40c2015-12-23 20:39:37 +0530498 break;
499 }
500
Jagan Teki96536b12016-08-08 17:12:12 +0530501 plat->mode = mode;
Mugunthan V N4b0f40c2015-12-23 20:39:37 +0530502
Simon Glassdd82d442014-10-13 23:41:52 -0600503 return 0;
504}
505
506UCLASS_DRIVER(spi) = {
507 .id = UCLASS_SPI,
508 .name = "spi",
Simon Glass0ccb0972015-01-25 08:27:05 -0700509 .flags = DM_UC_FLAG_SEQ_ALIAS,
Faiz Abbas8598c342020-09-14 12:11:14 +0530510#if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
Simon Glass18230342016-07-05 17:10:10 -0600511 .post_bind = dm_scan_fdt_dev,
Simon Glass3fb33392016-11-13 14:22:01 -0700512#endif
Simon Glassdd82d442014-10-13 23:41:52 -0600513 .post_probe = spi_post_probe,
Simon Glass82c2f502015-01-25 08:27:11 -0700514 .child_pre_probe = spi_child_pre_probe,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700515 .per_device_auto = sizeof(struct dm_spi_bus),
516 .per_child_auto = sizeof(struct spi_slave),
Simon Glassb75b15b2020-12-03 16:55:23 -0700517 .per_child_plat_auto = sizeof(struct dm_spi_slave_plat),
Simon Glass3fb33392016-11-13 14:22:01 -0700518#if !CONFIG_IS_ENABLED(OF_PLATDATA)
Simon Glass5d2ee052015-01-25 08:27:12 -0700519 .child_post_bind = spi_child_post_bind,
Simon Glass3fb33392016-11-13 14:22:01 -0700520#endif
Simon Glassdd82d442014-10-13 23:41:52 -0600521};
522
523UCLASS_DRIVER(spi_generic) = {
524 .id = UCLASS_SPI_GENERIC,
525 .name = "spi_generic",
526};
527
528U_BOOT_DRIVER(spi_generic_drv) = {
529 .name = "spi_generic_drv",
530 .id = UCLASS_SPI_GENERIC,
531};