mxc_spi.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  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. /* Reset spi */
  119. reg_write(&regs->ctrl, 0);
  120. reg_write(&regs->ctrl, MXC_CSPICTRL_EN);
  121. reg_ctrl = reg_read(&regs->ctrl);
  122. /*
  123. * The following computation is taken directly from Freescale's code.
  124. */
  125. if (clk_src > max_hz) {
  126. pre_div = DIV_ROUND_UP(clk_src, max_hz);
  127. if (pre_div > 16) {
  128. post_div = pre_div / 16;
  129. pre_div = 15;
  130. }
  131. if (post_div != 0) {
  132. for (i = 0; i < 16; i++) {
  133. if ((1 << i) >= post_div)
  134. break;
  135. }
  136. if (i == 16) {
  137. printf("Error: no divider for the freq: %d\n",
  138. max_hz);
  139. return -1;
  140. }
  141. post_div = i;
  142. }
  143. }
  144. debug("pre_div = %d, post_div=%d\n", pre_div, post_div);
  145. reg_ctrl = (reg_ctrl & ~MXC_CSPICTRL_SELCHAN(3)) |
  146. MXC_CSPICTRL_SELCHAN(cs);
  147. reg_ctrl = (reg_ctrl & ~MXC_CSPICTRL_PREDIV(0x0F)) |
  148. MXC_CSPICTRL_PREDIV(pre_div);
  149. reg_ctrl = (reg_ctrl & ~MXC_CSPICTRL_POSTDIV(0x0F)) |
  150. MXC_CSPICTRL_POSTDIV(post_div);
  151. /* always set to master mode */
  152. reg_ctrl |= 1 << (cs + 4);
  153. /* We need to disable SPI before changing registers */
  154. reg_ctrl &= ~MXC_CSPICTRL_EN;
  155. if (mode & SPI_CS_HIGH)
  156. ss_pol = 1;
  157. if (mode & SPI_CPOL)
  158. sclkpol = 1;
  159. if (mode & SPI_CPHA)
  160. sclkpha = 1;
  161. reg_config = reg_read(&regs->cfg);
  162. /*
  163. * Configuration register setup
  164. * The MX51 supports different setup for each SS
  165. */
  166. reg_config = (reg_config & ~(1 << (cs + MXC_CSPICON_SSPOL))) |
  167. (ss_pol << (cs + MXC_CSPICON_SSPOL));
  168. reg_config = (reg_config & ~(1 << (cs + MXC_CSPICON_POL))) |
  169. (sclkpol << (cs + MXC_CSPICON_POL));
  170. reg_config = (reg_config & ~(1 << (cs + MXC_CSPICON_PHA))) |
  171. (sclkpha << (cs + MXC_CSPICON_PHA));
  172. debug("reg_ctrl = 0x%x\n", reg_ctrl);
  173. reg_write(&regs->ctrl, reg_ctrl);
  174. debug("reg_config = 0x%x\n", reg_config);
  175. reg_write(&regs->cfg, reg_config);
  176. /* save config register and control register */
  177. mxcs->ctrl_reg = reg_ctrl;
  178. mxcs->cfg_reg = reg_config;
  179. /* clear interrupt reg */
  180. reg_write(&regs->intr, 0);
  181. reg_write(&regs->stat, MXC_CSPICTRL_TC | MXC_CSPICTRL_RXOVF);
  182. return 0;
  183. }
  184. #endif
  185. int spi_xchg_single(struct spi_slave *slave, unsigned int bitlen,
  186. const u8 *dout, u8 *din, unsigned long flags)
  187. {
  188. struct mxc_spi_slave *mxcs = to_mxc_spi_slave(slave);
  189. int nbytes = (bitlen + 7) / 8;
  190. u32 data, cnt, i;
  191. struct cspi_regs *regs = (struct cspi_regs *)mxcs->base;
  192. debug("%s: bitlen %d dout 0x%x din 0x%x\n",
  193. __func__, bitlen, (u32)dout, (u32)din);
  194. mxcs->ctrl_reg = (mxcs->ctrl_reg &
  195. ~MXC_CSPICTRL_BITCOUNT(MXC_CSPICTRL_MAXBITS)) |
  196. MXC_CSPICTRL_BITCOUNT(bitlen - 1);
  197. reg_write(&regs->ctrl, mxcs->ctrl_reg | MXC_CSPICTRL_EN);
  198. #ifdef MXC_ECSPI
  199. reg_write(&regs->cfg, mxcs->cfg_reg);
  200. #endif
  201. /* Clear interrupt register */
  202. reg_write(&regs->stat, MXC_CSPICTRL_TC | MXC_CSPICTRL_RXOVF);
  203. /*
  204. * The SPI controller works only with words,
  205. * check if less than a word is sent.
  206. * Access to the FIFO is only 32 bit
  207. */
  208. if (bitlen % 32) {
  209. data = 0;
  210. cnt = (bitlen % 32) / 8;
  211. if (dout) {
  212. for (i = 0; i < cnt; i++) {
  213. data = (data << 8) | (*dout++ & 0xFF);
  214. }
  215. }
  216. debug("Sending SPI 0x%x\n", data);
  217. reg_write(&regs->txdata, data);
  218. nbytes -= cnt;
  219. }
  220. data = 0;
  221. while (nbytes > 0) {
  222. data = 0;
  223. if (dout) {
  224. /* Buffer is not 32-bit aligned */
  225. if ((unsigned long)dout & 0x03) {
  226. data = 0;
  227. for (i = 0; i < 4; i++)
  228. data = (data << 8) | (*dout++ & 0xFF);
  229. } else {
  230. data = *(u32 *)dout;
  231. data = cpu_to_be32(data);
  232. }
  233. dout += 4;
  234. }
  235. debug("Sending SPI 0x%x\n", data);
  236. reg_write(&regs->txdata, data);
  237. nbytes -= 4;
  238. }
  239. /* FIFO is written, now starts the transfer setting the XCH bit */
  240. reg_write(&regs->ctrl, mxcs->ctrl_reg |
  241. MXC_CSPICTRL_EN | MXC_CSPICTRL_XCH);
  242. /* Wait until the TC (Transfer completed) bit is set */
  243. while ((reg_read(&regs->stat) & MXC_CSPICTRL_TC) == 0)
  244. ;
  245. /* Transfer completed, clear any pending request */
  246. reg_write(&regs->stat, MXC_CSPICTRL_TC | MXC_CSPICTRL_RXOVF);
  247. nbytes = (bitlen + 7) / 8;
  248. cnt = nbytes % 32;
  249. if (bitlen % 32) {
  250. data = reg_read(&regs->rxdata);
  251. cnt = (bitlen % 32) / 8;
  252. data = cpu_to_be32(data) >> ((sizeof(data) - cnt) * 8);
  253. debug("SPI Rx unaligned: 0x%x\n", data);
  254. if (din) {
  255. memcpy(din, &data, cnt);
  256. din += cnt;
  257. }
  258. nbytes -= cnt;
  259. }
  260. while (nbytes > 0) {
  261. u32 tmp;
  262. tmp = reg_read(&regs->rxdata);
  263. data = cpu_to_be32(tmp);
  264. debug("SPI Rx: 0x%x 0x%x\n", tmp, data);
  265. cnt = min(nbytes, sizeof(data));
  266. if (din) {
  267. memcpy(din, &data, cnt);
  268. din += cnt;
  269. }
  270. nbytes -= cnt;
  271. }
  272. return 0;
  273. }
  274. int spi_xfer(struct spi_slave *slave, unsigned int bitlen, const void *dout,
  275. void *din, unsigned long flags)
  276. {
  277. int n_bytes = (bitlen + 7) / 8;
  278. int n_bits;
  279. int ret;
  280. u32 blk_size;
  281. u8 *p_outbuf = (u8 *)dout;
  282. u8 *p_inbuf = (u8 *)din;
  283. if (!slave)
  284. return -1;
  285. if (flags & SPI_XFER_BEGIN)
  286. spi_cs_activate(slave);
  287. while (n_bytes > 0) {
  288. if (n_bytes < MAX_SPI_BYTES)
  289. blk_size = n_bytes;
  290. else
  291. blk_size = MAX_SPI_BYTES;
  292. n_bits = blk_size * 8;
  293. ret = spi_xchg_single(slave, n_bits, p_outbuf, p_inbuf, 0);
  294. if (ret)
  295. return ret;
  296. if (dout)
  297. p_outbuf += blk_size;
  298. if (din)
  299. p_inbuf += blk_size;
  300. n_bytes -= blk_size;
  301. }
  302. if (flags & SPI_XFER_END) {
  303. spi_cs_deactivate(slave);
  304. }
  305. return 0;
  306. }
  307. void spi_init(void)
  308. {
  309. }
  310. static int decode_cs(struct mxc_spi_slave *mxcs, unsigned int cs)
  311. {
  312. int ret;
  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 call spi_setup_slave() with
  318. * cs = internal_cs | (gpio << 8), and you have to use some unused
  319. * on this SPI controller cs between 0 and 3.
  320. */
  321. if (cs > 3) {
  322. mxcs->gpio = cs >> 8;
  323. cs &= 3;
  324. ret = gpio_direction_output(mxcs->gpio, !(mxcs->ss_pol));
  325. if (ret) {
  326. printf("mxc_spi: cannot setup gpio %d\n", mxcs->gpio);
  327. return -EINVAL;
  328. }
  329. } else {
  330. mxcs->gpio = -1;
  331. }
  332. return cs;
  333. }
  334. struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
  335. unsigned int max_hz, unsigned int mode)
  336. {
  337. struct mxc_spi_slave *mxcs;
  338. int ret;
  339. if (bus >= ARRAY_SIZE(spi_bases))
  340. return NULL;
  341. mxcs = calloc(sizeof(struct mxc_spi_slave), 1);
  342. if (!mxcs) {
  343. puts("mxc_spi: SPI Slave not allocated !\n");
  344. return NULL;
  345. }
  346. mxcs->ss_pol = (mode & SPI_CS_HIGH) ? 1 : 0;
  347. ret = decode_cs(mxcs, cs);
  348. if (ret < 0) {
  349. free(mxcs);
  350. return NULL;
  351. }
  352. cs = ret;
  353. mxcs->slave.bus = bus;
  354. mxcs->slave.cs = cs;
  355. mxcs->base = spi_bases[bus];
  356. ret = spi_cfg_mxc(mxcs, cs, max_hz, mode);
  357. if (ret) {
  358. printf("mxc_spi: cannot setup SPI controller\n");
  359. free(mxcs);
  360. return NULL;
  361. }
  362. return &mxcs->slave;
  363. }
  364. void spi_free_slave(struct spi_slave *slave)
  365. {
  366. struct mxc_spi_slave *mxcs = to_mxc_spi_slave(slave);
  367. free(mxcs);
  368. }
  369. int spi_claim_bus(struct spi_slave *slave)
  370. {
  371. struct mxc_spi_slave *mxcs = to_mxc_spi_slave(slave);
  372. struct cspi_regs *regs = (struct cspi_regs *)mxcs->base;
  373. reg_write(&regs->rxdata, 1);
  374. udelay(1);
  375. reg_write(&regs->ctrl, mxcs->ctrl_reg);
  376. reg_write(&regs->period, MXC_CSPIPERIOD_32KHZ);
  377. reg_write(&regs->intr, 0);
  378. return 0;
  379. }
  380. void spi_release_bus(struct spi_slave *slave)
  381. {
  382. /* TODO: Shut the controller down */
  383. }