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