1080c646dSJean-Christophe PLAGNIOL-VILLARD /* 292477a63STimur Tabi * Copyright 2006,2009 Freescale Semiconductor, Inc. 3080c646dSJean-Christophe PLAGNIOL-VILLARD * 400f792e0SHeiko Schocher * 2012, Heiko Schocher, DENX Software Engineering, hs@denx.de. 500f792e0SHeiko Schocher * Changes for multibus/multiadapter I2C support. 600f792e0SHeiko Schocher * 75b8031ccSTom Rini * SPDX-License-Identifier: GPL-2.0 8080c646dSJean-Christophe PLAGNIOL-VILLARD */ 9080c646dSJean-Christophe PLAGNIOL-VILLARD 10080c646dSJean-Christophe PLAGNIOL-VILLARD #include <common.h> 11080c646dSJean-Christophe PLAGNIOL-VILLARD #include <command.h> 12080c646dSJean-Christophe PLAGNIOL-VILLARD #include <i2c.h> /* Functional interface */ 13080c646dSJean-Christophe PLAGNIOL-VILLARD #include <asm/io.h> 14080c646dSJean-Christophe PLAGNIOL-VILLARD #include <asm/fsl_i2c.h> /* HW definitions */ 15080c646dSJean-Christophe PLAGNIOL-VILLARD 1692477a63STimur Tabi /* The maximum number of microseconds we will wait until another master has 1792477a63STimur Tabi * released the bus. If not defined in the board header file, then use a 1892477a63STimur Tabi * generic value. 1992477a63STimur Tabi */ 2092477a63STimur Tabi #ifndef CONFIG_I2C_MBB_TIMEOUT 2192477a63STimur Tabi #define CONFIG_I2C_MBB_TIMEOUT 100000 2292477a63STimur Tabi #endif 2392477a63STimur Tabi 2492477a63STimur Tabi /* The maximum number of microseconds we will wait for a read or write 2592477a63STimur Tabi * operation to complete. If not defined in the board header file, then use a 2692477a63STimur Tabi * generic value. 2792477a63STimur Tabi */ 2892477a63STimur Tabi #ifndef CONFIG_I2C_TIMEOUT 296dd38cc3SShaveta Leekha #define CONFIG_I2C_TIMEOUT 100000 3092477a63STimur Tabi #endif 31080c646dSJean-Christophe PLAGNIOL-VILLARD 32080c646dSJean-Christophe PLAGNIOL-VILLARD #define I2C_READ_BIT 1 33080c646dSJean-Christophe PLAGNIOL-VILLARD #define I2C_WRITE_BIT 0 34080c646dSJean-Christophe PLAGNIOL-VILLARD 35d8c82db4STimur Tabi DECLARE_GLOBAL_DATA_PTR; 36d8c82db4STimur Tabi 37ec2c81c5Smario.six@gdsys.cc static const struct fsl_i2c_base *i2c_base[4] = { 38ec2c81c5Smario.six@gdsys.cc (struct fsl_i2c_base *)(CONFIG_SYS_IMMR + CONFIG_SYS_FSL_I2C_OFFSET), 3900f792e0SHeiko Schocher #ifdef CONFIG_SYS_FSL_I2C2_OFFSET 40ec2c81c5Smario.six@gdsys.cc (struct fsl_i2c_base *)(CONFIG_SYS_IMMR + CONFIG_SYS_FSL_I2C2_OFFSET), 41a17fd10fSShengzhou Liu #endif 42a17fd10fSShengzhou Liu #ifdef CONFIG_SYS_FSL_I2C3_OFFSET 43ec2c81c5Smario.six@gdsys.cc (struct fsl_i2c_base *)(CONFIG_SYS_IMMR + CONFIG_SYS_FSL_I2C3_OFFSET), 44a17fd10fSShengzhou Liu #endif 45a17fd10fSShengzhou Liu #ifdef CONFIG_SYS_FSL_I2C4_OFFSET 46ec2c81c5Smario.six@gdsys.cc (struct fsl_i2c_base *)(CONFIG_SYS_IMMR + CONFIG_SYS_FSL_I2C4_OFFSET) 47080c646dSJean-Christophe PLAGNIOL-VILLARD #endif 48080c646dSJean-Christophe PLAGNIOL-VILLARD }; 49080c646dSJean-Christophe PLAGNIOL-VILLARD 50d8c82db4STimur Tabi /* I2C speed map for a DFSR value of 1 */ 51d8c82db4STimur Tabi 52d8c82db4STimur Tabi /* 53d8c82db4STimur Tabi * Map I2C frequency dividers to FDR and DFSR values 54d8c82db4STimur Tabi * 55d8c82db4STimur Tabi * This structure is used to define the elements of a table that maps I2C 56d8c82db4STimur Tabi * frequency divider (I2C clock rate divided by I2C bus speed) to a value to be 57d8c82db4STimur Tabi * programmed into the Frequency Divider Ratio (FDR) and Digital Filter 58d8c82db4STimur Tabi * Sampling Rate (DFSR) registers. 59d8c82db4STimur Tabi * 60d8c82db4STimur Tabi * The actual table should be defined in the board file, and it must be called 61d8c82db4STimur Tabi * fsl_i2c_speed_map[]. 62d8c82db4STimur Tabi * 63d8c82db4STimur Tabi * The last entry of the table must have a value of {-1, X}, where X is same 64d8c82db4STimur Tabi * FDR/DFSR values as the second-to-last entry. This guarantees that any 65d8c82db4STimur Tabi * search through the array will always find a match. 66d8c82db4STimur Tabi * 67d8c82db4STimur Tabi * The values of the divider must be in increasing numerical order, i.e. 68d8c82db4STimur Tabi * fsl_i2c_speed_map[x+1].divider > fsl_i2c_speed_map[x].divider. 69d8c82db4STimur Tabi * 70d8c82db4STimur Tabi * For this table, the values are based on a value of 1 for the DFSR 71d8c82db4STimur Tabi * register. See the application note AN2919 "Determining the I2C Frequency 72d8c82db4STimur Tabi * Divider Ratio for SCL" 735d9a5efaSTsiChung Liew * 745d9a5efaSTsiChung Liew * ColdFire I2C frequency dividers for FDR values are different from 755d9a5efaSTsiChung Liew * PowerPC. The protocol to use the I2C module is still the same. 765d9a5efaSTsiChung Liew * A different table is defined and are based on MCF5xxx user manual. 775d9a5efaSTsiChung Liew * 78d8c82db4STimur Tabi */ 79d8c82db4STimur Tabi static const struct { 80d8c82db4STimur Tabi unsigned short divider; 81d8c82db4STimur Tabi u8 fdr; 82d8c82db4STimur Tabi } fsl_i2c_speed_map[] = { 8399404202SJoakim Tjernlund #ifdef __M68K__ 845d9a5efaSTsiChung Liew {20, 32}, {22, 33}, {24, 34}, {26, 35}, 855d9a5efaSTsiChung Liew {28, 0}, {28, 36}, {30, 1}, {32, 37}, 865d9a5efaSTsiChung Liew {34, 2}, {36, 38}, {40, 3}, {40, 39}, 875d9a5efaSTsiChung Liew {44, 4}, {48, 5}, {48, 40}, {56, 6}, 885d9a5efaSTsiChung Liew {56, 41}, {64, 42}, {68, 7}, {72, 43}, 895d9a5efaSTsiChung Liew {80, 8}, {80, 44}, {88, 9}, {96, 41}, 905d9a5efaSTsiChung Liew {104, 10}, {112, 42}, {128, 11}, {128, 43}, 915d9a5efaSTsiChung Liew {144, 12}, {160, 13}, {160, 48}, {192, 14}, 925d9a5efaSTsiChung Liew {192, 49}, {224, 50}, {240, 15}, {256, 51}, 935d9a5efaSTsiChung Liew {288, 16}, {320, 17}, {320, 52}, {384, 18}, 945d9a5efaSTsiChung Liew {384, 53}, {448, 54}, {480, 19}, {512, 55}, 955d9a5efaSTsiChung Liew {576, 20}, {640, 21}, {640, 56}, {768, 22}, 965d9a5efaSTsiChung Liew {768, 57}, {960, 23}, {896, 58}, {1024, 59}, 975d9a5efaSTsiChung Liew {1152, 24}, {1280, 25}, {1280, 60}, {1536, 26}, 985d9a5efaSTsiChung Liew {1536, 61}, {1792, 62}, {1920, 27}, {2048, 63}, 995d9a5efaSTsiChung Liew {2304, 28}, {2560, 29}, {3072, 30}, {3840, 31}, 1005d9a5efaSTsiChung Liew {-1, 31} 1015d9a5efaSTsiChung Liew #endif 102d8c82db4STimur Tabi }; 103d8c82db4STimur Tabi 104d8c82db4STimur Tabi /** 105d8c82db4STimur Tabi * Set the I2C bus speed for a given I2C device 106d8c82db4STimur Tabi * 107ec2c81c5Smario.six@gdsys.cc * @param base: the I2C device registers 108d8c82db4STimur Tabi * @i2c_clk: I2C bus clock frequency 109d8c82db4STimur Tabi * @speed: the desired speed of the bus 110d8c82db4STimur Tabi * 111d8c82db4STimur Tabi * The I2C device must be stopped before calling this function. 112d8c82db4STimur Tabi * 113d8c82db4STimur Tabi * The return value is the actual bus speed that is set. 114d8c82db4STimur Tabi */ 115ec2c81c5Smario.six@gdsys.cc static unsigned int set_i2c_bus_speed(const struct fsl_i2c_base *base, 116d8c82db4STimur Tabi unsigned int i2c_clk, unsigned int speed) 117d8c82db4STimur Tabi { 118b4141195SMasahiro Yamada unsigned short divider = min(i2c_clk / speed, (unsigned int)USHRT_MAX); 119d8c82db4STimur Tabi 120d8c82db4STimur Tabi /* 121d8c82db4STimur Tabi * We want to choose an FDR/DFSR that generates an I2C bus speed that 122d8c82db4STimur Tabi * is equal to or lower than the requested speed. That means that we 123d8c82db4STimur Tabi * want the first divider that is equal to or greater than the 124d8c82db4STimur Tabi * calculated divider. 125d8c82db4STimur Tabi */ 1265d9a5efaSTsiChung Liew #ifdef __PPC__ 12799404202SJoakim Tjernlund u8 dfsr, fdr = 0x31; /* Default if no FDR found */ 12899404202SJoakim Tjernlund /* a, b and dfsr matches identifiers A,B and C respectively in AN2919 */ 12999404202SJoakim Tjernlund unsigned short a, b, ga, gb; 13099404202SJoakim Tjernlund unsigned long c_div, est_div; 13199404202SJoakim Tjernlund 132d01ee4dbSJoakim Tjernlund #ifdef CONFIG_FSL_I2C_CUSTOM_DFSR 133d01ee4dbSJoakim Tjernlund dfsr = CONFIG_FSL_I2C_CUSTOM_DFSR; 134d01ee4dbSJoakim Tjernlund #else 13599404202SJoakim Tjernlund /* Condition 1: dfsr <= 50/T */ 13699404202SJoakim Tjernlund dfsr = (5 * (i2c_clk / 1000)) / 100000; 1375d9a5efaSTsiChung Liew #endif 138d01ee4dbSJoakim Tjernlund #ifdef CONFIG_FSL_I2C_CUSTOM_FDR 139d01ee4dbSJoakim Tjernlund fdr = CONFIG_FSL_I2C_CUSTOM_FDR; 140d01ee4dbSJoakim Tjernlund speed = i2c_clk / divider; /* Fake something */ 141d01ee4dbSJoakim Tjernlund #else 14299404202SJoakim Tjernlund debug("Requested speed:%d, i2c_clk:%d\n", speed, i2c_clk); 14399404202SJoakim Tjernlund if (!dfsr) 14499404202SJoakim Tjernlund dfsr = 1; 14599404202SJoakim Tjernlund 14699404202SJoakim Tjernlund est_div = ~0; 14799404202SJoakim Tjernlund for (ga = 0x4, a = 10; a <= 30; ga++, a += 2) { 14899404202SJoakim Tjernlund for (gb = 0; gb < 8; gb++) { 14999404202SJoakim Tjernlund b = 16 << gb; 15099404202SJoakim Tjernlund c_div = b * (a + ((3*dfsr)/b)*2); 15199404202SJoakim Tjernlund if ((c_div > divider) && (c_div < est_div)) { 15299404202SJoakim Tjernlund unsigned short bin_gb, bin_ga; 15399404202SJoakim Tjernlund 15499404202SJoakim Tjernlund est_div = c_div; 15599404202SJoakim Tjernlund bin_gb = gb << 2; 15699404202SJoakim Tjernlund bin_ga = (ga & 0x3) | ((ga & 0x4) << 3); 15799404202SJoakim Tjernlund fdr = bin_gb | bin_ga; 15899404202SJoakim Tjernlund speed = i2c_clk / est_div; 15999404202SJoakim Tjernlund debug("FDR:0x%.2x, div:%ld, ga:0x%x, gb:0x%x, " 16099404202SJoakim Tjernlund "a:%d, b:%d, speed:%d\n", 16199404202SJoakim Tjernlund fdr, est_div, ga, gb, a, b, speed); 16299404202SJoakim Tjernlund /* Condition 2 not accounted for */ 16399404202SJoakim Tjernlund debug("Tr <= %d ns\n", 16499404202SJoakim Tjernlund (b - 3 * dfsr) * 1000000 / 16599404202SJoakim Tjernlund (i2c_clk / 1000)); 16699404202SJoakim Tjernlund } 16799404202SJoakim Tjernlund } 16899404202SJoakim Tjernlund if (a == 20) 16999404202SJoakim Tjernlund a += 2; 17099404202SJoakim Tjernlund if (a == 24) 17199404202SJoakim Tjernlund a += 4; 17299404202SJoakim Tjernlund } 17399404202SJoakim Tjernlund debug("divider:%d, est_div:%ld, DFSR:%d\n", divider, est_div, dfsr); 17499404202SJoakim Tjernlund debug("FDR:0x%.2x, speed:%d\n", fdr, speed); 17599404202SJoakim Tjernlund #endif 176ec2c81c5Smario.six@gdsys.cc writeb(dfsr, &base->dfsrr); /* set default filter */ 177ec2c81c5Smario.six@gdsys.cc writeb(fdr, &base->fdr); /* set bus speed */ 17899404202SJoakim Tjernlund #else 17999404202SJoakim Tjernlund unsigned int i; 18099404202SJoakim Tjernlund 18199404202SJoakim Tjernlund for (i = 0; i < ARRAY_SIZE(fsl_i2c_speed_map); i++) 18299404202SJoakim Tjernlund if (fsl_i2c_speed_map[i].divider >= divider) { 18399404202SJoakim Tjernlund u8 fdr; 18499404202SJoakim Tjernlund 185d01ee4dbSJoakim Tjernlund fdr = fsl_i2c_speed_map[i].fdr; 186d01ee4dbSJoakim Tjernlund speed = i2c_clk / fsl_i2c_speed_map[i].divider; 187ec2c81c5Smario.six@gdsys.cc writeb(fdr, &base->fdr); /* set bus speed */ 188d01ee4dbSJoakim Tjernlund 1893e3f766aSKumar Gala break; 1903e3f766aSKumar Gala } 19199404202SJoakim Tjernlund #endif 192d8c82db4STimur Tabi return speed; 193d8c82db4STimur Tabi } 194d8c82db4STimur Tabi 19562f730ffSKim Phillips static unsigned int get_i2c_clock(int bus) 196c9a8b25eSJerry Huang { 197c9a8b25eSJerry Huang if (bus) 198609e6ec3SSimon Glass return gd->arch.i2c2_clk; /* I2C2 clock */ 199c9a8b25eSJerry Huang else 200609e6ec3SSimon Glass return gd->arch.i2c1_clk; /* I2C1 clock */ 201c9a8b25eSJerry Huang } 202c9a8b25eSJerry Huang 203ec2c81c5Smario.six@gdsys.cc static int fsl_i2c_fixup(const struct fsl_i2c_base *base) 204b8ce3343SChunhe Lan { 205b8ce3343SChunhe Lan const unsigned long long timeout = usec2ticks(CONFIG_I2C_MBB_TIMEOUT); 206b8ce3343SChunhe Lan unsigned long long timeval = 0; 207b8ce3343SChunhe Lan int ret = -1; 2089c3f77ebSChunhe Lan unsigned int flags = 0; 2099c3f77ebSChunhe Lan 2109c3f77ebSChunhe Lan #ifdef CONFIG_SYS_FSL_ERRATUM_I2C_A004447 2119c3f77ebSChunhe Lan unsigned int svr = get_svr(); 2129c3f77ebSChunhe Lan if ((SVR_SOC_VER(svr) == SVR_8548 && IS_SVR_REV(svr, 3, 1)) || 2139c3f77ebSChunhe Lan (SVR_REV(svr) <= CONFIG_SYS_FSL_A004447_SVR_REV)) 2149c3f77ebSChunhe Lan flags = I2C_CR_BIT6; 2159c3f77ebSChunhe Lan #endif 216b8ce3343SChunhe Lan 217ec2c81c5Smario.six@gdsys.cc writeb(I2C_CR_MEN | I2C_CR_MSTA, &base->cr); 218b8ce3343SChunhe Lan 219b8ce3343SChunhe Lan timeval = get_ticks(); 220ec2c81c5Smario.six@gdsys.cc while (!(readb(&base->sr) & I2C_SR_MBB)) { 221b8ce3343SChunhe Lan if ((get_ticks() - timeval) > timeout) 222b8ce3343SChunhe Lan goto err; 223b8ce3343SChunhe Lan } 224b8ce3343SChunhe Lan 225ec2c81c5Smario.six@gdsys.cc if (readb(&base->sr) & I2C_SR_MAL) { 226b8ce3343SChunhe Lan /* SDA is stuck low */ 227ec2c81c5Smario.six@gdsys.cc writeb(0, &base->cr); 228b8ce3343SChunhe Lan udelay(100); 229ec2c81c5Smario.six@gdsys.cc writeb(I2C_CR_MSTA | flags, &base->cr); 230ec2c81c5Smario.six@gdsys.cc writeb(I2C_CR_MEN | I2C_CR_MSTA | flags, &base->cr); 231b8ce3343SChunhe Lan } 232b8ce3343SChunhe Lan 233ec2c81c5Smario.six@gdsys.cc readb(&base->dr); 234b8ce3343SChunhe Lan 235b8ce3343SChunhe Lan timeval = get_ticks(); 236ec2c81c5Smario.six@gdsys.cc while (!(readb(&base->sr) & I2C_SR_MIF)) { 237b8ce3343SChunhe Lan if ((get_ticks() - timeval) > timeout) 238b8ce3343SChunhe Lan goto err; 239b8ce3343SChunhe Lan } 240b8ce3343SChunhe Lan ret = 0; 241b8ce3343SChunhe Lan 242b8ce3343SChunhe Lan err: 243ec2c81c5Smario.six@gdsys.cc writeb(I2C_CR_MEN | flags, &base->cr); 244ec2c81c5Smario.six@gdsys.cc writeb(0, &base->sr); 245b8ce3343SChunhe Lan udelay(100); 246b8ce3343SChunhe Lan 247b8ce3343SChunhe Lan return ret; 248b8ce3343SChunhe Lan } 249b8ce3343SChunhe Lan 250*ecf591e3Smario.six@gdsys.cc static void __i2c_init(const struct fsl_i2c_base *base, int speed, int 251*ecf591e3Smario.six@gdsys.cc slaveadd, int i2c_clk, int busnum) 252080c646dSJean-Christophe PLAGNIOL-VILLARD { 253b8ce3343SChunhe Lan const unsigned long long timeout = usec2ticks(CONFIG_I2C_MBB_TIMEOUT); 254b8ce3343SChunhe Lan unsigned long long timeval; 255080c646dSJean-Christophe PLAGNIOL-VILLARD 25639df00d9SHeiko Schocher #ifdef CONFIG_SYS_I2C_INIT_BOARD 25726a33504SRichard Retanubun /* Call board specific i2c bus reset routine before accessing the 25826a33504SRichard Retanubun * environment, which might be in a chip on that bus. For details 25926a33504SRichard Retanubun * about this problem see doc/I2C_Edge_Conditions. 26026a33504SRichard Retanubun */ 26139df00d9SHeiko Schocher i2c_init_board(); 26239df00d9SHeiko Schocher #endif 263ec2c81c5Smario.six@gdsys.cc writeb(0, &base->cr); /* stop I2C controller */ 264080c646dSJean-Christophe PLAGNIOL-VILLARD udelay(5); /* let it shutdown in peace */ 265*ecf591e3Smario.six@gdsys.cc set_i2c_bus_speed(base, i2c_clk, speed); 266ec2c81c5Smario.six@gdsys.cc writeb(slaveadd << 1, &base->adr);/* write slave address */ 267ec2c81c5Smario.six@gdsys.cc writeb(0x0, &base->sr); /* clear status register */ 268ec2c81c5Smario.six@gdsys.cc writeb(I2C_CR_MEN, &base->cr); /* start I2C controller */ 26926a33504SRichard Retanubun 270b8ce3343SChunhe Lan timeval = get_ticks(); 271ec2c81c5Smario.six@gdsys.cc while (readb(&base->sr) & I2C_SR_MBB) { 272b8ce3343SChunhe Lan if ((get_ticks() - timeval) < timeout) 273b8ce3343SChunhe Lan continue; 274b8ce3343SChunhe Lan 275ec2c81c5Smario.six@gdsys.cc if (fsl_i2c_fixup(base)) 276b8ce3343SChunhe Lan debug("i2c_init: BUS#%d failed to init\n", 277*ecf591e3Smario.six@gdsys.cc busnum); 278b8ce3343SChunhe Lan 279b8ce3343SChunhe Lan break; 280b8ce3343SChunhe Lan } 281b8ce3343SChunhe Lan 28226a33504SRichard Retanubun #ifdef CONFIG_SYS_I2C_BOARD_LATE_INIT 28326a33504SRichard Retanubun /* Call board specific i2c bus reset routine AFTER the bus has been 28426a33504SRichard Retanubun * initialized. Use either this callpoint or i2c_init_board; 28526a33504SRichard Retanubun * which is called before i2c_init operations. 28626a33504SRichard Retanubun * For details about this problem see doc/I2C_Edge_Conditions. 28726a33504SRichard Retanubun */ 28826a33504SRichard Retanubun i2c_board_late_init(); 28926a33504SRichard Retanubun #endif 290080c646dSJean-Christophe PLAGNIOL-VILLARD } 291080c646dSJean-Christophe PLAGNIOL-VILLARD 29221f4cbb7SJoakim Tjernlund static int 293*ecf591e3Smario.six@gdsys.cc i2c_wait4bus(const struct fsl_i2c_base *base) 294080c646dSJean-Christophe PLAGNIOL-VILLARD { 295f2302d44SStefan Roese unsigned long long timeval = get_ticks(); 29692477a63STimur Tabi const unsigned long long timeout = usec2ticks(CONFIG_I2C_MBB_TIMEOUT); 297080c646dSJean-Christophe PLAGNIOL-VILLARD 298ec2c81c5Smario.six@gdsys.cc while (readb(&base->sr) & I2C_SR_MBB) { 29992477a63STimur Tabi if ((get_ticks() - timeval) > timeout) 300080c646dSJean-Christophe PLAGNIOL-VILLARD return -1; 301080c646dSJean-Christophe PLAGNIOL-VILLARD } 302080c646dSJean-Christophe PLAGNIOL-VILLARD 303080c646dSJean-Christophe PLAGNIOL-VILLARD return 0; 304080c646dSJean-Christophe PLAGNIOL-VILLARD } 305080c646dSJean-Christophe PLAGNIOL-VILLARD 306*ecf591e3Smario.six@gdsys.cc static inline int 307*ecf591e3Smario.six@gdsys.cc i2c_wait(const struct fsl_i2c_base *base, int write) 308080c646dSJean-Christophe PLAGNIOL-VILLARD { 309080c646dSJean-Christophe PLAGNIOL-VILLARD u32 csr; 310f2302d44SStefan Roese unsigned long long timeval = get_ticks(); 31192477a63STimur Tabi const unsigned long long timeout = usec2ticks(CONFIG_I2C_TIMEOUT); 312080c646dSJean-Christophe PLAGNIOL-VILLARD 313080c646dSJean-Christophe PLAGNIOL-VILLARD do { 314ec2c81c5Smario.six@gdsys.cc csr = readb(&base->sr); 315080c646dSJean-Christophe PLAGNIOL-VILLARD if (!(csr & I2C_SR_MIF)) 316080c646dSJean-Christophe PLAGNIOL-VILLARD continue; 31721f4cbb7SJoakim Tjernlund /* Read again to allow register to stabilise */ 318ec2c81c5Smario.six@gdsys.cc csr = readb(&base->sr); 319080c646dSJean-Christophe PLAGNIOL-VILLARD 320ec2c81c5Smario.six@gdsys.cc writeb(0x0, &base->sr); 321080c646dSJean-Christophe PLAGNIOL-VILLARD 322080c646dSJean-Christophe PLAGNIOL-VILLARD if (csr & I2C_SR_MAL) { 323080c646dSJean-Christophe PLAGNIOL-VILLARD debug("i2c_wait: MAL\n"); 324080c646dSJean-Christophe PLAGNIOL-VILLARD return -1; 325080c646dSJean-Christophe PLAGNIOL-VILLARD } 326080c646dSJean-Christophe PLAGNIOL-VILLARD 327080c646dSJean-Christophe PLAGNIOL-VILLARD if (!(csr & I2C_SR_MCF)) { 328080c646dSJean-Christophe PLAGNIOL-VILLARD debug("i2c_wait: unfinished\n"); 329080c646dSJean-Christophe PLAGNIOL-VILLARD return -1; 330080c646dSJean-Christophe PLAGNIOL-VILLARD } 331080c646dSJean-Christophe PLAGNIOL-VILLARD 332080c646dSJean-Christophe PLAGNIOL-VILLARD if (write == I2C_WRITE_BIT && (csr & I2C_SR_RXAK)) { 333080c646dSJean-Christophe PLAGNIOL-VILLARD debug("i2c_wait: No RXACK\n"); 334080c646dSJean-Christophe PLAGNIOL-VILLARD return -1; 335080c646dSJean-Christophe PLAGNIOL-VILLARD } 336080c646dSJean-Christophe PLAGNIOL-VILLARD 337080c646dSJean-Christophe PLAGNIOL-VILLARD return 0; 33892477a63STimur Tabi } while ((get_ticks() - timeval) < timeout); 339080c646dSJean-Christophe PLAGNIOL-VILLARD 340080c646dSJean-Christophe PLAGNIOL-VILLARD debug("i2c_wait: timed out\n"); 341080c646dSJean-Christophe PLAGNIOL-VILLARD return -1; 342080c646dSJean-Christophe PLAGNIOL-VILLARD } 343080c646dSJean-Christophe PLAGNIOL-VILLARD 344*ecf591e3Smario.six@gdsys.cc static inline int 345*ecf591e3Smario.six@gdsys.cc i2c_write_addr(const struct fsl_i2c_base *base, u8 dev, u8 dir, int rsta) 346080c646dSJean-Christophe PLAGNIOL-VILLARD { 347080c646dSJean-Christophe PLAGNIOL-VILLARD writeb(I2C_CR_MEN | I2C_CR_MSTA | I2C_CR_MTX 348080c646dSJean-Christophe PLAGNIOL-VILLARD | (rsta ? I2C_CR_RSTA : 0), 349ec2c81c5Smario.six@gdsys.cc &base->cr); 350080c646dSJean-Christophe PLAGNIOL-VILLARD 351ec2c81c5Smario.six@gdsys.cc writeb((dev << 1) | dir, &base->dr); 352080c646dSJean-Christophe PLAGNIOL-VILLARD 353*ecf591e3Smario.six@gdsys.cc if (i2c_wait(base, I2C_WRITE_BIT) < 0) 354080c646dSJean-Christophe PLAGNIOL-VILLARD return 0; 355080c646dSJean-Christophe PLAGNIOL-VILLARD 356080c646dSJean-Christophe PLAGNIOL-VILLARD return 1; 357080c646dSJean-Christophe PLAGNIOL-VILLARD } 358080c646dSJean-Christophe PLAGNIOL-VILLARD 359*ecf591e3Smario.six@gdsys.cc static inline int 360*ecf591e3Smario.six@gdsys.cc __i2c_write_data(const struct fsl_i2c_base *base, u8 *data, int length) 361080c646dSJean-Christophe PLAGNIOL-VILLARD { 362080c646dSJean-Christophe PLAGNIOL-VILLARD int i; 363080c646dSJean-Christophe PLAGNIOL-VILLARD 364080c646dSJean-Christophe PLAGNIOL-VILLARD for (i = 0; i < length; i++) { 365ec2c81c5Smario.six@gdsys.cc writeb(data[i], &base->dr); 366080c646dSJean-Christophe PLAGNIOL-VILLARD 367*ecf591e3Smario.six@gdsys.cc if (i2c_wait(base, I2C_WRITE_BIT) < 0) 368080c646dSJean-Christophe PLAGNIOL-VILLARD break; 369080c646dSJean-Christophe PLAGNIOL-VILLARD } 370080c646dSJean-Christophe PLAGNIOL-VILLARD 371080c646dSJean-Christophe PLAGNIOL-VILLARD return i; 372080c646dSJean-Christophe PLAGNIOL-VILLARD } 373080c646dSJean-Christophe PLAGNIOL-VILLARD 374*ecf591e3Smario.six@gdsys.cc static inline int 375*ecf591e3Smario.six@gdsys.cc __i2c_read_data(const struct fsl_i2c_base *base, u8 *data, int length) 376080c646dSJean-Christophe PLAGNIOL-VILLARD { 377080c646dSJean-Christophe PLAGNIOL-VILLARD int i; 378080c646dSJean-Christophe PLAGNIOL-VILLARD 379080c646dSJean-Christophe PLAGNIOL-VILLARD writeb(I2C_CR_MEN | I2C_CR_MSTA | ((length == 1) ? I2C_CR_TXAK : 0), 380ec2c81c5Smario.six@gdsys.cc &base->cr); 381080c646dSJean-Christophe PLAGNIOL-VILLARD 382080c646dSJean-Christophe PLAGNIOL-VILLARD /* dummy read */ 383ec2c81c5Smario.six@gdsys.cc readb(&base->dr); 384080c646dSJean-Christophe PLAGNIOL-VILLARD 385080c646dSJean-Christophe PLAGNIOL-VILLARD for (i = 0; i < length; i++) { 386*ecf591e3Smario.six@gdsys.cc if (i2c_wait(base, I2C_READ_BIT) < 0) 387080c646dSJean-Christophe PLAGNIOL-VILLARD break; 388080c646dSJean-Christophe PLAGNIOL-VILLARD 389080c646dSJean-Christophe PLAGNIOL-VILLARD /* Generate ack on last next to last byte */ 390080c646dSJean-Christophe PLAGNIOL-VILLARD if (i == length - 2) 391080c646dSJean-Christophe PLAGNIOL-VILLARD writeb(I2C_CR_MEN | I2C_CR_MSTA | I2C_CR_TXAK, 392ec2c81c5Smario.six@gdsys.cc &base->cr); 393080c646dSJean-Christophe PLAGNIOL-VILLARD 394d1c9e5b3SJoakim Tjernlund /* Do not generate stop on last byte */ 395080c646dSJean-Christophe PLAGNIOL-VILLARD if (i == length - 1) 396d1c9e5b3SJoakim Tjernlund writeb(I2C_CR_MEN | I2C_CR_MSTA | I2C_CR_MTX, 397ec2c81c5Smario.six@gdsys.cc &base->cr); 398080c646dSJean-Christophe PLAGNIOL-VILLARD 399ec2c81c5Smario.six@gdsys.cc data[i] = readb(&base->dr); 400080c646dSJean-Christophe PLAGNIOL-VILLARD } 401080c646dSJean-Christophe PLAGNIOL-VILLARD 402080c646dSJean-Christophe PLAGNIOL-VILLARD return i; 403080c646dSJean-Christophe PLAGNIOL-VILLARD } 404080c646dSJean-Christophe PLAGNIOL-VILLARD 40500f792e0SHeiko Schocher static int 406*ecf591e3Smario.six@gdsys.cc __i2c_read(const struct fsl_i2c_base *base, u8 chip_addr, u8 *offset, int olen, 4072b21e960Smario.six@gdsys.cc u8 *data, int dlen) 408080c646dSJean-Christophe PLAGNIOL-VILLARD { 4092b21e960Smario.six@gdsys.cc int ret = -1; /* signal error */ 410080c646dSJean-Christophe PLAGNIOL-VILLARD 411*ecf591e3Smario.six@gdsys.cc if (i2c_wait4bus(base) < 0) 412b778c1b5SReinhard Pfau return -1; 413b778c1b5SReinhard Pfau 414386b2769Smario.six@gdsys.cc /* Some drivers use offset lengths in excess of 4 bytes. These drivers 415386b2769Smario.six@gdsys.cc * adhere to the following convention: 416386b2769Smario.six@gdsys.cc * - the offset length is passed as negative (that is, the absolute 417386b2769Smario.six@gdsys.cc * value of olen is the actual offset length) 418386b2769Smario.six@gdsys.cc * - the offset itself is passed in data, which is overwritten by the 419386b2769Smario.six@gdsys.cc * subsequent read operation 420a405764cSShaveta Leekha */ 4212b21e960Smario.six@gdsys.cc if (olen < 0) { 422*ecf591e3Smario.six@gdsys.cc if (i2c_write_addr(base, chip_addr, I2C_WRITE_BIT, 0) != 0) 423*ecf591e3Smario.six@gdsys.cc ret = __i2c_write_data(base, data, -olen); 424a405764cSShaveta Leekha 42503a112aaSmario.six@gdsys.cc if (ret != -olen) 426a405764cSShaveta Leekha return -1; 427a405764cSShaveta Leekha 428*ecf591e3Smario.six@gdsys.cc if (dlen && i2c_write_addr(base, chip_addr, 4292b21e960Smario.six@gdsys.cc I2C_READ_BIT, 1) != 0) 430*ecf591e3Smario.six@gdsys.cc ret = __i2c_read_data(base, data, dlen); 431a405764cSShaveta Leekha } else { 4322b21e960Smario.six@gdsys.cc if ((!dlen || olen > 0) && 433*ecf591e3Smario.six@gdsys.cc i2c_write_addr(base, chip_addr, I2C_WRITE_BIT, 0) != 0 && 434*ecf591e3Smario.six@gdsys.cc __i2c_write_data(base, offset, olen) == olen) 4352b21e960Smario.six@gdsys.cc ret = 0; /* No error so far */ 436080c646dSJean-Christophe PLAGNIOL-VILLARD 437*ecf591e3Smario.six@gdsys.cc if (dlen && i2c_write_addr(base, chip_addr, I2C_READ_BIT, 4382b21e960Smario.six@gdsys.cc olen ? 1 : 0) != 0) 439*ecf591e3Smario.six@gdsys.cc ret = __i2c_read_data(base, data, dlen); 440a405764cSShaveta Leekha } 441080c646dSJean-Christophe PLAGNIOL-VILLARD 442ec2c81c5Smario.six@gdsys.cc writeb(I2C_CR_MEN, &base->cr); 443080c646dSJean-Christophe PLAGNIOL-VILLARD 444*ecf591e3Smario.six@gdsys.cc if (i2c_wait4bus(base)) /* Wait until STOP */ 445d1c9e5b3SJoakim Tjernlund debug("i2c_read: wait4bus timed out\n"); 446d1c9e5b3SJoakim Tjernlund 4472b21e960Smario.six@gdsys.cc if (ret == dlen) 448080c646dSJean-Christophe PLAGNIOL-VILLARD return 0; 449080c646dSJean-Christophe PLAGNIOL-VILLARD 450080c646dSJean-Christophe PLAGNIOL-VILLARD return -1; 451080c646dSJean-Christophe PLAGNIOL-VILLARD } 452080c646dSJean-Christophe PLAGNIOL-VILLARD 45300f792e0SHeiko Schocher static int 454*ecf591e3Smario.six@gdsys.cc __i2c_write(const struct fsl_i2c_base *base, u8 chip_addr, u8 *offset, int olen, 4552b21e960Smario.six@gdsys.cc u8 *data, int dlen) 456080c646dSJean-Christophe PLAGNIOL-VILLARD { 4572b21e960Smario.six@gdsys.cc int ret = -1; /* signal error */ 458080c646dSJean-Christophe PLAGNIOL-VILLARD 459*ecf591e3Smario.six@gdsys.cc if (i2c_wait4bus(base) < 0) 460b8ce3343SChunhe Lan return -1; 461b8ce3343SChunhe Lan 462*ecf591e3Smario.six@gdsys.cc if (i2c_write_addr(base, chip_addr, I2C_WRITE_BIT, 0) != 0 && 463*ecf591e3Smario.six@gdsys.cc __i2c_write_data(base, offset, olen) == olen) { 464*ecf591e3Smario.six@gdsys.cc ret = __i2c_write_data(base, data, dlen); 465080c646dSJean-Christophe PLAGNIOL-VILLARD } 466080c646dSJean-Christophe PLAGNIOL-VILLARD 467ec2c81c5Smario.six@gdsys.cc writeb(I2C_CR_MEN, &base->cr); 468*ecf591e3Smario.six@gdsys.cc if (i2c_wait4bus(base)) /* Wait until STOP */ 46921f4cbb7SJoakim Tjernlund debug("i2c_write: wait4bus timed out\n"); 470080c646dSJean-Christophe PLAGNIOL-VILLARD 4712b21e960Smario.six@gdsys.cc if (ret == dlen) 472080c646dSJean-Christophe PLAGNIOL-VILLARD return 0; 473080c646dSJean-Christophe PLAGNIOL-VILLARD 474080c646dSJean-Christophe PLAGNIOL-VILLARD return -1; 475080c646dSJean-Christophe PLAGNIOL-VILLARD } 476080c646dSJean-Christophe PLAGNIOL-VILLARD 47700f792e0SHeiko Schocher static int 478*ecf591e3Smario.six@gdsys.cc __i2c_probe_chip(const struct fsl_i2c_base *base, uchar chip) 479080c646dSJean-Christophe PLAGNIOL-VILLARD { 480080c646dSJean-Christophe PLAGNIOL-VILLARD /* For unknow reason the controller will ACK when 481080c646dSJean-Christophe PLAGNIOL-VILLARD * probing for a slave with the same address, so skip 482080c646dSJean-Christophe PLAGNIOL-VILLARD * it. 483080c646dSJean-Christophe PLAGNIOL-VILLARD */ 484ec2c81c5Smario.six@gdsys.cc if (chip == (readb(&base->adr) >> 1)) 485080c646dSJean-Christophe PLAGNIOL-VILLARD return -1; 486080c646dSJean-Christophe PLAGNIOL-VILLARD 487*ecf591e3Smario.six@gdsys.cc return __i2c_read(base, chip, 0, 0, NULL, 0); 488080c646dSJean-Christophe PLAGNIOL-VILLARD } 489080c646dSJean-Christophe PLAGNIOL-VILLARD 490*ecf591e3Smario.six@gdsys.cc static unsigned int __i2c_set_bus_speed(const struct fsl_i2c_base *base, 491*ecf591e3Smario.six@gdsys.cc unsigned int speed, int i2c_clk) 492080c646dSJean-Christophe PLAGNIOL-VILLARD { 493ec2c81c5Smario.six@gdsys.cc writeb(0, &base->cr); /* stop controller */ 494*ecf591e3Smario.six@gdsys.cc set_i2c_bus_speed(base, i2c_clk, speed); 495ec2c81c5Smario.six@gdsys.cc writeb(I2C_CR_MEN, &base->cr); /* start controller */ 496d8c82db4STimur Tabi 497d8c82db4STimur Tabi return 0; 498080c646dSJean-Christophe PLAGNIOL-VILLARD } 499080c646dSJean-Christophe PLAGNIOL-VILLARD 500ad7e657cSmario.six@gdsys.cc static void fsl_i2c_init(struct i2c_adapter *adap, int speed, int slaveadd) 501ad7e657cSmario.six@gdsys.cc { 502*ecf591e3Smario.six@gdsys.cc __i2c_init(i2c_base[adap->hwadapnr], speed, slaveadd, 503*ecf591e3Smario.six@gdsys.cc get_i2c_clock(adap->hwadapnr), adap->hwadapnr); 504ad7e657cSmario.six@gdsys.cc } 505ad7e657cSmario.six@gdsys.cc 506ad7e657cSmario.six@gdsys.cc static int 507ad7e657cSmario.six@gdsys.cc fsl_i2c_probe_chip(struct i2c_adapter *adap, uchar chip) 508ad7e657cSmario.six@gdsys.cc { 509*ecf591e3Smario.six@gdsys.cc return __i2c_probe_chip(i2c_base[adap->hwadapnr], chip); 510ad7e657cSmario.six@gdsys.cc } 511ad7e657cSmario.six@gdsys.cc 512ad7e657cSmario.six@gdsys.cc static int 513ad7e657cSmario.six@gdsys.cc fsl_i2c_read(struct i2c_adapter *adap, u8 chip_addr, uint offset, int olen, 514ad7e657cSmario.six@gdsys.cc u8 *data, int dlen) 515ad7e657cSmario.six@gdsys.cc { 516*ecf591e3Smario.six@gdsys.cc u8 *o = (u8 *)&offset; 517*ecf591e3Smario.six@gdsys.cc return __i2c_read(i2c_base[adap->hwadapnr], chip_addr, &o[4 - olen], 518*ecf591e3Smario.six@gdsys.cc olen, data, dlen); 519ad7e657cSmario.six@gdsys.cc } 520ad7e657cSmario.six@gdsys.cc 521ad7e657cSmario.six@gdsys.cc static int 522ad7e657cSmario.six@gdsys.cc fsl_i2c_write(struct i2c_adapter *adap, u8 chip_addr, uint offset, int olen, 523ad7e657cSmario.six@gdsys.cc u8 *data, int dlen) 524ad7e657cSmario.six@gdsys.cc { 525*ecf591e3Smario.six@gdsys.cc u8 *o = (u8 *)&offset; 526*ecf591e3Smario.six@gdsys.cc return __i2c_write(i2c_base[adap->hwadapnr], chip_addr, &o[4 - olen], 527*ecf591e3Smario.six@gdsys.cc olen, data, dlen); 528ad7e657cSmario.six@gdsys.cc } 529ad7e657cSmario.six@gdsys.cc 530ad7e657cSmario.six@gdsys.cc static unsigned int fsl_i2c_set_bus_speed(struct i2c_adapter *adap, 531ad7e657cSmario.six@gdsys.cc unsigned int speed) 532ad7e657cSmario.six@gdsys.cc { 533*ecf591e3Smario.six@gdsys.cc return __i2c_set_bus_speed(i2c_base[adap->hwadapnr], speed, 534*ecf591e3Smario.six@gdsys.cc get_i2c_clock(adap->hwadapnr)); 535ad7e657cSmario.six@gdsys.cc } 536ad7e657cSmario.six@gdsys.cc 53700f792e0SHeiko Schocher /* 53800f792e0SHeiko Schocher * Register fsl i2c adapters 53900f792e0SHeiko Schocher */ 54016579ecbSmario.six@gdsys.cc U_BOOT_I2C_ADAP_COMPLETE(fsl_0, fsl_i2c_init, fsl_i2c_probe_chip, fsl_i2c_read, 54100f792e0SHeiko Schocher fsl_i2c_write, fsl_i2c_set_bus_speed, 54200f792e0SHeiko Schocher CONFIG_SYS_FSL_I2C_SPEED, CONFIG_SYS_FSL_I2C_SLAVE, 54300f792e0SHeiko Schocher 0) 54400f792e0SHeiko Schocher #ifdef CONFIG_SYS_FSL_I2C2_OFFSET 54516579ecbSmario.six@gdsys.cc U_BOOT_I2C_ADAP_COMPLETE(fsl_1, fsl_i2c_init, fsl_i2c_probe_chip, fsl_i2c_read, 54600f792e0SHeiko Schocher fsl_i2c_write, fsl_i2c_set_bus_speed, 54700f792e0SHeiko Schocher CONFIG_SYS_FSL_I2C2_SPEED, CONFIG_SYS_FSL_I2C2_SLAVE, 54800f792e0SHeiko Schocher 1) 549c1bce4ffSHeiko Schocher #endif 550a17fd10fSShengzhou Liu #ifdef CONFIG_SYS_FSL_I2C3_OFFSET 55116579ecbSmario.six@gdsys.cc U_BOOT_I2C_ADAP_COMPLETE(fsl_2, fsl_i2c_init, fsl_i2c_probe_chip, fsl_i2c_read, 552a17fd10fSShengzhou Liu fsl_i2c_write, fsl_i2c_set_bus_speed, 553a17fd10fSShengzhou Liu CONFIG_SYS_FSL_I2C3_SPEED, CONFIG_SYS_FSL_I2C3_SLAVE, 554a17fd10fSShengzhou Liu 2) 555a17fd10fSShengzhou Liu #endif 556a17fd10fSShengzhou Liu #ifdef CONFIG_SYS_FSL_I2C4_OFFSET 55716579ecbSmario.six@gdsys.cc U_BOOT_I2C_ADAP_COMPLETE(fsl_3, fsl_i2c_init, fsl_i2c_probe_chip, fsl_i2c_read, 558a17fd10fSShengzhou Liu fsl_i2c_write, fsl_i2c_set_bus_speed, 559a17fd10fSShengzhou Liu CONFIG_SYS_FSL_I2C4_SPEED, CONFIG_SYS_FSL_I2C4_SLAVE, 560a17fd10fSShengzhou Liu 3) 561a17fd10fSShengzhou Liu #endif 562