Fixed a few stability issues in the logging implementation. Added support for logging sensors once the reappear.
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-2013 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;
127 logicalDeviceNumber = FINTEK_HARDWARE_MONITOR_LDN;
130 chip = Chip.F71889ED;
131 logicalDeviceNumber = FINTEK_HARDWARE_MONITOR_LDN;
137 chip = Chip.F71889AD;
138 logicalDeviceNumber = FINTEK_HARDWARE_MONITOR_LDN;
142 logicalDeviceNumber = FINTEK_HARDWARE_MONITOR_LDN;
150 chip = Chip.W83627HF;
151 logicalDeviceNumber = WINBOND_NUVOTON_HARDWARE_MONITOR_LDN;
155 switch (revision & 0xF0) {
157 chip = Chip.W83627THF;
158 logicalDeviceNumber = WINBOND_NUVOTON_HARDWARE_MONITOR_LDN;
164 chip = Chip.W83687THF;
165 logicalDeviceNumber = WINBOND_NUVOTON_HARDWARE_MONITOR_LDN;
169 switch (revision & 0xF0) {
172 chip = Chip.W83627EHF;
173 logicalDeviceNumber = WINBOND_NUVOTON_HARDWARE_MONITOR_LDN;
177 switch (revision & 0xF0) {
179 chip = Chip.W83627DHG;
180 logicalDeviceNumber = WINBOND_NUVOTON_HARDWARE_MONITOR_LDN;
184 switch (revision & 0xF0) {
186 chip = Chip.W83667HG;
187 logicalDeviceNumber = WINBOND_NUVOTON_HARDWARE_MONITOR_LDN;
191 switch (revision & 0xF0) {
193 chip = Chip.W83627DHGP;
194 logicalDeviceNumber = WINBOND_NUVOTON_HARDWARE_MONITOR_LDN;
198 switch (revision & 0xF0) {
200 chip = Chip.W83667HGB;
201 logicalDeviceNumber = WINBOND_NUVOTON_HARDWARE_MONITOR_LDN;
205 switch (revision & 0xF0) {
207 chip = Chip.NCT6771F;
208 logicalDeviceNumber = WINBOND_NUVOTON_HARDWARE_MONITOR_LDN;
212 switch (revision & 0xF0) {
214 chip = Chip.NCT6776F;
215 logicalDeviceNumber = WINBOND_NUVOTON_HARDWARE_MONITOR_LDN;
219 switch (revision & 0xF0) {
221 chip = Chip.NCT6779D;
222 logicalDeviceNumber = WINBOND_NUVOTON_HARDWARE_MONITOR_LDN;
228 chip = Chip.NCT6791D;
229 logicalDeviceNumber = WINBOND_NUVOTON_HARDWARE_MONITOR_LDN;
233 if (chip == Chip.Unknown) {
234 if (id != 0 && id != 0xff) {
235 WinbondNuvotonFintekExit();
237 ReportUnknownChip("Winbond / Nuvoton / Fintek",
238 ((id << 8) | revision));
242 Select(logicalDeviceNumber);
243 ushort address = ReadWord(BASE_ADDRESS_REGISTER);
245 ushort verify = ReadWord(BASE_ADDRESS_REGISTER);
247 ushort vendorID = ReadWord(FINTEK_VENDOR_ID_REGISTER);
249 WinbondNuvotonFintekExit();
251 if (address != verify) {
252 report.Append("Chip ID: 0x");
253 report.AppendLine(chip.ToString("X"));
254 report.Append("Chip revision: 0x");
255 report.AppendLine(revision.ToString("X",
256 CultureInfo.InvariantCulture));
257 report.AppendLine("Error: Address verification failed");
262 // some Fintek chips have address register offset 0x05 added already
263 if ((address & 0x07) == 0x05)
266 if (address < 0x100 || (address & 0xF007) != 0) {
267 report.Append("Chip ID: 0x");
268 report.AppendLine(chip.ToString("X"));
269 report.Append("Chip revision: 0x");
270 report.AppendLine(revision.ToString("X",
271 CultureInfo.InvariantCulture));
272 report.Append("Error: Invalid address 0x");
273 report.AppendLine(address.ToString("X",
274 CultureInfo.InvariantCulture));
281 case Chip.W83627DHGP:
288 superIOs.Add(new W836XX(chip, revision, address));
294 superIOs.Add(new NCT677X(chip, revision, address));
305 if (vendorID != FINTEK_VENDOR_ID) {
306 report.Append("Chip ID: 0x");
307 report.AppendLine(chip.ToString("X"));
308 report.Append("Chip revision: 0x");
309 report.AppendLine(revision.ToString("X",
310 CultureInfo.InvariantCulture));
311 report.Append("Error: Invalid vendor ID 0x");
312 report.AppendLine(vendorID.ToString("X",
313 CultureInfo.InvariantCulture));
317 superIOs.Add(new F718XX(chip, address));
332 private const byte IT87_ENVIRONMENT_CONTROLLER_LDN = 0x04;
333 private const byte IT8705_GPIO_LDN = 0x05;
334 private const byte IT87XX_GPIO_LDN = 0x07;
335 private const byte IT87_CHIP_VERSION_REGISTER = 0x22;
337 private void IT87Enter() {
338 Ring0.WriteIoPort(registerPort, 0x87);
339 Ring0.WriteIoPort(registerPort, 0x01);
340 Ring0.WriteIoPort(registerPort, 0x55);
341 Ring0.WriteIoPort(registerPort, 0x55);
344 private void IT87Exit() {
345 Ring0.WriteIoPort(registerPort, CONFIGURATION_CONTROL_REGISTER);
346 Ring0.WriteIoPort(valuePort, 0x02);
349 private bool DetectIT87() {
351 // IT87XX can enter only on port 0x2E
352 if (registerPort != 0x2E)
357 ushort chipID = ReadWord(CHIP_ID_REGISTER);
360 case 0x8705: chip = Chip.IT8705F; break;
361 case 0x8712: chip = Chip.IT8712F; break;
362 case 0x8716: chip = Chip.IT8716F; break;
363 case 0x8718: chip = Chip.IT8718F; break;
364 case 0x8720: chip = Chip.IT8720F; break;
365 case 0x8721: chip = Chip.IT8721F; break;
366 case 0x8726: chip = Chip.IT8726F; break;
367 case 0x8728: chip = Chip.IT8728F; break;
368 case 0x8771: chip = Chip.IT8771E; break;
369 case 0x8772: chip = Chip.IT8772E; break;
370 default: chip = Chip.Unknown; break;
372 if (chip == Chip.Unknown) {
373 if (chipID != 0 && chipID != 0xffff) {
376 ReportUnknownChip("ITE", chipID);
379 Select(IT87_ENVIRONMENT_CONTROLLER_LDN);
380 ushort address = ReadWord(BASE_ADDRESS_REGISTER);
382 ushort verify = ReadWord(BASE_ADDRESS_REGISTER);
384 byte version = (byte)(ReadByte(IT87_CHIP_VERSION_REGISTER) & 0x0F);
388 if (chip == Chip.IT8705F) {
389 Select(IT8705_GPIO_LDN);
390 gpioAddress = ReadWord(BASE_ADDRESS_REGISTER);
392 gpioVerify = ReadWord(BASE_ADDRESS_REGISTER);
394 Select(IT87XX_GPIO_LDN);
395 gpioAddress = ReadWord(BASE_ADDRESS_REGISTER + 2);
397 gpioVerify = ReadWord(BASE_ADDRESS_REGISTER + 2);
402 if (address != verify || address < 0x100 || (address & 0xF007) != 0) {
403 report.Append("Chip ID: 0x");
404 report.AppendLine(chip.ToString("X"));
405 report.Append("Error: Invalid address 0x");
406 report.AppendLine(address.ToString("X",
407 CultureInfo.InvariantCulture));
412 if (gpioAddress != gpioVerify || gpioAddress < 0x100 ||
413 (gpioAddress & 0xF007) != 0) {
414 report.Append("Chip ID: 0x");
415 report.AppendLine(chip.ToString("X"));
416 report.Append("Error: Invalid GPIO address 0x");
417 report.AppendLine(gpioAddress.ToString("X",
418 CultureInfo.InvariantCulture));
423 superIOs.Add(new IT87XX(chip, address, gpioAddress, version));
434 private void SMSCEnter() {
435 Ring0.WriteIoPort(registerPort, 0x55);
438 private void SMSCExit() {
439 Ring0.WriteIoPort(registerPort, 0xAA);
442 private bool DetectSMSC() {
445 ushort chipID = ReadWord(CHIP_ID_REGISTER);
448 default: chip = Chip.Unknown; break;
450 if (chip == Chip.Unknown) {
451 if (chipID != 0 && chipID != 0xffff) {
454 ReportUnknownChip("SMSC", chipID);
466 private void Detect() {
468 for (int i = 0; i < REGISTER_PORTS.Length; i++) {
469 registerPort = REGISTER_PORTS[i];
470 valuePort = VALUE_PORTS[i];
472 if (DetectWinbondFintek()) continue;
474 if (DetectIT87()) continue;
476 if (DetectSMSC()) continue;
484 if (!Ring0.WaitIsaBusMutex(100))
489 Ring0.ReleaseIsaBusMutex();
492 public ISuperIO[] SuperIO {
494 return superIOs.ToArray();
498 public string GetReport() {
499 if (report.Length > 0) {
500 return "LPCIO" + Environment.NewLine + Environment.NewLine + report;