bfin_spi6xx.c 6.9 KB

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