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