1 /*
2  * Copyright (c) 2020 Abram Early
3  * Copyright (c) 2023 Andriy Gelman
4  *
5  * SPDX-License-Identifier: Apache-2.0
6  */
7 
8 #ifndef ZEPHYR_DRIVERS_CAN_MICROCHIP_MCP251XFD_H_
9 #define ZEPHYR_DRIVERS_CAN_MICROCHIP_MCP251XFD_H_
10 
11 #include <stdint.h>
12 
13 #include <zephyr/drivers/can.h>
14 #include <zephyr/drivers/gpio.h>
15 #include <zephyr/drivers/spi.h>
16 
17 #define MCP251XFD_UINT32_FLAG_TO_BYTE_MASK(flag_u32)			\
18 	((flag_u32) >> ROUND_DOWN(LOG2((flag_u32)), 8))
19 
20 #define MCP251XFD_RAM_START_ADDR 0x400
21 #define MCP251XFD_RAM_SIZE       2048
22 #define MCP251XFD_RAM_ALIGNMENT  4
23 #define MCP251XFD_PAYLOAD_SIZE   CAN_MAX_DLEN
24 
25 #define MCP251XFD_FIFO_TYPE_TEF 0
26 #define MCP251XFD_FIFO_TYPE_RX  1
27 
28 #define MCP251XFD_TEF_FIFO_ITEM_SIZE 8
29 #define MCP251XFD_TX_QUEUE_ITEM_SIZE (8 + MCP251XFD_PAYLOAD_SIZE)
30 
31 #if defined(CONFIG_CAN_RX_TIMESTAMP)
32 #define MCP251XFD_RX_FIFO_ITEM_SIZE  (4 + 8 + MCP251XFD_PAYLOAD_SIZE)
33 #else
34 #define MCP251XFD_RX_FIFO_ITEM_SIZE  (8 + MCP251XFD_PAYLOAD_SIZE)
35 #endif
36 
37 #define MCP251XFD_TEF_FIFO_START_ADDR 0
38 #define MCP251XFD_TEF_FIFO_ITEMS      CONFIG_CAN_MCP251XFD_MAX_TX_QUEUE
39 #define MCP251XFD_TEF_FIFO_SIZE       (MCP251XFD_TEF_FIFO_ITEMS * MCP251XFD_TEF_FIFO_ITEM_SIZE)
40 
41 #define MCP251XFD_TX_QUEUE_START_ADDR MCP251XFD_TEF_FIFO_SIZE
42 #define MCP251XFD_TX_QUEUE_ITEMS      CONFIG_CAN_MCP251XFD_MAX_TX_QUEUE
43 #define MCP251XFD_TX_QUEUE_SIZE       (MCP251XFD_TX_QUEUE_ITEMS * MCP251XFD_TX_QUEUE_ITEM_SIZE)
44 
45 #define MCP251XFD_RX_FIFO_START_ADDR (MCP251XFD_TX_QUEUE_START_ADDR + MCP251XFD_TX_QUEUE_SIZE)
46 #define MCP251XFD_RX_FIFO_SIZE_MAX   (MCP251XFD_RAM_SIZE - MCP251XFD_RX_FIFO_START_ADDR)
47 #define MCP251XFD_RX_FIFO_ITEMS_MAX  (MCP251XFD_RX_FIFO_SIZE_MAX / MCP251XFD_RX_FIFO_ITEM_SIZE)
48 
49 #define MCP251XFD_RX_FIFO_ITEMS CONFIG_CAN_MCP251XFD_RX_FIFO_ITEMS
50 #define MCP251XFD_RX_FIFO_SIZE  (MCP251XFD_RX_FIFO_ITEMS * MCP251XFD_RX_FIFO_ITEM_SIZE)
51 
52 #define MCP251XFD_RX_FIFO_IDX 1
53 #define MCP251XFD_REG_SIZE    4
54 
55 #define MCP251XFD_CRC_POLY 0x8005
56 #define MCP251XFD_CRC_SEED 0xffff
57 
58 BUILD_ASSERT(MCP251XFD_TEF_FIFO_SIZE + MCP251XFD_TX_QUEUE_SIZE +
59 	     MCP251XFD_RX_FIFO_SIZE <= MCP251XFD_RAM_SIZE,
60 	     "Cannot fit FIFOs into RAM");
61 
62 /* Timeout for changing mode */
63 #define MCP251XFD_MODE_CHANGE_TIMEOUT_USEC  200000U
64 #define MCP251XFD_MODE_CHANGE_RETRIES       100
65 
66 #define MCP251XFD_PLLRDY_TIMEOUT_USEC 100000
67 #define MCP251XFD_PLLRDY_RETRIES      100
68 
69 #define MCP251XFD_MAX_INT_HANDLER_CALLS  10
70 #define MCP251XFD_INT_HANDLER_SLEEP_USEC 10000
71 
72 /* Delay time found experimentally to fix occasional init issue */
73 #define MCP251XFD_RESET_DELAY_USEC 5000
74 
75 struct mcp251xfd_mailbox {
76 	can_tx_callback_t cb;
77 	void *cb_arg;
78 };
79 
80 #define MCP251XFD_SPI_CMD_LEN       2
81 #define MCP251XFD_SPI_LEN_FIELD_LEN 1
82 #define MCP251XFD_SPI_CRC_LEN       2
83 
84 /* MPC251x registers - mostly copied from Linux kernel implementation of driver */
85 
86 /* CAN FD Controller Module SFR */
87 #define MCP251XFD_REG_CON                   0x00
88 #define MCP251XFD_REG_CON_TXBWS_MASK        GENMASK(31, 28)
89 #define MCP251XFD_REG_CON_ABAT              BIT(27)
90 #define MCP251XFD_REG_CON_REQOP_MASK        GENMASK(26, 24)
91 #define MCP251XFD_REG_CON_MODE_MIXED        0
92 #define MCP251XFD_REG_CON_MODE_SLEEP        1
93 #define MCP251XFD_REG_CON_MODE_INT_LOOPBACK 2
94 #define MCP251XFD_REG_CON_MODE_LISTENONLY   3
95 #define MCP251XFD_REG_CON_MODE_CONFIG       4
96 #define MCP251XFD_REG_CON_MODE_EXT_LOOPBACK 5
97 #define MCP251XFD_REG_CON_MODE_CAN2_0       6
98 #define MCP251XFD_REG_CON_MODE_RESTRICTED   7
99 #define MCP251XFD_REG_CON_OPMOD_MASK        GENMASK(23, 21)
100 #define MCP251XFD_REG_CON_TXQEN             BIT(20)
101 #define MCP251XFD_REG_CON_STEF              BIT(19)
102 #define MCP251XFD_REG_CON_SERR2LOM          BIT(18)
103 #define MCP251XFD_REG_CON_ESIGM             BIT(17)
104 #define MCP251XFD_REG_CON_RTXAT             BIT(16)
105 #define MCP251XFD_REG_CON_BRSDIS            BIT(12)
106 #define MCP251XFD_REG_CON_BUSY              BIT(11)
107 #define MCP251XFD_REG_CON_WFT_MASK          GENMASK(10, 9)
108 #define MCP251XFD_REG_CON_WFT_T00FILTER     0x0
109 #define MCP251XFD_REG_CON_WFT_T01FILTER     0x1
110 #define MCP251XFD_REG_CON_WFT_T10FILTER     0x2
111 #define MCP251XFD_REG_CON_WFT_T11FILTER     0x3
112 #define MCP251XFD_REG_CON_WAKFIL            BIT(8)
113 #define MCP251XFD_REG_CON_PXEDIS            BIT(6)
114 #define MCP251XFD_REG_CON_ISOCRCEN          BIT(5)
115 #define MCP251XFD_REG_CON_DNCNT_MASK        GENMASK(4, 0)
116 
117 #define MCP251XFD_REG_CON_B2                (MCP251XFD_REG_CON + 2)
118 #define MCP251XFD_REG_CON_B3                (MCP251XFD_REG_CON + 3)
119 
120 #define MCP251XFD_REG_NBTCFG                0x04
121 #define MCP251XFD_REG_NBTCFG_BRP_MASK       GENMASK(31, 24)
122 #define MCP251XFD_REG_NBTCFG_TSEG1_MASK     GENMASK(23, 16)
123 #define MCP251XFD_REG_NBTCFG_TSEG2_MASK     GENMASK(14, 8)
124 #define MCP251XFD_REG_NBTCFG_SJW_MASK       GENMASK(6, 0)
125 
126 #define MCP251XFD_REG_DBTCFG                0x08
127 #define MCP251XFD_REG_DBTCFG_BRP_MASK       GENMASK(31, 24)
128 #define MCP251XFD_REG_DBTCFG_TSEG1_MASK     GENMASK(20, 16)
129 #define MCP251XFD_REG_DBTCFG_TSEG2_MASK     GENMASK(11, 8)
130 #define MCP251XFD_REG_DBTCFG_SJW_MASK       GENMASK(3, 0)
131 
132 #define MCP251XFD_REG_TDC                   0x0c
133 #define MCP251XFD_REG_TDC_EDGFLTEN          BIT(25)
134 #define MCP251XFD_REG_TDC_SID11EN           BIT(24)
135 #define MCP251XFD_REG_TDC_TDCMOD_MASK       GENMASK(17, 16)
136 #define MCP251XFD_REG_TDC_TDCMOD_AUTO       2
137 #define MCP251XFD_REG_TDC_TDCMOD_MANUAL     1
138 #define MCP251XFD_REG_TDC_TDCMOD_DISABLED   0
139 #define MCP251XFD_REG_TDC_TDCO_MASK         GENMASK(14, 8)
140 #define MCP251XFD_REG_TDC_TDCV_MASK         GENMASK(5, 0)
141 #define MCP251XFD_REG_TDC_TDCO_MIN -64
142 #define MCP251XFD_REG_TDC_TDCO_MAX 63
143 
144 #define MCP251XFD_REG_TBC                   0x10
145 
146 #define MCP251XFD_REG_TSCON                 0x14
147 #define MCP251XFD_REG_TSCON_TSRES           BIT(18)
148 #define MCP251XFD_REG_TSCON_TSEOF           BIT(17)
149 #define MCP251XFD_REG_TSCON_TBCEN           BIT(16)
150 #define MCP251XFD_REG_TSCON_TBCPRE_MASK     GENMASK(9, 0)
151 
152 #define MCP251XFD_REG_VEC                   0x18
153 #define MCP251XFD_REG_VEC_RXCODE_MASK       GENMASK(30, 24)
154 #define MCP251XFD_REG_VEC_TXCODE_MASK       GENMASK(22, 16)
155 #define MCP251XFD_REG_VEC_FILHIT_MASK       GENMASK(12, 8)
156 #define MCP251XFD_REG_VEC_ICODE_MASK        GENMASK(6, 0)
157 
158 #define MCP251XFD_REG_INT                   0x1c
159 #define MCP251XFD_REG_INT_IF_MASK           GENMASK(15, 0)
160 #define MCP251XFD_REG_INT_IE_MASK           GENMASK(31, 16)
161 #define MCP251XFD_REG_INT_IVMIE             BIT(31)
162 #define MCP251XFD_REG_INT_WAKIE             BIT(30)
163 #define MCP251XFD_REG_INT_CERRIE            BIT(29)
164 #define MCP251XFD_REG_INT_SERRIE            BIT(28)
165 #define MCP251XFD_REG_INT_RXOVIE            BIT(27)
166 #define MCP251XFD_REG_INT_TXATIE            BIT(26)
167 #define MCP251XFD_REG_INT_SPICRCIE          BIT(25)
168 #define MCP251XFD_REG_INT_ECCIE             BIT(24)
169 #define MCP251XFD_REG_INT_TEFIE             BIT(20)
170 #define MCP251XFD_REG_INT_MODIE             BIT(19)
171 #define MCP251XFD_REG_INT_TBCIE             BIT(18)
172 #define MCP251XFD_REG_INT_RXIE              BIT(17)
173 #define MCP251XFD_REG_INT_TXIE              BIT(16)
174 #define MCP251XFD_REG_INT_IVMIF             BIT(15)
175 #define MCP251XFD_REG_INT_WAKIF             BIT(14)
176 #define MCP251XFD_REG_INT_CERRIF            BIT(13)
177 #define MCP251XFD_REG_INT_SERRIF            BIT(12)
178 #define MCP251XFD_REG_INT_RXOVIF            BIT(11)
179 #define MCP251XFD_REG_INT_TXATIF            BIT(10)
180 #define MCP251XFD_REG_INT_SPICRCIF          BIT(9)
181 #define MCP251XFD_REG_INT_ECCIF             BIT(8)
182 #define MCP251XFD_REG_INT_TEFIF             BIT(4)
183 #define MCP251XFD_REG_INT_MODIF             BIT(3)
184 #define MCP251XFD_REG_INT_TBCIF             BIT(2)
185 #define MCP251XFD_REG_INT_RXIF              BIT(1)
186 #define MCP251XFD_REG_INT_TXIF              BIT(0)
187 
188 /* These IRQ flags must be cleared by SW in the CAN_INT register */
189 #define MCP251XFD_REG_INT_IF_CLEARABLE_MASK                                                        \
190 	(MCP251XFD_REG_INT_IVMIF | MCP251XFD_REG_INT_WAKIF | MCP251XFD_REG_INT_CERRIF |            \
191 	 MCP251XFD_REG_INT_SERRIF | MCP251XFD_REG_INT_MODIF)
192 
193 #define MCP251XFD_REG_RXIF                  0x20
194 #define MCP251XFD_REG_TXIF                  0x24
195 #define MCP251XFD_REG_RXOVIF                0x28
196 #define MCP251XFD_REG_TXATIF                0x2c
197 #define MCP251XFD_REG_TXREQ                 0x30
198 
199 #define MCP251XFD_REG_TREC                  0x34
200 #define MCP251XFD_REG_TREC_TXBO             BIT(21)
201 #define MCP251XFD_REG_TREC_TXBP             BIT(20)
202 #define MCP251XFD_REG_TREC_RXBP             BIT(19)
203 #define MCP251XFD_REG_TREC_TXWARN           BIT(18)
204 #define MCP251XFD_REG_TREC_RXWARN           BIT(17)
205 #define MCP251XFD_REG_TREC_EWARN            BIT(16)
206 #define MCP251XFD_REG_TREC_TEC_MASK         GENMASK(15, 8)
207 #define MCP251XFD_REG_TREC_REC_MASK         GENMASK(7, 0)
208 
209 #define MCP251XFD_REG_BDIAG0                0x38
210 #define MCP251XFD_REG_BDIAG0_DTERRCNT_MASK  GENMASK(31, 24)
211 #define MCP251XFD_REG_BDIAG0_DRERRCNT_MASK  GENMASK(23, 16)
212 #define MCP251XFD_REG_BDIAG0_NTERRCNT_MASK  GENMASK(15, 8)
213 #define MCP251XFD_REG_BDIAG0_NRERRCNT_MASK  GENMASK(7, 0)
214 
215 #define MCP251XFD_REG_BDIAG1                0x3c
216 #define MCP251XFD_REG_BDIAG1_DLCMM          BIT(31)
217 #define MCP251XFD_REG_BDIAG1_ESI            BIT(30)
218 #define MCP251XFD_REG_BDIAG1_DCRCERR        BIT(29)
219 #define MCP251XFD_REG_BDIAG1_DSTUFERR       BIT(28)
220 #define MCP251XFD_REG_BDIAG1_DFORMERR       BIT(27)
221 #define MCP251XFD_REG_BDIAG1_DBIT1ERR       BIT(25)
222 #define MCP251XFD_REG_BDIAG1_DBIT0ERR       BIT(24)
223 #define MCP251XFD_REG_BDIAG1_TXBOERR        BIT(23)
224 #define MCP251XFD_REG_BDIAG1_NCRCERR        BIT(21)
225 #define MCP251XFD_REG_BDIAG1_NSTUFERR       BIT(20)
226 #define MCP251XFD_REG_BDIAG1_NFORMERR       BIT(19)
227 #define MCP251XFD_REG_BDIAG1_NACKERR        BIT(18)
228 #define MCP251XFD_REG_BDIAG1_NBIT1ERR       BIT(17)
229 #define MCP251XFD_REG_BDIAG1_NBIT0ERR       BIT(16)
230 #define MCP251XFD_REG_BDIAG1_BERR_MASK                                                             \
231 	(MCP251XFD_REG_BDIAG1_DLCMM | MCP251XFD_REG_BDIAG1_ESI | MCP251XFD_REG_BDIAG1_DCRCERR |    \
232 	 MCP251XFD_REG_BDIAG1_DSTUFERR | MCP251XFD_REG_BDIAG1_DFORMERR |                           \
233 	 MCP251XFD_REG_BDIAG1_DBIT1ERR | MCP251XFD_REG_BDIAG1_DBIT0ERR |                           \
234 	 MCP251XFD_REG_BDIAG1_TXBOERR | MCP251XFD_REG_BDIAG1_NCRCERR |                             \
235 	 MCP251XFD_REG_BDIAG1_NSTUFERR | MCP251XFD_REG_BDIAG1_NFORMERR |                           \
236 	 MCP251XFD_REG_BDIAG1_NACKERR | MCP251XFD_REG_BDIAG1_NBIT1ERR |                            \
237 	 MCP251XFD_REG_BDIAG1_NBIT0ERR)
238 #define MCP251XFD_REG_BDIAG1_EFMSGCNT_MASK GENMASK(15, 0)
239 
240 #define MCP251XFD_REG_TEFCON               0x40
241 #define MCP251XFD_REG_TEFCON_FSIZE_MASK    GENMASK(28, 24)
242 #define MCP251XFD_REG_TEFCON_FRESET        BIT(10)
243 #define MCP251XFD_REG_TEFCON_UINC          BIT(8)
244 #define MCP251XFD_REG_TEFCON_TEFTSEN       BIT(5)
245 #define MCP251XFD_REG_TEFCON_TEFOVIE       BIT(3)
246 #define MCP251XFD_REG_TEFCON_TEFFIE        BIT(2)
247 #define MCP251XFD_REG_TEFCON_TEFHIE        BIT(1)
248 #define MCP251XFD_REG_TEFCON_TEFNEIE       BIT(0)
249 
250 #define MCP251XFD_REG_TEFSTA               0x44
251 #define MCP251XFD_REG_TEFSTA_TEFOVIF       BIT(3)
252 #define MCP251XFD_REG_TEFSTA_TEFFIF        BIT(2)
253 #define MCP251XFD_REG_TEFSTA_TEFHIF        BIT(1)
254 #define MCP251XFD_REG_TEFSTA_TEFNEIF       BIT(0)
255 
256 #define MCP251XFD_REG_TEFUA                  0x48
257 
258 #define MCP251XFD_REG_TXQCON                 0x50
259 #define MCP251XFD_REG_TXQCON_PLSIZE_MASK     GENMASK(31, 29)
260 #define MCP251XFD_REG_TXQCON_PLSIZE_8        0
261 #define MCP251XFD_REG_TXQCON_PLSIZE_12       1
262 #define MCP251XFD_REG_TXQCON_PLSIZE_16       2
263 #define MCP251XFD_REG_TXQCON_PLSIZE_20       3
264 #define MCP251XFD_REG_TXQCON_PLSIZE_24       4
265 #define MCP251XFD_REG_TXQCON_PLSIZE_32       5
266 #define MCP251XFD_REG_TXQCON_PLSIZE_48       6
267 #define MCP251XFD_REG_TXQCON_PLSIZE_64       7
268 #define MCP251XFD_REG_TXQCON_FSIZE_MASK      GENMASK(28, 24)
269 #define MCP251XFD_REG_TXQCON_TXAT_UNLIMITED  3
270 #define MCP251XFD_REG_TXQCON_TXAT_THREE_SHOT 1
271 #define MCP251XFD_REG_TXQCON_TXAT_ONE_SHOT   0
272 #define MCP251XFD_REG_TXQCON_TXAT_MASK       GENMASK(22, 21)
273 #define MCP251XFD_REG_TXQCON_TXPRI_MASK      GENMASK(20, 16)
274 #define MCP251XFD_REG_TXQCON_FRESET          BIT(10)
275 #define MCP251XFD_REG_TXQCON_TXREQ           BIT(9)
276 #define MCP251XFD_REG_TXQCON_UINC            BIT(8)
277 #define MCP251XFD_REG_TXQCON_TXEN            BIT(7)
278 #define MCP251XFD_REG_TXQCON_TXATIE          BIT(4)
279 #define MCP251XFD_REG_TXQCON_TXQEIE          BIT(2)
280 #define MCP251XFD_REG_TXQCON_TXQNIE          BIT(0)
281 
282 #define MCP251XFD_REG_TXQSTA                 0x54
283 #define MCP251XFD_REG_TXQSTA_TXQCI_MASK      GENMASK(12, 8)
284 #define MCP251XFD_REG_TXQSTA_TXABT           BIT(7)
285 #define MCP251XFD_REG_TXQSTA_TXLARB          BIT(6)
286 #define MCP251XFD_REG_TXQSTA_TXERR           BIT(5)
287 #define MCP251XFD_REG_TXQSTA_TXATIF          BIT(4)
288 #define MCP251XFD_REG_TXQSTA_TXQEIF          BIT(2)
289 #define MCP251XFD_REG_TXQSTA_TXQNIF          BIT(0)
290 
291 #define MCP251XFD_REG_TXQUA                   0x58
292 
293 #define MCP251XFD_REG_FIFOCON(x)              (0x50 + 0xc * (x))
294 #define MCP251XFD_REG_FIFOCON_PLSIZE_MASK     GENMASK(31, 29)
295 #define MCP251XFD_REG_FIFOCON_PLSIZE_8        0
296 #define MCP251XFD_REG_FIFOCON_PLSIZE_12       1
297 #define MCP251XFD_REG_FIFOCON_PLSIZE_16       2
298 #define MCP251XFD_REG_FIFOCON_PLSIZE_20       3
299 #define MCP251XFD_REG_FIFOCON_PLSIZE_24       4
300 #define MCP251XFD_REG_FIFOCON_PLSIZE_32       5
301 #define MCP251XFD_REG_FIFOCON_PLSIZE_48       6
302 #define MCP251XFD_REG_FIFOCON_PLSIZE_64       7
303 #define MCP251XFD_REG_FIFOCON_FSIZE_MASK      GENMASK(28, 24)
304 #define MCP251XFD_REG_FIFOCON_TXAT_MASK       GENMASK(22, 21)
305 #define MCP251XFD_REG_FIFOCON_TXAT_ONE_SHOT   0
306 #define MCP251XFD_REG_FIFOCON_TXAT_THREE_SHOT 1
307 #define MCP251XFD_REG_FIFOCON_TXAT_UNLIMITED  3
308 #define MCP251XFD_REG_FIFOCON_TXPRI_MASK      GENMASK(20, 16)
309 #define MCP251XFD_REG_FIFOCON_FRESET          BIT(10)
310 #define MCP251XFD_REG_FIFOCON_TXREQ           BIT(9)
311 #define MCP251XFD_REG_FIFOCON_UINC            BIT(8)
312 #define MCP251XFD_REG_FIFOCON_TXEN            BIT(7)
313 #define MCP251XFD_REG_FIFOCON_RTREN           BIT(6)
314 #define MCP251XFD_REG_FIFOCON_RXTSEN          BIT(5)
315 #define MCP251XFD_REG_FIFOCON_TXATIE          BIT(4)
316 #define MCP251XFD_REG_FIFOCON_RXOVIE          BIT(3)
317 #define MCP251XFD_REG_FIFOCON_TFERFFIE        BIT(2)
318 #define MCP251XFD_REG_FIFOCON_TFHRFHIE        BIT(1)
319 #define MCP251XFD_REG_FIFOCON_TFNRFNIE        BIT(0)
320 
321 #define MCP251XFD_REG_FIFOSTA(x)              (0x54 + 0xc * (x))
322 #define MCP251XFD_REG_FIFOSTA_FIFOCI_MASK     GENMASK(12, 8)
323 #define MCP251XFD_REG_FIFOSTA_TXABT           BIT(7)
324 #define MCP251XFD_REG_FIFOSTA_TXLARB          BIT(6)
325 #define MCP251XFD_REG_FIFOSTA_TXERR           BIT(5)
326 #define MCP251XFD_REG_FIFOSTA_TXATIF          BIT(4)
327 #define MCP251XFD_REG_FIFOSTA_RXOVIF          BIT(3)
328 #define MCP251XFD_REG_FIFOSTA_TFERFFIF        BIT(2)
329 #define MCP251XFD_REG_FIFOSTA_TFHRFHIF        BIT(1)
330 #define MCP251XFD_REG_FIFOSTA_TFNRFNIF        BIT(0)
331 
332 #define MCP251XFD_REG_FIFOUA(x)               (0x58 + 0xc * (x))
333 
334 #define MCP251XFD_REG_BYTE_FLTCON(m)	      (0x1d0 + m)
335 #define MCP251XFD_REG_BYTE_FLTCON_FBP_MASK    GENMASK(4, 0)
336 #define MCP251XFD_REG_BYTE_FLTCON_FLTEN       BIT(7)
337 
338 #define MCP251XFD_REG_FLTOBJ(x)               (0x1f0 + 0x8 * (x))
339 #define MCP251XFD_REG_FLTOBJ_EXIDE            BIT(30)
340 #define MCP251XFD_REG_FLTOBJ_SID11            BIT(29)
341 #define MCP251XFD_REG_FLTOBJ_EID_MASK         GENMASK(28, 11)
342 #define MCP251XFD_REG_FLTOBJ_SID_MASK         GENMASK(10, 0)
343 
344 #define MCP251XFD_REG_FLTMASK(x)              (0x1f4 + 0x8 * (x))
345 #define MCP251XFD_REG_MASK_MIDE               BIT(30)
346 #define MCP251XFD_REG_MASK_MSID11             BIT(29)
347 #define MCP251XFD_REG_MASK_MEID_MASK          GENMASK(28, 11)
348 #define MCP251XFD_REG_MASK_MSID_MASK          GENMASK(10, 0)
349 
350 /* Message Object */
351 #define MCP251XFD_OBJ_ID_SID11                 BIT(29)
352 #define MCP251XFD_OBJ_ID_EID_MASK              GENMASK(28, 11)
353 #define MCP251XFD_OBJ_ID_SID_MASK              GENMASK(10, 0)
354 #define MCP251XFD_OBJ_FLAGS_SEQ_MCP2518FD_MASK GENMASK(31, 9)
355 #define MCP251XFD_OBJ_FLAGS_SEQ_MCP2517FD_MASK GENMASK(15, 9)
356 #define MCP251XFD_OBJ_FLAGS_SEQ_MASK           MCP251XFD_OBJ_FLAGS_SEQ_MCP2518FD_MASK
357 #define MCP251XFD_OBJ_FLAGS_ESI                BIT(8)
358 #define MCP251XFD_OBJ_FLAGS_FDF                BIT(7)
359 #define MCP251XFD_OBJ_FLAGS_BRS                BIT(6)
360 #define MCP251XFD_OBJ_FLAGS_RTR                BIT(5)
361 #define MCP251XFD_OBJ_FLAGS_IDE                BIT(4)
362 #define MCP251XFD_OBJ_FLAGS_DLC_MASK           GENMASK(3, 0)
363 #define MCP251XFD_OBJ_FILHIT_MASK	       GENMASK(15, 11)
364 
365 #define MCP251XFD_OBJ_DATA_OFFSET	       2 /* offset to the data in sizeof(uint32_t) */
366 #define MCP251XFD_OBJ_HEADER_SIZE	       (MCP251XFD_OBJ_DATA_OFFSET * MCP251XFD_REG_SIZE)
367 
368 #define MCP251XFD_REG_FRAME_EFF_SID_MASK       GENMASK(28, 18)
369 #define MCP251XFD_REG_FRAME_EFF_EID_MASK       GENMASK(17, 0)
370 
371 /* MCP2517/18FD SFR */
372 #define MCP251XFD_REG_OSC                      0xe00
373 #define MCP251XFD_REG_OSC_SCLKRDY              BIT(12)
374 #define MCP251XFD_REG_OSC_OSCRDY               BIT(10)
375 #define MCP251XFD_REG_OSC_PLLRDY               BIT(8)
376 #define MCP251XFD_REG_OSC_CLKODIV_10           3
377 #define MCP251XFD_REG_OSC_CLKODIV_4            2
378 #define MCP251XFD_REG_OSC_CLKODIV_2            1
379 #define MCP251XFD_REG_OSC_CLKODIV_1            0
380 #define MCP251XFD_REG_OSC_CLKODIV_MASK         GENMASK(6, 5)
381 #define MCP251XFD_REG_OSC_SCLKDIV              BIT(4)
382 #define MCP251XFD_REG_OSC_LPMEN                BIT(3) /* MCP2518FD only */
383 #define MCP251XFD_REG_OSC_OSCDIS               BIT(2)
384 #define MCP251XFD_REG_OSC_PLLEN                BIT(0)
385 
386 #define MCP251XFD_REG_IOCON                    0xe04
387 #define MCP251XFD_REG_IOCON_INTOD              BIT(30)
388 #define MCP251XFD_REG_IOCON_SOF                BIT(29)
389 #define MCP251XFD_REG_IOCON_TXCANOD            BIT(28)
390 #define MCP251XFD_REG_IOCON_PM1                BIT(25)
391 #define MCP251XFD_REG_IOCON_PM0                BIT(24)
392 #define MCP251XFD_REG_IOCON_GPIO1              BIT(17)
393 #define MCP251XFD_REG_IOCON_GPIO0              BIT(16)
394 #define MCP251XFD_REG_IOCON_LAT1               BIT(9)
395 #define MCP251XFD_REG_IOCON_LAT0               BIT(8)
396 #define MCP251XFD_REG_IOCON_XSTBYEN            BIT(6)
397 #define MCP251XFD_REG_IOCON_TRIS1              BIT(1)
398 #define MCP251XFD_REG_IOCON_TRIS0              BIT(0)
399 
400 #define MCP251XFD_REG_CRC                      0xe08
401 #define MCP251XFD_REG_CRC_FERRIE               BIT(25)
402 #define MCP251XFD_REG_CRC_CRCERRIE             BIT(24)
403 #define MCP251XFD_REG_CRC_FERRIF               BIT(17)
404 #define MCP251XFD_REG_CRC_CRCERRIF             BIT(16)
405 #define MCP251XFD_REG_CRC_IF_MASK              GENMASK(17, 16)
406 #define MCP251XFD_REG_CRC_MASK                 GENMASK(15, 0)
407 
408 #define MCP251XFD_REG_ECCCON                   0xe0c
409 #define MCP251XFD_REG_ECCCON_PARITY_MASK       GENMASK(14, 8)
410 #define MCP251XFD_REG_ECCCON_DEDIE             BIT(2)
411 #define MCP251XFD_REG_ECCCON_SECIE             BIT(1)
412 #define MCP251XFD_REG_ECCCON_ECCEN             BIT(0)
413 
414 #define MCP251XFD_REG_ECCSTAT                  0xe10
415 #define MCP251XFD_REG_ECCSTAT_ERRADDR_MASK     GENMASK(27, 16)
416 #define MCP251XFD_REG_ECCSTAT_IF_MASK          GENMASK(2, 1)
417 #define MCP251XFD_REG_ECCSTAT_DEDIF            BIT(2)
418 #define MCP251XFD_REG_ECCSTAT_SECIF            BIT(1)
419 
420 #define MCP251XFD_REG_DEVID                    0xe14 /* MCP2518FD only */
421 #define MCP251XFD_REG_DEVID_ID_MASK            GENMASK(7, 4)
422 #define MCP251XFD_REG_DEVID_REV_MASK           GENMASK(3, 0)
423 
424 /* SPI commands */
425 #define MCP251XFD_SPI_INSTRUCTION_RESET          0x0000
426 #define MCP251XFD_SPI_INSTRUCTION_WRITE          0x2000
427 #define MCP251XFD_SPI_INSTRUCTION_READ           0x3000
428 #define MCP251XFD_SPI_INSTRUCTION_WRITE_CRC      0xa000
429 #define MCP251XFD_SPI_INSTRUCTION_READ_CRC       0xb000
430 #define MCP251XFD_SPI_INSTRUCTION_WRITE_CRC_SAFE 0xc000
431 #define MCP251XFD_SPI_ADDRESS_MASK               GENMASK(11, 0)
432 
433 #define MCP251XFD_REG_FIFOCON_TO_STA(addr) (addr + 0x4)
434 
435 #define MCP251XFD_REG_FLTCON(m) (0x1d0 + m)
436 
437 struct mcp251xfd_txobj {
438 	uint32_t id;
439 	uint32_t flags;
440 	uint8_t data[CAN_MAX_DLEN];
441 } __packed;
442 
443 struct mcp251xfd_rxobj {
444 	uint32_t id;
445 	uint32_t flags;
446 #if defined(CONFIG_CAN_RX_TIMESTAMP)
447 	uint32_t timestamp;
448 #endif
449 	uint8_t data[CAN_MAX_DLEN];
450 } __packed;
451 
452 struct mcp251xfd_tefobj {
453 	uint32_t id;
454 	uint32_t flags;
455 } __packed;
456 
457 #define MCP251XFD_MAX_READ_FIFO_BUF_SIZE                                                           \
458 	MAX((MCP251XFD_RX_FIFO_ITEM_SIZE * MCP251XFD_RX_FIFO_ITEMS),                               \
459 	    (MCP251XFD_TEF_FIFO_ITEM_SIZE * MCP251XFD_TEF_FIFO_ITEMS))
460 
461 #define MCP251XFD_MAX_READ_CRC_BUF_SIZE                                                            \
462 	(MCP251XFD_SPI_CRC_LEN + 2 * MCP251XFD_REG_SIZE)
463 
464 #define MCP251XFD_SPI_BUF_SIZE                                                                     \
465 	MAX(MCP251XFD_MAX_READ_FIFO_BUF_SIZE, MCP251XFD_MAX_READ_CRC_BUF_SIZE)
466 #define MCP251XFD_SPI_HEADER_LEN (MCP251XFD_SPI_CMD_LEN + MCP251XFD_SPI_LEN_FIELD_LEN)
467 
468 struct mcp251xfd_spi_data {
469 	uint8_t _unused[4 - (MCP251XFD_SPI_HEADER_LEN % 4)]; /* so that buf is 4-byte aligned */
470 	uint8_t header[MCP251XFD_SPI_HEADER_LEN]; /* contains spi_cmd and length field (if used) */
471 	uint8_t buf[MCP251XFD_SPI_BUF_SIZE];
472 } __packed __aligned(4);
473 
474 struct mcp251xfd_fifo {
475 	uint32_t ram_start_addr;
476 	uint16_t reg_fifocon_addr;
477 	uint8_t capacity;
478 	uint8_t item_size;
479 	void (*msg_handler)(const struct device *dev, void *data);
480 };
481 
482 struct mcp251xfd_data {
483 	struct can_driver_data common;
484 
485 	/* Interrupt Data */
486 	struct gpio_callback int_gpio_cb;
487 	struct k_thread int_thread;
488 	k_thread_stack_t *int_thread_stack;
489 	struct k_sem int_sem;
490 
491 	/* General */
492 	enum can_state state;
493 	struct k_mutex mutex;
494 
495 	/* TX Callback */
496 	struct k_sem tx_sem;
497 	uint32_t mailbox_usage;
498 	struct mcp251xfd_mailbox mailbox[CONFIG_CAN_MCP251XFD_MAX_TX_QUEUE];
499 
500 	/* Filter Data */
501 	uint32_t filter_usage;
502 	struct can_filter filter[CONFIG_CAN_MAX_FILTER];
503 	can_rx_callback_t rx_cb[CONFIG_CAN_MAX_FILTER];
504 	void *cb_arg[CONFIG_CAN_MAX_FILTER];
505 
506 	const struct device *dev;
507 
508 	uint8_t next_mcp251xfd_mode;
509 	uint8_t current_mcp251xfd_mode;
510 	int tdco;
511 
512 	struct mcp251xfd_spi_data spi_data;
513 
514 };
515 
516 struct mcp251xfd_config {
517 	const struct can_driver_config common;
518 
519 	/* spi configuration */
520 	struct spi_dt_spec bus;
521 	struct gpio_dt_spec int_gpio_dt;
522 
523 	uint32_t osc_freq;
524 
525 	/* IO Config */
526 	bool sof_on_clko;
527 	bool pll_enable;
528 	uint8_t clko_div;
529 
530 	uint16_t timestamp_prescaler;
531 
532 	const struct device *clk_dev;
533 	uint8_t clk_id;
534 
535 	struct mcp251xfd_fifo rx_fifo;
536 	struct mcp251xfd_fifo tef_fifo;
537 };
538 
539 #endif /* ZEPHYR_DRIVERS_CAN_MICROCHIP_MCP251XFD_H_ */
540