xref: /linux/arch/powerpc/platforms/powernv/opal-rtc.c (revision 628daa8d5abfd904a7329a660c5c374212230123)
1*628daa8dSBenjamin Herrenschmidt /*
2*628daa8dSBenjamin Herrenschmidt  * PowerNV Real Time Clock.
3*628daa8dSBenjamin Herrenschmidt  *
4*628daa8dSBenjamin Herrenschmidt  * Copyright 2011 IBM Corp.
5*628daa8dSBenjamin Herrenschmidt  *
6*628daa8dSBenjamin Herrenschmidt  * This program is free software; you can redistribute it and/or
7*628daa8dSBenjamin Herrenschmidt  * modify it under the terms of the GNU General Public License
8*628daa8dSBenjamin Herrenschmidt  * as published by the Free Software Foundation; either version
9*628daa8dSBenjamin Herrenschmidt  * 2 of the License, or (at your option) any later version.
10*628daa8dSBenjamin Herrenschmidt  */
11*628daa8dSBenjamin Herrenschmidt 
12*628daa8dSBenjamin Herrenschmidt 
13*628daa8dSBenjamin Herrenschmidt #include <linux/kernel.h>
14*628daa8dSBenjamin Herrenschmidt #include <linux/time.h>
15*628daa8dSBenjamin Herrenschmidt #include <linux/bcd.h>
16*628daa8dSBenjamin Herrenschmidt #include <linux/rtc.h>
17*628daa8dSBenjamin Herrenschmidt #include <linux/delay.h>
18*628daa8dSBenjamin Herrenschmidt 
19*628daa8dSBenjamin Herrenschmidt #include <asm/opal.h>
20*628daa8dSBenjamin Herrenschmidt #include <asm/firmware.h>
21*628daa8dSBenjamin Herrenschmidt 
22*628daa8dSBenjamin Herrenschmidt static void opal_to_tm(u32 y_m_d, u64 h_m_s_ms, struct rtc_time *tm)
23*628daa8dSBenjamin Herrenschmidt {
24*628daa8dSBenjamin Herrenschmidt 	tm->tm_year	= ((bcd2bin(y_m_d >> 24) * 100) +
25*628daa8dSBenjamin Herrenschmidt 			   bcd2bin((y_m_d >> 16) & 0xff)) - 1900;
26*628daa8dSBenjamin Herrenschmidt 	tm->tm_mon	= bcd2bin((y_m_d >> 8) & 0xff) - 1;
27*628daa8dSBenjamin Herrenschmidt 	tm->tm_mday	= bcd2bin(y_m_d & 0xff);
28*628daa8dSBenjamin Herrenschmidt 	tm->tm_hour	= bcd2bin((h_m_s_ms >> 56) & 0xff);
29*628daa8dSBenjamin Herrenschmidt 	tm->tm_min	= bcd2bin((h_m_s_ms >> 48) & 0xff);
30*628daa8dSBenjamin Herrenschmidt 	tm->tm_sec	= bcd2bin((h_m_s_ms >> 40) & 0xff);
31*628daa8dSBenjamin Herrenschmidt 
32*628daa8dSBenjamin Herrenschmidt         GregorianDay(tm);
33*628daa8dSBenjamin Herrenschmidt }
34*628daa8dSBenjamin Herrenschmidt 
35*628daa8dSBenjamin Herrenschmidt unsigned long __init opal_get_boot_time(void)
36*628daa8dSBenjamin Herrenschmidt {
37*628daa8dSBenjamin Herrenschmidt 	struct rtc_time tm;
38*628daa8dSBenjamin Herrenschmidt 	u32 y_m_d;
39*628daa8dSBenjamin Herrenschmidt 	u64 h_m_s_ms;
40*628daa8dSBenjamin Herrenschmidt 	long rc = OPAL_BUSY;
41*628daa8dSBenjamin Herrenschmidt 
42*628daa8dSBenjamin Herrenschmidt 	while (rc == OPAL_BUSY || rc == OPAL_BUSY_EVENT) {
43*628daa8dSBenjamin Herrenschmidt 		rc = opal_rtc_read(&y_m_d, &h_m_s_ms);
44*628daa8dSBenjamin Herrenschmidt 		if (rc == OPAL_BUSY_EVENT)
45*628daa8dSBenjamin Herrenschmidt 			opal_poll_events(NULL);
46*628daa8dSBenjamin Herrenschmidt 		else
47*628daa8dSBenjamin Herrenschmidt 			mdelay(10);
48*628daa8dSBenjamin Herrenschmidt 	}
49*628daa8dSBenjamin Herrenschmidt 	if (rc != OPAL_SUCCESS)
50*628daa8dSBenjamin Herrenschmidt 		return 0;
51*628daa8dSBenjamin Herrenschmidt 	opal_to_tm(y_m_d, h_m_s_ms, &tm);
52*628daa8dSBenjamin Herrenschmidt 	return mktime(tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
53*628daa8dSBenjamin Herrenschmidt 		      tm.tm_hour, tm.tm_min, tm.tm_sec);
54*628daa8dSBenjamin Herrenschmidt }
55*628daa8dSBenjamin Herrenschmidt 
56*628daa8dSBenjamin Herrenschmidt void opal_get_rtc_time(struct rtc_time *tm)
57*628daa8dSBenjamin Herrenschmidt {
58*628daa8dSBenjamin Herrenschmidt 	long rc = OPAL_BUSY;
59*628daa8dSBenjamin Herrenschmidt 	u32 y_m_d;
60*628daa8dSBenjamin Herrenschmidt 	u64 h_m_s_ms;
61*628daa8dSBenjamin Herrenschmidt 
62*628daa8dSBenjamin Herrenschmidt 	while (rc == OPAL_BUSY || rc == OPAL_BUSY_EVENT) {
63*628daa8dSBenjamin Herrenschmidt 		rc = opal_rtc_read(&y_m_d, &h_m_s_ms);
64*628daa8dSBenjamin Herrenschmidt 		if (rc == OPAL_BUSY_EVENT)
65*628daa8dSBenjamin Herrenschmidt 			opal_poll_events(NULL);
66*628daa8dSBenjamin Herrenschmidt 		else
67*628daa8dSBenjamin Herrenschmidt 			mdelay(10);
68*628daa8dSBenjamin Herrenschmidt 	}
69*628daa8dSBenjamin Herrenschmidt 	if (rc != OPAL_SUCCESS)
70*628daa8dSBenjamin Herrenschmidt 		return;
71*628daa8dSBenjamin Herrenschmidt 	opal_to_tm(y_m_d, h_m_s_ms, tm);
72*628daa8dSBenjamin Herrenschmidt }
73*628daa8dSBenjamin Herrenschmidt 
74*628daa8dSBenjamin Herrenschmidt int opal_set_rtc_time(struct rtc_time *tm)
75*628daa8dSBenjamin Herrenschmidt {
76*628daa8dSBenjamin Herrenschmidt 	long rc = OPAL_BUSY;
77*628daa8dSBenjamin Herrenschmidt 	u32 y_m_d = 0;
78*628daa8dSBenjamin Herrenschmidt 	u64 h_m_s_ms = 0;
79*628daa8dSBenjamin Herrenschmidt 
80*628daa8dSBenjamin Herrenschmidt 	y_m_d |= ((u32)bin2bcd((tm->tm_year + 1900) / 100)) << 24;
81*628daa8dSBenjamin Herrenschmidt 	y_m_d |= ((u32)bin2bcd((tm->tm_year + 1900) % 100)) << 16;
82*628daa8dSBenjamin Herrenschmidt 	y_m_d |= ((u32)bin2bcd((tm->tm_mon + 1))) << 8;
83*628daa8dSBenjamin Herrenschmidt 	y_m_d |= ((u32)bin2bcd(tm->tm_mday));
84*628daa8dSBenjamin Herrenschmidt 
85*628daa8dSBenjamin Herrenschmidt 	h_m_s_ms |= ((u64)bin2bcd(tm->tm_hour)) << 56;
86*628daa8dSBenjamin Herrenschmidt 	h_m_s_ms |= ((u64)bin2bcd(tm->tm_min)) << 48;
87*628daa8dSBenjamin Herrenschmidt 	h_m_s_ms |= ((u64)bin2bcd(tm->tm_sec)) << 40;
88*628daa8dSBenjamin Herrenschmidt 
89*628daa8dSBenjamin Herrenschmidt 	while (rc == OPAL_BUSY || rc == OPAL_BUSY_EVENT) {
90*628daa8dSBenjamin Herrenschmidt 		rc = opal_rtc_write(y_m_d, h_m_s_ms);
91*628daa8dSBenjamin Herrenschmidt 		if (rc == OPAL_BUSY_EVENT)
92*628daa8dSBenjamin Herrenschmidt 			opal_poll_events(NULL);
93*628daa8dSBenjamin Herrenschmidt 		else
94*628daa8dSBenjamin Herrenschmidt 			mdelay(10);
95*628daa8dSBenjamin Herrenschmidt 	}
96*628daa8dSBenjamin Herrenschmidt 	return rc == OPAL_SUCCESS ? 0 : -EIO;
97*628daa8dSBenjamin Herrenschmidt }
98