cpts.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /*
  2. * TI Common Platform Time Sync
  3. *
  4. * Copyright (C) 2012 Richard Cochran <richardcochran@gmail.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #ifndef _TI_CPTS_H_
  21. #define _TI_CPTS_H_
  22. #if IS_ENABLED(CONFIG_TI_CPTS)
  23. #include <linux/clk.h>
  24. #include <linux/clkdev.h>
  25. #include <linux/clocksource.h>
  26. #include <linux/device.h>
  27. #include <linux/list.h>
  28. #include <linux/of.h>
  29. #include <linux/ptp_clock_kernel.h>
  30. #include <linux/skbuff.h>
  31. #include <linux/ptp_classify.h>
  32. #include <linux/timecounter.h>
  33. struct cpsw_cpts {
  34. u32 idver; /* Identification and version */
  35. u32 control; /* Time sync control */
  36. u32 res1;
  37. u32 ts_push; /* Time stamp event push */
  38. u32 ts_load_val; /* Time stamp load value */
  39. u32 ts_load_en; /* Time stamp load enable */
  40. u32 res2[2];
  41. u32 intstat_raw; /* Time sync interrupt status raw */
  42. u32 intstat_masked; /* Time sync interrupt status masked */
  43. u32 int_enable; /* Time sync interrupt enable */
  44. u32 res3;
  45. u32 event_pop; /* Event interrupt pop */
  46. u32 event_low; /* 32 Bit Event Time Stamp */
  47. u32 event_high; /* Event Type Fields */
  48. };
  49. /* Bit definitions for the IDVER register */
  50. #define TX_IDENT_SHIFT (16) /* TX Identification Value */
  51. #define TX_IDENT_MASK (0xffff)
  52. #define RTL_VER_SHIFT (11) /* RTL Version Value */
  53. #define RTL_VER_MASK (0x1f)
  54. #define MAJOR_VER_SHIFT (8) /* Major Version Value */
  55. #define MAJOR_VER_MASK (0x7)
  56. #define MINOR_VER_SHIFT (0) /* Minor Version Value */
  57. #define MINOR_VER_MASK (0xff)
  58. /* Bit definitions for the CONTROL register */
  59. #define HW4_TS_PUSH_EN (1<<11) /* Hardware push 4 enable */
  60. #define HW3_TS_PUSH_EN (1<<10) /* Hardware push 3 enable */
  61. #define HW2_TS_PUSH_EN (1<<9) /* Hardware push 2 enable */
  62. #define HW1_TS_PUSH_EN (1<<8) /* Hardware push 1 enable */
  63. #define INT_TEST (1<<1) /* Interrupt Test */
  64. #define CPTS_EN (1<<0) /* Time Sync Enable */
  65. /*
  66. * Definitions for the single bit resisters:
  67. * TS_PUSH TS_LOAD_EN INTSTAT_RAW INTSTAT_MASKED INT_ENABLE EVENT_POP
  68. */
  69. #define TS_PUSH (1<<0) /* Time stamp event push */
  70. #define TS_LOAD_EN (1<<0) /* Time Stamp Load */
  71. #define TS_PEND_RAW (1<<0) /* int read (before enable) */
  72. #define TS_PEND (1<<0) /* masked interrupt read (after enable) */
  73. #define TS_PEND_EN (1<<0) /* masked interrupt enable */
  74. #define EVENT_POP (1<<0) /* writing discards one event */
  75. /* Bit definitions for the EVENT_HIGH register */
  76. #define PORT_NUMBER_SHIFT (24) /* Indicates Ethernet port or HW pin */
  77. #define PORT_NUMBER_MASK (0x1f)
  78. #define EVENT_TYPE_SHIFT (20) /* Time sync event type */
  79. #define EVENT_TYPE_MASK (0xf)
  80. #define MESSAGE_TYPE_SHIFT (16) /* PTP message type */
  81. #define MESSAGE_TYPE_MASK (0xf)
  82. #define SEQUENCE_ID_SHIFT (0) /* PTP message sequence ID */
  83. #define SEQUENCE_ID_MASK (0xffff)
  84. enum {
  85. CPTS_EV_PUSH, /* Time Stamp Push Event */
  86. CPTS_EV_ROLL, /* Time Stamp Rollover Event */
  87. CPTS_EV_HALF, /* Time Stamp Half Rollover Event */
  88. CPTS_EV_HW, /* Hardware Time Stamp Push Event */
  89. CPTS_EV_RX, /* Ethernet Receive Event */
  90. CPTS_EV_TX, /* Ethernet Transmit Event */
  91. };
  92. #define CPTS_FIFO_DEPTH 16
  93. #define CPTS_MAX_EVENTS 32
  94. struct cpts_event {
  95. struct list_head list;
  96. unsigned long tmo;
  97. u32 high;
  98. u32 low;
  99. };
  100. struct cpts {
  101. struct device *dev;
  102. struct cpsw_cpts __iomem *reg;
  103. int tx_enable;
  104. int rx_enable;
  105. struct ptp_clock_info info;
  106. struct ptp_clock *clock;
  107. spinlock_t lock; /* protects time registers */
  108. u32 cc_mult; /* for the nominal frequency */
  109. struct cyclecounter cc;
  110. struct timecounter tc;
  111. int phc_index;
  112. struct clk *refclk;
  113. struct list_head events;
  114. struct list_head pool;
  115. struct cpts_event pool_data[CPTS_MAX_EVENTS];
  116. unsigned long ov_check_period;
  117. struct sk_buff_head txq;
  118. };
  119. void cpts_rx_timestamp(struct cpts *cpts, struct sk_buff *skb);
  120. void cpts_tx_timestamp(struct cpts *cpts, struct sk_buff *skb);
  121. int cpts_register(struct cpts *cpts);
  122. void cpts_unregister(struct cpts *cpts);
  123. struct cpts *cpts_create(struct device *dev, void __iomem *regs,
  124. struct device_node *node);
  125. void cpts_release(struct cpts *cpts);
  126. static inline void cpts_rx_enable(struct cpts *cpts, int enable)
  127. {
  128. cpts->rx_enable = enable;
  129. }
  130. static inline bool cpts_is_rx_enabled(struct cpts *cpts)
  131. {
  132. return !!cpts->rx_enable;
  133. }
  134. static inline void cpts_tx_enable(struct cpts *cpts, int enable)
  135. {
  136. cpts->tx_enable = enable;
  137. }
  138. static inline bool cpts_is_tx_enabled(struct cpts *cpts)
  139. {
  140. return !!cpts->tx_enable;
  141. }
  142. static inline bool cpts_can_timestamp(struct cpts *cpts, struct sk_buff *skb)
  143. {
  144. unsigned int class = ptp_classify_raw(skb);
  145. if (class == PTP_CLASS_NONE)
  146. return false;
  147. return true;
  148. }
  149. #else
  150. struct cpts;
  151. static inline void cpts_rx_timestamp(struct cpts *cpts, struct sk_buff *skb)
  152. {
  153. }
  154. static inline void cpts_tx_timestamp(struct cpts *cpts, struct sk_buff *skb)
  155. {
  156. }
  157. static inline
  158. struct cpts *cpts_create(struct device *dev, void __iomem *regs,
  159. struct device_node *node)
  160. {
  161. return NULL;
  162. }
  163. static inline void cpts_release(struct cpts *cpts)
  164. {
  165. }
  166. static inline int
  167. cpts_register(struct cpts *cpts)
  168. {
  169. return 0;
  170. }
  171. static inline void cpts_unregister(struct cpts *cpts)
  172. {
  173. }
  174. static inline void cpts_rx_enable(struct cpts *cpts, int enable)
  175. {
  176. }
  177. static inline bool cpts_is_rx_enabled(struct cpts *cpts)
  178. {
  179. return false;
  180. }
  181. static inline void cpts_tx_enable(struct cpts *cpts, int enable)
  182. {
  183. }
  184. static inline bool cpts_is_tx_enabled(struct cpts *cpts)
  185. {
  186. return false;
  187. }
  188. static inline bool cpts_can_timestamp(struct cpts *cpts, struct sk_buff *skb)
  189. {
  190. return false;
  191. }
  192. #endif
  193. #endif