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