davinci_spi.c 7.4 KB

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