blob: d2d091f5ea19b80c375115ec105db3a0fc098223 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glassd7af6a42014-10-13 23:41:52 -06002/*
3 * Copyright (c) 2014 Google, Inc
Simon Glassd7af6a42014-10-13 23:41:52 -06004 */
5
6#include <common.h>
7#include <dm.h>
8#include <errno.h>
Simon Glassd7af6a42014-10-13 23:41:52 -06009#include <malloc.h>
10#include <spi.h>
11#include <dm/device-internal.h>
12#include <dm/uclass-internal.h>
Simon Glassd7af6a42014-10-13 23:41:52 -060013#include <dm/lists.h>
14#include <dm/util.h>
15
16DECLARE_GLOBAL_DATA_PTR;
17
18static int spi_set_speed_mode(struct udevice *bus, int speed, int mode)
19{
20 struct dm_spi_ops *ops;
21 int ret;
22
23 ops = spi_get_ops(bus);
24 if (ops->set_speed)
25 ret = ops->set_speed(bus, speed);
26 else
27 ret = -EINVAL;
28 if (ret) {
29 printf("Cannot set speed (err=%d)\n", ret);
30 return ret;
31 }
32
33 if (ops->set_mode)
34 ret = ops->set_mode(bus, mode);
35 else
36 ret = -EINVAL;
37 if (ret) {
38 printf("Cannot set mode (err=%d)\n", ret);
39 return ret;
40 }
41
42 return 0;
43}
44
Peng Fan7a3eff42016-05-03 10:02:22 +080045int dm_spi_claim_bus(struct udevice *dev)
Simon Glassd7af6a42014-10-13 23:41:52 -060046{
Simon Glassd7af6a42014-10-13 23:41:52 -060047 struct udevice *bus = dev->parent;
48 struct dm_spi_ops *ops = spi_get_ops(bus);
Simon Glasse564f052015-03-05 12:25:20 -070049 struct dm_spi_bus *spi = dev_get_uclass_priv(bus);
Peng Fan7a3eff42016-05-03 10:02:22 +080050 struct spi_slave *slave = dev_get_parent_priv(dev);
Simon Glassd7af6a42014-10-13 23:41:52 -060051 int speed;
Simon Glassd7af6a42014-10-13 23:41:52 -060052
53 speed = slave->max_hz;
54 if (spi->max_hz) {
55 if (speed)
Masahiro Yamadab4141192014-11-07 03:03:31 +090056 speed = min(speed, (int)spi->max_hz);
Simon Glassd7af6a42014-10-13 23:41:52 -060057 else
58 speed = spi->max_hz;
59 }
60 if (!speed)
61 speed = 100000;
Simon Glass60e28092015-02-17 15:29:35 -070062 if (speed != slave->speed) {
Mario Six24fc1ec2018-01-15 11:08:41 +010063 int ret = spi_set_speed_mode(bus, speed, slave->mode);
64
Simon Glass60e28092015-02-17 15:29:35 -070065 if (ret)
66 return ret;
67 slave->speed = speed;
68 }
Simon Glassd7af6a42014-10-13 23:41:52 -060069
Simon Glass9694b722015-04-19 09:05:40 -060070 return ops->claim_bus ? ops->claim_bus(dev) : 0;
Simon Glassd7af6a42014-10-13 23:41:52 -060071}
72
Peng Fan7a3eff42016-05-03 10:02:22 +080073void dm_spi_release_bus(struct udevice *dev)
Simon Glassd7af6a42014-10-13 23:41:52 -060074{
Simon Glassd7af6a42014-10-13 23:41:52 -060075 struct udevice *bus = dev->parent;
76 struct dm_spi_ops *ops = spi_get_ops(bus);
77
78 if (ops->release_bus)
Simon Glass9694b722015-04-19 09:05:40 -060079 ops->release_bus(dev);
Simon Glassd7af6a42014-10-13 23:41:52 -060080}
81
Peng Fan7a3eff42016-05-03 10:02:22 +080082int dm_spi_xfer(struct udevice *dev, unsigned int bitlen,
83 const void *dout, void *din, unsigned long flags)
Simon Glassd7af6a42014-10-13 23:41:52 -060084{
Simon Glassd7af6a42014-10-13 23:41:52 -060085 struct udevice *bus = dev->parent;
86
87 if (bus->uclass->uc_drv->id != UCLASS_SPI)
88 return -EOPNOTSUPP;
89
90 return spi_get_ops(bus)->xfer(dev, bitlen, dout, din, flags);
91}
92
Peng Fan7a3eff42016-05-03 10:02:22 +080093int spi_claim_bus(struct spi_slave *slave)
94{
95 return dm_spi_claim_bus(slave->dev);
96}
97
98void spi_release_bus(struct spi_slave *slave)
99{
100 dm_spi_release_bus(slave->dev);
101}
102
103int spi_xfer(struct spi_slave *slave, unsigned int bitlen,
104 const void *dout, void *din, unsigned long flags)
105{
106 return dm_spi_xfer(slave->dev, bitlen, dout, din, flags);
107}
108
Simon Glass71634f22016-11-13 14:22:01 -0700109#if !CONFIG_IS_ENABLED(OF_PLATDATA)
Simon Glass6f849c32015-06-23 15:39:05 -0600110static int spi_child_post_bind(struct udevice *dev)
Simon Glassd7af6a42014-10-13 23:41:52 -0600111{
Simon Glassd0cff032015-01-25 08:27:12 -0700112 struct dm_spi_slave_platdata *plat = dev_get_parent_platdata(dev);
Simon Glassd7af6a42014-10-13 23:41:52 -0600113
Simon Glass279e26f2017-05-18 20:09:54 -0600114 if (!dev_of_valid(dev))
Simon Glassd0cff032015-01-25 08:27:12 -0700115 return 0;
116
Simon Glass279e26f2017-05-18 20:09:54 -0600117 return spi_slave_ofdata_to_platdata(dev, plat);
Simon Glassd0cff032015-01-25 08:27:12 -0700118}
Simon Glass71634f22016-11-13 14:22:01 -0700119#endif
Simon Glassd0cff032015-01-25 08:27:12 -0700120
Simon Glass6f849c32015-06-23 15:39:05 -0600121static int spi_post_probe(struct udevice *bus)
Simon Glassd0cff032015-01-25 08:27:12 -0700122{
Simon Glass71634f22016-11-13 14:22:01 -0700123#if !CONFIG_IS_ENABLED(OF_PLATDATA)
Simon Glasse564f052015-03-05 12:25:20 -0700124 struct dm_spi_bus *spi = dev_get_uclass_priv(bus);
Simon Glassd0cff032015-01-25 08:27:12 -0700125
Simon Glass279e26f2017-05-18 20:09:54 -0600126 spi->max_hz = dev_read_u32_default(bus, "spi-max-frequency", 0);
Simon Glass71634f22016-11-13 14:22:01 -0700127#endif
Michal Simek281f1562015-10-27 13:36:42 +0100128#if defined(CONFIG_NEEDS_MANUAL_RELOC)
129 struct dm_spi_ops *ops = spi_get_ops(bus);
130
Michal Simek281f1562015-10-27 13:36:42 +0100131 if (ops->claim_bus)
132 ops->claim_bus += gd->reloc_off;
133 if (ops->release_bus)
134 ops->release_bus += gd->reloc_off;
135 if (ops->set_wordlen)
136 ops->set_wordlen += gd->reloc_off;
137 if (ops->xfer)
138 ops->xfer += gd->reloc_off;
139 if (ops->set_speed)
140 ops->set_speed += gd->reloc_off;
141 if (ops->set_mode)
142 ops->set_mode += gd->reloc_off;
143 if (ops->cs_info)
144 ops->cs_info += gd->reloc_off;
145#endif
146
Simon Glassd7af6a42014-10-13 23:41:52 -0600147 return 0;
148}
149
Simon Glass6f849c32015-06-23 15:39:05 -0600150static int spi_child_pre_probe(struct udevice *dev)
Simon Glass440714e2015-01-25 08:27:11 -0700151{
Simon Glassd0cff032015-01-25 08:27:12 -0700152 struct dm_spi_slave_platdata *plat = dev_get_parent_platdata(dev);
Simon Glassbcbe3d12015-09-28 23:32:01 -0600153 struct spi_slave *slave = dev_get_parent_priv(dev);
Simon Glass440714e2015-01-25 08:27:11 -0700154
Simon Glassd0cff032015-01-25 08:27:12 -0700155 /*
156 * This is needed because we pass struct spi_slave around the place
157 * instead slave->dev (a struct udevice). So we have to have some
158 * way to access the slave udevice given struct spi_slave. Once we
159 * change the SPI API to use udevice instead of spi_slave, we can
160 * drop this.
161 */
Simon Glass440714e2015-01-25 08:27:11 -0700162 slave->dev = dev;
163
Simon Glassd0cff032015-01-25 08:27:12 -0700164 slave->max_hz = plat->max_hz;
165 slave->mode = plat->mode;
Christophe Ricard674f3602016-01-17 11:56:48 +0100166 slave->wordlen = SPI_DEFAULT_WORDLEN;
Simon Glassd0cff032015-01-25 08:27:12 -0700167
Simon Glass440714e2015-01-25 08:27:11 -0700168 return 0;
169}
170
Simon Glassd7af6a42014-10-13 23:41:52 -0600171int spi_chip_select(struct udevice *dev)
172{
Simon Glassd0cff032015-01-25 08:27:12 -0700173 struct dm_spi_slave_platdata *plat = dev_get_parent_platdata(dev);
Simon Glassd7af6a42014-10-13 23:41:52 -0600174
Simon Glassd0cff032015-01-25 08:27:12 -0700175 return plat ? plat->cs : -ENOENT;
Simon Glassd7af6a42014-10-13 23:41:52 -0600176}
177
Simon Glassff56bba2014-11-11 10:46:22 -0700178int spi_find_chip_select(struct udevice *bus, int cs, struct udevice **devp)
Simon Glassd7af6a42014-10-13 23:41:52 -0600179{
180 struct udevice *dev;
181
182 for (device_find_first_child(bus, &dev); dev;
183 device_find_next_child(&dev)) {
Simon Glassd0cff032015-01-25 08:27:12 -0700184 struct dm_spi_slave_platdata *plat;
Simon Glassd7af6a42014-10-13 23:41:52 -0600185
Simon Glassd0cff032015-01-25 08:27:12 -0700186 plat = dev_get_parent_platdata(dev);
187 debug("%s: plat=%p, cs=%d\n", __func__, plat, plat->cs);
188 if (plat->cs == cs) {
Simon Glassd7af6a42014-10-13 23:41:52 -0600189 *devp = dev;
190 return 0;
191 }
192 }
193
194 return -ENODEV;
195}
196
197int spi_cs_is_valid(unsigned int busnum, unsigned int cs)
198{
199 struct spi_cs_info info;
200 struct udevice *bus;
201 int ret;
202
203 ret = uclass_find_device_by_seq(UCLASS_SPI, busnum, false, &bus);
204 if (ret) {
205 debug("%s: No bus %d\n", __func__, busnum);
206 return ret;
207 }
208
209 return spi_cs_info(bus, cs, &info);
210}
211
212int spi_cs_info(struct udevice *bus, uint cs, struct spi_cs_info *info)
213{
214 struct spi_cs_info local_info;
215 struct dm_spi_ops *ops;
216 int ret;
217
218 if (!info)
219 info = &local_info;
220
221 /* If there is a device attached, return it */
222 info->dev = NULL;
223 ret = spi_find_chip_select(bus, cs, &info->dev);
224 if (!ret)
225 return 0;
226
227 /*
228 * Otherwise ask the driver. For the moment we don't have CS info.
229 * When we do we could provide the driver with a helper function
230 * to figure out what chip selects are valid, or just handle the
231 * request.
232 */
233 ops = spi_get_ops(bus);
234 if (ops->cs_info)
235 return ops->cs_info(bus, cs, info);
236
237 /*
238 * We could assume there is at least one valid chip select, but best
239 * to be sure and return an error in this case. The driver didn't
240 * care enough to tell us.
241 */
242 return -ENODEV;
243}
244
Simon Glassd7af6a42014-10-13 23:41:52 -0600245int spi_find_bus_and_cs(int busnum, int cs, struct udevice **busp,
246 struct udevice **devp)
247{
248 struct udevice *bus, *dev;
249 int ret;
250
251 ret = uclass_find_device_by_seq(UCLASS_SPI, busnum, false, &bus);
252 if (ret) {
253 debug("%s: No bus %d\n", __func__, busnum);
254 return ret;
255 }
256 ret = spi_find_chip_select(bus, cs, &dev);
257 if (ret) {
258 debug("%s: No cs %d\n", __func__, cs);
259 return ret;
260 }
261 *busp = bus;
262 *devp = dev;
263
264 return ret;
265}
266
267int spi_get_bus_and_cs(int busnum, int cs, int speed, int mode,
268 const char *drv_name, const char *dev_name,
269 struct udevice **busp, struct spi_slave **devp)
270{
271 struct udevice *bus, *dev;
Vignesh R96907c02016-07-06 10:04:28 +0530272 struct dm_spi_slave_platdata *plat;
Simon Glassd7af6a42014-10-13 23:41:52 -0600273 bool created = false;
274 int ret;
275
Simon Glass71634f22016-11-13 14:22:01 -0700276#if CONFIG_IS_ENABLED(OF_PLATDATA)
277 ret = uclass_first_device_err(UCLASS_SPI, &bus);
278#else
Simon Glassd7af6a42014-10-13 23:41:52 -0600279 ret = uclass_get_device_by_seq(UCLASS_SPI, busnum, &bus);
Simon Glass71634f22016-11-13 14:22:01 -0700280#endif
Simon Glassd7af6a42014-10-13 23:41:52 -0600281 if (ret) {
282 printf("Invalid bus %d (err=%d)\n", busnum, ret);
283 return ret;
284 }
285 ret = spi_find_chip_select(bus, cs, &dev);
286
287 /*
288 * If there is no such device, create one automatically. This means
289 * that we don't need a device tree node or platform data for the
290 * SPI flash chip - we will bind to the correct driver.
291 */
292 if (ret == -ENODEV && drv_name) {
293 debug("%s: Binding new device '%s', busnum=%d, cs=%d, driver=%s\n",
294 __func__, dev_name, busnum, cs, drv_name);
Simon Glass6b186562014-11-11 10:46:23 -0700295 ret = device_bind_driver(bus, drv_name, dev_name, &dev);
Simon Glass28f98852016-11-13 14:22:05 -0700296 if (ret) {
297 debug("%s: Unable to bind driver (ret=%d)\n", __func__,
298 ret);
Simon Glassd7af6a42014-10-13 23:41:52 -0600299 return ret;
Simon Glass28f98852016-11-13 14:22:05 -0700300 }
Simon Glassd0cff032015-01-25 08:27:12 -0700301 plat = dev_get_parent_platdata(dev);
302 plat->cs = cs;
303 plat->max_hz = speed;
304 plat->mode = mode;
Simon Glassd7af6a42014-10-13 23:41:52 -0600305 created = true;
306 } else if (ret) {
307 printf("Invalid chip select %d:%d (err=%d)\n", busnum, cs,
308 ret);
309 return ret;
310 }
311
312 if (!device_active(dev)) {
Simon Glassd0cff032015-01-25 08:27:12 -0700313 struct spi_slave *slave;
Simon Glassd7af6a42014-10-13 23:41:52 -0600314
Simon Glassd0cff032015-01-25 08:27:12 -0700315 ret = device_probe(dev);
Simon Glassd7af6a42014-10-13 23:41:52 -0600316 if (ret)
317 goto err;
Simon Glassbcbe3d12015-09-28 23:32:01 -0600318 slave = dev_get_parent_priv(dev);
Simon Glassd7af6a42014-10-13 23:41:52 -0600319 slave->dev = dev;
Simon Glassd7af6a42014-10-13 23:41:52 -0600320 }
321
Vignesh R96907c02016-07-06 10:04:28 +0530322 plat = dev_get_parent_platdata(dev);
323 if (!speed) {
324 speed = plat->max_hz;
325 mode = plat->mode;
326 }
Simon Glassd7af6a42014-10-13 23:41:52 -0600327 ret = spi_set_speed_mode(bus, speed, mode);
328 if (ret)
329 goto err;
330
331 *busp = bus;
Simon Glassbcbe3d12015-09-28 23:32:01 -0600332 *devp = dev_get_parent_priv(dev);
Simon Glassd7af6a42014-10-13 23:41:52 -0600333 debug("%s: bus=%p, slave=%p\n", __func__, bus, *devp);
334
335 return 0;
336
337err:
Anatolij Gustschinc8864d72016-04-21 09:28:02 +0200338 debug("%s: Error path, created=%d, device '%s'\n", __func__,
Simon Glassd0cff032015-01-25 08:27:12 -0700339 created, dev->name);
Simon Glassd7af6a42014-10-13 23:41:52 -0600340 if (created) {
Stefan Roese706865a2017-03-20 12:51:48 +0100341 device_remove(dev, DM_REMOVE_NORMAL);
Simon Glassd7af6a42014-10-13 23:41:52 -0600342 device_unbind(dev);
343 }
344
345 return ret;
346}
347
348/* Compatibility function - to be removed */
Simon Glassd7af6a42014-10-13 23:41:52 -0600349struct spi_slave *spi_setup_slave(unsigned int busnum, unsigned int cs,
350 unsigned int speed, unsigned int mode)
351{
352 struct spi_slave *slave;
353 struct udevice *dev;
354 int ret;
355
356 ret = spi_get_bus_and_cs(busnum, cs, speed, mode, NULL, 0, &dev,
Simon Glass279e26f2017-05-18 20:09:54 -0600357 &slave);
Simon Glassd7af6a42014-10-13 23:41:52 -0600358 if (ret)
359 return NULL;
360
361 return slave;
362}
363
364void spi_free_slave(struct spi_slave *slave)
365{
Stefan Roese706865a2017-03-20 12:51:48 +0100366 device_remove(slave->dev, DM_REMOVE_NORMAL);
Simon Glassd7af6a42014-10-13 23:41:52 -0600367 slave->dev = NULL;
368}
369
Simon Glass279e26f2017-05-18 20:09:54 -0600370int spi_slave_ofdata_to_platdata(struct udevice *dev,
Simon Glassd0cff032015-01-25 08:27:12 -0700371 struct dm_spi_slave_platdata *plat)
Simon Glassd7af6a42014-10-13 23:41:52 -0600372{
Jagan Teki08fe9c22016-08-08 17:12:12 +0530373 int mode = 0;
Mugunthan V Nf8e2f922015-12-23 20:39:37 +0530374 int value;
Simon Glassd7af6a42014-10-13 23:41:52 -0600375
Simon Glass279e26f2017-05-18 20:09:54 -0600376 plat->cs = dev_read_u32_default(dev, "reg", -1);
377 plat->max_hz = dev_read_u32_default(dev, "spi-max-frequency", 0);
378 if (dev_read_bool(dev, "spi-cpol"))
Simon Glassd7af6a42014-10-13 23:41:52 -0600379 mode |= SPI_CPOL;
Simon Glass279e26f2017-05-18 20:09:54 -0600380 if (dev_read_bool(dev, "spi-cpha"))
Simon Glassd7af6a42014-10-13 23:41:52 -0600381 mode |= SPI_CPHA;
Simon Glass279e26f2017-05-18 20:09:54 -0600382 if (dev_read_bool(dev, "spi-cs-high"))
Simon Glassd7af6a42014-10-13 23:41:52 -0600383 mode |= SPI_CS_HIGH;
Simon Glass279e26f2017-05-18 20:09:54 -0600384 if (dev_read_bool(dev, "spi-3wire"))
Jagan Teki379b49d2015-12-03 22:19:05 +0530385 mode |= SPI_3WIRE;
Simon Glass279e26f2017-05-18 20:09:54 -0600386 if (dev_read_bool(dev, "spi-half-duplex"))
Simon Glassd7af6a42014-10-13 23:41:52 -0600387 mode |= SPI_PREAMBLE;
Mugunthan V Nf8e2f922015-12-23 20:39:37 +0530388
389 /* Device DUAL/QUAD mode */
Simon Glass279e26f2017-05-18 20:09:54 -0600390 value = dev_read_u32_default(dev, "spi-tx-bus-width", 1);
Mugunthan V Nf8e2f922015-12-23 20:39:37 +0530391 switch (value) {
392 case 1:
393 break;
394 case 2:
395 mode |= SPI_TX_DUAL;
396 break;
397 case 4:
398 mode |= SPI_TX_QUAD;
399 break;
400 default:
Simon Glass1b7c28f2016-11-29 20:00:13 -0700401 warn_non_spl("spi-tx-bus-width %d not supported\n", value);
Mugunthan V Nf8e2f922015-12-23 20:39:37 +0530402 break;
403 }
404
Simon Glass279e26f2017-05-18 20:09:54 -0600405 value = dev_read_u32_default(dev, "spi-rx-bus-width", 1);
Mugunthan V Nf8e2f922015-12-23 20:39:37 +0530406 switch (value) {
407 case 1:
408 break;
409 case 2:
Jagan Teki08fe9c22016-08-08 17:12:12 +0530410 mode |= SPI_RX_DUAL;
Mugunthan V Nf8e2f922015-12-23 20:39:37 +0530411 break;
412 case 4:
Jagan Teki08fe9c22016-08-08 17:12:12 +0530413 mode |= SPI_RX_QUAD;
Mugunthan V Nf8e2f922015-12-23 20:39:37 +0530414 break;
415 default:
Simon Glass1b7c28f2016-11-29 20:00:13 -0700416 warn_non_spl("spi-rx-bus-width %d not supported\n", value);
Mugunthan V Nf8e2f922015-12-23 20:39:37 +0530417 break;
418 }
419
Jagan Teki08fe9c22016-08-08 17:12:12 +0530420 plat->mode = mode;
Mugunthan V Nf8e2f922015-12-23 20:39:37 +0530421
Simon Glassd7af6a42014-10-13 23:41:52 -0600422 return 0;
423}
424
425UCLASS_DRIVER(spi) = {
426 .id = UCLASS_SPI,
427 .name = "spi",
Simon Glass9cc36a22015-01-25 08:27:05 -0700428 .flags = DM_UC_FLAG_SEQ_ALIAS,
Simon Glass71634f22016-11-13 14:22:01 -0700429#if !CONFIG_IS_ENABLED(OF_PLATDATA)
Simon Glass91195482016-07-05 17:10:10 -0600430 .post_bind = dm_scan_fdt_dev,
Simon Glass71634f22016-11-13 14:22:01 -0700431#endif
Simon Glassd7af6a42014-10-13 23:41:52 -0600432 .post_probe = spi_post_probe,
Simon Glass440714e2015-01-25 08:27:11 -0700433 .child_pre_probe = spi_child_pre_probe,
Simon Glassd7af6a42014-10-13 23:41:52 -0600434 .per_device_auto_alloc_size = sizeof(struct dm_spi_bus),
Simon Glass19a25f62015-01-25 08:27:07 -0700435 .per_child_auto_alloc_size = sizeof(struct spi_slave),
Simon Glassd0cff032015-01-25 08:27:12 -0700436 .per_child_platdata_auto_alloc_size =
437 sizeof(struct dm_spi_slave_platdata),
Simon Glass71634f22016-11-13 14:22:01 -0700438#if !CONFIG_IS_ENABLED(OF_PLATDATA)
Simon Glassd0cff032015-01-25 08:27:12 -0700439 .child_post_bind = spi_child_post_bind,
Simon Glass71634f22016-11-13 14:22:01 -0700440#endif
Simon Glassd7af6a42014-10-13 23:41:52 -0600441};
442
443UCLASS_DRIVER(spi_generic) = {
444 .id = UCLASS_SPI_GENERIC,
445 .name = "spi_generic",
446};
447
448U_BOOT_DRIVER(spi_generic_drv) = {
449 .name = "spi_generic_drv",
450 .id = UCLASS_SPI_GENERIC,
451};