blob: aa2a5139462ea5f6c81f51c8f025c9af84ce0be2 [file] [log] [blame]
Benjamin Herrenschmidt628daa82011-09-19 17:45:01 +00001/*
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 Gupta16b1d262014-10-14 14:08:36 +053018#include <linux/platform_device.h>
19#include <linux/of_platform.h>
Benjamin Herrenschmidt628daa82011-09-19 17:45:01 +000020
21#include <asm/opal.h>
22#include <asm/firmware.h>
Michael Neuling2c491952013-11-29 14:22:48 +110023#include <asm/machdep.h>
Benjamin Herrenschmidt628daa82011-09-19 17:45:01 +000024
25static 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);
Daniel Axtens00b912b2015-12-15 18:09:14 +110034 tm->tm_wday = -1;
Benjamin Herrenschmidt628daa82011-09-19 17:45:01 +000035}
36
37unsigned long __init opal_get_boot_time(void)
38{
39 struct rtc_time tm;
40 u32 y_m_d;
41 u64 h_m_s_ms;
Anton Blanchard6feff6d2013-09-23 12:05:05 +100042 __be32 __y_m_d;
43 __be64 __h_m_s_ms;
Benjamin Herrenschmidt628daa82011-09-19 17:45:01 +000044 long rc = OPAL_BUSY;
45
Michael Neuling035ed262014-08-19 14:48:00 +100046 if (!opal_check_token(OPAL_RTC_READ))
Neelesh Gupta16b1d262014-10-14 14:08:36 +053047 return 0;
Michael Neuling035ed262014-08-19 14:48:00 +100048
Benjamin Herrenschmidt628daa82011-09-19 17:45:01 +000049 while (rc == OPAL_BUSY || rc == OPAL_BUSY_EVENT) {
Anton Blanchard6feff6d2013-09-23 12:05:05 +100050 rc = opal_rtc_read(&__y_m_d, &__h_m_s_ms);
Nicholas Piggina2a8b262018-04-10 21:49:32 +100051 if (rc == OPAL_BUSY_EVENT) {
52 mdelay(OPAL_BUSY_DELAY_MS);
Benjamin Herrenschmidt628daa82011-09-19 17:45:01 +000053 opal_poll_events(NULL);
Nicholas Piggina2a8b262018-04-10 21:49:32 +100054 } else if (rc == OPAL_BUSY) {
55 mdelay(OPAL_BUSY_DELAY_MS);
56 }
Benjamin Herrenschmidt628daa82011-09-19 17:45:01 +000057 }
Michael Neuling035ed262014-08-19 14:48:00 +100058 if (rc != OPAL_SUCCESS)
Neelesh Gupta16b1d262014-10-14 14:08:36 +053059 return 0;
Michael Neuling035ed262014-08-19 14:48:00 +100060
Anton Blanchard6feff6d2013-09-23 12:05:05 +100061 y_m_d = be32_to_cpu(__y_m_d);
62 h_m_s_ms = be64_to_cpu(__h_m_s_ms);
Benjamin Herrenschmidt628daa82011-09-19 17:45:01 +000063 opal_to_tm(y_m_d, h_m_s_ms, &tm);
64 return mktime(tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
65 tm.tm_hour, tm.tm_min, tm.tm_sec);
66}
67
Neelesh Gupta16b1d262014-10-14 14:08:36 +053068static __init int opal_time_init(void)
Benjamin Herrenschmidt628daa82011-09-19 17:45:01 +000069{
Neelesh Gupta16b1d262014-10-14 14:08:36 +053070 struct platform_device *pdev;
71 struct device_node *rtc;
Benjamin Herrenschmidt628daa82011-09-19 17:45:01 +000072
Neelesh Gupta16b1d262014-10-14 14:08:36 +053073 rtc = of_find_node_by_path("/ibm,opal/rtc");
74 if (rtc) {
75 pdev = of_platform_device_create(rtc, "opal-rtc", NULL);
76 of_node_put(rtc);
77 } else {
78 if (opal_check_token(OPAL_RTC_READ) ||
79 opal_check_token(OPAL_READ_TPO))
80 pdev = platform_device_register_simple("opal-rtc", -1,
81 NULL, 0);
Benjamin Herrenschmidt628daa82011-09-19 17:45:01 +000082 else
Neelesh Gupta16b1d262014-10-14 14:08:36 +053083 return -ENODEV;
Benjamin Herrenschmidt628daa82011-09-19 17:45:01 +000084 }
Neelesh Gupta16b1d262014-10-14 14:08:36 +053085
86 return PTR_ERR_OR_ZERO(pdev);
Benjamin Herrenschmidt628daa82011-09-19 17:45:01 +000087}
Neelesh Gupta16b1d262014-10-14 14:08:36 +053088machine_subsys_initcall(powernv, opal_time_init);