rtc-zynqmp.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. /*
  2. * Xilinx Zynq Ultrascale+ MPSoC Real Time Clock Driver
  3. *
  4. * Copyright (C) 2015 Xilinx, Inc.
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms and conditions of the GNU General Public License,
  8. * version 2, as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope it will be useful, but WITHOUT
  11. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. * more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along with
  16. * this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. */
  19. #include <linux/delay.h>
  20. #include <linux/init.h>
  21. #include <linux/io.h>
  22. #include <linux/module.h>
  23. #include <linux/of.h>
  24. #include <linux/platform_device.h>
  25. #include <linux/rtc.h>
  26. /* RTC Registers */
  27. #define RTC_SET_TM_WR 0x00
  28. #define RTC_SET_TM_RD 0x04
  29. #define RTC_CALIB_WR 0x08
  30. #define RTC_CALIB_RD 0x0C
  31. #define RTC_CUR_TM 0x10
  32. #define RTC_CUR_TICK 0x14
  33. #define RTC_ALRM 0x18
  34. #define RTC_INT_STS 0x20
  35. #define RTC_INT_MASK 0x24
  36. #define RTC_INT_EN 0x28
  37. #define RTC_INT_DIS 0x2C
  38. #define RTC_CTRL 0x40
  39. #define RTC_FR_EN BIT(20)
  40. #define RTC_FR_DATSHIFT 16
  41. #define RTC_TICK_MASK 0xFFFF
  42. #define RTC_INT_SEC BIT(0)
  43. #define RTC_INT_ALRM BIT(1)
  44. #define RTC_OSC_EN BIT(24)
  45. #define RTC_CALIB_DEF 0x198233
  46. #define RTC_CALIB_MASK 0x1FFFFF
  47. #define RTC_SEC_MAX_VAL 0xFFFFFFFF
  48. struct xlnx_rtc_dev {
  49. struct rtc_device *rtc;
  50. void __iomem *reg_base;
  51. int alarm_irq;
  52. int sec_irq;
  53. };
  54. static int xlnx_rtc_set_time(struct device *dev, struct rtc_time *tm)
  55. {
  56. struct xlnx_rtc_dev *xrtcdev = dev_get_drvdata(dev);
  57. unsigned long new_time;
  58. new_time = rtc_tm_to_time64(tm);
  59. if (new_time > RTC_SEC_MAX_VAL)
  60. return -EINVAL;
  61. writel(new_time, xrtcdev->reg_base + RTC_SET_TM_WR);
  62. return 0;
  63. }
  64. static int xlnx_rtc_read_time(struct device *dev, struct rtc_time *tm)
  65. {
  66. struct xlnx_rtc_dev *xrtcdev = dev_get_drvdata(dev);
  67. rtc_time64_to_tm(readl(xrtcdev->reg_base + RTC_CUR_TM), tm);
  68. return rtc_valid_tm(tm);
  69. }
  70. static int xlnx_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
  71. {
  72. struct xlnx_rtc_dev *xrtcdev = dev_get_drvdata(dev);
  73. rtc_time64_to_tm(readl(xrtcdev->reg_base + RTC_ALRM), &alrm->time);
  74. alrm->enabled = readl(xrtcdev->reg_base + RTC_INT_MASK) & RTC_INT_ALRM;
  75. return 0;
  76. }
  77. static int xlnx_rtc_alarm_irq_enable(struct device *dev, u32 enabled)
  78. {
  79. struct xlnx_rtc_dev *xrtcdev = dev_get_drvdata(dev);
  80. if (enabled)
  81. writel(RTC_INT_ALRM, xrtcdev->reg_base + RTC_INT_EN);
  82. else
  83. writel(RTC_INT_ALRM, xrtcdev->reg_base + RTC_INT_DIS);
  84. return 0;
  85. }
  86. static int xlnx_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
  87. {
  88. struct xlnx_rtc_dev *xrtcdev = dev_get_drvdata(dev);
  89. unsigned long alarm_time;
  90. alarm_time = rtc_tm_to_time64(&alrm->time);
  91. if (alarm_time > RTC_SEC_MAX_VAL)
  92. return -EINVAL;
  93. writel((u32)alarm_time, (xrtcdev->reg_base + RTC_ALRM));
  94. xlnx_rtc_alarm_irq_enable(dev, alrm->enabled);
  95. return 0;
  96. }
  97. static void xlnx_init_rtc(struct xlnx_rtc_dev *xrtcdev, u32 calibval)
  98. {
  99. /*
  100. * Based on crystal freq of 33.330 KHz
  101. * set the seconds counter and enable, set fractions counter
  102. * to default value suggested as per design spec
  103. * to correct RTC delay in frequency over period of time.
  104. */
  105. calibval &= RTC_CALIB_MASK;
  106. writel(calibval, (xrtcdev->reg_base + RTC_CALIB_WR));
  107. }
  108. static const struct rtc_class_ops xlnx_rtc_ops = {
  109. .set_time = xlnx_rtc_set_time,
  110. .read_time = xlnx_rtc_read_time,
  111. .read_alarm = xlnx_rtc_read_alarm,
  112. .set_alarm = xlnx_rtc_set_alarm,
  113. .alarm_irq_enable = xlnx_rtc_alarm_irq_enable,
  114. };
  115. static irqreturn_t xlnx_rtc_interrupt(int irq, void *id)
  116. {
  117. struct xlnx_rtc_dev *xrtcdev = (struct xlnx_rtc_dev *)id;
  118. unsigned int status;
  119. status = readl(xrtcdev->reg_base + RTC_INT_STS);
  120. /* Check if interrupt asserted */
  121. if (!(status & (RTC_INT_SEC | RTC_INT_ALRM)))
  122. return IRQ_NONE;
  123. /* Clear interrupt */
  124. writel(status, xrtcdev->reg_base + RTC_INT_STS);
  125. if (status & RTC_INT_SEC)
  126. rtc_update_irq(xrtcdev->rtc, 1, RTC_IRQF | RTC_UF);
  127. if (status & RTC_INT_ALRM)
  128. rtc_update_irq(xrtcdev->rtc, 1, RTC_IRQF | RTC_AF);
  129. return IRQ_HANDLED;
  130. }
  131. static int xlnx_rtc_probe(struct platform_device *pdev)
  132. {
  133. struct xlnx_rtc_dev *xrtcdev;
  134. struct resource *res;
  135. int ret;
  136. unsigned int calibvalue;
  137. xrtcdev = devm_kzalloc(&pdev->dev, sizeof(*xrtcdev), GFP_KERNEL);
  138. if (!xrtcdev)
  139. return -ENOMEM;
  140. platform_set_drvdata(pdev, xrtcdev);
  141. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  142. xrtcdev->reg_base = devm_ioremap_resource(&pdev->dev, res);
  143. if (IS_ERR(xrtcdev->reg_base))
  144. return PTR_ERR(xrtcdev->reg_base);
  145. xrtcdev->alarm_irq = platform_get_irq_byname(pdev, "alarm");
  146. if (xrtcdev->alarm_irq < 0) {
  147. dev_err(&pdev->dev, "no irq resource\n");
  148. return xrtcdev->alarm_irq;
  149. }
  150. ret = devm_request_irq(&pdev->dev, xrtcdev->alarm_irq,
  151. xlnx_rtc_interrupt, 0,
  152. dev_name(&pdev->dev), xrtcdev);
  153. if (ret) {
  154. dev_err(&pdev->dev, "request irq failed\n");
  155. return ret;
  156. }
  157. xrtcdev->sec_irq = platform_get_irq_byname(pdev, "sec");
  158. if (xrtcdev->sec_irq < 0) {
  159. dev_err(&pdev->dev, "no irq resource\n");
  160. return xrtcdev->sec_irq;
  161. }
  162. ret = devm_request_irq(&pdev->dev, xrtcdev->sec_irq,
  163. xlnx_rtc_interrupt, 0,
  164. dev_name(&pdev->dev), xrtcdev);
  165. if (ret) {
  166. dev_err(&pdev->dev, "request irq failed\n");
  167. return ret;
  168. }
  169. ret = of_property_read_u32(pdev->dev.of_node, "calibration",
  170. &calibvalue);
  171. if (ret)
  172. calibvalue = RTC_CALIB_DEF;
  173. xlnx_init_rtc(xrtcdev, calibvalue);
  174. device_init_wakeup(&pdev->dev, 1);
  175. xrtcdev->rtc = devm_rtc_device_register(&pdev->dev, pdev->name,
  176. &xlnx_rtc_ops, THIS_MODULE);
  177. return PTR_ERR_OR_ZERO(xrtcdev->rtc);
  178. }
  179. static int xlnx_rtc_remove(struct platform_device *pdev)
  180. {
  181. xlnx_rtc_alarm_irq_enable(&pdev->dev, 0);
  182. device_init_wakeup(&pdev->dev, 0);
  183. return 0;
  184. }
  185. static int __maybe_unused xlnx_rtc_suspend(struct device *dev)
  186. {
  187. struct platform_device *pdev = to_platform_device(dev);
  188. struct xlnx_rtc_dev *xrtcdev = platform_get_drvdata(pdev);
  189. if (device_may_wakeup(&pdev->dev))
  190. enable_irq_wake(xrtcdev->alarm_irq);
  191. else
  192. xlnx_rtc_alarm_irq_enable(dev, 0);
  193. return 0;
  194. }
  195. static int __maybe_unused xlnx_rtc_resume(struct device *dev)
  196. {
  197. struct platform_device *pdev = to_platform_device(dev);
  198. struct xlnx_rtc_dev *xrtcdev = platform_get_drvdata(pdev);
  199. if (device_may_wakeup(&pdev->dev))
  200. disable_irq_wake(xrtcdev->alarm_irq);
  201. else
  202. xlnx_rtc_alarm_irq_enable(dev, 1);
  203. return 0;
  204. }
  205. static SIMPLE_DEV_PM_OPS(xlnx_rtc_pm_ops, xlnx_rtc_suspend, xlnx_rtc_resume);
  206. static const struct of_device_id xlnx_rtc_of_match[] = {
  207. {.compatible = "xlnx,zynqmp-rtc" },
  208. { }
  209. };
  210. MODULE_DEVICE_TABLE(of, xlnx_rtc_of_match);
  211. static struct platform_driver xlnx_rtc_driver = {
  212. .probe = xlnx_rtc_probe,
  213. .remove = xlnx_rtc_remove,
  214. .driver = {
  215. .name = KBUILD_MODNAME,
  216. .pm = &xlnx_rtc_pm_ops,
  217. .of_match_table = xlnx_rtc_of_match,
  218. },
  219. };
  220. module_platform_driver(xlnx_rtc_driver);
  221. MODULE_DESCRIPTION("Xilinx Zynq MPSoC RTC driver");
  222. MODULE_AUTHOR("Xilinx Inc.");
  223. MODULE_LICENSE("GPL v2");