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