moel@1: /*
moel@1:   
moel@1:   Version: MPL 1.1/GPL 2.0/LGPL 2.1
moel@1: 
moel@1:   The contents of this file are subject to the Mozilla Public License Version
moel@1:   1.1 (the "License"); you may not use this file except in compliance with
moel@1:   the License. You may obtain a copy of the License at
moel@1:  
moel@1:   http://www.mozilla.org/MPL/
moel@1: 
moel@1:   Software distributed under the License is distributed on an "AS IS" basis,
moel@1:   WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
moel@1:   for the specific language governing rights and limitations under the License.
moel@1: 
moel@1:   The Original Code is the Open Hardware Monitor code.
moel@1: 
moel@1:   The Initial Developer of the Original Code is 
moel@1:   Michael Möller <m.moeller@gmx.ch>.
moel@1:   Portions created by the Initial Developer are Copyright (C) 2009-2010
moel@1:   the Initial Developer. All Rights Reserved.
moel@1: 
moel@1:   Contributor(s):
moel@1: 
moel@1:   Alternatively, the contents of this file may be used under the terms of
moel@1:   either the GNU General Public License Version 2 or later (the "GPL"), or
moel@1:   the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
moel@1:   in which case the provisions of the GPL or the LGPL are applicable instead
moel@1:   of those above. If you wish to allow use of your version of this file only
moel@1:   under the terms of either the GPL or the LGPL, and not to allow others to
moel@1:   use your version of this file under the terms of the MPL, indicate your
moel@1:   decision by deleting the provisions above and replace them with the notice
moel@1:   and other provisions required by the GPL or the LGPL. If you do not delete
moel@1:   the provisions above, a recipient may use your version of this file under
moel@1:   the terms of any one of the MPL, the GPL or the LGPL.
moel@1:  
moel@1: */
moel@1: 
moel@1: using System;
moel@1: using System.Collections.Generic;
moel@1: using System.Text;
moel@1: using System.Threading;
moel@1: 
moel@1: namespace OpenHardwareMonitor.Hardware.LPC {
moel@1:   public class LPCGroup : IGroup {
moel@1:     private List<IHardware> hardware = new List<IHardware>();
moel@1: 
moel@1:     private Chip chip = Chip.Unknown;
moel@1: 
moel@1:     // I/O Ports
moel@7:     private ushort[] REGISTER_PORTS = new ushort[] { 0x2e, 0x4e };
moel@7:     private ushort[] VALUE_PORTS = new ushort[] { 0x2f, 0x4f };
moel@7: 
moel@7:     private ushort registerPort;
moel@7:     private ushort valuePort;
moel@1: 
moel@1:     // Registers
moel@1:     private const byte CONFIGURATION_CONTROL_REGISTER = 0x02;
moel@1:     private const byte DEVCIE_SELECT_REGISTER = 0x07;
moel@1:     private const byte CHIP_ID_REGISTER = 0x20;
moel@1:     private const byte CHIP_REVISION_REGISTER = 0x21;
moel@7:     private const byte BASE_ADDRESS_REGISTER = 0x60;
moel@1: 
moel@7:     private byte ReadByte(byte register) {
moel@7:       WinRing0.WriteIoPortByte(registerPort, register);
moel@7:       return WinRing0.ReadIoPortByte(valuePort);
moel@13:     } 
moel@1: 
moel@7:     private ushort ReadWord(byte register) {
moel@7:       return (ushort)((ReadByte(register) << 8) | 
moel@7:         ReadByte((byte)(register + 1)));
moel@1:     }
moel@1: 
moel@7:     private void Select(byte logicalDeviceNumber) {
moel@7:       WinRing0.WriteIoPortByte(registerPort, DEVCIE_SELECT_REGISTER);
moel@7:       WinRing0.WriteIoPortByte(valuePort, logicalDeviceNumber);
moel@1:     }
moel@1: 
moel@34:     // ITE
moel@7:     private const byte IT87_ENVIRONMENT_CONTROLLER_LDN = 0x04;    
moel@1: 
moel@7:     private void IT87Enter() {
moel@7:       WinRing0.WriteIoPortByte(registerPort, 0x87);
moel@7:       WinRing0.WriteIoPortByte(registerPort, 0x01);
moel@7:       WinRing0.WriteIoPortByte(registerPort, 0x55);
moel@7:       WinRing0.WriteIoPortByte(registerPort, 0x55);
moel@1:     }
moel@1: 
moel@7:     internal void IT87Exit() {
moel@7:       WinRing0.WriteIoPortByte(registerPort, CONFIGURATION_CONTROL_REGISTER);
moel@7:       WinRing0.WriteIoPortByte(valuePort, 0x02);
moel@1:     }
moel@1: 
moel@7:     // Winbond, Fintek
moel@7:     private const byte FINTEK_VENDOR_ID_REGISTER = 0x23;
moel@7:     private const ushort FINTEK_VENDOR_ID = 0x1934;
moel@7: 
moel@34:     private const byte WINBOND_HARDWARE_MONITOR_LDN = 0x0B;
moel@16: 
moel@16:     private const byte F71858_HARDWARE_MONITOR_LDN = 0x02;
moel@16:     private const byte FINTEK_HARDWARE_MONITOR_LDN = 0x04;
moel@7: 
moel@7:     private void WinbondFintekEnter() {
moel@7:       WinRing0.WriteIoPortByte(registerPort, 0x87);
moel@7:       WinRing0.WriteIoPortByte(registerPort, 0x87);
moel@1:     }
moel@1: 
moel@7:     private void WinbondFintekExit() {
moel@7:       WinRing0.WriteIoPortByte(registerPort, 0xAA);      
moel@1:     }
moel@1: 
moel@1:     public LPCGroup() {
moel@1:       if (!WinRing0.IsAvailable)
moel@1:         return;
moel@1: 
moel@7:       for (int i = 0; i < REGISTER_PORTS.Length; i++) {
moel@7:         registerPort = REGISTER_PORTS[i];
moel@7:         valuePort = VALUE_PORTS[i];
moel@1: 
moel@7:         WinbondFintekEnter();
moel@1: 
moel@16:         byte logicalDeviceNumber;
moel@7:         byte id = ReadByte(CHIP_ID_REGISTER);
moel@7:         byte revision = ReadByte(CHIP_REVISION_REGISTER);
moel@34:         chip = Chip.Unknown;
moel@34:         logicalDeviceNumber = 0;
moel@7:         switch (id) {
moel@16:           case 0x05:
moel@16:             switch (revision) {
moel@16:               case 0x41:
moel@16:                 chip = Chip.F71882;
moel@16:                 logicalDeviceNumber = FINTEK_HARDWARE_MONITOR_LDN;
moel@34:                 break;              
moel@16:             } break;
moel@16:           case 0x06:
moel@16:             switch (revision) {             
moel@16:               case 0x01:
moel@16:                 chip = Chip.F71862;
moel@16:                 logicalDeviceNumber = FINTEK_HARDWARE_MONITOR_LDN;
moel@34:                 break;              
moel@16:             } break;
moel@16:           case 0x07:
moel@16:             switch (revision) {
moel@16:               case 0x23:
moel@16:                 chip = Chip.F71889;
moel@16:                 logicalDeviceNumber = FINTEK_HARDWARE_MONITOR_LDN;
moel@34:                 break;              
moel@16:             } break;
moel@16:           case 0x08:
moel@16:             switch (revision) {
moel@16:               case 0x14:
moel@16:                 chip = Chip.F71869;
moel@16:                 logicalDeviceNumber = FINTEK_HARDWARE_MONITOR_LDN;
moel@34:                 break;              
moel@16:             } break;
moel@31:           case 0x52:
moel@31:             switch (revision) {
moel@31:               case 0x17:
moel@31:               case 0x3A:
moel@31:                 chip = Chip.W83627HF;
moel@34:                 logicalDeviceNumber = WINBOND_HARDWARE_MONITOR_LDN;
moel@34:                 break;             
moel@34:             } break;
moel@54:           case 0x82:
moel@54:             switch (revision) {
moel@54:               case 0x83:
moel@54:                 chip = Chip.W83627THF;
moel@54:                 logicalDeviceNumber = WINBOND_HARDWARE_MONITOR_LDN;
moel@54:                 break;
moel@54:             } break;
moel@34:           case 0x88:
moel@34:             switch (revision & 0xF0) {
moel@34:               case 0x60:
moel@34:                 chip = Chip.W83627EHF;
moel@34:                 logicalDeviceNumber = WINBOND_HARDWARE_MONITOR_LDN;
moel@31:                 break;
moel@31:             } break;
moel@7:           case 0xA0:
moel@7:             switch (revision & 0xF0) {
moel@7:               case 0x20: 
moel@7:                 chip = Chip.W83627DHG;
moel@34:                 logicalDeviceNumber = WINBOND_HARDWARE_MONITOR_LDN;  
moel@34:                 break;             
moel@34:             } break;
moel@34:           case 0xA5:
moel@34:             switch (revision & 0xF0) {
moel@34:               case 0x10:
moel@34:                 chip = Chip.W83667HG;
moel@34:                 logicalDeviceNumber = WINBOND_HARDWARE_MONITOR_LDN;
moel@7:                 break;
moel@19:             } break;
moel@19:           case 0xB0:
moel@19:             switch (revision & 0xF0) {
moel@19:               case 0x70:
moel@19:                 chip = Chip.W83627DHGP;
moel@34:                 logicalDeviceNumber = WINBOND_HARDWARE_MONITOR_LDN;
moel@34:                 break;             
moel@34:             } break;
moel@34:           case 0xB3:
moel@34:             switch (revision & 0xF0) {
moel@34:               case 0x50:
moel@34:                 chip = Chip.W83667HGB;
moel@34:                 logicalDeviceNumber = WINBOND_HARDWARE_MONITOR_LDN;
moel@19:                 break;
moel@34:             } break; 
moel@7:         }
moel@7:         if (chip != Chip.Unknown) {
moel@1: 
moel@16:           Select(logicalDeviceNumber);
moel@60:           ushort address = ReadWord(BASE_ADDRESS_REGISTER);          
moel@7:           Thread.Sleep(1);
moel@7:           ushort verify = ReadWord(BASE_ADDRESS_REGISTER);
moel@60:           
moel@7:           ushort vendorID = 0;
moel@16:           if (chip == Chip.F71862 || chip == Chip.F71882 || chip == Chip.F71889)
moel@7:             vendorID = ReadWord(FINTEK_VENDOR_ID_REGISTER);
moel@1: 
moel@7:           WinbondFintekExit();
moel@1: 
moel@60:           if (address != verify)
moel@60:             return;
moel@60: 
moel@60:           // some Fintek chips have address register offset 0x05 added already
moel@60:           if ((address & 0x07) == 0x05)
moel@60:             address &= 0xFFF8;
moel@60: 
moel@60:           if (address < 0x100 || (address & 0xF007) != 0)
moel@7:             return;
moel@7:           
moel@7:           switch (chip) {
moel@7:             case Chip.W83627DHG:
moel@19:             case Chip.W83627DHGP:
moel@34:             case Chip.W83627EHF:
moel@31:             case Chip.W83627HF:
moel@54:             case Chip.W83627THF:
moel@34:             case Chip.W83667HG:
moel@34:             case Chip.W83667HGB:
moel@34:               W836XX w836XX = new W836XX(chip, revision, address);
moel@34:               if (w836XX.IsAvailable)
moel@34:                 hardware.Add(w836XX);
moel@7:               break;
moel@16:             case Chip.F71862:
moel@16:             case Chip.F71882:
moel@16:             case Chip.F71889: 
moel@7:               if (vendorID == FINTEK_VENDOR_ID)
moel@16:                 hardware.Add(new F718XX(chip, address));
moel@16:               break;
moel@16:             case Chip.F71869:
moel@16:               hardware.Add(new F718XX(chip, address));
moel@7:               break;
moel@7:             default: break;
moel@7:           }
moel@7:           
moel@7:           return;
moel@7:         }
moel@1: 
moel@7:         IT87Enter();
moel@1: 
moel@7:         switch (ReadWord(CHIP_ID_REGISTER)) {
moel@21:           case 0x8716: chip = Chip.IT8716F; break;
moel@21:           case 0x8718: chip = Chip.IT8718F; break;
moel@21:           case 0x8720: chip = Chip.IT8720F; break;
moel@21:           case 0x8726: chip = Chip.IT8726F; break;
moel@7:           default: chip = Chip.Unknown; break;
moel@7:         }
moel@7: 
moel@7:         if (chip != Chip.Unknown) {
moel@7:           Select(IT87_ENVIRONMENT_CONTROLLER_LDN);
moel@7:           ushort address = ReadWord(BASE_ADDRESS_REGISTER);
moel@7:           Thread.Sleep(1);
moel@7:           ushort verify = ReadWord(BASE_ADDRESS_REGISTER);
moel@7: 
moel@7:           IT87Exit();
moel@7: 
moel@34:           if (address != verify || address < 0x100 || (address & 0xF007) != 0)
moel@7:             return;
moel@7: 
moel@16:           IT87XX it87 = new IT87XX(chip, address);
moel@7:           if (it87.IsAvailable)
moel@7:             hardware.Add(it87);
moel@7: 
moel@1:           return;
moel@7:         }
moel@7:       }   
moel@1:     }
moel@1: 
moel@1:     public IHardware[] Hardware {
moel@1:       get {
moel@1:         return hardware.ToArray();
moel@1:       }
moel@1:     }
moel@1: 
moel@1:     public string GetReport() {
moel@1:       return null;
moel@1:     }
moel@1: 
moel@1:     public void Close() { }
moel@1:   }
moel@1: }