fp@2585: /******************************************************************************* fp@2585: fp@2585: Intel PRO/1000 Linux driver fp@2585: Copyright(c) 1999 - 2013 Intel Corporation. fp@2585: fp@2585: This program is free software; you can redistribute it and/or modify it fp@2585: under the terms and conditions of the GNU General Public License, fp@2585: version 2, as published by the Free Software Foundation. fp@2585: fp@2585: This program is distributed in the hope it will be useful, but WITHOUT fp@2585: ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or fp@2585: FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for fp@2585: more details. fp@2585: fp@2585: You should have received a copy of the GNU General Public License along with fp@2585: this program; if not, write to the Free Software Foundation, Inc., fp@2585: 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA. fp@2585: fp@2585: The full GNU General Public License is included in this distribution in fp@2585: the file called "COPYING". fp@2585: fp@2585: Contact Information: fp@2585: Linux NICS fp@2585: e1000-devel Mailing List fp@2585: Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497 fp@2585: fp@2585: *******************************************************************************/ fp@2585: fp@2585: /* PTP 1588 Hardware Clock (PHC) fp@2585: * Derived from PTP Hardware Clock driver for Intel 82576 and 82580 (igb) fp@2585: * Copyright (C) 2011 Richard Cochran fp@2585: */ fp@2585: fp@2585: #include "e1000.h" fp@2585: fp@2585: /** fp@2585: * e1000e_phc_adjfreq - adjust the frequency of the hardware clock fp@2585: * @ptp: ptp clock structure fp@2585: * @delta: Desired frequency change in parts per billion fp@2585: * fp@2585: * Adjust the frequency of the PHC cycle counter by the indicated delta from fp@2585: * the base frequency. fp@2585: **/ fp@2585: static int e1000e_phc_adjfreq(struct ptp_clock_info *ptp, s32 delta) fp@2585: { fp@2585: struct e1000_adapter *adapter = container_of(ptp, struct e1000_adapter, fp@2585: ptp_clock_info); fp@2585: struct e1000_hw *hw = &adapter->hw; fp@2585: bool neg_adj = false; fp@2585: u64 adjustment; fp@2585: u32 timinca, incvalue; fp@2585: s32 ret_val; fp@2585: fp@2585: if ((delta > ptp->max_adj) || (delta <= -1000000000)) fp@2585: return -EINVAL; fp@2585: fp@2585: if (delta < 0) { fp@2585: neg_adj = true; fp@2585: delta = -delta; fp@2585: } fp@2585: fp@2585: /* Get the System Time Register SYSTIM base frequency */ fp@2585: ret_val = e1000e_get_base_timinca(adapter, &timinca); fp@2585: if (ret_val) fp@2585: return ret_val; fp@2585: fp@2585: incvalue = timinca & E1000_TIMINCA_INCVALUE_MASK; fp@2585: fp@2585: adjustment = incvalue; fp@2585: adjustment *= delta; fp@2585: adjustment = div_u64(adjustment, 1000000000); fp@2585: fp@2585: incvalue = neg_adj ? (incvalue - adjustment) : (incvalue + adjustment); fp@2585: fp@2585: timinca &= ~E1000_TIMINCA_INCVALUE_MASK; fp@2585: timinca |= incvalue; fp@2585: fp@2585: ew32(TIMINCA, timinca); fp@2585: fp@2585: return 0; fp@2585: } fp@2585: fp@2585: /** fp@2585: * e1000e_phc_adjtime - Shift the time of the hardware clock fp@2585: * @ptp: ptp clock structure fp@2585: * @delta: Desired change in nanoseconds fp@2585: * fp@2585: * Adjust the timer by resetting the timecounter structure. fp@2585: **/ fp@2585: static int e1000e_phc_adjtime(struct ptp_clock_info *ptp, s64 delta) fp@2585: { fp@2585: struct e1000_adapter *adapter = container_of(ptp, struct e1000_adapter, fp@2585: ptp_clock_info); fp@2585: unsigned long flags; fp@2585: s64 now; fp@2585: fp@2585: spin_lock_irqsave(&adapter->systim_lock, flags); fp@2585: now = timecounter_read(&adapter->tc); fp@2585: now += delta; fp@2585: timecounter_init(&adapter->tc, &adapter->cc, now); fp@2585: spin_unlock_irqrestore(&adapter->systim_lock, flags); fp@2585: fp@2585: return 0; fp@2585: } fp@2585: fp@2585: /** fp@2585: * e1000e_phc_gettime - Reads the current time from the hardware clock fp@2585: * @ptp: ptp clock structure fp@2585: * @ts: timespec structure to hold the current time value fp@2585: * fp@2585: * Read the timecounter and return the correct value in ns after converting fp@2585: * it into a struct timespec. fp@2585: **/ fp@2585: static int e1000e_phc_gettime(struct ptp_clock_info *ptp, struct timespec *ts) fp@2585: { fp@2585: struct e1000_adapter *adapter = container_of(ptp, struct e1000_adapter, fp@2585: ptp_clock_info); fp@2585: unsigned long flags; fp@2585: u32 remainder; fp@2585: u64 ns; fp@2585: fp@2585: spin_lock_irqsave(&adapter->systim_lock, flags); fp@2585: ns = timecounter_read(&adapter->tc); fp@2585: spin_unlock_irqrestore(&adapter->systim_lock, flags); fp@2585: fp@2585: ts->tv_sec = div_u64_rem(ns, NSEC_PER_SEC, &remainder); fp@2585: ts->tv_nsec = remainder; fp@2585: fp@2585: return 0; fp@2585: } fp@2585: fp@2585: /** fp@2585: * e1000e_phc_settime - Set the current time on the hardware clock fp@2585: * @ptp: ptp clock structure fp@2585: * @ts: timespec containing the new time for the cycle counter fp@2585: * fp@2585: * Reset the timecounter to use a new base value instead of the kernel fp@2585: * wall timer value. fp@2585: **/ fp@2585: static int e1000e_phc_settime(struct ptp_clock_info *ptp, fp@2585: const struct timespec *ts) fp@2585: { fp@2585: struct e1000_adapter *adapter = container_of(ptp, struct e1000_adapter, fp@2585: ptp_clock_info); fp@2585: unsigned long flags; fp@2585: u64 ns; fp@2585: fp@2585: ns = timespec_to_ns(ts); fp@2585: fp@2585: /* reset the timecounter */ fp@2585: spin_lock_irqsave(&adapter->systim_lock, flags); fp@2585: timecounter_init(&adapter->tc, &adapter->cc, ns); fp@2585: spin_unlock_irqrestore(&adapter->systim_lock, flags); fp@2585: fp@2585: return 0; fp@2585: } fp@2585: fp@2585: /** fp@2585: * e1000e_phc_enable - enable or disable an ancillary feature fp@2585: * @ptp: ptp clock structure fp@2585: * @request: Desired resource to enable or disable fp@2585: * @on: Caller passes one to enable or zero to disable fp@2585: * fp@2585: * Enable (or disable) ancillary features of the PHC subsystem. fp@2585: * Currently, no ancillary features are supported. fp@2585: **/ fp@2585: static int e1000e_phc_enable(struct ptp_clock_info __always_unused *ptp, fp@2585: struct ptp_clock_request __always_unused *request, fp@2585: int __always_unused on) fp@2585: { fp@2585: return -EOPNOTSUPP; fp@2585: } fp@2585: fp@2585: static void e1000e_systim_overflow_work(struct work_struct *work) fp@2585: { fp@2585: struct e1000_adapter *adapter = container_of(work, struct e1000_adapter, fp@2585: systim_overflow_work.work); fp@2585: struct e1000_hw *hw = &adapter->hw; fp@2585: struct timespec ts; fp@2585: fp@2585: adapter->ptp_clock_info.gettime(&adapter->ptp_clock_info, &ts); fp@2585: fp@2585: e_dbg("SYSTIM overflow check at %ld.%09lu\n", ts.tv_sec, ts.tv_nsec); fp@2585: fp@2585: schedule_delayed_work(&adapter->systim_overflow_work, fp@2585: E1000_SYSTIM_OVERFLOW_PERIOD); fp@2585: } fp@2585: fp@2585: static const struct ptp_clock_info e1000e_ptp_clock_info = { fp@2585: .owner = THIS_MODULE, fp@2585: .n_alarm = 0, fp@2585: .n_ext_ts = 0, fp@2585: .n_per_out = 0, fp@2585: .pps = 0, fp@2585: .adjfreq = e1000e_phc_adjfreq, fp@2585: .adjtime = e1000e_phc_adjtime, fp@2585: .gettime = e1000e_phc_gettime, fp@2585: .settime = e1000e_phc_settime, fp@2585: .enable = e1000e_phc_enable, fp@2585: }; fp@2585: fp@2585: /** fp@2585: * e1000e_ptp_init - initialize PTP for devices which support it fp@2585: * @adapter: board private structure fp@2585: * fp@2585: * This function performs the required steps for enabling PTP support. fp@2585: * If PTP support has already been loaded it simply calls the cyclecounter fp@2585: * init routine and exits. fp@2585: **/ fp@2585: void e1000e_ptp_init(struct e1000_adapter *adapter) fp@2585: { fp@2585: struct e1000_hw *hw = &adapter->hw; fp@2585: fp@2585: adapter->ptp_clock = NULL; fp@2585: fp@2585: if (!(adapter->flags & FLAG_HAS_HW_TIMESTAMP)) fp@2585: return; fp@2585: fp@2585: adapter->ptp_clock_info = e1000e_ptp_clock_info; fp@2585: fp@2585: snprintf(adapter->ptp_clock_info.name, fp@2585: sizeof(adapter->ptp_clock_info.name), "%pm", fp@2585: adapter->netdev->perm_addr); fp@2585: fp@2585: switch (hw->mac.type) { fp@2585: case e1000_pch2lan: fp@2585: case e1000_pch_lpt: fp@2585: if ((hw->mac.type != e1000_pch_lpt) || fp@2585: (er32(TSYNCRXCTL) & E1000_TSYNCRXCTL_SYSCFI)) { fp@2585: adapter->ptp_clock_info.max_adj = 24000000 - 1; fp@2585: break; fp@2585: } fp@2585: /* fall-through */ fp@2585: case e1000_82574: fp@2585: case e1000_82583: fp@2585: adapter->ptp_clock_info.max_adj = 600000000 - 1; fp@2585: break; fp@2585: default: fp@2585: break; fp@2585: } fp@2585: fp@2585: INIT_DELAYED_WORK(&adapter->systim_overflow_work, fp@2585: e1000e_systim_overflow_work); fp@2585: fp@2585: schedule_delayed_work(&adapter->systim_overflow_work, fp@2585: E1000_SYSTIM_OVERFLOW_PERIOD); fp@2585: fp@2585: adapter->ptp_clock = ptp_clock_register(&adapter->ptp_clock_info, fp@2585: &adapter->pdev->dev); fp@2585: if (IS_ERR(adapter->ptp_clock)) { fp@2585: adapter->ptp_clock = NULL; fp@2585: e_err("ptp_clock_register failed\n"); fp@2585: } else { fp@2585: e_info("registered PHC clock\n"); fp@2585: } fp@2585: } fp@2585: fp@2585: /** fp@2585: * e1000e_ptp_remove - disable PTP device and stop the overflow check fp@2585: * @adapter: board private structure fp@2585: * fp@2585: * Stop the PTP support, and cancel the delayed work. fp@2585: **/ fp@2585: void e1000e_ptp_remove(struct e1000_adapter *adapter) fp@2585: { fp@2585: if (!(adapter->flags & FLAG_HAS_HW_TIMESTAMP)) fp@2585: return; fp@2585: fp@2585: cancel_delayed_work_sync(&adapter->systim_overflow_work); fp@2585: fp@2585: if (adapter->ptp_clock) { fp@2585: ptp_clock_unregister(adapter->ptp_clock); fp@2585: adapter->ptp_clock = NULL; fp@2585: e_info("removed PHC\n"); fp@2585: } fp@2585: }