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.
11 #include <linux/kernel.h>
12 #include <linux/module.h>
15 #include <asm/blackfin.h>
16 #include <asm/gptimers.h>
19 # define tassert(expr)
21 # define tassert(expr) \
23 printk(KERN_DEBUG "%s:%s:%i: Assertion failed: " #expr "\n", __FILE__, __func__, __LINE__);
26 #define BFIN_TIMER_NUM_GROUP (BFIN_TIMER_OCTET(MAX_BLACKFIN_GPTIMERS - 1) + 1)
28 static struct bfin_gptimer_regs * const timer_regs[MAX_BLACKFIN_GPTIMERS] =
30 (void *)TIMER0_CONFIG,
31 (void *)TIMER1_CONFIG,
32 (void *)TIMER2_CONFIG,
33 #if (MAX_BLACKFIN_GPTIMERS > 3)
34 (void *)TIMER3_CONFIG,
35 (void *)TIMER4_CONFIG,
36 (void *)TIMER5_CONFIG,
37 (void *)TIMER6_CONFIG,
38 (void *)TIMER7_CONFIG,
39 # if (MAX_BLACKFIN_GPTIMERS > 8)
40 (void *)TIMER8_CONFIG,
41 (void *)TIMER9_CONFIG,
42 (void *)TIMER10_CONFIG,
43 # if (MAX_BLACKFIN_GPTIMERS > 11)
44 (void *)TIMER11_CONFIG,
50 static struct bfin_gptimer_group_regs * const group_regs[BFIN_TIMER_NUM_GROUP] =
52 (void *)TIMER0_GROUP_REG,
53 #if (MAX_BLACKFIN_GPTIMERS > 8)
54 (void *)TIMER8_GROUP_REG,
58 static uint32_t const trun_mask[MAX_BLACKFIN_GPTIMERS] =
63 #if (MAX_BLACKFIN_GPTIMERS > 3)
69 # if (MAX_BLACKFIN_GPTIMERS > 8)
73 # if (MAX_BLACKFIN_GPTIMERS > 11)
80 static uint32_t const tovf_mask[MAX_BLACKFIN_GPTIMERS] =
85 #if (MAX_BLACKFIN_GPTIMERS > 3)
91 # if (MAX_BLACKFIN_GPTIMERS > 8)
95 # if (MAX_BLACKFIN_GPTIMERS > 11)
102 static uint32_t const timil_mask[MAX_BLACKFIN_GPTIMERS] =
107 #if (MAX_BLACKFIN_GPTIMERS > 3)
113 # if (MAX_BLACKFIN_GPTIMERS > 8)
116 TIMER_STATUS_TIMIL10,
117 # if (MAX_BLACKFIN_GPTIMERS > 11)
118 TIMER_STATUS_TIMIL11,
124 void set_gptimer_pwidth(unsigned int timer_id, uint32_t value)
126 tassert(timer_id < MAX_BLACKFIN_GPTIMERS);
127 bfin_write(&timer_regs[timer_id]->width, value);
130 EXPORT_SYMBOL(set_gptimer_pwidth);
132 uint32_t get_gptimer_pwidth(unsigned int timer_id)
134 tassert(timer_id < MAX_BLACKFIN_GPTIMERS);
135 return bfin_read(&timer_regs[timer_id]->width);
137 EXPORT_SYMBOL(get_gptimer_pwidth);
139 void set_gptimer_period(unsigned int timer_id, uint32_t period)
141 tassert(timer_id < MAX_BLACKFIN_GPTIMERS);
142 bfin_write(&timer_regs[timer_id]->period, period);
145 EXPORT_SYMBOL(set_gptimer_period);
147 uint32_t get_gptimer_period(unsigned int timer_id)
149 tassert(timer_id < MAX_BLACKFIN_GPTIMERS);
150 return bfin_read(&timer_regs[timer_id]->period);
152 EXPORT_SYMBOL(get_gptimer_period);
154 uint32_t get_gptimer_count(unsigned int timer_id)
156 tassert(timer_id < MAX_BLACKFIN_GPTIMERS);
157 return bfin_read(&timer_regs[timer_id]->counter);
159 EXPORT_SYMBOL(get_gptimer_count);
161 uint32_t get_gptimer_status(unsigned int group)
163 tassert(group < BFIN_TIMER_NUM_GROUP);
164 return bfin_read(&group_regs[group]->status);
166 EXPORT_SYMBOL(get_gptimer_status);
168 void set_gptimer_status(unsigned int group, uint32_t value)
170 tassert(group < BFIN_TIMER_NUM_GROUP);
171 bfin_write(&group_regs[group]->status, value);
174 EXPORT_SYMBOL(set_gptimer_status);
176 static uint32_t read_gptimer_status(unsigned int timer_id)
178 return bfin_read(&group_regs[BFIN_TIMER_OCTET(timer_id)]->status);
181 int get_gptimer_intr(unsigned int timer_id)
183 tassert(timer_id < MAX_BLACKFIN_GPTIMERS);
184 return !!(read_gptimer_status(timer_id) & timil_mask[timer_id]);
186 EXPORT_SYMBOL(get_gptimer_intr);
188 void clear_gptimer_intr(unsigned int timer_id)
190 tassert(timer_id < MAX_BLACKFIN_GPTIMERS);
191 bfin_write(&group_regs[BFIN_TIMER_OCTET(timer_id)]->status, timil_mask[timer_id]);
193 EXPORT_SYMBOL(clear_gptimer_intr);
195 int get_gptimer_over(unsigned int timer_id)
197 tassert(timer_id < MAX_BLACKFIN_GPTIMERS);
198 return !!(read_gptimer_status(timer_id) & tovf_mask[timer_id]);
200 EXPORT_SYMBOL(get_gptimer_over);
202 void clear_gptimer_over(unsigned int timer_id)
204 tassert(timer_id < MAX_BLACKFIN_GPTIMERS);
205 bfin_write(&group_regs[BFIN_TIMER_OCTET(timer_id)]->status, tovf_mask[timer_id]);
207 EXPORT_SYMBOL(clear_gptimer_over);
209 int get_gptimer_run(unsigned int timer_id)
211 tassert(timer_id < MAX_BLACKFIN_GPTIMERS);
212 return !!(read_gptimer_status(timer_id) & trun_mask[timer_id]);
214 EXPORT_SYMBOL(get_gptimer_run);
216 void set_gptimer_config(unsigned int timer_id, uint16_t config)
218 tassert(timer_id < MAX_BLACKFIN_GPTIMERS);
219 bfin_write(&timer_regs[timer_id]->config, config);
222 EXPORT_SYMBOL(set_gptimer_config);
224 uint16_t get_gptimer_config(unsigned int timer_id)
226 tassert(timer_id < MAX_BLACKFIN_GPTIMERS);
227 return bfin_read(&timer_regs[timer_id]->config);
229 EXPORT_SYMBOL(get_gptimer_config);
231 void enable_gptimers(uint16_t mask)
234 tassert((mask & ~BLACKFIN_GPTIMER_IDMASK) == 0);
235 for (i = 0; i < BFIN_TIMER_NUM_GROUP; ++i) {
236 bfin_write(&group_regs[i]->enable, mask & 0xFF);
241 EXPORT_SYMBOL(enable_gptimers);
243 static void _disable_gptimers(uint16_t mask)
247 tassert((mask & ~BLACKFIN_GPTIMER_IDMASK) == 0);
248 for (i = 0; i < BFIN_TIMER_NUM_GROUP; ++i) {
249 bfin_write(&group_regs[i]->disable, m & 0xFF);
254 void disable_gptimers(uint16_t mask)
257 _disable_gptimers(mask);
258 for (i = 0; i < MAX_BLACKFIN_GPTIMERS; ++i)
260 bfin_write(&group_regs[BFIN_TIMER_OCTET(i)]->status, trun_mask[i]);
263 EXPORT_SYMBOL(disable_gptimers);
265 void disable_gptimers_sync(uint16_t mask)
267 _disable_gptimers(mask);
270 EXPORT_SYMBOL(disable_gptimers_sync);
272 void set_gptimer_pulse_hi(unsigned int timer_id)
274 tassert(timer_id < MAX_BLACKFIN_GPTIMERS);
275 bfin_write_or(&timer_regs[timer_id]->config, TIMER_PULSE_HI);
278 EXPORT_SYMBOL(set_gptimer_pulse_hi);
280 void clear_gptimer_pulse_hi(unsigned int timer_id)
282 tassert(timer_id < MAX_BLACKFIN_GPTIMERS);
283 bfin_write_and(&timer_regs[timer_id]->config, ~TIMER_PULSE_HI);
286 EXPORT_SYMBOL(clear_gptimer_pulse_hi);
288 uint16_t get_enabled_gptimers(void)
292 for (i = 0; i < BFIN_TIMER_NUM_GROUP; ++i)
293 result |= (bfin_read(&group_regs[i]->enable) << (i << 3));
296 EXPORT_SYMBOL(get_enabled_gptimers);
298 MODULE_AUTHOR("Axel Weiss (awe@aglaia-gmbh.de)");
299 MODULE_DESCRIPTION("Blackfin General Purpose Timers API");
300 MODULE_LICENSE("GPL");