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