rtc.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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. #ifdef CONFIG_ACPI
  18. #include <linux/acpi.h>
  19. #endif
  20. #define RTC_PIE 0x40 /* periodic interrupt enable */
  21. #define RTC_AIE 0x20 /* alarm interrupt enable */
  22. #define RTC_UIE 0x10 /* update-finished interrupt enable */
  23. /* some dummy definitions */
  24. #define RTC_BATT_BAD 0x100 /* battery bad */
  25. #define RTC_SQWE 0x08 /* enable square-wave output */
  26. #define RTC_DM_BINARY 0x04 /* all time/date values are BCD if clear */
  27. #define RTC_24H 0x02 /* 24 hour mode - else hours bit 7 means pm */
  28. #define RTC_DST_EN 0x01 /* auto switch DST - works f. USA only */
  29. /*
  30. * Returns true if a clock update is in progress
  31. */
  32. static inline unsigned char rtc_is_updating(void)
  33. {
  34. unsigned char uip;
  35. unsigned long flags;
  36. spin_lock_irqsave(&rtc_lock, flags);
  37. uip = (CMOS_READ(RTC_FREQ_SELECT) & RTC_UIP);
  38. spin_unlock_irqrestore(&rtc_lock, flags);
  39. return uip;
  40. }
  41. static inline unsigned int __get_rtc_time(struct rtc_time *time)
  42. {
  43. unsigned char ctrl;
  44. unsigned long flags;
  45. unsigned char century = 0;
  46. #ifdef CONFIG_MACH_DECSTATION
  47. unsigned int real_year;
  48. #endif
  49. /*
  50. * read RTC once any update in progress is done. The update
  51. * can take just over 2ms. We wait 20ms. There is no need to
  52. * to poll-wait (up to 1s - eeccch) for the falling edge of RTC_UIP.
  53. * If you need to know *exactly* when a second has started, enable
  54. * periodic update complete interrupts, (via ioctl) and then
  55. * immediately read /dev/rtc which will block until you get the IRQ.
  56. * Once the read clears, read the RTC time (again via ioctl). Easy.
  57. */
  58. if (rtc_is_updating())
  59. mdelay(20);
  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. #ifdef CONFIG_ACPI
  77. if (acpi_gbl_FADT.header.revision >= FADT2_REVISION_ID &&
  78. acpi_gbl_FADT.century)
  79. century = CMOS_READ(acpi_gbl_FADT.century);
  80. #endif
  81. ctrl = CMOS_READ(RTC_CONTROL);
  82. spin_unlock_irqrestore(&rtc_lock, flags);
  83. if (!(ctrl & RTC_DM_BINARY) || RTC_ALWAYS_BCD)
  84. {
  85. time->tm_sec = bcd2bin(time->tm_sec);
  86. time->tm_min = bcd2bin(time->tm_min);
  87. time->tm_hour = bcd2bin(time->tm_hour);
  88. time->tm_mday = bcd2bin(time->tm_mday);
  89. time->tm_mon = bcd2bin(time->tm_mon);
  90. time->tm_year = bcd2bin(time->tm_year);
  91. century = bcd2bin(century);
  92. }
  93. #ifdef CONFIG_MACH_DECSTATION
  94. time->tm_year += real_year - 72;
  95. #endif
  96. if (century)
  97. time->tm_year += (century - 19) * 100;
  98. /*
  99. * Account for differences between how the RTC uses the values
  100. * and how they are defined in a struct rtc_time;
  101. */
  102. if (time->tm_year <= 69)
  103. time->tm_year += 100;
  104. time->tm_mon--;
  105. return RTC_24H;
  106. }
  107. #ifndef get_rtc_time
  108. #define get_rtc_time __get_rtc_time
  109. #endif
  110. /* Set the current date and time in the real time clock. */
  111. static inline int __set_rtc_time(struct rtc_time *time)
  112. {
  113. unsigned long flags;
  114. unsigned char mon, day, hrs, min, sec;
  115. unsigned char save_control, save_freq_select;
  116. unsigned int yrs;
  117. #ifdef CONFIG_MACH_DECSTATION
  118. unsigned int real_yrs, leap_yr;
  119. #endif
  120. unsigned char century = 0;
  121. yrs = time->tm_year;
  122. mon = time->tm_mon + 1; /* tm_mon starts at zero */
  123. day = time->tm_mday;
  124. hrs = time->tm_hour;
  125. min = time->tm_min;
  126. sec = time->tm_sec;
  127. if (yrs > 255) /* They are unsigned */
  128. return -EINVAL;
  129. spin_lock_irqsave(&rtc_lock, flags);
  130. #ifdef CONFIG_MACH_DECSTATION
  131. real_yrs = yrs;
  132. leap_yr = ((!((yrs + 1900) % 4) && ((yrs + 1900) % 100)) ||
  133. !((yrs + 1900) % 400));
  134. yrs = 72;
  135. /*
  136. * We want to keep the year set to 73 until March
  137. * for non-leap years, so that Feb, 29th is handled
  138. * correctly.
  139. */
  140. if (!leap_yr && mon < 3) {
  141. real_yrs--;
  142. yrs = 73;
  143. }
  144. #endif
  145. #ifdef CONFIG_ACPI
  146. if (acpi_gbl_FADT.header.revision >= FADT2_REVISION_ID &&
  147. acpi_gbl_FADT.century) {
  148. century = (yrs + 1900) / 100;
  149. yrs %= 100;
  150. }
  151. #endif
  152. /* These limits and adjustments are independent of
  153. * whether the chip is in binary mode or not.
  154. */
  155. if (yrs > 169) {
  156. spin_unlock_irqrestore(&rtc_lock, flags);
  157. return -EINVAL;
  158. }
  159. if (yrs >= 100)
  160. yrs -= 100;
  161. if (!(CMOS_READ(RTC_CONTROL) & RTC_DM_BINARY)
  162. || RTC_ALWAYS_BCD) {
  163. sec = bin2bcd(sec);
  164. min = bin2bcd(min);
  165. hrs = bin2bcd(hrs);
  166. day = bin2bcd(day);
  167. mon = bin2bcd(mon);
  168. yrs = bin2bcd(yrs);
  169. century = bin2bcd(century);
  170. }
  171. save_control = CMOS_READ(RTC_CONTROL);
  172. CMOS_WRITE((save_control|RTC_SET), RTC_CONTROL);
  173. save_freq_select = CMOS_READ(RTC_FREQ_SELECT);
  174. CMOS_WRITE((save_freq_select|RTC_DIV_RESET2), RTC_FREQ_SELECT);
  175. #ifdef CONFIG_MACH_DECSTATION
  176. CMOS_WRITE(real_yrs, RTC_DEC_YEAR);
  177. #endif
  178. CMOS_WRITE(yrs, RTC_YEAR);
  179. CMOS_WRITE(mon, RTC_MONTH);
  180. CMOS_WRITE(day, RTC_DAY_OF_MONTH);
  181. CMOS_WRITE(hrs, RTC_HOURS);
  182. CMOS_WRITE(min, RTC_MINUTES);
  183. CMOS_WRITE(sec, RTC_SECONDS);
  184. #ifdef CONFIG_ACPI
  185. if (acpi_gbl_FADT.header.revision >= FADT2_REVISION_ID &&
  186. acpi_gbl_FADT.century)
  187. CMOS_WRITE(century, acpi_gbl_FADT.century);
  188. #endif
  189. CMOS_WRITE(save_control, RTC_CONTROL);
  190. CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
  191. spin_unlock_irqrestore(&rtc_lock, flags);
  192. return 0;
  193. }
  194. #ifndef set_rtc_time
  195. #define set_rtc_time __set_rtc_time
  196. #endif
  197. static inline unsigned int get_rtc_ss(void)
  198. {
  199. struct rtc_time h;
  200. get_rtc_time(&h);
  201. return h.tm_sec;
  202. }
  203. static inline int get_rtc_pll(struct rtc_pll_info *pll)
  204. {
  205. return -EINVAL;
  206. }
  207. static inline int set_rtc_pll(struct rtc_pll_info *pll)
  208. {
  209. return -EINVAL;
  210. }
  211. #endif /* __ASM_RTC_H__ */