blob: 79228c2757bde2944e0e4601374806cace55f179 [file] [log] [blame]
Sascha Hauercdace062008-03-26 20:40:49 +01001/*
Marek Vasutdb841402011-09-22 09:22:12 +00002 * i2c driver for Freescale i.MX series
Sascha Hauercdace062008-03-26 20:40:49 +01003 *
4 * (c) 2007 Pengutronix, Sascha Hauer <s.hauer@pengutronix.de>
Marek Vasutdb841402011-09-22 09:22:12 +00005 * (c) 2011 Marek Vasut <marek.vasut@gmail.com>
6 *
7 * Based on i2c-imx.c from linux kernel:
8 * Copyright (C) 2005 Torsten Koschorrek <koschorrek at synertronixx.de>
9 * Copyright (C) 2005 Matthias Blaschke <blaschke at synertronixx.de>
10 * Copyright (C) 2007 RightHand Technologies, Inc.
11 * Copyright (C) 2008 Darius Augulis <darius.augulis at teltonika.lt>
12 *
Sascha Hauercdace062008-03-26 20:40:49 +010013 *
Wolfgang Denk1a459662013-07-08 09:37:19 +020014 * SPDX-License-Identifier: GPL-2.0+
Sascha Hauercdace062008-03-26 20:40:49 +010015 */
16
17#include <common.h>
Liu Hui-R64343127cec12011-01-03 22:27:39 +000018#include <asm/arch/clock.h>
Stefano Babic86271112011-03-14 15:43:56 +010019#include <asm/arch/imx-regs.h>
Masahiro Yamada1221ce42016-09-21 11:28:55 +090020#include <linux/errno.h>
Stefano Babic552a8482017-06-29 10:16:06 +020021#include <asm/mach-imx/mxc_i2c.h>
Troy Kisky24cd7382012-07-19 08:18:03 +000022#include <asm/io.h>
Marek Vasutbf0783d2011-10-26 00:05:44 +000023#include <i2c.h>
Troy Kisky7aa57a02012-07-19 08:18:09 +000024#include <watchdog.h>
Peng Fan71204e92015-05-15 07:29:12 +080025#include <dm.h>
Peng Fane1bed802016-03-11 16:47:50 +080026#include <dm/pinctrl.h>
Peng Fan71204e92015-05-15 07:29:12 +080027#include <fdtdec.h>
Sascha Hauercdace062008-03-26 20:40:49 +010028
York Sundec18612014-02-10 14:02:52 -080029DECLARE_GLOBAL_DATA_PTR;
30
Peng Fan71204e92015-05-15 07:29:12 +080031#define I2C_QUIRK_FLAG (1 << 0)
32
33#define IMX_I2C_REGSHIFT 2
34#define VF610_I2C_REGSHIFT 0
Yuan Yao9d10c2d2016-06-08 18:24:51 +080035
36#define I2C_EARLY_INIT_INDEX 0
37#ifdef CONFIG_SYS_I2C_IFDR_DIV
38#define I2C_IFDR_DIV_CONSERVATIVE CONFIG_SYS_I2C_IFDR_DIV
39#else
40#define I2C_IFDR_DIV_CONSERVATIVE 0x7e
41#endif
42
Peng Fan71204e92015-05-15 07:29:12 +080043/* Register index */
44#define IADR 0
45#define IFDR 1
46#define I2CR 2
47#define I2SR 3
48#define I2DR 4
Sascha Hauercdace062008-03-26 20:40:49 +010049
Sascha Hauercdace062008-03-26 20:40:49 +010050#define I2CR_IIEN (1 << 6)
51#define I2CR_MSTA (1 << 5)
52#define I2CR_MTX (1 << 4)
53#define I2CR_TX_NO_AK (1 << 3)
54#define I2CR_RSTA (1 << 2)
55
56#define I2SR_ICF (1 << 7)
57#define I2SR_IBB (1 << 5)
Troy Kiskyd5383a62012-07-19 08:18:15 +000058#define I2SR_IAL (1 << 4)
Sascha Hauercdace062008-03-26 20:40:49 +010059#define I2SR_IIF (1 << 1)
60#define I2SR_RX_NO_AK (1 << 0)
61
Alison Wang30ea41a2013-06-17 15:30:39 +080062#ifdef I2C_QUIRK_REG
63#define I2CR_IEN (0 << 7)
64#define I2CR_IDIS (1 << 7)
65#define I2SR_IIF_CLEAR (1 << 1)
66#else
67#define I2CR_IEN (1 << 7)
68#define I2CR_IDIS (0 << 7)
69#define I2SR_IIF_CLEAR (0 << 1)
70#endif
71
Alison Wang30ea41a2013-06-17 15:30:39 +080072#ifdef I2C_QUIRK_REG
73static u16 i2c_clk_div[60][2] = {
74 { 20, 0x00 }, { 22, 0x01 }, { 24, 0x02 }, { 26, 0x03 },
75 { 28, 0x04 }, { 30, 0x05 }, { 32, 0x09 }, { 34, 0x06 },
76 { 36, 0x0A }, { 40, 0x07 }, { 44, 0x0C }, { 48, 0x0D },
77 { 52, 0x43 }, { 56, 0x0E }, { 60, 0x45 }, { 64, 0x12 },
78 { 68, 0x0F }, { 72, 0x13 }, { 80, 0x14 }, { 88, 0x15 },
79 { 96, 0x19 }, { 104, 0x16 }, { 112, 0x1A }, { 128, 0x17 },
80 { 136, 0x4F }, { 144, 0x1C }, { 160, 0x1D }, { 176, 0x55 },
81 { 192, 0x1E }, { 208, 0x56 }, { 224, 0x22 }, { 228, 0x24 },
82 { 240, 0x1F }, { 256, 0x23 }, { 288, 0x5C }, { 320, 0x25 },
83 { 384, 0x26 }, { 448, 0x2A }, { 480, 0x27 }, { 512, 0x2B },
84 { 576, 0x2C }, { 640, 0x2D }, { 768, 0x31 }, { 896, 0x32 },
85 { 960, 0x2F }, { 1024, 0x33 }, { 1152, 0x34 }, { 1280, 0x35 },
86 { 1536, 0x36 }, { 1792, 0x3A }, { 1920, 0x37 }, { 2048, 0x3B },
87 { 2304, 0x3C }, { 2560, 0x3D }, { 3072, 0x3E }, { 3584, 0x7A },
88 { 3840, 0x3F }, { 4096, 0x7B }, { 5120, 0x7D }, { 6144, 0x7E },
89};
90#else
Marek Vasutdb841402011-09-22 09:22:12 +000091static u16 i2c_clk_div[50][2] = {
92 { 22, 0x20 }, { 24, 0x21 }, { 26, 0x22 }, { 28, 0x23 },
93 { 30, 0x00 }, { 32, 0x24 }, { 36, 0x25 }, { 40, 0x26 },
94 { 42, 0x03 }, { 44, 0x27 }, { 48, 0x28 }, { 52, 0x05 },
95 { 56, 0x29 }, { 60, 0x06 }, { 64, 0x2A }, { 72, 0x2B },
96 { 80, 0x2C }, { 88, 0x09 }, { 96, 0x2D }, { 104, 0x0A },
97 { 112, 0x2E }, { 128, 0x2F }, { 144, 0x0C }, { 160, 0x30 },
98 { 192, 0x31 }, { 224, 0x32 }, { 240, 0x0F }, { 256, 0x33 },
99 { 288, 0x10 }, { 320, 0x34 }, { 384, 0x35 }, { 448, 0x36 },
100 { 480, 0x13 }, { 512, 0x37 }, { 576, 0x14 }, { 640, 0x38 },
101 { 768, 0x39 }, { 896, 0x3A }, { 960, 0x17 }, { 1024, 0x3B },
102 { 1152, 0x18 }, { 1280, 0x3C }, { 1536, 0x3D }, { 1792, 0x3E },
103 { 1920, 0x1B }, { 2048, 0x3F }, { 2304, 0x1C }, { 2560, 0x1D },
104 { 3072, 0x1E }, { 3840, 0x1F }
105};
Alison Wang30ea41a2013-06-17 15:30:39 +0800106#endif
Sascha Hauercdace062008-03-26 20:40:49 +0100107
tremfac96402013-09-21 18:13:35 +0200108#ifndef CONFIG_SYS_MXC_I2C1_SPEED
109#define CONFIG_SYS_MXC_I2C1_SPEED 100000
110#endif
111#ifndef CONFIG_SYS_MXC_I2C2_SPEED
112#define CONFIG_SYS_MXC_I2C2_SPEED 100000
113#endif
114#ifndef CONFIG_SYS_MXC_I2C3_SPEED
115#define CONFIG_SYS_MXC_I2C3_SPEED 100000
116#endif
York Sunf8cb1012015-03-20 10:20:40 -0700117#ifndef CONFIG_SYS_MXC_I2C4_SPEED
118#define CONFIG_SYS_MXC_I2C4_SPEED 100000
119#endif
tremfac96402013-09-21 18:13:35 +0200120
121#ifndef CONFIG_SYS_MXC_I2C1_SLAVE
122#define CONFIG_SYS_MXC_I2C1_SLAVE 0
123#endif
124#ifndef CONFIG_SYS_MXC_I2C2_SLAVE
125#define CONFIG_SYS_MXC_I2C2_SLAVE 0
126#endif
127#ifndef CONFIG_SYS_MXC_I2C3_SLAVE
128#define CONFIG_SYS_MXC_I2C3_SLAVE 0
129#endif
York Sunf8cb1012015-03-20 10:20:40 -0700130#ifndef CONFIG_SYS_MXC_I2C4_SLAVE
131#define CONFIG_SYS_MXC_I2C4_SLAVE 0
132#endif
tremfac96402013-09-21 18:13:35 +0200133
Marek Vasutdb841402011-09-22 09:22:12 +0000134/*
135 * Calculate and set proper clock divider
136 */
Peng Fan71204e92015-05-15 07:29:12 +0800137static uint8_t i2c_imx_get_clk(struct mxc_i2c_bus *i2c_bus, unsigned int rate)
Stefano Babic1d549ad2011-01-20 07:50:44 +0000138{
Marek Vasutdb841402011-09-22 09:22:12 +0000139 unsigned int i2c_clk_rate;
140 unsigned int div;
Marek Vasutbf0783d2011-10-26 00:05:44 +0000141 u8 clk_div;
Sascha Hauercdace062008-03-26 20:40:49 +0100142
Liu Hui-R64343127cec12011-01-03 22:27:39 +0000143#if defined(CONFIG_MX31)
Stefano Babic1d549ad2011-01-20 07:50:44 +0000144 struct clock_control_regs *sc_regs =
145 (struct clock_control_regs *)CCM_BASE;
Marek Vasutdb841402011-09-22 09:22:12 +0000146
Guennadi Liakhovetskie7de18a2009-02-13 09:23:36 +0100147 /* start the required I2C clock */
Troy Kiskyde6f6042012-04-24 17:33:25 +0000148 writel(readl(&sc_regs->cgr0) | (3 << CONFIG_SYS_I2C_CLK_OFFSET),
Stefano Babic1d549ad2011-01-20 07:50:44 +0000149 &sc_regs->cgr0);
Liu Hui-R64343127cec12011-01-03 22:27:39 +0000150#endif
Guennadi Liakhovetskie7de18a2009-02-13 09:23:36 +0100151
Marek Vasutdb841402011-09-22 09:22:12 +0000152 /* Divider value calculation */
Matthias Weissere7bed5c2012-09-24 02:46:53 +0000153 i2c_clk_rate = mxc_get_clock(MXC_I2C_CLK);
Marek Vasutdb841402011-09-22 09:22:12 +0000154 div = (i2c_clk_rate + rate - 1) / rate;
155 if (div < i2c_clk_div[0][0])
Marek Vasutb567b8f2011-09-27 06:34:11 +0000156 clk_div = 0;
Marek Vasutdb841402011-09-22 09:22:12 +0000157 else if (div > i2c_clk_div[ARRAY_SIZE(i2c_clk_div) - 1][0])
Marek Vasutb567b8f2011-09-27 06:34:11 +0000158 clk_div = ARRAY_SIZE(i2c_clk_div) - 1;
Marek Vasutdb841402011-09-22 09:22:12 +0000159 else
Marek Vasutb567b8f2011-09-27 06:34:11 +0000160 for (clk_div = 0; i2c_clk_div[clk_div][0] < div; clk_div++)
Marek Vasutdb841402011-09-22 09:22:12 +0000161 ;
Sascha Hauercdace062008-03-26 20:40:49 +0100162
Marek Vasutdb841402011-09-22 09:22:12 +0000163 /* Store divider value */
Marek Vasutbf0783d2011-10-26 00:05:44 +0000164 return clk_div;
Marek Vasutdb841402011-09-22 09:22:12 +0000165}
Sascha Hauercdace062008-03-26 20:40:49 +0100166
Marek Vasutdb841402011-09-22 09:22:12 +0000167/*
Troy Kiskye4ff5252012-07-19 08:18:18 +0000168 * Set I2C Bus speed
Marek Vasutdb841402011-09-22 09:22:12 +0000169 */
Peng Fan71204e92015-05-15 07:29:12 +0800170static int bus_i2c_set_bus_speed(struct mxc_i2c_bus *i2c_bus, int speed)
Marek Vasutdb841402011-09-22 09:22:12 +0000171{
Peng Fan71204e92015-05-15 07:29:12 +0800172 ulong base = i2c_bus->base;
173 bool quirk = i2c_bus->driver_data & I2C_QUIRK_FLAG ? true : false;
174 u8 clk_idx = i2c_imx_get_clk(i2c_bus, speed);
Marek Vasutbf0783d2011-10-26 00:05:44 +0000175 u8 idx = i2c_clk_div[clk_idx][1];
Peng Fan71204e92015-05-15 07:29:12 +0800176 int reg_shift = quirk ? VF610_I2C_REGSHIFT : IMX_I2C_REGSHIFT;
Marek Vasutbf0783d2011-10-26 00:05:44 +0000177
Heiko Schochere6c8b712015-05-18 10:58:12 +0200178 if (!base)
Simon Glass7c843192017-09-17 16:54:53 -0600179 return -EINVAL;
Heiko Schochere6c8b712015-05-18 10:58:12 +0200180
Marek Vasutbf0783d2011-10-26 00:05:44 +0000181 /* Store divider value */
Peng Fan71204e92015-05-15 07:29:12 +0800182 writeb(idx, base + (IFDR << reg_shift));
Marek Vasutbf0783d2011-10-26 00:05:44 +0000183
Troy Kisky83a1a192012-07-19 08:18:12 +0000184 /* Reset module */
Peng Fan71204e92015-05-15 07:29:12 +0800185 writeb(I2CR_IDIS, base + (I2CR << reg_shift));
186 writeb(0, base + (I2SR << reg_shift));
Marek Vasutb567b8f2011-09-27 06:34:11 +0000187 return 0;
188}
189
Troy Kisky7aa57a02012-07-19 08:18:09 +0000190#define ST_BUS_IDLE (0 | (I2SR_IBB << 8))
191#define ST_BUS_BUSY (I2SR_IBB | (I2SR_IBB << 8))
192#define ST_IIF (I2SR_IIF | (I2SR_IIF << 8))
193
Peng Fan71204e92015-05-15 07:29:12 +0800194static int wait_for_sr_state(struct mxc_i2c_bus *i2c_bus, unsigned state)
Stefano Babic81687212011-01-20 07:51:31 +0000195{
Troy Kisky7aa57a02012-07-19 08:18:09 +0000196 unsigned sr;
197 ulong elapsed;
Peng Fan71204e92015-05-15 07:29:12 +0800198 bool quirk = i2c_bus->driver_data & I2C_QUIRK_FLAG ? true : false;
199 int reg_shift = quirk ? VF610_I2C_REGSHIFT : IMX_I2C_REGSHIFT;
200 ulong base = i2c_bus->base;
Troy Kisky7aa57a02012-07-19 08:18:09 +0000201 ulong start_time = get_timer(0);
202 for (;;) {
Peng Fan71204e92015-05-15 07:29:12 +0800203 sr = readb(base + (I2SR << reg_shift));
Troy Kiskyd5383a62012-07-19 08:18:15 +0000204 if (sr & I2SR_IAL) {
Peng Fan71204e92015-05-15 07:29:12 +0800205 if (quirk)
206 writeb(sr | I2SR_IAL, base +
207 (I2SR << reg_shift));
208 else
209 writeb(sr & ~I2SR_IAL, base +
210 (I2SR << reg_shift));
Troy Kiskyd5383a62012-07-19 08:18:15 +0000211 printf("%s: Arbitration lost sr=%x cr=%x state=%x\n",
Peng Fan71204e92015-05-15 07:29:12 +0800212 __func__, sr, readb(base + (I2CR << reg_shift)),
213 state);
Troy Kiskyd5383a62012-07-19 08:18:15 +0000214 return -ERESTART;
215 }
Troy Kisky7aa57a02012-07-19 08:18:09 +0000216 if ((sr & (state >> 8)) == (unsigned char)state)
217 return sr;
218 WATCHDOG_RESET();
219 elapsed = get_timer(start_time);
220 if (elapsed > (CONFIG_SYS_HZ / 10)) /* .1 seconds */
221 break;
Stefano Babic81687212011-01-20 07:51:31 +0000222 }
Troy Kisky7aa57a02012-07-19 08:18:09 +0000223 printf("%s: failed sr=%x cr=%x state=%x\n", __func__,
Peng Fan71204e92015-05-15 07:29:12 +0800224 sr, readb(base + (I2CR << reg_shift)), state);
Troy Kiskycea60b02012-07-19 08:18:04 +0000225 return -ETIMEDOUT;
Stefano Babic81687212011-01-20 07:51:31 +0000226}
227
Peng Fan71204e92015-05-15 07:29:12 +0800228static int tx_byte(struct mxc_i2c_bus *i2c_bus, u8 byte)
Sascha Hauercdace062008-03-26 20:40:49 +0100229{
Troy Kiskycea60b02012-07-19 08:18:04 +0000230 int ret;
Peng Fan71204e92015-05-15 07:29:12 +0800231 int reg_shift = i2c_bus->driver_data & I2C_QUIRK_FLAG ?
232 VF610_I2C_REGSHIFT : IMX_I2C_REGSHIFT;
233 ulong base = i2c_bus->base;
Sascha Hauercdace062008-03-26 20:40:49 +0100234
Peng Fan71204e92015-05-15 07:29:12 +0800235 writeb(I2SR_IIF_CLEAR, base + (I2SR << reg_shift));
236 writeb(byte, base + (I2DR << reg_shift));
237
238 ret = wait_for_sr_state(i2c_bus, ST_IIF);
Troy Kiskycea60b02012-07-19 08:18:04 +0000239 if (ret < 0)
240 return ret;
Troy Kiskycea60b02012-07-19 08:18:04 +0000241 if (ret & I2SR_RX_NO_AK)
Simon Glass7c843192017-09-17 16:54:53 -0600242 return -EREMOTEIO;
Troy Kiskycea60b02012-07-19 08:18:04 +0000243 return 0;
Marek Vasutdb841402011-09-22 09:22:12 +0000244}
245
246/*
Peng Fan71204e92015-05-15 07:29:12 +0800247 * Stub implementations for outer i2c slave operations.
248 */
249void __i2c_force_reset_slave(void)
250{
251}
252void i2c_force_reset_slave(void)
253 __attribute__((weak, alias("__i2c_force_reset_slave")));
254
255/*
Troy Kisky90a5b702012-07-19 08:18:13 +0000256 * Stop I2C transaction
Marek Vasutdb841402011-09-22 09:22:12 +0000257 */
Peng Fan71204e92015-05-15 07:29:12 +0800258static void i2c_imx_stop(struct mxc_i2c_bus *i2c_bus)
Sascha Hauercdace062008-03-26 20:40:49 +0100259{
Troy Kisky7aa57a02012-07-19 08:18:09 +0000260 int ret;
Peng Fan71204e92015-05-15 07:29:12 +0800261 int reg_shift = i2c_bus->driver_data & I2C_QUIRK_FLAG ?
262 VF610_I2C_REGSHIFT : IMX_I2C_REGSHIFT;
263 ulong base = i2c_bus->base;
264 unsigned int temp = readb(base + (I2CR << reg_shift));
Sascha Hauercdace062008-03-26 20:40:49 +0100265
Troy Kisky1c076db2012-07-19 08:18:02 +0000266 temp &= ~(I2CR_MSTA | I2CR_MTX);
Peng Fan71204e92015-05-15 07:29:12 +0800267 writeb(temp, base + (I2CR << reg_shift));
268 ret = wait_for_sr_state(i2c_bus, ST_BUS_IDLE);
Troy Kisky7aa57a02012-07-19 08:18:09 +0000269 if (ret < 0)
270 printf("%s:trigger stop failed\n", __func__);
Sascha Hauercdace062008-03-26 20:40:49 +0100271}
272
Marek Vasutdb841402011-09-22 09:22:12 +0000273/*
Troy Kiskyb230ddc2012-07-19 08:18:06 +0000274 * Send start signal, chip address and
275 * write register address
Marek Vasutdb841402011-09-22 09:22:12 +0000276 */
Peng Fan71204e92015-05-15 07:29:12 +0800277static int i2c_init_transfer_(struct mxc_i2c_bus *i2c_bus, u8 chip,
278 u32 addr, int alen)
Sascha Hauercdace062008-03-26 20:40:49 +0100279{
Troy Kisky71e9f3c2012-07-19 08:18:11 +0000280 unsigned int temp;
281 int ret;
Peng Fan71204e92015-05-15 07:29:12 +0800282 bool quirk = i2c_bus->driver_data & I2C_QUIRK_FLAG ? true : false;
283 ulong base = i2c_bus->base;
284 int reg_shift = quirk ? VF610_I2C_REGSHIFT : IMX_I2C_REGSHIFT;
285
286 /* Reset i2c slave */
287 i2c_force_reset_slave();
Troy Kisky71e9f3c2012-07-19 08:18:11 +0000288
289 /* Enable I2C controller */
Peng Fan71204e92015-05-15 07:29:12 +0800290 if (quirk)
291 ret = readb(base + (I2CR << reg_shift)) & I2CR_IDIS;
292 else
293 ret = !(readb(base + (I2CR << reg_shift)) & I2CR_IEN);
294
295 if (ret) {
296 writeb(I2CR_IEN, base + (I2CR << reg_shift));
Troy Kisky90a5b702012-07-19 08:18:13 +0000297 /* Wait for controller to be stable */
298 udelay(50);
299 }
Peng Fan71204e92015-05-15 07:29:12 +0800300
301 if (readb(base + (IADR << reg_shift)) == (chip << 1))
302 writeb((chip << 1) ^ 2, base + (IADR << reg_shift));
303 writeb(I2SR_IIF_CLEAR, base + (I2SR << reg_shift));
304 ret = wait_for_sr_state(i2c_bus, ST_BUS_IDLE);
Troy Kisky90a5b702012-07-19 08:18:13 +0000305 if (ret < 0)
Troy Kiskya7f1a002012-07-19 08:18:16 +0000306 return ret;
Troy Kisky71e9f3c2012-07-19 08:18:11 +0000307
308 /* Start I2C transaction */
Peng Fan71204e92015-05-15 07:29:12 +0800309 temp = readb(base + (I2CR << reg_shift));
Troy Kisky71e9f3c2012-07-19 08:18:11 +0000310 temp |= I2CR_MSTA;
Peng Fan71204e92015-05-15 07:29:12 +0800311 writeb(temp, base + (I2CR << reg_shift));
Troy Kisky71e9f3c2012-07-19 08:18:11 +0000312
Peng Fan71204e92015-05-15 07:29:12 +0800313 ret = wait_for_sr_state(i2c_bus, ST_BUS_BUSY);
Troy Kisky71e9f3c2012-07-19 08:18:11 +0000314 if (ret < 0)
Troy Kiskya7f1a002012-07-19 08:18:16 +0000315 return ret;
Troy Kiskyb230ddc2012-07-19 08:18:06 +0000316
Troy Kisky71e9f3c2012-07-19 08:18:11 +0000317 temp |= I2CR_MTX | I2CR_TX_NO_AK;
Peng Fan71204e92015-05-15 07:29:12 +0800318 writeb(temp, base + (I2CR << reg_shift));
Troy Kisky71e9f3c2012-07-19 08:18:11 +0000319
Nandor Han2feec4e2017-11-08 15:35:09 +0000320 if (alen >= 0) {
321 /* write slave address */
322 ret = tx_byte(i2c_bus, chip << 1);
Troy Kiskycea60b02012-07-19 08:18:04 +0000323 if (ret < 0)
Troy Kiskya7f1a002012-07-19 08:18:16 +0000324 return ret;
Nandor Han2feec4e2017-11-08 15:35:09 +0000325
326 while (alen--) {
327 ret = tx_byte(i2c_bus, (addr >> (alen * 8)) & 0xff);
328 if (ret < 0)
329 return ret;
330 }
Stefano Babic81687212011-01-20 07:51:31 +0000331 }
Nandor Han2feec4e2017-11-08 15:35:09 +0000332
Troy Kiskyb230ddc2012-07-19 08:18:06 +0000333 return 0;
Troy Kiskya7f1a002012-07-19 08:18:16 +0000334}
335
Peng Fan71204e92015-05-15 07:29:12 +0800336#ifndef CONFIG_DM_I2C
337int i2c_idle_bus(struct mxc_i2c_bus *i2c_bus)
338{
339 if (i2c_bus && i2c_bus->idle_bus_fn)
340 return i2c_bus->idle_bus_fn(i2c_bus->idle_bus_data);
341 return 0;
342}
343#else
344/*
Peng Fane1bed802016-03-11 16:47:50 +0800345 * See Linux Documentation/devicetree/bindings/i2c/i2c-imx.txt
346 * "
347 * scl-gpios: specify the gpio related to SCL pin
348 * sda-gpios: specify the gpio related to SDA pin
349 * add pinctrl to configure i2c pins to gpio function for i2c
350 * bus recovery, call it "gpio" state
351 * "
352 *
353 * The i2c_idle_bus is an implementation following Linux Kernel.
Peng Fan71204e92015-05-15 07:29:12 +0800354 */
Peng Fan71204e92015-05-15 07:29:12 +0800355int i2c_idle_bus(struct mxc_i2c_bus *i2c_bus)
Peng Fane1bed802016-03-11 16:47:50 +0800356{
357 struct udevice *bus = i2c_bus->bus;
358 struct gpio_desc *scl_gpio = &i2c_bus->scl_gpio;
359 struct gpio_desc *sda_gpio = &i2c_bus->sda_gpio;
360 int sda, scl;
361 int i, ret = 0;
362 ulong elapsed, start_time;
363
364 if (pinctrl_select_state(bus, "gpio")) {
365 dev_dbg(bus, "Can not to switch to use gpio pinmux\n");
366 /*
367 * GPIO pinctrl for i2c force idle is not a must,
368 * but it is strongly recommended to be used.
369 * Because it can help you to recover from bad
370 * i2c bus state. Do not return failure, because
371 * it is not a must.
372 */
373 return 0;
374 }
375
376 dm_gpio_set_dir_flags(scl_gpio, GPIOD_IS_IN);
377 dm_gpio_set_dir_flags(sda_gpio, GPIOD_IS_IN);
378 scl = dm_gpio_get_value(scl_gpio);
379 sda = dm_gpio_get_value(sda_gpio);
380
381 if ((sda & scl) == 1)
382 goto exit; /* Bus is idle already */
383
384 /* Send high and low on the SCL line */
385 for (i = 0; i < 9; i++) {
386 dm_gpio_set_dir_flags(scl_gpio, GPIOD_IS_OUT);
387 dm_gpio_set_value(scl_gpio, 0);
388 udelay(50);
389 dm_gpio_set_dir_flags(scl_gpio, GPIOD_IS_IN);
390 udelay(50);
391 }
392 start_time = get_timer(0);
393 for (;;) {
394 dm_gpio_set_dir_flags(scl_gpio, GPIOD_IS_IN);
395 dm_gpio_set_dir_flags(sda_gpio, GPIOD_IS_IN);
396 scl = dm_gpio_get_value(scl_gpio);
397 sda = dm_gpio_get_value(sda_gpio);
398 if ((sda & scl) == 1)
399 break;
400 WATCHDOG_RESET();
401 elapsed = get_timer(start_time);
402 if (elapsed > (CONFIG_SYS_HZ / 5)) { /* .2 seconds */
403 ret = -EBUSY;
404 printf("%s: failed to clear bus, sda=%d scl=%d\n", __func__, sda, scl);
405 break;
406 }
407 }
408
409exit:
410 pinctrl_select_state(bus, "default");
411 return ret;
412}
Peng Fan71204e92015-05-15 07:29:12 +0800413#endif
414
415static int i2c_init_transfer(struct mxc_i2c_bus *i2c_bus, u8 chip,
416 u32 addr, int alen)
Troy Kiskya7f1a002012-07-19 08:18:16 +0000417{
418 int retry;
419 int ret;
Peng Fan71204e92015-05-15 07:29:12 +0800420 int reg_shift = i2c_bus->driver_data & I2C_QUIRK_FLAG ?
421 VF610_I2C_REGSHIFT : IMX_I2C_REGSHIFT;
Heiko Schochere6c8b712015-05-18 10:58:12 +0200422
423 if (!i2c_bus->base)
Simon Glass7c843192017-09-17 16:54:53 -0600424 return -EINVAL;
Heiko Schochere6c8b712015-05-18 10:58:12 +0200425
Troy Kiskya7f1a002012-07-19 08:18:16 +0000426 for (retry = 0; retry < 3; retry++) {
Peng Fan71204e92015-05-15 07:29:12 +0800427 ret = i2c_init_transfer_(i2c_bus, chip, addr, alen);
Troy Kiskya7f1a002012-07-19 08:18:16 +0000428 if (ret >= 0)
429 return 0;
Peng Fan71204e92015-05-15 07:29:12 +0800430 i2c_imx_stop(i2c_bus);
Simon Glass7c843192017-09-17 16:54:53 -0600431 if (ret == -EREMOTEIO)
Troy Kiskya7f1a002012-07-19 08:18:16 +0000432 return ret;
433
434 printf("%s: failed for chip 0x%x retry=%d\n", __func__, chip,
435 retry);
436 if (ret != -ERESTART)
Alison Wang30ea41a2013-06-17 15:30:39 +0800437 /* Disable controller */
Peng Fan71204e92015-05-15 07:29:12 +0800438 writeb(I2CR_IDIS, i2c_bus->base + (I2CR << reg_shift));
Troy Kiskya7f1a002012-07-19 08:18:16 +0000439 udelay(100);
Peng Fan71204e92015-05-15 07:29:12 +0800440 if (i2c_idle_bus(i2c_bus) < 0)
Troy Kisky96c19bd2012-07-19 08:18:19 +0000441 break;
Troy Kiskya7f1a002012-07-19 08:18:16 +0000442 }
Peng Fan71204e92015-05-15 07:29:12 +0800443 printf("%s: give up i2c_regs=0x%lx\n", __func__, i2c_bus->base);
Marek Vasutdb841402011-09-22 09:22:12 +0000444 return ret;
Sascha Hauercdace062008-03-26 20:40:49 +0100445}
446
Peng Fan71204e92015-05-15 07:29:12 +0800447
448static int i2c_write_data(struct mxc_i2c_bus *i2c_bus, u8 chip, const u8 *buf,
449 int len)
450{
451 int i, ret = 0;
452
453 debug("i2c_write_data: chip=0x%x, len=0x%x\n", chip, len);
454 debug("write_data: ");
455 /* use rc for counter */
456 for (i = 0; i < len; ++i)
457 debug(" 0x%02x", buf[i]);
458 debug("\n");
459
460 for (i = 0; i < len; i++) {
461 ret = tx_byte(i2c_bus, buf[i]);
462 if (ret < 0) {
463 debug("i2c_write_data(): rc=%d\n", ret);
464 break;
465 }
466 }
467
468 return ret;
469}
470
471static int i2c_read_data(struct mxc_i2c_bus *i2c_bus, uchar chip, uchar *buf,
472 int len)
Marek Vasutdb841402011-09-22 09:22:12 +0000473{
Marek Vasutdb841402011-09-22 09:22:12 +0000474 int ret;
475 unsigned int temp;
476 int i;
Peng Fan71204e92015-05-15 07:29:12 +0800477 int reg_shift = i2c_bus->driver_data & I2C_QUIRK_FLAG ?
478 VF610_I2C_REGSHIFT : IMX_I2C_REGSHIFT;
479 ulong base = i2c_bus->base;
Marek Vasutdb841402011-09-22 09:22:12 +0000480
Peng Fan71204e92015-05-15 07:29:12 +0800481 debug("i2c_read_data: chip=0x%x, len=0x%x\n", chip, len);
Marek Vasutdb841402011-09-22 09:22:12 +0000482
483 /* setup bus to read data */
Peng Fan71204e92015-05-15 07:29:12 +0800484 temp = readb(base + (I2CR << reg_shift));
Marek Vasutdb841402011-09-22 09:22:12 +0000485 temp &= ~(I2CR_MTX | I2CR_TX_NO_AK);
486 if (len == 1)
487 temp |= I2CR_TX_NO_AK;
Peng Fan71204e92015-05-15 07:29:12 +0800488 writeb(temp, base + (I2CR << reg_shift));
489 writeb(I2SR_IIF_CLEAR, base + (I2SR << reg_shift));
490 /* dummy read to clear ICF */
491 readb(base + (I2DR << reg_shift));
Marek Vasutdb841402011-09-22 09:22:12 +0000492
493 /* read data */
494 for (i = 0; i < len; i++) {
Peng Fan71204e92015-05-15 07:29:12 +0800495 ret = wait_for_sr_state(i2c_bus, ST_IIF);
Troy Kisky7aa57a02012-07-19 08:18:09 +0000496 if (ret < 0) {
Peng Fan71204e92015-05-15 07:29:12 +0800497 debug("i2c_read_data(): ret=%d\n", ret);
498 i2c_imx_stop(i2c_bus);
Marek Vasutdb841402011-09-22 09:22:12 +0000499 return ret;
Troy Kiskyc4330d22012-07-19 08:18:07 +0000500 }
Marek Vasutdb841402011-09-22 09:22:12 +0000501
502 /*
503 * It must generate STOP before read I2DR to prevent
504 * controller from generating another clock cycle
505 */
506 if (i == (len - 1)) {
Peng Fan71204e92015-05-15 07:29:12 +0800507 i2c_imx_stop(i2c_bus);
Marek Vasutdb841402011-09-22 09:22:12 +0000508 } else if (i == (len - 2)) {
Peng Fan71204e92015-05-15 07:29:12 +0800509 temp = readb(base + (I2CR << reg_shift));
Marek Vasutdb841402011-09-22 09:22:12 +0000510 temp |= I2CR_TX_NO_AK;
Peng Fan71204e92015-05-15 07:29:12 +0800511 writeb(temp, base + (I2CR << reg_shift));
Marek Vasutdb841402011-09-22 09:22:12 +0000512 }
Peng Fan71204e92015-05-15 07:29:12 +0800513 writeb(I2SR_IIF_CLEAR, base + (I2SR << reg_shift));
514 buf[i] = readb(base + (I2DR << reg_shift));
Marek Vasutdb841402011-09-22 09:22:12 +0000515 }
Peng Fan71204e92015-05-15 07:29:12 +0800516
517 /* reuse ret for counter*/
518 for (ret = 0; ret < len; ++ret)
519 debug(" 0x%02x", buf[ret]);
520 debug("\n");
521
522 i2c_imx_stop(i2c_bus);
Troy Kisky7aa57a02012-07-19 08:18:09 +0000523 return 0;
Marek Vasutdb841402011-09-22 09:22:12 +0000524}
525
Peng Fan71204e92015-05-15 07:29:12 +0800526#ifndef CONFIG_DM_I2C
527/*
528 * Read data from I2C device
529 */
530static int bus_i2c_read(struct mxc_i2c_bus *i2c_bus, u8 chip, u32 addr,
531 int alen, u8 *buf, int len)
532{
533 int ret = 0;
534 u32 temp;
535 int reg_shift = i2c_bus->driver_data & I2C_QUIRK_FLAG ?
536 VF610_I2C_REGSHIFT : IMX_I2C_REGSHIFT;
537 ulong base = i2c_bus->base;
538
539 ret = i2c_init_transfer(i2c_bus, chip, addr, alen);
540 if (ret < 0)
541 return ret;
542
Nandor Han2feec4e2017-11-08 15:35:09 +0000543 if (alen >= 0) {
544 temp = readb(base + (I2CR << reg_shift));
545 temp |= I2CR_RSTA;
546 writeb(temp, base + (I2CR << reg_shift));
547 }
Peng Fan71204e92015-05-15 07:29:12 +0800548
549 ret = tx_byte(i2c_bus, (chip << 1) | 1);
550 if (ret < 0) {
551 i2c_imx_stop(i2c_bus);
552 return ret;
553 }
554
555 ret = i2c_read_data(i2c_bus, chip, buf, len);
556
557 i2c_imx_stop(i2c_bus);
558 return ret;
559}
560
Marek Vasutdb841402011-09-22 09:22:12 +0000561/*
562 * Write data to I2C device
563 */
Peng Fan71204e92015-05-15 07:29:12 +0800564static int bus_i2c_write(struct mxc_i2c_bus *i2c_bus, u8 chip, u32 addr,
565 int alen, const u8 *buf, int len)
Sascha Hauercdace062008-03-26 20:40:49 +0100566{
Peng Fan71204e92015-05-15 07:29:12 +0800567 int ret = 0;
Sascha Hauercdace062008-03-26 20:40:49 +0100568
Peng Fan71204e92015-05-15 07:29:12 +0800569 ret = i2c_init_transfer(i2c_bus, chip, addr, alen);
Troy Kiskycea60b02012-07-19 08:18:04 +0000570 if (ret < 0)
Marek Vasutdb841402011-09-22 09:22:12 +0000571 return ret;
Sascha Hauercdace062008-03-26 20:40:49 +0100572
Peng Fan71204e92015-05-15 07:29:12 +0800573 ret = i2c_write_data(i2c_bus, chip, buf, len);
574
575 i2c_imx_stop(i2c_bus);
576
Marek Vasutdb841402011-09-22 09:22:12 +0000577 return ret;
Sascha Hauercdace062008-03-26 20:40:49 +0100578}
Troy Kiskycfbb88d2012-07-19 08:18:08 +0000579
Heiko Schochere6c8b712015-05-18 10:58:12 +0200580#if !defined(I2C2_BASE_ADDR)
581#define I2C2_BASE_ADDR 0
Heiko Schocher21a26942015-05-18 10:56:24 +0200582#endif
Heiko Schochere6c8b712015-05-18 10:58:12 +0200583
584#if !defined(I2C3_BASE_ADDR)
585#define I2C3_BASE_ADDR 0
586#endif
587
588#if !defined(I2C4_BASE_ADDR)
589#define I2C4_BASE_ADDR 0
590#endif
591
592static struct mxc_i2c_bus mxc_i2c_buses[] = {
York Sun73fb5832017-03-27 11:41:03 -0700593#if defined(CONFIG_ARCH_LS1021A) || defined(CONFIG_VF610) || \
Mingkai Hu9f3183d2015-10-26 19:47:50 +0800594 defined(CONFIG_FSL_LAYERSCAPE)
Peng Fan71204e92015-05-15 07:29:12 +0800595 { 0, I2C1_BASE_ADDR, I2C_QUIRK_FLAG },
596 { 1, I2C2_BASE_ADDR, I2C_QUIRK_FLAG },
597 { 2, I2C3_BASE_ADDR, I2C_QUIRK_FLAG },
598 { 3, I2C4_BASE_ADDR, I2C_QUIRK_FLAG },
Troy Kiskye4ff5252012-07-19 08:18:18 +0000599#else
Heiko Schochere6c8b712015-05-18 10:58:12 +0200600 { 0, I2C1_BASE_ADDR, 0 },
601 { 1, I2C2_BASE_ADDR, 0 },
602 { 2, I2C3_BASE_ADDR, 0 },
603 { 3, I2C4_BASE_ADDR, 0 },
Troy Kiskye4ff5252012-07-19 08:18:18 +0000604#endif
tremfac96402013-09-21 18:13:35 +0200605};
606
Peng Fan71204e92015-05-15 07:29:12 +0800607struct mxc_i2c_bus *i2c_get_base(struct i2c_adapter *adap)
tremfac96402013-09-21 18:13:35 +0200608{
Peng Fan71204e92015-05-15 07:29:12 +0800609 return &mxc_i2c_buses[adap->hwadapnr];
Troy Kisky96c19bd2012-07-19 08:18:19 +0000610}
611
tremfac96402013-09-21 18:13:35 +0200612static int mxc_i2c_read(struct i2c_adapter *adap, uint8_t chip,
613 uint addr, int alen, uint8_t *buffer,
614 int len)
Troy Kisky98153262012-07-19 08:18:20 +0000615{
tremfac96402013-09-21 18:13:35 +0200616 return bus_i2c_read(i2c_get_base(adap), chip, addr, alen, buffer, len);
Troy Kisky98153262012-07-19 08:18:20 +0000617}
618
tremfac96402013-09-21 18:13:35 +0200619static int mxc_i2c_write(struct i2c_adapter *adap, uint8_t chip,
620 uint addr, int alen, uint8_t *buffer,
621 int len)
Troy Kisky98153262012-07-19 08:18:20 +0000622{
tremfac96402013-09-21 18:13:35 +0200623 return bus_i2c_write(i2c_get_base(adap), chip, addr, alen, buffer, len);
Troy Kiskye4ff5252012-07-19 08:18:18 +0000624}
625
Troy Kiskycfbb88d2012-07-19 08:18:08 +0000626/*
627 * Test if a chip at a given address responds (probe the chip)
628 */
tremfac96402013-09-21 18:13:35 +0200629static int mxc_i2c_probe(struct i2c_adapter *adap, uint8_t chip)
Troy Kiskycfbb88d2012-07-19 08:18:08 +0000630{
tremfac96402013-09-21 18:13:35 +0200631 return bus_i2c_write(i2c_get_base(adap), chip, 0, 0, NULL, 0);
Troy Kiskye4ff5252012-07-19 08:18:18 +0000632}
633
Peng Fan71204e92015-05-15 07:29:12 +0800634int __enable_i2c_clk(unsigned char enable, unsigned i2c_num)
Troy Kiskye4ff5252012-07-19 08:18:18 +0000635{
Peng Fan71204e92015-05-15 07:29:12 +0800636 return 1;
637}
638int enable_i2c_clk(unsigned char enable, unsigned i2c_num)
639 __attribute__((weak, alias("__enable_i2c_clk")));
640
641void bus_i2c_init(int index, int speed, int unused,
642 int (*idle_bus_fn)(void *p), void *idle_bus_data)
643{
644 int ret;
645
646 if (index >= ARRAY_SIZE(mxc_i2c_buses)) {
647 debug("Error i2c index\n");
Troy Kiskye4ff5252012-07-19 08:18:18 +0000648 return;
Troy Kiskye4ff5252012-07-19 08:18:18 +0000649 }
Peng Fan71204e92015-05-15 07:29:12 +0800650
Gong Qianyuaee3fdd2015-12-18 17:38:01 +0800651 /*
652 * Warning: Be careful to allow the assignment to a static
653 * variable here. This function could be called while U-Boot is
654 * still running in flash memory. So such assignment is equal
655 * to write data to flash without erasing.
656 */
657 if (idle_bus_fn)
658 mxc_i2c_buses[index].idle_bus_fn = idle_bus_fn;
659 if (idle_bus_data)
660 mxc_i2c_buses[index].idle_bus_data = idle_bus_data;
Peng Fan71204e92015-05-15 07:29:12 +0800661
662 ret = enable_i2c_clk(1, index);
663 if (ret < 0) {
664 debug("I2C-%d clk fail to enable.\n", index);
665 return;
666 }
667
668 bus_i2c_set_bus_speed(&mxc_i2c_buses[index], speed);
Troy Kiskye4ff5252012-07-19 08:18:18 +0000669}
670
671/*
Yuan Yao9d10c2d2016-06-08 18:24:51 +0800672 * Early init I2C for prepare read the clk through I2C.
673 */
674void i2c_early_init_f(void)
675{
676 ulong base = mxc_i2c_buses[I2C_EARLY_INIT_INDEX].base;
677 bool quirk = mxc_i2c_buses[I2C_EARLY_INIT_INDEX].driver_data
678 & I2C_QUIRK_FLAG ? true : false;
679 int reg_shift = quirk ? VF610_I2C_REGSHIFT : IMX_I2C_REGSHIFT;
680
681 /* Set I2C divider value */
682 writeb(I2C_IFDR_DIV_CONSERVATIVE, base + (IFDR << reg_shift));
683 /* Reset module */
684 writeb(I2CR_IDIS, base + (I2CR << reg_shift));
685 writeb(0, base + (I2SR << reg_shift));
686 /* Enable I2C */
687 writeb(I2CR_IEN, base + (I2CR << reg_shift));
688}
689
690/*
Troy Kiskye4ff5252012-07-19 08:18:18 +0000691 * Init I2C Bus
692 */
tremfac96402013-09-21 18:13:35 +0200693static void mxc_i2c_init(struct i2c_adapter *adap, int speed, int slaveaddr)
Troy Kiskye4ff5252012-07-19 08:18:18 +0000694{
Peng Fan71204e92015-05-15 07:29:12 +0800695 bus_i2c_init(adap->hwadapnr, speed, slaveaddr, NULL, NULL);
Troy Kiskye4ff5252012-07-19 08:18:18 +0000696}
697
698/*
699 * Set I2C Speed
700 */
Peng Fan71204e92015-05-15 07:29:12 +0800701static u32 mxc_i2c_set_bus_speed(struct i2c_adapter *adap, uint speed)
Troy Kiskye4ff5252012-07-19 08:18:18 +0000702{
tremfac96402013-09-21 18:13:35 +0200703 return bus_i2c_set_bus_speed(i2c_get_base(adap), speed);
Troy Kiskye4ff5252012-07-19 08:18:18 +0000704}
705
706/*
tremfac96402013-09-21 18:13:35 +0200707 * Register mxc i2c adapters
Troy Kiskye4ff5252012-07-19 08:18:18 +0000708 */
Albert ARIBAUD \\(3ADEV\\)03544c62015-09-21 22:43:38 +0200709#ifdef CONFIG_SYS_I2C_MXC_I2C1
tremfac96402013-09-21 18:13:35 +0200710U_BOOT_I2C_ADAP_COMPLETE(mxc0, mxc_i2c_init, mxc_i2c_probe,
711 mxc_i2c_read, mxc_i2c_write,
712 mxc_i2c_set_bus_speed,
713 CONFIG_SYS_MXC_I2C1_SPEED,
714 CONFIG_SYS_MXC_I2C1_SLAVE, 0)
Albert ARIBAUD \\(3ADEV\\)03544c62015-09-21 22:43:38 +0200715#endif
716
717#ifdef CONFIG_SYS_I2C_MXC_I2C2
tremfac96402013-09-21 18:13:35 +0200718U_BOOT_I2C_ADAP_COMPLETE(mxc1, mxc_i2c_init, mxc_i2c_probe,
719 mxc_i2c_read, mxc_i2c_write,
720 mxc_i2c_set_bus_speed,
721 CONFIG_SYS_MXC_I2C2_SPEED,
722 CONFIG_SYS_MXC_I2C2_SLAVE, 1)
Albert ARIBAUD \\(3ADEV\\)03544c62015-09-21 22:43:38 +0200723#endif
724
York Sunf8cb1012015-03-20 10:20:40 -0700725#ifdef CONFIG_SYS_I2C_MXC_I2C3
tremfac96402013-09-21 18:13:35 +0200726U_BOOT_I2C_ADAP_COMPLETE(mxc2, mxc_i2c_init, mxc_i2c_probe,
727 mxc_i2c_read, mxc_i2c_write,
728 mxc_i2c_set_bus_speed,
729 CONFIG_SYS_MXC_I2C3_SPEED,
730 CONFIG_SYS_MXC_I2C3_SLAVE, 2)
731#endif
Peng Fan71204e92015-05-15 07:29:12 +0800732
York Sunf8cb1012015-03-20 10:20:40 -0700733#ifdef CONFIG_SYS_I2C_MXC_I2C4
734U_BOOT_I2C_ADAP_COMPLETE(mxc3, mxc_i2c_init, mxc_i2c_probe,
735 mxc_i2c_read, mxc_i2c_write,
736 mxc_i2c_set_bus_speed,
737 CONFIG_SYS_MXC_I2C4_SPEED,
738 CONFIG_SYS_MXC_I2C4_SLAVE, 3)
739#endif
Peng Fan71204e92015-05-15 07:29:12 +0800740
741#else
742
743static int mxc_i2c_set_bus_speed(struct udevice *bus, unsigned int speed)
744{
745 struct mxc_i2c_bus *i2c_bus = dev_get_priv(bus);
746
747 return bus_i2c_set_bus_speed(i2c_bus, speed);
748}
749
750static int mxc_i2c_probe(struct udevice *bus)
751{
752 struct mxc_i2c_bus *i2c_bus = dev_get_priv(bus);
Peng Fane1bed802016-03-11 16:47:50 +0800753 const void *fdt = gd->fdt_blob;
Simon Glasse160f7d2017-01-17 16:52:55 -0700754 int node = dev_of_offset(bus);
Peng Fan71204e92015-05-15 07:29:12 +0800755 fdt_addr_t addr;
Peng Fane1bed802016-03-11 16:47:50 +0800756 int ret, ret2;
Peng Fan71204e92015-05-15 07:29:12 +0800757
758 i2c_bus->driver_data = dev_get_driver_data(bus);
759
Simon Glassa821c4a2017-05-17 17:18:05 -0600760 addr = devfdt_get_addr(bus);
Peng Fan71204e92015-05-15 07:29:12 +0800761 if (addr == FDT_ADDR_T_NONE)
Simon Glass7c843192017-09-17 16:54:53 -0600762 return -EINVAL;
Peng Fan71204e92015-05-15 07:29:12 +0800763
764 i2c_bus->base = addr;
765 i2c_bus->index = bus->seq;
Peng Fane1bed802016-03-11 16:47:50 +0800766 i2c_bus->bus = bus;
Peng Fan71204e92015-05-15 07:29:12 +0800767
768 /* Enable clk */
769 ret = enable_i2c_clk(1, bus->seq);
770 if (ret < 0)
771 return ret;
772
Peng Fane1bed802016-03-11 16:47:50 +0800773 /*
774 * See Documentation/devicetree/bindings/i2c/i2c-imx.txt
775 * Use gpio to force bus idle when necessary.
776 */
Simon Glassb02e4042016-10-02 17:59:28 -0600777 ret = fdt_stringlist_search(fdt, node, "pinctrl-names", "gpio");
Peng Fane1bed802016-03-11 16:47:50 +0800778 if (ret < 0) {
Jagan Teki2da24fe2016-12-06 00:00:59 +0100779 debug("i2c bus %d at 0x%2lx, no gpio pinctrl state.\n", bus->seq, i2c_bus->base);
Peng Fane1bed802016-03-11 16:47:50 +0800780 } else {
Simon Glass150c5af2017-05-30 21:47:09 -0600781 ret = gpio_request_by_name_nodev(offset_to_ofnode(node),
782 "scl-gpios", 0, &i2c_bus->scl_gpio,
783 GPIOD_IS_OUT);
784 ret2 = gpio_request_by_name_nodev(offset_to_ofnode(node),
785 "sda-gpios", 0, &i2c_bus->sda_gpio,
786 GPIOD_IS_OUT);
Peng Fanfb012872017-12-29 15:06:08 +0800787 if (!dm_gpio_is_valid(&i2c_bus->sda_gpio) ||
788 !dm_gpio_is_valid(&i2c_bus->scl_gpio) ||
789 ret || ret2) {
Peng Fane1bed802016-03-11 16:47:50 +0800790 dev_err(dev, "i2c bus %d at %lu, fail to request scl/sda gpio\n", bus->seq, i2c_bus->base);
Simon Glass7c843192017-09-17 16:54:53 -0600791 return -EINVAL;
Peng Fane1bed802016-03-11 16:47:50 +0800792 }
793 }
794
Peng Fan71204e92015-05-15 07:29:12 +0800795 ret = i2c_idle_bus(i2c_bus);
796 if (ret < 0) {
797 /* Disable clk */
798 enable_i2c_clk(0, bus->seq);
799 return ret;
800 }
801
802 /*
803 * Pinmux settings are in board file now, until pinmux is supported,
804 * we can set pinmux here in probe function.
805 */
806
807 debug("i2c : controller bus %d at %lu , speed %d: ",
808 bus->seq, i2c_bus->base,
809 i2c_bus->speed);
810
811 return 0;
812}
813
814static int mxc_i2c_probe_chip(struct udevice *bus, u32 chip_addr,
815 u32 chip_flags)
816{
817 int ret;
818 struct mxc_i2c_bus *i2c_bus = dev_get_priv(bus);
819
820 ret = i2c_init_transfer(i2c_bus, chip_addr, 0, 0);
821 if (ret < 0) {
822 debug("%s failed, ret = %d\n", __func__, ret);
823 return ret;
824 }
825
826 i2c_imx_stop(i2c_bus);
827
828 return 0;
829}
830
831static int mxc_i2c_xfer(struct udevice *bus, struct i2c_msg *msg, int nmsgs)
832{
833 struct mxc_i2c_bus *i2c_bus = dev_get_priv(bus);
834 int ret = 0;
835 ulong base = i2c_bus->base;
836 int reg_shift = i2c_bus->driver_data & I2C_QUIRK_FLAG ?
837 VF610_I2C_REGSHIFT : IMX_I2C_REGSHIFT;
838
839 /*
840 * Here the 3rd parameter addr and the 4th one alen are set to 0,
841 * because here we only want to send out chip address. The register
842 * address is wrapped in msg.
843 */
844 ret = i2c_init_transfer(i2c_bus, msg->addr, 0, 0);
845 if (ret < 0) {
846 debug("i2c_init_transfer error: %d\n", ret);
847 return ret;
848 }
849
850 for (; nmsgs > 0; nmsgs--, msg++) {
851 bool next_is_read = nmsgs > 1 && (msg[1].flags & I2C_M_RD);
852 debug("i2c_xfer: chip=0x%x, len=0x%x\n", msg->addr, msg->len);
853 if (msg->flags & I2C_M_RD)
854 ret = i2c_read_data(i2c_bus, msg->addr, msg->buf,
855 msg->len);
856 else {
857 ret = i2c_write_data(i2c_bus, msg->addr, msg->buf,
858 msg->len);
859 if (ret)
860 break;
861 if (next_is_read) {
862 /* Reuse ret */
863 ret = readb(base + (I2CR << reg_shift));
864 ret |= I2CR_RSTA;
865 writeb(ret, base + (I2CR << reg_shift));
866
867 ret = tx_byte(i2c_bus, (msg->addr << 1) | 1);
868 if (ret < 0) {
869 i2c_imx_stop(i2c_bus);
870 break;
871 }
872 }
873 }
874 }
875
876 if (ret)
877 debug("i2c_write: error sending\n");
878
879 i2c_imx_stop(i2c_bus);
880
881 return ret;
882}
883
884static const struct dm_i2c_ops mxc_i2c_ops = {
885 .xfer = mxc_i2c_xfer,
886 .probe_chip = mxc_i2c_probe_chip,
887 .set_bus_speed = mxc_i2c_set_bus_speed,
888};
889
890static const struct udevice_id mxc_i2c_ids[] = {
891 { .compatible = "fsl,imx21-i2c", },
892 { .compatible = "fsl,vf610-i2c", .data = I2C_QUIRK_FLAG, },
893 {}
894};
895
896U_BOOT_DRIVER(i2c_mxc) = {
897 .name = "i2c_mxc",
898 .id = UCLASS_I2C,
899 .of_match = mxc_i2c_ids,
900 .probe = mxc_i2c_probe,
901 .priv_auto_alloc_size = sizeof(struct mxc_i2c_bus),
902 .ops = &mxc_i2c_ops,
903};
904#endif