xref: /linux/arch/powerpc/platforms/powernv/opal-rtc.c (revision 81d7cac4d11cc65f29be68c72759429d5194347a)
12874c5fdSThomas 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>
14*81d7cac4SRob Herring #include <linux/of.h>
1516b1d26eSNeelesh Gupta #include <linux/of_platform.h>
16*81d7cac4SRob Herring #include <linux/platform_device.h>
17628daa8dSBenjamin Herrenschmidt 
18628daa8dSBenjamin Herrenschmidt #include <asm/opal.h>
19628daa8dSBenjamin Herrenschmidt #include <asm/firmware.h>
202c49195bSMichael Neuling #include <asm/machdep.h>
21628daa8dSBenjamin Herrenschmidt 
22e5913db1SNick Child static void __init opal_to_tm(u32 y_m_d, u64 h_m_s_ms, struct rtc_time *tm)
23628daa8dSBenjamin Herrenschmidt {
24628daa8dSBenjamin Herrenschmidt 	tm->tm_year	= ((bcd2bin(y_m_d >> 24) * 100) +
25628daa8dSBenjamin Herrenschmidt 			   bcd2bin((y_m_d >> 16) & 0xff)) - 1900;
26628daa8dSBenjamin Herrenschmidt 	tm->tm_mon	= bcd2bin((y_m_d >> 8) & 0xff) - 1;
27628daa8dSBenjamin Herrenschmidt 	tm->tm_mday	= bcd2bin(y_m_d & 0xff);
28628daa8dSBenjamin Herrenschmidt 	tm->tm_hour	= bcd2bin((h_m_s_ms >> 56) & 0xff);
29628daa8dSBenjamin Herrenschmidt 	tm->tm_min	= bcd2bin((h_m_s_ms >> 48) & 0xff);
30628daa8dSBenjamin Herrenschmidt 	tm->tm_sec	= bcd2bin((h_m_s_ms >> 40) & 0xff);
3100b912b0SDaniel Axtens 	tm->tm_wday     = -1;
32628daa8dSBenjamin Herrenschmidt }
33628daa8dSBenjamin Herrenschmidt 
345bfd6435SArnd Bergmann time64_t __init opal_get_boot_time(void)
35628daa8dSBenjamin Herrenschmidt {
36628daa8dSBenjamin Herrenschmidt 	struct rtc_time tm;
37628daa8dSBenjamin Herrenschmidt 	u32 y_m_d;
38628daa8dSBenjamin Herrenschmidt 	u64 h_m_s_ms;
396feff6d4SAnton Blanchard 	__be32 __y_m_d;
406feff6d4SAnton Blanchard 	__be64 __h_m_s_ms;
41628daa8dSBenjamin Herrenschmidt 	long rc = OPAL_BUSY;
42628daa8dSBenjamin Herrenschmidt 
43035ed26fSMichael Neuling 	if (!opal_check_token(OPAL_RTC_READ))
4416b1d26eSNeelesh Gupta 		return 0;
45035ed26fSMichael Neuling 
46628daa8dSBenjamin Herrenschmidt 	while (rc == OPAL_BUSY || rc == OPAL_BUSY_EVENT) {
476feff6d4SAnton Blanchard 		rc = opal_rtc_read(&__y_m_d, &__h_m_s_ms);
48682e6b4dSNicholas Piggin 		if (rc == OPAL_BUSY_EVENT) {
49682e6b4dSNicholas Piggin 			mdelay(OPAL_BUSY_DELAY_MS);
50628daa8dSBenjamin Herrenschmidt 			opal_poll_events(NULL);
51682e6b4dSNicholas Piggin 		} else if (rc == OPAL_BUSY) {
52682e6b4dSNicholas Piggin 			mdelay(OPAL_BUSY_DELAY_MS);
53682e6b4dSNicholas Piggin 		}
54628daa8dSBenjamin Herrenschmidt 	}
55035ed26fSMichael Neuling 	if (rc != OPAL_SUCCESS)
5616b1d26eSNeelesh Gupta 		return 0;
57035ed26fSMichael Neuling 
586feff6d4SAnton Blanchard 	y_m_d = be32_to_cpu(__y_m_d);
596feff6d4SAnton Blanchard 	h_m_s_ms = be64_to_cpu(__h_m_s_ms);
60628daa8dSBenjamin Herrenschmidt 	opal_to_tm(y_m_d, h_m_s_ms, &tm);
615bfd6435SArnd Bergmann 	return rtc_tm_to_time64(&tm);
62628daa8dSBenjamin Herrenschmidt }
63628daa8dSBenjamin Herrenschmidt 
6416b1d26eSNeelesh Gupta static __init int opal_time_init(void)
65628daa8dSBenjamin Herrenschmidt {
6616b1d26eSNeelesh Gupta 	struct platform_device *pdev;
6716b1d26eSNeelesh Gupta 	struct device_node *rtc;
68628daa8dSBenjamin Herrenschmidt 
6916b1d26eSNeelesh Gupta 	rtc = of_find_node_by_path("/ibm,opal/rtc");
7016b1d26eSNeelesh Gupta 	if (rtc) {
7116b1d26eSNeelesh Gupta 		pdev = of_platform_device_create(rtc, "opal-rtc", NULL);
7216b1d26eSNeelesh Gupta 		of_node_put(rtc);
7316b1d26eSNeelesh Gupta 	} else {
7416b1d26eSNeelesh Gupta 		if (opal_check_token(OPAL_RTC_READ) ||
7516b1d26eSNeelesh Gupta 		    opal_check_token(OPAL_READ_TPO))
7616b1d26eSNeelesh Gupta 			pdev = platform_device_register_simple("opal-rtc", -1,
7716b1d26eSNeelesh Gupta 							       NULL, 0);
78628daa8dSBenjamin Herrenschmidt 		else
7916b1d26eSNeelesh Gupta 			return -ENODEV;
80628daa8dSBenjamin Herrenschmidt 	}
81628daa8dSBenjamin Herrenschmidt 
8216b1d26eSNeelesh Gupta 	return PTR_ERR_OR_ZERO(pdev);
83628daa8dSBenjamin Herrenschmidt }
8416b1d26eSNeelesh Gupta machine_subsys_initcall(powernv, opal_time_init);
85