Hardware/LPC/LPCGroup.cs
author moel.mich
Fri, 29 Jan 2010 21:58:31 +0000
changeset 12 a105b5eee02f
parent 7 9523a3322777
child 13 d32fc5f2e822
permissions -rw-r--r--
Release version 0.1.4. Fixed EntryPointNotFoundException for ADL_Adapter_ID_Get by adding two fallback implementations.
moel@1
     1
/*
moel@1
     2
  
moel@1
     3
  Version: MPL 1.1/GPL 2.0/LGPL 2.1
moel@1
     4
moel@1
     5
  The contents of this file are subject to the Mozilla Public License Version
moel@1
     6
  1.1 (the "License"); you may not use this file except in compliance with
moel@1
     7
  the License. You may obtain a copy of the License at
moel@1
     8
 
moel@1
     9
  http://www.mozilla.org/MPL/
moel@1
    10
moel@1
    11
  Software distributed under the License is distributed on an "AS IS" basis,
moel@1
    12
  WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
moel@1
    13
  for the specific language governing rights and limitations under the License.
moel@1
    14
moel@1
    15
  The Original Code is the Open Hardware Monitor code.
moel@1
    16
moel@1
    17
  The Initial Developer of the Original Code is 
moel@1
    18
  Michael Möller <m.moeller@gmx.ch>.
moel@1
    19
  Portions created by the Initial Developer are Copyright (C) 2009-2010
moel@1
    20
  the Initial Developer. All Rights Reserved.
moel@1
    21
moel@1
    22
  Contributor(s):
moel@1
    23
moel@1
    24
  Alternatively, the contents of this file may be used under the terms of
moel@1
    25
  either the GNU General Public License Version 2 or later (the "GPL"), or
moel@1
    26
  the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
moel@1
    27
  in which case the provisions of the GPL or the LGPL are applicable instead
moel@1
    28
  of those above. If you wish to allow use of your version of this file only
moel@1
    29
  under the terms of either the GPL or the LGPL, and not to allow others to
moel@1
    30
  use your version of this file under the terms of the MPL, indicate your
moel@1
    31
  decision by deleting the provisions above and replace them with the notice
moel@1
    32
  and other provisions required by the GPL or the LGPL. If you do not delete
moel@1
    33
  the provisions above, a recipient may use your version of this file under
moel@1
    34
  the terms of any one of the MPL, the GPL or the LGPL.
moel@1
    35
 
moel@1
    36
*/
moel@1
    37
moel@1
    38
using System;
moel@1
    39
using System.Collections.Generic;
moel@1
    40
using System.Text;
moel@1
    41
using System.Threading;
moel@1
    42
moel@1
    43
namespace OpenHardwareMonitor.Hardware.LPC {
moel@1
    44
  public class LPCGroup : IGroup {
moel@1
    45
    private List<IHardware> hardware = new List<IHardware>();
moel@1
    46
moel@1
    47
    private Chip chip = Chip.Unknown;
moel@1
    48
moel@1
    49
    // I/O Ports
moel@7
    50
    private ushort[] REGISTER_PORTS = new ushort[] { 0x2e, 0x4e };
moel@7
    51
    private ushort[] VALUE_PORTS = new ushort[] { 0x2f, 0x4f };
moel@7
    52
moel@7
    53
    private ushort registerPort;
moel@7
    54
    private ushort valuePort;
moel@1
    55
moel@1
    56
    // Registers
moel@1
    57
    private const byte CONFIGURATION_CONTROL_REGISTER = 0x02;
moel@1
    58
    private const byte DEVCIE_SELECT_REGISTER = 0x07;
moel@1
    59
    private const byte CHIP_ID_REGISTER = 0x20;
moel@1
    60
    private const byte CHIP_REVISION_REGISTER = 0x21;
moel@7
    61
    private const byte BASE_ADDRESS_REGISTER = 0x60;
moel@1
    62
moel@7
    63
    private byte ReadByte(byte register) {
moel@7
    64
      WinRing0.WriteIoPortByte(registerPort, register);
moel@7
    65
      return WinRing0.ReadIoPortByte(valuePort);
moel@1
    66
    }
moel@1
    67
moel@7
    68
    private ushort ReadWord(byte register) {
moel@7
    69
      return (ushort)((ReadByte(register) << 8) | 
moel@7
    70
        ReadByte((byte)(register + 1)));
moel@1
    71
    }
moel@1
    72
moel@7
    73
    private void Select(byte logicalDeviceNumber) {
moel@7
    74
      WinRing0.WriteIoPortByte(registerPort, DEVCIE_SELECT_REGISTER);
moel@7
    75
      WinRing0.WriteIoPortByte(valuePort, logicalDeviceNumber);
moel@1
    76
    }
moel@1
    77
moel@1
    78
    // IT87
moel@1
    79
    private const ushort IT8716F_CHIP_ID = 0x8716;
moel@1
    80
    private const ushort IT8718F_CHIP_ID = 0x8718;
moel@1
    81
    private const ushort IT8720F_CHIP_ID = 0x8720;
moel@1
    82
    private const ushort IT8726F_CHIP_ID = 0x8726;
moel@1
    83
moel@7
    84
    private const byte IT87_ENVIRONMENT_CONTROLLER_LDN = 0x04;    
moel@1
    85
moel@7
    86
    private void IT87Enter() {
moel@7
    87
      WinRing0.WriteIoPortByte(registerPort, 0x87);
moel@7
    88
      WinRing0.WriteIoPortByte(registerPort, 0x01);
moel@7
    89
      WinRing0.WriteIoPortByte(registerPort, 0x55);
moel@7
    90
      WinRing0.WriteIoPortByte(registerPort, 0x55);
moel@1
    91
    }
moel@1
    92
moel@7
    93
    internal void IT87Exit() {
moel@7
    94
      WinRing0.WriteIoPortByte(registerPort, CONFIGURATION_CONTROL_REGISTER);
moel@7
    95
      WinRing0.WriteIoPortByte(valuePort, 0x02);
moel@1
    96
    }
moel@1
    97
moel@7
    98
    // Winbond, Fintek
moel@7
    99
    private const byte FINTEK_VENDOR_ID_REGISTER = 0x23;
moel@7
   100
    private const ushort FINTEK_VENDOR_ID = 0x1934;
moel@7
   101
moel@7
   102
    private const byte W83627DHG_HARDWARE_MONITOR_LDN = 0x0B;
moel@7
   103
    private const byte F71882FG_HARDWARE_MONITOR_LDN = 0x04;
moel@7
   104
moel@7
   105
    private void WinbondFintekEnter() {
moel@7
   106
      WinRing0.WriteIoPortByte(registerPort, 0x87);
moel@7
   107
      WinRing0.WriteIoPortByte(registerPort, 0x87);
moel@1
   108
    }
moel@1
   109
moel@7
   110
    private void WinbondFintekExit() {
moel@7
   111
      WinRing0.WriteIoPortByte(registerPort, 0xAA);      
moel@1
   112
    }
moel@1
   113
moel@1
   114
    public LPCGroup() {
moel@1
   115
      if (!WinRing0.IsAvailable)
moel@1
   116
        return;
moel@1
   117
moel@7
   118
      for (int i = 0; i < REGISTER_PORTS.Length; i++) {
moel@7
   119
        registerPort = REGISTER_PORTS[i];
moel@7
   120
        valuePort = VALUE_PORTS[i];
moel@1
   121
moel@7
   122
        WinbondFintekEnter();
moel@1
   123
moel@7
   124
        byte hardwareMonitorLDN;
moel@7
   125
        byte id = ReadByte(CHIP_ID_REGISTER);
moel@7
   126
        byte revision = ReadByte(CHIP_REVISION_REGISTER);
moel@7
   127
        switch (id) {
moel@7
   128
          case 0xA0:
moel@7
   129
            switch (revision & 0xF0) {
moel@7
   130
              case 0x20: 
moel@7
   131
                chip = Chip.W83627DHG;
moel@7
   132
                hardwareMonitorLDN = W83627DHG_HARDWARE_MONITOR_LDN;  
moel@7
   133
                break;
moel@7
   134
              default: 
moel@7
   135
                chip = Chip.Unknown;
moel@7
   136
                hardwareMonitorLDN = 0;
moel@7
   137
                break;
moel@7
   138
            } break;
moel@7
   139
          case 0x05:
moel@7
   140
            switch (revision) {
moel@7
   141
              case 0x41: 
moel@7
   142
                chip = Chip.F71882FG;
moel@7
   143
                hardwareMonitorLDN = F71882FG_HARDWARE_MONITOR_LDN; 
moel@7
   144
                break;
moel@7
   145
              default: 
moel@7
   146
                chip = Chip.Unknown; 
moel@7
   147
                hardwareMonitorLDN = 0;
moel@7
   148
                break;
moel@7
   149
            } break;
moel@7
   150
          default:
moel@7
   151
            chip = Chip.Unknown; 
moel@7
   152
            hardwareMonitorLDN = 0;
moel@7
   153
            break;
moel@7
   154
        }
moel@7
   155
        if (chip != Chip.Unknown) {
moel@1
   156
moel@7
   157
          Select(hardwareMonitorLDN);
moel@7
   158
          ushort address = ReadWord(BASE_ADDRESS_REGISTER);
moel@7
   159
          Thread.Sleep(1);
moel@7
   160
          ushort verify = ReadWord(BASE_ADDRESS_REGISTER);
moel@1
   161
moel@7
   162
          ushort vendorID = 0;
moel@7
   163
          if (chip == Chip.F71882FG)
moel@7
   164
            vendorID = ReadWord(FINTEK_VENDOR_ID_REGISTER);
moel@1
   165
moel@7
   166
          WinbondFintekExit();
moel@1
   167
moel@7
   168
          if (address != verify || address == 0 || (address & 0xF007) != 0)
moel@7
   169
            return;
moel@7
   170
          
moel@7
   171
          switch (chip) {
moel@7
   172
            case Chip.W83627DHG:
moel@7
   173
              W83627DHG w83627dhg = new W83627DHG(revision, address);
moel@7
   174
              if (w83627dhg.IsAvailable)
moel@7
   175
                hardware.Add(w83627dhg);
moel@7
   176
              break;
moel@7
   177
            case Chip.F71882FG:  
moel@7
   178
              if (vendorID == FINTEK_VENDOR_ID)
moel@9
   179
                hardware.Add(new F71882(address));
moel@7
   180
              break;
moel@7
   181
            default: break;
moel@7
   182
          }
moel@7
   183
          
moel@7
   184
          return;
moel@7
   185
        }
moel@1
   186
moel@7
   187
        IT87Enter();
moel@1
   188
moel@7
   189
        switch (ReadWord(CHIP_ID_REGISTER)) {
moel@7
   190
          case 0x8716: chip = Chip.IT8716F; break;
moel@7
   191
          case 0x8718: chip = Chip.IT8718F; break;
moel@7
   192
          case 0x8720: chip = Chip.IT8720F; break;
moel@7
   193
          case 0x8726: chip = Chip.IT8726F; break;
moel@7
   194
          default: chip = Chip.Unknown; break;
moel@7
   195
        }
moel@7
   196
moel@7
   197
        if (chip != Chip.Unknown) {
moel@7
   198
          Select(IT87_ENVIRONMENT_CONTROLLER_LDN);
moel@7
   199
          ushort address = ReadWord(BASE_ADDRESS_REGISTER);
moel@7
   200
          Thread.Sleep(1);
moel@7
   201
          ushort verify = ReadWord(BASE_ADDRESS_REGISTER);
moel@7
   202
moel@7
   203
          IT87Exit();
moel@7
   204
moel@7
   205
          if (address != verify || address == 0 || (address & 0xF007) != 0)
moel@7
   206
            return;
moel@7
   207
moel@7
   208
          IT87 it87 = new IT87(chip, address);
moel@7
   209
          if (it87.IsAvailable)
moel@7
   210
            hardware.Add(it87);
moel@7
   211
moel@1
   212
          return;
moel@7
   213
        }
moel@7
   214
      }   
moel@1
   215
    }
moel@1
   216
moel@1
   217
    public IHardware[] Hardware {
moel@1
   218
      get {
moel@1
   219
        return hardware.ToArray();
moel@1
   220
      }
moel@1
   221
    }
moel@1
   222
moel@1
   223
    public string GetReport() {
moel@1
   224
      return null;
moel@1
   225
    }
moel@1
   226
moel@1
   227
    public void Close() { }
moel@1
   228
  }
moel@1
   229
}