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