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)
58 DECLARE_GLOBAL_DATA_PTR;
60 /* davinci spi register set */
61 struct davinci_spi_regs {
62 dv_reg gcr0; /* 0x00 */
63 dv_reg gcr1; /* 0x04 */
64 dv_reg int0; /* 0x08 */
65 dv_reg lvl; /* 0x0c */
66 dv_reg flg; /* 0x10 */
67 dv_reg pc0; /* 0x14 */
68 dv_reg pc1; /* 0x18 */
69 dv_reg pc2; /* 0x1c */
70 dv_reg pc3; /* 0x20 */
71 dv_reg pc4; /* 0x24 */
72 dv_reg pc5; /* 0x28 */
74 dv_reg dat0; /* 0x38 */
75 dv_reg dat1; /* 0x3c */
76 dv_reg buf; /* 0x40 */
77 dv_reg emu; /* 0x44 */
78 dv_reg delay; /* 0x48 */
79 dv_reg def; /* 0x4c */
80 dv_reg fmt0; /* 0x50 */
81 dv_reg fmt1; /* 0x54 */
82 dv_reg fmt2; /* 0x58 */
83 dv_reg fmt3; /* 0x5c */
84 dv_reg intvec0; /* 0x60 */
85 dv_reg intvec1; /* 0x64 */
88 /* davinci spi slave */
89 struct davinci_spi_slave {
90 struct davinci_spi_regs *regs;
91 unsigned int freq; /* current SPI bus frequency */
92 unsigned int mode; /* current SPI mode used */
93 u8 num_cs; /* total no. of CS available */
94 u8 cur_cs; /* CS of current slave */
95 bool half_duplex; /* true, if master is half-duplex only */
99 * This functions needs to act like a macro to avoid pipeline reloads in the
100 * loops below. Use always_inline. This gains us about 160KiB/s and the bloat
101 * appears to be zero bytes (da830).
103 __attribute__((always_inline))
104 static inline u32 davinci_spi_xfer_data(struct davinci_spi_slave *ds, u32 data)
109 writel(data, &ds->regs->dat1);
111 /* wait for the data to clock in/out */
112 while ((buf_reg_val = readl(&ds->regs->buf)) & SPIBUF_RXEMPTY_MASK)
118 static int davinci_spi_read(struct davinci_spi_slave *ds, unsigned int len,
119 u8 *rxp, unsigned long flags)
121 unsigned int data1_reg_val;
123 /* enable CS hold, CS[n] and clear the data bits */
124 data1_reg_val = ((1 << SPIDAT1_CSHOLD_SHIFT) |
125 (ds->cur_cs << SPIDAT1_CSNR_SHIFT));
127 /* wait till TXFULL is deasserted */
128 while (readl(&ds->regs->buf) & SPIBUF_TXFULL_MASK)
131 /* preload the TX buffer to avoid clock starvation */
132 writel(data1_reg_val, &ds->regs->dat1);
134 /* keep reading 1 byte until only 1 byte left */
136 *rxp++ = davinci_spi_xfer_data(ds, data1_reg_val);
138 /* clear CS hold when we reach the end */
139 if (flags & SPI_XFER_END)
140 data1_reg_val &= ~(1 << SPIDAT1_CSHOLD_SHIFT);
142 /* read the last byte */
143 *rxp = davinci_spi_xfer_data(ds, data1_reg_val);
148 static int davinci_spi_write(struct davinci_spi_slave *ds, unsigned int len,
149 const u8 *txp, unsigned long flags)
151 unsigned int data1_reg_val;
153 /* enable CS hold and clear the data bits */
154 data1_reg_val = ((1 << SPIDAT1_CSHOLD_SHIFT) |
155 (ds->cur_cs << SPIDAT1_CSNR_SHIFT));
157 /* wait till TXFULL is deasserted */
158 while (readl(&ds->regs->buf) & SPIBUF_TXFULL_MASK)
161 /* preload the TX buffer to avoid clock starvation */
163 writel(data1_reg_val | *txp++, &ds->regs->dat1);
167 /* keep writing 1 byte until only 1 byte left */
169 davinci_spi_xfer_data(ds, data1_reg_val | *txp++);
171 /* clear CS hold when we reach the end */
172 if (flags & SPI_XFER_END)
173 data1_reg_val &= ~(1 << SPIDAT1_CSHOLD_SHIFT);
175 /* write the last byte */
176 davinci_spi_xfer_data(ds, data1_reg_val | *txp);
181 static int davinci_spi_read_write(struct davinci_spi_slave *ds, unsigned
182 int len, u8 *rxp, const u8 *txp,
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 /* keep reading and writing 1 byte until only 1 byte left */
197 *rxp++ = davinci_spi_xfer_data(ds, data1_reg_val | *txp++);
199 /* clear CS hold when we reach the end */
200 if (flags & SPI_XFER_END)
201 data1_reg_val &= ~(1 << SPIDAT1_CSHOLD_SHIFT);
203 /* read and write the last byte */
204 *rxp = davinci_spi_xfer_data(ds, data1_reg_val | *txp);
210 static int __davinci_spi_claim_bus(struct davinci_spi_slave *ds, int cs)
212 unsigned int mode = 0, scalar;
214 /* Enable the SPI hardware */
215 writel(SPIGCR0_SPIRST_MASK, &ds->regs->gcr0);
217 writel(SPIGCR0_SPIENA_MASK, &ds->regs->gcr0);
219 /* Set master mode, powered up and not activated */
220 writel(SPIGCR1_MASTER_MASK | SPIGCR1_CLKMOD_MASK, &ds->regs->gcr1);
222 /* CS, CLK, SIMO and SOMI are functional pins */
223 writel(((1 << cs) | SPIPC0_CLKFUN_MASK |
224 SPIPC0_DOFUN_MASK | SPIPC0_DIFUN_MASK), &ds->regs->pc0);
227 scalar = ((CONFIG_SYS_SPI_CLK / ds->freq) - 1) & 0xFF;
230 * Use following format:
231 * character length = 8,
232 * MSB shifted out first
234 if (ds->mode & SPI_CPOL)
236 if (!(ds->mode & SPI_CPHA))
238 writel(8 | (scalar << SPIFMT_PRESCALE_SHIFT) |
239 (mode << SPIFMT_PHASE_SHIFT), &ds->regs->fmt0);
242 * Including a minor delay. No science here. Should be good even with
245 writel((50 << SPI_C2TDELAY_SHIFT) |
246 (50 << SPI_T2CDELAY_SHIFT), &ds->regs->delay);
248 /* default chip select register */
249 writel(SPIDEF_CSDEF0_MASK, &ds->regs->def);
252 writel(0, &ds->regs->int0);
253 writel(0, &ds->regs->lvl);
256 writel((readl(&ds->regs->gcr1) | SPIGCR1_SPIENA_MASK), &ds->regs->gcr1);
261 static int __davinci_spi_release_bus(struct davinci_spi_slave *ds)
263 /* Disable the SPI hardware */
264 writel(SPIGCR0_SPIRST_MASK, &ds->regs->gcr0);
269 static int __davinci_spi_xfer(struct davinci_spi_slave *ds,
270 unsigned int bitlen, const void *dout, void *din,
276 /* Finish any previously submitted transfers */
280 * It's not clear how non-8-bit-aligned transfers are supposed to be
281 * represented as a stream of bytes...this is a limitation of
282 * the current SPI interface - here we terminate on receiving such a
286 /* Errors always terminate an ongoing transfer */
287 flags |= SPI_XFER_END;
294 return davinci_spi_read(ds, len, din, flags);
296 return davinci_spi_write(ds, len, dout, flags);
297 if (!ds->half_duplex)
298 return davinci_spi_read_write(ds, len, din, dout, flags);
300 printf("SPI full duplex not supported\n");
301 flags |= SPI_XFER_END;
304 if (flags & SPI_XFER_END) {
306 davinci_spi_write(ds, 1, &dummy, flags);
311 static int davinci_spi_set_speed(struct udevice *bus, uint max_hz)
313 struct davinci_spi_slave *ds = dev_get_priv(bus);
315 debug("%s speed %u\n", __func__, max_hz);
316 if (max_hz > CONFIG_SYS_SPI_CLK / 2)
324 static int davinci_spi_set_mode(struct udevice *bus, uint mode)
326 struct davinci_spi_slave *ds = dev_get_priv(bus);
328 debug("%s mode %u\n", __func__, mode);
334 static int davinci_spi_claim_bus(struct udevice *dev)
336 struct dm_spi_slave_platdata *slave_plat =
337 dev_get_parent_platdata(dev);
338 struct udevice *bus = dev->parent;
339 struct davinci_spi_slave *ds = dev_get_priv(bus);
341 if (slave_plat->cs >= ds->num_cs) {
342 printf("Invalid SPI chipselect\n");
345 ds->half_duplex = slave_plat->mode & SPI_PREAMBLE;
347 return __davinci_spi_claim_bus(ds, slave_plat->cs);
350 static int davinci_spi_release_bus(struct udevice *dev)
352 struct davinci_spi_slave *ds = dev_get_priv(dev->parent);
354 return __davinci_spi_release_bus(ds);
357 static int davinci_spi_xfer(struct udevice *dev, unsigned int bitlen,
358 const void *dout, void *din,
361 struct dm_spi_slave_platdata *slave =
362 dev_get_parent_platdata(dev);
363 struct udevice *bus = dev->parent;
364 struct davinci_spi_slave *ds = dev_get_priv(bus);
366 if (slave->cs >= ds->num_cs) {
367 printf("Invalid SPI chipselect\n");
370 ds->cur_cs = slave->cs;
372 return __davinci_spi_xfer(ds, bitlen, dout, din, flags);
375 static const struct dm_spi_ops davinci_spi_ops = {
376 .claim_bus = davinci_spi_claim_bus,
377 .release_bus = davinci_spi_release_bus,
378 .xfer = davinci_spi_xfer,
379 .set_speed = davinci_spi_set_speed,
380 .set_mode = davinci_spi_set_mode,
383 static int davinci_spi_probe(struct udevice *bus)
385 struct davinci_spi_slave *ds = dev_get_priv(bus);
386 struct davinci_spi_platdata *plat = bus->platdata;
387 ds->regs = plat->regs;
388 ds->num_cs = plat->num_cs;
393 #if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
394 static int davinci_ofdata_to_platadata(struct udevice *bus)
396 struct davinci_spi_platdata *plat = bus->platdata;
399 addr = dev_read_addr(bus);
400 if (addr == FDT_ADDR_T_NONE)
403 plat->regs = (struct davinci_spi_regs *)addr;
404 plat->num_cs = fdtdec_get_int(gd->fdt_blob, dev_of_offset(bus), "num-cs", 4);
409 static const struct udevice_id davinci_spi_ids[] = {
410 { .compatible = "ti,keystone-spi" },
411 { .compatible = "ti,dm6441-spi" },
412 { .compatible = "ti,da830-spi" },
417 U_BOOT_DRIVER(davinci_spi) = {
418 .name = "davinci_spi",
420 #if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
421 .of_match = davinci_spi_ids,
422 .ofdata_to_platdata = davinci_ofdata_to_platadata,
423 .platdata_auto_alloc_size = sizeof(struct davinci_spi_platdata),
425 .probe = davinci_spi_probe,
426 .ops = &davinci_spi_ops,
427 .priv_auto_alloc_size = sizeof(struct davinci_spi_slave),