mxc_spi.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  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 pre_div = 0, post_div = 0, i, reg_ctrl, reg_config;
  112. u32 ss_pol = 0, sclkpol = 0, sclkpha = 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. /*
  128. * The following computation is taken directly from Freescale's code.
  129. */
  130. if (clk_src > max_hz) {
  131. pre_div = DIV_ROUND_UP(clk_src, max_hz);
  132. if (pre_div > 16) {
  133. post_div = pre_div / 16;
  134. pre_div = 15;
  135. }
  136. if (post_div != 0) {
  137. for (i = 0; i < 16; i++) {
  138. if ((1 << i) >= post_div)
  139. break;
  140. }
  141. if (i == 16) {
  142. printf("Error: no divider for the freq: %d\n",
  143. max_hz);
  144. return -1;
  145. }
  146. post_div = i;
  147. }
  148. }
  149. debug("pre_div = %d, post_div=%d\n", pre_div, post_div);
  150. reg_ctrl = (reg_ctrl & ~MXC_CSPICTRL_SELCHAN(3)) |
  151. MXC_CSPICTRL_SELCHAN(cs);
  152. reg_ctrl = (reg_ctrl & ~MXC_CSPICTRL_PREDIV(0x0F)) |
  153. MXC_CSPICTRL_PREDIV(pre_div);
  154. reg_ctrl = (reg_ctrl & ~MXC_CSPICTRL_POSTDIV(0x0F)) |
  155. MXC_CSPICTRL_POSTDIV(post_div);
  156. /* We need to disable SPI before changing registers */
  157. reg_ctrl &= ~MXC_CSPICTRL_EN;
  158. if (mode & SPI_CS_HIGH)
  159. ss_pol = 1;
  160. if (mode & SPI_CPOL)
  161. sclkpol = 1;
  162. if (mode & SPI_CPHA)
  163. sclkpha = 1;
  164. reg_config = reg_read(&regs->cfg);
  165. /*
  166. * Configuration register setup
  167. * The MX51 supports different setup for each SS
  168. */
  169. reg_config = (reg_config & ~(1 << (cs + MXC_CSPICON_SSPOL))) |
  170. (ss_pol << (cs + MXC_CSPICON_SSPOL));
  171. reg_config = (reg_config & ~(1 << (cs + MXC_CSPICON_POL))) |
  172. (sclkpol << (cs + MXC_CSPICON_POL));
  173. reg_config = (reg_config & ~(1 << (cs + MXC_CSPICON_PHA))) |
  174. (sclkpha << (cs + MXC_CSPICON_PHA));
  175. debug("reg_ctrl = 0x%x\n", reg_ctrl);
  176. reg_write(&regs->ctrl, reg_ctrl);
  177. debug("reg_config = 0x%x\n", reg_config);
  178. reg_write(&regs->cfg, reg_config);
  179. /* save config register and control register */
  180. mxcs->ctrl_reg = reg_ctrl;
  181. mxcs->cfg_reg = reg_config;
  182. /* clear interrupt reg */
  183. reg_write(&regs->intr, 0);
  184. reg_write(&regs->stat, MXC_CSPICTRL_TC | MXC_CSPICTRL_RXOVF);
  185. return 0;
  186. }
  187. #endif
  188. int spi_xchg_single(struct spi_slave *slave, unsigned int bitlen,
  189. const u8 *dout, u8 *din, unsigned long flags)
  190. {
  191. struct mxc_spi_slave *mxcs = to_mxc_spi_slave(slave);
  192. int nbytes = (bitlen + 7) / 8;
  193. u32 data, cnt, i;
  194. struct cspi_regs *regs = (struct cspi_regs *)mxcs->base;
  195. debug("%s: bitlen %d dout 0x%x din 0x%x\n",
  196. __func__, bitlen, (u32)dout, (u32)din);
  197. mxcs->ctrl_reg = (mxcs->ctrl_reg &
  198. ~MXC_CSPICTRL_BITCOUNT(MXC_CSPICTRL_MAXBITS)) |
  199. MXC_CSPICTRL_BITCOUNT(bitlen - 1);
  200. reg_write(&regs->ctrl, mxcs->ctrl_reg | MXC_CSPICTRL_EN);
  201. #ifdef MXC_ECSPI
  202. reg_write(&regs->cfg, mxcs->cfg_reg);
  203. #endif
  204. /* Clear interrupt register */
  205. reg_write(&regs->stat, MXC_CSPICTRL_TC | MXC_CSPICTRL_RXOVF);
  206. /*
  207. * The SPI controller works only with words,
  208. * check if less than a word is sent.
  209. * Access to the FIFO is only 32 bit
  210. */
  211. if (bitlen % 32) {
  212. data = 0;
  213. cnt = (bitlen % 32) / 8;
  214. if (dout) {
  215. for (i = 0; i < cnt; i++) {
  216. data = (data << 8) | (*dout++ & 0xFF);
  217. }
  218. }
  219. debug("Sending SPI 0x%x\n", data);
  220. reg_write(&regs->txdata, data);
  221. nbytes -= cnt;
  222. }
  223. data = 0;
  224. while (nbytes > 0) {
  225. data = 0;
  226. if (dout) {
  227. /* Buffer is not 32-bit aligned */
  228. if ((unsigned long)dout & 0x03) {
  229. data = 0;
  230. for (i = 0; i < 4; i++)
  231. data = (data << 8) | (*dout++ & 0xFF);
  232. } else {
  233. data = *(u32 *)dout;
  234. data = cpu_to_be32(data);
  235. }
  236. dout += 4;
  237. }
  238. debug("Sending SPI 0x%x\n", data);
  239. reg_write(&regs->txdata, data);
  240. nbytes -= 4;
  241. }
  242. /* FIFO is written, now starts the transfer setting the XCH bit */
  243. reg_write(&regs->ctrl, mxcs->ctrl_reg |
  244. MXC_CSPICTRL_EN | MXC_CSPICTRL_XCH);
  245. /* Wait until the TC (Transfer completed) bit is set */
  246. while ((reg_read(&regs->stat) & MXC_CSPICTRL_TC) == 0)
  247. ;
  248. /* Transfer completed, clear any pending request */
  249. reg_write(&regs->stat, MXC_CSPICTRL_TC | MXC_CSPICTRL_RXOVF);
  250. nbytes = (bitlen + 7) / 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(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 = (bitlen + 7) / 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. static int decode_cs(struct mxc_spi_slave *mxcs, unsigned int cs)
  314. {
  315. int ret;
  316. /*
  317. * Some SPI devices require active chip-select over multiple
  318. * transactions, we achieve this using a GPIO. Still, the SPI
  319. * controller has to be configured to use one of its own chipselects.
  320. * To use this feature you have to call spi_setup_slave() with
  321. * cs = internal_cs | (gpio << 8), and you have to use some unused
  322. * on this SPI controller cs between 0 and 3.
  323. */
  324. if (cs > 3) {
  325. mxcs->gpio = cs >> 8;
  326. cs &= 3;
  327. ret = gpio_direction_output(mxcs->gpio, !(mxcs->ss_pol));
  328. if (ret) {
  329. printf("mxc_spi: cannot setup gpio %d\n", mxcs->gpio);
  330. return -EINVAL;
  331. }
  332. } else {
  333. mxcs->gpio = -1;
  334. }
  335. return cs;
  336. }
  337. struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
  338. unsigned int max_hz, unsigned int mode)
  339. {
  340. struct mxc_spi_slave *mxcs;
  341. int ret;
  342. if (bus >= ARRAY_SIZE(spi_bases))
  343. return NULL;
  344. mxcs = calloc(sizeof(struct mxc_spi_slave), 1);
  345. if (!mxcs) {
  346. puts("mxc_spi: SPI Slave not allocated !\n");
  347. return NULL;
  348. }
  349. mxcs->ss_pol = (mode & SPI_CS_HIGH) ? 1 : 0;
  350. ret = decode_cs(mxcs, cs);
  351. if (ret < 0) {
  352. free(mxcs);
  353. return NULL;
  354. }
  355. cs = ret;
  356. mxcs->slave.bus = bus;
  357. mxcs->slave.cs = cs;
  358. mxcs->base = spi_bases[bus];
  359. ret = spi_cfg_mxc(mxcs, cs, max_hz, mode);
  360. if (ret) {
  361. printf("mxc_spi: cannot setup SPI controller\n");
  362. free(mxcs);
  363. return NULL;
  364. }
  365. return &mxcs->slave;
  366. }
  367. void spi_free_slave(struct spi_slave *slave)
  368. {
  369. struct mxc_spi_slave *mxcs = to_mxc_spi_slave(slave);
  370. free(mxcs);
  371. }
  372. int spi_claim_bus(struct spi_slave *slave)
  373. {
  374. struct mxc_spi_slave *mxcs = to_mxc_spi_slave(slave);
  375. struct cspi_regs *regs = (struct cspi_regs *)mxcs->base;
  376. reg_write(&regs->rxdata, 1);
  377. udelay(1);
  378. reg_write(&regs->ctrl, mxcs->ctrl_reg);
  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. }