1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2018 Synopsys, Inc. and/or its affiliates.
5 * Author: Vitor Soares <vitor.soares@synopsys.com>
8 #include <linux/bitops.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>
20 #include <linux/pinctrl/consumer.h>
21 #include <linux/platform_device.h>
22 #include <linux/pm_runtime.h>
23 #include <linux/reset.h>
24 #include <linux/slab.h>
26 #include "dw-i3c-master.h"
28 #define DEVICE_CTRL 0x0
29 #define DEV_CTRL_ENABLE BIT(31)
30 #define DEV_CTRL_RESUME BIT(30)
31 #define DEV_CTRL_HOT_JOIN_NACK BIT(8)
32 #define DEV_CTRL_I2C_SLAVE_PRESENT BIT(7)
34 #define DEVICE_ADDR 0x4
35 #define DEV_ADDR_DYNAMIC_ADDR_VALID BIT(31)
36 #define DEV_ADDR_DYNAMIC(x) (((x) << 16) & GENMASK(22, 16))
38 #define HW_CAPABILITY 0x8
39 #define COMMAND_QUEUE_PORT 0xc
40 #define COMMAND_PORT_TOC BIT(30)
41 #define COMMAND_PORT_READ_TRANSFER BIT(28)
42 #define COMMAND_PORT_SDAP BIT(27)
43 #define COMMAND_PORT_ROC BIT(26)
44 #define COMMAND_PORT_SPEED(x) (((x) << 21) & GENMASK(23, 21))
45 #define COMMAND_PORT_DEV_INDEX(x) (((x) << 16) & GENMASK(20, 16))
46 #define COMMAND_PORT_CP BIT(15)
47 #define COMMAND_PORT_CMD(x) (((x) << 7) & GENMASK(14, 7))
48 #define COMMAND_PORT_TID(x) (((x) << 3) & GENMASK(6, 3))
50 #define COMMAND_PORT_ARG_DATA_LEN(x) (((x) << 16) & GENMASK(31, 16))
51 #define COMMAND_PORT_ARG_DATA_LEN_MAX 65536
52 #define COMMAND_PORT_TRANSFER_ARG 0x01
54 #define COMMAND_PORT_SDA_DATA_BYTE_3(x) (((x) << 24) & GENMASK(31, 24))
55 #define COMMAND_PORT_SDA_DATA_BYTE_2(x) (((x) << 16) & GENMASK(23, 16))
56 #define COMMAND_PORT_SDA_DATA_BYTE_1(x) (((x) << 8) & GENMASK(15, 8))
57 #define COMMAND_PORT_SDA_BYTE_STRB_3 BIT(5)
58 #define COMMAND_PORT_SDA_BYTE_STRB_2 BIT(4)
59 #define COMMAND_PORT_SDA_BYTE_STRB_1 BIT(3)
60 #define COMMAND_PORT_SHORT_DATA_ARG 0x02
62 #define COMMAND_PORT_DEV_COUNT(x) (((x) << 21) & GENMASK(25, 21))
63 #define COMMAND_PORT_ADDR_ASSGN_CMD 0x03
65 #define RESPONSE_QUEUE_PORT 0x10
66 #define RESPONSE_PORT_ERR_STATUS(x) (((x) & GENMASK(31, 28)) >> 28)
67 #define RESPONSE_NO_ERROR 0
68 #define RESPONSE_ERROR_CRC 1
69 #define RESPONSE_ERROR_PARITY 2
70 #define RESPONSE_ERROR_FRAME 3
71 #define RESPONSE_ERROR_IBA_NACK 4
72 #define RESPONSE_ERROR_ADDRESS_NACK 5
73 #define RESPONSE_ERROR_OVER_UNDER_FLOW 6
74 #define RESPONSE_ERROR_TRANSF_ABORT 8
75 #define RESPONSE_ERROR_I2C_W_NACK_ERR 9
76 #define RESPONSE_PORT_TID(x) (((x) & GENMASK(27, 24)) >> 24)
77 #define RESPONSE_PORT_DATA_LEN(x) ((x) & GENMASK(15, 0))
79 #define RX_TX_DATA_PORT 0x14
80 #define IBI_QUEUE_STATUS 0x18
81 #define IBI_QUEUE_STATUS_IBI_ID(x) (((x) & GENMASK(15, 8)) >> 8)
82 #define IBI_QUEUE_STATUS_DATA_LEN(x) ((x) & GENMASK(7, 0))
83 #define IBI_QUEUE_IBI_ADDR(x) (IBI_QUEUE_STATUS_IBI_ID(x) >> 1)
84 #define IBI_QUEUE_IBI_RNW(x) (IBI_QUEUE_STATUS_IBI_ID(x) & BIT(0))
85 #define IBI_TYPE_MR(x) \
86 ((IBI_QUEUE_IBI_ADDR(x) != I3C_HOT_JOIN_ADDR) && !IBI_QUEUE_IBI_RNW(x))
87 #define IBI_TYPE_HJ(x) \
88 ((IBI_QUEUE_IBI_ADDR(x) == I3C_HOT_JOIN_ADDR) && !IBI_QUEUE_IBI_RNW(x))
89 #define IBI_TYPE_SIRQ(x) \
90 ((IBI_QUEUE_IBI_ADDR(x) != I3C_HOT_JOIN_ADDR) && IBI_QUEUE_IBI_RNW(x))
92 #define QUEUE_THLD_CTRL 0x1c
93 #define QUEUE_THLD_CTRL_IBI_STAT_MASK GENMASK(31, 24)
94 #define QUEUE_THLD_CTRL_IBI_STAT(x) (((x) - 1) << 24)
95 #define QUEUE_THLD_CTRL_IBI_DATA_MASK GENMASK(20, 16)
96 #define QUEUE_THLD_CTRL_IBI_DATA(x) ((x) << 16)
97 #define QUEUE_THLD_CTRL_RESP_BUF_MASK GENMASK(15, 8)
98 #define QUEUE_THLD_CTRL_RESP_BUF(x) (((x) - 1) << 8)
100 #define DATA_BUFFER_THLD_CTRL 0x20
101 #define DATA_BUFFER_THLD_CTRL_RX_BUF GENMASK(11, 8)
103 #define IBI_QUEUE_CTRL 0x24
104 #define IBI_MR_REQ_REJECT 0x2C
105 #define IBI_SIR_REQ_REJECT 0x30
106 #define IBI_REQ_REJECT_ALL GENMASK(31, 0)
108 #define RESET_CTRL 0x34
109 #define RESET_CTRL_IBI_QUEUE BIT(5)
110 #define RESET_CTRL_RX_FIFO BIT(4)
111 #define RESET_CTRL_TX_FIFO BIT(3)
112 #define RESET_CTRL_RESP_QUEUE BIT(2)
113 #define RESET_CTRL_CMD_QUEUE BIT(1)
114 #define RESET_CTRL_SOFT BIT(0)
116 #define SLV_EVENT_CTRL 0x38
117 #define INTR_STATUS 0x3c
118 #define INTR_STATUS_EN 0x40
119 #define INTR_SIGNAL_EN 0x44
120 #define INTR_FORCE 0x48
121 #define INTR_BUSOWNER_UPDATE_STAT BIT(13)
122 #define INTR_IBI_UPDATED_STAT BIT(12)
123 #define INTR_READ_REQ_RECV_STAT BIT(11)
124 #define INTR_DEFSLV_STAT BIT(10)
125 #define INTR_TRANSFER_ERR_STAT BIT(9)
126 #define INTR_DYN_ADDR_ASSGN_STAT BIT(8)
127 #define INTR_CCC_UPDATED_STAT BIT(6)
128 #define INTR_TRANSFER_ABORT_STAT BIT(5)
129 #define INTR_RESP_READY_STAT BIT(4)
130 #define INTR_CMD_QUEUE_READY_STAT BIT(3)
131 #define INTR_IBI_THLD_STAT BIT(2)
132 #define INTR_RX_THLD_STAT BIT(1)
133 #define INTR_TX_THLD_STAT BIT(0)
134 #define INTR_ALL (INTR_BUSOWNER_UPDATE_STAT | \
135 INTR_IBI_UPDATED_STAT | \
136 INTR_READ_REQ_RECV_STAT | \
138 INTR_TRANSFER_ERR_STAT | \
139 INTR_DYN_ADDR_ASSGN_STAT | \
140 INTR_CCC_UPDATED_STAT | \
141 INTR_TRANSFER_ABORT_STAT | \
142 INTR_RESP_READY_STAT | \
143 INTR_CMD_QUEUE_READY_STAT | \
144 INTR_IBI_THLD_STAT | \
145 INTR_TX_THLD_STAT | \
148 #define INTR_MASTER_MASK (INTR_TRANSFER_ERR_STAT | \
149 INTR_RESP_READY_STAT)
151 #define QUEUE_STATUS_LEVEL 0x4c
152 #define QUEUE_STATUS_IBI_STATUS_CNT(x) (((x) & GENMASK(28, 24)) >> 24)
153 #define QUEUE_STATUS_IBI_BUF_BLR(x) (((x) & GENMASK(23, 16)) >> 16)
154 #define QUEUE_STATUS_LEVEL_RESP(x) (((x) & GENMASK(15, 8)) >> 8)
155 #define QUEUE_STATUS_LEVEL_CMD(x) ((x) & GENMASK(7, 0))
157 #define DATA_BUFFER_STATUS_LEVEL 0x50
158 #define DATA_BUFFER_STATUS_LEVEL_TX(x) ((x) & GENMASK(7, 0))
160 #define PRESENT_STATE 0x54
161 #define CCC_DEVICE_STATUS 0x58
162 #define DEVICE_ADDR_TABLE_POINTER 0x5c
163 #define DEVICE_ADDR_TABLE_DEPTH(x) (((x) & GENMASK(31, 16)) >> 16)
164 #define DEVICE_ADDR_TABLE_ADDR(x) ((x) & GENMASK(7, 0))
166 #define DEV_CHAR_TABLE_POINTER 0x60
167 #define VENDOR_SPECIFIC_REG_POINTER 0x6c
168 #define SLV_PID_VALUE 0x74
169 #define SLV_CHAR_CTRL 0x78
170 #define SLV_MAX_LEN 0x7c
171 #define MAX_READ_TURNAROUND 0x80
172 #define MAX_DATA_SPEED 0x84
173 #define SLV_DEBUG_STATUS 0x88
174 #define SLV_INTR_REQ 0x8c
175 #define DEVICE_CTRL_EXTENDED 0xb0
176 #define SCL_I3C_OD_TIMING 0xb4
177 #define SCL_I3C_PP_TIMING 0xb8
178 #define SCL_I3C_TIMING_HCNT(x) (((x) << 16) & GENMASK(23, 16))
179 #define SCL_I3C_TIMING_LCNT(x) ((x) & GENMASK(7, 0))
180 #define SCL_I3C_TIMING_CNT_MIN 5
182 #define SCL_I2C_FM_TIMING 0xbc
183 #define SCL_I2C_FM_TIMING_HCNT(x) (((x) << 16) & GENMASK(31, 16))
184 #define SCL_I2C_FM_TIMING_LCNT(x) ((x) & GENMASK(15, 0))
186 #define SCL_I2C_FMP_TIMING 0xc0
187 #define SCL_I2C_FMP_TIMING_HCNT(x) (((x) << 16) & GENMASK(23, 16))
188 #define SCL_I2C_FMP_TIMING_LCNT(x) ((x) & GENMASK(15, 0))
190 #define SCL_EXT_LCNT_TIMING 0xc8
191 #define SCL_EXT_LCNT_4(x) (((x) << 24) & GENMASK(31, 24))
192 #define SCL_EXT_LCNT_3(x) (((x) << 16) & GENMASK(23, 16))
193 #define SCL_EXT_LCNT_2(x) (((x) << 8) & GENMASK(15, 8))
194 #define SCL_EXT_LCNT_1(x) ((x) & GENMASK(7, 0))
196 #define SCL_EXT_TERMN_LCNT_TIMING 0xcc
197 #define BUS_FREE_TIMING 0xd4
198 #define BUS_I3C_MST_FREE(x) ((x) & GENMASK(15, 0))
200 #define BUS_IDLE_TIMING 0xd8
201 #define I3C_VER_ID 0xe0
202 #define I3C_VER_TYPE 0xe4
203 #define EXTENDED_CAPABILITY 0xe8
204 #define SLAVE_CONFIG 0xec
206 #define DEV_ADDR_TABLE_IBI_MDB BIT(12)
207 #define DEV_ADDR_TABLE_SIR_REJECT BIT(13)
208 #define DEV_ADDR_TABLE_LEGACY_I2C_DEV BIT(31)
209 #define DEV_ADDR_TABLE_DYNAMIC_ADDR(x) (((x) << 16) & GENMASK(23, 16))
210 #define DEV_ADDR_TABLE_STATIC_ADDR(x) ((x) & GENMASK(6, 0))
211 #define DEV_ADDR_TABLE_LOC(start, idx) ((start) + ((idx) << 2))
213 #define I3C_BUS_SDR1_SCL_RATE 8000000
214 #define I3C_BUS_SDR2_SCL_RATE 6000000
215 #define I3C_BUS_SDR3_SCL_RATE 4000000
216 #define I3C_BUS_SDR4_SCL_RATE 2000000
217 #define I3C_BUS_I2C_FM_TLOW_MIN_NS 1300
218 #define I3C_BUS_I2C_FMP_TLOW_MIN_NS 500
219 #define I3C_BUS_THIGH_MAX_NS 41
221 #define XFER_TIMEOUT (msecs_to_jiffies(1000))
222 #define RPM_AUTOSUSPEND_TIMEOUT 1000 /* ms */
224 /* Timing values to configure 12.5MHz frequency */
225 #define AMD_I3C_OD_TIMING 0x4C007C
226 #define AMD_I3C_PP_TIMING 0x8001A
229 #define AMD_I3C_OD_PP_TIMING BIT(1)
242 struct list_head node
;
243 struct completion comp
;
246 struct dw_i3c_cmd cmds
[] __counted_by(ncmds
);
249 struct dw_i3c_i2c_dev_data
{
251 struct i3c_generic_ibi_pool
*ibi_pool
;
254 static u8
even_parity(u8 p
)
259 return (0x9669 >> p
) & 1;
262 static bool dw_i3c_master_supports_ccc_cmd(struct i3c_master_controller
*m
,
263 const struct i3c_ccc_cmd
*cmd
)
269 case I3C_CCC_ENEC(true):
270 case I3C_CCC_ENEC(false):
271 case I3C_CCC_DISEC(true):
272 case I3C_CCC_DISEC(false):
273 case I3C_CCC_ENTAS(0, true):
274 case I3C_CCC_ENTAS(0, false):
275 case I3C_CCC_RSTDAA(true):
276 case I3C_CCC_RSTDAA(false):
278 case I3C_CCC_SETMWL(true):
279 case I3C_CCC_SETMWL(false):
280 case I3C_CCC_SETMRL(true):
281 case I3C_CCC_SETMRL(false):
282 case I3C_CCC_ENTHDR(0):
283 case I3C_CCC_SETDASA
:
284 case I3C_CCC_SETNEWDA
:
290 case I3C_CCC_GETSTATUS
:
291 case I3C_CCC_GETMXDS
:
292 case I3C_CCC_GETHDRCAP
:
299 static inline struct dw_i3c_master
*
300 to_dw_i3c_master(struct i3c_master_controller
*master
)
302 return container_of(master
, struct dw_i3c_master
, base
);
305 static void dw_i3c_master_disable(struct dw_i3c_master
*master
)
307 writel(readl(master
->regs
+ DEVICE_CTRL
) & ~DEV_CTRL_ENABLE
,
308 master
->regs
+ DEVICE_CTRL
);
311 static void dw_i3c_master_enable(struct dw_i3c_master
*master
)
315 dev_ctrl
= readl(master
->regs
+ DEVICE_CTRL
);
316 /* For now don't support Hot-Join */
317 dev_ctrl
|= DEV_CTRL_HOT_JOIN_NACK
;
318 if (master
->i2c_slv_prsnt
)
319 dev_ctrl
|= DEV_CTRL_I2C_SLAVE_PRESENT
;
320 writel(dev_ctrl
| DEV_CTRL_ENABLE
,
321 master
->regs
+ DEVICE_CTRL
);
324 static int dw_i3c_master_get_addr_pos(struct dw_i3c_master
*master
, u8 addr
)
328 for (pos
= 0; pos
< master
->maxdevs
; pos
++) {
329 if (addr
== master
->devs
[pos
].addr
)
336 static int dw_i3c_master_get_free_pos(struct dw_i3c_master
*master
)
338 if (!(master
->free_pos
& GENMASK(master
->maxdevs
- 1, 0)))
341 return ffs(master
->free_pos
) - 1;
344 static void dw_i3c_master_wr_tx_fifo(struct dw_i3c_master
*master
,
345 const u8
*bytes
, int nbytes
)
347 writesl(master
->regs
+ RX_TX_DATA_PORT
, bytes
, nbytes
/ 4);
351 memcpy(&tmp
, bytes
+ (nbytes
& ~3), nbytes
& 3);
352 writesl(master
->regs
+ RX_TX_DATA_PORT
, &tmp
, 1);
356 static void dw_i3c_master_read_fifo(struct dw_i3c_master
*master
,
357 int reg
, u8
*bytes
, int nbytes
)
359 readsl(master
->regs
+ reg
, bytes
, nbytes
/ 4);
363 readsl(master
->regs
+ reg
, &tmp
, 1);
364 memcpy(bytes
+ (nbytes
& ~3), &tmp
, nbytes
& 3);
368 static void dw_i3c_master_read_rx_fifo(struct dw_i3c_master
*master
,
369 u8
*bytes
, int nbytes
)
371 return dw_i3c_master_read_fifo(master
, RX_TX_DATA_PORT
, bytes
, nbytes
);
374 static void dw_i3c_master_read_ibi_fifo(struct dw_i3c_master
*master
,
375 u8
*bytes
, int nbytes
)
377 return dw_i3c_master_read_fifo(master
, IBI_QUEUE_STATUS
, bytes
, nbytes
);
380 static struct dw_i3c_xfer
*
381 dw_i3c_master_alloc_xfer(struct dw_i3c_master
*master
, unsigned int ncmds
)
383 struct dw_i3c_xfer
*xfer
;
385 xfer
= kzalloc(struct_size(xfer
, cmds
, ncmds
), GFP_KERNEL
);
389 INIT_LIST_HEAD(&xfer
->node
);
391 xfer
->ret
= -ETIMEDOUT
;
396 static void dw_i3c_master_free_xfer(struct dw_i3c_xfer
*xfer
)
401 static void dw_i3c_master_start_xfer_locked(struct dw_i3c_master
*master
)
403 struct dw_i3c_xfer
*xfer
= master
->xferqueue
.cur
;
410 for (i
= 0; i
< xfer
->ncmds
; i
++) {
411 struct dw_i3c_cmd
*cmd
= &xfer
->cmds
[i
];
413 dw_i3c_master_wr_tx_fifo(master
, cmd
->tx_buf
, cmd
->tx_len
);
416 thld_ctrl
= readl(master
->regs
+ QUEUE_THLD_CTRL
);
417 thld_ctrl
&= ~QUEUE_THLD_CTRL_RESP_BUF_MASK
;
418 thld_ctrl
|= QUEUE_THLD_CTRL_RESP_BUF(xfer
->ncmds
);
419 writel(thld_ctrl
, master
->regs
+ QUEUE_THLD_CTRL
);
421 for (i
= 0; i
< xfer
->ncmds
; i
++) {
422 struct dw_i3c_cmd
*cmd
= &xfer
->cmds
[i
];
424 writel(cmd
->cmd_hi
, master
->regs
+ COMMAND_QUEUE_PORT
);
425 writel(cmd
->cmd_lo
, master
->regs
+ COMMAND_QUEUE_PORT
);
429 static void dw_i3c_master_enqueue_xfer(struct dw_i3c_master
*master
,
430 struct dw_i3c_xfer
*xfer
)
434 init_completion(&xfer
->comp
);
435 spin_lock_irqsave(&master
->xferqueue
.lock
, flags
);
436 if (master
->xferqueue
.cur
) {
437 list_add_tail(&xfer
->node
, &master
->xferqueue
.list
);
439 master
->xferqueue
.cur
= xfer
;
440 dw_i3c_master_start_xfer_locked(master
);
442 spin_unlock_irqrestore(&master
->xferqueue
.lock
, flags
);
445 static void dw_i3c_master_dequeue_xfer_locked(struct dw_i3c_master
*master
,
446 struct dw_i3c_xfer
*xfer
)
448 if (master
->xferqueue
.cur
== xfer
) {
451 master
->xferqueue
.cur
= NULL
;
453 writel(RESET_CTRL_RX_FIFO
| RESET_CTRL_TX_FIFO
|
454 RESET_CTRL_RESP_QUEUE
| RESET_CTRL_CMD_QUEUE
,
455 master
->regs
+ RESET_CTRL
);
457 readl_poll_timeout_atomic(master
->regs
+ RESET_CTRL
, status
,
458 !status
, 10, 1000000);
460 list_del_init(&xfer
->node
);
464 static void dw_i3c_master_dequeue_xfer(struct dw_i3c_master
*master
,
465 struct dw_i3c_xfer
*xfer
)
469 spin_lock_irqsave(&master
->xferqueue
.lock
, flags
);
470 dw_i3c_master_dequeue_xfer_locked(master
, xfer
);
471 spin_unlock_irqrestore(&master
->xferqueue
.lock
, flags
);
474 static void dw_i3c_master_end_xfer_locked(struct dw_i3c_master
*master
, u32 isr
)
476 struct dw_i3c_xfer
*xfer
= master
->xferqueue
.cur
;
483 nresp
= readl(master
->regs
+ QUEUE_STATUS_LEVEL
);
484 nresp
= QUEUE_STATUS_LEVEL_RESP(nresp
);
486 for (i
= 0; i
< nresp
; i
++) {
487 struct dw_i3c_cmd
*cmd
;
490 resp
= readl(master
->regs
+ RESPONSE_QUEUE_PORT
);
492 cmd
= &xfer
->cmds
[RESPONSE_PORT_TID(resp
)];
493 cmd
->rx_len
= RESPONSE_PORT_DATA_LEN(resp
);
494 cmd
->error
= RESPONSE_PORT_ERR_STATUS(resp
);
495 if (cmd
->rx_len
&& !cmd
->error
)
496 dw_i3c_master_read_rx_fifo(master
, cmd
->rx_buf
,
500 for (i
= 0; i
< nresp
; i
++) {
501 switch (xfer
->cmds
[i
].error
) {
502 case RESPONSE_NO_ERROR
:
504 case RESPONSE_ERROR_PARITY
:
505 case RESPONSE_ERROR_IBA_NACK
:
506 case RESPONSE_ERROR_TRANSF_ABORT
:
507 case RESPONSE_ERROR_CRC
:
508 case RESPONSE_ERROR_FRAME
:
511 case RESPONSE_ERROR_OVER_UNDER_FLOW
:
514 case RESPONSE_ERROR_I2C_W_NACK_ERR
:
515 case RESPONSE_ERROR_ADDRESS_NACK
:
523 complete(&xfer
->comp
);
526 dw_i3c_master_dequeue_xfer_locked(master
, xfer
);
527 writel(readl(master
->regs
+ DEVICE_CTRL
) | DEV_CTRL_RESUME
,
528 master
->regs
+ DEVICE_CTRL
);
531 xfer
= list_first_entry_or_null(&master
->xferqueue
.list
,
535 list_del_init(&xfer
->node
);
537 master
->xferqueue
.cur
= xfer
;
538 dw_i3c_master_start_xfer_locked(master
);
541 static void dw_i3c_master_set_intr_regs(struct dw_i3c_master
*master
)
545 thld_ctrl
= readl(master
->regs
+ QUEUE_THLD_CTRL
);
546 thld_ctrl
&= ~(QUEUE_THLD_CTRL_RESP_BUF_MASK
|
547 QUEUE_THLD_CTRL_IBI_STAT_MASK
|
548 QUEUE_THLD_CTRL_IBI_DATA_MASK
);
549 thld_ctrl
|= QUEUE_THLD_CTRL_IBI_STAT(1) |
550 QUEUE_THLD_CTRL_IBI_DATA(31);
551 writel(thld_ctrl
, master
->regs
+ QUEUE_THLD_CTRL
);
553 thld_ctrl
= readl(master
->regs
+ DATA_BUFFER_THLD_CTRL
);
554 thld_ctrl
&= ~DATA_BUFFER_THLD_CTRL_RX_BUF
;
555 writel(thld_ctrl
, master
->regs
+ DATA_BUFFER_THLD_CTRL
);
557 writel(INTR_ALL
, master
->regs
+ INTR_STATUS
);
558 writel(INTR_MASTER_MASK
, master
->regs
+ INTR_STATUS_EN
);
559 writel(INTR_MASTER_MASK
, master
->regs
+ INTR_SIGNAL_EN
);
561 master
->sir_rej_mask
= IBI_REQ_REJECT_ALL
;
562 writel(master
->sir_rej_mask
, master
->regs
+ IBI_SIR_REQ_REJECT
);
564 writel(IBI_REQ_REJECT_ALL
, master
->regs
+ IBI_MR_REQ_REJECT
);
567 static int dw_i3c_clk_cfg(struct dw_i3c_master
*master
)
569 unsigned long core_rate
, core_period
;
573 core_rate
= clk_get_rate(master
->core_clk
);
577 core_period
= DIV_ROUND_UP(1000000000, core_rate
);
579 hcnt
= DIV_ROUND_UP(I3C_BUS_THIGH_MAX_NS
, core_period
) - 1;
580 if (hcnt
< SCL_I3C_TIMING_CNT_MIN
)
581 hcnt
= SCL_I3C_TIMING_CNT_MIN
;
583 lcnt
= DIV_ROUND_UP(core_rate
, master
->base
.bus
.scl_rate
.i3c
) - hcnt
;
584 if (lcnt
< SCL_I3C_TIMING_CNT_MIN
)
585 lcnt
= SCL_I3C_TIMING_CNT_MIN
;
587 scl_timing
= SCL_I3C_TIMING_HCNT(hcnt
) | SCL_I3C_TIMING_LCNT(lcnt
);
588 writel(scl_timing
, master
->regs
+ SCL_I3C_PP_TIMING
);
589 master
->i3c_pp_timing
= scl_timing
;
592 * In pure i3c mode, MST_FREE represents tCAS. In shared mode, this
593 * will be set up by dw_i2c_clk_cfg as tLOW.
595 if (master
->base
.bus
.mode
== I3C_BUS_MODE_PURE
) {
596 writel(BUS_I3C_MST_FREE(lcnt
), master
->regs
+ BUS_FREE_TIMING
);
597 master
->bus_free_timing
= BUS_I3C_MST_FREE(lcnt
);
601 DIV_ROUND_UP(I3C_BUS_TLOW_OD_MIN_NS
, core_period
), lcnt
);
602 scl_timing
= SCL_I3C_TIMING_HCNT(hcnt
) | SCL_I3C_TIMING_LCNT(lcnt
);
603 writel(scl_timing
, master
->regs
+ SCL_I3C_OD_TIMING
);
604 master
->i3c_od_timing
= scl_timing
;
606 lcnt
= DIV_ROUND_UP(core_rate
, I3C_BUS_SDR1_SCL_RATE
) - hcnt
;
607 scl_timing
= SCL_EXT_LCNT_1(lcnt
);
608 lcnt
= DIV_ROUND_UP(core_rate
, I3C_BUS_SDR2_SCL_RATE
) - hcnt
;
609 scl_timing
|= SCL_EXT_LCNT_2(lcnt
);
610 lcnt
= DIV_ROUND_UP(core_rate
, I3C_BUS_SDR3_SCL_RATE
) - hcnt
;
611 scl_timing
|= SCL_EXT_LCNT_3(lcnt
);
612 lcnt
= DIV_ROUND_UP(core_rate
, I3C_BUS_SDR4_SCL_RATE
) - hcnt
;
613 scl_timing
|= SCL_EXT_LCNT_4(lcnt
);
614 writel(scl_timing
, master
->regs
+ SCL_EXT_LCNT_TIMING
);
615 master
->ext_lcnt_timing
= scl_timing
;
620 static int dw_i2c_clk_cfg(struct dw_i3c_master
*master
)
622 unsigned long core_rate
, core_period
;
626 core_rate
= clk_get_rate(master
->core_clk
);
630 core_period
= DIV_ROUND_UP(1000000000, core_rate
);
632 lcnt
= DIV_ROUND_UP(I3C_BUS_I2C_FMP_TLOW_MIN_NS
, core_period
);
633 hcnt
= DIV_ROUND_UP(core_rate
, I3C_BUS_I2C_FM_PLUS_SCL_RATE
) - lcnt
;
634 scl_timing
= SCL_I2C_FMP_TIMING_HCNT(hcnt
) |
635 SCL_I2C_FMP_TIMING_LCNT(lcnt
);
636 writel(scl_timing
, master
->regs
+ SCL_I2C_FMP_TIMING
);
637 master
->i2c_fmp_timing
= scl_timing
;
639 lcnt
= DIV_ROUND_UP(I3C_BUS_I2C_FM_TLOW_MIN_NS
, core_period
);
640 hcnt
= DIV_ROUND_UP(core_rate
, I3C_BUS_I2C_FM_SCL_RATE
) - lcnt
;
641 scl_timing
= SCL_I2C_FM_TIMING_HCNT(hcnt
) |
642 SCL_I2C_FM_TIMING_LCNT(lcnt
);
643 writel(scl_timing
, master
->regs
+ SCL_I2C_FM_TIMING
);
644 master
->i2c_fm_timing
= scl_timing
;
646 writel(BUS_I3C_MST_FREE(lcnt
), master
->regs
+ BUS_FREE_TIMING
);
647 master
->bus_free_timing
= BUS_I3C_MST_FREE(lcnt
);
649 writel(readl(master
->regs
+ DEVICE_CTRL
) | DEV_CTRL_I2C_SLAVE_PRESENT
,
650 master
->regs
+ DEVICE_CTRL
);
651 master
->i2c_slv_prsnt
= true;
656 static int dw_i3c_master_bus_init(struct i3c_master_controller
*m
)
658 struct dw_i3c_master
*master
= to_dw_i3c_master(m
);
659 struct i3c_bus
*bus
= i3c_master_get_bus(m
);
660 struct i3c_device_info info
= { };
663 ret
= pm_runtime_resume_and_get(master
->dev
);
666 "<%s> cannot resume i3c bus master, err: %d\n",
671 ret
= master
->platform_ops
->init(master
);
676 case I3C_BUS_MODE_MIXED_FAST
:
677 case I3C_BUS_MODE_MIXED_LIMITED
:
678 ret
= dw_i2c_clk_cfg(master
);
682 case I3C_BUS_MODE_PURE
:
683 ret
= dw_i3c_clk_cfg(master
);
692 ret
= i3c_master_get_free_addr(m
, 0);
696 writel(DEV_ADDR_DYNAMIC_ADDR_VALID
| DEV_ADDR_DYNAMIC(ret
),
697 master
->regs
+ DEVICE_ADDR
);
698 master
->dev_addr
= ret
;
699 memset(&info
, 0, sizeof(info
));
702 ret
= i3c_master_set_info(&master
->base
, &info
);
706 dw_i3c_master_set_intr_regs(master
);
707 dw_i3c_master_enable(master
);
710 pm_runtime_mark_last_busy(master
->dev
);
711 pm_runtime_put_autosuspend(master
->dev
);
715 static void dw_i3c_master_bus_cleanup(struct i3c_master_controller
*m
)
717 struct dw_i3c_master
*master
= to_dw_i3c_master(m
);
719 dw_i3c_master_disable(master
);
722 static int dw_i3c_ccc_set(struct dw_i3c_master
*master
,
723 struct i3c_ccc_cmd
*ccc
)
725 struct dw_i3c_xfer
*xfer
;
726 struct dw_i3c_cmd
*cmd
;
729 if (ccc
->id
& I3C_CCC_DIRECT
) {
730 pos
= dw_i3c_master_get_addr_pos(master
, ccc
->dests
[0].addr
);
735 xfer
= dw_i3c_master_alloc_xfer(master
, 1);
740 cmd
->tx_buf
= ccc
->dests
[0].payload
.data
;
741 cmd
->tx_len
= ccc
->dests
[0].payload
.len
;
743 cmd
->cmd_hi
= COMMAND_PORT_ARG_DATA_LEN(ccc
->dests
[0].payload
.len
) |
744 COMMAND_PORT_TRANSFER_ARG
;
746 cmd
->cmd_lo
= COMMAND_PORT_CP
|
747 COMMAND_PORT_DEV_INDEX(pos
) |
748 COMMAND_PORT_CMD(ccc
->id
) |
752 dw_i3c_master_enqueue_xfer(master
, xfer
);
753 if (!wait_for_completion_timeout(&xfer
->comp
, XFER_TIMEOUT
))
754 dw_i3c_master_dequeue_xfer(master
, xfer
);
757 if (xfer
->cmds
[0].error
== RESPONSE_ERROR_IBA_NACK
)
758 ccc
->err
= I3C_ERROR_M2
;
760 dw_i3c_master_free_xfer(xfer
);
765 static int dw_i3c_ccc_get(struct dw_i3c_master
*master
, struct i3c_ccc_cmd
*ccc
)
767 struct dw_i3c_xfer
*xfer
;
768 struct dw_i3c_cmd
*cmd
;
771 pos
= dw_i3c_master_get_addr_pos(master
, ccc
->dests
[0].addr
);
775 xfer
= dw_i3c_master_alloc_xfer(master
, 1);
780 cmd
->rx_buf
= ccc
->dests
[0].payload
.data
;
781 cmd
->rx_len
= ccc
->dests
[0].payload
.len
;
783 cmd
->cmd_hi
= COMMAND_PORT_ARG_DATA_LEN(ccc
->dests
[0].payload
.len
) |
784 COMMAND_PORT_TRANSFER_ARG
;
786 cmd
->cmd_lo
= COMMAND_PORT_READ_TRANSFER
|
788 COMMAND_PORT_DEV_INDEX(pos
) |
789 COMMAND_PORT_CMD(ccc
->id
) |
793 dw_i3c_master_enqueue_xfer(master
, xfer
);
794 if (!wait_for_completion_timeout(&xfer
->comp
, XFER_TIMEOUT
))
795 dw_i3c_master_dequeue_xfer(master
, xfer
);
798 if (xfer
->cmds
[0].error
== RESPONSE_ERROR_IBA_NACK
)
799 ccc
->err
= I3C_ERROR_M2
;
800 dw_i3c_master_free_xfer(xfer
);
805 static void amd_configure_od_pp_quirk(struct dw_i3c_master
*master
)
807 master
->i3c_od_timing
= AMD_I3C_OD_TIMING
;
808 master
->i3c_pp_timing
= AMD_I3C_PP_TIMING
;
811 static int dw_i3c_master_send_ccc_cmd(struct i3c_master_controller
*m
,
812 struct i3c_ccc_cmd
*ccc
)
814 struct dw_i3c_master
*master
= to_dw_i3c_master(m
);
817 if (ccc
->id
== I3C_CCC_ENTDAA
)
820 /* AMD platform specific OD and PP timings */
821 if (master
->quirks
& AMD_I3C_OD_PP_TIMING
) {
822 amd_configure_od_pp_quirk(master
);
823 writel(master
->i3c_pp_timing
, master
->regs
+ SCL_I3C_PP_TIMING
);
824 writel(master
->i3c_od_timing
, master
->regs
+ SCL_I3C_OD_TIMING
);
827 ret
= pm_runtime_resume_and_get(master
->dev
);
830 "<%s> cannot resume i3c bus master, err: %d\n",
836 ret
= dw_i3c_ccc_get(master
, ccc
);
838 ret
= dw_i3c_ccc_set(master
, ccc
);
840 pm_runtime_mark_last_busy(master
->dev
);
841 pm_runtime_put_autosuspend(master
->dev
);
845 static int dw_i3c_master_daa(struct i3c_master_controller
*m
)
847 struct dw_i3c_master
*master
= to_dw_i3c_master(m
);
848 struct dw_i3c_xfer
*xfer
;
849 struct dw_i3c_cmd
*cmd
;
850 u32 olddevs
, newdevs
;
854 ret
= pm_runtime_resume_and_get(master
->dev
);
857 "<%s> cannot resume i3c bus master, err: %d\n",
862 olddevs
= ~(master
->free_pos
);
864 /* Prepare DAT before launching DAA. */
865 for (pos
= 0; pos
< master
->maxdevs
; pos
++) {
866 if (olddevs
& BIT(pos
))
869 ret
= i3c_master_get_free_addr(m
, last_addr
+ 1);
875 master
->devs
[pos
].addr
= ret
;
876 p
= even_parity(ret
);
880 writel(DEV_ADDR_TABLE_DYNAMIC_ADDR(ret
),
882 DEV_ADDR_TABLE_LOC(master
->datstartaddr
, pos
));
887 xfer
= dw_i3c_master_alloc_xfer(master
, 1);
893 pos
= dw_i3c_master_get_free_pos(master
);
895 dw_i3c_master_free_xfer(xfer
);
899 cmd
= &xfer
->cmds
[0];
901 cmd
->cmd_lo
= COMMAND_PORT_DEV_COUNT(master
->maxdevs
- pos
) |
902 COMMAND_PORT_DEV_INDEX(pos
) |
903 COMMAND_PORT_CMD(I3C_CCC_ENTDAA
) |
904 COMMAND_PORT_ADDR_ASSGN_CMD
|
908 dw_i3c_master_enqueue_xfer(master
, xfer
);
909 if (!wait_for_completion_timeout(&xfer
->comp
, XFER_TIMEOUT
))
910 dw_i3c_master_dequeue_xfer(master
, xfer
);
912 newdevs
= GENMASK(master
->maxdevs
- cmd
->rx_len
- 1, 0);
915 for (pos
= 0; pos
< master
->maxdevs
; pos
++) {
916 if (newdevs
& BIT(pos
))
917 i3c_master_add_i3c_dev_locked(m
, master
->devs
[pos
].addr
);
920 dw_i3c_master_free_xfer(xfer
);
923 pm_runtime_mark_last_busy(master
->dev
);
924 pm_runtime_put_autosuspend(master
->dev
);
928 static int dw_i3c_master_priv_xfers(struct i3c_dev_desc
*dev
,
929 struct i3c_priv_xfer
*i3c_xfers
,
932 struct dw_i3c_i2c_dev_data
*data
= i3c_dev_get_master_data(dev
);
933 struct i3c_master_controller
*m
= i3c_dev_get_master(dev
);
934 struct dw_i3c_master
*master
= to_dw_i3c_master(m
);
935 unsigned int nrxwords
= 0, ntxwords
= 0;
936 struct dw_i3c_xfer
*xfer
;
942 if (i3c_nxfers
> master
->caps
.cmdfifodepth
)
945 for (i
= 0; i
< i3c_nxfers
; i
++) {
946 if (i3c_xfers
[i
].rnw
)
947 nrxwords
+= DIV_ROUND_UP(i3c_xfers
[i
].len
, 4);
949 ntxwords
+= DIV_ROUND_UP(i3c_xfers
[i
].len
, 4);
952 if (ntxwords
> master
->caps
.datafifodepth
||
953 nrxwords
> master
->caps
.datafifodepth
)
956 xfer
= dw_i3c_master_alloc_xfer(master
, i3c_nxfers
);
960 ret
= pm_runtime_resume_and_get(master
->dev
);
963 "<%s> cannot resume i3c bus master, err: %d\n",
968 for (i
= 0; i
< i3c_nxfers
; i
++) {
969 struct dw_i3c_cmd
*cmd
= &xfer
->cmds
[i
];
971 cmd
->cmd_hi
= COMMAND_PORT_ARG_DATA_LEN(i3c_xfers
[i
].len
) |
972 COMMAND_PORT_TRANSFER_ARG
;
974 if (i3c_xfers
[i
].rnw
) {
975 cmd
->rx_buf
= i3c_xfers
[i
].data
.in
;
976 cmd
->rx_len
= i3c_xfers
[i
].len
;
977 cmd
->cmd_lo
= COMMAND_PORT_READ_TRANSFER
|
978 COMMAND_PORT_SPEED(dev
->info
.max_read_ds
);
981 cmd
->tx_buf
= i3c_xfers
[i
].data
.out
;
982 cmd
->tx_len
= i3c_xfers
[i
].len
;
984 COMMAND_PORT_SPEED(dev
->info
.max_write_ds
);
987 cmd
->cmd_lo
|= COMMAND_PORT_TID(i
) |
988 COMMAND_PORT_DEV_INDEX(data
->index
) |
991 if (i
== (i3c_nxfers
- 1))
992 cmd
->cmd_lo
|= COMMAND_PORT_TOC
;
995 dw_i3c_master_enqueue_xfer(master
, xfer
);
996 if (!wait_for_completion_timeout(&xfer
->comp
, XFER_TIMEOUT
))
997 dw_i3c_master_dequeue_xfer(master
, xfer
);
999 for (i
= 0; i
< i3c_nxfers
; i
++) {
1000 struct dw_i3c_cmd
*cmd
= &xfer
->cmds
[i
];
1002 if (i3c_xfers
[i
].rnw
)
1003 i3c_xfers
[i
].len
= cmd
->rx_len
;
1007 dw_i3c_master_free_xfer(xfer
);
1009 pm_runtime_mark_last_busy(master
->dev
);
1010 pm_runtime_put_autosuspend(master
->dev
);
1014 static int dw_i3c_master_reattach_i3c_dev(struct i3c_dev_desc
*dev
,
1017 struct dw_i3c_i2c_dev_data
*data
= i3c_dev_get_master_data(dev
);
1018 struct i3c_master_controller
*m
= i3c_dev_get_master(dev
);
1019 struct dw_i3c_master
*master
= to_dw_i3c_master(m
);
1022 pos
= dw_i3c_master_get_free_pos(master
);
1024 if (data
->index
> pos
&& pos
> 0) {
1027 DEV_ADDR_TABLE_LOC(master
->datstartaddr
, data
->index
));
1029 master
->devs
[data
->index
].addr
= 0;
1030 master
->free_pos
|= BIT(data
->index
);
1033 master
->devs
[pos
].addr
= dev
->info
.dyn_addr
;
1034 master
->free_pos
&= ~BIT(pos
);
1037 writel(DEV_ADDR_TABLE_DYNAMIC_ADDR(dev
->info
.dyn_addr
),
1039 DEV_ADDR_TABLE_LOC(master
->datstartaddr
, data
->index
));
1041 master
->devs
[data
->index
].addr
= dev
->info
.dyn_addr
;
1046 static int dw_i3c_master_attach_i3c_dev(struct i3c_dev_desc
*dev
)
1048 struct i3c_master_controller
*m
= i3c_dev_get_master(dev
);
1049 struct dw_i3c_master
*master
= to_dw_i3c_master(m
);
1050 struct dw_i3c_i2c_dev_data
*data
;
1053 pos
= dw_i3c_master_get_free_pos(master
);
1057 data
= kzalloc(sizeof(*data
), GFP_KERNEL
);
1062 master
->devs
[pos
].addr
= dev
->info
.dyn_addr
? : dev
->info
.static_addr
;
1063 master
->free_pos
&= ~BIT(pos
);
1064 i3c_dev_set_master_data(dev
, data
);
1066 writel(DEV_ADDR_TABLE_DYNAMIC_ADDR(master
->devs
[pos
].addr
),
1068 DEV_ADDR_TABLE_LOC(master
->datstartaddr
, data
->index
));
1073 static void dw_i3c_master_detach_i3c_dev(struct i3c_dev_desc
*dev
)
1075 struct dw_i3c_i2c_dev_data
*data
= i3c_dev_get_master_data(dev
);
1076 struct i3c_master_controller
*m
= i3c_dev_get_master(dev
);
1077 struct dw_i3c_master
*master
= to_dw_i3c_master(m
);
1081 DEV_ADDR_TABLE_LOC(master
->datstartaddr
, data
->index
));
1083 i3c_dev_set_master_data(dev
, NULL
);
1084 master
->devs
[data
->index
].addr
= 0;
1085 master
->free_pos
|= BIT(data
->index
);
1089 static int dw_i3c_master_i2c_xfers(struct i2c_dev_desc
*dev
,
1090 const struct i2c_msg
*i2c_xfers
,
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 unsigned int nrxwords
= 0, ntxwords
= 0;
1097 struct dw_i3c_xfer
*xfer
;
1103 if (i2c_nxfers
> master
->caps
.cmdfifodepth
)
1106 for (i
= 0; i
< i2c_nxfers
; i
++) {
1107 if (i2c_xfers
[i
].flags
& I2C_M_RD
)
1108 nrxwords
+= DIV_ROUND_UP(i2c_xfers
[i
].len
, 4);
1110 ntxwords
+= DIV_ROUND_UP(i2c_xfers
[i
].len
, 4);
1113 if (ntxwords
> master
->caps
.datafifodepth
||
1114 nrxwords
> master
->caps
.datafifodepth
)
1117 xfer
= dw_i3c_master_alloc_xfer(master
, i2c_nxfers
);
1121 ret
= pm_runtime_resume_and_get(master
->dev
);
1123 dev_err(master
->dev
,
1124 "<%s> cannot resume i3c bus master, err: %d\n",
1129 for (i
= 0; i
< i2c_nxfers
; i
++) {
1130 struct dw_i3c_cmd
*cmd
= &xfer
->cmds
[i
];
1132 cmd
->cmd_hi
= COMMAND_PORT_ARG_DATA_LEN(i2c_xfers
[i
].len
) |
1133 COMMAND_PORT_TRANSFER_ARG
;
1135 cmd
->cmd_lo
= COMMAND_PORT_TID(i
) |
1136 COMMAND_PORT_DEV_INDEX(data
->index
) |
1139 if (i2c_xfers
[i
].flags
& I2C_M_RD
) {
1140 cmd
->cmd_lo
|= COMMAND_PORT_READ_TRANSFER
;
1141 cmd
->rx_buf
= i2c_xfers
[i
].buf
;
1142 cmd
->rx_len
= i2c_xfers
[i
].len
;
1144 cmd
->tx_buf
= i2c_xfers
[i
].buf
;
1145 cmd
->tx_len
= i2c_xfers
[i
].len
;
1148 if (i
== (i2c_nxfers
- 1))
1149 cmd
->cmd_lo
|= COMMAND_PORT_TOC
;
1152 dw_i3c_master_enqueue_xfer(master
, xfer
);
1153 if (!wait_for_completion_timeout(&xfer
->comp
, XFER_TIMEOUT
))
1154 dw_i3c_master_dequeue_xfer(master
, xfer
);
1157 dw_i3c_master_free_xfer(xfer
);
1159 pm_runtime_mark_last_busy(master
->dev
);
1160 pm_runtime_put_autosuspend(master
->dev
);
1164 static int dw_i3c_master_attach_i2c_dev(struct i2c_dev_desc
*dev
)
1166 struct i3c_master_controller
*m
= i2c_dev_get_master(dev
);
1167 struct dw_i3c_master
*master
= to_dw_i3c_master(m
);
1168 struct dw_i3c_i2c_dev_data
*data
;
1171 pos
= dw_i3c_master_get_free_pos(master
);
1175 data
= kzalloc(sizeof(*data
), GFP_KERNEL
);
1180 master
->devs
[pos
].addr
= dev
->addr
;
1181 master
->devs
[pos
].is_i2c_addr
= true;
1182 master
->free_pos
&= ~BIT(pos
);
1183 i2c_dev_set_master_data(dev
, data
);
1185 writel(DEV_ADDR_TABLE_LEGACY_I2C_DEV
|
1186 DEV_ADDR_TABLE_STATIC_ADDR(dev
->addr
),
1188 DEV_ADDR_TABLE_LOC(master
->datstartaddr
, data
->index
));
1193 static void dw_i3c_master_detach_i2c_dev(struct i2c_dev_desc
*dev
)
1195 struct dw_i3c_i2c_dev_data
*data
= i2c_dev_get_master_data(dev
);
1196 struct i3c_master_controller
*m
= i2c_dev_get_master(dev
);
1197 struct dw_i3c_master
*master
= to_dw_i3c_master(m
);
1201 DEV_ADDR_TABLE_LOC(master
->datstartaddr
, data
->index
));
1203 i2c_dev_set_master_data(dev
, NULL
);
1204 master
->devs
[data
->index
].addr
= 0;
1205 master
->free_pos
|= BIT(data
->index
);
1209 static int dw_i3c_master_request_ibi(struct i3c_dev_desc
*dev
,
1210 const struct i3c_ibi_setup
*req
)
1212 struct dw_i3c_i2c_dev_data
*data
= i3c_dev_get_master_data(dev
);
1213 struct i3c_master_controller
*m
= i3c_dev_get_master(dev
);
1214 struct dw_i3c_master
*master
= to_dw_i3c_master(m
);
1215 unsigned long flags
;
1217 data
->ibi_pool
= i3c_generic_ibi_alloc_pool(dev
, req
);
1218 if (IS_ERR(data
->ibi_pool
))
1219 return PTR_ERR(data
->ibi_pool
);
1221 spin_lock_irqsave(&master
->devs_lock
, flags
);
1222 master
->devs
[data
->index
].ibi_dev
= dev
;
1223 spin_unlock_irqrestore(&master
->devs_lock
, flags
);
1228 static void dw_i3c_master_free_ibi(struct i3c_dev_desc
*dev
)
1230 struct dw_i3c_i2c_dev_data
*data
= i3c_dev_get_master_data(dev
);
1231 struct i3c_master_controller
*m
= i3c_dev_get_master(dev
);
1232 struct dw_i3c_master
*master
= to_dw_i3c_master(m
);
1233 unsigned long flags
;
1235 spin_lock_irqsave(&master
->devs_lock
, flags
);
1236 master
->devs
[data
->index
].ibi_dev
= NULL
;
1237 spin_unlock_irqrestore(&master
->devs_lock
, flags
);
1239 i3c_generic_ibi_free_pool(data
->ibi_pool
);
1240 data
->ibi_pool
= NULL
;
1243 static void dw_i3c_master_enable_sir_signal(struct dw_i3c_master
*master
, bool enable
)
1247 reg
= readl(master
->regs
+ INTR_STATUS_EN
);
1248 reg
&= ~INTR_IBI_THLD_STAT
;
1250 reg
|= INTR_IBI_THLD_STAT
;
1251 writel(reg
, master
->regs
+ INTR_STATUS_EN
);
1253 reg
= readl(master
->regs
+ INTR_SIGNAL_EN
);
1254 reg
&= ~INTR_IBI_THLD_STAT
;
1256 reg
|= INTR_IBI_THLD_STAT
;
1257 writel(reg
, master
->regs
+ INTR_SIGNAL_EN
);
1260 static void dw_i3c_master_set_sir_enabled(struct dw_i3c_master
*master
,
1261 struct i3c_dev_desc
*dev
,
1262 u8 idx
, bool enable
)
1264 unsigned long flags
;
1268 dat_entry
= DEV_ADDR_TABLE_LOC(master
->datstartaddr
, idx
);
1270 spin_lock_irqsave(&master
->devs_lock
, flags
);
1271 reg
= readl(master
->regs
+ dat_entry
);
1273 reg
&= ~DEV_ADDR_TABLE_SIR_REJECT
;
1274 if (dev
->info
.bcr
& I3C_BCR_IBI_PAYLOAD
)
1275 reg
|= DEV_ADDR_TABLE_IBI_MDB
;
1277 reg
|= DEV_ADDR_TABLE_SIR_REJECT
;
1279 master
->platform_ops
->set_dat_ibi(master
, dev
, enable
, ®
);
1280 writel(reg
, master
->regs
+ dat_entry
);
1283 global
= (master
->sir_rej_mask
== IBI_REQ_REJECT_ALL
);
1284 master
->sir_rej_mask
&= ~BIT(idx
);
1286 bool hj_rejected
= !!(readl(master
->regs
+ DEVICE_CTRL
) & DEV_CTRL_HOT_JOIN_NACK
);
1288 master
->sir_rej_mask
|= BIT(idx
);
1289 global
= (master
->sir_rej_mask
== IBI_REQ_REJECT_ALL
) && hj_rejected
;
1291 writel(master
->sir_rej_mask
, master
->regs
+ IBI_SIR_REQ_REJECT
);
1294 dw_i3c_master_enable_sir_signal(master
, enable
);
1297 spin_unlock_irqrestore(&master
->devs_lock
, flags
);
1300 static int dw_i3c_master_enable_hotjoin(struct i3c_master_controller
*m
)
1302 struct dw_i3c_master
*master
= to_dw_i3c_master(m
);
1305 ret
= pm_runtime_resume_and_get(master
->dev
);
1307 dev_err(master
->dev
,
1308 "<%s> cannot resume i3c bus master, err: %d\n",
1313 dw_i3c_master_enable_sir_signal(master
, true);
1314 writel(readl(master
->regs
+ DEVICE_CTRL
) & ~DEV_CTRL_HOT_JOIN_NACK
,
1315 master
->regs
+ DEVICE_CTRL
);
1320 static int dw_i3c_master_disable_hotjoin(struct i3c_master_controller
*m
)
1322 struct dw_i3c_master
*master
= to_dw_i3c_master(m
);
1324 writel(readl(master
->regs
+ DEVICE_CTRL
) | DEV_CTRL_HOT_JOIN_NACK
,
1325 master
->regs
+ DEVICE_CTRL
);
1327 pm_runtime_mark_last_busy(master
->dev
);
1328 pm_runtime_put_autosuspend(master
->dev
);
1332 static int dw_i3c_master_enable_ibi(struct i3c_dev_desc
*dev
)
1334 struct dw_i3c_i2c_dev_data
*data
= i3c_dev_get_master_data(dev
);
1335 struct i3c_master_controller
*m
= i3c_dev_get_master(dev
);
1336 struct dw_i3c_master
*master
= to_dw_i3c_master(m
);
1339 rc
= pm_runtime_resume_and_get(master
->dev
);
1341 dev_err(master
->dev
,
1342 "<%s> cannot resume i3c bus master, err: %d\n",
1347 dw_i3c_master_set_sir_enabled(master
, dev
, data
->index
, true);
1349 rc
= i3c_master_enec_locked(m
, dev
->info
.dyn_addr
, I3C_CCC_EVENT_SIR
);
1352 dw_i3c_master_set_sir_enabled(master
, dev
, data
->index
, false);
1353 pm_runtime_mark_last_busy(master
->dev
);
1354 pm_runtime_put_autosuspend(master
->dev
);
1360 static int dw_i3c_master_disable_ibi(struct i3c_dev_desc
*dev
)
1362 struct dw_i3c_i2c_dev_data
*data
= i3c_dev_get_master_data(dev
);
1363 struct i3c_master_controller
*m
= i3c_dev_get_master(dev
);
1364 struct dw_i3c_master
*master
= to_dw_i3c_master(m
);
1367 rc
= i3c_master_disec_locked(m
, dev
->info
.dyn_addr
, I3C_CCC_EVENT_SIR
);
1371 dw_i3c_master_set_sir_enabled(master
, dev
, data
->index
, false);
1373 pm_runtime_mark_last_busy(master
->dev
);
1374 pm_runtime_put_autosuspend(master
->dev
);
1378 static void dw_i3c_master_recycle_ibi_slot(struct i3c_dev_desc
*dev
,
1379 struct i3c_ibi_slot
*slot
)
1381 struct dw_i3c_i2c_dev_data
*data
= i3c_dev_get_master_data(dev
);
1383 i3c_generic_ibi_recycle_slot(data
->ibi_pool
, slot
);
1386 static void dw_i3c_master_drain_ibi_queue(struct dw_i3c_master
*master
,
1391 for (i
= 0; i
< DIV_ROUND_UP(len
, 4); i
++)
1392 readl(master
->regs
+ IBI_QUEUE_STATUS
);
1395 static void dw_i3c_master_handle_ibi_sir(struct dw_i3c_master
*master
,
1398 struct dw_i3c_i2c_dev_data
*data
;
1399 struct i3c_ibi_slot
*slot
;
1400 struct i3c_dev_desc
*dev
;
1401 unsigned long flags
;
1405 addr
= IBI_QUEUE_IBI_ADDR(status
);
1406 len
= IBI_QUEUE_STATUS_DATA_LEN(status
);
1409 * We be tempted to check the error status in bit 30; however, due
1410 * to the PEC errata workaround on some platform implementations (see
1411 * ast2600_i3c_set_dat_ibi()), those will almost always have a PEC
1412 * error on IBI payload data, as well as losing the last byte of
1415 * If we implement error status checking on that bit, we may need
1416 * a new platform op to validate it.
1419 spin_lock_irqsave(&master
->devs_lock
, flags
);
1420 idx
= dw_i3c_master_get_addr_pos(master
, addr
);
1422 dev_dbg_ratelimited(&master
->base
.dev
,
1423 "IBI from unknown addr 0x%x\n", addr
);
1427 dev
= master
->devs
[idx
].ibi_dev
;
1428 if (!dev
|| !dev
->ibi
) {
1429 dev_dbg_ratelimited(&master
->base
.dev
,
1430 "IBI from non-requested dev idx %d\n", idx
);
1434 data
= i3c_dev_get_master_data(dev
);
1435 slot
= i3c_generic_ibi_get_free_slot(data
->ibi_pool
);
1437 dev_dbg_ratelimited(&master
->base
.dev
,
1438 "No IBI slots available\n");
1442 if (dev
->ibi
->max_payload_len
< len
) {
1443 dev_dbg_ratelimited(&master
->base
.dev
,
1444 "IBI payload len %d greater than max %d\n",
1445 len
, dev
->ibi
->max_payload_len
);
1450 dw_i3c_master_read_ibi_fifo(master
, slot
->data
, len
);
1453 i3c_master_queue_ibi(dev
, slot
);
1455 spin_unlock_irqrestore(&master
->devs_lock
, flags
);
1460 dw_i3c_master_drain_ibi_queue(master
, len
);
1462 spin_unlock_irqrestore(&master
->devs_lock
, flags
);
1465 /* "ibis": referring to In-Band Interrupts, and not
1466 * https://en.wikipedia.org/wiki/Australian_white_ibis. The latter should
1469 static void dw_i3c_master_irq_handle_ibis(struct dw_i3c_master
*master
)
1471 unsigned int i
, len
, n_ibis
;
1474 reg
= readl(master
->regs
+ QUEUE_STATUS_LEVEL
);
1475 n_ibis
= QUEUE_STATUS_IBI_STATUS_CNT(reg
);
1479 for (i
= 0; i
< n_ibis
; i
++) {
1480 reg
= readl(master
->regs
+ IBI_QUEUE_STATUS
);
1482 if (IBI_TYPE_SIRQ(reg
)) {
1483 dw_i3c_master_handle_ibi_sir(master
, reg
);
1484 } else if (IBI_TYPE_HJ(reg
)) {
1485 queue_work(master
->base
.wq
, &master
->hj_work
);
1487 len
= IBI_QUEUE_STATUS_DATA_LEN(reg
);
1488 dev_info(&master
->base
.dev
,
1489 "unsupported IBI type 0x%lx len %d\n",
1490 IBI_QUEUE_STATUS_IBI_ID(reg
), len
);
1491 dw_i3c_master_drain_ibi_queue(master
, len
);
1496 static irqreturn_t
dw_i3c_master_irq_handler(int irq
, void *dev_id
)
1498 struct dw_i3c_master
*master
= dev_id
;
1501 status
= readl(master
->regs
+ INTR_STATUS
);
1503 if (!(status
& readl(master
->regs
+ INTR_STATUS_EN
))) {
1504 writel(INTR_ALL
, master
->regs
+ INTR_STATUS
);
1508 spin_lock(&master
->xferqueue
.lock
);
1509 dw_i3c_master_end_xfer_locked(master
, status
);
1510 if (status
& INTR_TRANSFER_ERR_STAT
)
1511 writel(INTR_TRANSFER_ERR_STAT
, master
->regs
+ INTR_STATUS
);
1512 spin_unlock(&master
->xferqueue
.lock
);
1514 if (status
& INTR_IBI_THLD_STAT
)
1515 dw_i3c_master_irq_handle_ibis(master
);
1520 static const struct i3c_master_controller_ops dw_mipi_i3c_ops
= {
1521 .bus_init
= dw_i3c_master_bus_init
,
1522 .bus_cleanup
= dw_i3c_master_bus_cleanup
,
1523 .attach_i3c_dev
= dw_i3c_master_attach_i3c_dev
,
1524 .reattach_i3c_dev
= dw_i3c_master_reattach_i3c_dev
,
1525 .detach_i3c_dev
= dw_i3c_master_detach_i3c_dev
,
1526 .do_daa
= dw_i3c_master_daa
,
1527 .supports_ccc_cmd
= dw_i3c_master_supports_ccc_cmd
,
1528 .send_ccc_cmd
= dw_i3c_master_send_ccc_cmd
,
1529 .priv_xfers
= dw_i3c_master_priv_xfers
,
1530 .attach_i2c_dev
= dw_i3c_master_attach_i2c_dev
,
1531 .detach_i2c_dev
= dw_i3c_master_detach_i2c_dev
,
1532 .i2c_xfers
= dw_i3c_master_i2c_xfers
,
1533 .request_ibi
= dw_i3c_master_request_ibi
,
1534 .free_ibi
= dw_i3c_master_free_ibi
,
1535 .enable_ibi
= dw_i3c_master_enable_ibi
,
1536 .disable_ibi
= dw_i3c_master_disable_ibi
,
1537 .recycle_ibi_slot
= dw_i3c_master_recycle_ibi_slot
,
1538 .enable_hotjoin
= dw_i3c_master_enable_hotjoin
,
1539 .disable_hotjoin
= dw_i3c_master_disable_hotjoin
,
1542 /* default platform ops implementations */
1543 static int dw_i3c_platform_init_nop(struct dw_i3c_master
*i3c
)
1548 static void dw_i3c_platform_set_dat_ibi_nop(struct dw_i3c_master
*i3c
,
1549 struct i3c_dev_desc
*dev
,
1550 bool enable
, u32
*dat
)
1554 static const struct dw_i3c_platform_ops dw_i3c_platform_ops_default
= {
1555 .init
= dw_i3c_platform_init_nop
,
1556 .set_dat_ibi
= dw_i3c_platform_set_dat_ibi_nop
,
1559 static void dw_i3c_hj_work(struct work_struct
*work
)
1561 struct dw_i3c_master
*master
=
1562 container_of(work
, typeof(*master
), hj_work
);
1564 i3c_master_do_daa(&master
->base
);
1567 int dw_i3c_common_probe(struct dw_i3c_master
*master
,
1568 struct platform_device
*pdev
)
1572 if (!master
->platform_ops
)
1573 master
->platform_ops
= &dw_i3c_platform_ops_default
;
1575 master
->dev
= &pdev
->dev
;
1577 master
->regs
= devm_platform_ioremap_resource(pdev
, 0);
1578 if (IS_ERR(master
->regs
))
1579 return PTR_ERR(master
->regs
);
1581 master
->core_clk
= devm_clk_get_enabled(&pdev
->dev
, NULL
);
1582 if (IS_ERR(master
->core_clk
))
1583 return PTR_ERR(master
->core_clk
);
1585 master
->pclk
= devm_clk_get_optional_enabled(&pdev
->dev
, "pclk");
1586 if (IS_ERR(master
->pclk
))
1587 return PTR_ERR(master
->pclk
);
1589 master
->core_rst
= devm_reset_control_get_optional_exclusive(&pdev
->dev
,
1591 if (IS_ERR(master
->core_rst
))
1592 return PTR_ERR(master
->core_rst
);
1594 reset_control_deassert(master
->core_rst
);
1596 spin_lock_init(&master
->xferqueue
.lock
);
1597 INIT_LIST_HEAD(&master
->xferqueue
.list
);
1599 writel(INTR_ALL
, master
->regs
+ INTR_STATUS
);
1600 irq
= platform_get_irq(pdev
, 0);
1601 ret
= devm_request_irq(&pdev
->dev
, irq
,
1602 dw_i3c_master_irq_handler
, 0,
1603 dev_name(&pdev
->dev
), master
);
1605 goto err_assert_rst
;
1607 platform_set_drvdata(pdev
, master
);
1609 pm_runtime_set_autosuspend_delay(&pdev
->dev
, RPM_AUTOSUSPEND_TIMEOUT
);
1610 pm_runtime_use_autosuspend(&pdev
->dev
);
1611 pm_runtime_set_active(&pdev
->dev
);
1612 pm_runtime_enable(&pdev
->dev
);
1614 /* Information regarding the FIFOs/QUEUEs depth */
1615 ret
= readl(master
->regs
+ QUEUE_STATUS_LEVEL
);
1616 master
->caps
.cmdfifodepth
= QUEUE_STATUS_LEVEL_CMD(ret
);
1618 ret
= readl(master
->regs
+ DATA_BUFFER_STATUS_LEVEL
);
1619 master
->caps
.datafifodepth
= DATA_BUFFER_STATUS_LEVEL_TX(ret
);
1621 ret
= readl(master
->regs
+ DEVICE_ADDR_TABLE_POINTER
);
1622 master
->datstartaddr
= ret
;
1623 master
->maxdevs
= ret
>> 16;
1624 master
->free_pos
= GENMASK(master
->maxdevs
- 1, 0);
1626 master
->quirks
= (unsigned long)device_get_match_data(&pdev
->dev
);
1628 INIT_WORK(&master
->hj_work
, dw_i3c_hj_work
);
1629 ret
= i3c_master_register(&master
->base
, &pdev
->dev
,
1630 &dw_mipi_i3c_ops
, false);
1632 goto err_disable_pm
;
1637 pm_runtime_disable(&pdev
->dev
);
1638 pm_runtime_set_suspended(&pdev
->dev
);
1639 pm_runtime_dont_use_autosuspend(&pdev
->dev
);
1642 reset_control_assert(master
->core_rst
);
1646 EXPORT_SYMBOL_GPL(dw_i3c_common_probe
);
1648 void dw_i3c_common_remove(struct dw_i3c_master
*master
)
1650 i3c_master_unregister(&master
->base
);
1652 pm_runtime_disable(master
->dev
);
1653 pm_runtime_set_suspended(master
->dev
);
1654 pm_runtime_dont_use_autosuspend(master
->dev
);
1656 EXPORT_SYMBOL_GPL(dw_i3c_common_remove
);
1658 /* base platform implementation */
1660 static int dw_i3c_probe(struct platform_device
*pdev
)
1662 struct dw_i3c_master
*master
;
1664 master
= devm_kzalloc(&pdev
->dev
, sizeof(*master
), GFP_KERNEL
);
1668 return dw_i3c_common_probe(master
, pdev
);
1671 static void dw_i3c_remove(struct platform_device
*pdev
)
1673 struct dw_i3c_master
*master
= platform_get_drvdata(pdev
);
1675 dw_i3c_common_remove(master
);
1678 static void dw_i3c_master_restore_addrs(struct dw_i3c_master
*master
)
1682 writel(DEV_ADDR_DYNAMIC_ADDR_VALID
| DEV_ADDR_DYNAMIC(master
->dev_addr
),
1683 master
->regs
+ DEVICE_ADDR
);
1685 for (pos
= 0; pos
< master
->maxdevs
; pos
++) {
1686 if (master
->free_pos
& BIT(pos
))
1689 if (master
->devs
[pos
].is_i2c_addr
)
1690 reg_val
= DEV_ADDR_TABLE_LEGACY_I2C_DEV
|
1691 DEV_ADDR_TABLE_STATIC_ADDR(master
->devs
[pos
].addr
);
1693 reg_val
= DEV_ADDR_TABLE_DYNAMIC_ADDR(master
->devs
[pos
].addr
);
1695 writel(reg_val
, master
->regs
+ DEV_ADDR_TABLE_LOC(master
->datstartaddr
, pos
));
1699 static void dw_i3c_master_restore_timing_regs(struct dw_i3c_master
*master
)
1701 /* AMD platform specific OD and PP timings */
1702 if (master
->quirks
& AMD_I3C_OD_PP_TIMING
)
1703 amd_configure_od_pp_quirk(master
);
1705 writel(master
->i3c_pp_timing
, master
->regs
+ SCL_I3C_PP_TIMING
);
1706 writel(master
->bus_free_timing
, master
->regs
+ BUS_FREE_TIMING
);
1707 writel(master
->i3c_od_timing
, master
->regs
+ SCL_I3C_OD_TIMING
);
1708 writel(master
->ext_lcnt_timing
, master
->regs
+ SCL_EXT_LCNT_TIMING
);
1710 if (master
->i2c_slv_prsnt
) {
1711 writel(master
->i2c_fmp_timing
, master
->regs
+ SCL_I2C_FMP_TIMING
);
1712 writel(master
->i2c_fm_timing
, master
->regs
+ SCL_I2C_FM_TIMING
);
1716 static int dw_i3c_master_enable_clks(struct dw_i3c_master
*master
)
1720 ret
= clk_prepare_enable(master
->core_clk
);
1724 ret
= clk_prepare_enable(master
->pclk
);
1726 clk_disable_unprepare(master
->core_clk
);
1733 static inline void dw_i3c_master_disable_clks(struct dw_i3c_master
*master
)
1735 clk_disable_unprepare(master
->pclk
);
1736 clk_disable_unprepare(master
->core_clk
);
1739 static int __maybe_unused
dw_i3c_master_runtime_suspend(struct device
*dev
)
1741 struct dw_i3c_master
*master
= dev_get_drvdata(dev
);
1743 dw_i3c_master_disable(master
);
1745 reset_control_assert(master
->core_rst
);
1746 dw_i3c_master_disable_clks(master
);
1747 pinctrl_pm_select_sleep_state(dev
);
1751 static int __maybe_unused
dw_i3c_master_runtime_resume(struct device
*dev
)
1753 struct dw_i3c_master
*master
= dev_get_drvdata(dev
);
1755 pinctrl_pm_select_default_state(dev
);
1756 dw_i3c_master_enable_clks(master
);
1757 reset_control_deassert(master
->core_rst
);
1759 dw_i3c_master_set_intr_regs(master
);
1760 dw_i3c_master_restore_timing_regs(master
);
1761 dw_i3c_master_restore_addrs(master
);
1763 dw_i3c_master_enable(master
);
1767 static const struct dev_pm_ops dw_i3c_pm_ops
= {
1768 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend
, pm_runtime_force_resume
)
1769 SET_RUNTIME_PM_OPS(dw_i3c_master_runtime_suspend
, dw_i3c_master_runtime_resume
, NULL
)
1772 static const struct of_device_id dw_i3c_master_of_match
[] = {
1773 { .compatible
= "snps,dw-i3c-master-1.00a", },
1776 MODULE_DEVICE_TABLE(of
, dw_i3c_master_of_match
);
1778 static const struct acpi_device_id amd_i3c_device_match
[] = {
1779 { "AMDI0015", AMD_I3C_OD_PP_TIMING
},
1782 MODULE_DEVICE_TABLE(acpi
, amd_i3c_device_match
);
1784 static struct platform_driver dw_i3c_driver
= {
1785 .probe
= dw_i3c_probe
,
1786 .remove
= dw_i3c_remove
,
1788 .name
= "dw-i3c-master",
1789 .of_match_table
= dw_i3c_master_of_match
,
1790 .acpi_match_table
= amd_i3c_device_match
,
1791 .pm
= &dw_i3c_pm_ops
,
1794 module_platform_driver(dw_i3c_driver
);
1796 MODULE_AUTHOR("Vitor Soares <vitor.soares@synopsys.com>");
1797 MODULE_DESCRIPTION("DesignWare MIPI I3C driver");
1798 MODULE_LICENSE("GPL v2");