/arch/ppc64/kernel/maple_time.c

https://bitbucket.org/evzijst/gittest · C · 226 lines · 154 code · 36 blank · 36 comment · 18 complexity · 2fd7acae66bb681796170f75be3ca6e9 MD5 · raw file

  1. /*
  2. * arch/ppc64/kernel/maple_time.c
  3. *
  4. * (c) Copyright 2004 Benjamin Herrenschmidt (benh@kernel.crashing.org),
  5. * IBM Corp.
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version
  10. * 2 of the License, or (at your option) any later version.
  11. *
  12. */
  13. #undef DEBUG
  14. #include <linux/config.h>
  15. #include <linux/errno.h>
  16. #include <linux/sched.h>
  17. #include <linux/kernel.h>
  18. #include <linux/param.h>
  19. #include <linux/string.h>
  20. #include <linux/mm.h>
  21. #include <linux/init.h>
  22. #include <linux/time.h>
  23. #include <linux/adb.h>
  24. #include <linux/pmu.h>
  25. #include <linux/interrupt.h>
  26. #include <linux/mc146818rtc.h>
  27. #include <linux/bcd.h>
  28. #include <asm/sections.h>
  29. #include <asm/prom.h>
  30. #include <asm/system.h>
  31. #include <asm/io.h>
  32. #include <asm/pgtable.h>
  33. #include <asm/machdep.h>
  34. #include <asm/time.h>
  35. #ifdef DEBUG
  36. #define DBG(x...) printk(x)
  37. #else
  38. #define DBG(x...)
  39. #endif
  40. extern void setup_default_decr(void);
  41. extern void GregorianDay(struct rtc_time * tm);
  42. extern unsigned long ppc_tb_freq;
  43. extern unsigned long ppc_proc_freq;
  44. static int maple_rtc_addr;
  45. static int maple_clock_read(int addr)
  46. {
  47. outb_p(addr, maple_rtc_addr);
  48. return inb_p(maple_rtc_addr+1);
  49. }
  50. static void maple_clock_write(unsigned long val, int addr)
  51. {
  52. outb_p(addr, maple_rtc_addr);
  53. outb_p(val, maple_rtc_addr+1);
  54. }
  55. void maple_get_rtc_time(struct rtc_time *tm)
  56. {
  57. int uip, i;
  58. /* The Linux interpretation of the CMOS clock register contents:
  59. * When the Update-In-Progress (UIP) flag goes from 1 to 0, the
  60. * RTC registers show the second which has precisely just started.
  61. * Let's hope other operating systems interpret the RTC the same way.
  62. */
  63. /* Since the UIP flag is set for about 2.2 ms and the clock
  64. * is typically written with a precision of 1 jiffy, trying
  65. * to obtain a precision better than a few milliseconds is
  66. * an illusion. Only consistency is interesting, this also
  67. * allows to use the routine for /dev/rtc without a potential
  68. * 1 second kernel busy loop triggered by any reader of /dev/rtc.
  69. */
  70. for (i = 0; i<1000000; i++) {
  71. uip = maple_clock_read(RTC_FREQ_SELECT);
  72. tm->tm_sec = maple_clock_read(RTC_SECONDS);
  73. tm->tm_min = maple_clock_read(RTC_MINUTES);
  74. tm->tm_hour = maple_clock_read(RTC_HOURS);
  75. tm->tm_mday = maple_clock_read(RTC_DAY_OF_MONTH);
  76. tm->tm_mon = maple_clock_read(RTC_MONTH);
  77. tm->tm_year = maple_clock_read(RTC_YEAR);
  78. uip |= maple_clock_read(RTC_FREQ_SELECT);
  79. if ((uip & RTC_UIP)==0)
  80. break;
  81. }
  82. if (!(maple_clock_read(RTC_CONTROL) & RTC_DM_BINARY)
  83. || RTC_ALWAYS_BCD) {
  84. BCD_TO_BIN(tm->tm_sec);
  85. BCD_TO_BIN(tm->tm_min);
  86. BCD_TO_BIN(tm->tm_hour);
  87. BCD_TO_BIN(tm->tm_mday);
  88. BCD_TO_BIN(tm->tm_mon);
  89. BCD_TO_BIN(tm->tm_year);
  90. }
  91. if ((tm->tm_year + 1900) < 1970)
  92. tm->tm_year += 100;
  93. GregorianDay(tm);
  94. }
  95. int maple_set_rtc_time(struct rtc_time *tm)
  96. {
  97. unsigned char save_control, save_freq_select;
  98. int sec, min, hour, mon, mday, year;
  99. spin_lock(&rtc_lock);
  100. save_control = maple_clock_read(RTC_CONTROL); /* tell the clock it's being set */
  101. maple_clock_write((save_control|RTC_SET), RTC_CONTROL);
  102. save_freq_select = maple_clock_read(RTC_FREQ_SELECT); /* stop and reset prescaler */
  103. maple_clock_write((save_freq_select|RTC_DIV_RESET2), RTC_FREQ_SELECT);
  104. sec = tm->tm_sec;
  105. min = tm->tm_min;
  106. hour = tm->tm_hour;
  107. mon = tm->tm_mon;
  108. mday = tm->tm_mday;
  109. year = tm->tm_year;
  110. if (!(save_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
  111. BIN_TO_BCD(sec);
  112. BIN_TO_BCD(min);
  113. BIN_TO_BCD(hour);
  114. BIN_TO_BCD(mon);
  115. BIN_TO_BCD(mday);
  116. BIN_TO_BCD(year);
  117. }
  118. maple_clock_write(sec, RTC_SECONDS);
  119. maple_clock_write(min, RTC_MINUTES);
  120. maple_clock_write(hour, RTC_HOURS);
  121. maple_clock_write(mon, RTC_MONTH);
  122. maple_clock_write(mday, RTC_DAY_OF_MONTH);
  123. maple_clock_write(year, RTC_YEAR);
  124. /* The following flags have to be released exactly in this order,
  125. * otherwise the DS12887 (popular MC146818A clone with integrated
  126. * battery and quartz) will not reset the oscillator and will not
  127. * update precisely 500 ms later. You won't find this mentioned in
  128. * the Dallas Semiconductor data sheets, but who believes data
  129. * sheets anyway ... -- Markus Kuhn
  130. */
  131. maple_clock_write(save_control, RTC_CONTROL);
  132. maple_clock_write(save_freq_select, RTC_FREQ_SELECT);
  133. spin_unlock(&rtc_lock);
  134. return 0;
  135. }
  136. void __init maple_get_boot_time(struct rtc_time *tm)
  137. {
  138. struct device_node *rtcs;
  139. rtcs = find_compatible_devices("rtc", "pnpPNP,b00");
  140. if (rtcs && rtcs->addrs) {
  141. maple_rtc_addr = rtcs->addrs[0].address;
  142. printk(KERN_INFO "Maple: Found RTC at 0x%x\n", maple_rtc_addr);
  143. } else {
  144. maple_rtc_addr = RTC_PORT(0); /* legacy address */
  145. printk(KERN_INFO "Maple: No device node for RTC, assuming "
  146. "legacy address (0x%x)\n", maple_rtc_addr);
  147. }
  148. maple_get_rtc_time(tm);
  149. }
  150. /* XXX FIXME: Some sane defaults: 125 MHz timebase, 1GHz processor */
  151. #define DEFAULT_TB_FREQ 125000000UL
  152. #define DEFAULT_PROC_FREQ (DEFAULT_TB_FREQ * 8)
  153. void __init maple_calibrate_decr(void)
  154. {
  155. struct device_node *cpu;
  156. struct div_result divres;
  157. unsigned int *fp = NULL;
  158. /*
  159. * The cpu node should have a timebase-frequency property
  160. * to tell us the rate at which the decrementer counts.
  161. */
  162. cpu = of_find_node_by_type(NULL, "cpu");
  163. ppc_tb_freq = DEFAULT_TB_FREQ;
  164. if (cpu != 0)
  165. fp = (unsigned int *)get_property(cpu, "timebase-frequency", NULL);
  166. if (fp != NULL)
  167. ppc_tb_freq = *fp;
  168. else
  169. printk(KERN_ERR "WARNING: Estimating decrementer frequency (not found)\n");
  170. fp = NULL;
  171. ppc_proc_freq = DEFAULT_PROC_FREQ;
  172. if (cpu != 0)
  173. fp = (unsigned int *)get_property(cpu, "clock-frequency", NULL);
  174. if (fp != NULL)
  175. ppc_proc_freq = *fp;
  176. else
  177. printk(KERN_ERR "WARNING: Estimating processor frequency (not found)\n");
  178. of_node_put(cpu);
  179. printk(KERN_INFO "time_init: decrementer frequency = %lu.%.6lu MHz\n",
  180. ppc_tb_freq/1000000, ppc_tb_freq%1000000);
  181. printk(KERN_INFO "time_init: processor frequency = %lu.%.6lu MHz\n",
  182. ppc_proc_freq/1000000, ppc_proc_freq%1000000);
  183. tb_ticks_per_jiffy = ppc_tb_freq / HZ;
  184. tb_ticks_per_sec = tb_ticks_per_jiffy * HZ;
  185. tb_ticks_per_usec = ppc_tb_freq / 1000000;
  186. tb_to_us = mulhwu_scale_factor(ppc_tb_freq, 1000000);
  187. div128_by_32(1024*1024, 0, tb_ticks_per_sec, &divres);
  188. tb_to_xs = divres.result_low;
  189. setup_default_decr();
  190. }