rtas-rtc.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/kernel.h>
  3. #include <linux/time.h>
  4. #include <linux/timer.h>
  5. #include <linux/init.h>
  6. #include <linux/rtc.h>
  7. #include <linux/delay.h>
  8. #include <linux/ratelimit.h>
  9. #include <asm/prom.h>
  10. #include <asm/rtas.h>
  11. #include <asm/time.h>
  12. #define MAX_RTC_WAIT 5000 /* 5 sec */
  13. #define RTAS_CLOCK_BUSY (-2)
  14. time64_t __init rtas_get_boot_time(void)
  15. {
  16. int ret[8];
  17. int error;
  18. unsigned int wait_time;
  19. u64 max_wait_tb;
  20. max_wait_tb = get_tb() + tb_ticks_per_usec * 1000 * MAX_RTC_WAIT;
  21. do {
  22. error = rtas_call(rtas_token("get-time-of-day"), 0, 8, ret);
  23. wait_time = rtas_busy_delay_time(error);
  24. if (wait_time) {
  25. /* This is boot time so we spin. */
  26. udelay(wait_time*1000);
  27. }
  28. } while (wait_time && (get_tb() < max_wait_tb));
  29. if (error != 0) {
  30. printk_ratelimited(KERN_WARNING
  31. "error: reading the clock failed (%d)\n",
  32. error);
  33. return 0;
  34. }
  35. return mktime64(ret[0], ret[1], ret[2], ret[3], ret[4], ret[5]);
  36. }
  37. /* NOTE: get_rtc_time will get an error if executed in interrupt context
  38. * and if a delay is needed to read the clock. In this case we just
  39. * silently return without updating rtc_tm.
  40. */
  41. void rtas_get_rtc_time(struct rtc_time *rtc_tm)
  42. {
  43. int ret[8];
  44. int error;
  45. unsigned int wait_time;
  46. u64 max_wait_tb;
  47. max_wait_tb = get_tb() + tb_ticks_per_usec * 1000 * MAX_RTC_WAIT;
  48. do {
  49. error = rtas_call(rtas_token("get-time-of-day"), 0, 8, ret);
  50. wait_time = rtas_busy_delay_time(error);
  51. if (wait_time) {
  52. if (in_interrupt()) {
  53. memset(rtc_tm, 0, sizeof(struct rtc_time));
  54. printk_ratelimited(KERN_WARNING
  55. "error: reading clock "
  56. "would delay interrupt\n");
  57. return; /* delay not allowed */
  58. }
  59. msleep(wait_time);
  60. }
  61. } while (wait_time && (get_tb() < max_wait_tb));
  62. if (error != 0) {
  63. printk_ratelimited(KERN_WARNING
  64. "error: reading the clock failed (%d)\n",
  65. error);
  66. return;
  67. }
  68. rtc_tm->tm_sec = ret[5];
  69. rtc_tm->tm_min = ret[4];
  70. rtc_tm->tm_hour = ret[3];
  71. rtc_tm->tm_mday = ret[2];
  72. rtc_tm->tm_mon = ret[1] - 1;
  73. rtc_tm->tm_year = ret[0] - 1900;
  74. }
  75. int rtas_set_rtc_time(struct rtc_time *tm)
  76. {
  77. int error, wait_time;
  78. u64 max_wait_tb;
  79. max_wait_tb = get_tb() + tb_ticks_per_usec * 1000 * MAX_RTC_WAIT;
  80. do {
  81. error = rtas_call(rtas_token("set-time-of-day"), 7, 1, NULL,
  82. tm->tm_year + 1900, tm->tm_mon + 1,
  83. tm->tm_mday, tm->tm_hour, tm->tm_min,
  84. tm->tm_sec, 0);
  85. wait_time = rtas_busy_delay_time(error);
  86. if (wait_time) {
  87. if (in_interrupt())
  88. return 1; /* probably decrementer */
  89. msleep(wait_time);
  90. }
  91. } while (wait_time && (get_tb() < max_wait_tb));
  92. if (error != 0)
  93. printk_ratelimited(KERN_WARNING
  94. "error: setting the clock failed (%d)\n",
  95. error);
  96. return 0;
  97. }