blob: eafd801cdc3af047750fb4d8d489e04d6df041b7 [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>
Simon Glass3ba929a2020-10-30 21:38:53 -060014#include <asm/global_data.h>
Jon Loeligere4773be2006-10-19 11:02:16 -050015#include <asm/io.h>
Jon Loeliger43d818f2006-10-20 15:50:15 -050016#include <asm/fsl_i2c.h> /* HW definitions */
Mario Six2fe2ed62018-03-28 14:37:44 +020017#include <clk.h>
mario.six@gdsys.cc349686c2016-04-25 08:31:09 +020018#include <dm.h>
19#include <mapmem.h>
Simon Glassdbd79542020-05-10 11:40:11 -060020#include <linux/delay.h>
Jon Loeligere4773be2006-10-19 11:02:16 -050021
Timur Tabi2165c622009-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 Leekha2ba098f2014-11-03 10:43:14 +053035#define CONFIG_I2C_TIMEOUT 100000
Timur Tabi2165c622009-09-04 16:28:35 -050036#endif
Jon Loeligere4773be2006-10-19 11:02:16 -050037
Joakim Tjernlundc32c5f72006-11-28 16:17:27 -060038#define I2C_READ_BIT 1
39#define I2C_WRITE_BIT 0
40
Timur Tabib301fda2008-03-14 17:45:29 -050041DECLARE_GLOBAL_DATA_PTR;
42
Igor Opaniukf7c91762021-02-09 13:52:45 +020043#if !CONFIG_IS_ENABLED(DM_I2C)
mario.six@gdsys.cc7d43b4e2016-04-25 08:31:01 +020044static const struct fsl_i2c_base *i2c_base[4] = {
45 (struct fsl_i2c_base *)(CONFIG_SYS_IMMR + CONFIG_SYS_FSL_I2C_OFFSET),
Heiko Schocherf2850742012-10-24 13:48:22 +020046#ifdef CONFIG_SYS_FSL_I2C2_OFFSET
mario.six@gdsys.cc7d43b4e2016-04-25 08:31:01 +020047 (struct fsl_i2c_base *)(CONFIG_SYS_IMMR + CONFIG_SYS_FSL_I2C2_OFFSET),
Shengzhou Liu37787f62014-07-07 12:17:48 +080048#endif
49#ifdef CONFIG_SYS_FSL_I2C3_OFFSET
mario.six@gdsys.cc7d43b4e2016-04-25 08:31:01 +020050 (struct fsl_i2c_base *)(CONFIG_SYS_IMMR + CONFIG_SYS_FSL_I2C3_OFFSET),
Shengzhou Liu37787f62014-07-07 12:17:48 +080051#endif
52#ifdef CONFIG_SYS_FSL_I2C4_OFFSET
mario.six@gdsys.cc7d43b4e2016-04-25 08:31:01 +020053 (struct fsl_i2c_base *)(CONFIG_SYS_IMMR + CONFIG_SYS_FSL_I2C4_OFFSET)
Timur Tabiab347542006-11-03 19:15:00 -060054#endif
55};
mario.six@gdsys.cc349686c2016-04-25 08:31:09 +020056#endif
Jon Loeligere4773be2006-10-19 11:02:16 -050057
Timur Tabib301fda2008-03-14 17:45:29 -050058/* I2C speed map for a DFSR value of 1 */
59
Tom Rini56762c12017-02-09 15:40:16 -050060#ifdef __M68K__
Timur Tabib301fda2008-03-14 17:45:29 -050061/*
62 * Map I2C frequency dividers to FDR and DFSR values
63 *
64 * This structure is used to define the elements of a table that maps I2C
65 * frequency divider (I2C clock rate divided by I2C bus speed) to a value to be
66 * programmed into the Frequency Divider Ratio (FDR) and Digital Filter
67 * Sampling Rate (DFSR) registers.
68 *
69 * The actual table should be defined in the board file, and it must be called
70 * fsl_i2c_speed_map[].
71 *
72 * The last entry of the table must have a value of {-1, X}, where X is same
73 * FDR/DFSR values as the second-to-last entry. This guarantees that any
74 * search through the array will always find a match.
75 *
76 * The values of the divider must be in increasing numerical order, i.e.
77 * fsl_i2c_speed_map[x+1].divider > fsl_i2c_speed_map[x].divider.
78 *
79 * For this table, the values are based on a value of 1 for the DFSR
80 * register. See the application note AN2919 "Determining the I2C Frequency
81 * Divider Ratio for SCL"
TsiChung Liew00648a72008-08-19 00:56:46 +060082 *
83 * ColdFire I2C frequency dividers for FDR values are different from
84 * PowerPC. The protocol to use the I2C module is still the same.
85 * A different table is defined and are based on MCF5xxx user manual.
86 *
Timur Tabib301fda2008-03-14 17:45:29 -050087 */
88static const struct {
89 unsigned short divider;
Timur Tabib301fda2008-03-14 17:45:29 -050090 u8 fdr;
91} fsl_i2c_speed_map[] = {
TsiChung Liew00648a72008-08-19 00:56:46 +060092 {20, 32}, {22, 33}, {24, 34}, {26, 35},
93 {28, 0}, {28, 36}, {30, 1}, {32, 37},
94 {34, 2}, {36, 38}, {40, 3}, {40, 39},
95 {44, 4}, {48, 5}, {48, 40}, {56, 6},
96 {56, 41}, {64, 42}, {68, 7}, {72, 43},
97 {80, 8}, {80, 44}, {88, 9}, {96, 41},
98 {104, 10}, {112, 42}, {128, 11}, {128, 43},
99 {144, 12}, {160, 13}, {160, 48}, {192, 14},
100 {192, 49}, {224, 50}, {240, 15}, {256, 51},
101 {288, 16}, {320, 17}, {320, 52}, {384, 18},
102 {384, 53}, {448, 54}, {480, 19}, {512, 55},
103 {576, 20}, {640, 21}, {640, 56}, {768, 22},
104 {768, 57}, {960, 23}, {896, 58}, {1024, 59},
105 {1152, 24}, {1280, 25}, {1280, 60}, {1536, 26},
106 {1536, 61}, {1792, 62}, {1920, 27}, {2048, 63},
107 {2304, 28}, {2560, 29}, {3072, 30}, {3840, 31},
108 {-1, 31}
Timur Tabib301fda2008-03-14 17:45:29 -0500109};
Tom Rini56762c12017-02-09 15:40:16 -0500110#endif
Timur Tabib301fda2008-03-14 17:45:29 -0500111
112/**
113 * Set the I2C bus speed for a given I2C device
114 *
mario.six@gdsys.cc7d43b4e2016-04-25 08:31:01 +0200115 * @param base: the I2C device registers
Timur Tabib301fda2008-03-14 17:45:29 -0500116 * @i2c_clk: I2C bus clock frequency
117 * @speed: the desired speed of the bus
118 *
119 * The I2C device must be stopped before calling this function.
120 *
121 * The return value is the actual bus speed that is set.
122 */
Mario Sixa5f35c42018-01-15 11:08:07 +0100123static uint set_i2c_bus_speed(const struct fsl_i2c_base *base,
124 uint i2c_clk, uint speed)
Timur Tabib301fda2008-03-14 17:45:29 -0500125{
Mario Sixa5f35c42018-01-15 11:08:07 +0100126 ushort divider = min(i2c_clk / speed, (uint)USHRT_MAX);
Timur Tabib301fda2008-03-14 17:45:29 -0500127
128 /*
129 * We want to choose an FDR/DFSR that generates an I2C bus speed that
130 * is equal to or lower than the requested speed. That means that we
131 * want the first divider that is equal to or greater than the
132 * calculated divider.
133 */
TsiChung Liew00648a72008-08-19 00:56:46 +0600134#ifdef __PPC__
Joakim Tjernlund5a6e0612009-09-17 11:07:17 +0200135 u8 dfsr, fdr = 0x31; /* Default if no FDR found */
136 /* a, b and dfsr matches identifiers A,B and C respectively in AN2919 */
Mario Sixa5f35c42018-01-15 11:08:07 +0100137 ushort a, b, ga, gb;
138 ulong c_div, est_div;
Joakim Tjernlund5a6e0612009-09-17 11:07:17 +0200139
Joakim Tjernlunde677e702009-09-17 11:07:16 +0200140#ifdef CONFIG_FSL_I2C_CUSTOM_DFSR
Joakim Tjernlund5a6e0612009-09-17 11:07:17 +0200141 dfsr = CONFIG_FSL_I2C_CUSTOM_DFSR;
Joakim Tjernlunde677e702009-09-17 11:07:16 +0200142#else
Joakim Tjernlund5a6e0612009-09-17 11:07:17 +0200143 /* Condition 1: dfsr <= 50/T */
144 dfsr = (5 * (i2c_clk / 1000)) / 100000;
Joakim Tjernlunde677e702009-09-17 11:07:16 +0200145#endif
146#ifdef CONFIG_FSL_I2C_CUSTOM_FDR
Joakim Tjernlund5a6e0612009-09-17 11:07:17 +0200147 fdr = CONFIG_FSL_I2C_CUSTOM_FDR;
148 speed = i2c_clk / divider; /* Fake something */
149#else
150 debug("Requested speed:%d, i2c_clk:%d\n", speed, i2c_clk);
151 if (!dfsr)
152 dfsr = 1;
153
154 est_div = ~0;
155 for (ga = 0x4, a = 10; a <= 30; ga++, a += 2) {
156 for (gb = 0; gb < 8; gb++) {
157 b = 16 << gb;
Mario Sixa5f35c42018-01-15 11:08:07 +0100158 c_div = b * (a + ((3 * dfsr) / b) * 2);
159 if (c_div > divider && c_div < est_div) {
160 ushort bin_gb, bin_ga;
Joakim Tjernlund5a6e0612009-09-17 11:07:17 +0200161
162 est_div = c_div;
163 bin_gb = gb << 2;
164 bin_ga = (ga & 0x3) | ((ga & 0x4) << 3);
165 fdr = bin_gb | bin_ga;
166 speed = i2c_clk / est_div;
Mario Sixa5f35c42018-01-15 11:08:07 +0100167
168 debug("FDR: 0x%.2x, ", fdr);
169 debug("div: %ld, ", est_div);
170 debug("ga: 0x%x, gb: 0x%x, ", ga, gb);
171 debug("a: %d, b: %d, speed: %d\n", a, b, speed);
172
Joakim Tjernlund5a6e0612009-09-17 11:07:17 +0200173 /* Condition 2 not accounted for */
174 debug("Tr <= %d ns\n",
175 (b - 3 * dfsr) * 1000000 /
176 (i2c_clk / 1000));
177 }
178 }
179 if (a == 20)
180 a += 2;
181 if (a == 24)
182 a += 4;
183 }
Mario Sixa5f35c42018-01-15 11:08:07 +0100184 debug("divider: %d, est_div: %ld, DFSR: %d\n", divider, est_div, dfsr);
185 debug("FDR: 0x%.2x, speed: %d\n", fdr, speed);
Joakim Tjernlund5a6e0612009-09-17 11:07:17 +0200186#endif
mario.six@gdsys.cc7d43b4e2016-04-25 08:31:01 +0200187 writeb(dfsr, &base->dfsrr); /* set default filter */
188 writeb(fdr, &base->fdr); /* set bus speed */
Joakim Tjernlunde677e702009-09-17 11:07:16 +0200189#else
Mario Sixa5f35c42018-01-15 11:08:07 +0100190 uint i;
Joakim Tjernlund5a6e0612009-09-17 11:07:17 +0200191
192 for (i = 0; i < ARRAY_SIZE(fsl_i2c_speed_map); i++)
193 if (fsl_i2c_speed_map[i].divider >= divider) {
194 u8 fdr;
195
Timur Tabib301fda2008-03-14 17:45:29 -0500196 fdr = fsl_i2c_speed_map[i].fdr;
197 speed = i2c_clk / fsl_i2c_speed_map[i].divider;
mario.six@gdsys.cc7d43b4e2016-04-25 08:31:01 +0200198 writeb(fdr, &base->fdr); /* set bus speed */
Joakim Tjernlunde677e702009-09-17 11:07:16 +0200199
Timur Tabib301fda2008-03-14 17:45:29 -0500200 break;
201 }
Joakim Tjernlund5a6e0612009-09-17 11:07:17 +0200202#endif
Timur Tabib301fda2008-03-14 17:45:29 -0500203 return speed;
204}
205
Igor Opaniukf7c91762021-02-09 13:52:45 +0200206#if !CONFIG_IS_ENABLED(DM_I2C)
Mario Sixa5f35c42018-01-15 11:08:07 +0100207static uint get_i2c_clock(int bus)
Jerry Huang5e015612011-10-26 15:29:38 +0000208{
209 if (bus)
Simon Glassc2baaec2012-12-13 20:48:49 +0000210 return gd->arch.i2c2_clk; /* I2C2 clock */
Jerry Huang5e015612011-10-26 15:29:38 +0000211 else
Simon Glassc2baaec2012-12-13 20:48:49 +0000212 return gd->arch.i2c1_clk; /* I2C1 clock */
Jerry Huang5e015612011-10-26 15:29:38 +0000213}
mario.six@gdsys.cc349686c2016-04-25 08:31:09 +0200214#endif
Jerry Huang5e015612011-10-26 15:29:38 +0000215
mario.six@gdsys.cc7d43b4e2016-04-25 08:31:01 +0200216static int fsl_i2c_fixup(const struct fsl_i2c_base *base)
Chunhe Lan2e13d572013-08-16 15:10:36 +0800217{
218 const unsigned long long timeout = usec2ticks(CONFIG_I2C_MBB_TIMEOUT);
219 unsigned long long timeval = 0;
220 int ret = -1;
Mario Sixa5f35c42018-01-15 11:08:07 +0100221 uint flags = 0;
Chunhe Lan92546402013-08-16 15:10:37 +0800222
223#ifdef CONFIG_SYS_FSL_ERRATUM_I2C_A004447
Mario Sixa5f35c42018-01-15 11:08:07 +0100224 uint svr = get_svr();
225
Chunhe Lan92546402013-08-16 15:10:37 +0800226 if ((SVR_SOC_VER(svr) == SVR_8548 && IS_SVR_REV(svr, 3, 1)) ||
227 (SVR_REV(svr) <= CONFIG_SYS_FSL_A004447_SVR_REV))
228 flags = I2C_CR_BIT6;
229#endif
Chunhe Lan2e13d572013-08-16 15:10:36 +0800230
mario.six@gdsys.cc7d43b4e2016-04-25 08:31:01 +0200231 writeb(I2C_CR_MEN | I2C_CR_MSTA, &base->cr);
Chunhe Lan2e13d572013-08-16 15:10:36 +0800232
233 timeval = get_ticks();
mario.six@gdsys.cc7d43b4e2016-04-25 08:31:01 +0200234 while (!(readb(&base->sr) & I2C_SR_MBB)) {
Chunhe Lan2e13d572013-08-16 15:10:36 +0800235 if ((get_ticks() - timeval) > timeout)
236 goto err;
237 }
238
mario.six@gdsys.cc7d43b4e2016-04-25 08:31:01 +0200239 if (readb(&base->sr) & I2C_SR_MAL) {
Chunhe Lan2e13d572013-08-16 15:10:36 +0800240 /* SDA is stuck low */
mario.six@gdsys.cc7d43b4e2016-04-25 08:31:01 +0200241 writeb(0, &base->cr);
Chunhe Lan2e13d572013-08-16 15:10:36 +0800242 udelay(100);
mario.six@gdsys.cc7d43b4e2016-04-25 08:31:01 +0200243 writeb(I2C_CR_MSTA | flags, &base->cr);
244 writeb(I2C_CR_MEN | I2C_CR_MSTA | flags, &base->cr);
Chunhe Lan2e13d572013-08-16 15:10:36 +0800245 }
246
mario.six@gdsys.cc7d43b4e2016-04-25 08:31:01 +0200247 readb(&base->dr);
Chunhe Lan2e13d572013-08-16 15:10:36 +0800248
249 timeval = get_ticks();
mario.six@gdsys.cc7d43b4e2016-04-25 08:31:01 +0200250 while (!(readb(&base->sr) & I2C_SR_MIF)) {
Chunhe Lan2e13d572013-08-16 15:10:36 +0800251 if ((get_ticks() - timeval) > timeout)
252 goto err;
253 }
254 ret = 0;
255
256err:
mario.six@gdsys.cc7d43b4e2016-04-25 08:31:01 +0200257 writeb(I2C_CR_MEN | flags, &base->cr);
258 writeb(0, &base->sr);
Chunhe Lan2e13d572013-08-16 15:10:36 +0800259 udelay(100);
260
261 return ret;
262}
263
mario.six@gdsys.cc416b2dc2016-04-25 08:31:08 +0200264static void __i2c_init(const struct fsl_i2c_base *base, int speed, int
265 slaveadd, int i2c_clk, int busnum)
Jon Loeligere4773be2006-10-19 11:02:16 -0500266{
Chunhe Lan2e13d572013-08-16 15:10:36 +0800267 const unsigned long long timeout = usec2ticks(CONFIG_I2C_MBB_TIMEOUT);
268 unsigned long long timeval;
Jon Loeligere4773be2006-10-19 11:02:16 -0500269
Heiko Schocherc5ca01f2009-07-09 12:04:26 +0200270#ifdef CONFIG_SYS_I2C_INIT_BOARD
Richard Retanubundf0149c2010-04-12 15:08:17 -0400271 /* Call board specific i2c bus reset routine before accessing the
272 * environment, which might be in a chip on that bus. For details
273 * about this problem see doc/I2C_Edge_Conditions.
Mario Sixa5f35c42018-01-15 11:08:07 +0100274 */
Heiko Schocherc5ca01f2009-07-09 12:04:26 +0200275 i2c_init_board();
276#endif
mario.six@gdsys.cc7d43b4e2016-04-25 08:31:01 +0200277 writeb(0, &base->cr); /* stop I2C controller */
Heiko Schocherf2850742012-10-24 13:48:22 +0200278 udelay(5); /* let it shutdown in peace */
mario.six@gdsys.cc416b2dc2016-04-25 08:31:08 +0200279 set_i2c_bus_speed(base, i2c_clk, speed);
mario.six@gdsys.cc7d43b4e2016-04-25 08:31:01 +0200280 writeb(slaveadd << 1, &base->adr);/* write slave address */
281 writeb(0x0, &base->sr); /* clear status register */
282 writeb(I2C_CR_MEN, &base->cr); /* start I2C controller */
Richard Retanubundf0149c2010-04-12 15:08:17 -0400283
Chunhe Lan2e13d572013-08-16 15:10:36 +0800284 timeval = get_ticks();
mario.six@gdsys.cc7d43b4e2016-04-25 08:31:01 +0200285 while (readb(&base->sr) & I2C_SR_MBB) {
Chunhe Lan2e13d572013-08-16 15:10:36 +0800286 if ((get_ticks() - timeval) < timeout)
287 continue;
288
mario.six@gdsys.cc7d43b4e2016-04-25 08:31:01 +0200289 if (fsl_i2c_fixup(base))
Chunhe Lan2e13d572013-08-16 15:10:36 +0800290 debug("i2c_init: BUS#%d failed to init\n",
mario.six@gdsys.cc416b2dc2016-04-25 08:31:08 +0200291 busnum);
Chunhe Lan2e13d572013-08-16 15:10:36 +0800292
293 break;
294 }
Jon Loeligere4773be2006-10-19 11:02:16 -0500295}
296
Mario Sixa5f35c42018-01-15 11:08:07 +0100297static int i2c_wait4bus(const struct fsl_i2c_base *base)
Jon Loeligere4773be2006-10-19 11:02:16 -0500298{
Stefan Roese37628252008-08-06 14:05:38 +0200299 unsigned long long timeval = get_ticks();
Timur Tabi2165c622009-09-04 16:28:35 -0500300 const unsigned long long timeout = usec2ticks(CONFIG_I2C_MBB_TIMEOUT);
Jon Loeligere4773be2006-10-19 11:02:16 -0500301
mario.six@gdsys.cc7d43b4e2016-04-25 08:31:01 +0200302 while (readb(&base->sr) & I2C_SR_MBB) {
Timur Tabi2165c622009-09-04 16:28:35 -0500303 if ((get_ticks() - timeval) > timeout)
Jon Loeligere4773be2006-10-19 11:02:16 -0500304 return -1;
Jon Loeligere4773be2006-10-19 11:02:16 -0500305 }
306
307 return 0;
308}
309
Mario Six484cdb82018-01-15 11:08:08 +0100310static int i2c_wait(const struct fsl_i2c_base *base, int write)
Jon Loeligere4773be2006-10-19 11:02:16 -0500311{
312 u32 csr;
Stefan Roese37628252008-08-06 14:05:38 +0200313 unsigned long long timeval = get_ticks();
Timur Tabi2165c622009-09-04 16:28:35 -0500314 const unsigned long long timeout = usec2ticks(CONFIG_I2C_TIMEOUT);
Jon Loeligere4773be2006-10-19 11:02:16 -0500315
316 do {
mario.six@gdsys.cc7d43b4e2016-04-25 08:31:01 +0200317 csr = readb(&base->sr);
Jon Loeligere4773be2006-10-19 11:02:16 -0500318 if (!(csr & I2C_SR_MIF))
319 continue;
Joakim Tjernlundc324b782009-09-17 11:07:15 +0200320 /* Read again to allow register to stabilise */
mario.six@gdsys.cc7d43b4e2016-04-25 08:31:01 +0200321 csr = readb(&base->sr);
Jon Loeligere4773be2006-10-19 11:02:16 -0500322
mario.six@gdsys.cc7d43b4e2016-04-25 08:31:01 +0200323 writeb(0x0, &base->sr);
Jon Loeligere4773be2006-10-19 11:02:16 -0500324
325 if (csr & I2C_SR_MAL) {
Mario Sixa5f35c42018-01-15 11:08:07 +0100326 debug("%s: MAL\n", __func__);
Jon Loeligere4773be2006-10-19 11:02:16 -0500327 return -1;
328 }
329
330 if (!(csr & I2C_SR_MCF)) {
Mario Sixa5f35c42018-01-15 11:08:07 +0100331 debug("%s: unfinished\n", __func__);
Jon Loeligere4773be2006-10-19 11:02:16 -0500332 return -1;
333 }
334
Joakim Tjernlundc32c5f72006-11-28 16:17:27 -0600335 if (write == I2C_WRITE_BIT && (csr & I2C_SR_RXAK)) {
Mario Sixa5f35c42018-01-15 11:08:07 +0100336 debug("%s: No RXACK\n", __func__);
Jon Loeligere4773be2006-10-19 11:02:16 -0500337 return -1;
338 }
339
340 return 0;
Timur Tabi2165c622009-09-04 16:28:35 -0500341 } while ((get_ticks() - timeval) < timeout);
Jon Loeligere4773be2006-10-19 11:02:16 -0500342
Mario Sixa5f35c42018-01-15 11:08:07 +0100343 debug("%s: timed out\n", __func__);
Jon Loeligere4773be2006-10-19 11:02:16 -0500344 return -1;
345}
346
Mario Six484cdb82018-01-15 11:08:08 +0100347static int i2c_write_addr(const struct fsl_i2c_base *base, u8 dev,
348 u8 dir, int rsta)
Jon Loeligere4773be2006-10-19 11:02:16 -0500349{
350 writeb(I2C_CR_MEN | I2C_CR_MSTA | I2C_CR_MTX
351 | (rsta ? I2C_CR_RSTA : 0),
mario.six@gdsys.cc7d43b4e2016-04-25 08:31:01 +0200352 &base->cr);
Jon Loeligere4773be2006-10-19 11:02:16 -0500353
mario.six@gdsys.cc7d43b4e2016-04-25 08:31:01 +0200354 writeb((dev << 1) | dir, &base->dr);
Jon Loeligere4773be2006-10-19 11:02:16 -0500355
mario.six@gdsys.cc416b2dc2016-04-25 08:31:08 +0200356 if (i2c_wait(base, I2C_WRITE_BIT) < 0)
Jon Loeligere4773be2006-10-19 11:02:16 -0500357 return 0;
358
359 return 1;
360}
361
Mario Six484cdb82018-01-15 11:08:08 +0100362static int __i2c_write_data(const struct fsl_i2c_base *base, u8 *data,
363 int length)
Jon Loeligere4773be2006-10-19 11:02:16 -0500364{
365 int i;
366
Jon Loeligere4773be2006-10-19 11:02:16 -0500367 for (i = 0; i < length; i++) {
mario.six@gdsys.cc7d43b4e2016-04-25 08:31:01 +0200368 writeb(data[i], &base->dr);
Jon Loeligere4773be2006-10-19 11:02:16 -0500369
mario.six@gdsys.cc416b2dc2016-04-25 08:31:08 +0200370 if (i2c_wait(base, I2C_WRITE_BIT) < 0)
Jon Loeligere4773be2006-10-19 11:02:16 -0500371 break;
372 }
373
374 return i;
375}
376
Mario Six484cdb82018-01-15 11:08:08 +0100377static int __i2c_read_data(const struct fsl_i2c_base *base, u8 *data,
378 int length)
Jon Loeligere4773be2006-10-19 11:02:16 -0500379{
380 int i;
381
382 writeb(I2C_CR_MEN | I2C_CR_MSTA | ((length == 1) ? I2C_CR_TXAK : 0),
mario.six@gdsys.cc7d43b4e2016-04-25 08:31:01 +0200383 &base->cr);
Jon Loeligere4773be2006-10-19 11:02:16 -0500384
385 /* dummy read */
mario.six@gdsys.cc7d43b4e2016-04-25 08:31:01 +0200386 readb(&base->dr);
Jon Loeligere4773be2006-10-19 11:02:16 -0500387
388 for (i = 0; i < length; i++) {
mario.six@gdsys.cc416b2dc2016-04-25 08:31:08 +0200389 if (i2c_wait(base, I2C_READ_BIT) < 0)
Jon Loeligere4773be2006-10-19 11:02:16 -0500390 break;
391
392 /* Generate ack on last next to last byte */
393 if (i == length - 2)
394 writeb(I2C_CR_MEN | I2C_CR_MSTA | I2C_CR_TXAK,
mario.six@gdsys.cc7d43b4e2016-04-25 08:31:01 +0200395 &base->cr);
Jon Loeligere4773be2006-10-19 11:02:16 -0500396
Joakim Tjernlund6384da22009-09-22 13:40:44 +0200397 /* Do not generate stop on last byte */
Jon Loeligere4773be2006-10-19 11:02:16 -0500398 if (i == length - 1)
Joakim Tjernlund6384da22009-09-22 13:40:44 +0200399 writeb(I2C_CR_MEN | I2C_CR_MSTA | I2C_CR_MTX,
mario.six@gdsys.cc7d43b4e2016-04-25 08:31:01 +0200400 &base->cr);
Jon Loeligere4773be2006-10-19 11:02:16 -0500401
mario.six@gdsys.cc7d43b4e2016-04-25 08:31:01 +0200402 data[i] = readb(&base->dr);
Jon Loeligere4773be2006-10-19 11:02:16 -0500403 }
404
405 return i;
406}
407
Mario Sixa5f35c42018-01-15 11:08:07 +0100408static int __i2c_read(const struct fsl_i2c_base *base, u8 chip_addr, u8 *offset,
409 int olen, u8 *data, int dlen)
Jon Loeligere4773be2006-10-19 11:02:16 -0500410{
mario.six@gdsys.cca4472652016-04-25 08:31:02 +0200411 int ret = -1; /* signal error */
Jon Loeligere4773be2006-10-19 11:02:16 -0500412
mario.six@gdsys.cc416b2dc2016-04-25 08:31:08 +0200413 if (i2c_wait4bus(base) < 0)
Reinhard Pfau2d878de2013-06-26 15:55:14 +0200414 return -1;
415
mario.six@gdsys.cc2eae9d02016-04-25 08:31:03 +0200416 /* Some drivers use offset lengths in excess of 4 bytes. These drivers
417 * adhere to the following convention:
418 * - the offset length is passed as negative (that is, the absolute
419 * value of olen is the actual offset length)
420 * - the offset itself is passed in data, which is overwritten by the
421 * subsequent read operation
Shaveta Leekhafdd7efe2014-04-24 14:51:23 +0530422 */
mario.six@gdsys.cca4472652016-04-25 08:31:02 +0200423 if (olen < 0) {
mario.six@gdsys.cc416b2dc2016-04-25 08:31:08 +0200424 if (i2c_write_addr(base, chip_addr, I2C_WRITE_BIT, 0) != 0)
425 ret = __i2c_write_data(base, data, -olen);
Shaveta Leekhafdd7efe2014-04-24 14:51:23 +0530426
mario.six@gdsys.cc8230fc42016-04-25 08:31:04 +0200427 if (ret != -olen)
Shaveta Leekhafdd7efe2014-04-24 14:51:23 +0530428 return -1;
429
mario.six@gdsys.cc416b2dc2016-04-25 08:31:08 +0200430 if (dlen && i2c_write_addr(base, chip_addr,
mario.six@gdsys.cca4472652016-04-25 08:31:02 +0200431 I2C_READ_BIT, 1) != 0)
mario.six@gdsys.cc416b2dc2016-04-25 08:31:08 +0200432 ret = __i2c_read_data(base, data, dlen);
Shaveta Leekhafdd7efe2014-04-24 14:51:23 +0530433 } else {
mario.six@gdsys.cca4472652016-04-25 08:31:02 +0200434 if ((!dlen || olen > 0) &&
mario.six@gdsys.cc416b2dc2016-04-25 08:31:08 +0200435 i2c_write_addr(base, chip_addr, I2C_WRITE_BIT, 0) != 0 &&
436 __i2c_write_data(base, offset, olen) == olen)
mario.six@gdsys.cca4472652016-04-25 08:31:02 +0200437 ret = 0; /* No error so far */
Joakim Tjernlundb648fe72007-01-31 11:04:19 +0100438
mario.six@gdsys.cc416b2dc2016-04-25 08:31:08 +0200439 if (dlen && i2c_write_addr(base, chip_addr, I2C_READ_BIT,
mario.six@gdsys.cca4472652016-04-25 08:31:02 +0200440 olen ? 1 : 0) != 0)
mario.six@gdsys.cc416b2dc2016-04-25 08:31:08 +0200441 ret = __i2c_read_data(base, data, dlen);
Shaveta Leekhafdd7efe2014-04-24 14:51:23 +0530442 }
Jon Loeligere4773be2006-10-19 11:02:16 -0500443
mario.six@gdsys.cc7d43b4e2016-04-25 08:31:01 +0200444 writeb(I2C_CR_MEN, &base->cr);
Jon Loeligere4773be2006-10-19 11:02:16 -0500445
mario.six@gdsys.cc416b2dc2016-04-25 08:31:08 +0200446 if (i2c_wait4bus(base)) /* Wait until STOP */
Joakim Tjernlund6384da22009-09-22 13:40:44 +0200447 debug("i2c_read: wait4bus timed out\n");
448
mario.six@gdsys.cca4472652016-04-25 08:31:02 +0200449 if (ret == dlen)
450 return 0;
Jon Loeliger24df9772006-10-19 12:02:24 -0500451
452 return -1;
Jon Loeligere4773be2006-10-19 11:02:16 -0500453}
454
Mario Sixa5f35c42018-01-15 11:08:07 +0100455static int __i2c_write(const struct fsl_i2c_base *base, u8 chip_addr,
456 u8 *offset, int olen, u8 *data, int dlen)
Jon Loeligere4773be2006-10-19 11:02:16 -0500457{
mario.six@gdsys.cca4472652016-04-25 08:31:02 +0200458 int ret = -1; /* signal error */
Jon Loeligere4773be2006-10-19 11:02:16 -0500459
mario.six@gdsys.cc416b2dc2016-04-25 08:31:08 +0200460 if (i2c_wait4bus(base) < 0)
Chunhe Lan2e13d572013-08-16 15:10:36 +0800461 return -1;
462
mario.six@gdsys.cc416b2dc2016-04-25 08:31:08 +0200463 if (i2c_write_addr(base, chip_addr, I2C_WRITE_BIT, 0) != 0 &&
464 __i2c_write_data(base, offset, olen) == olen) {
465 ret = __i2c_write_data(base, data, dlen);
Jon Loeliger24df9772006-10-19 12:02:24 -0500466 }
Jon Loeligere4773be2006-10-19 11:02:16 -0500467
mario.six@gdsys.cc7d43b4e2016-04-25 08:31:01 +0200468 writeb(I2C_CR_MEN, &base->cr);
mario.six@gdsys.cc416b2dc2016-04-25 08:31:08 +0200469 if (i2c_wait4bus(base)) /* Wait until STOP */
Joakim Tjernlundc324b782009-09-17 11:07:15 +0200470 debug("i2c_write: wait4bus timed out\n");
Jon Loeligere4773be2006-10-19 11:02:16 -0500471
mario.six@gdsys.cca4472652016-04-25 08:31:02 +0200472 if (ret == dlen)
473 return 0;
Jon Loeliger24df9772006-10-19 12:02:24 -0500474
475 return -1;
Jon Loeligere4773be2006-10-19 11:02:16 -0500476}
477
Mario Sixa5f35c42018-01-15 11:08:07 +0100478static int __i2c_probe_chip(const struct fsl_i2c_base *base, uchar chip)
Jon Loeligere4773be2006-10-19 11:02:16 -0500479{
Mario Sixa5f35c42018-01-15 11:08:07 +0100480 /* For unknown reason the controller will ACK when
Joakim Tjernlundb648fe72007-01-31 11:04:19 +0100481 * probing for a slave with the same address, so skip
482 * it.
Jon Loeligere4773be2006-10-19 11:02:16 -0500483 */
mario.six@gdsys.cc7d43b4e2016-04-25 08:31:01 +0200484 if (chip == (readb(&base->adr) >> 1))
Timur Tabiab347542006-11-03 19:15:00 -0600485 return -1;
Timur Tabiab347542006-11-03 19:15:00 -0600486
mario.six@gdsys.cc416b2dc2016-04-25 08:31:08 +0200487 return __i2c_read(base, chip, 0, 0, NULL, 0);
Timur Tabiab347542006-11-03 19:15:00 -0600488}
489
Mario Sixa5f35c42018-01-15 11:08:07 +0100490static uint __i2c_set_bus_speed(const struct fsl_i2c_base *base,
491 uint speed, int i2c_clk)
Timur Tabiab347542006-11-03 19:15:00 -0600492{
mario.six@gdsys.cc7d43b4e2016-04-25 08:31:01 +0200493 writeb(0, &base->cr); /* stop controller */
mario.six@gdsys.cc416b2dc2016-04-25 08:31:08 +0200494 set_i2c_bus_speed(base, i2c_clk, speed);
mario.six@gdsys.cc7d43b4e2016-04-25 08:31:01 +0200495 writeb(I2C_CR_MEN, &base->cr); /* start controller */
Timur Tabib301fda2008-03-14 17:45:29 -0500496
497 return 0;
Timur Tabiab347542006-11-03 19:15:00 -0600498}
499
Igor Opaniukf7c91762021-02-09 13:52:45 +0200500#if !CONFIG_IS_ENABLED(DM_I2C)
mario.six@gdsys.cc2d96aa72016-04-25 08:31:07 +0200501static void fsl_i2c_init(struct i2c_adapter *adap, int speed, int slaveadd)
502{
mario.six@gdsys.cc416b2dc2016-04-25 08:31:08 +0200503 __i2c_init(i2c_base[adap->hwadapnr], speed, slaveadd,
504 get_i2c_clock(adap->hwadapnr), adap->hwadapnr);
mario.six@gdsys.cc2d96aa72016-04-25 08:31:07 +0200505}
506
Mario Sixa5f35c42018-01-15 11:08:07 +0100507static int fsl_i2c_probe_chip(struct i2c_adapter *adap, uchar chip)
mario.six@gdsys.cc2d96aa72016-04-25 08:31:07 +0200508{
mario.six@gdsys.cc416b2dc2016-04-25 08:31:08 +0200509 return __i2c_probe_chip(i2c_base[adap->hwadapnr], chip);
mario.six@gdsys.cc2d96aa72016-04-25 08:31:07 +0200510}
511
Mario Sixa5f35c42018-01-15 11:08:07 +0100512static int fsl_i2c_read(struct i2c_adapter *adap, u8 chip_addr, uint offset,
513 int olen, u8 *data, int dlen)
mario.six@gdsys.cc2d96aa72016-04-25 08:31:07 +0200514{
mario.six@gdsys.cc416b2dc2016-04-25 08:31:08 +0200515 u8 *o = (u8 *)&offset;
Mario Sixa5f35c42018-01-15 11:08:07 +0100516
mario.six@gdsys.cc416b2dc2016-04-25 08:31:08 +0200517 return __i2c_read(i2c_base[adap->hwadapnr], chip_addr, &o[4 - olen],
518 olen, data, dlen);
mario.six@gdsys.cc2d96aa72016-04-25 08:31:07 +0200519}
520
Mario Sixa5f35c42018-01-15 11:08:07 +0100521static int fsl_i2c_write(struct i2c_adapter *adap, u8 chip_addr, uint offset,
522 int olen, u8 *data, int dlen)
mario.six@gdsys.cc2d96aa72016-04-25 08:31:07 +0200523{
mario.six@gdsys.cc416b2dc2016-04-25 08:31:08 +0200524 u8 *o = (u8 *)&offset;
Mario Sixa5f35c42018-01-15 11:08:07 +0100525
mario.six@gdsys.cc416b2dc2016-04-25 08:31:08 +0200526 return __i2c_write(i2c_base[adap->hwadapnr], chip_addr, &o[4 - olen],
527 olen, data, dlen);
mario.six@gdsys.cc2d96aa72016-04-25 08:31:07 +0200528}
529
Mario Sixa5f35c42018-01-15 11:08:07 +0100530static uint fsl_i2c_set_bus_speed(struct i2c_adapter *adap, uint speed)
mario.six@gdsys.cc2d96aa72016-04-25 08:31:07 +0200531{
mario.six@gdsys.cc416b2dc2016-04-25 08:31:08 +0200532 return __i2c_set_bus_speed(i2c_base[adap->hwadapnr], speed,
533 get_i2c_clock(adap->hwadapnr));
mario.six@gdsys.cc2d96aa72016-04-25 08:31:07 +0200534}
535
Heiko Schocherf2850742012-10-24 13:48:22 +0200536/*
537 * Register fsl i2c adapters
538 */
mario.six@gdsys.cc4a790692016-04-25 08:31:05 +0200539U_BOOT_I2C_ADAP_COMPLETE(fsl_0, fsl_i2c_init, fsl_i2c_probe_chip, fsl_i2c_read,
Heiko Schocherf2850742012-10-24 13:48:22 +0200540 fsl_i2c_write, fsl_i2c_set_bus_speed,
Tom Rinibe94c762021-08-18 23:12:35 -0400541 CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE,
Heiko Schocherf2850742012-10-24 13:48:22 +0200542 0)
543#ifdef CONFIG_SYS_FSL_I2C2_OFFSET
mario.six@gdsys.cc4a790692016-04-25 08:31:05 +0200544U_BOOT_I2C_ADAP_COMPLETE(fsl_1, fsl_i2c_init, fsl_i2c_probe_chip, fsl_i2c_read,
Heiko Schocherf2850742012-10-24 13:48:22 +0200545 fsl_i2c_write, fsl_i2c_set_bus_speed,
Tom Rinibe94c762021-08-18 23:12:35 -0400546 CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE,
Heiko Schocherf2850742012-10-24 13:48:22 +0200547 1)
Heiko Schocher2c9f3a42009-02-24 11:30:37 +0100548#endif
Shengzhou Liu37787f62014-07-07 12:17:48 +0800549#ifdef CONFIG_SYS_FSL_I2C3_OFFSET
mario.six@gdsys.cc4a790692016-04-25 08:31:05 +0200550U_BOOT_I2C_ADAP_COMPLETE(fsl_2, fsl_i2c_init, fsl_i2c_probe_chip, fsl_i2c_read,
Shengzhou Liu37787f62014-07-07 12:17:48 +0800551 fsl_i2c_write, fsl_i2c_set_bus_speed,
Tom Rinibe94c762021-08-18 23:12:35 -0400552 CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE,
Shengzhou Liu37787f62014-07-07 12:17:48 +0800553 2)
554#endif
555#ifdef CONFIG_SYS_FSL_I2C4_OFFSET
mario.six@gdsys.cc4a790692016-04-25 08:31:05 +0200556U_BOOT_I2C_ADAP_COMPLETE(fsl_3, fsl_i2c_init, fsl_i2c_probe_chip, fsl_i2c_read,
Shengzhou Liu37787f62014-07-07 12:17:48 +0800557 fsl_i2c_write, fsl_i2c_set_bus_speed,
Tom Rinibe94c762021-08-18 23:12:35 -0400558 CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE,
Shengzhou Liu37787f62014-07-07 12:17:48 +0800559 3)
560#endif
mario.six@gdsys.cc349686c2016-04-25 08:31:09 +0200561#else /* CONFIG_DM_I2C */
562static int fsl_i2c_probe_chip(struct udevice *bus, u32 chip_addr,
563 u32 chip_flags)
564{
565 struct fsl_i2c_dev *dev = dev_get_priv(bus);
Mario Sixa5f35c42018-01-15 11:08:07 +0100566
mario.six@gdsys.cc349686c2016-04-25 08:31:09 +0200567 return __i2c_probe_chip(dev->base, chip_addr);
568}
569
Mario Sixa5f35c42018-01-15 11:08:07 +0100570static int fsl_i2c_set_bus_speed(struct udevice *bus, uint speed)
mario.six@gdsys.cc349686c2016-04-25 08:31:09 +0200571{
572 struct fsl_i2c_dev *dev = dev_get_priv(bus);
Mario Sixa5f35c42018-01-15 11:08:07 +0100573
mario.six@gdsys.cc349686c2016-04-25 08:31:09 +0200574 return __i2c_set_bus_speed(dev->base, speed, dev->i2c_clk);
575}
576
Simon Glassaad29ae2020-12-03 16:55:21 -0700577static int fsl_i2c_of_to_plat(struct udevice *bus)
mario.six@gdsys.cc349686c2016-04-25 08:31:09 +0200578{
579 struct fsl_i2c_dev *dev = dev_get_priv(bus);
Mario Six2fe2ed62018-03-28 14:37:44 +0200580 struct clk clock;
mario.six@gdsys.cc349686c2016-04-25 08:31:09 +0200581
Mario Six486b2d52018-03-28 14:37:43 +0200582 dev->base = map_sysmem(dev_read_addr(bus), sizeof(struct fsl_i2c_base));
mario.six@gdsys.cc349686c2016-04-25 08:31:09 +0200583
584 if (!dev->base)
585 return -ENOMEM;
586
Mario Six84b68b82018-01-15 11:08:09 +0100587 dev->index = dev_read_u32_default(bus, "cell-index", -1);
588 dev->slaveadd = dev_read_u32_default(bus, "u-boot,i2c-slave-addr",
589 0x7f);
Simon Glassf0c99c52020-01-23 11:48:22 -0700590 dev->speed = dev_read_u32_default(bus, "clock-frequency",
591 I2C_SPEED_FAST_RATE);
mario.six@gdsys.cc349686c2016-04-25 08:31:09 +0200592
Mario Six2fe2ed62018-03-28 14:37:44 +0200593 if (!clk_get_by_index(bus, 0, &clock))
594 dev->i2c_clk = clk_get_rate(&clock);
595 else
596 dev->i2c_clk = dev->index ? gd->arch.i2c2_clk :
597 gd->arch.i2c1_clk;
mario.six@gdsys.cc349686c2016-04-25 08:31:09 +0200598
599 return 0;
600}
601
602static int fsl_i2c_probe(struct udevice *bus)
603{
604 struct fsl_i2c_dev *dev = dev_get_priv(bus);
Mario Sixa5f35c42018-01-15 11:08:07 +0100605
mario.six@gdsys.cc349686c2016-04-25 08:31:09 +0200606 __i2c_init(dev->base, dev->speed, dev->slaveadd, dev->i2c_clk,
607 dev->index);
608 return 0;
609}
610
611static int fsl_i2c_xfer(struct udevice *bus, struct i2c_msg *msg, int nmsgs)
612{
613 struct fsl_i2c_dev *dev = dev_get_priv(bus);
614 struct i2c_msg *dmsg, *omsg, dummy;
615
616 memset(&dummy, 0, sizeof(struct i2c_msg));
617
618 /* We expect either two messages (one with an offset and one with the
Mario Sixa5f35c42018-01-15 11:08:07 +0100619 * actual data) or one message (just data)
620 */
mario.six@gdsys.cc349686c2016-04-25 08:31:09 +0200621 if (nmsgs > 2 || nmsgs == 0) {
622 debug("%s: Only one or two messages are supported.", __func__);
623 return -1;
624 }
625
626 omsg = nmsgs == 1 ? &dummy : msg;
627 dmsg = nmsgs == 1 ? msg : msg + 1;
628
629 if (dmsg->flags & I2C_M_RD)
630 return __i2c_read(dev->base, dmsg->addr, omsg->buf, omsg->len,
631 dmsg->buf, dmsg->len);
632 else
633 return __i2c_write(dev->base, dmsg->addr, omsg->buf, omsg->len,
634 dmsg->buf, dmsg->len);
635}
636
637static const struct dm_i2c_ops fsl_i2c_ops = {
638 .xfer = fsl_i2c_xfer,
639 .probe_chip = fsl_i2c_probe_chip,
640 .set_bus_speed = fsl_i2c_set_bus_speed,
641};
642
643static const struct udevice_id fsl_i2c_ids[] = {
644 { .compatible = "fsl-i2c", },
645 { /* sentinel */ }
646};
647
648U_BOOT_DRIVER(i2c_fsl) = {
649 .name = "i2c_fsl",
650 .id = UCLASS_I2C,
651 .of_match = fsl_i2c_ids,
652 .probe = fsl_i2c_probe,
Simon Glassaad29ae2020-12-03 16:55:21 -0700653 .of_to_plat = fsl_i2c_of_to_plat,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700654 .priv_auto = sizeof(struct fsl_i2c_dev),
mario.six@gdsys.cc349686c2016-04-25 08:31:09 +0200655 .ops = &fsl_i2c_ops,
656};
657
658#endif /* CONFIG_DM_I2C */