/*1* AMD 10Gb Ethernet driver2*3* This file is available to you under your choice of the following two4* licenses:5*6* License 1: GPLv27*8* Copyright (c) 2014 Advanced Micro Devices, Inc.9*10* This file is free software; you may copy, redistribute and/or modify11* it under the terms of the GNU General Public License as published by12* the Free Software Foundation, either version 2 of the License, or (at13* your option) any later version.14*15* This file is distributed in the hope that it will be useful, but16* WITHOUT ANY WARRANTY; without even the implied warranty of17* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU18* General Public License for more details.19*20* You should have received a copy of the GNU General Public License21* along with this program. If not, see <http://www.gnu.org/licenses/>.22*23* This file incorporates work covered by the following copyright and24* permission notice:25* The Synopsys DWC ETHER XGMAC Software Driver and documentation26* (hereinafter "Software") is an unsupported proprietary work of Synopsys,27* Inc. unless otherwise expressly agreed to in writing between Synopsys28* and you.29*30* The Software IS NOT an item of Licensed Software or Licensed Product31* under any End User Software License Agreement or Agreement for Licensed32* Product with Synopsys or any supplement thereto. Permission is hereby33* granted, free of charge, to any person obtaining a copy of this software34* annotated with this license and the Software, to deal in the Software35* without restriction, including without limitation the rights to use,36* copy, modify, merge, publish, distribute, sublicense, and/or sell copies37* of the Software, and to permit persons to whom the Software is furnished38* to do so, subject to the following conditions:39*40* The above copyright notice and this permission notice shall be included41* in all copies or substantial portions of the Software.42*43* THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS"44* BASIS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED45* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A46* PARTICULAR PURPOSE ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS47* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR48* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF49* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS50* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN51* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)52* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF53* THE POSSIBILITY OF SUCH DAMAGE.54*55*56* License 2: Modified BSD57*58* Copyright (c) 2014 Advanced Micro Devices, Inc.59* All rights reserved.60*61* Redistribution and use in source and binary forms, with or without62* modification, are permitted provided that the following conditions are met:63* * Redistributions of source code must retain the above copyright64* notice, this list of conditions and the following disclaimer.65* * Redistributions in binary form must reproduce the above copyright66* notice, this list of conditions and the following disclaimer in the67* documentation and/or other materials provided with the distribution.68* * Neither the name of Advanced Micro Devices, Inc. nor the69* names of its contributors may be used to endorse or promote products70* derived from this software without specific prior written permission.71*72* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"73* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE74* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE75* ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY76* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES77* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;78* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND79* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT80* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF81* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.82*83* This file incorporates work covered by the following copyright and84* permission notice:85* The Synopsys DWC ETHER XGMAC Software Driver and documentation86* (hereinafter "Software") is an unsupported proprietary work of Synopsys,87* Inc. unless otherwise expressly agreed to in writing between Synopsys88* and you.89*90* The Software IS NOT an item of Licensed Software or Licensed Product91* under any End User Software License Agreement or Agreement for Licensed92* Product with Synopsys or any supplement thereto. Permission is hereby93* granted, free of charge, to any person obtaining a copy of this software94* annotated with this license and the Software, to deal in the Software95* without restriction, including without limitation the rights to use,96* copy, modify, merge, publish, distribute, sublicense, and/or sell copies97* of the Software, and to permit persons to whom the Software is furnished98* to do so, subject to the following conditions:99*100* The above copyright notice and this permission notice shall be included101* in all copies or substantial portions of the Software.102*103* THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS"104* BASIS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED105* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A106* PARTICULAR PURPOSE ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS107* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR108* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF109* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS110* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN111* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)112* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF113* THE POSSIBILITY OF SUCH DAMAGE.114*/115#include <sys/cdefs.h>116#include "xgbe.h"117118static u64 xgbe_cc_read(const struct cyclecounter *cc)119{120struct xgbe_prv_data *pdata = container_of(cc,121struct xgbe_prv_data,122tstamp_cc);123u64 nsec;124125nsec = pdata->hw_if.get_tstamp_time(pdata);126127return (nsec);128}129130static int xgbe_adjfreq(struct ptp_clock_info *info, s32 delta)131{132struct xgbe_prv_data *pdata = container_of(info,133struct xgbe_prv_data,134ptp_clock_info);135unsigned long flags;136u64 adjust;137u32 addend, diff;138unsigned int neg_adjust = 0;139140if (delta < 0) {141neg_adjust = 1;142delta = -delta;143}144145adjust = pdata->tstamp_addend;146adjust *= delta;147diff = div_u64(adjust, 1000000000UL);148149addend = (neg_adjust) ? pdata->tstamp_addend - diff :150pdata->tstamp_addend + diff;151152spin_lock_irqsave(&pdata->tstamp_lock, flags);153154pdata->hw_if.update_tstamp_addend(pdata, addend);155156spin_unlock_irqrestore(&pdata->tstamp_lock, flags);157158return (0);159}160161static int xgbe_adjtime(struct ptp_clock_info *info, s64 delta)162{163struct xgbe_prv_data *pdata = container_of(info,164struct xgbe_prv_data,165ptp_clock_info);166unsigned long flags;167168spin_lock_irqsave(&pdata->tstamp_lock, flags);169timecounter_adjtime(&pdata->tstamp_tc, delta);170spin_unlock_irqrestore(&pdata->tstamp_lock, flags);171172return (0);173}174175static int xgbe_gettime(struct ptp_clock_info *info, struct timespec64 *ts)176{177struct xgbe_prv_data *pdata = container_of(info,178struct xgbe_prv_data,179ptp_clock_info);180unsigned long flags;181u64 nsec;182183spin_lock_irqsave(&pdata->tstamp_lock, flags);184185nsec = timecounter_read(&pdata->tstamp_tc);186187spin_unlock_irqrestore(&pdata->tstamp_lock, flags);188189*ts = ns_to_timespec64(nsec);190191return (0);192}193194static int xgbe_settime(struct ptp_clock_info *info,195const struct timespec64 *ts)196{197struct xgbe_prv_data *pdata = container_of(info,198struct xgbe_prv_data,199ptp_clock_info);200unsigned long flags;201u64 nsec;202203nsec = timespec64_to_ns(ts);204205spin_lock_irqsave(&pdata->tstamp_lock, flags);206207timecounter_init(&pdata->tstamp_tc, &pdata->tstamp_cc, nsec);208209spin_unlock_irqrestore(&pdata->tstamp_lock, flags);210211return (0);212}213214static int xgbe_enable(struct ptp_clock_info *info,215void *request, int on)216{217return (-EOPNOTSUPP);218}219220void xgbe_ptp_register(struct xgbe_prv_data *pdata)221{222struct ptp_clock_info *info = &pdata->ptp_clock_info;223//struct ptp_clock *clock;224struct cyclecounter *cc = &pdata->tstamp_cc;225u64 dividend;226227snprintf(info->name, sizeof(info->name), "axgbe-ptp");228//info->owner = THIS_MODULE;229info->max_adj = pdata->ptpclk_rate;230info->adjfreq = xgbe_adjfreq;231info->adjtime = xgbe_adjtime;232info->gettime64 = xgbe_gettime;233info->settime64 = xgbe_settime;234info->enable = xgbe_enable;235#if 0236clock = ptp_clock_register(info, pdata->dev);237if (IS_ERR(clock)) {238dev_err(pdata->dev, "ptp_clock_register failed\n");239return;240}241242pdata->ptp_clock = clock;243#endif244/* Calculate the addend:245* addend = 2^32 / (PTP ref clock / 50Mhz)246* = (2^32 * 50Mhz) / PTP ref clock247*/248dividend = 50000000;249dividend <<= 32;250pdata->tstamp_addend = div_u64(dividend, pdata->ptpclk_rate);251252/* Setup the timecounter */253cc->read = xgbe_cc_read;254cc->mask = CLOCKSOURCE_MASK(64);255cc->mult = 1;256cc->shift = 0;257258timecounter_init(&pdata->tstamp_tc, &pdata->tstamp_cc,259ktime_to_ns(ktime_get_real()));260261/* Disable all timestamping to start */262XGMAC_IOWRITE(pdata, MAC_TSCR, 0);263pdata->tstamp_config.tx_type = HWTSTAMP_TX_OFF;264pdata->tstamp_config.rx_filter = HWTSTAMP_FILTER_NONE;265}266267void xgbe_ptp_unregister(struct xgbe_prv_data *pdata)268{269#if 0270if (pdata->ptp_clock)271ptp_clock_unregister(pdata->ptp_clock);272#endif273}274275276