1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2018 Socionext Inc.
7 #include <linux/interrupt.h>
9 #include <linux/irqreturn.h>
10 #include <linux/sched_clock.h>
13 #define MLB_TMR_TMCSR_OFS 0x0
14 #define MLB_TMR_TMR_OFS 0x4
15 #define MLB_TMR_TMRLR1_OFS 0x8
16 #define MLB_TMR_TMRLR2_OFS 0xc
17 #define MLB_TMR_REGSZPCH 0x10
19 #define MLB_TMR_TMCSR_OUTL BIT(5)
20 #define MLB_TMR_TMCSR_RELD BIT(4)
21 #define MLB_TMR_TMCSR_INTE BIT(3)
22 #define MLB_TMR_TMCSR_UF BIT(2)
23 #define MLB_TMR_TMCSR_CNTE BIT(1)
24 #define MLB_TMR_TMCSR_TRG BIT(0)
26 #define MLB_TMR_TMCSR_CSL_DIV2 0
27 #define MLB_TMR_DIV_CNT 2
29 #define MLB_TMR_SRC_CH 1
30 #define MLB_TMR_EVT_CH 0
32 #define MLB_TMR_SRC_CH_OFS (MLB_TMR_REGSZPCH * MLB_TMR_SRC_CH)
33 #define MLB_TMR_EVT_CH_OFS (MLB_TMR_REGSZPCH * MLB_TMR_EVT_CH)
35 #define MLB_TMR_SRC_TMCSR_OFS (MLB_TMR_SRC_CH_OFS + MLB_TMR_TMCSR_OFS)
36 #define MLB_TMR_SRC_TMR_OFS (MLB_TMR_SRC_CH_OFS + MLB_TMR_TMR_OFS)
37 #define MLB_TMR_SRC_TMRLR1_OFS (MLB_TMR_SRC_CH_OFS + MLB_TMR_TMRLR1_OFS)
38 #define MLB_TMR_SRC_TMRLR2_OFS (MLB_TMR_SRC_CH_OFS + MLB_TMR_TMRLR2_OFS)
40 #define MLB_TMR_EVT_TMCSR_OFS (MLB_TMR_EVT_CH_OFS + MLB_TMR_TMCSR_OFS)
41 #define MLB_TMR_EVT_TMR_OFS (MLB_TMR_EVT_CH_OFS + MLB_TMR_TMR_OFS)
42 #define MLB_TMR_EVT_TMRLR1_OFS (MLB_TMR_EVT_CH_OFS + MLB_TMR_TMRLR1_OFS)
43 #define MLB_TMR_EVT_TMRLR2_OFS (MLB_TMR_EVT_CH_OFS + MLB_TMR_TMRLR2_OFS)
45 #define MLB_TIMER_RATING 500
46 #define MLB_TIMER_ONESHOT 0
47 #define MLB_TIMER_PERIODIC 1
49 static irqreturn_t
mlb_timer_interrupt(int irq
, void *dev_id
)
51 struct clock_event_device
*clk
= dev_id
;
52 struct timer_of
*to
= to_timer_of(clk
);
55 val
= readl_relaxed(timer_of_base(to
) + MLB_TMR_EVT_TMCSR_OFS
);
56 val
&= ~MLB_TMR_TMCSR_UF
;
57 writel_relaxed(val
, timer_of_base(to
) + MLB_TMR_EVT_TMCSR_OFS
);
59 clk
->event_handler(clk
);
64 static void mlb_evt_timer_start(struct timer_of
*to
, bool periodic
)
66 u32 val
= MLB_TMR_TMCSR_CSL_DIV2
;
68 val
|= MLB_TMR_TMCSR_CNTE
| MLB_TMR_TMCSR_TRG
| MLB_TMR_TMCSR_INTE
;
70 val
|= MLB_TMR_TMCSR_RELD
;
71 writel_relaxed(val
, timer_of_base(to
) + MLB_TMR_EVT_TMCSR_OFS
);
74 static void mlb_evt_timer_stop(struct timer_of
*to
)
76 u32 val
= readl_relaxed(timer_of_base(to
) + MLB_TMR_EVT_TMCSR_OFS
);
78 val
&= ~MLB_TMR_TMCSR_CNTE
;
79 writel_relaxed(val
, timer_of_base(to
) + MLB_TMR_EVT_TMCSR_OFS
);
82 static void mlb_evt_timer_register_count(struct timer_of
*to
, unsigned long cnt
)
84 writel_relaxed(cnt
, timer_of_base(to
) + MLB_TMR_EVT_TMRLR1_OFS
);
87 static int mlb_set_state_periodic(struct clock_event_device
*clk
)
89 struct timer_of
*to
= to_timer_of(clk
);
91 mlb_evt_timer_stop(to
);
92 mlb_evt_timer_register_count(to
, to
->of_clk
.period
);
93 mlb_evt_timer_start(to
, MLB_TIMER_PERIODIC
);
97 static int mlb_set_state_oneshot(struct clock_event_device
*clk
)
99 struct timer_of
*to
= to_timer_of(clk
);
101 mlb_evt_timer_stop(to
);
102 mlb_evt_timer_start(to
, MLB_TIMER_ONESHOT
);
106 static int mlb_set_state_shutdown(struct clock_event_device
*clk
)
108 struct timer_of
*to
= to_timer_of(clk
);
110 mlb_evt_timer_stop(to
);
114 static int mlb_clkevt_next_event(unsigned long event
,
115 struct clock_event_device
*clk
)
117 struct timer_of
*to
= to_timer_of(clk
);
119 mlb_evt_timer_stop(to
);
120 mlb_evt_timer_register_count(to
, event
);
121 mlb_evt_timer_start(to
, MLB_TIMER_ONESHOT
);
125 static int mlb_config_clock_source(struct timer_of
*to
)
127 u32 val
= MLB_TMR_TMCSR_CSL_DIV2
;
129 writel_relaxed(val
, timer_of_base(to
) + MLB_TMR_SRC_TMCSR_OFS
);
130 writel_relaxed(~0, timer_of_base(to
) + MLB_TMR_SRC_TMRLR1_OFS
);
131 writel_relaxed(~0, timer_of_base(to
) + MLB_TMR_SRC_TMRLR2_OFS
);
132 val
|= MLB_TMR_TMCSR_RELD
| MLB_TMR_TMCSR_CNTE
| MLB_TMR_TMCSR_TRG
;
133 writel_relaxed(val
, timer_of_base(to
) + MLB_TMR_SRC_TMCSR_OFS
);
137 static int mlb_config_clock_event(struct timer_of
*to
)
139 writel_relaxed(0, timer_of_base(to
) + MLB_TMR_EVT_TMCSR_OFS
);
143 static struct timer_of to
= {
144 .flags
= TIMER_OF_IRQ
| TIMER_OF_BASE
| TIMER_OF_CLOCK
,
147 .name
= "mlb-clkevt",
148 .rating
= MLB_TIMER_RATING
,
149 .cpumask
= cpu_possible_mask
,
150 .features
= CLOCK_EVT_FEAT_DYNIRQ
| CLOCK_EVT_FEAT_ONESHOT
,
151 .set_state_oneshot
= mlb_set_state_oneshot
,
152 .set_state_periodic
= mlb_set_state_periodic
,
153 .set_state_shutdown
= mlb_set_state_shutdown
,
154 .set_next_event
= mlb_clkevt_next_event
,
158 .flags
= IRQF_TIMER
| IRQF_IRQPOLL
,
159 .handler
= mlb_timer_interrupt
,
163 static u64 notrace
mlb_timer_sched_read(void)
165 return ~readl_relaxed(timer_of_base(&to
) + MLB_TMR_SRC_TMR_OFS
);
168 static int __init
mlb_timer_init(struct device_node
*node
)
173 ret
= timer_of_init(node
, &to
);
177 rate
= timer_of_rate(&to
) / MLB_TMR_DIV_CNT
;
178 mlb_config_clock_source(&to
);
179 clocksource_mmio_init(timer_of_base(&to
) + MLB_TMR_SRC_TMR_OFS
,
180 node
->name
, rate
, MLB_TIMER_RATING
, 32,
181 clocksource_mmio_readl_down
);
182 sched_clock_register(mlb_timer_sched_read
, 32, rate
);
183 mlb_config_clock_event(&to
);
184 clockevents_config_and_register(&to
.clkevt
, timer_of_rate(&to
), 15,
188 TIMER_OF_DECLARE(mlb_peritimer
, "socionext,milbeaut-timer",