blob: 9a3c8241bc64f01550f4d75e846accff191881f4 [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 */
Simon Glassf7ae49f2020-05-10 11:40:05 -060012#include <log.h>
Simon Glass6887c5b2019-11-14 12:57:26 -070013#include <time.h>
Simon Glass401d1c42020-10-30 21:38:53 -060014#include <asm/global_data.h>
Jon Loeliger7237c032006-10-19 11:02:16 -050015#include <asm/io.h>
Jon Loeliger20476722006-10-20 15:50:15 -050016#include <asm/fsl_i2c.h> /* HW definitions */
Mario Sixe5c762f2018-03-28 14:37:44 +020017#include <clk.h>
mario.six@gdsys.ccdbc82ce2016-04-25 08:31:09 +020018#include <dm.h>
19#include <mapmem.h>
Simon Glassc05ed002020-05-10 11:40:11 -060020#include <linux/delay.h>
Jon Loeliger7237c032006-10-19 11:02:16 -050021
Timur Tabi92477a62009-09-04 16:28:35 -050022/* The maximum number of microseconds we will wait until another master has
23 * released the bus. If not defined in the board header file, then use a
24 * generic value.
25 */
26#ifndef CONFIG_I2C_MBB_TIMEOUT
27#define CONFIG_I2C_MBB_TIMEOUT 100000
28#endif
29
30/* The maximum number of microseconds we will wait for a read or write
31 * operation to complete. If not defined in the board header file, then use a
32 * generic value.
33 */
34#ifndef CONFIG_I2C_TIMEOUT
Shaveta Leekha6dd38cc2014-11-03 10:43:14 +053035#define CONFIG_I2C_TIMEOUT 100000
Timur Tabi92477a62009-09-04 16:28:35 -050036#endif
Jon Loeliger7237c032006-10-19 11:02:16 -050037
Joakim Tjernlund1939d962006-11-28 16:17:27 -060038#define I2C_READ_BIT 1
39#define I2C_WRITE_BIT 0
40
Timur Tabid8c82db2008-03-14 17:45:29 -050041DECLARE_GLOBAL_DATA_PTR;
42
Tom Rinibe7dbb62021-12-12 22:12:30 -050043#ifdef CONFIG_M68K
44#define CONFIG_SYS_IMMR CONFIG_SYS_MBAR
45#endif
46
Igor Opaniuk2147a162021-02-09 13:52:45 +020047#if !CONFIG_IS_ENABLED(DM_I2C)
mario.six@gdsys.ccec2c81c2016-04-25 08:31:01 +020048static const struct fsl_i2c_base *i2c_base[4] = {
49 (struct fsl_i2c_base *)(CONFIG_SYS_IMMR + CONFIG_SYS_FSL_I2C_OFFSET),
Heiko Schocher00f792e2012-10-24 13:48:22 +020050#ifdef CONFIG_SYS_FSL_I2C2_OFFSET
mario.six@gdsys.ccec2c81c2016-04-25 08:31:01 +020051 (struct fsl_i2c_base *)(CONFIG_SYS_IMMR + CONFIG_SYS_FSL_I2C2_OFFSET),
Shengzhou Liua17fd102014-07-07 12:17:48 +080052#endif
53#ifdef CONFIG_SYS_FSL_I2C3_OFFSET
mario.six@gdsys.ccec2c81c2016-04-25 08:31:01 +020054 (struct fsl_i2c_base *)(CONFIG_SYS_IMMR + CONFIG_SYS_FSL_I2C3_OFFSET),
Shengzhou Liua17fd102014-07-07 12:17:48 +080055#endif
56#ifdef CONFIG_SYS_FSL_I2C4_OFFSET
mario.six@gdsys.ccec2c81c2016-04-25 08:31:01 +020057 (struct fsl_i2c_base *)(CONFIG_SYS_IMMR + CONFIG_SYS_FSL_I2C4_OFFSET)
Timur Tabibe5e6182006-11-03 19:15:00 -060058#endif
59};
mario.six@gdsys.ccdbc82ce2016-04-25 08:31:09 +020060#endif
Jon Loeliger7237c032006-10-19 11:02:16 -050061
Timur Tabid8c82db2008-03-14 17:45:29 -050062/* I2C speed map for a DFSR value of 1 */
63
Tom Rini645cb462017-02-09 15:40:16 -050064#ifdef __M68K__
Timur Tabid8c82db2008-03-14 17:45:29 -050065/*
66 * Map I2C frequency dividers to FDR and DFSR values
67 *
68 * This structure is used to define the elements of a table that maps I2C
69 * frequency divider (I2C clock rate divided by I2C bus speed) to a value to be
70 * programmed into the Frequency Divider Ratio (FDR) and Digital Filter
71 * Sampling Rate (DFSR) registers.
72 *
73 * The actual table should be defined in the board file, and it must be called
74 * fsl_i2c_speed_map[].
75 *
76 * The last entry of the table must have a value of {-1, X}, where X is same
77 * FDR/DFSR values as the second-to-last entry. This guarantees that any
78 * search through the array will always find a match.
79 *
80 * The values of the divider must be in increasing numerical order, i.e.
81 * fsl_i2c_speed_map[x+1].divider > fsl_i2c_speed_map[x].divider.
82 *
83 * For this table, the values are based on a value of 1 for the DFSR
84 * register. See the application note AN2919 "Determining the I2C Frequency
85 * Divider Ratio for SCL"
TsiChung Liew5d9a5ef2008-08-19 00:56:46 +060086 *
87 * ColdFire I2C frequency dividers for FDR values are different from
88 * PowerPC. The protocol to use the I2C module is still the same.
89 * A different table is defined and are based on MCF5xxx user manual.
90 *
Timur Tabid8c82db2008-03-14 17:45:29 -050091 */
92static const struct {
93 unsigned short divider;
Timur Tabid8c82db2008-03-14 17:45:29 -050094 u8 fdr;
95} fsl_i2c_speed_map[] = {
TsiChung Liew5d9a5ef2008-08-19 00:56:46 +060096 {20, 32}, {22, 33}, {24, 34}, {26, 35},
97 {28, 0}, {28, 36}, {30, 1}, {32, 37},
98 {34, 2}, {36, 38}, {40, 3}, {40, 39},
99 {44, 4}, {48, 5}, {48, 40}, {56, 6},
100 {56, 41}, {64, 42}, {68, 7}, {72, 43},
101 {80, 8}, {80, 44}, {88, 9}, {96, 41},
102 {104, 10}, {112, 42}, {128, 11}, {128, 43},
103 {144, 12}, {160, 13}, {160, 48}, {192, 14},
104 {192, 49}, {224, 50}, {240, 15}, {256, 51},
105 {288, 16}, {320, 17}, {320, 52}, {384, 18},
106 {384, 53}, {448, 54}, {480, 19}, {512, 55},
107 {576, 20}, {640, 21}, {640, 56}, {768, 22},
108 {768, 57}, {960, 23}, {896, 58}, {1024, 59},
109 {1152, 24}, {1280, 25}, {1280, 60}, {1536, 26},
110 {1536, 61}, {1792, 62}, {1920, 27}, {2048, 63},
111 {2304, 28}, {2560, 29}, {3072, 30}, {3840, 31},
112 {-1, 31}
Timur Tabid8c82db2008-03-14 17:45:29 -0500113};
Tom Rini645cb462017-02-09 15:40:16 -0500114#endif
Timur Tabid8c82db2008-03-14 17:45:29 -0500115
116/**
117 * Set the I2C bus speed for a given I2C device
118 *
mario.six@gdsys.ccec2c81c2016-04-25 08:31:01 +0200119 * @param base: the I2C device registers
Timur Tabid8c82db2008-03-14 17:45:29 -0500120 * @i2c_clk: I2C bus clock frequency
121 * @speed: the desired speed of the bus
122 *
123 * The I2C device must be stopped before calling this function.
124 *
125 * The return value is the actual bus speed that is set.
126 */
Mario Sixa059de12018-01-15 11:08:07 +0100127static uint set_i2c_bus_speed(const struct fsl_i2c_base *base,
128 uint i2c_clk, uint speed)
Timur Tabid8c82db2008-03-14 17:45:29 -0500129{
Mario Sixa059de12018-01-15 11:08:07 +0100130 ushort divider = min(i2c_clk / speed, (uint)USHRT_MAX);
Timur Tabid8c82db2008-03-14 17:45:29 -0500131
132 /*
133 * We want to choose an FDR/DFSR that generates an I2C bus speed that
134 * is equal to or lower than the requested speed. That means that we
135 * want the first divider that is equal to or greater than the
136 * calculated divider.
137 */
Joakim Tjernlund99404202009-09-17 11:07:17 +0200138#ifdef __PPC__
139 u8 dfsr, fdr = 0x31; /* Default if no FDR found */
140 /* a, b and dfsr matches identifiers A,B and C respectively in AN2919 */
Mario Sixa059de12018-01-15 11:08:07 +0100141 ushort a, b, ga, gb;
142 ulong c_div, est_div;
Joakim Tjernlund99404202009-09-17 11:07:17 +0200143
144#ifdef CONFIG_FSL_I2C_CUSTOM_DFSR
145 dfsr = CONFIG_FSL_I2C_CUSTOM_DFSR;
146#else
147 /* Condition 1: dfsr <= 50/T */
148 dfsr = (5 * (i2c_clk / 1000)) / 100000;
149#endif
150#ifdef CONFIG_FSL_I2C_CUSTOM_FDR
151 fdr = CONFIG_FSL_I2C_CUSTOM_FDR;
152 speed = i2c_clk / divider; /* Fake something */
153#else
154 debug("Requested speed:%d, i2c_clk:%d\n", speed, i2c_clk);
155 if (!dfsr)
156 dfsr = 1;
157
158 est_div = ~0;
159 for (ga = 0x4, a = 10; a <= 30; ga++, a += 2) {
160 for (gb = 0; gb < 8; gb++) {
161 b = 16 << gb;
Mario Sixa059de12018-01-15 11:08:07 +0100162 c_div = b * (a + ((3 * dfsr) / b) * 2);
163 if (c_div > divider && c_div < est_div) {
164 ushort bin_gb, bin_ga;
Joakim Tjernlund99404202009-09-17 11:07:17 +0200165
166 est_div = c_div;
167 bin_gb = gb << 2;
168 bin_ga = (ga & 0x3) | ((ga & 0x4) << 3);
169 fdr = bin_gb | bin_ga;
170 speed = i2c_clk / est_div;
Mario Sixa059de12018-01-15 11:08:07 +0100171
172 debug("FDR: 0x%.2x, ", fdr);
173 debug("div: %ld, ", est_div);
174 debug("ga: 0x%x, gb: 0x%x, ", ga, gb);
175 debug("a: %d, b: %d, speed: %d\n", a, b, speed);
176
Joakim Tjernlund99404202009-09-17 11:07:17 +0200177 /* Condition 2 not accounted for */
178 debug("Tr <= %d ns\n",
179 (b - 3 * dfsr) * 1000000 /
180 (i2c_clk / 1000));
181 }
182 }
183 if (a == 20)
184 a += 2;
185 if (a == 24)
186 a += 4;
187 }
Mario Sixa059de12018-01-15 11:08:07 +0100188 debug("divider: %d, est_div: %ld, DFSR: %d\n", divider, est_div, dfsr);
189 debug("FDR: 0x%.2x, speed: %d\n", fdr, speed);
Joakim Tjernlund99404202009-09-17 11:07:17 +0200190#endif
mario.six@gdsys.ccec2c81c2016-04-25 08:31:01 +0200191 writeb(dfsr, &base->dfsrr); /* set default filter */
192 writeb(fdr, &base->fdr); /* set bus speed */
Joakim Tjernlund99404202009-09-17 11:07:17 +0200193#else
Mario Sixa059de12018-01-15 11:08:07 +0100194 uint i;
Timur Tabid8c82db2008-03-14 17:45:29 -0500195
196 for (i = 0; i < ARRAY_SIZE(fsl_i2c_speed_map); i++)
197 if (fsl_i2c_speed_map[i].divider >= divider) {
TsiChung Liew5d9a5ef2008-08-19 00:56:46 +0600198 u8 fdr;
Joakim Tjernlund99404202009-09-17 11:07:17 +0200199
Joakim Tjernlundd01ee4d2009-09-17 11:07:16 +0200200 fdr = fsl_i2c_speed_map[i].fdr;
201 speed = i2c_clk / fsl_i2c_speed_map[i].divider;
mario.six@gdsys.ccec2c81c2016-04-25 08:31:01 +0200202 writeb(fdr, &base->fdr); /* set bus speed */
Joakim Tjernlundd01ee4d2009-09-17 11:07:16 +0200203
Timur Tabid8c82db2008-03-14 17:45:29 -0500204 break;
205 }
Joakim Tjernlund99404202009-09-17 11:07:17 +0200206#endif
Timur Tabid8c82db2008-03-14 17:45:29 -0500207 return speed;
208}
209
Igor Opaniuk2147a162021-02-09 13:52:45 +0200210#if !CONFIG_IS_ENABLED(DM_I2C)
Mario Sixa059de12018-01-15 11:08:07 +0100211static uint get_i2c_clock(int bus)
Jerry Huangc9a8b252011-10-26 15:29:38 +0000212{
213 if (bus)
Simon Glass609e6ec2012-12-13 20:48:49 +0000214 return gd->arch.i2c2_clk; /* I2C2 clock */
Jerry Huangc9a8b252011-10-26 15:29:38 +0000215 else
Simon Glass609e6ec2012-12-13 20:48:49 +0000216 return gd->arch.i2c1_clk; /* I2C1 clock */
Jerry Huangc9a8b252011-10-26 15:29:38 +0000217}
mario.six@gdsys.ccdbc82ce2016-04-25 08:31:09 +0200218#endif
Jerry Huangc9a8b252011-10-26 15:29:38 +0000219
mario.six@gdsys.ccec2c81c2016-04-25 08:31:01 +0200220static int fsl_i2c_fixup(const struct fsl_i2c_base *base)
Chunhe Lanb8ce3342013-08-16 15:10:36 +0800221{
222 const unsigned long long timeout = usec2ticks(CONFIG_I2C_MBB_TIMEOUT);
223 unsigned long long timeval = 0;
224 int ret = -1;
Mario Sixa059de12018-01-15 11:08:07 +0100225 uint flags = 0;
Chunhe Lan9c3f77e2013-08-16 15:10:37 +0800226
227#ifdef CONFIG_SYS_FSL_ERRATUM_I2C_A004447
Mario Sixa059de12018-01-15 11:08:07 +0100228 uint svr = get_svr();
229
Chunhe Lan9c3f77e2013-08-16 15:10:37 +0800230 if ((SVR_SOC_VER(svr) == SVR_8548 && IS_SVR_REV(svr, 3, 1)) ||
231 (SVR_REV(svr) <= CONFIG_SYS_FSL_A004447_SVR_REV))
232 flags = I2C_CR_BIT6;
233#endif
Chunhe Lanb8ce3342013-08-16 15:10:36 +0800234
mario.six@gdsys.ccec2c81c2016-04-25 08:31:01 +0200235 writeb(I2C_CR_MEN | I2C_CR_MSTA, &base->cr);
Chunhe Lanb8ce3342013-08-16 15:10:36 +0800236
237 timeval = get_ticks();
mario.six@gdsys.ccec2c81c2016-04-25 08:31:01 +0200238 while (!(readb(&base->sr) & I2C_SR_MBB)) {
Chunhe Lanb8ce3342013-08-16 15:10:36 +0800239 if ((get_ticks() - timeval) > timeout)
240 goto err;
241 }
242
mario.six@gdsys.ccec2c81c2016-04-25 08:31:01 +0200243 if (readb(&base->sr) & I2C_SR_MAL) {
Chunhe Lanb8ce3342013-08-16 15:10:36 +0800244 /* SDA is stuck low */
mario.six@gdsys.ccec2c81c2016-04-25 08:31:01 +0200245 writeb(0, &base->cr);
Chunhe Lanb8ce3342013-08-16 15:10:36 +0800246 udelay(100);
mario.six@gdsys.ccec2c81c2016-04-25 08:31:01 +0200247 writeb(I2C_CR_MSTA | flags, &base->cr);
248 writeb(I2C_CR_MEN | I2C_CR_MSTA | flags, &base->cr);
Chunhe Lanb8ce3342013-08-16 15:10:36 +0800249 }
250
mario.six@gdsys.ccec2c81c2016-04-25 08:31:01 +0200251 readb(&base->dr);
Chunhe Lanb8ce3342013-08-16 15:10:36 +0800252
253 timeval = get_ticks();
mario.six@gdsys.ccec2c81c2016-04-25 08:31:01 +0200254 while (!(readb(&base->sr) & I2C_SR_MIF)) {
Chunhe Lanb8ce3342013-08-16 15:10:36 +0800255 if ((get_ticks() - timeval) > timeout)
256 goto err;
257 }
258 ret = 0;
259
260err:
mario.six@gdsys.ccec2c81c2016-04-25 08:31:01 +0200261 writeb(I2C_CR_MEN | flags, &base->cr);
262 writeb(0, &base->sr);
Chunhe Lanb8ce3342013-08-16 15:10:36 +0800263 udelay(100);
264
265 return ret;
266}
267
mario.six@gdsys.ccecf591e2016-04-25 08:31:08 +0200268static void __i2c_init(const struct fsl_i2c_base *base, int speed, int
269 slaveadd, int i2c_clk, int busnum)
Jon Loeliger7237c032006-10-19 11:02:16 -0500270{
Chunhe Lanb8ce3342013-08-16 15:10:36 +0800271 const unsigned long long timeout = usec2ticks(CONFIG_I2C_MBB_TIMEOUT);
272 unsigned long long timeval;
Jon Loeliger7237c032006-10-19 11:02:16 -0500273
Heiko Schocher39df00d2009-07-09 12:04:26 +0200274#ifdef CONFIG_SYS_I2C_INIT_BOARD
Richard Retanubun26a33502010-04-12 15:08:17 -0400275 /* Call board specific i2c bus reset routine before accessing the
276 * environment, which might be in a chip on that bus. For details
277 * about this problem see doc/I2C_Edge_Conditions.
Mario Sixa059de12018-01-15 11:08:07 +0100278 */
Heiko Schocher39df00d2009-07-09 12:04:26 +0200279 i2c_init_board();
280#endif
mario.six@gdsys.ccec2c81c2016-04-25 08:31:01 +0200281 writeb(0, &base->cr); /* stop I2C controller */
Heiko Schocher00f792e2012-10-24 13:48:22 +0200282 udelay(5); /* let it shutdown in peace */
mario.six@gdsys.ccecf591e2016-04-25 08:31:08 +0200283 set_i2c_bus_speed(base, i2c_clk, speed);
mario.six@gdsys.ccec2c81c2016-04-25 08:31:01 +0200284 writeb(slaveadd << 1, &base->adr);/* write slave address */
285 writeb(0x0, &base->sr); /* clear status register */
286 writeb(I2C_CR_MEN, &base->cr); /* start I2C controller */
Richard Retanubun26a33502010-04-12 15:08:17 -0400287
Chunhe Lanb8ce3342013-08-16 15:10:36 +0800288 timeval = get_ticks();
mario.six@gdsys.ccec2c81c2016-04-25 08:31:01 +0200289 while (readb(&base->sr) & I2C_SR_MBB) {
Chunhe Lanb8ce3342013-08-16 15:10:36 +0800290 if ((get_ticks() - timeval) < timeout)
291 continue;
292
mario.six@gdsys.ccec2c81c2016-04-25 08:31:01 +0200293 if (fsl_i2c_fixup(base))
Chunhe Lanb8ce3342013-08-16 15:10:36 +0800294 debug("i2c_init: BUS#%d failed to init\n",
mario.six@gdsys.ccecf591e2016-04-25 08:31:08 +0200295 busnum);
Chunhe Lanb8ce3342013-08-16 15:10:36 +0800296
297 break;
298 }
Jon Loeliger7237c032006-10-19 11:02:16 -0500299}
300
Mario Sixa059de12018-01-15 11:08:07 +0100301static int i2c_wait4bus(const struct fsl_i2c_base *base)
Jon Loeliger7237c032006-10-19 11:02:16 -0500302{
Stefan Roesef2302d42008-08-06 14:05:38 +0200303 unsigned long long timeval = get_ticks();
Timur Tabi92477a62009-09-04 16:28:35 -0500304 const unsigned long long timeout = usec2ticks(CONFIG_I2C_MBB_TIMEOUT);
Jon Loeliger7237c032006-10-19 11:02:16 -0500305
mario.six@gdsys.ccec2c81c2016-04-25 08:31:01 +0200306 while (readb(&base->sr) & I2C_SR_MBB) {
Timur Tabi92477a62009-09-04 16:28:35 -0500307 if ((get_ticks() - timeval) > timeout)
Jon Loeliger7237c032006-10-19 11:02:16 -0500308 return -1;
Jon Loeliger7237c032006-10-19 11:02:16 -0500309 }
310
311 return 0;
312}
313
Mario Sixd4f422f2018-01-15 11:08:08 +0100314static int i2c_wait(const struct fsl_i2c_base *base, int write)
Jon Loeliger7237c032006-10-19 11:02:16 -0500315{
316 u32 csr;
Stefan Roesef2302d42008-08-06 14:05:38 +0200317 unsigned long long timeval = get_ticks();
Timur Tabi92477a62009-09-04 16:28:35 -0500318 const unsigned long long timeout = usec2ticks(CONFIG_I2C_TIMEOUT);
Jon Loeliger7237c032006-10-19 11:02:16 -0500319
320 do {
mario.six@gdsys.ccec2c81c2016-04-25 08:31:01 +0200321 csr = readb(&base->sr);
Jon Loeliger7237c032006-10-19 11:02:16 -0500322 if (!(csr & I2C_SR_MIF))
323 continue;
Joakim Tjernlund21f4cbb2009-09-17 11:07:15 +0200324 /* Read again to allow register to stabilise */
mario.six@gdsys.ccec2c81c2016-04-25 08:31:01 +0200325 csr = readb(&base->sr);
Jon Loeliger7237c032006-10-19 11:02:16 -0500326
mario.six@gdsys.ccec2c81c2016-04-25 08:31:01 +0200327 writeb(0x0, &base->sr);
Jon Loeliger7237c032006-10-19 11:02:16 -0500328
329 if (csr & I2C_SR_MAL) {
Mario Sixa059de12018-01-15 11:08:07 +0100330 debug("%s: MAL\n", __func__);
Jon Loeliger7237c032006-10-19 11:02:16 -0500331 return -1;
332 }
333
334 if (!(csr & I2C_SR_MCF)) {
Mario Sixa059de12018-01-15 11:08:07 +0100335 debug("%s: unfinished\n", __func__);
Jon Loeliger7237c032006-10-19 11:02:16 -0500336 return -1;
337 }
338
Joakim Tjernlund1939d962006-11-28 16:17:27 -0600339 if (write == I2C_WRITE_BIT && (csr & I2C_SR_RXAK)) {
Mario Sixa059de12018-01-15 11:08:07 +0100340 debug("%s: No RXACK\n", __func__);
Jon Loeliger7237c032006-10-19 11:02:16 -0500341 return -1;
342 }
343
344 return 0;
Timur Tabi92477a62009-09-04 16:28:35 -0500345 } while ((get_ticks() - timeval) < timeout);
Jon Loeliger7237c032006-10-19 11:02:16 -0500346
Mario Sixa059de12018-01-15 11:08:07 +0100347 debug("%s: timed out\n", __func__);
Jon Loeliger7237c032006-10-19 11:02:16 -0500348 return -1;
349}
350
Mario Sixd4f422f2018-01-15 11:08:08 +0100351static int i2c_write_addr(const struct fsl_i2c_base *base, u8 dev,
352 u8 dir, int rsta)
Jon Loeliger7237c032006-10-19 11:02:16 -0500353{
354 writeb(I2C_CR_MEN | I2C_CR_MSTA | I2C_CR_MTX
355 | (rsta ? I2C_CR_RSTA : 0),
mario.six@gdsys.ccec2c81c2016-04-25 08:31:01 +0200356 &base->cr);
Jon Loeliger7237c032006-10-19 11:02:16 -0500357
mario.six@gdsys.ccec2c81c2016-04-25 08:31:01 +0200358 writeb((dev << 1) | dir, &base->dr);
Jon Loeliger7237c032006-10-19 11:02:16 -0500359
mario.six@gdsys.ccecf591e2016-04-25 08:31:08 +0200360 if (i2c_wait(base, I2C_WRITE_BIT) < 0)
Jon Loeliger7237c032006-10-19 11:02:16 -0500361 return 0;
362
363 return 1;
364}
365
Mario Sixd4f422f2018-01-15 11:08:08 +0100366static int __i2c_write_data(const struct fsl_i2c_base *base, u8 *data,
367 int length)
Jon Loeliger7237c032006-10-19 11:02:16 -0500368{
369 int i;
370
Jon Loeliger7237c032006-10-19 11:02:16 -0500371 for (i = 0; i < length; i++) {
mario.six@gdsys.ccec2c81c2016-04-25 08:31:01 +0200372 writeb(data[i], &base->dr);
Jon Loeliger7237c032006-10-19 11:02:16 -0500373
mario.six@gdsys.ccecf591e2016-04-25 08:31:08 +0200374 if (i2c_wait(base, I2C_WRITE_BIT) < 0)
Jon Loeliger7237c032006-10-19 11:02:16 -0500375 break;
376 }
377
378 return i;
379}
380
Mario Sixd4f422f2018-01-15 11:08:08 +0100381static int __i2c_read_data(const struct fsl_i2c_base *base, u8 *data,
382 int length)
Jon Loeliger7237c032006-10-19 11:02:16 -0500383{
384 int i;
385
386 writeb(I2C_CR_MEN | I2C_CR_MSTA | ((length == 1) ? I2C_CR_TXAK : 0),
mario.six@gdsys.ccec2c81c2016-04-25 08:31:01 +0200387 &base->cr);
Jon Loeliger7237c032006-10-19 11:02:16 -0500388
389 /* dummy read */
mario.six@gdsys.ccec2c81c2016-04-25 08:31:01 +0200390 readb(&base->dr);
Jon Loeliger7237c032006-10-19 11:02:16 -0500391
392 for (i = 0; i < length; i++) {
mario.six@gdsys.ccecf591e2016-04-25 08:31:08 +0200393 if (i2c_wait(base, I2C_READ_BIT) < 0)
Jon Loeliger7237c032006-10-19 11:02:16 -0500394 break;
395
396 /* Generate ack on last next to last byte */
397 if (i == length - 2)
398 writeb(I2C_CR_MEN | I2C_CR_MSTA | I2C_CR_TXAK,
mario.six@gdsys.ccec2c81c2016-04-25 08:31:01 +0200399 &base->cr);
Jon Loeliger7237c032006-10-19 11:02:16 -0500400
Joakim Tjernlundd1c9e5b2009-09-22 13:40:44 +0200401 /* Do not generate stop on last byte */
Jon Loeliger7237c032006-10-19 11:02:16 -0500402 if (i == length - 1)
Joakim Tjernlundd1c9e5b2009-09-22 13:40:44 +0200403 writeb(I2C_CR_MEN | I2C_CR_MSTA | I2C_CR_MTX,
mario.six@gdsys.ccec2c81c2016-04-25 08:31:01 +0200404 &base->cr);
Jon Loeliger7237c032006-10-19 11:02:16 -0500405
mario.six@gdsys.ccec2c81c2016-04-25 08:31:01 +0200406 data[i] = readb(&base->dr);
Jon Loeliger7237c032006-10-19 11:02:16 -0500407 }
408
409 return i;
410}
411
Mario Sixa059de12018-01-15 11:08:07 +0100412static int __i2c_read(const struct fsl_i2c_base *base, u8 chip_addr, u8 *offset,
413 int olen, u8 *data, int dlen)
Jon Loeliger7237c032006-10-19 11:02:16 -0500414{
mario.six@gdsys.cc2b21e962016-04-25 08:31:02 +0200415 int ret = -1; /* signal error */
Jon Loeliger7237c032006-10-19 11:02:16 -0500416
mario.six@gdsys.ccecf591e2016-04-25 08:31:08 +0200417 if (i2c_wait4bus(base) < 0)
Reinhard Pfaub778c1b2013-06-26 15:55:14 +0200418 return -1;
419
mario.six@gdsys.cc386b2762016-04-25 08:31:03 +0200420 /* Some drivers use offset lengths in excess of 4 bytes. These drivers
421 * adhere to the following convention:
422 * - the offset length is passed as negative (that is, the absolute
423 * value of olen is the actual offset length)
424 * - the offset itself is passed in data, which is overwritten by the
425 * subsequent read operation
Shaveta Leekhaa4057642014-04-24 14:51:23 +0530426 */
mario.six@gdsys.cc2b21e962016-04-25 08:31:02 +0200427 if (olen < 0) {
mario.six@gdsys.ccecf591e2016-04-25 08:31:08 +0200428 if (i2c_write_addr(base, chip_addr, I2C_WRITE_BIT, 0) != 0)
429 ret = __i2c_write_data(base, data, -olen);
Joakim Tjernlundf6f5f702007-01-31 11:04:19 +0100430
mario.six@gdsys.cc03a112a2016-04-25 08:31:04 +0200431 if (ret != -olen)
Shaveta Leekhaa4057642014-04-24 14:51:23 +0530432 return -1;
433
mario.six@gdsys.ccecf591e2016-04-25 08:31:08 +0200434 if (dlen && i2c_write_addr(base, chip_addr,
mario.six@gdsys.cc2b21e962016-04-25 08:31:02 +0200435 I2C_READ_BIT, 1) != 0)
mario.six@gdsys.ccecf591e2016-04-25 08:31:08 +0200436 ret = __i2c_read_data(base, data, dlen);
Shaveta Leekhaa4057642014-04-24 14:51:23 +0530437 } else {
mario.six@gdsys.cc2b21e962016-04-25 08:31:02 +0200438 if ((!dlen || olen > 0) &&
mario.six@gdsys.ccecf591e2016-04-25 08:31:08 +0200439 i2c_write_addr(base, chip_addr, I2C_WRITE_BIT, 0) != 0 &&
440 __i2c_write_data(base, offset, olen) == olen)
mario.six@gdsys.cc2b21e962016-04-25 08:31:02 +0200441 ret = 0; /* No error so far */
Shaveta Leekhaa4057642014-04-24 14:51:23 +0530442
mario.six@gdsys.ccecf591e2016-04-25 08:31:08 +0200443 if (dlen && i2c_write_addr(base, chip_addr, I2C_READ_BIT,
mario.six@gdsys.cc2b21e962016-04-25 08:31:02 +0200444 olen ? 1 : 0) != 0)
mario.six@gdsys.ccecf591e2016-04-25 08:31:08 +0200445 ret = __i2c_read_data(base, data, dlen);
Shaveta Leekhaa4057642014-04-24 14:51:23 +0530446 }
Jon Loeliger7237c032006-10-19 11:02:16 -0500447
mario.six@gdsys.ccec2c81c2016-04-25 08:31:01 +0200448 writeb(I2C_CR_MEN, &base->cr);
Jon Loeliger7237c032006-10-19 11:02:16 -0500449
mario.six@gdsys.ccecf591e2016-04-25 08:31:08 +0200450 if (i2c_wait4bus(base)) /* Wait until STOP */
Joakim Tjernlundd1c9e5b2009-09-22 13:40:44 +0200451 debug("i2c_read: wait4bus timed out\n");
452
mario.six@gdsys.cc2b21e962016-04-25 08:31:02 +0200453 if (ret == dlen)
454 return 0;
Jon Loeliger4d45f692006-10-19 12:02:24 -0500455
456 return -1;
Jon Loeliger7237c032006-10-19 11:02:16 -0500457}
458
Mario Sixa059de12018-01-15 11:08:07 +0100459static int __i2c_write(const struct fsl_i2c_base *base, u8 chip_addr,
460 u8 *offset, int olen, u8 *data, int dlen)
Jon Loeliger7237c032006-10-19 11:02:16 -0500461{
mario.six@gdsys.cc2b21e962016-04-25 08:31:02 +0200462 int ret = -1; /* signal error */
Jon Loeliger7237c032006-10-19 11:02:16 -0500463
mario.six@gdsys.ccecf591e2016-04-25 08:31:08 +0200464 if (i2c_wait4bus(base) < 0)
Chunhe Lanb8ce3342013-08-16 15:10:36 +0800465 return -1;
466
mario.six@gdsys.ccecf591e2016-04-25 08:31:08 +0200467 if (i2c_write_addr(base, chip_addr, I2C_WRITE_BIT, 0) != 0 &&
468 __i2c_write_data(base, offset, olen) == olen) {
469 ret = __i2c_write_data(base, data, dlen);
Jon Loeliger4d45f692006-10-19 12:02:24 -0500470 }
Jon Loeliger7237c032006-10-19 11:02:16 -0500471
mario.six@gdsys.ccec2c81c2016-04-25 08:31:01 +0200472 writeb(I2C_CR_MEN, &base->cr);
mario.six@gdsys.ccecf591e2016-04-25 08:31:08 +0200473 if (i2c_wait4bus(base)) /* Wait until STOP */
Joakim Tjernlund21f4cbb2009-09-17 11:07:15 +0200474 debug("i2c_write: wait4bus timed out\n");
Jon Loeliger7237c032006-10-19 11:02:16 -0500475
mario.six@gdsys.cc2b21e962016-04-25 08:31:02 +0200476 if (ret == dlen)
477 return 0;
Jon Loeliger4d45f692006-10-19 12:02:24 -0500478
479 return -1;
Jon Loeliger7237c032006-10-19 11:02:16 -0500480}
481
Mario Sixa059de12018-01-15 11:08:07 +0100482static int __i2c_probe_chip(const struct fsl_i2c_base *base, uchar chip)
Jon Loeliger7237c032006-10-19 11:02:16 -0500483{
Mario Sixa059de12018-01-15 11:08:07 +0100484 /* For unknown reason the controller will ACK when
Joakim Tjernlundf6f5f702007-01-31 11:04:19 +0100485 * probing for a slave with the same address, so skip
486 * it.
Jon Loeliger7237c032006-10-19 11:02:16 -0500487 */
mario.six@gdsys.ccec2c81c2016-04-25 08:31:01 +0200488 if (chip == (readb(&base->adr) >> 1))
Joakim Tjernlundf6f5f702007-01-31 11:04:19 +0100489 return -1;
Jon Loeliger7237c032006-10-19 11:02:16 -0500490
mario.six@gdsys.ccecf591e2016-04-25 08:31:08 +0200491 return __i2c_read(base, chip, 0, 0, NULL, 0);
Jon Loeliger7237c032006-10-19 11:02:16 -0500492}
493
Mario Sixa059de12018-01-15 11:08:07 +0100494static uint __i2c_set_bus_speed(const struct fsl_i2c_base *base,
495 uint speed, int i2c_clk)
Timur Tabibe5e6182006-11-03 19:15:00 -0600496{
mario.six@gdsys.ccec2c81c2016-04-25 08:31:01 +0200497 writeb(0, &base->cr); /* stop controller */
mario.six@gdsys.ccecf591e2016-04-25 08:31:08 +0200498 set_i2c_bus_speed(base, i2c_clk, speed);
mario.six@gdsys.ccec2c81c2016-04-25 08:31:01 +0200499 writeb(I2C_CR_MEN, &base->cr); /* start controller */
Timur Tabid8c82db2008-03-14 17:45:29 -0500500
501 return 0;
Timur Tabibe5e6182006-11-03 19:15:00 -0600502}
503
Igor Opaniuk2147a162021-02-09 13:52:45 +0200504#if !CONFIG_IS_ENABLED(DM_I2C)
mario.six@gdsys.ccad7e6572016-04-25 08:31:07 +0200505static void fsl_i2c_init(struct i2c_adapter *adap, int speed, int slaveadd)
506{
mario.six@gdsys.ccecf591e2016-04-25 08:31:08 +0200507 __i2c_init(i2c_base[adap->hwadapnr], speed, slaveadd,
508 get_i2c_clock(adap->hwadapnr), adap->hwadapnr);
mario.six@gdsys.ccad7e6572016-04-25 08:31:07 +0200509}
510
Mario Sixa059de12018-01-15 11:08:07 +0100511static int fsl_i2c_probe_chip(struct i2c_adapter *adap, uchar chip)
mario.six@gdsys.ccad7e6572016-04-25 08:31:07 +0200512{
mario.six@gdsys.ccecf591e2016-04-25 08:31:08 +0200513 return __i2c_probe_chip(i2c_base[adap->hwadapnr], chip);
mario.six@gdsys.ccad7e6572016-04-25 08:31:07 +0200514}
515
Mario Sixa059de12018-01-15 11:08:07 +0100516static int fsl_i2c_read(struct i2c_adapter *adap, u8 chip_addr, uint offset,
517 int olen, u8 *data, int dlen)
mario.six@gdsys.ccad7e6572016-04-25 08:31:07 +0200518{
mario.six@gdsys.ccecf591e2016-04-25 08:31:08 +0200519 u8 *o = (u8 *)&offset;
Mario Sixa059de12018-01-15 11:08:07 +0100520
mario.six@gdsys.ccecf591e2016-04-25 08:31:08 +0200521 return __i2c_read(i2c_base[adap->hwadapnr], chip_addr, &o[4 - olen],
522 olen, data, dlen);
mario.six@gdsys.ccad7e6572016-04-25 08:31:07 +0200523}
524
Mario Sixa059de12018-01-15 11:08:07 +0100525static int fsl_i2c_write(struct i2c_adapter *adap, u8 chip_addr, uint offset,
526 int olen, u8 *data, int dlen)
mario.six@gdsys.ccad7e6572016-04-25 08:31:07 +0200527{
mario.six@gdsys.ccecf591e2016-04-25 08:31:08 +0200528 u8 *o = (u8 *)&offset;
Mario Sixa059de12018-01-15 11:08:07 +0100529
mario.six@gdsys.ccecf591e2016-04-25 08:31:08 +0200530 return __i2c_write(i2c_base[adap->hwadapnr], chip_addr, &o[4 - olen],
531 olen, data, dlen);
mario.six@gdsys.ccad7e6572016-04-25 08:31:07 +0200532}
533
Mario Sixa059de12018-01-15 11:08:07 +0100534static uint fsl_i2c_set_bus_speed(struct i2c_adapter *adap, uint speed)
mario.six@gdsys.ccad7e6572016-04-25 08:31:07 +0200535{
mario.six@gdsys.ccecf591e2016-04-25 08:31:08 +0200536 return __i2c_set_bus_speed(i2c_base[adap->hwadapnr], speed,
537 get_i2c_clock(adap->hwadapnr));
mario.six@gdsys.ccad7e6572016-04-25 08:31:07 +0200538}
539
Heiko Schocher00f792e2012-10-24 13:48:22 +0200540/*
541 * Register fsl i2c adapters
542 */
mario.six@gdsys.cc16579ec2016-04-25 08:31:05 +0200543U_BOOT_I2C_ADAP_COMPLETE(fsl_0, fsl_i2c_init, fsl_i2c_probe_chip, fsl_i2c_read,
Heiko Schocher00f792e2012-10-24 13:48:22 +0200544 fsl_i2c_write, fsl_i2c_set_bus_speed,
Tom Rini6d5d0c92021-08-18 23:12:35 -0400545 CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE,
Heiko Schocher00f792e2012-10-24 13:48:22 +0200546 0)
547#ifdef CONFIG_SYS_FSL_I2C2_OFFSET
mario.six@gdsys.cc16579ec2016-04-25 08:31:05 +0200548U_BOOT_I2C_ADAP_COMPLETE(fsl_1, fsl_i2c_init, fsl_i2c_probe_chip, fsl_i2c_read,
Heiko Schocher00f792e2012-10-24 13:48:22 +0200549 fsl_i2c_write, fsl_i2c_set_bus_speed,
Tom Rini6d5d0c92021-08-18 23:12:35 -0400550 CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE,
Heiko Schocher00f792e2012-10-24 13:48:22 +0200551 1)
Heiko Schocherc1bce4f2009-02-24 11:30:37 +0100552#endif
Shengzhou Liua17fd102014-07-07 12:17:48 +0800553#ifdef CONFIG_SYS_FSL_I2C3_OFFSET
mario.six@gdsys.cc16579ec2016-04-25 08:31:05 +0200554U_BOOT_I2C_ADAP_COMPLETE(fsl_2, fsl_i2c_init, fsl_i2c_probe_chip, fsl_i2c_read,
Shengzhou Liua17fd102014-07-07 12:17:48 +0800555 fsl_i2c_write, fsl_i2c_set_bus_speed,
Tom Rini6d5d0c92021-08-18 23:12:35 -0400556 CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE,
Shengzhou Liua17fd102014-07-07 12:17:48 +0800557 2)
558#endif
559#ifdef CONFIG_SYS_FSL_I2C4_OFFSET
mario.six@gdsys.cc16579ec2016-04-25 08:31:05 +0200560U_BOOT_I2C_ADAP_COMPLETE(fsl_3, fsl_i2c_init, fsl_i2c_probe_chip, fsl_i2c_read,
Shengzhou Liua17fd102014-07-07 12:17:48 +0800561 fsl_i2c_write, fsl_i2c_set_bus_speed,
Tom Rini6d5d0c92021-08-18 23:12:35 -0400562 CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE,
Shengzhou Liua17fd102014-07-07 12:17:48 +0800563 3)
564#endif
mario.six@gdsys.ccdbc82ce2016-04-25 08:31:09 +0200565#else /* CONFIG_DM_I2C */
566static int fsl_i2c_probe_chip(struct udevice *bus, u32 chip_addr,
567 u32 chip_flags)
568{
569 struct fsl_i2c_dev *dev = dev_get_priv(bus);
Mario Sixa059de12018-01-15 11:08:07 +0100570
mario.six@gdsys.ccdbc82ce2016-04-25 08:31:09 +0200571 return __i2c_probe_chip(dev->base, chip_addr);
572}
573
Mario Sixa059de12018-01-15 11:08:07 +0100574static int fsl_i2c_set_bus_speed(struct udevice *bus, uint speed)
mario.six@gdsys.ccdbc82ce2016-04-25 08:31:09 +0200575{
576 struct fsl_i2c_dev *dev = dev_get_priv(bus);
Mario Sixa059de12018-01-15 11:08:07 +0100577
mario.six@gdsys.ccdbc82ce2016-04-25 08:31:09 +0200578 return __i2c_set_bus_speed(dev->base, speed, dev->i2c_clk);
579}
580
Simon Glassd1998a92020-12-03 16:55:21 -0700581static int fsl_i2c_of_to_plat(struct udevice *bus)
mario.six@gdsys.ccdbc82ce2016-04-25 08:31:09 +0200582{
583 struct fsl_i2c_dev *dev = dev_get_priv(bus);
Mario Sixe5c762f2018-03-28 14:37:44 +0200584 struct clk clock;
mario.six@gdsys.ccdbc82ce2016-04-25 08:31:09 +0200585
Mario Sixd9348322018-03-28 14:37:43 +0200586 dev->base = map_sysmem(dev_read_addr(bus), sizeof(struct fsl_i2c_base));
mario.six@gdsys.ccdbc82ce2016-04-25 08:31:09 +0200587
588 if (!dev->base)
589 return -ENOMEM;
590
Mario Six84a4d342018-01-15 11:08:09 +0100591 dev->index = dev_read_u32_default(bus, "cell-index", -1);
592 dev->slaveadd = dev_read_u32_default(bus, "u-boot,i2c-slave-addr",
593 0x7f);
Simon Glassf3d46152020-01-23 11:48:22 -0700594 dev->speed = dev_read_u32_default(bus, "clock-frequency",
595 I2C_SPEED_FAST_RATE);
mario.six@gdsys.ccdbc82ce2016-04-25 08:31:09 +0200596
Mario Sixe5c762f2018-03-28 14:37:44 +0200597 if (!clk_get_by_index(bus, 0, &clock))
598 dev->i2c_clk = clk_get_rate(&clock);
599 else
600 dev->i2c_clk = dev->index ? gd->arch.i2c2_clk :
601 gd->arch.i2c1_clk;
mario.six@gdsys.ccdbc82ce2016-04-25 08:31:09 +0200602
603 return 0;
604}
605
606static int fsl_i2c_probe(struct udevice *bus)
607{
608 struct fsl_i2c_dev *dev = dev_get_priv(bus);
Mario Sixa059de12018-01-15 11:08:07 +0100609
mario.six@gdsys.ccdbc82ce2016-04-25 08:31:09 +0200610 __i2c_init(dev->base, dev->speed, dev->slaveadd, dev->i2c_clk,
611 dev->index);
612 return 0;
613}
614
615static int fsl_i2c_xfer(struct udevice *bus, struct i2c_msg *msg, int nmsgs)
616{
617 struct fsl_i2c_dev *dev = dev_get_priv(bus);
618 struct i2c_msg *dmsg, *omsg, dummy;
619
620 memset(&dummy, 0, sizeof(struct i2c_msg));
621
622 /* We expect either two messages (one with an offset and one with the
Mario Sixa059de12018-01-15 11:08:07 +0100623 * actual data) or one message (just data)
624 */
mario.six@gdsys.ccdbc82ce2016-04-25 08:31:09 +0200625 if (nmsgs > 2 || nmsgs == 0) {
626 debug("%s: Only one or two messages are supported.", __func__);
627 return -1;
628 }
629
630 omsg = nmsgs == 1 ? &dummy : msg;
631 dmsg = nmsgs == 1 ? msg : msg + 1;
632
633 if (dmsg->flags & I2C_M_RD)
634 return __i2c_read(dev->base, dmsg->addr, omsg->buf, omsg->len,
635 dmsg->buf, dmsg->len);
636 else
637 return __i2c_write(dev->base, dmsg->addr, omsg->buf, omsg->len,
638 dmsg->buf, dmsg->len);
639}
640
641static const struct dm_i2c_ops fsl_i2c_ops = {
642 .xfer = fsl_i2c_xfer,
643 .probe_chip = fsl_i2c_probe_chip,
644 .set_bus_speed = fsl_i2c_set_bus_speed,
645};
646
647static const struct udevice_id fsl_i2c_ids[] = {
648 { .compatible = "fsl-i2c", },
649 { /* sentinel */ }
650};
651
652U_BOOT_DRIVER(i2c_fsl) = {
653 .name = "i2c_fsl",
654 .id = UCLASS_I2C,
655 .of_match = fsl_i2c_ids,
656 .probe = fsl_i2c_probe,
Simon Glassd1998a92020-12-03 16:55:21 -0700657 .of_to_plat = fsl_i2c_of_to_plat,
Simon Glass41575d82020-12-03 16:55:17 -0700658 .priv_auto = sizeof(struct fsl_i2c_dev),
mario.six@gdsys.ccdbc82ce2016-04-25 08:31:09 +0200659 .ops = &fsl_i2c_ops,
660};
661
662#endif /* CONFIG_DM_I2C */