blob: 1592650b2c92e0d84988c2ed45658140911c15ff [file] [log] [blame]
Stefan Wahren64db8bb2018-11-10 16:28:39 +01001// SPDX-License-Identifier: GPL-2.0+
Simon Arlottee4af562012-09-10 22:38:35 -06002/*
3 * Copyright 2012 Simon Arlott
Simon Arlottee4af562012-09-10 22:38:35 -06004 */
5
Simon Arlottee4af562012-09-10 22:38:35 -06006#include <linux/bitops.h>
7#include <linux/clockchips.h>
8#include <linux/clocksource.h>
9#include <linux/interrupt.h>
10#include <linux/irqreturn.h>
11#include <linux/kernel.h>
12#include <linux/module.h>
13#include <linux/of_address.h>
14#include <linux/of_irq.h>
15#include <linux/of_platform.h>
16#include <linux/slab.h>
17#include <linux/string.h>
Stephen Boyd38ff87f2013-06-01 23:39:40 -070018#include <linux/sched_clock.h>
Simon Arlottee4af562012-09-10 22:38:35 -060019
Simon Arlottee4af562012-09-10 22:38:35 -060020#include <asm/irq.h>
21
22#define REG_CONTROL 0x00
23#define REG_COUNTER_LO 0x04
24#define REG_COUNTER_HI 0x08
25#define REG_COMPARE(n) (0x0c + (n) * 4)
26#define MAX_TIMER 3
27#define DEFAULT_TIMER 3
28
29struct bcm2835_timer {
30 void __iomem *control;
31 void __iomem *compare;
32 int match_mask;
33 struct clock_event_device evt;
Simon Arlottee4af562012-09-10 22:38:35 -060034};
35
36static void __iomem *system_clock __read_mostly;
37
Stephen Boyd18952f22013-07-18 16:21:20 -070038static u64 notrace bcm2835_sched_read(void)
Simon Arlottee4af562012-09-10 22:38:35 -060039{
40 return readl_relaxed(system_clock);
41}
42
Simon Arlottee4af562012-09-10 22:38:35 -060043static int bcm2835_time_set_next_event(unsigned long event,
44 struct clock_event_device *evt_dev)
45{
46 struct bcm2835_timer *timer = container_of(evt_dev,
47 struct bcm2835_timer, evt);
48 writel_relaxed(readl_relaxed(system_clock) + event,
49 timer->compare);
50 return 0;
51}
52
53static irqreturn_t bcm2835_time_interrupt(int irq, void *dev_id)
54{
55 struct bcm2835_timer *timer = dev_id;
56 void (*event_handler)(struct clock_event_device *);
57 if (readl_relaxed(timer->control) & timer->match_mask) {
58 writel_relaxed(timer->match_mask, timer->control);
59
Mark Rutland6aa7de02017-10-23 14:07:29 -070060 event_handler = READ_ONCE(timer->evt.event_handler);
Simon Arlottee4af562012-09-10 22:38:35 -060061 if (event_handler)
62 event_handler(&timer->evt);
63 return IRQ_HANDLED;
64 } else {
65 return IRQ_NONE;
66 }
67}
68
Daniel Lezcano524a7f02016-06-02 18:46:11 +020069static int __init bcm2835_timer_init(struct device_node *node)
Simon Arlottee4af562012-09-10 22:38:35 -060070{
Simon Arlottee4af562012-09-10 22:38:35 -060071 void __iomem *base;
72 u32 freq;
Daniel Lezcano524a7f02016-06-02 18:46:11 +020073 int irq, ret;
Simon Arlottee4af562012-09-10 22:38:35 -060074 struct bcm2835_timer *timer;
75
Simon Arlottee4af562012-09-10 22:38:35 -060076 base = of_iomap(node, 0);
Daniel Lezcano524a7f02016-06-02 18:46:11 +020077 if (!base) {
Rafał Miłeckiac9ce6d2017-03-09 10:47:10 +010078 pr_err("Can't remap registers\n");
Daniel Lezcano524a7f02016-06-02 18:46:11 +020079 return -ENXIO;
80 }
Simon Arlottee4af562012-09-10 22:38:35 -060081
Daniel Lezcano524a7f02016-06-02 18:46:11 +020082 ret = of_property_read_u32(node, "clock-frequency", &freq);
83 if (ret) {
Rafał Miłeckiac9ce6d2017-03-09 10:47:10 +010084 pr_err("Can't read clock-frequency\n");
Arvind Yadav84c39b82016-09-21 23:03:57 +053085 goto err_iounmap;
Daniel Lezcano524a7f02016-06-02 18:46:11 +020086 }
Simon Arlottee4af562012-09-10 22:38:35 -060087
88 system_clock = base + REG_COUNTER_LO;
Stephen Boyd18952f22013-07-18 16:21:20 -070089 sched_clock_register(bcm2835_sched_read, 32, freq);
Simon Arlottee4af562012-09-10 22:38:35 -060090
91 clocksource_mmio_init(base + REG_COUNTER_LO, node->name,
92 freq, 300, 32, clocksource_mmio_readl_up);
93
94 irq = irq_of_parse_and_map(node, DEFAULT_TIMER);
Daniel Lezcano524a7f02016-06-02 18:46:11 +020095 if (irq <= 0) {
Rafał Miłeckiac9ce6d2017-03-09 10:47:10 +010096 pr_err("Can't parse IRQ\n");
Arvind Yadav84c39b82016-09-21 23:03:57 +053097 ret = -EINVAL;
98 goto err_iounmap;
Daniel Lezcano524a7f02016-06-02 18:46:11 +020099 }
Simon Arlottee4af562012-09-10 22:38:35 -0600100
101 timer = kzalloc(sizeof(*timer), GFP_KERNEL);
Daniel Lezcano524a7f02016-06-02 18:46:11 +0200102 if (!timer) {
Arvind Yadav84c39b82016-09-21 23:03:57 +0530103 ret = -ENOMEM;
104 goto err_iounmap;
Daniel Lezcano524a7f02016-06-02 18:46:11 +0200105 }
Simon Arlottee4af562012-09-10 22:38:35 -0600106
107 timer->control = base + REG_CONTROL;
108 timer->compare = base + REG_COMPARE(DEFAULT_TIMER);
109 timer->match_mask = BIT(DEFAULT_TIMER);
110 timer->evt.name = node->name;
111 timer->evt.rating = 300;
112 timer->evt.features = CLOCK_EVT_FEAT_ONESHOT;
Simon Arlottee4af562012-09-10 22:38:35 -0600113 timer->evt.set_next_event = bcm2835_time_set_next_event;
114 timer->evt.cpumask = cpumask_of(0);
Simon Arlottee4af562012-09-10 22:38:35 -0600115
afzal mohammedcc2550b2020-02-27 16:29:02 +0530116 ret = request_irq(irq, bcm2835_time_interrupt, IRQF_TIMER | IRQF_SHARED,
117 node->name, timer);
Daniel Lezcano524a7f02016-06-02 18:46:11 +0200118 if (ret) {
119 pr_err("Can't set up timer IRQ\n");
Colin Ian King2052d0322019-12-19 21:32:46 +0000120 goto err_timer_free;
Daniel Lezcano524a7f02016-06-02 18:46:11 +0200121 }
Simon Arlottee4af562012-09-10 22:38:35 -0600122
123 clockevents_config_and_register(&timer->evt, freq, 0xf, 0xffffffff);
124
125 pr_info("bcm2835: system timer (irq = %d)\n", irq);
Daniel Lezcano524a7f02016-06-02 18:46:11 +0200126
127 return 0;
Arvind Yadav84c39b82016-09-21 23:03:57 +0530128
Colin Ian King2052d0322019-12-19 21:32:46 +0000129err_timer_free:
130 kfree(timer);
131
Arvind Yadav84c39b82016-09-21 23:03:57 +0530132err_iounmap:
133 iounmap(base);
134 return ret;
Simon Arlottee4af562012-09-10 22:38:35 -0600135}
Daniel Lezcano17273392017-05-26 16:56:11 +0200136TIMER_OF_DECLARE(bcm2835, "brcm,bcm2835-system-timer",
Stephen Warrenc1b724f2013-01-03 20:23:13 -0700137 bcm2835_timer_init);