mxc_i2c.c 22 KB

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