cpts.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /* SPDX-License-Identifier: GPL-2.0+ */
  2. /*
  3. * TI Common Platform Time Sync
  4. *
  5. * Copyright (C) 2012 Richard Cochran <[email protected]>
  6. *
  7. */
  8. #ifndef _TI_CPTS_H_
  9. #define _TI_CPTS_H_
  10. #if IS_ENABLED(CONFIG_TI_CPTS)
  11. #include <linux/clk.h>
  12. #include <linux/clkdev.h>
  13. #include <linux/clocksource.h>
  14. #include <linux/device.h>
  15. #include <linux/list.h>
  16. #include <linux/of.h>
  17. #include <linux/ptp_clock_kernel.h>
  18. #include <linux/skbuff.h>
  19. #include <linux/ptp_classify.h>
  20. #include <linux/timecounter.h>
  21. struct cpsw_cpts {
  22. u32 idver; /* Identification and version */
  23. u32 control; /* Time sync control */
  24. u32 rftclk_sel; /* Reference Clock Select Register */
  25. u32 ts_push; /* Time stamp event push */
  26. u32 ts_load_val; /* Time stamp load value */
  27. u32 ts_load_en; /* Time stamp load enable */
  28. u32 res2[2];
  29. u32 intstat_raw; /* Time sync interrupt status raw */
  30. u32 intstat_masked; /* Time sync interrupt status masked */
  31. u32 int_enable; /* Time sync interrupt enable */
  32. u32 res3;
  33. u32 event_pop; /* Event interrupt pop */
  34. u32 event_low; /* 32 Bit Event Time Stamp */
  35. u32 event_high; /* Event Type Fields */
  36. };
  37. /* Bit definitions for the IDVER register */
  38. #define TX_IDENT_SHIFT (16) /* TX Identification Value */
  39. #define TX_IDENT_MASK (0xffff)
  40. #define RTL_VER_SHIFT (11) /* RTL Version Value */
  41. #define RTL_VER_MASK (0x1f)
  42. #define MAJOR_VER_SHIFT (8) /* Major Version Value */
  43. #define MAJOR_VER_MASK (0x7)
  44. #define MINOR_VER_SHIFT (0) /* Minor Version Value */
  45. #define MINOR_VER_MASK (0xff)
  46. /* Bit definitions for the CONTROL register */
  47. #define HW4_TS_PUSH_EN (1<<11) /* Hardware push 4 enable */
  48. #define HW3_TS_PUSH_EN (1<<10) /* Hardware push 3 enable */
  49. #define HW2_TS_PUSH_EN (1<<9) /* Hardware push 2 enable */
  50. #define HW1_TS_PUSH_EN (1<<8) /* Hardware push 1 enable */
  51. #define INT_TEST (1<<1) /* Interrupt Test */
  52. #define CPTS_EN (1<<0) /* Time Sync Enable */
  53. /*
  54. * Definitions for the single bit resisters:
  55. * TS_PUSH TS_LOAD_EN INTSTAT_RAW INTSTAT_MASKED INT_ENABLE EVENT_POP
  56. */
  57. #define TS_PUSH (1<<0) /* Time stamp event push */
  58. #define TS_LOAD_EN (1<<0) /* Time Stamp Load */
  59. #define TS_PEND_RAW (1<<0) /* int read (before enable) */
  60. #define TS_PEND (1<<0) /* masked interrupt read (after enable) */
  61. #define TS_PEND_EN (1<<0) /* masked interrupt enable */
  62. #define EVENT_POP (1<<0) /* writing discards one event */
  63. /* Bit definitions for the EVENT_HIGH register */
  64. #define PORT_NUMBER_SHIFT (24) /* Indicates Ethernet port or HW pin */
  65. #define PORT_NUMBER_MASK (0x1f)
  66. #define EVENT_TYPE_SHIFT (20) /* Time sync event type */
  67. #define EVENT_TYPE_MASK (0xf)
  68. #define MESSAGE_TYPE_SHIFT (16) /* PTP message type */
  69. #define MESSAGE_TYPE_MASK (0xf)
  70. #define SEQUENCE_ID_SHIFT (0) /* PTP message sequence ID */
  71. #define SEQUENCE_ID_MASK (0xffff)
  72. enum {
  73. CPTS_EV_PUSH, /* Time Stamp Push Event */
  74. CPTS_EV_ROLL, /* Time Stamp Rollover Event */
  75. CPTS_EV_HALF, /* Time Stamp Half Rollover Event */
  76. CPTS_EV_HW, /* Hardware Time Stamp Push Event */
  77. CPTS_EV_RX, /* Ethernet Receive Event */
  78. CPTS_EV_TX, /* Ethernet Transmit Event */
  79. };
  80. #define CPTS_FIFO_DEPTH 16
  81. #define CPTS_MAX_EVENTS 32
  82. struct cpts_event {
  83. struct list_head list;
  84. unsigned long tmo;
  85. u32 high;
  86. u32 low;
  87. u64 timestamp;
  88. };
  89. struct cpts {
  90. struct device *dev;
  91. struct cpsw_cpts __iomem *reg;
  92. int tx_enable;
  93. int rx_enable;
  94. struct ptp_clock_info info;
  95. struct ptp_clock *clock;
  96. spinlock_t lock; /* protects fifo/events */
  97. u32 cc_mult; /* for the nominal frequency */
  98. struct cyclecounter cc;
  99. struct timecounter tc;
  100. int phc_index;
  101. struct clk *refclk;
  102. struct list_head events;
  103. struct list_head pool;
  104. struct cpts_event pool_data[CPTS_MAX_EVENTS];
  105. unsigned long ov_check_period;
  106. struct sk_buff_head txq;
  107. u64 cur_timestamp;
  108. u32 mult_new;
  109. struct mutex ptp_clk_mutex; /* sync PTP interface and worker */
  110. bool irq_poll;
  111. struct completion ts_push_complete;
  112. u32 hw_ts_enable;
  113. };
  114. void cpts_rx_timestamp(struct cpts *cpts, struct sk_buff *skb);
  115. void cpts_tx_timestamp(struct cpts *cpts, struct sk_buff *skb);
  116. int cpts_register(struct cpts *cpts);
  117. void cpts_unregister(struct cpts *cpts);
  118. struct cpts *cpts_create(struct device *dev, void __iomem *regs,
  119. struct device_node *node, u32 n_ext_ts);
  120. void cpts_release(struct cpts *cpts);
  121. void cpts_misc_interrupt(struct cpts *cpts);
  122. static inline bool cpts_can_timestamp(struct cpts *cpts, struct sk_buff *skb)
  123. {
  124. unsigned int class = ptp_classify_raw(skb);
  125. if (class == PTP_CLASS_NONE)
  126. return false;
  127. return true;
  128. }
  129. static inline void cpts_set_irqpoll(struct cpts *cpts, bool en)
  130. {
  131. cpts->irq_poll = en;
  132. }
  133. #else
  134. struct cpts;
  135. static inline void cpts_rx_timestamp(struct cpts *cpts, struct sk_buff *skb)
  136. {
  137. }
  138. static inline void cpts_tx_timestamp(struct cpts *cpts, struct sk_buff *skb)
  139. {
  140. }
  141. static inline
  142. struct cpts *cpts_create(struct device *dev, void __iomem *regs,
  143. struct device_node *node, u32 n_ext_ts)
  144. {
  145. return NULL;
  146. }
  147. static inline void cpts_release(struct cpts *cpts)
  148. {
  149. }
  150. static inline int
  151. cpts_register(struct cpts *cpts)
  152. {
  153. return 0;
  154. }
  155. static inline void cpts_unregister(struct cpts *cpts)
  156. {
  157. }
  158. static inline bool cpts_can_timestamp(struct cpts *cpts, struct sk_buff *skb)
  159. {
  160. return false;
  161. }
  162. static inline void cpts_misc_interrupt(struct cpts *cpts)
  163. {
  164. }
  165. static inline void cpts_set_irqpoll(struct cpts *cpts, bool en)
  166. {
  167. }
  168. #endif
  169. #endif