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