301 lines · c
1/* SPDX-License-Identifier: GPL-2.0-only */2/*3 * CAN driver for PEAK System micro-CAN based adapters4 *5 * Copyright (C) 2003-2011 PEAK System-Technik GmbH6 * Copyright (C) 2011-2013 Stephane Grosjean <s.grosjean@peak-system.com>7 */8#ifndef PUCAN_H9#define PUCAN_H10 11/* uCAN commands opcodes list (low-order 10 bits) */12#define PUCAN_CMD_NOP 0x00013#define PUCAN_CMD_RESET_MODE 0x00114#define PUCAN_CMD_NORMAL_MODE 0x00215#define PUCAN_CMD_LISTEN_ONLY_MODE 0x00316#define PUCAN_CMD_TIMING_SLOW 0x00417#define PUCAN_CMD_TIMING_FAST 0x00518#define PUCAN_CMD_SET_STD_FILTER 0x00619#define PUCAN_CMD_RESERVED2 0x00720#define PUCAN_CMD_FILTER_STD 0x00821#define PUCAN_CMD_TX_ABORT 0x00922#define PUCAN_CMD_WR_ERR_CNT 0x00a23#define PUCAN_CMD_SET_EN_OPTION 0x00b24#define PUCAN_CMD_CLR_DIS_OPTION 0x00c25#define PUCAN_CMD_RX_BARRIER 0x01026#define PUCAN_CMD_END_OF_COLLECTION 0x3ff27 28/* uCAN received messages list */29#define PUCAN_MSG_CAN_RX 0x000130#define PUCAN_MSG_ERROR 0x000231#define PUCAN_MSG_STATUS 0x000332#define PUCAN_MSG_BUSLOAD 0x000433 34#define PUCAN_MSG_CACHE_CRITICAL 0x010235 36/* uCAN transmitted messages */37#define PUCAN_MSG_CAN_TX 0x100038 39/* uCAN command common header */40struct __packed pucan_command {41 __le16 opcode_channel;42 u16 args[3];43};44 45/* return the opcode from the opcode_channel field of a command */46static inline u16 pucan_cmd_get_opcode(struct pucan_command *c)47{48 return le16_to_cpu(c->opcode_channel) & 0x3ff;49}50 51#define PUCAN_TSLOW_BRP_BITS 1052#define PUCAN_TSLOW_TSGEG1_BITS 853#define PUCAN_TSLOW_TSGEG2_BITS 754#define PUCAN_TSLOW_SJW_BITS 755 56#define PUCAN_TSLOW_BRP_MASK ((1 << PUCAN_TSLOW_BRP_BITS) - 1)57#define PUCAN_TSLOW_TSEG1_MASK ((1 << PUCAN_TSLOW_TSGEG1_BITS) - 1)58#define PUCAN_TSLOW_TSEG2_MASK ((1 << PUCAN_TSLOW_TSGEG2_BITS) - 1)59#define PUCAN_TSLOW_SJW_MASK ((1 << PUCAN_TSLOW_SJW_BITS) - 1)60 61/* uCAN TIMING_SLOW command fields */62#define PUCAN_TSLOW_SJW_T(s, t) (((s) & PUCAN_TSLOW_SJW_MASK) | \63 ((!!(t)) << 7))64#define PUCAN_TSLOW_TSEG2(t) ((t) & PUCAN_TSLOW_TSEG2_MASK)65#define PUCAN_TSLOW_TSEG1(t) ((t) & PUCAN_TSLOW_TSEG1_MASK)66#define PUCAN_TSLOW_BRP(b) ((b) & PUCAN_TSLOW_BRP_MASK)67 68struct __packed pucan_timing_slow {69 __le16 opcode_channel;70 71 u8 ewl; /* Error Warning limit */72 u8 sjw_t; /* Sync Jump Width + Triple sampling */73 u8 tseg2; /* Timing SEGment 2 */74 u8 tseg1; /* Timing SEGment 1 */75 76 __le16 brp; /* BaudRate Prescaler */77};78 79#define PUCAN_TFAST_BRP_BITS 1080#define PUCAN_TFAST_TSGEG1_BITS 581#define PUCAN_TFAST_TSGEG2_BITS 482#define PUCAN_TFAST_SJW_BITS 483 84#define PUCAN_TFAST_BRP_MASK ((1 << PUCAN_TFAST_BRP_BITS) - 1)85#define PUCAN_TFAST_TSEG1_MASK ((1 << PUCAN_TFAST_TSGEG1_BITS) - 1)86#define PUCAN_TFAST_TSEG2_MASK ((1 << PUCAN_TFAST_TSGEG2_BITS) - 1)87#define PUCAN_TFAST_SJW_MASK ((1 << PUCAN_TFAST_SJW_BITS) - 1)88 89/* uCAN TIMING_FAST command fields */90#define PUCAN_TFAST_SJW(s) ((s) & PUCAN_TFAST_SJW_MASK)91#define PUCAN_TFAST_TSEG2(t) ((t) & PUCAN_TFAST_TSEG2_MASK)92#define PUCAN_TFAST_TSEG1(t) ((t) & PUCAN_TFAST_TSEG1_MASK)93#define PUCAN_TFAST_BRP(b) ((b) & PUCAN_TFAST_BRP_MASK)94 95struct __packed pucan_timing_fast {96 __le16 opcode_channel;97 98 u8 unused;99 u8 sjw; /* Sync Jump Width */100 u8 tseg2; /* Timing SEGment 2 */101 u8 tseg1; /* Timing SEGment 1 */102 103 __le16 brp; /* BaudRate Prescaler */104};105 106/* uCAN FILTER_STD command fields */107#define PUCAN_FLTSTD_ROW_IDX_BITS 6108 109struct __packed pucan_filter_std {110 __le16 opcode_channel;111 112 __le16 idx;113 __le32 mask; /* CAN-ID bitmask in idx range */114};115 116#define PUCAN_FLTSTD_ROW_IDX_MAX ((1 << PUCAN_FLTSTD_ROW_IDX_BITS) - 1)117 118/* uCAN SET_STD_FILTER command fields */119struct __packed pucan_std_filter {120 __le16 opcode_channel;121 122 u8 unused;123 u8 idx;124 __le32 mask; /* CAN-ID bitmask in idx range */125};126 127/* uCAN TX_ABORT commands fields */128#define PUCAN_TX_ABORT_FLUSH 0x0001129 130struct __packed pucan_tx_abort {131 __le16 opcode_channel;132 133 __le16 flags;134 u32 unused;135};136 137/* uCAN WR_ERR_CNT command fields */138#define PUCAN_WRERRCNT_TE 0x4000 /* Tx error cntr write Enable */139#define PUCAN_WRERRCNT_RE 0x8000 /* Rx error cntr write Enable */140 141struct __packed pucan_wr_err_cnt {142 __le16 opcode_channel;143 144 __le16 sel_mask;145 u8 tx_counter; /* Tx error counter new value */146 u8 rx_counter; /* Rx error counter new value */147 148 u16 unused;149};150 151/* uCAN SET_EN/CLR_DIS _OPTION command fields */152#define PUCAN_OPTION_ERROR 0x0001153#define PUCAN_OPTION_BUSLOAD 0x0002154#define PUCAN_OPTION_CANDFDISO 0x0004155 156struct __packed pucan_options {157 __le16 opcode_channel;158 159 __le16 options;160 u32 unused;161};162 163/* uCAN received messages global format */164struct __packed pucan_msg {165 __le16 size;166 __le16 type;167 __le32 ts_low;168 __le32 ts_high;169};170 171/* uCAN flags for CAN/CANFD messages */172#define PUCAN_MSG_SELF_RECEIVE 0x80173#define PUCAN_MSG_ERROR_STATE_IND 0x40 /* error state indicator */174#define PUCAN_MSG_BITRATE_SWITCH 0x20 /* bitrate switch */175#define PUCAN_MSG_EXT_DATA_LEN 0x10 /* extended data length */176#define PUCAN_MSG_SINGLE_SHOT 0x08177#define PUCAN_MSG_LOOPED_BACK 0x04178#define PUCAN_MSG_EXT_ID 0x02179#define PUCAN_MSG_RTR 0x01180 181struct __packed pucan_rx_msg {182 __le16 size;183 __le16 type;184 __le32 ts_low;185 __le32 ts_high;186 __le32 tag_low;187 __le32 tag_high;188 u8 channel_dlc;189 u8 client;190 __le16 flags;191 __le32 can_id;192 u8 d[];193};194 195/* uCAN error types */196#define PUCAN_ERMSG_BIT_ERROR 0197#define PUCAN_ERMSG_FORM_ERROR 1198#define PUCAN_ERMSG_STUFF_ERROR 2199#define PUCAN_ERMSG_OTHER_ERROR 3200#define PUCAN_ERMSG_ERR_CNT_DEC 4201 202struct __packed pucan_error_msg {203 __le16 size;204 __le16 type;205 __le32 ts_low;206 __le32 ts_high;207 u8 channel_type_d;208 u8 code_g;209 u8 tx_err_cnt;210 u8 rx_err_cnt;211};212 213static inline int pucan_error_get_channel(const struct pucan_error_msg *msg)214{215 return msg->channel_type_d & 0x0f;216}217 218#define PUCAN_RX_BARRIER 0x10219#define PUCAN_BUS_PASSIVE 0x20220#define PUCAN_BUS_WARNING 0x40221#define PUCAN_BUS_BUSOFF 0x80222 223struct __packed pucan_status_msg {224 __le16 size;225 __le16 type;226 __le32 ts_low;227 __le32 ts_high;228 u8 channel_p_w_b;229 u8 unused[3];230};231 232static inline int pucan_status_get_channel(const struct pucan_status_msg *msg)233{234 return msg->channel_p_w_b & 0x0f;235}236 237static inline int pucan_status_is_rx_barrier(const struct pucan_status_msg *msg)238{239 return msg->channel_p_w_b & PUCAN_RX_BARRIER;240}241 242static inline int pucan_status_is_passive(const struct pucan_status_msg *msg)243{244 return msg->channel_p_w_b & PUCAN_BUS_PASSIVE;245}246 247static inline int pucan_status_is_warning(const struct pucan_status_msg *msg)248{249 return msg->channel_p_w_b & PUCAN_BUS_WARNING;250}251 252static inline int pucan_status_is_busoff(const struct pucan_status_msg *msg)253{254 return msg->channel_p_w_b & PUCAN_BUS_BUSOFF;255}256 257/* uCAN transmitted message format */258#define PUCAN_MSG_CHANNEL_DLC(c, d) (((c) & 0xf) | ((d) << 4))259 260struct __packed pucan_tx_msg {261 __le16 size;262 __le16 type;263 __le32 tag_low;264 __le32 tag_high;265 u8 channel_dlc;266 u8 client;267 __le16 flags;268 __le32 can_id;269 u8 d[];270};271 272/* build the cmd opcode_channel field with respect to the correct endianness */273static inline __le16 pucan_cmd_opcode_channel(int index, int opcode)274{275 return cpu_to_le16(((index) << 12) | ((opcode) & 0x3ff));276}277 278/* return the channel number part from any received message channel_dlc field */279static inline int pucan_msg_get_channel(const struct pucan_rx_msg *msg)280{281 return msg->channel_dlc & 0xf;282}283 284/* return the dlc value from any received message channel_dlc field */285static inline u8 pucan_msg_get_dlc(const struct pucan_rx_msg *msg)286{287 return msg->channel_dlc >> 4;288}289 290static inline int pucan_ermsg_get_channel(const struct pucan_error_msg *msg)291{292 return msg->channel_type_d & 0x0f;293}294 295static inline int pucan_stmsg_get_channel(const struct pucan_status_msg *msg)296{297 return msg->channel_p_w_b & 0x0f;298}299 300#endif301