blob: 9ab1733fe38c21ef28115623df7b359d55a8a5fd [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001// SPDX-License-Identifier: GPL-2.0-only
2
3#include <linux/clockchips.h>
4#include <linux/init.h>
5#include <asm/time.h>
6#include <linux/interrupt.h>
7#include <linux/of_address.h>
8#include <linux/of_irq.h>
9#include <linux/sched_clock.h>
10#include "timer-of.h"
11
12#include <mach-rtl83xx.h>
13
14/*
15 * Timer registers
16 * the RTL9300/9310 SoCs have 6 timers, each register block 0x10 apart
17 */
18#define RTL9300_TC_DATA 0x0
19#define RTL9300_TC_CNT 0x4
20#define RTL9300_TC_CTRL 0x8
21#define RTL9300_TC_CTRL_MODE BIT(24)
22#define RTL9300_TC_CTRL_EN BIT(28)
23#define RTL9300_TC_INT 0xc
24#define RTL9300_TC_INT_IP BIT(16)
25#define RTL9300_TC_INT_IE BIT(20)
26
27// Clocksource is using timer 0, clock event uses timer 1
28#define TIMER_CLK_SRC 0
29#define TIMER_CLK_EVT 1
30#define TIMER_BLK_EVT (TIMER_CLK_EVT << 4)
31
32// Timer modes
33#define TIMER_MODE_REPEAT 1
34#define TIMER_MODE_ONCE 0
35
36// Minimum divider is 2
37#define DIVISOR_RTL9300 2
38
39#define N_BITS 28
40
41static void __iomem *rtl9300_sched_reg __read_mostly;
42
43static u64 notrace rtl9300_sched_clock_read(void)
44{
45/* pr_info("In %s: %x\n", __func__, readl_relaxed(rtl9300_sched_reg));
46 dump_stack();*/
47 return readl_relaxed(rtl9300_sched_reg);
48}
49
50static irqreturn_t rtl9300_timer_interrupt(int irq, void *dev_id)
51{
52 struct clock_event_device *clk = dev_id;
53 struct timer_of *to = to_timer_of(clk);
54 u32 v = readl(timer_of_base(to) + TIMER_BLK_EVT + RTL9300_TC_INT);
55
56 // Acknowledge the IRQ
57 v |= RTL9300_TC_INT_IP;
58 writel(v, timer_of_base(to) + TIMER_BLK_EVT + RTL9300_TC_INT);
59
60 clk->event_handler(clk);
61 return IRQ_HANDLED;
62}
63
64static void rtl9300_timer_stop(struct timer_of *to)
65{
66 u32 v;
67
68 writel(0, timer_of_base(to) + TIMER_BLK_EVT + RTL9300_TC_CTRL);
69
70 // Acknowledge possibly pending IRQ
71 v = readl(timer_of_base(to) + TIMER_BLK_EVT + RTL9300_TC_INT);
72 if (v & RTL9300_TC_INT_IP)
73 writel(v, timer_of_base(to) + TIMER_BLK_EVT + RTL9300_TC_INT);
74}
75
76static void rtl9300_timer_start(struct timer_of *to, int timer, bool periodic)
77{
78 u32 v = (periodic ? RTL9300_TC_CTRL_MODE : 0) | RTL9300_TC_CTRL_EN | DIVISOR_RTL9300;
79 writel(v, timer_of_base(to) + timer * 0x10 + RTL9300_TC_CTRL);
80}
81
82static int rtl9300_set_next_event(unsigned long delta, struct clock_event_device *clk)
83{
84 struct timer_of *to = to_timer_of(clk);
85
86 rtl9300_timer_stop(to);
87 writel(delta, timer_of_base(to) + TIMER_BLK_EVT + RTL9300_TC_DATA);
88 rtl9300_timer_start(to, TIMER_CLK_EVT, TIMER_MODE_ONCE);
89 return 0;
90}
91
92static int rtl9300_set_state_periodic(struct clock_event_device *clk)
93{
94 struct timer_of *to = to_timer_of(clk);
95
96 rtl9300_timer_stop(to);
97 writel(to->of_clk.period, timer_of_base(to) + TIMER_BLK_EVT + RTL9300_TC_DATA);
98 rtl9300_timer_start(to, TIMER_CLK_EVT, TIMER_MODE_REPEAT);
99 return 0;
100}
101
102static int rtl9300_set_state_oneshot(struct clock_event_device *clk)
103{
104 struct timer_of *to = to_timer_of(clk);
105
106 rtl9300_timer_stop(to);
107 writel(to->of_clk.period, timer_of_base(to) + TIMER_BLK_EVT + RTL9300_TC_DATA);
108 rtl9300_timer_start(to, TIMER_CLK_EVT, TIMER_MODE_ONCE);
109 return 0;
110}
111
112static int rtl9300_set_state_shutdown(struct clock_event_device *clk)
113{
114 struct timer_of *to = to_timer_of(clk);
115
116 rtl9300_timer_stop(to);
117 return 0;
118}
119
120static struct timer_of t_of = {
121 .flags = TIMER_OF_BASE | TIMER_OF_IRQ | TIMER_OF_CLOCK,
122
123 .clkevt = {
124 .name = "rtl9300_timer",
125 .rating = 350,
126 .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT,
127 .set_next_event = rtl9300_set_next_event,
128 .set_state_oneshot = rtl9300_set_state_oneshot,
129 .set_state_periodic = rtl9300_set_state_periodic,
130 .set_state_shutdown = rtl9300_set_state_shutdown,
131 },
132
133 .of_irq = {
134 .name = "ostimer",
135 .handler = rtl9300_timer_interrupt,
136 .flags = IRQF_TIMER,
137 },
138};
139
140static void __init rtl9300_timer_setup(u8 timer)
141{
142 u32 v;
143
144 // Disable timer
145 writel(0, timer_of_base(&t_of) + 0x10 * timer + RTL9300_TC_CTRL);
146
147 // Acknowledge possibly pending IRQ
148 v = readl(timer_of_base(&t_of) + 0x10 * timer + RTL9300_TC_INT);
149 if (v & RTL9300_TC_INT_IP)
150 writel(v, timer_of_base(&t_of) + 0x10 * timer + RTL9300_TC_INT);
151
152 // Setup maximum period (for use as clock-source)
153 writel(0x0fffffff, timer_of_base(&t_of) + 0x10 * timer + RTL9300_TC_DATA);
154}
155
156
157static int __init rtl9300_timer_init(struct device_node *node)
158{
159 int err = 0;
160 unsigned long rate;
161
162 pr_info("%s: setting up timer\n", __func__);
163
164 err = timer_of_init(node, &t_of);
165 if (err)
166 return err;
167
168 rate = timer_of_rate(&t_of) / DIVISOR_RTL9300;
169 pr_info("Frequency in dts: %ld, my rate is %ld, period %ld\n",
170 timer_of_rate(&t_of), rate, timer_of_period(&t_of));
171 pr_info("With base %08x IRQ: %d\n", (u32)timer_of_base(&t_of), timer_of_irq(&t_of));
172
173 // Configure clock source and register it for scheduling
174 rtl9300_timer_setup(TIMER_CLK_SRC);
175 rtl9300_timer_start(&t_of, TIMER_CLK_SRC, TIMER_MODE_REPEAT);
176
177 rtl9300_sched_reg = timer_of_base(&t_of) + TIMER_CLK_SRC * 0x10 + RTL9300_TC_CNT;
178
179 err = clocksource_mmio_init(rtl9300_sched_reg, node->name, rate , 100, N_BITS,
180 clocksource_mmio_readl_up);
181 if (err)
182 return err;
183
184 sched_clock_register(rtl9300_sched_clock_read, N_BITS, rate);
185
186 // Configure clock event source
187 rtl9300_timer_setup(TIMER_CLK_EVT);
188 clockevents_config_and_register(&t_of.clkevt, rate, 100, 0x0fffffff);
189
190 // Enable interrupt
191 writel(RTL9300_TC_INT_IE, timer_of_base(&t_of) + TIMER_BLK_EVT + RTL9300_TC_INT);
192
193 return err;
194}
195
196TIMER_OF_DECLARE(rtl9300_timer, "realtek,rtl9300-timer", rtl9300_timer_init);