xref: /linux/drivers/i2c/busses/i2c-designware-master.c (revision 73dae605cad7921800677d982bfaeb81cc930dec)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Synopsys DesignWare I2C adapter driver (master only).
4  *
5  * Based on the TI DAVINCI I2C adapter driver.
6  *
7  * Copyright (C) 2006 Texas Instruments.
8  * Copyright (C) 2007 MontaVista Software Inc.
9  * Copyright (C) 2009 Provigent Ltd.
10  */
11 
12 #define DEFAULT_SYMBOL_NAMESPACE	"I2C_DW"
13 
14 #include <linux/delay.h>
15 #include <linux/err.h>
16 #include <linux/errno.h>
17 #include <linux/export.h>
18 #include <linux/gpio/consumer.h>
19 #include <linux/i2c.h>
20 #include <linux/interrupt.h>
21 #include <linux/io.h>
22 #include <linux/module.h>
23 #include <linux/pinctrl/consumer.h>
24 #include <linux/pm_runtime.h>
25 #include <linux/regmap.h>
26 #include <linux/reset.h>
27 
28 #include <linux/designware_i2c.h>
29 
30 #include "i2c-designware-core.h"
31 
32 #define AMD_TIMEOUT_MIN_US	25
33 #define AMD_TIMEOUT_MAX_US	250
34 #define AMD_MASTERCFG_MASK	GENMASK(15, 0)
35 
36 static int i2c_dw_set_timings_master(struct dw_i2c_dev *dev)
37 {
38 	unsigned int comp_param1;
39 	u32 sda_falling_time, scl_falling_time;
40 	struct i2c_timings *t = &dev->timings;
41 	const char *fp_str = "";
42 	u32 ic_clk;
43 	int ret;
44 
45 	ret = i2c_dw_acquire_lock(dev);
46 	if (ret)
47 		return ret;
48 
49 	ret = regmap_read(dev->map, DW_IC_COMP_PARAM_1, &comp_param1);
50 	i2c_dw_release_lock(dev);
51 	if (ret)
52 		return ret;
53 
54 	/* Set standard and fast speed dividers for high/low periods */
55 	sda_falling_time = t->sda_fall_ns ?: 300; /* ns */
56 	scl_falling_time = t->scl_fall_ns ?: 300; /* ns */
57 
58 	/* Calculate SCL timing parameters for standard mode if not set */
59 	if (!dev->ss_hcnt || !dev->ss_lcnt) {
60 		ic_clk = i2c_dw_clk_rate(dev);
61 		dev->ss_hcnt =
62 			i2c_dw_scl_hcnt(dev,
63 					DW_IC_SS_SCL_HCNT,
64 					ic_clk,
65 					4000,	/* tHD;STA = tHIGH = 4.0 us */
66 					sda_falling_time,
67 					0);	/* No offset */
68 		dev->ss_lcnt =
69 			i2c_dw_scl_lcnt(dev,
70 					DW_IC_SS_SCL_LCNT,
71 					ic_clk,
72 					4700,	/* tLOW = 4.7 us */
73 					scl_falling_time,
74 					0);	/* No offset */
75 	}
76 	dev_dbg(dev->dev, "Standard Mode HCNT:LCNT = %d:%d\n",
77 		dev->ss_hcnt, dev->ss_lcnt);
78 
79 	/*
80 	 * Set SCL timing parameters for fast mode or fast mode plus. Only
81 	 * difference is the timing parameter values since the registers are
82 	 * the same.
83 	 */
84 	if (t->bus_freq_hz == I2C_MAX_FAST_MODE_PLUS_FREQ) {
85 		/*
86 		 * Check are Fast Mode Plus parameters available. Calculate
87 		 * SCL timing parameters for Fast Mode Plus if not set.
88 		 */
89 		if (dev->fp_hcnt && dev->fp_lcnt) {
90 			dev->fs_hcnt = dev->fp_hcnt;
91 			dev->fs_lcnt = dev->fp_lcnt;
92 		} else {
93 			ic_clk = i2c_dw_clk_rate(dev);
94 			dev->fs_hcnt =
95 				i2c_dw_scl_hcnt(dev,
96 						DW_IC_FS_SCL_HCNT,
97 						ic_clk,
98 						260,	/* tHIGH = 260 ns */
99 						sda_falling_time,
100 						0);	/* No offset */
101 			dev->fs_lcnt =
102 				i2c_dw_scl_lcnt(dev,
103 						DW_IC_FS_SCL_LCNT,
104 						ic_clk,
105 						500,	/* tLOW = 500 ns */
106 						scl_falling_time,
107 						0);	/* No offset */
108 		}
109 		fp_str = " Plus";
110 	}
111 	/*
112 	 * Calculate SCL timing parameters for fast mode if not set. They are
113 	 * needed also in high speed mode.
114 	 */
115 	if (!dev->fs_hcnt || !dev->fs_lcnt) {
116 		ic_clk = i2c_dw_clk_rate(dev);
117 		dev->fs_hcnt =
118 			i2c_dw_scl_hcnt(dev,
119 					DW_IC_FS_SCL_HCNT,
120 					ic_clk,
121 					600,	/* tHD;STA = tHIGH = 0.6 us */
122 					sda_falling_time,
123 					0);	/* No offset */
124 		dev->fs_lcnt =
125 			i2c_dw_scl_lcnt(dev,
126 					DW_IC_FS_SCL_LCNT,
127 					ic_clk,
128 					1300,	/* tLOW = 1.3 us */
129 					scl_falling_time,
130 					0);	/* No offset */
131 	}
132 	dev_dbg(dev->dev, "Fast Mode%s HCNT:LCNT = %d:%d\n",
133 		fp_str, dev->fs_hcnt, dev->fs_lcnt);
134 
135 	/* Check is high speed possible and fall back to fast mode if not */
136 	if ((dev->master_cfg & DW_IC_CON_SPEED_MASK) ==
137 		DW_IC_CON_SPEED_HIGH) {
138 		if ((comp_param1 & DW_IC_COMP_PARAM_1_SPEED_MODE_MASK)
139 			!= DW_IC_COMP_PARAM_1_SPEED_MODE_HIGH) {
140 			dev_err(dev->dev, "High Speed not supported!\n");
141 			t->bus_freq_hz = I2C_MAX_FAST_MODE_FREQ;
142 			dev->master_cfg &= ~DW_IC_CON_SPEED_MASK;
143 			dev->master_cfg |= DW_IC_CON_SPEED_FAST;
144 			dev->hs_hcnt = 0;
145 			dev->hs_lcnt = 0;
146 		} else if (!dev->hs_hcnt || !dev->hs_lcnt) {
147 			u32 t_high, t_low;
148 
149 			/*
150 			 * The legal values stated in the databook for bus
151 			 * capacitance are only 100pF and 400pF.
152 			 * If dev->bus_capacitance_pF is greater than or equals
153 			 * to 400, t_high and t_low are assumed to be
154 			 * appropriate values for 400pF, otherwise 100pF.
155 			 */
156 			if (dev->bus_capacitance_pF >= 400) {
157 				/* assume bus capacitance is 400pF */
158 				t_high = dev->clk_freq_optimized ? 160 : 120;
159 				t_low = 320;
160 			} else {
161 				/* assume bus capacitance is 100pF */
162 				t_high = 60;
163 				t_low = dev->clk_freq_optimized ? 120 : 160;
164 			}
165 
166 			ic_clk = i2c_dw_clk_rate(dev);
167 			dev->hs_hcnt =
168 				i2c_dw_scl_hcnt(dev,
169 						DW_IC_HS_SCL_HCNT,
170 						ic_clk,
171 						t_high,
172 						sda_falling_time,
173 						0);	/* No offset */
174 			dev->hs_lcnt =
175 				i2c_dw_scl_lcnt(dev,
176 						DW_IC_HS_SCL_LCNT,
177 						ic_clk,
178 						t_low,
179 						scl_falling_time,
180 						0);	/* No offset */
181 		}
182 		dev_dbg(dev->dev, "High Speed Mode HCNT:LCNT = %d:%d\n",
183 			dev->hs_hcnt, dev->hs_lcnt);
184 	}
185 
186 	dev_dbg(dev->dev, "Bus speed: %s\n", i2c_freq_mode_string(t->bus_freq_hz));
187 	return 0;
188 }
189 
190 static void i2c_dw_xfer_init(struct dw_i2c_dev *dev)
191 {
192 	struct i2c_msg *msgs = dev->msgs;
193 	u32 ic_con = 0, ic_tar = 0;
194 	unsigned int dummy;
195 
196 	/* Disable the adapter */
197 	__i2c_dw_disable(dev);
198 
199 	i2c_dw_set_mode(dev, DW_IC_MASTER);
200 
201 	/* If the slave address is ten bit address, enable 10BITADDR */
202 	if (msgs[dev->msg_write_idx].flags & I2C_M_TEN) {
203 		ic_con = DW_IC_CON_10BITADDR_MASTER;
204 		/*
205 		 * If I2C_DYNAMIC_TAR_UPDATE is set, the 10-bit addressing
206 		 * mode has to be enabled via bit 12 of IC_TAR register.
207 		 * We set it always as I2C_DYNAMIC_TAR_UPDATE can't be
208 		 * detected from registers.
209 		 */
210 		ic_tar = DW_IC_TAR_10BITADDR_MASTER;
211 	}
212 
213 	regmap_update_bits(dev->map, DW_IC_CON, DW_IC_CON_10BITADDR_MASTER,
214 			   ic_con);
215 
216 	/*
217 	 * Set the slave (target) address and enable 10-bit addressing mode
218 	 * if applicable.
219 	 */
220 	regmap_write(dev->map, DW_IC_TAR,
221 		     msgs[dev->msg_write_idx].addr | ic_tar);
222 
223 	/* Enforce disabled interrupts (due to HW issues) */
224 	__i2c_dw_write_intr_mask(dev, 0);
225 
226 	/* Enable the adapter */
227 	__i2c_dw_enable(dev);
228 
229 	/* Dummy read to avoid the register getting stuck on Bay Trail */
230 	regmap_read(dev->map, DW_IC_ENABLE_STATUS, &dummy);
231 
232 	/* Clear and enable interrupts */
233 	regmap_read(dev->map, DW_IC_CLR_INTR, &dummy);
234 	__i2c_dw_write_intr_mask(dev, DW_IC_INTR_MASTER_MASK);
235 }
236 
237 /*
238  * This function waits for the controller to be idle before disabling I2C
239  * When the controller is not in the IDLE state, the MST_ACTIVITY bit
240  * (IC_STATUS[5]) is set.
241  *
242  * Values:
243  * 0x1 (ACTIVE): Controller not idle
244  * 0x0 (IDLE): Controller is idle
245  *
246  * The function is called after completing the current transfer.
247  *
248  * Returns:
249  * False when the controller is in the IDLE state.
250  * True when the controller is in the ACTIVE state.
251  */
252 static bool i2c_dw_is_controller_active(struct dw_i2c_dev *dev)
253 {
254 	u32 status;
255 
256 	regmap_read(dev->map, DW_IC_STATUS, &status);
257 	if (!(status & DW_IC_STATUS_MASTER_ACTIVITY))
258 		return false;
259 
260 	return regmap_read_poll_timeout(dev->map, DW_IC_STATUS, status,
261 				       !(status & DW_IC_STATUS_MASTER_ACTIVITY),
262 				       1100, 20000) != 0;
263 }
264 
265 static int i2c_dw_check_stopbit(struct dw_i2c_dev *dev)
266 {
267 	u32 val;
268 	int ret;
269 
270 	ret = regmap_read_poll_timeout(dev->map, DW_IC_INTR_STAT, val,
271 				       !(val & DW_IC_INTR_STOP_DET),
272 					1100, 20000);
273 	if (ret)
274 		dev_err(dev->dev, "i2c timeout error %d\n", ret);
275 
276 	return ret;
277 }
278 
279 static int i2c_dw_status(struct dw_i2c_dev *dev)
280 {
281 	int status;
282 
283 	status = i2c_dw_wait_bus_not_busy(dev);
284 	if (status)
285 		return status;
286 
287 	return i2c_dw_check_stopbit(dev);
288 }
289 
290 /*
291  * Initiate and continue master read/write transaction with polling
292  * based transfer routine afterward write messages into the Tx buffer.
293  */
294 static int amd_i2c_dw_xfer_quirk(struct dw_i2c_dev *dev, struct i2c_msg *msgs, int num_msgs)
295 {
296 	int msg_wrt_idx, msg_itr_lmt, buf_len, data_idx;
297 	int cmd = 0, status;
298 	u8 *tx_buf;
299 	unsigned int val;
300 
301 	PM_RUNTIME_ACQUIRE_AUTOSUSPEND(dev->dev, pm);
302 	if (PM_RUNTIME_ACQUIRE_ERR(&pm))
303 		return -ENXIO;
304 
305 	/*
306 	 * In order to enable the interrupt for UCSI i.e. AMD NAVI GPU card,
307 	 * it is mandatory to set the right value in specific register
308 	 * (offset:0x474) as per the hardware IP specification.
309 	 */
310 	regmap_write(dev->map, AMD_UCSI_INTR_REG, AMD_UCSI_INTR_EN);
311 
312 	dev->msgs = msgs;
313 	dev->msgs_num = num_msgs;
314 	dev->msg_write_idx = 0;
315 	i2c_dw_xfer_init(dev);
316 
317 	/* Initiate messages read/write transaction */
318 	for (msg_wrt_idx = 0; msg_wrt_idx < num_msgs; msg_wrt_idx++) {
319 		tx_buf = msgs[msg_wrt_idx].buf;
320 		buf_len = msgs[msg_wrt_idx].len;
321 
322 		if (!(msgs[msg_wrt_idx].flags & I2C_M_RD))
323 			regmap_write(dev->map, DW_IC_TX_TL, buf_len - 1);
324 		/*
325 		 * Initiate the i2c read/write transaction of buffer length,
326 		 * and poll for bus busy status. For the last message transfer,
327 		 * update the command with stop bit enable.
328 		 */
329 		for (msg_itr_lmt = buf_len; msg_itr_lmt > 0; msg_itr_lmt--) {
330 			if (msg_wrt_idx == num_msgs - 1 && msg_itr_lmt == 1)
331 				cmd |= BIT(9);
332 
333 			if (msgs[msg_wrt_idx].flags & I2C_M_RD) {
334 				/* Due to hardware bug, need to write the same command twice. */
335 				regmap_write(dev->map, DW_IC_DATA_CMD, 0x100);
336 				regmap_write(dev->map, DW_IC_DATA_CMD, 0x100 | cmd);
337 				if (cmd) {
338 					regmap_write(dev->map, DW_IC_TX_TL, 2 * (buf_len - 1));
339 					regmap_write(dev->map, DW_IC_RX_TL, 2 * (buf_len - 1));
340 					/*
341 					 * Need to check the stop bit. However, it cannot be
342 					 * detected from the registers so we check it always
343 					 * when read/write the last byte.
344 					 */
345 					status = i2c_dw_status(dev);
346 					if (status)
347 						return status;
348 
349 					for (data_idx = 0; data_idx < buf_len; data_idx++) {
350 						regmap_read(dev->map, DW_IC_DATA_CMD, &val);
351 						tx_buf[data_idx] = val;
352 					}
353 					status = i2c_dw_check_stopbit(dev);
354 					if (status)
355 						return status;
356 				}
357 			} else {
358 				regmap_write(dev->map, DW_IC_DATA_CMD, *tx_buf++ | cmd);
359 				usleep_range(AMD_TIMEOUT_MIN_US, AMD_TIMEOUT_MAX_US);
360 			}
361 		}
362 		status = i2c_dw_check_stopbit(dev);
363 		if (status)
364 			return status;
365 	}
366 
367 	return 0;
368 }
369 
370 /*
371  * Initiate (and continue) low level master read/write transaction.
372  * This function is only called from i2c_dw_isr(), and pumping i2c_msg
373  * messages into the tx buffer.  Even if the size of i2c_msg data is
374  * longer than the size of the tx buffer, it handles everything.
375  */
376 static void
377 i2c_dw_xfer_msg(struct dw_i2c_dev *dev)
378 {
379 	struct i2c_msg *msgs = dev->msgs;
380 	u32 intr_mask;
381 	int tx_limit, rx_limit, rx_tl;
382 	u32 buf_len = dev->tx_buf_len;
383 	u8 *buf = dev->tx_buf;
384 	bool need_restart = false;
385 	unsigned int flr;
386 
387 	intr_mask = DW_IC_INTR_MASTER_MASK;
388 
389 	for (; dev->msg_write_idx < dev->msgs_num; dev->msg_write_idx++) {
390 		u32 flags = msgs[dev->msg_write_idx].flags;
391 
392 		if (!(dev->status & STATUS_WRITE_IN_PROGRESS)) {
393 			/* new i2c_msg */
394 			buf = msgs[dev->msg_write_idx].buf;
395 			buf_len = msgs[dev->msg_write_idx].len;
396 
397 			/*
398 			 * If both IC_EMPTYFIFO_HOLD_MASTER_EN and
399 			 * IC_RESTART_EN are set, we must manually
400 			 * set restart bit between messages.
401 			 */
402 			if ((dev->master_cfg & DW_IC_CON_RESTART_EN) &&
403 					(dev->msg_write_idx > 0))
404 				need_restart = true;
405 		}
406 
407 		regmap_read(dev->map, DW_IC_TXFLR, &flr);
408 		tx_limit = dev->tx_fifo_depth - flr;
409 
410 		regmap_read(dev->map, DW_IC_RXFLR, &flr);
411 		rx_limit = dev->rx_fifo_depth - flr;
412 
413 		while (buf_len > 0 && tx_limit > 0 && rx_limit > 0) {
414 			u32 cmd = 0;
415 
416 			/*
417 			 * If IC_EMPTYFIFO_HOLD_MASTER_EN is set we must
418 			 * manually set the stop bit. However, it cannot be
419 			 * detected from the registers so we set it always
420 			 * when writing/reading the last byte.
421 			 */
422 
423 			/*
424 			 * i2c-core always sets the buffer length of
425 			 * I2C_FUNC_SMBUS_BLOCK_DATA to 1. The length will
426 			 * be adjusted when receiving the first byte.
427 			 * Thus we can't stop the transaction here.
428 			 */
429 			if (dev->msg_write_idx == dev->msgs_num - 1 &&
430 			    buf_len == 1 && !(flags & I2C_M_RECV_LEN))
431 				cmd |= BIT(9);
432 
433 			if (need_restart) {
434 				cmd |= BIT(10);
435 				need_restart = false;
436 			}
437 
438 			if (msgs[dev->msg_write_idx].flags & I2C_M_RD) {
439 
440 				/* Avoid rx buffer overrun */
441 				if (dev->rx_outstanding >= dev->rx_fifo_depth)
442 					break;
443 
444 				/*
445 				 * For block reads, queue only the length
446 				 * byte until i2c_dw_recv_len() runs.
447 				 */
448 				if ((flags & I2C_M_RECV_LEN) &&
449 				    buf_len < msgs[dev->msg_write_idx].len)
450 					break;
451 
452 				regmap_write(dev->map, DW_IC_DATA_CMD,
453 					     cmd | 0x100);
454 				rx_limit--;
455 				dev->rx_outstanding++;
456 			} else {
457 				regmap_write(dev->map, DW_IC_DATA_CMD,
458 					     cmd | *buf++);
459 			}
460 			tx_limit--; buf_len--;
461 		}
462 
463 		dev->tx_buf = buf;
464 		dev->tx_buf_len = buf_len;
465 
466 		/*
467 		 * Because we don't know the buffer length in the
468 		 * I2C_FUNC_SMBUS_BLOCK_DATA case, we can't stop the
469 		 * transaction here. Also disable the TX_EMPTY IRQ
470 		 * while waiting for the data length byte to avoid the
471 		 * bogus interrupts flood.
472 		 */
473 		if (flags & I2C_M_RECV_LEN) {
474 			dev->status |= STATUS_WRITE_IN_PROGRESS;
475 			intr_mask &= ~DW_IC_INTR_TX_EMPTY;
476 			break;
477 		} else if (buf_len > 0) {
478 			/* more bytes to be written */
479 			dev->status |= STATUS_WRITE_IN_PROGRESS;
480 			break;
481 		} else
482 			dev->status &= ~STATUS_WRITE_IN_PROGRESS;
483 	}
484 
485 	/*
486 	 * If i2c_msg index search is completed, we don't need TX_EMPTY
487 	 * interrupt any more.
488 	 */
489 	if (dev->msg_write_idx == dev->msgs_num)
490 		intr_mask &= ~DW_IC_INTR_TX_EMPTY;
491 
492 	if (dev->msg_err)
493 		intr_mask = 0;
494 
495 	/*
496 	 * Size the RX FIFO threshold to the reads already queued, so the
497 	 * controller raises one RX_FULL for the whole burst instead of one per
498 	 * received byte. While messages remain to be queued, cap it so RX_FULL
499 	 * still arrives in time to drain the FIFO and let the next TX_EMPTY
500 	 * queue the rest.
501 	 */
502 	rx_tl = dev->rx_outstanding;
503 	if (dev->msg_write_idx < dev->msgs_num)
504 		rx_tl = min_t(int, rx_tl, dev->rx_fifo_depth / 2);
505 
506 	regmap_write(dev->map, DW_IC_RX_TL, rx_tl ? rx_tl - 1 : 0);
507 
508 	__i2c_dw_write_intr_mask(dev, intr_mask);
509 }
510 
511 static u8
512 i2c_dw_recv_len(struct dw_i2c_dev *dev, u8 len)
513 {
514 	struct i2c_msg *msgs = dev->msgs;
515 	u32 flags = msgs[dev->msg_read_idx].flags;
516 	unsigned int intr_mask;
517 
518 	/*
519 	 * Adjust the buffer length and mask the flag
520 	 * after receiving the first byte.
521 	 */
522 	len += (flags & I2C_CLIENT_PEC) ? 2 : 1;
523 	dev->tx_buf_len = len - min(len, dev->rx_outstanding);
524 	msgs[dev->msg_read_idx].len = len;
525 	msgs[dev->msg_read_idx].flags &= ~I2C_M_RECV_LEN;
526 
527 	/*
528 	 * Received buffer length, re-enable TX_EMPTY interrupt
529 	 * to resume the SMBUS transaction.
530 	 */
531 	__i2c_dw_read_intr_mask(dev, &intr_mask);
532 	intr_mask |= DW_IC_INTR_TX_EMPTY;
533 	__i2c_dw_write_intr_mask(dev, intr_mask);
534 
535 	return len;
536 }
537 
538 static void
539 i2c_dw_read(struct dw_i2c_dev *dev)
540 {
541 	struct i2c_msg *msgs = dev->msgs;
542 	unsigned int rx_valid;
543 
544 	for (; dev->msg_read_idx < dev->msgs_num; dev->msg_read_idx++) {
545 		u32 flags = msgs[dev->msg_read_idx].flags;
546 		unsigned int tmp;
547 		u32 len;
548 		u8 *buf;
549 
550 		if (!(flags & I2C_M_RD))
551 			continue;
552 
553 		if (!(dev->status & STATUS_READ_IN_PROGRESS)) {
554 			len = msgs[dev->msg_read_idx].len;
555 			buf = msgs[dev->msg_read_idx].buf;
556 		} else {
557 			len = dev->rx_buf_len;
558 			buf = dev->rx_buf;
559 		}
560 
561 		regmap_read(dev->map, DW_IC_RXFLR, &rx_valid);
562 
563 		for (; len > 0 && rx_valid > 0; len--, rx_valid--) {
564 			regmap_read(dev->map, DW_IC_DATA_CMD, &tmp);
565 			tmp &= DW_IC_DATA_CMD_DAT;
566 			/* Ensure length byte is a valid value */
567 			if (flags & I2C_M_RECV_LEN) {
568 				/*
569 				 * if IC_EMPTYFIFO_HOLD_MASTER_EN is set, which cannot be
570 				 * detected from the registers, the controller can be
571 				 * disabled if the STOP bit is set. But it is only set
572 				 * after receiving block data response length in
573 				 * I2C_FUNC_SMBUS_BLOCK_DATA case. That needs to read
574 				 * another byte with STOP bit set when the block data
575 				 * response length is invalid to complete the transaction.
576 				 */
577 				if (!tmp || tmp > I2C_SMBUS_BLOCK_MAX)
578 					tmp = 1;
579 
580 				len = i2c_dw_recv_len(dev, tmp);
581 			}
582 			*buf++ = tmp;
583 			dev->rx_outstanding--;
584 		}
585 
586 		if (len > 0) {
587 			dev->status |= STATUS_READ_IN_PROGRESS;
588 			dev->rx_buf_len = len;
589 			dev->rx_buf = buf;
590 			return;
591 		} else
592 			dev->status &= ~STATUS_READ_IN_PROGRESS;
593 	}
594 }
595 
596 static u32 i2c_dw_read_clear_intrbits(struct dw_i2c_dev *dev)
597 {
598 	unsigned int stat, dummy;
599 
600 	/*
601 	 * The IC_INTR_STAT register just indicates "enabled" interrupts.
602 	 * The unmasked raw version of interrupt status bits is available
603 	 * in the IC_RAW_INTR_STAT register.
604 	 *
605 	 * That is,
606 	 *   stat = readl(IC_INTR_STAT);
607 	 * equals to,
608 	 *   stat = readl(IC_RAW_INTR_STAT) & readl(IC_INTR_MASK);
609 	 *
610 	 * The raw version might be useful for debugging purposes.
611 	 */
612 	if (!(dev->flags & ACCESS_POLLING)) {
613 		regmap_read(dev->map, DW_IC_INTR_STAT, &stat);
614 	} else {
615 		regmap_read(dev->map, DW_IC_RAW_INTR_STAT, &stat);
616 		stat &= dev->sw_mask;
617 	}
618 
619 	/*
620 	 * Do not use the IC_CLR_INTR register to clear interrupts, or
621 	 * you'll miss some interrupts, triggered during the period from
622 	 * readl(IC_INTR_STAT) to readl(IC_CLR_INTR).
623 	 *
624 	 * Instead, use the separately-prepared IC_CLR_* registers.
625 	 */
626 	if (stat & DW_IC_INTR_RX_UNDER)
627 		regmap_read(dev->map, DW_IC_CLR_RX_UNDER, &dummy);
628 	if (stat & DW_IC_INTR_RX_OVER)
629 		regmap_read(dev->map, DW_IC_CLR_RX_OVER, &dummy);
630 	if (stat & DW_IC_INTR_TX_OVER)
631 		regmap_read(dev->map, DW_IC_CLR_TX_OVER, &dummy);
632 	if (stat & DW_IC_INTR_RD_REQ)
633 		regmap_read(dev->map, DW_IC_CLR_RD_REQ, &dummy);
634 	if (stat & DW_IC_INTR_TX_ABRT) {
635 		/*
636 		 * The IC_TX_ABRT_SOURCE register is cleared whenever
637 		 * the IC_CLR_TX_ABRT is read.  Preserve it beforehand.
638 		 */
639 		regmap_read(dev->map, DW_IC_TX_ABRT_SOURCE, &dev->abort_source);
640 		regmap_read(dev->map, DW_IC_CLR_TX_ABRT, &dummy);
641 	}
642 	if (stat & DW_IC_INTR_RX_DONE)
643 		regmap_read(dev->map, DW_IC_CLR_RX_DONE, &dummy);
644 	if (stat & DW_IC_INTR_ACTIVITY)
645 		regmap_read(dev->map, DW_IC_CLR_ACTIVITY, &dummy);
646 	if ((stat & DW_IC_INTR_STOP_DET) &&
647 	    ((dev->rx_outstanding == 0) || (stat & DW_IC_INTR_RX_FULL)))
648 		regmap_read(dev->map, DW_IC_CLR_STOP_DET, &dummy);
649 	if (stat & DW_IC_INTR_START_DET)
650 		regmap_read(dev->map, DW_IC_CLR_START_DET, &dummy);
651 	if (stat & DW_IC_INTR_GEN_CALL)
652 		regmap_read(dev->map, DW_IC_CLR_GEN_CALL, &dummy);
653 
654 	return stat;
655 }
656 
657 static void i2c_dw_process_transfer(struct dw_i2c_dev *dev, unsigned int stat)
658 {
659 	if (stat & DW_IC_INTR_TX_ABRT) {
660 		dev->cmd_err |= DW_IC_ERR_TX_ABRT;
661 		dev->status &= ~STATUS_MASK;
662 		dev->rx_outstanding = 0;
663 
664 		/*
665 		 * Anytime TX_ABRT is set, the contents of the tx/rx
666 		 * buffers are flushed. Make sure to skip them.
667 		 */
668 		__i2c_dw_write_intr_mask(dev, 0);
669 		goto tx_aborted;
670 	}
671 
672 	if (stat & DW_IC_INTR_RX_FULL)
673 		i2c_dw_read(dev);
674 
675 	if (stat & DW_IC_INTR_TX_EMPTY)
676 		i2c_dw_xfer_msg(dev);
677 
678 	/* Abort if we detect a STOP in the middle of a read or a write */
679 	if ((stat & DW_IC_INTR_STOP_DET) &&
680 	    (dev->status & (STATUS_READ_IN_PROGRESS | STATUS_WRITE_IN_PROGRESS))) {
681 		dev_err(dev->dev, "spurious STOP detected\n");
682 		dev->rx_outstanding = 0;
683 		dev->msg_err = -EIO;
684 	}
685 
686 	/*
687 	 * No need to modify or disable the interrupt mask here.
688 	 * i2c_dw_xfer_msg() will take care of it according to
689 	 * the current transmit status.
690 	 */
691 
692 tx_aborted:
693 	if (((stat & (DW_IC_INTR_TX_ABRT | DW_IC_INTR_STOP_DET)) || dev->msg_err) &&
694 	     (dev->rx_outstanding == 0))
695 		complete(&dev->cmd_complete);
696 	else if (unlikely(dev->flags & ACCESS_INTR_MASK)) {
697 		/* Workaround to trigger pending interrupt */
698 		__i2c_dw_read_intr_mask(dev, &stat);
699 		__i2c_dw_write_intr_mask(dev, 0);
700 		__i2c_dw_write_intr_mask(dev, stat);
701 	}
702 }
703 
704 /*
705  * Interrupt service routine. This gets called whenever an I2C master interrupt
706  * occurs.
707  */
708 irqreturn_t i2c_dw_isr_master(struct dw_i2c_dev *dev)
709 {
710 	unsigned int stat, enabled;
711 
712 	regmap_read(dev->map, DW_IC_ENABLE, &enabled);
713 	regmap_read(dev->map, DW_IC_RAW_INTR_STAT, &stat);
714 	if (!enabled || !(stat & ~DW_IC_INTR_ACTIVITY))
715 		return IRQ_NONE;
716 	if (pm_runtime_suspended(dev->dev) || stat == GENMASK(31, 0))
717 		return IRQ_NONE;
718 	dev_dbg(dev->dev, "enabled=%#x stat=%#x\n", enabled, stat);
719 
720 	stat = i2c_dw_read_clear_intrbits(dev);
721 
722 	if (!(dev->status & STATUS_ACTIVE)) {
723 		/*
724 		 * Unexpected interrupt in driver point of view. State
725 		 * variables are either unset or stale so acknowledge and
726 		 * disable interrupts for suppressing further interrupts if
727 		 * interrupt really came from this HW (E.g. firmware has left
728 		 * the HW active).
729 		 */
730 		__i2c_dw_write_intr_mask(dev, 0);
731 		return IRQ_HANDLED;
732 	}
733 
734 	i2c_dw_process_transfer(dev, stat);
735 
736 	return IRQ_HANDLED;
737 }
738 
739 static int i2c_dw_wait_transfer(struct dw_i2c_dev *dev)
740 {
741 	unsigned long timeout = dev->adapter.timeout;
742 	unsigned int stat;
743 	int ret;
744 
745 	if (!(dev->flags & ACCESS_POLLING)) {
746 		ret = wait_for_completion_timeout(&dev->cmd_complete, timeout);
747 	} else {
748 		timeout += jiffies;
749 		do {
750 			ret = try_wait_for_completion(&dev->cmd_complete);
751 			if (ret)
752 				break;
753 
754 			stat = i2c_dw_read_clear_intrbits(dev);
755 			if (stat)
756 				i2c_dw_process_transfer(dev, stat);
757 			else
758 				/* Try save some power */
759 				usleep_range(3, 25);
760 		} while (time_before(jiffies, timeout));
761 	}
762 
763 	return ret ? 0 : -ETIMEDOUT;
764 }
765 
766 /*
767  * Prepare controller for a transaction, start the transfer of the @msgs
768  * and wait for completion, either a STOP or a error.
769  * Return: 0 or a negative error code.
770  */
771 static int
772 __i2c_dw_xfer_one_part(struct dw_i2c_dev *dev, struct i2c_msg *msgs, size_t num)
773 {
774 	int ret;
775 
776 	reinit_completion(&dev->cmd_complete);
777 	dev->msgs = msgs;
778 	dev->msgs_num = num;
779 	dev->cmd_err = 0;
780 	dev->msg_write_idx = 0;
781 	dev->msg_read_idx = 0;
782 	dev->msg_err = 0;
783 	dev->status = 0;
784 	dev->abort_source = 0;
785 	dev->rx_outstanding = 0;
786 
787 	ret = i2c_dw_wait_bus_not_busy(dev);
788 	if (ret < 0)
789 		return ret;
790 
791 	/* Start the transfers */
792 	i2c_dw_xfer_init(dev);
793 
794 	/* Wait for tx to complete */
795 	ret = i2c_dw_wait_transfer(dev);
796 	if (ret) {
797 		dev_err(dev->dev, "controller timed out\n");
798 		/* i2c_dw_init() implicitly disables the adapter */
799 		i2c_recover_bus(&dev->adapter);
800 		i2c_dw_init(dev);
801 		return ret;
802 	}
803 
804 	/*
805 	 * This happens rarely (~1:500) and is hard to reproduce. Debug trace
806 	 * showed that IC_STATUS had value of 0x23 when STOP_DET occurred,
807 	 * if disable IC_ENABLE.ENABLE immediately that can result in
808 	 * IC_RAW_INTR_STAT.MASTER_ON_HOLD holding SCL low. Check if
809 	 * controller is still ACTIVE before disabling I2C.
810 	 */
811 	if (i2c_dw_is_controller_active(dev)) {
812 		/*
813 		 * If the controller is still active after the timeout, attempt a
814 		 * bus recovery to clear any potentially locked state.
815 		 */
816 		dev_err(dev->dev, "controller active after xfer, recovering\n");
817 		i2c_recover_bus(&dev->adapter);
818 		i2c_dw_init(dev);
819 	} else {
820 		/*
821 		 * We must disable the adapter before returning and signaling the end
822 		 * of the current transfer. Otherwise the hardware might continue
823 		 * generating interrupts which in turn causes a race condition with
824 		 * the following transfer. Needs some more investigation if the
825 		 * additional interrupts are a hardware bug or this driver doesn't
826 		 * handle them correctly yet.
827 		 */
828 		__i2c_dw_disable_nowait(dev);
829 	}
830 
831 	if (dev->msg_err)
832 		return dev->msg_err;
833 
834 	/* No error */
835 	if (likely(!dev->cmd_err && !dev->status))
836 		return 0;
837 
838 	/* We have an error */
839 	if (dev->cmd_err == DW_IC_ERR_TX_ABRT)
840 		return i2c_dw_handle_tx_abort(dev);
841 
842 	if (dev->status)
843 		dev_err(dev->dev,
844 			"transfer terminated early - interrupt latency too high?\n");
845 
846 	return -EIO;
847 }
848 
849 /*
850  * Verify that the message at index @idx can be processed as part
851  * of a single transaction. The @msgs array contains the messages
852  * of the transaction. The message is checked against its predecessor
853  * to ensure that it respects the limitation of the controller.
854  * Return: true if the message can be processed, false otherwise.
855  */
856 static bool
857 i2c_dw_msg_is_valid(struct dw_i2c_dev *dev, const struct i2c_msg *msgs, size_t idx)
858 {
859 	/*
860 	 * The first message of a transaction is valid,
861 	 * no constraints from a previous message.
862 	 */
863 	if (!idx)
864 		return true;
865 
866 	/*
867 	 * We cannot change the target address during a transaction, so make
868 	 * sure the address is identical to the one of the previous message.
869 	 */
870 	if (msgs[idx - 1].addr != msgs[idx].addr) {
871 		dev_err(dev->dev, "invalid target address\n");
872 		return false;
873 	}
874 
875 	/*
876 	 * Make sure we don't need explicit RESTART between two messages
877 	 * in the same direction for controllers that cannot emit them.
878 	 */
879 	if (!dev->emptyfifo_hold_master &&
880 	    (msgs[idx - 1].flags & I2C_M_RD) == (msgs[idx].flags & I2C_M_RD)) {
881 		dev_err(dev->dev, "cannot emit RESTART\n");
882 		return false;
883 	}
884 
885 	return true;
886 }
887 
888 static int
889 i2c_dw_xfer_common(struct dw_i2c_dev *dev, struct i2c_msg msgs[], int num)
890 {
891 	struct i2c_msg *msgs_part;
892 	size_t cnt;
893 	int ret;
894 
895 	dev_dbg(dev->dev, "msgs: %d\n", num);
896 
897 	PM_RUNTIME_ACQUIRE_AUTOSUSPEND(dev->dev, pm);
898 	if (PM_RUNTIME_ACQUIRE_ERR(&pm))
899 		return -ENXIO;
900 
901 	ret = i2c_dw_acquire_lock(dev);
902 	if (ret)
903 		return ret;
904 
905 	/*
906 	 * If the I2C_M_STOP is present in some the messages,
907 	 * we do one transaction for each part up to the STOP.
908 	 */
909 	for (msgs_part = msgs; msgs_part < msgs + num; msgs_part += cnt) {
910 		/*
911 		 * Count the messages in a transaction, up to a STOP or
912 		 * the end of the msgs. The last if below guarantees that
913 		 * we check all messages and that msg_parts and cnt are
914 		 * in-bounds of msgs and num.
915 		 */
916 		for (cnt = 1; ; cnt++) {
917 			if (!i2c_dw_msg_is_valid(dev, msgs_part, cnt - 1)) {
918 				ret = -EINVAL;
919 				break;
920 			}
921 
922 			if ((msgs_part[cnt - 1].flags & I2C_M_STOP) ||
923 			    (msgs_part + cnt == msgs + num))
924 				break;
925 		}
926 		if (ret < 0)
927 			break;
928 
929 		/* transfer one part up to a STOP */
930 		ret = __i2c_dw_xfer_one_part(dev, msgs_part, cnt);
931 		if (ret < 0)
932 			break;
933 	}
934 
935 	i2c_dw_set_mode(dev, DW_IC_SLAVE);
936 
937 	i2c_dw_release_lock(dev);
938 
939 	if (ret < 0)
940 		return ret;
941 	return num;
942 }
943 
944 int i2c_dw_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
945 {
946 	struct dw_i2c_dev *dev = i2c_get_adapdata(adap);
947 
948 	if ((dev->flags & MODEL_MASK) == MODEL_AMD_NAVI_GPU)
949 		return amd_i2c_dw_xfer_quirk(dev, msgs, num);
950 
951 	return i2c_dw_xfer_common(dev, msgs, num);
952 }
953 
954 void i2c_dw_configure_master(struct dw_i2c_dev *dev)
955 {
956 	struct i2c_timings *t = &dev->timings;
957 
958 	dev->functionality |= I2C_FUNC_10BIT_ADDR | DW_IC_DEFAULT_FUNCTIONALITY;
959 
960 	/* amd_i2c_dw_xfer_quirk() does not implement protocol mangling */
961 	if ((dev->flags & MODEL_MASK) != MODEL_AMD_NAVI_GPU)
962 		dev->functionality |= I2C_FUNC_PROTOCOL_MANGLING;
963 
964 	dev->master_cfg = DW_IC_CON_MASTER | DW_IC_CON_SLAVE_DISABLE |
965 			  DW_IC_CON_RESTART_EN;
966 
967 	dev->mode = DW_IC_MASTER;
968 
969 	switch (t->bus_freq_hz) {
970 	case I2C_MAX_STANDARD_MODE_FREQ:
971 		dev->master_cfg |= DW_IC_CON_SPEED_STD;
972 		break;
973 	case I2C_MAX_HIGH_SPEED_MODE_FREQ:
974 		dev->master_cfg |= DW_IC_CON_SPEED_HIGH;
975 		break;
976 	default:
977 		dev->master_cfg |= DW_IC_CON_SPEED_FAST;
978 	}
979 }
980 EXPORT_SYMBOL_GPL(i2c_dw_configure_master);
981 
982 static void i2c_dw_prepare_recovery(struct i2c_adapter *adap)
983 {
984 	struct dw_i2c_dev *dev = i2c_get_adapdata(adap);
985 
986 	i2c_dw_disable(dev);
987 	reset_control_assert(dev->rst);
988 	i2c_dw_prepare_clk(dev, false);
989 }
990 
991 static void i2c_dw_unprepare_recovery(struct i2c_adapter *adap)
992 {
993 	struct dw_i2c_dev *dev = i2c_get_adapdata(adap);
994 
995 	i2c_dw_prepare_clk(dev, true);
996 	reset_control_deassert(dev->rst);
997 	i2c_dw_init(dev);
998 }
999 
1000 static int i2c_dw_init_recovery_info(struct dw_i2c_dev *dev)
1001 {
1002 	struct i2c_bus_recovery_info *rinfo = &dev->rinfo;
1003 	struct i2c_adapter *adap = &dev->adapter;
1004 	struct gpio_desc *gpio;
1005 
1006 	gpio = devm_gpiod_get_optional(dev->dev, "scl", GPIOD_OUT_HIGH);
1007 	if (IS_ERR_OR_NULL(gpio))
1008 		return PTR_ERR_OR_ZERO(gpio);
1009 
1010 	rinfo->scl_gpiod = gpio;
1011 
1012 	gpio = devm_gpiod_get_optional(dev->dev, "sda", GPIOD_IN);
1013 	if (IS_ERR(gpio))
1014 		return PTR_ERR(gpio);
1015 	rinfo->sda_gpiod = gpio;
1016 
1017 	rinfo->pinctrl = devm_pinctrl_get(dev->dev);
1018 	if (IS_ERR(rinfo->pinctrl)) {
1019 		if (PTR_ERR(rinfo->pinctrl) == -EPROBE_DEFER)
1020 			return PTR_ERR(rinfo->pinctrl);
1021 
1022 		rinfo->pinctrl = NULL;
1023 		dev_err(dev->dev, "getting pinctrl info failed: bus recovery might not work\n");
1024 	} else if (!rinfo->pinctrl) {
1025 		dev_dbg(dev->dev, "pinctrl is disabled, bus recovery might not work\n");
1026 	}
1027 
1028 	rinfo->recover_bus = i2c_generic_scl_recovery;
1029 	rinfo->prepare_recovery = i2c_dw_prepare_recovery;
1030 	rinfo->unprepare_recovery = i2c_dw_unprepare_recovery;
1031 	adap->bus_recovery_info = rinfo;
1032 
1033 	dev_info(dev->dev, "running with GPIO recovery mode! scl%s",
1034 		 rinfo->sda_gpiod ? ",sda" : "");
1035 
1036 	return 0;
1037 }
1038 
1039 int i2c_dw_probe_master(struct dw_i2c_dev *dev)
1040 {
1041 	unsigned int ic_con;
1042 	int ret;
1043 
1044 	init_completion(&dev->cmd_complete);
1045 
1046 	ret = i2c_dw_set_timings_master(dev);
1047 	if (ret)
1048 		return ret;
1049 
1050 	/* Lock the bus for accessing DW_IC_CON */
1051 	ret = i2c_dw_acquire_lock(dev);
1052 	if (ret)
1053 		return ret;
1054 
1055 	/*
1056 	 * On AMD platforms BIOS advertises the bus clear feature
1057 	 * and enables the SCL/SDA stuck low. SMU FW does the
1058 	 * bus recovery process. Driver should not ignore this BIOS
1059 	 * advertisement of bus clear feature.
1060 	 */
1061 	ret = regmap_read(dev->map, DW_IC_CON, &ic_con);
1062 	i2c_dw_release_lock(dev);
1063 	if (ret)
1064 		return ret;
1065 
1066 	if (ic_con & DW_IC_CON_BUS_CLEAR_CTRL)
1067 		dev->master_cfg |= DW_IC_CON_BUS_CLEAR_CTRL;
1068 
1069 	return i2c_dw_init_recovery_info(dev);
1070 }
1071 
1072 MODULE_DESCRIPTION("Synopsys DesignWare I2C bus master adapter");
1073 MODULE_LICENSE("GPL");
1074 MODULE_IMPORT_NS("I2C_DW_COMMON");
1075