Hardware/LPC/LPCIO.cs
author moel.mich
Tue, 02 Aug 2011 21:05:17 +0000
changeset 319 35cda448fc5f
parent 296 414783d1cda9
child 341 5172a92c5c20
permissions -rw-r--r--
Added initial support for the ITE IT8772E super I/O chip.
     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-2011
    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.Globalization;
    41 using System.Text;
    42 using System.Threading;
    43 
    44 namespace OpenHardwareMonitor.Hardware.LPC {
    45   internal class LPCIO {
    46 
    47     private readonly List<ISuperIO> superIOs = new List<ISuperIO>();
    48     private readonly StringBuilder report = new StringBuilder();
    49 
    50     // I/O Ports
    51     private readonly ushort[] REGISTER_PORTS = new ushort[] { 0x2E, 0x4E };
    52     private readonly ushort[] VALUE_PORTS = new ushort[] { 0x2F, 0x4F };
    53 
    54     private ushort registerPort;
    55     private ushort valuePort;
    56 
    57     // Registers
    58     private const byte CONFIGURATION_CONTROL_REGISTER = 0x02;
    59     private const byte DEVCIE_SELECT_REGISTER = 0x07;
    60     private const byte CHIP_ID_REGISTER = 0x20;
    61     private const byte CHIP_REVISION_REGISTER = 0x21;
    62     private const byte BASE_ADDRESS_REGISTER = 0x60;
    63 
    64     private byte ReadByte(byte register) {
    65       Ring0.WriteIoPort(registerPort, register);
    66       return Ring0.ReadIoPort(valuePort);
    67     }
    68 
    69     private ushort ReadWord(byte register) {
    70       return (ushort)((ReadByte(register) << 8) |
    71         ReadByte((byte)(register + 1)));
    72     }
    73 
    74     private void Select(byte logicalDeviceNumber) {
    75       Ring0.WriteIoPort(registerPort, DEVCIE_SELECT_REGISTER);
    76       Ring0.WriteIoPort(valuePort, logicalDeviceNumber);
    77     }
    78 
    79     private void ReportUnknownChip(string type, int chip) {
    80       report.Append("Chip ID: Unknown ");
    81       report.Append(type);
    82       report.Append(" with ID 0x");
    83       report.Append(chip.ToString("X", CultureInfo.InvariantCulture));
    84       report.Append(" at 0x");
    85       report.Append(registerPort.ToString("X", CultureInfo.InvariantCulture));
    86       report.Append("/0x");
    87       report.AppendLine(valuePort.ToString("X", CultureInfo.InvariantCulture));
    88       report.AppendLine();
    89     }
    90 
    91     #region Winbond, Nuvoton, Fintek
    92 
    93     private const byte FINTEK_VENDOR_ID_REGISTER = 0x23;
    94     private const ushort FINTEK_VENDOR_ID = 0x1934;
    95 
    96     private const byte WINBOND_NUVOTON_HARDWARE_MONITOR_LDN = 0x0B;
    97 
    98     private const byte F71858_HARDWARE_MONITOR_LDN = 0x02;
    99     private const byte FINTEK_HARDWARE_MONITOR_LDN = 0x04;
   100 
   101     private void WinbondNuvotonFintekEnter() {
   102       Ring0.WriteIoPort(registerPort, 0x87);
   103       Ring0.WriteIoPort(registerPort, 0x87);
   104     }
   105 
   106     private void WinbondNuvotonFintekExit() {
   107       Ring0.WriteIoPort(registerPort, 0xAA);
   108     }
   109 
   110     private bool DetectWinbondFintek() {
   111       WinbondNuvotonFintekEnter();
   112 
   113       byte logicalDeviceNumber = 0;
   114       byte id = ReadByte(CHIP_ID_REGISTER);
   115       byte revision = ReadByte(CHIP_REVISION_REGISTER);
   116       Chip chip = Chip.Unknown;
   117       switch (id) {
   118         case 0x05:
   119           switch (revision) {
   120             case 0x07:
   121               chip = Chip.F71858;
   122               logicalDeviceNumber = F71858_HARDWARE_MONITOR_LDN;
   123               break;
   124             case 0x41:
   125               chip = Chip.F71882;
   126               logicalDeviceNumber = FINTEK_HARDWARE_MONITOR_LDN;
   127               break;
   128           } break;
   129         case 0x06:
   130           switch (revision) {
   131             case 0x01:
   132               chip = Chip.F71862;
   133               logicalDeviceNumber = FINTEK_HARDWARE_MONITOR_LDN;
   134               break;
   135           } break;
   136         case 0x07:
   137           switch (revision) {
   138             case 0x23:
   139               chip = Chip.F71889F;
   140               logicalDeviceNumber = FINTEK_HARDWARE_MONITOR_LDN;
   141               break;
   142           } break;
   143         case 0x08:
   144           switch (revision) {
   145             case 0x14:
   146               chip = Chip.F71869;
   147               logicalDeviceNumber = FINTEK_HARDWARE_MONITOR_LDN;
   148               break;
   149           } break;
   150         case 0x09:
   151           switch (revision) {
   152             case 0x09:
   153               chip = Chip.F71889ED;
   154               logicalDeviceNumber = FINTEK_HARDWARE_MONITOR_LDN;
   155               break;
   156           } break;
   157         case 0x10:
   158           switch (revision) {
   159             case 0x05:
   160               chip = Chip.F71889AD;
   161               logicalDeviceNumber = FINTEK_HARDWARE_MONITOR_LDN;
   162               break;
   163           } break;
   164         case 0x52:
   165           switch (revision) {
   166             case 0x17:
   167             case 0x3A:
   168             case 0x41:
   169               chip = Chip.W83627HF;
   170               logicalDeviceNumber = WINBOND_NUVOTON_HARDWARE_MONITOR_LDN;
   171               break;
   172           } break;
   173         case 0x82:
   174           switch (revision & 0xF0) {
   175             case 0x80:
   176               chip = Chip.W83627THF;
   177               logicalDeviceNumber = WINBOND_NUVOTON_HARDWARE_MONITOR_LDN;
   178               break;
   179           } break;
   180         case 0x85:
   181           switch (revision) {
   182             case 0x41:
   183               chip = Chip.W83687THF;
   184               logicalDeviceNumber = WINBOND_NUVOTON_HARDWARE_MONITOR_LDN;
   185               break;
   186           } break;
   187         case 0x88:
   188           switch (revision & 0xF0) {
   189             case 0x50:
   190             case 0x60:
   191               chip = Chip.W83627EHF;
   192               logicalDeviceNumber = WINBOND_NUVOTON_HARDWARE_MONITOR_LDN;
   193               break;
   194           } break;
   195         case 0xA0:
   196           switch (revision & 0xF0) {
   197             case 0x20:
   198               chip = Chip.W83627DHG;
   199               logicalDeviceNumber = WINBOND_NUVOTON_HARDWARE_MONITOR_LDN;
   200               break;
   201           } break;
   202         case 0xA5:
   203           switch (revision & 0xF0) {
   204             case 0x10:
   205               chip = Chip.W83667HG;
   206               logicalDeviceNumber = WINBOND_NUVOTON_HARDWARE_MONITOR_LDN;
   207               break;
   208           } break;
   209         case 0xB0:
   210           switch (revision & 0xF0) {
   211             case 0x70:
   212               chip = Chip.W83627DHGP;
   213               logicalDeviceNumber = WINBOND_NUVOTON_HARDWARE_MONITOR_LDN;
   214               break;
   215           } break;
   216         case 0xB3:
   217           switch (revision & 0xF0) {
   218             case 0x50:
   219               chip = Chip.W83667HGB;
   220               logicalDeviceNumber = WINBOND_NUVOTON_HARDWARE_MONITOR_LDN;
   221               break;
   222           } break;
   223         case 0xB4:
   224           switch (revision & 0xF0) {
   225             case 0x70:
   226               chip = Chip.NCT6771F;
   227               logicalDeviceNumber = WINBOND_NUVOTON_HARDWARE_MONITOR_LDN;
   228               break;
   229           } break;
   230         case 0xC3:
   231           switch (revision & 0xF0) {
   232             case 0x30:
   233               chip = Chip.NCT6776F;
   234               logicalDeviceNumber = WINBOND_NUVOTON_HARDWARE_MONITOR_LDN;
   235               break;
   236           } break;
   237       }
   238       if (chip == Chip.Unknown) {
   239         if (id != 0 && id != 0xff) {
   240           WinbondNuvotonFintekExit();
   241 
   242           ReportUnknownChip("Winbond / Nuvoton / Fintek", 
   243             ((id << 8) | revision));
   244         }
   245       } else {
   246 
   247         Select(logicalDeviceNumber);
   248         ushort address = ReadWord(BASE_ADDRESS_REGISTER);
   249         Thread.Sleep(1);
   250         ushort verify = ReadWord(BASE_ADDRESS_REGISTER);
   251 
   252         ushort vendorID = ReadWord(FINTEK_VENDOR_ID_REGISTER);
   253 
   254         WinbondNuvotonFintekExit();
   255 
   256         if (address != verify) {
   257           report.Append("Chip ID: 0x");
   258           report.AppendLine(chip.ToString("X"));
   259           report.Append("Chip revision: 0x");
   260           report.AppendLine(revision.ToString("X",
   261             CultureInfo.InvariantCulture));
   262           report.AppendLine("Error: Address verification failed");
   263           report.AppendLine();
   264           return false;
   265         }
   266 
   267         // some Fintek chips have address register offset 0x05 added already
   268         if ((address & 0x07) == 0x05)
   269           address &= 0xFFF8;
   270 
   271         if (address < 0x100 || (address & 0xF007) != 0) {
   272           report.Append("Chip ID: 0x");
   273           report.AppendLine(chip.ToString("X"));
   274           report.Append("Chip revision: 0x");
   275           report.AppendLine(revision.ToString("X",
   276             CultureInfo.InvariantCulture));
   277           report.Append("Error: Invalid address 0x");
   278           report.AppendLine(address.ToString("X",
   279             CultureInfo.InvariantCulture));
   280           report.AppendLine();
   281           return false;
   282         }
   283 
   284         switch (chip) {
   285           case Chip.W83627DHG:
   286           case Chip.W83627DHGP:
   287           case Chip.W83627EHF:
   288           case Chip.W83627HF:
   289           case Chip.W83627THF:
   290           case Chip.W83667HG:
   291           case Chip.W83667HGB:
   292           case Chip.W83687THF:
   293             superIOs.Add(new W836XX(chip, revision, address));
   294             break;
   295           case Chip.NCT6771F:
   296           case Chip.NCT6776F:
   297             superIOs.Add(new NCT677X(chip, revision, address));
   298             break;
   299           case Chip.F71858:
   300           case Chip.F71862:
   301           case Chip.F71869:
   302           case Chip.F71882:
   303           case Chip.F71889AD:
   304           case Chip.F71889ED:
   305           case Chip.F71889F:
   306             if (vendorID != FINTEK_VENDOR_ID) {
   307               report.Append("Chip ID: 0x");
   308               report.AppendLine(chip.ToString("X"));
   309               report.Append("Chip revision: 0x");
   310               report.AppendLine(revision.ToString("X",
   311                 CultureInfo.InvariantCulture));
   312               report.Append("Error: Invalid vendor ID 0x");
   313               report.AppendLine(vendorID.ToString("X",
   314                 CultureInfo.InvariantCulture));
   315               report.AppendLine();
   316               return false;
   317             }
   318             superIOs.Add(new F718XX(chip, address));
   319             break;
   320           default: break;
   321         }
   322 
   323         return true;
   324       }
   325 
   326       return false;
   327     }
   328 
   329     #endregion
   330 
   331     #region ITE
   332 
   333     private const byte IT87_ENVIRONMENT_CONTROLLER_LDN = 0x04;
   334     private const byte IT87_GPIO_LDN = 0x07;
   335     private const byte IT87_CHIP_VERSION_REGISTER = 0x22;
   336 
   337     private void IT87Enter() {
   338       Ring0.WriteIoPort(registerPort, 0x87);
   339       Ring0.WriteIoPort(registerPort, 0x01);
   340       Ring0.WriteIoPort(registerPort, 0x55);
   341       Ring0.WriteIoPort(registerPort, 0x55);
   342     }
   343 
   344     private void IT87Exit() {
   345       Ring0.WriteIoPort(registerPort, CONFIGURATION_CONTROL_REGISTER);
   346       Ring0.WriteIoPort(valuePort, 0x02);
   347     }
   348 
   349     private bool DetectIT87() {
   350 
   351       // IT87XX can enter only on port 0x2E
   352       if (registerPort != 0x2E)
   353         return false;
   354 
   355       IT87Enter();
   356 
   357       ushort chipID = ReadWord(CHIP_ID_REGISTER);
   358       Chip chip;
   359       switch (chipID) {
   360         case 0x8712: chip = Chip.IT8712F; break;
   361         case 0x8716: chip = Chip.IT8716F; break;
   362         case 0x8718: chip = Chip.IT8718F; break;
   363         case 0x8720: chip = Chip.IT8720F; break;
   364         case 0x8721: chip = Chip.IT8721F; break;
   365         case 0x8726: chip = Chip.IT8726F; break;
   366         case 0x8728: chip = Chip.IT8728F; break;
   367         case 0x8772: chip = Chip.IT8772E; break;
   368         default: chip = Chip.Unknown; break;
   369       }
   370       if (chip == Chip.Unknown) {
   371         if (chipID != 0 && chipID != 0xffff) {
   372           IT87Exit();
   373 
   374           ReportUnknownChip("ITE", chipID);
   375         }
   376       } else {
   377         Select(IT87_ENVIRONMENT_CONTROLLER_LDN);
   378         ushort address = ReadWord(BASE_ADDRESS_REGISTER);
   379         Thread.Sleep(1);
   380         ushort verify = ReadWord(BASE_ADDRESS_REGISTER);
   381 
   382         byte version = (byte)(ReadByte(IT87_CHIP_VERSION_REGISTER) & 0x0F);
   383 
   384         Select(IT87_GPIO_LDN);
   385         ushort gpioAddress = ReadWord(BASE_ADDRESS_REGISTER + 2);
   386         Thread.Sleep(1);
   387         ushort gpioVerify = ReadWord(BASE_ADDRESS_REGISTER + 2);
   388 
   389         IT87Exit();
   390 
   391         if (address != verify || address < 0x100 || (address & 0xF007) != 0) {
   392           report.Append("Chip ID: 0x");
   393           report.AppendLine(chip.ToString("X"));
   394           report.Append("Error: Invalid address 0x");
   395           report.AppendLine(address.ToString("X",
   396             CultureInfo.InvariantCulture));
   397           report.AppendLine();
   398           return false;
   399         }
   400 
   401         if (gpioAddress != gpioVerify || gpioAddress < 0x100 ||
   402           (gpioAddress & 0xF007) != 0) {
   403           report.Append("Chip ID: 0x");
   404           report.AppendLine(chip.ToString("X"));
   405           report.Append("Error: Invalid GPIO address 0x");
   406           report.AppendLine(gpioAddress.ToString("X",
   407             CultureInfo.InvariantCulture));
   408           report.AppendLine();
   409           return false;
   410         }
   411 
   412         superIOs.Add(new IT87XX(chip, address, gpioAddress, version));
   413         return true;
   414       }
   415 
   416       return false;
   417     }
   418 
   419     #endregion
   420 
   421     #region SMSC
   422 
   423     private void SMSCEnter() {
   424       Ring0.WriteIoPort(registerPort, 0x55);
   425     }
   426 
   427     private void SMSCExit() {
   428       Ring0.WriteIoPort(registerPort, 0xAA);
   429     }
   430 
   431     private bool DetectSMSC() {
   432       SMSCEnter();
   433 
   434       ushort chipID = ReadWord(CHIP_ID_REGISTER);
   435       Chip chip;
   436       switch (chipID) {
   437         default: chip = Chip.Unknown; break;
   438       }
   439       if (chip == Chip.Unknown) {
   440         if (chipID != 0 && chipID != 0xffff) {
   441           SMSCExit();
   442 
   443           ReportUnknownChip("SMSC", chipID);
   444         }
   445       } else {
   446         SMSCExit();
   447         return true;
   448       }
   449 
   450       return false;
   451     }
   452 
   453     #endregion
   454 
   455     private void Detect() {
   456 
   457       for (int i = 0; i < REGISTER_PORTS.Length; i++) {
   458         registerPort = REGISTER_PORTS[i];
   459         valuePort = VALUE_PORTS[i];
   460 
   461         if (DetectWinbondFintek()) continue;
   462 
   463         if (DetectIT87()) continue;
   464 
   465         if (DetectSMSC()) continue;
   466       }
   467     }
   468 
   469     public LPCIO() {
   470       if (!Ring0.IsOpen)
   471         return;
   472 
   473       if (!Ring0.WaitIsaBusMutex(100))
   474         return;
   475 
   476       Detect();
   477 
   478       Ring0.ReleaseIsaBusMutex();
   479     }
   480 
   481     public ISuperIO[] SuperIO {
   482       get {
   483         return superIOs.ToArray();
   484       }
   485     }
   486 
   487     public string GetReport() {
   488       if (report.Length > 0) {
   489         return "LPCIO" + Environment.NewLine + Environment.NewLine + report;
   490       } else
   491         return null;
   492     }
   493   }
   494 }