Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Ralf Baechle | 54d0a21 | 2006-07-09 21:38:56 +0100 | [diff] [blame] | 2 | * Copytight (C) 1999, 2000, 05, 06 Ralf Baechle (ralf@linux-mips.org) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | #if 0 |
| 46 | static int set_rtc_mmss(unsigned long nowtime) |
| 47 | { |
| 48 | int retval = 0; |
| 49 | int real_seconds, real_minutes, cmos_minutes; |
| 50 | struct m48t35_rtc *rtc; |
| 51 | nasid_t nid; |
| 52 | |
| 53 | nid = get_nasid(); |
| 54 | rtc = (struct m48t35_rtc *)(KL_CONFIG_CH_CONS_INFO(nid)->memory_base + |
| 55 | IOC3_BYTEBUS_DEV0); |
| 56 | |
| 57 | rtc->control |= M48T35_RTC_READ; |
| 58 | cmos_minutes = BCD2BIN(rtc->min); |
| 59 | rtc->control &= ~M48T35_RTC_READ; |
| 60 | |
| 61 | /* |
| 62 | * Since we're only adjusting minutes and seconds, don't interfere with |
| 63 | * hour overflow. This avoids messing with unknown time zones but |
| 64 | * requires your RTC not to be off by more than 15 minutes |
| 65 | */ |
| 66 | real_seconds = nowtime % 60; |
| 67 | real_minutes = nowtime / 60; |
| 68 | if (((abs(real_minutes - cmos_minutes) + 15)/30) & 1) |
| 69 | real_minutes += 30; /* correct for half hour time zone */ |
| 70 | real_minutes %= 60; |
| 71 | |
| 72 | if (abs(real_minutes - cmos_minutes) < 30) { |
| 73 | real_seconds = BIN2BCD(real_seconds); |
| 74 | real_minutes = BIN2BCD(real_minutes); |
| 75 | rtc->control |= M48T35_RTC_SET; |
| 76 | rtc->sec = real_seconds; |
| 77 | rtc->min = real_minutes; |
| 78 | rtc->control &= ~M48T35_RTC_SET; |
| 79 | } else { |
| 80 | printk(KERN_WARNING |
| 81 | "set_rtc_mmss: can't update from %d to %d\n", |
| 82 | cmos_minutes, real_minutes); |
| 83 | retval = -1; |
| 84 | } |
| 85 | |
| 86 | return retval; |
| 87 | } |
| 88 | #endif |
| 89 | |
Ralf Baechle | 3c00944 | 2006-06-16 17:10:49 +0200 | [diff] [blame] | 90 | static unsigned int rt_timer_irq; |
| 91 | |
Ralf Baechle | 937a801 | 2006-10-07 19:44:33 +0100 | [diff] [blame] | 92 | void ip27_rt_timer_interrupt(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | { |
| 94 | int cpu = smp_processor_id(); |
| 95 | int cpuA = cputoslice(cpu) == 0; |
Ralf Baechle | 3c00944 | 2006-06-16 17:10:49 +0200 | [diff] [blame] | 96 | unsigned int irq = rt_timer_irq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | |
| 98 | irq_enter(); |
| 99 | write_seqlock(&xtime_lock); |
| 100 | |
| 101 | again: |
| 102 | LOCAL_HUB_S(cpuA ? PI_RT_PEND_A : PI_RT_PEND_B, 0); /* Ack */ |
| 103 | ct_cur[cpu] += CYCLES_PER_JIFFY; |
| 104 | LOCAL_HUB_S(cpuA ? PI_RT_COMPARE_A : PI_RT_COMPARE_B, ct_cur[cpu]); |
| 105 | |
| 106 | if (LOCAL_HUB_L(PI_RT_COUNT) >= ct_cur[cpu]) |
| 107 | goto again; |
| 108 | |
| 109 | kstat_this_cpu.irqs[irq]++; /* kstat only for bootcpu? */ |
| 110 | |
| 111 | if (cpu == 0) |
Atsushi Nemoto | 3171a03 | 2006-09-29 02:00:32 -0700 | [diff] [blame] | 112 | do_timer(1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | |
Ralf Baechle | 937a801 | 2006-10-07 19:44:33 +0100 | [diff] [blame] | 114 | update_process_times(user_mode(get_irq_regs())); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | |
| 116 | /* |
| 117 | * If we have an externally synchronized Linux clock, then update |
| 118 | * RTC clock accordingly every ~11 minutes. Set_rtc_mmss() has to be |
| 119 | * called as close as possible to when a second starts. |
| 120 | */ |
john stultz | b149ee2 | 2005-09-06 15:17:46 -0700 | [diff] [blame] | 121 | if (ntp_synced() && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | xtime.tv_sec > last_rtc_update + 660 && |
| 123 | (xtime.tv_nsec / 1000) >= 500000 - ((unsigned) TICK_SIZE) / 2 && |
| 124 | (xtime.tv_nsec / 1000) <= 500000 + ((unsigned) TICK_SIZE) / 2) { |
Atsushi Nemoto | c0858d8 | 2006-04-20 00:12:05 +0900 | [diff] [blame] | 125 | if (rtc_mips_set_time(xtime.tv_sec) == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | last_rtc_update = xtime.tv_sec; |
| 127 | } else { |
| 128 | last_rtc_update = xtime.tv_sec - 600; |
| 129 | /* do it again in 60 s */ |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | write_sequnlock(&xtime_lock); |
| 134 | irq_exit(); |
| 135 | } |
| 136 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | /* Includes for ioc3_init(). */ |
| 138 | #include <asm/sn/types.h> |
| 139 | #include <asm/sn/sn0/addrs.h> |
| 140 | #include <asm/sn/sn0/hubni.h> |
| 141 | #include <asm/sn/sn0/hubio.h> |
| 142 | #include <asm/pci/bridge.h> |
| 143 | |
| 144 | static __init unsigned long get_m48t35_time(void) |
| 145 | { |
| 146 | unsigned int year, month, date, hour, min, sec; |
| 147 | struct m48t35_rtc *rtc; |
| 148 | nasid_t nid; |
| 149 | |
| 150 | nid = get_nasid(); |
| 151 | rtc = (struct m48t35_rtc *)(KL_CONFIG_CH_CONS_INFO(nid)->memory_base + |
| 152 | IOC3_BYTEBUS_DEV0); |
| 153 | |
| 154 | rtc->control |= M48T35_RTC_READ; |
| 155 | sec = rtc->sec; |
| 156 | min = rtc->min; |
| 157 | hour = rtc->hour; |
| 158 | date = rtc->date; |
| 159 | month = rtc->month; |
| 160 | year = rtc->year; |
| 161 | rtc->control &= ~M48T35_RTC_READ; |
| 162 | |
| 163 | sec = BCD2BIN(sec); |
| 164 | min = BCD2BIN(min); |
| 165 | hour = BCD2BIN(hour); |
| 166 | date = BCD2BIN(date); |
| 167 | month = BCD2BIN(month); |
| 168 | year = BCD2BIN(year); |
| 169 | |
| 170 | year += 1970; |
| 171 | |
| 172 | return mktime(year, month, date, hour, min, sec); |
| 173 | } |
| 174 | |
Ralf Baechle | 3c00944 | 2006-06-16 17:10:49 +0200 | [diff] [blame] | 175 | static void enable_rt_irq(unsigned int irq) |
| 176 | { |
| 177 | } |
| 178 | |
| 179 | static void disable_rt_irq(unsigned int irq) |
| 180 | { |
| 181 | } |
| 182 | |
Ralf Baechle | 3c00944 | 2006-06-16 17:10:49 +0200 | [diff] [blame] | 183 | static void end_rt_irq(unsigned int irq) |
| 184 | { |
| 185 | } |
| 186 | |
Ralf Baechle | 94dee17 | 2006-07-02 14:41:42 +0100 | [diff] [blame] | 187 | static struct irq_chip rt_irq_type = { |
Ralf Baechle | 3c00944 | 2006-06-16 17:10:49 +0200 | [diff] [blame] | 188 | .typename = "SN HUB RT timer", |
Atsushi Nemoto | 1603b5a | 2006-11-02 02:08:36 +0900 | [diff] [blame] | 189 | .ack = disable_rt_irq, |
| 190 | .mask = disable_rt_irq, |
| 191 | .mask_ack = disable_rt_irq, |
| 192 | .unmask = enable_rt_irq, |
Atsushi Nemoto | 1417836 | 2006-11-14 01:13:18 +0900 | [diff] [blame^] | 193 | .eoi = enable_rt_irq, |
Ralf Baechle | 3c00944 | 2006-06-16 17:10:49 +0200 | [diff] [blame] | 194 | .end = end_rt_irq, |
| 195 | }; |
| 196 | |
| 197 | static struct irqaction rt_irqaction = { |
| 198 | .handler = ip27_rt_timer_interrupt, |
Thomas Gleixner | f40298f | 2006-07-01 19:29:20 -0700 | [diff] [blame] | 199 | .flags = IRQF_DISABLED, |
Ralf Baechle | 3c00944 | 2006-06-16 17:10:49 +0200 | [diff] [blame] | 200 | .mask = CPU_MASK_NONE, |
| 201 | .name = "timer" |
| 202 | }; |
| 203 | |
Ralf Baechle | 54d0a21 | 2006-07-09 21:38:56 +0100 | [diff] [blame] | 204 | void __init plat_timer_setup(struct irqaction *irq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | { |
Ralf Baechle | 3c00944 | 2006-06-16 17:10:49 +0200 | [diff] [blame] | 206 | int irqno = allocate_irqno(); |
| 207 | |
| 208 | if (irqno < 0) |
| 209 | panic("Can't allocate interrupt number for timer interrupt"); |
| 210 | |
Atsushi Nemoto | 1417836 | 2006-11-14 01:13:18 +0900 | [diff] [blame^] | 211 | set_irq_chip_and_handler(irqno, &rt_irq_type, handle_percpu_irq); |
Ralf Baechle | 3c00944 | 2006-06-16 17:10:49 +0200 | [diff] [blame] | 212 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | /* over-write the handler, we use our own way */ |
| 214 | irq->handler = no_action; |
| 215 | |
| 216 | /* setup irqaction */ |
Ralf Baechle | 3c00944 | 2006-06-16 17:10:49 +0200 | [diff] [blame] | 217 | irq_desc[irqno].status |= IRQ_PER_CPU; |
| 218 | |
| 219 | rt_timer_irq = irqno; |
Ralf Baechle | bf28363 | 2006-07-07 23:56:32 +0100 | [diff] [blame] | 220 | /* |
| 221 | * Only needed to get /proc/interrupt to display timer irq stats |
| 222 | */ |
| 223 | setup_irq(irqno, &rt_irqaction); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 | } |
| 225 | |
Atsushi Nemoto | 16b7b2a | 2006-10-24 00:21:27 +0900 | [diff] [blame] | 226 | static unsigned int ip27_hpt_read(void) |
| 227 | { |
| 228 | return REMOTE_HUB_L(cputonasid(0), PI_RT_COUNT); |
| 229 | } |
| 230 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 231 | void __init ip27_time_init(void) |
| 232 | { |
Atsushi Nemoto | 16b7b2a | 2006-10-24 00:21:27 +0900 | [diff] [blame] | 233 | mips_hpt_read = ip27_hpt_read; |
| 234 | mips_hpt_frequency = CYCLES_PER_SEC; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | xtime.tv_sec = get_m48t35_time(); |
| 236 | xtime.tv_nsec = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | void __init cpu_time_init(void) |
| 240 | { |
| 241 | lboard_t *board; |
| 242 | klcpu_t *cpu; |
| 243 | int cpuid; |
| 244 | |
| 245 | /* Don't use ARCS. ARCS is fragile. Klconfig is simple and sane. */ |
| 246 | board = find_lboard(KL_CONFIG_INFO(get_nasid()), KLTYPE_IP27); |
| 247 | if (!board) |
| 248 | panic("Can't find board info for myself."); |
| 249 | |
| 250 | cpuid = LOCAL_HUB_L(PI_CPU_NUM) ? IP27_CPU0_INDEX : IP27_CPU1_INDEX; |
| 251 | cpu = (klcpu_t *) KLCF_COMP(board, cpuid); |
| 252 | if (!cpu) |
| 253 | panic("No information about myself?"); |
| 254 | |
| 255 | printk("CPU %d clock is %dMHz.\n", smp_processor_id(), cpu->cpu_speed); |
| 256 | |
| 257 | set_c0_status(SRB_TIMOCLK); |
| 258 | } |
| 259 | |
| 260 | void __init hub_rtc_init(cnodeid_t cnode) |
| 261 | { |
| 262 | /* |
| 263 | * We only need to initialize the current node. |
| 264 | * If this is not the current node then it is a cpuless |
| 265 | * node and timeouts will not happen there. |
| 266 | */ |
| 267 | if (get_compact_nodeid() == cnode) { |
| 268 | int cpu = smp_processor_id(); |
| 269 | LOCAL_HUB_S(PI_RT_EN_A, 1); |
| 270 | LOCAL_HUB_S(PI_RT_EN_B, 1); |
| 271 | LOCAL_HUB_S(PI_PROF_EN_A, 0); |
| 272 | LOCAL_HUB_S(PI_PROF_EN_B, 0); |
| 273 | ct_cur[cpu] = CYCLES_PER_JIFFY; |
| 274 | LOCAL_HUB_S(PI_RT_COMPARE_A, ct_cur[cpu]); |
| 275 | LOCAL_HUB_S(PI_RT_COUNT, 0); |
| 276 | LOCAL_HUB_S(PI_RT_PEND_A, 0); |
| 277 | LOCAL_HUB_S(PI_RT_COMPARE_B, ct_cur[cpu]); |
| 278 | LOCAL_HUB_S(PI_RT_COUNT, 0); |
| 279 | LOCAL_HUB_S(PI_RT_PEND_B, 0); |
| 280 | } |
| 281 | } |