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