1*83d290c5STom Rini // SPDX-License-Identifier: GPL-2.0+
2cdace066SSascha Hauer /*
3db84140bSMarek Vasut * i2c driver for Freescale i.MX series
4cdace066SSascha Hauer *
5cdace066SSascha Hauer * (c) 2007 Pengutronix, Sascha Hauer <s.hauer@pengutronix.de>
6db84140bSMarek Vasut * (c) 2011 Marek Vasut <marek.vasut@gmail.com>
7db84140bSMarek Vasut *
8db84140bSMarek Vasut * Based on i2c-imx.c from linux kernel:
9db84140bSMarek Vasut * Copyright (C) 2005 Torsten Koschorrek <koschorrek at synertronixx.de>
10db84140bSMarek Vasut * Copyright (C) 2005 Matthias Blaschke <blaschke at synertronixx.de>
11db84140bSMarek Vasut * Copyright (C) 2007 RightHand Technologies, Inc.
12db84140bSMarek Vasut * Copyright (C) 2008 Darius Augulis <darius.augulis at teltonika.lt>
13db84140bSMarek Vasut *
14cdace066SSascha Hauer */
15cdace066SSascha Hauer
16cdace066SSascha Hauer #include <common.h>
17127cec18SLiu Hui-R64343 #include <asm/arch/clock.h>
1886271115SStefano Babic #include <asm/arch/imx-regs.h>
191221ce45SMasahiro Yamada #include <linux/errno.h>
20552a848eSStefano Babic #include <asm/mach-imx/mxc_i2c.h>
2124cd738bSTroy Kisky #include <asm/io.h>
22bf0783dfSMarek Vasut #include <i2c.h>
237aa57a01STroy Kisky #include <watchdog.h>
2471204e95SPeng Fan #include <dm.h>
25e1bed802SPeng Fan #include <dm/pinctrl.h>
2671204e95SPeng Fan #include <fdtdec.h>
27cdace066SSascha Hauer
28dec1861bSYork Sun DECLARE_GLOBAL_DATA_PTR;
29dec1861bSYork Sun
3071204e95SPeng Fan #define I2C_QUIRK_FLAG (1 << 0)
3171204e95SPeng Fan
3271204e95SPeng Fan #define IMX_I2C_REGSHIFT 2
3371204e95SPeng Fan #define VF610_I2C_REGSHIFT 0
349d10c2d3SYuan Yao
359d10c2d3SYuan Yao #define I2C_EARLY_INIT_INDEX 0
369d10c2d3SYuan Yao #ifdef CONFIG_SYS_I2C_IFDR_DIV
379d10c2d3SYuan Yao #define I2C_IFDR_DIV_CONSERVATIVE CONFIG_SYS_I2C_IFDR_DIV
389d10c2d3SYuan Yao #else
399d10c2d3SYuan Yao #define I2C_IFDR_DIV_CONSERVATIVE 0x7e
409d10c2d3SYuan Yao #endif
419d10c2d3SYuan Yao
4271204e95SPeng Fan /* Register index */
4371204e95SPeng Fan #define IADR 0
4471204e95SPeng Fan #define IFDR 1
4571204e95SPeng Fan #define I2CR 2
4671204e95SPeng Fan #define I2SR 3
4771204e95SPeng Fan #define I2DR 4
48cdace066SSascha Hauer
49cdace066SSascha Hauer #define I2CR_IIEN (1 << 6)
50cdace066SSascha Hauer #define I2CR_MSTA (1 << 5)
51cdace066SSascha Hauer #define I2CR_MTX (1 << 4)
52cdace066SSascha Hauer #define I2CR_TX_NO_AK (1 << 3)
53cdace066SSascha Hauer #define I2CR_RSTA (1 << 2)
54cdace066SSascha Hauer
55cdace066SSascha Hauer #define I2SR_ICF (1 << 7)
56cdace066SSascha Hauer #define I2SR_IBB (1 << 5)
57d5383a63STroy Kisky #define I2SR_IAL (1 << 4)
58cdace066SSascha Hauer #define I2SR_IIF (1 << 1)
59cdace066SSascha Hauer #define I2SR_RX_NO_AK (1 << 0)
60cdace066SSascha Hauer
6130ea41a4SAlison Wang #ifdef I2C_QUIRK_REG
6230ea41a4SAlison Wang #define I2CR_IEN (0 << 7)
6330ea41a4SAlison Wang #define I2CR_IDIS (1 << 7)
6430ea41a4SAlison Wang #define I2SR_IIF_CLEAR (1 << 1)
6530ea41a4SAlison Wang #else
6630ea41a4SAlison Wang #define I2CR_IEN (1 << 7)
6730ea41a4SAlison Wang #define I2CR_IDIS (0 << 7)
6830ea41a4SAlison Wang #define I2SR_IIF_CLEAR (0 << 1)
6930ea41a4SAlison Wang #endif
7030ea41a4SAlison Wang
7130ea41a4SAlison Wang #ifdef I2C_QUIRK_REG
7230ea41a4SAlison Wang static u16 i2c_clk_div[60][2] = {
7330ea41a4SAlison Wang { 20, 0x00 }, { 22, 0x01 }, { 24, 0x02 }, { 26, 0x03 },
7430ea41a4SAlison Wang { 28, 0x04 }, { 30, 0x05 }, { 32, 0x09 }, { 34, 0x06 },
7530ea41a4SAlison Wang { 36, 0x0A }, { 40, 0x07 }, { 44, 0x0C }, { 48, 0x0D },
7630ea41a4SAlison Wang { 52, 0x43 }, { 56, 0x0E }, { 60, 0x45 }, { 64, 0x12 },
7730ea41a4SAlison Wang { 68, 0x0F }, { 72, 0x13 }, { 80, 0x14 }, { 88, 0x15 },
7830ea41a4SAlison Wang { 96, 0x19 }, { 104, 0x16 }, { 112, 0x1A }, { 128, 0x17 },
7930ea41a4SAlison Wang { 136, 0x4F }, { 144, 0x1C }, { 160, 0x1D }, { 176, 0x55 },
8030ea41a4SAlison Wang { 192, 0x1E }, { 208, 0x56 }, { 224, 0x22 }, { 228, 0x24 },
8130ea41a4SAlison Wang { 240, 0x1F }, { 256, 0x23 }, { 288, 0x5C }, { 320, 0x25 },
8230ea41a4SAlison Wang { 384, 0x26 }, { 448, 0x2A }, { 480, 0x27 }, { 512, 0x2B },
8330ea41a4SAlison Wang { 576, 0x2C }, { 640, 0x2D }, { 768, 0x31 }, { 896, 0x32 },
8430ea41a4SAlison Wang { 960, 0x2F }, { 1024, 0x33 }, { 1152, 0x34 }, { 1280, 0x35 },
8530ea41a4SAlison Wang { 1536, 0x36 }, { 1792, 0x3A }, { 1920, 0x37 }, { 2048, 0x3B },
8630ea41a4SAlison Wang { 2304, 0x3C }, { 2560, 0x3D }, { 3072, 0x3E }, { 3584, 0x7A },
8730ea41a4SAlison Wang { 3840, 0x3F }, { 4096, 0x7B }, { 5120, 0x7D }, { 6144, 0x7E },
8830ea41a4SAlison Wang };
8930ea41a4SAlison Wang #else
90db84140bSMarek Vasut static u16 i2c_clk_div[50][2] = {
91db84140bSMarek Vasut { 22, 0x20 }, { 24, 0x21 }, { 26, 0x22 }, { 28, 0x23 },
92db84140bSMarek Vasut { 30, 0x00 }, { 32, 0x24 }, { 36, 0x25 }, { 40, 0x26 },
93db84140bSMarek Vasut { 42, 0x03 }, { 44, 0x27 }, { 48, 0x28 }, { 52, 0x05 },
94db84140bSMarek Vasut { 56, 0x29 }, { 60, 0x06 }, { 64, 0x2A }, { 72, 0x2B },
95db84140bSMarek Vasut { 80, 0x2C }, { 88, 0x09 }, { 96, 0x2D }, { 104, 0x0A },
96db84140bSMarek Vasut { 112, 0x2E }, { 128, 0x2F }, { 144, 0x0C }, { 160, 0x30 },
97db84140bSMarek Vasut { 192, 0x31 }, { 224, 0x32 }, { 240, 0x0F }, { 256, 0x33 },
98db84140bSMarek Vasut { 288, 0x10 }, { 320, 0x34 }, { 384, 0x35 }, { 448, 0x36 },
99db84140bSMarek Vasut { 480, 0x13 }, { 512, 0x37 }, { 576, 0x14 }, { 640, 0x38 },
100db84140bSMarek Vasut { 768, 0x39 }, { 896, 0x3A }, { 960, 0x17 }, { 1024, 0x3B },
101db84140bSMarek Vasut { 1152, 0x18 }, { 1280, 0x3C }, { 1536, 0x3D }, { 1792, 0x3E },
102db84140bSMarek Vasut { 1920, 0x1B }, { 2048, 0x3F }, { 2304, 0x1C }, { 2560, 0x1D },
103db84140bSMarek Vasut { 3072, 0x1E }, { 3840, 0x1F }
104db84140bSMarek Vasut };
10530ea41a4SAlison Wang #endif
106cdace066SSascha Hauer
107fac96408Strem #ifndef CONFIG_SYS_MXC_I2C1_SPEED
108fac96408Strem #define CONFIG_SYS_MXC_I2C1_SPEED 100000
109fac96408Strem #endif
110fac96408Strem #ifndef CONFIG_SYS_MXC_I2C2_SPEED
111fac96408Strem #define CONFIG_SYS_MXC_I2C2_SPEED 100000
112fac96408Strem #endif
113fac96408Strem #ifndef CONFIG_SYS_MXC_I2C3_SPEED
114fac96408Strem #define CONFIG_SYS_MXC_I2C3_SPEED 100000
115fac96408Strem #endif
116f8cb101eSYork Sun #ifndef CONFIG_SYS_MXC_I2C4_SPEED
117f8cb101eSYork Sun #define CONFIG_SYS_MXC_I2C4_SPEED 100000
118f8cb101eSYork Sun #endif
119fac96408Strem
120fac96408Strem #ifndef CONFIG_SYS_MXC_I2C1_SLAVE
121fac96408Strem #define CONFIG_SYS_MXC_I2C1_SLAVE 0
122fac96408Strem #endif
123fac96408Strem #ifndef CONFIG_SYS_MXC_I2C2_SLAVE
124fac96408Strem #define CONFIG_SYS_MXC_I2C2_SLAVE 0
125fac96408Strem #endif
126fac96408Strem #ifndef CONFIG_SYS_MXC_I2C3_SLAVE
127fac96408Strem #define CONFIG_SYS_MXC_I2C3_SLAVE 0
128fac96408Strem #endif
129f8cb101eSYork Sun #ifndef CONFIG_SYS_MXC_I2C4_SLAVE
130f8cb101eSYork Sun #define CONFIG_SYS_MXC_I2C4_SLAVE 0
131f8cb101eSYork Sun #endif
132fac96408Strem
133db84140bSMarek Vasut /*
134db84140bSMarek Vasut * Calculate and set proper clock divider
135db84140bSMarek Vasut */
i2c_imx_get_clk(struct mxc_i2c_bus * i2c_bus,unsigned int rate)13671204e95SPeng Fan static uint8_t i2c_imx_get_clk(struct mxc_i2c_bus *i2c_bus, unsigned int rate)
1371d549adeSStefano Babic {
138db84140bSMarek Vasut unsigned int i2c_clk_rate;
139db84140bSMarek Vasut unsigned int div;
140bf0783dfSMarek Vasut u8 clk_div;
141cdace066SSascha Hauer
142127cec18SLiu Hui-R64343 #if defined(CONFIG_MX31)
1431d549adeSStefano Babic struct clock_control_regs *sc_regs =
1441d549adeSStefano Babic (struct clock_control_regs *)CCM_BASE;
145db84140bSMarek Vasut
146e7de18afSGuennadi Liakhovetski /* start the required I2C clock */
147de6f604dSTroy Kisky writel(readl(&sc_regs->cgr0) | (3 << CONFIG_SYS_I2C_CLK_OFFSET),
1481d549adeSStefano Babic &sc_regs->cgr0);
149127cec18SLiu Hui-R64343 #endif
150e7de18afSGuennadi Liakhovetski
151db84140bSMarek Vasut /* Divider value calculation */
152e7bed5c2SMatthias Weisser i2c_clk_rate = mxc_get_clock(MXC_I2C_CLK);
153db84140bSMarek Vasut div = (i2c_clk_rate + rate - 1) / rate;
154db84140bSMarek Vasut if (div < i2c_clk_div[0][0])
155b567b8ffSMarek Vasut clk_div = 0;
156db84140bSMarek Vasut else if (div > i2c_clk_div[ARRAY_SIZE(i2c_clk_div) - 1][0])
157b567b8ffSMarek Vasut clk_div = ARRAY_SIZE(i2c_clk_div) - 1;
158db84140bSMarek Vasut else
159b567b8ffSMarek Vasut for (clk_div = 0; i2c_clk_div[clk_div][0] < div; clk_div++)
160db84140bSMarek Vasut ;
161cdace066SSascha Hauer
162db84140bSMarek Vasut /* Store divider value */
163bf0783dfSMarek Vasut return clk_div;
164db84140bSMarek Vasut }
165cdace066SSascha Hauer
166db84140bSMarek Vasut /*
167e4ff525fSTroy Kisky * Set I2C Bus speed
168db84140bSMarek Vasut */
bus_i2c_set_bus_speed(struct mxc_i2c_bus * i2c_bus,int speed)16971204e95SPeng Fan static int bus_i2c_set_bus_speed(struct mxc_i2c_bus *i2c_bus, int speed)
170db84140bSMarek Vasut {
17171204e95SPeng Fan ulong base = i2c_bus->base;
17271204e95SPeng Fan bool quirk = i2c_bus->driver_data & I2C_QUIRK_FLAG ? true : false;
17371204e95SPeng Fan u8 clk_idx = i2c_imx_get_clk(i2c_bus, speed);
174bf0783dfSMarek Vasut u8 idx = i2c_clk_div[clk_idx][1];
17571204e95SPeng Fan int reg_shift = quirk ? VF610_I2C_REGSHIFT : IMX_I2C_REGSHIFT;
176bf0783dfSMarek Vasut
177e6c8b716SHeiko Schocher if (!base)
1787c84319aSSimon Glass return -EINVAL;
179e6c8b716SHeiko Schocher
180bf0783dfSMarek Vasut /* Store divider value */
18171204e95SPeng Fan writeb(idx, base + (IFDR << reg_shift));
182bf0783dfSMarek Vasut
18383a1a190STroy Kisky /* Reset module */
18471204e95SPeng Fan writeb(I2CR_IDIS, base + (I2CR << reg_shift));
18571204e95SPeng Fan writeb(0, base + (I2SR << reg_shift));
186b567b8ffSMarek Vasut return 0;
187b567b8ffSMarek Vasut }
188b567b8ffSMarek Vasut
1897aa57a01STroy Kisky #define ST_BUS_IDLE (0 | (I2SR_IBB << 8))
1907aa57a01STroy Kisky #define ST_BUS_BUSY (I2SR_IBB | (I2SR_IBB << 8))
1917aa57a01STroy Kisky #define ST_IIF (I2SR_IIF | (I2SR_IIF << 8))
1927aa57a01STroy Kisky
wait_for_sr_state(struct mxc_i2c_bus * i2c_bus,unsigned state)19371204e95SPeng Fan static int wait_for_sr_state(struct mxc_i2c_bus *i2c_bus, unsigned state)
19481687212SStefano Babic {
1957aa57a01STroy Kisky unsigned sr;
1967aa57a01STroy Kisky ulong elapsed;
19771204e95SPeng Fan bool quirk = i2c_bus->driver_data & I2C_QUIRK_FLAG ? true : false;
19871204e95SPeng Fan int reg_shift = quirk ? VF610_I2C_REGSHIFT : IMX_I2C_REGSHIFT;
19971204e95SPeng Fan ulong base = i2c_bus->base;
2007aa57a01STroy Kisky ulong start_time = get_timer(0);
2017aa57a01STroy Kisky for (;;) {
20271204e95SPeng Fan sr = readb(base + (I2SR << reg_shift));
203d5383a63STroy Kisky if (sr & I2SR_IAL) {
20471204e95SPeng Fan if (quirk)
20571204e95SPeng Fan writeb(sr | I2SR_IAL, base +
20671204e95SPeng Fan (I2SR << reg_shift));
20771204e95SPeng Fan else
20871204e95SPeng Fan writeb(sr & ~I2SR_IAL, base +
20971204e95SPeng Fan (I2SR << reg_shift));
210d5383a63STroy Kisky printf("%s: Arbitration lost sr=%x cr=%x state=%x\n",
21171204e95SPeng Fan __func__, sr, readb(base + (I2CR << reg_shift)),
21271204e95SPeng Fan state);
213d5383a63STroy Kisky return -ERESTART;
214d5383a63STroy Kisky }
2157aa57a01STroy Kisky if ((sr & (state >> 8)) == (unsigned char)state)
2167aa57a01STroy Kisky return sr;
2177aa57a01STroy Kisky WATCHDOG_RESET();
2187aa57a01STroy Kisky elapsed = get_timer(start_time);
2197aa57a01STroy Kisky if (elapsed > (CONFIG_SYS_HZ / 10)) /* .1 seconds */
2207aa57a01STroy Kisky break;
22181687212SStefano Babic }
2227aa57a01STroy Kisky printf("%s: failed sr=%x cr=%x state=%x\n", __func__,
22371204e95SPeng Fan sr, readb(base + (I2CR << reg_shift)), state);
224cea60b0cSTroy Kisky return -ETIMEDOUT;
225db84140bSMarek Vasut }
226db84140bSMarek Vasut
tx_byte(struct mxc_i2c_bus * i2c_bus,u8 byte)22771204e95SPeng Fan static int tx_byte(struct mxc_i2c_bus *i2c_bus, u8 byte)
228cdace066SSascha Hauer {
229cea60b0cSTroy Kisky int ret;
23071204e95SPeng Fan int reg_shift = i2c_bus->driver_data & I2C_QUIRK_FLAG ?
23171204e95SPeng Fan VF610_I2C_REGSHIFT : IMX_I2C_REGSHIFT;
23271204e95SPeng Fan ulong base = i2c_bus->base;
233db84140bSMarek Vasut
23471204e95SPeng Fan writeb(I2SR_IIF_CLEAR, base + (I2SR << reg_shift));
23571204e95SPeng Fan writeb(byte, base + (I2DR << reg_shift));
23671204e95SPeng Fan
23771204e95SPeng Fan ret = wait_for_sr_state(i2c_bus, ST_IIF);
238cea60b0cSTroy Kisky if (ret < 0)
239cea60b0cSTroy Kisky return ret;
240cea60b0cSTroy Kisky if (ret & I2SR_RX_NO_AK)
2417c84319aSSimon Glass return -EREMOTEIO;
242cea60b0cSTroy Kisky return 0;
243db84140bSMarek Vasut }
244db84140bSMarek Vasut
245db84140bSMarek Vasut /*
24671204e95SPeng Fan * Stub implementations for outer i2c slave operations.
24771204e95SPeng Fan */
__i2c_force_reset_slave(void)24871204e95SPeng Fan void __i2c_force_reset_slave(void)
24971204e95SPeng Fan {
25071204e95SPeng Fan }
25171204e95SPeng Fan void i2c_force_reset_slave(void)
25271204e95SPeng Fan __attribute__((weak, alias("__i2c_force_reset_slave")));
25371204e95SPeng Fan
25471204e95SPeng Fan /*
25590a5b70fSTroy Kisky * Stop I2C transaction
256db84140bSMarek Vasut */
i2c_imx_stop(struct mxc_i2c_bus * i2c_bus)25771204e95SPeng Fan static void i2c_imx_stop(struct mxc_i2c_bus *i2c_bus)
258db84140bSMarek Vasut {
2597aa57a01STroy Kisky int ret;
26071204e95SPeng Fan int reg_shift = i2c_bus->driver_data & I2C_QUIRK_FLAG ?
26171204e95SPeng Fan VF610_I2C_REGSHIFT : IMX_I2C_REGSHIFT;
26271204e95SPeng Fan ulong base = i2c_bus->base;
26371204e95SPeng Fan unsigned int temp = readb(base + (I2CR << reg_shift));
264db84140bSMarek Vasut
2651c076dbaSTroy Kisky temp &= ~(I2CR_MSTA | I2CR_MTX);
26671204e95SPeng Fan writeb(temp, base + (I2CR << reg_shift));
26771204e95SPeng Fan ret = wait_for_sr_state(i2c_bus, ST_BUS_IDLE);
2687aa57a01STroy Kisky if (ret < 0)
2697aa57a01STroy Kisky printf("%s:trigger stop failed\n", __func__);
270db84140bSMarek Vasut }
271db84140bSMarek Vasut
272db84140bSMarek Vasut /*
273b230ddc2STroy Kisky * Send start signal, chip address and
274b230ddc2STroy Kisky * write register address
275db84140bSMarek Vasut */
i2c_init_transfer_(struct mxc_i2c_bus * i2c_bus,u8 chip,u32 addr,int alen)27671204e95SPeng Fan static int i2c_init_transfer_(struct mxc_i2c_bus *i2c_bus, u8 chip,
27771204e95SPeng Fan u32 addr, int alen)
278cdace066SSascha Hauer {
27971e9f3cbSTroy Kisky unsigned int temp;
28071e9f3cbSTroy Kisky int ret;
28171204e95SPeng Fan bool quirk = i2c_bus->driver_data & I2C_QUIRK_FLAG ? true : false;
28271204e95SPeng Fan ulong base = i2c_bus->base;
28371204e95SPeng Fan int reg_shift = quirk ? VF610_I2C_REGSHIFT : IMX_I2C_REGSHIFT;
28471204e95SPeng Fan
28571204e95SPeng Fan /* Reset i2c slave */
28671204e95SPeng Fan i2c_force_reset_slave();
28771e9f3cbSTroy Kisky
28871e9f3cbSTroy Kisky /* Enable I2C controller */
28971204e95SPeng Fan if (quirk)
29071204e95SPeng Fan ret = readb(base + (I2CR << reg_shift)) & I2CR_IDIS;
29171204e95SPeng Fan else
29271204e95SPeng Fan ret = !(readb(base + (I2CR << reg_shift)) & I2CR_IEN);
29371204e95SPeng Fan
29471204e95SPeng Fan if (ret) {
29571204e95SPeng Fan writeb(I2CR_IEN, base + (I2CR << reg_shift));
29671e9f3cbSTroy Kisky /* Wait for controller to be stable */
29771e9f3cbSTroy Kisky udelay(50);
29890a5b70fSTroy Kisky }
29971204e95SPeng Fan
30071204e95SPeng Fan if (readb(base + (IADR << reg_shift)) == (chip << 1))
30171204e95SPeng Fan writeb((chip << 1) ^ 2, base + (IADR << reg_shift));
30271204e95SPeng Fan writeb(I2SR_IIF_CLEAR, base + (I2SR << reg_shift));
30371204e95SPeng Fan ret = wait_for_sr_state(i2c_bus, ST_BUS_IDLE);
30490a5b70fSTroy Kisky if (ret < 0)
305a7f1a005STroy Kisky return ret;
30671e9f3cbSTroy Kisky
30771e9f3cbSTroy Kisky /* Start I2C transaction */
30871204e95SPeng Fan temp = readb(base + (I2CR << reg_shift));
30971e9f3cbSTroy Kisky temp |= I2CR_MSTA;
31071204e95SPeng Fan writeb(temp, base + (I2CR << reg_shift));
31171e9f3cbSTroy Kisky
31271204e95SPeng Fan ret = wait_for_sr_state(i2c_bus, ST_BUS_BUSY);
31371e9f3cbSTroy Kisky if (ret < 0)
314a7f1a005STroy Kisky return ret;
315b230ddc2STroy Kisky
31671e9f3cbSTroy Kisky temp |= I2CR_MTX | I2CR_TX_NO_AK;
31771204e95SPeng Fan writeb(temp, base + (I2CR << reg_shift));
31871e9f3cbSTroy Kisky
3192feec4eaSNandor Han if (alen >= 0) {
320b230ddc2STroy Kisky /* write slave address */
32171204e95SPeng Fan ret = tx_byte(i2c_bus, chip << 1);
322b230ddc2STroy Kisky if (ret < 0)
323a7f1a005STroy Kisky return ret;
324db84140bSMarek Vasut
325bf0783dfSMarek Vasut while (alen--) {
32671204e95SPeng Fan ret = tx_byte(i2c_bus, (addr >> (alen * 8)) & 0xff);
327cea60b0cSTroy Kisky if (ret < 0)
328a7f1a005STroy Kisky return ret;
32981687212SStefano Babic }
3302feec4eaSNandor Han }
3312feec4eaSNandor Han
332b230ddc2STroy Kisky return 0;
333a7f1a005STroy Kisky }
334a7f1a005STroy Kisky
33571204e95SPeng Fan #ifndef CONFIG_DM_I2C
i2c_idle_bus(struct mxc_i2c_bus * i2c_bus)33671204e95SPeng Fan int i2c_idle_bus(struct mxc_i2c_bus *i2c_bus)
33771204e95SPeng Fan {
33871204e95SPeng Fan if (i2c_bus && i2c_bus->idle_bus_fn)
33971204e95SPeng Fan return i2c_bus->idle_bus_fn(i2c_bus->idle_bus_data);
34071204e95SPeng Fan return 0;
34171204e95SPeng Fan }
34271204e95SPeng Fan #else
34371204e95SPeng Fan /*
344e1bed802SPeng Fan * See Linux Documentation/devicetree/bindings/i2c/i2c-imx.txt
345e1bed802SPeng Fan * "
346e1bed802SPeng Fan * scl-gpios: specify the gpio related to SCL pin
347e1bed802SPeng Fan * sda-gpios: specify the gpio related to SDA pin
348e1bed802SPeng Fan * add pinctrl to configure i2c pins to gpio function for i2c
349e1bed802SPeng Fan * bus recovery, call it "gpio" state
350e1bed802SPeng Fan * "
351e1bed802SPeng Fan *
352e1bed802SPeng Fan * The i2c_idle_bus is an implementation following Linux Kernel.
35371204e95SPeng Fan */
i2c_idle_bus(struct mxc_i2c_bus * i2c_bus)354e1bed802SPeng Fan int i2c_idle_bus(struct mxc_i2c_bus *i2c_bus)
35571204e95SPeng Fan {
356e1bed802SPeng Fan struct udevice *bus = i2c_bus->bus;
357e1bed802SPeng Fan struct gpio_desc *scl_gpio = &i2c_bus->scl_gpio;
358e1bed802SPeng Fan struct gpio_desc *sda_gpio = &i2c_bus->sda_gpio;
359e1bed802SPeng Fan int sda, scl;
360e1bed802SPeng Fan int i, ret = 0;
361e1bed802SPeng Fan ulong elapsed, start_time;
362e1bed802SPeng Fan
363e1bed802SPeng Fan if (pinctrl_select_state(bus, "gpio")) {
364e1bed802SPeng Fan dev_dbg(bus, "Can not to switch to use gpio pinmux\n");
365e1bed802SPeng Fan /*
366e1bed802SPeng Fan * GPIO pinctrl for i2c force idle is not a must,
367e1bed802SPeng Fan * but it is strongly recommended to be used.
368e1bed802SPeng Fan * Because it can help you to recover from bad
369e1bed802SPeng Fan * i2c bus state. Do not return failure, because
370e1bed802SPeng Fan * it is not a must.
371e1bed802SPeng Fan */
37271204e95SPeng Fan return 0;
37371204e95SPeng Fan }
37496c19bd3STroy Kisky
375e1bed802SPeng Fan dm_gpio_set_dir_flags(scl_gpio, GPIOD_IS_IN);
376e1bed802SPeng Fan dm_gpio_set_dir_flags(sda_gpio, GPIOD_IS_IN);
377e1bed802SPeng Fan scl = dm_gpio_get_value(scl_gpio);
378e1bed802SPeng Fan sda = dm_gpio_get_value(sda_gpio);
379e1bed802SPeng Fan
380e1bed802SPeng Fan if ((sda & scl) == 1)
381e1bed802SPeng Fan goto exit; /* Bus is idle already */
382e1bed802SPeng Fan
383e1bed802SPeng Fan /* Send high and low on the SCL line */
384e1bed802SPeng Fan for (i = 0; i < 9; i++) {
385e1bed802SPeng Fan dm_gpio_set_dir_flags(scl_gpio, GPIOD_IS_OUT);
386e1bed802SPeng Fan dm_gpio_set_value(scl_gpio, 0);
387e1bed802SPeng Fan udelay(50);
388e1bed802SPeng Fan dm_gpio_set_dir_flags(scl_gpio, GPIOD_IS_IN);
389e1bed802SPeng Fan udelay(50);
390e1bed802SPeng Fan }
391e1bed802SPeng Fan start_time = get_timer(0);
392e1bed802SPeng Fan for (;;) {
393e1bed802SPeng Fan dm_gpio_set_dir_flags(scl_gpio, GPIOD_IS_IN);
394e1bed802SPeng Fan dm_gpio_set_dir_flags(sda_gpio, GPIOD_IS_IN);
395e1bed802SPeng Fan scl = dm_gpio_get_value(scl_gpio);
396e1bed802SPeng Fan sda = dm_gpio_get_value(sda_gpio);
397e1bed802SPeng Fan if ((sda & scl) == 1)
398e1bed802SPeng Fan break;
399e1bed802SPeng Fan WATCHDOG_RESET();
400e1bed802SPeng Fan elapsed = get_timer(start_time);
401e1bed802SPeng Fan if (elapsed > (CONFIG_SYS_HZ / 5)) { /* .2 seconds */
402e1bed802SPeng Fan ret = -EBUSY;
403e1bed802SPeng Fan printf("%s: failed to clear bus, sda=%d scl=%d\n", __func__, sda, scl);
404e1bed802SPeng Fan break;
405e1bed802SPeng Fan }
406e1bed802SPeng Fan }
407e1bed802SPeng Fan
408e1bed802SPeng Fan exit:
409e1bed802SPeng Fan pinctrl_select_state(bus, "default");
410e1bed802SPeng Fan return ret;
411e1bed802SPeng Fan }
41271204e95SPeng Fan #endif
41371204e95SPeng Fan
i2c_init_transfer(struct mxc_i2c_bus * i2c_bus,u8 chip,u32 addr,int alen)41471204e95SPeng Fan static int i2c_init_transfer(struct mxc_i2c_bus *i2c_bus, u8 chip,
41571204e95SPeng Fan u32 addr, int alen)
416a7f1a005STroy Kisky {
417a7f1a005STroy Kisky int retry;
418a7f1a005STroy Kisky int ret;
41971204e95SPeng Fan int reg_shift = i2c_bus->driver_data & I2C_QUIRK_FLAG ?
42071204e95SPeng Fan VF610_I2C_REGSHIFT : IMX_I2C_REGSHIFT;
421e6c8b716SHeiko Schocher
422e6c8b716SHeiko Schocher if (!i2c_bus->base)
4237c84319aSSimon Glass return -EINVAL;
424e6c8b716SHeiko Schocher
425a7f1a005STroy Kisky for (retry = 0; retry < 3; retry++) {
42671204e95SPeng Fan ret = i2c_init_transfer_(i2c_bus, chip, addr, alen);
427a7f1a005STroy Kisky if (ret >= 0)
428a7f1a005STroy Kisky return 0;
42971204e95SPeng Fan i2c_imx_stop(i2c_bus);
4307c84319aSSimon Glass if (ret == -EREMOTEIO)
431a7f1a005STroy Kisky return ret;
432a7f1a005STroy Kisky
433a7f1a005STroy Kisky printf("%s: failed for chip 0x%x retry=%d\n", __func__, chip,
434a7f1a005STroy Kisky retry);
435a7f1a005STroy Kisky if (ret != -ERESTART)
43630ea41a4SAlison Wang /* Disable controller */
43771204e95SPeng Fan writeb(I2CR_IDIS, i2c_bus->base + (I2CR << reg_shift));
438a7f1a005STroy Kisky udelay(100);
43971204e95SPeng Fan if (i2c_idle_bus(i2c_bus) < 0)
44096c19bd3STroy Kisky break;
441a7f1a005STroy Kisky }
44271204e95SPeng Fan printf("%s: give up i2c_regs=0x%lx\n", __func__, i2c_bus->base);
443db84140bSMarek Vasut return ret;
444cdace066SSascha Hauer }
445cdace066SSascha Hauer
44671204e95SPeng Fan
i2c_write_data(struct mxc_i2c_bus * i2c_bus,u8 chip,const u8 * buf,int len)44771204e95SPeng Fan static int i2c_write_data(struct mxc_i2c_bus *i2c_bus, u8 chip, const u8 *buf,
44871204e95SPeng Fan int len)
44971204e95SPeng Fan {
45071204e95SPeng Fan int i, ret = 0;
45171204e95SPeng Fan
45271204e95SPeng Fan debug("i2c_write_data: chip=0x%x, len=0x%x\n", chip, len);
45371204e95SPeng Fan debug("write_data: ");
45471204e95SPeng Fan /* use rc for counter */
45571204e95SPeng Fan for (i = 0; i < len; ++i)
45671204e95SPeng Fan debug(" 0x%02x", buf[i]);
45771204e95SPeng Fan debug("\n");
45871204e95SPeng Fan
45971204e95SPeng Fan for (i = 0; i < len; i++) {
46071204e95SPeng Fan ret = tx_byte(i2c_bus, buf[i]);
46171204e95SPeng Fan if (ret < 0) {
46271204e95SPeng Fan debug("i2c_write_data(): rc=%d\n", ret);
46371204e95SPeng Fan break;
46471204e95SPeng Fan }
46571204e95SPeng Fan }
46671204e95SPeng Fan
46771204e95SPeng Fan return ret;
46871204e95SPeng Fan }
46971204e95SPeng Fan
i2c_read_data(struct mxc_i2c_bus * i2c_bus,uchar chip,uchar * buf,int len)47071204e95SPeng Fan static int i2c_read_data(struct mxc_i2c_bus *i2c_bus, uchar chip, uchar *buf,
471e4ff525fSTroy Kisky int len)
472db84140bSMarek Vasut {
473db84140bSMarek Vasut int ret;
474db84140bSMarek Vasut unsigned int temp;
475db84140bSMarek Vasut int i;
47671204e95SPeng Fan int reg_shift = i2c_bus->driver_data & I2C_QUIRK_FLAG ?
47771204e95SPeng Fan VF610_I2C_REGSHIFT : IMX_I2C_REGSHIFT;
47871204e95SPeng Fan ulong base = i2c_bus->base;
479cdace066SSascha Hauer
48071204e95SPeng Fan debug("i2c_read_data: chip=0x%x, len=0x%x\n", chip, len);
481db84140bSMarek Vasut
482db84140bSMarek Vasut /* setup bus to read data */
48371204e95SPeng Fan temp = readb(base + (I2CR << reg_shift));
484db84140bSMarek Vasut temp &= ~(I2CR_MTX | I2CR_TX_NO_AK);
485db84140bSMarek Vasut if (len == 1)
486db84140bSMarek Vasut temp |= I2CR_TX_NO_AK;
48771204e95SPeng Fan writeb(temp, base + (I2CR << reg_shift));
48871204e95SPeng Fan writeb(I2SR_IIF_CLEAR, base + (I2SR << reg_shift));
48971204e95SPeng Fan /* dummy read to clear ICF */
49071204e95SPeng Fan readb(base + (I2DR << reg_shift));
491db84140bSMarek Vasut
492db84140bSMarek Vasut /* read data */
493db84140bSMarek Vasut for (i = 0; i < len; i++) {
49471204e95SPeng Fan ret = wait_for_sr_state(i2c_bus, ST_IIF);
4957aa57a01STroy Kisky if (ret < 0) {
49671204e95SPeng Fan debug("i2c_read_data(): ret=%d\n", ret);
49771204e95SPeng Fan i2c_imx_stop(i2c_bus);
498db84140bSMarek Vasut return ret;
499c4330d28STroy Kisky }
500db84140bSMarek Vasut
501db84140bSMarek Vasut /*
502db84140bSMarek Vasut * It must generate STOP before read I2DR to prevent
503db84140bSMarek Vasut * controller from generating another clock cycle
504db84140bSMarek Vasut */
505db84140bSMarek Vasut if (i == (len - 1)) {
50671204e95SPeng Fan i2c_imx_stop(i2c_bus);
507db84140bSMarek Vasut } else if (i == (len - 2)) {
50871204e95SPeng Fan temp = readb(base + (I2CR << reg_shift));
509db84140bSMarek Vasut temp |= I2CR_TX_NO_AK;
51071204e95SPeng Fan writeb(temp, base + (I2CR << reg_shift));
511cdace066SSascha Hauer }
51271204e95SPeng Fan writeb(I2SR_IIF_CLEAR, base + (I2SR << reg_shift));
51371204e95SPeng Fan buf[i] = readb(base + (I2DR << reg_shift));
514cdace066SSascha Hauer }
51571204e95SPeng Fan
51671204e95SPeng Fan /* reuse ret for counter*/
51771204e95SPeng Fan for (ret = 0; ret < len; ++ret)
51871204e95SPeng Fan debug(" 0x%02x", buf[ret]);
51971204e95SPeng Fan debug("\n");
52071204e95SPeng Fan
52171204e95SPeng Fan i2c_imx_stop(i2c_bus);
5227aa57a01STroy Kisky return 0;
523db84140bSMarek Vasut }
524db84140bSMarek Vasut
52571204e95SPeng Fan #ifndef CONFIG_DM_I2C
52671204e95SPeng Fan /*
52771204e95SPeng Fan * Read data from I2C device
52871204e95SPeng Fan */
bus_i2c_read(struct mxc_i2c_bus * i2c_bus,u8 chip,u32 addr,int alen,u8 * buf,int len)52971204e95SPeng Fan static int bus_i2c_read(struct mxc_i2c_bus *i2c_bus, u8 chip, u32 addr,
53071204e95SPeng Fan int alen, u8 *buf, int len)
53171204e95SPeng Fan {
53271204e95SPeng Fan int ret = 0;
53371204e95SPeng Fan u32 temp;
53471204e95SPeng Fan int reg_shift = i2c_bus->driver_data & I2C_QUIRK_FLAG ?
53571204e95SPeng Fan VF610_I2C_REGSHIFT : IMX_I2C_REGSHIFT;
53671204e95SPeng Fan ulong base = i2c_bus->base;
53771204e95SPeng Fan
53871204e95SPeng Fan ret = i2c_init_transfer(i2c_bus, chip, addr, alen);
53971204e95SPeng Fan if (ret < 0)
54071204e95SPeng Fan return ret;
54171204e95SPeng Fan
5422feec4eaSNandor Han if (alen >= 0) {
54371204e95SPeng Fan temp = readb(base + (I2CR << reg_shift));
54471204e95SPeng Fan temp |= I2CR_RSTA;
54571204e95SPeng Fan writeb(temp, base + (I2CR << reg_shift));
5462feec4eaSNandor Han }
54771204e95SPeng Fan
54871204e95SPeng Fan ret = tx_byte(i2c_bus, (chip << 1) | 1);
54971204e95SPeng Fan if (ret < 0) {
55071204e95SPeng Fan i2c_imx_stop(i2c_bus);
55171204e95SPeng Fan return ret;
55271204e95SPeng Fan }
55371204e95SPeng Fan
55471204e95SPeng Fan ret = i2c_read_data(i2c_bus, chip, buf, len);
55571204e95SPeng Fan
55671204e95SPeng Fan i2c_imx_stop(i2c_bus);
55771204e95SPeng Fan return ret;
55871204e95SPeng Fan }
55971204e95SPeng Fan
560db84140bSMarek Vasut /*
561db84140bSMarek Vasut * Write data to I2C device
562db84140bSMarek Vasut */
bus_i2c_write(struct mxc_i2c_bus * i2c_bus,u8 chip,u32 addr,int alen,const u8 * buf,int len)56371204e95SPeng Fan static int bus_i2c_write(struct mxc_i2c_bus *i2c_bus, u8 chip, u32 addr,
56471204e95SPeng Fan int alen, const u8 *buf, int len)
565cdace066SSascha Hauer {
56671204e95SPeng Fan int ret = 0;
567cdace066SSascha Hauer
56871204e95SPeng Fan ret = i2c_init_transfer(i2c_bus, chip, addr, alen);
569cea60b0cSTroy Kisky if (ret < 0)
570db84140bSMarek Vasut return ret;
571cdace066SSascha Hauer
57271204e95SPeng Fan ret = i2c_write_data(i2c_bus, chip, buf, len);
57371204e95SPeng Fan
57471204e95SPeng Fan i2c_imx_stop(i2c_bus);
57571204e95SPeng Fan
576db84140bSMarek Vasut return ret;
577db84140bSMarek Vasut }
578cfbb88d3STroy Kisky
579e6c8b716SHeiko Schocher #if !defined(I2C2_BASE_ADDR)
580e6c8b716SHeiko Schocher #define I2C2_BASE_ADDR 0
58121a26940SHeiko Schocher #endif
582e6c8b716SHeiko Schocher
583e6c8b716SHeiko Schocher #if !defined(I2C3_BASE_ADDR)
584e6c8b716SHeiko Schocher #define I2C3_BASE_ADDR 0
585e6c8b716SHeiko Schocher #endif
586e6c8b716SHeiko Schocher
587e6c8b716SHeiko Schocher #if !defined(I2C4_BASE_ADDR)
588e6c8b716SHeiko Schocher #define I2C4_BASE_ADDR 0
589e6c8b716SHeiko Schocher #endif
590e6c8b716SHeiko Schocher
591fa452192SSriram Dash #if !defined(I2C5_BASE_ADDR)
592fa452192SSriram Dash #define I2C5_BASE_ADDR 0
593fa452192SSriram Dash #endif
594fa452192SSriram Dash
595fa452192SSriram Dash #if !defined(I2C6_BASE_ADDR)
596fa452192SSriram Dash #define I2C6_BASE_ADDR 0
597fa452192SSriram Dash #endif
598fa452192SSriram Dash
599fa452192SSriram Dash #if !defined(I2C7_BASE_ADDR)
600fa452192SSriram Dash #define I2C7_BASE_ADDR 0
601fa452192SSriram Dash #endif
602fa452192SSriram Dash
603fa452192SSriram Dash #if !defined(I2C8_BASE_ADDR)
604fa452192SSriram Dash #define I2C8_BASE_ADDR 0
605fa452192SSriram Dash #endif
606fa452192SSriram Dash
607e6c8b716SHeiko Schocher static struct mxc_i2c_bus mxc_i2c_buses[] = {
60873fb5838SYork Sun #if defined(CONFIG_ARCH_LS1021A) || defined(CONFIG_VF610) || \
6099f3183d2SMingkai Hu defined(CONFIG_FSL_LAYERSCAPE)
61071204e95SPeng Fan { 0, I2C1_BASE_ADDR, I2C_QUIRK_FLAG },
61171204e95SPeng Fan { 1, I2C2_BASE_ADDR, I2C_QUIRK_FLAG },
61271204e95SPeng Fan { 2, I2C3_BASE_ADDR, I2C_QUIRK_FLAG },
61371204e95SPeng Fan { 3, I2C4_BASE_ADDR, I2C_QUIRK_FLAG },
614fa452192SSriram Dash { 4, I2C5_BASE_ADDR, I2C_QUIRK_FLAG },
615fa452192SSriram Dash { 5, I2C6_BASE_ADDR, I2C_QUIRK_FLAG },
616fa452192SSriram Dash { 6, I2C7_BASE_ADDR, I2C_QUIRK_FLAG },
617fa452192SSriram Dash { 7, I2C8_BASE_ADDR, I2C_QUIRK_FLAG },
618e4ff525fSTroy Kisky #else
619e6c8b716SHeiko Schocher { 0, I2C1_BASE_ADDR, 0 },
620e6c8b716SHeiko Schocher { 1, I2C2_BASE_ADDR, 0 },
621e6c8b716SHeiko Schocher { 2, I2C3_BASE_ADDR, 0 },
622e6c8b716SHeiko Schocher { 3, I2C4_BASE_ADDR, 0 },
623fa452192SSriram Dash { 4, I2C5_BASE_ADDR, 0 },
624fa452192SSriram Dash { 5, I2C6_BASE_ADDR, 0 },
625fa452192SSriram Dash { 6, I2C7_BASE_ADDR, 0 },
626fa452192SSriram Dash { 7, I2C8_BASE_ADDR, 0 },
627e4ff525fSTroy Kisky #endif
628fac96408Strem };
629fac96408Strem
i2c_get_base(struct i2c_adapter * adap)63071204e95SPeng Fan struct mxc_i2c_bus *i2c_get_base(struct i2c_adapter *adap)
631fac96408Strem {
63271204e95SPeng Fan return &mxc_i2c_buses[adap->hwadapnr];
63396c19bd3STroy Kisky }
63496c19bd3STroy Kisky
mxc_i2c_read(struct i2c_adapter * adap,uint8_t chip,uint addr,int alen,uint8_t * buffer,int len)635fac96408Strem static int mxc_i2c_read(struct i2c_adapter *adap, uint8_t chip,
636fac96408Strem uint addr, int alen, uint8_t *buffer,
637fac96408Strem int len)
6389815326dSTroy Kisky {
639fac96408Strem return bus_i2c_read(i2c_get_base(adap), chip, addr, alen, buffer, len);
6409815326dSTroy Kisky }
6419815326dSTroy Kisky
mxc_i2c_write(struct i2c_adapter * adap,uint8_t chip,uint addr,int alen,uint8_t * buffer,int len)642fac96408Strem static int mxc_i2c_write(struct i2c_adapter *adap, uint8_t chip,
643fac96408Strem uint addr, int alen, uint8_t *buffer,
644fac96408Strem int len)
6459815326dSTroy Kisky {
646fac96408Strem return bus_i2c_write(i2c_get_base(adap), chip, addr, alen, buffer, len);
647e4ff525fSTroy Kisky }
648e4ff525fSTroy Kisky
649cfbb88d3STroy Kisky /*
650cfbb88d3STroy Kisky * Test if a chip at a given address responds (probe the chip)
651cfbb88d3STroy Kisky */
mxc_i2c_probe(struct i2c_adapter * adap,uint8_t chip)652fac96408Strem static int mxc_i2c_probe(struct i2c_adapter *adap, uint8_t chip)
653cfbb88d3STroy Kisky {
654fac96408Strem return bus_i2c_write(i2c_get_base(adap), chip, 0, 0, NULL, 0);
655e4ff525fSTroy Kisky }
656e4ff525fSTroy Kisky
__enable_i2c_clk(unsigned char enable,unsigned i2c_num)65771204e95SPeng Fan int __enable_i2c_clk(unsigned char enable, unsigned i2c_num)
65871204e95SPeng Fan {
65971204e95SPeng Fan return 1;
66071204e95SPeng Fan }
66171204e95SPeng Fan int enable_i2c_clk(unsigned char enable, unsigned i2c_num)
66271204e95SPeng Fan __attribute__((weak, alias("__enable_i2c_clk")));
66371204e95SPeng Fan
bus_i2c_init(int index,int speed,int unused,int (* idle_bus_fn)(void * p),void * idle_bus_data)66471204e95SPeng Fan void bus_i2c_init(int index, int speed, int unused,
665e4ff525fSTroy Kisky int (*idle_bus_fn)(void *p), void *idle_bus_data)
666e4ff525fSTroy Kisky {
66771204e95SPeng Fan int ret;
66871204e95SPeng Fan
66971204e95SPeng Fan if (index >= ARRAY_SIZE(mxc_i2c_buses)) {
67071204e95SPeng Fan debug("Error i2c index\n");
671e4ff525fSTroy Kisky return;
672e4ff525fSTroy Kisky }
67371204e95SPeng Fan
674aee3fddbSGong Qianyu /*
675aee3fddbSGong Qianyu * Warning: Be careful to allow the assignment to a static
676aee3fddbSGong Qianyu * variable here. This function could be called while U-Boot is
677aee3fddbSGong Qianyu * still running in flash memory. So such assignment is equal
678aee3fddbSGong Qianyu * to write data to flash without erasing.
679aee3fddbSGong Qianyu */
680aee3fddbSGong Qianyu if (idle_bus_fn)
68171204e95SPeng Fan mxc_i2c_buses[index].idle_bus_fn = idle_bus_fn;
682aee3fddbSGong Qianyu if (idle_bus_data)
68371204e95SPeng Fan mxc_i2c_buses[index].idle_bus_data = idle_bus_data;
68471204e95SPeng Fan
68571204e95SPeng Fan ret = enable_i2c_clk(1, index);
68671204e95SPeng Fan if (ret < 0) {
68771204e95SPeng Fan debug("I2C-%d clk fail to enable.\n", index);
68871204e95SPeng Fan return;
68971204e95SPeng Fan }
69071204e95SPeng Fan
69171204e95SPeng Fan bus_i2c_set_bus_speed(&mxc_i2c_buses[index], speed);
692e4ff525fSTroy Kisky }
693e4ff525fSTroy Kisky
694e4ff525fSTroy Kisky /*
6959d10c2d3SYuan Yao * Early init I2C for prepare read the clk through I2C.
6969d10c2d3SYuan Yao */
i2c_early_init_f(void)6979d10c2d3SYuan Yao void i2c_early_init_f(void)
6989d10c2d3SYuan Yao {
6999d10c2d3SYuan Yao ulong base = mxc_i2c_buses[I2C_EARLY_INIT_INDEX].base;
7009d10c2d3SYuan Yao bool quirk = mxc_i2c_buses[I2C_EARLY_INIT_INDEX].driver_data
7019d10c2d3SYuan Yao & I2C_QUIRK_FLAG ? true : false;
7029d10c2d3SYuan Yao int reg_shift = quirk ? VF610_I2C_REGSHIFT : IMX_I2C_REGSHIFT;
7039d10c2d3SYuan Yao
7049d10c2d3SYuan Yao /* Set I2C divider value */
7059d10c2d3SYuan Yao writeb(I2C_IFDR_DIV_CONSERVATIVE, base + (IFDR << reg_shift));
7069d10c2d3SYuan Yao /* Reset module */
7079d10c2d3SYuan Yao writeb(I2CR_IDIS, base + (I2CR << reg_shift));
7089d10c2d3SYuan Yao writeb(0, base + (I2SR << reg_shift));
7099d10c2d3SYuan Yao /* Enable I2C */
7109d10c2d3SYuan Yao writeb(I2CR_IEN, base + (I2CR << reg_shift));
7119d10c2d3SYuan Yao }
7129d10c2d3SYuan Yao
7139d10c2d3SYuan Yao /*
714e4ff525fSTroy Kisky * Init I2C Bus
715e4ff525fSTroy Kisky */
mxc_i2c_init(struct i2c_adapter * adap,int speed,int slaveaddr)716fac96408Strem static void mxc_i2c_init(struct i2c_adapter *adap, int speed, int slaveaddr)
717e4ff525fSTroy Kisky {
71871204e95SPeng Fan bus_i2c_init(adap->hwadapnr, speed, slaveaddr, NULL, NULL);
719e4ff525fSTroy Kisky }
720e4ff525fSTroy Kisky
721e4ff525fSTroy Kisky /*
722e4ff525fSTroy Kisky * Set I2C Speed
723e4ff525fSTroy Kisky */
mxc_i2c_set_bus_speed(struct i2c_adapter * adap,uint speed)72471204e95SPeng Fan static u32 mxc_i2c_set_bus_speed(struct i2c_adapter *adap, uint speed)
725e4ff525fSTroy Kisky {
726fac96408Strem return bus_i2c_set_bus_speed(i2c_get_base(adap), speed);
727e4ff525fSTroy Kisky }
728e4ff525fSTroy Kisky
729e4ff525fSTroy Kisky /*
730fac96408Strem * Register mxc i2c adapters
731e4ff525fSTroy Kisky */
73203544c66SAlbert ARIBAUD \\(3ADEV\\) #ifdef CONFIG_SYS_I2C_MXC_I2C1
733fac96408Strem U_BOOT_I2C_ADAP_COMPLETE(mxc0, mxc_i2c_init, mxc_i2c_probe,
734fac96408Strem mxc_i2c_read, mxc_i2c_write,
735fac96408Strem mxc_i2c_set_bus_speed,
736fac96408Strem CONFIG_SYS_MXC_I2C1_SPEED,
737fac96408Strem CONFIG_SYS_MXC_I2C1_SLAVE, 0)
73803544c66SAlbert ARIBAUD \\(3ADEV\\) #endif
73903544c66SAlbert ARIBAUD \\(3ADEV\\)
74003544c66SAlbert ARIBAUD \\(3ADEV\\) #ifdef CONFIG_SYS_I2C_MXC_I2C2
741fac96408Strem U_BOOT_I2C_ADAP_COMPLETE(mxc1, mxc_i2c_init, mxc_i2c_probe,
742fac96408Strem mxc_i2c_read, mxc_i2c_write,
743fac96408Strem mxc_i2c_set_bus_speed,
744fac96408Strem CONFIG_SYS_MXC_I2C2_SPEED,
745fac96408Strem CONFIG_SYS_MXC_I2C2_SLAVE, 1)
74603544c66SAlbert ARIBAUD \\(3ADEV\\) #endif
74703544c66SAlbert ARIBAUD \\(3ADEV\\)
748f8cb101eSYork Sun #ifdef CONFIG_SYS_I2C_MXC_I2C3
749fac96408Strem U_BOOT_I2C_ADAP_COMPLETE(mxc2, mxc_i2c_init, mxc_i2c_probe,
750fac96408Strem mxc_i2c_read, mxc_i2c_write,
751fac96408Strem mxc_i2c_set_bus_speed,
752fac96408Strem CONFIG_SYS_MXC_I2C3_SPEED,
753fac96408Strem CONFIG_SYS_MXC_I2C3_SLAVE, 2)
754fac96408Strem #endif
75571204e95SPeng Fan
756f8cb101eSYork Sun #ifdef CONFIG_SYS_I2C_MXC_I2C4
757f8cb101eSYork Sun U_BOOT_I2C_ADAP_COMPLETE(mxc3, mxc_i2c_init, mxc_i2c_probe,
758f8cb101eSYork Sun mxc_i2c_read, mxc_i2c_write,
759f8cb101eSYork Sun mxc_i2c_set_bus_speed,
760f8cb101eSYork Sun CONFIG_SYS_MXC_I2C4_SPEED,
761f8cb101eSYork Sun CONFIG_SYS_MXC_I2C4_SLAVE, 3)
762f8cb101eSYork Sun #endif
76371204e95SPeng Fan
764fa452192SSriram Dash #ifdef CONFIG_SYS_I2C_MXC_I2C5
765fa452192SSriram Dash U_BOOT_I2C_ADAP_COMPLETE(mxc4, mxc_i2c_init, mxc_i2c_probe,
766fa452192SSriram Dash mxc_i2c_read, mxc_i2c_write,
767fa452192SSriram Dash mxc_i2c_set_bus_speed,
768fa452192SSriram Dash CONFIG_SYS_MXC_I2C5_SPEED,
769fa452192SSriram Dash CONFIG_SYS_MXC_I2C5_SLAVE, 4)
770fa452192SSriram Dash #endif
771fa452192SSriram Dash
772fa452192SSriram Dash #ifdef CONFIG_SYS_I2C_MXC_I2C6
773fa452192SSriram Dash U_BOOT_I2C_ADAP_COMPLETE(mxc5, mxc_i2c_init, mxc_i2c_probe,
774fa452192SSriram Dash mxc_i2c_read, mxc_i2c_write,
775fa452192SSriram Dash mxc_i2c_set_bus_speed,
776fa452192SSriram Dash CONFIG_SYS_MXC_I2C6_SPEED,
777fa452192SSriram Dash CONFIG_SYS_MXC_I2C6_SLAVE, 5)
778fa452192SSriram Dash #endif
779fa452192SSriram Dash
780fa452192SSriram Dash #ifdef CONFIG_SYS_I2C_MXC_I2C7
781fa452192SSriram Dash U_BOOT_I2C_ADAP_COMPLETE(mxc6, mxc_i2c_init, mxc_i2c_probe,
782fa452192SSriram Dash mxc_i2c_read, mxc_i2c_write,
783fa452192SSriram Dash mxc_i2c_set_bus_speed,
784fa452192SSriram Dash CONFIG_SYS_MXC_I2C7_SPEED,
785fa452192SSriram Dash CONFIG_SYS_MXC_I2C7_SLAVE, 6)
786fa452192SSriram Dash #endif
787fa452192SSriram Dash
788fa452192SSriram Dash #ifdef CONFIG_SYS_I2C_MXC_I2C8
789fa452192SSriram Dash U_BOOT_I2C_ADAP_COMPLETE(mxc7, mxc_i2c_init, mxc_i2c_probe,
790fa452192SSriram Dash mxc_i2c_read, mxc_i2c_write,
791fa452192SSriram Dash mxc_i2c_set_bus_speed,
792fa452192SSriram Dash CONFIG_SYS_MXC_I2C8_SPEED,
793fa452192SSriram Dash CONFIG_SYS_MXC_I2C8_SLAVE, 7)
794fa452192SSriram Dash #endif
795fa452192SSriram Dash
79671204e95SPeng Fan #else
79771204e95SPeng Fan
79871204e95SPeng Fan static int mxc_i2c_set_bus_speed(struct udevice *bus, unsigned int speed)
79971204e95SPeng Fan {
80071204e95SPeng Fan struct mxc_i2c_bus *i2c_bus = dev_get_priv(bus);
80171204e95SPeng Fan
80271204e95SPeng Fan return bus_i2c_set_bus_speed(i2c_bus, speed);
80371204e95SPeng Fan }
80471204e95SPeng Fan
80571204e95SPeng Fan static int mxc_i2c_probe(struct udevice *bus)
80671204e95SPeng Fan {
80771204e95SPeng Fan struct mxc_i2c_bus *i2c_bus = dev_get_priv(bus);
808e1bed802SPeng Fan const void *fdt = gd->fdt_blob;
809e160f7d4SSimon Glass int node = dev_of_offset(bus);
81071204e95SPeng Fan fdt_addr_t addr;
811e1bed802SPeng Fan int ret, ret2;
81271204e95SPeng Fan
81371204e95SPeng Fan i2c_bus->driver_data = dev_get_driver_data(bus);
81471204e95SPeng Fan
815a821c4afSSimon Glass addr = devfdt_get_addr(bus);
81671204e95SPeng Fan if (addr == FDT_ADDR_T_NONE)
8177c84319aSSimon Glass return -EINVAL;
81871204e95SPeng Fan
81971204e95SPeng Fan i2c_bus->base = addr;
82071204e95SPeng Fan i2c_bus->index = bus->seq;
821e1bed802SPeng Fan i2c_bus->bus = bus;
82271204e95SPeng Fan
82371204e95SPeng Fan /* Enable clk */
82471204e95SPeng Fan ret = enable_i2c_clk(1, bus->seq);
82571204e95SPeng Fan if (ret < 0)
82671204e95SPeng Fan return ret;
82771204e95SPeng Fan
828e1bed802SPeng Fan /*
829e1bed802SPeng Fan * See Documentation/devicetree/bindings/i2c/i2c-imx.txt
830e1bed802SPeng Fan * Use gpio to force bus idle when necessary.
831e1bed802SPeng Fan */
832b02e4044SSimon Glass ret = fdt_stringlist_search(fdt, node, "pinctrl-names", "gpio");
833e1bed802SPeng Fan if (ret < 0) {
8342da24fe5SJagan Teki debug("i2c bus %d at 0x%2lx, no gpio pinctrl state.\n", bus->seq, i2c_bus->base);
835e1bed802SPeng Fan } else {
836150c5afeSSimon Glass ret = gpio_request_by_name_nodev(offset_to_ofnode(node),
837150c5afeSSimon Glass "scl-gpios", 0, &i2c_bus->scl_gpio,
838e1bed802SPeng Fan GPIOD_IS_OUT);
839150c5afeSSimon Glass ret2 = gpio_request_by_name_nodev(offset_to_ofnode(node),
840150c5afeSSimon Glass "sda-gpios", 0, &i2c_bus->sda_gpio,
841e1bed802SPeng Fan GPIOD_IS_OUT);
842fb012873SPeng Fan if (!dm_gpio_is_valid(&i2c_bus->sda_gpio) ||
843fb012873SPeng Fan !dm_gpio_is_valid(&i2c_bus->scl_gpio) ||
844fb012873SPeng Fan ret || ret2) {
845e1bed802SPeng Fan dev_err(dev, "i2c bus %d at %lu, fail to request scl/sda gpio\n", bus->seq, i2c_bus->base);
8467c84319aSSimon Glass return -EINVAL;
847e1bed802SPeng Fan }
848e1bed802SPeng Fan }
849e1bed802SPeng Fan
85071204e95SPeng Fan ret = i2c_idle_bus(i2c_bus);
85171204e95SPeng Fan if (ret < 0) {
85271204e95SPeng Fan /* Disable clk */
85371204e95SPeng Fan enable_i2c_clk(0, bus->seq);
85471204e95SPeng Fan return ret;
85571204e95SPeng Fan }
85671204e95SPeng Fan
85771204e95SPeng Fan /*
85871204e95SPeng Fan * Pinmux settings are in board file now, until pinmux is supported,
85971204e95SPeng Fan * we can set pinmux here in probe function.
86071204e95SPeng Fan */
86171204e95SPeng Fan
86271204e95SPeng Fan debug("i2c : controller bus %d at %lu , speed %d: ",
86371204e95SPeng Fan bus->seq, i2c_bus->base,
86471204e95SPeng Fan i2c_bus->speed);
86571204e95SPeng Fan
86671204e95SPeng Fan return 0;
86771204e95SPeng Fan }
86871204e95SPeng Fan
86971204e95SPeng Fan static int mxc_i2c_probe_chip(struct udevice *bus, u32 chip_addr,
87071204e95SPeng Fan u32 chip_flags)
87171204e95SPeng Fan {
87271204e95SPeng Fan int ret;
87371204e95SPeng Fan struct mxc_i2c_bus *i2c_bus = dev_get_priv(bus);
87471204e95SPeng Fan
87571204e95SPeng Fan ret = i2c_init_transfer(i2c_bus, chip_addr, 0, 0);
87671204e95SPeng Fan if (ret < 0) {
87771204e95SPeng Fan debug("%s failed, ret = %d\n", __func__, ret);
87871204e95SPeng Fan return ret;
87971204e95SPeng Fan }
88071204e95SPeng Fan
88171204e95SPeng Fan i2c_imx_stop(i2c_bus);
88271204e95SPeng Fan
88371204e95SPeng Fan return 0;
88471204e95SPeng Fan }
88571204e95SPeng Fan
88671204e95SPeng Fan static int mxc_i2c_xfer(struct udevice *bus, struct i2c_msg *msg, int nmsgs)
88771204e95SPeng Fan {
88871204e95SPeng Fan struct mxc_i2c_bus *i2c_bus = dev_get_priv(bus);
88971204e95SPeng Fan int ret = 0;
89071204e95SPeng Fan ulong base = i2c_bus->base;
89171204e95SPeng Fan int reg_shift = i2c_bus->driver_data & I2C_QUIRK_FLAG ?
89271204e95SPeng Fan VF610_I2C_REGSHIFT : IMX_I2C_REGSHIFT;
89371204e95SPeng Fan
89471204e95SPeng Fan /*
89571204e95SPeng Fan * Here the 3rd parameter addr and the 4th one alen are set to 0,
89671204e95SPeng Fan * because here we only want to send out chip address. The register
89771204e95SPeng Fan * address is wrapped in msg.
89871204e95SPeng Fan */
89971204e95SPeng Fan ret = i2c_init_transfer(i2c_bus, msg->addr, 0, 0);
90071204e95SPeng Fan if (ret < 0) {
90171204e95SPeng Fan debug("i2c_init_transfer error: %d\n", ret);
90271204e95SPeng Fan return ret;
90371204e95SPeng Fan }
90471204e95SPeng Fan
90571204e95SPeng Fan for (; nmsgs > 0; nmsgs--, msg++) {
90671204e95SPeng Fan bool next_is_read = nmsgs > 1 && (msg[1].flags & I2C_M_RD);
90771204e95SPeng Fan debug("i2c_xfer: chip=0x%x, len=0x%x\n", msg->addr, msg->len);
90871204e95SPeng Fan if (msg->flags & I2C_M_RD)
90971204e95SPeng Fan ret = i2c_read_data(i2c_bus, msg->addr, msg->buf,
91071204e95SPeng Fan msg->len);
91171204e95SPeng Fan else {
91271204e95SPeng Fan ret = i2c_write_data(i2c_bus, msg->addr, msg->buf,
91371204e95SPeng Fan msg->len);
91471204e95SPeng Fan if (ret)
91571204e95SPeng Fan break;
91671204e95SPeng Fan if (next_is_read) {
91771204e95SPeng Fan /* Reuse ret */
91871204e95SPeng Fan ret = readb(base + (I2CR << reg_shift));
91971204e95SPeng Fan ret |= I2CR_RSTA;
92071204e95SPeng Fan writeb(ret, base + (I2CR << reg_shift));
92171204e95SPeng Fan
92271204e95SPeng Fan ret = tx_byte(i2c_bus, (msg->addr << 1) | 1);
92371204e95SPeng Fan if (ret < 0) {
92471204e95SPeng Fan i2c_imx_stop(i2c_bus);
92571204e95SPeng Fan break;
92671204e95SPeng Fan }
92771204e95SPeng Fan }
92871204e95SPeng Fan }
92971204e95SPeng Fan }
93071204e95SPeng Fan
93171204e95SPeng Fan if (ret)
93271204e95SPeng Fan debug("i2c_write: error sending\n");
93371204e95SPeng Fan
93471204e95SPeng Fan i2c_imx_stop(i2c_bus);
93571204e95SPeng Fan
93671204e95SPeng Fan return ret;
93771204e95SPeng Fan }
93871204e95SPeng Fan
93971204e95SPeng Fan static const struct dm_i2c_ops mxc_i2c_ops = {
94071204e95SPeng Fan .xfer = mxc_i2c_xfer,
94171204e95SPeng Fan .probe_chip = mxc_i2c_probe_chip,
94271204e95SPeng Fan .set_bus_speed = mxc_i2c_set_bus_speed,
94371204e95SPeng Fan };
94471204e95SPeng Fan
94571204e95SPeng Fan static const struct udevice_id mxc_i2c_ids[] = {
94671204e95SPeng Fan { .compatible = "fsl,imx21-i2c", },
94771204e95SPeng Fan { .compatible = "fsl,vf610-i2c", .data = I2C_QUIRK_FLAG, },
94871204e95SPeng Fan {}
94971204e95SPeng Fan };
95071204e95SPeng Fan
95171204e95SPeng Fan U_BOOT_DRIVER(i2c_mxc) = {
95271204e95SPeng Fan .name = "i2c_mxc",
95371204e95SPeng Fan .id = UCLASS_I2C,
95471204e95SPeng Fan .of_match = mxc_i2c_ids,
95571204e95SPeng Fan .probe = mxc_i2c_probe,
95671204e95SPeng Fan .priv_auto_alloc_size = sizeof(struct mxc_i2c_bus),
95771204e95SPeng Fan .ops = &mxc_i2c_ops,
95871204e95SPeng Fan };
95971204e95SPeng Fan #endif
960