pcan_ucan.h 6.4 KB

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