ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / arch / mips / sgi-ip27 / ip27-timer.c
1 /*
2  * Copytight (C) 1999, 2000 Ralf Baechle (ralf@gnu.org)
3  * Copytight (C) 1999, 2000 Silicon Graphics, Inc.
4  */
5 #include <linux/bcd.h>
6 #include <linux/config.h>
7 #include <linux/init.h>
8 #include <linux/kernel.h>
9 #include <linux/sched.h>
10 #include <linux/interrupt.h>
11 #include <linux/kernel_stat.h>
12 #include <linux/param.h>
13 #include <linux/time.h>
14 #include <linux/timex.h>
15 #include <linux/mm.h>
16
17 #include <asm/time.h>
18 #include <asm/pgtable.h>
19 #include <asm/sgialib.h>
20 #include <asm/sn/ioc3.h>
21 #include <asm/m48t35.h>
22 #include <asm/sn/klconfig.h>
23 #include <asm/sn/arch.h>
24 #include <asm/sn/addrs.h>
25 #include <asm/sn/sn_private.h>
26 #include <asm/sn/sn0/ip27.h>
27 #include <asm/sn/sn0/hub.h>
28
29 /*
30  * This is a hack; we really need to figure these values out dynamically
31  *
32  * Since 800 ns works very well with various HUB frequencies, such as
33  * 360, 380, 390 and 400 MHZ, we use 800 ns rtc cycle time.
34  *
35  * Ralf: which clock rate is used to feed the counter?
36  */
37 #define NSEC_PER_CYCLE          800
38 #define CYCLES_PER_SEC          (NSEC_PER_SEC/NSEC_PER_CYCLE)
39 #define CYCLES_PER_JIFFY        (CYCLES_PER_SEC/HZ)
40
41 #define TICK_SIZE (tick_nsec / 1000)
42
43 static unsigned long ct_cur[NR_CPUS];   /* What counter should be at next timer irq */
44 static long last_rtc_update;            /* Last time the rtc clock got updated */
45
46 extern volatile unsigned long wall_jiffies;
47
48 #if 0
49 static int set_rtc_mmss(unsigned long nowtime)
50 {
51         int retval = 0;
52         int real_seconds, real_minutes, cmos_minutes;
53         struct m48t35_rtc *rtc;
54         nasid_t nid;
55
56         nid = get_nasid();
57         rtc = (struct m48t35_rtc *)(KL_CONFIG_CH_CONS_INFO(nid)->memory_base +
58                                                         IOC3_BYTEBUS_DEV0);
59
60         rtc->control |= M48T35_RTC_READ;
61         cmos_minutes = BCD2BIN(rtc->min);
62         rtc->control &= ~M48T35_RTC_READ;
63
64         /*
65          * Since we're only adjusting minutes and seconds, don't interfere with
66          * hour overflow. This avoids messing with unknown time zones but
67          * requires your RTC not to be off by more than 15 minutes
68          */
69         real_seconds = nowtime % 60;
70         real_minutes = nowtime / 60;
71         if (((abs(real_minutes - cmos_minutes) + 15)/30) & 1)
72                 real_minutes += 30;     /* correct for half hour time zone */
73         real_minutes %= 60;
74
75         if (abs(real_minutes - cmos_minutes) < 30) {
76                 real_seconds = BIN2BCD(real_seconds);
77                 real_minutes = BIN2BCD(real_minutes);
78                 rtc->control |= M48T35_RTC_SET;
79                 rtc->sec = real_seconds;
80                 rtc->min = real_minutes;
81                 rtc->control &= ~M48T35_RTC_SET;
82         } else {
83                 printk(KERN_WARNING
84                        "set_rtc_mmss: can't update from %d to %d\n",
85                        cmos_minutes, real_minutes);
86                 retval = -1;
87         }
88
89         return retval;
90 }
91 #endif
92
93 void ip27_rt_timer_interrupt(struct pt_regs *regs)
94 {
95         int cpu = smp_processor_id();
96         int cpuA = cputoslice(cpu) == 0;
97         int irq = 9;                            /* XXX Assign number */
98
99         irq_enter();
100         write_seqlock(&xtime_lock);
101
102 again:
103         LOCAL_HUB_S(cpuA ? PI_RT_PEND_A : PI_RT_PEND_B, 0);     /* Ack  */
104         ct_cur[cpu] += CYCLES_PER_JIFFY;
105         LOCAL_HUB_S(cpuA ? PI_RT_COMPARE_A : PI_RT_COMPARE_B, ct_cur[cpu]);
106
107         if (LOCAL_HUB_L(PI_RT_COUNT) >= ct_cur[cpu])
108                 goto again;
109
110         kstat_this_cpu.irqs[irq]++;             /* kstat only for bootcpu? */
111
112         if (cpu == 0)
113                 do_timer(regs);
114
115 #ifdef CONFIG_SMP
116         update_process_times(user_mode(regs));
117 #endif /* CONFIG_SMP */
118
119         /*
120          * If we have an externally synchronized Linux clock, then update
121          * RTC clock accordingly every ~11 minutes. Set_rtc_mmss() has to be
122          * called as close as possible to when a second starts.
123          */
124         if ((time_status & STA_UNSYNC) == 0 &&
125             xtime.tv_sec > last_rtc_update + 660 &&
126             (xtime.tv_nsec / 1000) >= 500000 - ((unsigned) TICK_SIZE) / 2 &&
127             (xtime.tv_nsec / 1000) <= 500000 + ((unsigned) TICK_SIZE) / 2) {
128                 if (rtc_set_time(xtime.tv_sec) == 0) {
129                         last_rtc_update = xtime.tv_sec;
130                 } else {
131                         last_rtc_update = xtime.tv_sec - 600;
132                         /* do it again in 60 s */
133                 }
134         }
135
136         write_sequnlock(&xtime_lock);
137         irq_exit();
138 }
139
140 unsigned long ip27_do_gettimeoffset(void)
141 {
142         unsigned long ct_cur1;
143         ct_cur1 = REMOTE_HUB_L(cputonasid(0), PI_RT_COUNT) + CYCLES_PER_JIFFY;
144         return (ct_cur1 - ct_cur[0]) * NSEC_PER_CYCLE / 1000;
145 }
146
147 /* Includes for ioc3_init().  */
148 #include <asm/sn/types.h>
149 #include <asm/sn/sn0/addrs.h>
150 #include <asm/sn/sn0/hubni.h>
151 #include <asm/sn/sn0/hubio.h>
152 #include <asm/pci/bridge.h>
153
154 static __init unsigned long get_m48t35_time(void)
155 {
156         unsigned int year, month, date, hour, min, sec;
157         struct m48t35_rtc *rtc;
158         nasid_t nid;
159
160         nid = get_nasid();
161         rtc = (struct m48t35_rtc *)(KL_CONFIG_CH_CONS_INFO(nid)->memory_base +
162                                                         IOC3_BYTEBUS_DEV0);
163
164         rtc->control |= M48T35_RTC_READ;
165         sec = rtc->sec;
166         min = rtc->min;
167         hour = rtc->hour;
168         date = rtc->date;
169         month = rtc->month;
170         year = rtc->year;
171         rtc->control &= ~M48T35_RTC_READ;
172
173         sec = BCD2BIN(sec);
174         min = BCD2BIN(min);
175         hour = BCD2BIN(hour);
176         date = BCD2BIN(date);
177         month = BCD2BIN(month);
178         year = BCD2BIN(year);
179
180         year += 1970;
181
182         return mktime(year, month, date, hour, min, sec);
183 }
184
185 static void ip27_timer_setup(struct irqaction *irq)
186 {
187         /* over-write the handler, we use our own way */
188         irq->handler = no_action;
189
190         /* setup irqaction */
191 //      setup_irq(IP27_TIMER_IRQ, irq);         /* XXX Can't do this yet.  */
192 }
193
194 void __init ip27_time_init(void)
195 {
196         xtime.tv_sec = get_m48t35_time();
197         xtime.tv_nsec = 0;
198
199         do_gettimeoffset = ip27_do_gettimeoffset;
200
201         board_timer_setup = ip27_timer_setup;
202 }
203
204 void __init cpu_time_init(void)
205 {
206         lboard_t *board;
207         klcpu_t *cpu;
208         int cpuid;
209
210         /* Don't use ARCS.  ARCS is fragile.  Klconfig is simple and sane.  */
211         board = find_lboard(KL_CONFIG_INFO(get_nasid()), KLTYPE_IP27);
212         if (!board)
213                 panic("Can't find board info for myself.");
214
215         cpuid = LOCAL_HUB_L(PI_CPU_NUM) ? IP27_CPU0_INDEX : IP27_CPU1_INDEX;
216         cpu = (klcpu_t *) KLCF_COMP(board, cpuid);
217         if (!cpu)
218                 panic("No information about myself?");
219
220         printk("CPU %d clock is %dMHz.\n", smp_processor_id(), cpu->cpu_speed);
221
222         set_c0_status(SRB_TIMOCLK);
223 }
224
225 void __init hub_rtc_init(cnodeid_t cnode)
226 {
227         /*
228          * We only need to initialize the current node.
229          * If this is not the current node then it is a cpuless
230          * node and timeouts will not happen there.
231          */
232         if (get_compact_nodeid() == cnode) {
233                 int cpu = smp_processor_id();
234                 LOCAL_HUB_S(PI_RT_EN_A, 1);
235                 LOCAL_HUB_S(PI_RT_EN_B, 1);
236                 LOCAL_HUB_S(PI_PROF_EN_A, 0);
237                 LOCAL_HUB_S(PI_PROF_EN_B, 0);
238                 ct_cur[cpu] = CYCLES_PER_JIFFY;
239                 LOCAL_HUB_S(PI_RT_COMPARE_A, ct_cur[cpu]);
240                 LOCAL_HUB_S(PI_RT_COUNT, 0);
241                 LOCAL_HUB_S(PI_RT_PEND_A, 0);
242                 LOCAL_HUB_S(PI_RT_COMPARE_B, ct_cur[cpu]);
243                 LOCAL_HUB_S(PI_RT_COUNT, 0);
244                 LOCAL_HUB_S(PI_RT_PEND_B, 0);
245         }
246 }