ptp_dte.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. /*
  2. * Copyright 2017 Broadcom
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation version 2.
  7. *
  8. * This program is distributed "as is" WITHOUT ANY WARRANTY of any
  9. * kind, whether express or implied; without even the implied warranty
  10. * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. */
  13. #include <linux/err.h>
  14. #include <linux/io.h>
  15. #include <linux/module.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/ptp_clock_kernel.h>
  18. #include <linux/types.h>
  19. #define DTE_NCO_LOW_TIME_REG 0x00
  20. #define DTE_NCO_TIME_REG 0x04
  21. #define DTE_NCO_OVERFLOW_REG 0x08
  22. #define DTE_NCO_INC_REG 0x0c
  23. #define DTE_NCO_SUM2_MASK 0xffffffff
  24. #define DTE_NCO_SUM2_SHIFT 4ULL
  25. #define DTE_NCO_SUM3_MASK 0xff
  26. #define DTE_NCO_SUM3_SHIFT 36ULL
  27. #define DTE_NCO_SUM3_WR_SHIFT 8
  28. #define DTE_NCO_TS_WRAP_MASK 0xfff
  29. #define DTE_NCO_TS_WRAP_LSHIFT 32
  30. #define DTE_NCO_INC_DEFAULT 0x80000000
  31. #define DTE_NUM_REGS_TO_RESTORE 4
  32. /* Full wrap around is 44bits in ns (~4.887 hrs) */
  33. #define DTE_WRAP_AROUND_NSEC_SHIFT 44
  34. /* 44 bits NCO */
  35. #define DTE_NCO_MAX_NS 0xFFFFFFFFFFFLL
  36. /* 125MHz with 3.29 reg cfg */
  37. #define DTE_PPB_ADJ(ppb) (u32)(div64_u64((((u64)abs(ppb) * BIT(28)) +\
  38. 62500000ULL), 125000000ULL))
  39. /* ptp dte priv structure */
  40. struct ptp_dte {
  41. void __iomem *regs;
  42. struct ptp_clock *ptp_clk;
  43. struct ptp_clock_info caps;
  44. struct device *dev;
  45. u32 ts_ovf_last;
  46. u32 ts_wrap_cnt;
  47. spinlock_t lock;
  48. u32 reg_val[DTE_NUM_REGS_TO_RESTORE];
  49. };
  50. static void dte_write_nco(void __iomem *regs, s64 ns)
  51. {
  52. u32 sum2, sum3;
  53. sum2 = (u32)((ns >> DTE_NCO_SUM2_SHIFT) & DTE_NCO_SUM2_MASK);
  54. /* compensate for ignoring sum1 */
  55. if (sum2 != DTE_NCO_SUM2_MASK)
  56. sum2++;
  57. /* to write sum3, bits [15:8] needs to be written */
  58. sum3 = (u32)(((ns >> DTE_NCO_SUM3_SHIFT) & DTE_NCO_SUM3_MASK) <<
  59. DTE_NCO_SUM3_WR_SHIFT);
  60. writel(0, (regs + DTE_NCO_LOW_TIME_REG));
  61. writel(sum2, (regs + DTE_NCO_TIME_REG));
  62. writel(sum3, (regs + DTE_NCO_OVERFLOW_REG));
  63. }
  64. static s64 dte_read_nco(void __iomem *regs)
  65. {
  66. u32 sum2, sum3;
  67. s64 ns;
  68. /*
  69. * ignoring sum1 (4 bits) gives a 16ns resolution, which
  70. * works due to the async register read.
  71. */
  72. sum3 = readl(regs + DTE_NCO_OVERFLOW_REG) & DTE_NCO_SUM3_MASK;
  73. sum2 = readl(regs + DTE_NCO_TIME_REG);
  74. ns = ((s64)sum3 << DTE_NCO_SUM3_SHIFT) |
  75. ((s64)sum2 << DTE_NCO_SUM2_SHIFT);
  76. return ns;
  77. }
  78. static void dte_write_nco_delta(struct ptp_dte *ptp_dte, s64 delta)
  79. {
  80. s64 ns;
  81. ns = dte_read_nco(ptp_dte->regs);
  82. /* handle wraparound conditions */
  83. if ((delta < 0) && (abs(delta) > ns)) {
  84. if (ptp_dte->ts_wrap_cnt) {
  85. ns += DTE_NCO_MAX_NS + delta;
  86. ptp_dte->ts_wrap_cnt--;
  87. } else {
  88. ns = 0;
  89. }
  90. } else {
  91. ns += delta;
  92. if (ns > DTE_NCO_MAX_NS) {
  93. ptp_dte->ts_wrap_cnt++;
  94. ns -= DTE_NCO_MAX_NS;
  95. }
  96. }
  97. dte_write_nco(ptp_dte->regs, ns);
  98. ptp_dte->ts_ovf_last = (ns >> DTE_NCO_TS_WRAP_LSHIFT) &
  99. DTE_NCO_TS_WRAP_MASK;
  100. }
  101. static s64 dte_read_nco_with_ovf(struct ptp_dte *ptp_dte)
  102. {
  103. u32 ts_ovf;
  104. s64 ns = 0;
  105. ns = dte_read_nco(ptp_dte->regs);
  106. /*Timestamp overflow: 8 LSB bits of sum3, 4 MSB bits of sum2 */
  107. ts_ovf = (ns >> DTE_NCO_TS_WRAP_LSHIFT) & DTE_NCO_TS_WRAP_MASK;
  108. /* Check for wrap around */
  109. if (ts_ovf < ptp_dte->ts_ovf_last)
  110. ptp_dte->ts_wrap_cnt++;
  111. ptp_dte->ts_ovf_last = ts_ovf;
  112. /* adjust for wraparounds */
  113. ns += (s64)(BIT_ULL(DTE_WRAP_AROUND_NSEC_SHIFT) * ptp_dte->ts_wrap_cnt);
  114. return ns;
  115. }
  116. static int ptp_dte_adjfreq(struct ptp_clock_info *ptp, s32 ppb)
  117. {
  118. u32 nco_incr;
  119. unsigned long flags;
  120. struct ptp_dte *ptp_dte = container_of(ptp, struct ptp_dte, caps);
  121. if (abs(ppb) > ptp_dte->caps.max_adj) {
  122. dev_err(ptp_dte->dev, "ppb adj too big\n");
  123. return -EINVAL;
  124. }
  125. if (ppb < 0)
  126. nco_incr = DTE_NCO_INC_DEFAULT - DTE_PPB_ADJ(ppb);
  127. else
  128. nco_incr = DTE_NCO_INC_DEFAULT + DTE_PPB_ADJ(ppb);
  129. spin_lock_irqsave(&ptp_dte->lock, flags);
  130. writel(nco_incr, ptp_dte->regs + DTE_NCO_INC_REG);
  131. spin_unlock_irqrestore(&ptp_dte->lock, flags);
  132. return 0;
  133. }
  134. static int ptp_dte_adjtime(struct ptp_clock_info *ptp, s64 delta)
  135. {
  136. unsigned long flags;
  137. struct ptp_dte *ptp_dte = container_of(ptp, struct ptp_dte, caps);
  138. spin_lock_irqsave(&ptp_dte->lock, flags);
  139. dte_write_nco_delta(ptp_dte, delta);
  140. spin_unlock_irqrestore(&ptp_dte->lock, flags);
  141. return 0;
  142. }
  143. static int ptp_dte_gettime(struct ptp_clock_info *ptp, struct timespec64 *ts)
  144. {
  145. unsigned long flags;
  146. struct ptp_dte *ptp_dte = container_of(ptp, struct ptp_dte, caps);
  147. spin_lock_irqsave(&ptp_dte->lock, flags);
  148. *ts = ns_to_timespec64(dte_read_nco_with_ovf(ptp_dte));
  149. spin_unlock_irqrestore(&ptp_dte->lock, flags);
  150. return 0;
  151. }
  152. static int ptp_dte_settime(struct ptp_clock_info *ptp,
  153. const struct timespec64 *ts)
  154. {
  155. unsigned long flags;
  156. struct ptp_dte *ptp_dte = container_of(ptp, struct ptp_dte, caps);
  157. spin_lock_irqsave(&ptp_dte->lock, flags);
  158. /* Disable nco increment */
  159. writel(0, ptp_dte->regs + DTE_NCO_INC_REG);
  160. dte_write_nco(ptp_dte->regs, timespec64_to_ns(ts));
  161. /* reset overflow and wrap counter */
  162. ptp_dte->ts_ovf_last = 0;
  163. ptp_dte->ts_wrap_cnt = 0;
  164. /* Enable nco increment */
  165. writel(DTE_NCO_INC_DEFAULT, ptp_dte->regs + DTE_NCO_INC_REG);
  166. spin_unlock_irqrestore(&ptp_dte->lock, flags);
  167. return 0;
  168. }
  169. static int ptp_dte_enable(struct ptp_clock_info *ptp,
  170. struct ptp_clock_request *rq, int on)
  171. {
  172. return -EOPNOTSUPP;
  173. }
  174. static const struct ptp_clock_info ptp_dte_caps = {
  175. .owner = THIS_MODULE,
  176. .name = "DTE PTP timer",
  177. .max_adj = 50000000,
  178. .n_ext_ts = 0,
  179. .n_pins = 0,
  180. .pps = 0,
  181. .adjfreq = ptp_dte_adjfreq,
  182. .adjtime = ptp_dte_adjtime,
  183. .gettime64 = ptp_dte_gettime,
  184. .settime64 = ptp_dte_settime,
  185. .enable = ptp_dte_enable,
  186. };
  187. static int ptp_dte_probe(struct platform_device *pdev)
  188. {
  189. struct ptp_dte *ptp_dte;
  190. struct device *dev = &pdev->dev;
  191. struct resource *res;
  192. ptp_dte = devm_kzalloc(dev, sizeof(struct ptp_dte), GFP_KERNEL);
  193. if (!ptp_dte)
  194. return -ENOMEM;
  195. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  196. ptp_dte->regs = devm_ioremap_resource(dev, res);
  197. if (IS_ERR(ptp_dte->regs)) {
  198. dev_err(dev,
  199. "%s: io remap failed\n", __func__);
  200. return PTR_ERR(ptp_dte->regs);
  201. }
  202. spin_lock_init(&ptp_dte->lock);
  203. ptp_dte->dev = dev;
  204. ptp_dte->caps = ptp_dte_caps;
  205. ptp_dte->ptp_clk = ptp_clock_register(&ptp_dte->caps, &pdev->dev);
  206. if (IS_ERR(ptp_dte->ptp_clk)) {
  207. dev_err(dev,
  208. "%s: Failed to register ptp clock\n", __func__);
  209. return PTR_ERR(ptp_dte->ptp_clk);
  210. }
  211. platform_set_drvdata(pdev, ptp_dte);
  212. dev_info(dev, "ptp clk probe done\n");
  213. return 0;
  214. }
  215. static int ptp_dte_remove(struct platform_device *pdev)
  216. {
  217. struct ptp_dte *ptp_dte = platform_get_drvdata(pdev);
  218. u8 i;
  219. ptp_clock_unregister(ptp_dte->ptp_clk);
  220. for (i = 0; i < DTE_NUM_REGS_TO_RESTORE; i++)
  221. writel(0, ptp_dte->regs + (i * sizeof(u32)));
  222. return 0;
  223. }
  224. #ifdef CONFIG_PM_SLEEP
  225. static int ptp_dte_suspend(struct device *dev)
  226. {
  227. struct platform_device *pdev = to_platform_device(dev);
  228. struct ptp_dte *ptp_dte = platform_get_drvdata(pdev);
  229. u8 i;
  230. for (i = 0; i < DTE_NUM_REGS_TO_RESTORE; i++) {
  231. ptp_dte->reg_val[i] =
  232. readl(ptp_dte->regs + (i * sizeof(u32)));
  233. }
  234. /* disable the nco */
  235. writel(0, ptp_dte->regs + DTE_NCO_INC_REG);
  236. return 0;
  237. }
  238. static int ptp_dte_resume(struct device *dev)
  239. {
  240. struct platform_device *pdev = to_platform_device(dev);
  241. struct ptp_dte *ptp_dte = platform_get_drvdata(pdev);
  242. u8 i;
  243. for (i = 0; i < DTE_NUM_REGS_TO_RESTORE; i++) {
  244. if ((i * sizeof(u32)) != DTE_NCO_OVERFLOW_REG)
  245. writel(ptp_dte->reg_val[i],
  246. (ptp_dte->regs + (i * sizeof(u32))));
  247. else
  248. writel(((ptp_dte->reg_val[i] &
  249. DTE_NCO_SUM3_MASK) << DTE_NCO_SUM3_WR_SHIFT),
  250. (ptp_dte->regs + (i * sizeof(u32))));
  251. }
  252. return 0;
  253. }
  254. static const struct dev_pm_ops ptp_dte_pm_ops = {
  255. .suspend = ptp_dte_suspend,
  256. .resume = ptp_dte_resume
  257. };
  258. #define PTP_DTE_PM_OPS (&ptp_dte_pm_ops)
  259. #else
  260. #define PTP_DTE_PM_OPS NULL
  261. #endif
  262. static const struct of_device_id ptp_dte_of_match[] = {
  263. { .compatible = "brcm,ptp-dte", },
  264. {},
  265. };
  266. MODULE_DEVICE_TABLE(of, ptp_dte_of_match);
  267. static struct platform_driver ptp_dte_driver = {
  268. .driver = {
  269. .name = "ptp-dte",
  270. .pm = PTP_DTE_PM_OPS,
  271. .of_match_table = ptp_dte_of_match,
  272. },
  273. .probe = ptp_dte_probe,
  274. .remove = ptp_dte_remove,
  275. };
  276. module_platform_driver(ptp_dte_driver);
  277. MODULE_AUTHOR("Broadcom");
  278. MODULE_DESCRIPTION("Broadcom DTE PTP Clock driver");
  279. MODULE_LICENSE("GPL v2");