1 /*
2  * Copytight (C) 1999, 2000, 2002 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/time.h>
16 #include <asm/pgtable.h>
17 #include <asm/sgialib.h>
18 #include <asm/sn/ioc3.h>
19 #include <asm/m48t35.h>
20 #include <asm/sn/klconfig.h>
21 #include <asm/sn/arch.h>
22 #include <asm/sn/addrs.h>
23 #include <asm/sn/sn_private.h>
24 #include <asm/sn/sn0/ip27.h>
25 #include <asm/sn/sn0/hub.h>
26 
27 /*
28  * This is a hack; we really need to figure these values out dynamically
29  *
30  * Since 800 ns works very well with various HUB frequencies, such as
31  * 360, 380, 390 and 400 MHZ, we use 800 ns rtc cycle time.
32  *
33  * Ralf: which clock rate is used to feed the counter?
34  */
35 #define NSEC_PER_CYCLE		800
36 #define NSEC_PER_SEC		1000000000
37 #define CYCLES_PER_SEC		(NSEC_PER_SEC/NSEC_PER_CYCLE)
38 #define CYCLES_PER_JIFFY	(CYCLES_PER_SEC/HZ)
39 
40 static unsigned long ct_cur[NR_CPUS];	/* What counter should be at next timer irq */
41 static long last_rtc_update;		/* Last time the rtc clock got updated */
42 
43 extern rwlock_t xtime_lock;
44 extern volatile unsigned long wall_jiffies;
45 
46 
set_rtc_mmss(unsigned long nowtime)47 static int set_rtc_mmss(unsigned long nowtime)
48 {
49 	int retval = 0;
50 	int real_seconds, real_minutes, cmos_minutes;
51 	struct m48t35_rtc *rtc;
52 	nasid_t nid;
53 
54 	rtc = (struct m48t35_rtc *)
55 	(KL_CONFIG_CH_CONS_INFO(master_nasid)->memory_base + IOC3_BYTEBUS_DEV0);
56 
57 	spin_lock(&rtc_lock);
58 	rtc->control |= M48T35_RTC_READ;
59 	cmos_minutes = rtc->min;
60 	BCD_TO_BIN(cmos_minutes);
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 		BIN_TO_BCD(real_seconds);
76 		BIN_TO_BCD(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 	spin_unlock(&rtc_lock);
88 
89 	return retval;
90 }
91 
92 #define IP27_TIMER_IRQ	9			/* XXX Assign number */
93 
rt_timer_interrupt(struct pt_regs * regs)94 void rt_timer_interrupt(struct pt_regs *regs)
95 {
96 	int cpu = smp_processor_id();
97 	int cpuA = ((cputoslice(cpu)) == 0);
98 	int irq = IP27_TIMER_IRQ;
99 
100 	irq_enter(cpu, irq);
101 	write_lock(&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.irqs[cpu][irq]++;		/* kstat only for bootcpu? */
112 
113 	if (cpu == 0)
114 		do_timer(regs);
115 
116 #ifdef CONFIG_SMP
117 	update_process_times(user_mode(regs));
118 #endif /* CONFIG_SMP */
119 
120 	/*
121 	 * If we have an externally synchronized Linux clock, then update
122 	 * RTC clock accordingly every ~11 minutes. Set_rtc_mmss() has to be
123 	 * called as close as possible to when a second starts.
124 	 */
125 	if ((time_status & STA_UNSYNC) == 0 &&
126 	    xtime.tv_sec > last_rtc_update + 660) {
127 		if (xtime.tv_usec >= 1000000 - ((unsigned) tick) / 2) {
128 			if (set_rtc_mmss(xtime.tv_sec + 1) == 0)
129 				last_rtc_update = xtime.tv_sec;
130 			else
131 				last_rtc_update = xtime.tv_sec - 600;
132 		} else if (xtime.tv_usec <= ((unsigned) tick) / 2) {
133 			if (set_rtc_mmss(xtime.tv_sec) == 0)
134 				last_rtc_update = xtime.tv_sec;
135 			else
136 				last_rtc_update = xtime.tv_sec - 600;
137 		}
138         }
139 
140 	write_unlock(&xtime_lock);
141 	irq_exit(cpu, irq);
142 
143 	if (softirq_pending(cpu))
144 		do_softirq();
145 }
146 
ip27_do_gettimeoffset(void)147 unsigned long ip27_do_gettimeoffset(void)
148 {
149 	unsigned long ct_cur1;
150 	ct_cur1 = REMOTE_HUB_L(cputonasid(0), PI_RT_COUNT) + CYCLES_PER_JIFFY;
151 	return (ct_cur1 - ct_cur[0]) * NSEC_PER_CYCLE / 1000;
152 }
153 
154 /* Includes for ioc3_init().  */
155 #include <asm/sn/types.h>
156 #include <asm/sn/sn0/addrs.h>
157 #include <asm/sn/sn0/hubni.h>
158 #include <asm/sn/sn0/hubio.h>
159 #include <asm/pci/bridge.h>
160 
get_m48t35_time(void)161 static __init unsigned long get_m48t35_time(void)
162 {
163 	unsigned int year, month, date, hour, min, sec;
164 	struct m48t35_rtc *rtc;
165 	nasid_t nid;
166 
167 	nid = get_nasid();
168 	rtc = (struct m48t35_rtc *)(KL_CONFIG_CH_CONS_INFO(nid)->memory_base +
169 							IOC3_BYTEBUS_DEV0);
170 
171 	spin_lock(&rtc_lock);
172 	rtc->control |= M48T35_RTC_READ;
173 	sec = rtc->sec;
174 	min = rtc->min;
175 	hour = rtc->hour;
176 	date = rtc->date;
177 	month = rtc->month;
178 	year = rtc->year;
179 	rtc->control &= ~M48T35_RTC_READ;
180 	spin_unlock(&rtc_lock);
181 
182 	BCD_TO_BIN(sec);
183 	BCD_TO_BIN(min);
184 	BCD_TO_BIN(hour);
185 	BCD_TO_BIN(date);
186 	BCD_TO_BIN(month);
187 	BCD_TO_BIN(year);
188 
189 	year += 1970;
190 
191 	return mktime(year, month, date, hour, min, sec);
192 }
193 
ip27_timer_setup(struct irqaction * irq)194 static void ip27_timer_setup(struct irqaction *irq)
195 {
196 	/* over-write the handler, we use our own way */
197 	irq->handler = no_action;
198 
199 	/* setup irqaction */
200 //	setup_irq(IP27_TIMER_IRQ, irq);		/* XXX Can't do this yet.  */
201 }
202 
ip27_time_init(void)203 void __init ip27_time_init(void)
204 {
205 	xtime.tv_sec = get_m48t35_time();
206 	xtime.tv_usec = 0;
207 
208 	do_gettimeoffset = ip27_do_gettimeoffset;
209 
210 	// board_time_init = ip27_time_init;
211 	board_timer_setup = ip27_timer_setup;
212 }
213 
cpu_time_init(void)214 void __init cpu_time_init(void)
215 {
216 	lboard_t *board;
217 	klcpu_t *cpu;
218 	int cpuid;
219 
220 	/* Don't use ARCS.  ARCS is fragile.  Klconfig is simple and sane.  */
221 	board = find_lboard(KL_CONFIG_INFO(get_nasid()), KLTYPE_IP27);
222 	if (!board)
223 		panic("Can't find board info for myself.");
224 
225 	cpuid = LOCAL_HUB_L(PI_CPU_NUM) ? IP27_CPU0_INDEX : IP27_CPU1_INDEX;
226 	cpu = (klcpu_t *) KLCF_COMP(board, cpuid);
227 	if (!cpu)
228 		panic("No information about myself?");
229 
230 	printk("CPU %d clock is %dMHz.\n", smp_processor_id(), cpu->cpu_speed);
231 
232 	set_c0_status(SRB_TIMOCLK);
233 }
234 
hub_rtc_init(cnodeid_t cnode)235 void __init hub_rtc_init(cnodeid_t cnode)
236 {
237 	/*
238 	 * We only need to initialize the current node.
239 	 * If this is not the current node then it is a cpuless
240 	 * node and timeouts will not happen there.
241 	 */
242 	if (get_compact_nodeid() == cnode) {
243 		int cpu = smp_processor_id();
244 		LOCAL_HUB_S(PI_RT_EN_A, 1);
245 		LOCAL_HUB_S(PI_RT_EN_B, 1);
246 		LOCAL_HUB_S(PI_PROF_EN_A, 0);
247 		LOCAL_HUB_S(PI_PROF_EN_B, 0);
248 		ct_cur[cpu] = CYCLES_PER_JIFFY;
249 		LOCAL_HUB_S(PI_RT_COMPARE_A, ct_cur[cpu]);
250 		LOCAL_HUB_S(PI_RT_COUNT, 0);
251 		LOCAL_HUB_S(PI_RT_PEND_A, 0);
252 		LOCAL_HUB_S(PI_RT_COMPARE_B, ct_cur[cpu]);
253 		LOCAL_HUB_S(PI_RT_COUNT, 0);
254 		LOCAL_HUB_S(PI_RT_PEND_B, 0);
255 	}
256 }
257