mxc_i2c.c 27 KB

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