blob: 13c6b77d73df350f943b1dd3c77ce8c7e0fef0c3 [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)
60 speed = min(speed, spi->max_hz);
61 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
101int spi_post_probe(struct udevice *dev)
102{
103 struct dm_spi_bus *spi = dev->uclass_priv;
104
105 spi->max_hz = fdtdec_get_int(gd->fdt_blob, dev->of_offset,
106 "spi-max-frequency", 0);
107
108 return 0;
109}
110
111int spi_chip_select(struct udevice *dev)
112{
113 struct spi_slave *slave = dev_get_parentdata(dev);
114
115 return slave ? slave->cs : -ENOENT;
116}
117
118/**
119 * spi_find_chip_select() - Find the slave attached to chip select
120 *
121 * @bus: SPI bus to search
122 * @cs: Chip select to look for
123 * @devp: Returns the slave device if found
124 * @return 0 if found, -ENODEV on error
125 */
126static int spi_find_chip_select(struct udevice *bus, int cs,
127 struct udevice **devp)
128{
129 struct udevice *dev;
130
131 for (device_find_first_child(bus, &dev); dev;
132 device_find_next_child(&dev)) {
133 struct spi_slave store;
134 struct spi_slave *slave = dev_get_parentdata(dev);
135
136 if (!slave) {
137 slave = &store;
138 spi_ofdata_to_platdata(gd->fdt_blob, dev->of_offset,
139 slave);
140 }
141 debug("%s: slave=%p, cs=%d\n", __func__, slave,
142 slave ? slave->cs : -1);
143 if (slave && slave->cs == cs) {
144 *devp = dev;
145 return 0;
146 }
147 }
148
149 return -ENODEV;
150}
151
152int spi_cs_is_valid(unsigned int busnum, unsigned int cs)
153{
154 struct spi_cs_info info;
155 struct udevice *bus;
156 int ret;
157
158 ret = uclass_find_device_by_seq(UCLASS_SPI, busnum, false, &bus);
159 if (ret) {
160 debug("%s: No bus %d\n", __func__, busnum);
161 return ret;
162 }
163
164 return spi_cs_info(bus, cs, &info);
165}
166
167int spi_cs_info(struct udevice *bus, uint cs, struct spi_cs_info *info)
168{
169 struct spi_cs_info local_info;
170 struct dm_spi_ops *ops;
171 int ret;
172
173 if (!info)
174 info = &local_info;
175
176 /* If there is a device attached, return it */
177 info->dev = NULL;
178 ret = spi_find_chip_select(bus, cs, &info->dev);
179 if (!ret)
180 return 0;
181
182 /*
183 * Otherwise ask the driver. For the moment we don't have CS info.
184 * When we do we could provide the driver with a helper function
185 * to figure out what chip selects are valid, or just handle the
186 * request.
187 */
188 ops = spi_get_ops(bus);
189 if (ops->cs_info)
190 return ops->cs_info(bus, cs, info);
191
192 /*
193 * We could assume there is at least one valid chip select, but best
194 * to be sure and return an error in this case. The driver didn't
195 * care enough to tell us.
196 */
197 return -ENODEV;
198}
199
200int spi_bind_device(struct udevice *bus, int cs, const char *drv_name,
201 const char *dev_name, struct udevice **devp)
202{
203 struct driver *drv;
204 int ret;
205
206 drv = lists_driver_lookup_name(drv_name);
207 if (!drv) {
208 printf("Cannot find driver '%s'\n", drv_name);
209 return -ENOENT;
210 }
211 ret = device_bind(bus, drv, dev_name, NULL, -1, devp);
212 if (ret) {
213 printf("Cannot create device named '%s' (err=%d)\n",
214 dev_name, ret);
215 return ret;
216 }
217
218 return 0;
219}
220
221int spi_find_bus_and_cs(int busnum, int cs, struct udevice **busp,
222 struct udevice **devp)
223{
224 struct udevice *bus, *dev;
225 int ret;
226
227 ret = uclass_find_device_by_seq(UCLASS_SPI, busnum, false, &bus);
228 if (ret) {
229 debug("%s: No bus %d\n", __func__, busnum);
230 return ret;
231 }
232 ret = spi_find_chip_select(bus, cs, &dev);
233 if (ret) {
234 debug("%s: No cs %d\n", __func__, cs);
235 return ret;
236 }
237 *busp = bus;
238 *devp = dev;
239
240 return ret;
241}
242
243int spi_get_bus_and_cs(int busnum, int cs, int speed, int mode,
244 const char *drv_name, const char *dev_name,
245 struct udevice **busp, struct spi_slave **devp)
246{
247 struct udevice *bus, *dev;
248 struct spi_slave *slave;
249 bool created = false;
250 int ret;
251
252 ret = uclass_get_device_by_seq(UCLASS_SPI, busnum, &bus);
253 if (ret) {
254 printf("Invalid bus %d (err=%d)\n", busnum, ret);
255 return ret;
256 }
257 ret = spi_find_chip_select(bus, cs, &dev);
258
259 /*
260 * If there is no such device, create one automatically. This means
261 * that we don't need a device tree node or platform data for the
262 * SPI flash chip - we will bind to the correct driver.
263 */
264 if (ret == -ENODEV && drv_name) {
265 debug("%s: Binding new device '%s', busnum=%d, cs=%d, driver=%s\n",
266 __func__, dev_name, busnum, cs, drv_name);
267 ret = spi_bind_device(bus, cs, drv_name, dev_name, &dev);
268 if (ret)
269 return ret;
270 created = true;
271 } else if (ret) {
272 printf("Invalid chip select %d:%d (err=%d)\n", busnum, cs,
273 ret);
274 return ret;
275 }
276
277 if (!device_active(dev)) {
278 slave = (struct spi_slave *)calloc(1,
279 sizeof(struct spi_slave));
280 if (!slave) {
281 ret = -ENOMEM;
282 goto err;
283 }
284
285 ret = spi_ofdata_to_platdata(gd->fdt_blob, dev->of_offset,
286 slave);
287 if (ret)
288 goto err;
289 slave->cs = cs;
290 slave->dev = dev;
291 ret = device_probe_child(dev, slave);
292 free(slave);
293 if (ret)
294 goto err;
295 }
296
297 ret = spi_set_speed_mode(bus, speed, mode);
298 if (ret)
299 goto err;
300
301 *busp = bus;
302 *devp = dev_get_parentdata(dev);
303 debug("%s: bus=%p, slave=%p\n", __func__, bus, *devp);
304
305 return 0;
306
307err:
308 if (created) {
309 device_remove(dev);
310 device_unbind(dev);
311 }
312
313 return ret;
314}
315
316/* Compatibility function - to be removed */
317struct spi_slave *spi_setup_slave_fdt(const void *blob, int node,
318 int bus_node)
319{
320 struct udevice *bus, *dev;
321 int ret;
322
323 ret = uclass_get_device_by_of_offset(UCLASS_SPI, bus_node, &bus);
324 if (ret)
325 return NULL;
326 ret = device_get_child_by_of_offset(bus, node, &dev);
327 if (ret)
328 return NULL;
329 return dev_get_parentdata(dev);
330}
331
332/* Compatibility function - to be removed */
333struct spi_slave *spi_setup_slave(unsigned int busnum, unsigned int cs,
334 unsigned int speed, unsigned int mode)
335{
336 struct spi_slave *slave;
337 struct udevice *dev;
338 int ret;
339
340 ret = spi_get_bus_and_cs(busnum, cs, speed, mode, NULL, 0, &dev,
341 &slave);
342 if (ret)
343 return NULL;
344
345 return slave;
346}
347
348void spi_free_slave(struct spi_slave *slave)
349{
350 device_remove(slave->dev);
351 slave->dev = NULL;
352}
353
354int spi_ofdata_to_platdata(const void *blob, int node,
355 struct spi_slave *spi)
356{
357 int mode = 0;
358
359 spi->cs = fdtdec_get_int(blob, node, "reg", -1);
360 spi->max_hz = fdtdec_get_int(blob, node, "spi-max-frequency", 0);
361 if (fdtdec_get_bool(blob, node, "spi-cpol"))
362 mode |= SPI_CPOL;
363 if (fdtdec_get_bool(blob, node, "spi-cpha"))
364 mode |= SPI_CPHA;
365 if (fdtdec_get_bool(blob, node, "spi-cs-high"))
366 mode |= SPI_CS_HIGH;
367 if (fdtdec_get_bool(blob, node, "spi-half-duplex"))
368 mode |= SPI_PREAMBLE;
369 spi->mode = mode;
370
371 return 0;
372}
373
374UCLASS_DRIVER(spi) = {
375 .id = UCLASS_SPI,
376 .name = "spi",
377 .post_bind = spi_post_bind,
378 .post_probe = spi_post_probe,
379 .per_device_auto_alloc_size = sizeof(struct dm_spi_bus),
380};
381
382UCLASS_DRIVER(spi_generic) = {
383 .id = UCLASS_SPI_GENERIC,
384 .name = "spi_generic",
385};
386
387U_BOOT_DRIVER(spi_generic_drv) = {
388 .name = "spi_generic_drv",
389 .id = UCLASS_SPI_GENERIC,
390};