rtc.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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. unsigned long flags;
  41. int retval = 0;
  42. spin_lock_irqsave(&rtc_lock, flags);
  43. /* tell the clock it's being set */
  44. save_control = CMOS_READ(RTC_CONTROL);
  45. CMOS_WRITE((save_control|RTC_SET), RTC_CONTROL);
  46. /* stop and reset prescaler */
  47. save_freq_select = CMOS_READ(RTC_FREQ_SELECT);
  48. CMOS_WRITE((save_freq_select|RTC_DIV_RESET2), RTC_FREQ_SELECT);
  49. cmos_minutes = CMOS_READ(RTC_MINUTES);
  50. if (!(save_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD)
  51. cmos_minutes = bcd2bin(cmos_minutes);
  52. /*
  53. * since we're only adjusting minutes and seconds,
  54. * don't interfere with hour overflow. This avoids
  55. * messing with unknown time zones but requires your
  56. * RTC not to be off by more than 15 minutes
  57. */
  58. real_seconds = nowtime % 60;
  59. real_minutes = nowtime / 60;
  60. /* correct for half hour time zone */
  61. if (((abs(real_minutes - cmos_minutes) + 15)/30) & 1)
  62. real_minutes += 30;
  63. real_minutes %= 60;
  64. if (abs(real_minutes - cmos_minutes) < 30) {
  65. if (!(save_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
  66. real_seconds = bin2bcd(real_seconds);
  67. real_minutes = bin2bcd(real_minutes);
  68. }
  69. CMOS_WRITE(real_seconds, RTC_SECONDS);
  70. CMOS_WRITE(real_minutes, RTC_MINUTES);
  71. } else {
  72. printk_once(KERN_NOTICE
  73. "set_rtc_mmss: can't update from %d to %d\n",
  74. cmos_minutes, real_minutes);
  75. retval = -1;
  76. }
  77. /* The following flags have to be released exactly in this order,
  78. * otherwise the DS12887 (popular MC146818A clone with integrated
  79. * battery and quartz) will not reset the oscillator and will not
  80. * update precisely 500 ms later. You won't find this mentioned in
  81. * the Dallas Semiconductor data sheets, but who believes data
  82. * sheets anyway ... -- Markus Kuhn
  83. */
  84. CMOS_WRITE(save_control, RTC_CONTROL);
  85. CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
  86. spin_unlock_irqrestore(&rtc_lock, flags);
  87. return retval;
  88. }
  89. unsigned long mach_get_cmos_time(void)
  90. {
  91. unsigned int status, year, mon, day, hour, min, sec, century = 0;
  92. unsigned long flags;
  93. spin_lock_irqsave(&rtc_lock, flags);
  94. /*
  95. * If UIP is clear, then we have >= 244 microseconds before
  96. * RTC registers will be updated. Spec sheet says that this
  97. * is the reliable way to read RTC - registers. If UIP is set
  98. * then the register access might be invalid.
  99. */
  100. while ((CMOS_READ(RTC_FREQ_SELECT) & RTC_UIP))
  101. cpu_relax();
  102. sec = CMOS_READ(RTC_SECONDS);
  103. min = CMOS_READ(RTC_MINUTES);
  104. hour = CMOS_READ(RTC_HOURS);
  105. day = CMOS_READ(RTC_DAY_OF_MONTH);
  106. mon = CMOS_READ(RTC_MONTH);
  107. year = CMOS_READ(RTC_YEAR);
  108. #ifdef CONFIG_ACPI
  109. if (acpi_gbl_FADT.header.revision >= FADT2_REVISION_ID &&
  110. acpi_gbl_FADT.century)
  111. century = CMOS_READ(acpi_gbl_FADT.century);
  112. #endif
  113. status = CMOS_READ(RTC_CONTROL);
  114. WARN_ON_ONCE(RTC_ALWAYS_BCD && (status & RTC_DM_BINARY));
  115. spin_unlock_irqrestore(&rtc_lock, flags);
  116. if (RTC_ALWAYS_BCD || !(status & RTC_DM_BINARY)) {
  117. sec = bcd2bin(sec);
  118. min = bcd2bin(min);
  119. hour = bcd2bin(hour);
  120. day = bcd2bin(day);
  121. mon = bcd2bin(mon);
  122. year = bcd2bin(year);
  123. }
  124. if (century) {
  125. century = bcd2bin(century);
  126. year += century * 100;
  127. printk(KERN_INFO "Extended CMOS year: %d\n", century * 100);
  128. } else
  129. year += CMOS_YEARS_OFFS;
  130. return mktime(year, mon, day, hour, min, sec);
  131. }
  132. /* Routines for accessing the CMOS RAM/RTC. */
  133. unsigned char rtc_cmos_read(unsigned char addr)
  134. {
  135. unsigned char val;
  136. lock_cmos_prefix(addr);
  137. outb(addr, RTC_PORT(0));
  138. val = inb(RTC_PORT(1));
  139. lock_cmos_suffix(addr);
  140. return val;
  141. }
  142. EXPORT_SYMBOL(rtc_cmos_read);
  143. void rtc_cmos_write(unsigned char val, unsigned char addr)
  144. {
  145. lock_cmos_prefix(addr);
  146. outb(addr, RTC_PORT(0));
  147. outb(val, RTC_PORT(1));
  148. lock_cmos_suffix(addr);
  149. }
  150. EXPORT_SYMBOL(rtc_cmos_write);
  151. int update_persistent_clock(struct timespec now)
  152. {
  153. return x86_platform.set_wallclock(now.tv_sec);
  154. }
  155. /* not static: needed by APM */
  156. void read_persistent_clock(struct timespec *ts)
  157. {
  158. unsigned long retval;
  159. retval = x86_platform.get_wallclock();
  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. if (of_have_populated_dt())
  204. return 0;
  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);