1 // SPDX-License-Identifier: GPL-2.0
2
3 /* Driver for ETAS GmbH ES58X USB CAN(-FD) Bus Interfaces.
4 *
5 * File es58x_fd.c: Adds support to ETAS ES582.1 and ES584.1 (naming
6 * convention: we use the term "ES58X FD" when referring to those two
7 * variants together).
8 *
9 * Copyright (c) 2019 Robert Bosch Engineering and Business Solutions. All rights reserved.
10 * Copyright (c) 2020 ETAS K.K.. All rights reserved.
11 * Copyright (c) 2020, 2021 Vincent Mailhol <mailhol.vincent@wanadoo.fr>
12 */
13
14 #include <linux/kernel.h>
15 #include <asm/unaligned.h>
16
17 #include "es58x_core.h"
18 #include "es58x_fd.h"
19
20 /**
21 * es58x_fd_sizeof_rx_tx_msg() - Calculate the actual length of the
22 * structure of a rx or tx message.
23 * @msg: message of variable length, must have a dlc and a len fields.
24 *
25 * Even if RTR frames have actually no payload, the ES58X devices
26 * still expect it. Must be a macro in order to accept several types
27 * (struct es58x_fd_tx_can_msg and struct es58x_fd_rx_can_msg) as an
28 * input.
29 *
30 * Return: length of the message.
31 */
32 #define es58x_fd_sizeof_rx_tx_msg(msg) \
33 ({ \
34 typeof(msg) __msg = (msg); \
35 size_t __msg_len; \
36 \
37 if (__msg.flags & ES58X_FLAG_FD_DATA) \
38 __msg_len = canfd_sanitize_len(__msg.len); \
39 else \
40 __msg_len = can_cc_dlc2len(__msg.dlc); \
41 \
42 offsetof(typeof(__msg), data[__msg_len]); \
43 })
44
es58x_fd_cmd_type(struct net_device * netdev)45 static enum es58x_fd_cmd_type es58x_fd_cmd_type(struct net_device *netdev)
46 {
47 u32 ctrlmode = es58x_priv(netdev)->can.ctrlmode;
48
49 if (ctrlmode & (CAN_CTRLMODE_FD | CAN_CTRLMODE_FD_NON_ISO))
50 return ES58X_FD_CMD_TYPE_CANFD;
51 else
52 return ES58X_FD_CMD_TYPE_CAN;
53 }
54
es58x_fd_get_msg_len(const union es58x_urb_cmd * urb_cmd)55 static u16 es58x_fd_get_msg_len(const union es58x_urb_cmd *urb_cmd)
56 {
57 return get_unaligned_le16(&urb_cmd->es58x_fd_urb_cmd.msg_len);
58 }
59
es58x_fd_echo_msg(struct net_device * netdev,const struct es58x_fd_urb_cmd * es58x_fd_urb_cmd)60 static int es58x_fd_echo_msg(struct net_device *netdev,
61 const struct es58x_fd_urb_cmd *es58x_fd_urb_cmd)
62 {
63 struct es58x_priv *priv = es58x_priv(netdev);
64 const struct es58x_fd_echo_msg *echo_msg;
65 struct es58x_device *es58x_dev = priv->es58x_dev;
66 u64 *tstamps = es58x_dev->timestamps;
67 u16 msg_len = get_unaligned_le16(&es58x_fd_urb_cmd->msg_len);
68 int i, num_element;
69 u32 rcv_packet_idx;
70
71 const u32 mask = GENMASK(31, sizeof(echo_msg->packet_idx) * 8);
72
73 num_element = es58x_msg_num_element(es58x_dev->dev,
74 es58x_fd_urb_cmd->echo_msg,
75 msg_len);
76 if (num_element < 0)
77 return num_element;
78 echo_msg = es58x_fd_urb_cmd->echo_msg;
79
80 rcv_packet_idx = (priv->tx_tail & mask) | echo_msg[0].packet_idx;
81 for (i = 0; i < num_element; i++) {
82 if ((u8)rcv_packet_idx != echo_msg[i].packet_idx) {
83 netdev_err(netdev, "Packet idx jumped from %u to %u\n",
84 (u8)rcv_packet_idx - 1,
85 echo_msg[i].packet_idx);
86 return -EBADMSG;
87 }
88
89 tstamps[i] = get_unaligned_le64(&echo_msg[i].timestamp);
90 rcv_packet_idx++;
91 }
92
93 return es58x_can_get_echo_skb(netdev, priv->tx_tail, tstamps, num_element);
94 }
95
es58x_fd_rx_can_msg(struct net_device * netdev,const struct es58x_fd_urb_cmd * es58x_fd_urb_cmd)96 static int es58x_fd_rx_can_msg(struct net_device *netdev,
97 const struct es58x_fd_urb_cmd *es58x_fd_urb_cmd)
98 {
99 struct es58x_device *es58x_dev = es58x_priv(netdev)->es58x_dev;
100 const u8 *rx_can_msg_buf = es58x_fd_urb_cmd->rx_can_msg_buf;
101 u16 rx_can_msg_buf_len = get_unaligned_le16(&es58x_fd_urb_cmd->msg_len);
102 int pkts, ret;
103
104 ret = es58x_check_msg_max_len(es58x_dev->dev,
105 es58x_fd_urb_cmd->rx_can_msg_buf,
106 rx_can_msg_buf_len);
107 if (ret)
108 return ret;
109
110 for (pkts = 0; rx_can_msg_buf_len > 0; pkts++) {
111 const struct es58x_fd_rx_can_msg *rx_can_msg =
112 (const struct es58x_fd_rx_can_msg *)rx_can_msg_buf;
113 bool is_can_fd = !!(rx_can_msg->flags & ES58X_FLAG_FD_DATA);
114 /* rx_can_msg_len is the length of the rx_can_msg
115 * buffer. Not to be confused with rx_can_msg->len
116 * which is the length of the CAN payload
117 * rx_can_msg->data.
118 */
119 u16 rx_can_msg_len = es58x_fd_sizeof_rx_tx_msg(*rx_can_msg);
120
121 if (rx_can_msg_len > rx_can_msg_buf_len) {
122 netdev_err(netdev,
123 "%s: Expected a rx_can_msg of size %d but only %d bytes are left in rx_can_msg_buf\n",
124 __func__,
125 rx_can_msg_len, rx_can_msg_buf_len);
126 return -EMSGSIZE;
127 }
128 if (rx_can_msg->len > CANFD_MAX_DLEN) {
129 netdev_err(netdev,
130 "%s: Data length is %d but maximum should be %d\n",
131 __func__, rx_can_msg->len, CANFD_MAX_DLEN);
132 return -EMSGSIZE;
133 }
134
135 if (netif_running(netdev)) {
136 u64 tstamp = get_unaligned_le64(&rx_can_msg->timestamp);
137 canid_t can_id = get_unaligned_le32(&rx_can_msg->can_id);
138 u8 dlc;
139
140 if (is_can_fd)
141 dlc = can_fd_len2dlc(rx_can_msg->len);
142 else
143 dlc = rx_can_msg->dlc;
144
145 ret = es58x_rx_can_msg(netdev, tstamp, rx_can_msg->data,
146 can_id, rx_can_msg->flags, dlc);
147 if (ret)
148 break;
149 }
150
151 rx_can_msg_buf_len -= rx_can_msg_len;
152 rx_can_msg_buf += rx_can_msg_len;
153 }
154
155 if (!netif_running(netdev)) {
156 if (net_ratelimit())
157 netdev_info(netdev,
158 "%s: %s is down, dropping %d rx packets\n",
159 __func__, netdev->name, pkts);
160 netdev->stats.rx_dropped += pkts;
161 }
162
163 return ret;
164 }
165
es58x_fd_rx_event_msg(struct net_device * netdev,const struct es58x_fd_urb_cmd * es58x_fd_urb_cmd)166 static int es58x_fd_rx_event_msg(struct net_device *netdev,
167 const struct es58x_fd_urb_cmd *es58x_fd_urb_cmd)
168 {
169 struct es58x_device *es58x_dev = es58x_priv(netdev)->es58x_dev;
170 u16 msg_len = get_unaligned_le16(&es58x_fd_urb_cmd->msg_len);
171 const struct es58x_fd_rx_event_msg *rx_event_msg;
172 int ret;
173
174 rx_event_msg = &es58x_fd_urb_cmd->rx_event_msg;
175 ret = es58x_check_msg_len(es58x_dev->dev, *rx_event_msg, msg_len);
176 if (ret)
177 return ret;
178
179 return es58x_rx_err_msg(netdev, rx_event_msg->error_code,
180 rx_event_msg->event_code,
181 get_unaligned_le64(&rx_event_msg->timestamp));
182 }
183
es58x_fd_rx_cmd_ret_u32(struct net_device * netdev,const struct es58x_fd_urb_cmd * es58x_fd_urb_cmd,enum es58x_ret_type cmd_ret_type)184 static int es58x_fd_rx_cmd_ret_u32(struct net_device *netdev,
185 const struct es58x_fd_urb_cmd *es58x_fd_urb_cmd,
186 enum es58x_ret_type cmd_ret_type)
187 {
188 struct es58x_device *es58x_dev = es58x_priv(netdev)->es58x_dev;
189 u16 msg_len = get_unaligned_le16(&es58x_fd_urb_cmd->msg_len);
190 int ret;
191
192 ret = es58x_check_msg_len(es58x_dev->dev,
193 es58x_fd_urb_cmd->rx_cmd_ret_le32, msg_len);
194 if (ret)
195 return ret;
196
197 return es58x_rx_cmd_ret_u32(netdev, cmd_ret_type,
198 get_unaligned_le32(&es58x_fd_urb_cmd->rx_cmd_ret_le32));
199 }
200
es58x_fd_tx_ack_msg(struct net_device * netdev,const struct es58x_fd_urb_cmd * es58x_fd_urb_cmd)201 static int es58x_fd_tx_ack_msg(struct net_device *netdev,
202 const struct es58x_fd_urb_cmd *es58x_fd_urb_cmd)
203 {
204 struct es58x_device *es58x_dev = es58x_priv(netdev)->es58x_dev;
205 const struct es58x_fd_tx_ack_msg *tx_ack_msg;
206 u16 msg_len = get_unaligned_le16(&es58x_fd_urb_cmd->msg_len);
207 int ret;
208
209 tx_ack_msg = &es58x_fd_urb_cmd->tx_ack_msg;
210 ret = es58x_check_msg_len(es58x_dev->dev, *tx_ack_msg, msg_len);
211 if (ret)
212 return ret;
213
214 return es58x_tx_ack_msg(netdev,
215 get_unaligned_le16(&tx_ack_msg->tx_free_entries),
216 get_unaligned_le32(&tx_ack_msg->rx_cmd_ret_le32));
217 }
218
es58x_fd_can_cmd_id(struct es58x_device * es58x_dev,const struct es58x_fd_urb_cmd * es58x_fd_urb_cmd)219 static int es58x_fd_can_cmd_id(struct es58x_device *es58x_dev,
220 const struct es58x_fd_urb_cmd *es58x_fd_urb_cmd)
221 {
222 struct net_device *netdev;
223 int ret;
224
225 ret = es58x_get_netdev(es58x_dev, es58x_fd_urb_cmd->channel_idx,
226 ES58X_FD_CHANNEL_IDX_OFFSET, &netdev);
227 if (ret)
228 return ret;
229
230 switch ((enum es58x_fd_can_cmd_id)es58x_fd_urb_cmd->cmd_id) {
231 case ES58X_FD_CAN_CMD_ID_ENABLE_CHANNEL:
232 return es58x_fd_rx_cmd_ret_u32(netdev, es58x_fd_urb_cmd,
233 ES58X_RET_TYPE_ENABLE_CHANNEL);
234
235 case ES58X_FD_CAN_CMD_ID_DISABLE_CHANNEL:
236 return es58x_fd_rx_cmd_ret_u32(netdev, es58x_fd_urb_cmd,
237 ES58X_RET_TYPE_DISABLE_CHANNEL);
238
239 case ES58X_FD_CAN_CMD_ID_TX_MSG:
240 return es58x_fd_tx_ack_msg(netdev, es58x_fd_urb_cmd);
241
242 case ES58X_FD_CAN_CMD_ID_ECHO_MSG:
243 return es58x_fd_echo_msg(netdev, es58x_fd_urb_cmd);
244
245 case ES58X_FD_CAN_CMD_ID_RX_MSG:
246 return es58x_fd_rx_can_msg(netdev, es58x_fd_urb_cmd);
247
248 case ES58X_FD_CAN_CMD_ID_RESET_RX:
249 return es58x_fd_rx_cmd_ret_u32(netdev, es58x_fd_urb_cmd,
250 ES58X_RET_TYPE_RESET_RX);
251
252 case ES58X_FD_CAN_CMD_ID_RESET_TX:
253 return es58x_fd_rx_cmd_ret_u32(netdev, es58x_fd_urb_cmd,
254 ES58X_RET_TYPE_RESET_TX);
255
256 case ES58X_FD_CAN_CMD_ID_ERROR_OR_EVENT_MSG:
257 return es58x_fd_rx_event_msg(netdev, es58x_fd_urb_cmd);
258
259 default:
260 return -EBADRQC;
261 }
262 }
263
es58x_fd_device_cmd_id(struct es58x_device * es58x_dev,const struct es58x_fd_urb_cmd * es58x_fd_urb_cmd)264 static int es58x_fd_device_cmd_id(struct es58x_device *es58x_dev,
265 const struct es58x_fd_urb_cmd *es58x_fd_urb_cmd)
266 {
267 u16 msg_len = get_unaligned_le16(&es58x_fd_urb_cmd->msg_len);
268 int ret;
269
270 switch ((enum es58x_fd_dev_cmd_id)es58x_fd_urb_cmd->cmd_id) {
271 case ES58X_FD_DEV_CMD_ID_TIMESTAMP:
272 ret = es58x_check_msg_len(es58x_dev->dev,
273 es58x_fd_urb_cmd->timestamp, msg_len);
274 if (ret)
275 return ret;
276 es58x_rx_timestamp(es58x_dev,
277 get_unaligned_le64(&es58x_fd_urb_cmd->timestamp));
278 return 0;
279
280 default:
281 return -EBADRQC;
282 }
283 }
284
es58x_fd_handle_urb_cmd(struct es58x_device * es58x_dev,const union es58x_urb_cmd * urb_cmd)285 static int es58x_fd_handle_urb_cmd(struct es58x_device *es58x_dev,
286 const union es58x_urb_cmd *urb_cmd)
287 {
288 const struct es58x_fd_urb_cmd *es58x_fd_urb_cmd;
289 int ret;
290
291 es58x_fd_urb_cmd = &urb_cmd->es58x_fd_urb_cmd;
292
293 switch ((enum es58x_fd_cmd_type)es58x_fd_urb_cmd->cmd_type) {
294 case ES58X_FD_CMD_TYPE_CAN:
295 case ES58X_FD_CMD_TYPE_CANFD:
296 ret = es58x_fd_can_cmd_id(es58x_dev, es58x_fd_urb_cmd);
297 break;
298
299 case ES58X_FD_CMD_TYPE_DEVICE:
300 ret = es58x_fd_device_cmd_id(es58x_dev, es58x_fd_urb_cmd);
301 break;
302
303 default:
304 ret = -EBADRQC;
305 break;
306 }
307
308 if (ret == -EBADRQC)
309 dev_err(es58x_dev->dev,
310 "%s: Unknown command type (0x%02X) and command ID (0x%02X) combination\n",
311 __func__, es58x_fd_urb_cmd->cmd_type,
312 es58x_fd_urb_cmd->cmd_id);
313
314 return ret;
315 }
316
es58x_fd_fill_urb_header(union es58x_urb_cmd * urb_cmd,u8 cmd_type,u8 cmd_id,u8 channel_idx,u16 msg_len)317 static void es58x_fd_fill_urb_header(union es58x_urb_cmd *urb_cmd, u8 cmd_type,
318 u8 cmd_id, u8 channel_idx, u16 msg_len)
319 {
320 struct es58x_fd_urb_cmd *es58x_fd_urb_cmd = &urb_cmd->es58x_fd_urb_cmd;
321
322 es58x_fd_urb_cmd->SOF = cpu_to_le16(es58x_fd_param.tx_start_of_frame);
323 es58x_fd_urb_cmd->cmd_type = cmd_type;
324 es58x_fd_urb_cmd->cmd_id = cmd_id;
325 es58x_fd_urb_cmd->channel_idx = channel_idx;
326 es58x_fd_urb_cmd->msg_len = cpu_to_le16(msg_len);
327 }
328
es58x_fd_tx_can_msg(struct es58x_priv * priv,const struct sk_buff * skb)329 static int es58x_fd_tx_can_msg(struct es58x_priv *priv,
330 const struct sk_buff *skb)
331 {
332 struct es58x_device *es58x_dev = priv->es58x_dev;
333 union es58x_urb_cmd *urb_cmd = priv->tx_urb->transfer_buffer;
334 struct es58x_fd_urb_cmd *es58x_fd_urb_cmd = &urb_cmd->es58x_fd_urb_cmd;
335 struct can_frame *cf = (struct can_frame *)skb->data;
336 struct es58x_fd_tx_can_msg *tx_can_msg;
337 bool is_fd = can_is_canfd_skb(skb);
338 u16 msg_len;
339 int ret;
340
341 if (priv->tx_can_msg_cnt == 0) {
342 msg_len = 0;
343 es58x_fd_fill_urb_header(urb_cmd,
344 is_fd ? ES58X_FD_CMD_TYPE_CANFD
345 : ES58X_FD_CMD_TYPE_CAN,
346 ES58X_FD_CAN_CMD_ID_TX_MSG_NO_ACK,
347 priv->channel_idx, msg_len);
348 } else {
349 msg_len = es58x_fd_get_msg_len(urb_cmd);
350 }
351
352 ret = es58x_check_msg_max_len(es58x_dev->dev,
353 es58x_fd_urb_cmd->tx_can_msg_buf,
354 msg_len + sizeof(*tx_can_msg));
355 if (ret)
356 return ret;
357
358 /* Fill message contents. */
359 tx_can_msg = (typeof(tx_can_msg))&es58x_fd_urb_cmd->raw_msg[msg_len];
360 tx_can_msg->packet_idx = (u8)priv->tx_head;
361 put_unaligned_le32(es58x_get_raw_can_id(cf), &tx_can_msg->can_id);
362 tx_can_msg->flags = (u8)es58x_get_flags(skb);
363 if (is_fd)
364 tx_can_msg->len = cf->len;
365 else
366 tx_can_msg->dlc = can_get_cc_dlc(cf, priv->can.ctrlmode);
367 memcpy(tx_can_msg->data, cf->data, cf->len);
368
369 /* Calculate new sizes */
370 msg_len += es58x_fd_sizeof_rx_tx_msg(*tx_can_msg);
371 priv->tx_urb->transfer_buffer_length = es58x_get_urb_cmd_len(es58x_dev,
372 msg_len);
373 put_unaligned_le16(msg_len, &es58x_fd_urb_cmd->msg_len);
374
375 return 0;
376 }
377
es58x_fd_convert_bittiming(struct es58x_fd_bittiming * es58x_fd_bt,struct can_bittiming * bt)378 static void es58x_fd_convert_bittiming(struct es58x_fd_bittiming *es58x_fd_bt,
379 struct can_bittiming *bt)
380 {
381 /* The actual value set in the hardware registers is one less
382 * than the functional value.
383 */
384 const int offset = 1;
385
386 es58x_fd_bt->bitrate = cpu_to_le32(bt->bitrate);
387 es58x_fd_bt->tseg1 =
388 cpu_to_le16(bt->prop_seg + bt->phase_seg1 - offset);
389 es58x_fd_bt->tseg2 = cpu_to_le16(bt->phase_seg2 - offset);
390 es58x_fd_bt->brp = cpu_to_le16(bt->brp - offset);
391 es58x_fd_bt->sjw = cpu_to_le16(bt->sjw - offset);
392 }
393
es58x_fd_enable_channel(struct es58x_priv * priv)394 static int es58x_fd_enable_channel(struct es58x_priv *priv)
395 {
396 struct es58x_device *es58x_dev = priv->es58x_dev;
397 struct net_device *netdev = es58x_dev->netdev[priv->channel_idx];
398 struct es58x_fd_tx_conf_msg tx_conf_msg = { 0 };
399 u32 ctrlmode;
400 size_t conf_len = 0;
401
402 es58x_fd_convert_bittiming(&tx_conf_msg.nominal_bittiming,
403 &priv->can.bittiming);
404 ctrlmode = priv->can.ctrlmode;
405
406 if (ctrlmode & CAN_CTRLMODE_3_SAMPLES)
407 tx_conf_msg.samples_per_bit = ES58X_SAMPLES_PER_BIT_THREE;
408 else
409 tx_conf_msg.samples_per_bit = ES58X_SAMPLES_PER_BIT_ONE;
410 tx_conf_msg.sync_edge = ES58X_SYNC_EDGE_SINGLE;
411 tx_conf_msg.physical_layer = ES58X_PHYSICAL_LAYER_HIGH_SPEED;
412 tx_conf_msg.echo_mode = ES58X_ECHO_ON;
413 if (ctrlmode & CAN_CTRLMODE_LISTENONLY)
414 tx_conf_msg.ctrlmode |= ES58X_FD_CTRLMODE_PASSIVE;
415 else
416 tx_conf_msg.ctrlmode |= ES58X_FD_CTRLMODE_ACTIVE;
417
418 if (ctrlmode & CAN_CTRLMODE_FD_NON_ISO) {
419 tx_conf_msg.ctrlmode |= ES58X_FD_CTRLMODE_FD_NON_ISO;
420 tx_conf_msg.canfd_enabled = 1;
421 } else if (ctrlmode & CAN_CTRLMODE_FD) {
422 tx_conf_msg.ctrlmode |= ES58X_FD_CTRLMODE_FD;
423 tx_conf_msg.canfd_enabled = 1;
424 }
425
426 if (tx_conf_msg.canfd_enabled) {
427 es58x_fd_convert_bittiming(&tx_conf_msg.data_bittiming,
428 &priv->can.data_bittiming);
429
430 if (priv->can.tdc.tdco) {
431 tx_conf_msg.tdc_enabled = 1;
432 tx_conf_msg.tdco = cpu_to_le16(priv->can.tdc.tdco);
433 tx_conf_msg.tdcf = cpu_to_le16(priv->can.tdc.tdcf);
434 }
435
436 conf_len = ES58X_FD_CANFD_CONF_LEN;
437 } else {
438 conf_len = ES58X_FD_CAN_CONF_LEN;
439 }
440
441 return es58x_send_msg(es58x_dev, es58x_fd_cmd_type(netdev),
442 ES58X_FD_CAN_CMD_ID_ENABLE_CHANNEL,
443 &tx_conf_msg, conf_len, priv->channel_idx);
444 }
445
es58x_fd_disable_channel(struct es58x_priv * priv)446 static int es58x_fd_disable_channel(struct es58x_priv *priv)
447 {
448 /* The type (ES58X_FD_CMD_TYPE_CAN or ES58X_FD_CMD_TYPE_CANFD) does
449 * not matter here.
450 */
451 return es58x_send_msg(priv->es58x_dev, ES58X_FD_CMD_TYPE_CAN,
452 ES58X_FD_CAN_CMD_ID_DISABLE_CHANNEL,
453 ES58X_EMPTY_MSG, 0, priv->channel_idx);
454 }
455
es58x_fd_get_timestamp(struct es58x_device * es58x_dev)456 static int es58x_fd_get_timestamp(struct es58x_device *es58x_dev)
457 {
458 return es58x_send_msg(es58x_dev, ES58X_FD_CMD_TYPE_DEVICE,
459 ES58X_FD_DEV_CMD_ID_TIMESTAMP, ES58X_EMPTY_MSG,
460 0, ES58X_CHANNEL_IDX_NA);
461 }
462
463 /* Nominal bittiming constants for ES582.1 and ES584.1 as specified in
464 * the microcontroller datasheet: "SAM E70/S70/V70/V71 Family" section
465 * 49.6.8 "MCAN Nominal Bit Timing and Prescaler Register" from
466 * Microchip.
467 *
468 * The values from the specification are the hardware register
469 * values. To convert them to the functional values, all ranges were
470 * incremented by 1 (e.g. range [0..n-1] changed to [1..n]).
471 */
472 static const struct can_bittiming_const es58x_fd_nom_bittiming_const = {
473 .name = "ES582.1/ES584.1",
474 .tseg1_min = 2,
475 .tseg1_max = 256,
476 .tseg2_min = 2,
477 .tseg2_max = 128,
478 .sjw_max = 128,
479 .brp_min = 1,
480 .brp_max = 512,
481 .brp_inc = 1
482 };
483
484 /* Data bittiming constants for ES582.1 and ES584.1 as specified in
485 * the microcontroller datasheet: "SAM E70/S70/V70/V71 Family" section
486 * 49.6.4 "MCAN Data Bit Timing and Prescaler Register" from
487 * Microchip.
488 */
489 static const struct can_bittiming_const es58x_fd_data_bittiming_const = {
490 .name = "ES582.1/ES584.1",
491 .tseg1_min = 2,
492 .tseg1_max = 32,
493 .tseg2_min = 1,
494 .tseg2_max = 16,
495 .sjw_max = 8,
496 .brp_min = 1,
497 .brp_max = 32,
498 .brp_inc = 1
499 };
500
501 /* Transmission Delay Compensation constants for ES582.1 and ES584.1
502 * as specified in the microcontroller datasheet: "SAM E70/S70/V70/V71
503 * Family" section 49.6.15 "MCAN Transmitter Delay Compensation
504 * Register" from Microchip.
505 */
506 static const struct can_tdc_const es58x_tdc_const = {
507 .tdcv_max = 0, /* Manual mode not supported. */
508 .tdco_max = 127,
509 .tdcf_max = 127
510 };
511
512 const struct es58x_parameters es58x_fd_param = {
513 .bittiming_const = &es58x_fd_nom_bittiming_const,
514 .data_bittiming_const = &es58x_fd_data_bittiming_const,
515 .tdc_const = &es58x_tdc_const,
516 /* The devices use NXP TJA1044G transievers which guarantee
517 * the timing for data rates up to 5 Mbps. Bitrates up to 8
518 * Mbps work in an optimal environment but are not recommended
519 * for production environment.
520 */
521 .bitrate_max = 8 * CAN_MBPS,
522 .clock = {.freq = 80 * CAN_MHZ},
523 .ctrlmode_supported = CAN_CTRLMODE_LOOPBACK | CAN_CTRLMODE_LISTENONLY |
524 CAN_CTRLMODE_3_SAMPLES | CAN_CTRLMODE_FD | CAN_CTRLMODE_FD_NON_ISO |
525 CAN_CTRLMODE_CC_LEN8_DLC,
526 .tx_start_of_frame = 0xCEFA, /* FACE in little endian */
527 .rx_start_of_frame = 0xFECA, /* CAFE in little endian */
528 .tx_urb_cmd_max_len = ES58X_FD_TX_URB_CMD_MAX_LEN,
529 .rx_urb_cmd_max_len = ES58X_FD_RX_URB_CMD_MAX_LEN,
530 /* Size of internal device TX queue is 500.
531 *
532 * However, when reaching value around 278, the device's busy
533 * LED turns on and thus maximum value of 500 is never reached
534 * in practice. Also, when this value is too high, some error
535 * on the echo_msg were witnessed when the device is
536 * recovering from bus off.
537 *
538 * For above reasons, a value that would prevent the device
539 * from becoming busy was chosen. In practice, BQL would
540 * prevent the value from even getting closer to below
541 * maximum, so no impact on performance was measured.
542 */
543 .fifo_mask = 255, /* echo_skb_max = 256 */
544 .dql_min_limit = CAN_FRAME_LEN_MAX * 15, /* Empirical value. */
545 .tx_bulk_max = ES58X_FD_TX_BULK_MAX,
546 .urb_cmd_header_len = ES58X_FD_URB_CMD_HEADER_LEN,
547 .rx_urb_max = ES58X_RX_URBS_MAX,
548 .tx_urb_max = ES58X_TX_URBS_MAX
549 };
550
551 const struct es58x_operators es58x_fd_ops = {
552 .get_msg_len = es58x_fd_get_msg_len,
553 .handle_urb_cmd = es58x_fd_handle_urb_cmd,
554 .fill_urb_header = es58x_fd_fill_urb_header,
555 .tx_can_msg = es58x_fd_tx_can_msg,
556 .enable_channel = es58x_fd_enable_channel,
557 .disable_channel = es58x_fd_disable_channel,
558 .reset_device = NULL, /* Not implemented in the device firmware. */
559 .get_timestamp = es58x_fd_get_timestamp
560 };
561