1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2010, 2011 Texas Instruments Incorporated
4 * Contributed by: Mark Salter (msalter@redhat.com)
7 #include <linux/clockchips.h>
8 #include <linux/interrupt.h>
11 #include <linux/of_irq.h>
12 #include <linux/of_address.h>
15 #include <asm/special_insns.h>
16 #include <asm/timer64.h>
32 static struct timer_regs __iomem
*timer
;
34 #define TCR_TSTATLO 0x001
35 #define TCR_INVOUTPLO 0x002
36 #define TCR_INVINPLO 0x004
37 #define TCR_CPLO 0x008
38 #define TCR_ENAMODELO_ONCE 0x040
39 #define TCR_ENAMODELO_CONT 0x080
40 #define TCR_ENAMODELO_MASK 0x0c0
41 #define TCR_PWIDLO_MASK 0x030
42 #define TCR_CLKSRCLO 0x100
43 #define TCR_TIENLO 0x200
44 #define TCR_TSTATHI (0x001 << 16)
45 #define TCR_INVOUTPHI (0x002 << 16)
46 #define TCR_CPHI (0x008 << 16)
47 #define TCR_PWIDHI_MASK (0x030 << 16)
48 #define TCR_ENAMODEHI_ONCE (0x040 << 16)
49 #define TCR_ENAMODEHI_CONT (0x080 << 16)
50 #define TCR_ENAMODEHI_MASK (0x0c0 << 16)
52 #define TGCR_TIMLORS 0x001
53 #define TGCR_TIMHIRS 0x002
54 #define TGCR_TIMMODE_UD32 0x004
55 #define TGCR_TIMMODE_WDT64 0x008
56 #define TGCR_TIMMODE_CD32 0x00c
57 #define TGCR_TIMMODE_MASK 0x00c
58 #define TGCR_PSCHI_MASK (0x00f << 8)
59 #define TGCR_TDDRHI_MASK (0x00f << 12)
62 * Timer clocks are divided down from the CPU clock
63 * The divisor is in the EMUMGTCLKSPD register
65 #define TIMER_DIVISOR \
66 ((soc_readl(&timer->emumgt) & (0xf << 16)) >> 16)
68 #define TIMER64_RATE (c6x_core_freq / TIMER_DIVISOR)
70 #define TIMER64_MODE_DISABLED 0
71 #define TIMER64_MODE_ONE_SHOT TCR_ENAMODELO_ONCE
72 #define TIMER64_MODE_PERIODIC TCR_ENAMODELO_CONT
74 static int timer64_mode
;
75 static int timer64_devstate_id
= -1;
77 static void timer64_config(unsigned long period
)
79 u32 tcr
= soc_readl(&timer
->tcr
) & ~TCR_ENAMODELO_MASK
;
81 soc_writel(tcr
, &timer
->tcr
);
82 soc_writel(period
- 1, &timer
->prdlo
);
83 soc_writel(0, &timer
->cntlo
);
85 soc_writel(tcr
, &timer
->tcr
);
88 static void timer64_enable(void)
92 if (timer64_devstate_id
>= 0)
93 dscr_set_devstate(timer64_devstate_id
, DSCR_DEVSTATE_ENABLED
);
95 /* disable timer, reset count */
96 soc_writel(soc_readl(&timer
->tcr
) & ~TCR_ENAMODELO_MASK
, &timer
->tcr
);
97 soc_writel(0, &timer
->prdlo
);
99 /* use internal clock and 1 cycle pulse width */
100 val
= soc_readl(&timer
->tcr
);
101 soc_writel(val
& ~(TCR_CLKSRCLO
| TCR_PWIDLO_MASK
), &timer
->tcr
);
103 /* dual 32-bit unchained mode */
104 val
= soc_readl(&timer
->tgcr
) & ~TGCR_TIMMODE_MASK
;
105 soc_writel(val
, &timer
->tgcr
);
106 soc_writel(val
| (TGCR_TIMLORS
| TGCR_TIMMODE_UD32
), &timer
->tgcr
);
109 static void timer64_disable(void)
111 /* disable timer, reset count */
112 soc_writel(soc_readl(&timer
->tcr
) & ~TCR_ENAMODELO_MASK
, &timer
->tcr
);
113 soc_writel(0, &timer
->prdlo
);
115 if (timer64_devstate_id
>= 0)
116 dscr_set_devstate(timer64_devstate_id
, DSCR_DEVSTATE_DISABLED
);
119 static int next_event(unsigned long delta
,
120 struct clock_event_device
*evt
)
122 timer64_config(delta
);
126 static int set_periodic(struct clock_event_device
*evt
)
129 timer64_mode
= TIMER64_MODE_PERIODIC
;
130 timer64_config(TIMER64_RATE
/ HZ
);
134 static int set_oneshot(struct clock_event_device
*evt
)
137 timer64_mode
= TIMER64_MODE_ONE_SHOT
;
141 static int shutdown(struct clock_event_device
*evt
)
143 timer64_mode
= TIMER64_MODE_DISABLED
;
148 static struct clock_event_device t64_clockevent_device
= {
149 .name
= "TIMER64_EVT32_TIMER",
150 .features
= CLOCK_EVT_FEAT_ONESHOT
|
151 CLOCK_EVT_FEAT_PERIODIC
,
153 .set_state_shutdown
= shutdown
,
154 .set_state_periodic
= set_periodic
,
155 .set_state_oneshot
= set_oneshot
,
156 .set_next_event
= next_event
,
159 static irqreturn_t
timer_interrupt(int irq
, void *dev_id
)
161 struct clock_event_device
*cd
= &t64_clockevent_device
;
163 cd
->event_handler(cd
);
168 void __init
timer64_init(void)
170 struct clock_event_device
*cd
= &t64_clockevent_device
;
171 struct device_node
*np
, *first
= NULL
;
175 for_each_compatible_node(np
, NULL
, "ti,c64x+timer64") {
176 err
= of_property_read_u32(np
, "ti,core-mask", &val
);
178 if (val
& (1 << get_coreid())) {
186 /* try first one with no core-mask */
188 np
= of_node_get(first
);
190 pr_debug("Cannot find ti,c64x+timer64 timer.\n");
195 timer
= of_iomap(np
, 0);
197 pr_debug("%pOF: Cannot map timer registers.\n", np
);
200 pr_debug("%pOF: Timer registers=%p.\n", np
, timer
);
202 cd
->irq
= irq_of_parse_and_map(np
, 0);
203 if (cd
->irq
== NO_IRQ
) {
204 pr_debug("%pOF: Cannot find interrupt.\n", np
);
209 /* If there is a device state control, save the ID. */
210 err
= of_property_read_u32(np
, "ti,dscr-dev-enable", &val
);
212 timer64_devstate_id
= val
;
215 * It is necessary to enable the timer block here because
216 * the TIMER_DIVISOR macro needs to read a timer register
217 * to get the divisor.
219 dscr_set_devstate(timer64_devstate_id
, DSCR_DEVSTATE_ENABLED
);
222 pr_debug("%pOF: Timer irq=%d.\n", np
, cd
->irq
);
224 clockevents_calc_mult_shift(cd
, c6x_core_freq
/ TIMER_DIVISOR
, 5);
226 cd
->max_delta_ns
= clockevent_delta2ns(0x7fffffff, cd
);
227 cd
->max_delta_ticks
= 0x7fffffff;
228 cd
->min_delta_ns
= clockevent_delta2ns(250, cd
);
229 cd
->min_delta_ticks
= 250;
231 cd
->cpumask
= cpumask_of(smp_processor_id());
233 clockevents_register_device(cd
);
234 if (request_irq(cd
->irq
, timer_interrupt
, IRQF_TIMER
, "timer",
235 &t64_clockevent_device
))
236 pr_err("Failed to request irq %d (timer)\n", cd
->irq
);