1 /*
2  * Copyright (c) 2022 Meta Platforms, Inc. and its affiliates.
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <errno.h>
8 
9 #include <zephyr/drivers/i3c.h>
10 #include <zephyr/init.h>
11 #include <zephyr/kernel.h>
12 #include <zephyr/logging/log.h>
13 #include <zephyr/sys/byteorder.h>
14 #include <zephyr/sys/sys_io.h>
15 #include <zephyr/sys/util.h>
16 
17 #define DEV_ID            0x0
18 #define DEV_ID_I3C_MASTER 0x5034
19 
20 #define CONF_STATUS0                 0x4
21 #define CONF_STATUS0_CMDR_DEPTH(x)   (4 << (((x)&GENMASK(31, 29)) >> 29))
22 #define CONF_STATUS0_ECC_CHK         BIT(28)
23 #define CONF_STATUS0_INTEG_CHK       BIT(27)
24 #define CONF_STATUS0_CSR_DAP_CHK     BIT(26)
25 #define CONF_STATUS0_TRANS_TOUT_CHK  BIT(25)
26 #define CONF_STATUS0_PROT_FAULTS_CHK BIT(24)
27 #define CONF_STATUS0_GPO_NUM(x)      (((x)&GENMASK(23, 16)) >> 16)
28 #define CONF_STATUS0_GPI_NUM(x)      (((x)&GENMASK(15, 8)) >> 8)
29 #define CONF_STATUS0_IBIR_DEPTH(x)   (4 << (((x)&GENMASK(7, 6)) >> 7))
30 #define CONF_STATUS0_SUPPORTS_DDR    BIT(5)
31 #define CONF_STATUS0_SEC_MASTER      BIT(4)
32 #define CONF_STATUS0_DEVS_NUM(x)     ((x)&GENMASK(3, 0))
33 
34 #define CONF_STATUS1                    0x8
35 #define CONF_STATUS1_IBI_HW_RES(x)      ((((x)&GENMASK(31, 28)) >> 28) + 1)
36 #define CONF_STATUS1_CMD_DEPTH(x)       (4 << (((x)&GENMASK(27, 26)) >> 26))
37 #define CONF_STATUS1_SLVDDR_RX_DEPTH(x) (8 << (((x)&GENMASK(25, 21)) >> 21))
38 #define CONF_STATUS1_SLVDDR_TX_DEPTH(x) (8 << (((x)&GENMASK(20, 16)) >> 16))
39 #define CONF_STATUS1_IBI_DEPTH(x)       (2 << (((x)&GENMASK(12, 10)) >> 10))
40 #define CONF_STATUS1_RX_DEPTH(x)        (8 << (((x)&GENMASK(9, 5)) >> 5))
41 #define CONF_STATUS1_TX_DEPTH(x)        (8 << ((x)&GENMASK(4, 0)))
42 
43 #define REV_ID               0xc
44 #define REV_ID_VID(id)       (((id)&GENMASK(31, 20)) >> 20)
45 #define REV_ID_PID(id)       (((id)&GENMASK(19, 8)) >> 8)
46 #define REV_ID_REV(id)       ((id)&GENMASK(7, 0))
47 #define REV_ID_VERSION(m, n) ((m << 5) | (n))
48 #define REV_ID_REV_MAJOR(id) (((id)&GENMASK(7, 5)) >> 5)
49 #define REV_ID_REV_MINOR(id) ((id)&GENMASK(4, 0))
50 
51 #define CTRL                     0x10
52 #define CTRL_DEV_EN              BIT(31)
53 #define CTRL_HALT_EN             BIT(30)
54 #define CTRL_MCS                 BIT(29)
55 #define CTRL_MCS_EN              BIT(28)
56 #define CTRL_I3C_11_SUPP         BIT(26)
57 #define CTRL_THD_DELAY(x)        (((x) << 24) & GENMASK(25, 24))
58 #define CTRL_HJ_DISEC            BIT(8)
59 #define CTRL_MST_ACK             BIT(7)
60 #define CTRL_HJ_ACK              BIT(6)
61 #define CTRL_HJ_INIT             BIT(5)
62 #define CTRL_MST_INIT            BIT(4)
63 #define CTRL_AHDR_OPT            BIT(3)
64 #define CTRL_PURE_BUS_MODE       0
65 #define CTRL_MIXED_FAST_BUS_MODE 2
66 #define CTRL_MIXED_SLOW_BUS_MODE 3
67 #define CTRL_BUS_MODE_MASK       GENMASK(1, 0)
68 #define THD_DELAY_MAX            3
69 
70 #define PRESCL_CTRL0        0x14
71 #define PRESCL_CTRL0_I2C(x) ((x) << 16)
72 #define PRESCL_CTRL0_I3C(x) (x)
73 #define PRESCL_CTRL0_MAX    GENMASK(9, 0)
74 
75 #define PRESCL_CTRL1             0x18
76 #define PRESCL_CTRL1_PP_LOW_MASK GENMASK(15, 8)
77 #define PRESCL_CTRL1_PP_LOW(x)   ((x) << 8)
78 #define PRESCL_CTRL1_OD_LOW_MASK GENMASK(7, 0)
79 #define PRESCL_CTRL1_OD_LOW(x)   (x)
80 
81 #define MST_IER          0x20
82 #define MST_IDR          0x24
83 #define MST_IMR          0x28
84 #define MST_ICR          0x2c
85 #define MST_ISR          0x30
86 #define MST_INT_HALTED   BIT(18)
87 #define MST_INT_MR_DONE  BIT(17)
88 #define MST_INT_IMM_COMP BIT(16)
89 #define MST_INT_TX_THR   BIT(15)
90 #define MST_INT_TX_OVF   BIT(14)
91 #define MST_INT_IBID_THR BIT(12)
92 #define MST_INT_IBID_UNF BIT(11)
93 #define MST_INT_IBIR_THR BIT(10)
94 #define MST_INT_IBIR_UNF BIT(9)
95 #define MST_INT_IBIR_OVF BIT(8)
96 #define MST_INT_RX_THR   BIT(7)
97 #define MST_INT_RX_UNF   BIT(6)
98 #define MST_INT_CMDD_EMP BIT(5)
99 #define MST_INT_CMDD_THR BIT(4)
100 #define MST_INT_CMDD_OVF BIT(3)
101 #define MST_INT_CMDR_THR BIT(2)
102 #define MST_INT_CMDR_UNF BIT(1)
103 #define MST_INT_CMDR_OVF BIT(0)
104 #define MST_INT_MASK     GENMASK(18, 0)
105 
106 #define MST_STATUS0             0x34
107 #define MST_STATUS0_IDLE        BIT(18)
108 #define MST_STATUS0_HALTED      BIT(17)
109 #define MST_STATUS0_MASTER_MODE BIT(16)
110 #define MST_STATUS0_TX_FULL     BIT(13)
111 #define MST_STATUS0_IBID_FULL   BIT(12)
112 #define MST_STATUS0_IBIR_FULL   BIT(11)
113 #define MST_STATUS0_RX_FULL     BIT(10)
114 #define MST_STATUS0_CMDD_FULL   BIT(9)
115 #define MST_STATUS0_CMDR_FULL   BIT(8)
116 #define MST_STATUS0_TX_EMP      BIT(5)
117 #define MST_STATUS0_IBID_EMP    BIT(4)
118 #define MST_STATUS0_IBIR_EMP    BIT(3)
119 #define MST_STATUS0_RX_EMP      BIT(2)
120 #define MST_STATUS0_CMDD_EMP    BIT(1)
121 #define MST_STATUS0_CMDR_EMP    BIT(0)
122 
123 #define CMDR                    0x38
124 #define CMDR_NO_ERROR           0
125 #define CMDR_DDR_PREAMBLE_ERROR 1
126 #define CMDR_DDR_PARITY_ERROR   2
127 #define CMDR_DDR_RX_FIFO_OVF    3
128 #define CMDR_DDR_TX_FIFO_UNF    4
129 #define CMDR_M0_ERROR           5
130 #define CMDR_M1_ERROR           6
131 #define CMDR_M2_ERROR           7
132 #define CMDR_MST_ABORT          8
133 #define CMDR_NACK_RESP          9
134 #define CMDR_INVALID_DA         10
135 #define CMDR_DDR_DROPPED        11
136 #define CMDR_ERROR(x)           (((x)&GENMASK(27, 24)) >> 24)
137 #define CMDR_XFER_BYTES(x)      (((x)&GENMASK(19, 8)) >> 8)
138 #define CMDR_CMDID_HJACK_DISEC  0xfe
139 #define CMDR_CMDID_HJACK_ENTDAA 0xff
140 #define CMDR_CMDID(x)           ((x)&GENMASK(7, 0))
141 
142 #define IBIR               0x3c
143 #define IBIR_ACKED         BIT(12)
144 #define IBIR_SLVID(x)      (((x)&GENMASK(11, 8)) >> 8)
145 #define IBIR_SLVID_INV     0xF
146 #define IBIR_ERROR         BIT(7)
147 #define IBIR_XFER_BYTES(x) (((x)&GENMASK(6, 2)) >> 2)
148 #define IBIR_TYPE_IBI      0
149 #define IBIR_TYPE_HJ       1
150 #define IBIR_TYPE_MR       2
151 #define IBIR_TYPE(x)       ((x)&GENMASK(1, 0))
152 
153 #define SLV_IER             0x40
154 #define SLV_IDR             0x44
155 #define SLV_IMR             0x48
156 #define SLV_ICR             0x4c
157 #define SLV_ISR             0x50
158 #define SLV_INT_DEFSLVS     BIT(21)
159 #define SLV_INT_TM          BIT(20)
160 #define SLV_INT_ERROR       BIT(19)
161 #define SLV_INT_EVENT_UP    BIT(18)
162 #define SLV_INT_HJ_DONE     BIT(17)
163 #define SLV_INT_MR_DONE     BIT(16)
164 #define SLV_INT_DA_UPD      BIT(15)
165 #define SLV_INT_SDR_FAIL    BIT(14)
166 #define SLV_INT_DDR_FAIL    BIT(13)
167 #define SLV_INT_M_RD_ABORT  BIT(12)
168 #define SLV_INT_DDR_RX_THR  BIT(11)
169 #define SLV_INT_DDR_TX_THR  BIT(10)
170 #define SLV_INT_SDR_RX_THR  BIT(9)
171 #define SLV_INT_SDR_TX_THR  BIT(8)
172 #define SLV_INT_DDR_RX_UNF  BIT(7)
173 #define SLV_INT_DDR_TX_OVF  BIT(6)
174 #define SLV_INT_SDR_RX_UNF  BIT(5)
175 #define SLV_INT_SDR_TX_OVF  BIT(4)
176 #define SLV_INT_DDR_RD_COMP BIT(3)
177 #define SLV_INT_DDR_WR_COMP BIT(2)
178 #define SLV_INT_SDR_RD_COMP BIT(1)
179 #define SLV_INT_SDR_WR_COMP BIT(0)
180 #define SLV_INT_MASK        GENMASK(20, 0)
181 
182 #define SLV_STATUS0               0x54
183 #define SLV_STATUS0_REG_ADDR(s)   (((s)&GENMASK(23, 16)) >> 16)
184 #define SLV_STATUS0_XFRD_BYTES(s) ((s)&GENMASK(15, 0))
185 
186 #define SLV_STATUS1              0x58
187 #define SLV_STATUS1_AS(s)        (((s)&GENMASK(21, 20)) >> 20)
188 #define SLV_STATUS1_VEN_TM       BIT(19)
189 #define SLV_STATUS1_HJ_DIS       BIT(18)
190 #define SLV_STATUS1_MR_DIS       BIT(17)
191 #define SLV_STATUS1_PROT_ERR     BIT(16)
192 #define SLV_STATUS1_DA(s)        (((s)&GENMASK(15, 9)) >> 9)
193 #define SLV_STATUS1_HAS_DA       BIT(8)
194 #define SLV_STATUS1_DDR_RX_FULL  BIT(7)
195 #define SLV_STATUS1_DDR_TX_FULL  BIT(6)
196 #define SLV_STATUS1_DDR_RX_EMPTY BIT(5)
197 #define SLV_STATUS1_DDR_TX_EMPTY BIT(4)
198 #define SLV_STATUS1_SDR_RX_FULL  BIT(3)
199 #define SLV_STATUS1_SDR_TX_FULL  BIT(2)
200 #define SLV_STATUS1_SDR_RX_EMPTY BIT(1)
201 #define SLV_STATUS1_SDR_TX_EMPTY BIT(0)
202 
203 #define CMD0_FIFO                   0x60
204 #define CMD0_FIFO_IS_DDR            BIT(31)
205 #define CMD0_FIFO_IS_CCC            BIT(30)
206 #define CMD0_FIFO_BCH               BIT(29)
207 #define XMIT_BURST_STATIC_SUBADDR   0
208 #define XMIT_SINGLE_INC_SUBADDR     1
209 #define XMIT_SINGLE_STATIC_SUBADDR  2
210 #define XMIT_BURST_WITHOUT_SUBADDR  3
211 #define CMD0_FIFO_PRIV_XMIT_MODE(m) ((m) << 27)
212 #define CMD0_FIFO_SBCA              BIT(26)
213 #define CMD0_FIFO_RSBC              BIT(25)
214 #define CMD0_FIFO_IS_10B            BIT(24)
215 #define CMD0_FIFO_PL_LEN(l)         ((l) << 12)
216 #define CMD0_FIFO_PL_LEN_MAX        4095
217 #define CMD0_FIFO_DEV_ADDR(a)       ((a) << 1)
218 #define CMD0_FIFO_RNW               BIT(0)
219 
220 #define CMD1_FIFO            0x64
221 #define CMD1_FIFO_CMDID(id)  ((id) << 24)
222 #define CMD1_FIFO_CSRADDR(a) (a)
223 #define CMD1_FIFO_CCC(id)    (id)
224 
225 #define TX_FIFO 0x68
226 
227 #define IMD_CMD0             0x70
228 #define IMD_CMD0_PL_LEN(l)   ((l) << 12)
229 #define IMD_CMD0_DEV_ADDR(a) ((a) << 1)
230 #define IMD_CMD0_RNW         BIT(0)
231 
232 #define IMD_CMD1         0x74
233 #define IMD_CMD1_CCC(id) (id)
234 
235 #define IMD_DATA        0x78
236 #define RX_FIFO         0x80
237 #define IBI_DATA_FIFO   0x84
238 #define SLV_DDR_TX_FIFO 0x88
239 #define SLV_DDR_RX_FIFO 0x8c
240 
241 #define CMD_IBI_THR_CTRL 0x90
242 #define IBIR_THR(t)      ((t) << 24)
243 #define CMDR_THR(t)      ((t) << 16)
244 #define CMDR_THR_MASK    (GENMASK(20, 16))
245 #define IBI_THR(t)       ((t) << 8)
246 #define CMD_THR(t)       (t)
247 
248 #define TX_RX_THR_CTRL 0x94
249 #define RX_THR(t)      ((t) << 16)
250 #define RX_THR_MASK    (GENMASK(31, 16))
251 #define TX_THR(t)      (t)
252 #define TX_THR_MASK    (GENMASK(15, 0))
253 
254 #define SLV_DDR_TX_RX_THR_CTRL 0x98
255 #define SLV_DDR_RX_THR(t)      ((t) << 16)
256 #define SLV_DDR_TX_THR(t)      (t)
257 
258 #define FLUSH_CTRL            0x9c
259 #define FLUSH_IBI_RESP        BIT(23)
260 #define FLUSH_CMD_RESP        BIT(22)
261 #define FLUSH_SLV_DDR_RX_FIFO BIT(22)
262 #define FLUSH_SLV_DDR_TX_FIFO BIT(21)
263 #define FLUSH_IMM_FIFO        BIT(20)
264 #define FLUSH_IBI_FIFO        BIT(19)
265 #define FLUSH_RX_FIFO         BIT(18)
266 #define FLUSH_TX_FIFO         BIT(17)
267 #define FLUSH_CMD_FIFO        BIT(16)
268 
269 #define TTO_PRESCL_CTRL0               0xb0
270 #define TTO_PRESCL_CTRL0_PRESCL_I2C(x) ((x) << 16)
271 #define TTO_PRESCL_CTRL0_PRESCL_I3C(x) (x)
272 
273 #define TTO_PRESCL_CTRL1           0xb4
274 #define TTO_PRESCL_CTRL1_DIVB(x)   ((x) << 16)
275 #define TTO_PRESCL_CTRL1_DIVA(x)   (x)
276 #define TTO_PRESCL_CTRL1_PP_LOW(x) ((x) << 8)
277 #define TTO_PRESCL_CTRL1_OD_LOW(x) (x)
278 
279 #define DEVS_CTRL                  0xb8
280 #define DEVS_CTRL_DEV_CLR_SHIFT    16
281 #define DEVS_CTRL_DEV_CLR_ALL      GENMASK(31, 16)
282 #define DEVS_CTRL_DEV_CLR(dev)     BIT(16 + (dev))
283 #define DEVS_CTRL_DEV_ACTIVE(dev)  BIT(dev)
284 #define DEVS_CTRL_DEVS_ACTIVE_MASK GENMASK(15, 0)
285 #define MAX_DEVS                   16
286 
287 #define DEV_ID_RR0(d)              (0xc0 + ((d)*0x10))
288 #define DEV_ID_RR0_LVR_EXT_ADDR    BIT(11)
289 #define DEV_ID_RR0_HDR_CAP         BIT(10)
290 #define DEV_ID_RR0_IS_I3C          BIT(9)
291 #define DEV_ID_RR0_DEV_ADDR_MASK   (GENMASK(6, 0) | GENMASK(15, 13))
292 #define DEV_ID_RR0_SET_DEV_ADDR(a) (((a)&GENMASK(6, 0)) | (((a)&GENMASK(9, 7)) << 6))
293 #define DEV_ID_RR0_GET_DEV_ADDR(x) ((((x) >> 1) & GENMASK(6, 0)) | (((x) >> 6) & GENMASK(9, 7)))
294 
295 #define DEV_ID_RR1(d)           (0xc4 + ((d)*0x10))
296 #define DEV_ID_RR1_PID_MSB(pid) (pid)
297 
298 #define DEV_ID_RR2(d)           (0xc8 + ((d)*0x10))
299 #define DEV_ID_RR2_PID_LSB(pid) ((pid) << 16)
300 #define DEV_ID_RR2_BCR(bcr)     ((bcr) << 8)
301 #define DEV_ID_RR2_DCR(dcr)     (dcr)
302 #define DEV_ID_RR2_LVR(lvr)     (lvr)
303 
304 #define SIR_MAP(x)               (0x180 + ((x)*4))
305 #define SIR_MAP_DEV_REG(d)       SIR_MAP((d) / 2)
306 #define SIR_MAP_DEV_SHIFT(d, fs) ((fs) + (((d) % 2) ? 16 : 0))
307 #define SIR_MAP_DEV_CONF_MASK(d) (GENMASK(15, 0) << (((d) % 2) ? 16 : 0))
308 #define SIR_MAP_DEV_CONF(d, c)   ((c) << (((d) % 2) ? 16 : 0))
309 #define DEV_ROLE_SLAVE           0
310 #define DEV_ROLE_MASTER          1
311 #define SIR_MAP_DEV_ROLE(role)   ((role) << 14)
312 #define SIR_MAP_DEV_SLOW         BIT(13)
313 #define SIR_MAP_DEV_PL(l)        ((l) << 8)
314 #define SIR_MAP_PL_MAX           GENMASK(4, 0)
315 #define SIR_MAP_DEV_DA(a)        ((a) << 1)
316 #define SIR_MAP_DEV_ACK          BIT(0)
317 
318 #define GPIR_WORD(x)     (0x200 + ((x)*4))
319 #define GPI_REG(val, id) (((val) >> (((id) % 4) * 8)) & GENMASK(7, 0))
320 
321 #define GPOR_WORD(x)     (0x220 + ((x)*4))
322 #define GPO_REG(val, id) (((val) >> (((id) % 4) * 8)) & GENMASK(7, 0))
323 
324 #define ASF_INT_STATUS        0x300
325 #define ASF_INT_RAW_STATUS    0x304
326 #define ASF_INT_MASK          0x308
327 #define ASF_INT_TEST          0x30c
328 #define ASF_INT_FATAL_SELECT  0x310
329 #define ASF_INTEGRITY_ERR     BIT(6)
330 #define ASF_PROTOCOL_ERR      BIT(5)
331 #define ASF_TRANS_TIMEOUT_ERR BIT(4)
332 #define ASF_CSR_ERR           BIT(3)
333 #define ASF_DAP_ERR           BIT(2)
334 #define ASF_SRAM_UNCORR_ERR   BIT(1)
335 #define ASF_SRAM_CORR_ERR     BIT(0)
336 
337 #define ASF_SRAM_CORR_FAULT_STATUS      0x320
338 #define ASF_SRAM_UNCORR_FAULT_STATUS    0x324
339 #define ASF_SRAM_CORR_FAULT_INSTANCE(x) ((x) >> 24)
340 #define ASF_SRAM_CORR_FAULT_ADDR(x)     ((x)&GENMASK(23, 0))
341 
342 #define ASF_SRAM_FAULT_STATS           0x328
343 #define ASF_SRAM_FAULT_UNCORR_STATS(x) ((x) >> 16)
344 #define ASF_SRAM_FAULT_CORR_STATS(x)   ((x)&GENMASK(15, 0))
345 
346 #define ASF_TRANS_TOUT_CTRL   0x330
347 #define ASF_TRANS_TOUT_EN     BIT(31)
348 #define ASF_TRANS_TOUT_VAL(x) (x)
349 
350 #define ASF_TRANS_TOUT_FAULT_MASK      0x334
351 #define ASF_TRANS_TOUT_FAULT_STATUS    0x338
352 #define ASF_TRANS_TOUT_FAULT_APB       BIT(3)
353 #define ASF_TRANS_TOUT_FAULT_SCL_LOW   BIT(2)
354 #define ASF_TRANS_TOUT_FAULT_SCL_HIGH  BIT(1)
355 #define ASF_TRANS_TOUT_FAULT_FSCL_HIGH BIT(0)
356 
357 #define ASF_PROTO_FAULT_MASK            0x340
358 #define ASF_PROTO_FAULT_STATUS          0x344
359 #define ASF_PROTO_FAULT_SLVSDR_RD_ABORT BIT(31)
360 #define ASF_PROTO_FAULT_SLVDDR_FAIL     BIT(30)
361 #define ASF_PROTO_FAULT_S(x)            BIT(16 + (x))
362 #define ASF_PROTO_FAULT_MSTSDR_RD_ABORT BIT(15)
363 #define ASF_PROTO_FAULT_MSTDDR_FAIL     BIT(14)
364 #define ASF_PROTO_FAULT_M(x)            BIT(x)
365 
366 /*******************************************************************************
367  * Local Constants Definition
368  ******************************************************************************/
369 
370 /* TODO: this needs to be configurable in the dts...somehow */
371 #define I3C_CONTROLLER_ADDR 0x08
372 
373 /* Maximum i3c devices that the IP can be built with */
374 #define I3C_MAX_DEVS                     11
375 #define I3C_MAX_MSGS                     10
376 #define I3C_SIR_DEFAULT_DA               0x7F
377 #define I3C_MAX_IDLE_CANCEL_WAIT_RETRIES 50
378 #define I3C_PRESCL_REG_SCALE             (4)
379 #define I2C_PRESCL_REG_SCALE             (5)
380 #define I3C_WAIT_FOR_IDLE_STATE_US       100
381 #define I3C_IDLE_TIMEOUT_CYC                                                                       \
382 	(I3C_WAIT_FOR_IDLE_STATE_US * (sys_clock_hw_cycles_per_sec() / USEC_PER_SEC))
383 
384 /* Target T_LOW period in open-drain mode. */
385 #define I3C_BUS_TLOW_OD_MIN_NS 200
386 
387 /* MIPI I3C v1.1.1 Spec defines tsco max as 12ns */
388 #define I3C_TSCO_DEFAULT_NS 10
389 
390 /* Interrupt thresholds. */
391 /* command response fifo threshold */
392 #define I3C_CMDR_THR 1
393 /* command tx fifo threshold - unused */
394 #define I3C_CMDD_THR 1
395 /* in-band-interrupt data fifo threshold - unused */
396 #define I3C_IBID_THR 1
397 /* in-band-interrupt response queue threshold */
398 #define I3C_IBIR_THR 1
399 /* tx data threshold - unused */
400 #define I3C_TX_THR   1
401 
402 #define LOG_MODULE_NAME I3C_CADENCE
403 LOG_MODULE_REGISTER(I3C_CADENCE, CONFIG_I3C_CADENCE_LOG_LEVEL);
404 
405 /*******************************************************************************
406  * Local Types Definition
407  ******************************************************************************/
408 
409 /** Describes peripheral HW configuration determined from CONFx registers. */
410 struct cdns_i3c_hw_config {
411 	/* The maxiumum command queue depth. */
412 	uint32_t cmd_mem_depth;
413 	/* The maxiumum command response queue depth. */
414 	uint32_t cmdr_mem_depth;
415 	/* The maximum RX FIFO depth. */
416 	uint32_t rx_mem_depth;
417 	/* The maximum TX FIFO depth. */
418 	uint32_t tx_mem_depth;
419 	/* The maximum IBIR FIFO depth. */
420 	uint32_t ibir_mem_depth;
421 };
422 
423 /* Cadence I3C/I2C Device Private Data */
424 struct cdns_i3c_i2c_dev_data {
425 	/* Device id within the retaining registers. This is set after bus initialization by the
426 	 * controller.
427 	 */
428 	uint8_t id;
429 };
430 
431 /* Single command/transfer */
432 struct cdns_i3c_cmd {
433 	uint32_t cmd0;
434 	uint32_t cmd1;
435 	uint32_t len;
436 	uint32_t *num_xfer;
437 	void *buf;
438 	uint32_t error;
439 };
440 
441 /* Transfer data */
442 struct cdns_i3c_xfer {
443 	struct k_sem complete;
444 	int ret;
445 	int num_cmds;
446 	struct cdns_i3c_cmd cmds[I3C_MAX_MSGS];
447 };
448 
449 /* Driver config */
450 struct cdns_i3c_config {
451 	struct i3c_driver_config common;
452 	/** base address of the controller */
453 	uintptr_t base;
454 	/** input frequency to the I3C Cadence */
455 	uint32_t input_frequency;
456 	/** Interrupt configuration function. */
457 	void (*irq_config_func)(const struct device *dev);
458 };
459 
460 /* Driver instance data */
461 struct cdns_i3c_data {
462 	struct i3c_driver_data common;
463 	struct cdns_i3c_hw_config hw_cfg;
464 	struct k_mutex bus_lock;
465 	struct cdns_i3c_i2c_dev_data cdns_i3c_i2c_priv_data[I3C_MAX_DEVS];
466 	struct cdns_i3c_xfer xfer;
467 	struct i3c_target_config *target_config;
468 	struct k_sem ibi_hj_complete;
469 	uint32_t free_rr_slots;
470 	uint8_t max_devs;
471 };
472 
473 /*******************************************************************************
474  * Global Variables Declaration
475  ******************************************************************************/
476 
477 /*******************************************************************************
478  * Local Functions Declaration
479  ******************************************************************************/
480 
481 /*******************************************************************************
482  * Private Functions Code
483  ******************************************************************************/
484 
485 /* Computes and sets parity */
486 /* Returns [7:1] 7-bit addr, [0] even/xor parity */
cdns_i3c_even_parity(uint8_t byte)487 static uint8_t cdns_i3c_even_parity(uint8_t byte)
488 {
489 	uint8_t parity = 0;
490 	uint8_t b = byte;
491 
492 	while (b) {
493 		parity = !parity;
494 		b = b & (b - 1);
495 	}
496 	b = (byte << 1) | !parity;
497 
498 	return b;
499 }
500 
501 /* Check if command response fifo is empty */
cdns_i3c_cmd_rsp_fifo_empty(const struct cdns_i3c_config * config)502 static inline bool cdns_i3c_cmd_rsp_fifo_empty(const struct cdns_i3c_config *config)
503 {
504 	uint32_t mst_st = sys_read32(config->base + MST_STATUS0);
505 
506 	return ((mst_st & MST_STATUS0_CMDR_EMP) ? true : false);
507 }
508 
509 /* Check if command fifo is empty */
cdns_i3c_cmd_fifo_empty(const struct cdns_i3c_config * config)510 static inline bool cdns_i3c_cmd_fifo_empty(const struct cdns_i3c_config *config)
511 {
512 	uint32_t mst_st = sys_read32(config->base + MST_STATUS0);
513 
514 	return ((mst_st & MST_STATUS0_CMDD_EMP) ? true : false);
515 }
516 
517 /* Check if command fifo is full */
cdns_i3c_cmd_fifo_full(const struct cdns_i3c_config * config)518 static inline bool cdns_i3c_cmd_fifo_full(const struct cdns_i3c_config *config)
519 {
520 	uint32_t mst_st = sys_read32(config->base + MST_STATUS0);
521 
522 	return ((mst_st & MST_STATUS0_CMDD_FULL) ? true : false);
523 }
524 
525 /* Check if ibi response fifo is empty */
cdns_i3c_ibi_rsp_fifo_empty(const struct cdns_i3c_config * config)526 static inline bool cdns_i3c_ibi_rsp_fifo_empty(const struct cdns_i3c_config *config)
527 {
528 	uint32_t mst_st = sys_read32(config->base + MST_STATUS0);
529 
530 	return ((mst_st & MST_STATUS0_IBIR_EMP) ? true : false);
531 }
532 
533 /* Check if tx fifo is full */
cdns_i3c_tx_fifo_full(const struct cdns_i3c_config * config)534 static inline bool cdns_i3c_tx_fifo_full(const struct cdns_i3c_config *config)
535 {
536 	uint32_t mst_st = sys_read32(config->base + MST_STATUS0);
537 
538 	return ((mst_st & MST_STATUS0_TX_FULL) ? true : false);
539 }
540 
541 /* Check if rx fifo is full */
cdns_i3c_rx_fifo_full(const struct cdns_i3c_config * config)542 static inline bool cdns_i3c_rx_fifo_full(const struct cdns_i3c_config *config)
543 {
544 	uint32_t mst_st = sys_read32(config->base + MST_STATUS0);
545 
546 	return ((mst_st & MST_STATUS0_RX_FULL) ? true : false);
547 }
548 
549 /* Check if rx fifo is empty */
cdns_i3c_rx_fifo_empty(const struct cdns_i3c_config * config)550 static inline bool cdns_i3c_rx_fifo_empty(const struct cdns_i3c_config *config)
551 {
552 	uint32_t mst_st = sys_read32(config->base + MST_STATUS0);
553 
554 	return ((mst_st & MST_STATUS0_RX_EMP) ? true : false);
555 }
556 
557 /* Check if ibi fifo is empty */
cdns_i3c_ibi_fifo_empty(const struct cdns_i3c_config * config)558 static inline bool cdns_i3c_ibi_fifo_empty(const struct cdns_i3c_config *config)
559 {
560 	uint32_t mst_st = sys_read32(config->base + MST_STATUS0);
561 
562 	return ((mst_st & MST_STATUS0_IBID_EMP) ? true : false);
563 }
564 
565 /* Interrupt handling */
cdns_i3c_interrupts_disable(const struct cdns_i3c_config * config)566 static inline void cdns_i3c_interrupts_disable(const struct cdns_i3c_config *config)
567 {
568 	sys_write32(MST_INT_MASK, config->base + MST_IDR);
569 }
570 
cdns_i3c_interrupts_clear(const struct cdns_i3c_config * config)571 static inline void cdns_i3c_interrupts_clear(const struct cdns_i3c_config *config)
572 {
573 	sys_write32(MST_INT_MASK, config->base + MST_ICR);
574 }
575 
576 /* FIFO mgmt */
cdns_i3c_write_tx_fifo(const struct cdns_i3c_config * config,const void * buf,uint32_t len)577 static void cdns_i3c_write_tx_fifo(const struct cdns_i3c_config *config, const void *buf,
578 				   uint32_t len)
579 {
580 	const uint32_t *ptr = buf;
581 	uint32_t remain, val;
582 
583 	for (remain = len; remain >= 4; remain -= 4) {
584 		val = *ptr++;
585 		sys_write32(val, config->base + TX_FIFO);
586 	}
587 
588 	if (remain > 0) {
589 		val = 0;
590 		memcpy(&val, ptr, remain);
591 		sys_write32(val, config->base + TX_FIFO);
592 	}
593 }
594 
cdns_i3c_read_rx_fifo(const struct cdns_i3c_config * config,void * buf,uint32_t len)595 static int cdns_i3c_read_rx_fifo(const struct cdns_i3c_config *config, void *buf, uint32_t len)
596 {
597 	uint32_t *ptr = buf;
598 	uint32_t remain, val;
599 
600 	for (remain = len; remain >= 4; remain -= 4) {
601 		if (cdns_i3c_rx_fifo_empty(config)) {
602 			return -EIO;
603 		}
604 		val = sys_le32_to_cpu(sys_read32(config->base + RX_FIFO));
605 		*ptr++ = val;
606 	}
607 
608 	if (remain > 0) {
609 		if (cdns_i3c_rx_fifo_empty(config)) {
610 			return -EIO;
611 		}
612 		val = sys_le32_to_cpu(sys_read32(config->base + RX_FIFO));
613 		memcpy(ptr, &val, remain);
614 	}
615 
616 	return 0;
617 }
618 
cdns_i3c_wait_for_idle(const struct device * dev)619 static inline int cdns_i3c_wait_for_idle(const struct device *dev)
620 {
621 	const struct cdns_i3c_config *config = dev->config;
622 	uint32_t start_time = k_cycle_get_32();
623 
624 	/**
625 	 * Spin waiting for device to go idle. It is unlikely that this will
626 	 * actually take any time unless if the last transaction came immediately
627 	 * after an error condition.
628 	 */
629 	while (!(sys_read32(config->base + MST_STATUS0) & MST_STATUS0_IDLE)) {
630 		if (k_cycle_get_32() - start_time > I3C_IDLE_TIMEOUT_CYC) {
631 			return -EAGAIN;
632 		}
633 	}
634 
635 	return 0;
636 }
637 
cdns_i3c_set_prescalers(const struct device * dev)638 static void cdns_i3c_set_prescalers(const struct device *dev)
639 {
640 	struct cdns_i3c_data *data = dev->data;
641 	const struct cdns_i3c_config *config = dev->config;
642 	struct i3c_config_controller *ctrl_config = &data->common.ctrl_config;
643 
644 	/* These formulas are from section 6.2.1 of the Cadence I3C Master User Guide. */
645 	uint32_t prescl_i3c = DIV_ROUND_UP(config->input_frequency,
646 					   (ctrl_config->scl.i3c * I3C_PRESCL_REG_SCALE)) -
647 			      1;
648 	uint32_t prescl_i2c = DIV_ROUND_UP(config->input_frequency,
649 					   (ctrl_config->scl.i2c * I2C_PRESCL_REG_SCALE)) -
650 			      1;
651 
652 	/* update with actual value */
653 	ctrl_config->scl.i3c = config->input_frequency / ((prescl_i3c + 1) * I3C_PRESCL_REG_SCALE);
654 	ctrl_config->scl.i2c = config->input_frequency / ((prescl_i2c + 1) * I2C_PRESCL_REG_SCALE);
655 
656 	LOG_DBG("%s: I3C speed = %u, PRESCL_CTRL0.i3c = 0x%x", dev->name, ctrl_config->scl.i3c,
657 		prescl_i3c);
658 	LOG_DBG("%s: I2C speed = %u, PRESCL_CTRL0.i2c = 0x%x", dev->name, ctrl_config->scl.i2c,
659 		prescl_i2c);
660 
661 	/* Calculate the OD_LOW value assuming a desired T_low period of 210ns. */
662 	uint32_t pres_step = 1000000000 / (ctrl_config->scl.i3c * 4);
663 	int32_t od_low = DIV_ROUND_UP(I3C_BUS_TLOW_OD_MIN_NS, pres_step) - 2;
664 
665 	if (od_low < 0) {
666 		od_low = 0;
667 	}
668 	LOG_DBG("%s: PRESCL_CTRL1.od_low = 0x%x", dev->name, od_low);
669 
670 	/* disable in order to update timing */
671 	uint32_t ctrl = sys_read32(config->base + CTRL);
672 
673 	if (ctrl & CTRL_DEV_EN) {
674 		sys_write32(~CTRL_DEV_EN & ctrl, config->base + CTRL);
675 	}
676 
677 	sys_write32(PRESCL_CTRL0_I3C(prescl_i3c) | PRESCL_CTRL0_I2C(prescl_i2c),
678 		    config->base + PRESCL_CTRL0);
679 
680 	/* Sets the open drain low time relative to the push-pull. */
681 	sys_write32(PRESCL_CTRL1_OD_LOW(od_low & PRESCL_CTRL1_OD_LOW_MASK),
682 		    config->base + PRESCL_CTRL1);
683 
684 	/* reenable */
685 	if (ctrl & CTRL_DEV_EN) {
686 		sys_write32(CTRL_DEV_EN | ctrl, config->base + CTRL);
687 	}
688 }
689 
690 /**
691  * @brief Compute RR0 Value from addr
692  *
693  * @param addr Address of the target
694  *
695  * @return RR0 value
696  */
prepare_rr0_dev_address(uint16_t addr)697 static uint32_t prepare_rr0_dev_address(uint16_t addr)
698 {
699 	/* RR0[7:1] = addr[6:0] | parity^[0] */
700 	uint32_t ret = cdns_i3c_even_parity(addr);
701 
702 	if (addr & GENMASK(9, 7)) {
703 		/* RR0[15:13] = addr[9:7] */
704 		ret |= (addr & GENMASK(9, 7)) << 6;
705 		/* RR0[11] = 10b lvr addr */
706 		ret |= DEV_ID_RR0_LVR_EXT_ADDR;
707 	}
708 
709 	return ret;
710 }
711 
712 /**
713  * @brief Program Retaining Registers with device lists
714  *
715  * This will program the retaining register with the controller itself
716  *
717  * @param dev Pointer to controller device driver instance.
718  */
cdns_i3c_program_controller_retaining_reg(const struct device * dev)719 static void cdns_i3c_program_controller_retaining_reg(const struct device *dev)
720 {
721 	const struct cdns_i3c_config *config = dev->config;
722 	struct cdns_i3c_data *data = dev->data;
723 	/* Set controller retaining register */
724 	uint8_t controller_da = I3C_CONTROLLER_ADDR;
725 
726 	if (!i3c_addr_slots_is_free(&data->common.attached_dev.addr_slots, controller_da)) {
727 		controller_da =
728 			i3c_addr_slots_next_free_find(&data->common.attached_dev.addr_slots, 0);
729 		LOG_DBG("%s: 0x%02x DA selected for controller", dev->name, controller_da);
730 	}
731 	sys_write32(prepare_rr0_dev_address(controller_da), config->base + DEV_ID_RR0(0));
732 	/* Mark the address as I3C device */
733 	i3c_addr_slots_mark_i3c(&data->common.attached_dev.addr_slots, controller_da);
734 }
735 
736 #ifdef CONFIG_I3C_USE_IBI
cdns_i3c_controller_ibi_enable(const struct device * dev,struct i3c_device_desc * target)737 static int cdns_i3c_controller_ibi_enable(const struct device *dev, struct i3c_device_desc *target)
738 {
739 	uint32_t sir_map;
740 	uint32_t sir_cfg;
741 	const struct cdns_i3c_config *config = dev->config;
742 	struct cdns_i3c_i2c_dev_data *cdns_i3c_device_data = target->controller_priv;
743 	struct i3c_ccc_events i3c_events;
744 	int ret = 0;
745 
746 	if (!i3c_device_is_ibi_capable(target)) {
747 		ret = -EINVAL;
748 		return ret;
749 	}
750 
751 	/* TODO: check for duplicate in SIR */
752 
753 	sir_cfg = SIR_MAP_DEV_ROLE(I3C_BCR_DEVICE_ROLE(target->bcr)) |
754 		  SIR_MAP_DEV_DA(target->dynamic_addr) |
755 		  SIR_MAP_DEV_PL(target->data_length.max_ibi);
756 	if (target->ibi_cb != NULL) {
757 		sir_cfg |= SIR_MAP_DEV_ACK;
758 	}
759 	if (target->bcr & I3C_BCR_MAX_DATA_SPEED_LIMIT) {
760 		sir_cfg |= SIR_MAP_DEV_SLOW;
761 	}
762 
763 	LOG_DBG("%s: IBI enabling for 0x%02x (BCR 0x%02x)", dev->name, target->dynamic_addr,
764 		target->bcr);
765 
766 	/* Tell target to enable IBI */
767 	i3c_events.events = I3C_CCC_EVT_INTR;
768 	ret = i3c_ccc_do_events_set(target, true, &i3c_events);
769 	if (ret != 0) {
770 		LOG_ERR("%s: Error sending IBI ENEC for 0x%02x (%d)", dev->name,
771 			target->dynamic_addr, ret);
772 		return ret;
773 	}
774 
775 	sir_map = sys_read32(config->base + SIR_MAP_DEV_REG(cdns_i3c_device_data->id - 1));
776 	sir_map &= ~SIR_MAP_DEV_CONF_MASK(cdns_i3c_device_data->id - 1);
777 	sir_map |= SIR_MAP_DEV_CONF(cdns_i3c_device_data->id - 1, sir_cfg);
778 
779 	sys_write32(sir_map, config->base + SIR_MAP_DEV_REG(cdns_i3c_device_data->id - 1));
780 
781 	return ret;
782 }
783 
cdns_i3c_controller_ibi_disable(const struct device * dev,struct i3c_device_desc * target)784 static int cdns_i3c_controller_ibi_disable(const struct device *dev, struct i3c_device_desc *target)
785 {
786 	uint32_t sir_map;
787 	const struct cdns_i3c_config *config = dev->config;
788 	struct cdns_i3c_i2c_dev_data *cdns_i3c_device_data = target->controller_priv;
789 	struct i3c_ccc_events i3c_events;
790 	int ret = 0;
791 
792 	if (!i3c_device_is_ibi_capable(target)) {
793 		ret = -EINVAL;
794 		return ret;
795 	}
796 
797 	/* Tell target to disable IBI */
798 	i3c_events.events = I3C_CCC_EVT_INTR;
799 	ret = i3c_ccc_do_events_set(target, false, &i3c_events);
800 	if (ret != 0) {
801 		LOG_ERR("%s: Error sending IBI DISEC for 0x%02x (%d)", dev->name,
802 			target->dynamic_addr, ret);
803 		return ret;
804 	}
805 
806 	sir_map = sys_read32(config->base + SIR_MAP_DEV_REG(cdns_i3c_device_data->id - 1));
807 	sir_map &= ~SIR_MAP_DEV_CONF_MASK(cdns_i3c_device_data->id - 1);
808 	sir_map |=
809 		SIR_MAP_DEV_CONF(cdns_i3c_device_data->id - 1, SIR_MAP_DEV_DA(I3C_BROADCAST_ADDR));
810 	sys_write32(sir_map, config->base + SIR_MAP_DEV_REG(cdns_i3c_device_data->id - 1));
811 
812 	return ret;
813 }
814 
cdns_i3c_target_ibi_raise_hj(const struct device * dev)815 static int cdns_i3c_target_ibi_raise_hj(const struct device *dev)
816 {
817 	const struct cdns_i3c_config *config = dev->config;
818 	struct cdns_i3c_data *data = dev->data;
819 	struct i3c_config_controller *ctrl_config = &data->common.ctrl_config;
820 
821 	/* HJ requests should not be done by primary controllers */
822 	if (!ctrl_config->is_secondary) {
823 		LOG_ERR("%s: controller is primary, HJ not available", dev->name);
824 		return -ENOTSUP;
825 	}
826 	/* Check if target already has a DA assigned to it */
827 	if (sys_read32(config->base + SLV_STATUS1) & SLV_STATUS1_HAS_DA) {
828 		LOG_ERR("%s: HJ not available, DA already assigned", dev->name);
829 		return -EACCES;
830 	}
831 	/* Check if HJ requests DISEC CCC with DISHJ field set has been received */
832 	if (sys_read32(config->base + SLV_STATUS1) & SLV_STATUS1_HJ_DIS) {
833 		LOG_ERR("%s: HJ requests are currently disabled by DISEC", dev->name);
834 		return -EAGAIN;
835 	}
836 
837 	sys_write32(CTRL_HJ_INIT | sys_read32(config->base + CTRL), config->base + CTRL);
838 	k_sem_reset(&data->ibi_hj_complete);
839 	if (k_sem_take(&data->ibi_hj_complete, K_MSEC(500)) != 0) {
840 		LOG_ERR("%s: timeout waiting for DAA after HJ", dev->name);
841 		return -ETIMEDOUT;
842 	}
843 	return 0;
844 }
845 
cdns_i3c_target_ibi_raise(const struct device * dev,struct i3c_ibi * request)846 static int cdns_i3c_target_ibi_raise(const struct device *dev, struct i3c_ibi *request)
847 {
848 	if (request == NULL) {
849 		return -EINVAL;
850 	}
851 
852 	switch (request->ibi_type) {
853 	case I3C_IBI_TARGET_INTR:
854 		return -ENOTSUP;
855 	case I3C_IBI_CONTROLLER_ROLE_REQUEST:
856 		/* TODO: Cadence I3C can support CR, but not implemented yet */
857 		return -ENOTSUP;
858 	case I3C_IBI_HOTJOIN:
859 		return cdns_i3c_target_ibi_raise_hj(dev);
860 	default:
861 		return -EINVAL;
862 	}
863 }
864 #endif
865 
cdns_i3c_cancel_transfer(const struct device * dev)866 static void cdns_i3c_cancel_transfer(const struct device *dev)
867 {
868 	struct cdns_i3c_data *data = dev->data;
869 	const struct cdns_i3c_config *config = dev->config;
870 	uint32_t val;
871 	uint32_t retry_count;
872 
873 	/* Disable further interrupts */
874 	sys_write32(MST_INT_CMDD_EMP, config->base + MST_IDR);
875 
876 	/* Ignore if no pending transfer */
877 	if (data->xfer.num_cmds == 0) {
878 		return;
879 	}
880 
881 	data->xfer.num_cmds = 0;
882 
883 	/* Clear main enable bit to disable further transactions */
884 	sys_write32(~CTRL_DEV_EN & sys_read32(config->base + CTRL), config->base + CTRL);
885 
886 	/**
887 	 * Spin waiting for device to go idle. It is unlikely that this will
888 	 * actually take any time since we only get here if a transaction didn't
889 	 * complete in a long time.
890 	 */
891 	retry_count = I3C_MAX_IDLE_CANCEL_WAIT_RETRIES;
892 	while (retry_count--) {
893 		val = sys_read32(config->base + MST_STATUS0);
894 		if (val & MST_STATUS0_IDLE) {
895 			break;
896 		}
897 		k_msleep(10);
898 	}
899 	if (retry_count == 0) {
900 		data->xfer.ret = -ETIMEDOUT;
901 	}
902 
903 	/**
904 	 * Flush all queues.
905 	 */
906 	sys_write32(FLUSH_RX_FIFO | FLUSH_TX_FIFO | FLUSH_CMD_FIFO | FLUSH_CMD_RESP,
907 		    config->base + FLUSH_CTRL);
908 
909 	/* Re-enable device */
910 	sys_write32(CTRL_DEV_EN | sys_read32(config->base + CTRL), config->base + CTRL);
911 }
912 
913 /**
914  * @brief Start a I3C/I2C Transfer
915  *
916  * This is to be called from a I3C/I2C transfer function. This will write
917  * all data to tx and cmd fifos
918  *
919  * @param dev Pointer to controller device driver instance.
920  */
cdns_i3c_start_transfer(const struct device * dev)921 static void cdns_i3c_start_transfer(const struct device *dev)
922 {
923 	struct cdns_i3c_data *data = dev->data;
924 	const struct cdns_i3c_config *config = dev->config;
925 	struct cdns_i3c_xfer *xfer = &data->xfer;
926 
927 	/* Ensure no pending command response queue threshold interrupt */
928 	sys_write32(MST_INT_CMDD_EMP, config->base + MST_ICR);
929 
930 	/* Make sure RX FIFO is empty. */
931 	while (!cdns_i3c_rx_fifo_empty(config)) {
932 		(void)sys_read32(config->base + RX_FIFO);
933 	}
934 	/* Make sure CMDR FIFO is empty too */
935 	while (!cdns_i3c_cmd_rsp_fifo_empty(config)) {
936 		(void)sys_read32(config->base + CMDR);
937 	}
938 
939 	/* Write all tx data to fifo */
940 	for (unsigned int i = 0; i < xfer->num_cmds; i++) {
941 		if (!(xfer->cmds[i].cmd0 & CMD0_FIFO_RNW)) {
942 			cdns_i3c_write_tx_fifo(config, xfer->cmds[i].buf, xfer->cmds[i].len);
943 		}
944 	}
945 
946 	/* Write all data to cmd fifos */
947 	for (unsigned int i = 0; i < xfer->num_cmds; i++) {
948 		/* The command ID is just the msg index. */
949 		xfer->cmds[i].cmd1 |= CMD1_FIFO_CMDID(i);
950 		sys_write32(xfer->cmds[i].cmd1, config->base + CMD1_FIFO);
951 		sys_write32(xfer->cmds[i].cmd0, config->base + CMD0_FIFO);
952 	}
953 
954 	/* kickoff transfer */
955 	sys_write32(CTRL_MCS | sys_read32(config->base + CTRL), config->base + CTRL);
956 	sys_write32(MST_INT_CMDD_EMP, config->base + MST_IER);
957 }
958 
959 /**
960  * @brief Send Common Command Code (CCC).
961  *
962  * @see i3c_do_ccc
963  *
964  * @param dev Pointer to controller device driver instance.
965  * @param payload Pointer to CCC payload.
966  *
967  * @return @see i3c_do_ccc
968  */
cdns_i3c_do_ccc(const struct device * dev,struct i3c_ccc_payload * payload)969 static int cdns_i3c_do_ccc(const struct device *dev, struct i3c_ccc_payload *payload)
970 {
971 	const struct cdns_i3c_config *config = dev->config;
972 	struct cdns_i3c_data *data = dev->data;
973 	struct cdns_i3c_cmd *dcmd = &data->xfer.cmds[0];
974 	int ret = 0;
975 	int num_cmds = 0;
976 
977 	/* make sure we are currently the active controller */
978 	if (!(sys_read32(config->base + MST_STATUS0) & MST_STATUS0_MASTER_MODE)) {
979 		return -EACCES;
980 	}
981 
982 	if (payload == NULL) {
983 		return -EINVAL;
984 	}
985 
986 	/*
987 	 * Ensure data will fit within FIFOs.
988 	 *
989 	 * TODO: This limitation prevents burst transfers greater than the
990 	 *       FIFO sizes and should be replaced with an implementation that
991 	 *       utilizes the RX/TX data threshold interrupts.
992 	 */
993 	uint32_t num_msgs =
994 		1 + ((payload->ccc.data_len > 0) ? payload->targets.num_targets
995 						 : MAX(payload->targets.num_targets - 1, 0));
996 	if (num_msgs > data->hw_cfg.cmd_mem_depth || num_msgs > data->hw_cfg.cmdr_mem_depth) {
997 		LOG_ERR("%s: Too many messages", dev->name);
998 		return -ENOMEM;
999 	}
1000 
1001 	uint32_t rxsize = 0;
1002 	uint32_t txsize = ROUND_UP(payload->ccc.data_len, 4);
1003 
1004 	for (int i = 0; i < payload->targets.num_targets; i++) {
1005 		if (payload->targets.payloads[i].rnw) {
1006 			rxsize += ROUND_UP(payload->targets.payloads[i].data_len, 4);
1007 		} else {
1008 			txsize += ROUND_UP(payload->targets.payloads[i].data_len, 4);
1009 		}
1010 	}
1011 	if ((rxsize > data->hw_cfg.rx_mem_depth) || (txsize > data->hw_cfg.tx_mem_depth)) {
1012 		LOG_ERR("%s: Total RX and/or TX transfer larger than FIFO", dev->name);
1013 		return -ENOMEM;
1014 	}
1015 
1016 	LOG_DBG("%s: CCC[0x%02x]", dev->name, payload->ccc.id);
1017 
1018 	k_mutex_lock(&data->bus_lock, K_FOREVER);
1019 
1020 	/* wait for idle */
1021 	ret = cdns_i3c_wait_for_idle(dev);
1022 	if (ret != 0) {
1023 		goto error;
1024 	}
1025 
1026 	dcmd->cmd1 = CMD1_FIFO_CCC(payload->ccc.id);
1027 	dcmd->cmd0 = CMD0_FIFO_IS_CCC;
1028 	dcmd->len = 0;
1029 
1030 	size_t idx = 0;
1031 
1032 	if (payload->ccc.data_len > 0) {
1033 		/* Write additional data for CCC if needed */
1034 		dcmd->buf = payload->ccc.data;
1035 		dcmd->len = payload->ccc.data_len;
1036 		dcmd->cmd0 |= CMD0_FIFO_PL_LEN(payload->ccc.data_len);
1037 		/* write the address of num_xfer which is to be updated upon message completion */
1038 		dcmd->num_xfer = &(payload->ccc.num_xfer);
1039 	} else if (payload->targets.num_targets > 0) {
1040 		dcmd->buf = payload->targets.payloads[0].data;
1041 		dcmd->len = payload->targets.payloads[0].data_len;
1042 		dcmd->cmd0 |= CMD0_FIFO_DEV_ADDR(payload->targets.payloads[0].addr) |
1043 			      CMD0_FIFO_PL_LEN(payload->targets.payloads[0].data_len);
1044 		if (payload->targets.payloads[0].rnw) {
1045 			dcmd->cmd0 |= CMD0_FIFO_RNW;
1046 		}
1047 		/* write the address of num_xfer which is to be updated upon message completion */
1048 		dcmd->num_xfer = &(payload->targets.payloads[0].num_xfer);
1049 		idx++;
1050 	}
1051 	num_cmds++;
1052 
1053 	if (!i3c_ccc_is_payload_broadcast(payload)) {
1054 		/*
1055 		 * If there are payload(s) for each target,
1056 		 * RESTART and then send payload for each target.
1057 		 */
1058 		while (idx < payload->targets.num_targets) {
1059 			num_cmds++;
1060 			struct cdns_i3c_cmd *cmd = &data->xfer.cmds[idx + 1];
1061 			struct i3c_ccc_target_payload *tgt_payload =
1062 				&payload->targets.payloads[idx];
1063 			/* Send repeated start on all transfers except the last */
1064 			if (idx < (payload->targets.num_targets - 1)) {
1065 				cmd->cmd0 |= CMD0_FIFO_RSBC;
1066 			}
1067 			cmd->cmd0 |= CMD0_FIFO_DEV_ADDR(tgt_payload->addr);
1068 			if (tgt_payload->rnw) {
1069 				cmd->cmd0 |= CMD0_FIFO_RNW;
1070 			}
1071 
1072 			cmd->buf = tgt_payload->data;
1073 			cmd->len = tgt_payload->data_len;
1074 			/*
1075 			 * write the address of num_xfer which is to be updated upon message
1076 			 * completion
1077 			 */
1078 			cmd->num_xfer = &(tgt_payload->num_xfer);
1079 
1080 			idx++;
1081 		}
1082 	}
1083 
1084 	data->xfer.ret = -ETIMEDOUT;
1085 	data->xfer.num_cmds = num_cmds;
1086 
1087 	cdns_i3c_start_transfer(dev);
1088 	if (k_sem_take(&data->xfer.complete, K_MSEC(1000)) != 0) {
1089 		cdns_i3c_cancel_transfer(dev);
1090 	}
1091 
1092 	if (data->xfer.ret < 0) {
1093 		LOG_ERR("%s: CCC[0x%02x] error (%d)", dev->name, payload->ccc.id, data->xfer.ret);
1094 	}
1095 
1096 	ret = data->xfer.ret;
1097 error:
1098 	k_mutex_unlock(&data->bus_lock);
1099 
1100 	return ret;
1101 }
1102 
1103 /**
1104  * @brief Perform Dynamic Address Assignment.
1105  *
1106  * @see i3c_do_daa
1107  *
1108  * @param dev Pointer to controller device driver instance.
1109  *
1110  * @return @see i3c_do_daa
1111  */
cdns_i3c_do_daa(const struct device * dev)1112 static int cdns_i3c_do_daa(const struct device *dev)
1113 {
1114 	struct cdns_i3c_data *data = dev->data;
1115 	const struct cdns_i3c_config *config = dev->config;
1116 	struct i3c_config_controller *ctrl_config = &data->common.ctrl_config;
1117 
1118 	/* DAA should not be done by secondary controllers */
1119 	if (ctrl_config->is_secondary) {
1120 		return -ENOTSUP;
1121 	}
1122 
1123 	/* read dev active reg */
1124 	uint32_t olddevs = sys_read32(config->base + DEVS_CTRL) & DEVS_CTRL_DEVS_ACTIVE_MASK;
1125 	/* ignore the controller register */
1126 	olddevs |= BIT(0);
1127 
1128 	/* the Cadence I3C IP will assign an address for it from the RR */
1129 	struct i3c_ccc_payload entdaa_ccc;
1130 
1131 	memset(&entdaa_ccc, 0, sizeof(entdaa_ccc));
1132 	entdaa_ccc.ccc.id = I3C_CCC_ENTDAA;
1133 
1134 	int status = cdns_i3c_do_ccc(dev, &entdaa_ccc);
1135 
1136 	if (status != 0) {
1137 		return status;
1138 	}
1139 
1140 	/* read again dev active reg  */
1141 	uint32_t newdevs = sys_read32(config->base + DEVS_CTRL) & DEVS_CTRL_DEVS_ACTIVE_MASK;
1142 	/* look for new bits that were set */
1143 	newdevs &= ~olddevs;
1144 
1145 	if (newdevs) {
1146 		/* loop through each set bit for new devices */
1147 		for (uint8_t i = find_lsb_set(newdevs); i <= find_msb_set(newdevs); i++) {
1148 			uint8_t rr_idx = i - 1;
1149 
1150 			if (newdevs & BIT(rr_idx)) {
1151 				/* Read RRx registers */
1152 				uint32_t dev_id_rr0 = sys_read32(config->base + DEV_ID_RR0(rr_idx));
1153 				uint32_t dev_id_rr1 = sys_read32(config->base + DEV_ID_RR1(rr_idx));
1154 				uint32_t dev_id_rr2 = sys_read32(config->base + DEV_ID_RR2(rr_idx));
1155 
1156 				uint64_t pid = ((uint64_t)dev_id_rr1 << 16) + (dev_id_rr2 >> 16);
1157 				uint8_t dyn_addr = (dev_id_rr0 & 0xFE) >> 1;
1158 				uint8_t bcr = dev_id_rr2 >> 8;
1159 				uint8_t dcr = dev_id_rr2 & 0xFF;
1160 
1161 				const struct i3c_device_id i3c_id = I3C_DEVICE_ID(pid);
1162 				struct i3c_device_desc *target = i3c_device_find(dev, &i3c_id);
1163 
1164 				if (target == NULL) {
1165 					LOG_INF("%s: PID 0x%012llx is not in registered device "
1166 						"list, given DA 0x%02x",
1167 						dev->name, pid, dyn_addr);
1168 					i3c_addr_slots_mark_i3c(
1169 						&data->common.attached_dev.addr_slots, dyn_addr);
1170 				} else {
1171 					target->dynamic_addr = dyn_addr;
1172 					target->bcr = bcr;
1173 					target->dcr = dcr;
1174 
1175 					LOG_DBG("%s: PID 0x%012llx assigned dynamic address 0x%02x",
1176 						dev->name, pid, dyn_addr);
1177 				}
1178 			}
1179 		}
1180 	} else {
1181 		LOG_DBG("%s: ENTDAA: No devices found", dev->name);
1182 	}
1183 
1184 	/* mark slot as not free, may already be set if already attached */
1185 	data->free_rr_slots &= ~newdevs;
1186 
1187 	/* Unmask Hot-Join request interrupts. HJ will send DISEC HJ from the CTRL value */
1188 	struct i3c_ccc_events i3c_events;
1189 
1190 	i3c_events.events = I3C_CCC_EVT_HJ;
1191 	status = i3c_ccc_do_events_all_set(dev, true, &i3c_events);
1192 	if (status != 0) {
1193 		LOG_DBG("%s: Broadcast ENEC was NACK", dev->name);
1194 	}
1195 
1196 	return 0;
1197 }
1198 
1199 /**
1200  * @brief Configure I2C hardware.
1201  *
1202  * @param dev Pointer to controller device driver instance.
1203  * @param config Value of the configuration parameters.
1204  *
1205  * @retval 0 If successful.
1206  * @retval -EINVAL If invalid configure parameters.
1207  * @retval -EIO General Input/Output errors.
1208  * @retval -ENOSYS If not implemented.
1209  */
cdns_i3c_i2c_api_configure(const struct device * dev,uint32_t config)1210 static int cdns_i3c_i2c_api_configure(const struct device *dev, uint32_t config)
1211 {
1212 	struct cdns_i3c_data *data = dev->data;
1213 	struct i3c_config_controller *ctrl_config = &data->common.ctrl_config;
1214 
1215 	switch (I2C_SPEED_GET(config)) {
1216 	case I2C_SPEED_STANDARD:
1217 		ctrl_config->scl.i2c = 100000;
1218 		break;
1219 	case I2C_SPEED_FAST:
1220 		ctrl_config->scl.i2c = 400000;
1221 		break;
1222 	case I2C_SPEED_FAST_PLUS:
1223 		ctrl_config->scl.i2c = 1000000;
1224 		break;
1225 	case I2C_SPEED_HIGH:
1226 		ctrl_config->scl.i2c = 3400000;
1227 		break;
1228 	case I2C_SPEED_ULTRA:
1229 		ctrl_config->scl.i2c = 5000000;
1230 		break;
1231 	default:
1232 		break;
1233 	}
1234 
1235 	cdns_i3c_set_prescalers(dev);
1236 
1237 	return 0;
1238 }
1239 
1240 /**
1241  * @brief Configure I3C hardware.
1242  *
1243  * @param dev Pointer to controller device driver instance.
1244  * @param type Type of configuration parameters being passed
1245  *             in @p config.
1246  * @param config Pointer to the configuration parameters.
1247  *
1248  * @retval 0 If successful.
1249  * @retval -EINVAL If invalid configure parameters.
1250  * @retval -EIO General Input/Output errors.
1251  * @retval -ENOSYS If not implemented.
1252  */
cdns_i3c_configure(const struct device * dev,enum i3c_config_type type,void * config)1253 static int cdns_i3c_configure(const struct device *dev, enum i3c_config_type type, void *config)
1254 {
1255 	struct cdns_i3c_data *data = dev->data;
1256 	struct i3c_config_controller *ctrl_cfg = config;
1257 
1258 	if ((ctrl_cfg->scl.i2c == 0U) || (ctrl_cfg->scl.i3c == 0U)) {
1259 		return -EINVAL;
1260 	}
1261 
1262 	data->common.ctrl_config.scl.i3c = ctrl_cfg->scl.i3c;
1263 	data->common.ctrl_config.scl.i2c = ctrl_cfg->scl.i2c;
1264 	cdns_i3c_set_prescalers(dev);
1265 
1266 	return 0;
1267 }
1268 
1269 /**
1270  * @brief Complete a I3C/I2C Transfer
1271  *
1272  * This is to be called from an ISR when the Command Response FIFO
1273  * is Empty. This will check each Command Response reading the RX
1274  * FIFO if message was a RnW and if any message had an error.
1275  *
1276  * @param dev Pointer to controller device driver instance.
1277  */
cdns_i3c_complete_transfer(const struct device * dev)1278 static void cdns_i3c_complete_transfer(const struct device *dev)
1279 {
1280 	struct cdns_i3c_data *data = dev->data;
1281 	const struct cdns_i3c_config *config = dev->config;
1282 	uint32_t cmdr;
1283 	uint32_t id = 0;
1284 	uint32_t rx = 0;
1285 	int ret = 0;
1286 	struct cdns_i3c_cmd *cmd;
1287 	bool was_full;
1288 
1289 	/* Used only to determine in the case of a controller abort */
1290 	was_full = cdns_i3c_rx_fifo_full(config);
1291 
1292 	/* Disable further interrupts */
1293 	sys_write32(MST_INT_CMDD_EMP, config->base + MST_IDR);
1294 
1295 	/* Ignore if no pending transfer */
1296 	if (data->xfer.num_cmds == 0) {
1297 		return;
1298 	}
1299 
1300 	/* Process all results in fifo */
1301 	for (uint32_t status0 = sys_read32(config->base + MST_STATUS0);
1302 	     !(status0 & MST_STATUS0_CMDR_EMP); status0 = sys_read32(config->base + MST_STATUS0)) {
1303 		cmdr = sys_read32(config->base + CMDR);
1304 		id = CMDR_CMDID(cmdr);
1305 
1306 		if (id == CMDR_CMDID_HJACK_DISEC || id == CMDR_CMDID_HJACK_ENTDAA ||
1307 		    id >= data->xfer.num_cmds) {
1308 			continue;
1309 		}
1310 
1311 		cmd = &data->xfer.cmds[id];
1312 
1313 		/* Read any rx data into buffer */
1314 		if (cmd->cmd0 & CMD0_FIFO_RNW) {
1315 			rx = MIN(CMDR_XFER_BYTES(cmdr), cmd->len);
1316 			if (cmd->num_xfer != NULL) {
1317 				*cmd->num_xfer = rx;
1318 			}
1319 			ret = cdns_i3c_read_rx_fifo(config, cmd->buf, rx);
1320 		}
1321 
1322 		/* Record error */
1323 		cmd->error = CMDR_ERROR(cmdr);
1324 	}
1325 
1326 	for (int i = 0; i < data->xfer.num_cmds; i++) {
1327 		switch (data->xfer.cmds[i].error) {
1328 		case CMDR_NO_ERROR:
1329 			break;
1330 
1331 		case CMDR_MST_ABORT:
1332 			/*
1333 			 * A controller abort is forced if the RX FIFO fills up
1334 			 * There is also the case where the fifo can be full as
1335 			 * the len of the packet is the same length of the fifo
1336 			 * Check that the requested len is greater than the total
1337 			 * transferred to confirm that is not case. Otherwise the
1338 			 * abort was caused by the buffer length being meet and
1339 			 * the target did not give an End of Data (EoD) in the T
1340 			 * bit. Do not treat that condition as an error because
1341 			 * some targets will just auto-increment the read address
1342 			 * way beyond the buffer not giving an EoD.
1343 			 */
1344 			if ((was_full) && (data->xfer.cmds[i].len > *data->xfer.cmds[i].num_xfer)) {
1345 				ret = -ENOSPC;
1346 			} else {
1347 				LOG_DBG("%s: Controller Abort due to buffer length excedded with "
1348 					"no EoD from target",
1349 					dev->name);
1350 			}
1351 			break;
1352 
1353 		case CMDR_DDR_PREAMBLE_ERROR:
1354 		case CMDR_DDR_PARITY_ERROR:
1355 		case CMDR_M0_ERROR:
1356 		case CMDR_M1_ERROR:
1357 		case CMDR_M2_ERROR:
1358 		case CMDR_NACK_RESP:
1359 		case CMDR_DDR_DROPPED:
1360 			ret = -EIO;
1361 			break;
1362 
1363 		case CMDR_DDR_RX_FIFO_OVF:
1364 		case CMDR_DDR_TX_FIFO_UNF:
1365 			ret = -ENOSPC;
1366 			break;
1367 
1368 		case CMDR_INVALID_DA:
1369 		default:
1370 			ret = -EINVAL;
1371 			break;
1372 		}
1373 	}
1374 
1375 	data->xfer.ret = ret;
1376 
1377 	/* Indicate no transfer is pending */
1378 	data->xfer.num_cmds = 0;
1379 
1380 	k_sem_give(&data->xfer.complete);
1381 }
1382 
1383 /**
1384  * @brief Transfer messages in I2C mode.
1385  *
1386  * @param dev Pointer to device driver instance.
1387  * @param target Pointer to target device descriptor.
1388  * @param msgs Pointer to I2C messages.
1389  * @param num_msgs Number of messages to transfers.
1390  *
1391  * @retval 0 If successful.
1392  * @retval -EIO General input / output error.
1393  * @retval -EINVAL Address not registered
1394  */
cdns_i3c_i2c_transfer(const struct device * dev,struct i3c_i2c_device_desc * i2c_dev,struct i2c_msg * msgs,uint8_t num_msgs)1395 static int cdns_i3c_i2c_transfer(const struct device *dev, struct i3c_i2c_device_desc *i2c_dev,
1396 				 struct i2c_msg *msgs, uint8_t num_msgs)
1397 {
1398 	const struct cdns_i3c_config *config = dev->config;
1399 	struct cdns_i3c_data *data = dev->data;
1400 	uint32_t txsize = 0;
1401 	uint32_t rxsize = 0;
1402 	int ret;
1403 
1404 	/* make sure we are currently the active controller */
1405 	if (!(sys_read32(config->base + MST_STATUS0) & MST_STATUS0_MASTER_MODE)) {
1406 		return -EACCES;
1407 	}
1408 
1409 	if (num_msgs == 0) {
1410 		return 0;
1411 	}
1412 
1413 	if (num_msgs > data->hw_cfg.cmd_mem_depth || num_msgs > data->hw_cfg.cmdr_mem_depth) {
1414 		LOG_ERR("%s: Too many messages", dev->name);
1415 		return -ENOMEM;
1416 	}
1417 
1418 	/*
1419 	 * Ensure data will fit within FIFOs
1420 	 */
1421 	for (unsigned int i = 0; i < num_msgs; i++) {
1422 		if ((msgs[i].flags & I2C_MSG_RW_MASK) == I2C_MSG_READ) {
1423 			rxsize += ROUND_UP(msgs[i].len, 4);
1424 		} else {
1425 			txsize += ROUND_UP(msgs[i].len, 4);
1426 		}
1427 	}
1428 	if ((rxsize > data->hw_cfg.rx_mem_depth) || (txsize > data->hw_cfg.tx_mem_depth)) {
1429 		LOG_ERR("%s: Total RX and/or TX transfer larger than FIFO", dev->name);
1430 		return -ENOMEM;
1431 	}
1432 
1433 	k_mutex_lock(&data->bus_lock, K_FOREVER);
1434 
1435 	/* wait for idle */
1436 	ret = cdns_i3c_wait_for_idle(dev);
1437 	if (ret != 0) {
1438 		goto error;
1439 	}
1440 
1441 	for (unsigned int i = 0; i < num_msgs; i++) {
1442 		struct cdns_i3c_cmd *cmd = &data->xfer.cmds[i];
1443 
1444 		cmd->len = msgs[i].len;
1445 		cmd->buf = msgs[i].buf;
1446 
1447 		cmd->cmd0 = CMD0_FIFO_PRIV_XMIT_MODE(XMIT_BURST_WITHOUT_SUBADDR);
1448 		cmd->cmd0 |= CMD0_FIFO_DEV_ADDR(i2c_dev->addr);
1449 		cmd->cmd0 |= CMD0_FIFO_PL_LEN(msgs[i].len);
1450 
1451 		/* Send repeated start on all transfers except the last or those marked STOP. */
1452 		if ((i < (num_msgs - 1)) && ((msgs[i].flags & I2C_MSG_STOP) == 0)) {
1453 			cmd->cmd0 |= CMD0_FIFO_RSBC;
1454 		}
1455 
1456 		if (msgs[i].flags & I2C_MSG_ADDR_10_BITS) {
1457 			cmd->cmd0 |= CMD0_FIFO_IS_10B;
1458 		}
1459 
1460 		if ((msgs[i].flags & I2C_MSG_RW_MASK) == I2C_MSG_READ) {
1461 			cmd->cmd0 |= CMD0_FIFO_RNW;
1462 		}
1463 
1464 		/* i2c transfers are a don't care for num_xfer */
1465 		cmd->num_xfer = NULL;
1466 	}
1467 
1468 	data->xfer.ret = -ETIMEDOUT;
1469 	data->xfer.num_cmds = num_msgs;
1470 
1471 	cdns_i3c_start_transfer(dev);
1472 	if (k_sem_take(&data->xfer.complete, K_MSEC(1000)) != 0) {
1473 		cdns_i3c_cancel_transfer(dev);
1474 	}
1475 
1476 	ret = data->xfer.ret;
1477 error:
1478 	k_mutex_unlock(&data->bus_lock);
1479 
1480 	return ret;
1481 }
1482 
cdns_i3c_master_get_rr_slot(const struct device * dev,uint8_t dyn_addr)1483 static int cdns_i3c_master_get_rr_slot(const struct device *dev, uint8_t dyn_addr)
1484 {
1485 	struct cdns_i3c_data *data = dev->data;
1486 	const struct cdns_i3c_config *config = dev->config;
1487 
1488 	if (dyn_addr == 0) {
1489 		if (!data->free_rr_slots) {
1490 			return -ENOSPC;
1491 		}
1492 
1493 		return find_lsb_set(data->free_rr_slots) - 1;
1494 	}
1495 
1496 	uint32_t activedevs = sys_read32(config->base + DEVS_CTRL) & DEVS_CTRL_DEVS_ACTIVE_MASK;
1497 
1498 	activedevs &= ~BIT(0);
1499 
1500 	/* loop through each set bit for new devices */
1501 	for (uint8_t i = find_lsb_set(activedevs); i <= find_msb_set(activedevs); i++) {
1502 		if (activedevs & BIT(i)) {
1503 			uint32_t rr = sys_read32(config->base + DEV_ID_RR0(i));
1504 
1505 			if (!(rr & DEV_ID_RR0_IS_I3C) || DEV_ID_RR0_GET_DEV_ADDR(rr) != dyn_addr) {
1506 				continue;
1507 			}
1508 			return i;
1509 		}
1510 	}
1511 
1512 	return -EINVAL;
1513 }
1514 
cdns_i3c_attach_device(const struct device * dev,struct i3c_device_desc * desc,uint8_t addr)1515 static int cdns_i3c_attach_device(const struct device *dev, struct i3c_device_desc *desc,
1516 				  uint8_t addr)
1517 {
1518 	const struct cdns_i3c_config *config = dev->config;
1519 	struct cdns_i3c_data *data = dev->data;
1520 	int slot = cdns_i3c_master_get_rr_slot(dev, desc->dynamic_addr);
1521 
1522 	if (slot < 0) {
1523 		LOG_ERR("%s: no space for i3c device: %s", dev->name, desc->dev->name);
1524 		return slot;
1525 	}
1526 
1527 	k_mutex_lock(&data->bus_lock, K_FOREVER);
1528 
1529 	data->cdns_i3c_i2c_priv_data[slot].id = slot;
1530 	desc->controller_priv = &(data->cdns_i3c_i2c_priv_data[slot]);
1531 	data->free_rr_slots &= ~BIT(slot);
1532 
1533 	uint32_t dev_id_rr0 = DEV_ID_RR0_IS_I3C | prepare_rr0_dev_address(addr);
1534 	uint32_t dev_id_rr1 = DEV_ID_RR1_PID_MSB((desc->pid & 0xFFFFFFFF0000) >> 16);
1535 	uint32_t dev_id_rr2 = DEV_ID_RR2_PID_LSB(desc->pid & 0xFFFF);
1536 
1537 	sys_write32(dev_id_rr0, config->base + DEV_ID_RR0(slot));
1538 	sys_write32(dev_id_rr1, config->base + DEV_ID_RR1(slot));
1539 	sys_write32(dev_id_rr2, config->base + DEV_ID_RR2(slot));
1540 
1541 	/** Mark Devices as active, devices that will be found and marked active during DAA,
1542 	 * it will be given the exact DA programmed in it's RR if the PID matches and marked
1543 	 * as active duing ENTDAA, otherwise they get set as active here. If dynamic address
1544 	 * is set, then it assumed that it was already initialized by the primary controller.
1545 	 */
1546 	if ((desc->static_addr != 0) || (desc->dynamic_addr != 0)) {
1547 		sys_write32(sys_read32(config->base + DEVS_CTRL) | DEVS_CTRL_DEV_ACTIVE(slot),
1548 			    config->base + DEVS_CTRL);
1549 	}
1550 
1551 	k_mutex_unlock(&data->bus_lock);
1552 
1553 	return 0;
1554 }
1555 
cdns_i3c_reattach_device(const struct device * dev,struct i3c_device_desc * desc,uint8_t old_dyn_addr)1556 static int cdns_i3c_reattach_device(const struct device *dev, struct i3c_device_desc *desc,
1557 				    uint8_t old_dyn_addr)
1558 {
1559 	const struct cdns_i3c_config *config = dev->config;
1560 	struct cdns_i3c_data *data = dev->data;
1561 	struct cdns_i3c_i2c_dev_data *cdns_i3c_device_data = desc->controller_priv;
1562 
1563 	if (cdns_i3c_device_data == NULL) {
1564 		LOG_ERR("%s: %s: device not attached", dev->name, desc->dev->name);
1565 		return -EINVAL;
1566 	}
1567 
1568 	k_mutex_lock(&data->bus_lock, K_FOREVER);
1569 
1570 	uint32_t dev_id_rr0 = DEV_ID_RR0_IS_I3C | prepare_rr0_dev_address(desc->dynamic_addr);
1571 	uint32_t dev_id_rr1 = DEV_ID_RR1_PID_MSB((desc->pid & 0xFFFFFFFF0000) >> 16);
1572 	uint32_t dev_id_rr2 = DEV_ID_RR2_PID_LSB(desc->pid & 0xFFFF) | DEV_ID_RR2_BCR(desc->bcr) |
1573 			      DEV_ID_RR2_DCR(desc->dcr);
1574 
1575 	sys_write32(dev_id_rr0, config->base + DEV_ID_RR0(cdns_i3c_device_data->id));
1576 	sys_write32(dev_id_rr1, config->base + DEV_ID_RR1(cdns_i3c_device_data->id));
1577 	sys_write32(dev_id_rr2, config->base + DEV_ID_RR2(cdns_i3c_device_data->id));
1578 
1579 	k_mutex_unlock(&data->bus_lock);
1580 
1581 	return 0;
1582 }
1583 
cdns_i3c_detach_device(const struct device * dev,struct i3c_device_desc * desc)1584 static int cdns_i3c_detach_device(const struct device *dev, struct i3c_device_desc *desc)
1585 {
1586 	const struct cdns_i3c_config *config = dev->config;
1587 	struct cdns_i3c_data *data = dev->data;
1588 	struct cdns_i3c_i2c_dev_data *cdns_i3c_device_data = desc->controller_priv;
1589 
1590 	if (cdns_i3c_device_data == NULL) {
1591 		LOG_ERR("%s: %s: device not attached", dev->name, desc->dev->name);
1592 		return -EINVAL;
1593 	}
1594 
1595 	k_mutex_lock(&data->bus_lock, K_FOREVER);
1596 
1597 	sys_write32(sys_read32(config->base + DEVS_CTRL) |
1598 			    DEVS_CTRL_DEV_CLR(cdns_i3c_device_data->id),
1599 		    config->base + DEVS_CTRL);
1600 	data->free_rr_slots |= BIT(cdns_i3c_device_data->id);
1601 	desc->controller_priv = NULL;
1602 
1603 	k_mutex_unlock(&data->bus_lock);
1604 
1605 	return 0;
1606 }
1607 
cdns_i3c_i2c_attach_device(const struct device * dev,struct i3c_i2c_device_desc * desc)1608 static int cdns_i3c_i2c_attach_device(const struct device *dev, struct i3c_i2c_device_desc *desc)
1609 {
1610 	const struct cdns_i3c_config *config = dev->config;
1611 	struct cdns_i3c_data *data = dev->data;
1612 
1613 	int slot = cdns_i3c_master_get_rr_slot(dev, 0);
1614 
1615 	if (slot < 0) {
1616 		LOG_ERR("%s: no space for i2c device: addr 0x%02x", dev->name, desc->addr);
1617 		return slot;
1618 	}
1619 
1620 	k_mutex_lock(&data->bus_lock, K_FOREVER);
1621 
1622 	uint32_t dev_id_rr0 = prepare_rr0_dev_address(desc->addr);
1623 	uint32_t dev_id_rr2 = DEV_ID_RR2_LVR(desc->lvr);
1624 
1625 	sys_write32(dev_id_rr0, config->base + DEV_ID_RR0(slot));
1626 	sys_write32(0, config->base + DEV_ID_RR1(slot));
1627 	sys_write32(dev_id_rr2, config->base + DEV_ID_RR2(slot));
1628 
1629 	data->cdns_i3c_i2c_priv_data[slot].id = slot;
1630 	desc->controller_priv = &(data->cdns_i3c_i2c_priv_data[slot]);
1631 	data->free_rr_slots &= ~BIT(slot);
1632 
1633 	sys_write32(sys_read32(config->base + DEVS_CTRL) | DEVS_CTRL_DEV_ACTIVE(slot),
1634 		    config->base + DEVS_CTRL);
1635 
1636 	k_mutex_unlock(&data->bus_lock);
1637 
1638 	return 0;
1639 }
1640 
cdns_i3c_i2c_detach_device(const struct device * dev,struct i3c_i2c_device_desc * desc)1641 static int cdns_i3c_i2c_detach_device(const struct device *dev, struct i3c_i2c_device_desc *desc)
1642 {
1643 	const struct cdns_i3c_config *config = dev->config;
1644 	struct cdns_i3c_data *data = dev->data;
1645 	struct cdns_i3c_i2c_dev_data *cdns_i2c_device_data = desc->controller_priv;
1646 
1647 	if (cdns_i2c_device_data == NULL) {
1648 		LOG_ERR("%s: device not attached", dev->name);
1649 		return -EINVAL;
1650 	}
1651 
1652 	k_mutex_lock(&data->bus_lock, K_FOREVER);
1653 
1654 	sys_write32(sys_read32(config->base + DEVS_CTRL) |
1655 			    DEVS_CTRL_DEV_CLR(cdns_i2c_device_data->id),
1656 		    config->base + DEVS_CTRL);
1657 	data->free_rr_slots |= BIT(cdns_i2c_device_data->id);
1658 	desc->controller_priv = NULL;
1659 
1660 	k_mutex_unlock(&data->bus_lock);
1661 
1662 	return 0;
1663 }
1664 
1665 /**
1666  * @brief Transfer messages in I3C mode.
1667  *
1668  * @see i3c_transfer
1669  *
1670  * @param dev Pointer to device driver instance.
1671  * @param target Pointer to target device descriptor.
1672  * @param msgs Pointer to I3C messages.
1673  * @param num_msgs Number of messages to transfers.
1674  *
1675  * @return @see i3c_transfer
1676  */
cdns_i3c_transfer(const struct device * dev,struct i3c_device_desc * target,struct i3c_msg * msgs,uint8_t num_msgs)1677 static int cdns_i3c_transfer(const struct device *dev, struct i3c_device_desc *target,
1678 			     struct i3c_msg *msgs, uint8_t num_msgs)
1679 {
1680 	const struct cdns_i3c_config *config = dev->config;
1681 	struct cdns_i3c_data *data = dev->data;
1682 	int txsize = 0;
1683 	int rxsize = 0;
1684 	int ret;
1685 
1686 	/* make sure we are currently the active controller */
1687 	if (!(sys_read32(config->base + MST_STATUS0) & MST_STATUS0_MASTER_MODE)) {
1688 		return -EACCES;
1689 	}
1690 
1691 	if (num_msgs == 0) {
1692 		return 0;
1693 	}
1694 
1695 	if (num_msgs > data->hw_cfg.cmd_mem_depth || num_msgs > data->hw_cfg.cmdr_mem_depth) {
1696 		LOG_ERR("%s: Too many messages", dev->name);
1697 		return -ENOMEM;
1698 	}
1699 
1700 	/*
1701 	 * Ensure data will fit within FIFOs.
1702 	 *
1703 	 * TODO: This limitation prevents burst transfers greater than the
1704 	 *       FIFO sizes and should be replaced with an implementation that
1705 	 *       utilizes the RX/TX data interrupts.
1706 	 */
1707 	for (int i = 0; i < num_msgs; i++) {
1708 		if ((msgs[i].flags & I3C_MSG_RW_MASK) == I3C_MSG_READ) {
1709 			rxsize += ROUND_UP(msgs[i].len, 4);
1710 		} else {
1711 			txsize += ROUND_UP(msgs[i].len, 4);
1712 		}
1713 	}
1714 	if ((rxsize > data->hw_cfg.rx_mem_depth) || (txsize > data->hw_cfg.tx_mem_depth)) {
1715 		LOG_ERR("%s: Total RX and/or TX transfer larger than FIFO", dev->name);
1716 		return -ENOMEM;
1717 	}
1718 
1719 	k_mutex_lock(&data->bus_lock, K_FOREVER);
1720 
1721 	/* wait for idle */
1722 	ret = cdns_i3c_wait_for_idle(dev);
1723 	if (ret != 0) {
1724 		goto error;
1725 	}
1726 
1727 	/*
1728 	 * Prepare transfer commands. Currently there is only a single transfer
1729 	 * in-flight but it would be possible to keep a queue of transfers. If so,
1730 	 * this preparation could be completed outside of the bus lock allowing
1731 	 * greater parallelism.
1732 	 */
1733 	bool send_broadcast = true;
1734 
1735 	for (int i = 0; i < num_msgs; i++) {
1736 		struct cdns_i3c_cmd *cmd = &data->xfer.cmds[i];
1737 		uint32_t pl = msgs[i].len;
1738 
1739 		cmd->len = pl;
1740 		cmd->buf = msgs[i].buf;
1741 
1742 		cmd->cmd0 = CMD0_FIFO_PRIV_XMIT_MODE(XMIT_BURST_WITHOUT_SUBADDR);
1743 		cmd->cmd0 |= CMD0_FIFO_DEV_ADDR(target->dynamic_addr);
1744 		if ((msgs[i].flags & I3C_MSG_RW_MASK) == I3C_MSG_READ) {
1745 			cmd->cmd0 |= CMD0_FIFO_RNW;
1746 			/*
1747 			 * For I3C_XMIT_MODE_NO_ADDR reads in SDN mode,
1748 			 * CMD0_FIFO_PL_LEN specifies the abort limit not bytes to read
1749 			 */
1750 			cmd->cmd0 |= CMD0_FIFO_PL_LEN(pl + 1);
1751 		} else {
1752 			cmd->cmd0 |= CMD0_FIFO_PL_LEN(pl);
1753 		}
1754 
1755 		/* Send broadcast header on first transfer or after a STOP. */
1756 		if (!(msgs[i].flags & I3C_MSG_NBCH) && (send_broadcast)) {
1757 			cmd->cmd0 |= CMD0_FIFO_BCH;
1758 			send_broadcast = false;
1759 		}
1760 
1761 		/* Send repeated start on all transfers except the last or those marked STOP. */
1762 		if ((i < (num_msgs - 1)) && ((msgs[i].flags & I3C_MSG_STOP) == 0)) {
1763 			cmd->cmd0 |= CMD0_FIFO_RSBC;
1764 		} else {
1765 			send_broadcast = true;
1766 		}
1767 
1768 		/* write the address of num_xfer which is to be updated upon message completion */
1769 		cmd->num_xfer = &(msgs[i].num_xfer);
1770 	}
1771 
1772 	data->xfer.ret = -ETIMEDOUT;
1773 	data->xfer.num_cmds = num_msgs;
1774 
1775 	cdns_i3c_start_transfer(dev);
1776 	if (k_sem_take(&data->xfer.complete, K_MSEC(1000)) != 0) {
1777 		LOG_ERR("%s: transfer timed out", dev->name);
1778 		cdns_i3c_cancel_transfer(dev);
1779 	}
1780 
1781 	ret = data->xfer.ret;
1782 error:
1783 	k_mutex_unlock(&data->bus_lock);
1784 
1785 	return ret;
1786 }
1787 
1788 #ifdef CONFIG_I3C_USE_IBI
cdns_i3c_read_ibi_fifo(const struct cdns_i3c_config * config,void * buf,uint32_t len)1789 static int cdns_i3c_read_ibi_fifo(const struct cdns_i3c_config *config, void *buf, uint32_t len)
1790 {
1791 	uint32_t *ptr = buf;
1792 	uint32_t remain, val;
1793 
1794 	for (remain = len; remain >= 4; remain -= 4) {
1795 		if (cdns_i3c_ibi_fifo_empty(config)) {
1796 			return -EIO;
1797 		}
1798 		val = sys_le32_to_cpu(sys_read32(config->base + IBI_DATA_FIFO));
1799 		*ptr++ = val;
1800 	}
1801 
1802 	if (remain > 0) {
1803 		if (cdns_i3c_ibi_fifo_empty(config)) {
1804 			return -EIO;
1805 		}
1806 		val = sys_le32_to_cpu(sys_read32(config->base + IBI_DATA_FIFO));
1807 		memcpy(ptr, &val, remain);
1808 	}
1809 
1810 	return 0;
1811 }
1812 
cdns_i3c_handle_ibi(const struct device * dev,uint32_t ibir)1813 static void cdns_i3c_handle_ibi(const struct device *dev, uint32_t ibir)
1814 {
1815 	const struct cdns_i3c_config *config = dev->config;
1816 	struct cdns_i3c_data *data = dev->data;
1817 
1818 	uint8_t ibi_data[CONFIG_I3C_IBI_MAX_PAYLOAD_SIZE];
1819 
1820 	/* The slave ID returned here is the device ID in the SIR map NOT the device ID
1821 	 * in the RR map.
1822 	 */
1823 	uint8_t slave_id = IBIR_SLVID(ibir);
1824 
1825 	if (slave_id == IBIR_SLVID_INV) {
1826 		/* DA does not match any value among SIR map */
1827 		return;
1828 	}
1829 
1830 	uint32_t dev_id_rr0 = sys_read32(config->base + DEV_ID_RR0(slave_id + 1));
1831 	uint8_t dyn_addr = DEV_ID_RR0_GET_DEV_ADDR(dev_id_rr0);
1832 	struct i3c_device_desc *desc =
1833 		i3c_dev_list_i3c_addr_find(&data->common.attached_dev, dyn_addr);
1834 
1835 	/*
1836 	 * Check for NAK or error conditions.
1837 	 *
1838 	 * Note: The logging is for debugging only so will be compiled out in most cases.
1839 	 * However, if the log level for this module is DEBUG and log mode is IMMEDIATE or MINIMAL,
1840 	 * this option is also set this may cause problems due to being inside an ISR.
1841 	 */
1842 	if (!(IBIR_ACKED & ibir)) {
1843 		LOG_DBG("%s: NAK for slave ID %u", dev->name, (unsigned int)slave_id);
1844 		return;
1845 	}
1846 	if (ibir & IBIR_ERROR) {
1847 		LOG_ERR("%s: Data overflow", dev->name);
1848 		return;
1849 	}
1850 
1851 	/* Read out any payload bytes */
1852 	uint8_t ibi_len = IBIR_XFER_BYTES(ibir);
1853 
1854 	if (ibi_len > 0) {
1855 		if (cdns_i3c_read_ibi_fifo(config, ibi_data, ibi_len) < 0) {
1856 			LOG_ERR("%s: Failed to get payload", dev->name);
1857 		}
1858 	}
1859 
1860 	if (i3c_ibi_work_enqueue_target_irq(desc, ibi_data, ibi_len) != 0) {
1861 		LOG_ERR("%s: Error enqueue IBI IRQ work", dev->name);
1862 	}
1863 }
1864 
cdns_i3c_handle_hj(const struct device * dev,uint32_t ibir)1865 static void cdns_i3c_handle_hj(const struct device *dev, uint32_t ibir)
1866 {
1867 	if (!(IBIR_ACKED & ibir)) {
1868 		LOG_DBG("%s: NAK for HJ", dev->name);
1869 		return;
1870 	}
1871 
1872 	if (i3c_ibi_work_enqueue_hotjoin(dev) != 0) {
1873 		LOG_ERR("%s: Error enqueue IBI HJ work", dev->name);
1874 	}
1875 }
1876 
cnds_i3c_master_demux_ibis(const struct device * dev)1877 static void cnds_i3c_master_demux_ibis(const struct device *dev)
1878 {
1879 	const struct cdns_i3c_config *config = dev->config;
1880 
1881 	for (uint32_t status0 = sys_read32(config->base + MST_STATUS0);
1882 	     !(status0 & MST_STATUS0_IBIR_EMP); status0 = sys_read32(config->base + MST_STATUS0)) {
1883 		uint32_t ibir = sys_read32(config->base + IBIR);
1884 
1885 		switch (IBIR_TYPE(ibir)) {
1886 		case IBIR_TYPE_IBI:
1887 			cdns_i3c_handle_ibi(dev, ibir);
1888 			break;
1889 		case IBIR_TYPE_HJ:
1890 			cdns_i3c_handle_hj(dev, ibir);
1891 			break;
1892 		case IBIR_TYPE_MR:
1893 			/* not implemented */
1894 			break;
1895 		default:
1896 			break;
1897 		}
1898 	}
1899 }
1900 
cdns_i3c_target_ibi_hj_complete(const struct device * dev)1901 static void cdns_i3c_target_ibi_hj_complete(const struct device *dev)
1902 {
1903 	struct cdns_i3c_data *data = dev->data;
1904 
1905 	k_sem_give(&data->ibi_hj_complete);
1906 }
1907 #endif
1908 
cdns_i3c_irq_handler(const struct device * dev)1909 static void cdns_i3c_irq_handler(const struct device *dev)
1910 {
1911 	const struct cdns_i3c_config *config = dev->config;
1912 
1913 	if (sys_read32(config->base + MST_STATUS0) & MST_STATUS0_MASTER_MODE) {
1914 		uint32_t int_st = sys_read32(config->base + MST_ISR);
1915 
1916 		/* Command queue empty */
1917 		if (int_st & MST_INT_HALTED) {
1918 			LOG_WRN("Core Halted, 2 read aborts");
1919 			sys_write32(MST_INT_HALTED, config->base + MST_ICR);
1920 		}
1921 
1922 		/* Command queue empty */
1923 		if (int_st & MST_INT_CMDD_EMP) {
1924 			cdns_i3c_complete_transfer(dev);
1925 			sys_write32(MST_INT_CMDD_EMP, config->base + MST_ICR);
1926 		}
1927 
1928 		/* Command queue threshold */
1929 		if (int_st & MST_INT_CMDD_THR) {
1930 			sys_write32(MST_INT_CMDD_THR, config->base + MST_ICR);
1931 		}
1932 
1933 		/* Command response threshold hit */
1934 		if (int_st & MST_INT_CMDR_THR) {
1935 			sys_write32(MST_INT_CMDR_THR, config->base + MST_ICR);
1936 		}
1937 
1938 		/* RX data ready */
1939 		if (int_st & MST_INT_RX_THR) {
1940 			sys_write32(MST_INT_RX_THR, config->base + MST_ICR);
1941 		}
1942 
1943 		/* In-band interrupt */
1944 		if (int_st & MST_INT_IBIR_THR) {
1945 			sys_write32(MST_INT_IBIR_THR, config->base + MST_ICR);
1946 #ifdef CONFIG_I3C_USE_IBI
1947 			cnds_i3c_master_demux_ibis(dev);
1948 #else
1949 			LOG_ERR("%s: IBI received - Kconfig for using IBIs is not enabled",
1950 				dev->name);
1951 #endif
1952 		}
1953 
1954 		/* In-band interrupt data */
1955 		if (int_st & MST_INT_IBID_THR) {
1956 			sys_write32(MST_INT_IBID_THR, config->base + MST_ICR);
1957 		}
1958 
1959 		/* In-band interrupt data */
1960 		if (int_st & MST_INT_TX_OVF) {
1961 			sys_write32(MST_INT_TX_OVF, config->base + MST_ICR);
1962 			LOG_ERR("%s: controller tx buffer overflow,", dev->name);
1963 		}
1964 
1965 		/* In-band interrupt data */
1966 		if (int_st & MST_INT_RX_UNF) {
1967 			sys_write32(MST_INT_RX_UNF, config->base + MST_ICR);
1968 			LOG_ERR("%s: controller rx buffer underflow,", dev->name);
1969 		}
1970 
1971 		/* In-band interrupt data */
1972 		if (int_st & MST_INT_IBID_THR) {
1973 			sys_write32(MST_INT_IBID_THR, config->base + MST_ICR);
1974 		}
1975 	} else {
1976 		uint32_t int_sl = sys_read32(config->base + SLV_ISR);
1977 		struct cdns_i3c_data *data = dev->data;
1978 		const struct i3c_target_callbacks *target_cb = data->target_config->callbacks;
1979 
1980 		/* SLV SDR rx fifo threshold */
1981 		if (int_sl & SLV_INT_SDR_RX_THR) {
1982 			/* while rx fifo is not empty */
1983 			while (!(sys_read32(config->base + SLV_STATUS1) &
1984 				 SLV_STATUS1_SDR_RX_EMPTY)) {
1985 				/* Target writes only write to the first byte of the 32 bit width
1986 				 * fifo
1987 				 */
1988 				uint8_t rx_data = (uint8_t)sys_read32(config->base + RX_FIFO);
1989 				/* call function pointer for write */
1990 				if (target_cb != NULL && target_cb->write_received_cb != NULL) {
1991 					target_cb->write_received_cb(data->target_config, rx_data);
1992 				}
1993 			}
1994 		}
1995 
1996 		/* SLV SDR tx fifo threshold */
1997 		if (int_sl & SLV_INT_SDR_TX_THR) {
1998 			int status = 0;
1999 
2000 			if (target_cb != NULL && target_cb->read_processed_cb) {
2001 				/* while tx fifo is not full and there is still data available */
2002 				while ((!(sys_read32(config->base + SLV_STATUS1) &
2003 					  SLV_STATUS1_SDR_TX_FULL)) &&
2004 				       (status == 0)) {
2005 					/* call function pointer for read */
2006 					uint8_t byte;
2007 					/* will return negative if no data left to transmit and 0 if
2008 					 * data available
2009 					 */
2010 					status = target_cb->read_processed_cb(data->target_config,
2011 									      &byte);
2012 					if (status == 0) {
2013 						cdns_i3c_write_tx_fifo(config, &byte, sizeof(byte));
2014 					}
2015 				}
2016 			}
2017 		}
2018 
2019 		/* SLV SDR rx complete */
2020 		if (int_sl & SLV_INT_SDR_RD_COMP) {
2021 			/* a read needs to be done on slv_status 0 else a NACK will happen */
2022 			(void)sys_read32(config->base + SLV_STATUS0);
2023 			/* call stop function pointer */
2024 			if (target_cb != NULL && target_cb->stop_cb) {
2025 				target_cb->stop_cb(data->target_config);
2026 			}
2027 		}
2028 
2029 		/* SLV SDR tx complete */
2030 		if (int_sl & SLV_INT_SDR_WR_COMP) {
2031 			/* a read needs to be done on slv_status 0 else a NACK will happen */
2032 			(void)sys_read32(config->base + SLV_STATUS0);
2033 			/* call stop function pointer */
2034 			if (target_cb != NULL && target_cb->stop_cb) {
2035 				target_cb->stop_cb(data->target_config);
2036 			}
2037 		}
2038 
2039 		/* DA has been updated */
2040 		if (int_sl & SLV_INT_DA_UPD) {
2041 			LOG_INF("%s: DA updated to 0x%02lx", dev->name,
2042 				SLV_STATUS1_DA(sys_read32(config->base + SLV_STATUS1)));
2043 			/* HJ could send a DISEC which would trigger the SLV_INT_EVENT_UP bit,
2044 			 * but it's still expected to eventually send a DAA
2045 			 */
2046 #ifdef CONFIG_I3C_USE_IBI
2047 			cdns_i3c_target_ibi_hj_complete(dev);
2048 #endif
2049 		}
2050 
2051 		/* HJ complete and DA has been assigned */
2052 		if (int_sl & SLV_INT_HJ_DONE) {
2053 		}
2054 
2055 		/* Controllership has been been given */
2056 		if (int_sl & SLV_INT_MR_DONE) {
2057 			/* TODO: implement support for controllership handoff */
2058 		}
2059 
2060 		/* EISC or DISEC has been received */
2061 		if (int_sl & SLV_INT_EVENT_UP) {
2062 		}
2063 
2064 		/* sdr transfer aborted by controller */
2065 		if (int_sl & SLV_INT_M_RD_ABORT) {
2066 			/* TODO: consider flushing tx buffer? */
2067 		}
2068 
2069 		/* SLV SDR rx fifo underflow */
2070 		if (int_sl & SLV_INT_SDR_RX_UNF) {
2071 			LOG_ERR("%s: slave sdr rx buffer underflow", dev->name);
2072 		}
2073 
2074 		/* SLV SDR tx fifo overflow */
2075 		if (int_sl & SLV_INT_SDR_TX_OVF) {
2076 			LOG_ERR("%s: slave sdr tx buffer overflow,", dev->name);
2077 		}
2078 
2079 		sys_write32(int_sl, config->base + SLV_ICR);
2080 	}
2081 }
2082 
cdns_i3c_read_hw_cfg(const struct device * dev)2083 static void cdns_i3c_read_hw_cfg(const struct device *dev)
2084 {
2085 	const struct cdns_i3c_config *config = dev->config;
2086 	struct cdns_i3c_data *data = dev->data;
2087 
2088 	uint32_t devid = sys_read32(config->base + DEV_ID);
2089 	uint32_t revid = sys_read32(config->base + REV_ID);
2090 
2091 	LOG_DBG("%s: Device info:\r\n"
2092 		"  vid: 0x%03lX, pid: 0x%03lX\r\n"
2093 		"  revision: major = %lu, minor = %lu\r\n"
2094 		"  device ID: 0x%04X",
2095 		dev->name, REV_ID_VID(revid), REV_ID_PID(revid), REV_ID_REV_MAJOR(revid),
2096 		REV_ID_REV_MINOR(revid), devid);
2097 
2098 	/*
2099 	 * Depths are specified as number of words (32bit), convert to bytes
2100 	 */
2101 	uint32_t cfg0 = sys_read32(config->base + CONF_STATUS0);
2102 	uint32_t cfg1 = sys_read32(config->base + CONF_STATUS1);
2103 
2104 	data->hw_cfg.cmdr_mem_depth = CONF_STATUS0_CMDR_DEPTH(cfg0) * 4;
2105 	data->hw_cfg.cmd_mem_depth = CONF_STATUS1_CMD_DEPTH(cfg1) * 4;
2106 	data->hw_cfg.rx_mem_depth = CONF_STATUS1_RX_DEPTH(cfg1) * 4;
2107 	data->hw_cfg.tx_mem_depth = CONF_STATUS1_TX_DEPTH(cfg1) * 4;
2108 	data->hw_cfg.ibir_mem_depth = CONF_STATUS0_IBIR_DEPTH(cfg0) * 4;
2109 
2110 	LOG_DBG("%s: FIFO info:\r\n"
2111 		"  cmd_mem_depth = %u\r\n"
2112 		"  cmdr_mem_depth = %u\r\n"
2113 		"  rx_mem_depth = %u\r\n"
2114 		"  tx_mem_depth = %u\r\n"
2115 		"  ibir_mem_depth = %u",
2116 		dev->name, data->hw_cfg.cmd_mem_depth, data->hw_cfg.cmdr_mem_depth,
2117 		data->hw_cfg.rx_mem_depth, data->hw_cfg.tx_mem_depth, data->hw_cfg.ibir_mem_depth);
2118 
2119 	/* Regardless of the cmd depth size we are limited by our cmd array length. */
2120 	data->hw_cfg.cmd_mem_depth = MIN(data->hw_cfg.cmd_mem_depth, ARRAY_SIZE(data->xfer.cmds));
2121 }
2122 
2123 /**
2124  * @brief Get configuration of the I3C hardware.
2125  *
2126  * This provides a way to get the current configuration of the I3C hardware.
2127  *
2128  * This can return cached config or probed hardware parameters, but it has to
2129  * be up to date with current configuration.
2130  *
2131  * @param[in] dev Pointer to controller device driver instance.
2132  * @param[in] type Type of configuration parameters being passed
2133  *                 in @p config.
2134  * @param[in,out] config Pointer to the configuration parameters.
2135  *
2136  * Note that if @p type is @c I3C_CONFIG_CUSTOM, @p config must contain
2137  * the ID of the parameter to be retrieved.
2138  *
2139  * @retval 0 If successful.
2140  * @retval -EIO General Input/Output errors.
2141  * @retval -ENOSYS If not implemented.
2142  */
cdns_i3c_config_get(const struct device * dev,enum i3c_config_type type,void * config)2143 static int cdns_i3c_config_get(const struct device *dev, enum i3c_config_type type, void *config)
2144 {
2145 	struct cdns_i3c_data *data = dev->data;
2146 	int ret = 0;
2147 
2148 	if (config == NULL) {
2149 		ret = -EINVAL;
2150 		goto out_configure;
2151 	}
2152 
2153 	(void)memcpy(config, &data->common.ctrl_config, sizeof(data->common.ctrl_config));
2154 
2155 out_configure:
2156 	return ret;
2157 }
2158 
2159 /**
2160  * @brief Writes to the Target's TX FIFO
2161  *
2162  * The Cadence I3C will then ACK read requests to it's TX FIFO from a
2163  * Controller
2164  *
2165  * @param dev Pointer to the device structure for an I3C controller
2166  *            driver configured in target mode.
2167  * @param buf Pointer to the buffer
2168  * @param len Length of the buffer
2169  *
2170  * @retval Total number of bytes written
2171  * @retval -EACCES Not in Target Mode
2172  * @retval -ENOSPC No space in Tx FIFO
2173  */
cdns_i3c_target_tx_write(const struct device * dev,uint8_t * buf,uint16_t len)2174 static int cdns_i3c_target_tx_write(const struct device *dev, uint8_t *buf, uint16_t len)
2175 {
2176 	const struct cdns_i3c_config *config = dev->config;
2177 	struct cdns_i3c_data *data = dev->data;
2178 
2179 	/* check if we are currently a target */
2180 	if (sys_read32(config->base + MST_STATUS0) & MST_STATUS0_MASTER_MODE) {
2181 		return -EACCES;
2182 	}
2183 
2184 	/* check if there is space available in the tx fifo */
2185 	if (sys_read32(config->base + SLV_STATUS1) & SLV_STATUS1_SDR_TX_FULL) {
2186 		return -ENOSPC;
2187 	}
2188 
2189 	k_mutex_lock(&data->bus_lock, K_FOREVER);
2190 
2191 	/* write as much as you can to the fifo */
2192 	uint16_t i;
2193 
2194 	for (i = 0;
2195 	     i < len && (!(sys_read32(config->base + SLV_STATUS1) & SLV_STATUS1_SDR_TX_FULL));
2196 	     i++) {
2197 		sys_write32((uint32_t)buf[i], config->base + TX_FIFO);
2198 	}
2199 
2200 	/* setup THR interrupt */
2201 	uint32_t thr_ctrl = sys_read32(config->base + TX_RX_THR_CTRL);
2202 
2203 	/*
2204 	 * Interrupt at half of the data or FIFO depth to give it enough time to be
2205 	 * processed. The ISR will then callback to the function pointer
2206 	 * `read_processed_cb` to collect more data to transmit
2207 	 */
2208 	thr_ctrl &= ~TX_THR_MASK;
2209 	thr_ctrl |= TX_THR(MIN((data->hw_cfg.tx_mem_depth / 4) / 2, i / 2));
2210 	sys_write32(thr_ctrl, config->base + TX_RX_THR_CTRL);
2211 
2212 	k_mutex_unlock(&data->bus_lock);
2213 
2214 	/* return total bytes written */
2215 	return i;
2216 }
2217 
2218 /**
2219  * @brief Instructs the I3C Target device to register itself to the I3C Controller
2220  *
2221  * This routine instructs the I3C Target device to register itself to the I3C
2222  * Controller via its parent controller's i3c_target_register() API.
2223  *
2224  * @param dev Pointer to target device driver instance.
2225  * @param cfg Config struct with functions and parameters used by the I3C driver
2226  * to send bus events
2227  *
2228  * @return @see i3c_device_find.
2229  */
cdns_i3c_target_register(const struct device * dev,struct i3c_target_config * cfg)2230 static int cdns_i3c_target_register(const struct device *dev, struct i3c_target_config *cfg)
2231 {
2232 	struct cdns_i3c_data *data = dev->data;
2233 
2234 	data->target_config = cfg;
2235 	return 0;
2236 }
2237 
2238 /**
2239  * @brief Unregisters the provided config as Target device
2240  *
2241  * This routine disables I3C target mode for the 'dev' I3C bus driver using
2242  * the provided 'config' struct containing the functions and parameters
2243  * to send bus events.
2244  *
2245  * @param dev Pointer to target device driver instance.
2246  * @param cfg Config struct with functions and parameters used by the I3C driver
2247  * to send bus events
2248  *
2249  * @return @see i3c_device_find.
2250  */
cdns_i3c_target_unregister(const struct device * dev,struct i3c_target_config * cfg)2251 static int cdns_i3c_target_unregister(const struct device *dev, struct i3c_target_config *cfg)
2252 {
2253 	/* no way to disable? maybe write DA to 0? */
2254 	return 0;
2255 }
2256 
2257 /**
2258  * @brief Find a registered I3C target device.
2259  *
2260  * This returns the I3C device descriptor of the I3C device
2261  * matching the incoming @p id.
2262  *
2263  * @param dev Pointer to controller device driver instance.
2264  * @param id Pointer to I3C device ID.
2265  *
2266  * @return @see i3c_device_find.
2267  */
cdns_i3c_device_find(const struct device * dev,const struct i3c_device_id * id)2268 static struct i3c_device_desc *cdns_i3c_device_find(const struct device *dev,
2269 						    const struct i3c_device_id *id)
2270 {
2271 	const struct cdns_i3c_config *config = dev->config;
2272 
2273 	return i3c_dev_list_find(&config->common.dev_list, id);
2274 }
2275 
2276 /**
2277  * Find a registered I2C target device.
2278  *
2279  * Controller only API.
2280  *
2281  * This returns the I2C device descriptor of the I2C device
2282  * matching the device address @p addr.
2283  *
2284  * @param dev Pointer to controller device driver instance.
2285  * @param id I2C target device address.
2286  *
2287  * @return @see i3c_i2c_device_find.
2288  */
cdns_i3c_i2c_device_find(const struct device * dev,uint16_t addr)2289 static struct i3c_i2c_device_desc *cdns_i3c_i2c_device_find(const struct device *dev, uint16_t addr)
2290 {
2291 	struct cdns_i3c_data *data = dev->data;
2292 
2293 	return i3c_dev_list_i2c_addr_find(&data->common.attached_dev, addr);
2294 }
2295 
2296 /**
2297  * @brief Transfer messages in I2C mode.
2298  *
2299  * @see i2c_transfer
2300  *
2301  * @param dev Pointer to device driver instance.
2302  * @param target Pointer to target device descriptor.
2303  * @param msgs Pointer to I2C messages.
2304  * @param num_msgs Number of messages to transfers.
2305  *
2306  * @return @see i2c_transfer
2307  */
cdns_i3c_i2c_api_transfer(const struct device * dev,struct i2c_msg * msgs,uint8_t num_msgs,uint16_t addr)2308 static int cdns_i3c_i2c_api_transfer(const struct device *dev, struct i2c_msg *msgs,
2309 				     uint8_t num_msgs, uint16_t addr)
2310 {
2311 	struct i3c_i2c_device_desc *i2c_dev = cdns_i3c_i2c_device_find(dev, addr);
2312 	int ret;
2313 
2314 	if (i2c_dev == NULL) {
2315 		ret = -ENODEV;
2316 	} else {
2317 		ret = cdns_i3c_i2c_transfer(dev, i2c_dev, msgs, num_msgs);
2318 	}
2319 
2320 	return ret;
2321 }
2322 
2323 /**
2324  * Determine I3C bus mode from the i2c devices on the bus
2325  *
2326  * Reads the LVR of all I2C devices and returns the I3C bus
2327  * Mode
2328  *
2329  * @param dev_list Pointer to device list
2330  *
2331  * @return @see enum i3c_bus_mode.
2332  */
i3c_bus_mode(const struct i3c_dev_list * dev_list)2333 static enum i3c_bus_mode i3c_bus_mode(const struct i3c_dev_list *dev_list)
2334 {
2335 	enum i3c_bus_mode mode = I3C_BUS_MODE_PURE;
2336 
2337 	for (int i = 0; i < dev_list->num_i2c; i++) {
2338 		switch (I3C_LVR_I2C_DEV_IDX(dev_list->i2c[i].lvr)) {
2339 		case I3C_LVR_I2C_DEV_IDX_0:
2340 			if (mode < I3C_BUS_MODE_MIXED_FAST) {
2341 				mode = I3C_BUS_MODE_MIXED_FAST;
2342 			}
2343 			break;
2344 		case I3C_LVR_I2C_DEV_IDX_1:
2345 			if (mode < I3C_BUS_MODE_MIXED_LIMITED) {
2346 				mode = I3C_BUS_MODE_MIXED_LIMITED;
2347 			}
2348 			break;
2349 		case I3C_LVR_I2C_DEV_IDX_2:
2350 			if (mode < I3C_BUS_MODE_MIXED_SLOW) {
2351 				mode = I3C_BUS_MODE_MIXED_SLOW;
2352 			}
2353 			break;
2354 		default:
2355 			mode = I3C_BUS_MODE_INVALID;
2356 			break;
2357 		}
2358 	}
2359 	return mode;
2360 }
2361 
2362 /**
2363  * Determine THD_DEL value for CTRL register
2364  *
2365  * @param dev Pointer to device driver instance.
2366  *
2367  * @return Value to be written to THD_DEL
2368  */
cdns_i3c_clk_to_data_turnaround(const struct device * dev)2369 static uint8_t cdns_i3c_clk_to_data_turnaround(const struct device *dev)
2370 {
2371 	const struct cdns_i3c_config *config = dev->config;
2372 	uint32_t input_clock_frequency = config->input_frequency;
2373 	uint8_t thd_delay =
2374 		DIV_ROUND_UP(I3C_TSCO_DEFAULT_NS, (NSEC_PER_SEC / input_clock_frequency));
2375 
2376 	if (thd_delay > THD_DELAY_MAX) {
2377 		thd_delay = THD_DELAY_MAX;
2378 	}
2379 
2380 	return (THD_DELAY_MAX - thd_delay);
2381 }
2382 
2383 /**
2384  * @brief Initialize the hardware.
2385  *
2386  * @param dev Pointer to controller device driver instance.
2387  */
cdns_i3c_bus_init(const struct device * dev)2388 static int cdns_i3c_bus_init(const struct device *dev)
2389 {
2390 	struct cdns_i3c_data *data = dev->data;
2391 	const struct cdns_i3c_config *config = dev->config;
2392 	struct i3c_config_controller *ctrl_config = &data->common.ctrl_config;
2393 
2394 	/* Clear all retaining regs */
2395 	sys_write32(DEVS_CTRL_DEV_CLR_ALL, config->base + DEVS_CTRL);
2396 
2397 	uint32_t conf0 = sys_read32(config->base + CONF_STATUS0);
2398 
2399 	data->max_devs = CONF_STATUS0_DEVS_NUM(conf0);
2400 	data->free_rr_slots = GENMASK(data->max_devs, 1);
2401 	ctrl_config->is_secondary = (conf0 & CONF_STATUS0_SEC_MASTER) ? true : false;
2402 	ctrl_config->supported_hdr = (conf0 & CONF_STATUS0_SUPPORTS_DDR) ? I3C_MSG_HDR_DDR : 0;
2403 
2404 	k_mutex_init(&data->bus_lock);
2405 	k_sem_init(&data->xfer.complete, 0, 1);
2406 	k_sem_init(&data->ibi_hj_complete, 0, 1);
2407 
2408 	cdns_i3c_interrupts_disable(config);
2409 	cdns_i3c_interrupts_clear(config);
2410 
2411 	config->irq_config_func(dev);
2412 
2413 	/* Ensure the bus is disabled. */
2414 	sys_write32(~CTRL_DEV_EN & sys_read32(config->base + CTRL), config->base + CTRL);
2415 
2416 	cdns_i3c_read_hw_cfg(dev);
2417 
2418 	/* determine prescaler timings for i3c and i2c scl */
2419 	cdns_i3c_set_prescalers(dev);
2420 
2421 	enum i3c_bus_mode mode = i3c_bus_mode(&config->common.dev_list);
2422 
2423 	LOG_DBG("%s: i3c bus mode %d", dev->name, mode);
2424 	int cdns_mode;
2425 
2426 	switch (mode) {
2427 	case I3C_BUS_MODE_PURE:
2428 		cdns_mode = CTRL_PURE_BUS_MODE;
2429 		break;
2430 	case I3C_BUS_MODE_MIXED_FAST:
2431 		cdns_mode = CTRL_MIXED_FAST_BUS_MODE;
2432 		break;
2433 	case I3C_BUS_MODE_MIXED_LIMITED:
2434 	case I3C_BUS_MODE_MIXED_SLOW:
2435 		cdns_mode = CTRL_MIXED_SLOW_BUS_MODE;
2436 		break;
2437 	default:
2438 		return -EINVAL;
2439 	}
2440 
2441 	/*
2442 	 * When a Hot-Join request happens, disable all events coming from this device.
2443 	 * We will issue ENTDAA afterwards from the threaded IRQ handler.
2444 	 * Set HJ ACK later after bus init to prevent targets from indirect DAA enforcement.
2445 	 *
2446 	 * Set the I3C Bus Mode based on the LVR of the I2C devices
2447 	 */
2448 	uint32_t ctrl = CTRL_HJ_DISEC | CTRL_MCS_EN | (CTRL_BUS_MODE_MASK & cdns_mode) |
2449 			CTRL_THD_DELAY(cdns_i3c_clk_to_data_turnaround(dev));
2450 	/* Disable Controllership requests as it is not supported yet by the driver */
2451 	ctrl &= ~CTRL_MST_ACK;
2452 
2453 	/*
2454 	 * Cadence I3C release r105v1p0 and above support I3C v1.1 timing change
2455 	 * for tCASHr_min = tCAS_min / 2, otherwise tCASr_min = tCAS_min (as
2456 	 * per MIPI spec v1.0)
2457 	 */
2458 	uint32_t rev_id = sys_read32(config->base + REV_ID);
2459 
2460 	if (REV_ID_REV(rev_id) >= REV_ID_VERSION(1, 5)) {
2461 		ctrl |= CTRL_I3C_11_SUPP;
2462 	}
2463 
2464 	/* write ctrl register value */
2465 	sys_write32(ctrl, config->base + CTRL);
2466 
2467 	/* enable Core */
2468 	sys_write32(CTRL_DEV_EN | ctrl, config->base + CTRL);
2469 
2470 	/* Set fifo thresholds. */
2471 	sys_write32(CMD_THR(I3C_CMDD_THR) | IBI_THR(I3C_IBID_THR) | CMDR_THR(I3C_CMDR_THR) |
2472 			    IBIR_THR(I3C_IBIR_THR),
2473 		    config->base + CMD_IBI_THR_CTRL);
2474 
2475 	/* Set TX/RX interrupt thresholds. */
2476 	if (sys_read32(config->base + MST_STATUS0) & MST_STATUS0_MASTER_MODE) {
2477 		sys_write32(TX_THR(I3C_TX_THR) | RX_THR(data->hw_cfg.rx_mem_depth),
2478 			    config->base + TX_RX_THR_CTRL);
2479 	} else {
2480 		sys_write32(TX_THR(1) | RX_THR(1), config->base + TX_RX_THR_CTRL);
2481 	}
2482 
2483 	/* enable target interrupts */
2484 	sys_write32(SLV_INT_DA_UPD | SLV_INT_SDR_RD_COMP | SLV_INT_SDR_WR_COMP |
2485 			    SLV_INT_SDR_RX_THR | SLV_INT_SDR_TX_THR | SLV_INT_SDR_RX_UNF |
2486 			    SLV_INT_SDR_TX_OVF | SLV_INT_HJ_DONE,
2487 		    config->base + SLV_IER);
2488 
2489 	/* Enable IBI interrupts. */
2490 	sys_write32(MST_INT_IBIR_THR | MST_INT_RX_UNF | MST_INT_HALTED | MST_INT_TX_OVF,
2491 		    config->base + MST_IER);
2492 
2493 	int ret = i3c_addr_slots_init(dev);
2494 
2495 	if (ret != 0) {
2496 		return ret;
2497 	}
2498 
2499 	/* Program retaining regs. */
2500 	cdns_i3c_program_controller_retaining_reg(dev);
2501 
2502 	/* only primary controllers are responsible for initializing the bus */
2503 	if (!ctrl_config->is_secondary) {
2504 		/* Perform bus initialization */
2505 		ret = i3c_bus_init(dev, &config->common.dev_list);
2506 #ifdef CONFIG_I3C_USE_IBI
2507 		/* Bus Initialization Complete, allow HJ ACKs */
2508 		sys_write32(CTRL_HJ_ACK | sys_read32(config->base + CTRL), config->base + CTRL);
2509 #endif
2510 	}
2511 
2512 	return 0;
2513 }
2514 
2515 static struct i3c_driver_api api = {
2516 	.i2c_api.configure = cdns_i3c_i2c_api_configure,
2517 	.i2c_api.transfer = cdns_i3c_i2c_api_transfer,
2518 
2519 	.configure = cdns_i3c_configure,
2520 	.config_get = cdns_i3c_config_get,
2521 
2522 	.attach_i3c_device = cdns_i3c_attach_device,
2523 	.reattach_i3c_device = cdns_i3c_reattach_device,
2524 	.detach_i3c_device = cdns_i3c_detach_device,
2525 	.attach_i2c_device = cdns_i3c_i2c_attach_device,
2526 	.detach_i2c_device = cdns_i3c_i2c_detach_device,
2527 
2528 	.do_daa = cdns_i3c_do_daa,
2529 	.do_ccc = cdns_i3c_do_ccc,
2530 
2531 	.i3c_device_find = cdns_i3c_device_find,
2532 
2533 	.i3c_xfers = cdns_i3c_transfer,
2534 
2535 	.target_tx_write = cdns_i3c_target_tx_write,
2536 	.target_register = cdns_i3c_target_register,
2537 	.target_unregister = cdns_i3c_target_unregister,
2538 
2539 #ifdef CONFIG_I3C_USE_IBI
2540 	.ibi_enable = cdns_i3c_controller_ibi_enable,
2541 	.ibi_disable = cdns_i3c_controller_ibi_disable,
2542 	.ibi_raise = cdns_i3c_target_ibi_raise,
2543 #endif
2544 };
2545 
2546 #define CADENCE_I3C_INSTANTIATE(n)                                                                 \
2547 	static void cdns_i3c_config_func_##n(const struct device *dev);                            \
2548 	static struct i3c_device_desc cdns_i3c_device_array_##n[] = I3C_DEVICE_ARRAY_DT_INST(n);   \
2549 	static struct i3c_i2c_device_desc cdns_i3c_i2c_device_array_##n[] =                        \
2550 		I3C_I2C_DEVICE_ARRAY_DT_INST(n);                                                   \
2551 	static const struct cdns_i3c_config i3c_config_##n = {                                     \
2552 		.base = DT_INST_REG_ADDR(n),                                                       \
2553 		.input_frequency = DT_INST_PROP(n, input_clock_frequency),                         \
2554 		.irq_config_func = cdns_i3c_config_func_##n,                                       \
2555 		.common.dev_list.i3c = cdns_i3c_device_array_##n,                                  \
2556 		.common.dev_list.num_i3c = ARRAY_SIZE(cdns_i3c_device_array_##n),                  \
2557 		.common.dev_list.i2c = cdns_i3c_i2c_device_array_##n,                              \
2558 		.common.dev_list.num_i2c = ARRAY_SIZE(cdns_i3c_i2c_device_array_##n),              \
2559 	};                                                                                         \
2560 	static struct cdns_i3c_data i3c_data_##n = {                                               \
2561 		.common.ctrl_config.scl.i3c = DT_INST_PROP_OR(n, i3c_scl_hz, 0),                   \
2562 		.common.ctrl_config.scl.i2c = DT_INST_PROP_OR(n, i2c_scl_hz, 0),                   \
2563 	};                                                                                         \
2564 	DEVICE_DT_INST_DEFINE(n, cdns_i3c_bus_init, NULL, &i3c_data_##n, &i3c_config_##n,          \
2565 			      POST_KERNEL, CONFIG_I3C_CONTROLLER_INIT_PRIORITY, &api);             \
2566 	static void cdns_i3c_config_func_##n(const struct device *dev)                             \
2567 	{                                                                                          \
2568 		IRQ_CONNECT(DT_INST_IRQN(n), DT_INST_IRQ(n, priority), cdns_i3c_irq_handler,       \
2569 			    DEVICE_DT_INST_GET(n), 0);                                             \
2570 		irq_enable(DT_INST_IRQN(n));                                                       \
2571 	};
2572 
2573 #define DT_DRV_COMPAT cdns_i3c
2574 DT_INST_FOREACH_STATUS_OKAY(CADENCE_I3C_INSTANTIATE)
2575