oc_tiny_spi.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /*
  2. * Opencore tiny_spi driver
  3. *
  4. * http://opencores.org/project,tiny_spi
  5. *
  6. * based on bfin_spi.c
  7. * Copyright (c) 2005-2008 Analog Devices Inc.
  8. * Copyright (C) 2010 Thomas Chou <thomas@wytron.com.tw>
  9. *
  10. * Licensed under the GPL-2 or later.
  11. */
  12. #include <common.h>
  13. #include <asm/io.h>
  14. #include <malloc.h>
  15. #include <spi.h>
  16. #include <asm/gpio.h>
  17. #define TINY_SPI_STATUS_TXE 0x1
  18. #define TINY_SPI_STATUS_TXR 0x2
  19. struct tiny_spi_regs {
  20. unsigned rxdata; /* Rx data reg */
  21. unsigned txdata; /* Tx data reg */
  22. unsigned status; /* Status reg */
  23. unsigned control; /* Control reg */
  24. unsigned baud; /* Baud reg */
  25. };
  26. struct tiny_spi_host {
  27. uint base;
  28. uint freq;
  29. uint baudwidth;
  30. };
  31. static const struct tiny_spi_host tiny_spi_host_list[] =
  32. CONFIG_SYS_TINY_SPI_LIST;
  33. struct tiny_spi_slave {
  34. struct spi_slave slave;
  35. const struct tiny_spi_host *host;
  36. uint mode;
  37. uint baud;
  38. uint flg;
  39. };
  40. #define to_tiny_spi_slave(s) container_of(s, struct tiny_spi_slave, slave)
  41. int spi_cs_is_valid(unsigned int bus, unsigned int cs)
  42. {
  43. return bus < ARRAY_SIZE(tiny_spi_host_list) && gpio_is_valid(cs);
  44. }
  45. void spi_cs_activate(struct spi_slave *slave)
  46. {
  47. struct tiny_spi_slave *tiny_spi = to_tiny_spi_slave(slave);
  48. unsigned int cs = slave->cs;
  49. gpio_set_value(cs, tiny_spi->flg);
  50. debug("%s: SPI_CS_GPIO:%x\n", __func__, gpio_get_value(cs));
  51. }
  52. void spi_cs_deactivate(struct spi_slave *slave)
  53. {
  54. struct tiny_spi_slave *tiny_spi = to_tiny_spi_slave(slave);
  55. unsigned int cs = slave->cs;
  56. gpio_set_value(cs, !tiny_spi->flg);
  57. debug("%s: SPI_CS_GPIO:%x\n", __func__, gpio_get_value(cs));
  58. }
  59. void spi_set_speed(struct spi_slave *slave, uint hz)
  60. {
  61. struct tiny_spi_slave *tiny_spi = to_tiny_spi_slave(slave);
  62. const struct tiny_spi_host *host = tiny_spi->host;
  63. tiny_spi->baud = min(DIV_ROUND_UP(host->freq, hz * 2),
  64. (1 << host->baudwidth)) - 1;
  65. debug("%s: speed %u actual %u\n", __func__, hz,
  66. host->freq / ((tiny_spi->baud + 1) * 2));
  67. }
  68. void spi_init(void)
  69. {
  70. }
  71. struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
  72. unsigned int hz, unsigned int mode)
  73. {
  74. struct tiny_spi_slave *tiny_spi;
  75. if (!spi_cs_is_valid(bus, cs) || gpio_request(cs, "tiny_spi"))
  76. return NULL;
  77. tiny_spi = spi_alloc_slave(struct tiny_spi_slave, bus, cs);
  78. if (!tiny_spi)
  79. return NULL;
  80. tiny_spi->host = &tiny_spi_host_list[bus];
  81. tiny_spi->mode = mode & (SPI_CPOL | SPI_CPHA);
  82. tiny_spi->flg = mode & SPI_CS_HIGH ? 1 : 0;
  83. spi_set_speed(&tiny_spi->slave, hz);
  84. debug("%s: bus:%i cs:%i base:%lx\n", __func__,
  85. bus, cs, tiny_spi->host->base);
  86. return &tiny_spi->slave;
  87. }
  88. void spi_free_slave(struct spi_slave *slave)
  89. {
  90. struct tiny_spi_slave *tiny_spi = to_tiny_spi_slave(slave);
  91. gpio_free(slave->cs);
  92. free(tiny_spi);
  93. }
  94. int spi_claim_bus(struct spi_slave *slave)
  95. {
  96. struct tiny_spi_slave *tiny_spi = to_tiny_spi_slave(slave);
  97. struct tiny_spi_regs *regs = (void *)tiny_spi->host->base;
  98. debug("%s: bus:%i cs:%i\n", __func__, slave->bus, slave->cs);
  99. gpio_direction_output(slave->cs, !tiny_spi->flg);
  100. writel(tiny_spi->mode, &regs->control);
  101. writel(tiny_spi->baud, &regs->baud);
  102. return 0;
  103. }
  104. void spi_release_bus(struct spi_slave *slave)
  105. {
  106. debug("%s: bus:%i cs:%i\n", __func__, slave->bus, slave->cs);
  107. }
  108. #ifndef CONFIG_TINY_SPI_IDLE_VAL
  109. # define CONFIG_TINY_SPI_IDLE_VAL 0xff
  110. #endif
  111. int spi_xfer(struct spi_slave *slave, unsigned int bitlen, const void *dout,
  112. void *din, unsigned long flags)
  113. {
  114. struct tiny_spi_slave *tiny_spi = to_tiny_spi_slave(slave);
  115. struct tiny_spi_regs *regs = (void *)tiny_spi->host->base;
  116. const u8 *txp = dout;
  117. u8 *rxp = din;
  118. uint bytes = bitlen / 8;
  119. uint i;
  120. debug("%s: bus:%i cs:%i bitlen:%i bytes:%i flags:%lx\n", __func__,
  121. slave->bus, slave->cs, bitlen, bytes, flags);
  122. if (bitlen == 0)
  123. goto done;
  124. /* assume to do 8 bits transfers */
  125. if (bitlen % 8) {
  126. flags |= SPI_XFER_END;
  127. goto done;
  128. }
  129. if (flags & SPI_XFER_BEGIN)
  130. spi_cs_activate(slave);
  131. /* we need to tighten the transfer loop */
  132. if (txp && rxp) {
  133. writeb(*txp++, &regs->txdata);
  134. if (bytes > 1) {
  135. writeb(*txp++, &regs->txdata);
  136. for (i = 2; i < bytes; i++) {
  137. u8 rx, tx = *txp++;
  138. while (!(readb(&regs->status) &
  139. TINY_SPI_STATUS_TXR))
  140. ;
  141. rx = readb(&regs->txdata);
  142. writeb(tx, &regs->txdata);
  143. *rxp++ = rx;
  144. }
  145. while (!(readb(&regs->status) &
  146. TINY_SPI_STATUS_TXR))
  147. ;
  148. *rxp++ = readb(&regs->txdata);
  149. }
  150. while (!(readb(&regs->status) &
  151. TINY_SPI_STATUS_TXE))
  152. ;
  153. *rxp++ = readb(&regs->rxdata);
  154. } else if (rxp) {
  155. writeb(CONFIG_TINY_SPI_IDLE_VAL, &regs->txdata);
  156. if (bytes > 1) {
  157. writeb(CONFIG_TINY_SPI_IDLE_VAL,
  158. &regs->txdata);
  159. for (i = 2; i < bytes; i++) {
  160. u8 rx;
  161. while (!(readb(&regs->status) &
  162. TINY_SPI_STATUS_TXR))
  163. ;
  164. rx = readb(&regs->txdata);
  165. writeb(CONFIG_TINY_SPI_IDLE_VAL,
  166. &regs->txdata);
  167. *rxp++ = rx;
  168. }
  169. while (!(readb(&regs->status) &
  170. TINY_SPI_STATUS_TXR))
  171. ;
  172. *rxp++ = readb(&regs->txdata);
  173. }
  174. while (!(readb(&regs->status) &
  175. TINY_SPI_STATUS_TXE))
  176. ;
  177. *rxp++ = readb(&regs->rxdata);
  178. } else if (txp) {
  179. writeb(*txp++, &regs->txdata);
  180. if (bytes > 1) {
  181. writeb(*txp++, &regs->txdata);
  182. for (i = 2; i < bytes; i++) {
  183. u8 tx = *txp++;
  184. while (!(readb(&regs->status) &
  185. TINY_SPI_STATUS_TXR))
  186. ;
  187. writeb(tx, &regs->txdata);
  188. }
  189. }
  190. while (!(readb(&regs->status) &
  191. TINY_SPI_STATUS_TXE))
  192. ;
  193. } else {
  194. writeb(CONFIG_TINY_SPI_IDLE_VAL, &regs->txdata);
  195. if (bytes > 1) {
  196. writeb(CONFIG_TINY_SPI_IDLE_VAL,
  197. &regs->txdata);
  198. for (i = 2; i < bytes; i++) {
  199. while (!(readb(&regs->status) &
  200. TINY_SPI_STATUS_TXR))
  201. ;
  202. writeb(CONFIG_TINY_SPI_IDLE_VAL,
  203. &regs->txdata);
  204. }
  205. }
  206. while (!(readb(&regs->status) &
  207. TINY_SPI_STATUS_TXE))
  208. ;
  209. }
  210. done:
  211. if (flags & SPI_XFER_END)
  212. spi_cs_deactivate(slave);
  213. return 0;
  214. }