Hardware/LPC/LPCGroup.cs
author moel.mich
Tue, 02 Mar 2010 20:32:24 +0000
changeset 68 d706e16a79c0
parent 67 bb5696abab23
child 92 8a5ff8bdf3ca
permissions -rw-r--r--
Added support for Fintek F71858 chips. Corrected the number of fans for Fintek.
     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 System.Threading;
    42 
    43 namespace OpenHardwareMonitor.Hardware.LPC {
    44   public class LPCGroup : IGroup {
    45 
    46     private List<IHardware> hardware = new List<IHardware>();
    47     private StringBuilder report = new StringBuilder();
    48 
    49     private Chip chip = Chip.Unknown;
    50 
    51     // I/O Ports
    52     private ushort[] REGISTER_PORTS = new ushort[] { 0x2e, 0x4e };
    53     private ushort[] VALUE_PORTS = new ushort[] { 0x2f, 0x4f };
    54 
    55     private ushort registerPort;
    56     private ushort valuePort;
    57 
    58     // Registers
    59     private const byte CONFIGURATION_CONTROL_REGISTER = 0x02;
    60     private const byte DEVCIE_SELECT_REGISTER = 0x07;
    61     private const byte CHIP_ID_REGISTER = 0x20;
    62     private const byte CHIP_REVISION_REGISTER = 0x21;
    63     private const byte BASE_ADDRESS_REGISTER = 0x60;
    64 
    65     private byte ReadByte(byte register) {
    66       WinRing0.WriteIoPortByte(registerPort, register);
    67       return WinRing0.ReadIoPortByte(valuePort);
    68     } 
    69 
    70     private ushort ReadWord(byte register) {
    71       return (ushort)((ReadByte(register) << 8) | 
    72         ReadByte((byte)(register + 1)));
    73     }
    74 
    75     private void Select(byte logicalDeviceNumber) {
    76       WinRing0.WriteIoPortByte(registerPort, DEVCIE_SELECT_REGISTER);
    77       WinRing0.WriteIoPortByte(valuePort, logicalDeviceNumber);
    78     }
    79 
    80     // ITE
    81     private const byte IT87_ENVIRONMENT_CONTROLLER_LDN = 0x04;    
    82 
    83     private void IT87Enter() {
    84       WinRing0.WriteIoPortByte(registerPort, 0x87);
    85       WinRing0.WriteIoPortByte(registerPort, 0x01);
    86       WinRing0.WriteIoPortByte(registerPort, 0x55);
    87       WinRing0.WriteIoPortByte(registerPort, 0x55);
    88     }
    89 
    90     internal void IT87Exit() {
    91       WinRing0.WriteIoPortByte(registerPort, CONFIGURATION_CONTROL_REGISTER);
    92       WinRing0.WriteIoPortByte(valuePort, 0x02);
    93     }
    94 
    95     // Winbond, Fintek
    96     private const byte FINTEK_VENDOR_ID_REGISTER = 0x23;
    97     private const ushort FINTEK_VENDOR_ID = 0x1934;
    98 
    99     private const byte WINBOND_HARDWARE_MONITOR_LDN = 0x0B;
   100 
   101     private const byte F71858_HARDWARE_MONITOR_LDN = 0x02;
   102     private const byte FINTEK_HARDWARE_MONITOR_LDN = 0x04;
   103 
   104     private void WinbondFintekEnter() {
   105       WinRing0.WriteIoPortByte(registerPort, 0x87);
   106       WinRing0.WriteIoPortByte(registerPort, 0x87);
   107     }
   108 
   109     private void WinbondFintekExit() {
   110       WinRing0.WriteIoPortByte(registerPort, 0xAA);      
   111     }
   112 
   113     // SMSC
   114     private void SMSCEnter() {
   115       WinRing0.WriteIoPortByte(registerPort, 0x55);
   116     }
   117 
   118     private void SMSCExit() {
   119       WinRing0.WriteIoPortByte(registerPort, 0xAA);
   120     }
   121 
   122     public LPCGroup() {
   123       if (!WinRing0.IsAvailable)
   124         return;
   125 
   126       for (int i = 0; i < REGISTER_PORTS.Length; i++) {
   127         registerPort = REGISTER_PORTS[i];
   128         valuePort = VALUE_PORTS[i];
   129 
   130         WinbondFintekEnter();
   131 
   132         byte logicalDeviceNumber;
   133         byte id = ReadByte(CHIP_ID_REGISTER);
   134         byte revision = ReadByte(CHIP_REVISION_REGISTER);
   135         chip = Chip.Unknown;
   136         logicalDeviceNumber = 0;
   137         switch (id) {
   138           case 0x05:
   139             switch (revision) {
   140               case 0x07:
   141                 chip = Chip.F71858;
   142                 logicalDeviceNumber = F71858_HARDWARE_MONITOR_LDN;
   143                 break;
   144               case 0x41:
   145                 chip = Chip.F71882;
   146                 logicalDeviceNumber = FINTEK_HARDWARE_MONITOR_LDN;
   147                 break;              
   148             } break;
   149           case 0x06:
   150             switch (revision) {             
   151               case 0x01:
   152                 chip = Chip.F71862;
   153                 logicalDeviceNumber = FINTEK_HARDWARE_MONITOR_LDN;
   154                 break;              
   155             } break;
   156           case 0x07:
   157             switch (revision) {
   158               case 0x23:
   159                 chip = Chip.F71889;
   160                 logicalDeviceNumber = FINTEK_HARDWARE_MONITOR_LDN;
   161                 break;              
   162             } break;
   163           case 0x08:
   164             switch (revision) {
   165               case 0x14:
   166                 chip = Chip.F71869;
   167                 logicalDeviceNumber = FINTEK_HARDWARE_MONITOR_LDN;
   168                 break;              
   169             } break;
   170           case 0x52:
   171             switch (revision) {
   172               case 0x17:
   173               case 0x3A:
   174                 chip = Chip.W83627HF;
   175                 logicalDeviceNumber = WINBOND_HARDWARE_MONITOR_LDN;
   176                 break;             
   177             } break;
   178           case 0x82:
   179             switch (revision) {
   180               case 0x83:
   181                 chip = Chip.W83627THF;
   182                 logicalDeviceNumber = WINBOND_HARDWARE_MONITOR_LDN;
   183                 break;
   184             } break;
   185           case 0x85:
   186             switch (revision) {
   187               case 0x41:
   188                 chip = Chip.W83687THF;
   189                 logicalDeviceNumber = WINBOND_HARDWARE_MONITOR_LDN;
   190                 break;
   191             } break;
   192           case 0x88:
   193             switch (revision & 0xF0) {
   194               case 0x60:
   195                 chip = Chip.W83627EHF;
   196                 logicalDeviceNumber = WINBOND_HARDWARE_MONITOR_LDN;
   197                 break;
   198             } break;
   199           case 0xA0:
   200             switch (revision & 0xF0) {
   201               case 0x20: 
   202                 chip = Chip.W83627DHG;
   203                 logicalDeviceNumber = WINBOND_HARDWARE_MONITOR_LDN;  
   204                 break;             
   205             } break;
   206           case 0xA5:
   207             switch (revision & 0xF0) {
   208               case 0x10:
   209                 chip = Chip.W83667HG;
   210                 logicalDeviceNumber = WINBOND_HARDWARE_MONITOR_LDN;
   211                 break;
   212             } break;
   213           case 0xB0:
   214             switch (revision & 0xF0) {
   215               case 0x70:
   216                 chip = Chip.W83627DHGP;
   217                 logicalDeviceNumber = WINBOND_HARDWARE_MONITOR_LDN;
   218                 break;             
   219             } break;
   220           case 0xB3:
   221             switch (revision & 0xF0) {
   222               case 0x50:
   223                 chip = Chip.W83667HGB;
   224                 logicalDeviceNumber = WINBOND_HARDWARE_MONITOR_LDN;
   225                 break;
   226             } break; 
   227         }
   228         if (chip == Chip.Unknown) {
   229           if (id != 0 && id != 0xff) {
   230             WinbondFintekExit();
   231 
   232             report.Append("Chip ID: Unknown Winbond / Fintek with ID 0x"); 
   233             report.AppendLine(((id << 8) | revision).ToString("X"));
   234             report.AppendLine();
   235           }
   236         } else {
   237 
   238           Select(logicalDeviceNumber);
   239           ushort address = ReadWord(BASE_ADDRESS_REGISTER);          
   240           Thread.Sleep(1);
   241           ushort verify = ReadWord(BASE_ADDRESS_REGISTER);
   242 
   243           ushort vendorID = FINTEK_VENDOR_ID;
   244           if (chip == Chip.F71858 || chip == Chip.F71862 || 
   245             chip == Chip.F71882 || chip == Chip.F71889)
   246             vendorID = ReadWord(FINTEK_VENDOR_ID_REGISTER);
   247 
   248           WinbondFintekExit();
   249 
   250           if (address != verify) {            
   251             report.Append("Chip ID: 0x"); 
   252             report.AppendLine(chip.ToString("X"));
   253             report.Append("Chip revision: 0x"); 
   254             report.AppendLine(revision.ToString("X"));
   255             report.AppendLine("Error: Address verification failed");
   256             report.AppendLine();
   257             return;
   258           }
   259 
   260           // some Fintek chips have address register offset 0x05 added already
   261           if ((address & 0x07) == 0x05)
   262             address &= 0xFFF8;
   263 
   264           if (address < 0x100 || (address & 0xF007) != 0) {            
   265             report.Append("Chip ID: 0x");
   266             report.AppendLine(chip.ToString("X"));
   267             report.Append("Chip revision: 0x");
   268             report.AppendLine(revision.ToString("X"));
   269             report.Append("Error: Invalid address 0x");
   270             report.AppendLine(address.ToString("X"));
   271             report.AppendLine();
   272             return;
   273           }
   274 
   275           switch (chip) {
   276             case Chip.W83627DHG:
   277             case Chip.W83627DHGP:
   278             case Chip.W83627EHF:
   279             case Chip.W83627HF:
   280             case Chip.W83627THF:
   281             case Chip.W83667HG:
   282             case Chip.W83667HGB:
   283             case Chip.W83687THF:
   284               W836XX w836XX = new W836XX(chip, revision, address);
   285               if (w836XX.IsAvailable)
   286                 hardware.Add(w836XX);
   287               break;
   288             case Chip.F71858:
   289             case Chip.F71862:
   290             case Chip.F71882:
   291             case Chip.F71889: 
   292             case Chip.F71869:
   293               hardware.Add(new F718XX(chip, address));
   294               break;
   295             default: break;
   296           }
   297           
   298           return;
   299         }
   300 
   301         IT87Enter();
   302 
   303         ushort chipID = ReadWord(CHIP_ID_REGISTER);
   304         switch (chipID) {
   305           case 0x8716: chip = Chip.IT8716F; break;
   306           case 0x8718: chip = Chip.IT8718F; break;
   307           case 0x8720: chip = Chip.IT8720F; break;
   308           case 0x8726: chip = Chip.IT8726F; break; 
   309           default: chip = Chip.Unknown; break;
   310         }
   311         if (chip == Chip.Unknown) {
   312           if (chipID != 0 && chipID != 0xffff) {
   313             IT87Exit();
   314 
   315             report.Append("Chip ID: Unknown ITE with ID 0x");
   316             report.AppendLine(chipID.ToString("X"));
   317             report.AppendLine();
   318           }
   319         } else {
   320           Select(IT87_ENVIRONMENT_CONTROLLER_LDN);
   321           ushort address = ReadWord(BASE_ADDRESS_REGISTER);
   322           Thread.Sleep(1);
   323           ushort verify = ReadWord(BASE_ADDRESS_REGISTER);
   324 
   325           IT87Exit();
   326 
   327           if (address != verify || address < 0x100 || (address & 0xF007) != 0) {
   328             report.Append("Chip ID: 0x");
   329             report.AppendLine(chip.ToString("X"));            
   330             report.Append("Error: Invalid address 0x");
   331             report.AppendLine(address.ToString("X"));
   332             report.AppendLine();
   333             return;
   334           }
   335 
   336           IT87XX it87 = new IT87XX(chip, address);
   337           if (it87.IsAvailable)
   338             hardware.Add(it87);
   339 
   340           return;
   341         }
   342 
   343         SMSCEnter();
   344 
   345         chipID = ReadWord(CHIP_ID_REGISTER);
   346         switch (chipID) {
   347           default: chip = Chip.Unknown; break;
   348         }
   349         if (chip == Chip.Unknown) {
   350           if (chipID != 0 && chipID != 0xffff) {
   351             SMSCExit();
   352 
   353             report.Append("Chip ID: Unknown SMSC with ID 0x");
   354             report.AppendLine(chipID.ToString("X"));
   355             report.AppendLine();
   356           }
   357         } else {
   358           SMSCExit();
   359 
   360           return;
   361         }
   362       }   
   363     }
   364 
   365     public IHardware[] Hardware {
   366       get {
   367         return hardware.ToArray();
   368       }
   369     }
   370 
   371     public string GetReport() {
   372       if (report.Length > 0) {
   373         report.Insert(0, "LPCIO" + Environment.NewLine +
   374           Environment.NewLine);        
   375         return report.ToString();
   376       } else
   377         return null;
   378     }
   379 
   380     public void Close() { }
   381   }
   382 }