File: /usr/src/linux/arch/mips64/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/config.h>
6     #include <linux/init.h>
7     #include <linux/kernel.h>
8     #include <linux/sched.h>
9     #include <linux/interrupt.h>
10     #include <linux/kernel_stat.h>
11     #include <linux/param.h>
12     #include <linux/timex.h>
13     #include <linux/mm.h>		
14     
15     #include <asm/pgtable.h>
16     #include <asm/sgialib.h>
17     #include <asm/sn/ioc3.h>
18     #include <asm/m48t35.h>
19     #include <asm/sn/klconfig.h>
20     #include <asm/sn/arch.h>
21     #include <asm/sn/addrs.h>
22     #include <asm/sn/sn_private.h>
23     #include <asm/sn/sn0/ip27.h>
24     #include <asm/sn/sn0/hub.h>
25     
26     /*
27      * This is a hack; we really need to figure these values out dynamically
28      * 
29      * Since 800 ns works very well with various HUB frequencies, such as
30      * 360, 380, 390 and 400 MHZ, we use 800 ns rtc cycle time.
31      *
32      * Ralf: which clock rate is used to feed the counter?
33      */
34     #define NSEC_PER_CYCLE		800
35     #define NSEC_PER_SEC		1000000000
36     #define CYCLES_PER_SEC		(NSEC_PER_SEC/NSEC_PER_CYCLE)
37     #define CYCLES_PER_JIFFY	(CYCLES_PER_SEC/HZ)
38     
39     static unsigned long ct_cur[NR_CPUS];	/* What counter should be at next timer irq */
40     static long last_rtc_update;		/* Last time the rtc clock got updated */
41     
42     extern rwlock_t xtime_lock;
43     extern volatile unsigned long wall_jiffies;
44     
45     
46     static int set_rtc_mmss(unsigned long nowtime)
47     {
48     	int retval = 0;
49     	int real_seconds, real_minutes, cmos_minutes;
50     	struct m48t35_rtc *rtc;
51     	nasid_t nid;
52     
53     	nid = get_nasid();
54     	rtc = (struct m48t35_rtc *)(KL_CONFIG_CH_CONS_INFO(nid)->memory_base + 
55     							IOC3_BYTEBUS_DEV0);
56     
57     	rtc->control |= M48T35_RTC_READ;
58     	cmos_minutes = rtc->min;
59     	BCD_TO_BIN(cmos_minutes);
60     	rtc->control &= ~M48T35_RTC_READ;
61     
62     	/*
63     	 * Since we're only adjusting minutes and seconds, don't interfere with
64     	 * hour overflow. This avoids messing with unknown time zones but
65     	 * requires your RTC not to be off by more than 15 minutes
66     	 */
67     	real_seconds = nowtime % 60;
68     	real_minutes = nowtime / 60;
69     	if (((abs(real_minutes - cmos_minutes) + 15)/30) & 1)
70     		real_minutes += 30;	/* correct for half hour time zone */
71     	real_minutes %= 60;
72     
73     	if (abs(real_minutes - cmos_minutes) < 30) {
74     		BIN_TO_BCD(real_seconds);
75     		BIN_TO_BCD(real_minutes);
76     		rtc->control |= M48T35_RTC_SET;
77     		rtc->sec = real_seconds;
78     		rtc->min = real_minutes;
79     		rtc->control &= ~M48T35_RTC_SET;
80     	} else {
81     		printk(KERN_WARNING
82     		       "set_rtc_mmss: can't update from %d to %d\n",
83     		       cmos_minutes, real_minutes);
84     		retval = -1;
85     	}
86     
87     	return retval;
88     }
89     
90     void rt_timer_interrupt(struct pt_regs *regs)
91     {
92     	int cpu = smp_processor_id();
93     	int cpuA = ((cputoslice(cpu)) == 0);
94     	int irq = 7;				/* XXX Assign number */
95     
96     	write_lock(&xtime_lock);
97     
98     again:
99     	LOCAL_HUB_S(cpuA ? PI_RT_PEND_A : PI_RT_PEND_B, 0);	/* Ack  */
100     	ct_cur[cpu] += CYCLES_PER_JIFFY;
101     	LOCAL_HUB_S(cpuA ? PI_RT_COMPARE_A : PI_RT_COMPARE_B, ct_cur[cpu]);
102     
103     	if (LOCAL_HUB_L(PI_RT_COUNT) >= ct_cur[cpu])
104     		goto again;
105     
106     	kstat.irqs[cpu][irq]++;		/* kstat only for bootcpu? */
107     
108     	if (cpu == 0)
109     		do_timer(regs);
110     
111     #ifdef CONFIG_SMP
112     	{
113     		int user = user_mode(regs);
114     
115     		/*
116     		 * update_process_times() expects us to have done irq_enter().
117     		 * Besides, if we don't timer interrupts ignore the global
118     		 * interrupt lock, which is the WrongThing (tm) to do.
119     		 * Picked from i386 code.
120     		 */
121     		irq_enter(cpu, 0);
122     		update_process_times(user);
123     		irq_exit(cpu, 0);
124     	}
125     #endif /* CONFIG_SMP */
126     	
127     	/*
128     	 * If we have an externally synchronized Linux clock, then update
129     	 * RTC clock accordingly every ~11 minutes. Set_rtc_mmss() has to be
130     	 * called as close as possible to when a second starts.
131     	 */
132     	if ((time_status & STA_UNSYNC) == 0 &&
133     	    xtime.tv_sec > last_rtc_update + 660) {
134     		if (xtime.tv_usec >= 1000000 - ((unsigned) tick) / 2) {
135     			if (set_rtc_mmss(xtime.tv_sec + 1) == 0)
136     				last_rtc_update = xtime.tv_sec;
137     			else    
138     				last_rtc_update = xtime.tv_sec - 600;
139     		} else if (xtime.tv_usec <= ((unsigned) tick) / 2) {
140     			if (set_rtc_mmss(xtime.tv_sec) == 0)
141     				last_rtc_update = xtime.tv_sec;
142     			else    
143     				last_rtc_update = xtime.tv_sec - 600;
144     		}
145             }
146     
147     	write_unlock(&xtime_lock);
148     
149     	if (softirq_pending(cpu))
150     		do_softirq();
151     }
152     
153     unsigned long inline do_gettimeoffset(void)
154     {
155     	unsigned long ct_cur1;
156     	ct_cur1 = REMOTE_HUB_L(cputonasid(0), PI_RT_COUNT) + CYCLES_PER_JIFFY;
157     	return (ct_cur1 - ct_cur[0]) * NSEC_PER_CYCLE / 1000;
158     }
159     
160     void do_gettimeofday(struct timeval *tv)
161     {
162     	unsigned long flags;
163     	unsigned long usec, sec;
164     
165     	read_lock_irqsave(&xtime_lock, flags);
166     	usec = do_gettimeoffset();
167     	{
168     		unsigned long lost = jiffies - wall_jiffies;
169     		if (lost)
170     			usec += lost * (1000000 / HZ);
171     	}
172     	sec = xtime.tv_sec;
173     	usec += xtime.tv_usec;
174     	read_unlock_irqrestore(&xtime_lock, flags);
175     
176     	while (usec >= 1000000) {
177     		usec -= 1000000;
178     		sec++;
179     	}
180     
181     	tv->tv_sec = sec;
182     	tv->tv_usec = usec;
183     }
184     
185     void do_settimeofday(struct timeval *tv)
186     {
187     	write_lock_irq(&xtime_lock);
188     	tv->tv_usec -= do_gettimeoffset();
189     	tv->tv_usec -= (jiffies - wall_jiffies) * (1000000 / HZ);
190     
191     	while (tv->tv_usec < 0) {
192     		tv->tv_usec += 1000000;
193     		tv->tv_sec--;
194     	}
195     
196     	xtime = *tv;
197     	time_adjust = 0;		/* stop active adjtime() */
198     	time_status |= STA_UNSYNC;
199     	time_maxerror = NTP_PHASE_LIMIT;
200     	time_esterror = NTP_PHASE_LIMIT;
201     	write_unlock_irq(&xtime_lock);
202     }
203     
204     /* Includes for ioc3_init().  */
205     #include <asm/sn/types.h>
206     #include <asm/sn/sn0/addrs.h>
207     #include <asm/sn/sn0/hubni.h>
208     #include <asm/sn/sn0/hubio.h>
209     #include <asm/pci/bridge.h>
210     
211     static __init unsigned long get_m48t35_time(void)
212     {
213             unsigned int year, month, date, hour, min, sec;
214     	struct m48t35_rtc *rtc;
215     	nasid_t nid;
216     
217     	nid = get_nasid();
218     	rtc = (struct m48t35_rtc *)(KL_CONFIG_CH_CONS_INFO(nid)->memory_base + 
219     							IOC3_BYTEBUS_DEV0);
220     
221     	rtc->control |= M48T35_RTC_READ;
222     	sec = rtc->sec;
223     	min = rtc->min;
224     	hour = rtc->hour;
225     	date = rtc->date;
226     	month = rtc->month;
227     	year = rtc->year;
228     	rtc->control &= ~M48T35_RTC_READ;
229     
230             BCD_TO_BIN(sec);
231             BCD_TO_BIN(min);
232             BCD_TO_BIN(hour);
233             BCD_TO_BIN(date);
234             BCD_TO_BIN(month);
235             BCD_TO_BIN(year);
236     
237             year += 1970;
238     
239             return mktime(year, month, date, hour, min, sec);
240     }
241     
242     void __init time_init(void)
243     {
244     	xtime.tv_sec = get_m48t35_time();
245     	xtime.tv_usec = 0;
246     }
247     
248     void __init cpu_time_init(void)
249     {
250     	lboard_t *board;
251     	klcpu_t *cpu;
252     	int cpuid;
253     
254     	/* Don't use ARCS.  ARCS is fragile.  Klconfig is simple and sane.  */
255     	board = find_lboard(KL_CONFIG_INFO(get_nasid()), KLTYPE_IP27);
256     	if (!board)
257     		panic("Can't find board info for myself.");
258     
259     	cpuid = LOCAL_HUB_L(PI_CPU_NUM) ? IP27_CPU0_INDEX : IP27_CPU1_INDEX;
260     	cpu = (klcpu_t *) KLCF_COMP(board, cpuid);
261     	if (!cpu)
262     		panic("No information about myself?");
263     
264     	printk("CPU %d clock is %dMHz.\n", smp_processor_id(), cpu->cpu_speed);
265     
266     	set_cp0_status(SRB_TIMOCLK, SRB_TIMOCLK);
267     }
268     
269     void __init hub_rtc_init(cnodeid_t cnode)
270     {
271     	/*
272     	 * We only need to initialize the current node.
273     	 * If this is not the current node then it is a cpuless
274     	 * node and timeouts will not happen there.
275     	 */
276     	if (get_compact_nodeid() == cnode) {
277     		int cpu = smp_processor_id();
278     		LOCAL_HUB_S(PI_RT_EN_A, 1);
279     		LOCAL_HUB_S(PI_RT_EN_B, 1);
280     		LOCAL_HUB_S(PI_PROF_EN_A, 0);
281     		LOCAL_HUB_S(PI_PROF_EN_B, 0);
282     		ct_cur[cpu] = CYCLES_PER_JIFFY;
283     		LOCAL_HUB_S(PI_RT_COMPARE_A, ct_cur[cpu]);
284     		LOCAL_HUB_S(PI_RT_COUNT, 0);
285     		LOCAL_HUB_S(PI_RT_PEND_A, 0);
286     		LOCAL_HUB_S(PI_RT_COMPARE_B, ct_cur[cpu]);
287     		LOCAL_HUB_S(PI_RT_COUNT, 0);
288     		LOCAL_HUB_S(PI_RT_PEND_B, 0);
289     	}
290     }
291