1 // SPDX-License-Identifier: GPL-2.0
2 /* rtc-sun4v.c: Hypervisor based RTC for SUN4V systems.
4 * Author: David S. Miller
6 * Copyright (C) 2008 David S. Miller <davem@davemloft.net>
9 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
11 #include <linux/kernel.h>
12 #include <linux/delay.h>
13 #include <linux/init.h>
14 #include <linux/rtc.h>
15 #include <linux/platform_device.h>
17 #include <asm/hypervisor.h>
19 static unsigned long hypervisor_get_time(void)
21 unsigned long ret
, time
;
25 ret
= sun4v_tod_get(&time
);
28 if (ret
== HV_EWOULDBLOCK
) {
33 pr_warn("tod_get() timed out.\n");
36 pr_warn("tod_get() not supported.\n");
40 static int sun4v_read_time(struct device
*dev
, struct rtc_time
*tm
)
42 rtc_time64_to_tm(hypervisor_get_time(), tm
);
46 static int hypervisor_set_time(unsigned long secs
)
52 ret
= sun4v_tod_set(secs
);
55 if (ret
== HV_EWOULDBLOCK
) {
60 pr_warn("tod_set() timed out.\n");
63 pr_warn("tod_set() not supported.\n");
67 static int sun4v_set_time(struct device
*dev
, struct rtc_time
*tm
)
69 return hypervisor_set_time(rtc_tm_to_time64(tm
));
72 static const struct rtc_class_ops sun4v_rtc_ops
= {
73 .read_time
= sun4v_read_time
,
74 .set_time
= sun4v_set_time
,
77 static int __init
sun4v_rtc_probe(struct platform_device
*pdev
)
79 struct rtc_device
*rtc
;
81 rtc
= devm_rtc_allocate_device(&pdev
->dev
);
85 rtc
->ops
= &sun4v_rtc_ops
;
86 rtc
->range_max
= U64_MAX
;
87 platform_set_drvdata(pdev
, rtc
);
89 return rtc_register_device(rtc
);
92 static struct platform_driver sun4v_rtc_driver
= {
98 builtin_platform_driver_probe(sun4v_rtc_driver
, sun4v_rtc_probe
);