Hardware/LPC/W836XX.cs
author moel.mich
Wed, 04 Aug 2010 20:27:05 +0000
changeset 162 2129ccee0bd1
parent 152 98187e7a08b2
child 163 67be1c62f950
permissions -rw-r--r--
Added an ISA bus mutex.
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.Text;
moel@34
    41
moel@34
    42
namespace OpenHardwareMonitor.Hardware.LPC {
moel@130
    43
  public class W836XX : ISuperIO {
moel@34
    44
moel@34
    45
    private ushort address;
moel@34
    46
    private byte revision;
moel@34
    47
moel@130
    48
    private Chip chip;
moel@34
    49
moel@130
    50
    private float?[] voltages = new float?[0];
moel@130
    51
    private float?[] temperatures = new float?[0];    
moel@130
    52
    private float?[] fans = new float?[0];
moel@34
    53
moel@130
    54
    private bool[] peciTemperature = new bool[0];
moel@130
    55
    private byte[] voltageRegister = new byte[0];
moel@130
    56
    private byte[] voltageBank = new byte[0];
moel@130
    57
    private float voltageGain = 0.008f;
moel@34
    58
moel@34
    59
    // Consts 
moel@34
    60
    private const ushort WINBOND_VENDOR_ID = 0x5CA3;
moel@34
    61
    private const byte HIGH_BYTE = 0x80;
moel@34
    62
moel@34
    63
    // Hardware Monitor
moel@34
    64
    private const byte ADDRESS_REGISTER_OFFSET = 0x05;
moel@34
    65
    private const byte DATA_REGISTER_OFFSET = 0x06;
moel@34
    66
moel@34
    67
    // Hardware Monitor Registers
moel@130
    68
    private const byte VOLTAGE_VBAT_REG = 0x51;
moel@54
    69
    private const byte BANK_SELECT_REGISTER = 0x4E;
moel@34
    70
    private const byte VENDOR_ID_REGISTER = 0x4F;
moel@56
    71
    private const byte TEMPERATURE_SOURCE_SELECT_REG = 0x49;
moel@56
    72
moel@56
    73
    private byte[] TEMPERATURE_REG = new byte[] { 0x50, 0x50, 0x27 };
moel@56
    74
    private byte[] TEMPERATURE_BANK = new byte[] { 1, 2, 0 };
moel@34
    75
moel@34
    76
    private byte[] FAN_TACHO_REG = new byte[] { 0x28, 0x29, 0x2A, 0x3F, 0x53 };
moel@34
    77
    private byte[] FAN_TACHO_BANK = new byte[] { 0, 0, 0, 0, 5 };       
moel@34
    78
    private byte[] FAN_BIT_REG = new byte[] { 0x47, 0x4B, 0x4C, 0x59, 0x5D };
moel@34
    79
    private byte[] FAN_DIV_BIT0 = new byte[] { 36, 38, 30, 8, 10 };
moel@34
    80
    private byte[] FAN_DIV_BIT1 = new byte[] { 37, 39, 31, 9, 11 };
moel@34
    81
    private byte[] FAN_DIV_BIT2 = new byte[] { 5, 6, 7, 23, 15 };
moel@34
    82
moel@34
    83
    private byte ReadByte(byte bank, byte register) {
moel@34
    84
      WinRing0.WriteIoPortByte(
moel@34
    85
         (ushort)(address + ADDRESS_REGISTER_OFFSET), BANK_SELECT_REGISTER);
moel@34
    86
      WinRing0.WriteIoPortByte(
moel@34
    87
         (ushort)(address + DATA_REGISTER_OFFSET), bank);
moel@34
    88
      WinRing0.WriteIoPortByte(
moel@34
    89
         (ushort)(address + ADDRESS_REGISTER_OFFSET), register);
moel@34
    90
      return WinRing0.ReadIoPortByte(
moel@34
    91
        (ushort)(address + DATA_REGISTER_OFFSET));
moel@130
    92
    } 
moel@34
    93
moel@56
    94
    private void WriteByte(byte bank, byte register, byte value) {
moel@56
    95
      WinRing0.WriteIoPortByte(
moel@56
    96
         (ushort)(address + ADDRESS_REGISTER_OFFSET), BANK_SELECT_REGISTER);
moel@56
    97
      WinRing0.WriteIoPortByte(
moel@56
    98
         (ushort)(address + DATA_REGISTER_OFFSET), bank);
moel@56
    99
      WinRing0.WriteIoPortByte(
moel@56
   100
         (ushort)(address + ADDRESS_REGISTER_OFFSET), register);
moel@56
   101
      WinRing0.WriteIoPortByte(
moel@56
   102
         (ushort)(address + DATA_REGISTER_OFFSET), value); 
moel@56
   103
    }
moel@56
   104
   
moel@130
   105
    public W836XX(Chip chip, byte revision, ushort address) {
moel@34
   106
      this.address = address;
moel@34
   107
      this.revision = revision;
moel@130
   108
      this.chip = chip;
moel@34
   109
moel@130
   110
      if (!IsWinbondVendor())
moel@130
   111
        return;
moel@130
   112
      
moel@130
   113
      temperatures = new float?[3];
moel@130
   114
      peciTemperature = new bool[3];
moel@56
   115
      switch (chip) {
moel@63
   116
        case Chip.W83667HG:
moel@63
   117
        case Chip.W83667HGB:
moel@130
   118
          // note temperature sensor registers that read PECI
moel@63
   119
          byte flag = ReadByte(0, TEMPERATURE_SOURCE_SELECT_REG);
moel@130
   120
          peciTemperature[0] = (flag & 0x04) != 0;
moel@130
   121
          peciTemperature[1] = (flag & 0x40) != 0;
moel@130
   122
          peciTemperature[2] = false;
moel@63
   123
          break;
moel@63
   124
        case Chip.W83627DHG:        
moel@63
   125
        case Chip.W83627DHGP:
moel@152
   126
          // note temperature sensor registers that read PECI
moel@56
   127
          byte sel = ReadByte(0, TEMPERATURE_SOURCE_SELECT_REG);
moel@130
   128
          peciTemperature[0] = (sel & 0x07) != 0;
moel@130
   129
          peciTemperature[1] = (sel & 0x70) != 0;
moel@130
   130
          peciTemperature[2] = false;
moel@56
   131
          break;
moel@56
   132
        default:
moel@130
   133
          // no PECI support
moel@130
   134
          peciTemperature[0] = false;
moel@130
   135
          peciTemperature[1] = false;
moel@130
   136
          peciTemperature[2] = false;
moel@56
   137
          break;
moel@56
   138
      }
moel@34
   139
moel@34
   140
      switch (chip) {
moel@130
   141
        case Chip.W83627EHF:
moel@130
   142
          voltages = new float?[10];
moel@130
   143
          voltageRegister = new byte[] { 
moel@130
   144
            0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x50, 0x51, 0x52 };
moel@130
   145
          voltageBank = new byte[] { 0, 0, 0, 0, 0, 0, 0, 5, 5, 5 };
moel@130
   146
          voltageGain = 0.008f;
moel@130
   147
          fans = new float?[5];
moel@130
   148
          break;
moel@34
   149
        case Chip.W83627DHG:
moel@130
   150
        case Chip.W83627DHGP:        
moel@34
   151
        case Chip.W83667HG:
moel@130
   152
        case Chip.W83667HGB:
moel@130
   153
          voltages = new float?[9];
moel@130
   154
          voltageRegister = new byte[] { 
moel@130
   155
            0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x50, 0x51 };
moel@130
   156
          voltageBank = new byte[] { 0, 0, 0, 0, 0, 0, 0, 5, 5 };
moel@130
   157
          voltageGain = 0.008f;
moel@130
   158
          fans = new float?[5];
moel@34
   159
          break;
moel@54
   160
        case Chip.W83627HF:
moel@54
   161
        case Chip.W83627THF:
moel@67
   162
        case Chip.W83687THF:
moel@130
   163
          voltages = new float?[7];
moel@130
   164
          voltageRegister = new byte[] { 
moel@130
   165
            0x20, 0x21, 0x22, 0x23, 0x24, 0x50, 0x51 };
moel@130
   166
          voltageBank = new byte[] { 0, 0, 0, 0, 0, 5, 5 };
moel@130
   167
          voltageGain = 0.016f;
moel@130
   168
          fans = new float?[3];         
moel@34
   169
          break;
moel@34
   170
      }
moel@130
   171
    }    
moel@34
   172
moel@34
   173
    private bool IsWinbondVendor() {
moel@34
   174
      ushort vendorId =
moel@34
   175
        (ushort)((ReadByte(HIGH_BYTE, VENDOR_ID_REGISTER) << 8) |
moel@34
   176
           ReadByte(0, VENDOR_ID_REGISTER));
moel@34
   177
      return vendorId == WINBOND_VENDOR_ID;
moel@34
   178
    }
moel@34
   179
moel@85
   180
    private ulong SetBit(ulong target, int bit, int value) {
moel@85
   181
      if ((value & 1) != value)
moel@85
   182
        throw new ArgumentException("Value must be one bit only.");
moel@85
   183
moel@85
   184
      if (bit < 0 || bit > 63)
moel@85
   185
        throw new ArgumentException("Bit out of range.");
moel@85
   186
moel@85
   187
      ulong mask = (((ulong)1) << bit);
moel@85
   188
      return value > 0 ? target | mask : target & ~mask;
moel@85
   189
    }
moel@85
   190
moel@130
   191
    public Chip Chip { get { return chip; } }
moel@130
   192
    public float?[] Voltages { get { return voltages; } }
moel@130
   193
    public float?[] Temperatures { get { return temperatures; } }
moel@130
   194
    public float?[] Fans { get { return fans; } }
moel@56
   195
moel@130
   196
    public void Update() {
moel@162
   197
      if (!WinRing0.WaitIsaBusMutex())
moel@162
   198
        return;
moel@130
   199
moel@130
   200
      for (int i = 0; i < voltages.Length; i++) {
moel@130
   201
        if (voltageRegister[i] != VOLTAGE_VBAT_REG) {
moel@58
   202
          // two special VCore measurement modes for W83627THF
moel@130
   203
          float fvalue;
moel@67
   204
          if ((chip == Chip.W83627HF || chip == Chip.W83627THF || 
moel@130
   205
            chip == Chip.W83687THF) && i == 0) 
moel@67
   206
          {
moel@58
   207
            byte vrmConfiguration = ReadByte(0, 0x18);
moel@130
   208
            int value = ReadByte(voltageBank[i], voltageRegister[i]);
moel@58
   209
            if ((vrmConfiguration & 0x01) == 0)
moel@130
   210
              fvalue = 0.016f * value; // VRM8 formula
moel@58
   211
            else
moel@130
   212
              fvalue = 0.00488f * value + 0.69f; // VRM9 formula
moel@58
   213
          } else {
moel@130
   214
            int value = ReadByte(voltageBank[i], voltageRegister[i]);
moel@130
   215
            fvalue = voltageGain * value;
moel@58
   216
          }
moel@130
   217
          if (fvalue > 0)
moel@130
   218
            voltages[i] = fvalue;
moel@130
   219
          else
moel@130
   220
            voltages[i] = null;
moel@34
   221
        } else {
moel@34
   222
          // Battery voltage
moel@34
   223
          bool valid = (ReadByte(0, 0x5D) & 0x01) > 0;
moel@34
   224
          if (valid) {
moel@130
   225
            voltages[i] = voltageGain * ReadByte(5, VOLTAGE_VBAT_REG);
moel@84
   226
          } else {
moel@130
   227
            voltages[i] = null;
moel@84
   228
          }
moel@34
   229
        }
moel@34
   230
      }
moel@34
   231
moel@130
   232
      for (int i = 0; i < temperatures.Length; i++) {
moel@130
   233
        int value = ((sbyte)ReadByte(TEMPERATURE_BANK[i], 
moel@130
   234
          TEMPERATURE_REG[i])) << 1;
moel@130
   235
        if (TEMPERATURE_BANK[i] > 0) 
moel@130
   236
          value |= ReadByte(TEMPERATURE_BANK[i],
moel@130
   237
            (byte)(TEMPERATURE_REG[i] + 1)) >> 7;
moel@63
   238
moel@56
   239
        float temperature = value / 2.0f;
moel@130
   240
        if (temperature <= 125 && temperature >= -55 && !peciTemperature[i]) {
moel@130
   241
          temperatures[i] = temperature;
moel@34
   242
        } else {
moel@130
   243
          temperatures[i] = null;
moel@34
   244
        }
moel@34
   245
      }
moel@34
   246
moel@85
   247
      ulong bits = 0;
moel@34
   248
      for (int i = 0; i < FAN_BIT_REG.Length; i++)
moel@34
   249
        bits = (bits << 8) | ReadByte(0, FAN_BIT_REG[i]);
moel@85
   250
      ulong newBits = bits;
moel@130
   251
      for (int i = 0; i < fans.Length; i++) {
moel@130
   252
        int count = ReadByte(FAN_TACHO_BANK[i], FAN_TACHO_REG[i]);
moel@85
   253
        
moel@85
   254
        // assemble fan divisor
moel@34
   255
        int divisorBits = (int)(
moel@130
   256
          (((bits >> FAN_DIV_BIT2[i]) & 1) << 2) |
moel@130
   257
          (((bits >> FAN_DIV_BIT1[i]) & 1) << 1) |
moel@130
   258
           ((bits >> FAN_DIV_BIT0[i]) & 1));
moel@34
   259
        int divisor = 1 << divisorBits;
moel@85
   260
       
moel@34
   261
        float value = (count < 0xff) ? 1.35e6f / (count * divisor) : 0;
moel@130
   262
        fans[i] = value;
moel@85
   263
moel@85
   264
        // update fan divisor
moel@85
   265
        if (count > 192 && divisorBits < 7) 
moel@85
   266
          divisorBits++;
moel@85
   267
        if (count < 96 && divisorBits > 0)
moel@85
   268
          divisorBits--;
moel@85
   269
moel@130
   270
        newBits = SetBit(newBits, FAN_DIV_BIT2[i], (divisorBits >> 2) & 1);
moel@130
   271
        newBits = SetBit(newBits, FAN_DIV_BIT1[i], (divisorBits >> 1) & 1);
moel@130
   272
        newBits = SetBit(newBits, FAN_DIV_BIT0[i], divisorBits & 1);
moel@85
   273
      }
moel@85
   274
     
moel@85
   275
      // write new fan divisors 
moel@85
   276
      for (int i = FAN_BIT_REG.Length - 1; i >= 0; i--) {
moel@85
   277
        byte oldByte = (byte)(bits & 0xFF);
moel@85
   278
        byte newByte = (byte)(newBits & 0xFF);
moel@85
   279
        bits = bits >> 8;
moel@85
   280
        newBits = newBits >> 8;
moel@85
   281
        if (oldByte != newByte) 
moel@85
   282
          WriteByte(0, FAN_BIT_REG[i], newByte);        
moel@85
   283
      }
moel@162
   284
moel@162
   285
      WinRing0.ReleaseIsaBusMutex();
moel@34
   286
    }
moel@34
   287
moel@130
   288
    public string GetReport() {
moel@34
   289
      StringBuilder r = new StringBuilder();
moel@34
   290
moel@34
   291
      r.AppendLine("LPC " + this.GetType().Name);
moel@34
   292
      r.AppendLine();
moel@34
   293
      r.Append("Chip ID: 0x"); r.AppendLine(chip.ToString("X"));
moel@34
   294
      r.Append("Chip revision: 0x"); r.AppendLine(revision.ToString("X"));
moel@34
   295
      r.Append("Base Adress: 0x"); r.AppendLine(address.ToString("X4"));
moel@34
   296
      r.AppendLine();
moel@162
   297
moel@162
   298
      if (!WinRing0.WaitIsaBusMutex())
moel@162
   299
        return r.ToString();
moel@162
   300
moel@34
   301
      r.AppendLine("Hardware Monitor Registers");
moel@34
   302
      r.AppendLine();
moel@34
   303
      r.AppendLine("      00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F");
moel@34
   304
      r.AppendLine();
moel@56
   305
      for (int i = 0; i <= 0x7; i++) {
moel@34
   306
        r.Append(" "); r.Append((i << 4).ToString("X2")); r.Append("  ");
moel@34
   307
        for (int j = 0; j <= 0xF; j++) {
moel@34
   308
          r.Append(" ");
moel@34
   309
          r.Append(ReadByte(0, (byte)((i << 4) | j)).ToString("X2"));
moel@34
   310
        }
moel@34
   311
        r.AppendLine();
moel@34
   312
      }
moel@53
   313
      for (int k = 1; k <= 15; k++) {
moel@34
   314
        r.AppendLine("Bank " + k);
moel@34
   315
        for (int i = 0x5; i < 0x6; i++) {
moel@34
   316
          r.Append(" "); r.Append((i << 4).ToString("X2")); r.Append("  ");
moel@34
   317
          for (int j = 0; j <= 0xF; j++) {
moel@34
   318
            r.Append(" ");
moel@34
   319
            r.Append(ReadByte((byte)(k),
moel@34
   320
              (byte)((i << 4) | j)).ToString("X2"));
moel@34
   321
          }
moel@34
   322
          r.AppendLine();
moel@34
   323
        }
moel@34
   324
      }
moel@34
   325
      r.AppendLine();
moel@34
   326
moel@162
   327
      WinRing0.ReleaseIsaBusMutex();
moel@162
   328
moel@34
   329
      return r.ToString();
moel@34
   330
    }
moel@130
   331
  } 
moel@34
   332
}