time.c 4.6 KB

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