mxc_i2c.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  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/io.h>
  34. #if defined(CONFIG_HARD_I2C)
  35. #include <asm/arch/clock.h>
  36. #include <asm/arch/imx-regs.h>
  37. struct mxc_i2c_regs {
  38. uint32_t iadr;
  39. uint32_t ifdr;
  40. uint32_t i2cr;
  41. uint32_t i2sr;
  42. uint32_t i2dr;
  43. };
  44. #define I2CR_IEN (1 << 7)
  45. #define I2CR_IIEN (1 << 6)
  46. #define I2CR_MSTA (1 << 5)
  47. #define I2CR_MTX (1 << 4)
  48. #define I2CR_TX_NO_AK (1 << 3)
  49. #define I2CR_RSTA (1 << 2)
  50. #define I2SR_ICF (1 << 7)
  51. #define I2SR_IBB (1 << 5)
  52. #define I2SR_IIF (1 << 1)
  53. #define I2SR_RX_NO_AK (1 << 0)
  54. #if defined(CONFIG_SYS_I2C_MX31_PORT1)
  55. #define I2C_BASE 0x43f80000
  56. #define I2C_CLK_OFFSET 26
  57. #elif defined (CONFIG_SYS_I2C_MX31_PORT2)
  58. #define I2C_BASE 0x43f98000
  59. #define I2C_CLK_OFFSET 28
  60. #elif defined (CONFIG_SYS_I2C_MX31_PORT3)
  61. #define I2C_BASE 0x43f84000
  62. #define I2C_CLK_OFFSET 30
  63. #elif defined(CONFIG_SYS_I2C_MX53_PORT1)
  64. #define I2C_BASE I2C1_BASE_ADDR
  65. #elif defined(CONFIG_SYS_I2C_MX53_PORT2)
  66. #define I2C_BASE I2C2_BASE_ADDR
  67. #elif defined(CONFIG_SYS_I2C_MX35_PORT1)
  68. #define I2C_BASE I2C_BASE_ADDR
  69. #else
  70. #error "define CONFIG_SYS_I2C_MX<Processor>_PORTx to use the mx I2C driver"
  71. #endif
  72. #define I2C_MAX_TIMEOUT 10000
  73. static u16 i2c_clk_div[50][2] = {
  74. { 22, 0x20 }, { 24, 0x21 }, { 26, 0x22 }, { 28, 0x23 },
  75. { 30, 0x00 }, { 32, 0x24 }, { 36, 0x25 }, { 40, 0x26 },
  76. { 42, 0x03 }, { 44, 0x27 }, { 48, 0x28 }, { 52, 0x05 },
  77. { 56, 0x29 }, { 60, 0x06 }, { 64, 0x2A }, { 72, 0x2B },
  78. { 80, 0x2C }, { 88, 0x09 }, { 96, 0x2D }, { 104, 0x0A },
  79. { 112, 0x2E }, { 128, 0x2F }, { 144, 0x0C }, { 160, 0x30 },
  80. { 192, 0x31 }, { 224, 0x32 }, { 240, 0x0F }, { 256, 0x33 },
  81. { 288, 0x10 }, { 320, 0x34 }, { 384, 0x35 }, { 448, 0x36 },
  82. { 480, 0x13 }, { 512, 0x37 }, { 576, 0x14 }, { 640, 0x38 },
  83. { 768, 0x39 }, { 896, 0x3A }, { 960, 0x17 }, { 1024, 0x3B },
  84. { 1152, 0x18 }, { 1280, 0x3C }, { 1536, 0x3D }, { 1792, 0x3E },
  85. { 1920, 0x1B }, { 2048, 0x3F }, { 2304, 0x1C }, { 2560, 0x1D },
  86. { 3072, 0x1E }, { 3840, 0x1F }
  87. };
  88. static u8 clk_idx;
  89. /*
  90. * Calculate and set proper clock divider
  91. */
  92. static void i2c_imx_set_clk(unsigned int rate)
  93. {
  94. struct mxc_i2c_regs *i2c_regs = (struct mxc_i2c_regs *)I2C_BASE;
  95. unsigned int i2c_clk_rate;
  96. unsigned int div;
  97. int i;
  98. #if defined(CONFIG_MX31)
  99. struct clock_control_regs *sc_regs =
  100. (struct clock_control_regs *)CCM_BASE;
  101. /* start the required I2C clock */
  102. writel(readl(&sc_regs->cgr0) | (3 << I2C_CLK_OFFSET),
  103. &sc_regs->cgr0);
  104. #endif
  105. /* Divider value calculation */
  106. i2c_clk_rate = mxc_get_clock(MXC_IPG_PERCLK);
  107. div = (i2c_clk_rate + rate - 1) / rate;
  108. if (div < i2c_clk_div[0][0])
  109. i = 0;
  110. else if (div > i2c_clk_div[ARRAY_SIZE(i2c_clk_div) - 1][0])
  111. i = ARRAY_SIZE(i2c_clk_div) - 1;
  112. else
  113. for (i = 0; i2c_clk_div[i][0] < div; i++)
  114. ;
  115. /* Store divider value */
  116. clk_idx = i2c_clk_div[i][1];
  117. writeb(clk_idx, &i2c_regs->ifdr);
  118. }
  119. /*
  120. * Reset I2C Controller
  121. */
  122. void i2c_reset(void)
  123. {
  124. struct mxc_i2c_regs *i2c_regs = (struct mxc_i2c_regs *)I2C_BASE;
  125. writeb(0, &i2c_regs->i2cr); /* Reset module */
  126. writeb(0, &i2c_regs->i2sr);
  127. }
  128. /*
  129. * Init I2C Bus
  130. */
  131. void i2c_init(int speed, int unused)
  132. {
  133. i2c_imx_set_clk(speed);
  134. i2c_reset();
  135. }
  136. /*
  137. * Wait for bus to be busy (or free if for_busy = 0)
  138. *
  139. * for_busy = 1: Wait for IBB to be asserted
  140. * for_busy = 0: Wait for IBB to be de-asserted
  141. */
  142. int i2c_imx_bus_busy(int for_busy)
  143. {
  144. struct mxc_i2c_regs *i2c_regs = (struct mxc_i2c_regs *)I2C_BASE;
  145. unsigned int temp;
  146. int timeout = I2C_MAX_TIMEOUT;
  147. while (timeout--) {
  148. temp = readb(&i2c_regs->i2sr);
  149. if (for_busy && (temp & I2SR_IBB))
  150. return 0;
  151. if (!for_busy && !(temp & I2SR_IBB))
  152. return 0;
  153. udelay(1);
  154. }
  155. return 1;
  156. }
  157. /*
  158. * Wait for transaction to complete
  159. */
  160. int i2c_imx_trx_complete(void)
  161. {
  162. struct mxc_i2c_regs *i2c_regs = (struct mxc_i2c_regs *)I2C_BASE;
  163. int timeout = I2C_MAX_TIMEOUT;
  164. while (timeout--) {
  165. if (readb(&i2c_regs->i2sr) & I2SR_IIF) {
  166. writeb(0, &i2c_regs->i2sr);
  167. return 0;
  168. }
  169. udelay(1);
  170. }
  171. return 1;
  172. }
  173. /*
  174. * Check if the transaction was ACKed
  175. */
  176. int i2c_imx_acked(void)
  177. {
  178. struct mxc_i2c_regs *i2c_regs = (struct mxc_i2c_regs *)I2C_BASE;
  179. return readb(&i2c_regs->i2sr) & I2SR_RX_NO_AK;
  180. }
  181. /*
  182. * Start the controller
  183. */
  184. int i2c_imx_start(void)
  185. {
  186. struct mxc_i2c_regs *i2c_regs = (struct mxc_i2c_regs *)I2C_BASE;
  187. unsigned int temp = 0;
  188. int result;
  189. writeb(clk_idx, &i2c_regs->ifdr);
  190. /* Enable I2C controller */
  191. writeb(0, &i2c_regs->i2sr);
  192. writeb(I2CR_IEN, &i2c_regs->i2cr);
  193. /* Wait controller to be stable */
  194. udelay(50);
  195. /* Start I2C transaction */
  196. temp = readb(&i2c_regs->i2cr);
  197. temp |= I2CR_MSTA;
  198. writeb(temp, &i2c_regs->i2cr);
  199. result = i2c_imx_bus_busy(1);
  200. if (result)
  201. return result;
  202. temp |= I2CR_MTX | I2CR_TX_NO_AK;
  203. writeb(temp, &i2c_regs->i2cr);
  204. return 0;
  205. }
  206. /*
  207. * Stop the controller
  208. */
  209. void i2c_imx_stop(void)
  210. {
  211. struct mxc_i2c_regs *i2c_regs = (struct mxc_i2c_regs *)I2C_BASE;
  212. unsigned int temp = 0;
  213. /* Stop I2C transaction */
  214. temp = readb(&i2c_regs->i2cr);
  215. temp |= ~(I2CR_MSTA | I2CR_MTX);
  216. writeb(temp, &i2c_regs->i2cr);
  217. i2c_imx_bus_busy(0);
  218. /* Disable I2C controller */
  219. writeb(0, &i2c_regs->i2cr);
  220. }
  221. /*
  222. * Set chip address and access mode
  223. *
  224. * read = 1: READ access
  225. * read = 0: WRITE access
  226. */
  227. int i2c_imx_set_chip_addr(uchar chip, int read)
  228. {
  229. struct mxc_i2c_regs *i2c_regs = (struct mxc_i2c_regs *)I2C_BASE;
  230. int ret;
  231. writeb((chip << 1) | read, &i2c_regs->i2dr);
  232. ret = i2c_imx_trx_complete();
  233. if (ret)
  234. return ret;
  235. ret = i2c_imx_acked();
  236. if (ret)
  237. return ret;
  238. return ret;
  239. }
  240. /*
  241. * Write register address
  242. */
  243. int i2c_imx_set_reg_addr(uint addr, int alen)
  244. {
  245. struct mxc_i2c_regs *i2c_regs = (struct mxc_i2c_regs *)I2C_BASE;
  246. int ret;
  247. int i;
  248. for (i = 0; i < (8 * alen); i += 8) {
  249. writeb((addr >> i) & 0xff, &i2c_regs->i2dr);
  250. ret = i2c_imx_trx_complete();
  251. if (ret)
  252. break;
  253. ret = i2c_imx_acked();
  254. if (ret)
  255. break;
  256. }
  257. return ret;
  258. }
  259. /*
  260. * Try if a chip add given address responds (probe the chip)
  261. */
  262. int i2c_probe(uchar chip)
  263. {
  264. int ret;
  265. ret = i2c_imx_start();
  266. if (ret)
  267. return ret;
  268. ret = i2c_imx_set_chip_addr(chip, 0);
  269. if (ret)
  270. return ret;
  271. i2c_imx_stop();
  272. return ret;
  273. }
  274. /*
  275. * Read data from I2C device
  276. */
  277. int i2c_read(uchar chip, uint addr, int alen, uchar *buf, int len)
  278. {
  279. struct mxc_i2c_regs *i2c_regs = (struct mxc_i2c_regs *)I2C_BASE;
  280. int ret;
  281. unsigned int temp;
  282. int i;
  283. ret = i2c_imx_start();
  284. if (ret)
  285. return ret;
  286. /* write slave address */
  287. ret = i2c_imx_set_chip_addr(chip, 0);
  288. if (ret)
  289. return ret;
  290. ret = i2c_imx_set_reg_addr(addr, alen);
  291. if (ret)
  292. return ret;
  293. temp = readb(&i2c_regs->i2cr);
  294. temp |= I2CR_RSTA;
  295. writeb(temp, &i2c_regs->i2cr);
  296. ret = i2c_imx_set_chip_addr(chip, 1);
  297. if (ret)
  298. return ret;
  299. /* setup bus to read data */
  300. temp = readb(&i2c_regs->i2cr);
  301. temp &= ~(I2CR_MTX | I2CR_TX_NO_AK);
  302. if (len == 1)
  303. temp |= I2CR_TX_NO_AK;
  304. writeb(temp, &i2c_regs->i2cr);
  305. readb(&i2c_regs->i2dr);
  306. /* read data */
  307. for (i = 0; i < len; i++) {
  308. ret = i2c_imx_trx_complete();
  309. if (ret)
  310. return ret;
  311. /*
  312. * It must generate STOP before read I2DR to prevent
  313. * controller from generating another clock cycle
  314. */
  315. if (i == (len - 1)) {
  316. temp = readb(&i2c_regs->i2cr);
  317. temp &= ~(I2CR_MSTA | I2CR_MTX);
  318. writeb(temp, &i2c_regs->i2cr);
  319. i2c_imx_bus_busy(0);
  320. } else if (i == (len - 2)) {
  321. temp = readb(&i2c_regs->i2cr);
  322. temp |= I2CR_TX_NO_AK;
  323. writeb(temp, &i2c_regs->i2cr);
  324. }
  325. buf[i] = readb(&i2c_regs->i2dr);
  326. }
  327. i2c_imx_stop();
  328. return ret;
  329. }
  330. /*
  331. * Write data to I2C device
  332. */
  333. int i2c_write(uchar chip, uint addr, int alen, uchar *buf, int len)
  334. {
  335. struct mxc_i2c_regs *i2c_regs = (struct mxc_i2c_regs *)I2C_BASE;
  336. int ret;
  337. int i;
  338. ret = i2c_imx_start();
  339. if (ret)
  340. return ret;
  341. /* write slave address */
  342. ret = i2c_imx_set_chip_addr(chip, 0);
  343. if (ret)
  344. return ret;
  345. ret = i2c_imx_set_reg_addr(addr, alen);
  346. if (ret)
  347. return ret;
  348. for (i = 0; i < len; i++) {
  349. writeb(buf[i], &i2c_regs->i2dr);
  350. ret = i2c_imx_trx_complete();
  351. if (ret)
  352. return ret;
  353. ret = i2c_imx_acked();
  354. if (ret)
  355. return ret;
  356. }
  357. i2c_imx_stop();
  358. return ret;
  359. }
  360. #endif /* CONFIG_HARD_I2C */