1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2009 Texas Instruments Incorporated - http://www.ti.com/
5 * Driver for SPI controller on DaVinci. Based on atmel_spi.c
8 * Copyright (C) 2007 Atmel Corporation
16 #include <asm/arch/hardware.h>
18 #include <dm/platform_data/spi_davinci.h>
19 #include <linux/bitops.h>
20 #include <linux/delay.h>
23 #define SPIGCR0_SPIENA_MASK 0x1
24 #define SPIGCR0_SPIRST_MASK 0x0
27 #define SPIGCR1_CLKMOD_MASK BIT(1)
28 #define SPIGCR1_MASTER_MASK BIT(0)
29 #define SPIGCR1_SPIENA_MASK BIT(24)
32 #define SPIPC0_DIFUN_MASK BIT(11) /* SIMO */
33 #define SPIPC0_DOFUN_MASK BIT(10) /* SOMI */
34 #define SPIPC0_CLKFUN_MASK BIT(9) /* CLK */
35 #define SPIPC0_EN0FUN_MASK BIT(0)
38 #define SPIFMT_SHIFTDIR_SHIFT 20
39 #define SPIFMT_POLARITY_SHIFT 17
40 #define SPIFMT_PHASE_SHIFT 16
41 #define SPIFMT_PRESCALE_SHIFT 8
44 #define SPIDAT1_CSHOLD_SHIFT 28
45 #define SPIDAT1_CSNR_SHIFT 16
48 #define SPI_C2TDELAY_SHIFT 24
49 #define SPI_T2CDELAY_SHIFT 16
52 #define SPIBUF_RXEMPTY_MASK BIT(31)
53 #define SPIBUF_TXFULL_MASK BIT(29)
56 #define SPIDEF_CSDEF0_MASK BIT(0)
60 #define SPI0_BASE CONFIG_SYS_SPI_BASE
62 * Define default SPI0_NUM_CS as 1 for existing platforms that uses this
63 * driver. Platform can configure number of CS using CONFIG_SYS_SPI0_NUM_CS
64 * if more than one CS is supported and by defining CONFIG_SYS_SPI0.
66 #ifndef CONFIG_SYS_SPI0
69 #define SPI0_NUM_CS CONFIG_SYS_SPI0_NUM_CS
73 * define CONFIG_SYS_SPI1 when platform has spi-1 device (bus #1) and
74 * CONFIG_SYS_SPI1_NUM_CS defines number of CS on this bus
76 #ifdef CONFIG_SYS_SPI1
78 #define SPI1_NUM_CS CONFIG_SYS_SPI1_NUM_CS
79 #define SPI1_BASE CONFIG_SYS_SPI1_BASE
83 * define CONFIG_SYS_SPI2 when platform has spi-2 device (bus #2) and
84 * CONFIG_SYS_SPI2_NUM_CS defines number of CS on this bus
86 #ifdef CONFIG_SYS_SPI2
88 #define SPI2_NUM_CS CONFIG_SYS_SPI2_NUM_CS
89 #define SPI2_BASE CONFIG_SYS_SPI2_BASE
93 DECLARE_GLOBAL_DATA_PTR;
95 /* davinci spi register set */
96 struct davinci_spi_regs {
97 dv_reg gcr0; /* 0x00 */
98 dv_reg gcr1; /* 0x04 */
99 dv_reg int0; /* 0x08 */
100 dv_reg lvl; /* 0x0c */
101 dv_reg flg; /* 0x10 */
102 dv_reg pc0; /* 0x14 */
103 dv_reg pc1; /* 0x18 */
104 dv_reg pc2; /* 0x1c */
105 dv_reg pc3; /* 0x20 */
106 dv_reg pc4; /* 0x24 */
107 dv_reg pc5; /* 0x28 */
109 dv_reg dat0; /* 0x38 */
110 dv_reg dat1; /* 0x3c */
111 dv_reg buf; /* 0x40 */
112 dv_reg emu; /* 0x44 */
113 dv_reg delay; /* 0x48 */
114 dv_reg def; /* 0x4c */
115 dv_reg fmt0; /* 0x50 */
116 dv_reg fmt1; /* 0x54 */
117 dv_reg fmt2; /* 0x58 */
118 dv_reg fmt3; /* 0x5c */
119 dv_reg intvec0; /* 0x60 */
120 dv_reg intvec1; /* 0x64 */
123 /* davinci spi slave */
124 struct davinci_spi_slave {
125 #ifndef CONFIG_DM_SPI
126 struct spi_slave slave;
128 struct davinci_spi_regs *regs;
129 unsigned int freq; /* current SPI bus frequency */
130 unsigned int mode; /* current SPI mode used */
131 u8 num_cs; /* total no. of CS available */
132 u8 cur_cs; /* CS of current slave */
133 bool half_duplex; /* true, if master is half-duplex only */
137 * This functions needs to act like a macro to avoid pipeline reloads in the
138 * loops below. Use always_inline. This gains us about 160KiB/s and the bloat
139 * appears to be zero bytes (da830).
141 __attribute__((always_inline))
142 static inline u32 davinci_spi_xfer_data(struct davinci_spi_slave *ds, u32 data)
147 writel(data, &ds->regs->dat1);
149 /* wait for the data to clock in/out */
150 while ((buf_reg_val = readl(&ds->regs->buf)) & SPIBUF_RXEMPTY_MASK)
156 static int davinci_spi_read(struct davinci_spi_slave *ds, unsigned int len,
157 u8 *rxp, unsigned long flags)
159 unsigned int data1_reg_val;
161 /* enable CS hold, CS[n] and clear the data bits */
162 data1_reg_val = ((1 << SPIDAT1_CSHOLD_SHIFT) |
163 (ds->cur_cs << SPIDAT1_CSNR_SHIFT));
165 /* wait till TXFULL is deasserted */
166 while (readl(&ds->regs->buf) & SPIBUF_TXFULL_MASK)
169 /* preload the TX buffer to avoid clock starvation */
170 writel(data1_reg_val, &ds->regs->dat1);
172 /* keep reading 1 byte until only 1 byte left */
174 *rxp++ = davinci_spi_xfer_data(ds, data1_reg_val);
176 /* clear CS hold when we reach the end */
177 if (flags & SPI_XFER_END)
178 data1_reg_val &= ~(1 << SPIDAT1_CSHOLD_SHIFT);
180 /* read the last byte */
181 *rxp = davinci_spi_xfer_data(ds, data1_reg_val);
186 static int davinci_spi_write(struct davinci_spi_slave *ds, unsigned int len,
187 const u8 *txp, unsigned long flags)
189 unsigned int data1_reg_val;
191 /* enable CS hold and clear the data bits */
192 data1_reg_val = ((1 << SPIDAT1_CSHOLD_SHIFT) |
193 (ds->cur_cs << SPIDAT1_CSNR_SHIFT));
195 /* wait till TXFULL is deasserted */
196 while (readl(&ds->regs->buf) & SPIBUF_TXFULL_MASK)
199 /* preload the TX buffer to avoid clock starvation */
201 writel(data1_reg_val | *txp++, &ds->regs->dat1);
205 /* keep writing 1 byte until only 1 byte left */
207 davinci_spi_xfer_data(ds, data1_reg_val | *txp++);
209 /* clear CS hold when we reach the end */
210 if (flags & SPI_XFER_END)
211 data1_reg_val &= ~(1 << SPIDAT1_CSHOLD_SHIFT);
213 /* write the last byte */
214 davinci_spi_xfer_data(ds, data1_reg_val | *txp);
219 static int davinci_spi_read_write(struct davinci_spi_slave *ds, unsigned
220 int len, u8 *rxp, const u8 *txp,
223 unsigned int data1_reg_val;
225 /* enable CS hold and clear the data bits */
226 data1_reg_val = ((1 << SPIDAT1_CSHOLD_SHIFT) |
227 (ds->cur_cs << SPIDAT1_CSNR_SHIFT));
229 /* wait till TXFULL is deasserted */
230 while (readl(&ds->regs->buf) & SPIBUF_TXFULL_MASK)
233 /* keep reading and writing 1 byte until only 1 byte left */
235 *rxp++ = davinci_spi_xfer_data(ds, data1_reg_val | *txp++);
237 /* clear CS hold when we reach the end */
238 if (flags & SPI_XFER_END)
239 data1_reg_val &= ~(1 << SPIDAT1_CSHOLD_SHIFT);
241 /* read and write the last byte */
242 *rxp = davinci_spi_xfer_data(ds, data1_reg_val | *txp);
248 static int __davinci_spi_claim_bus(struct davinci_spi_slave *ds, int cs)
250 unsigned int mode = 0, scalar;
252 /* Enable the SPI hardware */
253 writel(SPIGCR0_SPIRST_MASK, &ds->regs->gcr0);
255 writel(SPIGCR0_SPIENA_MASK, &ds->regs->gcr0);
257 /* Set master mode, powered up and not activated */
258 writel(SPIGCR1_MASTER_MASK | SPIGCR1_CLKMOD_MASK, &ds->regs->gcr1);
260 /* CS, CLK, SIMO and SOMI are functional pins */
261 writel(((1 << cs) | SPIPC0_CLKFUN_MASK |
262 SPIPC0_DOFUN_MASK | SPIPC0_DIFUN_MASK), &ds->regs->pc0);
265 scalar = ((CONFIG_SYS_SPI_CLK / ds->freq) - 1) & 0xFF;
268 * Use following format:
269 * character length = 8,
270 * MSB shifted out first
272 if (ds->mode & SPI_CPOL)
274 if (!(ds->mode & SPI_CPHA))
276 writel(8 | (scalar << SPIFMT_PRESCALE_SHIFT) |
277 (mode << SPIFMT_PHASE_SHIFT), &ds->regs->fmt0);
280 * Including a minor delay. No science here. Should be good even with
283 writel((50 << SPI_C2TDELAY_SHIFT) |
284 (50 << SPI_T2CDELAY_SHIFT), &ds->regs->delay);
286 /* default chip select register */
287 writel(SPIDEF_CSDEF0_MASK, &ds->regs->def);
290 writel(0, &ds->regs->int0);
291 writel(0, &ds->regs->lvl);
294 writel((readl(&ds->regs->gcr1) | SPIGCR1_SPIENA_MASK), &ds->regs->gcr1);
299 static int __davinci_spi_release_bus(struct davinci_spi_slave *ds)
301 /* Disable the SPI hardware */
302 writel(SPIGCR0_SPIRST_MASK, &ds->regs->gcr0);
307 static int __davinci_spi_xfer(struct davinci_spi_slave *ds,
308 unsigned int bitlen, const void *dout, void *din,
314 /* Finish any previously submitted transfers */
318 * It's not clear how non-8-bit-aligned transfers are supposed to be
319 * represented as a stream of bytes...this is a limitation of
320 * the current SPI interface - here we terminate on receiving such a
324 /* Errors always terminate an ongoing transfer */
325 flags |= SPI_XFER_END;
332 return davinci_spi_read(ds, len, din, flags);
334 return davinci_spi_write(ds, len, dout, flags);
335 if (!ds->half_duplex)
336 return davinci_spi_read_write(ds, len, din, dout, flags);
338 printf("SPI full duplex not supported\n");
339 flags |= SPI_XFER_END;
342 if (flags & SPI_XFER_END) {
344 davinci_spi_write(ds, 1, &dummy, flags);
349 #ifndef CONFIG_DM_SPI
351 static inline struct davinci_spi_slave *to_davinci_spi(struct spi_slave *slave)
353 return container_of(slave, struct davinci_spi_slave, slave);
356 int spi_cs_is_valid(unsigned int bus, unsigned int cs)
362 if (cs < SPI0_NUM_CS)
365 #ifdef CONFIG_SYS_SPI1
367 if (cs < SPI1_NUM_CS)
371 #ifdef CONFIG_SYS_SPI2
373 if (cs < SPI2_NUM_CS)
378 /* Invalid bus number. Do nothing */
384 void spi_cs_activate(struct spi_slave *slave)
389 void spi_cs_deactivate(struct spi_slave *slave)
394 struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
395 unsigned int max_hz, unsigned int mode)
397 struct davinci_spi_slave *ds;
399 if (!spi_cs_is_valid(bus, cs))
402 ds = spi_alloc_slave(struct davinci_spi_slave, bus, cs);
408 ds->regs = (struct davinci_spi_regs *)SPI0_BASE;
410 #ifdef CONFIG_SYS_SPI1
412 ds->regs = (struct davinci_spi_regs *)SPI1_BASE;
415 #ifdef CONFIG_SYS_SPI2
417 ds->regs = (struct davinci_spi_regs *)SPI2_BASE;
420 default: /* Invalid bus number */
430 void spi_free_slave(struct spi_slave *slave)
432 struct davinci_spi_slave *ds = to_davinci_spi(slave);
437 int spi_xfer(struct spi_slave *slave, unsigned int bitlen,
438 const void *dout, void *din, unsigned long flags)
440 struct davinci_spi_slave *ds = to_davinci_spi(slave);
442 ds->cur_cs = slave->cs;
444 return __davinci_spi_xfer(ds, bitlen, dout, din, flags);
447 int spi_claim_bus(struct spi_slave *slave)
449 struct davinci_spi_slave *ds = to_davinci_spi(slave);
451 #ifdef CONFIG_SPI_HALF_DUPLEX
452 ds->half_duplex = true;
454 ds->half_duplex = false;
456 return __davinci_spi_claim_bus(ds, ds->slave.cs);
459 void spi_release_bus(struct spi_slave *slave)
461 struct davinci_spi_slave *ds = to_davinci_spi(slave);
463 __davinci_spi_release_bus(ds);
467 static int davinci_spi_set_speed(struct udevice *bus, uint max_hz)
469 struct davinci_spi_slave *ds = dev_get_priv(bus);
471 debug("%s speed %u\n", __func__, max_hz);
472 if (max_hz > CONFIG_SYS_SPI_CLK / 2)
480 static int davinci_spi_set_mode(struct udevice *bus, uint mode)
482 struct davinci_spi_slave *ds = dev_get_priv(bus);
484 debug("%s mode %u\n", __func__, mode);
490 static int davinci_spi_claim_bus(struct udevice *dev)
492 struct dm_spi_slave_platdata *slave_plat =
493 dev_get_parent_platdata(dev);
494 struct udevice *bus = dev->parent;
495 struct davinci_spi_slave *ds = dev_get_priv(bus);
497 if (slave_plat->cs >= ds->num_cs) {
498 printf("Invalid SPI chipselect\n");
501 ds->half_duplex = slave_plat->mode & SPI_PREAMBLE;
503 return __davinci_spi_claim_bus(ds, slave_plat->cs);
506 static int davinci_spi_release_bus(struct udevice *dev)
508 struct davinci_spi_slave *ds = dev_get_priv(dev->parent);
510 return __davinci_spi_release_bus(ds);
513 static int davinci_spi_xfer(struct udevice *dev, unsigned int bitlen,
514 const void *dout, void *din,
517 struct dm_spi_slave_platdata *slave =
518 dev_get_parent_platdata(dev);
519 struct udevice *bus = dev->parent;
520 struct davinci_spi_slave *ds = dev_get_priv(bus);
522 if (slave->cs >= ds->num_cs) {
523 printf("Invalid SPI chipselect\n");
526 ds->cur_cs = slave->cs;
528 return __davinci_spi_xfer(ds, bitlen, dout, din, flags);
531 static const struct dm_spi_ops davinci_spi_ops = {
532 .claim_bus = davinci_spi_claim_bus,
533 .release_bus = davinci_spi_release_bus,
534 .xfer = davinci_spi_xfer,
535 .set_speed = davinci_spi_set_speed,
536 .set_mode = davinci_spi_set_mode,
539 static int davinci_spi_probe(struct udevice *bus)
541 struct davinci_spi_slave *ds = dev_get_priv(bus);
542 struct davinci_spi_platdata *plat = bus->platdata;
543 ds->regs = plat->regs;
544 ds->num_cs = plat->num_cs;
549 #if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
550 static int davinci_ofdata_to_platadata(struct udevice *bus)
552 struct davinci_spi_platdata *plat = bus->platdata;
555 addr = devfdt_get_addr(bus);
556 if (addr == FDT_ADDR_T_NONE)
559 plat->regs = (struct davinci_spi_regs *)addr;
560 plat->num_cs = fdtdec_get_int(gd->fdt_blob, dev_of_offset(bus), "num-cs", 4);
565 static const struct udevice_id davinci_spi_ids[] = {
566 { .compatible = "ti,keystone-spi" },
567 { .compatible = "ti,dm6441-spi" },
568 { .compatible = "ti,da830-spi" },
573 U_BOOT_DRIVER(davinci_spi) = {
574 .name = "davinci_spi",
576 #if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
577 .of_match = davinci_spi_ids,
578 .ofdata_to_platdata = davinci_ofdata_to_platadata,
579 .platdata_auto_alloc_size = sizeof(struct davinci_spi_platdata),
581 .probe = davinci_spi_probe,
582 .ops = &davinci_spi_ops,
583 .priv_auto_alloc_size = sizeof(struct davinci_spi_slave),