18
18
* along with this program; if not, write to the Free Software
19
19
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21
#include <linux/clk.h>
21
22
#include <linux/clocksource.h>
22
23
#include <linux/clockchips.h>
24
#include <linux/err.h>
23
25
#include <linux/interrupt.h>
24
26
#include <linux/irq.h>
25
27
#include <linux/io.h>
27
29
#include <asm/hardware/arm_timer.h>
30
* These timers are currently always setup to be clocked at 1MHz.
32
#define TIMER_FREQ_KHZ (1000)
33
#define TIMER_RELOAD (TIMER_FREQ_KHZ * 1000 / HZ)
35
static void __iomem *clksrc_base;
37
static cycle_t sp804_read(struct clocksource *cs)
31
static long __init sp804_get_clock_rate(const char *name)
39
return ~readl(clksrc_base + TIMER_VALUE);
37
clk = clk_get_sys("sp804", name);
39
pr_err("sp804: %s clock not found: %d\n", name,
44
err = clk_enable(clk);
46
pr_err("sp804: %s clock failed to enable: %d\n", name, err);
51
rate = clk_get_rate(clk);
53
pr_err("sp804: %s clock failed to get rate: %ld\n", name, rate);
42
static struct clocksource clocksource_sp804 = {
46
.mask = CLOCKSOURCE_MASK(32),
47
.flags = CLOCK_SOURCE_IS_CONTINUOUS,
50
void __init sp804_clocksource_init(void __iomem *base)
61
void __init sp804_clocksource_init(void __iomem *base, const char *name)
52
struct clocksource *cs = &clocksource_sp804;
63
long rate = sp804_get_clock_rate(name);
56
68
/* setup timer 0 as free-running clocksource */
57
writel(0, clksrc_base + TIMER_CTRL);
58
writel(0xffffffff, clksrc_base + TIMER_LOAD);
59
writel(0xffffffff, clksrc_base + TIMER_VALUE);
69
writel(0, base + TIMER_CTRL);
70
writel(0xffffffff, base + TIMER_LOAD);
71
writel(0xffffffff, base + TIMER_VALUE);
60
72
writel(TIMER_CTRL_32BIT | TIMER_CTRL_ENABLE | TIMER_CTRL_PERIODIC,
61
clksrc_base + TIMER_CTRL);
63
clocksource_register_khz(cs, TIMER_FREQ_KHZ);
75
clocksource_mmio_init(base + TIMER_VALUE, name,
76
rate, 200, 32, clocksource_mmio_readl_down);
67
80
static void __iomem *clkevt_base;
81
static unsigned long clkevt_reload;
70
84
* IRQ handler for the timer
136
149
.dev_id = &sp804_clockevent,
139
void __init sp804_clockevents_init(void __iomem *base, unsigned int timer_irq)
152
void __init sp804_clockevents_init(void __iomem *base, unsigned int irq,
141
155
struct clock_event_device *evt = &sp804_clockevent;
156
long rate = sp804_get_clock_rate(name);
143
161
clkevt_base = base;
162
clkevt_reload = DIV_ROUND_CLOSEST(rate, HZ);
145
evt->irq = timer_irq;
146
evt->mult = div_sc(TIMER_FREQ_KHZ, NSEC_PER_MSEC, evt->shift);
166
evt->mult = div_sc(rate, NSEC_PER_SEC, evt->shift);
147
167
evt->max_delta_ns = clockevent_delta2ns(0xffffffff, evt);
148
168
evt->min_delta_ns = clockevent_delta2ns(0xf, evt);
150
setup_irq(timer_irq, &sp804_timer_irq);
170
setup_irq(irq, &sp804_timer_irq);
151
171
clockevents_register_device(evt);