17113afc8SEmmanuel Vadot /*
27113afc8SEmmanuel Vadot * AMD 10Gb Ethernet driver
37113afc8SEmmanuel Vadot *
47113afc8SEmmanuel Vadot * This file is available to you under your choice of the following two
57113afc8SEmmanuel Vadot * licenses:
67113afc8SEmmanuel Vadot *
77113afc8SEmmanuel Vadot * License 1: GPLv2
87113afc8SEmmanuel Vadot *
97113afc8SEmmanuel Vadot * Copyright (c) 2014 Advanced Micro Devices, Inc.
107113afc8SEmmanuel Vadot *
117113afc8SEmmanuel Vadot * This file is free software; you may copy, redistribute and/or modify
127113afc8SEmmanuel Vadot * it under the terms of the GNU General Public License as published by
137113afc8SEmmanuel Vadot * the Free Software Foundation, either version 2 of the License, or (at
147113afc8SEmmanuel Vadot * your option) any later version.
157113afc8SEmmanuel Vadot *
167113afc8SEmmanuel Vadot * This file is distributed in the hope that it will be useful, but
177113afc8SEmmanuel Vadot * WITHOUT ANY WARRANTY; without even the implied warranty of
187113afc8SEmmanuel Vadot * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
197113afc8SEmmanuel Vadot * General Public License for more details.
207113afc8SEmmanuel Vadot *
217113afc8SEmmanuel Vadot * You should have received a copy of the GNU General Public License
227113afc8SEmmanuel Vadot * along with this program. If not, see <http://www.gnu.org/licenses/>.
237113afc8SEmmanuel Vadot *
247113afc8SEmmanuel Vadot * This file incorporates work covered by the following copyright and
257113afc8SEmmanuel Vadot * permission notice:
267113afc8SEmmanuel Vadot * The Synopsys DWC ETHER XGMAC Software Driver and documentation
277113afc8SEmmanuel Vadot * (hereinafter "Software") is an unsupported proprietary work of Synopsys,
287113afc8SEmmanuel Vadot * Inc. unless otherwise expressly agreed to in writing between Synopsys
297113afc8SEmmanuel Vadot * and you.
307113afc8SEmmanuel Vadot *
317113afc8SEmmanuel Vadot * The Software IS NOT an item of Licensed Software or Licensed Product
327113afc8SEmmanuel Vadot * under any End User Software License Agreement or Agreement for Licensed
337113afc8SEmmanuel Vadot * Product with Synopsys or any supplement thereto. Permission is hereby
347113afc8SEmmanuel Vadot * granted, free of charge, to any person obtaining a copy of this software
357113afc8SEmmanuel Vadot * annotated with this license and the Software, to deal in the Software
367113afc8SEmmanuel Vadot * without restriction, including without limitation the rights to use,
377113afc8SEmmanuel Vadot * copy, modify, merge, publish, distribute, sublicense, and/or sell copies
387113afc8SEmmanuel Vadot * of the Software, and to permit persons to whom the Software is furnished
397113afc8SEmmanuel Vadot * to do so, subject to the following conditions:
407113afc8SEmmanuel Vadot *
417113afc8SEmmanuel Vadot * The above copyright notice and this permission notice shall be included
427113afc8SEmmanuel Vadot * in all copies or substantial portions of the Software.
437113afc8SEmmanuel Vadot *
447113afc8SEmmanuel Vadot * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS"
457113afc8SEmmanuel Vadot * BASIS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
467113afc8SEmmanuel Vadot * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
477113afc8SEmmanuel Vadot * PARTICULAR PURPOSE ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS
487113afc8SEmmanuel Vadot * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
497113afc8SEmmanuel Vadot * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
507113afc8SEmmanuel Vadot * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
517113afc8SEmmanuel Vadot * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
527113afc8SEmmanuel Vadot * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
537113afc8SEmmanuel Vadot * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
547113afc8SEmmanuel Vadot * THE POSSIBILITY OF SUCH DAMAGE.
557113afc8SEmmanuel Vadot *
567113afc8SEmmanuel Vadot *
577113afc8SEmmanuel Vadot * License 2: Modified BSD
587113afc8SEmmanuel Vadot *
597113afc8SEmmanuel Vadot * Copyright (c) 2014 Advanced Micro Devices, Inc.
607113afc8SEmmanuel Vadot * All rights reserved.
617113afc8SEmmanuel Vadot *
627113afc8SEmmanuel Vadot * Redistribution and use in source and binary forms, with or without
637113afc8SEmmanuel Vadot * modification, are permitted provided that the following conditions are met:
647113afc8SEmmanuel Vadot * * Redistributions of source code must retain the above copyright
657113afc8SEmmanuel Vadot * notice, this list of conditions and the following disclaimer.
667113afc8SEmmanuel Vadot * * Redistributions in binary form must reproduce the above copyright
677113afc8SEmmanuel Vadot * notice, this list of conditions and the following disclaimer in the
687113afc8SEmmanuel Vadot * documentation and/or other materials provided with the distribution.
697113afc8SEmmanuel Vadot * * Neither the name of Advanced Micro Devices, Inc. nor the
707113afc8SEmmanuel Vadot * names of its contributors may be used to endorse or promote products
717113afc8SEmmanuel Vadot * derived from this software without specific prior written permission.
727113afc8SEmmanuel Vadot *
737113afc8SEmmanuel Vadot * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
747113afc8SEmmanuel Vadot * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
757113afc8SEmmanuel Vadot * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
767113afc8SEmmanuel Vadot * ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
777113afc8SEmmanuel Vadot * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
787113afc8SEmmanuel Vadot * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
797113afc8SEmmanuel Vadot * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
807113afc8SEmmanuel Vadot * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
817113afc8SEmmanuel Vadot * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
827113afc8SEmmanuel Vadot * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
837113afc8SEmmanuel Vadot *
847113afc8SEmmanuel Vadot * This file incorporates work covered by the following copyright and
857113afc8SEmmanuel Vadot * permission notice:
867113afc8SEmmanuel Vadot * The Synopsys DWC ETHER XGMAC Software Driver and documentation
877113afc8SEmmanuel Vadot * (hereinafter "Software") is an unsupported proprietary work of Synopsys,
887113afc8SEmmanuel Vadot * Inc. unless otherwise expressly agreed to in writing between Synopsys
897113afc8SEmmanuel Vadot * and you.
907113afc8SEmmanuel Vadot *
917113afc8SEmmanuel Vadot * The Software IS NOT an item of Licensed Software or Licensed Product
927113afc8SEmmanuel Vadot * under any End User Software License Agreement or Agreement for Licensed
937113afc8SEmmanuel Vadot * Product with Synopsys or any supplement thereto. Permission is hereby
947113afc8SEmmanuel Vadot * granted, free of charge, to any person obtaining a copy of this software
957113afc8SEmmanuel Vadot * annotated with this license and the Software, to deal in the Software
967113afc8SEmmanuel Vadot * without restriction, including without limitation the rights to use,
977113afc8SEmmanuel Vadot * copy, modify, merge, publish, distribute, sublicense, and/or sell copies
987113afc8SEmmanuel Vadot * of the Software, and to permit persons to whom the Software is furnished
997113afc8SEmmanuel Vadot * to do so, subject to the following conditions:
1007113afc8SEmmanuel Vadot *
1017113afc8SEmmanuel Vadot * The above copyright notice and this permission notice shall be included
1027113afc8SEmmanuel Vadot * in all copies or substantial portions of the Software.
1037113afc8SEmmanuel Vadot *
1047113afc8SEmmanuel Vadot * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS"
1057113afc8SEmmanuel Vadot * BASIS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
1067113afc8SEmmanuel Vadot * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
1077113afc8SEmmanuel Vadot * PARTICULAR PURPOSE ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS
1087113afc8SEmmanuel Vadot * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
1097113afc8SEmmanuel Vadot * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
1107113afc8SEmmanuel Vadot * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
1117113afc8SEmmanuel Vadot * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
1127113afc8SEmmanuel Vadot * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
1137113afc8SEmmanuel Vadot * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
1147113afc8SEmmanuel Vadot * THE POSSIBILITY OF SUCH DAMAGE.
1157113afc8SEmmanuel Vadot */
1167113afc8SEmmanuel Vadot #include <sys/cdefs.h>
1177113afc8SEmmanuel Vadot #include "xgbe.h"
1187113afc8SEmmanuel Vadot
xgbe_cc_read(const struct cyclecounter * cc)1197113afc8SEmmanuel Vadot static u64 xgbe_cc_read(const struct cyclecounter *cc)
1207113afc8SEmmanuel Vadot {
1217113afc8SEmmanuel Vadot struct xgbe_prv_data *pdata = container_of(cc,
1227113afc8SEmmanuel Vadot struct xgbe_prv_data,
1237113afc8SEmmanuel Vadot tstamp_cc);
1247113afc8SEmmanuel Vadot u64 nsec;
1257113afc8SEmmanuel Vadot
1267113afc8SEmmanuel Vadot nsec = pdata->hw_if.get_tstamp_time(pdata);
1277113afc8SEmmanuel Vadot
1287113afc8SEmmanuel Vadot return (nsec);
1297113afc8SEmmanuel Vadot }
1307113afc8SEmmanuel Vadot
xgbe_adjfreq(struct ptp_clock_info * info,s32 delta)1317113afc8SEmmanuel Vadot static int xgbe_adjfreq(struct ptp_clock_info *info, s32 delta)
1327113afc8SEmmanuel Vadot {
1337113afc8SEmmanuel Vadot struct xgbe_prv_data *pdata = container_of(info,
1347113afc8SEmmanuel Vadot struct xgbe_prv_data,
1357113afc8SEmmanuel Vadot ptp_clock_info);
1367113afc8SEmmanuel Vadot unsigned long flags;
1377113afc8SEmmanuel Vadot u64 adjust;
1387113afc8SEmmanuel Vadot u32 addend, diff;
1397113afc8SEmmanuel Vadot unsigned int neg_adjust = 0;
1407113afc8SEmmanuel Vadot
1417113afc8SEmmanuel Vadot if (delta < 0) {
1427113afc8SEmmanuel Vadot neg_adjust = 1;
1437113afc8SEmmanuel Vadot delta = -delta;
1447113afc8SEmmanuel Vadot }
1457113afc8SEmmanuel Vadot
1467113afc8SEmmanuel Vadot adjust = pdata->tstamp_addend;
1477113afc8SEmmanuel Vadot adjust *= delta;
1487113afc8SEmmanuel Vadot diff = div_u64(adjust, 1000000000UL);
1497113afc8SEmmanuel Vadot
1507113afc8SEmmanuel Vadot addend = (neg_adjust) ? pdata->tstamp_addend - diff :
1517113afc8SEmmanuel Vadot pdata->tstamp_addend + diff;
1527113afc8SEmmanuel Vadot
1537113afc8SEmmanuel Vadot spin_lock_irqsave(&pdata->tstamp_lock, flags);
1547113afc8SEmmanuel Vadot
1557113afc8SEmmanuel Vadot pdata->hw_if.update_tstamp_addend(pdata, addend);
1567113afc8SEmmanuel Vadot
1577113afc8SEmmanuel Vadot spin_unlock_irqrestore(&pdata->tstamp_lock, flags);
1587113afc8SEmmanuel Vadot
1597113afc8SEmmanuel Vadot return (0);
1607113afc8SEmmanuel Vadot }
1617113afc8SEmmanuel Vadot
xgbe_adjtime(struct ptp_clock_info * info,s64 delta)1627113afc8SEmmanuel Vadot static int xgbe_adjtime(struct ptp_clock_info *info, s64 delta)
1637113afc8SEmmanuel Vadot {
1647113afc8SEmmanuel Vadot struct xgbe_prv_data *pdata = container_of(info,
1657113afc8SEmmanuel Vadot struct xgbe_prv_data,
1667113afc8SEmmanuel Vadot ptp_clock_info);
1677113afc8SEmmanuel Vadot unsigned long flags;
1687113afc8SEmmanuel Vadot
1697113afc8SEmmanuel Vadot spin_lock_irqsave(&pdata->tstamp_lock, flags);
1707113afc8SEmmanuel Vadot timecounter_adjtime(&pdata->tstamp_tc, delta);
1717113afc8SEmmanuel Vadot spin_unlock_irqrestore(&pdata->tstamp_lock, flags);
1727113afc8SEmmanuel Vadot
1737113afc8SEmmanuel Vadot return (0);
1747113afc8SEmmanuel Vadot }
1757113afc8SEmmanuel Vadot
xgbe_gettime(struct ptp_clock_info * info,struct timespec64 * ts)1767113afc8SEmmanuel Vadot static int xgbe_gettime(struct ptp_clock_info *info, struct timespec64 *ts)
1777113afc8SEmmanuel Vadot {
1787113afc8SEmmanuel Vadot struct xgbe_prv_data *pdata = container_of(info,
1797113afc8SEmmanuel Vadot struct xgbe_prv_data,
1807113afc8SEmmanuel Vadot ptp_clock_info);
1817113afc8SEmmanuel Vadot unsigned long flags;
1827113afc8SEmmanuel Vadot u64 nsec;
1837113afc8SEmmanuel Vadot
1847113afc8SEmmanuel Vadot spin_lock_irqsave(&pdata->tstamp_lock, flags);
1857113afc8SEmmanuel Vadot
1867113afc8SEmmanuel Vadot nsec = timecounter_read(&pdata->tstamp_tc);
1877113afc8SEmmanuel Vadot
1887113afc8SEmmanuel Vadot spin_unlock_irqrestore(&pdata->tstamp_lock, flags);
1897113afc8SEmmanuel Vadot
1907113afc8SEmmanuel Vadot *ts = ns_to_timespec64(nsec);
1917113afc8SEmmanuel Vadot
1927113afc8SEmmanuel Vadot return (0);
1937113afc8SEmmanuel Vadot }
1947113afc8SEmmanuel Vadot
xgbe_settime(struct ptp_clock_info * info,const struct timespec64 * ts)1957113afc8SEmmanuel Vadot static int xgbe_settime(struct ptp_clock_info *info,
1967113afc8SEmmanuel Vadot const struct timespec64 *ts)
1977113afc8SEmmanuel Vadot {
1987113afc8SEmmanuel Vadot struct xgbe_prv_data *pdata = container_of(info,
1997113afc8SEmmanuel Vadot struct xgbe_prv_data,
2007113afc8SEmmanuel Vadot ptp_clock_info);
2017113afc8SEmmanuel Vadot unsigned long flags;
2027113afc8SEmmanuel Vadot u64 nsec;
2037113afc8SEmmanuel Vadot
2047113afc8SEmmanuel Vadot nsec = timespec64_to_ns(ts);
2057113afc8SEmmanuel Vadot
2067113afc8SEmmanuel Vadot spin_lock_irqsave(&pdata->tstamp_lock, flags);
2077113afc8SEmmanuel Vadot
2087113afc8SEmmanuel Vadot timecounter_init(&pdata->tstamp_tc, &pdata->tstamp_cc, nsec);
2097113afc8SEmmanuel Vadot
2107113afc8SEmmanuel Vadot spin_unlock_irqrestore(&pdata->tstamp_lock, flags);
2117113afc8SEmmanuel Vadot
2127113afc8SEmmanuel Vadot return (0);
2137113afc8SEmmanuel Vadot }
2147113afc8SEmmanuel Vadot
xgbe_enable(struct ptp_clock_info * info,void * request,int on)2157113afc8SEmmanuel Vadot static int xgbe_enable(struct ptp_clock_info *info,
2167113afc8SEmmanuel Vadot void *request, int on)
2177113afc8SEmmanuel Vadot {
2187113afc8SEmmanuel Vadot return (-EOPNOTSUPP);
2197113afc8SEmmanuel Vadot }
2207113afc8SEmmanuel Vadot
xgbe_ptp_register(struct xgbe_prv_data * pdata)2217113afc8SEmmanuel Vadot void xgbe_ptp_register(struct xgbe_prv_data *pdata)
2227113afc8SEmmanuel Vadot {
2237113afc8SEmmanuel Vadot struct ptp_clock_info *info = &pdata->ptp_clock_info;
2247113afc8SEmmanuel Vadot //struct ptp_clock *clock;
2257113afc8SEmmanuel Vadot struct cyclecounter *cc = &pdata->tstamp_cc;
2267113afc8SEmmanuel Vadot u64 dividend;
2277113afc8SEmmanuel Vadot
2287113afc8SEmmanuel Vadot snprintf(info->name, sizeof(info->name), "axgbe-ptp");
2297113afc8SEmmanuel Vadot //info->owner = THIS_MODULE;
2307113afc8SEmmanuel Vadot info->max_adj = pdata->ptpclk_rate;
2317113afc8SEmmanuel Vadot info->adjfreq = xgbe_adjfreq;
2327113afc8SEmmanuel Vadot info->adjtime = xgbe_adjtime;
2337113afc8SEmmanuel Vadot info->gettime64 = xgbe_gettime;
2347113afc8SEmmanuel Vadot info->settime64 = xgbe_settime;
2357113afc8SEmmanuel Vadot info->enable = xgbe_enable;
2367113afc8SEmmanuel Vadot #if 0
2377113afc8SEmmanuel Vadot clock = ptp_clock_register(info, pdata->dev);
2387113afc8SEmmanuel Vadot if (IS_ERR(clock)) {
2397113afc8SEmmanuel Vadot dev_err(pdata->dev, "ptp_clock_register failed\n");
2407113afc8SEmmanuel Vadot return;
2417113afc8SEmmanuel Vadot }
2427113afc8SEmmanuel Vadot
2437113afc8SEmmanuel Vadot pdata->ptp_clock = clock;
2447113afc8SEmmanuel Vadot #endif
2457113afc8SEmmanuel Vadot /* Calculate the addend:
2467113afc8SEmmanuel Vadot * addend = 2^32 / (PTP ref clock / 50Mhz)
2477113afc8SEmmanuel Vadot * = (2^32 * 50Mhz) / PTP ref clock
2487113afc8SEmmanuel Vadot */
2497113afc8SEmmanuel Vadot dividend = 50000000;
2507113afc8SEmmanuel Vadot dividend <<= 32;
2517113afc8SEmmanuel Vadot pdata->tstamp_addend = div_u64(dividend, pdata->ptpclk_rate);
2527113afc8SEmmanuel Vadot
2537113afc8SEmmanuel Vadot /* Setup the timecounter */
2547113afc8SEmmanuel Vadot cc->read = xgbe_cc_read;
2557113afc8SEmmanuel Vadot cc->mask = CLOCKSOURCE_MASK(64);
2567113afc8SEmmanuel Vadot cc->mult = 1;
2577113afc8SEmmanuel Vadot cc->shift = 0;
2587113afc8SEmmanuel Vadot
2597113afc8SEmmanuel Vadot timecounter_init(&pdata->tstamp_tc, &pdata->tstamp_cc,
2607113afc8SEmmanuel Vadot ktime_to_ns(ktime_get_real()));
2617113afc8SEmmanuel Vadot
2627113afc8SEmmanuel Vadot /* Disable all timestamping to start */
2637113afc8SEmmanuel Vadot XGMAC_IOWRITE(pdata, MAC_TSCR, 0);
2647113afc8SEmmanuel Vadot pdata->tstamp_config.tx_type = HWTSTAMP_TX_OFF;
2657113afc8SEmmanuel Vadot pdata->tstamp_config.rx_filter = HWTSTAMP_FILTER_NONE;
2667113afc8SEmmanuel Vadot }
2677113afc8SEmmanuel Vadot
xgbe_ptp_unregister(struct xgbe_prv_data * pdata)2687113afc8SEmmanuel Vadot void xgbe_ptp_unregister(struct xgbe_prv_data *pdata)
2697113afc8SEmmanuel Vadot {
2707113afc8SEmmanuel Vadot #if 0
2717113afc8SEmmanuel Vadot if (pdata->ptp_clock)
2727113afc8SEmmanuel Vadot ptp_clock_unregister(pdata->ptp_clock);
2737113afc8SEmmanuel Vadot #endif
2747113afc8SEmmanuel Vadot }
275