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
15 +++ b/arch/mips/include/asm/mach-lantiq/lantiq_timer.h
17 +#ifndef __DANUBE_GPTU_DEV_H__2005_07_26__10_19__
18 +#define __DANUBE_GPTU_DEV_H__2005_07_26__10_19__
21 +/******************************************************************************
22 + Copyright (c) 2002, Infineon Technologies. All rights reserved.
25 + Because the program is licensed free of charge, there is no warranty for
26 + the program, to the extent permitted by applicable law. Except when
27 + otherwise stated in writing the copyright holders and/or other parties
28 + provide the program "as is" without warranty of any kind, either
29 + expressed or implied, including, but not limited to, the implied
30 + warranties of merchantability and fitness for a particular purpose. The
31 + entire risk as to the quality and performance of the program is with
32 + you. should the program prove defective, you assume the cost of all
33 + necessary servicing, repair or correction.
35 + In no event unless required by applicable law or agreed to in writing
36 + will any copyright holder, or any other party who may modify and/or
37 + redistribute the program as permitted above, be liable to you for
38 + damages, including any general, special, incidental or consequential
39 + damages arising out of the use or inability to use the program
40 + (including but not limited to loss of data or data being rendered
41 + inaccurate or losses sustained by you or third parties or a failure of
42 + the program to operate with any other programs), even if such holder or
43 + other party has been advised of the possibility of such damages.
44 +******************************************************************************/
48 + * ####################################
50 + * ####################################
54 + * Available Timer/Counter Index
56 +#define TIMER(n, X) (n * 2 + (X ? 1 : 0))
57 +#define TIMER_ANY 0x00
58 +#define TIMER1A TIMER(1, 0)
59 +#define TIMER1B TIMER(1, 1)
60 +#define TIMER2A TIMER(2, 0)
61 +#define TIMER2B TIMER(2, 1)
62 +#define TIMER3A TIMER(3, 0)
63 +#define TIMER3B TIMER(3, 1)
66 + * Flag of Timer/Counter
67 + * These flags specify the way in which timer is configured.
69 +/* Bit size of timer/counter. */
70 +#define TIMER_FLAG_16BIT 0x0000
71 +#define TIMER_FLAG_32BIT 0x0001
72 +/* Switch between timer and counter. */
73 +#define TIMER_FLAG_TIMER 0x0000
74 +#define TIMER_FLAG_COUNTER 0x0002
75 +/* Stop or continue when overflowing/underflowing. */
76 +#define TIMER_FLAG_ONCE 0x0000
77 +#define TIMER_FLAG_CYCLIC 0x0004
78 +/* Count up or counter down. */
79 +#define TIMER_FLAG_UP 0x0000
80 +#define TIMER_FLAG_DOWN 0x0008
81 +/* Count on specific level or edge. */
82 +#define TIMER_FLAG_HIGH_LEVEL_SENSITIVE 0x0000
83 +#define TIMER_FLAG_LOW_LEVEL_SENSITIVE 0x0040
84 +#define TIMER_FLAG_RISE_EDGE 0x0010
85 +#define TIMER_FLAG_FALL_EDGE 0x0020
86 +#define TIMER_FLAG_ANY_EDGE 0x0030
87 +/* Signal is syncronous to module clock or not. */
88 +#define TIMER_FLAG_UNSYNC 0x0000
89 +#define TIMER_FLAG_SYNC 0x0080
90 +/* Different interrupt handle type. */
91 +#define TIMER_FLAG_NO_HANDLE 0x0000
92 +#if defined(__KERNEL__)
93 + #define TIMER_FLAG_CALLBACK_IN_IRQ 0x0100
94 +#endif // defined(__KERNEL__)
95 +#define TIMER_FLAG_SIGNAL 0x0300
96 +/* Internal clock source or external clock source */
97 +#define TIMER_FLAG_INT_SRC 0x0000
98 +#define TIMER_FLAG_EXT_SRC 0x1000
104 +#define GPTU_REQUEST_TIMER 0x01 /* General method to setup timer/counter. */
105 +#define GPTU_FREE_TIMER 0x02 /* Free timer/counter. */
106 +#define GPTU_START_TIMER 0x03 /* Start or resume timer/counter. */
107 +#define GPTU_STOP_TIMER 0x04 /* Suspend timer/counter. */
108 +#define GPTU_GET_COUNT_VALUE 0x05 /* Get current count value. */
109 +#define GPTU_CALCULATE_DIVIDER 0x06 /* Calculate timer divider from given freq.*/
110 +#define GPTU_SET_TIMER 0x07 /* Simplified method to setup timer. */
111 +#define GPTU_SET_COUNTER 0x08 /* Simplified method to setup counter. */
114 + * Data Type Used to Call ioctl
116 +struct gptu_ioctl_param {
117 + unsigned int timer; /* In command GPTU_REQUEST_TIMER, GPTU_SET_TIMER, and *
118 + * GPTU_SET_COUNTER, this field is ID of expected *
119 + * timer/counter. If it's zero, a timer/counter would *
120 + * be dynamically allocated and ID would be stored in *
122 + * In command GPTU_GET_COUNT_VALUE, this field is *
124 + * In other command, this field is ID of timer/counter *
126 + unsigned int flag; /* In command GPTU_REQUEST_TIMER, GPTU_SET_TIMER, and *
127 + * GPTU_SET_COUNTER, this field contains flags to *
128 + * specify how to configure timer/counter. *
129 + * In command GPTU_START_TIMER, zero indicate start *
130 + * and non-zero indicate resume timer/counter. *
131 + * In other command, this field is ignored. */
132 + unsigned long value; /* In command GPTU_REQUEST_TIMER, this field contains *
133 + * init/reload value. *
134 + * In command GPTU_SET_TIMER, this field contains *
135 + * frequency (0.001Hz) of timer. *
136 + * In command GPTU_GET_COUNT_VALUE, current count *
137 + * value would be stored in this field. *
138 + * In command GPTU_CALCULATE_DIVIDER, this field *
139 + * contains frequency wanted, and after calculation, *
140 + * divider would be stored in this field to overwrite *
142 + * In other command, this field is ignored. */
143 + int pid; /* In command GPTU_REQUEST_TIMER and GPTU_SET_TIMER, *
144 + * if signal is required, this field contains process *
145 + * ID to which signal would be sent. *
146 + * In other command, this field is ignored. */
147 + int sig; /* In command GPTU_REQUEST_TIMER and GPTU_SET_TIMER, *
148 + * if signal is required, this field contains signal *
149 + * number which would be sent. *
150 + * In other command, this field is ignored. */
154 + * ####################################
156 + * ####################################
158 +typedef void (*timer_callback)(unsigned long arg);
160 +extern int lq_request_timer(unsigned int, unsigned int, unsigned long, unsigned long, unsigned long);
161 +extern int lq_free_timer(unsigned int);
162 +extern int lq_start_timer(unsigned int, int);
163 +extern int lq_stop_timer(unsigned int);
164 +extern int lq_reset_counter_flags(u32 timer, u32 flags);
165 +extern int lq_get_count_value(unsigned int, unsigned long *);
166 +extern u32 lq_cal_divider(unsigned long);
167 +extern int lq_set_timer(unsigned int, unsigned int, int, int, unsigned int, unsigned long, unsigned long);
168 +extern int lq_set_counter(unsigned int timer, unsigned int flag,
169 + u32 reload, unsigned long arg1, unsigned long arg2);
171 +#endif /* __DANUBE_GPTU_DEV_H__2005_07_26__10_19__ */
172 --- a/arch/mips/lantiq/xway/Makefile
173 +++ b/arch/mips/lantiq/xway/Makefile
175 -obj-y := sysctrl.o reset.o gpio.o gpio_stp.o gpio_ebu.o devices.o dma.o nand.o
176 +obj-y := sysctrl.o reset.o gpio.o gpio_stp.o gpio_ebu.o devices.o dma.o nand.o timer.o
178 obj-$(CONFIG_SOC_XWAY) += clk-xway.o prom-xway.o
179 obj-$(CONFIG_SOC_AMAZON_SE) += clk-ase.o prom-ase.o
181 +++ b/arch/mips/lantiq/xway/timer.c
183 +#include <linux/kernel.h>
184 +#include <linux/module.h>
185 +#include <linux/version.h>
186 +#include <linux/types.h>
187 +#include <linux/fs.h>
188 +#include <linux/miscdevice.h>
189 +#include <linux/init.h>
190 +#include <linux/uaccess.h>
191 +#include <linux/unistd.h>
192 +#include <linux/errno.h>
193 +#include <linux/interrupt.h>
194 +#include <linux/sched.h>
196 +#include <asm/irq.h>
197 +#include <asm/div64.h>
199 +#include <lantiq_soc.h>
200 +#include <lantiq_irq.h>
201 +#include <lantiq_timer.h>
203 +#define MAX_NUM_OF_32BIT_TIMER_BLOCKS 6
206 +#define FIRST_TIMER TIMER1A
208 +#define FIRST_TIMER 2
212 + * GPTC divider is set or not.
214 +#define GPTU_CLC_RMC_IS_SET 0
217 + * Timer Interrupt (IRQ)
219 +/* Must be adjusted when ICU driver is available */
220 +#define TIMER_INTERRUPT (INT_NUM_IM3_IRL0 + 22)
225 +#define GET_BITS(x, msb, lsb) \
226 + (((x) & ((1 << ((msb) + 1)) - 1)) >> (lsb))
227 +#define SET_BITS(x, msb, lsb, value) \
228 + (((x) & ~(((1 << ((msb) + 1)) - 1) ^ ((1 << (lsb)) - 1))) | \
229 + (((value) & ((1 << (1 + (msb) - (lsb))) - 1)) << (lsb)))
232 + * GPTU Register Mapping
234 +#define LQ_GPTU (KSEG1 + 0x1E100A00)
235 +#define LQ_GPTU_CLC ((volatile u32 *)(LQ_GPTU + 0x0000))
236 +#define LQ_GPTU_ID ((volatile u32 *)(LQ_GPTU + 0x0008))
237 +#define LQ_GPTU_CON(n, X) ((volatile u32 *)(LQ_GPTU + 0x0010 + ((X) * 4) + ((n) - 1) * 0x0020)) /* X must be either A or B */
238 +#define LQ_GPTU_RUN(n, X) ((volatile u32 *)(LQ_GPTU + 0x0018 + ((X) * 4) + ((n) - 1) * 0x0020)) /* X must be either A or B */
239 +#define LQ_GPTU_RELOAD(n, X) ((volatile u32 *)(LQ_GPTU + 0x0020 + ((X) * 4) + ((n) - 1) * 0x0020)) /* X must be either A or B */
240 +#define LQ_GPTU_COUNT(n, X) ((volatile u32 *)(LQ_GPTU + 0x0028 + ((X) * 4) + ((n) - 1) * 0x0020)) /* X must be either A or B */
241 +#define LQ_GPTU_IRNEN ((volatile u32 *)(LQ_GPTU + 0x00F4))
242 +#define LQ_GPTU_IRNICR ((volatile u32 *)(LQ_GPTU + 0x00F8))
243 +#define LQ_GPTU_IRNCR ((volatile u32 *)(LQ_GPTU + 0x00FC))
246 + * Clock Control Register
248 +#define GPTU_CLC_SMC GET_BITS(*LQ_GPTU_CLC, 23, 16)
249 +#define GPTU_CLC_RMC GET_BITS(*LQ_GPTU_CLC, 15, 8)
250 +#define GPTU_CLC_FSOE (*LQ_GPTU_CLC & (1 << 5))
251 +#define GPTU_CLC_EDIS (*LQ_GPTU_CLC & (1 << 3))
252 +#define GPTU_CLC_SPEN (*LQ_GPTU_CLC & (1 << 2))
253 +#define GPTU_CLC_DISS (*LQ_GPTU_CLC & (1 << 1))
254 +#define GPTU_CLC_DISR (*LQ_GPTU_CLC & (1 << 0))
256 +#define GPTU_CLC_SMC_SET(value) SET_BITS(0, 23, 16, (value))
257 +#define GPTU_CLC_RMC_SET(value) SET_BITS(0, 15, 8, (value))
258 +#define GPTU_CLC_FSOE_SET(value) ((value) ? (1 << 5) : 0)
259 +#define GPTU_CLC_SBWE_SET(value) ((value) ? (1 << 4) : 0)
260 +#define GPTU_CLC_EDIS_SET(value) ((value) ? (1 << 3) : 0)
261 +#define GPTU_CLC_SPEN_SET(value) ((value) ? (1 << 2) : 0)
262 +#define GPTU_CLC_DISR_SET(value) ((value) ? (1 << 0) : 0)
267 +#define GPTU_ID_ID GET_BITS(*LQ_GPTU_ID, 15, 8)
268 +#define GPTU_ID_CFG GET_BITS(*LQ_GPTU_ID, 7, 5)
269 +#define GPTU_ID_REV GET_BITS(*LQ_GPTU_ID, 4, 0)
272 + * Control Register of Timer/Counter nX
273 + * n is the index of block (1 based index)
274 + * X is either A or B
276 +#define GPTU_CON_SRC_EG(n, X) (*LQ_GPTU_CON(n, X) & (1 << 10))
277 +#define GPTU_CON_SRC_EXT(n, X) (*LQ_GPTU_CON(n, X) & (1 << 9))
278 +#define GPTU_CON_SYNC(n, X) (*LQ_GPTU_CON(n, X) & (1 << 8))
279 +#define GPTU_CON_EDGE(n, X) GET_BITS(*LQ_GPTU_CON(n, X), 7, 6)
280 +#define GPTU_CON_INV(n, X) (*LQ_GPTU_CON(n, X) & (1 << 5))
281 +#define GPTU_CON_EXT(n, X) (*LQ_GPTU_CON(n, A) & (1 << 4)) /* Timer/Counter B does not have this bit */
282 +#define GPTU_CON_STP(n, X) (*LQ_GPTU_CON(n, X) & (1 << 3))
283 +#define GPTU_CON_CNT(n, X) (*LQ_GPTU_CON(n, X) & (1 << 2))
284 +#define GPTU_CON_DIR(n, X) (*LQ_GPTU_CON(n, X) & (1 << 1))
285 +#define GPTU_CON_EN(n, X) (*LQ_GPTU_CON(n, X) & (1 << 0))
287 +#define GPTU_CON_SRC_EG_SET(value) ((value) ? 0 : (1 << 10))
288 +#define GPTU_CON_SRC_EXT_SET(value) ((value) ? (1 << 9) : 0)
289 +#define GPTU_CON_SYNC_SET(value) ((value) ? (1 << 8) : 0)
290 +#define GPTU_CON_EDGE_SET(value) SET_BITS(0, 7, 6, (value))
291 +#define GPTU_CON_INV_SET(value) ((value) ? (1 << 5) : 0)
292 +#define GPTU_CON_EXT_SET(value) ((value) ? (1 << 4) : 0)
293 +#define GPTU_CON_STP_SET(value) ((value) ? (1 << 3) : 0)
294 +#define GPTU_CON_CNT_SET(value) ((value) ? (1 << 2) : 0)
295 +#define GPTU_CON_DIR_SET(value) ((value) ? (1 << 1) : 0)
297 +#define GPTU_RUN_RL_SET(value) ((value) ? (1 << 2) : 0)
298 +#define GPTU_RUN_CEN_SET(value) ((value) ? (1 << 1) : 0)
299 +#define GPTU_RUN_SEN_SET(value) ((value) ? (1 << 0) : 0)
301 +#define GPTU_IRNEN_TC_SET(n, X, value) ((value) ? (1 << (((n) - 1) * 2 + (X))) : 0)
302 +#define GPTU_IRNCR_TC_SET(n, X, value) ((value) ? (1 << (((n) - 1) * 2 + (X))) : 0)
304 +#define TIMER_FLAG_MASK_SIZE(x) (x & 0x0001)
305 +#define TIMER_FLAG_MASK_TYPE(x) (x & 0x0002)
306 +#define TIMER_FLAG_MASK_STOP(x) (x & 0x0004)
307 +#define TIMER_FLAG_MASK_DIR(x) (x & 0x0008)
308 +#define TIMER_FLAG_NONE_EDGE 0x0000
309 +#define TIMER_FLAG_MASK_EDGE(x) (x & 0x0030)
310 +#define TIMER_FLAG_REAL 0x0000
311 +#define TIMER_FLAG_INVERT 0x0040
312 +#define TIMER_FLAG_MASK_INVERT(x) (x & 0x0040)
313 +#define TIMER_FLAG_MASK_TRIGGER(x) (x & 0x0070)
314 +#define TIMER_FLAG_MASK_SYNC(x) (x & 0x0080)
315 +#define TIMER_FLAG_CALLBACK_IN_HB 0x0200
316 +#define TIMER_FLAG_MASK_HANDLE(x) (x & 0x0300)
317 +#define TIMER_FLAG_MASK_SRC(x) (x & 0x1000)
319 +struct timer_dev_timer {
320 + unsigned int f_irq_on;
323 + unsigned long arg1;
324 + unsigned long arg2;
328 + struct mutex gptu_mutex;
329 + unsigned int number_of_timers;
330 + unsigned int occupation;
331 + unsigned int f_gptu_on;
332 + struct timer_dev_timer timer[MAX_NUM_OF_32BIT_TIMER_BLOCKS * 2];
335 +unsigned int ltq_get_fpi_bus_clock(int fpi);
337 +static long gptu_ioctl(struct file *, unsigned int, unsigned long);
338 +static int gptu_open(struct inode *, struct file *);
339 +static int gptu_release(struct inode *, struct file *);
341 +static struct file_operations gptu_fops = {
342 + .owner = THIS_MODULE,
343 + .unlocked_ioctl = gptu_ioctl,
345 + .release = gptu_release
348 +static struct miscdevice gptu_miscdev = {
349 + .minor = MISC_DYNAMIC_MINOR,
351 + .fops = &gptu_fops,
354 +static struct timer_dev timer_dev;
356 +static irqreturn_t timer_irq_handler(int irq, void *p)
358 + unsigned int timer;
360 + struct timer_dev_timer *dev_timer = (struct timer_dev_timer *)p;
362 + timer = irq - TIMER_INTERRUPT;
363 + if (timer < timer_dev.number_of_timers
364 + && dev_timer == &timer_dev.timer[timer]) {
365 + /* Clear interrupt. */
366 + ltq_w32(1 << timer, LQ_GPTU_IRNCR);
368 + /* Call user hanler or signal. */
369 + flag = dev_timer->flag;
370 + if (!(timer & 0x01)
371 + || TIMER_FLAG_MASK_SIZE(flag) == TIMER_FLAG_16BIT) {
372 + /* 16-bit timer or timer A of 32-bit timer */
373 + switch (TIMER_FLAG_MASK_HANDLE(flag)) {
374 + case TIMER_FLAG_CALLBACK_IN_IRQ:
375 + case TIMER_FLAG_CALLBACK_IN_HB:
376 + if (dev_timer->arg1)
377 + (*(timer_callback)dev_timer->arg1)(dev_timer->arg2);
379 + case TIMER_FLAG_SIGNAL:
380 + send_sig((int)dev_timer->arg2, (struct task_struct *)dev_timer->arg1, 0);
385 + return IRQ_HANDLED;
388 +static inline void lq_enable_gptu(void)
390 + ltq_pmu_enable(PMU_GPT);
392 + /* Set divider as 1, disable write protection for SPEN, enable module. */
394 + GPTU_CLC_SMC_SET(0x00) |
395 + GPTU_CLC_RMC_SET(0x01) |
396 + GPTU_CLC_FSOE_SET(0) |
397 + GPTU_CLC_SBWE_SET(1) |
398 + GPTU_CLC_EDIS_SET(0) |
399 + GPTU_CLC_SPEN_SET(0) |
400 + GPTU_CLC_DISR_SET(0);
403 +static inline void lq_disable_gptu(void)
405 + ltq_w32(0x00, LQ_GPTU_IRNEN);
406 + ltq_w32(0xfff, LQ_GPTU_IRNCR);
408 + /* Set divider as 0, enable write protection for SPEN, disable module. */
410 + GPTU_CLC_SMC_SET(0x00) |
411 + GPTU_CLC_RMC_SET(0x00) |
412 + GPTU_CLC_FSOE_SET(0) |
413 + GPTU_CLC_SBWE_SET(0) |
414 + GPTU_CLC_EDIS_SET(0) |
415 + GPTU_CLC_SPEN_SET(0) |
416 + GPTU_CLC_DISR_SET(1);
418 + ltq_pmu_disable(PMU_GPT);
421 +int lq_request_timer(unsigned int timer, unsigned int flag,
422 + unsigned long value, unsigned long arg1, unsigned long arg2)
425 + unsigned int con_reg, irnen_reg;
428 + if (timer >= FIRST_TIMER + timer_dev.number_of_timers)
431 + printk(KERN_INFO "request_timer(%d, 0x%08X, %lu)...",
432 + timer, flag, value);
434 + if (TIMER_FLAG_MASK_SIZE(flag) == TIMER_FLAG_16BIT)
439 + mutex_lock(&timer_dev.gptu_mutex);
444 + if (timer < FIRST_TIMER) {
446 + unsigned int shift;
447 + /* This takes care of TIMER1B which is the only choice for Voice TAPI system */
448 + unsigned int offset = TIMER2A;
451 + * Pick up a free timer.
453 + if (TIMER_FLAG_MASK_SIZE(flag) == TIMER_FLAG_16BIT) {
454 + mask = 1 << offset;
457 + mask = 3 << offset;
460 + for (timer = offset;
461 + timer < offset + timer_dev.number_of_timers;
462 + timer += shift, mask <<= shift)
463 + if (!(timer_dev.occupation & mask)) {
464 + timer_dev.occupation |= mask;
467 + if (timer >= offset + timer_dev.number_of_timers) {
468 + printk("failed![%d]\n", __LINE__);
469 + mutex_unlock(&timer_dev.gptu_mutex);
474 + register unsigned int mask;
477 + * Check if the requested timer is free.
479 + mask = (TIMER_FLAG_MASK_SIZE(flag) == TIMER_FLAG_16BIT ? 1 : 3) << timer;
480 + if ((timer_dev.occupation & mask)) {
481 + printk("failed![%d] mask %#x, timer_dev.occupation %#x\n",
482 + __LINE__, mask, timer_dev.occupation);
483 + mutex_unlock(&timer_dev.gptu_mutex);
486 + timer_dev.occupation |= mask;
492 + * Prepare control register value.
494 + switch (TIMER_FLAG_MASK_EDGE(flag)) {
496 + case TIMER_FLAG_NONE_EDGE:
497 + con_reg = GPTU_CON_EDGE_SET(0x00);
499 + case TIMER_FLAG_RISE_EDGE:
500 + con_reg = GPTU_CON_EDGE_SET(0x01);
502 + case TIMER_FLAG_FALL_EDGE:
503 + con_reg = GPTU_CON_EDGE_SET(0x02);
505 + case TIMER_FLAG_ANY_EDGE:
506 + con_reg = GPTU_CON_EDGE_SET(0x03);
509 + if (TIMER_FLAG_MASK_TYPE(flag) == TIMER_FLAG_TIMER)
511 + TIMER_FLAG_MASK_SRC(flag) ==
512 + TIMER_FLAG_EXT_SRC ? GPTU_CON_SRC_EXT_SET(1) :
513 + GPTU_CON_SRC_EXT_SET(0);
516 + TIMER_FLAG_MASK_SRC(flag) ==
517 + TIMER_FLAG_EXT_SRC ? GPTU_CON_SRC_EG_SET(1) :
518 + GPTU_CON_SRC_EG_SET(0);
520 + TIMER_FLAG_MASK_SYNC(flag) ==
521 + TIMER_FLAG_UNSYNC ? GPTU_CON_SYNC_SET(0) :
522 + GPTU_CON_SYNC_SET(1);
524 + TIMER_FLAG_MASK_INVERT(flag) ==
525 + TIMER_FLAG_REAL ? GPTU_CON_INV_SET(0) : GPTU_CON_INV_SET(1);
527 + TIMER_FLAG_MASK_SIZE(flag) ==
528 + TIMER_FLAG_16BIT ? GPTU_CON_EXT_SET(0) :
529 + GPTU_CON_EXT_SET(1);
531 + TIMER_FLAG_MASK_STOP(flag) ==
532 + TIMER_FLAG_ONCE ? GPTU_CON_STP_SET(1) : GPTU_CON_STP_SET(0);
534 + TIMER_FLAG_MASK_TYPE(flag) ==
535 + TIMER_FLAG_TIMER ? GPTU_CON_CNT_SET(0) :
536 + GPTU_CON_CNT_SET(1);
538 + TIMER_FLAG_MASK_DIR(flag) ==
539 + TIMER_FLAG_UP ? GPTU_CON_DIR_SET(1) : GPTU_CON_DIR_SET(0);
542 + * Fill up running data.
544 + timer_dev.timer[timer - FIRST_TIMER].flag = flag;
545 + timer_dev.timer[timer - FIRST_TIMER].arg1 = arg1;
546 + timer_dev.timer[timer - FIRST_TIMER].arg2 = arg2;
547 + if (TIMER_FLAG_MASK_SIZE(flag) != TIMER_FLAG_16BIT)
548 + timer_dev.timer[timer - FIRST_TIMER + 1].flag = flag;
551 + * Enable GPTU module.
553 + if (!timer_dev.f_gptu_on) {
555 + timer_dev.f_gptu_on = 1;
561 + if (TIMER_FLAG_MASK_HANDLE(flag) != TIMER_FLAG_NO_HANDLE) {
562 + if (TIMER_FLAG_MASK_HANDLE(flag) == TIMER_FLAG_SIGNAL)
563 + timer_dev.timer[timer - FIRST_TIMER].arg1 =
564 + (unsigned long) find_task_by_vpid((int) arg1);
566 + irnen_reg = 1 << (timer - FIRST_TIMER);
568 + if (TIMER_FLAG_MASK_HANDLE(flag) == TIMER_FLAG_SIGNAL
569 + || (TIMER_FLAG_MASK_HANDLE(flag) ==
570 + TIMER_FLAG_CALLBACK_IN_IRQ
571 + && timer_dev.timer[timer - FIRST_TIMER].arg1)) {
572 + enable_irq(timer_dev.timer[timer - FIRST_TIMER].irq);
573 + timer_dev.timer[timer - FIRST_TIMER].f_irq_on = 1;
579 + * Write config register, reload value and enable interrupt.
583 + *LQ_GPTU_CON(n, X) = con_reg;
584 + *LQ_GPTU_RELOAD(n, X) = value;
585 + /* printk("reload value = %d\n", (u32)value); */
586 + *LQ_GPTU_IRNEN |= irnen_reg;
588 + mutex_unlock(&timer_dev.gptu_mutex);
589 + printk("successful!\n");
592 +EXPORT_SYMBOL(lq_request_timer);
594 +int lq_free_timer(unsigned int timer)
600 + if (!timer_dev.f_gptu_on)
603 + if (timer < FIRST_TIMER || timer >= FIRST_TIMER + timer_dev.number_of_timers)
606 + mutex_lock(&timer_dev.gptu_mutex);
608 + flag = timer_dev.timer[timer - FIRST_TIMER].flag;
609 + if (TIMER_FLAG_MASK_SIZE(flag) != TIMER_FLAG_16BIT)
612 + mask = (TIMER_FLAG_MASK_SIZE(flag) == TIMER_FLAG_16BIT ? 1 : 3) << timer;
613 + if (((timer_dev.occupation & mask) ^ mask)) {
614 + mutex_unlock(&timer_dev.gptu_mutex);
621 + if (GPTU_CON_EN(n, X))
622 + *LQ_GPTU_RUN(n, X) = GPTU_RUN_CEN_SET(1);
624 + *LQ_GPTU_IRNEN &= ~GPTU_IRNEN_TC_SET(n, X, 1);
625 + *LQ_GPTU_IRNCR |= GPTU_IRNCR_TC_SET(n, X, 1);
627 + if (timer_dev.timer[timer - FIRST_TIMER].f_irq_on) {
628 + disable_irq(timer_dev.timer[timer - FIRST_TIMER].irq);
629 + timer_dev.timer[timer - FIRST_TIMER].f_irq_on = 0;
632 + timer_dev.occupation &= ~mask;
633 + if (!timer_dev.occupation && timer_dev.f_gptu_on) {
635 + timer_dev.f_gptu_on = 0;
638 + mutex_unlock(&timer_dev.gptu_mutex);
642 +EXPORT_SYMBOL(lq_free_timer);
644 +int lq_start_timer(unsigned int timer, int is_resume)
650 + if (!timer_dev.f_gptu_on)
653 + if (timer < FIRST_TIMER || timer >= FIRST_TIMER + timer_dev.number_of_timers)
656 + mutex_lock(&timer_dev.gptu_mutex);
658 + flag = timer_dev.timer[timer - FIRST_TIMER].flag;
659 + if (TIMER_FLAG_MASK_SIZE(flag) != TIMER_FLAG_16BIT)
662 + mask = (TIMER_FLAG_MASK_SIZE(flag) ==
663 + TIMER_FLAG_16BIT ? 1 : 3) << timer;
664 + if (((timer_dev.occupation & mask) ^ mask)) {
665 + mutex_unlock(&timer_dev.gptu_mutex);
672 + *LQ_GPTU_RUN(n, X) = GPTU_RUN_RL_SET(!is_resume) | GPTU_RUN_SEN_SET(1);
674 + mutex_unlock(&timer_dev.gptu_mutex);
678 +EXPORT_SYMBOL(lq_start_timer);
680 +int lq_stop_timer(unsigned int timer)
686 + if (!timer_dev.f_gptu_on)
689 + if (timer < FIRST_TIMER
690 + || timer >= FIRST_TIMER + timer_dev.number_of_timers)
693 + mutex_lock(&timer_dev.gptu_mutex);
695 + flag = timer_dev.timer[timer - FIRST_TIMER].flag;
696 + if (TIMER_FLAG_MASK_SIZE(flag) != TIMER_FLAG_16BIT)
699 + mask = (TIMER_FLAG_MASK_SIZE(flag) == TIMER_FLAG_16BIT ? 1 : 3) << timer;
700 + if (((timer_dev.occupation & mask) ^ mask)) {
701 + mutex_unlock(&timer_dev.gptu_mutex);
708 + *LQ_GPTU_RUN(n, X) = GPTU_RUN_CEN_SET(1);
710 + mutex_unlock(&timer_dev.gptu_mutex);
714 +EXPORT_SYMBOL(lq_stop_timer);
716 +int lq_reset_counter_flags(u32 timer, u32 flags)
718 + unsigned int oflag;
719 + unsigned int mask, con_reg;
722 + if (!timer_dev.f_gptu_on)
725 + if (timer < FIRST_TIMER || timer >= FIRST_TIMER + timer_dev.number_of_timers)
728 + mutex_lock(&timer_dev.gptu_mutex);
730 + oflag = timer_dev.timer[timer - FIRST_TIMER].flag;
731 + if (TIMER_FLAG_MASK_SIZE(oflag) != TIMER_FLAG_16BIT)
734 + mask = (TIMER_FLAG_MASK_SIZE(oflag) == TIMER_FLAG_16BIT ? 1 : 3) << timer;
735 + if (((timer_dev.occupation & mask) ^ mask)) {
736 + mutex_unlock(&timer_dev.gptu_mutex);
740 + switch (TIMER_FLAG_MASK_EDGE(flags)) {
742 + case TIMER_FLAG_NONE_EDGE:
743 + con_reg = GPTU_CON_EDGE_SET(0x00);
745 + case TIMER_FLAG_RISE_EDGE:
746 + con_reg = GPTU_CON_EDGE_SET(0x01);
748 + case TIMER_FLAG_FALL_EDGE:
749 + con_reg = GPTU_CON_EDGE_SET(0x02);
751 + case TIMER_FLAG_ANY_EDGE:
752 + con_reg = GPTU_CON_EDGE_SET(0x03);
755 + if (TIMER_FLAG_MASK_TYPE(flags) == TIMER_FLAG_TIMER)
756 + con_reg |= TIMER_FLAG_MASK_SRC(flags) == TIMER_FLAG_EXT_SRC ? GPTU_CON_SRC_EXT_SET(1) : GPTU_CON_SRC_EXT_SET(0);
758 + con_reg |= TIMER_FLAG_MASK_SRC(flags) == TIMER_FLAG_EXT_SRC ? GPTU_CON_SRC_EG_SET(1) : GPTU_CON_SRC_EG_SET(0);
759 + con_reg |= TIMER_FLAG_MASK_SYNC(flags) == TIMER_FLAG_UNSYNC ? GPTU_CON_SYNC_SET(0) : GPTU_CON_SYNC_SET(1);
760 + con_reg |= TIMER_FLAG_MASK_INVERT(flags) == TIMER_FLAG_REAL ? GPTU_CON_INV_SET(0) : GPTU_CON_INV_SET(1);
761 + con_reg |= TIMER_FLAG_MASK_SIZE(flags) == TIMER_FLAG_16BIT ? GPTU_CON_EXT_SET(0) : GPTU_CON_EXT_SET(1);
762 + con_reg |= TIMER_FLAG_MASK_STOP(flags) == TIMER_FLAG_ONCE ? GPTU_CON_STP_SET(1) : GPTU_CON_STP_SET(0);
763 + con_reg |= TIMER_FLAG_MASK_TYPE(flags) == TIMER_FLAG_TIMER ? GPTU_CON_CNT_SET(0) : GPTU_CON_CNT_SET(1);
764 + con_reg |= TIMER_FLAG_MASK_DIR(flags) == TIMER_FLAG_UP ? GPTU_CON_DIR_SET(1) : GPTU_CON_DIR_SET(0);
766 + timer_dev.timer[timer - FIRST_TIMER].flag = flags;
767 + if (TIMER_FLAG_MASK_SIZE(flags) != TIMER_FLAG_16BIT)
768 + timer_dev.timer[timer - FIRST_TIMER + 1].flag = flags;
773 + *LQ_GPTU_CON(n, X) = con_reg;
775 + printk(KERN_INFO "[%s]: counter%d oflags %#x, nflags %#x, GPTU_CON %#x\n", __func__, timer, oflag, flags, *LQ_GPTU_CON(n, X));
776 + mutex_unlock(&timer_dev.gptu_mutex);
779 +EXPORT_SYMBOL(lq_reset_counter_flags);
781 +int lq_get_count_value(unsigned int timer, unsigned long *value)
787 + if (!timer_dev.f_gptu_on)
790 + if (timer < FIRST_TIMER
791 + || timer >= FIRST_TIMER + timer_dev.number_of_timers)
794 + mutex_lock(&timer_dev.gptu_mutex);
796 + flag = timer_dev.timer[timer - FIRST_TIMER].flag;
797 + if (TIMER_FLAG_MASK_SIZE(flag) != TIMER_FLAG_16BIT)
800 + mask = (TIMER_FLAG_MASK_SIZE(flag) == TIMER_FLAG_16BIT ? 1 : 3) << timer;
801 + if (((timer_dev.occupation & mask) ^ mask)) {
802 + mutex_unlock(&timer_dev.gptu_mutex);
809 + *value = *LQ_GPTU_COUNT(n, X);
811 + mutex_unlock(&timer_dev.gptu_mutex);
815 +EXPORT_SYMBOL(lq_get_count_value);
817 +u32 lq_cal_divider(unsigned long freq)
819 + u64 module_freq, fpi = ltq_get_fpi_bus_clock(2);
820 + u32 clock_divider = 1;
821 + module_freq = fpi * 1000;
822 + do_div(module_freq, clock_divider * freq);
823 + return module_freq;
825 +EXPORT_SYMBOL(lq_cal_divider);
827 +int lq_set_timer(unsigned int timer, unsigned int freq, int is_cyclic,
828 + int is_ext_src, unsigned int handle_flag, unsigned long arg1,
829 + unsigned long arg2)
831 + unsigned long divider;
834 + divider = lq_cal_divider(freq);
837 + flag = ((divider & ~0xFFFF) ? TIMER_FLAG_32BIT : TIMER_FLAG_16BIT)
838 + | (is_cyclic ? TIMER_FLAG_CYCLIC : TIMER_FLAG_ONCE)
839 + | (is_ext_src ? TIMER_FLAG_EXT_SRC : TIMER_FLAG_INT_SRC)
840 + | TIMER_FLAG_TIMER | TIMER_FLAG_DOWN
841 + | TIMER_FLAG_MASK_HANDLE(handle_flag);
843 + printk(KERN_INFO "lq_set_timer(%d, %d), divider = %lu\n",
844 + timer, freq, divider);
845 + return lq_request_timer(timer, flag, divider, arg1, arg2);
847 +EXPORT_SYMBOL(lq_set_timer);
849 +int lq_set_counter(unsigned int timer, unsigned int flag, u32 reload,
850 + unsigned long arg1, unsigned long arg2)
852 + printk(KERN_INFO "lq_set_counter(%d, %#x, %d)\n", timer, flag, reload);
853 + return lq_request_timer(timer, flag, reload, arg1, arg2);
855 +EXPORT_SYMBOL(lq_set_counter);
857 +static long gptu_ioctl(struct file *file, unsigned int cmd,
861 + struct gptu_ioctl_param param;
863 + if (!access_ok(VERIFY_READ, arg, sizeof(struct gptu_ioctl_param)))
865 + copy_from_user(¶m, (void *) arg, sizeof(param));
867 + if ((((cmd == GPTU_REQUEST_TIMER || cmd == GPTU_SET_TIMER
868 + || GPTU_SET_COUNTER) && param.timer < 2)
869 + || cmd == GPTU_GET_COUNT_VALUE || cmd == GPTU_CALCULATE_DIVIDER)
870 + && !access_ok(VERIFY_WRITE, arg,
871 + sizeof(struct gptu_ioctl_param)))
875 + case GPTU_REQUEST_TIMER:
876 + ret = lq_request_timer(param.timer, param.flag, param.value,
877 + (unsigned long) param.pid,
878 + (unsigned long) param.sig);
880 + copy_to_user(&((struct gptu_ioctl_param *) arg)->
881 + timer, &ret, sizeof(&ret));
885 + case GPTU_FREE_TIMER:
886 + ret = lq_free_timer(param.timer);
888 + case GPTU_START_TIMER:
889 + ret = lq_start_timer(param.timer, param.flag);
891 + case GPTU_STOP_TIMER:
892 + ret = lq_stop_timer(param.timer);
894 + case GPTU_GET_COUNT_VALUE:
895 + ret = lq_get_count_value(param.timer, ¶m.value);
897 + copy_to_user(&((struct gptu_ioctl_param *) arg)->
898 + value, ¶m.value,
899 + sizeof(param.value));
901 + case GPTU_CALCULATE_DIVIDER:
902 + param.value = lq_cal_divider(param.value);
903 + if (param.value == 0)
906 + copy_to_user(&((struct gptu_ioctl_param *) arg)->
907 + value, ¶m.value,
908 + sizeof(param.value));
912 + case GPTU_SET_TIMER:
913 + ret = lq_set_timer(param.timer, param.value,
914 + TIMER_FLAG_MASK_STOP(param.flag) !=
915 + TIMER_FLAG_ONCE ? 1 : 0,
916 + TIMER_FLAG_MASK_SRC(param.flag) ==
917 + TIMER_FLAG_EXT_SRC ? 1 : 0,
918 + TIMER_FLAG_MASK_HANDLE(param.flag) ==
919 + TIMER_FLAG_SIGNAL ? TIMER_FLAG_SIGNAL :
920 + TIMER_FLAG_NO_HANDLE,
921 + (unsigned long) param.pid,
922 + (unsigned long) param.sig);
924 + copy_to_user(&((struct gptu_ioctl_param *) arg)->
925 + timer, &ret, sizeof(&ret));
929 + case GPTU_SET_COUNTER:
930 + lq_set_counter(param.timer, param.flag, param.value, 0, 0);
932 + copy_to_user(&((struct gptu_ioctl_param *) arg)->
933 + timer, &ret, sizeof(&ret));
944 +static int gptu_open(struct inode *inode, struct file *file)
949 +static int gptu_release(struct inode *inode, struct file *file)
954 +int __init lq_gptu_init(void)
959 + ltq_w32(0, LQ_GPTU_IRNEN);
960 + ltq_w32(0xfff, LQ_GPTU_IRNCR);
962 + memset(&timer_dev, 0, sizeof(timer_dev));
963 + mutex_init(&timer_dev.gptu_mutex);
966 + timer_dev.number_of_timers = GPTU_ID_CFG * 2;
968 + if (timer_dev.number_of_timers > MAX_NUM_OF_32BIT_TIMER_BLOCKS * 2)
969 + timer_dev.number_of_timers = MAX_NUM_OF_32BIT_TIMER_BLOCKS * 2;
970 + printk(KERN_INFO "gptu: totally %d 16-bit timers/counters\n", timer_dev.number_of_timers);
972 + ret = misc_register(&gptu_miscdev);
974 + printk(KERN_ERR "gptu: can't misc_register, get error %d\n", -ret);
977 + printk(KERN_INFO "gptu: misc_register on minor %d\n", gptu_miscdev.minor);
980 + for (i = 0; i < timer_dev.number_of_timers; i++) {
981 + ret = request_irq(TIMER_INTERRUPT + i, timer_irq_handler, IRQF_TIMER, gptu_miscdev.name, &timer_dev.timer[i]);
983 + for (; i >= 0; i--)
984 + free_irq(TIMER_INTERRUPT + i, &timer_dev.timer[i]);
985 + misc_deregister(&gptu_miscdev);
986 + printk(KERN_ERR "gptu: failed in requesting irq (%d), get error %d\n", i, -ret);
989 + timer_dev.timer[i].irq = TIMER_INTERRUPT + i;
990 + disable_irq(timer_dev.timer[i].irq);
991 + printk(KERN_INFO "gptu: succeeded to request irq %d\n", timer_dev.timer[i].irq);
998 +void __exit lq_gptu_exit(void)
1002 + for (i = 0; i < timer_dev.number_of_timers; i++) {
1003 + if (timer_dev.timer[i].f_irq_on)
1004 + disable_irq(timer_dev.timer[i].irq);
1005 + free_irq(timer_dev.timer[i].irq, &timer_dev.timer[i]);
1007 + lq_disable_gptu();
1008 + misc_deregister(&gptu_miscdev);
1011 +module_init(lq_gptu_init);
1012 +module_exit(lq_gptu_exit);