davinci_spi.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. /*
  2. * Copyright (C) 2009 Texas Instruments Incorporated - http://www.ti.com/
  3. *
  4. * Driver for SPI controller on DaVinci. Based on atmel_spi.c
  5. * by Atmel Corporation
  6. *
  7. * Copyright (C) 2007 Atmel Corporation
  8. *
  9. * SPDX-License-Identifier: GPL-2.0+
  10. */
  11. #include <common.h>
  12. #include <spi.h>
  13. #include <malloc.h>
  14. #include <asm/io.h>
  15. #include <asm/arch/hardware.h>
  16. #include "davinci_spi.h"
  17. void spi_init()
  18. {
  19. /* do nothing */
  20. }
  21. struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
  22. unsigned int max_hz, unsigned int mode)
  23. {
  24. struct davinci_spi_slave *ds;
  25. if (!spi_cs_is_valid(bus, cs))
  26. return NULL;
  27. ds = spi_alloc_slave(struct davinci_spi_slave, bus, cs);
  28. if (!ds)
  29. return NULL;
  30. ds->regs = (struct davinci_spi_regs *)CONFIG_SYS_SPI_BASE;
  31. ds->freq = max_hz;
  32. return &ds->slave;
  33. }
  34. void spi_free_slave(struct spi_slave *slave)
  35. {
  36. struct davinci_spi_slave *ds = to_davinci_spi(slave);
  37. free(ds);
  38. }
  39. int spi_claim_bus(struct spi_slave *slave)
  40. {
  41. struct davinci_spi_slave *ds = to_davinci_spi(slave);
  42. unsigned int scalar;
  43. /* Enable the SPI hardware */
  44. writel(SPIGCR0_SPIRST_MASK, &ds->regs->gcr0);
  45. udelay(1000);
  46. writel(SPIGCR0_SPIENA_MASK, &ds->regs->gcr0);
  47. /* Set master mode, powered up and not activated */
  48. writel(SPIGCR1_MASTER_MASK | SPIGCR1_CLKMOD_MASK, &ds->regs->gcr1);
  49. /* CS, CLK, SIMO and SOMI are functional pins */
  50. writel((SPIPC0_EN0FUN_MASK | SPIPC0_CLKFUN_MASK |
  51. SPIPC0_DOFUN_MASK | SPIPC0_DIFUN_MASK), &ds->regs->pc0);
  52. /* setup format */
  53. scalar = ((CONFIG_SYS_SPI_CLK / ds->freq) - 1) & 0xFF;
  54. /*
  55. * Use following format:
  56. * character length = 8,
  57. * clock signal delayed by half clk cycle,
  58. * clock low in idle state - Mode 0,
  59. * MSB shifted out first
  60. */
  61. writel(8 | (scalar << SPIFMT_PRESCALE_SHIFT) |
  62. (1 << SPIFMT_PHASE_SHIFT), &ds->regs->fmt0);
  63. /*
  64. * Including a minor delay. No science here. Should be good even with
  65. * no delay
  66. */
  67. writel((50 << SPI_C2TDELAY_SHIFT) |
  68. (50 << SPI_T2CDELAY_SHIFT), &ds->regs->delay);
  69. /* default chip select register */
  70. writel(SPIDEF_CSDEF0_MASK, &ds->regs->def);
  71. /* no interrupts */
  72. writel(0, &ds->regs->int0);
  73. writel(0, &ds->regs->lvl);
  74. /* enable SPI */
  75. writel((readl(&ds->regs->gcr1) | SPIGCR1_SPIENA_MASK), &ds->regs->gcr1);
  76. return 0;
  77. }
  78. void spi_release_bus(struct spi_slave *slave)
  79. {
  80. struct davinci_spi_slave *ds = to_davinci_spi(slave);
  81. /* Disable the SPI hardware */
  82. writel(SPIGCR0_SPIRST_MASK, &ds->regs->gcr0);
  83. }
  84. /*
  85. * This functions needs to act like a macro to avoid pipeline reloads in the
  86. * loops below. Use always_inline. This gains us about 160KiB/s and the bloat
  87. * appears to be zero bytes (da830).
  88. */
  89. __attribute__((always_inline))
  90. static inline u32 davinci_spi_xfer_data(struct davinci_spi_slave *ds, u32 data)
  91. {
  92. u32 buf_reg_val;
  93. /* send out data */
  94. writel(data, &ds->regs->dat1);
  95. /* wait for the data to clock in/out */
  96. while ((buf_reg_val = readl(&ds->regs->buf)) & SPIBUF_RXEMPTY_MASK)
  97. ;
  98. return buf_reg_val;
  99. }
  100. static int davinci_spi_read(struct spi_slave *slave, unsigned int len,
  101. u8 *rxp, unsigned long flags)
  102. {
  103. struct davinci_spi_slave *ds = to_davinci_spi(slave);
  104. unsigned int data1_reg_val;
  105. /* enable CS hold, CS[n] and clear the data bits */
  106. data1_reg_val = ((1 << SPIDAT1_CSHOLD_SHIFT) |
  107. (slave->cs << SPIDAT1_CSNR_SHIFT));
  108. /* wait till TXFULL is deasserted */
  109. while (readl(&ds->regs->buf) & SPIBUF_TXFULL_MASK)
  110. ;
  111. /* preload the TX buffer to avoid clock starvation */
  112. writel(data1_reg_val, &ds->regs->dat1);
  113. /* keep reading 1 byte until only 1 byte left */
  114. while ((len--) > 1)
  115. *rxp++ = davinci_spi_xfer_data(ds, data1_reg_val);
  116. /* clear CS hold when we reach the end */
  117. if (flags & SPI_XFER_END)
  118. data1_reg_val &= ~(1 << SPIDAT1_CSHOLD_SHIFT);
  119. /* read the last byte */
  120. *rxp = davinci_spi_xfer_data(ds, data1_reg_val);
  121. return 0;
  122. }
  123. static int davinci_spi_write(struct spi_slave *slave, unsigned int len,
  124. const u8 *txp, unsigned long flags)
  125. {
  126. struct davinci_spi_slave *ds = to_davinci_spi(slave);
  127. unsigned int data1_reg_val;
  128. /* enable CS hold and clear the data bits */
  129. data1_reg_val = ((1 << SPIDAT1_CSHOLD_SHIFT) |
  130. (slave->cs << SPIDAT1_CSNR_SHIFT));
  131. /* wait till TXFULL is deasserted */
  132. while (readl(&ds->regs->buf) & SPIBUF_TXFULL_MASK)
  133. ;
  134. /* preload the TX buffer to avoid clock starvation */
  135. if (len > 2) {
  136. writel(data1_reg_val | *txp++, &ds->regs->dat1);
  137. len--;
  138. }
  139. /* keep writing 1 byte until only 1 byte left */
  140. while ((len--) > 1)
  141. davinci_spi_xfer_data(ds, data1_reg_val | *txp++);
  142. /* clear CS hold when we reach the end */
  143. if (flags & SPI_XFER_END)
  144. data1_reg_val &= ~(1 << SPIDAT1_CSHOLD_SHIFT);
  145. /* write the last byte */
  146. davinci_spi_xfer_data(ds, data1_reg_val | *txp);
  147. return 0;
  148. }
  149. #ifndef CONFIG_SPI_HALF_DUPLEX
  150. static int davinci_spi_read_write(struct spi_slave *slave, unsigned int len,
  151. u8 *rxp, const u8 *txp, unsigned long flags)
  152. {
  153. struct davinci_spi_slave *ds = to_davinci_spi(slave);
  154. unsigned int data1_reg_val;
  155. /* enable CS hold and clear the data bits */
  156. data1_reg_val = ((1 << SPIDAT1_CSHOLD_SHIFT) |
  157. (slave->cs << SPIDAT1_CSNR_SHIFT));
  158. /* wait till TXFULL is deasserted */
  159. while (readl(&ds->regs->buf) & SPIBUF_TXFULL_MASK)
  160. ;
  161. /* keep reading and writing 1 byte until only 1 byte left */
  162. while ((len--) > 1)
  163. *rxp++ = davinci_spi_xfer_data(ds, data1_reg_val | *txp++);
  164. /* clear CS hold when we reach the end */
  165. if (flags & SPI_XFER_END)
  166. data1_reg_val &= ~(1 << SPIDAT1_CSHOLD_SHIFT);
  167. /* read and write the last byte */
  168. *rxp = davinci_spi_xfer_data(ds, data1_reg_val | *txp);
  169. return 0;
  170. }
  171. #endif
  172. int spi_xfer(struct spi_slave *slave, unsigned int bitlen,
  173. const void *dout, void *din, unsigned long flags)
  174. {
  175. unsigned int len;
  176. if (bitlen == 0)
  177. /* Finish any previously submitted transfers */
  178. goto out;
  179. /*
  180. * It's not clear how non-8-bit-aligned transfers are supposed to be
  181. * represented as a stream of bytes...this is a limitation of
  182. * the current SPI interface - here we terminate on receiving such a
  183. * transfer request.
  184. */
  185. if (bitlen % 8) {
  186. /* Errors always terminate an ongoing transfer */
  187. flags |= SPI_XFER_END;
  188. goto out;
  189. }
  190. len = bitlen / 8;
  191. if (!dout)
  192. return davinci_spi_read(slave, len, din, flags);
  193. else if (!din)
  194. return davinci_spi_write(slave, len, dout, flags);
  195. #ifndef CONFIG_SPI_HALF_DUPLEX
  196. else
  197. return davinci_spi_read_write(slave, len, din, dout, flags);
  198. #else
  199. printf("SPI full duplex transaction requested with "
  200. "CONFIG_SPI_HALF_DUPLEX defined.\n");
  201. flags |= SPI_XFER_END;
  202. #endif
  203. out:
  204. if (flags & SPI_XFER_END) {
  205. u8 dummy = 0;
  206. davinci_spi_write(slave, 1, &dummy, flags);
  207. }
  208. return 0;
  209. }
  210. int spi_cs_is_valid(unsigned int bus, unsigned int cs)
  211. {
  212. return bus == 0 && cs == 0;
  213. }
  214. void spi_cs_activate(struct spi_slave *slave)
  215. {
  216. /* do nothing */
  217. }
  218. void spi_cs_deactivate(struct spi_slave *slave)
  219. {
  220. /* do nothing */
  221. }