Hardware/LPC/LPCGroup.cs
changeset 1 361e324a0ed4
child 7 9523a3322777
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/Hardware/LPC/LPCGroup.cs	Tue Jan 26 22:37:48 2010 +0000
     1.3 @@ -0,0 +1,176 @@
     1.4 +/*
     1.5 +  
     1.6 +  Version: MPL 1.1/GPL 2.0/LGPL 2.1
     1.7 +
     1.8 +  The contents of this file are subject to the Mozilla Public License Version
     1.9 +  1.1 (the "License"); you may not use this file except in compliance with
    1.10 +  the License. You may obtain a copy of the License at
    1.11 + 
    1.12 +  http://www.mozilla.org/MPL/
    1.13 +
    1.14 +  Software distributed under the License is distributed on an "AS IS" basis,
    1.15 +  WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
    1.16 +  for the specific language governing rights and limitations under the License.
    1.17 +
    1.18 +  The Original Code is the Open Hardware Monitor code.
    1.19 +
    1.20 +  The Initial Developer of the Original Code is 
    1.21 +  Michael Möller <m.moeller@gmx.ch>.
    1.22 +  Portions created by the Initial Developer are Copyright (C) 2009-2010
    1.23 +  the Initial Developer. All Rights Reserved.
    1.24 +
    1.25 +  Contributor(s):
    1.26 +
    1.27 +  Alternatively, the contents of this file may be used under the terms of
    1.28 +  either the GNU General Public License Version 2 or later (the "GPL"), or
    1.29 +  the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
    1.30 +  in which case the provisions of the GPL or the LGPL are applicable instead
    1.31 +  of those above. If you wish to allow use of your version of this file only
    1.32 +  under the terms of either the GPL or the LGPL, and not to allow others to
    1.33 +  use your version of this file under the terms of the MPL, indicate your
    1.34 +  decision by deleting the provisions above and replace them with the notice
    1.35 +  and other provisions required by the GPL or the LGPL. If you do not delete
    1.36 +  the provisions above, a recipient may use your version of this file under
    1.37 +  the terms of any one of the MPL, the GPL or the LGPL.
    1.38 + 
    1.39 +*/
    1.40 +
    1.41 +using System;
    1.42 +using System.Collections.Generic;
    1.43 +using System.Text;
    1.44 +using System.Threading;
    1.45 +
    1.46 +namespace OpenHardwareMonitor.Hardware.LPC {
    1.47 +  public class LPCGroup : IGroup {
    1.48 +    private List<IHardware> hardware = new List<IHardware>();
    1.49 +
    1.50 +    private Chip chip = Chip.Unknown;
    1.51 +
    1.52 +    // I/O Ports
    1.53 +    private const ushort REGISTER_PORT = 0x2e;
    1.54 +    private const ushort VALUE_PORT = 0x2f;
    1.55 +
    1.56 +    // Registers
    1.57 +    private const byte CONFIGURATION_CONTROL_REGISTER = 0x02;
    1.58 +    private const byte DEVCIE_SELECT_REGISTER = 0x07;
    1.59 +    private const byte CHIP_ID_REGISTER = 0x20;
    1.60 +    private const byte CHIP_REVISION_REGISTER = 0x21;
    1.61 +
    1.62 +    private static byte ReadByte(byte register) {
    1.63 +      WinRing0.WriteIoPortByte(REGISTER_PORT, register);
    1.64 +      return WinRing0.ReadIoPortByte(VALUE_PORT);
    1.65 +    }
    1.66 +
    1.67 +    private static ushort ReadWord(byte register) {
    1.68 +      ushort value;
    1.69 +      WinRing0.WriteIoPortByte(REGISTER_PORT, register);
    1.70 +      value = (ushort)(((ushort)WinRing0.ReadIoPortByte(VALUE_PORT)) << 8);
    1.71 +      WinRing0.WriteIoPortByte(REGISTER_PORT, (byte)(register + 1));
    1.72 +      value |= (ushort)WinRing0.ReadIoPortByte(VALUE_PORT);
    1.73 +      return value;
    1.74 +    }
    1.75 +
    1.76 +    private static void Select(byte logicalDeviceNumber) {
    1.77 +      WinRing0.WriteIoPortByte(REGISTER_PORT, DEVCIE_SELECT_REGISTER);
    1.78 +      WinRing0.WriteIoPortByte(VALUE_PORT, logicalDeviceNumber);
    1.79 +    }
    1.80 +
    1.81 +    // IT87
    1.82 +    private const ushort IT8716F_CHIP_ID = 0x8716;
    1.83 +    private const ushort IT8718F_CHIP_ID = 0x8718;
    1.84 +    private const ushort IT8720F_CHIP_ID = 0x8720;
    1.85 +    private const ushort IT8726F_CHIP_ID = 0x8726;
    1.86 +
    1.87 +    private const byte IT87_ENVIRONMENT_CONTROLLER_LDN = 0x04;
    1.88 +    private const byte IT87_ENVIRONMENT_CONTROLLER_BASE_ADDR_REG = 0x60;
    1.89 +
    1.90 +    private static void IT87Enter() {
    1.91 +      WinRing0.WriteIoPortByte(REGISTER_PORT, 0x87);
    1.92 +      WinRing0.WriteIoPortByte(REGISTER_PORT, 0x01);
    1.93 +      WinRing0.WriteIoPortByte(REGISTER_PORT, 0x55);
    1.94 +      WinRing0.WriteIoPortByte(REGISTER_PORT, 0x55);
    1.95 +    }
    1.96 +
    1.97 +    internal static void IT87Exit() {
    1.98 +      WinRing0.WriteIoPortByte(REGISTER_PORT, CONFIGURATION_CONTROL_REGISTER);
    1.99 +      WinRing0.WriteIoPortByte(VALUE_PORT, 0x02);
   1.100 +    }
   1.101 +
   1.102 +    // Winbond
   1.103 +    private static void WinbondEnter() {
   1.104 +      WinRing0.WriteIoPortByte(REGISTER_PORT, 0x87);
   1.105 +      WinRing0.WriteIoPortByte(REGISTER_PORT, 0x87);
   1.106 +    }
   1.107 +
   1.108 +    private static void WinbondExit() {
   1.109 +      WinRing0.WriteIoPortByte(REGISTER_PORT, 0xAA);      
   1.110 +    }
   1.111 +
   1.112 +    public LPCGroup() {
   1.113 +      if (!WinRing0.IsAvailable)
   1.114 +        return;
   1.115 +
   1.116 +      WinbondEnter();
   1.117 +
   1.118 +      byte id = ReadByte(CHIP_ID_REGISTER);
   1.119 +      byte revision = ReadByte(CHIP_REVISION_REGISTER);
   1.120 +      switch (id) {
   1.121 +        case 0xA0:
   1.122 +          switch (revision & 0xF0) {
   1.123 +            case 0x20: chip = Chip.W83627DHG; break;
   1.124 +            default: chip = Chip.Unknown; break;
   1.125 +          } break;
   1.126 +        default: chip = Chip.Unknown; break;
   1.127 +      }
   1.128 +      if (chip != Chip.Unknown) {
   1.129 +
   1.130 +        WinbondExit();
   1.131 +
   1.132 +        W83627DHG w83627dhg = new W83627DHG(revision);
   1.133 +        if (w83627dhg.IsAvailable)
   1.134 +          hardware.Add(w83627dhg);
   1.135 +        return;
   1.136 +      }
   1.137 +
   1.138 +      IT87Enter();
   1.139 +
   1.140 +      switch (ReadWord(CHIP_ID_REGISTER)) {
   1.141 +        case 0x8716: chip = Chip.IT8716F; break;
   1.142 +        case 0x8718: chip = Chip.IT8718F; break;
   1.143 +        case 0x8720: chip = Chip.IT8720F; break;
   1.144 +        case 0x8726: chip = Chip.IT8726F; break;
   1.145 +        default: chip = Chip.Unknown; break;
   1.146 +      }
   1.147 +
   1.148 +      if (chip != Chip.Unknown) {        
   1.149 +        Select(IT87_ENVIRONMENT_CONTROLLER_LDN);
   1.150 +        ushort address = ReadWord(IT87_ENVIRONMENT_CONTROLLER_BASE_ADDR_REG);
   1.151 +        Thread.Sleep(1);
   1.152 +        ushort verify = ReadWord(IT87_ENVIRONMENT_CONTROLLER_BASE_ADDR_REG);
   1.153 +
   1.154 +        IT87Exit();
   1.155 +
   1.156 +        if (address != verify || address == 0 || (address & 0xF007) != 0)
   1.157 +          return;
   1.158 +
   1.159 +        IT87 it87 = new IT87(chip, address);
   1.160 +        if (it87.IsAvailable)
   1.161 +          hardware.Add(it87);
   1.162 +        
   1.163 +        return;
   1.164 +      }                
   1.165 +    }
   1.166 +
   1.167 +    public IHardware[] Hardware {
   1.168 +      get {
   1.169 +        return hardware.ToArray();
   1.170 +      }
   1.171 +    }
   1.172 +
   1.173 +    public string GetReport() {
   1.174 +      return null;
   1.175 +    }
   1.176 +
   1.177 +    public void Close() { }
   1.178 +  }
   1.179 +}