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