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