rtc.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. /* Set the current date and time in the real time clock. */
  96. static inline int set_rtc_time(struct rtc_time *time)
  97. {
  98. unsigned long flags;
  99. unsigned char mon, day, hrs, min, sec;
  100. unsigned char save_control, save_freq_select;
  101. unsigned int yrs;
  102. #ifdef CONFIG_MACH_DECSTATION
  103. unsigned int real_yrs, leap_yr;
  104. #endif
  105. yrs = time->tm_year;
  106. mon = time->tm_mon + 1; /* tm_mon starts at zero */
  107. day = time->tm_mday;
  108. hrs = time->tm_hour;
  109. min = time->tm_min;
  110. sec = time->tm_sec;
  111. if (yrs > 255) /* They are unsigned */
  112. return -EINVAL;
  113. spin_lock_irqsave(&rtc_lock, flags);
  114. #ifdef CONFIG_MACH_DECSTATION
  115. real_yrs = yrs;
  116. leap_yr = ((!((yrs + 1900) % 4) && ((yrs + 1900) % 100)) ||
  117. !((yrs + 1900) % 400));
  118. yrs = 72;
  119. /*
  120. * We want to keep the year set to 73 until March
  121. * for non-leap years, so that Feb, 29th is handled
  122. * correctly.
  123. */
  124. if (!leap_yr && mon < 3) {
  125. real_yrs--;
  126. yrs = 73;
  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. }
  147. save_control = CMOS_READ(RTC_CONTROL);
  148. CMOS_WRITE((save_control|RTC_SET), RTC_CONTROL);
  149. save_freq_select = CMOS_READ(RTC_FREQ_SELECT);
  150. CMOS_WRITE((save_freq_select|RTC_DIV_RESET2), RTC_FREQ_SELECT);
  151. #ifdef CONFIG_MACH_DECSTATION
  152. CMOS_WRITE(real_yrs, RTC_DEC_YEAR);
  153. #endif
  154. CMOS_WRITE(yrs, RTC_YEAR);
  155. CMOS_WRITE(mon, RTC_MONTH);
  156. CMOS_WRITE(day, RTC_DAY_OF_MONTH);
  157. CMOS_WRITE(hrs, RTC_HOURS);
  158. CMOS_WRITE(min, RTC_MINUTES);
  159. CMOS_WRITE(sec, RTC_SECONDS);
  160. CMOS_WRITE(save_control, RTC_CONTROL);
  161. CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
  162. spin_unlock_irqrestore(&rtc_lock, flags);
  163. return 0;
  164. }
  165. static inline unsigned int get_rtc_ss(void)
  166. {
  167. struct rtc_time h;
  168. get_rtc_time(&h);
  169. return h.tm_sec;
  170. }
  171. static inline int get_rtc_pll(struct rtc_pll_info *pll)
  172. {
  173. return -EINVAL;
  174. }
  175. static inline int set_rtc_pll(struct rtc_pll_info *pll)
  176. {
  177. return -EINVAL;
  178. }
  179. #endif /* __ASM_RTC_H__ */