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 * 7080c646dSJean-Christophe PLAGNIOL-VILLARD * This program is free software; you can redistribute it and/or 8080c646dSJean-Christophe PLAGNIOL-VILLARD * modify it under the terms of the GNU General Public License 9080c646dSJean-Christophe PLAGNIOL-VILLARD * Version 2 as published by the Free Software Foundation. 10080c646dSJean-Christophe PLAGNIOL-VILLARD * 11080c646dSJean-Christophe PLAGNIOL-VILLARD * This program is distributed in the hope that it will be useful, 12080c646dSJean-Christophe PLAGNIOL-VILLARD * but WITHOUT ANY WARRANTY; without even the implied warranty of 13080c646dSJean-Christophe PLAGNIOL-VILLARD * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14080c646dSJean-Christophe PLAGNIOL-VILLARD * GNU General Public License for more details. 15080c646dSJean-Christophe PLAGNIOL-VILLARD * 16080c646dSJean-Christophe PLAGNIOL-VILLARD * You should have received a copy of the GNU General Public License 17080c646dSJean-Christophe PLAGNIOL-VILLARD * along with this program; if not, write to the Free Software 18080c646dSJean-Christophe PLAGNIOL-VILLARD * Foundation, Inc., 59 Temple Place, Suite 330, Boston, 19080c646dSJean-Christophe PLAGNIOL-VILLARD * MA 02111-1307 USA 20080c646dSJean-Christophe PLAGNIOL-VILLARD */ 21080c646dSJean-Christophe PLAGNIOL-VILLARD 22080c646dSJean-Christophe PLAGNIOL-VILLARD #include <common.h> 23080c646dSJean-Christophe PLAGNIOL-VILLARD #include <command.h> 24080c646dSJean-Christophe PLAGNIOL-VILLARD #include <i2c.h> /* Functional interface */ 25080c646dSJean-Christophe PLAGNIOL-VILLARD #include <asm/io.h> 26080c646dSJean-Christophe PLAGNIOL-VILLARD #include <asm/fsl_i2c.h> /* HW definitions */ 27080c646dSJean-Christophe PLAGNIOL-VILLARD 2892477a63STimur Tabi /* The maximum number of microseconds we will wait until another master has 2992477a63STimur Tabi * released the bus. If not defined in the board header file, then use a 3092477a63STimur Tabi * generic value. 3192477a63STimur Tabi */ 3292477a63STimur Tabi #ifndef CONFIG_I2C_MBB_TIMEOUT 3392477a63STimur Tabi #define CONFIG_I2C_MBB_TIMEOUT 100000 3492477a63STimur Tabi #endif 3592477a63STimur Tabi 3692477a63STimur Tabi /* The maximum number of microseconds we will wait for a read or write 3792477a63STimur Tabi * operation to complete. If not defined in the board header file, then use a 3892477a63STimur Tabi * generic value. 3992477a63STimur Tabi */ 4092477a63STimur Tabi #ifndef CONFIG_I2C_TIMEOUT 4192477a63STimur Tabi #define CONFIG_I2C_TIMEOUT 10000 4292477a63STimur Tabi #endif 43080c646dSJean-Christophe PLAGNIOL-VILLARD 44080c646dSJean-Christophe PLAGNIOL-VILLARD #define I2C_READ_BIT 1 45080c646dSJean-Christophe PLAGNIOL-VILLARD #define I2C_WRITE_BIT 0 46080c646dSJean-Christophe PLAGNIOL-VILLARD 47d8c82db4STimur Tabi DECLARE_GLOBAL_DATA_PTR; 48d8c82db4STimur Tabi 49d8c82db4STimur Tabi static const struct fsl_i2c *i2c_dev[2] = { 5000f792e0SHeiko Schocher (struct fsl_i2c *)(CONFIG_SYS_IMMR + CONFIG_SYS_FSL_I2C_OFFSET), 5100f792e0SHeiko Schocher #ifdef CONFIG_SYS_FSL_I2C2_OFFSET 5200f792e0SHeiko Schocher (struct fsl_i2c *)(CONFIG_SYS_IMMR + CONFIG_SYS_FSL_I2C2_OFFSET) 53080c646dSJean-Christophe PLAGNIOL-VILLARD #endif 54080c646dSJean-Christophe PLAGNIOL-VILLARD }; 55080c646dSJean-Christophe PLAGNIOL-VILLARD 56d8c82db4STimur Tabi /* I2C speed map for a DFSR value of 1 */ 57d8c82db4STimur Tabi 58d8c82db4STimur Tabi /* 59d8c82db4STimur Tabi * Map I2C frequency dividers to FDR and DFSR values 60d8c82db4STimur Tabi * 61d8c82db4STimur Tabi * This structure is used to define the elements of a table that maps I2C 62d8c82db4STimur Tabi * frequency divider (I2C clock rate divided by I2C bus speed) to a value to be 63d8c82db4STimur Tabi * programmed into the Frequency Divider Ratio (FDR) and Digital Filter 64d8c82db4STimur Tabi * Sampling Rate (DFSR) registers. 65d8c82db4STimur Tabi * 66d8c82db4STimur Tabi * The actual table should be defined in the board file, and it must be called 67d8c82db4STimur Tabi * fsl_i2c_speed_map[]. 68d8c82db4STimur Tabi * 69d8c82db4STimur Tabi * The last entry of the table must have a value of {-1, X}, where X is same 70d8c82db4STimur Tabi * FDR/DFSR values as the second-to-last entry. This guarantees that any 71d8c82db4STimur Tabi * search through the array will always find a match. 72d8c82db4STimur Tabi * 73d8c82db4STimur Tabi * The values of the divider must be in increasing numerical order, i.e. 74d8c82db4STimur Tabi * fsl_i2c_speed_map[x+1].divider > fsl_i2c_speed_map[x].divider. 75d8c82db4STimur Tabi * 76d8c82db4STimur Tabi * For this table, the values are based on a value of 1 for the DFSR 77d8c82db4STimur Tabi * register. See the application note AN2919 "Determining the I2C Frequency 78d8c82db4STimur Tabi * Divider Ratio for SCL" 795d9a5efaSTsiChung Liew * 805d9a5efaSTsiChung Liew * ColdFire I2C frequency dividers for FDR values are different from 815d9a5efaSTsiChung Liew * PowerPC. The protocol to use the I2C module is still the same. 825d9a5efaSTsiChung Liew * A different table is defined and are based on MCF5xxx user manual. 835d9a5efaSTsiChung Liew * 84d8c82db4STimur Tabi */ 85d8c82db4STimur Tabi static const struct { 86d8c82db4STimur Tabi unsigned short divider; 87d8c82db4STimur Tabi u8 fdr; 88d8c82db4STimur Tabi } fsl_i2c_speed_map[] = { 8999404202SJoakim Tjernlund #ifdef __M68K__ 905d9a5efaSTsiChung Liew {20, 32}, {22, 33}, {24, 34}, {26, 35}, 915d9a5efaSTsiChung Liew {28, 0}, {28, 36}, {30, 1}, {32, 37}, 925d9a5efaSTsiChung Liew {34, 2}, {36, 38}, {40, 3}, {40, 39}, 935d9a5efaSTsiChung Liew {44, 4}, {48, 5}, {48, 40}, {56, 6}, 945d9a5efaSTsiChung Liew {56, 41}, {64, 42}, {68, 7}, {72, 43}, 955d9a5efaSTsiChung Liew {80, 8}, {80, 44}, {88, 9}, {96, 41}, 965d9a5efaSTsiChung Liew {104, 10}, {112, 42}, {128, 11}, {128, 43}, 975d9a5efaSTsiChung Liew {144, 12}, {160, 13}, {160, 48}, {192, 14}, 985d9a5efaSTsiChung Liew {192, 49}, {224, 50}, {240, 15}, {256, 51}, 995d9a5efaSTsiChung Liew {288, 16}, {320, 17}, {320, 52}, {384, 18}, 1005d9a5efaSTsiChung Liew {384, 53}, {448, 54}, {480, 19}, {512, 55}, 1015d9a5efaSTsiChung Liew {576, 20}, {640, 21}, {640, 56}, {768, 22}, 1025d9a5efaSTsiChung Liew {768, 57}, {960, 23}, {896, 58}, {1024, 59}, 1035d9a5efaSTsiChung Liew {1152, 24}, {1280, 25}, {1280, 60}, {1536, 26}, 1045d9a5efaSTsiChung Liew {1536, 61}, {1792, 62}, {1920, 27}, {2048, 63}, 1055d9a5efaSTsiChung Liew {2304, 28}, {2560, 29}, {3072, 30}, {3840, 31}, 1065d9a5efaSTsiChung Liew {-1, 31} 1075d9a5efaSTsiChung Liew #endif 108d8c82db4STimur Tabi }; 109d8c82db4STimur Tabi 110d8c82db4STimur Tabi /** 111d8c82db4STimur Tabi * Set the I2C bus speed for a given I2C device 112d8c82db4STimur Tabi * 113d8c82db4STimur Tabi * @param dev: the I2C device 114d8c82db4STimur Tabi * @i2c_clk: I2C bus clock frequency 115d8c82db4STimur Tabi * @speed: the desired speed of the bus 116d8c82db4STimur Tabi * 117d8c82db4STimur Tabi * The I2C device must be stopped before calling this function. 118d8c82db4STimur Tabi * 119d8c82db4STimur Tabi * The return value is the actual bus speed that is set. 120d8c82db4STimur Tabi */ 121d8c82db4STimur Tabi static unsigned int set_i2c_bus_speed(const struct fsl_i2c *dev, 122d8c82db4STimur Tabi unsigned int i2c_clk, unsigned int speed) 123d8c82db4STimur Tabi { 124d8c82db4STimur Tabi unsigned short divider = min(i2c_clk / speed, (unsigned short) -1); 125d8c82db4STimur Tabi 126d8c82db4STimur Tabi /* 127d8c82db4STimur Tabi * We want to choose an FDR/DFSR that generates an I2C bus speed that 128d8c82db4STimur Tabi * is equal to or lower than the requested speed. That means that we 129d8c82db4STimur Tabi * want the first divider that is equal to or greater than the 130d8c82db4STimur Tabi * calculated divider. 131d8c82db4STimur Tabi */ 1325d9a5efaSTsiChung Liew #ifdef __PPC__ 13399404202SJoakim Tjernlund u8 dfsr, fdr = 0x31; /* Default if no FDR found */ 13499404202SJoakim Tjernlund /* a, b and dfsr matches identifiers A,B and C respectively in AN2919 */ 13599404202SJoakim Tjernlund unsigned short a, b, ga, gb; 13699404202SJoakim Tjernlund unsigned long c_div, est_div; 13799404202SJoakim Tjernlund 138d01ee4dbSJoakim Tjernlund #ifdef CONFIG_FSL_I2C_CUSTOM_DFSR 139d01ee4dbSJoakim Tjernlund dfsr = CONFIG_FSL_I2C_CUSTOM_DFSR; 140d01ee4dbSJoakim Tjernlund #else 14199404202SJoakim Tjernlund /* Condition 1: dfsr <= 50/T */ 14299404202SJoakim Tjernlund dfsr = (5 * (i2c_clk / 1000)) / 100000; 1435d9a5efaSTsiChung Liew #endif 144d01ee4dbSJoakim Tjernlund #ifdef CONFIG_FSL_I2C_CUSTOM_FDR 145d01ee4dbSJoakim Tjernlund fdr = CONFIG_FSL_I2C_CUSTOM_FDR; 146d01ee4dbSJoakim Tjernlund speed = i2c_clk / divider; /* Fake something */ 147d01ee4dbSJoakim Tjernlund #else 14899404202SJoakim Tjernlund debug("Requested speed:%d, i2c_clk:%d\n", speed, i2c_clk); 14999404202SJoakim Tjernlund if (!dfsr) 15099404202SJoakim Tjernlund dfsr = 1; 15199404202SJoakim Tjernlund 15299404202SJoakim Tjernlund est_div = ~0; 15399404202SJoakim Tjernlund for (ga = 0x4, a = 10; a <= 30; ga++, a += 2) { 15499404202SJoakim Tjernlund for (gb = 0; gb < 8; gb++) { 15599404202SJoakim Tjernlund b = 16 << gb; 15699404202SJoakim Tjernlund c_div = b * (a + ((3*dfsr)/b)*2); 15799404202SJoakim Tjernlund if ((c_div > divider) && (c_div < est_div)) { 15899404202SJoakim Tjernlund unsigned short bin_gb, bin_ga; 15999404202SJoakim Tjernlund 16099404202SJoakim Tjernlund est_div = c_div; 16199404202SJoakim Tjernlund bin_gb = gb << 2; 16299404202SJoakim Tjernlund bin_ga = (ga & 0x3) | ((ga & 0x4) << 3); 16399404202SJoakim Tjernlund fdr = bin_gb | bin_ga; 16499404202SJoakim Tjernlund speed = i2c_clk / est_div; 16599404202SJoakim Tjernlund debug("FDR:0x%.2x, div:%ld, ga:0x%x, gb:0x%x, " 16699404202SJoakim Tjernlund "a:%d, b:%d, speed:%d\n", 16799404202SJoakim Tjernlund fdr, est_div, ga, gb, a, b, speed); 16899404202SJoakim Tjernlund /* Condition 2 not accounted for */ 16999404202SJoakim Tjernlund debug("Tr <= %d ns\n", 17099404202SJoakim Tjernlund (b - 3 * dfsr) * 1000000 / 17199404202SJoakim Tjernlund (i2c_clk / 1000)); 17299404202SJoakim Tjernlund } 17399404202SJoakim Tjernlund } 17499404202SJoakim Tjernlund if (a == 20) 17599404202SJoakim Tjernlund a += 2; 17699404202SJoakim Tjernlund if (a == 24) 17799404202SJoakim Tjernlund a += 4; 17899404202SJoakim Tjernlund } 17999404202SJoakim Tjernlund debug("divider:%d, est_div:%ld, DFSR:%d\n", divider, est_div, dfsr); 18099404202SJoakim Tjernlund debug("FDR:0x%.2x, speed:%d\n", fdr, speed); 18199404202SJoakim Tjernlund #endif 18299404202SJoakim Tjernlund writeb(dfsr, &dev->dfsrr); /* set default filter */ 18399404202SJoakim Tjernlund writeb(fdr, &dev->fdr); /* set bus speed */ 18499404202SJoakim Tjernlund #else 18599404202SJoakim Tjernlund unsigned int i; 18699404202SJoakim Tjernlund 18799404202SJoakim Tjernlund for (i = 0; i < ARRAY_SIZE(fsl_i2c_speed_map); i++) 18899404202SJoakim Tjernlund if (fsl_i2c_speed_map[i].divider >= divider) { 18999404202SJoakim Tjernlund u8 fdr; 19099404202SJoakim Tjernlund 191d01ee4dbSJoakim Tjernlund fdr = fsl_i2c_speed_map[i].fdr; 192d01ee4dbSJoakim Tjernlund speed = i2c_clk / fsl_i2c_speed_map[i].divider; 193d01ee4dbSJoakim Tjernlund writeb(fdr, &dev->fdr); /* set bus speed */ 194d01ee4dbSJoakim Tjernlund 1953e3f766aSKumar Gala break; 1963e3f766aSKumar Gala } 19799404202SJoakim Tjernlund #endif 198d8c82db4STimur Tabi return speed; 199d8c82db4STimur Tabi } 200d8c82db4STimur Tabi 20162f730ffSKim Phillips static unsigned int get_i2c_clock(int bus) 202c9a8b25eSJerry Huang { 203c9a8b25eSJerry Huang if (bus) 204609e6ec3SSimon Glass return gd->arch.i2c2_clk; /* I2C2 clock */ 205c9a8b25eSJerry Huang else 206609e6ec3SSimon Glass return gd->arch.i2c1_clk; /* I2C1 clock */ 207c9a8b25eSJerry Huang } 208c9a8b25eSJerry Huang 209b8ce3343SChunhe Lan static int fsl_i2c_fixup(const struct fsl_i2c *dev) 210b8ce3343SChunhe Lan { 211b8ce3343SChunhe Lan const unsigned long long timeout = usec2ticks(CONFIG_I2C_MBB_TIMEOUT); 212b8ce3343SChunhe Lan unsigned long long timeval = 0; 213b8ce3343SChunhe Lan int ret = -1; 2149c3f77ebSChunhe Lan unsigned int flags = 0; 2159c3f77ebSChunhe Lan 2169c3f77ebSChunhe Lan #ifdef CONFIG_SYS_FSL_ERRATUM_I2C_A004447 2179c3f77ebSChunhe Lan unsigned int svr = get_svr(); 2189c3f77ebSChunhe Lan if ((SVR_SOC_VER(svr) == SVR_8548 && IS_SVR_REV(svr, 3, 1)) || 2199c3f77ebSChunhe Lan (SVR_REV(svr) <= CONFIG_SYS_FSL_A004447_SVR_REV)) 2209c3f77ebSChunhe Lan flags = I2C_CR_BIT6; 2219c3f77ebSChunhe Lan #endif 222b8ce3343SChunhe Lan 223b8ce3343SChunhe Lan writeb(I2C_CR_MEN | I2C_CR_MSTA, &dev->cr); 224b8ce3343SChunhe Lan 225b8ce3343SChunhe Lan timeval = get_ticks(); 226b8ce3343SChunhe Lan while (!(readb(&dev->sr) & I2C_SR_MBB)) { 227b8ce3343SChunhe Lan if ((get_ticks() - timeval) > timeout) 228b8ce3343SChunhe Lan goto err; 229b8ce3343SChunhe Lan } 230b8ce3343SChunhe Lan 231b8ce3343SChunhe Lan if (readb(&dev->sr) & I2C_SR_MAL) { 232b8ce3343SChunhe Lan /* SDA is stuck low */ 233b8ce3343SChunhe Lan writeb(0, &dev->cr); 234b8ce3343SChunhe Lan udelay(100); 2359c3f77ebSChunhe Lan writeb(I2C_CR_MSTA | flags, &dev->cr); 2369c3f77ebSChunhe Lan writeb(I2C_CR_MEN | I2C_CR_MSTA | flags, &dev->cr); 237b8ce3343SChunhe Lan } 238b8ce3343SChunhe Lan 239b8ce3343SChunhe Lan readb(&dev->dr); 240b8ce3343SChunhe Lan 241b8ce3343SChunhe Lan timeval = get_ticks(); 242b8ce3343SChunhe Lan while (!(readb(&dev->sr) & I2C_SR_MIF)) { 243b8ce3343SChunhe Lan if ((get_ticks() - timeval) > timeout) 244b8ce3343SChunhe Lan goto err; 245b8ce3343SChunhe Lan } 246b8ce3343SChunhe Lan ret = 0; 247b8ce3343SChunhe Lan 248b8ce3343SChunhe Lan err: 2499c3f77ebSChunhe Lan writeb(I2C_CR_MEN | flags, &dev->cr); 250b8ce3343SChunhe Lan writeb(0, &dev->sr); 251b8ce3343SChunhe Lan udelay(100); 252b8ce3343SChunhe Lan 253b8ce3343SChunhe Lan return ret; 254b8ce3343SChunhe Lan } 255b8ce3343SChunhe Lan 25600f792e0SHeiko Schocher static void fsl_i2c_init(struct i2c_adapter *adap, int speed, int slaveadd) 257080c646dSJean-Christophe PLAGNIOL-VILLARD { 258aa551215SKumar Gala const struct fsl_i2c *dev; 259b8ce3343SChunhe Lan const unsigned long long timeout = usec2ticks(CONFIG_I2C_MBB_TIMEOUT); 260b8ce3343SChunhe Lan unsigned long long timeval; 261080c646dSJean-Christophe PLAGNIOL-VILLARD 26239df00d9SHeiko Schocher #ifdef CONFIG_SYS_I2C_INIT_BOARD 26326a33504SRichard Retanubun /* Call board specific i2c bus reset routine before accessing the 26426a33504SRichard Retanubun * environment, which might be in a chip on that bus. For details 26526a33504SRichard Retanubun * about this problem see doc/I2C_Edge_Conditions. 26626a33504SRichard Retanubun */ 26739df00d9SHeiko Schocher i2c_init_board(); 26839df00d9SHeiko Schocher #endif 26900f792e0SHeiko Schocher dev = (struct fsl_i2c *)i2c_dev[adap->hwadapnr]; 270080c646dSJean-Christophe PLAGNIOL-VILLARD 271080c646dSJean-Christophe PLAGNIOL-VILLARD writeb(0, &dev->cr); /* stop I2C controller */ 272080c646dSJean-Christophe PLAGNIOL-VILLARD udelay(5); /* let it shutdown in peace */ 27300f792e0SHeiko Schocher set_i2c_bus_speed(dev, get_i2c_clock(adap->hwadapnr), speed); 274080c646dSJean-Christophe PLAGNIOL-VILLARD writeb(slaveadd << 1, &dev->adr);/* write slave address */ 275080c646dSJean-Christophe PLAGNIOL-VILLARD writeb(0x0, &dev->sr); /* clear status register */ 276080c646dSJean-Christophe PLAGNIOL-VILLARD writeb(I2C_CR_MEN, &dev->cr); /* start I2C controller */ 27726a33504SRichard Retanubun 278b8ce3343SChunhe Lan timeval = get_ticks(); 279b8ce3343SChunhe Lan while (readb(&dev->sr) & I2C_SR_MBB) { 280b8ce3343SChunhe Lan if ((get_ticks() - timeval) < timeout) 281b8ce3343SChunhe Lan continue; 282b8ce3343SChunhe Lan 283b8ce3343SChunhe Lan if (fsl_i2c_fixup(dev)) 284b8ce3343SChunhe Lan debug("i2c_init: BUS#%d failed to init\n", 285b8ce3343SChunhe Lan adap->hwadapnr); 286b8ce3343SChunhe Lan 287b8ce3343SChunhe Lan break; 288b8ce3343SChunhe Lan } 289b8ce3343SChunhe Lan 29026a33504SRichard Retanubun #ifdef CONFIG_SYS_I2C_BOARD_LATE_INIT 29126a33504SRichard Retanubun /* Call board specific i2c bus reset routine AFTER the bus has been 29226a33504SRichard Retanubun * initialized. Use either this callpoint or i2c_init_board; 29326a33504SRichard Retanubun * which is called before i2c_init operations. 29426a33504SRichard Retanubun * For details about this problem see doc/I2C_Edge_Conditions. 29526a33504SRichard Retanubun */ 29626a33504SRichard Retanubun i2c_board_late_init(); 29726a33504SRichard Retanubun #endif 298080c646dSJean-Christophe PLAGNIOL-VILLARD } 299080c646dSJean-Christophe PLAGNIOL-VILLARD 30021f4cbb7SJoakim Tjernlund static int 30100f792e0SHeiko Schocher i2c_wait4bus(struct i2c_adapter *adap) 302080c646dSJean-Christophe PLAGNIOL-VILLARD { 30300f792e0SHeiko Schocher struct fsl_i2c *dev = (struct fsl_i2c *)i2c_dev[adap->hwadapnr]; 304f2302d44SStefan Roese unsigned long long timeval = get_ticks(); 30592477a63STimur Tabi const unsigned long long timeout = usec2ticks(CONFIG_I2C_MBB_TIMEOUT); 306080c646dSJean-Christophe PLAGNIOL-VILLARD 30700f792e0SHeiko Schocher while (readb(&dev->sr) & I2C_SR_MBB) { 30892477a63STimur Tabi if ((get_ticks() - timeval) > timeout) 309080c646dSJean-Christophe PLAGNIOL-VILLARD return -1; 310080c646dSJean-Christophe PLAGNIOL-VILLARD } 311080c646dSJean-Christophe PLAGNIOL-VILLARD 312080c646dSJean-Christophe PLAGNIOL-VILLARD return 0; 313080c646dSJean-Christophe PLAGNIOL-VILLARD } 314080c646dSJean-Christophe PLAGNIOL-VILLARD 315080c646dSJean-Christophe PLAGNIOL-VILLARD static __inline__ int 31600f792e0SHeiko Schocher i2c_wait(struct i2c_adapter *adap, int write) 317080c646dSJean-Christophe PLAGNIOL-VILLARD { 318080c646dSJean-Christophe PLAGNIOL-VILLARD u32 csr; 319f2302d44SStefan Roese unsigned long long timeval = get_ticks(); 32092477a63STimur Tabi const unsigned long long timeout = usec2ticks(CONFIG_I2C_TIMEOUT); 32100f792e0SHeiko Schocher struct fsl_i2c *dev = (struct fsl_i2c *)i2c_dev[adap->hwadapnr]; 322080c646dSJean-Christophe PLAGNIOL-VILLARD 323080c646dSJean-Christophe PLAGNIOL-VILLARD do { 32400f792e0SHeiko Schocher csr = readb(&dev->sr); 325080c646dSJean-Christophe PLAGNIOL-VILLARD if (!(csr & I2C_SR_MIF)) 326080c646dSJean-Christophe PLAGNIOL-VILLARD continue; 32721f4cbb7SJoakim Tjernlund /* Read again to allow register to stabilise */ 32800f792e0SHeiko Schocher csr = readb(&dev->sr); 329080c646dSJean-Christophe PLAGNIOL-VILLARD 33000f792e0SHeiko Schocher writeb(0x0, &dev->sr); 331080c646dSJean-Christophe PLAGNIOL-VILLARD 332080c646dSJean-Christophe PLAGNIOL-VILLARD if (csr & I2C_SR_MAL) { 333080c646dSJean-Christophe PLAGNIOL-VILLARD debug("i2c_wait: MAL\n"); 334080c646dSJean-Christophe PLAGNIOL-VILLARD return -1; 335080c646dSJean-Christophe PLAGNIOL-VILLARD } 336080c646dSJean-Christophe PLAGNIOL-VILLARD 337080c646dSJean-Christophe PLAGNIOL-VILLARD if (!(csr & I2C_SR_MCF)) { 338080c646dSJean-Christophe PLAGNIOL-VILLARD debug("i2c_wait: unfinished\n"); 339080c646dSJean-Christophe PLAGNIOL-VILLARD return -1; 340080c646dSJean-Christophe PLAGNIOL-VILLARD } 341080c646dSJean-Christophe PLAGNIOL-VILLARD 342080c646dSJean-Christophe PLAGNIOL-VILLARD if (write == I2C_WRITE_BIT && (csr & I2C_SR_RXAK)) { 343080c646dSJean-Christophe PLAGNIOL-VILLARD debug("i2c_wait: No RXACK\n"); 344080c646dSJean-Christophe PLAGNIOL-VILLARD return -1; 345080c646dSJean-Christophe PLAGNIOL-VILLARD } 346080c646dSJean-Christophe PLAGNIOL-VILLARD 347080c646dSJean-Christophe PLAGNIOL-VILLARD return 0; 34892477a63STimur Tabi } while ((get_ticks() - timeval) < timeout); 349080c646dSJean-Christophe PLAGNIOL-VILLARD 350080c646dSJean-Christophe PLAGNIOL-VILLARD debug("i2c_wait: timed out\n"); 351080c646dSJean-Christophe PLAGNIOL-VILLARD return -1; 352080c646dSJean-Christophe PLAGNIOL-VILLARD } 353080c646dSJean-Christophe PLAGNIOL-VILLARD 354080c646dSJean-Christophe PLAGNIOL-VILLARD static __inline__ int 35500f792e0SHeiko Schocher i2c_write_addr(struct i2c_adapter *adap, u8 dev, u8 dir, int rsta) 356080c646dSJean-Christophe PLAGNIOL-VILLARD { 35700f792e0SHeiko Schocher struct fsl_i2c *device = (struct fsl_i2c *)i2c_dev[adap->hwadapnr]; 35800f792e0SHeiko Schocher 359080c646dSJean-Christophe PLAGNIOL-VILLARD writeb(I2C_CR_MEN | I2C_CR_MSTA | I2C_CR_MTX 360080c646dSJean-Christophe PLAGNIOL-VILLARD | (rsta ? I2C_CR_RSTA : 0), 36100f792e0SHeiko Schocher &device->cr); 362080c646dSJean-Christophe PLAGNIOL-VILLARD 36300f792e0SHeiko Schocher writeb((dev << 1) | dir, &device->dr); 364080c646dSJean-Christophe PLAGNIOL-VILLARD 36500f792e0SHeiko Schocher if (i2c_wait(adap, I2C_WRITE_BIT) < 0) 366080c646dSJean-Christophe PLAGNIOL-VILLARD return 0; 367080c646dSJean-Christophe PLAGNIOL-VILLARD 368080c646dSJean-Christophe PLAGNIOL-VILLARD return 1; 369080c646dSJean-Christophe PLAGNIOL-VILLARD } 370080c646dSJean-Christophe PLAGNIOL-VILLARD 371080c646dSJean-Christophe PLAGNIOL-VILLARD static __inline__ int 37200f792e0SHeiko Schocher __i2c_write(struct i2c_adapter *adap, u8 *data, int length) 373080c646dSJean-Christophe PLAGNIOL-VILLARD { 37400f792e0SHeiko Schocher struct fsl_i2c *dev = (struct fsl_i2c *)i2c_dev[adap->hwadapnr]; 375080c646dSJean-Christophe PLAGNIOL-VILLARD int i; 376080c646dSJean-Christophe PLAGNIOL-VILLARD 377080c646dSJean-Christophe PLAGNIOL-VILLARD for (i = 0; i < length; i++) { 37800f792e0SHeiko Schocher writeb(data[i], &dev->dr); 379080c646dSJean-Christophe PLAGNIOL-VILLARD 38000f792e0SHeiko Schocher if (i2c_wait(adap, I2C_WRITE_BIT) < 0) 381080c646dSJean-Christophe PLAGNIOL-VILLARD break; 382080c646dSJean-Christophe PLAGNIOL-VILLARD } 383080c646dSJean-Christophe PLAGNIOL-VILLARD 384080c646dSJean-Christophe PLAGNIOL-VILLARD return i; 385080c646dSJean-Christophe PLAGNIOL-VILLARD } 386080c646dSJean-Christophe PLAGNIOL-VILLARD 387080c646dSJean-Christophe PLAGNIOL-VILLARD static __inline__ int 38800f792e0SHeiko Schocher __i2c_read(struct i2c_adapter *adap, u8 *data, int length) 389080c646dSJean-Christophe PLAGNIOL-VILLARD { 39000f792e0SHeiko Schocher struct fsl_i2c *dev = (struct fsl_i2c *)i2c_dev[adap->hwadapnr]; 391080c646dSJean-Christophe PLAGNIOL-VILLARD int i; 392080c646dSJean-Christophe PLAGNIOL-VILLARD 393080c646dSJean-Christophe PLAGNIOL-VILLARD writeb(I2C_CR_MEN | I2C_CR_MSTA | ((length == 1) ? I2C_CR_TXAK : 0), 39400f792e0SHeiko Schocher &dev->cr); 395080c646dSJean-Christophe PLAGNIOL-VILLARD 396080c646dSJean-Christophe PLAGNIOL-VILLARD /* dummy read */ 39700f792e0SHeiko Schocher readb(&dev->dr); 398080c646dSJean-Christophe PLAGNIOL-VILLARD 399080c646dSJean-Christophe PLAGNIOL-VILLARD for (i = 0; i < length; i++) { 40000f792e0SHeiko Schocher if (i2c_wait(adap, I2C_READ_BIT) < 0) 401080c646dSJean-Christophe PLAGNIOL-VILLARD break; 402080c646dSJean-Christophe PLAGNIOL-VILLARD 403080c646dSJean-Christophe PLAGNIOL-VILLARD /* Generate ack on last next to last byte */ 404080c646dSJean-Christophe PLAGNIOL-VILLARD if (i == length - 2) 405080c646dSJean-Christophe PLAGNIOL-VILLARD writeb(I2C_CR_MEN | I2C_CR_MSTA | I2C_CR_TXAK, 40600f792e0SHeiko Schocher &dev->cr); 407080c646dSJean-Christophe PLAGNIOL-VILLARD 408d1c9e5b3SJoakim Tjernlund /* Do not generate stop on last byte */ 409080c646dSJean-Christophe PLAGNIOL-VILLARD if (i == length - 1) 410d1c9e5b3SJoakim Tjernlund writeb(I2C_CR_MEN | I2C_CR_MSTA | I2C_CR_MTX, 41100f792e0SHeiko Schocher &dev->cr); 412080c646dSJean-Christophe PLAGNIOL-VILLARD 41300f792e0SHeiko Schocher data[i] = readb(&dev->dr); 414080c646dSJean-Christophe PLAGNIOL-VILLARD } 415080c646dSJean-Christophe PLAGNIOL-VILLARD 416080c646dSJean-Christophe PLAGNIOL-VILLARD return i; 417080c646dSJean-Christophe PLAGNIOL-VILLARD } 418080c646dSJean-Christophe PLAGNIOL-VILLARD 41900f792e0SHeiko Schocher static int 42000f792e0SHeiko Schocher fsl_i2c_read(struct i2c_adapter *adap, u8 dev, uint addr, int alen, u8 *data, 42100f792e0SHeiko Schocher int length) 422080c646dSJean-Christophe PLAGNIOL-VILLARD { 42300f792e0SHeiko Schocher struct fsl_i2c *device = (struct fsl_i2c *)i2c_dev[adap->hwadapnr]; 424080c646dSJean-Christophe PLAGNIOL-VILLARD int i = -1; /* signal error */ 425080c646dSJean-Christophe PLAGNIOL-VILLARD u8 *a = (u8*)&addr; 426*a405764cSShaveta Leekha int len = alen * -1; 427080c646dSJean-Christophe PLAGNIOL-VILLARD 42800f792e0SHeiko Schocher if (i2c_wait4bus(adap) < 0) 429b778c1b5SReinhard Pfau return -1; 430b778c1b5SReinhard Pfau 431*a405764cSShaveta Leekha /* To handle the need of I2C devices that require to write few bytes 432*a405764cSShaveta Leekha * (more than 4 bytes of address as in the case of else part) 433*a405764cSShaveta Leekha * of data before reading, Negative equivalent of length(bytes to write) 434*a405764cSShaveta Leekha * is passed, but used the +ve part of len for writing data 435*a405764cSShaveta Leekha */ 436*a405764cSShaveta Leekha if (alen < 0) { 437*a405764cSShaveta Leekha /* Generate a START and send the Address and 438*a405764cSShaveta Leekha * the Tx Bytes to the slave. 439*a405764cSShaveta Leekha * "START: Address: Write bytes data[len]" 440*a405764cSShaveta Leekha * IF part supports writing any number of bytes in contrast 441*a405764cSShaveta Leekha * to the else part, which supports writing address offset 442*a405764cSShaveta Leekha * of upto 4 bytes only. 443*a405764cSShaveta Leekha * bytes that need to be written are passed in 444*a405764cSShaveta Leekha * "data", which will eventually keep the data READ, 445*a405764cSShaveta Leekha * after writing the len bytes out of it 446*a405764cSShaveta Leekha */ 447*a405764cSShaveta Leekha if (i2c_write_addr(adap, dev, I2C_WRITE_BIT, 0) != 0) 448*a405764cSShaveta Leekha i = __i2c_write(adap, data, len); 449*a405764cSShaveta Leekha 450*a405764cSShaveta Leekha if (i != len) 451*a405764cSShaveta Leekha return -1; 452*a405764cSShaveta Leekha 453*a405764cSShaveta Leekha if (length && i2c_write_addr(adap, dev, I2C_READ_BIT, 1) != 0) 454*a405764cSShaveta Leekha i = __i2c_read(adap, data, length); 455*a405764cSShaveta Leekha } else { 456*a405764cSShaveta Leekha if ((!length || alen > 0) && 457*a405764cSShaveta Leekha i2c_write_addr(adap, dev, I2C_WRITE_BIT, 0) != 0 && 458*a405764cSShaveta Leekha __i2c_write(adap, &a[4 - alen], alen) == alen) 459080c646dSJean-Christophe PLAGNIOL-VILLARD i = 0; /* No error so far */ 460080c646dSJean-Christophe PLAGNIOL-VILLARD 461b778c1b5SReinhard Pfau if (length && 46200f792e0SHeiko Schocher i2c_write_addr(adap, dev, I2C_READ_BIT, alen ? 1 : 0) != 0) 46300f792e0SHeiko Schocher i = __i2c_read(adap, data, length); 464*a405764cSShaveta Leekha } 465080c646dSJean-Christophe PLAGNIOL-VILLARD 46600f792e0SHeiko Schocher writeb(I2C_CR_MEN, &device->cr); 467080c646dSJean-Christophe PLAGNIOL-VILLARD 46800f792e0SHeiko Schocher if (i2c_wait4bus(adap)) /* Wait until STOP */ 469d1c9e5b3SJoakim Tjernlund debug("i2c_read: wait4bus timed out\n"); 470d1c9e5b3SJoakim Tjernlund 471080c646dSJean-Christophe PLAGNIOL-VILLARD if (i == length) 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 47800f792e0SHeiko Schocher fsl_i2c_write(struct i2c_adapter *adap, u8 dev, uint addr, int alen, 47900f792e0SHeiko Schocher u8 *data, int length) 480080c646dSJean-Christophe PLAGNIOL-VILLARD { 48100f792e0SHeiko Schocher struct fsl_i2c *device = (struct fsl_i2c *)i2c_dev[adap->hwadapnr]; 482080c646dSJean-Christophe PLAGNIOL-VILLARD int i = -1; /* signal error */ 483080c646dSJean-Christophe PLAGNIOL-VILLARD u8 *a = (u8*)&addr; 484080c646dSJean-Christophe PLAGNIOL-VILLARD 485b8ce3343SChunhe Lan if (i2c_wait4bus(adap) < 0) 486b8ce3343SChunhe Lan return -1; 487b8ce3343SChunhe Lan 488b8ce3343SChunhe Lan if (i2c_write_addr(adap, dev, I2C_WRITE_BIT, 0) != 0 && 48900f792e0SHeiko Schocher __i2c_write(adap, &a[4 - alen], alen) == alen) { 49000f792e0SHeiko Schocher i = __i2c_write(adap, data, length); 491080c646dSJean-Christophe PLAGNIOL-VILLARD } 492080c646dSJean-Christophe PLAGNIOL-VILLARD 49300f792e0SHeiko Schocher writeb(I2C_CR_MEN, &device->cr); 49400f792e0SHeiko Schocher if (i2c_wait4bus(adap)) /* Wait until STOP */ 49521f4cbb7SJoakim Tjernlund debug("i2c_write: wait4bus timed out\n"); 496080c646dSJean-Christophe PLAGNIOL-VILLARD 497080c646dSJean-Christophe PLAGNIOL-VILLARD if (i == length) 498080c646dSJean-Christophe PLAGNIOL-VILLARD return 0; 499080c646dSJean-Christophe PLAGNIOL-VILLARD 500080c646dSJean-Christophe PLAGNIOL-VILLARD return -1; 501080c646dSJean-Christophe PLAGNIOL-VILLARD } 502080c646dSJean-Christophe PLAGNIOL-VILLARD 50300f792e0SHeiko Schocher static int 50400f792e0SHeiko Schocher fsl_i2c_probe(struct i2c_adapter *adap, uchar chip) 505080c646dSJean-Christophe PLAGNIOL-VILLARD { 50600f792e0SHeiko Schocher struct fsl_i2c *dev = (struct fsl_i2c *)i2c_dev[adap->hwadapnr]; 507080c646dSJean-Christophe PLAGNIOL-VILLARD /* For unknow reason the controller will ACK when 508080c646dSJean-Christophe PLAGNIOL-VILLARD * probing for a slave with the same address, so skip 509080c646dSJean-Christophe PLAGNIOL-VILLARD * it. 510080c646dSJean-Christophe PLAGNIOL-VILLARD */ 51100f792e0SHeiko Schocher if (chip == (readb(&dev->adr) >> 1)) 512080c646dSJean-Christophe PLAGNIOL-VILLARD return -1; 513080c646dSJean-Christophe PLAGNIOL-VILLARD 51400f792e0SHeiko Schocher return fsl_i2c_read(adap, chip, 0, 0, NULL, 0); 515080c646dSJean-Christophe PLAGNIOL-VILLARD } 516080c646dSJean-Christophe PLAGNIOL-VILLARD 51700f792e0SHeiko Schocher static unsigned int fsl_i2c_set_bus_speed(struct i2c_adapter *adap, 51800f792e0SHeiko Schocher unsigned int speed) 519080c646dSJean-Christophe PLAGNIOL-VILLARD { 52000f792e0SHeiko Schocher struct fsl_i2c *dev = (struct fsl_i2c *)i2c_dev[adap->hwadapnr]; 521c1bce4ffSHeiko Schocher 52200f792e0SHeiko Schocher writeb(0, &dev->cr); /* stop controller */ 52300f792e0SHeiko Schocher set_i2c_bus_speed(dev, get_i2c_clock(adap->hwadapnr), speed); 52400f792e0SHeiko Schocher writeb(I2C_CR_MEN, &dev->cr); /* start controller */ 525d8c82db4STimur Tabi 526d8c82db4STimur Tabi return 0; 527080c646dSJean-Christophe PLAGNIOL-VILLARD } 528080c646dSJean-Christophe PLAGNIOL-VILLARD 52900f792e0SHeiko Schocher /* 53000f792e0SHeiko Schocher * Register fsl i2c adapters 53100f792e0SHeiko Schocher */ 53200f792e0SHeiko Schocher U_BOOT_I2C_ADAP_COMPLETE(fsl_0, fsl_i2c_init, fsl_i2c_probe, fsl_i2c_read, 53300f792e0SHeiko Schocher fsl_i2c_write, fsl_i2c_set_bus_speed, 53400f792e0SHeiko Schocher CONFIG_SYS_FSL_I2C_SPEED, CONFIG_SYS_FSL_I2C_SLAVE, 53500f792e0SHeiko Schocher 0) 53600f792e0SHeiko Schocher #ifdef CONFIG_SYS_FSL_I2C2_OFFSET 53700f792e0SHeiko Schocher U_BOOT_I2C_ADAP_COMPLETE(fsl_1, fsl_i2c_init, fsl_i2c_probe, fsl_i2c_read, 53800f792e0SHeiko Schocher fsl_i2c_write, fsl_i2c_set_bus_speed, 53900f792e0SHeiko Schocher CONFIG_SYS_FSL_I2C2_SPEED, CONFIG_SYS_FSL_I2C2_SLAVE, 54000f792e0SHeiko Schocher 1) 541c1bce4ffSHeiko Schocher #endif 542