blob: 9c1700e85be071bdcfeb8e32733cb7faed22ab65 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Ralf Baechle54d0a212006-07-09 21:38:56 +01002 * Copytight (C) 1999, 2000, 05, 06 Ralf Baechle (ralf@linux-mips.org)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * 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
42static unsigned long ct_cur[NR_CPUS]; /* What counter should be at next timer irq */
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#if 0
45static 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
Ralf Baechle3c009442006-06-16 17:10:49 +020089static unsigned int rt_timer_irq;
90
Ralf Baechle937a8012006-10-07 19:44:33 +010091void ip27_rt_timer_interrupt(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070092{
93 int cpu = smp_processor_id();
94 int cpuA = cputoslice(cpu) == 0;
Ralf Baechle3c009442006-06-16 17:10:49 +020095 unsigned int irq = rt_timer_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
97 irq_enter();
98 write_seqlock(&xtime_lock);
99
100again:
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)
Atsushi Nemoto3171a032006-09-29 02:00:32 -0700111 do_timer(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
Ralf Baechle937a8012006-10-07 19:44:33 +0100113 update_process_times(user_mode(get_irq_regs()));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 write_sequnlock(&xtime_lock);
116 irq_exit();
117}
118
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119/* 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
Ralf Baechle4b550482007-10-11 23:46:08 +0100126unsigned long read_persistent_clock(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127{
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
Ralf Baechle3c009442006-06-16 17:10:49 +0200157static void enable_rt_irq(unsigned int irq)
158{
159}
160
161static void disable_rt_irq(unsigned int irq)
162{
163}
164
Ralf Baechle94dee172006-07-02 14:41:42 +0100165static struct irq_chip rt_irq_type = {
Atsushi Nemoto70d21cd2007-01-15 00:07:25 +0900166 .name = "SN HUB RT timer",
Atsushi Nemoto1603b5a2006-11-02 02:08:36 +0900167 .ack = disable_rt_irq,
168 .mask = disable_rt_irq,
169 .mask_ack = disable_rt_irq,
170 .unmask = enable_rt_irq,
Atsushi Nemoto14178362006-11-14 01:13:18 +0900171 .eoi = enable_rt_irq,
Ralf Baechle3c009442006-06-16 17:10:49 +0200172};
173
174static struct irqaction rt_irqaction = {
Ralf Baechlef8aeb852007-02-14 10:18:59 +0000175 .handler = (irq_handler_t) ip27_rt_timer_interrupt,
Thomas Gleixnerf40298f2006-07-01 19:29:20 -0700176 .flags = IRQF_DISABLED,
Ralf Baechle3c009442006-06-16 17:10:49 +0200177 .mask = CPU_MASK_NONE,
178 .name = "timer"
179};
180
Ralf Baechle54d0a212006-07-09 21:38:56 +0100181void __init plat_timer_setup(struct irqaction *irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182{
Ralf Baechle3c009442006-06-16 17:10:49 +0200183 int irqno = allocate_irqno();
184
185 if (irqno < 0)
186 panic("Can't allocate interrupt number for timer interrupt");
187
Atsushi Nemoto14178362006-11-14 01:13:18 +0900188 set_irq_chip_and_handler(irqno, &rt_irq_type, handle_percpu_irq);
Ralf Baechle3c009442006-06-16 17:10:49 +0200189
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 /* over-write the handler, we use our own way */
191 irq->handler = no_action;
192
193 /* setup irqaction */
Ralf Baechle3c009442006-06-16 17:10:49 +0200194 irq_desc[irqno].status |= IRQ_PER_CPU;
195
196 rt_timer_irq = irqno;
Ralf Baechlebf283632006-07-07 23:56:32 +0100197 /*
198 * Only needed to get /proc/interrupt to display timer irq stats
199 */
200 setup_irq(irqno, &rt_irqaction);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201}
202
Atsushi Nemoto00598562006-11-12 00:10:28 +0900203static cycle_t ip27_hpt_read(void)
Atsushi Nemoto16b7b2a2006-10-24 00:21:27 +0900204{
205 return REMOTE_HUB_L(cputonasid(0), PI_RT_COUNT);
206}
207
Ralf Baechle4b550482007-10-11 23:46:08 +0100208void __init plat_time_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209{
Atsushi Nemoto00598562006-11-12 00:10:28 +0900210 clocksource_mips.read = ip27_hpt_read;
Atsushi Nemoto16b7b2a2006-10-24 00:21:27 +0900211 mips_hpt_frequency = CYCLES_PER_SEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212}
213
214void __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
235void __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}