Moved conditional compilation to Makefile for SK98 Ethernet driver
[oweals/u-boot.git] / drivers / net / sk98lin / sklm80.c
1 /******************************************************************************
2  *
3  * Name:        sklm80.c
4  * Project:     GEnesis, PCI Gigabit Ethernet Adapter
5  * Version:     $Revision: 1.20 $
6  * Date:        $Date: 2002/08/13 09:16:27 $
7  * Purpose:     Funktions to access Voltage and Temperature Sensor (LM80)
8  *
9  ******************************************************************************/
10
11 /******************************************************************************
12  *
13  *      (C)Copyright 1998-2002 SysKonnect GmbH.
14  *
15  *      This program is free software; you can redistribute it and/or modify
16  *      it under the terms of the GNU General Public License as published by
17  *      the Free Software Foundation; either version 2 of the License, or
18  *      (at your option) any later version.
19  *
20  *      The information in this file is provided "AS IS" without warranty.
21  *
22  ******************************************************************************/
23
24 /******************************************************************************
25  *
26  * History:
27  *
28  *      $Log: sklm80.c,v $
29  *      Revision 1.20  2002/08/13 09:16:27  rschmidt
30  *      Changed return value for SkLm80ReadSensor() back to 'int'
31  *      Editorial changes
32  *
33  *      Revision 1.19  2002/08/06 09:43:31  jschmalz
34  *      Extensions and changes for Yukon
35  *
36  *      Revision 1.18  2002/08/02 12:26:57  rschmidt
37  *      Editorial changes
38  *
39  *      Revision 1.17  1999/11/22 13:35:51  cgoos
40  *      Changed license header to GPL.
41  *
42  *      Revision 1.16  1999/05/27 14:05:47  malthoff
43  *      Fans: Set SenVal to 0 if the fan value is 0 or 0xff. Both values
44  *      are outside the limits (0: div zero error, 0xff: value not in
45  *      range, assume 0).
46  *
47  *      Revision 1.15  1999/05/27 13:38:51  malthoff
48  *      Pervent from Division by zero errors.
49  *
50  *      Revision 1.14  1999/05/20 09:20:01  cgoos
51  *      Changes for 1000Base-T (Fan sensors).
52  *
53  *      Revision 1.13  1998/10/22 09:48:14  gklug
54  *      fix: SysKonnectFileId typo
55  *
56  *      Revision 1.12  1998/10/09 06:12:06  malthoff
57  *      Remove ID_sccs by SysKonnectFileId.
58  *
59  *      Revision 1.11  1998/09/04 08:33:48  malthoff
60  *      bug fix: SenState = SK_SEN_IDLE when
61  *      leaving SK_SEN_VALEXT state
62  *
63  *      Revision 1.10  1998/08/20 12:02:10  gklug
64  *      fix: compiler warnings type mismatch
65  *
66  *      Revision 1.9  1998/08/20 11:37:38  gklug
67  *      chg: change Ioc to IoC
68  *
69  *      Revision 1.8  1998/08/19 12:20:58  gklug
70  *      fix: remove struct from C files (see CCC)
71  *
72  *      Revision 1.7  1998/08/17 07:04:57  malthoff
73  *      Take SkLm80RcvReg() function from ski2c.c.
74  *      Add IoC parameter to BREAK_OR_WAIT() macro.
75  *
76  *      Revision 1.6  1998/08/14 07:11:28  malthoff
77  *      remove pAc with pAC.
78  *
79  *      Revision 1.5  1998/08/14 06:46:55  gklug
80  *      fix: temperature can get negative
81  *
82  *      Revision 1.4  1998/08/13 08:27:04  gklug
83  *      add: temperature reading now o.k.
84  *      fix: pSen declaration, SK_ERR_LOG call, ADDR macro
85  *
86  *      Revision 1.3  1998/08/13 07:28:21  gklug
87  *      fix: pSen was wrong initialized
88  *      add: correct conversion for voltage readings
89  *
90  *      Revision 1.2  1998/08/11 07:52:14  gklug
91  *      add: Lm80 read sensor function
92  *
93  *      Revision 1.1  1998/07/17 09:57:12  gklug
94  *      initial version
95  *
96  *
97  *
98  ******************************************************************************/
99
100
101 #include <config.h>
102
103 /*
104         LM80 functions
105 */
106 static const char SysKonnectFileId[] =
107         "$Id: sklm80.c,v 1.20 2002/08/13 09:16:27 rschmidt Exp $" ;
108
109 #include "h/skdrv1st.h"         /* Driver Specific Definitions */
110 #include "h/lm80.h"
111 #include "h/skdrv2nd.h"         /* Adapter Control- and Driver specific Def. */
112
113 #ifdef  SK_DIAG
114 #define BREAK_OR_WAIT(pAC,IoC,Event)    SkI2cWait(pAC,IoC,Event)
115 #else   /* nSK_DIAG */
116 #define BREAK_OR_WAIT(pAC,IoC,Event)    break
117 #endif  /* nSK_DIAG */
118
119 #ifdef  SK_DIAG
120 /*
121  * read the register 'Reg' from the device 'Dev'
122  *
123  * return       read error      -1
124  *              success         the read value
125  */
126 int     SkLm80RcvReg(
127 SK_IOC  IoC,            /* Adapter Context */
128 int             Dev,            /* I2C device address */
129 int             Reg)            /* register to read */
130 {
131         int     Val = 0;
132         int     TempExt;
133
134         /* Signal device number */
135         if (SkI2cSndDev(IoC, Dev, I2C_WRITE)) {
136                 return(-1);
137         }
138
139         if (SkI2cSndByte(IoC, Reg)) {
140                 return(-1);
141         }
142
143         /* repeat start */
144         if (SkI2cSndDev(IoC, Dev, I2C_READ)) {
145                 return(-1);
146         }
147
148         switch (Reg) {
149         case LM80_TEMP_IN:
150                 Val = (int)SkI2cRcvByte(IoC, 1);
151
152                 /* First: correct the value: it might be negative */
153                 if ((Val & 0x80) != 0) {
154                         /* Value is negative */
155                         Val = Val - 256;
156                 }
157                 Val = Val * SK_LM80_TEMP_LSB;
158                 SkI2cStop(IoC);
159
160                 TempExt = (int)SkLm80RcvReg(IoC, LM80_ADDR, LM80_TEMP_CTRL);
161
162                 if (Val > 0) {
163                         Val += ((TempExt >> 7) * SK_LM80_TEMPEXT_LSB);
164                 }
165                 else {
166                         Val -= ((TempExt >> 7) * SK_LM80_TEMPEXT_LSB);
167                 }
168                 return(Val);
169                 break;
170         case LM80_VT0_IN:
171         case LM80_VT1_IN:
172         case LM80_VT2_IN:
173         case LM80_VT3_IN:
174                 Val = (int)SkI2cRcvByte(IoC, 1) * SK_LM80_VT_LSB;
175                 break;
176
177         default:
178                 Val = (int)SkI2cRcvByte(IoC, 1);
179                 break;
180         }
181
182         SkI2cStop(IoC);
183         return(Val);
184 }
185 #endif  /* SK_DIAG */
186
187 /*
188  * read a sensors value (LM80 specific)
189  *
190  * This function reads a sensors value from the I2C sensor chip LM80.
191  * The sensor is defined by its index into the sensors database in the struct
192  * pAC points to.
193  *
194  * Returns      1 if the read is completed
195  *              0 if the read must be continued (I2C Bus still allocated)
196  */
197 int SkLm80ReadSensor(
198 SK_AC           *pAC,   /* Adapter Context */
199 SK_IOC          IoC,    /* I/O Context needed in level 1 and 2 */
200 SK_SENSOR       *pSen)  /* Sensor to be read */
201 {
202         SK_I32          Value;
203
204         switch (pSen->SenState) {
205         case SK_SEN_IDLE:
206                 /* Send address to ADDR register */
207                 SK_I2C_CTL(IoC, I2C_READ, pSen->SenDev, pSen->SenReg, 0);
208
209                 pSen->SenState = SK_SEN_VALUE ;
210                 BREAK_OR_WAIT(pAC, IoC, I2C_READ);
211
212         case SK_SEN_VALUE:
213                 /* Read value from data register */
214                 SK_IN32(IoC, B2_I2C_DATA, ((SK_U32 *)&Value));
215
216                 Value &= 0xff; /* only least significant byte is valid */
217
218                 /* Do NOT check the Value against the thresholds */
219                 /* Checking is done in the calling instance */
220
221                 if (pSen->SenType == SK_SEN_VOLT) {
222                         /* Voltage sensor */
223                         pSen->SenValue = Value * SK_LM80_VT_LSB;
224                         pSen->SenState = SK_SEN_IDLE ;
225                         return(1);
226                 }
227
228                 if (pSen->SenType == SK_SEN_FAN) {
229                         if (Value != 0 && Value != 0xff) {
230                                 /* Fan speed counter */
231                                 pSen->SenValue = SK_LM80_FAN_FAKTOR/Value;
232                         }
233                         else {
234                                 /* Indicate Fan error */
235                                 pSen->SenValue = 0;
236                         }
237                         pSen->SenState = SK_SEN_IDLE ;
238                         return(1);
239                 }
240
241                 /* First: correct the value: it might be negative */
242                 if ((Value & 0x80) != 0) {
243                         /* Value is negative */
244                         Value = Value - 256;
245                 }
246
247                 /* We have a temperature sensor and need to get the signed extension.
248                  * For now we get the extension from the last reading, so in the normal
249                  * case we won't see flickering temperatures.
250                  */
251                 pSen->SenValue = (Value * SK_LM80_TEMP_LSB) +
252                         (pSen->SenValue % SK_LM80_TEMP_LSB);
253
254                 /* Send address to ADDR register */
255                 SK_I2C_CTL(IoC, I2C_READ, pSen->SenDev, LM80_TEMP_CTRL, 0);
256
257                 pSen->SenState = SK_SEN_VALEXT ;
258                 BREAK_OR_WAIT(pAC, IoC, I2C_READ);
259
260         case SK_SEN_VALEXT:
261                 /* Read value from data register */
262                 SK_IN32(IoC, B2_I2C_DATA, ((SK_U32 *)&Value));
263                 Value &= LM80_TEMP_LSB_9; /* only bit 7 is valid */
264
265                 /* cut the LSB bit */
266                 pSen->SenValue = ((pSen->SenValue / SK_LM80_TEMP_LSB) *
267                         SK_LM80_TEMP_LSB);
268
269                 if (pSen->SenValue < 0) {
270                         /* Value negative: The bit value must be subtracted */
271                         pSen->SenValue -= ((Value >> 7) * SK_LM80_TEMPEXT_LSB);
272                 }
273                 else {
274                         /* Value positive: The bit value must be added */
275                         pSen->SenValue += ((Value >> 7) * SK_LM80_TEMPEXT_LSB);
276                 }
277
278                 pSen->SenState = SK_SEN_IDLE ;
279                 return(1);
280
281         default:
282                 SK_ERR_LOG(pAC, SK_ERRCL_SW, SKERR_I2C_E007, SKERR_I2C_E007MSG);
283                 return(1);
284         }
285
286         /* Not completed */
287         return(0);
288 }