Hardware/LPC/W836XX.cs
author moel.mich
Sat, 20 Feb 2010 19:51:10 +0000
changeset 56 5cb7eb5bf628
parent 54 f940fe2a7c2b
child 58 d787f441286d
permissions -rw-r--r--
Improved Winbond temperature reading. Temperatures create by adding PECI Agent values (delta to TCC Activation Temperature) to a (possibly uncalibrated) TBase are not read. Direct reading temperatures from sensor report register if available. Added lower bound for temperatures on Winbond chips. Nvidia GPUs are now displayed even if they do not have any sensors.
     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     private byte[] TEMPERATURE_SEL = new byte[] { 1, 2, 0 };
    77 
    78     private byte[] FAN_TACHO_REG = new byte[] { 0x28, 0x29, 0x2A, 0x3F, 0x53 };
    79     private byte[] FAN_TACHO_BANK = new byte[] { 0, 0, 0, 0, 5 };       
    80     private byte[] FAN_BIT_REG = new byte[] { 0x47, 0x4B, 0x4C, 0x59, 0x5D };
    81     private byte[] FAN_DIV_BIT0 = new byte[] { 36, 38, 30, 8, 10 };
    82     private byte[] FAN_DIV_BIT1 = new byte[] { 37, 39, 31, 9, 11 };
    83     private byte[] FAN_DIV_BIT2 = new byte[] { 5, 6, 7, 23, 15 };
    84 
    85     private byte ReadByte(byte bank, byte register) {
    86       WinRing0.WriteIoPortByte(
    87          (ushort)(address + ADDRESS_REGISTER_OFFSET), BANK_SELECT_REGISTER);
    88       WinRing0.WriteIoPortByte(
    89          (ushort)(address + DATA_REGISTER_OFFSET), bank);
    90       WinRing0.WriteIoPortByte(
    91          (ushort)(address + ADDRESS_REGISTER_OFFSET), register);
    92       return WinRing0.ReadIoPortByte(
    93         (ushort)(address + DATA_REGISTER_OFFSET));
    94     } 
    95 
    96     private void WriteByte(byte bank, byte register, byte value) {
    97       WinRing0.WriteIoPortByte(
    98          (ushort)(address + ADDRESS_REGISTER_OFFSET), BANK_SELECT_REGISTER);
    99       WinRing0.WriteIoPortByte(
   100          (ushort)(address + DATA_REGISTER_OFFSET), bank);
   101       WinRing0.WriteIoPortByte(
   102          (ushort)(address + ADDRESS_REGISTER_OFFSET), register);
   103       WinRing0.WriteIoPortByte(
   104          (ushort)(address + DATA_REGISTER_OFFSET), value); 
   105     }
   106    
   107     public W836XX(Chip chip, byte revision, ushort address) 
   108       : base(chip)
   109     {
   110       this.address = address;
   111       this.revision = revision;
   112 
   113       available = IsWinbondVendor();
   114 
   115       List<Sensor> list = new List<Sensor>();
   116       switch (chip) {
   117         case Chip.W83627DHG:
   118           // do not add temperature sensor registers that read PECI agents
   119           byte sel = ReadByte(0, TEMPERATURE_SOURCE_SELECT_REG);
   120           if ((sel & 0x07) == 0)
   121             list.Add(new Sensor(TEMPERATURE_NAME[0], 0,
   122               SensorType.Temperature, this));
   123           if ((sel & 0x70) == 0)
   124             list.Add(new Sensor(TEMPERATURE_NAME[1], 1,
   125               SensorType.Temperature, this));
   126           list.Add(new Sensor(TEMPERATURE_NAME[2], 2,
   127             SensorType.Temperature, this));
   128           break;
   129         default:
   130           // no PECI support or extra sensor report register
   131           for (int i = 0; i < TEMPERATURE_NAME.Length; i++) {
   132             list.Add(new Sensor(TEMPERATURE_NAME[i], i,
   133               SensorType.Temperature, this));
   134           }
   135           break;
   136       }
   137       temperatures = list.ToArray();
   138 
   139       switch (chip) {
   140         case Chip.W83627DHG:
   141         case Chip.W83627DHGP:
   142         case Chip.W83627EHF:
   143         case Chip.W83667HG:
   144         case Chip.W83667HGB: 
   145           fanNames = new string[] { "System", "CPU", "Auxiliary", 
   146             "CPU #2", "Auxiliary #2" };
   147           voltageGains = new float[] { 1, 1, 1, 2, 1, 1, 1, 2 };
   148           voltages = new Sensor[3];
   149           voltages[0] = new Sensor("CPU VCore", 0, SensorType.Voltage, this);
   150           voltages[1] = new Sensor("+3.3V", 3, SensorType.Voltage, this);
   151           voltages[2] = new Sensor("Battery", 7, SensorType.Voltage, this);
   152           break;
   153         case Chip.W83627HF:
   154         case Chip.W83627THF:
   155           fanNames = new string[] { "System", "CPU", "Auxiliary" };
   156           voltageGains = new float[] { 2, 1, 2, 1, 1, 1, 1, 2 };
   157           voltages = new Sensor[3];
   158           voltages[0] = new Sensor("CPU VCore", 0, SensorType.Voltage, this);
   159           voltages[1] = new Sensor("+3.3V", 2, SensorType.Voltage, this);
   160           voltages[2] = new Sensor("Battery", 7, SensorType.Voltage, this);
   161           break;
   162         default: fanNames = new string[0];
   163           break;
   164       }
   165       
   166       fans = new Sensor[fanNames.Length];
   167       for (int i = 0; i < fanNames.Length; i++)
   168         fans[i] = new Sensor(fanNames[i], i, SensorType.Fan, this);
   169     }
   170 
   171     public bool IsAvailable {
   172       get { return available; }
   173     }        
   174 
   175     private bool IsWinbondVendor() {
   176       ushort vendorId =
   177         (ushort)((ReadByte(HIGH_BYTE, VENDOR_ID_REGISTER) << 8) |
   178            ReadByte(0, VENDOR_ID_REGISTER));
   179       return vendorId == WINBOND_VENDOR_ID;
   180     }
   181 
   182     public void Update() {
   183 
   184       foreach (Sensor sensor in voltages) {
   185         if (sensor.Index < 7) {
   186           int value = ReadByte(0, (byte)(VOLTAGE_BASE_REG + sensor.Index));
   187           sensor.Value = 0.008f * voltageGains[sensor.Index] * value;
   188           if (sensor.Value > 0)
   189             ActivateSensor(sensor);
   190           else
   191             DeactivateSensor(sensor);
   192         } else {
   193           // Battery voltage
   194           bool valid = (ReadByte(0, 0x5D) & 0x01) > 0;
   195           if (valid) {
   196             sensor.Value =
   197               0.008f * voltageGains[sensor.Index] * ReadByte(5, 0x51);
   198             ActivateSensor(sensor);
   199           } else
   200             DeactivateSensor(sensor);
   201         }
   202       }
   203 
   204       foreach (Sensor sensor in temperatures) {
   205         int value;
   206         switch (chip) {
   207           case Chip.W83667HG:
   208           case Chip.W83667HGB:
   209             WriteByte(0, 0x7D, TEMPERATURE_SEL[sensor.Index]);
   210             value = ((sbyte)ReadByte(0, 0x7E)) << 1;
   211             break;
   212           case Chip.W83627DHGP:
   213             WriteByte(0, 0x7C, TEMPERATURE_SEL[sensor.Index]);
   214             value = ((sbyte)ReadByte(0, 0x7D)) << 1;
   215             break;
   216           default:
   217             value = ((sbyte)ReadByte(TEMPERATURE_BANK[sensor.Index],
   218               TEMPERATURE_REG[sensor.Index])) << 1;
   219             if (TEMPERATURE_BANK[sensor.Index] > 0) {
   220               value |= ReadByte(TEMPERATURE_BANK[sensor.Index],
   221                 (byte)(TEMPERATURE_REG[sensor.Index] + 1)) >> 7;
   222             }            
   223             break;
   224         }
   225         float temperature = value / 2.0f;
   226         if (temperature <= 125 && temperature >= -55) {
   227           sensor.Value = temperature;
   228           ActivateSensor(sensor);
   229         } else {
   230           DeactivateSensor(sensor);
   231         }
   232       }
   233 
   234       long bits = 0;
   235       for (int i = 0; i < FAN_BIT_REG.Length; i++)
   236         bits = (bits << 8) | ReadByte(0, FAN_BIT_REG[i]);
   237       foreach (Sensor sensor in fans) {
   238         int count = ReadByte(FAN_TACHO_BANK[sensor.Index], 
   239           FAN_TACHO_REG[sensor.Index]);
   240         int divisorBits = (int)(
   241           (((bits >> FAN_DIV_BIT2[sensor.Index]) & 1) << 2) |
   242           (((bits >> FAN_DIV_BIT1[sensor.Index]) & 1) << 1) |
   243            ((bits >> FAN_DIV_BIT0[sensor.Index]) & 1));
   244         int divisor = 1 << divisorBits;
   245         float value = (count < 0xff) ? 1.35e6f / (count * divisor) : 0;
   246         sensor.Value = value;
   247         if (value > 0)
   248           ActivateSensor(sensor);        
   249       }     
   250     }
   251 
   252     public string GetReport() {
   253       StringBuilder r = new StringBuilder();
   254 
   255       r.AppendLine("LPC " + this.GetType().Name);
   256       r.AppendLine();
   257       r.Append("Chip ID: 0x"); r.AppendLine(chip.ToString("X"));
   258       r.Append("Chip revision: 0x"); r.AppendLine(revision.ToString("X"));
   259       r.Append("Base Adress: 0x"); r.AppendLine(address.ToString("X4"));
   260       r.AppendLine();
   261       r.AppendLine("Hardware Monitor Registers");
   262       r.AppendLine();
   263       r.AppendLine("      00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F");
   264       r.AppendLine();
   265       for (int i = 0; i <= 0x7; i++) {
   266         r.Append(" "); r.Append((i << 4).ToString("X2")); r.Append("  ");
   267         for (int j = 0; j <= 0xF; j++) {
   268           r.Append(" ");
   269           r.Append(ReadByte(0, (byte)((i << 4) | j)).ToString("X2"));
   270         }
   271         r.AppendLine();
   272       }
   273       for (int k = 1; k <= 15; k++) {
   274         r.AppendLine("Bank " + k);
   275         for (int i = 0x5; i < 0x6; i++) {
   276           r.Append(" "); r.Append((i << 4).ToString("X2")); r.Append("  ");
   277           for (int j = 0; j <= 0xF; j++) {
   278             r.Append(" ");
   279             r.Append(ReadByte((byte)(k),
   280               (byte)((i << 4) | j)).ToString("X2"));
   281           }
   282           r.AppendLine();
   283         }
   284       }
   285       r.AppendLine();
   286 
   287       return r.ToString();
   288     }
   289   }
   290 }