opal-rtc.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. static void opal_to_tm(u32 y_m_d, u64 h_m_s_ms, struct rtc_time *tm)
  19. {
  20. tm->tm_year = ((bcd2bin(y_m_d >> 24) * 100) +
  21. bcd2bin((y_m_d >> 16) & 0xff)) - 1900;
  22. tm->tm_mon = bcd2bin((y_m_d >> 8) & 0xff) - 1;
  23. tm->tm_mday = bcd2bin(y_m_d & 0xff);
  24. tm->tm_hour = bcd2bin((h_m_s_ms >> 56) & 0xff);
  25. tm->tm_min = bcd2bin((h_m_s_ms >> 48) & 0xff);
  26. tm->tm_sec = bcd2bin((h_m_s_ms >> 40) & 0xff);
  27. GregorianDay(tm);
  28. }
  29. unsigned long __init opal_get_boot_time(void)
  30. {
  31. struct rtc_time tm;
  32. u32 y_m_d;
  33. u64 h_m_s_ms;
  34. __be32 __y_m_d;
  35. __be64 __h_m_s_ms;
  36. long rc = OPAL_BUSY;
  37. while (rc == OPAL_BUSY || rc == OPAL_BUSY_EVENT) {
  38. rc = opal_rtc_read(&__y_m_d, &__h_m_s_ms);
  39. if (rc == OPAL_BUSY_EVENT)
  40. opal_poll_events(NULL);
  41. else
  42. mdelay(10);
  43. }
  44. if (rc != OPAL_SUCCESS)
  45. return 0;
  46. y_m_d = be32_to_cpu(__y_m_d);
  47. h_m_s_ms = be64_to_cpu(__h_m_s_ms);
  48. opal_to_tm(y_m_d, h_m_s_ms, &tm);
  49. return mktime(tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
  50. tm.tm_hour, tm.tm_min, tm.tm_sec);
  51. }
  52. void opal_get_rtc_time(struct rtc_time *tm)
  53. {
  54. long rc = OPAL_BUSY;
  55. u32 y_m_d;
  56. u64 h_m_s_ms;
  57. __be32 __y_m_d;
  58. __be64 __h_m_s_ms;
  59. while (rc == OPAL_BUSY || rc == OPAL_BUSY_EVENT) {
  60. rc = opal_rtc_read(&__y_m_d, &__h_m_s_ms);
  61. if (rc == OPAL_BUSY_EVENT)
  62. opal_poll_events(NULL);
  63. else
  64. mdelay(10);
  65. }
  66. if (rc != OPAL_SUCCESS)
  67. return;
  68. y_m_d = be32_to_cpu(__y_m_d);
  69. h_m_s_ms = be64_to_cpu(__h_m_s_ms);
  70. opal_to_tm(y_m_d, h_m_s_ms, tm);
  71. }
  72. int opal_set_rtc_time(struct rtc_time *tm)
  73. {
  74. long rc = OPAL_BUSY;
  75. u32 y_m_d = 0;
  76. u64 h_m_s_ms = 0;
  77. y_m_d |= ((u32)bin2bcd((tm->tm_year + 1900) / 100)) << 24;
  78. y_m_d |= ((u32)bin2bcd((tm->tm_year + 1900) % 100)) << 16;
  79. y_m_d |= ((u32)bin2bcd((tm->tm_mon + 1))) << 8;
  80. y_m_d |= ((u32)bin2bcd(tm->tm_mday));
  81. h_m_s_ms |= ((u64)bin2bcd(tm->tm_hour)) << 56;
  82. h_m_s_ms |= ((u64)bin2bcd(tm->tm_min)) << 48;
  83. h_m_s_ms |= ((u64)bin2bcd(tm->tm_sec)) << 40;
  84. while (rc == OPAL_BUSY || rc == OPAL_BUSY_EVENT) {
  85. rc = opal_rtc_write(y_m_d, h_m_s_ms);
  86. if (rc == OPAL_BUSY_EVENT)
  87. opal_poll_events(NULL);
  88. else
  89. mdelay(10);
  90. }
  91. return rc == OPAL_SUCCESS ? 0 : -EIO;
  92. }