davinci_spi.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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) | SPIGCR1_SPIENA_MASK), &ds->regs->gcr1);
  98. return 0;
  99. }
  100. void spi_release_bus(struct spi_slave *slave)
  101. {
  102. struct davinci_spi_slave *ds = to_davinci_spi(slave);
  103. /* Disable the SPI hardware */
  104. writel(SPIGCR0_SPIRST_MASK, &ds->regs->gcr0);
  105. }
  106. int spi_xfer(struct spi_slave *slave, unsigned int bitlen,
  107. const void *dout, void *din, unsigned long flags)
  108. {
  109. struct davinci_spi_slave *ds = to_davinci_spi(slave);
  110. unsigned int len, data1_reg_val = readl(&ds->regs->dat1);
  111. int ret, i;
  112. const u8 *txp = dout; /* dout can be NULL for read operation */
  113. u8 *rxp = din; /* din can be NULL for write operation */
  114. ret = 0;
  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. for (i = 0; i < len; i++) {
  134. /* wait till TXFULL is asserted */
  135. while (readl(&ds->regs->buf) & SPIBUF_TXFULL_MASK);
  136. /* write the data */
  137. data1_reg_val &= ~0xFFFF;
  138. if (txp) {
  139. data1_reg_val |= *txp;
  140. txp++;
  141. }
  142. /*
  143. * Write to DAT1 is required to keep the serial transfer going.
  144. * We just terminate when we reach the end.
  145. */
  146. if ((i == (len - 1)) && (flags & SPI_XFER_END)) {
  147. /* clear CS hold */
  148. writel(data1_reg_val &
  149. ~(1 << SPIDAT1_CSHOLD_SHIFT), &ds->regs->dat1);
  150. } else {
  151. /* enable CS hold */
  152. data1_reg_val |= ((1 << SPIDAT1_CSHOLD_SHIFT) |
  153. (slave->cs << SPIDAT1_CSNR_SHIFT));
  154. writel(data1_reg_val, &ds->regs->dat1);
  155. }
  156. /* read the data - wait for data availability */
  157. while (readl(&ds->regs->buf) & SPIBUF_RXEMPTY_MASK);
  158. if (rxp) {
  159. *rxp = readl(&ds->regs->buf) & 0xFF;
  160. rxp++;
  161. } else {
  162. /* simply drop the read character */
  163. readl(&ds->regs->buf);
  164. }
  165. }
  166. return 0;
  167. out:
  168. if (flags & SPI_XFER_END) {
  169. writel(data1_reg_val &
  170. ~(1 << SPIDAT1_CSHOLD_SHIFT), &ds->regs->dat1);
  171. }
  172. return 0;
  173. }
  174. int spi_cs_is_valid(unsigned int bus, unsigned int cs)
  175. {
  176. return bus == 0 && cs == 0;
  177. }
  178. void spi_cs_activate(struct spi_slave *slave)
  179. {
  180. /* do nothing */
  181. }
  182. void spi_cs_deactivate(struct spi_slave *slave)
  183. {
  184. /* do nothing */
  185. }