time.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /*
  2. * arch/ppc/platforms/chrp_time.c
  3. *
  4. * Copyright (C) 1991, 1992, 1995 Linus Torvalds
  5. *
  6. * Adapted for PowerPC (PReP) by Gary Thomas
  7. * Modified by Cort Dougan (cort@cs.nmt.edu).
  8. * Copied and modified from arch/i386/kernel/time.c
  9. *
  10. */
  11. #include <linux/errno.h>
  12. #include <linux/sched.h>
  13. #include <linux/kernel.h>
  14. #include <linux/param.h>
  15. #include <linux/string.h>
  16. #include <linux/mm.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/time.h>
  19. #include <linux/timex.h>
  20. #include <linux/kernel_stat.h>
  21. #include <linux/mc146818rtc.h>
  22. #include <linux/init.h>
  23. #include <linux/bcd.h>
  24. #include <linux/ioport.h>
  25. #include <asm/io.h>
  26. #include <asm/nvram.h>
  27. #include <asm/prom.h>
  28. #include <asm/sections.h>
  29. #include <asm/time.h>
  30. extern spinlock_t rtc_lock;
  31. static int nvram_as1 = NVRAM_AS1;
  32. static int nvram_as0 = NVRAM_AS0;
  33. static int nvram_data = NVRAM_DATA;
  34. long __init chrp_time_init(void)
  35. {
  36. struct device_node *rtcs;
  37. struct resource r;
  38. int base;
  39. rtcs = find_compatible_devices("rtc", "pnpPNP,b00");
  40. if (rtcs == NULL)
  41. rtcs = find_compatible_devices("rtc", "ds1385-rtc");
  42. if (rtcs == NULL || of_address_to_resource(rtcs, 0, &r))
  43. return 0;
  44. base = r.start;
  45. nvram_as1 = 0;
  46. nvram_as0 = base;
  47. nvram_data = base + 1;
  48. return 0;
  49. }
  50. int chrp_cmos_clock_read(int addr)
  51. {
  52. if (nvram_as1 != 0)
  53. outb(addr>>8, nvram_as1);
  54. outb(addr, nvram_as0);
  55. return (inb(nvram_data));
  56. }
  57. void chrp_cmos_clock_write(unsigned long val, int addr)
  58. {
  59. if (nvram_as1 != 0)
  60. outb(addr>>8, nvram_as1);
  61. outb(addr, nvram_as0);
  62. outb(val, nvram_data);
  63. return;
  64. }
  65. /*
  66. * Set the hardware clock. -- Cort
  67. */
  68. int chrp_set_rtc_time(struct rtc_time *tmarg)
  69. {
  70. unsigned char save_control, save_freq_select;
  71. struct rtc_time tm = *tmarg;
  72. spin_lock(&rtc_lock);
  73. save_control = chrp_cmos_clock_read(RTC_CONTROL); /* tell the clock it's being set */
  74. chrp_cmos_clock_write((save_control|RTC_SET), RTC_CONTROL);
  75. save_freq_select = chrp_cmos_clock_read(RTC_FREQ_SELECT); /* stop and reset prescaler */
  76. chrp_cmos_clock_write((save_freq_select|RTC_DIV_RESET2), RTC_FREQ_SELECT);
  77. if (!(save_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
  78. BIN_TO_BCD(tm.tm_sec);
  79. BIN_TO_BCD(tm.tm_min);
  80. BIN_TO_BCD(tm.tm_hour);
  81. BIN_TO_BCD(tm.tm_mon);
  82. BIN_TO_BCD(tm.tm_mday);
  83. BIN_TO_BCD(tm.tm_year);
  84. }
  85. chrp_cmos_clock_write(tm.tm_sec,RTC_SECONDS);
  86. chrp_cmos_clock_write(tm.tm_min,RTC_MINUTES);
  87. chrp_cmos_clock_write(tm.tm_hour,RTC_HOURS);
  88. chrp_cmos_clock_write(tm.tm_mon,RTC_MONTH);
  89. chrp_cmos_clock_write(tm.tm_mday,RTC_DAY_OF_MONTH);
  90. chrp_cmos_clock_write(tm.tm_year,RTC_YEAR);
  91. /* The following flags have to be released exactly in this order,
  92. * otherwise the DS12887 (popular MC146818A clone with integrated
  93. * battery and quartz) will not reset the oscillator and will not
  94. * update precisely 500 ms later. You won't find this mentioned in
  95. * the Dallas Semiconductor data sheets, but who believes data
  96. * sheets anyway ... -- Markus Kuhn
  97. */
  98. chrp_cmos_clock_write(save_control, RTC_CONTROL);
  99. chrp_cmos_clock_write(save_freq_select, RTC_FREQ_SELECT);
  100. spin_unlock(&rtc_lock);
  101. return 0;
  102. }
  103. void chrp_get_rtc_time(struct rtc_time *tm)
  104. {
  105. unsigned int year, mon, day, hour, min, sec;
  106. int uip, i;
  107. /* The Linux interpretation of the CMOS clock register contents:
  108. * When the Update-In-Progress (UIP) flag goes from 1 to 0, the
  109. * RTC registers show the second which has precisely just started.
  110. * Let's hope other operating systems interpret the RTC the same way.
  111. */
  112. /* Since the UIP flag is set for about 2.2 ms and the clock
  113. * is typically written with a precision of 1 jiffy, trying
  114. * to obtain a precision better than a few milliseconds is
  115. * an illusion. Only consistency is interesting, this also
  116. * allows to use the routine for /dev/rtc without a potential
  117. * 1 second kernel busy loop triggered by any reader of /dev/rtc.
  118. */
  119. for ( i = 0; i<1000000; i++) {
  120. uip = chrp_cmos_clock_read(RTC_FREQ_SELECT);
  121. sec = chrp_cmos_clock_read(RTC_SECONDS);
  122. min = chrp_cmos_clock_read(RTC_MINUTES);
  123. hour = chrp_cmos_clock_read(RTC_HOURS);
  124. day = chrp_cmos_clock_read(RTC_DAY_OF_MONTH);
  125. mon = chrp_cmos_clock_read(RTC_MONTH);
  126. year = chrp_cmos_clock_read(RTC_YEAR);
  127. uip |= chrp_cmos_clock_read(RTC_FREQ_SELECT);
  128. if ((uip & RTC_UIP)==0) break;
  129. }
  130. if (!(chrp_cmos_clock_read(RTC_CONTROL) & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
  131. BCD_TO_BIN(sec);
  132. BCD_TO_BIN(min);
  133. BCD_TO_BIN(hour);
  134. BCD_TO_BIN(day);
  135. BCD_TO_BIN(mon);
  136. BCD_TO_BIN(year);
  137. }
  138. if (year < 70)
  139. year += 100;
  140. tm->tm_sec = sec;
  141. tm->tm_min = min;
  142. tm->tm_hour = hour;
  143. tm->tm_mday = day;
  144. tm->tm_mon = mon;
  145. tm->tm_year = year;
  146. }
  147. void __init chrp_calibrate_decr(void)
  148. {
  149. struct device_node *cpu;
  150. unsigned int freq, *fp;
  151. /*
  152. * The cpu node should have a timebase-frequency property
  153. * to tell us the rate at which the decrementer counts.
  154. */
  155. freq = 16666000; /* hardcoded default */
  156. cpu = find_type_devices("cpu");
  157. if (cpu != 0) {
  158. fp = (unsigned int *)
  159. get_property(cpu, "timebase-frequency", NULL);
  160. if (fp != 0)
  161. freq = *fp;
  162. }
  163. ppc_tb_freq = freq;
  164. }