blob: da933452f5156c16b2195dff904cc0cd78ae28aa [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0
Jon Loeligere4773be2006-10-19 11:02:16 -05002/*
Timur Tabi2165c622009-09-04 16:28:35 -05003 * Copyright 2006,2009 Freescale Semiconductor, Inc.
Jon Loeligere4773be2006-10-19 11:02:16 -05004 *
Heiko Schocherf2850742012-10-24 13:48:22 +02005 * 2012, Heiko Schocher, DENX Software Engineering, hs@denx.de.
6 * Changes for multibus/multiadapter I2C support.
Jon Loeligere4773be2006-10-19 11:02:16 -05007 */
8
Jon Loeligere4773be2006-10-19 11:02:16 -05009#include <common.h>
Jon Loeliger24df9772006-10-19 12:02:24 -050010#include <command.h>
Jon Loeliger43d818f2006-10-20 15:50:15 -050011#include <i2c.h> /* Functional interface */
Simon Glass0f2af882020-05-10 11:40:05 -060012#include <log.h>
Simon Glass45c78902019-11-14 12:57:26 -070013#include <time.h>
Jon Loeligere4773be2006-10-19 11:02:16 -050014#include <asm/io.h>
Jon Loeliger43d818f2006-10-20 15:50:15 -050015#include <asm/fsl_i2c.h> /* HW definitions */
Mario Six2fe2ed62018-03-28 14:37:44 +020016#include <clk.h>
mario.six@gdsys.cc349686c2016-04-25 08:31:09 +020017#include <dm.h>
18#include <mapmem.h>
Simon Glassdbd79542020-05-10 11:40:11 -060019#include <linux/delay.h>
Jon Loeligere4773be2006-10-19 11:02:16 -050020
Timur Tabi2165c622009-09-04 16:28:35 -050021/* The maximum number of microseconds we will wait until another master has
22 * released the bus. If not defined in the board header file, then use a
23 * generic value.
24 */
25#ifndef CONFIG_I2C_MBB_TIMEOUT
26#define CONFIG_I2C_MBB_TIMEOUT 100000
27#endif
28
29/* The maximum number of microseconds we will wait for a read or write
30 * operation to complete. If not defined in the board header file, then use a
31 * generic value.
32 */
33#ifndef CONFIG_I2C_TIMEOUT
Shaveta Leekha2ba098f2014-11-03 10:43:14 +053034#define CONFIG_I2C_TIMEOUT 100000
Timur Tabi2165c622009-09-04 16:28:35 -050035#endif
Jon Loeligere4773be2006-10-19 11:02:16 -050036
Joakim Tjernlundc32c5f72006-11-28 16:17:27 -060037#define I2C_READ_BIT 1
38#define I2C_WRITE_BIT 0
39
Timur Tabib301fda2008-03-14 17:45:29 -050040DECLARE_GLOBAL_DATA_PTR;
41
mario.six@gdsys.cc349686c2016-04-25 08:31:09 +020042#ifndef CONFIG_DM_I2C
mario.six@gdsys.cc7d43b4e2016-04-25 08:31:01 +020043static const struct fsl_i2c_base *i2c_base[4] = {
44 (struct fsl_i2c_base *)(CONFIG_SYS_IMMR + CONFIG_SYS_FSL_I2C_OFFSET),
Heiko Schocherf2850742012-10-24 13:48:22 +020045#ifdef CONFIG_SYS_FSL_I2C2_OFFSET
mario.six@gdsys.cc7d43b4e2016-04-25 08:31:01 +020046 (struct fsl_i2c_base *)(CONFIG_SYS_IMMR + CONFIG_SYS_FSL_I2C2_OFFSET),
Shengzhou Liu37787f62014-07-07 12:17:48 +080047#endif
48#ifdef CONFIG_SYS_FSL_I2C3_OFFSET
mario.six@gdsys.cc7d43b4e2016-04-25 08:31:01 +020049 (struct fsl_i2c_base *)(CONFIG_SYS_IMMR + CONFIG_SYS_FSL_I2C3_OFFSET),
Shengzhou Liu37787f62014-07-07 12:17:48 +080050#endif
51#ifdef CONFIG_SYS_FSL_I2C4_OFFSET
mario.six@gdsys.cc7d43b4e2016-04-25 08:31:01 +020052 (struct fsl_i2c_base *)(CONFIG_SYS_IMMR + CONFIG_SYS_FSL_I2C4_OFFSET)
Timur Tabiab347542006-11-03 19:15:00 -060053#endif
54};
mario.six@gdsys.cc349686c2016-04-25 08:31:09 +020055#endif
Jon Loeligere4773be2006-10-19 11:02:16 -050056
Timur Tabib301fda2008-03-14 17:45:29 -050057/* I2C speed map for a DFSR value of 1 */
58
Tom Rini56762c12017-02-09 15:40:16 -050059#ifdef __M68K__
Timur Tabib301fda2008-03-14 17:45:29 -050060/*
61 * Map I2C frequency dividers to FDR and DFSR values
62 *
63 * This structure is used to define the elements of a table that maps I2C
64 * frequency divider (I2C clock rate divided by I2C bus speed) to a value to be
65 * programmed into the Frequency Divider Ratio (FDR) and Digital Filter
66 * Sampling Rate (DFSR) registers.
67 *
68 * The actual table should be defined in the board file, and it must be called
69 * fsl_i2c_speed_map[].
70 *
71 * The last entry of the table must have a value of {-1, X}, where X is same
72 * FDR/DFSR values as the second-to-last entry. This guarantees that any
73 * search through the array will always find a match.
74 *
75 * The values of the divider must be in increasing numerical order, i.e.
76 * fsl_i2c_speed_map[x+1].divider > fsl_i2c_speed_map[x].divider.
77 *
78 * For this table, the values are based on a value of 1 for the DFSR
79 * register. See the application note AN2919 "Determining the I2C Frequency
80 * Divider Ratio for SCL"
TsiChung Liew00648a72008-08-19 00:56:46 +060081 *
82 * ColdFire I2C frequency dividers for FDR values are different from
83 * PowerPC. The protocol to use the I2C module is still the same.
84 * A different table is defined and are based on MCF5xxx user manual.
85 *
Timur Tabib301fda2008-03-14 17:45:29 -050086 */
87static const struct {
88 unsigned short divider;
Timur Tabib301fda2008-03-14 17:45:29 -050089 u8 fdr;
90} fsl_i2c_speed_map[] = {
TsiChung Liew00648a72008-08-19 00:56:46 +060091 {20, 32}, {22, 33}, {24, 34}, {26, 35},
92 {28, 0}, {28, 36}, {30, 1}, {32, 37},
93 {34, 2}, {36, 38}, {40, 3}, {40, 39},
94 {44, 4}, {48, 5}, {48, 40}, {56, 6},
95 {56, 41}, {64, 42}, {68, 7}, {72, 43},
96 {80, 8}, {80, 44}, {88, 9}, {96, 41},
97 {104, 10}, {112, 42}, {128, 11}, {128, 43},
98 {144, 12}, {160, 13}, {160, 48}, {192, 14},
99 {192, 49}, {224, 50}, {240, 15}, {256, 51},
100 {288, 16}, {320, 17}, {320, 52}, {384, 18},
101 {384, 53}, {448, 54}, {480, 19}, {512, 55},
102 {576, 20}, {640, 21}, {640, 56}, {768, 22},
103 {768, 57}, {960, 23}, {896, 58}, {1024, 59},
104 {1152, 24}, {1280, 25}, {1280, 60}, {1536, 26},
105 {1536, 61}, {1792, 62}, {1920, 27}, {2048, 63},
106 {2304, 28}, {2560, 29}, {3072, 30}, {3840, 31},
107 {-1, 31}
Timur Tabib301fda2008-03-14 17:45:29 -0500108};
Tom Rini56762c12017-02-09 15:40:16 -0500109#endif
Timur Tabib301fda2008-03-14 17:45:29 -0500110
111/**
112 * Set the I2C bus speed for a given I2C device
113 *
mario.six@gdsys.cc7d43b4e2016-04-25 08:31:01 +0200114 * @param base: the I2C device registers
Timur Tabib301fda2008-03-14 17:45:29 -0500115 * @i2c_clk: I2C bus clock frequency
116 * @speed: the desired speed of the bus
117 *
118 * The I2C device must be stopped before calling this function.
119 *
120 * The return value is the actual bus speed that is set.
121 */
Mario Sixa5f35c42018-01-15 11:08:07 +0100122static uint set_i2c_bus_speed(const struct fsl_i2c_base *base,
123 uint i2c_clk, uint speed)
Timur Tabib301fda2008-03-14 17:45:29 -0500124{
Mario Sixa5f35c42018-01-15 11:08:07 +0100125 ushort divider = min(i2c_clk / speed, (uint)USHRT_MAX);
Timur Tabib301fda2008-03-14 17:45:29 -0500126
127 /*
128 * We want to choose an FDR/DFSR that generates an I2C bus speed that
129 * is equal to or lower than the requested speed. That means that we
130 * want the first divider that is equal to or greater than the
131 * calculated divider.
132 */
TsiChung Liew00648a72008-08-19 00:56:46 +0600133#ifdef __PPC__
Joakim Tjernlund5a6e0612009-09-17 11:07:17 +0200134 u8 dfsr, fdr = 0x31; /* Default if no FDR found */
135 /* a, b and dfsr matches identifiers A,B and C respectively in AN2919 */
Mario Sixa5f35c42018-01-15 11:08:07 +0100136 ushort a, b, ga, gb;
137 ulong c_div, est_div;
Joakim Tjernlund5a6e0612009-09-17 11:07:17 +0200138
Joakim Tjernlunde677e702009-09-17 11:07:16 +0200139#ifdef CONFIG_FSL_I2C_CUSTOM_DFSR
Joakim Tjernlund5a6e0612009-09-17 11:07:17 +0200140 dfsr = CONFIG_FSL_I2C_CUSTOM_DFSR;
Joakim Tjernlunde677e702009-09-17 11:07:16 +0200141#else
Joakim Tjernlund5a6e0612009-09-17 11:07:17 +0200142 /* Condition 1: dfsr <= 50/T */
143 dfsr = (5 * (i2c_clk / 1000)) / 100000;
Joakim Tjernlunde677e702009-09-17 11:07:16 +0200144#endif
145#ifdef CONFIG_FSL_I2C_CUSTOM_FDR
Joakim Tjernlund5a6e0612009-09-17 11:07:17 +0200146 fdr = CONFIG_FSL_I2C_CUSTOM_FDR;
147 speed = i2c_clk / divider; /* Fake something */
148#else
149 debug("Requested speed:%d, i2c_clk:%d\n", speed, i2c_clk);
150 if (!dfsr)
151 dfsr = 1;
152
153 est_div = ~0;
154 for (ga = 0x4, a = 10; a <= 30; ga++, a += 2) {
155 for (gb = 0; gb < 8; gb++) {
156 b = 16 << gb;
Mario Sixa5f35c42018-01-15 11:08:07 +0100157 c_div = b * (a + ((3 * dfsr) / b) * 2);
158 if (c_div > divider && c_div < est_div) {
159 ushort bin_gb, bin_ga;
Joakim Tjernlund5a6e0612009-09-17 11:07:17 +0200160
161 est_div = c_div;
162 bin_gb = gb << 2;
163 bin_ga = (ga & 0x3) | ((ga & 0x4) << 3);
164 fdr = bin_gb | bin_ga;
165 speed = i2c_clk / est_div;
Mario Sixa5f35c42018-01-15 11:08:07 +0100166
167 debug("FDR: 0x%.2x, ", fdr);
168 debug("div: %ld, ", est_div);
169 debug("ga: 0x%x, gb: 0x%x, ", ga, gb);
170 debug("a: %d, b: %d, speed: %d\n", a, b, speed);
171
Joakim Tjernlund5a6e0612009-09-17 11:07:17 +0200172 /* Condition 2 not accounted for */
173 debug("Tr <= %d ns\n",
174 (b - 3 * dfsr) * 1000000 /
175 (i2c_clk / 1000));
176 }
177 }
178 if (a == 20)
179 a += 2;
180 if (a == 24)
181 a += 4;
182 }
Mario Sixa5f35c42018-01-15 11:08:07 +0100183 debug("divider: %d, est_div: %ld, DFSR: %d\n", divider, est_div, dfsr);
184 debug("FDR: 0x%.2x, speed: %d\n", fdr, speed);
Joakim Tjernlund5a6e0612009-09-17 11:07:17 +0200185#endif
mario.six@gdsys.cc7d43b4e2016-04-25 08:31:01 +0200186 writeb(dfsr, &base->dfsrr); /* set default filter */
187 writeb(fdr, &base->fdr); /* set bus speed */
Joakim Tjernlunde677e702009-09-17 11:07:16 +0200188#else
Mario Sixa5f35c42018-01-15 11:08:07 +0100189 uint i;
Joakim Tjernlund5a6e0612009-09-17 11:07:17 +0200190
191 for (i = 0; i < ARRAY_SIZE(fsl_i2c_speed_map); i++)
192 if (fsl_i2c_speed_map[i].divider >= divider) {
193 u8 fdr;
194
Timur Tabib301fda2008-03-14 17:45:29 -0500195 fdr = fsl_i2c_speed_map[i].fdr;
196 speed = i2c_clk / fsl_i2c_speed_map[i].divider;
mario.six@gdsys.cc7d43b4e2016-04-25 08:31:01 +0200197 writeb(fdr, &base->fdr); /* set bus speed */
Joakim Tjernlunde677e702009-09-17 11:07:16 +0200198
Timur Tabib301fda2008-03-14 17:45:29 -0500199 break;
200 }
Joakim Tjernlund5a6e0612009-09-17 11:07:17 +0200201#endif
Timur Tabib301fda2008-03-14 17:45:29 -0500202 return speed;
203}
204
mario.six@gdsys.cc349686c2016-04-25 08:31:09 +0200205#ifndef CONFIG_DM_I2C
Mario Sixa5f35c42018-01-15 11:08:07 +0100206static uint get_i2c_clock(int bus)
Jerry Huang5e015612011-10-26 15:29:38 +0000207{
208 if (bus)
Simon Glassc2baaec2012-12-13 20:48:49 +0000209 return gd->arch.i2c2_clk; /* I2C2 clock */
Jerry Huang5e015612011-10-26 15:29:38 +0000210 else
Simon Glassc2baaec2012-12-13 20:48:49 +0000211 return gd->arch.i2c1_clk; /* I2C1 clock */
Jerry Huang5e015612011-10-26 15:29:38 +0000212}
mario.six@gdsys.cc349686c2016-04-25 08:31:09 +0200213#endif
Jerry Huang5e015612011-10-26 15:29:38 +0000214
mario.six@gdsys.cc7d43b4e2016-04-25 08:31:01 +0200215static int fsl_i2c_fixup(const struct fsl_i2c_base *base)
Chunhe Lan2e13d572013-08-16 15:10:36 +0800216{
217 const unsigned long long timeout = usec2ticks(CONFIG_I2C_MBB_TIMEOUT);
218 unsigned long long timeval = 0;
219 int ret = -1;
Mario Sixa5f35c42018-01-15 11:08:07 +0100220 uint flags = 0;
Chunhe Lan92546402013-08-16 15:10:37 +0800221
222#ifdef CONFIG_SYS_FSL_ERRATUM_I2C_A004447
Mario Sixa5f35c42018-01-15 11:08:07 +0100223 uint svr = get_svr();
224
Chunhe Lan92546402013-08-16 15:10:37 +0800225 if ((SVR_SOC_VER(svr) == SVR_8548 && IS_SVR_REV(svr, 3, 1)) ||
226 (SVR_REV(svr) <= CONFIG_SYS_FSL_A004447_SVR_REV))
227 flags = I2C_CR_BIT6;
228#endif
Chunhe Lan2e13d572013-08-16 15:10:36 +0800229
mario.six@gdsys.cc7d43b4e2016-04-25 08:31:01 +0200230 writeb(I2C_CR_MEN | I2C_CR_MSTA, &base->cr);
Chunhe Lan2e13d572013-08-16 15:10:36 +0800231
232 timeval = get_ticks();
mario.six@gdsys.cc7d43b4e2016-04-25 08:31:01 +0200233 while (!(readb(&base->sr) & I2C_SR_MBB)) {
Chunhe Lan2e13d572013-08-16 15:10:36 +0800234 if ((get_ticks() - timeval) > timeout)
235 goto err;
236 }
237
mario.six@gdsys.cc7d43b4e2016-04-25 08:31:01 +0200238 if (readb(&base->sr) & I2C_SR_MAL) {
Chunhe Lan2e13d572013-08-16 15:10:36 +0800239 /* SDA is stuck low */
mario.six@gdsys.cc7d43b4e2016-04-25 08:31:01 +0200240 writeb(0, &base->cr);
Chunhe Lan2e13d572013-08-16 15:10:36 +0800241 udelay(100);
mario.six@gdsys.cc7d43b4e2016-04-25 08:31:01 +0200242 writeb(I2C_CR_MSTA | flags, &base->cr);
243 writeb(I2C_CR_MEN | I2C_CR_MSTA | flags, &base->cr);
Chunhe Lan2e13d572013-08-16 15:10:36 +0800244 }
245
mario.six@gdsys.cc7d43b4e2016-04-25 08:31:01 +0200246 readb(&base->dr);
Chunhe Lan2e13d572013-08-16 15:10:36 +0800247
248 timeval = get_ticks();
mario.six@gdsys.cc7d43b4e2016-04-25 08:31:01 +0200249 while (!(readb(&base->sr) & I2C_SR_MIF)) {
Chunhe Lan2e13d572013-08-16 15:10:36 +0800250 if ((get_ticks() - timeval) > timeout)
251 goto err;
252 }
253 ret = 0;
254
255err:
mario.six@gdsys.cc7d43b4e2016-04-25 08:31:01 +0200256 writeb(I2C_CR_MEN | flags, &base->cr);
257 writeb(0, &base->sr);
Chunhe Lan2e13d572013-08-16 15:10:36 +0800258 udelay(100);
259
260 return ret;
261}
262
mario.six@gdsys.cc416b2dc2016-04-25 08:31:08 +0200263static void __i2c_init(const struct fsl_i2c_base *base, int speed, int
264 slaveadd, int i2c_clk, int busnum)
Jon Loeligere4773be2006-10-19 11:02:16 -0500265{
Chunhe Lan2e13d572013-08-16 15:10:36 +0800266 const unsigned long long timeout = usec2ticks(CONFIG_I2C_MBB_TIMEOUT);
267 unsigned long long timeval;
Jon Loeligere4773be2006-10-19 11:02:16 -0500268
Heiko Schocherc5ca01f2009-07-09 12:04:26 +0200269#ifdef CONFIG_SYS_I2C_INIT_BOARD
Richard Retanubundf0149c2010-04-12 15:08:17 -0400270 /* Call board specific i2c bus reset routine before accessing the
271 * environment, which might be in a chip on that bus. For details
272 * about this problem see doc/I2C_Edge_Conditions.
Mario Sixa5f35c42018-01-15 11:08:07 +0100273 */
Heiko Schocherc5ca01f2009-07-09 12:04:26 +0200274 i2c_init_board();
275#endif
mario.six@gdsys.cc7d43b4e2016-04-25 08:31:01 +0200276 writeb(0, &base->cr); /* stop I2C controller */
Heiko Schocherf2850742012-10-24 13:48:22 +0200277 udelay(5); /* let it shutdown in peace */
mario.six@gdsys.cc416b2dc2016-04-25 08:31:08 +0200278 set_i2c_bus_speed(base, i2c_clk, speed);
mario.six@gdsys.cc7d43b4e2016-04-25 08:31:01 +0200279 writeb(slaveadd << 1, &base->adr);/* write slave address */
280 writeb(0x0, &base->sr); /* clear status register */
281 writeb(I2C_CR_MEN, &base->cr); /* start I2C controller */
Richard Retanubundf0149c2010-04-12 15:08:17 -0400282
Chunhe Lan2e13d572013-08-16 15:10:36 +0800283 timeval = get_ticks();
mario.six@gdsys.cc7d43b4e2016-04-25 08:31:01 +0200284 while (readb(&base->sr) & I2C_SR_MBB) {
Chunhe Lan2e13d572013-08-16 15:10:36 +0800285 if ((get_ticks() - timeval) < timeout)
286 continue;
287
mario.six@gdsys.cc7d43b4e2016-04-25 08:31:01 +0200288 if (fsl_i2c_fixup(base))
Chunhe Lan2e13d572013-08-16 15:10:36 +0800289 debug("i2c_init: BUS#%d failed to init\n",
mario.six@gdsys.cc416b2dc2016-04-25 08:31:08 +0200290 busnum);
Chunhe Lan2e13d572013-08-16 15:10:36 +0800291
292 break;
293 }
Jon Loeligere4773be2006-10-19 11:02:16 -0500294}
295
Mario Sixa5f35c42018-01-15 11:08:07 +0100296static int i2c_wait4bus(const struct fsl_i2c_base *base)
Jon Loeligere4773be2006-10-19 11:02:16 -0500297{
Stefan Roese37628252008-08-06 14:05:38 +0200298 unsigned long long timeval = get_ticks();
Timur Tabi2165c622009-09-04 16:28:35 -0500299 const unsigned long long timeout = usec2ticks(CONFIG_I2C_MBB_TIMEOUT);
Jon Loeligere4773be2006-10-19 11:02:16 -0500300
mario.six@gdsys.cc7d43b4e2016-04-25 08:31:01 +0200301 while (readb(&base->sr) & I2C_SR_MBB) {
Timur Tabi2165c622009-09-04 16:28:35 -0500302 if ((get_ticks() - timeval) > timeout)
Jon Loeligere4773be2006-10-19 11:02:16 -0500303 return -1;
Jon Loeligere4773be2006-10-19 11:02:16 -0500304 }
305
306 return 0;
307}
308
Mario Six484cdb82018-01-15 11:08:08 +0100309static int i2c_wait(const struct fsl_i2c_base *base, int write)
Jon Loeligere4773be2006-10-19 11:02:16 -0500310{
311 u32 csr;
Stefan Roese37628252008-08-06 14:05:38 +0200312 unsigned long long timeval = get_ticks();
Timur Tabi2165c622009-09-04 16:28:35 -0500313 const unsigned long long timeout = usec2ticks(CONFIG_I2C_TIMEOUT);
Jon Loeligere4773be2006-10-19 11:02:16 -0500314
315 do {
mario.six@gdsys.cc7d43b4e2016-04-25 08:31:01 +0200316 csr = readb(&base->sr);
Jon Loeligere4773be2006-10-19 11:02:16 -0500317 if (!(csr & I2C_SR_MIF))
318 continue;
Joakim Tjernlundc324b782009-09-17 11:07:15 +0200319 /* Read again to allow register to stabilise */
mario.six@gdsys.cc7d43b4e2016-04-25 08:31:01 +0200320 csr = readb(&base->sr);
Jon Loeligere4773be2006-10-19 11:02:16 -0500321
mario.six@gdsys.cc7d43b4e2016-04-25 08:31:01 +0200322 writeb(0x0, &base->sr);
Jon Loeligere4773be2006-10-19 11:02:16 -0500323
324 if (csr & I2C_SR_MAL) {
Mario Sixa5f35c42018-01-15 11:08:07 +0100325 debug("%s: MAL\n", __func__);
Jon Loeligere4773be2006-10-19 11:02:16 -0500326 return -1;
327 }
328
329 if (!(csr & I2C_SR_MCF)) {
Mario Sixa5f35c42018-01-15 11:08:07 +0100330 debug("%s: unfinished\n", __func__);
Jon Loeligere4773be2006-10-19 11:02:16 -0500331 return -1;
332 }
333
Joakim Tjernlundc32c5f72006-11-28 16:17:27 -0600334 if (write == I2C_WRITE_BIT && (csr & I2C_SR_RXAK)) {
Mario Sixa5f35c42018-01-15 11:08:07 +0100335 debug("%s: No RXACK\n", __func__);
Jon Loeligere4773be2006-10-19 11:02:16 -0500336 return -1;
337 }
338
339 return 0;
Timur Tabi2165c622009-09-04 16:28:35 -0500340 } while ((get_ticks() - timeval) < timeout);
Jon Loeligere4773be2006-10-19 11:02:16 -0500341
Mario Sixa5f35c42018-01-15 11:08:07 +0100342 debug("%s: timed out\n", __func__);
Jon Loeligere4773be2006-10-19 11:02:16 -0500343 return -1;
344}
345
Mario Six484cdb82018-01-15 11:08:08 +0100346static int i2c_write_addr(const struct fsl_i2c_base *base, u8 dev,
347 u8 dir, int rsta)
Jon Loeligere4773be2006-10-19 11:02:16 -0500348{
349 writeb(I2C_CR_MEN | I2C_CR_MSTA | I2C_CR_MTX
350 | (rsta ? I2C_CR_RSTA : 0),
mario.six@gdsys.cc7d43b4e2016-04-25 08:31:01 +0200351 &base->cr);
Jon Loeligere4773be2006-10-19 11:02:16 -0500352
mario.six@gdsys.cc7d43b4e2016-04-25 08:31:01 +0200353 writeb((dev << 1) | dir, &base->dr);
Jon Loeligere4773be2006-10-19 11:02:16 -0500354
mario.six@gdsys.cc416b2dc2016-04-25 08:31:08 +0200355 if (i2c_wait(base, I2C_WRITE_BIT) < 0)
Jon Loeligere4773be2006-10-19 11:02:16 -0500356 return 0;
357
358 return 1;
359}
360
Mario Six484cdb82018-01-15 11:08:08 +0100361static int __i2c_write_data(const struct fsl_i2c_base *base, u8 *data,
362 int length)
Jon Loeligere4773be2006-10-19 11:02:16 -0500363{
364 int i;
365
Jon Loeligere4773be2006-10-19 11:02:16 -0500366 for (i = 0; i < length; i++) {
mario.six@gdsys.cc7d43b4e2016-04-25 08:31:01 +0200367 writeb(data[i], &base->dr);
Jon Loeligere4773be2006-10-19 11:02:16 -0500368
mario.six@gdsys.cc416b2dc2016-04-25 08:31:08 +0200369 if (i2c_wait(base, I2C_WRITE_BIT) < 0)
Jon Loeligere4773be2006-10-19 11:02:16 -0500370 break;
371 }
372
373 return i;
374}
375
Mario Six484cdb82018-01-15 11:08:08 +0100376static int __i2c_read_data(const struct fsl_i2c_base *base, u8 *data,
377 int length)
Jon Loeligere4773be2006-10-19 11:02:16 -0500378{
379 int i;
380
381 writeb(I2C_CR_MEN | I2C_CR_MSTA | ((length == 1) ? I2C_CR_TXAK : 0),
mario.six@gdsys.cc7d43b4e2016-04-25 08:31:01 +0200382 &base->cr);
Jon Loeligere4773be2006-10-19 11:02:16 -0500383
384 /* dummy read */
mario.six@gdsys.cc7d43b4e2016-04-25 08:31:01 +0200385 readb(&base->dr);
Jon Loeligere4773be2006-10-19 11:02:16 -0500386
387 for (i = 0; i < length; i++) {
mario.six@gdsys.cc416b2dc2016-04-25 08:31:08 +0200388 if (i2c_wait(base, I2C_READ_BIT) < 0)
Jon Loeligere4773be2006-10-19 11:02:16 -0500389 break;
390
391 /* Generate ack on last next to last byte */
392 if (i == length - 2)
393 writeb(I2C_CR_MEN | I2C_CR_MSTA | I2C_CR_TXAK,
mario.six@gdsys.cc7d43b4e2016-04-25 08:31:01 +0200394 &base->cr);
Jon Loeligere4773be2006-10-19 11:02:16 -0500395
Joakim Tjernlund6384da22009-09-22 13:40:44 +0200396 /* Do not generate stop on last byte */
Jon Loeligere4773be2006-10-19 11:02:16 -0500397 if (i == length - 1)
Joakim Tjernlund6384da22009-09-22 13:40:44 +0200398 writeb(I2C_CR_MEN | I2C_CR_MSTA | I2C_CR_MTX,
mario.six@gdsys.cc7d43b4e2016-04-25 08:31:01 +0200399 &base->cr);
Jon Loeligere4773be2006-10-19 11:02:16 -0500400
mario.six@gdsys.cc7d43b4e2016-04-25 08:31:01 +0200401 data[i] = readb(&base->dr);
Jon Loeligere4773be2006-10-19 11:02:16 -0500402 }
403
404 return i;
405}
406
Mario Sixa5f35c42018-01-15 11:08:07 +0100407static int __i2c_read(const struct fsl_i2c_base *base, u8 chip_addr, u8 *offset,
408 int olen, u8 *data, int dlen)
Jon Loeligere4773be2006-10-19 11:02:16 -0500409{
mario.six@gdsys.cca4472652016-04-25 08:31:02 +0200410 int ret = -1; /* signal error */
Jon Loeligere4773be2006-10-19 11:02:16 -0500411
mario.six@gdsys.cc416b2dc2016-04-25 08:31:08 +0200412 if (i2c_wait4bus(base) < 0)
Reinhard Pfau2d878de2013-06-26 15:55:14 +0200413 return -1;
414
mario.six@gdsys.cc2eae9d02016-04-25 08:31:03 +0200415 /* Some drivers use offset lengths in excess of 4 bytes. These drivers
416 * adhere to the following convention:
417 * - the offset length is passed as negative (that is, the absolute
418 * value of olen is the actual offset length)
419 * - the offset itself is passed in data, which is overwritten by the
420 * subsequent read operation
Shaveta Leekhafdd7efe2014-04-24 14:51:23 +0530421 */
mario.six@gdsys.cca4472652016-04-25 08:31:02 +0200422 if (olen < 0) {
mario.six@gdsys.cc416b2dc2016-04-25 08:31:08 +0200423 if (i2c_write_addr(base, chip_addr, I2C_WRITE_BIT, 0) != 0)
424 ret = __i2c_write_data(base, data, -olen);
Shaveta Leekhafdd7efe2014-04-24 14:51:23 +0530425
mario.six@gdsys.cc8230fc42016-04-25 08:31:04 +0200426 if (ret != -olen)
Shaveta Leekhafdd7efe2014-04-24 14:51:23 +0530427 return -1;
428
mario.six@gdsys.cc416b2dc2016-04-25 08:31:08 +0200429 if (dlen && i2c_write_addr(base, chip_addr,
mario.six@gdsys.cca4472652016-04-25 08:31:02 +0200430 I2C_READ_BIT, 1) != 0)
mario.six@gdsys.cc416b2dc2016-04-25 08:31:08 +0200431 ret = __i2c_read_data(base, data, dlen);
Shaveta Leekhafdd7efe2014-04-24 14:51:23 +0530432 } else {
mario.six@gdsys.cca4472652016-04-25 08:31:02 +0200433 if ((!dlen || olen > 0) &&
mario.six@gdsys.cc416b2dc2016-04-25 08:31:08 +0200434 i2c_write_addr(base, chip_addr, I2C_WRITE_BIT, 0) != 0 &&
435 __i2c_write_data(base, offset, olen) == olen)
mario.six@gdsys.cca4472652016-04-25 08:31:02 +0200436 ret = 0; /* No error so far */
Joakim Tjernlundb648fe72007-01-31 11:04:19 +0100437
mario.six@gdsys.cc416b2dc2016-04-25 08:31:08 +0200438 if (dlen && i2c_write_addr(base, chip_addr, I2C_READ_BIT,
mario.six@gdsys.cca4472652016-04-25 08:31:02 +0200439 olen ? 1 : 0) != 0)
mario.six@gdsys.cc416b2dc2016-04-25 08:31:08 +0200440 ret = __i2c_read_data(base, data, dlen);
Shaveta Leekhafdd7efe2014-04-24 14:51:23 +0530441 }
Jon Loeligere4773be2006-10-19 11:02:16 -0500442
mario.six@gdsys.cc7d43b4e2016-04-25 08:31:01 +0200443 writeb(I2C_CR_MEN, &base->cr);
Jon Loeligere4773be2006-10-19 11:02:16 -0500444
mario.six@gdsys.cc416b2dc2016-04-25 08:31:08 +0200445 if (i2c_wait4bus(base)) /* Wait until STOP */
Joakim Tjernlund6384da22009-09-22 13:40:44 +0200446 debug("i2c_read: wait4bus timed out\n");
447
mario.six@gdsys.cca4472652016-04-25 08:31:02 +0200448 if (ret == dlen)
449 return 0;
Jon Loeliger24df9772006-10-19 12:02:24 -0500450
451 return -1;
Jon Loeligere4773be2006-10-19 11:02:16 -0500452}
453
Mario Sixa5f35c42018-01-15 11:08:07 +0100454static int __i2c_write(const struct fsl_i2c_base *base, u8 chip_addr,
455 u8 *offset, int olen, u8 *data, int dlen)
Jon Loeligere4773be2006-10-19 11:02:16 -0500456{
mario.six@gdsys.cca4472652016-04-25 08:31:02 +0200457 int ret = -1; /* signal error */
Jon Loeligere4773be2006-10-19 11:02:16 -0500458
mario.six@gdsys.cc416b2dc2016-04-25 08:31:08 +0200459 if (i2c_wait4bus(base) < 0)
Chunhe Lan2e13d572013-08-16 15:10:36 +0800460 return -1;
461
mario.six@gdsys.cc416b2dc2016-04-25 08:31:08 +0200462 if (i2c_write_addr(base, chip_addr, I2C_WRITE_BIT, 0) != 0 &&
463 __i2c_write_data(base, offset, olen) == olen) {
464 ret = __i2c_write_data(base, data, dlen);
Jon Loeliger24df9772006-10-19 12:02:24 -0500465 }
Jon Loeligere4773be2006-10-19 11:02:16 -0500466
mario.six@gdsys.cc7d43b4e2016-04-25 08:31:01 +0200467 writeb(I2C_CR_MEN, &base->cr);
mario.six@gdsys.cc416b2dc2016-04-25 08:31:08 +0200468 if (i2c_wait4bus(base)) /* Wait until STOP */
Joakim Tjernlundc324b782009-09-17 11:07:15 +0200469 debug("i2c_write: wait4bus timed out\n");
Jon Loeligere4773be2006-10-19 11:02:16 -0500470
mario.six@gdsys.cca4472652016-04-25 08:31:02 +0200471 if (ret == dlen)
472 return 0;
Jon Loeliger24df9772006-10-19 12:02:24 -0500473
474 return -1;
Jon Loeligere4773be2006-10-19 11:02:16 -0500475}
476
Mario Sixa5f35c42018-01-15 11:08:07 +0100477static int __i2c_probe_chip(const struct fsl_i2c_base *base, uchar chip)
Jon Loeligere4773be2006-10-19 11:02:16 -0500478{
Mario Sixa5f35c42018-01-15 11:08:07 +0100479 /* For unknown reason the controller will ACK when
Joakim Tjernlundb648fe72007-01-31 11:04:19 +0100480 * probing for a slave with the same address, so skip
481 * it.
Jon Loeligere4773be2006-10-19 11:02:16 -0500482 */
mario.six@gdsys.cc7d43b4e2016-04-25 08:31:01 +0200483 if (chip == (readb(&base->adr) >> 1))
Timur Tabiab347542006-11-03 19:15:00 -0600484 return -1;
Timur Tabiab347542006-11-03 19:15:00 -0600485
mario.six@gdsys.cc416b2dc2016-04-25 08:31:08 +0200486 return __i2c_read(base, chip, 0, 0, NULL, 0);
Timur Tabiab347542006-11-03 19:15:00 -0600487}
488
Mario Sixa5f35c42018-01-15 11:08:07 +0100489static uint __i2c_set_bus_speed(const struct fsl_i2c_base *base,
490 uint speed, int i2c_clk)
Timur Tabiab347542006-11-03 19:15:00 -0600491{
mario.six@gdsys.cc7d43b4e2016-04-25 08:31:01 +0200492 writeb(0, &base->cr); /* stop controller */
mario.six@gdsys.cc416b2dc2016-04-25 08:31:08 +0200493 set_i2c_bus_speed(base, i2c_clk, speed);
mario.six@gdsys.cc7d43b4e2016-04-25 08:31:01 +0200494 writeb(I2C_CR_MEN, &base->cr); /* start controller */
Timur Tabib301fda2008-03-14 17:45:29 -0500495
496 return 0;
Timur Tabiab347542006-11-03 19:15:00 -0600497}
498
mario.six@gdsys.cc349686c2016-04-25 08:31:09 +0200499#ifndef CONFIG_DM_I2C
mario.six@gdsys.cc2d96aa72016-04-25 08:31:07 +0200500static void fsl_i2c_init(struct i2c_adapter *adap, int speed, int slaveadd)
501{
mario.six@gdsys.cc416b2dc2016-04-25 08:31:08 +0200502 __i2c_init(i2c_base[adap->hwadapnr], speed, slaveadd,
503 get_i2c_clock(adap->hwadapnr), adap->hwadapnr);
mario.six@gdsys.cc2d96aa72016-04-25 08:31:07 +0200504}
505
Mario Sixa5f35c42018-01-15 11:08:07 +0100506static int fsl_i2c_probe_chip(struct i2c_adapter *adap, uchar chip)
mario.six@gdsys.cc2d96aa72016-04-25 08:31:07 +0200507{
mario.six@gdsys.cc416b2dc2016-04-25 08:31:08 +0200508 return __i2c_probe_chip(i2c_base[adap->hwadapnr], chip);
mario.six@gdsys.cc2d96aa72016-04-25 08:31:07 +0200509}
510
Mario Sixa5f35c42018-01-15 11:08:07 +0100511static int fsl_i2c_read(struct i2c_adapter *adap, u8 chip_addr, uint offset,
512 int olen, u8 *data, int dlen)
mario.six@gdsys.cc2d96aa72016-04-25 08:31:07 +0200513{
mario.six@gdsys.cc416b2dc2016-04-25 08:31:08 +0200514 u8 *o = (u8 *)&offset;
Mario Sixa5f35c42018-01-15 11:08:07 +0100515
mario.six@gdsys.cc416b2dc2016-04-25 08:31:08 +0200516 return __i2c_read(i2c_base[adap->hwadapnr], chip_addr, &o[4 - olen],
517 olen, data, dlen);
mario.six@gdsys.cc2d96aa72016-04-25 08:31:07 +0200518}
519
Mario Sixa5f35c42018-01-15 11:08:07 +0100520static int fsl_i2c_write(struct i2c_adapter *adap, u8 chip_addr, uint offset,
521 int olen, u8 *data, int dlen)
mario.six@gdsys.cc2d96aa72016-04-25 08:31:07 +0200522{
mario.six@gdsys.cc416b2dc2016-04-25 08:31:08 +0200523 u8 *o = (u8 *)&offset;
Mario Sixa5f35c42018-01-15 11:08:07 +0100524
mario.six@gdsys.cc416b2dc2016-04-25 08:31:08 +0200525 return __i2c_write(i2c_base[adap->hwadapnr], chip_addr, &o[4 - olen],
526 olen, data, dlen);
mario.six@gdsys.cc2d96aa72016-04-25 08:31:07 +0200527}
528
Mario Sixa5f35c42018-01-15 11:08:07 +0100529static uint fsl_i2c_set_bus_speed(struct i2c_adapter *adap, uint speed)
mario.six@gdsys.cc2d96aa72016-04-25 08:31:07 +0200530{
mario.six@gdsys.cc416b2dc2016-04-25 08:31:08 +0200531 return __i2c_set_bus_speed(i2c_base[adap->hwadapnr], speed,
532 get_i2c_clock(adap->hwadapnr));
mario.six@gdsys.cc2d96aa72016-04-25 08:31:07 +0200533}
534
Heiko Schocherf2850742012-10-24 13:48:22 +0200535/*
536 * Register fsl i2c adapters
537 */
mario.six@gdsys.cc4a790692016-04-25 08:31:05 +0200538U_BOOT_I2C_ADAP_COMPLETE(fsl_0, fsl_i2c_init, fsl_i2c_probe_chip, fsl_i2c_read,
Heiko Schocherf2850742012-10-24 13:48:22 +0200539 fsl_i2c_write, fsl_i2c_set_bus_speed,
540 CONFIG_SYS_FSL_I2C_SPEED, CONFIG_SYS_FSL_I2C_SLAVE,
541 0)
542#ifdef CONFIG_SYS_FSL_I2C2_OFFSET
mario.six@gdsys.cc4a790692016-04-25 08:31:05 +0200543U_BOOT_I2C_ADAP_COMPLETE(fsl_1, fsl_i2c_init, fsl_i2c_probe_chip, fsl_i2c_read,
Heiko Schocherf2850742012-10-24 13:48:22 +0200544 fsl_i2c_write, fsl_i2c_set_bus_speed,
545 CONFIG_SYS_FSL_I2C2_SPEED, CONFIG_SYS_FSL_I2C2_SLAVE,
546 1)
Heiko Schocher2c9f3a42009-02-24 11:30:37 +0100547#endif
Shengzhou Liu37787f62014-07-07 12:17:48 +0800548#ifdef CONFIG_SYS_FSL_I2C3_OFFSET
mario.six@gdsys.cc4a790692016-04-25 08:31:05 +0200549U_BOOT_I2C_ADAP_COMPLETE(fsl_2, fsl_i2c_init, fsl_i2c_probe_chip, fsl_i2c_read,
Shengzhou Liu37787f62014-07-07 12:17:48 +0800550 fsl_i2c_write, fsl_i2c_set_bus_speed,
551 CONFIG_SYS_FSL_I2C3_SPEED, CONFIG_SYS_FSL_I2C3_SLAVE,
552 2)
553#endif
554#ifdef CONFIG_SYS_FSL_I2C4_OFFSET
mario.six@gdsys.cc4a790692016-04-25 08:31:05 +0200555U_BOOT_I2C_ADAP_COMPLETE(fsl_3, fsl_i2c_init, fsl_i2c_probe_chip, fsl_i2c_read,
Shengzhou Liu37787f62014-07-07 12:17:48 +0800556 fsl_i2c_write, fsl_i2c_set_bus_speed,
557 CONFIG_SYS_FSL_I2C4_SPEED, CONFIG_SYS_FSL_I2C4_SLAVE,
558 3)
559#endif
mario.six@gdsys.cc349686c2016-04-25 08:31:09 +0200560#else /* CONFIG_DM_I2C */
561static int fsl_i2c_probe_chip(struct udevice *bus, u32 chip_addr,
562 u32 chip_flags)
563{
564 struct fsl_i2c_dev *dev = dev_get_priv(bus);
Mario Sixa5f35c42018-01-15 11:08:07 +0100565
mario.six@gdsys.cc349686c2016-04-25 08:31:09 +0200566 return __i2c_probe_chip(dev->base, chip_addr);
567}
568
Mario Sixa5f35c42018-01-15 11:08:07 +0100569static int fsl_i2c_set_bus_speed(struct udevice *bus, uint speed)
mario.six@gdsys.cc349686c2016-04-25 08:31:09 +0200570{
571 struct fsl_i2c_dev *dev = dev_get_priv(bus);
Mario Sixa5f35c42018-01-15 11:08:07 +0100572
mario.six@gdsys.cc349686c2016-04-25 08:31:09 +0200573 return __i2c_set_bus_speed(dev->base, speed, dev->i2c_clk);
574}
575
576static int fsl_i2c_ofdata_to_platdata(struct udevice *bus)
577{
578 struct fsl_i2c_dev *dev = dev_get_priv(bus);
Mario Six2fe2ed62018-03-28 14:37:44 +0200579 struct clk clock;
mario.six@gdsys.cc349686c2016-04-25 08:31:09 +0200580
Mario Six486b2d52018-03-28 14:37:43 +0200581 dev->base = map_sysmem(dev_read_addr(bus), sizeof(struct fsl_i2c_base));
mario.six@gdsys.cc349686c2016-04-25 08:31:09 +0200582
583 if (!dev->base)
584 return -ENOMEM;
585
Mario Six84b68b82018-01-15 11:08:09 +0100586 dev->index = dev_read_u32_default(bus, "cell-index", -1);
587 dev->slaveadd = dev_read_u32_default(bus, "u-boot,i2c-slave-addr",
588 0x7f);
Simon Glassf0c99c52020-01-23 11:48:22 -0700589 dev->speed = dev_read_u32_default(bus, "clock-frequency",
590 I2C_SPEED_FAST_RATE);
mario.six@gdsys.cc349686c2016-04-25 08:31:09 +0200591
Mario Six2fe2ed62018-03-28 14:37:44 +0200592 if (!clk_get_by_index(bus, 0, &clock))
593 dev->i2c_clk = clk_get_rate(&clock);
594 else
595 dev->i2c_clk = dev->index ? gd->arch.i2c2_clk :
596 gd->arch.i2c1_clk;
mario.six@gdsys.cc349686c2016-04-25 08:31:09 +0200597
598 return 0;
599}
600
601static int fsl_i2c_probe(struct udevice *bus)
602{
603 struct fsl_i2c_dev *dev = dev_get_priv(bus);
Mario Sixa5f35c42018-01-15 11:08:07 +0100604
mario.six@gdsys.cc349686c2016-04-25 08:31:09 +0200605 __i2c_init(dev->base, dev->speed, dev->slaveadd, dev->i2c_clk,
606 dev->index);
607 return 0;
608}
609
610static int fsl_i2c_xfer(struct udevice *bus, struct i2c_msg *msg, int nmsgs)
611{
612 struct fsl_i2c_dev *dev = dev_get_priv(bus);
613 struct i2c_msg *dmsg, *omsg, dummy;
614
615 memset(&dummy, 0, sizeof(struct i2c_msg));
616
617 /* We expect either two messages (one with an offset and one with the
Mario Sixa5f35c42018-01-15 11:08:07 +0100618 * actual data) or one message (just data)
619 */
mario.six@gdsys.cc349686c2016-04-25 08:31:09 +0200620 if (nmsgs > 2 || nmsgs == 0) {
621 debug("%s: Only one or two messages are supported.", __func__);
622 return -1;
623 }
624
625 omsg = nmsgs == 1 ? &dummy : msg;
626 dmsg = nmsgs == 1 ? msg : msg + 1;
627
628 if (dmsg->flags & I2C_M_RD)
629 return __i2c_read(dev->base, dmsg->addr, omsg->buf, omsg->len,
630 dmsg->buf, dmsg->len);
631 else
632 return __i2c_write(dev->base, dmsg->addr, omsg->buf, omsg->len,
633 dmsg->buf, dmsg->len);
634}
635
636static const struct dm_i2c_ops fsl_i2c_ops = {
637 .xfer = fsl_i2c_xfer,
638 .probe_chip = fsl_i2c_probe_chip,
639 .set_bus_speed = fsl_i2c_set_bus_speed,
640};
641
642static const struct udevice_id fsl_i2c_ids[] = {
643 { .compatible = "fsl-i2c", },
644 { /* sentinel */ }
645};
646
647U_BOOT_DRIVER(i2c_fsl) = {
648 .name = "i2c_fsl",
649 .id = UCLASS_I2C,
650 .of_match = fsl_i2c_ids,
651 .probe = fsl_i2c_probe,
652 .ofdata_to_platdata = fsl_i2c_ofdata_to_platdata,
653 .priv_auto_alloc_size = sizeof(struct fsl_i2c_dev),
654 .ops = &fsl_i2c_ops,
655};
656
657#endif /* CONFIG_DM_I2C */