blob: 445fa2108248e0ea32f64e0ee34c9ef2fdf15104 [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>
Troy Kiskycea60b02012-07-19 08:18:04 +000020#include <asm/errno.h>
Peng Fan71204e92015-05-15 07:29:12 +080021#include <asm/imx-common/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
35/* Register index */
36#define IADR 0
37#define IFDR 1
38#define I2CR 2
39#define I2SR 3
40#define I2DR 4
Sascha Hauercdace062008-03-26 20:40:49 +010041
Sascha Hauercdace062008-03-26 20:40:49 +010042#define I2CR_IIEN (1 << 6)
43#define I2CR_MSTA (1 << 5)
44#define I2CR_MTX (1 << 4)
45#define I2CR_TX_NO_AK (1 << 3)
46#define I2CR_RSTA (1 << 2)
47
48#define I2SR_ICF (1 << 7)
49#define I2SR_IBB (1 << 5)
Troy Kiskyd5383a62012-07-19 08:18:15 +000050#define I2SR_IAL (1 << 4)
Sascha Hauercdace062008-03-26 20:40:49 +010051#define I2SR_IIF (1 << 1)
52#define I2SR_RX_NO_AK (1 << 0)
53
Alison Wang30ea41a2013-06-17 15:30:39 +080054#ifdef I2C_QUIRK_REG
55#define I2CR_IEN (0 << 7)
56#define I2CR_IDIS (1 << 7)
57#define I2SR_IIF_CLEAR (1 << 1)
58#else
59#define I2CR_IEN (1 << 7)
60#define I2CR_IDIS (0 << 7)
61#define I2SR_IIF_CLEAR (0 << 1)
62#endif
63
Troy Kiskye4ff5252012-07-19 08:18:18 +000064#if defined(CONFIG_HARD_I2C) && !defined(CONFIG_SYS_I2C_BASE)
Troy Kiskyde6f6042012-04-24 17:33:25 +000065#error "define CONFIG_SYS_I2C_BASE to use the mxc_i2c driver"
Sascha Hauercdace062008-03-26 20:40:49 +010066#endif
67
Alison Wang30ea41a2013-06-17 15:30:39 +080068#ifdef I2C_QUIRK_REG
69static u16 i2c_clk_div[60][2] = {
70 { 20, 0x00 }, { 22, 0x01 }, { 24, 0x02 }, { 26, 0x03 },
71 { 28, 0x04 }, { 30, 0x05 }, { 32, 0x09 }, { 34, 0x06 },
72 { 36, 0x0A }, { 40, 0x07 }, { 44, 0x0C }, { 48, 0x0D },
73 { 52, 0x43 }, { 56, 0x0E }, { 60, 0x45 }, { 64, 0x12 },
74 { 68, 0x0F }, { 72, 0x13 }, { 80, 0x14 }, { 88, 0x15 },
75 { 96, 0x19 }, { 104, 0x16 }, { 112, 0x1A }, { 128, 0x17 },
76 { 136, 0x4F }, { 144, 0x1C }, { 160, 0x1D }, { 176, 0x55 },
77 { 192, 0x1E }, { 208, 0x56 }, { 224, 0x22 }, { 228, 0x24 },
78 { 240, 0x1F }, { 256, 0x23 }, { 288, 0x5C }, { 320, 0x25 },
79 { 384, 0x26 }, { 448, 0x2A }, { 480, 0x27 }, { 512, 0x2B },
80 { 576, 0x2C }, { 640, 0x2D }, { 768, 0x31 }, { 896, 0x32 },
81 { 960, 0x2F }, { 1024, 0x33 }, { 1152, 0x34 }, { 1280, 0x35 },
82 { 1536, 0x36 }, { 1792, 0x3A }, { 1920, 0x37 }, { 2048, 0x3B },
83 { 2304, 0x3C }, { 2560, 0x3D }, { 3072, 0x3E }, { 3584, 0x7A },
84 { 3840, 0x3F }, { 4096, 0x7B }, { 5120, 0x7D }, { 6144, 0x7E },
85};
86#else
Marek Vasutdb841402011-09-22 09:22:12 +000087static u16 i2c_clk_div[50][2] = {
88 { 22, 0x20 }, { 24, 0x21 }, { 26, 0x22 }, { 28, 0x23 },
89 { 30, 0x00 }, { 32, 0x24 }, { 36, 0x25 }, { 40, 0x26 },
90 { 42, 0x03 }, { 44, 0x27 }, { 48, 0x28 }, { 52, 0x05 },
91 { 56, 0x29 }, { 60, 0x06 }, { 64, 0x2A }, { 72, 0x2B },
92 { 80, 0x2C }, { 88, 0x09 }, { 96, 0x2D }, { 104, 0x0A },
93 { 112, 0x2E }, { 128, 0x2F }, { 144, 0x0C }, { 160, 0x30 },
94 { 192, 0x31 }, { 224, 0x32 }, { 240, 0x0F }, { 256, 0x33 },
95 { 288, 0x10 }, { 320, 0x34 }, { 384, 0x35 }, { 448, 0x36 },
96 { 480, 0x13 }, { 512, 0x37 }, { 576, 0x14 }, { 640, 0x38 },
97 { 768, 0x39 }, { 896, 0x3A }, { 960, 0x17 }, { 1024, 0x3B },
98 { 1152, 0x18 }, { 1280, 0x3C }, { 1536, 0x3D }, { 1792, 0x3E },
99 { 1920, 0x1B }, { 2048, 0x3F }, { 2304, 0x1C }, { 2560, 0x1D },
100 { 3072, 0x1E }, { 3840, 0x1F }
101};
Alison Wang30ea41a2013-06-17 15:30:39 +0800102#endif
Sascha Hauercdace062008-03-26 20:40:49 +0100103
tremfac96402013-09-21 18:13:35 +0200104#ifndef CONFIG_SYS_MXC_I2C1_SPEED
105#define CONFIG_SYS_MXC_I2C1_SPEED 100000
106#endif
107#ifndef CONFIG_SYS_MXC_I2C2_SPEED
108#define CONFIG_SYS_MXC_I2C2_SPEED 100000
109#endif
110#ifndef CONFIG_SYS_MXC_I2C3_SPEED
111#define CONFIG_SYS_MXC_I2C3_SPEED 100000
112#endif
York Sunf8cb1012015-03-20 10:20:40 -0700113#ifndef CONFIG_SYS_MXC_I2C4_SPEED
114#define CONFIG_SYS_MXC_I2C4_SPEED 100000
115#endif
tremfac96402013-09-21 18:13:35 +0200116
117#ifndef CONFIG_SYS_MXC_I2C1_SLAVE
118#define CONFIG_SYS_MXC_I2C1_SLAVE 0
119#endif
120#ifndef CONFIG_SYS_MXC_I2C2_SLAVE
121#define CONFIG_SYS_MXC_I2C2_SLAVE 0
122#endif
123#ifndef CONFIG_SYS_MXC_I2C3_SLAVE
124#define CONFIG_SYS_MXC_I2C3_SLAVE 0
125#endif
York Sunf8cb1012015-03-20 10:20:40 -0700126#ifndef CONFIG_SYS_MXC_I2C4_SLAVE
127#define CONFIG_SYS_MXC_I2C4_SLAVE 0
128#endif
tremfac96402013-09-21 18:13:35 +0200129
Marek Vasutdb841402011-09-22 09:22:12 +0000130/*
131 * Calculate and set proper clock divider
132 */
Peng Fan71204e92015-05-15 07:29:12 +0800133static uint8_t i2c_imx_get_clk(struct mxc_i2c_bus *i2c_bus, unsigned int rate)
Stefano Babic1d549ad2011-01-20 07:50:44 +0000134{
Marek Vasutdb841402011-09-22 09:22:12 +0000135 unsigned int i2c_clk_rate;
136 unsigned int div;
Marek Vasutbf0783d2011-10-26 00:05:44 +0000137 u8 clk_div;
Sascha Hauercdace062008-03-26 20:40:49 +0100138
Liu Hui-R64343127cec12011-01-03 22:27:39 +0000139#if defined(CONFIG_MX31)
Stefano Babic1d549ad2011-01-20 07:50:44 +0000140 struct clock_control_regs *sc_regs =
141 (struct clock_control_regs *)CCM_BASE;
Marek Vasutdb841402011-09-22 09:22:12 +0000142
Guennadi Liakhovetskie7de18a2009-02-13 09:23:36 +0100143 /* start the required I2C clock */
Troy Kiskyde6f6042012-04-24 17:33:25 +0000144 writel(readl(&sc_regs->cgr0) | (3 << CONFIG_SYS_I2C_CLK_OFFSET),
Stefano Babic1d549ad2011-01-20 07:50:44 +0000145 &sc_regs->cgr0);
Liu Hui-R64343127cec12011-01-03 22:27:39 +0000146#endif
Guennadi Liakhovetskie7de18a2009-02-13 09:23:36 +0100147
Marek Vasutdb841402011-09-22 09:22:12 +0000148 /* Divider value calculation */
Matthias Weissere7bed5c2012-09-24 02:46:53 +0000149 i2c_clk_rate = mxc_get_clock(MXC_I2C_CLK);
Marek Vasutdb841402011-09-22 09:22:12 +0000150 div = (i2c_clk_rate + rate - 1) / rate;
151 if (div < i2c_clk_div[0][0])
Marek Vasutb567b8f2011-09-27 06:34:11 +0000152 clk_div = 0;
Marek Vasutdb841402011-09-22 09:22:12 +0000153 else if (div > i2c_clk_div[ARRAY_SIZE(i2c_clk_div) - 1][0])
Marek Vasutb567b8f2011-09-27 06:34:11 +0000154 clk_div = ARRAY_SIZE(i2c_clk_div) - 1;
Marek Vasutdb841402011-09-22 09:22:12 +0000155 else
Marek Vasutb567b8f2011-09-27 06:34:11 +0000156 for (clk_div = 0; i2c_clk_div[clk_div][0] < div; clk_div++)
Marek Vasutdb841402011-09-22 09:22:12 +0000157 ;
Sascha Hauercdace062008-03-26 20:40:49 +0100158
Marek Vasutdb841402011-09-22 09:22:12 +0000159 /* Store divider value */
Marek Vasutbf0783d2011-10-26 00:05:44 +0000160 return 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/*
Troy Kiskye4ff5252012-07-19 08:18:18 +0000164 * Set I2C Bus speed
Marek Vasutdb841402011-09-22 09:22:12 +0000165 */
Peng Fan71204e92015-05-15 07:29:12 +0800166static int bus_i2c_set_bus_speed(struct mxc_i2c_bus *i2c_bus, int speed)
Marek Vasutdb841402011-09-22 09:22:12 +0000167{
Peng Fan71204e92015-05-15 07:29:12 +0800168 ulong base = i2c_bus->base;
169 bool quirk = i2c_bus->driver_data & I2C_QUIRK_FLAG ? true : false;
170 u8 clk_idx = i2c_imx_get_clk(i2c_bus, speed);
Marek Vasutbf0783d2011-10-26 00:05:44 +0000171 u8 idx = i2c_clk_div[clk_idx][1];
Peng Fan71204e92015-05-15 07:29:12 +0800172 int reg_shift = quirk ? VF610_I2C_REGSHIFT : IMX_I2C_REGSHIFT;
Marek Vasutbf0783d2011-10-26 00:05:44 +0000173
Heiko Schochere6c8b712015-05-18 10:58:12 +0200174 if (!base)
175 return -ENODEV;
176
Marek Vasutbf0783d2011-10-26 00:05:44 +0000177 /* Store divider value */
Peng Fan71204e92015-05-15 07:29:12 +0800178 writeb(idx, base + (IFDR << reg_shift));
Marek Vasutbf0783d2011-10-26 00:05:44 +0000179
Troy Kisky83a1a192012-07-19 08:18:12 +0000180 /* Reset module */
Peng Fan71204e92015-05-15 07:29:12 +0800181 writeb(I2CR_IDIS, base + (I2CR << reg_shift));
182 writeb(0, base + (I2SR << reg_shift));
Marek Vasutb567b8f2011-09-27 06:34:11 +0000183 return 0;
184}
185
Troy Kisky7aa57a02012-07-19 08:18:09 +0000186#define ST_BUS_IDLE (0 | (I2SR_IBB << 8))
187#define ST_BUS_BUSY (I2SR_IBB | (I2SR_IBB << 8))
188#define ST_IIF (I2SR_IIF | (I2SR_IIF << 8))
189
Peng Fan71204e92015-05-15 07:29:12 +0800190static int wait_for_sr_state(struct mxc_i2c_bus *i2c_bus, unsigned state)
Stefano Babic81687212011-01-20 07:51:31 +0000191{
Troy Kisky7aa57a02012-07-19 08:18:09 +0000192 unsigned sr;
193 ulong elapsed;
Peng Fan71204e92015-05-15 07:29:12 +0800194 bool quirk = i2c_bus->driver_data & I2C_QUIRK_FLAG ? true : false;
195 int reg_shift = quirk ? VF610_I2C_REGSHIFT : IMX_I2C_REGSHIFT;
196 ulong base = i2c_bus->base;
Troy Kisky7aa57a02012-07-19 08:18:09 +0000197 ulong start_time = get_timer(0);
198 for (;;) {
Peng Fan71204e92015-05-15 07:29:12 +0800199 sr = readb(base + (I2SR << reg_shift));
Troy Kiskyd5383a62012-07-19 08:18:15 +0000200 if (sr & I2SR_IAL) {
Peng Fan71204e92015-05-15 07:29:12 +0800201 if (quirk)
202 writeb(sr | I2SR_IAL, base +
203 (I2SR << reg_shift));
204 else
205 writeb(sr & ~I2SR_IAL, base +
206 (I2SR << reg_shift));
Troy Kiskyd5383a62012-07-19 08:18:15 +0000207 printf("%s: Arbitration lost sr=%x cr=%x state=%x\n",
Peng Fan71204e92015-05-15 07:29:12 +0800208 __func__, sr, readb(base + (I2CR << reg_shift)),
209 state);
Troy Kiskyd5383a62012-07-19 08:18:15 +0000210 return -ERESTART;
211 }
Troy Kisky7aa57a02012-07-19 08:18:09 +0000212 if ((sr & (state >> 8)) == (unsigned char)state)
213 return sr;
214 WATCHDOG_RESET();
215 elapsed = get_timer(start_time);
216 if (elapsed > (CONFIG_SYS_HZ / 10)) /* .1 seconds */
217 break;
Stefano Babic81687212011-01-20 07:51:31 +0000218 }
Troy Kisky7aa57a02012-07-19 08:18:09 +0000219 printf("%s: failed sr=%x cr=%x state=%x\n", __func__,
Peng Fan71204e92015-05-15 07:29:12 +0800220 sr, readb(base + (I2CR << reg_shift)), state);
Troy Kiskycea60b02012-07-19 08:18:04 +0000221 return -ETIMEDOUT;
Stefano Babic81687212011-01-20 07:51:31 +0000222}
223
Peng Fan71204e92015-05-15 07:29:12 +0800224static int tx_byte(struct mxc_i2c_bus *i2c_bus, u8 byte)
Sascha Hauercdace062008-03-26 20:40:49 +0100225{
Troy Kiskycea60b02012-07-19 08:18:04 +0000226 int ret;
Peng Fan71204e92015-05-15 07:29:12 +0800227 int reg_shift = i2c_bus->driver_data & I2C_QUIRK_FLAG ?
228 VF610_I2C_REGSHIFT : IMX_I2C_REGSHIFT;
229 ulong base = i2c_bus->base;
Sascha Hauercdace062008-03-26 20:40:49 +0100230
Peng Fan71204e92015-05-15 07:29:12 +0800231 writeb(I2SR_IIF_CLEAR, base + (I2SR << reg_shift));
232 writeb(byte, base + (I2DR << reg_shift));
233
234 ret = wait_for_sr_state(i2c_bus, ST_IIF);
Troy Kiskycea60b02012-07-19 08:18:04 +0000235 if (ret < 0)
236 return ret;
Troy Kiskycea60b02012-07-19 08:18:04 +0000237 if (ret & I2SR_RX_NO_AK)
238 return -ENODEV;
239 return 0;
Marek Vasutdb841402011-09-22 09:22:12 +0000240}
241
242/*
Peng Fan71204e92015-05-15 07:29:12 +0800243 * Stub implementations for outer i2c slave operations.
244 */
245void __i2c_force_reset_slave(void)
246{
247}
248void i2c_force_reset_slave(void)
249 __attribute__((weak, alias("__i2c_force_reset_slave")));
250
251/*
Troy Kisky90a5b702012-07-19 08:18:13 +0000252 * Stop I2C transaction
Marek Vasutdb841402011-09-22 09:22:12 +0000253 */
Peng Fan71204e92015-05-15 07:29:12 +0800254static void i2c_imx_stop(struct mxc_i2c_bus *i2c_bus)
Sascha Hauercdace062008-03-26 20:40:49 +0100255{
Troy Kisky7aa57a02012-07-19 08:18:09 +0000256 int ret;
Peng Fan71204e92015-05-15 07:29:12 +0800257 int reg_shift = i2c_bus->driver_data & I2C_QUIRK_FLAG ?
258 VF610_I2C_REGSHIFT : IMX_I2C_REGSHIFT;
259 ulong base = i2c_bus->base;
260 unsigned int temp = readb(base + (I2CR << reg_shift));
Sascha Hauercdace062008-03-26 20:40:49 +0100261
Troy Kisky1c076db2012-07-19 08:18:02 +0000262 temp &= ~(I2CR_MSTA | I2CR_MTX);
Peng Fan71204e92015-05-15 07:29:12 +0800263 writeb(temp, base + (I2CR << reg_shift));
264 ret = wait_for_sr_state(i2c_bus, ST_BUS_IDLE);
Troy Kisky7aa57a02012-07-19 08:18:09 +0000265 if (ret < 0)
266 printf("%s:trigger stop failed\n", __func__);
Sascha Hauercdace062008-03-26 20:40:49 +0100267}
268
Marek Vasutdb841402011-09-22 09:22:12 +0000269/*
Troy Kiskyb230ddc2012-07-19 08:18:06 +0000270 * Send start signal, chip address and
271 * write register address
Marek Vasutdb841402011-09-22 09:22:12 +0000272 */
Peng Fan71204e92015-05-15 07:29:12 +0800273static int i2c_init_transfer_(struct mxc_i2c_bus *i2c_bus, u8 chip,
274 u32 addr, int alen)
Sascha Hauercdace062008-03-26 20:40:49 +0100275{
Troy Kisky71e9f3c2012-07-19 08:18:11 +0000276 unsigned int temp;
277 int ret;
Peng Fan71204e92015-05-15 07:29:12 +0800278 bool quirk = i2c_bus->driver_data & I2C_QUIRK_FLAG ? true : false;
279 ulong base = i2c_bus->base;
280 int reg_shift = quirk ? VF610_I2C_REGSHIFT : IMX_I2C_REGSHIFT;
281
282 /* Reset i2c slave */
283 i2c_force_reset_slave();
Troy Kisky71e9f3c2012-07-19 08:18:11 +0000284
285 /* Enable I2C controller */
Peng Fan71204e92015-05-15 07:29:12 +0800286 if (quirk)
287 ret = readb(base + (I2CR << reg_shift)) & I2CR_IDIS;
288 else
289 ret = !(readb(base + (I2CR << reg_shift)) & I2CR_IEN);
290
291 if (ret) {
292 writeb(I2CR_IEN, base + (I2CR << reg_shift));
Troy Kisky90a5b702012-07-19 08:18:13 +0000293 /* Wait for controller to be stable */
294 udelay(50);
295 }
Peng Fan71204e92015-05-15 07:29:12 +0800296
297 if (readb(base + (IADR << reg_shift)) == (chip << 1))
298 writeb((chip << 1) ^ 2, base + (IADR << reg_shift));
299 writeb(I2SR_IIF_CLEAR, base + (I2SR << reg_shift));
300 ret = wait_for_sr_state(i2c_bus, ST_BUS_IDLE);
Troy Kisky90a5b702012-07-19 08:18:13 +0000301 if (ret < 0)
Troy Kiskya7f1a002012-07-19 08:18:16 +0000302 return ret;
Troy Kisky71e9f3c2012-07-19 08:18:11 +0000303
304 /* Start I2C transaction */
Peng Fan71204e92015-05-15 07:29:12 +0800305 temp = readb(base + (I2CR << reg_shift));
Troy Kisky71e9f3c2012-07-19 08:18:11 +0000306 temp |= I2CR_MSTA;
Peng Fan71204e92015-05-15 07:29:12 +0800307 writeb(temp, base + (I2CR << reg_shift));
Troy Kisky71e9f3c2012-07-19 08:18:11 +0000308
Peng Fan71204e92015-05-15 07:29:12 +0800309 ret = wait_for_sr_state(i2c_bus, ST_BUS_BUSY);
Troy Kisky71e9f3c2012-07-19 08:18:11 +0000310 if (ret < 0)
Troy Kiskya7f1a002012-07-19 08:18:16 +0000311 return ret;
Troy Kiskyb230ddc2012-07-19 08:18:06 +0000312
Troy Kisky71e9f3c2012-07-19 08:18:11 +0000313 temp |= I2CR_MTX | I2CR_TX_NO_AK;
Peng Fan71204e92015-05-15 07:29:12 +0800314 writeb(temp, base + (I2CR << reg_shift));
Troy Kisky71e9f3c2012-07-19 08:18:11 +0000315
Troy Kiskyb230ddc2012-07-19 08:18:06 +0000316 /* write slave address */
Peng Fan71204e92015-05-15 07:29:12 +0800317 ret = tx_byte(i2c_bus, chip << 1);
Troy Kiskyb230ddc2012-07-19 08:18:06 +0000318 if (ret < 0)
Troy Kiskya7f1a002012-07-19 08:18:16 +0000319 return ret;
Marek Vasutdb841402011-09-22 09:22:12 +0000320
Marek Vasutbf0783d2011-10-26 00:05:44 +0000321 while (alen--) {
Peng Fan71204e92015-05-15 07:29:12 +0800322 ret = tx_byte(i2c_bus, (addr >> (alen * 8)) & 0xff);
Troy Kiskycea60b02012-07-19 08:18:04 +0000323 if (ret < 0)
Troy Kiskya7f1a002012-07-19 08:18:16 +0000324 return ret;
Stefano Babic81687212011-01-20 07:51:31 +0000325 }
Troy Kiskyb230ddc2012-07-19 08:18:06 +0000326 return 0;
Troy Kiskya7f1a002012-07-19 08:18:16 +0000327}
328
Peng Fan71204e92015-05-15 07:29:12 +0800329#ifndef CONFIG_DM_I2C
330int i2c_idle_bus(struct mxc_i2c_bus *i2c_bus)
331{
332 if (i2c_bus && i2c_bus->idle_bus_fn)
333 return i2c_bus->idle_bus_fn(i2c_bus->idle_bus_data);
334 return 0;
335}
336#else
337/*
Peng Fane1bed802016-03-11 16:47:50 +0800338 * See Linux Documentation/devicetree/bindings/i2c/i2c-imx.txt
339 * "
340 * scl-gpios: specify the gpio related to SCL pin
341 * sda-gpios: specify the gpio related to SDA pin
342 * add pinctrl to configure i2c pins to gpio function for i2c
343 * bus recovery, call it "gpio" state
344 * "
345 *
346 * The i2c_idle_bus is an implementation following Linux Kernel.
Peng Fan71204e92015-05-15 07:29:12 +0800347 */
Peng Fan71204e92015-05-15 07:29:12 +0800348int i2c_idle_bus(struct mxc_i2c_bus *i2c_bus)
Peng Fane1bed802016-03-11 16:47:50 +0800349{
350 struct udevice *bus = i2c_bus->bus;
351 struct gpio_desc *scl_gpio = &i2c_bus->scl_gpio;
352 struct gpio_desc *sda_gpio = &i2c_bus->sda_gpio;
353 int sda, scl;
354 int i, ret = 0;
355 ulong elapsed, start_time;
356
357 if (pinctrl_select_state(bus, "gpio")) {
358 dev_dbg(bus, "Can not to switch to use gpio pinmux\n");
359 /*
360 * GPIO pinctrl for i2c force idle is not a must,
361 * but it is strongly recommended to be used.
362 * Because it can help you to recover from bad
363 * i2c bus state. Do not return failure, because
364 * it is not a must.
365 */
366 return 0;
367 }
368
369 dm_gpio_set_dir_flags(scl_gpio, GPIOD_IS_IN);
370 dm_gpio_set_dir_flags(sda_gpio, GPIOD_IS_IN);
371 scl = dm_gpio_get_value(scl_gpio);
372 sda = dm_gpio_get_value(sda_gpio);
373
374 if ((sda & scl) == 1)
375 goto exit; /* Bus is idle already */
376
377 /* Send high and low on the SCL line */
378 for (i = 0; i < 9; i++) {
379 dm_gpio_set_dir_flags(scl_gpio, GPIOD_IS_OUT);
380 dm_gpio_set_value(scl_gpio, 0);
381 udelay(50);
382 dm_gpio_set_dir_flags(scl_gpio, GPIOD_IS_IN);
383 udelay(50);
384 }
385 start_time = get_timer(0);
386 for (;;) {
387 dm_gpio_set_dir_flags(scl_gpio, GPIOD_IS_IN);
388 dm_gpio_set_dir_flags(sda_gpio, GPIOD_IS_IN);
389 scl = dm_gpio_get_value(scl_gpio);
390 sda = dm_gpio_get_value(sda_gpio);
391 if ((sda & scl) == 1)
392 break;
393 WATCHDOG_RESET();
394 elapsed = get_timer(start_time);
395 if (elapsed > (CONFIG_SYS_HZ / 5)) { /* .2 seconds */
396 ret = -EBUSY;
397 printf("%s: failed to clear bus, sda=%d scl=%d\n", __func__, sda, scl);
398 break;
399 }
400 }
401
402exit:
403 pinctrl_select_state(bus, "default");
404 return ret;
405}
Peng Fan71204e92015-05-15 07:29:12 +0800406#endif
407
408static int i2c_init_transfer(struct mxc_i2c_bus *i2c_bus, u8 chip,
409 u32 addr, int alen)
Troy Kiskya7f1a002012-07-19 08:18:16 +0000410{
411 int retry;
412 int ret;
Peng Fan71204e92015-05-15 07:29:12 +0800413 int reg_shift = i2c_bus->driver_data & I2C_QUIRK_FLAG ?
414 VF610_I2C_REGSHIFT : IMX_I2C_REGSHIFT;
Heiko Schochere6c8b712015-05-18 10:58:12 +0200415
416 if (!i2c_bus->base)
417 return -ENODEV;
418
Troy Kiskya7f1a002012-07-19 08:18:16 +0000419 for (retry = 0; retry < 3; retry++) {
Peng Fan71204e92015-05-15 07:29:12 +0800420 ret = i2c_init_transfer_(i2c_bus, chip, addr, alen);
Troy Kiskya7f1a002012-07-19 08:18:16 +0000421 if (ret >= 0)
422 return 0;
Peng Fan71204e92015-05-15 07:29:12 +0800423 i2c_imx_stop(i2c_bus);
Troy Kiskya7f1a002012-07-19 08:18:16 +0000424 if (ret == -ENODEV)
425 return ret;
426
427 printf("%s: failed for chip 0x%x retry=%d\n", __func__, chip,
428 retry);
429 if (ret != -ERESTART)
Alison Wang30ea41a2013-06-17 15:30:39 +0800430 /* Disable controller */
Peng Fan71204e92015-05-15 07:29:12 +0800431 writeb(I2CR_IDIS, i2c_bus->base + (I2CR << reg_shift));
Troy Kiskya7f1a002012-07-19 08:18:16 +0000432 udelay(100);
Peng Fan71204e92015-05-15 07:29:12 +0800433 if (i2c_idle_bus(i2c_bus) < 0)
Troy Kisky96c19bd2012-07-19 08:18:19 +0000434 break;
Troy Kiskya7f1a002012-07-19 08:18:16 +0000435 }
Peng Fan71204e92015-05-15 07:29:12 +0800436 printf("%s: give up i2c_regs=0x%lx\n", __func__, i2c_bus->base);
Marek Vasutdb841402011-09-22 09:22:12 +0000437 return ret;
Sascha Hauercdace062008-03-26 20:40:49 +0100438}
439
Peng Fan71204e92015-05-15 07:29:12 +0800440
441static int i2c_write_data(struct mxc_i2c_bus *i2c_bus, u8 chip, const u8 *buf,
442 int len)
443{
444 int i, ret = 0;
445
446 debug("i2c_write_data: chip=0x%x, len=0x%x\n", chip, len);
447 debug("write_data: ");
448 /* use rc for counter */
449 for (i = 0; i < len; ++i)
450 debug(" 0x%02x", buf[i]);
451 debug("\n");
452
453 for (i = 0; i < len; i++) {
454 ret = tx_byte(i2c_bus, buf[i]);
455 if (ret < 0) {
456 debug("i2c_write_data(): rc=%d\n", ret);
457 break;
458 }
459 }
460
461 return ret;
462}
463
464static int i2c_read_data(struct mxc_i2c_bus *i2c_bus, uchar chip, uchar *buf,
465 int len)
Marek Vasutdb841402011-09-22 09:22:12 +0000466{
Marek Vasutdb841402011-09-22 09:22:12 +0000467 int ret;
468 unsigned int temp;
469 int i;
Peng Fan71204e92015-05-15 07:29:12 +0800470 int reg_shift = i2c_bus->driver_data & I2C_QUIRK_FLAG ?
471 VF610_I2C_REGSHIFT : IMX_I2C_REGSHIFT;
472 ulong base = i2c_bus->base;
Marek Vasutdb841402011-09-22 09:22:12 +0000473
Peng Fan71204e92015-05-15 07:29:12 +0800474 debug("i2c_read_data: chip=0x%x, len=0x%x\n", chip, len);
Marek Vasutdb841402011-09-22 09:22:12 +0000475
476 /* setup bus to read data */
Peng Fan71204e92015-05-15 07:29:12 +0800477 temp = readb(base + (I2CR << reg_shift));
Marek Vasutdb841402011-09-22 09:22:12 +0000478 temp &= ~(I2CR_MTX | I2CR_TX_NO_AK);
479 if (len == 1)
480 temp |= I2CR_TX_NO_AK;
Peng Fan71204e92015-05-15 07:29:12 +0800481 writeb(temp, base + (I2CR << reg_shift));
482 writeb(I2SR_IIF_CLEAR, base + (I2SR << reg_shift));
483 /* dummy read to clear ICF */
484 readb(base + (I2DR << reg_shift));
Marek Vasutdb841402011-09-22 09:22:12 +0000485
486 /* read data */
487 for (i = 0; i < len; i++) {
Peng Fan71204e92015-05-15 07:29:12 +0800488 ret = wait_for_sr_state(i2c_bus, ST_IIF);
Troy Kisky7aa57a02012-07-19 08:18:09 +0000489 if (ret < 0) {
Peng Fan71204e92015-05-15 07:29:12 +0800490 debug("i2c_read_data(): ret=%d\n", ret);
491 i2c_imx_stop(i2c_bus);
Marek Vasutdb841402011-09-22 09:22:12 +0000492 return ret;
Troy Kiskyc4330d22012-07-19 08:18:07 +0000493 }
Marek Vasutdb841402011-09-22 09:22:12 +0000494
495 /*
496 * It must generate STOP before read I2DR to prevent
497 * controller from generating another clock cycle
498 */
499 if (i == (len - 1)) {
Peng Fan71204e92015-05-15 07:29:12 +0800500 i2c_imx_stop(i2c_bus);
Marek Vasutdb841402011-09-22 09:22:12 +0000501 } else if (i == (len - 2)) {
Peng Fan71204e92015-05-15 07:29:12 +0800502 temp = readb(base + (I2CR << reg_shift));
Marek Vasutdb841402011-09-22 09:22:12 +0000503 temp |= I2CR_TX_NO_AK;
Peng Fan71204e92015-05-15 07:29:12 +0800504 writeb(temp, base + (I2CR << reg_shift));
Marek Vasutdb841402011-09-22 09:22:12 +0000505 }
Peng Fan71204e92015-05-15 07:29:12 +0800506 writeb(I2SR_IIF_CLEAR, base + (I2SR << reg_shift));
507 buf[i] = readb(base + (I2DR << reg_shift));
Marek Vasutdb841402011-09-22 09:22:12 +0000508 }
Peng Fan71204e92015-05-15 07:29:12 +0800509
510 /* reuse ret for counter*/
511 for (ret = 0; ret < len; ++ret)
512 debug(" 0x%02x", buf[ret]);
513 debug("\n");
514
515 i2c_imx_stop(i2c_bus);
Troy Kisky7aa57a02012-07-19 08:18:09 +0000516 return 0;
Marek Vasutdb841402011-09-22 09:22:12 +0000517}
518
Peng Fan71204e92015-05-15 07:29:12 +0800519#ifndef CONFIG_DM_I2C
520/*
521 * Read data from I2C device
522 */
523static int bus_i2c_read(struct mxc_i2c_bus *i2c_bus, u8 chip, u32 addr,
524 int alen, u8 *buf, int len)
525{
526 int ret = 0;
527 u32 temp;
528 int reg_shift = i2c_bus->driver_data & I2C_QUIRK_FLAG ?
529 VF610_I2C_REGSHIFT : IMX_I2C_REGSHIFT;
530 ulong base = i2c_bus->base;
531
532 ret = i2c_init_transfer(i2c_bus, chip, addr, alen);
533 if (ret < 0)
534 return ret;
535
536 temp = readb(base + (I2CR << reg_shift));
537 temp |= I2CR_RSTA;
538 writeb(temp, base + (I2CR << reg_shift));
539
540 ret = tx_byte(i2c_bus, (chip << 1) | 1);
541 if (ret < 0) {
542 i2c_imx_stop(i2c_bus);
543 return ret;
544 }
545
546 ret = i2c_read_data(i2c_bus, chip, buf, len);
547
548 i2c_imx_stop(i2c_bus);
549 return ret;
550}
551
Marek Vasutdb841402011-09-22 09:22:12 +0000552/*
553 * Write data to I2C device
554 */
Peng Fan71204e92015-05-15 07:29:12 +0800555static int bus_i2c_write(struct mxc_i2c_bus *i2c_bus, u8 chip, u32 addr,
556 int alen, const u8 *buf, int len)
Sascha Hauercdace062008-03-26 20:40:49 +0100557{
Peng Fan71204e92015-05-15 07:29:12 +0800558 int ret = 0;
Sascha Hauercdace062008-03-26 20:40:49 +0100559
Peng Fan71204e92015-05-15 07:29:12 +0800560 ret = i2c_init_transfer(i2c_bus, chip, addr, alen);
Troy Kiskycea60b02012-07-19 08:18:04 +0000561 if (ret < 0)
Marek Vasutdb841402011-09-22 09:22:12 +0000562 return ret;
Sascha Hauercdace062008-03-26 20:40:49 +0100563
Peng Fan71204e92015-05-15 07:29:12 +0800564 ret = i2c_write_data(i2c_bus, chip, buf, len);
565
566 i2c_imx_stop(i2c_bus);
567
Marek Vasutdb841402011-09-22 09:22:12 +0000568 return ret;
Sascha Hauercdace062008-03-26 20:40:49 +0100569}
Troy Kiskycfbb88d2012-07-19 08:18:08 +0000570
Heiko Schochere6c8b712015-05-18 10:58:12 +0200571#if !defined(I2C2_BASE_ADDR)
572#define I2C2_BASE_ADDR 0
Heiko Schocher21a26942015-05-18 10:56:24 +0200573#endif
Heiko Schochere6c8b712015-05-18 10:58:12 +0200574
575#if !defined(I2C3_BASE_ADDR)
576#define I2C3_BASE_ADDR 0
577#endif
578
579#if !defined(I2C4_BASE_ADDR)
580#define I2C4_BASE_ADDR 0
581#endif
582
583static struct mxc_i2c_bus mxc_i2c_buses[] = {
Mingkai Hu9f3183d2015-10-26 19:47:50 +0800584#if defined(CONFIG_LS102XA) || defined(CONFIG_VF610) || \
585 defined(CONFIG_FSL_LAYERSCAPE)
Peng Fan71204e92015-05-15 07:29:12 +0800586 { 0, I2C1_BASE_ADDR, I2C_QUIRK_FLAG },
587 { 1, I2C2_BASE_ADDR, I2C_QUIRK_FLAG },
588 { 2, I2C3_BASE_ADDR, I2C_QUIRK_FLAG },
589 { 3, I2C4_BASE_ADDR, I2C_QUIRK_FLAG },
Troy Kiskye4ff5252012-07-19 08:18:18 +0000590#else
Heiko Schochere6c8b712015-05-18 10:58:12 +0200591 { 0, I2C1_BASE_ADDR, 0 },
592 { 1, I2C2_BASE_ADDR, 0 },
593 { 2, I2C3_BASE_ADDR, 0 },
594 { 3, I2C4_BASE_ADDR, 0 },
Troy Kiskye4ff5252012-07-19 08:18:18 +0000595#endif
tremfac96402013-09-21 18:13:35 +0200596};
597
Peng Fan71204e92015-05-15 07:29:12 +0800598struct mxc_i2c_bus *i2c_get_base(struct i2c_adapter *adap)
tremfac96402013-09-21 18:13:35 +0200599{
Peng Fan71204e92015-05-15 07:29:12 +0800600 return &mxc_i2c_buses[adap->hwadapnr];
Troy Kisky96c19bd2012-07-19 08:18:19 +0000601}
602
tremfac96402013-09-21 18:13:35 +0200603static int mxc_i2c_read(struct i2c_adapter *adap, uint8_t chip,
604 uint addr, int alen, uint8_t *buffer,
605 int len)
Troy Kisky98153262012-07-19 08:18:20 +0000606{
tremfac96402013-09-21 18:13:35 +0200607 return bus_i2c_read(i2c_get_base(adap), chip, addr, alen, buffer, len);
Troy Kisky98153262012-07-19 08:18:20 +0000608}
609
tremfac96402013-09-21 18:13:35 +0200610static int mxc_i2c_write(struct i2c_adapter *adap, uint8_t chip,
611 uint addr, int alen, uint8_t *buffer,
612 int len)
Troy Kisky98153262012-07-19 08:18:20 +0000613{
tremfac96402013-09-21 18:13:35 +0200614 return bus_i2c_write(i2c_get_base(adap), chip, addr, alen, buffer, len);
Troy Kiskye4ff5252012-07-19 08:18:18 +0000615}
616
Troy Kiskycfbb88d2012-07-19 08:18:08 +0000617/*
618 * Test if a chip at a given address responds (probe the chip)
619 */
tremfac96402013-09-21 18:13:35 +0200620static int mxc_i2c_probe(struct i2c_adapter *adap, uint8_t chip)
Troy Kiskycfbb88d2012-07-19 08:18:08 +0000621{
tremfac96402013-09-21 18:13:35 +0200622 return bus_i2c_write(i2c_get_base(adap), chip, 0, 0, NULL, 0);
Troy Kiskye4ff5252012-07-19 08:18:18 +0000623}
624
Peng Fan71204e92015-05-15 07:29:12 +0800625int __enable_i2c_clk(unsigned char enable, unsigned i2c_num)
Troy Kiskye4ff5252012-07-19 08:18:18 +0000626{
Peng Fan71204e92015-05-15 07:29:12 +0800627 return 1;
628}
629int enable_i2c_clk(unsigned char enable, unsigned i2c_num)
630 __attribute__((weak, alias("__enable_i2c_clk")));
631
632void bus_i2c_init(int index, int speed, int unused,
633 int (*idle_bus_fn)(void *p), void *idle_bus_data)
634{
635 int ret;
636
637 if (index >= ARRAY_SIZE(mxc_i2c_buses)) {
638 debug("Error i2c index\n");
Troy Kiskye4ff5252012-07-19 08:18:18 +0000639 return;
Troy Kiskye4ff5252012-07-19 08:18:18 +0000640 }
Peng Fan71204e92015-05-15 07:29:12 +0800641
Gong Qianyuaee3fdd2015-12-18 17:38:01 +0800642 /*
643 * Warning: Be careful to allow the assignment to a static
644 * variable here. This function could be called while U-Boot is
645 * still running in flash memory. So such assignment is equal
646 * to write data to flash without erasing.
647 */
648 if (idle_bus_fn)
649 mxc_i2c_buses[index].idle_bus_fn = idle_bus_fn;
650 if (idle_bus_data)
651 mxc_i2c_buses[index].idle_bus_data = idle_bus_data;
Peng Fan71204e92015-05-15 07:29:12 +0800652
653 ret = enable_i2c_clk(1, index);
654 if (ret < 0) {
655 debug("I2C-%d clk fail to enable.\n", index);
656 return;
657 }
658
659 bus_i2c_set_bus_speed(&mxc_i2c_buses[index], speed);
Troy Kiskye4ff5252012-07-19 08:18:18 +0000660}
661
662/*
663 * Init I2C Bus
664 */
tremfac96402013-09-21 18:13:35 +0200665static void mxc_i2c_init(struct i2c_adapter *adap, int speed, int slaveaddr)
Troy Kiskye4ff5252012-07-19 08:18:18 +0000666{
Peng Fan71204e92015-05-15 07:29:12 +0800667 bus_i2c_init(adap->hwadapnr, speed, slaveaddr, NULL, NULL);
Troy Kiskye4ff5252012-07-19 08:18:18 +0000668}
669
670/*
671 * Set I2C Speed
672 */
Peng Fan71204e92015-05-15 07:29:12 +0800673static u32 mxc_i2c_set_bus_speed(struct i2c_adapter *adap, uint speed)
Troy Kiskye4ff5252012-07-19 08:18:18 +0000674{
tremfac96402013-09-21 18:13:35 +0200675 return bus_i2c_set_bus_speed(i2c_get_base(adap), speed);
Troy Kiskye4ff5252012-07-19 08:18:18 +0000676}
677
678/*
tremfac96402013-09-21 18:13:35 +0200679 * Register mxc i2c adapters
Troy Kiskye4ff5252012-07-19 08:18:18 +0000680 */
Albert ARIBAUD \\(3ADEV\\)03544c62015-09-21 22:43:38 +0200681#ifdef CONFIG_SYS_I2C_MXC_I2C1
tremfac96402013-09-21 18:13:35 +0200682U_BOOT_I2C_ADAP_COMPLETE(mxc0, mxc_i2c_init, mxc_i2c_probe,
683 mxc_i2c_read, mxc_i2c_write,
684 mxc_i2c_set_bus_speed,
685 CONFIG_SYS_MXC_I2C1_SPEED,
686 CONFIG_SYS_MXC_I2C1_SLAVE, 0)
Albert ARIBAUD \\(3ADEV\\)03544c62015-09-21 22:43:38 +0200687#endif
688
689#ifdef CONFIG_SYS_I2C_MXC_I2C2
tremfac96402013-09-21 18:13:35 +0200690U_BOOT_I2C_ADAP_COMPLETE(mxc1, mxc_i2c_init, mxc_i2c_probe,
691 mxc_i2c_read, mxc_i2c_write,
692 mxc_i2c_set_bus_speed,
693 CONFIG_SYS_MXC_I2C2_SPEED,
694 CONFIG_SYS_MXC_I2C2_SLAVE, 1)
Albert ARIBAUD \\(3ADEV\\)03544c62015-09-21 22:43:38 +0200695#endif
696
York Sunf8cb1012015-03-20 10:20:40 -0700697#ifdef CONFIG_SYS_I2C_MXC_I2C3
tremfac96402013-09-21 18:13:35 +0200698U_BOOT_I2C_ADAP_COMPLETE(mxc2, mxc_i2c_init, mxc_i2c_probe,
699 mxc_i2c_read, mxc_i2c_write,
700 mxc_i2c_set_bus_speed,
701 CONFIG_SYS_MXC_I2C3_SPEED,
702 CONFIG_SYS_MXC_I2C3_SLAVE, 2)
703#endif
Peng Fan71204e92015-05-15 07:29:12 +0800704
York Sunf8cb1012015-03-20 10:20:40 -0700705#ifdef CONFIG_SYS_I2C_MXC_I2C4
706U_BOOT_I2C_ADAP_COMPLETE(mxc3, mxc_i2c_init, mxc_i2c_probe,
707 mxc_i2c_read, mxc_i2c_write,
708 mxc_i2c_set_bus_speed,
709 CONFIG_SYS_MXC_I2C4_SPEED,
710 CONFIG_SYS_MXC_I2C4_SLAVE, 3)
711#endif
Peng Fan71204e92015-05-15 07:29:12 +0800712
713#else
714
715static int mxc_i2c_set_bus_speed(struct udevice *bus, unsigned int speed)
716{
717 struct mxc_i2c_bus *i2c_bus = dev_get_priv(bus);
718
719 return bus_i2c_set_bus_speed(i2c_bus, speed);
720}
721
722static int mxc_i2c_probe(struct udevice *bus)
723{
724 struct mxc_i2c_bus *i2c_bus = dev_get_priv(bus);
Peng Fane1bed802016-03-11 16:47:50 +0800725 const void *fdt = gd->fdt_blob;
726 int node = bus->of_offset;
Peng Fan71204e92015-05-15 07:29:12 +0800727 fdt_addr_t addr;
Peng Fane1bed802016-03-11 16:47:50 +0800728 int ret, ret2;
Peng Fan71204e92015-05-15 07:29:12 +0800729
730 i2c_bus->driver_data = dev_get_driver_data(bus);
731
732 addr = dev_get_addr(bus);
733 if (addr == FDT_ADDR_T_NONE)
734 return -ENODEV;
735
736 i2c_bus->base = addr;
737 i2c_bus->index = bus->seq;
Peng Fane1bed802016-03-11 16:47:50 +0800738 i2c_bus->bus = bus;
Peng Fan71204e92015-05-15 07:29:12 +0800739
740 /* Enable clk */
741 ret = enable_i2c_clk(1, bus->seq);
742 if (ret < 0)
743 return ret;
744
Peng Fane1bed802016-03-11 16:47:50 +0800745 /*
746 * See Documentation/devicetree/bindings/i2c/i2c-imx.txt
747 * Use gpio to force bus idle when necessary.
748 */
749 ret = fdt_find_string(fdt, node, "pinctrl-names", "gpio");
750 if (ret < 0) {
751 dev_info(dev, "i2c bus %d at %lu, no gpio pinctrl state.\n", bus->seq, i2c_bus->base);
752 } else {
753 ret = gpio_request_by_name_nodev(fdt, node, "scl-gpios",
754 0, &i2c_bus->scl_gpio,
755 GPIOD_IS_OUT);
756 ret2 = gpio_request_by_name_nodev(fdt, node, "sda-gpios",
757 0, &i2c_bus->sda_gpio,
758 GPIOD_IS_OUT);
759 if (!dm_gpio_is_valid(&i2c_bus->sda_gpio) |
760 !dm_gpio_is_valid(&i2c_bus->scl_gpio) |
761 ret | ret2) {
762 dev_err(dev, "i2c bus %d at %lu, fail to request scl/sda gpio\n", bus->seq, i2c_bus->base);
763 return -ENODEV;
764 }
765 }
766
Peng Fan71204e92015-05-15 07:29:12 +0800767 ret = i2c_idle_bus(i2c_bus);
768 if (ret < 0) {
769 /* Disable clk */
770 enable_i2c_clk(0, bus->seq);
771 return ret;
772 }
773
774 /*
775 * Pinmux settings are in board file now, until pinmux is supported,
776 * we can set pinmux here in probe function.
777 */
778
779 debug("i2c : controller bus %d at %lu , speed %d: ",
780 bus->seq, i2c_bus->base,
781 i2c_bus->speed);
782
783 return 0;
784}
785
786static int mxc_i2c_probe_chip(struct udevice *bus, u32 chip_addr,
787 u32 chip_flags)
788{
789 int ret;
790 struct mxc_i2c_bus *i2c_bus = dev_get_priv(bus);
791
792 ret = i2c_init_transfer(i2c_bus, chip_addr, 0, 0);
793 if (ret < 0) {
794 debug("%s failed, ret = %d\n", __func__, ret);
795 return ret;
796 }
797
798 i2c_imx_stop(i2c_bus);
799
800 return 0;
801}
802
803static int mxc_i2c_xfer(struct udevice *bus, struct i2c_msg *msg, int nmsgs)
804{
805 struct mxc_i2c_bus *i2c_bus = dev_get_priv(bus);
806 int ret = 0;
807 ulong base = i2c_bus->base;
808 int reg_shift = i2c_bus->driver_data & I2C_QUIRK_FLAG ?
809 VF610_I2C_REGSHIFT : IMX_I2C_REGSHIFT;
810
811 /*
812 * Here the 3rd parameter addr and the 4th one alen are set to 0,
813 * because here we only want to send out chip address. The register
814 * address is wrapped in msg.
815 */
816 ret = i2c_init_transfer(i2c_bus, msg->addr, 0, 0);
817 if (ret < 0) {
818 debug("i2c_init_transfer error: %d\n", ret);
819 return ret;
820 }
821
822 for (; nmsgs > 0; nmsgs--, msg++) {
823 bool next_is_read = nmsgs > 1 && (msg[1].flags & I2C_M_RD);
824 debug("i2c_xfer: chip=0x%x, len=0x%x\n", msg->addr, msg->len);
825 if (msg->flags & I2C_M_RD)
826 ret = i2c_read_data(i2c_bus, msg->addr, msg->buf,
827 msg->len);
828 else {
829 ret = i2c_write_data(i2c_bus, msg->addr, msg->buf,
830 msg->len);
831 if (ret)
832 break;
833 if (next_is_read) {
834 /* Reuse ret */
835 ret = readb(base + (I2CR << reg_shift));
836 ret |= I2CR_RSTA;
837 writeb(ret, base + (I2CR << reg_shift));
838
839 ret = tx_byte(i2c_bus, (msg->addr << 1) | 1);
840 if (ret < 0) {
841 i2c_imx_stop(i2c_bus);
842 break;
843 }
844 }
845 }
846 }
847
848 if (ret)
849 debug("i2c_write: error sending\n");
850
851 i2c_imx_stop(i2c_bus);
852
853 return ret;
854}
855
856static const struct dm_i2c_ops mxc_i2c_ops = {
857 .xfer = mxc_i2c_xfer,
858 .probe_chip = mxc_i2c_probe_chip,
859 .set_bus_speed = mxc_i2c_set_bus_speed,
860};
861
862static const struct udevice_id mxc_i2c_ids[] = {
863 { .compatible = "fsl,imx21-i2c", },
864 { .compatible = "fsl,vf610-i2c", .data = I2C_QUIRK_FLAG, },
865 {}
866};
867
868U_BOOT_DRIVER(i2c_mxc) = {
869 .name = "i2c_mxc",
870 .id = UCLASS_I2C,
871 .of_match = mxc_i2c_ids,
872 .probe = mxc_i2c_probe,
873 .priv_auto_alloc_size = sizeof(struct mxc_i2c_bus),
874 .ops = &mxc_i2c_ops,
875};
876#endif