rtc.h 5.3 KB

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