rtc-mc146818-lib.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. #include <linux/bcd.h>
  2. #include <linux/delay.h>
  3. #include <linux/export.h>
  4. #include <linux/mc146818rtc.h>
  5. #ifdef CONFIG_ACPI
  6. #include <linux/acpi.h>
  7. #endif
  8. /*
  9. * Returns true if a clock update is in progress
  10. */
  11. static inline unsigned char mc146818_is_updating(void)
  12. {
  13. unsigned char uip;
  14. unsigned long flags;
  15. spin_lock_irqsave(&rtc_lock, flags);
  16. uip = (CMOS_READ(RTC_FREQ_SELECT) & RTC_UIP);
  17. spin_unlock_irqrestore(&rtc_lock, flags);
  18. return uip;
  19. }
  20. unsigned int mc146818_get_time(struct rtc_time *time)
  21. {
  22. unsigned char ctrl;
  23. unsigned long flags;
  24. unsigned char century = 0;
  25. #ifdef CONFIG_MACH_DECSTATION
  26. unsigned int real_year;
  27. #endif
  28. /*
  29. * read RTC once any update in progress is done. The update
  30. * can take just over 2ms. We wait 20ms. There is no need to
  31. * to poll-wait (up to 1s - eeccch) for the falling edge of RTC_UIP.
  32. * If you need to know *exactly* when a second has started, enable
  33. * periodic update complete interrupts, (via ioctl) and then
  34. * immediately read /dev/rtc which will block until you get the IRQ.
  35. * Once the read clears, read the RTC time (again via ioctl). Easy.
  36. */
  37. if (mc146818_is_updating())
  38. mdelay(20);
  39. /*
  40. * Only the values that we read from the RTC are set. We leave
  41. * tm_wday, tm_yday and tm_isdst untouched. Even though the
  42. * RTC has RTC_DAY_OF_WEEK, we ignore it, as it is only updated
  43. * by the RTC when initially set to a non-zero value.
  44. */
  45. spin_lock_irqsave(&rtc_lock, flags);
  46. time->tm_sec = CMOS_READ(RTC_SECONDS);
  47. time->tm_min = CMOS_READ(RTC_MINUTES);
  48. time->tm_hour = CMOS_READ(RTC_HOURS);
  49. time->tm_mday = CMOS_READ(RTC_DAY_OF_MONTH);
  50. time->tm_mon = CMOS_READ(RTC_MONTH);
  51. time->tm_year = CMOS_READ(RTC_YEAR);
  52. #ifdef CONFIG_MACH_DECSTATION
  53. real_year = CMOS_READ(RTC_DEC_YEAR);
  54. #endif
  55. #ifdef CONFIG_ACPI
  56. if (acpi_gbl_FADT.header.revision >= FADT2_REVISION_ID &&
  57. acpi_gbl_FADT.century)
  58. century = CMOS_READ(acpi_gbl_FADT.century);
  59. #endif
  60. ctrl = CMOS_READ(RTC_CONTROL);
  61. spin_unlock_irqrestore(&rtc_lock, flags);
  62. if (!(ctrl & RTC_DM_BINARY) || RTC_ALWAYS_BCD)
  63. {
  64. time->tm_sec = bcd2bin(time->tm_sec);
  65. time->tm_min = bcd2bin(time->tm_min);
  66. time->tm_hour = bcd2bin(time->tm_hour);
  67. time->tm_mday = bcd2bin(time->tm_mday);
  68. time->tm_mon = bcd2bin(time->tm_mon);
  69. time->tm_year = bcd2bin(time->tm_year);
  70. century = bcd2bin(century);
  71. }
  72. #ifdef CONFIG_MACH_DECSTATION
  73. time->tm_year += real_year - 72;
  74. #endif
  75. if (century > 20)
  76. time->tm_year += (century - 19) * 100;
  77. /*
  78. * Account for differences between how the RTC uses the values
  79. * and how they are defined in a struct rtc_time;
  80. */
  81. if (time->tm_year <= 69)
  82. time->tm_year += 100;
  83. time->tm_mon--;
  84. return RTC_24H;
  85. }
  86. EXPORT_SYMBOL_GPL(mc146818_get_time);
  87. /* Set the current date and time in the real time clock. */
  88. int mc146818_set_time(struct rtc_time *time)
  89. {
  90. unsigned long flags;
  91. unsigned char mon, day, hrs, min, sec;
  92. unsigned char save_control, save_freq_select;
  93. unsigned int yrs;
  94. #ifdef CONFIG_MACH_DECSTATION
  95. unsigned int real_yrs, leap_yr;
  96. #endif
  97. unsigned char century = 0;
  98. yrs = time->tm_year;
  99. mon = time->tm_mon + 1; /* tm_mon starts at zero */
  100. day = time->tm_mday;
  101. hrs = time->tm_hour;
  102. min = time->tm_min;
  103. sec = time->tm_sec;
  104. if (yrs > 255) /* They are unsigned */
  105. return -EINVAL;
  106. spin_lock_irqsave(&rtc_lock, flags);
  107. #ifdef CONFIG_MACH_DECSTATION
  108. real_yrs = yrs;
  109. leap_yr = ((!((yrs + 1900) % 4) && ((yrs + 1900) % 100)) ||
  110. !((yrs + 1900) % 400));
  111. yrs = 72;
  112. /*
  113. * We want to keep the year set to 73 until March
  114. * for non-leap years, so that Feb, 29th is handled
  115. * correctly.
  116. */
  117. if (!leap_yr && mon < 3) {
  118. real_yrs--;
  119. yrs = 73;
  120. }
  121. #endif
  122. #ifdef CONFIG_ACPI
  123. if (acpi_gbl_FADT.header.revision >= FADT2_REVISION_ID &&
  124. acpi_gbl_FADT.century) {
  125. century = (yrs + 1900) / 100;
  126. yrs %= 100;
  127. }
  128. #endif
  129. /* These limits and adjustments are independent of
  130. * whether the chip is in binary mode or not.
  131. */
  132. if (yrs > 169) {
  133. spin_unlock_irqrestore(&rtc_lock, flags);
  134. return -EINVAL;
  135. }
  136. if (yrs >= 100)
  137. yrs -= 100;
  138. if (!(CMOS_READ(RTC_CONTROL) & RTC_DM_BINARY)
  139. || RTC_ALWAYS_BCD) {
  140. sec = bin2bcd(sec);
  141. min = bin2bcd(min);
  142. hrs = bin2bcd(hrs);
  143. day = bin2bcd(day);
  144. mon = bin2bcd(mon);
  145. yrs = bin2bcd(yrs);
  146. century = bin2bcd(century);
  147. }
  148. save_control = CMOS_READ(RTC_CONTROL);
  149. CMOS_WRITE((save_control|RTC_SET), RTC_CONTROL);
  150. save_freq_select = CMOS_READ(RTC_FREQ_SELECT);
  151. CMOS_WRITE((save_freq_select|RTC_DIV_RESET2), RTC_FREQ_SELECT);
  152. #ifdef CONFIG_MACH_DECSTATION
  153. CMOS_WRITE(real_yrs, RTC_DEC_YEAR);
  154. #endif
  155. CMOS_WRITE(yrs, RTC_YEAR);
  156. CMOS_WRITE(mon, RTC_MONTH);
  157. CMOS_WRITE(day, RTC_DAY_OF_MONTH);
  158. CMOS_WRITE(hrs, RTC_HOURS);
  159. CMOS_WRITE(min, RTC_MINUTES);
  160. CMOS_WRITE(sec, RTC_SECONDS);
  161. #ifdef CONFIG_ACPI
  162. if (acpi_gbl_FADT.header.revision >= FADT2_REVISION_ID &&
  163. acpi_gbl_FADT.century)
  164. CMOS_WRITE(century, acpi_gbl_FADT.century);
  165. #endif
  166. CMOS_WRITE(save_control, RTC_CONTROL);
  167. CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
  168. spin_unlock_irqrestore(&rtc_lock, flags);
  169. return 0;
  170. }
  171. EXPORT_SYMBOL_GPL(mc146818_set_time);