1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0+
2*4882a593Smuzhiyun //
3*4882a593Smuzhiyun // Copyright 2016 Freescale Semiconductor, Inc.
4*4882a593Smuzhiyun // Copyright 2017 NXP
5*4882a593Smuzhiyun
6*4882a593Smuzhiyun #include <linux/clk.h>
7*4882a593Smuzhiyun #include <linux/clockchips.h>
8*4882a593Smuzhiyun #include <linux/clocksource.h>
9*4882a593Smuzhiyun #include <linux/delay.h>
10*4882a593Smuzhiyun #include <linux/interrupt.h>
11*4882a593Smuzhiyun #include <linux/sched_clock.h>
12*4882a593Smuzhiyun
13*4882a593Smuzhiyun #include "timer-of.h"
14*4882a593Smuzhiyun
15*4882a593Smuzhiyun #define TPM_PARAM 0x4
16*4882a593Smuzhiyun #define TPM_PARAM_WIDTH_SHIFT 16
17*4882a593Smuzhiyun #define TPM_PARAM_WIDTH_MASK (0xff << 16)
18*4882a593Smuzhiyun #define TPM_SC 0x10
19*4882a593Smuzhiyun #define TPM_SC_CMOD_INC_PER_CNT (0x1 << 3)
20*4882a593Smuzhiyun #define TPM_SC_CMOD_DIV_DEFAULT 0x3
21*4882a593Smuzhiyun #define TPM_SC_CMOD_DIV_MAX 0x7
22*4882a593Smuzhiyun #define TPM_SC_TOF_MASK (0x1 << 7)
23*4882a593Smuzhiyun #define TPM_CNT 0x14
24*4882a593Smuzhiyun #define TPM_MOD 0x18
25*4882a593Smuzhiyun #define TPM_STATUS 0x1c
26*4882a593Smuzhiyun #define TPM_STATUS_CH0F BIT(0)
27*4882a593Smuzhiyun #define TPM_C0SC 0x20
28*4882a593Smuzhiyun #define TPM_C0SC_CHIE BIT(6)
29*4882a593Smuzhiyun #define TPM_C0SC_MODE_SHIFT 2
30*4882a593Smuzhiyun #define TPM_C0SC_MODE_MASK 0x3c
31*4882a593Smuzhiyun #define TPM_C0SC_MODE_SW_COMPARE 0x4
32*4882a593Smuzhiyun #define TPM_C0SC_CHF_MASK (0x1 << 7)
33*4882a593Smuzhiyun #define TPM_C0V 0x24
34*4882a593Smuzhiyun
35*4882a593Smuzhiyun static int counter_width;
36*4882a593Smuzhiyun static void __iomem *timer_base;
37*4882a593Smuzhiyun
tpm_timer_disable(void)38*4882a593Smuzhiyun static inline void tpm_timer_disable(void)
39*4882a593Smuzhiyun {
40*4882a593Smuzhiyun unsigned int val;
41*4882a593Smuzhiyun
42*4882a593Smuzhiyun /* channel disable */
43*4882a593Smuzhiyun val = readl(timer_base + TPM_C0SC);
44*4882a593Smuzhiyun val &= ~(TPM_C0SC_MODE_MASK | TPM_C0SC_CHIE);
45*4882a593Smuzhiyun writel(val, timer_base + TPM_C0SC);
46*4882a593Smuzhiyun }
47*4882a593Smuzhiyun
tpm_timer_enable(void)48*4882a593Smuzhiyun static inline void tpm_timer_enable(void)
49*4882a593Smuzhiyun {
50*4882a593Smuzhiyun unsigned int val;
51*4882a593Smuzhiyun
52*4882a593Smuzhiyun /* channel enabled in sw compare mode */
53*4882a593Smuzhiyun val = readl(timer_base + TPM_C0SC);
54*4882a593Smuzhiyun val |= (TPM_C0SC_MODE_SW_COMPARE << TPM_C0SC_MODE_SHIFT) |
55*4882a593Smuzhiyun TPM_C0SC_CHIE;
56*4882a593Smuzhiyun writel(val, timer_base + TPM_C0SC);
57*4882a593Smuzhiyun }
58*4882a593Smuzhiyun
tpm_irq_acknowledge(void)59*4882a593Smuzhiyun static inline void tpm_irq_acknowledge(void)
60*4882a593Smuzhiyun {
61*4882a593Smuzhiyun writel(TPM_STATUS_CH0F, timer_base + TPM_STATUS);
62*4882a593Smuzhiyun }
63*4882a593Smuzhiyun
tpm_read_counter(void)64*4882a593Smuzhiyun static inline unsigned long tpm_read_counter(void)
65*4882a593Smuzhiyun {
66*4882a593Smuzhiyun return readl(timer_base + TPM_CNT);
67*4882a593Smuzhiyun }
68*4882a593Smuzhiyun
69*4882a593Smuzhiyun #if defined(CONFIG_ARM)
70*4882a593Smuzhiyun static struct delay_timer tpm_delay_timer;
71*4882a593Smuzhiyun
tpm_read_current_timer(void)72*4882a593Smuzhiyun static unsigned long tpm_read_current_timer(void)
73*4882a593Smuzhiyun {
74*4882a593Smuzhiyun return tpm_read_counter();
75*4882a593Smuzhiyun }
76*4882a593Smuzhiyun #endif
77*4882a593Smuzhiyun
tpm_read_sched_clock(void)78*4882a593Smuzhiyun static u64 notrace tpm_read_sched_clock(void)
79*4882a593Smuzhiyun {
80*4882a593Smuzhiyun return tpm_read_counter();
81*4882a593Smuzhiyun }
82*4882a593Smuzhiyun
tpm_set_next_event(unsigned long delta,struct clock_event_device * evt)83*4882a593Smuzhiyun static int tpm_set_next_event(unsigned long delta,
84*4882a593Smuzhiyun struct clock_event_device *evt)
85*4882a593Smuzhiyun {
86*4882a593Smuzhiyun unsigned long next, now;
87*4882a593Smuzhiyun
88*4882a593Smuzhiyun next = tpm_read_counter();
89*4882a593Smuzhiyun next += delta;
90*4882a593Smuzhiyun writel(next, timer_base + TPM_C0V);
91*4882a593Smuzhiyun now = tpm_read_counter();
92*4882a593Smuzhiyun
93*4882a593Smuzhiyun /*
94*4882a593Smuzhiyun * NOTE: We observed in a very small probability, the bus fabric
95*4882a593Smuzhiyun * contention between GPU and A7 may results a few cycles delay
96*4882a593Smuzhiyun * of writing CNT registers which may cause the min_delta event got
97*4882a593Smuzhiyun * missed, so we need add a ETIME check here in case it happened.
98*4882a593Smuzhiyun */
99*4882a593Smuzhiyun return (int)(next - now) <= 0 ? -ETIME : 0;
100*4882a593Smuzhiyun }
101*4882a593Smuzhiyun
tpm_set_state_oneshot(struct clock_event_device * evt)102*4882a593Smuzhiyun static int tpm_set_state_oneshot(struct clock_event_device *evt)
103*4882a593Smuzhiyun {
104*4882a593Smuzhiyun tpm_timer_enable();
105*4882a593Smuzhiyun
106*4882a593Smuzhiyun return 0;
107*4882a593Smuzhiyun }
108*4882a593Smuzhiyun
tpm_set_state_shutdown(struct clock_event_device * evt)109*4882a593Smuzhiyun static int tpm_set_state_shutdown(struct clock_event_device *evt)
110*4882a593Smuzhiyun {
111*4882a593Smuzhiyun tpm_timer_disable();
112*4882a593Smuzhiyun
113*4882a593Smuzhiyun return 0;
114*4882a593Smuzhiyun }
115*4882a593Smuzhiyun
tpm_timer_interrupt(int irq,void * dev_id)116*4882a593Smuzhiyun static irqreturn_t tpm_timer_interrupt(int irq, void *dev_id)
117*4882a593Smuzhiyun {
118*4882a593Smuzhiyun struct clock_event_device *evt = dev_id;
119*4882a593Smuzhiyun
120*4882a593Smuzhiyun tpm_irq_acknowledge();
121*4882a593Smuzhiyun
122*4882a593Smuzhiyun evt->event_handler(evt);
123*4882a593Smuzhiyun
124*4882a593Smuzhiyun return IRQ_HANDLED;
125*4882a593Smuzhiyun }
126*4882a593Smuzhiyun
127*4882a593Smuzhiyun static struct timer_of to_tpm = {
128*4882a593Smuzhiyun .flags = TIMER_OF_IRQ | TIMER_OF_BASE | TIMER_OF_CLOCK,
129*4882a593Smuzhiyun .clkevt = {
130*4882a593Smuzhiyun .name = "i.MX7ULP TPM Timer",
131*4882a593Smuzhiyun .rating = 200,
132*4882a593Smuzhiyun .features = CLOCK_EVT_FEAT_ONESHOT,
133*4882a593Smuzhiyun .set_state_shutdown = tpm_set_state_shutdown,
134*4882a593Smuzhiyun .set_state_oneshot = tpm_set_state_oneshot,
135*4882a593Smuzhiyun .set_next_event = tpm_set_next_event,
136*4882a593Smuzhiyun .cpumask = cpu_possible_mask,
137*4882a593Smuzhiyun },
138*4882a593Smuzhiyun .of_irq = {
139*4882a593Smuzhiyun .handler = tpm_timer_interrupt,
140*4882a593Smuzhiyun .flags = IRQF_TIMER | IRQF_IRQPOLL,
141*4882a593Smuzhiyun },
142*4882a593Smuzhiyun .of_clk = {
143*4882a593Smuzhiyun .name = "per",
144*4882a593Smuzhiyun },
145*4882a593Smuzhiyun };
146*4882a593Smuzhiyun
tpm_clocksource_init(void)147*4882a593Smuzhiyun static int __init tpm_clocksource_init(void)
148*4882a593Smuzhiyun {
149*4882a593Smuzhiyun #if defined(CONFIG_ARM)
150*4882a593Smuzhiyun tpm_delay_timer.read_current_timer = &tpm_read_current_timer;
151*4882a593Smuzhiyun tpm_delay_timer.freq = timer_of_rate(&to_tpm) >> 3;
152*4882a593Smuzhiyun register_current_timer_delay(&tpm_delay_timer);
153*4882a593Smuzhiyun #endif
154*4882a593Smuzhiyun
155*4882a593Smuzhiyun sched_clock_register(tpm_read_sched_clock, counter_width,
156*4882a593Smuzhiyun timer_of_rate(&to_tpm) >> 3);
157*4882a593Smuzhiyun
158*4882a593Smuzhiyun return clocksource_mmio_init(timer_base + TPM_CNT,
159*4882a593Smuzhiyun "imx-tpm",
160*4882a593Smuzhiyun timer_of_rate(&to_tpm) >> 3,
161*4882a593Smuzhiyun to_tpm.clkevt.rating,
162*4882a593Smuzhiyun counter_width,
163*4882a593Smuzhiyun clocksource_mmio_readl_up);
164*4882a593Smuzhiyun }
165*4882a593Smuzhiyun
tpm_clockevent_init(void)166*4882a593Smuzhiyun static void __init tpm_clockevent_init(void)
167*4882a593Smuzhiyun {
168*4882a593Smuzhiyun clockevents_config_and_register(&to_tpm.clkevt,
169*4882a593Smuzhiyun timer_of_rate(&to_tpm) >> 3,
170*4882a593Smuzhiyun 300,
171*4882a593Smuzhiyun GENMASK(counter_width - 1,
172*4882a593Smuzhiyun 1));
173*4882a593Smuzhiyun }
174*4882a593Smuzhiyun
tpm_timer_init(struct device_node * np)175*4882a593Smuzhiyun static int __init tpm_timer_init(struct device_node *np)
176*4882a593Smuzhiyun {
177*4882a593Smuzhiyun struct clk *ipg;
178*4882a593Smuzhiyun int ret;
179*4882a593Smuzhiyun
180*4882a593Smuzhiyun ipg = of_clk_get_by_name(np, "ipg");
181*4882a593Smuzhiyun if (IS_ERR(ipg)) {
182*4882a593Smuzhiyun pr_err("tpm: failed to get ipg clk\n");
183*4882a593Smuzhiyun return -ENODEV;
184*4882a593Smuzhiyun }
185*4882a593Smuzhiyun /* enable clk before accessing registers */
186*4882a593Smuzhiyun ret = clk_prepare_enable(ipg);
187*4882a593Smuzhiyun if (ret) {
188*4882a593Smuzhiyun pr_err("tpm: ipg clock enable failed (%d)\n", ret);
189*4882a593Smuzhiyun clk_put(ipg);
190*4882a593Smuzhiyun return ret;
191*4882a593Smuzhiyun }
192*4882a593Smuzhiyun
193*4882a593Smuzhiyun ret = timer_of_init(np, &to_tpm);
194*4882a593Smuzhiyun if (ret)
195*4882a593Smuzhiyun return ret;
196*4882a593Smuzhiyun
197*4882a593Smuzhiyun timer_base = timer_of_base(&to_tpm);
198*4882a593Smuzhiyun
199*4882a593Smuzhiyun counter_width = (readl(timer_base + TPM_PARAM)
200*4882a593Smuzhiyun & TPM_PARAM_WIDTH_MASK) >> TPM_PARAM_WIDTH_SHIFT;
201*4882a593Smuzhiyun /* use rating 200 for 32-bit counter and 150 for 16-bit counter */
202*4882a593Smuzhiyun to_tpm.clkevt.rating = counter_width == 0x20 ? 200 : 150;
203*4882a593Smuzhiyun
204*4882a593Smuzhiyun /*
205*4882a593Smuzhiyun * Initialize tpm module to a known state
206*4882a593Smuzhiyun * 1) Counter disabled
207*4882a593Smuzhiyun * 2) TPM counter operates in up counting mode
208*4882a593Smuzhiyun * 3) Timer Overflow Interrupt disabled
209*4882a593Smuzhiyun * 4) Channel0 disabled
210*4882a593Smuzhiyun * 5) DMA transfers disabled
211*4882a593Smuzhiyun */
212*4882a593Smuzhiyun /* make sure counter is disabled */
213*4882a593Smuzhiyun writel(0, timer_base + TPM_SC);
214*4882a593Smuzhiyun /* TOF is W1C */
215*4882a593Smuzhiyun writel(TPM_SC_TOF_MASK, timer_base + TPM_SC);
216*4882a593Smuzhiyun writel(0, timer_base + TPM_CNT);
217*4882a593Smuzhiyun /* CHF is W1C */
218*4882a593Smuzhiyun writel(TPM_C0SC_CHF_MASK, timer_base + TPM_C0SC);
219*4882a593Smuzhiyun
220*4882a593Smuzhiyun /*
221*4882a593Smuzhiyun * increase per cnt,
222*4882a593Smuzhiyun * div 8 for 32-bit counter and div 128 for 16-bit counter
223*4882a593Smuzhiyun */
224*4882a593Smuzhiyun writel(TPM_SC_CMOD_INC_PER_CNT |
225*4882a593Smuzhiyun (counter_width == 0x20 ?
226*4882a593Smuzhiyun TPM_SC_CMOD_DIV_DEFAULT : TPM_SC_CMOD_DIV_MAX),
227*4882a593Smuzhiyun timer_base + TPM_SC);
228*4882a593Smuzhiyun
229*4882a593Smuzhiyun /* set MOD register to maximum for free running mode */
230*4882a593Smuzhiyun writel(GENMASK(counter_width - 1, 0), timer_base + TPM_MOD);
231*4882a593Smuzhiyun
232*4882a593Smuzhiyun tpm_clockevent_init();
233*4882a593Smuzhiyun
234*4882a593Smuzhiyun return tpm_clocksource_init();
235*4882a593Smuzhiyun }
236*4882a593Smuzhiyun TIMER_OF_DECLARE(imx7ulp, "fsl,imx7ulp-tpm", tpm_timer_init);
237