blob: b449084b5f01a84872562bfa9299efc1e124bb9c [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 <common.h>
9#include <clk.h>
10#include <dm.h>
11#include <i2c.h>
Simon Glass0f2af882020-05-10 11:40:05 -060012#include <log.h>
Patrick Delaunay4d15c182020-07-06 13:31:35 +020013#include <regmap.h>
Patrice Chotardebf442d2017-08-09 14:45:27 +020014#include <reset.h>
Patrick Delaunay4d15c182020-07-06 13:31:35 +020015#include <syscon.h>
Patrick Delaunay6f600e32020-11-06 19:01:50 +010016#include <dm/device.h>
17#include <dm/device_compat.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060018#include <linux/bitops.h>
Simon Glassdbd79542020-05-10 11:40:11 -060019#include <linux/delay.h>
Alain Volmate04600e2020-03-06 11:09:14 +010020#include <linux/err.h>
Patrice Chotardebf442d2017-08-09 14:45:27 +020021#include <linux/io.h>
22
23/* STM32 I2C registers */
24struct stm32_i2c_regs {
25 u32 cr1; /* I2C control register 1 */
26 u32 cr2; /* I2C control register 2 */
27 u32 oar1; /* I2C own address 1 register */
28 u32 oar2; /* I2C own address 2 register */
29 u32 timingr; /* I2C timing register */
30 u32 timeoutr; /* I2C timeout register */
31 u32 isr; /* I2C interrupt and status register */
32 u32 icr; /* I2C interrupt clear register */
33 u32 pecr; /* I2C packet error checking register */
34 u32 rxdr; /* I2C receive data register */
35 u32 txdr; /* I2C transmit data register */
36};
37
38#define STM32_I2C_CR1 0x00
39#define STM32_I2C_CR2 0x04
40#define STM32_I2C_TIMINGR 0x10
41#define STM32_I2C_ISR 0x18
42#define STM32_I2C_ICR 0x1C
43#define STM32_I2C_RXDR 0x24
44#define STM32_I2C_TXDR 0x28
45
46/* STM32 I2C control 1 */
47#define STM32_I2C_CR1_ANFOFF BIT(12)
48#define STM32_I2C_CR1_ERRIE BIT(7)
49#define STM32_I2C_CR1_TCIE BIT(6)
50#define STM32_I2C_CR1_STOPIE BIT(5)
51#define STM32_I2C_CR1_NACKIE BIT(4)
52#define STM32_I2C_CR1_ADDRIE BIT(3)
53#define STM32_I2C_CR1_RXIE BIT(2)
54#define STM32_I2C_CR1_TXIE BIT(1)
55#define STM32_I2C_CR1_PE BIT(0)
56
57/* STM32 I2C control 2 */
58#define STM32_I2C_CR2_AUTOEND BIT(25)
59#define STM32_I2C_CR2_RELOAD BIT(24)
60#define STM32_I2C_CR2_NBYTES_MASK GENMASK(23, 16)
61#define STM32_I2C_CR2_NBYTES(n) ((n & 0xff) << 16)
62#define STM32_I2C_CR2_NACK BIT(15)
63#define STM32_I2C_CR2_STOP BIT(14)
64#define STM32_I2C_CR2_START BIT(13)
65#define STM32_I2C_CR2_HEAD10R BIT(12)
66#define STM32_I2C_CR2_ADD10 BIT(11)
67#define STM32_I2C_CR2_RD_WRN BIT(10)
68#define STM32_I2C_CR2_SADD10_MASK GENMASK(9, 0)
Patrick Delaunayb13ab6f2018-10-29 15:31:55 +010069#define STM32_I2C_CR2_SADD10(n) (n & STM32_I2C_CR2_SADD10_MASK)
Patrice Chotardebf442d2017-08-09 14:45:27 +020070#define STM32_I2C_CR2_SADD7_MASK GENMASK(7, 1)
71#define STM32_I2C_CR2_SADD7(n) ((n & 0x7f) << 1)
72#define STM32_I2C_CR2_RESET_MASK (STM32_I2C_CR2_HEAD10R \
73 | STM32_I2C_CR2_NBYTES_MASK \
74 | STM32_I2C_CR2_SADD7_MASK \
75 | STM32_I2C_CR2_RELOAD \
76 | STM32_I2C_CR2_RD_WRN)
77
78/* STM32 I2C Interrupt Status */
79#define STM32_I2C_ISR_BUSY BIT(15)
80#define STM32_I2C_ISR_ARLO BIT(9)
81#define STM32_I2C_ISR_BERR BIT(8)
82#define STM32_I2C_ISR_TCR BIT(7)
83#define STM32_I2C_ISR_TC BIT(6)
84#define STM32_I2C_ISR_STOPF BIT(5)
85#define STM32_I2C_ISR_NACKF BIT(4)
86#define STM32_I2C_ISR_ADDR BIT(3)
87#define STM32_I2C_ISR_RXNE BIT(2)
88#define STM32_I2C_ISR_TXIS BIT(1)
89#define STM32_I2C_ISR_TXE BIT(0)
90#define STM32_I2C_ISR_ERRORS (STM32_I2C_ISR_BERR \
91 | STM32_I2C_ISR_ARLO)
92
93/* STM32 I2C Interrupt Clear */
94#define STM32_I2C_ICR_ARLOCF BIT(9)
95#define STM32_I2C_ICR_BERRCF BIT(8)
96#define STM32_I2C_ICR_STOPCF BIT(5)
97#define STM32_I2C_ICR_NACKCF BIT(4)
98
99/* STM32 I2C Timing */
100#define STM32_I2C_TIMINGR_PRESC(n) ((n & 0xf) << 28)
101#define STM32_I2C_TIMINGR_SCLDEL(n) ((n & 0xf) << 20)
102#define STM32_I2C_TIMINGR_SDADEL(n) ((n & 0xf) << 16)
103#define STM32_I2C_TIMINGR_SCLH(n) ((n & 0xff) << 8)
104#define STM32_I2C_TIMINGR_SCLL(n) (n & 0xff)
105
106#define STM32_I2C_MAX_LEN 0xff
107
108#define STM32_I2C_DNF_DEFAULT 0
109#define STM32_I2C_DNF_MAX 16
110
111#define STM32_I2C_ANALOG_FILTER_ENABLE 1
112#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
124#define STM32_NSEC_PER_SEC 1000000000L
125
Patrice Chotardebf442d2017-08-09 14:45:27 +0200126/**
127 * struct stm32_i2c_spec - private i2c specification timing
128 * @rate: I2C bus speed (Hz)
129 * @rate_min: 80% of I2C bus speed (Hz)
130 * @rate_max: 120% of I2C bus speed (Hz)
131 * @fall_max: Max fall time of both SDA and SCL signals (ns)
132 * @rise_max: Max rise time of both SDA and SCL signals (ns)
133 * @hddat_min: Min data hold time (ns)
134 * @vddat_max: Max data valid time (ns)
135 * @sudat_min: Min data setup time (ns)
136 * @l_min: Min low period of the SCL clock (ns)
137 * @h_min: Min high period of the SCL clock (ns)
138 */
139
140struct stm32_i2c_spec {
141 u32 rate;
142 u32 rate_min;
143 u32 rate_max;
144 u32 fall_max;
145 u32 rise_max;
146 u32 hddat_min;
147 u32 vddat_max;
148 u32 sudat_min;
149 u32 l_min;
150 u32 h_min;
151};
152
153/**
154 * struct stm32_i2c_setup - private I2C timing setup parameters
Patrice Chotardebf442d2017-08-09 14:45:27 +0200155 * @speed_freq: I2C speed frequency (Hz)
156 * @clock_src: I2C clock source frequency (Hz)
157 * @rise_time: Rise time (ns)
158 * @fall_time: Fall time (ns)
159 * @dnf: Digital filter coefficient (0-16)
160 * @analog_filter: Analog filter delay (On/Off)
161 */
162struct stm32_i2c_setup {
Patrice Chotardebf442d2017-08-09 14:45:27 +0200163 u32 speed_freq;
164 u32 clock_src;
165 u32 rise_time;
166 u32 fall_time;
167 u8 dnf;
168 bool analog_filter;
Patrick Delaunay58862782021-08-03 12:05:09 +0200169};
170
171/**
172 * struct stm32_i2c_data - driver data for I2C configuration by compatible
173 * @fmp_clr_offset: Fast Mode Plus clear register offset from set register
174 */
175struct stm32_i2c_data {
Patrick Delaunay4d15c182020-07-06 13:31:35 +0200176 u32 fmp_clr_offset;
Patrice Chotardebf442d2017-08-09 14:45:27 +0200177};
178
179/**
180 * struct stm32_i2c_timings - private I2C output parameters
181 * @prec: Prescaler value
182 * @scldel: Data setup time
183 * @sdadel: Data hold time
184 * @sclh: SCL high period (master mode)
185 * @sclh: SCL low period (master mode)
186 */
187struct stm32_i2c_timings {
188 struct list_head node;
189 u8 presc;
190 u8 scldel;
191 u8 sdadel;
192 u8 sclh;
193 u8 scll;
194};
195
Patrick Delaunay4d15c182020-07-06 13:31:35 +0200196/**
197 * struct stm32_i2c_priv - private data of the controller
198 * @regs: I2C registers address
199 * @clk: hw i2c clock
200 * @setup: I2C timing setup parameters
201 * @speed: I2C clock frequency of the controller. Standard, Fast or Fast+
202 * @regmap: holds SYSCFG phandle for Fast Mode Plus bit
203 * @regmap_sreg: register address for setting Fast Mode Plus bits
204 * @regmap_creg: register address for clearing Fast Mode Plus bits
205 * @regmap_mask: mask for Fast Mode Plus bits
206 */
Patrice Chotardebf442d2017-08-09 14:45:27 +0200207struct stm32_i2c_priv {
208 struct stm32_i2c_regs *regs;
209 struct clk clk;
Patrick Delaunay58862782021-08-03 12:05:09 +0200210 struct stm32_i2c_setup setup;
Alain Volmate04600e2020-03-06 11:09:14 +0100211 u32 speed;
Patrick Delaunay4d15c182020-07-06 13:31:35 +0200212 struct regmap *regmap;
213 u32 regmap_sreg;
214 u32 regmap_creg;
215 u32 regmap_mask;
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
Patrice Chotardebf442d2017-08-09 14:45:27 +0200268static int stm32_i2c_check_device_busy(struct stm32_i2c_priv *i2c_priv)
269{
270 struct stm32_i2c_regs *regs = i2c_priv->regs;
271 u32 status = readl(&regs->isr);
272
273 if (status & STM32_I2C_ISR_BUSY)
274 return -EBUSY;
275
276 return 0;
277}
278
279static void stm32_i2c_message_start(struct stm32_i2c_priv *i2c_priv,
Patrick Delaunayb13ab6f2018-10-29 15:31:55 +0100280 struct i2c_msg *msg, bool stop)
Patrice Chotardebf442d2017-08-09 14:45:27 +0200281{
282 struct stm32_i2c_regs *regs = i2c_priv->regs;
283 u32 cr2 = readl(&regs->cr2);
284
285 /* Set transfer direction */
286 cr2 &= ~STM32_I2C_CR2_RD_WRN;
287 if (msg->flags & I2C_M_RD)
288 cr2 |= STM32_I2C_CR2_RD_WRN;
289
290 /* Set slave address */
291 cr2 &= ~(STM32_I2C_CR2_HEAD10R | STM32_I2C_CR2_ADD10);
292 if (msg->flags & I2C_M_TEN) {
293 cr2 &= ~STM32_I2C_CR2_SADD10_MASK;
294 cr2 |= STM32_I2C_CR2_SADD10(msg->addr);
295 cr2 |= STM32_I2C_CR2_ADD10;
296 } else {
297 cr2 &= ~STM32_I2C_CR2_SADD7_MASK;
298 cr2 |= STM32_I2C_CR2_SADD7(msg->addr);
299 }
300
301 /* Set nb bytes to transfer and reload or autoend bits */
302 cr2 &= ~(STM32_I2C_CR2_NBYTES_MASK | STM32_I2C_CR2_RELOAD |
303 STM32_I2C_CR2_AUTOEND);
304 if (msg->len > STM32_I2C_MAX_LEN) {
305 cr2 |= STM32_I2C_CR2_NBYTES(STM32_I2C_MAX_LEN);
306 cr2 |= STM32_I2C_CR2_RELOAD;
307 } else {
308 cr2 |= STM32_I2C_CR2_NBYTES(msg->len);
309 }
310
311 /* Write configurations register */
312 writel(cr2, &regs->cr2);
313
314 /* START/ReSTART generation */
315 setbits_le32(&regs->cr2, STM32_I2C_CR2_START);
316}
317
318/*
319 * RELOAD mode must be selected if total number of data bytes to be
320 * sent is greater than MAX_LEN
321 */
322
323static void stm32_i2c_handle_reload(struct stm32_i2c_priv *i2c_priv,
Patrick Delaunayb13ab6f2018-10-29 15:31:55 +0100324 struct i2c_msg *msg, bool stop)
Patrice Chotardebf442d2017-08-09 14:45:27 +0200325{
326 struct stm32_i2c_regs *regs = i2c_priv->regs;
327 u32 cr2 = readl(&regs->cr2);
328
329 cr2 &= ~STM32_I2C_CR2_NBYTES_MASK;
330
331 if (msg->len > STM32_I2C_MAX_LEN) {
332 cr2 |= STM32_I2C_CR2_NBYTES(STM32_I2C_MAX_LEN);
333 } else {
334 cr2 &= ~STM32_I2C_CR2_RELOAD;
335 cr2 |= STM32_I2C_CR2_NBYTES(msg->len);
336 }
337
338 writel(cr2, &regs->cr2);
339}
340
341static int stm32_i2c_wait_flags(struct stm32_i2c_priv *i2c_priv,
Patrick Delaunayb13ab6f2018-10-29 15:31:55 +0100342 u32 flags, u32 *status)
Patrice Chotardebf442d2017-08-09 14:45:27 +0200343{
344 struct stm32_i2c_regs *regs = i2c_priv->regs;
345 u32 time_start = get_timer(0);
346
347 *status = readl(&regs->isr);
348 while (!(*status & flags)) {
349 if (get_timer(time_start) > CONFIG_SYS_HZ) {
Patrick Delaunay6f600e32020-11-06 19:01:50 +0100350 log_debug("i2c timeout\n");
Patrice Chotardebf442d2017-08-09 14:45:27 +0200351 return -ETIMEDOUT;
352 }
353
354 *status = readl(&regs->isr);
355 }
356
357 return 0;
358}
359
360static int stm32_i2c_check_end_of_message(struct stm32_i2c_priv *i2c_priv)
361{
362 struct stm32_i2c_regs *regs = i2c_priv->regs;
363 u32 mask = STM32_I2C_ISR_ERRORS | STM32_I2C_ISR_NACKF |
364 STM32_I2C_ISR_STOPF;
365 u32 status;
366 int ret;
367
368 ret = stm32_i2c_wait_flags(i2c_priv, mask, &status);
369 if (ret)
370 return ret;
371
372 if (status & STM32_I2C_ISR_BERR) {
Patrick Delaunay6f600e32020-11-06 19:01:50 +0100373 log_debug("Bus error\n");
Patrice Chotardebf442d2017-08-09 14:45:27 +0200374
375 /* Clear BERR flag */
376 setbits_le32(&regs->icr, STM32_I2C_ICR_BERRCF);
377
378 return -EIO;
379 }
380
381 if (status & STM32_I2C_ISR_ARLO) {
Patrick Delaunay6f600e32020-11-06 19:01:50 +0100382 log_debug("Arbitration lost\n");
Patrice Chotardebf442d2017-08-09 14:45:27 +0200383
384 /* Clear ARLO flag */
385 setbits_le32(&regs->icr, STM32_I2C_ICR_ARLOCF);
386
387 return -EAGAIN;
388 }
389
390 if (status & STM32_I2C_ISR_NACKF) {
Patrick Delaunay6f600e32020-11-06 19:01:50 +0100391 log_debug("Receive NACK\n");
Patrice Chotardebf442d2017-08-09 14:45:27 +0200392
393 /* Clear NACK flag */
394 setbits_le32(&regs->icr, STM32_I2C_ICR_NACKCF);
395
396 /* Wait until STOPF flag is set */
397 mask = STM32_I2C_ISR_STOPF;
398 ret = stm32_i2c_wait_flags(i2c_priv, mask, &status);
399 if (ret)
400 return ret;
401
402 ret = -EIO;
403 }
404
405 if (status & STM32_I2C_ISR_STOPF) {
406 /* Clear STOP flag */
407 setbits_le32(&regs->icr, STM32_I2C_ICR_STOPCF);
408
409 /* Clear control register 2 */
410 setbits_le32(&regs->cr2, STM32_I2C_CR2_RESET_MASK);
411 }
412
413 return ret;
414}
415
416static int stm32_i2c_message_xfer(struct stm32_i2c_priv *i2c_priv,
Patrick Delaunayb13ab6f2018-10-29 15:31:55 +0100417 struct i2c_msg *msg, bool stop)
Patrice Chotardebf442d2017-08-09 14:45:27 +0200418{
419 struct stm32_i2c_regs *regs = i2c_priv->regs;
420 u32 status;
421 u32 mask = msg->flags & I2C_M_RD ? STM32_I2C_ISR_RXNE :
422 STM32_I2C_ISR_TXIS | STM32_I2C_ISR_NACKF;
423 int bytes_to_rw = msg->len > STM32_I2C_MAX_LEN ?
424 STM32_I2C_MAX_LEN : msg->len;
425 int ret = 0;
426
427 /* Add errors */
428 mask |= STM32_I2C_ISR_ERRORS;
429
430 stm32_i2c_message_start(i2c_priv, msg, stop);
431
432 while (msg->len) {
433 /*
434 * Wait until TXIS/NACKF/BERR/ARLO flags or
435 * RXNE/BERR/ARLO flags are set
436 */
437 ret = stm32_i2c_wait_flags(i2c_priv, mask, &status);
438 if (ret)
439 break;
440
441 if (status & (STM32_I2C_ISR_NACKF | STM32_I2C_ISR_ERRORS))
442 break;
443
444 if (status & STM32_I2C_ISR_RXNE) {
445 *msg->buf++ = readb(&regs->rxdr);
446 msg->len--;
447 bytes_to_rw--;
448 }
449
450 if (status & STM32_I2C_ISR_TXIS) {
451 writeb(*msg->buf++, &regs->txdr);
452 msg->len--;
453 bytes_to_rw--;
454 }
455
456 if (!bytes_to_rw && msg->len) {
457 /* Wait until TCR flag is set */
458 mask = STM32_I2C_ISR_TCR;
459 ret = stm32_i2c_wait_flags(i2c_priv, mask, &status);
460 if (ret)
461 break;
462
463 bytes_to_rw = msg->len > STM32_I2C_MAX_LEN ?
464 STM32_I2C_MAX_LEN : msg->len;
465 mask = msg->flags & I2C_M_RD ? STM32_I2C_ISR_RXNE :
466 STM32_I2C_ISR_TXIS | STM32_I2C_ISR_NACKF;
467
468 stm32_i2c_handle_reload(i2c_priv, msg, stop);
469 } else if (!bytes_to_rw) {
470 /* Wait until TC flag is set */
471 mask = STM32_I2C_ISR_TC;
472 ret = stm32_i2c_wait_flags(i2c_priv, mask, &status);
473 if (ret)
474 break;
475
476 if (!stop)
477 /* Message sent, new message has to be sent */
478 return 0;
479 }
480 }
481
482 /* End of transfer, send stop condition */
483 mask = STM32_I2C_CR2_STOP;
484 setbits_le32(&regs->cr2, mask);
485
486 return stm32_i2c_check_end_of_message(i2c_priv);
487}
488
489static int stm32_i2c_xfer(struct udevice *bus, struct i2c_msg *msg,
Patrick Delaunayb13ab6f2018-10-29 15:31:55 +0100490 int nmsgs)
Patrice Chotardebf442d2017-08-09 14:45:27 +0200491{
492 struct stm32_i2c_priv *i2c_priv = dev_get_priv(bus);
493 int ret;
494
495 ret = stm32_i2c_check_device_busy(i2c_priv);
496 if (ret)
497 return ret;
498
499 for (; nmsgs > 0; nmsgs--, msg++) {
500 ret = stm32_i2c_message_xfer(i2c_priv, msg, nmsgs == 1);
501 if (ret)
502 return ret;
503 }
504
505 return 0;
506}
507
508static int stm32_i2c_compute_solutions(struct stm32_i2c_setup *setup,
Alain Volmate04600e2020-03-06 11:09:14 +0100509 const struct stm32_i2c_spec *specs,
Patrice Chotardebf442d2017-08-09 14:45:27 +0200510 struct list_head *solutions)
511{
512 struct stm32_i2c_timings *v;
513 u32 p_prev = STM32_PRESC_MAX;
514 u32 i2cclk = DIV_ROUND_CLOSEST(STM32_NSEC_PER_SEC,
515 setup->clock_src);
516 u32 af_delay_min, af_delay_max;
517 u16 p, l, a;
518 int sdadel_min, sdadel_max, scldel_min;
519 int ret = 0;
520
521 af_delay_min = setup->analog_filter ?
522 STM32_I2C_ANALOG_FILTER_DELAY_MIN : 0;
523 af_delay_max = setup->analog_filter ?
524 STM32_I2C_ANALOG_FILTER_DELAY_MAX : 0;
525
Alain Volmate04600e2020-03-06 11:09:14 +0100526 sdadel_min = specs->hddat_min + setup->fall_time -
Patrice Chotardebf442d2017-08-09 14:45:27 +0200527 af_delay_min - (setup->dnf + 3) * i2cclk;
528
Alain Volmate04600e2020-03-06 11:09:14 +0100529 sdadel_max = specs->vddat_max - setup->rise_time -
Patrice Chotardebf442d2017-08-09 14:45:27 +0200530 af_delay_max - (setup->dnf + 4) * i2cclk;
531
Alain Volmate04600e2020-03-06 11:09:14 +0100532 scldel_min = setup->rise_time + specs->sudat_min;
Patrice Chotardebf442d2017-08-09 14:45:27 +0200533
534 if (sdadel_min < 0)
535 sdadel_min = 0;
536 if (sdadel_max < 0)
537 sdadel_max = 0;
538
Patrick Delaunay6f600e32020-11-06 19:01:50 +0100539 log_debug("SDADEL(min/max): %i/%i, SCLDEL(Min): %i\n",
540 sdadel_min, sdadel_max, scldel_min);
Patrice Chotardebf442d2017-08-09 14:45:27 +0200541
542 /* Compute possible values for PRESC, SCLDEL and SDADEL */
543 for (p = 0; p < STM32_PRESC_MAX; p++) {
544 for (l = 0; l < STM32_SCLDEL_MAX; l++) {
Patrick Delaunay6f4f9122019-06-21 15:26:47 +0200545 int scldel = (l + 1) * (p + 1) * i2cclk;
Patrice Chotardebf442d2017-08-09 14:45:27 +0200546
547 if (scldel < scldel_min)
548 continue;
549
550 for (a = 0; a < STM32_SDADEL_MAX; a++) {
Patrick Delaunay6f4f9122019-06-21 15:26:47 +0200551 int sdadel = (a * (p + 1) + 1) * i2cclk;
Patrice Chotardebf442d2017-08-09 14:45:27 +0200552
553 if (((sdadel >= sdadel_min) &&
554 (sdadel <= sdadel_max)) &&
555 (p != p_prev)) {
Patrick Delaunay28b10a72018-03-12 10:46:09 +0100556 v = calloc(1, sizeof(*v));
Patrice Chotardebf442d2017-08-09 14:45:27 +0200557 if (!v)
558 return -ENOMEM;
559
560 v->presc = p;
561 v->scldel = l;
562 v->sdadel = a;
563 p_prev = p;
564
565 list_add_tail(&v->node, solutions);
Nicolas Le Bayonc41d3072019-04-18 17:32:43 +0200566 break;
Patrice Chotardebf442d2017-08-09 14:45:27 +0200567 }
568 }
Nicolas Le Bayonc41d3072019-04-18 17:32:43 +0200569
570 if (p_prev == p)
571 break;
Patrice Chotardebf442d2017-08-09 14:45:27 +0200572 }
573 }
574
575 if (list_empty(solutions)) {
Patrick Delaunay6f600e32020-11-06 19:01:50 +0100576 log_err("no Prescaler solution\n");
Patrice Chotardebf442d2017-08-09 14:45:27 +0200577 ret = -EPERM;
578 }
579
580 return ret;
581}
582
583static int stm32_i2c_choose_solution(struct stm32_i2c_setup *setup,
Alain Volmate04600e2020-03-06 11:09:14 +0100584 const struct stm32_i2c_spec *specs,
Patrice Chotardebf442d2017-08-09 14:45:27 +0200585 struct list_head *solutions,
586 struct stm32_i2c_timings *s)
587{
588 struct stm32_i2c_timings *v;
589 u32 i2cbus = DIV_ROUND_CLOSEST(STM32_NSEC_PER_SEC,
590 setup->speed_freq);
591 u32 clk_error_prev = i2cbus;
592 u32 i2cclk = DIV_ROUND_CLOSEST(STM32_NSEC_PER_SEC,
593 setup->clock_src);
594 u32 clk_min, clk_max;
595 u32 af_delay_min;
596 u32 dnf_delay;
597 u32 tsync;
598 u16 l, h;
Christophe Kerelloa71376a2017-10-17 11:21:32 +0200599 bool sol_found = false;
Patrice Chotardebf442d2017-08-09 14:45:27 +0200600 int ret = 0;
601
602 af_delay_min = setup->analog_filter ?
603 STM32_I2C_ANALOG_FILTER_DELAY_MIN : 0;
604 dnf_delay = setup->dnf * i2cclk;
605
606 tsync = af_delay_min + dnf_delay + (2 * i2cclk);
Alain Volmate04600e2020-03-06 11:09:14 +0100607 clk_max = STM32_NSEC_PER_SEC / specs->rate_min;
608 clk_min = STM32_NSEC_PER_SEC / specs->rate_max;
Patrice Chotardebf442d2017-08-09 14:45:27 +0200609
610 /*
611 * Among Prescaler possibilities discovered above figures out SCL Low
612 * and High Period. Provided:
613 * - SCL Low Period has to be higher than Low Period of the SCL Clock
614 * defined by I2C Specification. I2C Clock has to be lower than
615 * (SCL Low Period - Analog/Digital filters) / 4.
616 * - SCL High Period has to be lower than High Period of the SCL Clock
617 * defined by I2C Specification
618 * - I2C Clock has to be lower than SCL High Period
619 */
620 list_for_each_entry(v, solutions, node) {
621 u32 prescaler = (v->presc + 1) * i2cclk;
622
623 for (l = 0; l < STM32_SCLL_MAX; l++) {
624 u32 tscl_l = (l + 1) * prescaler + tsync;
Patrick Delaunayb13ab6f2018-10-29 15:31:55 +0100625
Alain Volmate04600e2020-03-06 11:09:14 +0100626 if (tscl_l < specs->l_min ||
Patrice Chotardebf442d2017-08-09 14:45:27 +0200627 (i2cclk >=
628 ((tscl_l - af_delay_min - dnf_delay) / 4))) {
629 continue;
630 }
631
632 for (h = 0; h < STM32_SCLH_MAX; h++) {
633 u32 tscl_h = (h + 1) * prescaler + tsync;
634 u32 tscl = tscl_l + tscl_h +
635 setup->rise_time + setup->fall_time;
636
637 if ((tscl >= clk_min) && (tscl <= clk_max) &&
Alain Volmate04600e2020-03-06 11:09:14 +0100638 (tscl_h >= specs->h_min) &&
Patrice Chotardebf442d2017-08-09 14:45:27 +0200639 (i2cclk < tscl_h)) {
Patrick Delaunay6f4f9122019-06-21 15:26:47 +0200640 u32 clk_error;
Patrice Chotardebf442d2017-08-09 14:45:27 +0200641
Patrick Delaunay6f4f9122019-06-21 15:26:47 +0200642 if (tscl > i2cbus)
643 clk_error = tscl - i2cbus;
644 else
645 clk_error = i2cbus - tscl;
Patrice Chotardebf442d2017-08-09 14:45:27 +0200646
647 if (clk_error < clk_error_prev) {
648 clk_error_prev = clk_error;
649 v->scll = l;
650 v->sclh = h;
Christophe Kerelloa71376a2017-10-17 11:21:32 +0200651 sol_found = true;
652 memcpy(s, v, sizeof(*s));
Patrice Chotardebf442d2017-08-09 14:45:27 +0200653 }
654 }
655 }
656 }
657 }
658
Christophe Kerelloa71376a2017-10-17 11:21:32 +0200659 if (!sol_found) {
Patrick Delaunay6f600e32020-11-06 19:01:50 +0100660 log_err("no solution at all\n");
Patrice Chotardebf442d2017-08-09 14:45:27 +0200661 ret = -EPERM;
662 }
663
664 return ret;
665}
666
Alain Volmate04600e2020-03-06 11:09:14 +0100667static const struct stm32_i2c_spec *get_specs(u32 rate)
668{
669 unsigned int i;
670
671 for (i = 0; i < ARRAY_SIZE(i2c_specs); i++)
672 if (rate <= i2c_specs[i].rate)
673 return &i2c_specs[i];
674
675 /* NOT REACHED */
676 return ERR_PTR(-EINVAL);
677}
678
Patrice Chotardebf442d2017-08-09 14:45:27 +0200679static int stm32_i2c_compute_timing(struct stm32_i2c_priv *i2c_priv,
Patrick Delaunayb13ab6f2018-10-29 15:31:55 +0100680 struct stm32_i2c_setup *setup,
681 struct stm32_i2c_timings *output)
Patrice Chotardebf442d2017-08-09 14:45:27 +0200682{
Alain Volmate04600e2020-03-06 11:09:14 +0100683 const struct stm32_i2c_spec *specs;
Patrice Chotardc37885d2017-10-17 11:21:33 +0200684 struct stm32_i2c_timings *v, *_v;
Patrice Chotardebf442d2017-08-09 14:45:27 +0200685 struct list_head solutions;
686 int ret;
687
Alain Volmate04600e2020-03-06 11:09:14 +0100688 specs = get_specs(setup->speed_freq);
689 if (specs == ERR_PTR(-EINVAL)) {
Patrick Delaunay6f600e32020-11-06 19:01:50 +0100690 log_err("speed out of bound {%d}\n",
691 setup->speed_freq);
Patrice Chotardebf442d2017-08-09 14:45:27 +0200692 return -EINVAL;
693 }
694
Alain Volmate04600e2020-03-06 11:09:14 +0100695 if (setup->rise_time > specs->rise_max ||
696 setup->fall_time > specs->fall_max) {
Patrick Delaunay6f600e32020-11-06 19:01:50 +0100697 log_err("timings out of bound Rise{%d>%d}/Fall{%d>%d}\n",
698 setup->rise_time, specs->rise_max,
699 setup->fall_time, specs->fall_max);
Patrice Chotardebf442d2017-08-09 14:45:27 +0200700 return -EINVAL;
701 }
702
703 if (setup->dnf > STM32_I2C_DNF_MAX) {
Patrick Delaunay6f600e32020-11-06 19:01:50 +0100704 log_err("DNF out of bound %d/%d\n",
705 setup->dnf, STM32_I2C_DNF_MAX);
Patrice Chotardebf442d2017-08-09 14:45:27 +0200706 return -EINVAL;
707 }
708
Patrice Chotardebf442d2017-08-09 14:45:27 +0200709 INIT_LIST_HEAD(&solutions);
Alain Volmate04600e2020-03-06 11:09:14 +0100710 ret = stm32_i2c_compute_solutions(setup, specs, &solutions);
Patrice Chotardebf442d2017-08-09 14:45:27 +0200711 if (ret)
712 goto exit;
713
Alain Volmate04600e2020-03-06 11:09:14 +0100714 ret = stm32_i2c_choose_solution(setup, specs, &solutions, output);
Patrice Chotardebf442d2017-08-09 14:45:27 +0200715 if (ret)
716 goto exit;
717
Patrick Delaunay6f600e32020-11-06 19:01:50 +0100718 log_debug("Presc: %i, scldel: %i, sdadel: %i, scll: %i, sclh: %i\n",
719 output->presc,
720 output->scldel, output->sdadel,
721 output->scll, output->sclh);
Patrice Chotardebf442d2017-08-09 14:45:27 +0200722
723exit:
724 /* Release list and memory */
725 list_for_each_entry_safe(v, _v, &solutions, node) {
726 list_del(&v->node);
Patrick Delaunay28b10a72018-03-12 10:46:09 +0100727 free(v);
Patrice Chotardebf442d2017-08-09 14:45:27 +0200728 }
729
730 return ret;
731}
732
Alain Volmate04600e2020-03-06 11:09:14 +0100733static u32 get_lower_rate(u32 rate)
734{
735 int i;
736
737 for (i = ARRAY_SIZE(i2c_specs) - 1; i >= 0; i--)
738 if (rate > i2c_specs[i].rate)
739 return i2c_specs[i].rate;
740
741 return i2c_specs[0].rate;
742}
743
Patrice Chotardebf442d2017-08-09 14:45:27 +0200744static int stm32_i2c_setup_timing(struct stm32_i2c_priv *i2c_priv,
Patrick Delaunayb13ab6f2018-10-29 15:31:55 +0100745 struct stm32_i2c_timings *timing)
Patrice Chotardebf442d2017-08-09 14:45:27 +0200746{
Patrick Delaunay58862782021-08-03 12:05:09 +0200747 struct stm32_i2c_setup *setup = &i2c_priv->setup;
Patrice Chotardebf442d2017-08-09 14:45:27 +0200748 int ret = 0;
749
Alain Volmate04600e2020-03-06 11:09:14 +0100750 setup->speed_freq = i2c_priv->speed;
Patrice Chotardebf442d2017-08-09 14:45:27 +0200751 setup->clock_src = clk_get_rate(&i2c_priv->clk);
752
753 if (!setup->clock_src) {
Patrick Delaunay6f600e32020-11-06 19:01:50 +0100754 log_err("clock rate is 0\n");
Patrice Chotardebf442d2017-08-09 14:45:27 +0200755 return -EINVAL;
756 }
757
758 do {
759 ret = stm32_i2c_compute_timing(i2c_priv, setup, timing);
760 if (ret) {
Patrick Delaunay6f600e32020-11-06 19:01:50 +0100761 log_debug("failed to compute I2C timings.\n");
Alain Volmate04600e2020-03-06 11:09:14 +0100762 if (setup->speed_freq > I2C_SPEED_STANDARD_RATE) {
Patrice Chotardebf442d2017-08-09 14:45:27 +0200763 setup->speed_freq =
Alain Volmate04600e2020-03-06 11:09:14 +0100764 get_lower_rate(setup->speed_freq);
Patrick Delaunay6f600e32020-11-06 19:01:50 +0100765 log_debug("downgrade I2C Speed Freq to (%i)\n",
766 setup->speed_freq);
Patrice Chotardebf442d2017-08-09 14:45:27 +0200767 } else {
768 break;
769 }
770 }
771 } while (ret);
772
773 if (ret) {
Patrick Delaunay6f600e32020-11-06 19:01:50 +0100774 log_err("impossible to compute I2C timings.\n");
Patrice Chotardebf442d2017-08-09 14:45:27 +0200775 return ret;
776 }
777
Patrick Delaunay6f600e32020-11-06 19:01:50 +0100778 log_debug("I2C Freq(%i), Clk Source(%i)\n",
779 setup->speed_freq, setup->clock_src);
780 log_debug("I2C Rise(%i) and Fall(%i) Time\n",
781 setup->rise_time, setup->fall_time);
782 log_debug("I2C Analog Filter(%s), DNF(%i)\n",
783 setup->analog_filter ? "On" : "Off", setup->dnf);
Patrice Chotardebf442d2017-08-09 14:45:27 +0200784
Alain Volmate04600e2020-03-06 11:09:14 +0100785 i2c_priv->speed = setup->speed_freq;
786
Patrice Chotardebf442d2017-08-09 14:45:27 +0200787 return 0;
788}
789
Patrick Delaunay4d15c182020-07-06 13:31:35 +0200790static int stm32_i2c_write_fm_plus_bits(struct stm32_i2c_priv *i2c_priv)
791{
792 int ret;
793 bool enable = i2c_priv->speed > I2C_SPEED_FAST_RATE;
794
795 /* Optional */
796 if (IS_ERR_OR_NULL(i2c_priv->regmap))
797 return 0;
798
799 if (i2c_priv->regmap_sreg == i2c_priv->regmap_creg)
800 ret = regmap_update_bits(i2c_priv->regmap,
801 i2c_priv->regmap_sreg,
802 i2c_priv->regmap_mask,
803 enable ? i2c_priv->regmap_mask : 0);
804 else
805 ret = regmap_write(i2c_priv->regmap,
806 enable ? i2c_priv->regmap_sreg :
807 i2c_priv->regmap_creg,
808 i2c_priv->regmap_mask);
809
810 return ret;
811}
812
Patrice Chotardebf442d2017-08-09 14:45:27 +0200813static int stm32_i2c_hw_config(struct stm32_i2c_priv *i2c_priv)
814{
815 struct stm32_i2c_regs *regs = i2c_priv->regs;
816 struct stm32_i2c_timings t;
817 int ret;
818 u32 timing = 0;
819
820 ret = stm32_i2c_setup_timing(i2c_priv, &t);
821 if (ret)
822 return ret;
823
824 /* Disable I2C */
825 clrbits_le32(&regs->cr1, STM32_I2C_CR1_PE);
826
Patrick Delaunay4d15c182020-07-06 13:31:35 +0200827 /* Setup Fast mode plus if necessary */
828 ret = stm32_i2c_write_fm_plus_bits(i2c_priv);
829 if (ret)
830 return ret;
831
Patrice Chotardebf442d2017-08-09 14:45:27 +0200832 /* Timing settings */
833 timing |= STM32_I2C_TIMINGR_PRESC(t.presc);
834 timing |= STM32_I2C_TIMINGR_SCLDEL(t.scldel);
835 timing |= STM32_I2C_TIMINGR_SDADEL(t.sdadel);
836 timing |= STM32_I2C_TIMINGR_SCLH(t.sclh);
837 timing |= STM32_I2C_TIMINGR_SCLL(t.scll);
838 writel(timing, &regs->timingr);
839
840 /* Enable I2C */
Patrick Delaunay58862782021-08-03 12:05:09 +0200841 if (i2c_priv->setup.analog_filter)
Patrice Chotardebf442d2017-08-09 14:45:27 +0200842 clrbits_le32(&regs->cr1, STM32_I2C_CR1_ANFOFF);
843 else
844 setbits_le32(&regs->cr1, STM32_I2C_CR1_ANFOFF);
Patrick Delaunay58862782021-08-03 12:05:09 +0200845
Patrice Chotardebf442d2017-08-09 14:45:27 +0200846 setbits_le32(&regs->cr1, STM32_I2C_CR1_PE);
847
848 return 0;
849}
850
Patrick Delaunay6f600e32020-11-06 19:01:50 +0100851static int stm32_i2c_set_bus_speed(struct udevice *dev, unsigned int speed)
Patrice Chotardebf442d2017-08-09 14:45:27 +0200852{
Patrick Delaunay6f600e32020-11-06 19:01:50 +0100853 struct stm32_i2c_priv *i2c_priv = dev_get_priv(dev);
Patrice Chotardebf442d2017-08-09 14:45:27 +0200854
Alain Volmate04600e2020-03-06 11:09:14 +0100855 if (speed > I2C_SPEED_FAST_PLUS_RATE) {
Patrick Delaunay6f600e32020-11-06 19:01:50 +0100856 dev_dbg(dev, "Speed %d not supported\n", speed);
Patrice Chotardebf442d2017-08-09 14:45:27 +0200857 return -EINVAL;
858 }
859
Alain Volmate04600e2020-03-06 11:09:14 +0100860 i2c_priv->speed = speed;
861
Patrice Chotardebf442d2017-08-09 14:45:27 +0200862 return stm32_i2c_hw_config(i2c_priv);
863}
864
865static int stm32_i2c_probe(struct udevice *dev)
866{
867 struct stm32_i2c_priv *i2c_priv = dev_get_priv(dev);
868 struct reset_ctl reset_ctl;
869 fdt_addr_t addr;
870 int ret;
871
872 addr = dev_read_addr(dev);
873 if (addr == FDT_ADDR_T_NONE)
874 return -EINVAL;
875
876 i2c_priv->regs = (struct stm32_i2c_regs *)addr;
877
878 ret = clk_get_by_index(dev, 0, &i2c_priv->clk);
879 if (ret)
880 return ret;
881
882 ret = clk_enable(&i2c_priv->clk);
883 if (ret)
884 goto clk_free;
885
886 ret = reset_get_by_index(dev, 0, &reset_ctl);
887 if (ret)
888 goto clk_disable;
889
890 reset_assert(&reset_ctl);
891 udelay(2);
892 reset_deassert(&reset_ctl);
893
894 return 0;
895
896clk_disable:
897 clk_disable(&i2c_priv->clk);
898clk_free:
899 clk_free(&i2c_priv->clk);
900
901 return ret;
902}
903
Simon Glassaad29ae2020-12-03 16:55:21 -0700904static int stm32_of_to_plat(struct udevice *dev)
Patrice Chotardebf442d2017-08-09 14:45:27 +0200905{
Patrick Delaunay58862782021-08-03 12:05:09 +0200906 const struct stm32_i2c_data *data;
Patrice Chotardebf442d2017-08-09 14:45:27 +0200907 struct stm32_i2c_priv *i2c_priv = dev_get_priv(dev);
908 u32 rise_time, fall_time;
Patrick Delaunay4d15c182020-07-06 13:31:35 +0200909 int ret;
Patrice Chotardebf442d2017-08-09 14:45:27 +0200910
Patrick Delaunay58862782021-08-03 12:05:09 +0200911 data = (const struct stm32_i2c_data *)dev_get_driver_data(dev);
912 if (!data)
Patrice Chotardebf442d2017-08-09 14:45:27 +0200913 return -EINVAL;
914
Patrick Delaunay58862782021-08-03 12:05:09 +0200915 rise_time = dev_read_u32_default(dev, "i2c-scl-rising-time-ns",
916 STM32_I2C_RISE_TIME_DEFAULT);
Patrice Chotardebf442d2017-08-09 14:45:27 +0200917
Patrick Delaunay58862782021-08-03 12:05:09 +0200918 fall_time = dev_read_u32_default(dev, "i2c-scl-falling-time-ns",
919 STM32_I2C_FALL_TIME_DEFAULT);
920
921 i2c_priv->setup.dnf = STM32_I2C_DNF_DEFAULT;
922 i2c_priv->setup.analog_filter = STM32_I2C_ANALOG_FILTER_ENABLE;
Patrice Chotardebf442d2017-08-09 14:45:27 +0200923
Patrick Delaunay4d15c182020-07-06 13:31:35 +0200924 /* Optional */
925 i2c_priv->regmap = syscon_regmap_lookup_by_phandle(dev,
926 "st,syscfg-fmp");
927 if (!IS_ERR(i2c_priv->regmap)) {
928 u32 fmp[3];
929
930 ret = dev_read_u32_array(dev, "st,syscfg-fmp", fmp, 3);
931 if (ret)
932 return ret;
933
934 i2c_priv->regmap_sreg = fmp[1];
Patrick Delaunay58862782021-08-03 12:05:09 +0200935 i2c_priv->regmap_creg = fmp[1] + data->fmp_clr_offset;
Patrick Delaunay4d15c182020-07-06 13:31:35 +0200936 i2c_priv->regmap_mask = fmp[2];
937 }
938
Patrice Chotardebf442d2017-08-09 14:45:27 +0200939 return 0;
940}
941
942static const struct dm_i2c_ops stm32_i2c_ops = {
943 .xfer = stm32_i2c_xfer,
944 .set_bus_speed = stm32_i2c_set_bus_speed,
945};
946
947static const struct udevice_id stm32_i2c_of_match[] = {
Patrick Delaunay58862782021-08-03 12:05:09 +0200948 { .compatible = "st,stm32f7-i2c", .data = (ulong)&stm32f7_data },
949 { .compatible = "st,stm32mp15-i2c", .data = (ulong)&stm32mp15_data },
Patrice Chotardebf442d2017-08-09 14:45:27 +0200950 {}
951};
952
953U_BOOT_DRIVER(stm32f7_i2c) = {
954 .name = "stm32f7-i2c",
955 .id = UCLASS_I2C,
956 .of_match = stm32_i2c_of_match,
Simon Glassaad29ae2020-12-03 16:55:21 -0700957 .of_to_plat = stm32_of_to_plat,
Patrice Chotardebf442d2017-08-09 14:45:27 +0200958 .probe = stm32_i2c_probe,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700959 .priv_auto = sizeof(struct stm32_i2c_priv),
Patrice Chotardebf442d2017-08-09 14:45:27 +0200960 .ops = &stm32_i2c_ops,
961};