1*2874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later 2628daa8dSBenjamin Herrenschmidt /* 3628daa8dSBenjamin Herrenschmidt * PowerNV Real Time Clock. 4628daa8dSBenjamin Herrenschmidt * 5628daa8dSBenjamin Herrenschmidt * Copyright 2011 IBM Corp. 6628daa8dSBenjamin Herrenschmidt */ 7628daa8dSBenjamin Herrenschmidt 8628daa8dSBenjamin Herrenschmidt 9628daa8dSBenjamin Herrenschmidt #include <linux/kernel.h> 10628daa8dSBenjamin Herrenschmidt #include <linux/time.h> 11628daa8dSBenjamin Herrenschmidt #include <linux/bcd.h> 12628daa8dSBenjamin Herrenschmidt #include <linux/rtc.h> 13628daa8dSBenjamin Herrenschmidt #include <linux/delay.h> 1416b1d26eSNeelesh Gupta #include <linux/platform_device.h> 1516b1d26eSNeelesh Gupta #include <linux/of_platform.h> 16628daa8dSBenjamin Herrenschmidt 17628daa8dSBenjamin Herrenschmidt #include <asm/opal.h> 18628daa8dSBenjamin Herrenschmidt #include <asm/firmware.h> 192c49195bSMichael Neuling #include <asm/machdep.h> 20628daa8dSBenjamin Herrenschmidt 21628daa8dSBenjamin Herrenschmidt static void opal_to_tm(u32 y_m_d, u64 h_m_s_ms, struct rtc_time *tm) 22628daa8dSBenjamin Herrenschmidt { 23628daa8dSBenjamin Herrenschmidt tm->tm_year = ((bcd2bin(y_m_d >> 24) * 100) + 24628daa8dSBenjamin Herrenschmidt bcd2bin((y_m_d >> 16) & 0xff)) - 1900; 25628daa8dSBenjamin Herrenschmidt tm->tm_mon = bcd2bin((y_m_d >> 8) & 0xff) - 1; 26628daa8dSBenjamin Herrenschmidt tm->tm_mday = bcd2bin(y_m_d & 0xff); 27628daa8dSBenjamin Herrenschmidt tm->tm_hour = bcd2bin((h_m_s_ms >> 56) & 0xff); 28628daa8dSBenjamin Herrenschmidt tm->tm_min = bcd2bin((h_m_s_ms >> 48) & 0xff); 29628daa8dSBenjamin Herrenschmidt tm->tm_sec = bcd2bin((h_m_s_ms >> 40) & 0xff); 3000b912b0SDaniel Axtens tm->tm_wday = -1; 31628daa8dSBenjamin Herrenschmidt } 32628daa8dSBenjamin Herrenschmidt 335bfd6435SArnd Bergmann time64_t __init opal_get_boot_time(void) 34628daa8dSBenjamin Herrenschmidt { 35628daa8dSBenjamin Herrenschmidt struct rtc_time tm; 36628daa8dSBenjamin Herrenschmidt u32 y_m_d; 37628daa8dSBenjamin Herrenschmidt u64 h_m_s_ms; 386feff6d4SAnton Blanchard __be32 __y_m_d; 396feff6d4SAnton Blanchard __be64 __h_m_s_ms; 40628daa8dSBenjamin Herrenschmidt long rc = OPAL_BUSY; 41628daa8dSBenjamin Herrenschmidt 42035ed26fSMichael Neuling if (!opal_check_token(OPAL_RTC_READ)) 4316b1d26eSNeelesh Gupta return 0; 44035ed26fSMichael Neuling 45628daa8dSBenjamin Herrenschmidt while (rc == OPAL_BUSY || rc == OPAL_BUSY_EVENT) { 466feff6d4SAnton Blanchard rc = opal_rtc_read(&__y_m_d, &__h_m_s_ms); 47682e6b4dSNicholas Piggin if (rc == OPAL_BUSY_EVENT) { 48682e6b4dSNicholas Piggin mdelay(OPAL_BUSY_DELAY_MS); 49628daa8dSBenjamin Herrenschmidt opal_poll_events(NULL); 50682e6b4dSNicholas Piggin } else if (rc == OPAL_BUSY) { 51682e6b4dSNicholas Piggin mdelay(OPAL_BUSY_DELAY_MS); 52682e6b4dSNicholas Piggin } 53628daa8dSBenjamin Herrenschmidt } 54035ed26fSMichael Neuling if (rc != OPAL_SUCCESS) 5516b1d26eSNeelesh Gupta return 0; 56035ed26fSMichael Neuling 576feff6d4SAnton Blanchard y_m_d = be32_to_cpu(__y_m_d); 586feff6d4SAnton Blanchard h_m_s_ms = be64_to_cpu(__h_m_s_ms); 59628daa8dSBenjamin Herrenschmidt opal_to_tm(y_m_d, h_m_s_ms, &tm); 605bfd6435SArnd Bergmann return rtc_tm_to_time64(&tm); 61628daa8dSBenjamin Herrenschmidt } 62628daa8dSBenjamin Herrenschmidt 6316b1d26eSNeelesh Gupta static __init int opal_time_init(void) 64628daa8dSBenjamin Herrenschmidt { 6516b1d26eSNeelesh Gupta struct platform_device *pdev; 6616b1d26eSNeelesh Gupta struct device_node *rtc; 67628daa8dSBenjamin Herrenschmidt 6816b1d26eSNeelesh Gupta rtc = of_find_node_by_path("/ibm,opal/rtc"); 6916b1d26eSNeelesh Gupta if (rtc) { 7016b1d26eSNeelesh Gupta pdev = of_platform_device_create(rtc, "opal-rtc", NULL); 7116b1d26eSNeelesh Gupta of_node_put(rtc); 7216b1d26eSNeelesh Gupta } else { 7316b1d26eSNeelesh Gupta if (opal_check_token(OPAL_RTC_READ) || 7416b1d26eSNeelesh Gupta opal_check_token(OPAL_READ_TPO)) 7516b1d26eSNeelesh Gupta pdev = platform_device_register_simple("opal-rtc", -1, 7616b1d26eSNeelesh Gupta NULL, 0); 77628daa8dSBenjamin Herrenschmidt else 7816b1d26eSNeelesh Gupta return -ENODEV; 79628daa8dSBenjamin Herrenschmidt } 80628daa8dSBenjamin Herrenschmidt 8116b1d26eSNeelesh Gupta return PTR_ERR_OR_ZERO(pdev); 82628daa8dSBenjamin Herrenschmidt } 8316b1d26eSNeelesh Gupta machine_subsys_initcall(powernv, opal_time_init); 84