moel@110: /* moel@110: moel@110: Version: MPL 1.1/GPL 2.0/LGPL 2.1 moel@110: moel@110: The contents of this file are subject to the Mozilla Public License Version moel@110: 1.1 (the "License"); you may not use this file except in compliance with moel@110: the License. You may obtain a copy of the License at moel@110: moel@110: http://www.mozilla.org/MPL/ moel@110: moel@110: Software distributed under the License is distributed on an "AS IS" basis, moel@110: WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License moel@110: for the specific language governing rights and limitations under the License. moel@110: moel@110: The Original Code is the Open Hardware Monitor code. moel@110: moel@110: The Initial Developer of the Original Code is moel@110: Michael Möller . moel@110: Portions created by the Initial Developer are Copyright (C) 2009-2010 moel@110: the Initial Developer. All Rights Reserved. moel@110: moel@110: Contributor(s): moel@110: moel@110: Alternatively, the contents of this file may be used under the terms of moel@110: either the GNU General Public License Version 2 or later (the "GPL"), or moel@110: the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), moel@110: in which case the provisions of the GPL or the LGPL are applicable instead moel@110: of those above. If you wish to allow use of your version of this file only moel@110: under the terms of either the GPL or the LGPL, and not to allow others to moel@110: use your version of this file under the terms of the MPL, indicate your moel@110: decision by deleting the provisions above and replace them with the notice moel@110: and other provisions required by the GPL or the LGPL. If you do not delete moel@110: the provisions above, a recipient may use your version of this file under moel@110: the terms of any one of the MPL, the GPL or the LGPL. moel@110: 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@130: private List superIOs = new List(); moel@110: private StringBuilder report = new StringBuilder(); moel@110: moel@110: // I/O Ports moel@169: private ushort[] REGISTER_PORTS = new ushort[] { 0x2E, 0x4E }; moel@169: private 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@110: WinRing0.WriteIoPortByte(registerPort, register); moel@110: return WinRing0.ReadIoPortByte(valuePort); moel@110: } moel@110: moel@110: private ushort ReadWord(byte register) { moel@110: return (ushort)((ReadByte(register) << 8) | moel@110: ReadByte((byte)(register + 1))); moel@110: } moel@110: moel@110: private void Select(byte logicalDeviceNumber) { moel@110: WinRing0.WriteIoPortByte(registerPort, DEVCIE_SELECT_REGISTER); moel@110: WinRing0.WriteIoPortByte(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@169: #region Winbond, 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@110: private const byte WINBOND_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@110: private void WinbondFintekEnter() { moel@110: WinRing0.WriteIoPortByte(registerPort, 0x87); moel@110: WinRing0.WriteIoPortByte(registerPort, 0x87); moel@110: } moel@110: moel@110: private void WinbondFintekExit() { moel@110: WinRing0.WriteIoPortByte(registerPort, 0xAA); moel@110: } moel@110: moel@167: private bool DetectWinbondFintek() { moel@167: WinbondFintekEnter(); moel@167: moel@167: byte logicalDeviceNumber; moel@167: byte id = ReadByte(CHIP_ID_REGISTER); moel@167: byte revision = ReadByte(CHIP_REVISION_REGISTER); moel@167: Chip chip = Chip.Unknown; moel@167: logicalDeviceNumber = 0; 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@167: case 0x09: moel@167: chip = Chip.F71889ED; moel@167: logicalDeviceNumber = FINTEK_HARDWARE_MONITOR_LDN; moel@167: break; moel@167: } 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@167: logicalDeviceNumber = WINBOND_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@167: logicalDeviceNumber = WINBOND_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@167: logicalDeviceNumber = WINBOND_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@167: logicalDeviceNumber = WINBOND_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@167: logicalDeviceNumber = WINBOND_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@167: logicalDeviceNumber = WINBOND_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@167: logicalDeviceNumber = WINBOND_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@167: logicalDeviceNumber = WINBOND_HARDWARE_MONITOR_LDN; moel@167: break; moel@167: } break; moel@167: } moel@167: if (chip == Chip.Unknown) { moel@167: if (id != 0 && id != 0xff) { moel@167: WinbondFintekExit(); moel@167: moel@167: ReportUnknownChip("Winbond / Fintek", ((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@167: WinbondFintekExit(); 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@167: 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@167: report.AppendLine(revision.ToString("X", moel@167: CultureInfo.InvariantCulture)); 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@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@167: case Chip.F71858: moel@167: case Chip.F71862: moel@167: case Chip.F71869: moel@167: case Chip.F71882: moel@167: case Chip.F71889ED: moel@167: case Chip.F71889F: 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@167: report.AppendLine(revision.ToString("X", moel@167: CultureInfo.InvariantCulture)); moel@167: report.Append("Error: Invalid vendor ID 0x"); moel@167: 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@169: private const byte IT87_CHIP_VERSION_REGISTER = 0x22; moel@169: moel@169: private void IT87Enter() { moel@169: WinRing0.WriteIoPortByte(registerPort, 0x87); moel@169: WinRing0.WriteIoPortByte(registerPort, 0x01); moel@169: WinRing0.WriteIoPortByte(registerPort, 0x55); moel@169: WinRing0.WriteIoPortByte(registerPort, 0x55); moel@169: } moel@169: moel@169: private void IT87Exit() { moel@169: WinRing0.WriteIoPortByte(registerPort, CONFIGURATION_CONTROL_REGISTER); moel@169: WinRing0.WriteIoPortByte(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@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@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@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@167: superIOs.Add(new IT87XX(chip, address, 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@169: WinRing0.WriteIoPortByte(registerPort, 0x55); moel@169: } moel@169: moel@169: private void SMSCExit() { moel@169: WinRing0.WriteIoPortByte(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@163: } moel@163: } moel@163: moel@163: public LPCIO() { moel@163: if (!WinRing0.IsAvailable) moel@163: return; moel@163: moel@163: if (!WinRing0.WaitIsaBusMutex(100)) moel@163: return; moel@163: moel@163: Detect(); moel@163: moel@163: WinRing0.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@169: return "LPCIO" + Environment.NewLine + Environment.NewLine + moel@169: report.ToString(); moel@110: } else moel@110: return null; moel@110: } moel@110: } moel@110: }