mxc_i2c.c 23 KB

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