1 // SPDX-License-Identifier: GPL-2.0-only
3 * 1588 PTP support for Cadence GEM device.
5 * Copyright (C) 2017 Cadence Design Systems - https://www.cadence.com
7 * Authors: Rafal Ozieblo <rafalo@cadence.com>
8 * Bartosz Folta <bfolta@cadence.com>
10 #include <linux/kernel.h>
11 #include <linux/types.h>
12 #include <linux/clk.h>
13 #include <linux/device.h>
14 #include <linux/etherdevice.h>
15 #include <linux/platform_device.h>
16 #include <linux/time64.h>
17 #include <linux/ptp_classify.h>
18 #include <linux/if_ether.h>
19 #include <linux/if_vlan.h>
20 #include <linux/net_tstamp.h>
21 #include <linux/circ_buf.h>
22 #include <linux/spinlock.h>
26 #define GEM_PTP_TIMER_NAME "gem-ptp-timer"
28 static struct macb_dma_desc_ptp
*macb_ptp_desc(struct macb
*bp
,
29 struct macb_dma_desc
*desc
)
31 if (bp
->hw_dma_cap
== HW_DMA_CAP_PTP
)
32 return (struct macb_dma_desc_ptp
*)
33 ((u8
*)desc
+ sizeof(struct macb_dma_desc
));
34 if (bp
->hw_dma_cap
== HW_DMA_CAP_64B_PTP
)
35 return (struct macb_dma_desc_ptp
*)
36 ((u8
*)desc
+ sizeof(struct macb_dma_desc
)
37 + sizeof(struct macb_dma_desc_64
));
41 static int gem_tsu_get_time(struct ptp_clock_info
*ptp
, struct timespec64
*ts
,
42 struct ptp_system_timestamp
*sts
)
44 struct macb
*bp
= container_of(ptp
, struct macb
, ptp_clock_info
);
49 spin_lock_irqsave(&bp
->tsu_clk_lock
, flags
);
50 ptp_read_system_prets(sts
);
51 first
= gem_readl(bp
, TN
);
52 ptp_read_system_postts(sts
);
53 secl
= gem_readl(bp
, TSL
);
54 sech
= gem_readl(bp
, TSH
);
55 second
= gem_readl(bp
, TN
);
57 /* test for nsec rollover */
59 /* if so, use later read & re-read seconds
60 * (assume all done within 1s)
62 ptp_read_system_prets(sts
);
63 ts
->tv_nsec
= gem_readl(bp
, TN
);
64 ptp_read_system_postts(sts
);
65 secl
= gem_readl(bp
, TSL
);
66 sech
= gem_readl(bp
, TSH
);
71 spin_unlock_irqrestore(&bp
->tsu_clk_lock
, flags
);
72 ts
->tv_sec
= (((u64
)sech
<< GEM_TSL_SIZE
) | secl
)
77 static int gem_tsu_set_time(struct ptp_clock_info
*ptp
,
78 const struct timespec64
*ts
)
80 struct macb
*bp
= container_of(ptp
, struct macb
, ptp_clock_info
);
84 secl
= (u32
)ts
->tv_sec
;
85 sech
= (ts
->tv_sec
>> GEM_TSL_SIZE
) & ((1 << GEM_TSH_SIZE
) - 1);
88 spin_lock_irqsave(&bp
->tsu_clk_lock
, flags
);
90 /* TSH doesn't latch the time and no atomicity! */
91 gem_writel(bp
, TN
, 0); /* clear to avoid overflow */
92 gem_writel(bp
, TSH
, sech
);
93 /* write lower bits 2nd, for synchronized secs update */
94 gem_writel(bp
, TSL
, secl
);
95 gem_writel(bp
, TN
, ns
);
97 spin_unlock_irqrestore(&bp
->tsu_clk_lock
, flags
);
102 static int gem_tsu_incr_set(struct macb
*bp
, struct tsu_incr
*incr_spec
)
106 /* tsu_timer_incr register must be written after
107 * the tsu_timer_incr_sub_ns register and the write operation
108 * will cause the value written to the tsu_timer_incr_sub_ns register
111 spin_lock_irqsave(&bp
->tsu_clk_lock
, flags
);
112 /* RegBit[15:0] = Subns[23:8]; RegBit[31:24] = Subns[7:0] */
113 gem_writel(bp
, TISUBN
, GEM_BF(SUBNSINCRL
, incr_spec
->sub_ns
) |
114 GEM_BF(SUBNSINCRH
, (incr_spec
->sub_ns
>>
115 GEM_SUBNSINCRL_SIZE
)));
116 gem_writel(bp
, TI
, GEM_BF(NSINCR
, incr_spec
->ns
));
117 spin_unlock_irqrestore(&bp
->tsu_clk_lock
, flags
);
122 static int gem_ptp_adjfine(struct ptp_clock_info
*ptp
, long scaled_ppm
)
124 struct macb
*bp
= container_of(ptp
, struct macb
, ptp_clock_info
);
125 struct tsu_incr incr_spec
;
126 bool neg_adj
= false;
130 if (scaled_ppm
< 0) {
132 scaled_ppm
= -scaled_ppm
;
135 /* Adjustment is relative to base frequency */
136 incr_spec
.sub_ns
= bp
->tsu_incr
.sub_ns
;
137 incr_spec
.ns
= bp
->tsu_incr
.ns
;
139 /* scaling: unused(8bit) | ns(8bit) | fractions(16bit) */
140 word
= ((u64
)incr_spec
.ns
<< GEM_SUBNSINCR_SIZE
) + incr_spec
.sub_ns
;
141 adj
= (u64
)scaled_ppm
* word
;
142 /* Divide with rounding, equivalent to floating dividing:
143 * (temp / USEC_PER_SEC) + 0.5
145 adj
+= (USEC_PER_SEC
>> 1);
146 adj
>>= PPM_FRACTION
; /* remove fractions */
147 adj
= div_u64(adj
, USEC_PER_SEC
);
148 adj
= neg_adj
? (word
- adj
) : (word
+ adj
);
150 incr_spec
.ns
= (adj
>> GEM_SUBNSINCR_SIZE
)
151 & ((1 << GEM_NSINCR_SIZE
) - 1);
152 incr_spec
.sub_ns
= adj
& ((1 << GEM_SUBNSINCR_SIZE
) - 1);
153 gem_tsu_incr_set(bp
, &incr_spec
);
157 static int gem_ptp_adjtime(struct ptp_clock_info
*ptp
, s64 delta
)
159 struct macb
*bp
= container_of(ptp
, struct macb
, ptp_clock_info
);
160 struct timespec64 now
, then
= ns_to_timespec64(delta
);
168 if (delta
> TSU_NSEC_MAX_VAL
) {
169 gem_tsu_get_time(&bp
->ptp_clock_info
, &now
, NULL
);
170 now
= timespec64_add(now
, then
);
172 gem_tsu_set_time(&bp
->ptp_clock_info
,
173 (const struct timespec64
*)&now
);
175 adj
= (sign
<< GEM_ADDSUB_OFFSET
) | delta
;
177 gem_writel(bp
, TA
, adj
);
183 static int gem_ptp_enable(struct ptp_clock_info
*ptp
,
184 struct ptp_clock_request
*rq
, int on
)
189 static const struct ptp_clock_info gem_ptp_caps_template
= {
190 .owner
= THIS_MODULE
,
191 .name
= GEM_PTP_TIMER_NAME
,
198 .adjfine
= gem_ptp_adjfine
,
199 .adjtime
= gem_ptp_adjtime
,
200 .gettimex64
= gem_tsu_get_time
,
201 .settime64
= gem_tsu_set_time
,
202 .enable
= gem_ptp_enable
,
205 static void gem_ptp_init_timer(struct macb
*bp
)
210 bp
->tsu_incr
.ns
= div_u64_rem(NSEC_PER_SEC
, bp
->tsu_rate
, &rem
);
213 adj
<<= GEM_SUBNSINCR_SIZE
;
214 bp
->tsu_incr
.sub_ns
= div_u64(adj
, bp
->tsu_rate
);
216 bp
->tsu_incr
.sub_ns
= 0;
220 static void gem_ptp_init_tsu(struct macb
*bp
)
222 struct timespec64 ts
;
224 /* 1. get current system time */
225 ts
= ns_to_timespec64(ktime_to_ns(ktime_get_real()));
227 /* 2. set ptp timer */
228 gem_tsu_set_time(&bp
->ptp_clock_info
, &ts
);
230 /* 3. set PTP timer increment value to BASE_INCREMENT */
231 gem_tsu_incr_set(bp
, &bp
->tsu_incr
);
233 gem_writel(bp
, TA
, 0);
236 static void gem_ptp_clear_timer(struct macb
*bp
)
238 bp
->tsu_incr
.sub_ns
= 0;
241 gem_writel(bp
, TISUBN
, GEM_BF(SUBNSINCR
, 0));
242 gem_writel(bp
, TI
, GEM_BF(NSINCR
, 0));
243 gem_writel(bp
, TA
, 0);
246 static int gem_hw_timestamp(struct macb
*bp
, u32 dma_desc_ts_1
,
247 u32 dma_desc_ts_2
, struct timespec64
*ts
)
249 struct timespec64 tsu
;
251 ts
->tv_sec
= (GEM_BFEXT(DMA_SECH
, dma_desc_ts_2
) << GEM_DMA_SECL_SIZE
) |
252 GEM_BFEXT(DMA_SECL
, dma_desc_ts_1
);
253 ts
->tv_nsec
= GEM_BFEXT(DMA_NSEC
, dma_desc_ts_1
);
255 /* TSU overlapping workaround
256 * The timestamp only contains lower few bits of seconds,
257 * so add value from 1588 timer
259 gem_tsu_get_time(&bp
->ptp_clock_info
, &tsu
, NULL
);
261 ts
->tv_sec
|= ((~GEM_DMA_SEC_MASK
) & tsu
.tv_sec
);
263 /* If the top bit is set in the timestamp,
264 * but not in 1588 timer, it has rolled over,
265 * so subtract max size
267 if ((ts
->tv_sec
& (GEM_DMA_SEC_TOP
>> 1)) &&
268 !(tsu
.tv_sec
& (GEM_DMA_SEC_TOP
>> 1)))
269 ts
->tv_sec
-= GEM_DMA_SEC_TOP
;
274 void gem_ptp_rxstamp(struct macb
*bp
, struct sk_buff
*skb
,
275 struct macb_dma_desc
*desc
)
277 struct skb_shared_hwtstamps
*shhwtstamps
= skb_hwtstamps(skb
);
278 struct macb_dma_desc_ptp
*desc_ptp
;
279 struct timespec64 ts
;
281 if (GEM_BFEXT(DMA_RXVALID
, desc
->addr
)) {
282 desc_ptp
= macb_ptp_desc(bp
, desc
);
283 /* Unlikely but check */
285 dev_warn_ratelimited(&bp
->pdev
->dev
,
286 "Timestamp not supported in BD\n");
289 gem_hw_timestamp(bp
, desc_ptp
->ts_1
, desc_ptp
->ts_2
, &ts
);
290 memset(shhwtstamps
, 0, sizeof(struct skb_shared_hwtstamps
));
291 shhwtstamps
->hwtstamp
= ktime_set(ts
.tv_sec
, ts
.tv_nsec
);
295 void gem_ptp_txstamp(struct macb
*bp
, struct sk_buff
*skb
,
296 struct macb_dma_desc
*desc
)
298 struct skb_shared_hwtstamps shhwtstamps
;
299 struct macb_dma_desc_ptp
*desc_ptp
;
300 struct timespec64 ts
;
302 if (!GEM_BFEXT(DMA_TXVALID
, desc
->ctrl
)) {
303 dev_warn_ratelimited(&bp
->pdev
->dev
,
304 "Timestamp not set in TX BD as expected\n");
308 desc_ptp
= macb_ptp_desc(bp
, desc
);
309 /* Unlikely but check */
311 dev_warn_ratelimited(&bp
->pdev
->dev
,
312 "Timestamp not supported in BD\n");
316 /* ensure ts_1/ts_2 is loaded after ctrl (TX_USED check) */
318 gem_hw_timestamp(bp
, desc_ptp
->ts_1
, desc_ptp
->ts_2
, &ts
);
320 memset(&shhwtstamps
, 0, sizeof(shhwtstamps
));
321 shhwtstamps
.hwtstamp
= ktime_set(ts
.tv_sec
, ts
.tv_nsec
);
322 skb_tstamp_tx(skb
, &shhwtstamps
);
325 void gem_ptp_init(struct net_device
*dev
)
327 struct macb
*bp
= netdev_priv(dev
);
329 bp
->ptp_clock_info
= gem_ptp_caps_template
;
331 /* nominal frequency and maximum adjustment in ppb */
332 bp
->tsu_rate
= bp
->ptp_info
->get_tsu_rate(bp
);
333 bp
->ptp_clock_info
.max_adj
= bp
->ptp_info
->get_ptp_max_adj();
334 gem_ptp_init_timer(bp
);
335 bp
->ptp_clock
= ptp_clock_register(&bp
->ptp_clock_info
, &dev
->dev
);
336 if (IS_ERR(bp
->ptp_clock
)) {
337 pr_err("ptp clock register failed: %ld\n",
338 PTR_ERR(bp
->ptp_clock
));
339 bp
->ptp_clock
= NULL
;
341 } else if (bp
->ptp_clock
== NULL
) {
342 pr_err("ptp clock register failed\n");
346 spin_lock_init(&bp
->tsu_clk_lock
);
348 gem_ptp_init_tsu(bp
);
350 dev_info(&bp
->pdev
->dev
, "%s ptp clock registered.\n",
354 void gem_ptp_remove(struct net_device
*ndev
)
356 struct macb
*bp
= netdev_priv(ndev
);
359 ptp_clock_unregister(bp
->ptp_clock
);
361 gem_ptp_clear_timer(bp
);
363 dev_info(&bp
->pdev
->dev
, "%s ptp clock unregistered.\n",
367 static int gem_ptp_set_ts_mode(struct macb
*bp
,
368 enum macb_bd_control tx_bd_control
,
369 enum macb_bd_control rx_bd_control
)
371 gem_writel(bp
, TXBDCTRL
, GEM_BF(TXTSMODE
, tx_bd_control
));
372 gem_writel(bp
, RXBDCTRL
, GEM_BF(RXTSMODE
, rx_bd_control
));
377 int gem_get_hwtst(struct net_device
*dev
,
378 struct kernel_hwtstamp_config
*tstamp_config
)
380 struct macb
*bp
= netdev_priv(dev
);
382 *tstamp_config
= bp
->tstamp_config
;
383 if ((bp
->hw_dma_cap
& HW_DMA_CAP_PTP
) == 0)
389 static void gem_ptp_set_one_step_sync(struct macb
*bp
, u8 enable
)
393 reg_val
= macb_readl(bp
, NCR
);
396 macb_writel(bp
, NCR
, reg_val
| MACB_BIT(OSSMODE
));
398 macb_writel(bp
, NCR
, reg_val
& ~MACB_BIT(OSSMODE
));
401 int gem_set_hwtst(struct net_device
*dev
,
402 struct kernel_hwtstamp_config
*tstamp_config
,
403 struct netlink_ext_ack
*extack
)
405 enum macb_bd_control tx_bd_control
= TSTAMP_DISABLED
;
406 enum macb_bd_control rx_bd_control
= TSTAMP_DISABLED
;
407 struct macb
*bp
= netdev_priv(dev
);
410 if ((bp
->hw_dma_cap
& HW_DMA_CAP_PTP
) == 0)
413 switch (tstamp_config
->tx_type
) {
414 case HWTSTAMP_TX_OFF
:
416 case HWTSTAMP_TX_ONESTEP_SYNC
:
417 gem_ptp_set_one_step_sync(bp
, 1);
418 tx_bd_control
= TSTAMP_ALL_FRAMES
;
421 gem_ptp_set_one_step_sync(bp
, 0);
422 tx_bd_control
= TSTAMP_ALL_FRAMES
;
428 switch (tstamp_config
->rx_filter
) {
429 case HWTSTAMP_FILTER_NONE
:
431 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC
:
433 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ
:
435 case HWTSTAMP_FILTER_PTP_V2_EVENT
:
436 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT
:
437 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT
:
438 case HWTSTAMP_FILTER_PTP_V2_SYNC
:
439 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC
:
440 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC
:
441 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ
:
442 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ
:
443 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ
:
444 rx_bd_control
= TSTAMP_ALL_PTP_FRAMES
;
445 tstamp_config
->rx_filter
= HWTSTAMP_FILTER_PTP_V2_EVENT
;
446 regval
= macb_readl(bp
, NCR
);
447 macb_writel(bp
, NCR
, (regval
| MACB_BIT(SRTSM
)));
449 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT
:
450 case HWTSTAMP_FILTER_ALL
:
451 rx_bd_control
= TSTAMP_ALL_FRAMES
;
452 tstamp_config
->rx_filter
= HWTSTAMP_FILTER_ALL
;
455 tstamp_config
->rx_filter
= HWTSTAMP_FILTER_NONE
;
459 bp
->tstamp_config
= *tstamp_config
;
461 if (gem_ptp_set_ts_mode(bp
, tx_bd_control
, rx_bd_control
) != 0)