mxc_i2c.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779
  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. /* Store divider value */
  156. writeb(idx, base + (IFDR << reg_shift));
  157. /* Reset module */
  158. writeb(I2CR_IDIS, base + (I2CR << reg_shift));
  159. writeb(0, base + (I2SR << reg_shift));
  160. return 0;
  161. }
  162. #define ST_BUS_IDLE (0 | (I2SR_IBB << 8))
  163. #define ST_BUS_BUSY (I2SR_IBB | (I2SR_IBB << 8))
  164. #define ST_IIF (I2SR_IIF | (I2SR_IIF << 8))
  165. static int wait_for_sr_state(struct mxc_i2c_bus *i2c_bus, unsigned state)
  166. {
  167. unsigned sr;
  168. ulong elapsed;
  169. bool quirk = i2c_bus->driver_data & I2C_QUIRK_FLAG ? true : false;
  170. int reg_shift = quirk ? VF610_I2C_REGSHIFT : IMX_I2C_REGSHIFT;
  171. ulong base = i2c_bus->base;
  172. ulong start_time = get_timer(0);
  173. for (;;) {
  174. sr = readb(base + (I2SR << reg_shift));
  175. if (sr & I2SR_IAL) {
  176. if (quirk)
  177. writeb(sr | I2SR_IAL, base +
  178. (I2SR << reg_shift));
  179. else
  180. writeb(sr & ~I2SR_IAL, base +
  181. (I2SR << reg_shift));
  182. printf("%s: Arbitration lost sr=%x cr=%x state=%x\n",
  183. __func__, sr, readb(base + (I2CR << reg_shift)),
  184. state);
  185. return -ERESTART;
  186. }
  187. if ((sr & (state >> 8)) == (unsigned char)state)
  188. return sr;
  189. WATCHDOG_RESET();
  190. elapsed = get_timer(start_time);
  191. if (elapsed > (CONFIG_SYS_HZ / 10)) /* .1 seconds */
  192. break;
  193. }
  194. printf("%s: failed sr=%x cr=%x state=%x\n", __func__,
  195. sr, readb(base + (I2CR << reg_shift)), state);
  196. return -ETIMEDOUT;
  197. }
  198. static int tx_byte(struct mxc_i2c_bus *i2c_bus, u8 byte)
  199. {
  200. int ret;
  201. int reg_shift = i2c_bus->driver_data & I2C_QUIRK_FLAG ?
  202. VF610_I2C_REGSHIFT : IMX_I2C_REGSHIFT;
  203. ulong base = i2c_bus->base;
  204. writeb(I2SR_IIF_CLEAR, base + (I2SR << reg_shift));
  205. writeb(byte, base + (I2DR << reg_shift));
  206. ret = wait_for_sr_state(i2c_bus, ST_IIF);
  207. if (ret < 0)
  208. return ret;
  209. if (ret & I2SR_RX_NO_AK)
  210. return -ENODEV;
  211. return 0;
  212. }
  213. /*
  214. * Stub implementations for outer i2c slave operations.
  215. */
  216. void __i2c_force_reset_slave(void)
  217. {
  218. }
  219. void i2c_force_reset_slave(void)
  220. __attribute__((weak, alias("__i2c_force_reset_slave")));
  221. /*
  222. * Stop I2C transaction
  223. */
  224. static void i2c_imx_stop(struct mxc_i2c_bus *i2c_bus)
  225. {
  226. int ret;
  227. int reg_shift = i2c_bus->driver_data & I2C_QUIRK_FLAG ?
  228. VF610_I2C_REGSHIFT : IMX_I2C_REGSHIFT;
  229. ulong base = i2c_bus->base;
  230. unsigned int temp = readb(base + (I2CR << reg_shift));
  231. temp &= ~(I2CR_MSTA | I2CR_MTX);
  232. writeb(temp, base + (I2CR << reg_shift));
  233. ret = wait_for_sr_state(i2c_bus, ST_BUS_IDLE);
  234. if (ret < 0)
  235. printf("%s:trigger stop failed\n", __func__);
  236. }
  237. /*
  238. * Send start signal, chip address and
  239. * write register address
  240. */
  241. static int i2c_init_transfer_(struct mxc_i2c_bus *i2c_bus, u8 chip,
  242. u32 addr, int alen)
  243. {
  244. unsigned int temp;
  245. int ret;
  246. bool quirk = i2c_bus->driver_data & I2C_QUIRK_FLAG ? true : false;
  247. ulong base = i2c_bus->base;
  248. int reg_shift = quirk ? VF610_I2C_REGSHIFT : IMX_I2C_REGSHIFT;
  249. /* Reset i2c slave */
  250. i2c_force_reset_slave();
  251. /* Enable I2C controller */
  252. if (quirk)
  253. ret = readb(base + (I2CR << reg_shift)) & I2CR_IDIS;
  254. else
  255. ret = !(readb(base + (I2CR << reg_shift)) & I2CR_IEN);
  256. if (ret) {
  257. writeb(I2CR_IEN, base + (I2CR << reg_shift));
  258. /* Wait for controller to be stable */
  259. udelay(50);
  260. }
  261. if (readb(base + (IADR << reg_shift)) == (chip << 1))
  262. writeb((chip << 1) ^ 2, base + (IADR << reg_shift));
  263. writeb(I2SR_IIF_CLEAR, base + (I2SR << reg_shift));
  264. ret = wait_for_sr_state(i2c_bus, ST_BUS_IDLE);
  265. if (ret < 0)
  266. return ret;
  267. /* Start I2C transaction */
  268. temp = readb(base + (I2CR << reg_shift));
  269. temp |= I2CR_MSTA;
  270. writeb(temp, base + (I2CR << reg_shift));
  271. ret = wait_for_sr_state(i2c_bus, ST_BUS_BUSY);
  272. if (ret < 0)
  273. return ret;
  274. temp |= I2CR_MTX | I2CR_TX_NO_AK;
  275. writeb(temp, base + (I2CR << reg_shift));
  276. /* write slave address */
  277. ret = tx_byte(i2c_bus, chip << 1);
  278. if (ret < 0)
  279. return ret;
  280. while (alen--) {
  281. ret = tx_byte(i2c_bus, (addr >> (alen * 8)) & 0xff);
  282. if (ret < 0)
  283. return ret;
  284. }
  285. return 0;
  286. }
  287. #ifndef CONFIG_DM_I2C
  288. int i2c_idle_bus(struct mxc_i2c_bus *i2c_bus)
  289. {
  290. if (i2c_bus && i2c_bus->idle_bus_fn)
  291. return i2c_bus->idle_bus_fn(i2c_bus->idle_bus_data);
  292. return 0;
  293. }
  294. #else
  295. /*
  296. * Since pinmux is not supported, implement a weak function here.
  297. * You can implement your i2c_bus_idle in board file. When pinctrl
  298. * is supported, this can be removed.
  299. */
  300. int __i2c_idle_bus(struct mxc_i2c_bus *i2c_bus)
  301. {
  302. return 0;
  303. }
  304. int i2c_idle_bus(struct mxc_i2c_bus *i2c_bus)
  305. __attribute__((weak, alias("__i2c_idle_bus")));
  306. #endif
  307. static int i2c_init_transfer(struct mxc_i2c_bus *i2c_bus, u8 chip,
  308. u32 addr, int alen)
  309. {
  310. int retry;
  311. int ret;
  312. int reg_shift = i2c_bus->driver_data & I2C_QUIRK_FLAG ?
  313. VF610_I2C_REGSHIFT : IMX_I2C_REGSHIFT;
  314. for (retry = 0; retry < 3; retry++) {
  315. ret = i2c_init_transfer_(i2c_bus, chip, addr, alen);
  316. if (ret >= 0)
  317. return 0;
  318. i2c_imx_stop(i2c_bus);
  319. if (ret == -ENODEV)
  320. return ret;
  321. printf("%s: failed for chip 0x%x retry=%d\n", __func__, chip,
  322. retry);
  323. if (ret != -ERESTART)
  324. /* Disable controller */
  325. writeb(I2CR_IDIS, i2c_bus->base + (I2CR << reg_shift));
  326. udelay(100);
  327. if (i2c_idle_bus(i2c_bus) < 0)
  328. break;
  329. }
  330. printf("%s: give up i2c_regs=0x%lx\n", __func__, i2c_bus->base);
  331. return ret;
  332. }
  333. static int i2c_write_data(struct mxc_i2c_bus *i2c_bus, u8 chip, const u8 *buf,
  334. int len)
  335. {
  336. int i, ret = 0;
  337. debug("i2c_write_data: chip=0x%x, len=0x%x\n", chip, len);
  338. debug("write_data: ");
  339. /* use rc for counter */
  340. for (i = 0; i < len; ++i)
  341. debug(" 0x%02x", buf[i]);
  342. debug("\n");
  343. for (i = 0; i < len; i++) {
  344. ret = tx_byte(i2c_bus, buf[i]);
  345. if (ret < 0) {
  346. debug("i2c_write_data(): rc=%d\n", ret);
  347. break;
  348. }
  349. }
  350. return ret;
  351. }
  352. static int i2c_read_data(struct mxc_i2c_bus *i2c_bus, uchar chip, uchar *buf,
  353. int len)
  354. {
  355. int ret;
  356. unsigned int temp;
  357. int i;
  358. int reg_shift = i2c_bus->driver_data & I2C_QUIRK_FLAG ?
  359. VF610_I2C_REGSHIFT : IMX_I2C_REGSHIFT;
  360. ulong base = i2c_bus->base;
  361. debug("i2c_read_data: chip=0x%x, len=0x%x\n", chip, len);
  362. /* setup bus to read data */
  363. temp = readb(base + (I2CR << reg_shift));
  364. temp &= ~(I2CR_MTX | I2CR_TX_NO_AK);
  365. if (len == 1)
  366. temp |= I2CR_TX_NO_AK;
  367. writeb(temp, base + (I2CR << reg_shift));
  368. writeb(I2SR_IIF_CLEAR, base + (I2SR << reg_shift));
  369. /* dummy read to clear ICF */
  370. readb(base + (I2DR << reg_shift));
  371. /* read data */
  372. for (i = 0; i < len; i++) {
  373. ret = wait_for_sr_state(i2c_bus, ST_IIF);
  374. if (ret < 0) {
  375. debug("i2c_read_data(): ret=%d\n", ret);
  376. i2c_imx_stop(i2c_bus);
  377. return ret;
  378. }
  379. /*
  380. * It must generate STOP before read I2DR to prevent
  381. * controller from generating another clock cycle
  382. */
  383. if (i == (len - 1)) {
  384. i2c_imx_stop(i2c_bus);
  385. } else if (i == (len - 2)) {
  386. temp = readb(base + (I2CR << reg_shift));
  387. temp |= I2CR_TX_NO_AK;
  388. writeb(temp, base + (I2CR << reg_shift));
  389. }
  390. writeb(I2SR_IIF_CLEAR, base + (I2SR << reg_shift));
  391. buf[i] = readb(base + (I2DR << reg_shift));
  392. }
  393. /* reuse ret for counter*/
  394. for (ret = 0; ret < len; ++ret)
  395. debug(" 0x%02x", buf[ret]);
  396. debug("\n");
  397. i2c_imx_stop(i2c_bus);
  398. return 0;
  399. }
  400. #ifndef CONFIG_DM_I2C
  401. /*
  402. * Read data from I2C device
  403. */
  404. static int bus_i2c_read(struct mxc_i2c_bus *i2c_bus, u8 chip, u32 addr,
  405. int alen, u8 *buf, int len)
  406. {
  407. int ret = 0;
  408. u32 temp;
  409. int reg_shift = i2c_bus->driver_data & I2C_QUIRK_FLAG ?
  410. VF610_I2C_REGSHIFT : IMX_I2C_REGSHIFT;
  411. ulong base = i2c_bus->base;
  412. ret = i2c_init_transfer(i2c_bus, chip, addr, alen);
  413. if (ret < 0)
  414. return ret;
  415. temp = readb(base + (I2CR << reg_shift));
  416. temp |= I2CR_RSTA;
  417. writeb(temp, base + (I2CR << reg_shift));
  418. ret = tx_byte(i2c_bus, (chip << 1) | 1);
  419. if (ret < 0) {
  420. i2c_imx_stop(i2c_bus);
  421. return ret;
  422. }
  423. ret = i2c_read_data(i2c_bus, chip, buf, len);
  424. i2c_imx_stop(i2c_bus);
  425. return ret;
  426. }
  427. /*
  428. * Write data to I2C device
  429. */
  430. static int bus_i2c_write(struct mxc_i2c_bus *i2c_bus, u8 chip, u32 addr,
  431. int alen, const u8 *buf, int len)
  432. {
  433. int ret = 0;
  434. ret = i2c_init_transfer(i2c_bus, chip, addr, alen);
  435. if (ret < 0)
  436. return ret;
  437. ret = i2c_write_data(i2c_bus, chip, buf, len);
  438. i2c_imx_stop(i2c_bus);
  439. return ret;
  440. }
  441. static struct mxc_i2c_bus mxc_i2c_buses[] = {
  442. #if defined(CONFIG_MX25)
  443. { 0, IMX_I2C_BASE },
  444. { 1, IMX_I2C2_BASE },
  445. { 2, IMX_I2C3_BASE },
  446. #elif defined(CONFIG_MX27)
  447. { 0, IMX_I2C1_BASE },
  448. { 1, IMX_I2C2_BASE },
  449. #elif defined(CONFIG_MX31) || defined(CONFIG_MX35) || \
  450. defined(CONFIG_MX51) || defined(CONFIG_MX53) || \
  451. defined(CONFIG_MX6)
  452. { 0, I2C1_BASE_ADDR },
  453. { 1, I2C2_BASE_ADDR },
  454. { 2, I2C3_BASE_ADDR },
  455. #if defined(CONFIG_MX6DL)
  456. { 3, I2C4_BASE_ADDR },
  457. #endif
  458. #elif defined(CONFIG_LS102XA)
  459. { 0, I2C1_BASE_ADDR, I2C_QUIRK_FLAG },
  460. { 1, I2C2_BASE_ADDR, I2C_QUIRK_FLAG },
  461. { 2, I2C3_BASE_ADDR, I2C_QUIRK_FLAG },
  462. #elif defined(CONFIG_VF610)
  463. { 0, I2C0_BASE_ADDR, I2C_QUIRK_FLAG },
  464. #elif defined(CONFIG_FSL_LSCH3)
  465. { 0, I2C1_BASE_ADDR, I2C_QUIRK_FLAG },
  466. { 1, I2C2_BASE_ADDR, I2C_QUIRK_FLAG },
  467. { 2, I2C3_BASE_ADDR, I2C_QUIRK_FLAG },
  468. { 3, I2C4_BASE_ADDR, I2C_QUIRK_FLAG },
  469. #else
  470. #error "architecture not supported"
  471. #endif
  472. { }
  473. };
  474. struct mxc_i2c_bus *i2c_get_base(struct i2c_adapter *adap)
  475. {
  476. return &mxc_i2c_buses[adap->hwadapnr];
  477. }
  478. static int mxc_i2c_read(struct i2c_adapter *adap, uint8_t chip,
  479. uint addr, int alen, uint8_t *buffer,
  480. int len)
  481. {
  482. return bus_i2c_read(i2c_get_base(adap), chip, addr, alen, buffer, len);
  483. }
  484. static int mxc_i2c_write(struct i2c_adapter *adap, uint8_t chip,
  485. uint addr, int alen, uint8_t *buffer,
  486. int len)
  487. {
  488. return bus_i2c_write(i2c_get_base(adap), chip, addr, alen, buffer, len);
  489. }
  490. /*
  491. * Test if a chip at a given address responds (probe the chip)
  492. */
  493. static int mxc_i2c_probe(struct i2c_adapter *adap, uint8_t chip)
  494. {
  495. return bus_i2c_write(i2c_get_base(adap), chip, 0, 0, NULL, 0);
  496. }
  497. int __enable_i2c_clk(unsigned char enable, unsigned i2c_num)
  498. {
  499. return 1;
  500. }
  501. int enable_i2c_clk(unsigned char enable, unsigned i2c_num)
  502. __attribute__((weak, alias("__enable_i2c_clk")));
  503. void bus_i2c_init(int index, int speed, int unused,
  504. int (*idle_bus_fn)(void *p), void *idle_bus_data)
  505. {
  506. int ret;
  507. if (index >= ARRAY_SIZE(mxc_i2c_buses)) {
  508. debug("Error i2c index\n");
  509. return;
  510. }
  511. mxc_i2c_buses[index].idle_bus_fn = idle_bus_fn;
  512. mxc_i2c_buses[index].idle_bus_data = idle_bus_data;
  513. ret = enable_i2c_clk(1, index);
  514. if (ret < 0) {
  515. debug("I2C-%d clk fail to enable.\n", index);
  516. return;
  517. }
  518. bus_i2c_set_bus_speed(&mxc_i2c_buses[index], speed);
  519. }
  520. /*
  521. * Init I2C Bus
  522. */
  523. static void mxc_i2c_init(struct i2c_adapter *adap, int speed, int slaveaddr)
  524. {
  525. bus_i2c_init(adap->hwadapnr, speed, slaveaddr, NULL, NULL);
  526. }
  527. /*
  528. * Set I2C Speed
  529. */
  530. static u32 mxc_i2c_set_bus_speed(struct i2c_adapter *adap, uint speed)
  531. {
  532. return bus_i2c_set_bus_speed(i2c_get_base(adap), speed);
  533. }
  534. /*
  535. * Register mxc i2c adapters
  536. */
  537. U_BOOT_I2C_ADAP_COMPLETE(mxc0, mxc_i2c_init, mxc_i2c_probe,
  538. mxc_i2c_read, mxc_i2c_write,
  539. mxc_i2c_set_bus_speed,
  540. CONFIG_SYS_MXC_I2C1_SPEED,
  541. CONFIG_SYS_MXC_I2C1_SLAVE, 0)
  542. U_BOOT_I2C_ADAP_COMPLETE(mxc1, mxc_i2c_init, mxc_i2c_probe,
  543. mxc_i2c_read, mxc_i2c_write,
  544. mxc_i2c_set_bus_speed,
  545. CONFIG_SYS_MXC_I2C2_SPEED,
  546. CONFIG_SYS_MXC_I2C2_SLAVE, 1)
  547. #ifdef CONFIG_SYS_I2C_MXC_I2C3
  548. U_BOOT_I2C_ADAP_COMPLETE(mxc2, mxc_i2c_init, mxc_i2c_probe,
  549. mxc_i2c_read, mxc_i2c_write,
  550. mxc_i2c_set_bus_speed,
  551. CONFIG_SYS_MXC_I2C3_SPEED,
  552. CONFIG_SYS_MXC_I2C3_SLAVE, 2)
  553. #endif
  554. #ifdef CONFIG_SYS_I2C_MXC_I2C4
  555. U_BOOT_I2C_ADAP_COMPLETE(mxc3, mxc_i2c_init, mxc_i2c_probe,
  556. mxc_i2c_read, mxc_i2c_write,
  557. mxc_i2c_set_bus_speed,
  558. CONFIG_SYS_MXC_I2C4_SPEED,
  559. CONFIG_SYS_MXC_I2C4_SLAVE, 3)
  560. #endif
  561. #else
  562. static int mxc_i2c_set_bus_speed(struct udevice *bus, unsigned int speed)
  563. {
  564. struct mxc_i2c_bus *i2c_bus = dev_get_priv(bus);
  565. return bus_i2c_set_bus_speed(i2c_bus, speed);
  566. }
  567. static int mxc_i2c_probe(struct udevice *bus)
  568. {
  569. struct mxc_i2c_bus *i2c_bus = dev_get_priv(bus);
  570. fdt_addr_t addr;
  571. int ret;
  572. i2c_bus->driver_data = dev_get_driver_data(bus);
  573. addr = dev_get_addr(bus);
  574. if (addr == FDT_ADDR_T_NONE)
  575. return -ENODEV;
  576. i2c_bus->base = addr;
  577. i2c_bus->index = bus->seq;
  578. /* Enable clk */
  579. ret = enable_i2c_clk(1, bus->seq);
  580. if (ret < 0)
  581. return ret;
  582. ret = i2c_idle_bus(i2c_bus);
  583. if (ret < 0) {
  584. /* Disable clk */
  585. enable_i2c_clk(0, bus->seq);
  586. return ret;
  587. }
  588. /*
  589. * Pinmux settings are in board file now, until pinmux is supported,
  590. * we can set pinmux here in probe function.
  591. */
  592. debug("i2c : controller bus %d at %lu , speed %d: ",
  593. bus->seq, i2c_bus->base,
  594. i2c_bus->speed);
  595. return 0;
  596. }
  597. static int mxc_i2c_probe_chip(struct udevice *bus, u32 chip_addr,
  598. u32 chip_flags)
  599. {
  600. int ret;
  601. struct mxc_i2c_bus *i2c_bus = dev_get_priv(bus);
  602. ret = i2c_init_transfer(i2c_bus, chip_addr, 0, 0);
  603. if (ret < 0) {
  604. debug("%s failed, ret = %d\n", __func__, ret);
  605. return ret;
  606. }
  607. i2c_imx_stop(i2c_bus);
  608. return 0;
  609. }
  610. static int mxc_i2c_xfer(struct udevice *bus, struct i2c_msg *msg, int nmsgs)
  611. {
  612. struct mxc_i2c_bus *i2c_bus = dev_get_priv(bus);
  613. int ret = 0;
  614. ulong base = i2c_bus->base;
  615. int reg_shift = i2c_bus->driver_data & I2C_QUIRK_FLAG ?
  616. VF610_I2C_REGSHIFT : IMX_I2C_REGSHIFT;
  617. /*
  618. * Here the 3rd parameter addr and the 4th one alen are set to 0,
  619. * because here we only want to send out chip address. The register
  620. * address is wrapped in msg.
  621. */
  622. ret = i2c_init_transfer(i2c_bus, msg->addr, 0, 0);
  623. if (ret < 0) {
  624. debug("i2c_init_transfer error: %d\n", ret);
  625. return ret;
  626. }
  627. for (; nmsgs > 0; nmsgs--, msg++) {
  628. bool next_is_read = nmsgs > 1 && (msg[1].flags & I2C_M_RD);
  629. debug("i2c_xfer: chip=0x%x, len=0x%x\n", msg->addr, msg->len);
  630. if (msg->flags & I2C_M_RD)
  631. ret = i2c_read_data(i2c_bus, msg->addr, msg->buf,
  632. msg->len);
  633. else {
  634. ret = i2c_write_data(i2c_bus, msg->addr, msg->buf,
  635. msg->len);
  636. if (ret)
  637. break;
  638. if (next_is_read) {
  639. /* Reuse ret */
  640. ret = readb(base + (I2CR << reg_shift));
  641. ret |= I2CR_RSTA;
  642. writeb(ret, base + (I2CR << reg_shift));
  643. ret = tx_byte(i2c_bus, (msg->addr << 1) | 1);
  644. if (ret < 0) {
  645. i2c_imx_stop(i2c_bus);
  646. break;
  647. }
  648. }
  649. }
  650. }
  651. if (ret)
  652. debug("i2c_write: error sending\n");
  653. i2c_imx_stop(i2c_bus);
  654. return ret;
  655. }
  656. static const struct dm_i2c_ops mxc_i2c_ops = {
  657. .xfer = mxc_i2c_xfer,
  658. .probe_chip = mxc_i2c_probe_chip,
  659. .set_bus_speed = mxc_i2c_set_bus_speed,
  660. };
  661. static const struct udevice_id mxc_i2c_ids[] = {
  662. { .compatible = "fsl,imx21-i2c", },
  663. { .compatible = "fsl,vf610-i2c", .data = I2C_QUIRK_FLAG, },
  664. {}
  665. };
  666. U_BOOT_DRIVER(i2c_mxc) = {
  667. .name = "i2c_mxc",
  668. .id = UCLASS_I2C,
  669. .of_match = mxc_i2c_ids,
  670. .probe = mxc_i2c_probe,
  671. .priv_auto_alloc_size = sizeof(struct mxc_i2c_bus),
  672. .ops = &mxc_i2c_ops,
  673. };
  674. #endif