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