Hardware/LPC/NCT677X.cs
author moel.mich
Sun, 21 Nov 2010 17:31:25 +0000
changeset 245 f8e72b2efcc0
child 246 59024371cd50
permissions -rw-r--r--
Added first experimental support for the Nuvoton NCT6771F 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) 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.Globalization;
    39 using System.Text;
    40 
    41 namespace OpenHardwareMonitor.Hardware.LPC {
    42   internal class NCT677X : ISuperIO {
    43 
    44     private readonly ushort port;
    45     private readonly byte revision;
    46 
    47     private readonly Chip chip;
    48 
    49     private readonly float?[] voltages = new float?[9];
    50     private readonly float?[] temperatures = new float?[3];
    51     private readonly float?[] fans = new float?[4];
    52 
    53     // Hardware Monitor
    54     private const uint ADDRESS_REGISTER_OFFSET = 0x05;
    55     private const uint DATA_REGISTER_OFFSET = 0x06;
    56     private const byte BANK_SELECT_REGISTER = 0x4E;
    57 
    58     private byte ReadByte(ushort address) {
    59       byte bank = (byte)(address >> 8);
    60       byte register = (byte)(address & 0xFF);
    61       Ring0.WriteIoPort(port + ADDRESS_REGISTER_OFFSET, BANK_SELECT_REGISTER);
    62       Ring0.WriteIoPort(port + DATA_REGISTER_OFFSET, bank);
    63       Ring0.WriteIoPort(port + ADDRESS_REGISTER_OFFSET, register);
    64       return Ring0.ReadIoPort(port + DATA_REGISTER_OFFSET);
    65     }
    66 
    67     // Consts 
    68     private const ushort NUVOTON_VENDOR_ID = 0x5CA3;
    69 
    70     // Hardware Monitor Registers    
    71     private const ushort VENDOR_ID_HIGH_REGISTER = 0x804F;
    72     private const ushort VENDOR_ID_LOW_REGISTER = 0x004F;
    73     private const ushort VOLTAGE_VBAT_REG = 0x0551;
    74 
    75     private readonly ushort[] TEMPERATURE_REG = 
    76       { 0x150, 0x250, 0x27, 0x62B, 0x62C, 0x62D };
    77     private readonly ushort[] TEMPERATURE_HALF_REG = 
    78       { 0x151, 0x251, 0, 0x62E, 0x62E, 0x62E };    
    79     private readonly ushort[] TEMPERATURE_SRC_REG = 
    80       { 0x621, 0x622, 0x623, 0x624, 0x625, 0x626 };
    81     private readonly int[] TEMPERATURE_HALF_BIT = { 7, 7, -1, 0, 1, 2 };
    82     private readonly ushort[] VOLTAGE_REG = 
    83       { 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x550, 0x551 };
    84     private readonly ushort[] FAN_RPM_REG = { 0x656, 0x658, 0x65A, 0x65C};
    85 
    86     private enum TemperatureSource : byte {
    87       SYSTIN = 1,
    88       CPUTIN = 2,
    89       AUXTIN = 3,
    90       SMBUSMASTER = 4,
    91       PECI0 = 5, 
    92       PECI1 = 6, 
    93       PECI2 = 7,
    94       PECI3 = 8,
    95       PECI4 = 9,
    96       PECI5 = 10,
    97       PECI6 = 11,
    98       PECI7 = 12,
    99       PCH_CHIP_CPU_MAX_TEMP = 13,
   100       PCH_CHIP_TEMP = 14,
   101       PCH_CPU_TEMP = 15,
   102       PCH_MCH_TEMP = 16, 
   103       PCH_DIM0_TEMP = 17,
   104       PCH_DIM1_TEMP = 18,
   105       PCH_DIM2_TEMP = 19,
   106       PCH_DIM3_TEMP = 20
   107     }
   108 
   109     public NCT677X(Chip chip, byte revision, ushort port) {
   110       this.chip = chip;
   111       this.revision = revision;
   112       this.port = port;
   113 
   114       if (!IsNuvotonVendor())
   115         return;      
   116     }
   117 
   118     private bool IsNuvotonVendor() {
   119       return ((ReadByte(VENDOR_ID_HIGH_REGISTER) << 8) |
   120         ReadByte(VENDOR_ID_LOW_REGISTER)) == NUVOTON_VENDOR_ID;
   121     }
   122 
   123     public byte? ReadGPIO(int index) {
   124       return null;
   125     }
   126 
   127     public void WriteGPIO(int index, byte value) { }
   128 
   129     public Chip Chip { get { return chip; } }
   130     public float?[] Voltages { get { return voltages; } }
   131     public float?[] Temperatures { get { return temperatures; } }
   132     public float?[] Fans { get { return fans; } }
   133 
   134     public void Update() {
   135       if (!Ring0.WaitIsaBusMutex(10))
   136         return;
   137 
   138       for (int i = 0; i < voltages.Length; i++) {
   139         float value = 0.008f * ReadByte(VOLTAGE_REG[i]);
   140         bool valid = value > 0;
   141 
   142         // check if battery voltage monitor is enabled
   143         if (valid && VOLTAGE_REG[i] == VOLTAGE_VBAT_REG) 
   144           valid = (ReadByte(0x005D) & 0x01) > 0;
   145 
   146         voltages[i] = valid ? value : (float?)null;
   147       }
   148 
   149       for (int i = 0; i < TEMPERATURE_REG.Length; i++) {
   150         float value = ReadByte(TEMPERATURE_REG[i]);
   151         if (TEMPERATURE_HALF_BIT[i] > 0) {
   152           value += 0.5f * ((ReadByte(TEMPERATURE_HALF_REG[i]) >> 
   153             TEMPERATURE_HALF_BIT[i]) & 0x1);
   154         }
   155 
   156         TemperatureSource source = (TemperatureSource)
   157           ReadByte(TEMPERATURE_SRC_REG[i]);
   158 
   159         switch (source) {
   160           case TemperatureSource.CPUTIN: temperatures[0] = value; break;
   161           case TemperatureSource.AUXTIN: temperatures[1] = value; break;
   162           case TemperatureSource.SYSTIN: temperatures[2] = value; break;
   163         }
   164       }
   165 
   166       for (int i = 0; i < fans.Length; i++) {
   167         byte high = ReadByte(FAN_RPM_REG[i]);
   168         byte low = ReadByte((ushort)(FAN_RPM_REG[i] + 1));
   169         fans[i] = (high << 8) | low;
   170       }
   171 
   172       Ring0.ReleaseIsaBusMutex();
   173     }
   174 
   175     public string GetReport() {
   176       StringBuilder r = new StringBuilder();
   177 
   178       r.AppendLine("LPC " + this.GetType().Name);
   179       r.AppendLine();
   180       r.Append("Chip ID: 0x"); r.AppendLine(chip.ToString("X"));
   181       r.Append("Chip revision: 0x");
   182       r.AppendLine(revision.ToString("X", CultureInfo.InvariantCulture));
   183       r.Append("Base Adress: 0x");
   184       r.AppendLine(port.ToString("X4", CultureInfo.InvariantCulture));
   185       r.AppendLine();
   186 
   187       if (!Ring0.WaitIsaBusMutex(100))
   188         return r.ToString();
   189 
   190       r.AppendLine("Hardware Monitor Registers");
   191       r.AppendLine();
   192       r.AppendLine("       00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F");
   193       r.AppendLine();
   194       for (int i = 0; i <= 0x7; i++) {
   195         r.Append(" ");
   196         r.Append((i << 4).ToString("X3", CultureInfo.InvariantCulture));
   197         r.Append("  ");
   198         for (int j = 0; j <= 0xF; j++) {
   199           r.Append(" ");
   200           r.Append(ReadByte((ushort)((i << 4) | j)).ToString(
   201             "X2", CultureInfo.InvariantCulture));
   202         }
   203         r.AppendLine();
   204       }
   205       for (int bank = 1; bank <= 15; bank++) {
   206         for (int i = 0x5; i < 0x6; i++) {
   207           r.Append(" ");
   208           r.Append((i << 4).ToString("X3", CultureInfo.InvariantCulture));
   209           r.Append("  ");
   210           for (int j = 0; j <= 0xF; j++) {
   211             r.Append(" ");
   212             r.Append(ReadByte((ushort)((bank << 8) | (i << 4) | j)).ToString(
   213               "X2", CultureInfo.InvariantCulture));
   214           }
   215           r.AppendLine();
   216         }
   217       }
   218       r.AppendLine();
   219 
   220       Ring0.ReleaseIsaBusMutex();
   221 
   222       return r.ToString();
   223     }
   224   }
   225 }