Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Jon Loeliger | 7237c03 | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 2 | /* |
Timur Tabi | 92477a6 | 2009-09-04 16:28:35 -0500 | [diff] [blame] | 3 | * Copyright 2006,2009 Freescale Semiconductor, Inc. |
Jon Loeliger | 7237c03 | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 4 | * |
Heiko Schocher | 00f792e | 2012-10-24 13:48:22 +0200 | [diff] [blame] | 5 | * 2012, Heiko Schocher, DENX Software Engineering, hs@denx.de. |
| 6 | * Changes for multibus/multiadapter I2C support. |
Jon Loeliger | 7237c03 | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 7 | */ |
| 8 | |
Jon Loeliger | 7237c03 | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 9 | #include <common.h> |
Jon Loeliger | 4d45f69 | 2006-10-19 12:02:24 -0500 | [diff] [blame] | 10 | #include <command.h> |
Jon Loeliger | 2047672 | 2006-10-20 15:50:15 -0500 | [diff] [blame] | 11 | #include <i2c.h> /* Functional interface */ |
Simon Glass | 6887c5b | 2019-11-14 12:57:26 -0700 | [diff] [blame] | 12 | #include <time.h> |
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 */ |
Mario Six | e5c762f | 2018-03-28 14:37:44 +0200 | [diff] [blame] | 15 | #include <clk.h> |
mario.six@gdsys.cc | dbc82ce | 2016-04-25 08:31:09 +0200 | [diff] [blame] | 16 | #include <dm.h> |
| 17 | #include <mapmem.h> |
Jon Loeliger | 7237c03 | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 18 | |
Timur Tabi | 92477a6 | 2009-09-04 16:28:35 -0500 | [diff] [blame] | 19 | /* The maximum number of microseconds we will wait until another master has |
| 20 | * released the bus. If not defined in the board header file, then use a |
| 21 | * generic value. |
| 22 | */ |
| 23 | #ifndef CONFIG_I2C_MBB_TIMEOUT |
| 24 | #define CONFIG_I2C_MBB_TIMEOUT 100000 |
| 25 | #endif |
| 26 | |
| 27 | /* The maximum number of microseconds we will wait for a read or write |
| 28 | * operation to complete. If not defined in the board header file, then use a |
| 29 | * generic value. |
| 30 | */ |
| 31 | #ifndef CONFIG_I2C_TIMEOUT |
Shaveta Leekha | 6dd38cc | 2014-11-03 10:43:14 +0530 | [diff] [blame] | 32 | #define CONFIG_I2C_TIMEOUT 100000 |
Timur Tabi | 92477a6 | 2009-09-04 16:28:35 -0500 | [diff] [blame] | 33 | #endif |
Jon Loeliger | 7237c03 | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 34 | |
Joakim Tjernlund | 1939d96 | 2006-11-28 16:17:27 -0600 | [diff] [blame] | 35 | #define I2C_READ_BIT 1 |
| 36 | #define I2C_WRITE_BIT 0 |
| 37 | |
Timur Tabi | d8c82db | 2008-03-14 17:45:29 -0500 | [diff] [blame] | 38 | DECLARE_GLOBAL_DATA_PTR; |
| 39 | |
mario.six@gdsys.cc | dbc82ce | 2016-04-25 08:31:09 +0200 | [diff] [blame] | 40 | #ifndef CONFIG_DM_I2C |
mario.six@gdsys.cc | ec2c81c | 2016-04-25 08:31:01 +0200 | [diff] [blame] | 41 | static const struct fsl_i2c_base *i2c_base[4] = { |
| 42 | (struct fsl_i2c_base *)(CONFIG_SYS_IMMR + CONFIG_SYS_FSL_I2C_OFFSET), |
Heiko Schocher | 00f792e | 2012-10-24 13:48:22 +0200 | [diff] [blame] | 43 | #ifdef CONFIG_SYS_FSL_I2C2_OFFSET |
mario.six@gdsys.cc | ec2c81c | 2016-04-25 08:31:01 +0200 | [diff] [blame] | 44 | (struct fsl_i2c_base *)(CONFIG_SYS_IMMR + CONFIG_SYS_FSL_I2C2_OFFSET), |
Shengzhou Liu | a17fd10 | 2014-07-07 12:17:48 +0800 | [diff] [blame] | 45 | #endif |
| 46 | #ifdef CONFIG_SYS_FSL_I2C3_OFFSET |
mario.six@gdsys.cc | ec2c81c | 2016-04-25 08:31:01 +0200 | [diff] [blame] | 47 | (struct fsl_i2c_base *)(CONFIG_SYS_IMMR + CONFIG_SYS_FSL_I2C3_OFFSET), |
Shengzhou Liu | a17fd10 | 2014-07-07 12:17:48 +0800 | [diff] [blame] | 48 | #endif |
| 49 | #ifdef CONFIG_SYS_FSL_I2C4_OFFSET |
mario.six@gdsys.cc | ec2c81c | 2016-04-25 08:31:01 +0200 | [diff] [blame] | 50 | (struct fsl_i2c_base *)(CONFIG_SYS_IMMR + CONFIG_SYS_FSL_I2C4_OFFSET) |
Timur Tabi | be5e618 | 2006-11-03 19:15:00 -0600 | [diff] [blame] | 51 | #endif |
| 52 | }; |
mario.six@gdsys.cc | dbc82ce | 2016-04-25 08:31:09 +0200 | [diff] [blame] | 53 | #endif |
Jon Loeliger | 7237c03 | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 54 | |
Timur Tabi | d8c82db | 2008-03-14 17:45:29 -0500 | [diff] [blame] | 55 | /* I2C speed map for a DFSR value of 1 */ |
| 56 | |
Tom Rini | 645cb46 | 2017-02-09 15:40:16 -0500 | [diff] [blame] | 57 | #ifdef __M68K__ |
Timur Tabi | d8c82db | 2008-03-14 17:45:29 -0500 | [diff] [blame] | 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 | 5d9a5ef | 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 | d8c82db | 2008-03-14 17:45:29 -0500 | [diff] [blame] | 84 | */ |
| 85 | static const struct { |
| 86 | unsigned short divider; |
Timur Tabi | d8c82db | 2008-03-14 17:45:29 -0500 | [diff] [blame] | 87 | u8 fdr; |
| 88 | } fsl_i2c_speed_map[] = { |
TsiChung Liew | 5d9a5ef | 2008-08-19 00:56:46 +0600 | [diff] [blame] | 89 | {20, 32}, {22, 33}, {24, 34}, {26, 35}, |
| 90 | {28, 0}, {28, 36}, {30, 1}, {32, 37}, |
| 91 | {34, 2}, {36, 38}, {40, 3}, {40, 39}, |
| 92 | {44, 4}, {48, 5}, {48, 40}, {56, 6}, |
| 93 | {56, 41}, {64, 42}, {68, 7}, {72, 43}, |
| 94 | {80, 8}, {80, 44}, {88, 9}, {96, 41}, |
| 95 | {104, 10}, {112, 42}, {128, 11}, {128, 43}, |
| 96 | {144, 12}, {160, 13}, {160, 48}, {192, 14}, |
| 97 | {192, 49}, {224, 50}, {240, 15}, {256, 51}, |
| 98 | {288, 16}, {320, 17}, {320, 52}, {384, 18}, |
| 99 | {384, 53}, {448, 54}, {480, 19}, {512, 55}, |
| 100 | {576, 20}, {640, 21}, {640, 56}, {768, 22}, |
| 101 | {768, 57}, {960, 23}, {896, 58}, {1024, 59}, |
| 102 | {1152, 24}, {1280, 25}, {1280, 60}, {1536, 26}, |
| 103 | {1536, 61}, {1792, 62}, {1920, 27}, {2048, 63}, |
| 104 | {2304, 28}, {2560, 29}, {3072, 30}, {3840, 31}, |
| 105 | {-1, 31} |
Timur Tabi | d8c82db | 2008-03-14 17:45:29 -0500 | [diff] [blame] | 106 | }; |
Tom Rini | 645cb46 | 2017-02-09 15:40:16 -0500 | [diff] [blame] | 107 | #endif |
Timur Tabi | d8c82db | 2008-03-14 17:45:29 -0500 | [diff] [blame] | 108 | |
| 109 | /** |
| 110 | * Set the I2C bus speed for a given I2C device |
| 111 | * |
mario.six@gdsys.cc | ec2c81c | 2016-04-25 08:31:01 +0200 | [diff] [blame] | 112 | * @param base: the I2C device registers |
Timur Tabi | d8c82db | 2008-03-14 17:45:29 -0500 | [diff] [blame] | 113 | * @i2c_clk: I2C bus clock frequency |
| 114 | * @speed: the desired speed of the bus |
| 115 | * |
| 116 | * The I2C device must be stopped before calling this function. |
| 117 | * |
| 118 | * The return value is the actual bus speed that is set. |
| 119 | */ |
Mario Six | a059de1 | 2018-01-15 11:08:07 +0100 | [diff] [blame] | 120 | static uint set_i2c_bus_speed(const struct fsl_i2c_base *base, |
| 121 | uint i2c_clk, uint speed) |
Timur Tabi | d8c82db | 2008-03-14 17:45:29 -0500 | [diff] [blame] | 122 | { |
Mario Six | a059de1 | 2018-01-15 11:08:07 +0100 | [diff] [blame] | 123 | ushort divider = min(i2c_clk / speed, (uint)USHRT_MAX); |
Timur Tabi | d8c82db | 2008-03-14 17:45:29 -0500 | [diff] [blame] | 124 | |
| 125 | /* |
| 126 | * We want to choose an FDR/DFSR that generates an I2C bus speed that |
| 127 | * is equal to or lower than the requested speed. That means that we |
| 128 | * want the first divider that is equal to or greater than the |
| 129 | * calculated divider. |
| 130 | */ |
Joakim Tjernlund | 9940420 | 2009-09-17 11:07:17 +0200 | [diff] [blame] | 131 | #ifdef __PPC__ |
| 132 | u8 dfsr, fdr = 0x31; /* Default if no FDR found */ |
| 133 | /* a, b and dfsr matches identifiers A,B and C respectively in AN2919 */ |
Mario Six | a059de1 | 2018-01-15 11:08:07 +0100 | [diff] [blame] | 134 | ushort a, b, ga, gb; |
| 135 | ulong c_div, est_div; |
Joakim Tjernlund | 9940420 | 2009-09-17 11:07:17 +0200 | [diff] [blame] | 136 | |
| 137 | #ifdef CONFIG_FSL_I2C_CUSTOM_DFSR |
| 138 | dfsr = CONFIG_FSL_I2C_CUSTOM_DFSR; |
| 139 | #else |
| 140 | /* Condition 1: dfsr <= 50/T */ |
| 141 | dfsr = (5 * (i2c_clk / 1000)) / 100000; |
| 142 | #endif |
| 143 | #ifdef CONFIG_FSL_I2C_CUSTOM_FDR |
| 144 | fdr = CONFIG_FSL_I2C_CUSTOM_FDR; |
| 145 | speed = i2c_clk / divider; /* Fake something */ |
| 146 | #else |
| 147 | debug("Requested speed:%d, i2c_clk:%d\n", speed, i2c_clk); |
| 148 | if (!dfsr) |
| 149 | dfsr = 1; |
| 150 | |
| 151 | est_div = ~0; |
| 152 | for (ga = 0x4, a = 10; a <= 30; ga++, a += 2) { |
| 153 | for (gb = 0; gb < 8; gb++) { |
| 154 | b = 16 << gb; |
Mario Six | a059de1 | 2018-01-15 11:08:07 +0100 | [diff] [blame] | 155 | c_div = b * (a + ((3 * dfsr) / b) * 2); |
| 156 | if (c_div > divider && c_div < est_div) { |
| 157 | ushort bin_gb, bin_ga; |
Joakim Tjernlund | 9940420 | 2009-09-17 11:07:17 +0200 | [diff] [blame] | 158 | |
| 159 | est_div = c_div; |
| 160 | bin_gb = gb << 2; |
| 161 | bin_ga = (ga & 0x3) | ((ga & 0x4) << 3); |
| 162 | fdr = bin_gb | bin_ga; |
| 163 | speed = i2c_clk / est_div; |
Mario Six | a059de1 | 2018-01-15 11:08:07 +0100 | [diff] [blame] | 164 | |
| 165 | debug("FDR: 0x%.2x, ", fdr); |
| 166 | debug("div: %ld, ", est_div); |
| 167 | debug("ga: 0x%x, gb: 0x%x, ", ga, gb); |
| 168 | debug("a: %d, b: %d, speed: %d\n", a, b, speed); |
| 169 | |
Joakim Tjernlund | 9940420 | 2009-09-17 11:07:17 +0200 | [diff] [blame] | 170 | /* Condition 2 not accounted for */ |
| 171 | debug("Tr <= %d ns\n", |
| 172 | (b - 3 * dfsr) * 1000000 / |
| 173 | (i2c_clk / 1000)); |
| 174 | } |
| 175 | } |
| 176 | if (a == 20) |
| 177 | a += 2; |
| 178 | if (a == 24) |
| 179 | a += 4; |
| 180 | } |
Mario Six | a059de1 | 2018-01-15 11:08:07 +0100 | [diff] [blame] | 181 | debug("divider: %d, est_div: %ld, DFSR: %d\n", divider, est_div, dfsr); |
| 182 | debug("FDR: 0x%.2x, speed: %d\n", fdr, speed); |
Joakim Tjernlund | 9940420 | 2009-09-17 11:07:17 +0200 | [diff] [blame] | 183 | #endif |
mario.six@gdsys.cc | ec2c81c | 2016-04-25 08:31:01 +0200 | [diff] [blame] | 184 | writeb(dfsr, &base->dfsrr); /* set default filter */ |
| 185 | writeb(fdr, &base->fdr); /* set bus speed */ |
Joakim Tjernlund | 9940420 | 2009-09-17 11:07:17 +0200 | [diff] [blame] | 186 | #else |
Mario Six | a059de1 | 2018-01-15 11:08:07 +0100 | [diff] [blame] | 187 | uint i; |
Timur Tabi | d8c82db | 2008-03-14 17:45:29 -0500 | [diff] [blame] | 188 | |
| 189 | for (i = 0; i < ARRAY_SIZE(fsl_i2c_speed_map); i++) |
| 190 | if (fsl_i2c_speed_map[i].divider >= divider) { |
TsiChung Liew | 5d9a5ef | 2008-08-19 00:56:46 +0600 | [diff] [blame] | 191 | u8 fdr; |
Joakim Tjernlund | 9940420 | 2009-09-17 11:07:17 +0200 | [diff] [blame] | 192 | |
Joakim Tjernlund | d01ee4d | 2009-09-17 11:07:16 +0200 | [diff] [blame] | 193 | fdr = fsl_i2c_speed_map[i].fdr; |
| 194 | speed = i2c_clk / fsl_i2c_speed_map[i].divider; |
mario.six@gdsys.cc | ec2c81c | 2016-04-25 08:31:01 +0200 | [diff] [blame] | 195 | writeb(fdr, &base->fdr); /* set bus speed */ |
Joakim Tjernlund | d01ee4d | 2009-09-17 11:07:16 +0200 | [diff] [blame] | 196 | |
Timur Tabi | d8c82db | 2008-03-14 17:45:29 -0500 | [diff] [blame] | 197 | break; |
| 198 | } |
Joakim Tjernlund | 9940420 | 2009-09-17 11:07:17 +0200 | [diff] [blame] | 199 | #endif |
Timur Tabi | d8c82db | 2008-03-14 17:45:29 -0500 | [diff] [blame] | 200 | return speed; |
| 201 | } |
| 202 | |
mario.six@gdsys.cc | dbc82ce | 2016-04-25 08:31:09 +0200 | [diff] [blame] | 203 | #ifndef CONFIG_DM_I2C |
Mario Six | a059de1 | 2018-01-15 11:08:07 +0100 | [diff] [blame] | 204 | static uint get_i2c_clock(int bus) |
Jerry Huang | c9a8b25 | 2011-10-26 15:29:38 +0000 | [diff] [blame] | 205 | { |
| 206 | if (bus) |
Simon Glass | 609e6ec | 2012-12-13 20:48:49 +0000 | [diff] [blame] | 207 | return gd->arch.i2c2_clk; /* I2C2 clock */ |
Jerry Huang | c9a8b25 | 2011-10-26 15:29:38 +0000 | [diff] [blame] | 208 | else |
Simon Glass | 609e6ec | 2012-12-13 20:48:49 +0000 | [diff] [blame] | 209 | return gd->arch.i2c1_clk; /* I2C1 clock */ |
Jerry Huang | c9a8b25 | 2011-10-26 15:29:38 +0000 | [diff] [blame] | 210 | } |
mario.six@gdsys.cc | dbc82ce | 2016-04-25 08:31:09 +0200 | [diff] [blame] | 211 | #endif |
Jerry Huang | c9a8b25 | 2011-10-26 15:29:38 +0000 | [diff] [blame] | 212 | |
mario.six@gdsys.cc | ec2c81c | 2016-04-25 08:31:01 +0200 | [diff] [blame] | 213 | static int fsl_i2c_fixup(const struct fsl_i2c_base *base) |
Chunhe Lan | b8ce334 | 2013-08-16 15:10:36 +0800 | [diff] [blame] | 214 | { |
| 215 | const unsigned long long timeout = usec2ticks(CONFIG_I2C_MBB_TIMEOUT); |
| 216 | unsigned long long timeval = 0; |
| 217 | int ret = -1; |
Mario Six | a059de1 | 2018-01-15 11:08:07 +0100 | [diff] [blame] | 218 | uint flags = 0; |
Chunhe Lan | 9c3f77e | 2013-08-16 15:10:37 +0800 | [diff] [blame] | 219 | |
| 220 | #ifdef CONFIG_SYS_FSL_ERRATUM_I2C_A004447 |
Mario Six | a059de1 | 2018-01-15 11:08:07 +0100 | [diff] [blame] | 221 | uint svr = get_svr(); |
| 222 | |
Chunhe Lan | 9c3f77e | 2013-08-16 15:10:37 +0800 | [diff] [blame] | 223 | if ((SVR_SOC_VER(svr) == SVR_8548 && IS_SVR_REV(svr, 3, 1)) || |
| 224 | (SVR_REV(svr) <= CONFIG_SYS_FSL_A004447_SVR_REV)) |
| 225 | flags = I2C_CR_BIT6; |
| 226 | #endif |
Chunhe Lan | b8ce334 | 2013-08-16 15:10:36 +0800 | [diff] [blame] | 227 | |
mario.six@gdsys.cc | ec2c81c | 2016-04-25 08:31:01 +0200 | [diff] [blame] | 228 | writeb(I2C_CR_MEN | I2C_CR_MSTA, &base->cr); |
Chunhe Lan | b8ce334 | 2013-08-16 15:10:36 +0800 | [diff] [blame] | 229 | |
| 230 | timeval = get_ticks(); |
mario.six@gdsys.cc | ec2c81c | 2016-04-25 08:31:01 +0200 | [diff] [blame] | 231 | while (!(readb(&base->sr) & I2C_SR_MBB)) { |
Chunhe Lan | b8ce334 | 2013-08-16 15:10:36 +0800 | [diff] [blame] | 232 | if ((get_ticks() - timeval) > timeout) |
| 233 | goto err; |
| 234 | } |
| 235 | |
mario.six@gdsys.cc | ec2c81c | 2016-04-25 08:31:01 +0200 | [diff] [blame] | 236 | if (readb(&base->sr) & I2C_SR_MAL) { |
Chunhe Lan | b8ce334 | 2013-08-16 15:10:36 +0800 | [diff] [blame] | 237 | /* SDA is stuck low */ |
mario.six@gdsys.cc | ec2c81c | 2016-04-25 08:31:01 +0200 | [diff] [blame] | 238 | writeb(0, &base->cr); |
Chunhe Lan | b8ce334 | 2013-08-16 15:10:36 +0800 | [diff] [blame] | 239 | udelay(100); |
mario.six@gdsys.cc | ec2c81c | 2016-04-25 08:31:01 +0200 | [diff] [blame] | 240 | writeb(I2C_CR_MSTA | flags, &base->cr); |
| 241 | writeb(I2C_CR_MEN | I2C_CR_MSTA | flags, &base->cr); |
Chunhe Lan | b8ce334 | 2013-08-16 15:10:36 +0800 | [diff] [blame] | 242 | } |
| 243 | |
mario.six@gdsys.cc | ec2c81c | 2016-04-25 08:31:01 +0200 | [diff] [blame] | 244 | readb(&base->dr); |
Chunhe Lan | b8ce334 | 2013-08-16 15:10:36 +0800 | [diff] [blame] | 245 | |
| 246 | timeval = get_ticks(); |
mario.six@gdsys.cc | ec2c81c | 2016-04-25 08:31:01 +0200 | [diff] [blame] | 247 | while (!(readb(&base->sr) & I2C_SR_MIF)) { |
Chunhe Lan | b8ce334 | 2013-08-16 15:10:36 +0800 | [diff] [blame] | 248 | if ((get_ticks() - timeval) > timeout) |
| 249 | goto err; |
| 250 | } |
| 251 | ret = 0; |
| 252 | |
| 253 | err: |
mario.six@gdsys.cc | ec2c81c | 2016-04-25 08:31:01 +0200 | [diff] [blame] | 254 | writeb(I2C_CR_MEN | flags, &base->cr); |
| 255 | writeb(0, &base->sr); |
Chunhe Lan | b8ce334 | 2013-08-16 15:10:36 +0800 | [diff] [blame] | 256 | udelay(100); |
| 257 | |
| 258 | return ret; |
| 259 | } |
| 260 | |
mario.six@gdsys.cc | ecf591e | 2016-04-25 08:31:08 +0200 | [diff] [blame] | 261 | static void __i2c_init(const struct fsl_i2c_base *base, int speed, int |
| 262 | slaveadd, int i2c_clk, int busnum) |
Jon Loeliger | 7237c03 | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 263 | { |
Chunhe Lan | b8ce334 | 2013-08-16 15:10:36 +0800 | [diff] [blame] | 264 | const unsigned long long timeout = usec2ticks(CONFIG_I2C_MBB_TIMEOUT); |
| 265 | unsigned long long timeval; |
Jon Loeliger | 7237c03 | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 266 | |
Heiko Schocher | 39df00d | 2009-07-09 12:04:26 +0200 | [diff] [blame] | 267 | #ifdef CONFIG_SYS_I2C_INIT_BOARD |
Richard Retanubun | 26a3350 | 2010-04-12 15:08:17 -0400 | [diff] [blame] | 268 | /* Call board specific i2c bus reset routine before accessing the |
| 269 | * environment, which might be in a chip on that bus. For details |
| 270 | * about this problem see doc/I2C_Edge_Conditions. |
Mario Six | a059de1 | 2018-01-15 11:08:07 +0100 | [diff] [blame] | 271 | */ |
Heiko Schocher | 39df00d | 2009-07-09 12:04:26 +0200 | [diff] [blame] | 272 | i2c_init_board(); |
| 273 | #endif |
mario.six@gdsys.cc | ec2c81c | 2016-04-25 08:31:01 +0200 | [diff] [blame] | 274 | writeb(0, &base->cr); /* stop I2C controller */ |
Heiko Schocher | 00f792e | 2012-10-24 13:48:22 +0200 | [diff] [blame] | 275 | udelay(5); /* let it shutdown in peace */ |
mario.six@gdsys.cc | ecf591e | 2016-04-25 08:31:08 +0200 | [diff] [blame] | 276 | set_i2c_bus_speed(base, i2c_clk, speed); |
mario.six@gdsys.cc | ec2c81c | 2016-04-25 08:31:01 +0200 | [diff] [blame] | 277 | writeb(slaveadd << 1, &base->adr);/* write slave address */ |
| 278 | writeb(0x0, &base->sr); /* clear status register */ |
| 279 | writeb(I2C_CR_MEN, &base->cr); /* start I2C controller */ |
Richard Retanubun | 26a3350 | 2010-04-12 15:08:17 -0400 | [diff] [blame] | 280 | |
Chunhe Lan | b8ce334 | 2013-08-16 15:10:36 +0800 | [diff] [blame] | 281 | timeval = get_ticks(); |
mario.six@gdsys.cc | ec2c81c | 2016-04-25 08:31:01 +0200 | [diff] [blame] | 282 | while (readb(&base->sr) & I2C_SR_MBB) { |
Chunhe Lan | b8ce334 | 2013-08-16 15:10:36 +0800 | [diff] [blame] | 283 | if ((get_ticks() - timeval) < timeout) |
| 284 | continue; |
| 285 | |
mario.six@gdsys.cc | ec2c81c | 2016-04-25 08:31:01 +0200 | [diff] [blame] | 286 | if (fsl_i2c_fixup(base)) |
Chunhe Lan | b8ce334 | 2013-08-16 15:10:36 +0800 | [diff] [blame] | 287 | debug("i2c_init: BUS#%d failed to init\n", |
mario.six@gdsys.cc | ecf591e | 2016-04-25 08:31:08 +0200 | [diff] [blame] | 288 | busnum); |
Chunhe Lan | b8ce334 | 2013-08-16 15:10:36 +0800 | [diff] [blame] | 289 | |
| 290 | break; |
| 291 | } |
Jon Loeliger | 7237c03 | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 292 | } |
| 293 | |
Mario Six | a059de1 | 2018-01-15 11:08:07 +0100 | [diff] [blame] | 294 | static int i2c_wait4bus(const struct fsl_i2c_base *base) |
Jon Loeliger | 7237c03 | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 295 | { |
Stefan Roese | f2302d4 | 2008-08-06 14:05:38 +0200 | [diff] [blame] | 296 | unsigned long long timeval = get_ticks(); |
Timur Tabi | 92477a6 | 2009-09-04 16:28:35 -0500 | [diff] [blame] | 297 | const unsigned long long timeout = usec2ticks(CONFIG_I2C_MBB_TIMEOUT); |
Jon Loeliger | 7237c03 | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 298 | |
mario.six@gdsys.cc | ec2c81c | 2016-04-25 08:31:01 +0200 | [diff] [blame] | 299 | while (readb(&base->sr) & I2C_SR_MBB) { |
Timur Tabi | 92477a6 | 2009-09-04 16:28:35 -0500 | [diff] [blame] | 300 | if ((get_ticks() - timeval) > timeout) |
Jon Loeliger | 7237c03 | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 301 | return -1; |
Jon Loeliger | 7237c03 | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | return 0; |
| 305 | } |
| 306 | |
Mario Six | d4f422f | 2018-01-15 11:08:08 +0100 | [diff] [blame] | 307 | static int 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) { |
Mario Six | a059de1 | 2018-01-15 11:08:07 +0100 | [diff] [blame] | 323 | debug("%s: MAL\n", __func__); |
Jon Loeliger | 7237c03 | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 324 | return -1; |
| 325 | } |
| 326 | |
| 327 | if (!(csr & I2C_SR_MCF)) { |
Mario Six | a059de1 | 2018-01-15 11:08:07 +0100 | [diff] [blame] | 328 | debug("%s: unfinished\n", __func__); |
Jon Loeliger | 7237c03 | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 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)) { |
Mario Six | a059de1 | 2018-01-15 11:08:07 +0100 | [diff] [blame] | 333 | debug("%s: No RXACK\n", __func__); |
Jon Loeliger | 7237c03 | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 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 | |
Mario Six | a059de1 | 2018-01-15 11:08:07 +0100 | [diff] [blame] | 340 | debug("%s: timed out\n", __func__); |
Jon Loeliger | 7237c03 | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 341 | return -1; |
| 342 | } |
| 343 | |
Mario Six | d4f422f | 2018-01-15 11:08:08 +0100 | [diff] [blame] | 344 | static int i2c_write_addr(const struct fsl_i2c_base *base, u8 dev, |
| 345 | 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 | d4f422f | 2018-01-15 11:08:08 +0100 | [diff] [blame] | 359 | static int __i2c_write_data(const struct fsl_i2c_base *base, u8 *data, |
| 360 | 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 | d4f422f | 2018-01-15 11:08:08 +0100 | [diff] [blame] | 374 | static int __i2c_read_data(const struct fsl_i2c_base *base, u8 *data, |
| 375 | 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 | |
Mario Six | a059de1 | 2018-01-15 11:08:07 +0100 | [diff] [blame] | 405 | static int __i2c_read(const struct fsl_i2c_base *base, u8 chip_addr, u8 *offset, |
| 406 | int olen, u8 *data, int dlen) |
Jon Loeliger | 7237c03 | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 407 | { |
mario.six@gdsys.cc | 2b21e96 | 2016-04-25 08:31:02 +0200 | [diff] [blame] | 408 | int ret = -1; /* signal error */ |
Jon Loeliger | 7237c03 | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 409 | |
mario.six@gdsys.cc | ecf591e | 2016-04-25 08:31:08 +0200 | [diff] [blame] | 410 | if (i2c_wait4bus(base) < 0) |
Reinhard Pfau | b778c1b | 2013-06-26 15:55:14 +0200 | [diff] [blame] | 411 | return -1; |
| 412 | |
mario.six@gdsys.cc | 386b276 | 2016-04-25 08:31:03 +0200 | [diff] [blame] | 413 | /* Some drivers use offset lengths in excess of 4 bytes. These drivers |
| 414 | * adhere to the following convention: |
| 415 | * - the offset length is passed as negative (that is, the absolute |
| 416 | * value of olen is the actual offset length) |
| 417 | * - the offset itself is passed in data, which is overwritten by the |
| 418 | * subsequent read operation |
Shaveta Leekha | a405764 | 2014-04-24 14:51:23 +0530 | [diff] [blame] | 419 | */ |
mario.six@gdsys.cc | 2b21e96 | 2016-04-25 08:31:02 +0200 | [diff] [blame] | 420 | if (olen < 0) { |
mario.six@gdsys.cc | ecf591e | 2016-04-25 08:31:08 +0200 | [diff] [blame] | 421 | if (i2c_write_addr(base, chip_addr, I2C_WRITE_BIT, 0) != 0) |
| 422 | ret = __i2c_write_data(base, data, -olen); |
Joakim Tjernlund | f6f5f70 | 2007-01-31 11:04:19 +0100 | [diff] [blame] | 423 | |
mario.six@gdsys.cc | 03a112a | 2016-04-25 08:31:04 +0200 | [diff] [blame] | 424 | if (ret != -olen) |
Shaveta Leekha | a405764 | 2014-04-24 14:51:23 +0530 | [diff] [blame] | 425 | return -1; |
| 426 | |
mario.six@gdsys.cc | ecf591e | 2016-04-25 08:31:08 +0200 | [diff] [blame] | 427 | if (dlen && i2c_write_addr(base, chip_addr, |
mario.six@gdsys.cc | 2b21e96 | 2016-04-25 08:31:02 +0200 | [diff] [blame] | 428 | I2C_READ_BIT, 1) != 0) |
mario.six@gdsys.cc | ecf591e | 2016-04-25 08:31:08 +0200 | [diff] [blame] | 429 | ret = __i2c_read_data(base, data, dlen); |
Shaveta Leekha | a405764 | 2014-04-24 14:51:23 +0530 | [diff] [blame] | 430 | } else { |
mario.six@gdsys.cc | 2b21e96 | 2016-04-25 08:31:02 +0200 | [diff] [blame] | 431 | if ((!dlen || olen > 0) && |
mario.six@gdsys.cc | ecf591e | 2016-04-25 08:31:08 +0200 | [diff] [blame] | 432 | i2c_write_addr(base, chip_addr, I2C_WRITE_BIT, 0) != 0 && |
| 433 | __i2c_write_data(base, offset, olen) == olen) |
mario.six@gdsys.cc | 2b21e96 | 2016-04-25 08:31:02 +0200 | [diff] [blame] | 434 | ret = 0; /* No error so far */ |
Shaveta Leekha | a405764 | 2014-04-24 14:51:23 +0530 | [diff] [blame] | 435 | |
mario.six@gdsys.cc | ecf591e | 2016-04-25 08:31:08 +0200 | [diff] [blame] | 436 | 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] | 437 | olen ? 1 : 0) != 0) |
mario.six@gdsys.cc | ecf591e | 2016-04-25 08:31:08 +0200 | [diff] [blame] | 438 | ret = __i2c_read_data(base, data, dlen); |
Shaveta Leekha | a405764 | 2014-04-24 14:51:23 +0530 | [diff] [blame] | 439 | } |
Jon Loeliger | 7237c03 | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 440 | |
mario.six@gdsys.cc | ec2c81c | 2016-04-25 08:31:01 +0200 | [diff] [blame] | 441 | writeb(I2C_CR_MEN, &base->cr); |
Jon Loeliger | 7237c03 | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 442 | |
mario.six@gdsys.cc | ecf591e | 2016-04-25 08:31:08 +0200 | [diff] [blame] | 443 | if (i2c_wait4bus(base)) /* Wait until STOP */ |
Joakim Tjernlund | d1c9e5b | 2009-09-22 13:40:44 +0200 | [diff] [blame] | 444 | debug("i2c_read: wait4bus timed out\n"); |
| 445 | |
mario.six@gdsys.cc | 2b21e96 | 2016-04-25 08:31:02 +0200 | [diff] [blame] | 446 | if (ret == dlen) |
| 447 | return 0; |
Jon Loeliger | 4d45f69 | 2006-10-19 12:02:24 -0500 | [diff] [blame] | 448 | |
| 449 | return -1; |
Jon Loeliger | 7237c03 | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 450 | } |
| 451 | |
Mario Six | a059de1 | 2018-01-15 11:08:07 +0100 | [diff] [blame] | 452 | static int __i2c_write(const struct fsl_i2c_base *base, u8 chip_addr, |
| 453 | u8 *offset, int olen, u8 *data, int dlen) |
Jon Loeliger | 7237c03 | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 454 | { |
mario.six@gdsys.cc | 2b21e96 | 2016-04-25 08:31:02 +0200 | [diff] [blame] | 455 | int ret = -1; /* signal error */ |
Jon Loeliger | 7237c03 | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 456 | |
mario.six@gdsys.cc | ecf591e | 2016-04-25 08:31:08 +0200 | [diff] [blame] | 457 | if (i2c_wait4bus(base) < 0) |
Chunhe Lan | b8ce334 | 2013-08-16 15:10:36 +0800 | [diff] [blame] | 458 | return -1; |
| 459 | |
mario.six@gdsys.cc | ecf591e | 2016-04-25 08:31:08 +0200 | [diff] [blame] | 460 | if (i2c_write_addr(base, chip_addr, I2C_WRITE_BIT, 0) != 0 && |
| 461 | __i2c_write_data(base, offset, olen) == olen) { |
| 462 | ret = __i2c_write_data(base, data, dlen); |
Jon Loeliger | 4d45f69 | 2006-10-19 12:02:24 -0500 | [diff] [blame] | 463 | } |
Jon Loeliger | 7237c03 | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 464 | |
mario.six@gdsys.cc | ec2c81c | 2016-04-25 08:31:01 +0200 | [diff] [blame] | 465 | writeb(I2C_CR_MEN, &base->cr); |
mario.six@gdsys.cc | ecf591e | 2016-04-25 08:31:08 +0200 | [diff] [blame] | 466 | if (i2c_wait4bus(base)) /* Wait until STOP */ |
Joakim Tjernlund | 21f4cbb | 2009-09-17 11:07:15 +0200 | [diff] [blame] | 467 | debug("i2c_write: wait4bus timed out\n"); |
Jon Loeliger | 7237c03 | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 468 | |
mario.six@gdsys.cc | 2b21e96 | 2016-04-25 08:31:02 +0200 | [diff] [blame] | 469 | if (ret == dlen) |
| 470 | return 0; |
Jon Loeliger | 4d45f69 | 2006-10-19 12:02:24 -0500 | [diff] [blame] | 471 | |
| 472 | return -1; |
Jon Loeliger | 7237c03 | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 473 | } |
| 474 | |
Mario Six | a059de1 | 2018-01-15 11:08:07 +0100 | [diff] [blame] | 475 | static int __i2c_probe_chip(const struct fsl_i2c_base *base, uchar chip) |
Jon Loeliger | 7237c03 | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 476 | { |
Mario Six | a059de1 | 2018-01-15 11:08:07 +0100 | [diff] [blame] | 477 | /* For unknown reason the controller will ACK when |
Joakim Tjernlund | f6f5f70 | 2007-01-31 11:04:19 +0100 | [diff] [blame] | 478 | * probing for a slave with the same address, so skip |
| 479 | * it. |
Jon Loeliger | 7237c03 | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 480 | */ |
mario.six@gdsys.cc | ec2c81c | 2016-04-25 08:31:01 +0200 | [diff] [blame] | 481 | if (chip == (readb(&base->adr) >> 1)) |
Joakim Tjernlund | f6f5f70 | 2007-01-31 11:04:19 +0100 | [diff] [blame] | 482 | return -1; |
Jon Loeliger | 7237c03 | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 483 | |
mario.six@gdsys.cc | ecf591e | 2016-04-25 08:31:08 +0200 | [diff] [blame] | 484 | return __i2c_read(base, chip, 0, 0, NULL, 0); |
Jon Loeliger | 7237c03 | 2006-10-19 11:02:16 -0500 | [diff] [blame] | 485 | } |
| 486 | |
Mario Six | a059de1 | 2018-01-15 11:08:07 +0100 | [diff] [blame] | 487 | static uint __i2c_set_bus_speed(const struct fsl_i2c_base *base, |
| 488 | uint speed, int i2c_clk) |
Timur Tabi | be5e618 | 2006-11-03 19:15:00 -0600 | [diff] [blame] | 489 | { |
mario.six@gdsys.cc | ec2c81c | 2016-04-25 08:31:01 +0200 | [diff] [blame] | 490 | writeb(0, &base->cr); /* stop controller */ |
mario.six@gdsys.cc | ecf591e | 2016-04-25 08:31:08 +0200 | [diff] [blame] | 491 | set_i2c_bus_speed(base, i2c_clk, speed); |
mario.six@gdsys.cc | ec2c81c | 2016-04-25 08:31:01 +0200 | [diff] [blame] | 492 | writeb(I2C_CR_MEN, &base->cr); /* start controller */ |
Timur Tabi | d8c82db | 2008-03-14 17:45:29 -0500 | [diff] [blame] | 493 | |
| 494 | return 0; |
Timur Tabi | be5e618 | 2006-11-03 19:15:00 -0600 | [diff] [blame] | 495 | } |
| 496 | |
mario.six@gdsys.cc | dbc82ce | 2016-04-25 08:31:09 +0200 | [diff] [blame] | 497 | #ifndef CONFIG_DM_I2C |
mario.six@gdsys.cc | ad7e657 | 2016-04-25 08:31:07 +0200 | [diff] [blame] | 498 | static void fsl_i2c_init(struct i2c_adapter *adap, int speed, int slaveadd) |
| 499 | { |
mario.six@gdsys.cc | ecf591e | 2016-04-25 08:31:08 +0200 | [diff] [blame] | 500 | __i2c_init(i2c_base[adap->hwadapnr], speed, slaveadd, |
| 501 | get_i2c_clock(adap->hwadapnr), adap->hwadapnr); |
mario.six@gdsys.cc | ad7e657 | 2016-04-25 08:31:07 +0200 | [diff] [blame] | 502 | } |
| 503 | |
Mario Six | a059de1 | 2018-01-15 11:08:07 +0100 | [diff] [blame] | 504 | static int fsl_i2c_probe_chip(struct i2c_adapter *adap, uchar chip) |
mario.six@gdsys.cc | ad7e657 | 2016-04-25 08:31:07 +0200 | [diff] [blame] | 505 | { |
mario.six@gdsys.cc | ecf591e | 2016-04-25 08:31:08 +0200 | [diff] [blame] | 506 | return __i2c_probe_chip(i2c_base[adap->hwadapnr], chip); |
mario.six@gdsys.cc | ad7e657 | 2016-04-25 08:31:07 +0200 | [diff] [blame] | 507 | } |
| 508 | |
Mario Six | a059de1 | 2018-01-15 11:08:07 +0100 | [diff] [blame] | 509 | static int fsl_i2c_read(struct i2c_adapter *adap, u8 chip_addr, uint offset, |
| 510 | int olen, u8 *data, int dlen) |
mario.six@gdsys.cc | ad7e657 | 2016-04-25 08:31:07 +0200 | [diff] [blame] | 511 | { |
mario.six@gdsys.cc | ecf591e | 2016-04-25 08:31:08 +0200 | [diff] [blame] | 512 | u8 *o = (u8 *)&offset; |
Mario Six | a059de1 | 2018-01-15 11:08:07 +0100 | [diff] [blame] | 513 | |
mario.six@gdsys.cc | ecf591e | 2016-04-25 08:31:08 +0200 | [diff] [blame] | 514 | return __i2c_read(i2c_base[adap->hwadapnr], chip_addr, &o[4 - olen], |
| 515 | olen, data, dlen); |
mario.six@gdsys.cc | ad7e657 | 2016-04-25 08:31:07 +0200 | [diff] [blame] | 516 | } |
| 517 | |
Mario Six | a059de1 | 2018-01-15 11:08:07 +0100 | [diff] [blame] | 518 | static int fsl_i2c_write(struct i2c_adapter *adap, u8 chip_addr, uint offset, |
| 519 | int olen, u8 *data, int dlen) |
mario.six@gdsys.cc | ad7e657 | 2016-04-25 08:31:07 +0200 | [diff] [blame] | 520 | { |
mario.six@gdsys.cc | ecf591e | 2016-04-25 08:31:08 +0200 | [diff] [blame] | 521 | u8 *o = (u8 *)&offset; |
Mario Six | a059de1 | 2018-01-15 11:08:07 +0100 | [diff] [blame] | 522 | |
mario.six@gdsys.cc | ecf591e | 2016-04-25 08:31:08 +0200 | [diff] [blame] | 523 | return __i2c_write(i2c_base[adap->hwadapnr], chip_addr, &o[4 - olen], |
| 524 | olen, data, dlen); |
mario.six@gdsys.cc | ad7e657 | 2016-04-25 08:31:07 +0200 | [diff] [blame] | 525 | } |
| 526 | |
Mario Six | a059de1 | 2018-01-15 11:08:07 +0100 | [diff] [blame] | 527 | static uint fsl_i2c_set_bus_speed(struct i2c_adapter *adap, uint speed) |
mario.six@gdsys.cc | ad7e657 | 2016-04-25 08:31:07 +0200 | [diff] [blame] | 528 | { |
mario.six@gdsys.cc | ecf591e | 2016-04-25 08:31:08 +0200 | [diff] [blame] | 529 | return __i2c_set_bus_speed(i2c_base[adap->hwadapnr], speed, |
| 530 | get_i2c_clock(adap->hwadapnr)); |
mario.six@gdsys.cc | ad7e657 | 2016-04-25 08:31:07 +0200 | [diff] [blame] | 531 | } |
| 532 | |
Heiko Schocher | 00f792e | 2012-10-24 13:48:22 +0200 | [diff] [blame] | 533 | /* |
| 534 | * Register fsl i2c adapters |
| 535 | */ |
mario.six@gdsys.cc | 16579ec | 2016-04-25 08:31:05 +0200 | [diff] [blame] | 536 | 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] | 537 | fsl_i2c_write, fsl_i2c_set_bus_speed, |
| 538 | CONFIG_SYS_FSL_I2C_SPEED, CONFIG_SYS_FSL_I2C_SLAVE, |
| 539 | 0) |
| 540 | #ifdef CONFIG_SYS_FSL_I2C2_OFFSET |
mario.six@gdsys.cc | 16579ec | 2016-04-25 08:31:05 +0200 | [diff] [blame] | 541 | 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] | 542 | fsl_i2c_write, fsl_i2c_set_bus_speed, |
| 543 | CONFIG_SYS_FSL_I2C2_SPEED, CONFIG_SYS_FSL_I2C2_SLAVE, |
| 544 | 1) |
Heiko Schocher | c1bce4f | 2009-02-24 11:30:37 +0100 | [diff] [blame] | 545 | #endif |
Shengzhou Liu | a17fd10 | 2014-07-07 12:17:48 +0800 | [diff] [blame] | 546 | #ifdef CONFIG_SYS_FSL_I2C3_OFFSET |
mario.six@gdsys.cc | 16579ec | 2016-04-25 08:31:05 +0200 | [diff] [blame] | 547 | 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] | 548 | fsl_i2c_write, fsl_i2c_set_bus_speed, |
| 549 | CONFIG_SYS_FSL_I2C3_SPEED, CONFIG_SYS_FSL_I2C3_SLAVE, |
| 550 | 2) |
| 551 | #endif |
| 552 | #ifdef CONFIG_SYS_FSL_I2C4_OFFSET |
mario.six@gdsys.cc | 16579ec | 2016-04-25 08:31:05 +0200 | [diff] [blame] | 553 | 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] | 554 | fsl_i2c_write, fsl_i2c_set_bus_speed, |
| 555 | CONFIG_SYS_FSL_I2C4_SPEED, CONFIG_SYS_FSL_I2C4_SLAVE, |
| 556 | 3) |
| 557 | #endif |
mario.six@gdsys.cc | dbc82ce | 2016-04-25 08:31:09 +0200 | [diff] [blame] | 558 | #else /* CONFIG_DM_I2C */ |
| 559 | static int fsl_i2c_probe_chip(struct udevice *bus, u32 chip_addr, |
| 560 | u32 chip_flags) |
| 561 | { |
| 562 | struct fsl_i2c_dev *dev = dev_get_priv(bus); |
Mario Six | a059de1 | 2018-01-15 11:08:07 +0100 | [diff] [blame] | 563 | |
mario.six@gdsys.cc | dbc82ce | 2016-04-25 08:31:09 +0200 | [diff] [blame] | 564 | return __i2c_probe_chip(dev->base, chip_addr); |
| 565 | } |
| 566 | |
Mario Six | a059de1 | 2018-01-15 11:08:07 +0100 | [diff] [blame] | 567 | static int fsl_i2c_set_bus_speed(struct udevice *bus, uint speed) |
mario.six@gdsys.cc | dbc82ce | 2016-04-25 08:31:09 +0200 | [diff] [blame] | 568 | { |
| 569 | struct fsl_i2c_dev *dev = dev_get_priv(bus); |
Mario Six | a059de1 | 2018-01-15 11:08:07 +0100 | [diff] [blame] | 570 | |
mario.six@gdsys.cc | dbc82ce | 2016-04-25 08:31:09 +0200 | [diff] [blame] | 571 | return __i2c_set_bus_speed(dev->base, speed, dev->i2c_clk); |
| 572 | } |
| 573 | |
| 574 | static int fsl_i2c_ofdata_to_platdata(struct udevice *bus) |
| 575 | { |
| 576 | struct fsl_i2c_dev *dev = dev_get_priv(bus); |
Mario Six | e5c762f | 2018-03-28 14:37:44 +0200 | [diff] [blame] | 577 | struct clk clock; |
mario.six@gdsys.cc | dbc82ce | 2016-04-25 08:31:09 +0200 | [diff] [blame] | 578 | |
Mario Six | d934832 | 2018-03-28 14:37:43 +0200 | [diff] [blame] | 579 | dev->base = map_sysmem(dev_read_addr(bus), sizeof(struct fsl_i2c_base)); |
mario.six@gdsys.cc | dbc82ce | 2016-04-25 08:31:09 +0200 | [diff] [blame] | 580 | |
| 581 | if (!dev->base) |
| 582 | return -ENOMEM; |
| 583 | |
Mario Six | 84a4d34 | 2018-01-15 11:08:09 +0100 | [diff] [blame] | 584 | dev->index = dev_read_u32_default(bus, "cell-index", -1); |
| 585 | dev->slaveadd = dev_read_u32_default(bus, "u-boot,i2c-slave-addr", |
| 586 | 0x7f); |
Simon Glass | f3d4615 | 2020-01-23 11:48:22 -0700 | [diff] [blame^] | 587 | dev->speed = dev_read_u32_default(bus, "clock-frequency", |
| 588 | I2C_SPEED_FAST_RATE); |
mario.six@gdsys.cc | dbc82ce | 2016-04-25 08:31:09 +0200 | [diff] [blame] | 589 | |
Mario Six | e5c762f | 2018-03-28 14:37:44 +0200 | [diff] [blame] | 590 | if (!clk_get_by_index(bus, 0, &clock)) |
| 591 | dev->i2c_clk = clk_get_rate(&clock); |
| 592 | else |
| 593 | dev->i2c_clk = dev->index ? gd->arch.i2c2_clk : |
| 594 | gd->arch.i2c1_clk; |
mario.six@gdsys.cc | dbc82ce | 2016-04-25 08:31:09 +0200 | [diff] [blame] | 595 | |
| 596 | return 0; |
| 597 | } |
| 598 | |
| 599 | static int fsl_i2c_probe(struct udevice *bus) |
| 600 | { |
| 601 | struct fsl_i2c_dev *dev = dev_get_priv(bus); |
Mario Six | a059de1 | 2018-01-15 11:08:07 +0100 | [diff] [blame] | 602 | |
mario.six@gdsys.cc | dbc82ce | 2016-04-25 08:31:09 +0200 | [diff] [blame] | 603 | __i2c_init(dev->base, dev->speed, dev->slaveadd, dev->i2c_clk, |
| 604 | dev->index); |
| 605 | return 0; |
| 606 | } |
| 607 | |
| 608 | static int fsl_i2c_xfer(struct udevice *bus, struct i2c_msg *msg, int nmsgs) |
| 609 | { |
| 610 | struct fsl_i2c_dev *dev = dev_get_priv(bus); |
| 611 | struct i2c_msg *dmsg, *omsg, dummy; |
| 612 | |
| 613 | memset(&dummy, 0, sizeof(struct i2c_msg)); |
| 614 | |
| 615 | /* We expect either two messages (one with an offset and one with the |
Mario Six | a059de1 | 2018-01-15 11:08:07 +0100 | [diff] [blame] | 616 | * actual data) or one message (just data) |
| 617 | */ |
mario.six@gdsys.cc | dbc82ce | 2016-04-25 08:31:09 +0200 | [diff] [blame] | 618 | if (nmsgs > 2 || nmsgs == 0) { |
| 619 | debug("%s: Only one or two messages are supported.", __func__); |
| 620 | return -1; |
| 621 | } |
| 622 | |
| 623 | omsg = nmsgs == 1 ? &dummy : msg; |
| 624 | dmsg = nmsgs == 1 ? msg : msg + 1; |
| 625 | |
| 626 | if (dmsg->flags & I2C_M_RD) |
| 627 | return __i2c_read(dev->base, dmsg->addr, omsg->buf, omsg->len, |
| 628 | dmsg->buf, dmsg->len); |
| 629 | else |
| 630 | return __i2c_write(dev->base, dmsg->addr, omsg->buf, omsg->len, |
| 631 | dmsg->buf, dmsg->len); |
| 632 | } |
| 633 | |
| 634 | static const struct dm_i2c_ops fsl_i2c_ops = { |
| 635 | .xfer = fsl_i2c_xfer, |
| 636 | .probe_chip = fsl_i2c_probe_chip, |
| 637 | .set_bus_speed = fsl_i2c_set_bus_speed, |
| 638 | }; |
| 639 | |
| 640 | static const struct udevice_id fsl_i2c_ids[] = { |
| 641 | { .compatible = "fsl-i2c", }, |
| 642 | { /* sentinel */ } |
| 643 | }; |
| 644 | |
| 645 | U_BOOT_DRIVER(i2c_fsl) = { |
| 646 | .name = "i2c_fsl", |
| 647 | .id = UCLASS_I2C, |
| 648 | .of_match = fsl_i2c_ids, |
| 649 | .probe = fsl_i2c_probe, |
| 650 | .ofdata_to_platdata = fsl_i2c_ofdata_to_platdata, |
| 651 | .priv_auto_alloc_size = sizeof(struct fsl_i2c_dev), |
| 652 | .ops = &fsl_i2c_ops, |
| 653 | }; |
| 654 | |
| 655 | #endif /* CONFIG_DM_I2C */ |