time.c 4.0 KB

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