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