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