bfin_spi.c 5.9 KB

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