blob: b0e65fcf65625f3539bcdcdcfcde8c9fe9553614 [file] [log] [blame]
Jon Loeliger7237c032006-10-19 11:02:16 -05001/*
Timur Tabi92477a62009-09-04 16:28:35 -05002 * Copyright 2006,2009 Freescale Semiconductor, Inc.
Jon Loeliger7237c032006-10-19 11:02:16 -05003 *
Heiko Schocher00f792e2012-10-24 13:48:22 +02004 * 2012, Heiko Schocher, DENX Software Engineering, hs@denx.de.
5 * Changes for multibus/multiadapter I2C support.
6 *
Tom Rini5b8031c2016-01-14 22:05:13 -05007 * SPDX-License-Identifier: GPL-2.0
Jon Loeliger7237c032006-10-19 11:02:16 -05008 */
9
Jon Loeliger7237c032006-10-19 11:02:16 -050010#include <common.h>
Jon Loeliger4d45f692006-10-19 12:02:24 -050011#include <command.h>
Jon Loeliger20476722006-10-20 15:50:15 -050012#include <i2c.h> /* Functional interface */
Jon Loeliger7237c032006-10-19 11:02:16 -050013#include <asm/io.h>
Jon Loeliger20476722006-10-20 15:50:15 -050014#include <asm/fsl_i2c.h> /* HW definitions */
Jon Loeliger7237c032006-10-19 11:02:16 -050015
Timur Tabi92477a62009-09-04 16:28:35 -050016/* The maximum number of microseconds we will wait until another master has
17 * released the bus. If not defined in the board header file, then use a
18 * generic value.
19 */
20#ifndef CONFIG_I2C_MBB_TIMEOUT
21#define CONFIG_I2C_MBB_TIMEOUT 100000
22#endif
23
24/* The maximum number of microseconds we will wait for a read or write
25 * operation to complete. If not defined in the board header file, then use a
26 * generic value.
27 */
28#ifndef CONFIG_I2C_TIMEOUT
Shaveta Leekha6dd38cc2014-11-03 10:43:14 +053029#define CONFIG_I2C_TIMEOUT 100000
Timur Tabi92477a62009-09-04 16:28:35 -050030#endif
Jon Loeliger7237c032006-10-19 11:02:16 -050031
Joakim Tjernlund1939d962006-11-28 16:17:27 -060032#define I2C_READ_BIT 1
33#define I2C_WRITE_BIT 0
34
Timur Tabid8c82db2008-03-14 17:45:29 -050035DECLARE_GLOBAL_DATA_PTR;
36
mario.six@gdsys.ccec2c81c2016-04-25 08:31:01 +020037static const struct fsl_i2c_base *i2c_base[4] = {
38 (struct fsl_i2c_base *)(CONFIG_SYS_IMMR + CONFIG_SYS_FSL_I2C_OFFSET),
Heiko Schocher00f792e2012-10-24 13:48:22 +020039#ifdef CONFIG_SYS_FSL_I2C2_OFFSET
mario.six@gdsys.ccec2c81c2016-04-25 08:31:01 +020040 (struct fsl_i2c_base *)(CONFIG_SYS_IMMR + CONFIG_SYS_FSL_I2C2_OFFSET),
Shengzhou Liua17fd102014-07-07 12:17:48 +080041#endif
42#ifdef CONFIG_SYS_FSL_I2C3_OFFSET
mario.six@gdsys.ccec2c81c2016-04-25 08:31:01 +020043 (struct fsl_i2c_base *)(CONFIG_SYS_IMMR + CONFIG_SYS_FSL_I2C3_OFFSET),
Shengzhou Liua17fd102014-07-07 12:17:48 +080044#endif
45#ifdef CONFIG_SYS_FSL_I2C4_OFFSET
mario.six@gdsys.ccec2c81c2016-04-25 08:31:01 +020046 (struct fsl_i2c_base *)(CONFIG_SYS_IMMR + CONFIG_SYS_FSL_I2C4_OFFSET)
Timur Tabibe5e6182006-11-03 19:15:00 -060047#endif
48};
Jon Loeliger7237c032006-10-19 11:02:16 -050049
Timur Tabid8c82db2008-03-14 17:45:29 -050050/* I2C speed map for a DFSR value of 1 */
51
52/*
53 * Map I2C frequency dividers to FDR and DFSR values
54 *
55 * This structure is used to define the elements of a table that maps I2C
56 * frequency divider (I2C clock rate divided by I2C bus speed) to a value to be
57 * programmed into the Frequency Divider Ratio (FDR) and Digital Filter
58 * Sampling Rate (DFSR) registers.
59 *
60 * The actual table should be defined in the board file, and it must be called
61 * fsl_i2c_speed_map[].
62 *
63 * The last entry of the table must have a value of {-1, X}, where X is same
64 * FDR/DFSR values as the second-to-last entry. This guarantees that any
65 * search through the array will always find a match.
66 *
67 * The values of the divider must be in increasing numerical order, i.e.
68 * fsl_i2c_speed_map[x+1].divider > fsl_i2c_speed_map[x].divider.
69 *
70 * For this table, the values are based on a value of 1 for the DFSR
71 * register. See the application note AN2919 "Determining the I2C Frequency
72 * Divider Ratio for SCL"
TsiChung Liew5d9a5ef2008-08-19 00:56:46 +060073 *
74 * ColdFire I2C frequency dividers for FDR values are different from
75 * PowerPC. The protocol to use the I2C module is still the same.
76 * A different table is defined and are based on MCF5xxx user manual.
77 *
Timur Tabid8c82db2008-03-14 17:45:29 -050078 */
79static const struct {
80 unsigned short divider;
Timur Tabid8c82db2008-03-14 17:45:29 -050081 u8 fdr;
82} fsl_i2c_speed_map[] = {
Joakim Tjernlund99404202009-09-17 11:07:17 +020083#ifdef __M68K__
TsiChung Liew5d9a5ef2008-08-19 00:56:46 +060084 {20, 32}, {22, 33}, {24, 34}, {26, 35},
85 {28, 0}, {28, 36}, {30, 1}, {32, 37},
86 {34, 2}, {36, 38}, {40, 3}, {40, 39},
87 {44, 4}, {48, 5}, {48, 40}, {56, 6},
88 {56, 41}, {64, 42}, {68, 7}, {72, 43},
89 {80, 8}, {80, 44}, {88, 9}, {96, 41},
90 {104, 10}, {112, 42}, {128, 11}, {128, 43},
91 {144, 12}, {160, 13}, {160, 48}, {192, 14},
92 {192, 49}, {224, 50}, {240, 15}, {256, 51},
93 {288, 16}, {320, 17}, {320, 52}, {384, 18},
94 {384, 53}, {448, 54}, {480, 19}, {512, 55},
95 {576, 20}, {640, 21}, {640, 56}, {768, 22},
96 {768, 57}, {960, 23}, {896, 58}, {1024, 59},
97 {1152, 24}, {1280, 25}, {1280, 60}, {1536, 26},
98 {1536, 61}, {1792, 62}, {1920, 27}, {2048, 63},
99 {2304, 28}, {2560, 29}, {3072, 30}, {3840, 31},
100 {-1, 31}
101#endif
Timur Tabid8c82db2008-03-14 17:45:29 -0500102};
103
104/**
105 * Set the I2C bus speed for a given I2C device
106 *
mario.six@gdsys.ccec2c81c2016-04-25 08:31:01 +0200107 * @param base: the I2C device registers
Timur Tabid8c82db2008-03-14 17:45:29 -0500108 * @i2c_clk: I2C bus clock frequency
109 * @speed: the desired speed of the bus
110 *
111 * The I2C device must be stopped before calling this function.
112 *
113 * The return value is the actual bus speed that is set.
114 */
mario.six@gdsys.ccec2c81c2016-04-25 08:31:01 +0200115static unsigned int set_i2c_bus_speed(const struct fsl_i2c_base *base,
Timur Tabid8c82db2008-03-14 17:45:29 -0500116 unsigned int i2c_clk, unsigned int speed)
117{
Masahiro Yamadab4141192014-11-07 03:03:31 +0900118 unsigned short divider = min(i2c_clk / speed, (unsigned int)USHRT_MAX);
Timur Tabid8c82db2008-03-14 17:45:29 -0500119
120 /*
121 * We want to choose an FDR/DFSR that generates an I2C bus speed that
122 * is equal to or lower than the requested speed. That means that we
123 * want the first divider that is equal to or greater than the
124 * calculated divider.
125 */
Joakim Tjernlund99404202009-09-17 11:07:17 +0200126#ifdef __PPC__
127 u8 dfsr, fdr = 0x31; /* Default if no FDR found */
128 /* a, b and dfsr matches identifiers A,B and C respectively in AN2919 */
129 unsigned short a, b, ga, gb;
130 unsigned long c_div, est_div;
131
132#ifdef CONFIG_FSL_I2C_CUSTOM_DFSR
133 dfsr = CONFIG_FSL_I2C_CUSTOM_DFSR;
134#else
135 /* Condition 1: dfsr <= 50/T */
136 dfsr = (5 * (i2c_clk / 1000)) / 100000;
137#endif
138#ifdef CONFIG_FSL_I2C_CUSTOM_FDR
139 fdr = CONFIG_FSL_I2C_CUSTOM_FDR;
140 speed = i2c_clk / divider; /* Fake something */
141#else
142 debug("Requested speed:%d, i2c_clk:%d\n", speed, i2c_clk);
143 if (!dfsr)
144 dfsr = 1;
145
146 est_div = ~0;
147 for (ga = 0x4, a = 10; a <= 30; ga++, a += 2) {
148 for (gb = 0; gb < 8; gb++) {
149 b = 16 << gb;
150 c_div = b * (a + ((3*dfsr)/b)*2);
151 if ((c_div > divider) && (c_div < est_div)) {
152 unsigned short bin_gb, bin_ga;
153
154 est_div = c_div;
155 bin_gb = gb << 2;
156 bin_ga = (ga & 0x3) | ((ga & 0x4) << 3);
157 fdr = bin_gb | bin_ga;
158 speed = i2c_clk / est_div;
159 debug("FDR:0x%.2x, div:%ld, ga:0x%x, gb:0x%x, "
160 "a:%d, b:%d, speed:%d\n",
161 fdr, est_div, ga, gb, a, b, speed);
162 /* Condition 2 not accounted for */
163 debug("Tr <= %d ns\n",
164 (b - 3 * dfsr) * 1000000 /
165 (i2c_clk / 1000));
166 }
167 }
168 if (a == 20)
169 a += 2;
170 if (a == 24)
171 a += 4;
172 }
173 debug("divider:%d, est_div:%ld, DFSR:%d\n", divider, est_div, dfsr);
174 debug("FDR:0x%.2x, speed:%d\n", fdr, speed);
175#endif
mario.six@gdsys.ccec2c81c2016-04-25 08:31:01 +0200176 writeb(dfsr, &base->dfsrr); /* set default filter */
177 writeb(fdr, &base->fdr); /* set bus speed */
Joakim Tjernlund99404202009-09-17 11:07:17 +0200178#else
179 unsigned int i;
Timur Tabid8c82db2008-03-14 17:45:29 -0500180
181 for (i = 0; i < ARRAY_SIZE(fsl_i2c_speed_map); i++)
182 if (fsl_i2c_speed_map[i].divider >= divider) {
TsiChung Liew5d9a5ef2008-08-19 00:56:46 +0600183 u8 fdr;
Joakim Tjernlund99404202009-09-17 11:07:17 +0200184
Joakim Tjernlundd01ee4d2009-09-17 11:07:16 +0200185 fdr = fsl_i2c_speed_map[i].fdr;
186 speed = i2c_clk / fsl_i2c_speed_map[i].divider;
mario.six@gdsys.ccec2c81c2016-04-25 08:31:01 +0200187 writeb(fdr, &base->fdr); /* set bus speed */
Joakim Tjernlundd01ee4d2009-09-17 11:07:16 +0200188
Timur Tabid8c82db2008-03-14 17:45:29 -0500189 break;
190 }
Joakim Tjernlund99404202009-09-17 11:07:17 +0200191#endif
Timur Tabid8c82db2008-03-14 17:45:29 -0500192 return speed;
193}
194
Kim Phillips62f730f2012-10-16 14:28:43 +0000195static unsigned int get_i2c_clock(int bus)
Jerry Huangc9a8b252011-10-26 15:29:38 +0000196{
197 if (bus)
Simon Glass609e6ec2012-12-13 20:48:49 +0000198 return gd->arch.i2c2_clk; /* I2C2 clock */
Jerry Huangc9a8b252011-10-26 15:29:38 +0000199 else
Simon Glass609e6ec2012-12-13 20:48:49 +0000200 return gd->arch.i2c1_clk; /* I2C1 clock */
Jerry Huangc9a8b252011-10-26 15:29:38 +0000201}
202
mario.six@gdsys.ccec2c81c2016-04-25 08:31:01 +0200203static int fsl_i2c_fixup(const struct fsl_i2c_base *base)
Chunhe Lanb8ce3342013-08-16 15:10:36 +0800204{
205 const unsigned long long timeout = usec2ticks(CONFIG_I2C_MBB_TIMEOUT);
206 unsigned long long timeval = 0;
207 int ret = -1;
Chunhe Lan9c3f77e2013-08-16 15:10:37 +0800208 unsigned int flags = 0;
209
210#ifdef CONFIG_SYS_FSL_ERRATUM_I2C_A004447
211 unsigned int svr = get_svr();
212 if ((SVR_SOC_VER(svr) == SVR_8548 && IS_SVR_REV(svr, 3, 1)) ||
213 (SVR_REV(svr) <= CONFIG_SYS_FSL_A004447_SVR_REV))
214 flags = I2C_CR_BIT6;
215#endif
Chunhe Lanb8ce3342013-08-16 15:10:36 +0800216
mario.six@gdsys.ccec2c81c2016-04-25 08:31:01 +0200217 writeb(I2C_CR_MEN | I2C_CR_MSTA, &base->cr);
Chunhe Lanb8ce3342013-08-16 15:10:36 +0800218
219 timeval = get_ticks();
mario.six@gdsys.ccec2c81c2016-04-25 08:31:01 +0200220 while (!(readb(&base->sr) & I2C_SR_MBB)) {
Chunhe Lanb8ce3342013-08-16 15:10:36 +0800221 if ((get_ticks() - timeval) > timeout)
222 goto err;
223 }
224
mario.six@gdsys.ccec2c81c2016-04-25 08:31:01 +0200225 if (readb(&base->sr) & I2C_SR_MAL) {
Chunhe Lanb8ce3342013-08-16 15:10:36 +0800226 /* SDA is stuck low */
mario.six@gdsys.ccec2c81c2016-04-25 08:31:01 +0200227 writeb(0, &base->cr);
Chunhe Lanb8ce3342013-08-16 15:10:36 +0800228 udelay(100);
mario.six@gdsys.ccec2c81c2016-04-25 08:31:01 +0200229 writeb(I2C_CR_MSTA | flags, &base->cr);
230 writeb(I2C_CR_MEN | I2C_CR_MSTA | flags, &base->cr);
Chunhe Lanb8ce3342013-08-16 15:10:36 +0800231 }
232
mario.six@gdsys.ccec2c81c2016-04-25 08:31:01 +0200233 readb(&base->dr);
Chunhe Lanb8ce3342013-08-16 15:10:36 +0800234
235 timeval = get_ticks();
mario.six@gdsys.ccec2c81c2016-04-25 08:31:01 +0200236 while (!(readb(&base->sr) & I2C_SR_MIF)) {
Chunhe Lanb8ce3342013-08-16 15:10:36 +0800237 if ((get_ticks() - timeval) > timeout)
238 goto err;
239 }
240 ret = 0;
241
242err:
mario.six@gdsys.ccec2c81c2016-04-25 08:31:01 +0200243 writeb(I2C_CR_MEN | flags, &base->cr);
244 writeb(0, &base->sr);
Chunhe Lanb8ce3342013-08-16 15:10:36 +0800245 udelay(100);
246
247 return ret;
248}
249
Heiko Schocher00f792e2012-10-24 13:48:22 +0200250static void fsl_i2c_init(struct i2c_adapter *adap, int speed, int slaveadd)
Jon Loeliger7237c032006-10-19 11:02:16 -0500251{
mario.six@gdsys.ccec2c81c2016-04-25 08:31:01 +0200252 const struct fsl_i2c_base *base;
Chunhe Lanb8ce3342013-08-16 15:10:36 +0800253 const unsigned long long timeout = usec2ticks(CONFIG_I2C_MBB_TIMEOUT);
254 unsigned long long timeval;
Jon Loeliger7237c032006-10-19 11:02:16 -0500255
Heiko Schocher39df00d2009-07-09 12:04:26 +0200256#ifdef CONFIG_SYS_I2C_INIT_BOARD
Richard Retanubun26a33502010-04-12 15:08:17 -0400257 /* Call board specific i2c bus reset routine before accessing the
258 * environment, which might be in a chip on that bus. For details
259 * about this problem see doc/I2C_Edge_Conditions.
260 */
Heiko Schocher39df00d2009-07-09 12:04:26 +0200261 i2c_init_board();
262#endif
mario.six@gdsys.ccec2c81c2016-04-25 08:31:01 +0200263 base = (struct fsl_i2c_base *)i2c_base[adap->hwadapnr];
Jerry Huangc9a8b252011-10-26 15:29:38 +0000264
mario.six@gdsys.ccec2c81c2016-04-25 08:31:01 +0200265 writeb(0, &base->cr); /* stop I2C controller */
Heiko Schocher00f792e2012-10-24 13:48:22 +0200266 udelay(5); /* let it shutdown in peace */
mario.six@gdsys.ccec2c81c2016-04-25 08:31:01 +0200267 set_i2c_bus_speed(base, get_i2c_clock(adap->hwadapnr), speed);
268 writeb(slaveadd << 1, &base->adr);/* write slave address */
269 writeb(0x0, &base->sr); /* clear status register */
270 writeb(I2C_CR_MEN, &base->cr); /* start I2C controller */
Richard Retanubun26a33502010-04-12 15:08:17 -0400271
Chunhe Lanb8ce3342013-08-16 15:10:36 +0800272 timeval = get_ticks();
mario.six@gdsys.ccec2c81c2016-04-25 08:31:01 +0200273 while (readb(&base->sr) & I2C_SR_MBB) {
Chunhe Lanb8ce3342013-08-16 15:10:36 +0800274 if ((get_ticks() - timeval) < timeout)
275 continue;
276
mario.six@gdsys.ccec2c81c2016-04-25 08:31:01 +0200277 if (fsl_i2c_fixup(base))
Chunhe Lanb8ce3342013-08-16 15:10:36 +0800278 debug("i2c_init: BUS#%d failed to init\n",
279 adap->hwadapnr);
280
281 break;
282 }
283
Richard Retanubun26a33502010-04-12 15:08:17 -0400284#ifdef CONFIG_SYS_I2C_BOARD_LATE_INIT
285 /* Call board specific i2c bus reset routine AFTER the bus has been
286 * initialized. Use either this callpoint or i2c_init_board;
287 * which is called before i2c_init operations.
288 * For details about this problem see doc/I2C_Edge_Conditions.
289 */
290 i2c_board_late_init();
291#endif
Jon Loeliger7237c032006-10-19 11:02:16 -0500292}
293
Joakim Tjernlund21f4cbb2009-09-17 11:07:15 +0200294static int
Heiko Schocher00f792e2012-10-24 13:48:22 +0200295i2c_wait4bus(struct i2c_adapter *adap)
Jon Loeliger7237c032006-10-19 11:02:16 -0500296{
mario.six@gdsys.ccec2c81c2016-04-25 08:31:01 +0200297 struct fsl_i2c_base *base =
298 (struct fsl_i2c_base *)i2c_base[adap->hwadapnr];
Stefan Roesef2302d42008-08-06 14:05:38 +0200299 unsigned long long timeval = get_ticks();
Timur Tabi92477a62009-09-04 16:28:35 -0500300 const unsigned long long timeout = usec2ticks(CONFIG_I2C_MBB_TIMEOUT);
Jon Loeliger7237c032006-10-19 11:02:16 -0500301
mario.six@gdsys.ccec2c81c2016-04-25 08:31:01 +0200302 while (readb(&base->sr) & I2C_SR_MBB) {
Timur Tabi92477a62009-09-04 16:28:35 -0500303 if ((get_ticks() - timeval) > timeout)
Jon Loeliger7237c032006-10-19 11:02:16 -0500304 return -1;
Jon Loeliger7237c032006-10-19 11:02:16 -0500305 }
306
307 return 0;
308}
309
310static __inline__ int
Heiko Schocher00f792e2012-10-24 13:48:22 +0200311i2c_wait(struct i2c_adapter *adap, int write)
Jon Loeliger7237c032006-10-19 11:02:16 -0500312{
313 u32 csr;
Stefan Roesef2302d42008-08-06 14:05:38 +0200314 unsigned long long timeval = get_ticks();
Timur Tabi92477a62009-09-04 16:28:35 -0500315 const unsigned long long timeout = usec2ticks(CONFIG_I2C_TIMEOUT);
mario.six@gdsys.ccec2c81c2016-04-25 08:31:01 +0200316 struct fsl_i2c_base *base =
317 (struct fsl_i2c_base *)i2c_base[adap->hwadapnr];
Jon Loeliger7237c032006-10-19 11:02:16 -0500318
319 do {
mario.six@gdsys.ccec2c81c2016-04-25 08:31:01 +0200320 csr = readb(&base->sr);
Jon Loeliger7237c032006-10-19 11:02:16 -0500321 if (!(csr & I2C_SR_MIF))
322 continue;
Joakim Tjernlund21f4cbb2009-09-17 11:07:15 +0200323 /* Read again to allow register to stabilise */
mario.six@gdsys.ccec2c81c2016-04-25 08:31:01 +0200324 csr = readb(&base->sr);
Jon Loeliger7237c032006-10-19 11:02:16 -0500325
mario.six@gdsys.ccec2c81c2016-04-25 08:31:01 +0200326 writeb(0x0, &base->sr);
Jon Loeliger7237c032006-10-19 11:02:16 -0500327
328 if (csr & I2C_SR_MAL) {
329 debug("i2c_wait: MAL\n");
330 return -1;
331 }
332
333 if (!(csr & I2C_SR_MCF)) {
334 debug("i2c_wait: unfinished\n");
335 return -1;
336 }
337
Joakim Tjernlund1939d962006-11-28 16:17:27 -0600338 if (write == I2C_WRITE_BIT && (csr & I2C_SR_RXAK)) {
Jon Loeliger7237c032006-10-19 11:02:16 -0500339 debug("i2c_wait: No RXACK\n");
340 return -1;
341 }
342
343 return 0;
Timur Tabi92477a62009-09-04 16:28:35 -0500344 } while ((get_ticks() - timeval) < timeout);
Jon Loeliger7237c032006-10-19 11:02:16 -0500345
346 debug("i2c_wait: timed out\n");
347 return -1;
348}
349
350static __inline__ int
Heiko Schocher00f792e2012-10-24 13:48:22 +0200351i2c_write_addr(struct i2c_adapter *adap, u8 dev, u8 dir, int rsta)
Jon Loeliger7237c032006-10-19 11:02:16 -0500352{
mario.six@gdsys.ccec2c81c2016-04-25 08:31:01 +0200353 struct fsl_i2c_base *base =
354 (struct fsl_i2c_base *)i2c_base[adap->hwadapnr];
Heiko Schocher00f792e2012-10-24 13:48:22 +0200355
Jon Loeliger7237c032006-10-19 11:02:16 -0500356 writeb(I2C_CR_MEN | I2C_CR_MSTA | I2C_CR_MTX
357 | (rsta ? I2C_CR_RSTA : 0),
mario.six@gdsys.ccec2c81c2016-04-25 08:31:01 +0200358 &base->cr);
Jon Loeliger7237c032006-10-19 11:02:16 -0500359
mario.six@gdsys.ccec2c81c2016-04-25 08:31:01 +0200360 writeb((dev << 1) | dir, &base->dr);
Jon Loeliger7237c032006-10-19 11:02:16 -0500361
Heiko Schocher00f792e2012-10-24 13:48:22 +0200362 if (i2c_wait(adap, I2C_WRITE_BIT) < 0)
Jon Loeliger7237c032006-10-19 11:02:16 -0500363 return 0;
364
365 return 1;
366}
367
368static __inline__ int
Heiko Schocher00f792e2012-10-24 13:48:22 +0200369__i2c_write(struct i2c_adapter *adap, u8 *data, int length)
Jon Loeliger7237c032006-10-19 11:02:16 -0500370{
mario.six@gdsys.ccec2c81c2016-04-25 08:31:01 +0200371 struct fsl_i2c_base *base =
372 (struct fsl_i2c_base *)i2c_base[adap->hwadapnr];
Jon Loeliger7237c032006-10-19 11:02:16 -0500373 int i;
374
Jon Loeliger7237c032006-10-19 11:02:16 -0500375 for (i = 0; i < length; i++) {
mario.six@gdsys.ccec2c81c2016-04-25 08:31:01 +0200376 writeb(data[i], &base->dr);
Jon Loeliger7237c032006-10-19 11:02:16 -0500377
Heiko Schocher00f792e2012-10-24 13:48:22 +0200378 if (i2c_wait(adap, I2C_WRITE_BIT) < 0)
Jon Loeliger7237c032006-10-19 11:02:16 -0500379 break;
380 }
381
382 return i;
383}
384
385static __inline__ int
Heiko Schocher00f792e2012-10-24 13:48:22 +0200386__i2c_read(struct i2c_adapter *adap, u8 *data, int length)
Jon Loeliger7237c032006-10-19 11:02:16 -0500387{
mario.six@gdsys.ccec2c81c2016-04-25 08:31:01 +0200388 struct fsl_i2c_base *base =
389 (struct fsl_i2c_base *)i2c_base[adap->hwadapnr];
Jon Loeliger7237c032006-10-19 11:02:16 -0500390 int i;
391
392 writeb(I2C_CR_MEN | I2C_CR_MSTA | ((length == 1) ? I2C_CR_TXAK : 0),
mario.six@gdsys.ccec2c81c2016-04-25 08:31:01 +0200393 &base->cr);
Jon Loeliger7237c032006-10-19 11:02:16 -0500394
395 /* dummy read */
mario.six@gdsys.ccec2c81c2016-04-25 08:31:01 +0200396 readb(&base->dr);
Jon Loeliger7237c032006-10-19 11:02:16 -0500397
398 for (i = 0; i < length; i++) {
Heiko Schocher00f792e2012-10-24 13:48:22 +0200399 if (i2c_wait(adap, I2C_READ_BIT) < 0)
Jon Loeliger7237c032006-10-19 11:02:16 -0500400 break;
401
402 /* Generate ack on last next to last byte */
403 if (i == length - 2)
404 writeb(I2C_CR_MEN | I2C_CR_MSTA | I2C_CR_TXAK,
mario.six@gdsys.ccec2c81c2016-04-25 08:31:01 +0200405 &base->cr);
Jon Loeliger7237c032006-10-19 11:02:16 -0500406
Joakim Tjernlundd1c9e5b2009-09-22 13:40:44 +0200407 /* Do not generate stop on last byte */
Jon Loeliger7237c032006-10-19 11:02:16 -0500408 if (i == length - 1)
Joakim Tjernlundd1c9e5b2009-09-22 13:40:44 +0200409 writeb(I2C_CR_MEN | I2C_CR_MSTA | I2C_CR_MTX,
mario.six@gdsys.ccec2c81c2016-04-25 08:31:01 +0200410 &base->cr);
Jon Loeliger7237c032006-10-19 11:02:16 -0500411
mario.six@gdsys.ccec2c81c2016-04-25 08:31:01 +0200412 data[i] = readb(&base->dr);
Jon Loeliger7237c032006-10-19 11:02:16 -0500413 }
414
415 return i;
416}
417
Heiko Schocher00f792e2012-10-24 13:48:22 +0200418static int
mario.six@gdsys.cc2b21e962016-04-25 08:31:02 +0200419fsl_i2c_read(struct i2c_adapter *adap, u8 chip_addr, uint offset, int olen,
420 u8 *data, int dlen)
Jon Loeliger7237c032006-10-19 11:02:16 -0500421{
mario.six@gdsys.ccec2c81c2016-04-25 08:31:01 +0200422 struct fsl_i2c_base *base =
423 (struct fsl_i2c_base *)i2c_base[adap->hwadapnr];
mario.six@gdsys.cc2b21e962016-04-25 08:31:02 +0200424 int ret = -1; /* signal error */
425 u8 *o = (u8 *)&offset;
426 int len = olen * -1;
Jon Loeliger7237c032006-10-19 11:02:16 -0500427
Heiko Schocher00f792e2012-10-24 13:48:22 +0200428 if (i2c_wait4bus(adap) < 0)
Reinhard Pfaub778c1b2013-06-26 15:55:14 +0200429 return -1;
430
Shaveta Leekhaa4057642014-04-24 14:51:23 +0530431 /* To handle the need of I2C devices that require to write few bytes
432 * (more than 4 bytes of address as in the case of else part)
mario.six@gdsys.cc2b21e962016-04-25 08:31:02 +0200433 * of data before reading, Negative equivalent of dlen(bytes to write)
Shaveta Leekhaa4057642014-04-24 14:51:23 +0530434 * is passed, but used the +ve part of len for writing data
435 */
mario.six@gdsys.cc2b21e962016-04-25 08:31:02 +0200436 if (olen < 0) {
Shaveta Leekhaa4057642014-04-24 14:51:23 +0530437 /* Generate a START and send the Address and
438 * the Tx Bytes to the slave.
439 * "START: Address: Write bytes data[len]"
440 * IF part supports writing any number of bytes in contrast
441 * to the else part, which supports writing address offset
442 * of upto 4 bytes only.
443 * bytes that need to be written are passed in
444 * "data", which will eventually keep the data READ,
445 * after writing the len bytes out of it
446 */
mario.six@gdsys.cc2b21e962016-04-25 08:31:02 +0200447 if (i2c_write_addr(adap, chip_addr, I2C_WRITE_BIT, 0) != 0)
448 ret = __i2c_write(adap, data, len);
Joakim Tjernlundf6f5f702007-01-31 11:04:19 +0100449
mario.six@gdsys.cc2b21e962016-04-25 08:31:02 +0200450 if (ret != len)
Shaveta Leekhaa4057642014-04-24 14:51:23 +0530451 return -1;
452
mario.six@gdsys.cc2b21e962016-04-25 08:31:02 +0200453 if (dlen && i2c_write_addr(adap, chip_addr,
454 I2C_READ_BIT, 1) != 0)
455 ret = __i2c_read(adap, data, dlen);
Shaveta Leekhaa4057642014-04-24 14:51:23 +0530456 } else {
mario.six@gdsys.cc2b21e962016-04-25 08:31:02 +0200457 if ((!dlen || olen > 0) &&
458 i2c_write_addr(adap, chip_addr, I2C_WRITE_BIT, 0) != 0 &&
459 __i2c_write(adap, &o[4 - olen], olen) == olen)
460 ret = 0; /* No error so far */
Shaveta Leekhaa4057642014-04-24 14:51:23 +0530461
mario.six@gdsys.cc2b21e962016-04-25 08:31:02 +0200462 if (dlen && i2c_write_addr(adap, chip_addr, I2C_READ_BIT,
463 olen ? 1 : 0) != 0)
464 ret = __i2c_read(adap, data, dlen);
Shaveta Leekhaa4057642014-04-24 14:51:23 +0530465 }
Jon Loeliger7237c032006-10-19 11:02:16 -0500466
mario.six@gdsys.ccec2c81c2016-04-25 08:31:01 +0200467 writeb(I2C_CR_MEN, &base->cr);
Jon Loeliger7237c032006-10-19 11:02:16 -0500468
Heiko Schocher00f792e2012-10-24 13:48:22 +0200469 if (i2c_wait4bus(adap)) /* Wait until STOP */
Joakim Tjernlundd1c9e5b2009-09-22 13:40:44 +0200470 debug("i2c_read: wait4bus timed out\n");
471
mario.six@gdsys.cc2b21e962016-04-25 08:31:02 +0200472 if (ret == dlen)
473 return 0;
Jon Loeliger4d45f692006-10-19 12:02:24 -0500474
475 return -1;
Jon Loeliger7237c032006-10-19 11:02:16 -0500476}
477
Heiko Schocher00f792e2012-10-24 13:48:22 +0200478static int
mario.six@gdsys.cc2b21e962016-04-25 08:31:02 +0200479fsl_i2c_write(struct i2c_adapter *adap, u8 chip_addr, uint offset, int olen,
480 u8 *data, int dlen)
Jon Loeliger7237c032006-10-19 11:02:16 -0500481{
mario.six@gdsys.ccec2c81c2016-04-25 08:31:01 +0200482 struct fsl_i2c_base *base =
483 (struct fsl_i2c_base *)i2c_base[adap->hwadapnr];
mario.six@gdsys.cc2b21e962016-04-25 08:31:02 +0200484 int ret = -1; /* signal error */
485 u8 *o = (u8 *)&offset;
Jon Loeliger7237c032006-10-19 11:02:16 -0500486
Chunhe Lanb8ce3342013-08-16 15:10:36 +0800487 if (i2c_wait4bus(adap) < 0)
488 return -1;
489
mario.six@gdsys.cc2b21e962016-04-25 08:31:02 +0200490 if (i2c_write_addr(adap, chip_addr, I2C_WRITE_BIT, 0) != 0 &&
491 __i2c_write(adap, &o[4 - olen], olen) == olen) {
492 ret = __i2c_write(adap, data, dlen);
Jon Loeliger4d45f692006-10-19 12:02:24 -0500493 }
Jon Loeliger7237c032006-10-19 11:02:16 -0500494
mario.six@gdsys.ccec2c81c2016-04-25 08:31:01 +0200495 writeb(I2C_CR_MEN, &base->cr);
Heiko Schocher00f792e2012-10-24 13:48:22 +0200496 if (i2c_wait4bus(adap)) /* Wait until STOP */
Joakim Tjernlund21f4cbb2009-09-17 11:07:15 +0200497 debug("i2c_write: wait4bus timed out\n");
Jon Loeliger7237c032006-10-19 11:02:16 -0500498
mario.six@gdsys.cc2b21e962016-04-25 08:31:02 +0200499 if (ret == dlen)
500 return 0;
Jon Loeliger4d45f692006-10-19 12:02:24 -0500501
502 return -1;
Jon Loeliger7237c032006-10-19 11:02:16 -0500503}
504
Heiko Schocher00f792e2012-10-24 13:48:22 +0200505static int
506fsl_i2c_probe(struct i2c_adapter *adap, uchar chip)
Jon Loeliger7237c032006-10-19 11:02:16 -0500507{
mario.six@gdsys.ccec2c81c2016-04-25 08:31:01 +0200508 struct fsl_i2c_base *base =
509 (struct fsl_i2c_base *)i2c_base[adap->hwadapnr];
Joakim Tjernlundf6f5f702007-01-31 11:04:19 +0100510 /* For unknow reason the controller will ACK when
511 * probing for a slave with the same address, so skip
512 * it.
Jon Loeliger7237c032006-10-19 11:02:16 -0500513 */
mario.six@gdsys.ccec2c81c2016-04-25 08:31:01 +0200514 if (chip == (readb(&base->adr) >> 1))
Joakim Tjernlundf6f5f702007-01-31 11:04:19 +0100515 return -1;
Jon Loeliger7237c032006-10-19 11:02:16 -0500516
Heiko Schocher00f792e2012-10-24 13:48:22 +0200517 return fsl_i2c_read(adap, chip, 0, 0, NULL, 0);
Jon Loeliger7237c032006-10-19 11:02:16 -0500518}
519
Heiko Schocher00f792e2012-10-24 13:48:22 +0200520static unsigned int fsl_i2c_set_bus_speed(struct i2c_adapter *adap,
521 unsigned int speed)
Timur Tabibe5e6182006-11-03 19:15:00 -0600522{
mario.six@gdsys.ccec2c81c2016-04-25 08:31:01 +0200523 struct fsl_i2c_base *base =
524 (struct fsl_i2c_base *)i2c_base[adap->hwadapnr];
Heiko Schocherc1bce4f2009-02-24 11:30:37 +0100525
mario.six@gdsys.ccec2c81c2016-04-25 08:31:01 +0200526 writeb(0, &base->cr); /* stop controller */
527 set_i2c_bus_speed(base, get_i2c_clock(adap->hwadapnr), speed);
528 writeb(I2C_CR_MEN, &base->cr); /* start controller */
Timur Tabid8c82db2008-03-14 17:45:29 -0500529
530 return 0;
Timur Tabibe5e6182006-11-03 19:15:00 -0600531}
532
Heiko Schocher00f792e2012-10-24 13:48:22 +0200533/*
534 * Register fsl i2c adapters
535 */
536U_BOOT_I2C_ADAP_COMPLETE(fsl_0, fsl_i2c_init, fsl_i2c_probe, fsl_i2c_read,
537 fsl_i2c_write, fsl_i2c_set_bus_speed,
538 CONFIG_SYS_FSL_I2C_SPEED, CONFIG_SYS_FSL_I2C_SLAVE,
539 0)
540#ifdef CONFIG_SYS_FSL_I2C2_OFFSET
541U_BOOT_I2C_ADAP_COMPLETE(fsl_1, fsl_i2c_init, fsl_i2c_probe, fsl_i2c_read,
542 fsl_i2c_write, fsl_i2c_set_bus_speed,
543 CONFIG_SYS_FSL_I2C2_SPEED, CONFIG_SYS_FSL_I2C2_SLAVE,
544 1)
Heiko Schocherc1bce4f2009-02-24 11:30:37 +0100545#endif
Shengzhou Liua17fd102014-07-07 12:17:48 +0800546#ifdef CONFIG_SYS_FSL_I2C3_OFFSET
547U_BOOT_I2C_ADAP_COMPLETE(fsl_2, fsl_i2c_init, fsl_i2c_probe, fsl_i2c_read,
548 fsl_i2c_write, fsl_i2c_set_bus_speed,
549 CONFIG_SYS_FSL_I2C3_SPEED, CONFIG_SYS_FSL_I2C3_SLAVE,
550 2)
551#endif
552#ifdef CONFIG_SYS_FSL_I2C4_OFFSET
553U_BOOT_I2C_ADAP_COMPLETE(fsl_3, fsl_i2c_init, fsl_i2c_probe, fsl_i2c_read,
554 fsl_i2c_write, fsl_i2c_set_bus_speed,
555 CONFIG_SYS_FSL_I2C4_SPEED, CONFIG_SYS_FSL_I2C4_SLAVE,
556 3)
557#endif