1 From 45dbb232686978816e8148753e12f27caa2b2eb3 Mon Sep 17 00:00:00 2001
2 From: John Crispin <blogic@openwrt.org>
3 Date: Thu, 29 Sep 2011 17:16:38 +0200
4 Subject: [PATCH 17/24] MIPS: lantiq: adds GPTU driver
7 arch/mips/include/asm/mach-lantiq/lantiq_timer.h | 155 ++++
8 arch/mips/lantiq/xway/Makefile | 2 +-
9 arch/mips/lantiq/xway/timer.c | 830 ++++++++++++++++++++++
10 3 files changed, 986 insertions(+), 1 deletions(-)
11 create mode 100644 arch/mips/include/asm/mach-lantiq/lantiq_timer.h
12 create mode 100644 arch/mips/lantiq/xway/timer.c
14 diff --git a/arch/mips/include/asm/mach-lantiq/lantiq_timer.h b/arch/mips/include/asm/mach-lantiq/lantiq_timer.h
16 index 0000000..ef564ab
18 +++ b/arch/mips/include/asm/mach-lantiq/lantiq_timer.h
20 +#ifndef __DANUBE_GPTU_DEV_H__2005_07_26__10_19__
21 +#define __DANUBE_GPTU_DEV_H__2005_07_26__10_19__
24 +/******************************************************************************
25 + Copyright (c) 2002, Infineon Technologies. All rights reserved.
28 + Because the program is licensed free of charge, there is no warranty for
29 + the program, to the extent permitted by applicable law. Except when
30 + otherwise stated in writing the copyright holders and/or other parties
31 + provide the program "as is" without warranty of any kind, either
32 + expressed or implied, including, but not limited to, the implied
33 + warranties of merchantability and fitness for a particular purpose. The
34 + entire risk as to the quality and performance of the program is with
35 + you. should the program prove defective, you assume the cost of all
36 + necessary servicing, repair or correction.
38 + In no event unless required by applicable law or agreed to in writing
39 + will any copyright holder, or any other party who may modify and/or
40 + redistribute the program as permitted above, be liable to you for
41 + damages, including any general, special, incidental or consequential
42 + damages arising out of the use or inability to use the program
43 + (including but not limited to loss of data or data being rendered
44 + inaccurate or losses sustained by you or third parties or a failure of
45 + the program to operate with any other programs), even if such holder or
46 + other party has been advised of the possibility of such damages.
47 +******************************************************************************/
51 + * ####################################
53 + * ####################################
57 + * Available Timer/Counter Index
59 +#define TIMER(n, X) (n * 2 + (X ? 1 : 0))
60 +#define TIMER_ANY 0x00
61 +#define TIMER1A TIMER(1, 0)
62 +#define TIMER1B TIMER(1, 1)
63 +#define TIMER2A TIMER(2, 0)
64 +#define TIMER2B TIMER(2, 1)
65 +#define TIMER3A TIMER(3, 0)
66 +#define TIMER3B TIMER(3, 1)
69 + * Flag of Timer/Counter
70 + * These flags specify the way in which timer is configured.
72 +/* Bit size of timer/counter. */
73 +#define TIMER_FLAG_16BIT 0x0000
74 +#define TIMER_FLAG_32BIT 0x0001
75 +/* Switch between timer and counter. */
76 +#define TIMER_FLAG_TIMER 0x0000
77 +#define TIMER_FLAG_COUNTER 0x0002
78 +/* Stop or continue when overflowing/underflowing. */
79 +#define TIMER_FLAG_ONCE 0x0000
80 +#define TIMER_FLAG_CYCLIC 0x0004
81 +/* Count up or counter down. */
82 +#define TIMER_FLAG_UP 0x0000
83 +#define TIMER_FLAG_DOWN 0x0008
84 +/* Count on specific level or edge. */
85 +#define TIMER_FLAG_HIGH_LEVEL_SENSITIVE 0x0000
86 +#define TIMER_FLAG_LOW_LEVEL_SENSITIVE 0x0040
87 +#define TIMER_FLAG_RISE_EDGE 0x0010
88 +#define TIMER_FLAG_FALL_EDGE 0x0020
89 +#define TIMER_FLAG_ANY_EDGE 0x0030
90 +/* Signal is syncronous to module clock or not. */
91 +#define TIMER_FLAG_UNSYNC 0x0000
92 +#define TIMER_FLAG_SYNC 0x0080
93 +/* Different interrupt handle type. */
94 +#define TIMER_FLAG_NO_HANDLE 0x0000
95 +#if defined(__KERNEL__)
96 + #define TIMER_FLAG_CALLBACK_IN_IRQ 0x0100
97 +#endif // defined(__KERNEL__)
98 +#define TIMER_FLAG_SIGNAL 0x0300
99 +/* Internal clock source or external clock source */
100 +#define TIMER_FLAG_INT_SRC 0x0000
101 +#define TIMER_FLAG_EXT_SRC 0x1000
107 +#define GPTU_REQUEST_TIMER 0x01 /* General method to setup timer/counter. */
108 +#define GPTU_FREE_TIMER 0x02 /* Free timer/counter. */
109 +#define GPTU_START_TIMER 0x03 /* Start or resume timer/counter. */
110 +#define GPTU_STOP_TIMER 0x04 /* Suspend timer/counter. */
111 +#define GPTU_GET_COUNT_VALUE 0x05 /* Get current count value. */
112 +#define GPTU_CALCULATE_DIVIDER 0x06 /* Calculate timer divider from given freq.*/
113 +#define GPTU_SET_TIMER 0x07 /* Simplified method to setup timer. */
114 +#define GPTU_SET_COUNTER 0x08 /* Simplified method to setup counter. */
117 + * Data Type Used to Call ioctl
119 +struct gptu_ioctl_param {
120 + unsigned int timer; /* In command GPTU_REQUEST_TIMER, GPTU_SET_TIMER, and *
121 + * GPTU_SET_COUNTER, this field is ID of expected *
122 + * timer/counter. If it's zero, a timer/counter would *
123 + * be dynamically allocated and ID would be stored in *
125 + * In command GPTU_GET_COUNT_VALUE, this field is *
127 + * In other command, this field is ID of timer/counter *
129 + unsigned int flag; /* In command GPTU_REQUEST_TIMER, GPTU_SET_TIMER, and *
130 + * GPTU_SET_COUNTER, this field contains flags to *
131 + * specify how to configure timer/counter. *
132 + * In command GPTU_START_TIMER, zero indicate start *
133 + * and non-zero indicate resume timer/counter. *
134 + * In other command, this field is ignored. */
135 + unsigned long value; /* In command GPTU_REQUEST_TIMER, this field contains *
136 + * init/reload value. *
137 + * In command GPTU_SET_TIMER, this field contains *
138 + * frequency (0.001Hz) of timer. *
139 + * In command GPTU_GET_COUNT_VALUE, current count *
140 + * value would be stored in this field. *
141 + * In command GPTU_CALCULATE_DIVIDER, this field *
142 + * contains frequency wanted, and after calculation, *
143 + * divider would be stored in this field to overwrite *
145 + * In other command, this field is ignored. */
146 + int pid; /* In command GPTU_REQUEST_TIMER and GPTU_SET_TIMER, *
147 + * if signal is required, this field contains process *
148 + * ID to which signal would be sent. *
149 + * In other command, this field is ignored. */
150 + int sig; /* In command GPTU_REQUEST_TIMER and GPTU_SET_TIMER, *
151 + * if signal is required, this field contains signal *
152 + * number which would be sent. *
153 + * In other command, this field is ignored. */
157 + * ####################################
159 + * ####################################
161 +typedef void (*timer_callback)(unsigned long arg);
163 +extern int lq_request_timer(unsigned int, unsigned int, unsigned long, unsigned long, unsigned long);
164 +extern int lq_free_timer(unsigned int);
165 +extern int lq_start_timer(unsigned int, int);
166 +extern int lq_stop_timer(unsigned int);
167 +extern int lq_reset_counter_flags(u32 timer, u32 flags);
168 +extern int lq_get_count_value(unsigned int, unsigned long *);
169 +extern u32 lq_cal_divider(unsigned long);
170 +extern int lq_set_timer(unsigned int, unsigned int, int, int, unsigned int, unsigned long, unsigned long);
171 +extern int lq_set_counter(unsigned int timer, unsigned int flag,
172 + u32 reload, unsigned long arg1, unsigned long arg2);
174 +#endif /* __DANUBE_GPTU_DEV_H__2005_07_26__10_19__ */
175 diff --git a/arch/mips/lantiq/xway/Makefile b/arch/mips/lantiq/xway/Makefile
176 index ac7cc34..1f41239 100644
177 --- a/arch/mips/lantiq/xway/Makefile
178 +++ b/arch/mips/lantiq/xway/Makefile
180 -obj-y := sysctrl.o reset.o gpio.o gpio_stp.o gpio_ebu.o devices.o dma.o nand.o
181 +obj-y := sysctrl.o reset.o gpio.o gpio_stp.o gpio_ebu.o devices.o dma.o nand.o timer.o
183 obj-$(CONFIG_SOC_XWAY) += clk-xway.o prom-xway.o
184 obj-$(CONFIG_SOC_AMAZON_SE) += clk-ase.o prom-ase.o
185 diff --git a/arch/mips/lantiq/xway/timer.c b/arch/mips/lantiq/xway/timer.c
187 index 0000000..cac2ea8
189 +++ b/arch/mips/lantiq/xway/timer.c
191 +#include <linux/kernel.h>
192 +#include <linux/module.h>
193 +#include <linux/version.h>
194 +#include <linux/types.h>
195 +#include <linux/fs.h>
196 +#include <linux/miscdevice.h>
197 +#include <linux/init.h>
198 +#include <linux/uaccess.h>
199 +#include <linux/unistd.h>
200 +#include <linux/errno.h>
201 +#include <linux/interrupt.h>
202 +#include <linux/sched.h>
204 +#include <asm/irq.h>
205 +#include <asm/div64.h>
207 +#include <lantiq_soc.h>
208 +#include <lantiq_irq.h>
209 +#include <lantiq_timer.h>
211 +#define MAX_NUM_OF_32BIT_TIMER_BLOCKS 6
214 +#define FIRST_TIMER TIMER1A
216 +#define FIRST_TIMER 2
220 + * GPTC divider is set or not.
222 +#define GPTU_CLC_RMC_IS_SET 0
225 + * Timer Interrupt (IRQ)
227 +/* Must be adjusted when ICU driver is available */
228 +#define TIMER_INTERRUPT (INT_NUM_IM3_IRL0 + 22)
233 +#define GET_BITS(x, msb, lsb) \
234 + (((x) & ((1 << ((msb) + 1)) - 1)) >> (lsb))
235 +#define SET_BITS(x, msb, lsb, value) \
236 + (((x) & ~(((1 << ((msb) + 1)) - 1) ^ ((1 << (lsb)) - 1))) | \
237 + (((value) & ((1 << (1 + (msb) - (lsb))) - 1)) << (lsb)))
240 + * GPTU Register Mapping
242 +#define LQ_GPTU (KSEG1 + 0x1E100A00)
243 +#define LQ_GPTU_CLC ((volatile u32 *)(LQ_GPTU + 0x0000))
244 +#define LQ_GPTU_ID ((volatile u32 *)(LQ_GPTU + 0x0008))
245 +#define LQ_GPTU_CON(n, X) ((volatile u32 *)(LQ_GPTU + 0x0010 + ((X) * 4) + ((n) - 1) * 0x0020)) /* X must be either A or B */
246 +#define LQ_GPTU_RUN(n, X) ((volatile u32 *)(LQ_GPTU + 0x0018 + ((X) * 4) + ((n) - 1) * 0x0020)) /* X must be either A or B */
247 +#define LQ_GPTU_RELOAD(n, X) ((volatile u32 *)(LQ_GPTU + 0x0020 + ((X) * 4) + ((n) - 1) * 0x0020)) /* X must be either A or B */
248 +#define LQ_GPTU_COUNT(n, X) ((volatile u32 *)(LQ_GPTU + 0x0028 + ((X) * 4) + ((n) - 1) * 0x0020)) /* X must be either A or B */
249 +#define LQ_GPTU_IRNEN ((volatile u32 *)(LQ_GPTU + 0x00F4))
250 +#define LQ_GPTU_IRNICR ((volatile u32 *)(LQ_GPTU + 0x00F8))
251 +#define LQ_GPTU_IRNCR ((volatile u32 *)(LQ_GPTU + 0x00FC))
254 + * Clock Control Register
256 +#define GPTU_CLC_SMC GET_BITS(*LQ_GPTU_CLC, 23, 16)
257 +#define GPTU_CLC_RMC GET_BITS(*LQ_GPTU_CLC, 15, 8)
258 +#define GPTU_CLC_FSOE (*LQ_GPTU_CLC & (1 << 5))
259 +#define GPTU_CLC_EDIS (*LQ_GPTU_CLC & (1 << 3))
260 +#define GPTU_CLC_SPEN (*LQ_GPTU_CLC & (1 << 2))
261 +#define GPTU_CLC_DISS (*LQ_GPTU_CLC & (1 << 1))
262 +#define GPTU_CLC_DISR (*LQ_GPTU_CLC & (1 << 0))
264 +#define GPTU_CLC_SMC_SET(value) SET_BITS(0, 23, 16, (value))
265 +#define GPTU_CLC_RMC_SET(value) SET_BITS(0, 15, 8, (value))
266 +#define GPTU_CLC_FSOE_SET(value) ((value) ? (1 << 5) : 0)
267 +#define GPTU_CLC_SBWE_SET(value) ((value) ? (1 << 4) : 0)
268 +#define GPTU_CLC_EDIS_SET(value) ((value) ? (1 << 3) : 0)
269 +#define GPTU_CLC_SPEN_SET(value) ((value) ? (1 << 2) : 0)
270 +#define GPTU_CLC_DISR_SET(value) ((value) ? (1 << 0) : 0)
275 +#define GPTU_ID_ID GET_BITS(*LQ_GPTU_ID, 15, 8)
276 +#define GPTU_ID_CFG GET_BITS(*LQ_GPTU_ID, 7, 5)
277 +#define GPTU_ID_REV GET_BITS(*LQ_GPTU_ID, 4, 0)
280 + * Control Register of Timer/Counter nX
281 + * n is the index of block (1 based index)
282 + * X is either A or B
284 +#define GPTU_CON_SRC_EG(n, X) (*LQ_GPTU_CON(n, X) & (1 << 10))
285 +#define GPTU_CON_SRC_EXT(n, X) (*LQ_GPTU_CON(n, X) & (1 << 9))
286 +#define GPTU_CON_SYNC(n, X) (*LQ_GPTU_CON(n, X) & (1 << 8))
287 +#define GPTU_CON_EDGE(n, X) GET_BITS(*LQ_GPTU_CON(n, X), 7, 6)
288 +#define GPTU_CON_INV(n, X) (*LQ_GPTU_CON(n, X) & (1 << 5))
289 +#define GPTU_CON_EXT(n, X) (*LQ_GPTU_CON(n, A) & (1 << 4)) /* Timer/Counter B does not have this bit */
290 +#define GPTU_CON_STP(n, X) (*LQ_GPTU_CON(n, X) & (1 << 3))
291 +#define GPTU_CON_CNT(n, X) (*LQ_GPTU_CON(n, X) & (1 << 2))
292 +#define GPTU_CON_DIR(n, X) (*LQ_GPTU_CON(n, X) & (1 << 1))
293 +#define GPTU_CON_EN(n, X) (*LQ_GPTU_CON(n, X) & (1 << 0))
295 +#define GPTU_CON_SRC_EG_SET(value) ((value) ? 0 : (1 << 10))
296 +#define GPTU_CON_SRC_EXT_SET(value) ((value) ? (1 << 9) : 0)
297 +#define GPTU_CON_SYNC_SET(value) ((value) ? (1 << 8) : 0)
298 +#define GPTU_CON_EDGE_SET(value) SET_BITS(0, 7, 6, (value))
299 +#define GPTU_CON_INV_SET(value) ((value) ? (1 << 5) : 0)
300 +#define GPTU_CON_EXT_SET(value) ((value) ? (1 << 4) : 0)
301 +#define GPTU_CON_STP_SET(value) ((value) ? (1 << 3) : 0)
302 +#define GPTU_CON_CNT_SET(value) ((value) ? (1 << 2) : 0)
303 +#define GPTU_CON_DIR_SET(value) ((value) ? (1 << 1) : 0)
305 +#define GPTU_RUN_RL_SET(value) ((value) ? (1 << 2) : 0)
306 +#define GPTU_RUN_CEN_SET(value) ((value) ? (1 << 1) : 0)
307 +#define GPTU_RUN_SEN_SET(value) ((value) ? (1 << 0) : 0)
309 +#define GPTU_IRNEN_TC_SET(n, X, value) ((value) ? (1 << (((n) - 1) * 2 + (X))) : 0)
310 +#define GPTU_IRNCR_TC_SET(n, X, value) ((value) ? (1 << (((n) - 1) * 2 + (X))) : 0)
312 +#define TIMER_FLAG_MASK_SIZE(x) (x & 0x0001)
313 +#define TIMER_FLAG_MASK_TYPE(x) (x & 0x0002)
314 +#define TIMER_FLAG_MASK_STOP(x) (x & 0x0004)
315 +#define TIMER_FLAG_MASK_DIR(x) (x & 0x0008)
316 +#define TIMER_FLAG_NONE_EDGE 0x0000
317 +#define TIMER_FLAG_MASK_EDGE(x) (x & 0x0030)
318 +#define TIMER_FLAG_REAL 0x0000
319 +#define TIMER_FLAG_INVERT 0x0040
320 +#define TIMER_FLAG_MASK_INVERT(x) (x & 0x0040)
321 +#define TIMER_FLAG_MASK_TRIGGER(x) (x & 0x0070)
322 +#define TIMER_FLAG_MASK_SYNC(x) (x & 0x0080)
323 +#define TIMER_FLAG_CALLBACK_IN_HB 0x0200
324 +#define TIMER_FLAG_MASK_HANDLE(x) (x & 0x0300)
325 +#define TIMER_FLAG_MASK_SRC(x) (x & 0x1000)
327 +struct timer_dev_timer {
328 + unsigned int f_irq_on;
331 + unsigned long arg1;
332 + unsigned long arg2;
336 + struct mutex gptu_mutex;
337 + unsigned int number_of_timers;
338 + unsigned int occupation;
339 + unsigned int f_gptu_on;
340 + struct timer_dev_timer timer[MAX_NUM_OF_32BIT_TIMER_BLOCKS * 2];
343 +unsigned int ltq_get_fpi_bus_clock(int fpi);
345 +static long gptu_ioctl(struct file *, unsigned int, unsigned long);
346 +static int gptu_open(struct inode *, struct file *);
347 +static int gptu_release(struct inode *, struct file *);
349 +static struct file_operations gptu_fops = {
350 + .owner = THIS_MODULE,
351 + .unlocked_ioctl = gptu_ioctl,
353 + .release = gptu_release
356 +static struct miscdevice gptu_miscdev = {
357 + .minor = MISC_DYNAMIC_MINOR,
359 + .fops = &gptu_fops,
362 +static struct timer_dev timer_dev;
364 +static irqreturn_t timer_irq_handler(int irq, void *p)
366 + unsigned int timer;
368 + struct timer_dev_timer *dev_timer = (struct timer_dev_timer *)p;
370 + timer = irq - TIMER_INTERRUPT;
371 + if (timer < timer_dev.number_of_timers
372 + && dev_timer == &timer_dev.timer[timer]) {
373 + /* Clear interrupt. */
374 + ltq_w32(1 << timer, LQ_GPTU_IRNCR);
376 + /* Call user hanler or signal. */
377 + flag = dev_timer->flag;
378 + if (!(timer & 0x01)
379 + || TIMER_FLAG_MASK_SIZE(flag) == TIMER_FLAG_16BIT) {
380 + /* 16-bit timer or timer A of 32-bit timer */
381 + switch (TIMER_FLAG_MASK_HANDLE(flag)) {
382 + case TIMER_FLAG_CALLBACK_IN_IRQ:
383 + case TIMER_FLAG_CALLBACK_IN_HB:
384 + if (dev_timer->arg1)
385 + (*(timer_callback)dev_timer->arg1)(dev_timer->arg2);
387 + case TIMER_FLAG_SIGNAL:
388 + send_sig((int)dev_timer->arg2, (struct task_struct *)dev_timer->arg1, 0);
393 + return IRQ_HANDLED;
396 +static inline void lq_enable_gptu(void)
398 + ltq_pmu_enable(PMU_GPT);
400 + /* Set divider as 1, disable write protection for SPEN, enable module. */
402 + GPTU_CLC_SMC_SET(0x00) |
403 + GPTU_CLC_RMC_SET(0x01) |
404 + GPTU_CLC_FSOE_SET(0) |
405 + GPTU_CLC_SBWE_SET(1) |
406 + GPTU_CLC_EDIS_SET(0) |
407 + GPTU_CLC_SPEN_SET(0) |
408 + GPTU_CLC_DISR_SET(0);
411 +static inline void lq_disable_gptu(void)
413 + ltq_w32(0x00, LQ_GPTU_IRNEN);
414 + ltq_w32(0xfff, LQ_GPTU_IRNCR);
416 + /* Set divider as 0, enable write protection for SPEN, disable module. */
418 + GPTU_CLC_SMC_SET(0x00) |
419 + GPTU_CLC_RMC_SET(0x00) |
420 + GPTU_CLC_FSOE_SET(0) |
421 + GPTU_CLC_SBWE_SET(0) |
422 + GPTU_CLC_EDIS_SET(0) |
423 + GPTU_CLC_SPEN_SET(0) |
424 + GPTU_CLC_DISR_SET(1);
426 + ltq_pmu_disable(PMU_GPT);
429 +int lq_request_timer(unsigned int timer, unsigned int flag,
430 + unsigned long value, unsigned long arg1, unsigned long arg2)
433 + unsigned int con_reg, irnen_reg;
436 + if (timer >= FIRST_TIMER + timer_dev.number_of_timers)
439 + printk(KERN_INFO "request_timer(%d, 0x%08X, %lu)...",
440 + timer, flag, value);
442 + if (TIMER_FLAG_MASK_SIZE(flag) == TIMER_FLAG_16BIT)
447 + mutex_lock(&timer_dev.gptu_mutex);
452 + if (timer < FIRST_TIMER) {
454 + unsigned int shift;
455 + /* This takes care of TIMER1B which is the only choice for Voice TAPI system */
456 + unsigned int offset = TIMER2A;
459 + * Pick up a free timer.
461 + if (TIMER_FLAG_MASK_SIZE(flag) == TIMER_FLAG_16BIT) {
462 + mask = 1 << offset;
465 + mask = 3 << offset;
468 + for (timer = offset;
469 + timer < offset + timer_dev.number_of_timers;
470 + timer += shift, mask <<= shift)
471 + if (!(timer_dev.occupation & mask)) {
472 + timer_dev.occupation |= mask;
475 + if (timer >= offset + timer_dev.number_of_timers) {
476 + printk("failed![%d]\n", __LINE__);
477 + mutex_unlock(&timer_dev.gptu_mutex);
482 + register unsigned int mask;
485 + * Check if the requested timer is free.
487 + mask = (TIMER_FLAG_MASK_SIZE(flag) == TIMER_FLAG_16BIT ? 1 : 3) << timer;
488 + if ((timer_dev.occupation & mask)) {
489 + printk("failed![%d] mask %#x, timer_dev.occupation %#x\n",
490 + __LINE__, mask, timer_dev.occupation);
491 + mutex_unlock(&timer_dev.gptu_mutex);
494 + timer_dev.occupation |= mask;
500 + * Prepare control register value.
502 + switch (TIMER_FLAG_MASK_EDGE(flag)) {
504 + case TIMER_FLAG_NONE_EDGE:
505 + con_reg = GPTU_CON_EDGE_SET(0x00);
507 + case TIMER_FLAG_RISE_EDGE:
508 + con_reg = GPTU_CON_EDGE_SET(0x01);
510 + case TIMER_FLAG_FALL_EDGE:
511 + con_reg = GPTU_CON_EDGE_SET(0x02);
513 + case TIMER_FLAG_ANY_EDGE:
514 + con_reg = GPTU_CON_EDGE_SET(0x03);
517 + if (TIMER_FLAG_MASK_TYPE(flag) == TIMER_FLAG_TIMER)
519 + TIMER_FLAG_MASK_SRC(flag) ==
520 + TIMER_FLAG_EXT_SRC ? GPTU_CON_SRC_EXT_SET(1) :
521 + GPTU_CON_SRC_EXT_SET(0);
524 + TIMER_FLAG_MASK_SRC(flag) ==
525 + TIMER_FLAG_EXT_SRC ? GPTU_CON_SRC_EG_SET(1) :
526 + GPTU_CON_SRC_EG_SET(0);
528 + TIMER_FLAG_MASK_SYNC(flag) ==
529 + TIMER_FLAG_UNSYNC ? GPTU_CON_SYNC_SET(0) :
530 + GPTU_CON_SYNC_SET(1);
532 + TIMER_FLAG_MASK_INVERT(flag) ==
533 + TIMER_FLAG_REAL ? GPTU_CON_INV_SET(0) : GPTU_CON_INV_SET(1);
535 + TIMER_FLAG_MASK_SIZE(flag) ==
536 + TIMER_FLAG_16BIT ? GPTU_CON_EXT_SET(0) :
537 + GPTU_CON_EXT_SET(1);
539 + TIMER_FLAG_MASK_STOP(flag) ==
540 + TIMER_FLAG_ONCE ? GPTU_CON_STP_SET(1) : GPTU_CON_STP_SET(0);
542 + TIMER_FLAG_MASK_TYPE(flag) ==
543 + TIMER_FLAG_TIMER ? GPTU_CON_CNT_SET(0) :
544 + GPTU_CON_CNT_SET(1);
546 + TIMER_FLAG_MASK_DIR(flag) ==
547 + TIMER_FLAG_UP ? GPTU_CON_DIR_SET(1) : GPTU_CON_DIR_SET(0);
550 + * Fill up running data.
552 + timer_dev.timer[timer - FIRST_TIMER].flag = flag;
553 + timer_dev.timer[timer - FIRST_TIMER].arg1 = arg1;
554 + timer_dev.timer[timer - FIRST_TIMER].arg2 = arg2;
555 + if (TIMER_FLAG_MASK_SIZE(flag) != TIMER_FLAG_16BIT)
556 + timer_dev.timer[timer - FIRST_TIMER + 1].flag = flag;
559 + * Enable GPTU module.
561 + if (!timer_dev.f_gptu_on) {
563 + timer_dev.f_gptu_on = 1;
569 + if (TIMER_FLAG_MASK_HANDLE(flag) != TIMER_FLAG_NO_HANDLE) {
570 + if (TIMER_FLAG_MASK_HANDLE(flag) == TIMER_FLAG_SIGNAL)
571 + timer_dev.timer[timer - FIRST_TIMER].arg1 =
572 + (unsigned long) find_task_by_vpid((int) arg1);
574 + irnen_reg = 1 << (timer - FIRST_TIMER);
576 + if (TIMER_FLAG_MASK_HANDLE(flag) == TIMER_FLAG_SIGNAL
577 + || (TIMER_FLAG_MASK_HANDLE(flag) ==
578 + TIMER_FLAG_CALLBACK_IN_IRQ
579 + && timer_dev.timer[timer - FIRST_TIMER].arg1)) {
580 + enable_irq(timer_dev.timer[timer - FIRST_TIMER].irq);
581 + timer_dev.timer[timer - FIRST_TIMER].f_irq_on = 1;
587 + * Write config register, reload value and enable interrupt.
591 + *LQ_GPTU_CON(n, X) = con_reg;
592 + *LQ_GPTU_RELOAD(n, X) = value;
593 + /* printk("reload value = %d\n", (u32)value); */
594 + *LQ_GPTU_IRNEN |= irnen_reg;
596 + mutex_unlock(&timer_dev.gptu_mutex);
597 + printk("successful!\n");
600 +EXPORT_SYMBOL(lq_request_timer);
602 +int lq_free_timer(unsigned int timer)
608 + if (!timer_dev.f_gptu_on)
611 + if (timer < FIRST_TIMER || timer >= FIRST_TIMER + timer_dev.number_of_timers)
614 + mutex_lock(&timer_dev.gptu_mutex);
616 + flag = timer_dev.timer[timer - FIRST_TIMER].flag;
617 + if (TIMER_FLAG_MASK_SIZE(flag) != TIMER_FLAG_16BIT)
620 + mask = (TIMER_FLAG_MASK_SIZE(flag) == TIMER_FLAG_16BIT ? 1 : 3) << timer;
621 + if (((timer_dev.occupation & mask) ^ mask)) {
622 + mutex_unlock(&timer_dev.gptu_mutex);
629 + if (GPTU_CON_EN(n, X))
630 + *LQ_GPTU_RUN(n, X) = GPTU_RUN_CEN_SET(1);
632 + *LQ_GPTU_IRNEN &= ~GPTU_IRNEN_TC_SET(n, X, 1);
633 + *LQ_GPTU_IRNCR |= GPTU_IRNCR_TC_SET(n, X, 1);
635 + if (timer_dev.timer[timer - FIRST_TIMER].f_irq_on) {
636 + disable_irq(timer_dev.timer[timer - FIRST_TIMER].irq);
637 + timer_dev.timer[timer - FIRST_TIMER].f_irq_on = 0;
640 + timer_dev.occupation &= ~mask;
641 + if (!timer_dev.occupation && timer_dev.f_gptu_on) {
643 + timer_dev.f_gptu_on = 0;
646 + mutex_unlock(&timer_dev.gptu_mutex);
650 +EXPORT_SYMBOL(lq_free_timer);
652 +int lq_start_timer(unsigned int timer, int is_resume)
658 + if (!timer_dev.f_gptu_on)
661 + if (timer < FIRST_TIMER || timer >= FIRST_TIMER + timer_dev.number_of_timers)
664 + mutex_lock(&timer_dev.gptu_mutex);
666 + flag = timer_dev.timer[timer - FIRST_TIMER].flag;
667 + if (TIMER_FLAG_MASK_SIZE(flag) != TIMER_FLAG_16BIT)
670 + mask = (TIMER_FLAG_MASK_SIZE(flag) ==
671 + TIMER_FLAG_16BIT ? 1 : 3) << timer;
672 + if (((timer_dev.occupation & mask) ^ mask)) {
673 + mutex_unlock(&timer_dev.gptu_mutex);
680 + *LQ_GPTU_RUN(n, X) = GPTU_RUN_RL_SET(!is_resume) | GPTU_RUN_SEN_SET(1);
682 + mutex_unlock(&timer_dev.gptu_mutex);
686 +EXPORT_SYMBOL(lq_start_timer);
688 +int lq_stop_timer(unsigned int timer)
694 + if (!timer_dev.f_gptu_on)
697 + if (timer < FIRST_TIMER
698 + || timer >= FIRST_TIMER + timer_dev.number_of_timers)
701 + mutex_lock(&timer_dev.gptu_mutex);
703 + flag = timer_dev.timer[timer - FIRST_TIMER].flag;
704 + if (TIMER_FLAG_MASK_SIZE(flag) != TIMER_FLAG_16BIT)
707 + mask = (TIMER_FLAG_MASK_SIZE(flag) == TIMER_FLAG_16BIT ? 1 : 3) << timer;
708 + if (((timer_dev.occupation & mask) ^ mask)) {
709 + mutex_unlock(&timer_dev.gptu_mutex);
716 + *LQ_GPTU_RUN(n, X) = GPTU_RUN_CEN_SET(1);
718 + mutex_unlock(&timer_dev.gptu_mutex);
722 +EXPORT_SYMBOL(lq_stop_timer);
724 +int lq_reset_counter_flags(u32 timer, u32 flags)
726 + unsigned int oflag;
727 + unsigned int mask, con_reg;
730 + if (!timer_dev.f_gptu_on)
733 + if (timer < FIRST_TIMER || timer >= FIRST_TIMER + timer_dev.number_of_timers)
736 + mutex_lock(&timer_dev.gptu_mutex);
738 + oflag = timer_dev.timer[timer - FIRST_TIMER].flag;
739 + if (TIMER_FLAG_MASK_SIZE(oflag) != TIMER_FLAG_16BIT)
742 + mask = (TIMER_FLAG_MASK_SIZE(oflag) == TIMER_FLAG_16BIT ? 1 : 3) << timer;
743 + if (((timer_dev.occupation & mask) ^ mask)) {
744 + mutex_unlock(&timer_dev.gptu_mutex);
748 + switch (TIMER_FLAG_MASK_EDGE(flags)) {
750 + case TIMER_FLAG_NONE_EDGE:
751 + con_reg = GPTU_CON_EDGE_SET(0x00);
753 + case TIMER_FLAG_RISE_EDGE:
754 + con_reg = GPTU_CON_EDGE_SET(0x01);
756 + case TIMER_FLAG_FALL_EDGE:
757 + con_reg = GPTU_CON_EDGE_SET(0x02);
759 + case TIMER_FLAG_ANY_EDGE:
760 + con_reg = GPTU_CON_EDGE_SET(0x03);
763 + if (TIMER_FLAG_MASK_TYPE(flags) == TIMER_FLAG_TIMER)
764 + con_reg |= TIMER_FLAG_MASK_SRC(flags) == TIMER_FLAG_EXT_SRC ? GPTU_CON_SRC_EXT_SET(1) : GPTU_CON_SRC_EXT_SET(0);
766 + con_reg |= TIMER_FLAG_MASK_SRC(flags) == TIMER_FLAG_EXT_SRC ? GPTU_CON_SRC_EG_SET(1) : GPTU_CON_SRC_EG_SET(0);
767 + con_reg |= TIMER_FLAG_MASK_SYNC(flags) == TIMER_FLAG_UNSYNC ? GPTU_CON_SYNC_SET(0) : GPTU_CON_SYNC_SET(1);
768 + con_reg |= TIMER_FLAG_MASK_INVERT(flags) == TIMER_FLAG_REAL ? GPTU_CON_INV_SET(0) : GPTU_CON_INV_SET(1);
769 + con_reg |= TIMER_FLAG_MASK_SIZE(flags) == TIMER_FLAG_16BIT ? GPTU_CON_EXT_SET(0) : GPTU_CON_EXT_SET(1);
770 + con_reg |= TIMER_FLAG_MASK_STOP(flags) == TIMER_FLAG_ONCE ? GPTU_CON_STP_SET(1) : GPTU_CON_STP_SET(0);
771 + con_reg |= TIMER_FLAG_MASK_TYPE(flags) == TIMER_FLAG_TIMER ? GPTU_CON_CNT_SET(0) : GPTU_CON_CNT_SET(1);
772 + con_reg |= TIMER_FLAG_MASK_DIR(flags) == TIMER_FLAG_UP ? GPTU_CON_DIR_SET(1) : GPTU_CON_DIR_SET(0);
774 + timer_dev.timer[timer - FIRST_TIMER].flag = flags;
775 + if (TIMER_FLAG_MASK_SIZE(flags) != TIMER_FLAG_16BIT)
776 + timer_dev.timer[timer - FIRST_TIMER + 1].flag = flags;
781 + *LQ_GPTU_CON(n, X) = con_reg;
783 + printk(KERN_INFO "[%s]: counter%d oflags %#x, nflags %#x, GPTU_CON %#x\n", __func__, timer, oflag, flags, *LQ_GPTU_CON(n, X));
784 + mutex_unlock(&timer_dev.gptu_mutex);
787 +EXPORT_SYMBOL(lq_reset_counter_flags);
789 +int lq_get_count_value(unsigned int timer, unsigned long *value)
795 + if (!timer_dev.f_gptu_on)
798 + if (timer < FIRST_TIMER
799 + || timer >= FIRST_TIMER + timer_dev.number_of_timers)
802 + mutex_lock(&timer_dev.gptu_mutex);
804 + flag = timer_dev.timer[timer - FIRST_TIMER].flag;
805 + if (TIMER_FLAG_MASK_SIZE(flag) != TIMER_FLAG_16BIT)
808 + mask = (TIMER_FLAG_MASK_SIZE(flag) == TIMER_FLAG_16BIT ? 1 : 3) << timer;
809 + if (((timer_dev.occupation & mask) ^ mask)) {
810 + mutex_unlock(&timer_dev.gptu_mutex);
817 + *value = *LQ_GPTU_COUNT(n, X);
819 + mutex_unlock(&timer_dev.gptu_mutex);
823 +EXPORT_SYMBOL(lq_get_count_value);
825 +u32 lq_cal_divider(unsigned long freq)
827 + u64 module_freq, fpi = ltq_get_fpi_bus_clock(2);
828 + u32 clock_divider = 1;
829 + module_freq = fpi * 1000;
830 + do_div(module_freq, clock_divider * freq);
831 + return module_freq;
833 +EXPORT_SYMBOL(lq_cal_divider);
835 +int lq_set_timer(unsigned int timer, unsigned int freq, int is_cyclic,
836 + int is_ext_src, unsigned int handle_flag, unsigned long arg1,
837 + unsigned long arg2)
839 + unsigned long divider;
842 + divider = lq_cal_divider(freq);
845 + flag = ((divider & ~0xFFFF) ? TIMER_FLAG_32BIT : TIMER_FLAG_16BIT)
846 + | (is_cyclic ? TIMER_FLAG_CYCLIC : TIMER_FLAG_ONCE)
847 + | (is_ext_src ? TIMER_FLAG_EXT_SRC : TIMER_FLAG_INT_SRC)
848 + | TIMER_FLAG_TIMER | TIMER_FLAG_DOWN
849 + | TIMER_FLAG_MASK_HANDLE(handle_flag);
851 + printk(KERN_INFO "lq_set_timer(%d, %d), divider = %lu\n",
852 + timer, freq, divider);
853 + return lq_request_timer(timer, flag, divider, arg1, arg2);
855 +EXPORT_SYMBOL(lq_set_timer);
857 +int lq_set_counter(unsigned int timer, unsigned int flag, u32 reload,
858 + unsigned long arg1, unsigned long arg2)
860 + printk(KERN_INFO "lq_set_counter(%d, %#x, %d)\n", timer, flag, reload);
861 + return lq_request_timer(timer, flag, reload, arg1, arg2);
863 +EXPORT_SYMBOL(lq_set_counter);
865 +static long gptu_ioctl(struct file *file, unsigned int cmd,
869 + struct gptu_ioctl_param param;
871 + if (!access_ok(VERIFY_READ, arg, sizeof(struct gptu_ioctl_param)))
873 + copy_from_user(¶m, (void *) arg, sizeof(param));
875 + if ((((cmd == GPTU_REQUEST_TIMER || cmd == GPTU_SET_TIMER
876 + || GPTU_SET_COUNTER) && param.timer < 2)
877 + || cmd == GPTU_GET_COUNT_VALUE || cmd == GPTU_CALCULATE_DIVIDER)
878 + && !access_ok(VERIFY_WRITE, arg,
879 + sizeof(struct gptu_ioctl_param)))
883 + case GPTU_REQUEST_TIMER:
884 + ret = lq_request_timer(param.timer, param.flag, param.value,
885 + (unsigned long) param.pid,
886 + (unsigned long) param.sig);
888 + copy_to_user(&((struct gptu_ioctl_param *) arg)->
889 + timer, &ret, sizeof(&ret));
893 + case GPTU_FREE_TIMER:
894 + ret = lq_free_timer(param.timer);
896 + case GPTU_START_TIMER:
897 + ret = lq_start_timer(param.timer, param.flag);
899 + case GPTU_STOP_TIMER:
900 + ret = lq_stop_timer(param.timer);
902 + case GPTU_GET_COUNT_VALUE:
903 + ret = lq_get_count_value(param.timer, ¶m.value);
905 + copy_to_user(&((struct gptu_ioctl_param *) arg)->
906 + value, ¶m.value,
907 + sizeof(param.value));
909 + case GPTU_CALCULATE_DIVIDER:
910 + param.value = lq_cal_divider(param.value);
911 + if (param.value == 0)
914 + copy_to_user(&((struct gptu_ioctl_param *) arg)->
915 + value, ¶m.value,
916 + sizeof(param.value));
920 + case GPTU_SET_TIMER:
921 + ret = lq_set_timer(param.timer, param.value,
922 + TIMER_FLAG_MASK_STOP(param.flag) !=
923 + TIMER_FLAG_ONCE ? 1 : 0,
924 + TIMER_FLAG_MASK_SRC(param.flag) ==
925 + TIMER_FLAG_EXT_SRC ? 1 : 0,
926 + TIMER_FLAG_MASK_HANDLE(param.flag) ==
927 + TIMER_FLAG_SIGNAL ? TIMER_FLAG_SIGNAL :
928 + TIMER_FLAG_NO_HANDLE,
929 + (unsigned long) param.pid,
930 + (unsigned long) param.sig);
932 + copy_to_user(&((struct gptu_ioctl_param *) arg)->
933 + timer, &ret, sizeof(&ret));
937 + case GPTU_SET_COUNTER:
938 + lq_set_counter(param.timer, param.flag, param.value, 0, 0);
940 + copy_to_user(&((struct gptu_ioctl_param *) arg)->
941 + timer, &ret, sizeof(&ret));
952 +static int gptu_open(struct inode *inode, struct file *file)
957 +static int gptu_release(struct inode *inode, struct file *file)
962 +int __init lq_gptu_init(void)
967 + ltq_w32(0, LQ_GPTU_IRNEN);
968 + ltq_w32(0xfff, LQ_GPTU_IRNCR);
970 + memset(&timer_dev, 0, sizeof(timer_dev));
971 + mutex_init(&timer_dev.gptu_mutex);
974 + timer_dev.number_of_timers = GPTU_ID_CFG * 2;
976 + if (timer_dev.number_of_timers > MAX_NUM_OF_32BIT_TIMER_BLOCKS * 2)
977 + timer_dev.number_of_timers = MAX_NUM_OF_32BIT_TIMER_BLOCKS * 2;
978 + printk(KERN_INFO "gptu: totally %d 16-bit timers/counters\n", timer_dev.number_of_timers);
980 + ret = misc_register(&gptu_miscdev);
982 + printk(KERN_ERR "gptu: can't misc_register, get error %d\n", -ret);
985 + printk(KERN_INFO "gptu: misc_register on minor %d\n", gptu_miscdev.minor);
988 + for (i = 0; i < timer_dev.number_of_timers; i++) {
989 + ret = request_irq(TIMER_INTERRUPT + i, timer_irq_handler, IRQF_TIMER, gptu_miscdev.name, &timer_dev.timer[i]);
991 + for (; i >= 0; i--)
992 + free_irq(TIMER_INTERRUPT + i, &timer_dev.timer[i]);
993 + misc_deregister(&gptu_miscdev);
994 + printk(KERN_ERR "gptu: failed in requesting irq (%d), get error %d\n", i, -ret);
997 + timer_dev.timer[i].irq = TIMER_INTERRUPT + i;
998 + disable_irq(timer_dev.timer[i].irq);
999 + printk(KERN_INFO "gptu: succeeded to request irq %d\n", timer_dev.timer[i].irq);
1006 +void __exit lq_gptu_exit(void)
1010 + for (i = 0; i < timer_dev.number_of_timers; i++) {
1011 + if (timer_dev.timer[i].f_irq_on)
1012 + disable_irq(timer_dev.timer[i].irq);
1013 + free_irq(timer_dev.timer[i].irq, &timer_dev.timer[i]);
1015 + lq_disable_gptu();
1016 + misc_deregister(&gptu_miscdev);
1019 +module_init(lq_gptu_init);
1020 +module_exit(lq_gptu_exit);