Hardware/LPC/W83627DHG.cs
author moel.mich
Sun, 31 Jan 2010 19:30:00 +0000
changeset 13 d32fc5f2e822
parent 7 9523a3322777
child 14 51c2f209da6d
permissions -rw-r--r--
Added support for W83627DHG chips. Changed Core i7 temperature reading. Fixed IT87 temperature reading.
moel@1
     1
using System;
moel@1
     2
using System.Collections.Generic;
moel@1
     3
using System.Drawing;
moel@1
     4
using System.Text;
moel@1
     5
moel@1
     6
namespace OpenHardwareMonitor.Hardware.LPC {
moel@1
     7
  public class W83627DHG : IHardware {
moel@1
     8
moel@1
     9
    private byte revision;
moel@1
    10
moel@1
    11
    private string name;
moel@1
    12
    private Image icon;
moel@1
    13
moel@1
    14
    private bool available = false;
moel@7
    15
    private ushort address;
moel@1
    16
moel@13
    17
    private List<ISensor> active = new List<ISensor>();
moel@13
    18
moel@13
    19
    private Sensor[] temperatures;
moel@13
    20
    private Sensor[] fans;
moel@13
    21
    private Sensor[] voltages;
moel@13
    22
moel@13
    23
    private float[] voltageGains;
moel@13
    24
moel@13
    25
    // Consts 
moel@13
    26
    private const ushort WINBOND_VENDOR_ID = 0x5CA3;
moel@13
    27
    private const byte HIGH_BYTE = 0x80;
moel@13
    28
moel@13
    29
    // Hardware Monitor
moel@13
    30
    private const byte ADDRESS_REGISTER_OFFSET = 0x05;
moel@13
    31
    private const byte DATA_REGISTER_OFFSET = 0x06;
moel@13
    32
moel@13
    33
    // Hardware Monitor Registers
moel@13
    34
    private const byte VOLTAGE_BASE_REG = 0x20;
moel@13
    35
    private const byte BANK_SELECT_REGISTER = 0x04E;
moel@13
    36
    private const byte VENDOR_ID_REGISTER = 0x4F;
moel@13
    37
    private const byte FIRST_BANK_REGISTER = 0x50;
moel@13
    38
    private const byte TEMPERATURE_BASE_REG = 0x50;
moel@13
    39
    private const byte TEMPERATURE_SYS_REG = 0x27;
moel@13
    40
moel@13
    41
    private byte[] FAN_TACHO_REG = new byte[] { 0x28, 0x29, 0x2A, 0x3F, 0x53 };
moel@13
    42
    private byte[] FAN_TACHO_BANK = new byte[] { 0, 0, 0, 0, 5 };    
moel@13
    43
    private string[] FAN_NAME = new string[] 
moel@13
    44
      { "System", "CPU #1", "Auxiliary #1", "CPU #2", "Auxiliary #2" };
moel@13
    45
    private byte[] FAN_BIT_REG = new byte[] { 0x47, 0x4B, 0x4C, 0x59, 0x5D };
moel@13
    46
    private byte[] FAN_DIV_BIT0 = new byte[] { 36, 38, 30, 8, 10 };
moel@13
    47
    private byte[] FAN_DIV_BIT1 = new byte[] { 37, 39, 31, 9, 11 };
moel@13
    48
    private byte[] FAN_DIV_BIT2 = new byte[] { 5, 6, 7, 23, 15 };
moel@13
    49
moel@13
    50
    private byte ReadByte(byte bank, byte register) {  
moel@13
    51
      WinRing0.WriteIoPortByte(
moel@13
    52
         (ushort)(address + ADDRESS_REGISTER_OFFSET), BANK_SELECT_REGISTER);
moel@13
    53
      WinRing0.WriteIoPortByte(
moel@13
    54
         (ushort)(address + DATA_REGISTER_OFFSET), bank);
moel@13
    55
      WinRing0.WriteIoPortByte(
moel@13
    56
         (ushort)(address + ADDRESS_REGISTER_OFFSET), register);
moel@13
    57
      return WinRing0.ReadIoPortByte(
moel@13
    58
        (ushort)(address + DATA_REGISTER_OFFSET));
moel@13
    59
    }    
moel@13
    60
moel@7
    61
    public W83627DHG(byte revision, ushort address) {      
moel@1
    62
      this.revision = revision;
moel@7
    63
      this.address = address;
moel@1
    64
moel@13
    65
      // Check vendor id
moel@13
    66
      ushort vendorId =
moel@13
    67
        (ushort)((ReadByte(HIGH_BYTE, VENDOR_ID_REGISTER) << 8) |
moel@13
    68
           ReadByte(0, VENDOR_ID_REGISTER));
moel@13
    69
      if (vendorId != WINBOND_VENDOR_ID)
moel@13
    70
        return;
moel@13
    71
moel@13
    72
      voltageGains = new float[] { 0.008f, 1, 1, 0.016f, 1, 1, 1, 0.016f };
moel@13
    73
      voltages = new Sensor[3];
moel@13
    74
      voltages[0] = new Sensor("CPU VCore", 0, SensorType.Voltage, this);
moel@13
    75
      voltages[1] = new Sensor("+3.3V", 3, SensorType.Voltage, this);
moel@13
    76
      voltages[2] = new Sensor("Battery", 7, SensorType.Voltage, this);
moel@13
    77
moel@13
    78
      temperatures = new Sensor[3];
moel@13
    79
      temperatures[0] = new Sensor("CPU", 0, SensorType.Temperature, this);
moel@13
    80
      temperatures[1] = new Sensor("Auxiliary", 1, SensorType.Temperature, this);
moel@13
    81
      temperatures[2] = new Sensor("System", 2, SensorType.Temperature, this);
moel@13
    82
moel@13
    83
      fans = new Sensor[FAN_NAME.Length];
moel@13
    84
      for (int i = 0; i < FAN_NAME.Length; i++)
moel@13
    85
        fans[i] = new Sensor(FAN_NAME[i], i, SensorType.Fan, this);
moel@13
    86
moel@1
    87
      this.name = "Winbond W83627DHG";
moel@1
    88
      this.icon = Utilities.EmbeddedResources.GetImage("chip.png");
moel@13
    89
      available = true;
moel@1
    90
    }
moel@1
    91
moel@1
    92
    public bool IsAvailable {
moel@1
    93
      get { return available; }
moel@1
    94
    }
moel@1
    95
moel@1
    96
    public string Name {
moel@1
    97
      get { return name; }
moel@1
    98
    }
moel@1
    99
moel@1
   100
    public string Identifier {
moel@1
   101
      get { return "/lpc/w83627dhg"; }
moel@1
   102
    }
moel@1
   103
moel@1
   104
    public Image Icon {
moel@1
   105
      get { return icon; }
moel@1
   106
    }
moel@1
   107
moel@1
   108
    public ISensor[] Sensors {
moel@13
   109
      get { return active.ToArray(); }
moel@1
   110
    }
moel@1
   111
moel@1
   112
    public string GetReport() {
moel@1
   113
      StringBuilder r = new StringBuilder();
moel@1
   114
moel@1
   115
      r.AppendLine("LPC W83627DHG");
moel@1
   116
      r.AppendLine();
moel@7
   117
      r.Append("Chip revision: 0x"); r.AppendLine(revision.ToString("X"));
moel@7
   118
      r.Append("Base Adress: 0x"); r.AppendLine(address.ToString("X4"));
moel@7
   119
      r.AppendLine();
moel@13
   120
      r.AppendLine("Hardware Monitor Registers");
moel@13
   121
      r.AppendLine();
moel@13
   122
      r.AppendLine("      00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F");
moel@13
   123
      r.AppendLine();
moel@13
   124
      for (int i = 0; i < 0x7; i++) {
moel@13
   125
        r.Append(" "); r.Append((i << 4).ToString("X2")); r.Append("  ");
moel@13
   126
        for (int j = 0; j <= 0xF; j++) {
moel@13
   127
          r.Append(" ");
moel@13
   128
          r.Append(ReadByte(0, (byte)((i << 4) | j)).ToString("X2"));
moel@13
   129
        }
moel@13
   130
        r.AppendLine();
moel@13
   131
      }      
moel@13
   132
      for (int k = 1; k <=5; k++) {
moel@13
   133
        r.AppendLine("Bank " + k);
moel@13
   134
        for (int i = 0x5; i < 0x6; i++) {
moel@13
   135
          r.Append(" "); r.Append((i << 4).ToString("X2")); r.Append("  ");
moel@13
   136
          for (int j = 0; j <= 0xF; j++) {
moel@13
   137
            r.Append(" ");
moel@13
   138
            r.Append(ReadByte((byte)(k), 
moel@13
   139
              (byte)((i << 4) | j)).ToString("X2"));
moel@13
   140
          }
moel@13
   141
          r.AppendLine();
moel@13
   142
        }        
moel@13
   143
      }
moel@13
   144
      r.AppendLine();
moel@1
   145
moel@1
   146
      return r.ToString();
moel@1
   147
    }
moel@1
   148
moel@13
   149
    public void Update() {
moel@13
   150
      foreach (Sensor sensor in voltages) {
moel@13
   151
        if (sensor.Index < 7) {
moel@13
   152
          int value = ReadByte(0, (byte)(VOLTAGE_BASE_REG + sensor.Index));
moel@13
   153
          sensor.Value = voltageGains[sensor.Index] * value;
moel@13
   154
          if (sensor.Value > 0)
moel@13
   155
            ActivateSensor(sensor);
moel@13
   156
          else
moel@13
   157
            DeactivateSensor(sensor);
moel@13
   158
        } else {
moel@13
   159
          // Battery voltage
moel@13
   160
          bool valid = (ReadByte(0, 0x5D) & 0x01) > 0;
moel@13
   161
          if (valid) {
moel@13
   162
            sensor.Value = voltageGains[sensor.Index] * 
moel@13
   163
              ReadByte(5, 0x51);
moel@13
   164
            ActivateSensor(sensor);
moel@13
   165
          } else
moel@13
   166
            DeactivateSensor(sensor);
moel@13
   167
        }
moel@13
   168
      }
moel@1
   169
moel@13
   170
      foreach (Sensor sensor in temperatures) {
moel@13
   171
        int value;
moel@13
   172
        if (sensor.Index < 2) {
moel@13
   173
          value = ReadByte((byte)(sensor.Index + 1), TEMPERATURE_BASE_REG);
moel@13
   174
          value = (value << 1) | ReadByte((byte)(sensor.Index + 1),
moel@13
   175
            (byte)(TEMPERATURE_BASE_REG + 1)) >> 7;
moel@13
   176
        } else {
moel@13
   177
          value = ReadByte(0, TEMPERATURE_SYS_REG) << 1;
moel@13
   178
        }
moel@13
   179
        sensor.Value = value / 2.0f;
moel@13
   180
        if (value < 0x1FE)
moel@13
   181
          ActivateSensor(sensor);
moel@13
   182
        else
moel@13
   183
          DeactivateSensor(sensor);
moel@13
   184
      }
moel@13
   185
moel@13
   186
      long bits = 0;
moel@13
   187
      for (int i = 0; i < FAN_BIT_REG.Length; i++)
moel@13
   188
        bits = (bits << 8) | ReadByte(0, FAN_BIT_REG[i]);
moel@13
   189
      foreach (Sensor sensor in fans) {
moel@13
   190
        int count = ReadByte(FAN_TACHO_BANK[sensor.Index], 
moel@13
   191
          FAN_TACHO_REG[sensor.Index]);
moel@13
   192
        int divisorBits = (int)(
moel@13
   193
          (((bits >> FAN_DIV_BIT2[sensor.Index]) & 1) << 2) |
moel@13
   194
          (((bits >> FAN_DIV_BIT1[sensor.Index]) & 1) << 1) |
moel@13
   195
           ((bits >> FAN_DIV_BIT0[sensor.Index]) & 1));
moel@13
   196
        int divisor = 1 << divisorBits;
moel@13
   197
        sensor.Value = (count < 0xff) ? 1.35e6f / (count * divisor) : 0;
moel@13
   198
        ActivateSensor(sensor);        
moel@13
   199
      }     
moel@13
   200
    }
moel@13
   201
moel@13
   202
    private void ActivateSensor(Sensor sensor) {
moel@13
   203
      if (!active.Contains(sensor)) {
moel@13
   204
        active.Add(sensor);
moel@13
   205
        SensorAdded(sensor);
moel@13
   206
      }
moel@13
   207
    }
moel@13
   208
moel@13
   209
    private void DeactivateSensor(Sensor sensor) {
moel@13
   210
      if (active.Contains(sensor)) {
moel@13
   211
        active.Remove(sensor);
moel@13
   212
        SensorRemoved(sensor);
moel@13
   213
      }
moel@13
   214
    }
moel@13
   215
moel@1
   216
    public event SensorEventHandler SensorAdded;
moel@1
   217
    public event SensorEventHandler SensorRemoved;
moel@1
   218
  }
moel@1
   219
}