rtc-imx-sc.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright 2018 NXP.
  4. */
  5. #include <dt-bindings/firmware/imx/rsrc.h>
  6. #include <linux/arm-smccc.h>
  7. #include <linux/firmware/imx/sci.h>
  8. #include <linux/module.h>
  9. #include <linux/of.h>
  10. #include <linux/platform_device.h>
  11. #include <linux/rtc.h>
  12. #define IMX_SC_TIMER_FUNC_GET_RTC_SEC1970 9
  13. #define IMX_SC_TIMER_FUNC_SET_RTC_ALARM 8
  14. #define IMX_SC_TIMER_FUNC_SET_RTC_TIME 6
  15. #define IMX_SIP_SRTC 0xC2000002
  16. #define IMX_SIP_SRTC_SET_TIME 0x0
  17. #define SC_IRQ_GROUP_RTC 2
  18. #define SC_IRQ_RTC 1
  19. static struct imx_sc_ipc *rtc_ipc_handle;
  20. static struct rtc_device *imx_sc_rtc;
  21. struct imx_sc_msg_timer_get_rtc_time {
  22. struct imx_sc_rpc_msg hdr;
  23. u32 time;
  24. } __packed;
  25. struct imx_sc_msg_timer_rtc_set_alarm {
  26. struct imx_sc_rpc_msg hdr;
  27. u16 year;
  28. u8 mon;
  29. u8 day;
  30. u8 hour;
  31. u8 min;
  32. u8 sec;
  33. } __packed __aligned(4);
  34. static int imx_sc_rtc_read_time(struct device *dev, struct rtc_time *tm)
  35. {
  36. struct imx_sc_msg_timer_get_rtc_time msg;
  37. struct imx_sc_rpc_msg *hdr = &msg.hdr;
  38. int ret;
  39. hdr->ver = IMX_SC_RPC_VERSION;
  40. hdr->svc = IMX_SC_RPC_SVC_TIMER;
  41. hdr->func = IMX_SC_TIMER_FUNC_GET_RTC_SEC1970;
  42. hdr->size = 1;
  43. ret = imx_scu_call_rpc(rtc_ipc_handle, &msg, true);
  44. if (ret) {
  45. dev_err(dev, "read rtc time failed, ret %d\n", ret);
  46. return ret;
  47. }
  48. rtc_time64_to_tm(msg.time, tm);
  49. return 0;
  50. }
  51. static int imx_sc_rtc_set_time(struct device *dev, struct rtc_time *tm)
  52. {
  53. struct arm_smccc_res res;
  54. /* pack 2 time parameters into 1 register, 16 bits for each */
  55. arm_smccc_smc(IMX_SIP_SRTC, IMX_SIP_SRTC_SET_TIME,
  56. ((tm->tm_year + 1900) << 16) | (tm->tm_mon + 1),
  57. (tm->tm_mday << 16) | tm->tm_hour,
  58. (tm->tm_min << 16) | tm->tm_sec,
  59. 0, 0, 0, &res);
  60. return res.a0;
  61. }
  62. static int imx_sc_rtc_alarm_irq_enable(struct device *dev, unsigned int enable)
  63. {
  64. return imx_scu_irq_group_enable(SC_IRQ_GROUP_RTC, SC_IRQ_RTC, enable);
  65. }
  66. static int imx_sc_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
  67. {
  68. /*
  69. * SCU firmware does NOT provide read alarm API, but .read_alarm
  70. * callback is required by RTC framework to support alarm function,
  71. * so just return here.
  72. */
  73. return 0;
  74. }
  75. static int imx_sc_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
  76. {
  77. struct imx_sc_msg_timer_rtc_set_alarm msg;
  78. struct imx_sc_rpc_msg *hdr = &msg.hdr;
  79. int ret;
  80. struct rtc_time *alrm_tm = &alrm->time;
  81. hdr->ver = IMX_SC_RPC_VERSION;
  82. hdr->svc = IMX_SC_RPC_SVC_TIMER;
  83. hdr->func = IMX_SC_TIMER_FUNC_SET_RTC_ALARM;
  84. hdr->size = 3;
  85. msg.year = alrm_tm->tm_year + 1900;
  86. msg.mon = alrm_tm->tm_mon + 1;
  87. msg.day = alrm_tm->tm_mday;
  88. msg.hour = alrm_tm->tm_hour;
  89. msg.min = alrm_tm->tm_min;
  90. msg.sec = alrm_tm->tm_sec;
  91. ret = imx_scu_call_rpc(rtc_ipc_handle, &msg, true);
  92. if (ret) {
  93. dev_err(dev, "set rtc alarm failed, ret %d\n", ret);
  94. return ret;
  95. }
  96. ret = imx_sc_rtc_alarm_irq_enable(dev, alrm->enabled);
  97. if (ret) {
  98. dev_err(dev, "enable rtc alarm failed, ret %d\n", ret);
  99. return ret;
  100. }
  101. return 0;
  102. }
  103. static const struct rtc_class_ops imx_sc_rtc_ops = {
  104. .read_time = imx_sc_rtc_read_time,
  105. .set_time = imx_sc_rtc_set_time,
  106. .read_alarm = imx_sc_rtc_read_alarm,
  107. .set_alarm = imx_sc_rtc_set_alarm,
  108. .alarm_irq_enable = imx_sc_rtc_alarm_irq_enable,
  109. };
  110. static int imx_sc_rtc_alarm_notify(struct notifier_block *nb,
  111. unsigned long event, void *group)
  112. {
  113. /* ignore non-rtc irq */
  114. if (!((event & SC_IRQ_RTC) && (*(u8 *)group == SC_IRQ_GROUP_RTC)))
  115. return 0;
  116. rtc_update_irq(imx_sc_rtc, 1, RTC_IRQF | RTC_AF);
  117. return 0;
  118. }
  119. static struct notifier_block imx_sc_rtc_alarm_sc_notifier = {
  120. .notifier_call = imx_sc_rtc_alarm_notify,
  121. };
  122. static int imx_sc_rtc_probe(struct platform_device *pdev)
  123. {
  124. int ret;
  125. ret = imx_scu_get_handle(&rtc_ipc_handle);
  126. if (ret)
  127. return ret;
  128. device_init_wakeup(&pdev->dev, true);
  129. imx_sc_rtc = devm_rtc_allocate_device(&pdev->dev);
  130. if (IS_ERR(imx_sc_rtc))
  131. return PTR_ERR(imx_sc_rtc);
  132. imx_sc_rtc->ops = &imx_sc_rtc_ops;
  133. imx_sc_rtc->range_min = 0;
  134. imx_sc_rtc->range_max = U32_MAX;
  135. ret = rtc_register_device(imx_sc_rtc);
  136. if (ret)
  137. return ret;
  138. imx_scu_irq_register_notifier(&imx_sc_rtc_alarm_sc_notifier);
  139. return 0;
  140. }
  141. static const struct of_device_id imx_sc_dt_ids[] = {
  142. { .compatible = "fsl,imx8qxp-sc-rtc", },
  143. {}
  144. };
  145. MODULE_DEVICE_TABLE(of, imx_sc_dt_ids);
  146. static struct platform_driver imx_sc_rtc_driver = {
  147. .driver = {
  148. .name = "imx-sc-rtc",
  149. .of_match_table = imx_sc_dt_ids,
  150. },
  151. .probe = imx_sc_rtc_probe,
  152. };
  153. module_platform_driver(imx_sc_rtc_driver);
  154. MODULE_AUTHOR("Anson Huang <Anson.Huang@nxp.com>");
  155. MODULE_DESCRIPTION("NXP i.MX System Controller RTC Driver");
  156. MODULE_LICENSE("GPL");