xref: /openbmc/u-boot/drivers/i2c/fsl_i2c.c (revision e5c762f5a7dbeaa21870720e8daf656153e1aef9)
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 */
15*e5c762f5SMario Six #include <clk.h>
16dbc82ce3Smario.six@gdsys.cc #include <dm.h>
17dbc82ce3Smario.six@gdsys.cc #include <mapmem.h>
18080c646dSJean-Christophe PLAGNIOL-VILLARD 
1992477a63STimur Tabi /* The maximum number of microseconds we will wait until another master has
2092477a63STimur Tabi  * released the bus.  If not defined in the board header file, then use a
2192477a63STimur Tabi  * generic value.
2292477a63STimur Tabi  */
2392477a63STimur Tabi #ifndef CONFIG_I2C_MBB_TIMEOUT
2492477a63STimur Tabi #define CONFIG_I2C_MBB_TIMEOUT	100000
2592477a63STimur Tabi #endif
2692477a63STimur Tabi 
2792477a63STimur Tabi /* The maximum number of microseconds we will wait for a read or write
2892477a63STimur Tabi  * operation to complete.  If not defined in the board header file, then use a
2992477a63STimur Tabi  * generic value.
3092477a63STimur Tabi  */
3192477a63STimur Tabi #ifndef CONFIG_I2C_TIMEOUT
326dd38cc3SShaveta Leekha #define CONFIG_I2C_TIMEOUT	100000
3392477a63STimur Tabi #endif
34080c646dSJean-Christophe PLAGNIOL-VILLARD 
35080c646dSJean-Christophe PLAGNIOL-VILLARD #define I2C_READ_BIT  1
36080c646dSJean-Christophe PLAGNIOL-VILLARD #define I2C_WRITE_BIT 0
37080c646dSJean-Christophe PLAGNIOL-VILLARD 
38d8c82db4STimur Tabi DECLARE_GLOBAL_DATA_PTR;
39d8c82db4STimur Tabi 
40dbc82ce3Smario.six@gdsys.cc #ifndef CONFIG_DM_I2C
41ec2c81c5Smario.six@gdsys.cc static const struct fsl_i2c_base *i2c_base[4] = {
42ec2c81c5Smario.six@gdsys.cc 	(struct fsl_i2c_base *)(CONFIG_SYS_IMMR + CONFIG_SYS_FSL_I2C_OFFSET),
4300f792e0SHeiko Schocher #ifdef CONFIG_SYS_FSL_I2C2_OFFSET
44ec2c81c5Smario.six@gdsys.cc 	(struct fsl_i2c_base *)(CONFIG_SYS_IMMR + CONFIG_SYS_FSL_I2C2_OFFSET),
45a17fd10fSShengzhou Liu #endif
46a17fd10fSShengzhou Liu #ifdef CONFIG_SYS_FSL_I2C3_OFFSET
47ec2c81c5Smario.six@gdsys.cc 	(struct fsl_i2c_base *)(CONFIG_SYS_IMMR + CONFIG_SYS_FSL_I2C3_OFFSET),
48a17fd10fSShengzhou Liu #endif
49a17fd10fSShengzhou Liu #ifdef CONFIG_SYS_FSL_I2C4_OFFSET
50ec2c81c5Smario.six@gdsys.cc 	(struct fsl_i2c_base *)(CONFIG_SYS_IMMR + CONFIG_SYS_FSL_I2C4_OFFSET)
51080c646dSJean-Christophe PLAGNIOL-VILLARD #endif
52080c646dSJean-Christophe PLAGNIOL-VILLARD };
53dbc82ce3Smario.six@gdsys.cc #endif
54080c646dSJean-Christophe PLAGNIOL-VILLARD 
55d8c82db4STimur Tabi /* I2C speed map for a DFSR value of 1 */
56d8c82db4STimur Tabi 
57645cb46eSTom Rini #ifdef __M68K__
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[] = {
895d9a5efaSTsiChung Liew 	{20, 32}, {22, 33}, {24, 34}, {26, 35},
905d9a5efaSTsiChung Liew 	{28, 0}, {28, 36}, {30, 1}, {32, 37},
915d9a5efaSTsiChung Liew 	{34, 2}, {36, 38}, {40, 3}, {40, 39},
925d9a5efaSTsiChung Liew 	{44, 4}, {48, 5}, {48, 40}, {56, 6},
935d9a5efaSTsiChung Liew 	{56, 41}, {64, 42}, {68, 7}, {72, 43},
945d9a5efaSTsiChung Liew 	{80, 8}, {80, 44}, {88, 9}, {96, 41},
955d9a5efaSTsiChung Liew 	{104, 10}, {112, 42}, {128, 11}, {128, 43},
965d9a5efaSTsiChung Liew 	{144, 12}, {160, 13}, {160, 48}, {192, 14},
975d9a5efaSTsiChung Liew 	{192, 49}, {224, 50}, {240, 15}, {256, 51},
985d9a5efaSTsiChung Liew 	{288, 16}, {320, 17}, {320, 52}, {384, 18},
995d9a5efaSTsiChung Liew 	{384, 53}, {448, 54}, {480, 19}, {512, 55},
1005d9a5efaSTsiChung Liew 	{576, 20}, {640, 21}, {640, 56}, {768, 22},
1015d9a5efaSTsiChung Liew 	{768, 57}, {960, 23}, {896, 58}, {1024, 59},
1025d9a5efaSTsiChung Liew 	{1152, 24}, {1280, 25}, {1280, 60}, {1536, 26},
1035d9a5efaSTsiChung Liew 	{1536, 61}, {1792, 62}, {1920, 27}, {2048, 63},
1045d9a5efaSTsiChung Liew 	{2304, 28}, {2560, 29}, {3072, 30}, {3840, 31},
1055d9a5efaSTsiChung Liew 	{-1, 31}
106d8c82db4STimur Tabi };
107645cb46eSTom Rini #endif
108d8c82db4STimur Tabi 
109d8c82db4STimur Tabi /**
110d8c82db4STimur Tabi  * Set the I2C bus speed for a given I2C device
111d8c82db4STimur Tabi  *
112ec2c81c5Smario.six@gdsys.cc  * @param base: the I2C device registers
113d8c82db4STimur Tabi  * @i2c_clk: I2C bus clock frequency
114d8c82db4STimur Tabi  * @speed: the desired speed of the bus
115d8c82db4STimur Tabi  *
116d8c82db4STimur Tabi  * The I2C device must be stopped before calling this function.
117d8c82db4STimur Tabi  *
118d8c82db4STimur Tabi  * The return value is the actual bus speed that is set.
119d8c82db4STimur Tabi  */
120a059de11SMario Six static uint set_i2c_bus_speed(const struct fsl_i2c_base *base,
121a059de11SMario Six 			      uint i2c_clk, uint speed)
122d8c82db4STimur Tabi {
123a059de11SMario Six 	ushort divider = min(i2c_clk / speed, (uint)USHRT_MAX);
124d8c82db4STimur Tabi 
125d8c82db4STimur Tabi 	/*
126d8c82db4STimur Tabi 	 * We want to choose an FDR/DFSR that generates an I2C bus speed that
127d8c82db4STimur Tabi 	 * is equal to or lower than the requested speed.  That means that we
128d8c82db4STimur Tabi 	 * want the first divider that is equal to or greater than the
129d8c82db4STimur Tabi 	 * calculated divider.
130d8c82db4STimur Tabi 	 */
1315d9a5efaSTsiChung Liew #ifdef __PPC__
13299404202SJoakim Tjernlund 	u8 dfsr, fdr = 0x31; /* Default if no FDR found */
13399404202SJoakim Tjernlund 	/* a, b and dfsr matches identifiers A,B and C respectively in AN2919 */
134a059de11SMario Six 	ushort a, b, ga, gb;
135a059de11SMario Six 	ulong c_div, est_div;
13699404202SJoakim Tjernlund 
137d01ee4dbSJoakim Tjernlund #ifdef CONFIG_FSL_I2C_CUSTOM_DFSR
138d01ee4dbSJoakim Tjernlund 	dfsr = CONFIG_FSL_I2C_CUSTOM_DFSR;
139d01ee4dbSJoakim Tjernlund #else
14099404202SJoakim Tjernlund 	/* Condition 1: dfsr <= 50/T */
14199404202SJoakim Tjernlund 	dfsr = (5 * (i2c_clk / 1000)) / 100000;
1425d9a5efaSTsiChung Liew #endif
143d01ee4dbSJoakim Tjernlund #ifdef CONFIG_FSL_I2C_CUSTOM_FDR
144d01ee4dbSJoakim Tjernlund 	fdr = CONFIG_FSL_I2C_CUSTOM_FDR;
145d01ee4dbSJoakim Tjernlund 	speed = i2c_clk / divider; /* Fake something */
146d01ee4dbSJoakim Tjernlund #else
14799404202SJoakim Tjernlund 	debug("Requested speed:%d, i2c_clk:%d\n", speed, i2c_clk);
14899404202SJoakim Tjernlund 	if (!dfsr)
14999404202SJoakim Tjernlund 		dfsr = 1;
15099404202SJoakim Tjernlund 
15199404202SJoakim Tjernlund 	est_div = ~0;
15299404202SJoakim Tjernlund 	for (ga = 0x4, a = 10; a <= 30; ga++, a += 2) {
15399404202SJoakim Tjernlund 		for (gb = 0; gb < 8; gb++) {
15499404202SJoakim Tjernlund 			b = 16 << gb;
15599404202SJoakim Tjernlund 			c_div = b * (a + ((3 * dfsr) / b) * 2);
156a059de11SMario Six 			if (c_div > divider && c_div < est_div) {
157a059de11SMario Six 				ushort bin_gb, bin_ga;
15899404202SJoakim Tjernlund 
15999404202SJoakim Tjernlund 				est_div = c_div;
16099404202SJoakim Tjernlund 				bin_gb = gb << 2;
16199404202SJoakim Tjernlund 				bin_ga = (ga & 0x3) | ((ga & 0x4) << 3);
16299404202SJoakim Tjernlund 				fdr = bin_gb | bin_ga;
16399404202SJoakim Tjernlund 				speed = i2c_clk / est_div;
164a059de11SMario Six 
165a059de11SMario Six 				debug("FDR: 0x%.2x, ", fdr);
166a059de11SMario Six 				debug("div: %ld, ", est_div);
167a059de11SMario Six 				debug("ga: 0x%x, gb: 0x%x, ", ga, gb);
168a059de11SMario Six 				debug("a: %d, b: %d, speed: %d\n", a, b, speed);
169a059de11SMario Six 
17099404202SJoakim Tjernlund 				/* Condition 2 not accounted for */
17199404202SJoakim Tjernlund 				debug("Tr <= %d ns\n",
17299404202SJoakim Tjernlund 				      (b - 3 * dfsr) * 1000000 /
17399404202SJoakim Tjernlund 				      (i2c_clk / 1000));
17499404202SJoakim Tjernlund 			}
17599404202SJoakim Tjernlund 		}
17699404202SJoakim Tjernlund 		if (a == 20)
17799404202SJoakim Tjernlund 			a += 2;
17899404202SJoakim Tjernlund 		if (a == 24)
17999404202SJoakim Tjernlund 			a += 4;
18099404202SJoakim Tjernlund 	}
18199404202SJoakim Tjernlund 	debug("divider: %d, est_div: %ld, DFSR: %d\n", divider, est_div, dfsr);
18299404202SJoakim Tjernlund 	debug("FDR: 0x%.2x, speed: %d\n", fdr, speed);
18399404202SJoakim Tjernlund #endif
184ec2c81c5Smario.six@gdsys.cc 	writeb(dfsr, &base->dfsrr);	/* set default filter */
185ec2c81c5Smario.six@gdsys.cc 	writeb(fdr, &base->fdr);	/* set bus speed */
18699404202SJoakim Tjernlund #else
187a059de11SMario Six 	uint i;
18899404202SJoakim Tjernlund 
18999404202SJoakim Tjernlund 	for (i = 0; i < ARRAY_SIZE(fsl_i2c_speed_map); i++)
19099404202SJoakim Tjernlund 		if (fsl_i2c_speed_map[i].divider >= divider) {
19199404202SJoakim Tjernlund 			u8 fdr;
19299404202SJoakim Tjernlund 
193d01ee4dbSJoakim Tjernlund 			fdr = fsl_i2c_speed_map[i].fdr;
194d01ee4dbSJoakim Tjernlund 			speed = i2c_clk / fsl_i2c_speed_map[i].divider;
195ec2c81c5Smario.six@gdsys.cc 			writeb(fdr, &base->fdr);	/* set bus speed */
196d01ee4dbSJoakim Tjernlund 
1973e3f766aSKumar Gala 			break;
1983e3f766aSKumar Gala 		}
19999404202SJoakim Tjernlund #endif
200d8c82db4STimur Tabi 	return speed;
201d8c82db4STimur Tabi }
202d8c82db4STimur Tabi 
203dbc82ce3Smario.six@gdsys.cc #ifndef CONFIG_DM_I2C
204a059de11SMario Six static uint get_i2c_clock(int bus)
205c9a8b25eSJerry Huang {
206c9a8b25eSJerry Huang 	if (bus)
207609e6ec3SSimon Glass 		return gd->arch.i2c2_clk;	/* I2C2 clock */
208c9a8b25eSJerry Huang 	else
209609e6ec3SSimon Glass 		return gd->arch.i2c1_clk;	/* I2C1 clock */
210c9a8b25eSJerry Huang }
211dbc82ce3Smario.six@gdsys.cc #endif
212c9a8b25eSJerry Huang 
213ec2c81c5Smario.six@gdsys.cc static int fsl_i2c_fixup(const struct fsl_i2c_base *base)
214b8ce3343SChunhe Lan {
215b8ce3343SChunhe Lan 	const unsigned long long timeout = usec2ticks(CONFIG_I2C_MBB_TIMEOUT);
216b8ce3343SChunhe Lan 	unsigned long long timeval = 0;
217b8ce3343SChunhe Lan 	int ret = -1;
218a059de11SMario Six 	uint flags = 0;
2199c3f77ebSChunhe Lan 
2209c3f77ebSChunhe Lan #ifdef CONFIG_SYS_FSL_ERRATUM_I2C_A004447
221a059de11SMario Six 	uint svr = get_svr();
222a059de11SMario Six 
2239c3f77ebSChunhe Lan 	if ((SVR_SOC_VER(svr) == SVR_8548 && IS_SVR_REV(svr, 3, 1)) ||
2249c3f77ebSChunhe Lan 	    (SVR_REV(svr) <= CONFIG_SYS_FSL_A004447_SVR_REV))
2259c3f77ebSChunhe Lan 		flags = I2C_CR_BIT6;
2269c3f77ebSChunhe Lan #endif
227b8ce3343SChunhe Lan 
228ec2c81c5Smario.six@gdsys.cc 	writeb(I2C_CR_MEN | I2C_CR_MSTA, &base->cr);
229b8ce3343SChunhe Lan 
230b8ce3343SChunhe Lan 	timeval = get_ticks();
231ec2c81c5Smario.six@gdsys.cc 	while (!(readb(&base->sr) & I2C_SR_MBB)) {
232b8ce3343SChunhe Lan 		if ((get_ticks() - timeval) > timeout)
233b8ce3343SChunhe Lan 			goto err;
234b8ce3343SChunhe Lan 	}
235b8ce3343SChunhe Lan 
236ec2c81c5Smario.six@gdsys.cc 	if (readb(&base->sr) & I2C_SR_MAL) {
237b8ce3343SChunhe Lan 		/* SDA is stuck low */
238ec2c81c5Smario.six@gdsys.cc 		writeb(0, &base->cr);
239b8ce3343SChunhe Lan 		udelay(100);
240ec2c81c5Smario.six@gdsys.cc 		writeb(I2C_CR_MSTA | flags, &base->cr);
241ec2c81c5Smario.six@gdsys.cc 		writeb(I2C_CR_MEN | I2C_CR_MSTA | flags, &base->cr);
242b8ce3343SChunhe Lan 	}
243b8ce3343SChunhe Lan 
244ec2c81c5Smario.six@gdsys.cc 	readb(&base->dr);
245b8ce3343SChunhe Lan 
246b8ce3343SChunhe Lan 	timeval = get_ticks();
247ec2c81c5Smario.six@gdsys.cc 	while (!(readb(&base->sr) & I2C_SR_MIF)) {
248b8ce3343SChunhe Lan 		if ((get_ticks() - timeval) > timeout)
249b8ce3343SChunhe Lan 			goto err;
250b8ce3343SChunhe Lan 	}
251b8ce3343SChunhe Lan 	ret = 0;
252b8ce3343SChunhe Lan 
253b8ce3343SChunhe Lan err:
254ec2c81c5Smario.six@gdsys.cc 	writeb(I2C_CR_MEN | flags, &base->cr);
255ec2c81c5Smario.six@gdsys.cc 	writeb(0, &base->sr);
256b8ce3343SChunhe Lan 	udelay(100);
257b8ce3343SChunhe Lan 
258b8ce3343SChunhe Lan 	return ret;
259b8ce3343SChunhe Lan }
260b8ce3343SChunhe Lan 
261ecf591e3Smario.six@gdsys.cc static void __i2c_init(const struct fsl_i2c_base *base, int speed, int
262ecf591e3Smario.six@gdsys.cc 		       slaveadd, int i2c_clk, int busnum)
263080c646dSJean-Christophe PLAGNIOL-VILLARD {
264b8ce3343SChunhe Lan 	const unsigned long long timeout = usec2ticks(CONFIG_I2C_MBB_TIMEOUT);
265b8ce3343SChunhe Lan 	unsigned long long timeval;
266080c646dSJean-Christophe PLAGNIOL-VILLARD 
26739df00d9SHeiko Schocher #ifdef CONFIG_SYS_I2C_INIT_BOARD
26826a33504SRichard Retanubun 	/* Call board specific i2c bus reset routine before accessing the
26926a33504SRichard Retanubun 	 * environment, which might be in a chip on that bus. For details
27026a33504SRichard Retanubun 	 * about this problem see doc/I2C_Edge_Conditions.
27126a33504SRichard Retanubun 	 */
27239df00d9SHeiko Schocher 	i2c_init_board();
27339df00d9SHeiko Schocher #endif
274ec2c81c5Smario.six@gdsys.cc 	writeb(0, &base->cr);		/* stop I2C controller */
275080c646dSJean-Christophe PLAGNIOL-VILLARD 	udelay(5);			/* let it shutdown in peace */
276ecf591e3Smario.six@gdsys.cc 	set_i2c_bus_speed(base, i2c_clk, speed);
277ec2c81c5Smario.six@gdsys.cc 	writeb(slaveadd << 1, &base->adr);/* write slave address */
278ec2c81c5Smario.six@gdsys.cc 	writeb(0x0, &base->sr);		/* clear status register */
279ec2c81c5Smario.six@gdsys.cc 	writeb(I2C_CR_MEN, &base->cr);	/* start I2C controller */
28026a33504SRichard Retanubun 
281b8ce3343SChunhe Lan 	timeval = get_ticks();
282ec2c81c5Smario.six@gdsys.cc 	while (readb(&base->sr) & I2C_SR_MBB) {
283b8ce3343SChunhe Lan 		if ((get_ticks() - timeval) < timeout)
284b8ce3343SChunhe Lan 			continue;
285b8ce3343SChunhe Lan 
286ec2c81c5Smario.six@gdsys.cc 		if (fsl_i2c_fixup(base))
287b8ce3343SChunhe Lan 			debug("i2c_init: BUS#%d failed to init\n",
288ecf591e3Smario.six@gdsys.cc 			      busnum);
289b8ce3343SChunhe Lan 
290b8ce3343SChunhe Lan 		break;
291b8ce3343SChunhe Lan 	}
292080c646dSJean-Christophe PLAGNIOL-VILLARD }
293080c646dSJean-Christophe PLAGNIOL-VILLARD 
294a059de11SMario Six static int i2c_wait4bus(const struct fsl_i2c_base *base)
295080c646dSJean-Christophe PLAGNIOL-VILLARD {
296f2302d44SStefan Roese 	unsigned long long timeval = get_ticks();
29792477a63STimur Tabi 	const unsigned long long timeout = usec2ticks(CONFIG_I2C_MBB_TIMEOUT);
298080c646dSJean-Christophe PLAGNIOL-VILLARD 
299ec2c81c5Smario.six@gdsys.cc 	while (readb(&base->sr) & I2C_SR_MBB) {
30092477a63STimur Tabi 		if ((get_ticks() - timeval) > timeout)
301080c646dSJean-Christophe PLAGNIOL-VILLARD 			return -1;
302080c646dSJean-Christophe PLAGNIOL-VILLARD 	}
303080c646dSJean-Christophe PLAGNIOL-VILLARD 
304080c646dSJean-Christophe PLAGNIOL-VILLARD 	return 0;
305080c646dSJean-Christophe PLAGNIOL-VILLARD }
306080c646dSJean-Christophe PLAGNIOL-VILLARD 
307d4f422f8SMario Six static int 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) {
323a059de11SMario Six 			debug("%s: MAL\n", __func__);
324080c646dSJean-Christophe PLAGNIOL-VILLARD 			return -1;
325080c646dSJean-Christophe PLAGNIOL-VILLARD 		}
326080c646dSJean-Christophe PLAGNIOL-VILLARD 
327080c646dSJean-Christophe PLAGNIOL-VILLARD 		if (!(csr & I2C_SR_MCF))	{
328a059de11SMario Six 			debug("%s: unfinished\n", __func__);
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)) {
333a059de11SMario Six 			debug("%s: No RXACK\n", __func__);
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 
340a059de11SMario Six 	debug("%s: timed out\n", __func__);
341080c646dSJean-Christophe PLAGNIOL-VILLARD 	return -1;
342080c646dSJean-Christophe PLAGNIOL-VILLARD }
343080c646dSJean-Christophe PLAGNIOL-VILLARD 
344d4f422f8SMario Six static int i2c_write_addr(const struct fsl_i2c_base *base, u8 dev,
345a059de11SMario Six 			  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 
353ecf591e3Smario.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 
359d4f422f8SMario Six static int __i2c_write_data(const struct fsl_i2c_base *base, u8 *data,
360a059de11SMario Six 			    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 
367ecf591e3Smario.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 
374d4f422f8SMario Six static int __i2c_read_data(const struct fsl_i2c_base *base, u8 *data,
375a059de11SMario Six 			   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++) {
386ecf591e3Smario.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 
405a059de11SMario Six static int __i2c_read(const struct fsl_i2c_base *base, u8 chip_addr, u8 *offset,
406a059de11SMario Six 		      int olen, u8 *data, int dlen)
407080c646dSJean-Christophe PLAGNIOL-VILLARD {
4082b21e960Smario.six@gdsys.cc 	int ret = -1; /* signal error */
409080c646dSJean-Christophe PLAGNIOL-VILLARD 
410ecf591e3Smario.six@gdsys.cc 	if (i2c_wait4bus(base) < 0)
411b778c1b5SReinhard Pfau 		return -1;
412b778c1b5SReinhard Pfau 
413386b2769Smario.six@gdsys.cc 	/* Some drivers use offset lengths in excess of 4 bytes. These drivers
414386b2769Smario.six@gdsys.cc 	 * adhere to the following convention:
415386b2769Smario.six@gdsys.cc 	 * - the offset length is passed as negative (that is, the absolute
416386b2769Smario.six@gdsys.cc 	 *   value of olen is the actual offset length)
417386b2769Smario.six@gdsys.cc 	 * - the offset itself is passed in data, which is overwritten by the
418386b2769Smario.six@gdsys.cc 	 *   subsequent read operation
419a405764cSShaveta Leekha 	 */
4202b21e960Smario.six@gdsys.cc 	if (olen < 0) {
421ecf591e3Smario.six@gdsys.cc 		if (i2c_write_addr(base, chip_addr, I2C_WRITE_BIT, 0) != 0)
422ecf591e3Smario.six@gdsys.cc 			ret = __i2c_write_data(base, data, -olen);
423a405764cSShaveta Leekha 
42403a112aaSmario.six@gdsys.cc 		if (ret != -olen)
425a405764cSShaveta Leekha 			return -1;
426a405764cSShaveta Leekha 
427ecf591e3Smario.six@gdsys.cc 		if (dlen && i2c_write_addr(base, chip_addr,
4282b21e960Smario.six@gdsys.cc 					   I2C_READ_BIT, 1) != 0)
429ecf591e3Smario.six@gdsys.cc 			ret = __i2c_read_data(base, data, dlen);
430a405764cSShaveta Leekha 	} else {
4312b21e960Smario.six@gdsys.cc 		if ((!dlen || olen > 0) &&
432ecf591e3Smario.six@gdsys.cc 		    i2c_write_addr(base, chip_addr, I2C_WRITE_BIT, 0) != 0  &&
433ecf591e3Smario.six@gdsys.cc 		    __i2c_write_data(base, offset, olen) == olen)
4342b21e960Smario.six@gdsys.cc 			ret = 0; /* No error so far */
435080c646dSJean-Christophe PLAGNIOL-VILLARD 
436ecf591e3Smario.six@gdsys.cc 		if (dlen && i2c_write_addr(base, chip_addr, I2C_READ_BIT,
4372b21e960Smario.six@gdsys.cc 					   olen ? 1 : 0) != 0)
438ecf591e3Smario.six@gdsys.cc 			ret = __i2c_read_data(base, data, dlen);
439a405764cSShaveta Leekha 	}
440080c646dSJean-Christophe PLAGNIOL-VILLARD 
441ec2c81c5Smario.six@gdsys.cc 	writeb(I2C_CR_MEN, &base->cr);
442080c646dSJean-Christophe PLAGNIOL-VILLARD 
443ecf591e3Smario.six@gdsys.cc 	if (i2c_wait4bus(base)) /* Wait until STOP */
444d1c9e5b3SJoakim Tjernlund 		debug("i2c_read: wait4bus timed out\n");
445d1c9e5b3SJoakim Tjernlund 
4462b21e960Smario.six@gdsys.cc 	if (ret == dlen)
447080c646dSJean-Christophe PLAGNIOL-VILLARD 		return 0;
448080c646dSJean-Christophe PLAGNIOL-VILLARD 
449080c646dSJean-Christophe PLAGNIOL-VILLARD 	return -1;
450080c646dSJean-Christophe PLAGNIOL-VILLARD }
451080c646dSJean-Christophe PLAGNIOL-VILLARD 
452a059de11SMario Six static int __i2c_write(const struct fsl_i2c_base *base, u8 chip_addr,
453a059de11SMario Six 		       u8 *offset, int olen, u8 *data, int dlen)
454080c646dSJean-Christophe PLAGNIOL-VILLARD {
4552b21e960Smario.six@gdsys.cc 	int ret = -1; /* signal error */
456080c646dSJean-Christophe PLAGNIOL-VILLARD 
457ecf591e3Smario.six@gdsys.cc 	if (i2c_wait4bus(base) < 0)
458b8ce3343SChunhe Lan 		return -1;
459b8ce3343SChunhe Lan 
460ecf591e3Smario.six@gdsys.cc 	if (i2c_write_addr(base, chip_addr, I2C_WRITE_BIT, 0) != 0 &&
461ecf591e3Smario.six@gdsys.cc 	    __i2c_write_data(base, offset, olen) == olen) {
462ecf591e3Smario.six@gdsys.cc 		ret = __i2c_write_data(base, data, dlen);
463080c646dSJean-Christophe PLAGNIOL-VILLARD 	}
464080c646dSJean-Christophe PLAGNIOL-VILLARD 
465ec2c81c5Smario.six@gdsys.cc 	writeb(I2C_CR_MEN, &base->cr);
466ecf591e3Smario.six@gdsys.cc 	if (i2c_wait4bus(base)) /* Wait until STOP */
46721f4cbb7SJoakim Tjernlund 		debug("i2c_write: wait4bus timed out\n");
468080c646dSJean-Christophe PLAGNIOL-VILLARD 
4692b21e960Smario.six@gdsys.cc 	if (ret == dlen)
470080c646dSJean-Christophe PLAGNIOL-VILLARD 		return 0;
471080c646dSJean-Christophe PLAGNIOL-VILLARD 
472080c646dSJean-Christophe PLAGNIOL-VILLARD 	return -1;
473080c646dSJean-Christophe PLAGNIOL-VILLARD }
474080c646dSJean-Christophe PLAGNIOL-VILLARD 
475a059de11SMario Six static int __i2c_probe_chip(const struct fsl_i2c_base *base, uchar chip)
476080c646dSJean-Christophe PLAGNIOL-VILLARD {
477a059de11SMario Six 	/* For unknown reason the controller will ACK when
478080c646dSJean-Christophe PLAGNIOL-VILLARD 	 * probing for a slave with the same address, so skip
479080c646dSJean-Christophe PLAGNIOL-VILLARD 	 * it.
480080c646dSJean-Christophe PLAGNIOL-VILLARD 	 */
481ec2c81c5Smario.six@gdsys.cc 	if (chip == (readb(&base->adr) >> 1))
482080c646dSJean-Christophe PLAGNIOL-VILLARD 		return -1;
483080c646dSJean-Christophe PLAGNIOL-VILLARD 
484ecf591e3Smario.six@gdsys.cc 	return __i2c_read(base, chip, 0, 0, NULL, 0);
485080c646dSJean-Christophe PLAGNIOL-VILLARD }
486080c646dSJean-Christophe PLAGNIOL-VILLARD 
487a059de11SMario Six static uint __i2c_set_bus_speed(const struct fsl_i2c_base *base,
488a059de11SMario Six 				uint speed, int i2c_clk)
489080c646dSJean-Christophe PLAGNIOL-VILLARD {
490ec2c81c5Smario.six@gdsys.cc 	writeb(0, &base->cr);		/* stop controller */
491ecf591e3Smario.six@gdsys.cc 	set_i2c_bus_speed(base, i2c_clk, speed);
492ec2c81c5Smario.six@gdsys.cc 	writeb(I2C_CR_MEN, &base->cr);	/* start controller */
493d8c82db4STimur Tabi 
494d8c82db4STimur Tabi 	return 0;
495080c646dSJean-Christophe PLAGNIOL-VILLARD }
496080c646dSJean-Christophe PLAGNIOL-VILLARD 
497dbc82ce3Smario.six@gdsys.cc #ifndef CONFIG_DM_I2C
498ad7e657cSmario.six@gdsys.cc static void fsl_i2c_init(struct i2c_adapter *adap, int speed, int slaveadd)
499ad7e657cSmario.six@gdsys.cc {
500ecf591e3Smario.six@gdsys.cc 	__i2c_init(i2c_base[adap->hwadapnr], speed, slaveadd,
501ecf591e3Smario.six@gdsys.cc 		   get_i2c_clock(adap->hwadapnr), adap->hwadapnr);
502ad7e657cSmario.six@gdsys.cc }
503ad7e657cSmario.six@gdsys.cc 
504a059de11SMario Six static int fsl_i2c_probe_chip(struct i2c_adapter *adap, uchar chip)
505ad7e657cSmario.six@gdsys.cc {
506ecf591e3Smario.six@gdsys.cc 	return __i2c_probe_chip(i2c_base[adap->hwadapnr], chip);
507ad7e657cSmario.six@gdsys.cc }
508ad7e657cSmario.six@gdsys.cc 
509a059de11SMario Six static int fsl_i2c_read(struct i2c_adapter *adap, u8 chip_addr, uint offset,
510a059de11SMario Six 			int olen, u8 *data, int dlen)
511ad7e657cSmario.six@gdsys.cc {
512ecf591e3Smario.six@gdsys.cc 	u8 *o = (u8 *)&offset;
513a059de11SMario Six 
514ecf591e3Smario.six@gdsys.cc 	return __i2c_read(i2c_base[adap->hwadapnr], chip_addr, &o[4 - olen],
515ecf591e3Smario.six@gdsys.cc 			  olen, data, dlen);
516ad7e657cSmario.six@gdsys.cc }
517ad7e657cSmario.six@gdsys.cc 
518a059de11SMario Six static int fsl_i2c_write(struct i2c_adapter *adap, u8 chip_addr, uint offset,
519a059de11SMario Six 			 int olen, u8 *data, int dlen)
520ad7e657cSmario.six@gdsys.cc {
521ecf591e3Smario.six@gdsys.cc 	u8 *o = (u8 *)&offset;
522a059de11SMario Six 
523ecf591e3Smario.six@gdsys.cc 	return __i2c_write(i2c_base[adap->hwadapnr], chip_addr, &o[4 - olen],
524ecf591e3Smario.six@gdsys.cc 			   olen, data, dlen);
525ad7e657cSmario.six@gdsys.cc }
526ad7e657cSmario.six@gdsys.cc 
527a059de11SMario Six static uint fsl_i2c_set_bus_speed(struct i2c_adapter *adap, uint speed)
528ad7e657cSmario.six@gdsys.cc {
529ecf591e3Smario.six@gdsys.cc 	return __i2c_set_bus_speed(i2c_base[adap->hwadapnr], speed,
530ecf591e3Smario.six@gdsys.cc 				   get_i2c_clock(adap->hwadapnr));
531ad7e657cSmario.six@gdsys.cc }
532ad7e657cSmario.six@gdsys.cc 
53300f792e0SHeiko Schocher /*
53400f792e0SHeiko Schocher  * Register fsl i2c adapters
53500f792e0SHeiko Schocher  */
53616579ecbSmario.six@gdsys.cc U_BOOT_I2C_ADAP_COMPLETE(fsl_0, fsl_i2c_init, fsl_i2c_probe_chip, fsl_i2c_read,
53700f792e0SHeiko Schocher 			 fsl_i2c_write, fsl_i2c_set_bus_speed,
53800f792e0SHeiko Schocher 			 CONFIG_SYS_FSL_I2C_SPEED, CONFIG_SYS_FSL_I2C_SLAVE,
53900f792e0SHeiko Schocher 			 0)
54000f792e0SHeiko Schocher #ifdef CONFIG_SYS_FSL_I2C2_OFFSET
54116579ecbSmario.six@gdsys.cc U_BOOT_I2C_ADAP_COMPLETE(fsl_1, fsl_i2c_init, fsl_i2c_probe_chip, fsl_i2c_read,
54200f792e0SHeiko Schocher 			 fsl_i2c_write, fsl_i2c_set_bus_speed,
54300f792e0SHeiko Schocher 			 CONFIG_SYS_FSL_I2C2_SPEED, CONFIG_SYS_FSL_I2C2_SLAVE,
54400f792e0SHeiko Schocher 			 1)
545c1bce4ffSHeiko Schocher #endif
546a17fd10fSShengzhou Liu #ifdef CONFIG_SYS_FSL_I2C3_OFFSET
54716579ecbSmario.six@gdsys.cc U_BOOT_I2C_ADAP_COMPLETE(fsl_2, fsl_i2c_init, fsl_i2c_probe_chip, fsl_i2c_read,
548a17fd10fSShengzhou Liu 			 fsl_i2c_write, fsl_i2c_set_bus_speed,
549a17fd10fSShengzhou Liu 			 CONFIG_SYS_FSL_I2C3_SPEED, CONFIG_SYS_FSL_I2C3_SLAVE,
550a17fd10fSShengzhou Liu 			 2)
551a17fd10fSShengzhou Liu #endif
552a17fd10fSShengzhou Liu #ifdef CONFIG_SYS_FSL_I2C4_OFFSET
55316579ecbSmario.six@gdsys.cc U_BOOT_I2C_ADAP_COMPLETE(fsl_3, fsl_i2c_init, fsl_i2c_probe_chip, fsl_i2c_read,
554a17fd10fSShengzhou Liu 			 fsl_i2c_write, fsl_i2c_set_bus_speed,
555a17fd10fSShengzhou Liu 			 CONFIG_SYS_FSL_I2C4_SPEED, CONFIG_SYS_FSL_I2C4_SLAVE,
556a17fd10fSShengzhou Liu 			 3)
557a17fd10fSShengzhou Liu #endif
558dbc82ce3Smario.six@gdsys.cc #else /* CONFIG_DM_I2C */
559dbc82ce3Smario.six@gdsys.cc static int fsl_i2c_probe_chip(struct udevice *bus, u32 chip_addr,
560dbc82ce3Smario.six@gdsys.cc 			      u32 chip_flags)
561dbc82ce3Smario.six@gdsys.cc {
562dbc82ce3Smario.six@gdsys.cc 	struct fsl_i2c_dev *dev = dev_get_priv(bus);
563a059de11SMario Six 
564dbc82ce3Smario.six@gdsys.cc 	return __i2c_probe_chip(dev->base, chip_addr);
565dbc82ce3Smario.six@gdsys.cc }
566dbc82ce3Smario.six@gdsys.cc 
567a059de11SMario Six static int fsl_i2c_set_bus_speed(struct udevice *bus, uint speed)
568dbc82ce3Smario.six@gdsys.cc {
569dbc82ce3Smario.six@gdsys.cc 	struct fsl_i2c_dev *dev = dev_get_priv(bus);
570a059de11SMario Six 
571dbc82ce3Smario.six@gdsys.cc 	return __i2c_set_bus_speed(dev->base, speed, dev->i2c_clk);
572dbc82ce3Smario.six@gdsys.cc }
573dbc82ce3Smario.six@gdsys.cc 
574dbc82ce3Smario.six@gdsys.cc static int fsl_i2c_ofdata_to_platdata(struct udevice *bus)
575dbc82ce3Smario.six@gdsys.cc {
576dbc82ce3Smario.six@gdsys.cc 	struct fsl_i2c_dev *dev = dev_get_priv(bus);
577*e5c762f5SMario Six 	struct clk clock;
578dbc82ce3Smario.six@gdsys.cc 
579d934832dSMario Six 	dev->base = map_sysmem(dev_read_addr(bus), sizeof(struct fsl_i2c_base));
580dbc82ce3Smario.six@gdsys.cc 
581dbc82ce3Smario.six@gdsys.cc 	if (!dev->base)
582dbc82ce3Smario.six@gdsys.cc 		return -ENOMEM;
583dbc82ce3Smario.six@gdsys.cc 
58484a4d34eSMario Six 	dev->index = dev_read_u32_default(bus, "cell-index", -1);
58584a4d34eSMario Six 	dev->slaveadd = dev_read_u32_default(bus, "u-boot,i2c-slave-addr",
58684a4d34eSMario Six 					     0x7f);
58784a4d34eSMario Six 	dev->speed = dev_read_u32_default(bus, "clock-frequency", 400000);
588dbc82ce3Smario.six@gdsys.cc 
589*e5c762f5SMario Six 	if (!clk_get_by_index(bus, 0, &clock))
590*e5c762f5SMario Six 		dev->i2c_clk = clk_get_rate(&clock);
591*e5c762f5SMario Six 	else
592*e5c762f5SMario Six 		dev->i2c_clk = dev->index ? gd->arch.i2c2_clk :
593*e5c762f5SMario Six 					    gd->arch.i2c1_clk;
594dbc82ce3Smario.six@gdsys.cc 
595dbc82ce3Smario.six@gdsys.cc 	return 0;
596dbc82ce3Smario.six@gdsys.cc }
597dbc82ce3Smario.six@gdsys.cc 
598dbc82ce3Smario.six@gdsys.cc static int fsl_i2c_probe(struct udevice *bus)
599dbc82ce3Smario.six@gdsys.cc {
600dbc82ce3Smario.six@gdsys.cc 	struct fsl_i2c_dev *dev = dev_get_priv(bus);
601a059de11SMario Six 
602dbc82ce3Smario.six@gdsys.cc 	__i2c_init(dev->base, dev->speed, dev->slaveadd, dev->i2c_clk,
603dbc82ce3Smario.six@gdsys.cc 		   dev->index);
604dbc82ce3Smario.six@gdsys.cc 	return 0;
605dbc82ce3Smario.six@gdsys.cc }
606dbc82ce3Smario.six@gdsys.cc 
607dbc82ce3Smario.six@gdsys.cc static int fsl_i2c_xfer(struct udevice *bus, struct i2c_msg *msg, int nmsgs)
608dbc82ce3Smario.six@gdsys.cc {
609dbc82ce3Smario.six@gdsys.cc 	struct fsl_i2c_dev *dev = dev_get_priv(bus);
610dbc82ce3Smario.six@gdsys.cc 	struct i2c_msg *dmsg, *omsg, dummy;
611dbc82ce3Smario.six@gdsys.cc 
612dbc82ce3Smario.six@gdsys.cc 	memset(&dummy, 0, sizeof(struct i2c_msg));
613dbc82ce3Smario.six@gdsys.cc 
614dbc82ce3Smario.six@gdsys.cc 	/* We expect either two messages (one with an offset and one with the
615a059de11SMario Six 	 * actual data) or one message (just data)
616a059de11SMario Six 	 */
617dbc82ce3Smario.six@gdsys.cc 	if (nmsgs > 2 || nmsgs == 0) {
618dbc82ce3Smario.six@gdsys.cc 		debug("%s: Only one or two messages are supported.", __func__);
619dbc82ce3Smario.six@gdsys.cc 		return -1;
620dbc82ce3Smario.six@gdsys.cc 	}
621dbc82ce3Smario.six@gdsys.cc 
622dbc82ce3Smario.six@gdsys.cc 	omsg = nmsgs == 1 ? &dummy : msg;
623dbc82ce3Smario.six@gdsys.cc 	dmsg = nmsgs == 1 ? msg : msg + 1;
624dbc82ce3Smario.six@gdsys.cc 
625dbc82ce3Smario.six@gdsys.cc 	if (dmsg->flags & I2C_M_RD)
626dbc82ce3Smario.six@gdsys.cc 		return __i2c_read(dev->base, dmsg->addr, omsg->buf, omsg->len,
627dbc82ce3Smario.six@gdsys.cc 				  dmsg->buf, dmsg->len);
628dbc82ce3Smario.six@gdsys.cc 	else
629dbc82ce3Smario.six@gdsys.cc 		return __i2c_write(dev->base, dmsg->addr, omsg->buf, omsg->len,
630dbc82ce3Smario.six@gdsys.cc 				   dmsg->buf, dmsg->len);
631dbc82ce3Smario.six@gdsys.cc }
632dbc82ce3Smario.six@gdsys.cc 
633dbc82ce3Smario.six@gdsys.cc static const struct dm_i2c_ops fsl_i2c_ops = {
634dbc82ce3Smario.six@gdsys.cc 	.xfer           = fsl_i2c_xfer,
635dbc82ce3Smario.six@gdsys.cc 	.probe_chip     = fsl_i2c_probe_chip,
636dbc82ce3Smario.six@gdsys.cc 	.set_bus_speed  = fsl_i2c_set_bus_speed,
637dbc82ce3Smario.six@gdsys.cc };
638dbc82ce3Smario.six@gdsys.cc 
639dbc82ce3Smario.six@gdsys.cc static const struct udevice_id fsl_i2c_ids[] = {
640dbc82ce3Smario.six@gdsys.cc 	{ .compatible = "fsl-i2c", },
641dbc82ce3Smario.six@gdsys.cc 	{ /* sentinel */ }
642dbc82ce3Smario.six@gdsys.cc };
643dbc82ce3Smario.six@gdsys.cc 
644dbc82ce3Smario.six@gdsys.cc U_BOOT_DRIVER(i2c_fsl) = {
645dbc82ce3Smario.six@gdsys.cc 	.name = "i2c_fsl",
646dbc82ce3Smario.six@gdsys.cc 	.id = UCLASS_I2C,
647dbc82ce3Smario.six@gdsys.cc 	.of_match = fsl_i2c_ids,
648dbc82ce3Smario.six@gdsys.cc 	.probe = fsl_i2c_probe,
649dbc82ce3Smario.six@gdsys.cc 	.ofdata_to_platdata = fsl_i2c_ofdata_to_platdata,
650dbc82ce3Smario.six@gdsys.cc 	.priv_auto_alloc_size = sizeof(struct fsl_i2c_dev),
651dbc82ce3Smario.six@gdsys.cc 	.ops = &fsl_i2c_ops,
652dbc82ce3Smario.six@gdsys.cc };
653dbc82ce3Smario.six@gdsys.cc 
654dbc82ce3Smario.six@gdsys.cc #endif /* CONFIG_DM_I2C */
655