mxc_i2c.c 22 KB

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