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 | * |
Tom Rini | e237880 | 2016-01-14 22:05:13 -0500 | [diff] [blame] | 7 | * SPDX-License-Identifier: GPL-2.0 |
Jon Loeliger | e4773be | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 8 | */ |
| 9 | |
Jon Loeliger | e4773be | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 10 | #include <common.h> |
Jon Loeliger | 24df977 | 2006-10-19 12:02:24 -0500 | [diff] [blame] | 11 | #include <command.h> |
Jon Loeliger | 43d818f | 2006-10-20 15:50:15 -0500 | [diff] [blame] | 12 | #include <i2c.h> /* Functional interface */ |
Jon Loeliger | e4773be | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 13 | #include <asm/io.h> |
Jon Loeliger | 43d818f | 2006-10-20 15:50:15 -0500 | [diff] [blame] | 14 | #include <asm/fsl_i2c.h> /* HW definitions */ |
mario.six@gdsys.cc | 349686c | 2016-04-25 08:31:09 +0200 | [diff] [blame] | 15 | #include <dm.h> |
| 16 | #include <mapmem.h> |
Jon Loeliger | e4773be | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 17 | |
Timur Tabi | 2165c62 | 2009-09-04 16:28:35 -0500 | [diff] [blame] | 18 | /* The maximum number of microseconds we will wait until another master has |
| 19 | * released the bus. If not defined in the board header file, then use a |
| 20 | * generic value. |
| 21 | */ |
| 22 | #ifndef CONFIG_I2C_MBB_TIMEOUT |
| 23 | #define CONFIG_I2C_MBB_TIMEOUT 100000 |
| 24 | #endif |
| 25 | |
| 26 | /* The maximum number of microseconds we will wait for a read or write |
| 27 | * operation to complete. If not defined in the board header file, then use a |
| 28 | * generic value. |
| 29 | */ |
| 30 | #ifndef CONFIG_I2C_TIMEOUT |
Shaveta Leekha | 2ba098f | 2014-11-03 10:43:14 +0530 | [diff] [blame] | 31 | #define CONFIG_I2C_TIMEOUT 100000 |
Timur Tabi | 2165c62 | 2009-09-04 16:28:35 -0500 | [diff] [blame] | 32 | #endif |
Jon Loeliger | e4773be | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 33 | |
Joakim Tjernlund | c32c5f7 | 2006-11-28 16:17:27 -0600 | [diff] [blame] | 34 | #define I2C_READ_BIT 1 |
| 35 | #define I2C_WRITE_BIT 0 |
| 36 | |
Timur Tabi | b301fda | 2008-03-14 17:45:29 -0500 | [diff] [blame] | 37 | DECLARE_GLOBAL_DATA_PTR; |
| 38 | |
mario.six@gdsys.cc | 349686c | 2016-04-25 08:31:09 +0200 | [diff] [blame] | 39 | #ifndef CONFIG_DM_I2C |
mario.six@gdsys.cc | 7d43b4e | 2016-04-25 08:31:01 +0200 | [diff] [blame] | 40 | static const struct fsl_i2c_base *i2c_base[4] = { |
| 41 | (struct fsl_i2c_base *)(CONFIG_SYS_IMMR + CONFIG_SYS_FSL_I2C_OFFSET), |
Heiko Schocher | f285074 | 2012-10-24 13:48:22 +0200 | [diff] [blame] | 42 | #ifdef CONFIG_SYS_FSL_I2C2_OFFSET |
mario.six@gdsys.cc | 7d43b4e | 2016-04-25 08:31:01 +0200 | [diff] [blame] | 43 | (struct fsl_i2c_base *)(CONFIG_SYS_IMMR + CONFIG_SYS_FSL_I2C2_OFFSET), |
Shengzhou Liu | 37787f6 | 2014-07-07 12:17:48 +0800 | [diff] [blame] | 44 | #endif |
| 45 | #ifdef CONFIG_SYS_FSL_I2C3_OFFSET |
mario.six@gdsys.cc | 7d43b4e | 2016-04-25 08:31:01 +0200 | [diff] [blame] | 46 | (struct fsl_i2c_base *)(CONFIG_SYS_IMMR + CONFIG_SYS_FSL_I2C3_OFFSET), |
Shengzhou Liu | 37787f6 | 2014-07-07 12:17:48 +0800 | [diff] [blame] | 47 | #endif |
| 48 | #ifdef CONFIG_SYS_FSL_I2C4_OFFSET |
mario.six@gdsys.cc | 7d43b4e | 2016-04-25 08:31:01 +0200 | [diff] [blame] | 49 | (struct fsl_i2c_base *)(CONFIG_SYS_IMMR + CONFIG_SYS_FSL_I2C4_OFFSET) |
Timur Tabi | ab34754 | 2006-11-03 19:15:00 -0600 | [diff] [blame] | 50 | #endif |
| 51 | }; |
mario.six@gdsys.cc | 349686c | 2016-04-25 08:31:09 +0200 | [diff] [blame] | 52 | #endif |
Jon Loeliger | e4773be | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 53 | |
Timur Tabi | b301fda | 2008-03-14 17:45:29 -0500 | [diff] [blame] | 54 | /* I2C speed map for a DFSR value of 1 */ |
| 55 | |
Tom Rini | 56762c1 | 2017-02-09 15:40:16 -0500 | [diff] [blame^] | 56 | #ifdef __M68K__ |
Timur Tabi | b301fda | 2008-03-14 17:45:29 -0500 | [diff] [blame] | 57 | /* |
| 58 | * Map I2C frequency dividers to FDR and DFSR values |
| 59 | * |
| 60 | * This structure is used to define the elements of a table that maps I2C |
| 61 | * frequency divider (I2C clock rate divided by I2C bus speed) to a value to be |
| 62 | * programmed into the Frequency Divider Ratio (FDR) and Digital Filter |
| 63 | * Sampling Rate (DFSR) registers. |
| 64 | * |
| 65 | * The actual table should be defined in the board file, and it must be called |
| 66 | * fsl_i2c_speed_map[]. |
| 67 | * |
| 68 | * The last entry of the table must have a value of {-1, X}, where X is same |
| 69 | * FDR/DFSR values as the second-to-last entry. This guarantees that any |
| 70 | * search through the array will always find a match. |
| 71 | * |
| 72 | * The values of the divider must be in increasing numerical order, i.e. |
| 73 | * fsl_i2c_speed_map[x+1].divider > fsl_i2c_speed_map[x].divider. |
| 74 | * |
| 75 | * For this table, the values are based on a value of 1 for the DFSR |
| 76 | * register. See the application note AN2919 "Determining the I2C Frequency |
| 77 | * Divider Ratio for SCL" |
TsiChung Liew | 00648a7 | 2008-08-19 00:56:46 +0600 | [diff] [blame] | 78 | * |
| 79 | * ColdFire I2C frequency dividers for FDR values are different from |
| 80 | * PowerPC. The protocol to use the I2C module is still the same. |
| 81 | * A different table is defined and are based on MCF5xxx user manual. |
| 82 | * |
Timur Tabi | b301fda | 2008-03-14 17:45:29 -0500 | [diff] [blame] | 83 | */ |
| 84 | static const struct { |
| 85 | unsigned short divider; |
Timur Tabi | b301fda | 2008-03-14 17:45:29 -0500 | [diff] [blame] | 86 | u8 fdr; |
| 87 | } fsl_i2c_speed_map[] = { |
TsiChung Liew | 00648a7 | 2008-08-19 00:56:46 +0600 | [diff] [blame] | 88 | {20, 32}, {22, 33}, {24, 34}, {26, 35}, |
| 89 | {28, 0}, {28, 36}, {30, 1}, {32, 37}, |
| 90 | {34, 2}, {36, 38}, {40, 3}, {40, 39}, |
| 91 | {44, 4}, {48, 5}, {48, 40}, {56, 6}, |
| 92 | {56, 41}, {64, 42}, {68, 7}, {72, 43}, |
| 93 | {80, 8}, {80, 44}, {88, 9}, {96, 41}, |
| 94 | {104, 10}, {112, 42}, {128, 11}, {128, 43}, |
| 95 | {144, 12}, {160, 13}, {160, 48}, {192, 14}, |
| 96 | {192, 49}, {224, 50}, {240, 15}, {256, 51}, |
| 97 | {288, 16}, {320, 17}, {320, 52}, {384, 18}, |
| 98 | {384, 53}, {448, 54}, {480, 19}, {512, 55}, |
| 99 | {576, 20}, {640, 21}, {640, 56}, {768, 22}, |
| 100 | {768, 57}, {960, 23}, {896, 58}, {1024, 59}, |
| 101 | {1152, 24}, {1280, 25}, {1280, 60}, {1536, 26}, |
| 102 | {1536, 61}, {1792, 62}, {1920, 27}, {2048, 63}, |
| 103 | {2304, 28}, {2560, 29}, {3072, 30}, {3840, 31}, |
| 104 | {-1, 31} |
Timur Tabi | b301fda | 2008-03-14 17:45:29 -0500 | [diff] [blame] | 105 | }; |
Tom Rini | 56762c1 | 2017-02-09 15:40:16 -0500 | [diff] [blame^] | 106 | #endif |
Timur Tabi | b301fda | 2008-03-14 17:45:29 -0500 | [diff] [blame] | 107 | |
| 108 | /** |
| 109 | * Set the I2C bus speed for a given I2C device |
| 110 | * |
mario.six@gdsys.cc | 7d43b4e | 2016-04-25 08:31:01 +0200 | [diff] [blame] | 111 | * @param base: the I2C device registers |
Timur Tabi | b301fda | 2008-03-14 17:45:29 -0500 | [diff] [blame] | 112 | * @i2c_clk: I2C bus clock frequency |
| 113 | * @speed: the desired speed of the bus |
| 114 | * |
| 115 | * The I2C device must be stopped before calling this function. |
| 116 | * |
| 117 | * The return value is the actual bus speed that is set. |
| 118 | */ |
mario.six@gdsys.cc | 7d43b4e | 2016-04-25 08:31:01 +0200 | [diff] [blame] | 119 | static unsigned int set_i2c_bus_speed(const struct fsl_i2c_base *base, |
Timur Tabi | b301fda | 2008-03-14 17:45:29 -0500 | [diff] [blame] | 120 | unsigned int i2c_clk, unsigned int speed) |
| 121 | { |
Masahiro Yamada | db20464 | 2014-11-07 03:03:31 +0900 | [diff] [blame] | 122 | unsigned short divider = min(i2c_clk / speed, (unsigned int)USHRT_MAX); |
Timur Tabi | b301fda | 2008-03-14 17:45:29 -0500 | [diff] [blame] | 123 | |
| 124 | /* |
| 125 | * We want to choose an FDR/DFSR that generates an I2C bus speed that |
| 126 | * is equal to or lower than the requested speed. That means that we |
| 127 | * want the first divider that is equal to or greater than the |
| 128 | * calculated divider. |
| 129 | */ |
TsiChung Liew | 00648a7 | 2008-08-19 00:56:46 +0600 | [diff] [blame] | 130 | #ifdef __PPC__ |
Joakim Tjernlund | 5a6e061 | 2009-09-17 11:07:17 +0200 | [diff] [blame] | 131 | u8 dfsr, fdr = 0x31; /* Default if no FDR found */ |
| 132 | /* a, b and dfsr matches identifiers A,B and C respectively in AN2919 */ |
| 133 | unsigned short a, b, ga, gb; |
| 134 | unsigned long c_div, est_div; |
| 135 | |
Joakim Tjernlund | e677e70 | 2009-09-17 11:07:16 +0200 | [diff] [blame] | 136 | #ifdef CONFIG_FSL_I2C_CUSTOM_DFSR |
Joakim Tjernlund | 5a6e061 | 2009-09-17 11:07:17 +0200 | [diff] [blame] | 137 | dfsr = CONFIG_FSL_I2C_CUSTOM_DFSR; |
Joakim Tjernlund | e677e70 | 2009-09-17 11:07:16 +0200 | [diff] [blame] | 138 | #else |
Joakim Tjernlund | 5a6e061 | 2009-09-17 11:07:17 +0200 | [diff] [blame] | 139 | /* Condition 1: dfsr <= 50/T */ |
| 140 | dfsr = (5 * (i2c_clk / 1000)) / 100000; |
Joakim Tjernlund | e677e70 | 2009-09-17 11:07:16 +0200 | [diff] [blame] | 141 | #endif |
| 142 | #ifdef CONFIG_FSL_I2C_CUSTOM_FDR |
Joakim Tjernlund | 5a6e061 | 2009-09-17 11:07:17 +0200 | [diff] [blame] | 143 | fdr = CONFIG_FSL_I2C_CUSTOM_FDR; |
| 144 | speed = i2c_clk / divider; /* Fake something */ |
| 145 | #else |
| 146 | debug("Requested speed:%d, i2c_clk:%d\n", speed, i2c_clk); |
| 147 | if (!dfsr) |
| 148 | dfsr = 1; |
| 149 | |
| 150 | est_div = ~0; |
| 151 | for (ga = 0x4, a = 10; a <= 30; ga++, a += 2) { |
| 152 | for (gb = 0; gb < 8; gb++) { |
| 153 | b = 16 << gb; |
| 154 | c_div = b * (a + ((3*dfsr)/b)*2); |
| 155 | if ((c_div > divider) && (c_div < est_div)) { |
| 156 | unsigned short bin_gb, bin_ga; |
| 157 | |
| 158 | est_div = c_div; |
| 159 | bin_gb = gb << 2; |
| 160 | bin_ga = (ga & 0x3) | ((ga & 0x4) << 3); |
| 161 | fdr = bin_gb | bin_ga; |
| 162 | speed = i2c_clk / est_div; |
| 163 | debug("FDR:0x%.2x, div:%ld, ga:0x%x, gb:0x%x, " |
| 164 | "a:%d, b:%d, speed:%d\n", |
| 165 | fdr, est_div, ga, gb, a, b, speed); |
| 166 | /* Condition 2 not accounted for */ |
| 167 | debug("Tr <= %d ns\n", |
| 168 | (b - 3 * dfsr) * 1000000 / |
| 169 | (i2c_clk / 1000)); |
| 170 | } |
| 171 | } |
| 172 | if (a == 20) |
| 173 | a += 2; |
| 174 | if (a == 24) |
| 175 | a += 4; |
| 176 | } |
| 177 | debug("divider:%d, est_div:%ld, DFSR:%d\n", divider, est_div, dfsr); |
| 178 | debug("FDR:0x%.2x, speed:%d\n", fdr, speed); |
| 179 | #endif |
mario.six@gdsys.cc | 7d43b4e | 2016-04-25 08:31:01 +0200 | [diff] [blame] | 180 | writeb(dfsr, &base->dfsrr); /* set default filter */ |
| 181 | writeb(fdr, &base->fdr); /* set bus speed */ |
Joakim Tjernlund | e677e70 | 2009-09-17 11:07:16 +0200 | [diff] [blame] | 182 | #else |
Joakim Tjernlund | 5a6e061 | 2009-09-17 11:07:17 +0200 | [diff] [blame] | 183 | unsigned int i; |
| 184 | |
| 185 | for (i = 0; i < ARRAY_SIZE(fsl_i2c_speed_map); i++) |
| 186 | if (fsl_i2c_speed_map[i].divider >= divider) { |
| 187 | u8 fdr; |
| 188 | |
Timur Tabi | b301fda | 2008-03-14 17:45:29 -0500 | [diff] [blame] | 189 | fdr = fsl_i2c_speed_map[i].fdr; |
| 190 | speed = i2c_clk / fsl_i2c_speed_map[i].divider; |
mario.six@gdsys.cc | 7d43b4e | 2016-04-25 08:31:01 +0200 | [diff] [blame] | 191 | writeb(fdr, &base->fdr); /* set bus speed */ |
Joakim Tjernlund | e677e70 | 2009-09-17 11:07:16 +0200 | [diff] [blame] | 192 | |
Timur Tabi | b301fda | 2008-03-14 17:45:29 -0500 | [diff] [blame] | 193 | break; |
| 194 | } |
Joakim Tjernlund | 5a6e061 | 2009-09-17 11:07:17 +0200 | [diff] [blame] | 195 | #endif |
Timur Tabi | b301fda | 2008-03-14 17:45:29 -0500 | [diff] [blame] | 196 | return speed; |
| 197 | } |
| 198 | |
mario.six@gdsys.cc | 349686c | 2016-04-25 08:31:09 +0200 | [diff] [blame] | 199 | #ifndef CONFIG_DM_I2C |
Kim Phillips | dd01863 | 2012-10-16 14:28:43 +0000 | [diff] [blame] | 200 | static unsigned int get_i2c_clock(int bus) |
Jerry Huang | 5e01561 | 2011-10-26 15:29:38 +0000 | [diff] [blame] | 201 | { |
| 202 | if (bus) |
Simon Glass | c2baaec | 2012-12-13 20:48:49 +0000 | [diff] [blame] | 203 | return gd->arch.i2c2_clk; /* I2C2 clock */ |
Jerry Huang | 5e01561 | 2011-10-26 15:29:38 +0000 | [diff] [blame] | 204 | else |
Simon Glass | c2baaec | 2012-12-13 20:48:49 +0000 | [diff] [blame] | 205 | return gd->arch.i2c1_clk; /* I2C1 clock */ |
Jerry Huang | 5e01561 | 2011-10-26 15:29:38 +0000 | [diff] [blame] | 206 | } |
mario.six@gdsys.cc | 349686c | 2016-04-25 08:31:09 +0200 | [diff] [blame] | 207 | #endif |
Jerry Huang | 5e01561 | 2011-10-26 15:29:38 +0000 | [diff] [blame] | 208 | |
mario.six@gdsys.cc | 7d43b4e | 2016-04-25 08:31:01 +0200 | [diff] [blame] | 209 | static int fsl_i2c_fixup(const struct fsl_i2c_base *base) |
Chunhe Lan | 2e13d57 | 2013-08-16 15:10:36 +0800 | [diff] [blame] | 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 | |
mario.six@gdsys.cc | 7d43b4e | 2016-04-25 08:31:01 +0200 | [diff] [blame] | 223 | writeb(I2C_CR_MEN | I2C_CR_MSTA, &base->cr); |
Chunhe Lan | 2e13d57 | 2013-08-16 15:10:36 +0800 | [diff] [blame] | 224 | |
| 225 | timeval = get_ticks(); |
mario.six@gdsys.cc | 7d43b4e | 2016-04-25 08:31:01 +0200 | [diff] [blame] | 226 | while (!(readb(&base->sr) & I2C_SR_MBB)) { |
Chunhe Lan | 2e13d57 | 2013-08-16 15:10:36 +0800 | [diff] [blame] | 227 | if ((get_ticks() - timeval) > timeout) |
| 228 | goto err; |
| 229 | } |
| 230 | |
mario.six@gdsys.cc | 7d43b4e | 2016-04-25 08:31:01 +0200 | [diff] [blame] | 231 | if (readb(&base->sr) & I2C_SR_MAL) { |
Chunhe Lan | 2e13d57 | 2013-08-16 15:10:36 +0800 | [diff] [blame] | 232 | /* SDA is stuck low */ |
mario.six@gdsys.cc | 7d43b4e | 2016-04-25 08:31:01 +0200 | [diff] [blame] | 233 | writeb(0, &base->cr); |
Chunhe Lan | 2e13d57 | 2013-08-16 15:10:36 +0800 | [diff] [blame] | 234 | udelay(100); |
mario.six@gdsys.cc | 7d43b4e | 2016-04-25 08:31:01 +0200 | [diff] [blame] | 235 | writeb(I2C_CR_MSTA | flags, &base->cr); |
| 236 | writeb(I2C_CR_MEN | I2C_CR_MSTA | flags, &base->cr); |
Chunhe Lan | 2e13d57 | 2013-08-16 15:10:36 +0800 | [diff] [blame] | 237 | } |
| 238 | |
mario.six@gdsys.cc | 7d43b4e | 2016-04-25 08:31:01 +0200 | [diff] [blame] | 239 | readb(&base->dr); |
Chunhe Lan | 2e13d57 | 2013-08-16 15:10:36 +0800 | [diff] [blame] | 240 | |
| 241 | timeval = get_ticks(); |
mario.six@gdsys.cc | 7d43b4e | 2016-04-25 08:31:01 +0200 | [diff] [blame] | 242 | while (!(readb(&base->sr) & I2C_SR_MIF)) { |
Chunhe Lan | 2e13d57 | 2013-08-16 15:10:36 +0800 | [diff] [blame] | 243 | if ((get_ticks() - timeval) > timeout) |
| 244 | goto err; |
| 245 | } |
| 246 | ret = 0; |
| 247 | |
| 248 | err: |
mario.six@gdsys.cc | 7d43b4e | 2016-04-25 08:31:01 +0200 | [diff] [blame] | 249 | writeb(I2C_CR_MEN | flags, &base->cr); |
| 250 | writeb(0, &base->sr); |
Chunhe Lan | 2e13d57 | 2013-08-16 15:10:36 +0800 | [diff] [blame] | 251 | udelay(100); |
| 252 | |
| 253 | return ret; |
| 254 | } |
| 255 | |
mario.six@gdsys.cc | 416b2dc | 2016-04-25 08:31:08 +0200 | [diff] [blame] | 256 | static void __i2c_init(const struct fsl_i2c_base *base, int speed, int |
| 257 | slaveadd, int i2c_clk, int busnum) |
Jon Loeliger | e4773be | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 258 | { |
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 |
mario.six@gdsys.cc | 7d43b4e | 2016-04-25 08:31:01 +0200 | [diff] [blame] | 269 | writeb(0, &base->cr); /* stop I2C controller */ |
Heiko Schocher | f285074 | 2012-10-24 13:48:22 +0200 | [diff] [blame] | 270 | udelay(5); /* let it shutdown in peace */ |
mario.six@gdsys.cc | 416b2dc | 2016-04-25 08:31:08 +0200 | [diff] [blame] | 271 | set_i2c_bus_speed(base, i2c_clk, speed); |
mario.six@gdsys.cc | 7d43b4e | 2016-04-25 08:31:01 +0200 | [diff] [blame] | 272 | writeb(slaveadd << 1, &base->adr);/* write slave address */ |
| 273 | writeb(0x0, &base->sr); /* clear status register */ |
| 274 | writeb(I2C_CR_MEN, &base->cr); /* start I2C controller */ |
Richard Retanubun | df0149c | 2010-04-12 15:08:17 -0400 | [diff] [blame] | 275 | |
Chunhe Lan | 2e13d57 | 2013-08-16 15:10:36 +0800 | [diff] [blame] | 276 | timeval = get_ticks(); |
mario.six@gdsys.cc | 7d43b4e | 2016-04-25 08:31:01 +0200 | [diff] [blame] | 277 | while (readb(&base->sr) & I2C_SR_MBB) { |
Chunhe Lan | 2e13d57 | 2013-08-16 15:10:36 +0800 | [diff] [blame] | 278 | if ((get_ticks() - timeval) < timeout) |
| 279 | continue; |
| 280 | |
mario.six@gdsys.cc | 7d43b4e | 2016-04-25 08:31:01 +0200 | [diff] [blame] | 281 | if (fsl_i2c_fixup(base)) |
Chunhe Lan | 2e13d57 | 2013-08-16 15:10:36 +0800 | [diff] [blame] | 282 | debug("i2c_init: BUS#%d failed to init\n", |
mario.six@gdsys.cc | 416b2dc | 2016-04-25 08:31:08 +0200 | [diff] [blame] | 283 | busnum); |
Chunhe Lan | 2e13d57 | 2013-08-16 15:10:36 +0800 | [diff] [blame] | 284 | |
| 285 | break; |
| 286 | } |
| 287 | |
Richard Retanubun | df0149c | 2010-04-12 15:08:17 -0400 | [diff] [blame] | 288 | #ifdef CONFIG_SYS_I2C_BOARD_LATE_INIT |
| 289 | /* Call board specific i2c bus reset routine AFTER the bus has been |
| 290 | * initialized. Use either this callpoint or i2c_init_board; |
| 291 | * which is called before i2c_init operations. |
| 292 | * For details about this problem see doc/I2C_Edge_Conditions. |
| 293 | */ |
| 294 | i2c_board_late_init(); |
| 295 | #endif |
Jon Loeliger | e4773be | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 296 | } |
| 297 | |
Joakim Tjernlund | c324b78 | 2009-09-17 11:07:15 +0200 | [diff] [blame] | 298 | static int |
mario.six@gdsys.cc | 416b2dc | 2016-04-25 08:31:08 +0200 | [diff] [blame] | 299 | i2c_wait4bus(const struct fsl_i2c_base *base) |
Jon Loeliger | e4773be | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 300 | { |
Stefan Roese | 3762825 | 2008-08-06 14:05:38 +0200 | [diff] [blame] | 301 | unsigned long long timeval = get_ticks(); |
Timur Tabi | 2165c62 | 2009-09-04 16:28:35 -0500 | [diff] [blame] | 302 | const unsigned long long timeout = usec2ticks(CONFIG_I2C_MBB_TIMEOUT); |
Jon Loeliger | e4773be | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 303 | |
mario.six@gdsys.cc | 7d43b4e | 2016-04-25 08:31:01 +0200 | [diff] [blame] | 304 | while (readb(&base->sr) & I2C_SR_MBB) { |
Timur Tabi | 2165c62 | 2009-09-04 16:28:35 -0500 | [diff] [blame] | 305 | if ((get_ticks() - timeval) > timeout) |
Jon Loeliger | e4773be | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 306 | return -1; |
Jon Loeliger | e4773be | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | return 0; |
| 310 | } |
| 311 | |
mario.six@gdsys.cc | 416b2dc | 2016-04-25 08:31:08 +0200 | [diff] [blame] | 312 | static inline int |
| 313 | i2c_wait(const struct fsl_i2c_base *base, int write) |
Jon Loeliger | e4773be | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 314 | { |
| 315 | u32 csr; |
Stefan Roese | 3762825 | 2008-08-06 14:05:38 +0200 | [diff] [blame] | 316 | unsigned long long timeval = get_ticks(); |
Timur Tabi | 2165c62 | 2009-09-04 16:28:35 -0500 | [diff] [blame] | 317 | const unsigned long long timeout = usec2ticks(CONFIG_I2C_TIMEOUT); |
Jon Loeliger | e4773be | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 318 | |
| 319 | do { |
mario.six@gdsys.cc | 7d43b4e | 2016-04-25 08:31:01 +0200 | [diff] [blame] | 320 | csr = readb(&base->sr); |
Jon Loeliger | e4773be | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 321 | if (!(csr & I2C_SR_MIF)) |
| 322 | continue; |
Joakim Tjernlund | c324b78 | 2009-09-17 11:07:15 +0200 | [diff] [blame] | 323 | /* Read again to allow register to stabilise */ |
mario.six@gdsys.cc | 7d43b4e | 2016-04-25 08:31:01 +0200 | [diff] [blame] | 324 | csr = readb(&base->sr); |
Jon Loeliger | e4773be | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 325 | |
mario.six@gdsys.cc | 7d43b4e | 2016-04-25 08:31:01 +0200 | [diff] [blame] | 326 | writeb(0x0, &base->sr); |
Jon Loeliger | e4773be | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 327 | |
| 328 | if (csr & I2C_SR_MAL) { |
| 329 | debug("i2c_wait: MAL\n"); |
| 330 | return -1; |
| 331 | } |
| 332 | |
| 333 | if (!(csr & I2C_SR_MCF)) { |
| 334 | debug("i2c_wait: unfinished\n"); |
| 335 | return -1; |
| 336 | } |
| 337 | |
Joakim Tjernlund | c32c5f7 | 2006-11-28 16:17:27 -0600 | [diff] [blame] | 338 | if (write == I2C_WRITE_BIT && (csr & I2C_SR_RXAK)) { |
Jon Loeliger | e4773be | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 339 | debug("i2c_wait: No RXACK\n"); |
| 340 | return -1; |
| 341 | } |
| 342 | |
| 343 | return 0; |
Timur Tabi | 2165c62 | 2009-09-04 16:28:35 -0500 | [diff] [blame] | 344 | } while ((get_ticks() - timeval) < timeout); |
Jon Loeliger | e4773be | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 345 | |
| 346 | debug("i2c_wait: timed out\n"); |
| 347 | return -1; |
| 348 | } |
| 349 | |
mario.six@gdsys.cc | 416b2dc | 2016-04-25 08:31:08 +0200 | [diff] [blame] | 350 | static inline int |
| 351 | i2c_write_addr(const struct fsl_i2c_base *base, u8 dev, u8 dir, int rsta) |
Jon Loeliger | e4773be | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 352 | { |
| 353 | writeb(I2C_CR_MEN | I2C_CR_MSTA | I2C_CR_MTX |
| 354 | | (rsta ? I2C_CR_RSTA : 0), |
mario.six@gdsys.cc | 7d43b4e | 2016-04-25 08:31:01 +0200 | [diff] [blame] | 355 | &base->cr); |
Jon Loeliger | e4773be | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 356 | |
mario.six@gdsys.cc | 7d43b4e | 2016-04-25 08:31:01 +0200 | [diff] [blame] | 357 | writeb((dev << 1) | dir, &base->dr); |
Jon Loeliger | e4773be | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 358 | |
mario.six@gdsys.cc | 416b2dc | 2016-04-25 08:31:08 +0200 | [diff] [blame] | 359 | if (i2c_wait(base, I2C_WRITE_BIT) < 0) |
Jon Loeliger | e4773be | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 360 | return 0; |
| 361 | |
| 362 | return 1; |
| 363 | } |
| 364 | |
mario.six@gdsys.cc | 416b2dc | 2016-04-25 08:31:08 +0200 | [diff] [blame] | 365 | static inline int |
| 366 | __i2c_write_data(const struct fsl_i2c_base *base, u8 *data, int length) |
Jon Loeliger | e4773be | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 367 | { |
| 368 | int i; |
| 369 | |
Jon Loeliger | e4773be | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 370 | for (i = 0; i < length; i++) { |
mario.six@gdsys.cc | 7d43b4e | 2016-04-25 08:31:01 +0200 | [diff] [blame] | 371 | writeb(data[i], &base->dr); |
Jon Loeliger | e4773be | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 372 | |
mario.six@gdsys.cc | 416b2dc | 2016-04-25 08:31:08 +0200 | [diff] [blame] | 373 | if (i2c_wait(base, I2C_WRITE_BIT) < 0) |
Jon Loeliger | e4773be | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 374 | break; |
| 375 | } |
| 376 | |
| 377 | return i; |
| 378 | } |
| 379 | |
mario.six@gdsys.cc | 416b2dc | 2016-04-25 08:31:08 +0200 | [diff] [blame] | 380 | static inline int |
| 381 | __i2c_read_data(const struct fsl_i2c_base *base, u8 *data, int length) |
Jon Loeliger | e4773be | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 382 | { |
| 383 | int i; |
| 384 | |
| 385 | writeb(I2C_CR_MEN | I2C_CR_MSTA | ((length == 1) ? I2C_CR_TXAK : 0), |
mario.six@gdsys.cc | 7d43b4e | 2016-04-25 08:31:01 +0200 | [diff] [blame] | 386 | &base->cr); |
Jon Loeliger | e4773be | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 387 | |
| 388 | /* dummy read */ |
mario.six@gdsys.cc | 7d43b4e | 2016-04-25 08:31:01 +0200 | [diff] [blame] | 389 | readb(&base->dr); |
Jon Loeliger | e4773be | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 390 | |
| 391 | for (i = 0; i < length; i++) { |
mario.six@gdsys.cc | 416b2dc | 2016-04-25 08:31:08 +0200 | [diff] [blame] | 392 | if (i2c_wait(base, I2C_READ_BIT) < 0) |
Jon Loeliger | e4773be | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 393 | break; |
| 394 | |
| 395 | /* Generate ack on last next to last byte */ |
| 396 | if (i == length - 2) |
| 397 | writeb(I2C_CR_MEN | I2C_CR_MSTA | I2C_CR_TXAK, |
mario.six@gdsys.cc | 7d43b4e | 2016-04-25 08:31:01 +0200 | [diff] [blame] | 398 | &base->cr); |
Jon Loeliger | e4773be | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 399 | |
Joakim Tjernlund | 6384da2 | 2009-09-22 13:40:44 +0200 | [diff] [blame] | 400 | /* Do not generate stop on last byte */ |
Jon Loeliger | e4773be | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 401 | if (i == length - 1) |
Joakim Tjernlund | 6384da2 | 2009-09-22 13:40:44 +0200 | [diff] [blame] | 402 | writeb(I2C_CR_MEN | I2C_CR_MSTA | I2C_CR_MTX, |
mario.six@gdsys.cc | 7d43b4e | 2016-04-25 08:31:01 +0200 | [diff] [blame] | 403 | &base->cr); |
Jon Loeliger | e4773be | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 404 | |
mario.six@gdsys.cc | 7d43b4e | 2016-04-25 08:31:01 +0200 | [diff] [blame] | 405 | data[i] = readb(&base->dr); |
Jon Loeliger | e4773be | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 406 | } |
| 407 | |
| 408 | return i; |
| 409 | } |
| 410 | |
Heiko Schocher | f285074 | 2012-10-24 13:48:22 +0200 | [diff] [blame] | 411 | static int |
mario.six@gdsys.cc | 416b2dc | 2016-04-25 08:31:08 +0200 | [diff] [blame] | 412 | __i2c_read(const struct fsl_i2c_base *base, u8 chip_addr, u8 *offset, int olen, |
mario.six@gdsys.cc | 2d96aa7 | 2016-04-25 08:31:07 +0200 | [diff] [blame] | 413 | u8 *data, int dlen) |
Jon Loeliger | e4773be | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 414 | { |
mario.six@gdsys.cc | a447265 | 2016-04-25 08:31:02 +0200 | [diff] [blame] | 415 | int ret = -1; /* signal error */ |
Jon Loeliger | e4773be | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 416 | |
mario.six@gdsys.cc | 416b2dc | 2016-04-25 08:31:08 +0200 | [diff] [blame] | 417 | if (i2c_wait4bus(base) < 0) |
Reinhard Pfau | 2d878de | 2013-06-26 15:55:14 +0200 | [diff] [blame] | 418 | return -1; |
| 419 | |
mario.six@gdsys.cc | 2eae9d0 | 2016-04-25 08:31:03 +0200 | [diff] [blame] | 420 | /* Some drivers use offset lengths in excess of 4 bytes. These drivers |
| 421 | * adhere to the following convention: |
| 422 | * - the offset length is passed as negative (that is, the absolute |
| 423 | * value of olen is the actual offset length) |
| 424 | * - the offset itself is passed in data, which is overwritten by the |
| 425 | * subsequent read operation |
Shaveta Leekha | fdd7efe | 2014-04-24 14:51:23 +0530 | [diff] [blame] | 426 | */ |
mario.six@gdsys.cc | a447265 | 2016-04-25 08:31:02 +0200 | [diff] [blame] | 427 | if (olen < 0) { |
mario.six@gdsys.cc | 416b2dc | 2016-04-25 08:31:08 +0200 | [diff] [blame] | 428 | if (i2c_write_addr(base, chip_addr, I2C_WRITE_BIT, 0) != 0) |
| 429 | ret = __i2c_write_data(base, data, -olen); |
Shaveta Leekha | fdd7efe | 2014-04-24 14:51:23 +0530 | [diff] [blame] | 430 | |
mario.six@gdsys.cc | 8230fc4 | 2016-04-25 08:31:04 +0200 | [diff] [blame] | 431 | if (ret != -olen) |
Shaveta Leekha | fdd7efe | 2014-04-24 14:51:23 +0530 | [diff] [blame] | 432 | return -1; |
| 433 | |
mario.six@gdsys.cc | 416b2dc | 2016-04-25 08:31:08 +0200 | [diff] [blame] | 434 | if (dlen && i2c_write_addr(base, chip_addr, |
mario.six@gdsys.cc | a447265 | 2016-04-25 08:31:02 +0200 | [diff] [blame] | 435 | I2C_READ_BIT, 1) != 0) |
mario.six@gdsys.cc | 416b2dc | 2016-04-25 08:31:08 +0200 | [diff] [blame] | 436 | ret = __i2c_read_data(base, data, dlen); |
Shaveta Leekha | fdd7efe | 2014-04-24 14:51:23 +0530 | [diff] [blame] | 437 | } else { |
mario.six@gdsys.cc | a447265 | 2016-04-25 08:31:02 +0200 | [diff] [blame] | 438 | if ((!dlen || olen > 0) && |
mario.six@gdsys.cc | 416b2dc | 2016-04-25 08:31:08 +0200 | [diff] [blame] | 439 | i2c_write_addr(base, chip_addr, I2C_WRITE_BIT, 0) != 0 && |
| 440 | __i2c_write_data(base, offset, olen) == olen) |
mario.six@gdsys.cc | a447265 | 2016-04-25 08:31:02 +0200 | [diff] [blame] | 441 | ret = 0; /* No error so far */ |
Joakim Tjernlund | b648fe7 | 2007-01-31 11:04:19 +0100 | [diff] [blame] | 442 | |
mario.six@gdsys.cc | 416b2dc | 2016-04-25 08:31:08 +0200 | [diff] [blame] | 443 | if (dlen && i2c_write_addr(base, chip_addr, I2C_READ_BIT, |
mario.six@gdsys.cc | a447265 | 2016-04-25 08:31:02 +0200 | [diff] [blame] | 444 | olen ? 1 : 0) != 0) |
mario.six@gdsys.cc | 416b2dc | 2016-04-25 08:31:08 +0200 | [diff] [blame] | 445 | ret = __i2c_read_data(base, data, dlen); |
Shaveta Leekha | fdd7efe | 2014-04-24 14:51:23 +0530 | [diff] [blame] | 446 | } |
Jon Loeliger | e4773be | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 447 | |
mario.six@gdsys.cc | 7d43b4e | 2016-04-25 08:31:01 +0200 | [diff] [blame] | 448 | writeb(I2C_CR_MEN, &base->cr); |
Jon Loeliger | e4773be | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 449 | |
mario.six@gdsys.cc | 416b2dc | 2016-04-25 08:31:08 +0200 | [diff] [blame] | 450 | if (i2c_wait4bus(base)) /* Wait until STOP */ |
Joakim Tjernlund | 6384da2 | 2009-09-22 13:40:44 +0200 | [diff] [blame] | 451 | debug("i2c_read: wait4bus timed out\n"); |
| 452 | |
mario.six@gdsys.cc | a447265 | 2016-04-25 08:31:02 +0200 | [diff] [blame] | 453 | if (ret == dlen) |
| 454 | return 0; |
Jon Loeliger | 24df977 | 2006-10-19 12:02:24 -0500 | [diff] [blame] | 455 | |
| 456 | return -1; |
Jon Loeliger | e4773be | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 457 | } |
| 458 | |
Heiko Schocher | f285074 | 2012-10-24 13:48:22 +0200 | [diff] [blame] | 459 | static int |
mario.six@gdsys.cc | 416b2dc | 2016-04-25 08:31:08 +0200 | [diff] [blame] | 460 | __i2c_write(const struct fsl_i2c_base *base, u8 chip_addr, u8 *offset, int olen, |
mario.six@gdsys.cc | 2d96aa7 | 2016-04-25 08:31:07 +0200 | [diff] [blame] | 461 | u8 *data, int dlen) |
Jon Loeliger | e4773be | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 462 | { |
mario.six@gdsys.cc | a447265 | 2016-04-25 08:31:02 +0200 | [diff] [blame] | 463 | int ret = -1; /* signal error */ |
Jon Loeliger | e4773be | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 464 | |
mario.six@gdsys.cc | 416b2dc | 2016-04-25 08:31:08 +0200 | [diff] [blame] | 465 | if (i2c_wait4bus(base) < 0) |
Chunhe Lan | 2e13d57 | 2013-08-16 15:10:36 +0800 | [diff] [blame] | 466 | return -1; |
| 467 | |
mario.six@gdsys.cc | 416b2dc | 2016-04-25 08:31:08 +0200 | [diff] [blame] | 468 | if (i2c_write_addr(base, chip_addr, I2C_WRITE_BIT, 0) != 0 && |
| 469 | __i2c_write_data(base, offset, olen) == olen) { |
| 470 | ret = __i2c_write_data(base, data, dlen); |
Jon Loeliger | 24df977 | 2006-10-19 12:02:24 -0500 | [diff] [blame] | 471 | } |
Jon Loeliger | e4773be | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 472 | |
mario.six@gdsys.cc | 7d43b4e | 2016-04-25 08:31:01 +0200 | [diff] [blame] | 473 | writeb(I2C_CR_MEN, &base->cr); |
mario.six@gdsys.cc | 416b2dc | 2016-04-25 08:31:08 +0200 | [diff] [blame] | 474 | if (i2c_wait4bus(base)) /* Wait until STOP */ |
Joakim Tjernlund | c324b78 | 2009-09-17 11:07:15 +0200 | [diff] [blame] | 475 | debug("i2c_write: wait4bus timed out\n"); |
Jon Loeliger | e4773be | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 476 | |
mario.six@gdsys.cc | a447265 | 2016-04-25 08:31:02 +0200 | [diff] [blame] | 477 | if (ret == dlen) |
| 478 | return 0; |
Jon Loeliger | 24df977 | 2006-10-19 12:02:24 -0500 | [diff] [blame] | 479 | |
| 480 | return -1; |
Jon Loeliger | e4773be | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 481 | } |
| 482 | |
Heiko Schocher | f285074 | 2012-10-24 13:48:22 +0200 | [diff] [blame] | 483 | static int |
mario.six@gdsys.cc | 416b2dc | 2016-04-25 08:31:08 +0200 | [diff] [blame] | 484 | __i2c_probe_chip(const struct fsl_i2c_base *base, uchar chip) |
Jon Loeliger | e4773be | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 485 | { |
Joakim Tjernlund | b648fe7 | 2007-01-31 11:04:19 +0100 | [diff] [blame] | 486 | /* For unknow reason the controller will ACK when |
| 487 | * probing for a slave with the same address, so skip |
| 488 | * it. |
Jon Loeliger | e4773be | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 489 | */ |
mario.six@gdsys.cc | 7d43b4e | 2016-04-25 08:31:01 +0200 | [diff] [blame] | 490 | if (chip == (readb(&base->adr) >> 1)) |
Timur Tabi | ab34754 | 2006-11-03 19:15:00 -0600 | [diff] [blame] | 491 | return -1; |
Timur Tabi | ab34754 | 2006-11-03 19:15:00 -0600 | [diff] [blame] | 492 | |
mario.six@gdsys.cc | 416b2dc | 2016-04-25 08:31:08 +0200 | [diff] [blame] | 493 | return __i2c_read(base, chip, 0, 0, NULL, 0); |
Timur Tabi | ab34754 | 2006-11-03 19:15:00 -0600 | [diff] [blame] | 494 | } |
| 495 | |
mario.six@gdsys.cc | 416b2dc | 2016-04-25 08:31:08 +0200 | [diff] [blame] | 496 | static unsigned int __i2c_set_bus_speed(const struct fsl_i2c_base *base, |
| 497 | unsigned int speed, int i2c_clk) |
Timur Tabi | ab34754 | 2006-11-03 19:15:00 -0600 | [diff] [blame] | 498 | { |
mario.six@gdsys.cc | 7d43b4e | 2016-04-25 08:31:01 +0200 | [diff] [blame] | 499 | writeb(0, &base->cr); /* stop controller */ |
mario.six@gdsys.cc | 416b2dc | 2016-04-25 08:31:08 +0200 | [diff] [blame] | 500 | set_i2c_bus_speed(base, i2c_clk, speed); |
mario.six@gdsys.cc | 7d43b4e | 2016-04-25 08:31:01 +0200 | [diff] [blame] | 501 | writeb(I2C_CR_MEN, &base->cr); /* start controller */ |
Timur Tabi | b301fda | 2008-03-14 17:45:29 -0500 | [diff] [blame] | 502 | |
| 503 | return 0; |
Timur Tabi | ab34754 | 2006-11-03 19:15:00 -0600 | [diff] [blame] | 504 | } |
| 505 | |
mario.six@gdsys.cc | 349686c | 2016-04-25 08:31:09 +0200 | [diff] [blame] | 506 | #ifndef CONFIG_DM_I2C |
mario.six@gdsys.cc | 2d96aa7 | 2016-04-25 08:31:07 +0200 | [diff] [blame] | 507 | static void fsl_i2c_init(struct i2c_adapter *adap, int speed, int slaveadd) |
| 508 | { |
mario.six@gdsys.cc | 416b2dc | 2016-04-25 08:31:08 +0200 | [diff] [blame] | 509 | __i2c_init(i2c_base[adap->hwadapnr], speed, slaveadd, |
| 510 | get_i2c_clock(adap->hwadapnr), adap->hwadapnr); |
mario.six@gdsys.cc | 2d96aa7 | 2016-04-25 08:31:07 +0200 | [diff] [blame] | 511 | } |
| 512 | |
| 513 | static int |
| 514 | fsl_i2c_probe_chip(struct i2c_adapter *adap, uchar chip) |
| 515 | { |
mario.six@gdsys.cc | 416b2dc | 2016-04-25 08:31:08 +0200 | [diff] [blame] | 516 | return __i2c_probe_chip(i2c_base[adap->hwadapnr], chip); |
mario.six@gdsys.cc | 2d96aa7 | 2016-04-25 08:31:07 +0200 | [diff] [blame] | 517 | } |
| 518 | |
| 519 | static int |
| 520 | fsl_i2c_read(struct i2c_adapter *adap, u8 chip_addr, uint offset, int olen, |
| 521 | u8 *data, int dlen) |
| 522 | { |
mario.six@gdsys.cc | 416b2dc | 2016-04-25 08:31:08 +0200 | [diff] [blame] | 523 | u8 *o = (u8 *)&offset; |
| 524 | return __i2c_read(i2c_base[adap->hwadapnr], chip_addr, &o[4 - olen], |
| 525 | olen, data, dlen); |
mario.six@gdsys.cc | 2d96aa7 | 2016-04-25 08:31:07 +0200 | [diff] [blame] | 526 | } |
| 527 | |
| 528 | static int |
| 529 | fsl_i2c_write(struct i2c_adapter *adap, u8 chip_addr, uint offset, int olen, |
| 530 | u8 *data, int dlen) |
| 531 | { |
mario.six@gdsys.cc | 416b2dc | 2016-04-25 08:31:08 +0200 | [diff] [blame] | 532 | u8 *o = (u8 *)&offset; |
| 533 | return __i2c_write(i2c_base[adap->hwadapnr], chip_addr, &o[4 - olen], |
| 534 | olen, data, dlen); |
mario.six@gdsys.cc | 2d96aa7 | 2016-04-25 08:31:07 +0200 | [diff] [blame] | 535 | } |
| 536 | |
| 537 | static unsigned int fsl_i2c_set_bus_speed(struct i2c_adapter *adap, |
| 538 | unsigned int speed) |
| 539 | { |
mario.six@gdsys.cc | 416b2dc | 2016-04-25 08:31:08 +0200 | [diff] [blame] | 540 | return __i2c_set_bus_speed(i2c_base[adap->hwadapnr], speed, |
| 541 | get_i2c_clock(adap->hwadapnr)); |
mario.six@gdsys.cc | 2d96aa7 | 2016-04-25 08:31:07 +0200 | [diff] [blame] | 542 | } |
| 543 | |
Heiko Schocher | f285074 | 2012-10-24 13:48:22 +0200 | [diff] [blame] | 544 | /* |
| 545 | * Register fsl i2c adapters |
| 546 | */ |
mario.six@gdsys.cc | 4a79069 | 2016-04-25 08:31:05 +0200 | [diff] [blame] | 547 | U_BOOT_I2C_ADAP_COMPLETE(fsl_0, fsl_i2c_init, fsl_i2c_probe_chip, fsl_i2c_read, |
Heiko Schocher | f285074 | 2012-10-24 13:48:22 +0200 | [diff] [blame] | 548 | fsl_i2c_write, fsl_i2c_set_bus_speed, |
| 549 | CONFIG_SYS_FSL_I2C_SPEED, CONFIG_SYS_FSL_I2C_SLAVE, |
| 550 | 0) |
| 551 | #ifdef CONFIG_SYS_FSL_I2C2_OFFSET |
mario.six@gdsys.cc | 4a79069 | 2016-04-25 08:31:05 +0200 | [diff] [blame] | 552 | U_BOOT_I2C_ADAP_COMPLETE(fsl_1, fsl_i2c_init, fsl_i2c_probe_chip, fsl_i2c_read, |
Heiko Schocher | f285074 | 2012-10-24 13:48:22 +0200 | [diff] [blame] | 553 | fsl_i2c_write, fsl_i2c_set_bus_speed, |
| 554 | CONFIG_SYS_FSL_I2C2_SPEED, CONFIG_SYS_FSL_I2C2_SLAVE, |
| 555 | 1) |
Heiko Schocher | 2c9f3a4 | 2009-02-24 11:30:37 +0100 | [diff] [blame] | 556 | #endif |
Shengzhou Liu | 37787f6 | 2014-07-07 12:17:48 +0800 | [diff] [blame] | 557 | #ifdef CONFIG_SYS_FSL_I2C3_OFFSET |
mario.six@gdsys.cc | 4a79069 | 2016-04-25 08:31:05 +0200 | [diff] [blame] | 558 | U_BOOT_I2C_ADAP_COMPLETE(fsl_2, fsl_i2c_init, fsl_i2c_probe_chip, fsl_i2c_read, |
Shengzhou Liu | 37787f6 | 2014-07-07 12:17:48 +0800 | [diff] [blame] | 559 | fsl_i2c_write, fsl_i2c_set_bus_speed, |
| 560 | CONFIG_SYS_FSL_I2C3_SPEED, CONFIG_SYS_FSL_I2C3_SLAVE, |
| 561 | 2) |
| 562 | #endif |
| 563 | #ifdef CONFIG_SYS_FSL_I2C4_OFFSET |
mario.six@gdsys.cc | 4a79069 | 2016-04-25 08:31:05 +0200 | [diff] [blame] | 564 | U_BOOT_I2C_ADAP_COMPLETE(fsl_3, fsl_i2c_init, fsl_i2c_probe_chip, fsl_i2c_read, |
Shengzhou Liu | 37787f6 | 2014-07-07 12:17:48 +0800 | [diff] [blame] | 565 | fsl_i2c_write, fsl_i2c_set_bus_speed, |
| 566 | CONFIG_SYS_FSL_I2C4_SPEED, CONFIG_SYS_FSL_I2C4_SLAVE, |
| 567 | 3) |
| 568 | #endif |
mario.six@gdsys.cc | 349686c | 2016-04-25 08:31:09 +0200 | [diff] [blame] | 569 | #else /* CONFIG_DM_I2C */ |
| 570 | static int fsl_i2c_probe_chip(struct udevice *bus, u32 chip_addr, |
| 571 | u32 chip_flags) |
| 572 | { |
| 573 | struct fsl_i2c_dev *dev = dev_get_priv(bus); |
| 574 | return __i2c_probe_chip(dev->base, chip_addr); |
| 575 | } |
| 576 | |
| 577 | static int fsl_i2c_set_bus_speed(struct udevice *bus, unsigned int speed) |
| 578 | { |
| 579 | struct fsl_i2c_dev *dev = dev_get_priv(bus); |
| 580 | return __i2c_set_bus_speed(dev->base, speed, dev->i2c_clk); |
| 581 | } |
| 582 | |
| 583 | static int fsl_i2c_ofdata_to_platdata(struct udevice *bus) |
| 584 | { |
| 585 | struct fsl_i2c_dev *dev = dev_get_priv(bus); |
mario.six@gdsys.cc | 6e6dce6 | 2016-05-23 10:12:11 +0200 | [diff] [blame] | 586 | fdt_addr_t addr; |
| 587 | fdt_size_t size; |
Simon Glass | dd79d6e | 2017-01-17 16:52:55 -0700 | [diff] [blame] | 588 | int node = dev_of_offset(bus); |
mario.six@gdsys.cc | 349686c | 2016-04-25 08:31:09 +0200 | [diff] [blame] | 589 | |
Simon Glass | dd79d6e | 2017-01-17 16:52:55 -0700 | [diff] [blame] | 590 | addr = fdtdec_get_addr_size_auto_noparent(gd->fdt_blob, node, "reg", 0, |
| 591 | &size, false); |
mario.six@gdsys.cc | 349686c | 2016-04-25 08:31:09 +0200 | [diff] [blame] | 592 | |
| 593 | dev->base = map_sysmem(CONFIG_SYS_IMMR + addr, size); |
| 594 | |
| 595 | if (!dev->base) |
| 596 | return -ENOMEM; |
| 597 | |
Simon Glass | dd79d6e | 2017-01-17 16:52:55 -0700 | [diff] [blame] | 598 | dev->index = fdtdec_get_int(gd->fdt_blob, node, "cell-index", -1); |
| 599 | dev->slaveadd = fdtdec_get_int(gd->fdt_blob, node, |
mario.six@gdsys.cc | 349686c | 2016-04-25 08:31:09 +0200 | [diff] [blame] | 600 | "u-boot,i2c-slave-addr", 0x7f); |
Simon Glass | dd79d6e | 2017-01-17 16:52:55 -0700 | [diff] [blame] | 601 | dev->speed = fdtdec_get_int(gd->fdt_blob, node, "clock-frequency", |
| 602 | 400000); |
mario.six@gdsys.cc | 349686c | 2016-04-25 08:31:09 +0200 | [diff] [blame] | 603 | |
| 604 | dev->i2c_clk = dev->index ? gd->arch.i2c2_clk : gd->arch.i2c1_clk; |
| 605 | |
| 606 | return 0; |
| 607 | } |
| 608 | |
| 609 | static int fsl_i2c_probe(struct udevice *bus) |
| 610 | { |
| 611 | struct fsl_i2c_dev *dev = dev_get_priv(bus); |
| 612 | __i2c_init(dev->base, dev->speed, dev->slaveadd, dev->i2c_clk, |
| 613 | dev->index); |
| 614 | return 0; |
| 615 | } |
| 616 | |
| 617 | static int fsl_i2c_xfer(struct udevice *bus, struct i2c_msg *msg, int nmsgs) |
| 618 | { |
| 619 | struct fsl_i2c_dev *dev = dev_get_priv(bus); |
| 620 | struct i2c_msg *dmsg, *omsg, dummy; |
| 621 | |
| 622 | memset(&dummy, 0, sizeof(struct i2c_msg)); |
| 623 | |
| 624 | /* We expect either two messages (one with an offset and one with the |
| 625 | * actucal data) or one message (just data) */ |
| 626 | if (nmsgs > 2 || nmsgs == 0) { |
| 627 | debug("%s: Only one or two messages are supported.", __func__); |
| 628 | return -1; |
| 629 | } |
| 630 | |
| 631 | omsg = nmsgs == 1 ? &dummy : msg; |
| 632 | dmsg = nmsgs == 1 ? msg : msg + 1; |
| 633 | |
| 634 | if (dmsg->flags & I2C_M_RD) |
| 635 | return __i2c_read(dev->base, dmsg->addr, omsg->buf, omsg->len, |
| 636 | dmsg->buf, dmsg->len); |
| 637 | else |
| 638 | return __i2c_write(dev->base, dmsg->addr, omsg->buf, omsg->len, |
| 639 | dmsg->buf, dmsg->len); |
| 640 | } |
| 641 | |
| 642 | static const struct dm_i2c_ops fsl_i2c_ops = { |
| 643 | .xfer = fsl_i2c_xfer, |
| 644 | .probe_chip = fsl_i2c_probe_chip, |
| 645 | .set_bus_speed = fsl_i2c_set_bus_speed, |
| 646 | }; |
| 647 | |
| 648 | static const struct udevice_id fsl_i2c_ids[] = { |
| 649 | { .compatible = "fsl-i2c", }, |
| 650 | { /* sentinel */ } |
| 651 | }; |
| 652 | |
| 653 | U_BOOT_DRIVER(i2c_fsl) = { |
| 654 | .name = "i2c_fsl", |
| 655 | .id = UCLASS_I2C, |
| 656 | .of_match = fsl_i2c_ids, |
| 657 | .probe = fsl_i2c_probe, |
| 658 | .ofdata_to_platdata = fsl_i2c_ofdata_to_platdata, |
| 659 | .priv_auto_alloc_size = sizeof(struct fsl_i2c_dev), |
| 660 | .ops = &fsl_i2c_ops, |
| 661 | }; |
| 662 | |
| 663 | #endif /* CONFIG_DM_I2C */ |