opal-rtc.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*
  2. * PowerNV Real Time Clock.
  3. *
  4. * Copyright 2011 IBM Corp.
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the License, or (at your option) any later version.
  10. */
  11. #include <linux/kernel.h>
  12. #include <linux/time.h>
  13. #include <linux/bcd.h>
  14. #include <linux/rtc.h>
  15. #include <linux/delay.h>
  16. #include <asm/opal.h>
  17. #include <asm/firmware.h>
  18. #include <asm/machdep.h>
  19. static void opal_to_tm(u32 y_m_d, u64 h_m_s_ms, struct rtc_time *tm)
  20. {
  21. tm->tm_year = ((bcd2bin(y_m_d >> 24) * 100) +
  22. bcd2bin((y_m_d >> 16) & 0xff)) - 1900;
  23. tm->tm_mon = bcd2bin((y_m_d >> 8) & 0xff) - 1;
  24. tm->tm_mday = bcd2bin(y_m_d & 0xff);
  25. tm->tm_hour = bcd2bin((h_m_s_ms >> 56) & 0xff);
  26. tm->tm_min = bcd2bin((h_m_s_ms >> 48) & 0xff);
  27. tm->tm_sec = bcd2bin((h_m_s_ms >> 40) & 0xff);
  28. GregorianDay(tm);
  29. }
  30. unsigned long __init opal_get_boot_time(void)
  31. {
  32. struct rtc_time tm;
  33. u32 y_m_d;
  34. u64 h_m_s_ms;
  35. __be32 __y_m_d;
  36. __be64 __h_m_s_ms;
  37. long rc = OPAL_BUSY;
  38. if (!opal_check_token(OPAL_RTC_READ))
  39. goto out;
  40. while (rc == OPAL_BUSY || rc == OPAL_BUSY_EVENT) {
  41. rc = opal_rtc_read(&__y_m_d, &__h_m_s_ms);
  42. if (rc == OPAL_BUSY_EVENT)
  43. opal_poll_events(NULL);
  44. else
  45. mdelay(10);
  46. }
  47. if (rc != OPAL_SUCCESS)
  48. goto out;
  49. y_m_d = be32_to_cpu(__y_m_d);
  50. h_m_s_ms = be64_to_cpu(__h_m_s_ms);
  51. opal_to_tm(y_m_d, h_m_s_ms, &tm);
  52. return mktime(tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
  53. tm.tm_hour, tm.tm_min, tm.tm_sec);
  54. out:
  55. ppc_md.get_rtc_time = NULL;
  56. ppc_md.set_rtc_time = NULL;
  57. return 0;
  58. }
  59. void opal_get_rtc_time(struct rtc_time *tm)
  60. {
  61. long rc = OPAL_BUSY;
  62. u32 y_m_d;
  63. u64 h_m_s_ms;
  64. __be32 __y_m_d;
  65. __be64 __h_m_s_ms;
  66. while (rc == OPAL_BUSY || rc == OPAL_BUSY_EVENT) {
  67. rc = opal_rtc_read(&__y_m_d, &__h_m_s_ms);
  68. if (rc == OPAL_BUSY_EVENT)
  69. opal_poll_events(NULL);
  70. else
  71. mdelay(10);
  72. }
  73. if (rc != OPAL_SUCCESS)
  74. return;
  75. y_m_d = be32_to_cpu(__y_m_d);
  76. h_m_s_ms = be64_to_cpu(__h_m_s_ms);
  77. opal_to_tm(y_m_d, h_m_s_ms, tm);
  78. }
  79. int opal_set_rtc_time(struct rtc_time *tm)
  80. {
  81. long rc = OPAL_BUSY;
  82. u32 y_m_d = 0;
  83. u64 h_m_s_ms = 0;
  84. y_m_d |= ((u32)bin2bcd((tm->tm_year + 1900) / 100)) << 24;
  85. y_m_d |= ((u32)bin2bcd((tm->tm_year + 1900) % 100)) << 16;
  86. y_m_d |= ((u32)bin2bcd((tm->tm_mon + 1))) << 8;
  87. y_m_d |= ((u32)bin2bcd(tm->tm_mday));
  88. h_m_s_ms |= ((u64)bin2bcd(tm->tm_hour)) << 56;
  89. h_m_s_ms |= ((u64)bin2bcd(tm->tm_min)) << 48;
  90. h_m_s_ms |= ((u64)bin2bcd(tm->tm_sec)) << 40;
  91. while (rc == OPAL_BUSY || rc == OPAL_BUSY_EVENT) {
  92. rc = opal_rtc_write(y_m_d, h_m_s_ms);
  93. if (rc == OPAL_BUSY_EVENT)
  94. opal_poll_events(NULL);
  95. else
  96. mdelay(10);
  97. }
  98. return rc == OPAL_SUCCESS ? 0 : -EIO;
  99. }