opal-rtc.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * PowerNV Real Time Clock.
  4. *
  5. * Copyright 2011 IBM Corp.
  6. */
  7. #include <linux/kernel.h>
  8. #include <linux/time.h>
  9. #include <linux/bcd.h>
  10. #include <linux/rtc.h>
  11. #include <linux/delay.h>
  12. #include <linux/of.h>
  13. #include <linux/of_platform.h>
  14. #include <linux/platform_device.h>
  15. #include <asm/opal.h>
  16. #include <asm/firmware.h>
  17. #include <asm/machdep.h>
  18. static void __init 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. tm->tm_wday = -1;
  28. }
  29. time64_t __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. if (!opal_check_token(OPAL_RTC_READ))
  38. return 0;
  39. while (rc == OPAL_BUSY || rc == OPAL_BUSY_EVENT) {
  40. rc = opal_rtc_read(&__y_m_d, &__h_m_s_ms);
  41. if (rc == OPAL_BUSY_EVENT) {
  42. mdelay(OPAL_BUSY_DELAY_MS);
  43. opal_poll_events(NULL);
  44. } else if (rc == OPAL_BUSY) {
  45. mdelay(OPAL_BUSY_DELAY_MS);
  46. }
  47. }
  48. if (rc != OPAL_SUCCESS)
  49. return 0;
  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 rtc_tm_to_time64(&tm);
  54. }
  55. static __init int opal_time_init(void)
  56. {
  57. struct platform_device *pdev;
  58. struct device_node *rtc;
  59. rtc = of_find_node_by_path("/ibm,opal/rtc");
  60. if (rtc) {
  61. pdev = of_platform_device_create(rtc, "opal-rtc", NULL);
  62. of_node_put(rtc);
  63. } else {
  64. if (opal_check_token(OPAL_RTC_READ) ||
  65. opal_check_token(OPAL_READ_TPO))
  66. pdev = platform_device_register_simple("opal-rtc", -1,
  67. NULL, 0);
  68. else
  69. return -ENODEV;
  70. }
  71. return PTR_ERR_OR_ZERO(pdev);
  72. }
  73. machine_subsys_initcall(powernv, opal_time_init);