time.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. #include "maple.h"
  36. #ifdef DEBUG
  37. #define DBG(x...) printk(x)
  38. #else
  39. #define DBG(x...)
  40. #endif
  41. extern void GregorianDay(struct rtc_time * tm);
  42. static int maple_rtc_addr;
  43. static int maple_clock_read(int addr)
  44. {
  45. outb_p(addr, maple_rtc_addr);
  46. return inb_p(maple_rtc_addr+1);
  47. }
  48. static void maple_clock_write(unsigned long val, int addr)
  49. {
  50. outb_p(addr, maple_rtc_addr);
  51. outb_p(val, maple_rtc_addr+1);
  52. }
  53. void maple_get_rtc_time(struct rtc_time *tm)
  54. {
  55. int uip, i;
  56. /* The Linux interpretation of the CMOS clock register contents:
  57. * When the Update-In-Progress (UIP) flag goes from 1 to 0, the
  58. * RTC registers show the second which has precisely just started.
  59. * Let's hope other operating systems interpret the RTC the same way.
  60. */
  61. /* Since the UIP flag is set for about 2.2 ms and the clock
  62. * is typically written with a precision of 1 jiffy, trying
  63. * to obtain a precision better than a few milliseconds is
  64. * an illusion. Only consistency is interesting, this also
  65. * allows to use the routine for /dev/rtc without a potential
  66. * 1 second kernel busy loop triggered by any reader of /dev/rtc.
  67. */
  68. for (i = 0; i<1000000; i++) {
  69. uip = maple_clock_read(RTC_FREQ_SELECT);
  70. tm->tm_sec = maple_clock_read(RTC_SECONDS);
  71. tm->tm_min = maple_clock_read(RTC_MINUTES);
  72. tm->tm_hour = maple_clock_read(RTC_HOURS);
  73. tm->tm_mday = maple_clock_read(RTC_DAY_OF_MONTH);
  74. tm->tm_mon = maple_clock_read(RTC_MONTH);
  75. tm->tm_year = maple_clock_read(RTC_YEAR);
  76. uip |= maple_clock_read(RTC_FREQ_SELECT);
  77. if ((uip & RTC_UIP)==0)
  78. break;
  79. }
  80. if (!(maple_clock_read(RTC_CONTROL) & RTC_DM_BINARY)
  81. || RTC_ALWAYS_BCD) {
  82. BCD_TO_BIN(tm->tm_sec);
  83. BCD_TO_BIN(tm->tm_min);
  84. BCD_TO_BIN(tm->tm_hour);
  85. BCD_TO_BIN(tm->tm_mday);
  86. BCD_TO_BIN(tm->tm_mon);
  87. BCD_TO_BIN(tm->tm_year);
  88. }
  89. if ((tm->tm_year + 1900) < 1970)
  90. tm->tm_year += 100;
  91. GregorianDay(tm);
  92. }
  93. int maple_set_rtc_time(struct rtc_time *tm)
  94. {
  95. unsigned char save_control, save_freq_select;
  96. int sec, min, hour, mon, mday, year;
  97. spin_lock(&rtc_lock);
  98. save_control = maple_clock_read(RTC_CONTROL); /* tell the clock it's being set */
  99. maple_clock_write((save_control|RTC_SET), RTC_CONTROL);
  100. save_freq_select = maple_clock_read(RTC_FREQ_SELECT); /* stop and reset prescaler */
  101. maple_clock_write((save_freq_select|RTC_DIV_RESET2), RTC_FREQ_SELECT);
  102. sec = tm->tm_sec;
  103. min = tm->tm_min;
  104. hour = tm->tm_hour;
  105. mon = tm->tm_mon;
  106. mday = tm->tm_mday;
  107. year = tm->tm_year;
  108. if (!(save_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
  109. BIN_TO_BCD(sec);
  110. BIN_TO_BCD(min);
  111. BIN_TO_BCD(hour);
  112. BIN_TO_BCD(mon);
  113. BIN_TO_BCD(mday);
  114. BIN_TO_BCD(year);
  115. }
  116. maple_clock_write(sec, RTC_SECONDS);
  117. maple_clock_write(min, RTC_MINUTES);
  118. maple_clock_write(hour, RTC_HOURS);
  119. maple_clock_write(mon, RTC_MONTH);
  120. maple_clock_write(mday, RTC_DAY_OF_MONTH);
  121. maple_clock_write(year, RTC_YEAR);
  122. /* The following flags have to be released exactly in this order,
  123. * otherwise the DS12887 (popular MC146818A clone with integrated
  124. * battery and quartz) will not reset the oscillator and will not
  125. * update precisely 500 ms later. You won't find this mentioned in
  126. * the Dallas Semiconductor data sheets, but who believes data
  127. * sheets anyway ... -- Markus Kuhn
  128. */
  129. maple_clock_write(save_control, RTC_CONTROL);
  130. maple_clock_write(save_freq_select, RTC_FREQ_SELECT);
  131. spin_unlock(&rtc_lock);
  132. return 0;
  133. }
  134. static struct resource rtc_iores = {
  135. .name = "rtc",
  136. .flags = IORESOURCE_BUSY,
  137. };
  138. unsigned long __init maple_get_boot_time(void)
  139. {
  140. struct rtc_time tm;
  141. struct device_node *rtcs;
  142. rtcs = of_find_compatible_node(NULL, "rtc", "pnpPNP,b00");
  143. if (rtcs) {
  144. struct resource r;
  145. if (of_address_to_resource(rtcs, 0, &r)) {
  146. printk(KERN_EMERG "Maple: Unable to translate RTC"
  147. " address\n");
  148. goto bail;
  149. }
  150. if (!(r.flags & IORESOURCE_IO)) {
  151. printk(KERN_EMERG "Maple: RTC address isn't PIO!\n");
  152. goto bail;
  153. }
  154. maple_rtc_addr = r.start;
  155. printk(KERN_INFO "Maple: Found RTC at IO 0x%x\n",
  156. maple_rtc_addr);
  157. }
  158. bail:
  159. if (maple_rtc_addr == 0) {
  160. maple_rtc_addr = RTC_PORT(0); /* legacy address */
  161. printk(KERN_INFO "Maple: No device node for RTC, assuming "
  162. "legacy address (0x%x)\n", maple_rtc_addr);
  163. }
  164. rtc_iores.start = maple_rtc_addr;
  165. rtc_iores.end = maple_rtc_addr + 7;
  166. request_resource(&ioport_resource, &rtc_iores);
  167. maple_get_rtc_time(&tm);
  168. return mktime(tm.tm_year+1900, tm.tm_mon+1, tm.tm_mday,
  169. tm.tm_hour, tm.tm_min, tm.tm_sec);
  170. }