Hardware/LPC/LPCIO.cs
author moel.mich
Sun, 08 Aug 2010 13:57:26 +0000
changeset 165 813d8bc3192f
parent 163 67be1c62f950
child 166 fa9dfbfc4145
permissions -rw-r--r--
Refactored the hardware monitoring code into a library (Issue 101).
     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   internal class LPCIO {
    45 
    46     private List<ISuperIO> superIOs = new List<ISuperIO>();
    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     private const byte IT87_CHIP_VERSION_REGISTER = 0x22;
    83 
    84     private void IT87Enter() {
    85       WinRing0.WriteIoPortByte(registerPort, 0x87);
    86       WinRing0.WriteIoPortByte(registerPort, 0x01);
    87       WinRing0.WriteIoPortByte(registerPort, 0x55);
    88       WinRing0.WriteIoPortByte(registerPort, 0x55);
    89     }
    90 
    91     private void IT87Exit() {
    92       WinRing0.WriteIoPortByte(registerPort, CONFIGURATION_CONTROL_REGISTER);
    93       WinRing0.WriteIoPortByte(valuePort, 0x02);
    94     }
    95 
    96     // Winbond, Fintek
    97     private const byte FINTEK_VENDOR_ID_REGISTER = 0x23;
    98     private const ushort FINTEK_VENDOR_ID = 0x1934;
    99 
   100     private const byte WINBOND_HARDWARE_MONITOR_LDN = 0x0B;
   101 
   102     private const byte F71858_HARDWARE_MONITOR_LDN = 0x02;
   103     private const byte FINTEK_HARDWARE_MONITOR_LDN = 0x04;
   104 
   105     private void WinbondFintekEnter() {
   106       WinRing0.WriteIoPortByte(registerPort, 0x87);
   107       WinRing0.WriteIoPortByte(registerPort, 0x87);
   108     }
   109 
   110     private void WinbondFintekExit() {
   111       WinRing0.WriteIoPortByte(registerPort, 0xAA);      
   112     }
   113 
   114     // SMSC
   115     private void SMSCEnter() {
   116       WinRing0.WriteIoPortByte(registerPort, 0x55);
   117     }
   118 
   119     private void SMSCExit() {
   120       WinRing0.WriteIoPortByte(registerPort, 0xAA);
   121     }
   122 
   123     private void Detect() {
   124 
   125       for (int i = 0; i < REGISTER_PORTS.Length; i++) {
   126         registerPort = REGISTER_PORTS[i];
   127         valuePort = VALUE_PORTS[i];
   128 
   129         WinbondFintekEnter();
   130 
   131         byte logicalDeviceNumber;
   132         byte id = ReadByte(CHIP_ID_REGISTER);
   133         byte revision = ReadByte(CHIP_REVISION_REGISTER);
   134         chip = Chip.Unknown;
   135         logicalDeviceNumber = 0;
   136         switch (id) {
   137           case 0x05:
   138             switch (revision) {
   139               case 0x07:
   140                 chip = Chip.F71858;
   141                 logicalDeviceNumber = F71858_HARDWARE_MONITOR_LDN;
   142                 break;
   143               case 0x41:
   144                 chip = Chip.F71882;
   145                 logicalDeviceNumber = FINTEK_HARDWARE_MONITOR_LDN;
   146                 break;
   147             } break;
   148           case 0x06:
   149             switch (revision) {
   150               case 0x01:
   151                 chip = Chip.F71862;
   152                 logicalDeviceNumber = FINTEK_HARDWARE_MONITOR_LDN;
   153                 break;
   154             } break;
   155           case 0x07:
   156             switch (revision) {
   157               case 0x23:
   158                 chip = Chip.F71889F;
   159                 logicalDeviceNumber = FINTEK_HARDWARE_MONITOR_LDN;
   160                 break;
   161             } break;
   162           case 0x08:
   163             switch (revision) {
   164               case 0x14:
   165                 chip = Chip.F71869;
   166                 logicalDeviceNumber = FINTEK_HARDWARE_MONITOR_LDN;
   167                 break;
   168             } break;
   169           case 0x09:
   170             switch (revision) {
   171               case 0x09:
   172                 chip = Chip.F71889ED;
   173                 logicalDeviceNumber = FINTEK_HARDWARE_MONITOR_LDN;
   174                 break;
   175             } break;
   176           case 0x52:
   177             switch (revision) {
   178               case 0x17:
   179               case 0x3A:
   180               case 0x41:
   181                 chip = Chip.W83627HF;
   182                 logicalDeviceNumber = WINBOND_HARDWARE_MONITOR_LDN;
   183                 break;
   184             } break;
   185           case 0x82:
   186             switch (revision & 0xF0) {
   187               case 0x80:
   188                 chip = Chip.W83627THF;
   189                 logicalDeviceNumber = WINBOND_HARDWARE_MONITOR_LDN;
   190                 break;
   191             } break;
   192           case 0x85:
   193             switch (revision) {
   194               case 0x41:
   195                 chip = Chip.W83687THF;
   196                 logicalDeviceNumber = WINBOND_HARDWARE_MONITOR_LDN;
   197                 break;
   198             } break;
   199           case 0x88:
   200             switch (revision & 0xF0) {
   201               case 0x50:
   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               superIOs.Add(new W836XX(chip, revision, address));
   290               break;
   291             case Chip.F71858:
   292             case Chip.F71862:
   293             case Chip.F71869:
   294             case Chip.F71882:
   295             case Chip.F71889ED:
   296             case Chip.F71889F:
   297               if (vendorID != FINTEK_VENDOR_ID) {
   298                 report.Append("Chip ID: 0x");
   299                 report.AppendLine(chip.ToString("X"));
   300                 report.Append("Chip revision: 0x");
   301                 report.AppendLine(revision.ToString("X"));
   302                 report.Append("Error: Invalid vendor ID 0x");
   303                 report.AppendLine(vendorID.ToString("X"));
   304                 report.AppendLine();
   305                 return;
   306               }
   307               superIOs.Add(new F718XX(chip, address));
   308               break;
   309             default: break;
   310           }
   311 
   312           return;
   313         }
   314 
   315         IT87Enter();
   316 
   317         ushort chipID = ReadWord(CHIP_ID_REGISTER);
   318         switch (chipID) {
   319           case 0x8712: chip = Chip.IT8712F; break;
   320           case 0x8716: chip = Chip.IT8716F; break;
   321           case 0x8718: chip = Chip.IT8718F; break;
   322           case 0x8720: chip = Chip.IT8720F; break;
   323           case 0x8726: chip = Chip.IT8726F; break;
   324           default: chip = Chip.Unknown; break;
   325         }
   326         if (chip == Chip.Unknown) {
   327           if (chipID != 0 && chipID != 0xffff) {
   328             IT87Exit();
   329 
   330             report.Append("Chip ID: Unknown ITE with ID 0x");
   331             report.AppendLine(chipID.ToString("X"));
   332             report.AppendLine();
   333           }
   334         } else {
   335           Select(IT87_ENVIRONMENT_CONTROLLER_LDN);
   336           ushort address = ReadWord(BASE_ADDRESS_REGISTER);
   337           Thread.Sleep(1);
   338           ushort verify = ReadWord(BASE_ADDRESS_REGISTER);
   339 
   340           byte version = (byte)(ReadByte(IT87_CHIP_VERSION_REGISTER) & 0x0F);
   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           superIOs.Add(new IT87XX(chip, address, version));
   354 
   355           return;
   356         }
   357 
   358         SMSCEnter();
   359 
   360         chipID = ReadWord(CHIP_ID_REGISTER);
   361         switch (chipID) {
   362           default: chip = Chip.Unknown; break;
   363         }
   364         if (chip == Chip.Unknown) {
   365           if (chipID != 0 && chipID != 0xffff) {
   366             SMSCExit();
   367 
   368             report.Append("Chip ID: Unknown SMSC with ID 0x");
   369             report.AppendLine(chipID.ToString("X"));
   370             report.AppendLine();
   371           }
   372         } else {
   373           SMSCExit();
   374 
   375           return;
   376         }
   377       }  
   378     }
   379 
   380     public LPCIO() {
   381       if (!WinRing0.IsAvailable)
   382         return;
   383 
   384       if (!WinRing0.WaitIsaBusMutex(100))
   385         return;
   386 
   387       Detect();
   388 
   389       WinRing0.ReleaseIsaBusMutex();      
   390     }
   391 
   392     public ISuperIO[] SuperIO {
   393       get {
   394         return superIOs.ToArray();
   395       }
   396     }
   397 
   398     public string GetReport() {
   399       if (report.Length > 0) {
   400         report.Insert(0, "LPCIO" + Environment.NewLine +
   401           Environment.NewLine);        
   402         return report.ToString();
   403       } else
   404         return null;
   405     }
   406   }
   407 }