rtc.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * arch/sh/boards/dreamcast/rtc.c
  4. *
  5. * Dreamcast AICA RTC routines.
  6. *
  7. * Copyright (c) 2001, 2002 M. R. Brown <[email protected]>
  8. * Copyright (c) 2002 Paul Mundt <[email protected]>
  9. */
  10. #include <linux/time.h>
  11. #include <linux/rtc.h>
  12. #include <linux/io.h>
  13. #include <linux/platform_device.h>
  14. /* The AICA RTC has an Epoch of 1/1/1950, so we must subtract 20 years (in
  15. seconds) to get the standard Unix Epoch when getting the time, and add
  16. 20 years when setting the time. */
  17. #define TWENTY_YEARS ((20 * 365LU + 5) * 86400)
  18. /* The AICA RTC is represented by a 32-bit seconds counter stored in 2 16-bit
  19. registers.*/
  20. #define AICA_RTC_SECS_H 0xa0710000
  21. #define AICA_RTC_SECS_L 0xa0710004
  22. /**
  23. * aica_rtc_gettimeofday - Get the time from the AICA RTC
  24. * @dev: the RTC device (ignored)
  25. * @tm: pointer to resulting RTC time structure
  26. *
  27. * Grabs the current RTC seconds counter and adjusts it to the Unix Epoch.
  28. */
  29. static int aica_rtc_gettimeofday(struct device *dev, struct rtc_time *tm)
  30. {
  31. unsigned long val1, val2;
  32. time64_t t;
  33. do {
  34. val1 = ((__raw_readl(AICA_RTC_SECS_H) & 0xffff) << 16) |
  35. (__raw_readl(AICA_RTC_SECS_L) & 0xffff);
  36. val2 = ((__raw_readl(AICA_RTC_SECS_H) & 0xffff) << 16) |
  37. (__raw_readl(AICA_RTC_SECS_L) & 0xffff);
  38. } while (val1 != val2);
  39. /* normalize to 1970..2106 time range */
  40. t = (u32)(val1 - TWENTY_YEARS);
  41. rtc_time64_to_tm(t, tm);
  42. return 0;
  43. }
  44. /**
  45. * aica_rtc_settimeofday - Set the AICA RTC to the current time
  46. * @dev: the RTC device (ignored)
  47. * @tm: pointer to new RTC time structure
  48. *
  49. * Adjusts the given @tv to the AICA Epoch and sets the RTC seconds counter.
  50. */
  51. static int aica_rtc_settimeofday(struct device *dev, struct rtc_time *tm)
  52. {
  53. unsigned long val1, val2;
  54. time64_t secs = rtc_tm_to_time64(tm);
  55. u32 adj = secs + TWENTY_YEARS;
  56. do {
  57. __raw_writel((adj & 0xffff0000) >> 16, AICA_RTC_SECS_H);
  58. __raw_writel((adj & 0xffff), AICA_RTC_SECS_L);
  59. val1 = ((__raw_readl(AICA_RTC_SECS_H) & 0xffff) << 16) |
  60. (__raw_readl(AICA_RTC_SECS_L) & 0xffff);
  61. val2 = ((__raw_readl(AICA_RTC_SECS_H) & 0xffff) << 16) |
  62. (__raw_readl(AICA_RTC_SECS_L) & 0xffff);
  63. } while (val1 != val2);
  64. return 0;
  65. }
  66. static const struct rtc_class_ops rtc_generic_ops = {
  67. .read_time = aica_rtc_gettimeofday,
  68. .set_time = aica_rtc_settimeofday,
  69. };
  70. static int __init aica_time_init(void)
  71. {
  72. struct platform_device *pdev;
  73. pdev = platform_device_register_data(NULL, "rtc-generic", -1,
  74. &rtc_generic_ops,
  75. sizeof(rtc_generic_ops));
  76. return PTR_ERR_OR_ZERO(pdev);
  77. }
  78. arch_initcall(aica_time_init);