1
2
3
4
5
6
7
8#include <linux/init.h>
9#include <linux/clockchips.h>
10#include <linux/clocksource.h>
11#include <linux/interrupt.h>
12#include <linux/err.h>
13#include <linux/platform_device.h>
14#include <linux/ioport.h>
15#include <linux/of.h>
16#include <linux/of_address.h>
17#include <linux/of_irq.h>
18#include <linux/module.h>
19
20#include <asm/timer-regs.h>
21#include <asm/hexagon_vm.h>
22
23
24
25
26
27
28
29
30
31
32cycles_t pcycle_freq_mhz;
33cycles_t thread_freq_mhz;
34cycles_t sleep_clk_freq;
35
36static struct resource rtos_timer_resources[] = {
37 {
38 .start = RTOS_TIMER_REGS_ADDR,
39 .end = RTOS_TIMER_REGS_ADDR+PAGE_SIZE-1,
40 .flags = IORESOURCE_MEM,
41 },
42};
43
44static struct platform_device rtos_timer_device = {
45 .name = "rtos_timer",
46 .id = -1,
47 .num_resources = ARRAY_SIZE(rtos_timer_resources),
48 .resource = rtos_timer_resources,
49};
50
51
52struct adsp_hw_timer_struct {
53 u32 match;
54 u32 count;
55 u32 enable;
56 u32 clear;
57};
58
59
60static __iomem struct adsp_hw_timer_struct *rtos_timer;
61
62static u64 timer_get_cycles(struct clocksource *cs)
63{
64 return (u64) __vmgettime();
65}
66
67static struct clocksource hexagon_clocksource = {
68 .name = "pcycles",
69 .rating = 250,
70 .read = timer_get_cycles,
71 .mask = CLOCKSOURCE_MASK(64),
72 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
73};
74
75static int set_next_event(unsigned long delta, struct clock_event_device *evt)
76{
77
78
79 iowrite32(1, &rtos_timer->clear);
80 iowrite32(0, &rtos_timer->clear);
81
82 iowrite32(delta, &rtos_timer->match);
83 iowrite32(1 << TIMER_ENABLE, &rtos_timer->enable);
84 return 0;
85}
86
87#ifdef CONFIG_SMP
88
89static void broadcast(const struct cpumask *mask)
90{
91 send_ipi(mask, IPI_TIMER);
92}
93#endif
94
95
96static struct clock_event_device hexagon_clockevent_dev = {
97 .name = "clockevent",
98 .features = CLOCK_EVT_FEAT_ONESHOT,
99 .rating = 400,
100 .irq = RTOS_TIMER_INT,
101 .set_next_event = set_next_event,
102#ifdef CONFIG_SMP
103 .broadcast = broadcast,
104#endif
105};
106
107#ifdef CONFIG_SMP
108static DEFINE_PER_CPU(struct clock_event_device, clock_events);
109
110void setup_percpu_clockdev(void)
111{
112 int cpu = smp_processor_id();
113 struct clock_event_device *ce_dev = &hexagon_clockevent_dev;
114 struct clock_event_device *dummy_clock_dev =
115 &per_cpu(clock_events, cpu);
116
117 memcpy(dummy_clock_dev, ce_dev, sizeof(*dummy_clock_dev));
118 INIT_LIST_HEAD(&dummy_clock_dev->list);
119
120 dummy_clock_dev->features = CLOCK_EVT_FEAT_DUMMY;
121 dummy_clock_dev->cpumask = cpumask_of(cpu);
122
123 clockevents_register_device(dummy_clock_dev);
124}
125
126
127void ipi_timer(void)
128{
129 int cpu = smp_processor_id();
130 struct clock_event_device *ce_dev = &per_cpu(clock_events, cpu);
131
132 ce_dev->event_handler(ce_dev);
133}
134#endif
135
136static irqreturn_t timer_interrupt(int irq, void *devid)
137{
138 struct clock_event_device *ce_dev = &hexagon_clockevent_dev;
139
140 iowrite32(0, &rtos_timer->enable);
141 ce_dev->event_handler(ce_dev);
142
143 return IRQ_HANDLED;
144}
145
146
147
148
149
150
151
152
153
154
155void __init time_init_deferred(void)
156{
157 struct resource *resource = NULL;
158 struct clock_event_device *ce_dev = &hexagon_clockevent_dev;
159 unsigned long flag = IRQF_TIMER | IRQF_TRIGGER_RISING;
160
161 ce_dev->cpumask = cpu_all_mask;
162
163 if (!resource)
164 resource = rtos_timer_device.resource;
165
166
167 rtos_timer = ioremap(resource->start, resource_size(resource));
168
169 if (!rtos_timer) {
170 release_mem_region(resource->start, resource_size(resource));
171 }
172 clocksource_register_khz(&hexagon_clocksource, pcycle_freq_mhz * 1000);
173
174
175
176
177
178
179
180 clockevents_calc_mult_shift(ce_dev, sleep_clk_freq, 4);
181
182 ce_dev->max_delta_ns = clockevent_delta2ns(0x7fffffff, ce_dev);
183 ce_dev->max_delta_ticks = 0x7fffffff;
184 ce_dev->min_delta_ns = clockevent_delta2ns(0xf, ce_dev);
185 ce_dev->min_delta_ticks = 0xf;
186
187#ifdef CONFIG_SMP
188 setup_percpu_clockdev();
189#endif
190
191 clockevents_register_device(ce_dev);
192 if (request_irq(ce_dev->irq, timer_interrupt, flag, "rtos_timer", NULL))
193 pr_err("Failed to register rtos_timer interrupt\n");
194}
195
196void __init time_init(void)
197{
198 late_time_init = time_init_deferred;
199}
200
201void __delay(unsigned long cycles)
202{
203 unsigned long long start = __vmgettime();
204
205 while ((__vmgettime() - start) < cycles)
206 cpu_relax();
207}
208EXPORT_SYMBOL(__delay);
209
210
211
212
213
214static long long fudgefactor = 350;
215
216void __udelay(unsigned long usecs)
217{
218 unsigned long long start = __vmgettime();
219 unsigned long long finish = (pcycle_freq_mhz * usecs) - fudgefactor;
220
221 while ((__vmgettime() - start) < finish)
222 cpu_relax();
223}
224EXPORT_SYMBOL(__udelay);
225