2 +++ b/arch/mips/lantiq/xway/timer.c
4 +#ifndef CONFIG_SOC_AMAZON_SE
6 +#include <linux/kernel.h>
7 +#include <linux/module.h>
8 +#include <linux/version.h>
9 +#include <linux/types.h>
10 +#include <linux/fs.h>
11 +#include <linux/miscdevice.h>
12 +#include <linux/init.h>
13 +#include <linux/uaccess.h>
14 +#include <linux/unistd.h>
15 +#include <linux/errno.h>
16 +#include <linux/interrupt.h>
17 +#include <linux/sched.h>
20 +#include <asm/div64.h>
23 +#include <lantiq_soc.h>
24 +#include <lantiq_irq.h>
25 +#include <lantiq_timer.h>
27 +#define MAX_NUM_OF_32BIT_TIMER_BLOCKS 6
30 +#define FIRST_TIMER TIMER1A
32 +#define FIRST_TIMER 2
36 + * GPTC divider is set or not.
38 +#define GPTU_CLC_RMC_IS_SET 0
41 + * Timer Interrupt (IRQ)
43 +/* Must be adjusted when ICU driver is available */
44 +#define TIMER_INTERRUPT (INT_NUM_IM3_IRL0 + 22)
49 +#define GET_BITS(x, msb, lsb) \
50 + (((x) & ((1 << ((msb) + 1)) - 1)) >> (lsb))
51 +#define SET_BITS(x, msb, lsb, value) \
52 + (((x) & ~(((1 << ((msb) + 1)) - 1) ^ ((1 << (lsb)) - 1))) | \
53 + (((value) & ((1 << (1 + (msb) - (lsb))) - 1)) << (lsb)))
56 + * GPTU Register Mapping
58 +#define LQ_GPTU (KSEG1 + 0x1E100A00)
59 +#define LQ_GPTU_CLC ((volatile u32 *)(LQ_GPTU + 0x0000))
60 +#define LQ_GPTU_ID ((volatile u32 *)(LQ_GPTU + 0x0008))
61 +#define LQ_GPTU_CON(n, X) ((volatile u32 *)(LQ_GPTU + 0x0010 + ((X) * 4) + ((n) - 1) * 0x0020)) /* X must be either A or B */
62 +#define LQ_GPTU_RUN(n, X) ((volatile u32 *)(LQ_GPTU + 0x0018 + ((X) * 4) + ((n) - 1) * 0x0020)) /* X must be either A or B */
63 +#define LQ_GPTU_RELOAD(n, X) ((volatile u32 *)(LQ_GPTU + 0x0020 + ((X) * 4) + ((n) - 1) * 0x0020)) /* X must be either A or B */
64 +#define LQ_GPTU_COUNT(n, X) ((volatile u32 *)(LQ_GPTU + 0x0028 + ((X) * 4) + ((n) - 1) * 0x0020)) /* X must be either A or B */
65 +#define LQ_GPTU_IRNEN ((volatile u32 *)(LQ_GPTU + 0x00F4))
66 +#define LQ_GPTU_IRNICR ((volatile u32 *)(LQ_GPTU + 0x00F8))
67 +#define LQ_GPTU_IRNCR ((volatile u32 *)(LQ_GPTU + 0x00FC))
70 + * Clock Control Register
72 +#define GPTU_CLC_SMC GET_BITS(*LQ_GPTU_CLC, 23, 16)
73 +#define GPTU_CLC_RMC GET_BITS(*LQ_GPTU_CLC, 15, 8)
74 +#define GPTU_CLC_FSOE (*LQ_GPTU_CLC & (1 << 5))
75 +#define GPTU_CLC_EDIS (*LQ_GPTU_CLC & (1 << 3))
76 +#define GPTU_CLC_SPEN (*LQ_GPTU_CLC & (1 << 2))
77 +#define GPTU_CLC_DISS (*LQ_GPTU_CLC & (1 << 1))
78 +#define GPTU_CLC_DISR (*LQ_GPTU_CLC & (1 << 0))
80 +#define GPTU_CLC_SMC_SET(value) SET_BITS(0, 23, 16, (value))
81 +#define GPTU_CLC_RMC_SET(value) SET_BITS(0, 15, 8, (value))
82 +#define GPTU_CLC_FSOE_SET(value) ((value) ? (1 << 5) : 0)
83 +#define GPTU_CLC_SBWE_SET(value) ((value) ? (1 << 4) : 0)
84 +#define GPTU_CLC_EDIS_SET(value) ((value) ? (1 << 3) : 0)
85 +#define GPTU_CLC_SPEN_SET(value) ((value) ? (1 << 2) : 0)
86 +#define GPTU_CLC_DISR_SET(value) ((value) ? (1 << 0) : 0)
91 +#define GPTU_ID_ID GET_BITS(*LQ_GPTU_ID, 15, 8)
92 +#define GPTU_ID_CFG GET_BITS(*LQ_GPTU_ID, 7, 5)
93 +#define GPTU_ID_REV GET_BITS(*LQ_GPTU_ID, 4, 0)
96 + * Control Register of Timer/Counter nX
97 + * n is the index of block (1 based index)
98 + * X is either A or B
100 +#define GPTU_CON_SRC_EG(n, X) (*LQ_GPTU_CON(n, X) & (1 << 10))
101 +#define GPTU_CON_SRC_EXT(n, X) (*LQ_GPTU_CON(n, X) & (1 << 9))
102 +#define GPTU_CON_SYNC(n, X) (*LQ_GPTU_CON(n, X) & (1 << 8))
103 +#define GPTU_CON_EDGE(n, X) GET_BITS(*LQ_GPTU_CON(n, X), 7, 6)
104 +#define GPTU_CON_INV(n, X) (*LQ_GPTU_CON(n, X) & (1 << 5))
105 +#define GPTU_CON_EXT(n, X) (*LQ_GPTU_CON(n, A) & (1 << 4)) /* Timer/Counter B does not have this bit */
106 +#define GPTU_CON_STP(n, X) (*LQ_GPTU_CON(n, X) & (1 << 3))
107 +#define GPTU_CON_CNT(n, X) (*LQ_GPTU_CON(n, X) & (1 << 2))
108 +#define GPTU_CON_DIR(n, X) (*LQ_GPTU_CON(n, X) & (1 << 1))
109 +#define GPTU_CON_EN(n, X) (*LQ_GPTU_CON(n, X) & (1 << 0))
111 +#define GPTU_CON_SRC_EG_SET(value) ((value) ? 0 : (1 << 10))
112 +#define GPTU_CON_SRC_EXT_SET(value) ((value) ? (1 << 9) : 0)
113 +#define GPTU_CON_SYNC_SET(value) ((value) ? (1 << 8) : 0)
114 +#define GPTU_CON_EDGE_SET(value) SET_BITS(0, 7, 6, (value))
115 +#define GPTU_CON_INV_SET(value) ((value) ? (1 << 5) : 0)
116 +#define GPTU_CON_EXT_SET(value) ((value) ? (1 << 4) : 0)
117 +#define GPTU_CON_STP_SET(value) ((value) ? (1 << 3) : 0)
118 +#define GPTU_CON_CNT_SET(value) ((value) ? (1 << 2) : 0)
119 +#define GPTU_CON_DIR_SET(value) ((value) ? (1 << 1) : 0)
121 +#define GPTU_RUN_RL_SET(value) ((value) ? (1 << 2) : 0)
122 +#define GPTU_RUN_CEN_SET(value) ((value) ? (1 << 1) : 0)
123 +#define GPTU_RUN_SEN_SET(value) ((value) ? (1 << 0) : 0)
125 +#define GPTU_IRNEN_TC_SET(n, X, value) ((value) ? (1 << (((n) - 1) * 2 + (X))) : 0)
126 +#define GPTU_IRNCR_TC_SET(n, X, value) ((value) ? (1 << (((n) - 1) * 2 + (X))) : 0)
128 +#define TIMER_FLAG_MASK_SIZE(x) (x & 0x0001)
129 +#define TIMER_FLAG_MASK_TYPE(x) (x & 0x0002)
130 +#define TIMER_FLAG_MASK_STOP(x) (x & 0x0004)
131 +#define TIMER_FLAG_MASK_DIR(x) (x & 0x0008)
132 +#define TIMER_FLAG_NONE_EDGE 0x0000
133 +#define TIMER_FLAG_MASK_EDGE(x) (x & 0x0030)
134 +#define TIMER_FLAG_REAL 0x0000
135 +#define TIMER_FLAG_INVERT 0x0040
136 +#define TIMER_FLAG_MASK_INVERT(x) (x & 0x0040)
137 +#define TIMER_FLAG_MASK_TRIGGER(x) (x & 0x0070)
138 +#define TIMER_FLAG_MASK_SYNC(x) (x & 0x0080)
139 +#define TIMER_FLAG_CALLBACK_IN_HB 0x0200
140 +#define TIMER_FLAG_MASK_HANDLE(x) (x & 0x0300)
141 +#define TIMER_FLAG_MASK_SRC(x) (x & 0x1000)
143 +struct timer_dev_timer {
144 + unsigned int f_irq_on;
147 + unsigned long arg1;
148 + unsigned long arg2;
152 + struct mutex gptu_mutex;
153 + unsigned int number_of_timers;
154 + unsigned int occupation;
155 + unsigned int f_gptu_on;
156 + struct timer_dev_timer timer[MAX_NUM_OF_32BIT_TIMER_BLOCKS * 2];
160 +unsigned int ltq_get_fpi_bus_clock(int fpi) {
161 + struct clk *clk = clk_get_fpi();
162 + return clk_get_rate(clk);
166 +static long gptu_ioctl(struct file *, unsigned int, unsigned long);
167 +static int gptu_open(struct inode *, struct file *);
168 +static int gptu_release(struct inode *, struct file *);
170 +static struct file_operations gptu_fops = {
171 + .owner = THIS_MODULE,
172 + .unlocked_ioctl = gptu_ioctl,
174 + .release = gptu_release
177 +static struct miscdevice gptu_miscdev = {
178 + .minor = MISC_DYNAMIC_MINOR,
180 + .fops = &gptu_fops,
183 +static struct timer_dev timer_dev;
185 +static irqreturn_t timer_irq_handler(int irq, void *p)
187 + unsigned int timer;
189 + struct timer_dev_timer *dev_timer = (struct timer_dev_timer *)p;
191 + timer = irq - TIMER_INTERRUPT;
192 + if (timer < timer_dev.number_of_timers
193 + && dev_timer == &timer_dev.timer[timer]) {
194 + /* Clear interrupt. */
195 + ltq_w32(1 << timer, LQ_GPTU_IRNCR);
197 + /* Call user hanler or signal. */
198 + flag = dev_timer->flag;
199 + if (!(timer & 0x01)
200 + || TIMER_FLAG_MASK_SIZE(flag) == TIMER_FLAG_16BIT) {
201 + /* 16-bit timer or timer A of 32-bit timer */
202 + switch (TIMER_FLAG_MASK_HANDLE(flag)) {
203 + case TIMER_FLAG_CALLBACK_IN_IRQ:
204 + case TIMER_FLAG_CALLBACK_IN_HB:
205 + if (dev_timer->arg1)
206 + (*(timer_callback)dev_timer->arg1)(dev_timer->arg2);
208 + case TIMER_FLAG_SIGNAL:
209 + send_sig((int)dev_timer->arg2, (struct task_struct *)dev_timer->arg1, 0);
214 + return IRQ_HANDLED;
217 +static inline void lq_enable_gptu(void)
219 + struct clk *clk = clk_get_sys("1e100a00.gptu", NULL);
222 + //ltq_pmu_enable(PMU_GPT);
224 + /* Set divider as 1, disable write protection for SPEN, enable module. */
226 + GPTU_CLC_SMC_SET(0x00) |
227 + GPTU_CLC_RMC_SET(0x01) |
228 + GPTU_CLC_FSOE_SET(0) |
229 + GPTU_CLC_SBWE_SET(1) |
230 + GPTU_CLC_EDIS_SET(0) |
231 + GPTU_CLC_SPEN_SET(0) |
232 + GPTU_CLC_DISR_SET(0);
235 +static inline void lq_disable_gptu(void)
237 + struct clk *clk = clk_get_sys("1e100a00.gptu", NULL);
238 + ltq_w32(0x00, LQ_GPTU_IRNEN);
239 + ltq_w32(0xfff, LQ_GPTU_IRNCR);
241 + /* Set divider as 0, enable write protection for SPEN, disable module. */
243 + GPTU_CLC_SMC_SET(0x00) |
244 + GPTU_CLC_RMC_SET(0x00) |
245 + GPTU_CLC_FSOE_SET(0) |
246 + GPTU_CLC_SBWE_SET(0) |
247 + GPTU_CLC_EDIS_SET(0) |
248 + GPTU_CLC_SPEN_SET(0) |
249 + GPTU_CLC_DISR_SET(1);
254 +int lq_request_timer(unsigned int timer, unsigned int flag,
255 + unsigned long value, unsigned long arg1, unsigned long arg2)
258 + unsigned int con_reg, irnen_reg;
261 + if (timer >= FIRST_TIMER + timer_dev.number_of_timers)
264 + printk(KERN_INFO "request_timer(%d, 0x%08X, %lu)...",
265 + timer, flag, value);
267 + if (TIMER_FLAG_MASK_SIZE(flag) == TIMER_FLAG_16BIT)
272 + mutex_lock(&timer_dev.gptu_mutex);
277 + if (timer < FIRST_TIMER) {
279 + unsigned int shift;
280 + /* This takes care of TIMER1B which is the only choice for Voice TAPI system */
281 + unsigned int offset = TIMER2A;
284 + * Pick up a free timer.
286 + if (TIMER_FLAG_MASK_SIZE(flag) == TIMER_FLAG_16BIT) {
287 + mask = 1 << offset;
290 + mask = 3 << offset;
293 + for (timer = offset;
294 + timer < offset + timer_dev.number_of_timers;
295 + timer += shift, mask <<= shift)
296 + if (!(timer_dev.occupation & mask)) {
297 + timer_dev.occupation |= mask;
300 + if (timer >= offset + timer_dev.number_of_timers) {
301 + printk("failed![%d]\n", __LINE__);
302 + mutex_unlock(&timer_dev.gptu_mutex);
307 + register unsigned int mask;
310 + * Check if the requested timer is free.
312 + mask = (TIMER_FLAG_MASK_SIZE(flag) == TIMER_FLAG_16BIT ? 1 : 3) << timer;
313 + if ((timer_dev.occupation & mask)) {
314 + printk("failed![%d] mask %#x, timer_dev.occupation %#x\n",
315 + __LINE__, mask, timer_dev.occupation);
316 + mutex_unlock(&timer_dev.gptu_mutex);
319 + timer_dev.occupation |= mask;
325 + * Prepare control register value.
327 + switch (TIMER_FLAG_MASK_EDGE(flag)) {
329 + case TIMER_FLAG_NONE_EDGE:
330 + con_reg = GPTU_CON_EDGE_SET(0x00);
332 + case TIMER_FLAG_RISE_EDGE:
333 + con_reg = GPTU_CON_EDGE_SET(0x01);
335 + case TIMER_FLAG_FALL_EDGE:
336 + con_reg = GPTU_CON_EDGE_SET(0x02);
338 + case TIMER_FLAG_ANY_EDGE:
339 + con_reg = GPTU_CON_EDGE_SET(0x03);
342 + if (TIMER_FLAG_MASK_TYPE(flag) == TIMER_FLAG_TIMER)
344 + TIMER_FLAG_MASK_SRC(flag) ==
345 + TIMER_FLAG_EXT_SRC ? GPTU_CON_SRC_EXT_SET(1) :
346 + GPTU_CON_SRC_EXT_SET(0);
349 + TIMER_FLAG_MASK_SRC(flag) ==
350 + TIMER_FLAG_EXT_SRC ? GPTU_CON_SRC_EG_SET(1) :
351 + GPTU_CON_SRC_EG_SET(0);
353 + TIMER_FLAG_MASK_SYNC(flag) ==
354 + TIMER_FLAG_UNSYNC ? GPTU_CON_SYNC_SET(0) :
355 + GPTU_CON_SYNC_SET(1);
357 + TIMER_FLAG_MASK_INVERT(flag) ==
358 + TIMER_FLAG_REAL ? GPTU_CON_INV_SET(0) : GPTU_CON_INV_SET(1);
360 + TIMER_FLAG_MASK_SIZE(flag) ==
361 + TIMER_FLAG_16BIT ? GPTU_CON_EXT_SET(0) :
362 + GPTU_CON_EXT_SET(1);
364 + TIMER_FLAG_MASK_STOP(flag) ==
365 + TIMER_FLAG_ONCE ? GPTU_CON_STP_SET(1) : GPTU_CON_STP_SET(0);
367 + TIMER_FLAG_MASK_TYPE(flag) ==
368 + TIMER_FLAG_TIMER ? GPTU_CON_CNT_SET(0) :
369 + GPTU_CON_CNT_SET(1);
371 + TIMER_FLAG_MASK_DIR(flag) ==
372 + TIMER_FLAG_UP ? GPTU_CON_DIR_SET(1) : GPTU_CON_DIR_SET(0);
375 + * Fill up running data.
377 + timer_dev.timer[timer - FIRST_TIMER].flag = flag;
378 + timer_dev.timer[timer - FIRST_TIMER].arg1 = arg1;
379 + timer_dev.timer[timer - FIRST_TIMER].arg2 = arg2;
380 + if (TIMER_FLAG_MASK_SIZE(flag) != TIMER_FLAG_16BIT)
381 + timer_dev.timer[timer - FIRST_TIMER + 1].flag = flag;
384 + * Enable GPTU module.
386 + if (!timer_dev.f_gptu_on) {
388 + timer_dev.f_gptu_on = 1;
394 + if (TIMER_FLAG_MASK_HANDLE(flag) != TIMER_FLAG_NO_HANDLE) {
395 + if (TIMER_FLAG_MASK_HANDLE(flag) == TIMER_FLAG_SIGNAL)
396 + timer_dev.timer[timer - FIRST_TIMER].arg1 =
397 + (unsigned long) find_task_by_vpid((int) arg1);
399 + irnen_reg = 1 << (timer - FIRST_TIMER);
401 + if (TIMER_FLAG_MASK_HANDLE(flag) == TIMER_FLAG_SIGNAL
402 + || (TIMER_FLAG_MASK_HANDLE(flag) ==
403 + TIMER_FLAG_CALLBACK_IN_IRQ
404 + && timer_dev.timer[timer - FIRST_TIMER].arg1)) {
405 + enable_irq(timer_dev.timer[timer - FIRST_TIMER].irq);
406 + timer_dev.timer[timer - FIRST_TIMER].f_irq_on = 1;
412 + * Write config register, reload value and enable interrupt.
416 + *LQ_GPTU_CON(n, X) = con_reg;
417 + *LQ_GPTU_RELOAD(n, X) = value;
418 + /* printk("reload value = %d\n", (u32)value); */
419 + *LQ_GPTU_IRNEN |= irnen_reg;
421 + mutex_unlock(&timer_dev.gptu_mutex);
422 + printk("successful!\n");
425 +EXPORT_SYMBOL(lq_request_timer);
427 +int lq_free_timer(unsigned int timer)
433 + if (!timer_dev.f_gptu_on)
436 + if (timer < FIRST_TIMER || timer >= FIRST_TIMER + timer_dev.number_of_timers)
439 + mutex_lock(&timer_dev.gptu_mutex);
441 + flag = timer_dev.timer[timer - FIRST_TIMER].flag;
442 + if (TIMER_FLAG_MASK_SIZE(flag) != TIMER_FLAG_16BIT)
445 + mask = (TIMER_FLAG_MASK_SIZE(flag) == TIMER_FLAG_16BIT ? 1 : 3) << timer;
446 + if (((timer_dev.occupation & mask) ^ mask)) {
447 + mutex_unlock(&timer_dev.gptu_mutex);
454 + if (GPTU_CON_EN(n, X))
455 + *LQ_GPTU_RUN(n, X) = GPTU_RUN_CEN_SET(1);
457 + *LQ_GPTU_IRNEN &= ~GPTU_IRNEN_TC_SET(n, X, 1);
458 + *LQ_GPTU_IRNCR |= GPTU_IRNCR_TC_SET(n, X, 1);
460 + if (timer_dev.timer[timer - FIRST_TIMER].f_irq_on) {
461 + disable_irq(timer_dev.timer[timer - FIRST_TIMER].irq);
462 + timer_dev.timer[timer - FIRST_TIMER].f_irq_on = 0;
465 + timer_dev.occupation &= ~mask;
466 + if (!timer_dev.occupation && timer_dev.f_gptu_on) {
468 + timer_dev.f_gptu_on = 0;
471 + mutex_unlock(&timer_dev.gptu_mutex);
475 +EXPORT_SYMBOL(lq_free_timer);
477 +int lq_start_timer(unsigned int timer, int is_resume)
483 + if (!timer_dev.f_gptu_on)
486 + if (timer < FIRST_TIMER || timer >= FIRST_TIMER + timer_dev.number_of_timers)
489 + mutex_lock(&timer_dev.gptu_mutex);
491 + flag = timer_dev.timer[timer - FIRST_TIMER].flag;
492 + if (TIMER_FLAG_MASK_SIZE(flag) != TIMER_FLAG_16BIT)
495 + mask = (TIMER_FLAG_MASK_SIZE(flag) ==
496 + TIMER_FLAG_16BIT ? 1 : 3) << timer;
497 + if (((timer_dev.occupation & mask) ^ mask)) {
498 + mutex_unlock(&timer_dev.gptu_mutex);
505 + *LQ_GPTU_RUN(n, X) = GPTU_RUN_RL_SET(!is_resume) | GPTU_RUN_SEN_SET(1);
508 + mutex_unlock(&timer_dev.gptu_mutex);
512 +EXPORT_SYMBOL(lq_start_timer);
514 +int lq_stop_timer(unsigned int timer)
520 + if (!timer_dev.f_gptu_on)
523 + if (timer < FIRST_TIMER
524 + || timer >= FIRST_TIMER + timer_dev.number_of_timers)
527 + mutex_lock(&timer_dev.gptu_mutex);
529 + flag = timer_dev.timer[timer - FIRST_TIMER].flag;
530 + if (TIMER_FLAG_MASK_SIZE(flag) != TIMER_FLAG_16BIT)
533 + mask = (TIMER_FLAG_MASK_SIZE(flag) == TIMER_FLAG_16BIT ? 1 : 3) << timer;
534 + if (((timer_dev.occupation & mask) ^ mask)) {
535 + mutex_unlock(&timer_dev.gptu_mutex);
542 + *LQ_GPTU_RUN(n, X) = GPTU_RUN_CEN_SET(1);
544 + mutex_unlock(&timer_dev.gptu_mutex);
548 +EXPORT_SYMBOL(lq_stop_timer);
550 +int lq_reset_counter_flags(u32 timer, u32 flags)
552 + unsigned int oflag;
553 + unsigned int mask, con_reg;
556 + if (!timer_dev.f_gptu_on)
559 + if (timer < FIRST_TIMER || timer >= FIRST_TIMER + timer_dev.number_of_timers)
562 + mutex_lock(&timer_dev.gptu_mutex);
564 + oflag = timer_dev.timer[timer - FIRST_TIMER].flag;
565 + if (TIMER_FLAG_MASK_SIZE(oflag) != TIMER_FLAG_16BIT)
568 + mask = (TIMER_FLAG_MASK_SIZE(oflag) == TIMER_FLAG_16BIT ? 1 : 3) << timer;
569 + if (((timer_dev.occupation & mask) ^ mask)) {
570 + mutex_unlock(&timer_dev.gptu_mutex);
574 + switch (TIMER_FLAG_MASK_EDGE(flags)) {
576 + case TIMER_FLAG_NONE_EDGE:
577 + con_reg = GPTU_CON_EDGE_SET(0x00);
579 + case TIMER_FLAG_RISE_EDGE:
580 + con_reg = GPTU_CON_EDGE_SET(0x01);
582 + case TIMER_FLAG_FALL_EDGE:
583 + con_reg = GPTU_CON_EDGE_SET(0x02);
585 + case TIMER_FLAG_ANY_EDGE:
586 + con_reg = GPTU_CON_EDGE_SET(0x03);
589 + if (TIMER_FLAG_MASK_TYPE(flags) == TIMER_FLAG_TIMER)
590 + con_reg |= TIMER_FLAG_MASK_SRC(flags) == TIMER_FLAG_EXT_SRC ? GPTU_CON_SRC_EXT_SET(1) : GPTU_CON_SRC_EXT_SET(0);
592 + con_reg |= TIMER_FLAG_MASK_SRC(flags) == TIMER_FLAG_EXT_SRC ? GPTU_CON_SRC_EG_SET(1) : GPTU_CON_SRC_EG_SET(0);
593 + con_reg |= TIMER_FLAG_MASK_SYNC(flags) == TIMER_FLAG_UNSYNC ? GPTU_CON_SYNC_SET(0) : GPTU_CON_SYNC_SET(1);
594 + con_reg |= TIMER_FLAG_MASK_INVERT(flags) == TIMER_FLAG_REAL ? GPTU_CON_INV_SET(0) : GPTU_CON_INV_SET(1);
595 + con_reg |= TIMER_FLAG_MASK_SIZE(flags) == TIMER_FLAG_16BIT ? GPTU_CON_EXT_SET(0) : GPTU_CON_EXT_SET(1);
596 + con_reg |= TIMER_FLAG_MASK_STOP(flags) == TIMER_FLAG_ONCE ? GPTU_CON_STP_SET(1) : GPTU_CON_STP_SET(0);
597 + con_reg |= TIMER_FLAG_MASK_TYPE(flags) == TIMER_FLAG_TIMER ? GPTU_CON_CNT_SET(0) : GPTU_CON_CNT_SET(1);
598 + con_reg |= TIMER_FLAG_MASK_DIR(flags) == TIMER_FLAG_UP ? GPTU_CON_DIR_SET(1) : GPTU_CON_DIR_SET(0);
600 + timer_dev.timer[timer - FIRST_TIMER].flag = flags;
601 + if (TIMER_FLAG_MASK_SIZE(flags) != TIMER_FLAG_16BIT)
602 + timer_dev.timer[timer - FIRST_TIMER + 1].flag = flags;
607 + *LQ_GPTU_CON(n, X) = con_reg;
609 + mutex_unlock(&timer_dev.gptu_mutex);
612 +EXPORT_SYMBOL(lq_reset_counter_flags);
614 +int lq_get_count_value(unsigned int timer, unsigned long *value)
620 + if (!timer_dev.f_gptu_on)
623 + if (timer < FIRST_TIMER
624 + || timer >= FIRST_TIMER + timer_dev.number_of_timers)
627 + mutex_lock(&timer_dev.gptu_mutex);
629 + flag = timer_dev.timer[timer - FIRST_TIMER].flag;
630 + if (TIMER_FLAG_MASK_SIZE(flag) != TIMER_FLAG_16BIT)
633 + mask = (TIMER_FLAG_MASK_SIZE(flag) == TIMER_FLAG_16BIT ? 1 : 3) << timer;
634 + if (((timer_dev.occupation & mask) ^ mask)) {
635 + mutex_unlock(&timer_dev.gptu_mutex);
642 + *value = *LQ_GPTU_COUNT(n, X);
645 + mutex_unlock(&timer_dev.gptu_mutex);
649 +EXPORT_SYMBOL(lq_get_count_value);
651 +u32 lq_cal_divider(unsigned long freq)
653 + u64 module_freq, fpi = ltq_get_fpi_bus_clock(2);
654 + u32 clock_divider = 1;
655 + module_freq = fpi * 1000;
656 + do_div(module_freq, clock_divider * freq);
657 + return module_freq;
659 +EXPORT_SYMBOL(lq_cal_divider);
661 +int lq_set_timer(unsigned int timer, unsigned int freq, int is_cyclic,
662 + int is_ext_src, unsigned int handle_flag, unsigned long arg1,
663 + unsigned long arg2)
665 + unsigned long divider;
668 + divider = lq_cal_divider(freq);
671 + flag = ((divider & ~0xFFFF) ? TIMER_FLAG_32BIT : TIMER_FLAG_16BIT)
672 + | (is_cyclic ? TIMER_FLAG_CYCLIC : TIMER_FLAG_ONCE)
673 + | (is_ext_src ? TIMER_FLAG_EXT_SRC : TIMER_FLAG_INT_SRC)
674 + | TIMER_FLAG_TIMER | TIMER_FLAG_DOWN
675 + | TIMER_FLAG_MASK_HANDLE(handle_flag);
677 + printk(KERN_INFO "lq_set_timer(%d, %d), divider = %lu\n",
678 + timer, freq, divider);
679 + return lq_request_timer(timer, flag, divider, arg1, arg2);
681 +EXPORT_SYMBOL(lq_set_timer);
683 +int lq_set_counter(unsigned int timer, unsigned int flag, u32 reload,
684 + unsigned long arg1, unsigned long arg2)
686 + printk(KERN_INFO "lq_set_counter(%d, %#x, %d)\n", timer, flag, reload);
687 + return lq_request_timer(timer, flag, reload, arg1, arg2);
689 +EXPORT_SYMBOL(lq_set_counter);
691 +static long gptu_ioctl(struct file *file, unsigned int cmd,
695 + struct gptu_ioctl_param param;
697 + if (!access_ok(VERIFY_READ, arg, sizeof(struct gptu_ioctl_param)))
699 + copy_from_user(¶m, (void *) arg, sizeof(param));
701 + if ((((cmd == GPTU_REQUEST_TIMER || cmd == GPTU_SET_TIMER
702 + || GPTU_SET_COUNTER) && param.timer < 2)
703 + || cmd == GPTU_GET_COUNT_VALUE || cmd == GPTU_CALCULATE_DIVIDER)
704 + && !access_ok(VERIFY_WRITE, arg,
705 + sizeof(struct gptu_ioctl_param)))
709 + case GPTU_REQUEST_TIMER:
710 + ret = lq_request_timer(param.timer, param.flag, param.value,
711 + (unsigned long) param.pid,
712 + (unsigned long) param.sig);
714 + copy_to_user(&((struct gptu_ioctl_param *) arg)->
715 + timer, &ret, sizeof(&ret));
719 + case GPTU_FREE_TIMER:
720 + ret = lq_free_timer(param.timer);
722 + case GPTU_START_TIMER:
723 + ret = lq_start_timer(param.timer, param.flag);
725 + case GPTU_STOP_TIMER:
726 + ret = lq_stop_timer(param.timer);
728 + case GPTU_GET_COUNT_VALUE:
729 + ret = lq_get_count_value(param.timer, ¶m.value);
731 + copy_to_user(&((struct gptu_ioctl_param *) arg)->
732 + value, ¶m.value,
733 + sizeof(param.value));
735 + case GPTU_CALCULATE_DIVIDER:
736 + param.value = lq_cal_divider(param.value);
737 + if (param.value == 0)
740 + copy_to_user(&((struct gptu_ioctl_param *) arg)->
741 + value, ¶m.value,
742 + sizeof(param.value));
746 + case GPTU_SET_TIMER:
747 + ret = lq_set_timer(param.timer, param.value,
748 + TIMER_FLAG_MASK_STOP(param.flag) !=
749 + TIMER_FLAG_ONCE ? 1 : 0,
750 + TIMER_FLAG_MASK_SRC(param.flag) ==
751 + TIMER_FLAG_EXT_SRC ? 1 : 0,
752 + TIMER_FLAG_MASK_HANDLE(param.flag) ==
753 + TIMER_FLAG_SIGNAL ? TIMER_FLAG_SIGNAL :
754 + TIMER_FLAG_NO_HANDLE,
755 + (unsigned long) param.pid,
756 + (unsigned long) param.sig);
758 + copy_to_user(&((struct gptu_ioctl_param *) arg)->
759 + timer, &ret, sizeof(&ret));
763 + case GPTU_SET_COUNTER:
764 + lq_set_counter(param.timer, param.flag, param.value, 0, 0);
766 + copy_to_user(&((struct gptu_ioctl_param *) arg)->
767 + timer, &ret, sizeof(&ret));
778 +static int gptu_open(struct inode *inode, struct file *file)
783 +static int gptu_release(struct inode *inode, struct file *file)
788 +int __init lq_gptu_init(void)
793 + ltq_w32(0, LQ_GPTU_IRNEN);
794 + ltq_w32(0xfff, LQ_GPTU_IRNCR);
796 + memset(&timer_dev, 0, sizeof(timer_dev));
797 + mutex_init(&timer_dev.gptu_mutex);
800 + timer_dev.number_of_timers = GPTU_ID_CFG * 2;
802 + if (timer_dev.number_of_timers > MAX_NUM_OF_32BIT_TIMER_BLOCKS * 2)
803 + timer_dev.number_of_timers = MAX_NUM_OF_32BIT_TIMER_BLOCKS * 2;
804 + printk(KERN_INFO "gptu: totally %d 16-bit timers/counters\n", timer_dev.number_of_timers);
806 + ret = misc_register(&gptu_miscdev);
808 + printk(KERN_ERR "gptu: can't misc_register, get error %d\n", -ret);
811 + printk(KERN_INFO "gptu: misc_register on minor %d\n", gptu_miscdev.minor);
814 + for (i = 0; i < timer_dev.number_of_timers; i++) {
815 + ret = request_irq(TIMER_INTERRUPT + i, timer_irq_handler, IRQF_TIMER, gptu_miscdev.name, &timer_dev.timer[i]);
817 + for (; i >= 0; i--)
818 + free_irq(TIMER_INTERRUPT + i, &timer_dev.timer[i]);
819 + misc_deregister(&gptu_miscdev);
820 + printk(KERN_ERR "gptu: failed in requesting irq (%d), get error %d\n", i, -ret);
823 + timer_dev.timer[i].irq = TIMER_INTERRUPT + i;
824 + disable_irq(timer_dev.timer[i].irq);
825 + printk(KERN_INFO "gptu: succeeded to request irq %d\n", timer_dev.timer[i].irq);
832 +void __exit lq_gptu_exit(void)
836 + for (i = 0; i < timer_dev.number_of_timers; i++) {
837 + if (timer_dev.timer[i].f_irq_on)
838 + disable_irq(timer_dev.timer[i].irq);
839 + free_irq(timer_dev.timer[i].irq, &timer_dev.timer[i]);
842 + misc_deregister(&gptu_miscdev);
845 +module_init(lq_gptu_init);
846 +module_exit(lq_gptu_exit);
850 +++ b/arch/mips/include/asm/mach-lantiq/lantiq_timer.h
852 +#ifndef __DANUBE_GPTU_DEV_H__2005_07_26__10_19__
853 +#define __DANUBE_GPTU_DEV_H__2005_07_26__10_19__
856 +/******************************************************************************
857 + Copyright (c) 2002, Infineon Technologies. All rights reserved.
860 + Because the program is licensed free of charge, there is no warranty for
861 + the program, to the extent permitted by applicable law. Except when
862 + otherwise stated in writing the copyright holders and/or other parties
863 + provide the program "as is" without warranty of any kind, either
864 + expressed or implied, including, but not limited to, the implied
865 + warranties of merchantability and fitness for a particular purpose. The
866 + entire risk as to the quality and performance of the program is with
867 + you. should the program prove defective, you assume the cost of all
868 + necessary servicing, repair or correction.
870 + In no event unless required by applicable law or agreed to in writing
871 + will any copyright holder, or any other party who may modify and/or
872 + redistribute the program as permitted above, be liable to you for
873 + damages, including any general, special, incidental or consequential
874 + damages arising out of the use or inability to use the program
875 + (including but not limited to loss of data or data being rendered
876 + inaccurate or losses sustained by you or third parties or a failure of
877 + the program to operate with any other programs), even if such holder or
878 + other party has been advised of the possibility of such damages.
879 +******************************************************************************/
883 + * ####################################
885 + * ####################################
889 + * Available Timer/Counter Index
891 +#define TIMER(n, X) (n * 2 + (X ? 1 : 0))
892 +#define TIMER_ANY 0x00
893 +#define TIMER1A TIMER(1, 0)
894 +#define TIMER1B TIMER(1, 1)
895 +#define TIMER2A TIMER(2, 0)
896 +#define TIMER2B TIMER(2, 1)
897 +#define TIMER3A TIMER(3, 0)
898 +#define TIMER3B TIMER(3, 1)
901 + * Flag of Timer/Counter
902 + * These flags specify the way in which timer is configured.
904 +/* Bit size of timer/counter. */
905 +#define TIMER_FLAG_16BIT 0x0000
906 +#define TIMER_FLAG_32BIT 0x0001
907 +/* Switch between timer and counter. */
908 +#define TIMER_FLAG_TIMER 0x0000
909 +#define TIMER_FLAG_COUNTER 0x0002
910 +/* Stop or continue when overflowing/underflowing. */
911 +#define TIMER_FLAG_ONCE 0x0000
912 +#define TIMER_FLAG_CYCLIC 0x0004
913 +/* Count up or counter down. */
914 +#define TIMER_FLAG_UP 0x0000
915 +#define TIMER_FLAG_DOWN 0x0008
916 +/* Count on specific level or edge. */
917 +#define TIMER_FLAG_HIGH_LEVEL_SENSITIVE 0x0000
918 +#define TIMER_FLAG_LOW_LEVEL_SENSITIVE 0x0040
919 +#define TIMER_FLAG_RISE_EDGE 0x0010
920 +#define TIMER_FLAG_FALL_EDGE 0x0020
921 +#define TIMER_FLAG_ANY_EDGE 0x0030
922 +/* Signal is syncronous to module clock or not. */
923 +#define TIMER_FLAG_UNSYNC 0x0000
924 +#define TIMER_FLAG_SYNC 0x0080
925 +/* Different interrupt handle type. */
926 +#define TIMER_FLAG_NO_HANDLE 0x0000
927 +#if defined(__KERNEL__)
928 + #define TIMER_FLAG_CALLBACK_IN_IRQ 0x0100
929 +#endif // defined(__KERNEL__)
930 +#define TIMER_FLAG_SIGNAL 0x0300
931 +/* Internal clock source or external clock source */
932 +#define TIMER_FLAG_INT_SRC 0x0000
933 +#define TIMER_FLAG_EXT_SRC 0x1000
939 +#define GPTU_REQUEST_TIMER 0x01 /* General method to setup timer/counter. */
940 +#define GPTU_FREE_TIMER 0x02 /* Free timer/counter. */
941 +#define GPTU_START_TIMER 0x03 /* Start or resume timer/counter. */
942 +#define GPTU_STOP_TIMER 0x04 /* Suspend timer/counter. */
943 +#define GPTU_GET_COUNT_VALUE 0x05 /* Get current count value. */
944 +#define GPTU_CALCULATE_DIVIDER 0x06 /* Calculate timer divider from given freq.*/
945 +#define GPTU_SET_TIMER 0x07 /* Simplified method to setup timer. */
946 +#define GPTU_SET_COUNTER 0x08 /* Simplified method to setup counter. */
949 + * Data Type Used to Call ioctl
951 +struct gptu_ioctl_param {
952 + unsigned int timer; /* In command GPTU_REQUEST_TIMER, GPTU_SET_TIMER, and *
953 + * GPTU_SET_COUNTER, this field is ID of expected *
954 + * timer/counter. If it's zero, a timer/counter would *
955 + * be dynamically allocated and ID would be stored in *
957 + * In command GPTU_GET_COUNT_VALUE, this field is *
959 + * In other command, this field is ID of timer/counter *
961 + unsigned int flag; /* In command GPTU_REQUEST_TIMER, GPTU_SET_TIMER, and *
962 + * GPTU_SET_COUNTER, this field contains flags to *
963 + * specify how to configure timer/counter. *
964 + * In command GPTU_START_TIMER, zero indicate start *
965 + * and non-zero indicate resume timer/counter. *
966 + * In other command, this field is ignored. */
967 + unsigned long value; /* In command GPTU_REQUEST_TIMER, this field contains *
968 + * init/reload value. *
969 + * In command GPTU_SET_TIMER, this field contains *
970 + * frequency (0.001Hz) of timer. *
971 + * In command GPTU_GET_COUNT_VALUE, current count *
972 + * value would be stored in this field. *
973 + * In command GPTU_CALCULATE_DIVIDER, this field *
974 + * contains frequency wanted, and after calculation, *
975 + * divider would be stored in this field to overwrite *
977 + * In other command, this field is ignored. */
978 + int pid; /* In command GPTU_REQUEST_TIMER and GPTU_SET_TIMER, *
979 + * if signal is required, this field contains process *
980 + * ID to which signal would be sent. *
981 + * In other command, this field is ignored. */
982 + int sig; /* In command GPTU_REQUEST_TIMER and GPTU_SET_TIMER, *
983 + * if signal is required, this field contains signal *
984 + * number which would be sent. *
985 + * In other command, this field is ignored. */
989 + * ####################################
991 + * ####################################
993 +typedef void (*timer_callback)(unsigned long arg);
995 +extern int lq_request_timer(unsigned int, unsigned int, unsigned long, unsigned long, unsigned long);
996 +extern int lq_free_timer(unsigned int);
997 +extern int lq_start_timer(unsigned int, int);
998 +extern int lq_stop_timer(unsigned int);
999 +extern int lq_reset_counter_flags(u32 timer, u32 flags);
1000 +extern int lq_get_count_value(unsigned int, unsigned long *);
1001 +extern u32 lq_cal_divider(unsigned long);
1002 +extern int lq_set_timer(unsigned int, unsigned int, int, int, unsigned int, unsigned long, unsigned long);
1003 +extern int lq_set_counter(unsigned int timer, unsigned int flag,
1004 + u32 reload, unsigned long arg1, unsigned long arg2);
1006 +#endif /* __DANUBE_GPTU_DEV_H__2005_07_26__10_19__ */
1007 --- a/arch/mips/lantiq/xway/Makefile
1008 +++ b/arch/mips/lantiq/xway/Makefile
1010 -obj-y := prom.o sysctrl.o clk.o reset.o dma.o gptu.o dcdc.o
1011 +obj-y := prom.o sysctrl.o clk.o reset.o dma.o timer.o dcdc.o
1013 obj-$(CONFIG_XRX200_PHY_FW) += xrx200_phy_fw.o