Split up brcm63xx into files/
[librecmc/librecmc.git] / target / linux / brcm63xx-2.6 / files / arch / mips / bcm963xx / time.c
1 /*
2 <:copyright-gpl
3  Copyright 2004 Broadcom Corp. All Rights Reserved.
4
5  This program is free software; you can distribute it and/or modify it
6  under the terms of the GNU General Public License (Version 2) as
7  published by the Free Software Foundation.
8
9  This program is distributed in the hope it will be useful, but WITHOUT
10  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  for more details.
13
14  You should have received a copy of the GNU General Public License along
15  with this program; if not, write to the Free Software Foundation, Inc.,
16  59 Temple Place - Suite 330, Boston MA 02111-1307, USA.
17 :>
18 */
19 /*
20  * Setup time for Broadcom 963xx MIPS boards
21  */
22
23 #include <linux/autoconf.h>
24 #include <linux/init.h>
25 #include <linux/kernel_stat.h>
26 #include <linux/sched.h>
27 #include <linux/spinlock.h>
28 #include <linux/interrupt.h>
29 #include <linux/module.h>
30 #include <linux/time.h>
31 #include <linux/timex.h>
32
33 #include <asm/mipsregs.h>
34 #include <asm/ptrace.h>
35 #include <asm/div64.h>
36 #include <asm/time.h>
37
38 #include <bcm_map_part.h>
39 #include <bcm_intr.h>
40
41 static unsigned long r4k_offset;        /* Amount to increment compare reg each time */
42 static unsigned long r4k_cur;           /* What counter should be at next timer irq */
43
44 /*  *********************************************************************
45     *  calculateCpuSpeed()
46     *      Calculate the BCM6348 CPU speed by reading the PLL strap register
47     *      and applying the following formula:
48     *      cpu_clk = (.25 * 64MHz freq) * (N1 + 1) * (N2 + 2) / (M1_CPU + 1)
49     *  Input parameters:
50     *      none
51     *  Return value:
52     *      none
53     ********************************************************************* */
54
55 static inline unsigned long __init calculateCpuSpeed(void)
56 {
57     UINT32 pllStrap = PERF->PllStrap;
58     int n1 = (pllStrap & PLL_N1_MASK) >> PLL_N1_SHFT;
59     int n2 = (pllStrap & PLL_N2_MASK) >> PLL_N2_SHFT;
60     int m1cpu = (pllStrap & PLL_M1_CPU_MASK) >> PLL_M1_CPU_SHFT;
61
62         return (16 * (n1 + 1) * (n2 + 2) / (m1cpu + 1)) * 1000000;
63 }
64
65
66 static inline unsigned long __init cal_r4koff(void)
67 {   
68         mips_hpt_frequency = calculateCpuSpeed() / 2;
69         return (mips_hpt_frequency / HZ);
70 }
71
72
73 /*
74  * There are a lot of conceptually broken versions of the MIPS timer interrupt
75  * handler floating around.  This one is rather different, but the algorithm
76  * is provably more robust.
77  */
78 irqreturn_t brcm_timer_interrupt(struct pt_regs *regs)
79 {
80         int irq = MIPS_TIMER_INT;
81
82         irq_enter();
83         kstat_this_cpu.irqs[irq]++;
84
85         timer_interrupt(irq, regs);
86         irq_exit();
87         return IRQ_HANDLED;
88 }
89
90
91 void __init brcm_time_init(void)
92 {
93         unsigned int est_freq, flags;
94         local_irq_save(flags);
95
96         printk("calculating r4koff... ");
97         r4k_offset = cal_r4koff();
98         printk("%08lx(%d)\n", r4k_offset, (int)r4k_offset);
99
100         est_freq = 2 * r4k_offset * HZ;
101         est_freq += 5000;   /* round */
102         est_freq -= est_freq % 10000;
103         printk("CPU frequency %d.%02d MHz\n", est_freq / 1000000,
104                    (est_freq % 1000000) * 100 / 1000000);
105         local_irq_restore(flags);
106 }
107
108
109 void __init plat_timer_setup(struct irqaction *irq)
110 {
111         r4k_cur = (read_c0_count() + r4k_offset);
112         write_c0_compare(r4k_cur);
113         set_c0_status(IE_IRQ5);
114 }