bfin_spi.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. /*
  2. * Driver for Blackfin On-Chip SPI device
  3. *
  4. * Copyright (c) 2005-2010 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 gpio_cs(cs) ((cs) - MAX_CTRL_CS)
  32. #ifdef CONFIG_BFIN_SPI_GPIO_CS
  33. # define is_gpio_cs(cs) ((cs) > MAX_CTRL_CS)
  34. #else
  35. # define is_gpio_cs(cs) 0
  36. #endif
  37. int spi_cs_is_valid(unsigned int bus, unsigned int cs)
  38. {
  39. if (is_gpio_cs(cs))
  40. return gpio_is_valid(gpio_cs(cs));
  41. else
  42. return (cs >= 1 && cs <= MAX_CTRL_CS);
  43. }
  44. void spi_cs_activate(struct spi_slave *slave)
  45. {
  46. struct bfin_spi_slave *bss = to_bfin_spi_slave(slave);
  47. if (is_gpio_cs(slave->cs)) {
  48. unsigned int cs = gpio_cs(slave->cs);
  49. gpio_set_value(cs, bss->flg);
  50. debug("%s: SPI_CS_GPIO:%x\n", __func__, gpio_get_value(cs));
  51. } else {
  52. write_SPI_FLG(bss,
  53. (read_SPI_FLG(bss) &
  54. ~((!bss->flg << 8) << slave->cs)) |
  55. (1 << slave->cs));
  56. debug("%s: SPI_FLG:%x\n", __func__, read_SPI_FLG(bss));
  57. }
  58. SSYNC();
  59. }
  60. void spi_cs_deactivate(struct spi_slave *slave)
  61. {
  62. struct bfin_spi_slave *bss = to_bfin_spi_slave(slave);
  63. if (is_gpio_cs(slave->cs)) {
  64. unsigned int cs = gpio_cs(slave->cs);
  65. gpio_set_value(cs, !bss->flg);
  66. debug("%s: SPI_CS_GPIO:%x\n", __func__, gpio_get_value(cs));
  67. } else {
  68. u16 flg;
  69. /* make sure we force the cs to deassert rather than let the
  70. * pin float back up. otherwise, exact timings may not be
  71. * met some of the time leading to random behavior (ugh).
  72. */
  73. flg = read_SPI_FLG(bss) | ((!bss->flg << 8) << slave->cs);
  74. write_SPI_FLG(bss, flg);
  75. SSYNC();
  76. debug("%s: SPI_FLG:%x\n", __func__, read_SPI_FLG(bss));
  77. flg &= ~(1 << slave->cs);
  78. write_SPI_FLG(bss, flg);
  79. debug("%s: SPI_FLG:%x\n", __func__, read_SPI_FLG(bss));
  80. }
  81. SSYNC();
  82. }
  83. void spi_init()
  84. {
  85. }
  86. #ifdef SPI_CTL
  87. # define SPI0_CTL SPI_CTL
  88. #endif
  89. #define SPI_PINS(n) \
  90. [n] = { 0, P_SPI##n##_SCK, P_SPI##n##_MISO, P_SPI##n##_MOSI, 0 }
  91. static unsigned short pins[][5] = {
  92. #ifdef SPI0_CTL
  93. SPI_PINS(0),
  94. #endif
  95. #ifdef SPI1_CTL
  96. SPI_PINS(1),
  97. #endif
  98. #ifdef SPI2_CTL
  99. SPI_PINS(2),
  100. #endif
  101. };
  102. #define SPI_CS_PINS(n) \
  103. [n] = { \
  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_CTL
  110. SPI_CS_PINS(0),
  111. #endif
  112. #ifdef SPI1_CTL
  113. SPI_CS_PINS(1),
  114. #endif
  115. #ifdef SPI2_CTL
  116. 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 baud;
  124. sclk = get_sclk();
  125. baud = sclk / (2 * hz);
  126. /* baud should be rounded up */
  127. if (sclk % (2 * hz))
  128. baud += 1;
  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. if (bus >= ARRAY_SIZE(pins) || pins[bus] == NULL) {
  143. debug("%s: invalid bus %u\n", __func__, bus);
  144. return NULL;
  145. }
  146. switch (bus) {
  147. #ifdef SPI0_CTL
  148. case 0: mmr_base = SPI0_CTL; break;
  149. #endif
  150. #ifdef SPI1_CTL
  151. case 1: mmr_base = SPI1_CTL; break;
  152. #endif
  153. #ifdef SPI2_CTL
  154. case 2: mmr_base = SPI2_CTL; break;
  155. #endif
  156. default: return NULL;
  157. }
  158. bss = spi_alloc_slave(struct bfin_spi_slave, bus, cs);
  159. if (!bss)
  160. return NULL;
  161. bss->mmr_base = (void *)mmr_base;
  162. bss->ctl = SPE | MSTR | TDBR_CORE;
  163. if (mode & SPI_CPHA) bss->ctl |= CPHA;
  164. if (mode & SPI_CPOL) bss->ctl |= CPOL;
  165. if (mode & SPI_LSB_FIRST) bss->ctl |= LSBF;
  166. bss->flg = mode & SPI_CS_HIGH ? 1 : 0;
  167. spi_set_speed(&bss->slave, max_hz);
  168. debug("%s: bus:%i cs:%i mmr:%x ctl:%x baud:%i flg:%i\n", __func__,
  169. bus, cs, mmr_base, bss->ctl, bss->baud, bss->flg);
  170. return &bss->slave;
  171. }
  172. void spi_free_slave(struct spi_slave *slave)
  173. {
  174. struct bfin_spi_slave *bss = to_bfin_spi_slave(slave);
  175. free(bss);
  176. }
  177. int spi_claim_bus(struct spi_slave *slave)
  178. {
  179. struct bfin_spi_slave *bss = to_bfin_spi_slave(slave);
  180. debug("%s: bus:%i cs:%i\n", __func__, slave->bus, slave->cs);
  181. if (is_gpio_cs(slave->cs)) {
  182. unsigned int cs = gpio_cs(slave->cs);
  183. gpio_request(cs, "bfin-spi");
  184. gpio_direction_output(cs, !bss->flg);
  185. pins[slave->bus][0] = P_DONTCARE;
  186. } else
  187. pins[slave->bus][0] = cs_pins[slave->bus][slave->cs - 1];
  188. peripheral_request_list(pins[slave->bus], "bfin-spi");
  189. write_SPI_CTL(bss, bss->ctl);
  190. write_SPI_BAUD(bss, bss->baud);
  191. SSYNC();
  192. return 0;
  193. }
  194. void spi_release_bus(struct spi_slave *slave)
  195. {
  196. struct bfin_spi_slave *bss = to_bfin_spi_slave(slave);
  197. debug("%s: bus:%i cs:%i\n", __func__, slave->bus, slave->cs);
  198. peripheral_free_list(pins[slave->bus]);
  199. if (is_gpio_cs(slave->cs))
  200. gpio_free(gpio_cs(slave->cs));
  201. write_SPI_CTL(bss, 0);
  202. SSYNC();
  203. }
  204. #ifndef CONFIG_BFIN_SPI_IDLE_VAL
  205. # define CONFIG_BFIN_SPI_IDLE_VAL 0xff
  206. #endif
  207. static int spi_pio_xfer(struct bfin_spi_slave *bss, const u8 *tx, u8 *rx,
  208. uint bytes)
  209. {
  210. /* discard invalid data and clear RXS */
  211. read_SPI_RDBR(bss);
  212. /* todo: take advantage of hardware fifos */
  213. while (bytes--) {
  214. u8 value = (tx ? *tx++ : CONFIG_BFIN_SPI_IDLE_VAL);
  215. debug("%s: tx:%x ", __func__, value);
  216. write_SPI_TDBR(bss, value);
  217. SSYNC();
  218. while ((read_SPI_STAT(bss) & TXS))
  219. if (ctrlc())
  220. return -1;
  221. while (!(read_SPI_STAT(bss) & SPIF))
  222. if (ctrlc())
  223. return -1;
  224. while (!(read_SPI_STAT(bss) & RXS))
  225. if (ctrlc())
  226. return -1;
  227. value = read_SPI_RDBR(bss);
  228. if (rx)
  229. *rx++ = value;
  230. debug("rx:%x\n", value);
  231. }
  232. return 0;
  233. }
  234. int spi_xfer(struct spi_slave *slave, unsigned int bitlen, const void *dout,
  235. void *din, unsigned long flags)
  236. {
  237. struct bfin_spi_slave *bss = to_bfin_spi_slave(slave);
  238. const u8 *tx = dout;
  239. u8 *rx = din;
  240. uint bytes = bitlen / 8;
  241. int ret = 0;
  242. debug("%s: bus:%i cs:%i bitlen:%i bytes:%i flags:%lx\n", __func__,
  243. slave->bus, slave->cs, bitlen, bytes, flags);
  244. if (bitlen == 0)
  245. goto done;
  246. /* we can only do 8 bit transfers */
  247. if (bitlen % 8) {
  248. flags |= SPI_XFER_END;
  249. goto done;
  250. }
  251. if (flags & SPI_XFER_BEGIN)
  252. spi_cs_activate(slave);
  253. ret = spi_pio_xfer(bss, tx, rx, bytes);
  254. done:
  255. if (flags & SPI_XFER_END)
  256. spi_cs_deactivate(slave);
  257. return ret;
  258. }