1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2017 STMicroelectronics
6 #define LOG_CATEGORY UCLASS_I2C
16 #include <dm/device.h>
17 #include <dm/device_compat.h>
18 #include <linux/bitops.h>
19 #include <linux/delay.h>
20 #include <linux/err.h>
23 /* STM32 I2C registers */
24 struct 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 */
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
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)
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)
69 #define STM32_I2C_CR2_SADD10(n) (n & STM32_I2C_CR2_SADD10_MASK)
70 #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)
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 \
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)
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)
106 #define STM32_I2C_MAX_LEN 0xff
108 #define STM32_I2C_DNF_DEFAULT 0
109 #define STM32_I2C_DNF_MAX 16
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 */
115 #define STM32_I2C_RISE_TIME_DEFAULT 25 /* ns */
116 #define STM32_I2C_FALL_TIME_DEFAULT 10 /* ns */
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)
124 #define STM32_NSEC_PER_SEC 1000000000L
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)
140 struct stm32_i2c_spec {
154 * struct stm32_i2c_setup - private I2C timing setup parameters
155 * @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 * @fmp_clr_offset: Fast Mode Plus clear register offset from set register
163 struct stm32_i2c_setup {
174 * struct stm32_i2c_timings - private I2C output parameters
175 * @prec: Prescaler value
176 * @scldel: Data setup time
177 * @sdadel: Data hold time
178 * @sclh: SCL high period (master mode)
179 * @sclh: SCL low period (master mode)
181 struct stm32_i2c_timings {
182 struct list_head node;
191 * struct stm32_i2c_priv - private data of the controller
192 * @regs: I2C registers address
194 * @setup: I2C timing setup parameters
195 * @speed: I2C clock frequency of the controller. Standard, Fast or Fast+
196 * @regmap: holds SYSCFG phandle for Fast Mode Plus bit
197 * @regmap_sreg: register address for setting Fast Mode Plus bits
198 * @regmap_creg: register address for clearing Fast Mode Plus bits
199 * @regmap_mask: mask for Fast Mode Plus bits
201 struct stm32_i2c_priv {
202 struct stm32_i2c_regs *regs;
204 struct stm32_i2c_setup *setup;
206 struct regmap *regmap;
212 static const struct stm32_i2c_spec i2c_specs[] = {
213 /* Standard speed - 100 KHz */
214 [IC_SPEED_MODE_STANDARD] = {
215 .rate = I2C_SPEED_STANDARD_RATE,
226 /* Fast speed - 400 KHz */
227 [IC_SPEED_MODE_FAST] = {
228 .rate = I2C_SPEED_FAST_RATE,
239 /* Fast Plus Speed - 1 MHz */
240 [IC_SPEED_MODE_FAST_PLUS] = {
241 .rate = I2C_SPEED_FAST_PLUS_RATE,
254 static const struct stm32_i2c_setup stm32f7_setup = {
255 .rise_time = STM32_I2C_RISE_TIME_DEFAULT,
256 .fall_time = STM32_I2C_FALL_TIME_DEFAULT,
257 .dnf = STM32_I2C_DNF_DEFAULT,
258 .analog_filter = STM32_I2C_ANALOG_FILTER_ENABLE,
261 static const struct stm32_i2c_setup stm32mp15_setup = {
262 .rise_time = STM32_I2C_RISE_TIME_DEFAULT,
263 .fall_time = STM32_I2C_FALL_TIME_DEFAULT,
264 .dnf = STM32_I2C_DNF_DEFAULT,
265 .analog_filter = STM32_I2C_ANALOG_FILTER_ENABLE,
266 .fmp_clr_offset = 0x40,
269 static int stm32_i2c_check_device_busy(struct stm32_i2c_priv *i2c_priv)
271 struct stm32_i2c_regs *regs = i2c_priv->regs;
272 u32 status = readl(®s->isr);
274 if (status & STM32_I2C_ISR_BUSY)
280 static void stm32_i2c_message_start(struct stm32_i2c_priv *i2c_priv,
281 struct i2c_msg *msg, bool stop)
283 struct stm32_i2c_regs *regs = i2c_priv->regs;
284 u32 cr2 = readl(®s->cr2);
286 /* Set transfer direction */
287 cr2 &= ~STM32_I2C_CR2_RD_WRN;
288 if (msg->flags & I2C_M_RD)
289 cr2 |= STM32_I2C_CR2_RD_WRN;
291 /* Set slave address */
292 cr2 &= ~(STM32_I2C_CR2_HEAD10R | STM32_I2C_CR2_ADD10);
293 if (msg->flags & I2C_M_TEN) {
294 cr2 &= ~STM32_I2C_CR2_SADD10_MASK;
295 cr2 |= STM32_I2C_CR2_SADD10(msg->addr);
296 cr2 |= STM32_I2C_CR2_ADD10;
298 cr2 &= ~STM32_I2C_CR2_SADD7_MASK;
299 cr2 |= STM32_I2C_CR2_SADD7(msg->addr);
302 /* Set nb bytes to transfer and reload or autoend bits */
303 cr2 &= ~(STM32_I2C_CR2_NBYTES_MASK | STM32_I2C_CR2_RELOAD |
304 STM32_I2C_CR2_AUTOEND);
305 if (msg->len > STM32_I2C_MAX_LEN) {
306 cr2 |= STM32_I2C_CR2_NBYTES(STM32_I2C_MAX_LEN);
307 cr2 |= STM32_I2C_CR2_RELOAD;
309 cr2 |= STM32_I2C_CR2_NBYTES(msg->len);
312 /* Write configurations register */
313 writel(cr2, ®s->cr2);
315 /* START/ReSTART generation */
316 setbits_le32(®s->cr2, STM32_I2C_CR2_START);
320 * RELOAD mode must be selected if total number of data bytes to be
321 * sent is greater than MAX_LEN
324 static void stm32_i2c_handle_reload(struct stm32_i2c_priv *i2c_priv,
325 struct i2c_msg *msg, bool stop)
327 struct stm32_i2c_regs *regs = i2c_priv->regs;
328 u32 cr2 = readl(®s->cr2);
330 cr2 &= ~STM32_I2C_CR2_NBYTES_MASK;
332 if (msg->len > STM32_I2C_MAX_LEN) {
333 cr2 |= STM32_I2C_CR2_NBYTES(STM32_I2C_MAX_LEN);
335 cr2 &= ~STM32_I2C_CR2_RELOAD;
336 cr2 |= STM32_I2C_CR2_NBYTES(msg->len);
339 writel(cr2, ®s->cr2);
342 static int stm32_i2c_wait_flags(struct stm32_i2c_priv *i2c_priv,
343 u32 flags, u32 *status)
345 struct stm32_i2c_regs *regs = i2c_priv->regs;
346 u32 time_start = get_timer(0);
348 *status = readl(®s->isr);
349 while (!(*status & flags)) {
350 if (get_timer(time_start) > CONFIG_SYS_HZ) {
351 log_debug("i2c timeout\n");
355 *status = readl(®s->isr);
361 static int stm32_i2c_check_end_of_message(struct stm32_i2c_priv *i2c_priv)
363 struct stm32_i2c_regs *regs = i2c_priv->regs;
364 u32 mask = STM32_I2C_ISR_ERRORS | STM32_I2C_ISR_NACKF |
369 ret = stm32_i2c_wait_flags(i2c_priv, mask, &status);
373 if (status & STM32_I2C_ISR_BERR) {
374 log_debug("Bus error\n");
376 /* Clear BERR flag */
377 setbits_le32(®s->icr, STM32_I2C_ICR_BERRCF);
382 if (status & STM32_I2C_ISR_ARLO) {
383 log_debug("Arbitration lost\n");
385 /* Clear ARLO flag */
386 setbits_le32(®s->icr, STM32_I2C_ICR_ARLOCF);
391 if (status & STM32_I2C_ISR_NACKF) {
392 log_debug("Receive NACK\n");
394 /* Clear NACK flag */
395 setbits_le32(®s->icr, STM32_I2C_ICR_NACKCF);
397 /* Wait until STOPF flag is set */
398 mask = STM32_I2C_ISR_STOPF;
399 ret = stm32_i2c_wait_flags(i2c_priv, mask, &status);
406 if (status & STM32_I2C_ISR_STOPF) {
407 /* Clear STOP flag */
408 setbits_le32(®s->icr, STM32_I2C_ICR_STOPCF);
410 /* Clear control register 2 */
411 setbits_le32(®s->cr2, STM32_I2C_CR2_RESET_MASK);
417 static int stm32_i2c_message_xfer(struct stm32_i2c_priv *i2c_priv,
418 struct i2c_msg *msg, bool stop)
420 struct stm32_i2c_regs *regs = i2c_priv->regs;
422 u32 mask = msg->flags & I2C_M_RD ? STM32_I2C_ISR_RXNE :
423 STM32_I2C_ISR_TXIS | STM32_I2C_ISR_NACKF;
424 int bytes_to_rw = msg->len > STM32_I2C_MAX_LEN ?
425 STM32_I2C_MAX_LEN : msg->len;
429 mask |= STM32_I2C_ISR_ERRORS;
431 stm32_i2c_message_start(i2c_priv, msg, stop);
435 * Wait until TXIS/NACKF/BERR/ARLO flags or
436 * RXNE/BERR/ARLO flags are set
438 ret = stm32_i2c_wait_flags(i2c_priv, mask, &status);
442 if (status & (STM32_I2C_ISR_NACKF | STM32_I2C_ISR_ERRORS))
445 if (status & STM32_I2C_ISR_RXNE) {
446 *msg->buf++ = readb(®s->rxdr);
451 if (status & STM32_I2C_ISR_TXIS) {
452 writeb(*msg->buf++, ®s->txdr);
457 if (!bytes_to_rw && msg->len) {
458 /* Wait until TCR flag is set */
459 mask = STM32_I2C_ISR_TCR;
460 ret = stm32_i2c_wait_flags(i2c_priv, mask, &status);
464 bytes_to_rw = msg->len > STM32_I2C_MAX_LEN ?
465 STM32_I2C_MAX_LEN : msg->len;
466 mask = msg->flags & I2C_M_RD ? STM32_I2C_ISR_RXNE :
467 STM32_I2C_ISR_TXIS | STM32_I2C_ISR_NACKF;
469 stm32_i2c_handle_reload(i2c_priv, msg, stop);
470 } else if (!bytes_to_rw) {
471 /* Wait until TC flag is set */
472 mask = STM32_I2C_ISR_TC;
473 ret = stm32_i2c_wait_flags(i2c_priv, mask, &status);
478 /* Message sent, new message has to be sent */
483 /* End of transfer, send stop condition */
484 mask = STM32_I2C_CR2_STOP;
485 setbits_le32(®s->cr2, mask);
487 return stm32_i2c_check_end_of_message(i2c_priv);
490 static int stm32_i2c_xfer(struct udevice *bus, struct i2c_msg *msg,
493 struct stm32_i2c_priv *i2c_priv = dev_get_priv(bus);
496 ret = stm32_i2c_check_device_busy(i2c_priv);
500 for (; nmsgs > 0; nmsgs--, msg++) {
501 ret = stm32_i2c_message_xfer(i2c_priv, msg, nmsgs == 1);
509 static int stm32_i2c_compute_solutions(struct stm32_i2c_setup *setup,
510 const struct stm32_i2c_spec *specs,
511 struct list_head *solutions)
513 struct stm32_i2c_timings *v;
514 u32 p_prev = STM32_PRESC_MAX;
515 u32 i2cclk = DIV_ROUND_CLOSEST(STM32_NSEC_PER_SEC,
517 u32 af_delay_min, af_delay_max;
519 int sdadel_min, sdadel_max, scldel_min;
522 af_delay_min = setup->analog_filter ?
523 STM32_I2C_ANALOG_FILTER_DELAY_MIN : 0;
524 af_delay_max = setup->analog_filter ?
525 STM32_I2C_ANALOG_FILTER_DELAY_MAX : 0;
527 sdadel_min = specs->hddat_min + setup->fall_time -
528 af_delay_min - (setup->dnf + 3) * i2cclk;
530 sdadel_max = specs->vddat_max - setup->rise_time -
531 af_delay_max - (setup->dnf + 4) * i2cclk;
533 scldel_min = setup->rise_time + specs->sudat_min;
540 log_debug("SDADEL(min/max): %i/%i, SCLDEL(Min): %i\n",
541 sdadel_min, sdadel_max, scldel_min);
543 /* Compute possible values for PRESC, SCLDEL and SDADEL */
544 for (p = 0; p < STM32_PRESC_MAX; p++) {
545 for (l = 0; l < STM32_SCLDEL_MAX; l++) {
546 int scldel = (l + 1) * (p + 1) * i2cclk;
548 if (scldel < scldel_min)
551 for (a = 0; a < STM32_SDADEL_MAX; a++) {
552 int sdadel = (a * (p + 1) + 1) * i2cclk;
554 if (((sdadel >= sdadel_min) &&
555 (sdadel <= sdadel_max)) &&
557 v = calloc(1, sizeof(*v));
566 list_add_tail(&v->node, solutions);
576 if (list_empty(solutions)) {
577 log_err("no Prescaler solution\n");
584 static int stm32_i2c_choose_solution(struct stm32_i2c_setup *setup,
585 const struct stm32_i2c_spec *specs,
586 struct list_head *solutions,
587 struct stm32_i2c_timings *s)
589 struct stm32_i2c_timings *v;
590 u32 i2cbus = DIV_ROUND_CLOSEST(STM32_NSEC_PER_SEC,
592 u32 clk_error_prev = i2cbus;
593 u32 i2cclk = DIV_ROUND_CLOSEST(STM32_NSEC_PER_SEC,
595 u32 clk_min, clk_max;
600 bool sol_found = false;
603 af_delay_min = setup->analog_filter ?
604 STM32_I2C_ANALOG_FILTER_DELAY_MIN : 0;
605 dnf_delay = setup->dnf * i2cclk;
607 tsync = af_delay_min + dnf_delay + (2 * i2cclk);
608 clk_max = STM32_NSEC_PER_SEC / specs->rate_min;
609 clk_min = STM32_NSEC_PER_SEC / specs->rate_max;
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
621 list_for_each_entry(v, solutions, node) {
622 u32 prescaler = (v->presc + 1) * i2cclk;
624 for (l = 0; l < STM32_SCLL_MAX; l++) {
625 u32 tscl_l = (l + 1) * prescaler + tsync;
627 if (tscl_l < specs->l_min ||
629 ((tscl_l - af_delay_min - dnf_delay) / 4))) {
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;
638 if ((tscl >= clk_min) && (tscl <= clk_max) &&
639 (tscl_h >= specs->h_min) &&
644 clk_error = tscl - i2cbus;
646 clk_error = i2cbus - tscl;
648 if (clk_error < clk_error_prev) {
649 clk_error_prev = clk_error;
653 memcpy(s, v, sizeof(*s));
661 log_err("no solution at all\n");
668 static const struct stm32_i2c_spec *get_specs(u32 rate)
672 for (i = 0; i < ARRAY_SIZE(i2c_specs); i++)
673 if (rate <= i2c_specs[i].rate)
674 return &i2c_specs[i];
677 return ERR_PTR(-EINVAL);
680 static int stm32_i2c_compute_timing(struct stm32_i2c_priv *i2c_priv,
681 struct stm32_i2c_setup *setup,
682 struct stm32_i2c_timings *output)
684 const struct stm32_i2c_spec *specs;
685 struct stm32_i2c_timings *v, *_v;
686 struct list_head solutions;
689 specs = get_specs(setup->speed_freq);
690 if (specs == ERR_PTR(-EINVAL)) {
691 log_err("speed out of bound {%d}\n",
696 if (setup->rise_time > specs->rise_max ||
697 setup->fall_time > specs->fall_max) {
698 log_err("timings out of bound Rise{%d>%d}/Fall{%d>%d}\n",
699 setup->rise_time, specs->rise_max,
700 setup->fall_time, specs->fall_max);
704 if (setup->dnf > STM32_I2C_DNF_MAX) {
705 log_err("DNF out of bound %d/%d\n",
706 setup->dnf, STM32_I2C_DNF_MAX);
710 INIT_LIST_HEAD(&solutions);
711 ret = stm32_i2c_compute_solutions(setup, specs, &solutions);
715 ret = stm32_i2c_choose_solution(setup, specs, &solutions, output);
719 log_debug("Presc: %i, scldel: %i, sdadel: %i, scll: %i, sclh: %i\n",
721 output->scldel, output->sdadel,
722 output->scll, output->sclh);
725 /* Release list and memory */
726 list_for_each_entry_safe(v, _v, &solutions, node) {
734 static u32 get_lower_rate(u32 rate)
738 for (i = ARRAY_SIZE(i2c_specs) - 1; i >= 0; i--)
739 if (rate > i2c_specs[i].rate)
740 return i2c_specs[i].rate;
742 return i2c_specs[0].rate;
745 static int stm32_i2c_setup_timing(struct stm32_i2c_priv *i2c_priv,
746 struct stm32_i2c_timings *timing)
748 struct stm32_i2c_setup *setup = i2c_priv->setup;
751 setup->speed_freq = i2c_priv->speed;
752 setup->clock_src = clk_get_rate(&i2c_priv->clk);
754 if (!setup->clock_src) {
755 log_err("clock rate is 0\n");
760 ret = stm32_i2c_compute_timing(i2c_priv, setup, timing);
762 log_debug("failed to compute I2C timings.\n");
763 if (setup->speed_freq > I2C_SPEED_STANDARD_RATE) {
765 get_lower_rate(setup->speed_freq);
766 log_debug("downgrade I2C Speed Freq to (%i)\n",
775 log_err("impossible to compute I2C timings.\n");
779 log_debug("I2C Freq(%i), Clk Source(%i)\n",
780 setup->speed_freq, setup->clock_src);
781 log_debug("I2C Rise(%i) and Fall(%i) Time\n",
782 setup->rise_time, setup->fall_time);
783 log_debug("I2C Analog Filter(%s), DNF(%i)\n",
784 setup->analog_filter ? "On" : "Off", setup->dnf);
786 i2c_priv->speed = setup->speed_freq;
791 static int stm32_i2c_write_fm_plus_bits(struct stm32_i2c_priv *i2c_priv)
794 bool enable = i2c_priv->speed > I2C_SPEED_FAST_RATE;
797 if (IS_ERR_OR_NULL(i2c_priv->regmap))
800 if (i2c_priv->regmap_sreg == i2c_priv->regmap_creg)
801 ret = regmap_update_bits(i2c_priv->regmap,
802 i2c_priv->regmap_sreg,
803 i2c_priv->regmap_mask,
804 enable ? i2c_priv->regmap_mask : 0);
806 ret = regmap_write(i2c_priv->regmap,
807 enable ? i2c_priv->regmap_sreg :
808 i2c_priv->regmap_creg,
809 i2c_priv->regmap_mask);
814 static int stm32_i2c_hw_config(struct stm32_i2c_priv *i2c_priv)
816 struct stm32_i2c_regs *regs = i2c_priv->regs;
817 struct stm32_i2c_timings t;
821 ret = stm32_i2c_setup_timing(i2c_priv, &t);
826 clrbits_le32(®s->cr1, STM32_I2C_CR1_PE);
828 /* Setup Fast mode plus if necessary */
829 ret = stm32_i2c_write_fm_plus_bits(i2c_priv);
833 /* Timing settings */
834 timing |= STM32_I2C_TIMINGR_PRESC(t.presc);
835 timing |= STM32_I2C_TIMINGR_SCLDEL(t.scldel);
836 timing |= STM32_I2C_TIMINGR_SDADEL(t.sdadel);
837 timing |= STM32_I2C_TIMINGR_SCLH(t.sclh);
838 timing |= STM32_I2C_TIMINGR_SCLL(t.scll);
839 writel(timing, ®s->timingr);
842 if (i2c_priv->setup->analog_filter)
843 clrbits_le32(®s->cr1, STM32_I2C_CR1_ANFOFF);
845 setbits_le32(®s->cr1, STM32_I2C_CR1_ANFOFF);
846 setbits_le32(®s->cr1, STM32_I2C_CR1_PE);
851 static int stm32_i2c_set_bus_speed(struct udevice *dev, unsigned int speed)
853 struct stm32_i2c_priv *i2c_priv = dev_get_priv(dev);
855 if (speed > I2C_SPEED_FAST_PLUS_RATE) {
856 dev_dbg(dev, "Speed %d not supported\n", speed);
860 i2c_priv->speed = speed;
862 return stm32_i2c_hw_config(i2c_priv);
865 static int stm32_i2c_probe(struct udevice *dev)
867 struct stm32_i2c_priv *i2c_priv = dev_get_priv(dev);
868 struct reset_ctl reset_ctl;
872 addr = dev_read_addr(dev);
873 if (addr == FDT_ADDR_T_NONE)
876 i2c_priv->regs = (struct stm32_i2c_regs *)addr;
878 ret = clk_get_by_index(dev, 0, &i2c_priv->clk);
882 ret = clk_enable(&i2c_priv->clk);
886 ret = reset_get_by_index(dev, 0, &reset_ctl);
890 reset_assert(&reset_ctl);
892 reset_deassert(&reset_ctl);
897 clk_disable(&i2c_priv->clk);
899 clk_free(&i2c_priv->clk);
904 static int stm32_of_to_plat(struct udevice *dev)
906 struct stm32_i2c_priv *i2c_priv = dev_get_priv(dev);
907 u32 rise_time, fall_time;
910 i2c_priv->setup = (struct stm32_i2c_setup *)dev_get_driver_data(dev);
911 if (!i2c_priv->setup)
914 rise_time = dev_read_u32_default(dev, "i2c-scl-rising-time-ns", 0);
916 i2c_priv->setup->rise_time = rise_time;
918 fall_time = dev_read_u32_default(dev, "i2c-scl-falling-time-ns", 0);
920 i2c_priv->setup->fall_time = fall_time;
923 i2c_priv->regmap = syscon_regmap_lookup_by_phandle(dev,
925 if (!IS_ERR(i2c_priv->regmap)) {
928 ret = dev_read_u32_array(dev, "st,syscfg-fmp", fmp, 3);
932 i2c_priv->regmap_sreg = fmp[1];
933 i2c_priv->regmap_creg = fmp[1] +
934 i2c_priv->setup->fmp_clr_offset;
935 i2c_priv->regmap_mask = fmp[2];
941 static const struct dm_i2c_ops stm32_i2c_ops = {
942 .xfer = stm32_i2c_xfer,
943 .set_bus_speed = stm32_i2c_set_bus_speed,
946 static const struct udevice_id stm32_i2c_of_match[] = {
947 { .compatible = "st,stm32f7-i2c", .data = (ulong)&stm32f7_setup },
948 { .compatible = "st,stm32mp15-i2c", .data = (ulong)&stm32mp15_setup },
952 U_BOOT_DRIVER(stm32f7_i2c) = {
953 .name = "stm32f7-i2c",
955 .of_match = stm32_i2c_of_match,
956 .of_to_plat = stm32_of_to_plat,
957 .probe = stm32_i2c_probe,
958 .priv_auto = sizeof(struct stm32_i2c_priv),
959 .ops = &stm32_i2c_ops,