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@110: using System.Text; moel@110: using System.Threading; moel@110: moel@110: namespace OpenHardwareMonitor.Hardware.LPC { moel@110: public class LPCIO { moel@110: moel@110: private List hardware = new List(); moel@110: private StringBuilder report = new StringBuilder(); moel@110: moel@110: private Chip chip = Chip.Unknown; moel@110: moel@110: // I/O Ports moel@110: private ushort[] REGISTER_PORTS = new ushort[] { 0x2e, 0x4e }; moel@110: 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@110: // ITE moel@110: private const byte IT87_ENVIRONMENT_CONTROLLER_LDN = 0x04; moel@110: moel@110: private void IT87Enter() { moel@110: WinRing0.WriteIoPortByte(registerPort, 0x87); moel@110: WinRing0.WriteIoPortByte(registerPort, 0x01); moel@110: WinRing0.WriteIoPortByte(registerPort, 0x55); moel@110: WinRing0.WriteIoPortByte(registerPort, 0x55); moel@110: } moel@110: moel@110: internal void IT87Exit() { moel@110: WinRing0.WriteIoPortByte(registerPort, CONFIGURATION_CONTROL_REGISTER); moel@110: WinRing0.WriteIoPortByte(valuePort, 0x02); moel@110: } moel@110: moel@110: // Winbond, Fintek 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@110: // SMSC moel@110: private void SMSCEnter() { moel@110: WinRing0.WriteIoPortByte(registerPort, 0x55); moel@110: } moel@110: moel@110: private void SMSCExit() { moel@110: WinRing0.WriteIoPortByte(registerPort, 0xAA); moel@110: } moel@110: moel@110: public LPCIO() { moel@110: if (!WinRing0.IsAvailable) moel@110: return; 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@110: WinbondFintekEnter(); moel@110: moel@110: byte logicalDeviceNumber; moel@110: byte id = ReadByte(CHIP_ID_REGISTER); moel@110: byte revision = ReadByte(CHIP_REVISION_REGISTER); moel@110: chip = Chip.Unknown; moel@110: logicalDeviceNumber = 0; moel@110: switch (id) { moel@110: case 0x05: moel@110: switch (revision) { moel@110: case 0x07: moel@110: chip = Chip.F71858; moel@110: logicalDeviceNumber = F71858_HARDWARE_MONITOR_LDN; moel@110: break; moel@110: case 0x41: moel@110: chip = Chip.F71882; moel@110: logicalDeviceNumber = FINTEK_HARDWARE_MONITOR_LDN; moel@110: break; moel@110: } break; moel@110: case 0x06: moel@110: switch (revision) { moel@110: case 0x01: moel@110: chip = Chip.F71862; moel@110: logicalDeviceNumber = FINTEK_HARDWARE_MONITOR_LDN; moel@110: break; moel@110: } break; moel@110: case 0x07: moel@110: switch (revision) { moel@110: case 0x23: moel@110: chip = Chip.F71889F; moel@110: logicalDeviceNumber = FINTEK_HARDWARE_MONITOR_LDN; moel@110: break; moel@110: } break; moel@110: case 0x08: moel@110: switch (revision) { moel@110: case 0x14: moel@110: chip = Chip.F71869; moel@110: logicalDeviceNumber = FINTEK_HARDWARE_MONITOR_LDN; moel@110: break; moel@110: } break; moel@110: case 0x09: moel@110: switch (revision) { moel@110: case 0x09: moel@110: chip = Chip.F71889ED; moel@110: logicalDeviceNumber = FINTEK_HARDWARE_MONITOR_LDN; moel@110: break; moel@110: } break; moel@110: case 0x52: moel@110: switch (revision) { moel@110: case 0x17: moel@110: case 0x3A: moel@110: case 0x41: moel@110: chip = Chip.W83627HF; moel@110: logicalDeviceNumber = WINBOND_HARDWARE_MONITOR_LDN; moel@110: break; moel@110: } break; moel@110: case 0x82: moel@110: switch (revision) { moel@110: case 0x83: moel@110: chip = Chip.W83627THF; moel@110: logicalDeviceNumber = WINBOND_HARDWARE_MONITOR_LDN; moel@110: break; moel@110: } break; moel@110: case 0x85: moel@110: switch (revision) { moel@110: case 0x41: moel@110: chip = Chip.W83687THF; moel@110: logicalDeviceNumber = WINBOND_HARDWARE_MONITOR_LDN; moel@110: break; moel@110: } break; moel@110: case 0x88: moel@110: switch (revision & 0xF0) { moel@110: case 0x50: moel@110: case 0x60: moel@110: chip = Chip.W83627EHF; moel@110: logicalDeviceNumber = WINBOND_HARDWARE_MONITOR_LDN; moel@110: break; moel@110: } break; moel@110: case 0xA0: moel@110: switch (revision & 0xF0) { moel@110: case 0x20: moel@110: chip = Chip.W83627DHG; moel@110: logicalDeviceNumber = WINBOND_HARDWARE_MONITOR_LDN; moel@110: break; moel@110: } break; moel@110: case 0xA5: moel@110: switch (revision & 0xF0) { moel@110: case 0x10: moel@110: chip = Chip.W83667HG; moel@110: logicalDeviceNumber = WINBOND_HARDWARE_MONITOR_LDN; moel@110: break; moel@110: } break; moel@110: case 0xB0: moel@110: switch (revision & 0xF0) { moel@110: case 0x70: moel@110: chip = Chip.W83627DHGP; moel@110: logicalDeviceNumber = WINBOND_HARDWARE_MONITOR_LDN; moel@110: break; moel@110: } break; moel@110: case 0xB3: moel@110: switch (revision & 0xF0) { moel@110: case 0x50: moel@110: chip = Chip.W83667HGB; moel@110: logicalDeviceNumber = WINBOND_HARDWARE_MONITOR_LDN; moel@110: break; moel@110: } break; moel@110: } moel@110: if (chip == Chip.Unknown) { moel@110: if (id != 0 && id != 0xff) { moel@110: WinbondFintekExit(); moel@110: moel@110: report.Append("Chip ID: Unknown Winbond / Fintek with ID 0x"); moel@110: report.AppendLine(((id << 8) | revision).ToString("X")); moel@110: report.AppendLine(); moel@110: } moel@110: } else { moel@110: moel@110: Select(logicalDeviceNumber); moel@110: ushort address = ReadWord(BASE_ADDRESS_REGISTER); moel@110: Thread.Sleep(1); moel@110: ushort verify = ReadWord(BASE_ADDRESS_REGISTER); moel@110: moel@110: ushort vendorID = ReadWord(FINTEK_VENDOR_ID_REGISTER); moel@110: moel@110: WinbondFintekExit(); moel@110: moel@110: if (address != verify) { moel@110: report.Append("Chip ID: 0x"); moel@110: report.AppendLine(chip.ToString("X")); moel@110: report.Append("Chip revision: 0x"); moel@110: report.AppendLine(revision.ToString("X")); moel@110: report.AppendLine("Error: Address verification failed"); moel@110: report.AppendLine(); moel@110: return; moel@110: } moel@110: moel@110: // some Fintek chips have address register offset 0x05 added already moel@110: if ((address & 0x07) == 0x05) moel@110: address &= 0xFFF8; moel@110: moel@110: if (address < 0x100 || (address & 0xF007) != 0) { moel@110: report.Append("Chip ID: 0x"); moel@110: report.AppendLine(chip.ToString("X")); moel@110: report.Append("Chip revision: 0x"); moel@110: report.AppendLine(revision.ToString("X")); moel@110: report.Append("Error: Invalid address 0x"); moel@110: report.AppendLine(address.ToString("X")); moel@110: report.AppendLine(); moel@110: return; moel@110: } moel@110: moel@110: switch (chip) { moel@110: case Chip.W83627DHG: moel@110: case Chip.W83627DHGP: moel@110: case Chip.W83627EHF: moel@110: case Chip.W83627HF: moel@110: case Chip.W83627THF: moel@110: case Chip.W83667HG: moel@110: case Chip.W83667HGB: moel@110: case Chip.W83687THF: moel@110: W836XX w836XX = new W836XX(chip, revision, address); moel@110: if (w836XX.IsAvailable) moel@110: hardware.Add(w836XX); moel@110: break; moel@110: case Chip.F71858: moel@110: case Chip.F71862: moel@110: case Chip.F71869: moel@110: case Chip.F71882: moel@110: case Chip.F71889ED: moel@110: case Chip.F71889F: moel@110: if (vendorID != FINTEK_VENDOR_ID) { moel@110: report.Append("Chip ID: 0x"); moel@110: report.AppendLine(chip.ToString("X")); moel@110: report.Append("Chip revision: 0x"); moel@110: report.AppendLine(revision.ToString("X")); moel@110: report.Append("Error: Invalid vendor ID 0x"); moel@110: report.AppendLine(vendorID.ToString("X")); moel@110: report.AppendLine(); moel@110: return; moel@110: } moel@110: hardware.Add(new F718XX(chip, address)); moel@110: break; moel@110: default: break; moel@110: } moel@110: moel@110: return; moel@110: } moel@110: moel@110: IT87Enter(); moel@110: moel@110: ushort chipID = ReadWord(CHIP_ID_REGISTER); moel@110: switch (chipID) { moel@110: case 0x8712: chip = Chip.IT8712F; break; moel@110: case 0x8716: chip = Chip.IT8716F; break; moel@110: case 0x8718: chip = Chip.IT8718F; break; moel@110: case 0x8720: chip = Chip.IT8720F; break; moel@110: case 0x8726: chip = Chip.IT8726F; break; moel@110: default: chip = Chip.Unknown; break; moel@110: } moel@110: if (chip == Chip.Unknown) { moel@110: if (chipID != 0 && chipID != 0xffff) { moel@110: IT87Exit(); moel@110: moel@110: report.Append("Chip ID: Unknown ITE with ID 0x"); moel@110: report.AppendLine(chipID.ToString("X")); moel@110: report.AppendLine(); moel@110: } moel@110: } else { moel@110: Select(IT87_ENVIRONMENT_CONTROLLER_LDN); moel@110: ushort address = ReadWord(BASE_ADDRESS_REGISTER); moel@110: Thread.Sleep(1); moel@110: ushort verify = ReadWord(BASE_ADDRESS_REGISTER); moel@110: moel@110: IT87Exit(); moel@110: moel@110: if (address != verify || address < 0x100 || (address & 0xF007) != 0) { moel@110: report.Append("Chip ID: 0x"); moel@110: report.AppendLine(chip.ToString("X")); moel@110: report.Append("Error: Invalid address 0x"); moel@110: report.AppendLine(address.ToString("X")); moel@110: report.AppendLine(); moel@110: return; moel@110: } moel@110: moel@110: IT87XX it87 = new IT87XX(chip, address); moel@110: if (it87.IsAvailable) moel@110: hardware.Add(it87); moel@110: moel@110: return; moel@110: } moel@110: moel@110: SMSCEnter(); moel@110: moel@110: chipID = ReadWord(CHIP_ID_REGISTER); moel@110: switch (chipID) { moel@110: default: chip = Chip.Unknown; break; moel@110: } moel@110: if (chip == Chip.Unknown) { moel@110: if (chipID != 0 && chipID != 0xffff) { moel@110: SMSCExit(); moel@110: moel@110: report.Append("Chip ID: Unknown SMSC with ID 0x"); moel@110: report.AppendLine(chipID.ToString("X")); moel@110: report.AppendLine(); moel@110: } moel@110: } else { moel@110: SMSCExit(); moel@110: moel@110: return; moel@110: } moel@110: } moel@110: } moel@110: moel@110: public IHardware[] Hardware { moel@110: get { moel@110: return hardware.ToArray(); moel@110: } moel@110: } moel@110: moel@110: public string GetReport() { moel@110: if (report.Length > 0) { moel@110: report.Insert(0, "LPCIO" + Environment.NewLine + moel@110: Environment.NewLine); moel@110: return report.ToString(); moel@110: } else moel@110: return null; moel@110: } moel@110: } moel@110: }