blob: 036463dfa103a771ddb08fe0e45631075956cf3d [file] [log] [blame]
Alexandre Belloni64bef022019-03-22 08:22:56 +01001// SPDX-License-Identifier: GPL-2.0
Alessandro Zummocecf61b2008-11-14 16:37:54 -08002/* rtc-sun4v.c: Hypervisor based RTC for SUN4V systems.
David S. Miller7a138ed2008-08-29 01:32:43 -07003 *
Paul Gortmaker8b610252016-10-31 14:55:27 -04004 * Author: David S. Miller
Paul Gortmaker8b610252016-10-31 14:55:27 -04005 *
David S. Miller7a138ed2008-08-29 01:32:43 -07006 * Copyright (C) 2008 David S. Miller <davem@davemloft.net>
7 */
8
Jingoo Hand959f732013-02-21 16:45:32 -08009#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10
David S. Miller7a138ed2008-08-29 01:32:43 -070011#include <linux/kernel.h>
David S. Miller7a138ed2008-08-29 01:32:43 -070012#include <linux/delay.h>
13#include <linux/init.h>
David S. Miller7a138ed2008-08-29 01:32:43 -070014#include <linux/rtc.h>
15#include <linux/platform_device.h>
16
17#include <asm/hypervisor.h>
18
David S. Miller7a138ed2008-08-29 01:32:43 -070019static unsigned long hypervisor_get_time(void)
20{
21 unsigned long ret, time;
22 int retries = 10000;
23
24retry:
25 ret = sun4v_tod_get(&time);
26 if (ret == HV_EOK)
27 return time;
28 if (ret == HV_EWOULDBLOCK) {
29 if (--retries > 0) {
30 udelay(100);
31 goto retry;
32 }
Jingoo Hand959f732013-02-21 16:45:32 -080033 pr_warn("tod_get() timed out.\n");
David S. Miller7a138ed2008-08-29 01:32:43 -070034 return 0;
35 }
Jingoo Hand959f732013-02-21 16:45:32 -080036 pr_warn("tod_get() not supported.\n");
David S. Miller7a138ed2008-08-29 01:32:43 -070037 return 0;
38}
39
40static int sun4v_read_time(struct device *dev, struct rtc_time *tm)
41{
Alexandre Belloni65c6f632019-03-22 08:22:54 +010042 rtc_time64_to_tm(hypervisor_get_time(), tm);
David S. Miller7a138ed2008-08-29 01:32:43 -070043 return 0;
44}
45
46static int hypervisor_set_time(unsigned long secs)
47{
48 unsigned long ret;
49 int retries = 10000;
50
51retry:
52 ret = sun4v_tod_set(secs);
53 if (ret == HV_EOK)
54 return 0;
55 if (ret == HV_EWOULDBLOCK) {
56 if (--retries > 0) {
57 udelay(100);
58 goto retry;
59 }
Jingoo Hand959f732013-02-21 16:45:32 -080060 pr_warn("tod_set() timed out.\n");
David S. Miller7a138ed2008-08-29 01:32:43 -070061 return -EAGAIN;
62 }
Jingoo Hand959f732013-02-21 16:45:32 -080063 pr_warn("tod_set() not supported.\n");
David S. Miller7a138ed2008-08-29 01:32:43 -070064 return -EOPNOTSUPP;
65}
66
67static int sun4v_set_time(struct device *dev, struct rtc_time *tm)
68{
Alexandre Belloni65c6f632019-03-22 08:22:54 +010069 return hypervisor_set_time(rtc_tm_to_time64(tm));
David S. Miller7a138ed2008-08-29 01:32:43 -070070}
71
72static const struct rtc_class_ops sun4v_rtc_ops = {
73 .read_time = sun4v_read_time,
74 .set_time = sun4v_set_time,
75};
76
Alessandro Zummocecf61b2008-11-14 16:37:54 -080077static int __init sun4v_rtc_probe(struct platform_device *pdev)
David S. Miller7a138ed2008-08-29 01:32:43 -070078{
Jingoo Hancc40d642013-04-29 16:19:51 -070079 struct rtc_device *rtc;
80
Alexandre Belloni3ec99d62019-03-22 08:22:55 +010081 rtc = devm_rtc_allocate_device(&pdev->dev);
Alessandro Zummocecf61b2008-11-14 16:37:54 -080082 if (IS_ERR(rtc))
83 return PTR_ERR(rtc);
84
Alexandre Belloni3ec99d62019-03-22 08:22:55 +010085 rtc->ops = &sun4v_rtc_ops;
86 rtc->range_max = U64_MAX;
Alessandro Zummocecf61b2008-11-14 16:37:54 -080087 platform_set_drvdata(pdev, rtc);
Alexandre Belloni3ec99d62019-03-22 08:22:55 +010088
89 return rtc_register_device(rtc);
David S. Miller7a138ed2008-08-29 01:32:43 -070090}
91
David S. Miller7a138ed2008-08-29 01:32:43 -070092static struct platform_driver sun4v_rtc_driver = {
93 .driver = {
94 .name = "rtc-sun4v",
David S. Miller7a138ed2008-08-29 01:32:43 -070095 },
David S. Miller7a138ed2008-08-29 01:32:43 -070096};
97
Paul Gortmaker8b610252016-10-31 14:55:27 -040098builtin_platform_driver_probe(sun4v_rtc_driver, sun4v_rtc_probe);