moel@245
|
1 |
/*
|
moel@245
|
2 |
|
moel@344
|
3 |
This Source Code Form is subject to the terms of the Mozilla Public
|
moel@344
|
4 |
License, v. 2.0. If a copy of the MPL was not distributed with this
|
moel@344
|
5 |
file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
moel@245
|
6 |
|
moel@344
|
7 |
Copyright (C) 2010-2011 Michael Möller <mmoeller@openhardwaremonitor.org>
|
moel@344
|
8 |
|
moel@245
|
9 |
*/
|
moel@245
|
10 |
|
moel@245
|
11 |
using System.Globalization;
|
moel@245
|
12 |
using System.Text;
|
moel@245
|
13 |
|
moel@245
|
14 |
namespace OpenHardwareMonitor.Hardware.LPC {
|
moel@245
|
15 |
internal class NCT677X : ISuperIO {
|
moel@245
|
16 |
|
moel@245
|
17 |
private readonly ushort port;
|
moel@245
|
18 |
private readonly byte revision;
|
moel@245
|
19 |
|
moel@245
|
20 |
private readonly Chip chip;
|
moel@245
|
21 |
|
moel@245
|
22 |
private readonly float?[] voltages = new float?[9];
|
moel@276
|
23 |
private readonly float?[] temperatures = new float?[4];
|
moel@265
|
24 |
private readonly float?[] fans = new float?[0];
|
moel@323
|
25 |
private readonly float?[] controls = new float?[3];
|
moel@245
|
26 |
|
moel@245
|
27 |
// Hardware Monitor
|
moel@245
|
28 |
private const uint ADDRESS_REGISTER_OFFSET = 0x05;
|
moel@245
|
29 |
private const uint DATA_REGISTER_OFFSET = 0x06;
|
moel@245
|
30 |
private const byte BANK_SELECT_REGISTER = 0x4E;
|
moel@245
|
31 |
|
moel@245
|
32 |
private byte ReadByte(ushort address) {
|
moel@245
|
33 |
byte bank = (byte)(address >> 8);
|
moel@245
|
34 |
byte register = (byte)(address & 0xFF);
|
moel@245
|
35 |
Ring0.WriteIoPort(port + ADDRESS_REGISTER_OFFSET, BANK_SELECT_REGISTER);
|
moel@245
|
36 |
Ring0.WriteIoPort(port + DATA_REGISTER_OFFSET, bank);
|
moel@245
|
37 |
Ring0.WriteIoPort(port + ADDRESS_REGISTER_OFFSET, register);
|
moel@245
|
38 |
return Ring0.ReadIoPort(port + DATA_REGISTER_OFFSET);
|
moel@323
|
39 |
}
|
moel@323
|
40 |
|
moel@323
|
41 |
private void WriteByte(ushort address, byte value) {
|
moel@323
|
42 |
byte bank = (byte)(address >> 8);
|
moel@323
|
43 |
byte register = (byte)(address & 0xFF);
|
moel@323
|
44 |
Ring0.WriteIoPort(port + ADDRESS_REGISTER_OFFSET, BANK_SELECT_REGISTER);
|
moel@323
|
45 |
Ring0.WriteIoPort(port + DATA_REGISTER_OFFSET, bank);
|
moel@323
|
46 |
Ring0.WriteIoPort(port + ADDRESS_REGISTER_OFFSET, register);
|
moel@323
|
47 |
Ring0.WriteIoPort(port + DATA_REGISTER_OFFSET, value);
|
moel@246
|
48 |
}
|
moel@245
|
49 |
|
moel@245
|
50 |
// Consts
|
moel@245
|
51 |
private const ushort NUVOTON_VENDOR_ID = 0x5CA3;
|
moel@245
|
52 |
|
moel@245
|
53 |
// Hardware Monitor Registers
|
moel@245
|
54 |
private const ushort VENDOR_ID_HIGH_REGISTER = 0x804F;
|
moel@245
|
55 |
private const ushort VENDOR_ID_LOW_REGISTER = 0x004F;
|
moel@245
|
56 |
private const ushort VOLTAGE_VBAT_REG = 0x0551;
|
moel@245
|
57 |
|
moel@245
|
58 |
private readonly ushort[] TEMPERATURE_REG =
|
moel@276
|
59 |
{ 0x027, 0x73, 0x75, 0x77, 0x150, 0x250, 0x62B, 0x62C, 0x62D };
|
moel@245
|
60 |
private readonly ushort[] TEMPERATURE_HALF_REG =
|
moel@276
|
61 |
{ 0, 0x74, 0x76, 0x78, 0x151, 0x251, 0x62E, 0x62E, 0x62E };
|
moel@245
|
62 |
private readonly ushort[] TEMPERATURE_SRC_REG =
|
moel@276
|
63 |
{ 0x621, 0x100, 0x200, 0x300, 0x622, 0x623, 0x624, 0x625, 0x626 };
|
moel@276
|
64 |
private readonly int[] TEMPERATURE_HALF_BIT =
|
moel@276
|
65 |
{ -1, 7, 7, 7, 7, 7, 0, 1, 2 };
|
moel@245
|
66 |
private readonly ushort[] VOLTAGE_REG =
|
moel@245
|
67 |
{ 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x550, 0x551 };
|
moel@265
|
68 |
private readonly ushort[] FAN_RPM_REG =
|
moel@265
|
69 |
{ 0x656, 0x658, 0x65A, 0x65C, 0x65E};
|
moel@323
|
70 |
private readonly ushort[] FAN_PWM_OUT_REG =
|
moel@323
|
71 |
{ 0x001, 0x003, 0x011 };
|
moel@323
|
72 |
private readonly ushort[] FAN_PWM_COMMAND_REG =
|
moel@323
|
73 |
{ 0x109, 0x209, 0x309 };
|
moel@323
|
74 |
private readonly ushort[] FAN_CONTROL_MODE_REG =
|
moel@323
|
75 |
{ 0x102, 0x202, 0x302 };
|
moel@265
|
76 |
|
moel@265
|
77 |
private readonly int minFanRPM;
|
moel@245
|
78 |
|
moel@323
|
79 |
private bool[] restoreDefaultFanControlRequired = { false, false, false };
|
moel@323
|
80 |
private byte[] initialFanControlMode = new byte[3];
|
moel@323
|
81 |
private byte[] initialFanPwmCommand = new byte[3];
|
moel@323
|
82 |
|
moel@276
|
83 |
private enum SourceNCT6771F : byte {
|
moel@245
|
84 |
SYSTIN = 1,
|
moel@245
|
85 |
CPUTIN = 2,
|
moel@245
|
86 |
AUXTIN = 3,
|
moel@245
|
87 |
SMBUSMASTER = 4,
|
moel@276
|
88 |
PECI_0 = 5,
|
moel@276
|
89 |
PECI_1 = 6,
|
moel@276
|
90 |
PECI_2 = 7,
|
moel@276
|
91 |
PECI_3 = 8,
|
moel@276
|
92 |
PECI_4 = 9,
|
moel@276
|
93 |
PECI_5 = 10,
|
moel@276
|
94 |
PECI_6 = 11,
|
moel@276
|
95 |
PECI_7 = 12,
|
moel@245
|
96 |
PCH_CHIP_CPU_MAX_TEMP = 13,
|
moel@245
|
97 |
PCH_CHIP_TEMP = 14,
|
moel@245
|
98 |
PCH_CPU_TEMP = 15,
|
moel@245
|
99 |
PCH_MCH_TEMP = 16,
|
moel@245
|
100 |
PCH_DIM0_TEMP = 17,
|
moel@245
|
101 |
PCH_DIM1_TEMP = 18,
|
moel@245
|
102 |
PCH_DIM2_TEMP = 19,
|
moel@245
|
103 |
PCH_DIM3_TEMP = 20
|
moel@245
|
104 |
}
|
moel@245
|
105 |
|
moel@276
|
106 |
private enum SourceNCT6776F : byte {
|
moel@276
|
107 |
SYSTIN = 1,
|
moel@276
|
108 |
CPUTIN = 2,
|
moel@276
|
109 |
AUXTIN = 3,
|
moel@276
|
110 |
SMBUSMASTER_0 = 4,
|
moel@276
|
111 |
SMBUSMASTER_1 = 5,
|
moel@276
|
112 |
SMBUSMASTER_2 = 6,
|
moel@276
|
113 |
SMBUSMASTER_3 = 7,
|
moel@276
|
114 |
SMBUSMASTER_4 = 8,
|
moel@276
|
115 |
SMBUSMASTER_5 = 9,
|
moel@276
|
116 |
SMBUSMASTER_6 = 10,
|
moel@276
|
117 |
SMBUSMASTER_7 = 11,
|
moel@276
|
118 |
PECI_0 = 12,
|
moel@276
|
119 |
PECI_1 = 13,
|
moel@276
|
120 |
PCH_CHIP_CPU_MAX_TEMP = 14,
|
moel@276
|
121 |
PCH_CHIP_TEMP = 15,
|
moel@276
|
122 |
PCH_CPU_TEMP = 16,
|
moel@276
|
123 |
PCH_MCH_TEMP = 17,
|
moel@276
|
124 |
PCH_DIM0_TEMP = 18,
|
moel@276
|
125 |
PCH_DIM1_TEMP = 19,
|
moel@276
|
126 |
PCH_DIM2_TEMP = 20,
|
moel@276
|
127 |
PCH_DIM3_TEMP = 21,
|
moel@276
|
128 |
BYTE_TEMP = 22
|
moel@276
|
129 |
}
|
moel@276
|
130 |
|
moel@245
|
131 |
public NCT677X(Chip chip, byte revision, ushort port) {
|
moel@245
|
132 |
this.chip = chip;
|
moel@245
|
133 |
this.revision = revision;
|
moel@245
|
134 |
this.port = port;
|
moel@245
|
135 |
|
moel@245
|
136 |
if (!IsNuvotonVendor())
|
moel@265
|
137 |
return;
|
moel@265
|
138 |
|
moel@265
|
139 |
switch (chip) {
|
moel@265
|
140 |
case LPC.Chip.NCT6771F:
|
moel@265
|
141 |
fans = new float?[4];
|
moel@265
|
142 |
// min value RPM value with 16-bit fan counter
|
moel@265
|
143 |
minFanRPM = (int)(1.35e6 / 0xFFFF);
|
moel@265
|
144 |
break;
|
moel@265
|
145 |
case LPC.Chip.NCT6776F:
|
moel@265
|
146 |
fans = new float?[5];
|
moel@265
|
147 |
// min value RPM value with 13-bit fan counter
|
moel@265
|
148 |
minFanRPM = (int)(1.35e6 / 0x1FFF);
|
moel@265
|
149 |
break;
|
moel@265
|
150 |
}
|
moel@245
|
151 |
}
|
moel@245
|
152 |
|
moel@245
|
153 |
private bool IsNuvotonVendor() {
|
moel@245
|
154 |
return ((ReadByte(VENDOR_ID_HIGH_REGISTER) << 8) |
|
moel@245
|
155 |
ReadByte(VENDOR_ID_LOW_REGISTER)) == NUVOTON_VENDOR_ID;
|
moel@245
|
156 |
}
|
moel@245
|
157 |
|
moel@245
|
158 |
public byte? ReadGPIO(int index) {
|
moel@245
|
159 |
return null;
|
moel@245
|
160 |
}
|
moel@245
|
161 |
|
moel@245
|
162 |
public void WriteGPIO(int index, byte value) { }
|
moel@245
|
163 |
|
moel@323
|
164 |
|
moel@323
|
165 |
private void SaveDefaultFanControl(int index) {
|
moel@323
|
166 |
if (!restoreDefaultFanControlRequired[index]) {
|
moel@323
|
167 |
initialFanControlMode[index] = ReadByte(FAN_CONTROL_MODE_REG[index]);
|
moel@323
|
168 |
initialFanPwmCommand[index] = ReadByte(FAN_PWM_COMMAND_REG[index]);
|
moel@323
|
169 |
restoreDefaultFanControlRequired[index] = true;
|
moel@323
|
170 |
}
|
moel@323
|
171 |
}
|
moel@323
|
172 |
|
moel@323
|
173 |
private void RestoreDefaultFanControl(int index) {
|
moel@323
|
174 |
if (restoreDefaultFanControlRequired[index]) {
|
moel@323
|
175 |
WriteByte(FAN_CONTROL_MODE_REG[index], initialFanControlMode[index]);
|
moel@323
|
176 |
WriteByte(FAN_PWM_COMMAND_REG[index], initialFanPwmCommand[index]);
|
moel@323
|
177 |
restoreDefaultFanControlRequired[index] = false;
|
moel@323
|
178 |
}
|
moel@323
|
179 |
}
|
moel@323
|
180 |
|
moel@323
|
181 |
public void SetControl(int index, byte? value) {
|
moel@323
|
182 |
if (!Ring0.WaitIsaBusMutex(10))
|
moel@323
|
183 |
return;
|
moel@323
|
184 |
|
moel@323
|
185 |
if (value.HasValue) {
|
moel@323
|
186 |
SaveDefaultFanControl(index);
|
moel@323
|
187 |
|
moel@323
|
188 |
// set manual mode
|
moel@323
|
189 |
WriteByte(FAN_CONTROL_MODE_REG[index], 0);
|
moel@323
|
190 |
|
moel@323
|
191 |
// set output value
|
moel@323
|
192 |
WriteByte(FAN_PWM_COMMAND_REG[index], value.Value);
|
moel@323
|
193 |
} else {
|
moel@323
|
194 |
RestoreDefaultFanControl(index);
|
moel@323
|
195 |
}
|
moel@323
|
196 |
|
moel@323
|
197 |
Ring0.ReleaseIsaBusMutex();
|
moel@323
|
198 |
}
|
moel@323
|
199 |
|
moel@245
|
200 |
public Chip Chip { get { return chip; } }
|
moel@245
|
201 |
public float?[] Voltages { get { return voltages; } }
|
moel@245
|
202 |
public float?[] Temperatures { get { return temperatures; } }
|
moel@245
|
203 |
public float?[] Fans { get { return fans; } }
|
moel@323
|
204 |
public float?[] Controls { get { return controls; } }
|
moel@245
|
205 |
|
moel@245
|
206 |
public void Update() {
|
moel@245
|
207 |
if (!Ring0.WaitIsaBusMutex(10))
|
moel@245
|
208 |
return;
|
moel@245
|
209 |
|
moel@245
|
210 |
for (int i = 0; i < voltages.Length; i++) {
|
moel@245
|
211 |
float value = 0.008f * ReadByte(VOLTAGE_REG[i]);
|
moel@245
|
212 |
bool valid = value > 0;
|
moel@245
|
213 |
|
moel@245
|
214 |
// check if battery voltage monitor is enabled
|
moel@245
|
215 |
if (valid && VOLTAGE_REG[i] == VOLTAGE_VBAT_REG)
|
moel@245
|
216 |
valid = (ReadByte(0x005D) & 0x01) > 0;
|
moel@245
|
217 |
|
moel@245
|
218 |
voltages[i] = valid ? value : (float?)null;
|
moel@245
|
219 |
}
|
moel@245
|
220 |
|
moel@276
|
221 |
for (int i = TEMPERATURE_REG.Length - 1; i >= 0 ; i--) {
|
moel@246
|
222 |
int value = ((sbyte)ReadByte(TEMPERATURE_REG[i])) << 1;
|
moel@245
|
223 |
if (TEMPERATURE_HALF_BIT[i] > 0) {
|
moel@246
|
224 |
value |= ((ReadByte(TEMPERATURE_HALF_REG[i]) >>
|
moel@245
|
225 |
TEMPERATURE_HALF_BIT[i]) & 0x1);
|
moel@245
|
226 |
}
|
moel@245
|
227 |
|
moel@276
|
228 |
byte source = ReadByte(TEMPERATURE_SRC_REG[i]);
|
moel@245
|
229 |
|
moel@246
|
230 |
float? temperature = 0.5f * value;
|
moel@246
|
231 |
if (temperature > 125 || temperature < -55)
|
moel@246
|
232 |
temperature = null;
|
moel@246
|
233 |
|
moel@276
|
234 |
switch (chip) {
|
moel@276
|
235 |
case Chip.NCT6771F:
|
moel@276
|
236 |
switch ((SourceNCT6771F)source) {
|
moel@276
|
237 |
case SourceNCT6771F.PECI_0: temperatures[0] = temperature; break;
|
moel@276
|
238 |
case SourceNCT6771F.CPUTIN: temperatures[1] = temperature; break;
|
moel@276
|
239 |
case SourceNCT6771F.AUXTIN: temperatures[2] = temperature; break;
|
moel@276
|
240 |
case SourceNCT6771F.SYSTIN: temperatures[3] = temperature; break;
|
moel@276
|
241 |
|
moel@276
|
242 |
} break;
|
moel@276
|
243 |
case Chip.NCT6776F:
|
moel@276
|
244 |
switch ((SourceNCT6776F)source) {
|
moel@276
|
245 |
case SourceNCT6776F.PECI_0: temperatures[0] = temperature; break;
|
moel@276
|
246 |
case SourceNCT6776F.CPUTIN: temperatures[1] = temperature; break;
|
moel@276
|
247 |
case SourceNCT6776F.AUXTIN: temperatures[2] = temperature; break;
|
moel@276
|
248 |
case SourceNCT6776F.SYSTIN: temperatures[3] = temperature; break;
|
moel@276
|
249 |
} break;
|
moel@276
|
250 |
}
|
moel@245
|
251 |
}
|
moel@245
|
252 |
|
moel@245
|
253 |
for (int i = 0; i < fans.Length; i++) {
|
moel@245
|
254 |
byte high = ReadByte(FAN_RPM_REG[i]);
|
moel@245
|
255 |
byte low = ReadByte((ushort)(FAN_RPM_REG[i] + 1));
|
moel@265
|
256 |
int value = (high << 8) | low;
|
moel@265
|
257 |
|
moel@265
|
258 |
fans[i] = value > minFanRPM ? value : 0;
|
moel@245
|
259 |
}
|
moel@245
|
260 |
|
moel@323
|
261 |
for (int i = 0; i < controls.Length; i++) {
|
moel@323
|
262 |
int value = ReadByte(FAN_PWM_OUT_REG[i]);
|
moel@323
|
263 |
controls[i] = value / 2.55f;
|
moel@323
|
264 |
}
|
moel@323
|
265 |
|
moel@245
|
266 |
Ring0.ReleaseIsaBusMutex();
|
moel@245
|
267 |
}
|
moel@245
|
268 |
|
moel@245
|
269 |
public string GetReport() {
|
moel@245
|
270 |
StringBuilder r = new StringBuilder();
|
moel@245
|
271 |
|
moel@245
|
272 |
r.AppendLine("LPC " + this.GetType().Name);
|
moel@245
|
273 |
r.AppendLine();
|
moel@245
|
274 |
r.Append("Chip ID: 0x"); r.AppendLine(chip.ToString("X"));
|
moel@245
|
275 |
r.Append("Chip revision: 0x");
|
moel@245
|
276 |
r.AppendLine(revision.ToString("X", CultureInfo.InvariantCulture));
|
moel@245
|
277 |
r.Append("Base Adress: 0x");
|
moel@245
|
278 |
r.AppendLine(port.ToString("X4", CultureInfo.InvariantCulture));
|
moel@245
|
279 |
r.AppendLine();
|
moel@245
|
280 |
|
moel@245
|
281 |
if (!Ring0.WaitIsaBusMutex(100))
|
moel@245
|
282 |
return r.ToString();
|
moel@245
|
283 |
|
moel@246
|
284 |
ushort[] addresses = new ushort[] {
|
moel@246
|
285 |
0x000, 0x010, 0x020, 0x030, 0x040, 0x050, 0x060, 0x070,
|
moel@246
|
286 |
0x100, 0x110, 0x120, 0x130, 0x140, 0x150,
|
moel@246
|
287 |
0x200, 0x220, 0x230, 0x240, 0x250,
|
moel@246
|
288 |
0x300, 0x320, 0x330, 0x340,
|
moel@246
|
289 |
0x400, 0x410, 0x420, 0x440, 0x450, 0x460,
|
moel@246
|
290 |
0x500, 0x550,
|
moel@246
|
291 |
0x600, 0x610 ,0x620, 0x630, 0x640, 0x650, 0x660, 0x670,
|
moel@246
|
292 |
0xA00, 0xA10, 0xA20, 0xA30, 0xA50, 0xA60, 0xA70,
|
moel@246
|
293 |
0xB00, 0xB10, 0xB20, 0xB30, 0xB50, 0xB60, 0xB70,
|
moel@246
|
294 |
0xC00, 0xC10, 0xC20, 0xC30, 0xC50, 0xC60, 0xC70,
|
moel@246
|
295 |
0xD00, 0xD10, 0xD20, 0xD30, 0xD50, 0xD60,
|
moel@246
|
296 |
0xE00, 0xE10, 0xE20, 0xE30,
|
moel@246
|
297 |
0xF00, 0xF10, 0xF20, 0xF30};
|
moel@246
|
298 |
|
moel@245
|
299 |
r.AppendLine("Hardware Monitor Registers");
|
moel@245
|
300 |
r.AppendLine();
|
moel@245
|
301 |
r.AppendLine(" 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F");
|
moel@245
|
302 |
r.AppendLine();
|
moel@246
|
303 |
foreach (ushort address in addresses) {
|
moel@245
|
304 |
r.Append(" ");
|
moel@246
|
305 |
r.Append(address.ToString("X3", CultureInfo.InvariantCulture));
|
moel@245
|
306 |
r.Append(" ");
|
moel@246
|
307 |
for (ushort j = 0; j <= 0xF; j++) {
|
moel@245
|
308 |
r.Append(" ");
|
moel@246
|
309 |
r.Append(ReadByte((ushort)(address | j)).ToString(
|
moel@245
|
310 |
"X2", CultureInfo.InvariantCulture));
|
moel@245
|
311 |
}
|
moel@245
|
312 |
r.AppendLine();
|
moel@245
|
313 |
}
|
moel@245
|
314 |
r.AppendLine();
|
moel@245
|
315 |
|
moel@245
|
316 |
Ring0.ReleaseIsaBusMutex();
|
moel@245
|
317 |
|
moel@245
|
318 |
return r.ToString();
|
moel@245
|
319 |
}
|
moel@245
|
320 |
}
|
moel@245
|
321 |
}
|