xref: /linux/arch/powerpc/platforms/powernv/opal-rtc.c (revision 035ed26fb090ff3277900259f19d57e54da2e116)
1628daa8dSBenjamin Herrenschmidt /*
2628daa8dSBenjamin Herrenschmidt  * PowerNV Real Time Clock.
3628daa8dSBenjamin Herrenschmidt  *
4628daa8dSBenjamin Herrenschmidt  * Copyright 2011 IBM Corp.
5628daa8dSBenjamin Herrenschmidt  *
6628daa8dSBenjamin Herrenschmidt  * This program is free software; you can redistribute it and/or
7628daa8dSBenjamin Herrenschmidt  * modify it under the terms of the GNU General Public License
8628daa8dSBenjamin Herrenschmidt  * as published by the Free Software Foundation; either version
9628daa8dSBenjamin Herrenschmidt  * 2 of the License, or (at your option) any later version.
10628daa8dSBenjamin Herrenschmidt  */
11628daa8dSBenjamin Herrenschmidt 
12628daa8dSBenjamin Herrenschmidt 
13628daa8dSBenjamin Herrenschmidt #include <linux/kernel.h>
14628daa8dSBenjamin Herrenschmidt #include <linux/time.h>
15628daa8dSBenjamin Herrenschmidt #include <linux/bcd.h>
16628daa8dSBenjamin Herrenschmidt #include <linux/rtc.h>
17628daa8dSBenjamin Herrenschmidt #include <linux/delay.h>
18628daa8dSBenjamin Herrenschmidt 
19628daa8dSBenjamin Herrenschmidt #include <asm/opal.h>
20628daa8dSBenjamin Herrenschmidt #include <asm/firmware.h>
212c49195bSMichael Neuling #include <asm/machdep.h>
22628daa8dSBenjamin Herrenschmidt 
23628daa8dSBenjamin Herrenschmidt static void opal_to_tm(u32 y_m_d, u64 h_m_s_ms, struct rtc_time *tm)
24628daa8dSBenjamin Herrenschmidt {
25628daa8dSBenjamin Herrenschmidt 	tm->tm_year	= ((bcd2bin(y_m_d >> 24) * 100) +
26628daa8dSBenjamin Herrenschmidt 			   bcd2bin((y_m_d >> 16) & 0xff)) - 1900;
27628daa8dSBenjamin Herrenschmidt 	tm->tm_mon	= bcd2bin((y_m_d >> 8) & 0xff) - 1;
28628daa8dSBenjamin Herrenschmidt 	tm->tm_mday	= bcd2bin(y_m_d & 0xff);
29628daa8dSBenjamin Herrenschmidt 	tm->tm_hour	= bcd2bin((h_m_s_ms >> 56) & 0xff);
30628daa8dSBenjamin Herrenschmidt 	tm->tm_min	= bcd2bin((h_m_s_ms >> 48) & 0xff);
31628daa8dSBenjamin Herrenschmidt 	tm->tm_sec	= bcd2bin((h_m_s_ms >> 40) & 0xff);
32628daa8dSBenjamin Herrenschmidt 
33628daa8dSBenjamin Herrenschmidt         GregorianDay(tm);
34628daa8dSBenjamin Herrenschmidt }
35628daa8dSBenjamin Herrenschmidt 
36628daa8dSBenjamin Herrenschmidt unsigned long __init opal_get_boot_time(void)
37628daa8dSBenjamin Herrenschmidt {
38628daa8dSBenjamin Herrenschmidt 	struct rtc_time tm;
39628daa8dSBenjamin Herrenschmidt 	u32 y_m_d;
40628daa8dSBenjamin Herrenschmidt 	u64 h_m_s_ms;
416feff6d4SAnton Blanchard 	__be32 __y_m_d;
426feff6d4SAnton Blanchard 	__be64 __h_m_s_ms;
43628daa8dSBenjamin Herrenschmidt 	long rc = OPAL_BUSY;
44628daa8dSBenjamin Herrenschmidt 
45*035ed26fSMichael Neuling 	if (!opal_check_token(OPAL_RTC_READ))
46*035ed26fSMichael Neuling 		goto out;
47*035ed26fSMichael Neuling 
48628daa8dSBenjamin Herrenschmidt 	while (rc == OPAL_BUSY || rc == OPAL_BUSY_EVENT) {
496feff6d4SAnton Blanchard 		rc = opal_rtc_read(&__y_m_d, &__h_m_s_ms);
50628daa8dSBenjamin Herrenschmidt 		if (rc == OPAL_BUSY_EVENT)
51628daa8dSBenjamin Herrenschmidt 			opal_poll_events(NULL);
52628daa8dSBenjamin Herrenschmidt 		else
53628daa8dSBenjamin Herrenschmidt 			mdelay(10);
54628daa8dSBenjamin Herrenschmidt 	}
55*035ed26fSMichael Neuling 	if (rc != OPAL_SUCCESS)
56*035ed26fSMichael Neuling 		goto out;
57*035ed26fSMichael 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);
61628daa8dSBenjamin Herrenschmidt 	return mktime(tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
62628daa8dSBenjamin Herrenschmidt 		      tm.tm_hour, tm.tm_min, tm.tm_sec);
63*035ed26fSMichael Neuling out:
64*035ed26fSMichael Neuling 	ppc_md.get_rtc_time = NULL;
65*035ed26fSMichael Neuling 	ppc_md.set_rtc_time = NULL;
66*035ed26fSMichael Neuling 	return 0;
67628daa8dSBenjamin Herrenschmidt }
68628daa8dSBenjamin Herrenschmidt 
69628daa8dSBenjamin Herrenschmidt void opal_get_rtc_time(struct rtc_time *tm)
70628daa8dSBenjamin Herrenschmidt {
71628daa8dSBenjamin Herrenschmidt 	long rc = OPAL_BUSY;
72628daa8dSBenjamin Herrenschmidt 	u32 y_m_d;
73628daa8dSBenjamin Herrenschmidt 	u64 h_m_s_ms;
746feff6d4SAnton Blanchard 	__be32 __y_m_d;
756feff6d4SAnton Blanchard 	__be64 __h_m_s_ms;
76628daa8dSBenjamin Herrenschmidt 
77628daa8dSBenjamin Herrenschmidt 	while (rc == OPAL_BUSY || rc == OPAL_BUSY_EVENT) {
786feff6d4SAnton Blanchard 		rc = opal_rtc_read(&__y_m_d, &__h_m_s_ms);
79628daa8dSBenjamin Herrenschmidt 		if (rc == OPAL_BUSY_EVENT)
80628daa8dSBenjamin Herrenschmidt 			opal_poll_events(NULL);
81628daa8dSBenjamin Herrenschmidt 		else
82628daa8dSBenjamin Herrenschmidt 			mdelay(10);
83628daa8dSBenjamin Herrenschmidt 	}
84628daa8dSBenjamin Herrenschmidt 	if (rc != OPAL_SUCCESS)
85628daa8dSBenjamin Herrenschmidt 		return;
866feff6d4SAnton Blanchard 	y_m_d = be32_to_cpu(__y_m_d);
876feff6d4SAnton Blanchard 	h_m_s_ms = be64_to_cpu(__h_m_s_ms);
88628daa8dSBenjamin Herrenschmidt 	opal_to_tm(y_m_d, h_m_s_ms, tm);
89628daa8dSBenjamin Herrenschmidt }
90628daa8dSBenjamin Herrenschmidt 
91628daa8dSBenjamin Herrenschmidt int opal_set_rtc_time(struct rtc_time *tm)
92628daa8dSBenjamin Herrenschmidt {
93628daa8dSBenjamin Herrenschmidt 	long rc = OPAL_BUSY;
94628daa8dSBenjamin Herrenschmidt 	u32 y_m_d = 0;
95628daa8dSBenjamin Herrenschmidt 	u64 h_m_s_ms = 0;
96628daa8dSBenjamin Herrenschmidt 
97628daa8dSBenjamin Herrenschmidt 	y_m_d |= ((u32)bin2bcd((tm->tm_year + 1900) / 100)) << 24;
98628daa8dSBenjamin Herrenschmidt 	y_m_d |= ((u32)bin2bcd((tm->tm_year + 1900) % 100)) << 16;
99628daa8dSBenjamin Herrenschmidt 	y_m_d |= ((u32)bin2bcd((tm->tm_mon + 1))) << 8;
100628daa8dSBenjamin Herrenschmidt 	y_m_d |= ((u32)bin2bcd(tm->tm_mday));
101628daa8dSBenjamin Herrenschmidt 
102628daa8dSBenjamin Herrenschmidt 	h_m_s_ms |= ((u64)bin2bcd(tm->tm_hour)) << 56;
103628daa8dSBenjamin Herrenschmidt 	h_m_s_ms |= ((u64)bin2bcd(tm->tm_min)) << 48;
104628daa8dSBenjamin Herrenschmidt 	h_m_s_ms |= ((u64)bin2bcd(tm->tm_sec)) << 40;
105628daa8dSBenjamin Herrenschmidt 
106628daa8dSBenjamin Herrenschmidt 	while (rc == OPAL_BUSY || rc == OPAL_BUSY_EVENT) {
107628daa8dSBenjamin Herrenschmidt 		rc = opal_rtc_write(y_m_d, h_m_s_ms);
108628daa8dSBenjamin Herrenschmidt 		if (rc == OPAL_BUSY_EVENT)
109628daa8dSBenjamin Herrenschmidt 			opal_poll_events(NULL);
110628daa8dSBenjamin Herrenschmidt 		else
111628daa8dSBenjamin Herrenschmidt 			mdelay(10);
112628daa8dSBenjamin Herrenschmidt 	}
113628daa8dSBenjamin Herrenschmidt 	return rc == OPAL_SUCCESS ? 0 : -EIO;
114628daa8dSBenjamin Herrenschmidt }
115