Path: blob/master/arch/powerpc/platforms/powernv/opal-rtc.c
26481 views
// SPDX-License-Identifier: GPL-2.0-or-later1/*2* PowerNV Real Time Clock.3*4* Copyright 2011 IBM Corp.5*/678#include <linux/kernel.h>9#include <linux/time.h>10#include <linux/bcd.h>11#include <linux/rtc.h>12#include <linux/delay.h>13#include <linux/of.h>14#include <linux/of_platform.h>15#include <linux/platform_device.h>1617#include <asm/opal.h>18#include <asm/firmware.h>19#include <asm/machdep.h>2021static void __init opal_to_tm(u32 y_m_d, u64 h_m_s_ms, struct rtc_time *tm)22{23tm->tm_year = ((bcd2bin(y_m_d >> 24) * 100) +24bcd2bin((y_m_d >> 16) & 0xff)) - 1900;25tm->tm_mon = bcd2bin((y_m_d >> 8) & 0xff) - 1;26tm->tm_mday = bcd2bin(y_m_d & 0xff);27tm->tm_hour = bcd2bin((h_m_s_ms >> 56) & 0xff);28tm->tm_min = bcd2bin((h_m_s_ms >> 48) & 0xff);29tm->tm_sec = bcd2bin((h_m_s_ms >> 40) & 0xff);30tm->tm_wday = -1;31}3233time64_t __init opal_get_boot_time(void)34{35struct rtc_time tm;36u32 y_m_d;37u64 h_m_s_ms;38__be32 __y_m_d;39__be64 __h_m_s_ms;40long rc = OPAL_BUSY;4142if (!opal_check_token(OPAL_RTC_READ))43return 0;4445while (rc == OPAL_BUSY || rc == OPAL_BUSY_EVENT) {46rc = opal_rtc_read(&__y_m_d, &__h_m_s_ms);47if (rc == OPAL_BUSY_EVENT) {48mdelay(OPAL_BUSY_DELAY_MS);49opal_poll_events(NULL);50} else if (rc == OPAL_BUSY) {51mdelay(OPAL_BUSY_DELAY_MS);52}53}54if (rc != OPAL_SUCCESS)55return 0;5657y_m_d = be32_to_cpu(__y_m_d);58h_m_s_ms = be64_to_cpu(__h_m_s_ms);59opal_to_tm(y_m_d, h_m_s_ms, &tm);60return rtc_tm_to_time64(&tm);61}6263static __init int opal_time_init(void)64{65struct platform_device *pdev;66struct device_node *rtc;6768rtc = of_find_node_by_path("/ibm,opal/rtc");69if (rtc) {70pdev = of_platform_device_create(rtc, "opal-rtc", NULL);71of_node_put(rtc);72} else {73if (opal_check_token(OPAL_RTC_READ) ||74opal_check_token(OPAL_READ_TPO))75pdev = platform_device_register_simple("opal-rtc", -1,76NULL, 0);77else78return -ENODEV;79}8081return PTR_ERR_OR_ZERO(pdev);82}83machine_subsys_initcall(powernv, opal_time_init);848586