blob: 054a8a61e3798204622211eac2e5efcbb63b1171 [file] [log] [blame]
Sascha Hauerbb6d8c82006-06-19 15:27:53 +01001/*
2 * arch/arm/mach-netx/time.c
3 *
4 * Copyright (c) 2005 Sascha Hauer <s.hauer@pengutronix.de>, Pengutronix
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2
8 * as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19
20#include <linux/init.h>
21#include <linux/interrupt.h>
Sascha Hauer1a815ae2006-12-12 09:23:45 +010022#include <linux/irq.h>
23#include <linux/clocksource.h>
Uwe Kleine-König2fcfe6b2008-12-09 21:57:24 +010024#include <linux/clockchips.h>
Russell Kingfced80c2008-09-06 12:10:45 +010025#include <linux/io.h>
Sascha Hauerbb6d8c82006-06-19 15:27:53 +010026
Russell Kinga09e64f2008-08-05 16:14:15 +010027#include <mach/hardware.h>
Sascha Hauerbb6d8c82006-06-19 15:27:53 +010028#include <asm/mach/time.h>
Russell Kinga09e64f2008-08-05 16:14:15 +010029#include <mach/netx-regs.h>
Sascha Hauerbb6d8c82006-06-19 15:27:53 +010030
Uwe Kleine-König5e3e2762013-11-12 20:56:02 +010031#define NETX_CLOCK_FREQ 100000000
32#define NETX_LATCH DIV_ROUND_CLOSEST(NETX_CLOCK_FREQ, HZ)
33
Uwe Kleine-König2fcfe6b2008-12-09 21:57:24 +010034#define TIMER_CLOCKEVENT 0
Uwe Kleine-König24e78572008-12-09 21:57:22 +010035#define TIMER_CLOCKSOURCE 1
36
Viresh Kumarc22437b2015-02-27 13:39:52 +053037static inline void timer_shutdown(struct clock_event_device *evt)
Uwe Kleine-König2fcfe6b2008-12-09 21:57:24 +010038{
Uwe Kleine-König2fcfe6b2008-12-09 21:57:24 +010039 /* disable timer */
40 writel(0, NETX_GPIO_COUNTER_CTRL(TIMER_CLOCKEVENT));
Viresh Kumarc22437b2015-02-27 13:39:52 +053041}
Uwe Kleine-König2fcfe6b2008-12-09 21:57:24 +010042
Viresh Kumarc22437b2015-02-27 13:39:52 +053043static int netx_shutdown(struct clock_event_device *evt)
44{
45 timer_shutdown(evt);
Uwe Kleine-König2fcfe6b2008-12-09 21:57:24 +010046
Viresh Kumarc22437b2015-02-27 13:39:52 +053047 return 0;
48}
Uwe Kleine-König2fcfe6b2008-12-09 21:57:24 +010049
Viresh Kumarc22437b2015-02-27 13:39:52 +053050static int netx_set_oneshot(struct clock_event_device *evt)
51{
52 u32 tmode = NETX_GPIO_COUNTER_CTRL_IRQ_EN | NETX_GPIO_COUNTER_CTRL_RUN;
Uwe Kleine-König2fcfe6b2008-12-09 21:57:24 +010053
Viresh Kumarc22437b2015-02-27 13:39:52 +053054 timer_shutdown(evt);
55 writel(0, NETX_GPIO_COUNTER_MAX(TIMER_CLOCKEVENT));
Uwe Kleine-König2fcfe6b2008-12-09 21:57:24 +010056 writel(tmode, NETX_GPIO_COUNTER_CTRL(TIMER_CLOCKEVENT));
Viresh Kumarc22437b2015-02-27 13:39:52 +053057
58 return 0;
59}
60
61static int netx_set_periodic(struct clock_event_device *evt)
62{
63 u32 tmode = NETX_GPIO_COUNTER_CTRL_RST_EN |
64 NETX_GPIO_COUNTER_CTRL_IRQ_EN | NETX_GPIO_COUNTER_CTRL_RUN;
65
66 timer_shutdown(evt);
67 writel(NETX_LATCH, NETX_GPIO_COUNTER_MAX(TIMER_CLOCKEVENT));
68 writel(tmode, NETX_GPIO_COUNTER_CTRL(TIMER_CLOCKEVENT));
69
70 return 0;
Uwe Kleine-König2fcfe6b2008-12-09 21:57:24 +010071}
72
73static int netx_set_next_event(unsigned long evt,
74 struct clock_event_device *clk)
75{
76 writel(0 - evt, NETX_GPIO_COUNTER_CURRENT(TIMER_CLOCKEVENT));
77 return 0;
78}
79
80static struct clock_event_device netx_clockevent = {
81 .name = "netx-timer" __stringify(TIMER_CLOCKEVENT),
Uwe Kleine-König2fcfe6b2008-12-09 21:57:24 +010082 .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT,
83 .set_next_event = netx_set_next_event,
Viresh Kumarc22437b2015-02-27 13:39:52 +053084 .set_state_shutdown = netx_shutdown,
85 .set_state_periodic = netx_set_periodic,
86 .set_state_oneshot = netx_set_oneshot,
87 .tick_resume = netx_shutdown,
Uwe Kleine-König2fcfe6b2008-12-09 21:57:24 +010088};
89
Sascha Hauerbb6d8c82006-06-19 15:27:53 +010090/*
Sascha Hauerbb6d8c82006-06-19 15:27:53 +010091 * IRQ handler for the timer
92 */
93static irqreturn_t
Linus Torvalds0cd61b62006-10-06 10:53:39 -070094netx_timer_interrupt(int irq, void *dev_id)
Sascha Hauerbb6d8c82006-06-19 15:27:53 +010095{
Uwe Kleine-König2fcfe6b2008-12-09 21:57:24 +010096 struct clock_event_device *evt = &netx_clockevent;
Sascha Hauer1a815ae2006-12-12 09:23:45 +010097
Sascha Hauerbb6d8c82006-06-19 15:27:53 +010098 /* acknowledge interrupt */
99 writel(COUNTER_BIT(0), NETX_GPIO_IRQ);
100
Uwe Kleine-König2fcfe6b2008-12-09 21:57:24 +0100101 evt->event_handler(evt);
102
Sascha Hauerbb6d8c82006-06-19 15:27:53 +0100103 return IRQ_HANDLED;
104}
105
Sascha Hauerbb6d8c82006-06-19 15:27:53 +0100106static struct irqaction netx_timer_irq = {
Uwe Kleine-König98538482008-12-09 21:57:21 +0100107 .name = "NetX Timer Tick",
Michael Opdenacker78f6db92014-03-04 22:04:50 +0100108 .flags = IRQF_TIMER | IRQF_IRQPOLL,
Uwe Kleine-König98538482008-12-09 21:57:21 +0100109 .handler = netx_timer_interrupt,
Sascha Hauerbb6d8c82006-06-19 15:27:53 +0100110};
111
112/*
113 * Set up timer interrupt
114 */
Stephen Warren6bb27d72012-11-08 12:40:59 -0700115void __init netx_timer_init(void)
Sascha Hauerbb6d8c82006-06-19 15:27:53 +0100116{
117 /* disable timer initially */
118 writel(0, NETX_GPIO_COUNTER_CTRL(0));
119
120 /* Reset the timer value to zero */
121 writel(0, NETX_GPIO_COUNTER_CURRENT(0));
122
Uwe Kleine-König5e3e2762013-11-12 20:56:02 +0100123 writel(NETX_LATCH, NETX_GPIO_COUNTER_MAX(0));
Sascha Hauerbb6d8c82006-06-19 15:27:53 +0100124
125 /* acknowledge interrupt */
126 writel(COUNTER_BIT(0), NETX_GPIO_IRQ);
127
Uwe Kleine-König98538482008-12-09 21:57:21 +0100128 /* Enable the interrupt in the specific timer
129 * register and start timer
130 */
Sascha Hauerbb6d8c82006-06-19 15:27:53 +0100131 writel(COUNTER_BIT(0), NETX_GPIO_IRQ_ENABLE);
132 writel(NETX_GPIO_COUNTER_CTRL_IRQ_EN | NETX_GPIO_COUNTER_CTRL_RUN,
Uwe Kleine-König98538482008-12-09 21:57:21 +0100133 NETX_GPIO_COUNTER_CTRL(0));
Sascha Hauerbb6d8c82006-06-19 15:27:53 +0100134
135 setup_irq(NETX_IRQ_TIMER0, &netx_timer_irq);
Sascha Hauer1a815ae2006-12-12 09:23:45 +0100136
137 /* Setup timer one for clocksource */
Uwe Kleine-König24e78572008-12-09 21:57:22 +0100138 writel(0, NETX_GPIO_COUNTER_CTRL(TIMER_CLOCKSOURCE));
139 writel(0, NETX_GPIO_COUNTER_CURRENT(TIMER_CLOCKSOURCE));
140 writel(0xffffffff, NETX_GPIO_COUNTER_MAX(TIMER_CLOCKSOURCE));
Sascha Hauer1a815ae2006-12-12 09:23:45 +0100141
Uwe Kleine-König98538482008-12-09 21:57:21 +0100142 writel(NETX_GPIO_COUNTER_CTRL_RUN,
Uwe Kleine-König24e78572008-12-09 21:57:22 +0100143 NETX_GPIO_COUNTER_CTRL(TIMER_CLOCKSOURCE));
Sascha Hauer1a815ae2006-12-12 09:23:45 +0100144
Russell King234b6ced2011-05-08 14:09:47 +0100145 clocksource_mmio_init(NETX_GPIO_COUNTER_CURRENT(TIMER_CLOCKSOURCE),
Uwe Kleine-König5e3e2762013-11-12 20:56:02 +0100146 "netx_timer", NETX_CLOCK_FREQ, 200, 32, clocksource_mmio_readl_up);
Uwe Kleine-König2fcfe6b2008-12-09 21:57:24 +0100147
Uwe Kleine-König2fcfe6b2008-12-09 21:57:24 +0100148 /* with max_delta_ns >= delta2ns(0x800) the system currently runs fine.
149 * Adding some safety ... */
Russell King29279262009-01-08 12:02:27 +0000150 netx_clockevent.cpumask = cpumask_of(0);
Uwe Kleine-König5e3e2762013-11-12 20:56:02 +0100151 clockevents_config_and_register(&netx_clockevent, NETX_CLOCK_FREQ,
Shawn Guo838a2ae2013-01-12 11:50:05 +0000152 0xa00, 0xfffffffe);
Sascha Hauerbb6d8c82006-06-19 15:27:53 +0100153}