gptimers.c 7.2 KB
Newer Older
1
/*
2
 * gptimers.c - Blackfin General Purpose Timer core API
3
 *
4 5 6
 * Copyright (c) 2005-2008 Analog Devices Inc.
 * Copyright (C) 2005 John DeHority
 * Copyright (C) 2006 Hella Aglaia GmbH (awe@aglaia-gmbh.de)
7 8 9 10 11 12
 *
 * Licensed under the GPLv2.
 */

#include <linux/kernel.h>
#include <linux/module.h>
13
#include <linux/io.h>
14 15 16 17 18 19 20 21 22

#include <asm/blackfin.h>
#include <asm/gptimers.h>

#ifdef DEBUG
# define tassert(expr)
#else
# define tassert(expr) \
	if (!(expr)) \
23
		printk(KERN_DEBUG "%s:%s:%i: Assertion failed: " #expr "\n", __FILE__, __func__, __LINE__);
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
#endif

#define BFIN_TIMER_NUM_GROUP  (BFIN_TIMER_OCTET(MAX_BLACKFIN_GPTIMERS - 1) + 1)

typedef struct {
	uint16_t config;
	uint16_t __pad;
	uint32_t counter;
	uint32_t period;
	uint32_t width;
} GPTIMER_timer_regs;

typedef struct {
	uint16_t enable;
	uint16_t __pad0;
	uint16_t disable;
	uint16_t __pad1;
	uint32_t status;
} GPTIMER_group_regs;

static volatile GPTIMER_timer_regs *const timer_regs[MAX_BLACKFIN_GPTIMERS] =
{
	(GPTIMER_timer_regs *)TIMER0_CONFIG,
	(GPTIMER_timer_regs *)TIMER1_CONFIG,
	(GPTIMER_timer_regs *)TIMER2_CONFIG,
#if (MAX_BLACKFIN_GPTIMERS > 3)
	(GPTIMER_timer_regs *)TIMER3_CONFIG,
	(GPTIMER_timer_regs *)TIMER4_CONFIG,
	(GPTIMER_timer_regs *)TIMER5_CONFIG,
	(GPTIMER_timer_regs *)TIMER6_CONFIG,
	(GPTIMER_timer_regs *)TIMER7_CONFIG,
55
# if (MAX_BLACKFIN_GPTIMERS > 8)
56 57 58
	(GPTIMER_timer_regs *)TIMER8_CONFIG,
	(GPTIMER_timer_regs *)TIMER9_CONFIG,
	(GPTIMER_timer_regs *)TIMER10_CONFIG,
59
#  if (MAX_BLACKFIN_GPTIMERS > 11)
60
	(GPTIMER_timer_regs *)TIMER11_CONFIG,
61 62
#  endif
# endif
63 64 65 66 67 68 69 70 71 72 73
#endif
};

static volatile GPTIMER_group_regs *const group_regs[BFIN_TIMER_NUM_GROUP] =
{
	(GPTIMER_group_regs *)TIMER0_GROUP_REG,
#if (MAX_BLACKFIN_GPTIMERS > 8)
	(GPTIMER_group_regs *)TIMER8_GROUP_REG,
#endif
};

74
static uint32_t const trun_mask[MAX_BLACKFIN_GPTIMERS] =
75 76 77 78 79 80 81 82 83 84
{
	TIMER_STATUS_TRUN0,
	TIMER_STATUS_TRUN1,
	TIMER_STATUS_TRUN2,
#if (MAX_BLACKFIN_GPTIMERS > 3)
	TIMER_STATUS_TRUN3,
	TIMER_STATUS_TRUN4,
	TIMER_STATUS_TRUN5,
	TIMER_STATUS_TRUN6,
	TIMER_STATUS_TRUN7,
85
# if (MAX_BLACKFIN_GPTIMERS > 8)
86 87 88
	TIMER_STATUS_TRUN8,
	TIMER_STATUS_TRUN9,
	TIMER_STATUS_TRUN10,
89
#  if (MAX_BLACKFIN_GPTIMERS > 11)
90
	TIMER_STATUS_TRUN11,
91 92
#  endif
# endif
93 94 95
#endif
};

96 97 98 99 100 101 102 103 104 105 106
static uint32_t const tovf_mask[MAX_BLACKFIN_GPTIMERS] =
{
	TIMER_STATUS_TOVF0,
	TIMER_STATUS_TOVF1,
	TIMER_STATUS_TOVF2,
#if (MAX_BLACKFIN_GPTIMERS > 3)
	TIMER_STATUS_TOVF3,
	TIMER_STATUS_TOVF4,
	TIMER_STATUS_TOVF5,
	TIMER_STATUS_TOVF6,
	TIMER_STATUS_TOVF7,
107
# if (MAX_BLACKFIN_GPTIMERS > 8)
108 109 110
	TIMER_STATUS_TOVF8,
	TIMER_STATUS_TOVF9,
	TIMER_STATUS_TOVF10,
111
#  if (MAX_BLACKFIN_GPTIMERS > 11)
112
	TIMER_STATUS_TOVF11,
113 114
#  endif
# endif
115 116 117 118
#endif
};

static uint32_t const timil_mask[MAX_BLACKFIN_GPTIMERS] =
119 120 121 122 123 124 125 126 127 128
{
	TIMER_STATUS_TIMIL0,
	TIMER_STATUS_TIMIL1,
	TIMER_STATUS_TIMIL2,
#if (MAX_BLACKFIN_GPTIMERS > 3)
	TIMER_STATUS_TIMIL3,
	TIMER_STATUS_TIMIL4,
	TIMER_STATUS_TIMIL5,
	TIMER_STATUS_TIMIL6,
	TIMER_STATUS_TIMIL7,
129
# if (MAX_BLACKFIN_GPTIMERS > 8)
130 131 132
	TIMER_STATUS_TIMIL8,
	TIMER_STATUS_TIMIL9,
	TIMER_STATUS_TIMIL10,
133
#  if (MAX_BLACKFIN_GPTIMERS > 11)
134
	TIMER_STATUS_TIMIL11,
135 136
#  endif
# endif
137 138 139
#endif
};

140
void set_gptimer_pwidth(unsigned int timer_id, uint32_t value)
141 142 143 144 145 146 147
{
	tassert(timer_id < MAX_BLACKFIN_GPTIMERS);
	timer_regs[timer_id]->width = value;
	SSYNC();
}
EXPORT_SYMBOL(set_gptimer_pwidth);

148
uint32_t get_gptimer_pwidth(unsigned int timer_id)
149 150 151 152 153 154
{
	tassert(timer_id < MAX_BLACKFIN_GPTIMERS);
	return timer_regs[timer_id]->width;
}
EXPORT_SYMBOL(get_gptimer_pwidth);

155
void set_gptimer_period(unsigned int timer_id, uint32_t period)
156 157 158 159 160 161 162
{
	tassert(timer_id < MAX_BLACKFIN_GPTIMERS);
	timer_regs[timer_id]->period = period;
	SSYNC();
}
EXPORT_SYMBOL(set_gptimer_period);

163
uint32_t get_gptimer_period(unsigned int timer_id)
164 165 166 167 168 169
{
	tassert(timer_id < MAX_BLACKFIN_GPTIMERS);
	return timer_regs[timer_id]->period;
}
EXPORT_SYMBOL(get_gptimer_period);

170
uint32_t get_gptimer_count(unsigned int timer_id)
171 172 173 174 175 176
{
	tassert(timer_id < MAX_BLACKFIN_GPTIMERS);
	return timer_regs[timer_id]->counter;
}
EXPORT_SYMBOL(get_gptimer_count);

177
uint32_t get_gptimer_status(unsigned int group)
178 179 180 181 182 183
{
	tassert(group < BFIN_TIMER_NUM_GROUP);
	return group_regs[group]->status;
}
EXPORT_SYMBOL(get_gptimer_status);

184
void set_gptimer_status(unsigned int group, uint32_t value)
185 186 187 188 189 190 191
{
	tassert(group < BFIN_TIMER_NUM_GROUP);
	group_regs[group]->status = value;
	SSYNC();
}
EXPORT_SYMBOL(set_gptimer_status);

192
int get_gptimer_intr(unsigned int timer_id)
193 194
{
	tassert(timer_id < MAX_BLACKFIN_GPTIMERS);
195
	return !!(group_regs[BFIN_TIMER_OCTET(timer_id)]->status & timil_mask[timer_id]);
196 197 198
}
EXPORT_SYMBOL(get_gptimer_intr);

199
void clear_gptimer_intr(unsigned int timer_id)
200 201
{
	tassert(timer_id < MAX_BLACKFIN_GPTIMERS);
202
	group_regs[BFIN_TIMER_OCTET(timer_id)]->status = timil_mask[timer_id];
203 204 205
}
EXPORT_SYMBOL(clear_gptimer_intr);

206
int get_gptimer_over(unsigned int timer_id)
207 208
{
	tassert(timer_id < MAX_BLACKFIN_GPTIMERS);
209
	return !!(group_regs[BFIN_TIMER_OCTET(timer_id)]->status & tovf_mask[timer_id]);
210 211 212
}
EXPORT_SYMBOL(get_gptimer_over);

213
void clear_gptimer_over(unsigned int timer_id)
214 215 216 217 218 219
{
	tassert(timer_id < MAX_BLACKFIN_GPTIMERS);
	group_regs[BFIN_TIMER_OCTET(timer_id)]->status = tovf_mask[timer_id];
}
EXPORT_SYMBOL(clear_gptimer_over);

220
int get_gptimer_run(unsigned int timer_id)
221 222 223 224 225 226
{
	tassert(timer_id < MAX_BLACKFIN_GPTIMERS);
	return !!(group_regs[BFIN_TIMER_OCTET(timer_id)]->status & trun_mask[timer_id]);
}
EXPORT_SYMBOL(get_gptimer_run);

227
void set_gptimer_config(unsigned int timer_id, uint16_t config)
228 229 230 231 232 233 234
{
	tassert(timer_id < MAX_BLACKFIN_GPTIMERS);
	timer_regs[timer_id]->config = config;
	SSYNC();
}
EXPORT_SYMBOL(set_gptimer_config);

235
uint16_t get_gptimer_config(unsigned int timer_id)
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
{
	tassert(timer_id < MAX_BLACKFIN_GPTIMERS);
	return timer_regs[timer_id]->config;
}
EXPORT_SYMBOL(get_gptimer_config);

void enable_gptimers(uint16_t mask)
{
	int i;
	tassert((mask & ~BLACKFIN_GPTIMER_IDMASK) == 0);
	for (i = 0; i < BFIN_TIMER_NUM_GROUP; ++i) {
		group_regs[i]->enable = mask & 0xFF;
		mask >>= 8;
	}
	SSYNC();
}
EXPORT_SYMBOL(enable_gptimers);

254
static void _disable_gptimers(uint16_t mask)
255 256 257 258 259 260 261 262
{
	int i;
	uint16_t m = mask;
	tassert((mask & ~BLACKFIN_GPTIMER_IDMASK) == 0);
	for (i = 0; i < BFIN_TIMER_NUM_GROUP; ++i) {
		group_regs[i]->disable = m & 0xFF;
		m >>= 8;
	}
263 264 265 266 267 268
}

void disable_gptimers(uint16_t mask)
{
	int i;
	_disable_gptimers(mask);
269 270
	for (i = 0; i < MAX_BLACKFIN_GPTIMERS; ++i)
		if (mask & (1 << i))
271
			group_regs[BFIN_TIMER_OCTET(i)]->status = trun_mask[i];
272 273 274 275
	SSYNC();
}
EXPORT_SYMBOL(disable_gptimers);

276 277 278 279 280 281 282
void disable_gptimers_sync(uint16_t mask)
{
	_disable_gptimers(mask);
	SSYNC();
}
EXPORT_SYMBOL(disable_gptimers_sync);

283
void set_gptimer_pulse_hi(unsigned int timer_id)
284 285 286 287 288 289 290
{
	tassert(timer_id < MAX_BLACKFIN_GPTIMERS);
	timer_regs[timer_id]->config |= TIMER_PULSE_HI;
	SSYNC();
}
EXPORT_SYMBOL(set_gptimer_pulse_hi);

291
void clear_gptimer_pulse_hi(unsigned int timer_id)
292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
{
	tassert(timer_id < MAX_BLACKFIN_GPTIMERS);
	timer_regs[timer_id]->config &= ~TIMER_PULSE_HI;
	SSYNC();
}
EXPORT_SYMBOL(clear_gptimer_pulse_hi);

uint16_t get_enabled_gptimers(void)
{
	int i;
	uint16_t result = 0;
	for (i = 0; i < BFIN_TIMER_NUM_GROUP; ++i)
		result |= (group_regs[i]->enable << (i << 3));
	return result;
}
EXPORT_SYMBOL(get_enabled_gptimers);

MODULE_AUTHOR("Axel Weiss (awe@aglaia-gmbh.de)");
MODULE_DESCRIPTION("Blackfin General Purpose Timers API");
MODULE_LICENSE("GPL");