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