1 /* rtc-sun4c.c: Hypervisor based RTC for SUN4V systems.
3 * Copyright (C) 2008 David S. Miller <davem@davemloft.net>
6 #include <linux/kernel.h>
7 #include <linux/module.h>
8 #include <linux/delay.h>
9 #include <linux/init.h>
10 #include <linux/time.h>
11 #include <linux/rtc.h>
12 #include <linux/platform_device.h>
14 #include <asm/hypervisor.h>
16 MODULE_AUTHOR("David S. Miller <davem@davemloft.net>");
17 MODULE_DESCRIPTION("SUN4V RTC driver");
18 MODULE_LICENSE("GPL");
21 struct rtc_device *rtc;
25 static unsigned long hypervisor_get_time(void)
27 unsigned long ret, time;
31 ret = sun4v_tod_get(&time);
34 if (ret == HV_EWOULDBLOCK) {
39 printk(KERN_WARNING "SUN4V: tod_get() timed out.\n");
42 printk(KERN_WARNING "SUN4V: tod_get() not supported.\n");
46 static int sun4v_read_time(struct device *dev, struct rtc_time *tm)
48 struct sun4v_rtc *p = dev_get_drvdata(dev);
49 unsigned long flags, secs;
51 spin_lock_irqsave(&p->lock, flags);
52 secs = hypervisor_get_time();
53 spin_unlock_irqrestore(&p->lock, flags);
55 rtc_time_to_tm(secs, tm);
60 static int hypervisor_set_time(unsigned long secs)
66 ret = sun4v_tod_set(secs);
69 if (ret == HV_EWOULDBLOCK) {
74 printk(KERN_WARNING "SUN4V: tod_set() timed out.\n");
77 printk(KERN_WARNING "SUN4V: tod_set() not supported.\n");
81 static int sun4v_set_time(struct device *dev, struct rtc_time *tm)
83 struct sun4v_rtc *p = dev_get_drvdata(dev);
84 unsigned long flags, secs;
87 err = rtc_tm_to_time(tm, &secs);
91 spin_lock_irqsave(&p->lock, flags);
92 err = hypervisor_set_time(secs);
93 spin_unlock_irqrestore(&p->lock, flags);
98 static const struct rtc_class_ops sun4v_rtc_ops = {
99 .read_time = sun4v_read_time,
100 .set_time = sun4v_set_time,
103 static int __devinit sun4v_rtc_probe(struct platform_device *pdev)
105 struct sun4v_rtc *p = kzalloc(sizeof(*p), GFP_KERNEL);
110 spin_lock_init(&p->lock);
112 p->rtc = rtc_device_register("sun4v", &pdev->dev,
113 &sun4v_rtc_ops, THIS_MODULE);
114 if (IS_ERR(p->rtc)) {
115 int err = PTR_ERR(p->rtc);
119 platform_set_drvdata(pdev, p);
123 static int __devexit sun4v_rtc_remove(struct platform_device *pdev)
125 struct sun4v_rtc *p = platform_get_drvdata(pdev);
127 rtc_device_unregister(p->rtc);
133 static struct platform_driver sun4v_rtc_driver = {
136 .owner = THIS_MODULE,
138 .probe = sun4v_rtc_probe,
139 .remove = __devexit_p(sun4v_rtc_remove),
142 static int __init sun4v_rtc_init(void)
144 return platform_driver_register(&sun4v_rtc_driver);
147 static void __exit sun4v_rtc_exit(void)
149 platform_driver_unregister(&sun4v_rtc_driver);
152 module_init(sun4v_rtc_init);
153 module_exit(sun4v_rtc_exit);