rtas-rtc.c 2.7 KB

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