Sascha Hauer | cdace06 | 2008-03-26 20:40:49 +0100 | [diff] [blame] | 1 | /* |
Marek Vasut | db84140 | 2011-09-22 09:22:12 +0000 | [diff] [blame] | 2 | * i2c driver for Freescale i.MX series |
Sascha Hauer | cdace06 | 2008-03-26 20:40:49 +0100 | [diff] [blame] | 3 | * |
| 4 | * (c) 2007 Pengutronix, Sascha Hauer <s.hauer@pengutronix.de> |
Marek Vasut | db84140 | 2011-09-22 09:22:12 +0000 | [diff] [blame] | 5 | * (c) 2011 Marek Vasut <marek.vasut@gmail.com> |
| 6 | * |
| 7 | * Based on i2c-imx.c from linux kernel: |
| 8 | * Copyright (C) 2005 Torsten Koschorrek <koschorrek at synertronixx.de> |
| 9 | * Copyright (C) 2005 Matthias Blaschke <blaschke at synertronixx.de> |
| 10 | * Copyright (C) 2007 RightHand Technologies, Inc. |
| 11 | * Copyright (C) 2008 Darius Augulis <darius.augulis at teltonika.lt> |
| 12 | * |
Sascha Hauer | cdace06 | 2008-03-26 20:40:49 +0100 | [diff] [blame] | 13 | * |
Wolfgang Denk | 1a45966 | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 14 | * SPDX-License-Identifier: GPL-2.0+ |
Sascha Hauer | cdace06 | 2008-03-26 20:40:49 +0100 | [diff] [blame] | 15 | */ |
| 16 | |
| 17 | #include <common.h> |
Liu Hui-R64343 | 127cec1 | 2011-01-03 22:27:39 +0000 | [diff] [blame] | 18 | #include <asm/arch/clock.h> |
Stefano Babic | 8627111 | 2011-03-14 15:43:56 +0100 | [diff] [blame] | 19 | #include <asm/arch/imx-regs.h> |
Troy Kisky | cea60b0 | 2012-07-19 08:18:04 +0000 | [diff] [blame] | 20 | #include <asm/errno.h> |
Peng Fan | 71204e9 | 2015-05-15 07:29:12 +0800 | [diff] [blame] | 21 | #include <asm/imx-common/mxc_i2c.h> |
Troy Kisky | 24cd738 | 2012-07-19 08:18:03 +0000 | [diff] [blame] | 22 | #include <asm/io.h> |
Marek Vasut | bf0783d | 2011-10-26 00:05:44 +0000 | [diff] [blame] | 23 | #include <i2c.h> |
Troy Kisky | 7aa57a0 | 2012-07-19 08:18:09 +0000 | [diff] [blame] | 24 | #include <watchdog.h> |
Peng Fan | 71204e9 | 2015-05-15 07:29:12 +0800 | [diff] [blame] | 25 | #include <dm.h> |
Peng Fan | e1bed80 | 2016-03-11 16:47:50 +0800 | [diff] [blame] | 26 | #include <dm/pinctrl.h> |
Peng Fan | 71204e9 | 2015-05-15 07:29:12 +0800 | [diff] [blame] | 27 | #include <fdtdec.h> |
Sascha Hauer | cdace06 | 2008-03-26 20:40:49 +0100 | [diff] [blame] | 28 | |
York Sun | dec1861 | 2014-02-10 14:02:52 -0800 | [diff] [blame] | 29 | DECLARE_GLOBAL_DATA_PTR; |
| 30 | |
Peng Fan | 71204e9 | 2015-05-15 07:29:12 +0800 | [diff] [blame] | 31 | #define I2C_QUIRK_FLAG (1 << 0) |
| 32 | |
| 33 | #define IMX_I2C_REGSHIFT 2 |
| 34 | #define VF610_I2C_REGSHIFT 0 |
| 35 | /* Register index */ |
| 36 | #define IADR 0 |
| 37 | #define IFDR 1 |
| 38 | #define I2CR 2 |
| 39 | #define I2SR 3 |
| 40 | #define I2DR 4 |
Sascha Hauer | cdace06 | 2008-03-26 20:40:49 +0100 | [diff] [blame] | 41 | |
Sascha Hauer | cdace06 | 2008-03-26 20:40:49 +0100 | [diff] [blame] | 42 | #define I2CR_IIEN (1 << 6) |
| 43 | #define I2CR_MSTA (1 << 5) |
| 44 | #define I2CR_MTX (1 << 4) |
| 45 | #define I2CR_TX_NO_AK (1 << 3) |
| 46 | #define I2CR_RSTA (1 << 2) |
| 47 | |
| 48 | #define I2SR_ICF (1 << 7) |
| 49 | #define I2SR_IBB (1 << 5) |
Troy Kisky | d5383a6 | 2012-07-19 08:18:15 +0000 | [diff] [blame] | 50 | #define I2SR_IAL (1 << 4) |
Sascha Hauer | cdace06 | 2008-03-26 20:40:49 +0100 | [diff] [blame] | 51 | #define I2SR_IIF (1 << 1) |
| 52 | #define I2SR_RX_NO_AK (1 << 0) |
| 53 | |
Alison Wang | 30ea41a | 2013-06-17 15:30:39 +0800 | [diff] [blame] | 54 | #ifdef I2C_QUIRK_REG |
| 55 | #define I2CR_IEN (0 << 7) |
| 56 | #define I2CR_IDIS (1 << 7) |
| 57 | #define I2SR_IIF_CLEAR (1 << 1) |
| 58 | #else |
| 59 | #define I2CR_IEN (1 << 7) |
| 60 | #define I2CR_IDIS (0 << 7) |
| 61 | #define I2SR_IIF_CLEAR (0 << 1) |
| 62 | #endif |
| 63 | |
Troy Kisky | e4ff525 | 2012-07-19 08:18:18 +0000 | [diff] [blame] | 64 | #if defined(CONFIG_HARD_I2C) && !defined(CONFIG_SYS_I2C_BASE) |
Troy Kisky | de6f604 | 2012-04-24 17:33:25 +0000 | [diff] [blame] | 65 | #error "define CONFIG_SYS_I2C_BASE to use the mxc_i2c driver" |
Sascha Hauer | cdace06 | 2008-03-26 20:40:49 +0100 | [diff] [blame] | 66 | #endif |
| 67 | |
Alison Wang | 30ea41a | 2013-06-17 15:30:39 +0800 | [diff] [blame] | 68 | #ifdef I2C_QUIRK_REG |
| 69 | static u16 i2c_clk_div[60][2] = { |
| 70 | { 20, 0x00 }, { 22, 0x01 }, { 24, 0x02 }, { 26, 0x03 }, |
| 71 | { 28, 0x04 }, { 30, 0x05 }, { 32, 0x09 }, { 34, 0x06 }, |
| 72 | { 36, 0x0A }, { 40, 0x07 }, { 44, 0x0C }, { 48, 0x0D }, |
| 73 | { 52, 0x43 }, { 56, 0x0E }, { 60, 0x45 }, { 64, 0x12 }, |
| 74 | { 68, 0x0F }, { 72, 0x13 }, { 80, 0x14 }, { 88, 0x15 }, |
| 75 | { 96, 0x19 }, { 104, 0x16 }, { 112, 0x1A }, { 128, 0x17 }, |
| 76 | { 136, 0x4F }, { 144, 0x1C }, { 160, 0x1D }, { 176, 0x55 }, |
| 77 | { 192, 0x1E }, { 208, 0x56 }, { 224, 0x22 }, { 228, 0x24 }, |
| 78 | { 240, 0x1F }, { 256, 0x23 }, { 288, 0x5C }, { 320, 0x25 }, |
| 79 | { 384, 0x26 }, { 448, 0x2A }, { 480, 0x27 }, { 512, 0x2B }, |
| 80 | { 576, 0x2C }, { 640, 0x2D }, { 768, 0x31 }, { 896, 0x32 }, |
| 81 | { 960, 0x2F }, { 1024, 0x33 }, { 1152, 0x34 }, { 1280, 0x35 }, |
| 82 | { 1536, 0x36 }, { 1792, 0x3A }, { 1920, 0x37 }, { 2048, 0x3B }, |
| 83 | { 2304, 0x3C }, { 2560, 0x3D }, { 3072, 0x3E }, { 3584, 0x7A }, |
| 84 | { 3840, 0x3F }, { 4096, 0x7B }, { 5120, 0x7D }, { 6144, 0x7E }, |
| 85 | }; |
| 86 | #else |
Marek Vasut | db84140 | 2011-09-22 09:22:12 +0000 | [diff] [blame] | 87 | static u16 i2c_clk_div[50][2] = { |
| 88 | { 22, 0x20 }, { 24, 0x21 }, { 26, 0x22 }, { 28, 0x23 }, |
| 89 | { 30, 0x00 }, { 32, 0x24 }, { 36, 0x25 }, { 40, 0x26 }, |
| 90 | { 42, 0x03 }, { 44, 0x27 }, { 48, 0x28 }, { 52, 0x05 }, |
| 91 | { 56, 0x29 }, { 60, 0x06 }, { 64, 0x2A }, { 72, 0x2B }, |
| 92 | { 80, 0x2C }, { 88, 0x09 }, { 96, 0x2D }, { 104, 0x0A }, |
| 93 | { 112, 0x2E }, { 128, 0x2F }, { 144, 0x0C }, { 160, 0x30 }, |
| 94 | { 192, 0x31 }, { 224, 0x32 }, { 240, 0x0F }, { 256, 0x33 }, |
| 95 | { 288, 0x10 }, { 320, 0x34 }, { 384, 0x35 }, { 448, 0x36 }, |
| 96 | { 480, 0x13 }, { 512, 0x37 }, { 576, 0x14 }, { 640, 0x38 }, |
| 97 | { 768, 0x39 }, { 896, 0x3A }, { 960, 0x17 }, { 1024, 0x3B }, |
| 98 | { 1152, 0x18 }, { 1280, 0x3C }, { 1536, 0x3D }, { 1792, 0x3E }, |
| 99 | { 1920, 0x1B }, { 2048, 0x3F }, { 2304, 0x1C }, { 2560, 0x1D }, |
| 100 | { 3072, 0x1E }, { 3840, 0x1F } |
| 101 | }; |
Alison Wang | 30ea41a | 2013-06-17 15:30:39 +0800 | [diff] [blame] | 102 | #endif |
Sascha Hauer | cdace06 | 2008-03-26 20:40:49 +0100 | [diff] [blame] | 103 | |
trem | fac9640 | 2013-09-21 18:13:35 +0200 | [diff] [blame] | 104 | #ifndef CONFIG_SYS_MXC_I2C1_SPEED |
| 105 | #define CONFIG_SYS_MXC_I2C1_SPEED 100000 |
| 106 | #endif |
| 107 | #ifndef CONFIG_SYS_MXC_I2C2_SPEED |
| 108 | #define CONFIG_SYS_MXC_I2C2_SPEED 100000 |
| 109 | #endif |
| 110 | #ifndef CONFIG_SYS_MXC_I2C3_SPEED |
| 111 | #define CONFIG_SYS_MXC_I2C3_SPEED 100000 |
| 112 | #endif |
York Sun | f8cb101 | 2015-03-20 10:20:40 -0700 | [diff] [blame] | 113 | #ifndef CONFIG_SYS_MXC_I2C4_SPEED |
| 114 | #define CONFIG_SYS_MXC_I2C4_SPEED 100000 |
| 115 | #endif |
trem | fac9640 | 2013-09-21 18:13:35 +0200 | [diff] [blame] | 116 | |
| 117 | #ifndef CONFIG_SYS_MXC_I2C1_SLAVE |
| 118 | #define CONFIG_SYS_MXC_I2C1_SLAVE 0 |
| 119 | #endif |
| 120 | #ifndef CONFIG_SYS_MXC_I2C2_SLAVE |
| 121 | #define CONFIG_SYS_MXC_I2C2_SLAVE 0 |
| 122 | #endif |
| 123 | #ifndef CONFIG_SYS_MXC_I2C3_SLAVE |
| 124 | #define CONFIG_SYS_MXC_I2C3_SLAVE 0 |
| 125 | #endif |
York Sun | f8cb101 | 2015-03-20 10:20:40 -0700 | [diff] [blame] | 126 | #ifndef CONFIG_SYS_MXC_I2C4_SLAVE |
| 127 | #define CONFIG_SYS_MXC_I2C4_SLAVE 0 |
| 128 | #endif |
trem | fac9640 | 2013-09-21 18:13:35 +0200 | [diff] [blame] | 129 | |
Marek Vasut | db84140 | 2011-09-22 09:22:12 +0000 | [diff] [blame] | 130 | /* |
| 131 | * Calculate and set proper clock divider |
| 132 | */ |
Peng Fan | 71204e9 | 2015-05-15 07:29:12 +0800 | [diff] [blame] | 133 | static uint8_t i2c_imx_get_clk(struct mxc_i2c_bus *i2c_bus, unsigned int rate) |
Stefano Babic | 1d549ad | 2011-01-20 07:50:44 +0000 | [diff] [blame] | 134 | { |
Marek Vasut | db84140 | 2011-09-22 09:22:12 +0000 | [diff] [blame] | 135 | unsigned int i2c_clk_rate; |
| 136 | unsigned int div; |
Marek Vasut | bf0783d | 2011-10-26 00:05:44 +0000 | [diff] [blame] | 137 | u8 clk_div; |
Sascha Hauer | cdace06 | 2008-03-26 20:40:49 +0100 | [diff] [blame] | 138 | |
Liu Hui-R64343 | 127cec1 | 2011-01-03 22:27:39 +0000 | [diff] [blame] | 139 | #if defined(CONFIG_MX31) |
Stefano Babic | 1d549ad | 2011-01-20 07:50:44 +0000 | [diff] [blame] | 140 | struct clock_control_regs *sc_regs = |
| 141 | (struct clock_control_regs *)CCM_BASE; |
Marek Vasut | db84140 | 2011-09-22 09:22:12 +0000 | [diff] [blame] | 142 | |
Guennadi Liakhovetski | e7de18a | 2009-02-13 09:23:36 +0100 | [diff] [blame] | 143 | /* start the required I2C clock */ |
Troy Kisky | de6f604 | 2012-04-24 17:33:25 +0000 | [diff] [blame] | 144 | writel(readl(&sc_regs->cgr0) | (3 << CONFIG_SYS_I2C_CLK_OFFSET), |
Stefano Babic | 1d549ad | 2011-01-20 07:50:44 +0000 | [diff] [blame] | 145 | &sc_regs->cgr0); |
Liu Hui-R64343 | 127cec1 | 2011-01-03 22:27:39 +0000 | [diff] [blame] | 146 | #endif |
Guennadi Liakhovetski | e7de18a | 2009-02-13 09:23:36 +0100 | [diff] [blame] | 147 | |
Marek Vasut | db84140 | 2011-09-22 09:22:12 +0000 | [diff] [blame] | 148 | /* Divider value calculation */ |
Matthias Weisser | e7bed5c | 2012-09-24 02:46:53 +0000 | [diff] [blame] | 149 | i2c_clk_rate = mxc_get_clock(MXC_I2C_CLK); |
Marek Vasut | db84140 | 2011-09-22 09:22:12 +0000 | [diff] [blame] | 150 | div = (i2c_clk_rate + rate - 1) / rate; |
| 151 | if (div < i2c_clk_div[0][0]) |
Marek Vasut | b567b8f | 2011-09-27 06:34:11 +0000 | [diff] [blame] | 152 | clk_div = 0; |
Marek Vasut | db84140 | 2011-09-22 09:22:12 +0000 | [diff] [blame] | 153 | else if (div > i2c_clk_div[ARRAY_SIZE(i2c_clk_div) - 1][0]) |
Marek Vasut | b567b8f | 2011-09-27 06:34:11 +0000 | [diff] [blame] | 154 | clk_div = ARRAY_SIZE(i2c_clk_div) - 1; |
Marek Vasut | db84140 | 2011-09-22 09:22:12 +0000 | [diff] [blame] | 155 | else |
Marek Vasut | b567b8f | 2011-09-27 06:34:11 +0000 | [diff] [blame] | 156 | for (clk_div = 0; i2c_clk_div[clk_div][0] < div; clk_div++) |
Marek Vasut | db84140 | 2011-09-22 09:22:12 +0000 | [diff] [blame] | 157 | ; |
Sascha Hauer | cdace06 | 2008-03-26 20:40:49 +0100 | [diff] [blame] | 158 | |
Marek Vasut | db84140 | 2011-09-22 09:22:12 +0000 | [diff] [blame] | 159 | /* Store divider value */ |
Marek Vasut | bf0783d | 2011-10-26 00:05:44 +0000 | [diff] [blame] | 160 | return clk_div; |
Marek Vasut | db84140 | 2011-09-22 09:22:12 +0000 | [diff] [blame] | 161 | } |
Sascha Hauer | cdace06 | 2008-03-26 20:40:49 +0100 | [diff] [blame] | 162 | |
Marek Vasut | db84140 | 2011-09-22 09:22:12 +0000 | [diff] [blame] | 163 | /* |
Troy Kisky | e4ff525 | 2012-07-19 08:18:18 +0000 | [diff] [blame] | 164 | * Set I2C Bus speed |
Marek Vasut | db84140 | 2011-09-22 09:22:12 +0000 | [diff] [blame] | 165 | */ |
Peng Fan | 71204e9 | 2015-05-15 07:29:12 +0800 | [diff] [blame] | 166 | static int bus_i2c_set_bus_speed(struct mxc_i2c_bus *i2c_bus, int speed) |
Marek Vasut | db84140 | 2011-09-22 09:22:12 +0000 | [diff] [blame] | 167 | { |
Peng Fan | 71204e9 | 2015-05-15 07:29:12 +0800 | [diff] [blame] | 168 | ulong base = i2c_bus->base; |
| 169 | bool quirk = i2c_bus->driver_data & I2C_QUIRK_FLAG ? true : false; |
| 170 | u8 clk_idx = i2c_imx_get_clk(i2c_bus, speed); |
Marek Vasut | bf0783d | 2011-10-26 00:05:44 +0000 | [diff] [blame] | 171 | u8 idx = i2c_clk_div[clk_idx][1]; |
Peng Fan | 71204e9 | 2015-05-15 07:29:12 +0800 | [diff] [blame] | 172 | int reg_shift = quirk ? VF610_I2C_REGSHIFT : IMX_I2C_REGSHIFT; |
Marek Vasut | bf0783d | 2011-10-26 00:05:44 +0000 | [diff] [blame] | 173 | |
Heiko Schocher | e6c8b71 | 2015-05-18 10:58:12 +0200 | [diff] [blame] | 174 | if (!base) |
| 175 | return -ENODEV; |
| 176 | |
Marek Vasut | bf0783d | 2011-10-26 00:05:44 +0000 | [diff] [blame] | 177 | /* Store divider value */ |
Peng Fan | 71204e9 | 2015-05-15 07:29:12 +0800 | [diff] [blame] | 178 | writeb(idx, base + (IFDR << reg_shift)); |
Marek Vasut | bf0783d | 2011-10-26 00:05:44 +0000 | [diff] [blame] | 179 | |
Troy Kisky | 83a1a19 | 2012-07-19 08:18:12 +0000 | [diff] [blame] | 180 | /* Reset module */ |
Peng Fan | 71204e9 | 2015-05-15 07:29:12 +0800 | [diff] [blame] | 181 | writeb(I2CR_IDIS, base + (I2CR << reg_shift)); |
| 182 | writeb(0, base + (I2SR << reg_shift)); |
Marek Vasut | b567b8f | 2011-09-27 06:34:11 +0000 | [diff] [blame] | 183 | return 0; |
| 184 | } |
| 185 | |
Troy Kisky | 7aa57a0 | 2012-07-19 08:18:09 +0000 | [diff] [blame] | 186 | #define ST_BUS_IDLE (0 | (I2SR_IBB << 8)) |
| 187 | #define ST_BUS_BUSY (I2SR_IBB | (I2SR_IBB << 8)) |
| 188 | #define ST_IIF (I2SR_IIF | (I2SR_IIF << 8)) |
| 189 | |
Peng Fan | 71204e9 | 2015-05-15 07:29:12 +0800 | [diff] [blame] | 190 | static int wait_for_sr_state(struct mxc_i2c_bus *i2c_bus, unsigned state) |
Stefano Babic | 8168721 | 2011-01-20 07:51:31 +0000 | [diff] [blame] | 191 | { |
Troy Kisky | 7aa57a0 | 2012-07-19 08:18:09 +0000 | [diff] [blame] | 192 | unsigned sr; |
| 193 | ulong elapsed; |
Peng Fan | 71204e9 | 2015-05-15 07:29:12 +0800 | [diff] [blame] | 194 | bool quirk = i2c_bus->driver_data & I2C_QUIRK_FLAG ? true : false; |
| 195 | int reg_shift = quirk ? VF610_I2C_REGSHIFT : IMX_I2C_REGSHIFT; |
| 196 | ulong base = i2c_bus->base; |
Troy Kisky | 7aa57a0 | 2012-07-19 08:18:09 +0000 | [diff] [blame] | 197 | ulong start_time = get_timer(0); |
| 198 | for (;;) { |
Peng Fan | 71204e9 | 2015-05-15 07:29:12 +0800 | [diff] [blame] | 199 | sr = readb(base + (I2SR << reg_shift)); |
Troy Kisky | d5383a6 | 2012-07-19 08:18:15 +0000 | [diff] [blame] | 200 | if (sr & I2SR_IAL) { |
Peng Fan | 71204e9 | 2015-05-15 07:29:12 +0800 | [diff] [blame] | 201 | if (quirk) |
| 202 | writeb(sr | I2SR_IAL, base + |
| 203 | (I2SR << reg_shift)); |
| 204 | else |
| 205 | writeb(sr & ~I2SR_IAL, base + |
| 206 | (I2SR << reg_shift)); |
Troy Kisky | d5383a6 | 2012-07-19 08:18:15 +0000 | [diff] [blame] | 207 | printf("%s: Arbitration lost sr=%x cr=%x state=%x\n", |
Peng Fan | 71204e9 | 2015-05-15 07:29:12 +0800 | [diff] [blame] | 208 | __func__, sr, readb(base + (I2CR << reg_shift)), |
| 209 | state); |
Troy Kisky | d5383a6 | 2012-07-19 08:18:15 +0000 | [diff] [blame] | 210 | return -ERESTART; |
| 211 | } |
Troy Kisky | 7aa57a0 | 2012-07-19 08:18:09 +0000 | [diff] [blame] | 212 | if ((sr & (state >> 8)) == (unsigned char)state) |
| 213 | return sr; |
| 214 | WATCHDOG_RESET(); |
| 215 | elapsed = get_timer(start_time); |
| 216 | if (elapsed > (CONFIG_SYS_HZ / 10)) /* .1 seconds */ |
| 217 | break; |
Stefano Babic | 8168721 | 2011-01-20 07:51:31 +0000 | [diff] [blame] | 218 | } |
Troy Kisky | 7aa57a0 | 2012-07-19 08:18:09 +0000 | [diff] [blame] | 219 | printf("%s: failed sr=%x cr=%x state=%x\n", __func__, |
Peng Fan | 71204e9 | 2015-05-15 07:29:12 +0800 | [diff] [blame] | 220 | sr, readb(base + (I2CR << reg_shift)), state); |
Troy Kisky | cea60b0 | 2012-07-19 08:18:04 +0000 | [diff] [blame] | 221 | return -ETIMEDOUT; |
Stefano Babic | 8168721 | 2011-01-20 07:51:31 +0000 | [diff] [blame] | 222 | } |
| 223 | |
Peng Fan | 71204e9 | 2015-05-15 07:29:12 +0800 | [diff] [blame] | 224 | static int tx_byte(struct mxc_i2c_bus *i2c_bus, u8 byte) |
Sascha Hauer | cdace06 | 2008-03-26 20:40:49 +0100 | [diff] [blame] | 225 | { |
Troy Kisky | cea60b0 | 2012-07-19 08:18:04 +0000 | [diff] [blame] | 226 | int ret; |
Peng Fan | 71204e9 | 2015-05-15 07:29:12 +0800 | [diff] [blame] | 227 | int reg_shift = i2c_bus->driver_data & I2C_QUIRK_FLAG ? |
| 228 | VF610_I2C_REGSHIFT : IMX_I2C_REGSHIFT; |
| 229 | ulong base = i2c_bus->base; |
Sascha Hauer | cdace06 | 2008-03-26 20:40:49 +0100 | [diff] [blame] | 230 | |
Peng Fan | 71204e9 | 2015-05-15 07:29:12 +0800 | [diff] [blame] | 231 | writeb(I2SR_IIF_CLEAR, base + (I2SR << reg_shift)); |
| 232 | writeb(byte, base + (I2DR << reg_shift)); |
| 233 | |
| 234 | ret = wait_for_sr_state(i2c_bus, ST_IIF); |
Troy Kisky | cea60b0 | 2012-07-19 08:18:04 +0000 | [diff] [blame] | 235 | if (ret < 0) |
| 236 | return ret; |
Troy Kisky | cea60b0 | 2012-07-19 08:18:04 +0000 | [diff] [blame] | 237 | if (ret & I2SR_RX_NO_AK) |
| 238 | return -ENODEV; |
| 239 | return 0; |
Marek Vasut | db84140 | 2011-09-22 09:22:12 +0000 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | /* |
Peng Fan | 71204e9 | 2015-05-15 07:29:12 +0800 | [diff] [blame] | 243 | * Stub implementations for outer i2c slave operations. |
| 244 | */ |
| 245 | void __i2c_force_reset_slave(void) |
| 246 | { |
| 247 | } |
| 248 | void i2c_force_reset_slave(void) |
| 249 | __attribute__((weak, alias("__i2c_force_reset_slave"))); |
| 250 | |
| 251 | /* |
Troy Kisky | 90a5b70 | 2012-07-19 08:18:13 +0000 | [diff] [blame] | 252 | * Stop I2C transaction |
Marek Vasut | db84140 | 2011-09-22 09:22:12 +0000 | [diff] [blame] | 253 | */ |
Peng Fan | 71204e9 | 2015-05-15 07:29:12 +0800 | [diff] [blame] | 254 | static void i2c_imx_stop(struct mxc_i2c_bus *i2c_bus) |
Sascha Hauer | cdace06 | 2008-03-26 20:40:49 +0100 | [diff] [blame] | 255 | { |
Troy Kisky | 7aa57a0 | 2012-07-19 08:18:09 +0000 | [diff] [blame] | 256 | int ret; |
Peng Fan | 71204e9 | 2015-05-15 07:29:12 +0800 | [diff] [blame] | 257 | int reg_shift = i2c_bus->driver_data & I2C_QUIRK_FLAG ? |
| 258 | VF610_I2C_REGSHIFT : IMX_I2C_REGSHIFT; |
| 259 | ulong base = i2c_bus->base; |
| 260 | unsigned int temp = readb(base + (I2CR << reg_shift)); |
Sascha Hauer | cdace06 | 2008-03-26 20:40:49 +0100 | [diff] [blame] | 261 | |
Troy Kisky | 1c076db | 2012-07-19 08:18:02 +0000 | [diff] [blame] | 262 | temp &= ~(I2CR_MSTA | I2CR_MTX); |
Peng Fan | 71204e9 | 2015-05-15 07:29:12 +0800 | [diff] [blame] | 263 | writeb(temp, base + (I2CR << reg_shift)); |
| 264 | ret = wait_for_sr_state(i2c_bus, ST_BUS_IDLE); |
Troy Kisky | 7aa57a0 | 2012-07-19 08:18:09 +0000 | [diff] [blame] | 265 | if (ret < 0) |
| 266 | printf("%s:trigger stop failed\n", __func__); |
Sascha Hauer | cdace06 | 2008-03-26 20:40:49 +0100 | [diff] [blame] | 267 | } |
| 268 | |
Marek Vasut | db84140 | 2011-09-22 09:22:12 +0000 | [diff] [blame] | 269 | /* |
Troy Kisky | b230ddc | 2012-07-19 08:18:06 +0000 | [diff] [blame] | 270 | * Send start signal, chip address and |
| 271 | * write register address |
Marek Vasut | db84140 | 2011-09-22 09:22:12 +0000 | [diff] [blame] | 272 | */ |
Peng Fan | 71204e9 | 2015-05-15 07:29:12 +0800 | [diff] [blame] | 273 | static int i2c_init_transfer_(struct mxc_i2c_bus *i2c_bus, u8 chip, |
| 274 | u32 addr, int alen) |
Sascha Hauer | cdace06 | 2008-03-26 20:40:49 +0100 | [diff] [blame] | 275 | { |
Troy Kisky | 71e9f3c | 2012-07-19 08:18:11 +0000 | [diff] [blame] | 276 | unsigned int temp; |
| 277 | int ret; |
Peng Fan | 71204e9 | 2015-05-15 07:29:12 +0800 | [diff] [blame] | 278 | bool quirk = i2c_bus->driver_data & I2C_QUIRK_FLAG ? true : false; |
| 279 | ulong base = i2c_bus->base; |
| 280 | int reg_shift = quirk ? VF610_I2C_REGSHIFT : IMX_I2C_REGSHIFT; |
| 281 | |
| 282 | /* Reset i2c slave */ |
| 283 | i2c_force_reset_slave(); |
Troy Kisky | 71e9f3c | 2012-07-19 08:18:11 +0000 | [diff] [blame] | 284 | |
| 285 | /* Enable I2C controller */ |
Peng Fan | 71204e9 | 2015-05-15 07:29:12 +0800 | [diff] [blame] | 286 | if (quirk) |
| 287 | ret = readb(base + (I2CR << reg_shift)) & I2CR_IDIS; |
| 288 | else |
| 289 | ret = !(readb(base + (I2CR << reg_shift)) & I2CR_IEN); |
| 290 | |
| 291 | if (ret) { |
| 292 | writeb(I2CR_IEN, base + (I2CR << reg_shift)); |
Troy Kisky | 90a5b70 | 2012-07-19 08:18:13 +0000 | [diff] [blame] | 293 | /* Wait for controller to be stable */ |
| 294 | udelay(50); |
| 295 | } |
Peng Fan | 71204e9 | 2015-05-15 07:29:12 +0800 | [diff] [blame] | 296 | |
| 297 | if (readb(base + (IADR << reg_shift)) == (chip << 1)) |
| 298 | writeb((chip << 1) ^ 2, base + (IADR << reg_shift)); |
| 299 | writeb(I2SR_IIF_CLEAR, base + (I2SR << reg_shift)); |
| 300 | ret = wait_for_sr_state(i2c_bus, ST_BUS_IDLE); |
Troy Kisky | 90a5b70 | 2012-07-19 08:18:13 +0000 | [diff] [blame] | 301 | if (ret < 0) |
Troy Kisky | a7f1a00 | 2012-07-19 08:18:16 +0000 | [diff] [blame] | 302 | return ret; |
Troy Kisky | 71e9f3c | 2012-07-19 08:18:11 +0000 | [diff] [blame] | 303 | |
| 304 | /* Start I2C transaction */ |
Peng Fan | 71204e9 | 2015-05-15 07:29:12 +0800 | [diff] [blame] | 305 | temp = readb(base + (I2CR << reg_shift)); |
Troy Kisky | 71e9f3c | 2012-07-19 08:18:11 +0000 | [diff] [blame] | 306 | temp |= I2CR_MSTA; |
Peng Fan | 71204e9 | 2015-05-15 07:29:12 +0800 | [diff] [blame] | 307 | writeb(temp, base + (I2CR << reg_shift)); |
Troy Kisky | 71e9f3c | 2012-07-19 08:18:11 +0000 | [diff] [blame] | 308 | |
Peng Fan | 71204e9 | 2015-05-15 07:29:12 +0800 | [diff] [blame] | 309 | ret = wait_for_sr_state(i2c_bus, ST_BUS_BUSY); |
Troy Kisky | 71e9f3c | 2012-07-19 08:18:11 +0000 | [diff] [blame] | 310 | if (ret < 0) |
Troy Kisky | a7f1a00 | 2012-07-19 08:18:16 +0000 | [diff] [blame] | 311 | return ret; |
Troy Kisky | b230ddc | 2012-07-19 08:18:06 +0000 | [diff] [blame] | 312 | |
Troy Kisky | 71e9f3c | 2012-07-19 08:18:11 +0000 | [diff] [blame] | 313 | temp |= I2CR_MTX | I2CR_TX_NO_AK; |
Peng Fan | 71204e9 | 2015-05-15 07:29:12 +0800 | [diff] [blame] | 314 | writeb(temp, base + (I2CR << reg_shift)); |
Troy Kisky | 71e9f3c | 2012-07-19 08:18:11 +0000 | [diff] [blame] | 315 | |
Troy Kisky | b230ddc | 2012-07-19 08:18:06 +0000 | [diff] [blame] | 316 | /* write slave address */ |
Peng Fan | 71204e9 | 2015-05-15 07:29:12 +0800 | [diff] [blame] | 317 | ret = tx_byte(i2c_bus, chip << 1); |
Troy Kisky | b230ddc | 2012-07-19 08:18:06 +0000 | [diff] [blame] | 318 | if (ret < 0) |
Troy Kisky | a7f1a00 | 2012-07-19 08:18:16 +0000 | [diff] [blame] | 319 | return ret; |
Marek Vasut | db84140 | 2011-09-22 09:22:12 +0000 | [diff] [blame] | 320 | |
Marek Vasut | bf0783d | 2011-10-26 00:05:44 +0000 | [diff] [blame] | 321 | while (alen--) { |
Peng Fan | 71204e9 | 2015-05-15 07:29:12 +0800 | [diff] [blame] | 322 | ret = tx_byte(i2c_bus, (addr >> (alen * 8)) & 0xff); |
Troy Kisky | cea60b0 | 2012-07-19 08:18:04 +0000 | [diff] [blame] | 323 | if (ret < 0) |
Troy Kisky | a7f1a00 | 2012-07-19 08:18:16 +0000 | [diff] [blame] | 324 | return ret; |
Stefano Babic | 8168721 | 2011-01-20 07:51:31 +0000 | [diff] [blame] | 325 | } |
Troy Kisky | b230ddc | 2012-07-19 08:18:06 +0000 | [diff] [blame] | 326 | return 0; |
Troy Kisky | a7f1a00 | 2012-07-19 08:18:16 +0000 | [diff] [blame] | 327 | } |
| 328 | |
Peng Fan | 71204e9 | 2015-05-15 07:29:12 +0800 | [diff] [blame] | 329 | #ifndef CONFIG_DM_I2C |
| 330 | int i2c_idle_bus(struct mxc_i2c_bus *i2c_bus) |
| 331 | { |
| 332 | if (i2c_bus && i2c_bus->idle_bus_fn) |
| 333 | return i2c_bus->idle_bus_fn(i2c_bus->idle_bus_data); |
| 334 | return 0; |
| 335 | } |
| 336 | #else |
| 337 | /* |
Peng Fan | e1bed80 | 2016-03-11 16:47:50 +0800 | [diff] [blame] | 338 | * See Linux Documentation/devicetree/bindings/i2c/i2c-imx.txt |
| 339 | * " |
| 340 | * scl-gpios: specify the gpio related to SCL pin |
| 341 | * sda-gpios: specify the gpio related to SDA pin |
| 342 | * add pinctrl to configure i2c pins to gpio function for i2c |
| 343 | * bus recovery, call it "gpio" state |
| 344 | * " |
| 345 | * |
| 346 | * The i2c_idle_bus is an implementation following Linux Kernel. |
Peng Fan | 71204e9 | 2015-05-15 07:29:12 +0800 | [diff] [blame] | 347 | */ |
Peng Fan | 71204e9 | 2015-05-15 07:29:12 +0800 | [diff] [blame] | 348 | int i2c_idle_bus(struct mxc_i2c_bus *i2c_bus) |
Peng Fan | e1bed80 | 2016-03-11 16:47:50 +0800 | [diff] [blame] | 349 | { |
| 350 | struct udevice *bus = i2c_bus->bus; |
| 351 | struct gpio_desc *scl_gpio = &i2c_bus->scl_gpio; |
| 352 | struct gpio_desc *sda_gpio = &i2c_bus->sda_gpio; |
| 353 | int sda, scl; |
| 354 | int i, ret = 0; |
| 355 | ulong elapsed, start_time; |
| 356 | |
| 357 | if (pinctrl_select_state(bus, "gpio")) { |
| 358 | dev_dbg(bus, "Can not to switch to use gpio pinmux\n"); |
| 359 | /* |
| 360 | * GPIO pinctrl for i2c force idle is not a must, |
| 361 | * but it is strongly recommended to be used. |
| 362 | * Because it can help you to recover from bad |
| 363 | * i2c bus state. Do not return failure, because |
| 364 | * it is not a must. |
| 365 | */ |
| 366 | return 0; |
| 367 | } |
| 368 | |
| 369 | dm_gpio_set_dir_flags(scl_gpio, GPIOD_IS_IN); |
| 370 | dm_gpio_set_dir_flags(sda_gpio, GPIOD_IS_IN); |
| 371 | scl = dm_gpio_get_value(scl_gpio); |
| 372 | sda = dm_gpio_get_value(sda_gpio); |
| 373 | |
| 374 | if ((sda & scl) == 1) |
| 375 | goto exit; /* Bus is idle already */ |
| 376 | |
| 377 | /* Send high and low on the SCL line */ |
| 378 | for (i = 0; i < 9; i++) { |
| 379 | dm_gpio_set_dir_flags(scl_gpio, GPIOD_IS_OUT); |
| 380 | dm_gpio_set_value(scl_gpio, 0); |
| 381 | udelay(50); |
| 382 | dm_gpio_set_dir_flags(scl_gpio, GPIOD_IS_IN); |
| 383 | udelay(50); |
| 384 | } |
| 385 | start_time = get_timer(0); |
| 386 | for (;;) { |
| 387 | dm_gpio_set_dir_flags(scl_gpio, GPIOD_IS_IN); |
| 388 | dm_gpio_set_dir_flags(sda_gpio, GPIOD_IS_IN); |
| 389 | scl = dm_gpio_get_value(scl_gpio); |
| 390 | sda = dm_gpio_get_value(sda_gpio); |
| 391 | if ((sda & scl) == 1) |
| 392 | break; |
| 393 | WATCHDOG_RESET(); |
| 394 | elapsed = get_timer(start_time); |
| 395 | if (elapsed > (CONFIG_SYS_HZ / 5)) { /* .2 seconds */ |
| 396 | ret = -EBUSY; |
| 397 | printf("%s: failed to clear bus, sda=%d scl=%d\n", __func__, sda, scl); |
| 398 | break; |
| 399 | } |
| 400 | } |
| 401 | |
| 402 | exit: |
| 403 | pinctrl_select_state(bus, "default"); |
| 404 | return ret; |
| 405 | } |
Peng Fan | 71204e9 | 2015-05-15 07:29:12 +0800 | [diff] [blame] | 406 | #endif |
| 407 | |
| 408 | static int i2c_init_transfer(struct mxc_i2c_bus *i2c_bus, u8 chip, |
| 409 | u32 addr, int alen) |
Troy Kisky | a7f1a00 | 2012-07-19 08:18:16 +0000 | [diff] [blame] | 410 | { |
| 411 | int retry; |
| 412 | int ret; |
Peng Fan | 71204e9 | 2015-05-15 07:29:12 +0800 | [diff] [blame] | 413 | int reg_shift = i2c_bus->driver_data & I2C_QUIRK_FLAG ? |
| 414 | VF610_I2C_REGSHIFT : IMX_I2C_REGSHIFT; |
Heiko Schocher | e6c8b71 | 2015-05-18 10:58:12 +0200 | [diff] [blame] | 415 | |
| 416 | if (!i2c_bus->base) |
| 417 | return -ENODEV; |
| 418 | |
Troy Kisky | a7f1a00 | 2012-07-19 08:18:16 +0000 | [diff] [blame] | 419 | for (retry = 0; retry < 3; retry++) { |
Peng Fan | 71204e9 | 2015-05-15 07:29:12 +0800 | [diff] [blame] | 420 | ret = i2c_init_transfer_(i2c_bus, chip, addr, alen); |
Troy Kisky | a7f1a00 | 2012-07-19 08:18:16 +0000 | [diff] [blame] | 421 | if (ret >= 0) |
| 422 | return 0; |
Peng Fan | 71204e9 | 2015-05-15 07:29:12 +0800 | [diff] [blame] | 423 | i2c_imx_stop(i2c_bus); |
Troy Kisky | a7f1a00 | 2012-07-19 08:18:16 +0000 | [diff] [blame] | 424 | if (ret == -ENODEV) |
| 425 | return ret; |
| 426 | |
| 427 | printf("%s: failed for chip 0x%x retry=%d\n", __func__, chip, |
| 428 | retry); |
| 429 | if (ret != -ERESTART) |
Alison Wang | 30ea41a | 2013-06-17 15:30:39 +0800 | [diff] [blame] | 430 | /* Disable controller */ |
Peng Fan | 71204e9 | 2015-05-15 07:29:12 +0800 | [diff] [blame] | 431 | writeb(I2CR_IDIS, i2c_bus->base + (I2CR << reg_shift)); |
Troy Kisky | a7f1a00 | 2012-07-19 08:18:16 +0000 | [diff] [blame] | 432 | udelay(100); |
Peng Fan | 71204e9 | 2015-05-15 07:29:12 +0800 | [diff] [blame] | 433 | if (i2c_idle_bus(i2c_bus) < 0) |
Troy Kisky | 96c19bd | 2012-07-19 08:18:19 +0000 | [diff] [blame] | 434 | break; |
Troy Kisky | a7f1a00 | 2012-07-19 08:18:16 +0000 | [diff] [blame] | 435 | } |
Peng Fan | 71204e9 | 2015-05-15 07:29:12 +0800 | [diff] [blame] | 436 | printf("%s: give up i2c_regs=0x%lx\n", __func__, i2c_bus->base); |
Marek Vasut | db84140 | 2011-09-22 09:22:12 +0000 | [diff] [blame] | 437 | return ret; |
Sascha Hauer | cdace06 | 2008-03-26 20:40:49 +0100 | [diff] [blame] | 438 | } |
| 439 | |
Peng Fan | 71204e9 | 2015-05-15 07:29:12 +0800 | [diff] [blame] | 440 | |
| 441 | static int i2c_write_data(struct mxc_i2c_bus *i2c_bus, u8 chip, const u8 *buf, |
| 442 | int len) |
| 443 | { |
| 444 | int i, ret = 0; |
| 445 | |
| 446 | debug("i2c_write_data: chip=0x%x, len=0x%x\n", chip, len); |
| 447 | debug("write_data: "); |
| 448 | /* use rc for counter */ |
| 449 | for (i = 0; i < len; ++i) |
| 450 | debug(" 0x%02x", buf[i]); |
| 451 | debug("\n"); |
| 452 | |
| 453 | for (i = 0; i < len; i++) { |
| 454 | ret = tx_byte(i2c_bus, buf[i]); |
| 455 | if (ret < 0) { |
| 456 | debug("i2c_write_data(): rc=%d\n", ret); |
| 457 | break; |
| 458 | } |
| 459 | } |
| 460 | |
| 461 | return ret; |
| 462 | } |
| 463 | |
| 464 | static int i2c_read_data(struct mxc_i2c_bus *i2c_bus, uchar chip, uchar *buf, |
| 465 | int len) |
Marek Vasut | db84140 | 2011-09-22 09:22:12 +0000 | [diff] [blame] | 466 | { |
Marek Vasut | db84140 | 2011-09-22 09:22:12 +0000 | [diff] [blame] | 467 | int ret; |
| 468 | unsigned int temp; |
| 469 | int i; |
Peng Fan | 71204e9 | 2015-05-15 07:29:12 +0800 | [diff] [blame] | 470 | int reg_shift = i2c_bus->driver_data & I2C_QUIRK_FLAG ? |
| 471 | VF610_I2C_REGSHIFT : IMX_I2C_REGSHIFT; |
| 472 | ulong base = i2c_bus->base; |
Marek Vasut | db84140 | 2011-09-22 09:22:12 +0000 | [diff] [blame] | 473 | |
Peng Fan | 71204e9 | 2015-05-15 07:29:12 +0800 | [diff] [blame] | 474 | debug("i2c_read_data: chip=0x%x, len=0x%x\n", chip, len); |
Marek Vasut | db84140 | 2011-09-22 09:22:12 +0000 | [diff] [blame] | 475 | |
| 476 | /* setup bus to read data */ |
Peng Fan | 71204e9 | 2015-05-15 07:29:12 +0800 | [diff] [blame] | 477 | temp = readb(base + (I2CR << reg_shift)); |
Marek Vasut | db84140 | 2011-09-22 09:22:12 +0000 | [diff] [blame] | 478 | temp &= ~(I2CR_MTX | I2CR_TX_NO_AK); |
| 479 | if (len == 1) |
| 480 | temp |= I2CR_TX_NO_AK; |
Peng Fan | 71204e9 | 2015-05-15 07:29:12 +0800 | [diff] [blame] | 481 | writeb(temp, base + (I2CR << reg_shift)); |
| 482 | writeb(I2SR_IIF_CLEAR, base + (I2SR << reg_shift)); |
| 483 | /* dummy read to clear ICF */ |
| 484 | readb(base + (I2DR << reg_shift)); |
Marek Vasut | db84140 | 2011-09-22 09:22:12 +0000 | [diff] [blame] | 485 | |
| 486 | /* read data */ |
| 487 | for (i = 0; i < len; i++) { |
Peng Fan | 71204e9 | 2015-05-15 07:29:12 +0800 | [diff] [blame] | 488 | ret = wait_for_sr_state(i2c_bus, ST_IIF); |
Troy Kisky | 7aa57a0 | 2012-07-19 08:18:09 +0000 | [diff] [blame] | 489 | if (ret < 0) { |
Peng Fan | 71204e9 | 2015-05-15 07:29:12 +0800 | [diff] [blame] | 490 | debug("i2c_read_data(): ret=%d\n", ret); |
| 491 | i2c_imx_stop(i2c_bus); |
Marek Vasut | db84140 | 2011-09-22 09:22:12 +0000 | [diff] [blame] | 492 | return ret; |
Troy Kisky | c4330d2 | 2012-07-19 08:18:07 +0000 | [diff] [blame] | 493 | } |
Marek Vasut | db84140 | 2011-09-22 09:22:12 +0000 | [diff] [blame] | 494 | |
| 495 | /* |
| 496 | * It must generate STOP before read I2DR to prevent |
| 497 | * controller from generating another clock cycle |
| 498 | */ |
| 499 | if (i == (len - 1)) { |
Peng Fan | 71204e9 | 2015-05-15 07:29:12 +0800 | [diff] [blame] | 500 | i2c_imx_stop(i2c_bus); |
Marek Vasut | db84140 | 2011-09-22 09:22:12 +0000 | [diff] [blame] | 501 | } else if (i == (len - 2)) { |
Peng Fan | 71204e9 | 2015-05-15 07:29:12 +0800 | [diff] [blame] | 502 | temp = readb(base + (I2CR << reg_shift)); |
Marek Vasut | db84140 | 2011-09-22 09:22:12 +0000 | [diff] [blame] | 503 | temp |= I2CR_TX_NO_AK; |
Peng Fan | 71204e9 | 2015-05-15 07:29:12 +0800 | [diff] [blame] | 504 | writeb(temp, base + (I2CR << reg_shift)); |
Marek Vasut | db84140 | 2011-09-22 09:22:12 +0000 | [diff] [blame] | 505 | } |
Peng Fan | 71204e9 | 2015-05-15 07:29:12 +0800 | [diff] [blame] | 506 | writeb(I2SR_IIF_CLEAR, base + (I2SR << reg_shift)); |
| 507 | buf[i] = readb(base + (I2DR << reg_shift)); |
Marek Vasut | db84140 | 2011-09-22 09:22:12 +0000 | [diff] [blame] | 508 | } |
Peng Fan | 71204e9 | 2015-05-15 07:29:12 +0800 | [diff] [blame] | 509 | |
| 510 | /* reuse ret for counter*/ |
| 511 | for (ret = 0; ret < len; ++ret) |
| 512 | debug(" 0x%02x", buf[ret]); |
| 513 | debug("\n"); |
| 514 | |
| 515 | i2c_imx_stop(i2c_bus); |
Troy Kisky | 7aa57a0 | 2012-07-19 08:18:09 +0000 | [diff] [blame] | 516 | return 0; |
Marek Vasut | db84140 | 2011-09-22 09:22:12 +0000 | [diff] [blame] | 517 | } |
| 518 | |
Peng Fan | 71204e9 | 2015-05-15 07:29:12 +0800 | [diff] [blame] | 519 | #ifndef CONFIG_DM_I2C |
| 520 | /* |
| 521 | * Read data from I2C device |
| 522 | */ |
| 523 | static int bus_i2c_read(struct mxc_i2c_bus *i2c_bus, u8 chip, u32 addr, |
| 524 | int alen, u8 *buf, int len) |
| 525 | { |
| 526 | int ret = 0; |
| 527 | u32 temp; |
| 528 | int reg_shift = i2c_bus->driver_data & I2C_QUIRK_FLAG ? |
| 529 | VF610_I2C_REGSHIFT : IMX_I2C_REGSHIFT; |
| 530 | ulong base = i2c_bus->base; |
| 531 | |
| 532 | ret = i2c_init_transfer(i2c_bus, chip, addr, alen); |
| 533 | if (ret < 0) |
| 534 | return ret; |
| 535 | |
| 536 | temp = readb(base + (I2CR << reg_shift)); |
| 537 | temp |= I2CR_RSTA; |
| 538 | writeb(temp, base + (I2CR << reg_shift)); |
| 539 | |
| 540 | ret = tx_byte(i2c_bus, (chip << 1) | 1); |
| 541 | if (ret < 0) { |
| 542 | i2c_imx_stop(i2c_bus); |
| 543 | return ret; |
| 544 | } |
| 545 | |
| 546 | ret = i2c_read_data(i2c_bus, chip, buf, len); |
| 547 | |
| 548 | i2c_imx_stop(i2c_bus); |
| 549 | return ret; |
| 550 | } |
| 551 | |
Marek Vasut | db84140 | 2011-09-22 09:22:12 +0000 | [diff] [blame] | 552 | /* |
| 553 | * Write data to I2C device |
| 554 | */ |
Peng Fan | 71204e9 | 2015-05-15 07:29:12 +0800 | [diff] [blame] | 555 | static int bus_i2c_write(struct mxc_i2c_bus *i2c_bus, u8 chip, u32 addr, |
| 556 | int alen, const u8 *buf, int len) |
Sascha Hauer | cdace06 | 2008-03-26 20:40:49 +0100 | [diff] [blame] | 557 | { |
Peng Fan | 71204e9 | 2015-05-15 07:29:12 +0800 | [diff] [blame] | 558 | int ret = 0; |
Sascha Hauer | cdace06 | 2008-03-26 20:40:49 +0100 | [diff] [blame] | 559 | |
Peng Fan | 71204e9 | 2015-05-15 07:29:12 +0800 | [diff] [blame] | 560 | ret = i2c_init_transfer(i2c_bus, chip, addr, alen); |
Troy Kisky | cea60b0 | 2012-07-19 08:18:04 +0000 | [diff] [blame] | 561 | if (ret < 0) |
Marek Vasut | db84140 | 2011-09-22 09:22:12 +0000 | [diff] [blame] | 562 | return ret; |
Sascha Hauer | cdace06 | 2008-03-26 20:40:49 +0100 | [diff] [blame] | 563 | |
Peng Fan | 71204e9 | 2015-05-15 07:29:12 +0800 | [diff] [blame] | 564 | ret = i2c_write_data(i2c_bus, chip, buf, len); |
| 565 | |
| 566 | i2c_imx_stop(i2c_bus); |
| 567 | |
Marek Vasut | db84140 | 2011-09-22 09:22:12 +0000 | [diff] [blame] | 568 | return ret; |
Sascha Hauer | cdace06 | 2008-03-26 20:40:49 +0100 | [diff] [blame] | 569 | } |
Troy Kisky | cfbb88d | 2012-07-19 08:18:08 +0000 | [diff] [blame] | 570 | |
Heiko Schocher | e6c8b71 | 2015-05-18 10:58:12 +0200 | [diff] [blame] | 571 | #if !defined(I2C2_BASE_ADDR) |
| 572 | #define I2C2_BASE_ADDR 0 |
Heiko Schocher | 21a2694 | 2015-05-18 10:56:24 +0200 | [diff] [blame] | 573 | #endif |
Heiko Schocher | e6c8b71 | 2015-05-18 10:58:12 +0200 | [diff] [blame] | 574 | |
| 575 | #if !defined(I2C3_BASE_ADDR) |
| 576 | #define I2C3_BASE_ADDR 0 |
| 577 | #endif |
| 578 | |
| 579 | #if !defined(I2C4_BASE_ADDR) |
| 580 | #define I2C4_BASE_ADDR 0 |
| 581 | #endif |
| 582 | |
| 583 | static struct mxc_i2c_bus mxc_i2c_buses[] = { |
Mingkai Hu | 9f3183d | 2015-10-26 19:47:50 +0800 | [diff] [blame] | 584 | #if defined(CONFIG_LS102XA) || defined(CONFIG_VF610) || \ |
| 585 | defined(CONFIG_FSL_LAYERSCAPE) |
Peng Fan | 71204e9 | 2015-05-15 07:29:12 +0800 | [diff] [blame] | 586 | { 0, I2C1_BASE_ADDR, I2C_QUIRK_FLAG }, |
| 587 | { 1, I2C2_BASE_ADDR, I2C_QUIRK_FLAG }, |
| 588 | { 2, I2C3_BASE_ADDR, I2C_QUIRK_FLAG }, |
| 589 | { 3, I2C4_BASE_ADDR, I2C_QUIRK_FLAG }, |
Troy Kisky | e4ff525 | 2012-07-19 08:18:18 +0000 | [diff] [blame] | 590 | #else |
Heiko Schocher | e6c8b71 | 2015-05-18 10:58:12 +0200 | [diff] [blame] | 591 | { 0, I2C1_BASE_ADDR, 0 }, |
| 592 | { 1, I2C2_BASE_ADDR, 0 }, |
| 593 | { 2, I2C3_BASE_ADDR, 0 }, |
| 594 | { 3, I2C4_BASE_ADDR, 0 }, |
Troy Kisky | e4ff525 | 2012-07-19 08:18:18 +0000 | [diff] [blame] | 595 | #endif |
trem | fac9640 | 2013-09-21 18:13:35 +0200 | [diff] [blame] | 596 | }; |
| 597 | |
Peng Fan | 71204e9 | 2015-05-15 07:29:12 +0800 | [diff] [blame] | 598 | struct mxc_i2c_bus *i2c_get_base(struct i2c_adapter *adap) |
trem | fac9640 | 2013-09-21 18:13:35 +0200 | [diff] [blame] | 599 | { |
Peng Fan | 71204e9 | 2015-05-15 07:29:12 +0800 | [diff] [blame] | 600 | return &mxc_i2c_buses[adap->hwadapnr]; |
Troy Kisky | 96c19bd | 2012-07-19 08:18:19 +0000 | [diff] [blame] | 601 | } |
| 602 | |
trem | fac9640 | 2013-09-21 18:13:35 +0200 | [diff] [blame] | 603 | static int mxc_i2c_read(struct i2c_adapter *adap, uint8_t chip, |
| 604 | uint addr, int alen, uint8_t *buffer, |
| 605 | int len) |
Troy Kisky | 9815326 | 2012-07-19 08:18:20 +0000 | [diff] [blame] | 606 | { |
trem | fac9640 | 2013-09-21 18:13:35 +0200 | [diff] [blame] | 607 | return bus_i2c_read(i2c_get_base(adap), chip, addr, alen, buffer, len); |
Troy Kisky | 9815326 | 2012-07-19 08:18:20 +0000 | [diff] [blame] | 608 | } |
| 609 | |
trem | fac9640 | 2013-09-21 18:13:35 +0200 | [diff] [blame] | 610 | static int mxc_i2c_write(struct i2c_adapter *adap, uint8_t chip, |
| 611 | uint addr, int alen, uint8_t *buffer, |
| 612 | int len) |
Troy Kisky | 9815326 | 2012-07-19 08:18:20 +0000 | [diff] [blame] | 613 | { |
trem | fac9640 | 2013-09-21 18:13:35 +0200 | [diff] [blame] | 614 | return bus_i2c_write(i2c_get_base(adap), chip, addr, alen, buffer, len); |
Troy Kisky | e4ff525 | 2012-07-19 08:18:18 +0000 | [diff] [blame] | 615 | } |
| 616 | |
Troy Kisky | cfbb88d | 2012-07-19 08:18:08 +0000 | [diff] [blame] | 617 | /* |
| 618 | * Test if a chip at a given address responds (probe the chip) |
| 619 | */ |
trem | fac9640 | 2013-09-21 18:13:35 +0200 | [diff] [blame] | 620 | static int mxc_i2c_probe(struct i2c_adapter *adap, uint8_t chip) |
Troy Kisky | cfbb88d | 2012-07-19 08:18:08 +0000 | [diff] [blame] | 621 | { |
trem | fac9640 | 2013-09-21 18:13:35 +0200 | [diff] [blame] | 622 | return bus_i2c_write(i2c_get_base(adap), chip, 0, 0, NULL, 0); |
Troy Kisky | e4ff525 | 2012-07-19 08:18:18 +0000 | [diff] [blame] | 623 | } |
| 624 | |
Peng Fan | 71204e9 | 2015-05-15 07:29:12 +0800 | [diff] [blame] | 625 | int __enable_i2c_clk(unsigned char enable, unsigned i2c_num) |
Troy Kisky | e4ff525 | 2012-07-19 08:18:18 +0000 | [diff] [blame] | 626 | { |
Peng Fan | 71204e9 | 2015-05-15 07:29:12 +0800 | [diff] [blame] | 627 | return 1; |
| 628 | } |
| 629 | int enable_i2c_clk(unsigned char enable, unsigned i2c_num) |
| 630 | __attribute__((weak, alias("__enable_i2c_clk"))); |
| 631 | |
| 632 | void bus_i2c_init(int index, int speed, int unused, |
| 633 | int (*idle_bus_fn)(void *p), void *idle_bus_data) |
| 634 | { |
| 635 | int ret; |
| 636 | |
| 637 | if (index >= ARRAY_SIZE(mxc_i2c_buses)) { |
| 638 | debug("Error i2c index\n"); |
Troy Kisky | e4ff525 | 2012-07-19 08:18:18 +0000 | [diff] [blame] | 639 | return; |
Troy Kisky | e4ff525 | 2012-07-19 08:18:18 +0000 | [diff] [blame] | 640 | } |
Peng Fan | 71204e9 | 2015-05-15 07:29:12 +0800 | [diff] [blame] | 641 | |
Gong Qianyu | aee3fdd | 2015-12-18 17:38:01 +0800 | [diff] [blame] | 642 | /* |
| 643 | * Warning: Be careful to allow the assignment to a static |
| 644 | * variable here. This function could be called while U-Boot is |
| 645 | * still running in flash memory. So such assignment is equal |
| 646 | * to write data to flash without erasing. |
| 647 | */ |
| 648 | if (idle_bus_fn) |
| 649 | mxc_i2c_buses[index].idle_bus_fn = idle_bus_fn; |
| 650 | if (idle_bus_data) |
| 651 | mxc_i2c_buses[index].idle_bus_data = idle_bus_data; |
Peng Fan | 71204e9 | 2015-05-15 07:29:12 +0800 | [diff] [blame] | 652 | |
| 653 | ret = enable_i2c_clk(1, index); |
| 654 | if (ret < 0) { |
| 655 | debug("I2C-%d clk fail to enable.\n", index); |
| 656 | return; |
| 657 | } |
| 658 | |
| 659 | bus_i2c_set_bus_speed(&mxc_i2c_buses[index], speed); |
Troy Kisky | e4ff525 | 2012-07-19 08:18:18 +0000 | [diff] [blame] | 660 | } |
| 661 | |
| 662 | /* |
| 663 | * Init I2C Bus |
| 664 | */ |
trem | fac9640 | 2013-09-21 18:13:35 +0200 | [diff] [blame] | 665 | static void mxc_i2c_init(struct i2c_adapter *adap, int speed, int slaveaddr) |
Troy Kisky | e4ff525 | 2012-07-19 08:18:18 +0000 | [diff] [blame] | 666 | { |
Peng Fan | 71204e9 | 2015-05-15 07:29:12 +0800 | [diff] [blame] | 667 | bus_i2c_init(adap->hwadapnr, speed, slaveaddr, NULL, NULL); |
Troy Kisky | e4ff525 | 2012-07-19 08:18:18 +0000 | [diff] [blame] | 668 | } |
| 669 | |
| 670 | /* |
| 671 | * Set I2C Speed |
| 672 | */ |
Peng Fan | 71204e9 | 2015-05-15 07:29:12 +0800 | [diff] [blame] | 673 | static u32 mxc_i2c_set_bus_speed(struct i2c_adapter *adap, uint speed) |
Troy Kisky | e4ff525 | 2012-07-19 08:18:18 +0000 | [diff] [blame] | 674 | { |
trem | fac9640 | 2013-09-21 18:13:35 +0200 | [diff] [blame] | 675 | return bus_i2c_set_bus_speed(i2c_get_base(adap), speed); |
Troy Kisky | e4ff525 | 2012-07-19 08:18:18 +0000 | [diff] [blame] | 676 | } |
| 677 | |
| 678 | /* |
trem | fac9640 | 2013-09-21 18:13:35 +0200 | [diff] [blame] | 679 | * Register mxc i2c adapters |
Troy Kisky | e4ff525 | 2012-07-19 08:18:18 +0000 | [diff] [blame] | 680 | */ |
Albert ARIBAUD \\(3ADEV\\) | 03544c6 | 2015-09-21 22:43:38 +0200 | [diff] [blame] | 681 | #ifdef CONFIG_SYS_I2C_MXC_I2C1 |
trem | fac9640 | 2013-09-21 18:13:35 +0200 | [diff] [blame] | 682 | U_BOOT_I2C_ADAP_COMPLETE(mxc0, mxc_i2c_init, mxc_i2c_probe, |
| 683 | mxc_i2c_read, mxc_i2c_write, |
| 684 | mxc_i2c_set_bus_speed, |
| 685 | CONFIG_SYS_MXC_I2C1_SPEED, |
| 686 | CONFIG_SYS_MXC_I2C1_SLAVE, 0) |
Albert ARIBAUD \\(3ADEV\\) | 03544c6 | 2015-09-21 22:43:38 +0200 | [diff] [blame] | 687 | #endif |
| 688 | |
| 689 | #ifdef CONFIG_SYS_I2C_MXC_I2C2 |
trem | fac9640 | 2013-09-21 18:13:35 +0200 | [diff] [blame] | 690 | U_BOOT_I2C_ADAP_COMPLETE(mxc1, mxc_i2c_init, mxc_i2c_probe, |
| 691 | mxc_i2c_read, mxc_i2c_write, |
| 692 | mxc_i2c_set_bus_speed, |
| 693 | CONFIG_SYS_MXC_I2C2_SPEED, |
| 694 | CONFIG_SYS_MXC_I2C2_SLAVE, 1) |
Albert ARIBAUD \\(3ADEV\\) | 03544c6 | 2015-09-21 22:43:38 +0200 | [diff] [blame] | 695 | #endif |
| 696 | |
York Sun | f8cb101 | 2015-03-20 10:20:40 -0700 | [diff] [blame] | 697 | #ifdef CONFIG_SYS_I2C_MXC_I2C3 |
trem | fac9640 | 2013-09-21 18:13:35 +0200 | [diff] [blame] | 698 | U_BOOT_I2C_ADAP_COMPLETE(mxc2, mxc_i2c_init, mxc_i2c_probe, |
| 699 | mxc_i2c_read, mxc_i2c_write, |
| 700 | mxc_i2c_set_bus_speed, |
| 701 | CONFIG_SYS_MXC_I2C3_SPEED, |
| 702 | CONFIG_SYS_MXC_I2C3_SLAVE, 2) |
| 703 | #endif |
Peng Fan | 71204e9 | 2015-05-15 07:29:12 +0800 | [diff] [blame] | 704 | |
York Sun | f8cb101 | 2015-03-20 10:20:40 -0700 | [diff] [blame] | 705 | #ifdef CONFIG_SYS_I2C_MXC_I2C4 |
| 706 | U_BOOT_I2C_ADAP_COMPLETE(mxc3, mxc_i2c_init, mxc_i2c_probe, |
| 707 | mxc_i2c_read, mxc_i2c_write, |
| 708 | mxc_i2c_set_bus_speed, |
| 709 | CONFIG_SYS_MXC_I2C4_SPEED, |
| 710 | CONFIG_SYS_MXC_I2C4_SLAVE, 3) |
| 711 | #endif |
Peng Fan | 71204e9 | 2015-05-15 07:29:12 +0800 | [diff] [blame] | 712 | |
| 713 | #else |
| 714 | |
| 715 | static int mxc_i2c_set_bus_speed(struct udevice *bus, unsigned int speed) |
| 716 | { |
| 717 | struct mxc_i2c_bus *i2c_bus = dev_get_priv(bus); |
| 718 | |
| 719 | return bus_i2c_set_bus_speed(i2c_bus, speed); |
| 720 | } |
| 721 | |
| 722 | static int mxc_i2c_probe(struct udevice *bus) |
| 723 | { |
| 724 | struct mxc_i2c_bus *i2c_bus = dev_get_priv(bus); |
Peng Fan | e1bed80 | 2016-03-11 16:47:50 +0800 | [diff] [blame] | 725 | const void *fdt = gd->fdt_blob; |
| 726 | int node = bus->of_offset; |
Peng Fan | 71204e9 | 2015-05-15 07:29:12 +0800 | [diff] [blame] | 727 | fdt_addr_t addr; |
Peng Fan | e1bed80 | 2016-03-11 16:47:50 +0800 | [diff] [blame] | 728 | int ret, ret2; |
Peng Fan | 71204e9 | 2015-05-15 07:29:12 +0800 | [diff] [blame] | 729 | |
| 730 | i2c_bus->driver_data = dev_get_driver_data(bus); |
| 731 | |
| 732 | addr = dev_get_addr(bus); |
| 733 | if (addr == FDT_ADDR_T_NONE) |
| 734 | return -ENODEV; |
| 735 | |
| 736 | i2c_bus->base = addr; |
| 737 | i2c_bus->index = bus->seq; |
Peng Fan | e1bed80 | 2016-03-11 16:47:50 +0800 | [diff] [blame] | 738 | i2c_bus->bus = bus; |
Peng Fan | 71204e9 | 2015-05-15 07:29:12 +0800 | [diff] [blame] | 739 | |
| 740 | /* Enable clk */ |
| 741 | ret = enable_i2c_clk(1, bus->seq); |
| 742 | if (ret < 0) |
| 743 | return ret; |
| 744 | |
Peng Fan | e1bed80 | 2016-03-11 16:47:50 +0800 | [diff] [blame] | 745 | /* |
| 746 | * See Documentation/devicetree/bindings/i2c/i2c-imx.txt |
| 747 | * Use gpio to force bus idle when necessary. |
| 748 | */ |
| 749 | ret = fdt_find_string(fdt, node, "pinctrl-names", "gpio"); |
| 750 | if (ret < 0) { |
| 751 | dev_info(dev, "i2c bus %d at %lu, no gpio pinctrl state.\n", bus->seq, i2c_bus->base); |
| 752 | } else { |
| 753 | ret = gpio_request_by_name_nodev(fdt, node, "scl-gpios", |
| 754 | 0, &i2c_bus->scl_gpio, |
| 755 | GPIOD_IS_OUT); |
| 756 | ret2 = gpio_request_by_name_nodev(fdt, node, "sda-gpios", |
| 757 | 0, &i2c_bus->sda_gpio, |
| 758 | GPIOD_IS_OUT); |
| 759 | if (!dm_gpio_is_valid(&i2c_bus->sda_gpio) | |
| 760 | !dm_gpio_is_valid(&i2c_bus->scl_gpio) | |
| 761 | ret | ret2) { |
| 762 | dev_err(dev, "i2c bus %d at %lu, fail to request scl/sda gpio\n", bus->seq, i2c_bus->base); |
| 763 | return -ENODEV; |
| 764 | } |
| 765 | } |
| 766 | |
Peng Fan | 71204e9 | 2015-05-15 07:29:12 +0800 | [diff] [blame] | 767 | ret = i2c_idle_bus(i2c_bus); |
| 768 | if (ret < 0) { |
| 769 | /* Disable clk */ |
| 770 | enable_i2c_clk(0, bus->seq); |
| 771 | return ret; |
| 772 | } |
| 773 | |
| 774 | /* |
| 775 | * Pinmux settings are in board file now, until pinmux is supported, |
| 776 | * we can set pinmux here in probe function. |
| 777 | */ |
| 778 | |
| 779 | debug("i2c : controller bus %d at %lu , speed %d: ", |
| 780 | bus->seq, i2c_bus->base, |
| 781 | i2c_bus->speed); |
| 782 | |
| 783 | return 0; |
| 784 | } |
| 785 | |
| 786 | static int mxc_i2c_probe_chip(struct udevice *bus, u32 chip_addr, |
| 787 | u32 chip_flags) |
| 788 | { |
| 789 | int ret; |
| 790 | struct mxc_i2c_bus *i2c_bus = dev_get_priv(bus); |
| 791 | |
| 792 | ret = i2c_init_transfer(i2c_bus, chip_addr, 0, 0); |
| 793 | if (ret < 0) { |
| 794 | debug("%s failed, ret = %d\n", __func__, ret); |
| 795 | return ret; |
| 796 | } |
| 797 | |
| 798 | i2c_imx_stop(i2c_bus); |
| 799 | |
| 800 | return 0; |
| 801 | } |
| 802 | |
| 803 | static int mxc_i2c_xfer(struct udevice *bus, struct i2c_msg *msg, int nmsgs) |
| 804 | { |
| 805 | struct mxc_i2c_bus *i2c_bus = dev_get_priv(bus); |
| 806 | int ret = 0; |
| 807 | ulong base = i2c_bus->base; |
| 808 | int reg_shift = i2c_bus->driver_data & I2C_QUIRK_FLAG ? |
| 809 | VF610_I2C_REGSHIFT : IMX_I2C_REGSHIFT; |
| 810 | |
| 811 | /* |
| 812 | * Here the 3rd parameter addr and the 4th one alen are set to 0, |
| 813 | * because here we only want to send out chip address. The register |
| 814 | * address is wrapped in msg. |
| 815 | */ |
| 816 | ret = i2c_init_transfer(i2c_bus, msg->addr, 0, 0); |
| 817 | if (ret < 0) { |
| 818 | debug("i2c_init_transfer error: %d\n", ret); |
| 819 | return ret; |
| 820 | } |
| 821 | |
| 822 | for (; nmsgs > 0; nmsgs--, msg++) { |
| 823 | bool next_is_read = nmsgs > 1 && (msg[1].flags & I2C_M_RD); |
| 824 | debug("i2c_xfer: chip=0x%x, len=0x%x\n", msg->addr, msg->len); |
| 825 | if (msg->flags & I2C_M_RD) |
| 826 | ret = i2c_read_data(i2c_bus, msg->addr, msg->buf, |
| 827 | msg->len); |
| 828 | else { |
| 829 | ret = i2c_write_data(i2c_bus, msg->addr, msg->buf, |
| 830 | msg->len); |
| 831 | if (ret) |
| 832 | break; |
| 833 | if (next_is_read) { |
| 834 | /* Reuse ret */ |
| 835 | ret = readb(base + (I2CR << reg_shift)); |
| 836 | ret |= I2CR_RSTA; |
| 837 | writeb(ret, base + (I2CR << reg_shift)); |
| 838 | |
| 839 | ret = tx_byte(i2c_bus, (msg->addr << 1) | 1); |
| 840 | if (ret < 0) { |
| 841 | i2c_imx_stop(i2c_bus); |
| 842 | break; |
| 843 | } |
| 844 | } |
| 845 | } |
| 846 | } |
| 847 | |
| 848 | if (ret) |
| 849 | debug("i2c_write: error sending\n"); |
| 850 | |
| 851 | i2c_imx_stop(i2c_bus); |
| 852 | |
| 853 | return ret; |
| 854 | } |
| 855 | |
| 856 | static const struct dm_i2c_ops mxc_i2c_ops = { |
| 857 | .xfer = mxc_i2c_xfer, |
| 858 | .probe_chip = mxc_i2c_probe_chip, |
| 859 | .set_bus_speed = mxc_i2c_set_bus_speed, |
| 860 | }; |
| 861 | |
| 862 | static const struct udevice_id mxc_i2c_ids[] = { |
| 863 | { .compatible = "fsl,imx21-i2c", }, |
| 864 | { .compatible = "fsl,vf610-i2c", .data = I2C_QUIRK_FLAG, }, |
| 865 | {} |
| 866 | }; |
| 867 | |
| 868 | U_BOOT_DRIVER(i2c_mxc) = { |
| 869 | .name = "i2c_mxc", |
| 870 | .id = UCLASS_I2C, |
| 871 | .of_match = mxc_i2c_ids, |
| 872 | .probe = mxc_i2c_probe, |
| 873 | .priv_auto_alloc_size = sizeof(struct mxc_i2c_bus), |
| 874 | .ops = &mxc_i2c_ops, |
| 875 | }; |
| 876 | #endif |