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