opal-rtc.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. while (rc == OPAL_BUSY || rc == OPAL_BUSY_EVENT) {
  39. rc = opal_rtc_read(&__y_m_d, &__h_m_s_ms);
  40. if (rc == OPAL_BUSY_EVENT)
  41. opal_poll_events(NULL);
  42. else
  43. mdelay(10);
  44. }
  45. if (rc != OPAL_SUCCESS) {
  46. ppc_md.get_rtc_time = NULL;
  47. ppc_md.set_rtc_time = NULL;
  48. return 0;
  49. }
  50. y_m_d = be32_to_cpu(__y_m_d);
  51. h_m_s_ms = be64_to_cpu(__h_m_s_ms);
  52. opal_to_tm(y_m_d, h_m_s_ms, &tm);
  53. return mktime(tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
  54. tm.tm_hour, tm.tm_min, tm.tm_sec);
  55. }
  56. void opal_get_rtc_time(struct rtc_time *tm)
  57. {
  58. long rc = OPAL_BUSY;
  59. u32 y_m_d;
  60. u64 h_m_s_ms;
  61. __be32 __y_m_d;
  62. __be64 __h_m_s_ms;
  63. while (rc == OPAL_BUSY || rc == OPAL_BUSY_EVENT) {
  64. rc = opal_rtc_read(&__y_m_d, &__h_m_s_ms);
  65. if (rc == OPAL_BUSY_EVENT)
  66. opal_poll_events(NULL);
  67. else
  68. mdelay(10);
  69. }
  70. if (rc != OPAL_SUCCESS)
  71. return;
  72. y_m_d = be32_to_cpu(__y_m_d);
  73. h_m_s_ms = be64_to_cpu(__h_m_s_ms);
  74. opal_to_tm(y_m_d, h_m_s_ms, tm);
  75. }
  76. int opal_set_rtc_time(struct rtc_time *tm)
  77. {
  78. long rc = OPAL_BUSY;
  79. u32 y_m_d = 0;
  80. u64 h_m_s_ms = 0;
  81. y_m_d |= ((u32)bin2bcd((tm->tm_year + 1900) / 100)) << 24;
  82. y_m_d |= ((u32)bin2bcd((tm->tm_year + 1900) % 100)) << 16;
  83. y_m_d |= ((u32)bin2bcd((tm->tm_mon + 1))) << 8;
  84. y_m_d |= ((u32)bin2bcd(tm->tm_mday));
  85. h_m_s_ms |= ((u64)bin2bcd(tm->tm_hour)) << 56;
  86. h_m_s_ms |= ((u64)bin2bcd(tm->tm_min)) << 48;
  87. h_m_s_ms |= ((u64)bin2bcd(tm->tm_sec)) << 40;
  88. while (rc == OPAL_BUSY || rc == OPAL_BUSY_EVENT) {
  89. rc = opal_rtc_write(y_m_d, h_m_s_ms);
  90. if (rc == OPAL_BUSY_EVENT)
  91. opal_poll_events(NULL);
  92. else
  93. mdelay(10);
  94. }
  95. return rc == OPAL_SUCCESS ? 0 : -EIO;
  96. }