xref: /openbmc/linux/arch/mips/sgi-ip27/ip27-timer.c (revision f5ff0a28)
1 /*
2  * Copytight (C) 1999, 2000, 05, 06 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 
44 #if 0
45 static int set_rtc_mmss(unsigned long nowtime)
46 {
47 	int retval = 0;
48 	int real_seconds, real_minutes, cmos_minutes;
49 	struct m48t35_rtc *rtc;
50 	nasid_t nid;
51 
52 	nid = get_nasid();
53 	rtc = (struct m48t35_rtc *)(KL_CONFIG_CH_CONS_INFO(nid)->memory_base +
54 							IOC3_BYTEBUS_DEV0);
55 
56 	rtc->control |= M48T35_RTC_READ;
57 	cmos_minutes = BCD2BIN(rtc->min);
58 	rtc->control &= ~M48T35_RTC_READ;
59 
60 	/*
61 	 * Since we're only adjusting minutes and seconds, don't interfere with
62 	 * hour overflow. This avoids messing with unknown time zones but
63 	 * requires your RTC not to be off by more than 15 minutes
64 	 */
65 	real_seconds = nowtime % 60;
66 	real_minutes = nowtime / 60;
67 	if (((abs(real_minutes - cmos_minutes) + 15)/30) & 1)
68 		real_minutes += 30;	/* correct for half hour time zone */
69 	real_minutes %= 60;
70 
71 	if (abs(real_minutes - cmos_minutes) < 30) {
72 		real_seconds = BIN2BCD(real_seconds);
73 		real_minutes = BIN2BCD(real_minutes);
74 		rtc->control |= M48T35_RTC_SET;
75 		rtc->sec = real_seconds;
76 		rtc->min = real_minutes;
77 		rtc->control &= ~M48T35_RTC_SET;
78 	} else {
79 		printk(KERN_WARNING
80 		       "set_rtc_mmss: can't update from %d to %d\n",
81 		       cmos_minutes, real_minutes);
82 		retval = -1;
83 	}
84 
85 	return retval;
86 }
87 #endif
88 
89 static unsigned int rt_timer_irq;
90 
91 void ip27_rt_timer_interrupt(void)
92 {
93 	int cpu = smp_processor_id();
94 	int cpuA = cputoslice(cpu) == 0;
95 	unsigned int irq = rt_timer_irq;
96 
97 	irq_enter();
98 	write_seqlock(&xtime_lock);
99 
100 again:
101 	LOCAL_HUB_S(cpuA ? PI_RT_PEND_A : PI_RT_PEND_B, 0);	/* Ack  */
102 	ct_cur[cpu] += CYCLES_PER_JIFFY;
103 	LOCAL_HUB_S(cpuA ? PI_RT_COMPARE_A : PI_RT_COMPARE_B, ct_cur[cpu]);
104 
105 	if (LOCAL_HUB_L(PI_RT_COUNT) >= ct_cur[cpu])
106 		goto again;
107 
108 	kstat_this_cpu.irqs[irq]++;		/* kstat only for bootcpu? */
109 
110 	if (cpu == 0)
111 		do_timer(1);
112 
113 	update_process_times(user_mode(get_irq_regs()));
114 
115 	write_sequnlock(&xtime_lock);
116 	irq_exit();
117 }
118 
119 /* Includes for ioc3_init().  */
120 #include <asm/sn/types.h>
121 #include <asm/sn/sn0/addrs.h>
122 #include <asm/sn/sn0/hubni.h>
123 #include <asm/sn/sn0/hubio.h>
124 #include <asm/pci/bridge.h>
125 
126 static __init unsigned long get_m48t35_time(void)
127 {
128         unsigned int year, month, date, hour, min, sec;
129 	struct m48t35_rtc *rtc;
130 	nasid_t nid;
131 
132 	nid = get_nasid();
133 	rtc = (struct m48t35_rtc *)(KL_CONFIG_CH_CONS_INFO(nid)->memory_base +
134 							IOC3_BYTEBUS_DEV0);
135 
136 	rtc->control |= M48T35_RTC_READ;
137 	sec = rtc->sec;
138 	min = rtc->min;
139 	hour = rtc->hour;
140 	date = rtc->date;
141 	month = rtc->month;
142 	year = rtc->year;
143 	rtc->control &= ~M48T35_RTC_READ;
144 
145         sec = BCD2BIN(sec);
146         min = BCD2BIN(min);
147         hour = BCD2BIN(hour);
148         date = BCD2BIN(date);
149         month = BCD2BIN(month);
150         year = BCD2BIN(year);
151 
152         year += 1970;
153 
154         return mktime(year, month, date, hour, min, sec);
155 }
156 
157 static void enable_rt_irq(unsigned int irq)
158 {
159 }
160 
161 static void disable_rt_irq(unsigned int irq)
162 {
163 }
164 
165 static struct irq_chip rt_irq_type = {
166 	.name		= "SN HUB RT timer",
167 	.ack		= disable_rt_irq,
168 	.mask		= disable_rt_irq,
169 	.mask_ack	= disable_rt_irq,
170 	.unmask		= enable_rt_irq,
171 	.eoi		= enable_rt_irq,
172 };
173 
174 static struct irqaction rt_irqaction = {
175 	.handler	= (irq_handler_t) ip27_rt_timer_interrupt,
176 	.flags		= IRQF_DISABLED,
177 	.mask		= CPU_MASK_NONE,
178 	.name		= "timer"
179 };
180 
181 void __init plat_timer_setup(struct irqaction *irq)
182 {
183 	int irqno  = allocate_irqno();
184 
185 	if (irqno < 0)
186 		panic("Can't allocate interrupt number for timer interrupt");
187 
188 	set_irq_chip_and_handler(irqno, &rt_irq_type, handle_percpu_irq);
189 
190 	/* over-write the handler, we use our own way */
191 	irq->handler = no_action;
192 
193 	/* setup irqaction */
194 	irq_desc[irqno].status |= IRQ_PER_CPU;
195 
196 	rt_timer_irq = irqno;
197 	/*
198 	 * Only needed to get /proc/interrupt to display timer irq stats
199 	 */
200 	setup_irq(irqno, &rt_irqaction);
201 }
202 
203 static cycle_t ip27_hpt_read(void)
204 {
205 	return REMOTE_HUB_L(cputonasid(0), PI_RT_COUNT);
206 }
207 
208 void __init ip27_time_init(void)
209 {
210 	clocksource_mips.read = ip27_hpt_read;
211 	mips_hpt_frequency = CYCLES_PER_SEC;
212 	xtime.tv_sec = get_m48t35_time();
213 	xtime.tv_nsec = 0;
214 }
215 
216 void __init cpu_time_init(void)
217 {
218 	lboard_t *board;
219 	klcpu_t *cpu;
220 	int cpuid;
221 
222 	/* Don't use ARCS.  ARCS is fragile.  Klconfig is simple and sane.  */
223 	board = find_lboard(KL_CONFIG_INFO(get_nasid()), KLTYPE_IP27);
224 	if (!board)
225 		panic("Can't find board info for myself.");
226 
227 	cpuid = LOCAL_HUB_L(PI_CPU_NUM) ? IP27_CPU0_INDEX : IP27_CPU1_INDEX;
228 	cpu = (klcpu_t *) KLCF_COMP(board, cpuid);
229 	if (!cpu)
230 		panic("No information about myself?");
231 
232 	printk("CPU %d clock is %dMHz.\n", smp_processor_id(), cpu->cpu_speed);
233 
234 	set_c0_status(SRB_TIMOCLK);
235 }
236 
237 void __init hub_rtc_init(cnodeid_t cnode)
238 {
239 	/*
240 	 * We only need to initialize the current node.
241 	 * If this is not the current node then it is a cpuless
242 	 * node and timeouts will not happen there.
243 	 */
244 	if (get_compact_nodeid() == cnode) {
245 		int cpu = smp_processor_id();
246 		LOCAL_HUB_S(PI_RT_EN_A, 1);
247 		LOCAL_HUB_S(PI_RT_EN_B, 1);
248 		LOCAL_HUB_S(PI_PROF_EN_A, 0);
249 		LOCAL_HUB_S(PI_PROF_EN_B, 0);
250 		ct_cur[cpu] = CYCLES_PER_JIFFY;
251 		LOCAL_HUB_S(PI_RT_COMPARE_A, ct_cur[cpu]);
252 		LOCAL_HUB_S(PI_RT_COUNT, 0);
253 		LOCAL_HUB_S(PI_RT_PEND_A, 0);
254 		LOCAL_HUB_S(PI_RT_COMPARE_B, ct_cur[cpu]);
255 		LOCAL_HUB_S(PI_RT_COUNT, 0);
256 		LOCAL_HUB_S(PI_RT_PEND_B, 0);
257 	}
258 }
259