rtc.c 6.4 KB

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