blob: c00a05af7d1b4483989289a36530f250a6ac6b70 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Sekhar Nori4708b172010-01-27 11:10:40 -05002/*
3 * Copyright (C) 2009 Texas Instruments Incorporated - http://www.ti.com/
4 *
5 * Driver for SPI controller on DaVinci. Based on atmel_spi.c
6 * by Atmel Corporation
7 *
8 * Copyright (C) 2007 Atmel Corporation
Sekhar Nori4708b172010-01-27 11:10:40 -05009 */
Jagan Tekiccaa8fc2015-06-27 00:51:29 +053010
Sekhar Nori4708b172010-01-27 11:10:40 -050011#include <common.h>
Simon Glass0f2af882020-05-10 11:40:05 -060012#include <log.h>
Sekhar Nori4708b172010-01-27 11:10:40 -050013#include <spi.h>
14#include <malloc.h>
15#include <asm/io.h>
16#include <asm/arch/hardware.h>
Vignesh Rc1091e12016-07-06 09:58:56 +053017#include <dm.h>
Jagan Tekia147f662018-09-03 23:00:23 +053018#include <dm/platform_data/spi_davinci.h>
Simon Glassdbd79542020-05-10 11:40:11 -060019#include <linux/delay.h>
Jagan Teki742d53d2015-06-27 00:51:28 +053020
Jagan Teki742d53d2015-06-27 00:51:28 +053021/* SPIGCR0 */
22#define SPIGCR0_SPIENA_MASK 0x1
23#define SPIGCR0_SPIRST_MASK 0x0
24
25/* SPIGCR0 */
26#define SPIGCR1_CLKMOD_MASK BIT(1)
27#define SPIGCR1_MASTER_MASK BIT(0)
28#define SPIGCR1_SPIENA_MASK BIT(24)
29
30/* SPIPC0 */
31#define SPIPC0_DIFUN_MASK BIT(11) /* SIMO */
32#define SPIPC0_DOFUN_MASK BIT(10) /* SOMI */
33#define SPIPC0_CLKFUN_MASK BIT(9) /* CLK */
34#define SPIPC0_EN0FUN_MASK BIT(0)
35
36/* SPIFMT0 */
37#define SPIFMT_SHIFTDIR_SHIFT 20
38#define SPIFMT_POLARITY_SHIFT 17
39#define SPIFMT_PHASE_SHIFT 16
40#define SPIFMT_PRESCALE_SHIFT 8
41
42/* SPIDAT1 */
43#define SPIDAT1_CSHOLD_SHIFT 28
44#define SPIDAT1_CSNR_SHIFT 16
45
46/* SPIDELAY */
47#define SPI_C2TDELAY_SHIFT 24
48#define SPI_T2CDELAY_SHIFT 16
49
50/* SPIBUF */
51#define SPIBUF_RXEMPTY_MASK BIT(31)
52#define SPIBUF_TXFULL_MASK BIT(29)
53
54/* SPIDEF */
55#define SPIDEF_CSDEF0_MASK BIT(0)
56
Vignesh Rc1091e12016-07-06 09:58:56 +053057#ifndef CONFIG_DM_SPI
Jagan Teki742d53d2015-06-27 00:51:28 +053058#define SPI0_BUS 0
59#define SPI0_BASE CONFIG_SYS_SPI_BASE
60/*
61 * Define default SPI0_NUM_CS as 1 for existing platforms that uses this
62 * driver. Platform can configure number of CS using CONFIG_SYS_SPI0_NUM_CS
63 * if more than one CS is supported and by defining CONFIG_SYS_SPI0.
64 */
65#ifndef CONFIG_SYS_SPI0
66#define SPI0_NUM_CS 1
67#else
68#define SPI0_NUM_CS CONFIG_SYS_SPI0_NUM_CS
69#endif
70
71/*
72 * define CONFIG_SYS_SPI1 when platform has spi-1 device (bus #1) and
73 * CONFIG_SYS_SPI1_NUM_CS defines number of CS on this bus
74 */
75#ifdef CONFIG_SYS_SPI1
76#define SPI1_BUS 1
77#define SPI1_NUM_CS CONFIG_SYS_SPI1_NUM_CS
78#define SPI1_BASE CONFIG_SYS_SPI1_BASE
79#endif
80
81/*
82 * define CONFIG_SYS_SPI2 when platform has spi-2 device (bus #2) and
83 * CONFIG_SYS_SPI2_NUM_CS defines number of CS on this bus
84 */
85#ifdef CONFIG_SYS_SPI2
86#define SPI2_BUS 2
87#define SPI2_NUM_CS CONFIG_SYS_SPI2_NUM_CS
88#define SPI2_BASE CONFIG_SYS_SPI2_BASE
89#endif
Vignesh Rc1091e12016-07-06 09:58:56 +053090#endif
91
92DECLARE_GLOBAL_DATA_PTR;
Jagan Teki742d53d2015-06-27 00:51:28 +053093
Jagan Tekiccaa8fc2015-06-27 00:51:29 +053094/* davinci spi register set */
95struct davinci_spi_regs {
96 dv_reg gcr0; /* 0x00 */
97 dv_reg gcr1; /* 0x04 */
98 dv_reg int0; /* 0x08 */
99 dv_reg lvl; /* 0x0c */
100 dv_reg flg; /* 0x10 */
101 dv_reg pc0; /* 0x14 */
102 dv_reg pc1; /* 0x18 */
103 dv_reg pc2; /* 0x1c */
104 dv_reg pc3; /* 0x20 */
105 dv_reg pc4; /* 0x24 */
106 dv_reg pc5; /* 0x28 */
107 dv_reg rsvd[3];
108 dv_reg dat0; /* 0x38 */
109 dv_reg dat1; /* 0x3c */
110 dv_reg buf; /* 0x40 */
111 dv_reg emu; /* 0x44 */
112 dv_reg delay; /* 0x48 */
113 dv_reg def; /* 0x4c */
114 dv_reg fmt0; /* 0x50 */
115 dv_reg fmt1; /* 0x54 */
116 dv_reg fmt2; /* 0x58 */
117 dv_reg fmt3; /* 0x5c */
118 dv_reg intvec0; /* 0x60 */
119 dv_reg intvec1; /* 0x64 */
120};
121
122/* davinci spi slave */
Jagan Teki742d53d2015-06-27 00:51:28 +0530123struct davinci_spi_slave {
Vignesh Rc1091e12016-07-06 09:58:56 +0530124#ifndef CONFIG_DM_SPI
Jagan Teki742d53d2015-06-27 00:51:28 +0530125 struct spi_slave slave;
Vignesh Rc1091e12016-07-06 09:58:56 +0530126#endif
Jagan Teki742d53d2015-06-27 00:51:28 +0530127 struct davinci_spi_regs *regs;
Vignesh Rc1091e12016-07-06 09:58:56 +0530128 unsigned int freq; /* current SPI bus frequency */
129 unsigned int mode; /* current SPI mode used */
130 u8 num_cs; /* total no. of CS available */
131 u8 cur_cs; /* CS of current slave */
132 bool half_duplex; /* true, if master is half-duplex only */
Jagan Teki742d53d2015-06-27 00:51:28 +0530133};
134
Nick Thompson213e4762010-06-22 11:06:01 -0400135/*
136 * This functions needs to act like a macro to avoid pipeline reloads in the
137 * loops below. Use always_inline. This gains us about 160KiB/s and the bloat
138 * appears to be zero bytes (da830).
139 */
140__attribute__((always_inline))
141static inline u32 davinci_spi_xfer_data(struct davinci_spi_slave *ds, u32 data)
142{
143 u32 buf_reg_val;
144
145 /* send out data */
146 writel(data, &ds->regs->dat1);
147
148 /* wait for the data to clock in/out */
149 while ((buf_reg_val = readl(&ds->regs->buf)) & SPIBUF_RXEMPTY_MASK)
150 ;
151
152 return buf_reg_val;
153}
154
Vignesh Rc1091e12016-07-06 09:58:56 +0530155static int davinci_spi_read(struct davinci_spi_slave *ds, unsigned int len,
Nick Thompson213e4762010-06-22 11:06:01 -0400156 u8 *rxp, unsigned long flags)
157{
Nick Thompson213e4762010-06-22 11:06:01 -0400158 unsigned int data1_reg_val;
159
160 /* enable CS hold, CS[n] and clear the data bits */
161 data1_reg_val = ((1 << SPIDAT1_CSHOLD_SHIFT) |
Vignesh Rc1091e12016-07-06 09:58:56 +0530162 (ds->cur_cs << SPIDAT1_CSNR_SHIFT));
Nick Thompson213e4762010-06-22 11:06:01 -0400163
164 /* wait till TXFULL is deasserted */
165 while (readl(&ds->regs->buf) & SPIBUF_TXFULL_MASK)
166 ;
167
168 /* preload the TX buffer to avoid clock starvation */
169 writel(data1_reg_val, &ds->regs->dat1);
170
171 /* keep reading 1 byte until only 1 byte left */
172 while ((len--) > 1)
173 *rxp++ = davinci_spi_xfer_data(ds, data1_reg_val);
174
175 /* clear CS hold when we reach the end */
176 if (flags & SPI_XFER_END)
177 data1_reg_val &= ~(1 << SPIDAT1_CSHOLD_SHIFT);
178
179 /* read the last byte */
180 *rxp = davinci_spi_xfer_data(ds, data1_reg_val);
181
182 return 0;
183}
184
Vignesh Rc1091e12016-07-06 09:58:56 +0530185static int davinci_spi_write(struct davinci_spi_slave *ds, unsigned int len,
Nick Thompson4bbd91f2010-07-05 20:00:40 -0400186 const u8 *txp, unsigned long flags)
Nick Thompson213e4762010-06-22 11:06:01 -0400187{
Nick Thompson213e4762010-06-22 11:06:01 -0400188 unsigned int data1_reg_val;
189
190 /* enable CS hold and clear the data bits */
191 data1_reg_val = ((1 << SPIDAT1_CSHOLD_SHIFT) |
Vignesh Rc1091e12016-07-06 09:58:56 +0530192 (ds->cur_cs << SPIDAT1_CSNR_SHIFT));
Nick Thompson213e4762010-06-22 11:06:01 -0400193
194 /* wait till TXFULL is deasserted */
195 while (readl(&ds->regs->buf) & SPIBUF_TXFULL_MASK)
196 ;
197
198 /* preload the TX buffer to avoid clock starvation */
199 if (len > 2) {
200 writel(data1_reg_val | *txp++, &ds->regs->dat1);
201 len--;
202 }
203
204 /* keep writing 1 byte until only 1 byte left */
205 while ((len--) > 1)
206 davinci_spi_xfer_data(ds, data1_reg_val | *txp++);
207
208 /* clear CS hold when we reach the end */
209 if (flags & SPI_XFER_END)
210 data1_reg_val &= ~(1 << SPIDAT1_CSHOLD_SHIFT);
211
212 /* write the last byte */
213 davinci_spi_xfer_data(ds, data1_reg_val | *txp);
214
215 return 0;
216}
217
Vignesh Rc1091e12016-07-06 09:58:56 +0530218static int davinci_spi_read_write(struct davinci_spi_slave *ds, unsigned
219 int len, u8 *rxp, const u8 *txp,
220 unsigned long flags)
Nick Thompson213e4762010-06-22 11:06:01 -0400221{
Nick Thompson213e4762010-06-22 11:06:01 -0400222 unsigned int data1_reg_val;
223
224 /* enable CS hold and clear the data bits */
225 data1_reg_val = ((1 << SPIDAT1_CSHOLD_SHIFT) |
Vignesh Rc1091e12016-07-06 09:58:56 +0530226 (ds->cur_cs << SPIDAT1_CSNR_SHIFT));
Nick Thompson213e4762010-06-22 11:06:01 -0400227
228 /* wait till TXFULL is deasserted */
229 while (readl(&ds->regs->buf) & SPIBUF_TXFULL_MASK)
230 ;
231
232 /* keep reading and writing 1 byte until only 1 byte left */
233 while ((len--) > 1)
234 *rxp++ = davinci_spi_xfer_data(ds, data1_reg_val | *txp++);
235
236 /* clear CS hold when we reach the end */
237 if (flags & SPI_XFER_END)
238 data1_reg_val &= ~(1 << SPIDAT1_CSHOLD_SHIFT);
239
240 /* read and write the last byte */
241 *rxp = davinci_spi_xfer_data(ds, data1_reg_val | *txp);
242
243 return 0;
244}
Vignesh Rc1091e12016-07-06 09:58:56 +0530245
246
247static int __davinci_spi_claim_bus(struct davinci_spi_slave *ds, int cs)
248{
249 unsigned int mode = 0, scalar;
250
251 /* Enable the SPI hardware */
252 writel(SPIGCR0_SPIRST_MASK, &ds->regs->gcr0);
253 udelay(1000);
254 writel(SPIGCR0_SPIENA_MASK, &ds->regs->gcr0);
255
256 /* Set master mode, powered up and not activated */
257 writel(SPIGCR1_MASTER_MASK | SPIGCR1_CLKMOD_MASK, &ds->regs->gcr1);
258
259 /* CS, CLK, SIMO and SOMI are functional pins */
260 writel(((1 << cs) | SPIPC0_CLKFUN_MASK |
261 SPIPC0_DOFUN_MASK | SPIPC0_DIFUN_MASK), &ds->regs->pc0);
262
263 /* setup format */
264 scalar = ((CONFIG_SYS_SPI_CLK / ds->freq) - 1) & 0xFF;
265
266 /*
267 * Use following format:
268 * character length = 8,
269 * MSB shifted out first
270 */
271 if (ds->mode & SPI_CPOL)
272 mode |= SPI_CPOL;
273 if (!(ds->mode & SPI_CPHA))
274 mode |= SPI_CPHA;
275 writel(8 | (scalar << SPIFMT_PRESCALE_SHIFT) |
276 (mode << SPIFMT_PHASE_SHIFT), &ds->regs->fmt0);
277
278 /*
279 * Including a minor delay. No science here. Should be good even with
280 * no delay
281 */
282 writel((50 << SPI_C2TDELAY_SHIFT) |
283 (50 << SPI_T2CDELAY_SHIFT), &ds->regs->delay);
284
285 /* default chip select register */
286 writel(SPIDEF_CSDEF0_MASK, &ds->regs->def);
287
288 /* no interrupts */
289 writel(0, &ds->regs->int0);
290 writel(0, &ds->regs->lvl);
291
292 /* enable SPI */
293 writel((readl(&ds->regs->gcr1) | SPIGCR1_SPIENA_MASK), &ds->regs->gcr1);
294
295 return 0;
296}
297
298static int __davinci_spi_release_bus(struct davinci_spi_slave *ds)
299{
300 /* Disable the SPI hardware */
301 writel(SPIGCR0_SPIRST_MASK, &ds->regs->gcr0);
302
303 return 0;
304}
305
306static int __davinci_spi_xfer(struct davinci_spi_slave *ds,
307 unsigned int bitlen, const void *dout, void *din,
308 unsigned long flags)
309{
310 unsigned int len;
311
312 if (bitlen == 0)
313 /* Finish any previously submitted transfers */
314 goto out;
315
316 /*
317 * It's not clear how non-8-bit-aligned transfers are supposed to be
318 * represented as a stream of bytes...this is a limitation of
319 * the current SPI interface - here we terminate on receiving such a
320 * transfer request.
321 */
322 if (bitlen % 8) {
323 /* Errors always terminate an ongoing transfer */
324 flags |= SPI_XFER_END;
325 goto out;
326 }
327
328 len = bitlen / 8;
329
330 if (!dout)
331 return davinci_spi_read(ds, len, din, flags);
332 if (!din)
333 return davinci_spi_write(ds, len, dout, flags);
334 if (!ds->half_duplex)
335 return davinci_spi_read_write(ds, len, din, dout, flags);
336
337 printf("SPI full duplex not supported\n");
338 flags |= SPI_XFER_END;
339
340out:
341 if (flags & SPI_XFER_END) {
342 u8 dummy = 0;
343 davinci_spi_write(ds, 1, &dummy, flags);
344 }
345 return 0;
346}
347
348#ifndef CONFIG_DM_SPI
349
350static inline struct davinci_spi_slave *to_davinci_spi(struct spi_slave *slave)
351{
352 return container_of(slave, struct davinci_spi_slave, slave);
353}
Nick Thompson213e4762010-06-22 11:06:01 -0400354
Jagan Tekiccaa8fc2015-06-27 00:51:29 +0530355int spi_cs_is_valid(unsigned int bus, unsigned int cs)
356{
357 int ret = 0;
358
359 switch (bus) {
360 case SPI0_BUS:
361 if (cs < SPI0_NUM_CS)
362 ret = 1;
363 break;
364#ifdef CONFIG_SYS_SPI1
365 case SPI1_BUS:
366 if (cs < SPI1_NUM_CS)
367 ret = 1;
368 break;
369#endif
370#ifdef CONFIG_SYS_SPI2
371 case SPI2_BUS:
372 if (cs < SPI2_NUM_CS)
373 ret = 1;
374 break;
375#endif
376 default:
377 /* Invalid bus number. Do nothing */
378 break;
379 }
380 return ret;
381}
382
383void spi_cs_activate(struct spi_slave *slave)
384{
385 /* do nothing */
386}
387
388void spi_cs_deactivate(struct spi_slave *slave)
389{
390 /* do nothing */
391}
392
Jagan Tekiccaa8fc2015-06-27 00:51:29 +0530393struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
394 unsigned int max_hz, unsigned int mode)
395{
396 struct davinci_spi_slave *ds;
397
398 if (!spi_cs_is_valid(bus, cs))
399 return NULL;
400
401 ds = spi_alloc_slave(struct davinci_spi_slave, bus, cs);
402 if (!ds)
403 return NULL;
404
405 switch (bus) {
406 case SPI0_BUS:
407 ds->regs = (struct davinci_spi_regs *)SPI0_BASE;
408 break;
409#ifdef CONFIG_SYS_SPI1
410 case SPI1_BUS:
411 ds->regs = (struct davinci_spi_regs *)SPI1_BASE;
412 break;
413#endif
414#ifdef CONFIG_SYS_SPI2
415 case SPI2_BUS:
416 ds->regs = (struct davinci_spi_regs *)SPI2_BASE;
417 break;
418#endif
419 default: /* Invalid bus number */
420 return NULL;
421 }
422
423 ds->freq = max_hz;
Vignesh Rc1091e12016-07-06 09:58:56 +0530424 ds->mode = mode;
Jagan Tekiccaa8fc2015-06-27 00:51:29 +0530425
426 return &ds->slave;
427}
428
429void spi_free_slave(struct spi_slave *slave)
430{
431 struct davinci_spi_slave *ds = to_davinci_spi(slave);
432
433 free(ds);
434}
435
Vignesh Rc1091e12016-07-06 09:58:56 +0530436int spi_xfer(struct spi_slave *slave, unsigned int bitlen,
437 const void *dout, void *din, unsigned long flags)
438{
439 struct davinci_spi_slave *ds = to_davinci_spi(slave);
440
441 ds->cur_cs = slave->cs;
442
443 return __davinci_spi_xfer(ds, bitlen, dout, din, flags);
444}
445
Jagan Tekiccaa8fc2015-06-27 00:51:29 +0530446int spi_claim_bus(struct spi_slave *slave)
447{
448 struct davinci_spi_slave *ds = to_davinci_spi(slave);
Jagan Tekiccaa8fc2015-06-27 00:51:29 +0530449
Vignesh Rc1091e12016-07-06 09:58:56 +0530450#ifdef CONFIG_SPI_HALF_DUPLEX
451 ds->half_duplex = true;
452#else
453 ds->half_duplex = false;
454#endif
455 return __davinci_spi_claim_bus(ds, ds->slave.cs);
456}
Jagan Tekiccaa8fc2015-06-27 00:51:29 +0530457
Vignesh Rc1091e12016-07-06 09:58:56 +0530458void spi_release_bus(struct spi_slave *slave)
459{
460 struct davinci_spi_slave *ds = to_davinci_spi(slave);
Jagan Tekiccaa8fc2015-06-27 00:51:29 +0530461
Vignesh Rc1091e12016-07-06 09:58:56 +0530462 __davinci_spi_release_bus(ds);
463}
Jagan Tekiccaa8fc2015-06-27 00:51:29 +0530464
Vignesh Rc1091e12016-07-06 09:58:56 +0530465#else
466static int davinci_spi_set_speed(struct udevice *bus, uint max_hz)
467{
468 struct davinci_spi_slave *ds = dev_get_priv(bus);
Jagan Tekiccaa8fc2015-06-27 00:51:29 +0530469
Vignesh Rc1091e12016-07-06 09:58:56 +0530470 debug("%s speed %u\n", __func__, max_hz);
471 if (max_hz > CONFIG_SYS_SPI_CLK / 2)
472 return -EINVAL;
Jagan Tekiccaa8fc2015-06-27 00:51:29 +0530473
Vignesh Rc1091e12016-07-06 09:58:56 +0530474 ds->freq = max_hz;
Jagan Tekiccaa8fc2015-06-27 00:51:29 +0530475
Vignesh Rc1091e12016-07-06 09:58:56 +0530476 return 0;
477}
Jagan Tekiccaa8fc2015-06-27 00:51:29 +0530478
Vignesh Rc1091e12016-07-06 09:58:56 +0530479static int davinci_spi_set_mode(struct udevice *bus, uint mode)
480{
481 struct davinci_spi_slave *ds = dev_get_priv(bus);
Jagan Tekiccaa8fc2015-06-27 00:51:29 +0530482
Vignesh Rc1091e12016-07-06 09:58:56 +0530483 debug("%s mode %u\n", __func__, mode);
484 ds->mode = mode;
Jagan Tekiccaa8fc2015-06-27 00:51:29 +0530485
486 return 0;
487}
488
Vignesh Rc1091e12016-07-06 09:58:56 +0530489static int davinci_spi_claim_bus(struct udevice *dev)
Jagan Tekiccaa8fc2015-06-27 00:51:29 +0530490{
Vignesh Rc1091e12016-07-06 09:58:56 +0530491 struct dm_spi_slave_platdata *slave_plat =
492 dev_get_parent_platdata(dev);
493 struct udevice *bus = dev->parent;
494 struct davinci_spi_slave *ds = dev_get_priv(bus);
Jagan Tekiccaa8fc2015-06-27 00:51:29 +0530495
Vignesh Rc1091e12016-07-06 09:58:56 +0530496 if (slave_plat->cs >= ds->num_cs) {
497 printf("Invalid SPI chipselect\n");
498 return -EINVAL;
499 }
500 ds->half_duplex = slave_plat->mode & SPI_PREAMBLE;
501
502 return __davinci_spi_claim_bus(ds, slave_plat->cs);
Jagan Tekiccaa8fc2015-06-27 00:51:29 +0530503}
504
Vignesh Rc1091e12016-07-06 09:58:56 +0530505static int davinci_spi_release_bus(struct udevice *dev)
Sekhar Nori4708b172010-01-27 11:10:40 -0500506{
Vignesh Rc1091e12016-07-06 09:58:56 +0530507 struct davinci_spi_slave *ds = dev_get_priv(dev->parent);
Sekhar Nori4708b172010-01-27 11:10:40 -0500508
Vignesh Rc1091e12016-07-06 09:58:56 +0530509 return __davinci_spi_release_bus(ds);
510}
Sekhar Nori4708b172010-01-27 11:10:40 -0500511
Vignesh Rc1091e12016-07-06 09:58:56 +0530512static int davinci_spi_xfer(struct udevice *dev, unsigned int bitlen,
513 const void *dout, void *din,
514 unsigned long flags)
515{
516 struct dm_spi_slave_platdata *slave =
517 dev_get_parent_platdata(dev);
518 struct udevice *bus = dev->parent;
519 struct davinci_spi_slave *ds = dev_get_priv(bus);
520
521 if (slave->cs >= ds->num_cs) {
522 printf("Invalid SPI chipselect\n");
523 return -EINVAL;
Sekhar Nori4708b172010-01-27 11:10:40 -0500524 }
Vignesh Rc1091e12016-07-06 09:58:56 +0530525 ds->cur_cs = slave->cs;
Sekhar Nori4708b172010-01-27 11:10:40 -0500526
Vignesh Rc1091e12016-07-06 09:58:56 +0530527 return __davinci_spi_xfer(ds, bitlen, dout, din, flags);
528}
Sekhar Nori4708b172010-01-27 11:10:40 -0500529
Jagan Tekia147f662018-09-03 23:00:23 +0530530static const struct dm_spi_ops davinci_spi_ops = {
531 .claim_bus = davinci_spi_claim_bus,
532 .release_bus = davinci_spi_release_bus,
533 .xfer = davinci_spi_xfer,
534 .set_speed = davinci_spi_set_speed,
535 .set_mode = davinci_spi_set_mode,
536};
537
Vignesh Rc1091e12016-07-06 09:58:56 +0530538static int davinci_spi_probe(struct udevice *bus)
539{
Jagan Tekia147f662018-09-03 23:00:23 +0530540 struct davinci_spi_slave *ds = dev_get_priv(bus);
541 struct davinci_spi_platdata *plat = bus->platdata;
542 ds->regs = plat->regs;
543 ds->num_cs = plat->num_cs;
544
Vignesh Rc1091e12016-07-06 09:58:56 +0530545 return 0;
546}
Sekhar Nori4708b172010-01-27 11:10:40 -0500547
Jagan Tekia147f662018-09-03 23:00:23 +0530548#if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
Vignesh Rc1091e12016-07-06 09:58:56 +0530549static int davinci_ofdata_to_platadata(struct udevice *bus)
550{
Jagan Tekia147f662018-09-03 23:00:23 +0530551 struct davinci_spi_platdata *plat = bus->platdata;
552 fdt_addr_t addr;
Vignesh Rc1091e12016-07-06 09:58:56 +0530553
Jagan Tekia147f662018-09-03 23:00:23 +0530554 addr = devfdt_get_addr(bus);
555 if (addr == FDT_ADDR_T_NONE)
Vignesh Rc1091e12016-07-06 09:58:56 +0530556 return -EINVAL;
Jagan Tekia147f662018-09-03 23:00:23 +0530557
558 plat->regs = (struct davinci_spi_regs *)addr;
559 plat->num_cs = fdtdec_get_int(gd->fdt_blob, dev_of_offset(bus), "num-cs", 4);
Vignesh Rc1091e12016-07-06 09:58:56 +0530560
Sekhar Nori4708b172010-01-27 11:10:40 -0500561 return 0;
562}
Vignesh Rc1091e12016-07-06 09:58:56 +0530563
Vignesh Rc1091e12016-07-06 09:58:56 +0530564static const struct udevice_id davinci_spi_ids[] = {
565 { .compatible = "ti,keystone-spi" },
566 { .compatible = "ti,dm6441-spi" },
Adam Forda2d2f562017-09-17 20:43:45 -0500567 { .compatible = "ti,da830-spi" },
Vignesh Rc1091e12016-07-06 09:58:56 +0530568 { }
569};
Jagan Tekia147f662018-09-03 23:00:23 +0530570#endif
Vignesh Rc1091e12016-07-06 09:58:56 +0530571
572U_BOOT_DRIVER(davinci_spi) = {
573 .name = "davinci_spi",
574 .id = UCLASS_SPI,
Jagan Tekia147f662018-09-03 23:00:23 +0530575#if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
Vignesh Rc1091e12016-07-06 09:58:56 +0530576 .of_match = davinci_spi_ids,
Vignesh Rc1091e12016-07-06 09:58:56 +0530577 .ofdata_to_platdata = davinci_ofdata_to_platadata,
Jagan Tekia147f662018-09-03 23:00:23 +0530578 .platdata_auto_alloc_size = sizeof(struct davinci_spi_platdata),
579#endif
Vignesh Rc1091e12016-07-06 09:58:56 +0530580 .probe = davinci_spi_probe,
Jagan Tekia147f662018-09-03 23:00:23 +0530581 .ops = &davinci_spi_ops,
582 .priv_auto_alloc_size = sizeof(struct davinci_spi_slave),
Vignesh Rc1091e12016-07-06 09:58:56 +0530583};
584#endif