Hardware/LPC/W83627DHG.cs
author moel.mich
Mon, 01 Feb 2010 22:11:06 +0000
changeset 19 890e8fca7f33
parent 15 0e812fe67bac
child 26 0e01b63e1fdc
permissions -rw-r--r--
Release version 0.1.8. Added support for W83627DHG-P super i/o chips.
moel@14
     1
/*
moel@14
     2
  
moel@14
     3
  Version: MPL 1.1/GPL 2.0/LGPL 2.1
moel@14
     4
moel@14
     5
  The contents of this file are subject to the Mozilla Public License Version
moel@14
     6
  1.1 (the "License"); you may not use this file except in compliance with
moel@14
     7
  the License. You may obtain a copy of the License at
moel@14
     8
 
moel@14
     9
  http://www.mozilla.org/MPL/
moel@14
    10
moel@14
    11
  Software distributed under the License is distributed on an "AS IS" basis,
moel@14
    12
  WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
moel@14
    13
  for the specific language governing rights and limitations under the License.
moel@14
    14
moel@14
    15
  The Original Code is the Open Hardware Monitor code.
moel@14
    16
moel@14
    17
  The Initial Developer of the Original Code is 
moel@14
    18
  Michael Möller <m.moeller@gmx.ch>.
moel@14
    19
  Portions created by the Initial Developer are Copyright (C) 2009-2010
moel@14
    20
  the Initial Developer. All Rights Reserved.
moel@14
    21
moel@14
    22
  Contributor(s):
moel@14
    23
moel@14
    24
  Alternatively, the contents of this file may be used under the terms of
moel@14
    25
  either the GNU General Public License Version 2 or later (the "GPL"), or
moel@14
    26
  the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
moel@14
    27
  in which case the provisions of the GPL or the LGPL are applicable instead
moel@14
    28
  of those above. If you wish to allow use of your version of this file only
moel@14
    29
  under the terms of either the GPL or the LGPL, and not to allow others to
moel@14
    30
  use your version of this file under the terms of the MPL, indicate your
moel@14
    31
  decision by deleting the provisions above and replace them with the notice
moel@14
    32
  and other provisions required by the GPL or the LGPL. If you do not delete
moel@14
    33
  the provisions above, a recipient may use your version of this file under
moel@14
    34
  the terms of any one of the MPL, the GPL or the LGPL.
moel@14
    35
 
moel@14
    36
*/
moel@14
    37
moel@14
    38
using System;
moel@1
    39
using System.Collections.Generic;
moel@1
    40
using System.Drawing;
moel@1
    41
using System.Text;
moel@1
    42
moel@1
    43
namespace OpenHardwareMonitor.Hardware.LPC {
moel@1
    44
  public class W83627DHG : IHardware {
moel@1
    45
moel@19
    46
    private Chip chip;
moel@1
    47
    private byte revision;
moel@1
    48
moel@1
    49
    private string name;
moel@1
    50
    private Image icon;
moel@1
    51
moel@1
    52
    private bool available = false;
moel@7
    53
    private ushort address;
moel@1
    54
moel@13
    55
    private List<ISensor> active = new List<ISensor>();
moel@13
    56
moel@13
    57
    private Sensor[] temperatures;
moel@13
    58
    private Sensor[] fans;
moel@13
    59
    private Sensor[] voltages;
moel@13
    60
moel@13
    61
    private float[] voltageGains;
moel@13
    62
moel@13
    63
    // Consts 
moel@13
    64
    private const ushort WINBOND_VENDOR_ID = 0x5CA3;
moel@13
    65
    private const byte HIGH_BYTE = 0x80;
moel@13
    66
moel@13
    67
    // Hardware Monitor
moel@13
    68
    private const byte ADDRESS_REGISTER_OFFSET = 0x05;
moel@13
    69
    private const byte DATA_REGISTER_OFFSET = 0x06;
moel@13
    70
moel@13
    71
    // Hardware Monitor Registers
moel@13
    72
    private const byte VOLTAGE_BASE_REG = 0x20;
moel@13
    73
    private const byte BANK_SELECT_REGISTER = 0x04E;
moel@13
    74
    private const byte VENDOR_ID_REGISTER = 0x4F;
moel@13
    75
    private const byte FIRST_BANK_REGISTER = 0x50;
moel@13
    76
    private const byte TEMPERATURE_BASE_REG = 0x50;
moel@13
    77
    private const byte TEMPERATURE_SYS_REG = 0x27;
moel@13
    78
moel@13
    79
    private byte[] FAN_TACHO_REG = new byte[] { 0x28, 0x29, 0x2A, 0x3F, 0x53 };
moel@13
    80
    private byte[] FAN_TACHO_BANK = new byte[] { 0, 0, 0, 0, 5 };    
moel@13
    81
    private string[] FAN_NAME = new string[] 
moel@13
    82
      { "System", "CPU #1", "Auxiliary #1", "CPU #2", "Auxiliary #2" };
moel@13
    83
    private byte[] FAN_BIT_REG = new byte[] { 0x47, 0x4B, 0x4C, 0x59, 0x5D };
moel@13
    84
    private byte[] FAN_DIV_BIT0 = new byte[] { 36, 38, 30, 8, 10 };
moel@13
    85
    private byte[] FAN_DIV_BIT1 = new byte[] { 37, 39, 31, 9, 11 };
moel@13
    86
    private byte[] FAN_DIV_BIT2 = new byte[] { 5, 6, 7, 23, 15 };
moel@13
    87
moel@13
    88
    private byte ReadByte(byte bank, byte register) {  
moel@13
    89
      WinRing0.WriteIoPortByte(
moel@13
    90
         (ushort)(address + ADDRESS_REGISTER_OFFSET), BANK_SELECT_REGISTER);
moel@13
    91
      WinRing0.WriteIoPortByte(
moel@13
    92
         (ushort)(address + DATA_REGISTER_OFFSET), bank);
moel@13
    93
      WinRing0.WriteIoPortByte(
moel@13
    94
         (ushort)(address + ADDRESS_REGISTER_OFFSET), register);
moel@13
    95
      return WinRing0.ReadIoPortByte(
moel@13
    96
        (ushort)(address + DATA_REGISTER_OFFSET));
moel@13
    97
    }    
moel@13
    98
moel@19
    99
    public W83627DHG(Chip chip, byte revision, ushort address) {
moel@19
   100
      this.chip = chip;
moel@1
   101
      this.revision = revision;
moel@7
   102
      this.address = address;
moel@1
   103
moel@13
   104
      // Check vendor id
moel@13
   105
      ushort vendorId =
moel@13
   106
        (ushort)((ReadByte(HIGH_BYTE, VENDOR_ID_REGISTER) << 8) |
moel@13
   107
           ReadByte(0, VENDOR_ID_REGISTER));
moel@13
   108
      if (vendorId != WINBOND_VENDOR_ID)
moel@13
   109
        return;
moel@13
   110
moel@13
   111
      voltageGains = new float[] { 0.008f, 1, 1, 0.016f, 1, 1, 1, 0.016f };
moel@13
   112
      voltages = new Sensor[3];
moel@13
   113
      voltages[0] = new Sensor("CPU VCore", 0, SensorType.Voltage, this);
moel@13
   114
      voltages[1] = new Sensor("+3.3V", 3, SensorType.Voltage, this);
moel@13
   115
      voltages[2] = new Sensor("Battery", 7, SensorType.Voltage, this);
moel@13
   116
moel@13
   117
      temperatures = new Sensor[3];
moel@13
   118
      temperatures[0] = new Sensor("CPU", 0, SensorType.Temperature, this);
moel@13
   119
      temperatures[1] = new Sensor("Auxiliary", 1, SensorType.Temperature, this);
moel@13
   120
      temperatures[2] = new Sensor("System", 2, SensorType.Temperature, this);
moel@13
   121
moel@13
   122
      fans = new Sensor[FAN_NAME.Length];
moel@13
   123
      for (int i = 0; i < FAN_NAME.Length; i++)
moel@13
   124
        fans[i] = new Sensor(FAN_NAME[i], i, SensorType.Fan, this);
moel@13
   125
moel@19
   126
      switch (chip) {
moel@19
   127
        case Chip.W83627DHG: name = "Winbond W83627DHG"; break;
moel@19
   128
        case Chip.W83627DHGP: name = "Winbond W83627DHG-P"; break;
moel@19
   129
        default: return;
moel@19
   130
      }
moel@19
   131
moel@1
   132
      this.icon = Utilities.EmbeddedResources.GetImage("chip.png");
moel@13
   133
      available = true;
moel@1
   134
    }
moel@1
   135
moel@1
   136
    public bool IsAvailable {
moel@1
   137
      get { return available; }
moel@1
   138
    }
moel@1
   139
moel@1
   140
    public string Name {
moel@1
   141
      get { return name; }
moel@1
   142
    }
moel@1
   143
moel@1
   144
    public string Identifier {
moel@19
   145
      get { return "/lpc/" + chip.ToString().ToLower(); }
moel@1
   146
    }
moel@1
   147
moel@1
   148
    public Image Icon {
moel@1
   149
      get { return icon; }
moel@1
   150
    }
moel@1
   151
moel@1
   152
    public ISensor[] Sensors {
moel@13
   153
      get { return active.ToArray(); }
moel@1
   154
    }
moel@1
   155
moel@1
   156
    public string GetReport() {
moel@1
   157
      StringBuilder r = new StringBuilder();
moel@1
   158
moel@1
   159
      r.AppendLine("LPC W83627DHG");
moel@1
   160
      r.AppendLine();
moel@19
   161
      r.Append("Chip ID: 0x"); r.AppendLine(chip.ToString("X"));
moel@7
   162
      r.Append("Chip revision: 0x"); r.AppendLine(revision.ToString("X"));
moel@7
   163
      r.Append("Base Adress: 0x"); r.AppendLine(address.ToString("X4"));
moel@7
   164
      r.AppendLine();
moel@13
   165
      r.AppendLine("Hardware Monitor Registers");
moel@13
   166
      r.AppendLine();
moel@13
   167
      r.AppendLine("      00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F");
moel@13
   168
      r.AppendLine();
moel@13
   169
      for (int i = 0; i < 0x7; i++) {
moel@13
   170
        r.Append(" "); r.Append((i << 4).ToString("X2")); r.Append("  ");
moel@13
   171
        for (int j = 0; j <= 0xF; j++) {
moel@13
   172
          r.Append(" ");
moel@13
   173
          r.Append(ReadByte(0, (byte)((i << 4) | j)).ToString("X2"));
moel@13
   174
        }
moel@13
   175
        r.AppendLine();
moel@13
   176
      }      
moel@13
   177
      for (int k = 1; k <=5; k++) {
moel@13
   178
        r.AppendLine("Bank " + k);
moel@13
   179
        for (int i = 0x5; i < 0x6; i++) {
moel@13
   180
          r.Append(" "); r.Append((i << 4).ToString("X2")); r.Append("  ");
moel@13
   181
          for (int j = 0; j <= 0xF; j++) {
moel@13
   182
            r.Append(" ");
moel@13
   183
            r.Append(ReadByte((byte)(k), 
moel@13
   184
              (byte)((i << 4) | j)).ToString("X2"));
moel@13
   185
          }
moel@13
   186
          r.AppendLine();
moel@13
   187
        }        
moel@13
   188
      }
moel@13
   189
      r.AppendLine();
moel@1
   190
moel@1
   191
      return r.ToString();
moel@1
   192
    }
moel@1
   193
moel@13
   194
    public void Update() {
moel@13
   195
      foreach (Sensor sensor in voltages) {
moel@13
   196
        if (sensor.Index < 7) {
moel@13
   197
          int value = ReadByte(0, (byte)(VOLTAGE_BASE_REG + sensor.Index));
moel@13
   198
          sensor.Value = voltageGains[sensor.Index] * value;
moel@13
   199
          if (sensor.Value > 0)
moel@13
   200
            ActivateSensor(sensor);
moel@13
   201
          else
moel@13
   202
            DeactivateSensor(sensor);
moel@13
   203
        } else {
moel@13
   204
          // Battery voltage
moel@13
   205
          bool valid = (ReadByte(0, 0x5D) & 0x01) > 0;
moel@13
   206
          if (valid) {
moel@13
   207
            sensor.Value = voltageGains[sensor.Index] * 
moel@13
   208
              ReadByte(5, 0x51);
moel@13
   209
            ActivateSensor(sensor);
moel@13
   210
          } else
moel@13
   211
            DeactivateSensor(sensor);
moel@13
   212
        }
moel@13
   213
      }
moel@1
   214
moel@13
   215
      foreach (Sensor sensor in temperatures) {
moel@13
   216
        int value;
moel@13
   217
        if (sensor.Index < 2) {
moel@13
   218
          value = ReadByte((byte)(sensor.Index + 1), TEMPERATURE_BASE_REG);
moel@13
   219
          value = (value << 1) | ReadByte((byte)(sensor.Index + 1),
moel@13
   220
            (byte)(TEMPERATURE_BASE_REG + 1)) >> 7;
moel@13
   221
        } else {
moel@13
   222
          value = ReadByte(0, TEMPERATURE_SYS_REG) << 1;
moel@13
   223
        }
moel@13
   224
        sensor.Value = value / 2.0f;
moel@13
   225
        if (value < 0x1FE)
moel@13
   226
          ActivateSensor(sensor);
moel@13
   227
        else
moel@13
   228
          DeactivateSensor(sensor);
moel@13
   229
      }
moel@13
   230
moel@13
   231
      long bits = 0;
moel@13
   232
      for (int i = 0; i < FAN_BIT_REG.Length; i++)
moel@13
   233
        bits = (bits << 8) | ReadByte(0, FAN_BIT_REG[i]);
moel@13
   234
      foreach (Sensor sensor in fans) {
moel@13
   235
        int count = ReadByte(FAN_TACHO_BANK[sensor.Index], 
moel@13
   236
          FAN_TACHO_REG[sensor.Index]);
moel@13
   237
        int divisorBits = (int)(
moel@13
   238
          (((bits >> FAN_DIV_BIT2[sensor.Index]) & 1) << 2) |
moel@13
   239
          (((bits >> FAN_DIV_BIT1[sensor.Index]) & 1) << 1) |
moel@13
   240
           ((bits >> FAN_DIV_BIT0[sensor.Index]) & 1));
moel@13
   241
        int divisor = 1 << divisorBits;
moel@13
   242
        sensor.Value = (count < 0xff) ? 1.35e6f / (count * divisor) : 0;
moel@13
   243
        ActivateSensor(sensor);        
moel@13
   244
      }     
moel@13
   245
    }
moel@13
   246
moel@13
   247
    private void ActivateSensor(Sensor sensor) {
moel@13
   248
      if (!active.Contains(sensor)) {
moel@13
   249
        active.Add(sensor);
moel@15
   250
        if (SensorAdded != null)
moel@15
   251
          SensorAdded(sensor);
moel@13
   252
      }
moel@13
   253
    }
moel@13
   254
moel@13
   255
    private void DeactivateSensor(Sensor sensor) {
moel@13
   256
      if (active.Contains(sensor)) {
moel@13
   257
        active.Remove(sensor);
moel@15
   258
        if (SensorRemoved != null)
moel@15
   259
          SensorRemoved(sensor);
moel@13
   260
      }
moel@13
   261
    }
moel@13
   262
moel@1
   263
    public event SensorEventHandler SensorAdded;
moel@1
   264
    public event SensorEventHandler SensorRemoved;
moel@1
   265
  }
moel@1
   266
}