2 * Copytight (C) 1999, 2000, 05, 06 Ralf Baechle (ralf@linux-mips.org)
3 * Copytight (C) 1999, 2000 Silicon Graphics, Inc.
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>
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>
29 * This is a hack; we really need to figure these values out dynamically
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.
34 * Ralf: which clock rate is used to feed the counter?
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)
40 #define TICK_SIZE (tick_nsec / 1000)
42 static unsigned long ct_cur[NR_CPUS]; /* What counter should be at next timer irq */
45 static int set_rtc_mmss(unsigned long nowtime)
48 int real_seconds, real_minutes, cmos_minutes;
49 struct m48t35_rtc *rtc;
53 rtc = (struct m48t35_rtc *)(KL_CONFIG_CH_CONS_INFO(nid)->memory_base +
56 rtc->control |= M48T35_RTC_READ;
57 cmos_minutes = BCD2BIN(rtc->min);
58 rtc->control &= ~M48T35_RTC_READ;
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
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 */
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;
80 "set_rtc_mmss: can't update from %d to %d\n",
81 cmos_minutes, real_minutes);
89 static unsigned int rt_timer_irq;
91 void ip27_rt_timer_interrupt(void)
93 int cpu = smp_processor_id();
94 int cpuA = cputoslice(cpu) == 0;
95 unsigned int irq = rt_timer_irq;
98 write_seqlock(&xtime_lock);
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]);
105 if (LOCAL_HUB_L(PI_RT_COUNT) >= ct_cur[cpu])
108 kstat_this_cpu.irqs[irq]++; /* kstat only for bootcpu? */
113 update_process_times(user_mode(get_irq_regs()));
115 write_sequnlock(&xtime_lock);
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>
126 unsigned long read_persistent_clock(void)
128 unsigned int year, month, date, hour, min, sec;
129 struct m48t35_rtc *rtc;
133 rtc = (struct m48t35_rtc *)(KL_CONFIG_CH_CONS_INFO(nid)->memory_base +
136 rtc->control |= M48T35_RTC_READ;
143 rtc->control &= ~M48T35_RTC_READ;
147 hour = BCD2BIN(hour);
148 date = BCD2BIN(date);
149 month = BCD2BIN(month);
150 year = BCD2BIN(year);
154 return mktime(year, month, date, hour, min, sec);
157 static void enable_rt_irq(unsigned int irq)
161 static void disable_rt_irq(unsigned int irq)
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,
174 static struct irqaction rt_irqaction = {
175 .handler = (irq_handler_t) ip27_rt_timer_interrupt,
176 .flags = IRQF_DISABLED,
177 .mask = CPU_MASK_NONE,
181 void __init plat_timer_setup(struct irqaction *irq)
183 int irqno = allocate_irqno();
186 panic("Can't allocate interrupt number for timer interrupt");
188 set_irq_chip_and_handler(irqno, &rt_irq_type, handle_percpu_irq);
190 /* over-write the handler, we use our own way */
191 irq->handler = no_action;
193 /* setup irqaction */
194 irq_desc[irqno].status |= IRQ_PER_CPU;
196 rt_timer_irq = irqno;
198 * Only needed to get /proc/interrupt to display timer irq stats
200 setup_irq(irqno, &rt_irqaction);
203 static cycle_t hub_rt_read(void)
205 return REMOTE_HUB_L(cputonasid(0), PI_RT_COUNT);
208 struct clocksource ht_rt_clocksource = {
212 .mask = CLOCKSOURCE_MASK(52),
214 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
217 void __init plat_time_init(void)
219 clocksource_register(&ht_rt_clocksource);
222 void __init cpu_time_init(void)
228 /* Don't use ARCS. ARCS is fragile. Klconfig is simple and sane. */
229 board = find_lboard(KL_CONFIG_INFO(get_nasid()), KLTYPE_IP27);
231 panic("Can't find board info for myself.");
233 cpuid = LOCAL_HUB_L(PI_CPU_NUM) ? IP27_CPU0_INDEX : IP27_CPU1_INDEX;
234 cpu = (klcpu_t *) KLCF_COMP(board, cpuid);
236 panic("No information about myself?");
238 printk("CPU %d clock is %dMHz.\n", smp_processor_id(), cpu->cpu_speed);
240 set_c0_status(SRB_TIMOCLK);
243 void __init hub_rtc_init(cnodeid_t cnode)
246 * We only need to initialize the current node.
247 * If this is not the current node then it is a cpuless
248 * node and timeouts will not happen there.
250 if (get_compact_nodeid() == cnode) {
251 int cpu = smp_processor_id();
252 LOCAL_HUB_S(PI_RT_EN_A, 1);
253 LOCAL_HUB_S(PI_RT_EN_B, 1);
254 LOCAL_HUB_S(PI_PROF_EN_A, 0);
255 LOCAL_HUB_S(PI_PROF_EN_B, 0);
256 ct_cur[cpu] = CYCLES_PER_JIFFY;
257 LOCAL_HUB_S(PI_RT_COMPARE_A, ct_cur[cpu]);
258 LOCAL_HUB_S(PI_RT_COUNT, 0);
259 LOCAL_HUB_S(PI_RT_PEND_A, 0);
260 LOCAL_HUB_S(PI_RT_COMPARE_B, ct_cur[cpu]);
261 LOCAL_HUB_S(PI_RT_COUNT, 0);
262 LOCAL_HUB_S(PI_RT_PEND_B, 0);