0001
0002
0003
0004
0005
0006
0007
0008
0009 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0010
0011 #include <linux/kernel.h>
0012 #include <linux/delay.h>
0013 #include <linux/init.h>
0014 #include <linux/rtc.h>
0015 #include <linux/platform_device.h>
0016
0017 #include <asm/hypervisor.h>
0018
0019 static unsigned long hypervisor_get_time(void)
0020 {
0021 unsigned long ret, time;
0022 int retries = 10000;
0023
0024 retry:
0025 ret = sun4v_tod_get(&time);
0026 if (ret == HV_EOK)
0027 return time;
0028 if (ret == HV_EWOULDBLOCK) {
0029 if (--retries > 0) {
0030 udelay(100);
0031 goto retry;
0032 }
0033 pr_warn("tod_get() timed out.\n");
0034 return 0;
0035 }
0036 pr_warn("tod_get() not supported.\n");
0037 return 0;
0038 }
0039
0040 static int sun4v_read_time(struct device *dev, struct rtc_time *tm)
0041 {
0042 rtc_time64_to_tm(hypervisor_get_time(), tm);
0043 return 0;
0044 }
0045
0046 static int hypervisor_set_time(unsigned long secs)
0047 {
0048 unsigned long ret;
0049 int retries = 10000;
0050
0051 retry:
0052 ret = sun4v_tod_set(secs);
0053 if (ret == HV_EOK)
0054 return 0;
0055 if (ret == HV_EWOULDBLOCK) {
0056 if (--retries > 0) {
0057 udelay(100);
0058 goto retry;
0059 }
0060 pr_warn("tod_set() timed out.\n");
0061 return -EAGAIN;
0062 }
0063 pr_warn("tod_set() not supported.\n");
0064 return -EOPNOTSUPP;
0065 }
0066
0067 static int sun4v_set_time(struct device *dev, struct rtc_time *tm)
0068 {
0069 return hypervisor_set_time(rtc_tm_to_time64(tm));
0070 }
0071
0072 static const struct rtc_class_ops sun4v_rtc_ops = {
0073 .read_time = sun4v_read_time,
0074 .set_time = sun4v_set_time,
0075 };
0076
0077 static int __init sun4v_rtc_probe(struct platform_device *pdev)
0078 {
0079 struct rtc_device *rtc;
0080
0081 rtc = devm_rtc_allocate_device(&pdev->dev);
0082 if (IS_ERR(rtc))
0083 return PTR_ERR(rtc);
0084
0085 rtc->ops = &sun4v_rtc_ops;
0086 rtc->range_max = U64_MAX;
0087 platform_set_drvdata(pdev, rtc);
0088
0089 return devm_rtc_register_device(rtc);
0090 }
0091
0092 static struct platform_driver sun4v_rtc_driver = {
0093 .driver = {
0094 .name = "rtc-sun4v",
0095 },
0096 };
0097
0098 builtin_platform_driver_probe(sun4v_rtc_driver, sun4v_rtc_probe);