• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 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_TX_OBJ_NUM_CAN 8
372 #define MCP251XFD_TX_OBJ_NUM_CANFD 4
373 
374 #if MCP251XFD_TX_OBJ_NUM_CAN > MCP251XFD_TX_OBJ_NUM_CANFD
375 #define MCP251XFD_TX_OBJ_NUM_MAX MCP251XFD_TX_OBJ_NUM_CAN
376 #else
377 #define MCP251XFD_TX_OBJ_NUM_MAX MCP251XFD_TX_OBJ_NUM_CANFD
378 #endif
379 
380 #define MCP251XFD_NAPI_WEIGHT 32
381 #define MCP251XFD_TX_FIFO 1
382 #define MCP251XFD_RX_FIFO(x) (MCP251XFD_TX_FIFO + 1 + (x))
383 
384 /* SPI commands */
385 #define MCP251XFD_SPI_INSTRUCTION_RESET 0x0000
386 #define MCP251XFD_SPI_INSTRUCTION_WRITE 0x2000
387 #define MCP251XFD_SPI_INSTRUCTION_READ 0x3000
388 #define MCP251XFD_SPI_INSTRUCTION_WRITE_CRC 0xa000
389 #define MCP251XFD_SPI_INSTRUCTION_READ_CRC 0xb000
390 #define MCP251XFD_SPI_INSTRUCTION_WRITE_CRC_SAFE 0xc000
391 #define MCP251XFD_SPI_ADDRESS_MASK GENMASK(11, 0)
392 
393 #define MCP251XFD_SYSCLOCK_HZ_MAX 40000000
394 #define MCP251XFD_SYSCLOCK_HZ_MIN 1000000
395 #define MCP251XFD_SPICLOCK_HZ_MAX 20000000
396 #define MCP251XFD_OSC_PLL_MULTIPLIER 10
397 #define MCP251XFD_OSC_STAB_SLEEP_US (3 * USEC_PER_MSEC)
398 #define MCP251XFD_OSC_STAB_TIMEOUT_US (10 * MCP251XFD_OSC_STAB_SLEEP_US)
399 #define MCP251XFD_POLL_SLEEP_US (10)
400 #define MCP251XFD_POLL_TIMEOUT_US (USEC_PER_MSEC)
401 #define MCP251XFD_SOFTRESET_RETRIES_MAX 3
402 #define MCP251XFD_READ_CRC_RETRIES_MAX 3
403 #define MCP251XFD_ECC_CNT_MAX 2
404 #define MCP251XFD_SANITIZE_SPI 1
405 #define MCP251XFD_SANITIZE_CAN 1
406 
407 /* Silence TX MAB overflow warnings */
408 #define MCP251XFD_QUIRK_MAB_NO_WARN BIT(0)
409 /* Use CRC to access registers */
410 #define MCP251XFD_QUIRK_CRC_REG BIT(1)
411 /* Use CRC to access RX/TEF-RAM */
412 #define MCP251XFD_QUIRK_CRC_RX BIT(2)
413 /* Use CRC to access TX-RAM */
414 #define MCP251XFD_QUIRK_CRC_TX BIT(3)
415 /* Enable ECC for RAM */
416 #define MCP251XFD_QUIRK_ECC BIT(4)
417 /* Use Half Duplex SPI transfers */
418 #define MCP251XFD_QUIRK_HALF_DUPLEX BIT(5)
419 
420 struct mcp251xfd_hw_tef_obj {
421 	u32 id;
422 	u32 flags;
423 	u32 ts;
424 };
425 
426 /* The tx_obj_raw version is used in spi async, i.e. without
427  * regmap. We have to take care of endianness ourselves.
428  */
429 struct __packed mcp251xfd_hw_tx_obj_raw {
430 	__le32 id;
431 	__le32 flags;
432 	u8 data[sizeof_field(struct canfd_frame, data)];
433 };
434 
435 struct mcp251xfd_hw_tx_obj_can {
436 	u32 id;
437 	u32 flags;
438 	u8 data[sizeof_field(struct can_frame, data)];
439 };
440 
441 struct mcp251xfd_hw_tx_obj_canfd {
442 	u32 id;
443 	u32 flags;
444 	u8 data[sizeof_field(struct canfd_frame, data)];
445 };
446 
447 struct mcp251xfd_hw_rx_obj_can {
448 	u32 id;
449 	u32 flags;
450 	u32 ts;
451 	u8 data[sizeof_field(struct can_frame, data)];
452 };
453 
454 struct mcp251xfd_hw_rx_obj_canfd {
455 	u32 id;
456 	u32 flags;
457 	u32 ts;
458 	u8 data[sizeof_field(struct canfd_frame, data)];
459 };
460 
461 struct mcp251xfd_tef_ring {
462 	unsigned int head;
463 	unsigned int tail;
464 
465 	/* u8 obj_num equals tx_ring->obj_num */
466 	/* u8 obj_size equals sizeof(struct mcp251xfd_hw_tef_obj) */
467 };
468 
469 struct __packed mcp251xfd_buf_cmd {
470 	__be16 cmd;
471 };
472 
473 struct __packed mcp251xfd_buf_cmd_crc {
474 	__be16 cmd;
475 	u8 len;
476 };
477 
478 union mcp251xfd_tx_obj_load_buf {
479 	struct __packed {
480 		struct mcp251xfd_buf_cmd cmd;
481 		struct mcp251xfd_hw_tx_obj_raw hw_tx_obj;
482 	} nocrc;
483 	struct __packed {
484 		struct mcp251xfd_buf_cmd_crc cmd;
485 		struct mcp251xfd_hw_tx_obj_raw hw_tx_obj;
486 		__be16 crc;
487 	} crc;
488 } ____cacheline_aligned;
489 
490 union mcp251xfd_write_reg_buf {
491 	struct __packed {
492 		struct mcp251xfd_buf_cmd cmd;
493 		u8 data[4];
494 	} nocrc;
495 	struct __packed {
496 		struct mcp251xfd_buf_cmd_crc cmd;
497 		u8 data[4];
498 		__be16 crc;
499 	} crc;
500 } ____cacheline_aligned;
501 
502 struct mcp251xfd_tx_obj {
503 	struct spi_message msg;
504 	struct spi_transfer xfer[2];
505 	union mcp251xfd_tx_obj_load_buf buf;
506 };
507 
508 struct mcp251xfd_tx_ring {
509 	unsigned int head;
510 	unsigned int tail;
511 
512 	u16 base;
513 	u8 obj_num;
514 	u8 obj_size;
515 
516 	struct mcp251xfd_tx_obj obj[MCP251XFD_TX_OBJ_NUM_MAX];
517 	union mcp251xfd_write_reg_buf rts_buf;
518 };
519 
520 struct mcp251xfd_rx_ring {
521 	unsigned int head;
522 	unsigned int tail;
523 
524 	u16 base;
525 	u8 nr;
526 	u8 fifo_nr;
527 	u8 obj_num;
528 	u8 obj_size;
529 
530 	struct mcp251xfd_hw_rx_obj_canfd obj[];
531 };
532 
533 struct __packed mcp251xfd_map_buf_nocrc {
534 	struct mcp251xfd_buf_cmd cmd;
535 	u8 data[256];
536 } ____cacheline_aligned;
537 
538 struct __packed mcp251xfd_map_buf_crc {
539 	struct mcp251xfd_buf_cmd_crc cmd;
540 	u8 data[256 - 4];
541 	__be16 crc;
542 } ____cacheline_aligned;
543 
544 struct mcp251xfd_ecc {
545 	u32 ecc_stat;
546 	int cnt;
547 };
548 
549 struct mcp251xfd_regs_status {
550 	u32 intf;
551 };
552 
553 enum mcp251xfd_model {
554 	MCP251XFD_MODEL_MCP2517FD = 0x2517,
555 	MCP251XFD_MODEL_MCP2518FD = 0x2518,
556 	MCP251XFD_MODEL_MCP251XFD = 0xffff,	/* autodetect model */
557 };
558 
559 struct mcp251xfd_devtype_data {
560 	enum mcp251xfd_model model;
561 	u32 quirks;
562 };
563 
564 struct mcp251xfd_priv {
565 	struct can_priv can;
566 	struct can_rx_offload offload;
567 	struct net_device *ndev;
568 
569 	struct regmap *map_reg;			/* register access */
570 	struct regmap *map_rx;			/* RX/TEF RAM access */
571 
572 	struct regmap *map_nocrc;
573 	struct mcp251xfd_map_buf_nocrc *map_buf_nocrc_rx;
574 	struct mcp251xfd_map_buf_nocrc *map_buf_nocrc_tx;
575 
576 	struct regmap *map_crc;
577 	struct mcp251xfd_map_buf_crc *map_buf_crc_rx;
578 	struct mcp251xfd_map_buf_crc *map_buf_crc_tx;
579 
580 	struct spi_device *spi;
581 	u32 spi_max_speed_hz_orig;
582 
583 	struct mcp251xfd_tef_ring tef;
584 	struct mcp251xfd_tx_ring tx[1];
585 	struct mcp251xfd_rx_ring *rx[1];
586 
587 	u8 rx_ring_num;
588 
589 	struct mcp251xfd_ecc ecc;
590 	struct mcp251xfd_regs_status regs_status;
591 
592 	struct gpio_desc *rx_int;
593 	struct clk *clk;
594 	struct regulator *reg_vdd;
595 	struct regulator *reg_xceiver;
596 
597 	struct mcp251xfd_devtype_data devtype_data;
598 	struct can_berr_counter bec;
599 };
600 
601 #define MCP251XFD_IS(_model) \
602 static inline bool \
603 mcp251xfd_is_##_model(const struct mcp251xfd_priv *priv) \
604 { \
605 	return priv->devtype_data.model == MCP251XFD_MODEL_MCP##_model##FD; \
606 }
607 
608 MCP251XFD_IS(2517);
609 MCP251XFD_IS(2518);
610 MCP251XFD_IS(251X);
611 
mcp251xfd_first_byte_set(u32 mask)612 static inline u8 mcp251xfd_first_byte_set(u32 mask)
613 {
614 	return (mask & 0x0000ffff) ?
615 		((mask & 0x000000ff) ? 0 : 1) :
616 		((mask & 0x00ff0000) ? 2 : 3);
617 }
618 
mcp251xfd_last_byte_set(u32 mask)619 static inline u8 mcp251xfd_last_byte_set(u32 mask)
620 {
621 	return (mask & 0xffff0000) ?
622 		((mask & 0xff000000) ? 3 : 2) :
623 		((mask & 0x0000ff00) ? 1 : 0);
624 }
625 
mcp251xfd_cmd_reset(void)626 static inline __be16 mcp251xfd_cmd_reset(void)
627 {
628 	return cpu_to_be16(MCP251XFD_SPI_INSTRUCTION_RESET);
629 }
630 
631 static inline void
mcp251xfd_spi_cmd_read_nocrc(struct mcp251xfd_buf_cmd * cmd,u16 addr)632 mcp251xfd_spi_cmd_read_nocrc(struct mcp251xfd_buf_cmd *cmd, u16 addr)
633 {
634 	cmd->cmd = cpu_to_be16(MCP251XFD_SPI_INSTRUCTION_READ | addr);
635 }
636 
637 static inline void
mcp251xfd_spi_cmd_write_nocrc(struct mcp251xfd_buf_cmd * cmd,u16 addr)638 mcp251xfd_spi_cmd_write_nocrc(struct mcp251xfd_buf_cmd *cmd, u16 addr)
639 {
640 	cmd->cmd = cpu_to_be16(MCP251XFD_SPI_INSTRUCTION_WRITE | addr);
641 }
642 
mcp251xfd_reg_in_ram(unsigned int reg)643 static inline bool mcp251xfd_reg_in_ram(unsigned int reg)
644 {
645 	static const struct regmap_range range =
646 		regmap_reg_range(MCP251XFD_RAM_START,
647 				 MCP251XFD_RAM_START + MCP251XFD_RAM_SIZE - 4);
648 
649 	return regmap_reg_in_range(reg, &range);
650 }
651 
652 static inline void
__mcp251xfd_spi_cmd_crc_set_len(struct mcp251xfd_buf_cmd_crc * cmd,u16 len,bool in_ram)653 __mcp251xfd_spi_cmd_crc_set_len(struct mcp251xfd_buf_cmd_crc *cmd,
654 				u16 len, bool in_ram)
655 {
656 	/* Number of u32 for RAM access, number of u8 otherwise. */
657 	if (in_ram)
658 		cmd->len = len >> 2;
659 	else
660 		cmd->len = len;
661 }
662 
663 static inline void
mcp251xfd_spi_cmd_crc_set_len_in_ram(struct mcp251xfd_buf_cmd_crc * cmd,u16 len)664 mcp251xfd_spi_cmd_crc_set_len_in_ram(struct mcp251xfd_buf_cmd_crc *cmd, u16 len)
665 {
666 	__mcp251xfd_spi_cmd_crc_set_len(cmd, len, true);
667 }
668 
669 static inline void
mcp251xfd_spi_cmd_crc_set_len_in_reg(struct mcp251xfd_buf_cmd_crc * cmd,u16 len)670 mcp251xfd_spi_cmd_crc_set_len_in_reg(struct mcp251xfd_buf_cmd_crc *cmd, u16 len)
671 {
672 	__mcp251xfd_spi_cmd_crc_set_len(cmd, len, false);
673 }
674 
675 static inline void
mcp251xfd_spi_cmd_read_crc_set_addr(struct mcp251xfd_buf_cmd_crc * cmd,u16 addr)676 mcp251xfd_spi_cmd_read_crc_set_addr(struct mcp251xfd_buf_cmd_crc *cmd, u16 addr)
677 {
678 	cmd->cmd = cpu_to_be16(MCP251XFD_SPI_INSTRUCTION_READ_CRC | addr);
679 }
680 
681 static inline void
mcp251xfd_spi_cmd_read_crc(struct mcp251xfd_buf_cmd_crc * cmd,u16 addr,u16 len)682 mcp251xfd_spi_cmd_read_crc(struct mcp251xfd_buf_cmd_crc *cmd,
683 			   u16 addr, u16 len)
684 {
685 	mcp251xfd_spi_cmd_read_crc_set_addr(cmd, addr);
686 	__mcp251xfd_spi_cmd_crc_set_len(cmd, len, mcp251xfd_reg_in_ram(addr));
687 }
688 
689 static inline void
mcp251xfd_spi_cmd_write_crc_set_addr(struct mcp251xfd_buf_cmd_crc * cmd,u16 addr)690 mcp251xfd_spi_cmd_write_crc_set_addr(struct mcp251xfd_buf_cmd_crc *cmd,
691 				     u16 addr)
692 {
693 	cmd->cmd = cpu_to_be16(MCP251XFD_SPI_INSTRUCTION_WRITE_CRC | addr);
694 }
695 
696 static inline void
mcp251xfd_spi_cmd_write_crc(struct mcp251xfd_buf_cmd_crc * cmd,u16 addr,u16 len)697 mcp251xfd_spi_cmd_write_crc(struct mcp251xfd_buf_cmd_crc *cmd,
698 			    u16 addr, u16 len)
699 {
700 	mcp251xfd_spi_cmd_write_crc_set_addr(cmd, addr);
701 	__mcp251xfd_spi_cmd_crc_set_len(cmd, len, mcp251xfd_reg_in_ram(addr));
702 }
703 
704 static inline u8 *
mcp251xfd_spi_cmd_write(const struct mcp251xfd_priv * priv,union mcp251xfd_write_reg_buf * write_reg_buf,u16 addr)705 mcp251xfd_spi_cmd_write(const struct mcp251xfd_priv *priv,
706 			union mcp251xfd_write_reg_buf *write_reg_buf,
707 			u16 addr)
708 {
709 	u8 *data;
710 
711 	if (priv->devtype_data.quirks & MCP251XFD_QUIRK_CRC_REG) {
712 		mcp251xfd_spi_cmd_write_crc_set_addr(&write_reg_buf->crc.cmd,
713 						     addr);
714 		data = write_reg_buf->crc.data;
715 	} else {
716 		mcp251xfd_spi_cmd_write_nocrc(&write_reg_buf->nocrc.cmd,
717 					      addr);
718 		data = write_reg_buf->nocrc.data;
719 	}
720 
721 	return data;
722 }
723 
mcp251xfd_get_tef_obj_addr(u8 n)724 static inline u16 mcp251xfd_get_tef_obj_addr(u8 n)
725 {
726 	return MCP251XFD_RAM_START +
727 		sizeof(struct mcp251xfd_hw_tef_obj) * n;
728 }
729 
730 static inline u16
mcp251xfd_get_tx_obj_addr(const struct mcp251xfd_tx_ring * ring,u8 n)731 mcp251xfd_get_tx_obj_addr(const struct mcp251xfd_tx_ring *ring, u8 n)
732 {
733 	return ring->base + ring->obj_size * n;
734 }
735 
736 static inline u16
mcp251xfd_get_rx_obj_addr(const struct mcp251xfd_rx_ring * ring,u8 n)737 mcp251xfd_get_rx_obj_addr(const struct mcp251xfd_rx_ring *ring, u8 n)
738 {
739 	return ring->base + ring->obj_size * n;
740 }
741 
mcp251xfd_get_tef_head(const struct mcp251xfd_priv * priv)742 static inline u8 mcp251xfd_get_tef_head(const struct mcp251xfd_priv *priv)
743 {
744 	return priv->tef.head & (priv->tx->obj_num - 1);
745 }
746 
mcp251xfd_get_tef_tail(const struct mcp251xfd_priv * priv)747 static inline u8 mcp251xfd_get_tef_tail(const struct mcp251xfd_priv *priv)
748 {
749 	return priv->tef.tail & (priv->tx->obj_num - 1);
750 }
751 
mcp251xfd_get_tef_len(const struct mcp251xfd_priv * priv)752 static inline u8 mcp251xfd_get_tef_len(const struct mcp251xfd_priv *priv)
753 {
754 	return priv->tef.head - priv->tef.tail;
755 }
756 
mcp251xfd_get_tef_linear_len(const struct mcp251xfd_priv * priv)757 static inline u8 mcp251xfd_get_tef_linear_len(const struct mcp251xfd_priv *priv)
758 {
759 	u8 len;
760 
761 	len = mcp251xfd_get_tef_len(priv);
762 
763 	return min_t(u8, len, priv->tx->obj_num - mcp251xfd_get_tef_tail(priv));
764 }
765 
mcp251xfd_get_tx_head(const struct mcp251xfd_tx_ring * ring)766 static inline u8 mcp251xfd_get_tx_head(const struct mcp251xfd_tx_ring *ring)
767 {
768 	return ring->head & (ring->obj_num - 1);
769 }
770 
mcp251xfd_get_tx_tail(const struct mcp251xfd_tx_ring * ring)771 static inline u8 mcp251xfd_get_tx_tail(const struct mcp251xfd_tx_ring *ring)
772 {
773 	return ring->tail & (ring->obj_num - 1);
774 }
775 
mcp251xfd_get_tx_free(const struct mcp251xfd_tx_ring * ring)776 static inline u8 mcp251xfd_get_tx_free(const struct mcp251xfd_tx_ring *ring)
777 {
778 	return ring->obj_num - (ring->head - ring->tail);
779 }
780 
781 static inline int
mcp251xfd_get_tx_nr_by_addr(const struct mcp251xfd_tx_ring * tx_ring,u8 * nr,u16 addr)782 mcp251xfd_get_tx_nr_by_addr(const struct mcp251xfd_tx_ring *tx_ring, u8 *nr,
783 			    u16 addr)
784 {
785 	if (addr < mcp251xfd_get_tx_obj_addr(tx_ring, 0) ||
786 	    addr >= mcp251xfd_get_tx_obj_addr(tx_ring, tx_ring->obj_num))
787 		return -ENOENT;
788 
789 	*nr = (addr - mcp251xfd_get_tx_obj_addr(tx_ring, 0)) /
790 		tx_ring->obj_size;
791 
792 	return 0;
793 }
794 
mcp251xfd_get_rx_head(const struct mcp251xfd_rx_ring * ring)795 static inline u8 mcp251xfd_get_rx_head(const struct mcp251xfd_rx_ring *ring)
796 {
797 	return ring->head & (ring->obj_num - 1);
798 }
799 
mcp251xfd_get_rx_tail(const struct mcp251xfd_rx_ring * ring)800 static inline u8 mcp251xfd_get_rx_tail(const struct mcp251xfd_rx_ring *ring)
801 {
802 	return ring->tail & (ring->obj_num - 1);
803 }
804 
mcp251xfd_get_rx_len(const struct mcp251xfd_rx_ring * ring)805 static inline u8 mcp251xfd_get_rx_len(const struct mcp251xfd_rx_ring *ring)
806 {
807 	return ring->head - ring->tail;
808 }
809 
810 static inline u8
mcp251xfd_get_rx_linear_len(const struct mcp251xfd_rx_ring * ring)811 mcp251xfd_get_rx_linear_len(const struct mcp251xfd_rx_ring *ring)
812 {
813 	u8 len;
814 
815 	len = mcp251xfd_get_rx_len(ring);
816 
817 	return min_t(u8, len, ring->obj_num - mcp251xfd_get_rx_tail(ring));
818 }
819 
820 #define mcp251xfd_for_each_tx_obj(ring, _obj, n) \
821 	for ((n) = 0, (_obj) = &(ring)->obj[(n)]; \
822 	     (n) < (ring)->obj_num; \
823 	     (n)++, (_obj) = &(ring)->obj[(n)])
824 
825 #define mcp251xfd_for_each_rx_ring(priv, ring, n) \
826 	for ((n) = 0, (ring) = *((priv)->rx + (n)); \
827 	     (n) < (priv)->rx_ring_num; \
828 	     (n)++, (ring) = *((priv)->rx + (n)))
829 
830 int mcp251xfd_regmap_init(struct mcp251xfd_priv *priv);
831 u16 mcp251xfd_crc16_compute2(const void *cmd, size_t cmd_size,
832 			     const void *data, size_t data_size);
833 u16 mcp251xfd_crc16_compute(const void *data, size_t data_size);
834 
835 #endif
836