time.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. do {
  54. tm->tm_sec = maple_clock_read(RTC_SECONDS);
  55. tm->tm_min = maple_clock_read(RTC_MINUTES);
  56. tm->tm_hour = maple_clock_read(RTC_HOURS);
  57. tm->tm_mday = maple_clock_read(RTC_DAY_OF_MONTH);
  58. tm->tm_mon = maple_clock_read(RTC_MONTH);
  59. tm->tm_year = maple_clock_read(RTC_YEAR);
  60. } while (tm->tm_sec != maple_clock_read(RTC_SECONDS));
  61. if (!(maple_clock_read(RTC_CONTROL) & RTC_DM_BINARY)
  62. || RTC_ALWAYS_BCD) {
  63. BCD_TO_BIN(tm->tm_sec);
  64. BCD_TO_BIN(tm->tm_min);
  65. BCD_TO_BIN(tm->tm_hour);
  66. BCD_TO_BIN(tm->tm_mday);
  67. BCD_TO_BIN(tm->tm_mon);
  68. BCD_TO_BIN(tm->tm_year);
  69. }
  70. if ((tm->tm_year + 1900) < 1970)
  71. tm->tm_year += 100;
  72. GregorianDay(tm);
  73. }
  74. int maple_set_rtc_time(struct rtc_time *tm)
  75. {
  76. unsigned char save_control, save_freq_select;
  77. int sec, min, hour, mon, mday, year;
  78. spin_lock(&rtc_lock);
  79. save_control = maple_clock_read(RTC_CONTROL); /* tell the clock it's being set */
  80. maple_clock_write((save_control|RTC_SET), RTC_CONTROL);
  81. save_freq_select = maple_clock_read(RTC_FREQ_SELECT); /* stop and reset prescaler */
  82. maple_clock_write((save_freq_select|RTC_DIV_RESET2), RTC_FREQ_SELECT);
  83. sec = tm->tm_sec;
  84. min = tm->tm_min;
  85. hour = tm->tm_hour;
  86. mon = tm->tm_mon;
  87. mday = tm->tm_mday;
  88. year = tm->tm_year;
  89. if (!(save_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
  90. BIN_TO_BCD(sec);
  91. BIN_TO_BCD(min);
  92. BIN_TO_BCD(hour);
  93. BIN_TO_BCD(mon);
  94. BIN_TO_BCD(mday);
  95. BIN_TO_BCD(year);
  96. }
  97. maple_clock_write(sec, RTC_SECONDS);
  98. maple_clock_write(min, RTC_MINUTES);
  99. maple_clock_write(hour, RTC_HOURS);
  100. maple_clock_write(mon, RTC_MONTH);
  101. maple_clock_write(mday, RTC_DAY_OF_MONTH);
  102. maple_clock_write(year, RTC_YEAR);
  103. /* The following flags have to be released exactly in this order,
  104. * otherwise the DS12887 (popular MC146818A clone with integrated
  105. * battery and quartz) will not reset the oscillator and will not
  106. * update precisely 500 ms later. You won't find this mentioned in
  107. * the Dallas Semiconductor data sheets, but who believes data
  108. * sheets anyway ... -- Markus Kuhn
  109. */
  110. maple_clock_write(save_control, RTC_CONTROL);
  111. maple_clock_write(save_freq_select, RTC_FREQ_SELECT);
  112. spin_unlock(&rtc_lock);
  113. return 0;
  114. }
  115. static struct resource rtc_iores = {
  116. .name = "rtc",
  117. .flags = IORESOURCE_BUSY,
  118. };
  119. unsigned long __init maple_get_boot_time(void)
  120. {
  121. struct rtc_time tm;
  122. struct device_node *rtcs;
  123. rtcs = of_find_compatible_node(NULL, "rtc", "pnpPNP,b00");
  124. if (rtcs) {
  125. struct resource r;
  126. if (of_address_to_resource(rtcs, 0, &r)) {
  127. printk(KERN_EMERG "Maple: Unable to translate RTC"
  128. " address\n");
  129. goto bail;
  130. }
  131. if (!(r.flags & IORESOURCE_IO)) {
  132. printk(KERN_EMERG "Maple: RTC address isn't PIO!\n");
  133. goto bail;
  134. }
  135. maple_rtc_addr = r.start;
  136. printk(KERN_INFO "Maple: Found RTC at IO 0x%x\n",
  137. maple_rtc_addr);
  138. }
  139. bail:
  140. if (maple_rtc_addr == 0) {
  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. rtc_iores.start = maple_rtc_addr;
  146. rtc_iores.end = maple_rtc_addr + 7;
  147. request_resource(&ioport_resource, &rtc_iores);
  148. maple_get_rtc_time(&tm);
  149. return mktime(tm.tm_year+1900, tm.tm_mon+1, tm.tm_mday,
  150. tm.tm_hour, tm.tm_min, tm.tm_sec);
  151. }