bfin_spi6xx.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. /*
  2. * Analog Devices SPI3 controller driver
  3. *
  4. * Copyright (c) 2011 Analog Devices Inc.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18. */
  19. #include <common.h>
  20. #include <malloc.h>
  21. #include <spi.h>
  22. #include <asm/blackfin.h>
  23. #include <asm/clock.h>
  24. #include <asm/gpio.h>
  25. #include <asm/portmux.h>
  26. #include <asm/mach-common/bits/spi6xx.h>
  27. struct bfin_spi_slave {
  28. struct spi_slave slave;
  29. u32 control, clock;
  30. struct bfin_spi_regs *regs;
  31. int cs_pol;
  32. };
  33. #define to_bfin_spi_slave(s) container_of(s, struct bfin_spi_slave, slave)
  34. #define gpio_cs(cs) ((cs) - MAX_CTRL_CS)
  35. #ifdef CONFIG_BFIN_SPI_GPIO_CS
  36. # define is_gpio_cs(cs) ((cs) > MAX_CTRL_CS)
  37. #else
  38. # define is_gpio_cs(cs) 0
  39. #endif
  40. int spi_cs_is_valid(unsigned int bus, unsigned int cs)
  41. {
  42. if (is_gpio_cs(cs))
  43. return gpio_is_valid(gpio_cs(cs));
  44. else
  45. return (cs >= 1 && cs <= MAX_CTRL_CS);
  46. }
  47. void spi_cs_activate(struct spi_slave *slave)
  48. {
  49. struct bfin_spi_slave *bss = to_bfin_spi_slave(slave);
  50. if (is_gpio_cs(slave->cs)) {
  51. unsigned int cs = gpio_cs(slave->cs);
  52. gpio_set_value(cs, bss->cs_pol);
  53. } else {
  54. u32 ssel;
  55. ssel = bfin_read32(&bss->regs->ssel);
  56. ssel |= 1 << slave->cs;
  57. if (bss->cs_pol)
  58. ssel |= (1 << 8) << slave->cs;
  59. else
  60. ssel &= ~((1 << 8) << slave->cs);
  61. bfin_write32(&bss->regs->ssel, ssel);
  62. }
  63. SSYNC();
  64. }
  65. void spi_cs_deactivate(struct spi_slave *slave)
  66. {
  67. struct bfin_spi_slave *bss = to_bfin_spi_slave(slave);
  68. if (is_gpio_cs(slave->cs)) {
  69. unsigned int cs = gpio_cs(slave->cs);
  70. gpio_set_value(cs, !bss->cs_pol);
  71. } else {
  72. u32 ssel;
  73. ssel = bfin_read32(&bss->regs->ssel);
  74. if (bss->cs_pol)
  75. ssel &= ~((1 << 8) << slave->cs);
  76. else
  77. ssel |= (1 << 8) << slave->cs;
  78. /* deassert cs */
  79. bfin_write32(&bss->regs->ssel, ssel);
  80. SSYNC();
  81. /* disable cs */
  82. ssel &= ~(1 << slave->cs);
  83. bfin_write32(&bss->regs->ssel, ssel);
  84. }
  85. SSYNC();
  86. }
  87. void spi_init()
  88. {
  89. }
  90. #define SPI_PINS(n) \
  91. { 0, P_SPI##n##_SCK, P_SPI##n##_MISO, P_SPI##n##_MOSI, 0 }
  92. static unsigned short pins[][5] = {
  93. #ifdef SPI0_REGBASE
  94. [0] = SPI_PINS(0),
  95. #endif
  96. #ifdef SPI1_REGBASE
  97. [1] = SPI_PINS(1),
  98. #endif
  99. #ifdef SPI2_REGBASE
  100. [2] = SPI_PINS(2),
  101. #endif
  102. };
  103. #define SPI_CS_PINS(n) \
  104. { \
  105. P_SPI##n##_SSEL1, P_SPI##n##_SSEL2, P_SPI##n##_SSEL3, \
  106. P_SPI##n##_SSEL4, P_SPI##n##_SSEL5, P_SPI##n##_SSEL6, \
  107. P_SPI##n##_SSEL7, \
  108. }
  109. static const unsigned short cs_pins[][7] = {
  110. #ifdef SPI0_REGBASE
  111. [0] = SPI_CS_PINS(0),
  112. #endif
  113. #ifdef SPI1_REGBASE
  114. [1] = SPI_CS_PINS(1),
  115. #endif
  116. #ifdef SPI2_REGBASE
  117. [2] = SPI_CS_PINS(2),
  118. #endif
  119. };
  120. void spi_set_speed(struct spi_slave *slave, uint hz)
  121. {
  122. struct bfin_spi_slave *bss = to_bfin_spi_slave(slave);
  123. ulong clk;
  124. u32 clock;
  125. clk = get_spi_clk();
  126. clock = clk / hz;
  127. if (clock)
  128. clock--;
  129. bss->clock = clock;
  130. }
  131. struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
  132. unsigned int max_hz, unsigned int mode)
  133. {
  134. struct bfin_spi_slave *bss;
  135. u32 reg_base;
  136. if (!spi_cs_is_valid(bus, cs))
  137. return NULL;
  138. switch (bus) {
  139. #ifdef SPI0_REGBASE
  140. case 0:
  141. reg_base = SPI0_REGBASE;
  142. break;
  143. #endif
  144. #ifdef SPI1_REGBASE
  145. case 1:
  146. reg_base = SPI1_REGBASE;
  147. break;
  148. #endif
  149. #ifdef SPI2_REGBASE
  150. case 2:
  151. reg_base = SPI2_REGBASE;
  152. break;
  153. #endif
  154. default:
  155. debug("%s: invalid bus %u\n", __func__, bus);
  156. return NULL;
  157. }
  158. bss = spi_alloc_slave(struct bfin_spi_slave, bus, cs);
  159. if (!bss)
  160. return NULL;
  161. bss->regs = (struct bfin_spi_regs *)reg_base;
  162. bss->control = SPI_CTL_EN | SPI_CTL_MSTR;
  163. if (mode & SPI_CPHA)
  164. bss->control |= SPI_CTL_CPHA;
  165. if (mode & SPI_CPOL)
  166. bss->control |= SPI_CTL_CPOL;
  167. if (mode & SPI_LSB_FIRST)
  168. bss->control |= SPI_CTL_LSBF;
  169. bss->control &= ~SPI_CTL_ASSEL;
  170. bss->cs_pol = mode & SPI_CS_HIGH ? 1 : 0;
  171. spi_set_speed(&bss->slave, max_hz);
  172. return &bss->slave;
  173. }
  174. void spi_free_slave(struct spi_slave *slave)
  175. {
  176. struct bfin_spi_slave *bss = to_bfin_spi_slave(slave);
  177. free(bss);
  178. }
  179. int spi_claim_bus(struct spi_slave *slave)
  180. {
  181. struct bfin_spi_slave *bss = to_bfin_spi_slave(slave);
  182. debug("%s: bus:%i cs:%i\n", __func__, slave->bus, slave->cs);
  183. if (is_gpio_cs(slave->cs)) {
  184. unsigned int cs = gpio_cs(slave->cs);
  185. gpio_request(cs, "bfin-spi");
  186. gpio_direction_output(cs, !bss->cs_pol);
  187. pins[slave->bus][0] = P_DONTCARE;
  188. } else
  189. pins[slave->bus][0] = cs_pins[slave->bus][slave->cs - 1];
  190. peripheral_request_list(pins[slave->bus], "bfin-spi");
  191. bfin_write32(&bss->regs->control, bss->control);
  192. bfin_write32(&bss->regs->clock, bss->clock);
  193. bfin_write32(&bss->regs->delay, 0x0);
  194. bfin_write32(&bss->regs->rx_control, SPI_RXCTL_REN);
  195. bfin_write32(&bss->regs->tx_control, SPI_TXCTL_TEN | SPI_TXCTL_TTI);
  196. SSYNC();
  197. return 0;
  198. }
  199. void spi_release_bus(struct spi_slave *slave)
  200. {
  201. struct bfin_spi_slave *bss = to_bfin_spi_slave(slave);
  202. debug("%s: bus:%i cs:%i\n", __func__, slave->bus, slave->cs);
  203. peripheral_free_list(pins[slave->bus]);
  204. if (is_gpio_cs(slave->cs))
  205. gpio_free(gpio_cs(slave->cs));
  206. bfin_write32(&bss->regs->rx_control, 0x0);
  207. bfin_write32(&bss->regs->tx_control, 0x0);
  208. bfin_write32(&bss->regs->control, 0x0);
  209. SSYNC();
  210. }
  211. #ifndef CONFIG_BFIN_SPI_IDLE_VAL
  212. # define CONFIG_BFIN_SPI_IDLE_VAL 0xff
  213. #endif
  214. static int spi_pio_xfer(struct bfin_spi_slave *bss, const u8 *tx, u8 *rx,
  215. uint bytes)
  216. {
  217. /* discard invalid rx data and empty rfifo */
  218. while (!(bfin_read32(&bss->regs->status) & SPI_STAT_RFE))
  219. bfin_read32(&bss->regs->rfifo);
  220. while (bytes--) {
  221. u8 value = (tx ? *tx++ : CONFIG_BFIN_SPI_IDLE_VAL);
  222. debug("%s: tx:%x ", __func__, value);
  223. bfin_write32(&bss->regs->tfifo, value);
  224. SSYNC();
  225. while (bfin_read32(&bss->regs->status) & SPI_STAT_RFE)
  226. if (ctrlc())
  227. return -1;
  228. value = bfin_read32(&bss->regs->rfifo);
  229. if (rx)
  230. *rx++ = value;
  231. debug("rx:%x\n", value);
  232. }
  233. return 0;
  234. }
  235. int spi_xfer(struct spi_slave *slave, unsigned int bitlen, const void *dout,
  236. void *din, unsigned long flags)
  237. {
  238. struct bfin_spi_slave *bss = to_bfin_spi_slave(slave);
  239. const u8 *tx = dout;
  240. u8 *rx = din;
  241. uint bytes = bitlen / 8;
  242. int ret = 0;
  243. debug("%s: bus:%i cs:%i bitlen:%i bytes:%i flags:%lx\n", __func__,
  244. slave->bus, slave->cs, bitlen, bytes, flags);
  245. if (bitlen == 0)
  246. goto done;
  247. /* we can only do 8 bit transfers */
  248. if (bitlen % 8) {
  249. flags |= SPI_XFER_END;
  250. goto done;
  251. }
  252. if (flags & SPI_XFER_BEGIN)
  253. spi_cs_activate(slave);
  254. ret = spi_pio_xfer(bss, tx, rx, bytes);
  255. done:
  256. if (flags & SPI_XFER_END)
  257. spi_cs_deactivate(slave);
  258. return ret;
  259. }