1 | // SPDX-License-Identifier: GPL-2.0+ |
2 | /* |
3 | * Copyright 2012 Simon Arlott |
4 | */ |
5 | |
6 | #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.h> |
14 | #include <linux/of_address.h> |
15 | #include <linux/of_irq.h> |
16 | #include <linux/slab.h> |
17 | #include <linux/string.h> |
18 | #include <linux/sched_clock.h> |
19 | |
20 | #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 | |
29 | struct bcm2835_timer { |
30 | void __iomem *control; |
31 | void __iomem *compare; |
32 | int match_mask; |
33 | struct clock_event_device evt; |
34 | }; |
35 | |
36 | static void __iomem *system_clock __read_mostly; |
37 | |
38 | static u64 notrace bcm2835_sched_read(void) |
39 | { |
40 | return readl_relaxed(system_clock); |
41 | } |
42 | |
43 | static 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 | |
53 | static 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 | |
60 | event_handler = READ_ONCE(timer->evt.event_handler); |
61 | if (event_handler) |
62 | event_handler(&timer->evt); |
63 | return IRQ_HANDLED; |
64 | } else { |
65 | return IRQ_NONE; |
66 | } |
67 | } |
68 | |
69 | static int __init bcm2835_timer_init(struct device_node *node) |
70 | { |
71 | void __iomem *base; |
72 | u32 freq; |
73 | int irq, ret; |
74 | struct bcm2835_timer *timer; |
75 | |
76 | base = of_iomap(node, index: 0); |
77 | if (!base) { |
78 | pr_err("Can't remap registers\n" ); |
79 | return -ENXIO; |
80 | } |
81 | |
82 | ret = of_property_read_u32(np: node, propname: "clock-frequency" , out_value: &freq); |
83 | if (ret) { |
84 | pr_err("Can't read clock-frequency\n" ); |
85 | goto err_iounmap; |
86 | } |
87 | |
88 | system_clock = base + REG_COUNTER_LO; |
89 | sched_clock_register(read: bcm2835_sched_read, bits: 32, rate: freq); |
90 | |
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); |
95 | if (irq <= 0) { |
96 | pr_err("Can't parse IRQ\n" ); |
97 | ret = -EINVAL; |
98 | goto err_iounmap; |
99 | } |
100 | |
101 | timer = kzalloc(size: sizeof(*timer), GFP_KERNEL); |
102 | if (!timer) { |
103 | ret = -ENOMEM; |
104 | goto err_iounmap; |
105 | } |
106 | |
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; |
113 | timer->evt.set_next_event = bcm2835_time_set_next_event; |
114 | timer->evt.cpumask = cpumask_of(0); |
115 | |
116 | ret = request_irq(irq, handler: bcm2835_time_interrupt, IRQF_TIMER | IRQF_SHARED, |
117 | name: node->name, dev: timer); |
118 | if (ret) { |
119 | pr_err("Can't set up timer IRQ\n" ); |
120 | goto err_timer_free; |
121 | } |
122 | |
123 | clockevents_config_and_register(dev: &timer->evt, freq, min_delta: 0xf, max_delta: 0xffffffff); |
124 | |
125 | pr_info("bcm2835: system timer (irq = %d)\n" , irq); |
126 | |
127 | return 0; |
128 | |
129 | err_timer_free: |
130 | kfree(objp: timer); |
131 | |
132 | err_iounmap: |
133 | iounmap(addr: base); |
134 | return ret; |
135 | } |
136 | TIMER_OF_DECLARE(bcm2835, "brcm,bcm2835-system-timer" , |
137 | bcm2835_timer_init); |
138 | |