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