time.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*
  2. * linux/include/asm-arm/arch-ebsa285/time.h
  3. *
  4. * Copyright (C) 1998 Russell King.
  5. * Copyright (C) 1998 Phil Blundell
  6. *
  7. * CATS has a real-time clock, though the evaluation board doesn't.
  8. *
  9. * Changelog:
  10. * 21-Mar-1998 RMK Created
  11. * 27-Aug-1998 PJB CATS support
  12. * 28-Dec-1998 APH Made leds optional
  13. * 20-Jan-1999 RMK Started merge of EBSA285, CATS and NetWinder
  14. * 16-Mar-1999 RMK More support for EBSA285-like machines with RTCs in
  15. */
  16. #define RTC_PORT(x) (rtc_base+(x))
  17. #define RTC_ALWAYS_BCD 0
  18. #include <linux/timex.h>
  19. #include <linux/init.h>
  20. #include <linux/sched.h>
  21. #include <linux/mc146818rtc.h>
  22. #include <linux/bcd.h>
  23. #include <asm/hardware.h>
  24. #include <asm/io.h>
  25. #include <asm/mach/time.h>
  26. #include "common.h"
  27. static int rtc_base;
  28. static unsigned long __init get_isa_cmos_time(void)
  29. {
  30. unsigned int year, mon, day, hour, min, sec;
  31. // check to see if the RTC makes sense.....
  32. if ((CMOS_READ(RTC_VALID) & RTC_VRT) == 0)
  33. return mktime(1970, 1, 1, 0, 0, 0);
  34. do {
  35. sec = CMOS_READ(RTC_SECONDS);
  36. min = CMOS_READ(RTC_MINUTES);
  37. hour = CMOS_READ(RTC_HOURS);
  38. day = CMOS_READ(RTC_DAY_OF_MONTH);
  39. mon = CMOS_READ(RTC_MONTH);
  40. year = CMOS_READ(RTC_YEAR);
  41. } while (sec != CMOS_READ(RTC_SECONDS));
  42. if (!(CMOS_READ(RTC_CONTROL) & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
  43. BCD_TO_BIN(sec);
  44. BCD_TO_BIN(min);
  45. BCD_TO_BIN(hour);
  46. BCD_TO_BIN(day);
  47. BCD_TO_BIN(mon);
  48. BCD_TO_BIN(year);
  49. }
  50. if ((year += 1900) < 1970)
  51. year += 100;
  52. return mktime(year, mon, day, hour, min, sec);
  53. }
  54. static int set_isa_cmos_time(void)
  55. {
  56. int retval = 0;
  57. int real_seconds, real_minutes, cmos_minutes;
  58. unsigned char save_control, save_freq_select;
  59. unsigned long nowtime = xtime.tv_sec;
  60. save_control = CMOS_READ(RTC_CONTROL); /* tell the clock it's being set */
  61. CMOS_WRITE((save_control|RTC_SET), RTC_CONTROL);
  62. save_freq_select = CMOS_READ(RTC_FREQ_SELECT); /* stop and reset prescaler */
  63. CMOS_WRITE((save_freq_select|RTC_DIV_RESET2), RTC_FREQ_SELECT);
  64. cmos_minutes = CMOS_READ(RTC_MINUTES);
  65. if (!(save_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD)
  66. BCD_TO_BIN(cmos_minutes);
  67. /*
  68. * since we're only adjusting minutes and seconds,
  69. * don't interfere with hour overflow. This avoids
  70. * messing with unknown time zones but requires your
  71. * RTC not to be off by more than 15 minutes
  72. */
  73. real_seconds = nowtime % 60;
  74. real_minutes = nowtime / 60;
  75. if (((abs(real_minutes - cmos_minutes) + 15)/30) & 1)
  76. real_minutes += 30; /* correct for half hour time zone */
  77. real_minutes %= 60;
  78. if (abs(real_minutes - cmos_minutes) < 30) {
  79. if (!(save_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
  80. BIN_TO_BCD(real_seconds);
  81. BIN_TO_BCD(real_minutes);
  82. }
  83. CMOS_WRITE(real_seconds,RTC_SECONDS);
  84. CMOS_WRITE(real_minutes,RTC_MINUTES);
  85. } else
  86. retval = -1;
  87. /* The following flags have to be released exactly in this order,
  88. * otherwise the DS12887 (popular MC146818A clone with integrated
  89. * battery and quartz) will not reset the oscillator and will not
  90. * update precisely 500 ms later. You won't find this mentioned in
  91. * the Dallas Semiconductor data sheets, but who believes data
  92. * sheets anyway ... -- Markus Kuhn
  93. */
  94. CMOS_WRITE(save_control, RTC_CONTROL);
  95. CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
  96. return retval;
  97. }
  98. void __init isa_rtc_init(void)
  99. {
  100. if (machine_is_co285() ||
  101. machine_is_personal_server())
  102. /*
  103. * Add-in 21285s shouldn't access the RTC
  104. */
  105. rtc_base = 0;
  106. else
  107. rtc_base = 0x70;
  108. if (rtc_base) {
  109. int reg_d, reg_b;
  110. /*
  111. * Probe for the RTC.
  112. */
  113. reg_d = CMOS_READ(RTC_REG_D);
  114. /*
  115. * make sure the divider is set
  116. */
  117. CMOS_WRITE(RTC_REF_CLCK_32KHZ, RTC_REG_A);
  118. /*
  119. * Set control reg B
  120. * (24 hour mode, update enabled)
  121. */
  122. reg_b = CMOS_READ(RTC_REG_B) & 0x7f;
  123. reg_b |= 2;
  124. CMOS_WRITE(reg_b, RTC_REG_B);
  125. if ((CMOS_READ(RTC_REG_A) & 0x7f) == RTC_REF_CLCK_32KHZ &&
  126. CMOS_READ(RTC_REG_B) == reg_b) {
  127. struct timespec tv;
  128. /*
  129. * We have a RTC. Check the battery
  130. */
  131. if ((reg_d & 0x80) == 0)
  132. printk(KERN_WARNING "RTC: *** warning: CMOS battery bad\n");
  133. tv.tv_nsec = 0;
  134. tv.tv_sec = get_isa_cmos_time();
  135. do_settimeofday(&tv);
  136. set_rtc = set_isa_cmos_time;
  137. } else
  138. rtc_base = 0;
  139. }
  140. }