blob: 2556399708ba452278d2c05c73b319d4fe864750 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070051static unsigned long r4k_offset; /* Amount to increment compare reg each time */
52static unsigned long r4k_cur; /* What counter should be at next timer irq */
53int no_au1xxx_32khz;
Pete Popovfe359bf2005-04-08 08:34:43 +000054extern int allow_au1k_wait; /* default off for CP0 Counter */
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
Linus Torvalds1da177e2005-04-16 15:20:36 -070056#ifdef CONFIG_PM
Pete Popov3ce86ee2005-07-19 07:05:36 +000057#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 Howells40220c12006-10-09 12:19:47 +010061extern void startup_match20_interrupt(irq_handler_t handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -070062static unsigned long last_pc0, last_match20;
63#endif
64
65static DEFINE_SPINLOCK(time_lock);
66
Linus Torvalds1da177e2005-04-16 15:20:36 -070067unsigned long wtimer;
Ralf Baechle937a8012006-10-07 19:44:33 +010068
Linus Torvalds1da177e2005-04-16 15:20:36 -070069#ifdef CONFIG_PM
Ralf Baechle937a8012006-10-07 19:44:33 +010070irqreturn_t counter0_irq(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -070071{
72 unsigned long pc0;
73 int time_elapsed;
74 static int jiffie_drift = 0;
75
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 if (au_readl(SYS_COUNTER_CNTRL) & SYS_CNTRL_M20) {
77 /* should never happen! */
Pete Popov3ce86ee2005-07-19 07:05:36 +000078 printk(KERN_WARNING "counter 0 w status error\n");
79 return IRQ_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 }
81
82 pc0 = au_readl(SYS_TOYREAD);
83 if (pc0 < last_match20) {
84 /* counter overflowed */
85 time_elapsed = (0xffffffff - last_match20) + pc0;
86 }
87 else {
88 time_elapsed = pc0 - last_match20;
89 }
90
91 while (time_elapsed > 0) {
Atsushi Nemoto3171a032006-09-29 02:00:32 -070092 do_timer(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093#ifndef CONFIG_SMP
Ralf Baechle937a8012006-10-07 19:44:33 +010094 update_process_times(user_mode(get_irq_regs()));
Linus Torvalds1da177e2005-04-16 15:20:36 -070095#endif
96 time_elapsed -= MATCH20_INC;
97 last_match20 += MATCH20_INC;
98 jiffie_drift++;
99 }
100
101 last_pc0 = pc0;
102 au_writel(last_match20 + MATCH20_INC, SYS_TOYMATCH2);
103 au_sync();
104
105 /* our counter ticks at 10.009765625 ms/tick, we we're running
106 * almost 10uS too slow per tick.
107 */
108
109 if (jiffie_drift >= 999) {
110 jiffie_drift -= 999;
Atsushi Nemoto3171a032006-09-29 02:00:32 -0700111 do_timer(1); /* increment jiffies by one */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112#ifndef CONFIG_SMP
Ralf Baechle937a8012006-10-07 19:44:33 +0100113 update_process_times(user_mode(get_irq_regs()));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114#endif
115 }
Pete Popov3ce86ee2005-07-19 07:05:36 +0000116
117 return IRQ_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118}
119
120/* When we wakeup from sleep, we have to "catch up" on all of the
121 * timer ticks we have missed.
122 */
123void
124wakeup_counter0_adjust(void)
125{
126 unsigned long pc0;
127 int time_elapsed;
128
129 pc0 = au_readl(SYS_TOYREAD);
130 if (pc0 < last_match20) {
131 /* counter overflowed */
132 time_elapsed = (0xffffffff - last_match20) + pc0;
133 }
134 else {
135 time_elapsed = pc0 - last_match20;
136 }
137
138 while (time_elapsed > 0) {
139 time_elapsed -= MATCH20_INC;
140 last_match20 += MATCH20_INC;
141 }
142
143 last_pc0 = pc0;
144 au_writel(last_match20 + MATCH20_INC, SYS_TOYMATCH2);
145 au_sync();
146
147}
148
149/* This is just for debugging to set the timer for a sleep delay.
150*/
151void
152wakeup_counter0_set(int ticks)
153{
154 unsigned long pc0;
155
156 pc0 = au_readl(SYS_TOYREAD);
157 last_pc0 = pc0;
158 au_writel(last_match20 + (MATCH20_INC * ticks), SYS_TOYMATCH2);
159 au_sync();
160}
161#endif
162
163/* I haven't found anyone that doesn't use a 12 MHz source clock,
164 * but just in case.....
165 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166#define AU1000_SRC_CLK 12000000
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
168/*
169 * We read the real processor speed from the PLL. This is important
170 * because it is more accurate than computing it from the 32KHz
171 * counter, if it exists. If we don't have an accurate processor
172 * speed, all of the peripherals that derive their clocks based on
173 * this advertised speed will introduce error and sometimes not work
174 * properly. This function is futher convoluted to still allow configurations
175 * to do that in case they have really, really old silicon with a
176 * write-only PLL register, that we need the 32KHz when power management
177 * "wait" is enabled, and we need to detect if the 32KHz isn't present
178 * but requested......got it? :-) -- Dan
179 */
180unsigned long cal_r4koff(void)
181{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 unsigned long cpu_speed;
183 unsigned long flags;
184 unsigned long counter;
185
186 spin_lock_irqsave(&time_lock, flags);
187
188 /* Power management cares if we don't have a 32KHz counter.
189 */
190 no_au1xxx_32khz = 0;
191 counter = au_readl(SYS_COUNTER_CNTRL);
192 if (counter & SYS_CNTRL_E0) {
193 int trim_divide = 16;
194
195 au_writel(counter | SYS_CNTRL_EN1, SYS_COUNTER_CNTRL);
196
197 while (au_readl(SYS_COUNTER_CNTRL) & SYS_CNTRL_T1S);
198 /* RTC now ticks at 32.768/16 kHz */
199 au_writel(trim_divide-1, SYS_RTCTRIM);
200 while (au_readl(SYS_COUNTER_CNTRL) & SYS_CNTRL_T1S);
201
202 while (au_readl(SYS_COUNTER_CNTRL) & SYS_CNTRL_C1S);
Ralf Baechle49a89ef2007-10-11 23:46:15 +0100203 au_writel(0, SYS_TOYWRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 while (au_readl(SYS_COUNTER_CNTRL) & SYS_CNTRL_C1S);
205
Ralf Baechle42a3b4f2005-09-03 15:56:17 -0700206 cpu_speed = (au_readl(SYS_CPUPLL) & 0x0000003f) *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 AU1000_SRC_CLK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 }
209 else {
210 /* The 32KHz oscillator isn't running, so assume there
211 * isn't one and grab the processor speed from the PLL.
212 * NOTE: some old silicon doesn't allow reading the PLL.
213 */
214 cpu_speed = (au_readl(SYS_CPUPLL) & 0x0000003f) * AU1000_SRC_CLK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 no_au1xxx_32khz = 1;
216 }
Sergei Shtylyov53c1b192006-09-03 22:17:10 +0400217 mips_hpt_frequency = cpu_speed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 // Equation: Baudrate = CPU / (SD * 2 * CLKDIV * 16)
219 set_au1x00_uart_baud_base(cpu_speed / (2 * ((int)(au_readl(SYS_POWERCTRL)&0x03) + 2) * 16));
220 spin_unlock_irqrestore(&time_lock, flags);
221 return (cpu_speed / HZ);
222}
223
Ralf Baechle54d0a212006-07-09 21:38:56 +0100224void __init plat_timer_setup(struct irqaction *irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225{
Sergei Shtylyovfbd7a382006-05-28 00:04:01 +0400226 unsigned int est_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
228 printk("calculating r4koff... ");
229 r4k_offset = cal_r4koff();
230 printk("%08lx(%d)\n", r4k_offset, (int) r4k_offset);
231
Ralf Baechle42a3b4f2005-09-03 15:56:17 -0700232 //est_freq = 2*r4k_offset*HZ;
233 est_freq = r4k_offset*HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 est_freq += 5000; /* round */
235 est_freq -= est_freq%10000;
Ralf Baechle42a3b4f2005-09-03 15:56:17 -0700236 printk("CPU frequency %d.%02d MHz\n", est_freq/1000000,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 (est_freq%1000000)*100/1000000);
238 set_au1x00_speed(est_freq);
239 set_au1x00_lcd_clock(); // program the LCD clock
240
241 r4k_cur = (read_c0_count() + r4k_offset);
242 write_c0_compare(r4k_cur);
243
244#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 */
257 if (no_au1xxx_32khz) {
258 unsigned int c0_status;
259
260 printk("WARNING: no 32KHz clock found.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
262 /* Ensure we get CPO_COUNTER interrupts.
263 */
264 c0_status = read_c0_status();
265 c0_status |= IE_IRQ5;
266 write_c0_status(c0_status);
267 }
268 else {
269 while (au_readl(SYS_COUNTER_CNTRL) & SYS_CNTRL_C0S);
270 au_writel(0, SYS_TOYWRITE);
271 while (au_readl(SYS_COUNTER_CNTRL) & SYS_CNTRL_C0S);
272
273 au_writel(au_readl(SYS_WAKEMSK) | (1<<8), SYS_WAKEMSK);
274 au_writel(~0, SYS_WAKESRC);
275 au_sync();
276 while (au_readl(SYS_COUNTER_CNTRL) & SYS_CNTRL_M20);
277
Pete Popov3ce86ee2005-07-19 07:05:36 +0000278 /* setup match20 to interrupt once every HZ */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 last_pc0 = last_match20 = au_readl(SYS_TOYREAD);
280 au_writel(last_match20 + MATCH20_INC, SYS_TOYMATCH2);
281 au_sync();
282 while (au_readl(SYS_COUNTER_CNTRL) & SYS_CNTRL_M20);
Pete Popova3701ca2005-03-13 08:19:05 +0000283 startup_match20_interrupt(counter0_irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 /* We can use the real 'wait' instruction.
286 */
Pete Popov494900a2005-04-07 00:42:10 +0000287 allow_au1k_wait = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 }
289
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290#endif
291}