xgbe-ptp.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. /*
  2. * AMD 10Gb Ethernet driver
  3. *
  4. * This file is available to you under your choice of the following two
  5. * licenses:
  6. *
  7. * License 1: GPLv2
  8. *
  9. * Copyright (c) 2014 Advanced Micro Devices, Inc.
  10. *
  11. * This file is free software; you may copy, redistribute and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation, either version 2 of the License, or (at
  14. * your option) any later version.
  15. *
  16. * This file is distributed in the hope that it will be useful, but
  17. * WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  19. * General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. *
  24. * This file incorporates work covered by the following copyright and
  25. * permission notice:
  26. * The Synopsys DWC ETHER XGMAC Software Driver and documentation
  27. * (hereinafter "Software") is an unsupported proprietary work of Synopsys,
  28. * Inc. unless otherwise expressly agreed to in writing between Synopsys
  29. * and you.
  30. *
  31. * The Software IS NOT an item of Licensed Software or Licensed Product
  32. * under any End User Software License Agreement or Agreement for Licensed
  33. * Product with Synopsys or any supplement thereto. Permission is hereby
  34. * granted, free of charge, to any person obtaining a copy of this software
  35. * annotated with this license and the Software, to deal in the Software
  36. * without restriction, including without limitation the rights to use,
  37. * copy, modify, merge, publish, distribute, sublicense, and/or sell copies
  38. * of the Software, and to permit persons to whom the Software is furnished
  39. * to do so, subject to the following conditions:
  40. *
  41. * The above copyright notice and this permission notice shall be included
  42. * in all copies or substantial portions of the Software.
  43. *
  44. * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS"
  45. * BASIS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  46. * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
  47. * PARTICULAR PURPOSE ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS
  48. * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  49. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  50. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  51. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  52. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  53. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  54. * THE POSSIBILITY OF SUCH DAMAGE.
  55. *
  56. *
  57. * License 2: Modified BSD
  58. *
  59. * Copyright (c) 2014 Advanced Micro Devices, Inc.
  60. * All rights reserved.
  61. *
  62. * Redistribution and use in source and binary forms, with or without
  63. * modification, are permitted provided that the following conditions are met:
  64. * * Redistributions of source code must retain the above copyright
  65. * notice, this list of conditions and the following disclaimer.
  66. * * Redistributions in binary form must reproduce the above copyright
  67. * notice, this list of conditions and the following disclaimer in the
  68. * documentation and/or other materials provided with the distribution.
  69. * * Neither the name of Advanced Micro Devices, Inc. nor the
  70. * names of its contributors may be used to endorse or promote products
  71. * derived from this software without specific prior written permission.
  72. *
  73. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  74. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  75. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  76. * ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
  77. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  78. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  79. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  80. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  81. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  82. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  83. *
  84. * This file incorporates work covered by the following copyright and
  85. * permission notice:
  86. * The Synopsys DWC ETHER XGMAC Software Driver and documentation
  87. * (hereinafter "Software") is an unsupported proprietary work of Synopsys,
  88. * Inc. unless otherwise expressly agreed to in writing between Synopsys
  89. * and you.
  90. *
  91. * The Software IS NOT an item of Licensed Software or Licensed Product
  92. * under any End User Software License Agreement or Agreement for Licensed
  93. * Product with Synopsys or any supplement thereto. Permission is hereby
  94. * granted, free of charge, to any person obtaining a copy of this software
  95. * annotated with this license and the Software, to deal in the Software
  96. * without restriction, including without limitation the rights to use,
  97. * copy, modify, merge, publish, distribute, sublicense, and/or sell copies
  98. * of the Software, and to permit persons to whom the Software is furnished
  99. * to do so, subject to the following conditions:
  100. *
  101. * The above copyright notice and this permission notice shall be included
  102. * in all copies or substantial portions of the Software.
  103. *
  104. * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS"
  105. * BASIS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  106. * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
  107. * PARTICULAR PURPOSE ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS
  108. * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  109. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  110. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  111. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  112. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  113. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  114. * THE POSSIBILITY OF SUCH DAMAGE.
  115. */
  116. #include <linux/clk.h>
  117. #include <linux/clocksource.h>
  118. #include <linux/ptp_clock_kernel.h>
  119. #include <linux/net_tstamp.h>
  120. #include "xgbe.h"
  121. #include "xgbe-common.h"
  122. static cycle_t xgbe_cc_read(const struct cyclecounter *cc)
  123. {
  124. struct xgbe_prv_data *pdata = container_of(cc,
  125. struct xgbe_prv_data,
  126. tstamp_cc);
  127. u64 nsec;
  128. nsec = pdata->hw_if.get_tstamp_time(pdata);
  129. return nsec;
  130. }
  131. static int xgbe_adjfreq(struct ptp_clock_info *info, s32 delta)
  132. {
  133. struct xgbe_prv_data *pdata = container_of(info,
  134. struct xgbe_prv_data,
  135. ptp_clock_info);
  136. unsigned long flags;
  137. u64 adjust;
  138. u32 addend, diff;
  139. unsigned int neg_adjust = 0;
  140. if (delta < 0) {
  141. neg_adjust = 1;
  142. delta = -delta;
  143. }
  144. adjust = pdata->tstamp_addend;
  145. adjust *= delta;
  146. diff = div_u64(adjust, 1000000000UL);
  147. addend = (neg_adjust) ? pdata->tstamp_addend - diff :
  148. pdata->tstamp_addend + diff;
  149. spin_lock_irqsave(&pdata->tstamp_lock, flags);
  150. pdata->hw_if.update_tstamp_addend(pdata, addend);
  151. spin_unlock_irqrestore(&pdata->tstamp_lock, flags);
  152. return 0;
  153. }
  154. static int xgbe_adjtime(struct ptp_clock_info *info, s64 delta)
  155. {
  156. struct xgbe_prv_data *pdata = container_of(info,
  157. struct xgbe_prv_data,
  158. ptp_clock_info);
  159. unsigned long flags;
  160. spin_lock_irqsave(&pdata->tstamp_lock, flags);
  161. timecounter_adjtime(&pdata->tstamp_tc, delta);
  162. spin_unlock_irqrestore(&pdata->tstamp_lock, flags);
  163. return 0;
  164. }
  165. static int xgbe_gettime(struct ptp_clock_info *info, struct timespec64 *ts)
  166. {
  167. struct xgbe_prv_data *pdata = container_of(info,
  168. struct xgbe_prv_data,
  169. ptp_clock_info);
  170. unsigned long flags;
  171. u64 nsec;
  172. spin_lock_irqsave(&pdata->tstamp_lock, flags);
  173. nsec = timecounter_read(&pdata->tstamp_tc);
  174. spin_unlock_irqrestore(&pdata->tstamp_lock, flags);
  175. *ts = ns_to_timespec64(nsec);
  176. return 0;
  177. }
  178. static int xgbe_settime(struct ptp_clock_info *info,
  179. const struct timespec64 *ts)
  180. {
  181. struct xgbe_prv_data *pdata = container_of(info,
  182. struct xgbe_prv_data,
  183. ptp_clock_info);
  184. unsigned long flags;
  185. u64 nsec;
  186. nsec = timespec64_to_ns(ts);
  187. spin_lock_irqsave(&pdata->tstamp_lock, flags);
  188. timecounter_init(&pdata->tstamp_tc, &pdata->tstamp_cc, nsec);
  189. spin_unlock_irqrestore(&pdata->tstamp_lock, flags);
  190. return 0;
  191. }
  192. static int xgbe_enable(struct ptp_clock_info *info,
  193. struct ptp_clock_request *request, int on)
  194. {
  195. return -EOPNOTSUPP;
  196. }
  197. void xgbe_ptp_register(struct xgbe_prv_data *pdata)
  198. {
  199. struct ptp_clock_info *info = &pdata->ptp_clock_info;
  200. struct ptp_clock *clock;
  201. struct cyclecounter *cc = &pdata->tstamp_cc;
  202. u64 dividend;
  203. snprintf(info->name, sizeof(info->name), "%s",
  204. netdev_name(pdata->netdev));
  205. info->owner = THIS_MODULE;
  206. info->max_adj = pdata->ptpclk_rate;
  207. info->adjfreq = xgbe_adjfreq;
  208. info->adjtime = xgbe_adjtime;
  209. info->gettime64 = xgbe_gettime;
  210. info->settime64 = xgbe_settime;
  211. info->enable = xgbe_enable;
  212. clock = ptp_clock_register(info, pdata->dev);
  213. if (IS_ERR(clock)) {
  214. dev_err(pdata->dev, "ptp_clock_register failed\n");
  215. return;
  216. }
  217. pdata->ptp_clock = clock;
  218. /* Calculate the addend:
  219. * addend = 2^32 / (PTP ref clock / 50Mhz)
  220. * = (2^32 * 50Mhz) / PTP ref clock
  221. */
  222. dividend = 50000000;
  223. dividend <<= 32;
  224. pdata->tstamp_addend = div_u64(dividend, pdata->ptpclk_rate);
  225. /* Setup the timecounter */
  226. cc->read = xgbe_cc_read;
  227. cc->mask = CLOCKSOURCE_MASK(64);
  228. cc->mult = 1;
  229. cc->shift = 0;
  230. timecounter_init(&pdata->tstamp_tc, &pdata->tstamp_cc,
  231. ktime_to_ns(ktime_get_real()));
  232. /* Disable all timestamping to start */
  233. XGMAC_IOWRITE(pdata, MAC_TCR, 0);
  234. pdata->tstamp_config.tx_type = HWTSTAMP_TX_OFF;
  235. pdata->tstamp_config.rx_filter = HWTSTAMP_FILTER_NONE;
  236. }
  237. void xgbe_ptp_unregister(struct xgbe_prv_data *pdata)
  238. {
  239. if (pdata->ptp_clock)
  240. ptp_clock_unregister(pdata->ptp_clock);
  241. }