rtc.h 5.2 KB

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