blob: 9ca32f55728bda7e8d74ee01883ccfcf2cdb05f7 [file] [log] [blame]
Uwe Kleine-König9918cda2007-02-16 15:36:55 +01001/*
Uwe Kleine-König724ce5e2008-02-15 08:41:06 +01002 * arch/arm/mach-ns9xxx/time-ns9360.c
Uwe Kleine-König9918cda2007-02-16 15:36:55 +01003 *
Uwe Kleine-König724ce5e2008-02-15 08:41:06 +01004 * Copyright (C) 2006,2007 by Digi International Inc.
Uwe Kleine-König9918cda2007-02-16 15:36:55 +01005 * 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
Russell Kinga09e64f2008-08-05 16:14:15 +010018#include <mach/processor-ns9360.h>
19#include <mach/regs-sys-ns9360.h>
20#include <mach/irqs.h>
21#include <mach/system.h>
Uwe Kleine-König9918cda2007-02-16 15:36:55 +010022#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
Magnus Damm8e196082009-04-21 12:24:00 -070028static cycle_t ns9360_clocksource_read(struct clocksource *cs)
Uwe Kleine-Königcef59752007-09-30 20:35:48 +010029{
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
Uwe Kleine-König724ce5e2008-02-15 08:41:06 +010033static struct clocksource ns9360_clocksource = {
34 .name = "ns9360-timer" __stringify(TIMER_CLOCKSOURCE),
Uwe Kleine-Königcef59752007-09-30 20:35:48 +010035 .rating = 300,
Uwe Kleine-König724ce5e2008-02-15 08:41:06 +010036 .read = ns9360_clocksource_read,
Uwe Kleine-Königcef59752007-09-30 20:35:48 +010037 .mask = CLOCKSOURCE_MASK(32),
Uwe Kleine-Königcef59752007-09-30 20:35:48 +010038 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
39};
40
Uwe Kleine-König724ce5e2008-02-15 08:41:06 +010041static void ns9360_clockevent_setmode(enum clock_event_mode mode,
Uwe Kleine-Königc0bb87f2007-09-30 20:36:00 +010042 struct clock_event_device *clk)
43{
Uwe Kleine-König361c7ad2007-09-30 20:36:33 +010044 u32 tc = __raw_readl(SYS_TC(TIMER_CLOCKEVENT));
Uwe Kleine-Königc0bb87f2007-09-30 20:36:00 +010045
Uwe Kleine-König724ce5e2008-02-15 08:41:06 +010046 switch (mode) {
Uwe Kleine-Königc0bb87f2007-09-30 20:36:00 +010047 case CLOCK_EVT_MODE_PERIODIC:
Uwe Kleine-König361c7ad2007-09-30 20:36:33 +010048 __raw_writel(latch, SYS_TRC(TIMER_CLOCKEVENT));
Uwe Kleine-Königc0bb87f2007-09-30 20:36:00 +010049 REGSET(tc, SYS_TCx, REN, EN);
50 REGSET(tc, SYS_TCx, INTS, EN);
51 REGSET(tc, SYS_TCx, TEN, EN);
52 break;
53
54 case CLOCK_EVT_MODE_ONESHOT:
55 REGSET(tc, SYS_TCx, REN, DIS);
56 REGSET(tc, SYS_TCx, INTS, EN);
57
58 /* fall through */
59
60 case CLOCK_EVT_MODE_UNUSED:
61 case CLOCK_EVT_MODE_SHUTDOWN:
62 case CLOCK_EVT_MODE_RESUME:
63 default:
64 REGSET(tc, SYS_TCx, TEN, DIS);
65 break;
66 }
67
Uwe Kleine-König361c7ad2007-09-30 20:36:33 +010068 __raw_writel(tc, SYS_TC(TIMER_CLOCKEVENT));
Uwe Kleine-Königc0bb87f2007-09-30 20:36:00 +010069}
70
Uwe Kleine-König724ce5e2008-02-15 08:41:06 +010071static int ns9360_clockevent_setnextevent(unsigned long evt,
Uwe Kleine-Königc0bb87f2007-09-30 20:36:00 +010072 struct clock_event_device *clk)
73{
Uwe Kleine-König361c7ad2007-09-30 20:36:33 +010074 u32 tc = __raw_readl(SYS_TC(TIMER_CLOCKEVENT));
Uwe Kleine-Königc0bb87f2007-09-30 20:36:00 +010075
76 if (REGGET(tc, SYS_TCx, TEN)) {
77 REGSET(tc, SYS_TCx, TEN, DIS);
Uwe Kleine-König361c7ad2007-09-30 20:36:33 +010078 __raw_writel(tc, SYS_TC(TIMER_CLOCKEVENT));
Uwe Kleine-Königc0bb87f2007-09-30 20:36:00 +010079 }
80
81 REGSET(tc, SYS_TCx, TEN, EN);
82
Uwe Kleine-König361c7ad2007-09-30 20:36:33 +010083 __raw_writel(evt, SYS_TRC(TIMER_CLOCKEVENT));
Uwe Kleine-Königc0bb87f2007-09-30 20:36:00 +010084
Uwe Kleine-König361c7ad2007-09-30 20:36:33 +010085 __raw_writel(tc, SYS_TC(TIMER_CLOCKEVENT));
Uwe Kleine-Königc0bb87f2007-09-30 20:36:00 +010086
87 return 0;
88}
89
Uwe Kleine-König724ce5e2008-02-15 08:41:06 +010090static struct clock_event_device ns9360_clockevent_device = {
91 .name = "ns9360-timer" __stringify(TIMER_CLOCKEVENT),
Uwe Kleine-Königc0bb87f2007-09-30 20:36:00 +010092 .shift = 20,
93 .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT,
Uwe Kleine-König724ce5e2008-02-15 08:41:06 +010094 .set_mode = ns9360_clockevent_setmode,
95 .set_next_event = ns9360_clockevent_setnextevent,
Uwe Kleine-Königc0bb87f2007-09-30 20:36:00 +010096};
97
Uwe Kleine-König724ce5e2008-02-15 08:41:06 +010098static irqreturn_t ns9360_clockevent_handler(int irq, void *dev_id)
Uwe Kleine-Königc0bb87f2007-09-30 20:36:00 +010099{
Uwe Kleine-König724ce5e2008-02-15 08:41:06 +0100100 int timerno = irq - IRQ_NS9360_TIMER0;
Uwe Kleine-Königc0bb87f2007-09-30 20:36:00 +0100101 u32 tc;
102
Uwe Kleine-König724ce5e2008-02-15 08:41:06 +0100103 struct clock_event_device *evt = &ns9360_clockevent_device;
Uwe Kleine-Königc0bb87f2007-09-30 20:36:00 +0100104
105 /* clear irq */
Uwe Kleine-König361c7ad2007-09-30 20:36:33 +0100106 tc = __raw_readl(SYS_TC(timerno));
Uwe Kleine-Königc0bb87f2007-09-30 20:36:00 +0100107 if (REGGET(tc, SYS_TCx, REN) == SYS_TCx_REN_DIS) {
108 REGSET(tc, SYS_TCx, TEN, DIS);
Uwe Kleine-König361c7ad2007-09-30 20:36:33 +0100109 __raw_writel(tc, SYS_TC(timerno));
Uwe Kleine-Königc0bb87f2007-09-30 20:36:00 +0100110 }
111 REGSET(tc, SYS_TCx, INTC, SET);
Uwe Kleine-König361c7ad2007-09-30 20:36:33 +0100112 __raw_writel(tc, SYS_TC(timerno));
Uwe Kleine-Königc0bb87f2007-09-30 20:36:00 +0100113 REGSET(tc, SYS_TCx, INTC, UNSET);
Uwe Kleine-König361c7ad2007-09-30 20:36:33 +0100114 __raw_writel(tc, SYS_TC(timerno));
Uwe Kleine-Königc0bb87f2007-09-30 20:36:00 +0100115
116 evt->event_handler(evt);
117
118 return IRQ_HANDLED;
119}
120
Uwe Kleine-König724ce5e2008-02-15 08:41:06 +0100121static struct irqaction ns9360_clockevent_action = {
122 .name = "ns9360-timer" __stringify(TIMER_CLOCKEVENT),
Uwe Kleine-Königc0bb87f2007-09-30 20:36:00 +0100123 .flags = IRQF_DISABLED | IRQF_TIMER | IRQF_IRQPOLL,
Uwe Kleine-König724ce5e2008-02-15 08:41:06 +0100124 .handler = ns9360_clockevent_handler,
Uwe Kleine-Königc0bb87f2007-09-30 20:36:00 +0100125};
126
Uwe Kleine-König724ce5e2008-02-15 08:41:06 +0100127static void __init ns9360_timer_init(void)
Uwe Kleine-König9918cda2007-02-16 15:36:55 +0100128{
129 int tc;
130
Uwe Kleine-König361c7ad2007-09-30 20:36:33 +0100131 tc = __raw_readl(SYS_TC(TIMER_CLOCKSOURCE));
Uwe Kleine-Königcef59752007-09-30 20:35:48 +0100132 if (REGGET(tc, SYS_TCx, TEN)) {
133 REGSET(tc, SYS_TCx, TEN, DIS);
Uwe Kleine-König361c7ad2007-09-30 20:36:33 +0100134 __raw_writel(tc, SYS_TC(TIMER_CLOCKSOURCE));
Uwe Kleine-Königcef59752007-09-30 20:35:48 +0100135 }
136
Uwe Kleine-König361c7ad2007-09-30 20:36:33 +0100137 __raw_writel(0, SYS_TRC(TIMER_CLOCKSOURCE));
Uwe Kleine-Königcef59752007-09-30 20:35:48 +0100138
139 REGSET(tc, SYS_TCx, TEN, EN);
140 REGSET(tc, SYS_TCx, TDBG, STOP);
141 REGSET(tc, SYS_TCx, TLCS, CPU);
142 REGSET(tc, SYS_TCx, TM, IEE);
143 REGSET(tc, SYS_TCx, INTS, DIS);
144 REGSET(tc, SYS_TCx, UDS, UP);
145 REGSET(tc, SYS_TCx, TSZ, 32);
146 REGSET(tc, SYS_TCx, REN, EN);
147
Uwe Kleine-König361c7ad2007-09-30 20:36:33 +0100148 __raw_writel(tc, SYS_TC(TIMER_CLOCKSOURCE));
Uwe Kleine-Königcef59752007-09-30 20:35:48 +0100149
Russell Kingb460ddb2010-12-13 13:18:32 +0000150 clocksource_register_hz(&ns9360_clocksource, ns9360_cpuclock());
Uwe Kleine-Königc0bb87f2007-09-30 20:36:00 +0100151
Uwe Kleine-König724ce5e2008-02-15 08:41:06 +0100152 latch = SH_DIV(ns9360_cpuclock(), HZ, 0);
Uwe Kleine-Königc0bb87f2007-09-30 20:36:00 +0100153
Uwe Kleine-König361c7ad2007-09-30 20:36:33 +0100154 tc = __raw_readl(SYS_TC(TIMER_CLOCKEVENT));
Uwe Kleine-Königc0bb87f2007-09-30 20:36:00 +0100155 REGSET(tc, SYS_TCx, TEN, DIS);
156 REGSET(tc, SYS_TCx, TDBG, STOP);
157 REGSET(tc, SYS_TCx, TLCS, CPU);
158 REGSET(tc, SYS_TCx, TM, IEE);
159 REGSET(tc, SYS_TCx, INTS, DIS);
160 REGSET(tc, SYS_TCx, UDS, DOWN);
161 REGSET(tc, SYS_TCx, TSZ, 32);
162 REGSET(tc, SYS_TCx, REN, EN);
Uwe Kleine-König361c7ad2007-09-30 20:36:33 +0100163 __raw_writel(tc, SYS_TC(TIMER_CLOCKEVENT));
Uwe Kleine-Königc0bb87f2007-09-30 20:36:00 +0100164
Uwe Kleine-König724ce5e2008-02-15 08:41:06 +0100165 ns9360_clockevent_device.mult = div_sc(ns9360_cpuclock(),
166 NSEC_PER_SEC, ns9360_clockevent_device.shift);
167 ns9360_clockevent_device.max_delta_ns =
168 clockevent_delta2ns(-1, &ns9360_clockevent_device);
169 ns9360_clockevent_device.min_delta_ns =
170 clockevent_delta2ns(1, &ns9360_clockevent_device);
Uwe Kleine-Königc0bb87f2007-09-30 20:36:00 +0100171
Rusty Russell320ab2b2008-12-13 21:20:26 +1030172 ns9360_clockevent_device.cpumask = cpumask_of(0);
Uwe Kleine-König724ce5e2008-02-15 08:41:06 +0100173 clockevents_register_device(&ns9360_clockevent_device);
Uwe Kleine-Königc0bb87f2007-09-30 20:36:00 +0100174
Uwe Kleine-König724ce5e2008-02-15 08:41:06 +0100175 setup_irq(IRQ_NS9360_TIMER0 + TIMER_CLOCKEVENT,
176 &ns9360_clockevent_action);
Uwe Kleine-König9918cda2007-02-16 15:36:55 +0100177}
178
Uwe Kleine-König724ce5e2008-02-15 08:41:06 +0100179struct sys_timer ns9360_timer = {
180 .init = ns9360_timer_init,
Uwe Kleine-König9918cda2007-02-16 15:36:55 +0100181};