^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /******************************************************************************
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * (C)Copyright 1998,1999 SysKonnect,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * a business unit of Schneider & Koch & Co. Datensysteme GmbH.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * See the file "skfddi.c" for further information.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * The information in this file is provided "AS IS" without warranty.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) ******************************************************************************/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) SMT timer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include "h/types.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include "h/fddi.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include "h/smc.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #ifndef lint
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) static const char ID_sccs[] = "@(#)smttimer.c 2.4 97/08/04 (C) SK " ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) static void timer_done(struct s_smc *smc, int restart);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) void smt_timer_init(struct s_smc *smc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) smc->t.st_queue = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) smc->t.st_fast.tm_active = FALSE ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) smc->t.st_fast.tm_next = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) hwt_init(smc) ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) void smt_timer_stop(struct s_smc *smc, struct smt_timer *timer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) struct smt_timer **prev ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) struct smt_timer *tm ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) * remove timer from queue
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) timer->tm_active = FALSE ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) if (smc->t.st_queue == timer && !timer->tm_next) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) hwt_stop(smc) ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) for (prev = &smc->t.st_queue ; (tm = *prev) ; prev = &tm->tm_next ) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) if (tm == timer) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) *prev = tm->tm_next ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) if (tm->tm_next) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) tm->tm_next->tm_delta += tm->tm_delta ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) return ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) void smt_timer_start(struct s_smc *smc, struct smt_timer *timer, u_long time,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) u_long token)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) struct smt_timer **prev ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) struct smt_timer *tm ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) u_long delta = 0 ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) time /= 16 ; /* input is uS, clock ticks are 16uS */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) if (!time)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) time = 1 ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) smt_timer_stop(smc,timer) ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) timer->tm_smc = smc ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) timer->tm_token = token ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) timer->tm_active = TRUE ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) if (!smc->t.st_queue) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) smc->t.st_queue = timer ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) timer->tm_next = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) timer->tm_delta = time ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) hwt_start(smc,time) ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) return ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) * timer correction
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) timer_done(smc,0) ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) * find position in queue
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) delta = 0 ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) for (prev = &smc->t.st_queue ; (tm = *prev) ; prev = &tm->tm_next ) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) if (delta + tm->tm_delta > time) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) break ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) delta += tm->tm_delta ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) /* insert in queue */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) *prev = timer ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) timer->tm_next = tm ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) timer->tm_delta = time - delta ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) if (tm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) tm->tm_delta -= timer->tm_delta ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) * start new with first
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) hwt_start(smc,smc->t.st_queue->tm_delta) ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) void smt_force_irq(struct s_smc *smc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) smt_timer_start(smc,&smc->t.st_fast,32L, EV_TOKEN(EVENT_SMT,SM_FAST));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) void smt_timer_done(struct s_smc *smc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) timer_done(smc,1) ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) static void timer_done(struct s_smc *smc, int restart)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) u_long delta ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) struct smt_timer *tm ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) struct smt_timer *next ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) struct smt_timer **last ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) int done = 0 ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) delta = hwt_read(smc) ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) last = &smc->t.st_queue ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) tm = smc->t.st_queue ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) while (tm && !done) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) if (delta >= tm->tm_delta) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) tm->tm_active = FALSE ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) delta -= tm->tm_delta ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) last = &tm->tm_next ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) tm = tm->tm_next ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) tm->tm_delta -= delta ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) delta = 0 ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) done = 1 ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) *last = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) next = smc->t.st_queue ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) smc->t.st_queue = tm ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) for ( tm = next ; tm ; tm = next) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) next = tm->tm_next ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) timer_event(smc,tm->tm_token) ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) if (restart && smc->t.st_queue)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) hwt_start(smc,smc->t.st_queue->tm_delta) ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152)