davinci_spi.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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, data1_reg_val = 0;
  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. /* hold cs active at end of transfer until explicitly de-asserted */
  82. data1_reg_val = (1 << SPIDAT1_CSHOLD_SHIFT) |
  83. (slave->cs << SPIDAT1_CSNR_SHIFT);
  84. writel(data1_reg_val, &ds->regs->dat1);
  85. /*
  86. * Including a minor delay. No science here. Should be good even with
  87. * no delay
  88. */
  89. writel((50 << SPI_C2TDELAY_SHIFT) |
  90. (50 << SPI_T2CDELAY_SHIFT), &ds->regs->delay);
  91. /* default chip select register */
  92. writel(SPIDEF_CSDEF0_MASK, &ds->regs->def);
  93. /* no interrupts */
  94. writel(0, &ds->regs->int0);
  95. writel(0, &ds->regs->lvl);
  96. /* enable SPI */
  97. writel((readl(&ds->regs->gcr1) |
  98. SPIGCR1_SPIENA_MASK), &ds->regs->gcr1);
  99. return 0;
  100. }
  101. void spi_release_bus(struct spi_slave *slave)
  102. {
  103. struct davinci_spi_slave *ds = to_davinci_spi(slave);
  104. /* Disable the SPI hardware */
  105. writel(SPIGCR0_SPIRST_MASK, &ds->regs->gcr0);
  106. }
  107. int spi_xfer(struct spi_slave *slave, unsigned int bitlen,
  108. const void *dout, void *din, unsigned long flags)
  109. {
  110. struct davinci_spi_slave *ds = to_davinci_spi(slave);
  111. unsigned int len, data1_reg_val = readl(&ds->regs->dat1);
  112. unsigned int i_cnt = 0, o_cnt = 0, buf_reg_val;
  113. const u8 *txp = dout; /* dout can be NULL for read operation */
  114. u8 *rxp = din; /* din can be NULL for write operation */
  115. if (bitlen == 0)
  116. /* Finish any previously submitted transfers */
  117. goto out;
  118. /*
  119. * It's not clear how non-8-bit-aligned transfers are supposed to be
  120. * represented as a stream of bytes...this is a limitation of
  121. * the current SPI interface - here we terminate on receiving such a
  122. * transfer request.
  123. */
  124. if (bitlen % 8) {
  125. /* Errors always terminate an ongoing transfer */
  126. flags |= SPI_XFER_END;
  127. goto out;
  128. }
  129. len = bitlen / 8;
  130. /* do an empty read to clear the current contents */
  131. readl(&ds->regs->buf);
  132. /* keep writing and reading 1 byte until done */
  133. while ((i_cnt < len) || (o_cnt < len)) {
  134. /* read RX buffer and flags */
  135. buf_reg_val = readl(&ds->regs->buf);
  136. /* if data is available */
  137. if ((i_cnt < len) &&
  138. (buf_reg_val & SPIBUF_RXEMPTY_MASK) == 0) {
  139. /*
  140. * If there is no read buffer simply
  141. * ignore the read character
  142. */
  143. if (rxp)
  144. *rxp++ = buf_reg_val & 0xFF;
  145. /* increment read words count */
  146. i_cnt++;
  147. }
  148. /*
  149. * if the tx buffer is empty and there
  150. * is still data to transmit
  151. */
  152. if ((o_cnt < len) &&
  153. ((buf_reg_val & SPIBUF_TXFULL_MASK) == 0)) {
  154. /* write the data */
  155. data1_reg_val &= ~0xFFFF;
  156. if (txp)
  157. data1_reg_val |= *txp++;
  158. /*
  159. * Write to DAT1 is required to keep
  160. * the serial transfer going.
  161. * We just terminate when we reach the end.
  162. */
  163. if ((o_cnt == (len - 1)) && (flags & SPI_XFER_END)) {
  164. /* clear CS hold */
  165. writel(data1_reg_val &
  166. ~(1 << SPIDAT1_CSHOLD_SHIFT),
  167. &ds->regs->dat1);
  168. } else {
  169. /* enable CS hold and write TX register */
  170. data1_reg_val |= ((1 << SPIDAT1_CSHOLD_SHIFT) |
  171. (slave->cs << SPIDAT1_CSNR_SHIFT));
  172. writel(data1_reg_val, &ds->regs->dat1);
  173. }
  174. /* increment written words count */
  175. o_cnt++;
  176. }
  177. }
  178. return 0;
  179. out:
  180. if (flags & SPI_XFER_END) {
  181. writel(data1_reg_val &
  182. ~(1 << SPIDAT1_CSHOLD_SHIFT), &ds->regs->dat1);
  183. }
  184. return 0;
  185. }
  186. int spi_cs_is_valid(unsigned int bus, unsigned int cs)
  187. {
  188. return bus == 0 && cs == 0;
  189. }
  190. void spi_cs_activate(struct spi_slave *slave)
  191. {
  192. /* do nothing */
  193. }
  194. void spi_cs_deactivate(struct spi_slave *slave)
  195. {
  196. /* do nothing */
  197. }