time.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*
  2. * Copyright (C) 1991, 1992, 1995 Linus Torvalds
  3. *
  4. * Adapted for PowerPC (PReP) by Gary Thomas
  5. * Modified by Cort Dougan (cort@cs.nmt.edu).
  6. * Copied and modified from arch/i386/kernel/time.c
  7. *
  8. */
  9. #include <linux/errno.h>
  10. #include <linux/sched.h>
  11. #include <linux/kernel.h>
  12. #include <linux/param.h>
  13. #include <linux/string.h>
  14. #include <linux/mm.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/time.h>
  17. #include <linux/timex.h>
  18. #include <linux/kernel_stat.h>
  19. #include <linux/mc146818rtc.h>
  20. #include <linux/init.h>
  21. #include <linux/bcd.h>
  22. #include <linux/ioport.h>
  23. #include <asm/io.h>
  24. #include <asm/nvram.h>
  25. #include <asm/prom.h>
  26. #include <asm/sections.h>
  27. #include <asm/time.h>
  28. extern spinlock_t rtc_lock;
  29. static int nvram_as1 = NVRAM_AS1;
  30. static int nvram_as0 = NVRAM_AS0;
  31. static int nvram_data = NVRAM_DATA;
  32. long __init chrp_time_init(void)
  33. {
  34. struct device_node *rtcs;
  35. struct resource r;
  36. int base;
  37. rtcs = of_find_compatible_node(NULL, "rtc", "pnpPNP,b00");
  38. if (rtcs == NULL)
  39. rtcs = of_find_compatible_node(NULL, "rtc", "ds1385-rtc");
  40. if (rtcs == NULL)
  41. return 0;
  42. if (of_address_to_resource(rtcs, 0, &r)) {
  43. of_node_put(rtcs);
  44. return 0;
  45. }
  46. of_node_put(rtcs);
  47. base = r.start;
  48. nvram_as1 = 0;
  49. nvram_as0 = base;
  50. nvram_data = base + 1;
  51. return 0;
  52. }
  53. int chrp_cmos_clock_read(int addr)
  54. {
  55. if (nvram_as1 != 0)
  56. outb(addr>>8, nvram_as1);
  57. outb(addr, nvram_as0);
  58. return (inb(nvram_data));
  59. }
  60. void chrp_cmos_clock_write(unsigned long val, int addr)
  61. {
  62. if (nvram_as1 != 0)
  63. outb(addr>>8, nvram_as1);
  64. outb(addr, nvram_as0);
  65. outb(val, nvram_data);
  66. return;
  67. }
  68. /*
  69. * Set the hardware clock. -- Cort
  70. */
  71. int chrp_set_rtc_time(struct rtc_time *tmarg)
  72. {
  73. unsigned char save_control, save_freq_select;
  74. struct rtc_time tm = *tmarg;
  75. spin_lock(&rtc_lock);
  76. save_control = chrp_cmos_clock_read(RTC_CONTROL); /* tell the clock it's being set */
  77. chrp_cmos_clock_write((save_control|RTC_SET), RTC_CONTROL);
  78. save_freq_select = chrp_cmos_clock_read(RTC_FREQ_SELECT); /* stop and reset prescaler */
  79. chrp_cmos_clock_write((save_freq_select|RTC_DIV_RESET2), RTC_FREQ_SELECT);
  80. if (!(save_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
  81. tm.tm_sec = bin2bcd(tm.tm_sec);
  82. tm.tm_min = bin2bcd(tm.tm_min);
  83. tm.tm_hour = bin2bcd(tm.tm_hour);
  84. tm.tm_mon = bin2bcd(tm.tm_mon);
  85. tm.tm_mday = bin2bcd(tm.tm_mday);
  86. tm.tm_year = bin2bcd(tm.tm_year);
  87. }
  88. chrp_cmos_clock_write(tm.tm_sec,RTC_SECONDS);
  89. chrp_cmos_clock_write(tm.tm_min,RTC_MINUTES);
  90. chrp_cmos_clock_write(tm.tm_hour,RTC_HOURS);
  91. chrp_cmos_clock_write(tm.tm_mon,RTC_MONTH);
  92. chrp_cmos_clock_write(tm.tm_mday,RTC_DAY_OF_MONTH);
  93. chrp_cmos_clock_write(tm.tm_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. chrp_cmos_clock_write(save_control, RTC_CONTROL);
  102. chrp_cmos_clock_write(save_freq_select, RTC_FREQ_SELECT);
  103. spin_unlock(&rtc_lock);
  104. return 0;
  105. }
  106. void chrp_get_rtc_time(struct rtc_time *tm)
  107. {
  108. unsigned int year, mon, day, hour, min, sec;
  109. do {
  110. sec = chrp_cmos_clock_read(RTC_SECONDS);
  111. min = chrp_cmos_clock_read(RTC_MINUTES);
  112. hour = chrp_cmos_clock_read(RTC_HOURS);
  113. day = chrp_cmos_clock_read(RTC_DAY_OF_MONTH);
  114. mon = chrp_cmos_clock_read(RTC_MONTH);
  115. year = chrp_cmos_clock_read(RTC_YEAR);
  116. } while (sec != chrp_cmos_clock_read(RTC_SECONDS));
  117. if (!(chrp_cmos_clock_read(RTC_CONTROL) & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
  118. sec = bcd2bin(sec);
  119. min = bcd2bin(min);
  120. hour = bcd2bin(hour);
  121. day = bcd2bin(day);
  122. mon = bcd2bin(mon);
  123. year = bcd2bin(year);
  124. }
  125. if (year < 70)
  126. year += 100;
  127. tm->tm_sec = sec;
  128. tm->tm_min = min;
  129. tm->tm_hour = hour;
  130. tm->tm_mday = day;
  131. tm->tm_mon = mon;
  132. tm->tm_year = year;
  133. }