1 /*
2  * CAN driver for PEAK System micro-CAN based adapters
3  *
4  * Copyright (C) 2003-2011 PEAK System-Technik GmbH
5  * Copyright (C) 2011-2013 Stephane Grosjean <s.grosjean@peak-system.com>
6  *
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.
10  *
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.
15  */
16 #ifndef PUCAN_H
17 #define PUCAN_H
18 
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
35 
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
41 
42 #define PUCAN_MSG_CACHE_CRITICAL	0x0102
43 
44 /* uCAN transmitted messages */
45 #define PUCAN_MSG_CAN_TX		0x1000
46 
47 /* uCAN command common header */
48 struct __packed pucan_command {
49 	__le16	opcode_channel;
50 	u16	args[3];
51 };
52 
53 /* return the opcode from the opcode_channel field of a command */
pucan_cmd_get_opcode(struct pucan_command * c)54 static inline u16 pucan_cmd_get_opcode(struct pucan_command *c)
55 {
56 	return le16_to_cpu(c->opcode_channel) & 0x3ff;
57 }
58 
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
63 
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)
68 
69 /* uCAN TIMING_SLOW command fields */
70 #define PUCAN_TSLOW_SJW_T(s, t)		(((s) & PUCAN_TSLOW_SJW_MASK) | \
71 								((!!(t)) << 7))
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)
75 
76 struct __packed pucan_timing_slow {
77 	__le16	opcode_channel;
78 
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 */
83 
84 	__le16	brp;		/* BaudRate Prescaler */
85 };
86 
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
91 
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)
96 
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)
102 
103 struct __packed pucan_timing_fast {
104 	__le16	opcode_channel;
105 
106 	u8	unused;
107 	u8	sjw;		/* Sync Jump Width */
108 	u8	tseg2;		/* Timing SEGment 2 */
109 	u8	tseg1;		/* Timing SEGment 1 */
110 
111 	__le16	brp;		/* BaudRate Prescaler */
112 };
113 
114 /* uCAN FILTER_STD command fields */
115 #define PUCAN_FLTSTD_ROW_IDX_BITS	6
116 
117 struct __packed pucan_filter_std {
118 	__le16	opcode_channel;
119 
120 	__le16	idx;
121 	__le32	mask;		/* CAN-ID bitmask in idx range */
122 };
123 
124 #define PUCAN_FLTSTD_ROW_IDX_MAX	((1 << PUCAN_FLTSTD_ROW_IDX_BITS) - 1)
125 
126 /* uCAN SET_STD_FILTER command fields */
127 struct __packed pucan_std_filter {
128 	__le16	opcode_channel;
129 
130 	u8	unused;
131 	u8	idx;
132 	__le32	mask;		/* CAN-ID bitmask in idx range */
133 };
134 
135 /* uCAN TX_ABORT commands fields */
136 #define PUCAN_TX_ABORT_FLUSH		0x0001
137 
138 struct __packed pucan_tx_abort {
139 	__le16	opcode_channel;
140 
141 	__le16	flags;
142 	u32	unused;
143 };
144 
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 */
148 
149 struct __packed pucan_wr_err_cnt {
150 	__le16	opcode_channel;
151 
152 	__le16	sel_mask;
153 	u8	tx_counter;	/* Tx error counter new value */
154 	u8	rx_counter;	/* Rx error counter new value */
155 
156 	u16	unused;
157 };
158 
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
163 
164 struct __packed pucan_options {
165 	__le16	opcode_channel;
166 
167 	__le16	options;
168 	u32	unused;
169 };
170 
171 /* uCAN received messages global format */
172 struct __packed pucan_msg {
173 	__le16	size;
174 	__le16	type;
175 	__le32	ts_low;
176 	__le32	ts_high;
177 };
178 
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
188 
189 struct __packed pucan_rx_msg {
190 	__le16	size;
191 	__le16	type;
192 	__le32	ts_low;
193 	__le32	ts_high;
194 	__le32	tag_low;
195 	__le32	tag_high;
196 	u8	channel_dlc;
197 	u8	client;
198 	__le16	flags;
199 	__le32	can_id;
200 	u8	d[0];
201 };
202 
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
209 
210 struct __packed pucan_error_msg {
211 	__le16	size;
212 	__le16	type;
213 	__le32	ts_low;
214 	__le32	ts_high;
215 	u8	channel_type_d;
216 	u8	code_g;
217 	u8	tx_err_cnt;
218 	u8	rx_err_cnt;
219 };
220 
pucan_error_get_channel(const struct pucan_error_msg * msg)221 static inline int pucan_error_get_channel(const struct pucan_error_msg *msg)
222 {
223 	return msg->channel_type_d & 0x0f;
224 }
225 
226 #define PUCAN_RX_BARRIER		0x10
227 #define PUCAN_BUS_PASSIVE		0x20
228 #define PUCAN_BUS_WARNING		0x40
229 #define PUCAN_BUS_BUSOFF		0x80
230 
231 struct __packed pucan_status_msg {
232 	__le16	size;
233 	__le16	type;
234 	__le32	ts_low;
235 	__le32	ts_high;
236 	u8	channel_p_w_b;
237 	u8	unused[3];
238 };
239 
pucan_status_get_channel(const struct pucan_status_msg * msg)240 static inline int pucan_status_get_channel(const struct pucan_status_msg *msg)
241 {
242 	return msg->channel_p_w_b & 0x0f;
243 }
244 
pucan_status_is_rx_barrier(const struct pucan_status_msg * msg)245 static inline int pucan_status_is_rx_barrier(const struct pucan_status_msg *msg)
246 {
247 	return msg->channel_p_w_b & PUCAN_RX_BARRIER;
248 }
249 
pucan_status_is_passive(const struct pucan_status_msg * msg)250 static inline int pucan_status_is_passive(const struct pucan_status_msg *msg)
251 {
252 	return msg->channel_p_w_b & PUCAN_BUS_PASSIVE;
253 }
254 
pucan_status_is_warning(const struct pucan_status_msg * msg)255 static inline int pucan_status_is_warning(const struct pucan_status_msg *msg)
256 {
257 	return msg->channel_p_w_b & PUCAN_BUS_WARNING;
258 }
259 
pucan_status_is_busoff(const struct pucan_status_msg * msg)260 static inline int pucan_status_is_busoff(const struct pucan_status_msg *msg)
261 {
262 	return msg->channel_p_w_b & PUCAN_BUS_BUSOFF;
263 }
264 
265 /* uCAN transmitted message format */
266 #define PUCAN_MSG_CHANNEL_DLC(c, d)	(((c) & 0xf) | ((d) << 4))
267 
268 struct __packed pucan_tx_msg {
269 	__le16	size;
270 	__le16	type;
271 	__le32	tag_low;
272 	__le32	tag_high;
273 	u8	channel_dlc;
274 	u8	client;
275 	__le16	flags;
276 	__le32	can_id;
277 	u8	d[0];
278 };
279 
280 /* build the cmd opcode_channel field with respect to the correct endianness */
pucan_cmd_opcode_channel(int index,int opcode)281 static inline __le16 pucan_cmd_opcode_channel(int index, int opcode)
282 {
283 	return cpu_to_le16(((index) << 12) | ((opcode) & 0x3ff));
284 }
285 
286 /* return the channel number part from any received message channel_dlc field */
pucan_msg_get_channel(const struct pucan_rx_msg * msg)287 static inline int pucan_msg_get_channel(const struct pucan_rx_msg *msg)
288 {
289 	return msg->channel_dlc & 0xf;
290 }
291 
292 /* return the dlc value from any received message channel_dlc field */
pucan_msg_get_dlc(const struct pucan_rx_msg * msg)293 static inline int pucan_msg_get_dlc(const struct pucan_rx_msg *msg)
294 {
295 	return msg->channel_dlc >> 4;
296 }
297 
pucan_ermsg_get_channel(const struct pucan_error_msg * msg)298 static inline int pucan_ermsg_get_channel(const struct pucan_error_msg *msg)
299 {
300 	return msg->channel_type_d & 0x0f;
301 }
302 
pucan_stmsg_get_channel(const struct pucan_status_msg * msg)303 static inline int pucan_stmsg_get_channel(const struct pucan_status_msg *msg)
304 {
305 	return msg->channel_p_w_b & 0x0f;
306 }
307 
308 #endif
309