hwt.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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. outpd(ADDR(B2_TI_INI), (u_long) cnt * 200) ; /* Load timer value. */
  71. outpw(ADDR(B2_TI_CRTL), TIM_START) ; /* Start timer. */
  72. smc->hw.timer_activ = TRUE ;
  73. }
  74. /************************
  75. *
  76. * hwt_stop
  77. *
  78. * Stop hardware timer.
  79. *
  80. * void hwt_stop(
  81. * struct s_smc *smc) ;
  82. * In
  83. * smc - A pointer to the SMT Context structure.
  84. * Out
  85. * Nothing.
  86. *
  87. ************************/
  88. void hwt_stop(struct s_smc *smc)
  89. {
  90. outpw(ADDR(B2_TI_CRTL), TIM_STOP) ;
  91. outpw(ADDR(B2_TI_CRTL), TIM_CL_IRQ) ;
  92. smc->hw.timer_activ = FALSE ;
  93. }
  94. /************************
  95. *
  96. * hwt_init
  97. *
  98. * Initialize hardware timer.
  99. *
  100. * void hwt_init(
  101. * struct s_smc *smc) ;
  102. * In
  103. * smc - A pointer to the SMT Context structure.
  104. * Out
  105. * Nothing.
  106. *
  107. ************************/
  108. void hwt_init(struct s_smc *smc)
  109. {
  110. smc->hw.t_start = 0 ;
  111. smc->hw.t_stop = 0 ;
  112. smc->hw.timer_activ = FALSE ;
  113. hwt_restart(smc) ;
  114. }
  115. /************************
  116. *
  117. * hwt_restart
  118. *
  119. * Clear timer interrupt.
  120. *
  121. * void hwt_restart(
  122. * struct s_smc *smc) ;
  123. * In
  124. * smc - A pointer to the SMT Context structure.
  125. * Out
  126. * Nothing.
  127. *
  128. ************************/
  129. void hwt_restart(struct s_smc *smc)
  130. {
  131. hwt_stop(smc) ;
  132. }
  133. /************************
  134. *
  135. * hwt_read
  136. *
  137. * Stop hardware timer and read time elapsed since last start.
  138. *
  139. * u_long hwt_read(smc) ;
  140. * In
  141. * smc - A pointer to the SMT Context structure.
  142. * Out
  143. * The elapsed time since last start in units of 16us.
  144. *
  145. ************************/
  146. u_long hwt_read(struct s_smc *smc)
  147. {
  148. u_short tr ;
  149. u_long is ;
  150. if (smc->hw.timer_activ) {
  151. hwt_stop(smc) ;
  152. tr = (u_short)((inpd(ADDR(B2_TI_VAL))/200) & 0xffff) ;
  153. is = GET_ISR() ;
  154. /* Check if timer expired (or wraparound). */
  155. if ((tr > smc->hw.t_start) || (is & IS_TIMINT)) {
  156. hwt_restart(smc) ;
  157. smc->hw.t_stop = smc->hw.t_start ;
  158. }
  159. else
  160. smc->hw.t_stop = smc->hw.t_start - tr ;
  161. }
  162. return smc->hw.t_stop;
  163. }
  164. #ifdef PCI
  165. /************************
  166. *
  167. * hwt_quick_read
  168. *
  169. * Stop hardware timer and read timer value and start the timer again.
  170. *
  171. * u_long hwt_read(smc) ;
  172. * In
  173. * smc - A pointer to the SMT Context structure.
  174. * Out
  175. * current timer value in units of 80ns.
  176. *
  177. ************************/
  178. u_long hwt_quick_read(struct s_smc *smc)
  179. {
  180. u_long interval ;
  181. u_long time ;
  182. interval = inpd(ADDR(B2_TI_INI)) ;
  183. outpw(ADDR(B2_TI_CRTL), TIM_STOP) ;
  184. time = inpd(ADDR(B2_TI_VAL)) ;
  185. outpd(ADDR(B2_TI_INI),time) ;
  186. outpw(ADDR(B2_TI_CRTL), TIM_START) ;
  187. outpd(ADDR(B2_TI_INI),interval) ;
  188. return time;
  189. }
  190. /************************
  191. *
  192. * hwt_wait_time(smc,start,duration)
  193. *
  194. * This function returnes after the amount of time is elapsed
  195. * since the start time.
  196. *
  197. * para start start time
  198. * duration time to wait
  199. *
  200. * NOTE: The function will return immediately, if the timer is not
  201. * started
  202. ************************/
  203. void hwt_wait_time(struct s_smc *smc, u_long start, long int duration)
  204. {
  205. long diff ;
  206. long interval ;
  207. int wrapped ;
  208. /*
  209. * check if timer is running
  210. */
  211. if (smc->hw.timer_activ == FALSE ||
  212. hwt_quick_read(smc) == hwt_quick_read(smc)) {
  213. return ;
  214. }
  215. interval = inpd(ADDR(B2_TI_INI)) ;
  216. if (interval > duration) {
  217. do {
  218. diff = (long)(start - hwt_quick_read(smc)) ;
  219. if (diff < 0) {
  220. diff += interval ;
  221. }
  222. } while (diff <= duration) ;
  223. }
  224. else {
  225. diff = interval ;
  226. wrapped = 0 ;
  227. do {
  228. if (!wrapped) {
  229. if (hwt_quick_read(smc) >= start) {
  230. diff += interval ;
  231. wrapped = 1 ;
  232. }
  233. }
  234. else {
  235. if (hwt_quick_read(smc) < start) {
  236. wrapped = 0 ;
  237. }
  238. }
  239. } while (diff <= duration) ;
  240. }
  241. }
  242. #endif