Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * |
Sergei Shtylyov | 0167509 | 2008-03-24 23:15:50 +0300 | [diff] [blame] | 3 | * Copyright (C) 2001, 2006, 2008 MontaVista Software, <source@mvista.com> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | #include <linux/spinlock.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | #include <asm/mipsregs.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | #include <asm/time.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | #include <asm/mach-au1x00/au1000.h> |
| 42 | |
Sergei Shtylyov | eba8291 | 2008-03-27 22:05:57 +0300 | [diff] [blame] | 43 | static int no_au1xxx_32khz; |
Pete Popov | fe359bf | 2005-04-08 08:34:43 +0000 | [diff] [blame] | 44 | extern int allow_au1k_wait; /* default off for CP0 Counter */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | #ifdef CONFIG_PM |
Pete Popov | 3ce86ee | 2005-07-19 07:05:36 +0000 | [diff] [blame] | 47 | #if HZ < 100 || HZ > 1000 |
| 48 | #error "unsupported HZ value! Must be in [100,1000]" |
| 49 | #endif |
| 50 | #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] | 51 | extern void startup_match20_interrupt(irq_handler_t handler); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | static unsigned long last_pc0, last_match20; |
| 53 | #endif |
| 54 | |
| 55 | static DEFINE_SPINLOCK(time_lock); |
| 56 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | unsigned long wtimer; |
Ralf Baechle | 937a801 | 2006-10-07 19:44:33 +0100 | [diff] [blame] | 58 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | #ifdef CONFIG_PM |
Ralf Baechle | 310a09d | 2007-10-23 02:59:55 +0100 | [diff] [blame] | 60 | static irqreturn_t counter0_irq(int irq, void *dev_id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | { |
| 62 | unsigned long pc0; |
| 63 | int time_elapsed; |
| 64 | static int jiffie_drift = 0; |
| 65 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | if (au_readl(SYS_COUNTER_CNTRL) & SYS_CNTRL_M20) { |
| 67 | /* should never happen! */ |
Pete Popov | 3ce86ee | 2005-07-19 07:05:36 +0000 | [diff] [blame] | 68 | printk(KERN_WARNING "counter 0 w status error\n"); |
| 69 | return IRQ_NONE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | pc0 = au_readl(SYS_TOYREAD); |
| 73 | if (pc0 < last_match20) { |
| 74 | /* counter overflowed */ |
| 75 | time_elapsed = (0xffffffff - last_match20) + pc0; |
| 76 | } |
| 77 | else { |
| 78 | time_elapsed = pc0 - last_match20; |
| 79 | } |
| 80 | |
| 81 | while (time_elapsed > 0) { |
Atsushi Nemoto | 3171a03 | 2006-09-29 02:00:32 -0700 | [diff] [blame] | 82 | do_timer(1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | #ifndef CONFIG_SMP |
Ralf Baechle | 937a801 | 2006-10-07 19:44:33 +0100 | [diff] [blame] | 84 | update_process_times(user_mode(get_irq_regs())); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | #endif |
| 86 | time_elapsed -= MATCH20_INC; |
| 87 | last_match20 += MATCH20_INC; |
| 88 | jiffie_drift++; |
| 89 | } |
| 90 | |
| 91 | last_pc0 = pc0; |
| 92 | au_writel(last_match20 + MATCH20_INC, SYS_TOYMATCH2); |
| 93 | au_sync(); |
| 94 | |
| 95 | /* our counter ticks at 10.009765625 ms/tick, we we're running |
| 96 | * almost 10uS too slow per tick. |
| 97 | */ |
| 98 | |
| 99 | if (jiffie_drift >= 999) { |
| 100 | jiffie_drift -= 999; |
Atsushi Nemoto | 3171a03 | 2006-09-29 02:00:32 -0700 | [diff] [blame] | 101 | do_timer(1); /* increment jiffies by one */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | #ifndef CONFIG_SMP |
Ralf Baechle | 937a801 | 2006-10-07 19:44:33 +0100 | [diff] [blame] | 103 | update_process_times(user_mode(get_irq_regs())); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | #endif |
| 105 | } |
Pete Popov | 3ce86ee | 2005-07-19 07:05:36 +0000 | [diff] [blame] | 106 | |
| 107 | return IRQ_HANDLED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | } |
| 109 | |
Ralf Baechle | 310a09d | 2007-10-23 02:59:55 +0100 | [diff] [blame] | 110 | struct irqaction counter0_action = { |
| 111 | .handler = counter0_irq, |
| 112 | .flags = IRQF_DISABLED, |
| 113 | .name = "alchemy-toy", |
| 114 | .dev_id = NULL, |
| 115 | }; |
| 116 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | /* When we wakeup from sleep, we have to "catch up" on all of the |
| 118 | * timer ticks we have missed. |
| 119 | */ |
| 120 | void |
| 121 | wakeup_counter0_adjust(void) |
| 122 | { |
| 123 | unsigned long pc0; |
| 124 | int time_elapsed; |
| 125 | |
| 126 | pc0 = au_readl(SYS_TOYREAD); |
| 127 | if (pc0 < last_match20) { |
| 128 | /* counter overflowed */ |
| 129 | time_elapsed = (0xffffffff - last_match20) + pc0; |
| 130 | } |
| 131 | else { |
| 132 | time_elapsed = pc0 - last_match20; |
| 133 | } |
| 134 | |
| 135 | while (time_elapsed > 0) { |
| 136 | time_elapsed -= MATCH20_INC; |
| 137 | last_match20 += MATCH20_INC; |
| 138 | } |
| 139 | |
| 140 | last_pc0 = pc0; |
| 141 | au_writel(last_match20 + MATCH20_INC, SYS_TOYMATCH2); |
| 142 | au_sync(); |
| 143 | |
| 144 | } |
| 145 | |
| 146 | /* This is just for debugging to set the timer for a sleep delay. |
| 147 | */ |
| 148 | void |
| 149 | wakeup_counter0_set(int ticks) |
| 150 | { |
| 151 | unsigned long pc0; |
| 152 | |
| 153 | pc0 = au_readl(SYS_TOYREAD); |
| 154 | last_pc0 = pc0; |
| 155 | au_writel(last_match20 + (MATCH20_INC * ticks), SYS_TOYMATCH2); |
| 156 | au_sync(); |
| 157 | } |
| 158 | #endif |
| 159 | |
| 160 | /* I haven't found anyone that doesn't use a 12 MHz source clock, |
| 161 | * but just in case..... |
| 162 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | #define AU1000_SRC_CLK 12000000 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | |
| 165 | /* |
| 166 | * We read the real processor speed from the PLL. This is important |
| 167 | * because it is more accurate than computing it from the 32KHz |
| 168 | * counter, if it exists. If we don't have an accurate processor |
| 169 | * speed, all of the peripherals that derive their clocks based on |
| 170 | * this advertised speed will introduce error and sometimes not work |
| 171 | * properly. This function is futher convoluted to still allow configurations |
| 172 | * to do that in case they have really, really old silicon with a |
| 173 | * write-only PLL register, that we need the 32KHz when power management |
| 174 | * "wait" is enabled, and we need to detect if the 32KHz isn't present |
| 175 | * but requested......got it? :-) -- Dan |
| 176 | */ |
Sergei Shtylyov | eba8291 | 2008-03-27 22:05:57 +0300 | [diff] [blame] | 177 | unsigned long calc_clock(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | unsigned long cpu_speed; |
| 180 | unsigned long flags; |
| 181 | unsigned long counter; |
| 182 | |
| 183 | spin_lock_irqsave(&time_lock, flags); |
| 184 | |
| 185 | /* Power management cares if we don't have a 32KHz counter. |
| 186 | */ |
| 187 | no_au1xxx_32khz = 0; |
| 188 | counter = au_readl(SYS_COUNTER_CNTRL); |
| 189 | if (counter & SYS_CNTRL_E0) { |
| 190 | int trim_divide = 16; |
| 191 | |
| 192 | au_writel(counter | SYS_CNTRL_EN1, SYS_COUNTER_CNTRL); |
| 193 | |
| 194 | while (au_readl(SYS_COUNTER_CNTRL) & SYS_CNTRL_T1S); |
| 195 | /* RTC now ticks at 32.768/16 kHz */ |
| 196 | au_writel(trim_divide-1, SYS_RTCTRIM); |
| 197 | while (au_readl(SYS_COUNTER_CNTRL) & SYS_CNTRL_T1S); |
| 198 | |
| 199 | while (au_readl(SYS_COUNTER_CNTRL) & SYS_CNTRL_C1S); |
Ralf Baechle | 49a89ef | 2007-10-11 23:46:15 +0100 | [diff] [blame] | 200 | au_writel(0, SYS_TOYWRITE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | while (au_readl(SYS_COUNTER_CNTRL) & SYS_CNTRL_C1S); |
Sergei Shtylyov | 758e285 | 2008-03-27 16:09:31 +0300 | [diff] [blame] | 202 | } else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | no_au1xxx_32khz = 1; |
Sergei Shtylyov | 758e285 | 2008-03-27 16:09:31 +0300 | [diff] [blame] | 204 | |
| 205 | /* |
| 206 | * On early Au1000, sys_cpupll was write-only. Since these |
| 207 | * silicon versions of Au1000 are not sold by AMD, we don't bend |
| 208 | * over backwards trying to determine the frequency. |
| 209 | */ |
| 210 | if (cur_cpu_spec[0]->cpu_pll_wo) |
| 211 | #ifdef CONFIG_SOC_AU1000_FREQUENCY |
| 212 | cpu_speed = CONFIG_SOC_AU1000_FREQUENCY; |
| 213 | #else |
| 214 | cpu_speed = 396000000; |
| 215 | #endif |
| 216 | else |
| 217 | cpu_speed = (au_readl(SYS_CPUPLL) & 0x0000003f) * AU1000_SRC_CLK; |
Sergei Shtylyov | 53c1b19 | 2006-09-03 22:17:10 +0400 | [diff] [blame] | 218 | mips_hpt_frequency = cpu_speed; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | // Equation: Baudrate = CPU / (SD * 2 * CLKDIV * 16) |
| 220 | set_au1x00_uart_baud_base(cpu_speed / (2 * ((int)(au_readl(SYS_POWERCTRL)&0x03) + 2) * 16)); |
| 221 | spin_unlock_irqrestore(&time_lock, flags); |
Sergei Shtylyov | eba8291 | 2008-03-27 22:05:57 +0300 | [diff] [blame] | 222 | return cpu_speed; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 | } |
| 224 | |
Ralf Baechle | bc2f2a2 | 2007-10-26 12:58:02 +0100 | [diff] [blame] | 225 | void __init plat_time_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | { |
Sergei Shtylyov | eba8291 | 2008-03-27 22:05:57 +0300 | [diff] [blame] | 227 | unsigned int est_freq = calc_clock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | est_freq += 5000; /* round */ |
| 230 | est_freq -= est_freq%10000; |
Ralf Baechle | 42a3b4f | 2005-09-03 15:56:17 -0700 | [diff] [blame] | 231 | printk("CPU frequency %d.%02d MHz\n", est_freq/1000000, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | (est_freq%1000000)*100/1000000); |
| 233 | set_au1x00_speed(est_freq); |
| 234 | set_au1x00_lcd_clock(); // program the LCD clock |
| 235 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | #ifdef CONFIG_PM |
| 237 | /* |
| 238 | * setup counter 0, since it keeps ticking after a |
| 239 | * 'wait' instruction has been executed. The CP0 timer and |
| 240 | * counter 1 do NOT continue running after 'wait' |
| 241 | * |
| 242 | * It's too early to call request_irq() here, so we handle |
| 243 | * counter 0 interrupt as a special irq and it doesn't show |
| 244 | * up under /proc/interrupts. |
| 245 | * |
| 246 | * Check to ensure we really have a 32KHz oscillator before |
| 247 | * we do this. |
| 248 | */ |
Sergei Shtylyov | 0167509 | 2008-03-24 23:15:50 +0300 | [diff] [blame] | 249 | if (no_au1xxx_32khz) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 250 | printk("WARNING: no 32KHz clock found.\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | else { |
| 252 | while (au_readl(SYS_COUNTER_CNTRL) & SYS_CNTRL_C0S); |
| 253 | au_writel(0, SYS_TOYWRITE); |
| 254 | while (au_readl(SYS_COUNTER_CNTRL) & SYS_CNTRL_C0S); |
| 255 | |
| 256 | au_writel(au_readl(SYS_WAKEMSK) | (1<<8), SYS_WAKEMSK); |
| 257 | au_writel(~0, SYS_WAKESRC); |
| 258 | au_sync(); |
| 259 | while (au_readl(SYS_COUNTER_CNTRL) & SYS_CNTRL_M20); |
| 260 | |
Pete Popov | 3ce86ee | 2005-07-19 07:05:36 +0000 | [diff] [blame] | 261 | /* setup match20 to interrupt once every HZ */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | last_pc0 = last_match20 = au_readl(SYS_TOYREAD); |
| 263 | au_writel(last_match20 + MATCH20_INC, SYS_TOYMATCH2); |
| 264 | au_sync(); |
| 265 | while (au_readl(SYS_COUNTER_CNTRL) & SYS_CNTRL_M20); |
Ralf Baechle | 310a09d | 2007-10-23 02:59:55 +0100 | [diff] [blame] | 266 | setup_irq(AU1000_TOY_MATCH2_INT, &counter0_action); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 267 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 | /* We can use the real 'wait' instruction. |
| 269 | */ |
Pete Popov | 494900a | 2005-04-07 00:42:10 +0000 | [diff] [blame] | 270 | allow_au1k_wait = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 271 | } |
| 272 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | #endif |
| 274 | } |