xref: /openbmc/u-boot/drivers/i2c/fsl_i2c.c (revision 645cb46e2b6b446d10745662edba680ed36b5184)
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 */
15dbc82ce3Smario.six@gdsys.cc #include <dm.h>
16dbc82ce3Smario.six@gdsys.cc #include <mapmem.h>
17080c646dSJean-Christophe PLAGNIOL-VILLARD 
1892477a63STimur Tabi /* The maximum number of microseconds we will wait until another master has
1992477a63STimur Tabi  * released the bus.  If not defined in the board header file, then use a
2092477a63STimur Tabi  * generic value.
2192477a63STimur Tabi  */
2292477a63STimur Tabi #ifndef CONFIG_I2C_MBB_TIMEOUT
2392477a63STimur Tabi #define CONFIG_I2C_MBB_TIMEOUT	100000
2492477a63STimur Tabi #endif
2592477a63STimur Tabi 
2692477a63STimur Tabi /* The maximum number of microseconds we will wait for a read or write
2792477a63STimur Tabi  * operation to complete.  If not defined in the board header file, then use a
2892477a63STimur Tabi  * generic value.
2992477a63STimur Tabi  */
3092477a63STimur Tabi #ifndef CONFIG_I2C_TIMEOUT
316dd38cc3SShaveta Leekha #define CONFIG_I2C_TIMEOUT	100000
3292477a63STimur Tabi #endif
33080c646dSJean-Christophe PLAGNIOL-VILLARD 
34080c646dSJean-Christophe PLAGNIOL-VILLARD #define I2C_READ_BIT  1
35080c646dSJean-Christophe PLAGNIOL-VILLARD #define I2C_WRITE_BIT 0
36080c646dSJean-Christophe PLAGNIOL-VILLARD 
37d8c82db4STimur Tabi DECLARE_GLOBAL_DATA_PTR;
38d8c82db4STimur Tabi 
39dbc82ce3Smario.six@gdsys.cc #ifndef CONFIG_DM_I2C
40ec2c81c5Smario.six@gdsys.cc static const struct fsl_i2c_base *i2c_base[4] = {
41ec2c81c5Smario.six@gdsys.cc 	(struct fsl_i2c_base *)(CONFIG_SYS_IMMR + CONFIG_SYS_FSL_I2C_OFFSET),
4200f792e0SHeiko Schocher #ifdef CONFIG_SYS_FSL_I2C2_OFFSET
43ec2c81c5Smario.six@gdsys.cc 	(struct fsl_i2c_base *)(CONFIG_SYS_IMMR + CONFIG_SYS_FSL_I2C2_OFFSET),
44a17fd10fSShengzhou Liu #endif
45a17fd10fSShengzhou Liu #ifdef CONFIG_SYS_FSL_I2C3_OFFSET
46ec2c81c5Smario.six@gdsys.cc 	(struct fsl_i2c_base *)(CONFIG_SYS_IMMR + CONFIG_SYS_FSL_I2C3_OFFSET),
47a17fd10fSShengzhou Liu #endif
48a17fd10fSShengzhou Liu #ifdef CONFIG_SYS_FSL_I2C4_OFFSET
49ec2c81c5Smario.six@gdsys.cc 	(struct fsl_i2c_base *)(CONFIG_SYS_IMMR + CONFIG_SYS_FSL_I2C4_OFFSET)
50080c646dSJean-Christophe PLAGNIOL-VILLARD #endif
51080c646dSJean-Christophe PLAGNIOL-VILLARD };
52dbc82ce3Smario.six@gdsys.cc #endif
53080c646dSJean-Christophe PLAGNIOL-VILLARD 
54d8c82db4STimur Tabi /* I2C speed map for a DFSR value of 1 */
55d8c82db4STimur Tabi 
56*645cb46eSTom Rini #ifdef __M68K__
57d8c82db4STimur Tabi /*
58d8c82db4STimur Tabi  * Map I2C frequency dividers to FDR and DFSR values
59d8c82db4STimur Tabi  *
60d8c82db4STimur Tabi  * This structure is used to define the elements of a table that maps I2C
61d8c82db4STimur Tabi  * frequency divider (I2C clock rate divided by I2C bus speed) to a value to be
62d8c82db4STimur Tabi  * programmed into the Frequency Divider Ratio (FDR) and Digital Filter
63d8c82db4STimur Tabi  * Sampling Rate (DFSR) registers.
64d8c82db4STimur Tabi  *
65d8c82db4STimur Tabi  * The actual table should be defined in the board file, and it must be called
66d8c82db4STimur Tabi  * fsl_i2c_speed_map[].
67d8c82db4STimur Tabi  *
68d8c82db4STimur Tabi  * The last entry of the table must have a value of {-1, X}, where X is same
69d8c82db4STimur Tabi  * FDR/DFSR values as the second-to-last entry.  This guarantees that any
70d8c82db4STimur Tabi  * search through the array will always find a match.
71d8c82db4STimur Tabi  *
72d8c82db4STimur Tabi  * The values of the divider must be in increasing numerical order, i.e.
73d8c82db4STimur Tabi  * fsl_i2c_speed_map[x+1].divider > fsl_i2c_speed_map[x].divider.
74d8c82db4STimur Tabi  *
75d8c82db4STimur Tabi  * For this table, the values are based on a value of 1 for the DFSR
76d8c82db4STimur Tabi  * register.  See the application note AN2919 "Determining the I2C Frequency
77d8c82db4STimur Tabi  * Divider Ratio for SCL"
785d9a5efaSTsiChung Liew  *
795d9a5efaSTsiChung Liew  * ColdFire I2C frequency dividers for FDR values are different from
805d9a5efaSTsiChung Liew  * PowerPC. The protocol to use the I2C module is still the same.
815d9a5efaSTsiChung Liew  * A different table is defined and are based on MCF5xxx user manual.
825d9a5efaSTsiChung Liew  *
83d8c82db4STimur Tabi  */
84d8c82db4STimur Tabi static const struct {
85d8c82db4STimur Tabi 	unsigned short divider;
86d8c82db4STimur Tabi 	u8 fdr;
87d8c82db4STimur Tabi } fsl_i2c_speed_map[] = {
885d9a5efaSTsiChung Liew 	{20, 32}, {22, 33}, {24, 34}, {26, 35},
895d9a5efaSTsiChung Liew 	{28, 0}, {28, 36}, {30, 1}, {32, 37},
905d9a5efaSTsiChung Liew 	{34, 2}, {36, 38}, {40, 3}, {40, 39},
915d9a5efaSTsiChung Liew 	{44, 4}, {48, 5}, {48, 40}, {56, 6},
925d9a5efaSTsiChung Liew 	{56, 41}, {64, 42}, {68, 7}, {72, 43},
935d9a5efaSTsiChung Liew 	{80, 8}, {80, 44}, {88, 9}, {96, 41},
945d9a5efaSTsiChung Liew 	{104, 10}, {112, 42}, {128, 11}, {128, 43},
955d9a5efaSTsiChung Liew 	{144, 12}, {160, 13}, {160, 48}, {192, 14},
965d9a5efaSTsiChung Liew 	{192, 49}, {224, 50}, {240, 15}, {256, 51},
975d9a5efaSTsiChung Liew 	{288, 16}, {320, 17}, {320, 52}, {384, 18},
985d9a5efaSTsiChung Liew 	{384, 53}, {448, 54}, {480, 19}, {512, 55},
995d9a5efaSTsiChung Liew 	{576, 20}, {640, 21}, {640, 56}, {768, 22},
1005d9a5efaSTsiChung Liew 	{768, 57}, {960, 23}, {896, 58}, {1024, 59},
1015d9a5efaSTsiChung Liew 	{1152, 24}, {1280, 25}, {1280, 60}, {1536, 26},
1025d9a5efaSTsiChung Liew 	{1536, 61}, {1792, 62}, {1920, 27}, {2048, 63},
1035d9a5efaSTsiChung Liew 	{2304, 28}, {2560, 29}, {3072, 30}, {3840, 31},
1045d9a5efaSTsiChung Liew 	{-1, 31}
105d8c82db4STimur Tabi };
106*645cb46eSTom Rini #endif
107d8c82db4STimur Tabi 
108d8c82db4STimur Tabi /**
109d8c82db4STimur Tabi  * Set the I2C bus speed for a given I2C device
110d8c82db4STimur Tabi  *
111ec2c81c5Smario.six@gdsys.cc  * @param base: the I2C device registers
112d8c82db4STimur Tabi  * @i2c_clk: I2C bus clock frequency
113d8c82db4STimur Tabi  * @speed: the desired speed of the bus
114d8c82db4STimur Tabi  *
115d8c82db4STimur Tabi  * The I2C device must be stopped before calling this function.
116d8c82db4STimur Tabi  *
117d8c82db4STimur Tabi  * The return value is the actual bus speed that is set.
118d8c82db4STimur Tabi  */
119ec2c81c5Smario.six@gdsys.cc static unsigned int set_i2c_bus_speed(const struct fsl_i2c_base *base,
120d8c82db4STimur Tabi 	unsigned int i2c_clk, unsigned int speed)
121d8c82db4STimur Tabi {
122b4141195SMasahiro Yamada 	unsigned short divider = min(i2c_clk / speed, (unsigned int)USHRT_MAX);
123d8c82db4STimur Tabi 
124d8c82db4STimur Tabi 	/*
125d8c82db4STimur Tabi 	 * We want to choose an FDR/DFSR that generates an I2C bus speed that
126d8c82db4STimur Tabi 	 * is equal to or lower than the requested speed.  That means that we
127d8c82db4STimur Tabi 	 * want the first divider that is equal to or greater than the
128d8c82db4STimur Tabi 	 * calculated divider.
129d8c82db4STimur Tabi 	 */
1305d9a5efaSTsiChung Liew #ifdef __PPC__
13199404202SJoakim Tjernlund 	u8 dfsr, fdr = 0x31; /* Default if no FDR found */
13299404202SJoakim Tjernlund 	/* a, b and dfsr matches identifiers A,B and C respectively in AN2919 */
13399404202SJoakim Tjernlund 	unsigned short a, b, ga, gb;
13499404202SJoakim Tjernlund 	unsigned long c_div, est_div;
13599404202SJoakim Tjernlund 
136d01ee4dbSJoakim Tjernlund #ifdef CONFIG_FSL_I2C_CUSTOM_DFSR
137d01ee4dbSJoakim Tjernlund 	dfsr = CONFIG_FSL_I2C_CUSTOM_DFSR;
138d01ee4dbSJoakim Tjernlund #else
13999404202SJoakim Tjernlund 	/* Condition 1: dfsr <= 50/T */
14099404202SJoakim Tjernlund 	dfsr = (5 * (i2c_clk / 1000)) / 100000;
1415d9a5efaSTsiChung Liew #endif
142d01ee4dbSJoakim Tjernlund #ifdef CONFIG_FSL_I2C_CUSTOM_FDR
143d01ee4dbSJoakim Tjernlund 	fdr = CONFIG_FSL_I2C_CUSTOM_FDR;
144d01ee4dbSJoakim Tjernlund 	speed = i2c_clk / divider; /* Fake something */
145d01ee4dbSJoakim Tjernlund #else
14699404202SJoakim Tjernlund 	debug("Requested speed:%d, i2c_clk:%d\n", speed, i2c_clk);
14799404202SJoakim Tjernlund 	if (!dfsr)
14899404202SJoakim Tjernlund 		dfsr = 1;
14999404202SJoakim Tjernlund 
15099404202SJoakim Tjernlund 	est_div = ~0;
15199404202SJoakim Tjernlund 	for (ga = 0x4, a = 10; a <= 30; ga++, a += 2) {
15299404202SJoakim Tjernlund 		for (gb = 0; gb < 8; gb++) {
15399404202SJoakim Tjernlund 			b = 16 << gb;
15499404202SJoakim Tjernlund 			c_div = b * (a + ((3*dfsr)/b)*2);
15599404202SJoakim Tjernlund 			if ((c_div > divider) && (c_div < est_div)) {
15699404202SJoakim Tjernlund 				unsigned short bin_gb, bin_ga;
15799404202SJoakim Tjernlund 
15899404202SJoakim Tjernlund 				est_div = c_div;
15999404202SJoakim Tjernlund 				bin_gb = gb << 2;
16099404202SJoakim Tjernlund 				bin_ga = (ga & 0x3) | ((ga & 0x4) << 3);
16199404202SJoakim Tjernlund 				fdr = bin_gb | bin_ga;
16299404202SJoakim Tjernlund 				speed = i2c_clk / est_div;
16399404202SJoakim Tjernlund 				debug("FDR:0x%.2x, div:%ld, ga:0x%x, gb:0x%x, "
16499404202SJoakim Tjernlund 				      "a:%d, b:%d, speed:%d\n",
16599404202SJoakim Tjernlund 				      fdr, est_div, ga, gb, a, b, speed);
16699404202SJoakim Tjernlund 				/* Condition 2 not accounted for */
16799404202SJoakim Tjernlund 				debug("Tr <= %d ns\n",
16899404202SJoakim Tjernlund 				      (b - 3 * dfsr) * 1000000 /
16999404202SJoakim Tjernlund 				      (i2c_clk / 1000));
17099404202SJoakim Tjernlund 			}
17199404202SJoakim Tjernlund 		}
17299404202SJoakim Tjernlund 		if (a == 20)
17399404202SJoakim Tjernlund 			a += 2;
17499404202SJoakim Tjernlund 		if (a == 24)
17599404202SJoakim Tjernlund 			a += 4;
17699404202SJoakim Tjernlund 	}
17799404202SJoakim Tjernlund 	debug("divider:%d, est_div:%ld, DFSR:%d\n", divider, est_div, dfsr);
17899404202SJoakim Tjernlund 	debug("FDR:0x%.2x, speed:%d\n", fdr, speed);
17999404202SJoakim Tjernlund #endif
180ec2c81c5Smario.six@gdsys.cc 	writeb(dfsr, &base->dfsrr);	/* set default filter */
181ec2c81c5Smario.six@gdsys.cc 	writeb(fdr, &base->fdr);	/* set bus speed */
18299404202SJoakim Tjernlund #else
18399404202SJoakim Tjernlund 	unsigned int i;
18499404202SJoakim Tjernlund 
18599404202SJoakim Tjernlund 	for (i = 0; i < ARRAY_SIZE(fsl_i2c_speed_map); i++)
18699404202SJoakim Tjernlund 		if (fsl_i2c_speed_map[i].divider >= divider) {
18799404202SJoakim Tjernlund 			u8 fdr;
18899404202SJoakim Tjernlund 
189d01ee4dbSJoakim Tjernlund 			fdr = fsl_i2c_speed_map[i].fdr;
190d01ee4dbSJoakim Tjernlund 			speed = i2c_clk / fsl_i2c_speed_map[i].divider;
191ec2c81c5Smario.six@gdsys.cc 			writeb(fdr, &base->fdr);	/* set bus speed */
192d01ee4dbSJoakim Tjernlund 
1933e3f766aSKumar Gala 			break;
1943e3f766aSKumar Gala 		}
19599404202SJoakim Tjernlund #endif
196d8c82db4STimur Tabi 	return speed;
197d8c82db4STimur Tabi }
198d8c82db4STimur Tabi 
199dbc82ce3Smario.six@gdsys.cc #ifndef CONFIG_DM_I2C
20062f730ffSKim Phillips static unsigned int get_i2c_clock(int bus)
201c9a8b25eSJerry Huang {
202c9a8b25eSJerry Huang 	if (bus)
203609e6ec3SSimon Glass 		return gd->arch.i2c2_clk;	/* I2C2 clock */
204c9a8b25eSJerry Huang 	else
205609e6ec3SSimon Glass 		return gd->arch.i2c1_clk;	/* I2C1 clock */
206c9a8b25eSJerry Huang }
207dbc82ce3Smario.six@gdsys.cc #endif
208c9a8b25eSJerry Huang 
209ec2c81c5Smario.six@gdsys.cc static int fsl_i2c_fixup(const struct fsl_i2c_base *base)
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 
223ec2c81c5Smario.six@gdsys.cc 	writeb(I2C_CR_MEN | I2C_CR_MSTA, &base->cr);
224b8ce3343SChunhe Lan 
225b8ce3343SChunhe Lan 	timeval = get_ticks();
226ec2c81c5Smario.six@gdsys.cc 	while (!(readb(&base->sr) & I2C_SR_MBB)) {
227b8ce3343SChunhe Lan 		if ((get_ticks() - timeval) > timeout)
228b8ce3343SChunhe Lan 			goto err;
229b8ce3343SChunhe Lan 	}
230b8ce3343SChunhe Lan 
231ec2c81c5Smario.six@gdsys.cc 	if (readb(&base->sr) & I2C_SR_MAL) {
232b8ce3343SChunhe Lan 		/* SDA is stuck low */
233ec2c81c5Smario.six@gdsys.cc 		writeb(0, &base->cr);
234b8ce3343SChunhe Lan 		udelay(100);
235ec2c81c5Smario.six@gdsys.cc 		writeb(I2C_CR_MSTA | flags, &base->cr);
236ec2c81c5Smario.six@gdsys.cc 		writeb(I2C_CR_MEN | I2C_CR_MSTA | flags, &base->cr);
237b8ce3343SChunhe Lan 	}
238b8ce3343SChunhe Lan 
239ec2c81c5Smario.six@gdsys.cc 	readb(&base->dr);
240b8ce3343SChunhe Lan 
241b8ce3343SChunhe Lan 	timeval = get_ticks();
242ec2c81c5Smario.six@gdsys.cc 	while (!(readb(&base->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:
249ec2c81c5Smario.six@gdsys.cc 	writeb(I2C_CR_MEN | flags, &base->cr);
250ec2c81c5Smario.six@gdsys.cc 	writeb(0, &base->sr);
251b8ce3343SChunhe Lan 	udelay(100);
252b8ce3343SChunhe Lan 
253b8ce3343SChunhe Lan 	return ret;
254b8ce3343SChunhe Lan }
255b8ce3343SChunhe Lan 
256ecf591e3Smario.six@gdsys.cc static void __i2c_init(const struct fsl_i2c_base *base, int speed, int
257ecf591e3Smario.six@gdsys.cc 		       slaveadd, int i2c_clk, int busnum)
258080c646dSJean-Christophe PLAGNIOL-VILLARD {
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
269ec2c81c5Smario.six@gdsys.cc 	writeb(0, &base->cr);		/* stop I2C controller */
270080c646dSJean-Christophe PLAGNIOL-VILLARD 	udelay(5);			/* let it shutdown in peace */
271ecf591e3Smario.six@gdsys.cc 	set_i2c_bus_speed(base, i2c_clk, speed);
272ec2c81c5Smario.six@gdsys.cc 	writeb(slaveadd << 1, &base->adr);/* write slave address */
273ec2c81c5Smario.six@gdsys.cc 	writeb(0x0, &base->sr);		/* clear status register */
274ec2c81c5Smario.six@gdsys.cc 	writeb(I2C_CR_MEN, &base->cr);	/* start I2C controller */
27526a33504SRichard Retanubun 
276b8ce3343SChunhe Lan 	timeval = get_ticks();
277ec2c81c5Smario.six@gdsys.cc 	while (readb(&base->sr) & I2C_SR_MBB) {
278b8ce3343SChunhe Lan 		if ((get_ticks() - timeval) < timeout)
279b8ce3343SChunhe Lan 			continue;
280b8ce3343SChunhe Lan 
281ec2c81c5Smario.six@gdsys.cc 		if (fsl_i2c_fixup(base))
282b8ce3343SChunhe Lan 			debug("i2c_init: BUS#%d failed to init\n",
283ecf591e3Smario.six@gdsys.cc 			      busnum);
284b8ce3343SChunhe Lan 
285b8ce3343SChunhe Lan 		break;
286b8ce3343SChunhe Lan 	}
287b8ce3343SChunhe Lan 
28826a33504SRichard Retanubun #ifdef CONFIG_SYS_I2C_BOARD_LATE_INIT
28926a33504SRichard Retanubun 	/* Call board specific i2c bus reset routine AFTER the bus has been
29026a33504SRichard Retanubun 	 * initialized. Use either this callpoint or i2c_init_board;
29126a33504SRichard Retanubun 	 * which is called before i2c_init operations.
29226a33504SRichard Retanubun 	 * For details about this problem see doc/I2C_Edge_Conditions.
29326a33504SRichard Retanubun 	*/
29426a33504SRichard Retanubun 	i2c_board_late_init();
29526a33504SRichard Retanubun #endif
296080c646dSJean-Christophe PLAGNIOL-VILLARD }
297080c646dSJean-Christophe PLAGNIOL-VILLARD 
29821f4cbb7SJoakim Tjernlund static int
299ecf591e3Smario.six@gdsys.cc i2c_wait4bus(const struct fsl_i2c_base *base)
300080c646dSJean-Christophe PLAGNIOL-VILLARD {
301f2302d44SStefan Roese 	unsigned long long timeval = get_ticks();
30292477a63STimur Tabi 	const unsigned long long timeout = usec2ticks(CONFIG_I2C_MBB_TIMEOUT);
303080c646dSJean-Christophe PLAGNIOL-VILLARD 
304ec2c81c5Smario.six@gdsys.cc 	while (readb(&base->sr) & I2C_SR_MBB) {
30592477a63STimur Tabi 		if ((get_ticks() - timeval) > timeout)
306080c646dSJean-Christophe PLAGNIOL-VILLARD 			return -1;
307080c646dSJean-Christophe PLAGNIOL-VILLARD 	}
308080c646dSJean-Christophe PLAGNIOL-VILLARD 
309080c646dSJean-Christophe PLAGNIOL-VILLARD 	return 0;
310080c646dSJean-Christophe PLAGNIOL-VILLARD }
311080c646dSJean-Christophe PLAGNIOL-VILLARD 
312ecf591e3Smario.six@gdsys.cc static inline int
313ecf591e3Smario.six@gdsys.cc i2c_wait(const struct fsl_i2c_base *base, int write)
314080c646dSJean-Christophe PLAGNIOL-VILLARD {
315080c646dSJean-Christophe PLAGNIOL-VILLARD 	u32 csr;
316f2302d44SStefan Roese 	unsigned long long timeval = get_ticks();
31792477a63STimur Tabi 	const unsigned long long timeout = usec2ticks(CONFIG_I2C_TIMEOUT);
318080c646dSJean-Christophe PLAGNIOL-VILLARD 
319080c646dSJean-Christophe PLAGNIOL-VILLARD 	do {
320ec2c81c5Smario.six@gdsys.cc 		csr = readb(&base->sr);
321080c646dSJean-Christophe PLAGNIOL-VILLARD 		if (!(csr & I2C_SR_MIF))
322080c646dSJean-Christophe PLAGNIOL-VILLARD 			continue;
32321f4cbb7SJoakim Tjernlund 		/* Read again to allow register to stabilise */
324ec2c81c5Smario.six@gdsys.cc 		csr = readb(&base->sr);
325080c646dSJean-Christophe PLAGNIOL-VILLARD 
326ec2c81c5Smario.six@gdsys.cc 		writeb(0x0, &base->sr);
327080c646dSJean-Christophe PLAGNIOL-VILLARD 
328080c646dSJean-Christophe PLAGNIOL-VILLARD 		if (csr & I2C_SR_MAL) {
329080c646dSJean-Christophe PLAGNIOL-VILLARD 			debug("i2c_wait: MAL\n");
330080c646dSJean-Christophe PLAGNIOL-VILLARD 			return -1;
331080c646dSJean-Christophe PLAGNIOL-VILLARD 		}
332080c646dSJean-Christophe PLAGNIOL-VILLARD 
333080c646dSJean-Christophe PLAGNIOL-VILLARD 		if (!(csr & I2C_SR_MCF))	{
334080c646dSJean-Christophe PLAGNIOL-VILLARD 			debug("i2c_wait: unfinished\n");
335080c646dSJean-Christophe PLAGNIOL-VILLARD 			return -1;
336080c646dSJean-Christophe PLAGNIOL-VILLARD 		}
337080c646dSJean-Christophe PLAGNIOL-VILLARD 
338080c646dSJean-Christophe PLAGNIOL-VILLARD 		if (write == I2C_WRITE_BIT && (csr & I2C_SR_RXAK)) {
339080c646dSJean-Christophe PLAGNIOL-VILLARD 			debug("i2c_wait: No RXACK\n");
340080c646dSJean-Christophe PLAGNIOL-VILLARD 			return -1;
341080c646dSJean-Christophe PLAGNIOL-VILLARD 		}
342080c646dSJean-Christophe PLAGNIOL-VILLARD 
343080c646dSJean-Christophe PLAGNIOL-VILLARD 		return 0;
34492477a63STimur Tabi 	} while ((get_ticks() - timeval) < timeout);
345080c646dSJean-Christophe PLAGNIOL-VILLARD 
346080c646dSJean-Christophe PLAGNIOL-VILLARD 	debug("i2c_wait: timed out\n");
347080c646dSJean-Christophe PLAGNIOL-VILLARD 	return -1;
348080c646dSJean-Christophe PLAGNIOL-VILLARD }
349080c646dSJean-Christophe PLAGNIOL-VILLARD 
350ecf591e3Smario.six@gdsys.cc static inline int
351ecf591e3Smario.six@gdsys.cc i2c_write_addr(const struct fsl_i2c_base *base, u8 dev, u8 dir, int rsta)
352080c646dSJean-Christophe PLAGNIOL-VILLARD {
353080c646dSJean-Christophe PLAGNIOL-VILLARD 	writeb(I2C_CR_MEN | I2C_CR_MSTA | I2C_CR_MTX
354080c646dSJean-Christophe PLAGNIOL-VILLARD 	       | (rsta ? I2C_CR_RSTA : 0),
355ec2c81c5Smario.six@gdsys.cc 	       &base->cr);
356080c646dSJean-Christophe PLAGNIOL-VILLARD 
357ec2c81c5Smario.six@gdsys.cc 	writeb((dev << 1) | dir, &base->dr);
358080c646dSJean-Christophe PLAGNIOL-VILLARD 
359ecf591e3Smario.six@gdsys.cc 	if (i2c_wait(base, I2C_WRITE_BIT) < 0)
360080c646dSJean-Christophe PLAGNIOL-VILLARD 		return 0;
361080c646dSJean-Christophe PLAGNIOL-VILLARD 
362080c646dSJean-Christophe PLAGNIOL-VILLARD 	return 1;
363080c646dSJean-Christophe PLAGNIOL-VILLARD }
364080c646dSJean-Christophe PLAGNIOL-VILLARD 
365ecf591e3Smario.six@gdsys.cc static inline int
366ecf591e3Smario.six@gdsys.cc __i2c_write_data(const struct fsl_i2c_base *base, u8 *data, int length)
367080c646dSJean-Christophe PLAGNIOL-VILLARD {
368080c646dSJean-Christophe PLAGNIOL-VILLARD 	int i;
369080c646dSJean-Christophe PLAGNIOL-VILLARD 
370080c646dSJean-Christophe PLAGNIOL-VILLARD 	for (i = 0; i < length; i++) {
371ec2c81c5Smario.six@gdsys.cc 		writeb(data[i], &base->dr);
372080c646dSJean-Christophe PLAGNIOL-VILLARD 
373ecf591e3Smario.six@gdsys.cc 		if (i2c_wait(base, I2C_WRITE_BIT) < 0)
374080c646dSJean-Christophe PLAGNIOL-VILLARD 			break;
375080c646dSJean-Christophe PLAGNIOL-VILLARD 	}
376080c646dSJean-Christophe PLAGNIOL-VILLARD 
377080c646dSJean-Christophe PLAGNIOL-VILLARD 	return i;
378080c646dSJean-Christophe PLAGNIOL-VILLARD }
379080c646dSJean-Christophe PLAGNIOL-VILLARD 
380ecf591e3Smario.six@gdsys.cc static inline int
381ecf591e3Smario.six@gdsys.cc __i2c_read_data(const struct fsl_i2c_base *base, u8 *data, int length)
382080c646dSJean-Christophe PLAGNIOL-VILLARD {
383080c646dSJean-Christophe PLAGNIOL-VILLARD 	int i;
384080c646dSJean-Christophe PLAGNIOL-VILLARD 
385080c646dSJean-Christophe PLAGNIOL-VILLARD 	writeb(I2C_CR_MEN | I2C_CR_MSTA | ((length == 1) ? I2C_CR_TXAK : 0),
386ec2c81c5Smario.six@gdsys.cc 	       &base->cr);
387080c646dSJean-Christophe PLAGNIOL-VILLARD 
388080c646dSJean-Christophe PLAGNIOL-VILLARD 	/* dummy read */
389ec2c81c5Smario.six@gdsys.cc 	readb(&base->dr);
390080c646dSJean-Christophe PLAGNIOL-VILLARD 
391080c646dSJean-Christophe PLAGNIOL-VILLARD 	for (i = 0; i < length; i++) {
392ecf591e3Smario.six@gdsys.cc 		if (i2c_wait(base, I2C_READ_BIT) < 0)
393080c646dSJean-Christophe PLAGNIOL-VILLARD 			break;
394080c646dSJean-Christophe PLAGNIOL-VILLARD 
395080c646dSJean-Christophe PLAGNIOL-VILLARD 		/* Generate ack on last next to last byte */
396080c646dSJean-Christophe PLAGNIOL-VILLARD 		if (i == length - 2)
397080c646dSJean-Christophe PLAGNIOL-VILLARD 			writeb(I2C_CR_MEN | I2C_CR_MSTA | I2C_CR_TXAK,
398ec2c81c5Smario.six@gdsys.cc 			       &base->cr);
399080c646dSJean-Christophe PLAGNIOL-VILLARD 
400d1c9e5b3SJoakim Tjernlund 		/* Do not generate stop on last byte */
401080c646dSJean-Christophe PLAGNIOL-VILLARD 		if (i == length - 1)
402d1c9e5b3SJoakim Tjernlund 			writeb(I2C_CR_MEN | I2C_CR_MSTA | I2C_CR_MTX,
403ec2c81c5Smario.six@gdsys.cc 			       &base->cr);
404080c646dSJean-Christophe PLAGNIOL-VILLARD 
405ec2c81c5Smario.six@gdsys.cc 		data[i] = readb(&base->dr);
406080c646dSJean-Christophe PLAGNIOL-VILLARD 	}
407080c646dSJean-Christophe PLAGNIOL-VILLARD 
408080c646dSJean-Christophe PLAGNIOL-VILLARD 	return i;
409080c646dSJean-Christophe PLAGNIOL-VILLARD }
410080c646dSJean-Christophe PLAGNIOL-VILLARD 
41100f792e0SHeiko Schocher static int
412ecf591e3Smario.six@gdsys.cc __i2c_read(const struct fsl_i2c_base *base, u8 chip_addr, u8 *offset, int olen,
4132b21e960Smario.six@gdsys.cc 	   u8 *data, int dlen)
414080c646dSJean-Christophe PLAGNIOL-VILLARD {
4152b21e960Smario.six@gdsys.cc 	int ret = -1; /* signal error */
416080c646dSJean-Christophe PLAGNIOL-VILLARD 
417ecf591e3Smario.six@gdsys.cc 	if (i2c_wait4bus(base) < 0)
418b778c1b5SReinhard Pfau 		return -1;
419b778c1b5SReinhard Pfau 
420386b2769Smario.six@gdsys.cc 	/* Some drivers use offset lengths in excess of 4 bytes. These drivers
421386b2769Smario.six@gdsys.cc 	 * adhere to the following convention:
422386b2769Smario.six@gdsys.cc 	 * - the offset length is passed as negative (that is, the absolute
423386b2769Smario.six@gdsys.cc 	 *   value of olen is the actual offset length)
424386b2769Smario.six@gdsys.cc 	 * - the offset itself is passed in data, which is overwritten by the
425386b2769Smario.six@gdsys.cc 	 *   subsequent read operation
426a405764cSShaveta Leekha 	 */
4272b21e960Smario.six@gdsys.cc 	if (olen < 0) {
428ecf591e3Smario.six@gdsys.cc 		if (i2c_write_addr(base, chip_addr, I2C_WRITE_BIT, 0) != 0)
429ecf591e3Smario.six@gdsys.cc 			ret = __i2c_write_data(base, data, -olen);
430a405764cSShaveta Leekha 
43103a112aaSmario.six@gdsys.cc 		if (ret != -olen)
432a405764cSShaveta Leekha 			return -1;
433a405764cSShaveta Leekha 
434ecf591e3Smario.six@gdsys.cc 		if (dlen && i2c_write_addr(base, chip_addr,
4352b21e960Smario.six@gdsys.cc 					   I2C_READ_BIT, 1) != 0)
436ecf591e3Smario.six@gdsys.cc 			ret = __i2c_read_data(base, data, dlen);
437a405764cSShaveta Leekha 	} else {
4382b21e960Smario.six@gdsys.cc 		if ((!dlen || olen > 0) &&
439ecf591e3Smario.six@gdsys.cc 		    i2c_write_addr(base, chip_addr, I2C_WRITE_BIT, 0) != 0  &&
440ecf591e3Smario.six@gdsys.cc 		    __i2c_write_data(base, offset, olen) == olen)
4412b21e960Smario.six@gdsys.cc 			ret = 0; /* No error so far */
442080c646dSJean-Christophe PLAGNIOL-VILLARD 
443ecf591e3Smario.six@gdsys.cc 		if (dlen && i2c_write_addr(base, chip_addr, I2C_READ_BIT,
4442b21e960Smario.six@gdsys.cc 					   olen ? 1 : 0) != 0)
445ecf591e3Smario.six@gdsys.cc 			ret = __i2c_read_data(base, data, dlen);
446a405764cSShaveta Leekha 	}
447080c646dSJean-Christophe PLAGNIOL-VILLARD 
448ec2c81c5Smario.six@gdsys.cc 	writeb(I2C_CR_MEN, &base->cr);
449080c646dSJean-Christophe PLAGNIOL-VILLARD 
450ecf591e3Smario.six@gdsys.cc 	if (i2c_wait4bus(base)) /* Wait until STOP */
451d1c9e5b3SJoakim Tjernlund 		debug("i2c_read: wait4bus timed out\n");
452d1c9e5b3SJoakim Tjernlund 
4532b21e960Smario.six@gdsys.cc 	if (ret == dlen)
454080c646dSJean-Christophe PLAGNIOL-VILLARD 		return 0;
455080c646dSJean-Christophe PLAGNIOL-VILLARD 
456080c646dSJean-Christophe PLAGNIOL-VILLARD 	return -1;
457080c646dSJean-Christophe PLAGNIOL-VILLARD }
458080c646dSJean-Christophe PLAGNIOL-VILLARD 
45900f792e0SHeiko Schocher static int
460ecf591e3Smario.six@gdsys.cc __i2c_write(const struct fsl_i2c_base *base, u8 chip_addr, u8 *offset, int olen,
4612b21e960Smario.six@gdsys.cc 	    u8 *data, int dlen)
462080c646dSJean-Christophe PLAGNIOL-VILLARD {
4632b21e960Smario.six@gdsys.cc 	int ret = -1; /* signal error */
464080c646dSJean-Christophe PLAGNIOL-VILLARD 
465ecf591e3Smario.six@gdsys.cc 	if (i2c_wait4bus(base) < 0)
466b8ce3343SChunhe Lan 		return -1;
467b8ce3343SChunhe Lan 
468ecf591e3Smario.six@gdsys.cc 	if (i2c_write_addr(base, chip_addr, I2C_WRITE_BIT, 0) != 0 &&
469ecf591e3Smario.six@gdsys.cc 	    __i2c_write_data(base, offset, olen) == olen) {
470ecf591e3Smario.six@gdsys.cc 		ret = __i2c_write_data(base, data, dlen);
471080c646dSJean-Christophe PLAGNIOL-VILLARD 	}
472080c646dSJean-Christophe PLAGNIOL-VILLARD 
473ec2c81c5Smario.six@gdsys.cc 	writeb(I2C_CR_MEN, &base->cr);
474ecf591e3Smario.six@gdsys.cc 	if (i2c_wait4bus(base)) /* Wait until STOP */
47521f4cbb7SJoakim Tjernlund 		debug("i2c_write: wait4bus timed out\n");
476080c646dSJean-Christophe PLAGNIOL-VILLARD 
4772b21e960Smario.six@gdsys.cc 	if (ret == dlen)
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
484ecf591e3Smario.six@gdsys.cc __i2c_probe_chip(const struct fsl_i2c_base *base, uchar chip)
485080c646dSJean-Christophe PLAGNIOL-VILLARD {
486080c646dSJean-Christophe PLAGNIOL-VILLARD 	/* For unknow reason the controller will ACK when
487080c646dSJean-Christophe PLAGNIOL-VILLARD 	 * probing for a slave with the same address, so skip
488080c646dSJean-Christophe PLAGNIOL-VILLARD 	 * it.
489080c646dSJean-Christophe PLAGNIOL-VILLARD 	 */
490ec2c81c5Smario.six@gdsys.cc 	if (chip == (readb(&base->adr) >> 1))
491080c646dSJean-Christophe PLAGNIOL-VILLARD 		return -1;
492080c646dSJean-Christophe PLAGNIOL-VILLARD 
493ecf591e3Smario.six@gdsys.cc 	return __i2c_read(base, chip, 0, 0, NULL, 0);
494080c646dSJean-Christophe PLAGNIOL-VILLARD }
495080c646dSJean-Christophe PLAGNIOL-VILLARD 
496ecf591e3Smario.six@gdsys.cc static unsigned int __i2c_set_bus_speed(const struct fsl_i2c_base *base,
497ecf591e3Smario.six@gdsys.cc 			unsigned int speed, int i2c_clk)
498080c646dSJean-Christophe PLAGNIOL-VILLARD {
499ec2c81c5Smario.six@gdsys.cc 	writeb(0, &base->cr);		/* stop controller */
500ecf591e3Smario.six@gdsys.cc 	set_i2c_bus_speed(base, i2c_clk, speed);
501ec2c81c5Smario.six@gdsys.cc 	writeb(I2C_CR_MEN, &base->cr);	/* start controller */
502d8c82db4STimur Tabi 
503d8c82db4STimur Tabi 	return 0;
504080c646dSJean-Christophe PLAGNIOL-VILLARD }
505080c646dSJean-Christophe PLAGNIOL-VILLARD 
506dbc82ce3Smario.six@gdsys.cc #ifndef CONFIG_DM_I2C
507ad7e657cSmario.six@gdsys.cc static void fsl_i2c_init(struct i2c_adapter *adap, int speed, int slaveadd)
508ad7e657cSmario.six@gdsys.cc {
509ecf591e3Smario.six@gdsys.cc 	__i2c_init(i2c_base[adap->hwadapnr], speed, slaveadd,
510ecf591e3Smario.six@gdsys.cc 		   get_i2c_clock(adap->hwadapnr), adap->hwadapnr);
511ad7e657cSmario.six@gdsys.cc }
512ad7e657cSmario.six@gdsys.cc 
513ad7e657cSmario.six@gdsys.cc static int
514ad7e657cSmario.six@gdsys.cc fsl_i2c_probe_chip(struct i2c_adapter *adap, uchar chip)
515ad7e657cSmario.six@gdsys.cc {
516ecf591e3Smario.six@gdsys.cc 	return __i2c_probe_chip(i2c_base[adap->hwadapnr], chip);
517ad7e657cSmario.six@gdsys.cc }
518ad7e657cSmario.six@gdsys.cc 
519ad7e657cSmario.six@gdsys.cc static int
520ad7e657cSmario.six@gdsys.cc fsl_i2c_read(struct i2c_adapter *adap, u8 chip_addr, uint offset, int olen,
521ad7e657cSmario.six@gdsys.cc 	     u8 *data, int dlen)
522ad7e657cSmario.six@gdsys.cc {
523ecf591e3Smario.six@gdsys.cc 	u8 *o = (u8 *)&offset;
524ecf591e3Smario.six@gdsys.cc 	return __i2c_read(i2c_base[adap->hwadapnr], chip_addr, &o[4 - olen],
525ecf591e3Smario.six@gdsys.cc 			  olen, data, dlen);
526ad7e657cSmario.six@gdsys.cc }
527ad7e657cSmario.six@gdsys.cc 
528ad7e657cSmario.six@gdsys.cc static int
529ad7e657cSmario.six@gdsys.cc fsl_i2c_write(struct i2c_adapter *adap, u8 chip_addr, uint offset, int olen,
530ad7e657cSmario.six@gdsys.cc 	      u8 *data, int dlen)
531ad7e657cSmario.six@gdsys.cc {
532ecf591e3Smario.six@gdsys.cc 	u8 *o = (u8 *)&offset;
533ecf591e3Smario.six@gdsys.cc 	return __i2c_write(i2c_base[adap->hwadapnr], chip_addr, &o[4 - olen],
534ecf591e3Smario.six@gdsys.cc 			   olen, data, dlen);
535ad7e657cSmario.six@gdsys.cc }
536ad7e657cSmario.six@gdsys.cc 
537ad7e657cSmario.six@gdsys.cc static unsigned int fsl_i2c_set_bus_speed(struct i2c_adapter *adap,
538ad7e657cSmario.six@gdsys.cc 					  unsigned int speed)
539ad7e657cSmario.six@gdsys.cc {
540ecf591e3Smario.six@gdsys.cc 	return __i2c_set_bus_speed(i2c_base[adap->hwadapnr], speed,
541ecf591e3Smario.six@gdsys.cc 				   get_i2c_clock(adap->hwadapnr));
542ad7e657cSmario.six@gdsys.cc }
543ad7e657cSmario.six@gdsys.cc 
54400f792e0SHeiko Schocher /*
54500f792e0SHeiko Schocher  * Register fsl i2c adapters
54600f792e0SHeiko Schocher  */
54716579ecbSmario.six@gdsys.cc U_BOOT_I2C_ADAP_COMPLETE(fsl_0, fsl_i2c_init, fsl_i2c_probe_chip, fsl_i2c_read,
54800f792e0SHeiko Schocher 			 fsl_i2c_write, fsl_i2c_set_bus_speed,
54900f792e0SHeiko Schocher 			 CONFIG_SYS_FSL_I2C_SPEED, CONFIG_SYS_FSL_I2C_SLAVE,
55000f792e0SHeiko Schocher 			 0)
55100f792e0SHeiko Schocher #ifdef CONFIG_SYS_FSL_I2C2_OFFSET
55216579ecbSmario.six@gdsys.cc U_BOOT_I2C_ADAP_COMPLETE(fsl_1, fsl_i2c_init, fsl_i2c_probe_chip, fsl_i2c_read,
55300f792e0SHeiko Schocher 			 fsl_i2c_write, fsl_i2c_set_bus_speed,
55400f792e0SHeiko Schocher 			 CONFIG_SYS_FSL_I2C2_SPEED, CONFIG_SYS_FSL_I2C2_SLAVE,
55500f792e0SHeiko Schocher 			 1)
556c1bce4ffSHeiko Schocher #endif
557a17fd10fSShengzhou Liu #ifdef CONFIG_SYS_FSL_I2C3_OFFSET
55816579ecbSmario.six@gdsys.cc U_BOOT_I2C_ADAP_COMPLETE(fsl_2, fsl_i2c_init, fsl_i2c_probe_chip, fsl_i2c_read,
559a17fd10fSShengzhou Liu 			 fsl_i2c_write, fsl_i2c_set_bus_speed,
560a17fd10fSShengzhou Liu 			 CONFIG_SYS_FSL_I2C3_SPEED, CONFIG_SYS_FSL_I2C3_SLAVE,
561a17fd10fSShengzhou Liu 			 2)
562a17fd10fSShengzhou Liu #endif
563a17fd10fSShengzhou Liu #ifdef CONFIG_SYS_FSL_I2C4_OFFSET
56416579ecbSmario.six@gdsys.cc U_BOOT_I2C_ADAP_COMPLETE(fsl_3, fsl_i2c_init, fsl_i2c_probe_chip, fsl_i2c_read,
565a17fd10fSShengzhou Liu 			 fsl_i2c_write, fsl_i2c_set_bus_speed,
566a17fd10fSShengzhou Liu 			 CONFIG_SYS_FSL_I2C4_SPEED, CONFIG_SYS_FSL_I2C4_SLAVE,
567a17fd10fSShengzhou Liu 			 3)
568a17fd10fSShengzhou Liu #endif
569dbc82ce3Smario.six@gdsys.cc #else /* CONFIG_DM_I2C */
570dbc82ce3Smario.six@gdsys.cc static int fsl_i2c_probe_chip(struct udevice *bus, u32 chip_addr,
571dbc82ce3Smario.six@gdsys.cc 			      u32 chip_flags)
572dbc82ce3Smario.six@gdsys.cc {
573dbc82ce3Smario.six@gdsys.cc 	struct fsl_i2c_dev *dev = dev_get_priv(bus);
574dbc82ce3Smario.six@gdsys.cc 	return __i2c_probe_chip(dev->base, chip_addr);
575dbc82ce3Smario.six@gdsys.cc }
576dbc82ce3Smario.six@gdsys.cc 
577dbc82ce3Smario.six@gdsys.cc static int fsl_i2c_set_bus_speed(struct udevice *bus, unsigned int speed)
578dbc82ce3Smario.six@gdsys.cc {
579dbc82ce3Smario.six@gdsys.cc 	struct fsl_i2c_dev *dev = dev_get_priv(bus);
580dbc82ce3Smario.six@gdsys.cc 	return __i2c_set_bus_speed(dev->base, speed, dev->i2c_clk);
581dbc82ce3Smario.six@gdsys.cc }
582dbc82ce3Smario.six@gdsys.cc 
583dbc82ce3Smario.six@gdsys.cc static int fsl_i2c_ofdata_to_platdata(struct udevice *bus)
584dbc82ce3Smario.six@gdsys.cc {
585dbc82ce3Smario.six@gdsys.cc 	struct fsl_i2c_dev *dev = dev_get_priv(bus);
58627059c3eSmario.six@gdsys.cc 	fdt_addr_t addr;
58727059c3eSmario.six@gdsys.cc 	fdt_size_t size;
588e160f7d4SSimon Glass 	int node = dev_of_offset(bus);
589dbc82ce3Smario.six@gdsys.cc 
590e160f7d4SSimon Glass 	addr = fdtdec_get_addr_size_auto_noparent(gd->fdt_blob, node, "reg", 0,
591e160f7d4SSimon Glass 						  &size, false);
592dbc82ce3Smario.six@gdsys.cc 
593dbc82ce3Smario.six@gdsys.cc 	dev->base = map_sysmem(CONFIG_SYS_IMMR + addr, size);
594dbc82ce3Smario.six@gdsys.cc 
595dbc82ce3Smario.six@gdsys.cc 	if (!dev->base)
596dbc82ce3Smario.six@gdsys.cc 		return -ENOMEM;
597dbc82ce3Smario.six@gdsys.cc 
598e160f7d4SSimon Glass 	dev->index = fdtdec_get_int(gd->fdt_blob, node, "cell-index", -1);
599e160f7d4SSimon Glass 	dev->slaveadd = fdtdec_get_int(gd->fdt_blob, node,
600dbc82ce3Smario.six@gdsys.cc 				       "u-boot,i2c-slave-addr", 0x7f);
601e160f7d4SSimon Glass 	dev->speed = fdtdec_get_int(gd->fdt_blob, node, "clock-frequency",
602e160f7d4SSimon Glass 				    400000);
603dbc82ce3Smario.six@gdsys.cc 
604dbc82ce3Smario.six@gdsys.cc 	dev->i2c_clk = dev->index ? gd->arch.i2c2_clk : gd->arch.i2c1_clk;
605dbc82ce3Smario.six@gdsys.cc 
606dbc82ce3Smario.six@gdsys.cc 	return 0;
607dbc82ce3Smario.six@gdsys.cc }
608dbc82ce3Smario.six@gdsys.cc 
609dbc82ce3Smario.six@gdsys.cc static int fsl_i2c_probe(struct udevice *bus)
610dbc82ce3Smario.six@gdsys.cc {
611dbc82ce3Smario.six@gdsys.cc 	struct fsl_i2c_dev *dev = dev_get_priv(bus);
612dbc82ce3Smario.six@gdsys.cc 	__i2c_init(dev->base, dev->speed, dev->slaveadd, dev->i2c_clk,
613dbc82ce3Smario.six@gdsys.cc 		   dev->index);
614dbc82ce3Smario.six@gdsys.cc 	return 0;
615dbc82ce3Smario.six@gdsys.cc }
616dbc82ce3Smario.six@gdsys.cc 
617dbc82ce3Smario.six@gdsys.cc static int fsl_i2c_xfer(struct udevice *bus, struct i2c_msg *msg, int nmsgs)
618dbc82ce3Smario.six@gdsys.cc {
619dbc82ce3Smario.six@gdsys.cc 	struct fsl_i2c_dev *dev = dev_get_priv(bus);
620dbc82ce3Smario.six@gdsys.cc 	struct i2c_msg *dmsg, *omsg, dummy;
621dbc82ce3Smario.six@gdsys.cc 
622dbc82ce3Smario.six@gdsys.cc 	memset(&dummy, 0, sizeof(struct i2c_msg));
623dbc82ce3Smario.six@gdsys.cc 
624dbc82ce3Smario.six@gdsys.cc 	/* We expect either two messages (one with an offset and one with the
625dbc82ce3Smario.six@gdsys.cc 	 * actucal data) or one message (just data) */
626dbc82ce3Smario.six@gdsys.cc 	if (nmsgs > 2 || nmsgs == 0) {
627dbc82ce3Smario.six@gdsys.cc 		debug("%s: Only one or two messages are supported.", __func__);
628dbc82ce3Smario.six@gdsys.cc 		return -1;
629dbc82ce3Smario.six@gdsys.cc 	}
630dbc82ce3Smario.six@gdsys.cc 
631dbc82ce3Smario.six@gdsys.cc 	omsg = nmsgs == 1 ? &dummy : msg;
632dbc82ce3Smario.six@gdsys.cc 	dmsg = nmsgs == 1 ? msg : msg + 1;
633dbc82ce3Smario.six@gdsys.cc 
634dbc82ce3Smario.six@gdsys.cc 	if (dmsg->flags & I2C_M_RD)
635dbc82ce3Smario.six@gdsys.cc 		return __i2c_read(dev->base, dmsg->addr, omsg->buf, omsg->len,
636dbc82ce3Smario.six@gdsys.cc 				  dmsg->buf, dmsg->len);
637dbc82ce3Smario.six@gdsys.cc 	else
638dbc82ce3Smario.six@gdsys.cc 		return __i2c_write(dev->base, dmsg->addr, omsg->buf, omsg->len,
639dbc82ce3Smario.six@gdsys.cc 				   dmsg->buf, dmsg->len);
640dbc82ce3Smario.six@gdsys.cc }
641dbc82ce3Smario.six@gdsys.cc 
642dbc82ce3Smario.six@gdsys.cc static const struct dm_i2c_ops fsl_i2c_ops = {
643dbc82ce3Smario.six@gdsys.cc 	.xfer           = fsl_i2c_xfer,
644dbc82ce3Smario.six@gdsys.cc 	.probe_chip     = fsl_i2c_probe_chip,
645dbc82ce3Smario.six@gdsys.cc 	.set_bus_speed  = fsl_i2c_set_bus_speed,
646dbc82ce3Smario.six@gdsys.cc };
647dbc82ce3Smario.six@gdsys.cc 
648dbc82ce3Smario.six@gdsys.cc static const struct udevice_id fsl_i2c_ids[] = {
649dbc82ce3Smario.six@gdsys.cc 	{ .compatible = "fsl-i2c", },
650dbc82ce3Smario.six@gdsys.cc 	{ /* sentinel */ }
651dbc82ce3Smario.six@gdsys.cc };
652dbc82ce3Smario.six@gdsys.cc 
653dbc82ce3Smario.six@gdsys.cc U_BOOT_DRIVER(i2c_fsl) = {
654dbc82ce3Smario.six@gdsys.cc 	.name = "i2c_fsl",
655dbc82ce3Smario.six@gdsys.cc 	.id = UCLASS_I2C,
656dbc82ce3Smario.six@gdsys.cc 	.of_match = fsl_i2c_ids,
657dbc82ce3Smario.six@gdsys.cc 	.probe = fsl_i2c_probe,
658dbc82ce3Smario.six@gdsys.cc 	.ofdata_to_platdata = fsl_i2c_ofdata_to_platdata,
659dbc82ce3Smario.six@gdsys.cc 	.priv_auto_alloc_size = sizeof(struct fsl_i2c_dev),
660dbc82ce3Smario.six@gdsys.cc 	.ops = &fsl_i2c_ops,
661dbc82ce3Smario.six@gdsys.cc };
662dbc82ce3Smario.six@gdsys.cc 
663dbc82ce3Smario.six@gdsys.cc #endif /* CONFIG_DM_I2C */
664