chrp_time.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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 <asm/segment.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. int base;
  38. rtcs = find_compatible_devices("rtc", "pnpPNP,b00");
  39. if (rtcs == NULL)
  40. rtcs = find_compatible_devices("rtc", "ds1385-rtc");
  41. if (rtcs == NULL || rtcs->addrs == NULL)
  42. return 0;
  43. base = rtcs->addrs[0].address;
  44. nvram_as1 = 0;
  45. nvram_as0 = base;
  46. nvram_data = base + 1;
  47. return 0;
  48. }
  49. int __chrp chrp_cmos_clock_read(int addr)
  50. {
  51. if (nvram_as1 != 0)
  52. outb(addr>>8, nvram_as1);
  53. outb(addr, nvram_as0);
  54. return (inb(nvram_data));
  55. }
  56. void __chrp chrp_cmos_clock_write(unsigned long val, int addr)
  57. {
  58. if (nvram_as1 != 0)
  59. outb(addr>>8, nvram_as1);
  60. outb(addr, nvram_as0);
  61. outb(val, nvram_data);
  62. return;
  63. }
  64. /*
  65. * Set the hardware clock. -- Cort
  66. */
  67. int __chrp chrp_set_rtc_time(unsigned long nowtime)
  68. {
  69. unsigned char save_control, save_freq_select;
  70. struct rtc_time tm;
  71. spin_lock(&rtc_lock);
  72. to_tm(nowtime, &tm);
  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. tm.tm_year -= 1900;
  78. if (!(save_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
  79. BIN_TO_BCD(tm.tm_sec);
  80. BIN_TO_BCD(tm.tm_min);
  81. BIN_TO_BCD(tm.tm_hour);
  82. BIN_TO_BCD(tm.tm_mon);
  83. BIN_TO_BCD(tm.tm_mday);
  84. BIN_TO_BCD(tm.tm_year);
  85. }
  86. chrp_cmos_clock_write(tm.tm_sec,RTC_SECONDS);
  87. chrp_cmos_clock_write(tm.tm_min,RTC_MINUTES);
  88. chrp_cmos_clock_write(tm.tm_hour,RTC_HOURS);
  89. chrp_cmos_clock_write(tm.tm_mon,RTC_MONTH);
  90. chrp_cmos_clock_write(tm.tm_mday,RTC_DAY_OF_MONTH);
  91. chrp_cmos_clock_write(tm.tm_year,RTC_YEAR);
  92. /* The following flags have to be released exactly in this order,
  93. * otherwise the DS12887 (popular MC146818A clone with integrated
  94. * battery and quartz) will not reset the oscillator and will not
  95. * update precisely 500 ms later. You won't find this mentioned in
  96. * the Dallas Semiconductor data sheets, but who believes data
  97. * sheets anyway ... -- Markus Kuhn
  98. */
  99. chrp_cmos_clock_write(save_control, RTC_CONTROL);
  100. chrp_cmos_clock_write(save_freq_select, RTC_FREQ_SELECT);
  101. spin_unlock(&rtc_lock);
  102. return 0;
  103. }
  104. unsigned long __chrp chrp_get_rtc_time(void)
  105. {
  106. unsigned int year, mon, day, hour, min, sec;
  107. int uip, i;
  108. /* The Linux interpretation of the CMOS clock register contents:
  109. * When the Update-In-Progress (UIP) flag goes from 1 to 0, the
  110. * RTC registers show the second which has precisely just started.
  111. * Let's hope other operating systems interpret the RTC the same way.
  112. */
  113. /* Since the UIP flag is set for about 2.2 ms and the clock
  114. * is typically written with a precision of 1 jiffy, trying
  115. * to obtain a precision better than a few milliseconds is
  116. * an illusion. Only consistency is interesting, this also
  117. * allows to use the routine for /dev/rtc without a potential
  118. * 1 second kernel busy loop triggered by any reader of /dev/rtc.
  119. */
  120. for ( i = 0; i<1000000; i++) {
  121. uip = chrp_cmos_clock_read(RTC_FREQ_SELECT);
  122. sec = chrp_cmos_clock_read(RTC_SECONDS);
  123. min = chrp_cmos_clock_read(RTC_MINUTES);
  124. hour = chrp_cmos_clock_read(RTC_HOURS);
  125. day = chrp_cmos_clock_read(RTC_DAY_OF_MONTH);
  126. mon = chrp_cmos_clock_read(RTC_MONTH);
  127. year = chrp_cmos_clock_read(RTC_YEAR);
  128. uip |= chrp_cmos_clock_read(RTC_FREQ_SELECT);
  129. if ((uip & RTC_UIP)==0) break;
  130. }
  131. if (!(chrp_cmos_clock_read(RTC_CONTROL) & RTC_DM_BINARY) || RTC_ALWAYS_BCD)
  132. {
  133. BCD_TO_BIN(sec);
  134. BCD_TO_BIN(min);
  135. BCD_TO_BIN(hour);
  136. BCD_TO_BIN(day);
  137. BCD_TO_BIN(mon);
  138. BCD_TO_BIN(year);
  139. }
  140. if ((year += 1900) < 1970)
  141. year += 100;
  142. return mktime(year, mon, day, hour, min, sec);
  143. }
  144. void __init chrp_calibrate_decr(void)
  145. {
  146. struct device_node *cpu;
  147. unsigned int freq, *fp;
  148. if (via_calibrate_decr())
  149. return;
  150. /*
  151. * The cpu node should have a timebase-frequency property
  152. * to tell us the rate at which the decrementer counts.
  153. */
  154. freq = 16666000; /* hardcoded default */
  155. cpu = find_type_devices("cpu");
  156. if (cpu != 0) {
  157. fp = (unsigned int *)
  158. get_property(cpu, "timebase-frequency", NULL);
  159. if (fp != 0)
  160. freq = *fp;
  161. }
  162. printk("time_init: decrementer frequency = %u.%.6u MHz\n",
  163. freq/1000000, freq%1000000);
  164. tb_ticks_per_jiffy = freq / HZ;
  165. tb_to_us = mulhwu_scale_factor(freq, 1000000);
  166. }