mxc_spi.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  1. /*
  2. * Copyright (C) 2008, Guennadi Liakhovetski <lg@denx.de>
  3. *
  4. * SPDX-License-Identifier: GPL-2.0+
  5. */
  6. #include <common.h>
  7. #include <malloc.h>
  8. #include <spi.h>
  9. #include <linux/errno.h>
  10. #include <asm/io.h>
  11. #include <asm/gpio.h>
  12. #include <asm/arch/imx-regs.h>
  13. #include <asm/arch/clock.h>
  14. #include <asm/imx-common/spi.h>
  15. #ifdef CONFIG_MX27
  16. /* i.MX27 has a completely wrong register layout and register definitions in the
  17. * datasheet, the correct one is in the Freescale's Linux driver */
  18. #error "i.MX27 CSPI not supported due to drastic differences in register definitions" \
  19. "See linux mxc_spi driver from Freescale for details."
  20. #endif
  21. static unsigned long spi_bases[] = {
  22. MXC_SPI_BASE_ADDRESSES
  23. };
  24. __weak int board_spi_cs_gpio(unsigned bus, unsigned cs)
  25. {
  26. return -1;
  27. }
  28. #define OUT MXC_GPIO_DIRECTION_OUT
  29. #define reg_read readl
  30. #define reg_write(a, v) writel(v, a)
  31. #if !defined(CONFIG_SYS_SPI_MXC_WAIT)
  32. #define CONFIG_SYS_SPI_MXC_WAIT (CONFIG_SYS_HZ/100) /* 10 ms */
  33. #endif
  34. struct mxc_spi_slave {
  35. struct spi_slave slave;
  36. unsigned long base;
  37. u32 ctrl_reg;
  38. #if defined(MXC_ECSPI)
  39. u32 cfg_reg;
  40. #endif
  41. int gpio;
  42. int ss_pol;
  43. unsigned int max_hz;
  44. unsigned int mode;
  45. };
  46. static inline struct mxc_spi_slave *to_mxc_spi_slave(struct spi_slave *slave)
  47. {
  48. return container_of(slave, struct mxc_spi_slave, slave);
  49. }
  50. void spi_cs_activate(struct spi_slave *slave)
  51. {
  52. struct mxc_spi_slave *mxcs = to_mxc_spi_slave(slave);
  53. if (mxcs->gpio > 0)
  54. gpio_set_value(mxcs->gpio, mxcs->ss_pol);
  55. }
  56. void spi_cs_deactivate(struct spi_slave *slave)
  57. {
  58. struct mxc_spi_slave *mxcs = to_mxc_spi_slave(slave);
  59. if (mxcs->gpio > 0)
  60. gpio_set_value(mxcs->gpio,
  61. !(mxcs->ss_pol));
  62. }
  63. u32 get_cspi_div(u32 div)
  64. {
  65. int i;
  66. for (i = 0; i < 8; i++) {
  67. if (div <= (4 << i))
  68. return i;
  69. }
  70. return i;
  71. }
  72. #ifdef MXC_CSPI
  73. static s32 spi_cfg_mxc(struct mxc_spi_slave *mxcs, unsigned int cs)
  74. {
  75. unsigned int ctrl_reg;
  76. u32 clk_src;
  77. u32 div;
  78. unsigned int max_hz = mxcs->max_hz;
  79. unsigned int mode = mxcs->mode;
  80. clk_src = mxc_get_clock(MXC_CSPI_CLK);
  81. div = DIV_ROUND_UP(clk_src, max_hz);
  82. div = get_cspi_div(div);
  83. debug("clk %d Hz, div %d, real clk %d Hz\n",
  84. max_hz, div, clk_src / (4 << div));
  85. ctrl_reg = MXC_CSPICTRL_CHIPSELECT(cs) |
  86. MXC_CSPICTRL_BITCOUNT(MXC_CSPICTRL_MAXBITS) |
  87. MXC_CSPICTRL_DATARATE(div) |
  88. MXC_CSPICTRL_EN |
  89. #ifdef CONFIG_MX35
  90. MXC_CSPICTRL_SSCTL |
  91. #endif
  92. MXC_CSPICTRL_MODE;
  93. if (mode & SPI_CPHA)
  94. ctrl_reg |= MXC_CSPICTRL_PHA;
  95. if (mode & SPI_CPOL)
  96. ctrl_reg |= MXC_CSPICTRL_POL;
  97. if (mode & SPI_CS_HIGH)
  98. ctrl_reg |= MXC_CSPICTRL_SSPOL;
  99. mxcs->ctrl_reg = ctrl_reg;
  100. return 0;
  101. }
  102. #endif
  103. #ifdef MXC_ECSPI
  104. static s32 spi_cfg_mxc(struct mxc_spi_slave *mxcs, unsigned int cs)
  105. {
  106. u32 clk_src = mxc_get_clock(MXC_CSPI_CLK);
  107. s32 reg_ctrl, reg_config;
  108. u32 ss_pol = 0, sclkpol = 0, sclkpha = 0, sclkctl = 0;
  109. u32 pre_div = 0, post_div = 0;
  110. struct cspi_regs *regs = (struct cspi_regs *)mxcs->base;
  111. unsigned int max_hz = mxcs->max_hz;
  112. unsigned int mode = mxcs->mode;
  113. /*
  114. * Reset SPI and set all CSs to master mode, if toggling
  115. * between slave and master mode we might see a glitch
  116. * on the clock line
  117. */
  118. reg_ctrl = MXC_CSPICTRL_MODE_MASK;
  119. reg_write(&regs->ctrl, reg_ctrl);
  120. reg_ctrl |= MXC_CSPICTRL_EN;
  121. reg_write(&regs->ctrl, reg_ctrl);
  122. if (clk_src > max_hz) {
  123. pre_div = (clk_src - 1) / max_hz;
  124. /* fls(1) = 1, fls(0x80000000) = 32, fls(16) = 5 */
  125. post_div = fls(pre_div);
  126. if (post_div > 4) {
  127. post_div -= 4;
  128. if (post_div >= 16) {
  129. printf("Error: no divider for the freq: %d\n",
  130. max_hz);
  131. return -1;
  132. }
  133. pre_div >>= post_div;
  134. } else {
  135. post_div = 0;
  136. }
  137. }
  138. debug("pre_div = %d, post_div=%d\n", pre_div, post_div);
  139. reg_ctrl = (reg_ctrl & ~MXC_CSPICTRL_SELCHAN(3)) |
  140. MXC_CSPICTRL_SELCHAN(cs);
  141. reg_ctrl = (reg_ctrl & ~MXC_CSPICTRL_PREDIV(0x0F)) |
  142. MXC_CSPICTRL_PREDIV(pre_div);
  143. reg_ctrl = (reg_ctrl & ~MXC_CSPICTRL_POSTDIV(0x0F)) |
  144. MXC_CSPICTRL_POSTDIV(post_div);
  145. if (mode & SPI_CS_HIGH)
  146. ss_pol = 1;
  147. if (mode & SPI_CPOL) {
  148. sclkpol = 1;
  149. sclkctl = 1;
  150. }
  151. if (mode & SPI_CPHA)
  152. sclkpha = 1;
  153. reg_config = reg_read(&regs->cfg);
  154. /*
  155. * Configuration register setup
  156. * The MX51 supports different setup for each SS
  157. */
  158. reg_config = (reg_config & ~(1 << (cs + MXC_CSPICON_SSPOL))) |
  159. (ss_pol << (cs + MXC_CSPICON_SSPOL));
  160. reg_config = (reg_config & ~(1 << (cs + MXC_CSPICON_POL))) |
  161. (sclkpol << (cs + MXC_CSPICON_POL));
  162. reg_config = (reg_config & ~(1 << (cs + MXC_CSPICON_CTL))) |
  163. (sclkctl << (cs + MXC_CSPICON_CTL));
  164. reg_config = (reg_config & ~(1 << (cs + MXC_CSPICON_PHA))) |
  165. (sclkpha << (cs + MXC_CSPICON_PHA));
  166. debug("reg_ctrl = 0x%x\n", reg_ctrl);
  167. reg_write(&regs->ctrl, reg_ctrl);
  168. debug("reg_config = 0x%x\n", reg_config);
  169. reg_write(&regs->cfg, reg_config);
  170. /* save config register and control register */
  171. mxcs->ctrl_reg = reg_ctrl;
  172. mxcs->cfg_reg = reg_config;
  173. /* clear interrupt reg */
  174. reg_write(&regs->intr, 0);
  175. reg_write(&regs->stat, MXC_CSPICTRL_TC | MXC_CSPICTRL_RXOVF);
  176. return 0;
  177. }
  178. #endif
  179. int spi_xchg_single(struct spi_slave *slave, unsigned int bitlen,
  180. const u8 *dout, u8 *din, unsigned long flags)
  181. {
  182. struct mxc_spi_slave *mxcs = to_mxc_spi_slave(slave);
  183. int nbytes = DIV_ROUND_UP(bitlen, 8);
  184. u32 data, cnt, i;
  185. struct cspi_regs *regs = (struct cspi_regs *)mxcs->base;
  186. u32 ts;
  187. int status;
  188. debug("%s: bitlen %d dout 0x%x din 0x%x\n",
  189. __func__, bitlen, (u32)dout, (u32)din);
  190. mxcs->ctrl_reg = (mxcs->ctrl_reg &
  191. ~MXC_CSPICTRL_BITCOUNT(MXC_CSPICTRL_MAXBITS)) |
  192. MXC_CSPICTRL_BITCOUNT(bitlen - 1);
  193. reg_write(&regs->ctrl, mxcs->ctrl_reg | MXC_CSPICTRL_EN);
  194. #ifdef MXC_ECSPI
  195. reg_write(&regs->cfg, mxcs->cfg_reg);
  196. #endif
  197. /* Clear interrupt register */
  198. reg_write(&regs->stat, MXC_CSPICTRL_TC | MXC_CSPICTRL_RXOVF);
  199. /*
  200. * The SPI controller works only with words,
  201. * check if less than a word is sent.
  202. * Access to the FIFO is only 32 bit
  203. */
  204. if (bitlen % 32) {
  205. data = 0;
  206. cnt = (bitlen % 32) / 8;
  207. if (dout) {
  208. for (i = 0; i < cnt; i++) {
  209. data = (data << 8) | (*dout++ & 0xFF);
  210. }
  211. }
  212. debug("Sending SPI 0x%x\n", data);
  213. reg_write(&regs->txdata, data);
  214. nbytes -= cnt;
  215. }
  216. data = 0;
  217. while (nbytes > 0) {
  218. data = 0;
  219. if (dout) {
  220. /* Buffer is not 32-bit aligned */
  221. if ((unsigned long)dout & 0x03) {
  222. data = 0;
  223. for (i = 0; i < 4; i++)
  224. data = (data << 8) | (*dout++ & 0xFF);
  225. } else {
  226. data = *(u32 *)dout;
  227. data = cpu_to_be32(data);
  228. dout += 4;
  229. }
  230. }
  231. debug("Sending SPI 0x%x\n", data);
  232. reg_write(&regs->txdata, data);
  233. nbytes -= 4;
  234. }
  235. /* FIFO is written, now starts the transfer setting the XCH bit */
  236. reg_write(&regs->ctrl, mxcs->ctrl_reg |
  237. MXC_CSPICTRL_EN | MXC_CSPICTRL_XCH);
  238. ts = get_timer(0);
  239. status = reg_read(&regs->stat);
  240. /* Wait until the TC (Transfer completed) bit is set */
  241. while ((status & MXC_CSPICTRL_TC) == 0) {
  242. if (get_timer(ts) > CONFIG_SYS_SPI_MXC_WAIT) {
  243. printf("spi_xchg_single: Timeout!\n");
  244. return -1;
  245. }
  246. status = reg_read(&regs->stat);
  247. }
  248. /* Transfer completed, clear any pending request */
  249. reg_write(&regs->stat, MXC_CSPICTRL_TC | MXC_CSPICTRL_RXOVF);
  250. nbytes = DIV_ROUND_UP(bitlen, 8);
  251. cnt = nbytes % 32;
  252. if (bitlen % 32) {
  253. data = reg_read(&regs->rxdata);
  254. cnt = (bitlen % 32) / 8;
  255. data = cpu_to_be32(data) >> ((sizeof(data) - cnt) * 8);
  256. debug("SPI Rx unaligned: 0x%x\n", data);
  257. if (din) {
  258. memcpy(din, &data, cnt);
  259. din += cnt;
  260. }
  261. nbytes -= cnt;
  262. }
  263. while (nbytes > 0) {
  264. u32 tmp;
  265. tmp = reg_read(&regs->rxdata);
  266. data = cpu_to_be32(tmp);
  267. debug("SPI Rx: 0x%x 0x%x\n", tmp, data);
  268. cnt = min_t(u32, nbytes, sizeof(data));
  269. if (din) {
  270. memcpy(din, &data, cnt);
  271. din += cnt;
  272. }
  273. nbytes -= cnt;
  274. }
  275. return 0;
  276. }
  277. int spi_xfer(struct spi_slave *slave, unsigned int bitlen, const void *dout,
  278. void *din, unsigned long flags)
  279. {
  280. int n_bytes = DIV_ROUND_UP(bitlen, 8);
  281. int n_bits;
  282. int ret;
  283. u32 blk_size;
  284. u8 *p_outbuf = (u8 *)dout;
  285. u8 *p_inbuf = (u8 *)din;
  286. if (!slave)
  287. return -1;
  288. if (flags & SPI_XFER_BEGIN)
  289. spi_cs_activate(slave);
  290. while (n_bytes > 0) {
  291. if (n_bytes < MAX_SPI_BYTES)
  292. blk_size = n_bytes;
  293. else
  294. blk_size = MAX_SPI_BYTES;
  295. n_bits = blk_size * 8;
  296. ret = spi_xchg_single(slave, n_bits, p_outbuf, p_inbuf, 0);
  297. if (ret)
  298. return ret;
  299. if (dout)
  300. p_outbuf += blk_size;
  301. if (din)
  302. p_inbuf += blk_size;
  303. n_bytes -= blk_size;
  304. }
  305. if (flags & SPI_XFER_END) {
  306. spi_cs_deactivate(slave);
  307. }
  308. return 0;
  309. }
  310. void spi_init(void)
  311. {
  312. }
  313. /*
  314. * Some SPI devices require active chip-select over multiple
  315. * transactions, we achieve this using a GPIO. Still, the SPI
  316. * controller has to be configured to use one of its own chipselects.
  317. * To use this feature you have to implement board_spi_cs_gpio() to assign
  318. * a gpio value for each cs (-1 if cs doesn't need to use gpio).
  319. * You must use some unused on this SPI controller cs between 0 and 3.
  320. */
  321. static int setup_cs_gpio(struct mxc_spi_slave *mxcs,
  322. unsigned int bus, unsigned int cs)
  323. {
  324. int ret;
  325. mxcs->gpio = board_spi_cs_gpio(bus, cs);
  326. if (mxcs->gpio == -1)
  327. return 0;
  328. ret = gpio_direction_output(mxcs->gpio, !(mxcs->ss_pol));
  329. if (ret) {
  330. printf("mxc_spi: cannot setup gpio %d\n", mxcs->gpio);
  331. return -EINVAL;
  332. }
  333. return 0;
  334. }
  335. struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
  336. unsigned int max_hz, unsigned int mode)
  337. {
  338. struct mxc_spi_slave *mxcs;
  339. int ret;
  340. if (bus >= ARRAY_SIZE(spi_bases))
  341. return NULL;
  342. if (max_hz == 0) {
  343. printf("Error: desired clock is 0\n");
  344. return NULL;
  345. }
  346. mxcs = spi_alloc_slave(struct mxc_spi_slave, bus, cs);
  347. if (!mxcs) {
  348. puts("mxc_spi: SPI Slave not allocated !\n");
  349. return NULL;
  350. }
  351. mxcs->ss_pol = (mode & SPI_CS_HIGH) ? 1 : 0;
  352. ret = setup_cs_gpio(mxcs, bus, cs);
  353. if (ret < 0) {
  354. free(mxcs);
  355. return NULL;
  356. }
  357. mxcs->base = spi_bases[bus];
  358. mxcs->max_hz = max_hz;
  359. mxcs->mode = mode;
  360. return &mxcs->slave;
  361. }
  362. void spi_free_slave(struct spi_slave *slave)
  363. {
  364. struct mxc_spi_slave *mxcs = to_mxc_spi_slave(slave);
  365. free(mxcs);
  366. }
  367. int spi_claim_bus(struct spi_slave *slave)
  368. {
  369. int ret;
  370. struct mxc_spi_slave *mxcs = to_mxc_spi_slave(slave);
  371. struct cspi_regs *regs = (struct cspi_regs *)mxcs->base;
  372. reg_write(&regs->rxdata, 1);
  373. udelay(1);
  374. ret = spi_cfg_mxc(mxcs, slave->cs);
  375. if (ret) {
  376. printf("mxc_spi: cannot setup SPI controller\n");
  377. return ret;
  378. }
  379. reg_write(&regs->period, MXC_CSPIPERIOD_32KHZ);
  380. reg_write(&regs->intr, 0);
  381. return 0;
  382. }
  383. void spi_release_bus(struct spi_slave *slave)
  384. {
  385. /* TODO: Shut the controller down */
  386. }