Hardware/LPC/LPCIO.cs
author moel.mich
Sun, 15 Aug 2010 17:56:57 +0000
changeset 168 7f90baeb96b0
parent 166 fa9dfbfc4145
child 169 3b2bcba8c02d
permissions -rw-r--r--
Fixed Issue 104.
moel@110
     1
/*
moel@110
     2
  
moel@110
     3
  Version: MPL 1.1/GPL 2.0/LGPL 2.1
moel@110
     4
moel@110
     5
  The contents of this file are subject to the Mozilla Public License Version
moel@110
     6
  1.1 (the "License"); you may not use this file except in compliance with
moel@110
     7
  the License. You may obtain a copy of the License at
moel@110
     8
 
moel@110
     9
  http://www.mozilla.org/MPL/
moel@110
    10
moel@110
    11
  Software distributed under the License is distributed on an "AS IS" basis,
moel@110
    12
  WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
moel@110
    13
  for the specific language governing rights and limitations under the License.
moel@110
    14
moel@110
    15
  The Original Code is the Open Hardware Monitor code.
moel@110
    16
moel@110
    17
  The Initial Developer of the Original Code is 
moel@110
    18
  Michael Möller <m.moeller@gmx.ch>.
moel@110
    19
  Portions created by the Initial Developer are Copyright (C) 2009-2010
moel@110
    20
  the Initial Developer. All Rights Reserved.
moel@110
    21
moel@110
    22
  Contributor(s):
moel@110
    23
moel@110
    24
  Alternatively, the contents of this file may be used under the terms of
moel@110
    25
  either the GNU General Public License Version 2 or later (the "GPL"), or
moel@110
    26
  the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
moel@110
    27
  in which case the provisions of the GPL or the LGPL are applicable instead
moel@110
    28
  of those above. If you wish to allow use of your version of this file only
moel@110
    29
  under the terms of either the GPL or the LGPL, and not to allow others to
moel@110
    30
  use your version of this file under the terms of the MPL, indicate your
moel@110
    31
  decision by deleting the provisions above and replace them with the notice
moel@110
    32
  and other provisions required by the GPL or the LGPL. If you do not delete
moel@110
    33
  the provisions above, a recipient may use your version of this file under
moel@110
    34
  the terms of any one of the MPL, the GPL or the LGPL.
moel@110
    35
 
moel@110
    36
*/
moel@110
    37
moel@110
    38
using System;
moel@110
    39
using System.Collections.Generic;
moel@166
    40
using System.Globalization;
moel@110
    41
using System.Text;
moel@110
    42
using System.Threading;
moel@110
    43
moel@110
    44
namespace OpenHardwareMonitor.Hardware.LPC {
moel@165
    45
  internal class LPCIO {
moel@110
    46
moel@130
    47
    private List<ISuperIO> superIOs = new List<ISuperIO>();
moel@110
    48
    private StringBuilder report = new StringBuilder();
moel@110
    49
moel@110
    50
    // I/O Ports
moel@110
    51
    private ushort[] REGISTER_PORTS = new ushort[] { 0x2e, 0x4e };
moel@110
    52
    private ushort[] VALUE_PORTS = new ushort[] { 0x2f, 0x4f };
moel@110
    53
moel@110
    54
    private ushort registerPort;
moel@110
    55
    private ushort valuePort;
moel@110
    56
moel@110
    57
    // Registers
moel@110
    58
    private const byte CONFIGURATION_CONTROL_REGISTER = 0x02;
moel@110
    59
    private const byte DEVCIE_SELECT_REGISTER = 0x07;
moel@110
    60
    private const byte CHIP_ID_REGISTER = 0x20;
moel@110
    61
    private const byte CHIP_REVISION_REGISTER = 0x21;
moel@110
    62
    private const byte BASE_ADDRESS_REGISTER = 0x60;
moel@110
    63
moel@110
    64
    private byte ReadByte(byte register) {
moel@110
    65
      WinRing0.WriteIoPortByte(registerPort, register);
moel@110
    66
      return WinRing0.ReadIoPortByte(valuePort);
moel@110
    67
    } 
moel@110
    68
moel@110
    69
    private ushort ReadWord(byte register) {
moel@110
    70
      return (ushort)((ReadByte(register) << 8) | 
moel@110
    71
        ReadByte((byte)(register + 1)));
moel@110
    72
    }
moel@110
    73
moel@110
    74
    private void Select(byte logicalDeviceNumber) {
moel@110
    75
      WinRing0.WriteIoPortByte(registerPort, DEVCIE_SELECT_REGISTER);
moel@110
    76
      WinRing0.WriteIoPortByte(valuePort, logicalDeviceNumber);
moel@110
    77
    }
moel@110
    78
moel@110
    79
    // ITE
moel@145
    80
    private const byte IT87_ENVIRONMENT_CONTROLLER_LDN = 0x04;
moel@145
    81
    private const byte IT87_CHIP_VERSION_REGISTER = 0x22;
moel@110
    82
moel@110
    83
    private void IT87Enter() {
moel@110
    84
      WinRing0.WriteIoPortByte(registerPort, 0x87);
moel@110
    85
      WinRing0.WriteIoPortByte(registerPort, 0x01);
moel@110
    86
      WinRing0.WriteIoPortByte(registerPort, 0x55);
moel@110
    87
      WinRing0.WriteIoPortByte(registerPort, 0x55);
moel@110
    88
    }
moel@110
    89
moel@130
    90
    private void IT87Exit() {
moel@110
    91
      WinRing0.WriteIoPortByte(registerPort, CONFIGURATION_CONTROL_REGISTER);
moel@110
    92
      WinRing0.WriteIoPortByte(valuePort, 0x02);
moel@110
    93
    }
moel@110
    94
moel@110
    95
    // Winbond, Fintek
moel@110
    96
    private const byte FINTEK_VENDOR_ID_REGISTER = 0x23;
moel@110
    97
    private const ushort FINTEK_VENDOR_ID = 0x1934;
moel@110
    98
moel@110
    99
    private const byte WINBOND_HARDWARE_MONITOR_LDN = 0x0B;
moel@110
   100
moel@110
   101
    private const byte F71858_HARDWARE_MONITOR_LDN = 0x02;
moel@110
   102
    private const byte FINTEK_HARDWARE_MONITOR_LDN = 0x04;
moel@110
   103
moel@110
   104
    private void WinbondFintekEnter() {
moel@110
   105
      WinRing0.WriteIoPortByte(registerPort, 0x87);
moel@110
   106
      WinRing0.WriteIoPortByte(registerPort, 0x87);
moel@110
   107
    }
moel@110
   108
moel@110
   109
    private void WinbondFintekExit() {
moel@110
   110
      WinRing0.WriteIoPortByte(registerPort, 0xAA);      
moel@110
   111
    }
moel@110
   112
moel@110
   113
    // SMSC
moel@110
   114
    private void SMSCEnter() {
moel@110
   115
      WinRing0.WriteIoPortByte(registerPort, 0x55);
moel@110
   116
    }
moel@110
   117
moel@110
   118
    private void SMSCExit() {
moel@110
   119
      WinRing0.WriteIoPortByte(registerPort, 0xAA);
moel@110
   120
    }
moel@110
   121
moel@167
   122
    private void ReportUnknownChip(string type, int chip) {
moel@167
   123
      report.Append("Chip ID: Unknown ");
moel@167
   124
      report.Append(type);
moel@167
   125
      report.Append(" with ID 0x");
moel@167
   126
      report.Append(chip.ToString("X", CultureInfo.InvariantCulture));
moel@167
   127
      report.Append(" at 0x");
moel@167
   128
      report.Append(registerPort.ToString("X", CultureInfo.InvariantCulture));
moel@167
   129
      report.Append("/0x");
moel@167
   130
      report.AppendLine(valuePort.ToString("X", CultureInfo.InvariantCulture));
moel@167
   131
      report.AppendLine();
moel@167
   132
    }
moel@167
   133
moel@167
   134
    private bool DetectWinbondFintek() {
moel@167
   135
      WinbondFintekEnter();
moel@167
   136
moel@167
   137
      byte logicalDeviceNumber;
moel@167
   138
      byte id = ReadByte(CHIP_ID_REGISTER);
moel@167
   139
      byte revision = ReadByte(CHIP_REVISION_REGISTER);
moel@167
   140
      Chip chip = Chip.Unknown;
moel@167
   141
      logicalDeviceNumber = 0;
moel@167
   142
      switch (id) {
moel@167
   143
        case 0x05:
moel@167
   144
          switch (revision) {
moel@167
   145
            case 0x07:
moel@167
   146
              chip = Chip.F71858;
moel@167
   147
              logicalDeviceNumber = F71858_HARDWARE_MONITOR_LDN;
moel@167
   148
              break;
moel@167
   149
            case 0x41:
moel@167
   150
              chip = Chip.F71882;
moel@167
   151
              logicalDeviceNumber = FINTEK_HARDWARE_MONITOR_LDN;
moel@167
   152
              break;
moel@167
   153
          } break;
moel@167
   154
        case 0x06:
moel@167
   155
          switch (revision) {
moel@167
   156
            case 0x01:
moel@167
   157
              chip = Chip.F71862;
moel@167
   158
              logicalDeviceNumber = FINTEK_HARDWARE_MONITOR_LDN;
moel@167
   159
              break;
moel@167
   160
          } break;
moel@167
   161
        case 0x07:
moel@167
   162
          switch (revision) {
moel@167
   163
            case 0x23:
moel@167
   164
              chip = Chip.F71889F;
moel@167
   165
              logicalDeviceNumber = FINTEK_HARDWARE_MONITOR_LDN;
moel@167
   166
              break;
moel@167
   167
          } break;
moel@167
   168
        case 0x08:
moel@167
   169
          switch (revision) {
moel@167
   170
            case 0x14:
moel@167
   171
              chip = Chip.F71869;
moel@167
   172
              logicalDeviceNumber = FINTEK_HARDWARE_MONITOR_LDN;
moel@167
   173
              break;
moel@167
   174
          } break;
moel@167
   175
        case 0x09:
moel@167
   176
          switch (revision) {
moel@167
   177
            case 0x09:
moel@167
   178
              chip = Chip.F71889ED;
moel@167
   179
              logicalDeviceNumber = FINTEK_HARDWARE_MONITOR_LDN;
moel@167
   180
              break;
moel@167
   181
          } break;
moel@167
   182
        case 0x52:
moel@167
   183
          switch (revision) {
moel@167
   184
            case 0x17:
moel@167
   185
            case 0x3A:
moel@167
   186
            case 0x41:
moel@167
   187
              chip = Chip.W83627HF;
moel@167
   188
              logicalDeviceNumber = WINBOND_HARDWARE_MONITOR_LDN;
moel@167
   189
              break;
moel@167
   190
          } break;
moel@167
   191
        case 0x82:
moel@167
   192
          switch (revision & 0xF0) {
moel@167
   193
            case 0x80:
moel@167
   194
              chip = Chip.W83627THF;
moel@167
   195
              logicalDeviceNumber = WINBOND_HARDWARE_MONITOR_LDN;
moel@167
   196
              break;
moel@167
   197
          } break;
moel@167
   198
        case 0x85:
moel@167
   199
          switch (revision) {
moel@167
   200
            case 0x41:
moel@167
   201
              chip = Chip.W83687THF;
moel@167
   202
              logicalDeviceNumber = WINBOND_HARDWARE_MONITOR_LDN;
moel@167
   203
              break;
moel@167
   204
          } break;
moel@167
   205
        case 0x88:
moel@167
   206
          switch (revision & 0xF0) {
moel@167
   207
            case 0x50:
moel@167
   208
            case 0x60:
moel@167
   209
              chip = Chip.W83627EHF;
moel@167
   210
              logicalDeviceNumber = WINBOND_HARDWARE_MONITOR_LDN;
moel@167
   211
              break;
moel@167
   212
          } break;
moel@167
   213
        case 0xA0:
moel@167
   214
          switch (revision & 0xF0) {
moel@167
   215
            case 0x20:
moel@167
   216
              chip = Chip.W83627DHG;
moel@167
   217
              logicalDeviceNumber = WINBOND_HARDWARE_MONITOR_LDN;
moel@167
   218
              break;
moel@167
   219
          } break;
moel@167
   220
        case 0xA5:
moel@167
   221
          switch (revision & 0xF0) {
moel@167
   222
            case 0x10:
moel@167
   223
              chip = Chip.W83667HG;
moel@167
   224
              logicalDeviceNumber = WINBOND_HARDWARE_MONITOR_LDN;
moel@167
   225
              break;
moel@167
   226
          } break;
moel@167
   227
        case 0xB0:
moel@167
   228
          switch (revision & 0xF0) {
moel@167
   229
            case 0x70:
moel@167
   230
              chip = Chip.W83627DHGP;
moel@167
   231
              logicalDeviceNumber = WINBOND_HARDWARE_MONITOR_LDN;
moel@167
   232
              break;
moel@167
   233
          } break;
moel@167
   234
        case 0xB3:
moel@167
   235
          switch (revision & 0xF0) {
moel@167
   236
            case 0x50:
moel@167
   237
              chip = Chip.W83667HGB;
moel@167
   238
              logicalDeviceNumber = WINBOND_HARDWARE_MONITOR_LDN;
moel@167
   239
              break;
moel@167
   240
          } break;
moel@167
   241
      }
moel@167
   242
      if (chip == Chip.Unknown) {
moel@167
   243
        if (id != 0 && id != 0xff) {
moel@167
   244
          WinbondFintekExit();
moel@167
   245
moel@167
   246
          ReportUnknownChip("Winbond / Fintek", ((id << 8) | revision));         
moel@167
   247
        }
moel@167
   248
      } else {
moel@167
   249
moel@167
   250
        Select(logicalDeviceNumber);
moel@167
   251
        ushort address = ReadWord(BASE_ADDRESS_REGISTER);
moel@167
   252
        Thread.Sleep(1);
moel@167
   253
        ushort verify = ReadWord(BASE_ADDRESS_REGISTER);
moel@167
   254
moel@167
   255
        ushort vendorID = ReadWord(FINTEK_VENDOR_ID_REGISTER);
moel@167
   256
moel@167
   257
        WinbondFintekExit();
moel@167
   258
moel@167
   259
        if (address != verify) {
moel@167
   260
          report.Append("Chip ID: 0x");
moel@167
   261
          report.AppendLine(chip.ToString("X"));
moel@167
   262
          report.Append("Chip revision: 0x");
moel@167
   263
          report.AppendLine(revision.ToString("X", 
moel@167
   264
            CultureInfo.InvariantCulture));
moel@167
   265
          report.AppendLine("Error: Address verification failed");
moel@167
   266
          report.AppendLine();
moel@167
   267
          return false;
moel@167
   268
        }
moel@167
   269
moel@167
   270
        // some Fintek chips have address register offset 0x05 added already
moel@167
   271
        if ((address & 0x07) == 0x05)
moel@167
   272
          address &= 0xFFF8;
moel@167
   273
moel@167
   274
        if (address < 0x100 || (address & 0xF007) != 0) {
moel@167
   275
          report.Append("Chip ID: 0x");
moel@167
   276
          report.AppendLine(chip.ToString("X"));
moel@167
   277
          report.Append("Chip revision: 0x");
moel@167
   278
          report.AppendLine(revision.ToString("X", 
moel@167
   279
            CultureInfo.InvariantCulture));
moel@167
   280
          report.Append("Error: Invalid address 0x");
moel@167
   281
          report.AppendLine(address.ToString("X", 
moel@167
   282
            CultureInfo.InvariantCulture));
moel@167
   283
          report.AppendLine();
moel@167
   284
          return false;
moel@167
   285
        }
moel@167
   286
moel@167
   287
        switch (chip) {
moel@167
   288
          case Chip.W83627DHG:
moel@167
   289
          case Chip.W83627DHGP:
moel@167
   290
          case Chip.W83627EHF:
moel@167
   291
          case Chip.W83627HF:
moel@167
   292
          case Chip.W83627THF:
moel@167
   293
          case Chip.W83667HG:
moel@167
   294
          case Chip.W83667HGB:
moel@167
   295
          case Chip.W83687THF:
moel@167
   296
            superIOs.Add(new W836XX(chip, revision, address));
moel@167
   297
            break;
moel@167
   298
          case Chip.F71858:
moel@167
   299
          case Chip.F71862:
moel@167
   300
          case Chip.F71869:
moel@167
   301
          case Chip.F71882:
moel@167
   302
          case Chip.F71889ED:
moel@167
   303
          case Chip.F71889F:
moel@167
   304
            if (vendorID != FINTEK_VENDOR_ID) {
moel@167
   305
              report.Append("Chip ID: 0x");
moel@167
   306
              report.AppendLine(chip.ToString("X"));
moel@167
   307
              report.Append("Chip revision: 0x");
moel@167
   308
              report.AppendLine(revision.ToString("X", 
moel@167
   309
                CultureInfo.InvariantCulture));
moel@167
   310
              report.Append("Error: Invalid vendor ID 0x");
moel@167
   311
              report.AppendLine(vendorID.ToString("X", 
moel@167
   312
                CultureInfo.InvariantCulture));
moel@167
   313
              report.AppendLine();
moel@167
   314
              return false;
moel@167
   315
            }
moel@167
   316
            superIOs.Add(new F718XX(chip, address));
moel@167
   317
            break;
moel@167
   318
          default: break;
moel@167
   319
        }
moel@167
   320
moel@167
   321
        return true;
moel@167
   322
      }
moel@167
   323
moel@167
   324
      return false;
moel@167
   325
    }
moel@167
   326
moel@167
   327
    private bool DetectIT87() {
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@167
   337
        case 0x8726: chip = Chip.IT8726F; break;
moel@167
   338
        default: chip = Chip.Unknown; break;
moel@167
   339
      }
moel@167
   340
      if (chip == Chip.Unknown) {
moel@167
   341
        if (chipID != 0 && chipID != 0xffff) {
moel@167
   342
          IT87Exit();
moel@167
   343
moel@167
   344
          ReportUnknownChip("ITE", chipID);
moel@167
   345
        }
moel@167
   346
      } else {
moel@167
   347
        Select(IT87_ENVIRONMENT_CONTROLLER_LDN);
moel@167
   348
        ushort address = ReadWord(BASE_ADDRESS_REGISTER);
moel@167
   349
        Thread.Sleep(1);
moel@167
   350
        ushort verify = ReadWord(BASE_ADDRESS_REGISTER);
moel@167
   351
moel@167
   352
        byte version = (byte)(ReadByte(IT87_CHIP_VERSION_REGISTER) & 0x0F);
moel@167
   353
moel@167
   354
        IT87Exit();
moel@167
   355
moel@167
   356
        if (address != verify || address < 0x100 || (address & 0xF007) != 0) {
moel@167
   357
          report.Append("Chip ID: 0x");
moel@167
   358
          report.AppendLine(chip.ToString("X"));
moel@167
   359
          report.Append("Error: Invalid address 0x");
moel@167
   360
          report.AppendLine(address.ToString("X",
moel@167
   361
            CultureInfo.InvariantCulture));
moel@167
   362
          report.AppendLine();
moel@167
   363
          return false;
moel@167
   364
        }
moel@167
   365
moel@167
   366
        superIOs.Add(new IT87XX(chip, address, version));
moel@167
   367
        return true;
moel@167
   368
      }
moel@167
   369
moel@167
   370
      return false;
moel@167
   371
    }
moel@167
   372
moel@167
   373
    private bool DetectSMSC() {
moel@167
   374
      SMSCEnter();
moel@167
   375
moel@167
   376
      ushort chipID = ReadWord(CHIP_ID_REGISTER);
moel@167
   377
      Chip chip;
moel@167
   378
      switch (chipID) {
moel@167
   379
        default: chip = Chip.Unknown; break;
moel@167
   380
      }
moel@167
   381
      if (chip == Chip.Unknown) {
moel@167
   382
        if (chipID != 0 && chipID != 0xffff) {
moel@167
   383
          SMSCExit();
moel@167
   384
moel@167
   385
          ReportUnknownChip("SMSC", chipID);
moel@167
   386
        }
moel@167
   387
      } else {
moel@167
   388
        SMSCExit();
moel@167
   389
        return true;
moel@167
   390
      }
moel@167
   391
moel@167
   392
      return false;
moel@167
   393
    }
moel@167
   394
moel@163
   395
    private void Detect() {
moel@110
   396
moel@110
   397
      for (int i = 0; i < REGISTER_PORTS.Length; i++) {
moel@110
   398
        registerPort = REGISTER_PORTS[i];
moel@110
   399
        valuePort = VALUE_PORTS[i];
moel@110
   400
moel@167
   401
        if (DetectWinbondFintek()) continue;
moel@110
   402
moel@167
   403
        if (DetectIT87()) continue;
moel@110
   404
moel@167
   405
        if (DetectSMSC()) continue;
moel@163
   406
      }  
moel@163
   407
    }
moel@163
   408
moel@163
   409
    public LPCIO() {
moel@163
   410
      if (!WinRing0.IsAvailable)
moel@163
   411
        return;
moel@163
   412
moel@163
   413
      if (!WinRing0.WaitIsaBusMutex(100))
moel@163
   414
        return;
moel@163
   415
moel@163
   416
      Detect();
moel@163
   417
moel@163
   418
      WinRing0.ReleaseIsaBusMutex();      
moel@110
   419
    }
moel@110
   420
moel@130
   421
    public ISuperIO[] SuperIO {
moel@110
   422
      get {
moel@130
   423
        return superIOs.ToArray();
moel@110
   424
      }
moel@110
   425
    }
moel@110
   426
moel@110
   427
    public string GetReport() {
moel@110
   428
      if (report.Length > 0) {
moel@110
   429
        report.Insert(0, "LPCIO" + Environment.NewLine +
moel@110
   430
          Environment.NewLine);        
moel@110
   431
        return report.ToString();
moel@110
   432
      } else
moel@110
   433
        return null;
moel@110
   434
    }
moel@110
   435
  }
moel@110
   436
}