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