Hardware/LPC/F718XX.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.Globalization;
    39 using System.Text;
    40 
    41 namespace OpenHardwareMonitor.Hardware.LPC {
    42   internal class F718XX : ISuperIO {
    43 
    44     private readonly ushort address;
    45     private readonly Chip chip;
    46 
    47     private readonly float?[] voltages;
    48     private readonly float?[] temperatures;
    49     private readonly float?[] fans;
    50     private readonly float?[] controls;
    51 
    52     // Hardware Monitor
    53     private const byte ADDRESS_REGISTER_OFFSET = 0x05;
    54     private const byte DATA_REGISTER_OFFSET = 0x06;
    55 
    56     // Hardware Monitor Registers
    57     private const byte VOLTAGE_BASE_REG = 0x20;
    58     private const byte TEMPERATURE_CONFIG_REG = 0x69;
    59     private const byte TEMPERATURE_BASE_REG = 0x70;
    60     private readonly byte[] FAN_TACHOMETER_REG = 
    61       new byte[] { 0xA0, 0xB0, 0xC0, 0xD0 };
    62     
    63     private byte ReadByte(byte register) {
    64       Ring0.WriteIoPort(
    65         (ushort)(address + ADDRESS_REGISTER_OFFSET), register);
    66       return Ring0.ReadIoPort((ushort)(address + DATA_REGISTER_OFFSET));
    67     }
    68 
    69     public byte? ReadGPIO(int index) {
    70       return null;
    71     }
    72 
    73     public void WriteGPIO(int index, byte value) { }
    74 
    75     public void SetControl(int index, byte? value) { }   
    76 
    77     public F718XX(Chip chip, ushort address) {
    78       this.address = address;
    79       this.chip = chip;
    80 
    81       voltages = new float?[chip == Chip.F71858 ? 3 : 9];
    82       temperatures = new float?[3];
    83       fans = new float?[chip == Chip.F71882 || chip == Chip.F71858? 4 : 3];
    84       controls = new float?[0];
    85     }
    86 
    87     public Chip Chip { get { return chip; } }
    88     public float?[] Voltages { get { return voltages; } }
    89     public float?[] Temperatures { get { return temperatures; } }
    90     public float?[] Fans { get { return fans; } }
    91     public float?[] Controls { get { return controls; } }
    92 
    93     public string GetReport() {
    94       StringBuilder r = new StringBuilder();
    95 
    96       r.AppendLine("LPC " + this.GetType().Name);
    97       r.AppendLine();
    98       r.Append("Base Adress: 0x"); 
    99       r.AppendLine(address.ToString("X4", CultureInfo.InvariantCulture));
   100       r.AppendLine();
   101 
   102       if (!Ring0.WaitIsaBusMutex(100))
   103         return r.ToString();
   104 
   105       r.AppendLine("Hardware Monitor Registers");
   106       r.AppendLine();
   107       r.AppendLine("      00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F");
   108       r.AppendLine();
   109       for (int i = 0; i <= 0xF; i++) {
   110         r.Append(" "); 
   111         r.Append((i << 4).ToString("X2", CultureInfo.InvariantCulture)); 
   112         r.Append("  ");
   113         for (int j = 0; j <= 0xF; j++) {
   114           r.Append(" ");
   115           r.Append(ReadByte((byte)((i << 4) | j)).ToString("X2", 
   116             CultureInfo.InvariantCulture));
   117         }
   118         r.AppendLine();
   119       }
   120       r.AppendLine();
   121 
   122       Ring0.ReleaseIsaBusMutex();
   123 
   124       return r.ToString();
   125     }
   126 
   127     public void Update() {
   128       if (!Ring0.WaitIsaBusMutex(10))
   129         return;
   130 
   131       for (int i = 0; i < voltages.Length; i++) {
   132         int value = ReadByte((byte)(VOLTAGE_BASE_REG + i));
   133         voltages[i] = 0.008f * value;
   134       }
   135      
   136       for (int i = 0; i < temperatures.Length; i++) {
   137         switch (chip) {
   138           case Chip.F71858: {
   139               int tableMode = 0x3 & ReadByte(TEMPERATURE_CONFIG_REG);
   140               int high = 
   141                 ReadByte((byte)(TEMPERATURE_BASE_REG + 2 * i));
   142               int low =
   143                 ReadByte((byte)(TEMPERATURE_BASE_REG + 2 * i + 1));              
   144               if (high != 0xbb && high != 0xcc) {
   145                 int bits = 0;
   146                 switch (tableMode) {
   147                   case 0: bits = 0; break;
   148                   case 1: bits = 0; break;
   149                   case 2: bits = (high & 0x80) << 8; break;
   150                   case 3: bits = (low & 0x01) << 15; break;
   151                 }
   152                 bits |= high << 7;
   153                 bits |= (low & 0xe0) >> 1;
   154                 short value = (short)(bits & 0xfff0);
   155                 temperatures[i] = value / 128.0f;
   156               } else {
   157                 temperatures[i] = null;
   158               }
   159           } break;
   160           default: {
   161             sbyte value = (sbyte)ReadByte((byte)(
   162               TEMPERATURE_BASE_REG + 2 * (i + 1)));            
   163             if (value < sbyte.MaxValue && value > 0)
   164               temperatures[i] = value;
   165             else
   166               temperatures[i] = null;
   167           } break;
   168         }
   169       }
   170 
   171       for (int i = 0; i < fans.Length; i++) {
   172         int value = ReadByte(FAN_TACHOMETER_REG[i]) << 8;
   173         value |= ReadByte((byte)(FAN_TACHOMETER_REG[i] + 1));
   174 
   175         if (value > 0) 
   176           fans[i] = (value < 0x0fff) ? 1.5e6f / value : 0;
   177         else 
   178           fans[i] = null;        
   179       }
   180 
   181       Ring0.ReleaseIsaBusMutex();
   182     }
   183   }
   184 }