rtc-goldfish.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. /*
  2. * Copyright (c) 2006-2022, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2022-11-26 GuEe-GUI first version
  9. */
  10. #include "rtc_dm.h"
  11. #define GOLDFISH_RTC_TIME_LOW 0x00 /* get low bits of current time and update GOLDFISH_RTC_TIME_HIGH */
  12. #define GOLDFISH_RTC_TIME_HIGH 0x04 /* get high bits of time at last GOLDFISH_RTC_TIME_LOW read */
  13. #define GOLDFISH_RTC_ALARM_LOW 0x08 /* set low bits of alarm and activate it */
  14. #define GOLDFISH_RTC_ALARM_HIGH 0x0c /* set high bits of next alarm */
  15. #define GOLDFISH_RTC_IRQ_ENABLED 0x10 /* enable alarm interrupt */
  16. #define GOLDFISH_RTC_CLEAR_ALARM 0x14 /* disarm an existing alarm */
  17. #define GOLDFISH_RTC_ALARM_STATUS 0x18 /* alarm status (running or not) */
  18. #define GOLDFISH_RTC_CLEAR_INTERRUPT 0x1c /* clear interrupt */
  19. #define NSEC_PER_SEC 1000000000L
  20. struct goldfish_rtc
  21. {
  22. struct rt_device parent;
  23. int irq;
  24. void *base;
  25. struct rt_rtc_wkalarm wkalarm;
  26. };
  27. #define raw_to_goldfish_rtc(raw) rt_container_of(raw, struct goldfish_rtc, parent)
  28. rt_inline rt_uint32_t goldfish_rtc_read(struct goldfish_rtc *grtc, int offset)
  29. {
  30. return HWREG32(grtc->base + offset);
  31. }
  32. rt_inline void goldfish_rtc_write(struct goldfish_rtc *grtc, int offset, rt_uint32_t value)
  33. {
  34. HWREG32(grtc->base + offset) = value;
  35. }
  36. static void goldfish_rtc_isr(int irqno, void *param)
  37. {
  38. struct goldfish_rtc *grtc = param;
  39. goldfish_rtc_write(grtc, GOLDFISH_RTC_CLEAR_INTERRUPT, 1);
  40. rt_alarm_update(&grtc->parent, 1);
  41. }
  42. static void goldfish_rtc_get_secs(struct goldfish_rtc *grtc, time_t *sec)
  43. {
  44. rt_uint64_t time = goldfish_rtc_read(grtc, GOLDFISH_RTC_TIME_LOW);
  45. if (sizeof(*sec) >= sizeof(rt_uint64_t))
  46. {
  47. rt_uint64_t time_high = goldfish_rtc_read(grtc, GOLDFISH_RTC_TIME_HIGH);
  48. time |= time_high << 32;
  49. }
  50. rt_do_div(time, NSEC_PER_SEC);
  51. rt_memcpy(sec, &time, sizeof(*sec));
  52. }
  53. static void goldfish_rtc_set_secs(struct goldfish_rtc *grtc, time_t *sec)
  54. {
  55. rt_uint64_t time = 0;
  56. rt_memcpy(&time, sec, sizeof(*sec));
  57. time *= NSEC_PER_SEC;
  58. goldfish_rtc_write(grtc, GOLDFISH_RTC_TIME_LOW, (rt_uint32_t)(time & RT_UINT32_MAX));
  59. if (sizeof(*sec) >= sizeof(rt_uint64_t))
  60. {
  61. goldfish_rtc_write(grtc, GOLDFISH_RTC_TIME_HIGH, (rt_uint32_t)(time >> 32));
  62. }
  63. }
  64. static void goldfish_rtc_get_alarm(struct goldfish_rtc *grtc, struct rt_rtc_wkalarm *alarm)
  65. {
  66. *alarm = grtc->wkalarm;
  67. if (goldfish_rtc_read(grtc, GOLDFISH_RTC_ALARM_STATUS))
  68. {
  69. alarm->enable = RT_TRUE;
  70. }
  71. else
  72. {
  73. alarm->enable = RT_FALSE;
  74. }
  75. }
  76. static void goldfish_rtc_set_alarm(struct goldfish_rtc *grtc, struct rt_rtc_wkalarm *alarm)
  77. {
  78. struct rt_rtc_wkalarm *wkalarm = &grtc->wkalarm;
  79. wkalarm->enable = alarm->enable;
  80. wkalarm->tm_hour = alarm->tm_hour;
  81. wkalarm->tm_min = alarm->tm_min;
  82. wkalarm->tm_sec = alarm->tm_sec;
  83. if (alarm->enable)
  84. {
  85. rt_uint64_t time = alarm->tm_hour * 3600 + alarm->tm_min * 60 + alarm->tm_sec;
  86. time *= NSEC_PER_SEC;
  87. goldfish_rtc_write(grtc, GOLDFISH_RTC_ALARM_HIGH, (rt_uint32_t)(time >> 32));
  88. goldfish_rtc_write(grtc, GOLDFISH_RTC_ALARM_LOW, (rt_uint32_t)(time & RT_UINT32_MAX));
  89. goldfish_rtc_write(grtc, GOLDFISH_RTC_IRQ_ENABLED, 1);
  90. }
  91. else
  92. {
  93. if (goldfish_rtc_read(grtc, GOLDFISH_RTC_ALARM_STATUS))
  94. {
  95. goldfish_rtc_write(grtc, GOLDFISH_RTC_CLEAR_ALARM, 1);
  96. }
  97. goldfish_rtc_write(grtc, GOLDFISH_RTC_IRQ_ENABLED, 0);
  98. }
  99. }
  100. static rt_err_t goldfish_rtc_control(rt_device_t dev, int cmd, void *args)
  101. {
  102. rt_err_t err = RT_EOK;
  103. struct goldfish_rtc *grtc = raw_to_goldfish_rtc(dev);
  104. if (!args)
  105. {
  106. return -RT_EINVAL;
  107. }
  108. switch (cmd)
  109. {
  110. case RT_DEVICE_CTRL_RTC_GET_TIME:
  111. goldfish_rtc_get_secs(grtc, args);
  112. break;
  113. case RT_DEVICE_CTRL_RTC_SET_TIME:
  114. goldfish_rtc_set_secs(grtc, args);
  115. break;
  116. case RT_DEVICE_CTRL_RTC_GET_TIMEVAL:
  117. goldfish_rtc_get_secs(grtc, (time_t *)&((struct timeval *)args)->tv_sec);
  118. break;
  119. case RT_DEVICE_CTRL_RTC_SET_TIMEVAL:
  120. goldfish_rtc_set_secs(grtc, (time_t *)&((struct timeval *)args)->tv_sec);
  121. break;
  122. case RT_DEVICE_CTRL_RTC_GET_ALARM:
  123. goldfish_rtc_get_alarm(grtc, args);
  124. break;
  125. case RT_DEVICE_CTRL_RTC_SET_ALARM:
  126. goldfish_rtc_set_alarm(grtc, args);
  127. break;
  128. default:
  129. err = -RT_EINVAL;
  130. break;
  131. }
  132. return err;
  133. }
  134. #ifdef RT_USING_DEVICE_OPS
  135. const static struct rt_device_ops goldfish_rtc_ops =
  136. {
  137. .control = goldfish_rtc_control,
  138. };
  139. #endif
  140. static rt_err_t goldfish_rtc_probe(struct rt_platform_device *pdev)
  141. {
  142. rt_err_t err = RT_EOK;
  143. const char *dev_name;
  144. struct rt_device *dev = &pdev->parent;
  145. struct goldfish_rtc *grtc = rt_calloc(1, sizeof(*grtc));
  146. if (!grtc)
  147. {
  148. return -RT_ENOMEM;
  149. }
  150. grtc->base = rt_dm_dev_iomap(dev, 0);
  151. if (!grtc->base)
  152. {
  153. err = -RT_EIO;
  154. goto _fail;
  155. }
  156. grtc->irq = rt_dm_dev_get_irq(dev, 0);
  157. if (grtc->irq < 0)
  158. {
  159. err = grtc->irq;
  160. goto _fail;
  161. }
  162. dev->user_data = grtc;
  163. grtc->parent.type = RT_Device_Class_RTC;
  164. #ifdef RT_USING_DEVICE_OPS
  165. grtc->parent.ops = &goldfish_rtc_ops;
  166. #else
  167. grtc->parent.control = goldfish_rtc_control;
  168. #endif
  169. rtc_dev_set_name(&grtc->parent);
  170. dev_name = rt_dm_dev_get_name(&grtc->parent);
  171. rt_device_register(&grtc->parent, dev_name, RT_DEVICE_FLAG_RDWR);
  172. rt_hw_interrupt_install(grtc->irq, goldfish_rtc_isr, grtc, "rtc-goldfish");
  173. rt_hw_interrupt_umask(grtc->irq);
  174. return RT_EOK;
  175. _fail:
  176. if (grtc->base)
  177. {
  178. rt_iounmap(grtc->base);
  179. }
  180. rt_free(grtc);
  181. return err;
  182. }
  183. static rt_err_t goldfish_rtc_remove(struct rt_platform_device *pdev)
  184. {
  185. struct goldfish_rtc *grtc = pdev->parent.user_data;
  186. rt_hw_interrupt_mask(grtc->irq);
  187. rt_pic_detach_irq(grtc->irq, grtc);
  188. rt_device_unregister(&grtc->parent);
  189. rt_iounmap(grtc->base);
  190. rt_free(grtc);
  191. return RT_EOK;
  192. }
  193. static const struct rt_ofw_node_id goldfish_rtc_ofw_ids[] =
  194. {
  195. { .compatible = "google,goldfish-rtc" },
  196. { /* sentinel */ }
  197. };
  198. static struct rt_platform_driver goldfish_rtc_driver =
  199. {
  200. .name = "rtc-goldfish",
  201. .ids = goldfish_rtc_ofw_ids,
  202. .probe = goldfish_rtc_probe,
  203. .remove = goldfish_rtc_remove,
  204. };
  205. RT_PLATFORM_DRIVER_EXPORT(goldfish_rtc_driver);