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