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