blob: fc5c1221e1e1cff595e1c0599251136fd3c302e3 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Patrice Chotard4fadcaf2017-08-09 14:45:27 +02002/*
3 * (C) Copyright 2017 STMicroelectronics
Patrice Chotard4fadcaf2017-08-09 14:45:27 +02004 */
5
6#include <common.h>
7#include <clk.h>
8#include <dm.h>
9#include <i2c.h>
10#include <reset.h>
11
12#include <dm/device.h>
Alain Volmatc3244652020-03-06 11:09:14 +010013#include <linux/err.h>
Patrice Chotard4fadcaf2017-08-09 14:45:27 +020014#include <linux/io.h>
15
16/* STM32 I2C registers */
17struct stm32_i2c_regs {
18 u32 cr1; /* I2C control register 1 */
19 u32 cr2; /* I2C control register 2 */
20 u32 oar1; /* I2C own address 1 register */
21 u32 oar2; /* I2C own address 2 register */
22 u32 timingr; /* I2C timing register */
23 u32 timeoutr; /* I2C timeout register */
24 u32 isr; /* I2C interrupt and status register */
25 u32 icr; /* I2C interrupt clear register */
26 u32 pecr; /* I2C packet error checking register */
27 u32 rxdr; /* I2C receive data register */
28 u32 txdr; /* I2C transmit data register */
29};
30
31#define STM32_I2C_CR1 0x00
32#define STM32_I2C_CR2 0x04
33#define STM32_I2C_TIMINGR 0x10
34#define STM32_I2C_ISR 0x18
35#define STM32_I2C_ICR 0x1C
36#define STM32_I2C_RXDR 0x24
37#define STM32_I2C_TXDR 0x28
38
39/* STM32 I2C control 1 */
40#define STM32_I2C_CR1_ANFOFF BIT(12)
41#define STM32_I2C_CR1_ERRIE BIT(7)
42#define STM32_I2C_CR1_TCIE BIT(6)
43#define STM32_I2C_CR1_STOPIE BIT(5)
44#define STM32_I2C_CR1_NACKIE BIT(4)
45#define STM32_I2C_CR1_ADDRIE BIT(3)
46#define STM32_I2C_CR1_RXIE BIT(2)
47#define STM32_I2C_CR1_TXIE BIT(1)
48#define STM32_I2C_CR1_PE BIT(0)
49
50/* STM32 I2C control 2 */
51#define STM32_I2C_CR2_AUTOEND BIT(25)
52#define STM32_I2C_CR2_RELOAD BIT(24)
53#define STM32_I2C_CR2_NBYTES_MASK GENMASK(23, 16)
54#define STM32_I2C_CR2_NBYTES(n) ((n & 0xff) << 16)
55#define STM32_I2C_CR2_NACK BIT(15)
56#define STM32_I2C_CR2_STOP BIT(14)
57#define STM32_I2C_CR2_START BIT(13)
58#define STM32_I2C_CR2_HEAD10R BIT(12)
59#define STM32_I2C_CR2_ADD10 BIT(11)
60#define STM32_I2C_CR2_RD_WRN BIT(10)
61#define STM32_I2C_CR2_SADD10_MASK GENMASK(9, 0)
Patrick Delaunayc0765f42018-10-29 15:31:55 +010062#define STM32_I2C_CR2_SADD10(n) (n & STM32_I2C_CR2_SADD10_MASK)
Patrice Chotard4fadcaf2017-08-09 14:45:27 +020063#define STM32_I2C_CR2_SADD7_MASK GENMASK(7, 1)
64#define STM32_I2C_CR2_SADD7(n) ((n & 0x7f) << 1)
65#define STM32_I2C_CR2_RESET_MASK (STM32_I2C_CR2_HEAD10R \
66 | STM32_I2C_CR2_NBYTES_MASK \
67 | STM32_I2C_CR2_SADD7_MASK \
68 | STM32_I2C_CR2_RELOAD \
69 | STM32_I2C_CR2_RD_WRN)
70
71/* STM32 I2C Interrupt Status */
72#define STM32_I2C_ISR_BUSY BIT(15)
73#define STM32_I2C_ISR_ARLO BIT(9)
74#define STM32_I2C_ISR_BERR BIT(8)
75#define STM32_I2C_ISR_TCR BIT(7)
76#define STM32_I2C_ISR_TC BIT(6)
77#define STM32_I2C_ISR_STOPF BIT(5)
78#define STM32_I2C_ISR_NACKF BIT(4)
79#define STM32_I2C_ISR_ADDR BIT(3)
80#define STM32_I2C_ISR_RXNE BIT(2)
81#define STM32_I2C_ISR_TXIS BIT(1)
82#define STM32_I2C_ISR_TXE BIT(0)
83#define STM32_I2C_ISR_ERRORS (STM32_I2C_ISR_BERR \
84 | STM32_I2C_ISR_ARLO)
85
86/* STM32 I2C Interrupt Clear */
87#define STM32_I2C_ICR_ARLOCF BIT(9)
88#define STM32_I2C_ICR_BERRCF BIT(8)
89#define STM32_I2C_ICR_STOPCF BIT(5)
90#define STM32_I2C_ICR_NACKCF BIT(4)
91
92/* STM32 I2C Timing */
93#define STM32_I2C_TIMINGR_PRESC(n) ((n & 0xf) << 28)
94#define STM32_I2C_TIMINGR_SCLDEL(n) ((n & 0xf) << 20)
95#define STM32_I2C_TIMINGR_SDADEL(n) ((n & 0xf) << 16)
96#define STM32_I2C_TIMINGR_SCLH(n) ((n & 0xff) << 8)
97#define STM32_I2C_TIMINGR_SCLL(n) (n & 0xff)
98
99#define STM32_I2C_MAX_LEN 0xff
100
101#define STM32_I2C_DNF_DEFAULT 0
102#define STM32_I2C_DNF_MAX 16
103
104#define STM32_I2C_ANALOG_FILTER_ENABLE 1
105#define STM32_I2C_ANALOG_FILTER_DELAY_MIN 50 /* ns */
106#define STM32_I2C_ANALOG_FILTER_DELAY_MAX 260 /* ns */
107
108#define STM32_I2C_RISE_TIME_DEFAULT 25 /* ns */
109#define STM32_I2C_FALL_TIME_DEFAULT 10 /* ns */
110
111#define STM32_PRESC_MAX BIT(4)
112#define STM32_SCLDEL_MAX BIT(4)
113#define STM32_SDADEL_MAX BIT(4)
114#define STM32_SCLH_MAX BIT(8)
115#define STM32_SCLL_MAX BIT(8)
116
117#define STM32_NSEC_PER_SEC 1000000000L
118
Patrice Chotard4fadcaf2017-08-09 14:45:27 +0200119/**
120 * struct stm32_i2c_spec - private i2c specification timing
121 * @rate: I2C bus speed (Hz)
122 * @rate_min: 80% of I2C bus speed (Hz)
123 * @rate_max: 120% of I2C bus speed (Hz)
124 * @fall_max: Max fall time of both SDA and SCL signals (ns)
125 * @rise_max: Max rise time of both SDA and SCL signals (ns)
126 * @hddat_min: Min data hold time (ns)
127 * @vddat_max: Max data valid time (ns)
128 * @sudat_min: Min data setup time (ns)
129 * @l_min: Min low period of the SCL clock (ns)
130 * @h_min: Min high period of the SCL clock (ns)
131 */
132
133struct stm32_i2c_spec {
134 u32 rate;
135 u32 rate_min;
136 u32 rate_max;
137 u32 fall_max;
138 u32 rise_max;
139 u32 hddat_min;
140 u32 vddat_max;
141 u32 sudat_min;
142 u32 l_min;
143 u32 h_min;
144};
145
146/**
147 * struct stm32_i2c_setup - private I2C timing setup parameters
Patrice Chotard4fadcaf2017-08-09 14:45:27 +0200148 * @speed_freq: I2C speed frequency (Hz)
149 * @clock_src: I2C clock source frequency (Hz)
150 * @rise_time: Rise time (ns)
151 * @fall_time: Fall time (ns)
152 * @dnf: Digital filter coefficient (0-16)
153 * @analog_filter: Analog filter delay (On/Off)
154 */
155struct stm32_i2c_setup {
Patrice Chotard4fadcaf2017-08-09 14:45:27 +0200156 u32 speed_freq;
157 u32 clock_src;
158 u32 rise_time;
159 u32 fall_time;
160 u8 dnf;
161 bool analog_filter;
162};
163
164/**
165 * struct stm32_i2c_timings - private I2C output parameters
166 * @prec: Prescaler value
167 * @scldel: Data setup time
168 * @sdadel: Data hold time
169 * @sclh: SCL high period (master mode)
170 * @sclh: SCL low period (master mode)
171 */
172struct stm32_i2c_timings {
173 struct list_head node;
174 u8 presc;
175 u8 scldel;
176 u8 sdadel;
177 u8 sclh;
178 u8 scll;
179};
180
181struct stm32_i2c_priv {
182 struct stm32_i2c_regs *regs;
183 struct clk clk;
184 struct stm32_i2c_setup *setup;
Alain Volmatc3244652020-03-06 11:09:14 +0100185 u32 speed;
Patrice Chotard4fadcaf2017-08-09 14:45:27 +0200186};
187
Patrick Delaunayc235b082018-10-29 15:31:56 +0100188static const struct stm32_i2c_spec i2c_specs[] = {
Alain Volmatc3244652020-03-06 11:09:14 +0100189 /* Standard speed - 100 KHz */
Simon Glassb0a22d02020-01-23 11:48:21 -0700190 [IC_SPEED_MODE_STANDARD] = {
191 .rate = I2C_SPEED_STANDARD_RATE,
Patrice Chotard4fadcaf2017-08-09 14:45:27 +0200192 .rate_min = 8000,
193 .rate_max = 120000,
194 .fall_max = 300,
195 .rise_max = 1000,
196 .hddat_min = 0,
197 .vddat_max = 3450,
198 .sudat_min = 250,
199 .l_min = 4700,
200 .h_min = 4000,
201 },
Alain Volmatc3244652020-03-06 11:09:14 +0100202 /* Fast speed - 400 KHz */
Simon Glassb0a22d02020-01-23 11:48:21 -0700203 [IC_SPEED_MODE_FAST] = {
204 .rate = I2C_SPEED_FAST_RATE,
Patrice Chotard4fadcaf2017-08-09 14:45:27 +0200205 .rate_min = 320000,
206 .rate_max = 480000,
207 .fall_max = 300,
208 .rise_max = 300,
209 .hddat_min = 0,
210 .vddat_max = 900,
211 .sudat_min = 100,
212 .l_min = 1300,
213 .h_min = 600,
214 },
Alain Volmatc3244652020-03-06 11:09:14 +0100215 /* Fast Plus Speed - 1 MHz */
Simon Glassb0a22d02020-01-23 11:48:21 -0700216 [IC_SPEED_MODE_FAST_PLUS] = {
217 .rate = I2C_SPEED_FAST_PLUS_RATE,
Patrice Chotard4fadcaf2017-08-09 14:45:27 +0200218 .rate_min = 800000,
219 .rate_max = 1200000,
220 .fall_max = 100,
221 .rise_max = 120,
222 .hddat_min = 0,
223 .vddat_max = 450,
224 .sudat_min = 50,
225 .l_min = 500,
226 .h_min = 260,
227 },
228};
229
Patrick Delaunayc235b082018-10-29 15:31:56 +0100230static const struct stm32_i2c_setup stm32f7_setup = {
Patrice Chotard4fadcaf2017-08-09 14:45:27 +0200231 .rise_time = STM32_I2C_RISE_TIME_DEFAULT,
232 .fall_time = STM32_I2C_FALL_TIME_DEFAULT,
233 .dnf = STM32_I2C_DNF_DEFAULT,
234 .analog_filter = STM32_I2C_ANALOG_FILTER_ENABLE,
235};
236
Patrice Chotard4fadcaf2017-08-09 14:45:27 +0200237static int stm32_i2c_check_device_busy(struct stm32_i2c_priv *i2c_priv)
238{
239 struct stm32_i2c_regs *regs = i2c_priv->regs;
240 u32 status = readl(&regs->isr);
241
242 if (status & STM32_I2C_ISR_BUSY)
243 return -EBUSY;
244
245 return 0;
246}
247
248static void stm32_i2c_message_start(struct stm32_i2c_priv *i2c_priv,
Patrick Delaunayc0765f42018-10-29 15:31:55 +0100249 struct i2c_msg *msg, bool stop)
Patrice Chotard4fadcaf2017-08-09 14:45:27 +0200250{
251 struct stm32_i2c_regs *regs = i2c_priv->regs;
252 u32 cr2 = readl(&regs->cr2);
253
254 /* Set transfer direction */
255 cr2 &= ~STM32_I2C_CR2_RD_WRN;
256 if (msg->flags & I2C_M_RD)
257 cr2 |= STM32_I2C_CR2_RD_WRN;
258
259 /* Set slave address */
260 cr2 &= ~(STM32_I2C_CR2_HEAD10R | STM32_I2C_CR2_ADD10);
261 if (msg->flags & I2C_M_TEN) {
262 cr2 &= ~STM32_I2C_CR2_SADD10_MASK;
263 cr2 |= STM32_I2C_CR2_SADD10(msg->addr);
264 cr2 |= STM32_I2C_CR2_ADD10;
265 } else {
266 cr2 &= ~STM32_I2C_CR2_SADD7_MASK;
267 cr2 |= STM32_I2C_CR2_SADD7(msg->addr);
268 }
269
270 /* Set nb bytes to transfer and reload or autoend bits */
271 cr2 &= ~(STM32_I2C_CR2_NBYTES_MASK | STM32_I2C_CR2_RELOAD |
272 STM32_I2C_CR2_AUTOEND);
273 if (msg->len > STM32_I2C_MAX_LEN) {
274 cr2 |= STM32_I2C_CR2_NBYTES(STM32_I2C_MAX_LEN);
275 cr2 |= STM32_I2C_CR2_RELOAD;
276 } else {
277 cr2 |= STM32_I2C_CR2_NBYTES(msg->len);
278 }
279
280 /* Write configurations register */
281 writel(cr2, &regs->cr2);
282
283 /* START/ReSTART generation */
284 setbits_le32(&regs->cr2, STM32_I2C_CR2_START);
285}
286
287/*
288 * RELOAD mode must be selected if total number of data bytes to be
289 * sent is greater than MAX_LEN
290 */
291
292static void stm32_i2c_handle_reload(struct stm32_i2c_priv *i2c_priv,
Patrick Delaunayc0765f42018-10-29 15:31:55 +0100293 struct i2c_msg *msg, bool stop)
Patrice Chotard4fadcaf2017-08-09 14:45:27 +0200294{
295 struct stm32_i2c_regs *regs = i2c_priv->regs;
296 u32 cr2 = readl(&regs->cr2);
297
298 cr2 &= ~STM32_I2C_CR2_NBYTES_MASK;
299
300 if (msg->len > STM32_I2C_MAX_LEN) {
301 cr2 |= STM32_I2C_CR2_NBYTES(STM32_I2C_MAX_LEN);
302 } else {
303 cr2 &= ~STM32_I2C_CR2_RELOAD;
304 cr2 |= STM32_I2C_CR2_NBYTES(msg->len);
305 }
306
307 writel(cr2, &regs->cr2);
308}
309
310static int stm32_i2c_wait_flags(struct stm32_i2c_priv *i2c_priv,
Patrick Delaunayc0765f42018-10-29 15:31:55 +0100311 u32 flags, u32 *status)
Patrice Chotard4fadcaf2017-08-09 14:45:27 +0200312{
313 struct stm32_i2c_regs *regs = i2c_priv->regs;
314 u32 time_start = get_timer(0);
315
316 *status = readl(&regs->isr);
317 while (!(*status & flags)) {
318 if (get_timer(time_start) > CONFIG_SYS_HZ) {
319 debug("%s: i2c timeout\n", __func__);
320 return -ETIMEDOUT;
321 }
322
323 *status = readl(&regs->isr);
324 }
325
326 return 0;
327}
328
329static int stm32_i2c_check_end_of_message(struct stm32_i2c_priv *i2c_priv)
330{
331 struct stm32_i2c_regs *regs = i2c_priv->regs;
332 u32 mask = STM32_I2C_ISR_ERRORS | STM32_I2C_ISR_NACKF |
333 STM32_I2C_ISR_STOPF;
334 u32 status;
335 int ret;
336
337 ret = stm32_i2c_wait_flags(i2c_priv, mask, &status);
338 if (ret)
339 return ret;
340
341 if (status & STM32_I2C_ISR_BERR) {
342 debug("%s: Bus error\n", __func__);
343
344 /* Clear BERR flag */
345 setbits_le32(&regs->icr, STM32_I2C_ICR_BERRCF);
346
347 return -EIO;
348 }
349
350 if (status & STM32_I2C_ISR_ARLO) {
351 debug("%s: Arbitration lost\n", __func__);
352
353 /* Clear ARLO flag */
354 setbits_le32(&regs->icr, STM32_I2C_ICR_ARLOCF);
355
356 return -EAGAIN;
357 }
358
359 if (status & STM32_I2C_ISR_NACKF) {
360 debug("%s: Receive NACK\n", __func__);
361
362 /* Clear NACK flag */
363 setbits_le32(&regs->icr, STM32_I2C_ICR_NACKCF);
364
365 /* Wait until STOPF flag is set */
366 mask = STM32_I2C_ISR_STOPF;
367 ret = stm32_i2c_wait_flags(i2c_priv, mask, &status);
368 if (ret)
369 return ret;
370
371 ret = -EIO;
372 }
373
374 if (status & STM32_I2C_ISR_STOPF) {
375 /* Clear STOP flag */
376 setbits_le32(&regs->icr, STM32_I2C_ICR_STOPCF);
377
378 /* Clear control register 2 */
379 setbits_le32(&regs->cr2, STM32_I2C_CR2_RESET_MASK);
380 }
381
382 return ret;
383}
384
385static int stm32_i2c_message_xfer(struct stm32_i2c_priv *i2c_priv,
Patrick Delaunayc0765f42018-10-29 15:31:55 +0100386 struct i2c_msg *msg, bool stop)
Patrice Chotard4fadcaf2017-08-09 14:45:27 +0200387{
388 struct stm32_i2c_regs *regs = i2c_priv->regs;
389 u32 status;
390 u32 mask = msg->flags & I2C_M_RD ? STM32_I2C_ISR_RXNE :
391 STM32_I2C_ISR_TXIS | STM32_I2C_ISR_NACKF;
392 int bytes_to_rw = msg->len > STM32_I2C_MAX_LEN ?
393 STM32_I2C_MAX_LEN : msg->len;
394 int ret = 0;
395
396 /* Add errors */
397 mask |= STM32_I2C_ISR_ERRORS;
398
399 stm32_i2c_message_start(i2c_priv, msg, stop);
400
401 while (msg->len) {
402 /*
403 * Wait until TXIS/NACKF/BERR/ARLO flags or
404 * RXNE/BERR/ARLO flags are set
405 */
406 ret = stm32_i2c_wait_flags(i2c_priv, mask, &status);
407 if (ret)
408 break;
409
410 if (status & (STM32_I2C_ISR_NACKF | STM32_I2C_ISR_ERRORS))
411 break;
412
413 if (status & STM32_I2C_ISR_RXNE) {
414 *msg->buf++ = readb(&regs->rxdr);
415 msg->len--;
416 bytes_to_rw--;
417 }
418
419 if (status & STM32_I2C_ISR_TXIS) {
420 writeb(*msg->buf++, &regs->txdr);
421 msg->len--;
422 bytes_to_rw--;
423 }
424
425 if (!bytes_to_rw && msg->len) {
426 /* Wait until TCR flag is set */
427 mask = STM32_I2C_ISR_TCR;
428 ret = stm32_i2c_wait_flags(i2c_priv, mask, &status);
429 if (ret)
430 break;
431
432 bytes_to_rw = msg->len > STM32_I2C_MAX_LEN ?
433 STM32_I2C_MAX_LEN : msg->len;
434 mask = msg->flags & I2C_M_RD ? STM32_I2C_ISR_RXNE :
435 STM32_I2C_ISR_TXIS | STM32_I2C_ISR_NACKF;
436
437 stm32_i2c_handle_reload(i2c_priv, msg, stop);
438 } else if (!bytes_to_rw) {
439 /* Wait until TC flag is set */
440 mask = STM32_I2C_ISR_TC;
441 ret = stm32_i2c_wait_flags(i2c_priv, mask, &status);
442 if (ret)
443 break;
444
445 if (!stop)
446 /* Message sent, new message has to be sent */
447 return 0;
448 }
449 }
450
451 /* End of transfer, send stop condition */
452 mask = STM32_I2C_CR2_STOP;
453 setbits_le32(&regs->cr2, mask);
454
455 return stm32_i2c_check_end_of_message(i2c_priv);
456}
457
458static int stm32_i2c_xfer(struct udevice *bus, struct i2c_msg *msg,
Patrick Delaunayc0765f42018-10-29 15:31:55 +0100459 int nmsgs)
Patrice Chotard4fadcaf2017-08-09 14:45:27 +0200460{
461 struct stm32_i2c_priv *i2c_priv = dev_get_priv(bus);
462 int ret;
463
464 ret = stm32_i2c_check_device_busy(i2c_priv);
465 if (ret)
466 return ret;
467
468 for (; nmsgs > 0; nmsgs--, msg++) {
469 ret = stm32_i2c_message_xfer(i2c_priv, msg, nmsgs == 1);
470 if (ret)
471 return ret;
472 }
473
474 return 0;
475}
476
477static int stm32_i2c_compute_solutions(struct stm32_i2c_setup *setup,
Alain Volmatc3244652020-03-06 11:09:14 +0100478 const struct stm32_i2c_spec *specs,
Patrice Chotard4fadcaf2017-08-09 14:45:27 +0200479 struct list_head *solutions)
480{
481 struct stm32_i2c_timings *v;
482 u32 p_prev = STM32_PRESC_MAX;
483 u32 i2cclk = DIV_ROUND_CLOSEST(STM32_NSEC_PER_SEC,
484 setup->clock_src);
485 u32 af_delay_min, af_delay_max;
486 u16 p, l, a;
487 int sdadel_min, sdadel_max, scldel_min;
488 int ret = 0;
489
490 af_delay_min = setup->analog_filter ?
491 STM32_I2C_ANALOG_FILTER_DELAY_MIN : 0;
492 af_delay_max = setup->analog_filter ?
493 STM32_I2C_ANALOG_FILTER_DELAY_MAX : 0;
494
Alain Volmatc3244652020-03-06 11:09:14 +0100495 sdadel_min = specs->hddat_min + setup->fall_time -
Patrice Chotard4fadcaf2017-08-09 14:45:27 +0200496 af_delay_min - (setup->dnf + 3) * i2cclk;
497
Alain Volmatc3244652020-03-06 11:09:14 +0100498 sdadel_max = specs->vddat_max - setup->rise_time -
Patrice Chotard4fadcaf2017-08-09 14:45:27 +0200499 af_delay_max - (setup->dnf + 4) * i2cclk;
500
Alain Volmatc3244652020-03-06 11:09:14 +0100501 scldel_min = setup->rise_time + specs->sudat_min;
Patrice Chotard4fadcaf2017-08-09 14:45:27 +0200502
503 if (sdadel_min < 0)
504 sdadel_min = 0;
505 if (sdadel_max < 0)
506 sdadel_max = 0;
507
508 debug("%s: SDADEL(min/max): %i/%i, SCLDEL(Min): %i\n", __func__,
509 sdadel_min, sdadel_max, scldel_min);
510
511 /* Compute possible values for PRESC, SCLDEL and SDADEL */
512 for (p = 0; p < STM32_PRESC_MAX; p++) {
513 for (l = 0; l < STM32_SCLDEL_MAX; l++) {
Patrick Delaunay499504b2019-06-21 15:26:47 +0200514 int scldel = (l + 1) * (p + 1) * i2cclk;
Patrice Chotard4fadcaf2017-08-09 14:45:27 +0200515
516 if (scldel < scldel_min)
517 continue;
518
519 for (a = 0; a < STM32_SDADEL_MAX; a++) {
Patrick Delaunay499504b2019-06-21 15:26:47 +0200520 int sdadel = (a * (p + 1) + 1) * i2cclk;
Patrice Chotard4fadcaf2017-08-09 14:45:27 +0200521
522 if (((sdadel >= sdadel_min) &&
523 (sdadel <= sdadel_max)) &&
524 (p != p_prev)) {
Patrick Delaunay35746c02018-03-12 10:46:09 +0100525 v = calloc(1, sizeof(*v));
Patrice Chotard4fadcaf2017-08-09 14:45:27 +0200526 if (!v)
527 return -ENOMEM;
528
529 v->presc = p;
530 v->scldel = l;
531 v->sdadel = a;
532 p_prev = p;
533
534 list_add_tail(&v->node, solutions);
Nicolas Le Bayon5237f372019-04-18 17:32:43 +0200535 break;
Patrice Chotard4fadcaf2017-08-09 14:45:27 +0200536 }
537 }
Nicolas Le Bayon5237f372019-04-18 17:32:43 +0200538
539 if (p_prev == p)
540 break;
Patrice Chotard4fadcaf2017-08-09 14:45:27 +0200541 }
542 }
543
544 if (list_empty(solutions)) {
Masahiro Yamada9b643e32017-09-16 14:10:41 +0900545 pr_err("%s: no Prescaler solution\n", __func__);
Patrice Chotard4fadcaf2017-08-09 14:45:27 +0200546 ret = -EPERM;
547 }
548
549 return ret;
550}
551
552static int stm32_i2c_choose_solution(struct stm32_i2c_setup *setup,
Alain Volmatc3244652020-03-06 11:09:14 +0100553 const struct stm32_i2c_spec *specs,
Patrice Chotard4fadcaf2017-08-09 14:45:27 +0200554 struct list_head *solutions,
555 struct stm32_i2c_timings *s)
556{
557 struct stm32_i2c_timings *v;
558 u32 i2cbus = DIV_ROUND_CLOSEST(STM32_NSEC_PER_SEC,
559 setup->speed_freq);
560 u32 clk_error_prev = i2cbus;
561 u32 i2cclk = DIV_ROUND_CLOSEST(STM32_NSEC_PER_SEC,
562 setup->clock_src);
563 u32 clk_min, clk_max;
564 u32 af_delay_min;
565 u32 dnf_delay;
566 u32 tsync;
567 u16 l, h;
Christophe Kerello81c48432017-10-17 11:21:32 +0200568 bool sol_found = false;
Patrice Chotard4fadcaf2017-08-09 14:45:27 +0200569 int ret = 0;
570
571 af_delay_min = setup->analog_filter ?
572 STM32_I2C_ANALOG_FILTER_DELAY_MIN : 0;
573 dnf_delay = setup->dnf * i2cclk;
574
575 tsync = af_delay_min + dnf_delay + (2 * i2cclk);
Alain Volmatc3244652020-03-06 11:09:14 +0100576 clk_max = STM32_NSEC_PER_SEC / specs->rate_min;
577 clk_min = STM32_NSEC_PER_SEC / specs->rate_max;
Patrice Chotard4fadcaf2017-08-09 14:45:27 +0200578
579 /*
580 * Among Prescaler possibilities discovered above figures out SCL Low
581 * and High Period. Provided:
582 * - SCL Low Period has to be higher than Low Period of the SCL Clock
583 * defined by I2C Specification. I2C Clock has to be lower than
584 * (SCL Low Period - Analog/Digital filters) / 4.
585 * - SCL High Period has to be lower than High Period of the SCL Clock
586 * defined by I2C Specification
587 * - I2C Clock has to be lower than SCL High Period
588 */
589 list_for_each_entry(v, solutions, node) {
590 u32 prescaler = (v->presc + 1) * i2cclk;
591
592 for (l = 0; l < STM32_SCLL_MAX; l++) {
593 u32 tscl_l = (l + 1) * prescaler + tsync;
Patrick Delaunayc0765f42018-10-29 15:31:55 +0100594
Alain Volmatc3244652020-03-06 11:09:14 +0100595 if (tscl_l < specs->l_min ||
Patrice Chotard4fadcaf2017-08-09 14:45:27 +0200596 (i2cclk >=
597 ((tscl_l - af_delay_min - dnf_delay) / 4))) {
598 continue;
599 }
600
601 for (h = 0; h < STM32_SCLH_MAX; h++) {
602 u32 tscl_h = (h + 1) * prescaler + tsync;
603 u32 tscl = tscl_l + tscl_h +
604 setup->rise_time + setup->fall_time;
605
606 if ((tscl >= clk_min) && (tscl <= clk_max) &&
Alain Volmatc3244652020-03-06 11:09:14 +0100607 (tscl_h >= specs->h_min) &&
Patrice Chotard4fadcaf2017-08-09 14:45:27 +0200608 (i2cclk < tscl_h)) {
Patrick Delaunay499504b2019-06-21 15:26:47 +0200609 u32 clk_error;
Patrice Chotard4fadcaf2017-08-09 14:45:27 +0200610
Patrick Delaunay499504b2019-06-21 15:26:47 +0200611 if (tscl > i2cbus)
612 clk_error = tscl - i2cbus;
613 else
614 clk_error = i2cbus - tscl;
Patrice Chotard4fadcaf2017-08-09 14:45:27 +0200615
616 if (clk_error < clk_error_prev) {
617 clk_error_prev = clk_error;
618 v->scll = l;
619 v->sclh = h;
Christophe Kerello81c48432017-10-17 11:21:32 +0200620 sol_found = true;
621 memcpy(s, v, sizeof(*s));
Patrice Chotard4fadcaf2017-08-09 14:45:27 +0200622 }
623 }
624 }
625 }
626 }
627
Christophe Kerello81c48432017-10-17 11:21:32 +0200628 if (!sol_found) {
Masahiro Yamada9b643e32017-09-16 14:10:41 +0900629 pr_err("%s: no solution at all\n", __func__);
Patrice Chotard4fadcaf2017-08-09 14:45:27 +0200630 ret = -EPERM;
631 }
632
633 return ret;
634}
635
Alain Volmatc3244652020-03-06 11:09:14 +0100636static const struct stm32_i2c_spec *get_specs(u32 rate)
637{
638 unsigned int i;
639
640 for (i = 0; i < ARRAY_SIZE(i2c_specs); i++)
641 if (rate <= i2c_specs[i].rate)
642 return &i2c_specs[i];
643
644 /* NOT REACHED */
645 return ERR_PTR(-EINVAL);
646}
647
Patrice Chotard4fadcaf2017-08-09 14:45:27 +0200648static int stm32_i2c_compute_timing(struct stm32_i2c_priv *i2c_priv,
Patrick Delaunayc0765f42018-10-29 15:31:55 +0100649 struct stm32_i2c_setup *setup,
650 struct stm32_i2c_timings *output)
Patrice Chotard4fadcaf2017-08-09 14:45:27 +0200651{
Alain Volmatc3244652020-03-06 11:09:14 +0100652 const struct stm32_i2c_spec *specs;
Patrice Chotardd10bd6c2017-10-17 11:21:33 +0200653 struct stm32_i2c_timings *v, *_v;
Patrice Chotard4fadcaf2017-08-09 14:45:27 +0200654 struct list_head solutions;
655 int ret;
656
Alain Volmatc3244652020-03-06 11:09:14 +0100657 specs = get_specs(setup->speed_freq);
658 if (specs == ERR_PTR(-EINVAL)) {
659 pr_err("%s: speed out of bound {%d}\n", __func__,
660 setup->speed_freq);
Patrice Chotard4fadcaf2017-08-09 14:45:27 +0200661 return -EINVAL;
662 }
663
Alain Volmatc3244652020-03-06 11:09:14 +0100664 if (setup->rise_time > specs->rise_max ||
665 setup->fall_time > specs->fall_max) {
Masahiro Yamada9b643e32017-09-16 14:10:41 +0900666 pr_err("%s :timings out of bound Rise{%d>%d}/Fall{%d>%d}\n",
Patrick Delaunayc0765f42018-10-29 15:31:55 +0100667 __func__,
Alain Volmatc3244652020-03-06 11:09:14 +0100668 setup->rise_time, specs->rise_max,
669 setup->fall_time, specs->fall_max);
Patrice Chotard4fadcaf2017-08-09 14:45:27 +0200670 return -EINVAL;
671 }
672
673 if (setup->dnf > STM32_I2C_DNF_MAX) {
Masahiro Yamada9b643e32017-09-16 14:10:41 +0900674 pr_err("%s: DNF out of bound %d/%d\n", __func__,
Patrick Delaunayc0765f42018-10-29 15:31:55 +0100675 setup->dnf, STM32_I2C_DNF_MAX);
Patrice Chotard4fadcaf2017-08-09 14:45:27 +0200676 return -EINVAL;
677 }
678
Patrice Chotard4fadcaf2017-08-09 14:45:27 +0200679 INIT_LIST_HEAD(&solutions);
Alain Volmatc3244652020-03-06 11:09:14 +0100680 ret = stm32_i2c_compute_solutions(setup, specs, &solutions);
Patrice Chotard4fadcaf2017-08-09 14:45:27 +0200681 if (ret)
682 goto exit;
683
Alain Volmatc3244652020-03-06 11:09:14 +0100684 ret = stm32_i2c_choose_solution(setup, specs, &solutions, output);
Patrice Chotard4fadcaf2017-08-09 14:45:27 +0200685 if (ret)
686 goto exit;
687
Patrice Chotard4fadcaf2017-08-09 14:45:27 +0200688 debug("%s: Presc: %i, scldel: %i, sdadel: %i, scll: %i, sclh: %i\n",
689 __func__, output->presc,
690 output->scldel, output->sdadel,
691 output->scll, output->sclh);
692
693exit:
694 /* Release list and memory */
695 list_for_each_entry_safe(v, _v, &solutions, node) {
696 list_del(&v->node);
Patrick Delaunay35746c02018-03-12 10:46:09 +0100697 free(v);
Patrice Chotard4fadcaf2017-08-09 14:45:27 +0200698 }
699
700 return ret;
701}
702
Alain Volmatc3244652020-03-06 11:09:14 +0100703static u32 get_lower_rate(u32 rate)
704{
705 int i;
706
707 for (i = ARRAY_SIZE(i2c_specs) - 1; i >= 0; i--)
708 if (rate > i2c_specs[i].rate)
709 return i2c_specs[i].rate;
710
711 return i2c_specs[0].rate;
712}
713
Patrice Chotard4fadcaf2017-08-09 14:45:27 +0200714static int stm32_i2c_setup_timing(struct stm32_i2c_priv *i2c_priv,
Patrick Delaunayc0765f42018-10-29 15:31:55 +0100715 struct stm32_i2c_timings *timing)
Patrice Chotard4fadcaf2017-08-09 14:45:27 +0200716{
717 struct stm32_i2c_setup *setup = i2c_priv->setup;
718 int ret = 0;
719
Alain Volmatc3244652020-03-06 11:09:14 +0100720 setup->speed_freq = i2c_priv->speed;
Patrice Chotard4fadcaf2017-08-09 14:45:27 +0200721 setup->clock_src = clk_get_rate(&i2c_priv->clk);
722
723 if (!setup->clock_src) {
Masahiro Yamada9b643e32017-09-16 14:10:41 +0900724 pr_err("%s: clock rate is 0\n", __func__);
Patrice Chotard4fadcaf2017-08-09 14:45:27 +0200725 return -EINVAL;
726 }
727
728 do {
729 ret = stm32_i2c_compute_timing(i2c_priv, setup, timing);
730 if (ret) {
731 debug("%s: failed to compute I2C timings.\n",
732 __func__);
Alain Volmatc3244652020-03-06 11:09:14 +0100733 if (setup->speed_freq > I2C_SPEED_STANDARD_RATE) {
Patrice Chotard4fadcaf2017-08-09 14:45:27 +0200734 setup->speed_freq =
Alain Volmatc3244652020-03-06 11:09:14 +0100735 get_lower_rate(setup->speed_freq);
Patrice Chotard4fadcaf2017-08-09 14:45:27 +0200736 debug("%s: downgrade I2C Speed Freq to (%i)\n",
Alain Volmatc3244652020-03-06 11:09:14 +0100737 __func__, setup->speed_freq);
Patrice Chotard4fadcaf2017-08-09 14:45:27 +0200738 } else {
739 break;
740 }
741 }
742 } while (ret);
743
744 if (ret) {
Masahiro Yamada9b643e32017-09-16 14:10:41 +0900745 pr_err("%s: impossible to compute I2C timings.\n", __func__);
Patrice Chotard4fadcaf2017-08-09 14:45:27 +0200746 return ret;
747 }
748
Alain Volmatc3244652020-03-06 11:09:14 +0100749 debug("%s: I2C Freq(%i), Clk Source(%i)\n", __func__,
750 setup->speed_freq, setup->clock_src);
Patrice Chotard4fadcaf2017-08-09 14:45:27 +0200751 debug("%s: I2C Rise(%i) and Fall(%i) Time\n", __func__,
752 setup->rise_time, setup->fall_time);
753 debug("%s: I2C Analog Filter(%s), DNF(%i)\n", __func__,
754 setup->analog_filter ? "On" : "Off", setup->dnf);
755
Alain Volmatc3244652020-03-06 11:09:14 +0100756 i2c_priv->speed = setup->speed_freq;
757
Patrice Chotard4fadcaf2017-08-09 14:45:27 +0200758 return 0;
759}
760
761static int stm32_i2c_hw_config(struct stm32_i2c_priv *i2c_priv)
762{
763 struct stm32_i2c_regs *regs = i2c_priv->regs;
764 struct stm32_i2c_timings t;
765 int ret;
766 u32 timing = 0;
767
768 ret = stm32_i2c_setup_timing(i2c_priv, &t);
769 if (ret)
770 return ret;
771
772 /* Disable I2C */
773 clrbits_le32(&regs->cr1, STM32_I2C_CR1_PE);
774
775 /* Timing settings */
776 timing |= STM32_I2C_TIMINGR_PRESC(t.presc);
777 timing |= STM32_I2C_TIMINGR_SCLDEL(t.scldel);
778 timing |= STM32_I2C_TIMINGR_SDADEL(t.sdadel);
779 timing |= STM32_I2C_TIMINGR_SCLH(t.sclh);
780 timing |= STM32_I2C_TIMINGR_SCLL(t.scll);
781 writel(timing, &regs->timingr);
782
783 /* Enable I2C */
784 if (i2c_priv->setup->analog_filter)
785 clrbits_le32(&regs->cr1, STM32_I2C_CR1_ANFOFF);
786 else
787 setbits_le32(&regs->cr1, STM32_I2C_CR1_ANFOFF);
788 setbits_le32(&regs->cr1, STM32_I2C_CR1_PE);
789
790 return 0;
791}
792
793static int stm32_i2c_set_bus_speed(struct udevice *bus, unsigned int speed)
794{
795 struct stm32_i2c_priv *i2c_priv = dev_get_priv(bus);
796
Alain Volmatc3244652020-03-06 11:09:14 +0100797 if (speed > I2C_SPEED_FAST_PLUS_RATE) {
Patrice Chotard4fadcaf2017-08-09 14:45:27 +0200798 debug("%s: Speed %d not supported\n", __func__, speed);
799 return -EINVAL;
800 }
801
Alain Volmatc3244652020-03-06 11:09:14 +0100802 i2c_priv->speed = speed;
803
Patrice Chotard4fadcaf2017-08-09 14:45:27 +0200804 return stm32_i2c_hw_config(i2c_priv);
805}
806
807static int stm32_i2c_probe(struct udevice *dev)
808{
809 struct stm32_i2c_priv *i2c_priv = dev_get_priv(dev);
810 struct reset_ctl reset_ctl;
811 fdt_addr_t addr;
812 int ret;
813
814 addr = dev_read_addr(dev);
815 if (addr == FDT_ADDR_T_NONE)
816 return -EINVAL;
817
818 i2c_priv->regs = (struct stm32_i2c_regs *)addr;
819
820 ret = clk_get_by_index(dev, 0, &i2c_priv->clk);
821 if (ret)
822 return ret;
823
824 ret = clk_enable(&i2c_priv->clk);
825 if (ret)
826 goto clk_free;
827
828 ret = reset_get_by_index(dev, 0, &reset_ctl);
829 if (ret)
830 goto clk_disable;
831
832 reset_assert(&reset_ctl);
833 udelay(2);
834 reset_deassert(&reset_ctl);
835
836 return 0;
837
838clk_disable:
839 clk_disable(&i2c_priv->clk);
840clk_free:
841 clk_free(&i2c_priv->clk);
842
843 return ret;
844}
845
846static int stm32_ofdata_to_platdata(struct udevice *dev)
847{
848 struct stm32_i2c_priv *i2c_priv = dev_get_priv(dev);
849 u32 rise_time, fall_time;
850
851 i2c_priv->setup = (struct stm32_i2c_setup *)dev_get_driver_data(dev);
852 if (!i2c_priv->setup)
853 return -EINVAL;
854
855 rise_time = dev_read_u32_default(dev, "i2c-scl-rising-time-ns", 0);
856 if (rise_time)
857 i2c_priv->setup->rise_time = rise_time;
858
859 fall_time = dev_read_u32_default(dev, "i2c-scl-falling-time-ns", 0);
860 if (fall_time)
861 i2c_priv->setup->fall_time = fall_time;
862
863 return 0;
864}
865
866static const struct dm_i2c_ops stm32_i2c_ops = {
867 .xfer = stm32_i2c_xfer,
868 .set_bus_speed = stm32_i2c_set_bus_speed,
869};
870
871static const struct udevice_id stm32_i2c_of_match[] = {
872 { .compatible = "st,stm32f7-i2c", .data = (ulong)&stm32f7_setup },
873 {}
874};
875
876U_BOOT_DRIVER(stm32f7_i2c) = {
877 .name = "stm32f7-i2c",
878 .id = UCLASS_I2C,
879 .of_match = stm32_i2c_of_match,
880 .ofdata_to_platdata = stm32_ofdata_to_platdata,
881 .probe = stm32_i2c_probe,
882 .priv_auto_alloc_size = sizeof(struct stm32_i2c_priv),
883 .ops = &stm32_i2c_ops,
884};