blob: ba325cf4e0d91340d48755c58047fc5d1e338bc2 [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
6#include <common.h>
7#include <dm.h>
8#include <errno.h>
Simon Glass0f2af882020-05-10 11:40:05 -06009#include <log.h>
Simon Glassdd82d442014-10-13 23:41:52 -060010#include <malloc.h>
11#include <spi.h>
12#include <dm/device-internal.h>
13#include <dm/uclass-internal.h>
Simon Glassdd82d442014-10-13 23:41:52 -060014#include <dm/lists.h>
15#include <dm/util.h>
16
17DECLARE_GLOBAL_DATA_PTR;
18
Simon Goldschmidta942e1a2018-10-30 21:09:48 +010019#define SPI_DEFAULT_SPEED_HZ 100000
20
Simon Glassdd82d442014-10-13 23:41:52 -060021static int spi_set_speed_mode(struct udevice *bus, int speed, int mode)
22{
23 struct dm_spi_ops *ops;
24 int ret;
25
26 ops = spi_get_ops(bus);
27 if (ops->set_speed)
28 ret = ops->set_speed(bus, speed);
29 else
30 ret = -EINVAL;
31 if (ret) {
32 printf("Cannot set speed (err=%d)\n", ret);
33 return ret;
34 }
35
36 if (ops->set_mode)
37 ret = ops->set_mode(bus, mode);
38 else
39 ret = -EINVAL;
40 if (ret) {
41 printf("Cannot set mode (err=%d)\n", ret);
42 return ret;
43 }
44
45 return 0;
46}
47
Peng Fanfdd88a32016-05-03 10:02:22 +080048int dm_spi_claim_bus(struct udevice *dev)
Simon Glassdd82d442014-10-13 23:41:52 -060049{
Simon Glassdd82d442014-10-13 23:41:52 -060050 struct udevice *bus = dev->parent;
51 struct dm_spi_ops *ops = spi_get_ops(bus);
Simon Glassde0977b2015-03-05 12:25:20 -070052 struct dm_spi_bus *spi = dev_get_uclass_priv(bus);
Peng Fanfdd88a32016-05-03 10:02:22 +080053 struct spi_slave *slave = dev_get_parent_priv(dev);
Ovidiu Panait40dcee12020-12-14 19:06:50 +020054 uint speed, mode;
Simon Glassdd82d442014-10-13 23:41:52 -060055
56 speed = slave->max_hz;
Ovidiu Panait40dcee12020-12-14 19:06:50 +020057 mode = slave->mode;
58
Simon Glassdd82d442014-10-13 23:41:52 -060059 if (spi->max_hz) {
60 if (speed)
Ovidiu Panait40dcee12020-12-14 19:06:50 +020061 speed = min(speed, spi->max_hz);
Simon Glassdd82d442014-10-13 23:41:52 -060062 else
63 speed = spi->max_hz;
64 }
65 if (!speed)
Simon Goldschmidta942e1a2018-10-30 21:09:48 +010066 speed = SPI_DEFAULT_SPEED_HZ;
Ovidiu Panait40dcee12020-12-14 19:06:50 +020067
68 if (speed != spi->speed || mode != spi->mode) {
Mario Six2d844dd2018-01-15 11:08:41 +010069 int ret = spi_set_speed_mode(bus, speed, slave->mode);
70
Simon Glassb46cb632015-02-17 15:29:35 -070071 if (ret)
Simon Glass00d99902018-10-01 12:22:24 -060072 return log_ret(ret);
Ovidiu Panait40dcee12020-12-14 19:06:50 +020073
74 spi->speed = speed;
75 spi->mode = mode;
Simon Glassb46cb632015-02-17 15:29:35 -070076 }
Simon Glassdd82d442014-10-13 23:41:52 -060077
Simon Glass00d99902018-10-01 12:22:24 -060078 return log_ret(ops->claim_bus ? ops->claim_bus(dev) : 0);
Simon Glassdd82d442014-10-13 23:41:52 -060079}
80
Peng Fanfdd88a32016-05-03 10:02:22 +080081void dm_spi_release_bus(struct udevice *dev)
Simon Glassdd82d442014-10-13 23:41:52 -060082{
Simon Glassdd82d442014-10-13 23:41:52 -060083 struct udevice *bus = dev->parent;
84 struct dm_spi_ops *ops = spi_get_ops(bus);
85
86 if (ops->release_bus)
Simon Glass5c74fba2015-04-19 09:05:40 -060087 ops->release_bus(dev);
Simon Glassdd82d442014-10-13 23:41:52 -060088}
89
Peng Fanfdd88a32016-05-03 10:02:22 +080090int dm_spi_xfer(struct udevice *dev, unsigned int bitlen,
91 const void *dout, void *din, unsigned long flags)
Simon Glassdd82d442014-10-13 23:41:52 -060092{
Simon Glassdd82d442014-10-13 23:41:52 -060093 struct udevice *bus = dev->parent;
Simon Glass2d2e8602019-12-06 21:42:35 -070094 struct dm_spi_ops *ops = spi_get_ops(bus);
Simon Glassdd82d442014-10-13 23:41:52 -060095
96 if (bus->uclass->uc_drv->id != UCLASS_SPI)
97 return -EOPNOTSUPP;
Simon Glass2d2e8602019-12-06 21:42:35 -070098 if (!ops->xfer)
99 return -ENOSYS;
Simon Glassdd82d442014-10-13 23:41:52 -0600100
Simon Glass2d2e8602019-12-06 21:42:35 -0700101 return ops->xfer(dev, bitlen, dout, din, flags);
Simon Glassdd82d442014-10-13 23:41:52 -0600102}
103
Simon Glass37ad0fe2019-10-20 21:31:47 -0600104int dm_spi_get_mmap(struct udevice *dev, ulong *map_basep, uint *map_sizep,
105 uint *offsetp)
106{
107 struct udevice *bus = dev->parent;
108 struct dm_spi_ops *ops = spi_get_ops(bus);
109
110 if (bus->uclass->uc_drv->id != UCLASS_SPI)
111 return -EOPNOTSUPP;
112 if (!ops->get_mmap)
113 return -ENOSYS;
114
115 return ops->get_mmap(dev, map_basep, map_sizep, offsetp);
116}
117
Peng Fanfdd88a32016-05-03 10:02:22 +0800118int spi_claim_bus(struct spi_slave *slave)
119{
Simon Glass00d99902018-10-01 12:22:24 -0600120 return log_ret(dm_spi_claim_bus(slave->dev));
Peng Fanfdd88a32016-05-03 10:02:22 +0800121}
122
123void spi_release_bus(struct spi_slave *slave)
124{
125 dm_spi_release_bus(slave->dev);
126}
127
128int spi_xfer(struct spi_slave *slave, unsigned int bitlen,
129 const void *dout, void *din, unsigned long flags)
130{
131 return dm_spi_xfer(slave->dev, bitlen, dout, din, flags);
132}
133
Jagan Teki7cc71fd2019-07-22 17:22:56 +0530134int spi_write_then_read(struct spi_slave *slave, const u8 *opcode,
135 size_t n_opcode, const u8 *txbuf, u8 *rxbuf,
136 size_t n_buf)
137{
138 unsigned long flags = SPI_XFER_BEGIN;
139 int ret;
140
141 if (n_buf == 0)
142 flags |= SPI_XFER_END;
143
144 ret = spi_xfer(slave, n_opcode * 8, opcode, NULL, flags);
145 if (ret) {
146 debug("spi: failed to send command (%zu bytes): %d\n",
147 n_opcode, ret);
148 } else if (n_buf != 0) {
149 ret = spi_xfer(slave, n_buf * 8, txbuf, rxbuf, SPI_XFER_END);
150 if (ret)
151 debug("spi: failed to transfer %zu bytes of data: %d\n",
152 n_buf, ret);
153 }
154
155 return ret;
156}
157
Simon Glass3fb33392016-11-13 14:22:01 -0700158#if !CONFIG_IS_ENABLED(OF_PLATDATA)
Simon Glassd941aaf2015-06-23 15:39:05 -0600159static int spi_child_post_bind(struct udevice *dev)
Simon Glassdd82d442014-10-13 23:41:52 -0600160{
Simon Glassb75b15b2020-12-03 16:55:23 -0700161 struct dm_spi_slave_plat *plat = dev_get_parent_plat(dev);
Simon Glassdd82d442014-10-13 23:41:52 -0600162
Simon Glassf1d50f72020-12-19 10:40:13 -0700163 if (!dev_has_ofnode(dev))
Simon Glass5d2ee052015-01-25 08:27:12 -0700164 return 0;
165
Simon Glassaad29ae2020-12-03 16:55:21 -0700166 return spi_slave_of_to_plat(dev, plat);
Simon Glass5d2ee052015-01-25 08:27:12 -0700167}
Simon Glass3fb33392016-11-13 14:22:01 -0700168#endif
Simon Glass5d2ee052015-01-25 08:27:12 -0700169
Simon Glassd941aaf2015-06-23 15:39:05 -0600170static int spi_post_probe(struct udevice *bus)
Simon Glass5d2ee052015-01-25 08:27:12 -0700171{
Simon Glass3fb33392016-11-13 14:22:01 -0700172#if !CONFIG_IS_ENABLED(OF_PLATDATA)
Simon Glassde0977b2015-03-05 12:25:20 -0700173 struct dm_spi_bus *spi = dev_get_uclass_priv(bus);
Simon Glass5d2ee052015-01-25 08:27:12 -0700174
Simon Glass4b280582017-05-18 20:09:54 -0600175 spi->max_hz = dev_read_u32_default(bus, "spi-max-frequency", 0);
Simon Glass3fb33392016-11-13 14:22:01 -0700176#endif
Michal Simek28d7e4e2015-10-27 13:36:42 +0100177#if defined(CONFIG_NEEDS_MANUAL_RELOC)
178 struct dm_spi_ops *ops = spi_get_ops(bus);
Ashok Reddy Soma1d89d8c2019-09-17 00:11:02 -0600179 static int reloc_done;
Michal Simek28d7e4e2015-10-27 13:36:42 +0100180
Ashok Reddy Soma1d89d8c2019-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 Simek28d7e4e2015-10-27 13:36:42 +0100198#endif
199
Simon Glassdd82d442014-10-13 23:41:52 -0600200 return 0;
201}
202
Simon Glassd941aaf2015-06-23 15:39:05 -0600203static int spi_child_pre_probe(struct udevice *dev)
Simon Glass82c2f502015-01-25 08:27:11 -0700204{
Simon Glassb75b15b2020-12-03 16:55:23 -0700205 struct dm_spi_slave_plat *plat = dev_get_parent_plat(dev);
Simon Glassde44acf2015-09-28 23:32:01 -0600206 struct spi_slave *slave = dev_get_parent_priv(dev);
Simon Glass82c2f502015-01-25 08:27:11 -0700207
Simon Glass5d2ee052015-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 Glass82c2f502015-01-25 08:27:11 -0700215 slave->dev = dev;
216
Simon Glass5d2ee052015-01-25 08:27:12 -0700217 slave->max_hz = plat->max_hz;
218 slave->mode = plat->mode;
Christophe Ricardfb0c53e2016-01-17 11:56:48 +0100219 slave->wordlen = SPI_DEFAULT_WORDLEN;
Simon Glass5d2ee052015-01-25 08:27:12 -0700220
Simon Glass82c2f502015-01-25 08:27:11 -0700221 return 0;
222}
223
Simon Glassdd82d442014-10-13 23:41:52 -0600224int spi_chip_select(struct udevice *dev)
225{
Simon Glassb75b15b2020-12-03 16:55:23 -0700226 struct dm_spi_slave_plat *plat = dev_get_parent_plat(dev);
Simon Glassdd82d442014-10-13 23:41:52 -0600227
Simon Glass5d2ee052015-01-25 08:27:12 -0700228 return plat ? plat->cs : -ENOENT;
Simon Glassdd82d442014-10-13 23:41:52 -0600229}
230
Simon Glass5ef36f22014-11-11 10:46:22 -0700231int spi_find_chip_select(struct udevice *bus, int cs, struct udevice **devp)
Simon Glassdd82d442014-10-13 23:41:52 -0600232{
Bin Meng9741e732019-09-09 06:00:02 -0700233 struct dm_spi_ops *ops;
234 struct spi_cs_info info;
Simon Glassdd82d442014-10-13 23:41:52 -0600235 struct udevice *dev;
Bin Meng9741e732019-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) {
256 printf("Invalid cs %d (err=%d)\n", cs, ret);
257 return ret;
258 }
Simon Glassdd82d442014-10-13 23:41:52 -0600259
260 for (device_find_first_child(bus, &dev); dev;
261 device_find_next_child(&dev)) {
Simon Glassb75b15b2020-12-03 16:55:23 -0700262 struct dm_spi_slave_plat *plat;
Simon Glassdd82d442014-10-13 23:41:52 -0600263
Simon Glass71fa5b42020-12-03 16:55:18 -0700264 plat = dev_get_parent_plat(dev);
Simon Glass5d2ee052015-01-25 08:27:12 -0700265 debug("%s: plat=%p, cs=%d\n", __func__, plat, plat->cs);
266 if (plat->cs == cs) {
Simon Glassdd82d442014-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
Simon Glass07e13382020-12-16 21:20:29 -0700281 ret = uclass_find_device_by_seq(UCLASS_SPI, busnum, &bus);
Simon Glassdd82d442014-10-13 23:41:52 -0600282 if (ret) {
283 debug("%s: No bus %d\n", __func__, busnum);
284 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 Glassdd82d442014-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 Meng9741e732019-09-09 06:00:02 -0700301 return ret == -ENODEV ? 0 : ret;
Simon Glassdd82d442014-10-13 23:41:52 -0600302}
303
Simon Glassdd82d442014-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
Simon Glass07e13382020-12-16 21:20:29 -0700310 ret = uclass_find_device_by_seq(UCLASS_SPI, busnum, &bus);
Simon Glassdd82d442014-10-13 23:41:52 -0600311 if (ret) {
312 debug("%s: No bus %d\n", __func__, busnum);
313 return ret;
314 }
315 ret = spi_find_chip_select(bus, cs, &dev);
316 if (ret) {
317 debug("%s: No cs %d\n", __func__, cs);
318 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;
Simon Glassb75b15b2020-12-03 16:55:23 -0700331 struct dm_spi_slave_plat *plat;
Ovidiu Panait40dcee12020-12-14 19:06:50 +0200332 struct dm_spi_bus *bus_data;
Marcin Wojtas70d8f982019-11-21 05:38:47 +0100333 struct spi_slave *slave;
Simon Glassdd82d442014-10-13 23:41:52 -0600334 bool created = false;
335 int ret;
336
Thomas Fitzsimmons59c90f32019-09-06 07:51:19 -0400337#if CONFIG_IS_ENABLED(OF_PLATDATA)
Simon Glass3fb33392016-11-13 14:22:01 -0700338 ret = uclass_first_device_err(UCLASS_SPI, &bus);
339#else
Simon Glassdd82d442014-10-13 23:41:52 -0600340 ret = uclass_get_device_by_seq(UCLASS_SPI, busnum, &bus);
Simon Glass3fb33392016-11-13 14:22:01 -0700341#endif
Simon Glassdd82d442014-10-13 23:41:52 -0600342 if (ret) {
343 printf("Invalid bus %d (err=%d)\n", busnum, ret);
344 return ret;
345 }
346 ret = spi_find_chip_select(bus, cs, &dev);
347
348 /*
349 * If there is no such device, create one automatically. This means
350 * that we don't need a device tree node or platform data for the
351 * SPI flash chip - we will bind to the correct driver.
352 */
353 if (ret == -ENODEV && drv_name) {
354 debug("%s: Binding new device '%s', busnum=%d, cs=%d, driver=%s\n",
355 __func__, dev_name, busnum, cs, drv_name);
Simon Glassd8a21f62014-11-11 10:46:23 -0700356 ret = device_bind_driver(bus, drv_name, dev_name, &dev);
Simon Glassf0307e12016-11-13 14:22:05 -0700357 if (ret) {
358 debug("%s: Unable to bind driver (ret=%d)\n", __func__,
359 ret);
Simon Glassdd82d442014-10-13 23:41:52 -0600360 return ret;
Simon Glassf0307e12016-11-13 14:22:05 -0700361 }
Simon Glass71fa5b42020-12-03 16:55:18 -0700362 plat = dev_get_parent_plat(dev);
Simon Glass5d2ee052015-01-25 08:27:12 -0700363 plat->cs = cs;
Simon Goldschmidta942e1a2018-10-30 21:09:48 +0100364 if (speed) {
365 plat->max_hz = speed;
366 } else {
367 printf("Warning: SPI speed fallback to %u kHz\n",
368 SPI_DEFAULT_SPEED_HZ / 1000);
369 plat->max_hz = SPI_DEFAULT_SPEED_HZ;
370 }
Simon Glass5d2ee052015-01-25 08:27:12 -0700371 plat->mode = mode;
Simon Glassdd82d442014-10-13 23:41:52 -0600372 created = true;
373 } else if (ret) {
374 printf("Invalid chip select %d:%d (err=%d)\n", busnum, cs,
375 ret);
376 return ret;
377 }
378
379 if (!device_active(dev)) {
Simon Glass5d2ee052015-01-25 08:27:12 -0700380 struct spi_slave *slave;
Simon Glassdd82d442014-10-13 23:41:52 -0600381
Simon Glass5d2ee052015-01-25 08:27:12 -0700382 ret = device_probe(dev);
Simon Glassdd82d442014-10-13 23:41:52 -0600383 if (ret)
384 goto err;
Simon Glassde44acf2015-09-28 23:32:01 -0600385 slave = dev_get_parent_priv(dev);
Simon Glassdd82d442014-10-13 23:41:52 -0600386 slave->dev = dev;
Simon Glassdd82d442014-10-13 23:41:52 -0600387 }
388
Marcin Wojtas70d8f982019-11-21 05:38:47 +0100389 slave = dev_get_parent_priv(dev);
Ovidiu Panait40dcee12020-12-14 19:06:50 +0200390 bus_data = dev_get_uclass_priv(bus);
Patrick Delaunayfa19c652019-02-27 15:36:44 +0100391
Marcin Wojtas70d8f982019-11-21 05:38:47 +0100392 /*
393 * In case the operation speed is not yet established by
394 * dm_spi_claim_bus() ensure the bus is configured properly.
395 */
Ovidiu Panait40dcee12020-12-14 19:06:50 +0200396 if (!bus_data->speed) {
Marcin Wojtas70d8f982019-11-21 05:38:47 +0100397 ret = spi_claim_bus(slave);
398 if (ret)
399 goto err;
Vignesh Rae569792016-07-06 10:04:28 +0530400 }
Simon Glassdd82d442014-10-13 23:41:52 -0600401
402 *busp = bus;
Marcin Wojtas70d8f982019-11-21 05:38:47 +0100403 *devp = slave;
Simon Glassdd82d442014-10-13 23:41:52 -0600404 debug("%s: bus=%p, slave=%p\n", __func__, bus, *devp);
405
406 return 0;
407
408err:
Anatolij Gustschin264359e2016-04-21 09:28:02 +0200409 debug("%s: Error path, created=%d, device '%s'\n", __func__,
Simon Glass5d2ee052015-01-25 08:27:12 -0700410 created, dev->name);
Simon Glassdd82d442014-10-13 23:41:52 -0600411 if (created) {
Stefan Roese80b5bc92017-03-20 12:51:48 +0100412 device_remove(dev, DM_REMOVE_NORMAL);
Simon Glassdd82d442014-10-13 23:41:52 -0600413 device_unbind(dev);
414 }
415
416 return ret;
417}
418
419/* Compatibility function - to be removed */
Simon Glassdd82d442014-10-13 23:41:52 -0600420struct spi_slave *spi_setup_slave(unsigned int busnum, unsigned int cs,
421 unsigned int speed, unsigned int mode)
422{
423 struct spi_slave *slave;
424 struct udevice *dev;
425 int ret;
426
427 ret = spi_get_bus_and_cs(busnum, cs, speed, mode, NULL, 0, &dev,
Simon Glass4b280582017-05-18 20:09:54 -0600428 &slave);
Simon Glassdd82d442014-10-13 23:41:52 -0600429 if (ret)
430 return NULL;
431
432 return slave;
433}
434
435void spi_free_slave(struct spi_slave *slave)
436{
Stefan Roese80b5bc92017-03-20 12:51:48 +0100437 device_remove(slave->dev, DM_REMOVE_NORMAL);
Simon Glassdd82d442014-10-13 23:41:52 -0600438}
439
Simon Glassb75b15b2020-12-03 16:55:23 -0700440int spi_slave_of_to_plat(struct udevice *dev, struct dm_spi_slave_plat *plat)
Simon Glassdd82d442014-10-13 23:41:52 -0600441{
Jagan Teki96536b12016-08-08 17:12:12 +0530442 int mode = 0;
Mugunthan V N4b0f40c2015-12-23 20:39:37 +0530443 int value;
Simon Glassdd82d442014-10-13 23:41:52 -0600444
Simon Glass4b280582017-05-18 20:09:54 -0600445 plat->cs = dev_read_u32_default(dev, "reg", -1);
Simon Goldschmidta942e1a2018-10-30 21:09:48 +0100446 plat->max_hz = dev_read_u32_default(dev, "spi-max-frequency",
447 SPI_DEFAULT_SPEED_HZ);
Simon Glass4b280582017-05-18 20:09:54 -0600448 if (dev_read_bool(dev, "spi-cpol"))
Simon Glassdd82d442014-10-13 23:41:52 -0600449 mode |= SPI_CPOL;
Simon Glass4b280582017-05-18 20:09:54 -0600450 if (dev_read_bool(dev, "spi-cpha"))
Simon Glassdd82d442014-10-13 23:41:52 -0600451 mode |= SPI_CPHA;
Simon Glass4b280582017-05-18 20:09:54 -0600452 if (dev_read_bool(dev, "spi-cs-high"))
Simon Glassdd82d442014-10-13 23:41:52 -0600453 mode |= SPI_CS_HIGH;
Simon Glass4b280582017-05-18 20:09:54 -0600454 if (dev_read_bool(dev, "spi-3wire"))
Jagan Tekid3868fd2015-12-03 22:19:05 +0530455 mode |= SPI_3WIRE;
Simon Glass4b280582017-05-18 20:09:54 -0600456 if (dev_read_bool(dev, "spi-half-duplex"))
Simon Glassdd82d442014-10-13 23:41:52 -0600457 mode |= SPI_PREAMBLE;
Mugunthan V N4b0f40c2015-12-23 20:39:37 +0530458
459 /* Device DUAL/QUAD mode */
Simon Glass4b280582017-05-18 20:09:54 -0600460 value = dev_read_u32_default(dev, "spi-tx-bus-width", 1);
Mugunthan V N4b0f40c2015-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 Raghavendrac063ee32019-12-05 15:46:05 +0530470 case 8:
471 mode |= SPI_TX_OCTAL;
472 break;
Mugunthan V N4b0f40c2015-12-23 20:39:37 +0530473 default:
Simon Glass6f5bed02016-11-29 20:00:13 -0700474 warn_non_spl("spi-tx-bus-width %d not supported\n", value);
Mugunthan V N4b0f40c2015-12-23 20:39:37 +0530475 break;
476 }
477
Simon Glass4b280582017-05-18 20:09:54 -0600478 value = dev_read_u32_default(dev, "spi-rx-bus-width", 1);
Mugunthan V N4b0f40c2015-12-23 20:39:37 +0530479 switch (value) {
480 case 1:
481 break;
482 case 2:
Jagan Teki96536b12016-08-08 17:12:12 +0530483 mode |= SPI_RX_DUAL;
Mugunthan V N4b0f40c2015-12-23 20:39:37 +0530484 break;
485 case 4:
Jagan Teki96536b12016-08-08 17:12:12 +0530486 mode |= SPI_RX_QUAD;
Mugunthan V N4b0f40c2015-12-23 20:39:37 +0530487 break;
Vignesh Raghavendrac063ee32019-12-05 15:46:05 +0530488 case 8:
489 mode |= SPI_RX_OCTAL;
490 break;
Mugunthan V N4b0f40c2015-12-23 20:39:37 +0530491 default:
Simon Glass6f5bed02016-11-29 20:00:13 -0700492 warn_non_spl("spi-rx-bus-width %d not supported\n", value);
Mugunthan V N4b0f40c2015-12-23 20:39:37 +0530493 break;
494 }
495
Jagan Teki96536b12016-08-08 17:12:12 +0530496 plat->mode = mode;
Mugunthan V N4b0f40c2015-12-23 20:39:37 +0530497
Simon Glassdd82d442014-10-13 23:41:52 -0600498 return 0;
499}
500
501UCLASS_DRIVER(spi) = {
502 .id = UCLASS_SPI,
503 .name = "spi",
Simon Glass0ccb0972015-01-25 08:27:05 -0700504 .flags = DM_UC_FLAG_SEQ_ALIAS,
Faiz Abbas8598c342020-09-14 12:11:14 +0530505#if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
Simon Glass18230342016-07-05 17:10:10 -0600506 .post_bind = dm_scan_fdt_dev,
Simon Glass3fb33392016-11-13 14:22:01 -0700507#endif
Simon Glassdd82d442014-10-13 23:41:52 -0600508 .post_probe = spi_post_probe,
Simon Glass82c2f502015-01-25 08:27:11 -0700509 .child_pre_probe = spi_child_pre_probe,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700510 .per_device_auto = sizeof(struct dm_spi_bus),
511 .per_child_auto = sizeof(struct spi_slave),
Simon Glassb75b15b2020-12-03 16:55:23 -0700512 .per_child_plat_auto = sizeof(struct dm_spi_slave_plat),
Simon Glass3fb33392016-11-13 14:22:01 -0700513#if !CONFIG_IS_ENABLED(OF_PLATDATA)
Simon Glass5d2ee052015-01-25 08:27:12 -0700514 .child_post_bind = spi_child_post_bind,
Simon Glass3fb33392016-11-13 14:22:01 -0700515#endif
Simon Glassdd82d442014-10-13 23:41:52 -0600516};
517
518UCLASS_DRIVER(spi_generic) = {
519 .id = UCLASS_SPI_GENERIC,
520 .name = "spi_generic",
521};
522
523U_BOOT_DRIVER(spi_generic_drv) = {
524 .name = "spi_generic_drv",
525 .id = UCLASS_SPI_GENERIC,
526};