rtc.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*
  2. * linux/arch/sh/boards/sh03/rtc.c -- CTP/PCI-SH03 on-chip RTC support
  3. *
  4. * Copyright (C) 2004 Saito.K & Jeanne(ksaito@interface.co.jp)
  5. *
  6. */
  7. #include <linux/init.h>
  8. #include <linux/kernel.h>
  9. #include <linux/sched.h>
  10. #include <linux/time.h>
  11. #include <linux/bcd.h>
  12. #include <asm/io.h>
  13. #include <linux/rtc.h>
  14. #include <linux/spinlock.h>
  15. #define RTC_BASE 0xb0000000
  16. #define RTC_SEC1 (RTC_BASE + 0)
  17. #define RTC_SEC10 (RTC_BASE + 1)
  18. #define RTC_MIN1 (RTC_BASE + 2)
  19. #define RTC_MIN10 (RTC_BASE + 3)
  20. #define RTC_HOU1 (RTC_BASE + 4)
  21. #define RTC_HOU10 (RTC_BASE + 5)
  22. #define RTC_WEE1 (RTC_BASE + 6)
  23. #define RTC_DAY1 (RTC_BASE + 7)
  24. #define RTC_DAY10 (RTC_BASE + 8)
  25. #define RTC_MON1 (RTC_BASE + 9)
  26. #define RTC_MON10 (RTC_BASE + 10)
  27. #define RTC_YEA1 (RTC_BASE + 11)
  28. #define RTC_YEA10 (RTC_BASE + 12)
  29. #define RTC_YEA100 (RTC_BASE + 13)
  30. #define RTC_YEA1000 (RTC_BASE + 14)
  31. #define RTC_CTL (RTC_BASE + 15)
  32. #define RTC_BUSY 1
  33. #define RTC_STOP 2
  34. extern void (*rtc_get_time)(struct timespec *);
  35. extern int (*rtc_set_time)(const time_t);
  36. extern spinlock_t rtc_lock;
  37. unsigned long get_cmos_time(void)
  38. {
  39. unsigned int year, mon, day, hour, min, sec;
  40. spin_lock(&rtc_lock);
  41. again:
  42. do {
  43. sec = (ctrl_inb(RTC_SEC1) & 0xf) + (ctrl_inb(RTC_SEC10) & 0x7) * 10;
  44. min = (ctrl_inb(RTC_MIN1) & 0xf) + (ctrl_inb(RTC_MIN10) & 0xf) * 10;
  45. hour = (ctrl_inb(RTC_HOU1) & 0xf) + (ctrl_inb(RTC_HOU10) & 0xf) * 10;
  46. day = (ctrl_inb(RTC_DAY1) & 0xf) + (ctrl_inb(RTC_DAY10) & 0xf) * 10;
  47. mon = (ctrl_inb(RTC_MON1) & 0xf) + (ctrl_inb(RTC_MON10) & 0xf) * 10;
  48. year = (ctrl_inb(RTC_YEA1) & 0xf) + (ctrl_inb(RTC_YEA10) & 0xf) * 10
  49. + (ctrl_inb(RTC_YEA100 ) & 0xf) * 100
  50. + (ctrl_inb(RTC_YEA1000) & 0xf) * 1000;
  51. } while (sec != (ctrl_inb(RTC_SEC1) & 0xf) + (ctrl_inb(RTC_SEC10) & 0x7) * 10);
  52. if (year == 0 || mon < 1 || mon > 12 || day > 31 || day < 1 ||
  53. hour > 23 || min > 59 || sec > 59) {
  54. printk(KERN_ERR
  55. "SH-03 RTC: invalid value, resetting to 1 Jan 2000\n");
  56. printk("year=%d, mon=%d, day=%d, hour=%d, min=%d, sec=%d\n",
  57. year, mon, day, hour, min, sec);
  58. ctrl_outb(0, RTC_SEC1); ctrl_outb(0, RTC_SEC10);
  59. ctrl_outb(0, RTC_MIN1); ctrl_outb(0, RTC_MIN10);
  60. ctrl_outb(0, RTC_HOU1); ctrl_outb(0, RTC_HOU10);
  61. ctrl_outb(6, RTC_WEE1);
  62. ctrl_outb(1, RTC_DAY1); ctrl_outb(0, RTC_DAY10);
  63. ctrl_outb(1, RTC_MON1); ctrl_outb(0, RTC_MON10);
  64. ctrl_outb(0, RTC_YEA1); ctrl_outb(0, RTC_YEA10);
  65. ctrl_outb(0, RTC_YEA100);
  66. ctrl_outb(2, RTC_YEA1000);
  67. ctrl_outb(0, RTC_CTL);
  68. goto again;
  69. }
  70. spin_unlock(&rtc_lock);
  71. return mktime(year, mon, day, hour, min, sec);
  72. }
  73. void sh03_rtc_gettimeofday(struct timespec *tv)
  74. {
  75. tv->tv_sec = get_cmos_time();
  76. tv->tv_nsec = 0;
  77. }
  78. static int set_rtc_mmss(unsigned long nowtime)
  79. {
  80. int retval = 0;
  81. int real_seconds, real_minutes, cmos_minutes;
  82. int i;
  83. /* gets recalled with irq locally disabled */
  84. spin_lock(&rtc_lock);
  85. for (i = 0 ; i < 1000000 ; i++) /* may take up to 1 second... */
  86. if (!(ctrl_inb(RTC_CTL) & RTC_BUSY))
  87. break;
  88. cmos_minutes = (ctrl_inb(RTC_MIN1) & 0xf) + (ctrl_inb(RTC_MIN10) & 0xf) * 10;
  89. real_seconds = nowtime % 60;
  90. real_minutes = nowtime / 60;
  91. if (((abs(real_minutes - cmos_minutes) + 15)/30) & 1)
  92. real_minutes += 30; /* correct for half hour time zone */
  93. real_minutes %= 60;
  94. if (abs(real_minutes - cmos_minutes) < 30) {
  95. ctrl_outb(real_seconds % 10, RTC_SEC1);
  96. ctrl_outb(real_seconds / 10, RTC_SEC10);
  97. ctrl_outb(real_minutes % 10, RTC_MIN1);
  98. ctrl_outb(real_minutes / 10, RTC_MIN10);
  99. } else {
  100. printk(KERN_WARNING
  101. "set_rtc_mmss: can't update from %d to %d\n",
  102. cmos_minutes, real_minutes);
  103. retval = -1;
  104. }
  105. spin_unlock(&rtc_lock);
  106. return retval;
  107. }
  108. int sh03_rtc_settimeofday(const time_t secs)
  109. {
  110. unsigned long nowtime = secs;
  111. return set_rtc_mmss(nowtime);
  112. }
  113. void sh03_time_init(void)
  114. {
  115. rtc_get_time = sh03_rtc_gettimeofday;
  116. rtc_set_time = sh03_rtc_settimeofday;
  117. }