Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 | /* * indy_timer.c: Setting up the clock on the INDY 8254 controller. * * Copyright (C) 1996 David S. Miller (dm@engr.sgi.com) * Copytight (C) 1997, 1998 Ralf Baechle (ralf@gnu.org) */ #include <linux/errno.h> #include <linux/init.h> #include <linux/sched.h> #include <linux/kernel.h> #include <linux/param.h> #include <linux/string.h> #include <linux/mm.h> #include <linux/interrupt.h> #include <linux/timex.h> #include <linux/kernel_stat.h> #include <asm/bootinfo.h> #include <asm/io.h> #include <asm/irq.h> #include <asm/ptrace.h> #include <asm/system.h> #include <asm/sgi/sgi.h> #include <asm/sgi/sgihpc.h> #include <asm/sgi/sgint23.h> #include <asm/sgialib.h> /* Because of a bug in the i8254 timer we need to use the onchip r4k * counter as our system wide timer interrupt running at 100HZ. */ static unsigned long r4k_offset; /* Amount to increment compare reg each time */ static unsigned long r4k_cur; /* What counter should be at next timer irq */ extern rwlock_t xtime_lock; static inline void ack_r4ktimer(unsigned long newval) { write_32bit_cp0_register(CP0_COMPARE, newval); } static int set_rtc_mmss(unsigned long nowtime) { struct indy_clock *clock = (struct indy_clock *)INDY_CLOCK_REGS; int retval = 0; int real_seconds, real_minutes, clock_minutes; #define FROB_FROM_CLOCK(x) (((x) & 0xf) | ((((x) & 0xf0) >> 4) * 10)); #define FROB_TO_CLOCK(x) ((((((x) & 0xff) / 10)<<4) | (((x) & 0xff) % 10)) & 0xff) clock->cmd &= ~(0x80); clock_minutes = clock->min; clock->cmd |= (0x80); clock_minutes = FROB_FROM_CLOCK(clock_minutes); real_seconds = nowtime % 60; real_minutes = nowtime / 60; if(((abs(real_minutes - clock_minutes) + 15)/30) & 1) real_minutes += 30; /* correct for half hour time zone */ real_minutes %= 60; if(abs(real_minutes - clock_minutes) < 30) { /* Force clock oscillator to be on. */ clock->month &= ~(0x80); /* Write real_seconds and real_minutes into the Dallas. */ clock->cmd &= ~(0x80); clock->sec = real_seconds; clock->min = real_minutes; clock->cmd |= (0x80); } else return -1; #undef FROB_FROM_CLOCK #undef FROB_TO_CLOCK return retval; } static long last_rtc_update; unsigned long missed_heart_beats; void indy_timer_interrupt(struct pt_regs *regs) { unsigned long count; int irq = 7; write_lock(&xtime_lock); /* Ack timer and compute new compare. */ count = read_32bit_cp0_register(CP0_COUNT); /* This has races. */ if ((count - r4k_cur) >= r4k_offset) { /* If this happens to often we'll need to compensate. */ missed_heart_beats++; r4k_cur = count + r4k_offset; } else r4k_cur += r4k_offset; ack_r4ktimer(r4k_cur); kstat.irqs[0][irq]++; do_timer(regs); /* We update the Dallas time of day approx. every 11 minutes, * because of how the numbers work out we need to make * absolutely sure we do this update within 500ms before the * next second starts, thus the following code. */ if ((time_status & STA_UNSYNC) == 0 && xtime.tv_sec > last_rtc_update + 660 && xtime.tv_usec >= 500000 - (tick >> 1) && xtime.tv_usec <= 500000 + (tick >> 1)) { if (set_rtc_mmss(xtime.tv_sec) == 0) last_rtc_update = xtime.tv_sec; else /* do it again in 60s */ last_rtc_update = xtime.tv_sec - 600; } write_unlock(&xtime_lock); } static unsigned long dosample(volatile unsigned char *tcwp, volatile unsigned char *tc2p) { unsigned long ct0, ct1; unsigned char msb, lsb; /* Start the counter. */ *tcwp = (SGINT_TCWORD_CNT2 | SGINT_TCWORD_CALL | SGINT_TCWORD_MRGEN); *tc2p = (SGINT_TCSAMP_COUNTER & 0xff); *tc2p = (SGINT_TCSAMP_COUNTER >> 8); /* Get initial counter invariant */ ct0 = read_32bit_cp0_register(CP0_COUNT); /* Latch and spin until top byte of counter2 is zero */ do { *tcwp = (SGINT_TCWORD_CNT2 | SGINT_TCWORD_CLAT); lsb = *tc2p; msb = *tc2p; ct1 = read_32bit_cp0_register(CP0_COUNT); } while(msb); /* Stop the counter. */ *tcwp = (SGINT_TCWORD_CNT2 | SGINT_TCWORD_CALL | SGINT_TCWORD_MSWST); /* Return the difference, this is how far the r4k counter increments * for every one HZ. */ return ct1 - ct0; } static unsigned long __init get_indy_time(void) { struct indy_clock *clock = (struct indy_clock *)INDY_CLOCK_REGS; unsigned int year, mon, day, hour, min, sec; /* Freeze it. */ clock->cmd &= ~(0x80); /* Read regs. */ sec = clock->sec; min = clock->min; hour = (clock->hr & 0x3f); day = (clock->date & 0x3f); mon = (clock->month & 0x1f); year = clock->year; /* Unfreeze clock. */ clock->cmd |= 0x80; /* Frob the bits. */ #define FROB1(x) (((x) & 0xf) + ((((x) & 0xf0) >> 4) * 10)); #define FROB2(x) (((x) & 0xf) + (((((x) & 0xf0) >> 4) & 0x3) * 10)); /* XXX Should really check that secs register is the same * XXX as when we first read it and if not go back and * XXX read the regs above again. */ sec = FROB1(sec); min = FROB1(min); day = FROB1(day); mon = FROB1(mon); year = FROB1(year); hour = FROB2(hour); #undef FROB1 #undef FROB2 /* Wheee... */ if(year < 45) year += 30; if ((year += 1940) < 1970) year += 100; return mktime(year, mon, day, hour, min, sec); } #define ALLINTS (IE_IRQ0 | IE_IRQ1 | IE_IRQ2 | IE_IRQ3 | IE_IRQ4 | IE_IRQ5) void __init indy_timer_init(void) { struct sgi_ioc_timers *p; volatile unsigned char *tcwp, *tc2p; /* Figure out the r4k offset, the algorithm is very simple and works * in _all_ cases as long as the 8254 counter register itself works ok * (as an interrupt driving timer it does not because of bug, this is * why we are using the onchip r4k counter/compare register to serve * this purpose, but for r4k_offset calculation it will work ok for us). * There are other very complicated ways of performing this calculation * but this one works just fine so I am not going to futz around. ;-) */ p = ioc_timers; tcwp = &p->tcword; tc2p = &p->tcnt2; printk("calculating r4koff... "); dosample(tcwp, tc2p); /* First sample. */ dosample(tcwp, tc2p); /* Eat one. */ r4k_offset = dosample(tcwp, tc2p); /* Second sample. */ printk("%08lx(%d)\n", r4k_offset, (int) r4k_offset); r4k_cur = (read_32bit_cp0_register(CP0_COUNT) + r4k_offset); write_32bit_cp0_register(CP0_COMPARE, r4k_cur); set_cp0_status(ST0_IM, ALLINTS); sti(); write_lock_irq(&xtime_lock); xtime.tv_sec = get_indy_time(); /* Read time from RTC. */ xtime.tv_usec = 0; write_unlock_irq(&xtime_lock); } void indy_8254timer_irq(void) { int cpu = smp_processor_id(); int irq = 4; irq_enter(cpu, irq); kstat.irqs[0][irq]++; panic("indy_8254timer_irq: Whoops, should not have gotten this IRQ\n"); irq_exit(cpu, irq); } void do_gettimeofday(struct timeval *tv) { unsigned long flags; read_lock_irqsave(&xtime_lock, flags); *tv = xtime; read_unlock_irqrestore(&xtime_lock, flags); } void do_settimeofday(struct timeval *tv) { write_lock_irq(&xtime_lock); xtime = *tv; time_adjust = 0; /* stop active adjtime() */ time_status |= STA_UNSYNC; time_maxerror = NTP_PHASE_LIMIT; time_esterror = NTP_PHASE_LIMIT; write_unlock_irq(&xtime_lock); } |