opal-rtc.c 2.5 KB

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