rtc-sun4v.c 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* rtc-sun4v.c: Hypervisor based RTC for SUN4V systems.
  3. *
  4. * Author: David S. Miller
  5. *
  6. * Copyright (C) 2008 David S. Miller <[email protected]>
  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_time64_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. return hypervisor_set_time(rtc_tm_to_time64(tm));
  61. }
  62. static const struct rtc_class_ops sun4v_rtc_ops = {
  63. .read_time = sun4v_read_time,
  64. .set_time = sun4v_set_time,
  65. };
  66. static int __init sun4v_rtc_probe(struct platform_device *pdev)
  67. {
  68. struct rtc_device *rtc;
  69. rtc = devm_rtc_allocate_device(&pdev->dev);
  70. if (IS_ERR(rtc))
  71. return PTR_ERR(rtc);
  72. rtc->ops = &sun4v_rtc_ops;
  73. rtc->range_max = U64_MAX;
  74. platform_set_drvdata(pdev, rtc);
  75. return devm_rtc_register_device(rtc);
  76. }
  77. static struct platform_driver sun4v_rtc_driver = {
  78. .driver = {
  79. .name = "rtc-sun4v",
  80. },
  81. };
  82. builtin_platform_driver_probe(sun4v_rtc_driver, sun4v_rtc_probe);