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
15 #include <asm/arch/hardware.h>
19 #define SPIGCR0_SPIENA_MASK 0x1
20 #define SPIGCR0_SPIRST_MASK 0x0
23 #define SPIGCR1_CLKMOD_MASK BIT(1)
24 #define SPIGCR1_MASTER_MASK BIT(0)
25 #define SPIGCR1_SPIENA_MASK BIT(24)
28 #define SPIPC0_DIFUN_MASK BIT(11) /* SIMO */
29 #define SPIPC0_DOFUN_MASK BIT(10) /* SOMI */
30 #define SPIPC0_CLKFUN_MASK BIT(9) /* CLK */
31 #define SPIPC0_EN0FUN_MASK BIT(0)
34 #define SPIFMT_SHIFTDIR_SHIFT 20
35 #define SPIFMT_POLARITY_SHIFT 17
36 #define SPIFMT_PHASE_SHIFT 16
37 #define SPIFMT_PRESCALE_SHIFT 8
40 #define SPIDAT1_CSHOLD_SHIFT 28
41 #define SPIDAT1_CSNR_SHIFT 16
44 #define SPI_C2TDELAY_SHIFT 24
45 #define SPI_T2CDELAY_SHIFT 16
48 #define SPIBUF_RXEMPTY_MASK BIT(31)
49 #define SPIBUF_TXFULL_MASK BIT(29)
52 #define SPIDEF_CSDEF0_MASK BIT(0)
56 #define SPI0_BASE CONFIG_SYS_SPI_BASE
58 * Define default SPI0_NUM_CS as 1 for existing platforms that uses this
59 * driver. Platform can configure number of CS using CONFIG_SYS_SPI0_NUM_CS
60 * if more than one CS is supported and by defining CONFIG_SYS_SPI0.
62 #ifndef CONFIG_SYS_SPI0
65 #define SPI0_NUM_CS CONFIG_SYS_SPI0_NUM_CS
69 * define CONFIG_SYS_SPI1 when platform has spi-1 device (bus #1) and
70 * CONFIG_SYS_SPI1_NUM_CS defines number of CS on this bus
72 #ifdef CONFIG_SYS_SPI1
74 #define SPI1_NUM_CS CONFIG_SYS_SPI1_NUM_CS
75 #define SPI1_BASE CONFIG_SYS_SPI1_BASE
79 * define CONFIG_SYS_SPI2 when platform has spi-2 device (bus #2) and
80 * CONFIG_SYS_SPI2_NUM_CS defines number of CS on this bus
82 #ifdef CONFIG_SYS_SPI2
84 #define SPI2_NUM_CS CONFIG_SYS_SPI2_NUM_CS
85 #define SPI2_BASE CONFIG_SYS_SPI2_BASE
89 DECLARE_GLOBAL_DATA_PTR;
91 /* davinci spi register set */
92 struct davinci_spi_regs {
93 dv_reg gcr0; /* 0x00 */
94 dv_reg gcr1; /* 0x04 */
95 dv_reg int0; /* 0x08 */
96 dv_reg lvl; /* 0x0c */
97 dv_reg flg; /* 0x10 */
98 dv_reg pc0; /* 0x14 */
99 dv_reg pc1; /* 0x18 */
100 dv_reg pc2; /* 0x1c */
101 dv_reg pc3; /* 0x20 */
102 dv_reg pc4; /* 0x24 */
103 dv_reg pc5; /* 0x28 */
105 dv_reg dat0; /* 0x38 */
106 dv_reg dat1; /* 0x3c */
107 dv_reg buf; /* 0x40 */
108 dv_reg emu; /* 0x44 */
109 dv_reg delay; /* 0x48 */
110 dv_reg def; /* 0x4c */
111 dv_reg fmt0; /* 0x50 */
112 dv_reg fmt1; /* 0x54 */
113 dv_reg fmt2; /* 0x58 */
114 dv_reg fmt3; /* 0x5c */
115 dv_reg intvec0; /* 0x60 */
116 dv_reg intvec1; /* 0x64 */
119 /* davinci spi slave */
120 struct davinci_spi_slave {
121 #ifndef CONFIG_DM_SPI
122 struct spi_slave slave;
124 struct davinci_spi_regs *regs;
125 unsigned int freq; /* current SPI bus frequency */
126 unsigned int mode; /* current SPI mode used */
127 u8 num_cs; /* total no. of CS available */
128 u8 cur_cs; /* CS of current slave */
129 bool half_duplex; /* true, if master is half-duplex only */
133 * This functions needs to act like a macro to avoid pipeline reloads in the
134 * loops below. Use always_inline. This gains us about 160KiB/s and the bloat
135 * appears to be zero bytes (da830).
137 __attribute__((always_inline))
138 static inline u32 davinci_spi_xfer_data(struct davinci_spi_slave *ds, u32 data)
143 writel(data, &ds->regs->dat1);
145 /* wait for the data to clock in/out */
146 while ((buf_reg_val = readl(&ds->regs->buf)) & SPIBUF_RXEMPTY_MASK)
152 static int davinci_spi_read(struct davinci_spi_slave *ds, unsigned int len,
153 u8 *rxp, unsigned long flags)
155 unsigned int data1_reg_val;
157 /* enable CS hold, CS[n] and clear the data bits */
158 data1_reg_val = ((1 << SPIDAT1_CSHOLD_SHIFT) |
159 (ds->cur_cs << SPIDAT1_CSNR_SHIFT));
161 /* wait till TXFULL is deasserted */
162 while (readl(&ds->regs->buf) & SPIBUF_TXFULL_MASK)
165 /* preload the TX buffer to avoid clock starvation */
166 writel(data1_reg_val, &ds->regs->dat1);
168 /* keep reading 1 byte until only 1 byte left */
170 *rxp++ = davinci_spi_xfer_data(ds, data1_reg_val);
172 /* clear CS hold when we reach the end */
173 if (flags & SPI_XFER_END)
174 data1_reg_val &= ~(1 << SPIDAT1_CSHOLD_SHIFT);
176 /* read the last byte */
177 *rxp = davinci_spi_xfer_data(ds, data1_reg_val);
182 static int davinci_spi_write(struct davinci_spi_slave *ds, unsigned int len,
183 const u8 *txp, unsigned long flags)
185 unsigned int data1_reg_val;
187 /* enable CS hold and clear the data bits */
188 data1_reg_val = ((1 << SPIDAT1_CSHOLD_SHIFT) |
189 (ds->cur_cs << SPIDAT1_CSNR_SHIFT));
191 /* wait till TXFULL is deasserted */
192 while (readl(&ds->regs->buf) & SPIBUF_TXFULL_MASK)
195 /* preload the TX buffer to avoid clock starvation */
197 writel(data1_reg_val | *txp++, &ds->regs->dat1);
201 /* keep writing 1 byte until only 1 byte left */
203 davinci_spi_xfer_data(ds, data1_reg_val | *txp++);
205 /* clear CS hold when we reach the end */
206 if (flags & SPI_XFER_END)
207 data1_reg_val &= ~(1 << SPIDAT1_CSHOLD_SHIFT);
209 /* write the last byte */
210 davinci_spi_xfer_data(ds, data1_reg_val | *txp);
215 static int davinci_spi_read_write(struct davinci_spi_slave *ds, unsigned
216 int len, u8 *rxp, const u8 *txp,
219 unsigned int data1_reg_val;
221 /* enable CS hold and clear the data bits */
222 data1_reg_val = ((1 << SPIDAT1_CSHOLD_SHIFT) |
223 (ds->cur_cs << SPIDAT1_CSNR_SHIFT));
225 /* wait till TXFULL is deasserted */
226 while (readl(&ds->regs->buf) & SPIBUF_TXFULL_MASK)
229 /* keep reading and writing 1 byte until only 1 byte left */
231 *rxp++ = davinci_spi_xfer_data(ds, data1_reg_val | *txp++);
233 /* clear CS hold when we reach the end */
234 if (flags & SPI_XFER_END)
235 data1_reg_val &= ~(1 << SPIDAT1_CSHOLD_SHIFT);
237 /* read and write the last byte */
238 *rxp = davinci_spi_xfer_data(ds, data1_reg_val | *txp);
244 static int __davinci_spi_claim_bus(struct davinci_spi_slave *ds, int cs)
246 unsigned int mode = 0, scalar;
248 /* Enable the SPI hardware */
249 writel(SPIGCR0_SPIRST_MASK, &ds->regs->gcr0);
251 writel(SPIGCR0_SPIENA_MASK, &ds->regs->gcr0);
253 /* Set master mode, powered up and not activated */
254 writel(SPIGCR1_MASTER_MASK | SPIGCR1_CLKMOD_MASK, &ds->regs->gcr1);
256 /* CS, CLK, SIMO and SOMI are functional pins */
257 writel(((1 << cs) | SPIPC0_CLKFUN_MASK |
258 SPIPC0_DOFUN_MASK | SPIPC0_DIFUN_MASK), &ds->regs->pc0);
261 scalar = ((CONFIG_SYS_SPI_CLK / ds->freq) - 1) & 0xFF;
264 * Use following format:
265 * character length = 8,
266 * MSB shifted out first
268 if (ds->mode & SPI_CPOL)
270 if (!(ds->mode & SPI_CPHA))
272 writel(8 | (scalar << SPIFMT_PRESCALE_SHIFT) |
273 (mode << SPIFMT_PHASE_SHIFT), &ds->regs->fmt0);
276 * Including a minor delay. No science here. Should be good even with
279 writel((50 << SPI_C2TDELAY_SHIFT) |
280 (50 << SPI_T2CDELAY_SHIFT), &ds->regs->delay);
282 /* default chip select register */
283 writel(SPIDEF_CSDEF0_MASK, &ds->regs->def);
286 writel(0, &ds->regs->int0);
287 writel(0, &ds->regs->lvl);
290 writel((readl(&ds->regs->gcr1) | SPIGCR1_SPIENA_MASK), &ds->regs->gcr1);
295 static int __davinci_spi_release_bus(struct davinci_spi_slave *ds)
297 /* Disable the SPI hardware */
298 writel(SPIGCR0_SPIRST_MASK, &ds->regs->gcr0);
303 static int __davinci_spi_xfer(struct davinci_spi_slave *ds,
304 unsigned int bitlen, const void *dout, void *din,
310 /* Finish any previously submitted transfers */
314 * It's not clear how non-8-bit-aligned transfers are supposed to be
315 * represented as a stream of bytes...this is a limitation of
316 * the current SPI interface - here we terminate on receiving such a
320 /* Errors always terminate an ongoing transfer */
321 flags |= SPI_XFER_END;
328 return davinci_spi_read(ds, len, din, flags);
330 return davinci_spi_write(ds, len, dout, flags);
331 if (!ds->half_duplex)
332 return davinci_spi_read_write(ds, len, din, dout, flags);
334 printf("SPI full duplex not supported\n");
335 flags |= SPI_XFER_END;
338 if (flags & SPI_XFER_END) {
340 davinci_spi_write(ds, 1, &dummy, flags);
345 #ifndef CONFIG_DM_SPI
347 static inline struct davinci_spi_slave *to_davinci_spi(struct spi_slave *slave)
349 return container_of(slave, struct davinci_spi_slave, slave);
352 int spi_cs_is_valid(unsigned int bus, unsigned int cs)
358 if (cs < SPI0_NUM_CS)
361 #ifdef CONFIG_SYS_SPI1
363 if (cs < SPI1_NUM_CS)
367 #ifdef CONFIG_SYS_SPI2
369 if (cs < SPI2_NUM_CS)
374 /* Invalid bus number. Do nothing */
380 void spi_cs_activate(struct spi_slave *slave)
385 void spi_cs_deactivate(struct spi_slave *slave)
395 struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
396 unsigned int max_hz, unsigned int mode)
398 struct davinci_spi_slave *ds;
400 if (!spi_cs_is_valid(bus, cs))
403 ds = spi_alloc_slave(struct davinci_spi_slave, bus, cs);
409 ds->regs = (struct davinci_spi_regs *)SPI0_BASE;
411 #ifdef CONFIG_SYS_SPI1
413 ds->regs = (struct davinci_spi_regs *)SPI1_BASE;
416 #ifdef CONFIG_SYS_SPI2
418 ds->regs = (struct davinci_spi_regs *)SPI2_BASE;
421 default: /* Invalid bus number */
431 void spi_free_slave(struct spi_slave *slave)
433 struct davinci_spi_slave *ds = to_davinci_spi(slave);
438 int spi_xfer(struct spi_slave *slave, unsigned int bitlen,
439 const void *dout, void *din, unsigned long flags)
441 struct davinci_spi_slave *ds = to_davinci_spi(slave);
443 ds->cur_cs = slave->cs;
445 return __davinci_spi_xfer(ds, bitlen, dout, din, flags);
448 int spi_claim_bus(struct spi_slave *slave)
450 struct davinci_spi_slave *ds = to_davinci_spi(slave);
452 #ifdef CONFIG_SPI_HALF_DUPLEX
453 ds->half_duplex = true;
455 ds->half_duplex = false;
457 return __davinci_spi_claim_bus(ds, ds->slave.cs);
460 void spi_release_bus(struct spi_slave *slave)
462 struct davinci_spi_slave *ds = to_davinci_spi(slave);
464 __davinci_spi_release_bus(ds);
468 static int davinci_spi_set_speed(struct udevice *bus, uint max_hz)
470 struct davinci_spi_slave *ds = dev_get_priv(bus);
472 debug("%s speed %u\n", __func__, max_hz);
473 if (max_hz > CONFIG_SYS_SPI_CLK / 2)
481 static int davinci_spi_set_mode(struct udevice *bus, uint mode)
483 struct davinci_spi_slave *ds = dev_get_priv(bus);
485 debug("%s mode %u\n", __func__, mode);
491 static int davinci_spi_claim_bus(struct udevice *dev)
493 struct dm_spi_slave_platdata *slave_plat =
494 dev_get_parent_platdata(dev);
495 struct udevice *bus = dev->parent;
496 struct davinci_spi_slave *ds = dev_get_priv(bus);
498 if (slave_plat->cs >= ds->num_cs) {
499 printf("Invalid SPI chipselect\n");
502 ds->half_duplex = slave_plat->mode & SPI_PREAMBLE;
504 return __davinci_spi_claim_bus(ds, slave_plat->cs);
507 static int davinci_spi_release_bus(struct udevice *dev)
509 struct davinci_spi_slave *ds = dev_get_priv(dev->parent);
511 return __davinci_spi_release_bus(ds);
514 static int davinci_spi_xfer(struct udevice *dev, unsigned int bitlen,
515 const void *dout, void *din,
518 struct dm_spi_slave_platdata *slave =
519 dev_get_parent_platdata(dev);
520 struct udevice *bus = dev->parent;
521 struct davinci_spi_slave *ds = dev_get_priv(bus);
523 if (slave->cs >= ds->num_cs) {
524 printf("Invalid SPI chipselect\n");
527 ds->cur_cs = slave->cs;
529 return __davinci_spi_xfer(ds, bitlen, dout, din, flags);
532 static int davinci_spi_probe(struct udevice *bus)
538 static int davinci_ofdata_to_platadata(struct udevice *bus)
540 struct davinci_spi_slave *ds = dev_get_priv(bus);
541 const void *blob = gd->fdt_blob;
542 int node = dev_of_offset(bus);
544 ds->regs = devfdt_map_physmem(bus, sizeof(struct davinci_spi_regs));
546 printf("%s: could not map device address\n", __func__);
549 ds->num_cs = fdtdec_get_int(blob, node, "num-cs", 4);
554 static const struct dm_spi_ops davinci_spi_ops = {
555 .claim_bus = davinci_spi_claim_bus,
556 .release_bus = davinci_spi_release_bus,
557 .xfer = davinci_spi_xfer,
558 .set_speed = davinci_spi_set_speed,
559 .set_mode = davinci_spi_set_mode,
562 static const struct udevice_id davinci_spi_ids[] = {
563 { .compatible = "ti,keystone-spi" },
564 { .compatible = "ti,dm6441-spi" },
565 { .compatible = "ti,da830-spi" },
569 U_BOOT_DRIVER(davinci_spi) = {
570 .name = "davinci_spi",
572 .of_match = davinci_spi_ids,
573 .ops = &davinci_spi_ops,
574 .ofdata_to_platdata = davinci_ofdata_to_platadata,
575 .priv_auto_alloc_size = sizeof(struct davinci_spi_slave),
576 .probe = davinci_spi_probe,