blob: a3aaa5658a4927b1cf627f4fc8e79b5c2b21f7b2 [file] [log] [blame]
Jonas Jensen07862c12013-07-17 10:04:57 +02001/*
2 * MOXA ART SoCs timer handling.
3 *
4 * Copyright (C) 2013 Jonas Jensen
5 *
6 * Jonas Jensen <jonas.jensen@gmail.com>
7 *
8 * This file is licensed under the terms of the GNU General Public
9 * License version 2. This program is licensed "as is" without any
10 * warranty of any kind, whether express or implied.
11 */
12
13#include <linux/clk.h>
14#include <linux/clockchips.h>
15#include <linux/interrupt.h>
16#include <linux/irq.h>
17#include <linux/irqreturn.h>
18#include <linux/of.h>
19#include <linux/of_address.h>
20#include <linux/of_irq.h>
21#include <linux/io.h>
22#include <linux/clocksource.h>
Jonas Jensenadf157e2013-07-26 16:03:38 +020023#include <linux/bitops.h>
Jonas Jensen07862c12013-07-17 10:04:57 +020024
25#define TIMER1_BASE 0x00
26#define TIMER2_BASE 0x10
27#define TIMER3_BASE 0x20
28
29#define REG_COUNT 0x0 /* writable */
30#define REG_LOAD 0x4
31#define REG_MATCH1 0x8
32#define REG_MATCH2 0xC
33
34#define TIMER_CR 0x30
35#define TIMER_INTR_STATE 0x34
36#define TIMER_INTR_MASK 0x38
37
38/*
39 * TIMER_CR flags:
40 *
41 * TIMEREG_CR_*_CLOCK 0: PCLK, 1: EXT1CLK
42 * TIMEREG_CR_*_INT overflow interrupt enable bit
43 */
44#define TIMEREG_CR_1_ENABLE BIT(0)
45#define TIMEREG_CR_1_CLOCK BIT(1)
46#define TIMEREG_CR_1_INT BIT(2)
47#define TIMEREG_CR_2_ENABLE BIT(3)
48#define TIMEREG_CR_2_CLOCK BIT(4)
49#define TIMEREG_CR_2_INT BIT(5)
50#define TIMEREG_CR_3_ENABLE BIT(6)
51#define TIMEREG_CR_3_CLOCK BIT(7)
52#define TIMEREG_CR_3_INT BIT(8)
53#define TIMEREG_CR_COUNT_UP BIT(9)
54
55#define TIMER1_ENABLE (TIMEREG_CR_2_ENABLE | TIMEREG_CR_1_ENABLE)
56#define TIMER1_DISABLE (TIMEREG_CR_2_ENABLE)
57
58static void __iomem *base;
59static unsigned int clock_count_per_tick;
60
Joel Stanley70164742016-07-21 23:13:51 +093061static inline void moxart_disable(struct clock_event_device *evt)
Jonas Jensen07862c12013-07-17 10:04:57 +020062{
Viresh Kumar37ae2472015-06-18 16:24:26 +053063 writel(TIMER1_DISABLE, base + TIMER_CR);
Joel Stanley70164742016-07-21 23:13:51 +093064}
65
66static inline void moxart_enable(struct clock_event_device *evt)
67{
68 writel(TIMER1_ENABLE, base + TIMER_CR);
69}
70
71static int moxart_shutdown(struct clock_event_device *evt)
72{
73 moxart_disable(evt);
Viresh Kumar37ae2472015-06-18 16:24:26 +053074 return 0;
75}
76
77static int moxart_set_oneshot(struct clock_event_device *evt)
78{
Joel Stanley70164742016-07-21 23:13:51 +093079 moxart_disable(evt);
Viresh Kumar37ae2472015-06-18 16:24:26 +053080 writel(~0, base + TIMER1_BASE + REG_LOAD);
81 return 0;
82}
83
84static int moxart_set_periodic(struct clock_event_device *evt)
85{
86 writel(clock_count_per_tick, base + TIMER1_BASE + REG_LOAD);
Joel Stanley70164742016-07-21 23:13:51 +093087 moxart_enable(evt);
Viresh Kumar37ae2472015-06-18 16:24:26 +053088 return 0;
Jonas Jensen07862c12013-07-17 10:04:57 +020089}
90
91static int moxart_clkevt_next_event(unsigned long cycles,
Joel Stanley70164742016-07-21 23:13:51 +093092 struct clock_event_device *evt)
Jonas Jensen07862c12013-07-17 10:04:57 +020093{
94 u32 u;
95
Joel Stanley70164742016-07-21 23:13:51 +093096 moxart_disable(evt);
Jonas Jensen07862c12013-07-17 10:04:57 +020097
98 u = readl(base + TIMER1_BASE + REG_COUNT) - cycles;
99 writel(u, base + TIMER1_BASE + REG_MATCH1);
100
Joel Stanley70164742016-07-21 23:13:51 +0930101 moxart_enable(evt);
Jonas Jensen07862c12013-07-17 10:04:57 +0200102
103 return 0;
104}
105
106static struct clock_event_device moxart_clockevent = {
Viresh Kumar37ae2472015-06-18 16:24:26 +0530107 .name = "moxart_timer",
108 .rating = 200,
109 .features = CLOCK_EVT_FEAT_PERIODIC |
110 CLOCK_EVT_FEAT_ONESHOT,
111 .set_state_shutdown = moxart_shutdown,
112 .set_state_periodic = moxart_set_periodic,
113 .set_state_oneshot = moxart_set_oneshot,
114 .tick_resume = moxart_set_oneshot,
115 .set_next_event = moxart_clkevt_next_event,
Jonas Jensen07862c12013-07-17 10:04:57 +0200116};
117
118static irqreturn_t moxart_timer_interrupt(int irq, void *dev_id)
119{
120 struct clock_event_device *evt = dev_id;
121 evt->event_handler(evt);
122 return IRQ_HANDLED;
123}
124
125static struct irqaction moxart_timer_irq = {
126 .name = "moxart-timer",
127 .flags = IRQF_TIMER,
128 .handler = moxart_timer_interrupt,
129 .dev_id = &moxart_clockevent,
130};
131
Daniel Lezcanob7357e62016-06-06 17:57:38 +0200132static int __init moxart_timer_init(struct device_node *node)
Jonas Jensen07862c12013-07-17 10:04:57 +0200133{
134 int ret, irq;
135 unsigned long pclk;
136 struct clk *clk;
137
138 base = of_iomap(node, 0);
Daniel Lezcanob7357e62016-06-06 17:57:38 +0200139 if (!base) {
140 pr_err("%s: of_iomap failed\n", node->full_name);
141 return -ENXIO;
142 }
Jonas Jensen07862c12013-07-17 10:04:57 +0200143
144 irq = irq_of_parse_and_map(node, 0);
Daniel Lezcanob7357e62016-06-06 17:57:38 +0200145 if (irq <= 0) {
146 pr_err("%s: irq_of_parse_and_map failed\n", node->full_name);
147 return -EINVAL;
148 }
Jonas Jensen07862c12013-07-17 10:04:57 +0200149
150 ret = setup_irq(irq, &moxart_timer_irq);
Daniel Lezcanob7357e62016-06-06 17:57:38 +0200151 if (ret) {
152 pr_err("%s: setup_irq failed\n", node->full_name);
153 return ret;
154 }
Jonas Jensen07862c12013-07-17 10:04:57 +0200155
156 clk = of_clk_get(node, 0);
Daniel Lezcanob7357e62016-06-06 17:57:38 +0200157 if (IS_ERR(clk)) {
158 pr_err("%s: of_clk_get failed\n", node->full_name);
159 return PTR_ERR(clk);
160 }
Jonas Jensen07862c12013-07-17 10:04:57 +0200161
162 pclk = clk_get_rate(clk);
163
Daniel Lezcanob7357e62016-06-06 17:57:38 +0200164 ret = clocksource_mmio_init(base + TIMER2_BASE + REG_COUNT,
165 "moxart_timer", pclk, 200, 32,
166 clocksource_mmio_readl_down);
167 if (ret) {
168 pr_err("%s: clocksource_mmio_init failed\n", node->full_name);
169 return ret;
170 }
Jonas Jensen07862c12013-07-17 10:04:57 +0200171
172 clock_count_per_tick = DIV_ROUND_CLOSEST(pclk, HZ);
173
174 writel(~0, base + TIMER2_BASE + REG_LOAD);
175 writel(TIMEREG_CR_2_ENABLE, base + TIMER_CR);
176
177 moxart_clockevent.cpumask = cpumask_of(0);
178 moxart_clockevent.irq = irq;
179
180 /*
181 * documentation is not publicly available:
182 * min_delta / max_delta obtained by trial-and-error,
183 * max_delta 0xfffffffe should be ok because count
184 * register size is u32
185 */
186 clockevents_config_and_register(&moxart_clockevent, pclk,
187 0x4, 0xfffffffe);
Daniel Lezcanob7357e62016-06-06 17:57:38 +0200188
189 return 0;
Jonas Jensen07862c12013-07-17 10:04:57 +0200190}
Daniel Lezcano177cf6e2016-06-07 00:27:44 +0200191CLOCKSOURCE_OF_DECLARE(moxart, "moxa,moxart-timer", moxart_timer_init);