Hardware/LPC/LPCIO.cs
author moel.mich
Sun, 31 Oct 2010 22:08:47 +0000
changeset 236 763675f19ff4
parent 228 458a6c3de579
child 245 f8e72b2efcc0
permissions -rw-r--r--
Replaced the non-kernel code of WinRing0 with a managed implementation. The new implementation should fix Issue 32 and simplify further work on Issue 46.
     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.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, 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_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 WinbondFintekEnter() {
   102       Ring0.WriteIoPort(registerPort, 0x87);
   103       Ring0.WriteIoPort(registerPort, 0x87);
   104     }
   105 
   106     private void WinbondFintekExit() {
   107       Ring0.WriteIoPort(registerPort, 0xAA);
   108     }
   109 
   110     private bool DetectWinbondFintek() {
   111       WinbondFintekEnter();
   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 0x52:
   158           switch (revision) {
   159             case 0x17:
   160             case 0x3A:
   161             case 0x41:
   162               chip = Chip.W83627HF;
   163               logicalDeviceNumber = WINBOND_HARDWARE_MONITOR_LDN;
   164               break;
   165           } break;
   166         case 0x82:
   167           switch (revision & 0xF0) {
   168             case 0x80:
   169               chip = Chip.W83627THF;
   170               logicalDeviceNumber = WINBOND_HARDWARE_MONITOR_LDN;
   171               break;
   172           } break;
   173         case 0x85:
   174           switch (revision) {
   175             case 0x41:
   176               chip = Chip.W83687THF;
   177               logicalDeviceNumber = WINBOND_HARDWARE_MONITOR_LDN;
   178               break;
   179           } break;
   180         case 0x88:
   181           switch (revision & 0xF0) {
   182             case 0x50:
   183             case 0x60:
   184               chip = Chip.W83627EHF;
   185               logicalDeviceNumber = WINBOND_HARDWARE_MONITOR_LDN;
   186               break;
   187           } break;
   188         case 0xA0:
   189           switch (revision & 0xF0) {
   190             case 0x20:
   191               chip = Chip.W83627DHG;
   192               logicalDeviceNumber = WINBOND_HARDWARE_MONITOR_LDN;
   193               break;
   194           } break;
   195         case 0xA5:
   196           switch (revision & 0xF0) {
   197             case 0x10:
   198               chip = Chip.W83667HG;
   199               logicalDeviceNumber = WINBOND_HARDWARE_MONITOR_LDN;
   200               break;
   201           } break;
   202         case 0xB0:
   203           switch (revision & 0xF0) {
   204             case 0x70:
   205               chip = Chip.W83627DHGP;
   206               logicalDeviceNumber = WINBOND_HARDWARE_MONITOR_LDN;
   207               break;
   208           } break;
   209         case 0xB3:
   210           switch (revision & 0xF0) {
   211             case 0x50:
   212               chip = Chip.W83667HGB;
   213               logicalDeviceNumber = WINBOND_HARDWARE_MONITOR_LDN;
   214               break;
   215           } break;
   216       }
   217       if (chip == Chip.Unknown) {
   218         if (id != 0 && id != 0xff) {
   219           WinbondFintekExit();
   220 
   221           ReportUnknownChip("Winbond / Fintek", ((id << 8) | revision));
   222         }
   223       } else {
   224 
   225         Select(logicalDeviceNumber);
   226         ushort address = ReadWord(BASE_ADDRESS_REGISTER);
   227         Thread.Sleep(1);
   228         ushort verify = ReadWord(BASE_ADDRESS_REGISTER);
   229 
   230         ushort vendorID = ReadWord(FINTEK_VENDOR_ID_REGISTER);
   231 
   232         WinbondFintekExit();
   233 
   234         if (address != verify) {
   235           report.Append("Chip ID: 0x");
   236           report.AppendLine(chip.ToString("X"));
   237           report.Append("Chip revision: 0x");
   238           report.AppendLine(revision.ToString("X",
   239             CultureInfo.InvariantCulture));
   240           report.AppendLine("Error: Address verification failed");
   241           report.AppendLine();
   242           return false;
   243         }
   244 
   245         // some Fintek chips have address register offset 0x05 added already
   246         if ((address & 0x07) == 0x05)
   247           address &= 0xFFF8;
   248 
   249         if (address < 0x100 || (address & 0xF007) != 0) {
   250           report.Append("Chip ID: 0x");
   251           report.AppendLine(chip.ToString("X"));
   252           report.Append("Chip revision: 0x");
   253           report.AppendLine(revision.ToString("X",
   254             CultureInfo.InvariantCulture));
   255           report.Append("Error: Invalid address 0x");
   256           report.AppendLine(address.ToString("X",
   257             CultureInfo.InvariantCulture));
   258           report.AppendLine();
   259           return false;
   260         }
   261 
   262         switch (chip) {
   263           case Chip.W83627DHG:
   264           case Chip.W83627DHGP:
   265           case Chip.W83627EHF:
   266           case Chip.W83627HF:
   267           case Chip.W83627THF:
   268           case Chip.W83667HG:
   269           case Chip.W83667HGB:
   270           case Chip.W83687THF:
   271             superIOs.Add(new W836XX(chip, revision, address));
   272             break;
   273           case Chip.F71858:
   274           case Chip.F71862:
   275           case Chip.F71869:
   276           case Chip.F71882:
   277           case Chip.F71889ED:
   278           case Chip.F71889F:
   279             if (vendorID != FINTEK_VENDOR_ID) {
   280               report.Append("Chip ID: 0x");
   281               report.AppendLine(chip.ToString("X"));
   282               report.Append("Chip revision: 0x");
   283               report.AppendLine(revision.ToString("X",
   284                 CultureInfo.InvariantCulture));
   285               report.Append("Error: Invalid vendor ID 0x");
   286               report.AppendLine(vendorID.ToString("X",
   287                 CultureInfo.InvariantCulture));
   288               report.AppendLine();
   289               return false;
   290             }
   291             superIOs.Add(new F718XX(chip, address));
   292             break;
   293           default: break;
   294         }
   295 
   296         return true;
   297       }
   298 
   299       return false;
   300     }
   301 
   302     #endregion
   303 
   304     #region ITE
   305 
   306     private const byte IT87_ENVIRONMENT_CONTROLLER_LDN = 0x04;
   307     private const byte IT87_GPIO_LDN = 0x07;
   308     private const byte IT87_CHIP_VERSION_REGISTER = 0x22;
   309 
   310     private void IT87Enter() {
   311       Ring0.WriteIoPort(registerPort, 0x87);
   312       Ring0.WriteIoPort(registerPort, 0x01);
   313       Ring0.WriteIoPort(registerPort, 0x55);
   314       Ring0.WriteIoPort(registerPort, 0x55);
   315     }
   316 
   317     private void IT87Exit() {
   318       Ring0.WriteIoPort(registerPort, CONFIGURATION_CONTROL_REGISTER);
   319       Ring0.WriteIoPort(valuePort, 0x02);
   320     }
   321 
   322     private bool DetectIT87() {
   323 
   324       // IT87XX can enter only on port 0x2E
   325       if (registerPort != 0x2E)
   326         return false;
   327 
   328       IT87Enter();
   329 
   330       ushort chipID = ReadWord(CHIP_ID_REGISTER);
   331       Chip chip;
   332       switch (chipID) {
   333         case 0x8712: chip = Chip.IT8712F; break;
   334         case 0x8716: chip = Chip.IT8716F; break;
   335         case 0x8718: chip = Chip.IT8718F; break;
   336         case 0x8720: chip = Chip.IT8720F; break;
   337         case 0x8721: chip = Chip.IT8721F; break;
   338         case 0x8726: chip = Chip.IT8726F; break;
   339         default: chip = Chip.Unknown; break;
   340       }
   341       if (chip == Chip.Unknown) {
   342         if (chipID != 0 && chipID != 0xffff) {
   343           IT87Exit();
   344 
   345           ReportUnknownChip("ITE", chipID);
   346         }
   347       } else {
   348         Select(IT87_ENVIRONMENT_CONTROLLER_LDN);
   349         ushort address = ReadWord(BASE_ADDRESS_REGISTER);
   350         Thread.Sleep(1);
   351         ushort verify = ReadWord(BASE_ADDRESS_REGISTER);
   352 
   353         byte version = (byte)(ReadByte(IT87_CHIP_VERSION_REGISTER) & 0x0F);
   354 
   355         Select(IT87_GPIO_LDN);
   356         ushort gpioAddress = ReadWord(BASE_ADDRESS_REGISTER + 2);
   357         Thread.Sleep(1);
   358         ushort gpioVerify = ReadWord(BASE_ADDRESS_REGISTER + 2);
   359 
   360         IT87Exit();
   361 
   362         if (address != verify || address < 0x100 || (address & 0xF007) != 0) {
   363           report.Append("Chip ID: 0x");
   364           report.AppendLine(chip.ToString("X"));
   365           report.Append("Error: Invalid address 0x");
   366           report.AppendLine(address.ToString("X",
   367             CultureInfo.InvariantCulture));
   368           report.AppendLine();
   369           return false;
   370         }
   371 
   372         if (gpioAddress != gpioVerify || gpioAddress < 0x100 ||
   373           (gpioAddress & 0xF007) != 0) {
   374           report.Append("Chip ID: 0x");
   375           report.AppendLine(chip.ToString("X"));
   376           report.Append("Error: Invalid GPIO address 0x");
   377           report.AppendLine(gpioAddress.ToString("X",
   378             CultureInfo.InvariantCulture));
   379           report.AppendLine();
   380           return false;
   381         }
   382 
   383         superIOs.Add(new IT87XX(chip, address, gpioAddress, version));
   384         return true;
   385       }
   386 
   387       return false;
   388     }
   389 
   390     #endregion
   391 
   392     #region SMSC
   393 
   394     private void SMSCEnter() {
   395       Ring0.WriteIoPort(registerPort, 0x55);
   396     }
   397 
   398     private void SMSCExit() {
   399       Ring0.WriteIoPort(registerPort, 0xAA);
   400     }
   401 
   402     private bool DetectSMSC() {
   403       SMSCEnter();
   404 
   405       ushort chipID = ReadWord(CHIP_ID_REGISTER);
   406       Chip chip;
   407       switch (chipID) {
   408         default: chip = Chip.Unknown; break;
   409       }
   410       if (chip == Chip.Unknown) {
   411         if (chipID != 0 && chipID != 0xffff) {
   412           SMSCExit();
   413 
   414           ReportUnknownChip("SMSC", chipID);
   415         }
   416       } else {
   417         SMSCExit();
   418         return true;
   419       }
   420 
   421       return false;
   422     }
   423 
   424     #endregion
   425 
   426     private void Detect() {
   427 
   428       for (int i = 0; i < REGISTER_PORTS.Length; i++) {
   429         registerPort = REGISTER_PORTS[i];
   430         valuePort = VALUE_PORTS[i];
   431 
   432         if (DetectWinbondFintek()) continue;
   433 
   434         if (DetectIT87()) continue;
   435 
   436         if (DetectSMSC()) continue;
   437       }
   438     }
   439 
   440     public LPCIO() {
   441       if (!Ring0.IsOpen)
   442         return;
   443 
   444       if (!Ring0.WaitIsaBusMutex(100))
   445         return;
   446 
   447       Detect();
   448 
   449       Ring0.ReleaseIsaBusMutex();
   450     }
   451 
   452     public ISuperIO[] SuperIO {
   453       get {
   454         return superIOs.ToArray();
   455       }
   456     }
   457 
   458     public string GetReport() {
   459       if (report.Length > 0) {
   460         return "LPCIO" + Environment.NewLine + Environment.NewLine + report;
   461       } else
   462         return null;
   463     }
   464   }
   465 }