pcan_ucan.h 5.5 KB

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