blob: 23c7638e2bb3a075cdcc6bdfd4c60c2a97c1e524 [file] [log] [blame]
Carlo Caionee4a6b372014-09-29 01:50:05 +02001/*
2 * Amlogic Meson6 SoCs timer handling.
3 *
4 * Copyright (C) 2014 Carlo Caione <carlo@caione.org>
5 *
6 * Based on code from Amlogic, Inc
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
Martin Blumenstinglbed8fc12018-11-15 23:46:56 +010013#include <linux/bitfield.h>
14#include <linux/bitops.h>
Carlo Caionee4a6b372014-09-29 01:50:05 +020015#include <linux/clk.h>
16#include <linux/clockchips.h>
17#include <linux/interrupt.h>
18#include <linux/irq.h>
19#include <linux/irqreturn.h>
20#include <linux/sched_clock.h>
21#include <linux/of.h>
22#include <linux/of_address.h>
23#include <linux/of_irq.h>
24
Martin Blumenstinglbed8fc12018-11-15 23:46:56 +010025#define MESON_ISA_TIMER_MUX 0x00
26#define MESON_ISA_TIMER_MUX_TIMERD_EN BIT(19)
27#define MESON_ISA_TIMER_MUX_TIMERC_EN BIT(18)
28#define MESON_ISA_TIMER_MUX_TIMERB_EN BIT(17)
29#define MESON_ISA_TIMER_MUX_TIMERA_EN BIT(16)
30#define MESON_ISA_TIMER_MUX_TIMERD_MODE BIT(15)
31#define MESON_ISA_TIMER_MUX_TIMERC_MODE BIT(14)
32#define MESON_ISA_TIMER_MUX_TIMERB_MODE BIT(13)
33#define MESON_ISA_TIMER_MUX_TIMERA_MODE BIT(12)
34#define MESON_ISA_TIMER_MUX_TIMERE_INPUT_CLOCK_MASK GENMASK(10, 8)
35#define MESON_ISA_TIMER_MUX_TIMERE_INPUT_CLOCK_SYSTEM_CLOCK 0x0
36#define MESON_ISA_TIMER_MUX_TIMERE_INPUT_CLOCK_1US 0x1
37#define MESON_ISA_TIMER_MUX_TIMERE_INPUT_CLOCK_10US 0x2
38#define MESON_ISA_TIMER_MUX_TIMERE_INPUT_CLOCK_100US 0x3
39#define MESON_ISA_TIMER_MUX_TIMERE_INPUT_CLOCK_1MS 0x4
40#define MESON_ISA_TIMER_MUX_TIMERD_INPUT_CLOCK_MASK GENMASK(7, 6)
41#define MESON_ISA_TIMER_MUX_TIMERC_INPUT_CLOCK_MASK GENMASK(5, 4)
42#define MESON_ISA_TIMER_MUX_TIMERB_INPUT_CLOCK_MASK GENMASK(3, 2)
43#define MESON_ISA_TIMER_MUX_TIMERA_INPUT_CLOCK_MASK GENMASK(1, 0)
44#define MESON_ISA_TIMER_MUX_TIMERABCD_INPUT_CLOCK_1US 0x0
45#define MESON_ISA_TIMER_MUX_TIMERABCD_INPUT_CLOCK_10US 0x1
46#define MESON_ISA_TIMER_MUX_TIMERABCD_INPUT_CLOCK_100US 0x0
47#define MESON_ISA_TIMER_MUX_TIMERABCD_INPUT_CLOCK_1MS 0x3
Carlo Caionee4a6b372014-09-29 01:50:05 +020048
Martin Blumenstinglbed8fc12018-11-15 23:46:56 +010049#define MESON_ISA_TIMERA 0x04
50#define MESON_ISA_TIMERB 0x08
51#define MESON_ISA_TIMERC 0x0c
52#define MESON_ISA_TIMERD 0x10
53#define MESON_ISA_TIMERE 0x14
Carlo Caionee4a6b372014-09-29 01:50:05 +020054
55static void __iomem *timer_base;
56
57static u64 notrace meson6_timer_sched_read(void)
58{
Martin Blumenstinglbed8fc12018-11-15 23:46:56 +010059 return (u64)readl(timer_base + MESON_ISA_TIMERE);
Carlo Caionee4a6b372014-09-29 01:50:05 +020060}
61
Martin Blumenstinglbed8fc12018-11-15 23:46:56 +010062static void meson6_clkevt_time_stop(void)
Carlo Caionee4a6b372014-09-29 01:50:05 +020063{
Martin Blumenstinglbed8fc12018-11-15 23:46:56 +010064 u32 val = readl(timer_base + MESON_ISA_TIMER_MUX);
Carlo Caionee4a6b372014-09-29 01:50:05 +020065
Martin Blumenstinglbed8fc12018-11-15 23:46:56 +010066 writel(val & ~MESON_ISA_TIMER_MUX_TIMERA_EN,
67 timer_base + MESON_ISA_TIMER_MUX);
Carlo Caionee4a6b372014-09-29 01:50:05 +020068}
69
Martin Blumenstinglbed8fc12018-11-15 23:46:56 +010070static void meson6_clkevt_time_setup(unsigned long delay)
Carlo Caionee4a6b372014-09-29 01:50:05 +020071{
Martin Blumenstinglbed8fc12018-11-15 23:46:56 +010072 writel(delay, timer_base + MESON_ISA_TIMERA);
Carlo Caionee4a6b372014-09-29 01:50:05 +020073}
74
Martin Blumenstinglbed8fc12018-11-15 23:46:56 +010075static void meson6_clkevt_time_start(bool periodic)
Carlo Caionee4a6b372014-09-29 01:50:05 +020076{
Martin Blumenstinglbed8fc12018-11-15 23:46:56 +010077 u32 val = readl(timer_base + MESON_ISA_TIMER_MUX);
Carlo Caionee4a6b372014-09-29 01:50:05 +020078
79 if (periodic)
Martin Blumenstinglbed8fc12018-11-15 23:46:56 +010080 val |= MESON_ISA_TIMER_MUX_TIMERA_MODE;
Carlo Caionee4a6b372014-09-29 01:50:05 +020081 else
Martin Blumenstinglbed8fc12018-11-15 23:46:56 +010082 val &= ~MESON_ISA_TIMER_MUX_TIMERA_MODE;
Carlo Caionee4a6b372014-09-29 01:50:05 +020083
Martin Blumenstinglbed8fc12018-11-15 23:46:56 +010084 writel(val | MESON_ISA_TIMER_MUX_TIMERA_EN,
85 timer_base + MESON_ISA_TIMER_MUX);
Carlo Caionee4a6b372014-09-29 01:50:05 +020086}
87
Viresh Kumar40117bd2015-06-18 16:24:23 +053088static int meson6_shutdown(struct clock_event_device *evt)
Carlo Caionee4a6b372014-09-29 01:50:05 +020089{
Martin Blumenstinglbed8fc12018-11-15 23:46:56 +010090 meson6_clkevt_time_stop();
Viresh Kumar40117bd2015-06-18 16:24:23 +053091 return 0;
92}
93
94static int meson6_set_oneshot(struct clock_event_device *evt)
95{
Martin Blumenstinglbed8fc12018-11-15 23:46:56 +010096 meson6_clkevt_time_stop();
97 meson6_clkevt_time_start(false);
Viresh Kumar40117bd2015-06-18 16:24:23 +053098 return 0;
99}
100
101static int meson6_set_periodic(struct clock_event_device *evt)
102{
Martin Blumenstinglbed8fc12018-11-15 23:46:56 +0100103 meson6_clkevt_time_stop();
104 meson6_clkevt_time_setup(USEC_PER_SEC / HZ - 1);
105 meson6_clkevt_time_start(true);
Viresh Kumar40117bd2015-06-18 16:24:23 +0530106 return 0;
Carlo Caionee4a6b372014-09-29 01:50:05 +0200107}
108
109static int meson6_clkevt_next_event(unsigned long evt,
110 struct clock_event_device *unused)
111{
Martin Blumenstinglbed8fc12018-11-15 23:46:56 +0100112 meson6_clkevt_time_stop();
113 meson6_clkevt_time_setup(evt);
114 meson6_clkevt_time_start(false);
Carlo Caionee4a6b372014-09-29 01:50:05 +0200115
116 return 0;
117}
118
119static struct clock_event_device meson6_clockevent = {
Viresh Kumar40117bd2015-06-18 16:24:23 +0530120 .name = "meson6_tick",
121 .rating = 400,
122 .features = CLOCK_EVT_FEAT_PERIODIC |
123 CLOCK_EVT_FEAT_ONESHOT,
124 .set_state_shutdown = meson6_shutdown,
125 .set_state_periodic = meson6_set_periodic,
126 .set_state_oneshot = meson6_set_oneshot,
127 .tick_resume = meson6_shutdown,
128 .set_next_event = meson6_clkevt_next_event,
Carlo Caionee4a6b372014-09-29 01:50:05 +0200129};
130
131static irqreturn_t meson6_timer_interrupt(int irq, void *dev_id)
132{
133 struct clock_event_device *evt = (struct clock_event_device *)dev_id;
134
135 evt->event_handler(evt);
136
137 return IRQ_HANDLED;
138}
139
140static struct irqaction meson6_timer_irq = {
141 .name = "meson6_timer",
142 .flags = IRQF_TIMER | IRQF_IRQPOLL,
143 .handler = meson6_timer_interrupt,
144 .dev_id = &meson6_clockevent,
145};
146
Daniel Lezcanoca46acb2016-06-06 17:57:07 +0200147static int __init meson6_timer_init(struct device_node *node)
Carlo Caionee4a6b372014-09-29 01:50:05 +0200148{
149 u32 val;
150 int ret, irq;
151
152 timer_base = of_io_request_and_map(node, 0, "meson6-timer");
Daniel Lezcanoca46acb2016-06-06 17:57:07 +0200153 if (IS_ERR(timer_base)) {
Rafał Miłeckiac9ce6d2017-03-09 10:47:10 +0100154 pr_err("Can't map registers\n");
Daniel Lezcanoca46acb2016-06-06 17:57:07 +0200155 return -ENXIO;
156 }
Carlo Caionee4a6b372014-09-29 01:50:05 +0200157
158 irq = irq_of_parse_and_map(node, 0);
Daniel Lezcanoca46acb2016-06-06 17:57:07 +0200159 if (irq <= 0) {
Rafał Miłeckiac9ce6d2017-03-09 10:47:10 +0100160 pr_err("Can't parse IRQ\n");
Daniel Lezcanoca46acb2016-06-06 17:57:07 +0200161 return -EINVAL;
162 }
Carlo Caionee4a6b372014-09-29 01:50:05 +0200163
164 /* Set 1us for timer E */
Martin Blumenstinglbed8fc12018-11-15 23:46:56 +0100165 val = readl(timer_base + MESON_ISA_TIMER_MUX);
166 val &= ~MESON_ISA_TIMER_MUX_TIMERE_INPUT_CLOCK_MASK;
167 val |= FIELD_PREP(MESON_ISA_TIMER_MUX_TIMERE_INPUT_CLOCK_MASK,
168 MESON_ISA_TIMER_MUX_TIMERE_INPUT_CLOCK_1US);
169 writel(val, timer_base + MESON_ISA_TIMER_MUX);
Carlo Caionee4a6b372014-09-29 01:50:05 +0200170
171 sched_clock_register(meson6_timer_sched_read, 32, USEC_PER_SEC);
Martin Blumenstinglbed8fc12018-11-15 23:46:56 +0100172 clocksource_mmio_init(timer_base + MESON_ISA_TIMERE, node->name,
Carlo Caionee4a6b372014-09-29 01:50:05 +0200173 1000 * 1000, 300, 32, clocksource_mmio_readl_up);
174
175 /* Timer A base 1us */
Martin Blumenstinglbed8fc12018-11-15 23:46:56 +0100176 val &= ~MESON_ISA_TIMER_MUX_TIMERA_INPUT_CLOCK_MASK;
177 val |= FIELD_PREP(MESON_ISA_TIMER_MUX_TIMERA_INPUT_CLOCK_MASK,
178 MESON_ISA_TIMER_MUX_TIMERABCD_INPUT_CLOCK_1US);
179 writel(val, timer_base + MESON_ISA_TIMER_MUX);
Carlo Caionee4a6b372014-09-29 01:50:05 +0200180
181 /* Stop the timer A */
Martin Blumenstinglbed8fc12018-11-15 23:46:56 +0100182 meson6_clkevt_time_stop();
Carlo Caionee4a6b372014-09-29 01:50:05 +0200183
184 ret = setup_irq(irq, &meson6_timer_irq);
Daniel Lezcanoca46acb2016-06-06 17:57:07 +0200185 if (ret) {
Carlo Caionee4a6b372014-09-29 01:50:05 +0200186 pr_warn("failed to setup irq %d\n", irq);
Daniel Lezcanoca46acb2016-06-06 17:57:07 +0200187 return ret;
188 }
Carlo Caionee4a6b372014-09-29 01:50:05 +0200189
190 meson6_clockevent.cpumask = cpu_possible_mask;
191 meson6_clockevent.irq = irq;
192
193 clockevents_config_and_register(&meson6_clockevent, USEC_PER_SEC,
194 1, 0xfffe);
Daniel Lezcanoca46acb2016-06-06 17:57:07 +0200195 return 0;
Carlo Caionee4a6b372014-09-29 01:50:05 +0200196}
Daniel Lezcano17273392017-05-26 16:56:11 +0200197TIMER_OF_DECLARE(meson6, "amlogic,meson6-timer",
Carlo Caionee4a6b372014-09-29 01:50:05 +0200198 meson6_timer_init);