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)
44 static volatile GPTIMER_timer_regs *const timer_regs[MAX_BLACKFIN_GPTIMERS] =
46 (GPTIMER_timer_regs *)TIMER0_CONFIG,
47 (GPTIMER_timer_regs *)TIMER1_CONFIG,
48 (GPTIMER_timer_regs *)TIMER2_CONFIG,
49 #if (MAX_BLACKFIN_GPTIMERS > 3)
50 (GPTIMER_timer_regs *)TIMER3_CONFIG,
51 (GPTIMER_timer_regs *)TIMER4_CONFIG,
52 (GPTIMER_timer_regs *)TIMER5_CONFIG,
53 (GPTIMER_timer_regs *)TIMER6_CONFIG,
54 (GPTIMER_timer_regs *)TIMER7_CONFIG,
56 #if (MAX_BLACKFIN_GPTIMERS > 8)
57 (GPTIMER_timer_regs *)TIMER8_CONFIG,
58 (GPTIMER_timer_regs *)TIMER9_CONFIG,
59 (GPTIMER_timer_regs *)TIMER10_CONFIG,
60 (GPTIMER_timer_regs *)TIMER11_CONFIG,
64 static volatile GPTIMER_group_regs *const group_regs[BFIN_TIMER_NUM_GROUP] =
66 (GPTIMER_group_regs *)TIMER0_GROUP_REG,
67 #if (MAX_BLACKFIN_GPTIMERS > 8)
68 (GPTIMER_group_regs *)TIMER8_GROUP_REG,
72 static uint32_t const trun_mask[MAX_BLACKFIN_GPTIMERS] =
77 #if (MAX_BLACKFIN_GPTIMERS > 3)
84 #if (MAX_BLACKFIN_GPTIMERS > 8)
92 static uint32_t const tovf_mask[MAX_BLACKFIN_GPTIMERS] =
97 #if (MAX_BLACKFIN_GPTIMERS > 3)
104 #if (MAX_BLACKFIN_GPTIMERS > 8)
112 static uint32_t const timil_mask[MAX_BLACKFIN_GPTIMERS] =
117 #if (MAX_BLACKFIN_GPTIMERS > 3)
124 #if (MAX_BLACKFIN_GPTIMERS > 8)
127 TIMER_STATUS_TIMIL10,
128 TIMER_STATUS_TIMIL11,
132 void set_gptimer_pwidth(int timer_id, uint32_t value)
134 tassert(timer_id < MAX_BLACKFIN_GPTIMERS);
135 timer_regs[timer_id]->width = value;
138 EXPORT_SYMBOL(set_gptimer_pwidth);
140 uint32_t get_gptimer_pwidth(int timer_id)
142 tassert(timer_id < MAX_BLACKFIN_GPTIMERS);
143 return timer_regs[timer_id]->width;
145 EXPORT_SYMBOL(get_gptimer_pwidth);
147 void set_gptimer_period(int timer_id, uint32_t period)
149 tassert(timer_id < MAX_BLACKFIN_GPTIMERS);
150 timer_regs[timer_id]->period = period;
153 EXPORT_SYMBOL(set_gptimer_period);
155 uint32_t get_gptimer_period(int timer_id)
157 tassert(timer_id < MAX_BLACKFIN_GPTIMERS);
158 return timer_regs[timer_id]->period;
160 EXPORT_SYMBOL(get_gptimer_period);
162 uint32_t get_gptimer_count(int timer_id)
164 tassert(timer_id < MAX_BLACKFIN_GPTIMERS);
165 return timer_regs[timer_id]->counter;
167 EXPORT_SYMBOL(get_gptimer_count);
169 uint32_t get_gptimer_status(int group)
171 tassert(group < BFIN_TIMER_NUM_GROUP);
172 return group_regs[group]->status;
174 EXPORT_SYMBOL(get_gptimer_status);
176 void set_gptimer_status(int group, uint32_t value)
178 tassert(group < BFIN_TIMER_NUM_GROUP);
179 group_regs[group]->status = value;
182 EXPORT_SYMBOL(set_gptimer_status);
184 uint16_t get_gptimer_intr(int timer_id)
186 tassert(timer_id < MAX_BLACKFIN_GPTIMERS);
187 return (group_regs[BFIN_TIMER_OCTET(timer_id)]->status & timil_mask[timer_id]) ? 1 : 0;
189 EXPORT_SYMBOL(get_gptimer_intr);
191 void clear_gptimer_intr(int timer_id)
193 tassert(timer_id < MAX_BLACKFIN_GPTIMERS);
194 group_regs[BFIN_TIMER_OCTET(timer_id)]->status = timil_mask[timer_id];
196 EXPORT_SYMBOL(clear_gptimer_intr);
198 uint16_t get_gptimer_over(int timer_id)
200 tassert(timer_id < MAX_BLACKFIN_GPTIMERS);
201 return (group_regs[BFIN_TIMER_OCTET(timer_id)]->status & tovf_mask[timer_id]) ? 1 : 0;
203 EXPORT_SYMBOL(get_gptimer_over);
205 void clear_gptimer_over(int timer_id)
207 tassert(timer_id < MAX_BLACKFIN_GPTIMERS);
208 group_regs[BFIN_TIMER_OCTET(timer_id)]->status = tovf_mask[timer_id];
210 EXPORT_SYMBOL(clear_gptimer_over);
212 void set_gptimer_config(int timer_id, uint16_t config)
214 tassert(timer_id < MAX_BLACKFIN_GPTIMERS);
215 timer_regs[timer_id]->config = config;
218 EXPORT_SYMBOL(set_gptimer_config);
220 uint16_t get_gptimer_config(int timer_id)
222 tassert(timer_id < MAX_BLACKFIN_GPTIMERS);
223 return timer_regs[timer_id]->config;
225 EXPORT_SYMBOL(get_gptimer_config);
227 void enable_gptimers(uint16_t mask)
230 tassert((mask & ~BLACKFIN_GPTIMER_IDMASK) == 0);
231 for (i = 0; i < BFIN_TIMER_NUM_GROUP; ++i) {
232 group_regs[i]->enable = mask & 0xFF;
237 EXPORT_SYMBOL(enable_gptimers);
239 void disable_gptimers(uint16_t mask)
243 tassert((mask & ~BLACKFIN_GPTIMER_IDMASK) == 0);
244 for (i = 0; i < BFIN_TIMER_NUM_GROUP; ++i) {
245 group_regs[i]->disable = m & 0xFF;
248 for (i = 0; i < MAX_BLACKFIN_GPTIMERS; ++i)
250 group_regs[BFIN_TIMER_OCTET(i)]->status |= trun_mask[i];
253 EXPORT_SYMBOL(disable_gptimers);
255 void set_gptimer_pulse_hi(int timer_id)
257 tassert(timer_id < MAX_BLACKFIN_GPTIMERS);
258 timer_regs[timer_id]->config |= TIMER_PULSE_HI;
261 EXPORT_SYMBOL(set_gptimer_pulse_hi);
263 void clear_gptimer_pulse_hi(int timer_id)
265 tassert(timer_id < MAX_BLACKFIN_GPTIMERS);
266 timer_regs[timer_id]->config &= ~TIMER_PULSE_HI;
269 EXPORT_SYMBOL(clear_gptimer_pulse_hi);
271 uint16_t get_enabled_gptimers(void)
275 for (i = 0; i < BFIN_TIMER_NUM_GROUP; ++i)
276 result |= (group_regs[i]->enable << (i << 3));
279 EXPORT_SYMBOL(get_enabled_gptimers);
281 MODULE_AUTHOR("Axel Weiss (awe@aglaia-gmbh.de)");
282 MODULE_DESCRIPTION("Blackfin General Purpose Timers API");
283 MODULE_LICENSE("GPL");