fix to allow usb modules to compile
[linux-2.4.21-pre4.git] / arch / ppc / platforms / pmac_time.c
1 /*
2  * BK Id: SCCS/s.pmac_time.c 1.26 12/18/02 00:17:27 benh
3  */
4 /*
5  * Support for periodic interrupts (100 per second) and for getting
6  * the current time from the RTC on Power Macintoshes.
7  *
8  * We use the decrementer register for our periodic interrupts.
9  *
10  * Paul Mackerras       August 1996.
11  * Copyright (C) 1996 Paul Mackerras.
12  */
13 #include <linux/config.h>
14 #include <linux/errno.h>
15 #include <linux/sched.h>
16 #include <linux/kernel.h>
17 #include <linux/param.h>
18 #include <linux/string.h>
19 #include <linux/mm.h>
20 #include <linux/init.h>
21 #include <linux/adb.h>
22 #include <linux/cuda.h>
23 #include <linux/pmu.h>
24
25 #include <asm/sections.h>
26 #include <asm/prom.h>
27 #include <asm/system.h>
28 #include <asm/io.h>
29 #include <asm/pgtable.h>
30 #include <asm/machdep.h>
31 #include <asm/hardirq.h>
32 #include <asm/time.h>
33 #include <asm/nvram.h>
34
35 extern rwlock_t xtime_lock;
36
37 /* Apparently the RTC stores seconds since 1 Jan 1904 */
38 #define RTC_OFFSET      2082844800
39
40 /*
41  * Calibrate the decrementer frequency with the VIA timer 1.
42  */
43 #define VIA_TIMER_FREQ_6        4700000 /* time 1 frequency * 6 */
44
45 /* VIA registers */
46 #define RS              0x200           /* skip between registers */
47 #define T1CL            (4*RS)          /* Timer 1 ctr/latch (low 8 bits) */
48 #define T1CH            (5*RS)          /* Timer 1 counter (high 8 bits) */
49 #define T1LL            (6*RS)          /* Timer 1 latch (low 8 bits) */
50 #define T1LH            (7*RS)          /* Timer 1 latch (high 8 bits) */
51 #define ACR             (11*RS)         /* Auxiliary control register */
52 #define IFR             (13*RS)         /* Interrupt flag register */
53
54 /* Bits in ACR */
55 #define T1MODE          0xc0            /* Timer 1 mode */
56 #define T1MODE_CONT     0x40            /*  continuous interrupts */
57
58 /* Bits in IFR and IER */
59 #define T1_INT          0x40            /* Timer 1 interrupt */
60
61 extern struct timezone sys_tz;
62
63 long __init
64 pmac_time_init(void)
65 {
66 #ifdef CONFIG_NVRAM
67         s32 delta = 0;
68         int dst;
69         
70         delta = ((s32)pmac_xpram_read(PMAC_XPRAM_MACHINE_LOC + 0x9)) << 16;
71         delta |= ((s32)pmac_xpram_read(PMAC_XPRAM_MACHINE_LOC + 0xa)) << 8;
72         delta |= pmac_xpram_read(PMAC_XPRAM_MACHINE_LOC + 0xb);
73         if (delta & 0x00800000UL)
74                 delta |= 0xFF000000UL;
75         dst = ((pmac_xpram_read(PMAC_XPRAM_MACHINE_LOC + 0x8) & 0x80) != 0);
76         printk("GMT Delta read from XPRAM: %d minutes, DST: %s\n", delta/60,
77                 dst ? "on" : "off");
78         return delta;
79 #else
80         return 0;
81 #endif
82 }
83
84 unsigned long __pmac
85 pmac_get_rtc_time(void)
86 {
87 #if defined(CONFIG_ADB_CUDA) || defined(CONFIG_ADB_PMU)
88         struct adb_request req;
89         unsigned long now;
90 #endif
91
92         /* Get the time from the RTC */
93         switch (sys_ctrler) {
94 #ifdef CONFIG_ADB_CUDA
95         case SYS_CTRLER_CUDA:
96                 if (cuda_request(&req, NULL, 2, CUDA_PACKET, CUDA_GET_TIME) < 0)
97                         return 0;
98                 while (!req.complete)
99                         cuda_poll();
100                 if (req.reply_len != 7)
101                         printk(KERN_ERR "pmac_get_rtc_time: got %d byte reply\n",
102                                req.reply_len);
103                 now = (req.reply[3] << 24) + (req.reply[4] << 16)
104                         + (req.reply[5] << 8) + req.reply[6];
105                 return now - RTC_OFFSET;
106 #endif /* CONFIG_ADB_CUDA */
107 #ifdef CONFIG_ADB_PMU
108         case SYS_CTRLER_PMU:
109                 if (pmu_request(&req, NULL, 1, PMU_READ_RTC) < 0)
110                         return 0;
111                 while (!req.complete)
112                         pmu_poll();
113                 if (req.reply_len != 4)
114                         printk(KERN_ERR "pmac_get_rtc_time: got %d byte reply\n",
115                                req.reply_len);
116                 now = (req.reply[0] << 24) + (req.reply[1] << 16)
117                         + (req.reply[2] << 8) + req.reply[3];
118                 return now - RTC_OFFSET;
119 #endif /* CONFIG_ADB_PMU */
120         default: ;
121         }
122         return 0;
123 }
124
125 int __pmac
126 pmac_set_rtc_time(unsigned long nowtime)
127 {
128 #if defined(CONFIG_ADB_CUDA) || defined(CONFIG_ADB_PMU)
129         struct adb_request req;
130 #endif
131
132         nowtime += RTC_OFFSET;
133
134         switch (sys_ctrler) {
135 #ifdef CONFIG_ADB_CUDA
136         case SYS_CTRLER_CUDA:
137                 if (cuda_request(&req, NULL, 6, CUDA_PACKET, CUDA_SET_TIME,
138                                  nowtime >> 24, nowtime >> 16, nowtime >> 8, nowtime) < 0)
139                         return 0;
140                 while (!req.complete)
141                         cuda_poll();
142                 if ((req.reply_len != 3) && (req.reply_len != 7))
143                         printk(KERN_ERR "pmac_set_rtc_time: got %d byte reply\n",
144                                req.reply_len);
145                 return 1;
146 #endif /* CONFIG_ADB_CUDA */
147 #ifdef CONFIG_ADB_PMU
148         case SYS_CTRLER_PMU:
149                 if (pmu_request(&req, NULL, 5, PMU_SET_RTC,
150                                 nowtime >> 24, nowtime >> 16, nowtime >> 8, nowtime) < 0)
151                         return 0;
152                 while (!req.complete)
153                         pmu_poll();
154                 if (req.reply_len != 0)
155                         printk(KERN_ERR "pmac_set_rtc_time: got %d byte reply\n",
156                                req.reply_len);
157                 return 1;
158 #endif /* CONFIG_ADB_PMU */
159         default:
160                 return 0;
161         }
162 }
163
164 /*
165  * Calibrate the decrementer register using VIA timer 1.
166  * This is used both on powermacs and CHRP machines.
167  */
168 int __init
169 via_calibrate_decr(void)
170 {
171         struct device_node *vias;
172         volatile unsigned char *via;
173         int count = VIA_TIMER_FREQ_6 / HZ;
174         unsigned int dstart, dend;
175
176         vias = find_devices("via-cuda");
177         if (vias == 0)
178                 vias = find_devices("via-pmu");
179         if (vias == 0)
180                 vias = find_devices("via");
181         if (vias == 0 || vias->n_addrs == 0)
182                 return 0;
183         via = (volatile unsigned char *)
184                 ioremap(vias->addrs[0].address, vias->addrs[0].size);
185
186         /* set timer 1 for continuous interrupts */
187         out_8(&via[ACR], (via[ACR] & ~T1MODE) | T1MODE_CONT);
188         /* set the counter to a small value */
189         out_8(&via[T1CH], 2);
190         /* set the latch to `count' */
191         out_8(&via[T1LL], count);
192         out_8(&via[T1LH], count >> 8);
193         /* wait until it hits 0 */
194         while ((in_8(&via[IFR]) & T1_INT) == 0)
195                 ;
196         dstart = get_dec();
197         /* clear the interrupt & wait until it hits 0 again */
198         in_8(&via[T1CL]);
199         while ((in_8(&via[IFR]) & T1_INT) == 0)
200                 ;
201         dend = get_dec();
202
203         tb_ticks_per_jiffy = (dstart - dend) / 6;
204         tb_to_us = mulhwu_scale_factor(dstart - dend, 60000);
205
206         printk(KERN_INFO "via_calibrate_decr: ticks per jiffy = %u (%u ticks)\n",
207                tb_ticks_per_jiffy, dstart - dend);
208
209         iounmap((void *)via);
210         
211         return 1;
212 }
213
214 #ifdef CONFIG_PMAC_PBOOK
215 /*
216  * Reset the time after a sleep.
217  */
218 static int __pmac
219 time_sleep_notify(struct pmu_sleep_notifier *self, int when)
220 {
221         static unsigned long time_diff;
222         unsigned long flags;
223
224         switch (when) {
225         case PBOOK_SLEEP_NOW:
226                 read_lock_irqsave(&xtime_lock, flags);
227                 time_diff = xtime.tv_sec - pmac_get_rtc_time();
228                 read_unlock_irqrestore(&xtime_lock, flags);
229                 break;
230         case PBOOK_WAKE:
231                 write_lock_irqsave(&xtime_lock, flags);
232                 xtime.tv_sec = pmac_get_rtc_time() + time_diff;
233                 xtime.tv_usec = 0;
234                 last_rtc_update = xtime.tv_sec;
235                 write_unlock_irqrestore(&xtime_lock, flags);
236                 break;
237         }
238         return PBOOK_SLEEP_OK;
239 }
240
241 static struct pmu_sleep_notifier time_sleep_notifier __pmacdata = {
242         time_sleep_notify, SLEEP_LEVEL_MISC,
243 };
244 #endif /* CONFIG_PMAC_PBOOK */
245
246 /*
247  * Query the OF and get the decr frequency.
248  * This was taken from the pmac time_init() when merging the prep/pmac
249  * time functions.
250  */
251 void __init
252 pmac_calibrate_decr(void)
253 {
254         struct device_node *cpu;
255         unsigned int freq, *fp;
256
257 #ifdef CONFIG_PMAC_PBOOK
258         pmu_register_sleep_notifier(&time_sleep_notifier);
259 #endif /* CONFIG_PMAC_PBOOK */
260
261         /* We assume MacRISC2 machines have correct device-tree
262          * calibration. That's better since the VIA itself seems
263          * to be slightly off. --BenH
264          */
265         if (!machine_is_compatible("MacRISC2"))
266                 if (via_calibrate_decr())
267                         return;
268
269         /*
270          * The cpu node should have a timebase-frequency property
271          * to tell us the rate at which the decrementer counts.
272          */
273         cpu = find_type_devices("cpu");
274         if (cpu == 0)
275                 panic("can't find cpu node in time_init");
276         fp = (unsigned int *) get_property(cpu, "timebase-frequency", NULL);
277         if (fp == 0)
278                 panic("can't get cpu timebase frequency");
279         freq = *fp;
280         printk("time_init: decrementer frequency = %u.%.6u MHz\n",
281                freq/1000000, freq%1000000);
282         tb_ticks_per_jiffy = freq / HZ;
283         tb_to_us = mulhwu_scale_factor(freq, 1000000);
284 }