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