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