bfin_spi6xx.c 6.8 KB

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