Sascha Hauer | 15ea70f | 2008-03-26 20:40:49 +0100 | [diff] [blame] | 1 | /* |
Marek Vasut | 94cb842 | 2011-09-22 09:22:12 +0000 | [diff] [blame] | 2 | * i2c driver for Freescale i.MX series |
Sascha Hauer | 15ea70f | 2008-03-26 20:40:49 +0100 | [diff] [blame] | 3 | * |
| 4 | * (c) 2007 Pengutronix, Sascha Hauer <s.hauer@pengutronix.de> |
Marek Vasut | 94cb842 | 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 | 15ea70f | 2008-03-26 20:40:49 +0100 | [diff] [blame] | 13 | * |
| 14 | * See file CREDITS for list of people who contributed to this |
| 15 | * project. |
| 16 | * |
| 17 | * This program is free software; you can redistribute it and/or |
| 18 | * modify it under the terms of the GNU General Public License as |
| 19 | * published by the Free Software Foundation; either version 2 of |
| 20 | * the License, or (at your option) any later version. |
| 21 | * |
| 22 | * This program is distributed in the hope that it will be useful, |
| 23 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 24 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 25 | * GNU General Public License for more details. |
| 26 | * |
| 27 | * You should have received a copy of the GNU General Public License |
| 28 | * along with this program; if not, write to the Free Software |
| 29 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 30 | * MA 02111-1307 USA |
| 31 | */ |
| 32 | |
| 33 | #include <common.h> |
Liu Hui-R64343 | 447beb1 | 2011-01-03 22:27:39 +0000 | [diff] [blame] | 34 | #include <asm/arch/clock.h> |
Stefano Babic | 78129d9 | 2011-03-14 15:43:56 +0100 | [diff] [blame] | 35 | #include <asm/arch/imx-regs.h> |
Troy Kisky | 752ac8f | 2012-07-19 08:18:04 +0000 | [diff] [blame] | 36 | #include <asm/errno.h> |
Troy Kisky | 2254b7f | 2012-07-19 08:18:03 +0000 | [diff] [blame] | 37 | #include <asm/io.h> |
Marek Vasut | 5f1291e | 2011-10-26 00:05:44 +0000 | [diff] [blame] | 38 | #include <i2c.h> |
Troy Kisky | f024a3b | 2012-07-19 08:18:09 +0000 | [diff] [blame] | 39 | #include <watchdog.h> |
Sascha Hauer | 15ea70f | 2008-03-26 20:40:49 +0100 | [diff] [blame] | 40 | |
Marek Vasut | 94cb842 | 2011-09-22 09:22:12 +0000 | [diff] [blame] | 41 | struct mxc_i2c_regs { |
| 42 | uint32_t iadr; |
| 43 | uint32_t ifdr; |
| 44 | uint32_t i2cr; |
| 45 | uint32_t i2sr; |
| 46 | uint32_t i2dr; |
| 47 | }; |
Sascha Hauer | 15ea70f | 2008-03-26 20:40:49 +0100 | [diff] [blame] | 48 | |
| 49 | #define I2CR_IEN (1 << 7) |
| 50 | #define I2CR_IIEN (1 << 6) |
| 51 | #define I2CR_MSTA (1 << 5) |
| 52 | #define I2CR_MTX (1 << 4) |
| 53 | #define I2CR_TX_NO_AK (1 << 3) |
| 54 | #define I2CR_RSTA (1 << 2) |
| 55 | |
| 56 | #define I2SR_ICF (1 << 7) |
| 57 | #define I2SR_IBB (1 << 5) |
Troy Kisky | 8ff683a | 2012-07-19 08:18:15 +0000 | [diff] [blame] | 58 | #define I2SR_IAL (1 << 4) |
Sascha Hauer | 15ea70f | 2008-03-26 20:40:49 +0100 | [diff] [blame] | 59 | #define I2SR_IIF (1 << 1) |
| 60 | #define I2SR_RX_NO_AK (1 << 0) |
| 61 | |
Troy Kisky | 8462c63 | 2012-04-24 17:33:25 +0000 | [diff] [blame] | 62 | #ifdef CONFIG_SYS_I2C_BASE |
| 63 | #define I2C_BASE CONFIG_SYS_I2C_BASE |
Sascha Hauer | 15ea70f | 2008-03-26 20:40:49 +0100 | [diff] [blame] | 64 | #else |
Troy Kisky | 8462c63 | 2012-04-24 17:33:25 +0000 | [diff] [blame] | 65 | #error "define CONFIG_SYS_I2C_BASE to use the mxc_i2c driver" |
Sascha Hauer | 15ea70f | 2008-03-26 20:40:49 +0100 | [diff] [blame] | 66 | #endif |
| 67 | |
Marek Vasut | 94cb842 | 2011-09-22 09:22:12 +0000 | [diff] [blame] | 68 | static u16 i2c_clk_div[50][2] = { |
| 69 | { 22, 0x20 }, { 24, 0x21 }, { 26, 0x22 }, { 28, 0x23 }, |
| 70 | { 30, 0x00 }, { 32, 0x24 }, { 36, 0x25 }, { 40, 0x26 }, |
| 71 | { 42, 0x03 }, { 44, 0x27 }, { 48, 0x28 }, { 52, 0x05 }, |
| 72 | { 56, 0x29 }, { 60, 0x06 }, { 64, 0x2A }, { 72, 0x2B }, |
| 73 | { 80, 0x2C }, { 88, 0x09 }, { 96, 0x2D }, { 104, 0x0A }, |
| 74 | { 112, 0x2E }, { 128, 0x2F }, { 144, 0x0C }, { 160, 0x30 }, |
| 75 | { 192, 0x31 }, { 224, 0x32 }, { 240, 0x0F }, { 256, 0x33 }, |
| 76 | { 288, 0x10 }, { 320, 0x34 }, { 384, 0x35 }, { 448, 0x36 }, |
| 77 | { 480, 0x13 }, { 512, 0x37 }, { 576, 0x14 }, { 640, 0x38 }, |
| 78 | { 768, 0x39 }, { 896, 0x3A }, { 960, 0x17 }, { 1024, 0x3B }, |
| 79 | { 1152, 0x18 }, { 1280, 0x3C }, { 1536, 0x3D }, { 1792, 0x3E }, |
| 80 | { 1920, 0x1B }, { 2048, 0x3F }, { 2304, 0x1C }, { 2560, 0x1D }, |
| 81 | { 3072, 0x1E }, { 3840, 0x1F } |
| 82 | }; |
Sascha Hauer | 15ea70f | 2008-03-26 20:40:49 +0100 | [diff] [blame] | 83 | |
Marek Vasut | 94cb842 | 2011-09-22 09:22:12 +0000 | [diff] [blame] | 84 | /* |
| 85 | * Calculate and set proper clock divider |
| 86 | */ |
Marek Vasut | 5f1291e | 2011-10-26 00:05:44 +0000 | [diff] [blame] | 87 | static uint8_t i2c_imx_get_clk(unsigned int rate) |
Sascha Hauer | 15ea70f | 2008-03-26 20:40:49 +0100 | [diff] [blame] | 88 | { |
Marek Vasut | 94cb842 | 2011-09-22 09:22:12 +0000 | [diff] [blame] | 89 | unsigned int i2c_clk_rate; |
| 90 | unsigned int div; |
Marek Vasut | 5f1291e | 2011-10-26 00:05:44 +0000 | [diff] [blame] | 91 | u8 clk_div; |
Sascha Hauer | 15ea70f | 2008-03-26 20:40:49 +0100 | [diff] [blame] | 92 | |
Liu Hui-R64343 | 447beb1 | 2011-01-03 22:27:39 +0000 | [diff] [blame] | 93 | #if defined(CONFIG_MX31) |
Stefano Babic | 2212172 | 2011-01-20 07:50:44 +0000 | [diff] [blame] | 94 | struct clock_control_regs *sc_regs = |
| 95 | (struct clock_control_regs *)CCM_BASE; |
Marek Vasut | 94cb842 | 2011-09-22 09:22:12 +0000 | [diff] [blame] | 96 | |
Guennadi Liakhovetski | 3314fc6 | 2009-02-13 09:23:36 +0100 | [diff] [blame] | 97 | /* start the required I2C clock */ |
Troy Kisky | 8462c63 | 2012-04-24 17:33:25 +0000 | [diff] [blame] | 98 | writel(readl(&sc_regs->cgr0) | (3 << CONFIG_SYS_I2C_CLK_OFFSET), |
Stefano Babic | 2212172 | 2011-01-20 07:50:44 +0000 | [diff] [blame] | 99 | &sc_regs->cgr0); |
Liu Hui-R64343 | 447beb1 | 2011-01-03 22:27:39 +0000 | [diff] [blame] | 100 | #endif |
Guennadi Liakhovetski | 3314fc6 | 2009-02-13 09:23:36 +0100 | [diff] [blame] | 101 | |
Marek Vasut | 94cb842 | 2011-09-22 09:22:12 +0000 | [diff] [blame] | 102 | /* Divider value calculation */ |
| 103 | i2c_clk_rate = mxc_get_clock(MXC_IPG_PERCLK); |
| 104 | div = (i2c_clk_rate + rate - 1) / rate; |
| 105 | if (div < i2c_clk_div[0][0]) |
Marek Vasut | 4f27444 | 2011-09-27 06:34:11 +0000 | [diff] [blame] | 106 | clk_div = 0; |
Marek Vasut | 94cb842 | 2011-09-22 09:22:12 +0000 | [diff] [blame] | 107 | 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] | 108 | clk_div = ARRAY_SIZE(i2c_clk_div) - 1; |
Marek Vasut | 94cb842 | 2011-09-22 09:22:12 +0000 | [diff] [blame] | 109 | else |
Marek Vasut | 4f27444 | 2011-09-27 06:34:11 +0000 | [diff] [blame] | 110 | 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] | 111 | ; |
| 112 | |
| 113 | /* Store divider value */ |
Marek Vasut | 5f1291e | 2011-10-26 00:05:44 +0000 | [diff] [blame] | 114 | return clk_div; |
Marek Vasut | 94cb842 | 2011-09-22 09:22:12 +0000 | [diff] [blame] | 115 | } |
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 | /* |
Marek Vasut | 94cb842 | 2011-09-22 09:22:12 +0000 | [diff] [blame] | 118 | * Init I2C Bus |
| 119 | */ |
| 120 | void i2c_init(int speed, int unused) |
| 121 | { |
Marek Vasut | 5f1291e | 2011-10-26 00:05:44 +0000 | [diff] [blame] | 122 | struct mxc_i2c_regs *i2c_regs = (struct mxc_i2c_regs *)I2C_BASE; |
| 123 | u8 clk_idx = i2c_imx_get_clk(speed); |
| 124 | u8 idx = i2c_clk_div[clk_idx][1]; |
| 125 | |
| 126 | /* Store divider value */ |
| 127 | writeb(idx, &i2c_regs->ifdr); |
| 128 | |
Troy Kisky | e6fa4d7 | 2012-07-19 08:18:12 +0000 | [diff] [blame] | 129 | /* Reset module */ |
| 130 | writeb(0, &i2c_regs->i2cr); |
| 131 | writeb(0, &i2c_regs->i2sr); |
Sascha Hauer | 15ea70f | 2008-03-26 20:40:49 +0100 | [diff] [blame] | 132 | } |
| 133 | |
Marek Vasut | 94cb842 | 2011-09-22 09:22:12 +0000 | [diff] [blame] | 134 | /* |
Marek Vasut | 4f27444 | 2011-09-27 06:34:11 +0000 | [diff] [blame] | 135 | * Set I2C Speed |
| 136 | */ |
| 137 | int i2c_set_bus_speed(unsigned int speed) |
| 138 | { |
| 139 | i2c_init(speed, 0); |
| 140 | return 0; |
| 141 | } |
| 142 | |
| 143 | /* |
| 144 | * Get I2C Speed |
| 145 | */ |
| 146 | unsigned int i2c_get_bus_speed(void) |
| 147 | { |
Marek Vasut | 5f1291e | 2011-10-26 00:05:44 +0000 | [diff] [blame] | 148 | struct mxc_i2c_regs *i2c_regs = (struct mxc_i2c_regs *)I2C_BASE; |
| 149 | u8 clk_idx = readb(&i2c_regs->ifdr); |
| 150 | u8 clk_div; |
| 151 | |
| 152 | for (clk_div = 0; i2c_clk_div[clk_div][1] != clk_idx; clk_div++) |
| 153 | ; |
| 154 | |
Marek Vasut | 4f27444 | 2011-09-27 06:34:11 +0000 | [diff] [blame] | 155 | return mxc_get_clock(MXC_IPG_PERCLK) / i2c_clk_div[clk_div][0]; |
| 156 | } |
| 157 | |
Troy Kisky | f024a3b | 2012-07-19 08:18:09 +0000 | [diff] [blame] | 158 | #define ST_BUS_IDLE (0 | (I2SR_IBB << 8)) |
| 159 | #define ST_BUS_BUSY (I2SR_IBB | (I2SR_IBB << 8)) |
| 160 | #define ST_IIF (I2SR_IIF | (I2SR_IIF << 8)) |
Stefano Babic | 848bb99 | 2011-01-20 07:51:31 +0000 | [diff] [blame] | 161 | |
Troy Kisky | f024a3b | 2012-07-19 08:18:09 +0000 | [diff] [blame] | 162 | static int wait_for_sr_state(struct mxc_i2c_regs *i2c_regs, unsigned state) |
Sascha Hauer | 15ea70f | 2008-03-26 20:40:49 +0100 | [diff] [blame] | 163 | { |
Troy Kisky | f024a3b | 2012-07-19 08:18:09 +0000 | [diff] [blame] | 164 | unsigned sr; |
| 165 | ulong elapsed; |
| 166 | ulong start_time = get_timer(0); |
| 167 | for (;;) { |
| 168 | sr = readb(&i2c_regs->i2sr); |
Troy Kisky | 8ff683a | 2012-07-19 08:18:15 +0000 | [diff] [blame] | 169 | if (sr & I2SR_IAL) { |
| 170 | writeb(sr & ~I2SR_IAL, &i2c_regs->i2sr); |
| 171 | printf("%s: Arbitration lost sr=%x cr=%x state=%x\n", |
| 172 | __func__, sr, readb(&i2c_regs->i2cr), state); |
| 173 | return -ERESTART; |
| 174 | } |
Troy Kisky | f024a3b | 2012-07-19 08:18:09 +0000 | [diff] [blame] | 175 | if ((sr & (state >> 8)) == (unsigned char)state) |
| 176 | return sr; |
| 177 | WATCHDOG_RESET(); |
| 178 | elapsed = get_timer(start_time); |
| 179 | if (elapsed > (CONFIG_SYS_HZ / 10)) /* .1 seconds */ |
| 180 | break; |
Marek Vasut | 94cb842 | 2011-09-22 09:22:12 +0000 | [diff] [blame] | 181 | } |
Troy Kisky | f024a3b | 2012-07-19 08:18:09 +0000 | [diff] [blame] | 182 | printf("%s: failed sr=%x cr=%x state=%x\n", __func__, |
| 183 | sr, readb(&i2c_regs->i2cr), state); |
Troy Kisky | 752ac8f | 2012-07-19 08:18:04 +0000 | [diff] [blame] | 184 | return -ETIMEDOUT; |
Sascha Hauer | 15ea70f | 2008-03-26 20:40:49 +0100 | [diff] [blame] | 185 | } |
| 186 | |
Troy Kisky | 752ac8f | 2012-07-19 08:18:04 +0000 | [diff] [blame] | 187 | static int tx_byte(struct mxc_i2c_regs *i2c_regs, u8 byte) |
Stefano Babic | 848bb99 | 2011-01-20 07:51:31 +0000 | [diff] [blame] | 188 | { |
Troy Kisky | 752ac8f | 2012-07-19 08:18:04 +0000 | [diff] [blame] | 189 | int ret; |
Stefano Babic | 848bb99 | 2011-01-20 07:51:31 +0000 | [diff] [blame] | 190 | |
Troy Kisky | 30fa77c | 2012-07-19 08:18:05 +0000 | [diff] [blame] | 191 | writeb(0, &i2c_regs->i2sr); |
Troy Kisky | 752ac8f | 2012-07-19 08:18:04 +0000 | [diff] [blame] | 192 | writeb(byte, &i2c_regs->i2dr); |
Troy Kisky | f024a3b | 2012-07-19 08:18:09 +0000 | [diff] [blame] | 193 | ret = wait_for_sr_state(i2c_regs, ST_IIF); |
Troy Kisky | 752ac8f | 2012-07-19 08:18:04 +0000 | [diff] [blame] | 194 | if (ret < 0) |
| 195 | return ret; |
Troy Kisky | 752ac8f | 2012-07-19 08:18:04 +0000 | [diff] [blame] | 196 | if (ret & I2SR_RX_NO_AK) |
| 197 | return -ENODEV; |
| 198 | return 0; |
Marek Vasut | 94cb842 | 2011-09-22 09:22:12 +0000 | [diff] [blame] | 199 | } |
Stefano Babic | 848bb99 | 2011-01-20 07:51:31 +0000 | [diff] [blame] | 200 | |
Marek Vasut | 94cb842 | 2011-09-22 09:22:12 +0000 | [diff] [blame] | 201 | /* |
Troy Kisky | fef163f | 2012-07-19 08:18:13 +0000 | [diff] [blame] | 202 | * Stop I2C transaction |
Marek Vasut | 94cb842 | 2011-09-22 09:22:12 +0000 | [diff] [blame] | 203 | */ |
Troy Kisky | a2d15da | 2012-07-19 08:18:17 +0000 | [diff] [blame^] | 204 | static void i2c_imx_stop(struct mxc_i2c_regs *i2c_regs) |
Sascha Hauer | 15ea70f | 2008-03-26 20:40:49 +0100 | [diff] [blame] | 205 | { |
Troy Kisky | f024a3b | 2012-07-19 08:18:09 +0000 | [diff] [blame] | 206 | int ret; |
Troy Kisky | fef163f | 2012-07-19 08:18:13 +0000 | [diff] [blame] | 207 | unsigned int temp = readb(&i2c_regs->i2cr); |
Sascha Hauer | 15ea70f | 2008-03-26 20:40:49 +0100 | [diff] [blame] | 208 | |
Troy Kisky | 1ac1e45 | 2012-07-19 08:18:02 +0000 | [diff] [blame] | 209 | temp &= ~(I2CR_MSTA | I2CR_MTX); |
Marek Vasut | 94cb842 | 2011-09-22 09:22:12 +0000 | [diff] [blame] | 210 | writeb(temp, &i2c_regs->i2cr); |
Troy Kisky | f024a3b | 2012-07-19 08:18:09 +0000 | [diff] [blame] | 211 | ret = wait_for_sr_state(i2c_regs, ST_BUS_IDLE); |
| 212 | if (ret < 0) |
| 213 | printf("%s:trigger stop failed\n", __func__); |
Sascha Hauer | 15ea70f | 2008-03-26 20:40:49 +0100 | [diff] [blame] | 214 | } |
| 215 | |
Marek Vasut | 94cb842 | 2011-09-22 09:22:12 +0000 | [diff] [blame] | 216 | /* |
Troy Kisky | 14db6f2 | 2012-07-19 08:18:06 +0000 | [diff] [blame] | 217 | * Send start signal, chip address and |
| 218 | * write register address |
Marek Vasut | 94cb842 | 2011-09-22 09:22:12 +0000 | [diff] [blame] | 219 | */ |
Troy Kisky | eca037a | 2012-07-19 08:18:16 +0000 | [diff] [blame] | 220 | static int i2c_init_transfer_(struct mxc_i2c_regs *i2c_regs, |
Troy Kisky | 14db6f2 | 2012-07-19 08:18:06 +0000 | [diff] [blame] | 221 | uchar chip, uint addr, int alen) |
Sascha Hauer | 15ea70f | 2008-03-26 20:40:49 +0100 | [diff] [blame] | 222 | { |
Troy Kisky | a974bcc | 2012-07-19 08:18:11 +0000 | [diff] [blame] | 223 | unsigned int temp; |
| 224 | int ret; |
| 225 | |
| 226 | /* Enable I2C controller */ |
Troy Kisky | fef163f | 2012-07-19 08:18:13 +0000 | [diff] [blame] | 227 | if (!(readb(&i2c_regs->i2cr) & I2CR_IEN)) { |
| 228 | writeb(I2CR_IEN, &i2c_regs->i2cr); |
| 229 | /* Wait for controller to be stable */ |
| 230 | udelay(50); |
| 231 | } |
Troy Kisky | e203df3 | 2012-07-19 08:18:14 +0000 | [diff] [blame] | 232 | if (readb(&i2c_regs->iadr) == (chip << 1)) |
| 233 | writeb((chip << 1) ^ 2, &i2c_regs->iadr); |
Troy Kisky | a974bcc | 2012-07-19 08:18:11 +0000 | [diff] [blame] | 234 | writeb(0, &i2c_regs->i2sr); |
Troy Kisky | fef163f | 2012-07-19 08:18:13 +0000 | [diff] [blame] | 235 | ret = wait_for_sr_state(i2c_regs, ST_BUS_IDLE); |
| 236 | if (ret < 0) |
Troy Kisky | eca037a | 2012-07-19 08:18:16 +0000 | [diff] [blame] | 237 | return ret; |
Troy Kisky | a974bcc | 2012-07-19 08:18:11 +0000 | [diff] [blame] | 238 | |
| 239 | /* Start I2C transaction */ |
| 240 | temp = readb(&i2c_regs->i2cr); |
| 241 | temp |= I2CR_MSTA; |
| 242 | writeb(temp, &i2c_regs->i2cr); |
| 243 | |
| 244 | ret = wait_for_sr_state(i2c_regs, ST_BUS_BUSY); |
| 245 | if (ret < 0) |
Troy Kisky | eca037a | 2012-07-19 08:18:16 +0000 | [diff] [blame] | 246 | return ret; |
Troy Kisky | 14db6f2 | 2012-07-19 08:18:06 +0000 | [diff] [blame] | 247 | |
Troy Kisky | a974bcc | 2012-07-19 08:18:11 +0000 | [diff] [blame] | 248 | temp |= I2CR_MTX | I2CR_TX_NO_AK; |
| 249 | writeb(temp, &i2c_regs->i2cr); |
| 250 | |
Troy Kisky | 14db6f2 | 2012-07-19 08:18:06 +0000 | [diff] [blame] | 251 | /* write slave address */ |
| 252 | ret = tx_byte(i2c_regs, chip << 1); |
| 253 | if (ret < 0) |
Troy Kisky | eca037a | 2012-07-19 08:18:16 +0000 | [diff] [blame] | 254 | return ret; |
Marek Vasut | 94cb842 | 2011-09-22 09:22:12 +0000 | [diff] [blame] | 255 | |
Marek Vasut | 5f1291e | 2011-10-26 00:05:44 +0000 | [diff] [blame] | 256 | while (alen--) { |
Troy Kisky | 752ac8f | 2012-07-19 08:18:04 +0000 | [diff] [blame] | 257 | ret = tx_byte(i2c_regs, (addr >> (alen * 8)) & 0xff); |
| 258 | if (ret < 0) |
Troy Kisky | eca037a | 2012-07-19 08:18:16 +0000 | [diff] [blame] | 259 | return ret; |
Stefano Babic | 848bb99 | 2011-01-20 07:51:31 +0000 | [diff] [blame] | 260 | } |
Troy Kisky | 14db6f2 | 2012-07-19 08:18:06 +0000 | [diff] [blame] | 261 | return 0; |
Troy Kisky | eca037a | 2012-07-19 08:18:16 +0000 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | static int i2c_init_transfer(struct mxc_i2c_regs *i2c_regs, |
| 265 | uchar chip, uint addr, int alen) |
| 266 | { |
| 267 | int retry; |
| 268 | int ret; |
| 269 | for (retry = 0; retry < 3; retry++) { |
| 270 | ret = i2c_init_transfer_(i2c_regs, chip, addr, alen); |
| 271 | if (ret >= 0) |
| 272 | return 0; |
Troy Kisky | a2d15da | 2012-07-19 08:18:17 +0000 | [diff] [blame^] | 273 | i2c_imx_stop(i2c_regs); |
Troy Kisky | eca037a | 2012-07-19 08:18:16 +0000 | [diff] [blame] | 274 | if (ret == -ENODEV) |
| 275 | return ret; |
| 276 | |
| 277 | printf("%s: failed for chip 0x%x retry=%d\n", __func__, chip, |
| 278 | retry); |
| 279 | if (ret != -ERESTART) |
| 280 | writeb(0, &i2c_regs->i2cr); /* Disable controller */ |
| 281 | udelay(100); |
| 282 | } |
| 283 | printf("%s: give up i2c_regs=%p\n", __func__, i2c_regs); |
Marek Vasut | 94cb842 | 2011-09-22 09:22:12 +0000 | [diff] [blame] | 284 | return ret; |
Sascha Hauer | 15ea70f | 2008-03-26 20:40:49 +0100 | [diff] [blame] | 285 | } |
| 286 | |
Marek Vasut | 94cb842 | 2011-09-22 09:22:12 +0000 | [diff] [blame] | 287 | /* |
Marek Vasut | 94cb842 | 2011-09-22 09:22:12 +0000 | [diff] [blame] | 288 | * Read data from I2C device |
| 289 | */ |
Sascha Hauer | 15ea70f | 2008-03-26 20:40:49 +0100 | [diff] [blame] | 290 | int i2c_read(uchar chip, uint addr, int alen, uchar *buf, int len) |
| 291 | { |
Marek Vasut | 94cb842 | 2011-09-22 09:22:12 +0000 | [diff] [blame] | 292 | struct mxc_i2c_regs *i2c_regs = (struct mxc_i2c_regs *)I2C_BASE; |
Sascha Hauer | 15ea70f | 2008-03-26 20:40:49 +0100 | [diff] [blame] | 293 | int ret; |
Marek Vasut | 94cb842 | 2011-09-22 09:22:12 +0000 | [diff] [blame] | 294 | unsigned int temp; |
| 295 | int i; |
Sascha Hauer | 15ea70f | 2008-03-26 20:40:49 +0100 | [diff] [blame] | 296 | |
Troy Kisky | 14db6f2 | 2012-07-19 08:18:06 +0000 | [diff] [blame] | 297 | ret = i2c_init_transfer(i2c_regs, chip, addr, alen); |
Troy Kisky | 752ac8f | 2012-07-19 08:18:04 +0000 | [diff] [blame] | 298 | if (ret < 0) |
Marek Vasut | 94cb842 | 2011-09-22 09:22:12 +0000 | [diff] [blame] | 299 | return ret; |
Sascha Hauer | 15ea70f | 2008-03-26 20:40:49 +0100 | [diff] [blame] | 300 | |
Marek Vasut | 94cb842 | 2011-09-22 09:22:12 +0000 | [diff] [blame] | 301 | temp = readb(&i2c_regs->i2cr); |
| 302 | temp |= I2CR_RSTA; |
| 303 | writeb(temp, &i2c_regs->i2cr); |
Sascha Hauer | 15ea70f | 2008-03-26 20:40:49 +0100 | [diff] [blame] | 304 | |
Troy Kisky | 752ac8f | 2012-07-19 08:18:04 +0000 | [diff] [blame] | 305 | ret = tx_byte(i2c_regs, (chip << 1) | 1); |
Troy Kisky | 0ce898d | 2012-07-19 08:18:07 +0000 | [diff] [blame] | 306 | if (ret < 0) { |
Troy Kisky | a2d15da | 2012-07-19 08:18:17 +0000 | [diff] [blame^] | 307 | i2c_imx_stop(i2c_regs); |
Marek Vasut | 94cb842 | 2011-09-22 09:22:12 +0000 | [diff] [blame] | 308 | return ret; |
Troy Kisky | 0ce898d | 2012-07-19 08:18:07 +0000 | [diff] [blame] | 309 | } |
Sascha Hauer | 15ea70f | 2008-03-26 20:40:49 +0100 | [diff] [blame] | 310 | |
Marek Vasut | 94cb842 | 2011-09-22 09:22:12 +0000 | [diff] [blame] | 311 | /* setup bus to read data */ |
| 312 | temp = readb(&i2c_regs->i2cr); |
| 313 | temp &= ~(I2CR_MTX | I2CR_TX_NO_AK); |
| 314 | if (len == 1) |
| 315 | temp |= I2CR_TX_NO_AK; |
| 316 | writeb(temp, &i2c_regs->i2cr); |
Troy Kisky | 30fa77c | 2012-07-19 08:18:05 +0000 | [diff] [blame] | 317 | writeb(0, &i2c_regs->i2sr); |
| 318 | readb(&i2c_regs->i2dr); /* dummy read to clear ICF */ |
Sascha Hauer | 15ea70f | 2008-03-26 20:40:49 +0100 | [diff] [blame] | 319 | |
Marek Vasut | 94cb842 | 2011-09-22 09:22:12 +0000 | [diff] [blame] | 320 | /* read data */ |
| 321 | for (i = 0; i < len; i++) { |
Troy Kisky | f024a3b | 2012-07-19 08:18:09 +0000 | [diff] [blame] | 322 | ret = wait_for_sr_state(i2c_regs, ST_IIF); |
| 323 | if (ret < 0) { |
Troy Kisky | a2d15da | 2012-07-19 08:18:17 +0000 | [diff] [blame^] | 324 | i2c_imx_stop(i2c_regs); |
Marek Vasut | 94cb842 | 2011-09-22 09:22:12 +0000 | [diff] [blame] | 325 | return ret; |
Troy Kisky | 0ce898d | 2012-07-19 08:18:07 +0000 | [diff] [blame] | 326 | } |
Sascha Hauer | 15ea70f | 2008-03-26 20:40:49 +0100 | [diff] [blame] | 327 | |
Marek Vasut | 94cb842 | 2011-09-22 09:22:12 +0000 | [diff] [blame] | 328 | /* |
| 329 | * It must generate STOP before read I2DR to prevent |
| 330 | * controller from generating another clock cycle |
| 331 | */ |
| 332 | if (i == (len - 1)) { |
Troy Kisky | a2d15da | 2012-07-19 08:18:17 +0000 | [diff] [blame^] | 333 | i2c_imx_stop(i2c_regs); |
Marek Vasut | 94cb842 | 2011-09-22 09:22:12 +0000 | [diff] [blame] | 334 | } else if (i == (len - 2)) { |
| 335 | temp = readb(&i2c_regs->i2cr); |
| 336 | temp |= I2CR_TX_NO_AK; |
| 337 | writeb(temp, &i2c_regs->i2cr); |
| 338 | } |
Troy Kisky | 30fa77c | 2012-07-19 08:18:05 +0000 | [diff] [blame] | 339 | writeb(0, &i2c_regs->i2sr); |
Marek Vasut | 94cb842 | 2011-09-22 09:22:12 +0000 | [diff] [blame] | 340 | buf[i] = readb(&i2c_regs->i2dr); |
| 341 | } |
Troy Kisky | a2d15da | 2012-07-19 08:18:17 +0000 | [diff] [blame^] | 342 | i2c_imx_stop(i2c_regs); |
Troy Kisky | f024a3b | 2012-07-19 08:18:09 +0000 | [diff] [blame] | 343 | return 0; |
Sascha Hauer | 15ea70f | 2008-03-26 20:40:49 +0100 | [diff] [blame] | 344 | } |
| 345 | |
Marek Vasut | 94cb842 | 2011-09-22 09:22:12 +0000 | [diff] [blame] | 346 | /* |
| 347 | * Write data to I2C device |
| 348 | */ |
Sascha Hauer | 15ea70f | 2008-03-26 20:40:49 +0100 | [diff] [blame] | 349 | int i2c_write(uchar chip, uint addr, int alen, uchar *buf, int len) |
| 350 | { |
Marek Vasut | 94cb842 | 2011-09-22 09:22:12 +0000 | [diff] [blame] | 351 | struct mxc_i2c_regs *i2c_regs = (struct mxc_i2c_regs *)I2C_BASE; |
| 352 | int ret; |
| 353 | int i; |
Sascha Hauer | 15ea70f | 2008-03-26 20:40:49 +0100 | [diff] [blame] | 354 | |
Troy Kisky | 14db6f2 | 2012-07-19 08:18:06 +0000 | [diff] [blame] | 355 | ret = i2c_init_transfer(i2c_regs, chip, addr, alen); |
Troy Kisky | 752ac8f | 2012-07-19 08:18:04 +0000 | [diff] [blame] | 356 | if (ret < 0) |
Marek Vasut | 94cb842 | 2011-09-22 09:22:12 +0000 | [diff] [blame] | 357 | return ret; |
Sascha Hauer | 15ea70f | 2008-03-26 20:40:49 +0100 | [diff] [blame] | 358 | |
Marek Vasut | 94cb842 | 2011-09-22 09:22:12 +0000 | [diff] [blame] | 359 | for (i = 0; i < len; i++) { |
Troy Kisky | 752ac8f | 2012-07-19 08:18:04 +0000 | [diff] [blame] | 360 | ret = tx_byte(i2c_regs, buf[i]); |
| 361 | if (ret < 0) |
Troy Kisky | 0ce898d | 2012-07-19 08:18:07 +0000 | [diff] [blame] | 362 | break; |
Marek Vasut | 94cb842 | 2011-09-22 09:22:12 +0000 | [diff] [blame] | 363 | } |
Troy Kisky | a2d15da | 2012-07-19 08:18:17 +0000 | [diff] [blame^] | 364 | i2c_imx_stop(i2c_regs); |
Marek Vasut | 94cb842 | 2011-09-22 09:22:12 +0000 | [diff] [blame] | 365 | return ret; |
| 366 | } |
Troy Kisky | 321a42b | 2012-07-19 08:18:08 +0000 | [diff] [blame] | 367 | |
| 368 | /* |
| 369 | * Test if a chip at a given address responds (probe the chip) |
| 370 | */ |
| 371 | int i2c_probe(uchar chip) |
| 372 | { |
| 373 | return i2c_write(chip, 0, 0, NULL, 0); |
| 374 | } |