blob: bf8d52d5ad5ffb2c48debe4c72948e1f1b7a05d1 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0
Jon Loeliger7237c032006-10-19 11:02:16 -05002/*
Timur Tabi92477a62009-09-04 16:28:35 -05003 * Copyright 2006,2009 Freescale Semiconductor, Inc.
Jon Loeliger7237c032006-10-19 11:02:16 -05004 *
Heiko Schocher00f792e2012-10-24 13:48:22 +02005 * 2012, Heiko Schocher, DENX Software Engineering, hs@denx.de.
6 * Changes for multibus/multiadapter I2C support.
Jon Loeliger7237c032006-10-19 11:02:16 -05007 */
8
Jon Loeliger7237c032006-10-19 11:02:16 -05009#include <common.h>
Jon Loeliger4d45f692006-10-19 12:02:24 -050010#include <command.h>
Jon Loeliger20476722006-10-20 15:50:15 -050011#include <i2c.h> /* Functional interface */
Jon Loeliger7237c032006-10-19 11:02:16 -050012#include <asm/io.h>
Jon Loeliger20476722006-10-20 15:50:15 -050013#include <asm/fsl_i2c.h> /* HW definitions */
Mario Sixe5c762f2018-03-28 14:37:44 +020014#include <clk.h>
mario.six@gdsys.ccdbc82ce2016-04-25 08:31:09 +020015#include <dm.h>
16#include <mapmem.h>
Jon Loeliger7237c032006-10-19 11:02:16 -050017
Timur Tabi92477a62009-09-04 16:28:35 -050018/* 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
Shaveta Leekha6dd38cc2014-11-03 10:43:14 +053031#define CONFIG_I2C_TIMEOUT 100000
Timur Tabi92477a62009-09-04 16:28:35 -050032#endif
Jon Loeliger7237c032006-10-19 11:02:16 -050033
Joakim Tjernlund1939d962006-11-28 16:17:27 -060034#define I2C_READ_BIT 1
35#define I2C_WRITE_BIT 0
36
Timur Tabid8c82db2008-03-14 17:45:29 -050037DECLARE_GLOBAL_DATA_PTR;
38
mario.six@gdsys.ccdbc82ce2016-04-25 08:31:09 +020039#ifndef CONFIG_DM_I2C
mario.six@gdsys.ccec2c81c2016-04-25 08:31:01 +020040static const struct fsl_i2c_base *i2c_base[4] = {
41 (struct fsl_i2c_base *)(CONFIG_SYS_IMMR + CONFIG_SYS_FSL_I2C_OFFSET),
Heiko Schocher00f792e2012-10-24 13:48:22 +020042#ifdef CONFIG_SYS_FSL_I2C2_OFFSET
mario.six@gdsys.ccec2c81c2016-04-25 08:31:01 +020043 (struct fsl_i2c_base *)(CONFIG_SYS_IMMR + CONFIG_SYS_FSL_I2C2_OFFSET),
Shengzhou Liua17fd102014-07-07 12:17:48 +080044#endif
45#ifdef CONFIG_SYS_FSL_I2C3_OFFSET
mario.six@gdsys.ccec2c81c2016-04-25 08:31:01 +020046 (struct fsl_i2c_base *)(CONFIG_SYS_IMMR + CONFIG_SYS_FSL_I2C3_OFFSET),
Shengzhou Liua17fd102014-07-07 12:17:48 +080047#endif
48#ifdef CONFIG_SYS_FSL_I2C4_OFFSET
mario.six@gdsys.ccec2c81c2016-04-25 08:31:01 +020049 (struct fsl_i2c_base *)(CONFIG_SYS_IMMR + CONFIG_SYS_FSL_I2C4_OFFSET)
Timur Tabibe5e6182006-11-03 19:15:00 -060050#endif
51};
mario.six@gdsys.ccdbc82ce2016-04-25 08:31:09 +020052#endif
Jon Loeliger7237c032006-10-19 11:02:16 -050053
Timur Tabid8c82db2008-03-14 17:45:29 -050054/* I2C speed map for a DFSR value of 1 */
55
Tom Rini645cb462017-02-09 15:40:16 -050056#ifdef __M68K__
Timur Tabid8c82db2008-03-14 17:45:29 -050057/*
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"
TsiChung Liew5d9a5ef2008-08-19 00:56:46 +060078 *
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 *
Timur Tabid8c82db2008-03-14 17:45:29 -050083 */
84static const struct {
85 unsigned short divider;
Timur Tabid8c82db2008-03-14 17:45:29 -050086 u8 fdr;
87} fsl_i2c_speed_map[] = {
TsiChung Liew5d9a5ef2008-08-19 00:56:46 +060088 {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}
Timur Tabid8c82db2008-03-14 17:45:29 -0500105};
Tom Rini645cb462017-02-09 15:40:16 -0500106#endif
Timur Tabid8c82db2008-03-14 17:45:29 -0500107
108/**
109 * Set the I2C bus speed for a given I2C device
110 *
mario.six@gdsys.ccec2c81c2016-04-25 08:31:01 +0200111 * @param base: the I2C device registers
Timur Tabid8c82db2008-03-14 17:45:29 -0500112 * @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 */
Mario Sixa059de12018-01-15 11:08:07 +0100119static uint set_i2c_bus_speed(const struct fsl_i2c_base *base,
120 uint i2c_clk, uint speed)
Timur Tabid8c82db2008-03-14 17:45:29 -0500121{
Mario Sixa059de12018-01-15 11:08:07 +0100122 ushort divider = min(i2c_clk / speed, (uint)USHRT_MAX);
Timur Tabid8c82db2008-03-14 17:45:29 -0500123
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 */
Joakim Tjernlund99404202009-09-17 11:07:17 +0200130#ifdef __PPC__
131 u8 dfsr, fdr = 0x31; /* Default if no FDR found */
132 /* a, b and dfsr matches identifiers A,B and C respectively in AN2919 */
Mario Sixa059de12018-01-15 11:08:07 +0100133 ushort a, b, ga, gb;
134 ulong c_div, est_div;
Joakim Tjernlund99404202009-09-17 11:07:17 +0200135
136#ifdef CONFIG_FSL_I2C_CUSTOM_DFSR
137 dfsr = CONFIG_FSL_I2C_CUSTOM_DFSR;
138#else
139 /* Condition 1: dfsr <= 50/T */
140 dfsr = (5 * (i2c_clk / 1000)) / 100000;
141#endif
142#ifdef CONFIG_FSL_I2C_CUSTOM_FDR
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;
Mario Sixa059de12018-01-15 11:08:07 +0100154 c_div = b * (a + ((3 * dfsr) / b) * 2);
155 if (c_div > divider && c_div < est_div) {
156 ushort bin_gb, bin_ga;
Joakim Tjernlund99404202009-09-17 11:07:17 +0200157
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;
Mario Sixa059de12018-01-15 11:08:07 +0100163
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
Joakim Tjernlund99404202009-09-17 11:07:17 +0200169 /* 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 }
Mario Sixa059de12018-01-15 11:08:07 +0100180 debug("divider: %d, est_div: %ld, DFSR: %d\n", divider, est_div, dfsr);
181 debug("FDR: 0x%.2x, speed: %d\n", fdr, speed);
Joakim Tjernlund99404202009-09-17 11:07:17 +0200182#endif
mario.six@gdsys.ccec2c81c2016-04-25 08:31:01 +0200183 writeb(dfsr, &base->dfsrr); /* set default filter */
184 writeb(fdr, &base->fdr); /* set bus speed */
Joakim Tjernlund99404202009-09-17 11:07:17 +0200185#else
Mario Sixa059de12018-01-15 11:08:07 +0100186 uint i;
Timur Tabid8c82db2008-03-14 17:45:29 -0500187
188 for (i = 0; i < ARRAY_SIZE(fsl_i2c_speed_map); i++)
189 if (fsl_i2c_speed_map[i].divider >= divider) {
TsiChung Liew5d9a5ef2008-08-19 00:56:46 +0600190 u8 fdr;
Joakim Tjernlund99404202009-09-17 11:07:17 +0200191
Joakim Tjernlundd01ee4d2009-09-17 11:07:16 +0200192 fdr = fsl_i2c_speed_map[i].fdr;
193 speed = i2c_clk / fsl_i2c_speed_map[i].divider;
mario.six@gdsys.ccec2c81c2016-04-25 08:31:01 +0200194 writeb(fdr, &base->fdr); /* set bus speed */
Joakim Tjernlundd01ee4d2009-09-17 11:07:16 +0200195
Timur Tabid8c82db2008-03-14 17:45:29 -0500196 break;
197 }
Joakim Tjernlund99404202009-09-17 11:07:17 +0200198#endif
Timur Tabid8c82db2008-03-14 17:45:29 -0500199 return speed;
200}
201
mario.six@gdsys.ccdbc82ce2016-04-25 08:31:09 +0200202#ifndef CONFIG_DM_I2C
Mario Sixa059de12018-01-15 11:08:07 +0100203static uint get_i2c_clock(int bus)
Jerry Huangc9a8b252011-10-26 15:29:38 +0000204{
205 if (bus)
Simon Glass609e6ec2012-12-13 20:48:49 +0000206 return gd->arch.i2c2_clk; /* I2C2 clock */
Jerry Huangc9a8b252011-10-26 15:29:38 +0000207 else
Simon Glass609e6ec2012-12-13 20:48:49 +0000208 return gd->arch.i2c1_clk; /* I2C1 clock */
Jerry Huangc9a8b252011-10-26 15:29:38 +0000209}
mario.six@gdsys.ccdbc82ce2016-04-25 08:31:09 +0200210#endif
Jerry Huangc9a8b252011-10-26 15:29:38 +0000211
mario.six@gdsys.ccec2c81c2016-04-25 08:31:01 +0200212static int fsl_i2c_fixup(const struct fsl_i2c_base *base)
Chunhe Lanb8ce3342013-08-16 15:10:36 +0800213{
214 const unsigned long long timeout = usec2ticks(CONFIG_I2C_MBB_TIMEOUT);
215 unsigned long long timeval = 0;
216 int ret = -1;
Mario Sixa059de12018-01-15 11:08:07 +0100217 uint flags = 0;
Chunhe Lan9c3f77e2013-08-16 15:10:37 +0800218
219#ifdef CONFIG_SYS_FSL_ERRATUM_I2C_A004447
Mario Sixa059de12018-01-15 11:08:07 +0100220 uint svr = get_svr();
221
Chunhe Lan9c3f77e2013-08-16 15:10:37 +0800222 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
Chunhe Lanb8ce3342013-08-16 15:10:36 +0800226
mario.six@gdsys.ccec2c81c2016-04-25 08:31:01 +0200227 writeb(I2C_CR_MEN | I2C_CR_MSTA, &base->cr);
Chunhe Lanb8ce3342013-08-16 15:10:36 +0800228
229 timeval = get_ticks();
mario.six@gdsys.ccec2c81c2016-04-25 08:31:01 +0200230 while (!(readb(&base->sr) & I2C_SR_MBB)) {
Chunhe Lanb8ce3342013-08-16 15:10:36 +0800231 if ((get_ticks() - timeval) > timeout)
232 goto err;
233 }
234
mario.six@gdsys.ccec2c81c2016-04-25 08:31:01 +0200235 if (readb(&base->sr) & I2C_SR_MAL) {
Chunhe Lanb8ce3342013-08-16 15:10:36 +0800236 /* SDA is stuck low */
mario.six@gdsys.ccec2c81c2016-04-25 08:31:01 +0200237 writeb(0, &base->cr);
Chunhe Lanb8ce3342013-08-16 15:10:36 +0800238 udelay(100);
mario.six@gdsys.ccec2c81c2016-04-25 08:31:01 +0200239 writeb(I2C_CR_MSTA | flags, &base->cr);
240 writeb(I2C_CR_MEN | I2C_CR_MSTA | flags, &base->cr);
Chunhe Lanb8ce3342013-08-16 15:10:36 +0800241 }
242
mario.six@gdsys.ccec2c81c2016-04-25 08:31:01 +0200243 readb(&base->dr);
Chunhe Lanb8ce3342013-08-16 15:10:36 +0800244
245 timeval = get_ticks();
mario.six@gdsys.ccec2c81c2016-04-25 08:31:01 +0200246 while (!(readb(&base->sr) & I2C_SR_MIF)) {
Chunhe Lanb8ce3342013-08-16 15:10:36 +0800247 if ((get_ticks() - timeval) > timeout)
248 goto err;
249 }
250 ret = 0;
251
252err:
mario.six@gdsys.ccec2c81c2016-04-25 08:31:01 +0200253 writeb(I2C_CR_MEN | flags, &base->cr);
254 writeb(0, &base->sr);
Chunhe Lanb8ce3342013-08-16 15:10:36 +0800255 udelay(100);
256
257 return ret;
258}
259
mario.six@gdsys.ccecf591e2016-04-25 08:31:08 +0200260static void __i2c_init(const struct fsl_i2c_base *base, int speed, int
261 slaveadd, int i2c_clk, int busnum)
Jon Loeliger7237c032006-10-19 11:02:16 -0500262{
Chunhe Lanb8ce3342013-08-16 15:10:36 +0800263 const unsigned long long timeout = usec2ticks(CONFIG_I2C_MBB_TIMEOUT);
264 unsigned long long timeval;
Jon Loeliger7237c032006-10-19 11:02:16 -0500265
Heiko Schocher39df00d2009-07-09 12:04:26 +0200266#ifdef CONFIG_SYS_I2C_INIT_BOARD
Richard Retanubun26a33502010-04-12 15:08:17 -0400267 /* 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.
Mario Sixa059de12018-01-15 11:08:07 +0100270 */
Heiko Schocher39df00d2009-07-09 12:04:26 +0200271 i2c_init_board();
272#endif
mario.six@gdsys.ccec2c81c2016-04-25 08:31:01 +0200273 writeb(0, &base->cr); /* stop I2C controller */
Heiko Schocher00f792e2012-10-24 13:48:22 +0200274 udelay(5); /* let it shutdown in peace */
mario.six@gdsys.ccecf591e2016-04-25 08:31:08 +0200275 set_i2c_bus_speed(base, i2c_clk, speed);
mario.six@gdsys.ccec2c81c2016-04-25 08:31:01 +0200276 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 */
Richard Retanubun26a33502010-04-12 15:08:17 -0400279
Chunhe Lanb8ce3342013-08-16 15:10:36 +0800280 timeval = get_ticks();
mario.six@gdsys.ccec2c81c2016-04-25 08:31:01 +0200281 while (readb(&base->sr) & I2C_SR_MBB) {
Chunhe Lanb8ce3342013-08-16 15:10:36 +0800282 if ((get_ticks() - timeval) < timeout)
283 continue;
284
mario.six@gdsys.ccec2c81c2016-04-25 08:31:01 +0200285 if (fsl_i2c_fixup(base))
Chunhe Lanb8ce3342013-08-16 15:10:36 +0800286 debug("i2c_init: BUS#%d failed to init\n",
mario.six@gdsys.ccecf591e2016-04-25 08:31:08 +0200287 busnum);
Chunhe Lanb8ce3342013-08-16 15:10:36 +0800288
289 break;
290 }
Jon Loeliger7237c032006-10-19 11:02:16 -0500291}
292
Mario Sixa059de12018-01-15 11:08:07 +0100293static int i2c_wait4bus(const struct fsl_i2c_base *base)
Jon Loeliger7237c032006-10-19 11:02:16 -0500294{
Stefan Roesef2302d42008-08-06 14:05:38 +0200295 unsigned long long timeval = get_ticks();
Timur Tabi92477a62009-09-04 16:28:35 -0500296 const unsigned long long timeout = usec2ticks(CONFIG_I2C_MBB_TIMEOUT);
Jon Loeliger7237c032006-10-19 11:02:16 -0500297
mario.six@gdsys.ccec2c81c2016-04-25 08:31:01 +0200298 while (readb(&base->sr) & I2C_SR_MBB) {
Timur Tabi92477a62009-09-04 16:28:35 -0500299 if ((get_ticks() - timeval) > timeout)
Jon Loeliger7237c032006-10-19 11:02:16 -0500300 return -1;
Jon Loeliger7237c032006-10-19 11:02:16 -0500301 }
302
303 return 0;
304}
305
Mario Sixd4f422f2018-01-15 11:08:08 +0100306static int i2c_wait(const struct fsl_i2c_base *base, int write)
Jon Loeliger7237c032006-10-19 11:02:16 -0500307{
308 u32 csr;
Stefan Roesef2302d42008-08-06 14:05:38 +0200309 unsigned long long timeval = get_ticks();
Timur Tabi92477a62009-09-04 16:28:35 -0500310 const unsigned long long timeout = usec2ticks(CONFIG_I2C_TIMEOUT);
Jon Loeliger7237c032006-10-19 11:02:16 -0500311
312 do {
mario.six@gdsys.ccec2c81c2016-04-25 08:31:01 +0200313 csr = readb(&base->sr);
Jon Loeliger7237c032006-10-19 11:02:16 -0500314 if (!(csr & I2C_SR_MIF))
315 continue;
Joakim Tjernlund21f4cbb2009-09-17 11:07:15 +0200316 /* Read again to allow register to stabilise */
mario.six@gdsys.ccec2c81c2016-04-25 08:31:01 +0200317 csr = readb(&base->sr);
Jon Loeliger7237c032006-10-19 11:02:16 -0500318
mario.six@gdsys.ccec2c81c2016-04-25 08:31:01 +0200319 writeb(0x0, &base->sr);
Jon Loeliger7237c032006-10-19 11:02:16 -0500320
321 if (csr & I2C_SR_MAL) {
Mario Sixa059de12018-01-15 11:08:07 +0100322 debug("%s: MAL\n", __func__);
Jon Loeliger7237c032006-10-19 11:02:16 -0500323 return -1;
324 }
325
326 if (!(csr & I2C_SR_MCF)) {
Mario Sixa059de12018-01-15 11:08:07 +0100327 debug("%s: unfinished\n", __func__);
Jon Loeliger7237c032006-10-19 11:02:16 -0500328 return -1;
329 }
330
Joakim Tjernlund1939d962006-11-28 16:17:27 -0600331 if (write == I2C_WRITE_BIT && (csr & I2C_SR_RXAK)) {
Mario Sixa059de12018-01-15 11:08:07 +0100332 debug("%s: No RXACK\n", __func__);
Jon Loeliger7237c032006-10-19 11:02:16 -0500333 return -1;
334 }
335
336 return 0;
Timur Tabi92477a62009-09-04 16:28:35 -0500337 } while ((get_ticks() - timeval) < timeout);
Jon Loeliger7237c032006-10-19 11:02:16 -0500338
Mario Sixa059de12018-01-15 11:08:07 +0100339 debug("%s: timed out\n", __func__);
Jon Loeliger7237c032006-10-19 11:02:16 -0500340 return -1;
341}
342
Mario Sixd4f422f2018-01-15 11:08:08 +0100343static int i2c_write_addr(const struct fsl_i2c_base *base, u8 dev,
344 u8 dir, int rsta)
Jon Loeliger7237c032006-10-19 11:02:16 -0500345{
346 writeb(I2C_CR_MEN | I2C_CR_MSTA | I2C_CR_MTX
347 | (rsta ? I2C_CR_RSTA : 0),
mario.six@gdsys.ccec2c81c2016-04-25 08:31:01 +0200348 &base->cr);
Jon Loeliger7237c032006-10-19 11:02:16 -0500349
mario.six@gdsys.ccec2c81c2016-04-25 08:31:01 +0200350 writeb((dev << 1) | dir, &base->dr);
Jon Loeliger7237c032006-10-19 11:02:16 -0500351
mario.six@gdsys.ccecf591e2016-04-25 08:31:08 +0200352 if (i2c_wait(base, I2C_WRITE_BIT) < 0)
Jon Loeliger7237c032006-10-19 11:02:16 -0500353 return 0;
354
355 return 1;
356}
357
Mario Sixd4f422f2018-01-15 11:08:08 +0100358static int __i2c_write_data(const struct fsl_i2c_base *base, u8 *data,
359 int length)
Jon Loeliger7237c032006-10-19 11:02:16 -0500360{
361 int i;
362
Jon Loeliger7237c032006-10-19 11:02:16 -0500363 for (i = 0; i < length; i++) {
mario.six@gdsys.ccec2c81c2016-04-25 08:31:01 +0200364 writeb(data[i], &base->dr);
Jon Loeliger7237c032006-10-19 11:02:16 -0500365
mario.six@gdsys.ccecf591e2016-04-25 08:31:08 +0200366 if (i2c_wait(base, I2C_WRITE_BIT) < 0)
Jon Loeliger7237c032006-10-19 11:02:16 -0500367 break;
368 }
369
370 return i;
371}
372
Mario Sixd4f422f2018-01-15 11:08:08 +0100373static int __i2c_read_data(const struct fsl_i2c_base *base, u8 *data,
374 int length)
Jon Loeliger7237c032006-10-19 11:02:16 -0500375{
376 int i;
377
378 writeb(I2C_CR_MEN | I2C_CR_MSTA | ((length == 1) ? I2C_CR_TXAK : 0),
mario.six@gdsys.ccec2c81c2016-04-25 08:31:01 +0200379 &base->cr);
Jon Loeliger7237c032006-10-19 11:02:16 -0500380
381 /* dummy read */
mario.six@gdsys.ccec2c81c2016-04-25 08:31:01 +0200382 readb(&base->dr);
Jon Loeliger7237c032006-10-19 11:02:16 -0500383
384 for (i = 0; i < length; i++) {
mario.six@gdsys.ccecf591e2016-04-25 08:31:08 +0200385 if (i2c_wait(base, I2C_READ_BIT) < 0)
Jon Loeliger7237c032006-10-19 11:02:16 -0500386 break;
387
388 /* Generate ack on last next to last byte */
389 if (i == length - 2)
390 writeb(I2C_CR_MEN | I2C_CR_MSTA | I2C_CR_TXAK,
mario.six@gdsys.ccec2c81c2016-04-25 08:31:01 +0200391 &base->cr);
Jon Loeliger7237c032006-10-19 11:02:16 -0500392
Joakim Tjernlundd1c9e5b2009-09-22 13:40:44 +0200393 /* Do not generate stop on last byte */
Jon Loeliger7237c032006-10-19 11:02:16 -0500394 if (i == length - 1)
Joakim Tjernlundd1c9e5b2009-09-22 13:40:44 +0200395 writeb(I2C_CR_MEN | I2C_CR_MSTA | I2C_CR_MTX,
mario.six@gdsys.ccec2c81c2016-04-25 08:31:01 +0200396 &base->cr);
Jon Loeliger7237c032006-10-19 11:02:16 -0500397
mario.six@gdsys.ccec2c81c2016-04-25 08:31:01 +0200398 data[i] = readb(&base->dr);
Jon Loeliger7237c032006-10-19 11:02:16 -0500399 }
400
401 return i;
402}
403
Mario Sixa059de12018-01-15 11:08:07 +0100404static int __i2c_read(const struct fsl_i2c_base *base, u8 chip_addr, u8 *offset,
405 int olen, u8 *data, int dlen)
Jon Loeliger7237c032006-10-19 11:02:16 -0500406{
mario.six@gdsys.cc2b21e962016-04-25 08:31:02 +0200407 int ret = -1; /* signal error */
Jon Loeliger7237c032006-10-19 11:02:16 -0500408
mario.six@gdsys.ccecf591e2016-04-25 08:31:08 +0200409 if (i2c_wait4bus(base) < 0)
Reinhard Pfaub778c1b2013-06-26 15:55:14 +0200410 return -1;
411
mario.six@gdsys.cc386b2762016-04-25 08:31:03 +0200412 /* 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
Shaveta Leekhaa4057642014-04-24 14:51:23 +0530418 */
mario.six@gdsys.cc2b21e962016-04-25 08:31:02 +0200419 if (olen < 0) {
mario.six@gdsys.ccecf591e2016-04-25 08:31:08 +0200420 if (i2c_write_addr(base, chip_addr, I2C_WRITE_BIT, 0) != 0)
421 ret = __i2c_write_data(base, data, -olen);
Joakim Tjernlundf6f5f702007-01-31 11:04:19 +0100422
mario.six@gdsys.cc03a112a2016-04-25 08:31:04 +0200423 if (ret != -olen)
Shaveta Leekhaa4057642014-04-24 14:51:23 +0530424 return -1;
425
mario.six@gdsys.ccecf591e2016-04-25 08:31:08 +0200426 if (dlen && i2c_write_addr(base, chip_addr,
mario.six@gdsys.cc2b21e962016-04-25 08:31:02 +0200427 I2C_READ_BIT, 1) != 0)
mario.six@gdsys.ccecf591e2016-04-25 08:31:08 +0200428 ret = __i2c_read_data(base, data, dlen);
Shaveta Leekhaa4057642014-04-24 14:51:23 +0530429 } else {
mario.six@gdsys.cc2b21e962016-04-25 08:31:02 +0200430 if ((!dlen || olen > 0) &&
mario.six@gdsys.ccecf591e2016-04-25 08:31:08 +0200431 i2c_write_addr(base, chip_addr, I2C_WRITE_BIT, 0) != 0 &&
432 __i2c_write_data(base, offset, olen) == olen)
mario.six@gdsys.cc2b21e962016-04-25 08:31:02 +0200433 ret = 0; /* No error so far */
Shaveta Leekhaa4057642014-04-24 14:51:23 +0530434
mario.six@gdsys.ccecf591e2016-04-25 08:31:08 +0200435 if (dlen && i2c_write_addr(base, chip_addr, I2C_READ_BIT,
mario.six@gdsys.cc2b21e962016-04-25 08:31:02 +0200436 olen ? 1 : 0) != 0)
mario.six@gdsys.ccecf591e2016-04-25 08:31:08 +0200437 ret = __i2c_read_data(base, data, dlen);
Shaveta Leekhaa4057642014-04-24 14:51:23 +0530438 }
Jon Loeliger7237c032006-10-19 11:02:16 -0500439
mario.six@gdsys.ccec2c81c2016-04-25 08:31:01 +0200440 writeb(I2C_CR_MEN, &base->cr);
Jon Loeliger7237c032006-10-19 11:02:16 -0500441
mario.six@gdsys.ccecf591e2016-04-25 08:31:08 +0200442 if (i2c_wait4bus(base)) /* Wait until STOP */
Joakim Tjernlundd1c9e5b2009-09-22 13:40:44 +0200443 debug("i2c_read: wait4bus timed out\n");
444
mario.six@gdsys.cc2b21e962016-04-25 08:31:02 +0200445 if (ret == dlen)
446 return 0;
Jon Loeliger4d45f692006-10-19 12:02:24 -0500447
448 return -1;
Jon Loeliger7237c032006-10-19 11:02:16 -0500449}
450
Mario Sixa059de12018-01-15 11:08:07 +0100451static int __i2c_write(const struct fsl_i2c_base *base, u8 chip_addr,
452 u8 *offset, int olen, u8 *data, int dlen)
Jon Loeliger7237c032006-10-19 11:02:16 -0500453{
mario.six@gdsys.cc2b21e962016-04-25 08:31:02 +0200454 int ret = -1; /* signal error */
Jon Loeliger7237c032006-10-19 11:02:16 -0500455
mario.six@gdsys.ccecf591e2016-04-25 08:31:08 +0200456 if (i2c_wait4bus(base) < 0)
Chunhe Lanb8ce3342013-08-16 15:10:36 +0800457 return -1;
458
mario.six@gdsys.ccecf591e2016-04-25 08:31:08 +0200459 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);
Jon Loeliger4d45f692006-10-19 12:02:24 -0500462 }
Jon Loeliger7237c032006-10-19 11:02:16 -0500463
mario.six@gdsys.ccec2c81c2016-04-25 08:31:01 +0200464 writeb(I2C_CR_MEN, &base->cr);
mario.six@gdsys.ccecf591e2016-04-25 08:31:08 +0200465 if (i2c_wait4bus(base)) /* Wait until STOP */
Joakim Tjernlund21f4cbb2009-09-17 11:07:15 +0200466 debug("i2c_write: wait4bus timed out\n");
Jon Loeliger7237c032006-10-19 11:02:16 -0500467
mario.six@gdsys.cc2b21e962016-04-25 08:31:02 +0200468 if (ret == dlen)
469 return 0;
Jon Loeliger4d45f692006-10-19 12:02:24 -0500470
471 return -1;
Jon Loeliger7237c032006-10-19 11:02:16 -0500472}
473
Mario Sixa059de12018-01-15 11:08:07 +0100474static int __i2c_probe_chip(const struct fsl_i2c_base *base, uchar chip)
Jon Loeliger7237c032006-10-19 11:02:16 -0500475{
Mario Sixa059de12018-01-15 11:08:07 +0100476 /* For unknown reason the controller will ACK when
Joakim Tjernlundf6f5f702007-01-31 11:04:19 +0100477 * probing for a slave with the same address, so skip
478 * it.
Jon Loeliger7237c032006-10-19 11:02:16 -0500479 */
mario.six@gdsys.ccec2c81c2016-04-25 08:31:01 +0200480 if (chip == (readb(&base->adr) >> 1))
Joakim Tjernlundf6f5f702007-01-31 11:04:19 +0100481 return -1;
Jon Loeliger7237c032006-10-19 11:02:16 -0500482
mario.six@gdsys.ccecf591e2016-04-25 08:31:08 +0200483 return __i2c_read(base, chip, 0, 0, NULL, 0);
Jon Loeliger7237c032006-10-19 11:02:16 -0500484}
485
Mario Sixa059de12018-01-15 11:08:07 +0100486static uint __i2c_set_bus_speed(const struct fsl_i2c_base *base,
487 uint speed, int i2c_clk)
Timur Tabibe5e6182006-11-03 19:15:00 -0600488{
mario.six@gdsys.ccec2c81c2016-04-25 08:31:01 +0200489 writeb(0, &base->cr); /* stop controller */
mario.six@gdsys.ccecf591e2016-04-25 08:31:08 +0200490 set_i2c_bus_speed(base, i2c_clk, speed);
mario.six@gdsys.ccec2c81c2016-04-25 08:31:01 +0200491 writeb(I2C_CR_MEN, &base->cr); /* start controller */
Timur Tabid8c82db2008-03-14 17:45:29 -0500492
493 return 0;
Timur Tabibe5e6182006-11-03 19:15:00 -0600494}
495
mario.six@gdsys.ccdbc82ce2016-04-25 08:31:09 +0200496#ifndef CONFIG_DM_I2C
mario.six@gdsys.ccad7e6572016-04-25 08:31:07 +0200497static void fsl_i2c_init(struct i2c_adapter *adap, int speed, int slaveadd)
498{
mario.six@gdsys.ccecf591e2016-04-25 08:31:08 +0200499 __i2c_init(i2c_base[adap->hwadapnr], speed, slaveadd,
500 get_i2c_clock(adap->hwadapnr), adap->hwadapnr);
mario.six@gdsys.ccad7e6572016-04-25 08:31:07 +0200501}
502
Mario Sixa059de12018-01-15 11:08:07 +0100503static int fsl_i2c_probe_chip(struct i2c_adapter *adap, uchar chip)
mario.six@gdsys.ccad7e6572016-04-25 08:31:07 +0200504{
mario.six@gdsys.ccecf591e2016-04-25 08:31:08 +0200505 return __i2c_probe_chip(i2c_base[adap->hwadapnr], chip);
mario.six@gdsys.ccad7e6572016-04-25 08:31:07 +0200506}
507
Mario Sixa059de12018-01-15 11:08:07 +0100508static int fsl_i2c_read(struct i2c_adapter *adap, u8 chip_addr, uint offset,
509 int olen, u8 *data, int dlen)
mario.six@gdsys.ccad7e6572016-04-25 08:31:07 +0200510{
mario.six@gdsys.ccecf591e2016-04-25 08:31:08 +0200511 u8 *o = (u8 *)&offset;
Mario Sixa059de12018-01-15 11:08:07 +0100512
mario.six@gdsys.ccecf591e2016-04-25 08:31:08 +0200513 return __i2c_read(i2c_base[adap->hwadapnr], chip_addr, &o[4 - olen],
514 olen, data, dlen);
mario.six@gdsys.ccad7e6572016-04-25 08:31:07 +0200515}
516
Mario Sixa059de12018-01-15 11:08:07 +0100517static int fsl_i2c_write(struct i2c_adapter *adap, u8 chip_addr, uint offset,
518 int olen, u8 *data, int dlen)
mario.six@gdsys.ccad7e6572016-04-25 08:31:07 +0200519{
mario.six@gdsys.ccecf591e2016-04-25 08:31:08 +0200520 u8 *o = (u8 *)&offset;
Mario Sixa059de12018-01-15 11:08:07 +0100521
mario.six@gdsys.ccecf591e2016-04-25 08:31:08 +0200522 return __i2c_write(i2c_base[adap->hwadapnr], chip_addr, &o[4 - olen],
523 olen, data, dlen);
mario.six@gdsys.ccad7e6572016-04-25 08:31:07 +0200524}
525
Mario Sixa059de12018-01-15 11:08:07 +0100526static uint fsl_i2c_set_bus_speed(struct i2c_adapter *adap, uint speed)
mario.six@gdsys.ccad7e6572016-04-25 08:31:07 +0200527{
mario.six@gdsys.ccecf591e2016-04-25 08:31:08 +0200528 return __i2c_set_bus_speed(i2c_base[adap->hwadapnr], speed,
529 get_i2c_clock(adap->hwadapnr));
mario.six@gdsys.ccad7e6572016-04-25 08:31:07 +0200530}
531
Heiko Schocher00f792e2012-10-24 13:48:22 +0200532/*
533 * Register fsl i2c adapters
534 */
mario.six@gdsys.cc16579ec2016-04-25 08:31:05 +0200535U_BOOT_I2C_ADAP_COMPLETE(fsl_0, fsl_i2c_init, fsl_i2c_probe_chip, fsl_i2c_read,
Heiko Schocher00f792e2012-10-24 13:48:22 +0200536 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
mario.six@gdsys.cc16579ec2016-04-25 08:31:05 +0200540U_BOOT_I2C_ADAP_COMPLETE(fsl_1, fsl_i2c_init, fsl_i2c_probe_chip, fsl_i2c_read,
Heiko Schocher00f792e2012-10-24 13:48:22 +0200541 fsl_i2c_write, fsl_i2c_set_bus_speed,
542 CONFIG_SYS_FSL_I2C2_SPEED, CONFIG_SYS_FSL_I2C2_SLAVE,
543 1)
Heiko Schocherc1bce4f2009-02-24 11:30:37 +0100544#endif
Shengzhou Liua17fd102014-07-07 12:17:48 +0800545#ifdef CONFIG_SYS_FSL_I2C3_OFFSET
mario.six@gdsys.cc16579ec2016-04-25 08:31:05 +0200546U_BOOT_I2C_ADAP_COMPLETE(fsl_2, fsl_i2c_init, fsl_i2c_probe_chip, fsl_i2c_read,
Shengzhou Liua17fd102014-07-07 12:17:48 +0800547 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
mario.six@gdsys.cc16579ec2016-04-25 08:31:05 +0200552U_BOOT_I2C_ADAP_COMPLETE(fsl_3, fsl_i2c_init, fsl_i2c_probe_chip, fsl_i2c_read,
Shengzhou Liua17fd102014-07-07 12:17:48 +0800553 fsl_i2c_write, fsl_i2c_set_bus_speed,
554 CONFIG_SYS_FSL_I2C4_SPEED, CONFIG_SYS_FSL_I2C4_SLAVE,
555 3)
556#endif
mario.six@gdsys.ccdbc82ce2016-04-25 08:31:09 +0200557#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);
Mario Sixa059de12018-01-15 11:08:07 +0100562
mario.six@gdsys.ccdbc82ce2016-04-25 08:31:09 +0200563 return __i2c_probe_chip(dev->base, chip_addr);
564}
565
Mario Sixa059de12018-01-15 11:08:07 +0100566static int fsl_i2c_set_bus_speed(struct udevice *bus, uint speed)
mario.six@gdsys.ccdbc82ce2016-04-25 08:31:09 +0200567{
568 struct fsl_i2c_dev *dev = dev_get_priv(bus);
Mario Sixa059de12018-01-15 11:08:07 +0100569
mario.six@gdsys.ccdbc82ce2016-04-25 08:31:09 +0200570 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);
Mario Sixe5c762f2018-03-28 14:37:44 +0200576 struct clk clock;
mario.six@gdsys.ccdbc82ce2016-04-25 08:31:09 +0200577
Mario Sixd9348322018-03-28 14:37:43 +0200578 dev->base = map_sysmem(dev_read_addr(bus), sizeof(struct fsl_i2c_base));
mario.six@gdsys.ccdbc82ce2016-04-25 08:31:09 +0200579
580 if (!dev->base)
581 return -ENOMEM;
582
Mario Six84a4d342018-01-15 11:08:09 +0100583 dev->index = dev_read_u32_default(bus, "cell-index", -1);
584 dev->slaveadd = dev_read_u32_default(bus, "u-boot,i2c-slave-addr",
585 0x7f);
586 dev->speed = dev_read_u32_default(bus, "clock-frequency", 400000);
mario.six@gdsys.ccdbc82ce2016-04-25 08:31:09 +0200587
Mario Sixe5c762f2018-03-28 14:37:44 +0200588 if (!clk_get_by_index(bus, 0, &clock))
589 dev->i2c_clk = clk_get_rate(&clock);
590 else
591 dev->i2c_clk = dev->index ? gd->arch.i2c2_clk :
592 gd->arch.i2c1_clk;
mario.six@gdsys.ccdbc82ce2016-04-25 08:31:09 +0200593
594 return 0;
595}
596
597static int fsl_i2c_probe(struct udevice *bus)
598{
599 struct fsl_i2c_dev *dev = dev_get_priv(bus);
Mario Sixa059de12018-01-15 11:08:07 +0100600
mario.six@gdsys.ccdbc82ce2016-04-25 08:31:09 +0200601 __i2c_init(dev->base, dev->speed, dev->slaveadd, dev->i2c_clk,
602 dev->index);
603 return 0;
604}
605
606static int fsl_i2c_xfer(struct udevice *bus, struct i2c_msg *msg, int nmsgs)
607{
608 struct fsl_i2c_dev *dev = dev_get_priv(bus);
609 struct i2c_msg *dmsg, *omsg, dummy;
610
611 memset(&dummy, 0, sizeof(struct i2c_msg));
612
613 /* We expect either two messages (one with an offset and one with the
Mario Sixa059de12018-01-15 11:08:07 +0100614 * actual data) or one message (just data)
615 */
mario.six@gdsys.ccdbc82ce2016-04-25 08:31:09 +0200616 if (nmsgs > 2 || nmsgs == 0) {
617 debug("%s: Only one or two messages are supported.", __func__);
618 return -1;
619 }
620
621 omsg = nmsgs == 1 ? &dummy : msg;
622 dmsg = nmsgs == 1 ? msg : msg + 1;
623
624 if (dmsg->flags & I2C_M_RD)
625 return __i2c_read(dev->base, dmsg->addr, omsg->buf, omsg->len,
626 dmsg->buf, dmsg->len);
627 else
628 return __i2c_write(dev->base, dmsg->addr, omsg->buf, omsg->len,
629 dmsg->buf, dmsg->len);
630}
631
632static const struct dm_i2c_ops fsl_i2c_ops = {
633 .xfer = fsl_i2c_xfer,
634 .probe_chip = fsl_i2c_probe_chip,
635 .set_bus_speed = fsl_i2c_set_bus_speed,
636};
637
638static const struct udevice_id fsl_i2c_ids[] = {
639 { .compatible = "fsl-i2c", },
640 { /* sentinel */ }
641};
642
643U_BOOT_DRIVER(i2c_fsl) = {
644 .name = "i2c_fsl",
645 .id = UCLASS_I2C,
646 .of_match = fsl_i2c_ids,
647 .probe = fsl_i2c_probe,
648 .ofdata_to_platdata = fsl_i2c_ofdata_to_platdata,
649 .priv_auto_alloc_size = sizeof(struct fsl_i2c_dev),
650 .ops = &fsl_i2c_ops,
651};
652
653#endif /* CONFIG_DM_I2C */