opal-rtc.c 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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/platform_device.h>
  13. #include <linux/of_platform.h>
  14. #include <asm/opal.h>
  15. #include <asm/firmware.h>
  16. #include <asm/machdep.h>
  17. static void __init opal_to_tm(u32 y_m_d, u64 h_m_s_ms, struct rtc_time *tm)
  18. {
  19. tm->tm_year = ((bcd2bin(y_m_d >> 24) * 100) +
  20. bcd2bin((y_m_d >> 16) & 0xff)) - 1900;
  21. tm->tm_mon = bcd2bin((y_m_d >> 8) & 0xff) - 1;
  22. tm->tm_mday = bcd2bin(y_m_d & 0xff);
  23. tm->tm_hour = bcd2bin((h_m_s_ms >> 56) & 0xff);
  24. tm->tm_min = bcd2bin((h_m_s_ms >> 48) & 0xff);
  25. tm->tm_sec = bcd2bin((h_m_s_ms >> 40) & 0xff);
  26. tm->tm_wday = -1;
  27. }
  28. time64_t __init opal_get_boot_time(void)
  29. {
  30. struct rtc_time tm;
  31. u32 y_m_d;
  32. u64 h_m_s_ms;
  33. __be32 __y_m_d;
  34. __be64 __h_m_s_ms;
  35. long rc = OPAL_BUSY;
  36. if (!opal_check_token(OPAL_RTC_READ))
  37. return 0;
  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. mdelay(OPAL_BUSY_DELAY_MS);
  42. opal_poll_events(NULL);
  43. } else if (rc == OPAL_BUSY) {
  44. mdelay(OPAL_BUSY_DELAY_MS);
  45. }
  46. }
  47. if (rc != OPAL_SUCCESS)
  48. return 0;
  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 rtc_tm_to_time64(&tm);
  53. }
  54. static __init int opal_time_init(void)
  55. {
  56. struct platform_device *pdev;
  57. struct device_node *rtc;
  58. rtc = of_find_node_by_path("/ibm,opal/rtc");
  59. if (rtc) {
  60. pdev = of_platform_device_create(rtc, "opal-rtc", NULL);
  61. of_node_put(rtc);
  62. } else {
  63. if (opal_check_token(OPAL_RTC_READ) ||
  64. opal_check_token(OPAL_READ_TPO))
  65. pdev = platform_device_register_simple("opal-rtc", -1,
  66. NULL, 0);
  67. else
  68. return -ENODEV;
  69. }
  70. return PTR_ERR_OR_ZERO(pdev);
  71. }
  72. machine_subsys_initcall(powernv, opal_time_init);