2 * CAN driver for PEAK System micro-CAN based adapters
4 * Copyright (C) 2003-2011 PEAK System-Technik GmbH
5 * Copyright (C) 2011-2013 Stephane Grosjean <s.grosjean@peak-system.com>
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published
9 * by the Free Software Foundation; version 2 of the License.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
19 /* uCAN commands opcodes list (low-order 10 bits) */
20 #define PUCAN_CMD_NOP 0x000
21 #define PUCAN_CMD_RESET_MODE 0x001
22 #define PUCAN_CMD_NORMAL_MODE 0x002
23 #define PUCAN_CMD_LISTEN_ONLY_MODE 0x003
24 #define PUCAN_CMD_TIMING_SLOW 0x004
25 #define PUCAN_CMD_TIMING_FAST 0x005
26 #define PUCAN_CMD_SET_STD_FILTER 0x006
27 #define PUCAN_CMD_RESERVED2 0x007
28 #define PUCAN_CMD_FILTER_STD 0x008
29 #define PUCAN_CMD_TX_ABORT 0x009
30 #define PUCAN_CMD_WR_ERR_CNT 0x00a
31 #define PUCAN_CMD_SET_EN_OPTION 0x00b
32 #define PUCAN_CMD_CLR_DIS_OPTION 0x00c
33 #define PUCAN_CMD_RX_BARRIER 0x010
34 #define PUCAN_CMD_END_OF_COLLECTION 0x3ff
36 /* uCAN received messages list */
37 #define PUCAN_MSG_CAN_RX 0x0001
38 #define PUCAN_MSG_ERROR 0x0002
39 #define PUCAN_MSG_STATUS 0x0003
40 #define PUCAN_MSG_BUSLOAD 0x0004
42 #define PUCAN_MSG_CACHE_CRITICAL 0x0102
44 /* uCAN transmitted messages */
45 #define PUCAN_MSG_CAN_TX 0x1000
47 /* uCAN command common header */
48 struct __packed pucan_command
{
49 __le16 opcode_channel
;
53 /* return the opcode from the opcode_channel field of a command */
54 static inline u16
pucan_cmd_get_opcode(struct pucan_command
*c
)
56 return le16_to_cpu(c
->opcode_channel
) & 0x3ff;
59 #define PUCAN_TSLOW_BRP_BITS 10
60 #define PUCAN_TSLOW_TSGEG1_BITS 8
61 #define PUCAN_TSLOW_TSGEG2_BITS 7
62 #define PUCAN_TSLOW_SJW_BITS 7
64 #define PUCAN_TSLOW_BRP_MASK ((1 << PUCAN_TSLOW_BRP_BITS) - 1)
65 #define PUCAN_TSLOW_TSEG1_MASK ((1 << PUCAN_TSLOW_TSGEG1_BITS) - 1)
66 #define PUCAN_TSLOW_TSEG2_MASK ((1 << PUCAN_TSLOW_TSGEG2_BITS) - 1)
67 #define PUCAN_TSLOW_SJW_MASK ((1 << PUCAN_TSLOW_SJW_BITS) - 1)
69 /* uCAN TIMING_SLOW command fields */
70 #define PUCAN_TSLOW_SJW_T(s, t) (((s) & PUCAN_TSLOW_SJW_MASK) | \
72 #define PUCAN_TSLOW_TSEG2(t) ((t) & PUCAN_TSLOW_TSEG2_MASK)
73 #define PUCAN_TSLOW_TSEG1(t) ((t) & PUCAN_TSLOW_TSEG1_MASK)
74 #define PUCAN_TSLOW_BRP(b) ((b) & PUCAN_TSLOW_BRP_MASK)
76 struct __packed pucan_timing_slow
{
77 __le16 opcode_channel
;
79 u8 ewl
; /* Error Warning limit */
80 u8 sjw_t
; /* Sync Jump Width + Triple sampling */
81 u8 tseg2
; /* Timing SEGment 2 */
82 u8 tseg1
; /* Timing SEGment 1 */
84 __le16 brp
; /* BaudRate Prescaler */
87 #define PUCAN_TFAST_BRP_BITS 10
88 #define PUCAN_TFAST_TSGEG1_BITS 5
89 #define PUCAN_TFAST_TSGEG2_BITS 4
90 #define PUCAN_TFAST_SJW_BITS 4
92 #define PUCAN_TFAST_BRP_MASK ((1 << PUCAN_TFAST_BRP_BITS) - 1)
93 #define PUCAN_TFAST_TSEG1_MASK ((1 << PUCAN_TFAST_TSGEG1_BITS) - 1)
94 #define PUCAN_TFAST_TSEG2_MASK ((1 << PUCAN_TFAST_TSGEG2_BITS) - 1)
95 #define PUCAN_TFAST_SJW_MASK ((1 << PUCAN_TFAST_SJW_BITS) - 1)
97 /* uCAN TIMING_FAST command fields */
98 #define PUCAN_TFAST_SJW(s) ((s) & PUCAN_TFAST_SJW_MASK)
99 #define PUCAN_TFAST_TSEG2(t) ((t) & PUCAN_TFAST_TSEG2_MASK)
100 #define PUCAN_TFAST_TSEG1(t) ((t) & PUCAN_TFAST_TSEG1_MASK)
101 #define PUCAN_TFAST_BRP(b) ((b) & PUCAN_TFAST_BRP_MASK)
103 struct __packed pucan_timing_fast
{
104 __le16 opcode_channel
;
107 u8 sjw
; /* Sync Jump Width */
108 u8 tseg2
; /* Timing SEGment 2 */
109 u8 tseg1
; /* Timing SEGment 1 */
111 __le16 brp
; /* BaudRate Prescaler */
114 /* uCAN FILTER_STD command fields */
115 #define PUCAN_FLTSTD_ROW_IDX_BITS 6
117 struct __packed pucan_filter_std
{
118 __le16 opcode_channel
;
121 __le32 mask
; /* CAN-ID bitmask in idx range */
124 #define PUCAN_FLTSTD_ROW_IDX_MAX ((1 << PUCAN_FLTSTD_ROW_IDX_BITS) - 1)
126 /* uCAN SET_STD_FILTER command fields */
127 struct __packed pucan_std_filter
{
128 __le16 opcode_channel
;
132 __le32 mask
; /* CAN-ID bitmask in idx range */
135 /* uCAN TX_ABORT commands fields */
136 #define PUCAN_TX_ABORT_FLUSH 0x0001
138 struct __packed pucan_tx_abort
{
139 __le16 opcode_channel
;
145 /* uCAN WR_ERR_CNT command fields */
146 #define PUCAN_WRERRCNT_TE 0x4000 /* Tx error cntr write Enable */
147 #define PUCAN_WRERRCNT_RE 0x8000 /* Rx error cntr write Enable */
149 struct __packed pucan_wr_err_cnt
{
150 __le16 opcode_channel
;
153 u8 tx_counter
; /* Tx error counter new value */
154 u8 rx_counter
; /* Rx error counter new value */
159 /* uCAN SET_EN/CLR_DIS _OPTION command fields */
160 #define PUCAN_OPTION_ERROR 0x0001
161 #define PUCAN_OPTION_BUSLOAD 0x0002
162 #define PUCAN_OPTION_CANDFDISO 0x0004
164 struct __packed pucan_options
{
165 __le16 opcode_channel
;
171 /* uCAN received messages global format */
172 struct __packed pucan_msg
{
179 /* uCAN flags for CAN/CANFD messages */
180 #define PUCAN_MSG_SELF_RECEIVE 0x80
181 #define PUCAN_MSG_ERROR_STATE_IND 0x40 /* error state indicator */
182 #define PUCAN_MSG_BITRATE_SWITCH 0x20 /* bitrate switch */
183 #define PUCAN_MSG_EXT_DATA_LEN 0x10 /* extended data length */
184 #define PUCAN_MSG_SINGLE_SHOT 0x08
185 #define PUCAN_MSG_LOOPED_BACK 0x04
186 #define PUCAN_MSG_EXT_ID 0x02
187 #define PUCAN_MSG_RTR 0x01
189 struct __packed pucan_rx_msg
{
203 /* uCAN error types */
204 #define PUCAN_ERMSG_BIT_ERROR 0
205 #define PUCAN_ERMSG_FORM_ERROR 1
206 #define PUCAN_ERMSG_STUFF_ERROR 2
207 #define PUCAN_ERMSG_OTHER_ERROR 3
208 #define PUCAN_ERMSG_ERR_CNT_DEC 4
210 struct __packed pucan_error_msg
{
221 static inline int pucan_error_get_channel(const struct pucan_error_msg
*msg
)
223 return msg
->channel_type_d
& 0x0f;
226 #define PUCAN_RX_BARRIER 0x10
227 #define PUCAN_BUS_PASSIVE 0x20
228 #define PUCAN_BUS_WARNING 0x40
229 #define PUCAN_BUS_BUSOFF 0x80
231 struct __packed pucan_status_msg
{
240 static inline int pucan_status_get_channel(const struct pucan_status_msg
*msg
)
242 return msg
->channel_p_w_b
& 0x0f;
245 static inline int pucan_status_is_rx_barrier(const struct pucan_status_msg
*msg
)
247 return msg
->channel_p_w_b
& PUCAN_RX_BARRIER
;
250 static inline int pucan_status_is_passive(const struct pucan_status_msg
*msg
)
252 return msg
->channel_p_w_b
& PUCAN_BUS_PASSIVE
;
255 static inline int pucan_status_is_warning(const struct pucan_status_msg
*msg
)
257 return msg
->channel_p_w_b
& PUCAN_BUS_WARNING
;
260 static inline int pucan_status_is_busoff(const struct pucan_status_msg
*msg
)
262 return msg
->channel_p_w_b
& PUCAN_BUS_BUSOFF
;
265 /* uCAN transmitted message format */
266 #define PUCAN_MSG_CHANNEL_DLC(c, d) (((c) & 0xf) | ((d) << 4))
268 struct __packed pucan_tx_msg
{
280 /* build the cmd opcode_channel field with respect to the correct endianness */
281 static inline __le16
pucan_cmd_opcode_channel(int index
, int opcode
)
283 return cpu_to_le16(((index
) << 12) | ((opcode
) & 0x3ff));
286 /* return the channel number part from any received message channel_dlc field */
287 static inline int pucan_msg_get_channel(const struct pucan_rx_msg
*msg
)
289 return msg
->channel_dlc
& 0xf;
292 /* return the dlc value from any received message channel_dlc field */
293 static inline int pucan_msg_get_dlc(const struct pucan_rx_msg
*msg
)
295 return msg
->channel_dlc
>> 4;
298 static inline int pucan_ermsg_get_channel(const struct pucan_error_msg
*msg
)
300 return msg
->channel_type_d
& 0x0f;
303 static inline int pucan_stmsg_get_channel(const struct pucan_status_msg
*msg
)
305 return msg
->channel_p_w_b
& 0x0f;