time.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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/errno.h>
  13. #include <linux/sched.h>
  14. #include <linux/kernel.h>
  15. #include <linux/param.h>
  16. #include <linux/string.h>
  17. #include <linux/mm.h>
  18. #include <linux/init.h>
  19. #include <linux/time.h>
  20. #include <linux/adb.h>
  21. #include <linux/pmu.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/mc146818rtc.h>
  24. #include <linux/bcd.h>
  25. #include <asm/sections.h>
  26. #include <asm/prom.h>
  27. #include <asm/io.h>
  28. #include <asm/pgtable.h>
  29. #include <asm/machdep.h>
  30. #include <asm/time.h>
  31. #include "maple.h"
  32. #ifdef DEBUG
  33. #define DBG(x...) printk(x)
  34. #else
  35. #define DBG(x...)
  36. #endif
  37. static int maple_rtc_addr;
  38. static int maple_clock_read(int addr)
  39. {
  40. outb_p(addr, maple_rtc_addr);
  41. return inb_p(maple_rtc_addr+1);
  42. }
  43. static void maple_clock_write(unsigned long val, int addr)
  44. {
  45. outb_p(addr, maple_rtc_addr);
  46. outb_p(val, maple_rtc_addr+1);
  47. }
  48. void maple_get_rtc_time(struct rtc_time *tm)
  49. {
  50. do {
  51. tm->tm_sec = maple_clock_read(RTC_SECONDS);
  52. tm->tm_min = maple_clock_read(RTC_MINUTES);
  53. tm->tm_hour = maple_clock_read(RTC_HOURS);
  54. tm->tm_mday = maple_clock_read(RTC_DAY_OF_MONTH);
  55. tm->tm_mon = maple_clock_read(RTC_MONTH);
  56. tm->tm_year = maple_clock_read(RTC_YEAR);
  57. } while (tm->tm_sec != maple_clock_read(RTC_SECONDS));
  58. if (!(maple_clock_read(RTC_CONTROL) & RTC_DM_BINARY)
  59. || RTC_ALWAYS_BCD) {
  60. tm->tm_sec = bcd2bin(tm->tm_sec);
  61. tm->tm_min = bcd2bin(tm->tm_min);
  62. tm->tm_hour = bcd2bin(tm->tm_hour);
  63. tm->tm_mday = bcd2bin(tm->tm_mday);
  64. tm->tm_mon = bcd2bin(tm->tm_mon);
  65. tm->tm_year = bcd2bin(tm->tm_year);
  66. }
  67. if ((tm->tm_year + 1900) < 1970)
  68. tm->tm_year += 100;
  69. GregorianDay(tm);
  70. }
  71. int maple_set_rtc_time(struct rtc_time *tm)
  72. {
  73. unsigned char save_control, save_freq_select;
  74. int sec, min, hour, mon, mday, year;
  75. spin_lock(&rtc_lock);
  76. save_control = maple_clock_read(RTC_CONTROL); /* tell the clock it's being set */
  77. maple_clock_write((save_control|RTC_SET), RTC_CONTROL);
  78. save_freq_select = maple_clock_read(RTC_FREQ_SELECT); /* stop and reset prescaler */
  79. maple_clock_write((save_freq_select|RTC_DIV_RESET2), RTC_FREQ_SELECT);
  80. sec = tm->tm_sec;
  81. min = tm->tm_min;
  82. hour = tm->tm_hour;
  83. mon = tm->tm_mon;
  84. mday = tm->tm_mday;
  85. year = tm->tm_year;
  86. if (!(save_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
  87. sec = bin2bcd(sec);
  88. min = bin2bcd(min);
  89. hour = bin2bcd(hour);
  90. mon = bin2bcd(mon);
  91. mday = bin2bcd(mday);
  92. year = bin2bcd(year);
  93. }
  94. maple_clock_write(sec, RTC_SECONDS);
  95. maple_clock_write(min, RTC_MINUTES);
  96. maple_clock_write(hour, RTC_HOURS);
  97. maple_clock_write(mon, RTC_MONTH);
  98. maple_clock_write(mday, RTC_DAY_OF_MONTH);
  99. maple_clock_write(year, RTC_YEAR);
  100. /* The following flags have to be released exactly in this order,
  101. * otherwise the DS12887 (popular MC146818A clone with integrated
  102. * battery and quartz) will not reset the oscillator and will not
  103. * update precisely 500 ms later. You won't find this mentioned in
  104. * the Dallas Semiconductor data sheets, but who believes data
  105. * sheets anyway ... -- Markus Kuhn
  106. */
  107. maple_clock_write(save_control, RTC_CONTROL);
  108. maple_clock_write(save_freq_select, RTC_FREQ_SELECT);
  109. spin_unlock(&rtc_lock);
  110. return 0;
  111. }
  112. static struct resource rtc_iores = {
  113. .name = "rtc",
  114. .flags = IORESOURCE_BUSY,
  115. };
  116. unsigned long __init maple_get_boot_time(void)
  117. {
  118. struct rtc_time tm;
  119. struct device_node *rtcs;
  120. rtcs = of_find_compatible_node(NULL, "rtc", "pnpPNP,b00");
  121. if (rtcs) {
  122. struct resource r;
  123. if (of_address_to_resource(rtcs, 0, &r)) {
  124. printk(KERN_EMERG "Maple: Unable to translate RTC"
  125. " address\n");
  126. goto bail;
  127. }
  128. if (!(r.flags & IORESOURCE_IO)) {
  129. printk(KERN_EMERG "Maple: RTC address isn't PIO!\n");
  130. goto bail;
  131. }
  132. maple_rtc_addr = r.start;
  133. printk(KERN_INFO "Maple: Found RTC at IO 0x%x\n",
  134. maple_rtc_addr);
  135. }
  136. bail:
  137. if (maple_rtc_addr == 0) {
  138. maple_rtc_addr = RTC_PORT(0); /* legacy address */
  139. printk(KERN_INFO "Maple: No device node for RTC, assuming "
  140. "legacy address (0x%x)\n", maple_rtc_addr);
  141. }
  142. rtc_iores.start = maple_rtc_addr;
  143. rtc_iores.end = maple_rtc_addr + 7;
  144. request_resource(&ioport_resource, &rtc_iores);
  145. maple_get_rtc_time(&tm);
  146. return mktime(tm.tm_year+1900, tm.tm_mon+1, tm.tm_mday,
  147. tm.tm_hour, tm.tm_min, tm.tm_sec);
  148. }