hwt.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. /******************************************************************************
  2. *
  3. * (C)Copyright 1998,1999 SysKonnect,
  4. * a business unit of Schneider & Koch & Co. Datensysteme GmbH.
  5. *
  6. * See the file "skfddi.c" for further information.
  7. *
  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.
  12. *
  13. * The information in this file is provided "AS IS" without warranty.
  14. *
  15. ******************************************************************************/
  16. /*
  17. * Timer Driver for FBI board (timer chip 82C54)
  18. */
  19. /*
  20. * Modifications:
  21. *
  22. * 28-Jun-1994 sw Edit v1.6.
  23. * MCA: Added support for the SK-NET FDDI-FM2 adapter. The
  24. * following functions have been added(+) or modified(*):
  25. * hwt_start(*), hwt_stop(*), hwt_restart(*), hwt_read(*)
  26. */
  27. #include "h/types.h"
  28. #include "h/fddi.h"
  29. #include "h/smc.h"
  30. #ifndef lint
  31. static const char ID_sccs[] = "@(#)hwt.c 1.13 97/04/23 (C) SK " ;
  32. #endif
  33. /*
  34. * Prototypes of local functions.
  35. */
  36. /* 28-Jun-1994 sw - Note: hwt_restart() is also used in module 'drvfbi.c'. */
  37. /*static void hwt_restart() ; */
  38. /************************
  39. *
  40. * hwt_start
  41. *
  42. * Start hardware timer (clock ticks are 16us).
  43. *
  44. * void hwt_start(
  45. * struct s_smc *smc,
  46. * u_long time) ;
  47. * In
  48. * smc - A pointer to the SMT Context structure.
  49. *
  50. * time - The time in units of 16us to load the timer with.
  51. * Out
  52. * Nothing.
  53. *
  54. ************************/
  55. #define HWT_MAX (65000)
  56. void hwt_start(struct s_smc *smc, u_long time)
  57. {
  58. u_short cnt ;
  59. if (time > HWT_MAX)
  60. time = HWT_MAX ;
  61. smc->hw.t_start = time ;
  62. smc->hw.t_stop = 0L ;
  63. cnt = (u_short)time ;
  64. /*
  65. * if time < 16 us
  66. * time = 16 us
  67. */
  68. if (!cnt)
  69. cnt++ ;
  70. #ifndef PCI
  71. /*
  72. * 6.25MHz -> CLK0 : T0 (cnt0 = 16us) -> OUT0
  73. * OUT0 -> CLK1 : T1 (cnt1) OUT1 -> ISRA(IS_TIMINT)
  74. */
  75. OUT_82c54_TIMER(3,1<<6 | 3<<4 | 0<<1) ; /* counter 1, mode 0 */
  76. OUT_82c54_TIMER(1,cnt & 0xff) ; /* LSB */
  77. OUT_82c54_TIMER(1,(cnt>>8) & 0xff) ; /* MSB */
  78. /*
  79. * start timer by switching counter 0 to mode 3
  80. * T0 resolution 16 us (CLK0=0.16us)
  81. */
  82. OUT_82c54_TIMER(3,0<<6 | 3<<4 | 3<<1) ; /* counter 0, mode 3 */
  83. OUT_82c54_TIMER(0,100) ; /* LSB */
  84. OUT_82c54_TIMER(0,0) ; /* MSB */
  85. #else /* PCI */
  86. outpd(ADDR(B2_TI_INI), (u_long) cnt * 200) ; /* Load timer value. */
  87. outpw(ADDR(B2_TI_CRTL), TIM_START) ; /* Start timer. */
  88. #endif /* PCI */
  89. smc->hw.timer_activ = TRUE ;
  90. }
  91. /************************
  92. *
  93. * hwt_stop
  94. *
  95. * Stop hardware timer.
  96. *
  97. * void hwt_stop(
  98. * struct s_smc *smc) ;
  99. * In
  100. * smc - A pointer to the SMT Context structure.
  101. * Out
  102. * Nothing.
  103. *
  104. ************************/
  105. void hwt_stop(struct s_smc *smc)
  106. {
  107. #ifndef PCI
  108. /* stop counter 0 by switching to mode 0 */
  109. OUT_82c54_TIMER(3,0<<6 | 3<<4 | 0<<1) ; /* counter 0, mode 0 */
  110. OUT_82c54_TIMER(0,0) ; /* LSB */
  111. OUT_82c54_TIMER(0,0) ; /* MSB */
  112. #else /* PCI */
  113. outpw(ADDR(B2_TI_CRTL), TIM_STOP) ;
  114. outpw(ADDR(B2_TI_CRTL), TIM_CL_IRQ) ;
  115. #endif /* PCI */
  116. smc->hw.timer_activ = FALSE ;
  117. }
  118. /************************
  119. *
  120. * hwt_init
  121. *
  122. * Initialize hardware timer.
  123. *
  124. * void hwt_init(
  125. * struct s_smc *smc) ;
  126. * In
  127. * smc - A pointer to the SMT Context structure.
  128. * Out
  129. * Nothing.
  130. *
  131. ************************/
  132. void hwt_init(struct s_smc *smc)
  133. {
  134. smc->hw.t_start = 0 ;
  135. smc->hw.t_stop = 0 ;
  136. smc->hw.timer_activ = FALSE ;
  137. hwt_restart(smc) ;
  138. }
  139. /************************
  140. *
  141. * hwt_restart
  142. *
  143. * Clear timer interrupt.
  144. *
  145. * void hwt_restart(
  146. * struct s_smc *smc) ;
  147. * In
  148. * smc - A pointer to the SMT Context structure.
  149. * Out
  150. * Nothing.
  151. *
  152. ************************/
  153. void hwt_restart(struct s_smc *smc)
  154. {
  155. hwt_stop(smc) ;
  156. #ifndef PCI
  157. OUT_82c54_TIMER(3,1<<6 | 3<<4 | 0<<1) ; /* counter 1, mode 0 */
  158. OUT_82c54_TIMER(1,1 ) ; /* LSB */
  159. OUT_82c54_TIMER(1,0 ) ; /* MSB */
  160. #endif
  161. }
  162. /************************
  163. *
  164. * hwt_read
  165. *
  166. * Stop hardware timer and read time elapsed since last start.
  167. *
  168. * u_long hwt_read(smc) ;
  169. * In
  170. * smc - A pointer to the SMT Context structure.
  171. * Out
  172. * The elapsed time since last start in units of 16us.
  173. *
  174. ************************/
  175. u_long hwt_read(struct s_smc *smc)
  176. {
  177. u_short tr ;
  178. #ifndef PCI
  179. u_short is ;
  180. #else
  181. u_long is ;
  182. #endif
  183. if (smc->hw.timer_activ) {
  184. hwt_stop(smc) ;
  185. #ifndef PCI
  186. OUT_82c54_TIMER(3,1<<6) ; /* latch command */
  187. tr = IN_82c54_TIMER(1) & 0xff ;
  188. tr += (IN_82c54_TIMER(1) & 0xff)<<8 ;
  189. #else /* PCI */
  190. tr = (u_short)((inpd(ADDR(B2_TI_VAL))/200) & 0xffff) ;
  191. #endif /* PCI */
  192. is = GET_ISR() ;
  193. /* Check if timer expired (or wraparound). */
  194. if ((tr > smc->hw.t_start) || (is & IS_TIMINT)) {
  195. hwt_restart(smc) ;
  196. smc->hw.t_stop = smc->hw.t_start ;
  197. }
  198. else
  199. smc->hw.t_stop = smc->hw.t_start - tr ;
  200. }
  201. return (smc->hw.t_stop) ;
  202. }
  203. #ifdef PCI
  204. /************************
  205. *
  206. * hwt_quick_read
  207. *
  208. * Stop hardware timer and read timer value and start the timer again.
  209. *
  210. * u_long hwt_read(smc) ;
  211. * In
  212. * smc - A pointer to the SMT Context structure.
  213. * Out
  214. * current timer value in units of 80ns.
  215. *
  216. ************************/
  217. u_long hwt_quick_read(struct s_smc *smc)
  218. {
  219. u_long interval ;
  220. u_long time ;
  221. interval = inpd(ADDR(B2_TI_INI)) ;
  222. outpw(ADDR(B2_TI_CRTL), TIM_STOP) ;
  223. time = inpd(ADDR(B2_TI_VAL)) ;
  224. outpd(ADDR(B2_TI_INI),time) ;
  225. outpw(ADDR(B2_TI_CRTL), TIM_START) ;
  226. outpd(ADDR(B2_TI_INI),interval) ;
  227. return(time) ;
  228. }
  229. /************************
  230. *
  231. * hwt_wait_time(smc,start,duration)
  232. *
  233. * This function returnes after the amount of time is elapsed
  234. * since the start time.
  235. *
  236. * para start start time
  237. * duration time to wait
  238. *
  239. * NOTE: The fuction will return immediately, if the timer is not
  240. * started
  241. ************************/
  242. void hwt_wait_time(struct s_smc *smc, u_long start, long int duration)
  243. {
  244. long diff ;
  245. long interval ;
  246. int wrapped ;
  247. /*
  248. * check if timer is running
  249. */
  250. if (smc->hw.timer_activ == FALSE ||
  251. hwt_quick_read(smc) == hwt_quick_read(smc)) {
  252. return ;
  253. }
  254. interval = inpd(ADDR(B2_TI_INI)) ;
  255. if (interval > duration) {
  256. do {
  257. diff = (long)(start - hwt_quick_read(smc)) ;
  258. if (diff < 0) {
  259. diff += interval ;
  260. }
  261. } while (diff <= duration) ;
  262. }
  263. else {
  264. diff = interval ;
  265. wrapped = 0 ;
  266. do {
  267. if (!wrapped) {
  268. if (hwt_quick_read(smc) >= start) {
  269. diff += interval ;
  270. wrapped = 1 ;
  271. }
  272. }
  273. else {
  274. if (hwt_quick_read(smc) < start) {
  275. wrapped = 0 ;
  276. }
  277. }
  278. } while (diff <= duration) ;
  279. }
  280. }
  281. #endif