1 /* SPDX-License-Identifier: GPL-2.0 */ 2 3 /* Driver for ETAS GmbH ES58X USB CAN(-FD) Bus Interfaces. 4 * 5 * File es58x_fd.h: Definitions and declarations specific to ETAS 6 * ES582.1 and ES584.1 (naming convention: we use the term "ES58X FD" 7 * when referring to those two 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 #ifndef __ES58X_FD_H__ 15 #define __ES58X_FD_H__ 16 17 #include <linux/types.h> 18 19 #define ES582_1_NUM_CAN_CH 2 20 #define ES584_1_NUM_CAN_CH 1 21 #define ES58X_FD_NUM_CAN_CH 2 22 #define ES58X_FD_CHANNEL_IDX_OFFSET 0 23 24 #define ES58X_FD_TX_BULK_MAX 100 25 #define ES58X_FD_RX_BULK_MAX 100 26 #define ES58X_FD_ECHO_BULK_MAX 100 27 28 enum es58x_fd_cmd_type { 29 ES58X_FD_CMD_TYPE_CAN = 0x03, 30 ES58X_FD_CMD_TYPE_CANFD = 0x04, 31 ES58X_FD_CMD_TYPE_DEVICE = 0xFF 32 }; 33 34 /* Command IDs for ES58X_FD_CMD_TYPE_{CAN,CANFD}. */ 35 enum es58x_fd_can_cmd_id { 36 ES58X_FD_CAN_CMD_ID_ENABLE_CHANNEL = 0x01, 37 ES58X_FD_CAN_CMD_ID_DISABLE_CHANNEL = 0x02, 38 ES58X_FD_CAN_CMD_ID_TX_MSG = 0x05, 39 ES58X_FD_CAN_CMD_ID_ECHO_MSG = 0x07, 40 ES58X_FD_CAN_CMD_ID_RX_MSG = 0x10, 41 ES58X_FD_CAN_CMD_ID_ERROR_OR_EVENT_MSG = 0x11, 42 ES58X_FD_CAN_CMD_ID_RESET_RX = 0x20, 43 ES58X_FD_CAN_CMD_ID_RESET_TX = 0x21, 44 ES58X_FD_CAN_CMD_ID_TX_MSG_NO_ACK = 0x55 45 }; 46 47 /* Command IDs for ES58X_FD_CMD_TYPE_DEVICE. */ 48 enum es58x_fd_dev_cmd_id { 49 ES58X_FD_DEV_CMD_ID_GETTIMETICKS = 0x01, 50 ES58X_FD_DEV_CMD_ID_TIMESTAMP = 0x02 51 }; 52 53 /** 54 * enum es58x_fd_ctrlmode - Controller mode. 55 * @ES58X_FD_CTRLMODE_ACTIVE: send and receive messages. 56 * @ES58X_FD_CTRLMODE_PASSIVE: only receive messages (monitor). Do not 57 * send anything, not even the acknowledgment bit. 58 * @ES58X_FD_CTRLMODE_FD: CAN FD according to ISO11898-1. 59 * @ES58X_FD_CTRLMODE_FD_NON_ISO: follow Bosch CAN FD Specification 60 * V1.0 61 * @ES58X_FD_CTRLMODE_DISABLE_PROTOCOL_EXCEPTION_HANDLING: How to 62 * behave when CAN FD reserved bit is monitored as 63 * dominant. (c.f. ISO 11898-1:2015, section 10.4.2.4 "Control 64 * field", paragraph "r0 bit"). 0 (not disable = enable): send 65 * error frame. 1 (disable): goes into bus integration mode 66 * (c.f. below). 67 * @ES58X_FD_CTRLMODE_EDGE_FILTER_DURING_BUS_INTEGRATION: 0: Edge 68 * filtering is disabled. 1: Edge filtering is enabled. Two 69 * consecutive dominant bits required to detect an edge for hard 70 * synchronization. 71 */ 72 enum es58x_fd_ctrlmode { 73 ES58X_FD_CTRLMODE_ACTIVE = 0, 74 ES58X_FD_CTRLMODE_PASSIVE = BIT(0), 75 ES58X_FD_CTRLMODE_FD = BIT(4), 76 ES58X_FD_CTRLMODE_FD_NON_ISO = BIT(5), 77 ES58X_FD_CTRLMODE_DISABLE_PROTOCOL_EXCEPTION_HANDLING = BIT(6), 78 ES58X_FD_CTRLMODE_EDGE_FILTER_DURING_BUS_INTEGRATION = BIT(7) 79 }; 80 81 struct es58x_fd_bittiming { 82 __le32 bitrate; 83 __le16 tseg1; /* range: [tseg1_min-1..tseg1_max-1] */ 84 __le16 tseg2; /* range: [tseg2_min-1..tseg2_max-1] */ 85 __le16 brp; /* range: [brp_min-1..brp_max-1] */ 86 __le16 sjw; /* range: [0..sjw_max-1] */ 87 } __packed; 88 89 /** 90 * struct es58x_fd_tx_conf_msg - Channel configuration. 91 * @nominal_bittiming: Nominal bittiming. 92 * @samples_per_bit: type enum es58x_samples_per_bit. 93 * @sync_edge: type enum es58x_sync_edge. 94 * @physical_layer: type enum es58x_physical_layer. 95 * @echo_mode: type enum es58x_echo_mode. 96 * @ctrlmode: type enum es58x_fd_ctrlmode. 97 * @canfd_enabled: boolean (0: Classical CAN, 1: CAN and/or CANFD). 98 * @data_bittiming: Bittiming for flexible data-rate transmission. 99 * @tdc_enabled: Transmitter Delay Compensation switch (0: disabled, 100 * 1: enabled). On very high bitrates, the delay between when the 101 * bit is sent and received on the CANTX and CANRX pins of the 102 * transceiver start to be significant enough for errors to occur 103 * and thus need to be compensated. 104 * @tdco: Transmitter Delay Compensation Offset. Offset value, in time 105 * quanta, defining the delay between the start of the bit 106 * reception on the CANRX pin of the transceiver and the SSP 107 * (Secondary Sample Point). Valid values: 0 to 127. 108 * @tdcf: Transmitter Delay Compensation Filter window. Defines the 109 * minimum value for the SSP position, in time quanta. The 110 * feature is enabled when TDCF is configured to a value greater 111 * than TDCO. Valid values: 0 to 127. 112 * 113 * Please refer to the microcontroller datasheet: "SAM 114 * E701/S70/V70/V71 Family" section 49 "Controller Area Network 115 * (MCAN)" for additional information. 116 */ 117 struct es58x_fd_tx_conf_msg { 118 struct es58x_fd_bittiming nominal_bittiming; 119 u8 samples_per_bit; 120 u8 sync_edge; 121 u8 physical_layer; 122 u8 echo_mode; 123 u8 ctrlmode; 124 u8 canfd_enabled; 125 struct es58x_fd_bittiming data_bittiming; 126 u8 tdc_enabled; 127 __le16 tdco; 128 __le16 tdcf; 129 } __packed; 130 131 #define ES58X_FD_CAN_CONF_LEN \ 132 (offsetof(struct es58x_fd_tx_conf_msg, canfd_enabled)) 133 #define ES58X_FD_CANFD_CONF_LEN (sizeof(struct es58x_fd_tx_conf_msg)) 134 135 struct es58x_fd_tx_can_msg { 136 u8 packet_idx; 137 __le32 can_id; 138 u8 flags; 139 union { 140 u8 dlc; /* Only if cmd_id is ES58X_FD_CMD_TYPE_CAN */ 141 u8 len; /* Only if cmd_id is ES58X_FD_CMD_TYPE_CANFD */ 142 } __packed; 143 u8 data[CANFD_MAX_DLEN]; 144 } __packed; 145 146 #define ES58X_FD_CAN_TX_LEN \ 147 (offsetof(struct es58x_fd_tx_can_msg, data[CAN_MAX_DLEN])) 148 #define ES58X_FD_CANFD_TX_LEN (sizeof(struct es58x_fd_tx_can_msg)) 149 150 struct es58x_fd_rx_can_msg { 151 __le64 timestamp; 152 __le32 can_id; 153 u8 flags; 154 union { 155 u8 dlc; /* Only if cmd_id is ES58X_FD_CMD_TYPE_CAN */ 156 u8 len; /* Only if cmd_id is ES58X_FD_CMD_TYPE_CANFD */ 157 } __packed; 158 u8 data[CANFD_MAX_DLEN]; 159 } __packed; 160 161 #define ES58X_FD_CAN_RX_LEN \ 162 (offsetof(struct es58x_fd_rx_can_msg, data[CAN_MAX_DLEN])) 163 #define ES58X_FD_CANFD_RX_LEN (sizeof(struct es58x_fd_rx_can_msg)) 164 165 struct es58x_fd_echo_msg { 166 __le64 timestamp; 167 u8 packet_idx; 168 } __packed; 169 170 struct es58x_fd_rx_event_msg { 171 __le64 timestamp; 172 __le32 can_id; 173 u8 flags; /* type enum es58x_flag */ 174 u8 error_type; /* 0: event, 1: error */ 175 u8 error_code; 176 u8 event_code; 177 } __packed; 178 179 struct es58x_fd_tx_ack_msg { 180 __le32 rx_cmd_ret_le32; /* type enum es58x_cmd_ret_code_u32 */ 181 __le16 tx_free_entries; /* Number of remaining free entries in the device TX queue */ 182 } __packed; 183 184 /** 185 * struct es58x_fd_urb_cmd - Commands received from or sent to the 186 * ES58X FD device. 187 * @SOF: Start of Frame. 188 * @cmd_type: Command Type (type: enum es58x_fd_cmd_type). The CRC 189 * calculation starts at this position. 190 * @cmd_id: Command ID (type: enum es58x_fd_cmd_id). 191 * @channel_idx: Channel index starting at 0. 192 * @msg_len: Length of the message, excluding CRC (i.e. length of the 193 * union). 194 * @tx_conf_msg: Channel configuration. 195 * @tx_can_msg_buf: Concatenation of Tx messages. Type is "u8[]" 196 * instead of "struct es58x_fd_tx_msg[]" because the structure 197 * has a flexible size. 198 * @rx_can_msg_buf: Concatenation Rx messages. Type is "u8[]" instead 199 * of "struct es58x_fd_rx_msg[]" because the structure has a 200 * flexible size. 201 * @echo_msg: Array of echo messages (e.g. Tx messages being looped 202 * back). 203 * @rx_event_msg: Error or event message. 204 * @tx_ack_msg: Tx acknowledgment message. 205 * @timestamp: Timestamp reply. 206 * @rx_cmd_ret_le32: Rx 32 bits return code (type: enum 207 * es58x_cmd_ret_code_u32). 208 * @raw_msg: Message raw payload. 209 * @reserved_for_crc16_do_not_use: The structure ends with a 210 * CRC16. Because the structures in above union are of variable 211 * lengths, we can not predict the offset of the CRC in 212 * advance. Use functions es58x_get_crc() and es58x_set_crc() to 213 * manipulate it. 214 */ 215 struct es58x_fd_urb_cmd { 216 __le16 SOF; 217 u8 cmd_type; 218 u8 cmd_id; 219 u8 channel_idx; 220 __le16 msg_len; 221 222 union { 223 struct es58x_fd_tx_conf_msg tx_conf_msg; 224 u8 tx_can_msg_buf[ES58X_FD_TX_BULK_MAX * ES58X_FD_CANFD_TX_LEN]; 225 u8 rx_can_msg_buf[ES58X_FD_RX_BULK_MAX * ES58X_FD_CANFD_RX_LEN]; 226 struct es58x_fd_echo_msg echo_msg[ES58X_FD_ECHO_BULK_MAX]; 227 struct es58x_fd_rx_event_msg rx_event_msg; 228 struct es58x_fd_tx_ack_msg tx_ack_msg; 229 __le64 timestamp; 230 __le32 rx_cmd_ret_le32; 231 u8 raw_msg[0]; 232 } __packed; 233 234 __le16 reserved_for_crc16_do_not_use; 235 } __packed; 236 237 #define ES58X_FD_URB_CMD_HEADER_LEN (offsetof(struct es58x_fd_urb_cmd, raw_msg)) 238 #define ES58X_FD_TX_URB_CMD_MAX_LEN \ 239 ES58X_SIZEOF_URB_CMD(struct es58x_fd_urb_cmd, tx_can_msg_buf) 240 #define ES58X_FD_RX_URB_CMD_MAX_LEN \ 241 ES58X_SIZEOF_URB_CMD(struct es58x_fd_urb_cmd, rx_can_msg_buf) 242 243 #endif /* __ES58X_FD_H__ */ 244