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 | /* |
| 3 | * Copyright (C) 2009 Texas Instruments Incorporated - http://www.ti.com/ |
| 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 | |
Sekhar Nori | 4708b17 | 2010-01-27 11:10:40 -0500 | [diff] [blame] | 11 | #include <common.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> |
| 15 | #include <asm/io.h> |
| 16 | #include <asm/arch/hardware.h> |
Vignesh R | c1091e1 | 2016-07-06 09:58:56 +0530 | [diff] [blame] | 17 | #include <dm.h> |
Jagan Teki | a147f66 | 2018-09-03 23:00:23 +0530 | [diff] [blame] | 18 | #include <dm/platform_data/spi_davinci.h> |
Simon Glass | dbd7954 | 2020-05-10 11:40:11 -0600 | [diff] [blame^] | 19 | #include <linux/delay.h> |
Jagan Teki | 742d53d | 2015-06-27 00:51:28 +0530 | [diff] [blame] | 20 | |
Jagan Teki | 742d53d | 2015-06-27 00:51:28 +0530 | [diff] [blame] | 21 | /* SPIGCR0 */ |
| 22 | #define SPIGCR0_SPIENA_MASK 0x1 |
| 23 | #define SPIGCR0_SPIRST_MASK 0x0 |
| 24 | |
| 25 | /* SPIGCR0 */ |
| 26 | #define SPIGCR1_CLKMOD_MASK BIT(1) |
| 27 | #define SPIGCR1_MASTER_MASK BIT(0) |
| 28 | #define SPIGCR1_SPIENA_MASK BIT(24) |
| 29 | |
| 30 | /* SPIPC0 */ |
| 31 | #define SPIPC0_DIFUN_MASK BIT(11) /* SIMO */ |
| 32 | #define SPIPC0_DOFUN_MASK BIT(10) /* SOMI */ |
| 33 | #define SPIPC0_CLKFUN_MASK BIT(9) /* CLK */ |
| 34 | #define SPIPC0_EN0FUN_MASK BIT(0) |
| 35 | |
| 36 | /* SPIFMT0 */ |
| 37 | #define SPIFMT_SHIFTDIR_SHIFT 20 |
| 38 | #define SPIFMT_POLARITY_SHIFT 17 |
| 39 | #define SPIFMT_PHASE_SHIFT 16 |
| 40 | #define SPIFMT_PRESCALE_SHIFT 8 |
| 41 | |
| 42 | /* SPIDAT1 */ |
| 43 | #define SPIDAT1_CSHOLD_SHIFT 28 |
| 44 | #define SPIDAT1_CSNR_SHIFT 16 |
| 45 | |
| 46 | /* SPIDELAY */ |
| 47 | #define SPI_C2TDELAY_SHIFT 24 |
| 48 | #define SPI_T2CDELAY_SHIFT 16 |
| 49 | |
| 50 | /* SPIBUF */ |
| 51 | #define SPIBUF_RXEMPTY_MASK BIT(31) |
| 52 | #define SPIBUF_TXFULL_MASK BIT(29) |
| 53 | |
| 54 | /* SPIDEF */ |
| 55 | #define SPIDEF_CSDEF0_MASK BIT(0) |
| 56 | |
Vignesh R | c1091e1 | 2016-07-06 09:58:56 +0530 | [diff] [blame] | 57 | #ifndef CONFIG_DM_SPI |
Jagan Teki | 742d53d | 2015-06-27 00:51:28 +0530 | [diff] [blame] | 58 | #define SPI0_BUS 0 |
| 59 | #define SPI0_BASE CONFIG_SYS_SPI_BASE |
| 60 | /* |
| 61 | * Define default SPI0_NUM_CS as 1 for existing platforms that uses this |
| 62 | * driver. Platform can configure number of CS using CONFIG_SYS_SPI0_NUM_CS |
| 63 | * if more than one CS is supported and by defining CONFIG_SYS_SPI0. |
| 64 | */ |
| 65 | #ifndef CONFIG_SYS_SPI0 |
| 66 | #define SPI0_NUM_CS 1 |
| 67 | #else |
| 68 | #define SPI0_NUM_CS CONFIG_SYS_SPI0_NUM_CS |
| 69 | #endif |
| 70 | |
| 71 | /* |
| 72 | * define CONFIG_SYS_SPI1 when platform has spi-1 device (bus #1) and |
| 73 | * CONFIG_SYS_SPI1_NUM_CS defines number of CS on this bus |
| 74 | */ |
| 75 | #ifdef CONFIG_SYS_SPI1 |
| 76 | #define SPI1_BUS 1 |
| 77 | #define SPI1_NUM_CS CONFIG_SYS_SPI1_NUM_CS |
| 78 | #define SPI1_BASE CONFIG_SYS_SPI1_BASE |
| 79 | #endif |
| 80 | |
| 81 | /* |
| 82 | * define CONFIG_SYS_SPI2 when platform has spi-2 device (bus #2) and |
| 83 | * CONFIG_SYS_SPI2_NUM_CS defines number of CS on this bus |
| 84 | */ |
| 85 | #ifdef CONFIG_SYS_SPI2 |
| 86 | #define SPI2_BUS 2 |
| 87 | #define SPI2_NUM_CS CONFIG_SYS_SPI2_NUM_CS |
| 88 | #define SPI2_BASE CONFIG_SYS_SPI2_BASE |
| 89 | #endif |
Vignesh R | c1091e1 | 2016-07-06 09:58:56 +0530 | [diff] [blame] | 90 | #endif |
| 91 | |
| 92 | DECLARE_GLOBAL_DATA_PTR; |
Jagan Teki | 742d53d | 2015-06-27 00:51:28 +0530 | [diff] [blame] | 93 | |
Jagan Teki | ccaa8fc | 2015-06-27 00:51:29 +0530 | [diff] [blame] | 94 | /* davinci spi register set */ |
| 95 | struct davinci_spi_regs { |
| 96 | dv_reg gcr0; /* 0x00 */ |
| 97 | dv_reg gcr1; /* 0x04 */ |
| 98 | dv_reg int0; /* 0x08 */ |
| 99 | dv_reg lvl; /* 0x0c */ |
| 100 | dv_reg flg; /* 0x10 */ |
| 101 | dv_reg pc0; /* 0x14 */ |
| 102 | dv_reg pc1; /* 0x18 */ |
| 103 | dv_reg pc2; /* 0x1c */ |
| 104 | dv_reg pc3; /* 0x20 */ |
| 105 | dv_reg pc4; /* 0x24 */ |
| 106 | dv_reg pc5; /* 0x28 */ |
| 107 | dv_reg rsvd[3]; |
| 108 | dv_reg dat0; /* 0x38 */ |
| 109 | dv_reg dat1; /* 0x3c */ |
| 110 | dv_reg buf; /* 0x40 */ |
| 111 | dv_reg emu; /* 0x44 */ |
| 112 | dv_reg delay; /* 0x48 */ |
| 113 | dv_reg def; /* 0x4c */ |
| 114 | dv_reg fmt0; /* 0x50 */ |
| 115 | dv_reg fmt1; /* 0x54 */ |
| 116 | dv_reg fmt2; /* 0x58 */ |
| 117 | dv_reg fmt3; /* 0x5c */ |
| 118 | dv_reg intvec0; /* 0x60 */ |
| 119 | dv_reg intvec1; /* 0x64 */ |
| 120 | }; |
| 121 | |
| 122 | /* davinci spi slave */ |
Jagan Teki | 742d53d | 2015-06-27 00:51:28 +0530 | [diff] [blame] | 123 | struct davinci_spi_slave { |
Vignesh R | c1091e1 | 2016-07-06 09:58:56 +0530 | [diff] [blame] | 124 | #ifndef CONFIG_DM_SPI |
Jagan Teki | 742d53d | 2015-06-27 00:51:28 +0530 | [diff] [blame] | 125 | struct spi_slave slave; |
Vignesh R | c1091e1 | 2016-07-06 09:58:56 +0530 | [diff] [blame] | 126 | #endif |
Jagan Teki | 742d53d | 2015-06-27 00:51:28 +0530 | [diff] [blame] | 127 | struct davinci_spi_regs *regs; |
Vignesh R | c1091e1 | 2016-07-06 09:58:56 +0530 | [diff] [blame] | 128 | unsigned int freq; /* current SPI bus frequency */ |
| 129 | unsigned int mode; /* current SPI mode used */ |
| 130 | u8 num_cs; /* total no. of CS available */ |
| 131 | u8 cur_cs; /* CS of current slave */ |
| 132 | bool half_duplex; /* true, if master is half-duplex only */ |
Jagan Teki | 742d53d | 2015-06-27 00:51:28 +0530 | [diff] [blame] | 133 | }; |
| 134 | |
Nick Thompson | 213e476 | 2010-06-22 11:06:01 -0400 | [diff] [blame] | 135 | /* |
| 136 | * This functions needs to act like a macro to avoid pipeline reloads in the |
| 137 | * loops below. Use always_inline. This gains us about 160KiB/s and the bloat |
| 138 | * appears to be zero bytes (da830). |
| 139 | */ |
| 140 | __attribute__((always_inline)) |
| 141 | static inline u32 davinci_spi_xfer_data(struct davinci_spi_slave *ds, u32 data) |
| 142 | { |
| 143 | u32 buf_reg_val; |
| 144 | |
| 145 | /* send out data */ |
| 146 | writel(data, &ds->regs->dat1); |
| 147 | |
| 148 | /* wait for the data to clock in/out */ |
| 149 | while ((buf_reg_val = readl(&ds->regs->buf)) & SPIBUF_RXEMPTY_MASK) |
| 150 | ; |
| 151 | |
| 152 | return buf_reg_val; |
| 153 | } |
| 154 | |
Vignesh R | c1091e1 | 2016-07-06 09:58:56 +0530 | [diff] [blame] | 155 | 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] | 156 | u8 *rxp, unsigned long flags) |
| 157 | { |
Nick Thompson | 213e476 | 2010-06-22 11:06:01 -0400 | [diff] [blame] | 158 | unsigned int data1_reg_val; |
| 159 | |
| 160 | /* enable CS hold, CS[n] and clear the data bits */ |
| 161 | data1_reg_val = ((1 << SPIDAT1_CSHOLD_SHIFT) | |
Vignesh R | c1091e1 | 2016-07-06 09:58:56 +0530 | [diff] [blame] | 162 | (ds->cur_cs << SPIDAT1_CSNR_SHIFT)); |
Nick Thompson | 213e476 | 2010-06-22 11:06:01 -0400 | [diff] [blame] | 163 | |
| 164 | /* wait till TXFULL is deasserted */ |
| 165 | while (readl(&ds->regs->buf) & SPIBUF_TXFULL_MASK) |
| 166 | ; |
| 167 | |
| 168 | /* preload the TX buffer to avoid clock starvation */ |
| 169 | writel(data1_reg_val, &ds->regs->dat1); |
| 170 | |
| 171 | /* keep reading 1 byte until only 1 byte left */ |
| 172 | while ((len--) > 1) |
| 173 | *rxp++ = davinci_spi_xfer_data(ds, data1_reg_val); |
| 174 | |
| 175 | /* clear CS hold when we reach the end */ |
| 176 | if (flags & SPI_XFER_END) |
| 177 | data1_reg_val &= ~(1 << SPIDAT1_CSHOLD_SHIFT); |
| 178 | |
| 179 | /* read the last byte */ |
| 180 | *rxp = davinci_spi_xfer_data(ds, data1_reg_val); |
| 181 | |
| 182 | return 0; |
| 183 | } |
| 184 | |
Vignesh R | c1091e1 | 2016-07-06 09:58:56 +0530 | [diff] [blame] | 185 | 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] | 186 | const u8 *txp, unsigned long flags) |
Nick Thompson | 213e476 | 2010-06-22 11:06:01 -0400 | [diff] [blame] | 187 | { |
Nick Thompson | 213e476 | 2010-06-22 11:06:01 -0400 | [diff] [blame] | 188 | unsigned int data1_reg_val; |
| 189 | |
| 190 | /* enable CS hold and clear the data bits */ |
| 191 | data1_reg_val = ((1 << SPIDAT1_CSHOLD_SHIFT) | |
Vignesh R | c1091e1 | 2016-07-06 09:58:56 +0530 | [diff] [blame] | 192 | (ds->cur_cs << SPIDAT1_CSNR_SHIFT)); |
Nick Thompson | 213e476 | 2010-06-22 11:06:01 -0400 | [diff] [blame] | 193 | |
| 194 | /* wait till TXFULL is deasserted */ |
| 195 | while (readl(&ds->regs->buf) & SPIBUF_TXFULL_MASK) |
| 196 | ; |
| 197 | |
| 198 | /* preload the TX buffer to avoid clock starvation */ |
| 199 | if (len > 2) { |
| 200 | writel(data1_reg_val | *txp++, &ds->regs->dat1); |
| 201 | len--; |
| 202 | } |
| 203 | |
| 204 | /* keep writing 1 byte until only 1 byte left */ |
| 205 | while ((len--) > 1) |
| 206 | davinci_spi_xfer_data(ds, data1_reg_val | *txp++); |
| 207 | |
| 208 | /* clear CS hold when we reach the end */ |
| 209 | if (flags & SPI_XFER_END) |
| 210 | data1_reg_val &= ~(1 << SPIDAT1_CSHOLD_SHIFT); |
| 211 | |
| 212 | /* write the last byte */ |
| 213 | davinci_spi_xfer_data(ds, data1_reg_val | *txp); |
| 214 | |
| 215 | return 0; |
| 216 | } |
| 217 | |
Vignesh R | c1091e1 | 2016-07-06 09:58:56 +0530 | [diff] [blame] | 218 | static int davinci_spi_read_write(struct davinci_spi_slave *ds, unsigned |
| 219 | int len, u8 *rxp, const u8 *txp, |
| 220 | unsigned long flags) |
Nick Thompson | 213e476 | 2010-06-22 11:06:01 -0400 | [diff] [blame] | 221 | { |
Nick Thompson | 213e476 | 2010-06-22 11:06:01 -0400 | [diff] [blame] | 222 | unsigned int data1_reg_val; |
| 223 | |
| 224 | /* enable CS hold and clear the data bits */ |
| 225 | data1_reg_val = ((1 << SPIDAT1_CSHOLD_SHIFT) | |
Vignesh R | c1091e1 | 2016-07-06 09:58:56 +0530 | [diff] [blame] | 226 | (ds->cur_cs << SPIDAT1_CSNR_SHIFT)); |
Nick Thompson | 213e476 | 2010-06-22 11:06:01 -0400 | [diff] [blame] | 227 | |
| 228 | /* wait till TXFULL is deasserted */ |
| 229 | while (readl(&ds->regs->buf) & SPIBUF_TXFULL_MASK) |
| 230 | ; |
| 231 | |
| 232 | /* keep reading and writing 1 byte until only 1 byte left */ |
| 233 | while ((len--) > 1) |
| 234 | *rxp++ = davinci_spi_xfer_data(ds, data1_reg_val | *txp++); |
| 235 | |
| 236 | /* clear CS hold when we reach the end */ |
| 237 | if (flags & SPI_XFER_END) |
| 238 | data1_reg_val &= ~(1 << SPIDAT1_CSHOLD_SHIFT); |
| 239 | |
| 240 | /* read and write the last byte */ |
| 241 | *rxp = davinci_spi_xfer_data(ds, data1_reg_val | *txp); |
| 242 | |
| 243 | return 0; |
| 244 | } |
Vignesh R | c1091e1 | 2016-07-06 09:58:56 +0530 | [diff] [blame] | 245 | |
| 246 | |
| 247 | static int __davinci_spi_claim_bus(struct davinci_spi_slave *ds, int cs) |
| 248 | { |
| 249 | unsigned int mode = 0, scalar; |
| 250 | |
| 251 | /* Enable the SPI hardware */ |
| 252 | writel(SPIGCR0_SPIRST_MASK, &ds->regs->gcr0); |
| 253 | udelay(1000); |
| 254 | writel(SPIGCR0_SPIENA_MASK, &ds->regs->gcr0); |
| 255 | |
| 256 | /* Set master mode, powered up and not activated */ |
| 257 | writel(SPIGCR1_MASTER_MASK | SPIGCR1_CLKMOD_MASK, &ds->regs->gcr1); |
| 258 | |
| 259 | /* CS, CLK, SIMO and SOMI are functional pins */ |
| 260 | writel(((1 << cs) | SPIPC0_CLKFUN_MASK | |
| 261 | SPIPC0_DOFUN_MASK | SPIPC0_DIFUN_MASK), &ds->regs->pc0); |
| 262 | |
| 263 | /* setup format */ |
| 264 | scalar = ((CONFIG_SYS_SPI_CLK / ds->freq) - 1) & 0xFF; |
| 265 | |
| 266 | /* |
| 267 | * Use following format: |
| 268 | * character length = 8, |
| 269 | * MSB shifted out first |
| 270 | */ |
| 271 | if (ds->mode & SPI_CPOL) |
| 272 | mode |= SPI_CPOL; |
| 273 | if (!(ds->mode & SPI_CPHA)) |
| 274 | mode |= SPI_CPHA; |
| 275 | writel(8 | (scalar << SPIFMT_PRESCALE_SHIFT) | |
| 276 | (mode << SPIFMT_PHASE_SHIFT), &ds->regs->fmt0); |
| 277 | |
| 278 | /* |
| 279 | * Including a minor delay. No science here. Should be good even with |
| 280 | * no delay |
| 281 | */ |
| 282 | writel((50 << SPI_C2TDELAY_SHIFT) | |
| 283 | (50 << SPI_T2CDELAY_SHIFT), &ds->regs->delay); |
| 284 | |
| 285 | /* default chip select register */ |
| 286 | writel(SPIDEF_CSDEF0_MASK, &ds->regs->def); |
| 287 | |
| 288 | /* no interrupts */ |
| 289 | writel(0, &ds->regs->int0); |
| 290 | writel(0, &ds->regs->lvl); |
| 291 | |
| 292 | /* enable SPI */ |
| 293 | writel((readl(&ds->regs->gcr1) | SPIGCR1_SPIENA_MASK), &ds->regs->gcr1); |
| 294 | |
| 295 | return 0; |
| 296 | } |
| 297 | |
| 298 | static int __davinci_spi_release_bus(struct davinci_spi_slave *ds) |
| 299 | { |
| 300 | /* Disable the SPI hardware */ |
| 301 | writel(SPIGCR0_SPIRST_MASK, &ds->regs->gcr0); |
| 302 | |
| 303 | return 0; |
| 304 | } |
| 305 | |
| 306 | static int __davinci_spi_xfer(struct davinci_spi_slave *ds, |
| 307 | unsigned int bitlen, const void *dout, void *din, |
| 308 | unsigned long flags) |
| 309 | { |
| 310 | unsigned int len; |
| 311 | |
| 312 | if (bitlen == 0) |
| 313 | /* Finish any previously submitted transfers */ |
| 314 | goto out; |
| 315 | |
| 316 | /* |
| 317 | * It's not clear how non-8-bit-aligned transfers are supposed to be |
| 318 | * represented as a stream of bytes...this is a limitation of |
| 319 | * the current SPI interface - here we terminate on receiving such a |
| 320 | * transfer request. |
| 321 | */ |
| 322 | if (bitlen % 8) { |
| 323 | /* Errors always terminate an ongoing transfer */ |
| 324 | flags |= SPI_XFER_END; |
| 325 | goto out; |
| 326 | } |
| 327 | |
| 328 | len = bitlen / 8; |
| 329 | |
| 330 | if (!dout) |
| 331 | return davinci_spi_read(ds, len, din, flags); |
| 332 | if (!din) |
| 333 | return davinci_spi_write(ds, len, dout, flags); |
| 334 | if (!ds->half_duplex) |
| 335 | return davinci_spi_read_write(ds, len, din, dout, flags); |
| 336 | |
| 337 | printf("SPI full duplex not supported\n"); |
| 338 | flags |= SPI_XFER_END; |
| 339 | |
| 340 | out: |
| 341 | if (flags & SPI_XFER_END) { |
| 342 | u8 dummy = 0; |
| 343 | davinci_spi_write(ds, 1, &dummy, flags); |
| 344 | } |
| 345 | return 0; |
| 346 | } |
| 347 | |
| 348 | #ifndef CONFIG_DM_SPI |
| 349 | |
| 350 | static inline struct davinci_spi_slave *to_davinci_spi(struct spi_slave *slave) |
| 351 | { |
| 352 | return container_of(slave, struct davinci_spi_slave, slave); |
| 353 | } |
Nick Thompson | 213e476 | 2010-06-22 11:06:01 -0400 | [diff] [blame] | 354 | |
Jagan Teki | ccaa8fc | 2015-06-27 00:51:29 +0530 | [diff] [blame] | 355 | int spi_cs_is_valid(unsigned int bus, unsigned int cs) |
| 356 | { |
| 357 | int ret = 0; |
| 358 | |
| 359 | switch (bus) { |
| 360 | case SPI0_BUS: |
| 361 | if (cs < SPI0_NUM_CS) |
| 362 | ret = 1; |
| 363 | break; |
| 364 | #ifdef CONFIG_SYS_SPI1 |
| 365 | case SPI1_BUS: |
| 366 | if (cs < SPI1_NUM_CS) |
| 367 | ret = 1; |
| 368 | break; |
| 369 | #endif |
| 370 | #ifdef CONFIG_SYS_SPI2 |
| 371 | case SPI2_BUS: |
| 372 | if (cs < SPI2_NUM_CS) |
| 373 | ret = 1; |
| 374 | break; |
| 375 | #endif |
| 376 | default: |
| 377 | /* Invalid bus number. Do nothing */ |
| 378 | break; |
| 379 | } |
| 380 | return ret; |
| 381 | } |
| 382 | |
| 383 | void spi_cs_activate(struct spi_slave *slave) |
| 384 | { |
| 385 | /* do nothing */ |
| 386 | } |
| 387 | |
| 388 | void spi_cs_deactivate(struct spi_slave *slave) |
| 389 | { |
| 390 | /* do nothing */ |
| 391 | } |
| 392 | |
Jagan Teki | ccaa8fc | 2015-06-27 00:51:29 +0530 | [diff] [blame] | 393 | struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs, |
| 394 | unsigned int max_hz, unsigned int mode) |
| 395 | { |
| 396 | struct davinci_spi_slave *ds; |
| 397 | |
| 398 | if (!spi_cs_is_valid(bus, cs)) |
| 399 | return NULL; |
| 400 | |
| 401 | ds = spi_alloc_slave(struct davinci_spi_slave, bus, cs); |
| 402 | if (!ds) |
| 403 | return NULL; |
| 404 | |
| 405 | switch (bus) { |
| 406 | case SPI0_BUS: |
| 407 | ds->regs = (struct davinci_spi_regs *)SPI0_BASE; |
| 408 | break; |
| 409 | #ifdef CONFIG_SYS_SPI1 |
| 410 | case SPI1_BUS: |
| 411 | ds->regs = (struct davinci_spi_regs *)SPI1_BASE; |
| 412 | break; |
| 413 | #endif |
| 414 | #ifdef CONFIG_SYS_SPI2 |
| 415 | case SPI2_BUS: |
| 416 | ds->regs = (struct davinci_spi_regs *)SPI2_BASE; |
| 417 | break; |
| 418 | #endif |
| 419 | default: /* Invalid bus number */ |
| 420 | return NULL; |
| 421 | } |
| 422 | |
| 423 | ds->freq = max_hz; |
Vignesh R | c1091e1 | 2016-07-06 09:58:56 +0530 | [diff] [blame] | 424 | ds->mode = mode; |
Jagan Teki | ccaa8fc | 2015-06-27 00:51:29 +0530 | [diff] [blame] | 425 | |
| 426 | return &ds->slave; |
| 427 | } |
| 428 | |
| 429 | void spi_free_slave(struct spi_slave *slave) |
| 430 | { |
| 431 | struct davinci_spi_slave *ds = to_davinci_spi(slave); |
| 432 | |
| 433 | free(ds); |
| 434 | } |
| 435 | |
Vignesh R | c1091e1 | 2016-07-06 09:58:56 +0530 | [diff] [blame] | 436 | int spi_xfer(struct spi_slave *slave, unsigned int bitlen, |
| 437 | const void *dout, void *din, unsigned long flags) |
| 438 | { |
| 439 | struct davinci_spi_slave *ds = to_davinci_spi(slave); |
| 440 | |
| 441 | ds->cur_cs = slave->cs; |
| 442 | |
| 443 | return __davinci_spi_xfer(ds, bitlen, dout, din, flags); |
| 444 | } |
| 445 | |
Jagan Teki | ccaa8fc | 2015-06-27 00:51:29 +0530 | [diff] [blame] | 446 | int spi_claim_bus(struct spi_slave *slave) |
| 447 | { |
| 448 | struct davinci_spi_slave *ds = to_davinci_spi(slave); |
Jagan Teki | ccaa8fc | 2015-06-27 00:51:29 +0530 | [diff] [blame] | 449 | |
Vignesh R | c1091e1 | 2016-07-06 09:58:56 +0530 | [diff] [blame] | 450 | #ifdef CONFIG_SPI_HALF_DUPLEX |
| 451 | ds->half_duplex = true; |
| 452 | #else |
| 453 | ds->half_duplex = false; |
| 454 | #endif |
| 455 | return __davinci_spi_claim_bus(ds, ds->slave.cs); |
| 456 | } |
Jagan Teki | ccaa8fc | 2015-06-27 00:51:29 +0530 | [diff] [blame] | 457 | |
Vignesh R | c1091e1 | 2016-07-06 09:58:56 +0530 | [diff] [blame] | 458 | void spi_release_bus(struct spi_slave *slave) |
| 459 | { |
| 460 | struct davinci_spi_slave *ds = to_davinci_spi(slave); |
Jagan Teki | ccaa8fc | 2015-06-27 00:51:29 +0530 | [diff] [blame] | 461 | |
Vignesh R | c1091e1 | 2016-07-06 09:58:56 +0530 | [diff] [blame] | 462 | __davinci_spi_release_bus(ds); |
| 463 | } |
Jagan Teki | ccaa8fc | 2015-06-27 00:51:29 +0530 | [diff] [blame] | 464 | |
Vignesh R | c1091e1 | 2016-07-06 09:58:56 +0530 | [diff] [blame] | 465 | #else |
| 466 | static int davinci_spi_set_speed(struct udevice *bus, uint max_hz) |
| 467 | { |
| 468 | struct davinci_spi_slave *ds = dev_get_priv(bus); |
Jagan Teki | ccaa8fc | 2015-06-27 00:51:29 +0530 | [diff] [blame] | 469 | |
Vignesh R | c1091e1 | 2016-07-06 09:58:56 +0530 | [diff] [blame] | 470 | debug("%s speed %u\n", __func__, max_hz); |
| 471 | if (max_hz > CONFIG_SYS_SPI_CLK / 2) |
| 472 | return -EINVAL; |
Jagan Teki | ccaa8fc | 2015-06-27 00:51:29 +0530 | [diff] [blame] | 473 | |
Vignesh R | c1091e1 | 2016-07-06 09:58:56 +0530 | [diff] [blame] | 474 | ds->freq = max_hz; |
Jagan Teki | ccaa8fc | 2015-06-27 00:51:29 +0530 | [diff] [blame] | 475 | |
Vignesh R | c1091e1 | 2016-07-06 09:58:56 +0530 | [diff] [blame] | 476 | return 0; |
| 477 | } |
Jagan Teki | ccaa8fc | 2015-06-27 00:51:29 +0530 | [diff] [blame] | 478 | |
Vignesh R | c1091e1 | 2016-07-06 09:58:56 +0530 | [diff] [blame] | 479 | static int davinci_spi_set_mode(struct udevice *bus, uint mode) |
| 480 | { |
| 481 | struct davinci_spi_slave *ds = dev_get_priv(bus); |
Jagan Teki | ccaa8fc | 2015-06-27 00:51:29 +0530 | [diff] [blame] | 482 | |
Vignesh R | c1091e1 | 2016-07-06 09:58:56 +0530 | [diff] [blame] | 483 | debug("%s mode %u\n", __func__, mode); |
| 484 | ds->mode = mode; |
Jagan Teki | ccaa8fc | 2015-06-27 00:51:29 +0530 | [diff] [blame] | 485 | |
| 486 | return 0; |
| 487 | } |
| 488 | |
Vignesh R | c1091e1 | 2016-07-06 09:58:56 +0530 | [diff] [blame] | 489 | static int davinci_spi_claim_bus(struct udevice *dev) |
Jagan Teki | ccaa8fc | 2015-06-27 00:51:29 +0530 | [diff] [blame] | 490 | { |
Vignesh R | c1091e1 | 2016-07-06 09:58:56 +0530 | [diff] [blame] | 491 | struct dm_spi_slave_platdata *slave_plat = |
| 492 | dev_get_parent_platdata(dev); |
| 493 | struct udevice *bus = dev->parent; |
| 494 | struct davinci_spi_slave *ds = dev_get_priv(bus); |
Jagan Teki | ccaa8fc | 2015-06-27 00:51:29 +0530 | [diff] [blame] | 495 | |
Vignesh R | c1091e1 | 2016-07-06 09:58:56 +0530 | [diff] [blame] | 496 | if (slave_plat->cs >= ds->num_cs) { |
| 497 | printf("Invalid SPI chipselect\n"); |
| 498 | return -EINVAL; |
| 499 | } |
| 500 | ds->half_duplex = slave_plat->mode & SPI_PREAMBLE; |
| 501 | |
| 502 | return __davinci_spi_claim_bus(ds, slave_plat->cs); |
Jagan Teki | ccaa8fc | 2015-06-27 00:51:29 +0530 | [diff] [blame] | 503 | } |
| 504 | |
Vignesh R | c1091e1 | 2016-07-06 09:58:56 +0530 | [diff] [blame] | 505 | static int davinci_spi_release_bus(struct udevice *dev) |
Sekhar Nori | 4708b17 | 2010-01-27 11:10:40 -0500 | [diff] [blame] | 506 | { |
Vignesh R | c1091e1 | 2016-07-06 09:58:56 +0530 | [diff] [blame] | 507 | struct davinci_spi_slave *ds = dev_get_priv(dev->parent); |
Sekhar Nori | 4708b17 | 2010-01-27 11:10:40 -0500 | [diff] [blame] | 508 | |
Vignesh R | c1091e1 | 2016-07-06 09:58:56 +0530 | [diff] [blame] | 509 | return __davinci_spi_release_bus(ds); |
| 510 | } |
Sekhar Nori | 4708b17 | 2010-01-27 11:10:40 -0500 | [diff] [blame] | 511 | |
Vignesh R | c1091e1 | 2016-07-06 09:58:56 +0530 | [diff] [blame] | 512 | static int davinci_spi_xfer(struct udevice *dev, unsigned int bitlen, |
| 513 | const void *dout, void *din, |
| 514 | unsigned long flags) |
| 515 | { |
| 516 | struct dm_spi_slave_platdata *slave = |
| 517 | dev_get_parent_platdata(dev); |
| 518 | struct udevice *bus = dev->parent; |
| 519 | struct davinci_spi_slave *ds = dev_get_priv(bus); |
| 520 | |
| 521 | if (slave->cs >= ds->num_cs) { |
| 522 | printf("Invalid SPI chipselect\n"); |
| 523 | return -EINVAL; |
Sekhar Nori | 4708b17 | 2010-01-27 11:10:40 -0500 | [diff] [blame] | 524 | } |
Vignesh R | c1091e1 | 2016-07-06 09:58:56 +0530 | [diff] [blame] | 525 | ds->cur_cs = slave->cs; |
Sekhar Nori | 4708b17 | 2010-01-27 11:10:40 -0500 | [diff] [blame] | 526 | |
Vignesh R | c1091e1 | 2016-07-06 09:58:56 +0530 | [diff] [blame] | 527 | return __davinci_spi_xfer(ds, bitlen, dout, din, flags); |
| 528 | } |
Sekhar Nori | 4708b17 | 2010-01-27 11:10:40 -0500 | [diff] [blame] | 529 | |
Jagan Teki | a147f66 | 2018-09-03 23:00:23 +0530 | [diff] [blame] | 530 | static const struct dm_spi_ops davinci_spi_ops = { |
| 531 | .claim_bus = davinci_spi_claim_bus, |
| 532 | .release_bus = davinci_spi_release_bus, |
| 533 | .xfer = davinci_spi_xfer, |
| 534 | .set_speed = davinci_spi_set_speed, |
| 535 | .set_mode = davinci_spi_set_mode, |
| 536 | }; |
| 537 | |
Vignesh R | c1091e1 | 2016-07-06 09:58:56 +0530 | [diff] [blame] | 538 | static int davinci_spi_probe(struct udevice *bus) |
| 539 | { |
Jagan Teki | a147f66 | 2018-09-03 23:00:23 +0530 | [diff] [blame] | 540 | struct davinci_spi_slave *ds = dev_get_priv(bus); |
| 541 | struct davinci_spi_platdata *plat = bus->platdata; |
| 542 | ds->regs = plat->regs; |
| 543 | ds->num_cs = plat->num_cs; |
| 544 | |
Vignesh R | c1091e1 | 2016-07-06 09:58:56 +0530 | [diff] [blame] | 545 | return 0; |
| 546 | } |
Sekhar Nori | 4708b17 | 2010-01-27 11:10:40 -0500 | [diff] [blame] | 547 | |
Jagan Teki | a147f66 | 2018-09-03 23:00:23 +0530 | [diff] [blame] | 548 | #if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA) |
Vignesh R | c1091e1 | 2016-07-06 09:58:56 +0530 | [diff] [blame] | 549 | static int davinci_ofdata_to_platadata(struct udevice *bus) |
| 550 | { |
Jagan Teki | a147f66 | 2018-09-03 23:00:23 +0530 | [diff] [blame] | 551 | struct davinci_spi_platdata *plat = bus->platdata; |
| 552 | fdt_addr_t addr; |
Vignesh R | c1091e1 | 2016-07-06 09:58:56 +0530 | [diff] [blame] | 553 | |
Jagan Teki | a147f66 | 2018-09-03 23:00:23 +0530 | [diff] [blame] | 554 | addr = devfdt_get_addr(bus); |
| 555 | if (addr == FDT_ADDR_T_NONE) |
Vignesh R | c1091e1 | 2016-07-06 09:58:56 +0530 | [diff] [blame] | 556 | return -EINVAL; |
Jagan Teki | a147f66 | 2018-09-03 23:00:23 +0530 | [diff] [blame] | 557 | |
| 558 | plat->regs = (struct davinci_spi_regs *)addr; |
| 559 | 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] | 560 | |
Sekhar Nori | 4708b17 | 2010-01-27 11:10:40 -0500 | [diff] [blame] | 561 | return 0; |
| 562 | } |
Vignesh R | c1091e1 | 2016-07-06 09:58:56 +0530 | [diff] [blame] | 563 | |
Vignesh R | c1091e1 | 2016-07-06 09:58:56 +0530 | [diff] [blame] | 564 | static const struct udevice_id davinci_spi_ids[] = { |
| 565 | { .compatible = "ti,keystone-spi" }, |
| 566 | { .compatible = "ti,dm6441-spi" }, |
Adam Ford | a2d2f56 | 2017-09-17 20:43:45 -0500 | [diff] [blame] | 567 | { .compatible = "ti,da830-spi" }, |
Vignesh R | c1091e1 | 2016-07-06 09:58:56 +0530 | [diff] [blame] | 568 | { } |
| 569 | }; |
Jagan Teki | a147f66 | 2018-09-03 23:00:23 +0530 | [diff] [blame] | 570 | #endif |
Vignesh R | c1091e1 | 2016-07-06 09:58:56 +0530 | [diff] [blame] | 571 | |
| 572 | U_BOOT_DRIVER(davinci_spi) = { |
| 573 | .name = "davinci_spi", |
| 574 | .id = UCLASS_SPI, |
Jagan Teki | a147f66 | 2018-09-03 23:00:23 +0530 | [diff] [blame] | 575 | #if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA) |
Vignesh R | c1091e1 | 2016-07-06 09:58:56 +0530 | [diff] [blame] | 576 | .of_match = davinci_spi_ids, |
Vignesh R | c1091e1 | 2016-07-06 09:58:56 +0530 | [diff] [blame] | 577 | .ofdata_to_platdata = davinci_ofdata_to_platadata, |
Jagan Teki | a147f66 | 2018-09-03 23:00:23 +0530 | [diff] [blame] | 578 | .platdata_auto_alloc_size = sizeof(struct davinci_spi_platdata), |
| 579 | #endif |
Vignesh R | c1091e1 | 2016-07-06 09:58:56 +0530 | [diff] [blame] | 580 | .probe = davinci_spi_probe, |
Jagan Teki | a147f66 | 2018-09-03 23:00:23 +0530 | [diff] [blame] | 581 | .ops = &davinci_spi_ops, |
| 582 | .priv_auto_alloc_size = sizeof(struct davinci_spi_slave), |
Vignesh R | c1091e1 | 2016-07-06 09:58:56 +0530 | [diff] [blame] | 583 | }; |
| 584 | #endif |