moel@110: /* moel@110: moel@344: This Source Code Form is subject to the terms of the Mozilla Public moel@344: License, v. 2.0. If a copy of the MPL was not distributed with this moel@344: file, You can obtain one at http://mozilla.org/MPL/2.0/. moel@110: moel@408: Copyright (C) 2009-2013 Michael Möller moel@344: moel@110: */ moel@110: moel@110: using System; moel@110: using System.Collections.Generic; moel@166: using System.Globalization; moel@110: using System.Text; moel@110: using System.Threading; moel@110: moel@110: namespace OpenHardwareMonitor.Hardware.LPC { moel@165: internal class LPCIO { moel@110: moel@195: private readonly List superIOs = new List(); moel@195: private readonly StringBuilder report = new StringBuilder(); moel@110: moel@110: // I/O Ports moel@195: private readonly ushort[] REGISTER_PORTS = new ushort[] { 0x2E, 0x4E }; moel@195: private readonly ushort[] VALUE_PORTS = new ushort[] { 0x2F, 0x4F }; moel@110: moel@110: private ushort registerPort; moel@110: private ushort valuePort; moel@110: moel@110: // Registers moel@110: private const byte CONFIGURATION_CONTROL_REGISTER = 0x02; moel@110: private const byte DEVCIE_SELECT_REGISTER = 0x07; moel@110: private const byte CHIP_ID_REGISTER = 0x20; moel@110: private const byte CHIP_REVISION_REGISTER = 0x21; moel@110: private const byte BASE_ADDRESS_REGISTER = 0x60; moel@110: moel@110: private byte ReadByte(byte register) { moel@236: Ring0.WriteIoPort(registerPort, register); moel@236: return Ring0.ReadIoPort(valuePort); moel@228: } moel@110: moel@423: private void WriteByte(byte register, byte value) { moel@423: Ring0.WriteIoPort(registerPort, register); moel@423: Ring0.WriteIoPort(valuePort, value); moel@423: } moel@423: moel@110: private ushort ReadWord(byte register) { moel@228: return (ushort)((ReadByte(register) << 8) | moel@110: ReadByte((byte)(register + 1))); moel@110: } moel@110: moel@110: private void Select(byte logicalDeviceNumber) { moel@236: Ring0.WriteIoPort(registerPort, DEVCIE_SELECT_REGISTER); moel@236: Ring0.WriteIoPort(valuePort, logicalDeviceNumber); moel@110: } moel@110: moel@169: private void ReportUnknownChip(string type, int chip) { moel@169: report.Append("Chip ID: Unknown "); moel@169: report.Append(type); moel@169: report.Append(" with ID 0x"); moel@169: report.Append(chip.ToString("X", CultureInfo.InvariantCulture)); moel@169: report.Append(" at 0x"); moel@169: report.Append(registerPort.ToString("X", CultureInfo.InvariantCulture)); moel@169: report.Append("/0x"); moel@169: report.AppendLine(valuePort.ToString("X", CultureInfo.InvariantCulture)); moel@169: report.AppendLine(); moel@110: } moel@110: moel@245: #region Winbond, Nuvoton, Fintek moel@110: moel@110: private const byte FINTEK_VENDOR_ID_REGISTER = 0x23; moel@110: private const ushort FINTEK_VENDOR_ID = 0x1934; moel@110: moel@245: private const byte WINBOND_NUVOTON_HARDWARE_MONITOR_LDN = 0x0B; moel@110: moel@110: private const byte F71858_HARDWARE_MONITOR_LDN = 0x02; moel@110: private const byte FINTEK_HARDWARE_MONITOR_LDN = 0x04; moel@110: moel@423: private const byte NUVOTON_HARDWARE_MONITOR_IO_SPACE_LOCK = 0x28; moel@423: moel@245: private void WinbondNuvotonFintekEnter() { moel@236: Ring0.WriteIoPort(registerPort, 0x87); moel@236: Ring0.WriteIoPort(registerPort, 0x87); moel@110: } moel@110: moel@245: private void WinbondNuvotonFintekExit() { moel@236: Ring0.WriteIoPort(registerPort, 0xAA); moel@110: } moel@110: moel@167: private bool DetectWinbondFintek() { moel@245: WinbondNuvotonFintekEnter(); moel@167: moel@195: byte logicalDeviceNumber = 0; moel@167: byte id = ReadByte(CHIP_ID_REGISTER); moel@167: byte revision = ReadByte(CHIP_REVISION_REGISTER); moel@167: Chip chip = Chip.Unknown; moel@167: switch (id) { moel@167: case 0x05: moel@167: switch (revision) { moel@167: case 0x07: moel@167: chip = Chip.F71858; moel@167: logicalDeviceNumber = F71858_HARDWARE_MONITOR_LDN; moel@167: break; moel@167: case 0x41: moel@167: chip = Chip.F71882; moel@167: logicalDeviceNumber = FINTEK_HARDWARE_MONITOR_LDN; moel@167: break; moel@167: } break; moel@167: case 0x06: moel@167: switch (revision) { moel@167: case 0x01: moel@167: chip = Chip.F71862; moel@167: logicalDeviceNumber = FINTEK_HARDWARE_MONITOR_LDN; moel@167: break; moel@167: } break; moel@167: case 0x07: moel@167: switch (revision) { moel@167: case 0x23: moel@167: chip = Chip.F71889F; moel@167: logicalDeviceNumber = FINTEK_HARDWARE_MONITOR_LDN; moel@167: break; moel@167: } break; moel@167: case 0x08: moel@167: switch (revision) { moel@167: case 0x14: moel@167: chip = Chip.F71869; moel@167: logicalDeviceNumber = FINTEK_HARDWARE_MONITOR_LDN; moel@167: break; moel@167: } break; moel@167: case 0x09: moel@167: switch (revision) { moel@352: case 0x01: moel@352: chip = Chip.F71808E; moel@352: logicalDeviceNumber = FINTEK_HARDWARE_MONITOR_LDN; moel@352: break; moel@167: case 0x09: moel@167: chip = Chip.F71889ED; moel@167: logicalDeviceNumber = FINTEK_HARDWARE_MONITOR_LDN; moel@167: break; moel@167: } break; moel@296: case 0x10: moel@296: switch (revision) { moel@296: case 0x05: moel@296: chip = Chip.F71889AD; moel@296: logicalDeviceNumber = FINTEK_HARDWARE_MONITOR_LDN; moel@296: break; moel@408: case 0x07: moel@408: chip = Chip.F71869A; moel@408: logicalDeviceNumber = FINTEK_HARDWARE_MONITOR_LDN; moel@408: break; moel@296: } break; moel@167: case 0x52: moel@167: switch (revision) { moel@167: case 0x17: moel@167: case 0x3A: moel@167: case 0x41: moel@167: chip = Chip.W83627HF; moel@245: logicalDeviceNumber = WINBOND_NUVOTON_HARDWARE_MONITOR_LDN; moel@167: break; moel@167: } break; moel@167: case 0x82: moel@167: switch (revision & 0xF0) { moel@167: case 0x80: moel@167: chip = Chip.W83627THF; moel@245: logicalDeviceNumber = WINBOND_NUVOTON_HARDWARE_MONITOR_LDN; moel@167: break; moel@167: } break; moel@167: case 0x85: moel@167: switch (revision) { moel@167: case 0x41: moel@167: chip = Chip.W83687THF; moel@245: logicalDeviceNumber = WINBOND_NUVOTON_HARDWARE_MONITOR_LDN; moel@167: break; moel@167: } break; moel@167: case 0x88: moel@167: switch (revision & 0xF0) { moel@167: case 0x50: moel@167: case 0x60: moel@167: chip = Chip.W83627EHF; moel@245: logicalDeviceNumber = WINBOND_NUVOTON_HARDWARE_MONITOR_LDN; moel@167: break; moel@167: } break; moel@167: case 0xA0: moel@167: switch (revision & 0xF0) { moel@167: case 0x20: moel@167: chip = Chip.W83627DHG; moel@245: logicalDeviceNumber = WINBOND_NUVOTON_HARDWARE_MONITOR_LDN; moel@167: break; moel@167: } break; moel@167: case 0xA5: moel@167: switch (revision & 0xF0) { moel@167: case 0x10: moel@167: chip = Chip.W83667HG; moel@245: logicalDeviceNumber = WINBOND_NUVOTON_HARDWARE_MONITOR_LDN; moel@167: break; moel@167: } break; moel@167: case 0xB0: moel@167: switch (revision & 0xF0) { moel@167: case 0x70: moel@167: chip = Chip.W83627DHGP; moel@245: logicalDeviceNumber = WINBOND_NUVOTON_HARDWARE_MONITOR_LDN; moel@167: break; moel@167: } break; moel@167: case 0xB3: moel@167: switch (revision & 0xF0) { moel@167: case 0x50: moel@167: chip = Chip.W83667HGB; moel@245: logicalDeviceNumber = WINBOND_NUVOTON_HARDWARE_MONITOR_LDN; moel@245: break; moel@245: } break; moel@245: case 0xB4: moel@245: switch (revision & 0xF0) { moel@245: case 0x70: moel@245: chip = Chip.NCT6771F; moel@245: logicalDeviceNumber = WINBOND_NUVOTON_HARDWARE_MONITOR_LDN; moel@167: break; moel@167: } break; moel@265: case 0xC3: moel@265: switch (revision & 0xF0) { moel@265: case 0x30: moel@265: chip = Chip.NCT6776F; moel@265: logicalDeviceNumber = WINBOND_NUVOTON_HARDWARE_MONITOR_LDN; moel@265: break; moel@265: } break; moel@355: case 0xC5: moel@355: switch (revision & 0xF0) { moel@355: case 0x60: moel@355: chip = Chip.NCT6779D; moel@355: logicalDeviceNumber = WINBOND_NUVOTON_HARDWARE_MONITOR_LDN; moel@355: break; moel@355: } break; moel@413: case 0xC8: moel@413: switch (revision) { moel@413: case 0x03: moel@413: chip = Chip.NCT6791D; moel@413: logicalDeviceNumber = WINBOND_NUVOTON_HARDWARE_MONITOR_LDN; moel@413: break; moel@413: } break; moel@167: } moel@167: if (chip == Chip.Unknown) { moel@167: if (id != 0 && id != 0xff) { moel@245: WinbondNuvotonFintekExit(); moel@167: moel@245: ReportUnknownChip("Winbond / Nuvoton / Fintek", moel@245: ((id << 8) | revision)); moel@167: } moel@167: } else { moel@167: moel@167: Select(logicalDeviceNumber); moel@167: ushort address = ReadWord(BASE_ADDRESS_REGISTER); moel@167: Thread.Sleep(1); moel@167: ushort verify = ReadWord(BASE_ADDRESS_REGISTER); moel@167: moel@167: ushort vendorID = ReadWord(FINTEK_VENDOR_ID_REGISTER); moel@167: moel@423: // disable the hardware monitor i/o space lock on NCT6791D chips moel@423: if (address == verify && chip == Chip.NCT6791D) { moel@423: byte options = ReadByte(NUVOTON_HARDWARE_MONITOR_IO_SPACE_LOCK); moel@423: moel@423: // if the i/o space lock is enabled moel@423: if ((options & 0x10) > 0) { moel@423: moel@423: // disable the i/o space lock moel@423: WriteByte(NUVOTON_HARDWARE_MONITOR_IO_SPACE_LOCK, moel@423: (byte)(options & ~0x10)); moel@423: } moel@423: } moel@423: moel@245: WinbondNuvotonFintekExit(); moel@167: moel@167: if (address != verify) { moel@167: report.Append("Chip ID: 0x"); moel@167: report.AppendLine(chip.ToString("X")); moel@167: report.Append("Chip revision: 0x"); moel@228: report.AppendLine(revision.ToString("X", moel@167: CultureInfo.InvariantCulture)); moel@167: report.AppendLine("Error: Address verification failed"); moel@167: report.AppendLine(); moel@167: return false; moel@167: } moel@167: moel@167: // some Fintek chips have address register offset 0x05 added already moel@167: if ((address & 0x07) == 0x05) moel@167: address &= 0xFFF8; moel@167: moel@167: if (address < 0x100 || (address & 0xF007) != 0) { moel@167: report.Append("Chip ID: 0x"); moel@167: report.AppendLine(chip.ToString("X")); moel@167: report.Append("Chip revision: 0x"); moel@228: report.AppendLine(revision.ToString("X", moel@167: CultureInfo.InvariantCulture)); moel@167: report.Append("Error: Invalid address 0x"); moel@228: report.AppendLine(address.ToString("X", moel@167: CultureInfo.InvariantCulture)); moel@167: report.AppendLine(); moel@167: return false; moel@167: } moel@167: moel@167: switch (chip) { moel@167: case Chip.W83627DHG: moel@167: case Chip.W83627DHGP: moel@167: case Chip.W83627EHF: moel@167: case Chip.W83627HF: moel@167: case Chip.W83627THF: moel@167: case Chip.W83667HG: moel@167: case Chip.W83667HGB: moel@167: case Chip.W83687THF: moel@167: superIOs.Add(new W836XX(chip, revision, address)); moel@167: break; moel@245: case Chip.NCT6771F: moel@265: case Chip.NCT6776F: moel@355: case Chip.NCT6779D: moel@413: case Chip.NCT6791D: moel@245: superIOs.Add(new NCT677X(chip, revision, address)); moel@245: break; moel@167: case Chip.F71858: moel@167: case Chip.F71862: moel@167: case Chip.F71869: moel@408: case Chip.F71869A: moel@167: case Chip.F71882: moel@296: case Chip.F71889AD: moel@167: case Chip.F71889ED: moel@167: case Chip.F71889F: moel@352: case Chip.F71808E: moel@167: if (vendorID != FINTEK_VENDOR_ID) { moel@167: report.Append("Chip ID: 0x"); moel@167: report.AppendLine(chip.ToString("X")); moel@167: report.Append("Chip revision: 0x"); moel@228: report.AppendLine(revision.ToString("X", moel@167: CultureInfo.InvariantCulture)); moel@167: report.Append("Error: Invalid vendor ID 0x"); moel@228: report.AppendLine(vendorID.ToString("X", moel@167: CultureInfo.InvariantCulture)); moel@167: report.AppendLine(); moel@167: return false; moel@167: } moel@167: superIOs.Add(new F718XX(chip, address)); moel@167: break; moel@167: default: break; moel@167: } moel@167: moel@167: return true; moel@167: } moel@167: moel@167: return false; moel@167: } moel@167: moel@169: #endregion moel@169: moel@169: #region ITE moel@169: moel@169: private const byte IT87_ENVIRONMENT_CONTROLLER_LDN = 0x04; moel@353: private const byte IT8705_GPIO_LDN = 0x05; moel@353: private const byte IT87XX_GPIO_LDN = 0x07; moel@169: private const byte IT87_CHIP_VERSION_REGISTER = 0x22; moel@169: moel@169: private void IT87Enter() { moel@236: Ring0.WriteIoPort(registerPort, 0x87); moel@236: Ring0.WriteIoPort(registerPort, 0x01); moel@236: Ring0.WriteIoPort(registerPort, 0x55); moel@236: Ring0.WriteIoPort(registerPort, 0x55); moel@169: } moel@169: moel@169: private void IT87Exit() { moel@236: Ring0.WriteIoPort(registerPort, CONFIGURATION_CONTROL_REGISTER); moel@236: Ring0.WriteIoPort(valuePort, 0x02); moel@169: } moel@169: moel@167: private bool DetectIT87() { moel@169: moel@169: // IT87XX can enter only on port 0x2E moel@169: if (registerPort != 0x2E) moel@169: return false; moel@169: moel@167: IT87Enter(); moel@167: moel@167: ushort chipID = ReadWord(CHIP_ID_REGISTER); moel@167: Chip chip; moel@167: switch (chipID) { moel@353: case 0x8705: chip = Chip.IT8705F; break; moel@167: case 0x8712: chip = Chip.IT8712F; break; moel@167: case 0x8716: chip = Chip.IT8716F; break; moel@167: case 0x8718: chip = Chip.IT8718F; break; moel@167: case 0x8720: chip = Chip.IT8720F; break; moel@170: case 0x8721: chip = Chip.IT8721F; break; moel@167: case 0x8726: chip = Chip.IT8726F; break; moel@277: case 0x8728: chip = Chip.IT8728F; break; moel@341: case 0x8771: chip = Chip.IT8771E; break; moel@319: case 0x8772: chip = Chip.IT8772E; break; moel@167: default: chip = Chip.Unknown; break; moel@167: } moel@167: if (chip == Chip.Unknown) { moel@167: if (chipID != 0 && chipID != 0xffff) { moel@167: IT87Exit(); moel@167: moel@167: ReportUnknownChip("ITE", chipID); moel@167: } moel@167: } else { moel@167: Select(IT87_ENVIRONMENT_CONTROLLER_LDN); moel@167: ushort address = ReadWord(BASE_ADDRESS_REGISTER); moel@167: Thread.Sleep(1); moel@167: ushort verify = ReadWord(BASE_ADDRESS_REGISTER); moel@167: moel@167: byte version = (byte)(ReadByte(IT87_CHIP_VERSION_REGISTER) & 0x0F); moel@167: moel@353: ushort gpioAddress; moel@353: ushort gpioVerify; moel@353: if (chip == Chip.IT8705F) { moel@353: Select(IT8705_GPIO_LDN); moel@353: gpioAddress = ReadWord(BASE_ADDRESS_REGISTER); moel@353: Thread.Sleep(1); moel@353: gpioVerify = ReadWord(BASE_ADDRESS_REGISTER); moel@353: } else { moel@353: Select(IT87XX_GPIO_LDN); moel@353: gpioAddress = ReadWord(BASE_ADDRESS_REGISTER + 2); moel@353: Thread.Sleep(1); moel@353: gpioVerify = ReadWord(BASE_ADDRESS_REGISTER + 2); moel@353: } moel@353: moel@167: IT87Exit(); moel@167: moel@167: if (address != verify || address < 0x100 || (address & 0xF007) != 0) { moel@167: report.Append("Chip ID: 0x"); moel@167: report.AppendLine(chip.ToString("X")); moel@167: report.Append("Error: Invalid address 0x"); moel@167: report.AppendLine(address.ToString("X", moel@167: CultureInfo.InvariantCulture)); moel@167: report.AppendLine(); moel@167: return false; moel@167: } moel@167: moel@228: if (gpioAddress != gpioVerify || gpioAddress < 0x100 || moel@228: (gpioAddress & 0xF007) != 0) { moel@228: report.Append("Chip ID: 0x"); moel@228: report.AppendLine(chip.ToString("X")); moel@228: report.Append("Error: Invalid GPIO address 0x"); moel@228: report.AppendLine(gpioAddress.ToString("X", moel@228: CultureInfo.InvariantCulture)); moel@228: report.AppendLine(); moel@228: return false; moel@228: } moel@228: moel@228: superIOs.Add(new IT87XX(chip, address, gpioAddress, version)); moel@167: return true; moel@167: } moel@167: moel@167: return false; moel@167: } moel@167: moel@169: #endregion moel@169: moel@169: #region SMSC moel@169: moel@169: private void SMSCEnter() { moel@236: Ring0.WriteIoPort(registerPort, 0x55); moel@169: } moel@169: moel@169: private void SMSCExit() { moel@236: Ring0.WriteIoPort(registerPort, 0xAA); moel@169: } moel@169: moel@167: private bool DetectSMSC() { moel@167: SMSCEnter(); moel@167: moel@167: ushort chipID = ReadWord(CHIP_ID_REGISTER); moel@167: Chip chip; moel@167: switch (chipID) { moel@167: default: chip = Chip.Unknown; break; moel@167: } moel@167: if (chip == Chip.Unknown) { moel@167: if (chipID != 0 && chipID != 0xffff) { moel@167: SMSCExit(); moel@167: moel@167: ReportUnknownChip("SMSC", chipID); moel@167: } moel@167: } else { moel@167: SMSCExit(); moel@167: return true; moel@167: } moel@167: moel@167: return false; moel@167: } moel@167: moel@169: #endregion moel@169: moel@163: private void Detect() { moel@110: moel@110: for (int i = 0; i < REGISTER_PORTS.Length; i++) { moel@110: registerPort = REGISTER_PORTS[i]; moel@110: valuePort = VALUE_PORTS[i]; moel@110: moel@167: if (DetectWinbondFintek()) continue; moel@110: moel@167: if (DetectIT87()) continue; moel@110: moel@167: if (DetectSMSC()) continue; moel@228: } moel@163: } moel@163: moel@163: public LPCIO() { moel@236: if (!Ring0.IsOpen) moel@163: return; moel@163: moel@236: if (!Ring0.WaitIsaBusMutex(100)) moel@163: return; moel@163: moel@163: Detect(); moel@163: moel@236: Ring0.ReleaseIsaBusMutex(); moel@110: } moel@110: moel@130: public ISuperIO[] SuperIO { moel@110: get { moel@130: return superIOs.ToArray(); moel@110: } moel@110: } moel@110: moel@110: public string GetReport() { moel@110: if (report.Length > 0) { moel@195: return "LPCIO" + Environment.NewLine + Environment.NewLine + report; moel@110: } else moel@110: return null; moel@110: } moel@110: } moel@110: }