xref: /linux/drivers/net/can/spi/mcp251xfd/mcp251xfd.h (revision ec8a42e7343234802b9054874fe01810880289ce)
1 /* SPDX-License-Identifier: GPL-2.0
2  *
3  * mcp251xfd - Microchip MCP251xFD Family CAN controller driver
4  *
5  * Copyright (c) 2019 Pengutronix,
6  *                    Marc Kleine-Budde <kernel@pengutronix.de>
7  * Copyright (c) 2019 Martin Sperl <kernel@martin.sperl.org>
8  */
9 
10 #ifndef _MCP251XFD_H
11 #define _MCP251XFD_H
12 
13 #include <linux/can/core.h>
14 #include <linux/can/dev.h>
15 #include <linux/can/rx-offload.h>
16 #include <linux/gpio/consumer.h>
17 #include <linux/kernel.h>
18 #include <linux/regmap.h>
19 #include <linux/regulator/consumer.h>
20 #include <linux/spi/spi.h>
21 
22 /* MPC251x registers */
23 
24 /* CAN FD Controller Module SFR */
25 #define MCP251XFD_REG_CON 0x00
26 #define MCP251XFD_REG_CON_TXBWS_MASK GENMASK(31, 28)
27 #define MCP251XFD_REG_CON_ABAT BIT(27)
28 #define MCP251XFD_REG_CON_REQOP_MASK GENMASK(26, 24)
29 #define MCP251XFD_REG_CON_MODE_MIXED 0
30 #define MCP251XFD_REG_CON_MODE_SLEEP 1
31 #define MCP251XFD_REG_CON_MODE_INT_LOOPBACK 2
32 #define MCP251XFD_REG_CON_MODE_LISTENONLY 3
33 #define MCP251XFD_REG_CON_MODE_CONFIG 4
34 #define MCP251XFD_REG_CON_MODE_EXT_LOOPBACK 5
35 #define MCP251XFD_REG_CON_MODE_CAN2_0 6
36 #define MCP251XFD_REG_CON_MODE_RESTRICTED 7
37 #define MCP251XFD_REG_CON_OPMOD_MASK GENMASK(23, 21)
38 #define MCP251XFD_REG_CON_TXQEN BIT(20)
39 #define MCP251XFD_REG_CON_STEF BIT(19)
40 #define MCP251XFD_REG_CON_SERR2LOM BIT(18)
41 #define MCP251XFD_REG_CON_ESIGM BIT(17)
42 #define MCP251XFD_REG_CON_RTXAT BIT(16)
43 #define MCP251XFD_REG_CON_BRSDIS BIT(12)
44 #define MCP251XFD_REG_CON_BUSY BIT(11)
45 #define MCP251XFD_REG_CON_WFT_MASK GENMASK(10, 9)
46 #define MCP251XFD_REG_CON_WFT_T00FILTER 0x0
47 #define MCP251XFD_REG_CON_WFT_T01FILTER 0x1
48 #define MCP251XFD_REG_CON_WFT_T10FILTER 0x2
49 #define MCP251XFD_REG_CON_WFT_T11FILTER 0x3
50 #define MCP251XFD_REG_CON_WAKFIL BIT(8)
51 #define MCP251XFD_REG_CON_PXEDIS BIT(6)
52 #define MCP251XFD_REG_CON_ISOCRCEN BIT(5)
53 #define MCP251XFD_REG_CON_DNCNT_MASK GENMASK(4, 0)
54 
55 #define MCP251XFD_REG_NBTCFG 0x04
56 #define MCP251XFD_REG_NBTCFG_BRP_MASK GENMASK(31, 24)
57 #define MCP251XFD_REG_NBTCFG_TSEG1_MASK GENMASK(23, 16)
58 #define MCP251XFD_REG_NBTCFG_TSEG2_MASK GENMASK(14, 8)
59 #define MCP251XFD_REG_NBTCFG_SJW_MASK GENMASK(6, 0)
60 
61 #define MCP251XFD_REG_DBTCFG 0x08
62 #define MCP251XFD_REG_DBTCFG_BRP_MASK GENMASK(31, 24)
63 #define MCP251XFD_REG_DBTCFG_TSEG1_MASK GENMASK(20, 16)
64 #define MCP251XFD_REG_DBTCFG_TSEG2_MASK GENMASK(11, 8)
65 #define MCP251XFD_REG_DBTCFG_SJW_MASK GENMASK(3, 0)
66 
67 #define MCP251XFD_REG_TDC 0x0c
68 #define MCP251XFD_REG_TDC_EDGFLTEN BIT(25)
69 #define MCP251XFD_REG_TDC_SID11EN BIT(24)
70 #define MCP251XFD_REG_TDC_TDCMOD_MASK GENMASK(17, 16)
71 #define MCP251XFD_REG_TDC_TDCMOD_AUTO 2
72 #define MCP251XFD_REG_TDC_TDCMOD_MANUAL 1
73 #define MCP251XFD_REG_TDC_TDCMOD_DISABLED 0
74 #define MCP251XFD_REG_TDC_TDCO_MASK GENMASK(14, 8)
75 #define MCP251XFD_REG_TDC_TDCV_MASK GENMASK(5, 0)
76 
77 #define MCP251XFD_REG_TBC 0x10
78 
79 #define MCP251XFD_REG_TSCON 0x14
80 #define MCP251XFD_REG_TSCON_TSRES BIT(18)
81 #define MCP251XFD_REG_TSCON_TSEOF BIT(17)
82 #define MCP251XFD_REG_TSCON_TBCEN BIT(16)
83 #define MCP251XFD_REG_TSCON_TBCPRE_MASK GENMASK(9, 0)
84 
85 #define MCP251XFD_REG_VEC 0x18
86 #define MCP251XFD_REG_VEC_RXCODE_MASK GENMASK(30, 24)
87 #define MCP251XFD_REG_VEC_TXCODE_MASK GENMASK(22, 16)
88 #define MCP251XFD_REG_VEC_FILHIT_MASK GENMASK(12, 8)
89 #define MCP251XFD_REG_VEC_ICODE_MASK GENMASK(6, 0)
90 
91 #define MCP251XFD_REG_INT 0x1c
92 #define MCP251XFD_REG_INT_IF_MASK GENMASK(15, 0)
93 #define MCP251XFD_REG_INT_IE_MASK GENMASK(31, 16)
94 #define MCP251XFD_REG_INT_IVMIE BIT(31)
95 #define MCP251XFD_REG_INT_WAKIE BIT(30)
96 #define MCP251XFD_REG_INT_CERRIE BIT(29)
97 #define MCP251XFD_REG_INT_SERRIE BIT(28)
98 #define MCP251XFD_REG_INT_RXOVIE BIT(27)
99 #define MCP251XFD_REG_INT_TXATIE BIT(26)
100 #define MCP251XFD_REG_INT_SPICRCIE BIT(25)
101 #define MCP251XFD_REG_INT_ECCIE BIT(24)
102 #define MCP251XFD_REG_INT_TEFIE BIT(20)
103 #define MCP251XFD_REG_INT_MODIE BIT(19)
104 #define MCP251XFD_REG_INT_TBCIE BIT(18)
105 #define MCP251XFD_REG_INT_RXIE BIT(17)
106 #define MCP251XFD_REG_INT_TXIE BIT(16)
107 #define MCP251XFD_REG_INT_IVMIF BIT(15)
108 #define MCP251XFD_REG_INT_WAKIF BIT(14)
109 #define MCP251XFD_REG_INT_CERRIF BIT(13)
110 #define MCP251XFD_REG_INT_SERRIF BIT(12)
111 #define MCP251XFD_REG_INT_RXOVIF BIT(11)
112 #define MCP251XFD_REG_INT_TXATIF BIT(10)
113 #define MCP251XFD_REG_INT_SPICRCIF BIT(9)
114 #define MCP251XFD_REG_INT_ECCIF BIT(8)
115 #define MCP251XFD_REG_INT_TEFIF BIT(4)
116 #define MCP251XFD_REG_INT_MODIF BIT(3)
117 #define MCP251XFD_REG_INT_TBCIF BIT(2)
118 #define MCP251XFD_REG_INT_RXIF BIT(1)
119 #define MCP251XFD_REG_INT_TXIF BIT(0)
120 /* These IRQ flags must be cleared by SW in the CAN_INT register */
121 #define MCP251XFD_REG_INT_IF_CLEARABLE_MASK \
122 	(MCP251XFD_REG_INT_IVMIF | MCP251XFD_REG_INT_WAKIF | \
123 	 MCP251XFD_REG_INT_CERRIF |  MCP251XFD_REG_INT_SERRIF | \
124 	 MCP251XFD_REG_INT_MODIF)
125 
126 #define MCP251XFD_REG_RXIF 0x20
127 #define MCP251XFD_REG_TXIF 0x24
128 #define MCP251XFD_REG_RXOVIF 0x28
129 #define MCP251XFD_REG_TXATIF 0x2c
130 #define MCP251XFD_REG_TXREQ 0x30
131 
132 #define MCP251XFD_REG_TREC 0x34
133 #define MCP251XFD_REG_TREC_TXBO BIT(21)
134 #define MCP251XFD_REG_TREC_TXBP BIT(20)
135 #define MCP251XFD_REG_TREC_RXBP BIT(19)
136 #define MCP251XFD_REG_TREC_TXWARN BIT(18)
137 #define MCP251XFD_REG_TREC_RXWARN BIT(17)
138 #define MCP251XFD_REG_TREC_EWARN BIT(16)
139 #define MCP251XFD_REG_TREC_TEC_MASK GENMASK(15, 8)
140 #define MCP251XFD_REG_TREC_REC_MASK GENMASK(7, 0)
141 
142 #define MCP251XFD_REG_BDIAG0 0x38
143 #define MCP251XFD_REG_BDIAG0_DTERRCNT_MASK GENMASK(31, 24)
144 #define MCP251XFD_REG_BDIAG0_DRERRCNT_MASK GENMASK(23, 16)
145 #define MCP251XFD_REG_BDIAG0_NTERRCNT_MASK GENMASK(15, 8)
146 #define MCP251XFD_REG_BDIAG0_NRERRCNT_MASK GENMASK(7, 0)
147 
148 #define MCP251XFD_REG_BDIAG1 0x3c
149 #define MCP251XFD_REG_BDIAG1_DLCMM BIT(31)
150 #define MCP251XFD_REG_BDIAG1_ESI BIT(30)
151 #define MCP251XFD_REG_BDIAG1_DCRCERR BIT(29)
152 #define MCP251XFD_REG_BDIAG1_DSTUFERR BIT(28)
153 #define MCP251XFD_REG_BDIAG1_DFORMERR BIT(27)
154 #define MCP251XFD_REG_BDIAG1_DBIT1ERR BIT(25)
155 #define MCP251XFD_REG_BDIAG1_DBIT0ERR BIT(24)
156 #define MCP251XFD_REG_BDIAG1_TXBOERR BIT(23)
157 #define MCP251XFD_REG_BDIAG1_NCRCERR BIT(21)
158 #define MCP251XFD_REG_BDIAG1_NSTUFERR BIT(20)
159 #define MCP251XFD_REG_BDIAG1_NFORMERR BIT(19)
160 #define MCP251XFD_REG_BDIAG1_NACKERR BIT(18)
161 #define MCP251XFD_REG_BDIAG1_NBIT1ERR BIT(17)
162 #define MCP251XFD_REG_BDIAG1_NBIT0ERR BIT(16)
163 #define MCP251XFD_REG_BDIAG1_BERR_MASK \
164 	(MCP251XFD_REG_BDIAG1_DLCMM | MCP251XFD_REG_BDIAG1_ESI | \
165 	 MCP251XFD_REG_BDIAG1_DCRCERR | MCP251XFD_REG_BDIAG1_DSTUFERR | \
166 	 MCP251XFD_REG_BDIAG1_DFORMERR | MCP251XFD_REG_BDIAG1_DBIT1ERR | \
167 	 MCP251XFD_REG_BDIAG1_DBIT0ERR | MCP251XFD_REG_BDIAG1_TXBOERR | \
168 	 MCP251XFD_REG_BDIAG1_NCRCERR | MCP251XFD_REG_BDIAG1_NSTUFERR | \
169 	 MCP251XFD_REG_BDIAG1_NFORMERR | MCP251XFD_REG_BDIAG1_NACKERR | \
170 	 MCP251XFD_REG_BDIAG1_NBIT1ERR | MCP251XFD_REG_BDIAG1_NBIT0ERR)
171 #define MCP251XFD_REG_BDIAG1_EFMSGCNT_MASK GENMASK(15, 0)
172 
173 #define MCP251XFD_REG_TEFCON 0x40
174 #define MCP251XFD_REG_TEFCON_FSIZE_MASK GENMASK(28, 24)
175 #define MCP251XFD_REG_TEFCON_FRESET BIT(10)
176 #define MCP251XFD_REG_TEFCON_UINC BIT(8)
177 #define MCP251XFD_REG_TEFCON_TEFTSEN BIT(5)
178 #define MCP251XFD_REG_TEFCON_TEFOVIE BIT(3)
179 #define MCP251XFD_REG_TEFCON_TEFFIE BIT(2)
180 #define MCP251XFD_REG_TEFCON_TEFHIE BIT(1)
181 #define MCP251XFD_REG_TEFCON_TEFNEIE BIT(0)
182 
183 #define MCP251XFD_REG_TEFSTA 0x44
184 #define MCP251XFD_REG_TEFSTA_TEFOVIF BIT(3)
185 #define MCP251XFD_REG_TEFSTA_TEFFIF BIT(2)
186 #define MCP251XFD_REG_TEFSTA_TEFHIF BIT(1)
187 #define MCP251XFD_REG_TEFSTA_TEFNEIF BIT(0)
188 
189 #define MCP251XFD_REG_TEFUA 0x48
190 
191 #define MCP251XFD_REG_TXQCON 0x50
192 #define MCP251XFD_REG_TXQCON_PLSIZE_MASK GENMASK(31, 29)
193 #define MCP251XFD_REG_TXQCON_PLSIZE_8 0
194 #define MCP251XFD_REG_TXQCON_PLSIZE_12 1
195 #define MCP251XFD_REG_TXQCON_PLSIZE_16 2
196 #define MCP251XFD_REG_TXQCON_PLSIZE_20 3
197 #define MCP251XFD_REG_TXQCON_PLSIZE_24 4
198 #define MCP251XFD_REG_TXQCON_PLSIZE_32 5
199 #define MCP251XFD_REG_TXQCON_PLSIZE_48 6
200 #define MCP251XFD_REG_TXQCON_PLSIZE_64 7
201 #define MCP251XFD_REG_TXQCON_FSIZE_MASK GENMASK(28, 24)
202 #define MCP251XFD_REG_TXQCON_TXAT_UNLIMITED 3
203 #define MCP251XFD_REG_TXQCON_TXAT_THREE_SHOT 1
204 #define MCP251XFD_REG_TXQCON_TXAT_ONE_SHOT 0
205 #define MCP251XFD_REG_TXQCON_TXAT_MASK GENMASK(22, 21)
206 #define MCP251XFD_REG_TXQCON_TXPRI_MASK GENMASK(20, 16)
207 #define MCP251XFD_REG_TXQCON_FRESET BIT(10)
208 #define MCP251XFD_REG_TXQCON_TXREQ BIT(9)
209 #define MCP251XFD_REG_TXQCON_UINC BIT(8)
210 #define MCP251XFD_REG_TXQCON_TXEN BIT(7)
211 #define MCP251XFD_REG_TXQCON_TXATIE BIT(4)
212 #define MCP251XFD_REG_TXQCON_TXQEIE BIT(2)
213 #define MCP251XFD_REG_TXQCON_TXQNIE BIT(0)
214 
215 #define MCP251XFD_REG_TXQSTA 0x54
216 #define MCP251XFD_REG_TXQSTA_TXQCI_MASK GENMASK(12, 8)
217 #define MCP251XFD_REG_TXQSTA_TXABT BIT(7)
218 #define MCP251XFD_REG_TXQSTA_TXLARB BIT(6)
219 #define MCP251XFD_REG_TXQSTA_TXERR BIT(5)
220 #define MCP251XFD_REG_TXQSTA_TXATIF BIT(4)
221 #define MCP251XFD_REG_TXQSTA_TXQEIF BIT(2)
222 #define MCP251XFD_REG_TXQSTA_TXQNIF BIT(0)
223 
224 #define MCP251XFD_REG_TXQUA 0x58
225 
226 #define MCP251XFD_REG_FIFOCON(x) (0x50 + 0xc * (x))
227 #define MCP251XFD_REG_FIFOCON_PLSIZE_MASK GENMASK(31, 29)
228 #define MCP251XFD_REG_FIFOCON_PLSIZE_8 0
229 #define MCP251XFD_REG_FIFOCON_PLSIZE_12 1
230 #define MCP251XFD_REG_FIFOCON_PLSIZE_16 2
231 #define MCP251XFD_REG_FIFOCON_PLSIZE_20 3
232 #define MCP251XFD_REG_FIFOCON_PLSIZE_24 4
233 #define MCP251XFD_REG_FIFOCON_PLSIZE_32 5
234 #define MCP251XFD_REG_FIFOCON_PLSIZE_48 6
235 #define MCP251XFD_REG_FIFOCON_PLSIZE_64 7
236 #define MCP251XFD_REG_FIFOCON_FSIZE_MASK GENMASK(28, 24)
237 #define MCP251XFD_REG_FIFOCON_TXAT_MASK GENMASK(22, 21)
238 #define MCP251XFD_REG_FIFOCON_TXAT_ONE_SHOT 0
239 #define MCP251XFD_REG_FIFOCON_TXAT_THREE_SHOT 1
240 #define MCP251XFD_REG_FIFOCON_TXAT_UNLIMITED 3
241 #define MCP251XFD_REG_FIFOCON_TXPRI_MASK GENMASK(20, 16)
242 #define MCP251XFD_REG_FIFOCON_FRESET BIT(10)
243 #define MCP251XFD_REG_FIFOCON_TXREQ BIT(9)
244 #define MCP251XFD_REG_FIFOCON_UINC BIT(8)
245 #define MCP251XFD_REG_FIFOCON_TXEN BIT(7)
246 #define MCP251XFD_REG_FIFOCON_RTREN BIT(6)
247 #define MCP251XFD_REG_FIFOCON_RXTSEN BIT(5)
248 #define MCP251XFD_REG_FIFOCON_TXATIE BIT(4)
249 #define MCP251XFD_REG_FIFOCON_RXOVIE BIT(3)
250 #define MCP251XFD_REG_FIFOCON_TFERFFIE BIT(2)
251 #define MCP251XFD_REG_FIFOCON_TFHRFHIE BIT(1)
252 #define MCP251XFD_REG_FIFOCON_TFNRFNIE BIT(0)
253 
254 #define MCP251XFD_REG_FIFOSTA(x) (0x54 + 0xc * (x))
255 #define MCP251XFD_REG_FIFOSTA_FIFOCI_MASK GENMASK(12, 8)
256 #define MCP251XFD_REG_FIFOSTA_TXABT BIT(7)
257 #define MCP251XFD_REG_FIFOSTA_TXLARB BIT(6)
258 #define MCP251XFD_REG_FIFOSTA_TXERR BIT(5)
259 #define MCP251XFD_REG_FIFOSTA_TXATIF BIT(4)
260 #define MCP251XFD_REG_FIFOSTA_RXOVIF BIT(3)
261 #define MCP251XFD_REG_FIFOSTA_TFERFFIF BIT(2)
262 #define MCP251XFD_REG_FIFOSTA_TFHRFHIF BIT(1)
263 #define MCP251XFD_REG_FIFOSTA_TFNRFNIF BIT(0)
264 
265 #define MCP251XFD_REG_FIFOUA(x) (0x58 + 0xc * (x))
266 
267 #define MCP251XFD_REG_FLTCON(x) (0x1d0 + 0x4 * (x))
268 #define MCP251XFD_REG_FLTCON_FLTEN3 BIT(31)
269 #define MCP251XFD_REG_FLTCON_F3BP_MASK GENMASK(28, 24)
270 #define MCP251XFD_REG_FLTCON_FLTEN2 BIT(23)
271 #define MCP251XFD_REG_FLTCON_F2BP_MASK GENMASK(20, 16)
272 #define MCP251XFD_REG_FLTCON_FLTEN1 BIT(15)
273 #define MCP251XFD_REG_FLTCON_F1BP_MASK GENMASK(12, 8)
274 #define MCP251XFD_REG_FLTCON_FLTEN0 BIT(7)
275 #define MCP251XFD_REG_FLTCON_F0BP_MASK GENMASK(4, 0)
276 #define MCP251XFD_REG_FLTCON_FLTEN(x) (BIT(7) << 8 * ((x) & 0x3))
277 #define MCP251XFD_REG_FLTCON_FLT_MASK(x) (GENMASK(7, 0) << (8 * ((x) & 0x3)))
278 #define MCP251XFD_REG_FLTCON_FBP(x, fifo) ((fifo) << 8 * ((x) & 0x3))
279 
280 #define MCP251XFD_REG_FLTOBJ(x) (0x1f0 + 0x8 * (x))
281 #define MCP251XFD_REG_FLTOBJ_EXIDE BIT(30)
282 #define MCP251XFD_REG_FLTOBJ_SID11 BIT(29)
283 #define MCP251XFD_REG_FLTOBJ_EID_MASK GENMASK(28, 11)
284 #define MCP251XFD_REG_FLTOBJ_SID_MASK GENMASK(10, 0)
285 
286 #define MCP251XFD_REG_FLTMASK(x) (0x1f4 + 0x8 * (x))
287 #define MCP251XFD_REG_MASK_MIDE BIT(30)
288 #define MCP251XFD_REG_MASK_MSID11 BIT(29)
289 #define MCP251XFD_REG_MASK_MEID_MASK GENMASK(28, 11)
290 #define MCP251XFD_REG_MASK_MSID_MASK GENMASK(10, 0)
291 
292 /* RAM */
293 #define MCP251XFD_RAM_START 0x400
294 #define MCP251XFD_RAM_SIZE SZ_2K
295 
296 /* Message Object */
297 #define MCP251XFD_OBJ_ID_SID11 BIT(29)
298 #define MCP251XFD_OBJ_ID_EID_MASK GENMASK(28, 11)
299 #define MCP251XFD_OBJ_ID_SID_MASK GENMASK(10, 0)
300 #define MCP251XFD_OBJ_FLAGS_SEQ_MCP2518FD_MASK GENMASK(31, 9)
301 #define MCP251XFD_OBJ_FLAGS_SEQ_MCP2517FD_MASK GENMASK(15, 9)
302 #define MCP251XFD_OBJ_FLAGS_SEQ_MASK MCP251XFD_OBJ_FLAGS_SEQ_MCP2518FD_MASK
303 #define MCP251XFD_OBJ_FLAGS_ESI BIT(8)
304 #define MCP251XFD_OBJ_FLAGS_FDF BIT(7)
305 #define MCP251XFD_OBJ_FLAGS_BRS BIT(6)
306 #define MCP251XFD_OBJ_FLAGS_RTR BIT(5)
307 #define MCP251XFD_OBJ_FLAGS_IDE BIT(4)
308 #define MCP251XFD_OBJ_FLAGS_DLC_MASK GENMASK(3, 0)
309 
310 #define MCP251XFD_REG_FRAME_EFF_SID_MASK GENMASK(28, 18)
311 #define MCP251XFD_REG_FRAME_EFF_EID_MASK GENMASK(17, 0)
312 
313 /* MCP2517/18FD SFR */
314 #define MCP251XFD_REG_OSC 0xe00
315 #define MCP251XFD_REG_OSC_SCLKRDY BIT(12)
316 #define MCP251XFD_REG_OSC_OSCRDY BIT(10)
317 #define MCP251XFD_REG_OSC_PLLRDY BIT(8)
318 #define MCP251XFD_REG_OSC_CLKODIV_10 3
319 #define MCP251XFD_REG_OSC_CLKODIV_4 2
320 #define MCP251XFD_REG_OSC_CLKODIV_2 1
321 #define MCP251XFD_REG_OSC_CLKODIV_1 0
322 #define MCP251XFD_REG_OSC_CLKODIV_MASK GENMASK(6, 5)
323 #define MCP251XFD_REG_OSC_SCLKDIV BIT(4)
324 #define MCP251XFD_REG_OSC_LPMEN BIT(3)	/* MCP2518FD only */
325 #define MCP251XFD_REG_OSC_OSCDIS BIT(2)
326 #define MCP251XFD_REG_OSC_PLLEN BIT(0)
327 
328 #define MCP251XFD_REG_IOCON 0xe04
329 #define MCP251XFD_REG_IOCON_INTOD BIT(30)
330 #define MCP251XFD_REG_IOCON_SOF BIT(29)
331 #define MCP251XFD_REG_IOCON_TXCANOD BIT(28)
332 #define MCP251XFD_REG_IOCON_PM1 BIT(25)
333 #define MCP251XFD_REG_IOCON_PM0 BIT(24)
334 #define MCP251XFD_REG_IOCON_GPIO1 BIT(17)
335 #define MCP251XFD_REG_IOCON_GPIO0 BIT(16)
336 #define MCP251XFD_REG_IOCON_LAT1 BIT(9)
337 #define MCP251XFD_REG_IOCON_LAT0 BIT(8)
338 #define MCP251XFD_REG_IOCON_XSTBYEN BIT(6)
339 #define MCP251XFD_REG_IOCON_TRIS1 BIT(1)
340 #define MCP251XFD_REG_IOCON_TRIS0 BIT(0)
341 
342 #define MCP251XFD_REG_CRC 0xe08
343 #define MCP251XFD_REG_CRC_FERRIE BIT(25)
344 #define MCP251XFD_REG_CRC_CRCERRIE BIT(24)
345 #define MCP251XFD_REG_CRC_FERRIF BIT(17)
346 #define MCP251XFD_REG_CRC_CRCERRIF BIT(16)
347 #define MCP251XFD_REG_CRC_IF_MASK GENMASK(17, 16)
348 #define MCP251XFD_REG_CRC_MASK GENMASK(15, 0)
349 
350 #define MCP251XFD_REG_ECCCON 0xe0c
351 #define MCP251XFD_REG_ECCCON_PARITY_MASK GENMASK(14, 8)
352 #define MCP251XFD_REG_ECCCON_DEDIE BIT(2)
353 #define MCP251XFD_REG_ECCCON_SECIE BIT(1)
354 #define MCP251XFD_REG_ECCCON_ECCEN BIT(0)
355 
356 #define MCP251XFD_REG_ECCSTAT 0xe10
357 #define MCP251XFD_REG_ECCSTAT_ERRADDR_MASK GENMASK(27, 16)
358 #define MCP251XFD_REG_ECCSTAT_IF_MASK GENMASK(2, 1)
359 #define MCP251XFD_REG_ECCSTAT_DEDIF BIT(2)
360 #define MCP251XFD_REG_ECCSTAT_SECIF BIT(1)
361 
362 #define MCP251XFD_REG_DEVID 0xe14	/* MCP2518FD only */
363 #define MCP251XFD_REG_DEVID_ID_MASK GENMASK(7, 4)
364 #define MCP251XFD_REG_DEVID_REV_MASK GENMASK(3, 0)
365 
366 /* number of TX FIFO objects, depending on CAN mode
367  *
368  * FIFO setup: tef: 8*12 bytes = 96 bytes, tx: 8*16 bytes = 128 bytes
369  * FIFO setup: tef: 4*12 bytes = 48 bytes, tx: 4*72 bytes = 288 bytes
370  */
371 #define MCP251XFD_RX_OBJ_NUM_MAX 32
372 #define MCP251XFD_TX_OBJ_NUM_CAN 8
373 #define MCP251XFD_TX_OBJ_NUM_CANFD 4
374 
375 #if MCP251XFD_TX_OBJ_NUM_CAN > MCP251XFD_TX_OBJ_NUM_CANFD
376 #define MCP251XFD_TX_OBJ_NUM_MAX MCP251XFD_TX_OBJ_NUM_CAN
377 #else
378 #define MCP251XFD_TX_OBJ_NUM_MAX MCP251XFD_TX_OBJ_NUM_CANFD
379 #endif
380 
381 #define MCP251XFD_NAPI_WEIGHT 32
382 #define MCP251XFD_TX_FIFO 1
383 #define MCP251XFD_RX_FIFO(x) (MCP251XFD_TX_FIFO + 1 + (x))
384 
385 /* SPI commands */
386 #define MCP251XFD_SPI_INSTRUCTION_RESET 0x0000
387 #define MCP251XFD_SPI_INSTRUCTION_WRITE 0x2000
388 #define MCP251XFD_SPI_INSTRUCTION_READ 0x3000
389 #define MCP251XFD_SPI_INSTRUCTION_WRITE_CRC 0xa000
390 #define MCP251XFD_SPI_INSTRUCTION_READ_CRC 0xb000
391 #define MCP251XFD_SPI_INSTRUCTION_WRITE_CRC_SAFE 0xc000
392 #define MCP251XFD_SPI_ADDRESS_MASK GENMASK(11, 0)
393 
394 #define MCP251XFD_SYSCLOCK_HZ_MAX 40000000
395 #define MCP251XFD_SYSCLOCK_HZ_MIN 1000000
396 #define MCP251XFD_SPICLOCK_HZ_MAX 20000000
397 #define MCP251XFD_OSC_PLL_MULTIPLIER 10
398 #define MCP251XFD_OSC_STAB_SLEEP_US (3 * USEC_PER_MSEC)
399 #define MCP251XFD_OSC_STAB_TIMEOUT_US (10 * MCP251XFD_OSC_STAB_SLEEP_US)
400 #define MCP251XFD_POLL_SLEEP_US (10)
401 #define MCP251XFD_POLL_TIMEOUT_US (USEC_PER_MSEC)
402 #define MCP251XFD_SOFTRESET_RETRIES_MAX 3
403 #define MCP251XFD_READ_CRC_RETRIES_MAX 3
404 #define MCP251XFD_ECC_CNT_MAX 2
405 #define MCP251XFD_SANITIZE_SPI 1
406 #define MCP251XFD_SANITIZE_CAN 1
407 
408 /* Silence TX MAB overflow warnings */
409 #define MCP251XFD_QUIRK_MAB_NO_WARN BIT(0)
410 /* Use CRC to access registers */
411 #define MCP251XFD_QUIRK_CRC_REG BIT(1)
412 /* Use CRC to access RX/TEF-RAM */
413 #define MCP251XFD_QUIRK_CRC_RX BIT(2)
414 /* Use CRC to access TX-RAM */
415 #define MCP251XFD_QUIRK_CRC_TX BIT(3)
416 /* Enable ECC for RAM */
417 #define MCP251XFD_QUIRK_ECC BIT(4)
418 /* Use Half Duplex SPI transfers */
419 #define MCP251XFD_QUIRK_HALF_DUPLEX BIT(5)
420 
421 struct mcp251xfd_hw_tef_obj {
422 	u32 id;
423 	u32 flags;
424 	u32 ts;
425 };
426 
427 /* The tx_obj_raw version is used in spi async, i.e. without
428  * regmap. We have to take care of endianness ourselves.
429  */
430 struct mcp251xfd_hw_tx_obj_raw {
431 	__le32 id;
432 	__le32 flags;
433 	u8 data[sizeof_field(struct canfd_frame, data)];
434 };
435 
436 struct mcp251xfd_hw_tx_obj_can {
437 	u32 id;
438 	u32 flags;
439 	u8 data[sizeof_field(struct can_frame, data)];
440 };
441 
442 struct mcp251xfd_hw_tx_obj_canfd {
443 	u32 id;
444 	u32 flags;
445 	u8 data[sizeof_field(struct canfd_frame, data)];
446 };
447 
448 struct mcp251xfd_hw_rx_obj_can {
449 	u32 id;
450 	u32 flags;
451 	u32 ts;
452 	u8 data[sizeof_field(struct can_frame, data)];
453 };
454 
455 struct mcp251xfd_hw_rx_obj_canfd {
456 	u32 id;
457 	u32 flags;
458 	u32 ts;
459 	u8 data[sizeof_field(struct canfd_frame, data)];
460 };
461 
462 struct __packed mcp251xfd_buf_cmd {
463 	__be16 cmd;
464 };
465 
466 struct __packed mcp251xfd_buf_cmd_crc {
467 	__be16 cmd;
468 	u8 len;
469 };
470 
471 union mcp251xfd_tx_obj_load_buf {
472 	struct __packed {
473 		struct mcp251xfd_buf_cmd cmd;
474 		struct mcp251xfd_hw_tx_obj_raw hw_tx_obj;
475 	} nocrc;
476 	struct __packed {
477 		struct mcp251xfd_buf_cmd_crc cmd;
478 		struct mcp251xfd_hw_tx_obj_raw hw_tx_obj;
479 		__be16 crc;
480 	} crc;
481 } ____cacheline_aligned;
482 
483 union mcp251xfd_write_reg_buf {
484 	struct __packed {
485 		struct mcp251xfd_buf_cmd cmd;
486 		u8 data[4];
487 	} nocrc;
488 	struct __packed {
489 		struct mcp251xfd_buf_cmd_crc cmd;
490 		u8 data[4];
491 		__be16 crc;
492 	} crc;
493 } ____cacheline_aligned;
494 
495 struct mcp251xfd_tx_obj {
496 	struct spi_message msg;
497 	struct spi_transfer xfer[2];
498 	union mcp251xfd_tx_obj_load_buf buf;
499 };
500 
501 struct mcp251xfd_tef_ring {
502 	unsigned int head;
503 	unsigned int tail;
504 
505 	/* u8 obj_num equals tx_ring->obj_num */
506 	/* u8 obj_size equals sizeof(struct mcp251xfd_hw_tef_obj) */
507 
508 	union mcp251xfd_write_reg_buf uinc_buf;
509 	struct spi_transfer uinc_xfer[MCP251XFD_TX_OBJ_NUM_MAX];
510 };
511 
512 struct mcp251xfd_tx_ring {
513 	unsigned int head;
514 	unsigned int tail;
515 
516 	u16 base;
517 	u8 obj_num;
518 	u8 obj_size;
519 
520 	struct mcp251xfd_tx_obj obj[MCP251XFD_TX_OBJ_NUM_MAX];
521 	union mcp251xfd_write_reg_buf rts_buf;
522 };
523 
524 struct mcp251xfd_rx_ring {
525 	unsigned int head;
526 	unsigned int tail;
527 
528 	u16 base;
529 	u8 nr;
530 	u8 fifo_nr;
531 	u8 obj_num;
532 	u8 obj_size;
533 
534 	union mcp251xfd_write_reg_buf uinc_buf;
535 	struct spi_transfer uinc_xfer[MCP251XFD_RX_OBJ_NUM_MAX];
536 	struct mcp251xfd_hw_rx_obj_canfd obj[];
537 };
538 
539 struct __packed mcp251xfd_map_buf_nocrc {
540 	struct mcp251xfd_buf_cmd cmd;
541 	u8 data[256];
542 } ____cacheline_aligned;
543 
544 struct __packed mcp251xfd_map_buf_crc {
545 	struct mcp251xfd_buf_cmd_crc cmd;
546 	u8 data[256 - 4];
547 	__be16 crc;
548 } ____cacheline_aligned;
549 
550 struct mcp251xfd_ecc {
551 	u32 ecc_stat;
552 	int cnt;
553 };
554 
555 struct mcp251xfd_regs_status {
556 	u32 intf;
557 };
558 
559 enum mcp251xfd_model {
560 	MCP251XFD_MODEL_MCP2517FD = 0x2517,
561 	MCP251XFD_MODEL_MCP2518FD = 0x2518,
562 	MCP251XFD_MODEL_MCP251XFD = 0xffff,	/* autodetect model */
563 };
564 
565 struct mcp251xfd_devtype_data {
566 	enum mcp251xfd_model model;
567 	u32 quirks;
568 };
569 
570 struct mcp251xfd_priv {
571 	struct can_priv can;
572 	struct can_rx_offload offload;
573 	struct net_device *ndev;
574 
575 	struct regmap *map_reg;			/* register access */
576 	struct regmap *map_rx;			/* RX/TEF RAM access */
577 
578 	struct regmap *map_nocrc;
579 	struct mcp251xfd_map_buf_nocrc *map_buf_nocrc_rx;
580 	struct mcp251xfd_map_buf_nocrc *map_buf_nocrc_tx;
581 
582 	struct regmap *map_crc;
583 	struct mcp251xfd_map_buf_crc *map_buf_crc_rx;
584 	struct mcp251xfd_map_buf_crc *map_buf_crc_tx;
585 
586 	struct spi_device *spi;
587 	u32 spi_max_speed_hz_orig;
588 
589 	struct mcp251xfd_tef_ring tef[1];
590 	struct mcp251xfd_tx_ring tx[1];
591 	struct mcp251xfd_rx_ring *rx[1];
592 
593 	u8 rx_ring_num;
594 
595 	struct mcp251xfd_ecc ecc;
596 	struct mcp251xfd_regs_status regs_status;
597 
598 	struct gpio_desc *rx_int;
599 	struct clk *clk;
600 	struct regulator *reg_vdd;
601 	struct regulator *reg_xceiver;
602 
603 	struct mcp251xfd_devtype_data devtype_data;
604 	struct can_berr_counter bec;
605 };
606 
607 #define MCP251XFD_IS(_model) \
608 static inline bool \
609 mcp251xfd_is_##_model(const struct mcp251xfd_priv *priv) \
610 { \
611 	return priv->devtype_data.model == MCP251XFD_MODEL_MCP##_model##FD; \
612 }
613 
614 MCP251XFD_IS(2517);
615 MCP251XFD_IS(2518);
616 MCP251XFD_IS(251X);
617 
618 static inline u8 mcp251xfd_first_byte_set(u32 mask)
619 {
620 	return (mask & 0x0000ffff) ?
621 		((mask & 0x000000ff) ? 0 : 1) :
622 		((mask & 0x00ff0000) ? 2 : 3);
623 }
624 
625 static inline u8 mcp251xfd_last_byte_set(u32 mask)
626 {
627 	return (mask & 0xffff0000) ?
628 		((mask & 0xff000000) ? 3 : 2) :
629 		((mask & 0x0000ff00) ? 1 : 0);
630 }
631 
632 static inline __be16 mcp251xfd_cmd_reset(void)
633 {
634 	return cpu_to_be16(MCP251XFD_SPI_INSTRUCTION_RESET);
635 }
636 
637 static inline void
638 mcp251xfd_spi_cmd_read_nocrc(struct mcp251xfd_buf_cmd *cmd, u16 addr)
639 {
640 	cmd->cmd = cpu_to_be16(MCP251XFD_SPI_INSTRUCTION_READ | addr);
641 }
642 
643 static inline void
644 mcp251xfd_spi_cmd_write_nocrc(struct mcp251xfd_buf_cmd *cmd, u16 addr)
645 {
646 	cmd->cmd = cpu_to_be16(MCP251XFD_SPI_INSTRUCTION_WRITE | addr);
647 }
648 
649 static inline bool mcp251xfd_reg_in_ram(unsigned int reg)
650 {
651 	static const struct regmap_range range =
652 		regmap_reg_range(MCP251XFD_RAM_START,
653 				 MCP251XFD_RAM_START + MCP251XFD_RAM_SIZE - 4);
654 
655 	return regmap_reg_in_range(reg, &range);
656 }
657 
658 static inline void
659 __mcp251xfd_spi_cmd_crc_set_len(struct mcp251xfd_buf_cmd_crc *cmd,
660 				u16 len, bool in_ram)
661 {
662 	/* Number of u32 for RAM access, number of u8 otherwise. */
663 	if (in_ram)
664 		cmd->len = len >> 2;
665 	else
666 		cmd->len = len;
667 }
668 
669 static inline void
670 mcp251xfd_spi_cmd_crc_set_len_in_ram(struct mcp251xfd_buf_cmd_crc *cmd, u16 len)
671 {
672 	__mcp251xfd_spi_cmd_crc_set_len(cmd, len, true);
673 }
674 
675 static inline void
676 mcp251xfd_spi_cmd_crc_set_len_in_reg(struct mcp251xfd_buf_cmd_crc *cmd, u16 len)
677 {
678 	__mcp251xfd_spi_cmd_crc_set_len(cmd, len, false);
679 }
680 
681 static inline void
682 mcp251xfd_spi_cmd_read_crc_set_addr(struct mcp251xfd_buf_cmd_crc *cmd, u16 addr)
683 {
684 	cmd->cmd = cpu_to_be16(MCP251XFD_SPI_INSTRUCTION_READ_CRC | addr);
685 }
686 
687 static inline void
688 mcp251xfd_spi_cmd_read_crc(struct mcp251xfd_buf_cmd_crc *cmd,
689 			   u16 addr, u16 len)
690 {
691 	mcp251xfd_spi_cmd_read_crc_set_addr(cmd, addr);
692 	__mcp251xfd_spi_cmd_crc_set_len(cmd, len, mcp251xfd_reg_in_ram(addr));
693 }
694 
695 static inline void
696 mcp251xfd_spi_cmd_write_crc_set_addr(struct mcp251xfd_buf_cmd_crc *cmd,
697 				     u16 addr)
698 {
699 	cmd->cmd = cpu_to_be16(MCP251XFD_SPI_INSTRUCTION_WRITE_CRC | addr);
700 }
701 
702 static inline void
703 mcp251xfd_spi_cmd_write_crc(struct mcp251xfd_buf_cmd_crc *cmd,
704 			    u16 addr, u16 len)
705 {
706 	mcp251xfd_spi_cmd_write_crc_set_addr(cmd, addr);
707 	__mcp251xfd_spi_cmd_crc_set_len(cmd, len, mcp251xfd_reg_in_ram(addr));
708 }
709 
710 static inline u8 *
711 mcp251xfd_spi_cmd_write(const struct mcp251xfd_priv *priv,
712 			union mcp251xfd_write_reg_buf *write_reg_buf,
713 			u16 addr)
714 {
715 	u8 *data;
716 
717 	if (priv->devtype_data.quirks & MCP251XFD_QUIRK_CRC_REG) {
718 		mcp251xfd_spi_cmd_write_crc_set_addr(&write_reg_buf->crc.cmd,
719 						     addr);
720 		data = write_reg_buf->crc.data;
721 	} else {
722 		mcp251xfd_spi_cmd_write_nocrc(&write_reg_buf->nocrc.cmd,
723 					      addr);
724 		data = write_reg_buf->nocrc.data;
725 	}
726 
727 	return data;
728 }
729 
730 static inline u16 mcp251xfd_get_tef_obj_addr(u8 n)
731 {
732 	return MCP251XFD_RAM_START +
733 		sizeof(struct mcp251xfd_hw_tef_obj) * n;
734 }
735 
736 static inline u16
737 mcp251xfd_get_tx_obj_addr(const struct mcp251xfd_tx_ring *ring, u8 n)
738 {
739 	return ring->base + ring->obj_size * n;
740 }
741 
742 static inline u16
743 mcp251xfd_get_rx_obj_addr(const struct mcp251xfd_rx_ring *ring, u8 n)
744 {
745 	return ring->base + ring->obj_size * n;
746 }
747 
748 static inline u8 mcp251xfd_get_tef_head(const struct mcp251xfd_priv *priv)
749 {
750 	return priv->tef->head & (priv->tx->obj_num - 1);
751 }
752 
753 static inline u8 mcp251xfd_get_tef_tail(const struct mcp251xfd_priv *priv)
754 {
755 	return priv->tef->tail & (priv->tx->obj_num - 1);
756 }
757 
758 static inline u8 mcp251xfd_get_tef_len(const struct mcp251xfd_priv *priv)
759 {
760 	return priv->tef->head - priv->tef->tail;
761 }
762 
763 static inline u8 mcp251xfd_get_tef_linear_len(const struct mcp251xfd_priv *priv)
764 {
765 	u8 len;
766 
767 	len = mcp251xfd_get_tef_len(priv);
768 
769 	return min_t(u8, len, priv->tx->obj_num - mcp251xfd_get_tef_tail(priv));
770 }
771 
772 static inline u8 mcp251xfd_get_tx_head(const struct mcp251xfd_tx_ring *ring)
773 {
774 	return ring->head & (ring->obj_num - 1);
775 }
776 
777 static inline u8 mcp251xfd_get_tx_tail(const struct mcp251xfd_tx_ring *ring)
778 {
779 	return ring->tail & (ring->obj_num - 1);
780 }
781 
782 static inline u8 mcp251xfd_get_tx_free(const struct mcp251xfd_tx_ring *ring)
783 {
784 	return ring->obj_num - (ring->head - ring->tail);
785 }
786 
787 static inline int
788 mcp251xfd_get_tx_nr_by_addr(const struct mcp251xfd_tx_ring *tx_ring, u8 *nr,
789 			    u16 addr)
790 {
791 	if (addr < mcp251xfd_get_tx_obj_addr(tx_ring, 0) ||
792 	    addr >= mcp251xfd_get_tx_obj_addr(tx_ring, tx_ring->obj_num))
793 		return -ENOENT;
794 
795 	*nr = (addr - mcp251xfd_get_tx_obj_addr(tx_ring, 0)) /
796 		tx_ring->obj_size;
797 
798 	return 0;
799 }
800 
801 static inline u8 mcp251xfd_get_rx_head(const struct mcp251xfd_rx_ring *ring)
802 {
803 	return ring->head & (ring->obj_num - 1);
804 }
805 
806 static inline u8 mcp251xfd_get_rx_tail(const struct mcp251xfd_rx_ring *ring)
807 {
808 	return ring->tail & (ring->obj_num - 1);
809 }
810 
811 static inline u8 mcp251xfd_get_rx_len(const struct mcp251xfd_rx_ring *ring)
812 {
813 	return ring->head - ring->tail;
814 }
815 
816 static inline u8
817 mcp251xfd_get_rx_linear_len(const struct mcp251xfd_rx_ring *ring)
818 {
819 	u8 len;
820 
821 	len = mcp251xfd_get_rx_len(ring);
822 
823 	return min_t(u8, len, ring->obj_num - mcp251xfd_get_rx_tail(ring));
824 }
825 
826 #define mcp251xfd_for_each_tx_obj(ring, _obj, n) \
827 	for ((n) = 0, (_obj) = &(ring)->obj[(n)]; \
828 	     (n) < (ring)->obj_num; \
829 	     (n)++, (_obj) = &(ring)->obj[(n)])
830 
831 #define mcp251xfd_for_each_rx_ring(priv, ring, n) \
832 	for ((n) = 0, (ring) = *((priv)->rx + (n)); \
833 	     (n) < (priv)->rx_ring_num; \
834 	     (n)++, (ring) = *((priv)->rx + (n)))
835 
836 int mcp251xfd_regmap_init(struct mcp251xfd_priv *priv);
837 u16 mcp251xfd_crc16_compute2(const void *cmd, size_t cmd_size,
838 			     const void *data, size_t data_size);
839 u16 mcp251xfd_crc16_compute(const void *data, size_t data_size);
840 
841 #endif
842