blob: 3f7fa8c01367377fe2aa857e0c8eb25c9b43c147 [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Alessandro Rubini28ad94e2009-07-02 19:06:47 +01002/*
Alessandro Rubini28ad94e2009-07-02 19:06:47 +01003 * Copyright (C) 2008 STMicroelectronics
Alessandro Rubinib102c012010-03-05 12:38:51 +01004 * Copyright (C) 2010 Alessandro Rubini
Linus Walleij8fbb97a22010-11-19 10:16:05 +01005 * Copyright (C) 2010 Linus Walleij for ST-Ericsson
Alessandro Rubini28ad94e2009-07-02 19:06:47 +01006 */
7#include <linux/init.h>
8#include <linux/interrupt.h>
9#include <linux/irq.h>
10#include <linux/io.h>
11#include <linux/clockchips.h>
Linus Walleij694e33a2012-10-18 14:01:25 +020012#include <linux/clocksource.h>
Rabin Vincentc7785ea2013-04-03 13:28:26 +020013#include <linux/of_address.h>
14#include <linux/of_irq.h>
15#include <linux/of_platform.h>
Linus Walleijba327b12010-05-26 07:38:54 +010016#include <linux/clk.h>
Alessandro Rubini28ad94e2009-07-02 19:06:47 +010017#include <linux/jiffies.h>
Fabio Baltieri6f179b72012-12-04 11:10:44 +010018#include <linux/delay.h>
Linus Walleijba327b12010-05-26 07:38:54 +010019#include <linux/err.h>
Stephen Boyd38ff87f2013-06-01 23:39:40 -070020#include <linux/sched_clock.h>
Alessandro Rubini28ad94e2009-07-02 19:06:47 +010021#include <asm/mach/time.h>
Alessandro Rubini28ad94e2009-07-02 19:06:47 +010022
Jonas Aaberg05387a92011-09-20 11:18:27 +020023/*
Jonas Aaberg05387a92011-09-20 11:18:27 +020024 * The MTU device hosts four different counters, with 4 set of
25 * registers. These are register names.
26 */
27
28#define MTU_IMSC 0x00 /* Interrupt mask set/clear */
29#define MTU_RIS 0x04 /* Raw interrupt status */
30#define MTU_MIS 0x08 /* Masked interrupt status */
31#define MTU_ICR 0x0C /* Interrupt clear register */
32
33/* per-timer registers take 0..3 as argument */
34#define MTU_LR(x) (0x10 + 0x10 * (x) + 0x00) /* Load value */
35#define MTU_VAL(x) (0x10 + 0x10 * (x) + 0x04) /* Current value */
36#define MTU_CR(x) (0x10 + 0x10 * (x) + 0x08) /* Control reg */
37#define MTU_BGLR(x) (0x10 + 0x10 * (x) + 0x0c) /* At next overflow */
38
39/* bits for the control register */
40#define MTU_CRn_ENA 0x80
41#define MTU_CRn_PERIODIC 0x40 /* if 0 = free-running */
42#define MTU_CRn_PRESCALE_MASK 0x0c
43#define MTU_CRn_PRESCALE_1 0x00
44#define MTU_CRn_PRESCALE_16 0x04
45#define MTU_CRn_PRESCALE_256 0x08
46#define MTU_CRn_32BITS 0x02
47#define MTU_CRn_ONESHOT 0x01 /* if 0 = wraps reloading from BGLR*/
48
49/* Other registers are usual amba/primecell registers, currently not used */
50#define MTU_ITCR 0xff0
51#define MTU_ITOP 0xff4
52
53#define MTU_PERIPH_ID0 0xfe0
54#define MTU_PERIPH_ID1 0xfe4
55#define MTU_PERIPH_ID2 0xfe8
56#define MTU_PERIPH_ID3 0xfeC
57
58#define MTU_PCELL0 0xff0
59#define MTU_PCELL1 0xff4
60#define MTU_PCELL2 0xff8
61#define MTU_PCELL3 0xffC
Alessandro Rubini28ad94e2009-07-02 19:06:47 +010062
Linus Walleijb9576622012-01-11 09:46:59 +010063static void __iomem *mtu_base;
Jonas Aaberg2f73a062011-09-14 10:32:51 +020064static bool clkevt_periodic;
65static u32 clk_prescale;
66static u32 nmdk_cycle; /* write-once */
Fabio Baltieri6f179b72012-12-04 11:10:44 +010067static struct delay_timer mtu_delay_timer;
Jonas Aaberg2f73a062011-09-14 10:32:51 +020068
Linus Walleij2a847512010-05-07 10:03:02 +010069/*
Linus Walleij2a847512010-05-07 10:03:02 +010070 * Override the global weak sched_clock symbol with this
71 * local implementation which uses the clocksource to get some
Linus Walleij8fbb97a22010-11-19 10:16:05 +010072 * better resolution when scheduling the kernel.
Linus Walleij2a847512010-05-07 10:03:02 +010073 */
Stephen Boyde25bc5f2013-07-18 16:21:24 -070074static u64 notrace nomadik_read_sched_clock(void)
Linus Walleij2a847512010-05-07 10:03:02 +010075{
Linus Walleij8fbb97a22010-11-19 10:16:05 +010076 if (unlikely(!mtu_base))
77 return 0;
78
Marc Zyngier2f0778af2011-12-15 12:19:23 +010079 return -readl(mtu_base + MTU_VAL(0));
Linus Walleij2a847512010-05-07 10:03:02 +010080}
Jonas Aaberg2f73a062011-09-14 10:32:51 +020081
Fabio Baltieri6f179b72012-12-04 11:10:44 +010082static unsigned long nmdk_timer_read_current_timer(void)
83{
84 return ~readl_relaxed(mtu_base + MTU_VAL(0));
85}
86
Alessandro Rubinib102c012010-03-05 12:38:51 +010087/* Clockevent device: use one-shot mode */
Jonas Aaberg2f73a062011-09-14 10:32:51 +020088static int nmdk_clkevt_next(unsigned long evt, struct clock_event_device *ev)
89{
90 writel(1 << 1, mtu_base + MTU_IMSC);
91 writel(evt, mtu_base + MTU_LR(1));
92 /* Load highest value, enable device, enable interrupts */
93 writel(MTU_CRn_ONESHOT | clk_prescale |
94 MTU_CRn_32BITS | MTU_CRn_ENA,
95 mtu_base + MTU_CR(1));
96
97 return 0;
98}
99
Linus Walleij7172c192013-11-19 22:23:21 +0100100static void nmdk_clkevt_reset(void)
Jonas Aaberg2f73a062011-09-14 10:32:51 +0200101{
102 if (clkevt_periodic) {
Jonas Aaberg2f73a062011-09-14 10:32:51 +0200103 /* Timer: configure load and background-load, and fire it up */
104 writel(nmdk_cycle, mtu_base + MTU_LR(1));
105 writel(nmdk_cycle, mtu_base + MTU_BGLR(1));
106
107 writel(MTU_CRn_PERIODIC | clk_prescale |
108 MTU_CRn_32BITS | MTU_CRn_ENA,
109 mtu_base + MTU_CR(1));
110 writel(1 << 1, mtu_base + MTU_IMSC);
111 } else {
112 /* Generate an interrupt to start the clockevent again */
113 (void) nmdk_clkevt_next(nmdk_cycle, NULL);
114 }
115}
116
Viresh Kumar9b0af692015-06-18 16:24:29 +0530117static int nmdk_clkevt_shutdown(struct clock_event_device *evt)
Alessandro Rubini28ad94e2009-07-02 19:06:47 +0100118{
Viresh Kumar9b0af692015-06-18 16:24:29 +0530119 writel(0, mtu_base + MTU_IMSC);
120 /* disable timer */
121 writel(0, mtu_base + MTU_CR(1));
122 /* load some high default value */
123 writel(0xffffffff, mtu_base + MTU_LR(1));
124 return 0;
125}
126
127static int nmdk_clkevt_set_oneshot(struct clock_event_device *evt)
128{
129 clkevt_periodic = false;
130 return 0;
131}
132
133static int nmdk_clkevt_set_periodic(struct clock_event_device *evt)
134{
135 clkevt_periodic = true;
136 nmdk_clkevt_reset();
137 return 0;
Alessandro Rubini28ad94e2009-07-02 19:06:47 +0100138}
139
Linus Walleij7172c192013-11-19 22:23:21 +0100140static void nmdk_clksrc_reset(void)
Stephen Warren8726e962012-11-07 17:07:45 -0700141{
142 /* Disable */
143 writel(0, mtu_base + MTU_CR(0));
144
145 /* ClockSource: configure load and background-load, and fire it up */
146 writel(nmdk_cycle, mtu_base + MTU_LR(0));
147 writel(nmdk_cycle, mtu_base + MTU_BGLR(0));
148
149 writel(clk_prescale | MTU_CRn_32BITS | MTU_CRn_ENA,
150 mtu_base + MTU_CR(0));
151}
152
153static void nmdk_clkevt_resume(struct clock_event_device *cedev)
154{
155 nmdk_clkevt_reset();
156 nmdk_clksrc_reset();
157}
158
Alessandro Rubini28ad94e2009-07-02 19:06:47 +0100159static struct clock_event_device nmdk_clkevt = {
Viresh Kumar9b0af692015-06-18 16:24:29 +0530160 .name = "mtu_1",
161 .features = CLOCK_EVT_FEAT_ONESHOT |
162 CLOCK_EVT_FEAT_PERIODIC |
163 CLOCK_EVT_FEAT_DYNIRQ,
164 .rating = 200,
165 .set_state_shutdown = nmdk_clkevt_shutdown,
166 .set_state_periodic = nmdk_clkevt_set_periodic,
167 .set_state_oneshot = nmdk_clkevt_set_oneshot,
168 .set_next_event = nmdk_clkevt_next,
169 .resume = nmdk_clkevt_resume,
Alessandro Rubini28ad94e2009-07-02 19:06:47 +0100170};
171
172/*
Alessandro Rubinib102c012010-03-05 12:38:51 +0100173 * IRQ Handler for timer 1 of the MTU block.
Alessandro Rubini28ad94e2009-07-02 19:06:47 +0100174 */
175static irqreturn_t nmdk_timer_interrupt(int irq, void *dev_id)
176{
Alessandro Rubinib102c012010-03-05 12:38:51 +0100177 struct clock_event_device *evdev = dev_id;
Alessandro Rubini28ad94e2009-07-02 19:06:47 +0100178
Alessandro Rubinib102c012010-03-05 12:38:51 +0100179 writel(1 << 1, mtu_base + MTU_ICR); /* Interrupt clear reg */
180 evdev->event_handler(evdev);
Alessandro Rubini28ad94e2009-07-02 19:06:47 +0100181 return IRQ_HANDLED;
182}
183
Alessandro Rubini28ad94e2009-07-02 19:06:47 +0100184static struct irqaction nmdk_timer_irq = {
185 .name = "Nomadik Timer Tick",
Michael Opdenacker38c30a82013-12-09 10:12:10 +0100186 .flags = IRQF_TIMER,
Alessandro Rubini28ad94e2009-07-02 19:06:47 +0100187 .handler = nmdk_timer_interrupt,
Alessandro Rubinib102c012010-03-05 12:38:51 +0100188 .dev_id = &nmdk_clkevt,
Alessandro Rubini28ad94e2009-07-02 19:06:47 +0100189};
190
Daniel Lezcanoe46105a2016-06-06 17:58:15 +0200191static int __init nmdk_timer_init(void __iomem *base, int irq,
Linus Walleij7172c192013-11-19 22:23:21 +0100192 struct clk *pclk, struct clk *clk)
Alessandro Rubini28ad94e2009-07-02 19:06:47 +0100193{
Alessandro Rubini28ad94e2009-07-02 19:06:47 +0100194 unsigned long rate;
Daniel Lezcanoe46105a2016-06-06 17:58:15 +0200195 int ret;
Linus Walleijba327b12010-05-26 07:38:54 +0100196
Linus Walleijb9576622012-01-11 09:46:59 +0100197 mtu_base = base;
Ulf Hansson16defa62012-10-24 14:13:41 +0200198
Rabin Vincentc7785ea2013-04-03 13:28:26 +0200199 BUG_ON(clk_prepare_enable(pclk));
200 BUG_ON(clk_prepare_enable(clk));
Alessandro Rubini28ad94e2009-07-02 19:06:47 +0100201
Alessandro Rubinib102c012010-03-05 12:38:51 +0100202 /*
Linus Walleija0719f52010-09-13 13:40:04 +0100203 * Tick rate is 2.4MHz for Nomadik and 2.4Mhz, 100MHz or 133 MHz
204 * for ux500.
205 * Use a divide-by-16 counter if the tick rate is more than 32MHz.
206 * At 32 MHz, the timer (with 32 bit counter) can be programmed
207 * to wake-up at a max 127s a head in time. Dividing a 2.4 MHz timer
208 * with 16 gives too low timer resolution.
Alessandro Rubinib102c012010-03-05 12:38:51 +0100209 */
Rabin Vincentc7785ea2013-04-03 13:28:26 +0200210 rate = clk_get_rate(clk);
Linus Walleija0719f52010-09-13 13:40:04 +0100211 if (rate > 32000000) {
Alessandro Rubinib102c012010-03-05 12:38:51 +0100212 rate /= 16;
Jonas Aaberg2f73a062011-09-14 10:32:51 +0200213 clk_prescale = MTU_CRn_PRESCALE_16;
Alessandro Rubinib102c012010-03-05 12:38:51 +0100214 } else {
Jonas Aaberg2f73a062011-09-14 10:32:51 +0200215 clk_prescale = MTU_CRn_PRESCALE_1;
Alessandro Rubinib102c012010-03-05 12:38:51 +0100216 }
Alessandro Rubini28ad94e2009-07-02 19:06:47 +0100217
Linus Walleij21366832012-10-18 11:12:31 +0200218 /* Cycles for periodic mode */
219 nmdk_cycle = DIV_ROUND_CLOSEST(rate, HZ);
Jonas Aaberg2f73a062011-09-14 10:32:51 +0200220
221
Alessandro Rubinib102c012010-03-05 12:38:51 +0100222 /* Timer 0 is the free running clocksource */
Jonas Aaberg2f73a062011-09-14 10:32:51 +0200223 nmdk_clksrc_reset();
Alessandro Rubini28ad94e2009-07-02 19:06:47 +0100224
Daniel Lezcanoe46105a2016-06-06 17:58:15 +0200225 ret = clocksource_mmio_init(mtu_base + MTU_VAL(0), "mtu_0",
226 rate, 200, 32, clocksource_mmio_readl_down);
227 if (ret) {
228 pr_err("timer: failed to initialize clock source %s\n", "mtu_0");
229 return ret;
230 }
Marc Zyngier2f0778af2011-12-15 12:19:23 +0100231
Stephen Boyde25bc5f2013-07-18 16:21:24 -0700232 sched_clock_register(nomadik_read_sched_clock, 32, rate);
Marc Zyngier2f0778af2011-12-15 12:19:23 +0100233
Linus Walleija3b86a62012-01-11 09:57:56 +0100234 /* Timer 1 is used for events, register irq and clockevents */
Linus Walleij08130692012-10-18 11:06:02 +0200235 setup_irq(irq, &nmdk_timer_irq);
Linus Walleija3b86a62012-01-11 09:57:56 +0100236 nmdk_clkevt.cpumask = cpumask_of(0);
Daniel Lezcano00f4e132013-02-22 16:44:30 +0100237 nmdk_clkevt.irq = irq;
Linus Walleija3b86a62012-01-11 09:57:56 +0100238 clockevents_config_and_register(&nmdk_clkevt, rate, 2, 0xffffffffU);
Fabio Baltieri6f179b72012-12-04 11:10:44 +0100239
240 mtu_delay_timer.read_current_timer = &nmdk_timer_read_current_timer;
241 mtu_delay_timer.freq = rate;
242 register_current_timer_delay(&mtu_delay_timer);
Daniel Lezcanoe46105a2016-06-06 17:58:15 +0200243
244 return 0;
Alessandro Rubini28ad94e2009-07-02 19:06:47 +0100245}
Rabin Vincentc7785ea2013-04-03 13:28:26 +0200246
Daniel Lezcanoe46105a2016-06-06 17:58:15 +0200247static int __init nmdk_timer_of_init(struct device_node *node)
Rabin Vincentc7785ea2013-04-03 13:28:26 +0200248{
Rabin Vincentc7785ea2013-04-03 13:28:26 +0200249 struct clk *pclk;
250 struct clk *clk;
251 void __iomem *base;
252 int irq;
253
Rabin Vincentc7785ea2013-04-03 13:28:26 +0200254 base = of_iomap(node, 0);
Daniel Lezcanoe46105a2016-06-06 17:58:15 +0200255 if (!base) {
Rafał Miłeckiac9ce6d2017-03-09 10:47:10 +0100256 pr_err("Can't remap registers\n");
Daniel Lezcanoe46105a2016-06-06 17:58:15 +0200257 return -ENXIO;
258 }
Rabin Vincentc7785ea2013-04-03 13:28:26 +0200259
260 pclk = of_clk_get_by_name(node, "apb_pclk");
Daniel Lezcanoe46105a2016-06-06 17:58:15 +0200261 if (IS_ERR(pclk)) {
Rafał Miłeckiac9ce6d2017-03-09 10:47:10 +0100262 pr_err("could not get apb_pclk\n");
Daniel Lezcanoe46105a2016-06-06 17:58:15 +0200263 return PTR_ERR(pclk);
264 }
Rabin Vincentc7785ea2013-04-03 13:28:26 +0200265
266 clk = of_clk_get_by_name(node, "timclk");
Daniel Lezcanoe46105a2016-06-06 17:58:15 +0200267 if (IS_ERR(clk)) {
Rafał Miłeckiac9ce6d2017-03-09 10:47:10 +0100268 pr_err("could not get timclk\n");
Daniel Lezcanoe46105a2016-06-06 17:58:15 +0200269 return PTR_ERR(clk);
270 }
Rabin Vincentc7785ea2013-04-03 13:28:26 +0200271
272 irq = irq_of_parse_and_map(node, 0);
Daniel Lezcanoe46105a2016-06-06 17:58:15 +0200273 if (irq <= 0) {
Rafał Miłeckiac9ce6d2017-03-09 10:47:10 +0100274 pr_err("Can't parse IRQ\n");
Daniel Lezcanoe46105a2016-06-06 17:58:15 +0200275 return -EINVAL;
276 }
Rabin Vincentc7785ea2013-04-03 13:28:26 +0200277
Daniel Lezcanoe46105a2016-06-06 17:58:15 +0200278 return nmdk_timer_init(base, irq, pclk, clk);
Rabin Vincentc7785ea2013-04-03 13:28:26 +0200279}
Daniel Lezcano17273392017-05-26 16:56:11 +0200280TIMER_OF_DECLARE(nomadik_mtu, "st,nomadik-mtu",
Rabin Vincentc7785ea2013-04-03 13:28:26 +0200281 nmdk_timer_of_init);