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