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