blob: 445b05ee8511d1aa9371df4ed7e3e8b7470d4876 [file] [log] [blame]
Russell Kinge3887712010-01-14 13:30:16 +00001/*
Rob Herring8a9618f2010-10-06 16:18:08 +01002 * linux/arch/arm/common/timer-sp.c
Russell Kinge3887712010-01-14 13:30:16 +00003 *
4 * Copyright (C) 1999 - 2003 ARM Limited
5 * Copyright (C) 2000 Deep Blue Solutions Ltd
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21#include <linux/clocksource.h>
22#include <linux/clockchips.h>
23#include <linux/interrupt.h>
24#include <linux/irq.h>
25#include <linux/io.h>
26
27#include <asm/hardware/arm_timer.h>
28
Russell Kinge3887712010-01-14 13:30:16 +000029/*
Russell King4ce17552010-01-15 18:19:56 +000030 * These timers are currently always setup to be clocked at 1MHz.
Russell Kinge3887712010-01-14 13:30:16 +000031 */
Russell King4ce17552010-01-15 18:19:56 +000032#define TIMER_FREQ_KHZ (1000)
33#define TIMER_RELOAD (TIMER_FREQ_KHZ * 1000 / HZ)
Russell Kinge3887712010-01-14 13:30:16 +000034
Russell Kinge3887712010-01-14 13:30:16 +000035void __init sp804_clocksource_init(void __iomem *base)
36{
Russell Kinge3887712010-01-14 13:30:16 +000037 /* setup timer 0 as free-running clocksource */
Russell Kingbfe45e02011-05-08 15:33:30 +010038 writel(0, base + TIMER_CTRL);
39 writel(0xffffffff, base + TIMER_LOAD);
40 writel(0xffffffff, base + TIMER_VALUE);
Russell Kinge3887712010-01-14 13:30:16 +000041 writel(TIMER_CTRL_32BIT | TIMER_CTRL_ENABLE | TIMER_CTRL_PERIODIC,
Russell Kingbfe45e02011-05-08 15:33:30 +010042 base + TIMER_CTRL);
Russell Kinge3887712010-01-14 13:30:16 +000043
Russell Kingbfe45e02011-05-08 15:33:30 +010044 clocksource_mmio_init(base + TIMER_VALUE, "timer3",
45 TIMER_FREQ_KHZ * 1000, 200, 32, clocksource_mmio_readl_down);
Russell Kinge3887712010-01-14 13:30:16 +000046}
47
48
49static void __iomem *clkevt_base;
50
51/*
52 * IRQ handler for the timer
53 */
54static irqreturn_t sp804_timer_interrupt(int irq, void *dev_id)
55{
56 struct clock_event_device *evt = dev_id;
57
58 /* clear the interrupt */
59 writel(1, clkevt_base + TIMER_INTCLR);
60
61 evt->event_handler(evt);
62
63 return IRQ_HANDLED;
64}
65
66static void sp804_set_mode(enum clock_event_mode mode,
67 struct clock_event_device *evt)
68{
69 unsigned long ctrl = TIMER_CTRL_32BIT | TIMER_CTRL_IE;
70
71 writel(ctrl, clkevt_base + TIMER_CTRL);
72
73 switch (mode) {
74 case CLOCK_EVT_MODE_PERIODIC:
75 writel(TIMER_RELOAD, clkevt_base + TIMER_LOAD);
76 ctrl |= TIMER_CTRL_PERIODIC | TIMER_CTRL_ENABLE;
77 break;
78
79 case CLOCK_EVT_MODE_ONESHOT:
80 /* period set, and timer enabled in 'next_event' hook */
81 ctrl |= TIMER_CTRL_ONESHOT;
82 break;
83
84 case CLOCK_EVT_MODE_UNUSED:
85 case CLOCK_EVT_MODE_SHUTDOWN:
86 default:
87 break;
88 }
89
90 writel(ctrl, clkevt_base + TIMER_CTRL);
91}
92
93static int sp804_set_next_event(unsigned long next,
94 struct clock_event_device *evt)
95{
96 unsigned long ctrl = readl(clkevt_base + TIMER_CTRL);
97
98 writel(next, clkevt_base + TIMER_LOAD);
99 writel(ctrl | TIMER_CTRL_ENABLE, clkevt_base + TIMER_CTRL);
100
101 return 0;
102}
103
104static struct clock_event_device sp804_clockevent = {
105 .name = "timer0",
106 .shift = 32,
107 .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT,
108 .set_mode = sp804_set_mode,
109 .set_next_event = sp804_set_next_event,
110 .rating = 300,
111 .cpumask = cpu_all_mask,
112};
113
114static struct irqaction sp804_timer_irq = {
115 .name = "timer",
116 .flags = IRQF_DISABLED | IRQF_TIMER | IRQF_IRQPOLL,
117 .handler = sp804_timer_interrupt,
118 .dev_id = &sp804_clockevent,
119};
120
121void __init sp804_clockevents_init(void __iomem *base, unsigned int timer_irq)
122{
123 struct clock_event_device *evt = &sp804_clockevent;
124
125 clkevt_base = base;
126
127 evt->irq = timer_irq;
Russell King4ce17552010-01-15 18:19:56 +0000128 evt->mult = div_sc(TIMER_FREQ_KHZ, NSEC_PER_MSEC, evt->shift);
Russell Kinge3887712010-01-14 13:30:16 +0000129 evt->max_delta_ns = clockevent_delta2ns(0xffffffff, evt);
130 evt->min_delta_ns = clockevent_delta2ns(0xf, evt);
131
132 setup_irq(timer_irq, &sp804_timer_irq);
133 clockevents_register_device(evt);
134}