rtc.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /* arch/sh/kernel/rtc-aica.c
  2. *
  3. * Dreamcast AICA RTC routines.
  4. *
  5. * Copyright (c) 2001, 2002 M. R. Brown <mrbrown@0xd6.org>
  6. * Copyright (c) 2002 Paul Mundt <lethal@chaoticdreams.org>
  7. *
  8. * Released under the terms of the GNU GPL v2.0.
  9. *
  10. */
  11. #include <linux/time.h>
  12. #include <asm/io.h>
  13. extern void (*rtc_get_time)(struct timespec *);
  14. extern int (*rtc_set_time)(const time_t);
  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 20
  17. 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. void aica_rtc_gettimeofday(struct timespec *ts) {
  30. unsigned long val1, val2;
  31. do {
  32. val1 = ((ctrl_inl(AICA_RTC_SECS_H) & 0xffff) << 16) |
  33. (ctrl_inl(AICA_RTC_SECS_L) & 0xffff);
  34. val2 = ((ctrl_inl(AICA_RTC_SECS_H) & 0xffff) << 16) |
  35. (ctrl_inl(AICA_RTC_SECS_L) & 0xffff);
  36. } while (val1 != val2);
  37. ts->tv_sec = val1 - TWENTY_YEARS;
  38. /* Can't get nanoseconds with just a seconds counter. */
  39. ts->tv_nsec = 0;
  40. }
  41. /**
  42. * aica_rtc_settimeofday - Set the AICA RTC to the current time
  43. * @secs: contains the time_t to set
  44. *
  45. * Adjusts the given @tv to the AICA Epoch and sets the RTC seconds counter.
  46. */
  47. int aica_rtc_settimeofday(const time_t secs) {
  48. unsigned long val1, val2;
  49. unsigned long adj = secs + TWENTY_YEARS;
  50. do {
  51. ctrl_outl((adj & 0xffff0000) >> 16, AICA_RTC_SECS_H);
  52. ctrl_outl((adj & 0xffff), AICA_RTC_SECS_L);
  53. val1 = ((ctrl_inl(AICA_RTC_SECS_H) & 0xffff) << 16) |
  54. (ctrl_inl(AICA_RTC_SECS_L) & 0xffff);
  55. val2 = ((ctrl_inl(AICA_RTC_SECS_H) & 0xffff) << 16) |
  56. (ctrl_inl(AICA_RTC_SECS_L) & 0xffff);
  57. } while (val1 != val2);
  58. return 0;
  59. }
  60. void aica_time_init(void)
  61. {
  62. rtc_get_time = aica_rtc_gettimeofday;
  63. rtc_set_time = aica_rtc_settimeofday;
  64. }