xref: /rk3399_rockchip-uboot/drivers/spi/spi-uclass.c (revision 706865afe54eee83c1f3d7e9ea2f51db8e986d7b)
1d7af6a48SSimon Glass /*
2d7af6a48SSimon Glass  * Copyright (c) 2014 Google, Inc
3d7af6a48SSimon Glass  *
4d7af6a48SSimon Glass  * SPDX-License-Identifier:	GPL-2.0+
5d7af6a48SSimon Glass  */
6d7af6a48SSimon Glass 
7d7af6a48SSimon Glass #include <common.h>
8d7af6a48SSimon Glass #include <dm.h>
9d7af6a48SSimon Glass #include <errno.h>
10d7af6a48SSimon Glass #include <fdtdec.h>
11d7af6a48SSimon Glass #include <malloc.h>
12d7af6a48SSimon Glass #include <spi.h>
13d7af6a48SSimon Glass #include <dm/device-internal.h>
14d7af6a48SSimon Glass #include <dm/uclass-internal.h>
15d7af6a48SSimon Glass #include <dm/lists.h>
16d7af6a48SSimon Glass #include <dm/util.h>
17d7af6a48SSimon Glass 
18d7af6a48SSimon Glass DECLARE_GLOBAL_DATA_PTR;
19d7af6a48SSimon Glass 
20d7af6a48SSimon Glass static int spi_set_speed_mode(struct udevice *bus, int speed, int mode)
21d7af6a48SSimon Glass {
22d7af6a48SSimon Glass 	struct dm_spi_ops *ops;
23d7af6a48SSimon Glass 	int ret;
24d7af6a48SSimon Glass 
25d7af6a48SSimon Glass 	ops = spi_get_ops(bus);
26d7af6a48SSimon Glass 	if (ops->set_speed)
27d7af6a48SSimon Glass 		ret = ops->set_speed(bus, speed);
28d7af6a48SSimon Glass 	else
29d7af6a48SSimon Glass 		ret = -EINVAL;
30d7af6a48SSimon Glass 	if (ret) {
31d7af6a48SSimon Glass 		printf("Cannot set speed (err=%d)\n", ret);
32d7af6a48SSimon Glass 		return ret;
33d7af6a48SSimon Glass 	}
34d7af6a48SSimon Glass 
35d7af6a48SSimon Glass 	if (ops->set_mode)
36d7af6a48SSimon Glass 		ret = ops->set_mode(bus, mode);
37d7af6a48SSimon Glass 	else
38d7af6a48SSimon Glass 		ret = -EINVAL;
39d7af6a48SSimon Glass 	if (ret) {
40d7af6a48SSimon Glass 		printf("Cannot set mode (err=%d)\n", ret);
41d7af6a48SSimon Glass 		return ret;
42d7af6a48SSimon Glass 	}
43d7af6a48SSimon Glass 
44d7af6a48SSimon Glass 	return 0;
45d7af6a48SSimon Glass }
46d7af6a48SSimon Glass 
477a3eff4cSPeng Fan int dm_spi_claim_bus(struct udevice *dev)
48d7af6a48SSimon Glass {
49d7af6a48SSimon Glass 	struct udevice *bus = dev->parent;
50d7af6a48SSimon Glass 	struct dm_spi_ops *ops = spi_get_ops(bus);
51e564f054SSimon Glass 	struct dm_spi_bus *spi = dev_get_uclass_priv(bus);
527a3eff4cSPeng Fan 	struct spi_slave *slave = dev_get_parent_priv(dev);
53d7af6a48SSimon Glass 	int speed;
54d7af6a48SSimon Glass 	int ret;
55d7af6a48SSimon Glass 
56d7af6a48SSimon Glass 	speed = slave->max_hz;
57d7af6a48SSimon Glass 	if (spi->max_hz) {
58d7af6a48SSimon Glass 		if (speed)
59b4141195SMasahiro Yamada 			speed = min(speed, (int)spi->max_hz);
60d7af6a48SSimon Glass 		else
61d7af6a48SSimon Glass 			speed = spi->max_hz;
62d7af6a48SSimon Glass 	}
63d7af6a48SSimon Glass 	if (!speed)
64d7af6a48SSimon Glass 		speed = 100000;
6560e2809aSSimon Glass 	if (speed != slave->speed) {
66d7af6a48SSimon Glass 		ret = spi_set_speed_mode(bus, speed, slave->mode);
67d7af6a48SSimon Glass 		if (ret)
68d7af6a48SSimon Glass 			return ret;
6960e2809aSSimon Glass 		slave->speed = speed;
7060e2809aSSimon Glass 	}
71d7af6a48SSimon Glass 
729694b724SSimon Glass 	return ops->claim_bus ? ops->claim_bus(dev) : 0;
73d7af6a48SSimon Glass }
74d7af6a48SSimon Glass 
757a3eff4cSPeng Fan void dm_spi_release_bus(struct udevice *dev)
76d7af6a48SSimon Glass {
77d7af6a48SSimon Glass 	struct udevice *bus = dev->parent;
78d7af6a48SSimon Glass 	struct dm_spi_ops *ops = spi_get_ops(bus);
79d7af6a48SSimon Glass 
80d7af6a48SSimon Glass 	if (ops->release_bus)
819694b724SSimon Glass 		ops->release_bus(dev);
82d7af6a48SSimon Glass }
83d7af6a48SSimon Glass 
847a3eff4cSPeng Fan int dm_spi_xfer(struct udevice *dev, unsigned int bitlen,
85d7af6a48SSimon Glass 		const void *dout, void *din, unsigned long flags)
86d7af6a48SSimon Glass {
87d7af6a48SSimon Glass 	struct udevice *bus = dev->parent;
88d7af6a48SSimon Glass 
89d7af6a48SSimon Glass 	if (bus->uclass->uc_drv->id != UCLASS_SPI)
90d7af6a48SSimon Glass 		return -EOPNOTSUPP;
91d7af6a48SSimon Glass 
92d7af6a48SSimon Glass 	return spi_get_ops(bus)->xfer(dev, bitlen, dout, din, flags);
93d7af6a48SSimon Glass }
94d7af6a48SSimon Glass 
957a3eff4cSPeng Fan int spi_claim_bus(struct spi_slave *slave)
967a3eff4cSPeng Fan {
977a3eff4cSPeng Fan 	return dm_spi_claim_bus(slave->dev);
987a3eff4cSPeng Fan }
997a3eff4cSPeng Fan 
1007a3eff4cSPeng Fan void spi_release_bus(struct spi_slave *slave)
1017a3eff4cSPeng Fan {
1027a3eff4cSPeng Fan 	dm_spi_release_bus(slave->dev);
1037a3eff4cSPeng Fan }
1047a3eff4cSPeng Fan 
1057a3eff4cSPeng Fan int spi_xfer(struct spi_slave *slave, unsigned int bitlen,
1067a3eff4cSPeng Fan 	     const void *dout, void *din, unsigned long flags)
1077a3eff4cSPeng Fan {
1087a3eff4cSPeng Fan 	return dm_spi_xfer(slave->dev, bitlen, dout, din, flags);
1097a3eff4cSPeng Fan }
1107a3eff4cSPeng Fan 
11171634f28SSimon Glass #if !CONFIG_IS_ENABLED(OF_PLATDATA)
1126f849c30SSimon Glass static int spi_child_post_bind(struct udevice *dev)
113d7af6a48SSimon Glass {
114d0cff03eSSimon Glass 	struct dm_spi_slave_platdata *plat = dev_get_parent_platdata(dev);
115d7af6a48SSimon Glass 
116e160f7d4SSimon Glass 	if (dev_of_offset(dev) == -1)
117d0cff03eSSimon Glass 		return 0;
118d0cff03eSSimon Glass 
119e160f7d4SSimon Glass 	return spi_slave_ofdata_to_platdata(gd->fdt_blob, dev_of_offset(dev),
120e160f7d4SSimon Glass 					    plat);
121d0cff03eSSimon Glass }
12271634f28SSimon Glass #endif
123d0cff03eSSimon Glass 
1246f849c30SSimon Glass static int spi_post_probe(struct udevice *bus)
125d0cff03eSSimon Glass {
12671634f28SSimon Glass #if !CONFIG_IS_ENABLED(OF_PLATDATA)
127e564f054SSimon Glass 	struct dm_spi_bus *spi = dev_get_uclass_priv(bus);
128d0cff03eSSimon Glass 
129e160f7d4SSimon Glass 	spi->max_hz = fdtdec_get_int(gd->fdt_blob, dev_of_offset(bus),
130d7af6a48SSimon Glass 				     "spi-max-frequency", 0);
13171634f28SSimon Glass #endif
132281f1566SMichal Simek #if defined(CONFIG_NEEDS_MANUAL_RELOC)
133281f1566SMichal Simek 	struct dm_spi_ops *ops = spi_get_ops(bus);
134281f1566SMichal Simek 
135281f1566SMichal Simek 
136281f1566SMichal Simek 	if (ops->claim_bus)
137281f1566SMichal Simek 		ops->claim_bus += gd->reloc_off;
138281f1566SMichal Simek 	if (ops->release_bus)
139281f1566SMichal Simek 		ops->release_bus += gd->reloc_off;
140281f1566SMichal Simek 	if (ops->set_wordlen)
141281f1566SMichal Simek 		ops->set_wordlen += gd->reloc_off;
142281f1566SMichal Simek 	if (ops->xfer)
143281f1566SMichal Simek 		ops->xfer += gd->reloc_off;
144281f1566SMichal Simek 	if (ops->set_speed)
145281f1566SMichal Simek 		ops->set_speed += gd->reloc_off;
146281f1566SMichal Simek 	if (ops->set_mode)
147281f1566SMichal Simek 		ops->set_mode += gd->reloc_off;
148281f1566SMichal Simek 	if (ops->cs_info)
149281f1566SMichal Simek 		ops->cs_info += gd->reloc_off;
150281f1566SMichal Simek #endif
151281f1566SMichal Simek 
152d7af6a48SSimon Glass 	return 0;
153d7af6a48SSimon Glass }
154d7af6a48SSimon Glass 
1556f849c30SSimon Glass static int spi_child_pre_probe(struct udevice *dev)
156440714eeSSimon Glass {
157d0cff03eSSimon Glass 	struct dm_spi_slave_platdata *plat = dev_get_parent_platdata(dev);
158bcbe3d15SSimon Glass 	struct spi_slave *slave = dev_get_parent_priv(dev);
159440714eeSSimon Glass 
160d0cff03eSSimon Glass 	/*
161d0cff03eSSimon Glass 	 * This is needed because we pass struct spi_slave around the place
162d0cff03eSSimon Glass 	 * instead slave->dev (a struct udevice). So we have to have some
163d0cff03eSSimon Glass 	 * way to access the slave udevice given struct spi_slave. Once we
164d0cff03eSSimon Glass 	 * change the SPI API to use udevice instead of spi_slave, we can
165d0cff03eSSimon Glass 	 * drop this.
166d0cff03eSSimon Glass 	 */
167440714eeSSimon Glass 	slave->dev = dev;
168440714eeSSimon Glass 
169d0cff03eSSimon Glass 	slave->max_hz = plat->max_hz;
170d0cff03eSSimon Glass 	slave->mode = plat->mode;
171674f3609SChristophe Ricard 	slave->wordlen = SPI_DEFAULT_WORDLEN;
172d0cff03eSSimon Glass 
173440714eeSSimon Glass 	return 0;
174440714eeSSimon Glass }
175440714eeSSimon Glass 
176d7af6a48SSimon Glass int spi_chip_select(struct udevice *dev)
177d7af6a48SSimon Glass {
178d0cff03eSSimon Glass 	struct dm_spi_slave_platdata *plat = dev_get_parent_platdata(dev);
179d7af6a48SSimon Glass 
180d0cff03eSSimon Glass 	return plat ? plat->cs : -ENOENT;
181d7af6a48SSimon Glass }
182d7af6a48SSimon Glass 
183ff56bba2SSimon Glass int spi_find_chip_select(struct udevice *bus, int cs, struct udevice **devp)
184d7af6a48SSimon Glass {
185d7af6a48SSimon Glass 	struct udevice *dev;
186d7af6a48SSimon Glass 
187d7af6a48SSimon Glass 	for (device_find_first_child(bus, &dev); dev;
188d7af6a48SSimon Glass 	     device_find_next_child(&dev)) {
189d0cff03eSSimon Glass 		struct dm_spi_slave_platdata *plat;
190d7af6a48SSimon Glass 
191d0cff03eSSimon Glass 		plat = dev_get_parent_platdata(dev);
192d0cff03eSSimon Glass 		debug("%s: plat=%p, cs=%d\n", __func__, plat, plat->cs);
193d0cff03eSSimon Glass 		if (plat->cs == cs) {
194d7af6a48SSimon Glass 			*devp = dev;
195d7af6a48SSimon Glass 			return 0;
196d7af6a48SSimon Glass 		}
197d7af6a48SSimon Glass 	}
198d7af6a48SSimon Glass 
199d7af6a48SSimon Glass 	return -ENODEV;
200d7af6a48SSimon Glass }
201d7af6a48SSimon Glass 
202d7af6a48SSimon Glass int spi_cs_is_valid(unsigned int busnum, unsigned int cs)
203d7af6a48SSimon Glass {
204d7af6a48SSimon Glass 	struct spi_cs_info info;
205d7af6a48SSimon Glass 	struct udevice *bus;
206d7af6a48SSimon Glass 	int ret;
207d7af6a48SSimon Glass 
208d7af6a48SSimon Glass 	ret = uclass_find_device_by_seq(UCLASS_SPI, busnum, false, &bus);
209d7af6a48SSimon Glass 	if (ret) {
210d7af6a48SSimon Glass 		debug("%s: No bus %d\n", __func__, busnum);
211d7af6a48SSimon Glass 		return ret;
212d7af6a48SSimon Glass 	}
213d7af6a48SSimon Glass 
214d7af6a48SSimon Glass 	return spi_cs_info(bus, cs, &info);
215d7af6a48SSimon Glass }
216d7af6a48SSimon Glass 
217d7af6a48SSimon Glass int spi_cs_info(struct udevice *bus, uint cs, struct spi_cs_info *info)
218d7af6a48SSimon Glass {
219d7af6a48SSimon Glass 	struct spi_cs_info local_info;
220d7af6a48SSimon Glass 	struct dm_spi_ops *ops;
221d7af6a48SSimon Glass 	int ret;
222d7af6a48SSimon Glass 
223d7af6a48SSimon Glass 	if (!info)
224d7af6a48SSimon Glass 		info = &local_info;
225d7af6a48SSimon Glass 
226d7af6a48SSimon Glass 	/* If there is a device attached, return it */
227d7af6a48SSimon Glass 	info->dev = NULL;
228d7af6a48SSimon Glass 	ret = spi_find_chip_select(bus, cs, &info->dev);
229d7af6a48SSimon Glass 	if (!ret)
230d7af6a48SSimon Glass 		return 0;
231d7af6a48SSimon Glass 
232d7af6a48SSimon Glass 	/*
233d7af6a48SSimon Glass 	 * Otherwise ask the driver. For the moment we don't have CS info.
234d7af6a48SSimon Glass 	 * When we do we could provide the driver with a helper function
235d7af6a48SSimon Glass 	 * to figure out what chip selects are valid, or just handle the
236d7af6a48SSimon Glass 	 * request.
237d7af6a48SSimon Glass 	 */
238d7af6a48SSimon Glass 	ops = spi_get_ops(bus);
239d7af6a48SSimon Glass 	if (ops->cs_info)
240d7af6a48SSimon Glass 		return ops->cs_info(bus, cs, info);
241d7af6a48SSimon Glass 
242d7af6a48SSimon Glass 	/*
243d7af6a48SSimon Glass 	 * We could assume there is at least one valid chip select, but best
244d7af6a48SSimon Glass 	 * to be sure and return an error in this case. The driver didn't
245d7af6a48SSimon Glass 	 * care enough to tell us.
246d7af6a48SSimon Glass 	 */
247d7af6a48SSimon Glass 	return -ENODEV;
248d7af6a48SSimon Glass }
249d7af6a48SSimon Glass 
250d7af6a48SSimon Glass int spi_find_bus_and_cs(int busnum, int cs, struct udevice **busp,
251d7af6a48SSimon Glass 			struct udevice **devp)
252d7af6a48SSimon Glass {
253d7af6a48SSimon Glass 	struct udevice *bus, *dev;
254d7af6a48SSimon Glass 	int ret;
255d7af6a48SSimon Glass 
256d7af6a48SSimon Glass 	ret = uclass_find_device_by_seq(UCLASS_SPI, busnum, false, &bus);
257d7af6a48SSimon Glass 	if (ret) {
258d7af6a48SSimon Glass 		debug("%s: No bus %d\n", __func__, busnum);
259d7af6a48SSimon Glass 		return ret;
260d7af6a48SSimon Glass 	}
261d7af6a48SSimon Glass 	ret = spi_find_chip_select(bus, cs, &dev);
262d7af6a48SSimon Glass 	if (ret) {
263d7af6a48SSimon Glass 		debug("%s: No cs %d\n", __func__, cs);
264d7af6a48SSimon Glass 		return ret;
265d7af6a48SSimon Glass 	}
266d7af6a48SSimon Glass 	*busp = bus;
267d7af6a48SSimon Glass 	*devp = dev;
268d7af6a48SSimon Glass 
269d7af6a48SSimon Glass 	return ret;
270d7af6a48SSimon Glass }
271d7af6a48SSimon Glass 
272d7af6a48SSimon Glass int spi_get_bus_and_cs(int busnum, int cs, int speed, int mode,
273d7af6a48SSimon Glass 		       const char *drv_name, const char *dev_name,
274d7af6a48SSimon Glass 		       struct udevice **busp, struct spi_slave **devp)
275d7af6a48SSimon Glass {
276d7af6a48SSimon Glass 	struct udevice *bus, *dev;
27796907c0fSVignesh R 	struct dm_spi_slave_platdata *plat;
278d7af6a48SSimon Glass 	bool created = false;
279d7af6a48SSimon Glass 	int ret;
280d7af6a48SSimon Glass 
28171634f28SSimon Glass #if CONFIG_IS_ENABLED(OF_PLATDATA)
28271634f28SSimon Glass 	ret = uclass_first_device_err(UCLASS_SPI, &bus);
28371634f28SSimon Glass #else
284d7af6a48SSimon Glass 	ret = uclass_get_device_by_seq(UCLASS_SPI, busnum, &bus);
28571634f28SSimon Glass #endif
286d7af6a48SSimon Glass 	if (ret) {
287d7af6a48SSimon Glass 		printf("Invalid bus %d (err=%d)\n", busnum, ret);
288d7af6a48SSimon Glass 		return ret;
289d7af6a48SSimon Glass 	}
290d7af6a48SSimon Glass 	ret = spi_find_chip_select(bus, cs, &dev);
291d7af6a48SSimon Glass 
292d7af6a48SSimon Glass 	/*
293d7af6a48SSimon Glass 	 * If there is no such device, create one automatically. This means
294d7af6a48SSimon Glass 	 * that we don't need a device tree node or platform data for the
295d7af6a48SSimon Glass 	 * SPI flash chip - we will bind to the correct driver.
296d7af6a48SSimon Glass 	 */
297d7af6a48SSimon Glass 	if (ret == -ENODEV && drv_name) {
298d7af6a48SSimon Glass 		debug("%s: Binding new device '%s', busnum=%d, cs=%d, driver=%s\n",
299d7af6a48SSimon Glass 		      __func__, dev_name, busnum, cs, drv_name);
3006b18656aSSimon Glass 		ret = device_bind_driver(bus, drv_name, dev_name, &dev);
30128f98858SSimon Glass 		if (ret) {
30228f98858SSimon Glass 			debug("%s: Unable to bind driver (ret=%d)\n", __func__,
30328f98858SSimon Glass 			      ret);
304d7af6a48SSimon Glass 			return ret;
30528f98858SSimon Glass 		}
306d0cff03eSSimon Glass 		plat = dev_get_parent_platdata(dev);
307d0cff03eSSimon Glass 		plat->cs = cs;
308d0cff03eSSimon Glass 		plat->max_hz = speed;
309d0cff03eSSimon Glass 		plat->mode = mode;
310d7af6a48SSimon Glass 		created = true;
311d7af6a48SSimon Glass 	} else if (ret) {
312d7af6a48SSimon Glass 		printf("Invalid chip select %d:%d (err=%d)\n", busnum, cs,
313d7af6a48SSimon Glass 		       ret);
314d7af6a48SSimon Glass 		return ret;
315d7af6a48SSimon Glass 	}
316d7af6a48SSimon Glass 
317d7af6a48SSimon Glass 	if (!device_active(dev)) {
318d0cff03eSSimon Glass 		struct spi_slave *slave;
319d7af6a48SSimon Glass 
320d0cff03eSSimon Glass 		ret = device_probe(dev);
321d7af6a48SSimon Glass 		if (ret)
322d7af6a48SSimon Glass 			goto err;
323bcbe3d15SSimon Glass 		slave = dev_get_parent_priv(dev);
324d7af6a48SSimon Glass 		slave->dev = dev;
325d7af6a48SSimon Glass 	}
326d7af6a48SSimon Glass 
32796907c0fSVignesh R 	plat = dev_get_parent_platdata(dev);
32896907c0fSVignesh R 	if (!speed) {
32996907c0fSVignesh R 		speed = plat->max_hz;
33096907c0fSVignesh R 		mode = plat->mode;
33196907c0fSVignesh R 	}
332d7af6a48SSimon Glass 	ret = spi_set_speed_mode(bus, speed, mode);
333d7af6a48SSimon Glass 	if (ret)
334d7af6a48SSimon Glass 		goto err;
335d7af6a48SSimon Glass 
336d7af6a48SSimon Glass 	*busp = bus;
337bcbe3d15SSimon Glass 	*devp = dev_get_parent_priv(dev);
338d7af6a48SSimon Glass 	debug("%s: bus=%p, slave=%p\n", __func__, bus, *devp);
339d7af6a48SSimon Glass 
340d7af6a48SSimon Glass 	return 0;
341d7af6a48SSimon Glass 
342d7af6a48SSimon Glass err:
343c8864d72SAnatolij Gustschin 	debug("%s: Error path, created=%d, device '%s'\n", __func__,
344d0cff03eSSimon Glass 	      created, dev->name);
345d7af6a48SSimon Glass 	if (created) {
346*706865afSStefan Roese 		device_remove(dev, DM_REMOVE_NORMAL);
347d7af6a48SSimon Glass 		device_unbind(dev);
348d7af6a48SSimon Glass 	}
349d7af6a48SSimon Glass 
350d7af6a48SSimon Glass 	return ret;
351d7af6a48SSimon Glass }
352d7af6a48SSimon Glass 
353d7af6a48SSimon Glass /* Compatibility function - to be removed */
354d7af6a48SSimon Glass struct spi_slave *spi_setup_slave_fdt(const void *blob, int node,
355d7af6a48SSimon Glass 				      int bus_node)
356d7af6a48SSimon Glass {
357d7af6a48SSimon Glass 	struct udevice *bus, *dev;
358d7af6a48SSimon Glass 	int ret;
359d7af6a48SSimon Glass 
360d7af6a48SSimon Glass 	ret = uclass_get_device_by_of_offset(UCLASS_SPI, bus_node, &bus);
361d7af6a48SSimon Glass 	if (ret)
362d7af6a48SSimon Glass 		return NULL;
363d7af6a48SSimon Glass 	ret = device_get_child_by_of_offset(bus, node, &dev);
364d7af6a48SSimon Glass 	if (ret)
365d7af6a48SSimon Glass 		return NULL;
366bcbe3d15SSimon Glass 	return dev_get_parent_priv(dev);
367d7af6a48SSimon Glass }
368d7af6a48SSimon Glass 
369d7af6a48SSimon Glass /* Compatibility function - to be removed */
370d7af6a48SSimon Glass struct spi_slave *spi_setup_slave(unsigned int busnum, unsigned int cs,
371d7af6a48SSimon Glass 				  unsigned int speed, unsigned int mode)
372d7af6a48SSimon Glass {
373d7af6a48SSimon Glass 	struct spi_slave *slave;
374d7af6a48SSimon Glass 	struct udevice *dev;
375d7af6a48SSimon Glass 	int ret;
376d7af6a48SSimon Glass 
377d7af6a48SSimon Glass 	ret = spi_get_bus_and_cs(busnum, cs, speed, mode, NULL, 0, &dev,
378d7af6a48SSimon Glass 				  &slave);
379d7af6a48SSimon Glass 	if (ret)
380d7af6a48SSimon Glass 		return NULL;
381d7af6a48SSimon Glass 
382d7af6a48SSimon Glass 	return slave;
383d7af6a48SSimon Glass }
384d7af6a48SSimon Glass 
385d7af6a48SSimon Glass void spi_free_slave(struct spi_slave *slave)
386d7af6a48SSimon Glass {
387*706865afSStefan Roese 	device_remove(slave->dev, DM_REMOVE_NORMAL);
388d7af6a48SSimon Glass 	slave->dev = NULL;
389d7af6a48SSimon Glass }
390d7af6a48SSimon Glass 
391d0cff03eSSimon Glass int spi_slave_ofdata_to_platdata(const void *blob, int node,
392d0cff03eSSimon Glass 				 struct dm_spi_slave_platdata *plat)
393d7af6a48SSimon Glass {
39408fe9c29SJagan Teki 	int mode = 0;
395f8e2f92dSMugunthan V N 	int value;
396d7af6a48SSimon Glass 
397d0cff03eSSimon Glass 	plat->cs = fdtdec_get_int(blob, node, "reg", -1);
398d0cff03eSSimon Glass 	plat->max_hz = fdtdec_get_int(blob, node, "spi-max-frequency", 0);
399d7af6a48SSimon Glass 	if (fdtdec_get_bool(blob, node, "spi-cpol"))
400d7af6a48SSimon Glass 		mode |= SPI_CPOL;
401d7af6a48SSimon Glass 	if (fdtdec_get_bool(blob, node, "spi-cpha"))
402d7af6a48SSimon Glass 		mode |= SPI_CPHA;
403d7af6a48SSimon Glass 	if (fdtdec_get_bool(blob, node, "spi-cs-high"))
404d7af6a48SSimon Glass 		mode |= SPI_CS_HIGH;
405379b49d8SJagan Teki 	if (fdtdec_get_bool(blob, node, "spi-3wire"))
406379b49d8SJagan Teki 		mode |= SPI_3WIRE;
407d7af6a48SSimon Glass 	if (fdtdec_get_bool(blob, node, "spi-half-duplex"))
408d7af6a48SSimon Glass 		mode |= SPI_PREAMBLE;
409f8e2f92dSMugunthan V N 
410f8e2f92dSMugunthan V N 	/* Device DUAL/QUAD mode */
411f8e2f92dSMugunthan V N 	value = fdtdec_get_uint(blob, node, "spi-tx-bus-width", 1);
412f8e2f92dSMugunthan V N 	switch (value) {
413f8e2f92dSMugunthan V N 	case 1:
414f8e2f92dSMugunthan V N 		break;
415f8e2f92dSMugunthan V N 	case 2:
416f8e2f92dSMugunthan V N 		mode |= SPI_TX_DUAL;
417f8e2f92dSMugunthan V N 		break;
418f8e2f92dSMugunthan V N 	case 4:
419f8e2f92dSMugunthan V N 		mode |= SPI_TX_QUAD;
420f8e2f92dSMugunthan V N 		break;
421f8e2f92dSMugunthan V N 	default:
4221b7c28f5SSimon Glass 		warn_non_spl("spi-tx-bus-width %d not supported\n", value);
423f8e2f92dSMugunthan V N 		break;
424f8e2f92dSMugunthan V N 	}
425f8e2f92dSMugunthan V N 
426f8e2f92dSMugunthan V N 	value = fdtdec_get_uint(blob, node, "spi-rx-bus-width", 1);
427f8e2f92dSMugunthan V N 	switch (value) {
428f8e2f92dSMugunthan V N 	case 1:
429f8e2f92dSMugunthan V N 		break;
430f8e2f92dSMugunthan V N 	case 2:
43108fe9c29SJagan Teki 		mode |= SPI_RX_DUAL;
432f8e2f92dSMugunthan V N 		break;
433f8e2f92dSMugunthan V N 	case 4:
43408fe9c29SJagan Teki 		mode |= SPI_RX_QUAD;
435f8e2f92dSMugunthan V N 		break;
436f8e2f92dSMugunthan V N 	default:
4371b7c28f5SSimon Glass 		warn_non_spl("spi-rx-bus-width %d not supported\n", value);
438f8e2f92dSMugunthan V N 		break;
439f8e2f92dSMugunthan V N 	}
440f8e2f92dSMugunthan V N 
44108fe9c29SJagan Teki 	plat->mode = mode;
442f8e2f92dSMugunthan V N 
443d7af6a48SSimon Glass 	return 0;
444d7af6a48SSimon Glass }
445d7af6a48SSimon Glass 
446d7af6a48SSimon Glass UCLASS_DRIVER(spi) = {
447d7af6a48SSimon Glass 	.id		= UCLASS_SPI,
448d7af6a48SSimon Glass 	.name		= "spi",
4499cc36a2bSSimon Glass 	.flags		= DM_UC_FLAG_SEQ_ALIAS,
45071634f28SSimon Glass #if !CONFIG_IS_ENABLED(OF_PLATDATA)
45191195485SSimon Glass 	.post_bind	= dm_scan_fdt_dev,
45271634f28SSimon Glass #endif
453d7af6a48SSimon Glass 	.post_probe	= spi_post_probe,
454440714eeSSimon Glass 	.child_pre_probe = spi_child_pre_probe,
455d7af6a48SSimon Glass 	.per_device_auto_alloc_size = sizeof(struct dm_spi_bus),
45619a25f67SSimon Glass 	.per_child_auto_alloc_size = sizeof(struct spi_slave),
457d0cff03eSSimon Glass 	.per_child_platdata_auto_alloc_size =
458d0cff03eSSimon Glass 			sizeof(struct dm_spi_slave_platdata),
45971634f28SSimon Glass #if !CONFIG_IS_ENABLED(OF_PLATDATA)
460d0cff03eSSimon Glass 	.child_post_bind = spi_child_post_bind,
46171634f28SSimon Glass #endif
462d7af6a48SSimon Glass };
463d7af6a48SSimon Glass 
464d7af6a48SSimon Glass UCLASS_DRIVER(spi_generic) = {
465d7af6a48SSimon Glass 	.id		= UCLASS_SPI_GENERIC,
466d7af6a48SSimon Glass 	.name		= "spi_generic",
467d7af6a48SSimon Glass };
468d7af6a48SSimon Glass 
469d7af6a48SSimon Glass U_BOOT_DRIVER(spi_generic_drv) = {
470d7af6a48SSimon Glass 	.name		= "spi_generic_drv",
471d7af6a48SSimon Glass 	.id		= UCLASS_SPI_GENERIC,
472d7af6a48SSimon Glass };
473