Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * PowerNV Real Time Clock.
0004  *
0005  * Copyright 2011 IBM Corp.
0006  */
0007 
0008 
0009 #include <linux/kernel.h>
0010 #include <linux/time.h>
0011 #include <linux/bcd.h>
0012 #include <linux/rtc.h>
0013 #include <linux/delay.h>
0014 #include <linux/platform_device.h>
0015 #include <linux/of_platform.h>
0016 
0017 #include <asm/opal.h>
0018 #include <asm/firmware.h>
0019 #include <asm/machdep.h>
0020 
0021 static void __init opal_to_tm(u32 y_m_d, u64 h_m_s_ms, struct rtc_time *tm)
0022 {
0023     tm->tm_year = ((bcd2bin(y_m_d >> 24) * 100) +
0024                bcd2bin((y_m_d >> 16) & 0xff)) - 1900;
0025     tm->tm_mon  = bcd2bin((y_m_d >> 8) & 0xff) - 1;
0026     tm->tm_mday = bcd2bin(y_m_d & 0xff);
0027     tm->tm_hour = bcd2bin((h_m_s_ms >> 56) & 0xff);
0028     tm->tm_min  = bcd2bin((h_m_s_ms >> 48) & 0xff);
0029     tm->tm_sec  = bcd2bin((h_m_s_ms >> 40) & 0xff);
0030     tm->tm_wday     = -1;
0031 }
0032 
0033 time64_t __init opal_get_boot_time(void)
0034 {
0035     struct rtc_time tm;
0036     u32 y_m_d;
0037     u64 h_m_s_ms;
0038     __be32 __y_m_d;
0039     __be64 __h_m_s_ms;
0040     long rc = OPAL_BUSY;
0041 
0042     if (!opal_check_token(OPAL_RTC_READ))
0043         return 0;
0044 
0045     while (rc == OPAL_BUSY || rc == OPAL_BUSY_EVENT) {
0046         rc = opal_rtc_read(&__y_m_d, &__h_m_s_ms);
0047         if (rc == OPAL_BUSY_EVENT) {
0048             mdelay(OPAL_BUSY_DELAY_MS);
0049             opal_poll_events(NULL);
0050         } else if (rc == OPAL_BUSY) {
0051             mdelay(OPAL_BUSY_DELAY_MS);
0052         }
0053     }
0054     if (rc != OPAL_SUCCESS)
0055         return 0;
0056 
0057     y_m_d = be32_to_cpu(__y_m_d);
0058     h_m_s_ms = be64_to_cpu(__h_m_s_ms);
0059     opal_to_tm(y_m_d, h_m_s_ms, &tm);
0060     return rtc_tm_to_time64(&tm);
0061 }
0062 
0063 static __init int opal_time_init(void)
0064 {
0065     struct platform_device *pdev;
0066     struct device_node *rtc;
0067 
0068     rtc = of_find_node_by_path("/ibm,opal/rtc");
0069     if (rtc) {
0070         pdev = of_platform_device_create(rtc, "opal-rtc", NULL);
0071         of_node_put(rtc);
0072     } else {
0073         if (opal_check_token(OPAL_RTC_READ) ||
0074             opal_check_token(OPAL_READ_TPO))
0075             pdev = platform_device_register_simple("opal-rtc", -1,
0076                                    NULL, 0);
0077         else
0078             return -ENODEV;
0079     }
0080 
0081     return PTR_ERR_OR_ZERO(pdev);
0082 }
0083 machine_subsys_initcall(powernv, opal_time_init);