xilinx_spi.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /*
  2. * Xilinx SPI driver
  3. *
  4. * supports 8 bit SPI transfers only, with or w/o FIFO
  5. *
  6. * based on bfin_spi.c, by way of altera_spi.c
  7. * Copyright (c) 2005-2008 Analog Devices Inc.
  8. * Copyright (c) 2010 Thomas Chou <thomas@wytron.com.tw>
  9. * Copyright (c) 2010 Graeme Smecher <graeme.smecher@mail.mcgill.ca>
  10. * Copyright (c) 2012 Stephan Linz <linz@li-pro.net>
  11. *
  12. * Licensed under the GPL-2 or later.
  13. *
  14. * [0]: http://www.xilinx.com/support/documentation
  15. *
  16. * [S]: [0]/ip_documentation/xps_spi.pdf
  17. * [0]/ip_documentation/axi_spi_ds742.pdf
  18. */
  19. #include <config.h>
  20. #include <common.h>
  21. #include <malloc.h>
  22. #include <spi.h>
  23. #include "xilinx_spi.h"
  24. #ifndef CONFIG_SYS_XILINX_SPI_LIST
  25. #define CONFIG_SYS_XILINX_SPI_LIST { CONFIG_SYS_SPI_BASE }
  26. #endif
  27. #ifndef CONFIG_XILINX_SPI_IDLE_VAL
  28. #define CONFIG_XILINX_SPI_IDLE_VAL 0xff
  29. #endif
  30. #define XILSPI_SPICR_DFLT_ON (SPICR_MANUAL_SS | \
  31. SPICR_MASTER_MODE | \
  32. SPICR_SPE)
  33. #define XILSPI_SPICR_DFLT_OFF (SPICR_MASTER_INHIBIT | \
  34. SPICR_MANUAL_SS)
  35. #define XILSPI_MAX_XFER_BITS 8
  36. static unsigned long xilinx_spi_base_list[] = CONFIG_SYS_XILINX_SPI_LIST;
  37. __attribute__((weak))
  38. int spi_cs_is_valid(unsigned int bus, unsigned int cs)
  39. {
  40. return bus < ARRAY_SIZE(xilinx_spi_base_list) && cs < 32;
  41. }
  42. __attribute__((weak))
  43. void spi_cs_activate(struct spi_slave *slave)
  44. {
  45. struct xilinx_spi_slave *xilspi = to_xilinx_spi_slave(slave);
  46. writel(SPISSR_ACT(slave->cs), &xilspi->regs->spissr);
  47. }
  48. __attribute__((weak))
  49. void spi_cs_deactivate(struct spi_slave *slave)
  50. {
  51. struct xilinx_spi_slave *xilspi = to_xilinx_spi_slave(slave);
  52. writel(SPISSR_OFF, &xilspi->regs->spissr);
  53. }
  54. void spi_init(void)
  55. {
  56. /* do nothing */
  57. }
  58. void spi_set_speed(struct spi_slave *slave, uint hz)
  59. {
  60. /* xilinx spi core does not support programmable speed */
  61. }
  62. struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
  63. unsigned int max_hz, unsigned int mode)
  64. {
  65. struct xilinx_spi_slave *xilspi;
  66. if (!spi_cs_is_valid(bus, cs)) {
  67. printf("XILSPI error: %s: unsupported bus %d / cs %d\n",
  68. __func__, bus, cs);
  69. return NULL;
  70. }
  71. xilspi = spi_alloc_slave(struct xilinx_spi_slave, bus, cs);
  72. if (!xilspi) {
  73. printf("XILSPI error: %s: malloc of SPI structure failed\n",
  74. __func__);
  75. return NULL;
  76. }
  77. xilspi->regs = (struct xilinx_spi_reg *)xilinx_spi_base_list[bus];
  78. xilspi->freq = max_hz;
  79. xilspi->mode = mode;
  80. debug("%s: bus:%i cs:%i base:%p mode:%x max_hz:%d\n", __func__,
  81. bus, cs, xilspi->regs, xilspi->mode, xilspi->freq);
  82. writel(SPISSR_RESET_VALUE, &xilspi->regs->srr);
  83. return &xilspi->slave;
  84. }
  85. void spi_free_slave(struct spi_slave *slave)
  86. {
  87. struct xilinx_spi_slave *xilspi = to_xilinx_spi_slave(slave);
  88. free(xilspi);
  89. }
  90. int spi_claim_bus(struct spi_slave *slave)
  91. {
  92. struct xilinx_spi_slave *xilspi = to_xilinx_spi_slave(slave);
  93. u32 spicr;
  94. debug("%s: bus:%i cs:%i\n", __func__, slave->bus, slave->cs);
  95. writel(SPISSR_OFF, &xilspi->regs->spissr);
  96. spicr = XILSPI_SPICR_DFLT_ON;
  97. if (xilspi->mode & SPI_LSB_FIRST)
  98. spicr |= SPICR_LSB_FIRST;
  99. if (xilspi->mode & SPI_CPHA)
  100. spicr |= SPICR_CPHA;
  101. if (xilspi->mode & SPI_CPOL)
  102. spicr |= SPICR_CPOL;
  103. if (xilspi->mode & SPI_LOOP)
  104. spicr |= SPICR_LOOP;
  105. writel(spicr, &xilspi->regs->spicr);
  106. return 0;
  107. }
  108. void spi_release_bus(struct spi_slave *slave)
  109. {
  110. struct xilinx_spi_slave *xilspi = to_xilinx_spi_slave(slave);
  111. debug("%s: bus:%i cs:%i\n", __func__, slave->bus, slave->cs);
  112. writel(SPISSR_OFF, &xilspi->regs->spissr);
  113. writel(XILSPI_SPICR_DFLT_OFF, &xilspi->regs->spicr);
  114. }
  115. int spi_xfer(struct spi_slave *slave, unsigned int bitlen, const void *dout,
  116. void *din, unsigned long flags)
  117. {
  118. struct xilinx_spi_slave *xilspi = to_xilinx_spi_slave(slave);
  119. /* assume spi core configured to do 8 bit transfers */
  120. unsigned int bytes = bitlen / XILSPI_MAX_XFER_BITS;
  121. const unsigned char *txp = dout;
  122. unsigned char *rxp = din;
  123. unsigned rxecount = 17; /* max. 16 elements in FIFO, leftover 1 */
  124. debug("%s: bus:%i cs:%i bitlen:%i bytes:%i flags:%lx\n", __func__,
  125. slave->bus, slave->cs, bitlen, bytes, flags);
  126. if (bitlen == 0)
  127. goto done;
  128. if (bitlen % XILSPI_MAX_XFER_BITS) {
  129. printf("XILSPI warning: %s: Not a multiple of %d bits\n",
  130. __func__, XILSPI_MAX_XFER_BITS);
  131. flags |= SPI_XFER_END;
  132. goto done;
  133. }
  134. /* empty read buffer */
  135. while (rxecount && !(readl(&xilspi->regs->spisr) & SPISR_RX_EMPTY)) {
  136. readl(&xilspi->regs->spidrr);
  137. rxecount--;
  138. }
  139. if (!rxecount) {
  140. printf("XILSPI error: %s: Rx buffer not empty\n", __func__);
  141. return -1;
  142. }
  143. if (flags & SPI_XFER_BEGIN)
  144. spi_cs_activate(slave);
  145. while (bytes--) {
  146. unsigned timeout = /* at least 1usec or greater, leftover 1 */
  147. xilspi->freq > XILSPI_MAX_XFER_BITS * 1000000 ? 2 :
  148. (XILSPI_MAX_XFER_BITS * 1000000 / xilspi->freq) + 1;
  149. /* get Tx element from data out buffer and count up */
  150. unsigned char d = txp ? *txp++ : CONFIG_XILINX_SPI_IDLE_VAL;
  151. debug("%s: tx:%x ", __func__, d);
  152. /* write out and wait for processing (receive data) */
  153. writel(d & SPIDTR_8BIT_MASK, &xilspi->regs->spidtr);
  154. while (timeout && readl(&xilspi->regs->spisr)
  155. & SPISR_RX_EMPTY) {
  156. timeout--;
  157. udelay(1);
  158. }
  159. if (!timeout) {
  160. printf("XILSPI error: %s: Xfer timeout\n", __func__);
  161. return -1;
  162. }
  163. /* read Rx element and push into data in buffer */
  164. d = readl(&xilspi->regs->spidrr) & SPIDRR_8BIT_MASK;
  165. if (rxp)
  166. *rxp++ = d;
  167. debug("rx:%x\n", d);
  168. }
  169. done:
  170. if (flags & SPI_XFER_END)
  171. spi_cs_deactivate(slave);
  172. return 0;
  173. }