xref: /rk3399_rockchip-uboot/drivers/spi/spi-uclass.c (revision 7a3eff4ce962de50bd9a1d83a9fce178c04999d3)
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/root.h>
16d7af6a48SSimon Glass #include <dm/lists.h>
17d7af6a48SSimon Glass #include <dm/util.h>
18d7af6a48SSimon Glass 
19d7af6a48SSimon Glass DECLARE_GLOBAL_DATA_PTR;
20d7af6a48SSimon Glass 
21d7af6a48SSimon Glass static int spi_set_speed_mode(struct udevice *bus, int speed, int mode)
22d7af6a48SSimon Glass {
23d7af6a48SSimon Glass 	struct dm_spi_ops *ops;
24d7af6a48SSimon Glass 	int ret;
25d7af6a48SSimon Glass 
26d7af6a48SSimon Glass 	ops = spi_get_ops(bus);
27d7af6a48SSimon Glass 	if (ops->set_speed)
28d7af6a48SSimon Glass 		ret = ops->set_speed(bus, speed);
29d7af6a48SSimon Glass 	else
30d7af6a48SSimon Glass 		ret = -EINVAL;
31d7af6a48SSimon Glass 	if (ret) {
32d7af6a48SSimon Glass 		printf("Cannot set speed (err=%d)\n", ret);
33d7af6a48SSimon Glass 		return ret;
34d7af6a48SSimon Glass 	}
35d7af6a48SSimon Glass 
36d7af6a48SSimon Glass 	if (ops->set_mode)
37d7af6a48SSimon Glass 		ret = ops->set_mode(bus, mode);
38d7af6a48SSimon Glass 	else
39d7af6a48SSimon Glass 		ret = -EINVAL;
40d7af6a48SSimon Glass 	if (ret) {
41d7af6a48SSimon Glass 		printf("Cannot set mode (err=%d)\n", ret);
42d7af6a48SSimon Glass 		return ret;
43d7af6a48SSimon Glass 	}
44d7af6a48SSimon Glass 
45d7af6a48SSimon Glass 	return 0;
46d7af6a48SSimon Glass }
47d7af6a48SSimon Glass 
48*7a3eff4cSPeng Fan int dm_spi_claim_bus(struct udevice *dev)
49d7af6a48SSimon Glass {
50d7af6a48SSimon Glass 	struct udevice *bus = dev->parent;
51d7af6a48SSimon Glass 	struct dm_spi_ops *ops = spi_get_ops(bus);
52e564f054SSimon Glass 	struct dm_spi_bus *spi = dev_get_uclass_priv(bus);
53*7a3eff4cSPeng Fan 	struct spi_slave *slave = dev_get_parent_priv(dev);
54d7af6a48SSimon Glass 	int speed;
55d7af6a48SSimon Glass 	int ret;
56d7af6a48SSimon Glass 
57d7af6a48SSimon Glass 	speed = slave->max_hz;
58d7af6a48SSimon Glass 	if (spi->max_hz) {
59d7af6a48SSimon Glass 		if (speed)
60b4141195SMasahiro Yamada 			speed = min(speed, (int)spi->max_hz);
61d7af6a48SSimon Glass 		else
62d7af6a48SSimon Glass 			speed = spi->max_hz;
63d7af6a48SSimon Glass 	}
64d7af6a48SSimon Glass 	if (!speed)
65d7af6a48SSimon Glass 		speed = 100000;
6660e2809aSSimon Glass 	if (speed != slave->speed) {
67d7af6a48SSimon Glass 		ret = spi_set_speed_mode(bus, speed, slave->mode);
68d7af6a48SSimon Glass 		if (ret)
69d7af6a48SSimon Glass 			return ret;
7060e2809aSSimon Glass 		slave->speed = speed;
7160e2809aSSimon Glass 	}
72d7af6a48SSimon Glass 
739694b724SSimon Glass 	return ops->claim_bus ? ops->claim_bus(dev) : 0;
74d7af6a48SSimon Glass }
75d7af6a48SSimon Glass 
76*7a3eff4cSPeng Fan void dm_spi_release_bus(struct udevice *dev)
77d7af6a48SSimon Glass {
78d7af6a48SSimon Glass 	struct udevice *bus = dev->parent;
79d7af6a48SSimon Glass 	struct dm_spi_ops *ops = spi_get_ops(bus);
80d7af6a48SSimon Glass 
81d7af6a48SSimon Glass 	if (ops->release_bus)
829694b724SSimon Glass 		ops->release_bus(dev);
83d7af6a48SSimon Glass }
84d7af6a48SSimon Glass 
85*7a3eff4cSPeng Fan int dm_spi_xfer(struct udevice *dev, unsigned int bitlen,
86d7af6a48SSimon Glass 		const void *dout, void *din, unsigned long flags)
87d7af6a48SSimon Glass {
88d7af6a48SSimon Glass 	struct udevice *bus = dev->parent;
89d7af6a48SSimon Glass 
90d7af6a48SSimon Glass 	if (bus->uclass->uc_drv->id != UCLASS_SPI)
91d7af6a48SSimon Glass 		return -EOPNOTSUPP;
92d7af6a48SSimon Glass 
93d7af6a48SSimon Glass 	return spi_get_ops(bus)->xfer(dev, bitlen, dout, din, flags);
94d7af6a48SSimon Glass }
95d7af6a48SSimon Glass 
96*7a3eff4cSPeng Fan int spi_claim_bus(struct spi_slave *slave)
97*7a3eff4cSPeng Fan {
98*7a3eff4cSPeng Fan 	return dm_spi_claim_bus(slave->dev);
99*7a3eff4cSPeng Fan }
100*7a3eff4cSPeng Fan 
101*7a3eff4cSPeng Fan void spi_release_bus(struct spi_slave *slave)
102*7a3eff4cSPeng Fan {
103*7a3eff4cSPeng Fan 	dm_spi_release_bus(slave->dev);
104*7a3eff4cSPeng Fan }
105*7a3eff4cSPeng Fan 
106*7a3eff4cSPeng Fan int spi_xfer(struct spi_slave *slave, unsigned int bitlen,
107*7a3eff4cSPeng Fan 	     const void *dout, void *din, unsigned long flags)
108*7a3eff4cSPeng Fan {
109*7a3eff4cSPeng Fan 	return dm_spi_xfer(slave->dev, bitlen, dout, din, flags);
110*7a3eff4cSPeng Fan }
111*7a3eff4cSPeng Fan 
1126f849c30SSimon Glass static int spi_post_bind(struct udevice *dev)
113d7af6a48SSimon Glass {
114d7af6a48SSimon Glass 	/* Scan the bus for devices */
115d7af6a48SSimon Glass 	return dm_scan_fdt_node(dev, gd->fdt_blob, dev->of_offset, false);
116d7af6a48SSimon Glass }
117d7af6a48SSimon Glass 
1186f849c30SSimon Glass static int spi_child_post_bind(struct udevice *dev)
119d7af6a48SSimon Glass {
120d0cff03eSSimon Glass 	struct dm_spi_slave_platdata *plat = dev_get_parent_platdata(dev);
121d7af6a48SSimon Glass 
122d0cff03eSSimon Glass 	if (dev->of_offset == -1)
123d0cff03eSSimon Glass 		return 0;
124d0cff03eSSimon Glass 
125d0cff03eSSimon Glass 	return spi_slave_ofdata_to_platdata(gd->fdt_blob, dev->of_offset, plat);
126d0cff03eSSimon Glass }
127d0cff03eSSimon Glass 
1286f849c30SSimon Glass static int spi_post_probe(struct udevice *bus)
129d0cff03eSSimon Glass {
130e564f054SSimon Glass 	struct dm_spi_bus *spi = dev_get_uclass_priv(bus);
131d0cff03eSSimon Glass 
132d0cff03eSSimon Glass 	spi->max_hz = fdtdec_get_int(gd->fdt_blob, bus->of_offset,
133d7af6a48SSimon Glass 				     "spi-max-frequency", 0);
134d7af6a48SSimon Glass 
135281f1566SMichal Simek #if defined(CONFIG_NEEDS_MANUAL_RELOC)
136281f1566SMichal Simek 	struct dm_spi_ops *ops = spi_get_ops(bus);
137281f1566SMichal Simek 
138281f1566SMichal Simek 
139281f1566SMichal Simek 	if (ops->claim_bus)
140281f1566SMichal Simek 		ops->claim_bus += gd->reloc_off;
141281f1566SMichal Simek 	if (ops->release_bus)
142281f1566SMichal Simek 		ops->release_bus += gd->reloc_off;
143281f1566SMichal Simek 	if (ops->set_wordlen)
144281f1566SMichal Simek 		ops->set_wordlen += gd->reloc_off;
145281f1566SMichal Simek 	if (ops->xfer)
146281f1566SMichal Simek 		ops->xfer += gd->reloc_off;
147281f1566SMichal Simek 	if (ops->set_speed)
148281f1566SMichal Simek 		ops->set_speed += gd->reloc_off;
149281f1566SMichal Simek 	if (ops->set_mode)
150281f1566SMichal Simek 		ops->set_mode += gd->reloc_off;
151281f1566SMichal Simek 	if (ops->cs_info)
152281f1566SMichal Simek 		ops->cs_info += gd->reloc_off;
153281f1566SMichal Simek #endif
154281f1566SMichal Simek 
155d7af6a48SSimon Glass 	return 0;
156d7af6a48SSimon Glass }
157d7af6a48SSimon Glass 
1586f849c30SSimon Glass static int spi_child_pre_probe(struct udevice *dev)
159440714eeSSimon Glass {
160d0cff03eSSimon Glass 	struct dm_spi_slave_platdata *plat = dev_get_parent_platdata(dev);
161bcbe3d15SSimon Glass 	struct spi_slave *slave = dev_get_parent_priv(dev);
162440714eeSSimon Glass 
163d0cff03eSSimon Glass 	/*
164d0cff03eSSimon Glass 	 * This is needed because we pass struct spi_slave around the place
165d0cff03eSSimon Glass 	 * instead slave->dev (a struct udevice). So we have to have some
166d0cff03eSSimon Glass 	 * way to access the slave udevice given struct spi_slave. Once we
167d0cff03eSSimon Glass 	 * change the SPI API to use udevice instead of spi_slave, we can
168d0cff03eSSimon Glass 	 * drop this.
169d0cff03eSSimon Glass 	 */
170440714eeSSimon Glass 	slave->dev = dev;
171440714eeSSimon Glass 
172d0cff03eSSimon Glass 	slave->max_hz = plat->max_hz;
173d0cff03eSSimon Glass 	slave->mode = plat->mode;
174f8e2f92dSMugunthan V N 	slave->mode_rx = plat->mode_rx;
175674f3609SChristophe Ricard 	slave->wordlen = SPI_DEFAULT_WORDLEN;
176d0cff03eSSimon Glass 
177440714eeSSimon Glass 	return 0;
178440714eeSSimon Glass }
179440714eeSSimon Glass 
180d7af6a48SSimon Glass int spi_chip_select(struct udevice *dev)
181d7af6a48SSimon Glass {
182d0cff03eSSimon Glass 	struct dm_spi_slave_platdata *plat = dev_get_parent_platdata(dev);
183d7af6a48SSimon Glass 
184d0cff03eSSimon Glass 	return plat ? plat->cs : -ENOENT;
185d7af6a48SSimon Glass }
186d7af6a48SSimon Glass 
187ff56bba2SSimon Glass int spi_find_chip_select(struct udevice *bus, int cs, struct udevice **devp)
188d7af6a48SSimon Glass {
189d7af6a48SSimon Glass 	struct udevice *dev;
190d7af6a48SSimon Glass 
191d7af6a48SSimon Glass 	for (device_find_first_child(bus, &dev); dev;
192d7af6a48SSimon Glass 	     device_find_next_child(&dev)) {
193d0cff03eSSimon Glass 		struct dm_spi_slave_platdata *plat;
194d7af6a48SSimon Glass 
195d0cff03eSSimon Glass 		plat = dev_get_parent_platdata(dev);
196d0cff03eSSimon Glass 		debug("%s: plat=%p, cs=%d\n", __func__, plat, plat->cs);
197d0cff03eSSimon Glass 		if (plat->cs == cs) {
198d7af6a48SSimon Glass 			*devp = dev;
199d7af6a48SSimon Glass 			return 0;
200d7af6a48SSimon Glass 		}
201d7af6a48SSimon Glass 	}
202d7af6a48SSimon Glass 
203d7af6a48SSimon Glass 	return -ENODEV;
204d7af6a48SSimon Glass }
205d7af6a48SSimon Glass 
206d7af6a48SSimon Glass int spi_cs_is_valid(unsigned int busnum, unsigned int cs)
207d7af6a48SSimon Glass {
208d7af6a48SSimon Glass 	struct spi_cs_info info;
209d7af6a48SSimon Glass 	struct udevice *bus;
210d7af6a48SSimon Glass 	int ret;
211d7af6a48SSimon Glass 
212d7af6a48SSimon Glass 	ret = uclass_find_device_by_seq(UCLASS_SPI, busnum, false, &bus);
213d7af6a48SSimon Glass 	if (ret) {
214d7af6a48SSimon Glass 		debug("%s: No bus %d\n", __func__, busnum);
215d7af6a48SSimon Glass 		return ret;
216d7af6a48SSimon Glass 	}
217d7af6a48SSimon Glass 
218d7af6a48SSimon Glass 	return spi_cs_info(bus, cs, &info);
219d7af6a48SSimon Glass }
220d7af6a48SSimon Glass 
221d7af6a48SSimon Glass int spi_cs_info(struct udevice *bus, uint cs, struct spi_cs_info *info)
222d7af6a48SSimon Glass {
223d7af6a48SSimon Glass 	struct spi_cs_info local_info;
224d7af6a48SSimon Glass 	struct dm_spi_ops *ops;
225d7af6a48SSimon Glass 	int ret;
226d7af6a48SSimon Glass 
227d7af6a48SSimon Glass 	if (!info)
228d7af6a48SSimon Glass 		info = &local_info;
229d7af6a48SSimon Glass 
230d7af6a48SSimon Glass 	/* If there is a device attached, return it */
231d7af6a48SSimon Glass 	info->dev = NULL;
232d7af6a48SSimon Glass 	ret = spi_find_chip_select(bus, cs, &info->dev);
233d7af6a48SSimon Glass 	if (!ret)
234d7af6a48SSimon Glass 		return 0;
235d7af6a48SSimon Glass 
236d7af6a48SSimon Glass 	/*
237d7af6a48SSimon Glass 	 * Otherwise ask the driver. For the moment we don't have CS info.
238d7af6a48SSimon Glass 	 * When we do we could provide the driver with a helper function
239d7af6a48SSimon Glass 	 * to figure out what chip selects are valid, or just handle the
240d7af6a48SSimon Glass 	 * request.
241d7af6a48SSimon Glass 	 */
242d7af6a48SSimon Glass 	ops = spi_get_ops(bus);
243d7af6a48SSimon Glass 	if (ops->cs_info)
244d7af6a48SSimon Glass 		return ops->cs_info(bus, cs, info);
245d7af6a48SSimon Glass 
246d7af6a48SSimon Glass 	/*
247d7af6a48SSimon Glass 	 * We could assume there is at least one valid chip select, but best
248d7af6a48SSimon Glass 	 * to be sure and return an error in this case. The driver didn't
249d7af6a48SSimon Glass 	 * care enough to tell us.
250d7af6a48SSimon Glass 	 */
251d7af6a48SSimon Glass 	return -ENODEV;
252d7af6a48SSimon Glass }
253d7af6a48SSimon Glass 
254d7af6a48SSimon Glass int spi_find_bus_and_cs(int busnum, int cs, struct udevice **busp,
255d7af6a48SSimon Glass 			struct udevice **devp)
256d7af6a48SSimon Glass {
257d7af6a48SSimon Glass 	struct udevice *bus, *dev;
258d7af6a48SSimon Glass 	int ret;
259d7af6a48SSimon Glass 
260d7af6a48SSimon Glass 	ret = uclass_find_device_by_seq(UCLASS_SPI, busnum, false, &bus);
261d7af6a48SSimon Glass 	if (ret) {
262d7af6a48SSimon Glass 		debug("%s: No bus %d\n", __func__, busnum);
263d7af6a48SSimon Glass 		return ret;
264d7af6a48SSimon Glass 	}
265d7af6a48SSimon Glass 	ret = spi_find_chip_select(bus, cs, &dev);
266d7af6a48SSimon Glass 	if (ret) {
267d7af6a48SSimon Glass 		debug("%s: No cs %d\n", __func__, cs);
268d7af6a48SSimon Glass 		return ret;
269d7af6a48SSimon Glass 	}
270d7af6a48SSimon Glass 	*busp = bus;
271d7af6a48SSimon Glass 	*devp = dev;
272d7af6a48SSimon Glass 
273d7af6a48SSimon Glass 	return ret;
274d7af6a48SSimon Glass }
275d7af6a48SSimon Glass 
276d7af6a48SSimon Glass int spi_get_bus_and_cs(int busnum, int cs, int speed, int mode,
277d7af6a48SSimon Glass 		       const char *drv_name, const char *dev_name,
278d7af6a48SSimon Glass 		       struct udevice **busp, struct spi_slave **devp)
279d7af6a48SSimon Glass {
280d7af6a48SSimon Glass 	struct udevice *bus, *dev;
281d7af6a48SSimon Glass 	bool created = false;
282d7af6a48SSimon Glass 	int ret;
283d7af6a48SSimon Glass 
284d7af6a48SSimon Glass 	ret = uclass_get_device_by_seq(UCLASS_SPI, busnum, &bus);
285d7af6a48SSimon Glass 	if (ret) {
286d7af6a48SSimon Glass 		printf("Invalid bus %d (err=%d)\n", busnum, ret);
287d7af6a48SSimon Glass 		return ret;
288d7af6a48SSimon Glass 	}
289d7af6a48SSimon Glass 	ret = spi_find_chip_select(bus, cs, &dev);
290d7af6a48SSimon Glass 
291d7af6a48SSimon Glass 	/*
292d7af6a48SSimon Glass 	 * If there is no such device, create one automatically. This means
293d7af6a48SSimon Glass 	 * that we don't need a device tree node or platform data for the
294d7af6a48SSimon Glass 	 * SPI flash chip - we will bind to the correct driver.
295d7af6a48SSimon Glass 	 */
296d7af6a48SSimon Glass 	if (ret == -ENODEV && drv_name) {
297d0cff03eSSimon Glass 		struct dm_spi_slave_platdata *plat;
298d0cff03eSSimon Glass 
299d7af6a48SSimon Glass 		debug("%s: Binding new device '%s', busnum=%d, cs=%d, driver=%s\n",
300d7af6a48SSimon Glass 		      __func__, dev_name, busnum, cs, drv_name);
3016b18656aSSimon Glass 		ret = device_bind_driver(bus, drv_name, dev_name, &dev);
302d7af6a48SSimon Glass 		if (ret)
303d7af6a48SSimon Glass 			return ret;
304d0cff03eSSimon Glass 		plat = dev_get_parent_platdata(dev);
305d0cff03eSSimon Glass 		plat->cs = cs;
306d0cff03eSSimon Glass 		plat->max_hz = speed;
307d0cff03eSSimon Glass 		plat->mode = mode;
308d7af6a48SSimon Glass 		created = true;
309d7af6a48SSimon Glass 	} else if (ret) {
310d7af6a48SSimon Glass 		printf("Invalid chip select %d:%d (err=%d)\n", busnum, cs,
311d7af6a48SSimon Glass 		       ret);
312d7af6a48SSimon Glass 		return ret;
313d7af6a48SSimon Glass 	}
314d7af6a48SSimon Glass 
315d7af6a48SSimon Glass 	if (!device_active(dev)) {
316d0cff03eSSimon Glass 		struct spi_slave *slave;
317d7af6a48SSimon Glass 
318d0cff03eSSimon Glass 		ret = device_probe(dev);
319d7af6a48SSimon Glass 		if (ret)
320d7af6a48SSimon Glass 			goto err;
321bcbe3d15SSimon Glass 		slave = dev_get_parent_priv(dev);
322d7af6a48SSimon Glass 		slave->dev = dev;
323d7af6a48SSimon Glass 	}
324d7af6a48SSimon Glass 
325d7af6a48SSimon Glass 	ret = spi_set_speed_mode(bus, speed, mode);
326d7af6a48SSimon Glass 	if (ret)
327d7af6a48SSimon Glass 		goto err;
328d7af6a48SSimon Glass 
329d7af6a48SSimon Glass 	*busp = bus;
330bcbe3d15SSimon Glass 	*devp = dev_get_parent_priv(dev);
331d7af6a48SSimon Glass 	debug("%s: bus=%p, slave=%p\n", __func__, bus, *devp);
332d7af6a48SSimon Glass 
333d7af6a48SSimon Glass 	return 0;
334d7af6a48SSimon Glass 
335d7af6a48SSimon Glass err:
336d0cff03eSSimon Glass 	debug("%s: Error path, credted=%d, device '%s'\n", __func__,
337d0cff03eSSimon Glass 	      created, dev->name);
338d7af6a48SSimon Glass 	if (created) {
339d7af6a48SSimon Glass 		device_remove(dev);
340d7af6a48SSimon Glass 		device_unbind(dev);
341d7af6a48SSimon Glass 	}
342d7af6a48SSimon Glass 
343d7af6a48SSimon Glass 	return ret;
344d7af6a48SSimon Glass }
345d7af6a48SSimon Glass 
346d7af6a48SSimon Glass /* Compatibility function - to be removed */
347d7af6a48SSimon Glass struct spi_slave *spi_setup_slave_fdt(const void *blob, int node,
348d7af6a48SSimon Glass 				      int bus_node)
349d7af6a48SSimon Glass {
350d7af6a48SSimon Glass 	struct udevice *bus, *dev;
351d7af6a48SSimon Glass 	int ret;
352d7af6a48SSimon Glass 
353d7af6a48SSimon Glass 	ret = uclass_get_device_by_of_offset(UCLASS_SPI, bus_node, &bus);
354d7af6a48SSimon Glass 	if (ret)
355d7af6a48SSimon Glass 		return NULL;
356d7af6a48SSimon Glass 	ret = device_get_child_by_of_offset(bus, node, &dev);
357d7af6a48SSimon Glass 	if (ret)
358d7af6a48SSimon Glass 		return NULL;
359bcbe3d15SSimon Glass 	return dev_get_parent_priv(dev);
360d7af6a48SSimon Glass }
361d7af6a48SSimon Glass 
362d7af6a48SSimon Glass /* Compatibility function - to be removed */
363d7af6a48SSimon Glass struct spi_slave *spi_setup_slave(unsigned int busnum, unsigned int cs,
364d7af6a48SSimon Glass 				  unsigned int speed, unsigned int mode)
365d7af6a48SSimon Glass {
366d7af6a48SSimon Glass 	struct spi_slave *slave;
367d7af6a48SSimon Glass 	struct udevice *dev;
368d7af6a48SSimon Glass 	int ret;
369d7af6a48SSimon Glass 
370d7af6a48SSimon Glass 	ret = spi_get_bus_and_cs(busnum, cs, speed, mode, NULL, 0, &dev,
371d7af6a48SSimon Glass 				  &slave);
372d7af6a48SSimon Glass 	if (ret)
373d7af6a48SSimon Glass 		return NULL;
374d7af6a48SSimon Glass 
375d7af6a48SSimon Glass 	return slave;
376d7af6a48SSimon Glass }
377d7af6a48SSimon Glass 
378d7af6a48SSimon Glass void spi_free_slave(struct spi_slave *slave)
379d7af6a48SSimon Glass {
380d7af6a48SSimon Glass 	device_remove(slave->dev);
381d7af6a48SSimon Glass 	slave->dev = NULL;
382d7af6a48SSimon Glass }
383d7af6a48SSimon Glass 
384d0cff03eSSimon Glass int spi_slave_ofdata_to_platdata(const void *blob, int node,
385d0cff03eSSimon Glass 				 struct dm_spi_slave_platdata *plat)
386d7af6a48SSimon Glass {
387f8e2f92dSMugunthan V N 	int mode = 0, mode_rx = 0;
388f8e2f92dSMugunthan V N 	int value;
389d7af6a48SSimon Glass 
390d0cff03eSSimon Glass 	plat->cs = fdtdec_get_int(blob, node, "reg", -1);
391d0cff03eSSimon Glass 	plat->max_hz = fdtdec_get_int(blob, node, "spi-max-frequency", 0);
392d7af6a48SSimon Glass 	if (fdtdec_get_bool(blob, node, "spi-cpol"))
393d7af6a48SSimon Glass 		mode |= SPI_CPOL;
394d7af6a48SSimon Glass 	if (fdtdec_get_bool(blob, node, "spi-cpha"))
395d7af6a48SSimon Glass 		mode |= SPI_CPHA;
396d7af6a48SSimon Glass 	if (fdtdec_get_bool(blob, node, "spi-cs-high"))
397d7af6a48SSimon Glass 		mode |= SPI_CS_HIGH;
398379b49d8SJagan Teki 	if (fdtdec_get_bool(blob, node, "spi-3wire"))
399379b49d8SJagan Teki 		mode |= SPI_3WIRE;
400d7af6a48SSimon Glass 	if (fdtdec_get_bool(blob, node, "spi-half-duplex"))
401d7af6a48SSimon Glass 		mode |= SPI_PREAMBLE;
402f8e2f92dSMugunthan V N 
403f8e2f92dSMugunthan V N 	/* Device DUAL/QUAD mode */
404f8e2f92dSMugunthan V N 	value = fdtdec_get_uint(blob, node, "spi-tx-bus-width", 1);
405f8e2f92dSMugunthan V N 	switch (value) {
406f8e2f92dSMugunthan V N 	case 1:
407f8e2f92dSMugunthan V N 		break;
408f8e2f92dSMugunthan V N 	case 2:
409f8e2f92dSMugunthan V N 		mode |= SPI_TX_DUAL;
410f8e2f92dSMugunthan V N 		break;
411f8e2f92dSMugunthan V N 	case 4:
412f8e2f92dSMugunthan V N 		mode |= SPI_TX_QUAD;
413f8e2f92dSMugunthan V N 		break;
414f8e2f92dSMugunthan V N 	default:
415f8e2f92dSMugunthan V N 		error("spi-tx-bus-width %d not supported\n", value);
416f8e2f92dSMugunthan V N 		break;
417f8e2f92dSMugunthan V N 	}
418f8e2f92dSMugunthan V N 
419d0cff03eSSimon Glass 	plat->mode = mode;
420d7af6a48SSimon Glass 
421f8e2f92dSMugunthan V N 	value = fdtdec_get_uint(blob, node, "spi-rx-bus-width", 1);
422f8e2f92dSMugunthan V N 	switch (value) {
423f8e2f92dSMugunthan V N 	case 1:
424f8e2f92dSMugunthan V N 		break;
425f8e2f92dSMugunthan V N 	case 2:
426f8e2f92dSMugunthan V N 		mode_rx |= SPI_RX_DUAL;
427f8e2f92dSMugunthan V N 		break;
428f8e2f92dSMugunthan V N 	case 4:
429f8e2f92dSMugunthan V N 		mode_rx |= SPI_RX_QUAD;
430f8e2f92dSMugunthan V N 		break;
431f8e2f92dSMugunthan V N 	default:
432f8e2f92dSMugunthan V N 		error("spi-rx-bus-width %d not supported\n", value);
433f8e2f92dSMugunthan V N 		break;
434f8e2f92dSMugunthan V N 	}
435f8e2f92dSMugunthan V N 
436f8e2f92dSMugunthan V N 	plat->mode_rx = mode_rx;
437f8e2f92dSMugunthan V N 
438d7af6a48SSimon Glass 	return 0;
439d7af6a48SSimon Glass }
440d7af6a48SSimon Glass 
441d7af6a48SSimon Glass UCLASS_DRIVER(spi) = {
442d7af6a48SSimon Glass 	.id		= UCLASS_SPI,
443d7af6a48SSimon Glass 	.name		= "spi",
4449cc36a2bSSimon Glass 	.flags		= DM_UC_FLAG_SEQ_ALIAS,
445d7af6a48SSimon Glass 	.post_bind	= spi_post_bind,
446d7af6a48SSimon Glass 	.post_probe	= spi_post_probe,
447440714eeSSimon Glass 	.child_pre_probe = spi_child_pre_probe,
448d7af6a48SSimon Glass 	.per_device_auto_alloc_size = sizeof(struct dm_spi_bus),
44919a25f67SSimon Glass 	.per_child_auto_alloc_size = sizeof(struct spi_slave),
450d0cff03eSSimon Glass 	.per_child_platdata_auto_alloc_size =
451d0cff03eSSimon Glass 			sizeof(struct dm_spi_slave_platdata),
452d0cff03eSSimon Glass 	.child_post_bind = spi_child_post_bind,
453d7af6a48SSimon Glass };
454d7af6a48SSimon Glass 
455d7af6a48SSimon Glass UCLASS_DRIVER(spi_generic) = {
456d7af6a48SSimon Glass 	.id		= UCLASS_SPI_GENERIC,
457d7af6a48SSimon Glass 	.name		= "spi_generic",
458d7af6a48SSimon Glass };
459d7af6a48SSimon Glass 
460d7af6a48SSimon Glass U_BOOT_DRIVER(spi_generic_drv) = {
461d7af6a48SSimon Glass 	.name		= "spi_generic_drv",
462d7af6a48SSimon Glass 	.id		= UCLASS_SPI_GENERIC,
463d7af6a48SSimon Glass };
464