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