xref: /openbmc/u-boot/drivers/i2c/fsl_i2c.c (revision 83d290c56fab2d38cd1ab4c4cc7099559c1d5046)
1*83d290c5STom Rini // SPDX-License-Identifier: GPL-2.0
2080c646dSJean-Christophe PLAGNIOL-VILLARD /*
392477a63STimur Tabi  * Copyright 2006,2009 Freescale Semiconductor, Inc.
4080c646dSJean-Christophe PLAGNIOL-VILLARD  *
500f792e0SHeiko Schocher  * 2012, Heiko Schocher, DENX Software Engineering, hs@denx.de.
600f792e0SHeiko Schocher  * Changes for multibus/multiadapter I2C support.
7080c646dSJean-Christophe PLAGNIOL-VILLARD  */
8080c646dSJean-Christophe PLAGNIOL-VILLARD 
9080c646dSJean-Christophe PLAGNIOL-VILLARD #include <common.h>
10080c646dSJean-Christophe PLAGNIOL-VILLARD #include <command.h>
11080c646dSJean-Christophe PLAGNIOL-VILLARD #include <i2c.h>		/* Functional interface */
12080c646dSJean-Christophe PLAGNIOL-VILLARD #include <asm/io.h>
13080c646dSJean-Christophe PLAGNIOL-VILLARD #include <asm/fsl_i2c.h>	/* HW definitions */
14e5c762f5SMario Six #include <clk.h>
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 
56645cb46eSTom 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 };
106645cb46eSTom 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  */
set_i2c_bus_speed(const struct fsl_i2c_base * base,uint i2c_clk,uint speed)119a059de11SMario Six static uint set_i2c_bus_speed(const struct fsl_i2c_base *base,
120a059de11SMario Six 			      uint i2c_clk, uint speed)
121d8c82db4STimur Tabi {
122a059de11SMario Six 	ushort divider = min(i2c_clk / speed, (uint)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 */
133a059de11SMario Six 	ushort a, b, ga, gb;
134a059de11SMario Six 	ulong 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);
155a059de11SMario Six 			if (c_div > divider && c_div < est_div) {
156a059de11SMario Six 				ushort 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;
163a059de11SMario Six 
164a059de11SMario Six 				debug("FDR: 0x%.2x, ", fdr);
165a059de11SMario Six 				debug("div: %ld, ", est_div);
166a059de11SMario Six 				debug("ga: 0x%x, gb: 0x%x, ", ga, gb);
167a059de11SMario Six 				debug("a: %d, b: %d, speed: %d\n", a, b, speed);
168a059de11SMario Six 
16999404202SJoakim Tjernlund 				/* Condition 2 not accounted for */
17099404202SJoakim Tjernlund 				debug("Tr <= %d ns\n",
17199404202SJoakim Tjernlund 				      (b - 3 * dfsr) * 1000000 /
17299404202SJoakim Tjernlund 				      (i2c_clk / 1000));
17399404202SJoakim Tjernlund 			}
17499404202SJoakim Tjernlund 		}
17599404202SJoakim Tjernlund 		if (a == 20)
17699404202SJoakim Tjernlund 			a += 2;
17799404202SJoakim Tjernlund 		if (a == 24)
17899404202SJoakim Tjernlund 			a += 4;
17999404202SJoakim Tjernlund 	}
18099404202SJoakim Tjernlund 	debug("divider: %d, est_div: %ld, DFSR: %d\n", divider, est_div, dfsr);
18199404202SJoakim Tjernlund 	debug("FDR: 0x%.2x, speed: %d\n", fdr, speed);
18299404202SJoakim Tjernlund #endif
183ec2c81c5Smario.six@gdsys.cc 	writeb(dfsr, &base->dfsrr);	/* set default filter */
184ec2c81c5Smario.six@gdsys.cc 	writeb(fdr, &base->fdr);	/* set bus speed */
18599404202SJoakim Tjernlund #else
186a059de11SMario Six 	uint i;
18799404202SJoakim Tjernlund 
18899404202SJoakim Tjernlund 	for (i = 0; i < ARRAY_SIZE(fsl_i2c_speed_map); i++)
18999404202SJoakim Tjernlund 		if (fsl_i2c_speed_map[i].divider >= divider) {
19099404202SJoakim Tjernlund 			u8 fdr;
19199404202SJoakim Tjernlund 
192d01ee4dbSJoakim Tjernlund 			fdr = fsl_i2c_speed_map[i].fdr;
193d01ee4dbSJoakim Tjernlund 			speed = i2c_clk / fsl_i2c_speed_map[i].divider;
194ec2c81c5Smario.six@gdsys.cc 			writeb(fdr, &base->fdr);	/* set bus speed */
195d01ee4dbSJoakim Tjernlund 
1963e3f766aSKumar Gala 			break;
1973e3f766aSKumar Gala 		}
19899404202SJoakim Tjernlund #endif
199d8c82db4STimur Tabi 	return speed;
200d8c82db4STimur Tabi }
201d8c82db4STimur Tabi 
202dbc82ce3Smario.six@gdsys.cc #ifndef CONFIG_DM_I2C
get_i2c_clock(int bus)203a059de11SMario Six static uint get_i2c_clock(int bus)
204c9a8b25eSJerry Huang {
205c9a8b25eSJerry Huang 	if (bus)
206609e6ec3SSimon Glass 		return gd->arch.i2c2_clk;	/* I2C2 clock */
207c9a8b25eSJerry Huang 	else
208609e6ec3SSimon Glass 		return gd->arch.i2c1_clk;	/* I2C1 clock */
209c9a8b25eSJerry Huang }
210dbc82ce3Smario.six@gdsys.cc #endif
211c9a8b25eSJerry Huang 
fsl_i2c_fixup(const struct fsl_i2c_base * base)212ec2c81c5Smario.six@gdsys.cc static int fsl_i2c_fixup(const struct fsl_i2c_base *base)
213b8ce3343SChunhe Lan {
214b8ce3343SChunhe Lan 	const unsigned long long timeout = usec2ticks(CONFIG_I2C_MBB_TIMEOUT);
215b8ce3343SChunhe Lan 	unsigned long long timeval = 0;
216b8ce3343SChunhe Lan 	int ret = -1;
217a059de11SMario Six 	uint flags = 0;
2189c3f77ebSChunhe Lan 
2199c3f77ebSChunhe Lan #ifdef CONFIG_SYS_FSL_ERRATUM_I2C_A004447
220a059de11SMario Six 	uint svr = get_svr();
221a059de11SMario Six 
2229c3f77ebSChunhe Lan 	if ((SVR_SOC_VER(svr) == SVR_8548 && IS_SVR_REV(svr, 3, 1)) ||
2239c3f77ebSChunhe Lan 	    (SVR_REV(svr) <= CONFIG_SYS_FSL_A004447_SVR_REV))
2249c3f77ebSChunhe Lan 		flags = I2C_CR_BIT6;
2259c3f77ebSChunhe Lan #endif
226b8ce3343SChunhe Lan 
227ec2c81c5Smario.six@gdsys.cc 	writeb(I2C_CR_MEN | I2C_CR_MSTA, &base->cr);
228b8ce3343SChunhe Lan 
229b8ce3343SChunhe Lan 	timeval = get_ticks();
230ec2c81c5Smario.six@gdsys.cc 	while (!(readb(&base->sr) & I2C_SR_MBB)) {
231b8ce3343SChunhe Lan 		if ((get_ticks() - timeval) > timeout)
232b8ce3343SChunhe Lan 			goto err;
233b8ce3343SChunhe Lan 	}
234b8ce3343SChunhe Lan 
235ec2c81c5Smario.six@gdsys.cc 	if (readb(&base->sr) & I2C_SR_MAL) {
236b8ce3343SChunhe Lan 		/* SDA is stuck low */
237ec2c81c5Smario.six@gdsys.cc 		writeb(0, &base->cr);
238b8ce3343SChunhe Lan 		udelay(100);
239ec2c81c5Smario.six@gdsys.cc 		writeb(I2C_CR_MSTA | flags, &base->cr);
240ec2c81c5Smario.six@gdsys.cc 		writeb(I2C_CR_MEN | I2C_CR_MSTA | flags, &base->cr);
241b8ce3343SChunhe Lan 	}
242b8ce3343SChunhe Lan 
243ec2c81c5Smario.six@gdsys.cc 	readb(&base->dr);
244b8ce3343SChunhe Lan 
245b8ce3343SChunhe Lan 	timeval = get_ticks();
246ec2c81c5Smario.six@gdsys.cc 	while (!(readb(&base->sr) & I2C_SR_MIF)) {
247b8ce3343SChunhe Lan 		if ((get_ticks() - timeval) > timeout)
248b8ce3343SChunhe Lan 			goto err;
249b8ce3343SChunhe Lan 	}
250b8ce3343SChunhe Lan 	ret = 0;
251b8ce3343SChunhe Lan 
252b8ce3343SChunhe Lan err:
253ec2c81c5Smario.six@gdsys.cc 	writeb(I2C_CR_MEN | flags, &base->cr);
254ec2c81c5Smario.six@gdsys.cc 	writeb(0, &base->sr);
255b8ce3343SChunhe Lan 	udelay(100);
256b8ce3343SChunhe Lan 
257b8ce3343SChunhe Lan 	return ret;
258b8ce3343SChunhe Lan }
259b8ce3343SChunhe Lan 
__i2c_init(const struct fsl_i2c_base * base,int speed,int slaveadd,int i2c_clk,int busnum)260ecf591e3Smario.six@gdsys.cc static void __i2c_init(const struct fsl_i2c_base *base, int speed, int
261ecf591e3Smario.six@gdsys.cc 		       slaveadd, int i2c_clk, int busnum)
262080c646dSJean-Christophe PLAGNIOL-VILLARD {
263b8ce3343SChunhe Lan 	const unsigned long long timeout = usec2ticks(CONFIG_I2C_MBB_TIMEOUT);
264b8ce3343SChunhe Lan 	unsigned long long timeval;
265080c646dSJean-Christophe PLAGNIOL-VILLARD 
26639df00d9SHeiko Schocher #ifdef CONFIG_SYS_I2C_INIT_BOARD
26726a33504SRichard Retanubun 	/* Call board specific i2c bus reset routine before accessing the
26826a33504SRichard Retanubun 	 * environment, which might be in a chip on that bus. For details
26926a33504SRichard Retanubun 	 * about this problem see doc/I2C_Edge_Conditions.
27026a33504SRichard Retanubun 	 */
27139df00d9SHeiko Schocher 	i2c_init_board();
27239df00d9SHeiko Schocher #endif
273ec2c81c5Smario.six@gdsys.cc 	writeb(0, &base->cr);		/* stop I2C controller */
274080c646dSJean-Christophe PLAGNIOL-VILLARD 	udelay(5);			/* let it shutdown in peace */
275ecf591e3Smario.six@gdsys.cc 	set_i2c_bus_speed(base, i2c_clk, speed);
276ec2c81c5Smario.six@gdsys.cc 	writeb(slaveadd << 1, &base->adr);/* write slave address */
277ec2c81c5Smario.six@gdsys.cc 	writeb(0x0, &base->sr);		/* clear status register */
278ec2c81c5Smario.six@gdsys.cc 	writeb(I2C_CR_MEN, &base->cr);	/* start I2C controller */
27926a33504SRichard Retanubun 
280b8ce3343SChunhe Lan 	timeval = get_ticks();
281ec2c81c5Smario.six@gdsys.cc 	while (readb(&base->sr) & I2C_SR_MBB) {
282b8ce3343SChunhe Lan 		if ((get_ticks() - timeval) < timeout)
283b8ce3343SChunhe Lan 			continue;
284b8ce3343SChunhe Lan 
285ec2c81c5Smario.six@gdsys.cc 		if (fsl_i2c_fixup(base))
286b8ce3343SChunhe Lan 			debug("i2c_init: BUS#%d failed to init\n",
287ecf591e3Smario.six@gdsys.cc 			      busnum);
288b8ce3343SChunhe Lan 
289b8ce3343SChunhe Lan 		break;
290b8ce3343SChunhe Lan 	}
291080c646dSJean-Christophe PLAGNIOL-VILLARD }
292080c646dSJean-Christophe PLAGNIOL-VILLARD 
i2c_wait4bus(const struct fsl_i2c_base * base)293a059de11SMario Six static int i2c_wait4bus(const struct fsl_i2c_base *base)
294080c646dSJean-Christophe PLAGNIOL-VILLARD {
295f2302d44SStefan Roese 	unsigned long long timeval = get_ticks();
29692477a63STimur Tabi 	const unsigned long long timeout = usec2ticks(CONFIG_I2C_MBB_TIMEOUT);
297080c646dSJean-Christophe PLAGNIOL-VILLARD 
298ec2c81c5Smario.six@gdsys.cc 	while (readb(&base->sr) & I2C_SR_MBB) {
29992477a63STimur Tabi 		if ((get_ticks() - timeval) > timeout)
300080c646dSJean-Christophe PLAGNIOL-VILLARD 			return -1;
301080c646dSJean-Christophe PLAGNIOL-VILLARD 	}
302080c646dSJean-Christophe PLAGNIOL-VILLARD 
303080c646dSJean-Christophe PLAGNIOL-VILLARD 	return 0;
304080c646dSJean-Christophe PLAGNIOL-VILLARD }
305080c646dSJean-Christophe PLAGNIOL-VILLARD 
i2c_wait(const struct fsl_i2c_base * base,int write)306d4f422f8SMario Six static int i2c_wait(const struct fsl_i2c_base *base, int write)
307080c646dSJean-Christophe PLAGNIOL-VILLARD {
308080c646dSJean-Christophe PLAGNIOL-VILLARD 	u32 csr;
309f2302d44SStefan Roese 	unsigned long long timeval = get_ticks();
31092477a63STimur Tabi 	const unsigned long long timeout = usec2ticks(CONFIG_I2C_TIMEOUT);
311080c646dSJean-Christophe PLAGNIOL-VILLARD 
312080c646dSJean-Christophe PLAGNIOL-VILLARD 	do {
313ec2c81c5Smario.six@gdsys.cc 		csr = readb(&base->sr);
314080c646dSJean-Christophe PLAGNIOL-VILLARD 		if (!(csr & I2C_SR_MIF))
315080c646dSJean-Christophe PLAGNIOL-VILLARD 			continue;
31621f4cbb7SJoakim Tjernlund 		/* Read again to allow register to stabilise */
317ec2c81c5Smario.six@gdsys.cc 		csr = readb(&base->sr);
318080c646dSJean-Christophe PLAGNIOL-VILLARD 
319ec2c81c5Smario.six@gdsys.cc 		writeb(0x0, &base->sr);
320080c646dSJean-Christophe PLAGNIOL-VILLARD 
321080c646dSJean-Christophe PLAGNIOL-VILLARD 		if (csr & I2C_SR_MAL) {
322a059de11SMario Six 			debug("%s: MAL\n", __func__);
323080c646dSJean-Christophe PLAGNIOL-VILLARD 			return -1;
324080c646dSJean-Christophe PLAGNIOL-VILLARD 		}
325080c646dSJean-Christophe PLAGNIOL-VILLARD 
326080c646dSJean-Christophe PLAGNIOL-VILLARD 		if (!(csr & I2C_SR_MCF))	{
327a059de11SMario Six 			debug("%s: unfinished\n", __func__);
328080c646dSJean-Christophe PLAGNIOL-VILLARD 			return -1;
329080c646dSJean-Christophe PLAGNIOL-VILLARD 		}
330080c646dSJean-Christophe PLAGNIOL-VILLARD 
331080c646dSJean-Christophe PLAGNIOL-VILLARD 		if (write == I2C_WRITE_BIT && (csr & I2C_SR_RXAK)) {
332a059de11SMario Six 			debug("%s: No RXACK\n", __func__);
333080c646dSJean-Christophe PLAGNIOL-VILLARD 			return -1;
334080c646dSJean-Christophe PLAGNIOL-VILLARD 		}
335080c646dSJean-Christophe PLAGNIOL-VILLARD 
336080c646dSJean-Christophe PLAGNIOL-VILLARD 		return 0;
33792477a63STimur Tabi 	} while ((get_ticks() - timeval) < timeout);
338080c646dSJean-Christophe PLAGNIOL-VILLARD 
339a059de11SMario Six 	debug("%s: timed out\n", __func__);
340080c646dSJean-Christophe PLAGNIOL-VILLARD 	return -1;
341080c646dSJean-Christophe PLAGNIOL-VILLARD }
342080c646dSJean-Christophe PLAGNIOL-VILLARD 
i2c_write_addr(const struct fsl_i2c_base * base,u8 dev,u8 dir,int rsta)343d4f422f8SMario Six static int i2c_write_addr(const struct fsl_i2c_base *base, u8 dev,
344a059de11SMario Six 			  u8 dir, int rsta)
345080c646dSJean-Christophe PLAGNIOL-VILLARD {
346080c646dSJean-Christophe PLAGNIOL-VILLARD 	writeb(I2C_CR_MEN | I2C_CR_MSTA | I2C_CR_MTX
347080c646dSJean-Christophe PLAGNIOL-VILLARD 	       | (rsta ? I2C_CR_RSTA : 0),
348ec2c81c5Smario.six@gdsys.cc 	       &base->cr);
349080c646dSJean-Christophe PLAGNIOL-VILLARD 
350ec2c81c5Smario.six@gdsys.cc 	writeb((dev << 1) | dir, &base->dr);
351080c646dSJean-Christophe PLAGNIOL-VILLARD 
352ecf591e3Smario.six@gdsys.cc 	if (i2c_wait(base, I2C_WRITE_BIT) < 0)
353080c646dSJean-Christophe PLAGNIOL-VILLARD 		return 0;
354080c646dSJean-Christophe PLAGNIOL-VILLARD 
355080c646dSJean-Christophe PLAGNIOL-VILLARD 	return 1;
356080c646dSJean-Christophe PLAGNIOL-VILLARD }
357080c646dSJean-Christophe PLAGNIOL-VILLARD 
__i2c_write_data(const struct fsl_i2c_base * base,u8 * data,int length)358d4f422f8SMario Six static int __i2c_write_data(const struct fsl_i2c_base *base, u8 *data,
359a059de11SMario Six 			    int length)
360080c646dSJean-Christophe PLAGNIOL-VILLARD {
361080c646dSJean-Christophe PLAGNIOL-VILLARD 	int i;
362080c646dSJean-Christophe PLAGNIOL-VILLARD 
363080c646dSJean-Christophe PLAGNIOL-VILLARD 	for (i = 0; i < length; i++) {
364ec2c81c5Smario.six@gdsys.cc 		writeb(data[i], &base->dr);
365080c646dSJean-Christophe PLAGNIOL-VILLARD 
366ecf591e3Smario.six@gdsys.cc 		if (i2c_wait(base, I2C_WRITE_BIT) < 0)
367080c646dSJean-Christophe PLAGNIOL-VILLARD 			break;
368080c646dSJean-Christophe PLAGNIOL-VILLARD 	}
369080c646dSJean-Christophe PLAGNIOL-VILLARD 
370080c646dSJean-Christophe PLAGNIOL-VILLARD 	return i;
371080c646dSJean-Christophe PLAGNIOL-VILLARD }
372080c646dSJean-Christophe PLAGNIOL-VILLARD 
__i2c_read_data(const struct fsl_i2c_base * base,u8 * data,int length)373d4f422f8SMario Six static int __i2c_read_data(const struct fsl_i2c_base *base, u8 *data,
374a059de11SMario Six 			   int length)
375080c646dSJean-Christophe PLAGNIOL-VILLARD {
376080c646dSJean-Christophe PLAGNIOL-VILLARD 	int i;
377080c646dSJean-Christophe PLAGNIOL-VILLARD 
378080c646dSJean-Christophe PLAGNIOL-VILLARD 	writeb(I2C_CR_MEN | I2C_CR_MSTA | ((length == 1) ? I2C_CR_TXAK : 0),
379ec2c81c5Smario.six@gdsys.cc 	       &base->cr);
380080c646dSJean-Christophe PLAGNIOL-VILLARD 
381080c646dSJean-Christophe PLAGNIOL-VILLARD 	/* dummy read */
382ec2c81c5Smario.six@gdsys.cc 	readb(&base->dr);
383080c646dSJean-Christophe PLAGNIOL-VILLARD 
384080c646dSJean-Christophe PLAGNIOL-VILLARD 	for (i = 0; i < length; i++) {
385ecf591e3Smario.six@gdsys.cc 		if (i2c_wait(base, I2C_READ_BIT) < 0)
386080c646dSJean-Christophe PLAGNIOL-VILLARD 			break;
387080c646dSJean-Christophe PLAGNIOL-VILLARD 
388080c646dSJean-Christophe PLAGNIOL-VILLARD 		/* Generate ack on last next to last byte */
389080c646dSJean-Christophe PLAGNIOL-VILLARD 		if (i == length - 2)
390080c646dSJean-Christophe PLAGNIOL-VILLARD 			writeb(I2C_CR_MEN | I2C_CR_MSTA | I2C_CR_TXAK,
391ec2c81c5Smario.six@gdsys.cc 			       &base->cr);
392080c646dSJean-Christophe PLAGNIOL-VILLARD 
393d1c9e5b3SJoakim Tjernlund 		/* Do not generate stop on last byte */
394080c646dSJean-Christophe PLAGNIOL-VILLARD 		if (i == length - 1)
395d1c9e5b3SJoakim Tjernlund 			writeb(I2C_CR_MEN | I2C_CR_MSTA | I2C_CR_MTX,
396ec2c81c5Smario.six@gdsys.cc 			       &base->cr);
397080c646dSJean-Christophe PLAGNIOL-VILLARD 
398ec2c81c5Smario.six@gdsys.cc 		data[i] = readb(&base->dr);
399080c646dSJean-Christophe PLAGNIOL-VILLARD 	}
400080c646dSJean-Christophe PLAGNIOL-VILLARD 
401080c646dSJean-Christophe PLAGNIOL-VILLARD 	return i;
402080c646dSJean-Christophe PLAGNIOL-VILLARD }
403080c646dSJean-Christophe PLAGNIOL-VILLARD 
__i2c_read(const struct fsl_i2c_base * base,u8 chip_addr,u8 * offset,int olen,u8 * data,int dlen)404a059de11SMario Six static int __i2c_read(const struct fsl_i2c_base *base, u8 chip_addr, u8 *offset,
405a059de11SMario Six 		      int olen, u8 *data, int dlen)
406080c646dSJean-Christophe PLAGNIOL-VILLARD {
4072b21e960Smario.six@gdsys.cc 	int ret = -1; /* signal error */
408080c646dSJean-Christophe PLAGNIOL-VILLARD 
409ecf591e3Smario.six@gdsys.cc 	if (i2c_wait4bus(base) < 0)
410b778c1b5SReinhard Pfau 		return -1;
411b778c1b5SReinhard Pfau 
412386b2769Smario.six@gdsys.cc 	/* Some drivers use offset lengths in excess of 4 bytes. These drivers
413386b2769Smario.six@gdsys.cc 	 * adhere to the following convention:
414386b2769Smario.six@gdsys.cc 	 * - the offset length is passed as negative (that is, the absolute
415386b2769Smario.six@gdsys.cc 	 *   value of olen is the actual offset length)
416386b2769Smario.six@gdsys.cc 	 * - the offset itself is passed in data, which is overwritten by the
417386b2769Smario.six@gdsys.cc 	 *   subsequent read operation
418a405764cSShaveta Leekha 	 */
4192b21e960Smario.six@gdsys.cc 	if (olen < 0) {
420ecf591e3Smario.six@gdsys.cc 		if (i2c_write_addr(base, chip_addr, I2C_WRITE_BIT, 0) != 0)
421ecf591e3Smario.six@gdsys.cc 			ret = __i2c_write_data(base, data, -olen);
422a405764cSShaveta Leekha 
42303a112aaSmario.six@gdsys.cc 		if (ret != -olen)
424a405764cSShaveta Leekha 			return -1;
425a405764cSShaveta Leekha 
426ecf591e3Smario.six@gdsys.cc 		if (dlen && i2c_write_addr(base, chip_addr,
4272b21e960Smario.six@gdsys.cc 					   I2C_READ_BIT, 1) != 0)
428ecf591e3Smario.six@gdsys.cc 			ret = __i2c_read_data(base, data, dlen);
429a405764cSShaveta Leekha 	} else {
4302b21e960Smario.six@gdsys.cc 		if ((!dlen || olen > 0) &&
431ecf591e3Smario.six@gdsys.cc 		    i2c_write_addr(base, chip_addr, I2C_WRITE_BIT, 0) != 0  &&
432ecf591e3Smario.six@gdsys.cc 		    __i2c_write_data(base, offset, olen) == olen)
4332b21e960Smario.six@gdsys.cc 			ret = 0; /* No error so far */
434080c646dSJean-Christophe PLAGNIOL-VILLARD 
435ecf591e3Smario.six@gdsys.cc 		if (dlen && i2c_write_addr(base, chip_addr, I2C_READ_BIT,
4362b21e960Smario.six@gdsys.cc 					   olen ? 1 : 0) != 0)
437ecf591e3Smario.six@gdsys.cc 			ret = __i2c_read_data(base, data, dlen);
438a405764cSShaveta Leekha 	}
439080c646dSJean-Christophe PLAGNIOL-VILLARD 
440ec2c81c5Smario.six@gdsys.cc 	writeb(I2C_CR_MEN, &base->cr);
441080c646dSJean-Christophe PLAGNIOL-VILLARD 
442ecf591e3Smario.six@gdsys.cc 	if (i2c_wait4bus(base)) /* Wait until STOP */
443d1c9e5b3SJoakim Tjernlund 		debug("i2c_read: wait4bus timed out\n");
444d1c9e5b3SJoakim Tjernlund 
4452b21e960Smario.six@gdsys.cc 	if (ret == dlen)
446080c646dSJean-Christophe PLAGNIOL-VILLARD 		return 0;
447080c646dSJean-Christophe PLAGNIOL-VILLARD 
448080c646dSJean-Christophe PLAGNIOL-VILLARD 	return -1;
449080c646dSJean-Christophe PLAGNIOL-VILLARD }
450080c646dSJean-Christophe PLAGNIOL-VILLARD 
__i2c_write(const struct fsl_i2c_base * base,u8 chip_addr,u8 * offset,int olen,u8 * data,int dlen)451a059de11SMario Six static int __i2c_write(const struct fsl_i2c_base *base, u8 chip_addr,
452a059de11SMario Six 		       u8 *offset, int olen, u8 *data, int dlen)
453080c646dSJean-Christophe PLAGNIOL-VILLARD {
4542b21e960Smario.six@gdsys.cc 	int ret = -1; /* signal error */
455080c646dSJean-Christophe PLAGNIOL-VILLARD 
456ecf591e3Smario.six@gdsys.cc 	if (i2c_wait4bus(base) < 0)
457b8ce3343SChunhe Lan 		return -1;
458b8ce3343SChunhe Lan 
459ecf591e3Smario.six@gdsys.cc 	if (i2c_write_addr(base, chip_addr, I2C_WRITE_BIT, 0) != 0 &&
460ecf591e3Smario.six@gdsys.cc 	    __i2c_write_data(base, offset, olen) == olen) {
461ecf591e3Smario.six@gdsys.cc 		ret = __i2c_write_data(base, data, dlen);
462080c646dSJean-Christophe PLAGNIOL-VILLARD 	}
463080c646dSJean-Christophe PLAGNIOL-VILLARD 
464ec2c81c5Smario.six@gdsys.cc 	writeb(I2C_CR_MEN, &base->cr);
465ecf591e3Smario.six@gdsys.cc 	if (i2c_wait4bus(base)) /* Wait until STOP */
46621f4cbb7SJoakim Tjernlund 		debug("i2c_write: wait4bus timed out\n");
467080c646dSJean-Christophe PLAGNIOL-VILLARD 
4682b21e960Smario.six@gdsys.cc 	if (ret == dlen)
469080c646dSJean-Christophe PLAGNIOL-VILLARD 		return 0;
470080c646dSJean-Christophe PLAGNIOL-VILLARD 
471080c646dSJean-Christophe PLAGNIOL-VILLARD 	return -1;
472080c646dSJean-Christophe PLAGNIOL-VILLARD }
473080c646dSJean-Christophe PLAGNIOL-VILLARD 
__i2c_probe_chip(const struct fsl_i2c_base * base,uchar chip)474a059de11SMario Six static int __i2c_probe_chip(const struct fsl_i2c_base *base, uchar chip)
475080c646dSJean-Christophe PLAGNIOL-VILLARD {
476a059de11SMario Six 	/* For unknown reason the controller will ACK when
477080c646dSJean-Christophe PLAGNIOL-VILLARD 	 * probing for a slave with the same address, so skip
478080c646dSJean-Christophe PLAGNIOL-VILLARD 	 * it.
479080c646dSJean-Christophe PLAGNIOL-VILLARD 	 */
480ec2c81c5Smario.six@gdsys.cc 	if (chip == (readb(&base->adr) >> 1))
481080c646dSJean-Christophe PLAGNIOL-VILLARD 		return -1;
482080c646dSJean-Christophe PLAGNIOL-VILLARD 
483ecf591e3Smario.six@gdsys.cc 	return __i2c_read(base, chip, 0, 0, NULL, 0);
484080c646dSJean-Christophe PLAGNIOL-VILLARD }
485080c646dSJean-Christophe PLAGNIOL-VILLARD 
__i2c_set_bus_speed(const struct fsl_i2c_base * base,uint speed,int i2c_clk)486a059de11SMario Six static uint __i2c_set_bus_speed(const struct fsl_i2c_base *base,
487a059de11SMario Six 				uint speed, int i2c_clk)
488080c646dSJean-Christophe PLAGNIOL-VILLARD {
489ec2c81c5Smario.six@gdsys.cc 	writeb(0, &base->cr);		/* stop controller */
490ecf591e3Smario.six@gdsys.cc 	set_i2c_bus_speed(base, i2c_clk, speed);
491ec2c81c5Smario.six@gdsys.cc 	writeb(I2C_CR_MEN, &base->cr);	/* start controller */
492d8c82db4STimur Tabi 
493d8c82db4STimur Tabi 	return 0;
494080c646dSJean-Christophe PLAGNIOL-VILLARD }
495080c646dSJean-Christophe PLAGNIOL-VILLARD 
496dbc82ce3Smario.six@gdsys.cc #ifndef CONFIG_DM_I2C
fsl_i2c_init(struct i2c_adapter * adap,int speed,int slaveadd)497ad7e657cSmario.six@gdsys.cc static void fsl_i2c_init(struct i2c_adapter *adap, int speed, int slaveadd)
498ad7e657cSmario.six@gdsys.cc {
499ecf591e3Smario.six@gdsys.cc 	__i2c_init(i2c_base[adap->hwadapnr], speed, slaveadd,
500ecf591e3Smario.six@gdsys.cc 		   get_i2c_clock(adap->hwadapnr), adap->hwadapnr);
501ad7e657cSmario.six@gdsys.cc }
502ad7e657cSmario.six@gdsys.cc 
fsl_i2c_probe_chip(struct i2c_adapter * adap,uchar chip)503a059de11SMario Six static int fsl_i2c_probe_chip(struct i2c_adapter *adap, uchar chip)
504ad7e657cSmario.six@gdsys.cc {
505ecf591e3Smario.six@gdsys.cc 	return __i2c_probe_chip(i2c_base[adap->hwadapnr], chip);
506ad7e657cSmario.six@gdsys.cc }
507ad7e657cSmario.six@gdsys.cc 
fsl_i2c_read(struct i2c_adapter * adap,u8 chip_addr,uint offset,int olen,u8 * data,int dlen)508a059de11SMario Six static int fsl_i2c_read(struct i2c_adapter *adap, u8 chip_addr, uint offset,
509a059de11SMario Six 			int olen, u8 *data, int dlen)
510ad7e657cSmario.six@gdsys.cc {
511ecf591e3Smario.six@gdsys.cc 	u8 *o = (u8 *)&offset;
512a059de11SMario Six 
513ecf591e3Smario.six@gdsys.cc 	return __i2c_read(i2c_base[adap->hwadapnr], chip_addr, &o[4 - olen],
514ecf591e3Smario.six@gdsys.cc 			  olen, data, dlen);
515ad7e657cSmario.six@gdsys.cc }
516ad7e657cSmario.six@gdsys.cc 
fsl_i2c_write(struct i2c_adapter * adap,u8 chip_addr,uint offset,int olen,u8 * data,int dlen)517a059de11SMario Six static int fsl_i2c_write(struct i2c_adapter *adap, u8 chip_addr, uint offset,
518a059de11SMario Six 			 int olen, u8 *data, int dlen)
519ad7e657cSmario.six@gdsys.cc {
520ecf591e3Smario.six@gdsys.cc 	u8 *o = (u8 *)&offset;
521a059de11SMario Six 
522ecf591e3Smario.six@gdsys.cc 	return __i2c_write(i2c_base[adap->hwadapnr], chip_addr, &o[4 - olen],
523ecf591e3Smario.six@gdsys.cc 			   olen, data, dlen);
524ad7e657cSmario.six@gdsys.cc }
525ad7e657cSmario.six@gdsys.cc 
fsl_i2c_set_bus_speed(struct i2c_adapter * adap,uint speed)526a059de11SMario Six static uint fsl_i2c_set_bus_speed(struct i2c_adapter *adap, uint speed)
527ad7e657cSmario.six@gdsys.cc {
528ecf591e3Smario.six@gdsys.cc 	return __i2c_set_bus_speed(i2c_base[adap->hwadapnr], speed,
529ecf591e3Smario.six@gdsys.cc 				   get_i2c_clock(adap->hwadapnr));
530ad7e657cSmario.six@gdsys.cc }
531ad7e657cSmario.six@gdsys.cc 
53200f792e0SHeiko Schocher /*
53300f792e0SHeiko Schocher  * Register fsl i2c adapters
53400f792e0SHeiko Schocher  */
53516579ecbSmario.six@gdsys.cc U_BOOT_I2C_ADAP_COMPLETE(fsl_0, fsl_i2c_init, fsl_i2c_probe_chip, fsl_i2c_read,
53600f792e0SHeiko Schocher 			 fsl_i2c_write, fsl_i2c_set_bus_speed,
53700f792e0SHeiko Schocher 			 CONFIG_SYS_FSL_I2C_SPEED, CONFIG_SYS_FSL_I2C_SLAVE,
53800f792e0SHeiko Schocher 			 0)
53900f792e0SHeiko Schocher #ifdef CONFIG_SYS_FSL_I2C2_OFFSET
54016579ecbSmario.six@gdsys.cc U_BOOT_I2C_ADAP_COMPLETE(fsl_1, fsl_i2c_init, fsl_i2c_probe_chip, fsl_i2c_read,
54100f792e0SHeiko Schocher 			 fsl_i2c_write, fsl_i2c_set_bus_speed,
54200f792e0SHeiko Schocher 			 CONFIG_SYS_FSL_I2C2_SPEED, CONFIG_SYS_FSL_I2C2_SLAVE,
54300f792e0SHeiko Schocher 			 1)
544c1bce4ffSHeiko Schocher #endif
545a17fd10fSShengzhou Liu #ifdef CONFIG_SYS_FSL_I2C3_OFFSET
54616579ecbSmario.six@gdsys.cc U_BOOT_I2C_ADAP_COMPLETE(fsl_2, fsl_i2c_init, fsl_i2c_probe_chip, fsl_i2c_read,
547a17fd10fSShengzhou Liu 			 fsl_i2c_write, fsl_i2c_set_bus_speed,
548a17fd10fSShengzhou Liu 			 CONFIG_SYS_FSL_I2C3_SPEED, CONFIG_SYS_FSL_I2C3_SLAVE,
549a17fd10fSShengzhou Liu 			 2)
550a17fd10fSShengzhou Liu #endif
551a17fd10fSShengzhou Liu #ifdef CONFIG_SYS_FSL_I2C4_OFFSET
55216579ecbSmario.six@gdsys.cc U_BOOT_I2C_ADAP_COMPLETE(fsl_3, fsl_i2c_init, fsl_i2c_probe_chip, fsl_i2c_read,
553a17fd10fSShengzhou Liu 			 fsl_i2c_write, fsl_i2c_set_bus_speed,
554a17fd10fSShengzhou Liu 			 CONFIG_SYS_FSL_I2C4_SPEED, CONFIG_SYS_FSL_I2C4_SLAVE,
555a17fd10fSShengzhou Liu 			 3)
556a17fd10fSShengzhou Liu #endif
557dbc82ce3Smario.six@gdsys.cc #else /* CONFIG_DM_I2C */
558dbc82ce3Smario.six@gdsys.cc static int fsl_i2c_probe_chip(struct udevice *bus, u32 chip_addr,
559dbc82ce3Smario.six@gdsys.cc 			      u32 chip_flags)
560dbc82ce3Smario.six@gdsys.cc {
561dbc82ce3Smario.six@gdsys.cc 	struct fsl_i2c_dev *dev = dev_get_priv(bus);
562a059de11SMario Six 
563dbc82ce3Smario.six@gdsys.cc 	return __i2c_probe_chip(dev->base, chip_addr);
564dbc82ce3Smario.six@gdsys.cc }
565dbc82ce3Smario.six@gdsys.cc 
566a059de11SMario Six static int fsl_i2c_set_bus_speed(struct udevice *bus, uint speed)
567dbc82ce3Smario.six@gdsys.cc {
568dbc82ce3Smario.six@gdsys.cc 	struct fsl_i2c_dev *dev = dev_get_priv(bus);
569a059de11SMario Six 
570dbc82ce3Smario.six@gdsys.cc 	return __i2c_set_bus_speed(dev->base, speed, dev->i2c_clk);
571dbc82ce3Smario.six@gdsys.cc }
572dbc82ce3Smario.six@gdsys.cc 
573dbc82ce3Smario.six@gdsys.cc static int fsl_i2c_ofdata_to_platdata(struct udevice *bus)
574dbc82ce3Smario.six@gdsys.cc {
575dbc82ce3Smario.six@gdsys.cc 	struct fsl_i2c_dev *dev = dev_get_priv(bus);
576e5c762f5SMario Six 	struct clk clock;
577dbc82ce3Smario.six@gdsys.cc 
578d934832dSMario Six 	dev->base = map_sysmem(dev_read_addr(bus), sizeof(struct fsl_i2c_base));
579dbc82ce3Smario.six@gdsys.cc 
580dbc82ce3Smario.six@gdsys.cc 	if (!dev->base)
581dbc82ce3Smario.six@gdsys.cc 		return -ENOMEM;
582dbc82ce3Smario.six@gdsys.cc 
58384a4d34eSMario Six 	dev->index = dev_read_u32_default(bus, "cell-index", -1);
58484a4d34eSMario Six 	dev->slaveadd = dev_read_u32_default(bus, "u-boot,i2c-slave-addr",
58584a4d34eSMario Six 					     0x7f);
58684a4d34eSMario Six 	dev->speed = dev_read_u32_default(bus, "clock-frequency", 400000);
587dbc82ce3Smario.six@gdsys.cc 
588e5c762f5SMario Six 	if (!clk_get_by_index(bus, 0, &clock))
589e5c762f5SMario Six 		dev->i2c_clk = clk_get_rate(&clock);
590e5c762f5SMario Six 	else
591e5c762f5SMario Six 		dev->i2c_clk = dev->index ? gd->arch.i2c2_clk :
592e5c762f5SMario Six 					    gd->arch.i2c1_clk;
593dbc82ce3Smario.six@gdsys.cc 
594dbc82ce3Smario.six@gdsys.cc 	return 0;
595dbc82ce3Smario.six@gdsys.cc }
596dbc82ce3Smario.six@gdsys.cc 
597dbc82ce3Smario.six@gdsys.cc static int fsl_i2c_probe(struct udevice *bus)
598dbc82ce3Smario.six@gdsys.cc {
599dbc82ce3Smario.six@gdsys.cc 	struct fsl_i2c_dev *dev = dev_get_priv(bus);
600a059de11SMario Six 
601dbc82ce3Smario.six@gdsys.cc 	__i2c_init(dev->base, dev->speed, dev->slaveadd, dev->i2c_clk,
602dbc82ce3Smario.six@gdsys.cc 		   dev->index);
603dbc82ce3Smario.six@gdsys.cc 	return 0;
604dbc82ce3Smario.six@gdsys.cc }
605dbc82ce3Smario.six@gdsys.cc 
606dbc82ce3Smario.six@gdsys.cc static int fsl_i2c_xfer(struct udevice *bus, struct i2c_msg *msg, int nmsgs)
607dbc82ce3Smario.six@gdsys.cc {
608dbc82ce3Smario.six@gdsys.cc 	struct fsl_i2c_dev *dev = dev_get_priv(bus);
609dbc82ce3Smario.six@gdsys.cc 	struct i2c_msg *dmsg, *omsg, dummy;
610dbc82ce3Smario.six@gdsys.cc 
611dbc82ce3Smario.six@gdsys.cc 	memset(&dummy, 0, sizeof(struct i2c_msg));
612dbc82ce3Smario.six@gdsys.cc 
613dbc82ce3Smario.six@gdsys.cc 	/* We expect either two messages (one with an offset and one with the
614a059de11SMario Six 	 * actual data) or one message (just data)
615a059de11SMario Six 	 */
616dbc82ce3Smario.six@gdsys.cc 	if (nmsgs > 2 || nmsgs == 0) {
617dbc82ce3Smario.six@gdsys.cc 		debug("%s: Only one or two messages are supported.", __func__);
618dbc82ce3Smario.six@gdsys.cc 		return -1;
619dbc82ce3Smario.six@gdsys.cc 	}
620dbc82ce3Smario.six@gdsys.cc 
621dbc82ce3Smario.six@gdsys.cc 	omsg = nmsgs == 1 ? &dummy : msg;
622dbc82ce3Smario.six@gdsys.cc 	dmsg = nmsgs == 1 ? msg : msg + 1;
623dbc82ce3Smario.six@gdsys.cc 
624dbc82ce3Smario.six@gdsys.cc 	if (dmsg->flags & I2C_M_RD)
625dbc82ce3Smario.six@gdsys.cc 		return __i2c_read(dev->base, dmsg->addr, omsg->buf, omsg->len,
626dbc82ce3Smario.six@gdsys.cc 				  dmsg->buf, dmsg->len);
627dbc82ce3Smario.six@gdsys.cc 	else
628dbc82ce3Smario.six@gdsys.cc 		return __i2c_write(dev->base, dmsg->addr, omsg->buf, omsg->len,
629dbc82ce3Smario.six@gdsys.cc 				   dmsg->buf, dmsg->len);
630dbc82ce3Smario.six@gdsys.cc }
631dbc82ce3Smario.six@gdsys.cc 
632dbc82ce3Smario.six@gdsys.cc static const struct dm_i2c_ops fsl_i2c_ops = {
633dbc82ce3Smario.six@gdsys.cc 	.xfer           = fsl_i2c_xfer,
634dbc82ce3Smario.six@gdsys.cc 	.probe_chip     = fsl_i2c_probe_chip,
635dbc82ce3Smario.six@gdsys.cc 	.set_bus_speed  = fsl_i2c_set_bus_speed,
636dbc82ce3Smario.six@gdsys.cc };
637dbc82ce3Smario.six@gdsys.cc 
638dbc82ce3Smario.six@gdsys.cc static const struct udevice_id fsl_i2c_ids[] = {
639dbc82ce3Smario.six@gdsys.cc 	{ .compatible = "fsl-i2c", },
640dbc82ce3Smario.six@gdsys.cc 	{ /* sentinel */ }
641dbc82ce3Smario.six@gdsys.cc };
642dbc82ce3Smario.six@gdsys.cc 
643dbc82ce3Smario.six@gdsys.cc U_BOOT_DRIVER(i2c_fsl) = {
644dbc82ce3Smario.six@gdsys.cc 	.name = "i2c_fsl",
645dbc82ce3Smario.six@gdsys.cc 	.id = UCLASS_I2C,
646dbc82ce3Smario.six@gdsys.cc 	.of_match = fsl_i2c_ids,
647dbc82ce3Smario.six@gdsys.cc 	.probe = fsl_i2c_probe,
648dbc82ce3Smario.six@gdsys.cc 	.ofdata_to_platdata = fsl_i2c_ofdata_to_platdata,
649dbc82ce3Smario.six@gdsys.cc 	.priv_auto_alloc_size = sizeof(struct fsl_i2c_dev),
650dbc82ce3Smario.six@gdsys.cc 	.ops = &fsl_i2c_ops,
651dbc82ce3Smario.six@gdsys.cc };
652dbc82ce3Smario.six@gdsys.cc 
653dbc82ce3Smario.six@gdsys.cc #endif /* CONFIG_DM_I2C */
654