1 /*
2  * gptimers.c - Blackfin General Purpose Timer core API
3  *
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)
7  *
8  * Licensed under the GPLv2.
9  */
10 
11 #include <linux/kernel.h>
12 #include <linux/module.h>
13 #include <linux/io.h>
14 
15 #include <asm/blackfin.h>
16 #include <asm/gptimers.h>
17 
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
25 
26 #define BFIN_TIMER_NUM_GROUP  (BFIN_TIMER_OCTET(MAX_BLACKFIN_GPTIMERS - 1) + 1)
27 
28 static struct bfin_gptimer_regs * const timer_regs[MAX_BLACKFIN_GPTIMERS] =
29 {
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,
45 #  endif
46 # endif
47 #endif
48 };
49 
50 static struct bfin_gptimer_group_regs * const group_regs[BFIN_TIMER_NUM_GROUP] =
51 {
52 	(void *)TIMER0_GROUP_REG,
53 #if (MAX_BLACKFIN_GPTIMERS > 8)
54 	(void *)TIMER8_GROUP_REG,
55 #endif
56 };
57 
58 static uint32_t const trun_mask[MAX_BLACKFIN_GPTIMERS] =
59 {
60 	TIMER_STATUS_TRUN0,
61 	TIMER_STATUS_TRUN1,
62 	TIMER_STATUS_TRUN2,
63 #if (MAX_BLACKFIN_GPTIMERS > 3)
64 	TIMER_STATUS_TRUN3,
65 	TIMER_STATUS_TRUN4,
66 	TIMER_STATUS_TRUN5,
67 	TIMER_STATUS_TRUN6,
68 	TIMER_STATUS_TRUN7,
69 # if (MAX_BLACKFIN_GPTIMERS > 8)
70 	TIMER_STATUS_TRUN8,
71 	TIMER_STATUS_TRUN9,
72 	TIMER_STATUS_TRUN10,
73 #  if (MAX_BLACKFIN_GPTIMERS > 11)
74 	TIMER_STATUS_TRUN11,
75 #  endif
76 # endif
77 #endif
78 };
79 
80 static uint32_t const tovf_mask[MAX_BLACKFIN_GPTIMERS] =
81 {
82 	TIMER_STATUS_TOVF0,
83 	TIMER_STATUS_TOVF1,
84 	TIMER_STATUS_TOVF2,
85 #if (MAX_BLACKFIN_GPTIMERS > 3)
86 	TIMER_STATUS_TOVF3,
87 	TIMER_STATUS_TOVF4,
88 	TIMER_STATUS_TOVF5,
89 	TIMER_STATUS_TOVF6,
90 	TIMER_STATUS_TOVF7,
91 # if (MAX_BLACKFIN_GPTIMERS > 8)
92 	TIMER_STATUS_TOVF8,
93 	TIMER_STATUS_TOVF9,
94 	TIMER_STATUS_TOVF10,
95 #  if (MAX_BLACKFIN_GPTIMERS > 11)
96 	TIMER_STATUS_TOVF11,
97 #  endif
98 # endif
99 #endif
100 };
101 
102 static uint32_t const timil_mask[MAX_BLACKFIN_GPTIMERS] =
103 {
104 	TIMER_STATUS_TIMIL0,
105 	TIMER_STATUS_TIMIL1,
106 	TIMER_STATUS_TIMIL2,
107 #if (MAX_BLACKFIN_GPTIMERS > 3)
108 	TIMER_STATUS_TIMIL3,
109 	TIMER_STATUS_TIMIL4,
110 	TIMER_STATUS_TIMIL5,
111 	TIMER_STATUS_TIMIL6,
112 	TIMER_STATUS_TIMIL7,
113 # if (MAX_BLACKFIN_GPTIMERS > 8)
114 	TIMER_STATUS_TIMIL8,
115 	TIMER_STATUS_TIMIL9,
116 	TIMER_STATUS_TIMIL10,
117 #  if (MAX_BLACKFIN_GPTIMERS > 11)
118 	TIMER_STATUS_TIMIL11,
119 #  endif
120 # endif
121 #endif
122 };
123 
set_gptimer_pwidth(unsigned int timer_id,uint32_t value)124 void set_gptimer_pwidth(unsigned int timer_id, uint32_t value)
125 {
126 	tassert(timer_id < MAX_BLACKFIN_GPTIMERS);
127 	bfin_write(&timer_regs[timer_id]->width, value);
128 	SSYNC();
129 }
130 EXPORT_SYMBOL(set_gptimer_pwidth);
131 
get_gptimer_pwidth(unsigned int timer_id)132 uint32_t get_gptimer_pwidth(unsigned int timer_id)
133 {
134 	tassert(timer_id < MAX_BLACKFIN_GPTIMERS);
135 	return bfin_read(&timer_regs[timer_id]->width);
136 }
137 EXPORT_SYMBOL(get_gptimer_pwidth);
138 
set_gptimer_period(unsigned int timer_id,uint32_t period)139 void set_gptimer_period(unsigned int timer_id, uint32_t period)
140 {
141 	tassert(timer_id < MAX_BLACKFIN_GPTIMERS);
142 	bfin_write(&timer_regs[timer_id]->period, period);
143 	SSYNC();
144 }
145 EXPORT_SYMBOL(set_gptimer_period);
146 
get_gptimer_period(unsigned int timer_id)147 uint32_t get_gptimer_period(unsigned int timer_id)
148 {
149 	tassert(timer_id < MAX_BLACKFIN_GPTIMERS);
150 	return bfin_read(&timer_regs[timer_id]->period);
151 }
152 EXPORT_SYMBOL(get_gptimer_period);
153 
get_gptimer_count(unsigned int timer_id)154 uint32_t get_gptimer_count(unsigned int timer_id)
155 {
156 	tassert(timer_id < MAX_BLACKFIN_GPTIMERS);
157 	return bfin_read(&timer_regs[timer_id]->counter);
158 }
159 EXPORT_SYMBOL(get_gptimer_count);
160 
get_gptimer_status(unsigned int group)161 uint32_t get_gptimer_status(unsigned int group)
162 {
163 	tassert(group < BFIN_TIMER_NUM_GROUP);
164 	return bfin_read(&group_regs[group]->status);
165 }
166 EXPORT_SYMBOL(get_gptimer_status);
167 
set_gptimer_status(unsigned int group,uint32_t value)168 void set_gptimer_status(unsigned int group, uint32_t value)
169 {
170 	tassert(group < BFIN_TIMER_NUM_GROUP);
171 	bfin_write(&group_regs[group]->status, value);
172 	SSYNC();
173 }
174 EXPORT_SYMBOL(set_gptimer_status);
175 
read_gptimer_status(unsigned int timer_id)176 static uint32_t read_gptimer_status(unsigned int timer_id)
177 {
178 	return bfin_read(&group_regs[BFIN_TIMER_OCTET(timer_id)]->status);
179 }
180 
get_gptimer_intr(unsigned int timer_id)181 int get_gptimer_intr(unsigned int timer_id)
182 {
183 	tassert(timer_id < MAX_BLACKFIN_GPTIMERS);
184 	return !!(read_gptimer_status(timer_id) & timil_mask[timer_id]);
185 }
186 EXPORT_SYMBOL(get_gptimer_intr);
187 
clear_gptimer_intr(unsigned int timer_id)188 void clear_gptimer_intr(unsigned int timer_id)
189 {
190 	tassert(timer_id < MAX_BLACKFIN_GPTIMERS);
191 	bfin_write(&group_regs[BFIN_TIMER_OCTET(timer_id)]->status, timil_mask[timer_id]);
192 }
193 EXPORT_SYMBOL(clear_gptimer_intr);
194 
get_gptimer_over(unsigned int timer_id)195 int get_gptimer_over(unsigned int timer_id)
196 {
197 	tassert(timer_id < MAX_BLACKFIN_GPTIMERS);
198 	return !!(read_gptimer_status(timer_id) & tovf_mask[timer_id]);
199 }
200 EXPORT_SYMBOL(get_gptimer_over);
201 
clear_gptimer_over(unsigned int timer_id)202 void clear_gptimer_over(unsigned int timer_id)
203 {
204 	tassert(timer_id < MAX_BLACKFIN_GPTIMERS);
205 	bfin_write(&group_regs[BFIN_TIMER_OCTET(timer_id)]->status, tovf_mask[timer_id]);
206 }
207 EXPORT_SYMBOL(clear_gptimer_over);
208 
get_gptimer_run(unsigned int timer_id)209 int get_gptimer_run(unsigned int timer_id)
210 {
211 	tassert(timer_id < MAX_BLACKFIN_GPTIMERS);
212 	return !!(read_gptimer_status(timer_id) & trun_mask[timer_id]);
213 }
214 EXPORT_SYMBOL(get_gptimer_run);
215 
set_gptimer_config(unsigned int timer_id,uint16_t config)216 void set_gptimer_config(unsigned int timer_id, uint16_t config)
217 {
218 	tassert(timer_id < MAX_BLACKFIN_GPTIMERS);
219 	bfin_write(&timer_regs[timer_id]->config, config);
220 	SSYNC();
221 }
222 EXPORT_SYMBOL(set_gptimer_config);
223 
get_gptimer_config(unsigned int timer_id)224 uint16_t get_gptimer_config(unsigned int timer_id)
225 {
226 	tassert(timer_id < MAX_BLACKFIN_GPTIMERS);
227 	return bfin_read(&timer_regs[timer_id]->config);
228 }
229 EXPORT_SYMBOL(get_gptimer_config);
230 
enable_gptimers(uint16_t mask)231 void enable_gptimers(uint16_t mask)
232 {
233 	int i;
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);
237 		mask >>= 8;
238 	}
239 	SSYNC();
240 }
241 EXPORT_SYMBOL(enable_gptimers);
242 
_disable_gptimers(uint16_t mask)243 static void _disable_gptimers(uint16_t mask)
244 {
245 	int i;
246 	uint16_t m = 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);
250 		m >>= 8;
251 	}
252 }
253 
disable_gptimers(uint16_t mask)254 void disable_gptimers(uint16_t mask)
255 {
256 	int i;
257 	_disable_gptimers(mask);
258 	for (i = 0; i < MAX_BLACKFIN_GPTIMERS; ++i)
259 		if (mask & (1 << i))
260 			bfin_write(&group_regs[BFIN_TIMER_OCTET(i)]->status, trun_mask[i]);
261 	SSYNC();
262 }
263 EXPORT_SYMBOL(disable_gptimers);
264 
disable_gptimers_sync(uint16_t mask)265 void disable_gptimers_sync(uint16_t mask)
266 {
267 	_disable_gptimers(mask);
268 	SSYNC();
269 }
270 EXPORT_SYMBOL(disable_gptimers_sync);
271 
set_gptimer_pulse_hi(unsigned int timer_id)272 void set_gptimer_pulse_hi(unsigned int timer_id)
273 {
274 	tassert(timer_id < MAX_BLACKFIN_GPTIMERS);
275 	bfin_write_or(&timer_regs[timer_id]->config, TIMER_PULSE_HI);
276 	SSYNC();
277 }
278 EXPORT_SYMBOL(set_gptimer_pulse_hi);
279 
clear_gptimer_pulse_hi(unsigned int timer_id)280 void clear_gptimer_pulse_hi(unsigned int timer_id)
281 {
282 	tassert(timer_id < MAX_BLACKFIN_GPTIMERS);
283 	bfin_write_and(&timer_regs[timer_id]->config, ~TIMER_PULSE_HI);
284 	SSYNC();
285 }
286 EXPORT_SYMBOL(clear_gptimer_pulse_hi);
287 
get_enabled_gptimers(void)288 uint16_t get_enabled_gptimers(void)
289 {
290 	int i;
291 	uint16_t result = 0;
292 	for (i = 0; i < BFIN_TIMER_NUM_GROUP; ++i)
293 		result |= (bfin_read(&group_regs[i]->enable) << (i << 3));
294 	return result;
295 }
296 EXPORT_SYMBOL(get_enabled_gptimers);
297 
298 MODULE_AUTHOR("Axel Weiss (awe@aglaia-gmbh.de)");
299 MODULE_DESCRIPTION("Blackfin General Purpose Timers API");
300 MODULE_LICENSE("GPL");
301