rtc.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /*
  2. * include/asm-generic/rtc.h
  3. *
  4. * Author: Tom Rini <trini@mvista.com>
  5. *
  6. * Based on:
  7. * drivers/char/rtc.c
  8. *
  9. * Please read the COPYING file for all license details.
  10. */
  11. #ifndef __ASM_RTC_H__
  12. #define __ASM_RTC_H__
  13. #ifdef __KERNEL__
  14. #include <linux/mc146818rtc.h>
  15. #include <linux/rtc.h>
  16. #include <linux/bcd.h>
  17. #define RTC_PIE 0x40 /* periodic interrupt enable */
  18. #define RTC_AIE 0x20 /* alarm interrupt enable */
  19. #define RTC_UIE 0x10 /* update-finished interrupt enable */
  20. /* some dummy definitions */
  21. #define RTC_BATT_BAD 0x100 /* battery bad */
  22. #define RTC_SQWE 0x08 /* enable square-wave output */
  23. #define RTC_DM_BINARY 0x04 /* all time/date values are BCD if clear */
  24. #define RTC_24H 0x02 /* 24 hour mode - else hours bit 7 means pm */
  25. #define RTC_DST_EN 0x01 /* auto switch DST - works f. USA only */
  26. /*
  27. * Returns true if a clock update is in progress
  28. */
  29. static inline unsigned char rtc_is_updating(void)
  30. {
  31. unsigned char uip;
  32. unsigned long flags;
  33. spin_lock_irqsave(&rtc_lock, flags);
  34. uip = (CMOS_READ(RTC_FREQ_SELECT) & RTC_UIP);
  35. spin_unlock_irqrestore(&rtc_lock, flags);
  36. return uip;
  37. }
  38. static inline unsigned int get_rtc_time(struct rtc_time *time)
  39. {
  40. unsigned long uip_watchdog = jiffies;
  41. unsigned char ctrl;
  42. unsigned long flags;
  43. #ifdef CONFIG_MACH_DECSTATION
  44. unsigned int real_year;
  45. #endif
  46. /*
  47. * read RTC once any update in progress is done. The update
  48. * can take just over 2ms. We wait 10 to 20ms. There is no need to
  49. * to poll-wait (up to 1s - eeccch) for the falling edge of RTC_UIP.
  50. * If you need to know *exactly* when a second has started, enable
  51. * periodic update complete interrupts, (via ioctl) and then
  52. * immediately read /dev/rtc which will block until you get the IRQ.
  53. * Once the read clears, read the RTC time (again via ioctl). Easy.
  54. */
  55. if (rtc_is_updating() != 0)
  56. while (jiffies - uip_watchdog < 2*HZ/100) {
  57. barrier();
  58. cpu_relax();
  59. }
  60. /*
  61. * Only the values that we read from the RTC are set. We leave
  62. * tm_wday, tm_yday and tm_isdst untouched. Even though the
  63. * RTC has RTC_DAY_OF_WEEK, we ignore it, as it is only updated
  64. * by the RTC when initially set to a non-zero value.
  65. */
  66. spin_lock_irqsave(&rtc_lock, flags);
  67. time->tm_sec = CMOS_READ(RTC_SECONDS);
  68. time->tm_min = CMOS_READ(RTC_MINUTES);
  69. time->tm_hour = CMOS_READ(RTC_HOURS);
  70. time->tm_mday = CMOS_READ(RTC_DAY_OF_MONTH);
  71. time->tm_mon = CMOS_READ(RTC_MONTH);
  72. time->tm_year = CMOS_READ(RTC_YEAR);
  73. #ifdef CONFIG_MACH_DECSTATION
  74. real_year = CMOS_READ(RTC_DEC_YEAR);
  75. #endif
  76. ctrl = CMOS_READ(RTC_CONTROL);
  77. spin_unlock_irqrestore(&rtc_lock, flags);
  78. if (!(ctrl & RTC_DM_BINARY) || RTC_ALWAYS_BCD)
  79. {
  80. BCD_TO_BIN(time->tm_sec);
  81. BCD_TO_BIN(time->tm_min);
  82. BCD_TO_BIN(time->tm_hour);
  83. BCD_TO_BIN(time->tm_mday);
  84. BCD_TO_BIN(time->tm_mon);
  85. BCD_TO_BIN(time->tm_year);
  86. }
  87. #ifdef CONFIG_MACH_DECSTATION
  88. time->tm_year += real_year - 72;
  89. #endif
  90. /*
  91. * Account for differences between how the RTC uses the values
  92. * and how they are defined in a struct rtc_time;
  93. */
  94. if (time->tm_year <= 69)
  95. time->tm_year += 100;
  96. time->tm_mon--;
  97. return RTC_24H;
  98. }
  99. /* Set the current date and time in the real time clock. */
  100. static inline int set_rtc_time(struct rtc_time *time)
  101. {
  102. unsigned long flags;
  103. unsigned char mon, day, hrs, min, sec;
  104. unsigned char save_control, save_freq_select;
  105. unsigned int yrs;
  106. #ifdef CONFIG_MACH_DECSTATION
  107. unsigned int real_yrs, leap_yr;
  108. #endif
  109. yrs = time->tm_year;
  110. mon = time->tm_mon + 1; /* tm_mon starts at zero */
  111. day = time->tm_mday;
  112. hrs = time->tm_hour;
  113. min = time->tm_min;
  114. sec = time->tm_sec;
  115. if (yrs > 255) /* They are unsigned */
  116. return -EINVAL;
  117. spin_lock_irqsave(&rtc_lock, flags);
  118. #ifdef CONFIG_MACH_DECSTATION
  119. real_yrs = yrs;
  120. leap_yr = ((!((yrs + 1900) % 4) && ((yrs + 1900) % 100)) ||
  121. !((yrs + 1900) % 400));
  122. yrs = 72;
  123. /*
  124. * We want to keep the year set to 73 until March
  125. * for non-leap years, so that Feb, 29th is handled
  126. * correctly.
  127. */
  128. if (!leap_yr && mon < 3) {
  129. real_yrs--;
  130. yrs = 73;
  131. }
  132. #endif
  133. /* These limits and adjustments are independent of
  134. * whether the chip is in binary mode or not.
  135. */
  136. if (yrs > 169) {
  137. spin_unlock_irqrestore(&rtc_lock, flags);
  138. return -EINVAL;
  139. }
  140. if (yrs >= 100)
  141. yrs -= 100;
  142. if (!(CMOS_READ(RTC_CONTROL) & RTC_DM_BINARY)
  143. || RTC_ALWAYS_BCD) {
  144. BIN_TO_BCD(sec);
  145. BIN_TO_BCD(min);
  146. BIN_TO_BCD(hrs);
  147. BIN_TO_BCD(day);
  148. BIN_TO_BCD(mon);
  149. BIN_TO_BCD(yrs);
  150. }
  151. save_control = CMOS_READ(RTC_CONTROL);
  152. CMOS_WRITE((save_control|RTC_SET), RTC_CONTROL);
  153. save_freq_select = CMOS_READ(RTC_FREQ_SELECT);
  154. CMOS_WRITE((save_freq_select|RTC_DIV_RESET2), RTC_FREQ_SELECT);
  155. #ifdef CONFIG_MACH_DECSTATION
  156. CMOS_WRITE(real_yrs, RTC_DEC_YEAR);
  157. #endif
  158. CMOS_WRITE(yrs, RTC_YEAR);
  159. CMOS_WRITE(mon, RTC_MONTH);
  160. CMOS_WRITE(day, RTC_DAY_OF_MONTH);
  161. CMOS_WRITE(hrs, RTC_HOURS);
  162. CMOS_WRITE(min, RTC_MINUTES);
  163. CMOS_WRITE(sec, RTC_SECONDS);
  164. CMOS_WRITE(save_control, RTC_CONTROL);
  165. CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
  166. spin_unlock_irqrestore(&rtc_lock, flags);
  167. return 0;
  168. }
  169. static inline unsigned int get_rtc_ss(void)
  170. {
  171. struct rtc_time h;
  172. get_rtc_time(&h);
  173. return h.tm_sec;
  174. }
  175. static inline int get_rtc_pll(struct rtc_pll_info *pll)
  176. {
  177. return -EINVAL;
  178. }
  179. static inline int set_rtc_pll(struct rtc_pll_info *pll)
  180. {
  181. return -EINVAL;
  182. }
  183. #endif /* __KERNEL__ */
  184. #endif /* __ASM_RTC_H__ */