Jon Loeliger | e4773be | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 1 | /* |
Timur Tabi | 2165c62 | 2009-09-04 16:28:35 -0500 | [diff] [blame] | 2 | * Copyright 2006,2009 Freescale Semiconductor, Inc. |
Jon Loeliger | e4773be | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 3 | * |
Heiko Schocher | f285074 | 2012-10-24 13:48:22 +0200 | [diff] [blame] | 4 | * 2012, Heiko Schocher, DENX Software Engineering, hs@denx.de. |
| 5 | * Changes for multibus/multiadapter I2C support. |
| 6 | * |
Jon Loeliger | e4773be | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 7 | * This program is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU General Public License |
| 9 | * Version 2 as published by the Free Software Foundation. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 19 | * MA 02111-1307 USA |
| 20 | */ |
| 21 | |
Jon Loeliger | e4773be | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 22 | #include <common.h> |
Jon Loeliger | 24df977 | 2006-10-19 12:02:24 -0500 | [diff] [blame] | 23 | #include <command.h> |
Jon Loeliger | 43d818f | 2006-10-20 15:50:15 -0500 | [diff] [blame] | 24 | #include <i2c.h> /* Functional interface */ |
Jon Loeliger | e4773be | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 25 | #include <asm/io.h> |
Jon Loeliger | 43d818f | 2006-10-20 15:50:15 -0500 | [diff] [blame] | 26 | #include <asm/fsl_i2c.h> /* HW definitions */ |
Jon Loeliger | e4773be | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 27 | |
Timur Tabi | 2165c62 | 2009-09-04 16:28:35 -0500 | [diff] [blame] | 28 | /* The maximum number of microseconds we will wait until another master has |
| 29 | * released the bus. If not defined in the board header file, then use a |
| 30 | * generic value. |
| 31 | */ |
| 32 | #ifndef CONFIG_I2C_MBB_TIMEOUT |
| 33 | #define CONFIG_I2C_MBB_TIMEOUT 100000 |
| 34 | #endif |
| 35 | |
| 36 | /* The maximum number of microseconds we will wait for a read or write |
| 37 | * operation to complete. If not defined in the board header file, then use a |
| 38 | * generic value. |
| 39 | */ |
| 40 | #ifndef CONFIG_I2C_TIMEOUT |
| 41 | #define CONFIG_I2C_TIMEOUT 10000 |
| 42 | #endif |
Jon Loeliger | e4773be | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 43 | |
Joakim Tjernlund | c32c5f7 | 2006-11-28 16:17:27 -0600 | [diff] [blame] | 44 | #define I2C_READ_BIT 1 |
| 45 | #define I2C_WRITE_BIT 0 |
| 46 | |
Timur Tabi | b301fda | 2008-03-14 17:45:29 -0500 | [diff] [blame] | 47 | DECLARE_GLOBAL_DATA_PTR; |
| 48 | |
Timur Tabi | b301fda | 2008-03-14 17:45:29 -0500 | [diff] [blame] | 49 | static const struct fsl_i2c *i2c_dev[2] = { |
Heiko Schocher | f285074 | 2012-10-24 13:48:22 +0200 | [diff] [blame] | 50 | (struct fsl_i2c *)(CONFIG_SYS_IMMR + CONFIG_SYS_FSL_I2C_OFFSET), |
| 51 | #ifdef CONFIG_SYS_FSL_I2C2_OFFSET |
| 52 | (struct fsl_i2c *)(CONFIG_SYS_IMMR + CONFIG_SYS_FSL_I2C2_OFFSET) |
Timur Tabi | ab34754 | 2006-11-03 19:15:00 -0600 | [diff] [blame] | 53 | #endif |
| 54 | }; |
Jon Loeliger | e4773be | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 55 | |
Timur Tabi | b301fda | 2008-03-14 17:45:29 -0500 | [diff] [blame] | 56 | /* I2C speed map for a DFSR value of 1 */ |
| 57 | |
| 58 | /* |
| 59 | * Map I2C frequency dividers to FDR and DFSR values |
| 60 | * |
| 61 | * This structure is used to define the elements of a table that maps I2C |
| 62 | * frequency divider (I2C clock rate divided by I2C bus speed) to a value to be |
| 63 | * programmed into the Frequency Divider Ratio (FDR) and Digital Filter |
| 64 | * Sampling Rate (DFSR) registers. |
| 65 | * |
| 66 | * The actual table should be defined in the board file, and it must be called |
| 67 | * fsl_i2c_speed_map[]. |
| 68 | * |
| 69 | * The last entry of the table must have a value of {-1, X}, where X is same |
| 70 | * FDR/DFSR values as the second-to-last entry. This guarantees that any |
| 71 | * search through the array will always find a match. |
| 72 | * |
| 73 | * The values of the divider must be in increasing numerical order, i.e. |
| 74 | * fsl_i2c_speed_map[x+1].divider > fsl_i2c_speed_map[x].divider. |
| 75 | * |
| 76 | * For this table, the values are based on a value of 1 for the DFSR |
| 77 | * register. See the application note AN2919 "Determining the I2C Frequency |
| 78 | * Divider Ratio for SCL" |
TsiChung Liew | 00648a7 | 2008-08-19 00:56:46 +0600 | [diff] [blame] | 79 | * |
| 80 | * ColdFire I2C frequency dividers for FDR values are different from |
| 81 | * PowerPC. The protocol to use the I2C module is still the same. |
| 82 | * A different table is defined and are based on MCF5xxx user manual. |
| 83 | * |
Timur Tabi | b301fda | 2008-03-14 17:45:29 -0500 | [diff] [blame] | 84 | */ |
| 85 | static const struct { |
| 86 | unsigned short divider; |
Timur Tabi | b301fda | 2008-03-14 17:45:29 -0500 | [diff] [blame] | 87 | u8 fdr; |
| 88 | } fsl_i2c_speed_map[] = { |
Joakim Tjernlund | 5a6e061 | 2009-09-17 11:07:17 +0200 | [diff] [blame] | 89 | #ifdef __M68K__ |
TsiChung Liew | 00648a7 | 2008-08-19 00:56:46 +0600 | [diff] [blame] | 90 | {20, 32}, {22, 33}, {24, 34}, {26, 35}, |
| 91 | {28, 0}, {28, 36}, {30, 1}, {32, 37}, |
| 92 | {34, 2}, {36, 38}, {40, 3}, {40, 39}, |
| 93 | {44, 4}, {48, 5}, {48, 40}, {56, 6}, |
| 94 | {56, 41}, {64, 42}, {68, 7}, {72, 43}, |
| 95 | {80, 8}, {80, 44}, {88, 9}, {96, 41}, |
| 96 | {104, 10}, {112, 42}, {128, 11}, {128, 43}, |
| 97 | {144, 12}, {160, 13}, {160, 48}, {192, 14}, |
| 98 | {192, 49}, {224, 50}, {240, 15}, {256, 51}, |
| 99 | {288, 16}, {320, 17}, {320, 52}, {384, 18}, |
| 100 | {384, 53}, {448, 54}, {480, 19}, {512, 55}, |
| 101 | {576, 20}, {640, 21}, {640, 56}, {768, 22}, |
| 102 | {768, 57}, {960, 23}, {896, 58}, {1024, 59}, |
| 103 | {1152, 24}, {1280, 25}, {1280, 60}, {1536, 26}, |
| 104 | {1536, 61}, {1792, 62}, {1920, 27}, {2048, 63}, |
| 105 | {2304, 28}, {2560, 29}, {3072, 30}, {3840, 31}, |
| 106 | {-1, 31} |
| 107 | #endif |
Timur Tabi | b301fda | 2008-03-14 17:45:29 -0500 | [diff] [blame] | 108 | }; |
| 109 | |
| 110 | /** |
| 111 | * Set the I2C bus speed for a given I2C device |
| 112 | * |
| 113 | * @param dev: the I2C device |
| 114 | * @i2c_clk: I2C bus clock frequency |
| 115 | * @speed: the desired speed of the bus |
| 116 | * |
| 117 | * The I2C device must be stopped before calling this function. |
| 118 | * |
| 119 | * The return value is the actual bus speed that is set. |
| 120 | */ |
| 121 | static unsigned int set_i2c_bus_speed(const struct fsl_i2c *dev, |
| 122 | unsigned int i2c_clk, unsigned int speed) |
| 123 | { |
| 124 | unsigned short divider = min(i2c_clk / speed, (unsigned short) -1); |
Timur Tabi | b301fda | 2008-03-14 17:45:29 -0500 | [diff] [blame] | 125 | |
| 126 | /* |
| 127 | * We want to choose an FDR/DFSR that generates an I2C bus speed that |
| 128 | * is equal to or lower than the requested speed. That means that we |
| 129 | * want the first divider that is equal to or greater than the |
| 130 | * calculated divider. |
| 131 | */ |
TsiChung Liew | 00648a7 | 2008-08-19 00:56:46 +0600 | [diff] [blame] | 132 | #ifdef __PPC__ |
Joakim Tjernlund | 5a6e061 | 2009-09-17 11:07:17 +0200 | [diff] [blame] | 133 | u8 dfsr, fdr = 0x31; /* Default if no FDR found */ |
| 134 | /* a, b and dfsr matches identifiers A,B and C respectively in AN2919 */ |
| 135 | unsigned short a, b, ga, gb; |
| 136 | unsigned long c_div, est_div; |
| 137 | |
Joakim Tjernlund | e677e70 | 2009-09-17 11:07:16 +0200 | [diff] [blame] | 138 | #ifdef CONFIG_FSL_I2C_CUSTOM_DFSR |
Joakim Tjernlund | 5a6e061 | 2009-09-17 11:07:17 +0200 | [diff] [blame] | 139 | dfsr = CONFIG_FSL_I2C_CUSTOM_DFSR; |
Joakim Tjernlund | e677e70 | 2009-09-17 11:07:16 +0200 | [diff] [blame] | 140 | #else |
Joakim Tjernlund | 5a6e061 | 2009-09-17 11:07:17 +0200 | [diff] [blame] | 141 | /* Condition 1: dfsr <= 50/T */ |
| 142 | dfsr = (5 * (i2c_clk / 1000)) / 100000; |
Joakim Tjernlund | e677e70 | 2009-09-17 11:07:16 +0200 | [diff] [blame] | 143 | #endif |
| 144 | #ifdef CONFIG_FSL_I2C_CUSTOM_FDR |
Joakim Tjernlund | 5a6e061 | 2009-09-17 11:07:17 +0200 | [diff] [blame] | 145 | fdr = CONFIG_FSL_I2C_CUSTOM_FDR; |
| 146 | speed = i2c_clk / divider; /* Fake something */ |
| 147 | #else |
| 148 | debug("Requested speed:%d, i2c_clk:%d\n", speed, i2c_clk); |
| 149 | if (!dfsr) |
| 150 | dfsr = 1; |
| 151 | |
| 152 | est_div = ~0; |
| 153 | for (ga = 0x4, a = 10; a <= 30; ga++, a += 2) { |
| 154 | for (gb = 0; gb < 8; gb++) { |
| 155 | b = 16 << gb; |
| 156 | c_div = b * (a + ((3*dfsr)/b)*2); |
| 157 | if ((c_div > divider) && (c_div < est_div)) { |
| 158 | unsigned short bin_gb, bin_ga; |
| 159 | |
| 160 | est_div = c_div; |
| 161 | bin_gb = gb << 2; |
| 162 | bin_ga = (ga & 0x3) | ((ga & 0x4) << 3); |
| 163 | fdr = bin_gb | bin_ga; |
| 164 | speed = i2c_clk / est_div; |
| 165 | debug("FDR:0x%.2x, div:%ld, ga:0x%x, gb:0x%x, " |
| 166 | "a:%d, b:%d, speed:%d\n", |
| 167 | fdr, est_div, ga, gb, a, b, speed); |
| 168 | /* Condition 2 not accounted for */ |
| 169 | debug("Tr <= %d ns\n", |
| 170 | (b - 3 * dfsr) * 1000000 / |
| 171 | (i2c_clk / 1000)); |
| 172 | } |
| 173 | } |
| 174 | if (a == 20) |
| 175 | a += 2; |
| 176 | if (a == 24) |
| 177 | a += 4; |
| 178 | } |
| 179 | debug("divider:%d, est_div:%ld, DFSR:%d\n", divider, est_div, dfsr); |
| 180 | debug("FDR:0x%.2x, speed:%d\n", fdr, speed); |
| 181 | #endif |
| 182 | writeb(dfsr, &dev->dfsrr); /* set default filter */ |
| 183 | writeb(fdr, &dev->fdr); /* set bus speed */ |
Joakim Tjernlund | e677e70 | 2009-09-17 11:07:16 +0200 | [diff] [blame] | 184 | #else |
Joakim Tjernlund | 5a6e061 | 2009-09-17 11:07:17 +0200 | [diff] [blame] | 185 | unsigned int i; |
| 186 | |
| 187 | for (i = 0; i < ARRAY_SIZE(fsl_i2c_speed_map); i++) |
| 188 | if (fsl_i2c_speed_map[i].divider >= divider) { |
| 189 | u8 fdr; |
| 190 | |
Timur Tabi | b301fda | 2008-03-14 17:45:29 -0500 | [diff] [blame] | 191 | fdr = fsl_i2c_speed_map[i].fdr; |
| 192 | speed = i2c_clk / fsl_i2c_speed_map[i].divider; |
Joakim Tjernlund | e677e70 | 2009-09-17 11:07:16 +0200 | [diff] [blame] | 193 | writeb(fdr, &dev->fdr); /* set bus speed */ |
| 194 | |
Timur Tabi | b301fda | 2008-03-14 17:45:29 -0500 | [diff] [blame] | 195 | break; |
| 196 | } |
Joakim Tjernlund | 5a6e061 | 2009-09-17 11:07:17 +0200 | [diff] [blame] | 197 | #endif |
Timur Tabi | b301fda | 2008-03-14 17:45:29 -0500 | [diff] [blame] | 198 | return speed; |
| 199 | } |
| 200 | |
Kim Phillips | dd01863 | 2012-10-16 14:28:43 +0000 | [diff] [blame] | 201 | static unsigned int get_i2c_clock(int bus) |
Jerry Huang | 5e01561 | 2011-10-26 15:29:38 +0000 | [diff] [blame] | 202 | { |
| 203 | if (bus) |
Simon Glass | c2baaec | 2012-12-13 20:48:49 +0000 | [diff] [blame] | 204 | return gd->arch.i2c2_clk; /* I2C2 clock */ |
Jerry Huang | 5e01561 | 2011-10-26 15:29:38 +0000 | [diff] [blame] | 205 | else |
Simon Glass | c2baaec | 2012-12-13 20:48:49 +0000 | [diff] [blame] | 206 | return gd->arch.i2c1_clk; /* I2C1 clock */ |
Jerry Huang | 5e01561 | 2011-10-26 15:29:38 +0000 | [diff] [blame] | 207 | } |
| 208 | |
Chunhe Lan | 2e13d57 | 2013-08-16 15:10:36 +0800 | [diff] [blame] | 209 | static int fsl_i2c_fixup(const struct fsl_i2c *dev) |
| 210 | { |
| 211 | const unsigned long long timeout = usec2ticks(CONFIG_I2C_MBB_TIMEOUT); |
| 212 | unsigned long long timeval = 0; |
| 213 | int ret = -1; |
Chunhe Lan | 9254640 | 2013-08-16 15:10:37 +0800 | [diff] [blame] | 214 | unsigned int flags = 0; |
| 215 | |
| 216 | #ifdef CONFIG_SYS_FSL_ERRATUM_I2C_A004447 |
| 217 | unsigned int svr = get_svr(); |
| 218 | if ((SVR_SOC_VER(svr) == SVR_8548 && IS_SVR_REV(svr, 3, 1)) || |
| 219 | (SVR_REV(svr) <= CONFIG_SYS_FSL_A004447_SVR_REV)) |
| 220 | flags = I2C_CR_BIT6; |
| 221 | #endif |
Chunhe Lan | 2e13d57 | 2013-08-16 15:10:36 +0800 | [diff] [blame] | 222 | |
| 223 | writeb(I2C_CR_MEN | I2C_CR_MSTA, &dev->cr); |
| 224 | |
| 225 | timeval = get_ticks(); |
| 226 | while (!(readb(&dev->sr) & I2C_SR_MBB)) { |
| 227 | if ((get_ticks() - timeval) > timeout) |
| 228 | goto err; |
| 229 | } |
| 230 | |
| 231 | if (readb(&dev->sr) & I2C_SR_MAL) { |
| 232 | /* SDA is stuck low */ |
| 233 | writeb(0, &dev->cr); |
| 234 | udelay(100); |
Chunhe Lan | 9254640 | 2013-08-16 15:10:37 +0800 | [diff] [blame] | 235 | writeb(I2C_CR_MSTA | flags, &dev->cr); |
| 236 | writeb(I2C_CR_MEN | I2C_CR_MSTA | flags, &dev->cr); |
Chunhe Lan | 2e13d57 | 2013-08-16 15:10:36 +0800 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | readb(&dev->dr); |
| 240 | |
| 241 | timeval = get_ticks(); |
| 242 | while (!(readb(&dev->sr) & I2C_SR_MIF)) { |
| 243 | if ((get_ticks() - timeval) > timeout) |
| 244 | goto err; |
| 245 | } |
| 246 | ret = 0; |
| 247 | |
| 248 | err: |
Chunhe Lan | 9254640 | 2013-08-16 15:10:37 +0800 | [diff] [blame] | 249 | writeb(I2C_CR_MEN | flags, &dev->cr); |
Chunhe Lan | 2e13d57 | 2013-08-16 15:10:36 +0800 | [diff] [blame] | 250 | writeb(0, &dev->sr); |
| 251 | udelay(100); |
| 252 | |
| 253 | return ret; |
| 254 | } |
| 255 | |
Heiko Schocher | f285074 | 2012-10-24 13:48:22 +0200 | [diff] [blame] | 256 | static void fsl_i2c_init(struct i2c_adapter *adap, int speed, int slaveadd) |
Jon Loeliger | e4773be | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 257 | { |
Kumar Gala | 4e506ca | 2011-11-08 03:39:43 +0000 | [diff] [blame] | 258 | const struct fsl_i2c *dev; |
Chunhe Lan | 2e13d57 | 2013-08-16 15:10:36 +0800 | [diff] [blame] | 259 | const unsigned long long timeout = usec2ticks(CONFIG_I2C_MBB_TIMEOUT); |
| 260 | unsigned long long timeval; |
Jon Loeliger | e4773be | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 261 | |
Heiko Schocher | c5ca01f | 2009-07-09 12:04:26 +0200 | [diff] [blame] | 262 | #ifdef CONFIG_SYS_I2C_INIT_BOARD |
Richard Retanubun | df0149c | 2010-04-12 15:08:17 -0400 | [diff] [blame] | 263 | /* Call board specific i2c bus reset routine before accessing the |
| 264 | * environment, which might be in a chip on that bus. For details |
| 265 | * about this problem see doc/I2C_Edge_Conditions. |
| 266 | */ |
Heiko Schocher | c5ca01f | 2009-07-09 12:04:26 +0200 | [diff] [blame] | 267 | i2c_init_board(); |
| 268 | #endif |
Heiko Schocher | f285074 | 2012-10-24 13:48:22 +0200 | [diff] [blame] | 269 | dev = (struct fsl_i2c *)i2c_dev[adap->hwadapnr]; |
Jerry Huang | 5e01561 | 2011-10-26 15:29:38 +0000 | [diff] [blame] | 270 | |
Heiko Schocher | f285074 | 2012-10-24 13:48:22 +0200 | [diff] [blame] | 271 | writeb(0, &dev->cr); /* stop I2C controller */ |
| 272 | udelay(5); /* let it shutdown in peace */ |
| 273 | set_i2c_bus_speed(dev, get_i2c_clock(adap->hwadapnr), speed); |
| 274 | writeb(slaveadd << 1, &dev->adr);/* write slave address */ |
| 275 | writeb(0x0, &dev->sr); /* clear status register */ |
| 276 | writeb(I2C_CR_MEN, &dev->cr); /* start I2C controller */ |
Richard Retanubun | df0149c | 2010-04-12 15:08:17 -0400 | [diff] [blame] | 277 | |
Chunhe Lan | 2e13d57 | 2013-08-16 15:10:36 +0800 | [diff] [blame] | 278 | timeval = get_ticks(); |
| 279 | while (readb(&dev->sr) & I2C_SR_MBB) { |
| 280 | if ((get_ticks() - timeval) < timeout) |
| 281 | continue; |
| 282 | |
| 283 | if (fsl_i2c_fixup(dev)) |
| 284 | debug("i2c_init: BUS#%d failed to init\n", |
| 285 | adap->hwadapnr); |
| 286 | |
| 287 | break; |
| 288 | } |
| 289 | |
Richard Retanubun | df0149c | 2010-04-12 15:08:17 -0400 | [diff] [blame] | 290 | #ifdef CONFIG_SYS_I2C_BOARD_LATE_INIT |
| 291 | /* Call board specific i2c bus reset routine AFTER the bus has been |
| 292 | * initialized. Use either this callpoint or i2c_init_board; |
| 293 | * which is called before i2c_init operations. |
| 294 | * For details about this problem see doc/I2C_Edge_Conditions. |
| 295 | */ |
| 296 | i2c_board_late_init(); |
| 297 | #endif |
Jon Loeliger | e4773be | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 298 | } |
| 299 | |
Joakim Tjernlund | c324b78 | 2009-09-17 11:07:15 +0200 | [diff] [blame] | 300 | static int |
Heiko Schocher | f285074 | 2012-10-24 13:48:22 +0200 | [diff] [blame] | 301 | i2c_wait4bus(struct i2c_adapter *adap) |
Jon Loeliger | e4773be | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 302 | { |
Heiko Schocher | f285074 | 2012-10-24 13:48:22 +0200 | [diff] [blame] | 303 | struct fsl_i2c *dev = (struct fsl_i2c *)i2c_dev[adap->hwadapnr]; |
Stefan Roese | 3762825 | 2008-08-06 14:05:38 +0200 | [diff] [blame] | 304 | unsigned long long timeval = get_ticks(); |
Timur Tabi | 2165c62 | 2009-09-04 16:28:35 -0500 | [diff] [blame] | 305 | const unsigned long long timeout = usec2ticks(CONFIG_I2C_MBB_TIMEOUT); |
Jon Loeliger | e4773be | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 306 | |
Heiko Schocher | f285074 | 2012-10-24 13:48:22 +0200 | [diff] [blame] | 307 | while (readb(&dev->sr) & I2C_SR_MBB) { |
Timur Tabi | 2165c62 | 2009-09-04 16:28:35 -0500 | [diff] [blame] | 308 | if ((get_ticks() - timeval) > timeout) |
Jon Loeliger | e4773be | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 309 | return -1; |
Jon Loeliger | e4773be | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | return 0; |
| 313 | } |
| 314 | |
| 315 | static __inline__ int |
Heiko Schocher | f285074 | 2012-10-24 13:48:22 +0200 | [diff] [blame] | 316 | i2c_wait(struct i2c_adapter *adap, int write) |
Jon Loeliger | e4773be | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 317 | { |
| 318 | u32 csr; |
Stefan Roese | 3762825 | 2008-08-06 14:05:38 +0200 | [diff] [blame] | 319 | unsigned long long timeval = get_ticks(); |
Timur Tabi | 2165c62 | 2009-09-04 16:28:35 -0500 | [diff] [blame] | 320 | const unsigned long long timeout = usec2ticks(CONFIG_I2C_TIMEOUT); |
Heiko Schocher | f285074 | 2012-10-24 13:48:22 +0200 | [diff] [blame] | 321 | struct fsl_i2c *dev = (struct fsl_i2c *)i2c_dev[adap->hwadapnr]; |
Jon Loeliger | e4773be | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 322 | |
| 323 | do { |
Heiko Schocher | f285074 | 2012-10-24 13:48:22 +0200 | [diff] [blame] | 324 | csr = readb(&dev->sr); |
Jon Loeliger | e4773be | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 325 | if (!(csr & I2C_SR_MIF)) |
| 326 | continue; |
Joakim Tjernlund | c324b78 | 2009-09-17 11:07:15 +0200 | [diff] [blame] | 327 | /* Read again to allow register to stabilise */ |
Heiko Schocher | f285074 | 2012-10-24 13:48:22 +0200 | [diff] [blame] | 328 | csr = readb(&dev->sr); |
Jon Loeliger | e4773be | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 329 | |
Heiko Schocher | f285074 | 2012-10-24 13:48:22 +0200 | [diff] [blame] | 330 | writeb(0x0, &dev->sr); |
Jon Loeliger | e4773be | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 331 | |
| 332 | if (csr & I2C_SR_MAL) { |
| 333 | debug("i2c_wait: MAL\n"); |
| 334 | return -1; |
| 335 | } |
| 336 | |
| 337 | if (!(csr & I2C_SR_MCF)) { |
| 338 | debug("i2c_wait: unfinished\n"); |
| 339 | return -1; |
| 340 | } |
| 341 | |
Joakim Tjernlund | c32c5f7 | 2006-11-28 16:17:27 -0600 | [diff] [blame] | 342 | if (write == I2C_WRITE_BIT && (csr & I2C_SR_RXAK)) { |
Jon Loeliger | e4773be | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 343 | debug("i2c_wait: No RXACK\n"); |
| 344 | return -1; |
| 345 | } |
| 346 | |
| 347 | return 0; |
Timur Tabi | 2165c62 | 2009-09-04 16:28:35 -0500 | [diff] [blame] | 348 | } while ((get_ticks() - timeval) < timeout); |
Jon Loeliger | e4773be | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 349 | |
| 350 | debug("i2c_wait: timed out\n"); |
| 351 | return -1; |
| 352 | } |
| 353 | |
| 354 | static __inline__ int |
Heiko Schocher | f285074 | 2012-10-24 13:48:22 +0200 | [diff] [blame] | 355 | i2c_write_addr(struct i2c_adapter *adap, u8 dev, u8 dir, int rsta) |
Jon Loeliger | e4773be | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 356 | { |
Heiko Schocher | f285074 | 2012-10-24 13:48:22 +0200 | [diff] [blame] | 357 | struct fsl_i2c *device = (struct fsl_i2c *)i2c_dev[adap->hwadapnr]; |
| 358 | |
Jon Loeliger | e4773be | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 359 | writeb(I2C_CR_MEN | I2C_CR_MSTA | I2C_CR_MTX |
| 360 | | (rsta ? I2C_CR_RSTA : 0), |
Heiko Schocher | f285074 | 2012-10-24 13:48:22 +0200 | [diff] [blame] | 361 | &device->cr); |
Jon Loeliger | e4773be | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 362 | |
Heiko Schocher | f285074 | 2012-10-24 13:48:22 +0200 | [diff] [blame] | 363 | writeb((dev << 1) | dir, &device->dr); |
Jon Loeliger | e4773be | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 364 | |
Heiko Schocher | f285074 | 2012-10-24 13:48:22 +0200 | [diff] [blame] | 365 | if (i2c_wait(adap, I2C_WRITE_BIT) < 0) |
Jon Loeliger | e4773be | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 366 | return 0; |
| 367 | |
| 368 | return 1; |
| 369 | } |
| 370 | |
| 371 | static __inline__ int |
Heiko Schocher | f285074 | 2012-10-24 13:48:22 +0200 | [diff] [blame] | 372 | __i2c_write(struct i2c_adapter *adap, u8 *data, int length) |
Jon Loeliger | e4773be | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 373 | { |
Heiko Schocher | f285074 | 2012-10-24 13:48:22 +0200 | [diff] [blame] | 374 | struct fsl_i2c *dev = (struct fsl_i2c *)i2c_dev[adap->hwadapnr]; |
Jon Loeliger | e4773be | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 375 | int i; |
| 376 | |
Jon Loeliger | e4773be | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 377 | for (i = 0; i < length; i++) { |
Heiko Schocher | f285074 | 2012-10-24 13:48:22 +0200 | [diff] [blame] | 378 | writeb(data[i], &dev->dr); |
Jon Loeliger | e4773be | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 379 | |
Heiko Schocher | f285074 | 2012-10-24 13:48:22 +0200 | [diff] [blame] | 380 | if (i2c_wait(adap, I2C_WRITE_BIT) < 0) |
Jon Loeliger | e4773be | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 381 | break; |
| 382 | } |
| 383 | |
| 384 | return i; |
| 385 | } |
| 386 | |
| 387 | static __inline__ int |
Heiko Schocher | f285074 | 2012-10-24 13:48:22 +0200 | [diff] [blame] | 388 | __i2c_read(struct i2c_adapter *adap, u8 *data, int length) |
Jon Loeliger | e4773be | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 389 | { |
Heiko Schocher | f285074 | 2012-10-24 13:48:22 +0200 | [diff] [blame] | 390 | struct fsl_i2c *dev = (struct fsl_i2c *)i2c_dev[adap->hwadapnr]; |
Jon Loeliger | e4773be | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 391 | int i; |
| 392 | |
| 393 | writeb(I2C_CR_MEN | I2C_CR_MSTA | ((length == 1) ? I2C_CR_TXAK : 0), |
Heiko Schocher | f285074 | 2012-10-24 13:48:22 +0200 | [diff] [blame] | 394 | &dev->cr); |
Jon Loeliger | e4773be | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 395 | |
| 396 | /* dummy read */ |
Heiko Schocher | f285074 | 2012-10-24 13:48:22 +0200 | [diff] [blame] | 397 | readb(&dev->dr); |
Jon Loeliger | e4773be | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 398 | |
| 399 | for (i = 0; i < length; i++) { |
Heiko Schocher | f285074 | 2012-10-24 13:48:22 +0200 | [diff] [blame] | 400 | if (i2c_wait(adap, I2C_READ_BIT) < 0) |
Jon Loeliger | e4773be | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 401 | break; |
| 402 | |
| 403 | /* Generate ack on last next to last byte */ |
| 404 | if (i == length - 2) |
| 405 | writeb(I2C_CR_MEN | I2C_CR_MSTA | I2C_CR_TXAK, |
Heiko Schocher | f285074 | 2012-10-24 13:48:22 +0200 | [diff] [blame] | 406 | &dev->cr); |
Jon Loeliger | e4773be | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 407 | |
Joakim Tjernlund | 6384da2 | 2009-09-22 13:40:44 +0200 | [diff] [blame] | 408 | /* Do not generate stop on last byte */ |
Jon Loeliger | e4773be | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 409 | if (i == length - 1) |
Joakim Tjernlund | 6384da2 | 2009-09-22 13:40:44 +0200 | [diff] [blame] | 410 | writeb(I2C_CR_MEN | I2C_CR_MSTA | I2C_CR_MTX, |
Heiko Schocher | f285074 | 2012-10-24 13:48:22 +0200 | [diff] [blame] | 411 | &dev->cr); |
Jon Loeliger | e4773be | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 412 | |
Heiko Schocher | f285074 | 2012-10-24 13:48:22 +0200 | [diff] [blame] | 413 | data[i] = readb(&dev->dr); |
Jon Loeliger | e4773be | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 414 | } |
| 415 | |
| 416 | return i; |
| 417 | } |
| 418 | |
Heiko Schocher | f285074 | 2012-10-24 13:48:22 +0200 | [diff] [blame] | 419 | static int |
| 420 | fsl_i2c_read(struct i2c_adapter *adap, u8 dev, uint addr, int alen, u8 *data, |
| 421 | int length) |
Jon Loeliger | e4773be | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 422 | { |
Heiko Schocher | f285074 | 2012-10-24 13:48:22 +0200 | [diff] [blame] | 423 | struct fsl_i2c *device = (struct fsl_i2c *)i2c_dev[adap->hwadapnr]; |
Joakim Tjernlund | b648fe7 | 2007-01-31 11:04:19 +0100 | [diff] [blame] | 424 | int i = -1; /* signal error */ |
Jon Loeliger | e4773be | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 425 | u8 *a = (u8*)&addr; |
| 426 | |
Heiko Schocher | f285074 | 2012-10-24 13:48:22 +0200 | [diff] [blame] | 427 | if (i2c_wait4bus(adap) < 0) |
Reinhard Pfau | 2d878de | 2013-06-26 15:55:14 +0200 | [diff] [blame] | 428 | return -1; |
| 429 | |
| 430 | if ((!length || alen > 0) |
Heiko Schocher | f285074 | 2012-10-24 13:48:22 +0200 | [diff] [blame] | 431 | && i2c_write_addr(adap, dev, I2C_WRITE_BIT, 0) != 0 |
| 432 | && __i2c_write(adap, &a[4 - alen], alen) == alen) |
Joakim Tjernlund | b648fe7 | 2007-01-31 11:04:19 +0100 | [diff] [blame] | 433 | i = 0; /* No error so far */ |
| 434 | |
Reinhard Pfau | 2d878de | 2013-06-26 15:55:14 +0200 | [diff] [blame] | 435 | if (length && |
Heiko Schocher | f285074 | 2012-10-24 13:48:22 +0200 | [diff] [blame] | 436 | i2c_write_addr(adap, dev, I2C_READ_BIT, alen ? 1 : 0) != 0) |
| 437 | i = __i2c_read(adap, data, length); |
Jon Loeliger | e4773be | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 438 | |
Heiko Schocher | f285074 | 2012-10-24 13:48:22 +0200 | [diff] [blame] | 439 | writeb(I2C_CR_MEN, &device->cr); |
Jon Loeliger | e4773be | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 440 | |
Heiko Schocher | f285074 | 2012-10-24 13:48:22 +0200 | [diff] [blame] | 441 | if (i2c_wait4bus(adap)) /* Wait until STOP */ |
Joakim Tjernlund | 6384da2 | 2009-09-22 13:40:44 +0200 | [diff] [blame] | 442 | debug("i2c_read: wait4bus timed out\n"); |
| 443 | |
Jon Loeliger | 24df977 | 2006-10-19 12:02:24 -0500 | [diff] [blame] | 444 | if (i == length) |
| 445 | return 0; |
| 446 | |
| 447 | return -1; |
Jon Loeliger | e4773be | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 448 | } |
| 449 | |
Heiko Schocher | f285074 | 2012-10-24 13:48:22 +0200 | [diff] [blame] | 450 | static int |
| 451 | fsl_i2c_write(struct i2c_adapter *adap, u8 dev, uint addr, int alen, |
| 452 | u8 *data, int length) |
Jon Loeliger | e4773be | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 453 | { |
Heiko Schocher | f285074 | 2012-10-24 13:48:22 +0200 | [diff] [blame] | 454 | struct fsl_i2c *device = (struct fsl_i2c *)i2c_dev[adap->hwadapnr]; |
Joakim Tjernlund | b648fe7 | 2007-01-31 11:04:19 +0100 | [diff] [blame] | 455 | int i = -1; /* signal error */ |
Jon Loeliger | e4773be | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 456 | u8 *a = (u8*)&addr; |
| 457 | |
Chunhe Lan | 2e13d57 | 2013-08-16 15:10:36 +0800 | [diff] [blame] | 458 | if (i2c_wait4bus(adap) < 0) |
| 459 | return -1; |
| 460 | |
| 461 | if (i2c_write_addr(adap, dev, I2C_WRITE_BIT, 0) != 0 && |
Heiko Schocher | f285074 | 2012-10-24 13:48:22 +0200 | [diff] [blame] | 462 | __i2c_write(adap, &a[4 - alen], alen) == alen) { |
| 463 | i = __i2c_write(adap, data, length); |
Jon Loeliger | 24df977 | 2006-10-19 12:02:24 -0500 | [diff] [blame] | 464 | } |
Jon Loeliger | e4773be | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 465 | |
Heiko Schocher | f285074 | 2012-10-24 13:48:22 +0200 | [diff] [blame] | 466 | writeb(I2C_CR_MEN, &device->cr); |
| 467 | if (i2c_wait4bus(adap)) /* Wait until STOP */ |
Joakim Tjernlund | c324b78 | 2009-09-17 11:07:15 +0200 | [diff] [blame] | 468 | debug("i2c_write: wait4bus timed out\n"); |
Jon Loeliger | e4773be | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 469 | |
Jon Loeliger | 24df977 | 2006-10-19 12:02:24 -0500 | [diff] [blame] | 470 | if (i == length) |
| 471 | return 0; |
| 472 | |
| 473 | return -1; |
Jon Loeliger | e4773be | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 474 | } |
| 475 | |
Heiko Schocher | f285074 | 2012-10-24 13:48:22 +0200 | [diff] [blame] | 476 | static int |
| 477 | fsl_i2c_probe(struct i2c_adapter *adap, uchar chip) |
Jon Loeliger | e4773be | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 478 | { |
Heiko Schocher | f285074 | 2012-10-24 13:48:22 +0200 | [diff] [blame] | 479 | struct fsl_i2c *dev = (struct fsl_i2c *)i2c_dev[adap->hwadapnr]; |
Joakim Tjernlund | b648fe7 | 2007-01-31 11:04:19 +0100 | [diff] [blame] | 480 | /* For unknow reason the controller will ACK when |
| 481 | * probing for a slave with the same address, so skip |
| 482 | * it. |
Jon Loeliger | e4773be | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 483 | */ |
Heiko Schocher | f285074 | 2012-10-24 13:48:22 +0200 | [diff] [blame] | 484 | if (chip == (readb(&dev->adr) >> 1)) |
Timur Tabi | ab34754 | 2006-11-03 19:15:00 -0600 | [diff] [blame] | 485 | return -1; |
Timur Tabi | ab34754 | 2006-11-03 19:15:00 -0600 | [diff] [blame] | 486 | |
Heiko Schocher | f285074 | 2012-10-24 13:48:22 +0200 | [diff] [blame] | 487 | return fsl_i2c_read(adap, chip, 0, 0, NULL, 0); |
Timur Tabi | ab34754 | 2006-11-03 19:15:00 -0600 | [diff] [blame] | 488 | } |
| 489 | |
Heiko Schocher | f285074 | 2012-10-24 13:48:22 +0200 | [diff] [blame] | 490 | static unsigned int fsl_i2c_set_bus_speed(struct i2c_adapter *adap, |
| 491 | unsigned int speed) |
Timur Tabi | ab34754 | 2006-11-03 19:15:00 -0600 | [diff] [blame] | 492 | { |
Heiko Schocher | f285074 | 2012-10-24 13:48:22 +0200 | [diff] [blame] | 493 | struct fsl_i2c *dev = (struct fsl_i2c *)i2c_dev[adap->hwadapnr]; |
Timur Tabi | b301fda | 2008-03-14 17:45:29 -0500 | [diff] [blame] | 494 | |
Heiko Schocher | f285074 | 2012-10-24 13:48:22 +0200 | [diff] [blame] | 495 | writeb(0, &dev->cr); /* stop controller */ |
| 496 | set_i2c_bus_speed(dev, get_i2c_clock(adap->hwadapnr), speed); |
| 497 | writeb(I2C_CR_MEN, &dev->cr); /* start controller */ |
Timur Tabi | b301fda | 2008-03-14 17:45:29 -0500 | [diff] [blame] | 498 | |
| 499 | return 0; |
Timur Tabi | ab34754 | 2006-11-03 19:15:00 -0600 | [diff] [blame] | 500 | } |
| 501 | |
Heiko Schocher | f285074 | 2012-10-24 13:48:22 +0200 | [diff] [blame] | 502 | /* |
| 503 | * Register fsl i2c adapters |
| 504 | */ |
| 505 | U_BOOT_I2C_ADAP_COMPLETE(fsl_0, fsl_i2c_init, fsl_i2c_probe, fsl_i2c_read, |
| 506 | fsl_i2c_write, fsl_i2c_set_bus_speed, |
| 507 | CONFIG_SYS_FSL_I2C_SPEED, CONFIG_SYS_FSL_I2C_SLAVE, |
| 508 | 0) |
| 509 | #ifdef CONFIG_SYS_FSL_I2C2_OFFSET |
| 510 | U_BOOT_I2C_ADAP_COMPLETE(fsl_1, fsl_i2c_init, fsl_i2c_probe, fsl_i2c_read, |
| 511 | fsl_i2c_write, fsl_i2c_set_bus_speed, |
| 512 | CONFIG_SYS_FSL_I2C2_SPEED, CONFIG_SYS_FSL_I2C2_SLAVE, |
| 513 | 1) |
Heiko Schocher | 2c9f3a4 | 2009-02-24 11:30:37 +0100 | [diff] [blame] | 514 | #endif |