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