Alessandro Zummo | cecf61b | 2008-11-14 16:37:54 -0800 | [diff] [blame] | 1 | /* rtc-sun4v.c: Hypervisor based RTC for SUN4V systems. |
David S. Miller | 7a138ed | 2008-08-29 01:32:43 -0700 | [diff] [blame] | 2 | * |
| 3 | * Copyright (C) 2008 David S. Miller <davem@davemloft.net> |
| 4 | */ |
| 5 | |
Jingoo Han | d959f73 | 2013-02-21 16:45:32 -0800 | [diff] [blame] | 6 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 7 | |
David S. Miller | 7a138ed | 2008-08-29 01:32:43 -0700 | [diff] [blame] | 8 | #include <linux/kernel.h> |
| 9 | #include <linux/module.h> |
| 10 | #include <linux/delay.h> |
| 11 | #include <linux/init.h> |
David S. Miller | 7a138ed | 2008-08-29 01:32:43 -0700 | [diff] [blame] | 12 | #include <linux/rtc.h> |
| 13 | #include <linux/platform_device.h> |
| 14 | |
| 15 | #include <asm/hypervisor.h> |
| 16 | |
David S. Miller | 7a138ed | 2008-08-29 01:32:43 -0700 | [diff] [blame] | 17 | static unsigned long hypervisor_get_time(void) |
| 18 | { |
| 19 | unsigned long ret, time; |
| 20 | int retries = 10000; |
| 21 | |
| 22 | retry: |
| 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 Han | d959f73 | 2013-02-21 16:45:32 -0800 | [diff] [blame] | 31 | pr_warn("tod_get() timed out.\n"); |
David S. Miller | 7a138ed | 2008-08-29 01:32:43 -0700 | [diff] [blame] | 32 | return 0; |
| 33 | } |
Jingoo Han | d959f73 | 2013-02-21 16:45:32 -0800 | [diff] [blame] | 34 | pr_warn("tod_get() not supported.\n"); |
David S. Miller | 7a138ed | 2008-08-29 01:32:43 -0700 | [diff] [blame] | 35 | return 0; |
| 36 | } |
| 37 | |
| 38 | static int sun4v_read_time(struct device *dev, struct rtc_time *tm) |
| 39 | { |
Alessandro Zummo | cecf61b | 2008-11-14 16:37:54 -0800 | [diff] [blame] | 40 | rtc_time_to_tm(hypervisor_get_time(), tm); |
David S. Miller | 7a138ed | 2008-08-29 01:32:43 -0700 | [diff] [blame] | 41 | return 0; |
| 42 | } |
| 43 | |
| 44 | static int hypervisor_set_time(unsigned long secs) |
| 45 | { |
| 46 | unsigned long ret; |
| 47 | int retries = 10000; |
| 48 | |
| 49 | retry: |
| 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 Han | d959f73 | 2013-02-21 16:45:32 -0800 | [diff] [blame] | 58 | pr_warn("tod_set() timed out.\n"); |
David S. Miller | 7a138ed | 2008-08-29 01:32:43 -0700 | [diff] [blame] | 59 | return -EAGAIN; |
| 60 | } |
Jingoo Han | d959f73 | 2013-02-21 16:45:32 -0800 | [diff] [blame] | 61 | pr_warn("tod_set() not supported.\n"); |
David S. Miller | 7a138ed | 2008-08-29 01:32:43 -0700 | [diff] [blame] | 62 | return -EOPNOTSUPP; |
| 63 | } |
| 64 | |
| 65 | static int sun4v_set_time(struct device *dev, struct rtc_time *tm) |
| 66 | { |
Alessandro Zummo | cecf61b | 2008-11-14 16:37:54 -0800 | [diff] [blame] | 67 | unsigned long secs; |
David S. Miller | 7a138ed | 2008-08-29 01:32:43 -0700 | [diff] [blame] | 68 | int err; |
| 69 | |
| 70 | err = rtc_tm_to_time(tm, &secs); |
| 71 | if (err) |
| 72 | return err; |
| 73 | |
Alessandro Zummo | cecf61b | 2008-11-14 16:37:54 -0800 | [diff] [blame] | 74 | return hypervisor_set_time(secs); |
David S. Miller | 7a138ed | 2008-08-29 01:32:43 -0700 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | static const struct rtc_class_ops sun4v_rtc_ops = { |
| 78 | .read_time = sun4v_read_time, |
| 79 | .set_time = sun4v_set_time, |
| 80 | }; |
| 81 | |
Alessandro Zummo | cecf61b | 2008-11-14 16:37:54 -0800 | [diff] [blame] | 82 | static int __init sun4v_rtc_probe(struct platform_device *pdev) |
David S. Miller | 7a138ed | 2008-08-29 01:32:43 -0700 | [diff] [blame] | 83 | { |
Alessandro Zummo | cecf61b | 2008-11-14 16:37:54 -0800 | [diff] [blame] | 84 | struct rtc_device *rtc = rtc_device_register("sun4v", &pdev->dev, |
David S. Miller | 7a138ed | 2008-08-29 01:32:43 -0700 | [diff] [blame] | 85 | &sun4v_rtc_ops, THIS_MODULE); |
Alessandro Zummo | cecf61b | 2008-11-14 16:37:54 -0800 | [diff] [blame] | 86 | if (IS_ERR(rtc)) |
| 87 | return PTR_ERR(rtc); |
| 88 | |
| 89 | platform_set_drvdata(pdev, rtc); |
David S. Miller | 7a138ed | 2008-08-29 01:32:43 -0700 | [diff] [blame] | 90 | return 0; |
| 91 | } |
| 92 | |
Alessandro Zummo | cecf61b | 2008-11-14 16:37:54 -0800 | [diff] [blame] | 93 | static int __exit sun4v_rtc_remove(struct platform_device *pdev) |
David S. Miller | 7a138ed | 2008-08-29 01:32:43 -0700 | [diff] [blame] | 94 | { |
Alessandro Zummo | cecf61b | 2008-11-14 16:37:54 -0800 | [diff] [blame] | 95 | struct rtc_device *rtc = platform_get_drvdata(pdev); |
David S. Miller | 7a138ed | 2008-08-29 01:32:43 -0700 | [diff] [blame] | 96 | |
Alessandro Zummo | cecf61b | 2008-11-14 16:37:54 -0800 | [diff] [blame] | 97 | rtc_device_unregister(rtc); |
David S. Miller | 7a138ed | 2008-08-29 01:32:43 -0700 | [diff] [blame] | 98 | return 0; |
| 99 | } |
| 100 | |
| 101 | static struct platform_driver sun4v_rtc_driver = { |
| 102 | .driver = { |
| 103 | .name = "rtc-sun4v", |
| 104 | .owner = THIS_MODULE, |
| 105 | }, |
Alessandro Zummo | cecf61b | 2008-11-14 16:37:54 -0800 | [diff] [blame] | 106 | .remove = __exit_p(sun4v_rtc_remove), |
David S. Miller | 7a138ed | 2008-08-29 01:32:43 -0700 | [diff] [blame] | 107 | }; |
| 108 | |
| 109 | static int __init sun4v_rtc_init(void) |
| 110 | { |
Alessandro Zummo | cecf61b | 2008-11-14 16:37:54 -0800 | [diff] [blame] | 111 | return platform_driver_probe(&sun4v_rtc_driver, sun4v_rtc_probe); |
David S. Miller | 7a138ed | 2008-08-29 01:32:43 -0700 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | static void __exit sun4v_rtc_exit(void) |
| 115 | { |
| 116 | platform_driver_unregister(&sun4v_rtc_driver); |
| 117 | } |
| 118 | |
| 119 | module_init(sun4v_rtc_init); |
| 120 | module_exit(sun4v_rtc_exit); |
Alessandro Zummo | cecf61b | 2008-11-14 16:37:54 -0800 | [diff] [blame] | 121 | |
| 122 | MODULE_AUTHOR("David S. Miller <davem@davemloft.net>"); |
| 123 | MODULE_DESCRIPTION("SUN4V RTC driver"); |
| 124 | MODULE_LICENSE("GPL"); |