peak_canfd.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * CAN driver for PEAK System micro-CAN based adapters
  4. *
  5. * Copyright (C) 2003-2011 PEAK System-Technik GmbH
  6. * Copyright (C) 2011-2013 Stephane Grosjean <[email protected]>
  7. */
  8. #ifndef PUCAN_H
  9. #define PUCAN_H
  10. /* uCAN commands opcodes list (low-order 10 bits) */
  11. #define PUCAN_CMD_NOP 0x000
  12. #define PUCAN_CMD_RESET_MODE 0x001
  13. #define PUCAN_CMD_NORMAL_MODE 0x002
  14. #define PUCAN_CMD_LISTEN_ONLY_MODE 0x003
  15. #define PUCAN_CMD_TIMING_SLOW 0x004
  16. #define PUCAN_CMD_TIMING_FAST 0x005
  17. #define PUCAN_CMD_SET_STD_FILTER 0x006
  18. #define PUCAN_CMD_RESERVED2 0x007
  19. #define PUCAN_CMD_FILTER_STD 0x008
  20. #define PUCAN_CMD_TX_ABORT 0x009
  21. #define PUCAN_CMD_WR_ERR_CNT 0x00a
  22. #define PUCAN_CMD_SET_EN_OPTION 0x00b
  23. #define PUCAN_CMD_CLR_DIS_OPTION 0x00c
  24. #define PUCAN_CMD_RX_BARRIER 0x010
  25. #define PUCAN_CMD_END_OF_COLLECTION 0x3ff
  26. /* uCAN received messages list */
  27. #define PUCAN_MSG_CAN_RX 0x0001
  28. #define PUCAN_MSG_ERROR 0x0002
  29. #define PUCAN_MSG_STATUS 0x0003
  30. #define PUCAN_MSG_BUSLOAD 0x0004
  31. #define PUCAN_MSG_CACHE_CRITICAL 0x0102
  32. /* uCAN transmitted messages */
  33. #define PUCAN_MSG_CAN_TX 0x1000
  34. /* uCAN command common header */
  35. struct __packed pucan_command {
  36. __le16 opcode_channel;
  37. u16 args[3];
  38. };
  39. /* return the opcode from the opcode_channel field of a command */
  40. static inline u16 pucan_cmd_get_opcode(struct pucan_command *c)
  41. {
  42. return le16_to_cpu(c->opcode_channel) & 0x3ff;
  43. }
  44. #define PUCAN_TSLOW_BRP_BITS 10
  45. #define PUCAN_TSLOW_TSGEG1_BITS 8
  46. #define PUCAN_TSLOW_TSGEG2_BITS 7
  47. #define PUCAN_TSLOW_SJW_BITS 7
  48. #define PUCAN_TSLOW_BRP_MASK ((1 << PUCAN_TSLOW_BRP_BITS) - 1)
  49. #define PUCAN_TSLOW_TSEG1_MASK ((1 << PUCAN_TSLOW_TSGEG1_BITS) - 1)
  50. #define PUCAN_TSLOW_TSEG2_MASK ((1 << PUCAN_TSLOW_TSGEG2_BITS) - 1)
  51. #define PUCAN_TSLOW_SJW_MASK ((1 << PUCAN_TSLOW_SJW_BITS) - 1)
  52. /* uCAN TIMING_SLOW command fields */
  53. #define PUCAN_TSLOW_SJW_T(s, t) (((s) & PUCAN_TSLOW_SJW_MASK) | \
  54. ((!!(t)) << 7))
  55. #define PUCAN_TSLOW_TSEG2(t) ((t) & PUCAN_TSLOW_TSEG2_MASK)
  56. #define PUCAN_TSLOW_TSEG1(t) ((t) & PUCAN_TSLOW_TSEG1_MASK)
  57. #define PUCAN_TSLOW_BRP(b) ((b) & PUCAN_TSLOW_BRP_MASK)
  58. struct __packed pucan_timing_slow {
  59. __le16 opcode_channel;
  60. u8 ewl; /* Error Warning limit */
  61. u8 sjw_t; /* Sync Jump Width + Triple sampling */
  62. u8 tseg2; /* Timing SEGment 2 */
  63. u8 tseg1; /* Timing SEGment 1 */
  64. __le16 brp; /* BaudRate Prescaler */
  65. };
  66. #define PUCAN_TFAST_BRP_BITS 10
  67. #define PUCAN_TFAST_TSGEG1_BITS 5
  68. #define PUCAN_TFAST_TSGEG2_BITS 4
  69. #define PUCAN_TFAST_SJW_BITS 4
  70. #define PUCAN_TFAST_BRP_MASK ((1 << PUCAN_TFAST_BRP_BITS) - 1)
  71. #define PUCAN_TFAST_TSEG1_MASK ((1 << PUCAN_TFAST_TSGEG1_BITS) - 1)
  72. #define PUCAN_TFAST_TSEG2_MASK ((1 << PUCAN_TFAST_TSGEG2_BITS) - 1)
  73. #define PUCAN_TFAST_SJW_MASK ((1 << PUCAN_TFAST_SJW_BITS) - 1)
  74. /* uCAN TIMING_FAST command fields */
  75. #define PUCAN_TFAST_SJW(s) ((s) & PUCAN_TFAST_SJW_MASK)
  76. #define PUCAN_TFAST_TSEG2(t) ((t) & PUCAN_TFAST_TSEG2_MASK)
  77. #define PUCAN_TFAST_TSEG1(t) ((t) & PUCAN_TFAST_TSEG1_MASK)
  78. #define PUCAN_TFAST_BRP(b) ((b) & PUCAN_TFAST_BRP_MASK)
  79. struct __packed pucan_timing_fast {
  80. __le16 opcode_channel;
  81. u8 unused;
  82. u8 sjw; /* Sync Jump Width */
  83. u8 tseg2; /* Timing SEGment 2 */
  84. u8 tseg1; /* Timing SEGment 1 */
  85. __le16 brp; /* BaudRate Prescaler */
  86. };
  87. /* uCAN FILTER_STD command fields */
  88. #define PUCAN_FLTSTD_ROW_IDX_BITS 6
  89. struct __packed pucan_filter_std {
  90. __le16 opcode_channel;
  91. __le16 idx;
  92. __le32 mask; /* CAN-ID bitmask in idx range */
  93. };
  94. #define PUCAN_FLTSTD_ROW_IDX_MAX ((1 << PUCAN_FLTSTD_ROW_IDX_BITS) - 1)
  95. /* uCAN SET_STD_FILTER command fields */
  96. struct __packed pucan_std_filter {
  97. __le16 opcode_channel;
  98. u8 unused;
  99. u8 idx;
  100. __le32 mask; /* CAN-ID bitmask in idx range */
  101. };
  102. /* uCAN TX_ABORT commands fields */
  103. #define PUCAN_TX_ABORT_FLUSH 0x0001
  104. struct __packed pucan_tx_abort {
  105. __le16 opcode_channel;
  106. __le16 flags;
  107. u32 unused;
  108. };
  109. /* uCAN WR_ERR_CNT command fields */
  110. #define PUCAN_WRERRCNT_TE 0x4000 /* Tx error cntr write Enable */
  111. #define PUCAN_WRERRCNT_RE 0x8000 /* Rx error cntr write Enable */
  112. struct __packed pucan_wr_err_cnt {
  113. __le16 opcode_channel;
  114. __le16 sel_mask;
  115. u8 tx_counter; /* Tx error counter new value */
  116. u8 rx_counter; /* Rx error counter new value */
  117. u16 unused;
  118. };
  119. /* uCAN SET_EN/CLR_DIS _OPTION command fields */
  120. #define PUCAN_OPTION_ERROR 0x0001
  121. #define PUCAN_OPTION_BUSLOAD 0x0002
  122. #define PUCAN_OPTION_CANDFDISO 0x0004
  123. struct __packed pucan_options {
  124. __le16 opcode_channel;
  125. __le16 options;
  126. u32 unused;
  127. };
  128. /* uCAN received messages global format */
  129. struct __packed pucan_msg {
  130. __le16 size;
  131. __le16 type;
  132. __le32 ts_low;
  133. __le32 ts_high;
  134. };
  135. /* uCAN flags for CAN/CANFD messages */
  136. #define PUCAN_MSG_SELF_RECEIVE 0x80
  137. #define PUCAN_MSG_ERROR_STATE_IND 0x40 /* error state indicator */
  138. #define PUCAN_MSG_BITRATE_SWITCH 0x20 /* bitrate switch */
  139. #define PUCAN_MSG_EXT_DATA_LEN 0x10 /* extended data length */
  140. #define PUCAN_MSG_SINGLE_SHOT 0x08
  141. #define PUCAN_MSG_LOOPED_BACK 0x04
  142. #define PUCAN_MSG_EXT_ID 0x02
  143. #define PUCAN_MSG_RTR 0x01
  144. struct __packed pucan_rx_msg {
  145. __le16 size;
  146. __le16 type;
  147. __le32 ts_low;
  148. __le32 ts_high;
  149. __le32 tag_low;
  150. __le32 tag_high;
  151. u8 channel_dlc;
  152. u8 client;
  153. __le16 flags;
  154. __le32 can_id;
  155. u8 d[];
  156. };
  157. /* uCAN error types */
  158. #define PUCAN_ERMSG_BIT_ERROR 0
  159. #define PUCAN_ERMSG_FORM_ERROR 1
  160. #define PUCAN_ERMSG_STUFF_ERROR 2
  161. #define PUCAN_ERMSG_OTHER_ERROR 3
  162. #define PUCAN_ERMSG_ERR_CNT_DEC 4
  163. struct __packed pucan_error_msg {
  164. __le16 size;
  165. __le16 type;
  166. __le32 ts_low;
  167. __le32 ts_high;
  168. u8 channel_type_d;
  169. u8 code_g;
  170. u8 tx_err_cnt;
  171. u8 rx_err_cnt;
  172. };
  173. static inline int pucan_error_get_channel(const struct pucan_error_msg *msg)
  174. {
  175. return msg->channel_type_d & 0x0f;
  176. }
  177. #define PUCAN_RX_BARRIER 0x10
  178. #define PUCAN_BUS_PASSIVE 0x20
  179. #define PUCAN_BUS_WARNING 0x40
  180. #define PUCAN_BUS_BUSOFF 0x80
  181. struct __packed pucan_status_msg {
  182. __le16 size;
  183. __le16 type;
  184. __le32 ts_low;
  185. __le32 ts_high;
  186. u8 channel_p_w_b;
  187. u8 unused[3];
  188. };
  189. static inline int pucan_status_get_channel(const struct pucan_status_msg *msg)
  190. {
  191. return msg->channel_p_w_b & 0x0f;
  192. }
  193. static inline int pucan_status_is_rx_barrier(const struct pucan_status_msg *msg)
  194. {
  195. return msg->channel_p_w_b & PUCAN_RX_BARRIER;
  196. }
  197. static inline int pucan_status_is_passive(const struct pucan_status_msg *msg)
  198. {
  199. return msg->channel_p_w_b & PUCAN_BUS_PASSIVE;
  200. }
  201. static inline int pucan_status_is_warning(const struct pucan_status_msg *msg)
  202. {
  203. return msg->channel_p_w_b & PUCAN_BUS_WARNING;
  204. }
  205. static inline int pucan_status_is_busoff(const struct pucan_status_msg *msg)
  206. {
  207. return msg->channel_p_w_b & PUCAN_BUS_BUSOFF;
  208. }
  209. /* uCAN transmitted message format */
  210. #define PUCAN_MSG_CHANNEL_DLC(c, d) (((c) & 0xf) | ((d) << 4))
  211. struct __packed pucan_tx_msg {
  212. __le16 size;
  213. __le16 type;
  214. __le32 tag_low;
  215. __le32 tag_high;
  216. u8 channel_dlc;
  217. u8 client;
  218. __le16 flags;
  219. __le32 can_id;
  220. u8 d[];
  221. };
  222. /* build the cmd opcode_channel field with respect to the correct endianness */
  223. static inline __le16 pucan_cmd_opcode_channel(int index, int opcode)
  224. {
  225. return cpu_to_le16(((index) << 12) | ((opcode) & 0x3ff));
  226. }
  227. /* return the channel number part from any received message channel_dlc field */
  228. static inline int pucan_msg_get_channel(const struct pucan_rx_msg *msg)
  229. {
  230. return msg->channel_dlc & 0xf;
  231. }
  232. /* return the dlc value from any received message channel_dlc field */
  233. static inline u8 pucan_msg_get_dlc(const struct pucan_rx_msg *msg)
  234. {
  235. return msg->channel_dlc >> 4;
  236. }
  237. static inline int pucan_ermsg_get_channel(const struct pucan_error_msg *msg)
  238. {
  239. return msg->channel_type_d & 0x0f;
  240. }
  241. static inline int pucan_stmsg_get_channel(const struct pucan_status_msg *msg)
  242. {
  243. return msg->channel_p_w_b & 0x0f;
  244. }
  245. #endif