time.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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 = find_compatible_devices("rtc", "pnpPNP,b00");
  38. if (rtcs == NULL)
  39. rtcs = find_compatible_devices("rtc", "ds1385-rtc");
  40. if (rtcs == NULL || of_address_to_resource(rtcs, 0, &r))
  41. return 0;
  42. base = r.start;
  43. nvram_as1 = 0;
  44. nvram_as0 = base;
  45. nvram_data = base + 1;
  46. return 0;
  47. }
  48. int chrp_cmos_clock_read(int addr)
  49. {
  50. if (nvram_as1 != 0)
  51. outb(addr>>8, nvram_as1);
  52. outb(addr, nvram_as0);
  53. return (inb(nvram_data));
  54. }
  55. void chrp_cmos_clock_write(unsigned long val, int addr)
  56. {
  57. if (nvram_as1 != 0)
  58. outb(addr>>8, nvram_as1);
  59. outb(addr, nvram_as0);
  60. outb(val, nvram_data);
  61. return;
  62. }
  63. /*
  64. * Set the hardware clock. -- Cort
  65. */
  66. int chrp_set_rtc_time(struct rtc_time *tmarg)
  67. {
  68. unsigned char save_control, save_freq_select;
  69. struct rtc_time tm = *tmarg;
  70. spin_lock(&rtc_lock);
  71. save_control = chrp_cmos_clock_read(RTC_CONTROL); /* tell the clock it's being set */
  72. chrp_cmos_clock_write((save_control|RTC_SET), RTC_CONTROL);
  73. save_freq_select = chrp_cmos_clock_read(RTC_FREQ_SELECT); /* stop and reset prescaler */
  74. chrp_cmos_clock_write((save_freq_select|RTC_DIV_RESET2), RTC_FREQ_SELECT);
  75. if (!(save_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
  76. BIN_TO_BCD(tm.tm_sec);
  77. BIN_TO_BCD(tm.tm_min);
  78. BIN_TO_BCD(tm.tm_hour);
  79. BIN_TO_BCD(tm.tm_mon);
  80. BIN_TO_BCD(tm.tm_mday);
  81. BIN_TO_BCD(tm.tm_year);
  82. }
  83. chrp_cmos_clock_write(tm.tm_sec,RTC_SECONDS);
  84. chrp_cmos_clock_write(tm.tm_min,RTC_MINUTES);
  85. chrp_cmos_clock_write(tm.tm_hour,RTC_HOURS);
  86. chrp_cmos_clock_write(tm.tm_mon,RTC_MONTH);
  87. chrp_cmos_clock_write(tm.tm_mday,RTC_DAY_OF_MONTH);
  88. chrp_cmos_clock_write(tm.tm_year,RTC_YEAR);
  89. /* The following flags have to be released exactly in this order,
  90. * otherwise the DS12887 (popular MC146818A clone with integrated
  91. * battery and quartz) will not reset the oscillator and will not
  92. * update precisely 500 ms later. You won't find this mentioned in
  93. * the Dallas Semiconductor data sheets, but who believes data
  94. * sheets anyway ... -- Markus Kuhn
  95. */
  96. chrp_cmos_clock_write(save_control, RTC_CONTROL);
  97. chrp_cmos_clock_write(save_freq_select, RTC_FREQ_SELECT);
  98. spin_unlock(&rtc_lock);
  99. return 0;
  100. }
  101. void chrp_get_rtc_time(struct rtc_time *tm)
  102. {
  103. unsigned int year, mon, day, hour, min, sec;
  104. do {
  105. sec = chrp_cmos_clock_read(RTC_SECONDS);
  106. min = chrp_cmos_clock_read(RTC_MINUTES);
  107. hour = chrp_cmos_clock_read(RTC_HOURS);
  108. day = chrp_cmos_clock_read(RTC_DAY_OF_MONTH);
  109. mon = chrp_cmos_clock_read(RTC_MONTH);
  110. year = chrp_cmos_clock_read(RTC_YEAR);
  111. } while (sec != chrp_cmos_clock_read(RTC_SECONDS));
  112. if (!(chrp_cmos_clock_read(RTC_CONTROL) & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
  113. BCD_TO_BIN(sec);
  114. BCD_TO_BIN(min);
  115. BCD_TO_BIN(hour);
  116. BCD_TO_BIN(day);
  117. BCD_TO_BIN(mon);
  118. BCD_TO_BIN(year);
  119. }
  120. if (year < 70)
  121. year += 100;
  122. tm->tm_sec = sec;
  123. tm->tm_min = min;
  124. tm->tm_hour = hour;
  125. tm->tm_mday = day;
  126. tm->tm_mon = mon;
  127. tm->tm_year = year;
  128. }