Pull altix-fpga-reset into release branch
[sfrench/cifs-2.6.git] / arch / ppc64 / kernel / pmac_time.c
1 /*
2  * Support for periodic interrupts (100 per second) and for getting
3  * the current time from the RTC on Power Macintoshes.
4  *
5  * We use the decrementer register for our periodic interrupts.
6  *
7  * Paul Mackerras       August 1996.
8  * Copyright (C) 1996 Paul Mackerras.
9  * Copyright (C) 2003-2005 Benjamin Herrenschmidt.
10  *
11  */
12 #include <linux/config.h>
13 #include <linux/errno.h>
14 #include <linux/sched.h>
15 #include <linux/kernel.h>
16 #include <linux/param.h>
17 #include <linux/string.h>
18 #include <linux/mm.h>
19 #include <linux/init.h>
20 #include <linux/time.h>
21 #include <linux/adb.h>
22 #include <linux/pmu.h>
23 #include <linux/interrupt.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/time.h>
32 #include <asm/nvram.h>
33 #include <asm/smu.h>
34
35 #undef DEBUG
36
37 #ifdef DEBUG
38 #define DBG(x...) printk(x)
39 #else
40 #define DBG(x...)
41 #endif
42
43 /* Apparently the RTC stores seconds since 1 Jan 1904 */
44 #define RTC_OFFSET      2082844800
45
46 /*
47  * Calibrate the decrementer frequency with the VIA timer 1.
48  */
49 #define VIA_TIMER_FREQ_6        4700000 /* time 1 frequency * 6 */
50
51 extern struct timezone sys_tz;
52 extern void to_tm(int tim, struct rtc_time * tm);
53
54 void __pmac pmac_get_rtc_time(struct rtc_time *tm)
55 {
56         switch(sys_ctrler) {
57 #ifdef CONFIG_ADB_PMU
58         case SYS_CTRLER_PMU: {
59                 /* TODO: Move that to a function in the PMU driver */
60                 struct adb_request req;
61                 unsigned int now;
62
63                 if (pmu_request(&req, NULL, 1, PMU_READ_RTC) < 0)
64                         return;
65                 pmu_wait_complete(&req);
66                 if (req.reply_len != 4)
67                         printk(KERN_ERR "pmac_get_rtc_time: PMU returned a %d"
68                                " bytes reply\n", req.reply_len);
69                 now = (req.reply[0] << 24) + (req.reply[1] << 16)
70                         + (req.reply[2] << 8) + req.reply[3];
71                 DBG("get: %u -> %u\n", (int)now, (int)(now - RTC_OFFSET));
72                 now -= RTC_OFFSET;
73
74                 to_tm(now, tm);
75                 tm->tm_year -= 1900;
76                 tm->tm_mon -= 1;
77         
78                 DBG("-> tm_mday: %d, tm_mon: %d, tm_year: %d, %d:%02d:%02d\n",
79                     tm->tm_mday, tm->tm_mon, tm->tm_year,
80                     tm->tm_hour, tm->tm_min, tm->tm_sec);
81                 break;
82         }
83 #endif /* CONFIG_ADB_PMU */
84
85 #ifdef CONFIG_PMAC_SMU
86         case SYS_CTRLER_SMU:
87                 smu_get_rtc_time(tm, 1);
88                 break;
89 #endif /* CONFIG_PMAC_SMU */
90         default:
91                 ;
92         }
93 }
94
95 int __pmac pmac_set_rtc_time(struct rtc_time *tm)
96 {
97         switch(sys_ctrler) {
98 #ifdef CONFIG_ADB_PMU
99         case SYS_CTRLER_PMU: {
100                 /* TODO: Move that to a function in the PMU driver */
101                 struct adb_request req;
102                 unsigned int nowtime;
103
104                 DBG("set: tm_mday: %d, tm_mon: %d, tm_year: %d,"
105                     " %d:%02d:%02d\n",
106                     tm->tm_mday, tm->tm_mon, tm->tm_year,
107                     tm->tm_hour, tm->tm_min, tm->tm_sec);
108
109                 nowtime = mktime(tm->tm_year + 1900, tm->tm_mon + 1,
110                                  tm->tm_mday, tm->tm_hour, tm->tm_min,
111                                  tm->tm_sec);
112
113                 DBG("-> %u -> %u\n", (int)nowtime,
114                     (int)(nowtime + RTC_OFFSET));
115                 nowtime += RTC_OFFSET;
116
117                 if (pmu_request(&req, NULL, 5, PMU_SET_RTC,
118                                 nowtime >> 24, nowtime >> 16,
119                                 nowtime >> 8, nowtime) < 0)
120                         return -ENXIO;
121                 pmu_wait_complete(&req);
122                 if (req.reply_len != 0)
123                         printk(KERN_ERR "pmac_set_rtc_time: PMU returned a %d"
124                                " bytes reply\n", req.reply_len);
125                 return 0;
126         }
127 #endif /* CONFIG_ADB_PMU */
128
129 #ifdef CONFIG_PMAC_SMU
130         case SYS_CTRLER_SMU:
131                 return smu_set_rtc_time(tm, 1);
132 #endif /* CONFIG_PMAC_SMU */
133         default:
134                 return -ENODEV;
135         }
136 }
137
138 void __init pmac_get_boot_time(struct rtc_time *tm)
139 {
140         pmac_get_rtc_time(tm);
141
142 #ifdef disabled__CONFIG_NVRAM
143         s32 delta = 0;
144         int dst;
145         
146         delta = ((s32)pmac_xpram_read(PMAC_XPRAM_MACHINE_LOC + 0x9)) << 16;
147         delta |= ((s32)pmac_xpram_read(PMAC_XPRAM_MACHINE_LOC + 0xa)) << 8;
148         delta |= pmac_xpram_read(PMAC_XPRAM_MACHINE_LOC + 0xb);
149         if (delta & 0x00800000UL)
150                 delta |= 0xFF000000UL;
151         dst = ((pmac_xpram_read(PMAC_XPRAM_MACHINE_LOC + 0x8) & 0x80) != 0);
152         printk("GMT Delta read from XPRAM: %d minutes, DST: %s\n", delta/60,
153                 dst ? "on" : "off");
154 #endif
155 }
156
157 /*
158  * Query the OF and get the decr frequency.
159  * FIXME: merge this with generic_calibrate_decr
160  */
161 void __init pmac_calibrate_decr(void)
162 {
163         struct device_node *cpu;
164         unsigned int freq, *fp;
165         struct div_result divres;
166
167         /*
168          * The cpu node should have a timebase-frequency property
169          * to tell us the rate at which the decrementer counts.
170          */
171         cpu = find_type_devices("cpu");
172         if (cpu == 0)
173                 panic("can't find cpu node in time_init");
174         fp = (unsigned int *) get_property(cpu, "timebase-frequency", NULL);
175         if (fp == 0)
176                 panic("can't get cpu timebase frequency");
177         freq = *fp;
178         printk("time_init: decrementer frequency = %u.%.6u MHz\n",
179                freq/1000000, freq%1000000);
180         tb_ticks_per_jiffy = freq / HZ;
181         tb_ticks_per_sec = tb_ticks_per_jiffy * HZ;
182         tb_ticks_per_usec = freq / 1000000;
183         tb_to_us = mulhwu_scale_factor(freq, 1000000);
184         div128_by_32( 1024*1024, 0, tb_ticks_per_sec, &divres );
185         tb_to_xs = divres.result_low;
186         ppc_tb_freq = freq;
187
188         fp = (unsigned int *)get_property(cpu, "clock-frequency", NULL);
189         if (fp == 0)
190                 panic("can't get cpu processor frequency");
191         ppc_proc_freq = *fp;
192
193         setup_default_decr();
194 }
195