blob: eeac1339c233716325c98539339ce9a232e81779 [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/*
Nishanth Menoneaa39c62023-11-01 15:56:03 -05003 * Copyright (C) 2009 Texas Instruments Incorporated - https://www.ti.com/
Sekhar Nori4708b172010-01-27 11:10:40 -05004 *
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
Tom Rinidec7ea02024-05-20 13:35:03 -060011#include <config.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>
Simon Glass3ba929a2020-10-30 21:38:53 -060015#include <asm/global_data.h>
Sekhar Nori4708b172010-01-27 11:10:40 -050016#include <asm/io.h>
17#include <asm/arch/hardware.h>
Vignesh Rc1091e12016-07-06 09:58:56 +053018#include <dm.h>
Jagan Tekia147f662018-09-03 23:00:23 +053019#include <dm/platform_data/spi_davinci.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060020#include <linux/bitops.h>
Simon Glassdbd79542020-05-10 11:40:11 -060021#include <linux/delay.h>
Jagan Teki742d53d2015-06-27 00:51:28 +053022
Jagan Teki742d53d2015-06-27 00:51:28 +053023/* SPIGCR0 */
24#define SPIGCR0_SPIENA_MASK 0x1
25#define SPIGCR0_SPIRST_MASK 0x0
26
27/* SPIGCR0 */
28#define SPIGCR1_CLKMOD_MASK BIT(1)
29#define SPIGCR1_MASTER_MASK BIT(0)
30#define SPIGCR1_SPIENA_MASK BIT(24)
31
32/* SPIPC0 */
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)
37
38/* SPIFMT0 */
39#define SPIFMT_SHIFTDIR_SHIFT 20
40#define SPIFMT_POLARITY_SHIFT 17
41#define SPIFMT_PHASE_SHIFT 16
42#define SPIFMT_PRESCALE_SHIFT 8
43
44/* SPIDAT1 */
45#define SPIDAT1_CSHOLD_SHIFT 28
46#define SPIDAT1_CSNR_SHIFT 16
47
48/* SPIDELAY */
49#define SPI_C2TDELAY_SHIFT 24
50#define SPI_T2CDELAY_SHIFT 16
51
52/* SPIBUF */
53#define SPIBUF_RXEMPTY_MASK BIT(31)
54#define SPIBUF_TXFULL_MASK BIT(29)
55
56/* SPIDEF */
57#define SPIDEF_CSDEF0_MASK BIT(0)
58
Vignesh Rc1091e12016-07-06 09:58:56 +053059DECLARE_GLOBAL_DATA_PTR;
Jagan Teki742d53d2015-06-27 00:51:28 +053060
Jagan Tekiccaa8fc2015-06-27 00:51:29 +053061/* davinci spi register set */
62struct 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 */
74 dv_reg rsvd[3];
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 */
87};
88
89/* davinci spi slave */
Jagan Teki742d53d2015-06-27 00:51:28 +053090struct davinci_spi_slave {
Jagan Teki742d53d2015-06-27 00:51:28 +053091 struct davinci_spi_regs *regs;
Vignesh Rc1091e12016-07-06 09:58:56 +053092 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 */
Jagan Teki742d53d2015-06-27 00:51:28 +053097};
98
Nick Thompson213e4762010-06-22 11:06:01 -040099/*
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).
103 */
104__attribute__((always_inline))
105static inline u32 davinci_spi_xfer_data(struct davinci_spi_slave *ds, u32 data)
106{
107 u32 buf_reg_val;
108
109 /* send out data */
110 writel(data, &ds->regs->dat1);
111
112 /* wait for the data to clock in/out */
113 while ((buf_reg_val = readl(&ds->regs->buf)) & SPIBUF_RXEMPTY_MASK)
114 ;
115
116 return buf_reg_val;
117}
118
Vignesh Rc1091e12016-07-06 09:58:56 +0530119static int davinci_spi_read(struct davinci_spi_slave *ds, unsigned int len,
Nick Thompson213e4762010-06-22 11:06:01 -0400120 u8 *rxp, unsigned long flags)
121{
Nick Thompson213e4762010-06-22 11:06:01 -0400122 unsigned int data1_reg_val;
123
124 /* enable CS hold, CS[n] and clear the data bits */
125 data1_reg_val = ((1 << SPIDAT1_CSHOLD_SHIFT) |
Vignesh Rc1091e12016-07-06 09:58:56 +0530126 (ds->cur_cs << SPIDAT1_CSNR_SHIFT));
Nick Thompson213e4762010-06-22 11:06:01 -0400127
128 /* wait till TXFULL is deasserted */
129 while (readl(&ds->regs->buf) & SPIBUF_TXFULL_MASK)
130 ;
131
Nick Thompson213e4762010-06-22 11:06:01 -0400132 /* keep reading 1 byte until only 1 byte left */
133 while ((len--) > 1)
134 *rxp++ = davinci_spi_xfer_data(ds, data1_reg_val);
135
136 /* clear CS hold when we reach the end */
137 if (flags & SPI_XFER_END)
138 data1_reg_val &= ~(1 << SPIDAT1_CSHOLD_SHIFT);
139
140 /* read the last byte */
141 *rxp = davinci_spi_xfer_data(ds, data1_reg_val);
142
143 return 0;
144}
145
Vignesh Rc1091e12016-07-06 09:58:56 +0530146static int davinci_spi_write(struct davinci_spi_slave *ds, unsigned int len,
Nick Thompson4bbd91f2010-07-05 20:00:40 -0400147 const u8 *txp, unsigned long flags)
Nick Thompson213e4762010-06-22 11:06:01 -0400148{
Nick Thompson213e4762010-06-22 11:06:01 -0400149 unsigned int data1_reg_val;
150
151 /* enable CS hold and clear the data bits */
152 data1_reg_val = ((1 << SPIDAT1_CSHOLD_SHIFT) |
Vignesh Rc1091e12016-07-06 09:58:56 +0530153 (ds->cur_cs << SPIDAT1_CSNR_SHIFT));
Nick Thompson213e4762010-06-22 11:06:01 -0400154
155 /* wait till TXFULL is deasserted */
156 while (readl(&ds->regs->buf) & SPIBUF_TXFULL_MASK)
157 ;
158
Nick Thompson213e4762010-06-22 11:06:01 -0400159 /* keep writing 1 byte until only 1 byte left */
160 while ((len--) > 1)
161 davinci_spi_xfer_data(ds, data1_reg_val | *txp++);
162
163 /* clear CS hold when we reach the end */
164 if (flags & SPI_XFER_END)
165 data1_reg_val &= ~(1 << SPIDAT1_CSHOLD_SHIFT);
166
167 /* write the last byte */
168 davinci_spi_xfer_data(ds, data1_reg_val | *txp);
169
170 return 0;
171}
172
Vignesh Rc1091e12016-07-06 09:58:56 +0530173static int davinci_spi_read_write(struct davinci_spi_slave *ds, unsigned
174 int len, u8 *rxp, const u8 *txp,
175 unsigned long flags)
Nick Thompson213e4762010-06-22 11:06:01 -0400176{
Nick Thompson213e4762010-06-22 11:06:01 -0400177 unsigned int data1_reg_val;
178
179 /* enable CS hold and clear the data bits */
180 data1_reg_val = ((1 << SPIDAT1_CSHOLD_SHIFT) |
Vignesh Rc1091e12016-07-06 09:58:56 +0530181 (ds->cur_cs << SPIDAT1_CSNR_SHIFT));
Nick Thompson213e4762010-06-22 11:06:01 -0400182
183 /* wait till TXFULL is deasserted */
184 while (readl(&ds->regs->buf) & SPIBUF_TXFULL_MASK)
185 ;
186
187 /* keep reading and writing 1 byte until only 1 byte left */
188 while ((len--) > 1)
189 *rxp++ = davinci_spi_xfer_data(ds, data1_reg_val | *txp++);
190
191 /* clear CS hold when we reach the end */
192 if (flags & SPI_XFER_END)
193 data1_reg_val &= ~(1 << SPIDAT1_CSHOLD_SHIFT);
194
195 /* read and write the last byte */
196 *rxp = davinci_spi_xfer_data(ds, data1_reg_val | *txp);
197
198 return 0;
199}
Vignesh Rc1091e12016-07-06 09:58:56 +0530200
Vignesh Rc1091e12016-07-06 09:58:56 +0530201static int __davinci_spi_claim_bus(struct davinci_spi_slave *ds, int cs)
202{
203 unsigned int mode = 0, scalar;
204
205 /* Enable the SPI hardware */
206 writel(SPIGCR0_SPIRST_MASK, &ds->regs->gcr0);
207 udelay(1000);
208 writel(SPIGCR0_SPIENA_MASK, &ds->regs->gcr0);
209
210 /* Set master mode, powered up and not activated */
211 writel(SPIGCR1_MASTER_MASK | SPIGCR1_CLKMOD_MASK, &ds->regs->gcr1);
212
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);
216
217 /* setup format */
Tom Rini6a5dccc2022-11-16 13:10:41 -0500218 scalar = ((CFG_SYS_SPI_CLK / ds->freq) - 1) & 0xFF;
Vignesh Rc1091e12016-07-06 09:58:56 +0530219
220 /*
221 * Use following format:
222 * character length = 8,
223 * MSB shifted out first
224 */
225 if (ds->mode & SPI_CPOL)
226 mode |= SPI_CPOL;
227 if (!(ds->mode & SPI_CPHA))
228 mode |= SPI_CPHA;
229 writel(8 | (scalar << SPIFMT_PRESCALE_SHIFT) |
230 (mode << SPIFMT_PHASE_SHIFT), &ds->regs->fmt0);
231
232 /*
233 * Including a minor delay. No science here. Should be good even with
234 * no delay
235 */
236 writel((50 << SPI_C2TDELAY_SHIFT) |
237 (50 << SPI_T2CDELAY_SHIFT), &ds->regs->delay);
238
239 /* default chip select register */
240 writel(SPIDEF_CSDEF0_MASK, &ds->regs->def);
241
242 /* no interrupts */
243 writel(0, &ds->regs->int0);
244 writel(0, &ds->regs->lvl);
245
246 /* enable SPI */
247 writel((readl(&ds->regs->gcr1) | SPIGCR1_SPIENA_MASK), &ds->regs->gcr1);
248
249 return 0;
250}
251
252static int __davinci_spi_release_bus(struct davinci_spi_slave *ds)
253{
254 /* Disable the SPI hardware */
255 writel(SPIGCR0_SPIRST_MASK, &ds->regs->gcr0);
256
257 return 0;
258}
259
260static int __davinci_spi_xfer(struct davinci_spi_slave *ds,
261 unsigned int bitlen, const void *dout, void *din,
262 unsigned long flags)
263{
264 unsigned int len;
265
266 if (bitlen == 0)
267 /* Finish any previously submitted transfers */
268 goto out;
269
270 /*
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
274 * transfer request.
275 */
276 if (bitlen % 8) {
277 /* Errors always terminate an ongoing transfer */
278 flags |= SPI_XFER_END;
279 goto out;
280 }
281
282 len = bitlen / 8;
283
284 if (!dout)
285 return davinci_spi_read(ds, len, din, flags);
286 if (!din)
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);
290
291 printf("SPI full duplex not supported\n");
292 flags |= SPI_XFER_END;
293
294out:
295 if (flags & SPI_XFER_END) {
296 u8 dummy = 0;
297 davinci_spi_write(ds, 1, &dummy, flags);
298 }
299 return 0;
300}
301
Vignesh Rc1091e12016-07-06 09:58:56 +0530302static int davinci_spi_set_speed(struct udevice *bus, uint max_hz)
303{
304 struct davinci_spi_slave *ds = dev_get_priv(bus);
Jagan Tekiccaa8fc2015-06-27 00:51:29 +0530305
Vignesh Rc1091e12016-07-06 09:58:56 +0530306 debug("%s speed %u\n", __func__, max_hz);
Tom Rini6a5dccc2022-11-16 13:10:41 -0500307 if (max_hz > CFG_SYS_SPI_CLK / 2)
Vignesh Rc1091e12016-07-06 09:58:56 +0530308 return -EINVAL;
Jagan Tekiccaa8fc2015-06-27 00:51:29 +0530309
Vignesh Rc1091e12016-07-06 09:58:56 +0530310 ds->freq = max_hz;
Jagan Tekiccaa8fc2015-06-27 00:51:29 +0530311
Vignesh Rc1091e12016-07-06 09:58:56 +0530312 return 0;
313}
Jagan Tekiccaa8fc2015-06-27 00:51:29 +0530314
Vignesh Rc1091e12016-07-06 09:58:56 +0530315static int davinci_spi_set_mode(struct udevice *bus, uint mode)
316{
317 struct davinci_spi_slave *ds = dev_get_priv(bus);
Jagan Tekiccaa8fc2015-06-27 00:51:29 +0530318
Vignesh Rc1091e12016-07-06 09:58:56 +0530319 debug("%s mode %u\n", __func__, mode);
320 ds->mode = mode;
Jagan Tekiccaa8fc2015-06-27 00:51:29 +0530321
322 return 0;
323}
324
Vignesh Rc1091e12016-07-06 09:58:56 +0530325static int davinci_spi_claim_bus(struct udevice *dev)
Jagan Tekiccaa8fc2015-06-27 00:51:29 +0530326{
Simon Glassb75b15b2020-12-03 16:55:23 -0700327 struct dm_spi_slave_plat *slave_plat =
Simon Glass71fa5b42020-12-03 16:55:18 -0700328 dev_get_parent_plat(dev);
Vignesh Rc1091e12016-07-06 09:58:56 +0530329 struct udevice *bus = dev->parent;
330 struct davinci_spi_slave *ds = dev_get_priv(bus);
Jagan Tekiccaa8fc2015-06-27 00:51:29 +0530331
Venkatesh Yadav Abbarapu91b9e372024-09-26 10:25:05 +0530332 if (slave_plat->cs[0] >= ds->num_cs) {
Vignesh Rc1091e12016-07-06 09:58:56 +0530333 printf("Invalid SPI chipselect\n");
334 return -EINVAL;
335 }
336 ds->half_duplex = slave_plat->mode & SPI_PREAMBLE;
337
Venkatesh Yadav Abbarapu91b9e372024-09-26 10:25:05 +0530338 return __davinci_spi_claim_bus(ds, slave_plat->cs[0]);
Jagan Tekiccaa8fc2015-06-27 00:51:29 +0530339}
340
Vignesh Rc1091e12016-07-06 09:58:56 +0530341static int davinci_spi_release_bus(struct udevice *dev)
Sekhar Nori4708b172010-01-27 11:10:40 -0500342{
Vignesh Rc1091e12016-07-06 09:58:56 +0530343 struct davinci_spi_slave *ds = dev_get_priv(dev->parent);
Sekhar Nori4708b172010-01-27 11:10:40 -0500344
Vignesh Rc1091e12016-07-06 09:58:56 +0530345 return __davinci_spi_release_bus(ds);
346}
Sekhar Nori4708b172010-01-27 11:10:40 -0500347
Vignesh Rc1091e12016-07-06 09:58:56 +0530348static int davinci_spi_xfer(struct udevice *dev, unsigned int bitlen,
349 const void *dout, void *din,
350 unsigned long flags)
351{
Simon Glassb75b15b2020-12-03 16:55:23 -0700352 struct dm_spi_slave_plat *slave =
Simon Glass71fa5b42020-12-03 16:55:18 -0700353 dev_get_parent_plat(dev);
Vignesh Rc1091e12016-07-06 09:58:56 +0530354 struct udevice *bus = dev->parent;
355 struct davinci_spi_slave *ds = dev_get_priv(bus);
356
Venkatesh Yadav Abbarapu91b9e372024-09-26 10:25:05 +0530357 if (slave->cs[0] >= ds->num_cs) {
Vignesh Rc1091e12016-07-06 09:58:56 +0530358 printf("Invalid SPI chipselect\n");
359 return -EINVAL;
Sekhar Nori4708b172010-01-27 11:10:40 -0500360 }
Venkatesh Yadav Abbarapu91b9e372024-09-26 10:25:05 +0530361 ds->cur_cs = slave->cs[0];
Sekhar Nori4708b172010-01-27 11:10:40 -0500362
Vignesh Rc1091e12016-07-06 09:58:56 +0530363 return __davinci_spi_xfer(ds, bitlen, dout, din, flags);
364}
Sekhar Nori4708b172010-01-27 11:10:40 -0500365
Jagan Tekia147f662018-09-03 23:00:23 +0530366static 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,
372};
373
Vignesh Rc1091e12016-07-06 09:58:56 +0530374static int davinci_spi_probe(struct udevice *bus)
375{
Jagan Tekia147f662018-09-03 23:00:23 +0530376 struct davinci_spi_slave *ds = dev_get_priv(bus);
Simon Glass95588622020-12-22 19:30:28 -0700377 struct davinci_spi_plat *plat = dev_get_plat(bus);
Jagan Tekia147f662018-09-03 23:00:23 +0530378 ds->regs = plat->regs;
379 ds->num_cs = plat->num_cs;
380
Vignesh Rc1091e12016-07-06 09:58:56 +0530381 return 0;
382}
Sekhar Nori4708b172010-01-27 11:10:40 -0500383
Simon Glass3580f6d2021-08-07 07:24:03 -0600384#if CONFIG_IS_ENABLED(OF_REAL)
Vignesh Rc1091e12016-07-06 09:58:56 +0530385static int davinci_ofdata_to_platadata(struct udevice *bus)
386{
Simon Glass95588622020-12-22 19:30:28 -0700387 struct davinci_spi_plat *plat = dev_get_plat(bus);
Jagan Tekia147f662018-09-03 23:00:23 +0530388 fdt_addr_t addr;
Vignesh Rc1091e12016-07-06 09:58:56 +0530389
Masahiro Yamadaa89b4de2020-07-17 14:36:48 +0900390 addr = dev_read_addr(bus);
Jagan Tekia147f662018-09-03 23:00:23 +0530391 if (addr == FDT_ADDR_T_NONE)
Vignesh Rc1091e12016-07-06 09:58:56 +0530392 return -EINVAL;
Jagan Tekia147f662018-09-03 23:00:23 +0530393
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);
Vignesh Rc1091e12016-07-06 09:58:56 +0530396
Sekhar Nori4708b172010-01-27 11:10:40 -0500397 return 0;
398}
Vignesh Rc1091e12016-07-06 09:58:56 +0530399
Vignesh Rc1091e12016-07-06 09:58:56 +0530400static const struct udevice_id davinci_spi_ids[] = {
401 { .compatible = "ti,keystone-spi" },
402 { .compatible = "ti,dm6441-spi" },
Adam Forda2d2f562017-09-17 20:43:45 -0500403 { .compatible = "ti,da830-spi" },
Vignesh Rc1091e12016-07-06 09:58:56 +0530404 { }
405};
Jagan Tekia147f662018-09-03 23:00:23 +0530406#endif
Vignesh Rc1091e12016-07-06 09:58:56 +0530407
408U_BOOT_DRIVER(davinci_spi) = {
409 .name = "davinci_spi",
410 .id = UCLASS_SPI,
Simon Glass3580f6d2021-08-07 07:24:03 -0600411#if CONFIG_IS_ENABLED(OF_REAL)
Vignesh Rc1091e12016-07-06 09:58:56 +0530412 .of_match = davinci_spi_ids,
Simon Glassaad29ae2020-12-03 16:55:21 -0700413 .of_to_plat = davinci_ofdata_to_platadata,
Simon Glassb75b15b2020-12-03 16:55:23 -0700414 .plat_auto = sizeof(struct davinci_spi_plat),
Jagan Tekia147f662018-09-03 23:00:23 +0530415#endif
Vignesh Rc1091e12016-07-06 09:58:56 +0530416 .probe = davinci_spi_probe,
Jagan Tekia147f662018-09-03 23:00:23 +0530417 .ops = &davinci_spi_ops,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700418 .priv_auto = sizeof(struct davinci_spi_slave),
Vignesh Rc1091e12016-07-06 09:58:56 +0530419};