rtc.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. #include <asm/rtc.h>
  16. #ifdef CONFIG_X86_32
  17. /*
  18. * This is a special lock that is owned by the CPU and holds the index
  19. * register we are working with. It is required for NMI access to the
  20. * CMOS/RTC registers. See include/asm-i386/mc146818rtc.h for details.
  21. */
  22. volatile unsigned long cmos_lock;
  23. EXPORT_SYMBOL(cmos_lock);
  24. #endif /* CONFIG_X86_32 */
  25. /* For two digit years assume time is always after that */
  26. #define CMOS_YEARS_OFFS 2000
  27. DEFINE_SPINLOCK(rtc_lock);
  28. EXPORT_SYMBOL(rtc_lock);
  29. /*
  30. * In order to set the CMOS clock precisely, set_rtc_mmss has to be
  31. * called 500 ms after the second nowtime has started, because when
  32. * nowtime is written into the registers of the CMOS clock, it will
  33. * jump to the next second precisely 500 ms later. Check the Motorola
  34. * MC146818A or Dallas DS12887 data sheet for details.
  35. */
  36. int mach_set_rtc_mmss(unsigned long nowtime)
  37. {
  38. struct rtc_time tm;
  39. int retval = 0;
  40. rtc_time_to_tm(nowtime, &tm);
  41. if (!rtc_valid_tm(&tm)) {
  42. retval = set_rtc_time(&tm);
  43. if (retval)
  44. printk(KERN_ERR "%s: RTC write failed with error %d\n",
  45. __FUNCTION__, retval);
  46. } else {
  47. printk(KERN_ERR
  48. "%s: Invalid RTC value: write of %lx to RTC failed\n",
  49. __FUNCTION__, nowtime);
  50. retval = -EINVAL;
  51. }
  52. return retval;
  53. }
  54. unsigned long mach_get_cmos_time(void)
  55. {
  56. unsigned int status, year, mon, day, hour, min, sec, century = 0;
  57. unsigned long flags;
  58. spin_lock_irqsave(&rtc_lock, flags);
  59. /*
  60. * If UIP is clear, then we have >= 244 microseconds before
  61. * RTC registers will be updated. Spec sheet says that this
  62. * is the reliable way to read RTC - registers. If UIP is set
  63. * then the register access might be invalid.
  64. */
  65. while ((CMOS_READ(RTC_FREQ_SELECT) & RTC_UIP))
  66. cpu_relax();
  67. sec = CMOS_READ(RTC_SECONDS);
  68. min = CMOS_READ(RTC_MINUTES);
  69. hour = CMOS_READ(RTC_HOURS);
  70. day = CMOS_READ(RTC_DAY_OF_MONTH);
  71. mon = CMOS_READ(RTC_MONTH);
  72. year = CMOS_READ(RTC_YEAR);
  73. #ifdef CONFIG_ACPI
  74. if (acpi_gbl_FADT.header.revision >= FADT2_REVISION_ID &&
  75. acpi_gbl_FADT.century)
  76. century = CMOS_READ(acpi_gbl_FADT.century);
  77. #endif
  78. status = CMOS_READ(RTC_CONTROL);
  79. WARN_ON_ONCE(RTC_ALWAYS_BCD && (status & RTC_DM_BINARY));
  80. spin_unlock_irqrestore(&rtc_lock, flags);
  81. if (RTC_ALWAYS_BCD || !(status & RTC_DM_BINARY)) {
  82. sec = bcd2bin(sec);
  83. min = bcd2bin(min);
  84. hour = bcd2bin(hour);
  85. day = bcd2bin(day);
  86. mon = bcd2bin(mon);
  87. year = bcd2bin(year);
  88. }
  89. if (century) {
  90. century = bcd2bin(century);
  91. year += century * 100;
  92. } else
  93. year += CMOS_YEARS_OFFS;
  94. return mktime(year, mon, day, hour, min, sec);
  95. }
  96. /* Routines for accessing the CMOS RAM/RTC. */
  97. unsigned char rtc_cmos_read(unsigned char addr)
  98. {
  99. unsigned char val;
  100. lock_cmos_prefix(addr);
  101. outb(addr, RTC_PORT(0));
  102. val = inb(RTC_PORT(1));
  103. lock_cmos_suffix(addr);
  104. return val;
  105. }
  106. EXPORT_SYMBOL(rtc_cmos_read);
  107. void rtc_cmos_write(unsigned char val, unsigned char addr)
  108. {
  109. lock_cmos_prefix(addr);
  110. outb(addr, RTC_PORT(0));
  111. outb(val, RTC_PORT(1));
  112. lock_cmos_suffix(addr);
  113. }
  114. EXPORT_SYMBOL(rtc_cmos_write);
  115. int update_persistent_clock(struct timespec now)
  116. {
  117. return x86_platform.set_wallclock(now.tv_sec);
  118. }
  119. /* not static: needed by APM */
  120. void read_persistent_clock(struct timespec *ts)
  121. {
  122. unsigned long retval;
  123. retval = x86_platform.get_wallclock();
  124. ts->tv_sec = retval;
  125. ts->tv_nsec = 0;
  126. }
  127. static struct resource rtc_resources[] = {
  128. [0] = {
  129. .start = RTC_PORT(0),
  130. .end = RTC_PORT(1),
  131. .flags = IORESOURCE_IO,
  132. },
  133. [1] = {
  134. .start = RTC_IRQ,
  135. .end = RTC_IRQ,
  136. .flags = IORESOURCE_IRQ,
  137. }
  138. };
  139. static struct platform_device rtc_device = {
  140. .name = "rtc_cmos",
  141. .id = -1,
  142. .resource = rtc_resources,
  143. .num_resources = ARRAY_SIZE(rtc_resources),
  144. };
  145. static __init int add_rtc_cmos(void)
  146. {
  147. #ifdef CONFIG_PNP
  148. static const char * const const ids[] __initconst =
  149. { "PNP0b00", "PNP0b01", "PNP0b02", };
  150. struct pnp_dev *dev;
  151. struct pnp_id *id;
  152. int i;
  153. pnp_for_each_dev(dev) {
  154. for (id = dev->id; id; id = id->next) {
  155. for (i = 0; i < ARRAY_SIZE(ids); i++) {
  156. if (compare_pnp_id(id, ids[i]) != 0)
  157. return 0;
  158. }
  159. }
  160. }
  161. #endif
  162. if (of_have_populated_dt())
  163. return 0;
  164. /* Intel MID platforms don't have ioport rtc */
  165. if (mrst_identify_cpu())
  166. return -ENODEV;
  167. platform_device_register(&rtc_device);
  168. dev_info(&rtc_device.dev,
  169. "registered platform RTC device (no PNP device found)\n");
  170. return 0;
  171. }
  172. device_initcall(add_rtc_cmos);