mxc_i2c.c 9.9 KB

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