1 /******************************************************************************
3 * (C)Copyright 1998,1999 SysKonnect,
4 * a business unit of Schneider & Koch & Co. Datensysteme GmbH.
6 * See the file "skfddi.c" for further information.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * The information in this file is provided "AS IS" without warranty.
15 ******************************************************************************/
18 * Timer Driver for FBI board (timer chip 82C54)
24 * 28-Jun-1994 sw Edit v1.6.
25 * MCA: Added support for the SK-NET FDDI-FM2 adapter. The
26 * following functions have been added(+) or modified(*):
27 * hwt_start(*), hwt_stop(*), hwt_restart(*), hwt_read(*)
35 static const char ID_sccs[] = "@(#)hwt.c 1.13 97/04/23 (C) SK " ;
39 * Prototypes of local functions.
41 /* 28-Jun-1994 sw - Note: hwt_restart() is also used in module 'drvfbi.c'. */
42 /*static void hwt_restart() ; */
44 /************************
48 * Start hardware timer (clock ticks are 16us).
54 * smc - A pointer to the SMT Context structure.
56 * time - The time in units of 16us to load the timer with.
60 ************************/
61 #define HWT_MAX (65000)
63 void hwt_start(struct s_smc *smc, u_long time)
70 smc->hw.t_start = time ;
82 * 6.25MHz -> CLK0 : T0 (cnt0 = 16us) -> OUT0
83 * OUT0 -> CLK1 : T1 (cnt1) OUT1 -> ISRA(IS_TIMINT)
85 OUT_82c54_TIMER(3,1<<6 | 3<<4 | 0<<1) ; /* counter 1, mode 0 */
86 OUT_82c54_TIMER(1,cnt & 0xff) ; /* LSB */
87 OUT_82c54_TIMER(1,(cnt>>8) & 0xff) ; /* MSB */
89 * start timer by switching counter 0 to mode 3
90 * T0 resolution 16 us (CLK0=0.16us)
92 OUT_82c54_TIMER(3,0<<6 | 3<<4 | 3<<1) ; /* counter 0, mode 3 */
93 OUT_82c54_TIMER(0,100) ; /* LSB */
94 OUT_82c54_TIMER(0,0) ; /* MSB */
96 outpd(ADDR(B2_TI_INI), (u_long) cnt * 200) ; /* Load timer value. */
97 outpw(ADDR(B2_TI_CRTL), TIM_START) ; /* Start timer. */
99 smc->hw.timer_activ = TRUE ;
102 /************************
106 * Stop hardware timer.
109 * struct s_smc *smc) ;
111 * smc - A pointer to the SMT Context structure.
115 ************************/
116 void hwt_stop(struct s_smc *smc)
119 /* stop counter 0 by switching to mode 0 */
120 OUT_82c54_TIMER(3,0<<6 | 3<<4 | 0<<1) ; /* counter 0, mode 0 */
121 OUT_82c54_TIMER(0,0) ; /* LSB */
122 OUT_82c54_TIMER(0,0) ; /* MSB */
124 outpw(ADDR(B2_TI_CRTL), TIM_STOP) ;
125 outpw(ADDR(B2_TI_CRTL), TIM_CL_IRQ) ;
128 smc->hw.timer_activ = FALSE ;
131 /************************
135 * Initialize hardware timer.
138 * struct s_smc *smc) ;
140 * smc - A pointer to the SMT Context structure.
144 ************************/
145 void hwt_init(struct s_smc *smc)
147 smc->hw.t_start = 0 ;
149 smc->hw.timer_activ = FALSE ;
154 /************************
158 * Clear timer interrupt.
161 * struct s_smc *smc) ;
163 * smc - A pointer to the SMT Context structure.
167 ************************/
168 void hwt_restart(struct s_smc *smc)
172 OUT_82c54_TIMER(3,1<<6 | 3<<4 | 0<<1) ; /* counter 1, mode 0 */
173 OUT_82c54_TIMER(1,1 ) ; /* LSB */
174 OUT_82c54_TIMER(1,0 ) ; /* MSB */
178 /************************
182 * Stop hardware timer and read time elapsed since last start.
184 * u_long hwt_read(smc) ;
186 * smc - A pointer to the SMT Context structure.
188 * The elapsed time since last start in units of 16us.
190 ************************/
191 u_long hwt_read(struct s_smc *smc)
200 if (smc->hw.timer_activ) {
203 OUT_82c54_TIMER(3,1<<6) ; /* latch command */
204 tr = IN_82c54_TIMER(1) & 0xff ;
205 tr += (IN_82c54_TIMER(1) & 0xff)<<8 ;
207 tr = (u_short)((inpd(ADDR(B2_TI_VAL))/200) & 0xffff) ;
210 /* Check if timer expired (or wraparound). */
211 if ((tr > smc->hw.t_start) || (is & IS_TIMINT)) {
213 smc->hw.t_stop = smc->hw.t_start ;
216 smc->hw.t_stop = smc->hw.t_start - tr ;
218 return (smc->hw.t_stop) ;
222 /************************
226 * Stop hardware timer and read timer value and start the timer again.
228 * u_long hwt_read(smc) ;
230 * smc - A pointer to the SMT Context structure.
232 * current timer value in units of 80ns.
234 ************************/
235 u_long hwt_quick_read(struct s_smc *smc)
240 interval = inpd(ADDR(B2_TI_INI)) ;
241 outpw(ADDR(B2_TI_CRTL), TIM_STOP) ;
242 time = inpd(ADDR(B2_TI_VAL)) ;
243 outpd(ADDR(B2_TI_INI),time) ;
244 outpw(ADDR(B2_TI_CRTL), TIM_START) ;
245 outpd(ADDR(B2_TI_INI),interval) ;
250 /************************
252 * hwt_wait_time(smc,start,duration)
254 * This function returnes after the amount of time is elapsed
255 * since the start time.
257 * para start start time
258 * duration time to wait
260 * NOTE: The fuction will return immediately, if the timer is not
262 ************************/
263 void hwt_wait_time(struct s_smc *smc, u_long start, long int duration)
270 * check if timer is running
272 if (smc->hw.timer_activ == FALSE ||
273 hwt_quick_read(smc) == hwt_quick_read(smc)) {
277 interval = inpd(ADDR(B2_TI_INI)) ;
278 if (interval > duration) {
280 diff = (long)(start - hwt_quick_read(smc)) ;
284 } while (diff <= duration) ;
291 if (hwt_quick_read(smc) >= start) {
297 if (hwt_quick_read(smc) < start) {
301 } while (diff <= duration) ;