aboutsummaryrefslogtreecommitdiffstats
path: root/arch/arm/mach-omap1
diff options
context:
space:
mode:
authorTony Lindgren <tony@atomide.com>2005-07-10 14:58:09 -0400
committerRussell King <rmk+kernel@arm.linux.org.uk>2005-07-10 14:58:09 -0400
commit3b59b6beb423267e8fe2ef3596d98aba0b910341 (patch)
tree585d06163371608f1192e1d104da06290f1c5bd9 /arch/arm/mach-omap1
parentb288f75ffa6f26f720d0c69fcd09b4ee7122e17b (diff)
[PATCH] ARM: 2800/1: OMAP update 3/11: Move OMAP1 core code into mach-omap1 directory
Patch from Tony Lindgren This patch by Paul Mundt and other OMAP developers moves OMAP1 specific IRQ, time, and FPGA code into mach-omap1 directory. Signed-off-by: Tony Lindgren <tony@atomide.com> Signed-off-by: Russell King <rmk+kernel@arm.linux.org.uk>
Diffstat (limited to 'arch/arm/mach-omap1')
-rw-r--r--arch/arm/mach-omap1/fpga.c188
-rw-r--r--arch/arm/mach-omap1/irq.c234
-rw-r--r--arch/arm/mach-omap1/time.c436
3 files changed, 858 insertions, 0 deletions
diff --git a/arch/arm/mach-omap1/fpga.c b/arch/arm/mach-omap1/fpga.c
new file mode 100644
index 000000000000..7c08f6c2e1d0
--- /dev/null
+++ b/arch/arm/mach-omap1/fpga.c
@@ -0,0 +1,188 @@
1/*
2 * linux/arch/arm/mach-omap/fpga.c
3 *
4 * Interrupt handler for OMAP-1510 Innovator FPGA
5 *
6 * Copyright (C) 2001 RidgeRun, Inc.
7 * Author: Greg Lonnon <glonnon@ridgerun.com>
8 *
9 * Copyright (C) 2002 MontaVista Software, Inc.
10 *
11 * Separated FPGA interrupts from innovator1510.c and cleaned up for 2.6
12 * Copyright (C) 2004 Nokia Corporation by Tony Lindrgen <tony@atomide.com>
13 *
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License version 2 as
16 * published by the Free Software Foundation.
17 */
18
19#include <linux/config.h>
20#include <linux/types.h>
21#include <linux/init.h>
22#include <linux/kernel.h>
23#include <linux/device.h>
24#include <linux/errno.h>
25
26#include <asm/hardware.h>
27#include <asm/io.h>
28#include <asm/irq.h>
29#include <asm/mach/irq.h>
30
31#include <asm/arch/fpga.h>
32#include <asm/arch/gpio.h>
33
34static void fpga_mask_irq(unsigned int irq)
35{
36 irq -= OMAP1510_IH_FPGA_BASE;
37
38 if (irq < 8)
39 __raw_writeb((__raw_readb(OMAP1510_FPGA_IMR_LO)
40 & ~(1 << irq)), OMAP1510_FPGA_IMR_LO);
41 else if (irq < 16)
42 __raw_writeb((__raw_readb(OMAP1510_FPGA_IMR_HI)
43 & ~(1 << (irq - 8))), OMAP1510_FPGA_IMR_HI);
44 else
45 __raw_writeb((__raw_readb(INNOVATOR_FPGA_IMR2)
46 & ~(1 << (irq - 16))), INNOVATOR_FPGA_IMR2);
47}
48
49
50static inline u32 get_fpga_unmasked_irqs(void)
51{
52 return
53 ((__raw_readb(OMAP1510_FPGA_ISR_LO) &
54 __raw_readb(OMAP1510_FPGA_IMR_LO))) |
55 ((__raw_readb(OMAP1510_FPGA_ISR_HI) &
56 __raw_readb(OMAP1510_FPGA_IMR_HI)) << 8) |
57 ((__raw_readb(INNOVATOR_FPGA_ISR2) &
58 __raw_readb(INNOVATOR_FPGA_IMR2)) << 16);
59}
60
61
62static void fpga_ack_irq(unsigned int irq)
63{
64 /* Don't need to explicitly ACK FPGA interrupts */
65}
66
67static void fpga_unmask_irq(unsigned int irq)
68{
69 irq -= OMAP1510_IH_FPGA_BASE;
70
71 if (irq < 8)
72 __raw_writeb((__raw_readb(OMAP1510_FPGA_IMR_LO) | (1 << irq)),
73 OMAP1510_FPGA_IMR_LO);
74 else if (irq < 16)
75 __raw_writeb((__raw_readb(OMAP1510_FPGA_IMR_HI)
76 | (1 << (irq - 8))), OMAP1510_FPGA_IMR_HI);
77 else
78 __raw_writeb((__raw_readb(INNOVATOR_FPGA_IMR2)
79 | (1 << (irq - 16))), INNOVATOR_FPGA_IMR2);
80}
81
82static void fpga_mask_ack_irq(unsigned int irq)
83{
84 fpga_mask_irq(irq);
85 fpga_ack_irq(irq);
86}
87
88void innovator_fpga_IRQ_demux(unsigned int irq, struct irqdesc *desc,
89 struct pt_regs *regs)
90{
91 struct irqdesc *d;
92 u32 stat;
93 int fpga_irq;
94
95 stat = get_fpga_unmasked_irqs();
96
97 if (!stat)
98 return;
99
100 for (fpga_irq = OMAP1510_IH_FPGA_BASE;
101 (fpga_irq < (OMAP1510_IH_FPGA_BASE + NR_FPGA_IRQS)) && stat;
102 fpga_irq++, stat >>= 1) {
103 if (stat & 1) {
104 d = irq_desc + fpga_irq;
105 d->handle(fpga_irq, d, regs);
106 }
107 }
108}
109
110static struct irqchip omap_fpga_irq_ack = {
111 .ack = fpga_mask_ack_irq,
112 .mask = fpga_mask_irq,
113 .unmask = fpga_unmask_irq,
114};
115
116
117static struct irqchip omap_fpga_irq = {
118 .ack = fpga_ack_irq,
119 .mask = fpga_mask_irq,
120 .unmask = fpga_unmask_irq,
121};
122
123/*
124 * All of the FPGA interrupt request inputs except for the touchscreen are
125 * edge-sensitive; the touchscreen is level-sensitive. The edge-sensitive
126 * interrupts are acknowledged as a side-effect of reading the interrupt
127 * status register from the FPGA. The edge-sensitive interrupt inputs
128 * cause a problem with level interrupt requests, such as Ethernet. The
129 * problem occurs when a level interrupt request is asserted while its
130 * interrupt input is masked in the FPGA, which results in a missed
131 * interrupt.
132 *
133 * In an attempt to workaround the problem with missed interrupts, the
134 * mask_ack routine for all of the FPGA interrupts has been changed from
135 * fpga_mask_ack_irq() to fpga_ack_irq() so that the specific FPGA interrupt
136 * being serviced is left unmasked. We can do this because the FPGA cascade
137 * interrupt is installed with the SA_INTERRUPT flag, which leaves all
138 * interrupts masked at the CPU while an FPGA interrupt handler executes.
139 *
140 * Limited testing indicates that this workaround appears to be effective
141 * for the smc9194 Ethernet driver used on the Innovator. It should work
142 * on other FPGA interrupts as well, but any drivers that explicitly mask
143 * interrupts at the interrupt controller via disable_irq/enable_irq
144 * could pose a problem.
145 */
146void omap1510_fpga_init_irq(void)
147{
148 int i;
149
150 __raw_writeb(0, OMAP1510_FPGA_IMR_LO);
151 __raw_writeb(0, OMAP1510_FPGA_IMR_HI);
152 __raw_writeb(0, INNOVATOR_FPGA_IMR2);
153
154 for (i = OMAP1510_IH_FPGA_BASE; i < (OMAP1510_IH_FPGA_BASE + NR_FPGA_IRQS); i++) {
155
156 if (i == OMAP1510_INT_FPGA_TS) {
157 /*
158 * The touchscreen interrupt is level-sensitive, so
159 * we'll use the regular mask_ack routine for it.
160 */
161 set_irq_chip(i, &omap_fpga_irq_ack);
162 }
163 else {
164 /*
165 * All FPGA interrupts except the touchscreen are
166 * edge-sensitive, so we won't mask them.
167 */
168 set_irq_chip(i, &omap_fpga_irq);
169 }
170
171 set_irq_handler(i, do_edge_IRQ);
172 set_irq_flags(i, IRQF_VALID);
173 }
174
175 /*
176 * The FPGA interrupt line is connected to GPIO13. Claim this pin for
177 * the ARM.
178 *
179 * NOTE: For general GPIO/MPUIO access and interrupts, please see
180 * gpio.[ch]
181 */
182 omap_request_gpio(13);
183 omap_set_gpio_direction(13, 1);
184 omap_set_gpio_edge_ctrl(13, OMAP_GPIO_RISING_EDGE);
185 set_irq_chained_handler(OMAP1510_INT_FPGA, innovator_fpga_IRQ_demux);
186}
187
188EXPORT_SYMBOL(omap1510_fpga_init_irq);
diff --git a/arch/arm/mach-omap1/irq.c b/arch/arm/mach-omap1/irq.c
new file mode 100644
index 000000000000..a11b6d807352
--- /dev/null
+++ b/arch/arm/mach-omap1/irq.c
@@ -0,0 +1,234 @@
1/*
2 * linux/arch/arm/mach-omap/irq.c
3 *
4 * Interrupt handler for all OMAP boards
5 *
6 * Copyright (C) 2004 Nokia Corporation
7 * Written by Tony Lindgren <tony@atomide.com>
8 * Major cleanups by Juha Yrjölä <juha.yrjola@nokia.com>
9 *
10 * Completely re-written to support various OMAP chips with bank specific
11 * interrupt handlers.
12 *
13 * Some snippets of the code taken from the older OMAP interrupt handler
14 * Copyright (C) 2001 RidgeRun, Inc. Greg Lonnon <glonnon@ridgerun.com>
15 *
16 * GPIO interrupt handler moved to gpio.c by Juha Yrjola
17 *
18 * This program is free software; you can redistribute it and/or modify it
19 * under the terms of the GNU General Public License as published by the
20 * Free Software Foundation; either version 2 of the License, or (at your
21 * option) any later version.
22 *
23 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
24 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
25 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
26 * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
29 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
30 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 *
34 * You should have received a copy of the GNU General Public License along
35 * with this program; if not, write to the Free Software Foundation, Inc.,
36 * 675 Mass Ave, Cambridge, MA 02139, USA.
37 */
38
39#include <linux/config.h>
40#include <linux/init.h>
41#include <linux/module.h>
42#include <linux/sched.h>
43#include <linux/interrupt.h>
44#include <linux/ptrace.h>
45
46#include <asm/hardware.h>
47#include <asm/irq.h>
48#include <asm/mach/irq.h>
49#include <asm/arch/gpio.h>
50
51#include <asm/io.h>
52
53#define IRQ_BANK(irq) ((irq) >> 5)
54#define IRQ_BIT(irq) ((irq) & 0x1f)
55
56struct omap_irq_bank {
57 unsigned long base_reg;
58 unsigned long trigger_map;
59 unsigned long wake_enable;
60};
61
62static unsigned int irq_bank_count = 0;
63static struct omap_irq_bank *irq_banks;
64
65static inline unsigned int irq_bank_readl(int bank, int offset)
66{
67 return omap_readl(irq_banks[bank].base_reg + offset);
68}
69
70static inline void irq_bank_writel(unsigned long value, int bank, int offset)
71{
72 omap_writel(value, irq_banks[bank].base_reg + offset);
73}
74
75static void omap_ack_irq(unsigned int irq)
76{
77 if (irq > 31)
78 omap_writel(0x1, OMAP_IH2_BASE + IRQ_CONTROL_REG_OFFSET);
79
80 omap_writel(0x1, OMAP_IH1_BASE + IRQ_CONTROL_REG_OFFSET);
81}
82
83static void omap_mask_irq(unsigned int irq)
84{
85 int bank = IRQ_BANK(irq);
86 u32 l;
87
88 l = omap_readl(irq_banks[bank].base_reg + IRQ_MIR_REG_OFFSET);
89 l |= 1 << IRQ_BIT(irq);
90 omap_writel(l, irq_banks[bank].base_reg + IRQ_MIR_REG_OFFSET);
91}
92
93static void omap_unmask_irq(unsigned int irq)
94{
95 int bank = IRQ_BANK(irq);
96 u32 l;
97
98 l = omap_readl(irq_banks[bank].base_reg + IRQ_MIR_REG_OFFSET);
99 l &= ~(1 << IRQ_BIT(irq));
100 omap_writel(l, irq_banks[bank].base_reg + IRQ_MIR_REG_OFFSET);
101}
102
103static void omap_mask_ack_irq(unsigned int irq)
104{
105 omap_mask_irq(irq);
106 omap_ack_irq(irq);
107}
108
109static int omap_wake_irq(unsigned int irq, unsigned int enable)
110{
111 int bank = IRQ_BANK(irq);
112
113 if (enable)
114 irq_banks[bank].wake_enable |= IRQ_BIT(irq);
115 else
116 irq_banks[bank].wake_enable &= ~IRQ_BIT(irq);
117
118 return 0;
119}
120
121
122/*
123 * Allows tuning the IRQ type and priority
124 *
125 * NOTE: There is currently no OMAP fiq handler for Linux. Read the
126 * mailing list threads on FIQ handlers if you are planning to
127 * add a FIQ handler for OMAP.
128 */
129static void omap_irq_set_cfg(int irq, int fiq, int priority, int trigger)
130{
131 signed int bank;
132 unsigned long val, offset;
133
134 bank = IRQ_BANK(irq);
135 /* FIQ is only available on bank 0 interrupts */
136 fiq = bank ? 0 : (fiq & 0x1);
137 val = fiq | ((priority & 0x1f) << 2) | ((trigger & 0x1) << 1);
138 offset = IRQ_ILR0_REG_OFFSET + IRQ_BIT(irq) * 0x4;
139 irq_bank_writel(val, bank, offset);
140}
141
142#ifdef CONFIG_ARCH_OMAP730
143static struct omap_irq_bank omap730_irq_banks[] = {
144 { .base_reg = OMAP_IH1_BASE, .trigger_map = 0xb3f8e22f },
145 { .base_reg = OMAP_IH2_BASE, .trigger_map = 0xfdb9c1f2 },
146 { .base_reg = OMAP_IH2_BASE + 0x100, .trigger_map = 0x800040f3 },
147};
148#endif
149
150#ifdef CONFIG_ARCH_OMAP1510
151static struct omap_irq_bank omap1510_irq_banks[] = {
152 { .base_reg = OMAP_IH1_BASE, .trigger_map = 0xb3febfff },
153 { .base_reg = OMAP_IH2_BASE, .trigger_map = 0xffbfffed },
154};
155#endif
156
157#if defined(CONFIG_ARCH_OMAP16XX)
158
159static struct omap_irq_bank omap1610_irq_banks[] = {
160 { .base_reg = OMAP_IH1_BASE, .trigger_map = 0xb3fefe8f },
161 { .base_reg = OMAP_IH2_BASE, .trigger_map = 0xfdb7c1fd },
162 { .base_reg = OMAP_IH2_BASE + 0x100, .trigger_map = 0xffffb7ff },
163 { .base_reg = OMAP_IH2_BASE + 0x200, .trigger_map = 0xffffffff },
164};
165#endif
166
167static struct irqchip omap_irq_chip = {
168 .ack = omap_mask_ack_irq,
169 .mask = omap_mask_irq,
170 .unmask = omap_unmask_irq,
171 .wake = omap_wake_irq,
172};
173
174void __init omap_init_irq(void)
175{
176 int i, j;
177
178#ifdef CONFIG_ARCH_OMAP730
179 if (cpu_is_omap730()) {
180 irq_banks = omap730_irq_banks;
181 irq_bank_count = ARRAY_SIZE(omap730_irq_banks);
182 }
183#endif
184#ifdef CONFIG_ARCH_OMAP1510
185 if (cpu_is_omap1510()) {
186 irq_banks = omap1510_irq_banks;
187 irq_bank_count = ARRAY_SIZE(omap1510_irq_banks);
188 }
189#endif
190#if defined(CONFIG_ARCH_OMAP16XX)
191 if (cpu_is_omap16xx()) {
192 irq_banks = omap1610_irq_banks;
193 irq_bank_count = ARRAY_SIZE(omap1610_irq_banks);
194 }
195#endif
196 printk("Total of %i interrupts in %i interrupt banks\n",
197 irq_bank_count * 32, irq_bank_count);
198
199 /* Mask and clear all interrupts */
200 for (i = 0; i < irq_bank_count; i++) {
201 irq_bank_writel(~0x0, i, IRQ_MIR_REG_OFFSET);
202 irq_bank_writel(0x0, i, IRQ_ITR_REG_OFFSET);
203 }
204
205 /* Clear any pending interrupts */
206 irq_bank_writel(0x03, 0, IRQ_CONTROL_REG_OFFSET);
207 irq_bank_writel(0x03, 1, IRQ_CONTROL_REG_OFFSET);
208
209 /* Enable interrupts in global mask */
210 if (cpu_is_omap730()) {
211 irq_bank_writel(0x0, 0, IRQ_GMR_REG_OFFSET);
212 }
213
214 /* Install the interrupt handlers for each bank */
215 for (i = 0; i < irq_bank_count; i++) {
216 for (j = i * 32; j < (i + 1) * 32; j++) {
217 int irq_trigger;
218
219 irq_trigger = irq_banks[i].trigger_map >> IRQ_BIT(j);
220 omap_irq_set_cfg(j, 0, 0, irq_trigger);
221
222 set_irq_chip(j, &omap_irq_chip);
223 set_irq_handler(j, do_level_IRQ);
224 set_irq_flags(j, IRQF_VALID);
225 }
226 }
227
228 /* Unmask level 2 handler */
229 if (cpu_is_omap730()) {
230 omap_unmask_irq(INT_730_IH2_IRQ);
231 } else {
232 omap_unmask_irq(INT_IH2_IRQ);
233 }
234}
diff --git a/arch/arm/mach-omap1/time.c b/arch/arm/mach-omap1/time.c
new file mode 100644
index 000000000000..d540539c9bbb
--- /dev/null
+++ b/arch/arm/mach-omap1/time.c
@@ -0,0 +1,436 @@
1/*
2 * linux/arch/arm/mach-omap1/time.c
3 *
4 * OMAP Timers
5 *
6 * Copyright (C) 2004 Nokia Corporation
7 * Partial timer rewrite and additional dynamic tick timer support by
8 * Tony Lindgen <tony@atomide.com> and
9 * Tuukka Tikkanen <tuukka.tikkanen@elektrobit.com>
10 *
11 * MPU timer code based on the older MPU timer code for OMAP
12 * Copyright (C) 2000 RidgeRun, Inc.
13 * Author: Greg Lonnon <glonnon@ridgerun.com>
14 *
15 * This program is free software; you can redistribute it and/or modify it
16 * under the terms of the GNU General Public License as published by the
17 * Free Software Foundation; either version 2 of the License, or (at your
18 * option) any later version.
19 *
20 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
21 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
22 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
23 * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
26 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
27 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 *
31 * You should have received a copy of the GNU General Public License along
32 * with this program; if not, write to the Free Software Foundation, Inc.,
33 * 675 Mass Ave, Cambridge, MA 02139, USA.
34 */
35
36#include <linux/config.h>
37#include <linux/kernel.h>
38#include <linux/init.h>
39#include <linux/delay.h>
40#include <linux/interrupt.h>
41#include <linux/sched.h>
42#include <linux/spinlock.h>
43
44#include <asm/system.h>
45#include <asm/hardware.h>
46#include <asm/io.h>
47#include <asm/leds.h>
48#include <asm/irq.h>
49#include <asm/mach/irq.h>
50#include <asm/mach/time.h>
51
52struct sys_timer omap_timer;
53
54#ifdef CONFIG_OMAP_MPU_TIMER
55
56/*
57 * ---------------------------------------------------------------------------
58 * MPU timer
59 * ---------------------------------------------------------------------------
60 */
61#define OMAP_MPU_TIMER_BASE OMAP_MPU_TIMER1_BASE
62#define OMAP_MPU_TIMER_OFFSET 0x100
63
64/* cycles to nsec conversions taken from arch/i386/kernel/timers/timer_tsc.c,
65 * converted to use kHz by Kevin Hilman */
66/* convert from cycles(64bits) => nanoseconds (64bits)
67 * basic equation:
68 * ns = cycles / (freq / ns_per_sec)
69 * ns = cycles * (ns_per_sec / freq)
70 * ns = cycles * (10^9 / (cpu_khz * 10^3))
71 * ns = cycles * (10^6 / cpu_khz)
72 *
73 * Then we use scaling math (suggested by george at mvista.com) to get:
74 * ns = cycles * (10^6 * SC / cpu_khz / SC
75 * ns = cycles * cyc2ns_scale / SC
76 *
77 * And since SC is a constant power of two, we can convert the div
78 * into a shift.
79 * -johnstul at us.ibm.com "math is hard, lets go shopping!"
80 */
81static unsigned long cyc2ns_scale;
82#define CYC2NS_SCALE_FACTOR 10 /* 2^10, carefully chosen */
83
84static inline void set_cyc2ns_scale(unsigned long cpu_khz)
85{
86 cyc2ns_scale = (1000000 << CYC2NS_SCALE_FACTOR)/cpu_khz;
87}
88
89static inline unsigned long long cycles_2_ns(unsigned long long cyc)
90{
91 return (cyc * cyc2ns_scale) >> CYC2NS_SCALE_FACTOR;
92}
93
94/*
95 * MPU_TICKS_PER_SEC must be an even number, otherwise machinecycles_to_usecs
96 * will break. On P2, the timer count rate is 6.5 MHz after programming PTV
97 * with 0. This divides the 13MHz input by 2, and is undocumented.
98 */
99#ifdef CONFIG_MACH_OMAP_PERSEUS2
100/* REVISIT: This ifdef construct should be replaced by a query to clock
101 * framework to see if timer base frequency is 12.0, 13.0 or 19.2 MHz.
102 */
103#define MPU_TICKS_PER_SEC (13000000 / 2)
104#else
105#define MPU_TICKS_PER_SEC (12000000 / 2)
106#endif
107
108#define MPU_TIMER_TICK_PERIOD ((MPU_TICKS_PER_SEC / HZ) - 1)
109
110typedef struct {
111 u32 cntl; /* CNTL_TIMER, R/W */
112 u32 load_tim; /* LOAD_TIM, W */
113 u32 read_tim; /* READ_TIM, R */
114} omap_mpu_timer_regs_t;
115
116#define omap_mpu_timer_base(n) \
117((volatile omap_mpu_timer_regs_t*)IO_ADDRESS(OMAP_MPU_TIMER_BASE + \
118 (n)*OMAP_MPU_TIMER_OFFSET))
119
120static inline unsigned long omap_mpu_timer_read(int nr)
121{
122 volatile omap_mpu_timer_regs_t* timer = omap_mpu_timer_base(nr);
123 return timer->read_tim;
124}
125
126static inline void omap_mpu_timer_start(int nr, unsigned long load_val)
127{
128 volatile omap_mpu_timer_regs_t* timer = omap_mpu_timer_base(nr);
129
130 timer->cntl = MPU_TIMER_CLOCK_ENABLE;
131 udelay(1);
132 timer->load_tim = load_val;
133 udelay(1);
134 timer->cntl = (MPU_TIMER_CLOCK_ENABLE | MPU_TIMER_AR | MPU_TIMER_ST);
135}
136
137unsigned long omap_mpu_timer_ticks_to_usecs(unsigned long nr_ticks)
138{
139 unsigned long long nsec;
140
141 nsec = cycles_2_ns((unsigned long long)nr_ticks);
142 return (unsigned long)nsec / 1000;
143}
144
145/*
146 * Last processed system timer interrupt
147 */
148static unsigned long omap_mpu_timer_last = 0;
149
150/*
151 * Returns elapsed usecs since last system timer interrupt
152 */
153static unsigned long omap_mpu_timer_gettimeoffset(void)
154{
155 unsigned long now = 0 - omap_mpu_timer_read(0);
156 unsigned long elapsed = now - omap_mpu_timer_last;
157
158 return omap_mpu_timer_ticks_to_usecs(elapsed);
159}
160
161/*
162 * Elapsed time between interrupts is calculated using timer0.
163 * Latency during the interrupt is calculated using timer1.
164 * Both timer0 and timer1 are counting at 6MHz (P2 6.5MHz).
165 */
166static irqreturn_t omap_mpu_timer_interrupt(int irq, void *dev_id,
167 struct pt_regs *regs)
168{
169 unsigned long now, latency;
170
171 write_seqlock(&xtime_lock);
172 now = 0 - omap_mpu_timer_read(0);
173 latency = MPU_TICKS_PER_SEC / HZ - omap_mpu_timer_read(1);
174 omap_mpu_timer_last = now - latency;
175 timer_tick(regs);
176 write_sequnlock(&xtime_lock);
177
178 return IRQ_HANDLED;
179}
180
181static struct irqaction omap_mpu_timer_irq = {
182 .name = "mpu timer",
183 .flags = SA_INTERRUPT | SA_TIMER,
184 .handler = omap_mpu_timer_interrupt,
185};
186
187static unsigned long omap_mpu_timer1_overflows;
188static irqreturn_t omap_mpu_timer1_interrupt(int irq, void *dev_id,
189 struct pt_regs *regs)
190{
191 omap_mpu_timer1_overflows++;
192 return IRQ_HANDLED;
193}
194
195static struct irqaction omap_mpu_timer1_irq = {
196 .name = "mpu timer1 overflow",
197 .flags = SA_INTERRUPT,
198 .handler = omap_mpu_timer1_interrupt,
199};
200
201static __init void omap_init_mpu_timer(void)
202{
203 set_cyc2ns_scale(MPU_TICKS_PER_SEC / 1000);
204 omap_timer.offset = omap_mpu_timer_gettimeoffset;
205 setup_irq(INT_TIMER1, &omap_mpu_timer1_irq);
206 setup_irq(INT_TIMER2, &omap_mpu_timer_irq);
207 omap_mpu_timer_start(0, 0xffffffff);
208 omap_mpu_timer_start(1, MPU_TIMER_TICK_PERIOD);
209}
210
211/*
212 * Scheduler clock - returns current time in nanosec units.
213 */
214unsigned long long sched_clock(void)
215{
216 unsigned long ticks = 0 - omap_mpu_timer_read(0);
217 unsigned long long ticks64;
218
219 ticks64 = omap_mpu_timer1_overflows;
220 ticks64 <<= 32;
221 ticks64 |= ticks;
222
223 return cycles_2_ns(ticks64);
224}
225#endif /* CONFIG_OMAP_MPU_TIMER */
226
227#ifdef CONFIG_OMAP_32K_TIMER
228
229#ifdef CONFIG_ARCH_OMAP1510
230#error OMAP 32KHz timer does not currently work on 1510!
231#endif
232
233/*
234 * ---------------------------------------------------------------------------
235 * 32KHz OS timer
236 *
237 * This currently works only on 16xx, as 1510 does not have the continuous
238 * 32KHz synchronous timer. The 32KHz synchronous timer is used to keep track
239 * of time in addition to the 32KHz OS timer. Using only the 32KHz OS timer
240 * on 1510 would be possible, but the timer would not be as accurate as
241 * with the 32KHz synchronized timer.
242 * ---------------------------------------------------------------------------
243 */
244#define OMAP_32K_TIMER_BASE 0xfffb9000
245#define OMAP_32K_TIMER_CR 0x08
246#define OMAP_32K_TIMER_TVR 0x00
247#define OMAP_32K_TIMER_TCR 0x04
248
249#define OMAP_32K_TICKS_PER_HZ (32768 / HZ)
250#if (32768 % HZ) != 0
251/* We cannot ignore modulo.
252 * Potential error can be as high as several percent.
253 */
254#define OMAP_32K_TICK_MODULO (32768 % HZ)
255static unsigned modulo_count = 0; /* Counts 1/HZ units */
256#endif
257
258/*
259 * TRM says 1 / HZ = ( TVR + 1) / 32768, so TRV = (32768 / HZ) - 1
260 * so with HZ = 100, TVR = 327.68.
261 */
262#define OMAP_32K_TIMER_TICK_PERIOD ((32768 / HZ) - 1)
263#define TIMER_32K_SYNCHRONIZED 0xfffbc410
264
265#define JIFFIES_TO_HW_TICKS(nr_jiffies, clock_rate) \
266 (((nr_jiffies) * (clock_rate)) / HZ)
267
268static inline void omap_32k_timer_write(int val, int reg)
269{
270 omap_writew(val, reg + OMAP_32K_TIMER_BASE);
271}
272
273static inline unsigned long omap_32k_timer_read(int reg)
274{
275 return omap_readl(reg + OMAP_32K_TIMER_BASE) & 0xffffff;
276}
277
278/*
279 * The 32KHz synchronized timer is an additional timer on 16xx.
280 * It is always running.
281 */
282static inline unsigned long omap_32k_sync_timer_read(void)
283{
284 return omap_readl(TIMER_32K_SYNCHRONIZED);
285}
286
287static inline void omap_32k_timer_start(unsigned long load_val)
288{
289 omap_32k_timer_write(load_val, OMAP_32K_TIMER_TVR);
290 omap_32k_timer_write(0x0f, OMAP_32K_TIMER_CR);
291}
292
293static inline void omap_32k_timer_stop(void)
294{
295 omap_32k_timer_write(0x0, OMAP_32K_TIMER_CR);
296}
297
298/*
299 * Rounds down to nearest usec
300 */
301static inline unsigned long omap_32k_ticks_to_usecs(unsigned long ticks_32k)
302{
303 return (ticks_32k * 5*5*5*5*5*5) >> 9;
304}
305
306static unsigned long omap_32k_last_tick = 0;
307
308/*
309 * Returns elapsed usecs since last 32k timer interrupt
310 */
311static unsigned long omap_32k_timer_gettimeoffset(void)
312{
313 unsigned long now = omap_32k_sync_timer_read();
314 return omap_32k_ticks_to_usecs(now - omap_32k_last_tick);
315}
316
317/*
318 * Timer interrupt for 32KHz timer. When dynamic tick is enabled, this
319 * function is also called from other interrupts to remove latency
320 * issues with dynamic tick. In the dynamic tick case, we need to lock
321 * with irqsave.
322 */
323static irqreturn_t omap_32k_timer_interrupt(int irq, void *dev_id,
324 struct pt_regs *regs)
325{
326 unsigned long flags;
327 unsigned long now;
328
329 write_seqlock_irqsave(&xtime_lock, flags);
330 now = omap_32k_sync_timer_read();
331
332 while (now - omap_32k_last_tick >= OMAP_32K_TICKS_PER_HZ) {
333#ifdef OMAP_32K_TICK_MODULO
334 /* Modulo addition may put omap_32k_last_tick ahead of now
335 * and cause unwanted repetition of the while loop.
336 */
337 if (unlikely(now - omap_32k_last_tick == ~0))
338 break;
339
340 modulo_count += OMAP_32K_TICK_MODULO;
341 if (modulo_count > HZ) {
342 ++omap_32k_last_tick;
343 modulo_count -= HZ;
344 }
345#endif
346 omap_32k_last_tick += OMAP_32K_TICKS_PER_HZ;
347 timer_tick(regs);
348 }
349
350 /* Restart timer so we don't drift off due to modulo or dynamic tick.
351 * By default we program the next timer to be continuous to avoid
352 * latencies during high system load. During dynamic tick operation the
353 * continuous timer can be overridden from pm_idle to be longer.
354 */
355 omap_32k_timer_start(omap_32k_last_tick + OMAP_32K_TICKS_PER_HZ - now);
356 write_sequnlock_irqrestore(&xtime_lock, flags);
357
358 return IRQ_HANDLED;
359}
360
361#ifdef CONFIG_NO_IDLE_HZ
362/*
363 * Programs the next timer interrupt needed. Called when dynamic tick is
364 * enabled, and to reprogram the ticks to skip from pm_idle. Note that
365 * we can keep the timer continuous, and don't need to set it to run in
366 * one-shot mode. This is because the timer will get reprogrammed again
367 * after next interrupt.
368 */
369void omap_32k_timer_reprogram(unsigned long next_tick)
370{
371 omap_32k_timer_start(JIFFIES_TO_HW_TICKS(next_tick, 32768) + 1);
372}
373
374static struct irqaction omap_32k_timer_irq;
375extern struct timer_update_handler timer_update;
376
377static int omap_32k_timer_enable_dyn_tick(void)
378{
379 /* No need to reprogram timer, just use the next interrupt */
380 return 0;
381}
382
383static int omap_32k_timer_disable_dyn_tick(void)
384{
385 omap_32k_timer_start(OMAP_32K_TIMER_TICK_PERIOD);
386 return 0;
387}
388
389static struct dyn_tick_timer omap_dyn_tick_timer = {
390 .enable = omap_32k_timer_enable_dyn_tick,
391 .disable = omap_32k_timer_disable_dyn_tick,
392 .reprogram = omap_32k_timer_reprogram,
393 .handler = omap_32k_timer_interrupt,
394};
395#endif /* CONFIG_NO_IDLE_HZ */
396
397static struct irqaction omap_32k_timer_irq = {
398 .name = "32KHz timer",
399 .flags = SA_INTERRUPT | SA_TIMER,
400 .handler = omap_32k_timer_interrupt,
401};
402
403static __init void omap_init_32k_timer(void)
404{
405
406#ifdef CONFIG_NO_IDLE_HZ
407 omap_timer.dyn_tick = &omap_dyn_tick_timer;
408#endif
409
410 setup_irq(INT_OS_TIMER, &omap_32k_timer_irq);
411 omap_timer.offset = omap_32k_timer_gettimeoffset;
412 omap_32k_last_tick = omap_32k_sync_timer_read();
413 omap_32k_timer_start(OMAP_32K_TIMER_TICK_PERIOD);
414}
415#endif /* CONFIG_OMAP_32K_TIMER */
416
417/*
418 * ---------------------------------------------------------------------------
419 * Timer initialization
420 * ---------------------------------------------------------------------------
421 */
422static void __init omap_timer_init(void)
423{
424#if defined(CONFIG_OMAP_MPU_TIMER)
425 omap_init_mpu_timer();
426#elif defined(CONFIG_OMAP_32K_TIMER)
427 omap_init_32k_timer();
428#else
429#error No system timer selected in Kconfig!
430#endif
431}
432
433struct sys_timer omap_timer = {
434 .init = omap_timer_init,
435 .offset = NULL, /* Initialized later */
436};