mm: hugetlb: fix hugepage memory leak caused by wrong reserve count
[linux/fpc-iii.git] / arch / blackfin / kernel / gptimers.c
blobd776773d38694818ae4e891a3274a43515e3e5de
1 /*
2 * gptimers.c - Blackfin General Purpose Timer core API
4 * Copyright (c) 2005-2008 Analog Devices Inc.
5 * Copyright (C) 2005 John DeHority
6 * Copyright (C) 2006 Hella Aglaia GmbH (awe@aglaia-gmbh.de)
8 * Licensed under the GPLv2.
9 */
11 #include <linux/kernel.h>
12 #include <linux/module.h>
13 #include <linux/io.h>
15 #include <asm/blackfin.h>
16 #include <asm/gptimers.h>
18 #ifdef DEBUG
19 # define tassert(expr)
20 #else
21 # define tassert(expr) \
22 if (!(expr)) \
23 printk(KERN_DEBUG "%s:%s:%i: Assertion failed: " #expr "\n", __FILE__, __func__, __LINE__);
24 #endif
26 #ifndef CONFIG_BF60x
27 # define BFIN_TIMER_NUM_GROUP (BFIN_TIMER_OCTET(MAX_BLACKFIN_GPTIMERS - 1) + 1)
28 #else
29 # define BFIN_TIMER_NUM_GROUP 1
30 #endif
32 static struct bfin_gptimer_regs * const timer_regs[MAX_BLACKFIN_GPTIMERS] =
34 (void *)TIMER0_CONFIG,
35 (void *)TIMER1_CONFIG,
36 (void *)TIMER2_CONFIG,
37 #if (MAX_BLACKFIN_GPTIMERS > 3)
38 (void *)TIMER3_CONFIG,
39 (void *)TIMER4_CONFIG,
40 (void *)TIMER5_CONFIG,
41 (void *)TIMER6_CONFIG,
42 (void *)TIMER7_CONFIG,
43 # if (MAX_BLACKFIN_GPTIMERS > 8)
44 (void *)TIMER8_CONFIG,
45 (void *)TIMER9_CONFIG,
46 (void *)TIMER10_CONFIG,
47 # if (MAX_BLACKFIN_GPTIMERS > 11)
48 (void *)TIMER11_CONFIG,
49 # endif
50 # endif
51 #endif
54 static struct bfin_gptimer_group_regs * const group_regs[BFIN_TIMER_NUM_GROUP] =
56 (void *)TIMER0_GROUP_REG,
57 #if (MAX_BLACKFIN_GPTIMERS > 8)
58 (void *)TIMER8_GROUP_REG,
59 #endif
62 static uint32_t const trun_mask[MAX_BLACKFIN_GPTIMERS] =
64 TIMER_STATUS_TRUN0,
65 TIMER_STATUS_TRUN1,
66 TIMER_STATUS_TRUN2,
67 #if (MAX_BLACKFIN_GPTIMERS > 3)
68 TIMER_STATUS_TRUN3,
69 TIMER_STATUS_TRUN4,
70 TIMER_STATUS_TRUN5,
71 TIMER_STATUS_TRUN6,
72 TIMER_STATUS_TRUN7,
73 # if (MAX_BLACKFIN_GPTIMERS > 8)
74 TIMER_STATUS_TRUN8,
75 TIMER_STATUS_TRUN9,
76 TIMER_STATUS_TRUN10,
77 # if (MAX_BLACKFIN_GPTIMERS > 11)
78 TIMER_STATUS_TRUN11,
79 # endif
80 # endif
81 #endif
84 static uint32_t const tovf_mask[MAX_BLACKFIN_GPTIMERS] =
86 TIMER_STATUS_TOVF0,
87 TIMER_STATUS_TOVF1,
88 TIMER_STATUS_TOVF2,
89 #if (MAX_BLACKFIN_GPTIMERS > 3)
90 TIMER_STATUS_TOVF3,
91 TIMER_STATUS_TOVF4,
92 TIMER_STATUS_TOVF5,
93 TIMER_STATUS_TOVF6,
94 TIMER_STATUS_TOVF7,
95 # if (MAX_BLACKFIN_GPTIMERS > 8)
96 TIMER_STATUS_TOVF8,
97 TIMER_STATUS_TOVF9,
98 TIMER_STATUS_TOVF10,
99 # if (MAX_BLACKFIN_GPTIMERS > 11)
100 TIMER_STATUS_TOVF11,
101 # endif
102 # endif
103 #endif
106 static uint32_t const timil_mask[MAX_BLACKFIN_GPTIMERS] =
108 TIMER_STATUS_TIMIL0,
109 TIMER_STATUS_TIMIL1,
110 TIMER_STATUS_TIMIL2,
111 #if (MAX_BLACKFIN_GPTIMERS > 3)
112 TIMER_STATUS_TIMIL3,
113 TIMER_STATUS_TIMIL4,
114 TIMER_STATUS_TIMIL5,
115 TIMER_STATUS_TIMIL6,
116 TIMER_STATUS_TIMIL7,
117 # if (MAX_BLACKFIN_GPTIMERS > 8)
118 TIMER_STATUS_TIMIL8,
119 TIMER_STATUS_TIMIL9,
120 TIMER_STATUS_TIMIL10,
121 # if (MAX_BLACKFIN_GPTIMERS > 11)
122 TIMER_STATUS_TIMIL11,
123 # endif
124 # endif
125 #endif
128 void set_gptimer_pwidth(unsigned int timer_id, uint32_t value)
130 tassert(timer_id < MAX_BLACKFIN_GPTIMERS);
131 bfin_write(&timer_regs[timer_id]->width, value);
132 SSYNC();
134 EXPORT_SYMBOL(set_gptimer_pwidth);
136 uint32_t get_gptimer_pwidth(unsigned int timer_id)
138 tassert(timer_id < MAX_BLACKFIN_GPTIMERS);
139 return bfin_read(&timer_regs[timer_id]->width);
141 EXPORT_SYMBOL(get_gptimer_pwidth);
143 void set_gptimer_period(unsigned int timer_id, uint32_t period)
145 tassert(timer_id < MAX_BLACKFIN_GPTIMERS);
146 bfin_write(&timer_regs[timer_id]->period, period);
147 SSYNC();
149 EXPORT_SYMBOL(set_gptimer_period);
151 uint32_t get_gptimer_period(unsigned int timer_id)
153 tassert(timer_id < MAX_BLACKFIN_GPTIMERS);
154 return bfin_read(&timer_regs[timer_id]->period);
156 EXPORT_SYMBOL(get_gptimer_period);
158 uint32_t get_gptimer_count(unsigned int timer_id)
160 tassert(timer_id < MAX_BLACKFIN_GPTIMERS);
161 return bfin_read(&timer_regs[timer_id]->counter);
163 EXPORT_SYMBOL(get_gptimer_count);
165 #ifdef CONFIG_BF60x
166 void set_gptimer_delay(unsigned int timer_id, uint32_t delay)
168 tassert(timer_id < MAX_BLACKFIN_GPTIMERS);
169 bfin_write(&timer_regs[timer_id]->delay, delay);
170 SSYNC();
172 EXPORT_SYMBOL(set_gptimer_delay);
174 uint32_t get_gptimer_delay(unsigned int timer_id)
176 tassert(timer_id < MAX_BLACKFIN_GPTIMERS);
177 return bfin_read(&timer_regs[timer_id]->delay);
179 EXPORT_SYMBOL(get_gptimer_delay);
180 #endif
182 #ifdef CONFIG_BF60x
183 int get_gptimer_intr(unsigned int timer_id)
185 tassert(timer_id < MAX_BLACKFIN_GPTIMERS);
186 return !!(bfin_read(&group_regs[BFIN_TIMER_OCTET(timer_id)]->data_ilat) & timil_mask[timer_id]);
188 EXPORT_SYMBOL(get_gptimer_intr);
190 void clear_gptimer_intr(unsigned int timer_id)
192 tassert(timer_id < MAX_BLACKFIN_GPTIMERS);
193 bfin_write(&group_regs[BFIN_TIMER_OCTET(timer_id)]->data_ilat, timil_mask[timer_id]);
195 EXPORT_SYMBOL(clear_gptimer_intr);
197 int get_gptimer_over(unsigned int timer_id)
199 tassert(timer_id < MAX_BLACKFIN_GPTIMERS);
200 return !!(bfin_read(&group_regs[BFIN_TIMER_OCTET(timer_id)]->stat_ilat) & tovf_mask[timer_id]);
202 EXPORT_SYMBOL(get_gptimer_over);
204 void clear_gptimer_over(unsigned int timer_id)
206 tassert(timer_id < MAX_BLACKFIN_GPTIMERS);
207 bfin_write(&group_regs[BFIN_TIMER_OCTET(timer_id)]->stat_ilat, tovf_mask[timer_id]);
209 EXPORT_SYMBOL(clear_gptimer_over);
211 int get_gptimer_run(unsigned int timer_id)
213 tassert(timer_id < MAX_BLACKFIN_GPTIMERS);
214 return !!(bfin_read(&group_regs[BFIN_TIMER_OCTET(timer_id)]->run) & trun_mask[timer_id]);
216 EXPORT_SYMBOL(get_gptimer_run);
218 uint32_t get_gptimer_status(unsigned int group)
220 tassert(group < BFIN_TIMER_NUM_GROUP);
221 return bfin_read(&group_regs[group]->data_ilat);
223 EXPORT_SYMBOL(get_gptimer_status);
225 void set_gptimer_status(unsigned int group, uint32_t value)
227 tassert(group < BFIN_TIMER_NUM_GROUP);
228 bfin_write(&group_regs[group]->data_ilat, value);
229 SSYNC();
231 EXPORT_SYMBOL(set_gptimer_status);
232 #else
233 uint32_t get_gptimer_status(unsigned int group)
235 tassert(group < BFIN_TIMER_NUM_GROUP);
236 return bfin_read(&group_regs[group]->status);
238 EXPORT_SYMBOL(get_gptimer_status);
240 void set_gptimer_status(unsigned int group, uint32_t value)
242 tassert(group < BFIN_TIMER_NUM_GROUP);
243 bfin_write(&group_regs[group]->status, value);
244 SSYNC();
246 EXPORT_SYMBOL(set_gptimer_status);
248 static uint32_t read_gptimer_status(unsigned int timer_id)
250 return bfin_read(&group_regs[BFIN_TIMER_OCTET(timer_id)]->status);
253 int get_gptimer_intr(unsigned int timer_id)
255 tassert(timer_id < MAX_BLACKFIN_GPTIMERS);
256 return !!(read_gptimer_status(timer_id) & timil_mask[timer_id]);
258 EXPORT_SYMBOL(get_gptimer_intr);
260 void clear_gptimer_intr(unsigned int timer_id)
262 tassert(timer_id < MAX_BLACKFIN_GPTIMERS);
263 bfin_write(&group_regs[BFIN_TIMER_OCTET(timer_id)]->status, timil_mask[timer_id]);
265 EXPORT_SYMBOL(clear_gptimer_intr);
267 int get_gptimer_over(unsigned int timer_id)
269 tassert(timer_id < MAX_BLACKFIN_GPTIMERS);
270 return !!(read_gptimer_status(timer_id) & tovf_mask[timer_id]);
272 EXPORT_SYMBOL(get_gptimer_over);
274 void clear_gptimer_over(unsigned int timer_id)
276 tassert(timer_id < MAX_BLACKFIN_GPTIMERS);
277 bfin_write(&group_regs[BFIN_TIMER_OCTET(timer_id)]->status, tovf_mask[timer_id]);
279 EXPORT_SYMBOL(clear_gptimer_over);
281 int get_gptimer_run(unsigned int timer_id)
283 tassert(timer_id < MAX_BLACKFIN_GPTIMERS);
284 return !!(read_gptimer_status(timer_id) & trun_mask[timer_id]);
286 EXPORT_SYMBOL(get_gptimer_run);
287 #endif
289 void set_gptimer_config(unsigned int timer_id, uint16_t config)
291 tassert(timer_id < MAX_BLACKFIN_GPTIMERS);
292 bfin_write(&timer_regs[timer_id]->config, config);
293 SSYNC();
295 EXPORT_SYMBOL(set_gptimer_config);
297 uint16_t get_gptimer_config(unsigned int timer_id)
299 tassert(timer_id < MAX_BLACKFIN_GPTIMERS);
300 return bfin_read(&timer_regs[timer_id]->config);
302 EXPORT_SYMBOL(get_gptimer_config);
304 void enable_gptimers(uint16_t mask)
306 int i;
307 #ifdef CONFIG_BF60x
308 uint16_t imask;
309 imask = bfin_read16(TIMER_DATA_IMSK);
310 imask &= ~mask;
311 bfin_write16(TIMER_DATA_IMSK, imask);
312 #endif
313 tassert((mask & ~BLACKFIN_GPTIMER_IDMASK) == 0);
314 for (i = 0; i < BFIN_TIMER_NUM_GROUP; ++i) {
315 bfin_write(&group_regs[i]->enable, mask & 0xFF);
316 mask >>= 8;
318 SSYNC();
320 EXPORT_SYMBOL(enable_gptimers);
322 static void _disable_gptimers(uint16_t mask)
324 int i;
325 uint16_t m = mask;
326 tassert((mask & ~BLACKFIN_GPTIMER_IDMASK) == 0);
327 for (i = 0; i < BFIN_TIMER_NUM_GROUP; ++i) {
328 bfin_write(&group_regs[i]->disable, m & 0xFF);
329 m >>= 8;
333 void disable_gptimers(uint16_t mask)
335 #ifndef CONFIG_BF60x
336 int i;
337 _disable_gptimers(mask);
338 for (i = 0; i < MAX_BLACKFIN_GPTIMERS; ++i)
339 if (mask & (1 << i))
340 bfin_write(&group_regs[BFIN_TIMER_OCTET(i)]->status, trun_mask[i]);
341 SSYNC();
342 #else
343 _disable_gptimers(mask);
344 #endif
346 EXPORT_SYMBOL(disable_gptimers);
348 void disable_gptimers_sync(uint16_t mask)
350 _disable_gptimers(mask);
351 SSYNC();
353 EXPORT_SYMBOL(disable_gptimers_sync);
355 void set_gptimer_pulse_hi(unsigned int timer_id)
357 tassert(timer_id < MAX_BLACKFIN_GPTIMERS);
358 bfin_write_or(&timer_regs[timer_id]->config, TIMER_PULSE_HI);
359 SSYNC();
361 EXPORT_SYMBOL(set_gptimer_pulse_hi);
363 void clear_gptimer_pulse_hi(unsigned int timer_id)
365 tassert(timer_id < MAX_BLACKFIN_GPTIMERS);
366 bfin_write_and(&timer_regs[timer_id]->config, ~TIMER_PULSE_HI);
367 SSYNC();
369 EXPORT_SYMBOL(clear_gptimer_pulse_hi);
371 uint16_t get_enabled_gptimers(void)
373 int i;
374 uint16_t result = 0;
375 for (i = 0; i < BFIN_TIMER_NUM_GROUP; ++i)
376 result |= (bfin_read(&group_regs[i]->enable) << (i << 3));
377 return result;
379 EXPORT_SYMBOL(get_enabled_gptimers);
381 MODULE_AUTHOR("Axel Weiss (awe@aglaia-gmbh.de)");
382 MODULE_DESCRIPTION("Blackfin General Purpose Timers API");
383 MODULE_LICENSE("GPL");