mxc_i2c.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. /*
  2. * i2c driver for Freescale i.MX series
  3. *
  4. * (c) 2007 Pengutronix, Sascha Hauer <s.hauer@pengutronix.de>
  5. * (c) 2011 Marek Vasut <marek.vasut@gmail.com>
  6. *
  7. * Based on i2c-imx.c from linux kernel:
  8. * Copyright (C) 2005 Torsten Koschorrek <koschorrek at synertronixx.de>
  9. * Copyright (C) 2005 Matthias Blaschke <blaschke at synertronixx.de>
  10. * Copyright (C) 2007 RightHand Technologies, Inc.
  11. * Copyright (C) 2008 Darius Augulis <darius.augulis at teltonika.lt>
  12. *
  13. *
  14. * See file CREDITS for list of people who contributed to this
  15. * project.
  16. *
  17. * This program is free software; you can redistribute it and/or
  18. * modify it under the terms of the GNU General Public License as
  19. * published by the Free Software Foundation; either version 2 of
  20. * the License, or (at your option) any later version.
  21. *
  22. * This program is distributed in the hope that it will be useful,
  23. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25. * GNU General Public License for more details.
  26. *
  27. * You should have received a copy of the GNU General Public License
  28. * along with this program; if not, write to the Free Software
  29. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  30. * MA 02111-1307 USA
  31. */
  32. #include <common.h>
  33. #include <asm/arch/clock.h>
  34. #include <asm/arch/imx-regs.h>
  35. #include <asm/errno.h>
  36. #include <asm/io.h>
  37. #include <i2c.h>
  38. #include <watchdog.h>
  39. struct mxc_i2c_regs {
  40. uint32_t iadr;
  41. uint32_t ifdr;
  42. uint32_t i2cr;
  43. uint32_t i2sr;
  44. uint32_t i2dr;
  45. };
  46. #define I2CR_IEN (1 << 7)
  47. #define I2CR_IIEN (1 << 6)
  48. #define I2CR_MSTA (1 << 5)
  49. #define I2CR_MTX (1 << 4)
  50. #define I2CR_TX_NO_AK (1 << 3)
  51. #define I2CR_RSTA (1 << 2)
  52. #define I2SR_ICF (1 << 7)
  53. #define I2SR_IBB (1 << 5)
  54. #define I2SR_IAL (1 << 4)
  55. #define I2SR_IIF (1 << 1)
  56. #define I2SR_RX_NO_AK (1 << 0)
  57. #ifdef CONFIG_SYS_I2C_BASE
  58. #define I2C_BASE CONFIG_SYS_I2C_BASE
  59. #else
  60. #error "define CONFIG_SYS_I2C_BASE to use the mxc_i2c driver"
  61. #endif
  62. static u16 i2c_clk_div[50][2] = {
  63. { 22, 0x20 }, { 24, 0x21 }, { 26, 0x22 }, { 28, 0x23 },
  64. { 30, 0x00 }, { 32, 0x24 }, { 36, 0x25 }, { 40, 0x26 },
  65. { 42, 0x03 }, { 44, 0x27 }, { 48, 0x28 }, { 52, 0x05 },
  66. { 56, 0x29 }, { 60, 0x06 }, { 64, 0x2A }, { 72, 0x2B },
  67. { 80, 0x2C }, { 88, 0x09 }, { 96, 0x2D }, { 104, 0x0A },
  68. { 112, 0x2E }, { 128, 0x2F }, { 144, 0x0C }, { 160, 0x30 },
  69. { 192, 0x31 }, { 224, 0x32 }, { 240, 0x0F }, { 256, 0x33 },
  70. { 288, 0x10 }, { 320, 0x34 }, { 384, 0x35 }, { 448, 0x36 },
  71. { 480, 0x13 }, { 512, 0x37 }, { 576, 0x14 }, { 640, 0x38 },
  72. { 768, 0x39 }, { 896, 0x3A }, { 960, 0x17 }, { 1024, 0x3B },
  73. { 1152, 0x18 }, { 1280, 0x3C }, { 1536, 0x3D }, { 1792, 0x3E },
  74. { 1920, 0x1B }, { 2048, 0x3F }, { 2304, 0x1C }, { 2560, 0x1D },
  75. { 3072, 0x1E }, { 3840, 0x1F }
  76. };
  77. /*
  78. * Calculate and set proper clock divider
  79. */
  80. static uint8_t i2c_imx_get_clk(unsigned int rate)
  81. {
  82. unsigned int i2c_clk_rate;
  83. unsigned int div;
  84. u8 clk_div;
  85. #if defined(CONFIG_MX31)
  86. struct clock_control_regs *sc_regs =
  87. (struct clock_control_regs *)CCM_BASE;
  88. /* start the required I2C clock */
  89. writel(readl(&sc_regs->cgr0) | (3 << CONFIG_SYS_I2C_CLK_OFFSET),
  90. &sc_regs->cgr0);
  91. #endif
  92. /* Divider value calculation */
  93. i2c_clk_rate = mxc_get_clock(MXC_IPG_PERCLK);
  94. div = (i2c_clk_rate + rate - 1) / rate;
  95. if (div < i2c_clk_div[0][0])
  96. clk_div = 0;
  97. else if (div > i2c_clk_div[ARRAY_SIZE(i2c_clk_div) - 1][0])
  98. clk_div = ARRAY_SIZE(i2c_clk_div) - 1;
  99. else
  100. for (clk_div = 0; i2c_clk_div[clk_div][0] < div; clk_div++)
  101. ;
  102. /* Store divider value */
  103. return clk_div;
  104. }
  105. /*
  106. * Init I2C Bus
  107. */
  108. void i2c_init(int speed, int unused)
  109. {
  110. struct mxc_i2c_regs *i2c_regs = (struct mxc_i2c_regs *)I2C_BASE;
  111. u8 clk_idx = i2c_imx_get_clk(speed);
  112. u8 idx = i2c_clk_div[clk_idx][1];
  113. /* Store divider value */
  114. writeb(idx, &i2c_regs->ifdr);
  115. /* Reset module */
  116. writeb(0, &i2c_regs->i2cr);
  117. writeb(0, &i2c_regs->i2sr);
  118. }
  119. /*
  120. * Set I2C Speed
  121. */
  122. int i2c_set_bus_speed(unsigned int speed)
  123. {
  124. i2c_init(speed, 0);
  125. return 0;
  126. }
  127. /*
  128. * Get I2C Speed
  129. */
  130. unsigned int i2c_get_bus_speed(void)
  131. {
  132. struct mxc_i2c_regs *i2c_regs = (struct mxc_i2c_regs *)I2C_BASE;
  133. u8 clk_idx = readb(&i2c_regs->ifdr);
  134. u8 clk_div;
  135. for (clk_div = 0; i2c_clk_div[clk_div][1] != clk_idx; clk_div++)
  136. ;
  137. return mxc_get_clock(MXC_IPG_PERCLK) / i2c_clk_div[clk_div][0];
  138. }
  139. #define ST_BUS_IDLE (0 | (I2SR_IBB << 8))
  140. #define ST_BUS_BUSY (I2SR_IBB | (I2SR_IBB << 8))
  141. #define ST_IIF (I2SR_IIF | (I2SR_IIF << 8))
  142. static int wait_for_sr_state(struct mxc_i2c_regs *i2c_regs, unsigned state)
  143. {
  144. unsigned sr;
  145. ulong elapsed;
  146. ulong start_time = get_timer(0);
  147. for (;;) {
  148. sr = readb(&i2c_regs->i2sr);
  149. if (sr & I2SR_IAL) {
  150. writeb(sr & ~I2SR_IAL, &i2c_regs->i2sr);
  151. printf("%s: Arbitration lost sr=%x cr=%x state=%x\n",
  152. __func__, sr, readb(&i2c_regs->i2cr), state);
  153. return -ERESTART;
  154. }
  155. if ((sr & (state >> 8)) == (unsigned char)state)
  156. return sr;
  157. WATCHDOG_RESET();
  158. elapsed = get_timer(start_time);
  159. if (elapsed > (CONFIG_SYS_HZ / 10)) /* .1 seconds */
  160. break;
  161. }
  162. printf("%s: failed sr=%x cr=%x state=%x\n", __func__,
  163. sr, readb(&i2c_regs->i2cr), state);
  164. return -ETIMEDOUT;
  165. }
  166. static int tx_byte(struct mxc_i2c_regs *i2c_regs, u8 byte)
  167. {
  168. int ret;
  169. writeb(0, &i2c_regs->i2sr);
  170. writeb(byte, &i2c_regs->i2dr);
  171. ret = wait_for_sr_state(i2c_regs, ST_IIF);
  172. if (ret < 0)
  173. return ret;
  174. if (ret & I2SR_RX_NO_AK)
  175. return -ENODEV;
  176. return 0;
  177. }
  178. /*
  179. * Stop I2C transaction
  180. */
  181. void i2c_imx_stop(void)
  182. {
  183. int ret;
  184. struct mxc_i2c_regs *i2c_regs = (struct mxc_i2c_regs *)I2C_BASE;
  185. unsigned int temp = readb(&i2c_regs->i2cr);
  186. temp &= ~(I2CR_MSTA | I2CR_MTX);
  187. writeb(temp, &i2c_regs->i2cr);
  188. ret = wait_for_sr_state(i2c_regs, ST_BUS_IDLE);
  189. if (ret < 0)
  190. printf("%s:trigger stop failed\n", __func__);
  191. }
  192. /*
  193. * Send start signal, chip address and
  194. * write register address
  195. */
  196. static int i2c_init_transfer(struct mxc_i2c_regs *i2c_regs,
  197. uchar chip, uint addr, int alen)
  198. {
  199. unsigned int temp;
  200. int ret;
  201. /* Enable I2C controller */
  202. if (!(readb(&i2c_regs->i2cr) & I2CR_IEN)) {
  203. writeb(I2CR_IEN, &i2c_regs->i2cr);
  204. /* Wait for controller to be stable */
  205. udelay(50);
  206. }
  207. if (readb(&i2c_regs->iadr) == (chip << 1))
  208. writeb((chip << 1) ^ 2, &i2c_regs->iadr);
  209. writeb(0, &i2c_regs->i2sr);
  210. ret = wait_for_sr_state(i2c_regs, ST_BUS_IDLE);
  211. if (ret < 0)
  212. goto exit;
  213. /* Start I2C transaction */
  214. temp = readb(&i2c_regs->i2cr);
  215. temp |= I2CR_MSTA;
  216. writeb(temp, &i2c_regs->i2cr);
  217. ret = wait_for_sr_state(i2c_regs, ST_BUS_BUSY);
  218. if (ret < 0)
  219. goto exit;
  220. temp |= I2CR_MTX | I2CR_TX_NO_AK;
  221. writeb(temp, &i2c_regs->i2cr);
  222. /* write slave address */
  223. ret = tx_byte(i2c_regs, chip << 1);
  224. if (ret < 0)
  225. goto exit;
  226. while (alen--) {
  227. ret = tx_byte(i2c_regs, (addr >> (alen * 8)) & 0xff);
  228. if (ret < 0)
  229. goto exit;
  230. }
  231. return 0;
  232. exit:
  233. i2c_imx_stop();
  234. /* Disable I2C controller */
  235. writeb(0, &i2c_regs->i2cr);
  236. return ret;
  237. }
  238. /*
  239. * Read data from I2C device
  240. */
  241. int i2c_read(uchar chip, uint addr, int alen, uchar *buf, int len)
  242. {
  243. struct mxc_i2c_regs *i2c_regs = (struct mxc_i2c_regs *)I2C_BASE;
  244. int ret;
  245. unsigned int temp;
  246. int i;
  247. ret = i2c_init_transfer(i2c_regs, chip, addr, alen);
  248. if (ret < 0)
  249. return ret;
  250. temp = readb(&i2c_regs->i2cr);
  251. temp |= I2CR_RSTA;
  252. writeb(temp, &i2c_regs->i2cr);
  253. ret = tx_byte(i2c_regs, (chip << 1) | 1);
  254. if (ret < 0) {
  255. i2c_imx_stop();
  256. return ret;
  257. }
  258. /* setup bus to read data */
  259. temp = readb(&i2c_regs->i2cr);
  260. temp &= ~(I2CR_MTX | I2CR_TX_NO_AK);
  261. if (len == 1)
  262. temp |= I2CR_TX_NO_AK;
  263. writeb(temp, &i2c_regs->i2cr);
  264. writeb(0, &i2c_regs->i2sr);
  265. readb(&i2c_regs->i2dr); /* dummy read to clear ICF */
  266. /* read data */
  267. for (i = 0; i < len; i++) {
  268. ret = wait_for_sr_state(i2c_regs, ST_IIF);
  269. if (ret < 0) {
  270. i2c_imx_stop();
  271. return ret;
  272. }
  273. /*
  274. * It must generate STOP before read I2DR to prevent
  275. * controller from generating another clock cycle
  276. */
  277. if (i == (len - 1)) {
  278. i2c_imx_stop();
  279. } else if (i == (len - 2)) {
  280. temp = readb(&i2c_regs->i2cr);
  281. temp |= I2CR_TX_NO_AK;
  282. writeb(temp, &i2c_regs->i2cr);
  283. }
  284. writeb(0, &i2c_regs->i2sr);
  285. buf[i] = readb(&i2c_regs->i2dr);
  286. }
  287. i2c_imx_stop();
  288. return 0;
  289. }
  290. /*
  291. * Write data to I2C device
  292. */
  293. int i2c_write(uchar chip, uint addr, int alen, uchar *buf, int len)
  294. {
  295. struct mxc_i2c_regs *i2c_regs = (struct mxc_i2c_regs *)I2C_BASE;
  296. int ret;
  297. int i;
  298. ret = i2c_init_transfer(i2c_regs, chip, addr, alen);
  299. if (ret < 0)
  300. return ret;
  301. for (i = 0; i < len; i++) {
  302. ret = tx_byte(i2c_regs, buf[i]);
  303. if (ret < 0)
  304. break;
  305. }
  306. i2c_imx_stop();
  307. return ret;
  308. }
  309. /*
  310. * Test if a chip at a given address responds (probe the chip)
  311. */
  312. int i2c_probe(uchar chip)
  313. {
  314. return i2c_write(chip, 0, 0, NULL, 0);
  315. }