maple_time.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /*
  2. * arch/ppc64/kernel/maple_time.c
  3. *
  4. * (c) Copyright 2004 Benjamin Herrenschmidt (benh@kernel.crashing.org),
  5. * IBM Corp.
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version
  10. * 2 of the License, or (at your option) any later version.
  11. *
  12. */
  13. #undef DEBUG
  14. #include <linux/config.h>
  15. #include <linux/errno.h>
  16. #include <linux/sched.h>
  17. #include <linux/kernel.h>
  18. #include <linux/param.h>
  19. #include <linux/string.h>
  20. #include <linux/mm.h>
  21. #include <linux/init.h>
  22. #include <linux/time.h>
  23. #include <linux/adb.h>
  24. #include <linux/pmu.h>
  25. #include <linux/interrupt.h>
  26. #include <linux/mc146818rtc.h>
  27. #include <linux/bcd.h>
  28. #include <asm/sections.h>
  29. #include <asm/prom.h>
  30. #include <asm/system.h>
  31. #include <asm/io.h>
  32. #include <asm/pgtable.h>
  33. #include <asm/machdep.h>
  34. #include <asm/time.h>
  35. #ifdef DEBUG
  36. #define DBG(x...) printk(x)
  37. #else
  38. #define DBG(x...)
  39. #endif
  40. extern void GregorianDay(struct rtc_time * tm);
  41. static int maple_rtc_addr;
  42. static int maple_clock_read(int addr)
  43. {
  44. outb_p(addr, maple_rtc_addr);
  45. return inb_p(maple_rtc_addr+1);
  46. }
  47. static void maple_clock_write(unsigned long val, int addr)
  48. {
  49. outb_p(addr, maple_rtc_addr);
  50. outb_p(val, maple_rtc_addr+1);
  51. }
  52. void maple_get_rtc_time(struct rtc_time *tm)
  53. {
  54. int uip, i;
  55. /* The Linux interpretation of the CMOS clock register contents:
  56. * When the Update-In-Progress (UIP) flag goes from 1 to 0, the
  57. * RTC registers show the second which has precisely just started.
  58. * Let's hope other operating systems interpret the RTC the same way.
  59. */
  60. /* Since the UIP flag is set for about 2.2 ms and the clock
  61. * is typically written with a precision of 1 jiffy, trying
  62. * to obtain a precision better than a few milliseconds is
  63. * an illusion. Only consistency is interesting, this also
  64. * allows to use the routine for /dev/rtc without a potential
  65. * 1 second kernel busy loop triggered by any reader of /dev/rtc.
  66. */
  67. for (i = 0; i<1000000; i++) {
  68. uip = maple_clock_read(RTC_FREQ_SELECT);
  69. tm->tm_sec = maple_clock_read(RTC_SECONDS);
  70. tm->tm_min = maple_clock_read(RTC_MINUTES);
  71. tm->tm_hour = maple_clock_read(RTC_HOURS);
  72. tm->tm_mday = maple_clock_read(RTC_DAY_OF_MONTH);
  73. tm->tm_mon = maple_clock_read(RTC_MONTH);
  74. tm->tm_year = maple_clock_read(RTC_YEAR);
  75. uip |= maple_clock_read(RTC_FREQ_SELECT);
  76. if ((uip & RTC_UIP)==0)
  77. break;
  78. }
  79. if (!(maple_clock_read(RTC_CONTROL) & RTC_DM_BINARY)
  80. || RTC_ALWAYS_BCD) {
  81. BCD_TO_BIN(tm->tm_sec);
  82. BCD_TO_BIN(tm->tm_min);
  83. BCD_TO_BIN(tm->tm_hour);
  84. BCD_TO_BIN(tm->tm_mday);
  85. BCD_TO_BIN(tm->tm_mon);
  86. BCD_TO_BIN(tm->tm_year);
  87. }
  88. if ((tm->tm_year + 1900) < 1970)
  89. tm->tm_year += 100;
  90. GregorianDay(tm);
  91. }
  92. int maple_set_rtc_time(struct rtc_time *tm)
  93. {
  94. unsigned char save_control, save_freq_select;
  95. int sec, min, hour, mon, mday, year;
  96. spin_lock(&rtc_lock);
  97. save_control = maple_clock_read(RTC_CONTROL); /* tell the clock it's being set */
  98. maple_clock_write((save_control|RTC_SET), RTC_CONTROL);
  99. save_freq_select = maple_clock_read(RTC_FREQ_SELECT); /* stop and reset prescaler */
  100. maple_clock_write((save_freq_select|RTC_DIV_RESET2), RTC_FREQ_SELECT);
  101. sec = tm->tm_sec;
  102. min = tm->tm_min;
  103. hour = tm->tm_hour;
  104. mon = tm->tm_mon;
  105. mday = tm->tm_mday;
  106. year = tm->tm_year;
  107. if (!(save_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
  108. BIN_TO_BCD(sec);
  109. BIN_TO_BCD(min);
  110. BIN_TO_BCD(hour);
  111. BIN_TO_BCD(mon);
  112. BIN_TO_BCD(mday);
  113. BIN_TO_BCD(year);
  114. }
  115. maple_clock_write(sec, RTC_SECONDS);
  116. maple_clock_write(min, RTC_MINUTES);
  117. maple_clock_write(hour, RTC_HOURS);
  118. maple_clock_write(mon, RTC_MONTH);
  119. maple_clock_write(mday, RTC_DAY_OF_MONTH);
  120. maple_clock_write(year, RTC_YEAR);
  121. /* The following flags have to be released exactly in this order,
  122. * otherwise the DS12887 (popular MC146818A clone with integrated
  123. * battery and quartz) will not reset the oscillator and will not
  124. * update precisely 500 ms later. You won't find this mentioned in
  125. * the Dallas Semiconductor data sheets, but who believes data
  126. * sheets anyway ... -- Markus Kuhn
  127. */
  128. maple_clock_write(save_control, RTC_CONTROL);
  129. maple_clock_write(save_freq_select, RTC_FREQ_SELECT);
  130. spin_unlock(&rtc_lock);
  131. return 0;
  132. }
  133. void __init maple_get_boot_time(struct rtc_time *tm)
  134. {
  135. struct device_node *rtcs;
  136. rtcs = find_compatible_devices("rtc", "pnpPNP,b00");
  137. if (rtcs && rtcs->addrs) {
  138. maple_rtc_addr = rtcs->addrs[0].address;
  139. printk(KERN_INFO "Maple: Found RTC at 0x%x\n", maple_rtc_addr);
  140. } else {
  141. maple_rtc_addr = RTC_PORT(0); /* legacy address */
  142. printk(KERN_INFO "Maple: No device node for RTC, assuming "
  143. "legacy address (0x%x)\n", maple_rtc_addr);
  144. }
  145. maple_get_rtc_time(tm);
  146. }