xref: /linux/drivers/i3c/master/dw-i3c-master.c (revision a0d48ebf)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2018 Synopsys, Inc. and/or its affiliates.
4  *
5  * Author: Vitor Soares <vitor.soares@synopsys.com>
6  */
7 
8 #include <linux/bitops.h>
9 #include <linux/clk.h>
10 #include <linux/completion.h>
11 #include <linux/err.h>
12 #include <linux/errno.h>
13 #include <linux/i3c/master.h>
14 #include <linux/interrupt.h>
15 #include <linux/ioport.h>
16 #include <linux/iopoll.h>
17 #include <linux/list.h>
18 #include <linux/module.h>
19 #include <linux/of.h>
20 #include <linux/platform_device.h>
21 #include <linux/reset.h>
22 #include <linux/slab.h>
23 
24 #include "dw-i3c-master.h"
25 
26 #define DEVICE_CTRL			0x0
27 #define DEV_CTRL_ENABLE			BIT(31)
28 #define DEV_CTRL_RESUME			BIT(30)
29 #define DEV_CTRL_HOT_JOIN_NACK		BIT(8)
30 #define DEV_CTRL_I2C_SLAVE_PRESENT	BIT(7)
31 
32 #define DEVICE_ADDR			0x4
33 #define DEV_ADDR_DYNAMIC_ADDR_VALID	BIT(31)
34 #define DEV_ADDR_DYNAMIC(x)		(((x) << 16) & GENMASK(22, 16))
35 
36 #define HW_CAPABILITY			0x8
37 #define COMMAND_QUEUE_PORT		0xc
38 #define COMMAND_PORT_TOC		BIT(30)
39 #define COMMAND_PORT_READ_TRANSFER	BIT(28)
40 #define COMMAND_PORT_SDAP		BIT(27)
41 #define COMMAND_PORT_ROC		BIT(26)
42 #define COMMAND_PORT_SPEED(x)		(((x) << 21) & GENMASK(23, 21))
43 #define COMMAND_PORT_DEV_INDEX(x)	(((x) << 16) & GENMASK(20, 16))
44 #define COMMAND_PORT_CP			BIT(15)
45 #define COMMAND_PORT_CMD(x)		(((x) << 7) & GENMASK(14, 7))
46 #define COMMAND_PORT_TID(x)		(((x) << 3) & GENMASK(6, 3))
47 
48 #define COMMAND_PORT_ARG_DATA_LEN(x)	(((x) << 16) & GENMASK(31, 16))
49 #define COMMAND_PORT_ARG_DATA_LEN_MAX	65536
50 #define COMMAND_PORT_TRANSFER_ARG	0x01
51 
52 #define COMMAND_PORT_SDA_DATA_BYTE_3(x)	(((x) << 24) & GENMASK(31, 24))
53 #define COMMAND_PORT_SDA_DATA_BYTE_2(x)	(((x) << 16) & GENMASK(23, 16))
54 #define COMMAND_PORT_SDA_DATA_BYTE_1(x)	(((x) << 8) & GENMASK(15, 8))
55 #define COMMAND_PORT_SDA_BYTE_STRB_3	BIT(5)
56 #define COMMAND_PORT_SDA_BYTE_STRB_2	BIT(4)
57 #define COMMAND_PORT_SDA_BYTE_STRB_1	BIT(3)
58 #define COMMAND_PORT_SHORT_DATA_ARG	0x02
59 
60 #define COMMAND_PORT_DEV_COUNT(x)	(((x) << 21) & GENMASK(25, 21))
61 #define COMMAND_PORT_ADDR_ASSGN_CMD	0x03
62 
63 #define RESPONSE_QUEUE_PORT		0x10
64 #define RESPONSE_PORT_ERR_STATUS(x)	(((x) & GENMASK(31, 28)) >> 28)
65 #define RESPONSE_NO_ERROR		0
66 #define RESPONSE_ERROR_CRC		1
67 #define RESPONSE_ERROR_PARITY		2
68 #define RESPONSE_ERROR_FRAME		3
69 #define RESPONSE_ERROR_IBA_NACK		4
70 #define RESPONSE_ERROR_ADDRESS_NACK	5
71 #define RESPONSE_ERROR_OVER_UNDER_FLOW	6
72 #define RESPONSE_ERROR_TRANSF_ABORT	8
73 #define RESPONSE_ERROR_I2C_W_NACK_ERR	9
74 #define RESPONSE_PORT_TID(x)		(((x) & GENMASK(27, 24)) >> 24)
75 #define RESPONSE_PORT_DATA_LEN(x)	((x) & GENMASK(15, 0))
76 
77 #define RX_TX_DATA_PORT			0x14
78 #define IBI_QUEUE_STATUS		0x18
79 #define IBI_QUEUE_STATUS_IBI_ID(x)	(((x) & GENMASK(15, 8)) >> 8)
80 #define IBI_QUEUE_STATUS_DATA_LEN(x)	((x) & GENMASK(7, 0))
81 #define IBI_QUEUE_IBI_ADDR(x)		(IBI_QUEUE_STATUS_IBI_ID(x) >> 1)
82 #define IBI_QUEUE_IBI_RNW(x)		(IBI_QUEUE_STATUS_IBI_ID(x) & BIT(0))
83 #define IBI_TYPE_MR(x)                                                         \
84 	((IBI_QUEUE_IBI_ADDR(x) != I3C_HOT_JOIN_ADDR) && !IBI_QUEUE_IBI_RNW(x))
85 #define IBI_TYPE_HJ(x)                                                         \
86 	((IBI_QUEUE_IBI_ADDR(x) == I3C_HOT_JOIN_ADDR) && !IBI_QUEUE_IBI_RNW(x))
87 #define IBI_TYPE_SIRQ(x)                                                        \
88 	((IBI_QUEUE_IBI_ADDR(x) != I3C_HOT_JOIN_ADDR) && IBI_QUEUE_IBI_RNW(x))
89 
90 #define QUEUE_THLD_CTRL			0x1c
91 #define QUEUE_THLD_CTRL_IBI_STAT_MASK	GENMASK(31, 24)
92 #define QUEUE_THLD_CTRL_IBI_STAT(x)	(((x) - 1) << 24)
93 #define QUEUE_THLD_CTRL_IBI_DATA_MASK	GENMASK(20, 16)
94 #define QUEUE_THLD_CTRL_IBI_DATA(x)	((x) << 16)
95 #define QUEUE_THLD_CTRL_RESP_BUF_MASK	GENMASK(15, 8)
96 #define QUEUE_THLD_CTRL_RESP_BUF(x)	(((x) - 1) << 8)
97 
98 #define DATA_BUFFER_THLD_CTRL		0x20
99 #define DATA_BUFFER_THLD_CTRL_RX_BUF	GENMASK(11, 8)
100 
101 #define IBI_QUEUE_CTRL			0x24
102 #define IBI_MR_REQ_REJECT		0x2C
103 #define IBI_SIR_REQ_REJECT		0x30
104 #define IBI_REQ_REJECT_ALL		GENMASK(31, 0)
105 
106 #define RESET_CTRL			0x34
107 #define RESET_CTRL_IBI_QUEUE		BIT(5)
108 #define RESET_CTRL_RX_FIFO		BIT(4)
109 #define RESET_CTRL_TX_FIFO		BIT(3)
110 #define RESET_CTRL_RESP_QUEUE		BIT(2)
111 #define RESET_CTRL_CMD_QUEUE		BIT(1)
112 #define RESET_CTRL_SOFT			BIT(0)
113 
114 #define SLV_EVENT_CTRL			0x38
115 #define INTR_STATUS			0x3c
116 #define INTR_STATUS_EN			0x40
117 #define INTR_SIGNAL_EN			0x44
118 #define INTR_FORCE			0x48
119 #define INTR_BUSOWNER_UPDATE_STAT	BIT(13)
120 #define INTR_IBI_UPDATED_STAT		BIT(12)
121 #define INTR_READ_REQ_RECV_STAT		BIT(11)
122 #define INTR_DEFSLV_STAT		BIT(10)
123 #define INTR_TRANSFER_ERR_STAT		BIT(9)
124 #define INTR_DYN_ADDR_ASSGN_STAT	BIT(8)
125 #define INTR_CCC_UPDATED_STAT		BIT(6)
126 #define INTR_TRANSFER_ABORT_STAT	BIT(5)
127 #define INTR_RESP_READY_STAT		BIT(4)
128 #define INTR_CMD_QUEUE_READY_STAT	BIT(3)
129 #define INTR_IBI_THLD_STAT		BIT(2)
130 #define INTR_RX_THLD_STAT		BIT(1)
131 #define INTR_TX_THLD_STAT		BIT(0)
132 #define INTR_ALL			(INTR_BUSOWNER_UPDATE_STAT |	\
133 					INTR_IBI_UPDATED_STAT |		\
134 					INTR_READ_REQ_RECV_STAT |	\
135 					INTR_DEFSLV_STAT |		\
136 					INTR_TRANSFER_ERR_STAT |	\
137 					INTR_DYN_ADDR_ASSGN_STAT |	\
138 					INTR_CCC_UPDATED_STAT |		\
139 					INTR_TRANSFER_ABORT_STAT |	\
140 					INTR_RESP_READY_STAT |		\
141 					INTR_CMD_QUEUE_READY_STAT |	\
142 					INTR_IBI_THLD_STAT |		\
143 					INTR_TX_THLD_STAT |		\
144 					INTR_RX_THLD_STAT)
145 
146 #define INTR_MASTER_MASK		(INTR_TRANSFER_ERR_STAT |	\
147 					 INTR_RESP_READY_STAT)
148 
149 #define QUEUE_STATUS_LEVEL		0x4c
150 #define QUEUE_STATUS_IBI_STATUS_CNT(x)	(((x) & GENMASK(28, 24)) >> 24)
151 #define QUEUE_STATUS_IBI_BUF_BLR(x)	(((x) & GENMASK(23, 16)) >> 16)
152 #define QUEUE_STATUS_LEVEL_RESP(x)	(((x) & GENMASK(15, 8)) >> 8)
153 #define QUEUE_STATUS_LEVEL_CMD(x)	((x) & GENMASK(7, 0))
154 
155 #define DATA_BUFFER_STATUS_LEVEL	0x50
156 #define DATA_BUFFER_STATUS_LEVEL_TX(x)	((x) & GENMASK(7, 0))
157 
158 #define PRESENT_STATE			0x54
159 #define CCC_DEVICE_STATUS		0x58
160 #define DEVICE_ADDR_TABLE_POINTER	0x5c
161 #define DEVICE_ADDR_TABLE_DEPTH(x)	(((x) & GENMASK(31, 16)) >> 16)
162 #define DEVICE_ADDR_TABLE_ADDR(x)	((x) & GENMASK(7, 0))
163 
164 #define DEV_CHAR_TABLE_POINTER		0x60
165 #define VENDOR_SPECIFIC_REG_POINTER	0x6c
166 #define SLV_PID_VALUE			0x74
167 #define SLV_CHAR_CTRL			0x78
168 #define SLV_MAX_LEN			0x7c
169 #define MAX_READ_TURNAROUND		0x80
170 #define MAX_DATA_SPEED			0x84
171 #define SLV_DEBUG_STATUS		0x88
172 #define SLV_INTR_REQ			0x8c
173 #define DEVICE_CTRL_EXTENDED		0xb0
174 #define SCL_I3C_OD_TIMING		0xb4
175 #define SCL_I3C_PP_TIMING		0xb8
176 #define SCL_I3C_TIMING_HCNT(x)		(((x) << 16) & GENMASK(23, 16))
177 #define SCL_I3C_TIMING_LCNT(x)		((x) & GENMASK(7, 0))
178 #define SCL_I3C_TIMING_CNT_MIN		5
179 
180 #define SCL_I2C_FM_TIMING		0xbc
181 #define SCL_I2C_FM_TIMING_HCNT(x)	(((x) << 16) & GENMASK(31, 16))
182 #define SCL_I2C_FM_TIMING_LCNT(x)	((x) & GENMASK(15, 0))
183 
184 #define SCL_I2C_FMP_TIMING		0xc0
185 #define SCL_I2C_FMP_TIMING_HCNT(x)	(((x) << 16) & GENMASK(23, 16))
186 #define SCL_I2C_FMP_TIMING_LCNT(x)	((x) & GENMASK(15, 0))
187 
188 #define SCL_EXT_LCNT_TIMING		0xc8
189 #define SCL_EXT_LCNT_4(x)		(((x) << 24) & GENMASK(31, 24))
190 #define SCL_EXT_LCNT_3(x)		(((x) << 16) & GENMASK(23, 16))
191 #define SCL_EXT_LCNT_2(x)		(((x) << 8) & GENMASK(15, 8))
192 #define SCL_EXT_LCNT_1(x)		((x) & GENMASK(7, 0))
193 
194 #define SCL_EXT_TERMN_LCNT_TIMING	0xcc
195 #define BUS_FREE_TIMING			0xd4
196 #define BUS_I3C_MST_FREE(x)		((x) & GENMASK(15, 0))
197 
198 #define BUS_IDLE_TIMING			0xd8
199 #define I3C_VER_ID			0xe0
200 #define I3C_VER_TYPE			0xe4
201 #define EXTENDED_CAPABILITY		0xe8
202 #define SLAVE_CONFIG			0xec
203 
204 #define DEV_ADDR_TABLE_IBI_MDB		BIT(12)
205 #define DEV_ADDR_TABLE_SIR_REJECT	BIT(13)
206 #define DEV_ADDR_TABLE_LEGACY_I2C_DEV	BIT(31)
207 #define DEV_ADDR_TABLE_DYNAMIC_ADDR(x)	(((x) << 16) & GENMASK(23, 16))
208 #define DEV_ADDR_TABLE_STATIC_ADDR(x)	((x) & GENMASK(6, 0))
209 #define DEV_ADDR_TABLE_LOC(start, idx)	((start) + ((idx) << 2))
210 
211 #define I3C_BUS_SDR1_SCL_RATE		8000000
212 #define I3C_BUS_SDR2_SCL_RATE		6000000
213 #define I3C_BUS_SDR3_SCL_RATE		4000000
214 #define I3C_BUS_SDR4_SCL_RATE		2000000
215 #define I3C_BUS_I2C_FM_TLOW_MIN_NS	1300
216 #define I3C_BUS_I2C_FMP_TLOW_MIN_NS	500
217 #define I3C_BUS_THIGH_MAX_NS		41
218 
219 #define XFER_TIMEOUT (msecs_to_jiffies(1000))
220 
221 struct dw_i3c_cmd {
222 	u32 cmd_lo;
223 	u32 cmd_hi;
224 	u16 tx_len;
225 	const void *tx_buf;
226 	u16 rx_len;
227 	void *rx_buf;
228 	u8 error;
229 };
230 
231 struct dw_i3c_xfer {
232 	struct list_head node;
233 	struct completion comp;
234 	int ret;
235 	unsigned int ncmds;
236 	struct dw_i3c_cmd cmds[] __counted_by(ncmds);
237 };
238 
239 struct dw_i3c_i2c_dev_data {
240 	u8 index;
241 	struct i3c_generic_ibi_pool *ibi_pool;
242 };
243 
244 static u8 even_parity(u8 p)
245 {
246 	p ^= p >> 4;
247 	p &= 0xf;
248 
249 	return (0x9669 >> p) & 1;
250 }
251 
252 static bool dw_i3c_master_supports_ccc_cmd(struct i3c_master_controller *m,
253 					   const struct i3c_ccc_cmd *cmd)
254 {
255 	if (cmd->ndests > 1)
256 		return false;
257 
258 	switch (cmd->id) {
259 	case I3C_CCC_ENEC(true):
260 	case I3C_CCC_ENEC(false):
261 	case I3C_CCC_DISEC(true):
262 	case I3C_CCC_DISEC(false):
263 	case I3C_CCC_ENTAS(0, true):
264 	case I3C_CCC_ENTAS(0, false):
265 	case I3C_CCC_RSTDAA(true):
266 	case I3C_CCC_RSTDAA(false):
267 	case I3C_CCC_ENTDAA:
268 	case I3C_CCC_SETMWL(true):
269 	case I3C_CCC_SETMWL(false):
270 	case I3C_CCC_SETMRL(true):
271 	case I3C_CCC_SETMRL(false):
272 	case I3C_CCC_ENTHDR(0):
273 	case I3C_CCC_SETDASA:
274 	case I3C_CCC_SETNEWDA:
275 	case I3C_CCC_GETMWL:
276 	case I3C_CCC_GETMRL:
277 	case I3C_CCC_GETPID:
278 	case I3C_CCC_GETBCR:
279 	case I3C_CCC_GETDCR:
280 	case I3C_CCC_GETSTATUS:
281 	case I3C_CCC_GETMXDS:
282 	case I3C_CCC_GETHDRCAP:
283 		return true;
284 	default:
285 		return false;
286 	}
287 }
288 
289 static inline struct dw_i3c_master *
290 to_dw_i3c_master(struct i3c_master_controller *master)
291 {
292 	return container_of(master, struct dw_i3c_master, base);
293 }
294 
295 static void dw_i3c_master_disable(struct dw_i3c_master *master)
296 {
297 	writel(readl(master->regs + DEVICE_CTRL) & ~DEV_CTRL_ENABLE,
298 	       master->regs + DEVICE_CTRL);
299 }
300 
301 static void dw_i3c_master_enable(struct dw_i3c_master *master)
302 {
303 	writel(readl(master->regs + DEVICE_CTRL) | DEV_CTRL_ENABLE,
304 	       master->regs + DEVICE_CTRL);
305 }
306 
307 static int dw_i3c_master_get_addr_pos(struct dw_i3c_master *master, u8 addr)
308 {
309 	int pos;
310 
311 	for (pos = 0; pos < master->maxdevs; pos++) {
312 		if (addr == master->devs[pos].addr)
313 			return pos;
314 	}
315 
316 	return -EINVAL;
317 }
318 
319 static int dw_i3c_master_get_free_pos(struct dw_i3c_master *master)
320 {
321 	if (!(master->free_pos & GENMASK(master->maxdevs - 1, 0)))
322 		return -ENOSPC;
323 
324 	return ffs(master->free_pos) - 1;
325 }
326 
327 static void dw_i3c_master_wr_tx_fifo(struct dw_i3c_master *master,
328 				     const u8 *bytes, int nbytes)
329 {
330 	writesl(master->regs + RX_TX_DATA_PORT, bytes, nbytes / 4);
331 	if (nbytes & 3) {
332 		u32 tmp = 0;
333 
334 		memcpy(&tmp, bytes + (nbytes & ~3), nbytes & 3);
335 		writesl(master->regs + RX_TX_DATA_PORT, &tmp, 1);
336 	}
337 }
338 
339 static void dw_i3c_master_read_fifo(struct dw_i3c_master *master,
340 				    int reg,  u8 *bytes, int nbytes)
341 {
342 	readsl(master->regs + reg, bytes, nbytes / 4);
343 	if (nbytes & 3) {
344 		u32 tmp;
345 
346 		readsl(master->regs + reg, &tmp, 1);
347 		memcpy(bytes + (nbytes & ~3), &tmp, nbytes & 3);
348 	}
349 }
350 
351 static void dw_i3c_master_read_rx_fifo(struct dw_i3c_master *master,
352 				       u8 *bytes, int nbytes)
353 {
354 	return dw_i3c_master_read_fifo(master, RX_TX_DATA_PORT, bytes, nbytes);
355 }
356 
357 static void dw_i3c_master_read_ibi_fifo(struct dw_i3c_master *master,
358 					u8 *bytes, int nbytes)
359 {
360 	return dw_i3c_master_read_fifo(master, IBI_QUEUE_STATUS, bytes, nbytes);
361 }
362 
363 static struct dw_i3c_xfer *
364 dw_i3c_master_alloc_xfer(struct dw_i3c_master *master, unsigned int ncmds)
365 {
366 	struct dw_i3c_xfer *xfer;
367 
368 	xfer = kzalloc(struct_size(xfer, cmds, ncmds), GFP_KERNEL);
369 	if (!xfer)
370 		return NULL;
371 
372 	INIT_LIST_HEAD(&xfer->node);
373 	xfer->ncmds = ncmds;
374 	xfer->ret = -ETIMEDOUT;
375 
376 	return xfer;
377 }
378 
379 static void dw_i3c_master_free_xfer(struct dw_i3c_xfer *xfer)
380 {
381 	kfree(xfer);
382 }
383 
384 static void dw_i3c_master_start_xfer_locked(struct dw_i3c_master *master)
385 {
386 	struct dw_i3c_xfer *xfer = master->xferqueue.cur;
387 	unsigned int i;
388 	u32 thld_ctrl;
389 
390 	if (!xfer)
391 		return;
392 
393 	for (i = 0; i < xfer->ncmds; i++) {
394 		struct dw_i3c_cmd *cmd = &xfer->cmds[i];
395 
396 		dw_i3c_master_wr_tx_fifo(master, cmd->tx_buf, cmd->tx_len);
397 	}
398 
399 	thld_ctrl = readl(master->regs + QUEUE_THLD_CTRL);
400 	thld_ctrl &= ~QUEUE_THLD_CTRL_RESP_BUF_MASK;
401 	thld_ctrl |= QUEUE_THLD_CTRL_RESP_BUF(xfer->ncmds);
402 	writel(thld_ctrl, master->regs + QUEUE_THLD_CTRL);
403 
404 	for (i = 0; i < xfer->ncmds; i++) {
405 		struct dw_i3c_cmd *cmd = &xfer->cmds[i];
406 
407 		writel(cmd->cmd_hi, master->regs + COMMAND_QUEUE_PORT);
408 		writel(cmd->cmd_lo, master->regs + COMMAND_QUEUE_PORT);
409 	}
410 }
411 
412 static void dw_i3c_master_enqueue_xfer(struct dw_i3c_master *master,
413 				       struct dw_i3c_xfer *xfer)
414 {
415 	unsigned long flags;
416 
417 	init_completion(&xfer->comp);
418 	spin_lock_irqsave(&master->xferqueue.lock, flags);
419 	if (master->xferqueue.cur) {
420 		list_add_tail(&xfer->node, &master->xferqueue.list);
421 	} else {
422 		master->xferqueue.cur = xfer;
423 		dw_i3c_master_start_xfer_locked(master);
424 	}
425 	spin_unlock_irqrestore(&master->xferqueue.lock, flags);
426 }
427 
428 static void dw_i3c_master_dequeue_xfer_locked(struct dw_i3c_master *master,
429 					      struct dw_i3c_xfer *xfer)
430 {
431 	if (master->xferqueue.cur == xfer) {
432 		u32 status;
433 
434 		master->xferqueue.cur = NULL;
435 
436 		writel(RESET_CTRL_RX_FIFO | RESET_CTRL_TX_FIFO |
437 		       RESET_CTRL_RESP_QUEUE | RESET_CTRL_CMD_QUEUE,
438 		       master->regs + RESET_CTRL);
439 
440 		readl_poll_timeout_atomic(master->regs + RESET_CTRL, status,
441 					  !status, 10, 1000000);
442 	} else {
443 		list_del_init(&xfer->node);
444 	}
445 }
446 
447 static void dw_i3c_master_dequeue_xfer(struct dw_i3c_master *master,
448 				       struct dw_i3c_xfer *xfer)
449 {
450 	unsigned long flags;
451 
452 	spin_lock_irqsave(&master->xferqueue.lock, flags);
453 	dw_i3c_master_dequeue_xfer_locked(master, xfer);
454 	spin_unlock_irqrestore(&master->xferqueue.lock, flags);
455 }
456 
457 static void dw_i3c_master_end_xfer_locked(struct dw_i3c_master *master, u32 isr)
458 {
459 	struct dw_i3c_xfer *xfer = master->xferqueue.cur;
460 	int i, ret = 0;
461 	u32 nresp;
462 
463 	if (!xfer)
464 		return;
465 
466 	nresp = readl(master->regs + QUEUE_STATUS_LEVEL);
467 	nresp = QUEUE_STATUS_LEVEL_RESP(nresp);
468 
469 	for (i = 0; i < nresp; i++) {
470 		struct dw_i3c_cmd *cmd;
471 		u32 resp;
472 
473 		resp = readl(master->regs + RESPONSE_QUEUE_PORT);
474 
475 		cmd = &xfer->cmds[RESPONSE_PORT_TID(resp)];
476 		cmd->rx_len = RESPONSE_PORT_DATA_LEN(resp);
477 		cmd->error = RESPONSE_PORT_ERR_STATUS(resp);
478 		if (cmd->rx_len && !cmd->error)
479 			dw_i3c_master_read_rx_fifo(master, cmd->rx_buf,
480 						   cmd->rx_len);
481 	}
482 
483 	for (i = 0; i < nresp; i++) {
484 		switch (xfer->cmds[i].error) {
485 		case RESPONSE_NO_ERROR:
486 			break;
487 		case RESPONSE_ERROR_PARITY:
488 		case RESPONSE_ERROR_IBA_NACK:
489 		case RESPONSE_ERROR_TRANSF_ABORT:
490 		case RESPONSE_ERROR_CRC:
491 		case RESPONSE_ERROR_FRAME:
492 			ret = -EIO;
493 			break;
494 		case RESPONSE_ERROR_OVER_UNDER_FLOW:
495 			ret = -ENOSPC;
496 			break;
497 		case RESPONSE_ERROR_I2C_W_NACK_ERR:
498 		case RESPONSE_ERROR_ADDRESS_NACK:
499 		default:
500 			ret = -EINVAL;
501 			break;
502 		}
503 	}
504 
505 	xfer->ret = ret;
506 	complete(&xfer->comp);
507 
508 	if (ret < 0) {
509 		dw_i3c_master_dequeue_xfer_locked(master, xfer);
510 		writel(readl(master->regs + DEVICE_CTRL) | DEV_CTRL_RESUME,
511 		       master->regs + DEVICE_CTRL);
512 	}
513 
514 	xfer = list_first_entry_or_null(&master->xferqueue.list,
515 					struct dw_i3c_xfer,
516 					node);
517 	if (xfer)
518 		list_del_init(&xfer->node);
519 
520 	master->xferqueue.cur = xfer;
521 	dw_i3c_master_start_xfer_locked(master);
522 }
523 
524 static int dw_i3c_clk_cfg(struct dw_i3c_master *master)
525 {
526 	unsigned long core_rate, core_period;
527 	u32 scl_timing;
528 	u8 hcnt, lcnt;
529 
530 	core_rate = clk_get_rate(master->core_clk);
531 	if (!core_rate)
532 		return -EINVAL;
533 
534 	core_period = DIV_ROUND_UP(1000000000, core_rate);
535 
536 	hcnt = DIV_ROUND_UP(I3C_BUS_THIGH_MAX_NS, core_period) - 1;
537 	if (hcnt < SCL_I3C_TIMING_CNT_MIN)
538 		hcnt = SCL_I3C_TIMING_CNT_MIN;
539 
540 	lcnt = DIV_ROUND_UP(core_rate, master->base.bus.scl_rate.i3c) - hcnt;
541 	if (lcnt < SCL_I3C_TIMING_CNT_MIN)
542 		lcnt = SCL_I3C_TIMING_CNT_MIN;
543 
544 	scl_timing = SCL_I3C_TIMING_HCNT(hcnt) | SCL_I3C_TIMING_LCNT(lcnt);
545 	writel(scl_timing, master->regs + SCL_I3C_PP_TIMING);
546 
547 	/*
548 	 * In pure i3c mode, MST_FREE represents tCAS. In shared mode, this
549 	 * will be set up by dw_i2c_clk_cfg as tLOW.
550 	 */
551 	if (master->base.bus.mode == I3C_BUS_MODE_PURE)
552 		writel(BUS_I3C_MST_FREE(lcnt), master->regs + BUS_FREE_TIMING);
553 
554 	lcnt = max_t(u8,
555 		     DIV_ROUND_UP(I3C_BUS_TLOW_OD_MIN_NS, core_period), lcnt);
556 	scl_timing = SCL_I3C_TIMING_HCNT(hcnt) | SCL_I3C_TIMING_LCNT(lcnt);
557 	writel(scl_timing, master->regs + SCL_I3C_OD_TIMING);
558 
559 	lcnt = DIV_ROUND_UP(core_rate, I3C_BUS_SDR1_SCL_RATE) - hcnt;
560 	scl_timing = SCL_EXT_LCNT_1(lcnt);
561 	lcnt = DIV_ROUND_UP(core_rate, I3C_BUS_SDR2_SCL_RATE) - hcnt;
562 	scl_timing |= SCL_EXT_LCNT_2(lcnt);
563 	lcnt = DIV_ROUND_UP(core_rate, I3C_BUS_SDR3_SCL_RATE) - hcnt;
564 	scl_timing |= SCL_EXT_LCNT_3(lcnt);
565 	lcnt = DIV_ROUND_UP(core_rate, I3C_BUS_SDR4_SCL_RATE) - hcnt;
566 	scl_timing |= SCL_EXT_LCNT_4(lcnt);
567 	writel(scl_timing, master->regs + SCL_EXT_LCNT_TIMING);
568 
569 	return 0;
570 }
571 
572 static int dw_i2c_clk_cfg(struct dw_i3c_master *master)
573 {
574 	unsigned long core_rate, core_period;
575 	u16 hcnt, lcnt;
576 	u32 scl_timing;
577 
578 	core_rate = clk_get_rate(master->core_clk);
579 	if (!core_rate)
580 		return -EINVAL;
581 
582 	core_period = DIV_ROUND_UP(1000000000, core_rate);
583 
584 	lcnt = DIV_ROUND_UP(I3C_BUS_I2C_FMP_TLOW_MIN_NS, core_period);
585 	hcnt = DIV_ROUND_UP(core_rate, I3C_BUS_I2C_FM_PLUS_SCL_RATE) - lcnt;
586 	scl_timing = SCL_I2C_FMP_TIMING_HCNT(hcnt) |
587 		     SCL_I2C_FMP_TIMING_LCNT(lcnt);
588 	writel(scl_timing, master->regs + SCL_I2C_FMP_TIMING);
589 
590 	lcnt = DIV_ROUND_UP(I3C_BUS_I2C_FM_TLOW_MIN_NS, core_period);
591 	hcnt = DIV_ROUND_UP(core_rate, I3C_BUS_I2C_FM_SCL_RATE) - lcnt;
592 	scl_timing = SCL_I2C_FM_TIMING_HCNT(hcnt) |
593 		     SCL_I2C_FM_TIMING_LCNT(lcnt);
594 	writel(scl_timing, master->regs + SCL_I2C_FM_TIMING);
595 
596 	writel(BUS_I3C_MST_FREE(lcnt), master->regs + BUS_FREE_TIMING);
597 	writel(readl(master->regs + DEVICE_CTRL) | DEV_CTRL_I2C_SLAVE_PRESENT,
598 	       master->regs + DEVICE_CTRL);
599 
600 	return 0;
601 }
602 
603 static int dw_i3c_master_bus_init(struct i3c_master_controller *m)
604 {
605 	struct dw_i3c_master *master = to_dw_i3c_master(m);
606 	struct i3c_bus *bus = i3c_master_get_bus(m);
607 	struct i3c_device_info info = { };
608 	u32 thld_ctrl;
609 	int ret;
610 
611 	ret = master->platform_ops->init(master);
612 	if (ret)
613 		return ret;
614 
615 	switch (bus->mode) {
616 	case I3C_BUS_MODE_MIXED_FAST:
617 	case I3C_BUS_MODE_MIXED_LIMITED:
618 		ret = dw_i2c_clk_cfg(master);
619 		if (ret)
620 			return ret;
621 		fallthrough;
622 	case I3C_BUS_MODE_PURE:
623 		ret = dw_i3c_clk_cfg(master);
624 		if (ret)
625 			return ret;
626 		break;
627 	default:
628 		return -EINVAL;
629 	}
630 
631 	thld_ctrl = readl(master->regs + QUEUE_THLD_CTRL);
632 	thld_ctrl &= ~(QUEUE_THLD_CTRL_RESP_BUF_MASK |
633 		       QUEUE_THLD_CTRL_IBI_STAT_MASK |
634 		       QUEUE_THLD_CTRL_IBI_DATA_MASK);
635 	thld_ctrl |= QUEUE_THLD_CTRL_IBI_STAT(1) |
636 		QUEUE_THLD_CTRL_IBI_DATA(31);
637 	writel(thld_ctrl, master->regs + QUEUE_THLD_CTRL);
638 
639 	thld_ctrl = readl(master->regs + DATA_BUFFER_THLD_CTRL);
640 	thld_ctrl &= ~DATA_BUFFER_THLD_CTRL_RX_BUF;
641 	writel(thld_ctrl, master->regs + DATA_BUFFER_THLD_CTRL);
642 
643 	writel(INTR_ALL, master->regs + INTR_STATUS);
644 	writel(INTR_MASTER_MASK, master->regs + INTR_STATUS_EN);
645 	writel(INTR_MASTER_MASK, master->regs + INTR_SIGNAL_EN);
646 
647 	ret = i3c_master_get_free_addr(m, 0);
648 	if (ret < 0)
649 		return ret;
650 
651 	writel(DEV_ADDR_DYNAMIC_ADDR_VALID | DEV_ADDR_DYNAMIC(ret),
652 	       master->regs + DEVICE_ADDR);
653 
654 	memset(&info, 0, sizeof(info));
655 	info.dyn_addr = ret;
656 
657 	ret = i3c_master_set_info(&master->base, &info);
658 	if (ret)
659 		return ret;
660 
661 	master->sir_rej_mask = IBI_REQ_REJECT_ALL;
662 	writel(master->sir_rej_mask, master->regs + IBI_SIR_REQ_REJECT);
663 
664 	writel(IBI_REQ_REJECT_ALL, master->regs + IBI_MR_REQ_REJECT);
665 
666 	/* For now don't support Hot-Join */
667 	writel(readl(master->regs + DEVICE_CTRL) | DEV_CTRL_HOT_JOIN_NACK,
668 	       master->regs + DEVICE_CTRL);
669 
670 	dw_i3c_master_enable(master);
671 
672 	return 0;
673 }
674 
675 static void dw_i3c_master_bus_cleanup(struct i3c_master_controller *m)
676 {
677 	struct dw_i3c_master *master = to_dw_i3c_master(m);
678 
679 	dw_i3c_master_disable(master);
680 }
681 
682 static int dw_i3c_ccc_set(struct dw_i3c_master *master,
683 			  struct i3c_ccc_cmd *ccc)
684 {
685 	struct dw_i3c_xfer *xfer;
686 	struct dw_i3c_cmd *cmd;
687 	int ret, pos = 0;
688 
689 	if (ccc->id & I3C_CCC_DIRECT) {
690 		pos = dw_i3c_master_get_addr_pos(master, ccc->dests[0].addr);
691 		if (pos < 0)
692 			return pos;
693 	}
694 
695 	xfer = dw_i3c_master_alloc_xfer(master, 1);
696 	if (!xfer)
697 		return -ENOMEM;
698 
699 	cmd = xfer->cmds;
700 	cmd->tx_buf = ccc->dests[0].payload.data;
701 	cmd->tx_len = ccc->dests[0].payload.len;
702 
703 	cmd->cmd_hi = COMMAND_PORT_ARG_DATA_LEN(ccc->dests[0].payload.len) |
704 		      COMMAND_PORT_TRANSFER_ARG;
705 
706 	cmd->cmd_lo = COMMAND_PORT_CP |
707 		      COMMAND_PORT_DEV_INDEX(pos) |
708 		      COMMAND_PORT_CMD(ccc->id) |
709 		      COMMAND_PORT_TOC |
710 		      COMMAND_PORT_ROC;
711 
712 	dw_i3c_master_enqueue_xfer(master, xfer);
713 	if (!wait_for_completion_timeout(&xfer->comp, XFER_TIMEOUT))
714 		dw_i3c_master_dequeue_xfer(master, xfer);
715 
716 	ret = xfer->ret;
717 	if (xfer->cmds[0].error == RESPONSE_ERROR_IBA_NACK)
718 		ccc->err = I3C_ERROR_M2;
719 
720 	dw_i3c_master_free_xfer(xfer);
721 
722 	return ret;
723 }
724 
725 static int dw_i3c_ccc_get(struct dw_i3c_master *master, struct i3c_ccc_cmd *ccc)
726 {
727 	struct dw_i3c_xfer *xfer;
728 	struct dw_i3c_cmd *cmd;
729 	int ret, pos;
730 
731 	pos = dw_i3c_master_get_addr_pos(master, ccc->dests[0].addr);
732 	if (pos < 0)
733 		return pos;
734 
735 	xfer = dw_i3c_master_alloc_xfer(master, 1);
736 	if (!xfer)
737 		return -ENOMEM;
738 
739 	cmd = xfer->cmds;
740 	cmd->rx_buf = ccc->dests[0].payload.data;
741 	cmd->rx_len = ccc->dests[0].payload.len;
742 
743 	cmd->cmd_hi = COMMAND_PORT_ARG_DATA_LEN(ccc->dests[0].payload.len) |
744 		      COMMAND_PORT_TRANSFER_ARG;
745 
746 	cmd->cmd_lo = COMMAND_PORT_READ_TRANSFER |
747 		      COMMAND_PORT_CP |
748 		      COMMAND_PORT_DEV_INDEX(pos) |
749 		      COMMAND_PORT_CMD(ccc->id) |
750 		      COMMAND_PORT_TOC |
751 		      COMMAND_PORT_ROC;
752 
753 	dw_i3c_master_enqueue_xfer(master, xfer);
754 	if (!wait_for_completion_timeout(&xfer->comp, XFER_TIMEOUT))
755 		dw_i3c_master_dequeue_xfer(master, xfer);
756 
757 	ret = xfer->ret;
758 	if (xfer->cmds[0].error == RESPONSE_ERROR_IBA_NACK)
759 		ccc->err = I3C_ERROR_M2;
760 	dw_i3c_master_free_xfer(xfer);
761 
762 	return ret;
763 }
764 
765 static int dw_i3c_master_send_ccc_cmd(struct i3c_master_controller *m,
766 				      struct i3c_ccc_cmd *ccc)
767 {
768 	struct dw_i3c_master *master = to_dw_i3c_master(m);
769 	int ret = 0;
770 
771 	if (ccc->id == I3C_CCC_ENTDAA)
772 		return -EINVAL;
773 
774 	if (ccc->rnw)
775 		ret = dw_i3c_ccc_get(master, ccc);
776 	else
777 		ret = dw_i3c_ccc_set(master, ccc);
778 
779 	return ret;
780 }
781 
782 static int dw_i3c_master_daa(struct i3c_master_controller *m)
783 {
784 	struct dw_i3c_master *master = to_dw_i3c_master(m);
785 	struct dw_i3c_xfer *xfer;
786 	struct dw_i3c_cmd *cmd;
787 	u32 olddevs, newdevs;
788 	u8 p, last_addr = 0;
789 	int ret, pos;
790 
791 	olddevs = ~(master->free_pos);
792 
793 	/* Prepare DAT before launching DAA. */
794 	for (pos = 0; pos < master->maxdevs; pos++) {
795 		if (olddevs & BIT(pos))
796 			continue;
797 
798 		ret = i3c_master_get_free_addr(m, last_addr + 1);
799 		if (ret < 0)
800 			return -ENOSPC;
801 
802 		master->devs[pos].addr = ret;
803 		p = even_parity(ret);
804 		last_addr = ret;
805 		ret |= (p << 7);
806 
807 		writel(DEV_ADDR_TABLE_DYNAMIC_ADDR(ret),
808 		       master->regs +
809 		       DEV_ADDR_TABLE_LOC(master->datstartaddr, pos));
810 	}
811 
812 	xfer = dw_i3c_master_alloc_xfer(master, 1);
813 	if (!xfer)
814 		return -ENOMEM;
815 
816 	pos = dw_i3c_master_get_free_pos(master);
817 	if (pos < 0) {
818 		dw_i3c_master_free_xfer(xfer);
819 		return pos;
820 	}
821 	cmd = &xfer->cmds[0];
822 	cmd->cmd_hi = 0x1;
823 	cmd->cmd_lo = COMMAND_PORT_DEV_COUNT(master->maxdevs - pos) |
824 		      COMMAND_PORT_DEV_INDEX(pos) |
825 		      COMMAND_PORT_CMD(I3C_CCC_ENTDAA) |
826 		      COMMAND_PORT_ADDR_ASSGN_CMD |
827 		      COMMAND_PORT_TOC |
828 		      COMMAND_PORT_ROC;
829 
830 	dw_i3c_master_enqueue_xfer(master, xfer);
831 	if (!wait_for_completion_timeout(&xfer->comp, XFER_TIMEOUT))
832 		dw_i3c_master_dequeue_xfer(master, xfer);
833 
834 	newdevs = GENMASK(master->maxdevs - cmd->rx_len - 1, 0);
835 	newdevs &= ~olddevs;
836 
837 	for (pos = 0; pos < master->maxdevs; pos++) {
838 		if (newdevs & BIT(pos))
839 			i3c_master_add_i3c_dev_locked(m, master->devs[pos].addr);
840 	}
841 
842 	dw_i3c_master_free_xfer(xfer);
843 
844 	return 0;
845 }
846 
847 static int dw_i3c_master_priv_xfers(struct i3c_dev_desc *dev,
848 				    struct i3c_priv_xfer *i3c_xfers,
849 				    int i3c_nxfers)
850 {
851 	struct dw_i3c_i2c_dev_data *data = i3c_dev_get_master_data(dev);
852 	struct i3c_master_controller *m = i3c_dev_get_master(dev);
853 	struct dw_i3c_master *master = to_dw_i3c_master(m);
854 	unsigned int nrxwords = 0, ntxwords = 0;
855 	struct dw_i3c_xfer *xfer;
856 	int i, ret = 0;
857 
858 	if (!i3c_nxfers)
859 		return 0;
860 
861 	if (i3c_nxfers > master->caps.cmdfifodepth)
862 		return -ENOTSUPP;
863 
864 	for (i = 0; i < i3c_nxfers; i++) {
865 		if (i3c_xfers[i].rnw)
866 			nrxwords += DIV_ROUND_UP(i3c_xfers[i].len, 4);
867 		else
868 			ntxwords += DIV_ROUND_UP(i3c_xfers[i].len, 4);
869 	}
870 
871 	if (ntxwords > master->caps.datafifodepth ||
872 	    nrxwords > master->caps.datafifodepth)
873 		return -ENOTSUPP;
874 
875 	xfer = dw_i3c_master_alloc_xfer(master, i3c_nxfers);
876 	if (!xfer)
877 		return -ENOMEM;
878 
879 	for (i = 0; i < i3c_nxfers; i++) {
880 		struct dw_i3c_cmd *cmd = &xfer->cmds[i];
881 
882 		cmd->cmd_hi = COMMAND_PORT_ARG_DATA_LEN(i3c_xfers[i].len) |
883 			COMMAND_PORT_TRANSFER_ARG;
884 
885 		if (i3c_xfers[i].rnw) {
886 			cmd->rx_buf = i3c_xfers[i].data.in;
887 			cmd->rx_len = i3c_xfers[i].len;
888 			cmd->cmd_lo = COMMAND_PORT_READ_TRANSFER |
889 				      COMMAND_PORT_SPEED(dev->info.max_read_ds);
890 
891 		} else {
892 			cmd->tx_buf = i3c_xfers[i].data.out;
893 			cmd->tx_len = i3c_xfers[i].len;
894 			cmd->cmd_lo =
895 				COMMAND_PORT_SPEED(dev->info.max_write_ds);
896 		}
897 
898 		cmd->cmd_lo |= COMMAND_PORT_TID(i) |
899 			       COMMAND_PORT_DEV_INDEX(data->index) |
900 			       COMMAND_PORT_ROC;
901 
902 		if (i == (i3c_nxfers - 1))
903 			cmd->cmd_lo |= COMMAND_PORT_TOC;
904 	}
905 
906 	dw_i3c_master_enqueue_xfer(master, xfer);
907 	if (!wait_for_completion_timeout(&xfer->comp, XFER_TIMEOUT))
908 		dw_i3c_master_dequeue_xfer(master, xfer);
909 
910 	for (i = 0; i < i3c_nxfers; i++) {
911 		struct dw_i3c_cmd *cmd = &xfer->cmds[i];
912 
913 		if (i3c_xfers[i].rnw)
914 			i3c_xfers[i].len = cmd->rx_len;
915 	}
916 
917 	ret = xfer->ret;
918 	dw_i3c_master_free_xfer(xfer);
919 
920 	return ret;
921 }
922 
923 static int dw_i3c_master_reattach_i3c_dev(struct i3c_dev_desc *dev,
924 					  u8 old_dyn_addr)
925 {
926 	struct dw_i3c_i2c_dev_data *data = i3c_dev_get_master_data(dev);
927 	struct i3c_master_controller *m = i3c_dev_get_master(dev);
928 	struct dw_i3c_master *master = to_dw_i3c_master(m);
929 	int pos;
930 
931 	pos = dw_i3c_master_get_free_pos(master);
932 
933 	if (data->index > pos && pos > 0) {
934 		writel(0,
935 		       master->regs +
936 		       DEV_ADDR_TABLE_LOC(master->datstartaddr, data->index));
937 
938 		master->devs[data->index].addr = 0;
939 		master->free_pos |= BIT(data->index);
940 
941 		data->index = pos;
942 		master->devs[pos].addr = dev->info.dyn_addr;
943 		master->free_pos &= ~BIT(pos);
944 	}
945 
946 	writel(DEV_ADDR_TABLE_DYNAMIC_ADDR(dev->info.dyn_addr),
947 	       master->regs +
948 	       DEV_ADDR_TABLE_LOC(master->datstartaddr, data->index));
949 
950 	master->devs[data->index].addr = dev->info.dyn_addr;
951 
952 	return 0;
953 }
954 
955 static int dw_i3c_master_attach_i3c_dev(struct i3c_dev_desc *dev)
956 {
957 	struct i3c_master_controller *m = i3c_dev_get_master(dev);
958 	struct dw_i3c_master *master = to_dw_i3c_master(m);
959 	struct dw_i3c_i2c_dev_data *data;
960 	int pos;
961 
962 	pos = dw_i3c_master_get_free_pos(master);
963 	if (pos < 0)
964 		return pos;
965 
966 	data = kzalloc(sizeof(*data), GFP_KERNEL);
967 	if (!data)
968 		return -ENOMEM;
969 
970 	data->index = pos;
971 	master->devs[pos].addr = dev->info.dyn_addr ? : dev->info.static_addr;
972 	master->free_pos &= ~BIT(pos);
973 	i3c_dev_set_master_data(dev, data);
974 
975 	writel(DEV_ADDR_TABLE_DYNAMIC_ADDR(master->devs[pos].addr),
976 	       master->regs +
977 	       DEV_ADDR_TABLE_LOC(master->datstartaddr, data->index));
978 
979 	return 0;
980 }
981 
982 static void dw_i3c_master_detach_i3c_dev(struct i3c_dev_desc *dev)
983 {
984 	struct dw_i3c_i2c_dev_data *data = i3c_dev_get_master_data(dev);
985 	struct i3c_master_controller *m = i3c_dev_get_master(dev);
986 	struct dw_i3c_master *master = to_dw_i3c_master(m);
987 
988 	writel(0,
989 	       master->regs +
990 	       DEV_ADDR_TABLE_LOC(master->datstartaddr, data->index));
991 
992 	i3c_dev_set_master_data(dev, NULL);
993 	master->devs[data->index].addr = 0;
994 	master->free_pos |= BIT(data->index);
995 	kfree(data);
996 }
997 
998 static int dw_i3c_master_i2c_xfers(struct i2c_dev_desc *dev,
999 				   const struct i2c_msg *i2c_xfers,
1000 				   int i2c_nxfers)
1001 {
1002 	struct dw_i3c_i2c_dev_data *data = i2c_dev_get_master_data(dev);
1003 	struct i3c_master_controller *m = i2c_dev_get_master(dev);
1004 	struct dw_i3c_master *master = to_dw_i3c_master(m);
1005 	unsigned int nrxwords = 0, ntxwords = 0;
1006 	struct dw_i3c_xfer *xfer;
1007 	int i, ret = 0;
1008 
1009 	if (!i2c_nxfers)
1010 		return 0;
1011 
1012 	if (i2c_nxfers > master->caps.cmdfifodepth)
1013 		return -ENOTSUPP;
1014 
1015 	for (i = 0; i < i2c_nxfers; i++) {
1016 		if (i2c_xfers[i].flags & I2C_M_RD)
1017 			nrxwords += DIV_ROUND_UP(i2c_xfers[i].len, 4);
1018 		else
1019 			ntxwords += DIV_ROUND_UP(i2c_xfers[i].len, 4);
1020 	}
1021 
1022 	if (ntxwords > master->caps.datafifodepth ||
1023 	    nrxwords > master->caps.datafifodepth)
1024 		return -ENOTSUPP;
1025 
1026 	xfer = dw_i3c_master_alloc_xfer(master, i2c_nxfers);
1027 	if (!xfer)
1028 		return -ENOMEM;
1029 
1030 	for (i = 0; i < i2c_nxfers; i++) {
1031 		struct dw_i3c_cmd *cmd = &xfer->cmds[i];
1032 
1033 		cmd->cmd_hi = COMMAND_PORT_ARG_DATA_LEN(i2c_xfers[i].len) |
1034 			COMMAND_PORT_TRANSFER_ARG;
1035 
1036 		cmd->cmd_lo = COMMAND_PORT_TID(i) |
1037 			      COMMAND_PORT_DEV_INDEX(data->index) |
1038 			      COMMAND_PORT_ROC;
1039 
1040 		if (i2c_xfers[i].flags & I2C_M_RD) {
1041 			cmd->cmd_lo |= COMMAND_PORT_READ_TRANSFER;
1042 			cmd->rx_buf = i2c_xfers[i].buf;
1043 			cmd->rx_len = i2c_xfers[i].len;
1044 		} else {
1045 			cmd->tx_buf = i2c_xfers[i].buf;
1046 			cmd->tx_len = i2c_xfers[i].len;
1047 		}
1048 
1049 		if (i == (i2c_nxfers - 1))
1050 			cmd->cmd_lo |= COMMAND_PORT_TOC;
1051 	}
1052 
1053 	dw_i3c_master_enqueue_xfer(master, xfer);
1054 	if (!wait_for_completion_timeout(&xfer->comp, XFER_TIMEOUT))
1055 		dw_i3c_master_dequeue_xfer(master, xfer);
1056 
1057 	ret = xfer->ret;
1058 	dw_i3c_master_free_xfer(xfer);
1059 
1060 	return ret;
1061 }
1062 
1063 static int dw_i3c_master_attach_i2c_dev(struct i2c_dev_desc *dev)
1064 {
1065 	struct i3c_master_controller *m = i2c_dev_get_master(dev);
1066 	struct dw_i3c_master *master = to_dw_i3c_master(m);
1067 	struct dw_i3c_i2c_dev_data *data;
1068 	int pos;
1069 
1070 	pos = dw_i3c_master_get_free_pos(master);
1071 	if (pos < 0)
1072 		return pos;
1073 
1074 	data = kzalloc(sizeof(*data), GFP_KERNEL);
1075 	if (!data)
1076 		return -ENOMEM;
1077 
1078 	data->index = pos;
1079 	master->devs[pos].addr = dev->addr;
1080 	master->free_pos &= ~BIT(pos);
1081 	i2c_dev_set_master_data(dev, data);
1082 
1083 	writel(DEV_ADDR_TABLE_LEGACY_I2C_DEV |
1084 	       DEV_ADDR_TABLE_STATIC_ADDR(dev->addr),
1085 	       master->regs +
1086 	       DEV_ADDR_TABLE_LOC(master->datstartaddr, data->index));
1087 
1088 	return 0;
1089 }
1090 
1091 static void dw_i3c_master_detach_i2c_dev(struct i2c_dev_desc *dev)
1092 {
1093 	struct dw_i3c_i2c_dev_data *data = i2c_dev_get_master_data(dev);
1094 	struct i3c_master_controller *m = i2c_dev_get_master(dev);
1095 	struct dw_i3c_master *master = to_dw_i3c_master(m);
1096 
1097 	writel(0,
1098 	       master->regs +
1099 	       DEV_ADDR_TABLE_LOC(master->datstartaddr, data->index));
1100 
1101 	i2c_dev_set_master_data(dev, NULL);
1102 	master->devs[data->index].addr = 0;
1103 	master->free_pos |= BIT(data->index);
1104 	kfree(data);
1105 }
1106 
1107 static int dw_i3c_master_request_ibi(struct i3c_dev_desc *dev,
1108 				     const struct i3c_ibi_setup *req)
1109 {
1110 	struct dw_i3c_i2c_dev_data *data = i3c_dev_get_master_data(dev);
1111 	struct i3c_master_controller *m = i3c_dev_get_master(dev);
1112 	struct dw_i3c_master *master = to_dw_i3c_master(m);
1113 	unsigned long flags;
1114 
1115 	data->ibi_pool = i3c_generic_ibi_alloc_pool(dev, req);
1116 	if (IS_ERR(data->ibi_pool))
1117 		return PTR_ERR(data->ibi_pool);
1118 
1119 	spin_lock_irqsave(&master->devs_lock, flags);
1120 	master->devs[data->index].ibi_dev = dev;
1121 	spin_unlock_irqrestore(&master->devs_lock, flags);
1122 
1123 	return 0;
1124 }
1125 
1126 static void dw_i3c_master_free_ibi(struct i3c_dev_desc *dev)
1127 {
1128 	struct dw_i3c_i2c_dev_data *data = i3c_dev_get_master_data(dev);
1129 	struct i3c_master_controller *m = i3c_dev_get_master(dev);
1130 	struct dw_i3c_master *master = to_dw_i3c_master(m);
1131 	unsigned long flags;
1132 
1133 	spin_lock_irqsave(&master->devs_lock, flags);
1134 	master->devs[data->index].ibi_dev = NULL;
1135 	spin_unlock_irqrestore(&master->devs_lock, flags);
1136 
1137 	i3c_generic_ibi_free_pool(data->ibi_pool);
1138 	data->ibi_pool = NULL;
1139 }
1140 
1141 static void dw_i3c_master_enable_sir_signal(struct dw_i3c_master *master, bool enable)
1142 {
1143 	u32 reg;
1144 
1145 	reg = readl(master->regs + INTR_STATUS_EN);
1146 	reg &= ~INTR_IBI_THLD_STAT;
1147 	if (enable)
1148 		reg |= INTR_IBI_THLD_STAT;
1149 	writel(reg, master->regs + INTR_STATUS_EN);
1150 
1151 	reg = readl(master->regs + INTR_SIGNAL_EN);
1152 	reg &= ~INTR_IBI_THLD_STAT;
1153 	if (enable)
1154 		reg |= INTR_IBI_THLD_STAT;
1155 	writel(reg, master->regs + INTR_SIGNAL_EN);
1156 }
1157 
1158 static void dw_i3c_master_set_sir_enabled(struct dw_i3c_master *master,
1159 					  struct i3c_dev_desc *dev,
1160 					  u8 idx, bool enable)
1161 {
1162 	unsigned long flags;
1163 	u32 dat_entry, reg;
1164 	bool global;
1165 
1166 	dat_entry = DEV_ADDR_TABLE_LOC(master->datstartaddr, idx);
1167 
1168 	spin_lock_irqsave(&master->devs_lock, flags);
1169 	reg = readl(master->regs + dat_entry);
1170 	if (enable) {
1171 		reg &= ~DEV_ADDR_TABLE_SIR_REJECT;
1172 		if (dev->info.bcr & I3C_BCR_IBI_PAYLOAD)
1173 			reg |= DEV_ADDR_TABLE_IBI_MDB;
1174 	} else {
1175 		reg |= DEV_ADDR_TABLE_SIR_REJECT;
1176 	}
1177 	master->platform_ops->set_dat_ibi(master, dev, enable, &reg);
1178 	writel(reg, master->regs + dat_entry);
1179 
1180 	if (enable) {
1181 		global = (master->sir_rej_mask == IBI_REQ_REJECT_ALL);
1182 		master->sir_rej_mask &= ~BIT(idx);
1183 	} else {
1184 		bool hj_rejected = !!(readl(master->regs + DEVICE_CTRL) & DEV_CTRL_HOT_JOIN_NACK);
1185 
1186 		master->sir_rej_mask |= BIT(idx);
1187 		global = (master->sir_rej_mask == IBI_REQ_REJECT_ALL) && hj_rejected;
1188 	}
1189 	writel(master->sir_rej_mask, master->regs + IBI_SIR_REQ_REJECT);
1190 
1191 	if (global)
1192 		dw_i3c_master_enable_sir_signal(master, enable);
1193 
1194 
1195 	spin_unlock_irqrestore(&master->devs_lock, flags);
1196 }
1197 
1198 static int dw_i3c_master_enable_hotjoin(struct i3c_master_controller *m)
1199 {
1200 	struct dw_i3c_master *master = to_dw_i3c_master(m);
1201 
1202 	dw_i3c_master_enable_sir_signal(master, true);
1203 	writel(readl(master->regs + DEVICE_CTRL) & ~DEV_CTRL_HOT_JOIN_NACK,
1204 	       master->regs + DEVICE_CTRL);
1205 
1206 	return 0;
1207 }
1208 
1209 static int dw_i3c_master_disable_hotjoin(struct i3c_master_controller *m)
1210 {
1211 	struct dw_i3c_master *master = to_dw_i3c_master(m);
1212 
1213 	writel(readl(master->regs + DEVICE_CTRL) | DEV_CTRL_HOT_JOIN_NACK,
1214 	       master->regs + DEVICE_CTRL);
1215 
1216 	return 0;
1217 }
1218 
1219 static int dw_i3c_master_enable_ibi(struct i3c_dev_desc *dev)
1220 {
1221 	struct dw_i3c_i2c_dev_data *data = i3c_dev_get_master_data(dev);
1222 	struct i3c_master_controller *m = i3c_dev_get_master(dev);
1223 	struct dw_i3c_master *master = to_dw_i3c_master(m);
1224 	int rc;
1225 
1226 	dw_i3c_master_set_sir_enabled(master, dev, data->index, true);
1227 
1228 	rc = i3c_master_enec_locked(m, dev->info.dyn_addr, I3C_CCC_EVENT_SIR);
1229 
1230 	if (rc)
1231 		dw_i3c_master_set_sir_enabled(master, dev, data->index, false);
1232 
1233 	return rc;
1234 }
1235 
1236 static int dw_i3c_master_disable_ibi(struct i3c_dev_desc *dev)
1237 {
1238 	struct dw_i3c_i2c_dev_data *data = i3c_dev_get_master_data(dev);
1239 	struct i3c_master_controller *m = i3c_dev_get_master(dev);
1240 	struct dw_i3c_master *master = to_dw_i3c_master(m);
1241 	int rc;
1242 
1243 	rc = i3c_master_disec_locked(m, dev->info.dyn_addr, I3C_CCC_EVENT_SIR);
1244 	if (rc)
1245 		return rc;
1246 
1247 	dw_i3c_master_set_sir_enabled(master, dev, data->index, false);
1248 
1249 	return 0;
1250 }
1251 
1252 static void dw_i3c_master_recycle_ibi_slot(struct i3c_dev_desc *dev,
1253 					   struct i3c_ibi_slot *slot)
1254 {
1255 	struct dw_i3c_i2c_dev_data *data = i3c_dev_get_master_data(dev);
1256 
1257 	i3c_generic_ibi_recycle_slot(data->ibi_pool, slot);
1258 }
1259 
1260 static void dw_i3c_master_drain_ibi_queue(struct dw_i3c_master *master,
1261 					  int len)
1262 {
1263 	int i;
1264 
1265 	for (i = 0; i < DIV_ROUND_UP(len, 4); i++)
1266 		readl(master->regs + IBI_QUEUE_STATUS);
1267 }
1268 
1269 static void dw_i3c_master_handle_ibi_sir(struct dw_i3c_master *master,
1270 					 u32 status)
1271 {
1272 	struct dw_i3c_i2c_dev_data *data;
1273 	struct i3c_ibi_slot *slot;
1274 	struct i3c_dev_desc *dev;
1275 	unsigned long flags;
1276 	u8 addr, len;
1277 	int idx;
1278 
1279 	addr = IBI_QUEUE_IBI_ADDR(status);
1280 	len = IBI_QUEUE_STATUS_DATA_LEN(status);
1281 
1282 	/*
1283 	 * We be tempted to check the error status in bit 30; however, due
1284 	 * to the PEC errata workaround on some platform implementations (see
1285 	 * ast2600_i3c_set_dat_ibi()), those will almost always have a PEC
1286 	 * error on IBI payload data, as well as losing the last byte of
1287 	 * payload.
1288 	 *
1289 	 * If we implement error status checking on that bit, we may need
1290 	 * a new platform op to validate it.
1291 	 */
1292 
1293 	spin_lock_irqsave(&master->devs_lock, flags);
1294 	idx = dw_i3c_master_get_addr_pos(master, addr);
1295 	if (idx < 0) {
1296 		dev_dbg_ratelimited(&master->base.dev,
1297 			 "IBI from unknown addr 0x%x\n", addr);
1298 		goto err_drain;
1299 	}
1300 
1301 	dev = master->devs[idx].ibi_dev;
1302 	if (!dev || !dev->ibi) {
1303 		dev_dbg_ratelimited(&master->base.dev,
1304 			 "IBI from non-requested dev idx %d\n", idx);
1305 		goto err_drain;
1306 	}
1307 
1308 	data = i3c_dev_get_master_data(dev);
1309 	slot = i3c_generic_ibi_get_free_slot(data->ibi_pool);
1310 	if (!slot) {
1311 		dev_dbg_ratelimited(&master->base.dev,
1312 				    "No IBI slots available\n");
1313 		goto err_drain;
1314 	}
1315 
1316 	if (dev->ibi->max_payload_len < len) {
1317 		dev_dbg_ratelimited(&master->base.dev,
1318 				    "IBI payload len %d greater than max %d\n",
1319 				    len, dev->ibi->max_payload_len);
1320 		goto err_drain;
1321 	}
1322 
1323 	if (len) {
1324 		dw_i3c_master_read_ibi_fifo(master, slot->data, len);
1325 		slot->len = len;
1326 	}
1327 	i3c_master_queue_ibi(dev, slot);
1328 
1329 	spin_unlock_irqrestore(&master->devs_lock, flags);
1330 
1331 	return;
1332 
1333 err_drain:
1334 	dw_i3c_master_drain_ibi_queue(master, len);
1335 
1336 	spin_unlock_irqrestore(&master->devs_lock, flags);
1337 }
1338 
1339 /* "ibis": referring to In-Band Interrupts, and not
1340  * https://en.wikipedia.org/wiki/Australian_white_ibis. The latter should
1341  * not be handled.
1342  */
1343 static void dw_i3c_master_irq_handle_ibis(struct dw_i3c_master *master)
1344 {
1345 	unsigned int i, len, n_ibis;
1346 	u32 reg;
1347 
1348 	reg = readl(master->regs + QUEUE_STATUS_LEVEL);
1349 	n_ibis = QUEUE_STATUS_IBI_STATUS_CNT(reg);
1350 	if (!n_ibis)
1351 		return;
1352 
1353 	for (i = 0; i < n_ibis; i++) {
1354 		reg = readl(master->regs + IBI_QUEUE_STATUS);
1355 
1356 		if (IBI_TYPE_SIRQ(reg)) {
1357 			dw_i3c_master_handle_ibi_sir(master, reg);
1358 		} else if (IBI_TYPE_HJ(reg)) {
1359 			queue_work(master->base.wq, &master->hj_work);
1360 		} else {
1361 			len = IBI_QUEUE_STATUS_DATA_LEN(reg);
1362 			dev_info(&master->base.dev,
1363 				 "unsupported IBI type 0x%lx len %d\n",
1364 				 IBI_QUEUE_STATUS_IBI_ID(reg), len);
1365 			dw_i3c_master_drain_ibi_queue(master, len);
1366 		}
1367 	}
1368 }
1369 
1370 static irqreturn_t dw_i3c_master_irq_handler(int irq, void *dev_id)
1371 {
1372 	struct dw_i3c_master *master = dev_id;
1373 	u32 status;
1374 
1375 	status = readl(master->regs + INTR_STATUS);
1376 
1377 	if (!(status & readl(master->regs + INTR_STATUS_EN))) {
1378 		writel(INTR_ALL, master->regs + INTR_STATUS);
1379 		return IRQ_NONE;
1380 	}
1381 
1382 	spin_lock(&master->xferqueue.lock);
1383 	dw_i3c_master_end_xfer_locked(master, status);
1384 	if (status & INTR_TRANSFER_ERR_STAT)
1385 		writel(INTR_TRANSFER_ERR_STAT, master->regs + INTR_STATUS);
1386 	spin_unlock(&master->xferqueue.lock);
1387 
1388 	if (status & INTR_IBI_THLD_STAT)
1389 		dw_i3c_master_irq_handle_ibis(master);
1390 
1391 	return IRQ_HANDLED;
1392 }
1393 
1394 static const struct i3c_master_controller_ops dw_mipi_i3c_ops = {
1395 	.bus_init = dw_i3c_master_bus_init,
1396 	.bus_cleanup = dw_i3c_master_bus_cleanup,
1397 	.attach_i3c_dev = dw_i3c_master_attach_i3c_dev,
1398 	.reattach_i3c_dev = dw_i3c_master_reattach_i3c_dev,
1399 	.detach_i3c_dev = dw_i3c_master_detach_i3c_dev,
1400 	.do_daa = dw_i3c_master_daa,
1401 	.supports_ccc_cmd = dw_i3c_master_supports_ccc_cmd,
1402 	.send_ccc_cmd = dw_i3c_master_send_ccc_cmd,
1403 	.priv_xfers = dw_i3c_master_priv_xfers,
1404 	.attach_i2c_dev = dw_i3c_master_attach_i2c_dev,
1405 	.detach_i2c_dev = dw_i3c_master_detach_i2c_dev,
1406 	.i2c_xfers = dw_i3c_master_i2c_xfers,
1407 	.request_ibi = dw_i3c_master_request_ibi,
1408 	.free_ibi = dw_i3c_master_free_ibi,
1409 	.enable_ibi = dw_i3c_master_enable_ibi,
1410 	.disable_ibi = dw_i3c_master_disable_ibi,
1411 	.recycle_ibi_slot = dw_i3c_master_recycle_ibi_slot,
1412 	.enable_hotjoin = dw_i3c_master_enable_hotjoin,
1413 	.disable_hotjoin = dw_i3c_master_disable_hotjoin,
1414 };
1415 
1416 /* default platform ops implementations */
1417 static int dw_i3c_platform_init_nop(struct dw_i3c_master *i3c)
1418 {
1419 	return 0;
1420 }
1421 
1422 static void dw_i3c_platform_set_dat_ibi_nop(struct dw_i3c_master *i3c,
1423 					struct i3c_dev_desc *dev,
1424 					bool enable, u32 *dat)
1425 {
1426 }
1427 
1428 static const struct dw_i3c_platform_ops dw_i3c_platform_ops_default = {
1429 	.init = dw_i3c_platform_init_nop,
1430 	.set_dat_ibi = dw_i3c_platform_set_dat_ibi_nop,
1431 };
1432 
1433 static void dw_i3c_hj_work(struct work_struct *work)
1434 {
1435 	struct dw_i3c_master *master =
1436 		container_of(work, typeof(*master), hj_work);
1437 
1438 	i3c_master_do_daa(&master->base);
1439 }
1440 
1441 int dw_i3c_common_probe(struct dw_i3c_master *master,
1442 			struct platform_device *pdev)
1443 {
1444 	int ret, irq;
1445 
1446 	if (!master->platform_ops)
1447 		master->platform_ops = &dw_i3c_platform_ops_default;
1448 
1449 	master->regs = devm_platform_ioremap_resource(pdev, 0);
1450 	if (IS_ERR(master->regs))
1451 		return PTR_ERR(master->regs);
1452 
1453 	master->core_clk = devm_clk_get_enabled(&pdev->dev, NULL);
1454 	if (IS_ERR(master->core_clk))
1455 		return PTR_ERR(master->core_clk);
1456 
1457 	master->pclk = devm_clk_get_optional_enabled(&pdev->dev, "pclk");
1458 	if (IS_ERR(master->pclk))
1459 		return PTR_ERR(master->pclk);
1460 
1461 	master->core_rst = devm_reset_control_get_optional_exclusive(&pdev->dev,
1462 								    "core_rst");
1463 	if (IS_ERR(master->core_rst))
1464 		return PTR_ERR(master->core_rst);
1465 
1466 	reset_control_deassert(master->core_rst);
1467 
1468 	spin_lock_init(&master->xferqueue.lock);
1469 	INIT_LIST_HEAD(&master->xferqueue.list);
1470 
1471 	writel(INTR_ALL, master->regs + INTR_STATUS);
1472 	irq = platform_get_irq(pdev, 0);
1473 	ret = devm_request_irq(&pdev->dev, irq,
1474 			       dw_i3c_master_irq_handler, 0,
1475 			       dev_name(&pdev->dev), master);
1476 	if (ret)
1477 		goto err_assert_rst;
1478 
1479 	platform_set_drvdata(pdev, master);
1480 
1481 	/* Information regarding the FIFOs/QUEUEs depth */
1482 	ret = readl(master->regs + QUEUE_STATUS_LEVEL);
1483 	master->caps.cmdfifodepth = QUEUE_STATUS_LEVEL_CMD(ret);
1484 
1485 	ret = readl(master->regs + DATA_BUFFER_STATUS_LEVEL);
1486 	master->caps.datafifodepth = DATA_BUFFER_STATUS_LEVEL_TX(ret);
1487 
1488 	ret = readl(master->regs + DEVICE_ADDR_TABLE_POINTER);
1489 	master->datstartaddr = ret;
1490 	master->maxdevs = ret >> 16;
1491 	master->free_pos = GENMASK(master->maxdevs - 1, 0);
1492 
1493 	INIT_WORK(&master->hj_work, dw_i3c_hj_work);
1494 	ret = i3c_master_register(&master->base, &pdev->dev,
1495 				  &dw_mipi_i3c_ops, false);
1496 	if (ret)
1497 		goto err_assert_rst;
1498 
1499 	return 0;
1500 
1501 err_assert_rst:
1502 	reset_control_assert(master->core_rst);
1503 
1504 	return ret;
1505 }
1506 EXPORT_SYMBOL_GPL(dw_i3c_common_probe);
1507 
1508 void dw_i3c_common_remove(struct dw_i3c_master *master)
1509 {
1510 	i3c_master_unregister(&master->base);
1511 
1512 	reset_control_assert(master->core_rst);
1513 }
1514 EXPORT_SYMBOL_GPL(dw_i3c_common_remove);
1515 
1516 /* base platform implementation */
1517 
1518 static int dw_i3c_probe(struct platform_device *pdev)
1519 {
1520 	struct dw_i3c_master *master;
1521 
1522 	master = devm_kzalloc(&pdev->dev, sizeof(*master), GFP_KERNEL);
1523 	if (!master)
1524 		return -ENOMEM;
1525 
1526 	return dw_i3c_common_probe(master, pdev);
1527 }
1528 
1529 static void dw_i3c_remove(struct platform_device *pdev)
1530 {
1531 	struct dw_i3c_master *master = platform_get_drvdata(pdev);
1532 
1533 	dw_i3c_common_remove(master);
1534 }
1535 
1536 static const struct of_device_id dw_i3c_master_of_match[] = {
1537 	{ .compatible = "snps,dw-i3c-master-1.00a", },
1538 	{},
1539 };
1540 MODULE_DEVICE_TABLE(of, dw_i3c_master_of_match);
1541 
1542 static struct platform_driver dw_i3c_driver = {
1543 	.probe = dw_i3c_probe,
1544 	.remove_new = dw_i3c_remove,
1545 	.driver = {
1546 		.name = "dw-i3c-master",
1547 		.of_match_table = dw_i3c_master_of_match,
1548 	},
1549 };
1550 module_platform_driver(dw_i3c_driver);
1551 
1552 MODULE_AUTHOR("Vitor Soares <vitor.soares@synopsys.com>");
1553 MODULE_DESCRIPTION("DesignWare MIPI I3C driver");
1554 MODULE_LICENSE("GPL v2");
1555