1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * Copyright (c) 2014 Google, Inc
4 */
5
6 #include <common.h>
7 #include <dm.h>
8 #include <errno.h>
9 #include <malloc.h>
10 #include <spi.h>
11 #include <dm/device-internal.h>
12 #include <dm/uclass-internal.h>
13 #include <dm/lists.h>
14 #include <dm/util.h>
15
16 DECLARE_GLOBAL_DATA_PTR;
17
18 #define SPI_DEFAULT_SPEED_HZ 100000
19
spi_set_speed_mode(struct udevice * bus,int speed,int mode)20 static int spi_set_speed_mode(struct udevice *bus, int speed, int mode)
21 {
22 struct dm_spi_ops *ops;
23 int ret;
24
25 ops = spi_get_ops(bus);
26 if (ops->set_speed)
27 ret = ops->set_speed(bus, speed);
28 else
29 ret = -EINVAL;
30 if (ret) {
31 printf("Cannot set speed (err=%d)\n", ret);
32 return ret;
33 }
34
35 if (ops->set_mode)
36 ret = ops->set_mode(bus, mode);
37 else
38 ret = -EINVAL;
39 if (ret) {
40 printf("Cannot set mode (err=%d)\n", ret);
41 return ret;
42 }
43
44 return 0;
45 }
46
dm_spi_claim_bus(struct udevice * dev)47 int dm_spi_claim_bus(struct udevice *dev)
48 {
49 struct udevice *bus = dev->parent;
50 struct dm_spi_ops *ops = spi_get_ops(bus);
51 struct dm_spi_bus *spi = dev_get_uclass_priv(bus);
52 struct spi_slave *slave = dev_get_parent_priv(dev);
53 int speed;
54
55 speed = slave->max_hz;
56 if (spi->max_hz) {
57 if (speed)
58 speed = min(speed, (int)spi->max_hz);
59 else
60 speed = spi->max_hz;
61 }
62 if (!speed)
63 speed = SPI_DEFAULT_SPEED_HZ;
64 if (speed != slave->speed) {
65 int ret = spi_set_speed_mode(bus, speed, slave->mode);
66
67 if (ret)
68 return log_ret(ret);
69 slave->speed = speed;
70 }
71
72 return log_ret(ops->claim_bus ? ops->claim_bus(dev) : 0);
73 }
74
dm_spi_release_bus(struct udevice * dev)75 void dm_spi_release_bus(struct udevice *dev)
76 {
77 struct udevice *bus = dev->parent;
78 struct dm_spi_ops *ops = spi_get_ops(bus);
79
80 if (ops->release_bus)
81 ops->release_bus(dev);
82 }
83
dm_spi_xfer(struct udevice * dev,unsigned int bitlen,const void * dout,void * din,unsigned long flags)84 int dm_spi_xfer(struct udevice *dev, unsigned int bitlen,
85 const void *dout, void *din, unsigned long flags)
86 {
87 struct udevice *bus = dev->parent;
88
89 if (bus->uclass->uc_drv->id != UCLASS_SPI)
90 return -EOPNOTSUPP;
91
92 return spi_get_ops(bus)->xfer(dev, bitlen, dout, din, flags);
93 }
94
dm_spi_nor_ctrl_wlock(struct udevice * dev,u32 offset,size_t len)95 int dm_spi_nor_ctrl_wlock(struct udevice *dev, u32 offset, size_t len)
96 {
97 struct udevice *bus = dev->parent;
98
99 if (bus->uclass->uc_drv->id != UCLASS_SPI)
100 return -EOPNOTSUPP;
101
102 return spi_get_ops(bus)->mem_ctrl_wlock(dev, offset, len);
103 }
104
dm_spi_nor_ctrl_wunlock(struct udevice * dev,u32 offset,size_t len)105 int dm_spi_nor_ctrl_wunlock(struct udevice *dev, u32 offset, size_t len)
106 {
107 struct udevice *bus = dev->parent;
108
109 if (bus->uclass->uc_drv->id != UCLASS_SPI)
110 return -EOPNOTSUPP;
111
112 return spi_get_ops(bus)->mem_ctrl_wunlock(dev, offset, len);
113 }
114
spi_claim_bus(struct spi_slave * slave)115 int spi_claim_bus(struct spi_slave *slave)
116 {
117 return log_ret(dm_spi_claim_bus(slave->dev));
118 }
119
spi_release_bus(struct spi_slave * slave)120 void spi_release_bus(struct spi_slave *slave)
121 {
122 dm_spi_release_bus(slave->dev);
123 }
124
spi_xfer(struct spi_slave * slave,unsigned int bitlen,const void * dout,void * din,unsigned long flags)125 int spi_xfer(struct spi_slave *slave, unsigned int bitlen,
126 const void *dout, void *din, unsigned long flags)
127 {
128 return dm_spi_xfer(slave->dev, bitlen, dout, din, flags);
129 }
130
spi_nor_ctrl_wlock(struct spi_slave * slave,u32 offset,size_t len)131 int spi_nor_ctrl_wlock(struct spi_slave *slave, u32 offset, size_t len)
132 {
133 return dm_spi_nor_ctrl_wlock(slave->dev, offset, len);
134 }
135
spi_nor_ctrl_wunlock(struct spi_slave * slave,u32 offset,size_t len)136 int spi_nor_ctrl_wunlock(struct spi_slave *slave, u32 offset, size_t len)
137 {
138 return dm_spi_nor_ctrl_wunlock(slave->dev, offset, len);
139 }
140
141 #if !CONFIG_IS_ENABLED(OF_PLATDATA)
spi_child_post_bind(struct udevice * dev)142 static int spi_child_post_bind(struct udevice *dev)
143 {
144 struct dm_spi_slave_platdata *plat = dev_get_parent_platdata(dev);
145
146 if (!dev_of_valid(dev))
147 return 0;
148
149 return spi_slave_ofdata_to_platdata(dev, plat);
150 }
151 #endif
152
spi_post_probe(struct udevice * bus)153 static int spi_post_probe(struct udevice *bus)
154 {
155 #if !CONFIG_IS_ENABLED(OF_PLATDATA)
156 struct dm_spi_bus *spi = dev_get_uclass_priv(bus);
157
158 spi->max_hz = dev_read_u32_default(bus, "spi-max-frequency", 0);
159 #endif
160 #if defined(CONFIG_NEEDS_MANUAL_RELOC)
161 struct dm_spi_ops *ops = spi_get_ops(bus);
162
163 if (ops->claim_bus)
164 ops->claim_bus += gd->reloc_off;
165 if (ops->release_bus)
166 ops->release_bus += gd->reloc_off;
167 if (ops->set_wordlen)
168 ops->set_wordlen += gd->reloc_off;
169 if (ops->xfer)
170 ops->xfer += gd->reloc_off;
171 if (ops->set_speed)
172 ops->set_speed += gd->reloc_off;
173 if (ops->set_mode)
174 ops->set_mode += gd->reloc_off;
175 if (ops->cs_info)
176 ops->cs_info += gd->reloc_off;
177 #endif
178
179 return 0;
180 }
181
spi_child_pre_probe(struct udevice * dev)182 static int spi_child_pre_probe(struct udevice *dev)
183 {
184 struct dm_spi_slave_platdata *plat = dev_get_parent_platdata(dev);
185 struct spi_slave *slave = dev_get_parent_priv(dev);
186
187 /*
188 * This is needed because we pass struct spi_slave around the place
189 * instead slave->dev (a struct udevice). So we have to have some
190 * way to access the slave udevice given struct spi_slave. Once we
191 * change the SPI API to use udevice instead of spi_slave, we can
192 * drop this.
193 */
194 slave->dev = dev;
195
196 slave->max_hz = plat->max_hz;
197 slave->mode = plat->mode;
198 slave->wordlen = SPI_DEFAULT_WORDLEN;
199
200 return 0;
201 }
202
spi_chip_select(struct udevice * dev)203 int spi_chip_select(struct udevice *dev)
204 {
205 struct dm_spi_slave_platdata *plat = dev_get_parent_platdata(dev);
206
207 return plat ? plat->cs : -ENOENT;
208 }
209
spi_find_chip_select(struct udevice * bus,int cs,struct udevice ** devp)210 int spi_find_chip_select(struct udevice *bus, int cs, struct udevice **devp)
211 {
212 struct udevice *dev;
213
214 for (device_find_first_child(bus, &dev); dev;
215 device_find_next_child(&dev)) {
216 struct dm_spi_slave_platdata *plat;
217
218 plat = dev_get_parent_platdata(dev);
219 debug("%s: plat=%p, cs=%d\n", __func__, plat, plat->cs);
220 if (plat->cs == cs) {
221 *devp = dev;
222 return 0;
223 }
224 }
225
226 return -ENODEV;
227 }
228
spi_cs_is_valid(unsigned int busnum,unsigned int cs)229 int spi_cs_is_valid(unsigned int busnum, unsigned int cs)
230 {
231 struct spi_cs_info info;
232 struct udevice *bus;
233 int ret;
234
235 ret = uclass_find_device_by_seq(UCLASS_SPI, busnum, false, &bus);
236 if (ret) {
237 debug("%s: No bus %d\n", __func__, busnum);
238 return ret;
239 }
240
241 return spi_cs_info(bus, cs, &info);
242 }
243
spi_cs_info(struct udevice * bus,uint cs,struct spi_cs_info * info)244 int spi_cs_info(struct udevice *bus, uint cs, struct spi_cs_info *info)
245 {
246 struct spi_cs_info local_info;
247 struct dm_spi_ops *ops;
248 int ret;
249
250 if (!info)
251 info = &local_info;
252
253 /* If there is a device attached, return it */
254 info->dev = NULL;
255 ret = spi_find_chip_select(bus, cs, &info->dev);
256 if (!ret)
257 return 0;
258
259 /*
260 * Otherwise ask the driver. For the moment we don't have CS info.
261 * When we do we could provide the driver with a helper function
262 * to figure out what chip selects are valid, or just handle the
263 * request.
264 */
265 ops = spi_get_ops(bus);
266 if (ops->cs_info)
267 return ops->cs_info(bus, cs, info);
268
269 /*
270 * We could assume there is at least one valid chip select, but best
271 * to be sure and return an error in this case. The driver didn't
272 * care enough to tell us.
273 */
274 return -ENODEV;
275 }
276
spi_find_bus_and_cs(int busnum,int cs,struct udevice ** busp,struct udevice ** devp)277 int spi_find_bus_and_cs(int busnum, int cs, struct udevice **busp,
278 struct udevice **devp)
279 {
280 struct udevice *bus, *dev;
281 int ret;
282
283 ret = uclass_find_device_by_seq(UCLASS_SPI, busnum, false, &bus);
284 if (ret) {
285 debug("%s: No bus %d\n", __func__, busnum);
286 return ret;
287 }
288 ret = spi_find_chip_select(bus, cs, &dev);
289 if (ret) {
290 debug("%s: No cs %d\n", __func__, cs);
291 return ret;
292 }
293 *busp = bus;
294 *devp = dev;
295
296 return ret;
297 }
298
spi_get_bus_and_cs(int busnum,int cs,int speed,int mode,const char * drv_name,const char * dev_name,struct udevice ** busp,struct spi_slave ** devp)299 int spi_get_bus_and_cs(int busnum, int cs, int speed, int mode,
300 const char *drv_name, const char *dev_name,
301 struct udevice **busp, struct spi_slave **devp)
302 {
303 struct udevice *bus, *dev;
304 struct dm_spi_slave_platdata *plat;
305 bool created = false;
306 int ret;
307
308 #if CONFIG_IS_ENABLED(OF_PLATDATA) || CONFIG_IS_ENABLED(OF_PRIOR_STAGE)
309 ret = uclass_first_device_err(UCLASS_SPI, &bus);
310 #else
311 ret = uclass_get_device_by_seq(UCLASS_SPI, busnum, &bus);
312 #endif
313 if (ret) {
314 printf("Invalid bus %d (err=%d)\n", busnum, ret);
315 return ret;
316 }
317 ret = spi_find_chip_select(bus, cs, &dev);
318
319 /*
320 * If there is no such device, create one automatically. This means
321 * that we don't need a device tree node or platform data for the
322 * SPI flash chip - we will bind to the correct driver.
323 */
324 if (ret == -ENODEV && drv_name) {
325 debug("%s: Binding new device '%s', busnum=%d, cs=%d, driver=%s\n",
326 __func__, dev_name, busnum, cs, drv_name);
327 ret = device_bind_driver(bus, drv_name, dev_name, &dev);
328 if (ret) {
329 debug("%s: Unable to bind driver (ret=%d)\n", __func__,
330 ret);
331 return ret;
332 }
333 plat = dev_get_parent_platdata(dev);
334 plat->cs = cs;
335 if (speed) {
336 plat->max_hz = speed;
337 } else {
338 printf("Warning: SPI speed fallback to %u kHz\n",
339 SPI_DEFAULT_SPEED_HZ / 1000);
340 plat->max_hz = SPI_DEFAULT_SPEED_HZ;
341 }
342 plat->mode = mode;
343 created = true;
344 } else if (ret) {
345 printf("Invalid chip select %d:%d (err=%d)\n", busnum, cs,
346 ret);
347 return ret;
348 }
349
350 if (!device_active(dev)) {
351 struct spi_slave *slave;
352
353 ret = device_probe(dev);
354 if (ret)
355 goto err;
356 slave = dev_get_parent_priv(dev);
357 slave->dev = dev;
358 }
359
360 plat = dev_get_parent_platdata(dev);
361 if (!speed) {
362 speed = plat->max_hz;
363 mode = plat->mode;
364 }
365 ret = spi_set_speed_mode(bus, speed, mode);
366 if (ret)
367 goto err;
368
369 *busp = bus;
370 *devp = dev_get_parent_priv(dev);
371 debug("%s: bus=%p, slave=%p\n", __func__, bus, *devp);
372
373 return 0;
374
375 err:
376 debug("%s: Error path, created=%d, device '%s'\n", __func__,
377 created, dev->name);
378 if (created) {
379 device_remove(dev, DM_REMOVE_NORMAL);
380 device_unbind(dev);
381 }
382
383 return ret;
384 }
385
386 /* Compatibility function - to be removed */
spi_setup_slave(unsigned int busnum,unsigned int cs,unsigned int speed,unsigned int mode)387 struct spi_slave *spi_setup_slave(unsigned int busnum, unsigned int cs,
388 unsigned int speed, unsigned int mode)
389 {
390 struct spi_slave *slave;
391 struct udevice *dev;
392 int ret;
393
394 ret = spi_get_bus_and_cs(busnum, cs, speed, mode, NULL, 0, &dev,
395 &slave);
396 if (ret)
397 return NULL;
398
399 return slave;
400 }
401
spi_free_slave(struct spi_slave * slave)402 void spi_free_slave(struct spi_slave *slave)
403 {
404 device_remove(slave->dev, DM_REMOVE_NORMAL);
405 slave->dev = NULL;
406 }
407
spi_slave_ofdata_to_platdata(struct udevice * dev,struct dm_spi_slave_platdata * plat)408 int spi_slave_ofdata_to_platdata(struct udevice *dev,
409 struct dm_spi_slave_platdata *plat)
410 {
411 int mode = 0;
412 int value;
413
414 plat->cs = dev_read_u32_default(dev, "reg", -1);
415 plat->max_hz = dev_read_u32_default(dev, "spi-max-frequency",
416 SPI_DEFAULT_SPEED_HZ);
417 if (dev_read_bool(dev, "spi-cpol"))
418 mode |= SPI_CPOL;
419 if (dev_read_bool(dev, "spi-cpha"))
420 mode |= SPI_CPHA;
421 if (dev_read_bool(dev, "spi-cs-high"))
422 mode |= SPI_CS_HIGH;
423 if (dev_read_bool(dev, "spi-3wire"))
424 mode |= SPI_3WIRE;
425 if (dev_read_bool(dev, "spi-half-duplex"))
426 mode |= SPI_PREAMBLE;
427
428 /* Device DUAL/QUAD mode */
429 value = dev_read_u32_default(dev, "spi-tx-bus-width", 1);
430 switch (value) {
431 case 1:
432 break;
433 case 2:
434 mode |= SPI_TX_DUAL;
435 break;
436 case 4:
437 mode |= SPI_TX_QUAD;
438 break;
439 default:
440 warn_non_spl("spi-tx-bus-width %d not supported\n", value);
441 break;
442 }
443
444 value = dev_read_u32_default(dev, "spi-rx-bus-width", 1);
445 switch (value) {
446 case 1:
447 break;
448 case 2:
449 mode |= SPI_RX_DUAL;
450 break;
451 case 4:
452 mode |= SPI_RX_QUAD;
453 break;
454 default:
455 warn_non_spl("spi-rx-bus-width %d not supported\n", value);
456 break;
457 }
458
459 plat->mode = mode;
460
461 return 0;
462 }
463
464 UCLASS_DRIVER(spi) = {
465 .id = UCLASS_SPI,
466 .name = "spi",
467 .flags = DM_UC_FLAG_SEQ_ALIAS,
468 #if !CONFIG_IS_ENABLED(OF_PLATDATA)
469 .post_bind = dm_scan_fdt_dev,
470 #endif
471 .post_probe = spi_post_probe,
472 .child_pre_probe = spi_child_pre_probe,
473 .per_device_auto_alloc_size = sizeof(struct dm_spi_bus),
474 .per_child_auto_alloc_size = sizeof(struct spi_slave),
475 .per_child_platdata_auto_alloc_size =
476 sizeof(struct dm_spi_slave_platdata),
477 #if !CONFIG_IS_ENABLED(OF_PLATDATA)
478 .child_post_bind = spi_child_post_bind,
479 #endif
480 };
481
482 UCLASS_DRIVER(spi_generic) = {
483 .id = UCLASS_SPI_GENERIC,
484 .name = "spi_generic",
485 };
486
487 U_BOOT_DRIVER(spi_generic_drv) = {
488 .name = "spi_generic_drv",
489 .id = UCLASS_SPI_GENERIC,
490 };
491