Fix dnotify/close race
[linux-2.6.22.y-op.git] / drivers / net / skfp / hwt.c
blobe01f8a0f35c6341679b9a1ded6370cadb5808837
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)
22 * Modifications:
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(*)
30 #include "h/types.h"
31 #include "h/fddi.h"
32 #include "h/smc.h"
34 #ifndef lint
35 static const char ID_sccs[] = "@(#)hwt.c 1.13 97/04/23 (C) SK " ;
36 #endif
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 /************************
46 * hwt_start
48 * Start hardware timer (clock ticks are 16us).
50 * void hwt_start(
51 * struct s_smc *smc,
52 * u_long time) ;
53 * In
54 * smc - A pointer to the SMT Context structure.
56 * time - The time in units of 16us to load the timer with.
57 * Out
58 * Nothing.
60 ************************/
61 #define HWT_MAX (65000)
63 void hwt_start(struct s_smc *smc, u_long time)
65 u_short cnt ;
67 if (time > HWT_MAX)
68 time = HWT_MAX ;
70 smc->hw.t_start = time ;
71 smc->hw.t_stop = 0L ;
73 cnt = (u_short)time ;
75 * if time < 16 us
76 * time = 16 us
78 if (!cnt)
79 cnt++ ;
80 #ifndef PCI
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 */
95 #else /* PCI */
96 outpd(ADDR(B2_TI_INI), (u_long) cnt * 200) ; /* Load timer value. */
97 outpw(ADDR(B2_TI_CRTL), TIM_START) ; /* Start timer. */
98 #endif /* PCI */
99 smc->hw.timer_activ = TRUE ;
102 /************************
104 * hwt_stop
106 * Stop hardware timer.
108 * void hwt_stop(
109 * struct s_smc *smc) ;
110 * In
111 * smc - A pointer to the SMT Context structure.
112 * Out
113 * Nothing.
115 ************************/
116 void hwt_stop(struct s_smc *smc)
118 #ifndef PCI
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 */
123 #else /* PCI */
124 outpw(ADDR(B2_TI_CRTL), TIM_STOP) ;
125 outpw(ADDR(B2_TI_CRTL), TIM_CL_IRQ) ;
126 #endif /* PCI */
128 smc->hw.timer_activ = FALSE ;
131 /************************
133 * hwt_init
135 * Initialize hardware timer.
137 * void hwt_init(
138 * struct s_smc *smc) ;
139 * In
140 * smc - A pointer to the SMT Context structure.
141 * Out
142 * Nothing.
144 ************************/
145 void hwt_init(struct s_smc *smc)
147 smc->hw.t_start = 0 ;
148 smc->hw.t_stop = 0 ;
149 smc->hw.timer_activ = FALSE ;
151 hwt_restart(smc) ;
154 /************************
156 * hwt_restart
158 * Clear timer interrupt.
160 * void hwt_restart(
161 * struct s_smc *smc) ;
162 * In
163 * smc - A pointer to the SMT Context structure.
164 * Out
165 * Nothing.
167 ************************/
168 void hwt_restart(struct s_smc *smc)
170 hwt_stop(smc) ;
171 #ifndef PCI
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 */
175 #endif
178 /************************
180 * hwt_read
182 * Stop hardware timer and read time elapsed since last start.
184 * u_long hwt_read(smc) ;
185 * In
186 * smc - A pointer to the SMT Context structure.
187 * Out
188 * The elapsed time since last start in units of 16us.
190 ************************/
191 u_long hwt_read(struct s_smc *smc)
193 u_short tr ;
194 #ifndef PCI
195 u_short is ;
196 #else
197 u_long is ;
198 #endif
200 if (smc->hw.timer_activ) {
201 hwt_stop(smc) ;
202 #ifndef PCI
203 OUT_82c54_TIMER(3,1<<6) ; /* latch command */
204 tr = IN_82c54_TIMER(1) & 0xff ;
205 tr += (IN_82c54_TIMER(1) & 0xff)<<8 ;
206 #else /* PCI */
207 tr = (u_short)((inpd(ADDR(B2_TI_VAL))/200) & 0xffff) ;
208 #endif /* PCI */
209 is = GET_ISR() ;
210 /* Check if timer expired (or wraparound). */
211 if ((tr > smc->hw.t_start) || (is & IS_TIMINT)) {
212 hwt_restart(smc) ;
213 smc->hw.t_stop = smc->hw.t_start ;
215 else
216 smc->hw.t_stop = smc->hw.t_start - tr ;
218 return (smc->hw.t_stop) ;
221 #ifdef PCI
222 /************************
224 * hwt_quick_read
226 * Stop hardware timer and read timer value and start the timer again.
228 * u_long hwt_read(smc) ;
229 * In
230 * smc - A pointer to the SMT Context structure.
231 * Out
232 * current timer value in units of 80ns.
234 ************************/
235 u_long hwt_quick_read(struct s_smc *smc)
237 u_long interval ;
238 u_long time ;
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) ;
247 return(time) ;
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
261 * started
262 ************************/
263 void hwt_wait_time(struct s_smc *smc, u_long start, long int duration)
265 long diff ;
266 long interval ;
267 int wrapped ;
270 * check if timer is running
272 if (smc->hw.timer_activ == FALSE ||
273 hwt_quick_read(smc) == hwt_quick_read(smc)) {
274 return ;
277 interval = inpd(ADDR(B2_TI_INI)) ;
278 if (interval > duration) {
279 do {
280 diff = (long)(start - hwt_quick_read(smc)) ;
281 if (diff < 0) {
282 diff += interval ;
284 } while (diff <= duration) ;
286 else {
287 diff = interval ;
288 wrapped = 0 ;
289 do {
290 if (!wrapped) {
291 if (hwt_quick_read(smc) >= start) {
292 diff += interval ;
293 wrapped = 1 ;
296 else {
297 if (hwt_quick_read(smc) < start) {
298 wrapped = 0 ;
301 } while (diff <= duration) ;
304 #endif