blob: a03c465c8f3ff75393eeab7bb3e2de1de2290f1c [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Sascha Hauer15ea70f2008-03-26 20:40:49 +01002/*
Marek Vasut94cb8422011-09-22 09:22:12 +00003 * i2c driver for Freescale i.MX series
Sascha Hauer15ea70f2008-03-26 20:40:49 +01004 *
5 * (c) 2007 Pengutronix, Sascha Hauer <s.hauer@pengutronix.de>
Marek Vasut94cb8422011-09-22 09:22:12 +00006 * (c) 2011 Marek Vasut <marek.vasut@gmail.com>
7 *
8 * Based on i2c-imx.c from linux kernel:
9 * Copyright (C) 2005 Torsten Koschorrek <koschorrek at synertronixx.de>
10 * Copyright (C) 2005 Matthias Blaschke <blaschke at synertronixx.de>
11 * Copyright (C) 2007 RightHand Technologies, Inc.
12 * Copyright (C) 2008 Darius Augulis <darius.augulis at teltonika.lt>
13 *
Sascha Hauer15ea70f2008-03-26 20:40:49 +010014 */
15
16#include <common.h>
Liu Hui-R64343447beb12011-01-03 22:27:39 +000017#include <asm/arch/clock.h>
Stefano Babic78129d92011-03-14 15:43:56 +010018#include <asm/arch/imx-regs.h>
Simon Glass9bc15642020-02-03 07:36:16 -070019#include <dm/device_compat.h>
Masahiro Yamada56a931c2016-09-21 11:28:55 +090020#include <linux/errno.h>
Stefano Babic33731bc2017-06-29 10:16:06 +020021#include <asm/mach-imx/mxc_i2c.h>
Troy Kisky2254b7f2012-07-19 08:18:03 +000022#include <asm/io.h>
Marek Vasut5f1291e2011-10-26 00:05:44 +000023#include <i2c.h>
Troy Kiskyf024a3b2012-07-19 08:18:09 +000024#include <watchdog.h>
Peng Fan8262cb12015-05-15 07:29:12 +080025#include <dm.h>
Peng Fan4f1a5812016-03-11 16:47:50 +080026#include <dm/pinctrl.h>
Peng Fan8262cb12015-05-15 07:29:12 +080027#include <fdtdec.h>
Sascha Hauer15ea70f2008-03-26 20:40:49 +010028
York Suna4c02662014-02-10 14:02:52 -080029DECLARE_GLOBAL_DATA_PTR;
30
Peng Fan8262cb12015-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 Yaod40c8852016-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 Fan8262cb12015-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 Hauer15ea70f2008-03-26 20:40:49 +010049
Sascha Hauer15ea70f2008-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 Kisky8ff683a2012-07-19 08:18:15 +000058#define I2SR_IAL (1 << 4)
Sascha Hauer15ea70f2008-03-26 20:40:49 +010059#define I2SR_IIF (1 << 1)
60#define I2SR_RX_NO_AK (1 << 0)
61
Alison Wangcf508002013-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 Wangcf508002013-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 Vasut94cb8422011-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 Wangcf508002013-06-17 15:30:39 +0800106#endif
Sascha Hauer15ea70f2008-03-26 20:40:49 +0100107
trema49f40a2013-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 Sunf1a52162015-03-20 10:20:40 -0700117#ifndef CONFIG_SYS_MXC_I2C4_SPEED
118#define CONFIG_SYS_MXC_I2C4_SPEED 100000
119#endif
trema49f40a2013-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 Sunf1a52162015-03-20 10:20:40 -0700130#ifndef CONFIG_SYS_MXC_I2C4_SLAVE
131#define CONFIG_SYS_MXC_I2C4_SLAVE 0
132#endif
trema49f40a2013-09-21 18:13:35 +0200133
Marek Vasut94cb8422011-09-22 09:22:12 +0000134/*
135 * Calculate and set proper clock divider
136 */
Peng Fan8262cb12015-05-15 07:29:12 +0800137static uint8_t i2c_imx_get_clk(struct mxc_i2c_bus *i2c_bus, unsigned int rate)
Sascha Hauer15ea70f2008-03-26 20:40:49 +0100138{
Marek Vasut94cb8422011-09-22 09:22:12 +0000139 unsigned int i2c_clk_rate;
140 unsigned int div;
Marek Vasut5f1291e2011-10-26 00:05:44 +0000141 u8 clk_div;
Sascha Hauer15ea70f2008-03-26 20:40:49 +0100142
Liu Hui-R64343447beb12011-01-03 22:27:39 +0000143#if defined(CONFIG_MX31)
Stefano Babic22121722011-01-20 07:50:44 +0000144 struct clock_control_regs *sc_regs =
145 (struct clock_control_regs *)CCM_BASE;
Marek Vasut94cb8422011-09-22 09:22:12 +0000146
Guennadi Liakhovetski3314fc62009-02-13 09:23:36 +0100147 /* start the required I2C clock */
Troy Kisky8462c632012-04-24 17:33:25 +0000148 writel(readl(&sc_regs->cgr0) | (3 << CONFIG_SYS_I2C_CLK_OFFSET),
Stefano Babic22121722011-01-20 07:50:44 +0000149 &sc_regs->cgr0);
Liu Hui-R64343447beb12011-01-03 22:27:39 +0000150#endif
Guennadi Liakhovetski3314fc62009-02-13 09:23:36 +0100151
Marek Vasut94cb8422011-09-22 09:22:12 +0000152 /* Divider value calculation */
Peng Fan87ea5622019-08-08 01:43:30 +0000153#if CONFIG_IS_ENABLED(CLK)
154 i2c_clk_rate = clk_get_rate(&i2c_bus->per_clk);
155#else
Matthias Weisser99ba3422012-09-24 02:46:53 +0000156 i2c_clk_rate = mxc_get_clock(MXC_I2C_CLK);
Peng Fan87ea5622019-08-08 01:43:30 +0000157#endif
158
Marek Vasut94cb8422011-09-22 09:22:12 +0000159 div = (i2c_clk_rate + rate - 1) / rate;
160 if (div < i2c_clk_div[0][0])
Marek Vasut4f274442011-09-27 06:34:11 +0000161 clk_div = 0;
Marek Vasut94cb8422011-09-22 09:22:12 +0000162 else if (div > i2c_clk_div[ARRAY_SIZE(i2c_clk_div) - 1][0])
Marek Vasut4f274442011-09-27 06:34:11 +0000163 clk_div = ARRAY_SIZE(i2c_clk_div) - 1;
Marek Vasut94cb8422011-09-22 09:22:12 +0000164 else
Marek Vasut4f274442011-09-27 06:34:11 +0000165 for (clk_div = 0; i2c_clk_div[clk_div][0] < div; clk_div++)
Marek Vasut94cb8422011-09-22 09:22:12 +0000166 ;
167
168 /* Store divider value */
Marek Vasut5f1291e2011-10-26 00:05:44 +0000169 return clk_div;
Marek Vasut94cb8422011-09-22 09:22:12 +0000170}
Sascha Hauer15ea70f2008-03-26 20:40:49 +0100171
Marek Vasut94cb8422011-09-22 09:22:12 +0000172/*
Troy Kiskyae447602012-07-19 08:18:18 +0000173 * Set I2C Bus speed
Marek Vasut94cb8422011-09-22 09:22:12 +0000174 */
Peng Fan8262cb12015-05-15 07:29:12 +0800175static int bus_i2c_set_bus_speed(struct mxc_i2c_bus *i2c_bus, int speed)
Marek Vasut94cb8422011-09-22 09:22:12 +0000176{
Peng Fan8262cb12015-05-15 07:29:12 +0800177 ulong base = i2c_bus->base;
178 bool quirk = i2c_bus->driver_data & I2C_QUIRK_FLAG ? true : false;
179 u8 clk_idx = i2c_imx_get_clk(i2c_bus, speed);
Marek Vasut5f1291e2011-10-26 00:05:44 +0000180 u8 idx = i2c_clk_div[clk_idx][1];
Peng Fan8262cb12015-05-15 07:29:12 +0800181 int reg_shift = quirk ? VF610_I2C_REGSHIFT : IMX_I2C_REGSHIFT;
Marek Vasut5f1291e2011-10-26 00:05:44 +0000182
Heiko Schocher24ebcf22015-05-18 10:58:12 +0200183 if (!base)
Simon Glassf44b4bf2017-09-17 16:54:53 -0600184 return -EINVAL;
Heiko Schocher24ebcf22015-05-18 10:58:12 +0200185
Marek Vasut5f1291e2011-10-26 00:05:44 +0000186 /* Store divider value */
Peng Fan8262cb12015-05-15 07:29:12 +0800187 writeb(idx, base + (IFDR << reg_shift));
Marek Vasut5f1291e2011-10-26 00:05:44 +0000188
Troy Kiskye6fa4d72012-07-19 08:18:12 +0000189 /* Reset module */
Peng Fan8262cb12015-05-15 07:29:12 +0800190 writeb(I2CR_IDIS, base + (I2CR << reg_shift));
191 writeb(0, base + (I2SR << reg_shift));
Marek Vasut4f274442011-09-27 06:34:11 +0000192 return 0;
193}
194
Troy Kiskyf024a3b2012-07-19 08:18:09 +0000195#define ST_BUS_IDLE (0 | (I2SR_IBB << 8))
196#define ST_BUS_BUSY (I2SR_IBB | (I2SR_IBB << 8))
197#define ST_IIF (I2SR_IIF | (I2SR_IIF << 8))
Stefano Babic848bb992011-01-20 07:51:31 +0000198
Peng Fan8262cb12015-05-15 07:29:12 +0800199static int wait_for_sr_state(struct mxc_i2c_bus *i2c_bus, unsigned state)
Sascha Hauer15ea70f2008-03-26 20:40:49 +0100200{
Troy Kiskyf024a3b2012-07-19 08:18:09 +0000201 unsigned sr;
202 ulong elapsed;
Peng Fan8262cb12015-05-15 07:29:12 +0800203 bool quirk = i2c_bus->driver_data & I2C_QUIRK_FLAG ? true : false;
204 int reg_shift = quirk ? VF610_I2C_REGSHIFT : IMX_I2C_REGSHIFT;
205 ulong base = i2c_bus->base;
Troy Kiskyf024a3b2012-07-19 08:18:09 +0000206 ulong start_time = get_timer(0);
207 for (;;) {
Peng Fan8262cb12015-05-15 07:29:12 +0800208 sr = readb(base + (I2SR << reg_shift));
Troy Kisky8ff683a2012-07-19 08:18:15 +0000209 if (sr & I2SR_IAL) {
Peng Fan8262cb12015-05-15 07:29:12 +0800210 if (quirk)
211 writeb(sr | I2SR_IAL, base +
212 (I2SR << reg_shift));
213 else
214 writeb(sr & ~I2SR_IAL, base +
215 (I2SR << reg_shift));
Troy Kisky8ff683a2012-07-19 08:18:15 +0000216 printf("%s: Arbitration lost sr=%x cr=%x state=%x\n",
Peng Fan8262cb12015-05-15 07:29:12 +0800217 __func__, sr, readb(base + (I2CR << reg_shift)),
218 state);
Troy Kisky8ff683a2012-07-19 08:18:15 +0000219 return -ERESTART;
220 }
Troy Kiskyf024a3b2012-07-19 08:18:09 +0000221 if ((sr & (state >> 8)) == (unsigned char)state)
222 return sr;
223 WATCHDOG_RESET();
224 elapsed = get_timer(start_time);
225 if (elapsed > (CONFIG_SYS_HZ / 10)) /* .1 seconds */
226 break;
Marek Vasut94cb8422011-09-22 09:22:12 +0000227 }
Troy Kiskyf024a3b2012-07-19 08:18:09 +0000228 printf("%s: failed sr=%x cr=%x state=%x\n", __func__,
Peng Fan8262cb12015-05-15 07:29:12 +0800229 sr, readb(base + (I2CR << reg_shift)), state);
Troy Kisky752ac8f2012-07-19 08:18:04 +0000230 return -ETIMEDOUT;
Sascha Hauer15ea70f2008-03-26 20:40:49 +0100231}
232
Peng Fan8262cb12015-05-15 07:29:12 +0800233static int tx_byte(struct mxc_i2c_bus *i2c_bus, u8 byte)
Stefano Babic848bb992011-01-20 07:51:31 +0000234{
Troy Kisky752ac8f2012-07-19 08:18:04 +0000235 int ret;
Peng Fan8262cb12015-05-15 07:29:12 +0800236 int reg_shift = i2c_bus->driver_data & I2C_QUIRK_FLAG ?
237 VF610_I2C_REGSHIFT : IMX_I2C_REGSHIFT;
238 ulong base = i2c_bus->base;
Stefano Babic848bb992011-01-20 07:51:31 +0000239
Peng Fan8262cb12015-05-15 07:29:12 +0800240 writeb(I2SR_IIF_CLEAR, base + (I2SR << reg_shift));
241 writeb(byte, base + (I2DR << reg_shift));
242
243 ret = wait_for_sr_state(i2c_bus, ST_IIF);
Troy Kisky752ac8f2012-07-19 08:18:04 +0000244 if (ret < 0)
245 return ret;
Troy Kisky752ac8f2012-07-19 08:18:04 +0000246 if (ret & I2SR_RX_NO_AK)
Simon Glassf44b4bf2017-09-17 16:54:53 -0600247 return -EREMOTEIO;
Troy Kisky752ac8f2012-07-19 08:18:04 +0000248 return 0;
Marek Vasut94cb8422011-09-22 09:22:12 +0000249}
Peng Fan8262cb12015-05-15 07:29:12 +0800250
251/*
252 * Stub implementations for outer i2c slave operations.
253 */
254void __i2c_force_reset_slave(void)
255{
256}
257void i2c_force_reset_slave(void)
258 __attribute__((weak, alias("__i2c_force_reset_slave")));
Stefano Babic848bb992011-01-20 07:51:31 +0000259
Marek Vasut94cb8422011-09-22 09:22:12 +0000260/*
Troy Kiskyfef163f2012-07-19 08:18:13 +0000261 * Stop I2C transaction
Marek Vasut94cb8422011-09-22 09:22:12 +0000262 */
Peng Fan8262cb12015-05-15 07:29:12 +0800263static void i2c_imx_stop(struct mxc_i2c_bus *i2c_bus)
Sascha Hauer15ea70f2008-03-26 20:40:49 +0100264{
Troy Kiskyf024a3b2012-07-19 08:18:09 +0000265 int ret;
Peng Fan8262cb12015-05-15 07:29:12 +0800266 int reg_shift = i2c_bus->driver_data & I2C_QUIRK_FLAG ?
267 VF610_I2C_REGSHIFT : IMX_I2C_REGSHIFT;
268 ulong base = i2c_bus->base;
269 unsigned int temp = readb(base + (I2CR << reg_shift));
Sascha Hauer15ea70f2008-03-26 20:40:49 +0100270
Troy Kisky1ac1e452012-07-19 08:18:02 +0000271 temp &= ~(I2CR_MSTA | I2CR_MTX);
Peng Fan8262cb12015-05-15 07:29:12 +0800272 writeb(temp, base + (I2CR << reg_shift));
273 ret = wait_for_sr_state(i2c_bus, ST_BUS_IDLE);
Troy Kiskyf024a3b2012-07-19 08:18:09 +0000274 if (ret < 0)
275 printf("%s:trigger stop failed\n", __func__);
Sascha Hauer15ea70f2008-03-26 20:40:49 +0100276}
277
Marek Vasut94cb8422011-09-22 09:22:12 +0000278/*
Troy Kisky14db6f22012-07-19 08:18:06 +0000279 * Send start signal, chip address and
280 * write register address
Marek Vasut94cb8422011-09-22 09:22:12 +0000281 */
Peng Fan8262cb12015-05-15 07:29:12 +0800282static int i2c_init_transfer_(struct mxc_i2c_bus *i2c_bus, u8 chip,
283 u32 addr, int alen)
Sascha Hauer15ea70f2008-03-26 20:40:49 +0100284{
Troy Kiskya974bcc2012-07-19 08:18:11 +0000285 unsigned int temp;
286 int ret;
Peng Fan8262cb12015-05-15 07:29:12 +0800287 bool quirk = i2c_bus->driver_data & I2C_QUIRK_FLAG ? true : false;
288 ulong base = i2c_bus->base;
289 int reg_shift = quirk ? VF610_I2C_REGSHIFT : IMX_I2C_REGSHIFT;
290
291 /* Reset i2c slave */
292 i2c_force_reset_slave();
Troy Kiskya974bcc2012-07-19 08:18:11 +0000293
294 /* Enable I2C controller */
Peng Fan8262cb12015-05-15 07:29:12 +0800295 if (quirk)
296 ret = readb(base + (I2CR << reg_shift)) & I2CR_IDIS;
297 else
298 ret = !(readb(base + (I2CR << reg_shift)) & I2CR_IEN);
299
300 if (ret) {
301 writeb(I2CR_IEN, base + (I2CR << reg_shift));
Troy Kiskyfef163f2012-07-19 08:18:13 +0000302 /* Wait for controller to be stable */
303 udelay(50);
304 }
Peng Fan8262cb12015-05-15 07:29:12 +0800305
306 if (readb(base + (IADR << reg_shift)) == (chip << 1))
307 writeb((chip << 1) ^ 2, base + (IADR << reg_shift));
308 writeb(I2SR_IIF_CLEAR, base + (I2SR << reg_shift));
309 ret = wait_for_sr_state(i2c_bus, ST_BUS_IDLE);
Troy Kiskyfef163f2012-07-19 08:18:13 +0000310 if (ret < 0)
Troy Kiskyeca037a2012-07-19 08:18:16 +0000311 return ret;
Troy Kiskya974bcc2012-07-19 08:18:11 +0000312
313 /* Start I2C transaction */
Peng Fan8262cb12015-05-15 07:29:12 +0800314 temp = readb(base + (I2CR << reg_shift));
Troy Kiskya974bcc2012-07-19 08:18:11 +0000315 temp |= I2CR_MSTA;
Peng Fan8262cb12015-05-15 07:29:12 +0800316 writeb(temp, base + (I2CR << reg_shift));
Troy Kiskya974bcc2012-07-19 08:18:11 +0000317
Peng Fan8262cb12015-05-15 07:29:12 +0800318 ret = wait_for_sr_state(i2c_bus, ST_BUS_BUSY);
Troy Kiskya974bcc2012-07-19 08:18:11 +0000319 if (ret < 0)
Troy Kiskyeca037a2012-07-19 08:18:16 +0000320 return ret;
Troy Kisky14db6f22012-07-19 08:18:06 +0000321
Troy Kiskya974bcc2012-07-19 08:18:11 +0000322 temp |= I2CR_MTX | I2CR_TX_NO_AK;
Peng Fan8262cb12015-05-15 07:29:12 +0800323 writeb(temp, base + (I2CR << reg_shift));
Troy Kiskya974bcc2012-07-19 08:18:11 +0000324
Nandor Hana51f9da2017-11-08 15:35:09 +0000325 if (alen >= 0) {
326 /* write slave address */
327 ret = tx_byte(i2c_bus, chip << 1);
Troy Kisky752ac8f2012-07-19 08:18:04 +0000328 if (ret < 0)
Troy Kiskyeca037a2012-07-19 08:18:16 +0000329 return ret;
Nandor Hana51f9da2017-11-08 15:35:09 +0000330
331 while (alen--) {
332 ret = tx_byte(i2c_bus, (addr >> (alen * 8)) & 0xff);
333 if (ret < 0)
334 return ret;
335 }
Stefano Babic848bb992011-01-20 07:51:31 +0000336 }
Nandor Hana51f9da2017-11-08 15:35:09 +0000337
Troy Kisky14db6f22012-07-19 08:18:06 +0000338 return 0;
Troy Kiskyeca037a2012-07-19 08:18:16 +0000339}
340
Peng Fan8262cb12015-05-15 07:29:12 +0800341#ifndef CONFIG_DM_I2C
342int i2c_idle_bus(struct mxc_i2c_bus *i2c_bus)
343{
344 if (i2c_bus && i2c_bus->idle_bus_fn)
345 return i2c_bus->idle_bus_fn(i2c_bus->idle_bus_data);
346 return 0;
347}
348#else
349/*
Peng Fan4f1a5812016-03-11 16:47:50 +0800350 * See Linux Documentation/devicetree/bindings/i2c/i2c-imx.txt
351 * "
352 * scl-gpios: specify the gpio related to SCL pin
353 * sda-gpios: specify the gpio related to SDA pin
354 * add pinctrl to configure i2c pins to gpio function for i2c
355 * bus recovery, call it "gpio" state
356 * "
357 *
358 * The i2c_idle_bus is an implementation following Linux Kernel.
Peng Fan8262cb12015-05-15 07:29:12 +0800359 */
Peng Fan4f1a5812016-03-11 16:47:50 +0800360int i2c_idle_bus(struct mxc_i2c_bus *i2c_bus)
Peng Fan8262cb12015-05-15 07:29:12 +0800361{
Peng Fan4f1a5812016-03-11 16:47:50 +0800362 struct udevice *bus = i2c_bus->bus;
Lukasz Majewskibc9aad62019-04-04 12:35:34 +0200363 struct dm_i2c_bus *i2c = dev_get_uclass_priv(bus);
Peng Fan4f1a5812016-03-11 16:47:50 +0800364 struct gpio_desc *scl_gpio = &i2c_bus->scl_gpio;
365 struct gpio_desc *sda_gpio = &i2c_bus->sda_gpio;
Lukasz Majewskibc9aad62019-04-04 12:35:34 +0200366 int sda, scl, idle_sclks;
Peng Fan4f1a5812016-03-11 16:47:50 +0800367 int i, ret = 0;
368 ulong elapsed, start_time;
Peng Fan8262cb12015-05-15 07:29:12 +0800369
Peng Fan4f1a5812016-03-11 16:47:50 +0800370 if (pinctrl_select_state(bus, "gpio")) {
371 dev_dbg(bus, "Can not to switch to use gpio pinmux\n");
372 /*
373 * GPIO pinctrl for i2c force idle is not a must,
374 * but it is strongly recommended to be used.
375 * Because it can help you to recover from bad
376 * i2c bus state. Do not return failure, because
377 * it is not a must.
378 */
379 return 0;
380 }
381
382 dm_gpio_set_dir_flags(scl_gpio, GPIOD_IS_IN);
383 dm_gpio_set_dir_flags(sda_gpio, GPIOD_IS_IN);
384 scl = dm_gpio_get_value(scl_gpio);
385 sda = dm_gpio_get_value(sda_gpio);
386
387 if ((sda & scl) == 1)
388 goto exit; /* Bus is idle already */
389
Lukasz Majewskibc9aad62019-04-04 12:35:34 +0200390 /*
391 * In most cases it is just enough to generate 8 + 1 SCLK
392 * clocks to recover I2C slave device from 'stuck' state
393 * (when for example SW reset was performed, in the middle of
394 * I2C transmission).
395 *
396 * However, there are devices which send data in packets of
397 * N bytes (N > 1). In such case we do need N * 8 + 1 SCLK
398 * clocks.
399 */
400 idle_sclks = 8 + 1;
401
402 if (i2c->max_transaction_bytes > 0)
403 idle_sclks = i2c->max_transaction_bytes * 8 + 1;
Peng Fan4f1a5812016-03-11 16:47:50 +0800404 /* Send high and low on the SCL line */
Lukasz Majewskibc9aad62019-04-04 12:35:34 +0200405 for (i = 0; i < idle_sclks; i++) {
Peng Fan4f1a5812016-03-11 16:47:50 +0800406 dm_gpio_set_dir_flags(scl_gpio, GPIOD_IS_OUT);
407 dm_gpio_set_value(scl_gpio, 0);
408 udelay(50);
409 dm_gpio_set_dir_flags(scl_gpio, GPIOD_IS_IN);
410 udelay(50);
411 }
412 start_time = get_timer(0);
413 for (;;) {
414 dm_gpio_set_dir_flags(scl_gpio, GPIOD_IS_IN);
415 dm_gpio_set_dir_flags(sda_gpio, GPIOD_IS_IN);
416 scl = dm_gpio_get_value(scl_gpio);
417 sda = dm_gpio_get_value(sda_gpio);
418 if ((sda & scl) == 1)
419 break;
420 WATCHDOG_RESET();
421 elapsed = get_timer(start_time);
422 if (elapsed > (CONFIG_SYS_HZ / 5)) { /* .2 seconds */
423 ret = -EBUSY;
424 printf("%s: failed to clear bus, sda=%d scl=%d\n", __func__, sda, scl);
425 break;
426 }
427 }
428
429exit:
430 pinctrl_select_state(bus, "default");
431 return ret;
432}
Peng Fan8262cb12015-05-15 07:29:12 +0800433#endif
Troy Kiskya23ab222012-07-19 08:18:19 +0000434
Peng Fan8262cb12015-05-15 07:29:12 +0800435static int i2c_init_transfer(struct mxc_i2c_bus *i2c_bus, u8 chip,
436 u32 addr, int alen)
Troy Kiskyeca037a2012-07-19 08:18:16 +0000437{
438 int retry;
439 int ret;
Peng Fan8262cb12015-05-15 07:29:12 +0800440 int reg_shift = i2c_bus->driver_data & I2C_QUIRK_FLAG ?
441 VF610_I2C_REGSHIFT : IMX_I2C_REGSHIFT;
Heiko Schocher24ebcf22015-05-18 10:58:12 +0200442
443 if (!i2c_bus->base)
Simon Glassf44b4bf2017-09-17 16:54:53 -0600444 return -EINVAL;
Heiko Schocher24ebcf22015-05-18 10:58:12 +0200445
Troy Kiskyeca037a2012-07-19 08:18:16 +0000446 for (retry = 0; retry < 3; retry++) {
Peng Fan8262cb12015-05-15 07:29:12 +0800447 ret = i2c_init_transfer_(i2c_bus, chip, addr, alen);
Troy Kiskyeca037a2012-07-19 08:18:16 +0000448 if (ret >= 0)
449 return 0;
Peng Fan8262cb12015-05-15 07:29:12 +0800450 i2c_imx_stop(i2c_bus);
Simon Glassf44b4bf2017-09-17 16:54:53 -0600451 if (ret == -EREMOTEIO)
Troy Kiskyeca037a2012-07-19 08:18:16 +0000452 return ret;
453
454 printf("%s: failed for chip 0x%x retry=%d\n", __func__, chip,
455 retry);
456 if (ret != -ERESTART)
Alison Wangcf508002013-06-17 15:30:39 +0800457 /* Disable controller */
Peng Fan8262cb12015-05-15 07:29:12 +0800458 writeb(I2CR_IDIS, i2c_bus->base + (I2CR << reg_shift));
Troy Kiskyeca037a2012-07-19 08:18:16 +0000459 udelay(100);
Peng Fan8262cb12015-05-15 07:29:12 +0800460 if (i2c_idle_bus(i2c_bus) < 0)
Troy Kiskya23ab222012-07-19 08:18:19 +0000461 break;
Troy Kiskyeca037a2012-07-19 08:18:16 +0000462 }
Peng Fan8262cb12015-05-15 07:29:12 +0800463 printf("%s: give up i2c_regs=0x%lx\n", __func__, i2c_bus->base);
Marek Vasut94cb8422011-09-22 09:22:12 +0000464 return ret;
Sascha Hauer15ea70f2008-03-26 20:40:49 +0100465}
466
Peng Fan8262cb12015-05-15 07:29:12 +0800467
468static int i2c_write_data(struct mxc_i2c_bus *i2c_bus, u8 chip, const u8 *buf,
469 int len)
470{
471 int i, ret = 0;
472
473 debug("i2c_write_data: chip=0x%x, len=0x%x\n", chip, len);
474 debug("write_data: ");
475 /* use rc for counter */
476 for (i = 0; i < len; ++i)
477 debug(" 0x%02x", buf[i]);
478 debug("\n");
479
480 for (i = 0; i < len; i++) {
481 ret = tx_byte(i2c_bus, buf[i]);
482 if (ret < 0) {
483 debug("i2c_write_data(): rc=%d\n", ret);
484 break;
485 }
486 }
487
488 return ret;
489}
490
Trent Piepho9c896a92019-04-30 16:08:19 +0000491/* Will generate a STOP after the last byte if "last" is true, i.e. this is the
492 * final message of a transaction. If not, it switches the bus back to TX mode
493 * and does not send a STOP, leaving the bus in a state where a repeated start
494 * and address can be sent for another message.
495 */
Peng Fan8262cb12015-05-15 07:29:12 +0800496static int i2c_read_data(struct mxc_i2c_bus *i2c_bus, uchar chip, uchar *buf,
Trent Piepho9c896a92019-04-30 16:08:19 +0000497 int len, bool last)
Sascha Hauer15ea70f2008-03-26 20:40:49 +0100498{
Sascha Hauer15ea70f2008-03-26 20:40:49 +0100499 int ret;
Marek Vasut94cb8422011-09-22 09:22:12 +0000500 unsigned int temp;
501 int i;
Peng Fan8262cb12015-05-15 07:29:12 +0800502 int reg_shift = i2c_bus->driver_data & I2C_QUIRK_FLAG ?
503 VF610_I2C_REGSHIFT : IMX_I2C_REGSHIFT;
504 ulong base = i2c_bus->base;
Sascha Hauer15ea70f2008-03-26 20:40:49 +0100505
Peng Fan8262cb12015-05-15 07:29:12 +0800506 debug("i2c_read_data: chip=0x%x, len=0x%x\n", chip, len);
Sascha Hauer15ea70f2008-03-26 20:40:49 +0100507
Marek Vasut94cb8422011-09-22 09:22:12 +0000508 /* setup bus to read data */
Peng Fan8262cb12015-05-15 07:29:12 +0800509 temp = readb(base + (I2CR << reg_shift));
Marek Vasut94cb8422011-09-22 09:22:12 +0000510 temp &= ~(I2CR_MTX | I2CR_TX_NO_AK);
511 if (len == 1)
512 temp |= I2CR_TX_NO_AK;
Peng Fan8262cb12015-05-15 07:29:12 +0800513 writeb(temp, base + (I2CR << reg_shift));
514 writeb(I2SR_IIF_CLEAR, base + (I2SR << reg_shift));
515 /* dummy read to clear ICF */
516 readb(base + (I2DR << reg_shift));
Sascha Hauer15ea70f2008-03-26 20:40:49 +0100517
Marek Vasut94cb8422011-09-22 09:22:12 +0000518 /* read data */
519 for (i = 0; i < len; i++) {
Peng Fan8262cb12015-05-15 07:29:12 +0800520 ret = wait_for_sr_state(i2c_bus, ST_IIF);
Troy Kiskyf024a3b2012-07-19 08:18:09 +0000521 if (ret < 0) {
Peng Fan8262cb12015-05-15 07:29:12 +0800522 debug("i2c_read_data(): ret=%d\n", ret);
523 i2c_imx_stop(i2c_bus);
Marek Vasut94cb8422011-09-22 09:22:12 +0000524 return ret;
Troy Kisky0ce898d2012-07-19 08:18:07 +0000525 }
Sascha Hauer15ea70f2008-03-26 20:40:49 +0100526
Marek Vasut94cb8422011-09-22 09:22:12 +0000527 if (i == (len - 1)) {
Trent Piepho9c896a92019-04-30 16:08:19 +0000528 /* Final byte has already been received by master! When
529 * we read it from I2DR, the master will start another
530 * cycle. We must program it first to send a STOP or
531 * switch to TX to avoid this.
532 */
533 if (last) {
534 i2c_imx_stop(i2c_bus);
535 } else {
536 /* Final read, no stop, switch back to tx */
537 temp = readb(base + (I2CR << reg_shift));
538 temp |= I2CR_MTX | I2CR_TX_NO_AK;
539 writeb(temp, base + (I2CR << reg_shift));
540 }
Marek Vasut94cb8422011-09-22 09:22:12 +0000541 } else if (i == (len - 2)) {
Trent Piepho9c896a92019-04-30 16:08:19 +0000542 /* Master has already recevied penultimate byte. When
543 * we read it from I2DR, master will start RX of final
544 * byte. We must set TX_NO_AK now so it does not ACK
545 * that final byte.
546 */
Peng Fan8262cb12015-05-15 07:29:12 +0800547 temp = readb(base + (I2CR << reg_shift));
Marek Vasut94cb8422011-09-22 09:22:12 +0000548 temp |= I2CR_TX_NO_AK;
Peng Fan8262cb12015-05-15 07:29:12 +0800549 writeb(temp, base + (I2CR << reg_shift));
Marek Vasut94cb8422011-09-22 09:22:12 +0000550 }
Trent Piepho9c896a92019-04-30 16:08:19 +0000551
Peng Fan8262cb12015-05-15 07:29:12 +0800552 writeb(I2SR_IIF_CLEAR, base + (I2SR << reg_shift));
553 buf[i] = readb(base + (I2DR << reg_shift));
Marek Vasut94cb8422011-09-22 09:22:12 +0000554 }
Peng Fan8262cb12015-05-15 07:29:12 +0800555
556 /* reuse ret for counter*/
557 for (ret = 0; ret < len; ++ret)
558 debug(" 0x%02x", buf[ret]);
559 debug("\n");
560
Trent Piepho9c896a92019-04-30 16:08:19 +0000561 /* It is not clear to me that this is necessary */
562 if (last)
563 i2c_imx_stop(i2c_bus);
Troy Kiskyf024a3b2012-07-19 08:18:09 +0000564 return 0;
Sascha Hauer15ea70f2008-03-26 20:40:49 +0100565}
566
Chuanhua Hand24d2d92019-07-10 21:00:22 +0800567int __enable_i2c_clk(unsigned char enable, unsigned int i2c_num)
568{
569 return 1;
570}
571
572int enable_i2c_clk(unsigned char enable, unsigned int i2c_num)
573 __attribute__((weak, alias("__enable_i2c_clk")));
574
Peng Fan8262cb12015-05-15 07:29:12 +0800575#ifndef CONFIG_DM_I2C
Marek Vasut94cb8422011-09-22 09:22:12 +0000576/*
Peng Fan8262cb12015-05-15 07:29:12 +0800577 * Read data from I2C device
Trent Piepho15410ba2019-04-30 16:08:18 +0000578 *
579 * The transactions use the syntax defined in the Linux kernel I2C docs.
580 *
581 * If alen is > 0, then this function will send a transaction of the form:
582 * S Chip Wr [A] Addr [A] S Chip Rd [A] [data] A ... NA P
583 * This is a normal I2C register read: writing the register address, then doing
584 * a repeated start and reading the data.
585 *
586 * If alen == 0, then we get this transaction:
587 * S Chip Wr [A] S Chip Rd [A] [data] A ... NA P
588 * This is somewhat unusual, though valid, transaction. It addresses the chip
589 * in write mode, but doesn't actually write any register address or data, then
590 * does a repeated start and reads data.
591 *
592 * If alen < 0, then we get this transaction:
593 * S Chip Rd [A] [data] A ... NA P
594 * The chip is addressed in read mode and then data is read. No register
595 * address is written first. This is perfectly valid on most devices and
596 * required on some (usually those that don't act like an array of registers).
Marek Vasut94cb8422011-09-22 09:22:12 +0000597 */
Peng Fan8262cb12015-05-15 07:29:12 +0800598static int bus_i2c_read(struct mxc_i2c_bus *i2c_bus, u8 chip, u32 addr,
599 int alen, u8 *buf, int len)
Sascha Hauer15ea70f2008-03-26 20:40:49 +0100600{
Peng Fan8262cb12015-05-15 07:29:12 +0800601 int ret = 0;
602 u32 temp;
603 int reg_shift = i2c_bus->driver_data & I2C_QUIRK_FLAG ?
604 VF610_I2C_REGSHIFT : IMX_I2C_REGSHIFT;
605 ulong base = i2c_bus->base;
Sascha Hauer15ea70f2008-03-26 20:40:49 +0100606
Peng Fan8262cb12015-05-15 07:29:12 +0800607 ret = i2c_init_transfer(i2c_bus, chip, addr, alen);
Troy Kisky752ac8f2012-07-19 08:18:04 +0000608 if (ret < 0)
Marek Vasut94cb8422011-09-22 09:22:12 +0000609 return ret;
Sascha Hauer15ea70f2008-03-26 20:40:49 +0100610
Nandor Hana51f9da2017-11-08 15:35:09 +0000611 if (alen >= 0) {
612 temp = readb(base + (I2CR << reg_shift));
613 temp |= I2CR_RSTA;
614 writeb(temp, base + (I2CR << reg_shift));
615 }
Peng Fan8262cb12015-05-15 07:29:12 +0800616
617 ret = tx_byte(i2c_bus, (chip << 1) | 1);
618 if (ret < 0) {
619 i2c_imx_stop(i2c_bus);
620 return ret;
Marek Vasut94cb8422011-09-22 09:22:12 +0000621 }
Peng Fan8262cb12015-05-15 07:29:12 +0800622
Trent Piepho9c896a92019-04-30 16:08:19 +0000623 ret = i2c_read_data(i2c_bus, chip, buf, len, true);
Peng Fan8262cb12015-05-15 07:29:12 +0800624
625 i2c_imx_stop(i2c_bus);
626 return ret;
627}
628
629/*
630 * Write data to I2C device
Trent Piepho15410ba2019-04-30 16:08:18 +0000631 *
632 * If alen > 0, we get this transaction:
633 * S Chip Wr [A] addr [A] data [A] ... [A] P
634 * An ordinary write register command.
635 *
636 * If alen == 0, then we get this:
637 * S Chip Wr [A] data [A] ... [A] P
638 * This is a simple I2C write.
639 *
640 * If alen < 0, then we get this:
641 * S data [A] ... [A] P
642 * This is most likely NOT something that should be used. It doesn't send the
643 * chip address first, so in effect, the first byte of data will be used as the
644 * address.
Peng Fan8262cb12015-05-15 07:29:12 +0800645 */
646static int bus_i2c_write(struct mxc_i2c_bus *i2c_bus, u8 chip, u32 addr,
647 int alen, const u8 *buf, int len)
648{
649 int ret = 0;
650
651 ret = i2c_init_transfer(i2c_bus, chip, addr, alen);
652 if (ret < 0)
653 return ret;
654
655 ret = i2c_write_data(i2c_bus, chip, buf, len);
656
657 i2c_imx_stop(i2c_bus);
658
Marek Vasut94cb8422011-09-22 09:22:12 +0000659 return ret;
660}
Troy Kisky321a42b2012-07-19 08:18:08 +0000661
Heiko Schocher24ebcf22015-05-18 10:58:12 +0200662#if !defined(I2C2_BASE_ADDR)
663#define I2C2_BASE_ADDR 0
Heiko Schocher5c4b1e92015-05-18 10:56:24 +0200664#endif
Heiko Schocher24ebcf22015-05-18 10:58:12 +0200665
666#if !defined(I2C3_BASE_ADDR)
667#define I2C3_BASE_ADDR 0
668#endif
669
670#if !defined(I2C4_BASE_ADDR)
671#define I2C4_BASE_ADDR 0
672#endif
673
Sriram Dasha64aa192018-02-06 11:26:31 +0530674#if !defined(I2C5_BASE_ADDR)
675#define I2C5_BASE_ADDR 0
676#endif
677
678#if !defined(I2C6_BASE_ADDR)
679#define I2C6_BASE_ADDR 0
680#endif
681
682#if !defined(I2C7_BASE_ADDR)
683#define I2C7_BASE_ADDR 0
684#endif
685
686#if !defined(I2C8_BASE_ADDR)
687#define I2C8_BASE_ADDR 0
688#endif
689
Heiko Schocher24ebcf22015-05-18 10:58:12 +0200690static struct mxc_i2c_bus mxc_i2c_buses[] = {
York Sunc4f047c2017-03-27 11:41:03 -0700691#if defined(CONFIG_ARCH_LS1021A) || defined(CONFIG_VF610) || \
Mingkai Hu0e58b512015-10-26 19:47:50 +0800692 defined(CONFIG_FSL_LAYERSCAPE)
Peng Fan8262cb12015-05-15 07:29:12 +0800693 { 0, I2C1_BASE_ADDR, I2C_QUIRK_FLAG },
694 { 1, I2C2_BASE_ADDR, I2C_QUIRK_FLAG },
695 { 2, I2C3_BASE_ADDR, I2C_QUIRK_FLAG },
696 { 3, I2C4_BASE_ADDR, I2C_QUIRK_FLAG },
Sriram Dasha64aa192018-02-06 11:26:31 +0530697 { 4, I2C5_BASE_ADDR, I2C_QUIRK_FLAG },
698 { 5, I2C6_BASE_ADDR, I2C_QUIRK_FLAG },
699 { 6, I2C7_BASE_ADDR, I2C_QUIRK_FLAG },
700 { 7, I2C8_BASE_ADDR, I2C_QUIRK_FLAG },
Troy Kiskyae447602012-07-19 08:18:18 +0000701#else
Heiko Schocher24ebcf22015-05-18 10:58:12 +0200702 { 0, I2C1_BASE_ADDR, 0 },
703 { 1, I2C2_BASE_ADDR, 0 },
704 { 2, I2C3_BASE_ADDR, 0 },
705 { 3, I2C4_BASE_ADDR, 0 },
Sriram Dasha64aa192018-02-06 11:26:31 +0530706 { 4, I2C5_BASE_ADDR, 0 },
707 { 5, I2C6_BASE_ADDR, 0 },
708 { 6, I2C7_BASE_ADDR, 0 },
709 { 7, I2C8_BASE_ADDR, 0 },
Troy Kiskyae447602012-07-19 08:18:18 +0000710#endif
trema49f40a2013-09-21 18:13:35 +0200711};
712
Peng Fan8262cb12015-05-15 07:29:12 +0800713struct mxc_i2c_bus *i2c_get_base(struct i2c_adapter *adap)
Troy Kiskya23ab222012-07-19 08:18:19 +0000714{
Peng Fan8262cb12015-05-15 07:29:12 +0800715 return &mxc_i2c_buses[adap->hwadapnr];
Troy Kiskya23ab222012-07-19 08:18:19 +0000716}
Troy Kiskyb6f98262012-07-19 08:18:20 +0000717
trema49f40a2013-09-21 18:13:35 +0200718static int mxc_i2c_read(struct i2c_adapter *adap, uint8_t chip,
719 uint addr, int alen, uint8_t *buffer,
720 int len)
Troy Kiskyae447602012-07-19 08:18:18 +0000721{
trema49f40a2013-09-21 18:13:35 +0200722 return bus_i2c_read(i2c_get_base(adap), chip, addr, alen, buffer, len);
Troy Kiskyae447602012-07-19 08:18:18 +0000723}
724
trema49f40a2013-09-21 18:13:35 +0200725static int mxc_i2c_write(struct i2c_adapter *adap, uint8_t chip,
726 uint addr, int alen, uint8_t *buffer,
727 int len)
Troy Kiskyae447602012-07-19 08:18:18 +0000728{
trema49f40a2013-09-21 18:13:35 +0200729 return bus_i2c_write(i2c_get_base(adap), chip, addr, alen, buffer, len);
Troy Kiskyae447602012-07-19 08:18:18 +0000730}
731
732/*
Troy Kisky321a42b2012-07-19 08:18:08 +0000733 * Test if a chip at a given address responds (probe the chip)
734 */
trema49f40a2013-09-21 18:13:35 +0200735static int mxc_i2c_probe(struct i2c_adapter *adap, uint8_t chip)
Troy Kisky321a42b2012-07-19 08:18:08 +0000736{
trema49f40a2013-09-21 18:13:35 +0200737 return bus_i2c_write(i2c_get_base(adap), chip, 0, 0, NULL, 0);
Troy Kiskyae447602012-07-19 08:18:18 +0000738}
Peng Fan8262cb12015-05-15 07:29:12 +0800739
Peng Fan8262cb12015-05-15 07:29:12 +0800740void bus_i2c_init(int index, int speed, int unused,
741 int (*idle_bus_fn)(void *p), void *idle_bus_data)
Troy Kiskyae447602012-07-19 08:18:18 +0000742{
Peng Fan8262cb12015-05-15 07:29:12 +0800743 int ret;
744
745 if (index >= ARRAY_SIZE(mxc_i2c_buses)) {
746 debug("Error i2c index\n");
Troy Kiskyae447602012-07-19 08:18:18 +0000747 return;
Peng Fan8262cb12015-05-15 07:29:12 +0800748 }
749
Gong Qianyufd999082015-12-18 17:38:01 +0800750 /*
751 * Warning: Be careful to allow the assignment to a static
752 * variable here. This function could be called while U-Boot is
753 * still running in flash memory. So such assignment is equal
754 * to write data to flash without erasing.
755 */
756 if (idle_bus_fn)
757 mxc_i2c_buses[index].idle_bus_fn = idle_bus_fn;
758 if (idle_bus_data)
759 mxc_i2c_buses[index].idle_bus_data = idle_bus_data;
Peng Fan8262cb12015-05-15 07:29:12 +0800760
761 ret = enable_i2c_clk(1, index);
762 if (ret < 0) {
763 debug("I2C-%d clk fail to enable.\n", index);
764 return;
Troy Kiskyae447602012-07-19 08:18:18 +0000765 }
Peng Fan8262cb12015-05-15 07:29:12 +0800766
767 bus_i2c_set_bus_speed(&mxc_i2c_buses[index], speed);
Troy Kiskyae447602012-07-19 08:18:18 +0000768}
769
770/*
Yuan Yaod40c8852016-06-08 18:24:51 +0800771 * Early init I2C for prepare read the clk through I2C.
772 */
773void i2c_early_init_f(void)
774{
775 ulong base = mxc_i2c_buses[I2C_EARLY_INIT_INDEX].base;
776 bool quirk = mxc_i2c_buses[I2C_EARLY_INIT_INDEX].driver_data
777 & I2C_QUIRK_FLAG ? true : false;
778 int reg_shift = quirk ? VF610_I2C_REGSHIFT : IMX_I2C_REGSHIFT;
779
780 /* Set I2C divider value */
781 writeb(I2C_IFDR_DIV_CONSERVATIVE, base + (IFDR << reg_shift));
782 /* Reset module */
783 writeb(I2CR_IDIS, base + (I2CR << reg_shift));
784 writeb(0, base + (I2SR << reg_shift));
785 /* Enable I2C */
786 writeb(I2CR_IEN, base + (I2CR << reg_shift));
787}
788
789/*
Troy Kiskyae447602012-07-19 08:18:18 +0000790 * Init I2C Bus
791 */
trema49f40a2013-09-21 18:13:35 +0200792static void mxc_i2c_init(struct i2c_adapter *adap, int speed, int slaveaddr)
Troy Kiskyae447602012-07-19 08:18:18 +0000793{
Peng Fan8262cb12015-05-15 07:29:12 +0800794 bus_i2c_init(adap->hwadapnr, speed, slaveaddr, NULL, NULL);
Troy Kiskyae447602012-07-19 08:18:18 +0000795}
796
797/*
798 * Set I2C Speed
799 */
Peng Fan8262cb12015-05-15 07:29:12 +0800800static u32 mxc_i2c_set_bus_speed(struct i2c_adapter *adap, uint speed)
Troy Kiskyae447602012-07-19 08:18:18 +0000801{
trema49f40a2013-09-21 18:13:35 +0200802 return bus_i2c_set_bus_speed(i2c_get_base(adap), speed);
Troy Kiskyae447602012-07-19 08:18:18 +0000803}
804
805/*
trema49f40a2013-09-21 18:13:35 +0200806 * Register mxc i2c adapters
Troy Kiskyae447602012-07-19 08:18:18 +0000807 */
Albert ARIBAUD \\(3ADEV\\)eb943872015-09-21 22:43:38 +0200808#ifdef CONFIG_SYS_I2C_MXC_I2C1
trema49f40a2013-09-21 18:13:35 +0200809U_BOOT_I2C_ADAP_COMPLETE(mxc0, mxc_i2c_init, mxc_i2c_probe,
810 mxc_i2c_read, mxc_i2c_write,
811 mxc_i2c_set_bus_speed,
812 CONFIG_SYS_MXC_I2C1_SPEED,
813 CONFIG_SYS_MXC_I2C1_SLAVE, 0)
Albert ARIBAUD \\(3ADEV\\)eb943872015-09-21 22:43:38 +0200814#endif
815
816#ifdef CONFIG_SYS_I2C_MXC_I2C2
trema49f40a2013-09-21 18:13:35 +0200817U_BOOT_I2C_ADAP_COMPLETE(mxc1, mxc_i2c_init, mxc_i2c_probe,
818 mxc_i2c_read, mxc_i2c_write,
819 mxc_i2c_set_bus_speed,
820 CONFIG_SYS_MXC_I2C2_SPEED,
821 CONFIG_SYS_MXC_I2C2_SLAVE, 1)
Albert ARIBAUD \\(3ADEV\\)eb943872015-09-21 22:43:38 +0200822#endif
823
York Sunf1a52162015-03-20 10:20:40 -0700824#ifdef CONFIG_SYS_I2C_MXC_I2C3
trema49f40a2013-09-21 18:13:35 +0200825U_BOOT_I2C_ADAP_COMPLETE(mxc2, mxc_i2c_init, mxc_i2c_probe,
826 mxc_i2c_read, mxc_i2c_write,
827 mxc_i2c_set_bus_speed,
828 CONFIG_SYS_MXC_I2C3_SPEED,
829 CONFIG_SYS_MXC_I2C3_SLAVE, 2)
830#endif
Peng Fan8262cb12015-05-15 07:29:12 +0800831
York Sunf1a52162015-03-20 10:20:40 -0700832#ifdef CONFIG_SYS_I2C_MXC_I2C4
833U_BOOT_I2C_ADAP_COMPLETE(mxc3, mxc_i2c_init, mxc_i2c_probe,
834 mxc_i2c_read, mxc_i2c_write,
835 mxc_i2c_set_bus_speed,
836 CONFIG_SYS_MXC_I2C4_SPEED,
837 CONFIG_SYS_MXC_I2C4_SLAVE, 3)
838#endif
Peng Fan8262cb12015-05-15 07:29:12 +0800839
Sriram Dasha64aa192018-02-06 11:26:31 +0530840#ifdef CONFIG_SYS_I2C_MXC_I2C5
841U_BOOT_I2C_ADAP_COMPLETE(mxc4, mxc_i2c_init, mxc_i2c_probe,
842 mxc_i2c_read, mxc_i2c_write,
843 mxc_i2c_set_bus_speed,
844 CONFIG_SYS_MXC_I2C5_SPEED,
845 CONFIG_SYS_MXC_I2C5_SLAVE, 4)
846#endif
847
848#ifdef CONFIG_SYS_I2C_MXC_I2C6
849U_BOOT_I2C_ADAP_COMPLETE(mxc5, mxc_i2c_init, mxc_i2c_probe,
850 mxc_i2c_read, mxc_i2c_write,
851 mxc_i2c_set_bus_speed,
852 CONFIG_SYS_MXC_I2C6_SPEED,
853 CONFIG_SYS_MXC_I2C6_SLAVE, 5)
854#endif
855
856#ifdef CONFIG_SYS_I2C_MXC_I2C7
857U_BOOT_I2C_ADAP_COMPLETE(mxc6, mxc_i2c_init, mxc_i2c_probe,
858 mxc_i2c_read, mxc_i2c_write,
859 mxc_i2c_set_bus_speed,
860 CONFIG_SYS_MXC_I2C7_SPEED,
861 CONFIG_SYS_MXC_I2C7_SLAVE, 6)
862#endif
863
864#ifdef CONFIG_SYS_I2C_MXC_I2C8
865U_BOOT_I2C_ADAP_COMPLETE(mxc7, mxc_i2c_init, mxc_i2c_probe,
866 mxc_i2c_read, mxc_i2c_write,
867 mxc_i2c_set_bus_speed,
868 CONFIG_SYS_MXC_I2C8_SPEED,
869 CONFIG_SYS_MXC_I2C8_SLAVE, 7)
870#endif
871
Peng Fan8262cb12015-05-15 07:29:12 +0800872#else
873
874static int mxc_i2c_set_bus_speed(struct udevice *bus, unsigned int speed)
875{
876 struct mxc_i2c_bus *i2c_bus = dev_get_priv(bus);
877
878 return bus_i2c_set_bus_speed(i2c_bus, speed);
879}
880
881static int mxc_i2c_probe(struct udevice *bus)
882{
883 struct mxc_i2c_bus *i2c_bus = dev_get_priv(bus);
Peng Fan4f1a5812016-03-11 16:47:50 +0800884 const void *fdt = gd->fdt_blob;
Simon Glassdd79d6e2017-01-17 16:52:55 -0700885 int node = dev_of_offset(bus);
Peng Fan8262cb12015-05-15 07:29:12 +0800886 fdt_addr_t addr;
Peng Fan4f1a5812016-03-11 16:47:50 +0800887 int ret, ret2;
Peng Fan8262cb12015-05-15 07:29:12 +0800888
889 i2c_bus->driver_data = dev_get_driver_data(bus);
890
Simon Glassba1dea42017-05-17 17:18:05 -0600891 addr = devfdt_get_addr(bus);
Peng Fan8262cb12015-05-15 07:29:12 +0800892 if (addr == FDT_ADDR_T_NONE)
Simon Glassf44b4bf2017-09-17 16:54:53 -0600893 return -EINVAL;
Peng Fan8262cb12015-05-15 07:29:12 +0800894
895 i2c_bus->base = addr;
896 i2c_bus->index = bus->seq;
Peng Fan4f1a5812016-03-11 16:47:50 +0800897 i2c_bus->bus = bus;
Peng Fan8262cb12015-05-15 07:29:12 +0800898
899 /* Enable clk */
Peng Fan87ea5622019-08-08 01:43:30 +0000900#if CONFIG_IS_ENABLED(CLK)
901 ret = clk_get_by_index(bus, 0, &i2c_bus->per_clk);
902 if (ret) {
903 printf("Failed to get i2c clk\n");
904 return ret;
905 }
906 ret = clk_enable(&i2c_bus->per_clk);
907 if (ret) {
908 printf("Failed to enable i2c clk\n");
909 return ret;
910 }
911#else
Peng Fan8262cb12015-05-15 07:29:12 +0800912 ret = enable_i2c_clk(1, bus->seq);
913 if (ret < 0)
914 return ret;
Peng Fan87ea5622019-08-08 01:43:30 +0000915#endif
Peng Fan8262cb12015-05-15 07:29:12 +0800916
Peng Fan4f1a5812016-03-11 16:47:50 +0800917 /*
918 * See Documentation/devicetree/bindings/i2c/i2c-imx.txt
919 * Use gpio to force bus idle when necessary.
920 */
Simon Glassb0ea7402016-10-02 17:59:28 -0600921 ret = fdt_stringlist_search(fdt, node, "pinctrl-names", "gpio");
Peng Fan4f1a5812016-03-11 16:47:50 +0800922 if (ret < 0) {
Jagan Tekicac30062016-12-06 00:00:59 +0100923 debug("i2c bus %d at 0x%2lx, no gpio pinctrl state.\n", bus->seq, i2c_bus->base);
Peng Fan4f1a5812016-03-11 16:47:50 +0800924 } else {
Simon Glass1d9af1f2017-05-30 21:47:09 -0600925 ret = gpio_request_by_name_nodev(offset_to_ofnode(node),
926 "scl-gpios", 0, &i2c_bus->scl_gpio,
927 GPIOD_IS_OUT);
928 ret2 = gpio_request_by_name_nodev(offset_to_ofnode(node),
929 "sda-gpios", 0, &i2c_bus->sda_gpio,
930 GPIOD_IS_OUT);
Peng Fan08eaa832017-12-29 15:06:08 +0800931 if (!dm_gpio_is_valid(&i2c_bus->sda_gpio) ||
932 !dm_gpio_is_valid(&i2c_bus->scl_gpio) ||
933 ret || ret2) {
Peng Fan4f1a5812016-03-11 16:47:50 +0800934 dev_err(dev, "i2c bus %d at %lu, fail to request scl/sda gpio\n", bus->seq, i2c_bus->base);
Simon Glassf44b4bf2017-09-17 16:54:53 -0600935 return -EINVAL;
Peng Fan4f1a5812016-03-11 16:47:50 +0800936 }
937 }
938
Peng Fan8262cb12015-05-15 07:29:12 +0800939 /*
940 * Pinmux settings are in board file now, until pinmux is supported,
941 * we can set pinmux here in probe function.
942 */
943
944 debug("i2c : controller bus %d at %lu , speed %d: ",
945 bus->seq, i2c_bus->base,
946 i2c_bus->speed);
947
948 return 0;
949}
950
Trent Piepho15410ba2019-04-30 16:08:18 +0000951/* Sends: S Addr Wr [A|NA] P */
Peng Fan8262cb12015-05-15 07:29:12 +0800952static int mxc_i2c_probe_chip(struct udevice *bus, u32 chip_addr,
953 u32 chip_flags)
954{
955 int ret;
956 struct mxc_i2c_bus *i2c_bus = dev_get_priv(bus);
957
958 ret = i2c_init_transfer(i2c_bus, chip_addr, 0, 0);
959 if (ret < 0) {
960 debug("%s failed, ret = %d\n", __func__, ret);
961 return ret;
962 }
963
964 i2c_imx_stop(i2c_bus);
965
966 return 0;
967}
968
969static int mxc_i2c_xfer(struct udevice *bus, struct i2c_msg *msg, int nmsgs)
970{
971 struct mxc_i2c_bus *i2c_bus = dev_get_priv(bus);
972 int ret = 0;
973 ulong base = i2c_bus->base;
974 int reg_shift = i2c_bus->driver_data & I2C_QUIRK_FLAG ?
975 VF610_I2C_REGSHIFT : IMX_I2C_REGSHIFT;
Trent Piepho9c896a92019-04-30 16:08:19 +0000976 int read_mode;
Peng Fan8262cb12015-05-15 07:29:12 +0800977
Trent Piepho9c896a92019-04-30 16:08:19 +0000978 /* Here address len is set to -1 to not send any address at first.
979 * Otherwise i2c_init_transfer will send the chip address with write
980 * mode set. This is wrong if the 1st message is read.
Peng Fan8262cb12015-05-15 07:29:12 +0800981 */
Trent Piepho9c896a92019-04-30 16:08:19 +0000982 ret = i2c_init_transfer(i2c_bus, msg->addr, 0, -1);
Peng Fan8262cb12015-05-15 07:29:12 +0800983 if (ret < 0) {
984 debug("i2c_init_transfer error: %d\n", ret);
985 return ret;
986 }
987
Trent Piepho9c896a92019-04-30 16:08:19 +0000988 read_mode = -1; /* So it's always different on the first message */
Peng Fan8262cb12015-05-15 07:29:12 +0800989 for (; nmsgs > 0; nmsgs--, msg++) {
Trent Piepho9c896a92019-04-30 16:08:19 +0000990 const int msg_is_read = !!(msg->flags & I2C_M_RD);
991
992 debug("i2c_xfer: chip=0x%x, len=0x%x, dir=%c\n", msg->addr,
993 msg->len, msg_is_read ? 'R' : 'W');
994
995 if (msg_is_read != read_mode) {
996 /* Send repeated start if not 1st message */
997 if (read_mode != -1) {
998 debug("i2c_xfer: [RSTART]\n");
Peng Fan8262cb12015-05-15 07:29:12 +0800999 ret = readb(base + (I2CR << reg_shift));
1000 ret |= I2CR_RSTA;
1001 writeb(ret, base + (I2CR << reg_shift));
Peng Fan8262cb12015-05-15 07:29:12 +08001002 }
Trent Piepho9c896a92019-04-30 16:08:19 +00001003 debug("i2c_xfer: [ADDR %02x | %c]\n", msg->addr,
1004 msg_is_read ? 'R' : 'W');
1005 ret = tx_byte(i2c_bus, (msg->addr << 1) | msg_is_read);
1006 if (ret < 0) {
1007 debug("i2c_xfer: [STOP]\n");
1008 i2c_imx_stop(i2c_bus);
1009 break;
1010 }
1011 read_mode = msg_is_read;
Peng Fan8262cb12015-05-15 07:29:12 +08001012 }
Trent Piepho9c896a92019-04-30 16:08:19 +00001013
1014 if (msg->flags & I2C_M_RD)
1015 ret = i2c_read_data(i2c_bus, msg->addr, msg->buf,
1016 msg->len, nmsgs == 1 ||
1017 (msg->flags & I2C_M_STOP));
1018 else
1019 ret = i2c_write_data(i2c_bus, msg->addr, msg->buf,
1020 msg->len);
1021
1022 if (ret < 0)
1023 break;
Peng Fan8262cb12015-05-15 07:29:12 +08001024 }
1025
1026 if (ret)
1027 debug("i2c_write: error sending\n");
1028
1029 i2c_imx_stop(i2c_bus);
1030
1031 return ret;
1032}
1033
1034static const struct dm_i2c_ops mxc_i2c_ops = {
1035 .xfer = mxc_i2c_xfer,
1036 .probe_chip = mxc_i2c_probe_chip,
1037 .set_bus_speed = mxc_i2c_set_bus_speed,
1038};
1039
1040static const struct udevice_id mxc_i2c_ids[] = {
1041 { .compatible = "fsl,imx21-i2c", },
1042 { .compatible = "fsl,vf610-i2c", .data = I2C_QUIRK_FLAG, },
1043 {}
1044};
1045
1046U_BOOT_DRIVER(i2c_mxc) = {
1047 .name = "i2c_mxc",
1048 .id = UCLASS_I2C,
1049 .of_match = mxc_i2c_ids,
1050 .probe = mxc_i2c_probe,
1051 .priv_auto_alloc_size = sizeof(struct mxc_i2c_bus),
1052 .ops = &mxc_i2c_ops,
Biwen Li41a5abc2019-12-31 15:33:39 +08001053 .flags = DM_FLAG_PRE_RELOC,
Peng Fan8262cb12015-05-15 07:29:12 +08001054};
1055#endif