mxc_i2c.c 24 KB

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