1 /* 2 * PowerNV Real Time Clock. 3 * 4 * Copyright 2011 IBM Corp. 5 * 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. 10 */ 11 12 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> 18 19 #include <asm/opal.h> 20 #include <asm/firmware.h> 21 #include <asm/machdep.h> 22 23 static void opal_to_tm(u32 y_m_d, u64 h_m_s_ms, struct rtc_time *tm) 24 { 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); 32 33 GregorianDay(tm); 34 } 35 36 unsigned long __init opal_get_boot_time(void) 37 { 38 struct rtc_time tm; 39 u32 y_m_d; 40 u64 h_m_s_ms; 41 __be32 __y_m_d; 42 __be64 __h_m_s_ms; 43 long rc = OPAL_BUSY; 44 45 if (!opal_check_token(OPAL_RTC_READ)) 46 goto out; 47 48 while (rc == OPAL_BUSY || rc == OPAL_BUSY_EVENT) { 49 rc = opal_rtc_read(&__y_m_d, &__h_m_s_ms); 50 if (rc == OPAL_BUSY_EVENT) 51 opal_poll_events(NULL); 52 else 53 mdelay(10); 54 } 55 if (rc != OPAL_SUCCESS) 56 goto out; 57 58 y_m_d = be32_to_cpu(__y_m_d); 59 h_m_s_ms = be64_to_cpu(__h_m_s_ms); 60 opal_to_tm(y_m_d, h_m_s_ms, &tm); 61 return mktime(tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, 62 tm.tm_hour, tm.tm_min, tm.tm_sec); 63 out: 64 ppc_md.get_rtc_time = NULL; 65 ppc_md.set_rtc_time = NULL; 66 return 0; 67 } 68 69 void opal_get_rtc_time(struct rtc_time *tm) 70 { 71 long rc = OPAL_BUSY; 72 u32 y_m_d; 73 u64 h_m_s_ms; 74 __be32 __y_m_d; 75 __be64 __h_m_s_ms; 76 77 while (rc == OPAL_BUSY || rc == OPAL_BUSY_EVENT) { 78 rc = opal_rtc_read(&__y_m_d, &__h_m_s_ms); 79 if (rc == OPAL_BUSY_EVENT) 80 opal_poll_events(NULL); 81 else 82 mdelay(10); 83 } 84 if (rc != OPAL_SUCCESS) 85 return; 86 y_m_d = be32_to_cpu(__y_m_d); 87 h_m_s_ms = be64_to_cpu(__h_m_s_ms); 88 opal_to_tm(y_m_d, h_m_s_ms, tm); 89 } 90 91 int opal_set_rtc_time(struct rtc_time *tm) 92 { 93 long rc = OPAL_BUSY; 94 u32 y_m_d = 0; 95 u64 h_m_s_ms = 0; 96 97 y_m_d |= ((u32)bin2bcd((tm->tm_year + 1900) / 100)) << 24; 98 y_m_d |= ((u32)bin2bcd((tm->tm_year + 1900) % 100)) << 16; 99 y_m_d |= ((u32)bin2bcd((tm->tm_mon + 1))) << 8; 100 y_m_d |= ((u32)bin2bcd(tm->tm_mday)); 101 102 h_m_s_ms |= ((u64)bin2bcd(tm->tm_hour)) << 56; 103 h_m_s_ms |= ((u64)bin2bcd(tm->tm_min)) << 48; 104 h_m_s_ms |= ((u64)bin2bcd(tm->tm_sec)) << 40; 105 106 while (rc == OPAL_BUSY || rc == OPAL_BUSY_EVENT) { 107 rc = opal_rtc_write(y_m_d, h_m_s_ms); 108 if (rc == OPAL_BUSY_EVENT) 109 opal_poll_events(NULL); 110 else 111 mdelay(10); 112 } 113 return rc == OPAL_SUCCESS ? 0 : -EIO; 114 } 115