]> git.ipfire.org Git - people/ms/u-boot.git/blame - drivers/i2c/fsl_i2c.c
dfu: Rename _FUNCTION_DFU to DFU_OVER_
[people/ms/u-boot.git] / drivers / i2c / fsl_i2c.c
CommitLineData
debb7354 1/*
92477a63 2 * Copyright 2006,2009 Freescale Semiconductor, Inc.
debb7354 3 *
00f792e0
HS
4 * 2012, Heiko Schocher, DENX Software Engineering, hs@denx.de.
5 * Changes for multibus/multiadapter I2C support.
6 *
5b8031cc 7 * SPDX-License-Identifier: GPL-2.0
debb7354
JL
8 */
9
10#include <common.h>
4d45f69e 11#include <command.h>
20476726 12#include <i2c.h> /* Functional interface */
7237c033 13#include <asm/io.h>
20476726 14#include <asm/fsl_i2c.h> /* HW definitions */
dbc82ce3 15#include <dm.h>
16#include <mapmem.h>
debb7354 17
92477a63
TT
18/* The maximum number of microseconds we will wait until another master has
19 * released the bus. If not defined in the board header file, then use a
20 * generic value.
21 */
22#ifndef CONFIG_I2C_MBB_TIMEOUT
23#define CONFIG_I2C_MBB_TIMEOUT 100000
24#endif
25
26/* The maximum number of microseconds we will wait for a read or write
27 * operation to complete. If not defined in the board header file, then use a
28 * generic value.
29 */
30#ifndef CONFIG_I2C_TIMEOUT
6dd38cc3 31#define CONFIG_I2C_TIMEOUT 100000
92477a63 32#endif
debb7354 33
1939d969
JT
34#define I2C_READ_BIT 1
35#define I2C_WRITE_BIT 0
36
d8c82db4
TT
37DECLARE_GLOBAL_DATA_PTR;
38
dbc82ce3 39#ifndef CONFIG_DM_I2C
ec2c81c5 40static const struct fsl_i2c_base *i2c_base[4] = {
41 (struct fsl_i2c_base *)(CONFIG_SYS_IMMR + CONFIG_SYS_FSL_I2C_OFFSET),
00f792e0 42#ifdef CONFIG_SYS_FSL_I2C2_OFFSET
ec2c81c5 43 (struct fsl_i2c_base *)(CONFIG_SYS_IMMR + CONFIG_SYS_FSL_I2C2_OFFSET),
a17fd10f
SL
44#endif
45#ifdef CONFIG_SYS_FSL_I2C3_OFFSET
ec2c81c5 46 (struct fsl_i2c_base *)(CONFIG_SYS_IMMR + CONFIG_SYS_FSL_I2C3_OFFSET),
a17fd10f
SL
47#endif
48#ifdef CONFIG_SYS_FSL_I2C4_OFFSET
ec2c81c5 49 (struct fsl_i2c_base *)(CONFIG_SYS_IMMR + CONFIG_SYS_FSL_I2C4_OFFSET)
be5e6181
TT
50#endif
51};
dbc82ce3 52#endif
debb7354 53
d8c82db4
TT
54/* I2C speed map for a DFSR value of 1 */
55
645cb46e 56#ifdef __M68K__
d8c82db4
TT
57/*
58 * Map I2C frequency dividers to FDR and DFSR values
59 *
60 * This structure is used to define the elements of a table that maps I2C
61 * frequency divider (I2C clock rate divided by I2C bus speed) to a value to be
62 * programmed into the Frequency Divider Ratio (FDR) and Digital Filter
63 * Sampling Rate (DFSR) registers.
64 *
65 * The actual table should be defined in the board file, and it must be called
66 * fsl_i2c_speed_map[].
67 *
68 * The last entry of the table must have a value of {-1, X}, where X is same
69 * FDR/DFSR values as the second-to-last entry. This guarantees that any
70 * search through the array will always find a match.
71 *
72 * The values of the divider must be in increasing numerical order, i.e.
73 * fsl_i2c_speed_map[x+1].divider > fsl_i2c_speed_map[x].divider.
74 *
75 * For this table, the values are based on a value of 1 for the DFSR
76 * register. See the application note AN2919 "Determining the I2C Frequency
77 * Divider Ratio for SCL"
5d9a5efa
TL
78 *
79 * ColdFire I2C frequency dividers for FDR values are different from
80 * PowerPC. The protocol to use the I2C module is still the same.
81 * A different table is defined and are based on MCF5xxx user manual.
82 *
d8c82db4
TT
83 */
84static const struct {
85 unsigned short divider;
d8c82db4
TT
86 u8 fdr;
87} fsl_i2c_speed_map[] = {
5d9a5efa
TL
88 {20, 32}, {22, 33}, {24, 34}, {26, 35},
89 {28, 0}, {28, 36}, {30, 1}, {32, 37},
90 {34, 2}, {36, 38}, {40, 3}, {40, 39},
91 {44, 4}, {48, 5}, {48, 40}, {56, 6},
92 {56, 41}, {64, 42}, {68, 7}, {72, 43},
93 {80, 8}, {80, 44}, {88, 9}, {96, 41},
94 {104, 10}, {112, 42}, {128, 11}, {128, 43},
95 {144, 12}, {160, 13}, {160, 48}, {192, 14},
96 {192, 49}, {224, 50}, {240, 15}, {256, 51},
97 {288, 16}, {320, 17}, {320, 52}, {384, 18},
98 {384, 53}, {448, 54}, {480, 19}, {512, 55},
99 {576, 20}, {640, 21}, {640, 56}, {768, 22},
100 {768, 57}, {960, 23}, {896, 58}, {1024, 59},
101 {1152, 24}, {1280, 25}, {1280, 60}, {1536, 26},
102 {1536, 61}, {1792, 62}, {1920, 27}, {2048, 63},
103 {2304, 28}, {2560, 29}, {3072, 30}, {3840, 31},
104 {-1, 31}
d8c82db4 105};
645cb46e 106#endif
d8c82db4
TT
107
108/**
109 * Set the I2C bus speed for a given I2C device
110 *
ec2c81c5 111 * @param base: the I2C device registers
d8c82db4
TT
112 * @i2c_clk: I2C bus clock frequency
113 * @speed: the desired speed of the bus
114 *
115 * The I2C device must be stopped before calling this function.
116 *
117 * The return value is the actual bus speed that is set.
118 */
a059de11
MS
119static uint set_i2c_bus_speed(const struct fsl_i2c_base *base,
120 uint i2c_clk, uint speed)
d8c82db4 121{
a059de11 122 ushort divider = min(i2c_clk / speed, (uint)USHRT_MAX);
d8c82db4
TT
123
124 /*
125 * We want to choose an FDR/DFSR that generates an I2C bus speed that
126 * is equal to or lower than the requested speed. That means that we
127 * want the first divider that is equal to or greater than the
128 * calculated divider.
129 */
5d9a5efa 130#ifdef __PPC__
99404202
JT
131 u8 dfsr, fdr = 0x31; /* Default if no FDR found */
132 /* a, b and dfsr matches identifiers A,B and C respectively in AN2919 */
a059de11
MS
133 ushort a, b, ga, gb;
134 ulong c_div, est_div;
99404202 135
d01ee4db 136#ifdef CONFIG_FSL_I2C_CUSTOM_DFSR
99404202 137 dfsr = CONFIG_FSL_I2C_CUSTOM_DFSR;
d01ee4db 138#else
99404202
JT
139 /* Condition 1: dfsr <= 50/T */
140 dfsr = (5 * (i2c_clk / 1000)) / 100000;
d01ee4db
JT
141#endif
142#ifdef CONFIG_FSL_I2C_CUSTOM_FDR
99404202
JT
143 fdr = CONFIG_FSL_I2C_CUSTOM_FDR;
144 speed = i2c_clk / divider; /* Fake something */
145#else
146 debug("Requested speed:%d, i2c_clk:%d\n", speed, i2c_clk);
147 if (!dfsr)
148 dfsr = 1;
149
150 est_div = ~0;
151 for (ga = 0x4, a = 10; a <= 30; ga++, a += 2) {
152 for (gb = 0; gb < 8; gb++) {
153 b = 16 << gb;
a059de11
MS
154 c_div = b * (a + ((3 * dfsr) / b) * 2);
155 if (c_div > divider && c_div < est_div) {
156 ushort bin_gb, bin_ga;
99404202
JT
157
158 est_div = c_div;
159 bin_gb = gb << 2;
160 bin_ga = (ga & 0x3) | ((ga & 0x4) << 3);
161 fdr = bin_gb | bin_ga;
162 speed = i2c_clk / est_div;
a059de11
MS
163
164 debug("FDR: 0x%.2x, ", fdr);
165 debug("div: %ld, ", est_div);
166 debug("ga: 0x%x, gb: 0x%x, ", ga, gb);
167 debug("a: %d, b: %d, speed: %d\n", a, b, speed);
168
99404202
JT
169 /* Condition 2 not accounted for */
170 debug("Tr <= %d ns\n",
171 (b - 3 * dfsr) * 1000000 /
172 (i2c_clk / 1000));
173 }
174 }
175 if (a == 20)
176 a += 2;
177 if (a == 24)
178 a += 4;
179 }
a059de11
MS
180 debug("divider: %d, est_div: %ld, DFSR: %d\n", divider, est_div, dfsr);
181 debug("FDR: 0x%.2x, speed: %d\n", fdr, speed);
99404202 182#endif
ec2c81c5 183 writeb(dfsr, &base->dfsrr); /* set default filter */
184 writeb(fdr, &base->fdr); /* set bus speed */
d01ee4db 185#else
a059de11 186 uint i;
99404202
JT
187
188 for (i = 0; i < ARRAY_SIZE(fsl_i2c_speed_map); i++)
189 if (fsl_i2c_speed_map[i].divider >= divider) {
190 u8 fdr;
191
d8c82db4
TT
192 fdr = fsl_i2c_speed_map[i].fdr;
193 speed = i2c_clk / fsl_i2c_speed_map[i].divider;
ec2c81c5 194 writeb(fdr, &base->fdr); /* set bus speed */
d01ee4db 195
d8c82db4
TT
196 break;
197 }
99404202 198#endif
d8c82db4
TT
199 return speed;
200}
201
dbc82ce3 202#ifndef CONFIG_DM_I2C
a059de11 203static uint get_i2c_clock(int bus)
c9a8b25e
JH
204{
205 if (bus)
609e6ec3 206 return gd->arch.i2c2_clk; /* I2C2 clock */
c9a8b25e 207 else
609e6ec3 208 return gd->arch.i2c1_clk; /* I2C1 clock */
c9a8b25e 209}
dbc82ce3 210#endif
c9a8b25e 211
ec2c81c5 212static int fsl_i2c_fixup(const struct fsl_i2c_base *base)
b8ce3343
CL
213{
214 const unsigned long long timeout = usec2ticks(CONFIG_I2C_MBB_TIMEOUT);
215 unsigned long long timeval = 0;
216 int ret = -1;
a059de11 217 uint flags = 0;
9c3f77eb
CL
218
219#ifdef CONFIG_SYS_FSL_ERRATUM_I2C_A004447
a059de11
MS
220 uint svr = get_svr();
221
9c3f77eb
CL
222 if ((SVR_SOC_VER(svr) == SVR_8548 && IS_SVR_REV(svr, 3, 1)) ||
223 (SVR_REV(svr) <= CONFIG_SYS_FSL_A004447_SVR_REV))
224 flags = I2C_CR_BIT6;
225#endif
b8ce3343 226
ec2c81c5 227 writeb(I2C_CR_MEN | I2C_CR_MSTA, &base->cr);
b8ce3343
CL
228
229 timeval = get_ticks();
ec2c81c5 230 while (!(readb(&base->sr) & I2C_SR_MBB)) {
b8ce3343
CL
231 if ((get_ticks() - timeval) > timeout)
232 goto err;
233 }
234
ec2c81c5 235 if (readb(&base->sr) & I2C_SR_MAL) {
b8ce3343 236 /* SDA is stuck low */
ec2c81c5 237 writeb(0, &base->cr);
b8ce3343 238 udelay(100);
ec2c81c5 239 writeb(I2C_CR_MSTA | flags, &base->cr);
240 writeb(I2C_CR_MEN | I2C_CR_MSTA | flags, &base->cr);
b8ce3343
CL
241 }
242
ec2c81c5 243 readb(&base->dr);
b8ce3343
CL
244
245 timeval = get_ticks();
ec2c81c5 246 while (!(readb(&base->sr) & I2C_SR_MIF)) {
b8ce3343
CL
247 if ((get_ticks() - timeval) > timeout)
248 goto err;
249 }
250 ret = 0;
251
252err:
ec2c81c5 253 writeb(I2C_CR_MEN | flags, &base->cr);
254 writeb(0, &base->sr);
b8ce3343
CL
255 udelay(100);
256
257 return ret;
258}
259
ecf591e3 260static void __i2c_init(const struct fsl_i2c_base *base, int speed, int
261 slaveadd, int i2c_clk, int busnum)
debb7354 262{
b8ce3343
CL
263 const unsigned long long timeout = usec2ticks(CONFIG_I2C_MBB_TIMEOUT);
264 unsigned long long timeval;
be5e6181 265
39df00d9 266#ifdef CONFIG_SYS_I2C_INIT_BOARD
26a33504
RR
267 /* Call board specific i2c bus reset routine before accessing the
268 * environment, which might be in a chip on that bus. For details
269 * about this problem see doc/I2C_Edge_Conditions.
a059de11 270 */
39df00d9
HS
271 i2c_init_board();
272#endif
ec2c81c5 273 writeb(0, &base->cr); /* stop I2C controller */
00f792e0 274 udelay(5); /* let it shutdown in peace */
ecf591e3 275 set_i2c_bus_speed(base, i2c_clk, speed);
ec2c81c5 276 writeb(slaveadd << 1, &base->adr);/* write slave address */
277 writeb(0x0, &base->sr); /* clear status register */
278 writeb(I2C_CR_MEN, &base->cr); /* start I2C controller */
26a33504 279
b8ce3343 280 timeval = get_ticks();
ec2c81c5 281 while (readb(&base->sr) & I2C_SR_MBB) {
b8ce3343
CL
282 if ((get_ticks() - timeval) < timeout)
283 continue;
284
ec2c81c5 285 if (fsl_i2c_fixup(base))
b8ce3343 286 debug("i2c_init: BUS#%d failed to init\n",
ecf591e3 287 busnum);
b8ce3343
CL
288
289 break;
290 }
debb7354
JL
291}
292
a059de11 293static int i2c_wait4bus(const struct fsl_i2c_base *base)
debb7354 294{
f2302d44 295 unsigned long long timeval = get_ticks();
92477a63 296 const unsigned long long timeout = usec2ticks(CONFIG_I2C_MBB_TIMEOUT);
debb7354 297
ec2c81c5 298 while (readb(&base->sr) & I2C_SR_MBB) {
92477a63 299 if ((get_ticks() - timeval) > timeout)
debb7354 300 return -1;
debb7354
JL
301 }
302
5c9efb36 303 return 0;
debb7354
JL
304}
305
d4f422f8 306static int i2c_wait(const struct fsl_i2c_base *base, int write)
debb7354
JL
307{
308 u32 csr;
f2302d44 309 unsigned long long timeval = get_ticks();
92477a63 310 const unsigned long long timeout = usec2ticks(CONFIG_I2C_TIMEOUT);
debb7354
JL
311
312 do {
ec2c81c5 313 csr = readb(&base->sr);
7237c033 314 if (!(csr & I2C_SR_MIF))
debb7354 315 continue;
21f4cbb7 316 /* Read again to allow register to stabilise */
ec2c81c5 317 csr = readb(&base->sr);
debb7354 318
ec2c81c5 319 writeb(0x0, &base->sr);
debb7354 320
7237c033 321 if (csr & I2C_SR_MAL) {
a059de11 322 debug("%s: MAL\n", __func__);
debb7354
JL
323 return -1;
324 }
325
7237c033 326 if (!(csr & I2C_SR_MCF)) {
a059de11 327 debug("%s: unfinished\n", __func__);
debb7354
JL
328 return -1;
329 }
330
1939d969 331 if (write == I2C_WRITE_BIT && (csr & I2C_SR_RXAK)) {
a059de11 332 debug("%s: No RXACK\n", __func__);
debb7354
JL
333 return -1;
334 }
335
336 return 0;
92477a63 337 } while ((get_ticks() - timeval) < timeout);
debb7354 338
a059de11 339 debug("%s: timed out\n", __func__);
debb7354
JL
340 return -1;
341}
342
d4f422f8
MS
343static int i2c_write_addr(const struct fsl_i2c_base *base, u8 dev,
344 u8 dir, int rsta)
debb7354 345{
7237c033
JL
346 writeb(I2C_CR_MEN | I2C_CR_MSTA | I2C_CR_MTX
347 | (rsta ? I2C_CR_RSTA : 0),
ec2c81c5 348 &base->cr);
debb7354 349
ec2c81c5 350 writeb((dev << 1) | dir, &base->dr);
debb7354 351
ecf591e3 352 if (i2c_wait(base, I2C_WRITE_BIT) < 0)
debb7354
JL
353 return 0;
354
355 return 1;
356}
357
d4f422f8
MS
358static int __i2c_write_data(const struct fsl_i2c_base *base, u8 *data,
359 int length)
debb7354
JL
360{
361 int i;
5c9efb36 362
5c9efb36 363 for (i = 0; i < length; i++) {
ec2c81c5 364 writeb(data[i], &base->dr);
debb7354 365
ecf591e3 366 if (i2c_wait(base, I2C_WRITE_BIT) < 0)
debb7354
JL
367 break;
368 }
369
370 return i;
371}
372
d4f422f8
MS
373static int __i2c_read_data(const struct fsl_i2c_base *base, u8 *data,
374 int length)
debb7354
JL
375{
376 int i;
377
7237c033 378 writeb(I2C_CR_MEN | I2C_CR_MSTA | ((length == 1) ? I2C_CR_TXAK : 0),
ec2c81c5 379 &base->cr);
debb7354
JL
380
381 /* dummy read */
ec2c81c5 382 readb(&base->dr);
debb7354 383
5c9efb36 384 for (i = 0; i < length; i++) {
ecf591e3 385 if (i2c_wait(base, I2C_READ_BIT) < 0)
debb7354
JL
386 break;
387
388 /* Generate ack on last next to last byte */
389 if (i == length - 2)
7237c033 390 writeb(I2C_CR_MEN | I2C_CR_MSTA | I2C_CR_TXAK,
ec2c81c5 391 &base->cr);
debb7354 392
d1c9e5b3 393 /* Do not generate stop on last byte */
debb7354 394 if (i == length - 1)
d1c9e5b3 395 writeb(I2C_CR_MEN | I2C_CR_MSTA | I2C_CR_MTX,
ec2c81c5 396 &base->cr);
debb7354 397
ec2c81c5 398 data[i] = readb(&base->dr);
debb7354 399 }
5c9efb36 400
debb7354
JL
401 return i;
402}
403
a059de11
MS
404static int __i2c_read(const struct fsl_i2c_base *base, u8 chip_addr, u8 *offset,
405 int olen, u8 *data, int dlen)
debb7354 406{
2b21e960 407 int ret = -1; /* signal error */
debb7354 408
ecf591e3 409 if (i2c_wait4bus(base) < 0)
b778c1b5
RP
410 return -1;
411
386b2769 412 /* Some drivers use offset lengths in excess of 4 bytes. These drivers
413 * adhere to the following convention:
414 * - the offset length is passed as negative (that is, the absolute
415 * value of olen is the actual offset length)
416 * - the offset itself is passed in data, which is overwritten by the
417 * subsequent read operation
a405764c 418 */
2b21e960 419 if (olen < 0) {
ecf591e3 420 if (i2c_write_addr(base, chip_addr, I2C_WRITE_BIT, 0) != 0)
421 ret = __i2c_write_data(base, data, -olen);
a405764c 422
03a112aa 423 if (ret != -olen)
a405764c 424 return -1;
f6f5f709 425
ecf591e3 426 if (dlen && i2c_write_addr(base, chip_addr,
2b21e960 427 I2C_READ_BIT, 1) != 0)
ecf591e3 428 ret = __i2c_read_data(base, data, dlen);
a405764c 429 } else {
2b21e960 430 if ((!dlen || olen > 0) &&
ecf591e3 431 i2c_write_addr(base, chip_addr, I2C_WRITE_BIT, 0) != 0 &&
432 __i2c_write_data(base, offset, olen) == olen)
2b21e960 433 ret = 0; /* No error so far */
434
ecf591e3 435 if (dlen && i2c_write_addr(base, chip_addr, I2C_READ_BIT,
2b21e960 436 olen ? 1 : 0) != 0)
ecf591e3 437 ret = __i2c_read_data(base, data, dlen);
a405764c 438 }
debb7354 439
ec2c81c5 440 writeb(I2C_CR_MEN, &base->cr);
debb7354 441
ecf591e3 442 if (i2c_wait4bus(base)) /* Wait until STOP */
d1c9e5b3
JT
443 debug("i2c_read: wait4bus timed out\n");
444
2b21e960 445 if (ret == dlen)
446 return 0;
4d45f69e
JL
447
448 return -1;
debb7354
JL
449}
450
a059de11
MS
451static int __i2c_write(const struct fsl_i2c_base *base, u8 chip_addr,
452 u8 *offset, int olen, u8 *data, int dlen)
debb7354 453{
2b21e960 454 int ret = -1; /* signal error */
debb7354 455
ecf591e3 456 if (i2c_wait4bus(base) < 0)
b8ce3343
CL
457 return -1;
458
ecf591e3 459 if (i2c_write_addr(base, chip_addr, I2C_WRITE_BIT, 0) != 0 &&
460 __i2c_write_data(base, offset, olen) == olen) {
461 ret = __i2c_write_data(base, data, dlen);
4d45f69e 462 }
debb7354 463
ec2c81c5 464 writeb(I2C_CR_MEN, &base->cr);
ecf591e3 465 if (i2c_wait4bus(base)) /* Wait until STOP */
21f4cbb7 466 debug("i2c_write: wait4bus timed out\n");
debb7354 467
2b21e960 468 if (ret == dlen)
469 return 0;
4d45f69e
JL
470
471 return -1;
debb7354
JL
472}
473
a059de11 474static int __i2c_probe_chip(const struct fsl_i2c_base *base, uchar chip)
debb7354 475{
a059de11 476 /* For unknown reason the controller will ACK when
f6f5f709
JT
477 * probing for a slave with the same address, so skip
478 * it.
debb7354 479 */
ec2c81c5 480 if (chip == (readb(&base->adr) >> 1))
be5e6181 481 return -1;
be5e6181 482
ecf591e3 483 return __i2c_read(base, chip, 0, 0, NULL, 0);
be5e6181
TT
484}
485
a059de11
MS
486static uint __i2c_set_bus_speed(const struct fsl_i2c_base *base,
487 uint speed, int i2c_clk)
be5e6181 488{
ec2c81c5 489 writeb(0, &base->cr); /* stop controller */
ecf591e3 490 set_i2c_bus_speed(base, i2c_clk, speed);
ec2c81c5 491 writeb(I2C_CR_MEN, &base->cr); /* start controller */
d8c82db4
TT
492
493 return 0;
be5e6181
TT
494}
495
dbc82ce3 496#ifndef CONFIG_DM_I2C
ad7e657c 497static void fsl_i2c_init(struct i2c_adapter *adap, int speed, int slaveadd)
498{
ecf591e3 499 __i2c_init(i2c_base[adap->hwadapnr], speed, slaveadd,
500 get_i2c_clock(adap->hwadapnr), adap->hwadapnr);
ad7e657c 501}
502
a059de11 503static int fsl_i2c_probe_chip(struct i2c_adapter *adap, uchar chip)
ad7e657c 504{
ecf591e3 505 return __i2c_probe_chip(i2c_base[adap->hwadapnr], chip);
ad7e657c 506}
507
a059de11
MS
508static int fsl_i2c_read(struct i2c_adapter *adap, u8 chip_addr, uint offset,
509 int olen, u8 *data, int dlen)
ad7e657c 510{
ecf591e3 511 u8 *o = (u8 *)&offset;
a059de11 512
ecf591e3 513 return __i2c_read(i2c_base[adap->hwadapnr], chip_addr, &o[4 - olen],
514 olen, data, dlen);
ad7e657c 515}
516
a059de11
MS
517static int fsl_i2c_write(struct i2c_adapter *adap, u8 chip_addr, uint offset,
518 int olen, u8 *data, int dlen)
ad7e657c 519{
ecf591e3 520 u8 *o = (u8 *)&offset;
a059de11 521
ecf591e3 522 return __i2c_write(i2c_base[adap->hwadapnr], chip_addr, &o[4 - olen],
523 olen, data, dlen);
ad7e657c 524}
525
a059de11 526static uint fsl_i2c_set_bus_speed(struct i2c_adapter *adap, uint speed)
ad7e657c 527{
ecf591e3 528 return __i2c_set_bus_speed(i2c_base[adap->hwadapnr], speed,
529 get_i2c_clock(adap->hwadapnr));
ad7e657c 530}
531
00f792e0
HS
532/*
533 * Register fsl i2c adapters
534 */
16579ecb 535U_BOOT_I2C_ADAP_COMPLETE(fsl_0, fsl_i2c_init, fsl_i2c_probe_chip, fsl_i2c_read,
00f792e0
HS
536 fsl_i2c_write, fsl_i2c_set_bus_speed,
537 CONFIG_SYS_FSL_I2C_SPEED, CONFIG_SYS_FSL_I2C_SLAVE,
538 0)
539#ifdef CONFIG_SYS_FSL_I2C2_OFFSET
16579ecb 540U_BOOT_I2C_ADAP_COMPLETE(fsl_1, fsl_i2c_init, fsl_i2c_probe_chip, fsl_i2c_read,
00f792e0
HS
541 fsl_i2c_write, fsl_i2c_set_bus_speed,
542 CONFIG_SYS_FSL_I2C2_SPEED, CONFIG_SYS_FSL_I2C2_SLAVE,
543 1)
c1bce4ff 544#endif
a17fd10f 545#ifdef CONFIG_SYS_FSL_I2C3_OFFSET
16579ecb 546U_BOOT_I2C_ADAP_COMPLETE(fsl_2, fsl_i2c_init, fsl_i2c_probe_chip, fsl_i2c_read,
a17fd10f
SL
547 fsl_i2c_write, fsl_i2c_set_bus_speed,
548 CONFIG_SYS_FSL_I2C3_SPEED, CONFIG_SYS_FSL_I2C3_SLAVE,
549 2)
550#endif
551#ifdef CONFIG_SYS_FSL_I2C4_OFFSET
16579ecb 552U_BOOT_I2C_ADAP_COMPLETE(fsl_3, fsl_i2c_init, fsl_i2c_probe_chip, fsl_i2c_read,
a17fd10f
SL
553 fsl_i2c_write, fsl_i2c_set_bus_speed,
554 CONFIG_SYS_FSL_I2C4_SPEED, CONFIG_SYS_FSL_I2C4_SLAVE,
555 3)
556#endif
dbc82ce3 557#else /* CONFIG_DM_I2C */
558static int fsl_i2c_probe_chip(struct udevice *bus, u32 chip_addr,
559 u32 chip_flags)
560{
561 struct fsl_i2c_dev *dev = dev_get_priv(bus);
a059de11 562
dbc82ce3 563 return __i2c_probe_chip(dev->base, chip_addr);
564}
565
a059de11 566static int fsl_i2c_set_bus_speed(struct udevice *bus, uint speed)
dbc82ce3 567{
568 struct fsl_i2c_dev *dev = dev_get_priv(bus);
a059de11 569
dbc82ce3 570 return __i2c_set_bus_speed(dev->base, speed, dev->i2c_clk);
571}
572
573static int fsl_i2c_ofdata_to_platdata(struct udevice *bus)
574{
575 struct fsl_i2c_dev *dev = dev_get_priv(bus);
27059c3e 576 fdt_addr_t addr;
dbc82ce3 577
84a4d34e 578 addr = dev_read_u32_default(bus, "reg", -1);
dbc82ce3 579
84a4d34e 580 dev->base = map_sysmem(CONFIG_SYS_IMMR + addr, sizeof(struct fsl_i2c_base));
dbc82ce3 581
582 if (!dev->base)
583 return -ENOMEM;
584
84a4d34e
MS
585 dev->index = dev_read_u32_default(bus, "cell-index", -1);
586 dev->slaveadd = dev_read_u32_default(bus, "u-boot,i2c-slave-addr",
587 0x7f);
588 dev->speed = dev_read_u32_default(bus, "clock-frequency", 400000);
dbc82ce3 589
590 dev->i2c_clk = dev->index ? gd->arch.i2c2_clk : gd->arch.i2c1_clk;
591
592 return 0;
593}
594
595static int fsl_i2c_probe(struct udevice *bus)
596{
597 struct fsl_i2c_dev *dev = dev_get_priv(bus);
a059de11 598
dbc82ce3 599 __i2c_init(dev->base, dev->speed, dev->slaveadd, dev->i2c_clk,
600 dev->index);
601 return 0;
602}
603
604static int fsl_i2c_xfer(struct udevice *bus, struct i2c_msg *msg, int nmsgs)
605{
606 struct fsl_i2c_dev *dev = dev_get_priv(bus);
607 struct i2c_msg *dmsg, *omsg, dummy;
608
609 memset(&dummy, 0, sizeof(struct i2c_msg));
610
611 /* We expect either two messages (one with an offset and one with the
a059de11
MS
612 * actual data) or one message (just data)
613 */
dbc82ce3 614 if (nmsgs > 2 || nmsgs == 0) {
615 debug("%s: Only one or two messages are supported.", __func__);
616 return -1;
617 }
618
619 omsg = nmsgs == 1 ? &dummy : msg;
620 dmsg = nmsgs == 1 ? msg : msg + 1;
621
622 if (dmsg->flags & I2C_M_RD)
623 return __i2c_read(dev->base, dmsg->addr, omsg->buf, omsg->len,
624 dmsg->buf, dmsg->len);
625 else
626 return __i2c_write(dev->base, dmsg->addr, omsg->buf, omsg->len,
627 dmsg->buf, dmsg->len);
628}
629
630static const struct dm_i2c_ops fsl_i2c_ops = {
631 .xfer = fsl_i2c_xfer,
632 .probe_chip = fsl_i2c_probe_chip,
633 .set_bus_speed = fsl_i2c_set_bus_speed,
634};
635
636static const struct udevice_id fsl_i2c_ids[] = {
637 { .compatible = "fsl-i2c", },
638 { /* sentinel */ }
639};
640
641U_BOOT_DRIVER(i2c_fsl) = {
642 .name = "i2c_fsl",
643 .id = UCLASS_I2C,
644 .of_match = fsl_i2c_ids,
645 .probe = fsl_i2c_probe,
646 .ofdata_to_platdata = fsl_i2c_ofdata_to_platdata,
647 .priv_auto_alloc_size = sizeof(struct fsl_i2c_dev),
648 .ops = &fsl_i2c_ops,
649};
650
651#endif /* CONFIG_DM_I2C */