davinci_spi.c 7.5 KB

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