bfin_spi.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. /*
  2. * Driver for Blackfin On-Chip SPI device
  3. *
  4. * Copyright (c) 2005-2008 Analog Devices Inc.
  5. *
  6. * Licensed under the GPL-2 or later.
  7. */
  8. /*#define DEBUG*/
  9. #include <common.h>
  10. #include <malloc.h>
  11. #include <spi.h>
  12. #include <asm/blackfin.h>
  13. #include <asm/gpio.h>
  14. #include <asm/portmux.h>
  15. #include <asm/mach-common/bits/spi.h>
  16. struct bfin_spi_slave {
  17. struct spi_slave slave;
  18. void *mmr_base;
  19. u16 ctl, baud, flg;
  20. };
  21. #define MAKE_SPI_FUNC(mmr, off) \
  22. static inline void write_##mmr(struct bfin_spi_slave *bss, u16 val) { bfin_write16(bss->mmr_base + off, val); } \
  23. static inline u16 read_##mmr(struct bfin_spi_slave *bss) { return bfin_read16(bss->mmr_base + off); }
  24. MAKE_SPI_FUNC(SPI_CTL, 0x00)
  25. MAKE_SPI_FUNC(SPI_FLG, 0x04)
  26. MAKE_SPI_FUNC(SPI_STAT, 0x08)
  27. MAKE_SPI_FUNC(SPI_TDBR, 0x0c)
  28. MAKE_SPI_FUNC(SPI_RDBR, 0x10)
  29. MAKE_SPI_FUNC(SPI_BAUD, 0x14)
  30. #define to_bfin_spi_slave(s) container_of(s, struct bfin_spi_slave, slave)
  31. #define MAX_CTRL_CS 7
  32. #define gpio_cs(cs) ((cs) - MAX_CTRL_CS)
  33. #ifdef CONFIG_BFIN_SPI_GPIO_CS
  34. # define is_gpio_cs(cs) ((cs) > MAX_CTRL_CS)
  35. #else
  36. # define is_gpio_cs(cs) 0
  37. #endif
  38. int spi_cs_is_valid(unsigned int bus, unsigned int cs)
  39. {
  40. if (is_gpio_cs(cs))
  41. return gpio_is_valid(gpio_cs(cs));
  42. else
  43. return (cs >= 1 && cs <= MAX_CTRL_CS);
  44. }
  45. void spi_cs_activate(struct spi_slave *slave)
  46. {
  47. struct bfin_spi_slave *bss = to_bfin_spi_slave(slave);
  48. if (is_gpio_cs(slave->cs)) {
  49. unsigned int cs = gpio_cs(slave->cs);
  50. gpio_set_value(cs, bss->flg);
  51. debug("%s: SPI_CS_GPIO:%x\n", __func__, gpio_get_value(cs));
  52. } else {
  53. write_SPI_FLG(bss,
  54. (read_SPI_FLG(bss) &
  55. ~((!bss->flg << 8) << slave->cs)) |
  56. (1 << slave->cs));
  57. debug("%s: SPI_FLG:%x\n", __func__, read_SPI_FLG(bss));
  58. }
  59. SSYNC();
  60. }
  61. void spi_cs_deactivate(struct spi_slave *slave)
  62. {
  63. struct bfin_spi_slave *bss = to_bfin_spi_slave(slave);
  64. if (is_gpio_cs(slave->cs)) {
  65. unsigned int cs = gpio_cs(slave->cs);
  66. gpio_set_value(cs, !bss->flg);
  67. debug("%s: SPI_CS_GPIO:%x\n", __func__, gpio_get_value(cs));
  68. } else {
  69. u16 flg;
  70. /* make sure we force the cs to deassert rather than let the
  71. * pin float back up. otherwise, exact timings may not be
  72. * met some of the time leading to random behavior (ugh).
  73. */
  74. flg = read_SPI_FLG(bss) | ((!bss->flg << 8) << slave->cs);
  75. write_SPI_FLG(bss, flg);
  76. SSYNC();
  77. debug("%s: SPI_FLG:%x\n", __func__, read_SPI_FLG(bss));
  78. flg &= ~(1 << slave->cs);
  79. write_SPI_FLG(bss, flg);
  80. debug("%s: SPI_FLG:%x\n", __func__, read_SPI_FLG(bss));
  81. }
  82. SSYNC();
  83. }
  84. void spi_init()
  85. {
  86. }
  87. #ifdef SPI_CTL
  88. # define SPI0_CTL SPI_CTL
  89. #endif
  90. #define SPI_PINS(n) \
  91. [n] = { 0, P_SPI##n##_SCK, P_SPI##n##_MISO, P_SPI##n##_MOSI, 0 }
  92. static unsigned short pins[][5] = {
  93. #ifdef SPI0_CTL
  94. SPI_PINS(0),
  95. #endif
  96. #ifdef SPI1_CTL
  97. SPI_PINS(1),
  98. #endif
  99. #ifdef SPI2_CTL
  100. SPI_PINS(2),
  101. #endif
  102. };
  103. #define SPI_CS_PINS(n) \
  104. [n] = { \
  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_CTL
  111. SPI_CS_PINS(0),
  112. #endif
  113. #ifdef SPI1_CTL
  114. SPI_CS_PINS(1),
  115. #endif
  116. #ifdef SPI2_CTL
  117. SPI_CS_PINS(2),
  118. #endif
  119. };
  120. struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
  121. unsigned int max_hz, unsigned int mode)
  122. {
  123. struct bfin_spi_slave *bss;
  124. ulong sclk;
  125. u32 mmr_base;
  126. u32 baud;
  127. if (!spi_cs_is_valid(bus, cs))
  128. return NULL;
  129. if (bus >= ARRAY_SIZE(pins) || pins[bus] == NULL) {
  130. debug("%s: invalid bus %u\n", __func__, bus);
  131. return NULL;
  132. }
  133. switch (bus) {
  134. #ifdef SPI0_CTL
  135. case 0: mmr_base = SPI0_CTL; break;
  136. #endif
  137. #ifdef SPI1_CTL
  138. case 1: mmr_base = SPI1_CTL; break;
  139. #endif
  140. #ifdef SPI2_CTL
  141. case 2: mmr_base = SPI2_CTL; break;
  142. #endif
  143. default: return NULL;
  144. }
  145. sclk = get_sclk();
  146. baud = sclk / (2 * max_hz);
  147. /* baud should be rounded up */
  148. if (sclk % (2 * max_hz))
  149. baud += 1;
  150. if (baud < 2)
  151. baud = 2;
  152. else if (baud > (u16)-1)
  153. baud = -1;
  154. bss = malloc(sizeof(*bss));
  155. if (!bss)
  156. return NULL;
  157. bss->slave.bus = bus;
  158. bss->slave.cs = cs;
  159. bss->mmr_base = (void *)mmr_base;
  160. bss->ctl = SPE | MSTR | TDBR_CORE;
  161. if (mode & SPI_CPHA) bss->ctl |= CPHA;
  162. if (mode & SPI_CPOL) bss->ctl |= CPOL;
  163. if (mode & SPI_LSB_FIRST) bss->ctl |= LSBF;
  164. bss->baud = baud;
  165. bss->flg = mode & SPI_CS_HIGH ? 1 : 0;
  166. debug("%s: bus:%i cs:%i mmr:%x ctl:%x baud:%i flg:%i\n", __func__,
  167. bus, cs, mmr_base, bss->ctl, baud, bss->flg);
  168. return &bss->slave;
  169. }
  170. void spi_free_slave(struct spi_slave *slave)
  171. {
  172. struct bfin_spi_slave *bss = to_bfin_spi_slave(slave);
  173. free(bss);
  174. }
  175. int spi_claim_bus(struct spi_slave *slave)
  176. {
  177. struct bfin_spi_slave *bss = to_bfin_spi_slave(slave);
  178. debug("%s: bus:%i cs:%i\n", __func__, slave->bus, slave->cs);
  179. if (is_gpio_cs(slave->cs)) {
  180. unsigned int cs = gpio_cs(slave->cs);
  181. gpio_request(cs, "bfin-spi");
  182. gpio_direction_output(cs, !bss->flg);
  183. pins[slave->bus][0] = P_DONTCARE;
  184. } else
  185. pins[slave->bus][0] = cs_pins[slave->bus][slave->cs - 1];
  186. peripheral_request_list(pins[slave->bus], "bfin-spi");
  187. write_SPI_CTL(bss, bss->ctl);
  188. write_SPI_BAUD(bss, bss->baud);
  189. SSYNC();
  190. return 0;
  191. }
  192. void spi_release_bus(struct spi_slave *slave)
  193. {
  194. struct bfin_spi_slave *bss = to_bfin_spi_slave(slave);
  195. debug("%s: bus:%i cs:%i\n", __func__, slave->bus, slave->cs);
  196. peripheral_free_list(pins[slave->bus]);
  197. if (is_gpio_cs(slave->cs))
  198. gpio_free(gpio_cs(slave->cs));
  199. write_SPI_CTL(bss, 0);
  200. SSYNC();
  201. }
  202. #ifndef CONFIG_BFIN_SPI_IDLE_VAL
  203. # define CONFIG_BFIN_SPI_IDLE_VAL 0xff
  204. #endif
  205. int spi_xfer(struct spi_slave *slave, unsigned int bitlen, const void *dout,
  206. void *din, unsigned long flags)
  207. {
  208. struct bfin_spi_slave *bss = to_bfin_spi_slave(slave);
  209. const u8 *tx = dout;
  210. u8 *rx = din;
  211. uint bytes = bitlen / 8;
  212. int ret = 0;
  213. debug("%s: bus:%i cs:%i bitlen:%i bytes:%i flags:%lx\n", __func__,
  214. slave->bus, slave->cs, bitlen, bytes, flags);
  215. if (bitlen == 0)
  216. goto done;
  217. /* we can only do 8 bit transfers */
  218. if (bitlen % 8) {
  219. flags |= SPI_XFER_END;
  220. goto done;
  221. }
  222. if (flags & SPI_XFER_BEGIN)
  223. spi_cs_activate(slave);
  224. /* todo: take advantage of hardware fifos and setup RX dma */
  225. while (bytes--) {
  226. u8 value = (tx ? *tx++ : CONFIG_BFIN_SPI_IDLE_VAL);
  227. debug("%s: tx:%x ", __func__, value);
  228. write_SPI_TDBR(bss, value);
  229. SSYNC();
  230. while ((read_SPI_STAT(bss) & TXS))
  231. if (ctrlc()) {
  232. ret = -1;
  233. goto done;
  234. }
  235. while (!(read_SPI_STAT(bss) & SPIF))
  236. if (ctrlc()) {
  237. ret = -1;
  238. goto done;
  239. }
  240. while (!(read_SPI_STAT(bss) & RXS))
  241. if (ctrlc()) {
  242. ret = -1;
  243. goto done;
  244. }
  245. value = read_SPI_RDBR(bss);
  246. if (rx)
  247. *rx++ = value;
  248. debug("rx:%x\n", value);
  249. }
  250. done:
  251. if (flags & SPI_XFER_END)
  252. spi_cs_deactivate(slave);
  253. return ret;
  254. }