bfin_spi.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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/mach-common/bits/spi.h>
  14. struct bfin_spi_slave {
  15. struct spi_slave slave;
  16. void *mmr_base;
  17. u16 ctl, baud, flg;
  18. };
  19. #define MAKE_SPI_FUNC(mmr, off) \
  20. static inline void write_##mmr(struct bfin_spi_slave *bss, u16 val) { bfin_write16(bss->mmr_base + off, val); } \
  21. static inline u16 read_##mmr(struct bfin_spi_slave *bss) { return bfin_read16(bss->mmr_base + off); }
  22. MAKE_SPI_FUNC(SPI_CTL, 0x00)
  23. MAKE_SPI_FUNC(SPI_FLG, 0x04)
  24. MAKE_SPI_FUNC(SPI_STAT, 0x08)
  25. MAKE_SPI_FUNC(SPI_TDBR, 0x0c)
  26. MAKE_SPI_FUNC(SPI_RDBR, 0x10)
  27. MAKE_SPI_FUNC(SPI_BAUD, 0x14)
  28. #define to_bfin_spi_slave(s) container_of(s, struct bfin_spi_slave, slave)
  29. __attribute__((weak))
  30. int spi_cs_is_valid(unsigned int bus, unsigned int cs)
  31. {
  32. #if defined(__ADSPBF538__) || defined(__ADSPBF539__)
  33. /* The SPI1/SPI2 buses are weird ... only 1 CS */
  34. if (bus > 0 && cs != 1)
  35. return 0;
  36. #endif
  37. return (cs >= 1 && cs <= 7);
  38. }
  39. __attribute__((weak))
  40. void spi_cs_activate(struct spi_slave *slave)
  41. {
  42. struct bfin_spi_slave *bss = to_bfin_spi_slave(slave);
  43. write_SPI_FLG(bss,
  44. (read_SPI_FLG(bss) &
  45. ~((!bss->flg << 8) << slave->cs)) |
  46. (1 << slave->cs));
  47. SSYNC();
  48. debug("%s: SPI_FLG:%x\n", __func__, read_SPI_FLG(bss));
  49. }
  50. __attribute__((weak))
  51. void spi_cs_deactivate(struct spi_slave *slave)
  52. {
  53. struct bfin_spi_slave *bss = to_bfin_spi_slave(slave);
  54. u16 flg;
  55. /* make sure we force the cs to deassert rather than let the
  56. * pin float back up. otherwise, exact timings may not be
  57. * met some of the time leading to random behavior (ugh).
  58. */
  59. flg = read_SPI_FLG(bss) | ((!bss->flg << 8) << slave->cs);
  60. write_SPI_FLG(bss, flg);
  61. SSYNC();
  62. debug("%s: SPI_FLG:%x\n", __func__, read_SPI_FLG(bss));
  63. flg &= ~(1 << slave->cs);
  64. write_SPI_FLG(bss, flg);
  65. SSYNC();
  66. debug("%s: SPI_FLG:%x\n", __func__, read_SPI_FLG(bss));
  67. }
  68. void spi_init()
  69. {
  70. }
  71. struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
  72. unsigned int max_hz, unsigned int mode)
  73. {
  74. struct bfin_spi_slave *bss;
  75. ulong sclk;
  76. u32 mmr_base;
  77. u32 baud;
  78. if (!spi_cs_is_valid(bus, cs))
  79. return NULL;
  80. switch (bus) {
  81. #ifdef SPI_CTL
  82. # define SPI0_CTL SPI_CTL
  83. #endif
  84. case 0: mmr_base = SPI0_CTL; break;
  85. #ifdef SPI1_CTL
  86. case 1: mmr_base = SPI1_CTL; break;
  87. #endif
  88. #ifdef SPI2_CTL
  89. case 2: mmr_base = SPI2_CTL; break;
  90. #endif
  91. default: return NULL;
  92. }
  93. sclk = get_sclk();
  94. baud = sclk / (2 * max_hz);
  95. /* baud should be rounded up */
  96. if (sclk % (2 * max_hz))
  97. baud += 1;
  98. if (baud < 2)
  99. baud = 2;
  100. else if (baud > (u16)-1)
  101. baud = -1;
  102. bss = malloc(sizeof(*bss));
  103. if (!bss)
  104. return NULL;
  105. bss->slave.bus = bus;
  106. bss->slave.cs = cs;
  107. bss->mmr_base = (void *)mmr_base;
  108. bss->ctl = SPE | MSTR | TDBR_CORE;
  109. if (mode & SPI_CPHA) bss->ctl |= CPHA;
  110. if (mode & SPI_CPOL) bss->ctl |= CPOL;
  111. if (mode & SPI_LSB_FIRST) bss->ctl |= LSBF;
  112. bss->baud = baud;
  113. bss->flg = mode & SPI_CS_HIGH ? 1 : 0;
  114. debug("%s: bus:%i cs:%i mmr:%x ctl:%x baud:%i flg:%i\n", __func__,
  115. bus, cs, mmr_base, bss->ctl, baud, bss->flg);
  116. return &bss->slave;
  117. }
  118. void spi_free_slave(struct spi_slave *slave)
  119. {
  120. struct bfin_spi_slave *bss = to_bfin_spi_slave(slave);
  121. free(bss);
  122. }
  123. static void spi_portmux(struct spi_slave *slave)
  124. {
  125. #if defined(__ADSPBF51x__)
  126. #define SET_MUX(port, mux, func) port##_mux = ((port##_mux & ~PORT_x_MUX_##mux##_MASK) | PORT_x_MUX_##mux##_FUNC_##func)
  127. u16 f_mux = bfin_read_PORTF_MUX();
  128. u16 f_fer = bfin_read_PORTF_FER();
  129. u16 g_mux = bfin_read_PORTG_MUX();
  130. u16 g_fer = bfin_read_PORTG_FER();
  131. u16 h_mux = bfin_read_PORTH_MUX();
  132. u16 h_fer = bfin_read_PORTH_FER();
  133. switch (slave->bus) {
  134. case 0:
  135. /* set SCK/MISO/MOSI */
  136. SET_MUX(g, 7, 1);
  137. g_fer |= PG12 | PG13 | PG14;
  138. switch (slave->cs) {
  139. case 1: SET_MUX(f, 2, 1); f_fer |= PF7; break;
  140. case 2: /* see G above */ g_fer |= PG15; break;
  141. case 3: SET_MUX(h, 1, 3); f_fer |= PH4; break;
  142. case 4: /* no muxing */ h_fer |= PH8; break;
  143. case 5: SET_MUX(g, 1, 3); h_fer |= PG3; break;
  144. case 6: /* no muxing */ break;
  145. case 7: /* no muxing */ break;
  146. }
  147. case 1:
  148. /* set SCK/MISO/MOSI */
  149. SET_MUX(h, 0, 2);
  150. h_fer |= PH1 | PH2 | PH3;
  151. switch (slave->cs) {
  152. case 1: SET_MUX(h, 2, 3); h_fer |= PH6; break;
  153. case 2: SET_MUX(f, 0, 3); f_fer |= PF0; break;
  154. case 3: SET_MUX(g, 0, 3); g_fer |= PG0; break;
  155. case 4: SET_MUX(f, 3, 3); f_fer |= PF8; break;
  156. case 5: SET_MUX(g, 6, 3); h_fer |= PG11; break;
  157. case 6: /* no muxing */ break;
  158. case 7: /* no muxing */ break;
  159. }
  160. }
  161. bfin_write_PORTF_MUX(f_mux);
  162. bfin_write_PORTF_FER(f_fer);
  163. bfin_write_PORTG_MUX(g_mux);
  164. bfin_write_PORTG_FER(g_fer);
  165. bfin_write_PORTH_MUX(h_mux);
  166. bfin_write_PORTH_FER(h_fer);
  167. #elif defined(__ADSPBF52x__)
  168. #define SET_MUX(port, mux, func) port##_mux = ((port##_mux & ~PORT_x_MUX_##mux##_MASK) | PORT_x_MUX_##mux##_FUNC_##func)
  169. u16 f_mux = bfin_read_PORTF_MUX();
  170. u16 f_fer = bfin_read_PORTF_FER();
  171. u16 g_mux = bfin_read_PORTG_MUX();
  172. u16 g_fer = bfin_read_PORTG_FER();
  173. u16 h_mux = bfin_read_PORTH_MUX();
  174. u16 h_fer = bfin_read_PORTH_FER();
  175. /* set SCK/MISO/MOSI */
  176. SET_MUX(g, 0, 3);
  177. g_fer |= PG2 | PG3 | PG4;
  178. switch (slave->cs) {
  179. case 1: /* see G above */ g_fer |= PG1; break;
  180. case 2: SET_MUX(f, 4, 3); f_fer |= PF12; break;
  181. case 3: SET_MUX(f, 4, 3); f_fer |= PF13; break;
  182. case 4: SET_MUX(h, 1, 1); h_fer |= PH8; break;
  183. case 5: SET_MUX(h, 2, 1); h_fer |= PH9; break;
  184. case 6: SET_MUX(f, 1, 3); f_fer |= PF9; break;
  185. case 7: SET_MUX(f, 2, 3); f_fer |= PF10; break;
  186. }
  187. bfin_write_PORTF_MUX(f_mux);
  188. bfin_write_PORTF_FER(f_fer);
  189. bfin_write_PORTG_MUX(g_mux);
  190. bfin_write_PORTG_FER(g_fer);
  191. bfin_write_PORTH_MUX(h_mux);
  192. bfin_write_PORTH_FER(h_fer);
  193. #elif defined(__ADSPBF534__) || defined(__ADSPBF536__) || defined(__ADSPBF537__)
  194. u16 mux = bfin_read_PORT_MUX();
  195. u16 f_fer = bfin_read_PORTF_FER();
  196. /* set SCK/MISO/MOSI */
  197. f_fer |= PF11 | PF12 | PF13;
  198. switch (slave->cs) {
  199. case 1: f_fer |= PF10; break;
  200. case 2: mux |= PJSE; break;
  201. case 3: mux |= PJSE; break;
  202. case 4: mux |= PFS4E; f_fer |= PF6; break;
  203. case 5: mux |= PFS5E; f_fer |= PF5; break;
  204. case 6: mux |= PFS6E; f_fer |= PF4; break;
  205. case 7: mux |= PJCE_SPI; break;
  206. }
  207. bfin_write_PORT_MUX(mux);
  208. bfin_write_PORTF_FER(f_fer);
  209. #elif defined(__ADSPBF538__) || defined(__ADSPBF539__)
  210. u16 fer, pins;
  211. if (slave->bus == 1)
  212. pins = PD0 | PD1 | PD2 | (slave->cs == 1 ? PD4 : 0);
  213. else if (slave->bus == 2)
  214. pins = PD5 | PD6 | PD7 | (slave->cs == 1 ? PD9 : 0);
  215. else
  216. pins = 0;
  217. if (pins) {
  218. fer = bfin_read_PORTDIO_FER();
  219. fer &= ~pins;
  220. bfin_write_PORTDIO_FER(fer);
  221. }
  222. #elif defined(__ADSPBF54x__)
  223. #define DO_MUX(port, pin) \
  224. mux = ((mux & ~PORT_x_MUX_##pin##_MASK) | PORT_x_MUX_##pin##_FUNC_1); \
  225. fer |= P##port##pin;
  226. u32 mux;
  227. u16 fer;
  228. switch (slave->bus) {
  229. case 0:
  230. mux = bfin_read_PORTE_MUX();
  231. fer = bfin_read_PORTE_FER();
  232. /* set SCK/MISO/MOSI */
  233. DO_MUX(E, 0);
  234. DO_MUX(E, 1);
  235. DO_MUX(E, 2);
  236. switch (slave->cs) {
  237. case 1: DO_MUX(E, 4); break;
  238. case 2: DO_MUX(E, 5); break;
  239. case 3: DO_MUX(E, 6); break;
  240. }
  241. bfin_write_PORTE_MUX(mux);
  242. bfin_write_PORTE_FER(fer);
  243. break;
  244. case 1:
  245. mux = bfin_read_PORTG_MUX();
  246. fer = bfin_read_PORTG_FER();
  247. /* set SCK/MISO/MOSI */
  248. DO_MUX(G, 8);
  249. DO_MUX(G, 9);
  250. DO_MUX(G, 10);
  251. switch (slave->cs) {
  252. case 1: DO_MUX(G, 5); break;
  253. case 2: DO_MUX(G, 6); break;
  254. case 3: DO_MUX(G, 7); break;
  255. }
  256. bfin_write_PORTG_MUX(mux);
  257. bfin_write_PORTG_FER(fer);
  258. break;
  259. case 2:
  260. mux = bfin_read_PORTB_MUX();
  261. fer = bfin_read_PORTB_FER();
  262. /* set SCK/MISO/MOSI */
  263. DO_MUX(B, 12);
  264. DO_MUX(B, 13);
  265. DO_MUX(B, 14);
  266. switch (slave->cs) {
  267. case 1: DO_MUX(B, 9); break;
  268. case 2: DO_MUX(B, 10); break;
  269. case 3: DO_MUX(B, 11); break;
  270. }
  271. bfin_write_PORTB_MUX(mux);
  272. bfin_write_PORTB_FER(fer);
  273. break;
  274. }
  275. #endif
  276. }
  277. int spi_claim_bus(struct spi_slave *slave)
  278. {
  279. struct bfin_spi_slave *bss = to_bfin_spi_slave(slave);
  280. debug("%s: bus:%i cs:%i\n", __func__, slave->bus, slave->cs);
  281. spi_portmux(slave);
  282. write_SPI_CTL(bss, bss->ctl);
  283. write_SPI_BAUD(bss, bss->baud);
  284. SSYNC();
  285. return 0;
  286. }
  287. void spi_release_bus(struct spi_slave *slave)
  288. {
  289. struct bfin_spi_slave *bss = to_bfin_spi_slave(slave);
  290. debug("%s: bus:%i cs:%i\n", __func__, slave->bus, slave->cs);
  291. write_SPI_CTL(bss, 0);
  292. SSYNC();
  293. }
  294. #ifndef CONFIG_BFIN_SPI_IDLE_VAL
  295. # define CONFIG_BFIN_SPI_IDLE_VAL 0xff
  296. #endif
  297. int spi_xfer(struct spi_slave *slave, unsigned int bitlen, const void *dout,
  298. void *din, unsigned long flags)
  299. {
  300. struct bfin_spi_slave *bss = to_bfin_spi_slave(slave);
  301. const u8 *tx = dout;
  302. u8 *rx = din;
  303. uint bytes = bitlen / 8;
  304. int ret = 0;
  305. debug("%s: bus:%i cs:%i bitlen:%i bytes:%i flags:%lx\n", __func__,
  306. slave->bus, slave->cs, bitlen, bytes, flags);
  307. if (bitlen == 0)
  308. goto done;
  309. /* we can only do 8 bit transfers */
  310. if (bitlen % 8) {
  311. flags |= SPI_XFER_END;
  312. goto done;
  313. }
  314. if (flags & SPI_XFER_BEGIN)
  315. spi_cs_activate(slave);
  316. /* todo: take advantage of hardware fifos and setup RX dma */
  317. while (bytes--) {
  318. u8 value = (tx ? *tx++ : CONFIG_BFIN_SPI_IDLE_VAL);
  319. debug("%s: tx:%x ", __func__, value);
  320. write_SPI_TDBR(bss, value);
  321. SSYNC();
  322. while ((read_SPI_STAT(bss) & TXS))
  323. if (ctrlc()) {
  324. ret = -1;
  325. goto done;
  326. }
  327. while (!(read_SPI_STAT(bss) & SPIF))
  328. if (ctrlc()) {
  329. ret = -1;
  330. goto done;
  331. }
  332. while (!(read_SPI_STAT(bss) & RXS))
  333. if (ctrlc()) {
  334. ret = -1;
  335. goto done;
  336. }
  337. value = read_SPI_RDBR(bss);
  338. if (rx)
  339. *rx++ = value;
  340. debug("rx:%x\n", value);
  341. }
  342. done:
  343. if (flags & SPI_XFER_END)
  344. spi_cs_deactivate(slave);
  345. return ret;
  346. }