mxc_i2c.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778
  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. * SPDX-License-Identifier: GPL-2.0+
  15. */
  16. #include <common.h>
  17. #include <asm/arch/clock.h>
  18. #include <asm/arch/imx-regs.h>
  19. #include <asm/errno.h>
  20. #include <asm/imx-common/mxc_i2c.h>
  21. #include <asm/io.h>
  22. #include <i2c.h>
  23. #include <watchdog.h>
  24. #include <dm.h>
  25. #include <fdtdec.h>
  26. DECLARE_GLOBAL_DATA_PTR;
  27. #define I2C_QUIRK_FLAG (1 << 0)
  28. #define IMX_I2C_REGSHIFT 2
  29. #define VF610_I2C_REGSHIFT 0
  30. /* Register index */
  31. #define IADR 0
  32. #define IFDR 1
  33. #define I2CR 2
  34. #define I2SR 3
  35. #define I2DR 4
  36. #define I2CR_IIEN (1 << 6)
  37. #define I2CR_MSTA (1 << 5)
  38. #define I2CR_MTX (1 << 4)
  39. #define I2CR_TX_NO_AK (1 << 3)
  40. #define I2CR_RSTA (1 << 2)
  41. #define I2SR_ICF (1 << 7)
  42. #define I2SR_IBB (1 << 5)
  43. #define I2SR_IAL (1 << 4)
  44. #define I2SR_IIF (1 << 1)
  45. #define I2SR_RX_NO_AK (1 << 0)
  46. #ifdef I2C_QUIRK_REG
  47. #define I2CR_IEN (0 << 7)
  48. #define I2CR_IDIS (1 << 7)
  49. #define I2SR_IIF_CLEAR (1 << 1)
  50. #else
  51. #define I2CR_IEN (1 << 7)
  52. #define I2CR_IDIS (0 << 7)
  53. #define I2SR_IIF_CLEAR (0 << 1)
  54. #endif
  55. #if defined(CONFIG_HARD_I2C) && !defined(CONFIG_SYS_I2C_BASE)
  56. #error "define CONFIG_SYS_I2C_BASE to use the mxc_i2c driver"
  57. #endif
  58. #ifdef I2C_QUIRK_REG
  59. static u16 i2c_clk_div[60][2] = {
  60. { 20, 0x00 }, { 22, 0x01 }, { 24, 0x02 }, { 26, 0x03 },
  61. { 28, 0x04 }, { 30, 0x05 }, { 32, 0x09 }, { 34, 0x06 },
  62. { 36, 0x0A }, { 40, 0x07 }, { 44, 0x0C }, { 48, 0x0D },
  63. { 52, 0x43 }, { 56, 0x0E }, { 60, 0x45 }, { 64, 0x12 },
  64. { 68, 0x0F }, { 72, 0x13 }, { 80, 0x14 }, { 88, 0x15 },
  65. { 96, 0x19 }, { 104, 0x16 }, { 112, 0x1A }, { 128, 0x17 },
  66. { 136, 0x4F }, { 144, 0x1C }, { 160, 0x1D }, { 176, 0x55 },
  67. { 192, 0x1E }, { 208, 0x56 }, { 224, 0x22 }, { 228, 0x24 },
  68. { 240, 0x1F }, { 256, 0x23 }, { 288, 0x5C }, { 320, 0x25 },
  69. { 384, 0x26 }, { 448, 0x2A }, { 480, 0x27 }, { 512, 0x2B },
  70. { 576, 0x2C }, { 640, 0x2D }, { 768, 0x31 }, { 896, 0x32 },
  71. { 960, 0x2F }, { 1024, 0x33 }, { 1152, 0x34 }, { 1280, 0x35 },
  72. { 1536, 0x36 }, { 1792, 0x3A }, { 1920, 0x37 }, { 2048, 0x3B },
  73. { 2304, 0x3C }, { 2560, 0x3D }, { 3072, 0x3E }, { 3584, 0x7A },
  74. { 3840, 0x3F }, { 4096, 0x7B }, { 5120, 0x7D }, { 6144, 0x7E },
  75. };
  76. #else
  77. static u16 i2c_clk_div[50][2] = {
  78. { 22, 0x20 }, { 24, 0x21 }, { 26, 0x22 }, { 28, 0x23 },
  79. { 30, 0x00 }, { 32, 0x24 }, { 36, 0x25 }, { 40, 0x26 },
  80. { 42, 0x03 }, { 44, 0x27 }, { 48, 0x28 }, { 52, 0x05 },
  81. { 56, 0x29 }, { 60, 0x06 }, { 64, 0x2A }, { 72, 0x2B },
  82. { 80, 0x2C }, { 88, 0x09 }, { 96, 0x2D }, { 104, 0x0A },
  83. { 112, 0x2E }, { 128, 0x2F }, { 144, 0x0C }, { 160, 0x30 },
  84. { 192, 0x31 }, { 224, 0x32 }, { 240, 0x0F }, { 256, 0x33 },
  85. { 288, 0x10 }, { 320, 0x34 }, { 384, 0x35 }, { 448, 0x36 },
  86. { 480, 0x13 }, { 512, 0x37 }, { 576, 0x14 }, { 640, 0x38 },
  87. { 768, 0x39 }, { 896, 0x3A }, { 960, 0x17 }, { 1024, 0x3B },
  88. { 1152, 0x18 }, { 1280, 0x3C }, { 1536, 0x3D }, { 1792, 0x3E },
  89. { 1920, 0x1B }, { 2048, 0x3F }, { 2304, 0x1C }, { 2560, 0x1D },
  90. { 3072, 0x1E }, { 3840, 0x1F }
  91. };
  92. #endif
  93. #ifndef CONFIG_SYS_MXC_I2C1_SPEED
  94. #define CONFIG_SYS_MXC_I2C1_SPEED 100000
  95. #endif
  96. #ifndef CONFIG_SYS_MXC_I2C2_SPEED
  97. #define CONFIG_SYS_MXC_I2C2_SPEED 100000
  98. #endif
  99. #ifndef CONFIG_SYS_MXC_I2C3_SPEED
  100. #define CONFIG_SYS_MXC_I2C3_SPEED 100000
  101. #endif
  102. #ifndef CONFIG_SYS_MXC_I2C4_SPEED
  103. #define CONFIG_SYS_MXC_I2C4_SPEED 100000
  104. #endif
  105. #ifndef CONFIG_SYS_MXC_I2C1_SLAVE
  106. #define CONFIG_SYS_MXC_I2C1_SLAVE 0
  107. #endif
  108. #ifndef CONFIG_SYS_MXC_I2C2_SLAVE
  109. #define CONFIG_SYS_MXC_I2C2_SLAVE 0
  110. #endif
  111. #ifndef CONFIG_SYS_MXC_I2C3_SLAVE
  112. #define CONFIG_SYS_MXC_I2C3_SLAVE 0
  113. #endif
  114. #ifndef CONFIG_SYS_MXC_I2C4_SLAVE
  115. #define CONFIG_SYS_MXC_I2C4_SLAVE 0
  116. #endif
  117. /*
  118. * Calculate and set proper clock divider
  119. */
  120. static uint8_t i2c_imx_get_clk(struct mxc_i2c_bus *i2c_bus, unsigned int rate)
  121. {
  122. unsigned int i2c_clk_rate;
  123. unsigned int div;
  124. u8 clk_div;
  125. #if defined(CONFIG_MX31)
  126. struct clock_control_regs *sc_regs =
  127. (struct clock_control_regs *)CCM_BASE;
  128. /* start the required I2C clock */
  129. writel(readl(&sc_regs->cgr0) | (3 << CONFIG_SYS_I2C_CLK_OFFSET),
  130. &sc_regs->cgr0);
  131. #endif
  132. /* Divider value calculation */
  133. i2c_clk_rate = mxc_get_clock(MXC_I2C_CLK);
  134. div = (i2c_clk_rate + rate - 1) / rate;
  135. if (div < i2c_clk_div[0][0])
  136. clk_div = 0;
  137. else if (div > i2c_clk_div[ARRAY_SIZE(i2c_clk_div) - 1][0])
  138. clk_div = ARRAY_SIZE(i2c_clk_div) - 1;
  139. else
  140. for (clk_div = 0; i2c_clk_div[clk_div][0] < div; clk_div++)
  141. ;
  142. /* Store divider value */
  143. return clk_div;
  144. }
  145. /*
  146. * Set I2C Bus speed
  147. */
  148. static int bus_i2c_set_bus_speed(struct mxc_i2c_bus *i2c_bus, int speed)
  149. {
  150. ulong base = i2c_bus->base;
  151. bool quirk = i2c_bus->driver_data & I2C_QUIRK_FLAG ? true : false;
  152. u8 clk_idx = i2c_imx_get_clk(i2c_bus, speed);
  153. u8 idx = i2c_clk_div[clk_idx][1];
  154. int reg_shift = quirk ? VF610_I2C_REGSHIFT : IMX_I2C_REGSHIFT;
  155. if (!base)
  156. return -ENODEV;
  157. /* Store divider value */
  158. writeb(idx, base + (IFDR << reg_shift));
  159. /* Reset module */
  160. writeb(I2CR_IDIS, base + (I2CR << reg_shift));
  161. writeb(0, base + (I2SR << reg_shift));
  162. return 0;
  163. }
  164. #define ST_BUS_IDLE (0 | (I2SR_IBB << 8))
  165. #define ST_BUS_BUSY (I2SR_IBB | (I2SR_IBB << 8))
  166. #define ST_IIF (I2SR_IIF | (I2SR_IIF << 8))
  167. static int wait_for_sr_state(struct mxc_i2c_bus *i2c_bus, unsigned state)
  168. {
  169. unsigned sr;
  170. ulong elapsed;
  171. bool quirk = i2c_bus->driver_data & I2C_QUIRK_FLAG ? true : false;
  172. int reg_shift = quirk ? VF610_I2C_REGSHIFT : IMX_I2C_REGSHIFT;
  173. ulong base = i2c_bus->base;
  174. ulong start_time = get_timer(0);
  175. for (;;) {
  176. sr = readb(base + (I2SR << reg_shift));
  177. if (sr & I2SR_IAL) {
  178. if (quirk)
  179. writeb(sr | I2SR_IAL, base +
  180. (I2SR << reg_shift));
  181. else
  182. writeb(sr & ~I2SR_IAL, base +
  183. (I2SR << reg_shift));
  184. printf("%s: Arbitration lost sr=%x cr=%x state=%x\n",
  185. __func__, sr, readb(base + (I2CR << reg_shift)),
  186. state);
  187. return -ERESTART;
  188. }
  189. if ((sr & (state >> 8)) == (unsigned char)state)
  190. return sr;
  191. WATCHDOG_RESET();
  192. elapsed = get_timer(start_time);
  193. if (elapsed > (CONFIG_SYS_HZ / 10)) /* .1 seconds */
  194. break;
  195. }
  196. printf("%s: failed sr=%x cr=%x state=%x\n", __func__,
  197. sr, readb(base + (I2CR << reg_shift)), state);
  198. return -ETIMEDOUT;
  199. }
  200. static int tx_byte(struct mxc_i2c_bus *i2c_bus, u8 byte)
  201. {
  202. int ret;
  203. int reg_shift = i2c_bus->driver_data & I2C_QUIRK_FLAG ?
  204. VF610_I2C_REGSHIFT : IMX_I2C_REGSHIFT;
  205. ulong base = i2c_bus->base;
  206. writeb(I2SR_IIF_CLEAR, base + (I2SR << reg_shift));
  207. writeb(byte, base + (I2DR << reg_shift));
  208. ret = wait_for_sr_state(i2c_bus, ST_IIF);
  209. if (ret < 0)
  210. return ret;
  211. if (ret & I2SR_RX_NO_AK)
  212. return -ENODEV;
  213. return 0;
  214. }
  215. /*
  216. * Stub implementations for outer i2c slave operations.
  217. */
  218. void __i2c_force_reset_slave(void)
  219. {
  220. }
  221. void i2c_force_reset_slave(void)
  222. __attribute__((weak, alias("__i2c_force_reset_slave")));
  223. /*
  224. * Stop I2C transaction
  225. */
  226. static void i2c_imx_stop(struct mxc_i2c_bus *i2c_bus)
  227. {
  228. int ret;
  229. int reg_shift = i2c_bus->driver_data & I2C_QUIRK_FLAG ?
  230. VF610_I2C_REGSHIFT : IMX_I2C_REGSHIFT;
  231. ulong base = i2c_bus->base;
  232. unsigned int temp = readb(base + (I2CR << reg_shift));
  233. temp &= ~(I2CR_MSTA | I2CR_MTX);
  234. writeb(temp, base + (I2CR << reg_shift));
  235. ret = wait_for_sr_state(i2c_bus, ST_BUS_IDLE);
  236. if (ret < 0)
  237. printf("%s:trigger stop failed\n", __func__);
  238. }
  239. /*
  240. * Send start signal, chip address and
  241. * write register address
  242. */
  243. static int i2c_init_transfer_(struct mxc_i2c_bus *i2c_bus, u8 chip,
  244. u32 addr, int alen)
  245. {
  246. unsigned int temp;
  247. int ret;
  248. bool quirk = i2c_bus->driver_data & I2C_QUIRK_FLAG ? true : false;
  249. ulong base = i2c_bus->base;
  250. int reg_shift = quirk ? VF610_I2C_REGSHIFT : IMX_I2C_REGSHIFT;
  251. /* Reset i2c slave */
  252. i2c_force_reset_slave();
  253. /* Enable I2C controller */
  254. if (quirk)
  255. ret = readb(base + (I2CR << reg_shift)) & I2CR_IDIS;
  256. else
  257. ret = !(readb(base + (I2CR << reg_shift)) & I2CR_IEN);
  258. if (ret) {
  259. writeb(I2CR_IEN, base + (I2CR << reg_shift));
  260. /* Wait for controller to be stable */
  261. udelay(50);
  262. }
  263. if (readb(base + (IADR << reg_shift)) == (chip << 1))
  264. writeb((chip << 1) ^ 2, base + (IADR << reg_shift));
  265. writeb(I2SR_IIF_CLEAR, base + (I2SR << reg_shift));
  266. ret = wait_for_sr_state(i2c_bus, ST_BUS_IDLE);
  267. if (ret < 0)
  268. return ret;
  269. /* Start I2C transaction */
  270. temp = readb(base + (I2CR << reg_shift));
  271. temp |= I2CR_MSTA;
  272. writeb(temp, base + (I2CR << reg_shift));
  273. ret = wait_for_sr_state(i2c_bus, ST_BUS_BUSY);
  274. if (ret < 0)
  275. return ret;
  276. temp |= I2CR_MTX | I2CR_TX_NO_AK;
  277. writeb(temp, base + (I2CR << reg_shift));
  278. /* write slave address */
  279. ret = tx_byte(i2c_bus, chip << 1);
  280. if (ret < 0)
  281. return ret;
  282. while (alen--) {
  283. ret = tx_byte(i2c_bus, (addr >> (alen * 8)) & 0xff);
  284. if (ret < 0)
  285. return ret;
  286. }
  287. return 0;
  288. }
  289. #ifndef CONFIG_DM_I2C
  290. int i2c_idle_bus(struct mxc_i2c_bus *i2c_bus)
  291. {
  292. if (i2c_bus && i2c_bus->idle_bus_fn)
  293. return i2c_bus->idle_bus_fn(i2c_bus->idle_bus_data);
  294. return 0;
  295. }
  296. #else
  297. /*
  298. * Since pinmux is not supported, implement a weak function here.
  299. * You can implement your i2c_bus_idle in board file. When pinctrl
  300. * is supported, this can be removed.
  301. */
  302. int __i2c_idle_bus(struct mxc_i2c_bus *i2c_bus)
  303. {
  304. return 0;
  305. }
  306. int i2c_idle_bus(struct mxc_i2c_bus *i2c_bus)
  307. __attribute__((weak, alias("__i2c_idle_bus")));
  308. #endif
  309. static int i2c_init_transfer(struct mxc_i2c_bus *i2c_bus, u8 chip,
  310. u32 addr, int alen)
  311. {
  312. int retry;
  313. int ret;
  314. int reg_shift = i2c_bus->driver_data & I2C_QUIRK_FLAG ?
  315. VF610_I2C_REGSHIFT : IMX_I2C_REGSHIFT;
  316. if (!i2c_bus->base)
  317. return -ENODEV;
  318. for (retry = 0; retry < 3; retry++) {
  319. ret = i2c_init_transfer_(i2c_bus, chip, addr, alen);
  320. if (ret >= 0)
  321. return 0;
  322. i2c_imx_stop(i2c_bus);
  323. if (ret == -ENODEV)
  324. return ret;
  325. printf("%s: failed for chip 0x%x retry=%d\n", __func__, chip,
  326. retry);
  327. if (ret != -ERESTART)
  328. /* Disable controller */
  329. writeb(I2CR_IDIS, i2c_bus->base + (I2CR << reg_shift));
  330. udelay(100);
  331. if (i2c_idle_bus(i2c_bus) < 0)
  332. break;
  333. }
  334. printf("%s: give up i2c_regs=0x%lx\n", __func__, i2c_bus->base);
  335. return ret;
  336. }
  337. static int i2c_write_data(struct mxc_i2c_bus *i2c_bus, u8 chip, const u8 *buf,
  338. int len)
  339. {
  340. int i, ret = 0;
  341. debug("i2c_write_data: chip=0x%x, len=0x%x\n", chip, len);
  342. debug("write_data: ");
  343. /* use rc for counter */
  344. for (i = 0; i < len; ++i)
  345. debug(" 0x%02x", buf[i]);
  346. debug("\n");
  347. for (i = 0; i < len; i++) {
  348. ret = tx_byte(i2c_bus, buf[i]);
  349. if (ret < 0) {
  350. debug("i2c_write_data(): rc=%d\n", ret);
  351. break;
  352. }
  353. }
  354. return ret;
  355. }
  356. static int i2c_read_data(struct mxc_i2c_bus *i2c_bus, uchar chip, uchar *buf,
  357. int len)
  358. {
  359. int ret;
  360. unsigned int temp;
  361. int i;
  362. int reg_shift = i2c_bus->driver_data & I2C_QUIRK_FLAG ?
  363. VF610_I2C_REGSHIFT : IMX_I2C_REGSHIFT;
  364. ulong base = i2c_bus->base;
  365. debug("i2c_read_data: chip=0x%x, len=0x%x\n", chip, len);
  366. /* setup bus to read data */
  367. temp = readb(base + (I2CR << reg_shift));
  368. temp &= ~(I2CR_MTX | I2CR_TX_NO_AK);
  369. if (len == 1)
  370. temp |= I2CR_TX_NO_AK;
  371. writeb(temp, base + (I2CR << reg_shift));
  372. writeb(I2SR_IIF_CLEAR, base + (I2SR << reg_shift));
  373. /* dummy read to clear ICF */
  374. readb(base + (I2DR << reg_shift));
  375. /* read data */
  376. for (i = 0; i < len; i++) {
  377. ret = wait_for_sr_state(i2c_bus, ST_IIF);
  378. if (ret < 0) {
  379. debug("i2c_read_data(): ret=%d\n", ret);
  380. i2c_imx_stop(i2c_bus);
  381. return ret;
  382. }
  383. /*
  384. * It must generate STOP before read I2DR to prevent
  385. * controller from generating another clock cycle
  386. */
  387. if (i == (len - 1)) {
  388. i2c_imx_stop(i2c_bus);
  389. } else if (i == (len - 2)) {
  390. temp = readb(base + (I2CR << reg_shift));
  391. temp |= I2CR_TX_NO_AK;
  392. writeb(temp, base + (I2CR << reg_shift));
  393. }
  394. writeb(I2SR_IIF_CLEAR, base + (I2SR << reg_shift));
  395. buf[i] = readb(base + (I2DR << reg_shift));
  396. }
  397. /* reuse ret for counter*/
  398. for (ret = 0; ret < len; ++ret)
  399. debug(" 0x%02x", buf[ret]);
  400. debug("\n");
  401. i2c_imx_stop(i2c_bus);
  402. return 0;
  403. }
  404. #ifndef CONFIG_DM_I2C
  405. /*
  406. * Read data from I2C device
  407. */
  408. static int bus_i2c_read(struct mxc_i2c_bus *i2c_bus, u8 chip, u32 addr,
  409. int alen, u8 *buf, int len)
  410. {
  411. int ret = 0;
  412. u32 temp;
  413. int reg_shift = i2c_bus->driver_data & I2C_QUIRK_FLAG ?
  414. VF610_I2C_REGSHIFT : IMX_I2C_REGSHIFT;
  415. ulong base = i2c_bus->base;
  416. ret = i2c_init_transfer(i2c_bus, chip, addr, alen);
  417. if (ret < 0)
  418. return ret;
  419. temp = readb(base + (I2CR << reg_shift));
  420. temp |= I2CR_RSTA;
  421. writeb(temp, base + (I2CR << reg_shift));
  422. ret = tx_byte(i2c_bus, (chip << 1) | 1);
  423. if (ret < 0) {
  424. i2c_imx_stop(i2c_bus);
  425. return ret;
  426. }
  427. ret = i2c_read_data(i2c_bus, chip, buf, len);
  428. i2c_imx_stop(i2c_bus);
  429. return ret;
  430. }
  431. /*
  432. * Write data to I2C device
  433. */
  434. static int bus_i2c_write(struct mxc_i2c_bus *i2c_bus, u8 chip, u32 addr,
  435. int alen, const u8 *buf, int len)
  436. {
  437. int ret = 0;
  438. ret = i2c_init_transfer(i2c_bus, chip, addr, alen);
  439. if (ret < 0)
  440. return ret;
  441. ret = i2c_write_data(i2c_bus, chip, buf, len);
  442. i2c_imx_stop(i2c_bus);
  443. return ret;
  444. }
  445. #if !defined(I2C2_BASE_ADDR)
  446. #define I2C2_BASE_ADDR 0
  447. #endif
  448. #if !defined(I2C3_BASE_ADDR)
  449. #define I2C3_BASE_ADDR 0
  450. #endif
  451. #if !defined(I2C4_BASE_ADDR)
  452. #define I2C4_BASE_ADDR 0
  453. #endif
  454. static struct mxc_i2c_bus mxc_i2c_buses[] = {
  455. #if defined(CONFIG_LS102XA) || defined(CONFIG_FSL_LSCH3)
  456. { 0, I2C1_BASE_ADDR, I2C_QUIRK_FLAG },
  457. { 1, I2C2_BASE_ADDR, I2C_QUIRK_FLAG },
  458. { 2, I2C3_BASE_ADDR, I2C_QUIRK_FLAG },
  459. { 3, I2C4_BASE_ADDR, I2C_QUIRK_FLAG },
  460. #else
  461. { 0, I2C1_BASE_ADDR, 0 },
  462. { 1, I2C2_BASE_ADDR, 0 },
  463. { 2, I2C3_BASE_ADDR, 0 },
  464. { 3, I2C4_BASE_ADDR, 0 },
  465. #endif
  466. };
  467. struct mxc_i2c_bus *i2c_get_base(struct i2c_adapter *adap)
  468. {
  469. return &mxc_i2c_buses[adap->hwadapnr];
  470. }
  471. static int mxc_i2c_read(struct i2c_adapter *adap, uint8_t chip,
  472. uint addr, int alen, uint8_t *buffer,
  473. int len)
  474. {
  475. return bus_i2c_read(i2c_get_base(adap), chip, addr, alen, buffer, len);
  476. }
  477. static int mxc_i2c_write(struct i2c_adapter *adap, uint8_t chip,
  478. uint addr, int alen, uint8_t *buffer,
  479. int len)
  480. {
  481. return bus_i2c_write(i2c_get_base(adap), chip, addr, alen, buffer, len);
  482. }
  483. /*
  484. * Test if a chip at a given address responds (probe the chip)
  485. */
  486. static int mxc_i2c_probe(struct i2c_adapter *adap, uint8_t chip)
  487. {
  488. return bus_i2c_write(i2c_get_base(adap), chip, 0, 0, NULL, 0);
  489. }
  490. int __enable_i2c_clk(unsigned char enable, unsigned i2c_num)
  491. {
  492. return 1;
  493. }
  494. int enable_i2c_clk(unsigned char enable, unsigned i2c_num)
  495. __attribute__((weak, alias("__enable_i2c_clk")));
  496. void bus_i2c_init(int index, int speed, int unused,
  497. int (*idle_bus_fn)(void *p), void *idle_bus_data)
  498. {
  499. int ret;
  500. if (index >= ARRAY_SIZE(mxc_i2c_buses)) {
  501. debug("Error i2c index\n");
  502. return;
  503. }
  504. mxc_i2c_buses[index].idle_bus_fn = idle_bus_fn;
  505. mxc_i2c_buses[index].idle_bus_data = idle_bus_data;
  506. ret = enable_i2c_clk(1, index);
  507. if (ret < 0) {
  508. debug("I2C-%d clk fail to enable.\n", index);
  509. return;
  510. }
  511. bus_i2c_set_bus_speed(&mxc_i2c_buses[index], speed);
  512. }
  513. /*
  514. * Init I2C Bus
  515. */
  516. static void mxc_i2c_init(struct i2c_adapter *adap, int speed, int slaveaddr)
  517. {
  518. bus_i2c_init(adap->hwadapnr, speed, slaveaddr, NULL, NULL);
  519. }
  520. /*
  521. * Set I2C Speed
  522. */
  523. static u32 mxc_i2c_set_bus_speed(struct i2c_adapter *adap, uint speed)
  524. {
  525. return bus_i2c_set_bus_speed(i2c_get_base(adap), speed);
  526. }
  527. /*
  528. * Register mxc i2c adapters
  529. */
  530. U_BOOT_I2C_ADAP_COMPLETE(mxc0, mxc_i2c_init, mxc_i2c_probe,
  531. mxc_i2c_read, mxc_i2c_write,
  532. mxc_i2c_set_bus_speed,
  533. CONFIG_SYS_MXC_I2C1_SPEED,
  534. CONFIG_SYS_MXC_I2C1_SLAVE, 0)
  535. U_BOOT_I2C_ADAP_COMPLETE(mxc1, mxc_i2c_init, mxc_i2c_probe,
  536. mxc_i2c_read, mxc_i2c_write,
  537. mxc_i2c_set_bus_speed,
  538. CONFIG_SYS_MXC_I2C2_SPEED,
  539. CONFIG_SYS_MXC_I2C2_SLAVE, 1)
  540. #ifdef CONFIG_SYS_I2C_MXC_I2C3
  541. U_BOOT_I2C_ADAP_COMPLETE(mxc2, mxc_i2c_init, mxc_i2c_probe,
  542. mxc_i2c_read, mxc_i2c_write,
  543. mxc_i2c_set_bus_speed,
  544. CONFIG_SYS_MXC_I2C3_SPEED,
  545. CONFIG_SYS_MXC_I2C3_SLAVE, 2)
  546. #endif
  547. #ifdef CONFIG_SYS_I2C_MXC_I2C4
  548. U_BOOT_I2C_ADAP_COMPLETE(mxc3, mxc_i2c_init, mxc_i2c_probe,
  549. mxc_i2c_read, mxc_i2c_write,
  550. mxc_i2c_set_bus_speed,
  551. CONFIG_SYS_MXC_I2C4_SPEED,
  552. CONFIG_SYS_MXC_I2C4_SLAVE, 3)
  553. #endif
  554. #else
  555. static int mxc_i2c_set_bus_speed(struct udevice *bus, unsigned int speed)
  556. {
  557. struct mxc_i2c_bus *i2c_bus = dev_get_priv(bus);
  558. return bus_i2c_set_bus_speed(i2c_bus, speed);
  559. }
  560. static int mxc_i2c_probe(struct udevice *bus)
  561. {
  562. struct mxc_i2c_bus *i2c_bus = dev_get_priv(bus);
  563. fdt_addr_t addr;
  564. int ret;
  565. i2c_bus->driver_data = dev_get_driver_data(bus);
  566. addr = dev_get_addr(bus);
  567. if (addr == FDT_ADDR_T_NONE)
  568. return -ENODEV;
  569. i2c_bus->base = addr;
  570. i2c_bus->index = bus->seq;
  571. /* Enable clk */
  572. ret = enable_i2c_clk(1, bus->seq);
  573. if (ret < 0)
  574. return ret;
  575. ret = i2c_idle_bus(i2c_bus);
  576. if (ret < 0) {
  577. /* Disable clk */
  578. enable_i2c_clk(0, bus->seq);
  579. return ret;
  580. }
  581. /*
  582. * Pinmux settings are in board file now, until pinmux is supported,
  583. * we can set pinmux here in probe function.
  584. */
  585. debug("i2c : controller bus %d at %lu , speed %d: ",
  586. bus->seq, i2c_bus->base,
  587. i2c_bus->speed);
  588. return 0;
  589. }
  590. static int mxc_i2c_probe_chip(struct udevice *bus, u32 chip_addr,
  591. u32 chip_flags)
  592. {
  593. int ret;
  594. struct mxc_i2c_bus *i2c_bus = dev_get_priv(bus);
  595. ret = i2c_init_transfer(i2c_bus, chip_addr, 0, 0);
  596. if (ret < 0) {
  597. debug("%s failed, ret = %d\n", __func__, ret);
  598. return ret;
  599. }
  600. i2c_imx_stop(i2c_bus);
  601. return 0;
  602. }
  603. static int mxc_i2c_xfer(struct udevice *bus, struct i2c_msg *msg, int nmsgs)
  604. {
  605. struct mxc_i2c_bus *i2c_bus = dev_get_priv(bus);
  606. int ret = 0;
  607. ulong base = i2c_bus->base;
  608. int reg_shift = i2c_bus->driver_data & I2C_QUIRK_FLAG ?
  609. VF610_I2C_REGSHIFT : IMX_I2C_REGSHIFT;
  610. /*
  611. * Here the 3rd parameter addr and the 4th one alen are set to 0,
  612. * because here we only want to send out chip address. The register
  613. * address is wrapped in msg.
  614. */
  615. ret = i2c_init_transfer(i2c_bus, msg->addr, 0, 0);
  616. if (ret < 0) {
  617. debug("i2c_init_transfer error: %d\n", ret);
  618. return ret;
  619. }
  620. for (; nmsgs > 0; nmsgs--, msg++) {
  621. bool next_is_read = nmsgs > 1 && (msg[1].flags & I2C_M_RD);
  622. debug("i2c_xfer: chip=0x%x, len=0x%x\n", msg->addr, msg->len);
  623. if (msg->flags & I2C_M_RD)
  624. ret = i2c_read_data(i2c_bus, msg->addr, msg->buf,
  625. msg->len);
  626. else {
  627. ret = i2c_write_data(i2c_bus, msg->addr, msg->buf,
  628. msg->len);
  629. if (ret)
  630. break;
  631. if (next_is_read) {
  632. /* Reuse ret */
  633. ret = readb(base + (I2CR << reg_shift));
  634. ret |= I2CR_RSTA;
  635. writeb(ret, base + (I2CR << reg_shift));
  636. ret = tx_byte(i2c_bus, (msg->addr << 1) | 1);
  637. if (ret < 0) {
  638. i2c_imx_stop(i2c_bus);
  639. break;
  640. }
  641. }
  642. }
  643. }
  644. if (ret)
  645. debug("i2c_write: error sending\n");
  646. i2c_imx_stop(i2c_bus);
  647. return ret;
  648. }
  649. static const struct dm_i2c_ops mxc_i2c_ops = {
  650. .xfer = mxc_i2c_xfer,
  651. .probe_chip = mxc_i2c_probe_chip,
  652. .set_bus_speed = mxc_i2c_set_bus_speed,
  653. };
  654. static const struct udevice_id mxc_i2c_ids[] = {
  655. { .compatible = "fsl,imx21-i2c", },
  656. { .compatible = "fsl,vf610-i2c", .data = I2C_QUIRK_FLAG, },
  657. {}
  658. };
  659. U_BOOT_DRIVER(i2c_mxc) = {
  660. .name = "i2c_mxc",
  661. .id = UCLASS_I2C,
  662. .of_match = mxc_i2c_ids,
  663. .probe = mxc_i2c_probe,
  664. .priv_auto_alloc_size = sizeof(struct mxc_i2c_bus),
  665. .ops = &mxc_i2c_ops,
  666. };
  667. #endif