xilinx_spi.c 5.3 KB

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