Hardware/LPC/W836XX.cs
author moel.mich
Fri, 19 Feb 2010 18:33:08 +0000
changeset 54 f940fe2a7c2b
parent 53 ccba498488e8
child 56 5cb7eb5bf628
permissions -rw-r--r--
Added support for Winbond W83627THF super I/O chip.
     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_BASE_REG = 0x50;
    71     private const byte TEMPERATURE_SYS_REG = 0x27;    
    72 
    73     private byte[] FAN_TACHO_REG = new byte[] { 0x28, 0x29, 0x2A, 0x3F, 0x53 };
    74     private byte[] FAN_TACHO_BANK = new byte[] { 0, 0, 0, 0, 5 };       
    75     private byte[] FAN_BIT_REG = new byte[] { 0x47, 0x4B, 0x4C, 0x59, 0x5D };
    76     private byte[] FAN_DIV_BIT0 = new byte[] { 36, 38, 30, 8, 10 };
    77     private byte[] FAN_DIV_BIT1 = new byte[] { 37, 39, 31, 9, 11 };
    78     private byte[] FAN_DIV_BIT2 = new byte[] { 5, 6, 7, 23, 15 };
    79 
    80     private byte ReadByte(byte bank, byte register) {
    81       WinRing0.WriteIoPortByte(
    82          (ushort)(address + ADDRESS_REGISTER_OFFSET), BANK_SELECT_REGISTER);
    83       WinRing0.WriteIoPortByte(
    84          (ushort)(address + DATA_REGISTER_OFFSET), bank);
    85       WinRing0.WriteIoPortByte(
    86          (ushort)(address + ADDRESS_REGISTER_OFFSET), register);
    87       return WinRing0.ReadIoPortByte(
    88         (ushort)(address + DATA_REGISTER_OFFSET));
    89     } 
    90 
    91     public W836XX(Chip chip, byte revision, ushort address) 
    92       : base(chip)
    93     {
    94       this.address = address;
    95       this.revision = revision;
    96 
    97       available = IsWinbondVendor();
    98 
    99       temperatures = new Sensor[3];
   100       temperatures[0] = new Sensor("CPU", 0, SensorType.Temperature, this);
   101       temperatures[1] = new Sensor("Auxiliary", 1, SensorType.Temperature, this);
   102       temperatures[2] = new Sensor("System", 2, SensorType.Temperature, this);
   103 
   104       switch (chip) {
   105         case Chip.W83627DHG:
   106         case Chip.W83627DHGP:
   107         case Chip.W83627EHF:
   108         case Chip.W83667HG:
   109         case Chip.W83667HGB: 
   110           fanNames = new string[] { "System", "CPU", "Auxiliary", 
   111             "CPU #2", "Auxiliary #2" };
   112           voltageGains = new float[] { 1, 1, 1, 2, 1, 1, 1, 2 };
   113           voltages = new Sensor[3];
   114           voltages[0] = new Sensor("CPU VCore", 0, SensorType.Voltage, this);
   115           voltages[1] = new Sensor("+3.3V", 3, SensorType.Voltage, this);
   116           voltages[2] = new Sensor("Battery", 7, SensorType.Voltage, this);
   117           break;
   118         case Chip.W83627HF:
   119         case Chip.W83627THF:
   120           fanNames = new string[] { "System", "CPU", "Auxiliary" };
   121           voltageGains = new float[] { 2, 1, 2, 1, 1, 1, 1, 2 };
   122           voltages = new Sensor[3];
   123           voltages[0] = new Sensor("CPU VCore", 0, SensorType.Voltage, this);
   124           voltages[1] = new Sensor("+3.3V", 2, SensorType.Voltage, this);
   125           voltages[2] = new Sensor("Battery", 7, SensorType.Voltage, this);
   126           break;
   127         default: fanNames = new string[0];
   128           break;
   129       }
   130       
   131       fans = new Sensor[fanNames.Length];
   132       for (int i = 0; i < fanNames.Length; i++)
   133         fans[i] = new Sensor(fanNames[i], i, SensorType.Fan, this);
   134     }
   135 
   136     public bool IsAvailable {
   137       get { return available; }
   138     }        
   139 
   140     private bool IsWinbondVendor() {
   141       ushort vendorId =
   142         (ushort)((ReadByte(HIGH_BYTE, VENDOR_ID_REGISTER) << 8) |
   143            ReadByte(0, VENDOR_ID_REGISTER));
   144       return vendorId == WINBOND_VENDOR_ID;
   145     }
   146 
   147     public void Update() {
   148       foreach (Sensor sensor in voltages) {
   149         if (sensor.Index < 7) {
   150           int value = ReadByte(0, (byte)(VOLTAGE_BASE_REG + sensor.Index));
   151           sensor.Value = 0.008f * voltageGains[sensor.Index] * value;
   152           if (sensor.Value > 0)
   153             ActivateSensor(sensor);
   154           else
   155             DeactivateSensor(sensor);
   156         } else {
   157           // Battery voltage
   158           bool valid = (ReadByte(0, 0x5D) & 0x01) > 0;
   159           if (valid) {
   160             sensor.Value = 
   161               0.008f * voltageGains[sensor.Index] * ReadByte(5, 0x51);
   162             ActivateSensor(sensor);
   163           } else
   164             DeactivateSensor(sensor);
   165         }
   166       }
   167 
   168       foreach (Sensor sensor in temperatures) {
   169         int value;
   170         if (sensor.Index < 2) {
   171           value = (sbyte)ReadByte((byte)(sensor.Index + 1), TEMPERATURE_BASE_REG);
   172           value = (value << 1) | ReadByte((byte)(sensor.Index + 1),
   173             (byte)(TEMPERATURE_BASE_REG + 1)) >> 7;
   174         } else {
   175           value = (sbyte)ReadByte(0, TEMPERATURE_SYS_REG) << 1;
   176         }
   177         sensor.Value = value / 2.0f;
   178         if (value < 0xFE)
   179           ActivateSensor(sensor);
   180         else
   181           DeactivateSensor(sensor);
   182       }
   183 
   184       long bits = 0;
   185       for (int i = 0; i < FAN_BIT_REG.Length; i++)
   186         bits = (bits << 8) | ReadByte(0, FAN_BIT_REG[i]);
   187       foreach (Sensor sensor in fans) {
   188         int count = ReadByte(FAN_TACHO_BANK[sensor.Index], 
   189           FAN_TACHO_REG[sensor.Index]);
   190         int divisorBits = (int)(
   191           (((bits >> FAN_DIV_BIT2[sensor.Index]) & 1) << 2) |
   192           (((bits >> FAN_DIV_BIT1[sensor.Index]) & 1) << 1) |
   193            ((bits >> FAN_DIV_BIT0[sensor.Index]) & 1));
   194         int divisor = 1 << divisorBits;
   195         float value = (count < 0xff) ? 1.35e6f / (count * divisor) : 0;
   196         sensor.Value = value;
   197         if (value > 0)
   198           ActivateSensor(sensor);        
   199       }     
   200     }
   201 
   202     public string GetReport() {
   203       StringBuilder r = new StringBuilder();
   204 
   205       r.AppendLine("LPC " + this.GetType().Name);
   206       r.AppendLine();
   207       r.Append("Chip ID: 0x"); r.AppendLine(chip.ToString("X"));
   208       r.Append("Chip revision: 0x"); r.AppendLine(revision.ToString("X"));
   209       r.Append("Base Adress: 0x"); r.AppendLine(address.ToString("X4"));
   210       r.AppendLine();
   211       r.AppendLine("Hardware Monitor Registers");
   212       r.AppendLine();
   213       r.AppendLine("      00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F");
   214       r.AppendLine();
   215       for (int i = 0; i < 0x7; i++) {
   216         r.Append(" "); r.Append((i << 4).ToString("X2")); r.Append("  ");
   217         for (int j = 0; j <= 0xF; j++) {
   218           r.Append(" ");
   219           r.Append(ReadByte(0, (byte)((i << 4) | j)).ToString("X2"));
   220         }
   221         r.AppendLine();
   222       }
   223       for (int k = 1; k <= 15; k++) {
   224         r.AppendLine("Bank " + k);
   225         for (int i = 0x5; i < 0x6; i++) {
   226           r.Append(" "); r.Append((i << 4).ToString("X2")); r.Append("  ");
   227           for (int j = 0; j <= 0xF; j++) {
   228             r.Append(" ");
   229             r.Append(ReadByte((byte)(k),
   230               (byte)((i << 4) | j)).ToString("X2"));
   231           }
   232           r.AppendLine();
   233         }
   234       }
   235       r.AppendLine();
   236 
   237       return r.ToString();
   238     }
   239   }
   240 }