Added support for reading more than one TBalancer fan controller.
3 This Source Code Form is subject to the terms of the Mozilla Public
4 License, v. 2.0. If a copy of the MPL was not distributed with this
5 file, You can obtain one at http://mozilla.org/MPL/2.0/.
7 Copyright (C) 2009-2012 Michael Möller <mmoeller@openhardwaremonitor.org>
12 using System.Collections.Generic;
13 using System.Globalization;
15 using System.Threading;
17 namespace OpenHardwareMonitor.Hardware.LPC {
18 internal class LPCIO {
20 private readonly List<ISuperIO> superIOs = new List<ISuperIO>();
21 private readonly StringBuilder report = new StringBuilder();
24 private readonly ushort[] REGISTER_PORTS = new ushort[] { 0x2E, 0x4E };
25 private readonly ushort[] VALUE_PORTS = new ushort[] { 0x2F, 0x4F };
27 private ushort registerPort;
28 private ushort valuePort;
31 private const byte CONFIGURATION_CONTROL_REGISTER = 0x02;
32 private const byte DEVCIE_SELECT_REGISTER = 0x07;
33 private const byte CHIP_ID_REGISTER = 0x20;
34 private const byte CHIP_REVISION_REGISTER = 0x21;
35 private const byte BASE_ADDRESS_REGISTER = 0x60;
37 private byte ReadByte(byte register) {
38 Ring0.WriteIoPort(registerPort, register);
39 return Ring0.ReadIoPort(valuePort);
42 private ushort ReadWord(byte register) {
43 return (ushort)((ReadByte(register) << 8) |
44 ReadByte((byte)(register + 1)));
47 private void Select(byte logicalDeviceNumber) {
48 Ring0.WriteIoPort(registerPort, DEVCIE_SELECT_REGISTER);
49 Ring0.WriteIoPort(valuePort, logicalDeviceNumber);
52 private void ReportUnknownChip(string type, int chip) {
53 report.Append("Chip ID: Unknown ");
55 report.Append(" with ID 0x");
56 report.Append(chip.ToString("X", CultureInfo.InvariantCulture));
57 report.Append(" at 0x");
58 report.Append(registerPort.ToString("X", CultureInfo.InvariantCulture));
60 report.AppendLine(valuePort.ToString("X", CultureInfo.InvariantCulture));
64 #region Winbond, Nuvoton, Fintek
66 private const byte FINTEK_VENDOR_ID_REGISTER = 0x23;
67 private const ushort FINTEK_VENDOR_ID = 0x1934;
69 private const byte WINBOND_NUVOTON_HARDWARE_MONITOR_LDN = 0x0B;
71 private const byte F71858_HARDWARE_MONITOR_LDN = 0x02;
72 private const byte FINTEK_HARDWARE_MONITOR_LDN = 0x04;
74 private void WinbondNuvotonFintekEnter() {
75 Ring0.WriteIoPort(registerPort, 0x87);
76 Ring0.WriteIoPort(registerPort, 0x87);
79 private void WinbondNuvotonFintekExit() {
80 Ring0.WriteIoPort(registerPort, 0xAA);
83 private bool DetectWinbondFintek() {
84 WinbondNuvotonFintekEnter();
86 byte logicalDeviceNumber = 0;
87 byte id = ReadByte(CHIP_ID_REGISTER);
88 byte revision = ReadByte(CHIP_REVISION_REGISTER);
89 Chip chip = Chip.Unknown;
95 logicalDeviceNumber = F71858_HARDWARE_MONITOR_LDN;
99 logicalDeviceNumber = FINTEK_HARDWARE_MONITOR_LDN;
106 logicalDeviceNumber = FINTEK_HARDWARE_MONITOR_LDN;
113 logicalDeviceNumber = FINTEK_HARDWARE_MONITOR_LDN;
120 logicalDeviceNumber = FINTEK_HARDWARE_MONITOR_LDN;
126 chip = Chip.F71889ED;
127 logicalDeviceNumber = FINTEK_HARDWARE_MONITOR_LDN;
133 chip = Chip.F71889AD;
134 logicalDeviceNumber = FINTEK_HARDWARE_MONITOR_LDN;
142 chip = Chip.W83627HF;
143 logicalDeviceNumber = WINBOND_NUVOTON_HARDWARE_MONITOR_LDN;
147 switch (revision & 0xF0) {
149 chip = Chip.W83627THF;
150 logicalDeviceNumber = WINBOND_NUVOTON_HARDWARE_MONITOR_LDN;
156 chip = Chip.W83687THF;
157 logicalDeviceNumber = WINBOND_NUVOTON_HARDWARE_MONITOR_LDN;
161 switch (revision & 0xF0) {
164 chip = Chip.W83627EHF;
165 logicalDeviceNumber = WINBOND_NUVOTON_HARDWARE_MONITOR_LDN;
169 switch (revision & 0xF0) {
171 chip = Chip.W83627DHG;
172 logicalDeviceNumber = WINBOND_NUVOTON_HARDWARE_MONITOR_LDN;
176 switch (revision & 0xF0) {
178 chip = Chip.W83667HG;
179 logicalDeviceNumber = WINBOND_NUVOTON_HARDWARE_MONITOR_LDN;
183 switch (revision & 0xF0) {
185 chip = Chip.W83627DHGP;
186 logicalDeviceNumber = WINBOND_NUVOTON_HARDWARE_MONITOR_LDN;
190 switch (revision & 0xF0) {
192 chip = Chip.W83667HGB;
193 logicalDeviceNumber = WINBOND_NUVOTON_HARDWARE_MONITOR_LDN;
197 switch (revision & 0xF0) {
199 chip = Chip.NCT6771F;
200 logicalDeviceNumber = WINBOND_NUVOTON_HARDWARE_MONITOR_LDN;
204 switch (revision & 0xF0) {
206 chip = Chip.NCT6776F;
207 logicalDeviceNumber = WINBOND_NUVOTON_HARDWARE_MONITOR_LDN;
211 if (chip == Chip.Unknown) {
212 if (id != 0 && id != 0xff) {
213 WinbondNuvotonFintekExit();
215 ReportUnknownChip("Winbond / Nuvoton / Fintek",
216 ((id << 8) | revision));
220 Select(logicalDeviceNumber);
221 ushort address = ReadWord(BASE_ADDRESS_REGISTER);
223 ushort verify = ReadWord(BASE_ADDRESS_REGISTER);
225 ushort vendorID = ReadWord(FINTEK_VENDOR_ID_REGISTER);
227 WinbondNuvotonFintekExit();
229 if (address != verify) {
230 report.Append("Chip ID: 0x");
231 report.AppendLine(chip.ToString("X"));
232 report.Append("Chip revision: 0x");
233 report.AppendLine(revision.ToString("X",
234 CultureInfo.InvariantCulture));
235 report.AppendLine("Error: Address verification failed");
240 // some Fintek chips have address register offset 0x05 added already
241 if ((address & 0x07) == 0x05)
244 if (address < 0x100 || (address & 0xF007) != 0) {
245 report.Append("Chip ID: 0x");
246 report.AppendLine(chip.ToString("X"));
247 report.Append("Chip revision: 0x");
248 report.AppendLine(revision.ToString("X",
249 CultureInfo.InvariantCulture));
250 report.Append("Error: Invalid address 0x");
251 report.AppendLine(address.ToString("X",
252 CultureInfo.InvariantCulture));
259 case Chip.W83627DHGP:
266 superIOs.Add(new W836XX(chip, revision, address));
270 superIOs.Add(new NCT677X(chip, revision, address));
279 if (vendorID != FINTEK_VENDOR_ID) {
280 report.Append("Chip ID: 0x");
281 report.AppendLine(chip.ToString("X"));
282 report.Append("Chip revision: 0x");
283 report.AppendLine(revision.ToString("X",
284 CultureInfo.InvariantCulture));
285 report.Append("Error: Invalid vendor ID 0x");
286 report.AppendLine(vendorID.ToString("X",
287 CultureInfo.InvariantCulture));
291 superIOs.Add(new F718XX(chip, address));
306 private const byte IT87_ENVIRONMENT_CONTROLLER_LDN = 0x04;
307 private const byte IT87_GPIO_LDN = 0x07;
308 private const byte IT87_CHIP_VERSION_REGISTER = 0x22;
310 private void IT87Enter() {
311 Ring0.WriteIoPort(registerPort, 0x87);
312 Ring0.WriteIoPort(registerPort, 0x01);
313 Ring0.WriteIoPort(registerPort, 0x55);
314 Ring0.WriteIoPort(registerPort, 0x55);
317 private void IT87Exit() {
318 Ring0.WriteIoPort(registerPort, CONFIGURATION_CONTROL_REGISTER);
319 Ring0.WriteIoPort(valuePort, 0x02);
322 private bool DetectIT87() {
324 // IT87XX can enter only on port 0x2E
325 if (registerPort != 0x2E)
330 ushort chipID = ReadWord(CHIP_ID_REGISTER);
333 case 0x8712: chip = Chip.IT8712F; break;
334 case 0x8716: chip = Chip.IT8716F; break;
335 case 0x8718: chip = Chip.IT8718F; break;
336 case 0x8720: chip = Chip.IT8720F; break;
337 case 0x8721: chip = Chip.IT8721F; break;
338 case 0x8726: chip = Chip.IT8726F; break;
339 case 0x8728: chip = Chip.IT8728F; break;
340 case 0x8771: chip = Chip.IT8771E; break;
341 case 0x8772: chip = Chip.IT8772E; break;
342 default: chip = Chip.Unknown; break;
344 if (chip == Chip.Unknown) {
345 if (chipID != 0 && chipID != 0xffff) {
348 ReportUnknownChip("ITE", chipID);
351 Select(IT87_ENVIRONMENT_CONTROLLER_LDN);
352 ushort address = ReadWord(BASE_ADDRESS_REGISTER);
354 ushort verify = ReadWord(BASE_ADDRESS_REGISTER);
356 byte version = (byte)(ReadByte(IT87_CHIP_VERSION_REGISTER) & 0x0F);
358 Select(IT87_GPIO_LDN);
359 ushort gpioAddress = ReadWord(BASE_ADDRESS_REGISTER + 2);
361 ushort gpioVerify = ReadWord(BASE_ADDRESS_REGISTER + 2);
365 if (address != verify || address < 0x100 || (address & 0xF007) != 0) {
366 report.Append("Chip ID: 0x");
367 report.AppendLine(chip.ToString("X"));
368 report.Append("Error: Invalid address 0x");
369 report.AppendLine(address.ToString("X",
370 CultureInfo.InvariantCulture));
375 if (gpioAddress != gpioVerify || gpioAddress < 0x100 ||
376 (gpioAddress & 0xF007) != 0) {
377 report.Append("Chip ID: 0x");
378 report.AppendLine(chip.ToString("X"));
379 report.Append("Error: Invalid GPIO address 0x");
380 report.AppendLine(gpioAddress.ToString("X",
381 CultureInfo.InvariantCulture));
386 superIOs.Add(new IT87XX(chip, address, gpioAddress, version));
397 private void SMSCEnter() {
398 Ring0.WriteIoPort(registerPort, 0x55);
401 private void SMSCExit() {
402 Ring0.WriteIoPort(registerPort, 0xAA);
405 private bool DetectSMSC() {
408 ushort chipID = ReadWord(CHIP_ID_REGISTER);
411 default: chip = Chip.Unknown; break;
413 if (chip == Chip.Unknown) {
414 if (chipID != 0 && chipID != 0xffff) {
417 ReportUnknownChip("SMSC", chipID);
429 private void Detect() {
431 for (int i = 0; i < REGISTER_PORTS.Length; i++) {
432 registerPort = REGISTER_PORTS[i];
433 valuePort = VALUE_PORTS[i];
435 if (DetectWinbondFintek()) continue;
437 if (DetectIT87()) continue;
439 if (DetectSMSC()) continue;
447 if (!Ring0.WaitIsaBusMutex(100))
452 Ring0.ReleaseIsaBusMutex();
455 public ISuperIO[] SuperIO {
457 return superIOs.ToArray();
461 public string GetReport() {
462 if (report.Length > 0) {
463 return "LPCIO" + Environment.NewLine + Environment.NewLine + report;