1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2009 Texas Instruments Incorporated - https://www.ti.com/
5 * Driver for SPI controller on DaVinci. Based on atmel_spi.c
8 * Copyright (C) 2007 Atmel Corporation
15 #include <asm/global_data.h>
17 #include <asm/arch/hardware.h>
19 #include <dm/platform_data/spi_davinci.h>
20 #include <linux/bitops.h>
21 #include <linux/delay.h>
24 #define SPIGCR0_SPIENA_MASK 0x1
25 #define SPIGCR0_SPIRST_MASK 0x0
28 #define SPIGCR1_CLKMOD_MASK BIT(1)
29 #define SPIGCR1_MASTER_MASK BIT(0)
30 #define SPIGCR1_SPIENA_MASK BIT(24)
33 #define SPIPC0_DIFUN_MASK BIT(11) /* SIMO */
34 #define SPIPC0_DOFUN_MASK BIT(10) /* SOMI */
35 #define SPIPC0_CLKFUN_MASK BIT(9) /* CLK */
36 #define SPIPC0_EN0FUN_MASK BIT(0)
39 #define SPIFMT_SHIFTDIR_SHIFT 20
40 #define SPIFMT_POLARITY_SHIFT 17
41 #define SPIFMT_PHASE_SHIFT 16
42 #define SPIFMT_PRESCALE_SHIFT 8
45 #define SPIDAT1_CSHOLD_SHIFT 28
46 #define SPIDAT1_CSNR_SHIFT 16
49 #define SPI_C2TDELAY_SHIFT 24
50 #define SPI_T2CDELAY_SHIFT 16
53 #define SPIBUF_RXEMPTY_MASK BIT(31)
54 #define SPIBUF_TXFULL_MASK BIT(29)
57 #define SPIDEF_CSDEF0_MASK BIT(0)
59 DECLARE_GLOBAL_DATA_PTR;
61 /* davinci spi register set */
62 struct davinci_spi_regs {
63 dv_reg gcr0; /* 0x00 */
64 dv_reg gcr1; /* 0x04 */
65 dv_reg int0; /* 0x08 */
66 dv_reg lvl; /* 0x0c */
67 dv_reg flg; /* 0x10 */
68 dv_reg pc0; /* 0x14 */
69 dv_reg pc1; /* 0x18 */
70 dv_reg pc2; /* 0x1c */
71 dv_reg pc3; /* 0x20 */
72 dv_reg pc4; /* 0x24 */
73 dv_reg pc5; /* 0x28 */
75 dv_reg dat0; /* 0x38 */
76 dv_reg dat1; /* 0x3c */
77 dv_reg buf; /* 0x40 */
78 dv_reg emu; /* 0x44 */
79 dv_reg delay; /* 0x48 */
80 dv_reg def; /* 0x4c */
81 dv_reg fmt0; /* 0x50 */
82 dv_reg fmt1; /* 0x54 */
83 dv_reg fmt2; /* 0x58 */
84 dv_reg fmt3; /* 0x5c */
85 dv_reg intvec0; /* 0x60 */
86 dv_reg intvec1; /* 0x64 */
89 /* davinci spi slave */
90 struct davinci_spi_slave {
91 struct davinci_spi_regs *regs;
92 unsigned int freq; /* current SPI bus frequency */
93 unsigned int mode; /* current SPI mode used */
94 u8 num_cs; /* total no. of CS available */
95 u8 cur_cs; /* CS of current slave */
96 bool half_duplex; /* true, if master is half-duplex only */
100 * This functions needs to act like a macro to avoid pipeline reloads in the
101 * loops below. Use always_inline. This gains us about 160KiB/s and the bloat
102 * appears to be zero bytes (da830).
104 __attribute__((always_inline))
105 static inline u32 davinci_spi_xfer_data(struct davinci_spi_slave *ds, u32 data)
110 writel(data, &ds->regs->dat1);
112 /* wait for the data to clock in/out */
113 while ((buf_reg_val = readl(&ds->regs->buf)) & SPIBUF_RXEMPTY_MASK)
119 static int davinci_spi_read(struct davinci_spi_slave *ds, unsigned int len,
120 u8 *rxp, unsigned long flags)
122 unsigned int data1_reg_val;
124 /* enable CS hold, CS[n] and clear the data bits */
125 data1_reg_val = ((1 << SPIDAT1_CSHOLD_SHIFT) |
126 (ds->cur_cs << SPIDAT1_CSNR_SHIFT));
128 /* wait till TXFULL is deasserted */
129 while (readl(&ds->regs->buf) & SPIBUF_TXFULL_MASK)
132 /* keep reading 1 byte until only 1 byte left */
134 *rxp++ = davinci_spi_xfer_data(ds, data1_reg_val);
136 /* clear CS hold when we reach the end */
137 if (flags & SPI_XFER_END)
138 data1_reg_val &= ~(1 << SPIDAT1_CSHOLD_SHIFT);
140 /* read the last byte */
141 *rxp = davinci_spi_xfer_data(ds, data1_reg_val);
146 static int davinci_spi_write(struct davinci_spi_slave *ds, unsigned int len,
147 const u8 *txp, unsigned long flags)
149 unsigned int data1_reg_val;
151 /* enable CS hold and clear the data bits */
152 data1_reg_val = ((1 << SPIDAT1_CSHOLD_SHIFT) |
153 (ds->cur_cs << SPIDAT1_CSNR_SHIFT));
155 /* wait till TXFULL is deasserted */
156 while (readl(&ds->regs->buf) & SPIBUF_TXFULL_MASK)
159 /* keep writing 1 byte until only 1 byte left */
161 davinci_spi_xfer_data(ds, data1_reg_val | *txp++);
163 /* clear CS hold when we reach the end */
164 if (flags & SPI_XFER_END)
165 data1_reg_val &= ~(1 << SPIDAT1_CSHOLD_SHIFT);
167 /* write the last byte */
168 davinci_spi_xfer_data(ds, data1_reg_val | *txp);
173 static int davinci_spi_read_write(struct davinci_spi_slave *ds, unsigned
174 int len, u8 *rxp, const u8 *txp,
177 unsigned int data1_reg_val;
179 /* enable CS hold and clear the data bits */
180 data1_reg_val = ((1 << SPIDAT1_CSHOLD_SHIFT) |
181 (ds->cur_cs << SPIDAT1_CSNR_SHIFT));
183 /* wait till TXFULL is deasserted */
184 while (readl(&ds->regs->buf) & SPIBUF_TXFULL_MASK)
187 /* keep reading and writing 1 byte until only 1 byte left */
189 *rxp++ = davinci_spi_xfer_data(ds, data1_reg_val | *txp++);
191 /* clear CS hold when we reach the end */
192 if (flags & SPI_XFER_END)
193 data1_reg_val &= ~(1 << SPIDAT1_CSHOLD_SHIFT);
195 /* read and write the last byte */
196 *rxp = davinci_spi_xfer_data(ds, data1_reg_val | *txp);
201 static int __davinci_spi_claim_bus(struct davinci_spi_slave *ds, int cs)
203 unsigned int mode = 0, scalar;
205 /* Enable the SPI hardware */
206 writel(SPIGCR0_SPIRST_MASK, &ds->regs->gcr0);
208 writel(SPIGCR0_SPIENA_MASK, &ds->regs->gcr0);
210 /* Set master mode, powered up and not activated */
211 writel(SPIGCR1_MASTER_MASK | SPIGCR1_CLKMOD_MASK, &ds->regs->gcr1);
213 /* CS, CLK, SIMO and SOMI are functional pins */
214 writel(((1 << cs) | SPIPC0_CLKFUN_MASK |
215 SPIPC0_DOFUN_MASK | SPIPC0_DIFUN_MASK), &ds->regs->pc0);
218 scalar = ((CFG_SYS_SPI_CLK / ds->freq) - 1) & 0xFF;
221 * Use following format:
222 * character length = 8,
223 * MSB shifted out first
225 if (ds->mode & SPI_CPOL)
227 if (!(ds->mode & SPI_CPHA))
229 writel(8 | (scalar << SPIFMT_PRESCALE_SHIFT) |
230 (mode << SPIFMT_PHASE_SHIFT), &ds->regs->fmt0);
233 * Including a minor delay. No science here. Should be good even with
236 writel((50 << SPI_C2TDELAY_SHIFT) |
237 (50 << SPI_T2CDELAY_SHIFT), &ds->regs->delay);
239 /* default chip select register */
240 writel(SPIDEF_CSDEF0_MASK, &ds->regs->def);
243 writel(0, &ds->regs->int0);
244 writel(0, &ds->regs->lvl);
247 writel((readl(&ds->regs->gcr1) | SPIGCR1_SPIENA_MASK), &ds->regs->gcr1);
252 static int __davinci_spi_release_bus(struct davinci_spi_slave *ds)
254 /* Disable the SPI hardware */
255 writel(SPIGCR0_SPIRST_MASK, &ds->regs->gcr0);
260 static int __davinci_spi_xfer(struct davinci_spi_slave *ds,
261 unsigned int bitlen, const void *dout, void *din,
267 /* Finish any previously submitted transfers */
271 * It's not clear how non-8-bit-aligned transfers are supposed to be
272 * represented as a stream of bytes...this is a limitation of
273 * the current SPI interface - here we terminate on receiving such a
277 /* Errors always terminate an ongoing transfer */
278 flags |= SPI_XFER_END;
285 return davinci_spi_read(ds, len, din, flags);
287 return davinci_spi_write(ds, len, dout, flags);
288 if (!ds->half_duplex)
289 return davinci_spi_read_write(ds, len, din, dout, flags);
291 printf("SPI full duplex not supported\n");
292 flags |= SPI_XFER_END;
295 if (flags & SPI_XFER_END) {
297 davinci_spi_write(ds, 1, &dummy, flags);
302 static int davinci_spi_set_speed(struct udevice *bus, uint max_hz)
304 struct davinci_spi_slave *ds = dev_get_priv(bus);
306 debug("%s speed %u\n", __func__, max_hz);
307 if (max_hz > CFG_SYS_SPI_CLK / 2)
315 static int davinci_spi_set_mode(struct udevice *bus, uint mode)
317 struct davinci_spi_slave *ds = dev_get_priv(bus);
319 debug("%s mode %u\n", __func__, mode);
325 static int davinci_spi_claim_bus(struct udevice *dev)
327 struct dm_spi_slave_plat *slave_plat =
328 dev_get_parent_plat(dev);
329 struct udevice *bus = dev->parent;
330 struct davinci_spi_slave *ds = dev_get_priv(bus);
332 if (slave_plat->cs[0] >= ds->num_cs) {
333 printf("Invalid SPI chipselect\n");
336 ds->half_duplex = slave_plat->mode & SPI_PREAMBLE;
338 return __davinci_spi_claim_bus(ds, slave_plat->cs[0]);
341 static int davinci_spi_release_bus(struct udevice *dev)
343 struct davinci_spi_slave *ds = dev_get_priv(dev->parent);
345 return __davinci_spi_release_bus(ds);
348 static int davinci_spi_xfer(struct udevice *dev, unsigned int bitlen,
349 const void *dout, void *din,
352 struct dm_spi_slave_plat *slave =
353 dev_get_parent_plat(dev);
354 struct udevice *bus = dev->parent;
355 struct davinci_spi_slave *ds = dev_get_priv(bus);
357 if (slave->cs[0] >= ds->num_cs) {
358 printf("Invalid SPI chipselect\n");
361 ds->cur_cs = slave->cs[0];
363 return __davinci_spi_xfer(ds, bitlen, dout, din, flags);
366 static const struct dm_spi_ops davinci_spi_ops = {
367 .claim_bus = davinci_spi_claim_bus,
368 .release_bus = davinci_spi_release_bus,
369 .xfer = davinci_spi_xfer,
370 .set_speed = davinci_spi_set_speed,
371 .set_mode = davinci_spi_set_mode,
374 static int davinci_spi_probe(struct udevice *bus)
376 struct davinci_spi_slave *ds = dev_get_priv(bus);
377 struct davinci_spi_plat *plat = dev_get_plat(bus);
378 ds->regs = plat->regs;
379 ds->num_cs = plat->num_cs;
384 #if CONFIG_IS_ENABLED(OF_REAL)
385 static int davinci_ofdata_to_platadata(struct udevice *bus)
387 struct davinci_spi_plat *plat = dev_get_plat(bus);
390 addr = dev_read_addr(bus);
391 if (addr == FDT_ADDR_T_NONE)
394 plat->regs = (struct davinci_spi_regs *)addr;
395 plat->num_cs = fdtdec_get_int(gd->fdt_blob, dev_of_offset(bus), "num-cs", 4);
400 static const struct udevice_id davinci_spi_ids[] = {
401 { .compatible = "ti,keystone-spi" },
402 { .compatible = "ti,dm6441-spi" },
403 { .compatible = "ti,da830-spi" },
408 U_BOOT_DRIVER(davinci_spi) = {
409 .name = "davinci_spi",
411 #if CONFIG_IS_ENABLED(OF_REAL)
412 .of_match = davinci_spi_ids,
413 .of_to_plat = davinci_ofdata_to_platadata,
414 .plat_auto = sizeof(struct davinci_spi_plat),
416 .probe = davinci_spi_probe,
417 .ops = &davinci_spi_ops,
418 .priv_auto = sizeof(struct davinci_spi_slave),