blob: dfc3bb410b00f23d644755410dbcf9452a8a99e6 [file] [log] [blame]
Alexey Charkov21f47fb2010-12-23 13:11:21 +01001/*
Tony Prisk83cc7692012-10-06 19:55:42 +13002 * arch/arm/mach-vt8500/timer.c
Alexey Charkov21f47fb2010-12-23 13:11:21 +01003 *
Tony Priske9a91de2012-08-03 21:00:06 +12004 * Copyright (C) 2012 Tony Prisk <linux@prisktech.co.nz>
Alexey Charkov21f47fb2010-12-23 13:11:21 +01005 * Copyright (C) 2010 Alexey Charkov <alchark@gmail.com>
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
Tony Priske9a91de2012-08-03 21:00:06 +120022/*
23 * This file is copied and modified from the original timer.c provided by
24 * Alexey Charkov. Minor changes have been made for Device Tree Support.
25 */
26
Alexey Charkov21f47fb2010-12-23 13:11:21 +010027#include <linux/io.h>
28#include <linux/irq.h>
29#include <linux/interrupt.h>
30#include <linux/clocksource.h>
31#include <linux/clockchips.h>
32#include <linux/delay.h>
Alexey Charkov21f47fb2010-12-23 13:11:21 +010033#include <asm/mach/time.h>
34
Tony Priske9a91de2012-08-03 21:00:06 +120035#include <linux/of.h>
36#include <linux/of_address.h>
37#include <linux/of_irq.h>
Alexey Charkov21f47fb2010-12-23 13:11:21 +010038
39#define VT8500_TIMER_OFFSET 0x0100
Tony Priske9a91de2012-08-03 21:00:06 +120040#define VT8500_TIMER_HZ 3000000
Alexey Charkov21f47fb2010-12-23 13:11:21 +010041#define TIMER_MATCH_VAL 0x0000
42#define TIMER_COUNT_VAL 0x0010
43#define TIMER_STATUS_VAL 0x0014
44#define TIMER_IER_VAL 0x001c /* interrupt enable */
45#define TIMER_CTRL_VAL 0x0020
46#define TIMER_AS_VAL 0x0024 /* access status */
47#define TIMER_COUNT_R_ACTIVE (1 << 5) /* not ready for read */
48#define TIMER_COUNT_W_ACTIVE (1 << 4) /* not ready for write */
49#define TIMER_MATCH_W_ACTIVE (1 << 0) /* not ready for write */
Alexey Charkov21f47fb2010-12-23 13:11:21 +010050
51#define msecs_to_loops(t) (loops_per_jiffy / 1000 * HZ * t)
52
Roman Volkovf9eccf22016-01-01 16:24:41 +030053#define MIN_OSCR_DELTA 16
54
Alexey Charkov21f47fb2010-12-23 13:11:21 +010055static void __iomem *regbase;
56
57static cycle_t vt8500_timer_read(struct clocksource *cs)
58{
59 int loops = msecs_to_loops(10);
60 writel(3, regbase + TIMER_CTRL_VAL);
61 while ((readl((regbase + TIMER_AS_VAL)) & TIMER_COUNT_R_ACTIVE)
62 && --loops)
63 cpu_relax();
64 return readl(regbase + TIMER_COUNT_VAL);
65}
66
Tony Priske9a91de2012-08-03 21:00:06 +120067static struct clocksource clocksource = {
Alexey Charkov21f47fb2010-12-23 13:11:21 +010068 .name = "vt8500_timer",
69 .rating = 200,
70 .read = vt8500_timer_read,
71 .mask = CLOCKSOURCE_MASK(32),
72 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
73};
74
75static int vt8500_timer_set_next_event(unsigned long cycles,
76 struct clock_event_device *evt)
77{
78 int loops = msecs_to_loops(10);
79 cycle_t alarm = clocksource.read(&clocksource) + cycles;
80 while ((readl(regbase + TIMER_AS_VAL) & TIMER_MATCH_W_ACTIVE)
81 && --loops)
82 cpu_relax();
83 writel((unsigned long)alarm, regbase + TIMER_MATCH_VAL);
84
Roman Volkovf9eccf22016-01-01 16:24:41 +030085 if ((signed)(alarm - clocksource.read(&clocksource)) <= MIN_OSCR_DELTA)
Alexey Charkov21f47fb2010-12-23 13:11:21 +010086 return -ETIME;
87
88 writel(1, regbase + TIMER_IER_VAL);
89
90 return 0;
91}
92
Viresh Kumar214bc752015-06-18 16:24:54 +053093static int vt8500_shutdown(struct clock_event_device *evt)
Alexey Charkov21f47fb2010-12-23 13:11:21 +010094{
Viresh Kumar214bc752015-06-18 16:24:54 +053095 writel(readl(regbase + TIMER_CTRL_VAL) | 1, regbase + TIMER_CTRL_VAL);
96 writel(0, regbase + TIMER_IER_VAL);
97 return 0;
Alexey Charkov21f47fb2010-12-23 13:11:21 +010098}
99
Tony Priske9a91de2012-08-03 21:00:06 +1200100static struct clock_event_device clockevent = {
Viresh Kumar214bc752015-06-18 16:24:54 +0530101 .name = "vt8500_timer",
102 .features = CLOCK_EVT_FEAT_ONESHOT,
103 .rating = 200,
104 .set_next_event = vt8500_timer_set_next_event,
105 .set_state_shutdown = vt8500_shutdown,
106 .set_state_oneshot = vt8500_shutdown,
Alexey Charkov21f47fb2010-12-23 13:11:21 +0100107};
108
109static irqreturn_t vt8500_timer_interrupt(int irq, void *dev_id)
110{
111 struct clock_event_device *evt = dev_id;
112 writel(0xf, regbase + TIMER_STATUS_VAL);
113 evt->event_handler(evt);
114
115 return IRQ_HANDLED;
116}
117
Tony Priske9a91de2012-08-03 21:00:06 +1200118static struct irqaction irq = {
Alexey Charkov21f47fb2010-12-23 13:11:21 +0100119 .name = "vt8500_timer",
Michael Opdenacker39039eb2013-12-09 10:38:50 +0100120 .flags = IRQF_TIMER | IRQF_IRQPOLL,
Alexey Charkov21f47fb2010-12-23 13:11:21 +0100121 .handler = vt8500_timer_interrupt,
122 .dev_id = &clockevent,
123};
124
Rob Herringeffbfdd2013-02-06 14:40:22 -0600125static void __init vt8500_timer_init(struct device_node *np)
Alexey Charkov21f47fb2010-12-23 13:11:21 +0100126{
Tony Priske9a91de2012-08-03 21:00:06 +1200127 int timer_irq;
128
Tony Priske9a91de2012-08-03 21:00:06 +1200129 regbase = of_iomap(np, 0);
130 if (!regbase) {
131 pr_err("%s: Missing iobase description in Device Tree\n",
132 __func__);
Tony Priske9a91de2012-08-03 21:00:06 +1200133 return;
134 }
135 timer_irq = irq_of_parse_and_map(np, 0);
136 if (!timer_irq) {
137 pr_err("%s: Missing irq description in Device Tree\n",
138 __func__);
Tony Priske9a91de2012-08-03 21:00:06 +1200139 return;
140 }
Alexey Charkov21f47fb2010-12-23 13:11:21 +0100141
142 writel(1, regbase + TIMER_CTRL_VAL);
143 writel(0xf, regbase + TIMER_STATUS_VAL);
144 writel(~0, regbase + TIMER_MATCH_VAL);
145
146 if (clocksource_register_hz(&clocksource, VT8500_TIMER_HZ))
Tony Priske9a91de2012-08-03 21:00:06 +1200147 pr_err("%s: vt8500_timer_init: clocksource_register failed for %s\n",
148 __func__, clocksource.name);
Alexey Charkov21f47fb2010-12-23 13:11:21 +0100149
Alexey Charkov21f47fb2010-12-23 13:11:21 +0100150 clockevent.cpumask = cpumask_of(0);
151
Tony Priske9a91de2012-08-03 21:00:06 +1200152 if (setup_irq(timer_irq, &irq))
153 pr_err("%s: setup_irq failed for %s\n", __func__,
154 clockevent.name);
Shawn Guo838a2ae2013-01-12 11:50:05 +0000155 clockevents_config_and_register(&clockevent, VT8500_TIMER_HZ,
Roman Volkovf9eccf22016-01-01 16:24:41 +0300156 MIN_OSCR_DELTA * 2, 0xf0000000);
Alexey Charkov21f47fb2010-12-23 13:11:21 +0100157}
158
Arnd Bergmann3d5a9652013-03-19 15:38:50 +0100159CLOCKSOURCE_OF_DECLARE(vt8500, "via,vt8500-timer", vt8500_timer_init);