rtc-sun4v.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /* rtc-sun4v.c: Hypervisor based RTC for SUN4V systems.
  2. *
  3. * Copyright (C) 2008 David S. Miller <davem@davemloft.net>
  4. */
  5. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  6. #include <linux/kernel.h>
  7. #include <linux/module.h>
  8. #include <linux/delay.h>
  9. #include <linux/init.h>
  10. #include <linux/rtc.h>
  11. #include <linux/platform_device.h>
  12. #include <asm/hypervisor.h>
  13. static unsigned long hypervisor_get_time(void)
  14. {
  15. unsigned long ret, time;
  16. int retries = 10000;
  17. retry:
  18. ret = sun4v_tod_get(&time);
  19. if (ret == HV_EOK)
  20. return time;
  21. if (ret == HV_EWOULDBLOCK) {
  22. if (--retries > 0) {
  23. udelay(100);
  24. goto retry;
  25. }
  26. pr_warn("tod_get() timed out.\n");
  27. return 0;
  28. }
  29. pr_warn("tod_get() not supported.\n");
  30. return 0;
  31. }
  32. static int sun4v_read_time(struct device *dev, struct rtc_time *tm)
  33. {
  34. rtc_time_to_tm(hypervisor_get_time(), tm);
  35. return 0;
  36. }
  37. static int hypervisor_set_time(unsigned long secs)
  38. {
  39. unsigned long ret;
  40. int retries = 10000;
  41. retry:
  42. ret = sun4v_tod_set(secs);
  43. if (ret == HV_EOK)
  44. return 0;
  45. if (ret == HV_EWOULDBLOCK) {
  46. if (--retries > 0) {
  47. udelay(100);
  48. goto retry;
  49. }
  50. pr_warn("tod_set() timed out.\n");
  51. return -EAGAIN;
  52. }
  53. pr_warn("tod_set() not supported.\n");
  54. return -EOPNOTSUPP;
  55. }
  56. static int sun4v_set_time(struct device *dev, struct rtc_time *tm)
  57. {
  58. unsigned long secs;
  59. int err;
  60. err = rtc_tm_to_time(tm, &secs);
  61. if (err)
  62. return err;
  63. return hypervisor_set_time(secs);
  64. }
  65. static const struct rtc_class_ops sun4v_rtc_ops = {
  66. .read_time = sun4v_read_time,
  67. .set_time = sun4v_set_time,
  68. };
  69. static int __init sun4v_rtc_probe(struct platform_device *pdev)
  70. {
  71. struct rtc_device *rtc;
  72. rtc = devm_rtc_device_register(&pdev->dev, "sun4v",
  73. &sun4v_rtc_ops, THIS_MODULE);
  74. if (IS_ERR(rtc))
  75. return PTR_ERR(rtc);
  76. platform_set_drvdata(pdev, rtc);
  77. return 0;
  78. }
  79. static struct platform_driver sun4v_rtc_driver = {
  80. .driver = {
  81. .name = "rtc-sun4v",
  82. },
  83. };
  84. module_platform_driver_probe(sun4v_rtc_driver, sun4v_rtc_probe);
  85. MODULE_AUTHOR("David S. Miller <davem@davemloft.net>");
  86. MODULE_DESCRIPTION("SUN4V RTC driver");
  87. MODULE_LICENSE("GPL");