rtc-sun4v.c 1.9 KB

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