183d290c5STom Rini // SPDX-License-Identifier: GPL-2.0+
2d7af6a48SSimon Glass /*
3d7af6a48SSimon Glass * Copyright (c) 2014 Google, Inc
4d7af6a48SSimon Glass */
5d7af6a48SSimon Glass
6d7af6a48SSimon Glass #include <common.h>
7d7af6a48SSimon Glass #include <dm.h>
8d7af6a48SSimon Glass #include <errno.h>
9d7af6a48SSimon Glass #include <malloc.h>
10d7af6a48SSimon Glass #include <spi.h>
11d7af6a48SSimon Glass #include <dm/device-internal.h>
12d7af6a48SSimon Glass #include <dm/uclass-internal.h>
13d7af6a48SSimon Glass #include <dm/lists.h>
14d7af6a48SSimon Glass #include <dm/util.h>
15d7af6a48SSimon Glass
16d7af6a48SSimon Glass DECLARE_GLOBAL_DATA_PTR;
17d7af6a48SSimon Glass
1812bfb2e0SSimon Goldschmidt #define SPI_DEFAULT_SPEED_HZ 100000
1912bfb2e0SSimon Goldschmidt
spi_set_speed_mode(struct udevice * bus,int speed,int mode)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
dm_spi_claim_bus(struct udevice * dev)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
55d7af6a48SSimon Glass speed = slave->max_hz;
56d7af6a48SSimon Glass if (spi->max_hz) {
57d7af6a48SSimon Glass if (speed)
58b4141195SMasahiro Yamada speed = min(speed, (int)spi->max_hz);
59d7af6a48SSimon Glass else
60d7af6a48SSimon Glass speed = spi->max_hz;
61d7af6a48SSimon Glass }
62d7af6a48SSimon Glass if (!speed)
6312bfb2e0SSimon Goldschmidt speed = SPI_DEFAULT_SPEED_HZ;
6460e2809aSSimon Glass if (speed != slave->speed) {
6524fc1ec2SMario Six int ret = spi_set_speed_mode(bus, speed, slave->mode);
6624fc1ec2SMario Six
67d7af6a48SSimon Glass if (ret)
685e24a2efSSimon Glass return log_ret(ret);
6960e2809aSSimon Glass slave->speed = speed;
7060e2809aSSimon Glass }
71d7af6a48SSimon Glass
725e24a2efSSimon Glass return log_ret(ops->claim_bus ? ops->claim_bus(dev) : 0);
73d7af6a48SSimon Glass }
74d7af6a48SSimon Glass
dm_spi_release_bus(struct udevice * dev)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
dm_spi_xfer(struct udevice * dev,unsigned int bitlen,const void * dout,void * din,unsigned long flags)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
dm_spi_nor_ctrl_wlock(struct udevice * dev,u32 offset,size_t len)95*591e1cf0SChin-Ting Kuo int dm_spi_nor_ctrl_wlock(struct udevice *dev, u32 offset, size_t len)
96*591e1cf0SChin-Ting Kuo {
97*591e1cf0SChin-Ting Kuo struct udevice *bus = dev->parent;
98*591e1cf0SChin-Ting Kuo
99*591e1cf0SChin-Ting Kuo if (bus->uclass->uc_drv->id != UCLASS_SPI)
100*591e1cf0SChin-Ting Kuo return -EOPNOTSUPP;
101*591e1cf0SChin-Ting Kuo
102*591e1cf0SChin-Ting Kuo return spi_get_ops(bus)->mem_ctrl_wlock(dev, offset, len);
103*591e1cf0SChin-Ting Kuo }
104*591e1cf0SChin-Ting Kuo
dm_spi_nor_ctrl_wunlock(struct udevice * dev,u32 offset,size_t len)105*591e1cf0SChin-Ting Kuo int dm_spi_nor_ctrl_wunlock(struct udevice *dev, u32 offset, size_t len)
106*591e1cf0SChin-Ting Kuo {
107*591e1cf0SChin-Ting Kuo struct udevice *bus = dev->parent;
108*591e1cf0SChin-Ting Kuo
109*591e1cf0SChin-Ting Kuo if (bus->uclass->uc_drv->id != UCLASS_SPI)
110*591e1cf0SChin-Ting Kuo return -EOPNOTSUPP;
111*591e1cf0SChin-Ting Kuo
112*591e1cf0SChin-Ting Kuo return spi_get_ops(bus)->mem_ctrl_wunlock(dev, offset, len);
113*591e1cf0SChin-Ting Kuo }
114*591e1cf0SChin-Ting Kuo
spi_claim_bus(struct spi_slave * slave)1157a3eff4cSPeng Fan int spi_claim_bus(struct spi_slave *slave)
1167a3eff4cSPeng Fan {
1175e24a2efSSimon Glass return log_ret(dm_spi_claim_bus(slave->dev));
1187a3eff4cSPeng Fan }
1197a3eff4cSPeng Fan
spi_release_bus(struct spi_slave * slave)1207a3eff4cSPeng Fan void spi_release_bus(struct spi_slave *slave)
1217a3eff4cSPeng Fan {
1227a3eff4cSPeng Fan dm_spi_release_bus(slave->dev);
1237a3eff4cSPeng Fan }
1247a3eff4cSPeng Fan
spi_xfer(struct spi_slave * slave,unsigned int bitlen,const void * dout,void * din,unsigned long flags)1257a3eff4cSPeng Fan int spi_xfer(struct spi_slave *slave, unsigned int bitlen,
1267a3eff4cSPeng Fan const void *dout, void *din, unsigned long flags)
1277a3eff4cSPeng Fan {
1287a3eff4cSPeng Fan return dm_spi_xfer(slave->dev, bitlen, dout, din, flags);
1297a3eff4cSPeng Fan }
1307a3eff4cSPeng Fan
spi_nor_ctrl_wlock(struct spi_slave * slave,u32 offset,size_t len)131*591e1cf0SChin-Ting Kuo int spi_nor_ctrl_wlock(struct spi_slave *slave, u32 offset, size_t len)
132*591e1cf0SChin-Ting Kuo {
133*591e1cf0SChin-Ting Kuo return dm_spi_nor_ctrl_wlock(slave->dev, offset, len);
134*591e1cf0SChin-Ting Kuo }
135*591e1cf0SChin-Ting Kuo
spi_nor_ctrl_wunlock(struct spi_slave * slave,u32 offset,size_t len)136*591e1cf0SChin-Ting Kuo int spi_nor_ctrl_wunlock(struct spi_slave *slave, u32 offset, size_t len)
137*591e1cf0SChin-Ting Kuo {
138*591e1cf0SChin-Ting Kuo return dm_spi_nor_ctrl_wunlock(slave->dev, offset, len);
139*591e1cf0SChin-Ting Kuo }
140*591e1cf0SChin-Ting Kuo
14171634f28SSimon Glass #if !CONFIG_IS_ENABLED(OF_PLATDATA)
spi_child_post_bind(struct udevice * dev)1426f849c30SSimon Glass static int spi_child_post_bind(struct udevice *dev)
143d7af6a48SSimon Glass {
144d0cff03eSSimon Glass struct dm_spi_slave_platdata *plat = dev_get_parent_platdata(dev);
145d7af6a48SSimon Glass
146279e26f5SSimon Glass if (!dev_of_valid(dev))
147d0cff03eSSimon Glass return 0;
148d0cff03eSSimon Glass
149279e26f5SSimon Glass return spi_slave_ofdata_to_platdata(dev, plat);
150d0cff03eSSimon Glass }
15171634f28SSimon Glass #endif
152d0cff03eSSimon Glass
spi_post_probe(struct udevice * bus)1536f849c30SSimon Glass static int spi_post_probe(struct udevice *bus)
154d0cff03eSSimon Glass {
15571634f28SSimon Glass #if !CONFIG_IS_ENABLED(OF_PLATDATA)
156e564f054SSimon Glass struct dm_spi_bus *spi = dev_get_uclass_priv(bus);
157d0cff03eSSimon Glass
158279e26f5SSimon Glass spi->max_hz = dev_read_u32_default(bus, "spi-max-frequency", 0);
15971634f28SSimon Glass #endif
160281f1566SMichal Simek #if defined(CONFIG_NEEDS_MANUAL_RELOC)
161281f1566SMichal Simek struct dm_spi_ops *ops = spi_get_ops(bus);
162281f1566SMichal Simek
163281f1566SMichal Simek if (ops->claim_bus)
164281f1566SMichal Simek ops->claim_bus += gd->reloc_off;
165281f1566SMichal Simek if (ops->release_bus)
166281f1566SMichal Simek ops->release_bus += gd->reloc_off;
167281f1566SMichal Simek if (ops->set_wordlen)
168281f1566SMichal Simek ops->set_wordlen += gd->reloc_off;
169281f1566SMichal Simek if (ops->xfer)
170281f1566SMichal Simek ops->xfer += gd->reloc_off;
171281f1566SMichal Simek if (ops->set_speed)
172281f1566SMichal Simek ops->set_speed += gd->reloc_off;
173281f1566SMichal Simek if (ops->set_mode)
174281f1566SMichal Simek ops->set_mode += gd->reloc_off;
175281f1566SMichal Simek if (ops->cs_info)
176281f1566SMichal Simek ops->cs_info += gd->reloc_off;
177281f1566SMichal Simek #endif
178281f1566SMichal Simek
179d7af6a48SSimon Glass return 0;
180d7af6a48SSimon Glass }
181d7af6a48SSimon Glass
spi_child_pre_probe(struct udevice * dev)1826f849c30SSimon Glass static int spi_child_pre_probe(struct udevice *dev)
183440714eeSSimon Glass {
184d0cff03eSSimon Glass struct dm_spi_slave_platdata *plat = dev_get_parent_platdata(dev);
185bcbe3d15SSimon Glass struct spi_slave *slave = dev_get_parent_priv(dev);
186440714eeSSimon Glass
187d0cff03eSSimon Glass /*
188d0cff03eSSimon Glass * This is needed because we pass struct spi_slave around the place
189d0cff03eSSimon Glass * instead slave->dev (a struct udevice). So we have to have some
190d0cff03eSSimon Glass * way to access the slave udevice given struct spi_slave. Once we
191d0cff03eSSimon Glass * change the SPI API to use udevice instead of spi_slave, we can
192d0cff03eSSimon Glass * drop this.
193d0cff03eSSimon Glass */
194440714eeSSimon Glass slave->dev = dev;
195440714eeSSimon Glass
196d0cff03eSSimon Glass slave->max_hz = plat->max_hz;
197d0cff03eSSimon Glass slave->mode = plat->mode;
198674f3609SChristophe Ricard slave->wordlen = SPI_DEFAULT_WORDLEN;
199d0cff03eSSimon Glass
200440714eeSSimon Glass return 0;
201440714eeSSimon Glass }
202440714eeSSimon Glass
spi_chip_select(struct udevice * dev)203d7af6a48SSimon Glass int spi_chip_select(struct udevice *dev)
204d7af6a48SSimon Glass {
205d0cff03eSSimon Glass struct dm_spi_slave_platdata *plat = dev_get_parent_platdata(dev);
206d7af6a48SSimon Glass
207d0cff03eSSimon Glass return plat ? plat->cs : -ENOENT;
208d7af6a48SSimon Glass }
209d7af6a48SSimon Glass
spi_find_chip_select(struct udevice * bus,int cs,struct udevice ** devp)210ff56bba2SSimon Glass int spi_find_chip_select(struct udevice *bus, int cs, struct udevice **devp)
211d7af6a48SSimon Glass {
212d7af6a48SSimon Glass struct udevice *dev;
213d7af6a48SSimon Glass
214d7af6a48SSimon Glass for (device_find_first_child(bus, &dev); dev;
215d7af6a48SSimon Glass device_find_next_child(&dev)) {
216d0cff03eSSimon Glass struct dm_spi_slave_platdata *plat;
217d7af6a48SSimon Glass
218d0cff03eSSimon Glass plat = dev_get_parent_platdata(dev);
219d0cff03eSSimon Glass debug("%s: plat=%p, cs=%d\n", __func__, plat, plat->cs);
220d0cff03eSSimon Glass if (plat->cs == cs) {
221d7af6a48SSimon Glass *devp = dev;
222d7af6a48SSimon Glass return 0;
223d7af6a48SSimon Glass }
224d7af6a48SSimon Glass }
225d7af6a48SSimon Glass
226d7af6a48SSimon Glass return -ENODEV;
227d7af6a48SSimon Glass }
228d7af6a48SSimon Glass
spi_cs_is_valid(unsigned int busnum,unsigned int cs)229d7af6a48SSimon Glass int spi_cs_is_valid(unsigned int busnum, unsigned int cs)
230d7af6a48SSimon Glass {
231d7af6a48SSimon Glass struct spi_cs_info info;
232d7af6a48SSimon Glass struct udevice *bus;
233d7af6a48SSimon Glass int ret;
234d7af6a48SSimon Glass
235d7af6a48SSimon Glass ret = uclass_find_device_by_seq(UCLASS_SPI, busnum, false, &bus);
236d7af6a48SSimon Glass if (ret) {
237d7af6a48SSimon Glass debug("%s: No bus %d\n", __func__, busnum);
238d7af6a48SSimon Glass return ret;
239d7af6a48SSimon Glass }
240d7af6a48SSimon Glass
241d7af6a48SSimon Glass return spi_cs_info(bus, cs, &info);
242d7af6a48SSimon Glass }
243d7af6a48SSimon Glass
spi_cs_info(struct udevice * bus,uint cs,struct spi_cs_info * info)244d7af6a48SSimon Glass int spi_cs_info(struct udevice *bus, uint cs, struct spi_cs_info *info)
245d7af6a48SSimon Glass {
246d7af6a48SSimon Glass struct spi_cs_info local_info;
247d7af6a48SSimon Glass struct dm_spi_ops *ops;
248d7af6a48SSimon Glass int ret;
249d7af6a48SSimon Glass
250d7af6a48SSimon Glass if (!info)
251d7af6a48SSimon Glass info = &local_info;
252d7af6a48SSimon Glass
253d7af6a48SSimon Glass /* If there is a device attached, return it */
254d7af6a48SSimon Glass info->dev = NULL;
255d7af6a48SSimon Glass ret = spi_find_chip_select(bus, cs, &info->dev);
256d7af6a48SSimon Glass if (!ret)
257d7af6a48SSimon Glass return 0;
258d7af6a48SSimon Glass
259d7af6a48SSimon Glass /*
260d7af6a48SSimon Glass * Otherwise ask the driver. For the moment we don't have CS info.
261d7af6a48SSimon Glass * When we do we could provide the driver with a helper function
262d7af6a48SSimon Glass * to figure out what chip selects are valid, or just handle the
263d7af6a48SSimon Glass * request.
264d7af6a48SSimon Glass */
265d7af6a48SSimon Glass ops = spi_get_ops(bus);
266d7af6a48SSimon Glass if (ops->cs_info)
267d7af6a48SSimon Glass return ops->cs_info(bus, cs, info);
268d7af6a48SSimon Glass
269d7af6a48SSimon Glass /*
270d7af6a48SSimon Glass * We could assume there is at least one valid chip select, but best
271d7af6a48SSimon Glass * to be sure and return an error in this case. The driver didn't
272d7af6a48SSimon Glass * care enough to tell us.
273d7af6a48SSimon Glass */
274d7af6a48SSimon Glass return -ENODEV;
275d7af6a48SSimon Glass }
276d7af6a48SSimon Glass
spi_find_bus_and_cs(int busnum,int cs,struct udevice ** busp,struct udevice ** devp)277d7af6a48SSimon Glass int spi_find_bus_and_cs(int busnum, int cs, struct udevice **busp,
278d7af6a48SSimon Glass struct udevice **devp)
279d7af6a48SSimon Glass {
280d7af6a48SSimon Glass struct udevice *bus, *dev;
281d7af6a48SSimon Glass int ret;
282d7af6a48SSimon Glass
283d7af6a48SSimon Glass ret = uclass_find_device_by_seq(UCLASS_SPI, busnum, false, &bus);
284d7af6a48SSimon Glass if (ret) {
285d7af6a48SSimon Glass debug("%s: No bus %d\n", __func__, busnum);
286d7af6a48SSimon Glass return ret;
287d7af6a48SSimon Glass }
288d7af6a48SSimon Glass ret = spi_find_chip_select(bus, cs, &dev);
289d7af6a48SSimon Glass if (ret) {
290d7af6a48SSimon Glass debug("%s: No cs %d\n", __func__, cs);
291d7af6a48SSimon Glass return ret;
292d7af6a48SSimon Glass }
293d7af6a48SSimon Glass *busp = bus;
294d7af6a48SSimon Glass *devp = dev;
295d7af6a48SSimon Glass
296d7af6a48SSimon Glass return ret;
297d7af6a48SSimon Glass }
298d7af6a48SSimon Glass
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)299d7af6a48SSimon Glass int spi_get_bus_and_cs(int busnum, int cs, int speed, int mode,
300d7af6a48SSimon Glass const char *drv_name, const char *dev_name,
301d7af6a48SSimon Glass struct udevice **busp, struct spi_slave **devp)
302d7af6a48SSimon Glass {
303d7af6a48SSimon Glass struct udevice *bus, *dev;
30496907c0fSVignesh R struct dm_spi_slave_platdata *plat;
305d7af6a48SSimon Glass bool created = false;
306d7af6a48SSimon Glass int ret;
307d7af6a48SSimon Glass
308894c3ad2SThomas Fitzsimmons #if CONFIG_IS_ENABLED(OF_PLATDATA) || CONFIG_IS_ENABLED(OF_PRIOR_STAGE)
30971634f28SSimon Glass ret = uclass_first_device_err(UCLASS_SPI, &bus);
31071634f28SSimon Glass #else
311d7af6a48SSimon Glass ret = uclass_get_device_by_seq(UCLASS_SPI, busnum, &bus);
31271634f28SSimon Glass #endif
313d7af6a48SSimon Glass if (ret) {
314d7af6a48SSimon Glass printf("Invalid bus %d (err=%d)\n", busnum, ret);
315d7af6a48SSimon Glass return ret;
316d7af6a48SSimon Glass }
317d7af6a48SSimon Glass ret = spi_find_chip_select(bus, cs, &dev);
318d7af6a48SSimon Glass
319d7af6a48SSimon Glass /*
320d7af6a48SSimon Glass * If there is no such device, create one automatically. This means
321d7af6a48SSimon Glass * that we don't need a device tree node or platform data for the
322d7af6a48SSimon Glass * SPI flash chip - we will bind to the correct driver.
323d7af6a48SSimon Glass */
324d7af6a48SSimon Glass if (ret == -ENODEV && drv_name) {
325d7af6a48SSimon Glass debug("%s: Binding new device '%s', busnum=%d, cs=%d, driver=%s\n",
326d7af6a48SSimon Glass __func__, dev_name, busnum, cs, drv_name);
3276b18656aSSimon Glass ret = device_bind_driver(bus, drv_name, dev_name, &dev);
32828f98858SSimon Glass if (ret) {
32928f98858SSimon Glass debug("%s: Unable to bind driver (ret=%d)\n", __func__,
33028f98858SSimon Glass ret);
331d7af6a48SSimon Glass return ret;
33228f98858SSimon Glass }
333d0cff03eSSimon Glass plat = dev_get_parent_platdata(dev);
334d0cff03eSSimon Glass plat->cs = cs;
33512bfb2e0SSimon Goldschmidt if (speed) {
336d0cff03eSSimon Glass plat->max_hz = speed;
33712bfb2e0SSimon Goldschmidt } else {
33812bfb2e0SSimon Goldschmidt printf("Warning: SPI speed fallback to %u kHz\n",
33912bfb2e0SSimon Goldschmidt SPI_DEFAULT_SPEED_HZ / 1000);
34012bfb2e0SSimon Goldschmidt plat->max_hz = SPI_DEFAULT_SPEED_HZ;
34112bfb2e0SSimon Goldschmidt }
342d0cff03eSSimon Glass plat->mode = mode;
343d7af6a48SSimon Glass created = true;
344d7af6a48SSimon Glass } else if (ret) {
345d7af6a48SSimon Glass printf("Invalid chip select %d:%d (err=%d)\n", busnum, cs,
346d7af6a48SSimon Glass ret);
347d7af6a48SSimon Glass return ret;
348d7af6a48SSimon Glass }
349d7af6a48SSimon Glass
350d7af6a48SSimon Glass if (!device_active(dev)) {
351d0cff03eSSimon Glass struct spi_slave *slave;
352d7af6a48SSimon Glass
353d0cff03eSSimon Glass ret = device_probe(dev);
354d7af6a48SSimon Glass if (ret)
355d7af6a48SSimon Glass goto err;
356bcbe3d15SSimon Glass slave = dev_get_parent_priv(dev);
357d7af6a48SSimon Glass slave->dev = dev;
358d7af6a48SSimon Glass }
359d7af6a48SSimon Glass
36096907c0fSVignesh R plat = dev_get_parent_platdata(dev);
36196907c0fSVignesh R if (!speed) {
36296907c0fSVignesh R speed = plat->max_hz;
36396907c0fSVignesh R mode = plat->mode;
36496907c0fSVignesh R }
365d7af6a48SSimon Glass ret = spi_set_speed_mode(bus, speed, mode);
366d7af6a48SSimon Glass if (ret)
367d7af6a48SSimon Glass goto err;
368d7af6a48SSimon Glass
369d7af6a48SSimon Glass *busp = bus;
370bcbe3d15SSimon Glass *devp = dev_get_parent_priv(dev);
371d7af6a48SSimon Glass debug("%s: bus=%p, slave=%p\n", __func__, bus, *devp);
372d7af6a48SSimon Glass
373d7af6a48SSimon Glass return 0;
374d7af6a48SSimon Glass
375d7af6a48SSimon Glass err:
376c8864d72SAnatolij Gustschin debug("%s: Error path, created=%d, device '%s'\n", __func__,
377d0cff03eSSimon Glass created, dev->name);
378d7af6a48SSimon Glass if (created) {
379706865afSStefan Roese device_remove(dev, DM_REMOVE_NORMAL);
380d7af6a48SSimon Glass device_unbind(dev);
381d7af6a48SSimon Glass }
382d7af6a48SSimon Glass
383d7af6a48SSimon Glass return ret;
384d7af6a48SSimon Glass }
385d7af6a48SSimon Glass
386d7af6a48SSimon Glass /* Compatibility function - to be removed */
spi_setup_slave(unsigned int busnum,unsigned int cs,unsigned int speed,unsigned int mode)387d7af6a48SSimon Glass struct spi_slave *spi_setup_slave(unsigned int busnum, unsigned int cs,
388d7af6a48SSimon Glass unsigned int speed, unsigned int mode)
389d7af6a48SSimon Glass {
390d7af6a48SSimon Glass struct spi_slave *slave;
391d7af6a48SSimon Glass struct udevice *dev;
392d7af6a48SSimon Glass int ret;
393d7af6a48SSimon Glass
394d7af6a48SSimon Glass ret = spi_get_bus_and_cs(busnum, cs, speed, mode, NULL, 0, &dev,
395d7af6a48SSimon Glass &slave);
396d7af6a48SSimon Glass if (ret)
397d7af6a48SSimon Glass return NULL;
398d7af6a48SSimon Glass
399d7af6a48SSimon Glass return slave;
400d7af6a48SSimon Glass }
401d7af6a48SSimon Glass
spi_free_slave(struct spi_slave * slave)402d7af6a48SSimon Glass void spi_free_slave(struct spi_slave *slave)
403d7af6a48SSimon Glass {
404706865afSStefan Roese device_remove(slave->dev, DM_REMOVE_NORMAL);
405d7af6a48SSimon Glass slave->dev = NULL;
406d7af6a48SSimon Glass }
407d7af6a48SSimon Glass
spi_slave_ofdata_to_platdata(struct udevice * dev,struct dm_spi_slave_platdata * plat)408279e26f5SSimon Glass int spi_slave_ofdata_to_platdata(struct udevice *dev,
409d0cff03eSSimon Glass struct dm_spi_slave_platdata *plat)
410d7af6a48SSimon Glass {
41108fe9c29SJagan Teki int mode = 0;
412f8e2f92dSMugunthan V N int value;
413d7af6a48SSimon Glass
414279e26f5SSimon Glass plat->cs = dev_read_u32_default(dev, "reg", -1);
41512bfb2e0SSimon Goldschmidt plat->max_hz = dev_read_u32_default(dev, "spi-max-frequency",
41612bfb2e0SSimon Goldschmidt SPI_DEFAULT_SPEED_HZ);
417279e26f5SSimon Glass if (dev_read_bool(dev, "spi-cpol"))
418d7af6a48SSimon Glass mode |= SPI_CPOL;
419279e26f5SSimon Glass if (dev_read_bool(dev, "spi-cpha"))
420d7af6a48SSimon Glass mode |= SPI_CPHA;
421279e26f5SSimon Glass if (dev_read_bool(dev, "spi-cs-high"))
422d7af6a48SSimon Glass mode |= SPI_CS_HIGH;
423279e26f5SSimon Glass if (dev_read_bool(dev, "spi-3wire"))
424379b49d8SJagan Teki mode |= SPI_3WIRE;
425279e26f5SSimon Glass if (dev_read_bool(dev, "spi-half-duplex"))
426d7af6a48SSimon Glass mode |= SPI_PREAMBLE;
427f8e2f92dSMugunthan V N
428f8e2f92dSMugunthan V N /* Device DUAL/QUAD mode */
429279e26f5SSimon Glass value = dev_read_u32_default(dev, "spi-tx-bus-width", 1);
430f8e2f92dSMugunthan V N switch (value) {
431f8e2f92dSMugunthan V N case 1:
432f8e2f92dSMugunthan V N break;
433f8e2f92dSMugunthan V N case 2:
434f8e2f92dSMugunthan V N mode |= SPI_TX_DUAL;
435f8e2f92dSMugunthan V N break;
436f8e2f92dSMugunthan V N case 4:
437f8e2f92dSMugunthan V N mode |= SPI_TX_QUAD;
438f8e2f92dSMugunthan V N break;
439f8e2f92dSMugunthan V N default:
4401b7c28f5SSimon Glass warn_non_spl("spi-tx-bus-width %d not supported\n", value);
441f8e2f92dSMugunthan V N break;
442f8e2f92dSMugunthan V N }
443f8e2f92dSMugunthan V N
444279e26f5SSimon Glass value = dev_read_u32_default(dev, "spi-rx-bus-width", 1);
445f8e2f92dSMugunthan V N switch (value) {
446f8e2f92dSMugunthan V N case 1:
447f8e2f92dSMugunthan V N break;
448f8e2f92dSMugunthan V N case 2:
44908fe9c29SJagan Teki mode |= SPI_RX_DUAL;
450f8e2f92dSMugunthan V N break;
451f8e2f92dSMugunthan V N case 4:
45208fe9c29SJagan Teki mode |= SPI_RX_QUAD;
453f8e2f92dSMugunthan V N break;
454f8e2f92dSMugunthan V N default:
4551b7c28f5SSimon Glass warn_non_spl("spi-rx-bus-width %d not supported\n", value);
456f8e2f92dSMugunthan V N break;
457f8e2f92dSMugunthan V N }
458f8e2f92dSMugunthan V N
45908fe9c29SJagan Teki plat->mode = mode;
460f8e2f92dSMugunthan V N
461d7af6a48SSimon Glass return 0;
462d7af6a48SSimon Glass }
463d7af6a48SSimon Glass
464d7af6a48SSimon Glass UCLASS_DRIVER(spi) = {
465d7af6a48SSimon Glass .id = UCLASS_SPI,
466d7af6a48SSimon Glass .name = "spi",
4679cc36a2bSSimon Glass .flags = DM_UC_FLAG_SEQ_ALIAS,
46871634f28SSimon Glass #if !CONFIG_IS_ENABLED(OF_PLATDATA)
46991195485SSimon Glass .post_bind = dm_scan_fdt_dev,
47071634f28SSimon Glass #endif
471d7af6a48SSimon Glass .post_probe = spi_post_probe,
472440714eeSSimon Glass .child_pre_probe = spi_child_pre_probe,
473d7af6a48SSimon Glass .per_device_auto_alloc_size = sizeof(struct dm_spi_bus),
47419a25f67SSimon Glass .per_child_auto_alloc_size = sizeof(struct spi_slave),
475d0cff03eSSimon Glass .per_child_platdata_auto_alloc_size =
476d0cff03eSSimon Glass sizeof(struct dm_spi_slave_platdata),
47771634f28SSimon Glass #if !CONFIG_IS_ENABLED(OF_PLATDATA)
478d0cff03eSSimon Glass .child_post_bind = spi_child_post_bind,
47971634f28SSimon Glass #endif
480d7af6a48SSimon Glass };
481d7af6a48SSimon Glass
482d7af6a48SSimon Glass UCLASS_DRIVER(spi_generic) = {
483d7af6a48SSimon Glass .id = UCLASS_SPI_GENERIC,
484d7af6a48SSimon Glass .name = "spi_generic",
485d7af6a48SSimon Glass };
486d7af6a48SSimon Glass
487d7af6a48SSimon Glass U_BOOT_DRIVER(spi_generic_drv) = {
488d7af6a48SSimon Glass .name = "spi_generic_drv",
489d7af6a48SSimon Glass .id = UCLASS_SPI_GENERIC,
490d7af6a48SSimon Glass };
491