rtc.c 6.3 KB

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