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> 21628daa8dSBenjamin Herrenschmidt 22628daa8dSBenjamin Herrenschmidt static void 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); 31628daa8dSBenjamin Herrenschmidt 32628daa8dSBenjamin Herrenschmidt GregorianDay(tm); 33628daa8dSBenjamin Herrenschmidt } 34628daa8dSBenjamin Herrenschmidt 35628daa8dSBenjamin Herrenschmidt unsigned long __init opal_get_boot_time(void) 36628daa8dSBenjamin Herrenschmidt { 37628daa8dSBenjamin Herrenschmidt struct rtc_time tm; 38628daa8dSBenjamin Herrenschmidt u32 y_m_d; 39628daa8dSBenjamin Herrenschmidt u64 h_m_s_ms; 40*6feff6d4SAnton Blanchard __be32 __y_m_d; 41*6feff6d4SAnton Blanchard __be64 __h_m_s_ms; 42628daa8dSBenjamin Herrenschmidt long rc = OPAL_BUSY; 43628daa8dSBenjamin Herrenschmidt 44628daa8dSBenjamin Herrenschmidt while (rc == OPAL_BUSY || rc == OPAL_BUSY_EVENT) { 45*6feff6d4SAnton Blanchard rc = opal_rtc_read(&__y_m_d, &__h_m_s_ms); 46628daa8dSBenjamin Herrenschmidt if (rc == OPAL_BUSY_EVENT) 47628daa8dSBenjamin Herrenschmidt opal_poll_events(NULL); 48628daa8dSBenjamin Herrenschmidt else 49628daa8dSBenjamin Herrenschmidt mdelay(10); 50628daa8dSBenjamin Herrenschmidt } 51628daa8dSBenjamin Herrenschmidt if (rc != OPAL_SUCCESS) 52628daa8dSBenjamin Herrenschmidt return 0; 53*6feff6d4SAnton Blanchard y_m_d = be32_to_cpu(__y_m_d); 54*6feff6d4SAnton Blanchard h_m_s_ms = be64_to_cpu(__h_m_s_ms); 55628daa8dSBenjamin Herrenschmidt opal_to_tm(y_m_d, h_m_s_ms, &tm); 56628daa8dSBenjamin Herrenschmidt return mktime(tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, 57628daa8dSBenjamin Herrenschmidt tm.tm_hour, tm.tm_min, tm.tm_sec); 58628daa8dSBenjamin Herrenschmidt } 59628daa8dSBenjamin Herrenschmidt 60628daa8dSBenjamin Herrenschmidt void opal_get_rtc_time(struct rtc_time *tm) 61628daa8dSBenjamin Herrenschmidt { 62628daa8dSBenjamin Herrenschmidt long rc = OPAL_BUSY; 63628daa8dSBenjamin Herrenschmidt u32 y_m_d; 64628daa8dSBenjamin Herrenschmidt u64 h_m_s_ms; 65*6feff6d4SAnton Blanchard __be32 __y_m_d; 66*6feff6d4SAnton Blanchard __be64 __h_m_s_ms; 67628daa8dSBenjamin Herrenschmidt 68628daa8dSBenjamin Herrenschmidt while (rc == OPAL_BUSY || rc == OPAL_BUSY_EVENT) { 69*6feff6d4SAnton Blanchard rc = opal_rtc_read(&__y_m_d, &__h_m_s_ms); 70628daa8dSBenjamin Herrenschmidt if (rc == OPAL_BUSY_EVENT) 71628daa8dSBenjamin Herrenschmidt opal_poll_events(NULL); 72628daa8dSBenjamin Herrenschmidt else 73628daa8dSBenjamin Herrenschmidt mdelay(10); 74628daa8dSBenjamin Herrenschmidt } 75628daa8dSBenjamin Herrenschmidt if (rc != OPAL_SUCCESS) 76628daa8dSBenjamin Herrenschmidt return; 77*6feff6d4SAnton Blanchard y_m_d = be32_to_cpu(__y_m_d); 78*6feff6d4SAnton Blanchard h_m_s_ms = be64_to_cpu(__h_m_s_ms); 79628daa8dSBenjamin Herrenschmidt opal_to_tm(y_m_d, h_m_s_ms, tm); 80628daa8dSBenjamin Herrenschmidt } 81628daa8dSBenjamin Herrenschmidt 82628daa8dSBenjamin Herrenschmidt int opal_set_rtc_time(struct rtc_time *tm) 83628daa8dSBenjamin Herrenschmidt { 84628daa8dSBenjamin Herrenschmidt long rc = OPAL_BUSY; 85628daa8dSBenjamin Herrenschmidt u32 y_m_d = 0; 86628daa8dSBenjamin Herrenschmidt u64 h_m_s_ms = 0; 87628daa8dSBenjamin Herrenschmidt 88628daa8dSBenjamin Herrenschmidt y_m_d |= ((u32)bin2bcd((tm->tm_year + 1900) / 100)) << 24; 89628daa8dSBenjamin Herrenschmidt y_m_d |= ((u32)bin2bcd((tm->tm_year + 1900) % 100)) << 16; 90628daa8dSBenjamin Herrenschmidt y_m_d |= ((u32)bin2bcd((tm->tm_mon + 1))) << 8; 91628daa8dSBenjamin Herrenschmidt y_m_d |= ((u32)bin2bcd(tm->tm_mday)); 92628daa8dSBenjamin Herrenschmidt 93628daa8dSBenjamin Herrenschmidt h_m_s_ms |= ((u64)bin2bcd(tm->tm_hour)) << 56; 94628daa8dSBenjamin Herrenschmidt h_m_s_ms |= ((u64)bin2bcd(tm->tm_min)) << 48; 95628daa8dSBenjamin Herrenschmidt h_m_s_ms |= ((u64)bin2bcd(tm->tm_sec)) << 40; 96628daa8dSBenjamin Herrenschmidt 97628daa8dSBenjamin Herrenschmidt while (rc == OPAL_BUSY || rc == OPAL_BUSY_EVENT) { 98628daa8dSBenjamin Herrenschmidt rc = opal_rtc_write(y_m_d, h_m_s_ms); 99628daa8dSBenjamin Herrenschmidt if (rc == OPAL_BUSY_EVENT) 100628daa8dSBenjamin Herrenschmidt opal_poll_events(NULL); 101628daa8dSBenjamin Herrenschmidt else 102628daa8dSBenjamin Herrenschmidt mdelay(10); 103628daa8dSBenjamin Herrenschmidt } 104628daa8dSBenjamin Herrenschmidt return rc == OPAL_SUCCESS ? 0 : -EIO; 105628daa8dSBenjamin Herrenschmidt } 106