opal-rtc.c 2.1 KB

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