rtas-rtc.c 2.7 KB

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