time.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. int i;
  32. // check to see if the RTC makes sense.....
  33. if ((CMOS_READ(RTC_VALID) & RTC_VRT) == 0)
  34. return mktime(1970, 1, 1, 0, 0, 0);
  35. /* The Linux interpretation of the CMOS clock register contents:
  36. * When the Update-In-Progress (UIP) flag goes from 1 to 0, the
  37. * RTC registers show the second which has precisely just started.
  38. * Let's hope other operating systems interpret the RTC the same way.
  39. */
  40. /* read RTC exactly on falling edge of update flag */
  41. for (i = 0 ; i < 1000000 ; i++) /* may take up to 1 second... */
  42. if (CMOS_READ(RTC_FREQ_SELECT) & RTC_UIP)
  43. break;
  44. for (i = 0 ; i < 1000000 ; i++) /* must try at least 2.228 ms */
  45. if (!(CMOS_READ(RTC_FREQ_SELECT) & RTC_UIP))
  46. break;
  47. do { /* Isn't this overkill ? UIP above should guarantee consistency */
  48. sec = CMOS_READ(RTC_SECONDS);
  49. min = CMOS_READ(RTC_MINUTES);
  50. hour = CMOS_READ(RTC_HOURS);
  51. day = CMOS_READ(RTC_DAY_OF_MONTH);
  52. mon = CMOS_READ(RTC_MONTH);
  53. year = CMOS_READ(RTC_YEAR);
  54. } while (sec != CMOS_READ(RTC_SECONDS));
  55. if (!(CMOS_READ(RTC_CONTROL) & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
  56. BCD_TO_BIN(sec);
  57. BCD_TO_BIN(min);
  58. BCD_TO_BIN(hour);
  59. BCD_TO_BIN(day);
  60. BCD_TO_BIN(mon);
  61. BCD_TO_BIN(year);
  62. }
  63. if ((year += 1900) < 1970)
  64. year += 100;
  65. return mktime(year, mon, day, hour, min, sec);
  66. }
  67. static int set_isa_cmos_time(void)
  68. {
  69. int retval = 0;
  70. int real_seconds, real_minutes, cmos_minutes;
  71. unsigned char save_control, save_freq_select;
  72. unsigned long nowtime = xtime.tv_sec;
  73. save_control = CMOS_READ(RTC_CONTROL); /* tell the clock it's being set */
  74. CMOS_WRITE((save_control|RTC_SET), RTC_CONTROL);
  75. save_freq_select = CMOS_READ(RTC_FREQ_SELECT); /* stop and reset prescaler */
  76. CMOS_WRITE((save_freq_select|RTC_DIV_RESET2), RTC_FREQ_SELECT);
  77. cmos_minutes = CMOS_READ(RTC_MINUTES);
  78. if (!(save_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD)
  79. BCD_TO_BIN(cmos_minutes);
  80. /*
  81. * since we're only adjusting minutes and seconds,
  82. * don't interfere with hour overflow. This avoids
  83. * messing with unknown time zones but requires your
  84. * RTC not to be off by more than 15 minutes
  85. */
  86. real_seconds = nowtime % 60;
  87. real_minutes = nowtime / 60;
  88. if (((abs(real_minutes - cmos_minutes) + 15)/30) & 1)
  89. real_minutes += 30; /* correct for half hour time zone */
  90. real_minutes %= 60;
  91. if (abs(real_minutes - cmos_minutes) < 30) {
  92. if (!(save_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
  93. BIN_TO_BCD(real_seconds);
  94. BIN_TO_BCD(real_minutes);
  95. }
  96. CMOS_WRITE(real_seconds,RTC_SECONDS);
  97. CMOS_WRITE(real_minutes,RTC_MINUTES);
  98. } else
  99. retval = -1;
  100. /* The following flags have to be released exactly in this order,
  101. * otherwise the DS12887 (popular MC146818A clone with integrated
  102. * battery and quartz) will not reset the oscillator and will not
  103. * update precisely 500 ms later. You won't find this mentioned in
  104. * the Dallas Semiconductor data sheets, but who believes data
  105. * sheets anyway ... -- Markus Kuhn
  106. */
  107. CMOS_WRITE(save_control, RTC_CONTROL);
  108. CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
  109. return retval;
  110. }
  111. void __init isa_rtc_init(void)
  112. {
  113. if (machine_is_co285() ||
  114. machine_is_personal_server())
  115. /*
  116. * Add-in 21285s shouldn't access the RTC
  117. */
  118. rtc_base = 0;
  119. else
  120. rtc_base = 0x70;
  121. if (rtc_base) {
  122. int reg_d, reg_b;
  123. /*
  124. * Probe for the RTC.
  125. */
  126. reg_d = CMOS_READ(RTC_REG_D);
  127. /*
  128. * make sure the divider is set
  129. */
  130. CMOS_WRITE(RTC_REF_CLCK_32KHZ, RTC_REG_A);
  131. /*
  132. * Set control reg B
  133. * (24 hour mode, update enabled)
  134. */
  135. reg_b = CMOS_READ(RTC_REG_B) & 0x7f;
  136. reg_b |= 2;
  137. CMOS_WRITE(reg_b, RTC_REG_B);
  138. if ((CMOS_READ(RTC_REG_A) & 0x7f) == RTC_REF_CLCK_32KHZ &&
  139. CMOS_READ(RTC_REG_B) == reg_b) {
  140. struct timespec tv;
  141. /*
  142. * We have a RTC. Check the battery
  143. */
  144. if ((reg_d & 0x80) == 0)
  145. printk(KERN_WARNING "RTC: *** warning: CMOS battery bad\n");
  146. tv.tv_nsec = 0;
  147. tv.tv_sec = get_isa_cmos_time();
  148. do_settimeofday(&tv);
  149. set_rtc = set_isa_cmos_time;
  150. } else
  151. rtc_base = 0;
  152. }
  153. }