Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * |
| 3 | * Copyright (C) 2001 MontaVista Software, ppopov@mvista.com |
| 4 | * Copied and modified Carsten Langgaard's time.c |
| 5 | * |
| 6 | * Carsten Langgaard, carstenl@mips.com |
| 7 | * Copyright (C) 1999,2000 MIPS Technologies, Inc. All rights reserved. |
| 8 | * |
| 9 | * ######################################################################## |
| 10 | * |
| 11 | * This program is free software; you can distribute it and/or modify it |
| 12 | * under the terms of the GNU General Public License (Version 2) as |
| 13 | * published by the Free Software Foundation. |
| 14 | * |
| 15 | * This program is distributed in the hope it will be useful, but WITHOUT |
| 16 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 17 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 18 | * for more details. |
| 19 | * |
| 20 | * You should have received a copy of the GNU General Public License along |
| 21 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 22 | * 59 Temple Place - Suite 330, Boston MA 02111-1307, USA. |
| 23 | * |
| 24 | * ######################################################################## |
| 25 | * |
| 26 | * Setting up the clock on the MIPS boards. |
| 27 | * |
| 28 | * Update. Always configure the kernel with CONFIG_NEW_TIME_C. This |
| 29 | * will use the user interface gettimeofday() functions from the |
| 30 | * arch/mips/kernel/time.c, and we provide the clock interrupt processing |
| 31 | * and the timer offset compute functions. If CONFIG_PM is selected, |
| 32 | * we also ensure the 32KHz timer is available. -- Dan |
| 33 | */ |
| 34 | |
| 35 | #include <linux/types.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | #include <linux/init.h> |
| 37 | #include <linux/kernel_stat.h> |
| 38 | #include <linux/sched.h> |
| 39 | #include <linux/spinlock.h> |
| 40 | #include <linux/hardirq.h> |
| 41 | |
| 42 | #include <asm/compiler.h> |
| 43 | #include <asm/mipsregs.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | #include <asm/time.h> |
| 45 | #include <asm/div64.h> |
| 46 | #include <asm/mach-au1x00/au1000.h> |
| 47 | |
| 48 | #include <linux/mc146818rtc.h> |
| 49 | #include <linux/timex.h> |
| 50 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | static unsigned long r4k_offset; /* Amount to increment compare reg each time */ |
| 52 | static unsigned long r4k_cur; /* What counter should be at next timer irq */ |
| 53 | int no_au1xxx_32khz; |
Pete Popov | fe359bf | 2005-04-08 08:34:43 +0000 | [diff] [blame] | 54 | extern int allow_au1k_wait; /* default off for CP0 Counter */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | #ifdef CONFIG_PM |
Pete Popov | 3ce86ee | 2005-07-19 07:05:36 +0000 | [diff] [blame] | 57 | #if HZ < 100 || HZ > 1000 |
| 58 | #error "unsupported HZ value! Must be in [100,1000]" |
| 59 | #endif |
| 60 | #define MATCH20_INC (328*100/HZ) /* magic number 328 is for HZ=100... */ |
David Howells | 40220c1 | 2006-10-09 12:19:47 +0100 | [diff] [blame] | 61 | extern void startup_match20_interrupt(irq_handler_t handler); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | static unsigned long last_pc0, last_match20; |
| 63 | #endif |
| 64 | |
| 65 | static DEFINE_SPINLOCK(time_lock); |
| 66 | |
| 67 | static inline void ack_r4ktimer(unsigned long newval) |
| 68 | { |
| 69 | write_c0_compare(newval); |
| 70 | } |
| 71 | |
| 72 | /* |
| 73 | * There are a lot of conceptually broken versions of the MIPS timer interrupt |
| 74 | * handler floating around. This one is rather different, but the algorithm |
| 75 | * is provably more robust. |
| 76 | */ |
| 77 | unsigned long wtimer; |
Ralf Baechle | 937a801 | 2006-10-07 19:44:33 +0100 | [diff] [blame] | 78 | |
| 79 | void mips_timer_interrupt(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | { |
| 81 | int irq = 63; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | |
| 83 | irq_enter(); |
| 84 | kstat_this_cpu.irqs[irq]++; |
| 85 | |
| 86 | if (r4k_offset == 0) |
| 87 | goto null; |
| 88 | |
| 89 | do { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | kstat_this_cpu.irqs[irq]++; |
Atsushi Nemoto | 3171a03 | 2006-09-29 02:00:32 -0700 | [diff] [blame] | 91 | do_timer(1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | #ifndef CONFIG_SMP |
Ralf Baechle | 937a801 | 2006-10-07 19:44:33 +0100 | [diff] [blame] | 93 | update_process_times(user_mode(get_irq_regs())); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | #endif |
| 95 | r4k_cur += r4k_offset; |
| 96 | ack_r4ktimer(r4k_cur); |
| 97 | |
| 98 | } while (((unsigned long)read_c0_count() |
| 99 | - r4k_cur) < 0x7fffffff); |
| 100 | |
| 101 | irq_exit(); |
| 102 | return; |
| 103 | |
| 104 | null: |
| 105 | ack_r4ktimer(0); |
Herbert Valerio Riedel | 343fdc3 | 2006-04-12 09:03:08 +0200 | [diff] [blame] | 106 | irq_exit(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | #ifdef CONFIG_PM |
Ralf Baechle | 937a801 | 2006-10-07 19:44:33 +0100 | [diff] [blame] | 110 | irqreturn_t counter0_irq(int irq, void *dev_id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | { |
| 112 | unsigned long pc0; |
| 113 | int time_elapsed; |
| 114 | static int jiffie_drift = 0; |
| 115 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | if (au_readl(SYS_COUNTER_CNTRL) & SYS_CNTRL_M20) { |
| 117 | /* should never happen! */ |
Pete Popov | 3ce86ee | 2005-07-19 07:05:36 +0000 | [diff] [blame] | 118 | printk(KERN_WARNING "counter 0 w status error\n"); |
| 119 | return IRQ_NONE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | pc0 = au_readl(SYS_TOYREAD); |
| 123 | if (pc0 < last_match20) { |
| 124 | /* counter overflowed */ |
| 125 | time_elapsed = (0xffffffff - last_match20) + pc0; |
| 126 | } |
| 127 | else { |
| 128 | time_elapsed = pc0 - last_match20; |
| 129 | } |
| 130 | |
| 131 | while (time_elapsed > 0) { |
Atsushi Nemoto | 3171a03 | 2006-09-29 02:00:32 -0700 | [diff] [blame] | 132 | do_timer(1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | #ifndef CONFIG_SMP |
Ralf Baechle | 937a801 | 2006-10-07 19:44:33 +0100 | [diff] [blame] | 134 | update_process_times(user_mode(get_irq_regs())); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | #endif |
| 136 | time_elapsed -= MATCH20_INC; |
| 137 | last_match20 += MATCH20_INC; |
| 138 | jiffie_drift++; |
| 139 | } |
| 140 | |
| 141 | last_pc0 = pc0; |
| 142 | au_writel(last_match20 + MATCH20_INC, SYS_TOYMATCH2); |
| 143 | au_sync(); |
| 144 | |
| 145 | /* our counter ticks at 10.009765625 ms/tick, we we're running |
| 146 | * almost 10uS too slow per tick. |
| 147 | */ |
| 148 | |
| 149 | if (jiffie_drift >= 999) { |
| 150 | jiffie_drift -= 999; |
Atsushi Nemoto | 3171a03 | 2006-09-29 02:00:32 -0700 | [diff] [blame] | 151 | do_timer(1); /* increment jiffies by one */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | #ifndef CONFIG_SMP |
Ralf Baechle | 937a801 | 2006-10-07 19:44:33 +0100 | [diff] [blame] | 153 | update_process_times(user_mode(get_irq_regs())); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | #endif |
| 155 | } |
Pete Popov | 3ce86ee | 2005-07-19 07:05:36 +0000 | [diff] [blame] | 156 | |
| 157 | return IRQ_HANDLED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | /* When we wakeup from sleep, we have to "catch up" on all of the |
| 161 | * timer ticks we have missed. |
| 162 | */ |
| 163 | void |
| 164 | wakeup_counter0_adjust(void) |
| 165 | { |
| 166 | unsigned long pc0; |
| 167 | int time_elapsed; |
| 168 | |
| 169 | pc0 = au_readl(SYS_TOYREAD); |
| 170 | if (pc0 < last_match20) { |
| 171 | /* counter overflowed */ |
| 172 | time_elapsed = (0xffffffff - last_match20) + pc0; |
| 173 | } |
| 174 | else { |
| 175 | time_elapsed = pc0 - last_match20; |
| 176 | } |
| 177 | |
| 178 | while (time_elapsed > 0) { |
| 179 | time_elapsed -= MATCH20_INC; |
| 180 | last_match20 += MATCH20_INC; |
| 181 | } |
| 182 | |
| 183 | last_pc0 = pc0; |
| 184 | au_writel(last_match20 + MATCH20_INC, SYS_TOYMATCH2); |
| 185 | au_sync(); |
| 186 | |
| 187 | } |
| 188 | |
| 189 | /* This is just for debugging to set the timer for a sleep delay. |
| 190 | */ |
| 191 | void |
| 192 | wakeup_counter0_set(int ticks) |
| 193 | { |
| 194 | unsigned long pc0; |
| 195 | |
| 196 | pc0 = au_readl(SYS_TOYREAD); |
| 197 | last_pc0 = pc0; |
| 198 | au_writel(last_match20 + (MATCH20_INC * ticks), SYS_TOYMATCH2); |
| 199 | au_sync(); |
| 200 | } |
| 201 | #endif |
| 202 | |
| 203 | /* I haven't found anyone that doesn't use a 12 MHz source clock, |
| 204 | * but just in case..... |
| 205 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | #define AU1000_SRC_CLK 12000000 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | |
| 208 | /* |
| 209 | * We read the real processor speed from the PLL. This is important |
| 210 | * because it is more accurate than computing it from the 32KHz |
| 211 | * counter, if it exists. If we don't have an accurate processor |
| 212 | * speed, all of the peripherals that derive their clocks based on |
| 213 | * this advertised speed will introduce error and sometimes not work |
| 214 | * properly. This function is futher convoluted to still allow configurations |
| 215 | * to do that in case they have really, really old silicon with a |
| 216 | * write-only PLL register, that we need the 32KHz when power management |
| 217 | * "wait" is enabled, and we need to detect if the 32KHz isn't present |
| 218 | * but requested......got it? :-) -- Dan |
| 219 | */ |
| 220 | unsigned long cal_r4koff(void) |
| 221 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | unsigned long cpu_speed; |
| 223 | unsigned long flags; |
| 224 | unsigned long counter; |
| 225 | |
| 226 | spin_lock_irqsave(&time_lock, flags); |
| 227 | |
| 228 | /* Power management cares if we don't have a 32KHz counter. |
| 229 | */ |
| 230 | no_au1xxx_32khz = 0; |
| 231 | counter = au_readl(SYS_COUNTER_CNTRL); |
| 232 | if (counter & SYS_CNTRL_E0) { |
| 233 | int trim_divide = 16; |
| 234 | |
| 235 | au_writel(counter | SYS_CNTRL_EN1, SYS_COUNTER_CNTRL); |
| 236 | |
| 237 | while (au_readl(SYS_COUNTER_CNTRL) & SYS_CNTRL_T1S); |
| 238 | /* RTC now ticks at 32.768/16 kHz */ |
| 239 | au_writel(trim_divide-1, SYS_RTCTRIM); |
| 240 | while (au_readl(SYS_COUNTER_CNTRL) & SYS_CNTRL_T1S); |
| 241 | |
| 242 | while (au_readl(SYS_COUNTER_CNTRL) & SYS_CNTRL_C1S); |
| 243 | au_writel (0, SYS_TOYWRITE); |
| 244 | while (au_readl(SYS_COUNTER_CNTRL) & SYS_CNTRL_C1S); |
| 245 | |
Ralf Baechle | 42a3b4f | 2005-09-03 15:56:17 -0700 | [diff] [blame] | 246 | cpu_speed = (au_readl(SYS_CPUPLL) & 0x0000003f) * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | AU1000_SRC_CLK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 248 | } |
| 249 | else { |
| 250 | /* The 32KHz oscillator isn't running, so assume there |
| 251 | * isn't one and grab the processor speed from the PLL. |
| 252 | * NOTE: some old silicon doesn't allow reading the PLL. |
| 253 | */ |
| 254 | cpu_speed = (au_readl(SYS_CPUPLL) & 0x0000003f) * AU1000_SRC_CLK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | no_au1xxx_32khz = 1; |
| 256 | } |
Sergei Shtylyov | 53c1b19 | 2006-09-03 22:17:10 +0400 | [diff] [blame] | 257 | mips_hpt_frequency = cpu_speed; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | // Equation: Baudrate = CPU / (SD * 2 * CLKDIV * 16) |
| 259 | set_au1x00_uart_baud_base(cpu_speed / (2 * ((int)(au_readl(SYS_POWERCTRL)&0x03) + 2) * 16)); |
| 260 | spin_unlock_irqrestore(&time_lock, flags); |
| 261 | return (cpu_speed / HZ); |
| 262 | } |
| 263 | |
Ralf Baechle | 54d0a21 | 2006-07-09 21:38:56 +0100 | [diff] [blame] | 264 | void __init plat_timer_setup(struct irqaction *irq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 | { |
Sergei Shtylyov | fbd7a38 | 2006-05-28 00:04:01 +0400 | [diff] [blame] | 266 | unsigned int est_freq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 267 | |
| 268 | printk("calculating r4koff... "); |
| 269 | r4k_offset = cal_r4koff(); |
| 270 | printk("%08lx(%d)\n", r4k_offset, (int) r4k_offset); |
| 271 | |
Ralf Baechle | 42a3b4f | 2005-09-03 15:56:17 -0700 | [diff] [blame] | 272 | //est_freq = 2*r4k_offset*HZ; |
| 273 | est_freq = r4k_offset*HZ; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 274 | est_freq += 5000; /* round */ |
| 275 | est_freq -= est_freq%10000; |
Ralf Baechle | 42a3b4f | 2005-09-03 15:56:17 -0700 | [diff] [blame] | 276 | printk("CPU frequency %d.%02d MHz\n", est_freq/1000000, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 277 | (est_freq%1000000)*100/1000000); |
| 278 | set_au1x00_speed(est_freq); |
| 279 | set_au1x00_lcd_clock(); // program the LCD clock |
| 280 | |
| 281 | r4k_cur = (read_c0_count() + r4k_offset); |
| 282 | write_c0_compare(r4k_cur); |
| 283 | |
| 284 | #ifdef CONFIG_PM |
| 285 | /* |
| 286 | * setup counter 0, since it keeps ticking after a |
| 287 | * 'wait' instruction has been executed. The CP0 timer and |
| 288 | * counter 1 do NOT continue running after 'wait' |
| 289 | * |
| 290 | * It's too early to call request_irq() here, so we handle |
| 291 | * counter 0 interrupt as a special irq and it doesn't show |
| 292 | * up under /proc/interrupts. |
| 293 | * |
| 294 | * Check to ensure we really have a 32KHz oscillator before |
| 295 | * we do this. |
| 296 | */ |
| 297 | if (no_au1xxx_32khz) { |
| 298 | unsigned int c0_status; |
| 299 | |
| 300 | printk("WARNING: no 32KHz clock found.\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 301 | |
| 302 | /* Ensure we get CPO_COUNTER interrupts. |
| 303 | */ |
| 304 | c0_status = read_c0_status(); |
| 305 | c0_status |= IE_IRQ5; |
| 306 | write_c0_status(c0_status); |
| 307 | } |
| 308 | else { |
| 309 | while (au_readl(SYS_COUNTER_CNTRL) & SYS_CNTRL_C0S); |
| 310 | au_writel(0, SYS_TOYWRITE); |
| 311 | while (au_readl(SYS_COUNTER_CNTRL) & SYS_CNTRL_C0S); |
| 312 | |
| 313 | au_writel(au_readl(SYS_WAKEMSK) | (1<<8), SYS_WAKEMSK); |
| 314 | au_writel(~0, SYS_WAKESRC); |
| 315 | au_sync(); |
| 316 | while (au_readl(SYS_COUNTER_CNTRL) & SYS_CNTRL_M20); |
| 317 | |
Pete Popov | 3ce86ee | 2005-07-19 07:05:36 +0000 | [diff] [blame] | 318 | /* setup match20 to interrupt once every HZ */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 319 | last_pc0 = last_match20 = au_readl(SYS_TOYREAD); |
| 320 | au_writel(last_match20 + MATCH20_INC, SYS_TOYMATCH2); |
| 321 | au_sync(); |
| 322 | while (au_readl(SYS_COUNTER_CNTRL) & SYS_CNTRL_M20); |
Pete Popov | a3701ca | 2005-03-13 08:19:05 +0000 | [diff] [blame] | 323 | startup_match20_interrupt(counter0_irq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 324 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 325 | /* We can use the real 'wait' instruction. |
| 326 | */ |
Pete Popov | 494900a | 2005-04-07 00:42:10 +0000 | [diff] [blame] | 327 | allow_au1k_wait = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 328 | } |
| 329 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 330 | #endif |
| 331 | } |
| 332 | |
| 333 | void __init au1xxx_time_init(void) |
| 334 | { |
| 335 | } |