Benjamin Herrenschmidt | 628daa8 | 2011-09-19 17:45:01 +0000 | [diff] [blame] | 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> |
Neelesh Gupta | 16b1d26 | 2014-10-14 14:08:36 +0530 | [diff] [blame] | 18 | #include <linux/platform_device.h> |
| 19 | #include <linux/of_platform.h> |
Benjamin Herrenschmidt | 628daa8 | 2011-09-19 17:45:01 +0000 | [diff] [blame] | 20 | |
| 21 | #include <asm/opal.h> |
| 22 | #include <asm/firmware.h> |
Michael Neuling | 2c49195 | 2013-11-29 14:22:48 +1100 | [diff] [blame] | 23 | #include <asm/machdep.h> |
Benjamin Herrenschmidt | 628daa8 | 2011-09-19 17:45:01 +0000 | [diff] [blame] | 24 | |
| 25 | static void opal_to_tm(u32 y_m_d, u64 h_m_s_ms, struct rtc_time *tm) |
| 26 | { |
| 27 | tm->tm_year = ((bcd2bin(y_m_d >> 24) * 100) + |
| 28 | bcd2bin((y_m_d >> 16) & 0xff)) - 1900; |
| 29 | tm->tm_mon = bcd2bin((y_m_d >> 8) & 0xff) - 1; |
| 30 | tm->tm_mday = bcd2bin(y_m_d & 0xff); |
| 31 | tm->tm_hour = bcd2bin((h_m_s_ms >> 56) & 0xff); |
| 32 | tm->tm_min = bcd2bin((h_m_s_ms >> 48) & 0xff); |
| 33 | tm->tm_sec = bcd2bin((h_m_s_ms >> 40) & 0xff); |
| 34 | |
| 35 | GregorianDay(tm); |
| 36 | } |
| 37 | |
| 38 | unsigned long __init opal_get_boot_time(void) |
| 39 | { |
| 40 | struct rtc_time tm; |
| 41 | u32 y_m_d; |
| 42 | u64 h_m_s_ms; |
Anton Blanchard | 6feff6d | 2013-09-23 12:05:05 +1000 | [diff] [blame] | 43 | __be32 __y_m_d; |
| 44 | __be64 __h_m_s_ms; |
Benjamin Herrenschmidt | 628daa8 | 2011-09-19 17:45:01 +0000 | [diff] [blame] | 45 | long rc = OPAL_BUSY; |
| 46 | |
Michael Neuling | 035ed26 | 2014-08-19 14:48:00 +1000 | [diff] [blame] | 47 | if (!opal_check_token(OPAL_RTC_READ)) |
Neelesh Gupta | 16b1d26 | 2014-10-14 14:08:36 +0530 | [diff] [blame] | 48 | return 0; |
Michael Neuling | 035ed26 | 2014-08-19 14:48:00 +1000 | [diff] [blame] | 49 | |
Benjamin Herrenschmidt | 628daa8 | 2011-09-19 17:45:01 +0000 | [diff] [blame] | 50 | while (rc == OPAL_BUSY || rc == OPAL_BUSY_EVENT) { |
Anton Blanchard | 6feff6d | 2013-09-23 12:05:05 +1000 | [diff] [blame] | 51 | rc = opal_rtc_read(&__y_m_d, &__h_m_s_ms); |
Benjamin Herrenschmidt | 628daa8 | 2011-09-19 17:45:01 +0000 | [diff] [blame] | 52 | if (rc == OPAL_BUSY_EVENT) |
| 53 | opal_poll_events(NULL); |
| 54 | else |
| 55 | mdelay(10); |
| 56 | } |
Michael Neuling | 035ed26 | 2014-08-19 14:48:00 +1000 | [diff] [blame] | 57 | if (rc != OPAL_SUCCESS) |
Neelesh Gupta | 16b1d26 | 2014-10-14 14:08:36 +0530 | [diff] [blame] | 58 | return 0; |
Michael Neuling | 035ed26 | 2014-08-19 14:48:00 +1000 | [diff] [blame] | 59 | |
Anton Blanchard | 6feff6d | 2013-09-23 12:05:05 +1000 | [diff] [blame] | 60 | y_m_d = be32_to_cpu(__y_m_d); |
| 61 | h_m_s_ms = be64_to_cpu(__h_m_s_ms); |
Benjamin Herrenschmidt | 628daa8 | 2011-09-19 17:45:01 +0000 | [diff] [blame] | 62 | opal_to_tm(y_m_d, h_m_s_ms, &tm); |
| 63 | return mktime(tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, |
| 64 | tm.tm_hour, tm.tm_min, tm.tm_sec); |
| 65 | } |
| 66 | |
Neelesh Gupta | 16b1d26 | 2014-10-14 14:08:36 +0530 | [diff] [blame] | 67 | static __init int opal_time_init(void) |
Benjamin Herrenschmidt | 628daa8 | 2011-09-19 17:45:01 +0000 | [diff] [blame] | 68 | { |
Neelesh Gupta | 16b1d26 | 2014-10-14 14:08:36 +0530 | [diff] [blame] | 69 | struct platform_device *pdev; |
| 70 | struct device_node *rtc; |
Benjamin Herrenschmidt | 628daa8 | 2011-09-19 17:45:01 +0000 | [diff] [blame] | 71 | |
Neelesh Gupta | 16b1d26 | 2014-10-14 14:08:36 +0530 | [diff] [blame] | 72 | rtc = of_find_node_by_path("/ibm,opal/rtc"); |
| 73 | if (rtc) { |
| 74 | pdev = of_platform_device_create(rtc, "opal-rtc", NULL); |
| 75 | of_node_put(rtc); |
| 76 | } else { |
| 77 | if (opal_check_token(OPAL_RTC_READ) || |
| 78 | opal_check_token(OPAL_READ_TPO)) |
| 79 | pdev = platform_device_register_simple("opal-rtc", -1, |
| 80 | NULL, 0); |
Benjamin Herrenschmidt | 628daa8 | 2011-09-19 17:45:01 +0000 | [diff] [blame] | 81 | else |
Neelesh Gupta | 16b1d26 | 2014-10-14 14:08:36 +0530 | [diff] [blame] | 82 | return -ENODEV; |
Benjamin Herrenschmidt | 628daa8 | 2011-09-19 17:45:01 +0000 | [diff] [blame] | 83 | } |
Neelesh Gupta | 16b1d26 | 2014-10-14 14:08:36 +0530 | [diff] [blame] | 84 | |
| 85 | return PTR_ERR_OR_ZERO(pdev); |
Benjamin Herrenschmidt | 628daa8 | 2011-09-19 17:45:01 +0000 | [diff] [blame] | 86 | } |
Neelesh Gupta | 16b1d26 | 2014-10-14 14:08:36 +0530 | [diff] [blame] | 87 | machine_subsys_initcall(powernv, opal_time_init); |