bfin_spi.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. /*
  2. * Driver for Blackfin On-Chip SPI device
  3. *
  4. * Copyright (c) 2005-2010 Analog Devices Inc.
  5. *
  6. * SPDX-License-Identifier: GPL-2.0+
  7. */
  8. /*#define DEBUG*/
  9. #include <common.h>
  10. #include <console.h>
  11. #include <malloc.h>
  12. #include <spi.h>
  13. #include <asm/blackfin.h>
  14. #include <asm/clock.h>
  15. #include <asm/gpio.h>
  16. #include <asm/portmux.h>
  17. #include <asm/mach-common/bits/spi.h>
  18. struct bfin_spi_slave {
  19. struct spi_slave slave;
  20. void *mmr_base;
  21. u16 ctl, baud, flg;
  22. };
  23. #define MAKE_SPI_FUNC(mmr, off) \
  24. static inline void write_##mmr(struct bfin_spi_slave *bss, u16 val) { bfin_write16(bss->mmr_base + off, val); } \
  25. static inline u16 read_##mmr(struct bfin_spi_slave *bss) { return bfin_read16(bss->mmr_base + off); }
  26. MAKE_SPI_FUNC(SPI_CTL, 0x00)
  27. MAKE_SPI_FUNC(SPI_FLG, 0x04)
  28. MAKE_SPI_FUNC(SPI_STAT, 0x08)
  29. MAKE_SPI_FUNC(SPI_TDBR, 0x0c)
  30. MAKE_SPI_FUNC(SPI_RDBR, 0x10)
  31. MAKE_SPI_FUNC(SPI_BAUD, 0x14)
  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->flg);
  52. debug("%s: SPI_CS_GPIO:%x\n", __func__, gpio_get_value(cs));
  53. } else {
  54. write_SPI_FLG(bss,
  55. (read_SPI_FLG(bss) &
  56. ~((!bss->flg << 8) << slave->cs)) |
  57. (1 << slave->cs));
  58. debug("%s: SPI_FLG:%x\n", __func__, read_SPI_FLG(bss));
  59. }
  60. SSYNC();
  61. }
  62. void spi_cs_deactivate(struct spi_slave *slave)
  63. {
  64. struct bfin_spi_slave *bss = to_bfin_spi_slave(slave);
  65. if (is_gpio_cs(slave->cs)) {
  66. unsigned int cs = gpio_cs(slave->cs);
  67. gpio_set_value(cs, !bss->flg);
  68. debug("%s: SPI_CS_GPIO:%x\n", __func__, gpio_get_value(cs));
  69. } else {
  70. u16 flg;
  71. /* make sure we force the cs to deassert rather than let the
  72. * pin float back up. otherwise, exact timings may not be
  73. * met some of the time leading to random behavior (ugh).
  74. */
  75. flg = read_SPI_FLG(bss) | ((!bss->flg << 8) << slave->cs);
  76. write_SPI_FLG(bss, flg);
  77. SSYNC();
  78. debug("%s: SPI_FLG:%x\n", __func__, read_SPI_FLG(bss));
  79. flg &= ~(1 << slave->cs);
  80. write_SPI_FLG(bss, flg);
  81. debug("%s: SPI_FLG:%x\n", __func__, read_SPI_FLG(bss));
  82. }
  83. SSYNC();
  84. }
  85. void spi_init()
  86. {
  87. }
  88. #ifdef SPI_CTL
  89. # define SPI0_CTL SPI_CTL
  90. #endif
  91. #define SPI_PINS(n) \
  92. [n] = { 0, P_SPI##n##_SCK, P_SPI##n##_MISO, P_SPI##n##_MOSI, 0 }
  93. static unsigned short pins[][5] = {
  94. #ifdef SPI0_CTL
  95. SPI_PINS(0),
  96. #endif
  97. #ifdef SPI1_CTL
  98. SPI_PINS(1),
  99. #endif
  100. #ifdef SPI2_CTL
  101. SPI_PINS(2),
  102. #endif
  103. };
  104. #define SPI_CS_PINS(n) \
  105. [n] = { \
  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_CTL
  112. SPI_CS_PINS(0),
  113. #endif
  114. #ifdef SPI1_CTL
  115. SPI_CS_PINS(1),
  116. #endif
  117. #ifdef SPI2_CTL
  118. 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 baud;
  126. clk = get_spi_clk();
  127. /* baud should be rounded up */
  128. baud = DIV_ROUND_UP(clk, 2 * hz);
  129. if (baud < 2)
  130. baud = 2;
  131. else if (baud > (u16)-1)
  132. baud = -1;
  133. bss->baud = baud;
  134. }
  135. struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
  136. unsigned int max_hz, unsigned int mode)
  137. {
  138. struct bfin_spi_slave *bss;
  139. u32 mmr_base;
  140. if (!spi_cs_is_valid(bus, cs))
  141. return NULL;
  142. switch (bus) {
  143. #ifdef SPI0_CTL
  144. case 0:
  145. mmr_base = SPI0_CTL; break;
  146. #endif
  147. #ifdef SPI1_CTL
  148. case 1:
  149. mmr_base = SPI1_CTL; break;
  150. #endif
  151. #ifdef SPI2_CTL
  152. case 2:
  153. mmr_base = SPI2_CTL; 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->mmr_base = (void *)mmr_base;
  163. bss->ctl = SPE | MSTR | TDBR_CORE;
  164. if (mode & SPI_CPHA) bss->ctl |= CPHA;
  165. if (mode & SPI_CPOL) bss->ctl |= CPOL;
  166. if (mode & SPI_LSB_FIRST) bss->ctl |= LSBF;
  167. bss->flg = mode & SPI_CS_HIGH ? 1 : 0;
  168. spi_set_speed(&bss->slave, max_hz);
  169. debug("%s: bus:%i cs:%i mmr:%x ctl:%x baud:%i flg:%i\n", __func__,
  170. bus, cs, mmr_base, bss->ctl, bss->baud, bss->flg);
  171. return &bss->slave;
  172. }
  173. void spi_free_slave(struct spi_slave *slave)
  174. {
  175. struct bfin_spi_slave *bss = to_bfin_spi_slave(slave);
  176. free(bss);
  177. }
  178. int spi_claim_bus(struct spi_slave *slave)
  179. {
  180. struct bfin_spi_slave *bss = to_bfin_spi_slave(slave);
  181. debug("%s: bus:%i cs:%i\n", __func__, slave->bus, slave->cs);
  182. if (is_gpio_cs(slave->cs)) {
  183. unsigned int cs = gpio_cs(slave->cs);
  184. gpio_request(cs, "bfin-spi");
  185. gpio_direction_output(cs, !bss->flg);
  186. pins[slave->bus][0] = P_DONTCARE;
  187. } else
  188. pins[slave->bus][0] = cs_pins[slave->bus][slave->cs - 1];
  189. peripheral_request_list(pins[slave->bus], "bfin-spi");
  190. write_SPI_CTL(bss, bss->ctl);
  191. write_SPI_BAUD(bss, bss->baud);
  192. SSYNC();
  193. return 0;
  194. }
  195. void spi_release_bus(struct spi_slave *slave)
  196. {
  197. struct bfin_spi_slave *bss = to_bfin_spi_slave(slave);
  198. debug("%s: bus:%i cs:%i\n", __func__, slave->bus, slave->cs);
  199. peripheral_free_list(pins[slave->bus]);
  200. if (is_gpio_cs(slave->cs))
  201. gpio_free(gpio_cs(slave->cs));
  202. write_SPI_CTL(bss, 0);
  203. SSYNC();
  204. }
  205. #ifndef CONFIG_BFIN_SPI_IDLE_VAL
  206. # define CONFIG_BFIN_SPI_IDLE_VAL 0xff
  207. #endif
  208. static int spi_pio_xfer(struct bfin_spi_slave *bss, const u8 *tx, u8 *rx,
  209. uint bytes)
  210. {
  211. /* discard invalid data and clear RXS */
  212. read_SPI_RDBR(bss);
  213. /* todo: take advantage of hardware fifos */
  214. while (bytes--) {
  215. u8 value = (tx ? *tx++ : CONFIG_BFIN_SPI_IDLE_VAL);
  216. debug("%s: tx:%x ", __func__, value);
  217. write_SPI_TDBR(bss, value);
  218. SSYNC();
  219. while ((read_SPI_STAT(bss) & TXS))
  220. if (ctrlc())
  221. return -1;
  222. while (!(read_SPI_STAT(bss) & SPIF))
  223. if (ctrlc())
  224. return -1;
  225. while (!(read_SPI_STAT(bss) & RXS))
  226. if (ctrlc())
  227. return -1;
  228. value = read_SPI_RDBR(bss);
  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. }