xref: /openbmc/u-boot/drivers/i2c/fsl_i2c.c (revision 6dd38cc35245db624443ee596eb78853fa9cca7a)
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
41*6dd38cc3SShaveta Leekha #define CONFIG_I2C_TIMEOUT	100000
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 
49a17fd10fSShengzhou Liu static const struct fsl_i2c *i2c_dev[4] = {
5000f792e0SHeiko Schocher 	(struct fsl_i2c *)(CONFIG_SYS_IMMR + CONFIG_SYS_FSL_I2C_OFFSET),
5100f792e0SHeiko Schocher #ifdef CONFIG_SYS_FSL_I2C2_OFFSET
52a17fd10fSShengzhou Liu 	(struct fsl_i2c *)(CONFIG_SYS_IMMR + CONFIG_SYS_FSL_I2C2_OFFSET),
53a17fd10fSShengzhou Liu #endif
54a17fd10fSShengzhou Liu #ifdef CONFIG_SYS_FSL_I2C3_OFFSET
55a17fd10fSShengzhou Liu 	(struct fsl_i2c *)(CONFIG_SYS_IMMR + CONFIG_SYS_FSL_I2C3_OFFSET),
56a17fd10fSShengzhou Liu #endif
57a17fd10fSShengzhou Liu #ifdef CONFIG_SYS_FSL_I2C4_OFFSET
58a17fd10fSShengzhou Liu 	(struct fsl_i2c *)(CONFIG_SYS_IMMR + CONFIG_SYS_FSL_I2C4_OFFSET)
59080c646dSJean-Christophe PLAGNIOL-VILLARD #endif
60080c646dSJean-Christophe PLAGNIOL-VILLARD };
61080c646dSJean-Christophe PLAGNIOL-VILLARD 
62d8c82db4STimur Tabi /* I2C speed map for a DFSR value of 1 */
63d8c82db4STimur Tabi 
64d8c82db4STimur Tabi /*
65d8c82db4STimur Tabi  * Map I2C frequency dividers to FDR and DFSR values
66d8c82db4STimur Tabi  *
67d8c82db4STimur Tabi  * This structure is used to define the elements of a table that maps I2C
68d8c82db4STimur Tabi  * frequency divider (I2C clock rate divided by I2C bus speed) to a value to be
69d8c82db4STimur Tabi  * programmed into the Frequency Divider Ratio (FDR) and Digital Filter
70d8c82db4STimur Tabi  * Sampling Rate (DFSR) registers.
71d8c82db4STimur Tabi  *
72d8c82db4STimur Tabi  * The actual table should be defined in the board file, and it must be called
73d8c82db4STimur Tabi  * fsl_i2c_speed_map[].
74d8c82db4STimur Tabi  *
75d8c82db4STimur Tabi  * The last entry of the table must have a value of {-1, X}, where X is same
76d8c82db4STimur Tabi  * FDR/DFSR values as the second-to-last entry.  This guarantees that any
77d8c82db4STimur Tabi  * search through the array will always find a match.
78d8c82db4STimur Tabi  *
79d8c82db4STimur Tabi  * The values of the divider must be in increasing numerical order, i.e.
80d8c82db4STimur Tabi  * fsl_i2c_speed_map[x+1].divider > fsl_i2c_speed_map[x].divider.
81d8c82db4STimur Tabi  *
82d8c82db4STimur Tabi  * For this table, the values are based on a value of 1 for the DFSR
83d8c82db4STimur Tabi  * register.  See the application note AN2919 "Determining the I2C Frequency
84d8c82db4STimur Tabi  * Divider Ratio for SCL"
855d9a5efaSTsiChung Liew  *
865d9a5efaSTsiChung Liew  * ColdFire I2C frequency dividers for FDR values are different from
875d9a5efaSTsiChung Liew  * PowerPC. The protocol to use the I2C module is still the same.
885d9a5efaSTsiChung Liew  * A different table is defined and are based on MCF5xxx user manual.
895d9a5efaSTsiChung Liew  *
90d8c82db4STimur Tabi  */
91d8c82db4STimur Tabi static const struct {
92d8c82db4STimur Tabi 	unsigned short divider;
93d8c82db4STimur Tabi 	u8 fdr;
94d8c82db4STimur Tabi } fsl_i2c_speed_map[] = {
9599404202SJoakim Tjernlund #ifdef __M68K__
965d9a5efaSTsiChung Liew 	{20, 32}, {22, 33}, {24, 34}, {26, 35},
975d9a5efaSTsiChung Liew 	{28, 0}, {28, 36}, {30, 1}, {32, 37},
985d9a5efaSTsiChung Liew 	{34, 2}, {36, 38}, {40, 3}, {40, 39},
995d9a5efaSTsiChung Liew 	{44, 4}, {48, 5}, {48, 40}, {56, 6},
1005d9a5efaSTsiChung Liew 	{56, 41}, {64, 42}, {68, 7}, {72, 43},
1015d9a5efaSTsiChung Liew 	{80, 8}, {80, 44}, {88, 9}, {96, 41},
1025d9a5efaSTsiChung Liew 	{104, 10}, {112, 42}, {128, 11}, {128, 43},
1035d9a5efaSTsiChung Liew 	{144, 12}, {160, 13}, {160, 48}, {192, 14},
1045d9a5efaSTsiChung Liew 	{192, 49}, {224, 50}, {240, 15}, {256, 51},
1055d9a5efaSTsiChung Liew 	{288, 16}, {320, 17}, {320, 52}, {384, 18},
1065d9a5efaSTsiChung Liew 	{384, 53}, {448, 54}, {480, 19}, {512, 55},
1075d9a5efaSTsiChung Liew 	{576, 20}, {640, 21}, {640, 56}, {768, 22},
1085d9a5efaSTsiChung Liew 	{768, 57}, {960, 23}, {896, 58}, {1024, 59},
1095d9a5efaSTsiChung Liew 	{1152, 24}, {1280, 25}, {1280, 60}, {1536, 26},
1105d9a5efaSTsiChung Liew 	{1536, 61}, {1792, 62}, {1920, 27}, {2048, 63},
1115d9a5efaSTsiChung Liew 	{2304, 28}, {2560, 29}, {3072, 30}, {3840, 31},
1125d9a5efaSTsiChung Liew 	{-1, 31}
1135d9a5efaSTsiChung Liew #endif
114d8c82db4STimur Tabi };
115d8c82db4STimur Tabi 
116d8c82db4STimur Tabi /**
117d8c82db4STimur Tabi  * Set the I2C bus speed for a given I2C device
118d8c82db4STimur Tabi  *
119d8c82db4STimur Tabi  * @param dev: the I2C device
120d8c82db4STimur Tabi  * @i2c_clk: I2C bus clock frequency
121d8c82db4STimur Tabi  * @speed: the desired speed of the bus
122d8c82db4STimur Tabi  *
123d8c82db4STimur Tabi  * The I2C device must be stopped before calling this function.
124d8c82db4STimur Tabi  *
125d8c82db4STimur Tabi  * The return value is the actual bus speed that is set.
126d8c82db4STimur Tabi  */
127d8c82db4STimur Tabi static unsigned int set_i2c_bus_speed(const struct fsl_i2c *dev,
128d8c82db4STimur Tabi 	unsigned int i2c_clk, unsigned int speed)
129d8c82db4STimur Tabi {
130d8c82db4STimur Tabi 	unsigned short divider = min(i2c_clk / speed, (unsigned short) -1);
131d8c82db4STimur Tabi 
132d8c82db4STimur Tabi 	/*
133d8c82db4STimur Tabi 	 * We want to choose an FDR/DFSR that generates an I2C bus speed that
134d8c82db4STimur Tabi 	 * is equal to or lower than the requested speed.  That means that we
135d8c82db4STimur Tabi 	 * want the first divider that is equal to or greater than the
136d8c82db4STimur Tabi 	 * calculated divider.
137d8c82db4STimur Tabi 	 */
1385d9a5efaSTsiChung Liew #ifdef __PPC__
13999404202SJoakim Tjernlund 	u8 dfsr, fdr = 0x31; /* Default if no FDR found */
14099404202SJoakim Tjernlund 	/* a, b and dfsr matches identifiers A,B and C respectively in AN2919 */
14199404202SJoakim Tjernlund 	unsigned short a, b, ga, gb;
14299404202SJoakim Tjernlund 	unsigned long c_div, est_div;
14399404202SJoakim Tjernlund 
144d01ee4dbSJoakim Tjernlund #ifdef CONFIG_FSL_I2C_CUSTOM_DFSR
145d01ee4dbSJoakim Tjernlund 	dfsr = CONFIG_FSL_I2C_CUSTOM_DFSR;
146d01ee4dbSJoakim Tjernlund #else
14799404202SJoakim Tjernlund 	/* Condition 1: dfsr <= 50/T */
14899404202SJoakim Tjernlund 	dfsr = (5 * (i2c_clk / 1000)) / 100000;
1495d9a5efaSTsiChung Liew #endif
150d01ee4dbSJoakim Tjernlund #ifdef CONFIG_FSL_I2C_CUSTOM_FDR
151d01ee4dbSJoakim Tjernlund 	fdr = CONFIG_FSL_I2C_CUSTOM_FDR;
152d01ee4dbSJoakim Tjernlund 	speed = i2c_clk / divider; /* Fake something */
153d01ee4dbSJoakim Tjernlund #else
15499404202SJoakim Tjernlund 	debug("Requested speed:%d, i2c_clk:%d\n", speed, i2c_clk);
15599404202SJoakim Tjernlund 	if (!dfsr)
15699404202SJoakim Tjernlund 		dfsr = 1;
15799404202SJoakim Tjernlund 
15899404202SJoakim Tjernlund 	est_div = ~0;
15999404202SJoakim Tjernlund 	for (ga = 0x4, a = 10; a <= 30; ga++, a += 2) {
16099404202SJoakim Tjernlund 		for (gb = 0; gb < 8; gb++) {
16199404202SJoakim Tjernlund 			b = 16 << gb;
16299404202SJoakim Tjernlund 			c_div = b * (a + ((3*dfsr)/b)*2);
16399404202SJoakim Tjernlund 			if ((c_div > divider) && (c_div < est_div)) {
16499404202SJoakim Tjernlund 				unsigned short bin_gb, bin_ga;
16599404202SJoakim Tjernlund 
16699404202SJoakim Tjernlund 				est_div = c_div;
16799404202SJoakim Tjernlund 				bin_gb = gb << 2;
16899404202SJoakim Tjernlund 				bin_ga = (ga & 0x3) | ((ga & 0x4) << 3);
16999404202SJoakim Tjernlund 				fdr = bin_gb | bin_ga;
17099404202SJoakim Tjernlund 				speed = i2c_clk / est_div;
17199404202SJoakim Tjernlund 				debug("FDR:0x%.2x, div:%ld, ga:0x%x, gb:0x%x, "
17299404202SJoakim Tjernlund 				      "a:%d, b:%d, speed:%d\n",
17399404202SJoakim Tjernlund 				      fdr, est_div, ga, gb, a, b, speed);
17499404202SJoakim Tjernlund 				/* Condition 2 not accounted for */
17599404202SJoakim Tjernlund 				debug("Tr <= %d ns\n",
17699404202SJoakim Tjernlund 				      (b - 3 * dfsr) * 1000000 /
17799404202SJoakim Tjernlund 				      (i2c_clk / 1000));
17899404202SJoakim Tjernlund 			}
17999404202SJoakim Tjernlund 		}
18099404202SJoakim Tjernlund 		if (a == 20)
18199404202SJoakim Tjernlund 			a += 2;
18299404202SJoakim Tjernlund 		if (a == 24)
18399404202SJoakim Tjernlund 			a += 4;
18499404202SJoakim Tjernlund 	}
18599404202SJoakim Tjernlund 	debug("divider:%d, est_div:%ld, DFSR:%d\n", divider, est_div, dfsr);
18699404202SJoakim Tjernlund 	debug("FDR:0x%.2x, speed:%d\n", fdr, speed);
18799404202SJoakim Tjernlund #endif
18899404202SJoakim Tjernlund 	writeb(dfsr, &dev->dfsrr);	/* set default filter */
18999404202SJoakim Tjernlund 	writeb(fdr, &dev->fdr);		/* set bus speed */
19099404202SJoakim Tjernlund #else
19199404202SJoakim Tjernlund 	unsigned int i;
19299404202SJoakim Tjernlund 
19399404202SJoakim Tjernlund 	for (i = 0; i < ARRAY_SIZE(fsl_i2c_speed_map); i++)
19499404202SJoakim Tjernlund 		if (fsl_i2c_speed_map[i].divider >= divider) {
19599404202SJoakim Tjernlund 			u8 fdr;
19699404202SJoakim Tjernlund 
197d01ee4dbSJoakim Tjernlund 			fdr = fsl_i2c_speed_map[i].fdr;
198d01ee4dbSJoakim Tjernlund 			speed = i2c_clk / fsl_i2c_speed_map[i].divider;
199d01ee4dbSJoakim Tjernlund 			writeb(fdr, &dev->fdr);		/* set bus speed */
200d01ee4dbSJoakim Tjernlund 
2013e3f766aSKumar Gala 			break;
2023e3f766aSKumar Gala 		}
20399404202SJoakim Tjernlund #endif
204d8c82db4STimur Tabi 	return speed;
205d8c82db4STimur Tabi }
206d8c82db4STimur Tabi 
20762f730ffSKim Phillips static unsigned int get_i2c_clock(int bus)
208c9a8b25eSJerry Huang {
209c9a8b25eSJerry Huang 	if (bus)
210609e6ec3SSimon Glass 		return gd->arch.i2c2_clk;	/* I2C2 clock */
211c9a8b25eSJerry Huang 	else
212609e6ec3SSimon Glass 		return gd->arch.i2c1_clk;	/* I2C1 clock */
213c9a8b25eSJerry Huang }
214c9a8b25eSJerry Huang 
215b8ce3343SChunhe Lan static int fsl_i2c_fixup(const struct fsl_i2c *dev)
216b8ce3343SChunhe Lan {
217b8ce3343SChunhe Lan 	const unsigned long long timeout = usec2ticks(CONFIG_I2C_MBB_TIMEOUT);
218b8ce3343SChunhe Lan 	unsigned long long timeval = 0;
219b8ce3343SChunhe Lan 	int ret = -1;
2209c3f77ebSChunhe Lan 	unsigned int flags = 0;
2219c3f77ebSChunhe Lan 
2229c3f77ebSChunhe Lan #ifdef CONFIG_SYS_FSL_ERRATUM_I2C_A004447
2239c3f77ebSChunhe Lan 	unsigned int svr = get_svr();
2249c3f77ebSChunhe Lan 	if ((SVR_SOC_VER(svr) == SVR_8548 && IS_SVR_REV(svr, 3, 1)) ||
2259c3f77ebSChunhe Lan 	    (SVR_REV(svr) <= CONFIG_SYS_FSL_A004447_SVR_REV))
2269c3f77ebSChunhe Lan 		flags = I2C_CR_BIT6;
2279c3f77ebSChunhe Lan #endif
228b8ce3343SChunhe Lan 
229b8ce3343SChunhe Lan 	writeb(I2C_CR_MEN | I2C_CR_MSTA, &dev->cr);
230b8ce3343SChunhe Lan 
231b8ce3343SChunhe Lan 	timeval = get_ticks();
232b8ce3343SChunhe Lan 	while (!(readb(&dev->sr) & I2C_SR_MBB)) {
233b8ce3343SChunhe Lan 		if ((get_ticks() - timeval) > timeout)
234b8ce3343SChunhe Lan 			goto err;
235b8ce3343SChunhe Lan 	}
236b8ce3343SChunhe Lan 
237b8ce3343SChunhe Lan 	if (readb(&dev->sr) & I2C_SR_MAL) {
238b8ce3343SChunhe Lan 		/* SDA is stuck low */
239b8ce3343SChunhe Lan 		writeb(0, &dev->cr);
240b8ce3343SChunhe Lan 		udelay(100);
2419c3f77ebSChunhe Lan 		writeb(I2C_CR_MSTA | flags, &dev->cr);
2429c3f77ebSChunhe Lan 		writeb(I2C_CR_MEN | I2C_CR_MSTA | flags, &dev->cr);
243b8ce3343SChunhe Lan 	}
244b8ce3343SChunhe Lan 
245b8ce3343SChunhe Lan 	readb(&dev->dr);
246b8ce3343SChunhe Lan 
247b8ce3343SChunhe Lan 	timeval = get_ticks();
248b8ce3343SChunhe Lan 	while (!(readb(&dev->sr) & I2C_SR_MIF)) {
249b8ce3343SChunhe Lan 		if ((get_ticks() - timeval) > timeout)
250b8ce3343SChunhe Lan 			goto err;
251b8ce3343SChunhe Lan 	}
252b8ce3343SChunhe Lan 	ret = 0;
253b8ce3343SChunhe Lan 
254b8ce3343SChunhe Lan err:
2559c3f77ebSChunhe Lan 	writeb(I2C_CR_MEN | flags, &dev->cr);
256b8ce3343SChunhe Lan 	writeb(0, &dev->sr);
257b8ce3343SChunhe Lan 	udelay(100);
258b8ce3343SChunhe Lan 
259b8ce3343SChunhe Lan 	return ret;
260b8ce3343SChunhe Lan }
261b8ce3343SChunhe Lan 
26200f792e0SHeiko Schocher static void fsl_i2c_init(struct i2c_adapter *adap, int speed, int slaveadd)
263080c646dSJean-Christophe PLAGNIOL-VILLARD {
264aa551215SKumar Gala 	const struct fsl_i2c *dev;
265b8ce3343SChunhe Lan 	const unsigned long long timeout = usec2ticks(CONFIG_I2C_MBB_TIMEOUT);
266b8ce3343SChunhe Lan 	unsigned long long timeval;
267080c646dSJean-Christophe PLAGNIOL-VILLARD 
26839df00d9SHeiko Schocher #ifdef CONFIG_SYS_I2C_INIT_BOARD
26926a33504SRichard Retanubun 	/* Call board specific i2c bus reset routine before accessing the
27026a33504SRichard Retanubun 	 * environment, which might be in a chip on that bus. For details
27126a33504SRichard Retanubun 	 * about this problem see doc/I2C_Edge_Conditions.
27226a33504SRichard Retanubun 	*/
27339df00d9SHeiko Schocher 	i2c_init_board();
27439df00d9SHeiko Schocher #endif
27500f792e0SHeiko Schocher 	dev = (struct fsl_i2c *)i2c_dev[adap->hwadapnr];
276080c646dSJean-Christophe PLAGNIOL-VILLARD 
277080c646dSJean-Christophe PLAGNIOL-VILLARD 	writeb(0, &dev->cr);		/* stop I2C controller */
278080c646dSJean-Christophe PLAGNIOL-VILLARD 	udelay(5);			/* let it shutdown in peace */
27900f792e0SHeiko Schocher 	set_i2c_bus_speed(dev, get_i2c_clock(adap->hwadapnr), speed);
280080c646dSJean-Christophe PLAGNIOL-VILLARD 	writeb(slaveadd << 1, &dev->adr);/* write slave address */
281080c646dSJean-Christophe PLAGNIOL-VILLARD 	writeb(0x0, &dev->sr);		/* clear status register */
282080c646dSJean-Christophe PLAGNIOL-VILLARD 	writeb(I2C_CR_MEN, &dev->cr);	/* start I2C controller */
28326a33504SRichard Retanubun 
284b8ce3343SChunhe Lan 	timeval = get_ticks();
285b8ce3343SChunhe Lan 	while (readb(&dev->sr) & I2C_SR_MBB) {
286b8ce3343SChunhe Lan 		if ((get_ticks() - timeval) < timeout)
287b8ce3343SChunhe Lan 			continue;
288b8ce3343SChunhe Lan 
289b8ce3343SChunhe Lan 		if (fsl_i2c_fixup(dev))
290b8ce3343SChunhe Lan 			debug("i2c_init: BUS#%d failed to init\n",
291b8ce3343SChunhe Lan 			      adap->hwadapnr);
292b8ce3343SChunhe Lan 
293b8ce3343SChunhe Lan 		break;
294b8ce3343SChunhe Lan 	}
295b8ce3343SChunhe Lan 
29626a33504SRichard Retanubun #ifdef CONFIG_SYS_I2C_BOARD_LATE_INIT
29726a33504SRichard Retanubun 	/* Call board specific i2c bus reset routine AFTER the bus has been
29826a33504SRichard Retanubun 	 * initialized. Use either this callpoint or i2c_init_board;
29926a33504SRichard Retanubun 	 * which is called before i2c_init operations.
30026a33504SRichard Retanubun 	 * For details about this problem see doc/I2C_Edge_Conditions.
30126a33504SRichard Retanubun 	*/
30226a33504SRichard Retanubun 	i2c_board_late_init();
30326a33504SRichard Retanubun #endif
304080c646dSJean-Christophe PLAGNIOL-VILLARD }
305080c646dSJean-Christophe PLAGNIOL-VILLARD 
30621f4cbb7SJoakim Tjernlund static int
30700f792e0SHeiko Schocher i2c_wait4bus(struct i2c_adapter *adap)
308080c646dSJean-Christophe PLAGNIOL-VILLARD {
30900f792e0SHeiko Schocher 	struct fsl_i2c *dev = (struct fsl_i2c *)i2c_dev[adap->hwadapnr];
310f2302d44SStefan Roese 	unsigned long long timeval = get_ticks();
31192477a63STimur Tabi 	const unsigned long long timeout = usec2ticks(CONFIG_I2C_MBB_TIMEOUT);
312080c646dSJean-Christophe PLAGNIOL-VILLARD 
31300f792e0SHeiko Schocher 	while (readb(&dev->sr) & I2C_SR_MBB) {
31492477a63STimur Tabi 		if ((get_ticks() - timeval) > timeout)
315080c646dSJean-Christophe PLAGNIOL-VILLARD 			return -1;
316080c646dSJean-Christophe PLAGNIOL-VILLARD 	}
317080c646dSJean-Christophe PLAGNIOL-VILLARD 
318080c646dSJean-Christophe PLAGNIOL-VILLARD 	return 0;
319080c646dSJean-Christophe PLAGNIOL-VILLARD }
320080c646dSJean-Christophe PLAGNIOL-VILLARD 
321080c646dSJean-Christophe PLAGNIOL-VILLARD static __inline__ int
32200f792e0SHeiko Schocher i2c_wait(struct i2c_adapter *adap, int write)
323080c646dSJean-Christophe PLAGNIOL-VILLARD {
324080c646dSJean-Christophe PLAGNIOL-VILLARD 	u32 csr;
325f2302d44SStefan Roese 	unsigned long long timeval = get_ticks();
32692477a63STimur Tabi 	const unsigned long long timeout = usec2ticks(CONFIG_I2C_TIMEOUT);
32700f792e0SHeiko Schocher 	struct fsl_i2c *dev = (struct fsl_i2c *)i2c_dev[adap->hwadapnr];
328080c646dSJean-Christophe PLAGNIOL-VILLARD 
329080c646dSJean-Christophe PLAGNIOL-VILLARD 	do {
33000f792e0SHeiko Schocher 		csr = readb(&dev->sr);
331080c646dSJean-Christophe PLAGNIOL-VILLARD 		if (!(csr & I2C_SR_MIF))
332080c646dSJean-Christophe PLAGNIOL-VILLARD 			continue;
33321f4cbb7SJoakim Tjernlund 		/* Read again to allow register to stabilise */
33400f792e0SHeiko Schocher 		csr = readb(&dev->sr);
335080c646dSJean-Christophe PLAGNIOL-VILLARD 
33600f792e0SHeiko Schocher 		writeb(0x0, &dev->sr);
337080c646dSJean-Christophe PLAGNIOL-VILLARD 
338080c646dSJean-Christophe PLAGNIOL-VILLARD 		if (csr & I2C_SR_MAL) {
339080c646dSJean-Christophe PLAGNIOL-VILLARD 			debug("i2c_wait: MAL\n");
340080c646dSJean-Christophe PLAGNIOL-VILLARD 			return -1;
341080c646dSJean-Christophe PLAGNIOL-VILLARD 		}
342080c646dSJean-Christophe PLAGNIOL-VILLARD 
343080c646dSJean-Christophe PLAGNIOL-VILLARD 		if (!(csr & I2C_SR_MCF))	{
344080c646dSJean-Christophe PLAGNIOL-VILLARD 			debug("i2c_wait: unfinished\n");
345080c646dSJean-Christophe PLAGNIOL-VILLARD 			return -1;
346080c646dSJean-Christophe PLAGNIOL-VILLARD 		}
347080c646dSJean-Christophe PLAGNIOL-VILLARD 
348080c646dSJean-Christophe PLAGNIOL-VILLARD 		if (write == I2C_WRITE_BIT && (csr & I2C_SR_RXAK)) {
349080c646dSJean-Christophe PLAGNIOL-VILLARD 			debug("i2c_wait: No RXACK\n");
350080c646dSJean-Christophe PLAGNIOL-VILLARD 			return -1;
351080c646dSJean-Christophe PLAGNIOL-VILLARD 		}
352080c646dSJean-Christophe PLAGNIOL-VILLARD 
353080c646dSJean-Christophe PLAGNIOL-VILLARD 		return 0;
35492477a63STimur Tabi 	} while ((get_ticks() - timeval) < timeout);
355080c646dSJean-Christophe PLAGNIOL-VILLARD 
356080c646dSJean-Christophe PLAGNIOL-VILLARD 	debug("i2c_wait: timed out\n");
357080c646dSJean-Christophe PLAGNIOL-VILLARD 	return -1;
358080c646dSJean-Christophe PLAGNIOL-VILLARD }
359080c646dSJean-Christophe PLAGNIOL-VILLARD 
360080c646dSJean-Christophe PLAGNIOL-VILLARD static __inline__ int
36100f792e0SHeiko Schocher i2c_write_addr(struct i2c_adapter *adap, u8 dev, u8 dir, int rsta)
362080c646dSJean-Christophe PLAGNIOL-VILLARD {
36300f792e0SHeiko Schocher 	struct fsl_i2c *device = (struct fsl_i2c *)i2c_dev[adap->hwadapnr];
36400f792e0SHeiko Schocher 
365080c646dSJean-Christophe PLAGNIOL-VILLARD 	writeb(I2C_CR_MEN | I2C_CR_MSTA | I2C_CR_MTX
366080c646dSJean-Christophe PLAGNIOL-VILLARD 	       | (rsta ? I2C_CR_RSTA : 0),
36700f792e0SHeiko Schocher 	       &device->cr);
368080c646dSJean-Christophe PLAGNIOL-VILLARD 
36900f792e0SHeiko Schocher 	writeb((dev << 1) | dir, &device->dr);
370080c646dSJean-Christophe PLAGNIOL-VILLARD 
37100f792e0SHeiko Schocher 	if (i2c_wait(adap, I2C_WRITE_BIT) < 0)
372080c646dSJean-Christophe PLAGNIOL-VILLARD 		return 0;
373080c646dSJean-Christophe PLAGNIOL-VILLARD 
374080c646dSJean-Christophe PLAGNIOL-VILLARD 	return 1;
375080c646dSJean-Christophe PLAGNIOL-VILLARD }
376080c646dSJean-Christophe PLAGNIOL-VILLARD 
377080c646dSJean-Christophe PLAGNIOL-VILLARD static __inline__ int
37800f792e0SHeiko Schocher __i2c_write(struct i2c_adapter *adap, u8 *data, int length)
379080c646dSJean-Christophe PLAGNIOL-VILLARD {
38000f792e0SHeiko Schocher 	struct fsl_i2c *dev = (struct fsl_i2c *)i2c_dev[adap->hwadapnr];
381080c646dSJean-Christophe PLAGNIOL-VILLARD 	int i;
382080c646dSJean-Christophe PLAGNIOL-VILLARD 
383080c646dSJean-Christophe PLAGNIOL-VILLARD 	for (i = 0; i < length; i++) {
38400f792e0SHeiko Schocher 		writeb(data[i], &dev->dr);
385080c646dSJean-Christophe PLAGNIOL-VILLARD 
38600f792e0SHeiko Schocher 		if (i2c_wait(adap, I2C_WRITE_BIT) < 0)
387080c646dSJean-Christophe PLAGNIOL-VILLARD 			break;
388080c646dSJean-Christophe PLAGNIOL-VILLARD 	}
389080c646dSJean-Christophe PLAGNIOL-VILLARD 
390080c646dSJean-Christophe PLAGNIOL-VILLARD 	return i;
391080c646dSJean-Christophe PLAGNIOL-VILLARD }
392080c646dSJean-Christophe PLAGNIOL-VILLARD 
393080c646dSJean-Christophe PLAGNIOL-VILLARD static __inline__ int
39400f792e0SHeiko Schocher __i2c_read(struct i2c_adapter *adap, u8 *data, int length)
395080c646dSJean-Christophe PLAGNIOL-VILLARD {
39600f792e0SHeiko Schocher 	struct fsl_i2c *dev = (struct fsl_i2c *)i2c_dev[adap->hwadapnr];
397080c646dSJean-Christophe PLAGNIOL-VILLARD 	int i;
398080c646dSJean-Christophe PLAGNIOL-VILLARD 
399080c646dSJean-Christophe PLAGNIOL-VILLARD 	writeb(I2C_CR_MEN | I2C_CR_MSTA | ((length == 1) ? I2C_CR_TXAK : 0),
40000f792e0SHeiko Schocher 	       &dev->cr);
401080c646dSJean-Christophe PLAGNIOL-VILLARD 
402080c646dSJean-Christophe PLAGNIOL-VILLARD 	/* dummy read */
40300f792e0SHeiko Schocher 	readb(&dev->dr);
404080c646dSJean-Christophe PLAGNIOL-VILLARD 
405080c646dSJean-Christophe PLAGNIOL-VILLARD 	for (i = 0; i < length; i++) {
40600f792e0SHeiko Schocher 		if (i2c_wait(adap, I2C_READ_BIT) < 0)
407080c646dSJean-Christophe PLAGNIOL-VILLARD 			break;
408080c646dSJean-Christophe PLAGNIOL-VILLARD 
409080c646dSJean-Christophe PLAGNIOL-VILLARD 		/* Generate ack on last next to last byte */
410080c646dSJean-Christophe PLAGNIOL-VILLARD 		if (i == length - 2)
411080c646dSJean-Christophe PLAGNIOL-VILLARD 			writeb(I2C_CR_MEN | I2C_CR_MSTA | I2C_CR_TXAK,
41200f792e0SHeiko Schocher 			       &dev->cr);
413080c646dSJean-Christophe PLAGNIOL-VILLARD 
414d1c9e5b3SJoakim Tjernlund 		/* Do not generate stop on last byte */
415080c646dSJean-Christophe PLAGNIOL-VILLARD 		if (i == length - 1)
416d1c9e5b3SJoakim Tjernlund 			writeb(I2C_CR_MEN | I2C_CR_MSTA | I2C_CR_MTX,
41700f792e0SHeiko Schocher 			       &dev->cr);
418080c646dSJean-Christophe PLAGNIOL-VILLARD 
41900f792e0SHeiko Schocher 		data[i] = readb(&dev->dr);
420080c646dSJean-Christophe PLAGNIOL-VILLARD 	}
421080c646dSJean-Christophe PLAGNIOL-VILLARD 
422080c646dSJean-Christophe PLAGNIOL-VILLARD 	return i;
423080c646dSJean-Christophe PLAGNIOL-VILLARD }
424080c646dSJean-Christophe PLAGNIOL-VILLARD 
42500f792e0SHeiko Schocher static int
42600f792e0SHeiko Schocher fsl_i2c_read(struct i2c_adapter *adap, u8 dev, uint addr, int alen, u8 *data,
42700f792e0SHeiko Schocher 	     int length)
428080c646dSJean-Christophe PLAGNIOL-VILLARD {
42900f792e0SHeiko Schocher 	struct fsl_i2c *device = (struct fsl_i2c *)i2c_dev[adap->hwadapnr];
430080c646dSJean-Christophe PLAGNIOL-VILLARD 	int i = -1; /* signal error */
431080c646dSJean-Christophe PLAGNIOL-VILLARD 	u8 *a = (u8*)&addr;
432a405764cSShaveta Leekha 	int len = alen * -1;
433080c646dSJean-Christophe PLAGNIOL-VILLARD 
43400f792e0SHeiko Schocher 	if (i2c_wait4bus(adap) < 0)
435b778c1b5SReinhard Pfau 		return -1;
436b778c1b5SReinhard Pfau 
437a405764cSShaveta Leekha 	/* To handle the need of I2C devices that require to write few bytes
438a405764cSShaveta Leekha 	 * (more than 4 bytes of address as in the case of else part)
439a405764cSShaveta Leekha 	 * of data before reading, Negative equivalent of length(bytes to write)
440a405764cSShaveta Leekha 	 * is passed, but used the +ve part of len for writing data
441a405764cSShaveta Leekha 	 */
442a405764cSShaveta Leekha 	if (alen < 0) {
443a405764cSShaveta Leekha 		/* Generate a START and send the Address and
444a405764cSShaveta Leekha 		 * the Tx Bytes to the slave.
445a405764cSShaveta Leekha 		 * "START: Address: Write bytes data[len]"
446a405764cSShaveta Leekha 		 * IF part supports writing any number of bytes in contrast
447a405764cSShaveta Leekha 		 * to the else part, which supports writing address offset
448a405764cSShaveta Leekha 		 * of upto 4 bytes only.
449a405764cSShaveta Leekha 		 * bytes that need to be written are passed in
450a405764cSShaveta Leekha 		 * "data", which will eventually keep the data READ,
451a405764cSShaveta Leekha 		 * after writing the len bytes out of it
452a405764cSShaveta Leekha 		 */
453a405764cSShaveta Leekha 		if (i2c_write_addr(adap, dev, I2C_WRITE_BIT, 0) != 0)
454a405764cSShaveta Leekha 			i = __i2c_write(adap, data, len);
455a405764cSShaveta Leekha 
456a405764cSShaveta Leekha 		if (i != len)
457a405764cSShaveta Leekha 			return -1;
458a405764cSShaveta Leekha 
459a405764cSShaveta Leekha 		if (length && i2c_write_addr(adap, dev, I2C_READ_BIT, 1) != 0)
460a405764cSShaveta Leekha 			i = __i2c_read(adap, data, length);
461a405764cSShaveta Leekha 	} else {
462a405764cSShaveta Leekha 		if ((!length || alen > 0) &&
463a405764cSShaveta Leekha 		    i2c_write_addr(adap, dev, I2C_WRITE_BIT, 0) != 0  &&
464a405764cSShaveta Leekha 		    __i2c_write(adap, &a[4 - alen], alen) == alen)
465080c646dSJean-Christophe PLAGNIOL-VILLARD 			i = 0; /* No error so far */
466080c646dSJean-Christophe PLAGNIOL-VILLARD 
467b778c1b5SReinhard Pfau 		if (length &&
46800f792e0SHeiko Schocher 		    i2c_write_addr(adap, dev, I2C_READ_BIT, alen ? 1 : 0) != 0)
46900f792e0SHeiko Schocher 			i = __i2c_read(adap, data, length);
470a405764cSShaveta Leekha 	}
471080c646dSJean-Christophe PLAGNIOL-VILLARD 
47200f792e0SHeiko Schocher 	writeb(I2C_CR_MEN, &device->cr);
473080c646dSJean-Christophe PLAGNIOL-VILLARD 
47400f792e0SHeiko Schocher 	if (i2c_wait4bus(adap)) /* Wait until STOP */
475d1c9e5b3SJoakim Tjernlund 		debug("i2c_read: wait4bus timed out\n");
476d1c9e5b3SJoakim Tjernlund 
477080c646dSJean-Christophe PLAGNIOL-VILLARD 	if (i == length)
478080c646dSJean-Christophe PLAGNIOL-VILLARD 	    return 0;
479080c646dSJean-Christophe PLAGNIOL-VILLARD 
480080c646dSJean-Christophe PLAGNIOL-VILLARD 	return -1;
481080c646dSJean-Christophe PLAGNIOL-VILLARD }
482080c646dSJean-Christophe PLAGNIOL-VILLARD 
48300f792e0SHeiko Schocher static int
48400f792e0SHeiko Schocher fsl_i2c_write(struct i2c_adapter *adap, u8 dev, uint addr, int alen,
48500f792e0SHeiko Schocher 	      u8 *data, int length)
486080c646dSJean-Christophe PLAGNIOL-VILLARD {
48700f792e0SHeiko Schocher 	struct fsl_i2c *device = (struct fsl_i2c *)i2c_dev[adap->hwadapnr];
488080c646dSJean-Christophe PLAGNIOL-VILLARD 	int i = -1; /* signal error */
489080c646dSJean-Christophe PLAGNIOL-VILLARD 	u8 *a = (u8*)&addr;
490080c646dSJean-Christophe PLAGNIOL-VILLARD 
491b8ce3343SChunhe Lan 	if (i2c_wait4bus(adap) < 0)
492b8ce3343SChunhe Lan 		return -1;
493b8ce3343SChunhe Lan 
494b8ce3343SChunhe Lan 	if (i2c_write_addr(adap, dev, I2C_WRITE_BIT, 0) != 0 &&
49500f792e0SHeiko Schocher 	    __i2c_write(adap, &a[4 - alen], alen) == alen) {
49600f792e0SHeiko Schocher 		i = __i2c_write(adap, data, length);
497080c646dSJean-Christophe PLAGNIOL-VILLARD 	}
498080c646dSJean-Christophe PLAGNIOL-VILLARD 
49900f792e0SHeiko Schocher 	writeb(I2C_CR_MEN, &device->cr);
50000f792e0SHeiko Schocher 	if (i2c_wait4bus(adap)) /* Wait until STOP */
50121f4cbb7SJoakim Tjernlund 		debug("i2c_write: wait4bus timed out\n");
502080c646dSJean-Christophe PLAGNIOL-VILLARD 
503080c646dSJean-Christophe PLAGNIOL-VILLARD 	if (i == length)
504080c646dSJean-Christophe PLAGNIOL-VILLARD 	    return 0;
505080c646dSJean-Christophe PLAGNIOL-VILLARD 
506080c646dSJean-Christophe PLAGNIOL-VILLARD 	return -1;
507080c646dSJean-Christophe PLAGNIOL-VILLARD }
508080c646dSJean-Christophe PLAGNIOL-VILLARD 
50900f792e0SHeiko Schocher static int
51000f792e0SHeiko Schocher fsl_i2c_probe(struct i2c_adapter *adap, uchar chip)
511080c646dSJean-Christophe PLAGNIOL-VILLARD {
51200f792e0SHeiko Schocher 	struct fsl_i2c *dev = (struct fsl_i2c *)i2c_dev[adap->hwadapnr];
513080c646dSJean-Christophe PLAGNIOL-VILLARD 	/* For unknow reason the controller will ACK when
514080c646dSJean-Christophe PLAGNIOL-VILLARD 	 * probing for a slave with the same address, so skip
515080c646dSJean-Christophe PLAGNIOL-VILLARD 	 * it.
516080c646dSJean-Christophe PLAGNIOL-VILLARD 	 */
51700f792e0SHeiko Schocher 	if (chip == (readb(&dev->adr) >> 1))
518080c646dSJean-Christophe PLAGNIOL-VILLARD 		return -1;
519080c646dSJean-Christophe PLAGNIOL-VILLARD 
52000f792e0SHeiko Schocher 	return fsl_i2c_read(adap, chip, 0, 0, NULL, 0);
521080c646dSJean-Christophe PLAGNIOL-VILLARD }
522080c646dSJean-Christophe PLAGNIOL-VILLARD 
52300f792e0SHeiko Schocher static unsigned int fsl_i2c_set_bus_speed(struct i2c_adapter *adap,
52400f792e0SHeiko Schocher 			unsigned int speed)
525080c646dSJean-Christophe PLAGNIOL-VILLARD {
52600f792e0SHeiko Schocher 	struct fsl_i2c *dev = (struct fsl_i2c *)i2c_dev[adap->hwadapnr];
527c1bce4ffSHeiko Schocher 
52800f792e0SHeiko Schocher 	writeb(0, &dev->cr);		/* stop controller */
52900f792e0SHeiko Schocher 	set_i2c_bus_speed(dev, get_i2c_clock(adap->hwadapnr), speed);
53000f792e0SHeiko Schocher 	writeb(I2C_CR_MEN, &dev->cr);	/* start controller */
531d8c82db4STimur Tabi 
532d8c82db4STimur Tabi 	return 0;
533080c646dSJean-Christophe PLAGNIOL-VILLARD }
534080c646dSJean-Christophe PLAGNIOL-VILLARD 
53500f792e0SHeiko Schocher /*
53600f792e0SHeiko Schocher  * Register fsl i2c adapters
53700f792e0SHeiko Schocher  */
53800f792e0SHeiko Schocher U_BOOT_I2C_ADAP_COMPLETE(fsl_0, fsl_i2c_init, fsl_i2c_probe, fsl_i2c_read,
53900f792e0SHeiko Schocher 			 fsl_i2c_write, fsl_i2c_set_bus_speed,
54000f792e0SHeiko Schocher 			 CONFIG_SYS_FSL_I2C_SPEED, CONFIG_SYS_FSL_I2C_SLAVE,
54100f792e0SHeiko Schocher 			 0)
54200f792e0SHeiko Schocher #ifdef CONFIG_SYS_FSL_I2C2_OFFSET
54300f792e0SHeiko Schocher U_BOOT_I2C_ADAP_COMPLETE(fsl_1, fsl_i2c_init, fsl_i2c_probe, fsl_i2c_read,
54400f792e0SHeiko Schocher 			 fsl_i2c_write, fsl_i2c_set_bus_speed,
54500f792e0SHeiko Schocher 			 CONFIG_SYS_FSL_I2C2_SPEED, CONFIG_SYS_FSL_I2C2_SLAVE,
54600f792e0SHeiko Schocher 			 1)
547c1bce4ffSHeiko Schocher #endif
548a17fd10fSShengzhou Liu #ifdef CONFIG_SYS_FSL_I2C3_OFFSET
549a17fd10fSShengzhou Liu U_BOOT_I2C_ADAP_COMPLETE(fsl_2, fsl_i2c_init, fsl_i2c_probe, fsl_i2c_read,
550a17fd10fSShengzhou Liu 			 fsl_i2c_write, fsl_i2c_set_bus_speed,
551a17fd10fSShengzhou Liu 			 CONFIG_SYS_FSL_I2C3_SPEED, CONFIG_SYS_FSL_I2C3_SLAVE,
552a17fd10fSShengzhou Liu 			 2)
553a17fd10fSShengzhou Liu #endif
554a17fd10fSShengzhou Liu #ifdef CONFIG_SYS_FSL_I2C4_OFFSET
555a17fd10fSShengzhou Liu U_BOOT_I2C_ADAP_COMPLETE(fsl_3, fsl_i2c_init, fsl_i2c_probe, fsl_i2c_read,
556a17fd10fSShengzhou Liu 			 fsl_i2c_write, fsl_i2c_set_bus_speed,
557a17fd10fSShengzhou Liu 			 CONFIG_SYS_FSL_I2C4_SPEED, CONFIG_SYS_FSL_I2C4_SLAVE,
558a17fd10fSShengzhou Liu 			 3)
559a17fd10fSShengzhou Liu #endif
560