2 * PowerNV Real Time Clock.
4 * Copyright 2011 IBM Corp.
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
13 #include <linux/kernel.h>
14 #include <linux/time.h>
15 #include <linux/bcd.h>
16 #include <linux/rtc.h>
17 #include <linux/delay.h>
20 #include <asm/firmware.h>
21 #include <asm/machdep.h>
23 static void opal_to_tm(u32 y_m_d, u64 h_m_s_ms, struct rtc_time *tm)
25 tm->tm_year = ((bcd2bin(y_m_d >> 24) * 100) +
26 bcd2bin((y_m_d >> 16) & 0xff)) - 1900;
27 tm->tm_mon = bcd2bin((y_m_d >> 8) & 0xff) - 1;
28 tm->tm_mday = bcd2bin(y_m_d & 0xff);
29 tm->tm_hour = bcd2bin((h_m_s_ms >> 56) & 0xff);
30 tm->tm_min = bcd2bin((h_m_s_ms >> 48) & 0xff);
31 tm->tm_sec = bcd2bin((h_m_s_ms >> 40) & 0xff);
36 unsigned long __init opal_get_boot_time(void)
45 while (rc == OPAL_BUSY || rc == OPAL_BUSY_EVENT) {
46 rc = opal_rtc_read(&__y_m_d, &__h_m_s_ms);
47 if (rc == OPAL_BUSY_EVENT)
48 opal_poll_events(NULL);
52 if (rc != OPAL_SUCCESS) {
53 ppc_md.get_rtc_time = NULL;
54 ppc_md.set_rtc_time = NULL;
57 y_m_d = be32_to_cpu(__y_m_d);
58 h_m_s_ms = be64_to_cpu(__h_m_s_ms);
59 opal_to_tm(y_m_d, h_m_s_ms, &tm);
60 return mktime(tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
61 tm.tm_hour, tm.tm_min, tm.tm_sec);
64 void opal_get_rtc_time(struct rtc_time *tm)
72 while (rc == OPAL_BUSY || rc == OPAL_BUSY_EVENT) {
73 rc = opal_rtc_read(&__y_m_d, &__h_m_s_ms);
74 if (rc == OPAL_BUSY_EVENT)
75 opal_poll_events(NULL);
79 if (rc != OPAL_SUCCESS)
81 y_m_d = be32_to_cpu(__y_m_d);
82 h_m_s_ms = be64_to_cpu(__h_m_s_ms);
83 opal_to_tm(y_m_d, h_m_s_ms, tm);
86 int opal_set_rtc_time(struct rtc_time *tm)
92 y_m_d |= ((u32)bin2bcd((tm->tm_year + 1900) / 100)) << 24;
93 y_m_d |= ((u32)bin2bcd((tm->tm_year + 1900) % 100)) << 16;
94 y_m_d |= ((u32)bin2bcd((tm->tm_mon + 1))) << 8;
95 y_m_d |= ((u32)bin2bcd(tm->tm_mday));
97 h_m_s_ms |= ((u64)bin2bcd(tm->tm_hour)) << 56;
98 h_m_s_ms |= ((u64)bin2bcd(tm->tm_min)) << 48;
99 h_m_s_ms |= ((u64)bin2bcd(tm->tm_sec)) << 40;
101 while (rc == OPAL_BUSY || rc == OPAL_BUSY_EVENT) {
102 rc = opal_rtc_write(y_m_d, h_m_s_ms);
103 if (rc == OPAL_BUSY_EVENT)
104 opal_poll_events(NULL);
108 return rc == OPAL_SUCCESS ? 0 : -EIO;