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__);
27 # define BFIN_TIMER_NUM_GROUP (BFIN_TIMER_OCTET(MAX_BLACKFIN_GPTIMERS - 1) + 1)
29 # define BFIN_TIMER_NUM_GROUP 1
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,
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,
62 static uint32_t const trun_mask[MAX_BLACKFIN_GPTIMERS] =
67 #if (MAX_BLACKFIN_GPTIMERS > 3)
73 # if (MAX_BLACKFIN_GPTIMERS > 8)
77 # if (MAX_BLACKFIN_GPTIMERS > 11)
84 static uint32_t const tovf_mask[MAX_BLACKFIN_GPTIMERS] =
89 #if (MAX_BLACKFIN_GPTIMERS > 3)
95 # if (MAX_BLACKFIN_GPTIMERS > 8)
99 # if (MAX_BLACKFIN_GPTIMERS > 11)
106 static uint32_t const timil_mask[MAX_BLACKFIN_GPTIMERS] =
111 #if (MAX_BLACKFIN_GPTIMERS > 3)
117 # if (MAX_BLACKFIN_GPTIMERS > 8)
120 TIMER_STATUS_TIMIL10,
121 # if (MAX_BLACKFIN_GPTIMERS > 11)
122 TIMER_STATUS_TIMIL11,
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);
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);
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);
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);
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);
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);
231 EXPORT_SYMBOL(set_gptimer_status);
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);
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);
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);
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)
309 imask = bfin_read16(TIMER_DATA_IMSK);
311 bfin_write16(TIMER_DATA_IMSK, imask);
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);
320 EXPORT_SYMBOL(enable_gptimers);
322 static void _disable_gptimers(uint16_t 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);
333 void disable_gptimers(uint16_t mask)
337 _disable_gptimers(mask);
338 for (i = 0; i < MAX_BLACKFIN_GPTIMERS; ++i)
340 bfin_write(&group_regs[BFIN_TIMER_OCTET(i)]->status, trun_mask[i]);
343 _disable_gptimers(mask);
346 EXPORT_SYMBOL(disable_gptimers);
348 void disable_gptimers_sync(uint16_t mask)
350 _disable_gptimers(mask);
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);
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);
369 EXPORT_SYMBOL(clear_gptimer_pulse_hi);
371 uint16_t get_enabled_gptimers(void)
375 for (i = 0; i < BFIN_TIMER_NUM_GROUP; ++i)
376 result |= (bfin_read(&group_regs[i]->enable) << (i << 3));
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");