rtc.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /*
  2. * arch/sh/boards/dreamcast/rtc.c
  3. *
  4. * Dreamcast AICA RTC routines.
  5. *
  6. * Copyright (c) 2001, 2002 M. R. Brown <mrbrown@0xd6.org>
  7. * Copyright (c) 2002 Paul Mundt <lethal@chaoticdreams.org>
  8. *
  9. * Released under the terms of the GNU GPL v2.0.
  10. *
  11. */
  12. #include <linux/time.h>
  13. #include <asm/rtc.h>
  14. #include <asm/io.h>
  15. /* The AICA RTC has an Epoch of 1/1/1950, so we must subtract 20 years (in
  16. seconds) to get the standard Unix Epoch when getting the time, and add
  17. 20 years when setting the time. */
  18. #define TWENTY_YEARS ((20 * 365LU + 5) * 86400)
  19. /* The AICA RTC is represented by a 32-bit seconds counter stored in 2 16-bit
  20. registers.*/
  21. #define AICA_RTC_SECS_H 0xa0710000
  22. #define AICA_RTC_SECS_L 0xa0710004
  23. /**
  24. * aica_rtc_gettimeofday - Get the time from the AICA RTC
  25. * @ts: pointer to resulting timespec
  26. *
  27. * Grabs the current RTC seconds counter and adjusts it to the Unix Epoch.
  28. */
  29. static void aica_rtc_gettimeofday(struct timespec *ts)
  30. {
  31. unsigned long val1, val2;
  32. do {
  33. val1 = ((__raw_readl(AICA_RTC_SECS_H) & 0xffff) << 16) |
  34. (__raw_readl(AICA_RTC_SECS_L) & 0xffff);
  35. val2 = ((__raw_readl(AICA_RTC_SECS_H) & 0xffff) << 16) |
  36. (__raw_readl(AICA_RTC_SECS_L) & 0xffff);
  37. } while (val1 != val2);
  38. ts->tv_sec = val1 - TWENTY_YEARS;
  39. /* Can't get nanoseconds with just a seconds counter. */
  40. ts->tv_nsec = 0;
  41. }
  42. /**
  43. * aica_rtc_settimeofday - Set the AICA RTC to the current time
  44. * @secs: contains the time_t to set
  45. *
  46. * Adjusts the given @tv to the AICA Epoch and sets the RTC seconds counter.
  47. */
  48. static int aica_rtc_settimeofday(const time_t secs)
  49. {
  50. unsigned long val1, val2;
  51. unsigned long adj = secs + TWENTY_YEARS;
  52. do {
  53. __raw_writel((adj & 0xffff0000) >> 16, AICA_RTC_SECS_H);
  54. __raw_writel((adj & 0xffff), AICA_RTC_SECS_L);
  55. val1 = ((__raw_readl(AICA_RTC_SECS_H) & 0xffff) << 16) |
  56. (__raw_readl(AICA_RTC_SECS_L) & 0xffff);
  57. val2 = ((__raw_readl(AICA_RTC_SECS_H) & 0xffff) << 16) |
  58. (__raw_readl(AICA_RTC_SECS_L) & 0xffff);
  59. } while (val1 != val2);
  60. return 0;
  61. }
  62. void aica_time_init(void)
  63. {
  64. rtc_sh_get_time = aica_rtc_gettimeofday;
  65. rtc_sh_set_time = aica_rtc_settimeofday;
  66. }