2 * Copyright (C) 2010, 2011 Texas Instruments Incorporated
3 * Contributed by: Mark Salter (msalter@redhat.com)
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
10 #include <linux/clockchips.h>
11 #include <linux/interrupt.h>
14 #include <linux/of_irq.h>
15 #include <linux/of_address.h>
18 #include <asm/special_insns.h>
19 #include <asm/timer64.h>
35 static struct timer_regs __iomem
*timer
;
37 #define TCR_TSTATLO 0x001
38 #define TCR_INVOUTPLO 0x002
39 #define TCR_INVINPLO 0x004
40 #define TCR_CPLO 0x008
41 #define TCR_ENAMODELO_ONCE 0x040
42 #define TCR_ENAMODELO_CONT 0x080
43 #define TCR_ENAMODELO_MASK 0x0c0
44 #define TCR_PWIDLO_MASK 0x030
45 #define TCR_CLKSRCLO 0x100
46 #define TCR_TIENLO 0x200
47 #define TCR_TSTATHI (0x001 << 16)
48 #define TCR_INVOUTPHI (0x002 << 16)
49 #define TCR_CPHI (0x008 << 16)
50 #define TCR_PWIDHI_MASK (0x030 << 16)
51 #define TCR_ENAMODEHI_ONCE (0x040 << 16)
52 #define TCR_ENAMODEHI_CONT (0x080 << 16)
53 #define TCR_ENAMODEHI_MASK (0x0c0 << 16)
55 #define TGCR_TIMLORS 0x001
56 #define TGCR_TIMHIRS 0x002
57 #define TGCR_TIMMODE_UD32 0x004
58 #define TGCR_TIMMODE_WDT64 0x008
59 #define TGCR_TIMMODE_CD32 0x00c
60 #define TGCR_TIMMODE_MASK 0x00c
61 #define TGCR_PSCHI_MASK (0x00f << 8)
62 #define TGCR_TDDRHI_MASK (0x00f << 12)
65 * Timer clocks are divided down from the CPU clock
66 * The divisor is in the EMUMGTCLKSPD register
68 #define TIMER_DIVISOR \
69 ((soc_readl(&timer->emumgt) & (0xf << 16)) >> 16)
71 #define TIMER64_RATE (c6x_core_freq / TIMER_DIVISOR)
73 #define TIMER64_MODE_DISABLED 0
74 #define TIMER64_MODE_ONE_SHOT TCR_ENAMODELO_ONCE
75 #define TIMER64_MODE_PERIODIC TCR_ENAMODELO_CONT
77 static int timer64_mode
;
78 static int timer64_devstate_id
= -1;
80 static void timer64_config(unsigned long period
)
82 u32 tcr
= soc_readl(&timer
->tcr
) & ~TCR_ENAMODELO_MASK
;
84 soc_writel(tcr
, &timer
->tcr
);
85 soc_writel(period
- 1, &timer
->prdlo
);
86 soc_writel(0, &timer
->cntlo
);
88 soc_writel(tcr
, &timer
->tcr
);
91 static void timer64_enable(void)
95 if (timer64_devstate_id
>= 0)
96 dscr_set_devstate(timer64_devstate_id
, DSCR_DEVSTATE_ENABLED
);
98 /* disable timer, reset count */
99 soc_writel(soc_readl(&timer
->tcr
) & ~TCR_ENAMODELO_MASK
, &timer
->tcr
);
100 soc_writel(0, &timer
->prdlo
);
102 /* use internal clock and 1 cycle pulse width */
103 val
= soc_readl(&timer
->tcr
);
104 soc_writel(val
& ~(TCR_CLKSRCLO
| TCR_PWIDLO_MASK
), &timer
->tcr
);
106 /* dual 32-bit unchained mode */
107 val
= soc_readl(&timer
->tgcr
) & ~TGCR_TIMMODE_MASK
;
108 soc_writel(val
, &timer
->tgcr
);
109 soc_writel(val
| (TGCR_TIMLORS
| TGCR_TIMMODE_UD32
), &timer
->tgcr
);
112 static void timer64_disable(void)
114 /* disable timer, reset count */
115 soc_writel(soc_readl(&timer
->tcr
) & ~TCR_ENAMODELO_MASK
, &timer
->tcr
);
116 soc_writel(0, &timer
->prdlo
);
118 if (timer64_devstate_id
>= 0)
119 dscr_set_devstate(timer64_devstate_id
, DSCR_DEVSTATE_DISABLED
);
122 static int next_event(unsigned long delta
,
123 struct clock_event_device
*evt
)
125 timer64_config(delta
);
129 static int set_periodic(struct clock_event_device
*evt
)
132 timer64_mode
= TIMER64_MODE_PERIODIC
;
133 timer64_config(TIMER64_RATE
/ HZ
);
137 static int set_oneshot(struct clock_event_device
*evt
)
140 timer64_mode
= TIMER64_MODE_ONE_SHOT
;
144 static int shutdown(struct clock_event_device
*evt
)
146 timer64_mode
= TIMER64_MODE_DISABLED
;
151 static struct clock_event_device t64_clockevent_device
= {
152 .name
= "TIMER64_EVT32_TIMER",
153 .features
= CLOCK_EVT_FEAT_ONESHOT
|
154 CLOCK_EVT_FEAT_PERIODIC
,
156 .set_state_shutdown
= shutdown
,
157 .set_state_periodic
= set_periodic
,
158 .set_state_oneshot
= set_oneshot
,
159 .set_next_event
= next_event
,
162 static irqreturn_t
timer_interrupt(int irq
, void *dev_id
)
164 struct clock_event_device
*cd
= &t64_clockevent_device
;
166 cd
->event_handler(cd
);
171 static struct irqaction timer_iact
= {
174 .handler
= timer_interrupt
,
175 .dev_id
= &t64_clockevent_device
,
178 void __init
timer64_init(void)
180 struct clock_event_device
*cd
= &t64_clockevent_device
;
181 struct device_node
*np
, *first
= NULL
;
185 for_each_compatible_node(np
, NULL
, "ti,c64x+timer64") {
186 err
= of_property_read_u32(np
, "ti,core-mask", &val
);
188 if (val
& (1 << get_coreid())) {
196 /* try first one with no core-mask */
198 np
= of_node_get(first
);
200 pr_debug("Cannot find ti,c64x+timer64 timer.\n");
205 timer
= of_iomap(np
, 0);
207 pr_debug("%s: Cannot map timer registers.\n", np
->full_name
);
210 pr_debug("%s: Timer registers=%p.\n", np
->full_name
, timer
);
212 cd
->irq
= irq_of_parse_and_map(np
, 0);
213 if (cd
->irq
== NO_IRQ
) {
214 pr_debug("%s: Cannot find interrupt.\n", np
->full_name
);
219 /* If there is a device state control, save the ID. */
220 err
= of_property_read_u32(np
, "ti,dscr-dev-enable", &val
);
222 timer64_devstate_id
= val
;
225 * It is necessary to enable the timer block here because
226 * the TIMER_DIVISOR macro needs to read a timer register
227 * to get the divisor.
229 dscr_set_devstate(timer64_devstate_id
, DSCR_DEVSTATE_ENABLED
);
232 pr_debug("%s: Timer irq=%d.\n", np
->full_name
, cd
->irq
);
234 clockevents_calc_mult_shift(cd
, c6x_core_freq
/ TIMER_DIVISOR
, 5);
236 cd
->max_delta_ns
= clockevent_delta2ns(0x7fffffff, cd
);
237 cd
->min_delta_ns
= clockevent_delta2ns(250, cd
);
239 cd
->cpumask
= cpumask_of(smp_processor_id());
241 clockevents_register_device(cd
);
242 setup_irq(cd
->irq
, &timer_iact
);