rtc.c 6.2 KB

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