ar71xx_spi.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /*
  2. * (C) Copyright 2010
  3. * Michael Kurz <michi.kurz@googlemail.com>
  4. *
  5. * See file CREDITS for list of people who contributed to this
  6. * project.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  21. * MA 02111-1307 USA
  22. */
  23. #include <common.h>
  24. #include <malloc.h>
  25. #include <spi.h>
  26. #include <asm/addrspace.h>
  27. #include <asm/types.h>
  28. #include <asm/ar71xx.h>
  29. /*-----------------------------------------------------------------------
  30. * Definitions
  31. */
  32. #ifdef DEBUG_SPI
  33. #define PRINTD(fmt,args...) printf (fmt ,##args)
  34. #else
  35. #define PRINTD(fmt,args...)
  36. #endif
  37. struct ar71xx_spi_slave {
  38. struct spi_slave slave;
  39. unsigned int mode;
  40. };
  41. static inline struct ar71xx_spi_slave *to_ar71xx_spi(struct spi_slave *slave)
  42. {
  43. return container_of(slave, struct ar71xx_spi_slave, slave);
  44. }
  45. /*=====================================================================*/
  46. /* Public Functions */
  47. /*=====================================================================*/
  48. /*-----------------------------------------------------------------------
  49. * Initialization
  50. */
  51. void spi_init()
  52. {
  53. PRINTD("ar71xx_spi: spi_init");
  54. // Init SPI Hardware, disable remap, set clock
  55. __raw_writel(0x43, KSEG1ADDR(AR71XX_SPI_BASE + SPI_REG_CTRL));
  56. PRINTD(" ---> out\n");
  57. }
  58. struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
  59. unsigned int max_hz, unsigned int mode)
  60. {
  61. struct ar71xx_spi_slave *ss;
  62. PRINTD("ar71xx_spi: spi_setup_slave");
  63. if ((bus != 0) || (cs > 2))
  64. return NULL;
  65. ss = malloc(sizeof(struct ar71xx_spi_slave));
  66. if (!ss)
  67. return NULL;
  68. ss->slave.bus = bus;
  69. ss->slave.cs = cs;
  70. ss->mode = mode;
  71. /* TODO: Use max_hz to limit the SCK rate */
  72. PRINTD(" ---> out\n");
  73. return &ss->slave;
  74. }
  75. void spi_free_slave(struct spi_slave *slave)
  76. {
  77. struct ar71xx_spi_slave *ss = to_ar71xx_spi(slave);
  78. free(ss);
  79. }
  80. int spi_claim_bus(struct spi_slave *slave)
  81. {
  82. return 0;
  83. }
  84. void spi_release_bus(struct spi_slave *slave)
  85. {
  86. }
  87. int spi_xfer(struct spi_slave *slave, unsigned int bitlen, const void *dout,
  88. void *din, unsigned long flags)
  89. {
  90. struct ar71xx_spi_slave *ss = to_ar71xx_spi(slave);
  91. uint8_t *rx = din;
  92. const uint8_t *tx = dout;
  93. uint8_t curbyte, curbitlen, restbits;
  94. uint32_t bytes = bitlen / 8;
  95. uint32_t out;
  96. uint32_t in;
  97. PRINTD("ar71xx_spi: spi_xfer: slave:%p bitlen:%08x dout:%p din:%p flags:%08x\n", slave, bitlen, dout, din, flags);
  98. if (flags & SPI_XFER_BEGIN) {
  99. __raw_writel(SPI_FS_GPIO, KSEG1ADDR(AR71XX_SPI_BASE + SPI_REG_FS));
  100. __raw_writel(SPI_IOC_CS_ALL, KSEG1ADDR(AR71XX_SPI_BASE + SPI_REG_IOC));
  101. }
  102. restbits = (bitlen % 8);
  103. if (restbits != 0)
  104. bytes++;
  105. // enable chip select
  106. out = SPI_IOC_CS_ALL & ~(SPI_IOC_CS(slave->cs));
  107. while (bytes--) {
  108. curbyte = 0;
  109. if (tx) {
  110. curbyte = *tx++;
  111. }
  112. if (restbits != 0) {
  113. curbitlen = restbits;
  114. curbyte <<= 8 - restbits;
  115. } else {
  116. curbitlen = 8;
  117. }
  118. PRINTD("ar71xx_spi: sending: data:%02x length:%d\n", curbyte, curbitlen);
  119. /* clock starts at inactive polarity */
  120. for (curbyte <<= (8 - curbitlen); curbitlen; curbitlen--) {
  121. if (curbyte & (1 << 7))
  122. out |= SPI_IOC_DO;
  123. else
  124. out &= ~(SPI_IOC_DO);
  125. /* setup MSB (to slave) on trailing edge */
  126. __raw_writel(out, KSEG1ADDR(AR71XX_SPI_BASE + SPI_REG_IOC));
  127. __raw_writel(out | SPI_IOC_CLK, KSEG1ADDR(AR71XX_SPI_BASE + SPI_REG_IOC));
  128. curbyte <<= 1;
  129. }
  130. in = __raw_readl(KSEG1ADDR(AR71XX_SPI_BASE + SPI_REG_RDS));
  131. PRINTD("ar71xx_spi: received:%02x\n", in);
  132. if (rx) {
  133. if (restbits == 0) {
  134. *rx++ = in;
  135. } else {
  136. *rx++ = (in << (8 - restbits));
  137. }
  138. }
  139. }
  140. if (flags & SPI_XFER_END) {
  141. __raw_writel(SPI_IOC_CS(slave->cs), KSEG1ADDR(AR71XX_SPI_BASE + SPI_REG_IOC));
  142. __raw_writel(SPI_IOC_CS_ALL, KSEG1ADDR(AR71XX_SPI_BASE + SPI_REG_IOC));
  143. __raw_writel(0, KSEG1ADDR(AR71XX_SPI_BASE + SPI_REG_FS));
  144. }
  145. PRINTD(" ---> out\n");
  146. return 0;
  147. }