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 = malloc(sizeof(*xilspi));
  72. if (!xilspi) {
  73. printf("XILSPI error: %s: malloc of SPI structure failed\n",
  74. __func__);
  75. return NULL;
  76. }
  77. xilspi->slave.bus = bus;
  78. xilspi->slave.cs = cs;
  79. xilspi->regs = (struct xilinx_spi_reg *)xilinx_spi_base_list[bus];
  80. xilspi->freq = max_hz;
  81. xilspi->mode = mode;
  82. debug("%s: bus:%i cs:%i base:%p mode:%x max_hz:%d\n", __func__,
  83. bus, cs, xilspi->regs, xilspi->mode, xilspi->freq);
  84. return &xilspi->slave;
  85. }
  86. void spi_free_slave(struct spi_slave *slave)
  87. {
  88. struct xilinx_spi_slave *xilspi = to_xilinx_spi_slave(slave);
  89. free(xilspi);
  90. }
  91. int spi_claim_bus(struct spi_slave *slave)
  92. {
  93. struct xilinx_spi_slave *xilspi = to_xilinx_spi_slave(slave);
  94. u32 spicr;
  95. debug("%s: bus:%i cs:%i\n", __func__, slave->bus, slave->cs);
  96. writel(SPISSR_OFF, &xilspi->regs->spissr);
  97. spicr = XILSPI_SPICR_DFLT_ON;
  98. if (xilspi->mode & SPI_LSB_FIRST)
  99. spicr |= SPICR_LSB_FIRST;
  100. if (xilspi->mode & SPI_CPHA)
  101. spicr |= SPICR_CPHA;
  102. if (xilspi->mode & SPI_CPOL)
  103. spicr |= SPICR_CPOL;
  104. if (xilspi->mode & SPI_LOOP)
  105. spicr |= SPICR_LOOP;
  106. writel(spicr, &xilspi->regs->spicr);
  107. return 0;
  108. }
  109. void spi_release_bus(struct spi_slave *slave)
  110. {
  111. struct xilinx_spi_slave *xilspi = to_xilinx_spi_slave(slave);
  112. debug("%s: bus:%i cs:%i\n", __func__, slave->bus, slave->cs);
  113. writel(SPISSR_OFF, &xilspi->regs->spissr);
  114. writel(XILSPI_SPICR_DFLT_OFF, &xilspi->regs->spicr);
  115. }
  116. int spi_xfer(struct spi_slave *slave, unsigned int bitlen, const void *dout,
  117. void *din, unsigned long flags)
  118. {
  119. struct xilinx_spi_slave *xilspi = to_xilinx_spi_slave(slave);
  120. /* assume spi core configured to do 8 bit transfers */
  121. unsigned int bytes = bitlen / XILSPI_MAX_XFER_BITS;
  122. const unsigned char *txp = dout;
  123. unsigned char *rxp = din;
  124. unsigned rxecount = 17; /* max. 16 elements in FIFO, leftover 1 */
  125. debug("%s: bus:%i cs:%i bitlen:%i bytes:%i flags:%lx\n", __func__,
  126. slave->bus, slave->cs, bitlen, bytes, flags);
  127. if (bitlen == 0)
  128. goto done;
  129. if (bitlen % XILSPI_MAX_XFER_BITS) {
  130. printf("XILSPI warning: %s: Not a multiple of %d bits\n",
  131. __func__, XILSPI_MAX_XFER_BITS);
  132. flags |= SPI_XFER_END;
  133. goto done;
  134. }
  135. /* empty read buffer */
  136. while (rxecount && !(readl(&xilspi->regs->spisr) & SPISR_RX_EMPTY)) {
  137. readl(&xilspi->regs->spidrr);
  138. rxecount--;
  139. }
  140. if (!rxecount) {
  141. printf("XILSPI error: %s: Rx buffer not empty\n", __func__);
  142. return -1;
  143. }
  144. if (flags & SPI_XFER_BEGIN)
  145. spi_cs_activate(slave);
  146. while (bytes--) {
  147. unsigned timeout = /* at least 1usec or greater, leftover 1 */
  148. xilspi->freq > XILSPI_MAX_XFER_BITS * 1000000 ? 2 :
  149. (XILSPI_MAX_XFER_BITS * 1000000 / xilspi->freq) + 1;
  150. /* get Tx element from data out buffer and count up */
  151. unsigned char d = txp ? *txp++ : CONFIG_XILINX_SPI_IDLE_VAL;
  152. debug("%s: tx:%x ", __func__, d);
  153. /* write out and wait for processing (receive data) */
  154. writel(d & SPIDTR_8BIT_MASK, &xilspi->regs->spidtr);
  155. while (timeout && readl(&xilspi->regs->spisr)
  156. & SPISR_RX_EMPTY) {
  157. timeout--;
  158. udelay(1);
  159. }
  160. if (!timeout) {
  161. printf("XILSPI error: %s: Xfer timeout\n", __func__);
  162. return -1;
  163. }
  164. /* read Rx element and push into data in buffer */
  165. d = readl(&xilspi->regs->spidrr) & SPIDRR_8BIT_MASK;
  166. if (rxp)
  167. *rxp++ = d;
  168. debug("rx:%x\n", d);
  169. }
  170. done:
  171. if (flags & SPI_XFER_END)
  172. spi_cs_deactivate(slave);
  173. return 0;
  174. }