mxc_spi.c 10 KB

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