blob: c3dd1f4acb9919e20141c2f187c3ec7ee7f51be2 [file] [log] [blame]
Uwe Kleine-König9918cda2007-02-16 15:36:55 +01001/*
2 * arch/arm/mach-ns9xxx/time.c
3 *
4 * Copyright (C) 2006 by Digi International Inc.
5 * All rights reserved.
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License version 2 as published by
9 * the Free Software Foundation.
10 */
11#include <linux/jiffies.h>
12#include <linux/interrupt.h>
13#include <linux/irq.h>
Uwe Kleine-Königcef59752007-09-30 20:35:48 +010014#include <linux/stringify.h>
15#include <linux/clocksource.h>
Uwe Kleine-Königc0bb87f2007-09-30 20:36:00 +010016#include <linux/clockchips.h>
Uwe Kleine-Königcef59752007-09-30 20:35:48 +010017
Uwe Kleine-König9918cda2007-02-16 15:36:55 +010018#include <asm/arch-ns9xxx/regs-sys.h>
19#include <asm/arch-ns9xxx/clock.h>
20#include <asm/arch-ns9xxx/irqs.h>
21#include <asm/arch/system.h>
22#include "generic.h"
23
Uwe Kleine-Königc0bb87f2007-09-30 20:36:00 +010024#define TIMER_CLOCKSOURCE 0
25#define TIMER_CLOCKEVENT 1
26static u32 latch;
Uwe Kleine-König9918cda2007-02-16 15:36:55 +010027
Uwe Kleine-Königcef59752007-09-30 20:35:48 +010028static cycle_t ns9xxx_clocksource_read(void)
29{
Uwe Kleine-König361c7ad2007-09-30 20:36:33 +010030 return __raw_readl(SYS_TR(TIMER_CLOCKSOURCE));
Uwe Kleine-Königcef59752007-09-30 20:35:48 +010031}
32
33static struct clocksource ns9xxx_clocksource = {
34 .name = "ns9xxx-timer" __stringify(TIMER_CLOCKSOURCE),
35 .rating = 300,
36 .read = ns9xxx_clocksource_read,
37 .mask = CLOCKSOURCE_MASK(32),
38 .shift = 20,
39 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
40};
41
Uwe Kleine-Königc0bb87f2007-09-30 20:36:00 +010042static void ns9xxx_clockevent_setmode(enum clock_event_mode mode,
43 struct clock_event_device *clk)
44{
Uwe Kleine-König361c7ad2007-09-30 20:36:33 +010045 u32 tc = __raw_readl(SYS_TC(TIMER_CLOCKEVENT));
Uwe Kleine-Königc0bb87f2007-09-30 20:36:00 +010046
47 switch(mode) {
48 case CLOCK_EVT_MODE_PERIODIC:
Uwe Kleine-König361c7ad2007-09-30 20:36:33 +010049 __raw_writel(latch, SYS_TRC(TIMER_CLOCKEVENT));
Uwe Kleine-Königc0bb87f2007-09-30 20:36:00 +010050 REGSET(tc, SYS_TCx, REN, EN);
51 REGSET(tc, SYS_TCx, INTS, EN);
52 REGSET(tc, SYS_TCx, TEN, EN);
53 break;
54
55 case CLOCK_EVT_MODE_ONESHOT:
56 REGSET(tc, SYS_TCx, REN, DIS);
57 REGSET(tc, SYS_TCx, INTS, EN);
58
59 /* fall through */
60
61 case CLOCK_EVT_MODE_UNUSED:
62 case CLOCK_EVT_MODE_SHUTDOWN:
63 case CLOCK_EVT_MODE_RESUME:
64 default:
65 REGSET(tc, SYS_TCx, TEN, DIS);
66 break;
67 }
68
Uwe Kleine-König361c7ad2007-09-30 20:36:33 +010069 __raw_writel(tc, SYS_TC(TIMER_CLOCKEVENT));
Uwe Kleine-Königc0bb87f2007-09-30 20:36:00 +010070}
71
72static int ns9xxx_clockevent_setnextevent(unsigned long evt,
73 struct clock_event_device *clk)
74{
Uwe Kleine-König361c7ad2007-09-30 20:36:33 +010075 u32 tc = __raw_readl(SYS_TC(TIMER_CLOCKEVENT));
Uwe Kleine-Königc0bb87f2007-09-30 20:36:00 +010076
77 if (REGGET(tc, SYS_TCx, TEN)) {
78 REGSET(tc, SYS_TCx, TEN, DIS);
Uwe Kleine-König361c7ad2007-09-30 20:36:33 +010079 __raw_writel(tc, SYS_TC(TIMER_CLOCKEVENT));
Uwe Kleine-Königc0bb87f2007-09-30 20:36:00 +010080 }
81
82 REGSET(tc, SYS_TCx, TEN, EN);
83
Uwe Kleine-König361c7ad2007-09-30 20:36:33 +010084 __raw_writel(evt, SYS_TRC(TIMER_CLOCKEVENT));
Uwe Kleine-Königc0bb87f2007-09-30 20:36:00 +010085
Uwe Kleine-König361c7ad2007-09-30 20:36:33 +010086 __raw_writel(tc, SYS_TC(TIMER_CLOCKEVENT));
Uwe Kleine-Königc0bb87f2007-09-30 20:36:00 +010087
88 return 0;
89}
90
91static struct clock_event_device ns9xxx_clockevent_device = {
92 .name = "ns9xxx-timer" __stringify(TIMER_CLOCKEVENT),
93 .shift = 20,
94 .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT,
95 .set_mode = ns9xxx_clockevent_setmode,
96 .set_next_event = ns9xxx_clockevent_setnextevent,
97};
98
99static irqreturn_t ns9xxx_clockevent_handler(int irq, void *dev_id)
100{
101 int timerno = irq - IRQ_TIMER0;
102 u32 tc;
103
104 struct clock_event_device *evt = &ns9xxx_clockevent_device;
105
106 /* clear irq */
Uwe Kleine-König361c7ad2007-09-30 20:36:33 +0100107 tc = __raw_readl(SYS_TC(timerno));
Uwe Kleine-Königc0bb87f2007-09-30 20:36:00 +0100108 if (REGGET(tc, SYS_TCx, REN) == SYS_TCx_REN_DIS) {
109 REGSET(tc, SYS_TCx, TEN, DIS);
Uwe Kleine-König361c7ad2007-09-30 20:36:33 +0100110 __raw_writel(tc, SYS_TC(timerno));
Uwe Kleine-Königc0bb87f2007-09-30 20:36:00 +0100111 }
112 REGSET(tc, SYS_TCx, INTC, SET);
Uwe Kleine-König361c7ad2007-09-30 20:36:33 +0100113 __raw_writel(tc, SYS_TC(timerno));
Uwe Kleine-Königc0bb87f2007-09-30 20:36:00 +0100114 REGSET(tc, SYS_TCx, INTC, UNSET);
Uwe Kleine-König361c7ad2007-09-30 20:36:33 +0100115 __raw_writel(tc, SYS_TC(timerno));
Uwe Kleine-Königc0bb87f2007-09-30 20:36:00 +0100116
117 evt->event_handler(evt);
118
119 return IRQ_HANDLED;
120}
121
122static struct irqaction ns9xxx_clockevent_action = {
123 .name = "ns9xxx-timer" __stringify(TIMER_CLOCKEVENT),
124 .flags = IRQF_DISABLED | IRQF_TIMER | IRQF_IRQPOLL,
125 .handler = ns9xxx_clockevent_handler,
126};
127
Uwe Kleine-König9918cda2007-02-16 15:36:55 +0100128static void __init ns9xxx_timer_init(void)
129{
130 int tc;
131
Uwe Kleine-König361c7ad2007-09-30 20:36:33 +0100132 tc = __raw_readl(SYS_TC(TIMER_CLOCKSOURCE));
Uwe Kleine-Königcef59752007-09-30 20:35:48 +0100133 if (REGGET(tc, SYS_TCx, TEN)) {
134 REGSET(tc, SYS_TCx, TEN, DIS);
Uwe Kleine-König361c7ad2007-09-30 20:36:33 +0100135 __raw_writel(tc, SYS_TC(TIMER_CLOCKSOURCE));
Uwe Kleine-Königcef59752007-09-30 20:35:48 +0100136 }
137
Uwe Kleine-König361c7ad2007-09-30 20:36:33 +0100138 __raw_writel(0, SYS_TRC(TIMER_CLOCKSOURCE));
Uwe Kleine-Königcef59752007-09-30 20:35:48 +0100139
140 REGSET(tc, SYS_TCx, TEN, EN);
141 REGSET(tc, SYS_TCx, TDBG, STOP);
142 REGSET(tc, SYS_TCx, TLCS, CPU);
143 REGSET(tc, SYS_TCx, TM, IEE);
144 REGSET(tc, SYS_TCx, INTS, DIS);
145 REGSET(tc, SYS_TCx, UDS, UP);
146 REGSET(tc, SYS_TCx, TSZ, 32);
147 REGSET(tc, SYS_TCx, REN, EN);
148
Uwe Kleine-König361c7ad2007-09-30 20:36:33 +0100149 __raw_writel(tc, SYS_TC(TIMER_CLOCKSOURCE));
Uwe Kleine-Königcef59752007-09-30 20:35:48 +0100150
151 ns9xxx_clocksource.mult = clocksource_hz2mult(ns9xxx_cpuclock(),
152 ns9xxx_clocksource.shift);
153
154 clocksource_register(&ns9xxx_clocksource);
Uwe Kleine-Königc0bb87f2007-09-30 20:36:00 +0100155
156 latch = SH_DIV(ns9xxx_cpuclock(), HZ, 0);
157
Uwe Kleine-König361c7ad2007-09-30 20:36:33 +0100158 tc = __raw_readl(SYS_TC(TIMER_CLOCKEVENT));
Uwe Kleine-Königc0bb87f2007-09-30 20:36:00 +0100159 REGSET(tc, SYS_TCx, TEN, DIS);
160 REGSET(tc, SYS_TCx, TDBG, STOP);
161 REGSET(tc, SYS_TCx, TLCS, CPU);
162 REGSET(tc, SYS_TCx, TM, IEE);
163 REGSET(tc, SYS_TCx, INTS, DIS);
164 REGSET(tc, SYS_TCx, UDS, DOWN);
165 REGSET(tc, SYS_TCx, TSZ, 32);
166 REGSET(tc, SYS_TCx, REN, EN);
Uwe Kleine-König361c7ad2007-09-30 20:36:33 +0100167 __raw_writel(tc, SYS_TC(TIMER_CLOCKEVENT));
Uwe Kleine-Königc0bb87f2007-09-30 20:36:00 +0100168
169 ns9xxx_clockevent_device.mult = div_sc(ns9xxx_cpuclock(),
170 NSEC_PER_SEC, ns9xxx_clockevent_device.shift);
171 ns9xxx_clockevent_device.max_delta_ns =
172 clockevent_delta2ns(-1, &ns9xxx_clockevent_device);
173 ns9xxx_clockevent_device.min_delta_ns =
174 clockevent_delta2ns(1, &ns9xxx_clockevent_device);
175
176 ns9xxx_clockevent_device.cpumask = cpumask_of_cpu(0);
177 clockevents_register_device(&ns9xxx_clockevent_device);
178
179 setup_irq(IRQ_TIMER0 + TIMER_CLOCKEVENT, &ns9xxx_clockevent_action);
Uwe Kleine-König9918cda2007-02-16 15:36:55 +0100180}
181
182struct sys_timer ns9xxx_timer = {
183 .init = ns9xxx_timer_init,
Uwe Kleine-König9918cda2007-02-16 15:36:55 +0100184};