25489c41928ec46377cb772ea2753089ecd5b7fb
[linux-flexiantxendom0-3.2.10.git] / drivers / net / sk98lin / skgehwt.c
1 /******************************************************************************
2  *
3  * Name:        skgehwt.c
4  * Project:     Gigabit Ethernet Adapters, Event Scheduler Module
5  * Purpose:     Hardware Timer
6  *
7  ******************************************************************************/
8
9 /******************************************************************************
10  *
11  *      (C)Copyright 1998-2002 SysKonnect GmbH.
12  *      (C)Copyright 2002-2003 Marvell.
13  *
14  *      This program is free software; you can redistribute it and/or modify
15  *      it under the terms of the GNU General Public License as published by
16  *      the Free Software Foundation; either version 2 of the License, or
17  *      (at your option) any later version.
18  *
19  *      The information in this file is provided "AS IS" without warranty.
20  *
21  ******************************************************************************/
22
23 /*
24  *      Event queue and dispatcher
25  */
26 #if (defined(DEBUG) || ((!defined(LINT)) && (!defined(SK_SLIM))))
27 static const char SysKonnectFileId[] =
28         "@(#) $Id: skgehwt.c,v 1.15 2003/09/16 13:41:23 rschmidt Exp $ (C) Marvell.";
29 #endif
30
31 #include "h/skdrv1st.h"         /* Driver Specific Definitions */
32 #include "h/skdrv2nd.h"         /* Adapter Control- and Driver specific Def. */
33
34 #ifdef __C2MAN__
35 /*
36  *   Hardware Timer function queue management.
37  */
38 intro()
39 {}
40 #endif
41
42 /*
43  * Prototypes of local functions.
44  */
45 #define SK_HWT_MAX      (65000)
46
47 /* correction factor */
48 #define SK_HWT_FAC      (1000 * (SK_U32)pAC->GIni.GIHstClkFact / 100)
49
50 /*
51  * Initialize hardware timer.
52  *
53  * Must be called during init level 1.
54  */
55 void    SkHwtInit(
56 SK_AC   *pAC,   /* Adapters context */
57 SK_IOC  Ioc)    /* IoContext */
58 {
59         pAC->Hwt.TStart = 0 ;
60         pAC->Hwt.TStop  = 0 ;
61         pAC->Hwt.TActive = SK_FALSE;
62
63         SkHwtStop(pAC, Ioc);
64 }
65
66 /*
67  *
68  * Start hardware timer (clock ticks are 16us).
69  *
70  */
71 void    SkHwtStart(
72 SK_AC   *pAC,   /* Adapters context */
73 SK_IOC  Ioc,    /* IoContext */
74 SK_U32  Time)   /* Time in units of 16us to load the timer with. */
75 {
76         SK_U32  Cnt;
77
78         if (Time > SK_HWT_MAX)
79                 Time = SK_HWT_MAX;
80
81         pAC->Hwt.TStart = Time;
82         pAC->Hwt.TStop = 0L;
83
84         Cnt = Time;
85
86         /*
87          * if time < 16 us
88          *      time = 16 us
89          */
90         if (!Cnt) {
91                 Cnt++;
92         }
93
94         SK_OUT32(Ioc, B2_TI_INI, Cnt * SK_HWT_FAC);
95         
96         SK_OUT16(Ioc, B2_TI_CTRL, TIM_START);   /* Start timer. */
97
98         pAC->Hwt.TActive = SK_TRUE;
99 }
100
101 /*
102  * Stop hardware timer.
103  * and clear the timer IRQ
104  */
105 void    SkHwtStop(
106 SK_AC   *pAC,   /* Adapters context */
107 SK_IOC  Ioc)    /* IoContext */
108 {
109         SK_OUT16(Ioc, B2_TI_CTRL, TIM_STOP);
110         
111         SK_OUT16(Ioc, B2_TI_CTRL, TIM_CLR_IRQ);
112
113         pAC->Hwt.TActive = SK_FALSE;
114 }
115
116
117 /*
118  *      Stop hardware timer and read time elapsed since last start.
119  *
120  * returns
121  *      The elapsed time since last start in units of 16us.
122  *
123  */
124 SK_U32  SkHwtRead(
125 SK_AC   *pAC,   /* Adapters context */
126 SK_IOC  Ioc)    /* IoContext */
127 {
128         SK_U32  TRead;
129         SK_U32  IStatus;
130
131         if (pAC->Hwt.TActive) {
132                 
133                 SkHwtStop(pAC, Ioc);
134
135                 SK_IN32(Ioc, B2_TI_VAL, &TRead);
136                 TRead /= SK_HWT_FAC;
137
138                 SK_IN32(Ioc, B0_ISRC, &IStatus);
139
140                 /* Check if timer expired (or wraped around) */
141                 if ((TRead > pAC->Hwt.TStart) || (IStatus & IS_TIMINT)) {
142                         
143                         SkHwtStop(pAC, Ioc);
144                         
145                         pAC->Hwt.TStop = pAC->Hwt.TStart;
146                 }
147                 else {
148                         
149                         pAC->Hwt.TStop = pAC->Hwt.TStart - TRead;
150                 }
151         }
152         return(pAC->Hwt.TStop);
153 }
154
155 /*
156  * interrupt source= timer
157  */
158 void    SkHwtIsr(
159 SK_AC   *pAC,   /* Adapters context */
160 SK_IOC  Ioc)    /* IoContext */
161 {
162         SkHwtStop(pAC, Ioc);
163         
164         pAC->Hwt.TStop = pAC->Hwt.TStart;
165         
166         SkTimerDone(pAC, Ioc);
167 }
168
169 /* End of file */