chrp_time.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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/io.h>
  25. #include <asm/nvram.h>
  26. #include <asm/prom.h>
  27. #include <asm/sections.h>
  28. #include <asm/time.h>
  29. extern spinlock_t rtc_lock;
  30. static int nvram_as1 = NVRAM_AS1;
  31. static int nvram_as0 = NVRAM_AS0;
  32. static int nvram_data = NVRAM_DATA;
  33. long __init chrp_time_init(void)
  34. {
  35. struct device_node *rtcs;
  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 || rtcs->addrs == NULL)
  41. return 0;
  42. base = rtcs->addrs[0].address;
  43. nvram_as1 = 0;
  44. nvram_as0 = base;
  45. nvram_data = base + 1;
  46. return 0;
  47. }
  48. int __chrp 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 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 chrp_set_rtc_time(unsigned long nowtime)
  67. {
  68. unsigned char save_control, save_freq_select;
  69. struct rtc_time tm;
  70. spin_lock(&rtc_lock);
  71. to_tm(nowtime, &tm);
  72. save_control = chrp_cmos_clock_read(RTC_CONTROL); /* tell the clock it's being set */
  73. chrp_cmos_clock_write((save_control|RTC_SET), RTC_CONTROL);
  74. save_freq_select = chrp_cmos_clock_read(RTC_FREQ_SELECT); /* stop and reset prescaler */
  75. chrp_cmos_clock_write((save_freq_select|RTC_DIV_RESET2), RTC_FREQ_SELECT);
  76. tm.tm_year -= 1900;
  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. unsigned long __chrp chrp_get_rtc_time(void)
  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. {
  132. BCD_TO_BIN(sec);
  133. BCD_TO_BIN(min);
  134. BCD_TO_BIN(hour);
  135. BCD_TO_BIN(day);
  136. BCD_TO_BIN(mon);
  137. BCD_TO_BIN(year);
  138. }
  139. if ((year += 1900) < 1970)
  140. year += 100;
  141. return mktime(year, mon, day, hour, min, sec);
  142. }
  143. void __init chrp_calibrate_decr(void)
  144. {
  145. struct device_node *cpu;
  146. unsigned int freq, *fp;
  147. if (via_calibrate_decr())
  148. return;
  149. /*
  150. * The cpu node should have a timebase-frequency property
  151. * to tell us the rate at which the decrementer counts.
  152. */
  153. freq = 16666000; /* hardcoded default */
  154. cpu = find_type_devices("cpu");
  155. if (cpu != 0) {
  156. fp = (unsigned int *)
  157. get_property(cpu, "timebase-frequency", NULL);
  158. if (fp != 0)
  159. freq = *fp;
  160. }
  161. printk("time_init: decrementer frequency = %u.%.6u MHz\n",
  162. freq/1000000, freq%1000000);
  163. tb_ticks_per_jiffy = freq / HZ;
  164. tb_to_us = mulhwu_scale_factor(freq, 1000000);
  165. }