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