stmmac_hwtstamp.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*******************************************************************************
  2. Copyright (C) 2013 Vayavya Labs Pvt Ltd
  3. This implements all the API for managing HW timestamp & PTP.
  4. This program is free software; you can redistribute it and/or modify it
  5. under the terms and conditions of the GNU General Public License,
  6. version 2, as published by the Free Software Foundation.
  7. This program is distributed in the hope it will be useful, but WITHOUT
  8. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  9. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  10. more details.
  11. You should have received a copy of the GNU General Public License along with
  12. this program; if not, write to the Free Software Foundation, Inc.,
  13. 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
  14. The full GNU General Public License is included in this distribution in
  15. the file called "COPYING".
  16. Author: Rayagond Kokatanur <rayagond@vayavyalabs.com>
  17. Author: Giuseppe Cavallaro <peppe.cavallaro@st.com>
  18. *******************************************************************************/
  19. #include <linux/io.h>
  20. #include <linux/delay.h>
  21. #include "common.h"
  22. #include "stmmac_ptp.h"
  23. static void stmmac_config_hw_tstamping(void __iomem *ioaddr, u32 data)
  24. {
  25. writel(data, ioaddr + PTP_TCR);
  26. }
  27. static u32 stmmac_config_sub_second_increment(void __iomem *ioaddr,
  28. u32 ptp_clock, int gmac4)
  29. {
  30. u32 value = readl(ioaddr + PTP_TCR);
  31. unsigned long data;
  32. u32 reg_value;
  33. /* For GMAC3.x, 4.x versions, convert the ptp_clock to nano second
  34. * formula = (1/ptp_clock) * 1000000000
  35. * where ptp_clock is 50MHz if fine method is used to update system
  36. */
  37. if (value & PTP_TCR_TSCFUPDT)
  38. data = (1000000000ULL / 50000000);
  39. else
  40. data = (1000000000ULL / ptp_clock);
  41. /* 0.465ns accuracy */
  42. if (!(value & PTP_TCR_TSCTRLSSR))
  43. data = (data * 1000) / 465;
  44. data &= PTP_SSIR_SSINC_MASK;
  45. reg_value = data;
  46. if (gmac4)
  47. reg_value <<= GMAC4_PTP_SSIR_SSINC_SHIFT;
  48. writel(reg_value, ioaddr + PTP_SSIR);
  49. return data;
  50. }
  51. static int stmmac_init_systime(void __iomem *ioaddr, u32 sec, u32 nsec)
  52. {
  53. int limit;
  54. u32 value;
  55. writel(sec, ioaddr + PTP_STSUR);
  56. writel(nsec, ioaddr + PTP_STNSUR);
  57. /* issue command to initialize the system time value */
  58. value = readl(ioaddr + PTP_TCR);
  59. value |= PTP_TCR_TSINIT;
  60. writel(value, ioaddr + PTP_TCR);
  61. /* wait for present system time initialize to complete */
  62. limit = 10;
  63. while (limit--) {
  64. if (!(readl(ioaddr + PTP_TCR) & PTP_TCR_TSINIT))
  65. break;
  66. mdelay(10);
  67. }
  68. if (limit < 0)
  69. return -EBUSY;
  70. return 0;
  71. }
  72. static int stmmac_config_addend(void __iomem *ioaddr, u32 addend)
  73. {
  74. u32 value;
  75. int limit;
  76. writel(addend, ioaddr + PTP_TAR);
  77. /* issue command to update the addend value */
  78. value = readl(ioaddr + PTP_TCR);
  79. value |= PTP_TCR_TSADDREG;
  80. writel(value, ioaddr + PTP_TCR);
  81. /* wait for present addend update to complete */
  82. limit = 10;
  83. while (limit--) {
  84. if (!(readl(ioaddr + PTP_TCR) & PTP_TCR_TSADDREG))
  85. break;
  86. mdelay(10);
  87. }
  88. if (limit < 0)
  89. return -EBUSY;
  90. return 0;
  91. }
  92. static int stmmac_adjust_systime(void __iomem *ioaddr, u32 sec, u32 nsec,
  93. int add_sub, int gmac4)
  94. {
  95. u32 value;
  96. int limit;
  97. if (add_sub) {
  98. /* If the new sec value needs to be subtracted with
  99. * the system time, then MAC_STSUR reg should be
  100. * programmed with (2^32 – <new_sec_value>)
  101. */
  102. if (gmac4)
  103. sec = (100000000ULL - sec);
  104. value = readl(ioaddr + PTP_TCR);
  105. if (value & PTP_TCR_TSCTRLSSR)
  106. nsec = (PTP_DIGITAL_ROLLOVER_MODE - nsec);
  107. else
  108. nsec = (PTP_BINARY_ROLLOVER_MODE - nsec);
  109. }
  110. writel(sec, ioaddr + PTP_STSUR);
  111. value = (add_sub << PTP_STNSUR_ADDSUB_SHIFT) | nsec;
  112. writel(value, ioaddr + PTP_STNSUR);
  113. /* issue command to initialize the system time value */
  114. value = readl(ioaddr + PTP_TCR);
  115. value |= PTP_TCR_TSUPDT;
  116. writel(value, ioaddr + PTP_TCR);
  117. /* wait for present system time adjust/update to complete */
  118. limit = 10;
  119. while (limit--) {
  120. if (!(readl(ioaddr + PTP_TCR) & PTP_TCR_TSUPDT))
  121. break;
  122. mdelay(10);
  123. }
  124. if (limit < 0)
  125. return -EBUSY;
  126. return 0;
  127. }
  128. static u64 stmmac_get_systime(void __iomem *ioaddr)
  129. {
  130. u64 ns;
  131. /* Get the TSSS value */
  132. ns = readl(ioaddr + PTP_STNSR);
  133. /* Get the TSS and convert sec time value to nanosecond */
  134. ns += readl(ioaddr + PTP_STSR) * 1000000000ULL;
  135. return ns;
  136. }
  137. const struct stmmac_hwtimestamp stmmac_ptp = {
  138. .config_hw_tstamping = stmmac_config_hw_tstamping,
  139. .init_systime = stmmac_init_systime,
  140. .config_sub_second_increment = stmmac_config_sub_second_increment,
  141. .config_addend = stmmac_config_addend,
  142. .adjust_systime = stmmac_adjust_systime,
  143. .get_systime = stmmac_get_systime,
  144. };