stmmac_hwtstamp.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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 void stmmac_config_sub_second_increment(void __iomem *ioaddr)
  28. {
  29. u32 value = readl(ioaddr + PTP_TCR);
  30. unsigned long data;
  31. /* Convert the ptp_clock to nano second
  32. * formula = (1/ptp_clock) * 1000000000
  33. * where, ptp_clock = 50MHz.
  34. */
  35. data = (1000000000ULL / 50000000);
  36. /* 0.465ns accuracy */
  37. if (!(value & PTP_TCR_TSCTRLSSR))
  38. data = (data * 1000) / 465;
  39. writel(data, ioaddr + PTP_SSIR);
  40. }
  41. static int stmmac_init_systime(void __iomem *ioaddr, u32 sec, u32 nsec)
  42. {
  43. int limit;
  44. u32 value;
  45. writel(sec, ioaddr + PTP_STSUR);
  46. writel(nsec, ioaddr + PTP_STNSUR);
  47. /* issue command to initialize the system time value */
  48. value = readl(ioaddr + PTP_TCR);
  49. value |= PTP_TCR_TSINIT;
  50. writel(value, ioaddr + PTP_TCR);
  51. /* wait for present system time initialize to complete */
  52. limit = 10;
  53. while (limit--) {
  54. if (!(readl(ioaddr + PTP_TCR) & PTP_TCR_TSINIT))
  55. break;
  56. mdelay(10);
  57. }
  58. if (limit < 0)
  59. return -EBUSY;
  60. return 0;
  61. }
  62. static int stmmac_config_addend(void __iomem *ioaddr, u32 addend)
  63. {
  64. u32 value;
  65. int limit;
  66. writel(addend, ioaddr + PTP_TAR);
  67. /* issue command to update the addend value */
  68. value = readl(ioaddr + PTP_TCR);
  69. value |= PTP_TCR_TSADDREG;
  70. writel(value, ioaddr + PTP_TCR);
  71. /* wait for present addend update to complete */
  72. limit = 10;
  73. while (limit--) {
  74. if (!(readl(ioaddr + PTP_TCR) & PTP_TCR_TSADDREG))
  75. break;
  76. mdelay(10);
  77. }
  78. if (limit < 0)
  79. return -EBUSY;
  80. return 0;
  81. }
  82. static int stmmac_adjust_systime(void __iomem *ioaddr, u32 sec, u32 nsec,
  83. int add_sub)
  84. {
  85. u32 value;
  86. int limit;
  87. writel(sec, ioaddr + PTP_STSUR);
  88. writel(((add_sub << PTP_STNSUR_ADDSUB_SHIFT) | nsec),
  89. ioaddr + PTP_STNSUR);
  90. /* issue command to initialize the system time value */
  91. value = readl(ioaddr + PTP_TCR);
  92. value |= PTP_TCR_TSUPDT;
  93. writel(value, ioaddr + PTP_TCR);
  94. /* wait for present system time adjust/update to complete */
  95. limit = 10;
  96. while (limit--) {
  97. if (!(readl(ioaddr + PTP_TCR) & PTP_TCR_TSUPDT))
  98. break;
  99. mdelay(10);
  100. }
  101. if (limit < 0)
  102. return -EBUSY;
  103. return 0;
  104. }
  105. static u64 stmmac_get_systime(void __iomem *ioaddr)
  106. {
  107. u64 ns;
  108. ns = readl(ioaddr + PTP_STNSR);
  109. /* convert sec time value to nanosecond */
  110. ns += readl(ioaddr + PTP_STSR) * 1000000000ULL;
  111. return ns;
  112. }
  113. const struct stmmac_hwtimestamp stmmac_ptp = {
  114. .config_hw_tstamping = stmmac_config_hw_tstamping,
  115. .init_systime = stmmac_init_systime,
  116. .config_sub_second_increment = stmmac_config_sub_second_increment,
  117. .config_addend = stmmac_config_addend,
  118. .adjust_systime = stmmac_adjust_systime,
  119. .get_systime = stmmac_get_systime,
  120. };