1
2
3
4
5
6#define LOG_CATEGORY UCLASS_SPI
7
8#include <common.h>
9#include <dm.h>
10#include <errno.h>
11#include <log.h>
12#include <malloc.h>
13#include <spi.h>
14#include <dm/device_compat.h>
15#include <dm/device-internal.h>
16#include <dm/uclass-internal.h>
17#include <dm/lists.h>
18#include <dm/util.h>
19
20DECLARE_GLOBAL_DATA_PTR;
21
22#define SPI_DEFAULT_SPEED_HZ 100000
23
24static 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) {
35 dev_err(bus, "Cannot set speed (err=%d)\n", ret);
36 return ret;
37 }
38
39 if (ops->set_mode)
40 ret = ops->set_mode(bus, mode);
41 else
42 ret = -EINVAL;
43 if (ret) {
44 dev_err(bus, "Cannot set mode (err=%d)\n", ret);
45 return ret;
46 }
47
48 return 0;
49}
50
51int dm_spi_claim_bus(struct udevice *dev)
52{
53 struct udevice *bus = dev->parent;
54 struct dm_spi_ops *ops = spi_get_ops(bus);
55 struct dm_spi_bus *spi = dev_get_uclass_priv(bus);
56 struct spi_slave *slave = dev_get_parent_priv(dev);
57 int speed;
58
59 speed = slave->max_hz;
60 if (spi->max_hz) {
61 if (speed)
62 speed = min(speed, (int)spi->max_hz);
63 else
64 speed = spi->max_hz;
65 }
66 if (!speed)
67 speed = SPI_DEFAULT_SPEED_HZ;
68 if (speed != slave->speed) {
69 int ret = spi_set_speed_mode(bus, speed, slave->mode);
70
71 if (ret)
72 return log_ret(ret);
73 slave->speed = speed;
74 }
75
76 return log_ret(ops->claim_bus ? ops->claim_bus(dev) : 0);
77}
78
79void dm_spi_release_bus(struct udevice *dev)
80{
81 struct udevice *bus = dev->parent;
82 struct dm_spi_ops *ops = spi_get_ops(bus);
83
84 if (ops->release_bus)
85 ops->release_bus(dev);
86}
87
88int dm_spi_xfer(struct udevice *dev, unsigned int bitlen,
89 const void *dout, void *din, unsigned long flags)
90{
91 struct udevice *bus = dev->parent;
92 struct dm_spi_ops *ops = spi_get_ops(bus);
93
94 if (bus->uclass->uc_drv->id != UCLASS_SPI)
95 return -EOPNOTSUPP;
96 if (!ops->xfer)
97 return -ENOSYS;
98
99 return ops->xfer(dev, bitlen, dout, din, flags);
100}
101
102int 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
116int spi_claim_bus(struct spi_slave *slave)
117{
118 return log_ret(dm_spi_claim_bus(slave->dev));
119}
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
132int 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) {
144 dev_dbg(slave->dev,
145 "spi: failed to send command (%zu bytes): %d\n",
146 n_opcode, ret);
147 } else if (n_buf != 0) {
148 ret = spi_xfer(slave, n_buf * 8, txbuf, rxbuf, SPI_XFER_END);
149 if (ret)
150 dev_dbg(slave->dev,
151 "spi: failed to transfer %zu bytes of data: %d\n",
152 n_buf, ret);
153 }
154
155 return ret;
156}
157
158#if !CONFIG_IS_ENABLED(OF_PLATDATA)
159static int spi_child_post_bind(struct udevice *dev)
160{
161 struct dm_spi_slave_platdata *plat = dev_get_parent_platdata(dev);
162
163 if (!dev_of_valid(dev))
164 return 0;
165
166 return spi_slave_ofdata_to_platdata(dev, plat);
167}
168#endif
169
170static int spi_post_probe(struct udevice *bus)
171{
172#if !CONFIG_IS_ENABLED(OF_PLATDATA)
173 struct dm_spi_bus *spi = dev_get_uclass_priv(bus);
174
175 spi->max_hz = dev_read_u32_default(bus, "spi-max-frequency", 0);
176#endif
177#if defined(CONFIG_NEEDS_MANUAL_RELOC)
178 struct dm_spi_ops *ops = spi_get_ops(bus);
179 static int reloc_done;
180
181 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 }
198#endif
199
200 return 0;
201}
202
203static int spi_child_pre_probe(struct udevice *dev)
204{
205 struct dm_spi_slave_platdata *plat = dev_get_parent_platdata(dev);
206 struct spi_slave *slave = dev_get_parent_priv(dev);
207
208
209
210
211
212
213
214
215 slave->dev = dev;
216
217 slave->max_hz = plat->max_hz;
218 slave->mode = plat->mode;
219 slave->wordlen = SPI_DEFAULT_WORDLEN;
220
221 return 0;
222}
223
224int spi_chip_select(struct udevice *dev)
225{
226 struct dm_spi_slave_platdata *plat = dev_get_parent_platdata(dev);
227
228 return plat ? plat->cs : -ENOENT;
229}
230
231int spi_find_chip_select(struct udevice *bus, int cs, struct udevice **devp)
232{
233 struct dm_spi_ops *ops;
234 struct spi_cs_info info;
235 struct udevice *dev;
236 int ret;
237
238
239
240
241
242
243
244 ops = spi_get_ops(bus);
245 if (ops->cs_info) {
246 ret = ops->cs_info(bus, cs, &info);
247 } else {
248
249
250
251
252 ret = 0;
253 }
254
255 if (ret) {
256 dev_err(bus, "Invalid cs %d (err=%d)\n", cs, ret);
257 return ret;
258 }
259
260 for (device_find_first_child(bus, &dev); dev;
261 device_find_next_child(&dev)) {
262 struct dm_spi_slave_platdata *plat;
263
264 plat = dev_get_parent_platdata(dev);
265 dev_dbg(bus, "%s: plat=%p, cs=%d\n", __func__, plat, plat->cs);
266 if (plat->cs == cs) {
267 *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) {
283 log_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;
293 int ret;
294
295 if (!info)
296 info = &local_info;
297
298
299 info->dev = NULL;
300 ret = spi_find_chip_select(bus, cs, &info->dev);
301 return ret == -ENODEV ? 0 : ret;
302}
303
304int 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) {
312 log_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 dev_dbg(bus, "%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;
331 struct dm_spi_slave_platdata *plat;
332 struct spi_slave *slave;
333 bool created = false;
334 int ret;
335
336#if CONFIG_IS_ENABLED(OF_PLATDATA)
337 ret = uclass_first_device_err(UCLASS_SPI, &bus);
338#else
339 ret = uclass_get_device_by_seq(UCLASS_SPI, busnum, &bus);
340#endif
341 if (ret) {
342 log_err("Invalid bus %d (err=%d)\n", busnum, ret);
343 return ret;
344 }
345 ret = spi_find_chip_select(bus, cs, &dev);
346
347
348
349
350
351
352 if (ret == -ENODEV && drv_name) {
353 dev_dbg(bus, "%s: Binding new device '%s', busnum=%d, cs=%d, driver=%s\n",
354 __func__, dev_name, busnum, cs, drv_name);
355 ret = device_bind_driver(bus, drv_name, dev_name, &dev);
356 if (ret) {
357 dev_dbg(bus, "%s: Unable to bind driver (ret=%d)\n",
358 __func__, ret);
359 return ret;
360 }
361 plat = dev_get_parent_platdata(dev);
362 plat->cs = cs;
363 if (speed) {
364 plat->max_hz = speed;
365 } else {
366 dev_warn(bus,
367 "Warning: SPI speed fallback to %u kHz\n",
368 SPI_DEFAULT_SPEED_HZ / 1000);
369 plat->max_hz = SPI_DEFAULT_SPEED_HZ;
370 }
371 plat->mode = mode;
372 created = true;
373 } else if (ret) {
374 dev_err(bus, "Invalid chip select %d:%d (err=%d)\n", busnum, cs, ret);
375 return ret;
376 }
377
378 if (!device_active(dev)) {
379 struct spi_slave *slave;
380
381 ret = device_probe(dev);
382 if (ret)
383 goto err;
384 slave = dev_get_parent_priv(dev);
385 slave->dev = dev;
386 }
387
388 slave = dev_get_parent_priv(dev);
389
390
391
392
393
394 if (!slave->speed) {
395 ret = spi_claim_bus(slave);
396 if (ret)
397 goto err;
398 }
399
400 *busp = bus;
401 *devp = slave;
402 log_debug("%s: bus=%p, slave=%p\n", __func__, bus, *devp);
403
404 return 0;
405
406err:
407 log_debug("%s: Error path, created=%d, device '%s'\n", __func__,
408 created, dev->name);
409 if (created) {
410 device_remove(dev, DM_REMOVE_NORMAL);
411 device_unbind(dev);
412 }
413
414 return ret;
415}
416
417
418struct 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,
426 &slave);
427 if (ret)
428 return NULL;
429
430 return slave;
431}
432
433void spi_free_slave(struct spi_slave *slave)
434{
435 device_remove(slave->dev, DM_REMOVE_NORMAL);
436 slave->dev = NULL;
437}
438
439int spi_slave_ofdata_to_platdata(struct udevice *dev,
440 struct dm_spi_slave_platdata *plat)
441{
442 int mode = 0;
443 int value;
444
445 plat->cs = dev_read_u32_default(dev, "reg", -1);
446 plat->max_hz = dev_read_u32_default(dev, "spi-max-frequency",
447 SPI_DEFAULT_SPEED_HZ);
448 if (dev_read_bool(dev, "spi-cpol"))
449 mode |= SPI_CPOL;
450 if (dev_read_bool(dev, "spi-cpha"))
451 mode |= SPI_CPHA;
452 if (dev_read_bool(dev, "spi-cs-high"))
453 mode |= SPI_CS_HIGH;
454 if (dev_read_bool(dev, "spi-3wire"))
455 mode |= SPI_3WIRE;
456 if (dev_read_bool(dev, "spi-half-duplex"))
457 mode |= SPI_PREAMBLE;
458
459
460 value = dev_read_u32_default(dev, "spi-tx-bus-width", 1);
461 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;
470 case 8:
471 mode |= SPI_TX_OCTAL;
472 break;
473 default:
474 warn_non_spl("spi-tx-bus-width %d not supported\n", value);
475 break;
476 }
477
478 value = dev_read_u32_default(dev, "spi-rx-bus-width", 1);
479 switch (value) {
480 case 1:
481 break;
482 case 2:
483 mode |= SPI_RX_DUAL;
484 break;
485 case 4:
486 mode |= SPI_RX_QUAD;
487 break;
488 case 8:
489 mode |= SPI_RX_OCTAL;
490 break;
491 default:
492 warn_non_spl("spi-rx-bus-width %d not supported\n", value);
493 break;
494 }
495
496 plat->mode = mode;
497
498 return 0;
499}
500
501UCLASS_DRIVER(spi) = {
502 .id = UCLASS_SPI,
503 .name = "spi",
504 .flags = DM_UC_FLAG_SEQ_ALIAS,
505#if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
506 .post_bind = dm_scan_fdt_dev,
507#endif
508 .post_probe = spi_post_probe,
509 .child_pre_probe = spi_child_pre_probe,
510 .per_device_auto_alloc_size = sizeof(struct dm_spi_bus),
511 .per_child_auto_alloc_size = sizeof(struct spi_slave),
512 .per_child_platdata_auto_alloc_size =
513 sizeof(struct dm_spi_slave_platdata),
514#if !CONFIG_IS_ENABLED(OF_PLATDATA)
515 .child_post_bind = spi_child_post_bind,
516#endif
517};
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};
528