15f295805SVinicius Costa Gomes // SPDX-License-Identifier: GPL-2.0
25f295805SVinicius Costa Gomes /* Copyright (c) 2019 Intel Corporation */
35f295805SVinicius Costa Gomes
45f295805SVinicius Costa Gomes #include "igc.h"
55f295805SVinicius Costa Gomes
65f295805SVinicius Costa Gomes #include <linux/module.h>
75f295805SVinicius Costa Gomes #include <linux/device.h>
85f295805SVinicius Costa Gomes #include <linux/pci.h>
95f295805SVinicius Costa Gomes #include <linux/ptp_classify.h>
105f295805SVinicius Costa Gomes #include <linux/clocksource.h>
11b03c49cdSVinicius Costa Gomes #include <linux/ktime.h>
12a90ec848SVinicius Costa Gomes #include <linux/delay.h>
13a90ec848SVinicius Costa Gomes #include <linux/iopoll.h>
145f295805SVinicius Costa Gomes
155f295805SVinicius Costa Gomes #define INCVALUE_MASK 0x7fffffff
165f295805SVinicius Costa Gomes #define ISGN 0x80000000
175f295805SVinicius Costa Gomes
185f295805SVinicius Costa Gomes #define IGC_PTP_TX_TIMEOUT (HZ * 15)
195f295805SVinicius Costa Gomes
20a90ec848SVinicius Costa Gomes #define IGC_PTM_STAT_SLEEP 2
21a90ec848SVinicius Costa Gomes #define IGC_PTM_STAT_TIMEOUT 100
22a90ec848SVinicius Costa Gomes
235f295805SVinicius Costa Gomes /* SYSTIM read access for I225 */
igc_ptp_read(struct igc_adapter * adapter,struct timespec64 * ts)24fec49eb4SVinicius Costa Gomes void igc_ptp_read(struct igc_adapter *adapter, struct timespec64 *ts)
255f295805SVinicius Costa Gomes {
265f295805SVinicius Costa Gomes struct igc_hw *hw = &adapter->hw;
275f295805SVinicius Costa Gomes u32 sec, nsec;
285f295805SVinicius Costa Gomes
29e5f020adSVinicius Costa Gomes /* The timestamp is latched when SYSTIML is read. */
305f295805SVinicius Costa Gomes nsec = rd32(IGC_SYSTIML);
315f295805SVinicius Costa Gomes sec = rd32(IGC_SYSTIMH);
325f295805SVinicius Costa Gomes
335f295805SVinicius Costa Gomes ts->tv_sec = sec;
345f295805SVinicius Costa Gomes ts->tv_nsec = nsec;
355f295805SVinicius Costa Gomes }
365f295805SVinicius Costa Gomes
igc_ptp_write_i225(struct igc_adapter * adapter,const struct timespec64 * ts)375f295805SVinicius Costa Gomes static void igc_ptp_write_i225(struct igc_adapter *adapter,
385f295805SVinicius Costa Gomes const struct timespec64 *ts)
395f295805SVinicius Costa Gomes {
405f295805SVinicius Costa Gomes struct igc_hw *hw = &adapter->hw;
415f295805SVinicius Costa Gomes
425f295805SVinicius Costa Gomes wr32(IGC_SYSTIML, ts->tv_nsec);
435f295805SVinicius Costa Gomes wr32(IGC_SYSTIMH, ts->tv_sec);
445f295805SVinicius Costa Gomes }
455f295805SVinicius Costa Gomes
igc_ptp_adjfine_i225(struct ptp_clock_info * ptp,long scaled_ppm)465f295805SVinicius Costa Gomes static int igc_ptp_adjfine_i225(struct ptp_clock_info *ptp, long scaled_ppm)
475f295805SVinicius Costa Gomes {
485f295805SVinicius Costa Gomes struct igc_adapter *igc = container_of(ptp, struct igc_adapter,
495f295805SVinicius Costa Gomes ptp_caps);
505f295805SVinicius Costa Gomes struct igc_hw *hw = &igc->hw;
515f295805SVinicius Costa Gomes int neg_adj = 0;
525f295805SVinicius Costa Gomes u64 rate;
535f295805SVinicius Costa Gomes u32 inca;
545f295805SVinicius Costa Gomes
555f295805SVinicius Costa Gomes if (scaled_ppm < 0) {
565f295805SVinicius Costa Gomes neg_adj = 1;
575f295805SVinicius Costa Gomes scaled_ppm = -scaled_ppm;
585f295805SVinicius Costa Gomes }
595f295805SVinicius Costa Gomes rate = scaled_ppm;
605f295805SVinicius Costa Gomes rate <<= 14;
615f295805SVinicius Costa Gomes rate = div_u64(rate, 78125);
625f295805SVinicius Costa Gomes
635f295805SVinicius Costa Gomes inca = rate & INCVALUE_MASK;
645f295805SVinicius Costa Gomes if (neg_adj)
655f295805SVinicius Costa Gomes inca |= ISGN;
665f295805SVinicius Costa Gomes
675f295805SVinicius Costa Gomes wr32(IGC_TIMINCA, inca);
685f295805SVinicius Costa Gomes
695f295805SVinicius Costa Gomes return 0;
705f295805SVinicius Costa Gomes }
715f295805SVinicius Costa Gomes
igc_ptp_adjtime_i225(struct ptp_clock_info * ptp,s64 delta)725f295805SVinicius Costa Gomes static int igc_ptp_adjtime_i225(struct ptp_clock_info *ptp, s64 delta)
735f295805SVinicius Costa Gomes {
745f295805SVinicius Costa Gomes struct igc_adapter *igc = container_of(ptp, struct igc_adapter,
755f295805SVinicius Costa Gomes ptp_caps);
765f295805SVinicius Costa Gomes struct timespec64 now, then = ns_to_timespec64(delta);
775f295805SVinicius Costa Gomes unsigned long flags;
785f295805SVinicius Costa Gomes
795f295805SVinicius Costa Gomes spin_lock_irqsave(&igc->tmreg_lock, flags);
805f295805SVinicius Costa Gomes
81fec49eb4SVinicius Costa Gomes igc_ptp_read(igc, &now);
825f295805SVinicius Costa Gomes now = timespec64_add(now, then);
835f295805SVinicius Costa Gomes igc_ptp_write_i225(igc, (const struct timespec64 *)&now);
845f295805SVinicius Costa Gomes
855f295805SVinicius Costa Gomes spin_unlock_irqrestore(&igc->tmreg_lock, flags);
865f295805SVinicius Costa Gomes
875f295805SVinicius Costa Gomes return 0;
885f295805SVinicius Costa Gomes }
895f295805SVinicius Costa Gomes
igc_ptp_gettimex64_i225(struct ptp_clock_info * ptp,struct timespec64 * ts,struct ptp_system_timestamp * sts)905f295805SVinicius Costa Gomes static int igc_ptp_gettimex64_i225(struct ptp_clock_info *ptp,
915f295805SVinicius Costa Gomes struct timespec64 *ts,
925f295805SVinicius Costa Gomes struct ptp_system_timestamp *sts)
935f295805SVinicius Costa Gomes {
945f295805SVinicius Costa Gomes struct igc_adapter *igc = container_of(ptp, struct igc_adapter,
955f295805SVinicius Costa Gomes ptp_caps);
965f295805SVinicius Costa Gomes struct igc_hw *hw = &igc->hw;
975f295805SVinicius Costa Gomes unsigned long flags;
985f295805SVinicius Costa Gomes
995f295805SVinicius Costa Gomes spin_lock_irqsave(&igc->tmreg_lock, flags);
1005f295805SVinicius Costa Gomes
1015f295805SVinicius Costa Gomes ptp_read_system_prets(sts);
1025f295805SVinicius Costa Gomes ts->tv_nsec = rd32(IGC_SYSTIML);
1035f295805SVinicius Costa Gomes ts->tv_sec = rd32(IGC_SYSTIMH);
104e5f020adSVinicius Costa Gomes ptp_read_system_postts(sts);
1055f295805SVinicius Costa Gomes
1065f295805SVinicius Costa Gomes spin_unlock_irqrestore(&igc->tmreg_lock, flags);
1075f295805SVinicius Costa Gomes
1085f295805SVinicius Costa Gomes return 0;
1095f295805SVinicius Costa Gomes }
1105f295805SVinicius Costa Gomes
igc_ptp_settime_i225(struct ptp_clock_info * ptp,const struct timespec64 * ts)1115f295805SVinicius Costa Gomes static int igc_ptp_settime_i225(struct ptp_clock_info *ptp,
1125f295805SVinicius Costa Gomes const struct timespec64 *ts)
1135f295805SVinicius Costa Gomes {
1145f295805SVinicius Costa Gomes struct igc_adapter *igc = container_of(ptp, struct igc_adapter,
1155f295805SVinicius Costa Gomes ptp_caps);
1165f295805SVinicius Costa Gomes unsigned long flags;
1175f295805SVinicius Costa Gomes
1185f295805SVinicius Costa Gomes spin_lock_irqsave(&igc->tmreg_lock, flags);
1195f295805SVinicius Costa Gomes
1205f295805SVinicius Costa Gomes igc_ptp_write_i225(igc, ts);
1215f295805SVinicius Costa Gomes
1225f295805SVinicius Costa Gomes spin_unlock_irqrestore(&igc->tmreg_lock, flags);
1235f295805SVinicius Costa Gomes
1245f295805SVinicius Costa Gomes return 0;
1255f295805SVinicius Costa Gomes }
1265f295805SVinicius Costa Gomes
igc_pin_direction(int pin,int input,u32 * ctrl,u32 * ctrl_ext)12787938851SEderson de Souza static void igc_pin_direction(int pin, int input, u32 *ctrl, u32 *ctrl_ext)
12887938851SEderson de Souza {
12987938851SEderson de Souza u32 *ptr = pin < 2 ? ctrl : ctrl_ext;
13087938851SEderson de Souza static const u32 mask[IGC_N_SDP] = {
13187938851SEderson de Souza IGC_CTRL_SDP0_DIR,
13287938851SEderson de Souza IGC_CTRL_SDP1_DIR,
13387938851SEderson de Souza IGC_CTRL_EXT_SDP2_DIR,
13487938851SEderson de Souza IGC_CTRL_EXT_SDP3_DIR,
13587938851SEderson de Souza };
13687938851SEderson de Souza
13787938851SEderson de Souza if (input)
13887938851SEderson de Souza *ptr &= ~mask[pin];
13987938851SEderson de Souza else
14087938851SEderson de Souza *ptr |= mask[pin];
14187938851SEderson de Souza }
14287938851SEderson de Souza
igc_pin_perout(struct igc_adapter * igc,int chan,int pin,int freq)14387938851SEderson de Souza static void igc_pin_perout(struct igc_adapter *igc, int chan, int pin, int freq)
14487938851SEderson de Souza {
14587938851SEderson de Souza static const u32 igc_aux0_sel_sdp[IGC_N_SDP] = {
14687938851SEderson de Souza IGC_AUX0_SEL_SDP0, IGC_AUX0_SEL_SDP1, IGC_AUX0_SEL_SDP2, IGC_AUX0_SEL_SDP3,
14787938851SEderson de Souza };
14887938851SEderson de Souza static const u32 igc_aux1_sel_sdp[IGC_N_SDP] = {
14987938851SEderson de Souza IGC_AUX1_SEL_SDP0, IGC_AUX1_SEL_SDP1, IGC_AUX1_SEL_SDP2, IGC_AUX1_SEL_SDP3,
15087938851SEderson de Souza };
15187938851SEderson de Souza static const u32 igc_ts_sdp_en[IGC_N_SDP] = {
15287938851SEderson de Souza IGC_TS_SDP0_EN, IGC_TS_SDP1_EN, IGC_TS_SDP2_EN, IGC_TS_SDP3_EN,
15387938851SEderson de Souza };
15487938851SEderson de Souza static const u32 igc_ts_sdp_sel_tt0[IGC_N_SDP] = {
15587938851SEderson de Souza IGC_TS_SDP0_SEL_TT0, IGC_TS_SDP1_SEL_TT0,
15687938851SEderson de Souza IGC_TS_SDP2_SEL_TT0, IGC_TS_SDP3_SEL_TT0,
15787938851SEderson de Souza };
15887938851SEderson de Souza static const u32 igc_ts_sdp_sel_tt1[IGC_N_SDP] = {
15987938851SEderson de Souza IGC_TS_SDP0_SEL_TT1, IGC_TS_SDP1_SEL_TT1,
16087938851SEderson de Souza IGC_TS_SDP2_SEL_TT1, IGC_TS_SDP3_SEL_TT1,
16187938851SEderson de Souza };
16287938851SEderson de Souza static const u32 igc_ts_sdp_sel_fc0[IGC_N_SDP] = {
16387938851SEderson de Souza IGC_TS_SDP0_SEL_FC0, IGC_TS_SDP1_SEL_FC0,
16487938851SEderson de Souza IGC_TS_SDP2_SEL_FC0, IGC_TS_SDP3_SEL_FC0,
16587938851SEderson de Souza };
16687938851SEderson de Souza static const u32 igc_ts_sdp_sel_fc1[IGC_N_SDP] = {
16787938851SEderson de Souza IGC_TS_SDP0_SEL_FC1, IGC_TS_SDP1_SEL_FC1,
16887938851SEderson de Souza IGC_TS_SDP2_SEL_FC1, IGC_TS_SDP3_SEL_FC1,
16987938851SEderson de Souza };
17087938851SEderson de Souza static const u32 igc_ts_sdp_sel_clr[IGC_N_SDP] = {
17187938851SEderson de Souza IGC_TS_SDP0_SEL_FC1, IGC_TS_SDP1_SEL_FC1,
17287938851SEderson de Souza IGC_TS_SDP2_SEL_FC1, IGC_TS_SDP3_SEL_FC1,
17387938851SEderson de Souza };
17487938851SEderson de Souza struct igc_hw *hw = &igc->hw;
17587938851SEderson de Souza u32 ctrl, ctrl_ext, tssdp = 0;
17687938851SEderson de Souza
17787938851SEderson de Souza ctrl = rd32(IGC_CTRL);
17887938851SEderson de Souza ctrl_ext = rd32(IGC_CTRL_EXT);
17987938851SEderson de Souza tssdp = rd32(IGC_TSSDP);
18087938851SEderson de Souza
18187938851SEderson de Souza igc_pin_direction(pin, 0, &ctrl, &ctrl_ext);
18287938851SEderson de Souza
18387938851SEderson de Souza /* Make sure this pin is not enabled as an input. */
18487938851SEderson de Souza if ((tssdp & IGC_AUX0_SEL_SDP3) == igc_aux0_sel_sdp[pin])
18587938851SEderson de Souza tssdp &= ~IGC_AUX0_TS_SDP_EN;
18687938851SEderson de Souza
18787938851SEderson de Souza if ((tssdp & IGC_AUX1_SEL_SDP3) == igc_aux1_sel_sdp[pin])
18887938851SEderson de Souza tssdp &= ~IGC_AUX1_TS_SDP_EN;
18987938851SEderson de Souza
19087938851SEderson de Souza tssdp &= ~igc_ts_sdp_sel_clr[pin];
19187938851SEderson de Souza if (freq) {
19287938851SEderson de Souza if (chan == 1)
19387938851SEderson de Souza tssdp |= igc_ts_sdp_sel_fc1[pin];
19487938851SEderson de Souza else
19587938851SEderson de Souza tssdp |= igc_ts_sdp_sel_fc0[pin];
19687938851SEderson de Souza } else {
19787938851SEderson de Souza if (chan == 1)
19887938851SEderson de Souza tssdp |= igc_ts_sdp_sel_tt1[pin];
19987938851SEderson de Souza else
20087938851SEderson de Souza tssdp |= igc_ts_sdp_sel_tt0[pin];
20187938851SEderson de Souza }
20287938851SEderson de Souza tssdp |= igc_ts_sdp_en[pin];
20387938851SEderson de Souza
20487938851SEderson de Souza wr32(IGC_TSSDP, tssdp);
20587938851SEderson de Souza wr32(IGC_CTRL, ctrl);
20687938851SEderson de Souza wr32(IGC_CTRL_EXT, ctrl_ext);
20787938851SEderson de Souza }
20887938851SEderson de Souza
igc_pin_extts(struct igc_adapter * igc,int chan,int pin)20987938851SEderson de Souza static void igc_pin_extts(struct igc_adapter *igc, int chan, int pin)
21087938851SEderson de Souza {
21187938851SEderson de Souza static const u32 igc_aux0_sel_sdp[IGC_N_SDP] = {
21287938851SEderson de Souza IGC_AUX0_SEL_SDP0, IGC_AUX0_SEL_SDP1, IGC_AUX0_SEL_SDP2, IGC_AUX0_SEL_SDP3,
21387938851SEderson de Souza };
21487938851SEderson de Souza static const u32 igc_aux1_sel_sdp[IGC_N_SDP] = {
21587938851SEderson de Souza IGC_AUX1_SEL_SDP0, IGC_AUX1_SEL_SDP1, IGC_AUX1_SEL_SDP2, IGC_AUX1_SEL_SDP3,
21687938851SEderson de Souza };
21787938851SEderson de Souza static const u32 igc_ts_sdp_en[IGC_N_SDP] = {
21887938851SEderson de Souza IGC_TS_SDP0_EN, IGC_TS_SDP1_EN, IGC_TS_SDP2_EN, IGC_TS_SDP3_EN,
21987938851SEderson de Souza };
22087938851SEderson de Souza struct igc_hw *hw = &igc->hw;
22187938851SEderson de Souza u32 ctrl, ctrl_ext, tssdp = 0;
22287938851SEderson de Souza
22387938851SEderson de Souza ctrl = rd32(IGC_CTRL);
22487938851SEderson de Souza ctrl_ext = rd32(IGC_CTRL_EXT);
22587938851SEderson de Souza tssdp = rd32(IGC_TSSDP);
22687938851SEderson de Souza
22787938851SEderson de Souza igc_pin_direction(pin, 1, &ctrl, &ctrl_ext);
22887938851SEderson de Souza
22987938851SEderson de Souza /* Make sure this pin is not enabled as an output. */
23087938851SEderson de Souza tssdp &= ~igc_ts_sdp_en[pin];
23187938851SEderson de Souza
23287938851SEderson de Souza if (chan == 1) {
23387938851SEderson de Souza tssdp &= ~IGC_AUX1_SEL_SDP3;
23487938851SEderson de Souza tssdp |= igc_aux1_sel_sdp[pin] | IGC_AUX1_TS_SDP_EN;
23587938851SEderson de Souza } else {
23687938851SEderson de Souza tssdp &= ~IGC_AUX0_SEL_SDP3;
23787938851SEderson de Souza tssdp |= igc_aux0_sel_sdp[pin] | IGC_AUX0_TS_SDP_EN;
23887938851SEderson de Souza }
23987938851SEderson de Souza
24087938851SEderson de Souza wr32(IGC_TSSDP, tssdp);
24187938851SEderson de Souza wr32(IGC_CTRL, ctrl);
24287938851SEderson de Souza wr32(IGC_CTRL_EXT, ctrl_ext);
24387938851SEderson de Souza }
24487938851SEderson de Souza
igc_ptp_feature_enable_i225(struct ptp_clock_info * ptp,struct ptp_clock_request * rq,int on)2455f295805SVinicius Costa Gomes static int igc_ptp_feature_enable_i225(struct ptp_clock_info *ptp,
2465f295805SVinicius Costa Gomes struct ptp_clock_request *rq, int on)
2475f295805SVinicius Costa Gomes {
24864433e5bSEderson de Souza struct igc_adapter *igc =
24964433e5bSEderson de Souza container_of(ptp, struct igc_adapter, ptp_caps);
25064433e5bSEderson de Souza struct igc_hw *hw = &igc->hw;
25164433e5bSEderson de Souza unsigned long flags;
25287938851SEderson de Souza struct timespec64 ts;
25387938851SEderson de Souza int use_freq = 0, pin = -1;
25487938851SEderson de Souza u32 tsim, tsauxc, tsauxc_mask, tsim_mask, trgttiml, trgttimh, freqout;
25587938851SEderson de Souza s64 ns;
25664433e5bSEderson de Souza
25764433e5bSEderson de Souza switch (rq->type) {
25887938851SEderson de Souza case PTP_CLK_REQ_EXTTS:
25987938851SEderson de Souza /* Reject requests with unsupported flags */
26087938851SEderson de Souza if (rq->extts.flags & ~(PTP_ENABLE_FEATURE |
26187938851SEderson de Souza PTP_RISING_EDGE |
26287938851SEderson de Souza PTP_FALLING_EDGE |
26387938851SEderson de Souza PTP_STRICT_FLAGS))
26487938851SEderson de Souza return -EOPNOTSUPP;
26587938851SEderson de Souza
26687938851SEderson de Souza /* Reject requests failing to enable both edges. */
26787938851SEderson de Souza if ((rq->extts.flags & PTP_STRICT_FLAGS) &&
26887938851SEderson de Souza (rq->extts.flags & PTP_ENABLE_FEATURE) &&
26987938851SEderson de Souza (rq->extts.flags & PTP_EXTTS_EDGES) != PTP_EXTTS_EDGES)
27087938851SEderson de Souza return -EOPNOTSUPP;
27187938851SEderson de Souza
27287938851SEderson de Souza if (on) {
27387938851SEderson de Souza pin = ptp_find_pin(igc->ptp_clock, PTP_PF_EXTTS,
27487938851SEderson de Souza rq->extts.index);
27587938851SEderson de Souza if (pin < 0)
27687938851SEderson de Souza return -EBUSY;
27787938851SEderson de Souza }
27887938851SEderson de Souza if (rq->extts.index == 1) {
27987938851SEderson de Souza tsauxc_mask = IGC_TSAUXC_EN_TS1;
28087938851SEderson de Souza tsim_mask = IGC_TSICR_AUTT1;
28187938851SEderson de Souza } else {
28287938851SEderson de Souza tsauxc_mask = IGC_TSAUXC_EN_TS0;
28387938851SEderson de Souza tsim_mask = IGC_TSICR_AUTT0;
28487938851SEderson de Souza }
28587938851SEderson de Souza spin_lock_irqsave(&igc->tmreg_lock, flags);
28687938851SEderson de Souza tsauxc = rd32(IGC_TSAUXC);
28787938851SEderson de Souza tsim = rd32(IGC_TSIM);
28887938851SEderson de Souza if (on) {
28987938851SEderson de Souza igc_pin_extts(igc, rq->extts.index, pin);
29087938851SEderson de Souza tsauxc |= tsauxc_mask;
29187938851SEderson de Souza tsim |= tsim_mask;
29287938851SEderson de Souza } else {
29387938851SEderson de Souza tsauxc &= ~tsauxc_mask;
29487938851SEderson de Souza tsim &= ~tsim_mask;
29587938851SEderson de Souza }
29687938851SEderson de Souza wr32(IGC_TSAUXC, tsauxc);
29787938851SEderson de Souza wr32(IGC_TSIM, tsim);
29887938851SEderson de Souza spin_unlock_irqrestore(&igc->tmreg_lock, flags);
29987938851SEderson de Souza return 0;
30087938851SEderson de Souza
30187938851SEderson de Souza case PTP_CLK_REQ_PEROUT:
30287938851SEderson de Souza /* Reject requests with unsupported flags */
30387938851SEderson de Souza if (rq->perout.flags)
30487938851SEderson de Souza return -EOPNOTSUPP;
30587938851SEderson de Souza
30687938851SEderson de Souza if (on) {
30787938851SEderson de Souza pin = ptp_find_pin(igc->ptp_clock, PTP_PF_PEROUT,
30887938851SEderson de Souza rq->perout.index);
30987938851SEderson de Souza if (pin < 0)
31087938851SEderson de Souza return -EBUSY;
31187938851SEderson de Souza }
31287938851SEderson de Souza ts.tv_sec = rq->perout.period.sec;
31387938851SEderson de Souza ts.tv_nsec = rq->perout.period.nsec;
31487938851SEderson de Souza ns = timespec64_to_ns(&ts);
31587938851SEderson de Souza ns = ns >> 1;
31687938851SEderson de Souza if (on && (ns <= 70000000LL || ns == 125000000LL ||
31787938851SEderson de Souza ns == 250000000LL || ns == 500000000LL)) {
31887938851SEderson de Souza if (ns < 8LL)
31987938851SEderson de Souza return -EINVAL;
32087938851SEderson de Souza use_freq = 1;
32187938851SEderson de Souza }
32287938851SEderson de Souza ts = ns_to_timespec64(ns);
32387938851SEderson de Souza if (rq->perout.index == 1) {
32487938851SEderson de Souza if (use_freq) {
3255e91c72eSChristopher S Hall tsauxc_mask = IGC_TSAUXC_EN_CLK1 | IGC_TSAUXC_ST1;
32687938851SEderson de Souza tsim_mask = 0;
32787938851SEderson de Souza } else {
32887938851SEderson de Souza tsauxc_mask = IGC_TSAUXC_EN_TT1;
32987938851SEderson de Souza tsim_mask = IGC_TSICR_TT1;
33087938851SEderson de Souza }
33187938851SEderson de Souza trgttiml = IGC_TRGTTIML1;
33287938851SEderson de Souza trgttimh = IGC_TRGTTIMH1;
33387938851SEderson de Souza freqout = IGC_FREQOUT1;
33487938851SEderson de Souza } else {
33587938851SEderson de Souza if (use_freq) {
3365e91c72eSChristopher S Hall tsauxc_mask = IGC_TSAUXC_EN_CLK0 | IGC_TSAUXC_ST0;
33787938851SEderson de Souza tsim_mask = 0;
33887938851SEderson de Souza } else {
33987938851SEderson de Souza tsauxc_mask = IGC_TSAUXC_EN_TT0;
34087938851SEderson de Souza tsim_mask = IGC_TSICR_TT0;
34187938851SEderson de Souza }
34287938851SEderson de Souza trgttiml = IGC_TRGTTIML0;
34387938851SEderson de Souza trgttimh = IGC_TRGTTIMH0;
34487938851SEderson de Souza freqout = IGC_FREQOUT0;
34587938851SEderson de Souza }
34687938851SEderson de Souza spin_lock_irqsave(&igc->tmreg_lock, flags);
34787938851SEderson de Souza tsauxc = rd32(IGC_TSAUXC);
34887938851SEderson de Souza tsim = rd32(IGC_TSIM);
34987938851SEderson de Souza if (rq->perout.index == 1) {
3505e91c72eSChristopher S Hall tsauxc &= ~(IGC_TSAUXC_EN_TT1 | IGC_TSAUXC_EN_CLK1 |
3515e91c72eSChristopher S Hall IGC_TSAUXC_ST1);
35287938851SEderson de Souza tsim &= ~IGC_TSICR_TT1;
35387938851SEderson de Souza } else {
3545e91c72eSChristopher S Hall tsauxc &= ~(IGC_TSAUXC_EN_TT0 | IGC_TSAUXC_EN_CLK0 |
3555e91c72eSChristopher S Hall IGC_TSAUXC_ST0);
35687938851SEderson de Souza tsim &= ~IGC_TSICR_TT0;
35787938851SEderson de Souza }
35887938851SEderson de Souza if (on) {
35984a192e4SAravindhan Gunasekaran struct timespec64 safe_start;
36087938851SEderson de Souza int i = rq->perout.index;
36187938851SEderson de Souza
36287938851SEderson de Souza igc_pin_perout(igc, i, pin, use_freq);
36384a192e4SAravindhan Gunasekaran igc_ptp_read(igc, &safe_start);
36484a192e4SAravindhan Gunasekaran
36584a192e4SAravindhan Gunasekaran /* PPS output start time is triggered by Target time(TT)
36684a192e4SAravindhan Gunasekaran * register. Programming any past time value into TT
36784a192e4SAravindhan Gunasekaran * register will cause PPS to never start. Need to make
36884a192e4SAravindhan Gunasekaran * sure we program the TT register a time ahead in
36984a192e4SAravindhan Gunasekaran * future. There isn't a stringent need to fire PPS out
37084a192e4SAravindhan Gunasekaran * right away. Adding +2 seconds should take care of
37184a192e4SAravindhan Gunasekaran * corner cases. Let's say if the SYSTIML is close to
37284a192e4SAravindhan Gunasekaran * wrap up and the timer keeps ticking as we program the
37384a192e4SAravindhan Gunasekaran * register, adding +2seconds is safe bet.
37484a192e4SAravindhan Gunasekaran */
37584a192e4SAravindhan Gunasekaran safe_start.tv_sec += 2;
37684a192e4SAravindhan Gunasekaran
37784a192e4SAravindhan Gunasekaran if (rq->perout.start.sec < safe_start.tv_sec)
37884a192e4SAravindhan Gunasekaran igc->perout[i].start.tv_sec = safe_start.tv_sec;
37984a192e4SAravindhan Gunasekaran else
38087938851SEderson de Souza igc->perout[i].start.tv_sec = rq->perout.start.sec;
38187938851SEderson de Souza igc->perout[i].start.tv_nsec = rq->perout.start.nsec;
38287938851SEderson de Souza igc->perout[i].period.tv_sec = ts.tv_sec;
38387938851SEderson de Souza igc->perout[i].period.tv_nsec = ts.tv_nsec;
38484a192e4SAravindhan Gunasekaran wr32(trgttimh, (u32)igc->perout[i].start.tv_sec);
38587938851SEderson de Souza /* For now, always select timer 0 as source. */
38684a192e4SAravindhan Gunasekaran wr32(trgttiml, (u32)(igc->perout[i].start.tv_nsec |
38784a192e4SAravindhan Gunasekaran IGC_TT_IO_TIMER_SEL_SYSTIM0));
38887938851SEderson de Souza if (use_freq)
38987938851SEderson de Souza wr32(freqout, ns);
39087938851SEderson de Souza tsauxc |= tsauxc_mask;
39187938851SEderson de Souza tsim |= tsim_mask;
39287938851SEderson de Souza }
39387938851SEderson de Souza wr32(IGC_TSAUXC, tsauxc);
39487938851SEderson de Souza wr32(IGC_TSIM, tsim);
39587938851SEderson de Souza spin_unlock_irqrestore(&igc->tmreg_lock, flags);
39687938851SEderson de Souza return 0;
39787938851SEderson de Souza
39864433e5bSEderson de Souza case PTP_CLK_REQ_PPS:
39964433e5bSEderson de Souza spin_lock_irqsave(&igc->tmreg_lock, flags);
40064433e5bSEderson de Souza tsim = rd32(IGC_TSIM);
40164433e5bSEderson de Souza if (on)
40264433e5bSEderson de Souza tsim |= IGC_TSICR_SYS_WRAP;
40364433e5bSEderson de Souza else
40464433e5bSEderson de Souza tsim &= ~IGC_TSICR_SYS_WRAP;
40564433e5bSEderson de Souza igc->pps_sys_wrap_on = on;
40664433e5bSEderson de Souza wr32(IGC_TSIM, tsim);
40764433e5bSEderson de Souza spin_unlock_irqrestore(&igc->tmreg_lock, flags);
40864433e5bSEderson de Souza return 0;
40964433e5bSEderson de Souza
41064433e5bSEderson de Souza default:
41164433e5bSEderson de Souza break;
41264433e5bSEderson de Souza }
41364433e5bSEderson de Souza
4145f295805SVinicius Costa Gomes return -EOPNOTSUPP;
4155f295805SVinicius Costa Gomes }
4165f295805SVinicius Costa Gomes
igc_ptp_verify_pin(struct ptp_clock_info * ptp,unsigned int pin,enum ptp_pin_function func,unsigned int chan)41787938851SEderson de Souza static int igc_ptp_verify_pin(struct ptp_clock_info *ptp, unsigned int pin,
41887938851SEderson de Souza enum ptp_pin_function func, unsigned int chan)
41987938851SEderson de Souza {
42087938851SEderson de Souza switch (func) {
42187938851SEderson de Souza case PTP_PF_NONE:
42287938851SEderson de Souza case PTP_PF_EXTTS:
42387938851SEderson de Souza case PTP_PF_PEROUT:
42487938851SEderson de Souza break;
42587938851SEderson de Souza case PTP_PF_PHYSYNC:
42687938851SEderson de Souza return -1;
42787938851SEderson de Souza }
42887938851SEderson de Souza return 0;
42987938851SEderson de Souza }
43087938851SEderson de Souza
43181b05520SVinicius Costa Gomes /**
43281b05520SVinicius Costa Gomes * igc_ptp_systim_to_hwtstamp - convert system time value to HW timestamp
43381b05520SVinicius Costa Gomes * @adapter: board private structure
43481b05520SVinicius Costa Gomes * @hwtstamps: timestamp structure to update
43581b05520SVinicius Costa Gomes * @systim: unsigned 64bit system time value
43681b05520SVinicius Costa Gomes *
43781b05520SVinicius Costa Gomes * We need to convert the system time value stored in the RX/TXSTMP registers
43881b05520SVinicius Costa Gomes * into a hwtstamp which can be used by the upper level timestamping functions.
439a2df8463STom Rix *
440a2df8463STom Rix * Returns 0 on success.
44181b05520SVinicius Costa Gomes **/
igc_ptp_systim_to_hwtstamp(struct igc_adapter * adapter,struct skb_shared_hwtstamps * hwtstamps,u64 systim)442a2df8463STom Rix static int igc_ptp_systim_to_hwtstamp(struct igc_adapter *adapter,
44381b05520SVinicius Costa Gomes struct skb_shared_hwtstamps *hwtstamps,
44481b05520SVinicius Costa Gomes u64 systim)
44581b05520SVinicius Costa Gomes {
44681b05520SVinicius Costa Gomes switch (adapter->hw.mac.type) {
44781b05520SVinicius Costa Gomes case igc_i225:
44881b05520SVinicius Costa Gomes memset(hwtstamps, 0, sizeof(*hwtstamps));
44981b05520SVinicius Costa Gomes /* Upper 32 bits contain s, lower 32 bits contain ns. */
45081b05520SVinicius Costa Gomes hwtstamps->hwtstamp = ktime_set(systim >> 32,
45181b05520SVinicius Costa Gomes systim & 0xFFFFFFFF);
45281b05520SVinicius Costa Gomes break;
45381b05520SVinicius Costa Gomes default:
454a2df8463STom Rix return -EINVAL;
45581b05520SVinicius Costa Gomes }
456a2df8463STom Rix return 0;
45781b05520SVinicius Costa Gomes }
45881b05520SVinicius Costa Gomes
45981b05520SVinicius Costa Gomes /**
460fc9e5020SAndre Guedes * igc_ptp_rx_pktstamp - Retrieve timestamp from Rx packet buffer
461e1ed4f92SAndre Guedes * @adapter: Pointer to adapter the packet buffer belongs to
462e1ed4f92SAndre Guedes * @buf: Pointer to packet buffer
46381b05520SVinicius Costa Gomes *
464fc9e5020SAndre Guedes * This function retrieves the timestamp saved in the beginning of packet
465fc9e5020SAndre Guedes * buffer. While two timestamps are available, one in timer0 reference and the
466fc9e5020SAndre Guedes * other in timer1 reference, this function considers only the timestamp in
467fc9e5020SAndre Guedes * timer0 reference.
468e1ed4f92SAndre Guedes *
469e1ed4f92SAndre Guedes * Returns timestamp value.
470fc9e5020SAndre Guedes */
igc_ptp_rx_pktstamp(struct igc_adapter * adapter,__le32 * buf)471e1ed4f92SAndre Guedes ktime_t igc_ptp_rx_pktstamp(struct igc_adapter *adapter, __le32 *buf)
47281b05520SVinicius Costa Gomes {
473e1ed4f92SAndre Guedes ktime_t timestamp;
474e1ed4f92SAndre Guedes u32 secs, nsecs;
475fc9e5020SAndre Guedes int adjust;
47681b05520SVinicius Costa Gomes
477fc9e5020SAndre Guedes /* Timestamps are saved in little endian at the beginning of the packet
478fc9e5020SAndre Guedes * buffer following the layout:
479fc9e5020SAndre Guedes *
480fc9e5020SAndre Guedes * DWORD: | 0 | 1 | 2 | 3 |
481fc9e5020SAndre Guedes * Field: | Timer1 SYSTIML | Timer1 SYSTIMH | Timer0 SYSTIML | Timer0 SYSTIMH |
482fc9e5020SAndre Guedes *
483fc9e5020SAndre Guedes * SYSTIML holds the nanoseconds part while SYSTIMH holds the seconds
484fc9e5020SAndre Guedes * part of the timestamp.
48581b05520SVinicius Costa Gomes */
486e1ed4f92SAndre Guedes nsecs = le32_to_cpu(buf[2]);
487e1ed4f92SAndre Guedes secs = le32_to_cpu(buf[3]);
488e1ed4f92SAndre Guedes
489e1ed4f92SAndre Guedes timestamp = ktime_set(secs, nsecs);
49081b05520SVinicius Costa Gomes
491fc9e5020SAndre Guedes /* Adjust timestamp for the RX latency based on link speed */
49281b05520SVinicius Costa Gomes switch (adapter->link_speed) {
49381b05520SVinicius Costa Gomes case SPEED_10:
49481b05520SVinicius Costa Gomes adjust = IGC_I225_RX_LATENCY_10;
49581b05520SVinicius Costa Gomes break;
49681b05520SVinicius Costa Gomes case SPEED_100:
49781b05520SVinicius Costa Gomes adjust = IGC_I225_RX_LATENCY_100;
49881b05520SVinicius Costa Gomes break;
49981b05520SVinicius Costa Gomes case SPEED_1000:
50081b05520SVinicius Costa Gomes adjust = IGC_I225_RX_LATENCY_1000;
50181b05520SVinicius Costa Gomes break;
50281b05520SVinicius Costa Gomes case SPEED_2500:
50381b05520SVinicius Costa Gomes adjust = IGC_I225_RX_LATENCY_2500;
50481b05520SVinicius Costa Gomes break;
505fc9e5020SAndre Guedes default:
506fc9e5020SAndre Guedes adjust = 0;
507fc9e5020SAndre Guedes netdev_warn_once(adapter->netdev, "Imprecise timestamp\n");
508fc9e5020SAndre Guedes break;
50981b05520SVinicius Costa Gomes }
510e1ed4f92SAndre Guedes
511e1ed4f92SAndre Guedes return ktime_sub_ns(timestamp, adjust);
51281b05520SVinicius Costa Gomes }
51381b05520SVinicius Costa Gomes
igc_ptp_disable_rx_timestamp(struct igc_adapter * adapter)5143df7fd79SAndre Guedes static void igc_ptp_disable_rx_timestamp(struct igc_adapter *adapter)
5153df7fd79SAndre Guedes {
5163df7fd79SAndre Guedes struct igc_hw *hw = &adapter->hw;
5173df7fd79SAndre Guedes u32 val;
5181cbedabfSAndre Guedes int i;
5193df7fd79SAndre Guedes
5203df7fd79SAndre Guedes wr32(IGC_TSYNCRXCTL, 0);
5213df7fd79SAndre Guedes
5221cbedabfSAndre Guedes for (i = 0; i < adapter->num_rx_queues; i++) {
5231cbedabfSAndre Guedes val = rd32(IGC_SRRCTL(i));
5241cbedabfSAndre Guedes val &= ~IGC_SRRCTL_TIMESTAMP;
5251cbedabfSAndre Guedes wr32(IGC_SRRCTL(i), val);
5261cbedabfSAndre Guedes }
5271cbedabfSAndre Guedes
5283df7fd79SAndre Guedes val = rd32(IGC_RXPBS);
5293df7fd79SAndre Guedes val &= ~IGC_RXPBS_CFG_TS_EN;
5303df7fd79SAndre Guedes wr32(IGC_RXPBS, val);
5313df7fd79SAndre Guedes }
5323df7fd79SAndre Guedes
igc_ptp_enable_rx_timestamp(struct igc_adapter * adapter)5333df7fd79SAndre Guedes static void igc_ptp_enable_rx_timestamp(struct igc_adapter *adapter)
5343df7fd79SAndre Guedes {
5353df7fd79SAndre Guedes struct igc_hw *hw = &adapter->hw;
5363df7fd79SAndre Guedes u32 val;
5371cbedabfSAndre Guedes int i;
5383df7fd79SAndre Guedes
5393df7fd79SAndre Guedes val = rd32(IGC_RXPBS);
5403df7fd79SAndre Guedes val |= IGC_RXPBS_CFG_TS_EN;
5413df7fd79SAndre Guedes wr32(IGC_RXPBS, val);
5423df7fd79SAndre Guedes
5431cbedabfSAndre Guedes for (i = 0; i < adapter->num_rx_queues; i++) {
5441cbedabfSAndre Guedes val = rd32(IGC_SRRCTL(i));
5451cbedabfSAndre Guedes /* FIXME: For now, only support retrieving RX timestamps from
5461cbedabfSAndre Guedes * timer 0.
5473df7fd79SAndre Guedes */
5481cbedabfSAndre Guedes val |= IGC_SRRCTL_TIMER1SEL(0) | IGC_SRRCTL_TIMER0SEL(0) |
5491cbedabfSAndre Guedes IGC_SRRCTL_TIMESTAMP;
5501cbedabfSAndre Guedes wr32(IGC_SRRCTL(i), val);
5511cbedabfSAndre Guedes }
5523df7fd79SAndre Guedes
5533df7fd79SAndre Guedes val = IGC_TSYNCRXCTL_ENABLED | IGC_TSYNCRXCTL_TYPE_ALL |
5543df7fd79SAndre Guedes IGC_TSYNCRXCTL_RXSYNSIG;
5553df7fd79SAndre Guedes wr32(IGC_TSYNCRXCTL, val);
5563df7fd79SAndre Guedes }
5573df7fd79SAndre Guedes
igc_ptp_clear_tx_tstamp(struct igc_adapter * adapter)558ce58c7ccSVinicius Costa Gomes static void igc_ptp_clear_tx_tstamp(struct igc_adapter *adapter)
559ce58c7ccSVinicius Costa Gomes {
560ce58c7ccSVinicius Costa Gomes unsigned long flags;
561*3ed247e7SVinicius Costa Gomes int i;
562ce58c7ccSVinicius Costa Gomes
563ce58c7ccSVinicius Costa Gomes spin_lock_irqsave(&adapter->ptp_tx_lock, flags);
564ce58c7ccSVinicius Costa Gomes
565*3ed247e7SVinicius Costa Gomes for (i = 0; i < IGC_MAX_TX_TSTAMP_REGS; i++) {
566*3ed247e7SVinicius Costa Gomes struct igc_tx_timestamp_request *tstamp = &adapter->tx_tstamp[i];
567*3ed247e7SVinicius Costa Gomes
568*3ed247e7SVinicius Costa Gomes dev_kfree_skb_any(tstamp->skb);
569*3ed247e7SVinicius Costa Gomes tstamp->skb = NULL;
570*3ed247e7SVinicius Costa Gomes }
571ce58c7ccSVinicius Costa Gomes
572ce58c7ccSVinicius Costa Gomes spin_unlock_irqrestore(&adapter->ptp_tx_lock, flags);
573ce58c7ccSVinicius Costa Gomes }
574ce58c7ccSVinicius Costa Gomes
igc_ptp_disable_tx_timestamp(struct igc_adapter * adapter)5753df7fd79SAndre Guedes static void igc_ptp_disable_tx_timestamp(struct igc_adapter *adapter)
5763df7fd79SAndre Guedes {
5773df7fd79SAndre Guedes struct igc_hw *hw = &adapter->hw;
578ce58c7ccSVinicius Costa Gomes int i;
579ce58c7ccSVinicius Costa Gomes
580ce58c7ccSVinicius Costa Gomes /* Clear the flags first to avoid new packets to be enqueued
581ce58c7ccSVinicius Costa Gomes * for TX timestamping.
582ce58c7ccSVinicius Costa Gomes */
583ce58c7ccSVinicius Costa Gomes for (i = 0; i < adapter->num_tx_queues; i++) {
584ce58c7ccSVinicius Costa Gomes struct igc_ring *tx_ring = adapter->tx_ring[i];
585ce58c7ccSVinicius Costa Gomes
586ce58c7ccSVinicius Costa Gomes clear_bit(IGC_RING_FLAG_TX_HWTSTAMP, &tx_ring->flags);
587ce58c7ccSVinicius Costa Gomes }
588ce58c7ccSVinicius Costa Gomes
589ce58c7ccSVinicius Costa Gomes /* Now we can clean the pending TX timestamp requests. */
590ce58c7ccSVinicius Costa Gomes igc_ptp_clear_tx_tstamp(adapter);
5913df7fd79SAndre Guedes
5923df7fd79SAndre Guedes wr32(IGC_TSYNCTXCTL, 0);
5933df7fd79SAndre Guedes }
5943df7fd79SAndre Guedes
igc_ptp_enable_tx_timestamp(struct igc_adapter * adapter)5953df7fd79SAndre Guedes static void igc_ptp_enable_tx_timestamp(struct igc_adapter *adapter)
5963df7fd79SAndre Guedes {
5973df7fd79SAndre Guedes struct igc_hw *hw = &adapter->hw;
598ce58c7ccSVinicius Costa Gomes int i;
5993df7fd79SAndre Guedes
6003df7fd79SAndre Guedes wr32(IGC_TSYNCTXCTL, IGC_TSYNCTXCTL_ENABLED | IGC_TSYNCTXCTL_TXSYNSIG);
6013df7fd79SAndre Guedes
6023df7fd79SAndre Guedes /* Read TXSTMP registers to discard any timestamp previously stored. */
6033df7fd79SAndre Guedes rd32(IGC_TXSTMPL);
6043df7fd79SAndre Guedes rd32(IGC_TXSTMPH);
605ce58c7ccSVinicius Costa Gomes
606ce58c7ccSVinicius Costa Gomes /* The hardware is ready to accept TX timestamp requests,
607ce58c7ccSVinicius Costa Gomes * notify the transmit path.
608ce58c7ccSVinicius Costa Gomes */
609ce58c7ccSVinicius Costa Gomes for (i = 0; i < adapter->num_tx_queues; i++) {
610ce58c7ccSVinicius Costa Gomes struct igc_ring *tx_ring = adapter->tx_ring[i];
611ce58c7ccSVinicius Costa Gomes
612ce58c7ccSVinicius Costa Gomes set_bit(IGC_RING_FLAG_TX_HWTSTAMP, &tx_ring->flags);
613ce58c7ccSVinicius Costa Gomes }
614ce58c7ccSVinicius Costa Gomes
6153df7fd79SAndre Guedes }
6163df7fd79SAndre Guedes
61781b05520SVinicius Costa Gomes /**
61881b05520SVinicius Costa Gomes * igc_ptp_set_timestamp_mode - setup hardware for timestamping
61981b05520SVinicius Costa Gomes * @adapter: networking device structure
62081b05520SVinicius Costa Gomes * @config: hwtstamp configuration
62181b05520SVinicius Costa Gomes *
6223b44d4c1SAndre Guedes * Return: 0 in case of success, negative errno code otherwise.
62381b05520SVinicius Costa Gomes */
igc_ptp_set_timestamp_mode(struct igc_adapter * adapter,struct hwtstamp_config * config)6245f295805SVinicius Costa Gomes static int igc_ptp_set_timestamp_mode(struct igc_adapter *adapter,
6255f295805SVinicius Costa Gomes struct hwtstamp_config *config)
6265f295805SVinicius Costa Gomes {
6272c344ae2SVinicius Costa Gomes switch (config->tx_type) {
6282c344ae2SVinicius Costa Gomes case HWTSTAMP_TX_OFF:
6293df7fd79SAndre Guedes igc_ptp_disable_tx_timestamp(adapter);
6303df7fd79SAndre Guedes break;
6312c344ae2SVinicius Costa Gomes case HWTSTAMP_TX_ON:
6323df7fd79SAndre Guedes igc_ptp_enable_tx_timestamp(adapter);
6332c344ae2SVinicius Costa Gomes break;
6342c344ae2SVinicius Costa Gomes default:
6352c344ae2SVinicius Costa Gomes return -ERANGE;
6362c344ae2SVinicius Costa Gomes }
6372c344ae2SVinicius Costa Gomes
63881b05520SVinicius Costa Gomes switch (config->rx_filter) {
63981b05520SVinicius Costa Gomes case HWTSTAMP_FILTER_NONE:
6403df7fd79SAndre Guedes igc_ptp_disable_rx_timestamp(adapter);
64181b05520SVinicius Costa Gomes break;
64281b05520SVinicius Costa Gomes case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
64381b05520SVinicius Costa Gomes case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
64481b05520SVinicius Costa Gomes case HWTSTAMP_FILTER_PTP_V2_EVENT:
64581b05520SVinicius Costa Gomes case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
64681b05520SVinicius Costa Gomes case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
64781b05520SVinicius Costa Gomes case HWTSTAMP_FILTER_PTP_V2_SYNC:
64881b05520SVinicius Costa Gomes case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
64981b05520SVinicius Costa Gomes case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
65081b05520SVinicius Costa Gomes case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
65181b05520SVinicius Costa Gomes case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
65281b05520SVinicius Costa Gomes case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
65381b05520SVinicius Costa Gomes case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
65481b05520SVinicius Costa Gomes case HWTSTAMP_FILTER_NTP_ALL:
65581b05520SVinicius Costa Gomes case HWTSTAMP_FILTER_ALL:
6563df7fd79SAndre Guedes igc_ptp_enable_rx_timestamp(adapter);
65781b05520SVinicius Costa Gomes config->rx_filter = HWTSTAMP_FILTER_ALL;
65881b05520SVinicius Costa Gomes break;
65981b05520SVinicius Costa Gomes default:
66081b05520SVinicius Costa Gomes return -ERANGE;
66181b05520SVinicius Costa Gomes }
66281b05520SVinicius Costa Gomes
6635f295805SVinicius Costa Gomes return 0;
6645f295805SVinicius Costa Gomes }
6655f295805SVinicius Costa Gomes
6669c50e2b1SVinicius Costa Gomes /* Requires adapter->ptp_tx_lock held by caller. */
igc_ptp_tx_timeout(struct igc_adapter * adapter,struct igc_tx_timestamp_request * tstamp)667*3ed247e7SVinicius Costa Gomes static void igc_ptp_tx_timeout(struct igc_adapter *adapter,
668*3ed247e7SVinicius Costa Gomes struct igc_tx_timestamp_request *tstamp)
66929b821feSAndre Guedes {
670*3ed247e7SVinicius Costa Gomes dev_kfree_skb_any(tstamp->skb);
671*3ed247e7SVinicius Costa Gomes tstamp->skb = NULL;
67229b821feSAndre Guedes adapter->tx_hwtstamp_timeouts++;
673*3ed247e7SVinicius Costa Gomes
67429b821feSAndre Guedes netdev_warn(adapter->netdev, "Tx timestamp timeout\n");
67529b821feSAndre Guedes }
67629b821feSAndre Guedes
igc_ptp_tx_hang(struct igc_adapter * adapter)6775f295805SVinicius Costa Gomes void igc_ptp_tx_hang(struct igc_adapter *adapter)
6785f295805SVinicius Costa Gomes {
679*3ed247e7SVinicius Costa Gomes struct igc_tx_timestamp_request *tstamp;
680*3ed247e7SVinicius Costa Gomes struct igc_hw *hw = &adapter->hw;
6819c50e2b1SVinicius Costa Gomes unsigned long flags;
682*3ed247e7SVinicius Costa Gomes bool found = false;
683*3ed247e7SVinicius Costa Gomes int i;
6845f295805SVinicius Costa Gomes
6859c50e2b1SVinicius Costa Gomes spin_lock_irqsave(&adapter->ptp_tx_lock, flags);
6865f295805SVinicius Costa Gomes
687*3ed247e7SVinicius Costa Gomes for (i = 0; i < IGC_MAX_TX_TSTAMP_REGS; i++) {
688*3ed247e7SVinicius Costa Gomes tstamp = &adapter->tx_tstamp[i];
6899c50e2b1SVinicius Costa Gomes
690*3ed247e7SVinicius Costa Gomes if (!tstamp->skb)
691*3ed247e7SVinicius Costa Gomes continue;
6929c50e2b1SVinicius Costa Gomes
693*3ed247e7SVinicius Costa Gomes if (time_is_after_jiffies(tstamp->start + IGC_PTP_TX_TIMEOUT))
694*3ed247e7SVinicius Costa Gomes continue;
6959c50e2b1SVinicius Costa Gomes
696*3ed247e7SVinicius Costa Gomes igc_ptp_tx_timeout(adapter, tstamp);
697*3ed247e7SVinicius Costa Gomes found = true;
698*3ed247e7SVinicius Costa Gomes }
699*3ed247e7SVinicius Costa Gomes
700*3ed247e7SVinicius Costa Gomes if (found) {
701*3ed247e7SVinicius Costa Gomes /* Reading the high register of the first set of timestamp registers
702*3ed247e7SVinicius Costa Gomes * clears all the equivalent bits in the TSYNCTXCTL register.
703*3ed247e7SVinicius Costa Gomes */
704*3ed247e7SVinicius Costa Gomes rd32(IGC_TXSTMPH_0);
705*3ed247e7SVinicius Costa Gomes }
706*3ed247e7SVinicius Costa Gomes
7079c50e2b1SVinicius Costa Gomes spin_unlock_irqrestore(&adapter->ptp_tx_lock, flags);
7085f295805SVinicius Costa Gomes }
7095f295805SVinicius Costa Gomes
igc_ptp_tx_reg_to_stamp(struct igc_adapter * adapter,struct igc_tx_timestamp_request * tstamp,u64 regval)710*3ed247e7SVinicius Costa Gomes static void igc_ptp_tx_reg_to_stamp(struct igc_adapter *adapter,
711*3ed247e7SVinicius Costa Gomes struct igc_tx_timestamp_request *tstamp, u64 regval)
712*3ed247e7SVinicius Costa Gomes {
713*3ed247e7SVinicius Costa Gomes struct skb_shared_hwtstamps shhwtstamps;
714*3ed247e7SVinicius Costa Gomes struct sk_buff *skb;
715*3ed247e7SVinicius Costa Gomes int adjust = 0;
716*3ed247e7SVinicius Costa Gomes
717*3ed247e7SVinicius Costa Gomes skb = tstamp->skb;
718*3ed247e7SVinicius Costa Gomes if (!skb)
719*3ed247e7SVinicius Costa Gomes return;
720*3ed247e7SVinicius Costa Gomes
721*3ed247e7SVinicius Costa Gomes if (igc_ptp_systim_to_hwtstamp(adapter, &shhwtstamps, regval))
722*3ed247e7SVinicius Costa Gomes return;
723*3ed247e7SVinicius Costa Gomes
724*3ed247e7SVinicius Costa Gomes switch (adapter->link_speed) {
725*3ed247e7SVinicius Costa Gomes case SPEED_10:
726*3ed247e7SVinicius Costa Gomes adjust = IGC_I225_TX_LATENCY_10;
727*3ed247e7SVinicius Costa Gomes break;
728*3ed247e7SVinicius Costa Gomes case SPEED_100:
729*3ed247e7SVinicius Costa Gomes adjust = IGC_I225_TX_LATENCY_100;
730*3ed247e7SVinicius Costa Gomes break;
731*3ed247e7SVinicius Costa Gomes case SPEED_1000:
732*3ed247e7SVinicius Costa Gomes adjust = IGC_I225_TX_LATENCY_1000;
733*3ed247e7SVinicius Costa Gomes break;
734*3ed247e7SVinicius Costa Gomes case SPEED_2500:
735*3ed247e7SVinicius Costa Gomes adjust = IGC_I225_TX_LATENCY_2500;
736*3ed247e7SVinicius Costa Gomes break;
737*3ed247e7SVinicius Costa Gomes }
738*3ed247e7SVinicius Costa Gomes
739*3ed247e7SVinicius Costa Gomes shhwtstamps.hwtstamp =
740*3ed247e7SVinicius Costa Gomes ktime_add_ns(shhwtstamps.hwtstamp, adjust);
741*3ed247e7SVinicius Costa Gomes
742*3ed247e7SVinicius Costa Gomes tstamp->skb = NULL;
743*3ed247e7SVinicius Costa Gomes
744*3ed247e7SVinicius Costa Gomes skb_tstamp_tx(skb, &shhwtstamps);
745*3ed247e7SVinicius Costa Gomes dev_kfree_skb_any(skb);
746*3ed247e7SVinicius Costa Gomes }
747*3ed247e7SVinicius Costa Gomes
7482c344ae2SVinicius Costa Gomes /**
7492c344ae2SVinicius Costa Gomes * igc_ptp_tx_hwtstamp - utility function which checks for TX time stamp
7502c344ae2SVinicius Costa Gomes * @adapter: Board private structure
7512c344ae2SVinicius Costa Gomes *
752*3ed247e7SVinicius Costa Gomes * Check against the ready mask for which of the timestamp register
753*3ed247e7SVinicius Costa Gomes * sets are ready to be retrieved, then retrieve that and notify the
754*3ed247e7SVinicius Costa Gomes * rest of the stack.
7559c50e2b1SVinicius Costa Gomes *
7569c50e2b1SVinicius Costa Gomes * Context: Expects adapter->ptp_tx_lock to be held by caller.
7572c344ae2SVinicius Costa Gomes */
igc_ptp_tx_hwtstamp(struct igc_adapter * adapter)7582c344ae2SVinicius Costa Gomes static void igc_ptp_tx_hwtstamp(struct igc_adapter *adapter)
7592c344ae2SVinicius Costa Gomes {
7602c344ae2SVinicius Costa Gomes struct igc_hw *hw = &adapter->hw;
7612c344ae2SVinicius Costa Gomes u64 regval;
762*3ed247e7SVinicius Costa Gomes u32 mask;
763*3ed247e7SVinicius Costa Gomes int i;
7642c344ae2SVinicius Costa Gomes
765*3ed247e7SVinicius Costa Gomes mask = rd32(IGC_TSYNCTXCTL) & IGC_TSYNCTXCTL_TXTT_ANY;
766*3ed247e7SVinicius Costa Gomes if (mask & IGC_TSYNCTXCTL_TXTT_0) {
7672c344ae2SVinicius Costa Gomes regval = rd32(IGC_TXSTMPL);
7682c344ae2SVinicius Costa Gomes regval |= (u64)rd32(IGC_TXSTMPH) << 32;
769c789ad7cSVinicius Costa Gomes } else {
770c789ad7cSVinicius Costa Gomes /* There's a bug in the hardware that could cause
771c789ad7cSVinicius Costa Gomes * missing interrupts for TX timestamping. The issue
772c789ad7cSVinicius Costa Gomes * is that for new interrupts to be triggered, the
773c789ad7cSVinicius Costa Gomes * IGC_TXSTMPH_0 register must be read.
774c789ad7cSVinicius Costa Gomes *
775c789ad7cSVinicius Costa Gomes * To avoid discarding a valid timestamp that just
776c789ad7cSVinicius Costa Gomes * happened at the "wrong" time, we need to confirm
777c789ad7cSVinicius Costa Gomes * that there was no timestamp captured, we do that by
778c789ad7cSVinicius Costa Gomes * assuming that no two timestamps in sequence have
779c789ad7cSVinicius Costa Gomes * the same nanosecond value.
780c789ad7cSVinicius Costa Gomes *
781c789ad7cSVinicius Costa Gomes * So, we read the "low" register, read the "high"
782c789ad7cSVinicius Costa Gomes * register (to latch a new timestamp) and read the
783c789ad7cSVinicius Costa Gomes * "low" register again, if "old" and "new" versions
784c789ad7cSVinicius Costa Gomes * of the "low" register are different, a valid
785c789ad7cSVinicius Costa Gomes * timestamp was captured, we can read the "high"
786c789ad7cSVinicius Costa Gomes * register again.
787c789ad7cSVinicius Costa Gomes */
788c789ad7cSVinicius Costa Gomes u32 txstmpl_old, txstmpl_new;
789c789ad7cSVinicius Costa Gomes
790c789ad7cSVinicius Costa Gomes txstmpl_old = rd32(IGC_TXSTMPL);
791c789ad7cSVinicius Costa Gomes rd32(IGC_TXSTMPH);
792c789ad7cSVinicius Costa Gomes txstmpl_new = rd32(IGC_TXSTMPL);
793c789ad7cSVinicius Costa Gomes
794c789ad7cSVinicius Costa Gomes if (txstmpl_old == txstmpl_new)
795*3ed247e7SVinicius Costa Gomes goto done;
796c789ad7cSVinicius Costa Gomes
797c789ad7cSVinicius Costa Gomes regval = txstmpl_new;
798c789ad7cSVinicius Costa Gomes regval |= (u64)rd32(IGC_TXSTMPH) << 32;
799c789ad7cSVinicius Costa Gomes }
8002c344ae2SVinicius Costa Gomes
801*3ed247e7SVinicius Costa Gomes igc_ptp_tx_reg_to_stamp(adapter, &adapter->tx_tstamp[0], regval);
802*3ed247e7SVinicius Costa Gomes
803*3ed247e7SVinicius Costa Gomes done:
804*3ed247e7SVinicius Costa Gomes /* Now that the problematic first register was handled, we can
805*3ed247e7SVinicius Costa Gomes * use retrieve the timestamps from the other registers
806*3ed247e7SVinicius Costa Gomes * (starting from '1') with less complications.
807*3ed247e7SVinicius Costa Gomes */
808*3ed247e7SVinicius Costa Gomes for (i = 1; i < IGC_MAX_TX_TSTAMP_REGS; i++) {
809*3ed247e7SVinicius Costa Gomes struct igc_tx_timestamp_request *tstamp = &adapter->tx_tstamp[i];
810*3ed247e7SVinicius Costa Gomes
811*3ed247e7SVinicius Costa Gomes if (!(tstamp->mask & mask))
812*3ed247e7SVinicius Costa Gomes continue;
813*3ed247e7SVinicius Costa Gomes
814*3ed247e7SVinicius Costa Gomes regval = rd32(tstamp->regl);
815*3ed247e7SVinicius Costa Gomes regval |= (u64)rd32(tstamp->regh) << 32;
816*3ed247e7SVinicius Costa Gomes
817*3ed247e7SVinicius Costa Gomes igc_ptp_tx_reg_to_stamp(adapter, tstamp, regval);
8184406e977SVinicius Costa Gomes }
8192c344ae2SVinicius Costa Gomes }
8202c344ae2SVinicius Costa Gomes
8212c344ae2SVinicius Costa Gomes /**
822afa14158SVinicius Costa Gomes * igc_ptp_tx_tstamp_event
823afa14158SVinicius Costa Gomes * @adapter: board private structure
8242c344ae2SVinicius Costa Gomes *
825afa14158SVinicius Costa Gomes * Called when a TX timestamp interrupt happens to retrieve the
826afa14158SVinicius Costa Gomes * timestamp and send it up to the socket.
8272c344ae2SVinicius Costa Gomes */
igc_ptp_tx_tstamp_event(struct igc_adapter * adapter)828afa14158SVinicius Costa Gomes void igc_ptp_tx_tstamp_event(struct igc_adapter *adapter)
8295f295805SVinicius Costa Gomes {
8309c50e2b1SVinicius Costa Gomes unsigned long flags;
8312c344ae2SVinicius Costa Gomes
8329c50e2b1SVinicius Costa Gomes spin_lock_irqsave(&adapter->ptp_tx_lock, flags);
8339c50e2b1SVinicius Costa Gomes
8342c344ae2SVinicius Costa Gomes igc_ptp_tx_hwtstamp(adapter);
8359c50e2b1SVinicius Costa Gomes
8369c50e2b1SVinicius Costa Gomes spin_unlock_irqrestore(&adapter->ptp_tx_lock, flags);
8375f295805SVinicius Costa Gomes }
8385f295805SVinicius Costa Gomes
8395f295805SVinicius Costa Gomes /**
8405f295805SVinicius Costa Gomes * igc_ptp_set_ts_config - set hardware time stamping config
8415f295805SVinicius Costa Gomes * @netdev: network interface device structure
842b50f7bcaSJesse Brandeburg * @ifr: interface request data
8435f295805SVinicius Costa Gomes *
8445f295805SVinicius Costa Gomes **/
igc_ptp_set_ts_config(struct net_device * netdev,struct ifreq * ifr)8455f295805SVinicius Costa Gomes int igc_ptp_set_ts_config(struct net_device *netdev, struct ifreq *ifr)
8465f295805SVinicius Costa Gomes {
8475f295805SVinicius Costa Gomes struct igc_adapter *adapter = netdev_priv(netdev);
8485f295805SVinicius Costa Gomes struct hwtstamp_config config;
8495f295805SVinicius Costa Gomes int err;
8505f295805SVinicius Costa Gomes
8515f295805SVinicius Costa Gomes if (copy_from_user(&config, ifr->ifr_data, sizeof(config)))
8525f295805SVinicius Costa Gomes return -EFAULT;
8535f295805SVinicius Costa Gomes
8545f295805SVinicius Costa Gomes err = igc_ptp_set_timestamp_mode(adapter, &config);
8555f295805SVinicius Costa Gomes if (err)
8565f295805SVinicius Costa Gomes return err;
8575f295805SVinicius Costa Gomes
8585f295805SVinicius Costa Gomes /* save these settings for future reference */
8595f295805SVinicius Costa Gomes memcpy(&adapter->tstamp_config, &config,
8605f295805SVinicius Costa Gomes sizeof(adapter->tstamp_config));
8615f295805SVinicius Costa Gomes
8625f295805SVinicius Costa Gomes return copy_to_user(ifr->ifr_data, &config, sizeof(config)) ?
8635f295805SVinicius Costa Gomes -EFAULT : 0;
8645f295805SVinicius Costa Gomes }
8655f295805SVinicius Costa Gomes
8665f295805SVinicius Costa Gomes /**
8675f295805SVinicius Costa Gomes * igc_ptp_get_ts_config - get hardware time stamping config
8685f295805SVinicius Costa Gomes * @netdev: network interface device structure
869b50f7bcaSJesse Brandeburg * @ifr: interface request data
8705f295805SVinicius Costa Gomes *
8715f295805SVinicius Costa Gomes * Get the hwtstamp_config settings to return to the user. Rather than attempt
8725f295805SVinicius Costa Gomes * to deconstruct the settings from the registers, just return a shadow copy
8735f295805SVinicius Costa Gomes * of the last known settings.
8745f295805SVinicius Costa Gomes **/
igc_ptp_get_ts_config(struct net_device * netdev,struct ifreq * ifr)8755f295805SVinicius Costa Gomes int igc_ptp_get_ts_config(struct net_device *netdev, struct ifreq *ifr)
8765f295805SVinicius Costa Gomes {
8775f295805SVinicius Costa Gomes struct igc_adapter *adapter = netdev_priv(netdev);
8785f295805SVinicius Costa Gomes struct hwtstamp_config *config = &adapter->tstamp_config;
8795f295805SVinicius Costa Gomes
8805f295805SVinicius Costa Gomes return copy_to_user(ifr->ifr_data, config, sizeof(*config)) ?
8815f295805SVinicius Costa Gomes -EFAULT : 0;
8825f295805SVinicius Costa Gomes }
8835f295805SVinicius Costa Gomes
884a90ec848SVinicius Costa Gomes /* The two conditions below must be met for cross timestamping via
885a90ec848SVinicius Costa Gomes * PCIe PTM:
886a90ec848SVinicius Costa Gomes *
887a90ec848SVinicius Costa Gomes * 1. We have an way to convert the timestamps in the PTM messages
888a90ec848SVinicius Costa Gomes * to something related to the system clocks (right now, only
889a90ec848SVinicius Costa Gomes * X86 systems with support for the Always Running Timer allow that);
890a90ec848SVinicius Costa Gomes *
891a90ec848SVinicius Costa Gomes * 2. We have PTM enabled in the path from the device to the PCIe root port.
892a90ec848SVinicius Costa Gomes */
igc_is_crosststamp_supported(struct igc_adapter * adapter)893a90ec848SVinicius Costa Gomes static bool igc_is_crosststamp_supported(struct igc_adapter *adapter)
894a90ec848SVinicius Costa Gomes {
8951e81dcc1SVinicius Costa Gomes if (!IS_ENABLED(CONFIG_X86_TSC))
8961e81dcc1SVinicius Costa Gomes return false;
8971e81dcc1SVinicius Costa Gomes
8981e81dcc1SVinicius Costa Gomes /* FIXME: it was noticed that enabling support for PCIe PTM in
8991e81dcc1SVinicius Costa Gomes * some i225-V models could cause lockups when bringing the
9001e81dcc1SVinicius Costa Gomes * interface up/down. There should be no downsides to
9011e81dcc1SVinicius Costa Gomes * disabling crosstimestamping support for i225-V, as it
9021e81dcc1SVinicius Costa Gomes * doesn't have any PTP support. That way we gain some time
9031e81dcc1SVinicius Costa Gomes * while root causing the issue.
9041e81dcc1SVinicius Costa Gomes */
9051e81dcc1SVinicius Costa Gomes if (adapter->pdev->device == IGC_DEV_ID_I225_V)
9061e81dcc1SVinicius Costa Gomes return false;
9071e81dcc1SVinicius Costa Gomes
9081e81dcc1SVinicius Costa Gomes return pcie_ptm_enabled(adapter->pdev);
909a90ec848SVinicius Costa Gomes }
910a90ec848SVinicius Costa Gomes
igc_device_tstamp_to_system(u64 tstamp)911a90ec848SVinicius Costa Gomes static struct system_counterval_t igc_device_tstamp_to_system(u64 tstamp)
912a90ec848SVinicius Costa Gomes {
913523994baSRandy Dunlap #if IS_ENABLED(CONFIG_X86_TSC) && !defined(CONFIG_UML)
914a90ec848SVinicius Costa Gomes return convert_art_ns_to_tsc(tstamp);
915a90ec848SVinicius Costa Gomes #else
916a90ec848SVinicius Costa Gomes return (struct system_counterval_t) { };
917a90ec848SVinicius Costa Gomes #endif
918a90ec848SVinicius Costa Gomes }
919a90ec848SVinicius Costa Gomes
igc_ptm_log_error(struct igc_adapter * adapter,u32 ptm_stat)920a90ec848SVinicius Costa Gomes static void igc_ptm_log_error(struct igc_adapter *adapter, u32 ptm_stat)
921a90ec848SVinicius Costa Gomes {
922a90ec848SVinicius Costa Gomes struct net_device *netdev = adapter->netdev;
923a90ec848SVinicius Costa Gomes
924a90ec848SVinicius Costa Gomes switch (ptm_stat) {
925a90ec848SVinicius Costa Gomes case IGC_PTM_STAT_RET_ERR:
926a90ec848SVinicius Costa Gomes netdev_err(netdev, "PTM Error: Root port timeout\n");
927a90ec848SVinicius Costa Gomes break;
928a90ec848SVinicius Costa Gomes case IGC_PTM_STAT_BAD_PTM_RES:
929a90ec848SVinicius Costa Gomes netdev_err(netdev, "PTM Error: Bad response, PTM Response Data expected\n");
930a90ec848SVinicius Costa Gomes break;
931a90ec848SVinicius Costa Gomes case IGC_PTM_STAT_T4M1_OVFL:
932a90ec848SVinicius Costa Gomes netdev_err(netdev, "PTM Error: T4 minus T1 overflow\n");
933a90ec848SVinicius Costa Gomes break;
934a90ec848SVinicius Costa Gomes case IGC_PTM_STAT_ADJUST_1ST:
935a90ec848SVinicius Costa Gomes netdev_err(netdev, "PTM Error: 1588 timer adjusted during first PTM cycle\n");
936a90ec848SVinicius Costa Gomes break;
937a90ec848SVinicius Costa Gomes case IGC_PTM_STAT_ADJUST_CYC:
938a90ec848SVinicius Costa Gomes netdev_err(netdev, "PTM Error: 1588 timer adjusted during non-first PTM cycle\n");
939a90ec848SVinicius Costa Gomes break;
940a90ec848SVinicius Costa Gomes default:
941a90ec848SVinicius Costa Gomes netdev_err(netdev, "PTM Error: Unknown error (%#x)\n", ptm_stat);
942a90ec848SVinicius Costa Gomes break;
943a90ec848SVinicius Costa Gomes }
944a90ec848SVinicius Costa Gomes }
945a90ec848SVinicius Costa Gomes
igc_phc_get_syncdevicetime(ktime_t * device,struct system_counterval_t * system,void * ctx)946a90ec848SVinicius Costa Gomes static int igc_phc_get_syncdevicetime(ktime_t *device,
947a90ec848SVinicius Costa Gomes struct system_counterval_t *system,
948a90ec848SVinicius Costa Gomes void *ctx)
949a90ec848SVinicius Costa Gomes {
950a90ec848SVinicius Costa Gomes u32 stat, t2_curr_h, t2_curr_l, ctrl;
951a90ec848SVinicius Costa Gomes struct igc_adapter *adapter = ctx;
952a90ec848SVinicius Costa Gomes struct igc_hw *hw = &adapter->hw;
953a90ec848SVinicius Costa Gomes int err, count = 100;
954a90ec848SVinicius Costa Gomes ktime_t t1, t2_curr;
955a90ec848SVinicius Costa Gomes
956a90ec848SVinicius Costa Gomes /* Get a snapshot of system clocks to use as historic value. */
957a90ec848SVinicius Costa Gomes ktime_get_snapshot(&adapter->snapshot);
958a90ec848SVinicius Costa Gomes
959a90ec848SVinicius Costa Gomes do {
960a90ec848SVinicius Costa Gomes /* Doing this in a loop because in the event of a
961a90ec848SVinicius Costa Gomes * badly timed (ha!) system clock adjustment, we may
962a90ec848SVinicius Costa Gomes * get PTM errors from the PCI root, but these errors
963a90ec848SVinicius Costa Gomes * are transitory. Repeating the process returns valid
964a90ec848SVinicius Costa Gomes * data eventually.
965a90ec848SVinicius Costa Gomes */
966a90ec848SVinicius Costa Gomes
967a90ec848SVinicius Costa Gomes /* To "manually" start the PTM cycle we need to clear and
968a90ec848SVinicius Costa Gomes * then set again the TRIG bit.
969a90ec848SVinicius Costa Gomes */
970a90ec848SVinicius Costa Gomes ctrl = rd32(IGC_PTM_CTRL);
971a90ec848SVinicius Costa Gomes ctrl &= ~IGC_PTM_CTRL_TRIG;
972a90ec848SVinicius Costa Gomes wr32(IGC_PTM_CTRL, ctrl);
973a90ec848SVinicius Costa Gomes ctrl |= IGC_PTM_CTRL_TRIG;
974a90ec848SVinicius Costa Gomes wr32(IGC_PTM_CTRL, ctrl);
975a90ec848SVinicius Costa Gomes
976a90ec848SVinicius Costa Gomes /* The cycle only starts "for real" when software notifies
977a90ec848SVinicius Costa Gomes * that it has read the registers, this is done by setting
978a90ec848SVinicius Costa Gomes * VALID bit.
979a90ec848SVinicius Costa Gomes */
980a90ec848SVinicius Costa Gomes wr32(IGC_PTM_STAT, IGC_PTM_STAT_VALID);
981a90ec848SVinicius Costa Gomes
982a90ec848SVinicius Costa Gomes err = readx_poll_timeout(rd32, IGC_PTM_STAT, stat,
983a90ec848SVinicius Costa Gomes stat, IGC_PTM_STAT_SLEEP,
984a90ec848SVinicius Costa Gomes IGC_PTM_STAT_TIMEOUT);
985a90ec848SVinicius Costa Gomes if (err < 0) {
986a90ec848SVinicius Costa Gomes netdev_err(adapter->netdev, "Timeout reading IGC_PTM_STAT register\n");
987a90ec848SVinicius Costa Gomes return err;
988a90ec848SVinicius Costa Gomes }
989a90ec848SVinicius Costa Gomes
990a90ec848SVinicius Costa Gomes if ((stat & IGC_PTM_STAT_VALID) == IGC_PTM_STAT_VALID)
991a90ec848SVinicius Costa Gomes break;
992a90ec848SVinicius Costa Gomes
993a90ec848SVinicius Costa Gomes if (stat & ~IGC_PTM_STAT_VALID) {
994a90ec848SVinicius Costa Gomes /* An error occurred, log it. */
995a90ec848SVinicius Costa Gomes igc_ptm_log_error(adapter, stat);
996a90ec848SVinicius Costa Gomes /* The STAT register is write-1-to-clear (W1C),
997a90ec848SVinicius Costa Gomes * so write the previous error status to clear it.
998a90ec848SVinicius Costa Gomes */
999a90ec848SVinicius Costa Gomes wr32(IGC_PTM_STAT, stat);
1000a90ec848SVinicius Costa Gomes continue;
1001a90ec848SVinicius Costa Gomes }
1002a90ec848SVinicius Costa Gomes } while (--count);
1003a90ec848SVinicius Costa Gomes
1004a90ec848SVinicius Costa Gomes if (!count) {
1005a90ec848SVinicius Costa Gomes netdev_err(adapter->netdev, "Exceeded number of tries for PTM cycle\n");
1006a90ec848SVinicius Costa Gomes return -ETIMEDOUT;
1007a90ec848SVinicius Costa Gomes }
1008a90ec848SVinicius Costa Gomes
1009a90ec848SVinicius Costa Gomes t1 = ktime_set(rd32(IGC_PTM_T1_TIM0_H), rd32(IGC_PTM_T1_TIM0_L));
1010a90ec848SVinicius Costa Gomes
1011a90ec848SVinicius Costa Gomes t2_curr_l = rd32(IGC_PTM_CURR_T2_L);
1012a90ec848SVinicius Costa Gomes t2_curr_h = rd32(IGC_PTM_CURR_T2_H);
1013a90ec848SVinicius Costa Gomes
1014a90ec848SVinicius Costa Gomes /* FIXME: When the register that tells the endianness of the
1015a90ec848SVinicius Costa Gomes * PTM registers are implemented, check them here and add the
1016a90ec848SVinicius Costa Gomes * appropriate conversion.
1017a90ec848SVinicius Costa Gomes */
1018a90ec848SVinicius Costa Gomes t2_curr_h = swab32(t2_curr_h);
1019a90ec848SVinicius Costa Gomes
1020a90ec848SVinicius Costa Gomes t2_curr = ((s64)t2_curr_h << 32 | t2_curr_l);
1021a90ec848SVinicius Costa Gomes
1022a90ec848SVinicius Costa Gomes *device = t1;
1023a90ec848SVinicius Costa Gomes *system = igc_device_tstamp_to_system(t2_curr);
1024a90ec848SVinicius Costa Gomes
1025a90ec848SVinicius Costa Gomes return 0;
1026a90ec848SVinicius Costa Gomes }
1027a90ec848SVinicius Costa Gomes
igc_ptp_getcrosststamp(struct ptp_clock_info * ptp,struct system_device_crosststamp * cts)1028a90ec848SVinicius Costa Gomes static int igc_ptp_getcrosststamp(struct ptp_clock_info *ptp,
1029a90ec848SVinicius Costa Gomes struct system_device_crosststamp *cts)
1030a90ec848SVinicius Costa Gomes {
1031a90ec848SVinicius Costa Gomes struct igc_adapter *adapter = container_of(ptp, struct igc_adapter,
1032a90ec848SVinicius Costa Gomes ptp_caps);
1033a90ec848SVinicius Costa Gomes
1034a90ec848SVinicius Costa Gomes return get_device_system_crosststamp(igc_phc_get_syncdevicetime,
1035a90ec848SVinicius Costa Gomes adapter, &adapter->snapshot, cts);
1036a90ec848SVinicius Costa Gomes }
1037a90ec848SVinicius Costa Gomes
10385f295805SVinicius Costa Gomes /**
10395f295805SVinicius Costa Gomes * igc_ptp_init - Initialize PTP functionality
10405f295805SVinicius Costa Gomes * @adapter: Board private structure
10415f295805SVinicius Costa Gomes *
10425f295805SVinicius Costa Gomes * This function is called at device probe to initialize the PTP
10435f295805SVinicius Costa Gomes * functionality.
10445f295805SVinicius Costa Gomes */
igc_ptp_init(struct igc_adapter * adapter)10455f295805SVinicius Costa Gomes void igc_ptp_init(struct igc_adapter *adapter)
10465f295805SVinicius Costa Gomes {
10475f295805SVinicius Costa Gomes struct net_device *netdev = adapter->netdev;
1048*3ed247e7SVinicius Costa Gomes struct igc_tx_timestamp_request *tstamp;
10495f295805SVinicius Costa Gomes struct igc_hw *hw = &adapter->hw;
105087938851SEderson de Souza int i;
10515f295805SVinicius Costa Gomes
1052*3ed247e7SVinicius Costa Gomes tstamp = &adapter->tx_tstamp[0];
1053*3ed247e7SVinicius Costa Gomes tstamp->mask = IGC_TSYNCTXCTL_TXTT_0;
1054*3ed247e7SVinicius Costa Gomes tstamp->regl = IGC_TXSTMPL_0;
1055*3ed247e7SVinicius Costa Gomes tstamp->regh = IGC_TXSTMPH_0;
1056*3ed247e7SVinicius Costa Gomes tstamp->flags = 0;
1057*3ed247e7SVinicius Costa Gomes
1058*3ed247e7SVinicius Costa Gomes tstamp = &adapter->tx_tstamp[1];
1059*3ed247e7SVinicius Costa Gomes tstamp->mask = IGC_TSYNCTXCTL_TXTT_1;
1060*3ed247e7SVinicius Costa Gomes tstamp->regl = IGC_TXSTMPL_1;
1061*3ed247e7SVinicius Costa Gomes tstamp->regh = IGC_TXSTMPH_1;
1062*3ed247e7SVinicius Costa Gomes tstamp->flags = IGC_TX_FLAGS_TSTAMP_1;
1063*3ed247e7SVinicius Costa Gomes
1064*3ed247e7SVinicius Costa Gomes tstamp = &adapter->tx_tstamp[2];
1065*3ed247e7SVinicius Costa Gomes tstamp->mask = IGC_TSYNCTXCTL_TXTT_2;
1066*3ed247e7SVinicius Costa Gomes tstamp->regl = IGC_TXSTMPL_2;
1067*3ed247e7SVinicius Costa Gomes tstamp->regh = IGC_TXSTMPH_2;
1068*3ed247e7SVinicius Costa Gomes tstamp->flags = IGC_TX_FLAGS_TSTAMP_2;
1069*3ed247e7SVinicius Costa Gomes
1070*3ed247e7SVinicius Costa Gomes tstamp = &adapter->tx_tstamp[3];
1071*3ed247e7SVinicius Costa Gomes tstamp->mask = IGC_TSYNCTXCTL_TXTT_3;
1072*3ed247e7SVinicius Costa Gomes tstamp->regl = IGC_TXSTMPL_3;
1073*3ed247e7SVinicius Costa Gomes tstamp->regh = IGC_TXSTMPH_3;
1074*3ed247e7SVinicius Costa Gomes tstamp->flags = IGC_TX_FLAGS_TSTAMP_3;
1075*3ed247e7SVinicius Costa Gomes
10765f295805SVinicius Costa Gomes switch (hw->mac.type) {
10775f295805SVinicius Costa Gomes case igc_i225:
107887938851SEderson de Souza for (i = 0; i < IGC_N_SDP; i++) {
107987938851SEderson de Souza struct ptp_pin_desc *ppd = &adapter->sdp_config[i];
108087938851SEderson de Souza
108187938851SEderson de Souza snprintf(ppd->name, sizeof(ppd->name), "SDP%d", i);
108287938851SEderson de Souza ppd->index = i;
108387938851SEderson de Souza ppd->func = PTP_PF_NONE;
108487938851SEderson de Souza }
10855f295805SVinicius Costa Gomes snprintf(adapter->ptp_caps.name, 16, "%pm", netdev->dev_addr);
10865f295805SVinicius Costa Gomes adapter->ptp_caps.owner = THIS_MODULE;
10875f295805SVinicius Costa Gomes adapter->ptp_caps.max_adj = 62499999;
10885f295805SVinicius Costa Gomes adapter->ptp_caps.adjfine = igc_ptp_adjfine_i225;
10895f295805SVinicius Costa Gomes adapter->ptp_caps.adjtime = igc_ptp_adjtime_i225;
10905f295805SVinicius Costa Gomes adapter->ptp_caps.gettimex64 = igc_ptp_gettimex64_i225;
10915f295805SVinicius Costa Gomes adapter->ptp_caps.settime64 = igc_ptp_settime_i225;
10925f295805SVinicius Costa Gomes adapter->ptp_caps.enable = igc_ptp_feature_enable_i225;
109364433e5bSEderson de Souza adapter->ptp_caps.pps = 1;
109487938851SEderson de Souza adapter->ptp_caps.pin_config = adapter->sdp_config;
109587938851SEderson de Souza adapter->ptp_caps.n_ext_ts = IGC_N_EXTTS;
109687938851SEderson de Souza adapter->ptp_caps.n_per_out = IGC_N_PEROUT;
109787938851SEderson de Souza adapter->ptp_caps.n_pins = IGC_N_SDP;
109887938851SEderson de Souza adapter->ptp_caps.verify = igc_ptp_verify_pin;
1099a90ec848SVinicius Costa Gomes
1100a90ec848SVinicius Costa Gomes if (!igc_is_crosststamp_supported(adapter))
1101a90ec848SVinicius Costa Gomes break;
1102a90ec848SVinicius Costa Gomes
1103a90ec848SVinicius Costa Gomes adapter->ptp_caps.getcrosststamp = igc_ptp_getcrosststamp;
11045f295805SVinicius Costa Gomes break;
11055f295805SVinicius Costa Gomes default:
11065f295805SVinicius Costa Gomes adapter->ptp_clock = NULL;
11075f295805SVinicius Costa Gomes return;
11085f295805SVinicius Costa Gomes }
11095f295805SVinicius Costa Gomes
11109c50e2b1SVinicius Costa Gomes spin_lock_init(&adapter->ptp_tx_lock);
11115f295805SVinicius Costa Gomes spin_lock_init(&adapter->tmreg_lock);
11125f295805SVinicius Costa Gomes
11135f295805SVinicius Costa Gomes adapter->tstamp_config.rx_filter = HWTSTAMP_FILTER_NONE;
11145f295805SVinicius Costa Gomes adapter->tstamp_config.tx_type = HWTSTAMP_TX_OFF;
11155f295805SVinicius Costa Gomes
1116b03c49cdSVinicius Costa Gomes adapter->prev_ptp_time = ktime_to_timespec64(ktime_get_real());
1117b03c49cdSVinicius Costa Gomes adapter->ptp_reset_start = ktime_get();
1118b03c49cdSVinicius Costa Gomes
11195f295805SVinicius Costa Gomes adapter->ptp_clock = ptp_clock_register(&adapter->ptp_caps,
11205f295805SVinicius Costa Gomes &adapter->pdev->dev);
11215f295805SVinicius Costa Gomes if (IS_ERR(adapter->ptp_clock)) {
11225f295805SVinicius Costa Gomes adapter->ptp_clock = NULL;
1123916a3c65SAndre Guedes netdev_err(netdev, "ptp_clock_register failed\n");
11245f295805SVinicius Costa Gomes } else if (adapter->ptp_clock) {
1125916a3c65SAndre Guedes netdev_info(netdev, "PHC added\n");
11265f295805SVinicius Costa Gomes adapter->ptp_flags |= IGC_PTP_ENABLED;
11275f295805SVinicius Costa Gomes }
11285f295805SVinicius Costa Gomes }
11295f295805SVinicius Costa Gomes
igc_ptp_time_save(struct igc_adapter * adapter)1130b03c49cdSVinicius Costa Gomes static void igc_ptp_time_save(struct igc_adapter *adapter)
1131b03c49cdSVinicius Costa Gomes {
1132fec49eb4SVinicius Costa Gomes igc_ptp_read(adapter, &adapter->prev_ptp_time);
1133b03c49cdSVinicius Costa Gomes adapter->ptp_reset_start = ktime_get();
1134b03c49cdSVinicius Costa Gomes }
1135b03c49cdSVinicius Costa Gomes
igc_ptp_time_restore(struct igc_adapter * adapter)1136b03c49cdSVinicius Costa Gomes static void igc_ptp_time_restore(struct igc_adapter *adapter)
1137b03c49cdSVinicius Costa Gomes {
1138b03c49cdSVinicius Costa Gomes struct timespec64 ts = adapter->prev_ptp_time;
1139b03c49cdSVinicius Costa Gomes ktime_t delta;
1140b03c49cdSVinicius Costa Gomes
1141b03c49cdSVinicius Costa Gomes delta = ktime_sub(ktime_get(), adapter->ptp_reset_start);
1142b03c49cdSVinicius Costa Gomes
1143b03c49cdSVinicius Costa Gomes timespec64_add_ns(&ts, ktime_to_ns(delta));
1144b03c49cdSVinicius Costa Gomes
1145b03c49cdSVinicius Costa Gomes igc_ptp_write_i225(adapter, &ts);
1146b03c49cdSVinicius Costa Gomes }
1147b03c49cdSVinicius Costa Gomes
igc_ptm_stop(struct igc_adapter * adapter)1148822f52e7SVinicius Costa Gomes static void igc_ptm_stop(struct igc_adapter *adapter)
1149822f52e7SVinicius Costa Gomes {
1150822f52e7SVinicius Costa Gomes struct igc_hw *hw = &adapter->hw;
1151822f52e7SVinicius Costa Gomes u32 ctrl;
1152822f52e7SVinicius Costa Gomes
1153822f52e7SVinicius Costa Gomes ctrl = rd32(IGC_PTM_CTRL);
1154822f52e7SVinicius Costa Gomes ctrl &= ~IGC_PTM_CTRL_EN;
1155822f52e7SVinicius Costa Gomes
1156822f52e7SVinicius Costa Gomes wr32(IGC_PTM_CTRL, ctrl);
1157822f52e7SVinicius Costa Gomes }
1158822f52e7SVinicius Costa Gomes
11595f295805SVinicius Costa Gomes /**
11605f295805SVinicius Costa Gomes * igc_ptp_suspend - Disable PTP work items and prepare for suspend
11615f295805SVinicius Costa Gomes * @adapter: Board private structure
11625f295805SVinicius Costa Gomes *
11635f295805SVinicius Costa Gomes * This function stops the overflow check work and PTP Tx timestamp work, and
11645f295805SVinicius Costa Gomes * will prepare the device for OS suspend.
11655f295805SVinicius Costa Gomes */
igc_ptp_suspend(struct igc_adapter * adapter)1166a5136f76SSasha Neftin void igc_ptp_suspend(struct igc_adapter *adapter)
11675f295805SVinicius Costa Gomes {
11685f295805SVinicius Costa Gomes if (!(adapter->ptp_flags & IGC_PTP_ENABLED))
11695f295805SVinicius Costa Gomes return;
11705f295805SVinicius Costa Gomes
1171ce58c7ccSVinicius Costa Gomes igc_ptp_clear_tx_tstamp(adapter);
1172b03c49cdSVinicius Costa Gomes
1173822f52e7SVinicius Costa Gomes if (pci_device_is_present(adapter->pdev)) {
1174b03c49cdSVinicius Costa Gomes igc_ptp_time_save(adapter);
1175822f52e7SVinicius Costa Gomes igc_ptm_stop(adapter);
1176822f52e7SVinicius Costa Gomes }
11775f295805SVinicius Costa Gomes }
11785f295805SVinicius Costa Gomes
11795f295805SVinicius Costa Gomes /**
11805f295805SVinicius Costa Gomes * igc_ptp_stop - Disable PTP device and stop the overflow check.
11815f295805SVinicius Costa Gomes * @adapter: Board private structure.
11825f295805SVinicius Costa Gomes *
11835f295805SVinicius Costa Gomes * This function stops the PTP support and cancels the delayed work.
11845f295805SVinicius Costa Gomes **/
igc_ptp_stop(struct igc_adapter * adapter)11855f295805SVinicius Costa Gomes void igc_ptp_stop(struct igc_adapter *adapter)
11865f295805SVinicius Costa Gomes {
11875f295805SVinicius Costa Gomes igc_ptp_suspend(adapter);
11885f295805SVinicius Costa Gomes
11895f295805SVinicius Costa Gomes if (adapter->ptp_clock) {
11905f295805SVinicius Costa Gomes ptp_clock_unregister(adapter->ptp_clock);
1191916a3c65SAndre Guedes netdev_info(adapter->netdev, "PHC removed\n");
11925f295805SVinicius Costa Gomes adapter->ptp_flags &= ~IGC_PTP_ENABLED;
11935f295805SVinicius Costa Gomes }
11945f295805SVinicius Costa Gomes }
11955f295805SVinicius Costa Gomes
11965f295805SVinicius Costa Gomes /**
11975f295805SVinicius Costa Gomes * igc_ptp_reset - Re-enable the adapter for PTP following a reset.
11985f295805SVinicius Costa Gomes * @adapter: Board private structure.
11995f295805SVinicius Costa Gomes *
12005f295805SVinicius Costa Gomes * This function handles the reset work required to re-enable the PTP device.
12015f295805SVinicius Costa Gomes **/
igc_ptp_reset(struct igc_adapter * adapter)12025f295805SVinicius Costa Gomes void igc_ptp_reset(struct igc_adapter *adapter)
12035f295805SVinicius Costa Gomes {
12045f295805SVinicius Costa Gomes struct igc_hw *hw = &adapter->hw;
1205a90ec848SVinicius Costa Gomes u32 cycle_ctrl, ctrl;
12065f295805SVinicius Costa Gomes unsigned long flags;
1207a90ec848SVinicius Costa Gomes u32 timadj;
12085f295805SVinicius Costa Gomes
12095f295805SVinicius Costa Gomes /* reset the tstamp_config */
12105f295805SVinicius Costa Gomes igc_ptp_set_timestamp_mode(adapter, &adapter->tstamp_config);
12115f295805SVinicius Costa Gomes
12125f295805SVinicius Costa Gomes spin_lock_irqsave(&adapter->tmreg_lock, flags);
12135f295805SVinicius Costa Gomes
12145f295805SVinicius Costa Gomes switch (adapter->hw.mac.type) {
12155f295805SVinicius Costa Gomes case igc_i225:
1216a90ec848SVinicius Costa Gomes timadj = rd32(IGC_TIMADJ);
1217a90ec848SVinicius Costa Gomes timadj |= IGC_TIMADJ_ADJUST_METH;
1218a90ec848SVinicius Costa Gomes wr32(IGC_TIMADJ, timadj);
1219a90ec848SVinicius Costa Gomes
12205f295805SVinicius Costa Gomes wr32(IGC_TSAUXC, 0x0);
12215f295805SVinicius Costa Gomes wr32(IGC_TSSDP, 0x0);
122264433e5bSEderson de Souza wr32(IGC_TSIM,
122364433e5bSEderson de Souza IGC_TSICR_INTERRUPTS |
122464433e5bSEderson de Souza (adapter->pps_sys_wrap_on ? IGC_TSICR_SYS_WRAP : 0));
12255f295805SVinicius Costa Gomes wr32(IGC_IMS, IGC_IMS_TS);
1226a90ec848SVinicius Costa Gomes
1227a90ec848SVinicius Costa Gomes if (!igc_is_crosststamp_supported(adapter))
1228a90ec848SVinicius Costa Gomes break;
1229a90ec848SVinicius Costa Gomes
1230a90ec848SVinicius Costa Gomes wr32(IGC_PCIE_DIG_DELAY, IGC_PCIE_DIG_DELAY_DEFAULT);
1231a90ec848SVinicius Costa Gomes wr32(IGC_PCIE_PHY_DELAY, IGC_PCIE_PHY_DELAY_DEFAULT);
1232a90ec848SVinicius Costa Gomes
1233a90ec848SVinicius Costa Gomes cycle_ctrl = IGC_PTM_CYCLE_CTRL_CYC_TIME(IGC_PTM_CYC_TIME_DEFAULT);
1234a90ec848SVinicius Costa Gomes
1235a90ec848SVinicius Costa Gomes wr32(IGC_PTM_CYCLE_CTRL, cycle_ctrl);
1236a90ec848SVinicius Costa Gomes
1237a90ec848SVinicius Costa Gomes ctrl = IGC_PTM_CTRL_EN |
1238a90ec848SVinicius Costa Gomes IGC_PTM_CTRL_START_NOW |
1239a90ec848SVinicius Costa Gomes IGC_PTM_CTRL_SHRT_CYC(IGC_PTM_SHORT_CYC_DEFAULT) |
1240a90ec848SVinicius Costa Gomes IGC_PTM_CTRL_PTM_TO(IGC_PTM_TIMEOUT_DEFAULT) |
1241a90ec848SVinicius Costa Gomes IGC_PTM_CTRL_TRIG;
1242a90ec848SVinicius Costa Gomes
1243a90ec848SVinicius Costa Gomes wr32(IGC_PTM_CTRL, ctrl);
1244a90ec848SVinicius Costa Gomes
1245a90ec848SVinicius Costa Gomes /* Force the first cycle to run. */
1246a90ec848SVinicius Costa Gomes wr32(IGC_PTM_STAT, IGC_PTM_STAT_VALID);
1247a90ec848SVinicius Costa Gomes
12485f295805SVinicius Costa Gomes break;
12495f295805SVinicius Costa Gomes default:
12505f295805SVinicius Costa Gomes /* No work to do. */
12515f295805SVinicius Costa Gomes goto out;
12525f295805SVinicius Costa Gomes }
12535f295805SVinicius Costa Gomes
12545f295805SVinicius Costa Gomes /* Re-initialize the timer. */
12555f295805SVinicius Costa Gomes if (hw->mac.type == igc_i225) {
1256b03c49cdSVinicius Costa Gomes igc_ptp_time_restore(adapter);
12575f295805SVinicius Costa Gomes } else {
12585f295805SVinicius Costa Gomes timecounter_init(&adapter->tc, &adapter->cc,
12595f295805SVinicius Costa Gomes ktime_to_ns(ktime_get_real()));
12605f295805SVinicius Costa Gomes }
12615f295805SVinicius Costa Gomes out:
12625f295805SVinicius Costa Gomes spin_unlock_irqrestore(&adapter->tmreg_lock, flags);
12635f295805SVinicius Costa Gomes
12645f295805SVinicius Costa Gomes wrfl();
12655f295805SVinicius Costa Gomes }
1266