pcan_ucan.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. /* 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 SET_EN/CLR_DIS _OPTION command fields */
  86. #define PUCAN_OPTION_ERROR 0x0001
  87. #define PUCAN_OPTION_BUSLOAD 0x0002
  88. #define PUCAN_OPTION_CANDFDISO 0x0004
  89. struct __packed pucan_options {
  90. __le16 opcode_channel;
  91. __le16 options;
  92. u32 unused;
  93. };
  94. /* uCAN received messages global format */
  95. struct __packed pucan_msg {
  96. __le16 size;
  97. __le16 type;
  98. __le32 ts_low;
  99. __le32 ts_high;
  100. };
  101. /* uCAN flags for CAN/CANFD messages */
  102. #define PUCAN_MSG_SELF_RECEIVE 0x80
  103. #define PUCAN_MSG_ERROR_STATE_IND 0x40 /* error state indicator */
  104. #define PUCAN_MSG_BITRATE_SWITCH 0x20 /* bitrate switch */
  105. #define PUCAN_MSG_EXT_DATA_LEN 0x10 /* extended data length */
  106. #define PUCAN_MSG_SINGLE_SHOT 0x08
  107. #define PUCAN_MSG_LOOPED_BACK 0x04
  108. #define PUCAN_MSG_EXT_ID 0x02
  109. #define PUCAN_MSG_RTR 0x01
  110. struct __packed pucan_rx_msg {
  111. __le16 size;
  112. __le16 type;
  113. __le32 ts_low;
  114. __le32 ts_high;
  115. __le32 tag_low;
  116. __le32 tag_high;
  117. u8 channel_dlc;
  118. u8 client;
  119. __le16 flags;
  120. __le32 can_id;
  121. u8 d[0];
  122. };
  123. /* uCAN error types */
  124. #define PUCAN_ERMSG_BIT_ERROR 0
  125. #define PUCAN_ERMSG_FORM_ERROR 1
  126. #define PUCAN_ERMSG_STUFF_ERROR 2
  127. #define PUCAN_ERMSG_OTHER_ERROR 3
  128. #define PUCAN_ERMSG_ERR_CNT_DEC 4
  129. struct __packed pucan_error_msg {
  130. __le16 size;
  131. __le16 type;
  132. __le32 ts_low;
  133. __le32 ts_high;
  134. u8 channel_type_d;
  135. u8 code_g;
  136. u8 tx_err_cnt;
  137. u8 rx_err_cnt;
  138. };
  139. #define PUCAN_BUS_PASSIVE 0x20
  140. #define PUCAN_BUS_WARNING 0x40
  141. #define PUCAN_BUS_BUSOFF 0x80
  142. struct __packed pucan_status_msg {
  143. __le16 size;
  144. __le16 type;
  145. __le32 ts_low;
  146. __le32 ts_high;
  147. u8 channel_p_w_b;
  148. u8 unused[3];
  149. };
  150. /* uCAN transmitted message format */
  151. #define PUCAN_MSG_CHANNEL_DLC(c, d) (((c) & 0xf) | ((d) << 4))
  152. struct __packed pucan_tx_msg {
  153. __le16 size;
  154. __le16 type;
  155. __le32 tag_low;
  156. __le32 tag_high;
  157. u8 channel_dlc;
  158. u8 client;
  159. __le16 flags;
  160. __le32 can_id;
  161. u8 d[0];
  162. };
  163. /* build the cmd opcode_channel field with respect to the correct endianness */
  164. static inline __le16 pucan_cmd_opcode_channel(struct peak_usb_device *dev,
  165. int opcode)
  166. {
  167. return cpu_to_le16(((dev->ctrl_idx) << 12) | ((opcode) & 0x3ff));
  168. }
  169. /* return the channel number part from any received message channel_dlc field */
  170. static inline int pucan_msg_get_channel(struct pucan_rx_msg *rm)
  171. {
  172. return rm->channel_dlc & 0xf;
  173. }
  174. /* return the dlc value from any received message channel_dlc field */
  175. static inline int pucan_msg_get_dlc(struct pucan_rx_msg *rm)
  176. {
  177. return rm->channel_dlc >> 4;
  178. }
  179. static inline int pucan_ermsg_get_channel(struct pucan_error_msg *em)
  180. {
  181. return em->channel_type_d & 0x0f;
  182. }
  183. static inline int pucan_stmsg_get_channel(struct pucan_status_msg *sm)
  184. {
  185. return sm->channel_p_w_b & 0x0f;
  186. }
  187. #endif