2 +++ b/arch/mips/lantiq/xway/timer.c
4 +#include <linux/kernel.h>
5 +#include <linux/module.h>
6 +#include <linux/version.h>
7 +#include <linux/types.h>
9 +#include <linux/miscdevice.h>
10 +#include <linux/init.h>
11 +#include <linux/uaccess.h>
12 +#include <linux/unistd.h>
13 +#include <linux/errno.h>
14 +#include <linux/interrupt.h>
15 +#include <linux/sched.h>
18 +#include <asm/div64.h>
20 +#include <lantiq_soc.h>
21 +#include <lantiq_irq.h>
22 +#include <lantiq_timer.h>
24 +#define MAX_NUM_OF_32BIT_TIMER_BLOCKS 6
27 +#define FIRST_TIMER TIMER1A
29 +#define FIRST_TIMER 2
33 + * GPTC divider is set or not.
35 +#define GPTU_CLC_RMC_IS_SET 0
38 + * Timer Interrupt (IRQ)
40 +/* Must be adjusted when ICU driver is available */
41 +#define TIMER_INTERRUPT (INT_NUM_IM3_IRL0 + 22)
46 +#define GET_BITS(x, msb, lsb) \
47 + (((x) & ((1 << ((msb) + 1)) - 1)) >> (lsb))
48 +#define SET_BITS(x, msb, lsb, value) \
49 + (((x) & ~(((1 << ((msb) + 1)) - 1) ^ ((1 << (lsb)) - 1))) | \
50 + (((value) & ((1 << (1 + (msb) - (lsb))) - 1)) << (lsb)))
53 + * GPTU Register Mapping
55 +#define LQ_GPTU (KSEG1 + 0x1E100A00)
56 +#define LQ_GPTU_CLC ((volatile u32 *)(LQ_GPTU + 0x0000))
57 +#define LQ_GPTU_ID ((volatile u32 *)(LQ_GPTU + 0x0008))
58 +#define LQ_GPTU_CON(n, X) ((volatile u32 *)(LQ_GPTU + 0x0010 + ((X) * 4) + ((n) - 1) * 0x0020)) /* X must be either A or B */
59 +#define LQ_GPTU_RUN(n, X) ((volatile u32 *)(LQ_GPTU + 0x0018 + ((X) * 4) + ((n) - 1) * 0x0020)) /* X must be either A or B */
60 +#define LQ_GPTU_RELOAD(n, X) ((volatile u32 *)(LQ_GPTU + 0x0020 + ((X) * 4) + ((n) - 1) * 0x0020)) /* X must be either A or B */
61 +#define LQ_GPTU_COUNT(n, X) ((volatile u32 *)(LQ_GPTU + 0x0028 + ((X) * 4) + ((n) - 1) * 0x0020)) /* X must be either A or B */
62 +#define LQ_GPTU_IRNEN ((volatile u32 *)(LQ_GPTU + 0x00F4))
63 +#define LQ_GPTU_IRNICR ((volatile u32 *)(LQ_GPTU + 0x00F8))
64 +#define LQ_GPTU_IRNCR ((volatile u32 *)(LQ_GPTU + 0x00FC))
67 + * Clock Control Register
69 +#define GPTU_CLC_SMC GET_BITS(*LQ_GPTU_CLC, 23, 16)
70 +#define GPTU_CLC_RMC GET_BITS(*LQ_GPTU_CLC, 15, 8)
71 +#define GPTU_CLC_FSOE (*LQ_GPTU_CLC & (1 << 5))
72 +#define GPTU_CLC_EDIS (*LQ_GPTU_CLC & (1 << 3))
73 +#define GPTU_CLC_SPEN (*LQ_GPTU_CLC & (1 << 2))
74 +#define GPTU_CLC_DISS (*LQ_GPTU_CLC & (1 << 1))
75 +#define GPTU_CLC_DISR (*LQ_GPTU_CLC & (1 << 0))
77 +#define GPTU_CLC_SMC_SET(value) SET_BITS(0, 23, 16, (value))
78 +#define GPTU_CLC_RMC_SET(value) SET_BITS(0, 15, 8, (value))
79 +#define GPTU_CLC_FSOE_SET(value) ((value) ? (1 << 5) : 0)
80 +#define GPTU_CLC_SBWE_SET(value) ((value) ? (1 << 4) : 0)
81 +#define GPTU_CLC_EDIS_SET(value) ((value) ? (1 << 3) : 0)
82 +#define GPTU_CLC_SPEN_SET(value) ((value) ? (1 << 2) : 0)
83 +#define GPTU_CLC_DISR_SET(value) ((value) ? (1 << 0) : 0)
88 +#define GPTU_ID_ID GET_BITS(*LQ_GPTU_ID, 15, 8)
89 +#define GPTU_ID_CFG GET_BITS(*LQ_GPTU_ID, 7, 5)
90 +#define GPTU_ID_REV GET_BITS(*LQ_GPTU_ID, 4, 0)
93 + * Control Register of Timer/Counter nX
94 + * n is the index of block (1 based index)
95 + * X is either A or B
97 +#define GPTU_CON_SRC_EG(n, X) (*LQ_GPTU_CON(n, X) & (1 << 10))
98 +#define GPTU_CON_SRC_EXT(n, X) (*LQ_GPTU_CON(n, X) & (1 << 9))
99 +#define GPTU_CON_SYNC(n, X) (*LQ_GPTU_CON(n, X) & (1 << 8))
100 +#define GPTU_CON_EDGE(n, X) GET_BITS(*LQ_GPTU_CON(n, X), 7, 6)
101 +#define GPTU_CON_INV(n, X) (*LQ_GPTU_CON(n, X) & (1 << 5))
102 +#define GPTU_CON_EXT(n, X) (*LQ_GPTU_CON(n, A) & (1 << 4)) /* Timer/Counter B does not have this bit */
103 +#define GPTU_CON_STP(n, X) (*LQ_GPTU_CON(n, X) & (1 << 3))
104 +#define GPTU_CON_CNT(n, X) (*LQ_GPTU_CON(n, X) & (1 << 2))
105 +#define GPTU_CON_DIR(n, X) (*LQ_GPTU_CON(n, X) & (1 << 1))
106 +#define GPTU_CON_EN(n, X) (*LQ_GPTU_CON(n, X) & (1 << 0))
108 +#define GPTU_CON_SRC_EG_SET(value) ((value) ? 0 : (1 << 10))
109 +#define GPTU_CON_SRC_EXT_SET(value) ((value) ? (1 << 9) : 0)
110 +#define GPTU_CON_SYNC_SET(value) ((value) ? (1 << 8) : 0)
111 +#define GPTU_CON_EDGE_SET(value) SET_BITS(0, 7, 6, (value))
112 +#define GPTU_CON_INV_SET(value) ((value) ? (1 << 5) : 0)
113 +#define GPTU_CON_EXT_SET(value) ((value) ? (1 << 4) : 0)
114 +#define GPTU_CON_STP_SET(value) ((value) ? (1 << 3) : 0)
115 +#define GPTU_CON_CNT_SET(value) ((value) ? (1 << 2) : 0)
116 +#define GPTU_CON_DIR_SET(value) ((value) ? (1 << 1) : 0)
118 +#define GPTU_RUN_RL_SET(value) ((value) ? (1 << 2) : 0)
119 +#define GPTU_RUN_CEN_SET(value) ((value) ? (1 << 1) : 0)
120 +#define GPTU_RUN_SEN_SET(value) ((value) ? (1 << 0) : 0)
122 +#define GPTU_IRNEN_TC_SET(n, X, value) ((value) ? (1 << (((n) - 1) * 2 + (X))) : 0)
123 +#define GPTU_IRNCR_TC_SET(n, X, value) ((value) ? (1 << (((n) - 1) * 2 + (X))) : 0)
125 +#define TIMER_FLAG_MASK_SIZE(x) (x & 0x0001)
126 +#define TIMER_FLAG_MASK_TYPE(x) (x & 0x0002)
127 +#define TIMER_FLAG_MASK_STOP(x) (x & 0x0004)
128 +#define TIMER_FLAG_MASK_DIR(x) (x & 0x0008)
129 +#define TIMER_FLAG_NONE_EDGE 0x0000
130 +#define TIMER_FLAG_MASK_EDGE(x) (x & 0x0030)
131 +#define TIMER_FLAG_REAL 0x0000
132 +#define TIMER_FLAG_INVERT 0x0040
133 +#define TIMER_FLAG_MASK_INVERT(x) (x & 0x0040)
134 +#define TIMER_FLAG_MASK_TRIGGER(x) (x & 0x0070)
135 +#define TIMER_FLAG_MASK_SYNC(x) (x & 0x0080)
136 +#define TIMER_FLAG_CALLBACK_IN_HB 0x0200
137 +#define TIMER_FLAG_MASK_HANDLE(x) (x & 0x0300)
138 +#define TIMER_FLAG_MASK_SRC(x) (x & 0x1000)
140 +struct timer_dev_timer {
141 + unsigned int f_irq_on;
144 + unsigned long arg1;
145 + unsigned long arg2;
149 + struct mutex gptu_mutex;
150 + unsigned int number_of_timers;
151 + unsigned int occupation;
152 + unsigned int f_gptu_on;
153 + struct timer_dev_timer timer[MAX_NUM_OF_32BIT_TIMER_BLOCKS * 2];
156 +unsigned int ltq_get_fpi_bus_clock(int fpi);
158 +static long gptu_ioctl(struct file *, unsigned int, unsigned long);
159 +static int gptu_open(struct inode *, struct file *);
160 +static int gptu_release(struct inode *, struct file *);
162 +static struct file_operations gptu_fops = {
163 + .owner = THIS_MODULE,
164 + .unlocked_ioctl = gptu_ioctl,
166 + .release = gptu_release
169 +static struct miscdevice gptu_miscdev = {
170 + .minor = MISC_DYNAMIC_MINOR,
172 + .fops = &gptu_fops,
175 +static struct timer_dev timer_dev;
177 +static irqreturn_t timer_irq_handler(int irq, void *p)
179 + unsigned int timer;
181 + struct timer_dev_timer *dev_timer = (struct timer_dev_timer *)p;
183 + timer = irq - TIMER_INTERRUPT;
184 + if (timer < timer_dev.number_of_timers
185 + && dev_timer == &timer_dev.timer[timer]) {
186 + /* Clear interrupt. */
187 + ltq_w32(1 << timer, LQ_GPTU_IRNCR);
189 + /* Call user hanler or signal. */
190 + flag = dev_timer->flag;
191 + if (!(timer & 0x01)
192 + || TIMER_FLAG_MASK_SIZE(flag) == TIMER_FLAG_16BIT) {
193 + /* 16-bit timer or timer A of 32-bit timer */
194 + switch (TIMER_FLAG_MASK_HANDLE(flag)) {
195 + case TIMER_FLAG_CALLBACK_IN_IRQ:
196 + case TIMER_FLAG_CALLBACK_IN_HB:
197 + if (dev_timer->arg1)
198 + (*(timer_callback)dev_timer->arg1)(dev_timer->arg2);
200 + case TIMER_FLAG_SIGNAL:
201 + send_sig((int)dev_timer->arg2, (struct task_struct *)dev_timer->arg1, 0);
206 + return IRQ_HANDLED;
209 +static inline void lq_enable_gptu(void)
211 + ltq_pmu_enable(PMU_GPT);
213 + /* Set divider as 1, disable write protection for SPEN, enable module. */
215 + GPTU_CLC_SMC_SET(0x00) |
216 + GPTU_CLC_RMC_SET(0x01) |
217 + GPTU_CLC_FSOE_SET(0) |
218 + GPTU_CLC_SBWE_SET(1) |
219 + GPTU_CLC_EDIS_SET(0) |
220 + GPTU_CLC_SPEN_SET(0) |
221 + GPTU_CLC_DISR_SET(0);
224 +static inline void lq_disable_gptu(void)
226 + ltq_w32(0x00, LQ_GPTU_IRNEN);
227 + ltq_w32(0xfff, LQ_GPTU_IRNCR);
229 + /* Set divider as 0, enable write protection for SPEN, disable module. */
231 + GPTU_CLC_SMC_SET(0x00) |
232 + GPTU_CLC_RMC_SET(0x00) |
233 + GPTU_CLC_FSOE_SET(0) |
234 + GPTU_CLC_SBWE_SET(0) |
235 + GPTU_CLC_EDIS_SET(0) |
236 + GPTU_CLC_SPEN_SET(0) |
237 + GPTU_CLC_DISR_SET(1);
239 + ltq_pmu_disable(PMU_GPT);
242 +int lq_request_timer(unsigned int timer, unsigned int flag,
243 + unsigned long value, unsigned long arg1, unsigned long arg2)
246 + unsigned int con_reg, irnen_reg;
249 + if (timer >= FIRST_TIMER + timer_dev.number_of_timers)
252 + printk(KERN_INFO "request_timer(%d, 0x%08X, %lu)...",
253 + timer, flag, value);
255 + if (TIMER_FLAG_MASK_SIZE(flag) == TIMER_FLAG_16BIT)
260 + mutex_lock(&timer_dev.gptu_mutex);
265 + if (timer < FIRST_TIMER) {
267 + unsigned int shift;
268 + /* This takes care of TIMER1B which is the only choice for Voice TAPI system */
269 + unsigned int offset = TIMER2A;
272 + * Pick up a free timer.
274 + if (TIMER_FLAG_MASK_SIZE(flag) == TIMER_FLAG_16BIT) {
275 + mask = 1 << offset;
278 + mask = 3 << offset;
281 + for (timer = offset;
282 + timer < offset + timer_dev.number_of_timers;
283 + timer += shift, mask <<= shift)
284 + if (!(timer_dev.occupation & mask)) {
285 + timer_dev.occupation |= mask;
288 + if (timer >= offset + timer_dev.number_of_timers) {
289 + printk("failed![%d]\n", __LINE__);
290 + mutex_unlock(&timer_dev.gptu_mutex);
295 + register unsigned int mask;
298 + * Check if the requested timer is free.
300 + mask = (TIMER_FLAG_MASK_SIZE(flag) == TIMER_FLAG_16BIT ? 1 : 3) << timer;
301 + if ((timer_dev.occupation & mask)) {
302 + printk("failed![%d] mask %#x, timer_dev.occupation %#x\n",
303 + __LINE__, mask, timer_dev.occupation);
304 + mutex_unlock(&timer_dev.gptu_mutex);
307 + timer_dev.occupation |= mask;
313 + * Prepare control register value.
315 + switch (TIMER_FLAG_MASK_EDGE(flag)) {
317 + case TIMER_FLAG_NONE_EDGE:
318 + con_reg = GPTU_CON_EDGE_SET(0x00);
320 + case TIMER_FLAG_RISE_EDGE:
321 + con_reg = GPTU_CON_EDGE_SET(0x01);
323 + case TIMER_FLAG_FALL_EDGE:
324 + con_reg = GPTU_CON_EDGE_SET(0x02);
326 + case TIMER_FLAG_ANY_EDGE:
327 + con_reg = GPTU_CON_EDGE_SET(0x03);
330 + if (TIMER_FLAG_MASK_TYPE(flag) == TIMER_FLAG_TIMER)
332 + TIMER_FLAG_MASK_SRC(flag) ==
333 + TIMER_FLAG_EXT_SRC ? GPTU_CON_SRC_EXT_SET(1) :
334 + GPTU_CON_SRC_EXT_SET(0);
337 + TIMER_FLAG_MASK_SRC(flag) ==
338 + TIMER_FLAG_EXT_SRC ? GPTU_CON_SRC_EG_SET(1) :
339 + GPTU_CON_SRC_EG_SET(0);
341 + TIMER_FLAG_MASK_SYNC(flag) ==
342 + TIMER_FLAG_UNSYNC ? GPTU_CON_SYNC_SET(0) :
343 + GPTU_CON_SYNC_SET(1);
345 + TIMER_FLAG_MASK_INVERT(flag) ==
346 + TIMER_FLAG_REAL ? GPTU_CON_INV_SET(0) : GPTU_CON_INV_SET(1);
348 + TIMER_FLAG_MASK_SIZE(flag) ==
349 + TIMER_FLAG_16BIT ? GPTU_CON_EXT_SET(0) :
350 + GPTU_CON_EXT_SET(1);
352 + TIMER_FLAG_MASK_STOP(flag) ==
353 + TIMER_FLAG_ONCE ? GPTU_CON_STP_SET(1) : GPTU_CON_STP_SET(0);
355 + TIMER_FLAG_MASK_TYPE(flag) ==
356 + TIMER_FLAG_TIMER ? GPTU_CON_CNT_SET(0) :
357 + GPTU_CON_CNT_SET(1);
359 + TIMER_FLAG_MASK_DIR(flag) ==
360 + TIMER_FLAG_UP ? GPTU_CON_DIR_SET(1) : GPTU_CON_DIR_SET(0);
363 + * Fill up running data.
365 + timer_dev.timer[timer - FIRST_TIMER].flag = flag;
366 + timer_dev.timer[timer - FIRST_TIMER].arg1 = arg1;
367 + timer_dev.timer[timer - FIRST_TIMER].arg2 = arg2;
368 + if (TIMER_FLAG_MASK_SIZE(flag) != TIMER_FLAG_16BIT)
369 + timer_dev.timer[timer - FIRST_TIMER + 1].flag = flag;
372 + * Enable GPTU module.
374 + if (!timer_dev.f_gptu_on) {
376 + timer_dev.f_gptu_on = 1;
382 + if (TIMER_FLAG_MASK_HANDLE(flag) != TIMER_FLAG_NO_HANDLE) {
383 + if (TIMER_FLAG_MASK_HANDLE(flag) == TIMER_FLAG_SIGNAL)
384 + timer_dev.timer[timer - FIRST_TIMER].arg1 =
385 + (unsigned long) find_task_by_vpid((int) arg1);
387 + irnen_reg = 1 << (timer - FIRST_TIMER);
389 + if (TIMER_FLAG_MASK_HANDLE(flag) == TIMER_FLAG_SIGNAL
390 + || (TIMER_FLAG_MASK_HANDLE(flag) ==
391 + TIMER_FLAG_CALLBACK_IN_IRQ
392 + && timer_dev.timer[timer - FIRST_TIMER].arg1)) {
393 + enable_irq(timer_dev.timer[timer - FIRST_TIMER].irq);
394 + timer_dev.timer[timer - FIRST_TIMER].f_irq_on = 1;
400 + * Write config register, reload value and enable interrupt.
404 + *LQ_GPTU_CON(n, X) = con_reg;
405 + *LQ_GPTU_RELOAD(n, X) = value;
406 + /* printk("reload value = %d\n", (u32)value); */
407 + *LQ_GPTU_IRNEN |= irnen_reg;
409 + mutex_unlock(&timer_dev.gptu_mutex);
410 + printk("successful!\n");
413 +EXPORT_SYMBOL(lq_request_timer);
415 +int lq_free_timer(unsigned int timer)
421 + if (!timer_dev.f_gptu_on)
424 + if (timer < FIRST_TIMER || timer >= FIRST_TIMER + timer_dev.number_of_timers)
427 + mutex_lock(&timer_dev.gptu_mutex);
429 + flag = timer_dev.timer[timer - FIRST_TIMER].flag;
430 + if (TIMER_FLAG_MASK_SIZE(flag) != TIMER_FLAG_16BIT)
433 + mask = (TIMER_FLAG_MASK_SIZE(flag) == TIMER_FLAG_16BIT ? 1 : 3) << timer;
434 + if (((timer_dev.occupation & mask) ^ mask)) {
435 + mutex_unlock(&timer_dev.gptu_mutex);
442 + if (GPTU_CON_EN(n, X))
443 + *LQ_GPTU_RUN(n, X) = GPTU_RUN_CEN_SET(1);
445 + *LQ_GPTU_IRNEN &= ~GPTU_IRNEN_TC_SET(n, X, 1);
446 + *LQ_GPTU_IRNCR |= GPTU_IRNCR_TC_SET(n, X, 1);
448 + if (timer_dev.timer[timer - FIRST_TIMER].f_irq_on) {
449 + disable_irq(timer_dev.timer[timer - FIRST_TIMER].irq);
450 + timer_dev.timer[timer - FIRST_TIMER].f_irq_on = 0;
453 + timer_dev.occupation &= ~mask;
454 + if (!timer_dev.occupation && timer_dev.f_gptu_on) {
456 + timer_dev.f_gptu_on = 0;
459 + mutex_unlock(&timer_dev.gptu_mutex);
463 +EXPORT_SYMBOL(lq_free_timer);
465 +int lq_start_timer(unsigned int timer, int is_resume)
471 + if (!timer_dev.f_gptu_on)
474 + if (timer < FIRST_TIMER || timer >= FIRST_TIMER + timer_dev.number_of_timers)
477 + mutex_lock(&timer_dev.gptu_mutex);
479 + flag = timer_dev.timer[timer - FIRST_TIMER].flag;
480 + if (TIMER_FLAG_MASK_SIZE(flag) != TIMER_FLAG_16BIT)
483 + mask = (TIMER_FLAG_MASK_SIZE(flag) ==
484 + TIMER_FLAG_16BIT ? 1 : 3) << timer;
485 + if (((timer_dev.occupation & mask) ^ mask)) {
486 + mutex_unlock(&timer_dev.gptu_mutex);
493 + *LQ_GPTU_RUN(n, X) = GPTU_RUN_RL_SET(!is_resume) | GPTU_RUN_SEN_SET(1);
495 + mutex_unlock(&timer_dev.gptu_mutex);
499 +EXPORT_SYMBOL(lq_start_timer);
501 +int lq_stop_timer(unsigned int timer)
507 + if (!timer_dev.f_gptu_on)
510 + if (timer < FIRST_TIMER
511 + || timer >= FIRST_TIMER + timer_dev.number_of_timers)
514 + mutex_lock(&timer_dev.gptu_mutex);
516 + flag = timer_dev.timer[timer - FIRST_TIMER].flag;
517 + if (TIMER_FLAG_MASK_SIZE(flag) != TIMER_FLAG_16BIT)
520 + mask = (TIMER_FLAG_MASK_SIZE(flag) == TIMER_FLAG_16BIT ? 1 : 3) << timer;
521 + if (((timer_dev.occupation & mask) ^ mask)) {
522 + mutex_unlock(&timer_dev.gptu_mutex);
529 + *LQ_GPTU_RUN(n, X) = GPTU_RUN_CEN_SET(1);
531 + mutex_unlock(&timer_dev.gptu_mutex);
535 +EXPORT_SYMBOL(lq_stop_timer);
537 +int lq_reset_counter_flags(u32 timer, u32 flags)
539 + unsigned int oflag;
540 + unsigned int mask, con_reg;
543 + if (!timer_dev.f_gptu_on)
546 + if (timer < FIRST_TIMER || timer >= FIRST_TIMER + timer_dev.number_of_timers)
549 + mutex_lock(&timer_dev.gptu_mutex);
551 + oflag = timer_dev.timer[timer - FIRST_TIMER].flag;
552 + if (TIMER_FLAG_MASK_SIZE(oflag) != TIMER_FLAG_16BIT)
555 + mask = (TIMER_FLAG_MASK_SIZE(oflag) == TIMER_FLAG_16BIT ? 1 : 3) << timer;
556 + if (((timer_dev.occupation & mask) ^ mask)) {
557 + mutex_unlock(&timer_dev.gptu_mutex);
561 + switch (TIMER_FLAG_MASK_EDGE(flags)) {
563 + case TIMER_FLAG_NONE_EDGE:
564 + con_reg = GPTU_CON_EDGE_SET(0x00);
566 + case TIMER_FLAG_RISE_EDGE:
567 + con_reg = GPTU_CON_EDGE_SET(0x01);
569 + case TIMER_FLAG_FALL_EDGE:
570 + con_reg = GPTU_CON_EDGE_SET(0x02);
572 + case TIMER_FLAG_ANY_EDGE:
573 + con_reg = GPTU_CON_EDGE_SET(0x03);
576 + if (TIMER_FLAG_MASK_TYPE(flags) == TIMER_FLAG_TIMER)
577 + con_reg |= TIMER_FLAG_MASK_SRC(flags) == TIMER_FLAG_EXT_SRC ? GPTU_CON_SRC_EXT_SET(1) : GPTU_CON_SRC_EXT_SET(0);
579 + con_reg |= TIMER_FLAG_MASK_SRC(flags) == TIMER_FLAG_EXT_SRC ? GPTU_CON_SRC_EG_SET(1) : GPTU_CON_SRC_EG_SET(0);
580 + con_reg |= TIMER_FLAG_MASK_SYNC(flags) == TIMER_FLAG_UNSYNC ? GPTU_CON_SYNC_SET(0) : GPTU_CON_SYNC_SET(1);
581 + con_reg |= TIMER_FLAG_MASK_INVERT(flags) == TIMER_FLAG_REAL ? GPTU_CON_INV_SET(0) : GPTU_CON_INV_SET(1);
582 + con_reg |= TIMER_FLAG_MASK_SIZE(flags) == TIMER_FLAG_16BIT ? GPTU_CON_EXT_SET(0) : GPTU_CON_EXT_SET(1);
583 + con_reg |= TIMER_FLAG_MASK_STOP(flags) == TIMER_FLAG_ONCE ? GPTU_CON_STP_SET(1) : GPTU_CON_STP_SET(0);
584 + con_reg |= TIMER_FLAG_MASK_TYPE(flags) == TIMER_FLAG_TIMER ? GPTU_CON_CNT_SET(0) : GPTU_CON_CNT_SET(1);
585 + con_reg |= TIMER_FLAG_MASK_DIR(flags) == TIMER_FLAG_UP ? GPTU_CON_DIR_SET(1) : GPTU_CON_DIR_SET(0);
587 + timer_dev.timer[timer - FIRST_TIMER].flag = flags;
588 + if (TIMER_FLAG_MASK_SIZE(flags) != TIMER_FLAG_16BIT)
589 + timer_dev.timer[timer - FIRST_TIMER + 1].flag = flags;
594 + *LQ_GPTU_CON(n, X) = con_reg;
596 + printk(KERN_INFO "[%s]: counter%d oflags %#x, nflags %#x, GPTU_CON %#x\n", __func__, timer, oflag, flags, *LQ_GPTU_CON(n, X));
597 + mutex_unlock(&timer_dev.gptu_mutex);
600 +EXPORT_SYMBOL(lq_reset_counter_flags);
602 +int lq_get_count_value(unsigned int timer, unsigned long *value)
608 + if (!timer_dev.f_gptu_on)
611 + if (timer < FIRST_TIMER
612 + || timer >= FIRST_TIMER + timer_dev.number_of_timers)
615 + mutex_lock(&timer_dev.gptu_mutex);
617 + flag = timer_dev.timer[timer - FIRST_TIMER].flag;
618 + if (TIMER_FLAG_MASK_SIZE(flag) != TIMER_FLAG_16BIT)
621 + mask = (TIMER_FLAG_MASK_SIZE(flag) == TIMER_FLAG_16BIT ? 1 : 3) << timer;
622 + if (((timer_dev.occupation & mask) ^ mask)) {
623 + mutex_unlock(&timer_dev.gptu_mutex);
630 + *value = *LQ_GPTU_COUNT(n, X);
632 + mutex_unlock(&timer_dev.gptu_mutex);
636 +EXPORT_SYMBOL(lq_get_count_value);
638 +u32 lq_cal_divider(unsigned long freq)
640 + u64 module_freq, fpi = ltq_get_fpi_bus_clock(2);
641 + u32 clock_divider = 1;
642 + module_freq = fpi * 1000;
643 + do_div(module_freq, clock_divider * freq);
644 + return module_freq;
646 +EXPORT_SYMBOL(lq_cal_divider);
648 +int lq_set_timer(unsigned int timer, unsigned int freq, int is_cyclic,
649 + int is_ext_src, unsigned int handle_flag, unsigned long arg1,
650 + unsigned long arg2)
652 + unsigned long divider;
655 + divider = lq_cal_divider(freq);
658 + flag = ((divider & ~0xFFFF) ? TIMER_FLAG_32BIT : TIMER_FLAG_16BIT)
659 + | (is_cyclic ? TIMER_FLAG_CYCLIC : TIMER_FLAG_ONCE)
660 + | (is_ext_src ? TIMER_FLAG_EXT_SRC : TIMER_FLAG_INT_SRC)
661 + | TIMER_FLAG_TIMER | TIMER_FLAG_DOWN
662 + | TIMER_FLAG_MASK_HANDLE(handle_flag);
664 + printk(KERN_INFO "lq_set_timer(%d, %d), divider = %lu\n",
665 + timer, freq, divider);
666 + return lq_request_timer(timer, flag, divider, arg1, arg2);
668 +EXPORT_SYMBOL(lq_set_timer);
670 +int lq_set_counter(unsigned int timer, unsigned int flag, u32 reload,
671 + unsigned long arg1, unsigned long arg2)
673 + printk(KERN_INFO "lq_set_counter(%d, %#x, %d)\n", timer, flag, reload);
674 + return lq_request_timer(timer, flag, reload, arg1, arg2);
676 +EXPORT_SYMBOL(lq_set_counter);
678 +static long gptu_ioctl(struct file *file, unsigned int cmd,
682 + struct gptu_ioctl_param param;
684 + if (!access_ok(VERIFY_READ, arg, sizeof(struct gptu_ioctl_param)))
686 + copy_from_user(¶m, (void *) arg, sizeof(param));
688 + if ((((cmd == GPTU_REQUEST_TIMER || cmd == GPTU_SET_TIMER
689 + || GPTU_SET_COUNTER) && param.timer < 2)
690 + || cmd == GPTU_GET_COUNT_VALUE || cmd == GPTU_CALCULATE_DIVIDER)
691 + && !access_ok(VERIFY_WRITE, arg,
692 + sizeof(struct gptu_ioctl_param)))
696 + case GPTU_REQUEST_TIMER:
697 + ret = lq_request_timer(param.timer, param.flag, param.value,
698 + (unsigned long) param.pid,
699 + (unsigned long) param.sig);
701 + copy_to_user(&((struct gptu_ioctl_param *) arg)->
702 + timer, &ret, sizeof(&ret));
706 + case GPTU_FREE_TIMER:
707 + ret = lq_free_timer(param.timer);
709 + case GPTU_START_TIMER:
710 + ret = lq_start_timer(param.timer, param.flag);
712 + case GPTU_STOP_TIMER:
713 + ret = lq_stop_timer(param.timer);
715 + case GPTU_GET_COUNT_VALUE:
716 + ret = lq_get_count_value(param.timer, ¶m.value);
718 + copy_to_user(&((struct gptu_ioctl_param *) arg)->
719 + value, ¶m.value,
720 + sizeof(param.value));
722 + case GPTU_CALCULATE_DIVIDER:
723 + param.value = lq_cal_divider(param.value);
724 + if (param.value == 0)
727 + copy_to_user(&((struct gptu_ioctl_param *) arg)->
728 + value, ¶m.value,
729 + sizeof(param.value));
733 + case GPTU_SET_TIMER:
734 + ret = lq_set_timer(param.timer, param.value,
735 + TIMER_FLAG_MASK_STOP(param.flag) !=
736 + TIMER_FLAG_ONCE ? 1 : 0,
737 + TIMER_FLAG_MASK_SRC(param.flag) ==
738 + TIMER_FLAG_EXT_SRC ? 1 : 0,
739 + TIMER_FLAG_MASK_HANDLE(param.flag) ==
740 + TIMER_FLAG_SIGNAL ? TIMER_FLAG_SIGNAL :
741 + TIMER_FLAG_NO_HANDLE,
742 + (unsigned long) param.pid,
743 + (unsigned long) param.sig);
745 + copy_to_user(&((struct gptu_ioctl_param *) arg)->
746 + timer, &ret, sizeof(&ret));
750 + case GPTU_SET_COUNTER:
751 + lq_set_counter(param.timer, param.flag, param.value, 0, 0);
753 + copy_to_user(&((struct gptu_ioctl_param *) arg)->
754 + timer, &ret, sizeof(&ret));
765 +static int gptu_open(struct inode *inode, struct file *file)
770 +static int gptu_release(struct inode *inode, struct file *file)
775 +int __init lq_gptu_init(void)
780 + ltq_w32(0, LQ_GPTU_IRNEN);
781 + ltq_w32(0xfff, LQ_GPTU_IRNCR);
783 + memset(&timer_dev, 0, sizeof(timer_dev));
784 + mutex_init(&timer_dev.gptu_mutex);
787 + timer_dev.number_of_timers = GPTU_ID_CFG * 2;
789 + if (timer_dev.number_of_timers > MAX_NUM_OF_32BIT_TIMER_BLOCKS * 2)
790 + timer_dev.number_of_timers = MAX_NUM_OF_32BIT_TIMER_BLOCKS * 2;
791 + printk(KERN_INFO "gptu: totally %d 16-bit timers/counters\n", timer_dev.number_of_timers);
793 + ret = misc_register(&gptu_miscdev);
795 + printk(KERN_ERR "gptu: can't misc_register, get error %d\n", -ret);
798 + printk(KERN_INFO "gptu: misc_register on minor %d\n", gptu_miscdev.minor);
801 + for (i = 0; i < timer_dev.number_of_timers; i++) {
802 + ret = request_irq(TIMER_INTERRUPT + i, timer_irq_handler, IRQF_TIMER, gptu_miscdev.name, &timer_dev.timer[i]);
804 + for (; i >= 0; i--)
805 + free_irq(TIMER_INTERRUPT + i, &timer_dev.timer[i]);
806 + misc_deregister(&gptu_miscdev);
807 + printk(KERN_ERR "gptu: failed in requesting irq (%d), get error %d\n", i, -ret);
810 + timer_dev.timer[i].irq = TIMER_INTERRUPT + i;
811 + disable_irq(timer_dev.timer[i].irq);
812 + printk(KERN_INFO "gptu: succeeded to request irq %d\n", timer_dev.timer[i].irq);
819 +void __exit lq_gptu_exit(void)
823 + for (i = 0; i < timer_dev.number_of_timers; i++) {
824 + if (timer_dev.timer[i].f_irq_on)
825 + disable_irq(timer_dev.timer[i].irq);
826 + free_irq(timer_dev.timer[i].irq, &timer_dev.timer[i]);
829 + misc_deregister(&gptu_miscdev);
832 +module_init(lq_gptu_init);
833 +module_exit(lq_gptu_exit);
835 +++ b/arch/mips/include/asm/mach-lantiq/lantiq_timer.h
837 +#ifndef __DANUBE_GPTU_DEV_H__2005_07_26__10_19__
838 +#define __DANUBE_GPTU_DEV_H__2005_07_26__10_19__
841 +/******************************************************************************
842 + Copyright (c) 2002, Infineon Technologies. All rights reserved.
845 + Because the program is licensed free of charge, there is no warranty for
846 + the program, to the extent permitted by applicable law. Except when
847 + otherwise stated in writing the copyright holders and/or other parties
848 + provide the program "as is" without warranty of any kind, either
849 + expressed or implied, including, but not limited to, the implied
850 + warranties of merchantability and fitness for a particular purpose. The
851 + entire risk as to the quality and performance of the program is with
852 + you. should the program prove defective, you assume the cost of all
853 + necessary servicing, repair or correction.
855 + In no event unless required by applicable law or agreed to in writing
856 + will any copyright holder, or any other party who may modify and/or
857 + redistribute the program as permitted above, be liable to you for
858 + damages, including any general, special, incidental or consequential
859 + damages arising out of the use or inability to use the program
860 + (including but not limited to loss of data or data being rendered
861 + inaccurate or losses sustained by you or third parties or a failure of
862 + the program to operate with any other programs), even if such holder or
863 + other party has been advised of the possibility of such damages.
864 +******************************************************************************/
868 + * ####################################
870 + * ####################################
874 + * Available Timer/Counter Index
876 +#define TIMER(n, X) (n * 2 + (X ? 1 : 0))
877 +#define TIMER_ANY 0x00
878 +#define TIMER1A TIMER(1, 0)
879 +#define TIMER1B TIMER(1, 1)
880 +#define TIMER2A TIMER(2, 0)
881 +#define TIMER2B TIMER(2, 1)
882 +#define TIMER3A TIMER(3, 0)
883 +#define TIMER3B TIMER(3, 1)
886 + * Flag of Timer/Counter
887 + * These flags specify the way in which timer is configured.
889 +/* Bit size of timer/counter. */
890 +#define TIMER_FLAG_16BIT 0x0000
891 +#define TIMER_FLAG_32BIT 0x0001
892 +/* Switch between timer and counter. */
893 +#define TIMER_FLAG_TIMER 0x0000
894 +#define TIMER_FLAG_COUNTER 0x0002
895 +/* Stop or continue when overflowing/underflowing. */
896 +#define TIMER_FLAG_ONCE 0x0000
897 +#define TIMER_FLAG_CYCLIC 0x0004
898 +/* Count up or counter down. */
899 +#define TIMER_FLAG_UP 0x0000
900 +#define TIMER_FLAG_DOWN 0x0008
901 +/* Count on specific level or edge. */
902 +#define TIMER_FLAG_HIGH_LEVEL_SENSITIVE 0x0000
903 +#define TIMER_FLAG_LOW_LEVEL_SENSITIVE 0x0040
904 +#define TIMER_FLAG_RISE_EDGE 0x0010
905 +#define TIMER_FLAG_FALL_EDGE 0x0020
906 +#define TIMER_FLAG_ANY_EDGE 0x0030
907 +/* Signal is syncronous to module clock or not. */
908 +#define TIMER_FLAG_UNSYNC 0x0000
909 +#define TIMER_FLAG_SYNC 0x0080
910 +/* Different interrupt handle type. */
911 +#define TIMER_FLAG_NO_HANDLE 0x0000
912 +#if defined(__KERNEL__)
913 + #define TIMER_FLAG_CALLBACK_IN_IRQ 0x0100
914 +#endif // defined(__KERNEL__)
915 +#define TIMER_FLAG_SIGNAL 0x0300
916 +/* Internal clock source or external clock source */
917 +#define TIMER_FLAG_INT_SRC 0x0000
918 +#define TIMER_FLAG_EXT_SRC 0x1000
924 +#define GPTU_REQUEST_TIMER 0x01 /* General method to setup timer/counter. */
925 +#define GPTU_FREE_TIMER 0x02 /* Free timer/counter. */
926 +#define GPTU_START_TIMER 0x03 /* Start or resume timer/counter. */
927 +#define GPTU_STOP_TIMER 0x04 /* Suspend timer/counter. */
928 +#define GPTU_GET_COUNT_VALUE 0x05 /* Get current count value. */
929 +#define GPTU_CALCULATE_DIVIDER 0x06 /* Calculate timer divider from given freq.*/
930 +#define GPTU_SET_TIMER 0x07 /* Simplified method to setup timer. */
931 +#define GPTU_SET_COUNTER 0x08 /* Simplified method to setup counter. */
934 + * Data Type Used to Call ioctl
936 +struct gptu_ioctl_param {
937 + unsigned int timer; /* In command GPTU_REQUEST_TIMER, GPTU_SET_TIMER, and *
938 + * GPTU_SET_COUNTER, this field is ID of expected *
939 + * timer/counter. If it's zero, a timer/counter would *
940 + * be dynamically allocated and ID would be stored in *
942 + * In command GPTU_GET_COUNT_VALUE, this field is *
944 + * In other command, this field is ID of timer/counter *
946 + unsigned int flag; /* In command GPTU_REQUEST_TIMER, GPTU_SET_TIMER, and *
947 + * GPTU_SET_COUNTER, this field contains flags to *
948 + * specify how to configure timer/counter. *
949 + * In command GPTU_START_TIMER, zero indicate start *
950 + * and non-zero indicate resume timer/counter. *
951 + * In other command, this field is ignored. */
952 + unsigned long value; /* In command GPTU_REQUEST_TIMER, this field contains *
953 + * init/reload value. *
954 + * In command GPTU_SET_TIMER, this field contains *
955 + * frequency (0.001Hz) of timer. *
956 + * In command GPTU_GET_COUNT_VALUE, current count *
957 + * value would be stored in this field. *
958 + * In command GPTU_CALCULATE_DIVIDER, this field *
959 + * contains frequency wanted, and after calculation, *
960 + * divider would be stored in this field to overwrite *
962 + * In other command, this field is ignored. */
963 + int pid; /* In command GPTU_REQUEST_TIMER and GPTU_SET_TIMER, *
964 + * if signal is required, this field contains process *
965 + * ID to which signal would be sent. *
966 + * In other command, this field is ignored. */
967 + int sig; /* In command GPTU_REQUEST_TIMER and GPTU_SET_TIMER, *
968 + * if signal is required, this field contains signal *
969 + * number which would be sent. *
970 + * In other command, this field is ignored. */
974 + * ####################################
976 + * ####################################
978 +typedef void (*timer_callback)(unsigned long arg);
980 +extern int lq_request_timer(unsigned int, unsigned int, unsigned long, unsigned long, unsigned long);
981 +extern int lq_free_timer(unsigned int);
982 +extern int lq_start_timer(unsigned int, int);
983 +extern int lq_stop_timer(unsigned int);
984 +extern int lq_reset_counter_flags(u32 timer, u32 flags);
985 +extern int lq_get_count_value(unsigned int, unsigned long *);
986 +extern u32 lq_cal_divider(unsigned long);
987 +extern int lq_set_timer(unsigned int, unsigned int, int, int, unsigned int, unsigned long, unsigned long);
988 +extern int lq_set_counter(unsigned int timer, unsigned int flag,
989 + u32 reload, unsigned long arg1, unsigned long arg2);
991 +#endif /* __DANUBE_GPTU_DEV_H__2005_07_26__10_19__ */
992 --- a/arch/mips/lantiq/xway/Makefile
993 +++ b/arch/mips/lantiq/xway/Makefile
995 -obj-y := pmu.o ebu.o reset.o gpio.o gpio_stp.o gpio_ebu.o devices.o dma.o
996 +obj-y := pmu.o ebu.o reset.o gpio.o gpio_stp.o gpio_ebu.o devices.o dma.o timer.o
998 obj-$(CONFIG_SOC_XWAY) += clk-xway.o prom-xway.o setup-xway.o
999 obj-$(CONFIG_SOC_AMAZON_SE) += clk-ase.o prom-ase.o setup-ase.o