time.c 4.3 KB

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