blob: 3f51b1dd1db42b2700acc731cd8425b312969371 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Patrice Chotardebf442d2017-08-09 14:45:27 +02002/*
3 * (C) Copyright 2017 STMicroelectronics
Patrice Chotardebf442d2017-08-09 14:45:27 +02004 */
5
Patrick Delaunay6f600e32020-11-06 19:01:50 +01006#define LOG_CATEGORY UCLASS_I2C
7
Patrice Chotardebf442d2017-08-09 14:45:27 +02008#include <clk.h>
9#include <dm.h>
10#include <i2c.h>
Simon Glass0f2af882020-05-10 11:40:05 -060011#include <log.h>
Patrick Delaunay4d15c182020-07-06 13:31:35 +020012#include <regmap.h>
Patrice Chotardebf442d2017-08-09 14:45:27 +020013#include <reset.h>
Patrick Delaunay4d15c182020-07-06 13:31:35 +020014#include <syscon.h>
Patrick Delaunay6f600e32020-11-06 19:01:50 +010015#include <dm/device.h>
16#include <dm/device_compat.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060017#include <linux/bitops.h>
Simon Glassdbd79542020-05-10 11:40:11 -060018#include <linux/delay.h>
Alain Volmate04600e2020-03-06 11:09:14 +010019#include <linux/err.h>
Patrice Chotardebf442d2017-08-09 14:45:27 +020020#include <linux/io.h>
Simon Glassbdd5f812023-09-14 18:21:46 -060021#include <linux/printk.h>
Igor Prusovc3421ea2023-11-09 20:10:04 +030022#include <linux/time.h>
Patrice Chotardebf442d2017-08-09 14:45:27 +020023
24/* STM32 I2C registers */
25struct stm32_i2c_regs {
26 u32 cr1; /* I2C control register 1 */
27 u32 cr2; /* I2C control register 2 */
28 u32 oar1; /* I2C own address 1 register */
29 u32 oar2; /* I2C own address 2 register */
30 u32 timingr; /* I2C timing register */
31 u32 timeoutr; /* I2C timeout register */
32 u32 isr; /* I2C interrupt and status register */
33 u32 icr; /* I2C interrupt clear register */
34 u32 pecr; /* I2C packet error checking register */
35 u32 rxdr; /* I2C receive data register */
36 u32 txdr; /* I2C transmit data register */
37};
38
39#define STM32_I2C_CR1 0x00
40#define STM32_I2C_CR2 0x04
41#define STM32_I2C_TIMINGR 0x10
42#define STM32_I2C_ISR 0x18
43#define STM32_I2C_ICR 0x1C
44#define STM32_I2C_RXDR 0x24
45#define STM32_I2C_TXDR 0x28
46
47/* STM32 I2C control 1 */
48#define STM32_I2C_CR1_ANFOFF BIT(12)
Patrick Delaunay05b8d602021-08-03 12:05:13 +020049#define STM32_I2C_CR1_DNF_MASK GENMASK(11, 8)
50#define STM32_I2C_CR1_DNF(n) (((n) & 0xf) << 8)
Patrice Chotardebf442d2017-08-09 14:45:27 +020051#define STM32_I2C_CR1_ERRIE BIT(7)
52#define STM32_I2C_CR1_TCIE BIT(6)
53#define STM32_I2C_CR1_STOPIE BIT(5)
54#define STM32_I2C_CR1_NACKIE BIT(4)
55#define STM32_I2C_CR1_ADDRIE BIT(3)
56#define STM32_I2C_CR1_RXIE BIT(2)
57#define STM32_I2C_CR1_TXIE BIT(1)
58#define STM32_I2C_CR1_PE BIT(0)
59
60/* STM32 I2C control 2 */
Patrice Chotardebf442d2017-08-09 14:45:27 +020061#define STM32_I2C_CR2_RELOAD BIT(24)
62#define STM32_I2C_CR2_NBYTES_MASK GENMASK(23, 16)
63#define STM32_I2C_CR2_NBYTES(n) ((n & 0xff) << 16)
64#define STM32_I2C_CR2_NACK BIT(15)
65#define STM32_I2C_CR2_STOP BIT(14)
66#define STM32_I2C_CR2_START BIT(13)
67#define STM32_I2C_CR2_HEAD10R BIT(12)
68#define STM32_I2C_CR2_ADD10 BIT(11)
69#define STM32_I2C_CR2_RD_WRN BIT(10)
70#define STM32_I2C_CR2_SADD10_MASK GENMASK(9, 0)
Patrick Delaunayb13ab6f2018-10-29 15:31:55 +010071#define STM32_I2C_CR2_SADD10(n) (n & STM32_I2C_CR2_SADD10_MASK)
Patrice Chotardebf442d2017-08-09 14:45:27 +020072#define STM32_I2C_CR2_SADD7_MASK GENMASK(7, 1)
73#define STM32_I2C_CR2_SADD7(n) ((n & 0x7f) << 1)
74#define STM32_I2C_CR2_RESET_MASK (STM32_I2C_CR2_HEAD10R \
75 | STM32_I2C_CR2_NBYTES_MASK \
76 | STM32_I2C_CR2_SADD7_MASK \
77 | STM32_I2C_CR2_RELOAD \
78 | STM32_I2C_CR2_RD_WRN)
79
80/* STM32 I2C Interrupt Status */
81#define STM32_I2C_ISR_BUSY BIT(15)
82#define STM32_I2C_ISR_ARLO BIT(9)
83#define STM32_I2C_ISR_BERR BIT(8)
84#define STM32_I2C_ISR_TCR BIT(7)
85#define STM32_I2C_ISR_TC BIT(6)
86#define STM32_I2C_ISR_STOPF BIT(5)
87#define STM32_I2C_ISR_NACKF BIT(4)
88#define STM32_I2C_ISR_ADDR BIT(3)
89#define STM32_I2C_ISR_RXNE BIT(2)
90#define STM32_I2C_ISR_TXIS BIT(1)
91#define STM32_I2C_ISR_TXE BIT(0)
92#define STM32_I2C_ISR_ERRORS (STM32_I2C_ISR_BERR \
93 | STM32_I2C_ISR_ARLO)
94
95/* STM32 I2C Interrupt Clear */
96#define STM32_I2C_ICR_ARLOCF BIT(9)
97#define STM32_I2C_ICR_BERRCF BIT(8)
98#define STM32_I2C_ICR_STOPCF BIT(5)
99#define STM32_I2C_ICR_NACKCF BIT(4)
100
101/* STM32 I2C Timing */
102#define STM32_I2C_TIMINGR_PRESC(n) ((n & 0xf) << 28)
103#define STM32_I2C_TIMINGR_SCLDEL(n) ((n & 0xf) << 20)
104#define STM32_I2C_TIMINGR_SDADEL(n) ((n & 0xf) << 16)
105#define STM32_I2C_TIMINGR_SCLH(n) ((n & 0xff) << 8)
106#define STM32_I2C_TIMINGR_SCLL(n) (n & 0xff)
107
108#define STM32_I2C_MAX_LEN 0xff
109
Patrick Delaunay05b8d602021-08-03 12:05:13 +0200110#define STM32_I2C_DNF_MAX 15
Patrice Chotardebf442d2017-08-09 14:45:27 +0200111
Patrice Chotardebf442d2017-08-09 14:45:27 +0200112#define STM32_I2C_ANALOG_FILTER_DELAY_MIN 50 /* ns */
113#define STM32_I2C_ANALOG_FILTER_DELAY_MAX 260 /* ns */
114
115#define STM32_I2C_RISE_TIME_DEFAULT 25 /* ns */
116#define STM32_I2C_FALL_TIME_DEFAULT 10 /* ns */
117
118#define STM32_PRESC_MAX BIT(4)
119#define STM32_SCLDEL_MAX BIT(4)
120#define STM32_SDADEL_MAX BIT(4)
121#define STM32_SCLH_MAX BIT(8)
122#define STM32_SCLL_MAX BIT(8)
123
Patrice Chotardebf442d2017-08-09 14:45:27 +0200124/**
125 * struct stm32_i2c_spec - private i2c specification timing
126 * @rate: I2C bus speed (Hz)
127 * @rate_min: 80% of I2C bus speed (Hz)
128 * @rate_max: 120% of I2C bus speed (Hz)
129 * @fall_max: Max fall time of both SDA and SCL signals (ns)
130 * @rise_max: Max rise time of both SDA and SCL signals (ns)
131 * @hddat_min: Min data hold time (ns)
132 * @vddat_max: Max data valid time (ns)
133 * @sudat_min: Min data setup time (ns)
134 * @l_min: Min low period of the SCL clock (ns)
135 * @h_min: Min high period of the SCL clock (ns)
136 */
137
138struct stm32_i2c_spec {
139 u32 rate;
140 u32 rate_min;
141 u32 rate_max;
142 u32 fall_max;
143 u32 rise_max;
144 u32 hddat_min;
145 u32 vddat_max;
146 u32 sudat_min;
147 u32 l_min;
148 u32 h_min;
149};
150
151/**
152 * struct stm32_i2c_setup - private I2C timing setup parameters
Patrice Chotardebf442d2017-08-09 14:45:27 +0200153 * @speed_freq: I2C speed frequency (Hz)
154 * @clock_src: I2C clock source frequency (Hz)
155 * @rise_time: Rise time (ns)
156 * @fall_time: Fall time (ns)
Patrick Delaunay05b8d602021-08-03 12:05:13 +0200157 * @dnf: value of digital filter to apply
Patrice Chotardebf442d2017-08-09 14:45:27 +0200158 * @analog_filter: Analog filter delay (On/Off)
159 */
160struct stm32_i2c_setup {
Patrice Chotardebf442d2017-08-09 14:45:27 +0200161 u32 speed_freq;
162 u32 clock_src;
163 u32 rise_time;
164 u32 fall_time;
165 u8 dnf;
166 bool analog_filter;
Patrick Delaunay58862782021-08-03 12:05:09 +0200167};
168
169/**
170 * struct stm32_i2c_data - driver data for I2C configuration by compatible
171 * @fmp_clr_offset: Fast Mode Plus clear register offset from set register
172 */
173struct stm32_i2c_data {
Patrick Delaunay4d15c182020-07-06 13:31:35 +0200174 u32 fmp_clr_offset;
Patrice Chotardebf442d2017-08-09 14:45:27 +0200175};
176
177/**
178 * struct stm32_i2c_timings - private I2C output parameters
179 * @prec: Prescaler value
180 * @scldel: Data setup time
181 * @sdadel: Data hold time
182 * @sclh: SCL high period (master mode)
183 * @sclh: SCL low period (master mode)
184 */
185struct stm32_i2c_timings {
186 struct list_head node;
187 u8 presc;
188 u8 scldel;
189 u8 sdadel;
190 u8 sclh;
191 u8 scll;
192};
193
Patrick Delaunay4d15c182020-07-06 13:31:35 +0200194/**
195 * struct stm32_i2c_priv - private data of the controller
196 * @regs: I2C registers address
197 * @clk: hw i2c clock
198 * @setup: I2C timing setup parameters
199 * @speed: I2C clock frequency of the controller. Standard, Fast or Fast+
200 * @regmap: holds SYSCFG phandle for Fast Mode Plus bit
201 * @regmap_sreg: register address for setting Fast Mode Plus bits
202 * @regmap_creg: register address for clearing Fast Mode Plus bits
203 * @regmap_mask: mask for Fast Mode Plus bits
Patrick Delaunaya14855d2021-08-03 12:05:14 +0200204 * @dnf_dt: value of digital filter requested via dt
Patrick Delaunay4d15c182020-07-06 13:31:35 +0200205 */
Patrice Chotardebf442d2017-08-09 14:45:27 +0200206struct stm32_i2c_priv {
207 struct stm32_i2c_regs *regs;
208 struct clk clk;
Patrick Delaunay58862782021-08-03 12:05:09 +0200209 struct stm32_i2c_setup setup;
Alain Volmate04600e2020-03-06 11:09:14 +0100210 u32 speed;
Patrick Delaunay4d15c182020-07-06 13:31:35 +0200211 struct regmap *regmap;
212 u32 regmap_sreg;
213 u32 regmap_creg;
214 u32 regmap_mask;
Patrick Delaunaya14855d2021-08-03 12:05:14 +0200215 u32 dnf_dt;
Patrice Chotardebf442d2017-08-09 14:45:27 +0200216};
217
Patrick Delaunaybf832e52018-10-29 15:31:56 +0100218static const struct stm32_i2c_spec i2c_specs[] = {
Alain Volmate04600e2020-03-06 11:09:14 +0100219 /* Standard speed - 100 KHz */
Simon Glasse8e01cc2020-01-23 11:48:21 -0700220 [IC_SPEED_MODE_STANDARD] = {
221 .rate = I2C_SPEED_STANDARD_RATE,
Patrice Chotardebf442d2017-08-09 14:45:27 +0200222 .rate_min = 8000,
223 .rate_max = 120000,
224 .fall_max = 300,
225 .rise_max = 1000,
226 .hddat_min = 0,
227 .vddat_max = 3450,
228 .sudat_min = 250,
229 .l_min = 4700,
230 .h_min = 4000,
231 },
Alain Volmate04600e2020-03-06 11:09:14 +0100232 /* Fast speed - 400 KHz */
Simon Glasse8e01cc2020-01-23 11:48:21 -0700233 [IC_SPEED_MODE_FAST] = {
234 .rate = I2C_SPEED_FAST_RATE,
Patrice Chotardebf442d2017-08-09 14:45:27 +0200235 .rate_min = 320000,
236 .rate_max = 480000,
237 .fall_max = 300,
238 .rise_max = 300,
239 .hddat_min = 0,
240 .vddat_max = 900,
241 .sudat_min = 100,
242 .l_min = 1300,
243 .h_min = 600,
244 },
Alain Volmate04600e2020-03-06 11:09:14 +0100245 /* Fast Plus Speed - 1 MHz */
Simon Glasse8e01cc2020-01-23 11:48:21 -0700246 [IC_SPEED_MODE_FAST_PLUS] = {
247 .rate = I2C_SPEED_FAST_PLUS_RATE,
Patrice Chotardebf442d2017-08-09 14:45:27 +0200248 .rate_min = 800000,
249 .rate_max = 1200000,
250 .fall_max = 100,
251 .rise_max = 120,
252 .hddat_min = 0,
253 .vddat_max = 450,
254 .sudat_min = 50,
255 .l_min = 500,
256 .h_min = 260,
257 },
258};
259
Patrick Delaunay58862782021-08-03 12:05:09 +0200260static const struct stm32_i2c_data stm32f7_data = {
261 .fmp_clr_offset = 0x00,
Patrick Delaunay4d15c182020-07-06 13:31:35 +0200262};
263
Patrick Delaunay58862782021-08-03 12:05:09 +0200264static const struct stm32_i2c_data stm32mp15_data = {
Patrick Delaunay4d15c182020-07-06 13:31:35 +0200265 .fmp_clr_offset = 0x40,
Patrice Chotardebf442d2017-08-09 14:45:27 +0200266};
267
Patrick Delaunay11c064e2022-06-30 10:20:14 +0200268static const struct stm32_i2c_data stm32mp13_data = {
269 .fmp_clr_offset = 0x4,
270};
271
Patrice Chotardebf442d2017-08-09 14:45:27 +0200272static int stm32_i2c_check_device_busy(struct stm32_i2c_priv *i2c_priv)
273{
274 struct stm32_i2c_regs *regs = i2c_priv->regs;
275 u32 status = readl(&regs->isr);
276
277 if (status & STM32_I2C_ISR_BUSY)
278 return -EBUSY;
279
280 return 0;
281}
282
283static void stm32_i2c_message_start(struct stm32_i2c_priv *i2c_priv,
Alain Volmat36fa3bb2022-09-12 10:41:59 +0200284 struct i2c_msg *msg)
Patrice Chotardebf442d2017-08-09 14:45:27 +0200285{
286 struct stm32_i2c_regs *regs = i2c_priv->regs;
287 u32 cr2 = readl(&regs->cr2);
288
289 /* Set transfer direction */
290 cr2 &= ~STM32_I2C_CR2_RD_WRN;
291 if (msg->flags & I2C_M_RD)
292 cr2 |= STM32_I2C_CR2_RD_WRN;
293
294 /* Set slave address */
295 cr2 &= ~(STM32_I2C_CR2_HEAD10R | STM32_I2C_CR2_ADD10);
296 if (msg->flags & I2C_M_TEN) {
297 cr2 &= ~STM32_I2C_CR2_SADD10_MASK;
298 cr2 |= STM32_I2C_CR2_SADD10(msg->addr);
299 cr2 |= STM32_I2C_CR2_ADD10;
300 } else {
301 cr2 &= ~STM32_I2C_CR2_SADD7_MASK;
302 cr2 |= STM32_I2C_CR2_SADD7(msg->addr);
303 }
304
Alain Volmat110efab2022-09-12 10:41:58 +0200305 /* Set nb bytes to transfer and reload (if needed) */
306 cr2 &= ~(STM32_I2C_CR2_NBYTES_MASK | STM32_I2C_CR2_RELOAD);
Patrice Chotardebf442d2017-08-09 14:45:27 +0200307 if (msg->len > STM32_I2C_MAX_LEN) {
308 cr2 |= STM32_I2C_CR2_NBYTES(STM32_I2C_MAX_LEN);
309 cr2 |= STM32_I2C_CR2_RELOAD;
310 } else {
311 cr2 |= STM32_I2C_CR2_NBYTES(msg->len);
312 }
313
314 /* Write configurations register */
315 writel(cr2, &regs->cr2);
316
317 /* START/ReSTART generation */
318 setbits_le32(&regs->cr2, STM32_I2C_CR2_START);
319}
320
321/*
322 * RELOAD mode must be selected if total number of data bytes to be
323 * sent is greater than MAX_LEN
324 */
325
326static void stm32_i2c_handle_reload(struct stm32_i2c_priv *i2c_priv,
Alain Volmat36fa3bb2022-09-12 10:41:59 +0200327 struct i2c_msg *msg)
Patrice Chotardebf442d2017-08-09 14:45:27 +0200328{
329 struct stm32_i2c_regs *regs = i2c_priv->regs;
330 u32 cr2 = readl(&regs->cr2);
331
332 cr2 &= ~STM32_I2C_CR2_NBYTES_MASK;
333
334 if (msg->len > STM32_I2C_MAX_LEN) {
335 cr2 |= STM32_I2C_CR2_NBYTES(STM32_I2C_MAX_LEN);
336 } else {
337 cr2 &= ~STM32_I2C_CR2_RELOAD;
338 cr2 |= STM32_I2C_CR2_NBYTES(msg->len);
339 }
340
341 writel(cr2, &regs->cr2);
342}
343
344static int stm32_i2c_wait_flags(struct stm32_i2c_priv *i2c_priv,
Patrick Delaunayb13ab6f2018-10-29 15:31:55 +0100345 u32 flags, u32 *status)
Patrice Chotardebf442d2017-08-09 14:45:27 +0200346{
347 struct stm32_i2c_regs *regs = i2c_priv->regs;
348 u32 time_start = get_timer(0);
349
350 *status = readl(&regs->isr);
351 while (!(*status & flags)) {
352 if (get_timer(time_start) > CONFIG_SYS_HZ) {
Patrick Delaunay6f600e32020-11-06 19:01:50 +0100353 log_debug("i2c timeout\n");
Patrice Chotardebf442d2017-08-09 14:45:27 +0200354 return -ETIMEDOUT;
355 }
356
357 *status = readl(&regs->isr);
358 }
359
360 return 0;
361}
362
363static int stm32_i2c_check_end_of_message(struct stm32_i2c_priv *i2c_priv)
364{
365 struct stm32_i2c_regs *regs = i2c_priv->regs;
366 u32 mask = STM32_I2C_ISR_ERRORS | STM32_I2C_ISR_NACKF |
367 STM32_I2C_ISR_STOPF;
368 u32 status;
369 int ret;
370
371 ret = stm32_i2c_wait_flags(i2c_priv, mask, &status);
372 if (ret)
373 return ret;
374
375 if (status & STM32_I2C_ISR_BERR) {
Patrick Delaunay6f600e32020-11-06 19:01:50 +0100376 log_debug("Bus error\n");
Patrice Chotardebf442d2017-08-09 14:45:27 +0200377
378 /* Clear BERR flag */
379 setbits_le32(&regs->icr, STM32_I2C_ICR_BERRCF);
380
381 return -EIO;
382 }
383
384 if (status & STM32_I2C_ISR_ARLO) {
Patrick Delaunay6f600e32020-11-06 19:01:50 +0100385 log_debug("Arbitration lost\n");
Patrice Chotardebf442d2017-08-09 14:45:27 +0200386
387 /* Clear ARLO flag */
388 setbits_le32(&regs->icr, STM32_I2C_ICR_ARLOCF);
389
390 return -EAGAIN;
391 }
392
393 if (status & STM32_I2C_ISR_NACKF) {
Patrick Delaunay6f600e32020-11-06 19:01:50 +0100394 log_debug("Receive NACK\n");
Patrice Chotardebf442d2017-08-09 14:45:27 +0200395
396 /* Clear NACK flag */
397 setbits_le32(&regs->icr, STM32_I2C_ICR_NACKCF);
398
399 /* Wait until STOPF flag is set */
400 mask = STM32_I2C_ISR_STOPF;
401 ret = stm32_i2c_wait_flags(i2c_priv, mask, &status);
402 if (ret)
403 return ret;
404
405 ret = -EIO;
406 }
407
408 if (status & STM32_I2C_ISR_STOPF) {
409 /* Clear STOP flag */
410 setbits_le32(&regs->icr, STM32_I2C_ICR_STOPCF);
411
412 /* Clear control register 2 */
Jorge Ramirez-Ortizddba62c2022-08-15 16:52:10 +0200413 clrbits_le32(&regs->cr2, STM32_I2C_CR2_RESET_MASK);
Patrice Chotardebf442d2017-08-09 14:45:27 +0200414 }
415
416 return ret;
417}
418
419static int stm32_i2c_message_xfer(struct stm32_i2c_priv *i2c_priv,
Patrick Delaunayb13ab6f2018-10-29 15:31:55 +0100420 struct i2c_msg *msg, bool stop)
Patrice Chotardebf442d2017-08-09 14:45:27 +0200421{
422 struct stm32_i2c_regs *regs = i2c_priv->regs;
423 u32 status;
424 u32 mask = msg->flags & I2C_M_RD ? STM32_I2C_ISR_RXNE :
425 STM32_I2C_ISR_TXIS | STM32_I2C_ISR_NACKF;
426 int bytes_to_rw = msg->len > STM32_I2C_MAX_LEN ?
427 STM32_I2C_MAX_LEN : msg->len;
428 int ret = 0;
429
430 /* Add errors */
431 mask |= STM32_I2C_ISR_ERRORS;
432
Alain Volmat36fa3bb2022-09-12 10:41:59 +0200433 stm32_i2c_message_start(i2c_priv, msg);
Patrice Chotardebf442d2017-08-09 14:45:27 +0200434
435 while (msg->len) {
436 /*
437 * Wait until TXIS/NACKF/BERR/ARLO flags or
438 * RXNE/BERR/ARLO flags are set
439 */
440 ret = stm32_i2c_wait_flags(i2c_priv, mask, &status);
441 if (ret)
442 break;
443
444 if (status & (STM32_I2C_ISR_NACKF | STM32_I2C_ISR_ERRORS))
445 break;
446
447 if (status & STM32_I2C_ISR_RXNE) {
448 *msg->buf++ = readb(&regs->rxdr);
449 msg->len--;
450 bytes_to_rw--;
451 }
452
453 if (status & STM32_I2C_ISR_TXIS) {
454 writeb(*msg->buf++, &regs->txdr);
455 msg->len--;
456 bytes_to_rw--;
457 }
458
459 if (!bytes_to_rw && msg->len) {
460 /* Wait until TCR flag is set */
461 mask = STM32_I2C_ISR_TCR;
462 ret = stm32_i2c_wait_flags(i2c_priv, mask, &status);
463 if (ret)
464 break;
465
466 bytes_to_rw = msg->len > STM32_I2C_MAX_LEN ?
467 STM32_I2C_MAX_LEN : msg->len;
468 mask = msg->flags & I2C_M_RD ? STM32_I2C_ISR_RXNE :
469 STM32_I2C_ISR_TXIS | STM32_I2C_ISR_NACKF;
470
Alain Volmat36fa3bb2022-09-12 10:41:59 +0200471 stm32_i2c_handle_reload(i2c_priv, msg);
Patrice Chotardebf442d2017-08-09 14:45:27 +0200472 } else if (!bytes_to_rw) {
473 /* Wait until TC flag is set */
474 mask = STM32_I2C_ISR_TC;
475 ret = stm32_i2c_wait_flags(i2c_priv, mask, &status);
476 if (ret)
477 break;
478
479 if (!stop)
480 /* Message sent, new message has to be sent */
481 return 0;
482 }
483 }
484
Alain Volmatef8d2a22022-09-12 10:42:00 +0200485 /* End of transfer, send stop condition if appropriate */
486 if (!ret && !(status & (STM32_I2C_ISR_NACKF | STM32_I2C_ISR_ERRORS)))
487 setbits_le32(&regs->cr2, STM32_I2C_CR2_STOP);
Patrice Chotardebf442d2017-08-09 14:45:27 +0200488
489 return stm32_i2c_check_end_of_message(i2c_priv);
490}
491
492static int stm32_i2c_xfer(struct udevice *bus, struct i2c_msg *msg,
Patrick Delaunayb13ab6f2018-10-29 15:31:55 +0100493 int nmsgs)
Patrice Chotardebf442d2017-08-09 14:45:27 +0200494{
495 struct stm32_i2c_priv *i2c_priv = dev_get_priv(bus);
496 int ret;
497
498 ret = stm32_i2c_check_device_busy(i2c_priv);
499 if (ret)
500 return ret;
501
502 for (; nmsgs > 0; nmsgs--, msg++) {
503 ret = stm32_i2c_message_xfer(i2c_priv, msg, nmsgs == 1);
504 if (ret)
505 return ret;
506 }
507
508 return 0;
509}
510
Patrick Delaunay10605f72021-08-03 12:05:15 +0200511static int stm32_i2c_compute_solutions(u32 i2cclk,
512 struct stm32_i2c_setup *setup,
Alain Volmate04600e2020-03-06 11:09:14 +0100513 const struct stm32_i2c_spec *specs,
Patrice Chotardebf442d2017-08-09 14:45:27 +0200514 struct list_head *solutions)
515{
516 struct stm32_i2c_timings *v;
517 u32 p_prev = STM32_PRESC_MAX;
Patrice Chotardebf442d2017-08-09 14:45:27 +0200518 u32 af_delay_min, af_delay_max;
519 u16 p, l, a;
520 int sdadel_min, sdadel_max, scldel_min;
521 int ret = 0;
522
523 af_delay_min = setup->analog_filter ?
524 STM32_I2C_ANALOG_FILTER_DELAY_MIN : 0;
525 af_delay_max = setup->analog_filter ?
526 STM32_I2C_ANALOG_FILTER_DELAY_MAX : 0;
527
Alain Volmate04600e2020-03-06 11:09:14 +0100528 sdadel_min = specs->hddat_min + setup->fall_time -
Patrice Chotardebf442d2017-08-09 14:45:27 +0200529 af_delay_min - (setup->dnf + 3) * i2cclk;
530
Alain Volmate04600e2020-03-06 11:09:14 +0100531 sdadel_max = specs->vddat_max - setup->rise_time -
Patrice Chotardebf442d2017-08-09 14:45:27 +0200532 af_delay_max - (setup->dnf + 4) * i2cclk;
533
Alain Volmate04600e2020-03-06 11:09:14 +0100534 scldel_min = setup->rise_time + specs->sudat_min;
Patrice Chotardebf442d2017-08-09 14:45:27 +0200535
536 if (sdadel_min < 0)
537 sdadel_min = 0;
538 if (sdadel_max < 0)
539 sdadel_max = 0;
540
Patrick Delaunay6f600e32020-11-06 19:01:50 +0100541 log_debug("SDADEL(min/max): %i/%i, SCLDEL(Min): %i\n",
542 sdadel_min, sdadel_max, scldel_min);
Patrice Chotardebf442d2017-08-09 14:45:27 +0200543
544 /* Compute possible values for PRESC, SCLDEL and SDADEL */
545 for (p = 0; p < STM32_PRESC_MAX; p++) {
546 for (l = 0; l < STM32_SCLDEL_MAX; l++) {
Patrick Delaunay6f4f9122019-06-21 15:26:47 +0200547 int scldel = (l + 1) * (p + 1) * i2cclk;
Patrice Chotardebf442d2017-08-09 14:45:27 +0200548
549 if (scldel < scldel_min)
550 continue;
551
552 for (a = 0; a < STM32_SDADEL_MAX; a++) {
Patrick Delaunay6f4f9122019-06-21 15:26:47 +0200553 int sdadel = (a * (p + 1) + 1) * i2cclk;
Patrice Chotardebf442d2017-08-09 14:45:27 +0200554
555 if (((sdadel >= sdadel_min) &&
556 (sdadel <= sdadel_max)) &&
557 (p != p_prev)) {
Patrick Delaunay28b10a72018-03-12 10:46:09 +0100558 v = calloc(1, sizeof(*v));
Patrice Chotardebf442d2017-08-09 14:45:27 +0200559 if (!v)
560 return -ENOMEM;
561
562 v->presc = p;
563 v->scldel = l;
564 v->sdadel = a;
565 p_prev = p;
566
567 list_add_tail(&v->node, solutions);
Nicolas Le Bayonc41d3072019-04-18 17:32:43 +0200568 break;
Patrice Chotardebf442d2017-08-09 14:45:27 +0200569 }
570 }
Nicolas Le Bayonc41d3072019-04-18 17:32:43 +0200571
572 if (p_prev == p)
573 break;
Patrice Chotardebf442d2017-08-09 14:45:27 +0200574 }
575 }
576
577 if (list_empty(solutions)) {
Patrick Delaunay6f600e32020-11-06 19:01:50 +0100578 log_err("no Prescaler solution\n");
Patrice Chotardebf442d2017-08-09 14:45:27 +0200579 ret = -EPERM;
580 }
581
582 return ret;
583}
584
Patrick Delaunay10605f72021-08-03 12:05:15 +0200585static int stm32_i2c_choose_solution(u32 i2cclk,
586 struct stm32_i2c_setup *setup,
Alain Volmate04600e2020-03-06 11:09:14 +0100587 const struct stm32_i2c_spec *specs,
Patrice Chotardebf442d2017-08-09 14:45:27 +0200588 struct list_head *solutions,
589 struct stm32_i2c_timings *s)
590{
591 struct stm32_i2c_timings *v;
Igor Prusovc3421ea2023-11-09 20:10:04 +0300592 u32 i2cbus = DIV_ROUND_CLOSEST(NSEC_PER_SEC,
Patrice Chotardebf442d2017-08-09 14:45:27 +0200593 setup->speed_freq);
594 u32 clk_error_prev = i2cbus;
Patrice Chotardebf442d2017-08-09 14:45:27 +0200595 u32 clk_min, clk_max;
596 u32 af_delay_min;
597 u32 dnf_delay;
598 u32 tsync;
599 u16 l, h;
Christophe Kerelloa71376a2017-10-17 11:21:32 +0200600 bool sol_found = false;
Patrice Chotardebf442d2017-08-09 14:45:27 +0200601 int ret = 0;
602
603 af_delay_min = setup->analog_filter ?
604 STM32_I2C_ANALOG_FILTER_DELAY_MIN : 0;
605 dnf_delay = setup->dnf * i2cclk;
606
607 tsync = af_delay_min + dnf_delay + (2 * i2cclk);
Igor Prusovc3421ea2023-11-09 20:10:04 +0300608 clk_max = NSEC_PER_SEC / specs->rate_min;
609 clk_min = NSEC_PER_SEC / specs->rate_max;
Patrice Chotardebf442d2017-08-09 14:45:27 +0200610
611 /*
612 * Among Prescaler possibilities discovered above figures out SCL Low
613 * and High Period. Provided:
614 * - SCL Low Period has to be higher than Low Period of the SCL Clock
615 * defined by I2C Specification. I2C Clock has to be lower than
616 * (SCL Low Period - Analog/Digital filters) / 4.
617 * - SCL High Period has to be lower than High Period of the SCL Clock
618 * defined by I2C Specification
619 * - I2C Clock has to be lower than SCL High Period
620 */
621 list_for_each_entry(v, solutions, node) {
622 u32 prescaler = (v->presc + 1) * i2cclk;
623
624 for (l = 0; l < STM32_SCLL_MAX; l++) {
625 u32 tscl_l = (l + 1) * prescaler + tsync;
Patrick Delaunayb13ab6f2018-10-29 15:31:55 +0100626
Alain Volmate04600e2020-03-06 11:09:14 +0100627 if (tscl_l < specs->l_min ||
Patrice Chotardebf442d2017-08-09 14:45:27 +0200628 (i2cclk >=
629 ((tscl_l - af_delay_min - dnf_delay) / 4))) {
630 continue;
631 }
632
633 for (h = 0; h < STM32_SCLH_MAX; h++) {
634 u32 tscl_h = (h + 1) * prescaler + tsync;
635 u32 tscl = tscl_l + tscl_h +
636 setup->rise_time + setup->fall_time;
637
638 if ((tscl >= clk_min) && (tscl <= clk_max) &&
Alain Volmate04600e2020-03-06 11:09:14 +0100639 (tscl_h >= specs->h_min) &&
Patrice Chotardebf442d2017-08-09 14:45:27 +0200640 (i2cclk < tscl_h)) {
Patrick Delaunay6f4f9122019-06-21 15:26:47 +0200641 u32 clk_error;
Patrice Chotardebf442d2017-08-09 14:45:27 +0200642
Patrick Delaunay6f4f9122019-06-21 15:26:47 +0200643 if (tscl > i2cbus)
644 clk_error = tscl - i2cbus;
645 else
646 clk_error = i2cbus - tscl;
Patrice Chotardebf442d2017-08-09 14:45:27 +0200647
648 if (clk_error < clk_error_prev) {
649 clk_error_prev = clk_error;
650 v->scll = l;
651 v->sclh = h;
Christophe Kerelloa71376a2017-10-17 11:21:32 +0200652 sol_found = true;
653 memcpy(s, v, sizeof(*s));
Patrice Chotardebf442d2017-08-09 14:45:27 +0200654 }
655 }
656 }
657 }
658 }
659
Christophe Kerelloa71376a2017-10-17 11:21:32 +0200660 if (!sol_found) {
Patrick Delaunay6f600e32020-11-06 19:01:50 +0100661 log_err("no solution at all\n");
Patrice Chotardebf442d2017-08-09 14:45:27 +0200662 ret = -EPERM;
663 }
664
665 return ret;
666}
667
Alain Volmate04600e2020-03-06 11:09:14 +0100668static const struct stm32_i2c_spec *get_specs(u32 rate)
669{
670 unsigned int i;
671
672 for (i = 0; i < ARRAY_SIZE(i2c_specs); i++)
673 if (rate <= i2c_specs[i].rate)
674 return &i2c_specs[i];
675
676 /* NOT REACHED */
677 return ERR_PTR(-EINVAL);
678}
679
Patrice Chotardebf442d2017-08-09 14:45:27 +0200680static int stm32_i2c_compute_timing(struct stm32_i2c_priv *i2c_priv,
Patrick Delaunayb13ab6f2018-10-29 15:31:55 +0100681 struct stm32_i2c_timings *output)
Patrice Chotardebf442d2017-08-09 14:45:27 +0200682{
Patrick Delaunay10605f72021-08-03 12:05:15 +0200683 struct stm32_i2c_setup *setup = &i2c_priv->setup;
Alain Volmate04600e2020-03-06 11:09:14 +0100684 const struct stm32_i2c_spec *specs;
Patrice Chotardc37885d2017-10-17 11:21:33 +0200685 struct stm32_i2c_timings *v, *_v;
Patrice Chotardebf442d2017-08-09 14:45:27 +0200686 struct list_head solutions;
Igor Prusovc3421ea2023-11-09 20:10:04 +0300687 u32 i2cclk = DIV_ROUND_CLOSEST(NSEC_PER_SEC, setup->clock_src);
Patrice Chotardebf442d2017-08-09 14:45:27 +0200688 int ret;
689
Alain Volmate04600e2020-03-06 11:09:14 +0100690 specs = get_specs(setup->speed_freq);
691 if (specs == ERR_PTR(-EINVAL)) {
Patrick Delaunay6f600e32020-11-06 19:01:50 +0100692 log_err("speed out of bound {%d}\n",
693 setup->speed_freq);
Patrice Chotardebf442d2017-08-09 14:45:27 +0200694 return -EINVAL;
695 }
696
Alain Volmate04600e2020-03-06 11:09:14 +0100697 if (setup->rise_time > specs->rise_max ||
698 setup->fall_time > specs->fall_max) {
Patrick Delaunay6f600e32020-11-06 19:01:50 +0100699 log_err("timings out of bound Rise{%d>%d}/Fall{%d>%d}\n",
700 setup->rise_time, specs->rise_max,
701 setup->fall_time, specs->fall_max);
Patrice Chotardebf442d2017-08-09 14:45:27 +0200702 return -EINVAL;
703 }
704
Patrick Delaunaya14855d2021-08-03 12:05:14 +0200705 /* Analog and Digital Filters */
706 setup->dnf = DIV_ROUND_CLOSEST(i2c_priv->dnf_dt, i2cclk);
Patrice Chotardebf442d2017-08-09 14:45:27 +0200707 if (setup->dnf > STM32_I2C_DNF_MAX) {
Patrick Delaunay6f600e32020-11-06 19:01:50 +0100708 log_err("DNF out of bound %d/%d\n",
709 setup->dnf, STM32_I2C_DNF_MAX);
Patrice Chotardebf442d2017-08-09 14:45:27 +0200710 return -EINVAL;
711 }
712
Patrice Chotardebf442d2017-08-09 14:45:27 +0200713 INIT_LIST_HEAD(&solutions);
Patrick Delaunay10605f72021-08-03 12:05:15 +0200714 ret = stm32_i2c_compute_solutions(i2cclk, setup, specs, &solutions);
Patrice Chotardebf442d2017-08-09 14:45:27 +0200715 if (ret)
716 goto exit;
717
Patrick Delaunay10605f72021-08-03 12:05:15 +0200718 ret = stm32_i2c_choose_solution(i2cclk, setup, specs, &solutions, output);
Patrice Chotardebf442d2017-08-09 14:45:27 +0200719 if (ret)
720 goto exit;
721
Patrick Delaunay6f600e32020-11-06 19:01:50 +0100722 log_debug("Presc: %i, scldel: %i, sdadel: %i, scll: %i, sclh: %i\n",
723 output->presc,
724 output->scldel, output->sdadel,
725 output->scll, output->sclh);
Patrice Chotardebf442d2017-08-09 14:45:27 +0200726
727exit:
728 /* Release list and memory */
729 list_for_each_entry_safe(v, _v, &solutions, node) {
730 list_del(&v->node);
Patrick Delaunay28b10a72018-03-12 10:46:09 +0100731 free(v);
Patrice Chotardebf442d2017-08-09 14:45:27 +0200732 }
733
734 return ret;
735}
736
Alain Volmate04600e2020-03-06 11:09:14 +0100737static u32 get_lower_rate(u32 rate)
738{
739 int i;
740
741 for (i = ARRAY_SIZE(i2c_specs) - 1; i >= 0; i--)
742 if (rate > i2c_specs[i].rate)
743 return i2c_specs[i].rate;
744
745 return i2c_specs[0].rate;
746}
747
Patrice Chotardebf442d2017-08-09 14:45:27 +0200748static int stm32_i2c_setup_timing(struct stm32_i2c_priv *i2c_priv,
Patrick Delaunayb13ab6f2018-10-29 15:31:55 +0100749 struct stm32_i2c_timings *timing)
Patrice Chotardebf442d2017-08-09 14:45:27 +0200750{
Patrick Delaunay58862782021-08-03 12:05:09 +0200751 struct stm32_i2c_setup *setup = &i2c_priv->setup;
Patrice Chotardebf442d2017-08-09 14:45:27 +0200752 int ret = 0;
753
Alain Volmate04600e2020-03-06 11:09:14 +0100754 setup->speed_freq = i2c_priv->speed;
Patrice Chotardebf442d2017-08-09 14:45:27 +0200755 setup->clock_src = clk_get_rate(&i2c_priv->clk);
756
757 if (!setup->clock_src) {
Patrick Delaunay6f600e32020-11-06 19:01:50 +0100758 log_err("clock rate is 0\n");
Patrice Chotardebf442d2017-08-09 14:45:27 +0200759 return -EINVAL;
760 }
761
762 do {
Patrick Delaunay10605f72021-08-03 12:05:15 +0200763 ret = stm32_i2c_compute_timing(i2c_priv, timing);
Patrice Chotardebf442d2017-08-09 14:45:27 +0200764 if (ret) {
Patrick Delaunay6f600e32020-11-06 19:01:50 +0100765 log_debug("failed to compute I2C timings.\n");
Alain Volmate04600e2020-03-06 11:09:14 +0100766 if (setup->speed_freq > I2C_SPEED_STANDARD_RATE) {
Patrice Chotardebf442d2017-08-09 14:45:27 +0200767 setup->speed_freq =
Alain Volmate04600e2020-03-06 11:09:14 +0100768 get_lower_rate(setup->speed_freq);
Patrick Delaunay6f600e32020-11-06 19:01:50 +0100769 log_debug("downgrade I2C Speed Freq to (%i)\n",
770 setup->speed_freq);
Patrice Chotardebf442d2017-08-09 14:45:27 +0200771 } else {
772 break;
773 }
774 }
775 } while (ret);
776
777 if (ret) {
Patrick Delaunay6f600e32020-11-06 19:01:50 +0100778 log_err("impossible to compute I2C timings.\n");
Patrice Chotardebf442d2017-08-09 14:45:27 +0200779 return ret;
780 }
781
Patrick Delaunay6f600e32020-11-06 19:01:50 +0100782 log_debug("I2C Freq(%i), Clk Source(%i)\n",
783 setup->speed_freq, setup->clock_src);
784 log_debug("I2C Rise(%i) and Fall(%i) Time\n",
785 setup->rise_time, setup->fall_time);
786 log_debug("I2C Analog Filter(%s), DNF(%i)\n",
787 setup->analog_filter ? "On" : "Off", setup->dnf);
Patrice Chotardebf442d2017-08-09 14:45:27 +0200788
Alain Volmate04600e2020-03-06 11:09:14 +0100789 i2c_priv->speed = setup->speed_freq;
790
Patrice Chotardebf442d2017-08-09 14:45:27 +0200791 return 0;
792}
793
Patrick Delaunay4d15c182020-07-06 13:31:35 +0200794static int stm32_i2c_write_fm_plus_bits(struct stm32_i2c_priv *i2c_priv)
795{
796 int ret;
797 bool enable = i2c_priv->speed > I2C_SPEED_FAST_RATE;
798
799 /* Optional */
800 if (IS_ERR_OR_NULL(i2c_priv->regmap))
801 return 0;
802
803 if (i2c_priv->regmap_sreg == i2c_priv->regmap_creg)
804 ret = regmap_update_bits(i2c_priv->regmap,
805 i2c_priv->regmap_sreg,
806 i2c_priv->regmap_mask,
807 enable ? i2c_priv->regmap_mask : 0);
808 else
809 ret = regmap_write(i2c_priv->regmap,
810 enable ? i2c_priv->regmap_sreg :
811 i2c_priv->regmap_creg,
812 i2c_priv->regmap_mask);
813
814 return ret;
815}
816
Patrice Chotardebf442d2017-08-09 14:45:27 +0200817static int stm32_i2c_hw_config(struct stm32_i2c_priv *i2c_priv)
818{
819 struct stm32_i2c_regs *regs = i2c_priv->regs;
820 struct stm32_i2c_timings t;
821 int ret;
822 u32 timing = 0;
823
824 ret = stm32_i2c_setup_timing(i2c_priv, &t);
825 if (ret)
826 return ret;
827
828 /* Disable I2C */
829 clrbits_le32(&regs->cr1, STM32_I2C_CR1_PE);
830
Patrick Delaunay4d15c182020-07-06 13:31:35 +0200831 /* Setup Fast mode plus if necessary */
832 ret = stm32_i2c_write_fm_plus_bits(i2c_priv);
833 if (ret)
834 return ret;
835
Patrice Chotardebf442d2017-08-09 14:45:27 +0200836 /* Timing settings */
837 timing |= STM32_I2C_TIMINGR_PRESC(t.presc);
838 timing |= STM32_I2C_TIMINGR_SCLDEL(t.scldel);
839 timing |= STM32_I2C_TIMINGR_SDADEL(t.sdadel);
840 timing |= STM32_I2C_TIMINGR_SCLH(t.sclh);
841 timing |= STM32_I2C_TIMINGR_SCLL(t.scll);
842 writel(timing, &regs->timingr);
843
844 /* Enable I2C */
Patrick Delaunay58862782021-08-03 12:05:09 +0200845 if (i2c_priv->setup.analog_filter)
Patrice Chotardebf442d2017-08-09 14:45:27 +0200846 clrbits_le32(&regs->cr1, STM32_I2C_CR1_ANFOFF);
847 else
848 setbits_le32(&regs->cr1, STM32_I2C_CR1_ANFOFF);
Patrick Delaunay58862782021-08-03 12:05:09 +0200849
Patrick Delaunay05b8d602021-08-03 12:05:13 +0200850 /* Program the Digital Filter */
851 clrsetbits_le32(&regs->cr1, STM32_I2C_CR1_DNF_MASK,
852 STM32_I2C_CR1_DNF(i2c_priv->setup.dnf));
853
Patrice Chotardebf442d2017-08-09 14:45:27 +0200854 setbits_le32(&regs->cr1, STM32_I2C_CR1_PE);
855
856 return 0;
857}
858
Patrick Delaunay6f600e32020-11-06 19:01:50 +0100859static int stm32_i2c_set_bus_speed(struct udevice *dev, unsigned int speed)
Patrice Chotardebf442d2017-08-09 14:45:27 +0200860{
Patrick Delaunay6f600e32020-11-06 19:01:50 +0100861 struct stm32_i2c_priv *i2c_priv = dev_get_priv(dev);
Patrice Chotardebf442d2017-08-09 14:45:27 +0200862
Alain Volmate04600e2020-03-06 11:09:14 +0100863 if (speed > I2C_SPEED_FAST_PLUS_RATE) {
Patrick Delaunay6f600e32020-11-06 19:01:50 +0100864 dev_dbg(dev, "Speed %d not supported\n", speed);
Patrice Chotardebf442d2017-08-09 14:45:27 +0200865 return -EINVAL;
866 }
867
Alain Volmate04600e2020-03-06 11:09:14 +0100868 i2c_priv->speed = speed;
869
Patrice Chotardebf442d2017-08-09 14:45:27 +0200870 return stm32_i2c_hw_config(i2c_priv);
871}
872
873static int stm32_i2c_probe(struct udevice *dev)
874{
875 struct stm32_i2c_priv *i2c_priv = dev_get_priv(dev);
876 struct reset_ctl reset_ctl;
877 fdt_addr_t addr;
878 int ret;
879
880 addr = dev_read_addr(dev);
881 if (addr == FDT_ADDR_T_NONE)
882 return -EINVAL;
883
884 i2c_priv->regs = (struct stm32_i2c_regs *)addr;
885
886 ret = clk_get_by_index(dev, 0, &i2c_priv->clk);
887 if (ret)
888 return ret;
889
890 ret = clk_enable(&i2c_priv->clk);
891 if (ret)
Sean Andersond318eb32023-12-16 14:38:42 -0500892 return ret;
Patrice Chotardebf442d2017-08-09 14:45:27 +0200893
894 ret = reset_get_by_index(dev, 0, &reset_ctl);
895 if (ret)
896 goto clk_disable;
897
898 reset_assert(&reset_ctl);
899 udelay(2);
900 reset_deassert(&reset_ctl);
901
902 return 0;
903
904clk_disable:
905 clk_disable(&i2c_priv->clk);
Patrice Chotardebf442d2017-08-09 14:45:27 +0200906
907 return ret;
908}
909
Simon Glassaad29ae2020-12-03 16:55:21 -0700910static int stm32_of_to_plat(struct udevice *dev)
Patrice Chotardebf442d2017-08-09 14:45:27 +0200911{
Patrick Delaunay58862782021-08-03 12:05:09 +0200912 const struct stm32_i2c_data *data;
Patrice Chotardebf442d2017-08-09 14:45:27 +0200913 struct stm32_i2c_priv *i2c_priv = dev_get_priv(dev);
Patrick Delaunay4d15c182020-07-06 13:31:35 +0200914 int ret;
Patrice Chotardebf442d2017-08-09 14:45:27 +0200915
Patrick Delaunay58862782021-08-03 12:05:09 +0200916 data = (const struct stm32_i2c_data *)dev_get_driver_data(dev);
917 if (!data)
Patrice Chotardebf442d2017-08-09 14:45:27 +0200918 return -EINVAL;
919
Jorge Ramirez-Ortizfeb3f9b2022-09-12 10:42:01 +0200920 i2c_priv->setup.rise_time = dev_read_u32_default(dev,
921 "i2c-scl-rising-time-ns",
922 STM32_I2C_RISE_TIME_DEFAULT);
Patrice Chotardebf442d2017-08-09 14:45:27 +0200923
Jorge Ramirez-Ortizfeb3f9b2022-09-12 10:42:01 +0200924 i2c_priv->setup.fall_time = dev_read_u32_default(dev,
925 "i2c-scl-falling-time-ns",
926 STM32_I2C_FALL_TIME_DEFAULT);
Patrick Delaunay58862782021-08-03 12:05:09 +0200927
Patrick Delaunaya14855d2021-08-03 12:05:14 +0200928 i2c_priv->dnf_dt = dev_read_u32_default(dev, "i2c-digital-filter-width-ns", 0);
929 if (!dev_read_bool(dev, "i2c-digital-filter"))
930 i2c_priv->dnf_dt = 0;
931
Patrick Delaunay20e3c412021-08-03 12:05:12 +0200932 i2c_priv->setup.analog_filter = dev_read_bool(dev, "i2c-analog-filter");
Patrice Chotardebf442d2017-08-09 14:45:27 +0200933
Patrick Delaunay4d15c182020-07-06 13:31:35 +0200934 /* Optional */
935 i2c_priv->regmap = syscon_regmap_lookup_by_phandle(dev,
936 "st,syscfg-fmp");
937 if (!IS_ERR(i2c_priv->regmap)) {
938 u32 fmp[3];
939
940 ret = dev_read_u32_array(dev, "st,syscfg-fmp", fmp, 3);
941 if (ret)
942 return ret;
943
944 i2c_priv->regmap_sreg = fmp[1];
Patrick Delaunay58862782021-08-03 12:05:09 +0200945 i2c_priv->regmap_creg = fmp[1] + data->fmp_clr_offset;
Patrick Delaunay4d15c182020-07-06 13:31:35 +0200946 i2c_priv->regmap_mask = fmp[2];
947 }
948
Patrice Chotardebf442d2017-08-09 14:45:27 +0200949 return 0;
950}
951
952static const struct dm_i2c_ops stm32_i2c_ops = {
953 .xfer = stm32_i2c_xfer,
954 .set_bus_speed = stm32_i2c_set_bus_speed,
955};
956
957static const struct udevice_id stm32_i2c_of_match[] = {
Patrick Delaunay58862782021-08-03 12:05:09 +0200958 { .compatible = "st,stm32f7-i2c", .data = (ulong)&stm32f7_data },
959 { .compatible = "st,stm32mp15-i2c", .data = (ulong)&stm32mp15_data },
Patrick Delaunay11c064e2022-06-30 10:20:14 +0200960 { .compatible = "st,stm32mp13-i2c", .data = (ulong)&stm32mp13_data },
Patrice Chotardebf442d2017-08-09 14:45:27 +0200961 {}
962};
963
964U_BOOT_DRIVER(stm32f7_i2c) = {
965 .name = "stm32f7-i2c",
966 .id = UCLASS_I2C,
967 .of_match = stm32_i2c_of_match,
Simon Glassaad29ae2020-12-03 16:55:21 -0700968 .of_to_plat = stm32_of_to_plat,
Patrice Chotardebf442d2017-08-09 14:45:27 +0200969 .probe = stm32_i2c_probe,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700970 .priv_auto = sizeof(struct stm32_i2c_priv),
Patrice Chotardebf442d2017-08-09 14:45:27 +0200971 .ops = &stm32_i2c_ops,
972};