[PATCH] libata: fix ata_qc_issue() error handling
[linux-2.6/suspend2-2.6.18.git] / arch / powerpc / platforms / chrp / time.c
blob36a0f97bb7b13143dee6bf88cf1818bbb95f446f
1 /*
2 * arch/ppc/platforms/chrp_time.c
4 * Copyright (C) 1991, 1992, 1995 Linus Torvalds
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
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 <linux/ioport.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>
32 extern spinlock_t rtc_lock;
34 static int nvram_as1 = NVRAM_AS1;
35 static int nvram_as0 = NVRAM_AS0;
36 static int nvram_data = NVRAM_DATA;
38 long __init chrp_time_init(void)
40 struct device_node *rtcs;
41 struct resource r;
42 int base;
44 rtcs = find_compatible_devices("rtc", "pnpPNP,b00");
45 if (rtcs == NULL)
46 rtcs = find_compatible_devices("rtc", "ds1385-rtc");
47 if (rtcs == NULL || of_address_to_resource(rtcs, 0, &r))
48 return 0;
50 base = r.start;
51 nvram_as1 = 0;
52 nvram_as0 = base;
53 nvram_data = base + 1;
55 return 0;
58 int chrp_cmos_clock_read(int addr)
60 if (nvram_as1 != 0)
61 outb(addr>>8, nvram_as1);
62 outb(addr, nvram_as0);
63 return (inb(nvram_data));
66 void chrp_cmos_clock_write(unsigned long val, int addr)
68 if (nvram_as1 != 0)
69 outb(addr>>8, nvram_as1);
70 outb(addr, nvram_as0);
71 outb(val, nvram_data);
72 return;
76 * Set the hardware clock. -- Cort
78 int chrp_set_rtc_time(struct rtc_time *tmarg)
80 unsigned char save_control, save_freq_select;
81 struct rtc_time tm = *tmarg;
83 spin_lock(&rtc_lock);
85 save_control = chrp_cmos_clock_read(RTC_CONTROL); /* tell the clock it's being set */
87 chrp_cmos_clock_write((save_control|RTC_SET), RTC_CONTROL);
89 save_freq_select = chrp_cmos_clock_read(RTC_FREQ_SELECT); /* stop and reset prescaler */
91 chrp_cmos_clock_write((save_freq_select|RTC_DIV_RESET2), RTC_FREQ_SELECT);
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);
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);
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
115 chrp_cmos_clock_write(save_control, RTC_CONTROL);
116 chrp_cmos_clock_write(save_freq_select, RTC_FREQ_SELECT);
118 spin_unlock(&rtc_lock);
119 return 0;
122 void chrp_get_rtc_time(struct rtc_time *tm)
124 unsigned int year, mon, day, hour, min, sec;
125 int uip, i;
127 /* The Linux interpretation of the CMOS clock register contents:
128 * When the Update-In-Progress (UIP) flag goes from 1 to 0, the
129 * RTC registers show the second which has precisely just started.
130 * Let's hope other operating systems interpret the RTC the same way.
133 /* Since the UIP flag is set for about 2.2 ms and the clock
134 * is typically written with a precision of 1 jiffy, trying
135 * to obtain a precision better than a few milliseconds is
136 * an illusion. Only consistency is interesting, this also
137 * allows to use the routine for /dev/rtc without a potential
138 * 1 second kernel busy loop triggered by any reader of /dev/rtc.
141 for ( i = 0; i<1000000; i++) {
142 uip = chrp_cmos_clock_read(RTC_FREQ_SELECT);
143 sec = chrp_cmos_clock_read(RTC_SECONDS);
144 min = chrp_cmos_clock_read(RTC_MINUTES);
145 hour = chrp_cmos_clock_read(RTC_HOURS);
146 day = chrp_cmos_clock_read(RTC_DAY_OF_MONTH);
147 mon = chrp_cmos_clock_read(RTC_MONTH);
148 year = chrp_cmos_clock_read(RTC_YEAR);
149 uip |= chrp_cmos_clock_read(RTC_FREQ_SELECT);
150 if ((uip & RTC_UIP)==0) break;
153 if (!(chrp_cmos_clock_read(RTC_CONTROL) & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
154 BCD_TO_BIN(sec);
155 BCD_TO_BIN(min);
156 BCD_TO_BIN(hour);
157 BCD_TO_BIN(day);
158 BCD_TO_BIN(mon);
159 BCD_TO_BIN(year);
161 if (year < 70)
162 year += 100;
163 tm->tm_sec = sec;
164 tm->tm_min = min;
165 tm->tm_hour = hour;
166 tm->tm_mday = day;
167 tm->tm_mon = mon;
168 tm->tm_year = year;
172 void __init chrp_calibrate_decr(void)
174 struct device_node *cpu;
175 unsigned int freq, *fp;
178 * The cpu node should have a timebase-frequency property
179 * to tell us the rate at which the decrementer counts.
181 freq = 16666000; /* hardcoded default */
182 cpu = find_type_devices("cpu");
183 if (cpu != 0) {
184 fp = (unsigned int *)
185 get_property(cpu, "timebase-frequency", NULL);
186 if (fp != 0)
187 freq = *fp;
189 ppc_tb_freq = freq;