lantiq: fix PHY state handling issue
[openwrt.git] / target / linux / lantiq / patches-4.4 / 0008-MIPS-lantiq-backport-old-timer-code.patch
1 From 94800350cb8d2f29dda2206b5e9a3772024ee168 Mon Sep 17 00:00:00 2001
2 From: John Crispin <blogic@openwrt.org>
3 Date: Thu, 7 Aug 2014 18:30:56 +0200
4 Subject: [PATCH 08/36] MIPS: lantiq: backport old timer code
5
6 Signed-off-by: John Crispin <blogic@openwrt.org>
7 ---
8  arch/mips/include/asm/mach-lantiq/lantiq_timer.h |  155 ++++
9  arch/mips/lantiq/xway/Makefile                   |    2 +-
10  arch/mips/lantiq/xway/timer.c                    |  845 ++++++++++++++++++++++
11  3 files changed, 1001 insertions(+), 1 deletion(-)
12  create mode 100644 arch/mips/include/asm/mach-lantiq/lantiq_timer.h
13  create mode 100644 arch/mips/lantiq/xway/timer.c
14
15 --- /dev/null
16 +++ b/arch/mips/include/asm/mach-lantiq/lantiq_timer.h
17 @@ -0,0 +1,155 @@
18 +#ifndef __DANUBE_GPTU_DEV_H__2005_07_26__10_19__
19 +#define __DANUBE_GPTU_DEV_H__2005_07_26__10_19__
20 +
21 +
22 +/******************************************************************************
23 +       Copyright (c) 2002, Infineon Technologies.  All rights reserved.
24 +
25 +                               No Warranty
26 +   Because the program is licensed free of charge, there is no warranty for
27 +   the program, to the extent permitted by applicable law.  Except when
28 +   otherwise stated in writing the copyright holders and/or other parties
29 +   provide the program "as is" without warranty of any kind, either
30 +   expressed or implied, including, but not limited to, the implied
31 +   warranties of merchantability and fitness for a particular purpose. The
32 +   entire risk as to the quality and performance of the program is with
33 +   you.  should the program prove defective, you assume the cost of all
34 +   necessary servicing, repair or correction.
35 +
36 +   In no event unless required by applicable law or agreed to in writing
37 +   will any copyright holder, or any other party who may modify and/or
38 +   redistribute the program as permitted above, be liable to you for
39 +   damages, including any general, special, incidental or consequential
40 +   damages arising out of the use or inability to use the program
41 +   (including but not limited to loss of data or data being rendered
42 +   inaccurate or losses sustained by you or third parties or a failure of
43 +   the program to operate with any other programs), even if such holder or
44 +   other party has been advised of the possibility of such damages.
45 +******************************************************************************/
46 +
47 +
48 +/*
49 + * ####################################
50 + *              Definition
51 + * ####################################
52 + */
53 +
54 +/*
55 + *  Available Timer/Counter Index
56 + */
57 +#define TIMER(n, X)                     (n * 2 + (X ? 1 : 0))
58 +#define TIMER_ANY                       0x00
59 +#define TIMER1A                         TIMER(1, 0)
60 +#define TIMER1B                         TIMER(1, 1)
61 +#define TIMER2A                         TIMER(2, 0)
62 +#define TIMER2B                         TIMER(2, 1)
63 +#define TIMER3A                         TIMER(3, 0)
64 +#define TIMER3B                         TIMER(3, 1)
65 +
66 +/*
67 + *  Flag of Timer/Counter
68 + *  These flags specify the way in which timer is configured.
69 + */
70 +/*  Bit size of timer/counter.                      */
71 +#define TIMER_FLAG_16BIT                0x0000
72 +#define TIMER_FLAG_32BIT                0x0001
73 +/*  Switch between timer and counter.               */
74 +#define TIMER_FLAG_TIMER                0x0000
75 +#define TIMER_FLAG_COUNTER              0x0002
76 +/*  Stop or continue when overflowing/underflowing. */
77 +#define TIMER_FLAG_ONCE                 0x0000
78 +#define TIMER_FLAG_CYCLIC               0x0004
79 +/*  Count up or counter down.                       */
80 +#define TIMER_FLAG_UP                   0x0000
81 +#define TIMER_FLAG_DOWN                 0x0008
82 +/*  Count on specific level or edge.                */
83 +#define TIMER_FLAG_HIGH_LEVEL_SENSITIVE 0x0000
84 +#define TIMER_FLAG_LOW_LEVEL_SENSITIVE  0x0040
85 +#define TIMER_FLAG_RISE_EDGE            0x0010
86 +#define TIMER_FLAG_FALL_EDGE            0x0020
87 +#define TIMER_FLAG_ANY_EDGE             0x0030
88 +/*  Signal is syncronous to module clock or not.    */
89 +#define TIMER_FLAG_UNSYNC               0x0000
90 +#define TIMER_FLAG_SYNC                 0x0080
91 +/*  Different interrupt handle type.                */
92 +#define TIMER_FLAG_NO_HANDLE            0x0000
93 +#if defined(__KERNEL__)
94 +    #define TIMER_FLAG_CALLBACK_IN_IRQ  0x0100
95 +#endif  //  defined(__KERNEL__)
96 +#define TIMER_FLAG_SIGNAL               0x0300
97 +/*  Internal clock source or external clock source  */
98 +#define TIMER_FLAG_INT_SRC              0x0000
99 +#define TIMER_FLAG_EXT_SRC              0x1000
100 +
101 +
102 +/*
103 + *  ioctl Command
104 + */
105 +#define GPTU_REQUEST_TIMER              0x01    /*  General method to setup timer/counter.  */
106 +#define GPTU_FREE_TIMER                 0x02    /*  Free timer/counter.                     */
107 +#define GPTU_START_TIMER                0x03    /*  Start or resume timer/counter.          */
108 +#define GPTU_STOP_TIMER                 0x04    /*  Suspend timer/counter.                  */
109 +#define GPTU_GET_COUNT_VALUE            0x05    /*  Get current count value.                */
110 +#define GPTU_CALCULATE_DIVIDER          0x06    /*  Calculate timer divider from given freq.*/
111 +#define GPTU_SET_TIMER                  0x07    /*  Simplified method to setup timer.       */
112 +#define GPTU_SET_COUNTER                0x08    /*  Simplified method to setup counter.     */
113 +
114 +/*
115 + *  Data Type Used to Call ioctl
116 + */
117 +struct gptu_ioctl_param {
118 +    unsigned int                        timer;  /*  In command GPTU_REQUEST_TIMER, GPTU_SET_TIMER, and  *
119 +                                                 *  GPTU_SET_COUNTER, this field is ID of expected      *
120 +                                                 *  timer/counter. If it's zero, a timer/counter would  *
121 +                                                 *  be dynamically allocated and ID would be stored in  *
122 +                                                 *  this field.                                         *
123 +                                                 *  In command GPTU_GET_COUNT_VALUE, this field is      *
124 +                                                 *  ignored.                                            *
125 +                                                 *  In other command, this field is ID of timer/counter *
126 +                                                 *  allocated.                                          */
127 +    unsigned int                        flag;   /*  In command GPTU_REQUEST_TIMER, GPTU_SET_TIMER, and  *
128 +                                                 *  GPTU_SET_COUNTER, this field contains flags to      *
129 +                                                 *  specify how to configure timer/counter.             *
130 +                                                 *  In command GPTU_START_TIMER, zero indicate start    *
131 +                                                 *  and non-zero indicate resume timer/counter.         *
132 +                                                 *  In other command, this field is ignored.            */
133 +    unsigned long                       value;  /*  In command GPTU_REQUEST_TIMER, this field contains  *
134 +                                                 *  init/reload value.                                  *
135 +                                                 *  In command GPTU_SET_TIMER, this field contains      *
136 +                                                 *  frequency (0.001Hz) of timer.                       *
137 +                                                 *  In command GPTU_GET_COUNT_VALUE, current count      *
138 +                                                 *  value would be stored in this field.                *
139 +                                                 *  In command GPTU_CALCULATE_DIVIDER, this field       *
140 +                                                 *  contains frequency wanted, and after calculation,   *
141 +                                                 *  divider would be stored in this field to overwrite  *
142 +                                                 *  the frequency.                                      *
143 +                                                 *  In other command, this field is ignored.            */
144 +    int                                 pid;    /*  In command GPTU_REQUEST_TIMER and GPTU_SET_TIMER,   *
145 +                                                 *  if signal is required, this field contains process  *
146 +                                                 *  ID to which signal would be sent.                   *
147 +                                                 *  In other command, this field is ignored.            */
148 +    int                                 sig;    /*  In command GPTU_REQUEST_TIMER and GPTU_SET_TIMER,   *
149 +                                                 *  if signal is required, this field contains signal   *
150 +                                                 *  number which would be sent.                         *
151 +                                                 *  In other command, this field is ignored.            */
152 +};
153 +
154 +/*
155 + * ####################################
156 + *              Data Type
157 + * ####################################
158 + */
159 +typedef void (*timer_callback)(unsigned long arg);
160 +
161 +extern int lq_request_timer(unsigned int, unsigned int, unsigned long, unsigned long, unsigned long);
162 +extern int lq_free_timer(unsigned int);
163 +extern int lq_start_timer(unsigned int, int);
164 +extern int lq_stop_timer(unsigned int);
165 +extern int lq_reset_counter_flags(u32 timer, u32 flags);
166 +extern int lq_get_count_value(unsigned int, unsigned long *);
167 +extern u32 lq_cal_divider(unsigned long);
168 +extern int lq_set_timer(unsigned int, unsigned int, int, int, unsigned int, unsigned long, unsigned long);
169 +extern int lq_set_counter(unsigned int timer, unsigned int flag,
170 +       u32 reload, unsigned long arg1, unsigned long arg2);
171 +
172 +#endif /* __DANUBE_GPTU_DEV_H__2005_07_26__10_19__ */
173 --- a/arch/mips/lantiq/xway/Makefile
174 +++ b/arch/mips/lantiq/xway/Makefile
175 @@ -1,4 +1,4 @@
176 -obj-y := prom.o sysctrl.o clk.o reset.o dma.o gptu.o dcdc.o
177 +obj-y := prom.o sysctrl.o clk.o reset.o dma.o timer.o dcdc.o
178  
179  obj-y += vmmc.o tffs.o
180  
181 --- /dev/null
182 +++ b/arch/mips/lantiq/xway/timer.c
183 @@ -0,0 +1,845 @@
184 +#ifndef CONFIG_SOC_AMAZON_SE
185 +
186 +#include <linux/kernel.h>
187 +#include <linux/module.h>
188 +#include <linux/version.h>
189 +#include <linux/types.h>
190 +#include <linux/fs.h>
191 +#include <linux/miscdevice.h>
192 +#include <linux/init.h>
193 +#include <linux/uaccess.h>
194 +#include <linux/unistd.h>
195 +#include <linux/errno.h>
196 +#include <linux/interrupt.h>
197 +#include <linux/sched.h>
198 +
199 +#include <asm/irq.h>
200 +#include <asm/div64.h>
201 +#include "../clk.h"
202 +
203 +#include <lantiq_soc.h>
204 +#include <lantiq_irq.h>
205 +#include <lantiq_timer.h>
206 +
207 +#define MAX_NUM_OF_32BIT_TIMER_BLOCKS  6
208 +
209 +#ifdef TIMER1A
210 +#define FIRST_TIMER                    TIMER1A
211 +#else
212 +#define FIRST_TIMER                    2
213 +#endif
214 +
215 +/*
216 + *  GPTC divider is set or not.
217 + */
218 +#define GPTU_CLC_RMC_IS_SET            0
219 +
220 +/*
221 + *  Timer Interrupt (IRQ)
222 + */
223 +/*  Must be adjusted when ICU driver is available */
224 +#define TIMER_INTERRUPT                        (INT_NUM_IM3_IRL0 + 22)
225 +
226 +/*
227 + *  Bits Operation
228 + */
229 +#define GET_BITS(x, msb, lsb)          \
230 +       (((x) & ((1 << ((msb) + 1)) - 1)) >> (lsb))
231 +#define SET_BITS(x, msb, lsb, value)   \
232 +       (((x) & ~(((1 << ((msb) + 1)) - 1) ^ ((1 << (lsb)) - 1))) | \
233 +       (((value) & ((1 << (1 + (msb) - (lsb))) - 1)) << (lsb)))
234 +
235 +/*
236 + *  GPTU Register Mapping
237 + */
238 +#define LQ_GPTU                        (KSEG1 + 0x1E100A00)
239 +#define LQ_GPTU_CLC            ((volatile u32 *)(LQ_GPTU + 0x0000))
240 +#define LQ_GPTU_ID                     ((volatile u32 *)(LQ_GPTU + 0x0008))
241 +#define LQ_GPTU_CON(n, X)              ((volatile u32 *)(LQ_GPTU + 0x0010 + ((X) * 4) + ((n) - 1) * 0x0020))   /* X must be either A or B */
242 +#define LQ_GPTU_RUN(n, X)              ((volatile u32 *)(LQ_GPTU + 0x0018 + ((X) * 4) + ((n) - 1) * 0x0020))   /* X must be either A or B */
243 +#define LQ_GPTU_RELOAD(n, X)   ((volatile u32 *)(LQ_GPTU + 0x0020 + ((X) * 4) + ((n) - 1) * 0x0020))   /* X must be either A or B */
244 +#define LQ_GPTU_COUNT(n, X)    ((volatile u32 *)(LQ_GPTU + 0x0028 + ((X) * 4) + ((n) - 1) * 0x0020))   /* X must be either A or B */
245 +#define LQ_GPTU_IRNEN          ((volatile u32 *)(LQ_GPTU + 0x00F4))
246 +#define LQ_GPTU_IRNICR         ((volatile u32 *)(LQ_GPTU + 0x00F8))
247 +#define LQ_GPTU_IRNCR          ((volatile u32 *)(LQ_GPTU + 0x00FC))
248 +
249 +/*
250 + *  Clock Control Register
251 + */
252 +#define GPTU_CLC_SMC                   GET_BITS(*LQ_GPTU_CLC, 23, 16)
253 +#define GPTU_CLC_RMC                   GET_BITS(*LQ_GPTU_CLC, 15, 8)
254 +#define GPTU_CLC_FSOE                  (*LQ_GPTU_CLC & (1 << 5))
255 +#define GPTU_CLC_EDIS                  (*LQ_GPTU_CLC & (1 << 3))
256 +#define GPTU_CLC_SPEN                  (*LQ_GPTU_CLC & (1 << 2))
257 +#define GPTU_CLC_DISS                  (*LQ_GPTU_CLC & (1 << 1))
258 +#define GPTU_CLC_DISR                  (*LQ_GPTU_CLC & (1 << 0))
259 +
260 +#define GPTU_CLC_SMC_SET(value)                SET_BITS(0, 23, 16, (value))
261 +#define GPTU_CLC_RMC_SET(value)                SET_BITS(0, 15, 8, (value))
262 +#define GPTU_CLC_FSOE_SET(value)       ((value) ? (1 << 5) : 0)
263 +#define GPTU_CLC_SBWE_SET(value)       ((value) ? (1 << 4) : 0)
264 +#define GPTU_CLC_EDIS_SET(value)       ((value) ? (1 << 3) : 0)
265 +#define GPTU_CLC_SPEN_SET(value)       ((value) ? (1 << 2) : 0)
266 +#define GPTU_CLC_DISR_SET(value)       ((value) ? (1 << 0) : 0)
267 +
268 +/*
269 + *  ID Register
270 + */
271 +#define GPTU_ID_ID                     GET_BITS(*LQ_GPTU_ID, 15, 8)
272 +#define GPTU_ID_CFG                    GET_BITS(*LQ_GPTU_ID, 7, 5)
273 +#define GPTU_ID_REV                    GET_BITS(*LQ_GPTU_ID, 4, 0)
274 +
275 +/*
276 + *  Control Register of Timer/Counter nX
277 + *    n is the index of block (1 based index)
278 + *    X is either A or B
279 + */
280 +#define GPTU_CON_SRC_EG(n, X)          (*LQ_GPTU_CON(n, X) & (1 << 10))
281 +#define GPTU_CON_SRC_EXT(n, X)         (*LQ_GPTU_CON(n, X) & (1 << 9))
282 +#define GPTU_CON_SYNC(n, X)            (*LQ_GPTU_CON(n, X) & (1 << 8))
283 +#define GPTU_CON_EDGE(n, X)            GET_BITS(*LQ_GPTU_CON(n, X), 7, 6)
284 +#define GPTU_CON_INV(n, X)             (*LQ_GPTU_CON(n, X) & (1 << 5))
285 +#define GPTU_CON_EXT(n, X)             (*LQ_GPTU_CON(n, A) & (1 << 4)) /* Timer/Counter B does not have this bit */
286 +#define GPTU_CON_STP(n, X)             (*LQ_GPTU_CON(n, X) & (1 << 3))
287 +#define GPTU_CON_CNT(n, X)             (*LQ_GPTU_CON(n, X) & (1 << 2))
288 +#define GPTU_CON_DIR(n, X)             (*LQ_GPTU_CON(n, X) & (1 << 1))
289 +#define GPTU_CON_EN(n, X)              (*LQ_GPTU_CON(n, X) & (1 << 0))
290 +
291 +#define GPTU_CON_SRC_EG_SET(value)     ((value) ? 0 : (1 << 10))
292 +#define GPTU_CON_SRC_EXT_SET(value)    ((value) ? (1 << 9) : 0)
293 +#define GPTU_CON_SYNC_SET(value)       ((value) ? (1 << 8) : 0)
294 +#define GPTU_CON_EDGE_SET(value)       SET_BITS(0, 7, 6, (value))
295 +#define GPTU_CON_INV_SET(value)                ((value) ? (1 << 5) : 0)
296 +#define GPTU_CON_EXT_SET(value)                ((value) ? (1 << 4) : 0)
297 +#define GPTU_CON_STP_SET(value)                ((value) ? (1 << 3) : 0)
298 +#define GPTU_CON_CNT_SET(value)                ((value) ? (1 << 2) : 0)
299 +#define GPTU_CON_DIR_SET(value)                ((value) ? (1 << 1) : 0)
300 +
301 +#define GPTU_RUN_RL_SET(value)         ((value) ? (1 << 2) : 0)
302 +#define GPTU_RUN_CEN_SET(value)                ((value) ? (1 << 1) : 0)
303 +#define GPTU_RUN_SEN_SET(value)                ((value) ? (1 << 0) : 0)
304 +
305 +#define GPTU_IRNEN_TC_SET(n, X, value) ((value) ? (1 << (((n) - 1) * 2 + (X))) : 0)
306 +#define GPTU_IRNCR_TC_SET(n, X, value) ((value) ? (1 << (((n) - 1) * 2 + (X))) : 0)
307 +
308 +#define TIMER_FLAG_MASK_SIZE(x)                (x & 0x0001)
309 +#define TIMER_FLAG_MASK_TYPE(x)                (x & 0x0002)
310 +#define TIMER_FLAG_MASK_STOP(x)                (x & 0x0004)
311 +#define TIMER_FLAG_MASK_DIR(x)         (x & 0x0008)
312 +#define TIMER_FLAG_NONE_EDGE           0x0000
313 +#define TIMER_FLAG_MASK_EDGE(x)                (x & 0x0030)
314 +#define TIMER_FLAG_REAL                        0x0000
315 +#define TIMER_FLAG_INVERT              0x0040
316 +#define TIMER_FLAG_MASK_INVERT(x)      (x & 0x0040)
317 +#define TIMER_FLAG_MASK_TRIGGER(x)     (x & 0x0070)
318 +#define TIMER_FLAG_MASK_SYNC(x)                (x & 0x0080)
319 +#define TIMER_FLAG_CALLBACK_IN_HB      0x0200
320 +#define TIMER_FLAG_MASK_HANDLE(x)      (x & 0x0300)
321 +#define TIMER_FLAG_MASK_SRC(x)         (x & 0x1000)
322 +
323 +struct timer_dev_timer {
324 +       unsigned int f_irq_on;
325 +       unsigned int irq;
326 +       unsigned int flag;
327 +       unsigned long arg1;
328 +       unsigned long arg2;
329 +};
330 +
331 +struct timer_dev {
332 +       struct mutex gptu_mutex;
333 +       unsigned int number_of_timers;
334 +       unsigned int occupation;
335 +       unsigned int f_gptu_on;
336 +       struct timer_dev_timer timer[MAX_NUM_OF_32BIT_TIMER_BLOCKS * 2];
337 +};
338 +
339 +
340 +unsigned int ltq_get_fpi_bus_clock(int fpi) {
341 +       struct clk *clk = clk_get_fpi();
342 +       return clk_get_rate(clk);
343 +}
344 +
345 +
346 +static long gptu_ioctl(struct file *, unsigned int, unsigned long);
347 +static int gptu_open(struct inode *, struct file *);
348 +static int gptu_release(struct inode *, struct file *);
349 +
350 +static struct file_operations gptu_fops = {
351 +       .owner = THIS_MODULE,
352 +       .unlocked_ioctl = gptu_ioctl,
353 +       .open = gptu_open,
354 +       .release = gptu_release
355 +};
356 +
357 +static struct miscdevice gptu_miscdev = {
358 +       .minor = MISC_DYNAMIC_MINOR,
359 +       .name = "gptu",
360 +       .fops = &gptu_fops,
361 +};
362 +
363 +static struct timer_dev timer_dev;
364 +
365 +static irqreturn_t timer_irq_handler(int irq, void *p)
366 +{
367 +       unsigned int timer;
368 +       unsigned int flag;
369 +       struct timer_dev_timer *dev_timer = (struct timer_dev_timer *)p;
370 +
371 +       timer = irq - TIMER_INTERRUPT;
372 +       if (timer < timer_dev.number_of_timers
373 +               && dev_timer == &timer_dev.timer[timer]) {
374 +               /*  Clear interrupt.    */
375 +               ltq_w32(1 << timer, LQ_GPTU_IRNCR);
376 +
377 +               /*  Call user hanler or signal. */
378 +               flag = dev_timer->flag;
379 +               if (!(timer & 0x01)
380 +                       || TIMER_FLAG_MASK_SIZE(flag) == TIMER_FLAG_16BIT) {
381 +                       /* 16-bit timer or timer A of 32-bit timer  */
382 +                       switch (TIMER_FLAG_MASK_HANDLE(flag)) {
383 +                       case TIMER_FLAG_CALLBACK_IN_IRQ:
384 +                       case TIMER_FLAG_CALLBACK_IN_HB:
385 +                               if (dev_timer->arg1)
386 +                                       (*(timer_callback)dev_timer->arg1)(dev_timer->arg2);
387 +                               break;
388 +                       case TIMER_FLAG_SIGNAL:
389 +                               send_sig((int)dev_timer->arg2, (struct task_struct *)dev_timer->arg1, 0);
390 +                               break;
391 +                       }
392 +               }
393 +       }
394 +       return IRQ_HANDLED;
395 +}
396 +
397 +static inline void lq_enable_gptu(void)
398 +{
399 +       struct clk *clk = clk_get_sys("1e100a00.gptu", NULL);
400 +       clk_enable(clk);
401 +
402 +       //ltq_pmu_enable(PMU_GPT);
403 +
404 +       /*  Set divider as 1, disable write protection for SPEN, enable module. */
405 +       *LQ_GPTU_CLC =
406 +               GPTU_CLC_SMC_SET(0x00) |
407 +               GPTU_CLC_RMC_SET(0x01) |
408 +               GPTU_CLC_FSOE_SET(0) |
409 +               GPTU_CLC_SBWE_SET(1) |
410 +               GPTU_CLC_EDIS_SET(0) |
411 +               GPTU_CLC_SPEN_SET(0) |
412 +               GPTU_CLC_DISR_SET(0);
413 +}
414 +
415 +static inline void lq_disable_gptu(void)
416 +{
417 +       struct clk *clk = clk_get_sys("1e100a00.gptu", NULL);
418 +       ltq_w32(0x00, LQ_GPTU_IRNEN);
419 +       ltq_w32(0xfff, LQ_GPTU_IRNCR);
420 +
421 +       /*  Set divider as 0, enable write protection for SPEN, disable module. */
422 +       *LQ_GPTU_CLC =
423 +               GPTU_CLC_SMC_SET(0x00) |
424 +               GPTU_CLC_RMC_SET(0x00) |
425 +               GPTU_CLC_FSOE_SET(0) |
426 +               GPTU_CLC_SBWE_SET(0) |
427 +               GPTU_CLC_EDIS_SET(0) |
428 +               GPTU_CLC_SPEN_SET(0) |
429 +               GPTU_CLC_DISR_SET(1);
430 +
431 +       clk_enable(clk);
432 +}
433 +
434 +int lq_request_timer(unsigned int timer, unsigned int flag,
435 +       unsigned long value, unsigned long arg1, unsigned long arg2)
436 +{
437 +       int ret = 0;
438 +       unsigned int con_reg, irnen_reg;
439 +       int n, X;
440 +
441 +       if (timer >= FIRST_TIMER + timer_dev.number_of_timers)
442 +               return -EINVAL;
443 +
444 +       printk(KERN_INFO "request_timer(%d, 0x%08X, %lu)...",
445 +               timer, flag, value);
446 +
447 +       if (TIMER_FLAG_MASK_SIZE(flag) == TIMER_FLAG_16BIT)
448 +               value &= 0xFFFF;
449 +       else
450 +               timer &= ~0x01;
451 +
452 +       mutex_lock(&timer_dev.gptu_mutex);
453 +
454 +       /*
455 +        *  Allocate timer.
456 +        */
457 +       if (timer < FIRST_TIMER) {
458 +               unsigned int mask;
459 +               unsigned int shift;
460 +               /* This takes care of TIMER1B which is the only choice for Voice TAPI system */
461 +               unsigned int offset = TIMER2A;
462 +
463 +               /*
464 +                *  Pick up a free timer.
465 +                */
466 +               if (TIMER_FLAG_MASK_SIZE(flag) == TIMER_FLAG_16BIT) {
467 +                       mask = 1 << offset;
468 +                       shift = 1;
469 +               } else {
470 +                       mask = 3 << offset;
471 +                       shift = 2;
472 +               }
473 +               for (timer = offset;
474 +                    timer < offset + timer_dev.number_of_timers;
475 +                    timer += shift, mask <<= shift)
476 +                       if (!(timer_dev.occupation & mask)) {
477 +                               timer_dev.occupation |= mask;
478 +                               break;
479 +                       }
480 +               if (timer >= offset + timer_dev.number_of_timers) {
481 +                       printk("failed![%d]\n", __LINE__);
482 +                       mutex_unlock(&timer_dev.gptu_mutex);
483 +                       return -EINVAL;
484 +               } else
485 +                       ret = timer;
486 +       } else {
487 +               register unsigned int mask;
488 +
489 +               /*
490 +                *  Check if the requested timer is free.
491 +                */
492 +               mask = (TIMER_FLAG_MASK_SIZE(flag) == TIMER_FLAG_16BIT ? 1 : 3) << timer;
493 +               if ((timer_dev.occupation & mask)) {
494 +                       printk("failed![%d] mask %#x, timer_dev.occupation %#x\n",
495 +                               __LINE__, mask, timer_dev.occupation);
496 +                       mutex_unlock(&timer_dev.gptu_mutex);
497 +                       return -EBUSY;
498 +               } else {
499 +                       timer_dev.occupation |= mask;
500 +                       ret = 0;
501 +               }
502 +       }
503 +
504 +       /*
505 +        *  Prepare control register value.
506 +        */
507 +       switch (TIMER_FLAG_MASK_EDGE(flag)) {
508 +       default:
509 +       case TIMER_FLAG_NONE_EDGE:
510 +               con_reg = GPTU_CON_EDGE_SET(0x00);
511 +               break;
512 +       case TIMER_FLAG_RISE_EDGE:
513 +               con_reg = GPTU_CON_EDGE_SET(0x01);
514 +               break;
515 +       case TIMER_FLAG_FALL_EDGE:
516 +               con_reg = GPTU_CON_EDGE_SET(0x02);
517 +               break;
518 +       case TIMER_FLAG_ANY_EDGE:
519 +               con_reg = GPTU_CON_EDGE_SET(0x03);
520 +               break;
521 +       }
522 +       if (TIMER_FLAG_MASK_TYPE(flag) == TIMER_FLAG_TIMER)
523 +               con_reg |=
524 +                       TIMER_FLAG_MASK_SRC(flag) ==
525 +                       TIMER_FLAG_EXT_SRC ? GPTU_CON_SRC_EXT_SET(1) :
526 +                       GPTU_CON_SRC_EXT_SET(0);
527 +       else
528 +               con_reg |=
529 +                       TIMER_FLAG_MASK_SRC(flag) ==
530 +                       TIMER_FLAG_EXT_SRC ? GPTU_CON_SRC_EG_SET(1) :
531 +                       GPTU_CON_SRC_EG_SET(0);
532 +       con_reg |=
533 +               TIMER_FLAG_MASK_SYNC(flag) ==
534 +               TIMER_FLAG_UNSYNC ? GPTU_CON_SYNC_SET(0) :
535 +               GPTU_CON_SYNC_SET(1);
536 +       con_reg |=
537 +               TIMER_FLAG_MASK_INVERT(flag) ==
538 +               TIMER_FLAG_REAL ? GPTU_CON_INV_SET(0) : GPTU_CON_INV_SET(1);
539 +       con_reg |=
540 +               TIMER_FLAG_MASK_SIZE(flag) ==
541 +               TIMER_FLAG_16BIT ? GPTU_CON_EXT_SET(0) :
542 +               GPTU_CON_EXT_SET(1);
543 +       con_reg |=
544 +               TIMER_FLAG_MASK_STOP(flag) ==
545 +               TIMER_FLAG_ONCE ? GPTU_CON_STP_SET(1) : GPTU_CON_STP_SET(0);
546 +       con_reg |=
547 +               TIMER_FLAG_MASK_TYPE(flag) ==
548 +               TIMER_FLAG_TIMER ? GPTU_CON_CNT_SET(0) :
549 +               GPTU_CON_CNT_SET(1);
550 +       con_reg |=
551 +               TIMER_FLAG_MASK_DIR(flag) ==
552 +               TIMER_FLAG_UP ? GPTU_CON_DIR_SET(1) : GPTU_CON_DIR_SET(0);
553 +
554 +       /*
555 +        *  Fill up running data.
556 +        */
557 +       timer_dev.timer[timer - FIRST_TIMER].flag = flag;
558 +       timer_dev.timer[timer - FIRST_TIMER].arg1 = arg1;
559 +       timer_dev.timer[timer - FIRST_TIMER].arg2 = arg2;
560 +       if (TIMER_FLAG_MASK_SIZE(flag) != TIMER_FLAG_16BIT)
561 +               timer_dev.timer[timer - FIRST_TIMER + 1].flag = flag;
562 +
563 +       /*
564 +        *  Enable GPTU module.
565 +        */
566 +       if (!timer_dev.f_gptu_on) {
567 +               lq_enable_gptu();
568 +               timer_dev.f_gptu_on = 1;
569 +       }
570 +
571 +       /*
572 +        *  Enable IRQ.
573 +        */
574 +       if (TIMER_FLAG_MASK_HANDLE(flag) != TIMER_FLAG_NO_HANDLE) {
575 +               if (TIMER_FLAG_MASK_HANDLE(flag) == TIMER_FLAG_SIGNAL)
576 +                       timer_dev.timer[timer - FIRST_TIMER].arg1 =
577 +                               (unsigned long) find_task_by_vpid((int) arg1);
578 +
579 +               irnen_reg = 1 << (timer - FIRST_TIMER);
580 +
581 +               if (TIMER_FLAG_MASK_HANDLE(flag) == TIMER_FLAG_SIGNAL
582 +                   || (TIMER_FLAG_MASK_HANDLE(flag) ==
583 +                       TIMER_FLAG_CALLBACK_IN_IRQ
584 +                       && timer_dev.timer[timer - FIRST_TIMER].arg1)) {
585 +                       enable_irq(timer_dev.timer[timer - FIRST_TIMER].irq);
586 +                       timer_dev.timer[timer - FIRST_TIMER].f_irq_on = 1;
587 +               }
588 +       } else
589 +               irnen_reg = 0;
590 +
591 +       /*
592 +        *  Write config register, reload value and enable interrupt.
593 +        */
594 +       n = timer >> 1;
595 +       X = timer & 0x01;
596 +       *LQ_GPTU_CON(n, X) = con_reg;
597 +       *LQ_GPTU_RELOAD(n, X) = value;
598 +       /* printk("reload value = %d\n", (u32)value); */
599 +       *LQ_GPTU_IRNEN |= irnen_reg;
600 +
601 +       mutex_unlock(&timer_dev.gptu_mutex);
602 +       printk("successful!\n");
603 +       return ret;
604 +}
605 +EXPORT_SYMBOL(lq_request_timer);
606 +
607 +int lq_free_timer(unsigned int timer)
608 +{
609 +       unsigned int flag;
610 +       unsigned int mask;
611 +       int n, X;
612 +
613 +       if (!timer_dev.f_gptu_on)
614 +               return -EINVAL;
615 +
616 +       if (timer < FIRST_TIMER || timer >= FIRST_TIMER + timer_dev.number_of_timers)
617 +               return -EINVAL;
618 +
619 +       mutex_lock(&timer_dev.gptu_mutex);
620 +
621 +       flag = timer_dev.timer[timer - FIRST_TIMER].flag;
622 +       if (TIMER_FLAG_MASK_SIZE(flag) != TIMER_FLAG_16BIT)
623 +               timer &= ~0x01;
624 +
625 +       mask = (TIMER_FLAG_MASK_SIZE(flag) == TIMER_FLAG_16BIT ? 1 : 3) << timer;
626 +       if (((timer_dev.occupation & mask) ^ mask)) {
627 +               mutex_unlock(&timer_dev.gptu_mutex);
628 +               return -EINVAL;
629 +       }
630 +
631 +       n = timer >> 1;
632 +       X = timer & 0x01;
633 +
634 +       if (GPTU_CON_EN(n, X))
635 +               *LQ_GPTU_RUN(n, X) = GPTU_RUN_CEN_SET(1);
636 +
637 +       *LQ_GPTU_IRNEN &= ~GPTU_IRNEN_TC_SET(n, X, 1);
638 +       *LQ_GPTU_IRNCR |= GPTU_IRNCR_TC_SET(n, X, 1);
639 +
640 +       if (timer_dev.timer[timer - FIRST_TIMER].f_irq_on) {
641 +               disable_irq(timer_dev.timer[timer - FIRST_TIMER].irq);
642 +               timer_dev.timer[timer - FIRST_TIMER].f_irq_on = 0;
643 +       }
644 +
645 +       timer_dev.occupation &= ~mask;
646 +       if (!timer_dev.occupation && timer_dev.f_gptu_on) {
647 +               lq_disable_gptu();
648 +               timer_dev.f_gptu_on = 0;
649 +       }
650 +
651 +       mutex_unlock(&timer_dev.gptu_mutex);
652 +
653 +       return 0;
654 +}
655 +EXPORT_SYMBOL(lq_free_timer);
656 +
657 +int lq_start_timer(unsigned int timer, int is_resume)
658 +{
659 +       unsigned int flag;
660 +       unsigned int mask;
661 +       int n, X;
662 +
663 +       if (!timer_dev.f_gptu_on)
664 +               return -EINVAL;
665 +
666 +       if (timer < FIRST_TIMER || timer >= FIRST_TIMER + timer_dev.number_of_timers)
667 +               return -EINVAL;
668 +
669 +       mutex_lock(&timer_dev.gptu_mutex);
670 +
671 +       flag = timer_dev.timer[timer - FIRST_TIMER].flag;
672 +       if (TIMER_FLAG_MASK_SIZE(flag) != TIMER_FLAG_16BIT)
673 +               timer &= ~0x01;
674 +
675 +       mask = (TIMER_FLAG_MASK_SIZE(flag) ==
676 +       TIMER_FLAG_16BIT ? 1 : 3) << timer;
677 +       if (((timer_dev.occupation & mask) ^ mask)) {
678 +               mutex_unlock(&timer_dev.gptu_mutex);
679 +               return -EINVAL;
680 +       }
681 +
682 +       n = timer >> 1;
683 +       X = timer & 0x01;
684 +
685 +       *LQ_GPTU_RUN(n, X) = GPTU_RUN_RL_SET(!is_resume) | GPTU_RUN_SEN_SET(1);
686 +
687 +
688 +       mutex_unlock(&timer_dev.gptu_mutex);
689 +
690 +       return 0;
691 +}
692 +EXPORT_SYMBOL(lq_start_timer);
693 +
694 +int lq_stop_timer(unsigned int timer)
695 +{
696 +       unsigned int flag;
697 +       unsigned int mask;
698 +       int n, X;
699 +
700 +       if (!timer_dev.f_gptu_on)
701 +               return -EINVAL;
702 +
703 +       if (timer < FIRST_TIMER
704 +           || timer >= FIRST_TIMER + timer_dev.number_of_timers)
705 +               return -EINVAL;
706 +
707 +       mutex_lock(&timer_dev.gptu_mutex);
708 +
709 +       flag = timer_dev.timer[timer - FIRST_TIMER].flag;
710 +       if (TIMER_FLAG_MASK_SIZE(flag) != TIMER_FLAG_16BIT)
711 +               timer &= ~0x01;
712 +
713 +       mask = (TIMER_FLAG_MASK_SIZE(flag) == TIMER_FLAG_16BIT ? 1 : 3) << timer;
714 +       if (((timer_dev.occupation & mask) ^ mask)) {
715 +               mutex_unlock(&timer_dev.gptu_mutex);
716 +               return -EINVAL;
717 +       }
718 +
719 +       n = timer >> 1;
720 +       X = timer & 0x01;
721 +
722 +       *LQ_GPTU_RUN(n, X) = GPTU_RUN_CEN_SET(1);
723 +
724 +       mutex_unlock(&timer_dev.gptu_mutex);
725 +
726 +       return 0;
727 +}
728 +EXPORT_SYMBOL(lq_stop_timer);
729 +
730 +int lq_reset_counter_flags(u32 timer, u32 flags)
731 +{
732 +       unsigned int oflag;
733 +       unsigned int mask, con_reg;
734 +       int n, X;
735 +
736 +       if (!timer_dev.f_gptu_on)
737 +               return -EINVAL;
738 +
739 +       if (timer < FIRST_TIMER || timer >= FIRST_TIMER + timer_dev.number_of_timers)
740 +               return -EINVAL;
741 +
742 +       mutex_lock(&timer_dev.gptu_mutex);
743 +
744 +       oflag = timer_dev.timer[timer - FIRST_TIMER].flag;
745 +       if (TIMER_FLAG_MASK_SIZE(oflag) != TIMER_FLAG_16BIT)
746 +               timer &= ~0x01;
747 +
748 +       mask = (TIMER_FLAG_MASK_SIZE(oflag) == TIMER_FLAG_16BIT ? 1 : 3) << timer;
749 +       if (((timer_dev.occupation & mask) ^ mask)) {
750 +               mutex_unlock(&timer_dev.gptu_mutex);
751 +               return -EINVAL;
752 +       }
753 +
754 +       switch (TIMER_FLAG_MASK_EDGE(flags)) {
755 +       default:
756 +       case TIMER_FLAG_NONE_EDGE:
757 +               con_reg = GPTU_CON_EDGE_SET(0x00);
758 +               break;
759 +       case TIMER_FLAG_RISE_EDGE:
760 +               con_reg = GPTU_CON_EDGE_SET(0x01);
761 +               break;
762 +       case TIMER_FLAG_FALL_EDGE:
763 +               con_reg = GPTU_CON_EDGE_SET(0x02);
764 +               break;
765 +       case TIMER_FLAG_ANY_EDGE:
766 +               con_reg = GPTU_CON_EDGE_SET(0x03);
767 +               break;
768 +       }
769 +       if (TIMER_FLAG_MASK_TYPE(flags) == TIMER_FLAG_TIMER)
770 +               con_reg |= TIMER_FLAG_MASK_SRC(flags) == TIMER_FLAG_EXT_SRC ? GPTU_CON_SRC_EXT_SET(1) : GPTU_CON_SRC_EXT_SET(0);
771 +       else
772 +               con_reg |= TIMER_FLAG_MASK_SRC(flags) == TIMER_FLAG_EXT_SRC ? GPTU_CON_SRC_EG_SET(1) : GPTU_CON_SRC_EG_SET(0);
773 +       con_reg |= TIMER_FLAG_MASK_SYNC(flags) == TIMER_FLAG_UNSYNC ? GPTU_CON_SYNC_SET(0) : GPTU_CON_SYNC_SET(1);
774 +       con_reg |= TIMER_FLAG_MASK_INVERT(flags) == TIMER_FLAG_REAL ? GPTU_CON_INV_SET(0) : GPTU_CON_INV_SET(1);
775 +       con_reg |= TIMER_FLAG_MASK_SIZE(flags) == TIMER_FLAG_16BIT ? GPTU_CON_EXT_SET(0) : GPTU_CON_EXT_SET(1);
776 +       con_reg |= TIMER_FLAG_MASK_STOP(flags) == TIMER_FLAG_ONCE ? GPTU_CON_STP_SET(1) : GPTU_CON_STP_SET(0);
777 +       con_reg |= TIMER_FLAG_MASK_TYPE(flags) == TIMER_FLAG_TIMER ? GPTU_CON_CNT_SET(0) : GPTU_CON_CNT_SET(1);
778 +       con_reg |= TIMER_FLAG_MASK_DIR(flags) == TIMER_FLAG_UP ? GPTU_CON_DIR_SET(1) : GPTU_CON_DIR_SET(0);
779 +
780 +       timer_dev.timer[timer - FIRST_TIMER].flag = flags;
781 +       if (TIMER_FLAG_MASK_SIZE(flags) != TIMER_FLAG_16BIT)
782 +               timer_dev.timer[timer - FIRST_TIMER + 1].flag = flags;
783 +
784 +       n = timer >> 1;
785 +       X = timer & 0x01;
786 +
787 +       *LQ_GPTU_CON(n, X) = con_reg;
788 +       smp_wmb();
789 +       mutex_unlock(&timer_dev.gptu_mutex);
790 +       return 0;
791 +}
792 +EXPORT_SYMBOL(lq_reset_counter_flags);
793 +
794 +int lq_get_count_value(unsigned int timer, unsigned long *value)
795 +{
796 +       unsigned int flag;
797 +       unsigned int mask;
798 +       int n, X;
799 +
800 +       if (!timer_dev.f_gptu_on)
801 +               return -EINVAL;
802 +
803 +       if (timer < FIRST_TIMER
804 +           || timer >= FIRST_TIMER + timer_dev.number_of_timers)
805 +               return -EINVAL;
806 +
807 +       mutex_lock(&timer_dev.gptu_mutex);
808 +
809 +       flag = timer_dev.timer[timer - FIRST_TIMER].flag;
810 +       if (TIMER_FLAG_MASK_SIZE(flag) != TIMER_FLAG_16BIT)
811 +               timer &= ~0x01;
812 +
813 +       mask = (TIMER_FLAG_MASK_SIZE(flag) == TIMER_FLAG_16BIT ? 1 : 3) << timer;
814 +       if (((timer_dev.occupation & mask) ^ mask)) {
815 +               mutex_unlock(&timer_dev.gptu_mutex);
816 +               return -EINVAL;
817 +       }
818 +
819 +       n = timer >> 1;
820 +       X = timer & 0x01;
821 +
822 +       *value = *LQ_GPTU_COUNT(n, X);
823 +
824 +
825 +       mutex_unlock(&timer_dev.gptu_mutex);
826 +
827 +       return 0;
828 +}
829 +EXPORT_SYMBOL(lq_get_count_value);
830 +
831 +u32 lq_cal_divider(unsigned long freq)
832 +{
833 +       u64 module_freq, fpi = ltq_get_fpi_bus_clock(2);
834 +       u32 clock_divider = 1;
835 +       module_freq = fpi * 1000;
836 +       do_div(module_freq, clock_divider * freq);
837 +       return module_freq;
838 +}
839 +EXPORT_SYMBOL(lq_cal_divider);
840 +
841 +int lq_set_timer(unsigned int timer, unsigned int freq, int is_cyclic,
842 +       int is_ext_src, unsigned int handle_flag, unsigned long arg1,
843 +       unsigned long arg2)
844 +{
845 +       unsigned long divider;
846 +       unsigned int flag;
847 +
848 +       divider = lq_cal_divider(freq);
849 +       if (divider == 0)
850 +               return -EINVAL;
851 +       flag = ((divider & ~0xFFFF) ? TIMER_FLAG_32BIT : TIMER_FLAG_16BIT)
852 +               | (is_cyclic ? TIMER_FLAG_CYCLIC : TIMER_FLAG_ONCE)
853 +               | (is_ext_src ? TIMER_FLAG_EXT_SRC : TIMER_FLAG_INT_SRC)
854 +               | TIMER_FLAG_TIMER | TIMER_FLAG_DOWN
855 +               | TIMER_FLAG_MASK_HANDLE(handle_flag);
856 +
857 +       printk(KERN_INFO "lq_set_timer(%d, %d), divider = %lu\n",
858 +               timer, freq, divider);
859 +       return lq_request_timer(timer, flag, divider, arg1, arg2);
860 +}
861 +EXPORT_SYMBOL(lq_set_timer);
862 +
863 +int lq_set_counter(unsigned int timer, unsigned int flag, u32 reload,
864 +       unsigned long arg1, unsigned long arg2)
865 +{
866 +       printk(KERN_INFO "lq_set_counter(%d, %#x, %d)\n", timer, flag, reload);
867 +       return lq_request_timer(timer, flag, reload, arg1, arg2);
868 +}
869 +EXPORT_SYMBOL(lq_set_counter);
870 +
871 +static long gptu_ioctl(struct file *file, unsigned int cmd,
872 +       unsigned long arg)
873 +{
874 +       int ret;
875 +       struct gptu_ioctl_param param;
876 +
877 +       if (!access_ok(VERIFY_READ, arg, sizeof(struct gptu_ioctl_param)))
878 +               return -EFAULT;
879 +       copy_from_user(&param, (void *) arg, sizeof(param));
880 +
881 +       if ((((cmd == GPTU_REQUEST_TIMER || cmd == GPTU_SET_TIMER
882 +              || GPTU_SET_COUNTER) && param.timer < 2)
883 +            || cmd == GPTU_GET_COUNT_VALUE || cmd == GPTU_CALCULATE_DIVIDER)
884 +           && !access_ok(VERIFY_WRITE, arg,
885 +                          sizeof(struct gptu_ioctl_param)))
886 +               return -EFAULT;
887 +
888 +       switch (cmd) {
889 +       case GPTU_REQUEST_TIMER:
890 +               ret = lq_request_timer(param.timer, param.flag, param.value,
891 +                                    (unsigned long) param.pid,
892 +                                    (unsigned long) param.sig);
893 +               if (ret > 0) {
894 +                       copy_to_user(&((struct gptu_ioctl_param *) arg)->
895 +                                     timer, &ret, sizeof(&ret));
896 +                       ret = 0;
897 +               }
898 +               break;
899 +       case GPTU_FREE_TIMER:
900 +               ret = lq_free_timer(param.timer);
901 +               break;
902 +       case GPTU_START_TIMER:
903 +               ret = lq_start_timer(param.timer, param.flag);
904 +               break;
905 +       case GPTU_STOP_TIMER:
906 +               ret = lq_stop_timer(param.timer);
907 +               break;
908 +       case GPTU_GET_COUNT_VALUE:
909 +               ret = lq_get_count_value(param.timer, &param.value);
910 +               if (!ret)
911 +                       copy_to_user(&((struct gptu_ioctl_param *) arg)->
912 +                                     value, &param.value,
913 +                                     sizeof(param.value));
914 +               break;
915 +       case GPTU_CALCULATE_DIVIDER:
916 +               param.value = lq_cal_divider(param.value);
917 +               if (param.value == 0)
918 +                       ret = -EINVAL;
919 +               else {
920 +                       copy_to_user(&((struct gptu_ioctl_param *) arg)->
921 +                                     value, &param.value,
922 +                                     sizeof(param.value));
923 +                       ret = 0;
924 +               }
925 +               break;
926 +       case GPTU_SET_TIMER:
927 +               ret = lq_set_timer(param.timer, param.value,
928 +                                TIMER_FLAG_MASK_STOP(param.flag) !=
929 +                                TIMER_FLAG_ONCE ? 1 : 0,
930 +                                TIMER_FLAG_MASK_SRC(param.flag) ==
931 +                                TIMER_FLAG_EXT_SRC ? 1 : 0,
932 +                                TIMER_FLAG_MASK_HANDLE(param.flag) ==
933 +                                TIMER_FLAG_SIGNAL ? TIMER_FLAG_SIGNAL :
934 +                                TIMER_FLAG_NO_HANDLE,
935 +                                (unsigned long) param.pid,
936 +                                (unsigned long) param.sig);
937 +               if (ret > 0) {
938 +                       copy_to_user(&((struct gptu_ioctl_param *) arg)->
939 +                                     timer, &ret, sizeof(&ret));
940 +                       ret = 0;
941 +               }
942 +               break;
943 +       case GPTU_SET_COUNTER:
944 +               lq_set_counter(param.timer, param.flag, param.value, 0, 0);
945 +               if (ret > 0) {
946 +                       copy_to_user(&((struct gptu_ioctl_param *) arg)->
947 +                                     timer, &ret, sizeof(&ret));
948 +                       ret = 0;
949 +               }
950 +               break;
951 +       default:
952 +               ret = -ENOTTY;
953 +       }
954 +
955 +       return ret;
956 +}
957 +
958 +static int gptu_open(struct inode *inode, struct file *file)
959 +{
960 +       return 0;
961 +}
962 +
963 +static int gptu_release(struct inode *inode, struct file *file)
964 +{
965 +       return 0;
966 +}
967 +
968 +int __init lq_gptu_init(void)
969 +{
970 +       int ret;
971 +       unsigned int i;
972 +
973 +       ltq_w32(0, LQ_GPTU_IRNEN);
974 +       ltq_w32(0xfff, LQ_GPTU_IRNCR);
975 +
976 +       memset(&timer_dev, 0, sizeof(timer_dev));
977 +       mutex_init(&timer_dev.gptu_mutex);
978 +
979 +       lq_enable_gptu();
980 +       timer_dev.number_of_timers = GPTU_ID_CFG * 2;
981 +       lq_disable_gptu();
982 +       if (timer_dev.number_of_timers > MAX_NUM_OF_32BIT_TIMER_BLOCKS * 2)
983 +               timer_dev.number_of_timers = MAX_NUM_OF_32BIT_TIMER_BLOCKS * 2;
984 +       printk(KERN_INFO "gptu: totally %d 16-bit timers/counters\n", timer_dev.number_of_timers);
985 +
986 +       ret = misc_register(&gptu_miscdev);
987 +       if (ret) {
988 +               printk(KERN_ERR "gptu: can't misc_register, get error %d\n", -ret);
989 +               return ret;
990 +       } else {
991 +               printk(KERN_INFO "gptu: misc_register on minor %d\n", gptu_miscdev.minor);
992 +       }
993 +
994 +       for (i = 0; i < timer_dev.number_of_timers; i++) {
995 +               ret = request_irq(TIMER_INTERRUPT + i, timer_irq_handler, IRQF_TIMER, gptu_miscdev.name, &timer_dev.timer[i]);
996 +               if (ret) {
997 +                       for (; i >= 0; i--)
998 +                               free_irq(TIMER_INTERRUPT + i, &timer_dev.timer[i]);
999 +                       misc_deregister(&gptu_miscdev);
1000 +                       printk(KERN_ERR "gptu: failed in requesting irq (%d), get error %d\n", i, -ret);
1001 +                       return ret;
1002 +               } else {
1003 +                       timer_dev.timer[i].irq = TIMER_INTERRUPT + i;
1004 +                       disable_irq(timer_dev.timer[i].irq);
1005 +                       printk(KERN_INFO "gptu: succeeded to request irq %d\n", timer_dev.timer[i].irq);
1006 +               }
1007 +       }
1008 +
1009 +       return 0;
1010 +}
1011 +
1012 +void __exit lq_gptu_exit(void)
1013 +{
1014 +       unsigned int i;
1015 +
1016 +       for (i = 0; i < timer_dev.number_of_timers; i++) {
1017 +               if (timer_dev.timer[i].f_irq_on)
1018 +                       disable_irq(timer_dev.timer[i].irq);
1019 +               free_irq(timer_dev.timer[i].irq, &timer_dev.timer[i]);
1020 +       }
1021 +       lq_disable_gptu();
1022 +       misc_deregister(&gptu_miscdev);
1023 +}
1024 +
1025 +module_init(lq_gptu_init);
1026 +module_exit(lq_gptu_exit);
1027 +
1028 +#endif