Jon Loeliger | 7237c03 | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 1 | /* |
Timur Tabi | 92477a6 | 2009-09-04 16:28:35 -0500 | [diff] [blame] | 2 | * Copyright 2006,2009 Freescale Semiconductor, Inc. |
Jon Loeliger | 7237c03 | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 3 | * |
Heiko Schocher | 00f792e | 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 | * |
Tom Rini | 5b8031c | 2016-01-14 22:05:13 -0500 | [diff] [blame] | 7 | * SPDX-License-Identifier: GPL-2.0 |
Jon Loeliger | 7237c03 | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 8 | */ |
| 9 | |
Jon Loeliger | 7237c03 | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 10 | #include <common.h> |
Jon Loeliger | 4d45f69 | 2006-10-19 12:02:24 -0500 | [diff] [blame] | 11 | #include <command.h> |
Jon Loeliger | 2047672 | 2006-10-20 15:50:15 -0500 | [diff] [blame] | 12 | #include <i2c.h> /* Functional interface */ |
Jon Loeliger | 7237c03 | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 13 | #include <asm/io.h> |
Jon Loeliger | 2047672 | 2006-10-20 15:50:15 -0500 | [diff] [blame] | 14 | #include <asm/fsl_i2c.h> /* HW definitions */ |
Jon Loeliger | 7237c03 | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 15 | |
Timur Tabi | 92477a6 | 2009-09-04 16:28:35 -0500 | [diff] [blame] | 16 | /* The maximum number of microseconds we will wait until another master has |
| 17 | * released the bus. If not defined in the board header file, then use a |
| 18 | * generic value. |
| 19 | */ |
| 20 | #ifndef CONFIG_I2C_MBB_TIMEOUT |
| 21 | #define CONFIG_I2C_MBB_TIMEOUT 100000 |
| 22 | #endif |
| 23 | |
| 24 | /* The maximum number of microseconds we will wait for a read or write |
| 25 | * operation to complete. If not defined in the board header file, then use a |
| 26 | * generic value. |
| 27 | */ |
| 28 | #ifndef CONFIG_I2C_TIMEOUT |
Shaveta Leekha | 6dd38cc | 2014-11-03 10:43:14 +0530 | [diff] [blame] | 29 | #define CONFIG_I2C_TIMEOUT 100000 |
Timur Tabi | 92477a6 | 2009-09-04 16:28:35 -0500 | [diff] [blame] | 30 | #endif |
Jon Loeliger | 7237c03 | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 31 | |
Joakim Tjernlund | 1939d96 | 2006-11-28 16:17:27 -0600 | [diff] [blame] | 32 | #define I2C_READ_BIT 1 |
| 33 | #define I2C_WRITE_BIT 0 |
| 34 | |
Timur Tabi | d8c82db | 2008-03-14 17:45:29 -0500 | [diff] [blame] | 35 | DECLARE_GLOBAL_DATA_PTR; |
| 36 | |
mario.six@gdsys.cc | ec2c81c | 2016-04-25 08:31:01 +0200 | [diff] [blame] | 37 | static const struct fsl_i2c_base *i2c_base[4] = { |
| 38 | (struct fsl_i2c_base *)(CONFIG_SYS_IMMR + CONFIG_SYS_FSL_I2C_OFFSET), |
Heiko Schocher | 00f792e | 2012-10-24 13:48:22 +0200 | [diff] [blame] | 39 | #ifdef CONFIG_SYS_FSL_I2C2_OFFSET |
mario.six@gdsys.cc | ec2c81c | 2016-04-25 08:31:01 +0200 | [diff] [blame] | 40 | (struct fsl_i2c_base *)(CONFIG_SYS_IMMR + CONFIG_SYS_FSL_I2C2_OFFSET), |
Shengzhou Liu | a17fd10 | 2014-07-07 12:17:48 +0800 | [diff] [blame] | 41 | #endif |
| 42 | #ifdef CONFIG_SYS_FSL_I2C3_OFFSET |
mario.six@gdsys.cc | ec2c81c | 2016-04-25 08:31:01 +0200 | [diff] [blame] | 43 | (struct fsl_i2c_base *)(CONFIG_SYS_IMMR + CONFIG_SYS_FSL_I2C3_OFFSET), |
Shengzhou Liu | a17fd10 | 2014-07-07 12:17:48 +0800 | [diff] [blame] | 44 | #endif |
| 45 | #ifdef CONFIG_SYS_FSL_I2C4_OFFSET |
mario.six@gdsys.cc | ec2c81c | 2016-04-25 08:31:01 +0200 | [diff] [blame] | 46 | (struct fsl_i2c_base *)(CONFIG_SYS_IMMR + CONFIG_SYS_FSL_I2C4_OFFSET) |
Timur Tabi | be5e618 | 2006-11-03 19:15:00 -0600 | [diff] [blame] | 47 | #endif |
| 48 | }; |
Jon Loeliger | 7237c03 | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 49 | |
Timur Tabi | d8c82db | 2008-03-14 17:45:29 -0500 | [diff] [blame] | 50 | /* I2C speed map for a DFSR value of 1 */ |
| 51 | |
| 52 | /* |
| 53 | * Map I2C frequency dividers to FDR and DFSR values |
| 54 | * |
| 55 | * This structure is used to define the elements of a table that maps I2C |
| 56 | * frequency divider (I2C clock rate divided by I2C bus speed) to a value to be |
| 57 | * programmed into the Frequency Divider Ratio (FDR) and Digital Filter |
| 58 | * Sampling Rate (DFSR) registers. |
| 59 | * |
| 60 | * The actual table should be defined in the board file, and it must be called |
| 61 | * fsl_i2c_speed_map[]. |
| 62 | * |
| 63 | * The last entry of the table must have a value of {-1, X}, where X is same |
| 64 | * FDR/DFSR values as the second-to-last entry. This guarantees that any |
| 65 | * search through the array will always find a match. |
| 66 | * |
| 67 | * The values of the divider must be in increasing numerical order, i.e. |
| 68 | * fsl_i2c_speed_map[x+1].divider > fsl_i2c_speed_map[x].divider. |
| 69 | * |
| 70 | * For this table, the values are based on a value of 1 for the DFSR |
| 71 | * register. See the application note AN2919 "Determining the I2C Frequency |
| 72 | * Divider Ratio for SCL" |
TsiChung Liew | 5d9a5ef | 2008-08-19 00:56:46 +0600 | [diff] [blame] | 73 | * |
| 74 | * ColdFire I2C frequency dividers for FDR values are different from |
| 75 | * PowerPC. The protocol to use the I2C module is still the same. |
| 76 | * A different table is defined and are based on MCF5xxx user manual. |
| 77 | * |
Timur Tabi | d8c82db | 2008-03-14 17:45:29 -0500 | [diff] [blame] | 78 | */ |
| 79 | static const struct { |
| 80 | unsigned short divider; |
Timur Tabi | d8c82db | 2008-03-14 17:45:29 -0500 | [diff] [blame] | 81 | u8 fdr; |
| 82 | } fsl_i2c_speed_map[] = { |
Joakim Tjernlund | 9940420 | 2009-09-17 11:07:17 +0200 | [diff] [blame] | 83 | #ifdef __M68K__ |
TsiChung Liew | 5d9a5ef | 2008-08-19 00:56:46 +0600 | [diff] [blame] | 84 | {20, 32}, {22, 33}, {24, 34}, {26, 35}, |
| 85 | {28, 0}, {28, 36}, {30, 1}, {32, 37}, |
| 86 | {34, 2}, {36, 38}, {40, 3}, {40, 39}, |
| 87 | {44, 4}, {48, 5}, {48, 40}, {56, 6}, |
| 88 | {56, 41}, {64, 42}, {68, 7}, {72, 43}, |
| 89 | {80, 8}, {80, 44}, {88, 9}, {96, 41}, |
| 90 | {104, 10}, {112, 42}, {128, 11}, {128, 43}, |
| 91 | {144, 12}, {160, 13}, {160, 48}, {192, 14}, |
| 92 | {192, 49}, {224, 50}, {240, 15}, {256, 51}, |
| 93 | {288, 16}, {320, 17}, {320, 52}, {384, 18}, |
| 94 | {384, 53}, {448, 54}, {480, 19}, {512, 55}, |
| 95 | {576, 20}, {640, 21}, {640, 56}, {768, 22}, |
| 96 | {768, 57}, {960, 23}, {896, 58}, {1024, 59}, |
| 97 | {1152, 24}, {1280, 25}, {1280, 60}, {1536, 26}, |
| 98 | {1536, 61}, {1792, 62}, {1920, 27}, {2048, 63}, |
| 99 | {2304, 28}, {2560, 29}, {3072, 30}, {3840, 31}, |
| 100 | {-1, 31} |
| 101 | #endif |
Timur Tabi | d8c82db | 2008-03-14 17:45:29 -0500 | [diff] [blame] | 102 | }; |
| 103 | |
| 104 | /** |
| 105 | * Set the I2C bus speed for a given I2C device |
| 106 | * |
mario.six@gdsys.cc | ec2c81c | 2016-04-25 08:31:01 +0200 | [diff] [blame] | 107 | * @param base: the I2C device registers |
Timur Tabi | d8c82db | 2008-03-14 17:45:29 -0500 | [diff] [blame] | 108 | * @i2c_clk: I2C bus clock frequency |
| 109 | * @speed: the desired speed of the bus |
| 110 | * |
| 111 | * The I2C device must be stopped before calling this function. |
| 112 | * |
| 113 | * The return value is the actual bus speed that is set. |
| 114 | */ |
mario.six@gdsys.cc | ec2c81c | 2016-04-25 08:31:01 +0200 | [diff] [blame] | 115 | static unsigned int set_i2c_bus_speed(const struct fsl_i2c_base *base, |
Timur Tabi | d8c82db | 2008-03-14 17:45:29 -0500 | [diff] [blame] | 116 | unsigned int i2c_clk, unsigned int speed) |
| 117 | { |
Masahiro Yamada | b414119 | 2014-11-07 03:03:31 +0900 | [diff] [blame] | 118 | unsigned short divider = min(i2c_clk / speed, (unsigned int)USHRT_MAX); |
Timur Tabi | d8c82db | 2008-03-14 17:45:29 -0500 | [diff] [blame] | 119 | |
| 120 | /* |
| 121 | * We want to choose an FDR/DFSR that generates an I2C bus speed that |
| 122 | * is equal to or lower than the requested speed. That means that we |
| 123 | * want the first divider that is equal to or greater than the |
| 124 | * calculated divider. |
| 125 | */ |
Joakim Tjernlund | 9940420 | 2009-09-17 11:07:17 +0200 | [diff] [blame] | 126 | #ifdef __PPC__ |
| 127 | u8 dfsr, fdr = 0x31; /* Default if no FDR found */ |
| 128 | /* a, b and dfsr matches identifiers A,B and C respectively in AN2919 */ |
| 129 | unsigned short a, b, ga, gb; |
| 130 | unsigned long c_div, est_div; |
| 131 | |
| 132 | #ifdef CONFIG_FSL_I2C_CUSTOM_DFSR |
| 133 | dfsr = CONFIG_FSL_I2C_CUSTOM_DFSR; |
| 134 | #else |
| 135 | /* Condition 1: dfsr <= 50/T */ |
| 136 | dfsr = (5 * (i2c_clk / 1000)) / 100000; |
| 137 | #endif |
| 138 | #ifdef CONFIG_FSL_I2C_CUSTOM_FDR |
| 139 | fdr = CONFIG_FSL_I2C_CUSTOM_FDR; |
| 140 | speed = i2c_clk / divider; /* Fake something */ |
| 141 | #else |
| 142 | debug("Requested speed:%d, i2c_clk:%d\n", speed, i2c_clk); |
| 143 | if (!dfsr) |
| 144 | dfsr = 1; |
| 145 | |
| 146 | est_div = ~0; |
| 147 | for (ga = 0x4, a = 10; a <= 30; ga++, a += 2) { |
| 148 | for (gb = 0; gb < 8; gb++) { |
| 149 | b = 16 << gb; |
| 150 | c_div = b * (a + ((3*dfsr)/b)*2); |
| 151 | if ((c_div > divider) && (c_div < est_div)) { |
| 152 | unsigned short bin_gb, bin_ga; |
| 153 | |
| 154 | est_div = c_div; |
| 155 | bin_gb = gb << 2; |
| 156 | bin_ga = (ga & 0x3) | ((ga & 0x4) << 3); |
| 157 | fdr = bin_gb | bin_ga; |
| 158 | speed = i2c_clk / est_div; |
| 159 | debug("FDR:0x%.2x, div:%ld, ga:0x%x, gb:0x%x, " |
| 160 | "a:%d, b:%d, speed:%d\n", |
| 161 | fdr, est_div, ga, gb, a, b, speed); |
| 162 | /* Condition 2 not accounted for */ |
| 163 | debug("Tr <= %d ns\n", |
| 164 | (b - 3 * dfsr) * 1000000 / |
| 165 | (i2c_clk / 1000)); |
| 166 | } |
| 167 | } |
| 168 | if (a == 20) |
| 169 | a += 2; |
| 170 | if (a == 24) |
| 171 | a += 4; |
| 172 | } |
| 173 | debug("divider:%d, est_div:%ld, DFSR:%d\n", divider, est_div, dfsr); |
| 174 | debug("FDR:0x%.2x, speed:%d\n", fdr, speed); |
| 175 | #endif |
mario.six@gdsys.cc | ec2c81c | 2016-04-25 08:31:01 +0200 | [diff] [blame] | 176 | writeb(dfsr, &base->dfsrr); /* set default filter */ |
| 177 | writeb(fdr, &base->fdr); /* set bus speed */ |
Joakim Tjernlund | 9940420 | 2009-09-17 11:07:17 +0200 | [diff] [blame] | 178 | #else |
| 179 | unsigned int i; |
Timur Tabi | d8c82db | 2008-03-14 17:45:29 -0500 | [diff] [blame] | 180 | |
| 181 | for (i = 0; i < ARRAY_SIZE(fsl_i2c_speed_map); i++) |
| 182 | if (fsl_i2c_speed_map[i].divider >= divider) { |
TsiChung Liew | 5d9a5ef | 2008-08-19 00:56:46 +0600 | [diff] [blame] | 183 | u8 fdr; |
Joakim Tjernlund | 9940420 | 2009-09-17 11:07:17 +0200 | [diff] [blame] | 184 | |
Joakim Tjernlund | d01ee4d | 2009-09-17 11:07:16 +0200 | [diff] [blame] | 185 | fdr = fsl_i2c_speed_map[i].fdr; |
| 186 | speed = i2c_clk / fsl_i2c_speed_map[i].divider; |
mario.six@gdsys.cc | ec2c81c | 2016-04-25 08:31:01 +0200 | [diff] [blame] | 187 | writeb(fdr, &base->fdr); /* set bus speed */ |
Joakim Tjernlund | d01ee4d | 2009-09-17 11:07:16 +0200 | [diff] [blame] | 188 | |
Timur Tabi | d8c82db | 2008-03-14 17:45:29 -0500 | [diff] [blame] | 189 | break; |
| 190 | } |
Joakim Tjernlund | 9940420 | 2009-09-17 11:07:17 +0200 | [diff] [blame] | 191 | #endif |
Timur Tabi | d8c82db | 2008-03-14 17:45:29 -0500 | [diff] [blame] | 192 | return speed; |
| 193 | } |
| 194 | |
Kim Phillips | 62f730f | 2012-10-16 14:28:43 +0000 | [diff] [blame] | 195 | static unsigned int get_i2c_clock(int bus) |
Jerry Huang | c9a8b25 | 2011-10-26 15:29:38 +0000 | [diff] [blame] | 196 | { |
| 197 | if (bus) |
Simon Glass | 609e6ec | 2012-12-13 20:48:49 +0000 | [diff] [blame] | 198 | return gd->arch.i2c2_clk; /* I2C2 clock */ |
Jerry Huang | c9a8b25 | 2011-10-26 15:29:38 +0000 | [diff] [blame] | 199 | else |
Simon Glass | 609e6ec | 2012-12-13 20:48:49 +0000 | [diff] [blame] | 200 | return gd->arch.i2c1_clk; /* I2C1 clock */ |
Jerry Huang | c9a8b25 | 2011-10-26 15:29:38 +0000 | [diff] [blame] | 201 | } |
| 202 | |
mario.six@gdsys.cc | ec2c81c | 2016-04-25 08:31:01 +0200 | [diff] [blame] | 203 | static int fsl_i2c_fixup(const struct fsl_i2c_base *base) |
Chunhe Lan | b8ce334 | 2013-08-16 15:10:36 +0800 | [diff] [blame] | 204 | { |
| 205 | const unsigned long long timeout = usec2ticks(CONFIG_I2C_MBB_TIMEOUT); |
| 206 | unsigned long long timeval = 0; |
| 207 | int ret = -1; |
Chunhe Lan | 9c3f77e | 2013-08-16 15:10:37 +0800 | [diff] [blame] | 208 | unsigned int flags = 0; |
| 209 | |
| 210 | #ifdef CONFIG_SYS_FSL_ERRATUM_I2C_A004447 |
| 211 | unsigned int svr = get_svr(); |
| 212 | if ((SVR_SOC_VER(svr) == SVR_8548 && IS_SVR_REV(svr, 3, 1)) || |
| 213 | (SVR_REV(svr) <= CONFIG_SYS_FSL_A004447_SVR_REV)) |
| 214 | flags = I2C_CR_BIT6; |
| 215 | #endif |
Chunhe Lan | b8ce334 | 2013-08-16 15:10:36 +0800 | [diff] [blame] | 216 | |
mario.six@gdsys.cc | ec2c81c | 2016-04-25 08:31:01 +0200 | [diff] [blame] | 217 | writeb(I2C_CR_MEN | I2C_CR_MSTA, &base->cr); |
Chunhe Lan | b8ce334 | 2013-08-16 15:10:36 +0800 | [diff] [blame] | 218 | |
| 219 | timeval = get_ticks(); |
mario.six@gdsys.cc | ec2c81c | 2016-04-25 08:31:01 +0200 | [diff] [blame] | 220 | while (!(readb(&base->sr) & I2C_SR_MBB)) { |
Chunhe Lan | b8ce334 | 2013-08-16 15:10:36 +0800 | [diff] [blame] | 221 | if ((get_ticks() - timeval) > timeout) |
| 222 | goto err; |
| 223 | } |
| 224 | |
mario.six@gdsys.cc | ec2c81c | 2016-04-25 08:31:01 +0200 | [diff] [blame] | 225 | if (readb(&base->sr) & I2C_SR_MAL) { |
Chunhe Lan | b8ce334 | 2013-08-16 15:10:36 +0800 | [diff] [blame] | 226 | /* SDA is stuck low */ |
mario.six@gdsys.cc | ec2c81c | 2016-04-25 08:31:01 +0200 | [diff] [blame] | 227 | writeb(0, &base->cr); |
Chunhe Lan | b8ce334 | 2013-08-16 15:10:36 +0800 | [diff] [blame] | 228 | udelay(100); |
mario.six@gdsys.cc | ec2c81c | 2016-04-25 08:31:01 +0200 | [diff] [blame] | 229 | writeb(I2C_CR_MSTA | flags, &base->cr); |
| 230 | writeb(I2C_CR_MEN | I2C_CR_MSTA | flags, &base->cr); |
Chunhe Lan | b8ce334 | 2013-08-16 15:10:36 +0800 | [diff] [blame] | 231 | } |
| 232 | |
mario.six@gdsys.cc | ec2c81c | 2016-04-25 08:31:01 +0200 | [diff] [blame] | 233 | readb(&base->dr); |
Chunhe Lan | b8ce334 | 2013-08-16 15:10:36 +0800 | [diff] [blame] | 234 | |
| 235 | timeval = get_ticks(); |
mario.six@gdsys.cc | ec2c81c | 2016-04-25 08:31:01 +0200 | [diff] [blame] | 236 | while (!(readb(&base->sr) & I2C_SR_MIF)) { |
Chunhe Lan | b8ce334 | 2013-08-16 15:10:36 +0800 | [diff] [blame] | 237 | if ((get_ticks() - timeval) > timeout) |
| 238 | goto err; |
| 239 | } |
| 240 | ret = 0; |
| 241 | |
| 242 | err: |
mario.six@gdsys.cc | ec2c81c | 2016-04-25 08:31:01 +0200 | [diff] [blame] | 243 | writeb(I2C_CR_MEN | flags, &base->cr); |
| 244 | writeb(0, &base->sr); |
Chunhe Lan | b8ce334 | 2013-08-16 15:10:36 +0800 | [diff] [blame] | 245 | udelay(100); |
| 246 | |
| 247 | return ret; |
| 248 | } |
| 249 | |
mario.six@gdsys.cc | ecf591e | 2016-04-25 08:31:08 +0200 | [diff] [blame^] | 250 | static void __i2c_init(const struct fsl_i2c_base *base, int speed, int |
| 251 | slaveadd, int i2c_clk, int busnum) |
Jon Loeliger | 7237c03 | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 252 | { |
Chunhe Lan | b8ce334 | 2013-08-16 15:10:36 +0800 | [diff] [blame] | 253 | const unsigned long long timeout = usec2ticks(CONFIG_I2C_MBB_TIMEOUT); |
| 254 | unsigned long long timeval; |
Jon Loeliger | 7237c03 | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 255 | |
Heiko Schocher | 39df00d | 2009-07-09 12:04:26 +0200 | [diff] [blame] | 256 | #ifdef CONFIG_SYS_I2C_INIT_BOARD |
Richard Retanubun | 26a3350 | 2010-04-12 15:08:17 -0400 | [diff] [blame] | 257 | /* Call board specific i2c bus reset routine before accessing the |
| 258 | * environment, which might be in a chip on that bus. For details |
| 259 | * about this problem see doc/I2C_Edge_Conditions. |
| 260 | */ |
Heiko Schocher | 39df00d | 2009-07-09 12:04:26 +0200 | [diff] [blame] | 261 | i2c_init_board(); |
| 262 | #endif |
mario.six@gdsys.cc | ec2c81c | 2016-04-25 08:31:01 +0200 | [diff] [blame] | 263 | writeb(0, &base->cr); /* stop I2C controller */ |
Heiko Schocher | 00f792e | 2012-10-24 13:48:22 +0200 | [diff] [blame] | 264 | udelay(5); /* let it shutdown in peace */ |
mario.six@gdsys.cc | ecf591e | 2016-04-25 08:31:08 +0200 | [diff] [blame^] | 265 | set_i2c_bus_speed(base, i2c_clk, speed); |
mario.six@gdsys.cc | ec2c81c | 2016-04-25 08:31:01 +0200 | [diff] [blame] | 266 | writeb(slaveadd << 1, &base->adr);/* write slave address */ |
| 267 | writeb(0x0, &base->sr); /* clear status register */ |
| 268 | writeb(I2C_CR_MEN, &base->cr); /* start I2C controller */ |
Richard Retanubun | 26a3350 | 2010-04-12 15:08:17 -0400 | [diff] [blame] | 269 | |
Chunhe Lan | b8ce334 | 2013-08-16 15:10:36 +0800 | [diff] [blame] | 270 | timeval = get_ticks(); |
mario.six@gdsys.cc | ec2c81c | 2016-04-25 08:31:01 +0200 | [diff] [blame] | 271 | while (readb(&base->sr) & I2C_SR_MBB) { |
Chunhe Lan | b8ce334 | 2013-08-16 15:10:36 +0800 | [diff] [blame] | 272 | if ((get_ticks() - timeval) < timeout) |
| 273 | continue; |
| 274 | |
mario.six@gdsys.cc | ec2c81c | 2016-04-25 08:31:01 +0200 | [diff] [blame] | 275 | if (fsl_i2c_fixup(base)) |
Chunhe Lan | b8ce334 | 2013-08-16 15:10:36 +0800 | [diff] [blame] | 276 | debug("i2c_init: BUS#%d failed to init\n", |
mario.six@gdsys.cc | ecf591e | 2016-04-25 08:31:08 +0200 | [diff] [blame^] | 277 | busnum); |
Chunhe Lan | b8ce334 | 2013-08-16 15:10:36 +0800 | [diff] [blame] | 278 | |
| 279 | break; |
| 280 | } |
| 281 | |
Richard Retanubun | 26a3350 | 2010-04-12 15:08:17 -0400 | [diff] [blame] | 282 | #ifdef CONFIG_SYS_I2C_BOARD_LATE_INIT |
| 283 | /* Call board specific i2c bus reset routine AFTER the bus has been |
| 284 | * initialized. Use either this callpoint or i2c_init_board; |
| 285 | * which is called before i2c_init operations. |
| 286 | * For details about this problem see doc/I2C_Edge_Conditions. |
| 287 | */ |
| 288 | i2c_board_late_init(); |
| 289 | #endif |
Jon Loeliger | 7237c03 | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 290 | } |
| 291 | |
Joakim Tjernlund | 21f4cbb | 2009-09-17 11:07:15 +0200 | [diff] [blame] | 292 | static int |
mario.six@gdsys.cc | ecf591e | 2016-04-25 08:31:08 +0200 | [diff] [blame^] | 293 | i2c_wait4bus(const struct fsl_i2c_base *base) |
Jon Loeliger | 7237c03 | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 294 | { |
Stefan Roese | f2302d4 | 2008-08-06 14:05:38 +0200 | [diff] [blame] | 295 | unsigned long long timeval = get_ticks(); |
Timur Tabi | 92477a6 | 2009-09-04 16:28:35 -0500 | [diff] [blame] | 296 | const unsigned long long timeout = usec2ticks(CONFIG_I2C_MBB_TIMEOUT); |
Jon Loeliger | 7237c03 | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 297 | |
mario.six@gdsys.cc | ec2c81c | 2016-04-25 08:31:01 +0200 | [diff] [blame] | 298 | while (readb(&base->sr) & I2C_SR_MBB) { |
Timur Tabi | 92477a6 | 2009-09-04 16:28:35 -0500 | [diff] [blame] | 299 | if ((get_ticks() - timeval) > timeout) |
Jon Loeliger | 7237c03 | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 300 | return -1; |
Jon Loeliger | 7237c03 | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 301 | } |
| 302 | |
| 303 | return 0; |
| 304 | } |
| 305 | |
mario.six@gdsys.cc | ecf591e | 2016-04-25 08:31:08 +0200 | [diff] [blame^] | 306 | static inline int |
| 307 | i2c_wait(const struct fsl_i2c_base *base, int write) |
Jon Loeliger | 7237c03 | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 308 | { |
| 309 | u32 csr; |
Stefan Roese | f2302d4 | 2008-08-06 14:05:38 +0200 | [diff] [blame] | 310 | unsigned long long timeval = get_ticks(); |
Timur Tabi | 92477a6 | 2009-09-04 16:28:35 -0500 | [diff] [blame] | 311 | const unsigned long long timeout = usec2ticks(CONFIG_I2C_TIMEOUT); |
Jon Loeliger | 7237c03 | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 312 | |
| 313 | do { |
mario.six@gdsys.cc | ec2c81c | 2016-04-25 08:31:01 +0200 | [diff] [blame] | 314 | csr = readb(&base->sr); |
Jon Loeliger | 7237c03 | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 315 | if (!(csr & I2C_SR_MIF)) |
| 316 | continue; |
Joakim Tjernlund | 21f4cbb | 2009-09-17 11:07:15 +0200 | [diff] [blame] | 317 | /* Read again to allow register to stabilise */ |
mario.six@gdsys.cc | ec2c81c | 2016-04-25 08:31:01 +0200 | [diff] [blame] | 318 | csr = readb(&base->sr); |
Jon Loeliger | 7237c03 | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 319 | |
mario.six@gdsys.cc | ec2c81c | 2016-04-25 08:31:01 +0200 | [diff] [blame] | 320 | writeb(0x0, &base->sr); |
Jon Loeliger | 7237c03 | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 321 | |
| 322 | if (csr & I2C_SR_MAL) { |
| 323 | debug("i2c_wait: MAL\n"); |
| 324 | return -1; |
| 325 | } |
| 326 | |
| 327 | if (!(csr & I2C_SR_MCF)) { |
| 328 | debug("i2c_wait: unfinished\n"); |
| 329 | return -1; |
| 330 | } |
| 331 | |
Joakim Tjernlund | 1939d96 | 2006-11-28 16:17:27 -0600 | [diff] [blame] | 332 | if (write == I2C_WRITE_BIT && (csr & I2C_SR_RXAK)) { |
Jon Loeliger | 7237c03 | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 333 | debug("i2c_wait: No RXACK\n"); |
| 334 | return -1; |
| 335 | } |
| 336 | |
| 337 | return 0; |
Timur Tabi | 92477a6 | 2009-09-04 16:28:35 -0500 | [diff] [blame] | 338 | } while ((get_ticks() - timeval) < timeout); |
Jon Loeliger | 7237c03 | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 339 | |
| 340 | debug("i2c_wait: timed out\n"); |
| 341 | return -1; |
| 342 | } |
| 343 | |
mario.six@gdsys.cc | ecf591e | 2016-04-25 08:31:08 +0200 | [diff] [blame^] | 344 | static inline int |
| 345 | i2c_write_addr(const struct fsl_i2c_base *base, u8 dev, u8 dir, int rsta) |
Jon Loeliger | 7237c03 | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 346 | { |
| 347 | writeb(I2C_CR_MEN | I2C_CR_MSTA | I2C_CR_MTX |
| 348 | | (rsta ? I2C_CR_RSTA : 0), |
mario.six@gdsys.cc | ec2c81c | 2016-04-25 08:31:01 +0200 | [diff] [blame] | 349 | &base->cr); |
Jon Loeliger | 7237c03 | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 350 | |
mario.six@gdsys.cc | ec2c81c | 2016-04-25 08:31:01 +0200 | [diff] [blame] | 351 | writeb((dev << 1) | dir, &base->dr); |
Jon Loeliger | 7237c03 | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 352 | |
mario.six@gdsys.cc | ecf591e | 2016-04-25 08:31:08 +0200 | [diff] [blame^] | 353 | if (i2c_wait(base, I2C_WRITE_BIT) < 0) |
Jon Loeliger | 7237c03 | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 354 | return 0; |
| 355 | |
| 356 | return 1; |
| 357 | } |
| 358 | |
mario.six@gdsys.cc | ecf591e | 2016-04-25 08:31:08 +0200 | [diff] [blame^] | 359 | static inline int |
| 360 | __i2c_write_data(const struct fsl_i2c_base *base, u8 *data, int length) |
Jon Loeliger | 7237c03 | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 361 | { |
| 362 | int i; |
| 363 | |
Jon Loeliger | 7237c03 | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 364 | for (i = 0; i < length; i++) { |
mario.six@gdsys.cc | ec2c81c | 2016-04-25 08:31:01 +0200 | [diff] [blame] | 365 | writeb(data[i], &base->dr); |
Jon Loeliger | 7237c03 | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 366 | |
mario.six@gdsys.cc | ecf591e | 2016-04-25 08:31:08 +0200 | [diff] [blame^] | 367 | if (i2c_wait(base, I2C_WRITE_BIT) < 0) |
Jon Loeliger | 7237c03 | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 368 | break; |
| 369 | } |
| 370 | |
| 371 | return i; |
| 372 | } |
| 373 | |
mario.six@gdsys.cc | ecf591e | 2016-04-25 08:31:08 +0200 | [diff] [blame^] | 374 | static inline int |
| 375 | __i2c_read_data(const struct fsl_i2c_base *base, u8 *data, int length) |
Jon Loeliger | 7237c03 | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 376 | { |
| 377 | int i; |
| 378 | |
| 379 | writeb(I2C_CR_MEN | I2C_CR_MSTA | ((length == 1) ? I2C_CR_TXAK : 0), |
mario.six@gdsys.cc | ec2c81c | 2016-04-25 08:31:01 +0200 | [diff] [blame] | 380 | &base->cr); |
Jon Loeliger | 7237c03 | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 381 | |
| 382 | /* dummy read */ |
mario.six@gdsys.cc | ec2c81c | 2016-04-25 08:31:01 +0200 | [diff] [blame] | 383 | readb(&base->dr); |
Jon Loeliger | 7237c03 | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 384 | |
| 385 | for (i = 0; i < length; i++) { |
mario.six@gdsys.cc | ecf591e | 2016-04-25 08:31:08 +0200 | [diff] [blame^] | 386 | if (i2c_wait(base, I2C_READ_BIT) < 0) |
Jon Loeliger | 7237c03 | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 387 | break; |
| 388 | |
| 389 | /* Generate ack on last next to last byte */ |
| 390 | if (i == length - 2) |
| 391 | writeb(I2C_CR_MEN | I2C_CR_MSTA | I2C_CR_TXAK, |
mario.six@gdsys.cc | ec2c81c | 2016-04-25 08:31:01 +0200 | [diff] [blame] | 392 | &base->cr); |
Jon Loeliger | 7237c03 | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 393 | |
Joakim Tjernlund | d1c9e5b | 2009-09-22 13:40:44 +0200 | [diff] [blame] | 394 | /* Do not generate stop on last byte */ |
Jon Loeliger | 7237c03 | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 395 | if (i == length - 1) |
Joakim Tjernlund | d1c9e5b | 2009-09-22 13:40:44 +0200 | [diff] [blame] | 396 | writeb(I2C_CR_MEN | I2C_CR_MSTA | I2C_CR_MTX, |
mario.six@gdsys.cc | ec2c81c | 2016-04-25 08:31:01 +0200 | [diff] [blame] | 397 | &base->cr); |
Jon Loeliger | 7237c03 | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 398 | |
mario.six@gdsys.cc | ec2c81c | 2016-04-25 08:31:01 +0200 | [diff] [blame] | 399 | data[i] = readb(&base->dr); |
Jon Loeliger | 7237c03 | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 400 | } |
| 401 | |
| 402 | return i; |
| 403 | } |
| 404 | |
Heiko Schocher | 00f792e | 2012-10-24 13:48:22 +0200 | [diff] [blame] | 405 | static int |
mario.six@gdsys.cc | ecf591e | 2016-04-25 08:31:08 +0200 | [diff] [blame^] | 406 | __i2c_read(const struct fsl_i2c_base *base, u8 chip_addr, u8 *offset, int olen, |
mario.six@gdsys.cc | ad7e657 | 2016-04-25 08:31:07 +0200 | [diff] [blame] | 407 | u8 *data, int dlen) |
Jon Loeliger | 7237c03 | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 408 | { |
mario.six@gdsys.cc | 2b21e96 | 2016-04-25 08:31:02 +0200 | [diff] [blame] | 409 | int ret = -1; /* signal error */ |
Jon Loeliger | 7237c03 | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 410 | |
mario.six@gdsys.cc | ecf591e | 2016-04-25 08:31:08 +0200 | [diff] [blame^] | 411 | if (i2c_wait4bus(base) < 0) |
Reinhard Pfau | b778c1b | 2013-06-26 15:55:14 +0200 | [diff] [blame] | 412 | return -1; |
| 413 | |
mario.six@gdsys.cc | 386b276 | 2016-04-25 08:31:03 +0200 | [diff] [blame] | 414 | /* Some drivers use offset lengths in excess of 4 bytes. These drivers |
| 415 | * adhere to the following convention: |
| 416 | * - the offset length is passed as negative (that is, the absolute |
| 417 | * value of olen is the actual offset length) |
| 418 | * - the offset itself is passed in data, which is overwritten by the |
| 419 | * subsequent read operation |
Shaveta Leekha | a405764 | 2014-04-24 14:51:23 +0530 | [diff] [blame] | 420 | */ |
mario.six@gdsys.cc | 2b21e96 | 2016-04-25 08:31:02 +0200 | [diff] [blame] | 421 | if (olen < 0) { |
mario.six@gdsys.cc | ecf591e | 2016-04-25 08:31:08 +0200 | [diff] [blame^] | 422 | if (i2c_write_addr(base, chip_addr, I2C_WRITE_BIT, 0) != 0) |
| 423 | ret = __i2c_write_data(base, data, -olen); |
Joakim Tjernlund | f6f5f70 | 2007-01-31 11:04:19 +0100 | [diff] [blame] | 424 | |
mario.six@gdsys.cc | 03a112a | 2016-04-25 08:31:04 +0200 | [diff] [blame] | 425 | if (ret != -olen) |
Shaveta Leekha | a405764 | 2014-04-24 14:51:23 +0530 | [diff] [blame] | 426 | return -1; |
| 427 | |
mario.six@gdsys.cc | ecf591e | 2016-04-25 08:31:08 +0200 | [diff] [blame^] | 428 | if (dlen && i2c_write_addr(base, chip_addr, |
mario.six@gdsys.cc | 2b21e96 | 2016-04-25 08:31:02 +0200 | [diff] [blame] | 429 | I2C_READ_BIT, 1) != 0) |
mario.six@gdsys.cc | ecf591e | 2016-04-25 08:31:08 +0200 | [diff] [blame^] | 430 | ret = __i2c_read_data(base, data, dlen); |
Shaveta Leekha | a405764 | 2014-04-24 14:51:23 +0530 | [diff] [blame] | 431 | } else { |
mario.six@gdsys.cc | 2b21e96 | 2016-04-25 08:31:02 +0200 | [diff] [blame] | 432 | if ((!dlen || olen > 0) && |
mario.six@gdsys.cc | ecf591e | 2016-04-25 08:31:08 +0200 | [diff] [blame^] | 433 | i2c_write_addr(base, chip_addr, I2C_WRITE_BIT, 0) != 0 && |
| 434 | __i2c_write_data(base, offset, olen) == olen) |
mario.six@gdsys.cc | 2b21e96 | 2016-04-25 08:31:02 +0200 | [diff] [blame] | 435 | ret = 0; /* No error so far */ |
Shaveta Leekha | a405764 | 2014-04-24 14:51:23 +0530 | [diff] [blame] | 436 | |
mario.six@gdsys.cc | ecf591e | 2016-04-25 08:31:08 +0200 | [diff] [blame^] | 437 | if (dlen && i2c_write_addr(base, chip_addr, I2C_READ_BIT, |
mario.six@gdsys.cc | 2b21e96 | 2016-04-25 08:31:02 +0200 | [diff] [blame] | 438 | olen ? 1 : 0) != 0) |
mario.six@gdsys.cc | ecf591e | 2016-04-25 08:31:08 +0200 | [diff] [blame^] | 439 | ret = __i2c_read_data(base, data, dlen); |
Shaveta Leekha | a405764 | 2014-04-24 14:51:23 +0530 | [diff] [blame] | 440 | } |
Jon Loeliger | 7237c03 | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 441 | |
mario.six@gdsys.cc | ec2c81c | 2016-04-25 08:31:01 +0200 | [diff] [blame] | 442 | writeb(I2C_CR_MEN, &base->cr); |
Jon Loeliger | 7237c03 | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 443 | |
mario.six@gdsys.cc | ecf591e | 2016-04-25 08:31:08 +0200 | [diff] [blame^] | 444 | if (i2c_wait4bus(base)) /* Wait until STOP */ |
Joakim Tjernlund | d1c9e5b | 2009-09-22 13:40:44 +0200 | [diff] [blame] | 445 | debug("i2c_read: wait4bus timed out\n"); |
| 446 | |
mario.six@gdsys.cc | 2b21e96 | 2016-04-25 08:31:02 +0200 | [diff] [blame] | 447 | if (ret == dlen) |
| 448 | return 0; |
Jon Loeliger | 4d45f69 | 2006-10-19 12:02:24 -0500 | [diff] [blame] | 449 | |
| 450 | return -1; |
Jon Loeliger | 7237c03 | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 451 | } |
| 452 | |
Heiko Schocher | 00f792e | 2012-10-24 13:48:22 +0200 | [diff] [blame] | 453 | static int |
mario.six@gdsys.cc | ecf591e | 2016-04-25 08:31:08 +0200 | [diff] [blame^] | 454 | __i2c_write(const struct fsl_i2c_base *base, u8 chip_addr, u8 *offset, int olen, |
mario.six@gdsys.cc | ad7e657 | 2016-04-25 08:31:07 +0200 | [diff] [blame] | 455 | u8 *data, int dlen) |
Jon Loeliger | 7237c03 | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 456 | { |
mario.six@gdsys.cc | 2b21e96 | 2016-04-25 08:31:02 +0200 | [diff] [blame] | 457 | int ret = -1; /* signal error */ |
Jon Loeliger | 7237c03 | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 458 | |
mario.six@gdsys.cc | ecf591e | 2016-04-25 08:31:08 +0200 | [diff] [blame^] | 459 | if (i2c_wait4bus(base) < 0) |
Chunhe Lan | b8ce334 | 2013-08-16 15:10:36 +0800 | [diff] [blame] | 460 | return -1; |
| 461 | |
mario.six@gdsys.cc | ecf591e | 2016-04-25 08:31:08 +0200 | [diff] [blame^] | 462 | if (i2c_write_addr(base, chip_addr, I2C_WRITE_BIT, 0) != 0 && |
| 463 | __i2c_write_data(base, offset, olen) == olen) { |
| 464 | ret = __i2c_write_data(base, data, dlen); |
Jon Loeliger | 4d45f69 | 2006-10-19 12:02:24 -0500 | [diff] [blame] | 465 | } |
Jon Loeliger | 7237c03 | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 466 | |
mario.six@gdsys.cc | ec2c81c | 2016-04-25 08:31:01 +0200 | [diff] [blame] | 467 | writeb(I2C_CR_MEN, &base->cr); |
mario.six@gdsys.cc | ecf591e | 2016-04-25 08:31:08 +0200 | [diff] [blame^] | 468 | if (i2c_wait4bus(base)) /* Wait until STOP */ |
Joakim Tjernlund | 21f4cbb | 2009-09-17 11:07:15 +0200 | [diff] [blame] | 469 | debug("i2c_write: wait4bus timed out\n"); |
Jon Loeliger | 7237c03 | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 470 | |
mario.six@gdsys.cc | 2b21e96 | 2016-04-25 08:31:02 +0200 | [diff] [blame] | 471 | if (ret == dlen) |
| 472 | return 0; |
Jon Loeliger | 4d45f69 | 2006-10-19 12:02:24 -0500 | [diff] [blame] | 473 | |
| 474 | return -1; |
Jon Loeliger | 7237c03 | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 475 | } |
| 476 | |
Heiko Schocher | 00f792e | 2012-10-24 13:48:22 +0200 | [diff] [blame] | 477 | static int |
mario.six@gdsys.cc | ecf591e | 2016-04-25 08:31:08 +0200 | [diff] [blame^] | 478 | __i2c_probe_chip(const struct fsl_i2c_base *base, uchar chip) |
Jon Loeliger | 7237c03 | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 479 | { |
Joakim Tjernlund | f6f5f70 | 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 | 7237c03 | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 483 | */ |
mario.six@gdsys.cc | ec2c81c | 2016-04-25 08:31:01 +0200 | [diff] [blame] | 484 | if (chip == (readb(&base->adr) >> 1)) |
Joakim Tjernlund | f6f5f70 | 2007-01-31 11:04:19 +0100 | [diff] [blame] | 485 | return -1; |
Jon Loeliger | 7237c03 | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 486 | |
mario.six@gdsys.cc | ecf591e | 2016-04-25 08:31:08 +0200 | [diff] [blame^] | 487 | return __i2c_read(base, chip, 0, 0, NULL, 0); |
Jon Loeliger | 7237c03 | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 488 | } |
| 489 | |
mario.six@gdsys.cc | ecf591e | 2016-04-25 08:31:08 +0200 | [diff] [blame^] | 490 | static unsigned int __i2c_set_bus_speed(const struct fsl_i2c_base *base, |
| 491 | unsigned int speed, int i2c_clk) |
Timur Tabi | be5e618 | 2006-11-03 19:15:00 -0600 | [diff] [blame] | 492 | { |
mario.six@gdsys.cc | ec2c81c | 2016-04-25 08:31:01 +0200 | [diff] [blame] | 493 | writeb(0, &base->cr); /* stop controller */ |
mario.six@gdsys.cc | ecf591e | 2016-04-25 08:31:08 +0200 | [diff] [blame^] | 494 | set_i2c_bus_speed(base, i2c_clk, speed); |
mario.six@gdsys.cc | ec2c81c | 2016-04-25 08:31:01 +0200 | [diff] [blame] | 495 | writeb(I2C_CR_MEN, &base->cr); /* start controller */ |
Timur Tabi | d8c82db | 2008-03-14 17:45:29 -0500 | [diff] [blame] | 496 | |
| 497 | return 0; |
Timur Tabi | be5e618 | 2006-11-03 19:15:00 -0600 | [diff] [blame] | 498 | } |
| 499 | |
mario.six@gdsys.cc | ad7e657 | 2016-04-25 08:31:07 +0200 | [diff] [blame] | 500 | static void fsl_i2c_init(struct i2c_adapter *adap, int speed, int slaveadd) |
| 501 | { |
mario.six@gdsys.cc | ecf591e | 2016-04-25 08:31:08 +0200 | [diff] [blame^] | 502 | __i2c_init(i2c_base[adap->hwadapnr], speed, slaveadd, |
| 503 | get_i2c_clock(adap->hwadapnr), adap->hwadapnr); |
mario.six@gdsys.cc | ad7e657 | 2016-04-25 08:31:07 +0200 | [diff] [blame] | 504 | } |
| 505 | |
| 506 | static int |
| 507 | fsl_i2c_probe_chip(struct i2c_adapter *adap, uchar chip) |
| 508 | { |
mario.six@gdsys.cc | ecf591e | 2016-04-25 08:31:08 +0200 | [diff] [blame^] | 509 | return __i2c_probe_chip(i2c_base[adap->hwadapnr], chip); |
mario.six@gdsys.cc | ad7e657 | 2016-04-25 08:31:07 +0200 | [diff] [blame] | 510 | } |
| 511 | |
| 512 | static int |
| 513 | fsl_i2c_read(struct i2c_adapter *adap, u8 chip_addr, uint offset, int olen, |
| 514 | u8 *data, int dlen) |
| 515 | { |
mario.six@gdsys.cc | ecf591e | 2016-04-25 08:31:08 +0200 | [diff] [blame^] | 516 | u8 *o = (u8 *)&offset; |
| 517 | return __i2c_read(i2c_base[adap->hwadapnr], chip_addr, &o[4 - olen], |
| 518 | olen, data, dlen); |
mario.six@gdsys.cc | ad7e657 | 2016-04-25 08:31:07 +0200 | [diff] [blame] | 519 | } |
| 520 | |
| 521 | static int |
| 522 | fsl_i2c_write(struct i2c_adapter *adap, u8 chip_addr, uint offset, int olen, |
| 523 | u8 *data, int dlen) |
| 524 | { |
mario.six@gdsys.cc | ecf591e | 2016-04-25 08:31:08 +0200 | [diff] [blame^] | 525 | u8 *o = (u8 *)&offset; |
| 526 | return __i2c_write(i2c_base[adap->hwadapnr], chip_addr, &o[4 - olen], |
| 527 | olen, data, dlen); |
mario.six@gdsys.cc | ad7e657 | 2016-04-25 08:31:07 +0200 | [diff] [blame] | 528 | } |
| 529 | |
| 530 | static unsigned int fsl_i2c_set_bus_speed(struct i2c_adapter *adap, |
| 531 | unsigned int speed) |
| 532 | { |
mario.six@gdsys.cc | ecf591e | 2016-04-25 08:31:08 +0200 | [diff] [blame^] | 533 | return __i2c_set_bus_speed(i2c_base[adap->hwadapnr], speed, |
| 534 | get_i2c_clock(adap->hwadapnr)); |
mario.six@gdsys.cc | ad7e657 | 2016-04-25 08:31:07 +0200 | [diff] [blame] | 535 | } |
| 536 | |
Heiko Schocher | 00f792e | 2012-10-24 13:48:22 +0200 | [diff] [blame] | 537 | /* |
| 538 | * Register fsl i2c adapters |
| 539 | */ |
mario.six@gdsys.cc | 16579ec | 2016-04-25 08:31:05 +0200 | [diff] [blame] | 540 | U_BOOT_I2C_ADAP_COMPLETE(fsl_0, fsl_i2c_init, fsl_i2c_probe_chip, fsl_i2c_read, |
Heiko Schocher | 00f792e | 2012-10-24 13:48:22 +0200 | [diff] [blame] | 541 | fsl_i2c_write, fsl_i2c_set_bus_speed, |
| 542 | CONFIG_SYS_FSL_I2C_SPEED, CONFIG_SYS_FSL_I2C_SLAVE, |
| 543 | 0) |
| 544 | #ifdef CONFIG_SYS_FSL_I2C2_OFFSET |
mario.six@gdsys.cc | 16579ec | 2016-04-25 08:31:05 +0200 | [diff] [blame] | 545 | U_BOOT_I2C_ADAP_COMPLETE(fsl_1, fsl_i2c_init, fsl_i2c_probe_chip, fsl_i2c_read, |
Heiko Schocher | 00f792e | 2012-10-24 13:48:22 +0200 | [diff] [blame] | 546 | fsl_i2c_write, fsl_i2c_set_bus_speed, |
| 547 | CONFIG_SYS_FSL_I2C2_SPEED, CONFIG_SYS_FSL_I2C2_SLAVE, |
| 548 | 1) |
Heiko Schocher | c1bce4f | 2009-02-24 11:30:37 +0100 | [diff] [blame] | 549 | #endif |
Shengzhou Liu | a17fd10 | 2014-07-07 12:17:48 +0800 | [diff] [blame] | 550 | #ifdef CONFIG_SYS_FSL_I2C3_OFFSET |
mario.six@gdsys.cc | 16579ec | 2016-04-25 08:31:05 +0200 | [diff] [blame] | 551 | U_BOOT_I2C_ADAP_COMPLETE(fsl_2, fsl_i2c_init, fsl_i2c_probe_chip, fsl_i2c_read, |
Shengzhou Liu | a17fd10 | 2014-07-07 12:17:48 +0800 | [diff] [blame] | 552 | fsl_i2c_write, fsl_i2c_set_bus_speed, |
| 553 | CONFIG_SYS_FSL_I2C3_SPEED, CONFIG_SYS_FSL_I2C3_SLAVE, |
| 554 | 2) |
| 555 | #endif |
| 556 | #ifdef CONFIG_SYS_FSL_I2C4_OFFSET |
mario.six@gdsys.cc | 16579ec | 2016-04-25 08:31:05 +0200 | [diff] [blame] | 557 | U_BOOT_I2C_ADAP_COMPLETE(fsl_3, fsl_i2c_init, fsl_i2c_probe_chip, fsl_i2c_read, |
Shengzhou Liu | a17fd10 | 2014-07-07 12:17:48 +0800 | [diff] [blame] | 558 | fsl_i2c_write, fsl_i2c_set_bus_speed, |
| 559 | CONFIG_SYS_FSL_I2C4_SPEED, CONFIG_SYS_FSL_I2C4_SLAVE, |
| 560 | 3) |
| 561 | #endif |