blob: 9ef54628bc9c8eecac4203d31f004cc790b0d210 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Copyright (C) 2000, 2001 Broadcom Corporation
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 */
18
19/*
20 * These are routines to set up and handle interrupts from the
21 * sb1250 general purpose timer 0. We're using the timer as a
22 * system clock, so we set it up to run at 100 Hz. On every
23 * interrupt, we update our idea of what the time of day is,
24 * then call do_timer() in the architecture-independent kernel
25 * code to do general bookkeeping (e.g. update jiffies, run
26 * bottom halves, etc.)
27 */
Ralf Baechle9b9ea222007-10-11 23:46:10 +010028#include <linux/clockchips.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/interrupt.h>
30#include <linux/sched.h>
31#include <linux/spinlock.h>
32#include <linux/kernel_stat.h>
33
34#include <asm/irq.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <asm/addrspace.h>
36#include <asm/time.h>
37#include <asm/io.h>
38
39#include <asm/sibyte/sb1250.h>
40#include <asm/sibyte/sb1250_regs.h>
41#include <asm/sibyte/sb1250_int.h>
42#include <asm/sibyte/sb1250_scd.h>
43
44
45#define IMR_IP2_VAL K_INT_MAP_I0
46#define IMR_IP3_VAL K_INT_MAP_I1
47#define IMR_IP4_VAL K_INT_MAP_I2
48
Ralf Baechlea904f742006-03-15 00:03:29 +000049#define SB1250_HPT_NUM 3
50#define SB1250_HPT_VALUE M_SCD_TIMER_CNT /* max value */
Ralf Baechlea904f742006-03-15 00:03:29 +000051
52
Linus Torvalds1da177e2005-04-16 15:20:36 -070053extern int sb1250_steal_irq(int irq);
54
Atsushi Nemoto00598562006-11-12 00:10:28 +090055static cycle_t sb1250_hpt_read(void);
Ralf Baechlea904f742006-03-15 00:03:29 +000056
57void __init sb1250_hpt_setup(void)
58{
59 int cpu = smp_processor_id();
60
61 if (!cpu) {
62 /* Setup hpt using timer #3 but do not enable irq for it */
63 __raw_writeq(0, IOADDR(A_SCD_TIMER_REGISTER(SB1250_HPT_NUM, R_SCD_TIMER_CFG)));
64 __raw_writeq(SB1250_HPT_VALUE,
65 IOADDR(A_SCD_TIMER_REGISTER(SB1250_HPT_NUM, R_SCD_TIMER_INIT)));
66 __raw_writeq(M_SCD_TIMER_ENABLE | M_SCD_TIMER_MODE_CONTINUOUS,
67 IOADDR(A_SCD_TIMER_REGISTER(SB1250_HPT_NUM, R_SCD_TIMER_CFG)));
68
Atsushi Nemoto16b7b2a2006-10-24 00:21:27 +090069 mips_hpt_frequency = V_SCD_TIMER_FREQ;
Atsushi Nemoto00598562006-11-12 00:10:28 +090070 clocksource_mips.read = sb1250_hpt_read;
71 clocksource_mips.mask = M_SCD_TIMER_INIT;
Ralf Baechlea904f742006-03-15 00:03:29 +000072 }
73}
74
Ralf Baechle9b9ea222007-10-11 23:46:10 +010075/*
76 * The general purpose timer ticks at 1 Mhz independent if
77 * the rest of the system
78 */
79static void sibyte_set_mode(enum clock_event_mode mode,
80 struct clock_event_device *evt)
Linus Torvalds1da177e2005-04-16 15:20:36 -070081{
Ralf Baechle9b9ea222007-10-11 23:46:10 +010082 unsigned int cpu = smp_processor_id();
83 void __iomem *timer_cfg, *timer_init;
84
85 timer_cfg = IOADDR(A_SCD_TIMER_REGISTER(cpu, R_SCD_TIMER_CFG));
Atsushi Nemoto9ee5389c2007-10-18 00:57:07 +090086 timer_init = IOADDR(A_SCD_TIMER_REGISTER(cpu, R_SCD_TIMER_INIT));
Ralf Baechle9b9ea222007-10-11 23:46:10 +010087
88 switch(mode) {
89 case CLOCK_EVT_MODE_PERIODIC:
90 __raw_writeq(0, timer_cfg);
91 __raw_writeq((V_SCD_TIMER_FREQ / HZ) - 1, timer_init);
92 __raw_writeq(M_SCD_TIMER_ENABLE | M_SCD_TIMER_MODE_CONTINUOUS,
93 timer_cfg);
94 break;
95
96 case CLOCK_EVT_MODE_ONESHOT:
97 /* Stop the timer until we actually program a shot */
98 case CLOCK_EVT_MODE_SHUTDOWN:
99 __raw_writeq(0, timer_cfg);
100 break;
101
102 case CLOCK_EVT_MODE_UNUSED: /* shuddup gcc */
Ralf Baechled527eef2007-10-19 08:22:38 +0100103 case CLOCK_EVT_MODE_RESUME:
Ralf Baechle9b9ea222007-10-11 23:46:10 +0100104 ;
105 }
106}
107
108static int
109sibyte_next_event(unsigned long delta, struct clock_event_device *evt)
110{
111 unsigned int cpu = smp_processor_id();
112 void __iomem *timer_cfg, *timer_init;
113
114 timer_cfg = IOADDR(A_SCD_TIMER_REGISTER(cpu, R_SCD_TIMER_CFG));
Atsushi Nemoto9ee5389c2007-10-18 00:57:07 +0900115 timer_init = IOADDR(A_SCD_TIMER_REGISTER(cpu, R_SCD_TIMER_INIT));
Ralf Baechle9b9ea222007-10-11 23:46:10 +0100116
117 __raw_writeq(0, timer_cfg);
118 __raw_writeq(delta, timer_init);
119 __raw_writeq(M_SCD_TIMER_ENABLE, timer_cfg);
120
121 return 0;
122}
123
124struct clock_event_device sibyte_hpt_clockevent = {
125 .name = "sb1250-counter",
126 .features = CLOCK_EVT_FEAT_PERIODIC,
127 .set_mode = sibyte_set_mode,
128 .set_next_event = sibyte_next_event,
129 .shift = 32,
130 .irq = 0,
131};
132
133static irqreturn_t sibyte_counter_handler(int irq, void *dev_id)
134{
135 struct clock_event_device *cd = &sibyte_hpt_clockevent;
136
137 cd->event_handler(cd);
138
139 return IRQ_HANDLED;
140}
141
142static struct irqaction sibyte_irqaction = {
143 .handler = sibyte_counter_handler,
144 .flags = IRQF_DISABLED | IRQF_PERCPU,
145 .name = "timer",
146};
147
Ralf Baechled527eef2007-10-19 08:22:38 +0100148void __cpuinit sb1250_clockevent_init(void)
Ralf Baechle9b9ea222007-10-11 23:46:10 +0100149{
150 struct clock_event_device *cd = &sibyte_hpt_clockevent;
151 unsigned int cpu = smp_processor_id();
152 int irq = K_INT_TIMER_0 + cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
Ralf Baechlea904f742006-03-15 00:03:29 +0000154 /* Only have 4 general purpose timers, and we use last one as hpt */
Ralf Baechle9b9ea222007-10-11 23:46:10 +0100155 BUG_ON(cpu > 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 sb1250_mask_irq(cpu, irq);
158
159 /* Map the timer interrupt to ip[4] of this cpu */
Maciej W. Rozycki65bda1a2005-02-22 21:51:30 +0000160 __raw_writeq(IMR_IP4_VAL,
161 IOADDR(A_IMR_REGISTER(cpu, R_IMR_INTERRUPT_MAP_BASE) +
162 (irq << 3)));
Ralf Baechle9b9ea222007-10-11 23:46:10 +0100163 cd->cpumask = cpumask_of_cpu(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
165 sb1250_unmask_irq(cpu, irq);
166 sb1250_steal_irq(irq);
Ralf Baechle9b9ea222007-10-11 23:46:10 +0100167
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 /*
169 * This interrupt is "special" in that it doesn't use the request_irq
170 * way to hook the irq line. The timer interrupt is initialized early
171 * enough to make this a major pain, and it's also firing enough to
172 * warrant a bit of special case code. sb1250_timer_interrupt is
173 * called directly from irq_handler.S when IP[4] is set during an
174 * interrupt
175 */
Ralf Baechle9b9ea222007-10-11 23:46:10 +0100176 setup_irq(irq, &sibyte_irqaction);
177
178 clockevents_register_device(cd);
179}
180
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181/*
Ralf Baechlea904f742006-03-15 00:03:29 +0000182 * The HPT is free running from SB1250_HPT_VALUE down to 0 then starts over
Atsushi Nemoto16b7b2a2006-10-24 00:21:27 +0900183 * again.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 */
Atsushi Nemoto00598562006-11-12 00:10:28 +0900185static cycle_t sb1250_hpt_read(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186{
Ralf Baechlea904f742006-03-15 00:03:29 +0000187 unsigned int count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
Ralf Baechlea904f742006-03-15 00:03:29 +0000189 count = G_SCD_TIMER_CNT(__raw_readq(IOADDR(A_SCD_TIMER_REGISTER(SB1250_HPT_NUM, R_SCD_TIMER_CNT))));
190
Atsushi Nemoto16b7b2a2006-10-24 00:21:27 +0900191 return SB1250_HPT_VALUE - count;
Ralf Baechlea904f742006-03-15 00:03:29 +0000192}
Ralf Baechled527eef2007-10-19 08:22:38 +0100193
194struct clocksource bcm1250_clocksource = {
195 .name = "MIPS",
196 .rating = 200,
197 .read = sb1250_hpt_read,
198 .mask = CLOCKSOURCE_MASK(32),
199 .shift = 32,
200 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
201};
202
203void __init sb1250_clocksource_init(void)
204{
205 struct clocksource *cs = &bcm1250_clocksource;
206
207 clocksource_set_clock(cs, V_SCD_TIMER_FREQ);
208 clocksource_register(cs);
209}
210
211void __init plat_time_init(void)
212{
213 sb1250_clocksource_init();
214 sb1250_clockevent_init();
215}