blob: 57aea3164d9a78518b9ffd4104887cda4fadca6e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 *
Sergei Shtylyov01675092008-03-24 23:15:50 +03003 * Copyright (C) 2001, 2006, 2008 MontaVista Software, <source@mvista.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 * 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 Torvalds1da177e2005-04-16 15:20:36 -070036#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 Torvalds1da177e2005-04-16 15:20:36 -070044#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
Sergei Shtylyoveba82912008-03-27 22:05:57 +030051static int no_au1xxx_32khz;
Pete Popovfe359bf2005-04-08 08:34:43 +000052extern int allow_au1k_wait; /* default off for CP0 Counter */
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
Linus Torvalds1da177e2005-04-16 15:20:36 -070054#ifdef CONFIG_PM
Pete Popov3ce86ee2005-07-19 07:05:36 +000055#if HZ < 100 || HZ > 1000
56#error "unsupported HZ value! Must be in [100,1000]"
57#endif
58#define MATCH20_INC (328*100/HZ) /* magic number 328 is for HZ=100... */
David Howells40220c12006-10-09 12:19:47 +010059extern void startup_match20_interrupt(irq_handler_t handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -070060static unsigned long last_pc0, last_match20;
61#endif
62
63static DEFINE_SPINLOCK(time_lock);
64
Linus Torvalds1da177e2005-04-16 15:20:36 -070065unsigned long wtimer;
Ralf Baechle937a8012006-10-07 19:44:33 +010066
Linus Torvalds1da177e2005-04-16 15:20:36 -070067#ifdef CONFIG_PM
Ralf Baechle310a09d2007-10-23 02:59:55 +010068static irqreturn_t counter0_irq(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -070069{
70 unsigned long pc0;
71 int time_elapsed;
72 static int jiffie_drift = 0;
73
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 if (au_readl(SYS_COUNTER_CNTRL) & SYS_CNTRL_M20) {
75 /* should never happen! */
Pete Popov3ce86ee2005-07-19 07:05:36 +000076 printk(KERN_WARNING "counter 0 w status error\n");
77 return IRQ_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 }
79
80 pc0 = au_readl(SYS_TOYREAD);
81 if (pc0 < last_match20) {
82 /* counter overflowed */
83 time_elapsed = (0xffffffff - last_match20) + pc0;
84 }
85 else {
86 time_elapsed = pc0 - last_match20;
87 }
88
89 while (time_elapsed > 0) {
Atsushi Nemoto3171a032006-09-29 02:00:32 -070090 do_timer(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091#ifndef CONFIG_SMP
Ralf Baechle937a8012006-10-07 19:44:33 +010092 update_process_times(user_mode(get_irq_regs()));
Linus Torvalds1da177e2005-04-16 15:20:36 -070093#endif
94 time_elapsed -= MATCH20_INC;
95 last_match20 += MATCH20_INC;
96 jiffie_drift++;
97 }
98
99 last_pc0 = pc0;
100 au_writel(last_match20 + MATCH20_INC, SYS_TOYMATCH2);
101 au_sync();
102
103 /* our counter ticks at 10.009765625 ms/tick, we we're running
104 * almost 10uS too slow per tick.
105 */
106
107 if (jiffie_drift >= 999) {
108 jiffie_drift -= 999;
Atsushi Nemoto3171a032006-09-29 02:00:32 -0700109 do_timer(1); /* increment jiffies by one */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110#ifndef CONFIG_SMP
Ralf Baechle937a8012006-10-07 19:44:33 +0100111 update_process_times(user_mode(get_irq_regs()));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112#endif
113 }
Pete Popov3ce86ee2005-07-19 07:05:36 +0000114
115 return IRQ_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116}
117
Ralf Baechle310a09d2007-10-23 02:59:55 +0100118struct irqaction counter0_action = {
119 .handler = counter0_irq,
120 .flags = IRQF_DISABLED,
121 .name = "alchemy-toy",
122 .dev_id = NULL,
123};
124
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125/* When we wakeup from sleep, we have to "catch up" on all of the
126 * timer ticks we have missed.
127 */
128void
129wakeup_counter0_adjust(void)
130{
131 unsigned long pc0;
132 int time_elapsed;
133
134 pc0 = au_readl(SYS_TOYREAD);
135 if (pc0 < last_match20) {
136 /* counter overflowed */
137 time_elapsed = (0xffffffff - last_match20) + pc0;
138 }
139 else {
140 time_elapsed = pc0 - last_match20;
141 }
142
143 while (time_elapsed > 0) {
144 time_elapsed -= MATCH20_INC;
145 last_match20 += MATCH20_INC;
146 }
147
148 last_pc0 = pc0;
149 au_writel(last_match20 + MATCH20_INC, SYS_TOYMATCH2);
150 au_sync();
151
152}
153
154/* This is just for debugging to set the timer for a sleep delay.
155*/
156void
157wakeup_counter0_set(int ticks)
158{
159 unsigned long pc0;
160
161 pc0 = au_readl(SYS_TOYREAD);
162 last_pc0 = pc0;
163 au_writel(last_match20 + (MATCH20_INC * ticks), SYS_TOYMATCH2);
164 au_sync();
165}
166#endif
167
168/* I haven't found anyone that doesn't use a 12 MHz source clock,
169 * but just in case.....
170 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171#define AU1000_SRC_CLK 12000000
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
173/*
174 * We read the real processor speed from the PLL. This is important
175 * because it is more accurate than computing it from the 32KHz
176 * counter, if it exists. If we don't have an accurate processor
177 * speed, all of the peripherals that derive their clocks based on
178 * this advertised speed will introduce error and sometimes not work
179 * properly. This function is futher convoluted to still allow configurations
180 * to do that in case they have really, really old silicon with a
181 * write-only PLL register, that we need the 32KHz when power management
182 * "wait" is enabled, and we need to detect if the 32KHz isn't present
183 * but requested......got it? :-) -- Dan
184 */
Sergei Shtylyoveba82912008-03-27 22:05:57 +0300185unsigned long calc_clock(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 unsigned long cpu_speed;
188 unsigned long flags;
189 unsigned long counter;
190
191 spin_lock_irqsave(&time_lock, flags);
192
193 /* Power management cares if we don't have a 32KHz counter.
194 */
195 no_au1xxx_32khz = 0;
196 counter = au_readl(SYS_COUNTER_CNTRL);
197 if (counter & SYS_CNTRL_E0) {
198 int trim_divide = 16;
199
200 au_writel(counter | SYS_CNTRL_EN1, SYS_COUNTER_CNTRL);
201
202 while (au_readl(SYS_COUNTER_CNTRL) & SYS_CNTRL_T1S);
203 /* RTC now ticks at 32.768/16 kHz */
204 au_writel(trim_divide-1, SYS_RTCTRIM);
205 while (au_readl(SYS_COUNTER_CNTRL) & SYS_CNTRL_T1S);
206
207 while (au_readl(SYS_COUNTER_CNTRL) & SYS_CNTRL_C1S);
Ralf Baechle49a89ef2007-10-11 23:46:15 +0100208 au_writel(0, SYS_TOYWRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 while (au_readl(SYS_COUNTER_CNTRL) & SYS_CNTRL_C1S);
Sergei Shtylyov758e2852008-03-27 16:09:31 +0300210 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 no_au1xxx_32khz = 1;
Sergei Shtylyov758e2852008-03-27 16:09:31 +0300212
213 /*
214 * On early Au1000, sys_cpupll was write-only. Since these
215 * silicon versions of Au1000 are not sold by AMD, we don't bend
216 * over backwards trying to determine the frequency.
217 */
218 if (cur_cpu_spec[0]->cpu_pll_wo)
219#ifdef CONFIG_SOC_AU1000_FREQUENCY
220 cpu_speed = CONFIG_SOC_AU1000_FREQUENCY;
221#else
222 cpu_speed = 396000000;
223#endif
224 else
225 cpu_speed = (au_readl(SYS_CPUPLL) & 0x0000003f) * AU1000_SRC_CLK;
Sergei Shtylyov53c1b192006-09-03 22:17:10 +0400226 mips_hpt_frequency = cpu_speed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 // Equation: Baudrate = CPU / (SD * 2 * CLKDIV * 16)
228 set_au1x00_uart_baud_base(cpu_speed / (2 * ((int)(au_readl(SYS_POWERCTRL)&0x03) + 2) * 16));
229 spin_unlock_irqrestore(&time_lock, flags);
Sergei Shtylyoveba82912008-03-27 22:05:57 +0300230 return cpu_speed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231}
232
Ralf Baechlebc2f2a22007-10-26 12:58:02 +0100233void __init plat_time_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234{
Sergei Shtylyoveba82912008-03-27 22:05:57 +0300235 unsigned int est_freq = calc_clock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 est_freq += 5000; /* round */
238 est_freq -= est_freq%10000;
Ralf Baechle42a3b4f2005-09-03 15:56:17 -0700239 printk("CPU frequency %d.%02d MHz\n", est_freq/1000000,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 (est_freq%1000000)*100/1000000);
241 set_au1x00_speed(est_freq);
242 set_au1x00_lcd_clock(); // program the LCD clock
243
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244#ifdef CONFIG_PM
245 /*
246 * setup counter 0, since it keeps ticking after a
247 * 'wait' instruction has been executed. The CP0 timer and
248 * counter 1 do NOT continue running after 'wait'
249 *
250 * It's too early to call request_irq() here, so we handle
251 * counter 0 interrupt as a special irq and it doesn't show
252 * up under /proc/interrupts.
253 *
254 * Check to ensure we really have a 32KHz oscillator before
255 * we do this.
256 */
Sergei Shtylyov01675092008-03-24 23:15:50 +0300257 if (no_au1xxx_32khz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 printk("WARNING: no 32KHz clock found.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 else {
260 while (au_readl(SYS_COUNTER_CNTRL) & SYS_CNTRL_C0S);
261 au_writel(0, SYS_TOYWRITE);
262 while (au_readl(SYS_COUNTER_CNTRL) & SYS_CNTRL_C0S);
263
264 au_writel(au_readl(SYS_WAKEMSK) | (1<<8), SYS_WAKEMSK);
265 au_writel(~0, SYS_WAKESRC);
266 au_sync();
267 while (au_readl(SYS_COUNTER_CNTRL) & SYS_CNTRL_M20);
268
Pete Popov3ce86ee2005-07-19 07:05:36 +0000269 /* setup match20 to interrupt once every HZ */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 last_pc0 = last_match20 = au_readl(SYS_TOYREAD);
271 au_writel(last_match20 + MATCH20_INC, SYS_TOYMATCH2);
272 au_sync();
273 while (au_readl(SYS_COUNTER_CNTRL) & SYS_CNTRL_M20);
Ralf Baechle310a09d2007-10-23 02:59:55 +0100274 setup_irq(AU1000_TOY_MATCH2_INT, &counter0_action);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 /* We can use the real 'wait' instruction.
277 */
Pete Popov494900a2005-04-07 00:42:10 +0000278 allow_au1k_wait = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 }
280
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281#endif
282}