rtc-zynqmp.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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_BATT_EN BIT(31)
  46. #define RTC_CALIB_DEF 0x198233
  47. #define RTC_CALIB_MASK 0x1FFFFF
  48. #define RTC_SEC_MAX_VAL 0xFFFFFFFF
  49. struct xlnx_rtc_dev {
  50. struct rtc_device *rtc;
  51. void __iomem *reg_base;
  52. int alarm_irq;
  53. int sec_irq;
  54. int calibval;
  55. };
  56. static int xlnx_rtc_set_time(struct device *dev, struct rtc_time *tm)
  57. {
  58. struct xlnx_rtc_dev *xrtcdev = dev_get_drvdata(dev);
  59. unsigned long new_time;
  60. /*
  61. * The value written will be updated after 1 sec into the
  62. * seconds read register, so we need to program time +1 sec
  63. * to get the correct time on read.
  64. */
  65. new_time = rtc_tm_to_time64(tm) + 1;
  66. if (new_time > RTC_SEC_MAX_VAL)
  67. return -EINVAL;
  68. /*
  69. * Writing into calibration register will clear the Tick Counter and
  70. * force the next second to be signaled exactly in 1 second period
  71. */
  72. xrtcdev->calibval &= RTC_CALIB_MASK;
  73. writel(xrtcdev->calibval, (xrtcdev->reg_base + RTC_CALIB_WR));
  74. writel(new_time, xrtcdev->reg_base + RTC_SET_TM_WR);
  75. /*
  76. * Clear the rtc interrupt status register after setting the
  77. * time. During a read_time function, the code should read the
  78. * RTC_INT_STATUS register and if bit 0 is still 0, it means
  79. * that one second has not elapsed yet since RTC was set and
  80. * the current time should be read from SET_TIME_READ register;
  81. * otherwise, CURRENT_TIME register is read to report the time
  82. */
  83. writel(RTC_INT_SEC, xrtcdev->reg_base + RTC_INT_STS);
  84. return 0;
  85. }
  86. static int xlnx_rtc_read_time(struct device *dev, struct rtc_time *tm)
  87. {
  88. u32 status;
  89. unsigned long read_time;
  90. struct xlnx_rtc_dev *xrtcdev = dev_get_drvdata(dev);
  91. status = readl(xrtcdev->reg_base + RTC_INT_STS);
  92. if (status & RTC_INT_SEC) {
  93. /*
  94. * RTC has updated the CURRENT_TIME with the time written into
  95. * SET_TIME_WRITE register.
  96. */
  97. rtc_time64_to_tm(readl(xrtcdev->reg_base + RTC_CUR_TM), tm);
  98. } else {
  99. /*
  100. * Time written in SET_TIME_WRITE has not yet updated into
  101. * the seconds read register, so read the time from the
  102. * SET_TIME_WRITE instead of CURRENT_TIME register.
  103. * Since we add +1 sec while writing, we need to -1 sec while
  104. * reading.
  105. */
  106. read_time = readl(xrtcdev->reg_base + RTC_SET_TM_RD) - 1;
  107. rtc_time64_to_tm(read_time, tm);
  108. }
  109. return rtc_valid_tm(tm);
  110. }
  111. static int xlnx_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
  112. {
  113. struct xlnx_rtc_dev *xrtcdev = dev_get_drvdata(dev);
  114. rtc_time64_to_tm(readl(xrtcdev->reg_base + RTC_ALRM), &alrm->time);
  115. alrm->enabled = readl(xrtcdev->reg_base + RTC_INT_MASK) & RTC_INT_ALRM;
  116. return 0;
  117. }
  118. static int xlnx_rtc_alarm_irq_enable(struct device *dev, u32 enabled)
  119. {
  120. struct xlnx_rtc_dev *xrtcdev = dev_get_drvdata(dev);
  121. if (enabled)
  122. writel(RTC_INT_ALRM, xrtcdev->reg_base + RTC_INT_EN);
  123. else
  124. writel(RTC_INT_ALRM, xrtcdev->reg_base + RTC_INT_DIS);
  125. return 0;
  126. }
  127. static int xlnx_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
  128. {
  129. struct xlnx_rtc_dev *xrtcdev = dev_get_drvdata(dev);
  130. unsigned long alarm_time;
  131. alarm_time = rtc_tm_to_time64(&alrm->time);
  132. if (alarm_time > RTC_SEC_MAX_VAL)
  133. return -EINVAL;
  134. writel((u32)alarm_time, (xrtcdev->reg_base + RTC_ALRM));
  135. xlnx_rtc_alarm_irq_enable(dev, alrm->enabled);
  136. return 0;
  137. }
  138. static void xlnx_init_rtc(struct xlnx_rtc_dev *xrtcdev)
  139. {
  140. u32 rtc_ctrl;
  141. /* Enable RTC switch to battery when VCC_PSAUX is not available */
  142. rtc_ctrl = readl(xrtcdev->reg_base + RTC_CTRL);
  143. rtc_ctrl |= RTC_BATT_EN;
  144. writel(rtc_ctrl, xrtcdev->reg_base + RTC_CTRL);
  145. /*
  146. * Based on crystal freq of 33.330 KHz
  147. * set the seconds counter and enable, set fractions counter
  148. * to default value suggested as per design spec
  149. * to correct RTC delay in frequency over period of time.
  150. */
  151. xrtcdev->calibval &= RTC_CALIB_MASK;
  152. writel(xrtcdev->calibval, (xrtcdev->reg_base + RTC_CALIB_WR));
  153. }
  154. static const struct rtc_class_ops xlnx_rtc_ops = {
  155. .set_time = xlnx_rtc_set_time,
  156. .read_time = xlnx_rtc_read_time,
  157. .read_alarm = xlnx_rtc_read_alarm,
  158. .set_alarm = xlnx_rtc_set_alarm,
  159. .alarm_irq_enable = xlnx_rtc_alarm_irq_enable,
  160. };
  161. static irqreturn_t xlnx_rtc_interrupt(int irq, void *id)
  162. {
  163. struct xlnx_rtc_dev *xrtcdev = (struct xlnx_rtc_dev *)id;
  164. unsigned int status;
  165. status = readl(xrtcdev->reg_base + RTC_INT_STS);
  166. /* Check if interrupt asserted */
  167. if (!(status & (RTC_INT_SEC | RTC_INT_ALRM)))
  168. return IRQ_NONE;
  169. /* Clear RTC_INT_ALRM interrupt only */
  170. writel(RTC_INT_ALRM, xrtcdev->reg_base + RTC_INT_STS);
  171. if (status & RTC_INT_ALRM)
  172. rtc_update_irq(xrtcdev->rtc, 1, RTC_IRQF | RTC_AF);
  173. return IRQ_HANDLED;
  174. }
  175. static int xlnx_rtc_probe(struct platform_device *pdev)
  176. {
  177. struct xlnx_rtc_dev *xrtcdev;
  178. struct resource *res;
  179. int ret;
  180. xrtcdev = devm_kzalloc(&pdev->dev, sizeof(*xrtcdev), GFP_KERNEL);
  181. if (!xrtcdev)
  182. return -ENOMEM;
  183. platform_set_drvdata(pdev, xrtcdev);
  184. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  185. xrtcdev->reg_base = devm_ioremap_resource(&pdev->dev, res);
  186. if (IS_ERR(xrtcdev->reg_base))
  187. return PTR_ERR(xrtcdev->reg_base);
  188. xrtcdev->alarm_irq = platform_get_irq_byname(pdev, "alarm");
  189. if (xrtcdev->alarm_irq < 0) {
  190. dev_err(&pdev->dev, "no irq resource\n");
  191. return xrtcdev->alarm_irq;
  192. }
  193. ret = devm_request_irq(&pdev->dev, xrtcdev->alarm_irq,
  194. xlnx_rtc_interrupt, 0,
  195. dev_name(&pdev->dev), xrtcdev);
  196. if (ret) {
  197. dev_err(&pdev->dev, "request irq failed\n");
  198. return ret;
  199. }
  200. xrtcdev->sec_irq = platform_get_irq_byname(pdev, "sec");
  201. if (xrtcdev->sec_irq < 0) {
  202. dev_err(&pdev->dev, "no irq resource\n");
  203. return xrtcdev->sec_irq;
  204. }
  205. ret = devm_request_irq(&pdev->dev, xrtcdev->sec_irq,
  206. xlnx_rtc_interrupt, 0,
  207. dev_name(&pdev->dev), xrtcdev);
  208. if (ret) {
  209. dev_err(&pdev->dev, "request irq failed\n");
  210. return ret;
  211. }
  212. ret = of_property_read_u32(pdev->dev.of_node, "calibration",
  213. &xrtcdev->calibval);
  214. if (ret)
  215. xrtcdev->calibval = RTC_CALIB_DEF;
  216. xlnx_init_rtc(xrtcdev);
  217. device_init_wakeup(&pdev->dev, 1);
  218. xrtcdev->rtc = devm_rtc_device_register(&pdev->dev, pdev->name,
  219. &xlnx_rtc_ops, THIS_MODULE);
  220. return PTR_ERR_OR_ZERO(xrtcdev->rtc);
  221. }
  222. static int xlnx_rtc_remove(struct platform_device *pdev)
  223. {
  224. xlnx_rtc_alarm_irq_enable(&pdev->dev, 0);
  225. device_init_wakeup(&pdev->dev, 0);
  226. return 0;
  227. }
  228. static int __maybe_unused xlnx_rtc_suspend(struct device *dev)
  229. {
  230. struct platform_device *pdev = to_platform_device(dev);
  231. struct xlnx_rtc_dev *xrtcdev = platform_get_drvdata(pdev);
  232. if (device_may_wakeup(&pdev->dev))
  233. enable_irq_wake(xrtcdev->alarm_irq);
  234. else
  235. xlnx_rtc_alarm_irq_enable(dev, 0);
  236. return 0;
  237. }
  238. static int __maybe_unused xlnx_rtc_resume(struct device *dev)
  239. {
  240. struct platform_device *pdev = to_platform_device(dev);
  241. struct xlnx_rtc_dev *xrtcdev = platform_get_drvdata(pdev);
  242. if (device_may_wakeup(&pdev->dev))
  243. disable_irq_wake(xrtcdev->alarm_irq);
  244. else
  245. xlnx_rtc_alarm_irq_enable(dev, 1);
  246. return 0;
  247. }
  248. static SIMPLE_DEV_PM_OPS(xlnx_rtc_pm_ops, xlnx_rtc_suspend, xlnx_rtc_resume);
  249. static const struct of_device_id xlnx_rtc_of_match[] = {
  250. {.compatible = "xlnx,zynqmp-rtc" },
  251. { }
  252. };
  253. MODULE_DEVICE_TABLE(of, xlnx_rtc_of_match);
  254. static struct platform_driver xlnx_rtc_driver = {
  255. .probe = xlnx_rtc_probe,
  256. .remove = xlnx_rtc_remove,
  257. .driver = {
  258. .name = KBUILD_MODNAME,
  259. .pm = &xlnx_rtc_pm_ops,
  260. .of_match_table = xlnx_rtc_of_match,
  261. },
  262. };
  263. module_platform_driver(xlnx_rtc_driver);
  264. MODULE_DESCRIPTION("Xilinx Zynq MPSoC RTC driver");
  265. MODULE_AUTHOR("Xilinx Inc.");
  266. MODULE_LICENSE("GPL v2");