time.c 5.3 KB

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