blob: 5592fe9181718b5076bbed5ccd6eb992aa6626dc [file] [log] [blame]
Weijie Gao9ad71f62022-09-09 19:59:48 +08001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright (C) 2022 MediaTek Inc. All Rights Reserved.
4 *
5 * Author: Mingming Lee <Mingming.Lee@mediatek.com>
6 *
7 * MediaTek I2C Interface driver
8 */
9
10#include <clk.h>
11#include <cpu_func.h>
12#include <dm.h>
13#include <i2c.h>
14#include <log.h>
15#include <asm/cache.h>
16#include <asm/io.h>
17#include <linux/delay.h>
18#include <linux/errno.h>
19
20#define I2C_RS_TRANSFER BIT(4)
21#define I2C_HS_NACKERR BIT(2)
22#define I2C_ACKERR BIT(1)
23#define I2C_TRANSAC_COMP BIT(0)
24#define I2C_TRANSAC_START BIT(0)
25#define I2C_RS_MUL_CNFG BIT(15)
26#define I2C_RS_MUL_TRIG BIT(14)
27#define I2C_DCM_DISABLE 0x0000
28#define I2C_IO_CONFIG_OPEN_DRAIN 0x0003
29#define I2C_IO_CONFIG_PUSH_PULL 0x0000
30#define I2C_SOFT_RST 0x0001
31#define I2C_FIFO_ADDR_CLR 0x0001
32#define I2C_DELAY_LEN 0x0002
33#define I2C_ST_START_CON 0x8001
34#define I2C_FS_START_CON 0x1800
35#define I2C_TIME_CLR_VALUE 0x0000
36#define I2C_TIME_DEFAULT_VALUE 0x0003
37#define I2C_WRRD_TRANAC_VALUE 0x0002
38#define I2C_RD_TRANAC_VALUE 0x0001
39
40#define I2C_DMA_CON_TX 0x0000
41#define I2C_DMA_CON_RX 0x0001
42#define I2C_DMA_START_EN 0x0001
43#define I2C_DMA_INT_FLAG_NONE 0x0000
44#define I2C_DMA_CLR_FLAG 0x0000
45#define I2C_DMA_TX_RX 0x0000
46#define I2C_DMA_HARD_RST 0x0002
47
48#define MAX_ST_MODE_SPEED 100000
49#define MAX_FS_MODE_SPEED 400000
50#define MAX_HS_MODE_SPEED 3400000
51#define MAX_SAMPLE_CNT_DIV 8
52#define MAX_STEP_CNT_DIV 64
53#define MAX_HS_STEP_CNT_DIV 8
54#define I2C_DEFAULT_CLK_DIV 4
55
56#define MAX_I2C_ADDR 0x7f
57#define MAX_I2C_LEN 0xff
58#define TRANS_ADDR_ONLY BIT(8)
59#define TRANSFER_TIMEOUT 50000 /* us */
60#define I2C_FIFO_STAT1_MASK 0x001f
61#define TIMING_SAMPLE_OFFSET 8
62#define HS_SAMPLE_OFFSET 12
63#define HS_STEP_OFFSET 8
64
65#define I2C_CONTROL_WRAPPER BIT(0)
66#define I2C_CONTROL_RS BIT(1)
67#define I2C_CONTROL_DMA_EN BIT(2)
68#define I2C_CONTROL_CLK_EXT_EN BIT(3)
69#define I2C_CONTROL_DIR_CHANGE BIT(4)
70#define I2C_CONTROL_ACKERR_DET_EN BIT(5)
71#define I2C_CONTROL_TRANSFER_LEN_CHANGE BIT(6)
72#define I2C_CONTROL_DMAACK BIT(8)
73#define I2C_CONTROL_ASYNC BIT(9)
74
75#define I2C_MASTER_WR BIT(0)
76#define I2C_MASTER_RD BIT(1)
77#define I2C_MASTER_WRRD (I2C_MASTER_WR | I2C_MASTER_RD)
78
79enum I2C_REGS_OFFSET {
80 REG_PORT,
81 REG_SLAVE_ADDR,
82 REG_INTR_MASK,
83 REG_INTR_STAT,
84 REG_CONTROL,
85 REG_TRANSFER_LEN,
86 REG_TRANSAC_LEN,
87 REG_DELAY_LEN,
88 REG_TIMING,
89 REG_START,
90 REG_EXT_CONF,
91 REG_FIFO_STAT1,
92 REG_LTIMING,
93 REG_FIFO_STAT,
94 REG_FIFO_THRESH,
95 REG_FIFO_ADDR_CLR,
96 REG_IO_CONFIG,
97 REG_RSV_DEBUG,
98 REG_HS,
99 REG_SOFTRESET,
100 REG_DCM_EN,
101 REG_PATH_DIR,
102 REG_DEBUGSTAT,
103 REG_DEBUGCTRL,
104 REG_TRANSFER_LEN_AUX,
105 REG_CLOCK_DIV,
106 REG_SCL_HL_RATIO,
107 REG_SCL_HS_HL_RATIO,
108 REG_SCL_MIS_COMP_POINT,
109 REG_STA_STOP_AC_TIME,
110 REG_HS_STA_STOP_AC_TIME,
111 REG_DATA_TIME,
112};
113
114enum DMA_REGS_OFFSET {
115 REG_INT_FLAG = 0x0,
116 REG_INT_EN = 0x04,
117 REG_EN = 0x08,
118 REG_RST = 0x0c,
119 REG_CON = 0x18,
120 REG_TX_MEM_ADDR = 0x1c,
121 REG_RX_MEM_ADDR = 0x20,
122 REG_TX_LEN = 0x24,
123 REG_RX_LEN = 0x28,
124};
125
126static const uint mt_i2c_regs_v1[] = {
127 [REG_PORT] = 0x0,
128 [REG_SLAVE_ADDR] = 0x4,
129 [REG_INTR_MASK] = 0x8,
130 [REG_INTR_STAT] = 0xc,
131 [REG_CONTROL] = 0x10,
132 [REG_TRANSFER_LEN] = 0x14,
133 [REG_TRANSAC_LEN] = 0x18,
134 [REG_DELAY_LEN] = 0x1c,
135 [REG_TIMING] = 0x20,
136 [REG_START] = 0x24,
137 [REG_EXT_CONF] = 0x28,
138 [REG_FIFO_STAT1] = 0x2c,
139 [REG_FIFO_STAT] = 0x30,
140 [REG_FIFO_THRESH] = 0x34,
141 [REG_FIFO_ADDR_CLR] = 0x38,
142 [REG_IO_CONFIG] = 0x40,
143 [REG_RSV_DEBUG] = 0x44,
144 [REG_HS] = 0x48,
145 [REG_SOFTRESET] = 0x50,
146 [REG_SOFTRESET] = 0x50,
147 [REG_DCM_EN] = 0x54,
148 [REG_DEBUGSTAT] = 0x64,
149 [REG_DEBUGCTRL] = 0x68,
150 [REG_TRANSFER_LEN_AUX] = 0x6c,
151 [REG_CLOCK_DIV] = 0x70,
152 [REG_SCL_HL_RATIO] = 0x74,
153 [REG_SCL_HS_HL_RATIO] = 0x78,
154 [REG_SCL_MIS_COMP_POINT] = 0x7c,
155 [REG_STA_STOP_AC_TIME] = 0x80,
156 [REG_HS_STA_STOP_AC_TIME] = 0x84,
157 [REG_DATA_TIME] = 0x88,
158};
159
160static const uint mt_i2c_regs_v2[] = {
161 [REG_PORT] = 0x0,
162 [REG_SLAVE_ADDR] = 0x4,
163 [REG_INTR_MASK] = 0x8,
164 [REG_INTR_STAT] = 0xc,
165 [REG_CONTROL] = 0x10,
166 [REG_TRANSFER_LEN] = 0x14,
167 [REG_TRANSAC_LEN] = 0x18,
168 [REG_DELAY_LEN] = 0x1c,
169 [REG_TIMING] = 0x20,
170 [REG_START] = 0x24,
171 [REG_EXT_CONF] = 0x28,
172 [REG_LTIMING] = 0x2c,
173 [REG_HS] = 0x30,
174 [REG_IO_CONFIG] = 0x34,
175 [REG_FIFO_ADDR_CLR] = 0x38,
176 [REG_TRANSFER_LEN_AUX] = 0x44,
177 [REG_CLOCK_DIV] = 0x48,
178 [REG_SOFTRESET] = 0x50,
179 [REG_DEBUGSTAT] = 0xe0,
180 [REG_DEBUGCTRL] = 0xe8,
181 [REG_FIFO_STAT] = 0xf4,
182 [REG_FIFO_THRESH] = 0xf8,
183 [REG_DCM_EN] = 0xf88,
184};
185
Weijie Gao7fa40df2023-07-19 17:16:15 +0800186static const uint mt_i2c_regs_v3[] = {
187 [REG_PORT] = 0x0,
188 [REG_INTR_MASK] = 0x8,
189 [REG_INTR_STAT] = 0xc,
190 [REG_CONTROL] = 0x10,
191 [REG_TRANSFER_LEN] = 0x14,
192 [REG_TRANSAC_LEN] = 0x18,
193 [REG_DELAY_LEN] = 0x1c,
194 [REG_TIMING] = 0x20,
195 [REG_START] = 0x24,
196 [REG_EXT_CONF] = 0x28,
197 [REG_LTIMING] = 0x2c,
198 [REG_HS] = 0x30,
199 [REG_IO_CONFIG] = 0x34,
200 [REG_FIFO_ADDR_CLR] = 0x38,
201 [REG_TRANSFER_LEN_AUX] = 0x44,
202 [REG_CLOCK_DIV] = 0x48,
203 [REG_SOFTRESET] = 0x50,
204 [REG_SLAVE_ADDR] = 0x94,
205 [REG_DEBUGSTAT] = 0xe4,
206 [REG_DEBUGCTRL] = 0xe8,
207 [REG_FIFO_STAT] = 0xf4,
208 [REG_FIFO_THRESH] = 0xf8,
209 [REG_DCM_EN] = 0xf88,
210};
211
Weijie Gao9ad71f62022-09-09 19:59:48 +0800212struct mtk_i2c_soc_data {
213 const uint *regs;
214 uint dma_sync: 1;
Weijie Gao7fa40df2023-07-19 17:16:15 +0800215 uint ltiming_adjust: 1;
Weijie Gao9ad71f62022-09-09 19:59:48 +0800216};
217
218struct mtk_i2c_priv {
219 /* set in i2c probe */
220 void __iomem *base; /* i2c base addr */
221 void __iomem *pdmabase; /* dma base address*/
222 struct clk clk_main; /* main clock for i2c bus */
223 struct clk clk_dma; /* DMA clock for i2c via DMA */
224 const struct mtk_i2c_soc_data *soc_data; /* Compatible data for different IC */
225 int op; /* operation mode */
226 bool zero_len; /* Only transfer slave address, no data */
227 bool pushpull; /* push pull mode or open drain mode */
228 bool filter_msg; /* filter msg error log */
229 bool auto_restart; /* restart mode */
230 bool ignore_restart_irq; /* ignore restart IRQ */
231 uint speed; /* i2c speed, unit: hz */
232};
233
234static inline void i2c_writel(struct mtk_i2c_priv *priv, uint reg, uint value)
235{
236 u32 offset = priv->soc_data->regs[reg];
237
238 writel(value, priv->base + offset);
239}
240
241static inline uint i2c_readl(struct mtk_i2c_priv *priv, uint offset)
242{
243 return readl(priv->base + priv->soc_data->regs[offset]);
244}
245
246static int mtk_i2c_clk_enable(struct mtk_i2c_priv *priv)
247{
248 int ret;
249
250 ret = clk_enable(&priv->clk_main);
251 if (ret)
252 return log_msg_ret("enable clk_main", ret);
253
254 ret = clk_enable(&priv->clk_dma);
255 if (ret)
256 return log_msg_ret("enable clk_dma", ret);
257
258 return 0;
259}
260
261static int mtk_i2c_clk_disable(struct mtk_i2c_priv *priv)
262{
263 int ret;
264
265 ret = clk_disable(&priv->clk_dma);
266 if (ret)
267 return log_msg_ret("disable clk_dma", ret);
268
269 ret = clk_disable(&priv->clk_main);
270 if (ret)
271 return log_msg_ret("disable clk_main", ret);
272
273 return 0;
274}
275
276static void mtk_i2c_init_hw(struct mtk_i2c_priv *priv)
277{
278 uint control_reg;
279
280 writel(I2C_DMA_HARD_RST, priv->pdmabase + REG_RST);
281 writel(I2C_DMA_CLR_FLAG, priv->pdmabase + REG_RST);
282 i2c_writel(priv, REG_SOFTRESET, I2C_SOFT_RST);
283 /* set ioconfig */
284 if (priv->pushpull)
285 i2c_writel(priv, REG_IO_CONFIG, I2C_IO_CONFIG_PUSH_PULL);
286 else
287 i2c_writel(priv, REG_IO_CONFIG, I2C_IO_CONFIG_OPEN_DRAIN);
288
289 i2c_writel(priv, REG_DCM_EN, I2C_DCM_DISABLE);
290 control_reg = I2C_CONTROL_ACKERR_DET_EN | I2C_CONTROL_CLK_EXT_EN;
291 if (priv->soc_data->dma_sync)
292 control_reg |= I2C_CONTROL_DMAACK | I2C_CONTROL_ASYNC;
293 i2c_writel(priv, REG_CONTROL, control_reg);
294 i2c_writel(priv, REG_DELAY_LEN, I2C_DELAY_LEN);
295}
296
297/*
298 * Calculate i2c port speed
299 *
300 * Hardware design:
301 * i2c_bus_freq = parent_clk / (clock_div * 2 * sample_cnt * step_cnt)
302 * clock_div: fixed in hardware, but may be various in different SoCs
303 *
304 * The calculation want to pick the highest bus frequency that is still
305 * less than or equal to target_speed. The calculation try to get
306 * sample_cnt and step_cn
307 * @param[in]
308 * clk_src: i2c clock source
309 * @param[out]
310 * timing_step_cnt: step cnt calculate result
311 * @param[out]
312 * timing_sample_cnt: sample cnt calculate result
313 * @return
314 * 0, set speed successfully.
315 * -EINVAL, Unsupported speed.
316 */
317static int mtk_i2c_calculate_speed(uint clk_src,
318 uint target_speed,
319 uint *timing_step_cnt,
320 uint *timing_sample_cnt)
321{
322 uint base_sample_cnt = MAX_SAMPLE_CNT_DIV;
323 uint base_step_cnt;
324 uint max_step_cnt;
325 uint sample_cnt;
326 uint step_cnt;
327 uint opt_div;
328 uint best_mul;
329 uint cnt_mul;
330
331 if (target_speed > MAX_HS_MODE_SPEED)
332 target_speed = MAX_HS_MODE_SPEED;
333
334 if (target_speed > MAX_FS_MODE_SPEED)
335 max_step_cnt = MAX_HS_STEP_CNT_DIV;
336 else
337 max_step_cnt = MAX_STEP_CNT_DIV;
338
339 base_step_cnt = max_step_cnt;
340 /* Find the best combination */
341 opt_div = DIV_ROUND_UP(clk_src >> 1, target_speed);
342 best_mul = MAX_SAMPLE_CNT_DIV * max_step_cnt;
343
344 /*
345 * Search for the best pair (sample_cnt, step_cnt) with
346 * 0 < sample_cnt < MAX_SAMPLE_CNT_DIV
347 * 0 < step_cnt < max_step_cnt
348 * sample_cnt * step_cnt >= opt_div
349 * optimizing for sample_cnt * step_cnt being minimal
350 */
351 for (sample_cnt = 1; sample_cnt <= MAX_SAMPLE_CNT_DIV; sample_cnt++) {
352 step_cnt = DIV_ROUND_UP(opt_div, sample_cnt);
353 cnt_mul = step_cnt * sample_cnt;
354 if (step_cnt > max_step_cnt)
355 continue;
356
357 if (cnt_mul < best_mul) {
358 best_mul = cnt_mul;
359 base_sample_cnt = sample_cnt;
360 base_step_cnt = step_cnt;
361 if (best_mul == opt_div)
362 break;
363 }
364 }
365
366 sample_cnt = base_sample_cnt;
367 step_cnt = base_step_cnt;
368
369 if ((clk_src / (2 * sample_cnt * step_cnt)) > target_speed) {
370 /*
371 * In this case, hardware can't support such
372 * low i2c_bus_freq
373 */
374 debug("Unsupported speed(%uhz)\n", target_speed);
375 return log_msg_ret("calculate speed", -EINVAL);
376 }
377
378 *timing_step_cnt = step_cnt - 1;
379 *timing_sample_cnt = sample_cnt - 1;
380
381 return 0;
382}
383
384/*
385 * mtk_i2c_set_speed
386 *
387 * @par Description
388 * Calculate i2c speed and write sample_cnt, step_cnt to TIMING register.
389 * @param[in]
390 * dev: udevice pointer, struct udevice contains i2c source clock,
391 * clock divide and speed.
392 * @return
393 * 0, set speed successfully.\n
394 * error code from mtk_i2c_calculate_speed().
395 */
396static int mtk_i2c_set_speed(struct udevice *dev, uint speed)
397{
398 struct mtk_i2c_priv *priv = dev_get_priv(dev);
399 uint high_speed_reg;
400 uint sample_cnt;
401 uint timing_reg;
402 uint step_cnt;
403 uint clk_src;
404 int ret = 0;
405
406 priv->speed = speed;
407 if (mtk_i2c_clk_enable(priv))
408 return log_msg_ret("set_speed enable clk", -1);
409
410 clk_src = clk_get_rate(&priv->clk_main) / I2C_DEFAULT_CLK_DIV;
411 i2c_writel(priv, REG_CLOCK_DIV, (I2C_DEFAULT_CLK_DIV - 1));
412 if (priv->speed > MAX_FS_MODE_SPEED) {
413 /* Set master code speed register */
414 ret = mtk_i2c_calculate_speed(clk_src, MAX_FS_MODE_SPEED,
415 &step_cnt, &sample_cnt);
416 if (ret < 0)
417 goto exit;
418
419 timing_reg = (sample_cnt << TIMING_SAMPLE_OFFSET) | step_cnt;
420 i2c_writel(priv, REG_TIMING, timing_reg);
421 /* Set the high speed mode register */
422 ret = mtk_i2c_calculate_speed(clk_src, priv->speed,
423 &step_cnt, &sample_cnt);
424 if (ret < 0)
425 goto exit;
426
427 high_speed_reg = I2C_TIME_DEFAULT_VALUE |
428 (sample_cnt << HS_SAMPLE_OFFSET) |
429 (step_cnt << HS_STEP_OFFSET);
430 i2c_writel(priv, REG_HS, high_speed_reg);
Weijie Gao7fa40df2023-07-19 17:16:15 +0800431 if (priv->soc_data->ltiming_adjust) {
432 timing_reg = (sample_cnt << 12) | (step_cnt << 9);
433 i2c_writel(priv, REG_LTIMING, timing_reg);
434 }
Weijie Gao9ad71f62022-09-09 19:59:48 +0800435 } else {
436 ret = mtk_i2c_calculate_speed(clk_src, priv->speed,
437 &step_cnt, &sample_cnt);
438 if (ret < 0)
439 goto exit;
440
441 timing_reg = (sample_cnt << TIMING_SAMPLE_OFFSET) | step_cnt;
442 /* Disable the high speed transaction */
443 high_speed_reg = I2C_TIME_CLR_VALUE;
444 i2c_writel(priv, REG_TIMING, timing_reg);
445 i2c_writel(priv, REG_HS, high_speed_reg);
Weijie Gao7fa40df2023-07-19 17:16:15 +0800446 if (priv->soc_data->ltiming_adjust) {
447 timing_reg = (sample_cnt << 6) | step_cnt;
448 i2c_writel(priv, REG_LTIMING, timing_reg);
449 }
Weijie Gao9ad71f62022-09-09 19:59:48 +0800450 }
Weijie Gao7fa40df2023-07-19 17:16:15 +0800451
Weijie Gao9ad71f62022-09-09 19:59:48 +0800452exit:
453 if (mtk_i2c_clk_disable(priv))
454 return log_msg_ret("set_speed disable clk", -1);
455
456 return ret;
457}
458
459/*
460 * mtk_i2c_do_transfer
461 *
462 * @par Description
463 * Configure i2c register and trigger transfer.
464 * @param[in]
465 * priv: mtk_i2cmtk_i2c_priv pointer, struct mtk_i2c_priv contains register base\n
466 * address, operation mode, interrupt status and i2c driver data.
467 * @param[in]
468 * msgs: i2c_msg pointer, struct i2c_msg contains slave\n
469 * address, operation mode, msg length and data buffer.
470 * @param[in]
471 * num: i2c_msg number.
472 * @param[in]
473 * left_num: left i2c_msg number.
474 * @return
475 * 0, i2c transfer successfully.\n
476 * -ETIMEDOUT, i2c transfer timeout.\n
477 * -EREMOTEIO, i2c transfer ack error.
478 */
479static int mtk_i2c_do_transfer(struct mtk_i2c_priv *priv,
480 struct i2c_msg *msgs,
481 int num, int left_num)
482{
483 struct i2c_msg *msg_rx = NULL;
484 uint restart_flag = 0;
485 uint trans_error = 0;
486 uint irq_stat = 0;
487 uint tmo_poll = 0;
488 uint control_reg;
489 bool tmo = false;
490 uint start_reg;
491 uint addr_reg;
492 int ret = 0;
493
494 if (priv->auto_restart)
495 restart_flag = I2C_RS_TRANSFER;
496
497 control_reg = i2c_readl(priv, REG_CONTROL) &
498 ~(I2C_CONTROL_DIR_CHANGE | I2C_CONTROL_RS);
499
500 if (priv->speed > MAX_FS_MODE_SPEED || num > 1)
501 control_reg |= I2C_CONTROL_RS;
502
503 if (priv->op == I2C_MASTER_WRRD)
504 control_reg |= I2C_CONTROL_DIR_CHANGE | I2C_CONTROL_RS;
505
506 control_reg |= I2C_CONTROL_DMA_EN;
507 i2c_writel(priv, REG_CONTROL, control_reg);
508
509 /* set start condition */
510 if (priv->speed <= MAX_ST_MODE_SPEED)
511 i2c_writel(priv, REG_EXT_CONF, I2C_ST_START_CON);
512 else
513 i2c_writel(priv, REG_EXT_CONF, I2C_FS_START_CON);
514
515 addr_reg = msgs->addr << 1;
516 if (priv->op == I2C_MASTER_RD)
517 addr_reg |= I2C_M_RD;
518 if (priv->zero_len)
519 i2c_writel(priv, REG_SLAVE_ADDR, addr_reg | TRANS_ADDR_ONLY);
520 else
521 i2c_writel(priv, REG_SLAVE_ADDR, addr_reg);
522
523 /* clear interrupt status */
524 i2c_writel(priv, REG_INTR_STAT, restart_flag | I2C_HS_NACKERR |
525 I2C_ACKERR | I2C_TRANSAC_COMP);
526 i2c_writel(priv, REG_FIFO_ADDR_CLR, I2C_FIFO_ADDR_CLR);
527
528 /* enable interrupt */
529 i2c_writel(priv, REG_INTR_MASK, restart_flag | I2C_HS_NACKERR |
530 I2C_ACKERR | I2C_TRANSAC_COMP);
531
532 /* set transfer and transaction len */
533 if (priv->op == I2C_MASTER_WRRD) {
534 i2c_writel(priv, REG_TRANSFER_LEN, msgs->len);
535 i2c_writel(priv, REG_TRANSFER_LEN_AUX, (msgs + 1)->len);
536 i2c_writel(priv, REG_TRANSAC_LEN, I2C_WRRD_TRANAC_VALUE);
537 } else {
538 i2c_writel(priv, REG_TRANSFER_LEN, msgs->len);
539 i2c_writel(priv, REG_TRANSAC_LEN, num);
540 }
541
542 /* Clear DMA interrupt flag */
543 writel(I2C_DMA_INT_FLAG_NONE, priv->pdmabase + REG_INT_FLAG);
544
545 /* Flush cache for first msg */
546 flush_cache((ulong)msgs->buf, msgs->len);
547
548 /*
549 * prepare buffer data to start transfer
550 * three cases here: read, write, write then read
551 */
552 if (priv->op & I2C_MASTER_WR) {
553 /* Set DMA direction TX (w/ or w/o RX) */
554 writel(I2C_DMA_CON_TX, priv->pdmabase + REG_CON);
555
556 /* Write the tx buffer address to dma register */
557 writel((ulong)msgs->buf, priv->pdmabase + REG_TX_MEM_ADDR);
558 /* Write the tx length to dma register */
559 writel(msgs->len, priv->pdmabase + REG_TX_LEN);
560
561 if (priv->op & I2C_MASTER_RD) {
562 /* write then read */
563 msg_rx = msgs + 1;
564
565 /* Flush cache for second msg */
566 flush_cache((ulong)msg_rx->buf, msg_rx->len);
567 }
568 }
569
570 if (priv->op & I2C_MASTER_RD) {
571 if (!msg_rx) {
572 /* Set DMA direction RX */
573 writel(I2C_DMA_CON_RX, priv->pdmabase + REG_CON);
574
575 msg_rx = msgs;
576 }
577
578 /* Write the rx buffer address to dma register */
579 writel((ulong)msg_rx->buf, priv->pdmabase + REG_RX_MEM_ADDR);
580 /* Write the rx length to dma register */
581 writel(msg_rx->len, priv->pdmabase + REG_RX_LEN);
582 }
583
584 writel(I2C_DMA_START_EN, priv->pdmabase + REG_EN);
585
586 if (!priv->auto_restart) {
587 start_reg = I2C_TRANSAC_START;
588 } else {
589 start_reg = I2C_TRANSAC_START | I2C_RS_MUL_TRIG;
590 if (left_num >= 1)
591 start_reg |= I2C_RS_MUL_CNFG;
592 }
593 i2c_writel(priv, REG_START, start_reg);
594
595 for (;;) {
596 irq_stat = i2c_readl(priv, REG_INTR_STAT);
597
598 /* ignore the first restart irq after the master code */
599 if (priv->ignore_restart_irq && (irq_stat & restart_flag)) {
600 priv->ignore_restart_irq = false;
601 irq_stat = 0;
602 i2c_writel(priv, REG_START, I2C_RS_MUL_CNFG |
603 I2C_RS_MUL_TRIG | I2C_TRANSAC_START);
604 }
605
606 if (irq_stat & (I2C_TRANSAC_COMP | restart_flag)) {
607 tmo = false;
608 if (irq_stat & (I2C_HS_NACKERR | I2C_ACKERR))
609 trans_error = 1;
610
611 break;
612 }
613 udelay(1);
614 if (tmo_poll++ >= TRANSFER_TIMEOUT) {
615 tmo = true;
616 break;
617 }
618 }
619
620 /* clear interrupt mask */
621 i2c_writel(priv, REG_INTR_MASK, ~(restart_flag | I2C_HS_NACKERR |
622 I2C_ACKERR | I2C_TRANSAC_COMP));
623
Francois Berder8cf61052023-09-08 18:47:46 +0200624 if (tmo || trans_error != 0) {
Weijie Gao9ad71f62022-09-09 19:59:48 +0800625 if (tmo) {
626 ret = -ETIMEDOUT;
627 if (!priv->filter_msg)
628 debug("I2C timeout! addr: 0x%x,\n", msgs->addr);
629 } else {
630 ret = -EREMOTEIO;
631 if (!priv->filter_msg)
632 debug("I2C ACKERR! addr: 0x%x,IRQ:0x%x\n",
633 msgs->addr, irq_stat);
634 }
635 mtk_i2c_init_hw(priv);
636 }
637
638 return ret;
639}
640
641/*
642 * mtk_i2c_transfer
643 *
644 * @par Description
645 * Common i2c transfer API. Set i2c transfer mode according to i2c_msg\n
646 * information, then call mtk_i2c_do_transfer() to configure i2c register\n
647 * and trigger transfer.
648 * @param[in]
649 * dev: udevice pointer, struct udevice contains struct mtk_i2c_priv, \n
650 * struct mtk_i2c_priv contains register base\n
651 * address, operation mode, interrupt status and i2c driver data.
652 * @param[in]
653 * msgs: i2c_msg pointer, struct i2c_msg contains slave\n
654 * address, operation mode, msg length and data buffer.
655 * @param[in]
656 * num: i2c_msg number.
657 * @return
658 * i2c_msg number, i2c transfer successfully.\n
659 * -EINVAL, msg length is more than 16\n
660 * use DMA MODE or slave address more than 0x7f.\n
661 * error code from mtk_i2c_init_base().\n
662 * error code from mtk_i2c_set_speed().\n
663 * error code from mtk_i2c_do_transfer().
664 */
665static int mtk_i2c_transfer(struct udevice *dev, struct i2c_msg *msg,
666 int nmsgs)
667{
668 struct mtk_i2c_priv *priv = dev_get_priv(dev);
669 int left_num;
670 uint num_cnt;
671 int ret;
672
673 priv->auto_restart = true;
674 left_num = nmsgs;
675 if (mtk_i2c_clk_enable(priv))
676 return log_msg_ret("transfer enable clk", -1);
677
678 for (num_cnt = 0; num_cnt < nmsgs; num_cnt++) {
679 if (((msg + num_cnt)->addr) > MAX_I2C_ADDR) {
680 ret = -EINVAL;
681 goto err_exit;
682 }
683 if ((msg + num_cnt)->len > MAX_I2C_LEN) {
684 ret = -EINVAL;
685 goto err_exit;
686 }
687 }
688
689 /* check if we can skip restart and optimize using WRRD mode */
690 if (priv->auto_restart && nmsgs == 2) {
691 if (!(msg[0].flags & I2C_M_RD) && (msg[1].flags & I2C_M_RD) &&
692 msg[0].addr == msg[1].addr) {
693 priv->auto_restart = false;
694 }
695 }
696
697 if (priv->auto_restart && nmsgs >= 2 && priv->speed > MAX_FS_MODE_SPEED)
698 /* ignore the first restart irq after the master code,
699 * otherwise the first transfer will be discarded.
700 */
701 priv->ignore_restart_irq = true;
702 else
703 priv->ignore_restart_irq = false;
704
705 while (left_num--) {
706 /* transfer slave address only to support devices detect */
707 if (!msg->buf)
708 priv->zero_len = true;
709 else
710 priv->zero_len = false;
711
712 if (msg->flags & I2C_M_RD)
713 priv->op = I2C_MASTER_RD;
714 else
715 priv->op = I2C_MASTER_WR;
716
717 if (!priv->auto_restart) {
718 if (nmsgs > 1) {
719 /* combined two messages into one transaction */
720 priv->op = I2C_MASTER_WRRD;
721 left_num--;
722 }
723 }
724 ret = mtk_i2c_do_transfer(priv, msg, nmsgs, left_num);
725 if (ret < 0)
726 goto err_exit;
727 msg++;
728 }
729 ret = 0;
730
731err_exit:
732 if (mtk_i2c_clk_disable(priv))
733 return log_msg_ret("transfer disable clk", -1);
734
735 return ret;
736}
737
738static int mtk_i2c_of_to_plat(struct udevice *dev)
739{
740 struct mtk_i2c_priv *priv = dev_get_priv(dev);
741 int ret;
742
743 priv->base = dev_remap_addr_index(dev, 0);
744 priv->pdmabase = dev_remap_addr_index(dev, 1);
745 ret = clk_get_by_index(dev, 0, &priv->clk_main);
746 if (ret)
747 return log_msg_ret("clk_get_by_index 0", ret);
748
749 ret = clk_get_by_index(dev, 1, &priv->clk_dma);
750
751 return ret;
752}
753
754static int mtk_i2c_probe(struct udevice *dev)
755{
756 struct mtk_i2c_priv *priv = dev_get_priv(dev);
757
758 priv->soc_data = (struct mtk_i2c_soc_data *)dev_get_driver_data(dev);
759
760 if (mtk_i2c_clk_enable(priv))
761 return log_msg_ret("probe enable clk", -1);
762
763 mtk_i2c_init_hw(priv);
Weijie Gao9ad71f62022-09-09 19:59:48 +0800764 if (mtk_i2c_clk_disable(priv))
765 return log_msg_ret("probe disable clk", -1);
766
767 return 0;
768}
769
770static int mtk_i2c_deblock(struct udevice *dev)
771{
772 struct mtk_i2c_priv *priv = dev_get_priv(dev);
773
774 if (mtk_i2c_clk_enable(priv))
775 return log_msg_ret("deblock enable clk", -1);
776
777 mtk_i2c_init_hw(priv);
778
779 if (mtk_i2c_clk_disable(priv))
780 return log_msg_ret("deblock disable clk", -1);
781
782 return 0;
783}
784
785static const struct mtk_i2c_soc_data mt76xx_soc_data = {
786 .regs = mt_i2c_regs_v1,
787 .dma_sync = 0,
Weijie Gao7fa40df2023-07-19 17:16:15 +0800788 .ltiming_adjust = 0,
Weijie Gao9ad71f62022-09-09 19:59:48 +0800789};
790
791static const struct mtk_i2c_soc_data mt7981_soc_data = {
Weijie Gao7fa40df2023-07-19 17:16:15 +0800792 .regs = mt_i2c_regs_v3,
Weijie Gao9ad71f62022-09-09 19:59:48 +0800793 .dma_sync = 1,
Weijie Gao7fa40df2023-07-19 17:16:15 +0800794 .ltiming_adjust = 1,
Weijie Gao9ad71f62022-09-09 19:59:48 +0800795};
796
797static const struct mtk_i2c_soc_data mt7986_soc_data = {
798 .regs = mt_i2c_regs_v1,
799 .dma_sync = 1,
Weijie Gao7fa40df2023-07-19 17:16:15 +0800800 .ltiming_adjust = 0,
Weijie Gao9ad71f62022-09-09 19:59:48 +0800801};
802
803static const struct mtk_i2c_soc_data mt8183_soc_data = {
804 .regs = mt_i2c_regs_v2,
805 .dma_sync = 1,
Weijie Gao7fa40df2023-07-19 17:16:15 +0800806 .ltiming_adjust = 0,
Weijie Gao9ad71f62022-09-09 19:59:48 +0800807};
808
809static const struct mtk_i2c_soc_data mt8518_soc_data = {
810 .regs = mt_i2c_regs_v1,
811 .dma_sync = 0,
Weijie Gao7fa40df2023-07-19 17:16:15 +0800812 .ltiming_adjust = 0,
Weijie Gao9ad71f62022-09-09 19:59:48 +0800813};
814
815static const struct mtk_i2c_soc_data mt8512_soc_data = {
816 .regs = mt_i2c_regs_v1,
817 .dma_sync = 1,
Weijie Gao7fa40df2023-07-19 17:16:15 +0800818 .ltiming_adjust = 0,
Weijie Gao9ad71f62022-09-09 19:59:48 +0800819};
820
821static const struct dm_i2c_ops mtk_i2c_ops = {
822 .xfer = mtk_i2c_transfer,
823 .set_bus_speed = mtk_i2c_set_speed,
824 .deblock = mtk_i2c_deblock,
825};
826
827static const struct udevice_id mtk_i2c_ids[] = {
828 {
829 .compatible = "mediatek,mt7622-i2c",
830 .data = (ulong)&mt76xx_soc_data,
831 }, {
832 .compatible = "mediatek,mt7623-i2c",
833 .data = (ulong)&mt76xx_soc_data,
834 }, {
835 .compatible = "mediatek,mt7629-i2c",
836 .data = (ulong)&mt76xx_soc_data,
837 }, {
838 .compatible = "mediatek,mt7981-i2c",
839 .data = (ulong)&mt7981_soc_data,
840 }, {
841 .compatible = "mediatek,mt7986-i2c",
842 .data = (ulong)&mt7986_soc_data,
843 }, {
844 .compatible = "mediatek,mt8183-i2c",
845 .data = (ulong)&mt8183_soc_data,
846 }, {
847 .compatible = "mediatek,mt8512-i2c",
848 .data = (ulong)&mt8512_soc_data,
849 }, {
850 .compatible = "mediatek,mt8518-i2c",
851 .data = (ulong)&mt8518_soc_data,
852 }
853};
854
855U_BOOT_DRIVER(mtk_i2c) = {
856 .name = "mtk_i2c",
857 .id = UCLASS_I2C,
858 .of_match = mtk_i2c_ids,
859 .of_to_plat = mtk_i2c_of_to_plat,
860 .probe = mtk_i2c_probe,
861 .priv_auto = sizeof(struct mtk_i2c_priv),
862 .ops = &mtk_i2c_ops,
863};