/arch/powerpc/platforms/chrp/time.c

http://github.com/mirrors/linux · C · 161 lines · 119 code · 23 blank · 19 comment · 16 complexity · 94ef61928b583b74bd98efe072760fb4 MD5 · raw file

  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 1991, 1992, 1995 Linus Torvalds
  4. *
  5. * Adapted for PowerPC (PReP) by Gary Thomas
  6. * Modified by Cort Dougan (cort@cs.nmt.edu).
  7. * Copied and modified from arch/i386/kernel/time.c
  8. *
  9. */
  10. #include <linux/errno.h>
  11. #include <linux/sched.h>
  12. #include <linux/kernel.h>
  13. #include <linux/param.h>
  14. #include <linux/string.h>
  15. #include <linux/mm.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/time.h>
  18. #include <linux/timex.h>
  19. #include <linux/kernel_stat.h>
  20. #include <linux/mc146818rtc.h>
  21. #include <linux/init.h>
  22. #include <linux/bcd.h>
  23. #include <linux/ioport.h>
  24. #include <asm/io.h>
  25. #include <asm/nvram.h>
  26. #include <asm/prom.h>
  27. #include <asm/sections.h>
  28. #include <asm/time.h>
  29. #include <platforms/chrp/chrp.h>
  30. extern spinlock_t rtc_lock;
  31. #define NVRAM_AS0 0x74
  32. #define NVRAM_AS1 0x75
  33. #define NVRAM_DATA 0x77
  34. static int nvram_as1 = NVRAM_AS1;
  35. static int nvram_as0 = NVRAM_AS0;
  36. static int nvram_data = NVRAM_DATA;
  37. long __init chrp_time_init(void)
  38. {
  39. struct device_node *rtcs;
  40. struct resource r;
  41. int base;
  42. rtcs = of_find_compatible_node(NULL, "rtc", "pnpPNP,b00");
  43. if (rtcs == NULL)
  44. rtcs = of_find_compatible_node(NULL, "rtc", "ds1385-rtc");
  45. if (rtcs == NULL)
  46. return 0;
  47. if (of_address_to_resource(rtcs, 0, &r)) {
  48. of_node_put(rtcs);
  49. return 0;
  50. }
  51. of_node_put(rtcs);
  52. base = r.start;
  53. nvram_as1 = 0;
  54. nvram_as0 = base;
  55. nvram_data = base + 1;
  56. return 0;
  57. }
  58. static int chrp_cmos_clock_read(int addr)
  59. {
  60. if (nvram_as1 != 0)
  61. outb(addr>>8, nvram_as1);
  62. outb(addr, nvram_as0);
  63. return (inb(nvram_data));
  64. }
  65. static void chrp_cmos_clock_write(unsigned long val, int addr)
  66. {
  67. if (nvram_as1 != 0)
  68. outb(addr>>8, nvram_as1);
  69. outb(addr, nvram_as0);
  70. outb(val, nvram_data);
  71. return;
  72. }
  73. /*
  74. * Set the hardware clock. -- Cort
  75. */
  76. int chrp_set_rtc_time(struct rtc_time *tmarg)
  77. {
  78. unsigned char save_control, save_freq_select;
  79. struct rtc_time tm = *tmarg;
  80. spin_lock(&rtc_lock);
  81. save_control = chrp_cmos_clock_read(RTC_CONTROL); /* tell the clock it's being set */
  82. chrp_cmos_clock_write((save_control|RTC_SET), RTC_CONTROL);
  83. save_freq_select = chrp_cmos_clock_read(RTC_FREQ_SELECT); /* stop and reset prescaler */
  84. chrp_cmos_clock_write((save_freq_select|RTC_DIV_RESET2), RTC_FREQ_SELECT);
  85. if (!(save_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
  86. tm.tm_sec = bin2bcd(tm.tm_sec);
  87. tm.tm_min = bin2bcd(tm.tm_min);
  88. tm.tm_hour = bin2bcd(tm.tm_hour);
  89. tm.tm_mon = bin2bcd(tm.tm_mon);
  90. tm.tm_mday = bin2bcd(tm.tm_mday);
  91. tm.tm_year = bin2bcd(tm.tm_year);
  92. }
  93. chrp_cmos_clock_write(tm.tm_sec,RTC_SECONDS);
  94. chrp_cmos_clock_write(tm.tm_min,RTC_MINUTES);
  95. chrp_cmos_clock_write(tm.tm_hour,RTC_HOURS);
  96. chrp_cmos_clock_write(tm.tm_mon,RTC_MONTH);
  97. chrp_cmos_clock_write(tm.tm_mday,RTC_DAY_OF_MONTH);
  98. chrp_cmos_clock_write(tm.tm_year,RTC_YEAR);
  99. /* The following flags have to be released exactly in this order,
  100. * otherwise the DS12887 (popular MC146818A clone with integrated
  101. * battery and quartz) will not reset the oscillator and will not
  102. * update precisely 500 ms later. You won't find this mentioned in
  103. * the Dallas Semiconductor data sheets, but who believes data
  104. * sheets anyway ... -- Markus Kuhn
  105. */
  106. chrp_cmos_clock_write(save_control, RTC_CONTROL);
  107. chrp_cmos_clock_write(save_freq_select, RTC_FREQ_SELECT);
  108. spin_unlock(&rtc_lock);
  109. return 0;
  110. }
  111. void chrp_get_rtc_time(struct rtc_time *tm)
  112. {
  113. unsigned int year, mon, day, hour, min, sec;
  114. do {
  115. sec = chrp_cmos_clock_read(RTC_SECONDS);
  116. min = chrp_cmos_clock_read(RTC_MINUTES);
  117. hour = chrp_cmos_clock_read(RTC_HOURS);
  118. day = chrp_cmos_clock_read(RTC_DAY_OF_MONTH);
  119. mon = chrp_cmos_clock_read(RTC_MONTH);
  120. year = chrp_cmos_clock_read(RTC_YEAR);
  121. } while (sec != chrp_cmos_clock_read(RTC_SECONDS));
  122. if (!(chrp_cmos_clock_read(RTC_CONTROL) & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
  123. sec = bcd2bin(sec);
  124. min = bcd2bin(min);
  125. hour = bcd2bin(hour);
  126. day = bcd2bin(day);
  127. mon = bcd2bin(mon);
  128. year = bcd2bin(year);
  129. }
  130. if (year < 70)
  131. year += 100;
  132. tm->tm_sec = sec;
  133. tm->tm_min = min;
  134. tm->tm_hour = hour;
  135. tm->tm_mday = day;
  136. tm->tm_mon = mon;
  137. tm->tm_year = year;
  138. }