rtc.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /*
  2. * RTC related functions
  3. */
  4. #include <linux/platform_device.h>
  5. #include <linux/mc146818rtc.h>
  6. #include <linux/acpi.h>
  7. #include <linux/bcd.h>
  8. #include <linux/pnp.h>
  9. #include <asm/vsyscall.h>
  10. #include <asm/x86_init.h>
  11. #include <asm/time.h>
  12. #ifdef CONFIG_X86_32
  13. /*
  14. * This is a special lock that is owned by the CPU and holds the index
  15. * register we are working with. It is required for NMI access to the
  16. * CMOS/RTC registers. See include/asm-i386/mc146818rtc.h for details.
  17. */
  18. volatile unsigned long cmos_lock;
  19. EXPORT_SYMBOL(cmos_lock);
  20. #endif /* CONFIG_X86_32 */
  21. /* For two digit years assume time is always after that */
  22. #define CMOS_YEARS_OFFS 2000
  23. DEFINE_SPINLOCK(rtc_lock);
  24. EXPORT_SYMBOL(rtc_lock);
  25. /*
  26. * In order to set the CMOS clock precisely, set_rtc_mmss has to be
  27. * called 500 ms after the second nowtime has started, because when
  28. * nowtime is written into the registers of the CMOS clock, it will
  29. * jump to the next second precisely 500 ms later. Check the Motorola
  30. * MC146818A or Dallas DS12887 data sheet for details.
  31. *
  32. * BUG: This routine does not handle hour overflow properly; it just
  33. * sets the minutes. Usually you'll only notice that after reboot!
  34. */
  35. int mach_set_rtc_mmss(unsigned long nowtime)
  36. {
  37. int real_seconds, real_minutes, cmos_minutes;
  38. unsigned char save_control, save_freq_select;
  39. int retval = 0;
  40. /* tell the clock it's being set */
  41. save_control = CMOS_READ(RTC_CONTROL);
  42. CMOS_WRITE((save_control|RTC_SET), RTC_CONTROL);
  43. /* stop and reset prescaler */
  44. save_freq_select = CMOS_READ(RTC_FREQ_SELECT);
  45. CMOS_WRITE((save_freq_select|RTC_DIV_RESET2), RTC_FREQ_SELECT);
  46. cmos_minutes = CMOS_READ(RTC_MINUTES);
  47. if (!(save_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD)
  48. cmos_minutes = bcd2bin(cmos_minutes);
  49. /*
  50. * since we're only adjusting minutes and seconds,
  51. * don't interfere with hour overflow. This avoids
  52. * messing with unknown time zones but requires your
  53. * RTC not to be off by more than 15 minutes
  54. */
  55. real_seconds = nowtime % 60;
  56. real_minutes = nowtime / 60;
  57. /* correct for half hour time zone */
  58. if (((abs(real_minutes - cmos_minutes) + 15)/30) & 1)
  59. real_minutes += 30;
  60. real_minutes %= 60;
  61. if (abs(real_minutes - cmos_minutes) < 30) {
  62. if (!(save_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
  63. real_seconds = bin2bcd(real_seconds);
  64. real_minutes = bin2bcd(real_minutes);
  65. }
  66. CMOS_WRITE(real_seconds, RTC_SECONDS);
  67. CMOS_WRITE(real_minutes, RTC_MINUTES);
  68. } else {
  69. printk(KERN_WARNING
  70. "set_rtc_mmss: can't update from %d to %d\n",
  71. cmos_minutes, real_minutes);
  72. retval = -1;
  73. }
  74. /* The following flags have to be released exactly in this order,
  75. * otherwise the DS12887 (popular MC146818A clone with integrated
  76. * battery and quartz) will not reset the oscillator and will not
  77. * update precisely 500 ms later. You won't find this mentioned in
  78. * the Dallas Semiconductor data sheets, but who believes data
  79. * sheets anyway ... -- Markus Kuhn
  80. */
  81. CMOS_WRITE(save_control, RTC_CONTROL);
  82. CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
  83. return retval;
  84. }
  85. unsigned long mach_get_cmos_time(void)
  86. {
  87. unsigned int status, year, mon, day, hour, min, sec, century = 0;
  88. /*
  89. * If UIP is clear, then we have >= 244 microseconds before
  90. * RTC registers will be updated. Spec sheet says that this
  91. * is the reliable way to read RTC - registers. If UIP is set
  92. * then the register access might be invalid.
  93. */
  94. while ((CMOS_READ(RTC_FREQ_SELECT) & RTC_UIP))
  95. cpu_relax();
  96. sec = CMOS_READ(RTC_SECONDS);
  97. min = CMOS_READ(RTC_MINUTES);
  98. hour = CMOS_READ(RTC_HOURS);
  99. day = CMOS_READ(RTC_DAY_OF_MONTH);
  100. mon = CMOS_READ(RTC_MONTH);
  101. year = CMOS_READ(RTC_YEAR);
  102. #ifdef CONFIG_ACPI
  103. if (acpi_gbl_FADT.header.revision >= FADT2_REVISION_ID &&
  104. acpi_gbl_FADT.century)
  105. century = CMOS_READ(acpi_gbl_FADT.century);
  106. #endif
  107. status = CMOS_READ(RTC_CONTROL);
  108. WARN_ON_ONCE(RTC_ALWAYS_BCD && (status & RTC_DM_BINARY));
  109. if (RTC_ALWAYS_BCD || !(status & RTC_DM_BINARY)) {
  110. sec = bcd2bin(sec);
  111. min = bcd2bin(min);
  112. hour = bcd2bin(hour);
  113. day = bcd2bin(day);
  114. mon = bcd2bin(mon);
  115. year = bcd2bin(year);
  116. }
  117. if (century) {
  118. century = bcd2bin(century);
  119. year += century * 100;
  120. printk(KERN_INFO "Extended CMOS year: %d\n", century * 100);
  121. } else
  122. year += CMOS_YEARS_OFFS;
  123. return mktime(year, mon, day, hour, min, sec);
  124. }
  125. /* Routines for accessing the CMOS RAM/RTC. */
  126. unsigned char rtc_cmos_read(unsigned char addr)
  127. {
  128. unsigned char val;
  129. lock_cmos_prefix(addr);
  130. outb(addr, RTC_PORT(0));
  131. val = inb(RTC_PORT(1));
  132. lock_cmos_suffix(addr);
  133. return val;
  134. }
  135. EXPORT_SYMBOL(rtc_cmos_read);
  136. void rtc_cmos_write(unsigned char val, unsigned char addr)
  137. {
  138. lock_cmos_prefix(addr);
  139. outb(addr, RTC_PORT(0));
  140. outb(val, RTC_PORT(1));
  141. lock_cmos_suffix(addr);
  142. }
  143. EXPORT_SYMBOL(rtc_cmos_write);
  144. int update_persistent_clock(struct timespec now)
  145. {
  146. unsigned long flags;
  147. int retval;
  148. spin_lock_irqsave(&rtc_lock, flags);
  149. retval = x86_platform.set_wallclock(now.tv_sec);
  150. spin_unlock_irqrestore(&rtc_lock, flags);
  151. return retval;
  152. }
  153. /* not static: needed by APM */
  154. void read_persistent_clock(struct timespec *ts)
  155. {
  156. unsigned long retval, flags;
  157. spin_lock_irqsave(&rtc_lock, flags);
  158. retval = x86_platform.get_wallclock();
  159. spin_unlock_irqrestore(&rtc_lock, flags);
  160. ts->tv_sec = retval;
  161. ts->tv_nsec = 0;
  162. }
  163. unsigned long long native_read_tsc(void)
  164. {
  165. return __native_read_tsc();
  166. }
  167. EXPORT_SYMBOL(native_read_tsc);
  168. static struct resource rtc_resources[] = {
  169. [0] = {
  170. .start = RTC_PORT(0),
  171. .end = RTC_PORT(1),
  172. .flags = IORESOURCE_IO,
  173. },
  174. [1] = {
  175. .start = RTC_IRQ,
  176. .end = RTC_IRQ,
  177. .flags = IORESOURCE_IRQ,
  178. }
  179. };
  180. static struct platform_device rtc_device = {
  181. .name = "rtc_cmos",
  182. .id = -1,
  183. .resource = rtc_resources,
  184. .num_resources = ARRAY_SIZE(rtc_resources),
  185. };
  186. static __init int add_rtc_cmos(void)
  187. {
  188. #ifdef CONFIG_PNP
  189. static const char *ids[] __initconst =
  190. { "PNP0b00", "PNP0b01", "PNP0b02", };
  191. struct pnp_dev *dev;
  192. struct pnp_id *id;
  193. int i;
  194. pnp_for_each_dev(dev) {
  195. for (id = dev->id; id; id = id->next) {
  196. for (i = 0; i < ARRAY_SIZE(ids); i++) {
  197. if (compare_pnp_id(id, ids[i]) != 0)
  198. return 0;
  199. }
  200. }
  201. }
  202. #endif
  203. platform_device_register(&rtc_device);
  204. dev_info(&rtc_device.dev,
  205. "registered platform RTC device (no PNP device found)\n");
  206. return 0;
  207. }
  208. device_initcall(add_rtc_cmos);