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_FILTER_STD 0x008 27 #define PUCAN_CMD_TX_ABORT 0x009 28 #define PUCAN_CMD_WR_ERR_CNT 0x00a 29 #define PUCAN_CMD_SET_EN_OPTION 0x00b 30 #define PUCAN_CMD_CLR_DIS_OPTION 0x00c 31 #define PUCAN_CMD_END_OF_COLLECTION 0x3ff 32 33 /* uCAN received messages list */ 34 #define PUCAN_MSG_CAN_RX 0x0001 35 #define PUCAN_MSG_ERROR 0x0002 36 #define PUCAN_MSG_STATUS 0x0003 37 #define PUCAN_MSG_BUSLOAD 0x0004 38 #define PUCAN_MSG_CAN_TX 0x1000 39 40 /* uCAN command common header */ 41 struct __packed pucan_command { 42 __le16 opcode_channel; 43 u16 args[3]; 44 }; 45 46 /* uCAN TIMING_SLOW command fields */ 47 #define PUCAN_TSLOW_SJW_T(s, t) (((s) & 0xf) | ((!!(t)) << 7)) 48 #define PUCAN_TSLOW_TSEG2(t) ((t) & 0xf) 49 #define PUCAN_TSLOW_TSEG1(t) ((t) & 0x3f) 50 #define PUCAN_TSLOW_BRP(b) ((b) & 0x3ff) 51 52 struct __packed pucan_timing_slow { 53 __le16 opcode_channel; 54 55 u8 ewl; /* Error Warning limit */ 56 u8 sjw_t; /* Sync Jump Width + Triple sampling */ 57 u8 tseg2; /* Timing SEGment 2 */ 58 u8 tseg1; /* Timing SEGment 1 */ 59 60 __le16 brp; /* BaudRate Prescaler */ 61 }; 62 63 /* uCAN TIMING_FAST command fields */ 64 #define PUCAN_TFAST_SJW(s) ((s) & 0x3) 65 #define PUCAN_TFAST_TSEG2(t) ((t) & 0x7) 66 #define PUCAN_TFAST_TSEG1(t) ((t) & 0xf) 67 #define PUCAN_TFAST_BRP(b) ((b) & 0x3ff) 68 69 struct __packed pucan_timing_fast { 70 __le16 opcode_channel; 71 72 u8 unused; 73 u8 sjw; /* Sync Jump Width */ 74 u8 tseg2; /* Timing SEGment 2 */ 75 u8 tseg1; /* Timing SEGment 1 */ 76 77 __le16 brp; /* BaudRate Prescaler */ 78 }; 79 80 /* uCAN FILTER_STD command fields */ 81 #define PUCAN_FLTSTD_ROW_IDX_BITS 6 82 83 struct __packed pucan_filter_std { 84 __le16 opcode_channel; 85 86 __le16 idx; 87 __le32 mask; /* CAN-ID bitmask in idx range */ 88 }; 89 90 /* uCAN WR_ERR_CNT command fields */ 91 #define PUCAN_WRERRCNT_TE 0x4000 /* Tx error cntr write Enable */ 92 #define PUCAN_WRERRCNT_RE 0x8000 /* Rx error cntr write Enable */ 93 94 struct __packed pucan_wr_err_cnt { 95 __le16 opcode_channel; 96 97 __le16 sel_mask; 98 u8 tx_counter; /* Tx error counter new value */ 99 u8 rx_counter; /* Rx error counter new value */ 100 101 u16 unused; 102 }; 103 104 /* uCAN SET_EN/CLR_DIS _OPTION command fields */ 105 #define PUCAN_OPTION_ERROR 0x0001 106 #define PUCAN_OPTION_BUSLOAD 0x0002 107 #define PUCAN_OPTION_CANDFDISO 0x0004 108 109 struct __packed pucan_options { 110 __le16 opcode_channel; 111 112 __le16 options; 113 u32 unused; 114 }; 115 116 /* uCAN received messages global format */ 117 struct __packed pucan_msg { 118 __le16 size; 119 __le16 type; 120 __le32 ts_low; 121 __le32 ts_high; 122 }; 123 124 /* uCAN flags for CAN/CANFD messages */ 125 #define PUCAN_MSG_SELF_RECEIVE 0x80 126 #define PUCAN_MSG_ERROR_STATE_IND 0x40 /* error state indicator */ 127 #define PUCAN_MSG_BITRATE_SWITCH 0x20 /* bitrate switch */ 128 #define PUCAN_MSG_EXT_DATA_LEN 0x10 /* extended data length */ 129 #define PUCAN_MSG_SINGLE_SHOT 0x08 130 #define PUCAN_MSG_LOOPED_BACK 0x04 131 #define PUCAN_MSG_EXT_ID 0x02 132 #define PUCAN_MSG_RTR 0x01 133 134 struct __packed pucan_rx_msg { 135 __le16 size; 136 __le16 type; 137 __le32 ts_low; 138 __le32 ts_high; 139 __le32 tag_low; 140 __le32 tag_high; 141 u8 channel_dlc; 142 u8 client; 143 __le16 flags; 144 __le32 can_id; 145 u8 d[0]; 146 }; 147 148 /* uCAN error types */ 149 #define PUCAN_ERMSG_BIT_ERROR 0 150 #define PUCAN_ERMSG_FORM_ERROR 1 151 #define PUCAN_ERMSG_STUFF_ERROR 2 152 #define PUCAN_ERMSG_OTHER_ERROR 3 153 #define PUCAN_ERMSG_ERR_CNT_DEC 4 154 155 struct __packed pucan_error_msg { 156 __le16 size; 157 __le16 type; 158 __le32 ts_low; 159 __le32 ts_high; 160 u8 channel_type_d; 161 u8 code_g; 162 u8 tx_err_cnt; 163 u8 rx_err_cnt; 164 }; 165 166 #define PUCAN_BUS_PASSIVE 0x20 167 #define PUCAN_BUS_WARNING 0x40 168 #define PUCAN_BUS_BUSOFF 0x80 169 170 struct __packed pucan_status_msg { 171 __le16 size; 172 __le16 type; 173 __le32 ts_low; 174 __le32 ts_high; 175 u8 channel_p_w_b; 176 u8 unused[3]; 177 }; 178 179 /* uCAN transmitted message format */ 180 #define PUCAN_MSG_CHANNEL_DLC(c, d) (((c) & 0xf) | ((d) << 4)) 181 182 struct __packed pucan_tx_msg { 183 __le16 size; 184 __le16 type; 185 __le32 tag_low; 186 __le32 tag_high; 187 u8 channel_dlc; 188 u8 client; 189 __le16 flags; 190 __le32 can_id; 191 u8 d[0]; 192 }; 193 194 /* build the cmd opcode_channel field with respect to the correct endianness */ pucan_cmd_opcode_channel(struct peak_usb_device * dev,int opcode)195static inline __le16 pucan_cmd_opcode_channel(struct peak_usb_device *dev, 196 int opcode) 197 { 198 return cpu_to_le16(((dev->ctrl_idx) << 12) | ((opcode) & 0x3ff)); 199 } 200 201 /* return the channel number part from any received message channel_dlc field */ pucan_msg_get_channel(struct pucan_rx_msg * rm)202static inline int pucan_msg_get_channel(struct pucan_rx_msg *rm) 203 { 204 return rm->channel_dlc & 0xf; 205 } 206 207 /* return the dlc value from any received message channel_dlc field */ pucan_msg_get_dlc(struct pucan_rx_msg * rm)208static inline int pucan_msg_get_dlc(struct pucan_rx_msg *rm) 209 { 210 return rm->channel_dlc >> 4; 211 } 212 pucan_ermsg_get_channel(struct pucan_error_msg * em)213static inline int pucan_ermsg_get_channel(struct pucan_error_msg *em) 214 { 215 return em->channel_type_d & 0x0f; 216 } 217 pucan_stmsg_get_channel(struct pucan_status_msg * sm)218static inline int pucan_stmsg_get_channel(struct pucan_status_msg *sm) 219 { 220 return sm->channel_p_w_b & 0x0f; 221 } 222 223 #endif 224