bfin_spi.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  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/dma.h>
  14. #include <asm/gpio.h>
  15. #include <asm/portmux.h>
  16. #include <asm/mach-common/bits/spi.h>
  17. struct bfin_spi_slave {
  18. struct spi_slave slave;
  19. void *mmr_base;
  20. u16 ctl, baud, flg;
  21. };
  22. #define MAKE_SPI_FUNC(mmr, off) \
  23. static inline void write_##mmr(struct bfin_spi_slave *bss, u16 val) { bfin_write16(bss->mmr_base + off, val); } \
  24. static inline u16 read_##mmr(struct bfin_spi_slave *bss) { return bfin_read16(bss->mmr_base + off); }
  25. MAKE_SPI_FUNC(SPI_CTL, 0x00)
  26. MAKE_SPI_FUNC(SPI_FLG, 0x04)
  27. MAKE_SPI_FUNC(SPI_STAT, 0x08)
  28. MAKE_SPI_FUNC(SPI_TDBR, 0x0c)
  29. MAKE_SPI_FUNC(SPI_RDBR, 0x10)
  30. MAKE_SPI_FUNC(SPI_BAUD, 0x14)
  31. #define to_bfin_spi_slave(s) container_of(s, struct bfin_spi_slave, slave)
  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. void spi_set_speed(struct spi_slave *slave, uint hz)
  121. {
  122. struct bfin_spi_slave *bss = to_bfin_spi_slave(slave);
  123. ulong sclk;
  124. u32 baud;
  125. sclk = get_sclk();
  126. baud = sclk / (2 * hz);
  127. /* baud should be rounded up */
  128. if (sclk % (2 * hz))
  129. baud += 1;
  130. if (baud < 2)
  131. baud = 2;
  132. else if (baud > (u16)-1)
  133. baud = -1;
  134. bss->baud = baud;
  135. }
  136. struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
  137. unsigned int max_hz, unsigned int mode)
  138. {
  139. struct bfin_spi_slave *bss;
  140. u32 mmr_base;
  141. if (!spi_cs_is_valid(bus, cs))
  142. return NULL;
  143. if (bus >= ARRAY_SIZE(pins) || pins[bus] == NULL) {
  144. debug("%s: invalid bus %u\n", __func__, bus);
  145. return NULL;
  146. }
  147. switch (bus) {
  148. #ifdef SPI0_CTL
  149. case 0: mmr_base = SPI0_CTL; break;
  150. #endif
  151. #ifdef SPI1_CTL
  152. case 1: mmr_base = SPI1_CTL; break;
  153. #endif
  154. #ifdef SPI2_CTL
  155. case 2: mmr_base = SPI2_CTL; break;
  156. #endif
  157. default: 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. #ifdef __ADSPBF54x__
  206. # define SPI_DMA_BASE DMA4_NEXT_DESC_PTR
  207. #elif defined(__ADSPBF533__) || defined(__ADSPBF532__) || defined(__ADSPBF531__) || \
  208. defined(__ADSPBF538__) || defined(__ADSPBF539__)
  209. # define SPI_DMA_BASE DMA5_NEXT_DESC_PTR
  210. #elif defined(__ADSPBF561__)
  211. # define SPI_DMA_BASE DMA2_4_NEXT_DESC_PTR
  212. #elif defined(__ADSPBF537__) || defined(__ADSPBF536__) || defined(__ADSPBF534__) || \
  213. defined(__ADSPBF52x__) || defined(__ADSPBF51x__)
  214. # define SPI_DMA_BASE DMA7_NEXT_DESC_PTR
  215. # elif defined(__ADSPBF50x__)
  216. # define SPI_DMA_BASE DMA6_NEXT_DESC_PTR
  217. #else
  218. # error "Please provide SPI DMA channel defines"
  219. #endif
  220. static volatile struct dma_register *dma = (void *)SPI_DMA_BASE;
  221. #ifndef CONFIG_BFIN_SPI_IDLE_VAL
  222. # define CONFIG_BFIN_SPI_IDLE_VAL 0xff
  223. #endif
  224. #ifdef CONFIG_BFIN_SPI_NO_DMA
  225. # define SPI_DMA 0
  226. #else
  227. # define SPI_DMA 1
  228. #endif
  229. static int spi_dma_xfer(struct bfin_spi_slave *bss, const u8 *tx, u8 *rx,
  230. uint bytes)
  231. {
  232. int ret = -1;
  233. u16 ndsize, spi_config, dma_config;
  234. struct dmasg dmasg[2];
  235. const u8 *buf;
  236. if (tx) {
  237. debug("%s: doing half duplex TX\n", __func__);
  238. buf = tx;
  239. spi_config = TDBR_DMA;
  240. dma_config = 0;
  241. } else {
  242. debug("%s: doing half duplex RX\n", __func__);
  243. buf = rx;
  244. spi_config = RDBR_DMA;
  245. dma_config = WNR;
  246. }
  247. dmasg[0].start_addr = (unsigned long)buf;
  248. dmasg[0].x_modify = 1;
  249. dma_config |= WDSIZE_8 | DMAEN;
  250. if (bytes <= 65536) {
  251. blackfin_dcache_flush_invalidate_range(buf, buf + bytes);
  252. ndsize = NDSIZE_5;
  253. dmasg[0].cfg = NDSIZE_0 | dma_config | FLOW_STOP | DI_EN;
  254. dmasg[0].x_count = bytes;
  255. } else {
  256. blackfin_dcache_flush_invalidate_range(buf, buf + 65536 - 1);
  257. ndsize = NDSIZE_7;
  258. dmasg[0].cfg = NDSIZE_5 | dma_config | FLOW_ARRAY | DMA2D;
  259. dmasg[0].x_count = 0; /* 2^16 */
  260. dmasg[0].y_count = bytes >> 16; /* count / 2^16 */
  261. dmasg[0].y_modify = 1;
  262. dmasg[1].start_addr = (unsigned long)(buf + (bytes & ~0xFFFF));
  263. dmasg[1].cfg = NDSIZE_0 | dma_config | FLOW_STOP | DI_EN;
  264. dmasg[1].x_count = bytes & 0xFFFF; /* count % 2^16 */
  265. dmasg[1].x_modify = 1;
  266. }
  267. dma->cfg = 0;
  268. dma->irq_status = DMA_DONE | DMA_ERR;
  269. dma->curr_desc_ptr = dmasg;
  270. write_SPI_CTL(bss, (bss->ctl & ~TDBR_CORE));
  271. write_SPI_STAT(bss, -1);
  272. SSYNC();
  273. write_SPI_TDBR(bss, CONFIG_BFIN_SPI_IDLE_VAL);
  274. dma->cfg = ndsize | FLOW_ARRAY | DMAEN;
  275. write_SPI_CTL(bss, (bss->ctl & ~TDBR_CORE) | spi_config);
  276. SSYNC();
  277. /*
  278. * We already invalidated the first 64k,
  279. * now while we just wait invalidate the remaining part.
  280. * Its not likely that the DMA is going to overtake
  281. */
  282. if (bytes > 65536)
  283. blackfin_dcache_flush_invalidate_range(buf + 65536, buf + bytes);
  284. while (!(dma->irq_status & DMA_DONE))
  285. if (ctrlc())
  286. goto done;
  287. dma->cfg = 0;
  288. ret = 0;
  289. done:
  290. write_SPI_CTL(bss, bss->ctl);
  291. return ret;
  292. }
  293. static int spi_pio_xfer(struct bfin_spi_slave *bss, const u8 *tx, u8 *rx,
  294. uint bytes)
  295. {
  296. /* todo: take advantage of hardware fifos */
  297. while (bytes--) {
  298. u8 value = (tx ? *tx++ : CONFIG_BFIN_SPI_IDLE_VAL);
  299. debug("%s: tx:%x ", __func__, value);
  300. write_SPI_TDBR(bss, value);
  301. SSYNC();
  302. while ((read_SPI_STAT(bss) & TXS))
  303. if (ctrlc())
  304. return -1;
  305. while (!(read_SPI_STAT(bss) & SPIF))
  306. if (ctrlc())
  307. return -1;
  308. while (!(read_SPI_STAT(bss) & RXS))
  309. if (ctrlc())
  310. return -1;
  311. value = read_SPI_RDBR(bss);
  312. if (rx)
  313. *rx++ = value;
  314. debug("rx:%x\n", value);
  315. }
  316. return 0;
  317. }
  318. int spi_xfer(struct spi_slave *slave, unsigned int bitlen, const void *dout,
  319. void *din, unsigned long flags)
  320. {
  321. struct bfin_spi_slave *bss = to_bfin_spi_slave(slave);
  322. const u8 *tx = dout;
  323. u8 *rx = din;
  324. uint bytes = bitlen / 8;
  325. int ret = 0;
  326. debug("%s: bus:%i cs:%i bitlen:%i bytes:%i flags:%lx\n", __func__,
  327. slave->bus, slave->cs, bitlen, bytes, flags);
  328. if (bitlen == 0)
  329. goto done;
  330. /* we can only do 8 bit transfers */
  331. if (bitlen % 8) {
  332. flags |= SPI_XFER_END;
  333. goto done;
  334. }
  335. if (flags & SPI_XFER_BEGIN)
  336. spi_cs_activate(slave);
  337. /* TX DMA doesn't work quite right */
  338. if (SPI_DMA && bytes > 6 && (!tx /*|| !rx*/))
  339. ret = spi_dma_xfer(bss, tx, rx, bytes);
  340. else
  341. ret = spi_pio_xfer(bss, tx, rx, bytes);
  342. done:
  343. if (flags & SPI_XFER_END)
  344. spi_cs_deactivate(slave);
  345. return ret;
  346. }