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