blob: 63a6217cc62d0085b6c4ba259415fae514651ba9 [file] [log] [blame]
Simon Glassdd82d442014-10-13 23:41:52 -06001/*
2 * Copyright (c) 2014 Google, Inc
3 *
4 * SPDX-License-Identifier: GPL-2.0+
5 */
6
7#include <common.h>
8#include <dm.h>
9#include <errno.h>
10#include <fdtdec.h>
11#include <malloc.h>
12#include <spi.h>
13#include <dm/device-internal.h>
14#include <dm/uclass-internal.h>
15#include <dm/root.h>
16#include <dm/lists.h>
17#include <dm/util.h>
18
19DECLARE_GLOBAL_DATA_PTR;
20
21static 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
48int spi_claim_bus(struct spi_slave *slave)
49{
50 struct udevice *dev = slave->dev;
51 struct udevice *bus = dev->parent;
52 struct dm_spi_ops *ops = spi_get_ops(bus);
53 struct dm_spi_bus *spi = bus->uclass_priv;
54 int speed;
55 int ret;
56
57 speed = slave->max_hz;
58 if (spi->max_hz) {
59 if (speed)
Masahiro Yamadadb204642014-11-07 03:03:31 +090060 speed = min(speed, (int)spi->max_hz);
Simon Glassdd82d442014-10-13 23:41:52 -060061 else
62 speed = spi->max_hz;
63 }
64 if (!speed)
65 speed = 100000;
66 ret = spi_set_speed_mode(bus, speed, slave->mode);
67 if (ret)
68 return ret;
69
70 return ops->claim_bus ? ops->claim_bus(bus) : 0;
71}
72
73void spi_release_bus(struct spi_slave *slave)
74{
75 struct udevice *dev = slave->dev;
76 struct udevice *bus = dev->parent;
77 struct dm_spi_ops *ops = spi_get_ops(bus);
78
79 if (ops->release_bus)
80 ops->release_bus(bus);
81}
82
83int spi_xfer(struct spi_slave *slave, unsigned int bitlen,
84 const void *dout, void *din, unsigned long flags)
85{
86 struct udevice *dev = slave->dev;
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
95int spi_post_bind(struct udevice *dev)
96{
97 /* Scan the bus for devices */
98 return dm_scan_fdt_node(dev, gd->fdt_blob, dev->of_offset, false);
99}
100
Simon Glass5d2ee052015-01-25 08:27:12 -0700101int spi_child_post_bind(struct udevice *dev)
Simon Glassdd82d442014-10-13 23:41:52 -0600102{
Simon Glass5d2ee052015-01-25 08:27:12 -0700103 struct dm_spi_slave_platdata *plat = dev_get_parent_platdata(dev);
Simon Glassdd82d442014-10-13 23:41:52 -0600104
Simon Glass5d2ee052015-01-25 08:27:12 -0700105 if (dev->of_offset == -1)
106 return 0;
107
108 return spi_slave_ofdata_to_platdata(gd->fdt_blob, dev->of_offset, plat);
109}
110
111int spi_post_probe(struct udevice *bus)
112{
113 struct dm_spi_bus *spi = bus->uclass_priv;
114
115 spi->max_hz = fdtdec_get_int(gd->fdt_blob, bus->of_offset,
Simon Glassdd82d442014-10-13 23:41:52 -0600116 "spi-max-frequency", 0);
117
118 return 0;
119}
120
Simon Glass82c2f502015-01-25 08:27:11 -0700121int spi_child_pre_probe(struct udevice *dev)
122{
Simon Glass5d2ee052015-01-25 08:27:12 -0700123 struct dm_spi_slave_platdata *plat = dev_get_parent_platdata(dev);
Simon Glass82c2f502015-01-25 08:27:11 -0700124 struct spi_slave *slave = dev_get_parentdata(dev);
125
Simon Glass5d2ee052015-01-25 08:27:12 -0700126 /*
127 * This is needed because we pass struct spi_slave around the place
128 * instead slave->dev (a struct udevice). So we have to have some
129 * way to access the slave udevice given struct spi_slave. Once we
130 * change the SPI API to use udevice instead of spi_slave, we can
131 * drop this.
132 */
Simon Glass82c2f502015-01-25 08:27:11 -0700133 slave->dev = dev;
134
Simon Glass5d2ee052015-01-25 08:27:12 -0700135 slave->max_hz = plat->max_hz;
136 slave->mode = plat->mode;
137
Simon Glass82c2f502015-01-25 08:27:11 -0700138 return 0;
139}
140
Simon Glassdd82d442014-10-13 23:41:52 -0600141int spi_chip_select(struct udevice *dev)
142{
Simon Glass5d2ee052015-01-25 08:27:12 -0700143 struct dm_spi_slave_platdata *plat = dev_get_parent_platdata(dev);
Simon Glassdd82d442014-10-13 23:41:52 -0600144
Simon Glass5d2ee052015-01-25 08:27:12 -0700145 return plat ? plat->cs : -ENOENT;
Simon Glassdd82d442014-10-13 23:41:52 -0600146}
147
Simon Glass5ef36f22014-11-11 10:46:22 -0700148int spi_find_chip_select(struct udevice *bus, int cs, struct udevice **devp)
Simon Glassdd82d442014-10-13 23:41:52 -0600149{
150 struct udevice *dev;
151
152 for (device_find_first_child(bus, &dev); dev;
153 device_find_next_child(&dev)) {
Simon Glass5d2ee052015-01-25 08:27:12 -0700154 struct dm_spi_slave_platdata *plat;
Simon Glassdd82d442014-10-13 23:41:52 -0600155
Simon Glass5d2ee052015-01-25 08:27:12 -0700156 plat = dev_get_parent_platdata(dev);
157 debug("%s: plat=%p, cs=%d\n", __func__, plat, plat->cs);
158 if (plat->cs == cs) {
Simon Glassdd82d442014-10-13 23:41:52 -0600159 *devp = dev;
160 return 0;
161 }
162 }
163
164 return -ENODEV;
165}
166
167int spi_cs_is_valid(unsigned int busnum, unsigned int cs)
168{
169 struct spi_cs_info info;
170 struct udevice *bus;
171 int ret;
172
173 ret = uclass_find_device_by_seq(UCLASS_SPI, busnum, false, &bus);
174 if (ret) {
175 debug("%s: No bus %d\n", __func__, busnum);
176 return ret;
177 }
178
179 return spi_cs_info(bus, cs, &info);
180}
181
182int spi_cs_info(struct udevice *bus, uint cs, struct spi_cs_info *info)
183{
184 struct spi_cs_info local_info;
185 struct dm_spi_ops *ops;
186 int ret;
187
188 if (!info)
189 info = &local_info;
190
191 /* If there is a device attached, return it */
192 info->dev = NULL;
193 ret = spi_find_chip_select(bus, cs, &info->dev);
194 if (!ret)
195 return 0;
196
197 /*
198 * Otherwise ask the driver. For the moment we don't have CS info.
199 * When we do we could provide the driver with a helper function
200 * to figure out what chip selects are valid, or just handle the
201 * request.
202 */
203 ops = spi_get_ops(bus);
204 if (ops->cs_info)
205 return ops->cs_info(bus, cs, info);
206
207 /*
208 * We could assume there is at least one valid chip select, but best
209 * to be sure and return an error in this case. The driver didn't
210 * care enough to tell us.
211 */
212 return -ENODEV;
213}
214
Simon Glassdd82d442014-10-13 23:41:52 -0600215int spi_find_bus_and_cs(int busnum, int cs, struct udevice **busp,
216 struct udevice **devp)
217{
218 struct udevice *bus, *dev;
219 int ret;
220
221 ret = uclass_find_device_by_seq(UCLASS_SPI, busnum, false, &bus);
222 if (ret) {
223 debug("%s: No bus %d\n", __func__, busnum);
224 return ret;
225 }
226 ret = spi_find_chip_select(bus, cs, &dev);
227 if (ret) {
228 debug("%s: No cs %d\n", __func__, cs);
229 return ret;
230 }
231 *busp = bus;
232 *devp = dev;
233
234 return ret;
235}
236
237int spi_get_bus_and_cs(int busnum, int cs, int speed, int mode,
238 const char *drv_name, const char *dev_name,
239 struct udevice **busp, struct spi_slave **devp)
240{
241 struct udevice *bus, *dev;
Simon Glassdd82d442014-10-13 23:41:52 -0600242 bool created = false;
243 int ret;
244
245 ret = uclass_get_device_by_seq(UCLASS_SPI, busnum, &bus);
246 if (ret) {
247 printf("Invalid bus %d (err=%d)\n", busnum, ret);
248 return ret;
249 }
250 ret = spi_find_chip_select(bus, cs, &dev);
251
252 /*
253 * If there is no such device, create one automatically. This means
254 * that we don't need a device tree node or platform data for the
255 * SPI flash chip - we will bind to the correct driver.
256 */
257 if (ret == -ENODEV && drv_name) {
Simon Glass5d2ee052015-01-25 08:27:12 -0700258 struct dm_spi_slave_platdata *plat;
259
Simon Glassdd82d442014-10-13 23:41:52 -0600260 debug("%s: Binding new device '%s', busnum=%d, cs=%d, driver=%s\n",
261 __func__, dev_name, busnum, cs, drv_name);
Simon Glassd8a21f62014-11-11 10:46:23 -0700262 ret = device_bind_driver(bus, drv_name, dev_name, &dev);
Simon Glassdd82d442014-10-13 23:41:52 -0600263 if (ret)
264 return ret;
Simon Glass5d2ee052015-01-25 08:27:12 -0700265 plat = dev_get_parent_platdata(dev);
266 plat->cs = cs;
267 plat->max_hz = speed;
268 plat->mode = mode;
Simon Glassdd82d442014-10-13 23:41:52 -0600269 created = true;
270 } else if (ret) {
271 printf("Invalid chip select %d:%d (err=%d)\n", busnum, cs,
272 ret);
273 return ret;
274 }
275
276 if (!device_active(dev)) {
Simon Glass5d2ee052015-01-25 08:27:12 -0700277 struct spi_slave *slave;
Simon Glassdd82d442014-10-13 23:41:52 -0600278
Simon Glass5d2ee052015-01-25 08:27:12 -0700279 ret = device_probe(dev);
Simon Glassdd82d442014-10-13 23:41:52 -0600280 if (ret)
281 goto err;
Simon Glass5d2ee052015-01-25 08:27:12 -0700282 slave = dev_get_parentdata(dev);
Simon Glassdd82d442014-10-13 23:41:52 -0600283 slave->dev = dev;
Simon Glassdd82d442014-10-13 23:41:52 -0600284 }
285
286 ret = spi_set_speed_mode(bus, speed, mode);
287 if (ret)
288 goto err;
289
290 *busp = bus;
291 *devp = dev_get_parentdata(dev);
292 debug("%s: bus=%p, slave=%p\n", __func__, bus, *devp);
293
294 return 0;
295
296err:
Simon Glass5d2ee052015-01-25 08:27:12 -0700297 debug("%s: Error path, credted=%d, device '%s'\n", __func__,
298 created, dev->name);
Simon Glassdd82d442014-10-13 23:41:52 -0600299 if (created) {
300 device_remove(dev);
301 device_unbind(dev);
302 }
303
304 return ret;
305}
306
307/* Compatibility function - to be removed */
308struct spi_slave *spi_setup_slave_fdt(const void *blob, int node,
309 int bus_node)
310{
311 struct udevice *bus, *dev;
312 int ret;
313
314 ret = uclass_get_device_by_of_offset(UCLASS_SPI, bus_node, &bus);
315 if (ret)
316 return NULL;
317 ret = device_get_child_by_of_offset(bus, node, &dev);
318 if (ret)
319 return NULL;
320 return dev_get_parentdata(dev);
321}
322
323/* Compatibility function - to be removed */
324struct spi_slave *spi_setup_slave(unsigned int busnum, unsigned int cs,
325 unsigned int speed, unsigned int mode)
326{
327 struct spi_slave *slave;
328 struct udevice *dev;
329 int ret;
330
331 ret = spi_get_bus_and_cs(busnum, cs, speed, mode, NULL, 0, &dev,
332 &slave);
333 if (ret)
334 return NULL;
335
336 return slave;
337}
338
339void spi_free_slave(struct spi_slave *slave)
340{
341 device_remove(slave->dev);
342 slave->dev = NULL;
343}
344
Simon Glass5d2ee052015-01-25 08:27:12 -0700345int spi_slave_ofdata_to_platdata(const void *blob, int node,
346 struct dm_spi_slave_platdata *plat)
Simon Glassdd82d442014-10-13 23:41:52 -0600347{
348 int mode = 0;
349
Simon Glass5d2ee052015-01-25 08:27:12 -0700350 plat->cs = fdtdec_get_int(blob, node, "reg", -1);
351 plat->max_hz = fdtdec_get_int(blob, node, "spi-max-frequency", 0);
Simon Glassdd82d442014-10-13 23:41:52 -0600352 if (fdtdec_get_bool(blob, node, "spi-cpol"))
353 mode |= SPI_CPOL;
354 if (fdtdec_get_bool(blob, node, "spi-cpha"))
355 mode |= SPI_CPHA;
356 if (fdtdec_get_bool(blob, node, "spi-cs-high"))
357 mode |= SPI_CS_HIGH;
358 if (fdtdec_get_bool(blob, node, "spi-half-duplex"))
359 mode |= SPI_PREAMBLE;
Simon Glass5d2ee052015-01-25 08:27:12 -0700360 plat->mode = mode;
Simon Glassdd82d442014-10-13 23:41:52 -0600361
362 return 0;
363}
364
365UCLASS_DRIVER(spi) = {
366 .id = UCLASS_SPI,
367 .name = "spi",
Simon Glass0ccb0972015-01-25 08:27:05 -0700368 .flags = DM_UC_FLAG_SEQ_ALIAS,
Simon Glassdd82d442014-10-13 23:41:52 -0600369 .post_bind = spi_post_bind,
370 .post_probe = spi_post_probe,
Simon Glass82c2f502015-01-25 08:27:11 -0700371 .child_pre_probe = spi_child_pre_probe,
Simon Glassdd82d442014-10-13 23:41:52 -0600372 .per_device_auto_alloc_size = sizeof(struct dm_spi_bus),
Simon Glass31a4d8d2015-01-25 08:27:07 -0700373 .per_child_auto_alloc_size = sizeof(struct spi_slave),
Simon Glass5d2ee052015-01-25 08:27:12 -0700374 .per_child_platdata_auto_alloc_size =
375 sizeof(struct dm_spi_slave_platdata),
376 .child_post_bind = spi_child_post_bind,
Simon Glassdd82d442014-10-13 23:41:52 -0600377};
378
379UCLASS_DRIVER(spi_generic) = {
380 .id = UCLASS_SPI_GENERIC,
381 .name = "spi_generic",
382};
383
384U_BOOT_DRIVER(spi_generic_drv) = {
385 .name = "spi_generic_drv",
386 .id = UCLASS_SPI_GENERIC,
387};