mxc_i2c.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899
  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. /* write slave address */
  283. ret = tx_byte(i2c_bus, chip << 1);
  284. if (ret < 0)
  285. return ret;
  286. while (alen--) {
  287. ret = tx_byte(i2c_bus, (addr >> (alen * 8)) & 0xff);
  288. if (ret < 0)
  289. return ret;
  290. }
  291. return 0;
  292. }
  293. #ifndef CONFIG_DM_I2C
  294. int i2c_idle_bus(struct mxc_i2c_bus *i2c_bus)
  295. {
  296. if (i2c_bus && i2c_bus->idle_bus_fn)
  297. return i2c_bus->idle_bus_fn(i2c_bus->idle_bus_data);
  298. return 0;
  299. }
  300. #else
  301. /*
  302. * See Linux Documentation/devicetree/bindings/i2c/i2c-imx.txt
  303. * "
  304. * scl-gpios: specify the gpio related to SCL pin
  305. * sda-gpios: specify the gpio related to SDA pin
  306. * add pinctrl to configure i2c pins to gpio function for i2c
  307. * bus recovery, call it "gpio" state
  308. * "
  309. *
  310. * The i2c_idle_bus is an implementation following Linux Kernel.
  311. */
  312. int i2c_idle_bus(struct mxc_i2c_bus *i2c_bus)
  313. {
  314. struct udevice *bus = i2c_bus->bus;
  315. struct gpio_desc *scl_gpio = &i2c_bus->scl_gpio;
  316. struct gpio_desc *sda_gpio = &i2c_bus->sda_gpio;
  317. int sda, scl;
  318. int i, ret = 0;
  319. ulong elapsed, start_time;
  320. if (pinctrl_select_state(bus, "gpio")) {
  321. dev_dbg(bus, "Can not to switch to use gpio pinmux\n");
  322. /*
  323. * GPIO pinctrl for i2c force idle is not a must,
  324. * but it is strongly recommended to be used.
  325. * Because it can help you to recover from bad
  326. * i2c bus state. Do not return failure, because
  327. * it is not a must.
  328. */
  329. return 0;
  330. }
  331. dm_gpio_set_dir_flags(scl_gpio, GPIOD_IS_IN);
  332. dm_gpio_set_dir_flags(sda_gpio, GPIOD_IS_IN);
  333. scl = dm_gpio_get_value(scl_gpio);
  334. sda = dm_gpio_get_value(sda_gpio);
  335. if ((sda & scl) == 1)
  336. goto exit; /* Bus is idle already */
  337. /* Send high and low on the SCL line */
  338. for (i = 0; i < 9; i++) {
  339. dm_gpio_set_dir_flags(scl_gpio, GPIOD_IS_OUT);
  340. dm_gpio_set_value(scl_gpio, 0);
  341. udelay(50);
  342. dm_gpio_set_dir_flags(scl_gpio, GPIOD_IS_IN);
  343. udelay(50);
  344. }
  345. start_time = get_timer(0);
  346. for (;;) {
  347. dm_gpio_set_dir_flags(scl_gpio, GPIOD_IS_IN);
  348. dm_gpio_set_dir_flags(sda_gpio, GPIOD_IS_IN);
  349. scl = dm_gpio_get_value(scl_gpio);
  350. sda = dm_gpio_get_value(sda_gpio);
  351. if ((sda & scl) == 1)
  352. break;
  353. WATCHDOG_RESET();
  354. elapsed = get_timer(start_time);
  355. if (elapsed > (CONFIG_SYS_HZ / 5)) { /* .2 seconds */
  356. ret = -EBUSY;
  357. printf("%s: failed to clear bus, sda=%d scl=%d\n", __func__, sda, scl);
  358. break;
  359. }
  360. }
  361. exit:
  362. pinctrl_select_state(bus, "default");
  363. return ret;
  364. }
  365. #endif
  366. static int i2c_init_transfer(struct mxc_i2c_bus *i2c_bus, u8 chip,
  367. u32 addr, int alen)
  368. {
  369. int retry;
  370. int ret;
  371. int reg_shift = i2c_bus->driver_data & I2C_QUIRK_FLAG ?
  372. VF610_I2C_REGSHIFT : IMX_I2C_REGSHIFT;
  373. if (!i2c_bus->base)
  374. return -EINVAL;
  375. for (retry = 0; retry < 3; retry++) {
  376. ret = i2c_init_transfer_(i2c_bus, chip, addr, alen);
  377. if (ret >= 0)
  378. return 0;
  379. i2c_imx_stop(i2c_bus);
  380. if (ret == -EREMOTEIO)
  381. return ret;
  382. printf("%s: failed for chip 0x%x retry=%d\n", __func__, chip,
  383. retry);
  384. if (ret != -ERESTART)
  385. /* Disable controller */
  386. writeb(I2CR_IDIS, i2c_bus->base + (I2CR << reg_shift));
  387. udelay(100);
  388. if (i2c_idle_bus(i2c_bus) < 0)
  389. break;
  390. }
  391. printf("%s: give up i2c_regs=0x%lx\n", __func__, i2c_bus->base);
  392. return ret;
  393. }
  394. static int i2c_write_data(struct mxc_i2c_bus *i2c_bus, u8 chip, const u8 *buf,
  395. int len)
  396. {
  397. int i, ret = 0;
  398. debug("i2c_write_data: chip=0x%x, len=0x%x\n", chip, len);
  399. debug("write_data: ");
  400. /* use rc for counter */
  401. for (i = 0; i < len; ++i)
  402. debug(" 0x%02x", buf[i]);
  403. debug("\n");
  404. for (i = 0; i < len; i++) {
  405. ret = tx_byte(i2c_bus, buf[i]);
  406. if (ret < 0) {
  407. debug("i2c_write_data(): rc=%d\n", ret);
  408. break;
  409. }
  410. }
  411. return ret;
  412. }
  413. static int i2c_read_data(struct mxc_i2c_bus *i2c_bus, uchar chip, uchar *buf,
  414. int len)
  415. {
  416. int ret;
  417. unsigned int temp;
  418. int i;
  419. int reg_shift = i2c_bus->driver_data & I2C_QUIRK_FLAG ?
  420. VF610_I2C_REGSHIFT : IMX_I2C_REGSHIFT;
  421. ulong base = i2c_bus->base;
  422. debug("i2c_read_data: chip=0x%x, len=0x%x\n", chip, len);
  423. /* setup bus to read data */
  424. temp = readb(base + (I2CR << reg_shift));
  425. temp &= ~(I2CR_MTX | I2CR_TX_NO_AK);
  426. if (len == 1)
  427. temp |= I2CR_TX_NO_AK;
  428. writeb(temp, base + (I2CR << reg_shift));
  429. writeb(I2SR_IIF_CLEAR, base + (I2SR << reg_shift));
  430. /* dummy read to clear ICF */
  431. readb(base + (I2DR << reg_shift));
  432. /* read data */
  433. for (i = 0; i < len; i++) {
  434. ret = wait_for_sr_state(i2c_bus, ST_IIF);
  435. if (ret < 0) {
  436. debug("i2c_read_data(): ret=%d\n", ret);
  437. i2c_imx_stop(i2c_bus);
  438. return ret;
  439. }
  440. /*
  441. * It must generate STOP before read I2DR to prevent
  442. * controller from generating another clock cycle
  443. */
  444. if (i == (len - 1)) {
  445. i2c_imx_stop(i2c_bus);
  446. } else if (i == (len - 2)) {
  447. temp = readb(base + (I2CR << reg_shift));
  448. temp |= I2CR_TX_NO_AK;
  449. writeb(temp, base + (I2CR << reg_shift));
  450. }
  451. writeb(I2SR_IIF_CLEAR, base + (I2SR << reg_shift));
  452. buf[i] = readb(base + (I2DR << reg_shift));
  453. }
  454. /* reuse ret for counter*/
  455. for (ret = 0; ret < len; ++ret)
  456. debug(" 0x%02x", buf[ret]);
  457. debug("\n");
  458. i2c_imx_stop(i2c_bus);
  459. return 0;
  460. }
  461. #ifndef CONFIG_DM_I2C
  462. /*
  463. * Read data from I2C device
  464. */
  465. static int bus_i2c_read(struct mxc_i2c_bus *i2c_bus, u8 chip, u32 addr,
  466. int alen, u8 *buf, int len)
  467. {
  468. int ret = 0;
  469. u32 temp;
  470. int reg_shift = i2c_bus->driver_data & I2C_QUIRK_FLAG ?
  471. VF610_I2C_REGSHIFT : IMX_I2C_REGSHIFT;
  472. ulong base = i2c_bus->base;
  473. ret = i2c_init_transfer(i2c_bus, chip, addr, alen);
  474. if (ret < 0)
  475. return ret;
  476. temp = readb(base + (I2CR << reg_shift));
  477. temp |= I2CR_RSTA;
  478. writeb(temp, base + (I2CR << reg_shift));
  479. ret = tx_byte(i2c_bus, (chip << 1) | 1);
  480. if (ret < 0) {
  481. i2c_imx_stop(i2c_bus);
  482. return ret;
  483. }
  484. ret = i2c_read_data(i2c_bus, chip, buf, len);
  485. i2c_imx_stop(i2c_bus);
  486. return ret;
  487. }
  488. /*
  489. * Write data to I2C device
  490. */
  491. static int bus_i2c_write(struct mxc_i2c_bus *i2c_bus, u8 chip, u32 addr,
  492. int alen, const u8 *buf, int len)
  493. {
  494. int ret = 0;
  495. ret = i2c_init_transfer(i2c_bus, chip, addr, alen);
  496. if (ret < 0)
  497. return ret;
  498. ret = i2c_write_data(i2c_bus, chip, buf, len);
  499. i2c_imx_stop(i2c_bus);
  500. return ret;
  501. }
  502. #if !defined(I2C2_BASE_ADDR)
  503. #define I2C2_BASE_ADDR 0
  504. #endif
  505. #if !defined(I2C3_BASE_ADDR)
  506. #define I2C3_BASE_ADDR 0
  507. #endif
  508. #if !defined(I2C4_BASE_ADDR)
  509. #define I2C4_BASE_ADDR 0
  510. #endif
  511. static struct mxc_i2c_bus mxc_i2c_buses[] = {
  512. #if defined(CONFIG_ARCH_LS1021A) || defined(CONFIG_VF610) || \
  513. defined(CONFIG_FSL_LAYERSCAPE)
  514. { 0, I2C1_BASE_ADDR, I2C_QUIRK_FLAG },
  515. { 1, I2C2_BASE_ADDR, I2C_QUIRK_FLAG },
  516. { 2, I2C3_BASE_ADDR, I2C_QUIRK_FLAG },
  517. { 3, I2C4_BASE_ADDR, I2C_QUIRK_FLAG },
  518. #else
  519. { 0, I2C1_BASE_ADDR, 0 },
  520. { 1, I2C2_BASE_ADDR, 0 },
  521. { 2, I2C3_BASE_ADDR, 0 },
  522. { 3, I2C4_BASE_ADDR, 0 },
  523. #endif
  524. };
  525. struct mxc_i2c_bus *i2c_get_base(struct i2c_adapter *adap)
  526. {
  527. return &mxc_i2c_buses[adap->hwadapnr];
  528. }
  529. static int mxc_i2c_read(struct i2c_adapter *adap, uint8_t chip,
  530. uint addr, int alen, uint8_t *buffer,
  531. int len)
  532. {
  533. return bus_i2c_read(i2c_get_base(adap), chip, addr, alen, buffer, len);
  534. }
  535. static int mxc_i2c_write(struct i2c_adapter *adap, uint8_t chip,
  536. uint addr, int alen, uint8_t *buffer,
  537. int len)
  538. {
  539. return bus_i2c_write(i2c_get_base(adap), chip, addr, alen, buffer, len);
  540. }
  541. /*
  542. * Test if a chip at a given address responds (probe the chip)
  543. */
  544. static int mxc_i2c_probe(struct i2c_adapter *adap, uint8_t chip)
  545. {
  546. return bus_i2c_write(i2c_get_base(adap), chip, 0, 0, NULL, 0);
  547. }
  548. int __enable_i2c_clk(unsigned char enable, unsigned i2c_num)
  549. {
  550. return 1;
  551. }
  552. int enable_i2c_clk(unsigned char enable, unsigned i2c_num)
  553. __attribute__((weak, alias("__enable_i2c_clk")));
  554. void bus_i2c_init(int index, int speed, int unused,
  555. int (*idle_bus_fn)(void *p), void *idle_bus_data)
  556. {
  557. int ret;
  558. if (index >= ARRAY_SIZE(mxc_i2c_buses)) {
  559. debug("Error i2c index\n");
  560. return;
  561. }
  562. /*
  563. * Warning: Be careful to allow the assignment to a static
  564. * variable here. This function could be called while U-Boot is
  565. * still running in flash memory. So such assignment is equal
  566. * to write data to flash without erasing.
  567. */
  568. if (idle_bus_fn)
  569. mxc_i2c_buses[index].idle_bus_fn = idle_bus_fn;
  570. if (idle_bus_data)
  571. mxc_i2c_buses[index].idle_bus_data = idle_bus_data;
  572. ret = enable_i2c_clk(1, index);
  573. if (ret < 0) {
  574. debug("I2C-%d clk fail to enable.\n", index);
  575. return;
  576. }
  577. bus_i2c_set_bus_speed(&mxc_i2c_buses[index], speed);
  578. }
  579. /*
  580. * Early init I2C for prepare read the clk through I2C.
  581. */
  582. void i2c_early_init_f(void)
  583. {
  584. ulong base = mxc_i2c_buses[I2C_EARLY_INIT_INDEX].base;
  585. bool quirk = mxc_i2c_buses[I2C_EARLY_INIT_INDEX].driver_data
  586. & I2C_QUIRK_FLAG ? true : false;
  587. int reg_shift = quirk ? VF610_I2C_REGSHIFT : IMX_I2C_REGSHIFT;
  588. /* Set I2C divider value */
  589. writeb(I2C_IFDR_DIV_CONSERVATIVE, base + (IFDR << reg_shift));
  590. /* Reset module */
  591. writeb(I2CR_IDIS, base + (I2CR << reg_shift));
  592. writeb(0, base + (I2SR << reg_shift));
  593. /* Enable I2C */
  594. writeb(I2CR_IEN, base + (I2CR << reg_shift));
  595. }
  596. /*
  597. * Init I2C Bus
  598. */
  599. static void mxc_i2c_init(struct i2c_adapter *adap, int speed, int slaveaddr)
  600. {
  601. bus_i2c_init(adap->hwadapnr, speed, slaveaddr, NULL, NULL);
  602. }
  603. /*
  604. * Set I2C Speed
  605. */
  606. static u32 mxc_i2c_set_bus_speed(struct i2c_adapter *adap, uint speed)
  607. {
  608. return bus_i2c_set_bus_speed(i2c_get_base(adap), speed);
  609. }
  610. /*
  611. * Register mxc i2c adapters
  612. */
  613. #ifdef CONFIG_SYS_I2C_MXC_I2C1
  614. U_BOOT_I2C_ADAP_COMPLETE(mxc0, mxc_i2c_init, mxc_i2c_probe,
  615. mxc_i2c_read, mxc_i2c_write,
  616. mxc_i2c_set_bus_speed,
  617. CONFIG_SYS_MXC_I2C1_SPEED,
  618. CONFIG_SYS_MXC_I2C1_SLAVE, 0)
  619. #endif
  620. #ifdef CONFIG_SYS_I2C_MXC_I2C2
  621. U_BOOT_I2C_ADAP_COMPLETE(mxc1, mxc_i2c_init, mxc_i2c_probe,
  622. mxc_i2c_read, mxc_i2c_write,
  623. mxc_i2c_set_bus_speed,
  624. CONFIG_SYS_MXC_I2C2_SPEED,
  625. CONFIG_SYS_MXC_I2C2_SLAVE, 1)
  626. #endif
  627. #ifdef CONFIG_SYS_I2C_MXC_I2C3
  628. U_BOOT_I2C_ADAP_COMPLETE(mxc2, mxc_i2c_init, mxc_i2c_probe,
  629. mxc_i2c_read, mxc_i2c_write,
  630. mxc_i2c_set_bus_speed,
  631. CONFIG_SYS_MXC_I2C3_SPEED,
  632. CONFIG_SYS_MXC_I2C3_SLAVE, 2)
  633. #endif
  634. #ifdef CONFIG_SYS_I2C_MXC_I2C4
  635. U_BOOT_I2C_ADAP_COMPLETE(mxc3, mxc_i2c_init, mxc_i2c_probe,
  636. mxc_i2c_read, mxc_i2c_write,
  637. mxc_i2c_set_bus_speed,
  638. CONFIG_SYS_MXC_I2C4_SPEED,
  639. CONFIG_SYS_MXC_I2C4_SLAVE, 3)
  640. #endif
  641. #else
  642. static int mxc_i2c_set_bus_speed(struct udevice *bus, unsigned int speed)
  643. {
  644. struct mxc_i2c_bus *i2c_bus = dev_get_priv(bus);
  645. return bus_i2c_set_bus_speed(i2c_bus, speed);
  646. }
  647. static int mxc_i2c_probe(struct udevice *bus)
  648. {
  649. struct mxc_i2c_bus *i2c_bus = dev_get_priv(bus);
  650. const void *fdt = gd->fdt_blob;
  651. int node = dev_of_offset(bus);
  652. fdt_addr_t addr;
  653. int ret, ret2;
  654. i2c_bus->driver_data = dev_get_driver_data(bus);
  655. addr = devfdt_get_addr(bus);
  656. if (addr == FDT_ADDR_T_NONE)
  657. return -EINVAL;
  658. i2c_bus->base = addr;
  659. i2c_bus->index = bus->seq;
  660. i2c_bus->bus = bus;
  661. /* Enable clk */
  662. ret = enable_i2c_clk(1, bus->seq);
  663. if (ret < 0)
  664. return ret;
  665. /*
  666. * See Documentation/devicetree/bindings/i2c/i2c-imx.txt
  667. * Use gpio to force bus idle when necessary.
  668. */
  669. ret = fdt_stringlist_search(fdt, node, "pinctrl-names", "gpio");
  670. if (ret < 0) {
  671. debug("i2c bus %d at 0x%2lx, no gpio pinctrl state.\n", bus->seq, i2c_bus->base);
  672. } else {
  673. ret = gpio_request_by_name_nodev(offset_to_ofnode(node),
  674. "scl-gpios", 0, &i2c_bus->scl_gpio,
  675. GPIOD_IS_OUT);
  676. ret2 = gpio_request_by_name_nodev(offset_to_ofnode(node),
  677. "sda-gpios", 0, &i2c_bus->sda_gpio,
  678. GPIOD_IS_OUT);
  679. if (!dm_gpio_is_valid(&i2c_bus->sda_gpio) |
  680. !dm_gpio_is_valid(&i2c_bus->scl_gpio) |
  681. ret | ret2) {
  682. dev_err(dev, "i2c bus %d at %lu, fail to request scl/sda gpio\n", bus->seq, i2c_bus->base);
  683. return -EINVAL;
  684. }
  685. }
  686. ret = i2c_idle_bus(i2c_bus);
  687. if (ret < 0) {
  688. /* Disable clk */
  689. enable_i2c_clk(0, bus->seq);
  690. return ret;
  691. }
  692. /*
  693. * Pinmux settings are in board file now, until pinmux is supported,
  694. * we can set pinmux here in probe function.
  695. */
  696. debug("i2c : controller bus %d at %lu , speed %d: ",
  697. bus->seq, i2c_bus->base,
  698. i2c_bus->speed);
  699. return 0;
  700. }
  701. static int mxc_i2c_probe_chip(struct udevice *bus, u32 chip_addr,
  702. u32 chip_flags)
  703. {
  704. int ret;
  705. struct mxc_i2c_bus *i2c_bus = dev_get_priv(bus);
  706. ret = i2c_init_transfer(i2c_bus, chip_addr, 0, 0);
  707. if (ret < 0) {
  708. debug("%s failed, ret = %d\n", __func__, ret);
  709. return ret;
  710. }
  711. i2c_imx_stop(i2c_bus);
  712. return 0;
  713. }
  714. static int mxc_i2c_xfer(struct udevice *bus, struct i2c_msg *msg, int nmsgs)
  715. {
  716. struct mxc_i2c_bus *i2c_bus = dev_get_priv(bus);
  717. int ret = 0;
  718. ulong base = i2c_bus->base;
  719. int reg_shift = i2c_bus->driver_data & I2C_QUIRK_FLAG ?
  720. VF610_I2C_REGSHIFT : IMX_I2C_REGSHIFT;
  721. /*
  722. * Here the 3rd parameter addr and the 4th one alen are set to 0,
  723. * because here we only want to send out chip address. The register
  724. * address is wrapped in msg.
  725. */
  726. ret = i2c_init_transfer(i2c_bus, msg->addr, 0, 0);
  727. if (ret < 0) {
  728. debug("i2c_init_transfer error: %d\n", ret);
  729. return ret;
  730. }
  731. for (; nmsgs > 0; nmsgs--, msg++) {
  732. bool next_is_read = nmsgs > 1 && (msg[1].flags & I2C_M_RD);
  733. debug("i2c_xfer: chip=0x%x, len=0x%x\n", msg->addr, msg->len);
  734. if (msg->flags & I2C_M_RD)
  735. ret = i2c_read_data(i2c_bus, msg->addr, msg->buf,
  736. msg->len);
  737. else {
  738. ret = i2c_write_data(i2c_bus, msg->addr, msg->buf,
  739. msg->len);
  740. if (ret)
  741. break;
  742. if (next_is_read) {
  743. /* Reuse ret */
  744. ret = readb(base + (I2CR << reg_shift));
  745. ret |= I2CR_RSTA;
  746. writeb(ret, base + (I2CR << reg_shift));
  747. ret = tx_byte(i2c_bus, (msg->addr << 1) | 1);
  748. if (ret < 0) {
  749. i2c_imx_stop(i2c_bus);
  750. break;
  751. }
  752. }
  753. }
  754. }
  755. if (ret)
  756. debug("i2c_write: error sending\n");
  757. i2c_imx_stop(i2c_bus);
  758. return ret;
  759. }
  760. static const struct dm_i2c_ops mxc_i2c_ops = {
  761. .xfer = mxc_i2c_xfer,
  762. .probe_chip = mxc_i2c_probe_chip,
  763. .set_bus_speed = mxc_i2c_set_bus_speed,
  764. };
  765. static const struct udevice_id mxc_i2c_ids[] = {
  766. { .compatible = "fsl,imx21-i2c", },
  767. { .compatible = "fsl,vf610-i2c", .data = I2C_QUIRK_FLAG, },
  768. {}
  769. };
  770. U_BOOT_DRIVER(i2c_mxc) = {
  771. .name = "i2c_mxc",
  772. .id = UCLASS_I2C,
  773. .of_match = mxc_i2c_ids,
  774. .probe = mxc_i2c_probe,
  775. .priv_auto_alloc_size = sizeof(struct mxc_i2c_bus),
  776. .ops = &mxc_i2c_ops,
  777. };
  778. #endif