Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 
0003 /* Driver for ETAS GmbH ES58X USB CAN(-FD) Bus Interfaces.
0004  *
0005  * File es58x_fd.h: Definitions and declarations specific to ETAS
0006  * ES582.1 and ES584.1 (naming convention: we use the term "ES58X FD"
0007  * when referring to those two variants together).
0008  *
0009  * Copyright (c) 2019 Robert Bosch Engineering and Business Solutions. All rights reserved.
0010  * Copyright (c) 2020 ETAS K.K.. All rights reserved.
0011  * Copyright (c) 2020, 2021 Vincent Mailhol <mailhol.vincent@wanadoo.fr>
0012  */
0013 
0014 #ifndef __ES58X_FD_H__
0015 #define __ES58X_FD_H__
0016 
0017 #include <linux/types.h>
0018 
0019 #define ES582_1_NUM_CAN_CH 2
0020 #define ES584_1_NUM_CAN_CH 1
0021 #define ES58X_FD_NUM_CAN_CH 2
0022 #define ES58X_FD_CHANNEL_IDX_OFFSET 0
0023 
0024 #define ES58X_FD_TX_BULK_MAX 100
0025 #define ES58X_FD_RX_BULK_MAX 100
0026 #define ES58X_FD_ECHO_BULK_MAX 100
0027 
0028 enum es58x_fd_cmd_type {
0029     ES58X_FD_CMD_TYPE_CAN = 0x03,
0030     ES58X_FD_CMD_TYPE_CANFD = 0x04,
0031     ES58X_FD_CMD_TYPE_DEVICE = 0xFF
0032 };
0033 
0034 /* Command IDs for ES58X_FD_CMD_TYPE_{CAN,CANFD}. */
0035 enum es58x_fd_can_cmd_id {
0036     ES58X_FD_CAN_CMD_ID_ENABLE_CHANNEL = 0x01,
0037     ES58X_FD_CAN_CMD_ID_DISABLE_CHANNEL = 0x02,
0038     ES58X_FD_CAN_CMD_ID_TX_MSG = 0x05,
0039     ES58X_FD_CAN_CMD_ID_ECHO_MSG = 0x07,
0040     ES58X_FD_CAN_CMD_ID_RX_MSG = 0x10,
0041     ES58X_FD_CAN_CMD_ID_ERROR_OR_EVENT_MSG = 0x11,
0042     ES58X_FD_CAN_CMD_ID_RESET_RX = 0x20,
0043     ES58X_FD_CAN_CMD_ID_RESET_TX = 0x21,
0044     ES58X_FD_CAN_CMD_ID_TX_MSG_NO_ACK = 0x55
0045 };
0046 
0047 /* Command IDs for ES58X_FD_CMD_TYPE_DEVICE. */
0048 enum es58x_fd_dev_cmd_id {
0049     ES58X_FD_DEV_CMD_ID_GETTIMETICKS = 0x01,
0050     ES58X_FD_DEV_CMD_ID_TIMESTAMP = 0x02
0051 };
0052 
0053 /**
0054  * enum es58x_fd_ctrlmode - Controller mode.
0055  * @ES58X_FD_CTRLMODE_ACTIVE: send and receive messages.
0056  * @ES58X_FD_CTRLMODE_PASSIVE: only receive messages (monitor). Do not
0057  *  send anything, not even the acknowledgment bit.
0058  * @ES58X_FD_CTRLMODE_FD: CAN FD according to ISO11898-1.
0059  * @ES58X_FD_CTRLMODE_FD_NON_ISO: follow Bosch CAN FD Specification
0060  *  V1.0
0061  * @ES58X_FD_CTRLMODE_DISABLE_PROTOCOL_EXCEPTION_HANDLING: How to
0062  *  behave when CAN FD reserved bit is monitored as
0063  *  dominant. (c.f. ISO 11898-1:2015, section 10.4.2.4 "Control
0064  *  field", paragraph "r0 bit"). 0 (not disable = enable): send
0065  *  error frame. 1 (disable): goes into bus integration mode
0066  *  (c.f. below).
0067  * @ES58X_FD_CTRLMODE_EDGE_FILTER_DURING_BUS_INTEGRATION: 0: Edge
0068  *  filtering is disabled. 1: Edge filtering is enabled. Two
0069  *  consecutive dominant bits required to detect an edge for hard
0070  *  synchronization.
0071  */
0072 enum es58x_fd_ctrlmode {
0073     ES58X_FD_CTRLMODE_ACTIVE = 0,
0074     ES58X_FD_CTRLMODE_PASSIVE = BIT(0),
0075     ES58X_FD_CTRLMODE_FD = BIT(4),
0076     ES58X_FD_CTRLMODE_FD_NON_ISO = BIT(5),
0077     ES58X_FD_CTRLMODE_DISABLE_PROTOCOL_EXCEPTION_HANDLING = BIT(6),
0078     ES58X_FD_CTRLMODE_EDGE_FILTER_DURING_BUS_INTEGRATION = BIT(7)
0079 };
0080 
0081 struct es58x_fd_bittiming {
0082     __le32 bitrate;
0083     __le16 tseg1;       /* range: [tseg1_min-1..tseg1_max-1] */
0084     __le16 tseg2;       /* range: [tseg2_min-1..tseg2_max-1] */
0085     __le16 brp;     /* range: [brp_min-1..brp_max-1] */
0086     __le16 sjw;     /* range: [0..sjw_max-1] */
0087 } __packed;
0088 
0089 /**
0090  * struct es58x_fd_tx_conf_msg - Channel configuration.
0091  * @nominal_bittiming: Nominal bittiming.
0092  * @samples_per_bit: type enum es58x_samples_per_bit.
0093  * @sync_edge: type enum es58x_sync_edge.
0094  * @physical_layer: type enum es58x_physical_layer.
0095  * @echo_mode: type enum es58x_echo_mode.
0096  * @ctrlmode: type enum es58x_fd_ctrlmode.
0097  * @canfd_enabled: boolean (0: Classical CAN, 1: CAN and/or CANFD).
0098  * @data_bittiming: Bittiming for flexible data-rate transmission.
0099  * @tdc_enabled: Transmitter Delay Compensation switch (0: TDC is
0100  *  disabled, 1: TDC is enabled).
0101  * @tdco: Transmitter Delay Compensation Offset.
0102  * @tdcf: Transmitter Delay Compensation Filter window.
0103  *
0104  * Please refer to the microcontroller datasheet: "SAM E70/S70/V70/V71
0105  * Family" section 49 "Controller Area Network (MCAN)" for additional
0106  * information.
0107  */
0108 struct es58x_fd_tx_conf_msg {
0109     struct es58x_fd_bittiming nominal_bittiming;
0110     u8 samples_per_bit;
0111     u8 sync_edge;
0112     u8 physical_layer;
0113     u8 echo_mode;
0114     u8 ctrlmode;
0115     u8 canfd_enabled;
0116     struct es58x_fd_bittiming data_bittiming;
0117     u8 tdc_enabled;
0118     __le16 tdco;
0119     __le16 tdcf;
0120 } __packed;
0121 
0122 #define ES58X_FD_CAN_CONF_LEN                   \
0123     (offsetof(struct es58x_fd_tx_conf_msg, canfd_enabled))
0124 #define ES58X_FD_CANFD_CONF_LEN (sizeof(struct es58x_fd_tx_conf_msg))
0125 
0126 struct es58x_fd_tx_can_msg {
0127     u8 packet_idx;
0128     __le32 can_id;
0129     u8 flags;
0130     union {
0131         u8 dlc;     /* Only if cmd_id is ES58X_FD_CMD_TYPE_CAN */
0132         u8 len;     /* Only if cmd_id is ES58X_FD_CMD_TYPE_CANFD */
0133     } __packed;
0134     u8 data[CANFD_MAX_DLEN];
0135 } __packed;
0136 
0137 #define ES58X_FD_CAN_TX_LEN                     \
0138     (offsetof(struct es58x_fd_tx_can_msg, data[CAN_MAX_DLEN]))
0139 #define ES58X_FD_CANFD_TX_LEN (sizeof(struct es58x_fd_tx_can_msg))
0140 
0141 struct es58x_fd_rx_can_msg {
0142     __le64 timestamp;
0143     __le32 can_id;
0144     u8 flags;
0145     union {
0146         u8 dlc;     /* Only if cmd_id is ES58X_FD_CMD_TYPE_CAN */
0147         u8 len;     /* Only if cmd_id is ES58X_FD_CMD_TYPE_CANFD */
0148     } __packed;
0149     u8 data[CANFD_MAX_DLEN];
0150 } __packed;
0151 
0152 #define ES58X_FD_CAN_RX_LEN                     \
0153     (offsetof(struct es58x_fd_rx_can_msg, data[CAN_MAX_DLEN]))
0154 #define ES58X_FD_CANFD_RX_LEN (sizeof(struct es58x_fd_rx_can_msg))
0155 
0156 struct es58x_fd_echo_msg {
0157     __le64 timestamp;
0158     u8 packet_idx;
0159 } __packed;
0160 
0161 struct es58x_fd_rx_event_msg {
0162     __le64 timestamp;
0163     __le32 can_id;
0164     u8 flags;       /* type enum es58x_flag */
0165     u8 error_type;      /* 0: event, 1: error */
0166     u8 error_code;
0167     u8 event_code;
0168 } __packed;
0169 
0170 struct es58x_fd_tx_ack_msg {
0171     __le32 rx_cmd_ret_le32; /* type enum es58x_cmd_ret_code_u32 */
0172     __le16 tx_free_entries; /* Number of remaining free entries in the device TX queue */
0173 } __packed;
0174 
0175 /**
0176  * struct es58x_fd_urb_cmd - Commands received from or sent to the
0177  *  ES58X FD device.
0178  * @SOF: Start of Frame.
0179  * @cmd_type: Command Type (type: enum es58x_fd_cmd_type). The CRC
0180  *  calculation starts at this position.
0181  * @cmd_id: Command ID (type: enum es58x_fd_cmd_id).
0182  * @channel_idx: Channel index starting at 0.
0183  * @msg_len: Length of the message, excluding CRC (i.e. length of the
0184  *  union).
0185  * @tx_conf_msg: Channel configuration.
0186  * @tx_can_msg_buf: Concatenation of Tx messages. Type is "u8[]"
0187  *  instead of "struct es58x_fd_tx_msg[]" because the structure
0188  *  has a flexible size.
0189  * @rx_can_msg_buf: Concatenation Rx messages. Type is "u8[]" instead
0190  *  of "struct es58x_fd_rx_msg[]" because the structure has a
0191  *  flexible size.
0192  * @echo_msg: Array of echo messages (e.g. Tx messages being looped
0193  *  back).
0194  * @rx_event_msg: Error or event message.
0195  * @tx_ack_msg: Tx acknowledgment message.
0196  * @timestamp: Timestamp reply.
0197  * @rx_cmd_ret_le32: Rx 32 bits return code (type: enum
0198  *  es58x_cmd_ret_code_u32).
0199  * @raw_msg: Message raw payload.
0200  * @reserved_for_crc16_do_not_use: The structure ends with a
0201  *  CRC16. Because the structures in above union are of variable
0202  *  lengths, we can not predict the offset of the CRC in
0203  *  advance. Use functions es58x_get_crc() and es58x_set_crc() to
0204  *  manipulate it.
0205  */
0206 struct es58x_fd_urb_cmd {
0207     __le16 SOF;
0208     u8 cmd_type;
0209     u8 cmd_id;
0210     u8 channel_idx;
0211     __le16 msg_len;
0212 
0213     union {
0214         struct es58x_fd_tx_conf_msg tx_conf_msg;
0215         u8 tx_can_msg_buf[ES58X_FD_TX_BULK_MAX * ES58X_FD_CANFD_TX_LEN];
0216         u8 rx_can_msg_buf[ES58X_FD_RX_BULK_MAX * ES58X_FD_CANFD_RX_LEN];
0217         struct es58x_fd_echo_msg echo_msg[ES58X_FD_ECHO_BULK_MAX];
0218         struct es58x_fd_rx_event_msg rx_event_msg;
0219         struct es58x_fd_tx_ack_msg tx_ack_msg;
0220         __le64 timestamp;
0221         __le32 rx_cmd_ret_le32;
0222         DECLARE_FLEX_ARRAY(u8, raw_msg);
0223     } __packed;
0224 
0225     __le16 reserved_for_crc16_do_not_use;
0226 } __packed;
0227 
0228 #define ES58X_FD_URB_CMD_HEADER_LEN (offsetof(struct es58x_fd_urb_cmd, raw_msg))
0229 #define ES58X_FD_TX_URB_CMD_MAX_LEN                 \
0230     ES58X_SIZEOF_URB_CMD(struct es58x_fd_urb_cmd, tx_can_msg_buf)
0231 #define ES58X_FD_RX_URB_CMD_MAX_LEN                 \
0232     ES58X_SIZEOF_URB_CMD(struct es58x_fd_urb_cmd, rx_can_msg_buf)
0233 
0234 #endif /* __ES58X_FD_H__ */