blob: ce42e5fa9e09f6e57f43afda93ab5e39ba74bbd1 [file] [log] [blame]
Alessandro Zummocecf61b2008-11-14 16:37:54 -08001/* rtc-sun4v.c: Hypervisor based RTC for SUN4V systems.
David S. Miller7a138ed2008-08-29 01:32:43 -07002 *
3 * Copyright (C) 2008 David S. Miller <davem@davemloft.net>
4 */
5
Jingoo Hand959f732013-02-21 16:45:32 -08006#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
7
David S. Miller7a138ed2008-08-29 01:32:43 -07008#include <linux/kernel.h>
9#include <linux/module.h>
10#include <linux/delay.h>
11#include <linux/init.h>
David S. Miller7a138ed2008-08-29 01:32:43 -070012#include <linux/rtc.h>
13#include <linux/platform_device.h>
14
15#include <asm/hypervisor.h>
16
David S. Miller7a138ed2008-08-29 01:32:43 -070017static unsigned long hypervisor_get_time(void)
18{
19 unsigned long ret, time;
20 int retries = 10000;
21
22retry:
23 ret = sun4v_tod_get(&time);
24 if (ret == HV_EOK)
25 return time;
26 if (ret == HV_EWOULDBLOCK) {
27 if (--retries > 0) {
28 udelay(100);
29 goto retry;
30 }
Jingoo Hand959f732013-02-21 16:45:32 -080031 pr_warn("tod_get() timed out.\n");
David S. Miller7a138ed2008-08-29 01:32:43 -070032 return 0;
33 }
Jingoo Hand959f732013-02-21 16:45:32 -080034 pr_warn("tod_get() not supported.\n");
David S. Miller7a138ed2008-08-29 01:32:43 -070035 return 0;
36}
37
38static int sun4v_read_time(struct device *dev, struct rtc_time *tm)
39{
Alessandro Zummocecf61b2008-11-14 16:37:54 -080040 rtc_time_to_tm(hypervisor_get_time(), tm);
David S. Miller7a138ed2008-08-29 01:32:43 -070041 return 0;
42}
43
44static int hypervisor_set_time(unsigned long secs)
45{
46 unsigned long ret;
47 int retries = 10000;
48
49retry:
50 ret = sun4v_tod_set(secs);
51 if (ret == HV_EOK)
52 return 0;
53 if (ret == HV_EWOULDBLOCK) {
54 if (--retries > 0) {
55 udelay(100);
56 goto retry;
57 }
Jingoo Hand959f732013-02-21 16:45:32 -080058 pr_warn("tod_set() timed out.\n");
David S. Miller7a138ed2008-08-29 01:32:43 -070059 return -EAGAIN;
60 }
Jingoo Hand959f732013-02-21 16:45:32 -080061 pr_warn("tod_set() not supported.\n");
David S. Miller7a138ed2008-08-29 01:32:43 -070062 return -EOPNOTSUPP;
63}
64
65static int sun4v_set_time(struct device *dev, struct rtc_time *tm)
66{
Alessandro Zummocecf61b2008-11-14 16:37:54 -080067 unsigned long secs;
David S. Miller7a138ed2008-08-29 01:32:43 -070068 int err;
69
70 err = rtc_tm_to_time(tm, &secs);
71 if (err)
72 return err;
73
Alessandro Zummocecf61b2008-11-14 16:37:54 -080074 return hypervisor_set_time(secs);
David S. Miller7a138ed2008-08-29 01:32:43 -070075}
76
77static const struct rtc_class_ops sun4v_rtc_ops = {
78 .read_time = sun4v_read_time,
79 .set_time = sun4v_set_time,
80};
81
Alessandro Zummocecf61b2008-11-14 16:37:54 -080082static int __init sun4v_rtc_probe(struct platform_device *pdev)
David S. Miller7a138ed2008-08-29 01:32:43 -070083{
Jingoo Hancc40d642013-04-29 16:19:51 -070084 struct rtc_device *rtc;
85
86 rtc = devm_rtc_device_register(&pdev->dev, "sun4v",
87 &sun4v_rtc_ops, THIS_MODULE);
Alessandro Zummocecf61b2008-11-14 16:37:54 -080088 if (IS_ERR(rtc))
89 return PTR_ERR(rtc);
90
91 platform_set_drvdata(pdev, rtc);
David S. Miller7a138ed2008-08-29 01:32:43 -070092 return 0;
93}
94
Alessandro Zummocecf61b2008-11-14 16:37:54 -080095static int __exit sun4v_rtc_remove(struct platform_device *pdev)
David S. Miller7a138ed2008-08-29 01:32:43 -070096{
David S. Miller7a138ed2008-08-29 01:32:43 -070097 return 0;
98}
99
100static struct platform_driver sun4v_rtc_driver = {
101 .driver = {
102 .name = "rtc-sun4v",
103 .owner = THIS_MODULE,
104 },
Alessandro Zummocecf61b2008-11-14 16:37:54 -0800105 .remove = __exit_p(sun4v_rtc_remove),
David S. Miller7a138ed2008-08-29 01:32:43 -0700106};
107
Jingoo Han61ce8252013-04-29 16:18:52 -0700108module_platform_driver_probe(sun4v_rtc_driver, sun4v_rtc_probe);
Alessandro Zummocecf61b2008-11-14 16:37:54 -0800109
110MODULE_AUTHOR("David S. Miller <davem@davemloft.net>");
111MODULE_DESCRIPTION("SUN4V RTC driver");
112MODULE_LICENSE("GPL");