rtas-rtc.c 2.7 KB

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