Hardware/LPC/LPCGroup.cs
author moel.mich
Tue, 27 Apr 2010 18:43:56 +0000
changeset 103 0845adc5402e
parent 93 99fbbbf50357
child 108 4f569432e14b
permissions -rw-r--r--
Added support for F71889ED 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-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.F71889F;
   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 0x09:
   171             switch (revision) {
   172               case 0x09:
   173                 chip = Chip.F71889ED;
   174                 logicalDeviceNumber = FINTEK_HARDWARE_MONITOR_LDN;
   175                 break;              
   176             } break;
   177           case 0x52:
   178             switch (revision) {
   179               case 0x17:
   180               case 0x3A:
   181               case 0x41:
   182                 chip = Chip.W83627HF;
   183                 logicalDeviceNumber = WINBOND_HARDWARE_MONITOR_LDN;
   184                 break;             
   185             } break;
   186           case 0x82:
   187             switch (revision) {
   188               case 0x83:
   189                 chip = Chip.W83627THF;
   190                 logicalDeviceNumber = WINBOND_HARDWARE_MONITOR_LDN;
   191                 break;
   192             } break;
   193           case 0x85:
   194             switch (revision) {
   195               case 0x41:
   196                 chip = Chip.W83687THF;
   197                 logicalDeviceNumber = WINBOND_HARDWARE_MONITOR_LDN;
   198                 break;
   199             } break;
   200           case 0x88:
   201             switch (revision & 0xF0) {
   202               case 0x60:
   203                 chip = Chip.W83627EHF;
   204                 logicalDeviceNumber = WINBOND_HARDWARE_MONITOR_LDN;
   205                 break;
   206             } break;
   207           case 0xA0:
   208             switch (revision & 0xF0) {
   209               case 0x20: 
   210                 chip = Chip.W83627DHG;
   211                 logicalDeviceNumber = WINBOND_HARDWARE_MONITOR_LDN;  
   212                 break;             
   213             } break;
   214           case 0xA5:
   215             switch (revision & 0xF0) {
   216               case 0x10:
   217                 chip = Chip.W83667HG;
   218                 logicalDeviceNumber = WINBOND_HARDWARE_MONITOR_LDN;
   219                 break;
   220             } break;
   221           case 0xB0:
   222             switch (revision & 0xF0) {
   223               case 0x70:
   224                 chip = Chip.W83627DHGP;
   225                 logicalDeviceNumber = WINBOND_HARDWARE_MONITOR_LDN;
   226                 break;             
   227             } break;
   228           case 0xB3:
   229             switch (revision & 0xF0) {
   230               case 0x50:
   231                 chip = Chip.W83667HGB;
   232                 logicalDeviceNumber = WINBOND_HARDWARE_MONITOR_LDN;
   233                 break;
   234             } break; 
   235         }
   236         if (chip == Chip.Unknown) {
   237           if (id != 0 && id != 0xff) {
   238             WinbondFintekExit();
   239 
   240             report.Append("Chip ID: Unknown Winbond / Fintek with ID 0x"); 
   241             report.AppendLine(((id << 8) | revision).ToString("X"));
   242             report.AppendLine();
   243           }
   244         } else {
   245 
   246           Select(logicalDeviceNumber);
   247           ushort address = ReadWord(BASE_ADDRESS_REGISTER);          
   248           Thread.Sleep(1);
   249           ushort verify = ReadWord(BASE_ADDRESS_REGISTER);
   250 
   251           ushort vendorID = ReadWord(FINTEK_VENDOR_ID_REGISTER);
   252 
   253           WinbondFintekExit();
   254 
   255           if (address != verify) {            
   256             report.Append("Chip ID: 0x"); 
   257             report.AppendLine(chip.ToString("X"));
   258             report.Append("Chip revision: 0x"); 
   259             report.AppendLine(revision.ToString("X"));
   260             report.AppendLine("Error: Address verification failed");
   261             report.AppendLine();
   262             return;
   263           }
   264 
   265           // some Fintek chips have address register offset 0x05 added already
   266           if ((address & 0x07) == 0x05)
   267             address &= 0xFFF8;
   268 
   269           if (address < 0x100 || (address & 0xF007) != 0) {            
   270             report.Append("Chip ID: 0x");
   271             report.AppendLine(chip.ToString("X"));
   272             report.Append("Chip revision: 0x");
   273             report.AppendLine(revision.ToString("X"));
   274             report.Append("Error: Invalid address 0x");
   275             report.AppendLine(address.ToString("X"));
   276             report.AppendLine();
   277             return;
   278           }
   279 
   280           switch (chip) {
   281             case Chip.W83627DHG:
   282             case Chip.W83627DHGP:
   283             case Chip.W83627EHF:
   284             case Chip.W83627HF:
   285             case Chip.W83627THF:
   286             case Chip.W83667HG:
   287             case Chip.W83667HGB:
   288             case Chip.W83687THF:
   289               W836XX w836XX = new W836XX(chip, revision, address);
   290               if (w836XX.IsAvailable)
   291                 hardware.Add(w836XX);
   292               break;
   293             case Chip.F71858:
   294             case Chip.F71862:
   295             case Chip.F71869:
   296             case Chip.F71882:
   297             case Chip.F71889ED:
   298             case Chip.F71889F:
   299               if (vendorID != FINTEK_VENDOR_ID) {
   300                 report.Append("Chip ID: 0x");
   301                 report.AppendLine(chip.ToString("X"));
   302                 report.Append("Chip revision: 0x");
   303                 report.AppendLine(revision.ToString("X"));
   304                 report.Append("Error: Invalid vendor ID 0x");
   305                 report.AppendLine(vendorID.ToString("X"));
   306                 report.AppendLine();
   307                 return;
   308               }
   309               hardware.Add(new F718XX(chip, address));
   310               break;
   311             default: break;
   312           }
   313           
   314           return;
   315         }
   316 
   317         IT87Enter();
   318 
   319         ushort chipID = ReadWord(CHIP_ID_REGISTER);
   320         switch (chipID) {
   321           case 0x8712: chip = Chip.IT8712F; break;
   322           case 0x8716: chip = Chip.IT8716F; break;
   323           case 0x8718: chip = Chip.IT8718F; break;
   324           case 0x8720: chip = Chip.IT8720F; break;
   325           case 0x8726: chip = Chip.IT8726F; break; 
   326           default: chip = Chip.Unknown; break;
   327         }
   328         if (chip == Chip.Unknown) {
   329           if (chipID != 0 && chipID != 0xffff) {
   330             IT87Exit();
   331 
   332             report.Append("Chip ID: Unknown ITE with ID 0x");
   333             report.AppendLine(chipID.ToString("X"));
   334             report.AppendLine();
   335           }
   336         } else {
   337           Select(IT87_ENVIRONMENT_CONTROLLER_LDN);
   338           ushort address = ReadWord(BASE_ADDRESS_REGISTER);
   339           Thread.Sleep(1);
   340           ushort verify = ReadWord(BASE_ADDRESS_REGISTER);
   341 
   342           IT87Exit();
   343 
   344           if (address != verify || address < 0x100 || (address & 0xF007) != 0) {
   345             report.Append("Chip ID: 0x");
   346             report.AppendLine(chip.ToString("X"));            
   347             report.Append("Error: Invalid address 0x");
   348             report.AppendLine(address.ToString("X"));
   349             report.AppendLine();
   350             return;
   351           }
   352 
   353           IT87XX it87 = new IT87XX(chip, address);
   354           if (it87.IsAvailable)
   355             hardware.Add(it87);
   356 
   357           return;
   358         }
   359 
   360         SMSCEnter();
   361 
   362         chipID = ReadWord(CHIP_ID_REGISTER);
   363         switch (chipID) {
   364           default: chip = Chip.Unknown; break;
   365         }
   366         if (chip == Chip.Unknown) {
   367           if (chipID != 0 && chipID != 0xffff) {
   368             SMSCExit();
   369 
   370             report.Append("Chip ID: Unknown SMSC with ID 0x");
   371             report.AppendLine(chipID.ToString("X"));
   372             report.AppendLine();
   373           }
   374         } else {
   375           SMSCExit();
   376 
   377           return;
   378         }
   379       }   
   380     }
   381 
   382     public IHardware[] Hardware {
   383       get {
   384         return hardware.ToArray();
   385       }
   386     }
   387 
   388     public string GetReport() {
   389       if (report.Length > 0) {
   390         report.Insert(0, "LPCIO" + Environment.NewLine +
   391           Environment.NewLine);        
   392         return report.ToString();
   393       } else
   394         return null;
   395     }
   396 
   397     public void Close() { }
   398   }
   399 }