mxc_i2c.c 9.1 KB

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