Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Sekhar Nori | 4708b17 | 2010-01-27 11:10:40 -0500 | [diff] [blame] | 2 | /* |
Nishanth Menon | eaa39c6 | 2023-11-01 15:56:03 -0500 | [diff] [blame] | 3 | * Copyright (C) 2009 Texas Instruments Incorporated - https://www.ti.com/ |
Sekhar Nori | 4708b17 | 2010-01-27 11:10:40 -0500 | [diff] [blame] | 4 | * |
| 5 | * Driver for SPI controller on DaVinci. Based on atmel_spi.c |
| 6 | * by Atmel Corporation |
| 7 | * |
| 8 | * Copyright (C) 2007 Atmel Corporation |
Sekhar Nori | 4708b17 | 2010-01-27 11:10:40 -0500 | [diff] [blame] | 9 | */ |
Jagan Teki | ccaa8fc | 2015-06-27 00:51:29 +0530 | [diff] [blame] | 10 | |
Tom Rini | dec7ea0 | 2024-05-20 13:35:03 -0600 | [diff] [blame] | 11 | #include <config.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 12 | #include <log.h> |
Sekhar Nori | 4708b17 | 2010-01-27 11:10:40 -0500 | [diff] [blame] | 13 | #include <spi.h> |
| 14 | #include <malloc.h> |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 15 | #include <asm/global_data.h> |
Sekhar Nori | 4708b17 | 2010-01-27 11:10:40 -0500 | [diff] [blame] | 16 | #include <asm/io.h> |
| 17 | #include <asm/arch/hardware.h> |
Vignesh R | c1091e1 | 2016-07-06 09:58:56 +0530 | [diff] [blame] | 18 | #include <dm.h> |
Jagan Teki | a147f66 | 2018-09-03 23:00:23 +0530 | [diff] [blame] | 19 | #include <dm/platform_data/spi_davinci.h> |
Simon Glass | 4dcacfc | 2020-05-10 11:40:13 -0600 | [diff] [blame] | 20 | #include <linux/bitops.h> |
Simon Glass | dbd7954 | 2020-05-10 11:40:11 -0600 | [diff] [blame] | 21 | #include <linux/delay.h> |
Jagan Teki | 742d53d | 2015-06-27 00:51:28 +0530 | [diff] [blame] | 22 | |
Jagan Teki | 742d53d | 2015-06-27 00:51:28 +0530 | [diff] [blame] | 23 | /* 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 R | c1091e1 | 2016-07-06 09:58:56 +0530 | [diff] [blame] | 59 | DECLARE_GLOBAL_DATA_PTR; |
Jagan Teki | 742d53d | 2015-06-27 00:51:28 +0530 | [diff] [blame] | 60 | |
Jagan Teki | ccaa8fc | 2015-06-27 00:51:29 +0530 | [diff] [blame] | 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 */ |
| 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 Teki | 742d53d | 2015-06-27 00:51:28 +0530 | [diff] [blame] | 90 | struct davinci_spi_slave { |
Jagan Teki | 742d53d | 2015-06-27 00:51:28 +0530 | [diff] [blame] | 91 | struct davinci_spi_regs *regs; |
Vignesh R | c1091e1 | 2016-07-06 09:58:56 +0530 | [diff] [blame] | 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 */ |
Jagan Teki | 742d53d | 2015-06-27 00:51:28 +0530 | [diff] [blame] | 97 | }; |
| 98 | |
Nick Thompson | 213e476 | 2010-06-22 11:06:01 -0400 | [diff] [blame] | 99 | /* |
| 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)) |
| 105 | static 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 R | c1091e1 | 2016-07-06 09:58:56 +0530 | [diff] [blame] | 119 | static int davinci_spi_read(struct davinci_spi_slave *ds, unsigned int len, |
Nick Thompson | 213e476 | 2010-06-22 11:06:01 -0400 | [diff] [blame] | 120 | u8 *rxp, unsigned long flags) |
| 121 | { |
Nick Thompson | 213e476 | 2010-06-22 11:06:01 -0400 | [diff] [blame] | 122 | 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 R | c1091e1 | 2016-07-06 09:58:56 +0530 | [diff] [blame] | 126 | (ds->cur_cs << SPIDAT1_CSNR_SHIFT)); |
Nick Thompson | 213e476 | 2010-06-22 11:06:01 -0400 | [diff] [blame] | 127 | |
| 128 | /* wait till TXFULL is deasserted */ |
| 129 | while (readl(&ds->regs->buf) & SPIBUF_TXFULL_MASK) |
| 130 | ; |
| 131 | |
Nick Thompson | 213e476 | 2010-06-22 11:06:01 -0400 | [diff] [blame] | 132 | /* 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 R | c1091e1 | 2016-07-06 09:58:56 +0530 | [diff] [blame] | 146 | static int davinci_spi_write(struct davinci_spi_slave *ds, unsigned int len, |
Nick Thompson | 4bbd91f | 2010-07-05 20:00:40 -0400 | [diff] [blame] | 147 | const u8 *txp, unsigned long flags) |
Nick Thompson | 213e476 | 2010-06-22 11:06:01 -0400 | [diff] [blame] | 148 | { |
Nick Thompson | 213e476 | 2010-06-22 11:06:01 -0400 | [diff] [blame] | 149 | unsigned int data1_reg_val; |
| 150 | |
| 151 | /* enable CS hold and clear the data bits */ |
| 152 | data1_reg_val = ((1 << SPIDAT1_CSHOLD_SHIFT) | |
Vignesh R | c1091e1 | 2016-07-06 09:58:56 +0530 | [diff] [blame] | 153 | (ds->cur_cs << SPIDAT1_CSNR_SHIFT)); |
Nick Thompson | 213e476 | 2010-06-22 11:06:01 -0400 | [diff] [blame] | 154 | |
| 155 | /* wait till TXFULL is deasserted */ |
| 156 | while (readl(&ds->regs->buf) & SPIBUF_TXFULL_MASK) |
| 157 | ; |
| 158 | |
Nick Thompson | 213e476 | 2010-06-22 11:06:01 -0400 | [diff] [blame] | 159 | /* 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 R | c1091e1 | 2016-07-06 09:58:56 +0530 | [diff] [blame] | 173 | static int davinci_spi_read_write(struct davinci_spi_slave *ds, unsigned |
| 174 | int len, u8 *rxp, const u8 *txp, |
| 175 | unsigned long flags) |
Nick Thompson | 213e476 | 2010-06-22 11:06:01 -0400 | [diff] [blame] | 176 | { |
Nick Thompson | 213e476 | 2010-06-22 11:06:01 -0400 | [diff] [blame] | 177 | unsigned int data1_reg_val; |
| 178 | |
| 179 | /* enable CS hold and clear the data bits */ |
| 180 | data1_reg_val = ((1 << SPIDAT1_CSHOLD_SHIFT) | |
Vignesh R | c1091e1 | 2016-07-06 09:58:56 +0530 | [diff] [blame] | 181 | (ds->cur_cs << SPIDAT1_CSNR_SHIFT)); |
Nick Thompson | 213e476 | 2010-06-22 11:06:01 -0400 | [diff] [blame] | 182 | |
| 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 R | c1091e1 | 2016-07-06 09:58:56 +0530 | [diff] [blame] | 200 | |
Vignesh R | c1091e1 | 2016-07-06 09:58:56 +0530 | [diff] [blame] | 201 | static 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 Rini | 6a5dccc | 2022-11-16 13:10:41 -0500 | [diff] [blame] | 218 | scalar = ((CFG_SYS_SPI_CLK / ds->freq) - 1) & 0xFF; |
Vignesh R | c1091e1 | 2016-07-06 09:58:56 +0530 | [diff] [blame] | 219 | |
| 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 | |
| 252 | static 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 | |
| 260 | static 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 | |
| 294 | out: |
| 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 R | c1091e1 | 2016-07-06 09:58:56 +0530 | [diff] [blame] | 302 | static int davinci_spi_set_speed(struct udevice *bus, uint max_hz) |
| 303 | { |
| 304 | struct davinci_spi_slave *ds = dev_get_priv(bus); |
Jagan Teki | ccaa8fc | 2015-06-27 00:51:29 +0530 | [diff] [blame] | 305 | |
Vignesh R | c1091e1 | 2016-07-06 09:58:56 +0530 | [diff] [blame] | 306 | debug("%s speed %u\n", __func__, max_hz); |
Tom Rini | 6a5dccc | 2022-11-16 13:10:41 -0500 | [diff] [blame] | 307 | if (max_hz > CFG_SYS_SPI_CLK / 2) |
Vignesh R | c1091e1 | 2016-07-06 09:58:56 +0530 | [diff] [blame] | 308 | return -EINVAL; |
Jagan Teki | ccaa8fc | 2015-06-27 00:51:29 +0530 | [diff] [blame] | 309 | |
Vignesh R | c1091e1 | 2016-07-06 09:58:56 +0530 | [diff] [blame] | 310 | ds->freq = max_hz; |
Jagan Teki | ccaa8fc | 2015-06-27 00:51:29 +0530 | [diff] [blame] | 311 | |
Vignesh R | c1091e1 | 2016-07-06 09:58:56 +0530 | [diff] [blame] | 312 | return 0; |
| 313 | } |
Jagan Teki | ccaa8fc | 2015-06-27 00:51:29 +0530 | [diff] [blame] | 314 | |
Vignesh R | c1091e1 | 2016-07-06 09:58:56 +0530 | [diff] [blame] | 315 | static int davinci_spi_set_mode(struct udevice *bus, uint mode) |
| 316 | { |
| 317 | struct davinci_spi_slave *ds = dev_get_priv(bus); |
Jagan Teki | ccaa8fc | 2015-06-27 00:51:29 +0530 | [diff] [blame] | 318 | |
Vignesh R | c1091e1 | 2016-07-06 09:58:56 +0530 | [diff] [blame] | 319 | debug("%s mode %u\n", __func__, mode); |
| 320 | ds->mode = mode; |
Jagan Teki | ccaa8fc | 2015-06-27 00:51:29 +0530 | [diff] [blame] | 321 | |
| 322 | return 0; |
| 323 | } |
| 324 | |
Vignesh R | c1091e1 | 2016-07-06 09:58:56 +0530 | [diff] [blame] | 325 | static int davinci_spi_claim_bus(struct udevice *dev) |
Jagan Teki | ccaa8fc | 2015-06-27 00:51:29 +0530 | [diff] [blame] | 326 | { |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 327 | struct dm_spi_slave_plat *slave_plat = |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 328 | dev_get_parent_plat(dev); |
Vignesh R | c1091e1 | 2016-07-06 09:58:56 +0530 | [diff] [blame] | 329 | struct udevice *bus = dev->parent; |
| 330 | struct davinci_spi_slave *ds = dev_get_priv(bus); |
Jagan Teki | ccaa8fc | 2015-06-27 00:51:29 +0530 | [diff] [blame] | 331 | |
Venkatesh Yadav Abbarapu | 91b9e37 | 2024-09-26 10:25:05 +0530 | [diff] [blame] | 332 | if (slave_plat->cs[0] >= ds->num_cs) { |
Vignesh R | c1091e1 | 2016-07-06 09:58:56 +0530 | [diff] [blame] | 333 | printf("Invalid SPI chipselect\n"); |
| 334 | return -EINVAL; |
| 335 | } |
| 336 | ds->half_duplex = slave_plat->mode & SPI_PREAMBLE; |
| 337 | |
Venkatesh Yadav Abbarapu | 91b9e37 | 2024-09-26 10:25:05 +0530 | [diff] [blame] | 338 | return __davinci_spi_claim_bus(ds, slave_plat->cs[0]); |
Jagan Teki | ccaa8fc | 2015-06-27 00:51:29 +0530 | [diff] [blame] | 339 | } |
| 340 | |
Vignesh R | c1091e1 | 2016-07-06 09:58:56 +0530 | [diff] [blame] | 341 | static int davinci_spi_release_bus(struct udevice *dev) |
Sekhar Nori | 4708b17 | 2010-01-27 11:10:40 -0500 | [diff] [blame] | 342 | { |
Vignesh R | c1091e1 | 2016-07-06 09:58:56 +0530 | [diff] [blame] | 343 | struct davinci_spi_slave *ds = dev_get_priv(dev->parent); |
Sekhar Nori | 4708b17 | 2010-01-27 11:10:40 -0500 | [diff] [blame] | 344 | |
Vignesh R | c1091e1 | 2016-07-06 09:58:56 +0530 | [diff] [blame] | 345 | return __davinci_spi_release_bus(ds); |
| 346 | } |
Sekhar Nori | 4708b17 | 2010-01-27 11:10:40 -0500 | [diff] [blame] | 347 | |
Vignesh R | c1091e1 | 2016-07-06 09:58:56 +0530 | [diff] [blame] | 348 | static int davinci_spi_xfer(struct udevice *dev, unsigned int bitlen, |
| 349 | const void *dout, void *din, |
| 350 | unsigned long flags) |
| 351 | { |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 352 | struct dm_spi_slave_plat *slave = |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 353 | dev_get_parent_plat(dev); |
Vignesh R | c1091e1 | 2016-07-06 09:58:56 +0530 | [diff] [blame] | 354 | struct udevice *bus = dev->parent; |
| 355 | struct davinci_spi_slave *ds = dev_get_priv(bus); |
| 356 | |
Venkatesh Yadav Abbarapu | 91b9e37 | 2024-09-26 10:25:05 +0530 | [diff] [blame] | 357 | if (slave->cs[0] >= ds->num_cs) { |
Vignesh R | c1091e1 | 2016-07-06 09:58:56 +0530 | [diff] [blame] | 358 | printf("Invalid SPI chipselect\n"); |
| 359 | return -EINVAL; |
Sekhar Nori | 4708b17 | 2010-01-27 11:10:40 -0500 | [diff] [blame] | 360 | } |
Venkatesh Yadav Abbarapu | 91b9e37 | 2024-09-26 10:25:05 +0530 | [diff] [blame] | 361 | ds->cur_cs = slave->cs[0]; |
Sekhar Nori | 4708b17 | 2010-01-27 11:10:40 -0500 | [diff] [blame] | 362 | |
Vignesh R | c1091e1 | 2016-07-06 09:58:56 +0530 | [diff] [blame] | 363 | return __davinci_spi_xfer(ds, bitlen, dout, din, flags); |
| 364 | } |
Sekhar Nori | 4708b17 | 2010-01-27 11:10:40 -0500 | [diff] [blame] | 365 | |
Jagan Teki | a147f66 | 2018-09-03 23:00:23 +0530 | [diff] [blame] | 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, |
| 372 | }; |
| 373 | |
Vignesh R | c1091e1 | 2016-07-06 09:58:56 +0530 | [diff] [blame] | 374 | static int davinci_spi_probe(struct udevice *bus) |
| 375 | { |
Jagan Teki | a147f66 | 2018-09-03 23:00:23 +0530 | [diff] [blame] | 376 | struct davinci_spi_slave *ds = dev_get_priv(bus); |
Simon Glass | 9558862 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 377 | struct davinci_spi_plat *plat = dev_get_plat(bus); |
Jagan Teki | a147f66 | 2018-09-03 23:00:23 +0530 | [diff] [blame] | 378 | ds->regs = plat->regs; |
| 379 | ds->num_cs = plat->num_cs; |
| 380 | |
Vignesh R | c1091e1 | 2016-07-06 09:58:56 +0530 | [diff] [blame] | 381 | return 0; |
| 382 | } |
Sekhar Nori | 4708b17 | 2010-01-27 11:10:40 -0500 | [diff] [blame] | 383 | |
Simon Glass | 3580f6d | 2021-08-07 07:24:03 -0600 | [diff] [blame] | 384 | #if CONFIG_IS_ENABLED(OF_REAL) |
Vignesh R | c1091e1 | 2016-07-06 09:58:56 +0530 | [diff] [blame] | 385 | static int davinci_ofdata_to_platadata(struct udevice *bus) |
| 386 | { |
Simon Glass | 9558862 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 387 | struct davinci_spi_plat *plat = dev_get_plat(bus); |
Jagan Teki | a147f66 | 2018-09-03 23:00:23 +0530 | [diff] [blame] | 388 | fdt_addr_t addr; |
Vignesh R | c1091e1 | 2016-07-06 09:58:56 +0530 | [diff] [blame] | 389 | |
Masahiro Yamada | a89b4de | 2020-07-17 14:36:48 +0900 | [diff] [blame] | 390 | addr = dev_read_addr(bus); |
Jagan Teki | a147f66 | 2018-09-03 23:00:23 +0530 | [diff] [blame] | 391 | if (addr == FDT_ADDR_T_NONE) |
Vignesh R | c1091e1 | 2016-07-06 09:58:56 +0530 | [diff] [blame] | 392 | return -EINVAL; |
Jagan Teki | a147f66 | 2018-09-03 23:00:23 +0530 | [diff] [blame] | 393 | |
| 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 R | c1091e1 | 2016-07-06 09:58:56 +0530 | [diff] [blame] | 396 | |
Sekhar Nori | 4708b17 | 2010-01-27 11:10:40 -0500 | [diff] [blame] | 397 | return 0; |
| 398 | } |
Vignesh R | c1091e1 | 2016-07-06 09:58:56 +0530 | [diff] [blame] | 399 | |
Vignesh R | c1091e1 | 2016-07-06 09:58:56 +0530 | [diff] [blame] | 400 | static const struct udevice_id davinci_spi_ids[] = { |
| 401 | { .compatible = "ti,keystone-spi" }, |
| 402 | { .compatible = "ti,dm6441-spi" }, |
Adam Ford | a2d2f56 | 2017-09-17 20:43:45 -0500 | [diff] [blame] | 403 | { .compatible = "ti,da830-spi" }, |
Vignesh R | c1091e1 | 2016-07-06 09:58:56 +0530 | [diff] [blame] | 404 | { } |
| 405 | }; |
Jagan Teki | a147f66 | 2018-09-03 23:00:23 +0530 | [diff] [blame] | 406 | #endif |
Vignesh R | c1091e1 | 2016-07-06 09:58:56 +0530 | [diff] [blame] | 407 | |
| 408 | U_BOOT_DRIVER(davinci_spi) = { |
| 409 | .name = "davinci_spi", |
| 410 | .id = UCLASS_SPI, |
Simon Glass | 3580f6d | 2021-08-07 07:24:03 -0600 | [diff] [blame] | 411 | #if CONFIG_IS_ENABLED(OF_REAL) |
Vignesh R | c1091e1 | 2016-07-06 09:58:56 +0530 | [diff] [blame] | 412 | .of_match = davinci_spi_ids, |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 413 | .of_to_plat = davinci_ofdata_to_platadata, |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 414 | .plat_auto = sizeof(struct davinci_spi_plat), |
Jagan Teki | a147f66 | 2018-09-03 23:00:23 +0530 | [diff] [blame] | 415 | #endif |
Vignesh R | c1091e1 | 2016-07-06 09:58:56 +0530 | [diff] [blame] | 416 | .probe = davinci_spi_probe, |
Jagan Teki | a147f66 | 2018-09-03 23:00:23 +0530 | [diff] [blame] | 417 | .ops = &davinci_spi_ops, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 418 | .priv_auto = sizeof(struct davinci_spi_slave), |
Vignesh R | c1091e1 | 2016-07-06 09:58:56 +0530 | [diff] [blame] | 419 | }; |