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@1:     private const ushort REGISTER_PORT = 0x2e;
moel@1:     private const ushort VALUE_PORT = 0x2f;
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@1: 
moel@1:     private static byte ReadByte(byte register) {
moel@1:       WinRing0.WriteIoPortByte(REGISTER_PORT, register);
moel@1:       return WinRing0.ReadIoPortByte(VALUE_PORT);
moel@1:     }
moel@1: 
moel@1:     private static ushort ReadWord(byte register) {
moel@1:       ushort value;
moel@1:       WinRing0.WriteIoPortByte(REGISTER_PORT, register);
moel@1:       value = (ushort)(((ushort)WinRing0.ReadIoPortByte(VALUE_PORT)) << 8);
moel@1:       WinRing0.WriteIoPortByte(REGISTER_PORT, (byte)(register + 1));
moel@1:       value |= (ushort)WinRing0.ReadIoPortByte(VALUE_PORT);
moel@1:       return value;
moel@1:     }
moel@1: 
moel@1:     private static void Select(byte logicalDeviceNumber) {
moel@1:       WinRing0.WriteIoPortByte(REGISTER_PORT, DEVCIE_SELECT_REGISTER);
moel@1:       WinRing0.WriteIoPortByte(VALUE_PORT, logicalDeviceNumber);
moel@1:     }
moel@1: 
moel@1:     // IT87
moel@1:     private const ushort IT8716F_CHIP_ID = 0x8716;
moel@1:     private const ushort IT8718F_CHIP_ID = 0x8718;
moel@1:     private const ushort IT8720F_CHIP_ID = 0x8720;
moel@1:     private const ushort IT8726F_CHIP_ID = 0x8726;
moel@1: 
moel@1:     private const byte IT87_ENVIRONMENT_CONTROLLER_LDN = 0x04;
moel@1:     private const byte IT87_ENVIRONMENT_CONTROLLER_BASE_ADDR_REG = 0x60;
moel@1: 
moel@1:     private static void IT87Enter() {
moel@1:       WinRing0.WriteIoPortByte(REGISTER_PORT, 0x87);
moel@1:       WinRing0.WriteIoPortByte(REGISTER_PORT, 0x01);
moel@1:       WinRing0.WriteIoPortByte(REGISTER_PORT, 0x55);
moel@1:       WinRing0.WriteIoPortByte(REGISTER_PORT, 0x55);
moel@1:     }
moel@1: 
moel@1:     internal static void IT87Exit() {
moel@1:       WinRing0.WriteIoPortByte(REGISTER_PORT, CONFIGURATION_CONTROL_REGISTER);
moel@1:       WinRing0.WriteIoPortByte(VALUE_PORT, 0x02);
moel@1:     }
moel@1: 
moel@1:     // Winbond
moel@1:     private static void WinbondEnter() {
moel@1:       WinRing0.WriteIoPortByte(REGISTER_PORT, 0x87);
moel@1:       WinRing0.WriteIoPortByte(REGISTER_PORT, 0x87);
moel@1:     }
moel@1: 
moel@1:     private static void WinbondExit() {
moel@1:       WinRing0.WriteIoPortByte(REGISTER_PORT, 0xAA);      
moel@1:     }
moel@1: 
moel@1:     public LPCGroup() {
moel@1:       if (!WinRing0.IsAvailable)
moel@1:         return;
moel@1: 
moel@1:       WinbondEnter();
moel@1: 
moel@1:       byte id = ReadByte(CHIP_ID_REGISTER);
moel@1:       byte revision = ReadByte(CHIP_REVISION_REGISTER);
moel@1:       switch (id) {
moel@1:         case 0xA0:
moel@1:           switch (revision & 0xF0) {
moel@1:             case 0x20: chip = Chip.W83627DHG; break;
moel@1:             default: chip = Chip.Unknown; break;
moel@1:           } break;
moel@1:         default: chip = Chip.Unknown; break;
moel@1:       }
moel@1:       if (chip != Chip.Unknown) {
moel@1: 
moel@1:         WinbondExit();
moel@1: 
moel@1:         W83627DHG w83627dhg = new W83627DHG(revision);
moel@1:         if (w83627dhg.IsAvailable)
moel@1:           hardware.Add(w83627dhg);
moel@1:         return;
moel@1:       }
moel@1: 
moel@1:       IT87Enter();
moel@1: 
moel@1:       switch (ReadWord(CHIP_ID_REGISTER)) {
moel@1:         case 0x8716: chip = Chip.IT8716F; break;
moel@1:         case 0x8718: chip = Chip.IT8718F; break;
moel@1:         case 0x8720: chip = Chip.IT8720F; break;
moel@1:         case 0x8726: chip = Chip.IT8726F; break;
moel@1:         default: chip = Chip.Unknown; break;
moel@1:       }
moel@1: 
moel@1:       if (chip != Chip.Unknown) {        
moel@1:         Select(IT87_ENVIRONMENT_CONTROLLER_LDN);
moel@1:         ushort address = ReadWord(IT87_ENVIRONMENT_CONTROLLER_BASE_ADDR_REG);
moel@1:         Thread.Sleep(1);
moel@1:         ushort verify = ReadWord(IT87_ENVIRONMENT_CONTROLLER_BASE_ADDR_REG);
moel@1: 
moel@1:         IT87Exit();
moel@1: 
moel@1:         if (address != verify || address == 0 || (address & 0xF007) != 0)
moel@1:           return;
moel@1: 
moel@1:         IT87 it87 = new IT87(chip, address);
moel@1:         if (it87.IsAvailable)
moel@1:           hardware.Add(it87);
moel@1:         
moel@1:         return;
moel@1:       }                
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: }