18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci *
38c2ecf20Sopenharmony_ci * mcp251xfd - Microchip MCP251xFD Family CAN controller driver
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (c) 2019 Pengutronix,
68c2ecf20Sopenharmony_ci *                    Marc Kleine-Budde <kernel@pengutronix.de>
78c2ecf20Sopenharmony_ci * Copyright (c) 2019 Martin Sperl <kernel@martin.sperl.org>
88c2ecf20Sopenharmony_ci */
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_ci#ifndef _MCP251XFD_H
118c2ecf20Sopenharmony_ci#define _MCP251XFD_H
128c2ecf20Sopenharmony_ci
138c2ecf20Sopenharmony_ci#include <linux/can/core.h>
148c2ecf20Sopenharmony_ci#include <linux/can/dev.h>
158c2ecf20Sopenharmony_ci#include <linux/can/rx-offload.h>
168c2ecf20Sopenharmony_ci#include <linux/gpio/consumer.h>
178c2ecf20Sopenharmony_ci#include <linux/kernel.h>
188c2ecf20Sopenharmony_ci#include <linux/regmap.h>
198c2ecf20Sopenharmony_ci#include <linux/regulator/consumer.h>
208c2ecf20Sopenharmony_ci#include <linux/spi/spi.h>
218c2ecf20Sopenharmony_ci
228c2ecf20Sopenharmony_ci/* MPC251x registers */
238c2ecf20Sopenharmony_ci
248c2ecf20Sopenharmony_ci/* CAN FD Controller Module SFR */
258c2ecf20Sopenharmony_ci#define MCP251XFD_REG_CON 0x00
268c2ecf20Sopenharmony_ci#define MCP251XFD_REG_CON_TXBWS_MASK GENMASK(31, 28)
278c2ecf20Sopenharmony_ci#define MCP251XFD_REG_CON_ABAT BIT(27)
288c2ecf20Sopenharmony_ci#define MCP251XFD_REG_CON_REQOP_MASK GENMASK(26, 24)
298c2ecf20Sopenharmony_ci#define MCP251XFD_REG_CON_MODE_MIXED 0
308c2ecf20Sopenharmony_ci#define MCP251XFD_REG_CON_MODE_SLEEP 1
318c2ecf20Sopenharmony_ci#define MCP251XFD_REG_CON_MODE_INT_LOOPBACK 2
328c2ecf20Sopenharmony_ci#define MCP251XFD_REG_CON_MODE_LISTENONLY 3
338c2ecf20Sopenharmony_ci#define MCP251XFD_REG_CON_MODE_CONFIG 4
348c2ecf20Sopenharmony_ci#define MCP251XFD_REG_CON_MODE_EXT_LOOPBACK 5
358c2ecf20Sopenharmony_ci#define MCP251XFD_REG_CON_MODE_CAN2_0 6
368c2ecf20Sopenharmony_ci#define MCP251XFD_REG_CON_MODE_RESTRICTED 7
378c2ecf20Sopenharmony_ci#define MCP251XFD_REG_CON_OPMOD_MASK GENMASK(23, 21)
388c2ecf20Sopenharmony_ci#define MCP251XFD_REG_CON_TXQEN BIT(20)
398c2ecf20Sopenharmony_ci#define MCP251XFD_REG_CON_STEF BIT(19)
408c2ecf20Sopenharmony_ci#define MCP251XFD_REG_CON_SERR2LOM BIT(18)
418c2ecf20Sopenharmony_ci#define MCP251XFD_REG_CON_ESIGM BIT(17)
428c2ecf20Sopenharmony_ci#define MCP251XFD_REG_CON_RTXAT BIT(16)
438c2ecf20Sopenharmony_ci#define MCP251XFD_REG_CON_BRSDIS BIT(12)
448c2ecf20Sopenharmony_ci#define MCP251XFD_REG_CON_BUSY BIT(11)
458c2ecf20Sopenharmony_ci#define MCP251XFD_REG_CON_WFT_MASK GENMASK(10, 9)
468c2ecf20Sopenharmony_ci#define MCP251XFD_REG_CON_WFT_T00FILTER 0x0
478c2ecf20Sopenharmony_ci#define MCP251XFD_REG_CON_WFT_T01FILTER 0x1
488c2ecf20Sopenharmony_ci#define MCP251XFD_REG_CON_WFT_T10FILTER 0x2
498c2ecf20Sopenharmony_ci#define MCP251XFD_REG_CON_WFT_T11FILTER 0x3
508c2ecf20Sopenharmony_ci#define MCP251XFD_REG_CON_WAKFIL BIT(8)
518c2ecf20Sopenharmony_ci#define MCP251XFD_REG_CON_PXEDIS BIT(6)
528c2ecf20Sopenharmony_ci#define MCP251XFD_REG_CON_ISOCRCEN BIT(5)
538c2ecf20Sopenharmony_ci#define MCP251XFD_REG_CON_DNCNT_MASK GENMASK(4, 0)
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_ci#define MCP251XFD_REG_NBTCFG 0x04
568c2ecf20Sopenharmony_ci#define MCP251XFD_REG_NBTCFG_BRP_MASK GENMASK(31, 24)
578c2ecf20Sopenharmony_ci#define MCP251XFD_REG_NBTCFG_TSEG1_MASK GENMASK(23, 16)
588c2ecf20Sopenharmony_ci#define MCP251XFD_REG_NBTCFG_TSEG2_MASK GENMASK(14, 8)
598c2ecf20Sopenharmony_ci#define MCP251XFD_REG_NBTCFG_SJW_MASK GENMASK(6, 0)
608c2ecf20Sopenharmony_ci
618c2ecf20Sopenharmony_ci#define MCP251XFD_REG_DBTCFG 0x08
628c2ecf20Sopenharmony_ci#define MCP251XFD_REG_DBTCFG_BRP_MASK GENMASK(31, 24)
638c2ecf20Sopenharmony_ci#define MCP251XFD_REG_DBTCFG_TSEG1_MASK GENMASK(20, 16)
648c2ecf20Sopenharmony_ci#define MCP251XFD_REG_DBTCFG_TSEG2_MASK GENMASK(11, 8)
658c2ecf20Sopenharmony_ci#define MCP251XFD_REG_DBTCFG_SJW_MASK GENMASK(3, 0)
668c2ecf20Sopenharmony_ci
678c2ecf20Sopenharmony_ci#define MCP251XFD_REG_TDC 0x0c
688c2ecf20Sopenharmony_ci#define MCP251XFD_REG_TDC_EDGFLTEN BIT(25)
698c2ecf20Sopenharmony_ci#define MCP251XFD_REG_TDC_SID11EN BIT(24)
708c2ecf20Sopenharmony_ci#define MCP251XFD_REG_TDC_TDCMOD_MASK GENMASK(17, 16)
718c2ecf20Sopenharmony_ci#define MCP251XFD_REG_TDC_TDCMOD_AUTO 2
728c2ecf20Sopenharmony_ci#define MCP251XFD_REG_TDC_TDCMOD_MANUAL 1
738c2ecf20Sopenharmony_ci#define MCP251XFD_REG_TDC_TDCMOD_DISABLED 0
748c2ecf20Sopenharmony_ci#define MCP251XFD_REG_TDC_TDCO_MASK GENMASK(14, 8)
758c2ecf20Sopenharmony_ci#define MCP251XFD_REG_TDC_TDCV_MASK GENMASK(5, 0)
768c2ecf20Sopenharmony_ci
778c2ecf20Sopenharmony_ci#define MCP251XFD_REG_TBC 0x10
788c2ecf20Sopenharmony_ci
798c2ecf20Sopenharmony_ci#define MCP251XFD_REG_TSCON 0x14
808c2ecf20Sopenharmony_ci#define MCP251XFD_REG_TSCON_TSRES BIT(18)
818c2ecf20Sopenharmony_ci#define MCP251XFD_REG_TSCON_TSEOF BIT(17)
828c2ecf20Sopenharmony_ci#define MCP251XFD_REG_TSCON_TBCEN BIT(16)
838c2ecf20Sopenharmony_ci#define MCP251XFD_REG_TSCON_TBCPRE_MASK GENMASK(9, 0)
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_ci#define MCP251XFD_REG_VEC 0x18
868c2ecf20Sopenharmony_ci#define MCP251XFD_REG_VEC_RXCODE_MASK GENMASK(30, 24)
878c2ecf20Sopenharmony_ci#define MCP251XFD_REG_VEC_TXCODE_MASK GENMASK(22, 16)
888c2ecf20Sopenharmony_ci#define MCP251XFD_REG_VEC_FILHIT_MASK GENMASK(12, 8)
898c2ecf20Sopenharmony_ci#define MCP251XFD_REG_VEC_ICODE_MASK GENMASK(6, 0)
908c2ecf20Sopenharmony_ci
918c2ecf20Sopenharmony_ci#define MCP251XFD_REG_INT 0x1c
928c2ecf20Sopenharmony_ci#define MCP251XFD_REG_INT_IF_MASK GENMASK(15, 0)
938c2ecf20Sopenharmony_ci#define MCP251XFD_REG_INT_IE_MASK GENMASK(31, 16)
948c2ecf20Sopenharmony_ci#define MCP251XFD_REG_INT_IVMIE BIT(31)
958c2ecf20Sopenharmony_ci#define MCP251XFD_REG_INT_WAKIE BIT(30)
968c2ecf20Sopenharmony_ci#define MCP251XFD_REG_INT_CERRIE BIT(29)
978c2ecf20Sopenharmony_ci#define MCP251XFD_REG_INT_SERRIE BIT(28)
988c2ecf20Sopenharmony_ci#define MCP251XFD_REG_INT_RXOVIE BIT(27)
998c2ecf20Sopenharmony_ci#define MCP251XFD_REG_INT_TXATIE BIT(26)
1008c2ecf20Sopenharmony_ci#define MCP251XFD_REG_INT_SPICRCIE BIT(25)
1018c2ecf20Sopenharmony_ci#define MCP251XFD_REG_INT_ECCIE BIT(24)
1028c2ecf20Sopenharmony_ci#define MCP251XFD_REG_INT_TEFIE BIT(20)
1038c2ecf20Sopenharmony_ci#define MCP251XFD_REG_INT_MODIE BIT(19)
1048c2ecf20Sopenharmony_ci#define MCP251XFD_REG_INT_TBCIE BIT(18)
1058c2ecf20Sopenharmony_ci#define MCP251XFD_REG_INT_RXIE BIT(17)
1068c2ecf20Sopenharmony_ci#define MCP251XFD_REG_INT_TXIE BIT(16)
1078c2ecf20Sopenharmony_ci#define MCP251XFD_REG_INT_IVMIF BIT(15)
1088c2ecf20Sopenharmony_ci#define MCP251XFD_REG_INT_WAKIF BIT(14)
1098c2ecf20Sopenharmony_ci#define MCP251XFD_REG_INT_CERRIF BIT(13)
1108c2ecf20Sopenharmony_ci#define MCP251XFD_REG_INT_SERRIF BIT(12)
1118c2ecf20Sopenharmony_ci#define MCP251XFD_REG_INT_RXOVIF BIT(11)
1128c2ecf20Sopenharmony_ci#define MCP251XFD_REG_INT_TXATIF BIT(10)
1138c2ecf20Sopenharmony_ci#define MCP251XFD_REG_INT_SPICRCIF BIT(9)
1148c2ecf20Sopenharmony_ci#define MCP251XFD_REG_INT_ECCIF BIT(8)
1158c2ecf20Sopenharmony_ci#define MCP251XFD_REG_INT_TEFIF BIT(4)
1168c2ecf20Sopenharmony_ci#define MCP251XFD_REG_INT_MODIF BIT(3)
1178c2ecf20Sopenharmony_ci#define MCP251XFD_REG_INT_TBCIF BIT(2)
1188c2ecf20Sopenharmony_ci#define MCP251XFD_REG_INT_RXIF BIT(1)
1198c2ecf20Sopenharmony_ci#define MCP251XFD_REG_INT_TXIF BIT(0)
1208c2ecf20Sopenharmony_ci/* These IRQ flags must be cleared by SW in the CAN_INT register */
1218c2ecf20Sopenharmony_ci#define MCP251XFD_REG_INT_IF_CLEARABLE_MASK \
1228c2ecf20Sopenharmony_ci	(MCP251XFD_REG_INT_IVMIF | MCP251XFD_REG_INT_WAKIF | \
1238c2ecf20Sopenharmony_ci	 MCP251XFD_REG_INT_CERRIF |  MCP251XFD_REG_INT_SERRIF | \
1248c2ecf20Sopenharmony_ci	 MCP251XFD_REG_INT_MODIF)
1258c2ecf20Sopenharmony_ci
1268c2ecf20Sopenharmony_ci#define MCP251XFD_REG_RXIF 0x20
1278c2ecf20Sopenharmony_ci#define MCP251XFD_REG_TXIF 0x24
1288c2ecf20Sopenharmony_ci#define MCP251XFD_REG_RXOVIF 0x28
1298c2ecf20Sopenharmony_ci#define MCP251XFD_REG_TXATIF 0x2c
1308c2ecf20Sopenharmony_ci#define MCP251XFD_REG_TXREQ 0x30
1318c2ecf20Sopenharmony_ci
1328c2ecf20Sopenharmony_ci#define MCP251XFD_REG_TREC 0x34
1338c2ecf20Sopenharmony_ci#define MCP251XFD_REG_TREC_TXBO BIT(21)
1348c2ecf20Sopenharmony_ci#define MCP251XFD_REG_TREC_TXBP BIT(20)
1358c2ecf20Sopenharmony_ci#define MCP251XFD_REG_TREC_RXBP BIT(19)
1368c2ecf20Sopenharmony_ci#define MCP251XFD_REG_TREC_TXWARN BIT(18)
1378c2ecf20Sopenharmony_ci#define MCP251XFD_REG_TREC_RXWARN BIT(17)
1388c2ecf20Sopenharmony_ci#define MCP251XFD_REG_TREC_EWARN BIT(16)
1398c2ecf20Sopenharmony_ci#define MCP251XFD_REG_TREC_TEC_MASK GENMASK(15, 8)
1408c2ecf20Sopenharmony_ci#define MCP251XFD_REG_TREC_REC_MASK GENMASK(7, 0)
1418c2ecf20Sopenharmony_ci
1428c2ecf20Sopenharmony_ci#define MCP251XFD_REG_BDIAG0 0x38
1438c2ecf20Sopenharmony_ci#define MCP251XFD_REG_BDIAG0_DTERRCNT_MASK GENMASK(31, 24)
1448c2ecf20Sopenharmony_ci#define MCP251XFD_REG_BDIAG0_DRERRCNT_MASK GENMASK(23, 16)
1458c2ecf20Sopenharmony_ci#define MCP251XFD_REG_BDIAG0_NTERRCNT_MASK GENMASK(15, 8)
1468c2ecf20Sopenharmony_ci#define MCP251XFD_REG_BDIAG0_NRERRCNT_MASK GENMASK(7, 0)
1478c2ecf20Sopenharmony_ci
1488c2ecf20Sopenharmony_ci#define MCP251XFD_REG_BDIAG1 0x3c
1498c2ecf20Sopenharmony_ci#define MCP251XFD_REG_BDIAG1_DLCMM BIT(31)
1508c2ecf20Sopenharmony_ci#define MCP251XFD_REG_BDIAG1_ESI BIT(30)
1518c2ecf20Sopenharmony_ci#define MCP251XFD_REG_BDIAG1_DCRCERR BIT(29)
1528c2ecf20Sopenharmony_ci#define MCP251XFD_REG_BDIAG1_DSTUFERR BIT(28)
1538c2ecf20Sopenharmony_ci#define MCP251XFD_REG_BDIAG1_DFORMERR BIT(27)
1548c2ecf20Sopenharmony_ci#define MCP251XFD_REG_BDIAG1_DBIT1ERR BIT(25)
1558c2ecf20Sopenharmony_ci#define MCP251XFD_REG_BDIAG1_DBIT0ERR BIT(24)
1568c2ecf20Sopenharmony_ci#define MCP251XFD_REG_BDIAG1_TXBOERR BIT(23)
1578c2ecf20Sopenharmony_ci#define MCP251XFD_REG_BDIAG1_NCRCERR BIT(21)
1588c2ecf20Sopenharmony_ci#define MCP251XFD_REG_BDIAG1_NSTUFERR BIT(20)
1598c2ecf20Sopenharmony_ci#define MCP251XFD_REG_BDIAG1_NFORMERR BIT(19)
1608c2ecf20Sopenharmony_ci#define MCP251XFD_REG_BDIAG1_NACKERR BIT(18)
1618c2ecf20Sopenharmony_ci#define MCP251XFD_REG_BDIAG1_NBIT1ERR BIT(17)
1628c2ecf20Sopenharmony_ci#define MCP251XFD_REG_BDIAG1_NBIT0ERR BIT(16)
1638c2ecf20Sopenharmony_ci#define MCP251XFD_REG_BDIAG1_BERR_MASK \
1648c2ecf20Sopenharmony_ci	(MCP251XFD_REG_BDIAG1_DLCMM | MCP251XFD_REG_BDIAG1_ESI | \
1658c2ecf20Sopenharmony_ci	 MCP251XFD_REG_BDIAG1_DCRCERR | MCP251XFD_REG_BDIAG1_DSTUFERR | \
1668c2ecf20Sopenharmony_ci	 MCP251XFD_REG_BDIAG1_DFORMERR | MCP251XFD_REG_BDIAG1_DBIT1ERR | \
1678c2ecf20Sopenharmony_ci	 MCP251XFD_REG_BDIAG1_DBIT0ERR | MCP251XFD_REG_BDIAG1_TXBOERR | \
1688c2ecf20Sopenharmony_ci	 MCP251XFD_REG_BDIAG1_NCRCERR | MCP251XFD_REG_BDIAG1_NSTUFERR | \
1698c2ecf20Sopenharmony_ci	 MCP251XFD_REG_BDIAG1_NFORMERR | MCP251XFD_REG_BDIAG1_NACKERR | \
1708c2ecf20Sopenharmony_ci	 MCP251XFD_REG_BDIAG1_NBIT1ERR | MCP251XFD_REG_BDIAG1_NBIT0ERR)
1718c2ecf20Sopenharmony_ci#define MCP251XFD_REG_BDIAG1_EFMSGCNT_MASK GENMASK(15, 0)
1728c2ecf20Sopenharmony_ci
1738c2ecf20Sopenharmony_ci#define MCP251XFD_REG_TEFCON 0x40
1748c2ecf20Sopenharmony_ci#define MCP251XFD_REG_TEFCON_FSIZE_MASK GENMASK(28, 24)
1758c2ecf20Sopenharmony_ci#define MCP251XFD_REG_TEFCON_FRESET BIT(10)
1768c2ecf20Sopenharmony_ci#define MCP251XFD_REG_TEFCON_UINC BIT(8)
1778c2ecf20Sopenharmony_ci#define MCP251XFD_REG_TEFCON_TEFTSEN BIT(5)
1788c2ecf20Sopenharmony_ci#define MCP251XFD_REG_TEFCON_TEFOVIE BIT(3)
1798c2ecf20Sopenharmony_ci#define MCP251XFD_REG_TEFCON_TEFFIE BIT(2)
1808c2ecf20Sopenharmony_ci#define MCP251XFD_REG_TEFCON_TEFHIE BIT(1)
1818c2ecf20Sopenharmony_ci#define MCP251XFD_REG_TEFCON_TEFNEIE BIT(0)
1828c2ecf20Sopenharmony_ci
1838c2ecf20Sopenharmony_ci#define MCP251XFD_REG_TEFSTA 0x44
1848c2ecf20Sopenharmony_ci#define MCP251XFD_REG_TEFSTA_TEFOVIF BIT(3)
1858c2ecf20Sopenharmony_ci#define MCP251XFD_REG_TEFSTA_TEFFIF BIT(2)
1868c2ecf20Sopenharmony_ci#define MCP251XFD_REG_TEFSTA_TEFHIF BIT(1)
1878c2ecf20Sopenharmony_ci#define MCP251XFD_REG_TEFSTA_TEFNEIF BIT(0)
1888c2ecf20Sopenharmony_ci
1898c2ecf20Sopenharmony_ci#define MCP251XFD_REG_TEFUA 0x48
1908c2ecf20Sopenharmony_ci
1918c2ecf20Sopenharmony_ci#define MCP251XFD_REG_TXQCON 0x50
1928c2ecf20Sopenharmony_ci#define MCP251XFD_REG_TXQCON_PLSIZE_MASK GENMASK(31, 29)
1938c2ecf20Sopenharmony_ci#define MCP251XFD_REG_TXQCON_PLSIZE_8 0
1948c2ecf20Sopenharmony_ci#define MCP251XFD_REG_TXQCON_PLSIZE_12 1
1958c2ecf20Sopenharmony_ci#define MCP251XFD_REG_TXQCON_PLSIZE_16 2
1968c2ecf20Sopenharmony_ci#define MCP251XFD_REG_TXQCON_PLSIZE_20 3
1978c2ecf20Sopenharmony_ci#define MCP251XFD_REG_TXQCON_PLSIZE_24 4
1988c2ecf20Sopenharmony_ci#define MCP251XFD_REG_TXQCON_PLSIZE_32 5
1998c2ecf20Sopenharmony_ci#define MCP251XFD_REG_TXQCON_PLSIZE_48 6
2008c2ecf20Sopenharmony_ci#define MCP251XFD_REG_TXQCON_PLSIZE_64 7
2018c2ecf20Sopenharmony_ci#define MCP251XFD_REG_TXQCON_FSIZE_MASK GENMASK(28, 24)
2028c2ecf20Sopenharmony_ci#define MCP251XFD_REG_TXQCON_TXAT_UNLIMITED 3
2038c2ecf20Sopenharmony_ci#define MCP251XFD_REG_TXQCON_TXAT_THREE_SHOT 1
2048c2ecf20Sopenharmony_ci#define MCP251XFD_REG_TXQCON_TXAT_ONE_SHOT 0
2058c2ecf20Sopenharmony_ci#define MCP251XFD_REG_TXQCON_TXAT_MASK GENMASK(22, 21)
2068c2ecf20Sopenharmony_ci#define MCP251XFD_REG_TXQCON_TXPRI_MASK GENMASK(20, 16)
2078c2ecf20Sopenharmony_ci#define MCP251XFD_REG_TXQCON_FRESET BIT(10)
2088c2ecf20Sopenharmony_ci#define MCP251XFD_REG_TXQCON_TXREQ BIT(9)
2098c2ecf20Sopenharmony_ci#define MCP251XFD_REG_TXQCON_UINC BIT(8)
2108c2ecf20Sopenharmony_ci#define MCP251XFD_REG_TXQCON_TXEN BIT(7)
2118c2ecf20Sopenharmony_ci#define MCP251XFD_REG_TXQCON_TXATIE BIT(4)
2128c2ecf20Sopenharmony_ci#define MCP251XFD_REG_TXQCON_TXQEIE BIT(2)
2138c2ecf20Sopenharmony_ci#define MCP251XFD_REG_TXQCON_TXQNIE BIT(0)
2148c2ecf20Sopenharmony_ci
2158c2ecf20Sopenharmony_ci#define MCP251XFD_REG_TXQSTA 0x54
2168c2ecf20Sopenharmony_ci#define MCP251XFD_REG_TXQSTA_TXQCI_MASK GENMASK(12, 8)
2178c2ecf20Sopenharmony_ci#define MCP251XFD_REG_TXQSTA_TXABT BIT(7)
2188c2ecf20Sopenharmony_ci#define MCP251XFD_REG_TXQSTA_TXLARB BIT(6)
2198c2ecf20Sopenharmony_ci#define MCP251XFD_REG_TXQSTA_TXERR BIT(5)
2208c2ecf20Sopenharmony_ci#define MCP251XFD_REG_TXQSTA_TXATIF BIT(4)
2218c2ecf20Sopenharmony_ci#define MCP251XFD_REG_TXQSTA_TXQEIF BIT(2)
2228c2ecf20Sopenharmony_ci#define MCP251XFD_REG_TXQSTA_TXQNIF BIT(0)
2238c2ecf20Sopenharmony_ci
2248c2ecf20Sopenharmony_ci#define MCP251XFD_REG_TXQUA 0x58
2258c2ecf20Sopenharmony_ci
2268c2ecf20Sopenharmony_ci#define MCP251XFD_REG_FIFOCON(x) (0x50 + 0xc * (x))
2278c2ecf20Sopenharmony_ci#define MCP251XFD_REG_FIFOCON_PLSIZE_MASK GENMASK(31, 29)
2288c2ecf20Sopenharmony_ci#define MCP251XFD_REG_FIFOCON_PLSIZE_8 0
2298c2ecf20Sopenharmony_ci#define MCP251XFD_REG_FIFOCON_PLSIZE_12 1
2308c2ecf20Sopenharmony_ci#define MCP251XFD_REG_FIFOCON_PLSIZE_16 2
2318c2ecf20Sopenharmony_ci#define MCP251XFD_REG_FIFOCON_PLSIZE_20 3
2328c2ecf20Sopenharmony_ci#define MCP251XFD_REG_FIFOCON_PLSIZE_24 4
2338c2ecf20Sopenharmony_ci#define MCP251XFD_REG_FIFOCON_PLSIZE_32 5
2348c2ecf20Sopenharmony_ci#define MCP251XFD_REG_FIFOCON_PLSIZE_48 6
2358c2ecf20Sopenharmony_ci#define MCP251XFD_REG_FIFOCON_PLSIZE_64 7
2368c2ecf20Sopenharmony_ci#define MCP251XFD_REG_FIFOCON_FSIZE_MASK GENMASK(28, 24)
2378c2ecf20Sopenharmony_ci#define MCP251XFD_REG_FIFOCON_TXAT_MASK GENMASK(22, 21)
2388c2ecf20Sopenharmony_ci#define MCP251XFD_REG_FIFOCON_TXAT_ONE_SHOT 0
2398c2ecf20Sopenharmony_ci#define MCP251XFD_REG_FIFOCON_TXAT_THREE_SHOT 1
2408c2ecf20Sopenharmony_ci#define MCP251XFD_REG_FIFOCON_TXAT_UNLIMITED 3
2418c2ecf20Sopenharmony_ci#define MCP251XFD_REG_FIFOCON_TXPRI_MASK GENMASK(20, 16)
2428c2ecf20Sopenharmony_ci#define MCP251XFD_REG_FIFOCON_FRESET BIT(10)
2438c2ecf20Sopenharmony_ci#define MCP251XFD_REG_FIFOCON_TXREQ BIT(9)
2448c2ecf20Sopenharmony_ci#define MCP251XFD_REG_FIFOCON_UINC BIT(8)
2458c2ecf20Sopenharmony_ci#define MCP251XFD_REG_FIFOCON_TXEN BIT(7)
2468c2ecf20Sopenharmony_ci#define MCP251XFD_REG_FIFOCON_RTREN BIT(6)
2478c2ecf20Sopenharmony_ci#define MCP251XFD_REG_FIFOCON_RXTSEN BIT(5)
2488c2ecf20Sopenharmony_ci#define MCP251XFD_REG_FIFOCON_TXATIE BIT(4)
2498c2ecf20Sopenharmony_ci#define MCP251XFD_REG_FIFOCON_RXOVIE BIT(3)
2508c2ecf20Sopenharmony_ci#define MCP251XFD_REG_FIFOCON_TFERFFIE BIT(2)
2518c2ecf20Sopenharmony_ci#define MCP251XFD_REG_FIFOCON_TFHRFHIE BIT(1)
2528c2ecf20Sopenharmony_ci#define MCP251XFD_REG_FIFOCON_TFNRFNIE BIT(0)
2538c2ecf20Sopenharmony_ci
2548c2ecf20Sopenharmony_ci#define MCP251XFD_REG_FIFOSTA(x) (0x54 + 0xc * (x))
2558c2ecf20Sopenharmony_ci#define MCP251XFD_REG_FIFOSTA_FIFOCI_MASK GENMASK(12, 8)
2568c2ecf20Sopenharmony_ci#define MCP251XFD_REG_FIFOSTA_TXABT BIT(7)
2578c2ecf20Sopenharmony_ci#define MCP251XFD_REG_FIFOSTA_TXLARB BIT(6)
2588c2ecf20Sopenharmony_ci#define MCP251XFD_REG_FIFOSTA_TXERR BIT(5)
2598c2ecf20Sopenharmony_ci#define MCP251XFD_REG_FIFOSTA_TXATIF BIT(4)
2608c2ecf20Sopenharmony_ci#define MCP251XFD_REG_FIFOSTA_RXOVIF BIT(3)
2618c2ecf20Sopenharmony_ci#define MCP251XFD_REG_FIFOSTA_TFERFFIF BIT(2)
2628c2ecf20Sopenharmony_ci#define MCP251XFD_REG_FIFOSTA_TFHRFHIF BIT(1)
2638c2ecf20Sopenharmony_ci#define MCP251XFD_REG_FIFOSTA_TFNRFNIF BIT(0)
2648c2ecf20Sopenharmony_ci
2658c2ecf20Sopenharmony_ci#define MCP251XFD_REG_FIFOUA(x) (0x58 + 0xc * (x))
2668c2ecf20Sopenharmony_ci
2678c2ecf20Sopenharmony_ci#define MCP251XFD_REG_FLTCON(x) (0x1d0 + 0x4 * (x))
2688c2ecf20Sopenharmony_ci#define MCP251XFD_REG_FLTCON_FLTEN3 BIT(31)
2698c2ecf20Sopenharmony_ci#define MCP251XFD_REG_FLTCON_F3BP_MASK GENMASK(28, 24)
2708c2ecf20Sopenharmony_ci#define MCP251XFD_REG_FLTCON_FLTEN2 BIT(23)
2718c2ecf20Sopenharmony_ci#define MCP251XFD_REG_FLTCON_F2BP_MASK GENMASK(20, 16)
2728c2ecf20Sopenharmony_ci#define MCP251XFD_REG_FLTCON_FLTEN1 BIT(15)
2738c2ecf20Sopenharmony_ci#define MCP251XFD_REG_FLTCON_F1BP_MASK GENMASK(12, 8)
2748c2ecf20Sopenharmony_ci#define MCP251XFD_REG_FLTCON_FLTEN0 BIT(7)
2758c2ecf20Sopenharmony_ci#define MCP251XFD_REG_FLTCON_F0BP_MASK GENMASK(4, 0)
2768c2ecf20Sopenharmony_ci#define MCP251XFD_REG_FLTCON_FLTEN(x) (BIT(7) << 8 * ((x) & 0x3))
2778c2ecf20Sopenharmony_ci#define MCP251XFD_REG_FLTCON_FLT_MASK(x) (GENMASK(7, 0) << (8 * ((x) & 0x3)))
2788c2ecf20Sopenharmony_ci#define MCP251XFD_REG_FLTCON_FBP(x, fifo) ((fifo) << 8 * ((x) & 0x3))
2798c2ecf20Sopenharmony_ci
2808c2ecf20Sopenharmony_ci#define MCP251XFD_REG_FLTOBJ(x) (0x1f0 + 0x8 * (x))
2818c2ecf20Sopenharmony_ci#define MCP251XFD_REG_FLTOBJ_EXIDE BIT(30)
2828c2ecf20Sopenharmony_ci#define MCP251XFD_REG_FLTOBJ_SID11 BIT(29)
2838c2ecf20Sopenharmony_ci#define MCP251XFD_REG_FLTOBJ_EID_MASK GENMASK(28, 11)
2848c2ecf20Sopenharmony_ci#define MCP251XFD_REG_FLTOBJ_SID_MASK GENMASK(10, 0)
2858c2ecf20Sopenharmony_ci
2868c2ecf20Sopenharmony_ci#define MCP251XFD_REG_FLTMASK(x) (0x1f4 + 0x8 * (x))
2878c2ecf20Sopenharmony_ci#define MCP251XFD_REG_MASK_MIDE BIT(30)
2888c2ecf20Sopenharmony_ci#define MCP251XFD_REG_MASK_MSID11 BIT(29)
2898c2ecf20Sopenharmony_ci#define MCP251XFD_REG_MASK_MEID_MASK GENMASK(28, 11)
2908c2ecf20Sopenharmony_ci#define MCP251XFD_REG_MASK_MSID_MASK GENMASK(10, 0)
2918c2ecf20Sopenharmony_ci
2928c2ecf20Sopenharmony_ci/* RAM */
2938c2ecf20Sopenharmony_ci#define MCP251XFD_RAM_START 0x400
2948c2ecf20Sopenharmony_ci#define MCP251XFD_RAM_SIZE SZ_2K
2958c2ecf20Sopenharmony_ci
2968c2ecf20Sopenharmony_ci/* Message Object */
2978c2ecf20Sopenharmony_ci#define MCP251XFD_OBJ_ID_SID11 BIT(29)
2988c2ecf20Sopenharmony_ci#define MCP251XFD_OBJ_ID_EID_MASK GENMASK(28, 11)
2998c2ecf20Sopenharmony_ci#define MCP251XFD_OBJ_ID_SID_MASK GENMASK(10, 0)
3008c2ecf20Sopenharmony_ci#define MCP251XFD_OBJ_FLAGS_SEQ_MCP2518FD_MASK GENMASK(31, 9)
3018c2ecf20Sopenharmony_ci#define MCP251XFD_OBJ_FLAGS_SEQ_MCP2517FD_MASK GENMASK(15, 9)
3028c2ecf20Sopenharmony_ci#define MCP251XFD_OBJ_FLAGS_SEQ_MASK MCP251XFD_OBJ_FLAGS_SEQ_MCP2518FD_MASK
3038c2ecf20Sopenharmony_ci#define MCP251XFD_OBJ_FLAGS_ESI BIT(8)
3048c2ecf20Sopenharmony_ci#define MCP251XFD_OBJ_FLAGS_FDF BIT(7)
3058c2ecf20Sopenharmony_ci#define MCP251XFD_OBJ_FLAGS_BRS BIT(6)
3068c2ecf20Sopenharmony_ci#define MCP251XFD_OBJ_FLAGS_RTR BIT(5)
3078c2ecf20Sopenharmony_ci#define MCP251XFD_OBJ_FLAGS_IDE BIT(4)
3088c2ecf20Sopenharmony_ci#define MCP251XFD_OBJ_FLAGS_DLC GENMASK(3, 0)
3098c2ecf20Sopenharmony_ci
3108c2ecf20Sopenharmony_ci#define MCP251XFD_REG_FRAME_EFF_SID_MASK GENMASK(28, 18)
3118c2ecf20Sopenharmony_ci#define MCP251XFD_REG_FRAME_EFF_EID_MASK GENMASK(17, 0)
3128c2ecf20Sopenharmony_ci
3138c2ecf20Sopenharmony_ci/* MCP2517/18FD SFR */
3148c2ecf20Sopenharmony_ci#define MCP251XFD_REG_OSC 0xe00
3158c2ecf20Sopenharmony_ci#define MCP251XFD_REG_OSC_SCLKRDY BIT(12)
3168c2ecf20Sopenharmony_ci#define MCP251XFD_REG_OSC_OSCRDY BIT(10)
3178c2ecf20Sopenharmony_ci#define MCP251XFD_REG_OSC_PLLRDY BIT(8)
3188c2ecf20Sopenharmony_ci#define MCP251XFD_REG_OSC_CLKODIV_10 3
3198c2ecf20Sopenharmony_ci#define MCP251XFD_REG_OSC_CLKODIV_4 2
3208c2ecf20Sopenharmony_ci#define MCP251XFD_REG_OSC_CLKODIV_2 1
3218c2ecf20Sopenharmony_ci#define MCP251XFD_REG_OSC_CLKODIV_1 0
3228c2ecf20Sopenharmony_ci#define MCP251XFD_REG_OSC_CLKODIV_MASK GENMASK(6, 5)
3238c2ecf20Sopenharmony_ci#define MCP251XFD_REG_OSC_SCLKDIV BIT(4)
3248c2ecf20Sopenharmony_ci#define MCP251XFD_REG_OSC_LPMEN BIT(3)	/* MCP2518FD only */
3258c2ecf20Sopenharmony_ci#define MCP251XFD_REG_OSC_OSCDIS BIT(2)
3268c2ecf20Sopenharmony_ci#define MCP251XFD_REG_OSC_PLLEN BIT(0)
3278c2ecf20Sopenharmony_ci
3288c2ecf20Sopenharmony_ci#define MCP251XFD_REG_IOCON 0xe04
3298c2ecf20Sopenharmony_ci#define MCP251XFD_REG_IOCON_INTOD BIT(30)
3308c2ecf20Sopenharmony_ci#define MCP251XFD_REG_IOCON_SOF BIT(29)
3318c2ecf20Sopenharmony_ci#define MCP251XFD_REG_IOCON_TXCANOD BIT(28)
3328c2ecf20Sopenharmony_ci#define MCP251XFD_REG_IOCON_PM1 BIT(25)
3338c2ecf20Sopenharmony_ci#define MCP251XFD_REG_IOCON_PM0 BIT(24)
3348c2ecf20Sopenharmony_ci#define MCP251XFD_REG_IOCON_GPIO1 BIT(17)
3358c2ecf20Sopenharmony_ci#define MCP251XFD_REG_IOCON_GPIO0 BIT(16)
3368c2ecf20Sopenharmony_ci#define MCP251XFD_REG_IOCON_LAT1 BIT(9)
3378c2ecf20Sopenharmony_ci#define MCP251XFD_REG_IOCON_LAT0 BIT(8)
3388c2ecf20Sopenharmony_ci#define MCP251XFD_REG_IOCON_XSTBYEN BIT(6)
3398c2ecf20Sopenharmony_ci#define MCP251XFD_REG_IOCON_TRIS1 BIT(1)
3408c2ecf20Sopenharmony_ci#define MCP251XFD_REG_IOCON_TRIS0 BIT(0)
3418c2ecf20Sopenharmony_ci
3428c2ecf20Sopenharmony_ci#define MCP251XFD_REG_CRC 0xe08
3438c2ecf20Sopenharmony_ci#define MCP251XFD_REG_CRC_FERRIE BIT(25)
3448c2ecf20Sopenharmony_ci#define MCP251XFD_REG_CRC_CRCERRIE BIT(24)
3458c2ecf20Sopenharmony_ci#define MCP251XFD_REG_CRC_FERRIF BIT(17)
3468c2ecf20Sopenharmony_ci#define MCP251XFD_REG_CRC_CRCERRIF BIT(16)
3478c2ecf20Sopenharmony_ci#define MCP251XFD_REG_CRC_IF_MASK GENMASK(17, 16)
3488c2ecf20Sopenharmony_ci#define MCP251XFD_REG_CRC_MASK GENMASK(15, 0)
3498c2ecf20Sopenharmony_ci
3508c2ecf20Sopenharmony_ci#define MCP251XFD_REG_ECCCON 0xe0c
3518c2ecf20Sopenharmony_ci#define MCP251XFD_REG_ECCCON_PARITY_MASK GENMASK(14, 8)
3528c2ecf20Sopenharmony_ci#define MCP251XFD_REG_ECCCON_DEDIE BIT(2)
3538c2ecf20Sopenharmony_ci#define MCP251XFD_REG_ECCCON_SECIE BIT(1)
3548c2ecf20Sopenharmony_ci#define MCP251XFD_REG_ECCCON_ECCEN BIT(0)
3558c2ecf20Sopenharmony_ci
3568c2ecf20Sopenharmony_ci#define MCP251XFD_REG_ECCSTAT 0xe10
3578c2ecf20Sopenharmony_ci#define MCP251XFD_REG_ECCSTAT_ERRADDR_MASK GENMASK(27, 16)
3588c2ecf20Sopenharmony_ci#define MCP251XFD_REG_ECCSTAT_IF_MASK GENMASK(2, 1)
3598c2ecf20Sopenharmony_ci#define MCP251XFD_REG_ECCSTAT_DEDIF BIT(2)
3608c2ecf20Sopenharmony_ci#define MCP251XFD_REG_ECCSTAT_SECIF BIT(1)
3618c2ecf20Sopenharmony_ci
3628c2ecf20Sopenharmony_ci#define MCP251XFD_REG_DEVID 0xe14	/* MCP2518FD only */
3638c2ecf20Sopenharmony_ci#define MCP251XFD_REG_DEVID_ID_MASK GENMASK(7, 4)
3648c2ecf20Sopenharmony_ci#define MCP251XFD_REG_DEVID_REV_MASK GENMASK(3, 0)
3658c2ecf20Sopenharmony_ci
3668c2ecf20Sopenharmony_ci/* number of TX FIFO objects, depending on CAN mode
3678c2ecf20Sopenharmony_ci *
3688c2ecf20Sopenharmony_ci * FIFO setup: tef: 8*12 bytes = 96 bytes, tx: 8*16 bytes = 128 bytes
3698c2ecf20Sopenharmony_ci * FIFO setup: tef: 4*12 bytes = 48 bytes, tx: 4*72 bytes = 288 bytes
3708c2ecf20Sopenharmony_ci */
3718c2ecf20Sopenharmony_ci#define MCP251XFD_TX_OBJ_NUM_CAN 8
3728c2ecf20Sopenharmony_ci#define MCP251XFD_TX_OBJ_NUM_CANFD 4
3738c2ecf20Sopenharmony_ci
3748c2ecf20Sopenharmony_ci#if MCP251XFD_TX_OBJ_NUM_CAN > MCP251XFD_TX_OBJ_NUM_CANFD
3758c2ecf20Sopenharmony_ci#define MCP251XFD_TX_OBJ_NUM_MAX MCP251XFD_TX_OBJ_NUM_CAN
3768c2ecf20Sopenharmony_ci#else
3778c2ecf20Sopenharmony_ci#define MCP251XFD_TX_OBJ_NUM_MAX MCP251XFD_TX_OBJ_NUM_CANFD
3788c2ecf20Sopenharmony_ci#endif
3798c2ecf20Sopenharmony_ci
3808c2ecf20Sopenharmony_ci#define MCP251XFD_NAPI_WEIGHT 32
3818c2ecf20Sopenharmony_ci#define MCP251XFD_TX_FIFO 1
3828c2ecf20Sopenharmony_ci#define MCP251XFD_RX_FIFO(x) (MCP251XFD_TX_FIFO + 1 + (x))
3838c2ecf20Sopenharmony_ci
3848c2ecf20Sopenharmony_ci/* SPI commands */
3858c2ecf20Sopenharmony_ci#define MCP251XFD_SPI_INSTRUCTION_RESET 0x0000
3868c2ecf20Sopenharmony_ci#define MCP251XFD_SPI_INSTRUCTION_WRITE 0x2000
3878c2ecf20Sopenharmony_ci#define MCP251XFD_SPI_INSTRUCTION_READ 0x3000
3888c2ecf20Sopenharmony_ci#define MCP251XFD_SPI_INSTRUCTION_WRITE_CRC 0xa000
3898c2ecf20Sopenharmony_ci#define MCP251XFD_SPI_INSTRUCTION_READ_CRC 0xb000
3908c2ecf20Sopenharmony_ci#define MCP251XFD_SPI_INSTRUCTION_WRITE_CRC_SAFE 0xc000
3918c2ecf20Sopenharmony_ci#define MCP251XFD_SPI_ADDRESS_MASK GENMASK(11, 0)
3928c2ecf20Sopenharmony_ci
3938c2ecf20Sopenharmony_ci#define MCP251XFD_SYSCLOCK_HZ_MAX 40000000
3948c2ecf20Sopenharmony_ci#define MCP251XFD_SYSCLOCK_HZ_MIN 1000000
3958c2ecf20Sopenharmony_ci#define MCP251XFD_SPICLOCK_HZ_MAX 20000000
3968c2ecf20Sopenharmony_ci#define MCP251XFD_OSC_PLL_MULTIPLIER 10
3978c2ecf20Sopenharmony_ci#define MCP251XFD_OSC_STAB_SLEEP_US (3 * USEC_PER_MSEC)
3988c2ecf20Sopenharmony_ci#define MCP251XFD_OSC_STAB_TIMEOUT_US (10 * MCP251XFD_OSC_STAB_SLEEP_US)
3998c2ecf20Sopenharmony_ci#define MCP251XFD_POLL_SLEEP_US (10)
4008c2ecf20Sopenharmony_ci#define MCP251XFD_POLL_TIMEOUT_US (USEC_PER_MSEC)
4018c2ecf20Sopenharmony_ci#define MCP251XFD_SOFTRESET_RETRIES_MAX 3
4028c2ecf20Sopenharmony_ci#define MCP251XFD_READ_CRC_RETRIES_MAX 3
4038c2ecf20Sopenharmony_ci#define MCP251XFD_ECC_CNT_MAX 2
4048c2ecf20Sopenharmony_ci#define MCP251XFD_SANITIZE_SPI 1
4058c2ecf20Sopenharmony_ci#define MCP251XFD_SANITIZE_CAN 1
4068c2ecf20Sopenharmony_ci
4078c2ecf20Sopenharmony_ci/* Silence TX MAB overflow warnings */
4088c2ecf20Sopenharmony_ci#define MCP251XFD_QUIRK_MAB_NO_WARN BIT(0)
4098c2ecf20Sopenharmony_ci/* Use CRC to access registers */
4108c2ecf20Sopenharmony_ci#define MCP251XFD_QUIRK_CRC_REG BIT(1)
4118c2ecf20Sopenharmony_ci/* Use CRC to access RX/TEF-RAM */
4128c2ecf20Sopenharmony_ci#define MCP251XFD_QUIRK_CRC_RX BIT(2)
4138c2ecf20Sopenharmony_ci/* Use CRC to access TX-RAM */
4148c2ecf20Sopenharmony_ci#define MCP251XFD_QUIRK_CRC_TX BIT(3)
4158c2ecf20Sopenharmony_ci/* Enable ECC for RAM */
4168c2ecf20Sopenharmony_ci#define MCP251XFD_QUIRK_ECC BIT(4)
4178c2ecf20Sopenharmony_ci/* Use Half Duplex SPI transfers */
4188c2ecf20Sopenharmony_ci#define MCP251XFD_QUIRK_HALF_DUPLEX BIT(5)
4198c2ecf20Sopenharmony_ci
4208c2ecf20Sopenharmony_cistruct mcp251xfd_hw_tef_obj {
4218c2ecf20Sopenharmony_ci	u32 id;
4228c2ecf20Sopenharmony_ci	u32 flags;
4238c2ecf20Sopenharmony_ci	u32 ts;
4248c2ecf20Sopenharmony_ci};
4258c2ecf20Sopenharmony_ci
4268c2ecf20Sopenharmony_ci/* The tx_obj_raw version is used in spi async, i.e. without
4278c2ecf20Sopenharmony_ci * regmap. We have to take care of endianness ourselves.
4288c2ecf20Sopenharmony_ci */
4298c2ecf20Sopenharmony_cistruct __packed mcp251xfd_hw_tx_obj_raw {
4308c2ecf20Sopenharmony_ci	__le32 id;
4318c2ecf20Sopenharmony_ci	__le32 flags;
4328c2ecf20Sopenharmony_ci	u8 data[sizeof_field(struct canfd_frame, data)];
4338c2ecf20Sopenharmony_ci};
4348c2ecf20Sopenharmony_ci
4358c2ecf20Sopenharmony_cistruct mcp251xfd_hw_tx_obj_can {
4368c2ecf20Sopenharmony_ci	u32 id;
4378c2ecf20Sopenharmony_ci	u32 flags;
4388c2ecf20Sopenharmony_ci	u8 data[sizeof_field(struct can_frame, data)];
4398c2ecf20Sopenharmony_ci};
4408c2ecf20Sopenharmony_ci
4418c2ecf20Sopenharmony_cistruct mcp251xfd_hw_tx_obj_canfd {
4428c2ecf20Sopenharmony_ci	u32 id;
4438c2ecf20Sopenharmony_ci	u32 flags;
4448c2ecf20Sopenharmony_ci	u8 data[sizeof_field(struct canfd_frame, data)];
4458c2ecf20Sopenharmony_ci};
4468c2ecf20Sopenharmony_ci
4478c2ecf20Sopenharmony_cistruct mcp251xfd_hw_rx_obj_can {
4488c2ecf20Sopenharmony_ci	u32 id;
4498c2ecf20Sopenharmony_ci	u32 flags;
4508c2ecf20Sopenharmony_ci	u32 ts;
4518c2ecf20Sopenharmony_ci	u8 data[sizeof_field(struct can_frame, data)];
4528c2ecf20Sopenharmony_ci};
4538c2ecf20Sopenharmony_ci
4548c2ecf20Sopenharmony_cistruct mcp251xfd_hw_rx_obj_canfd {
4558c2ecf20Sopenharmony_ci	u32 id;
4568c2ecf20Sopenharmony_ci	u32 flags;
4578c2ecf20Sopenharmony_ci	u32 ts;
4588c2ecf20Sopenharmony_ci	u8 data[sizeof_field(struct canfd_frame, data)];
4598c2ecf20Sopenharmony_ci};
4608c2ecf20Sopenharmony_ci
4618c2ecf20Sopenharmony_cistruct mcp251xfd_tef_ring {
4628c2ecf20Sopenharmony_ci	unsigned int head;
4638c2ecf20Sopenharmony_ci	unsigned int tail;
4648c2ecf20Sopenharmony_ci
4658c2ecf20Sopenharmony_ci	/* u8 obj_num equals tx_ring->obj_num */
4668c2ecf20Sopenharmony_ci	/* u8 obj_size equals sizeof(struct mcp251xfd_hw_tef_obj) */
4678c2ecf20Sopenharmony_ci};
4688c2ecf20Sopenharmony_ci
4698c2ecf20Sopenharmony_cistruct __packed mcp251xfd_buf_cmd {
4708c2ecf20Sopenharmony_ci	__be16 cmd;
4718c2ecf20Sopenharmony_ci};
4728c2ecf20Sopenharmony_ci
4738c2ecf20Sopenharmony_cistruct __packed mcp251xfd_buf_cmd_crc {
4748c2ecf20Sopenharmony_ci	__be16 cmd;
4758c2ecf20Sopenharmony_ci	u8 len;
4768c2ecf20Sopenharmony_ci};
4778c2ecf20Sopenharmony_ci
4788c2ecf20Sopenharmony_ciunion mcp251xfd_tx_obj_load_buf {
4798c2ecf20Sopenharmony_ci	struct __packed {
4808c2ecf20Sopenharmony_ci		struct mcp251xfd_buf_cmd cmd;
4818c2ecf20Sopenharmony_ci		struct mcp251xfd_hw_tx_obj_raw hw_tx_obj;
4828c2ecf20Sopenharmony_ci	} nocrc;
4838c2ecf20Sopenharmony_ci	struct __packed {
4848c2ecf20Sopenharmony_ci		struct mcp251xfd_buf_cmd_crc cmd;
4858c2ecf20Sopenharmony_ci		struct mcp251xfd_hw_tx_obj_raw hw_tx_obj;
4868c2ecf20Sopenharmony_ci		__be16 crc;
4878c2ecf20Sopenharmony_ci	} crc;
4888c2ecf20Sopenharmony_ci} ____cacheline_aligned;
4898c2ecf20Sopenharmony_ci
4908c2ecf20Sopenharmony_ciunion mcp251xfd_write_reg_buf {
4918c2ecf20Sopenharmony_ci	struct __packed {
4928c2ecf20Sopenharmony_ci		struct mcp251xfd_buf_cmd cmd;
4938c2ecf20Sopenharmony_ci		u8 data[4];
4948c2ecf20Sopenharmony_ci	} nocrc;
4958c2ecf20Sopenharmony_ci	struct __packed {
4968c2ecf20Sopenharmony_ci		struct mcp251xfd_buf_cmd_crc cmd;
4978c2ecf20Sopenharmony_ci		u8 data[4];
4988c2ecf20Sopenharmony_ci		__be16 crc;
4998c2ecf20Sopenharmony_ci	} crc;
5008c2ecf20Sopenharmony_ci} ____cacheline_aligned;
5018c2ecf20Sopenharmony_ci
5028c2ecf20Sopenharmony_cistruct mcp251xfd_tx_obj {
5038c2ecf20Sopenharmony_ci	struct spi_message msg;
5048c2ecf20Sopenharmony_ci	struct spi_transfer xfer[2];
5058c2ecf20Sopenharmony_ci	union mcp251xfd_tx_obj_load_buf buf;
5068c2ecf20Sopenharmony_ci};
5078c2ecf20Sopenharmony_ci
5088c2ecf20Sopenharmony_cistruct mcp251xfd_tx_ring {
5098c2ecf20Sopenharmony_ci	unsigned int head;
5108c2ecf20Sopenharmony_ci	unsigned int tail;
5118c2ecf20Sopenharmony_ci
5128c2ecf20Sopenharmony_ci	u16 base;
5138c2ecf20Sopenharmony_ci	u8 obj_num;
5148c2ecf20Sopenharmony_ci	u8 obj_size;
5158c2ecf20Sopenharmony_ci
5168c2ecf20Sopenharmony_ci	struct mcp251xfd_tx_obj obj[MCP251XFD_TX_OBJ_NUM_MAX];
5178c2ecf20Sopenharmony_ci	union mcp251xfd_write_reg_buf rts_buf;
5188c2ecf20Sopenharmony_ci};
5198c2ecf20Sopenharmony_ci
5208c2ecf20Sopenharmony_cistruct mcp251xfd_rx_ring {
5218c2ecf20Sopenharmony_ci	unsigned int head;
5228c2ecf20Sopenharmony_ci	unsigned int tail;
5238c2ecf20Sopenharmony_ci
5248c2ecf20Sopenharmony_ci	u16 base;
5258c2ecf20Sopenharmony_ci	u8 nr;
5268c2ecf20Sopenharmony_ci	u8 fifo_nr;
5278c2ecf20Sopenharmony_ci	u8 obj_num;
5288c2ecf20Sopenharmony_ci	u8 obj_size;
5298c2ecf20Sopenharmony_ci
5308c2ecf20Sopenharmony_ci	struct mcp251xfd_hw_rx_obj_canfd obj[];
5318c2ecf20Sopenharmony_ci};
5328c2ecf20Sopenharmony_ci
5338c2ecf20Sopenharmony_cistruct __packed mcp251xfd_map_buf_nocrc {
5348c2ecf20Sopenharmony_ci	struct mcp251xfd_buf_cmd cmd;
5358c2ecf20Sopenharmony_ci	u8 data[256];
5368c2ecf20Sopenharmony_ci} ____cacheline_aligned;
5378c2ecf20Sopenharmony_ci
5388c2ecf20Sopenharmony_cistruct __packed mcp251xfd_map_buf_crc {
5398c2ecf20Sopenharmony_ci	struct mcp251xfd_buf_cmd_crc cmd;
5408c2ecf20Sopenharmony_ci	u8 data[256 - 4];
5418c2ecf20Sopenharmony_ci	__be16 crc;
5428c2ecf20Sopenharmony_ci} ____cacheline_aligned;
5438c2ecf20Sopenharmony_ci
5448c2ecf20Sopenharmony_cistruct mcp251xfd_ecc {
5458c2ecf20Sopenharmony_ci	u32 ecc_stat;
5468c2ecf20Sopenharmony_ci	int cnt;
5478c2ecf20Sopenharmony_ci};
5488c2ecf20Sopenharmony_ci
5498c2ecf20Sopenharmony_cistruct mcp251xfd_regs_status {
5508c2ecf20Sopenharmony_ci	u32 intf;
5518c2ecf20Sopenharmony_ci};
5528c2ecf20Sopenharmony_ci
5538c2ecf20Sopenharmony_cienum mcp251xfd_model {
5548c2ecf20Sopenharmony_ci	MCP251XFD_MODEL_MCP2517FD = 0x2517,
5558c2ecf20Sopenharmony_ci	MCP251XFD_MODEL_MCP2518FD = 0x2518,
5568c2ecf20Sopenharmony_ci	MCP251XFD_MODEL_MCP251XFD = 0xffff,	/* autodetect model */
5578c2ecf20Sopenharmony_ci};
5588c2ecf20Sopenharmony_ci
5598c2ecf20Sopenharmony_cistruct mcp251xfd_devtype_data {
5608c2ecf20Sopenharmony_ci	enum mcp251xfd_model model;
5618c2ecf20Sopenharmony_ci	u32 quirks;
5628c2ecf20Sopenharmony_ci};
5638c2ecf20Sopenharmony_ci
5648c2ecf20Sopenharmony_cistruct mcp251xfd_priv {
5658c2ecf20Sopenharmony_ci	struct can_priv can;
5668c2ecf20Sopenharmony_ci	struct can_rx_offload offload;
5678c2ecf20Sopenharmony_ci	struct net_device *ndev;
5688c2ecf20Sopenharmony_ci
5698c2ecf20Sopenharmony_ci	struct regmap *map_reg;			/* register access */
5708c2ecf20Sopenharmony_ci	struct regmap *map_rx;			/* RX/TEF RAM access */
5718c2ecf20Sopenharmony_ci
5728c2ecf20Sopenharmony_ci	struct regmap *map_nocrc;
5738c2ecf20Sopenharmony_ci	struct mcp251xfd_map_buf_nocrc *map_buf_nocrc_rx;
5748c2ecf20Sopenharmony_ci	struct mcp251xfd_map_buf_nocrc *map_buf_nocrc_tx;
5758c2ecf20Sopenharmony_ci
5768c2ecf20Sopenharmony_ci	struct regmap *map_crc;
5778c2ecf20Sopenharmony_ci	struct mcp251xfd_map_buf_crc *map_buf_crc_rx;
5788c2ecf20Sopenharmony_ci	struct mcp251xfd_map_buf_crc *map_buf_crc_tx;
5798c2ecf20Sopenharmony_ci
5808c2ecf20Sopenharmony_ci	struct spi_device *spi;
5818c2ecf20Sopenharmony_ci	u32 spi_max_speed_hz_orig;
5828c2ecf20Sopenharmony_ci
5838c2ecf20Sopenharmony_ci	struct mcp251xfd_tef_ring tef;
5848c2ecf20Sopenharmony_ci	struct mcp251xfd_tx_ring tx[1];
5858c2ecf20Sopenharmony_ci	struct mcp251xfd_rx_ring *rx[1];
5868c2ecf20Sopenharmony_ci
5878c2ecf20Sopenharmony_ci	u8 rx_ring_num;
5888c2ecf20Sopenharmony_ci
5898c2ecf20Sopenharmony_ci	struct mcp251xfd_ecc ecc;
5908c2ecf20Sopenharmony_ci	struct mcp251xfd_regs_status regs_status;
5918c2ecf20Sopenharmony_ci
5928c2ecf20Sopenharmony_ci	struct gpio_desc *rx_int;
5938c2ecf20Sopenharmony_ci	struct clk *clk;
5948c2ecf20Sopenharmony_ci	struct regulator *reg_vdd;
5958c2ecf20Sopenharmony_ci	struct regulator *reg_xceiver;
5968c2ecf20Sopenharmony_ci
5978c2ecf20Sopenharmony_ci	struct mcp251xfd_devtype_data devtype_data;
5988c2ecf20Sopenharmony_ci	struct can_berr_counter bec;
5998c2ecf20Sopenharmony_ci};
6008c2ecf20Sopenharmony_ci
6018c2ecf20Sopenharmony_ci#define MCP251XFD_IS(_model) \
6028c2ecf20Sopenharmony_cistatic inline bool \
6038c2ecf20Sopenharmony_cimcp251xfd_is_##_model(const struct mcp251xfd_priv *priv) \
6048c2ecf20Sopenharmony_ci{ \
6058c2ecf20Sopenharmony_ci	return priv->devtype_data.model == MCP251XFD_MODEL_MCP##_model##FD; \
6068c2ecf20Sopenharmony_ci}
6078c2ecf20Sopenharmony_ci
6088c2ecf20Sopenharmony_ciMCP251XFD_IS(2517);
6098c2ecf20Sopenharmony_ciMCP251XFD_IS(2518);
6108c2ecf20Sopenharmony_ciMCP251XFD_IS(251X);
6118c2ecf20Sopenharmony_ci
6128c2ecf20Sopenharmony_cistatic inline u8 mcp251xfd_first_byte_set(u32 mask)
6138c2ecf20Sopenharmony_ci{
6148c2ecf20Sopenharmony_ci	return (mask & 0x0000ffff) ?
6158c2ecf20Sopenharmony_ci		((mask & 0x000000ff) ? 0 : 1) :
6168c2ecf20Sopenharmony_ci		((mask & 0x00ff0000) ? 2 : 3);
6178c2ecf20Sopenharmony_ci}
6188c2ecf20Sopenharmony_ci
6198c2ecf20Sopenharmony_cistatic inline u8 mcp251xfd_last_byte_set(u32 mask)
6208c2ecf20Sopenharmony_ci{
6218c2ecf20Sopenharmony_ci	return (mask & 0xffff0000) ?
6228c2ecf20Sopenharmony_ci		((mask & 0xff000000) ? 3 : 2) :
6238c2ecf20Sopenharmony_ci		((mask & 0x0000ff00) ? 1 : 0);
6248c2ecf20Sopenharmony_ci}
6258c2ecf20Sopenharmony_ci
6268c2ecf20Sopenharmony_cistatic inline __be16 mcp251xfd_cmd_reset(void)
6278c2ecf20Sopenharmony_ci{
6288c2ecf20Sopenharmony_ci	return cpu_to_be16(MCP251XFD_SPI_INSTRUCTION_RESET);
6298c2ecf20Sopenharmony_ci}
6308c2ecf20Sopenharmony_ci
6318c2ecf20Sopenharmony_cistatic inline void
6328c2ecf20Sopenharmony_cimcp251xfd_spi_cmd_read_nocrc(struct mcp251xfd_buf_cmd *cmd, u16 addr)
6338c2ecf20Sopenharmony_ci{
6348c2ecf20Sopenharmony_ci	cmd->cmd = cpu_to_be16(MCP251XFD_SPI_INSTRUCTION_READ | addr);
6358c2ecf20Sopenharmony_ci}
6368c2ecf20Sopenharmony_ci
6378c2ecf20Sopenharmony_cistatic inline void
6388c2ecf20Sopenharmony_cimcp251xfd_spi_cmd_write_nocrc(struct mcp251xfd_buf_cmd *cmd, u16 addr)
6398c2ecf20Sopenharmony_ci{
6408c2ecf20Sopenharmony_ci	cmd->cmd = cpu_to_be16(MCP251XFD_SPI_INSTRUCTION_WRITE | addr);
6418c2ecf20Sopenharmony_ci}
6428c2ecf20Sopenharmony_ci
6438c2ecf20Sopenharmony_cistatic inline bool mcp251xfd_reg_in_ram(unsigned int reg)
6448c2ecf20Sopenharmony_ci{
6458c2ecf20Sopenharmony_ci	static const struct regmap_range range =
6468c2ecf20Sopenharmony_ci		regmap_reg_range(MCP251XFD_RAM_START,
6478c2ecf20Sopenharmony_ci				 MCP251XFD_RAM_START + MCP251XFD_RAM_SIZE - 4);
6488c2ecf20Sopenharmony_ci
6498c2ecf20Sopenharmony_ci	return regmap_reg_in_range(reg, &range);
6508c2ecf20Sopenharmony_ci}
6518c2ecf20Sopenharmony_ci
6528c2ecf20Sopenharmony_cistatic inline void
6538c2ecf20Sopenharmony_ci__mcp251xfd_spi_cmd_crc_set_len(struct mcp251xfd_buf_cmd_crc *cmd,
6548c2ecf20Sopenharmony_ci				u16 len, bool in_ram)
6558c2ecf20Sopenharmony_ci{
6568c2ecf20Sopenharmony_ci	/* Number of u32 for RAM access, number of u8 otherwise. */
6578c2ecf20Sopenharmony_ci	if (in_ram)
6588c2ecf20Sopenharmony_ci		cmd->len = len >> 2;
6598c2ecf20Sopenharmony_ci	else
6608c2ecf20Sopenharmony_ci		cmd->len = len;
6618c2ecf20Sopenharmony_ci}
6628c2ecf20Sopenharmony_ci
6638c2ecf20Sopenharmony_cistatic inline void
6648c2ecf20Sopenharmony_cimcp251xfd_spi_cmd_crc_set_len_in_ram(struct mcp251xfd_buf_cmd_crc *cmd, u16 len)
6658c2ecf20Sopenharmony_ci{
6668c2ecf20Sopenharmony_ci	__mcp251xfd_spi_cmd_crc_set_len(cmd, len, true);
6678c2ecf20Sopenharmony_ci}
6688c2ecf20Sopenharmony_ci
6698c2ecf20Sopenharmony_cistatic inline void
6708c2ecf20Sopenharmony_cimcp251xfd_spi_cmd_crc_set_len_in_reg(struct mcp251xfd_buf_cmd_crc *cmd, u16 len)
6718c2ecf20Sopenharmony_ci{
6728c2ecf20Sopenharmony_ci	__mcp251xfd_spi_cmd_crc_set_len(cmd, len, false);
6738c2ecf20Sopenharmony_ci}
6748c2ecf20Sopenharmony_ci
6758c2ecf20Sopenharmony_cistatic inline void
6768c2ecf20Sopenharmony_cimcp251xfd_spi_cmd_read_crc_set_addr(struct mcp251xfd_buf_cmd_crc *cmd, u16 addr)
6778c2ecf20Sopenharmony_ci{
6788c2ecf20Sopenharmony_ci	cmd->cmd = cpu_to_be16(MCP251XFD_SPI_INSTRUCTION_READ_CRC | addr);
6798c2ecf20Sopenharmony_ci}
6808c2ecf20Sopenharmony_ci
6818c2ecf20Sopenharmony_cistatic inline void
6828c2ecf20Sopenharmony_cimcp251xfd_spi_cmd_read_crc(struct mcp251xfd_buf_cmd_crc *cmd,
6838c2ecf20Sopenharmony_ci			   u16 addr, u16 len)
6848c2ecf20Sopenharmony_ci{
6858c2ecf20Sopenharmony_ci	mcp251xfd_spi_cmd_read_crc_set_addr(cmd, addr);
6868c2ecf20Sopenharmony_ci	__mcp251xfd_spi_cmd_crc_set_len(cmd, len, mcp251xfd_reg_in_ram(addr));
6878c2ecf20Sopenharmony_ci}
6888c2ecf20Sopenharmony_ci
6898c2ecf20Sopenharmony_cistatic inline void
6908c2ecf20Sopenharmony_cimcp251xfd_spi_cmd_write_crc_set_addr(struct mcp251xfd_buf_cmd_crc *cmd,
6918c2ecf20Sopenharmony_ci				     u16 addr)
6928c2ecf20Sopenharmony_ci{
6938c2ecf20Sopenharmony_ci	cmd->cmd = cpu_to_be16(MCP251XFD_SPI_INSTRUCTION_WRITE_CRC | addr);
6948c2ecf20Sopenharmony_ci}
6958c2ecf20Sopenharmony_ci
6968c2ecf20Sopenharmony_cistatic inline void
6978c2ecf20Sopenharmony_cimcp251xfd_spi_cmd_write_crc(struct mcp251xfd_buf_cmd_crc *cmd,
6988c2ecf20Sopenharmony_ci			    u16 addr, u16 len)
6998c2ecf20Sopenharmony_ci{
7008c2ecf20Sopenharmony_ci	mcp251xfd_spi_cmd_write_crc_set_addr(cmd, addr);
7018c2ecf20Sopenharmony_ci	__mcp251xfd_spi_cmd_crc_set_len(cmd, len, mcp251xfd_reg_in_ram(addr));
7028c2ecf20Sopenharmony_ci}
7038c2ecf20Sopenharmony_ci
7048c2ecf20Sopenharmony_cistatic inline u8 *
7058c2ecf20Sopenharmony_cimcp251xfd_spi_cmd_write(const struct mcp251xfd_priv *priv,
7068c2ecf20Sopenharmony_ci			union mcp251xfd_write_reg_buf *write_reg_buf,
7078c2ecf20Sopenharmony_ci			u16 addr)
7088c2ecf20Sopenharmony_ci{
7098c2ecf20Sopenharmony_ci	u8 *data;
7108c2ecf20Sopenharmony_ci
7118c2ecf20Sopenharmony_ci	if (priv->devtype_data.quirks & MCP251XFD_QUIRK_CRC_REG) {
7128c2ecf20Sopenharmony_ci		mcp251xfd_spi_cmd_write_crc_set_addr(&write_reg_buf->crc.cmd,
7138c2ecf20Sopenharmony_ci						     addr);
7148c2ecf20Sopenharmony_ci		data = write_reg_buf->crc.data;
7158c2ecf20Sopenharmony_ci	} else {
7168c2ecf20Sopenharmony_ci		mcp251xfd_spi_cmd_write_nocrc(&write_reg_buf->nocrc.cmd,
7178c2ecf20Sopenharmony_ci					      addr);
7188c2ecf20Sopenharmony_ci		data = write_reg_buf->nocrc.data;
7198c2ecf20Sopenharmony_ci	}
7208c2ecf20Sopenharmony_ci
7218c2ecf20Sopenharmony_ci	return data;
7228c2ecf20Sopenharmony_ci}
7238c2ecf20Sopenharmony_ci
7248c2ecf20Sopenharmony_cistatic inline u16 mcp251xfd_get_tef_obj_addr(u8 n)
7258c2ecf20Sopenharmony_ci{
7268c2ecf20Sopenharmony_ci	return MCP251XFD_RAM_START +
7278c2ecf20Sopenharmony_ci		sizeof(struct mcp251xfd_hw_tef_obj) * n;
7288c2ecf20Sopenharmony_ci}
7298c2ecf20Sopenharmony_ci
7308c2ecf20Sopenharmony_cistatic inline u16
7318c2ecf20Sopenharmony_cimcp251xfd_get_tx_obj_addr(const struct mcp251xfd_tx_ring *ring, u8 n)
7328c2ecf20Sopenharmony_ci{
7338c2ecf20Sopenharmony_ci	return ring->base + ring->obj_size * n;
7348c2ecf20Sopenharmony_ci}
7358c2ecf20Sopenharmony_ci
7368c2ecf20Sopenharmony_cistatic inline u16
7378c2ecf20Sopenharmony_cimcp251xfd_get_rx_obj_addr(const struct mcp251xfd_rx_ring *ring, u8 n)
7388c2ecf20Sopenharmony_ci{
7398c2ecf20Sopenharmony_ci	return ring->base + ring->obj_size * n;
7408c2ecf20Sopenharmony_ci}
7418c2ecf20Sopenharmony_ci
7428c2ecf20Sopenharmony_cistatic inline u8 mcp251xfd_get_tef_head(const struct mcp251xfd_priv *priv)
7438c2ecf20Sopenharmony_ci{
7448c2ecf20Sopenharmony_ci	return priv->tef.head & (priv->tx->obj_num - 1);
7458c2ecf20Sopenharmony_ci}
7468c2ecf20Sopenharmony_ci
7478c2ecf20Sopenharmony_cistatic inline u8 mcp251xfd_get_tef_tail(const struct mcp251xfd_priv *priv)
7488c2ecf20Sopenharmony_ci{
7498c2ecf20Sopenharmony_ci	return priv->tef.tail & (priv->tx->obj_num - 1);
7508c2ecf20Sopenharmony_ci}
7518c2ecf20Sopenharmony_ci
7528c2ecf20Sopenharmony_cistatic inline u8 mcp251xfd_get_tef_len(const struct mcp251xfd_priv *priv)
7538c2ecf20Sopenharmony_ci{
7548c2ecf20Sopenharmony_ci	return priv->tef.head - priv->tef.tail;
7558c2ecf20Sopenharmony_ci}
7568c2ecf20Sopenharmony_ci
7578c2ecf20Sopenharmony_cistatic inline u8 mcp251xfd_get_tef_linear_len(const struct mcp251xfd_priv *priv)
7588c2ecf20Sopenharmony_ci{
7598c2ecf20Sopenharmony_ci	u8 len;
7608c2ecf20Sopenharmony_ci
7618c2ecf20Sopenharmony_ci	len = mcp251xfd_get_tef_len(priv);
7628c2ecf20Sopenharmony_ci
7638c2ecf20Sopenharmony_ci	return min_t(u8, len, priv->tx->obj_num - mcp251xfd_get_tef_tail(priv));
7648c2ecf20Sopenharmony_ci}
7658c2ecf20Sopenharmony_ci
7668c2ecf20Sopenharmony_cistatic inline u8 mcp251xfd_get_tx_head(const struct mcp251xfd_tx_ring *ring)
7678c2ecf20Sopenharmony_ci{
7688c2ecf20Sopenharmony_ci	return ring->head & (ring->obj_num - 1);
7698c2ecf20Sopenharmony_ci}
7708c2ecf20Sopenharmony_ci
7718c2ecf20Sopenharmony_cistatic inline u8 mcp251xfd_get_tx_tail(const struct mcp251xfd_tx_ring *ring)
7728c2ecf20Sopenharmony_ci{
7738c2ecf20Sopenharmony_ci	return ring->tail & (ring->obj_num - 1);
7748c2ecf20Sopenharmony_ci}
7758c2ecf20Sopenharmony_ci
7768c2ecf20Sopenharmony_cistatic inline u8 mcp251xfd_get_tx_free(const struct mcp251xfd_tx_ring *ring)
7778c2ecf20Sopenharmony_ci{
7788c2ecf20Sopenharmony_ci	return ring->obj_num - (ring->head - ring->tail);
7798c2ecf20Sopenharmony_ci}
7808c2ecf20Sopenharmony_ci
7818c2ecf20Sopenharmony_cistatic inline int
7828c2ecf20Sopenharmony_cimcp251xfd_get_tx_nr_by_addr(const struct mcp251xfd_tx_ring *tx_ring, u8 *nr,
7838c2ecf20Sopenharmony_ci			    u16 addr)
7848c2ecf20Sopenharmony_ci{
7858c2ecf20Sopenharmony_ci	if (addr < mcp251xfd_get_tx_obj_addr(tx_ring, 0) ||
7868c2ecf20Sopenharmony_ci	    addr >= mcp251xfd_get_tx_obj_addr(tx_ring, tx_ring->obj_num))
7878c2ecf20Sopenharmony_ci		return -ENOENT;
7888c2ecf20Sopenharmony_ci
7898c2ecf20Sopenharmony_ci	*nr = (addr - mcp251xfd_get_tx_obj_addr(tx_ring, 0)) /
7908c2ecf20Sopenharmony_ci		tx_ring->obj_size;
7918c2ecf20Sopenharmony_ci
7928c2ecf20Sopenharmony_ci	return 0;
7938c2ecf20Sopenharmony_ci}
7948c2ecf20Sopenharmony_ci
7958c2ecf20Sopenharmony_cistatic inline u8 mcp251xfd_get_rx_head(const struct mcp251xfd_rx_ring *ring)
7968c2ecf20Sopenharmony_ci{
7978c2ecf20Sopenharmony_ci	return ring->head & (ring->obj_num - 1);
7988c2ecf20Sopenharmony_ci}
7998c2ecf20Sopenharmony_ci
8008c2ecf20Sopenharmony_cistatic inline u8 mcp251xfd_get_rx_tail(const struct mcp251xfd_rx_ring *ring)
8018c2ecf20Sopenharmony_ci{
8028c2ecf20Sopenharmony_ci	return ring->tail & (ring->obj_num - 1);
8038c2ecf20Sopenharmony_ci}
8048c2ecf20Sopenharmony_ci
8058c2ecf20Sopenharmony_cistatic inline u8 mcp251xfd_get_rx_len(const struct mcp251xfd_rx_ring *ring)
8068c2ecf20Sopenharmony_ci{
8078c2ecf20Sopenharmony_ci	return ring->head - ring->tail;
8088c2ecf20Sopenharmony_ci}
8098c2ecf20Sopenharmony_ci
8108c2ecf20Sopenharmony_cistatic inline u8
8118c2ecf20Sopenharmony_cimcp251xfd_get_rx_linear_len(const struct mcp251xfd_rx_ring *ring)
8128c2ecf20Sopenharmony_ci{
8138c2ecf20Sopenharmony_ci	u8 len;
8148c2ecf20Sopenharmony_ci
8158c2ecf20Sopenharmony_ci	len = mcp251xfd_get_rx_len(ring);
8168c2ecf20Sopenharmony_ci
8178c2ecf20Sopenharmony_ci	return min_t(u8, len, ring->obj_num - mcp251xfd_get_rx_tail(ring));
8188c2ecf20Sopenharmony_ci}
8198c2ecf20Sopenharmony_ci
8208c2ecf20Sopenharmony_ci#define mcp251xfd_for_each_tx_obj(ring, _obj, n) \
8218c2ecf20Sopenharmony_ci	for ((n) = 0, (_obj) = &(ring)->obj[(n)]; \
8228c2ecf20Sopenharmony_ci	     (n) < (ring)->obj_num; \
8238c2ecf20Sopenharmony_ci	     (n)++, (_obj) = &(ring)->obj[(n)])
8248c2ecf20Sopenharmony_ci
8258c2ecf20Sopenharmony_ci#define mcp251xfd_for_each_rx_ring(priv, ring, n) \
8268c2ecf20Sopenharmony_ci	for ((n) = 0, (ring) = *((priv)->rx + (n)); \
8278c2ecf20Sopenharmony_ci	     (n) < (priv)->rx_ring_num; \
8288c2ecf20Sopenharmony_ci	     (n)++, (ring) = *((priv)->rx + (n)))
8298c2ecf20Sopenharmony_ci
8308c2ecf20Sopenharmony_ciint mcp251xfd_regmap_init(struct mcp251xfd_priv *priv);
8318c2ecf20Sopenharmony_ciu16 mcp251xfd_crc16_compute2(const void *cmd, size_t cmd_size,
8328c2ecf20Sopenharmony_ci			     const void *data, size_t data_size);
8338c2ecf20Sopenharmony_ciu16 mcp251xfd_crc16_compute(const void *data, size_t data_size);
8348c2ecf20Sopenharmony_ci
8358c2ecf20Sopenharmony_ci#endif
836