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