1d339b133SRichard Cochran /* 2d339b133SRichard Cochran * PTP Hardware Clock (PHC) driver for the Intel 82576 and 82580 3d339b133SRichard Cochran * 4d339b133SRichard Cochran * Copyright (C) 2011 Richard Cochran <richardcochran@gmail.com> 5d339b133SRichard Cochran * 6d339b133SRichard Cochran * This program is free software; you can redistribute it and/or modify 7d339b133SRichard Cochran * it under the terms of the GNU General Public License as published by 8d339b133SRichard Cochran * the Free Software Foundation; either version 2 of the License, or 9d339b133SRichard Cochran * (at your option) any later version. 10d339b133SRichard Cochran * 11d339b133SRichard Cochran * This program is distributed in the hope that it will be useful, 12d339b133SRichard Cochran * but WITHOUT ANY WARRANTY; without even the implied warranty of 13d339b133SRichard Cochran * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14d339b133SRichard Cochran * GNU General Public License for more details. 15d339b133SRichard Cochran * 16d339b133SRichard Cochran * You should have received a copy of the GNU General Public License along 17d339b133SRichard Cochran * with this program; if not, write to the Free Software Foundation, Inc., 18d339b133SRichard Cochran * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 19d339b133SRichard Cochran */ 20d339b133SRichard Cochran #include <linux/module.h> 21d339b133SRichard Cochran #include <linux/device.h> 22d339b133SRichard Cochran #include <linux/pci.h> 23d339b133SRichard Cochran 24d339b133SRichard Cochran #include "igb.h" 25d339b133SRichard Cochran 26d339b133SRichard Cochran #define INCVALUE_MASK 0x7fffffff 27d339b133SRichard Cochran #define ISGN 0x80000000 28d339b133SRichard Cochran 29d339b133SRichard Cochran /* 307ebae817SRichard Cochran * The 82580 timesync updates the system timer every 8ns by 8ns, 317ebae817SRichard Cochran * and this update value cannot be reprogrammed. 327ebae817SRichard Cochran * 33d339b133SRichard Cochran * Neither the 82576 nor the 82580 offer registers wide enough to hold 34d339b133SRichard Cochran * nanoseconds time values for very long. For the 82580, SYSTIM always 35d339b133SRichard Cochran * counts nanoseconds, but the upper 24 bits are not availible. The 36d339b133SRichard Cochran * frequency is adjusted by changing the 32 bit fractional nanoseconds 37d339b133SRichard Cochran * register, TIMINCA. 38d339b133SRichard Cochran * 39d339b133SRichard Cochran * For the 82576, the SYSTIM register time unit is affect by the 40d339b133SRichard Cochran * choice of the 24 bit TININCA:IV (incvalue) field. Five bits of this 41d339b133SRichard Cochran * field are needed to provide the nominal 16 nanosecond period, 42d339b133SRichard Cochran * leaving 19 bits for fractional nanoseconds. 43d339b133SRichard Cochran * 447ebae817SRichard Cochran * We scale the NIC clock cycle by a large factor so that relatively 457ebae817SRichard Cochran * small clock corrections can be added or subtracted at each clock 467ebae817SRichard Cochran * tick. The drawbacks of a large factor are a) that the clock 477ebae817SRichard Cochran * register overflows more quickly (not such a big deal) and b) that 487ebae817SRichard Cochran * the increment per tick has to fit into 24 bits. As a result we 497ebae817SRichard Cochran * need to use a shift of 19 so we can fit a value of 16 into the 507ebae817SRichard Cochran * TIMINCA register. 517ebae817SRichard Cochran * 52d339b133SRichard Cochran * 53d339b133SRichard Cochran * SYSTIMH SYSTIML 54d339b133SRichard Cochran * +--------------+ +---+---+------+ 55d339b133SRichard Cochran * 82576 | 32 | | 8 | 5 | 19 | 56d339b133SRichard Cochran * +--------------+ +---+---+------+ 57d339b133SRichard Cochran * \________ 45 bits _______/ fract 58d339b133SRichard Cochran * 59d339b133SRichard Cochran * +----------+---+ +--------------+ 60d339b133SRichard Cochran * 82580 | 24 | 8 | | 32 | 61d339b133SRichard Cochran * +----------+---+ +--------------+ 62d339b133SRichard Cochran * reserved \______ 40 bits _____/ 63d339b133SRichard Cochran * 64d339b133SRichard Cochran * 65d339b133SRichard Cochran * The 45 bit 82576 SYSTIM overflows every 66d339b133SRichard Cochran * 2^45 * 10^-9 / 3600 = 9.77 hours. 67d339b133SRichard Cochran * 68d339b133SRichard Cochran * The 40 bit 82580 SYSTIM overflows every 69d339b133SRichard Cochran * 2^40 * 10^-9 / 60 = 18.3 minutes. 70d339b133SRichard Cochran */ 71d339b133SRichard Cochran 72a79f4f88SMatthew Vick #define IGB_SYSTIM_OVERFLOW_PERIOD (HZ * 60 * 9) 73428f1f71SMatthew Vick #define IGB_PTP_TX_TIMEOUT (HZ * 15) 74d339b133SRichard Cochran #define INCPERIOD_82576 (1 << E1000_TIMINCA_16NS_SHIFT) 75d339b133SRichard Cochran #define INCVALUE_82576_MASK ((1 << E1000_TIMINCA_16NS_SHIFT) - 1) 76d339b133SRichard Cochran #define INCVALUE_82576 (16 << IGB_82576_TSYNC_SHIFT) 77d339b133SRichard Cochran #define IGB_NBITS_82580 40 78d339b133SRichard Cochran 79d339b133SRichard Cochran /* 80d339b133SRichard Cochran * SYSTIM read access for the 82576 81d339b133SRichard Cochran */ 82d339b133SRichard Cochran 83a79f4f88SMatthew Vick static cycle_t igb_ptp_read_82576(const struct cyclecounter *cc) 84d339b133SRichard Cochran { 85d339b133SRichard Cochran struct igb_adapter *igb = container_of(cc, struct igb_adapter, cc); 86d339b133SRichard Cochran struct e1000_hw *hw = &igb->hw; 87a79f4f88SMatthew Vick u64 val; 88a79f4f88SMatthew Vick u32 lo, hi; 89d339b133SRichard Cochran 90d339b133SRichard Cochran lo = rd32(E1000_SYSTIML); 91d339b133SRichard Cochran hi = rd32(E1000_SYSTIMH); 92d339b133SRichard Cochran 93d339b133SRichard Cochran val = ((u64) hi) << 32; 94d339b133SRichard Cochran val |= lo; 95d339b133SRichard Cochran 96d339b133SRichard Cochran return val; 97d339b133SRichard Cochran } 98d339b133SRichard Cochran 99d339b133SRichard Cochran /* 100d339b133SRichard Cochran * SYSTIM read access for the 82580 101d339b133SRichard Cochran */ 102d339b133SRichard Cochran 103a79f4f88SMatthew Vick static cycle_t igb_ptp_read_82580(const struct cyclecounter *cc) 104d339b133SRichard Cochran { 105d339b133SRichard Cochran struct igb_adapter *igb = container_of(cc, struct igb_adapter, cc); 106d339b133SRichard Cochran struct e1000_hw *hw = &igb->hw; 107a79f4f88SMatthew Vick u64 val; 108a79f4f88SMatthew Vick u32 lo, hi, jk; 109d339b133SRichard Cochran 1107ebae817SRichard Cochran /* 1117ebae817SRichard Cochran * The timestamp latches on lowest register read. For the 82580 1127ebae817SRichard Cochran * the lowest register is SYSTIMR instead of SYSTIML. However we only 1137ebae817SRichard Cochran * need to provide nanosecond resolution, so we just ignore it. 1147ebae817SRichard Cochran */ 115d339b133SRichard Cochran jk = rd32(E1000_SYSTIMR); 116d339b133SRichard Cochran lo = rd32(E1000_SYSTIML); 117d339b133SRichard Cochran hi = rd32(E1000_SYSTIMH); 118d339b133SRichard Cochran 119d339b133SRichard Cochran val = ((u64) hi) << 32; 120d339b133SRichard Cochran val |= lo; 121d339b133SRichard Cochran 122d339b133SRichard Cochran return val; 123d339b133SRichard Cochran } 124d339b133SRichard Cochran 125e57b8bdbSMatthew Vick /* 126e57b8bdbSMatthew Vick * SYSTIM read access for I210/I211 127e57b8bdbSMatthew Vick */ 128e57b8bdbSMatthew Vick 129e57b8bdbSMatthew Vick static void igb_ptp_read_i210(struct igb_adapter *adapter, struct timespec *ts) 130e57b8bdbSMatthew Vick { 131e57b8bdbSMatthew Vick struct e1000_hw *hw = &adapter->hw; 132e57b8bdbSMatthew Vick u32 sec, nsec, jk; 133e57b8bdbSMatthew Vick 134e57b8bdbSMatthew Vick /* 135e57b8bdbSMatthew Vick * The timestamp latches on lowest register read. For I210/I211, the 136e57b8bdbSMatthew Vick * lowest register is SYSTIMR. Since we only need to provide nanosecond 137e57b8bdbSMatthew Vick * resolution, we can ignore it. 138e57b8bdbSMatthew Vick */ 139e57b8bdbSMatthew Vick jk = rd32(E1000_SYSTIMR); 140e57b8bdbSMatthew Vick nsec = rd32(E1000_SYSTIML); 141e57b8bdbSMatthew Vick sec = rd32(E1000_SYSTIMH); 142e57b8bdbSMatthew Vick 143e57b8bdbSMatthew Vick ts->tv_sec = sec; 144e57b8bdbSMatthew Vick ts->tv_nsec = nsec; 145e57b8bdbSMatthew Vick } 146e57b8bdbSMatthew Vick 147e57b8bdbSMatthew Vick static void igb_ptp_write_i210(struct igb_adapter *adapter, 148e57b8bdbSMatthew Vick const struct timespec *ts) 149e57b8bdbSMatthew Vick { 150e57b8bdbSMatthew Vick struct e1000_hw *hw = &adapter->hw; 151e57b8bdbSMatthew Vick 152e57b8bdbSMatthew Vick /* 153e57b8bdbSMatthew Vick * Writing the SYSTIMR register is not necessary as it only provides 154e57b8bdbSMatthew Vick * sub-nanosecond resolution. 155e57b8bdbSMatthew Vick */ 156e57b8bdbSMatthew Vick wr32(E1000_SYSTIML, ts->tv_nsec); 157e57b8bdbSMatthew Vick wr32(E1000_SYSTIMH, ts->tv_sec); 158e57b8bdbSMatthew Vick } 159e57b8bdbSMatthew Vick 160a79f4f88SMatthew Vick /** 161a79f4f88SMatthew Vick * igb_ptp_systim_to_hwtstamp - convert system time value to hw timestamp 162a79f4f88SMatthew Vick * @adapter: board private structure 163a79f4f88SMatthew Vick * @hwtstamps: timestamp structure to update 164a79f4f88SMatthew Vick * @systim: unsigned 64bit system time value. 165a79f4f88SMatthew Vick * 166a79f4f88SMatthew Vick * We need to convert the system time value stored in the RX/TXSTMP registers 167a79f4f88SMatthew Vick * into a hwtstamp which can be used by the upper level timestamping functions. 168a79f4f88SMatthew Vick * 169a79f4f88SMatthew Vick * The 'tmreg_lock' spinlock is used to protect the consistency of the 170a79f4f88SMatthew Vick * system time value. This is needed because reading the 64 bit time 171a79f4f88SMatthew Vick * value involves reading two (or three) 32 bit registers. The first 172a79f4f88SMatthew Vick * read latches the value. Ditto for writing. 173a79f4f88SMatthew Vick * 174a79f4f88SMatthew Vick * In addition, here have extended the system time with an overflow 175a79f4f88SMatthew Vick * counter in software. 176a79f4f88SMatthew Vick **/ 177a79f4f88SMatthew Vick static void igb_ptp_systim_to_hwtstamp(struct igb_adapter *adapter, 178a79f4f88SMatthew Vick struct skb_shared_hwtstamps *hwtstamps, 179a79f4f88SMatthew Vick u64 systim) 180a79f4f88SMatthew Vick { 181a79f4f88SMatthew Vick unsigned long flags; 182a79f4f88SMatthew Vick u64 ns; 183a79f4f88SMatthew Vick 184a79f4f88SMatthew Vick switch (adapter->hw.mac.type) { 185a79f4f88SMatthew Vick case e1000_82576: 186e57b8bdbSMatthew Vick case e1000_82580: 187e57b8bdbSMatthew Vick case e1000_i350: 188a79f4f88SMatthew Vick spin_lock_irqsave(&adapter->tmreg_lock, flags); 189a79f4f88SMatthew Vick 190a79f4f88SMatthew Vick ns = timecounter_cyc2time(&adapter->tc, systim); 191a79f4f88SMatthew Vick 192a79f4f88SMatthew Vick spin_unlock_irqrestore(&adapter->tmreg_lock, flags); 193a79f4f88SMatthew Vick 194a79f4f88SMatthew Vick memset(hwtstamps, 0, sizeof(*hwtstamps)); 195a79f4f88SMatthew Vick hwtstamps->hwtstamp = ns_to_ktime(ns); 196e57b8bdbSMatthew Vick break; 197e57b8bdbSMatthew Vick case e1000_i210: 198e57b8bdbSMatthew Vick case e1000_i211: 199e57b8bdbSMatthew Vick memset(hwtstamps, 0, sizeof(*hwtstamps)); 200e57b8bdbSMatthew Vick /* Upper 32 bits contain s, lower 32 bits contain ns. */ 201e57b8bdbSMatthew Vick hwtstamps->hwtstamp = ktime_set(systim >> 32, 202e57b8bdbSMatthew Vick systim & 0xFFFFFFFF); 203e57b8bdbSMatthew Vick break; 204e57b8bdbSMatthew Vick default: 205e57b8bdbSMatthew Vick break; 206e57b8bdbSMatthew Vick } 207a79f4f88SMatthew Vick } 208a79f4f88SMatthew Vick 209d339b133SRichard Cochran /* 210d339b133SRichard Cochran * PTP clock operations 211d339b133SRichard Cochran */ 212d339b133SRichard Cochran 213a79f4f88SMatthew Vick static int igb_ptp_adjfreq_82576(struct ptp_clock_info *ptp, s32 ppb) 214d339b133SRichard Cochran { 215a79f4f88SMatthew Vick struct igb_adapter *igb = container_of(ptp, struct igb_adapter, 216a79f4f88SMatthew Vick ptp_caps); 217a79f4f88SMatthew Vick struct e1000_hw *hw = &igb->hw; 218a79f4f88SMatthew Vick int neg_adj = 0; 219d339b133SRichard Cochran u64 rate; 220d339b133SRichard Cochran u32 incvalue; 221d339b133SRichard Cochran 222d339b133SRichard Cochran if (ppb < 0) { 223d339b133SRichard Cochran neg_adj = 1; 224d339b133SRichard Cochran ppb = -ppb; 225d339b133SRichard Cochran } 226d339b133SRichard Cochran rate = ppb; 227d339b133SRichard Cochran rate <<= 14; 228d339b133SRichard Cochran rate = div_u64(rate, 1953125); 229d339b133SRichard Cochran 230d339b133SRichard Cochran incvalue = 16 << IGB_82576_TSYNC_SHIFT; 231d339b133SRichard Cochran 232d339b133SRichard Cochran if (neg_adj) 233d339b133SRichard Cochran incvalue -= rate; 234d339b133SRichard Cochran else 235d339b133SRichard Cochran incvalue += rate; 236d339b133SRichard Cochran 237d339b133SRichard Cochran wr32(E1000_TIMINCA, INCPERIOD_82576 | (incvalue & INCVALUE_82576_MASK)); 238d339b133SRichard Cochran 239d339b133SRichard Cochran return 0; 240d339b133SRichard Cochran } 241d339b133SRichard Cochran 242a79f4f88SMatthew Vick static int igb_ptp_adjfreq_82580(struct ptp_clock_info *ptp, s32 ppb) 243d339b133SRichard Cochran { 244a79f4f88SMatthew Vick struct igb_adapter *igb = container_of(ptp, struct igb_adapter, 245a79f4f88SMatthew Vick ptp_caps); 246a79f4f88SMatthew Vick struct e1000_hw *hw = &igb->hw; 247a79f4f88SMatthew Vick int neg_adj = 0; 248d339b133SRichard Cochran u64 rate; 249d339b133SRichard Cochran u32 inca; 250d339b133SRichard Cochran 251d339b133SRichard Cochran if (ppb < 0) { 252d339b133SRichard Cochran neg_adj = 1; 253d339b133SRichard Cochran ppb = -ppb; 254d339b133SRichard Cochran } 255d339b133SRichard Cochran rate = ppb; 256d339b133SRichard Cochran rate <<= 26; 257d339b133SRichard Cochran rate = div_u64(rate, 1953125); 258d339b133SRichard Cochran 259d339b133SRichard Cochran inca = rate & INCVALUE_MASK; 260d339b133SRichard Cochran if (neg_adj) 261d339b133SRichard Cochran inca |= ISGN; 262d339b133SRichard Cochran 263d339b133SRichard Cochran wr32(E1000_TIMINCA, inca); 264d339b133SRichard Cochran 265d339b133SRichard Cochran return 0; 266d339b133SRichard Cochran } 267d339b133SRichard Cochran 268e57b8bdbSMatthew Vick static int igb_ptp_adjtime_82576(struct ptp_clock_info *ptp, s64 delta) 269d339b133SRichard Cochran { 270a79f4f88SMatthew Vick struct igb_adapter *igb = container_of(ptp, struct igb_adapter, 271a79f4f88SMatthew Vick ptp_caps); 272d339b133SRichard Cochran unsigned long flags; 273a79f4f88SMatthew Vick s64 now; 274d339b133SRichard Cochran 275d339b133SRichard Cochran spin_lock_irqsave(&igb->tmreg_lock, flags); 276d339b133SRichard Cochran 277d339b133SRichard Cochran now = timecounter_read(&igb->tc); 278d339b133SRichard Cochran now += delta; 279d339b133SRichard Cochran timecounter_init(&igb->tc, &igb->cc, now); 280d339b133SRichard Cochran 281d339b133SRichard Cochran spin_unlock_irqrestore(&igb->tmreg_lock, flags); 282d339b133SRichard Cochran 283d339b133SRichard Cochran return 0; 284d339b133SRichard Cochran } 285d339b133SRichard Cochran 286e57b8bdbSMatthew Vick static int igb_ptp_adjtime_i210(struct ptp_clock_info *ptp, s64 delta) 287e57b8bdbSMatthew Vick { 288e57b8bdbSMatthew Vick struct igb_adapter *igb = container_of(ptp, struct igb_adapter, 289e57b8bdbSMatthew Vick ptp_caps); 290e57b8bdbSMatthew Vick unsigned long flags; 291e57b8bdbSMatthew Vick struct timespec now, then = ns_to_timespec(delta); 292e57b8bdbSMatthew Vick 293e57b8bdbSMatthew Vick spin_lock_irqsave(&igb->tmreg_lock, flags); 294e57b8bdbSMatthew Vick 295e57b8bdbSMatthew Vick igb_ptp_read_i210(igb, &now); 296e57b8bdbSMatthew Vick now = timespec_add(now, then); 297e57b8bdbSMatthew Vick igb_ptp_write_i210(igb, (const struct timespec *)&now); 298e57b8bdbSMatthew Vick 299e57b8bdbSMatthew Vick spin_unlock_irqrestore(&igb->tmreg_lock, flags); 300e57b8bdbSMatthew Vick 301e57b8bdbSMatthew Vick return 0; 302e57b8bdbSMatthew Vick } 303e57b8bdbSMatthew Vick 304e57b8bdbSMatthew Vick static int igb_ptp_gettime_82576(struct ptp_clock_info *ptp, 305e57b8bdbSMatthew Vick struct timespec *ts) 306d339b133SRichard Cochran { 307a79f4f88SMatthew Vick struct igb_adapter *igb = container_of(ptp, struct igb_adapter, 308a79f4f88SMatthew Vick ptp_caps); 309a79f4f88SMatthew Vick unsigned long flags; 310d339b133SRichard Cochran u64 ns; 311d339b133SRichard Cochran u32 remainder; 312d339b133SRichard Cochran 313d339b133SRichard Cochran spin_lock_irqsave(&igb->tmreg_lock, flags); 314d339b133SRichard Cochran 315d339b133SRichard Cochran ns = timecounter_read(&igb->tc); 316d339b133SRichard Cochran 317d339b133SRichard Cochran spin_unlock_irqrestore(&igb->tmreg_lock, flags); 318d339b133SRichard Cochran 319d339b133SRichard Cochran ts->tv_sec = div_u64_rem(ns, 1000000000, &remainder); 320d339b133SRichard Cochran ts->tv_nsec = remainder; 321d339b133SRichard Cochran 322d339b133SRichard Cochran return 0; 323d339b133SRichard Cochran } 324d339b133SRichard Cochran 325e57b8bdbSMatthew Vick static int igb_ptp_gettime_i210(struct ptp_clock_info *ptp, 326e57b8bdbSMatthew Vick struct timespec *ts) 327e57b8bdbSMatthew Vick { 328e57b8bdbSMatthew Vick struct igb_adapter *igb = container_of(ptp, struct igb_adapter, 329e57b8bdbSMatthew Vick ptp_caps); 330e57b8bdbSMatthew Vick unsigned long flags; 331e57b8bdbSMatthew Vick 332e57b8bdbSMatthew Vick spin_lock_irqsave(&igb->tmreg_lock, flags); 333e57b8bdbSMatthew Vick 334e57b8bdbSMatthew Vick igb_ptp_read_i210(igb, ts); 335e57b8bdbSMatthew Vick 336e57b8bdbSMatthew Vick spin_unlock_irqrestore(&igb->tmreg_lock, flags); 337e57b8bdbSMatthew Vick 338e57b8bdbSMatthew Vick return 0; 339e57b8bdbSMatthew Vick } 340e57b8bdbSMatthew Vick 341e57b8bdbSMatthew Vick static int igb_ptp_settime_82576(struct ptp_clock_info *ptp, 342a79f4f88SMatthew Vick const struct timespec *ts) 343d339b133SRichard Cochran { 344a79f4f88SMatthew Vick struct igb_adapter *igb = container_of(ptp, struct igb_adapter, 345a79f4f88SMatthew Vick ptp_caps); 346d339b133SRichard Cochran unsigned long flags; 347a79f4f88SMatthew Vick u64 ns; 348d339b133SRichard Cochran 349d339b133SRichard Cochran ns = ts->tv_sec * 1000000000ULL; 350d339b133SRichard Cochran ns += ts->tv_nsec; 351d339b133SRichard Cochran 352d339b133SRichard Cochran spin_lock_irqsave(&igb->tmreg_lock, flags); 353d339b133SRichard Cochran 354d339b133SRichard Cochran timecounter_init(&igb->tc, &igb->cc, ns); 355d339b133SRichard Cochran 356d339b133SRichard Cochran spin_unlock_irqrestore(&igb->tmreg_lock, flags); 357d339b133SRichard Cochran 358d339b133SRichard Cochran return 0; 359d339b133SRichard Cochran } 360d339b133SRichard Cochran 361e57b8bdbSMatthew Vick static int igb_ptp_settime_i210(struct ptp_clock_info *ptp, 362e57b8bdbSMatthew Vick const struct timespec *ts) 363e57b8bdbSMatthew Vick { 364e57b8bdbSMatthew Vick struct igb_adapter *igb = container_of(ptp, struct igb_adapter, 365e57b8bdbSMatthew Vick ptp_caps); 366e57b8bdbSMatthew Vick unsigned long flags; 367e57b8bdbSMatthew Vick 368e57b8bdbSMatthew Vick spin_lock_irqsave(&igb->tmreg_lock, flags); 369e57b8bdbSMatthew Vick 370e57b8bdbSMatthew Vick igb_ptp_write_i210(igb, ts); 371e57b8bdbSMatthew Vick 372e57b8bdbSMatthew Vick spin_unlock_irqrestore(&igb->tmreg_lock, flags); 373e57b8bdbSMatthew Vick 374e57b8bdbSMatthew Vick return 0; 375e57b8bdbSMatthew Vick } 376e57b8bdbSMatthew Vick 377a79f4f88SMatthew Vick static int igb_ptp_enable(struct ptp_clock_info *ptp, 378d339b133SRichard Cochran struct ptp_clock_request *rq, int on) 379d339b133SRichard Cochran { 380d339b133SRichard Cochran return -EOPNOTSUPP; 381d339b133SRichard Cochran } 382d339b133SRichard Cochran 3831f6e8178SMatthew Vick /** 3841f6e8178SMatthew Vick * igb_ptp_tx_work 3851f6e8178SMatthew Vick * @work: pointer to work struct 3861f6e8178SMatthew Vick * 3871f6e8178SMatthew Vick * This work function polls the TSYNCTXCTL valid bit to determine when a 3881f6e8178SMatthew Vick * timestamp has been taken for the current stored skb. 3891f6e8178SMatthew Vick */ 3901f6e8178SMatthew Vick void igb_ptp_tx_work(struct work_struct *work) 3911f6e8178SMatthew Vick { 3921f6e8178SMatthew Vick struct igb_adapter *adapter = container_of(work, struct igb_adapter, 3931f6e8178SMatthew Vick ptp_tx_work); 3941f6e8178SMatthew Vick struct e1000_hw *hw = &adapter->hw; 3951f6e8178SMatthew Vick u32 tsynctxctl; 3961f6e8178SMatthew Vick 3971f6e8178SMatthew Vick if (!adapter->ptp_tx_skb) 3981f6e8178SMatthew Vick return; 3991f6e8178SMatthew Vick 400428f1f71SMatthew Vick if (time_is_before_jiffies(adapter->ptp_tx_start + 401428f1f71SMatthew Vick IGB_PTP_TX_TIMEOUT)) { 402428f1f71SMatthew Vick dev_kfree_skb_any(adapter->ptp_tx_skb); 403428f1f71SMatthew Vick adapter->ptp_tx_skb = NULL; 404428f1f71SMatthew Vick adapter->tx_hwtstamp_timeouts++; 405428f1f71SMatthew Vick dev_warn(&adapter->pdev->dev, "clearing Tx timestamp hang"); 406428f1f71SMatthew Vick return; 407428f1f71SMatthew Vick } 408428f1f71SMatthew Vick 4091f6e8178SMatthew Vick tsynctxctl = rd32(E1000_TSYNCTXCTL); 4101f6e8178SMatthew Vick if (tsynctxctl & E1000_TSYNCTXCTL_VALID) 4111f6e8178SMatthew Vick igb_ptp_tx_hwtstamp(adapter); 4121f6e8178SMatthew Vick else 4131f6e8178SMatthew Vick /* reschedule to check later */ 4141f6e8178SMatthew Vick schedule_work(&adapter->ptp_tx_work); 4151f6e8178SMatthew Vick } 4161f6e8178SMatthew Vick 417a79f4f88SMatthew Vick static void igb_ptp_overflow_check(struct work_struct *work) 418d339b133SRichard Cochran { 419d339b133SRichard Cochran struct igb_adapter *igb = 420a79f4f88SMatthew Vick container_of(work, struct igb_adapter, ptp_overflow_work.work); 421a79f4f88SMatthew Vick struct timespec ts; 422d339b133SRichard Cochran 423e57b8bdbSMatthew Vick igb->ptp_caps.gettime(&igb->ptp_caps, &ts); 424d339b133SRichard Cochran 425d339b133SRichard Cochran pr_debug("igb overflow check at %ld.%09lu\n", ts.tv_sec, ts.tv_nsec); 426d339b133SRichard Cochran 427a79f4f88SMatthew Vick schedule_delayed_work(&igb->ptp_overflow_work, 428a79f4f88SMatthew Vick IGB_SYSTIM_OVERFLOW_PERIOD); 429a79f4f88SMatthew Vick } 430a79f4f88SMatthew Vick 431a79f4f88SMatthew Vick /** 432a79f4f88SMatthew Vick * igb_ptp_tx_hwtstamp - utility function which checks for TX time stamp 4331f6e8178SMatthew Vick * @adapter: Board private structure. 434a79f4f88SMatthew Vick * 435a79f4f88SMatthew Vick * If we were asked to do hardware stamping and such a time stamp is 436a79f4f88SMatthew Vick * available, then it must have been for this skb here because we only 437a79f4f88SMatthew Vick * allow only one such packet into the queue. 438a79f4f88SMatthew Vick */ 4391f6e8178SMatthew Vick void igb_ptp_tx_hwtstamp(struct igb_adapter *adapter) 440a79f4f88SMatthew Vick { 441a79f4f88SMatthew Vick struct e1000_hw *hw = &adapter->hw; 442a79f4f88SMatthew Vick struct skb_shared_hwtstamps shhwtstamps; 443a79f4f88SMatthew Vick u64 regval; 444a79f4f88SMatthew Vick 445a79f4f88SMatthew Vick regval = rd32(E1000_TXSTMPL); 446a79f4f88SMatthew Vick regval |= (u64)rd32(E1000_TXSTMPH) << 32; 447a79f4f88SMatthew Vick 448a79f4f88SMatthew Vick igb_ptp_systim_to_hwtstamp(adapter, &shhwtstamps, regval); 4491f6e8178SMatthew Vick skb_tstamp_tx(adapter->ptp_tx_skb, &shhwtstamps); 4501f6e8178SMatthew Vick dev_kfree_skb_any(adapter->ptp_tx_skb); 4511f6e8178SMatthew Vick adapter->ptp_tx_skb = NULL; 452a79f4f88SMatthew Vick } 453a79f4f88SMatthew Vick 454b534550aSAlexander Duyck /** 455b534550aSAlexander Duyck * igb_ptp_rx_pktstamp - retrieve Rx per packet timestamp 456b534550aSAlexander Duyck * @q_vector: Pointer to interrupt specific structure 457b534550aSAlexander Duyck * @va: Pointer to address containing Rx buffer 458b534550aSAlexander Duyck * @skb: Buffer containing timestamp and packet 459b534550aSAlexander Duyck * 460b534550aSAlexander Duyck * This function is meant to retrieve a timestamp from the first buffer of an 461b534550aSAlexander Duyck * incoming frame. The value is stored in little endian format starting on 462b534550aSAlexander Duyck * byte 8. 463b534550aSAlexander Duyck */ 464b534550aSAlexander Duyck void igb_ptp_rx_pktstamp(struct igb_q_vector *q_vector, 465b534550aSAlexander Duyck unsigned char *va, 466b534550aSAlexander Duyck struct sk_buff *skb) 467b534550aSAlexander Duyck { 468ac61d515SAlexander Duyck __le64 *regval = (__le64 *)va; 469b534550aSAlexander Duyck 470b534550aSAlexander Duyck /* 471b534550aSAlexander Duyck * The timestamp is recorded in little endian format. 472b534550aSAlexander Duyck * DWORD: 0 1 2 3 473b534550aSAlexander Duyck * Field: Reserved Reserved SYSTIML SYSTIMH 474b534550aSAlexander Duyck */ 475b534550aSAlexander Duyck igb_ptp_systim_to_hwtstamp(q_vector->adapter, skb_hwtstamps(skb), 476b534550aSAlexander Duyck le64_to_cpu(regval[1])); 477b534550aSAlexander Duyck } 478b534550aSAlexander Duyck 479b534550aSAlexander Duyck /** 480b534550aSAlexander Duyck * igb_ptp_rx_rgtstamp - retrieve Rx timestamp stored in register 481b534550aSAlexander Duyck * @q_vector: Pointer to interrupt specific structure 482b534550aSAlexander Duyck * @skb: Buffer containing timestamp and packet 483b534550aSAlexander Duyck * 484b534550aSAlexander Duyck * This function is meant to retrieve a timestamp from the internal registers 485b534550aSAlexander Duyck * of the adapter and store it in the skb. 486b534550aSAlexander Duyck */ 487b534550aSAlexander Duyck void igb_ptp_rx_rgtstamp(struct igb_q_vector *q_vector, 488a79f4f88SMatthew Vick struct sk_buff *skb) 489a79f4f88SMatthew Vick { 490a79f4f88SMatthew Vick struct igb_adapter *adapter = q_vector->adapter; 491a79f4f88SMatthew Vick struct e1000_hw *hw = &adapter->hw; 492a79f4f88SMatthew Vick u64 regval; 493a79f4f88SMatthew Vick 494a79f4f88SMatthew Vick /* 495a79f4f88SMatthew Vick * If this bit is set, then the RX registers contain the time stamp. No 496a79f4f88SMatthew Vick * other packet will be time stamped until we read these registers, so 497a79f4f88SMatthew Vick * read the registers to make them available again. Because only one 498a79f4f88SMatthew Vick * packet can be time stamped at a time, we know that the register 499a79f4f88SMatthew Vick * values must belong to this one here and therefore we don't need to 500a79f4f88SMatthew Vick * compare any of the additional attributes stored for it. 501a79f4f88SMatthew Vick * 502a79f4f88SMatthew Vick * If nothing went wrong, then it should have a shared tx_flags that we 503a79f4f88SMatthew Vick * can turn into a skb_shared_hwtstamps. 504a79f4f88SMatthew Vick */ 505a79f4f88SMatthew Vick if (!(rd32(E1000_TSYNCRXCTL) & E1000_TSYNCRXCTL_VALID)) 506a79f4f88SMatthew Vick return; 507a79f4f88SMatthew Vick 508a79f4f88SMatthew Vick regval = rd32(E1000_RXSTMPL); 509a79f4f88SMatthew Vick regval |= (u64)rd32(E1000_RXSTMPH) << 32; 510a79f4f88SMatthew Vick 511a79f4f88SMatthew Vick igb_ptp_systim_to_hwtstamp(adapter, skb_hwtstamps(skb), regval); 512a79f4f88SMatthew Vick } 513a79f4f88SMatthew Vick 514a79f4f88SMatthew Vick /** 515a79f4f88SMatthew Vick * igb_ptp_hwtstamp_ioctl - control hardware time stamping 516a79f4f88SMatthew Vick * @netdev: 517a79f4f88SMatthew Vick * @ifreq: 518a79f4f88SMatthew Vick * @cmd: 519a79f4f88SMatthew Vick * 520a79f4f88SMatthew Vick * Outgoing time stamping can be enabled and disabled. Play nice and 521a79f4f88SMatthew Vick * disable it when requested, although it shouldn't case any overhead 522a79f4f88SMatthew Vick * when no packet needs it. At most one packet in the queue may be 523a79f4f88SMatthew Vick * marked for time stamping, otherwise it would be impossible to tell 524a79f4f88SMatthew Vick * for sure to which packet the hardware time stamp belongs. 525a79f4f88SMatthew Vick * 526a79f4f88SMatthew Vick * Incoming time stamping has to be configured via the hardware 527a79f4f88SMatthew Vick * filters. Not all combinations are supported, in particular event 528a79f4f88SMatthew Vick * type has to be specified. Matching the kind of event packet is 529a79f4f88SMatthew Vick * not supported, with the exception of "all V2 events regardless of 530a79f4f88SMatthew Vick * level 2 or 4". 531a79f4f88SMatthew Vick * 532a79f4f88SMatthew Vick **/ 533a79f4f88SMatthew Vick int igb_ptp_hwtstamp_ioctl(struct net_device *netdev, 534a79f4f88SMatthew Vick struct ifreq *ifr, int cmd) 535a79f4f88SMatthew Vick { 536a79f4f88SMatthew Vick struct igb_adapter *adapter = netdev_priv(netdev); 537a79f4f88SMatthew Vick struct e1000_hw *hw = &adapter->hw; 538a79f4f88SMatthew Vick struct hwtstamp_config config; 539a79f4f88SMatthew Vick u32 tsync_tx_ctl = E1000_TSYNCTXCTL_ENABLED; 540a79f4f88SMatthew Vick u32 tsync_rx_ctl = E1000_TSYNCRXCTL_ENABLED; 541a79f4f88SMatthew Vick u32 tsync_rx_cfg = 0; 542a79f4f88SMatthew Vick bool is_l4 = false; 543a79f4f88SMatthew Vick bool is_l2 = false; 544a79f4f88SMatthew Vick u32 regval; 545a79f4f88SMatthew Vick 546a79f4f88SMatthew Vick if (copy_from_user(&config, ifr->ifr_data, sizeof(config))) 547a79f4f88SMatthew Vick return -EFAULT; 548a79f4f88SMatthew Vick 549a79f4f88SMatthew Vick /* reserved for future extensions */ 550a79f4f88SMatthew Vick if (config.flags) 551a79f4f88SMatthew Vick return -EINVAL; 552a79f4f88SMatthew Vick 553a79f4f88SMatthew Vick switch (config.tx_type) { 554a79f4f88SMatthew Vick case HWTSTAMP_TX_OFF: 555a79f4f88SMatthew Vick tsync_tx_ctl = 0; 556a79f4f88SMatthew Vick case HWTSTAMP_TX_ON: 557a79f4f88SMatthew Vick break; 558a79f4f88SMatthew Vick default: 559a79f4f88SMatthew Vick return -ERANGE; 560a79f4f88SMatthew Vick } 561a79f4f88SMatthew Vick 562a79f4f88SMatthew Vick switch (config.rx_filter) { 563a79f4f88SMatthew Vick case HWTSTAMP_FILTER_NONE: 564a79f4f88SMatthew Vick tsync_rx_ctl = 0; 565a79f4f88SMatthew Vick break; 566a79f4f88SMatthew Vick case HWTSTAMP_FILTER_PTP_V1_L4_SYNC: 567a79f4f88SMatthew Vick tsync_rx_ctl |= E1000_TSYNCRXCTL_TYPE_L4_V1; 568a79f4f88SMatthew Vick tsync_rx_cfg = E1000_TSYNCRXCFG_PTP_V1_SYNC_MESSAGE; 569a79f4f88SMatthew Vick is_l4 = true; 570a79f4f88SMatthew Vick break; 571a79f4f88SMatthew Vick case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ: 572a79f4f88SMatthew Vick tsync_rx_ctl |= E1000_TSYNCRXCTL_TYPE_L4_V1; 573a79f4f88SMatthew Vick tsync_rx_cfg = E1000_TSYNCRXCFG_PTP_V1_DELAY_REQ_MESSAGE; 574a79f4f88SMatthew Vick is_l4 = true; 575a79f4f88SMatthew Vick break; 5763e961a06SMatthew Vick case HWTSTAMP_FILTER_PTP_V2_EVENT: 5773e961a06SMatthew Vick case HWTSTAMP_FILTER_PTP_V2_L2_EVENT: 5783e961a06SMatthew Vick case HWTSTAMP_FILTER_PTP_V2_L4_EVENT: 5793e961a06SMatthew Vick case HWTSTAMP_FILTER_PTP_V2_SYNC: 580a79f4f88SMatthew Vick case HWTSTAMP_FILTER_PTP_V2_L2_SYNC: 581a79f4f88SMatthew Vick case HWTSTAMP_FILTER_PTP_V2_L4_SYNC: 5823e961a06SMatthew Vick case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ: 583a79f4f88SMatthew Vick case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ: 584a79f4f88SMatthew Vick case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ: 585a79f4f88SMatthew Vick tsync_rx_ctl |= E1000_TSYNCRXCTL_TYPE_EVENT_V2; 586a79f4f88SMatthew Vick config.rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT; 587a79f4f88SMatthew Vick is_l2 = true; 588a79f4f88SMatthew Vick is_l4 = true; 589a79f4f88SMatthew Vick break; 5903e961a06SMatthew Vick case HWTSTAMP_FILTER_PTP_V1_L4_EVENT: 5913e961a06SMatthew Vick case HWTSTAMP_FILTER_ALL: 5923e961a06SMatthew Vick /* 82576 cannot timestamp all packets, which it needs to do to 5933e961a06SMatthew Vick * support both V1 Sync and Delay_Req messages 5943e961a06SMatthew Vick */ 5953e961a06SMatthew Vick if (hw->mac.type != e1000_82576) { 5963e961a06SMatthew Vick tsync_rx_ctl |= E1000_TSYNCRXCTL_TYPE_ALL; 5973e961a06SMatthew Vick config.rx_filter = HWTSTAMP_FILTER_ALL; 5983e961a06SMatthew Vick break; 5993e961a06SMatthew Vick } 6003e961a06SMatthew Vick /* fall through */ 601a79f4f88SMatthew Vick default: 6023e961a06SMatthew Vick config.rx_filter = HWTSTAMP_FILTER_NONE; 603a79f4f88SMatthew Vick return -ERANGE; 604a79f4f88SMatthew Vick } 605a79f4f88SMatthew Vick 606a79f4f88SMatthew Vick if (hw->mac.type == e1000_82575) { 607a79f4f88SMatthew Vick if (tsync_rx_ctl | tsync_tx_ctl) 608a79f4f88SMatthew Vick return -EINVAL; 609a79f4f88SMatthew Vick return 0; 610a79f4f88SMatthew Vick } 611a79f4f88SMatthew Vick 612a79f4f88SMatthew Vick /* 613a79f4f88SMatthew Vick * Per-packet timestamping only works if all packets are 614a79f4f88SMatthew Vick * timestamped, so enable timestamping in all packets as 615a79f4f88SMatthew Vick * long as one rx filter was configured. 616a79f4f88SMatthew Vick */ 617a79f4f88SMatthew Vick if ((hw->mac.type >= e1000_82580) && tsync_rx_ctl) { 618a79f4f88SMatthew Vick tsync_rx_ctl = E1000_TSYNCRXCTL_ENABLED; 619a79f4f88SMatthew Vick tsync_rx_ctl |= E1000_TSYNCRXCTL_TYPE_ALL; 6203e961a06SMatthew Vick config.rx_filter = HWTSTAMP_FILTER_ALL; 6213e961a06SMatthew Vick is_l2 = true; 6223e961a06SMatthew Vick is_l4 = true; 623e57b8bdbSMatthew Vick 624e57b8bdbSMatthew Vick if ((hw->mac.type == e1000_i210) || 625e57b8bdbSMatthew Vick (hw->mac.type == e1000_i211)) { 626e57b8bdbSMatthew Vick regval = rd32(E1000_RXPBS); 627e57b8bdbSMatthew Vick regval |= E1000_RXPBS_CFG_TS_EN; 628e57b8bdbSMatthew Vick wr32(E1000_RXPBS, regval); 629e57b8bdbSMatthew Vick } 630a79f4f88SMatthew Vick } 631a79f4f88SMatthew Vick 632a79f4f88SMatthew Vick /* enable/disable TX */ 633a79f4f88SMatthew Vick regval = rd32(E1000_TSYNCTXCTL); 634a79f4f88SMatthew Vick regval &= ~E1000_TSYNCTXCTL_ENABLED; 635a79f4f88SMatthew Vick regval |= tsync_tx_ctl; 636a79f4f88SMatthew Vick wr32(E1000_TSYNCTXCTL, regval); 637a79f4f88SMatthew Vick 638a79f4f88SMatthew Vick /* enable/disable RX */ 639a79f4f88SMatthew Vick regval = rd32(E1000_TSYNCRXCTL); 640a79f4f88SMatthew Vick regval &= ~(E1000_TSYNCRXCTL_ENABLED | E1000_TSYNCRXCTL_TYPE_MASK); 641a79f4f88SMatthew Vick regval |= tsync_rx_ctl; 642a79f4f88SMatthew Vick wr32(E1000_TSYNCRXCTL, regval); 643a79f4f88SMatthew Vick 644a79f4f88SMatthew Vick /* define which PTP packets are time stamped */ 645a79f4f88SMatthew Vick wr32(E1000_TSYNCRXCFG, tsync_rx_cfg); 646a79f4f88SMatthew Vick 647a79f4f88SMatthew Vick /* define ethertype filter for timestamped packets */ 648a79f4f88SMatthew Vick if (is_l2) 649a79f4f88SMatthew Vick wr32(E1000_ETQF(3), 650a79f4f88SMatthew Vick (E1000_ETQF_FILTER_ENABLE | /* enable filter */ 651a79f4f88SMatthew Vick E1000_ETQF_1588 | /* enable timestamping */ 652a79f4f88SMatthew Vick ETH_P_1588)); /* 1588 eth protocol type */ 653a79f4f88SMatthew Vick else 654a79f4f88SMatthew Vick wr32(E1000_ETQF(3), 0); 655a79f4f88SMatthew Vick 656a79f4f88SMatthew Vick #define PTP_PORT 319 657a79f4f88SMatthew Vick /* L4 Queue Filter[3]: filter by destination port and protocol */ 658a79f4f88SMatthew Vick if (is_l4) { 659a79f4f88SMatthew Vick u32 ftqf = (IPPROTO_UDP /* UDP */ 660a79f4f88SMatthew Vick | E1000_FTQF_VF_BP /* VF not compared */ 661a79f4f88SMatthew Vick | E1000_FTQF_1588_TIME_STAMP /* Enable Timestamping */ 662a79f4f88SMatthew Vick | E1000_FTQF_MASK); /* mask all inputs */ 663a79f4f88SMatthew Vick ftqf &= ~E1000_FTQF_MASK_PROTO_BP; /* enable protocol check */ 664a79f4f88SMatthew Vick 665a79f4f88SMatthew Vick wr32(E1000_IMIR(3), htons(PTP_PORT)); 666a79f4f88SMatthew Vick wr32(E1000_IMIREXT(3), 667a79f4f88SMatthew Vick (E1000_IMIREXT_SIZE_BP | E1000_IMIREXT_CTRL_BP)); 668a79f4f88SMatthew Vick if (hw->mac.type == e1000_82576) { 669a79f4f88SMatthew Vick /* enable source port check */ 670a79f4f88SMatthew Vick wr32(E1000_SPQF(3), htons(PTP_PORT)); 671a79f4f88SMatthew Vick ftqf &= ~E1000_FTQF_MASK_SOURCE_PORT_BP; 672a79f4f88SMatthew Vick } 673a79f4f88SMatthew Vick wr32(E1000_FTQF(3), ftqf); 674a79f4f88SMatthew Vick } else { 675a79f4f88SMatthew Vick wr32(E1000_FTQF(3), E1000_FTQF_MASK); 676a79f4f88SMatthew Vick } 677a79f4f88SMatthew Vick wrfl(); 678a79f4f88SMatthew Vick 679a79f4f88SMatthew Vick /* clear TX/RX time stamp registers, just to be sure */ 680e57b8bdbSMatthew Vick regval = rd32(E1000_TXSTMPL); 681a79f4f88SMatthew Vick regval = rd32(E1000_TXSTMPH); 682e57b8bdbSMatthew Vick regval = rd32(E1000_RXSTMPL); 683a79f4f88SMatthew Vick regval = rd32(E1000_RXSTMPH); 684a79f4f88SMatthew Vick 685a79f4f88SMatthew Vick return copy_to_user(ifr->ifr_data, &config, sizeof(config)) ? 686a79f4f88SMatthew Vick -EFAULT : 0; 687d339b133SRichard Cochran } 688d339b133SRichard Cochran 689d339b133SRichard Cochran void igb_ptp_init(struct igb_adapter *adapter) 690d339b133SRichard Cochran { 691d339b133SRichard Cochran struct e1000_hw *hw = &adapter->hw; 692201987e3SMatthew Vick struct net_device *netdev = adapter->netdev; 693d339b133SRichard Cochran 694d339b133SRichard Cochran switch (hw->mac.type) { 695d339b133SRichard Cochran case e1000_82576: 696201987e3SMatthew Vick snprintf(adapter->ptp_caps.name, 16, "%pm", netdev->dev_addr); 697a79f4f88SMatthew Vick adapter->ptp_caps.owner = THIS_MODULE; 698a79f4f88SMatthew Vick adapter->ptp_caps.max_adj = 1000000000; 699a79f4f88SMatthew Vick adapter->ptp_caps.n_ext_ts = 0; 700a79f4f88SMatthew Vick adapter->ptp_caps.pps = 0; 701a79f4f88SMatthew Vick adapter->ptp_caps.adjfreq = igb_ptp_adjfreq_82576; 702e57b8bdbSMatthew Vick adapter->ptp_caps.adjtime = igb_ptp_adjtime_82576; 703e57b8bdbSMatthew Vick adapter->ptp_caps.gettime = igb_ptp_gettime_82576; 704e57b8bdbSMatthew Vick adapter->ptp_caps.settime = igb_ptp_settime_82576; 705a79f4f88SMatthew Vick adapter->ptp_caps.enable = igb_ptp_enable; 706a79f4f88SMatthew Vick adapter->cc.read = igb_ptp_read_82576; 707d339b133SRichard Cochran adapter->cc.mask = CLOCKSOURCE_MASK(64); 708d339b133SRichard Cochran adapter->cc.mult = 1; 709d339b133SRichard Cochran adapter->cc.shift = IGB_82576_TSYNC_SHIFT; 710d339b133SRichard Cochran /* Dial the nominal frequency. */ 711d339b133SRichard Cochran wr32(E1000_TIMINCA, INCPERIOD_82576 | INCVALUE_82576); 712d339b133SRichard Cochran break; 713e57b8bdbSMatthew Vick case e1000_82580: 714e57b8bdbSMatthew Vick case e1000_i350: 715e57b8bdbSMatthew Vick snprintf(adapter->ptp_caps.name, 16, "%pm", netdev->dev_addr); 716e57b8bdbSMatthew Vick adapter->ptp_caps.owner = THIS_MODULE; 717e57b8bdbSMatthew Vick adapter->ptp_caps.max_adj = 62499999; 718e57b8bdbSMatthew Vick adapter->ptp_caps.n_ext_ts = 0; 719e57b8bdbSMatthew Vick adapter->ptp_caps.pps = 0; 720e57b8bdbSMatthew Vick adapter->ptp_caps.adjfreq = igb_ptp_adjfreq_82580; 721e57b8bdbSMatthew Vick adapter->ptp_caps.adjtime = igb_ptp_adjtime_82576; 722e57b8bdbSMatthew Vick adapter->ptp_caps.gettime = igb_ptp_gettime_82576; 723e57b8bdbSMatthew Vick adapter->ptp_caps.settime = igb_ptp_settime_82576; 724e57b8bdbSMatthew Vick adapter->ptp_caps.enable = igb_ptp_enable; 725e57b8bdbSMatthew Vick adapter->cc.read = igb_ptp_read_82580; 726e57b8bdbSMatthew Vick adapter->cc.mask = CLOCKSOURCE_MASK(IGB_NBITS_82580); 727e57b8bdbSMatthew Vick adapter->cc.mult = 1; 728e57b8bdbSMatthew Vick adapter->cc.shift = 0; 729e57b8bdbSMatthew Vick /* Enable the timer functions by clearing bit 31. */ 730e57b8bdbSMatthew Vick wr32(E1000_TSAUXC, 0x0); 731e57b8bdbSMatthew Vick break; 732e57b8bdbSMatthew Vick case e1000_i210: 733e57b8bdbSMatthew Vick case e1000_i211: 734e57b8bdbSMatthew Vick snprintf(adapter->ptp_caps.name, 16, "%pm", netdev->dev_addr); 735e57b8bdbSMatthew Vick adapter->ptp_caps.owner = THIS_MODULE; 736e57b8bdbSMatthew Vick adapter->ptp_caps.max_adj = 62499999; 737e57b8bdbSMatthew Vick adapter->ptp_caps.n_ext_ts = 0; 738e57b8bdbSMatthew Vick adapter->ptp_caps.pps = 0; 739e57b8bdbSMatthew Vick adapter->ptp_caps.adjfreq = igb_ptp_adjfreq_82580; 740e57b8bdbSMatthew Vick adapter->ptp_caps.adjtime = igb_ptp_adjtime_i210; 741e57b8bdbSMatthew Vick adapter->ptp_caps.gettime = igb_ptp_gettime_i210; 742e57b8bdbSMatthew Vick adapter->ptp_caps.settime = igb_ptp_settime_i210; 743e57b8bdbSMatthew Vick adapter->ptp_caps.enable = igb_ptp_enable; 744e57b8bdbSMatthew Vick /* Enable the timer functions by clearing bit 31. */ 745e57b8bdbSMatthew Vick wr32(E1000_TSAUXC, 0x0); 746e57b8bdbSMatthew Vick break; 747d339b133SRichard Cochran default: 748d339b133SRichard Cochran adapter->ptp_clock = NULL; 749d339b133SRichard Cochran return; 750d339b133SRichard Cochran } 751d339b133SRichard Cochran 752d339b133SRichard Cochran wrfl(); 753d339b133SRichard Cochran 754e57b8bdbSMatthew Vick spin_lock_init(&adapter->tmreg_lock); 755e57b8bdbSMatthew Vick INIT_WORK(&adapter->ptp_tx_work, igb_ptp_tx_work); 756e57b8bdbSMatthew Vick 757e57b8bdbSMatthew Vick /* Initialize the clock and overflow work for devices that need it. */ 758e57b8bdbSMatthew Vick if ((hw->mac.type == e1000_i210) || (hw->mac.type == e1000_i211)) { 759e57b8bdbSMatthew Vick struct timespec ts = ktime_to_timespec(ktime_get_real()); 760e57b8bdbSMatthew Vick 761e57b8bdbSMatthew Vick igb_ptp_settime_i210(&adapter->ptp_caps, &ts); 762e57b8bdbSMatthew Vick } else { 763d339b133SRichard Cochran timecounter_init(&adapter->tc, &adapter->cc, 764d339b133SRichard Cochran ktime_to_ns(ktime_get_real())); 765d339b133SRichard Cochran 766e57b8bdbSMatthew Vick INIT_DELAYED_WORK(&adapter->ptp_overflow_work, 767e57b8bdbSMatthew Vick igb_ptp_overflow_check); 7681f6e8178SMatthew Vick 769a79f4f88SMatthew Vick schedule_delayed_work(&adapter->ptp_overflow_work, 770a79f4f88SMatthew Vick IGB_SYSTIM_OVERFLOW_PERIOD); 771e57b8bdbSMatthew Vick } 772d339b133SRichard Cochran 7731f6e8178SMatthew Vick /* Initialize the time sync interrupts for devices that support it. */ 7741f6e8178SMatthew Vick if (hw->mac.type >= e1000_82580) { 7751f6e8178SMatthew Vick wr32(E1000_TSIM, E1000_TSIM_TXTS); 7761f6e8178SMatthew Vick wr32(E1000_IMS, E1000_IMS_TS); 7771f6e8178SMatthew Vick } 7781f6e8178SMatthew Vick 7791ef76158SRichard Cochran adapter->ptp_clock = ptp_clock_register(&adapter->ptp_caps, 7801ef76158SRichard Cochran &adapter->pdev->dev); 781d339b133SRichard Cochran if (IS_ERR(adapter->ptp_clock)) { 782d339b133SRichard Cochran adapter->ptp_clock = NULL; 783d339b133SRichard Cochran dev_err(&adapter->pdev->dev, "ptp_clock_register failed\n"); 7841f6e8178SMatthew Vick } else { 785d339b133SRichard Cochran dev_info(&adapter->pdev->dev, "added PHC on %s\n", 786d339b133SRichard Cochran adapter->netdev->name); 7871f6e8178SMatthew Vick adapter->flags |= IGB_FLAG_PTP; 7881f6e8178SMatthew Vick } 789d339b133SRichard Cochran } 790d339b133SRichard Cochran 791a79f4f88SMatthew Vick /** 792a79f4f88SMatthew Vick * igb_ptp_stop - Disable PTP device and stop the overflow check. 793a79f4f88SMatthew Vick * @adapter: Board private structure. 794a79f4f88SMatthew Vick * 795a79f4f88SMatthew Vick * This function stops the PTP support and cancels the delayed work. 796a79f4f88SMatthew Vick **/ 797a79f4f88SMatthew Vick void igb_ptp_stop(struct igb_adapter *adapter) 798d339b133SRichard Cochran { 799d3eef8c8SCarolyn Wyborny switch (adapter->hw.mac.type) { 800d3eef8c8SCarolyn Wyborny case e1000_82576: 8011f6e8178SMatthew Vick case e1000_82580: 8021f6e8178SMatthew Vick case e1000_i350: 803a79f4f88SMatthew Vick cancel_delayed_work_sync(&adapter->ptp_overflow_work); 804d3eef8c8SCarolyn Wyborny break; 8051f6e8178SMatthew Vick case e1000_i210: 8061f6e8178SMatthew Vick case e1000_i211: 8071f6e8178SMatthew Vick /* No delayed work to cancel. */ 8081f6e8178SMatthew Vick break; 809d3eef8c8SCarolyn Wyborny default: 810d3eef8c8SCarolyn Wyborny return; 811d3eef8c8SCarolyn Wyborny } 812d339b133SRichard Cochran 8131f6e8178SMatthew Vick cancel_work_sync(&adapter->ptp_tx_work); 8141f6e8178SMatthew Vick 815d339b133SRichard Cochran if (adapter->ptp_clock) { 816d339b133SRichard Cochran ptp_clock_unregister(adapter->ptp_clock); 817d339b133SRichard Cochran dev_info(&adapter->pdev->dev, "removed PHC on %s\n", 818d339b133SRichard Cochran adapter->netdev->name); 8191f6e8178SMatthew Vick adapter->flags &= ~IGB_FLAG_PTP; 820d339b133SRichard Cochran } 821d339b133SRichard Cochran } 8221f6e8178SMatthew Vick 8231f6e8178SMatthew Vick /** 8241f6e8178SMatthew Vick * igb_ptp_reset - Re-enable the adapter for PTP following a reset. 8251f6e8178SMatthew Vick * @adapter: Board private structure. 8261f6e8178SMatthew Vick * 8271f6e8178SMatthew Vick * This function handles the reset work required to re-enable the PTP device. 8281f6e8178SMatthew Vick **/ 8291f6e8178SMatthew Vick void igb_ptp_reset(struct igb_adapter *adapter) 8301f6e8178SMatthew Vick { 8311f6e8178SMatthew Vick struct e1000_hw *hw = &adapter->hw; 8321f6e8178SMatthew Vick 8331f6e8178SMatthew Vick if (!(adapter->flags & IGB_FLAG_PTP)) 8341f6e8178SMatthew Vick return; 8351f6e8178SMatthew Vick 8361f6e8178SMatthew Vick switch (adapter->hw.mac.type) { 8371f6e8178SMatthew Vick case e1000_82576: 8381f6e8178SMatthew Vick /* Dial the nominal frequency. */ 8391f6e8178SMatthew Vick wr32(E1000_TIMINCA, INCPERIOD_82576 | INCVALUE_82576); 8401f6e8178SMatthew Vick break; 8411f6e8178SMatthew Vick case e1000_82580: 8421f6e8178SMatthew Vick case e1000_i350: 8431f6e8178SMatthew Vick case e1000_i210: 8441f6e8178SMatthew Vick case e1000_i211: 8451f6e8178SMatthew Vick /* Enable the timer functions and interrupts. */ 8461f6e8178SMatthew Vick wr32(E1000_TSAUXC, 0x0); 8471f6e8178SMatthew Vick wr32(E1000_TSIM, E1000_TSIM_TXTS); 8481f6e8178SMatthew Vick wr32(E1000_IMS, E1000_IMS_TS); 8491f6e8178SMatthew Vick break; 8501f6e8178SMatthew Vick default: 8511f6e8178SMatthew Vick /* No work to do. */ 8521f6e8178SMatthew Vick return; 8531f6e8178SMatthew Vick } 8541f6e8178SMatthew Vick 855e57b8bdbSMatthew Vick /* Re-initialize the timer. */ 856e57b8bdbSMatthew Vick if ((hw->mac.type == e1000_i210) || (hw->mac.type == e1000_i211)) { 857e57b8bdbSMatthew Vick struct timespec ts = ktime_to_timespec(ktime_get_real()); 858e57b8bdbSMatthew Vick 859e57b8bdbSMatthew Vick igb_ptp_settime_i210(&adapter->ptp_caps, &ts); 860e57b8bdbSMatthew Vick } else { 8611f6e8178SMatthew Vick timecounter_init(&adapter->tc, &adapter->cc, 8621f6e8178SMatthew Vick ktime_to_ns(ktime_get_real())); 8631f6e8178SMatthew Vick } 864e57b8bdbSMatthew Vick } 865