1 /*
2  * Copyright (c) 2022 Henrik Brix Andersen <henrik@brixandersen.dk>
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #ifndef ZEPHYR_DRIVERS_CAN_SJA1000_PRIV_H_
8 #define ZEPHYR_DRIVERS_CAN_SJA1000_PRIV_H_
9 
10 #include <zephyr/sys/util.h>
11 
12 /* SJA1000 register "CAN addresses", PeliCAN mode */
13 #define CAN_SJA1000_MOD			     (0U)
14 #define CAN_SJA1000_CMR			     (1U)
15 #define CAN_SJA1000_SR			     (2U)
16 #define CAN_SJA1000_IR			     (3U)
17 #define CAN_SJA1000_IER			     (4U)
18 #define CAN_SJA1000_BTR0		     (6U)
19 #define CAN_SJA1000_BTR1		     (7U)
20 #define CAN_SJA1000_OCR			     (8U)
21 #define CAN_SJA1000_ALC			     (11U)
22 #define CAN_SJA1000_ECC			     (12U)
23 #define CAN_SJA1000_EWLR		     (13U)
24 #define CAN_SJA1000_RXERR		     (14U)
25 #define CAN_SJA1000_TXERR		     (15U)
26 
27 /* Reset Mode access (acceptance codes/masks) */
28 #define CAN_SJA1000_ACR0		     (16U)
29 #define CAN_SJA1000_ACR1		     (17U)
30 #define CAN_SJA1000_ACR2		     (18U)
31 #define CAN_SJA1000_ACR3		     (19U)
32 #define CAN_SJA1000_AMR0		     (20U)
33 #define CAN_SJA1000_AMR1		     (21U)
34 #define CAN_SJA1000_AMR2		     (22U)
35 #define CAN_SJA1000_AMR3		     (23U)
36 
37 /* Operation Mode access (RX/TX SFF/EFF frame) */
38 #define CAN_SJA1000_FRAME_INFO		     (16U)
39 #define CAN_SJA1000_XFF_ID1		     (17U)
40 #define CAN_SJA1000_XFF_ID2		     (18U)
41 #define CAN_SJA1000_EFF_ID3		     (19U)
42 #define CAN_SJA1000_EFF_ID4		     (20U)
43 #define CAN_SJA1000_SFF_DATA		     (19U)
44 #define CAN_SJA1000_EFF_DATA		     (21U)
45 
46 #define CAN_SJA1000_RMC			     (29U)
47 #define CAN_SJA1000_RBSA		     (30U)
48 #define CAN_SJA1000_CDR			     (31U)
49 
50 /* Mode register (MOD) bits */
51 #define CAN_SJA1000_MOD_RM		     BIT(0)
52 #define CAN_SJA1000_MOD_LOM		     BIT(1)
53 #define CAN_SJA1000_MOD_STM		     BIT(2)
54 #define CAN_SJA1000_MOD_AFM		     BIT(3)
55 #define CAN_SJA1000_MOD_SM		     BIT(4)
56 
57 /* Command Register (CMR) bits */
58 #define CAN_SJA1000_CMR_TR		     BIT(0)
59 #define CAN_SJA1000_CMR_AT		     BIT(1)
60 #define CAN_SJA1000_CMR_RRB		     BIT(2)
61 #define CAN_SJA1000_CMR_CDO		     BIT(3)
62 #define CAN_SJA1000_CMR_SRR		     BIT(4)
63 
64 /* Status Register (SR) bits */
65 #define CAN_SJA1000_SR_RBS		     BIT(0)
66 #define CAN_SJA1000_SR_DOS		     BIT(1)
67 #define CAN_SJA1000_SR_TBS		     BIT(2)
68 #define CAN_SJA1000_SR_TCS		     BIT(3)
69 #define CAN_SJA1000_SR_RS		     BIT(4)
70 #define CAN_SJA1000_SR_TS		     BIT(5)
71 #define CAN_SJA1000_SR_ES		     BIT(6)
72 #define CAN_SJA1000_SR_BS		     BIT(7)
73 
74 /* Interrupt Register (IR) bits */
75 #define CAN_SJA1000_IR_RI		     BIT(0)
76 #define CAN_SJA1000_IR_TI		     BIT(1)
77 #define CAN_SJA1000_IR_EI		     BIT(2)
78 #define CAN_SJA1000_IR_DOI		     BIT(3)
79 #define CAN_SJA1000_IR_WUI		     BIT(4)
80 #define CAN_SJA1000_IR_EPI		     BIT(5)
81 #define CAN_SJA1000_IR_ALI		     BIT(6)
82 #define CAN_SJA1000_IR_BEI		     BIT(7)
83 
84 /* Interrupt Enable Register (IER) bits */
85 #define CAN_SJA1000_IER_RIE		     BIT(0)
86 #define CAN_SJA1000_IER_TIE		     BIT(1)
87 #define CAN_SJA1000_IER_EIE		     BIT(2)
88 #define CAN_SJA1000_IER_DOIE		     BIT(3)
89 #define CAN_SJA1000_IER_WUIE		     BIT(4)
90 #define CAN_SJA1000_IER_EPIE		     BIT(5)
91 #define CAN_SJA1000_IER_ALIE		     BIT(6)
92 #define CAN_SJA1000_IER_BEIE		     BIT(7)
93 
94 /* Bus Timing Register 0 (BTR0) bits */
95 #define CAN_SJA1000_BTR0_BRP_MASK	     GENMASK(5, 0)
96 #define CAN_SJA1000_BTR0_SJW_MASK	     GENMASK(7, 6)
97 
98 #define CAN_SJA1000_BTR0_BRP_PREP(brp)	     FIELD_PREP(CAN_SJA1000_BTR0_BRP_MASK, brp)
99 #define CAN_SJA1000_BTR0_SJW_PREP(sjw)	     FIELD_PREP(CAN_SJA1000_BTR0_SJW_MASK, sjw)
100 
101 /* Bus Timing Register 1 (BTR1) bits */
102 #define CAN_SJA1000_BTR1_TSEG1_MASK	     GENMASK(3, 0)
103 #define CAN_SJA1000_BTR1_TSEG2_MASK	     GENMASK(6, 4)
104 #define CAN_SJA1000_BTR1_SAM		     BIT(7)
105 
106 #define CAN_SJA1000_BTR1_TSEG1_PREP(tseg1)   FIELD_PREP(CAN_SJA1000_BTR1_TSEG1_MASK, tseg1)
107 #define CAN_SJA1000_BTR1_TSEG2_PREP(tseg2)   FIELD_PREP(CAN_SJA1000_BTR1_TSEG2_MASK, tseg2)
108 
109 /* Error Code Capture register (ECC) bits */
110 #define CAN_SJA1000_ECC_SEG_MASK	     GENMASK(4, 0)
111 #define CAN_SJA1000_ECC_DIR		     BIT(5)
112 #define CAN_SJA1000_ECC_ERRC_MASK	     GENMASK(7, 6)
113 
114 #define CAN_SJA1000_ECC_ERRC_BIT_ERROR	     FIELD_PREP(CAN_SJA1000_ECC_ERRC_MASK, 0U)
115 #define CAN_SJA1000_ECC_ERRC_FORM_ERROR	     FIELD_PREP(CAN_SJA1000_ECC_ERRC_MASK, 1U)
116 #define CAN_SJA1000_ECC_ERRC_STUFF_ERROR     FIELD_PREP(CAN_SJA1000_ECC_ERRC_MASK, 2U)
117 #define CAN_SJA1000_ECC_ERRC_OTHER_ERROR     FIELD_PREP(CAN_SJA1000_ECC_ERRC_MASK, 3U)
118 
119 /* RX/TX SFF/EFF Frame Information bits */
120 #define CAN_SJA1000_FRAME_INFO_DLC_MASK	     GENMASK(3, 0)
121 #define CAN_SJA1000_FRAME_INFO_RTR	     BIT(6)
122 #define CAN_SJA1000_FRAME_INFO_FF	     BIT(7)
123 
124 #define CAN_SJA1000_FRAME_INFO_DLC_PREP(dlc) FIELD_PREP(CAN_SJA1000_FRAME_INFO_DLC_MASK, dlc)
125 #define CAN_SJA1000_FRAME_INFO_DLC_GET(info) FIELD_GET(CAN_SJA1000_FRAME_INFO_DLC_MASK, info)
126 
127 #endif /* ZEPHYR_DRIVERS_CAN_SJA1000_PRIV_H_ */
128