Hardware/LPC/LPCIO.cs
author moel.mich
Thu, 05 Aug 2010 19:28:50 +0000
changeset 164 cc1e116d0f2c
parent 147 17f982ee3a46
child 165 813d8bc3192f
permissions -rw-r--r--
Added a fan control sensor for ATI GPUs.
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@110
    40
using System.Text;
moel@110
    41
using System.Threading;
moel@110
    42
moel@110
    43
namespace OpenHardwareMonitor.Hardware.LPC {
moel@110
    44
  public class LPCIO {
moel@110
    45
moel@130
    46
    private List<ISuperIO> superIOs = new List<ISuperIO>();
moel@110
    47
    private StringBuilder report = new StringBuilder();
moel@110
    48
moel@110
    49
    private Chip chip = Chip.Unknown;
moel@110
    50
moel@110
    51
    // I/O Ports
moel@110
    52
    private ushort[] REGISTER_PORTS = new ushort[] { 0x2e, 0x4e };
moel@110
    53
    private ushort[] VALUE_PORTS = new ushort[] { 0x2f, 0x4f };
moel@110
    54
moel@110
    55
    private ushort registerPort;
moel@110
    56
    private ushort valuePort;
moel@110
    57
moel@110
    58
    // Registers
moel@110
    59
    private const byte CONFIGURATION_CONTROL_REGISTER = 0x02;
moel@110
    60
    private const byte DEVCIE_SELECT_REGISTER = 0x07;
moel@110
    61
    private const byte CHIP_ID_REGISTER = 0x20;
moel@110
    62
    private const byte CHIP_REVISION_REGISTER = 0x21;
moel@110
    63
    private const byte BASE_ADDRESS_REGISTER = 0x60;
moel@110
    64
moel@110
    65
    private byte ReadByte(byte register) {
moel@110
    66
      WinRing0.WriteIoPortByte(registerPort, register);
moel@110
    67
      return WinRing0.ReadIoPortByte(valuePort);
moel@110
    68
    } 
moel@110
    69
moel@110
    70
    private ushort ReadWord(byte register) {
moel@110
    71
      return (ushort)((ReadByte(register) << 8) | 
moel@110
    72
        ReadByte((byte)(register + 1)));
moel@110
    73
    }
moel@110
    74
moel@110
    75
    private void Select(byte logicalDeviceNumber) {
moel@110
    76
      WinRing0.WriteIoPortByte(registerPort, DEVCIE_SELECT_REGISTER);
moel@110
    77
      WinRing0.WriteIoPortByte(valuePort, logicalDeviceNumber);
moel@110
    78
    }
moel@110
    79
moel@110
    80
    // ITE
moel@145
    81
    private const byte IT87_ENVIRONMENT_CONTROLLER_LDN = 0x04;
moel@145
    82
    private const byte IT87_CHIP_VERSION_REGISTER = 0x22;
moel@110
    83
moel@110
    84
    private void IT87Enter() {
moel@110
    85
      WinRing0.WriteIoPortByte(registerPort, 0x87);
moel@110
    86
      WinRing0.WriteIoPortByte(registerPort, 0x01);
moel@110
    87
      WinRing0.WriteIoPortByte(registerPort, 0x55);
moel@110
    88
      WinRing0.WriteIoPortByte(registerPort, 0x55);
moel@110
    89
    }
moel@110
    90
moel@130
    91
    private void IT87Exit() {
moel@110
    92
      WinRing0.WriteIoPortByte(registerPort, CONFIGURATION_CONTROL_REGISTER);
moel@110
    93
      WinRing0.WriteIoPortByte(valuePort, 0x02);
moel@110
    94
    }
moel@110
    95
moel@110
    96
    // Winbond, Fintek
moel@110
    97
    private const byte FINTEK_VENDOR_ID_REGISTER = 0x23;
moel@110
    98
    private const ushort FINTEK_VENDOR_ID = 0x1934;
moel@110
    99
moel@110
   100
    private const byte WINBOND_HARDWARE_MONITOR_LDN = 0x0B;
moel@110
   101
moel@110
   102
    private const byte F71858_HARDWARE_MONITOR_LDN = 0x02;
moel@110
   103
    private const byte FINTEK_HARDWARE_MONITOR_LDN = 0x04;
moel@110
   104
moel@110
   105
    private void WinbondFintekEnter() {
moel@110
   106
      WinRing0.WriteIoPortByte(registerPort, 0x87);
moel@110
   107
      WinRing0.WriteIoPortByte(registerPort, 0x87);
moel@110
   108
    }
moel@110
   109
moel@110
   110
    private void WinbondFintekExit() {
moel@110
   111
      WinRing0.WriteIoPortByte(registerPort, 0xAA);      
moel@110
   112
    }
moel@110
   113
moel@110
   114
    // SMSC
moel@110
   115
    private void SMSCEnter() {
moel@110
   116
      WinRing0.WriteIoPortByte(registerPort, 0x55);
moel@110
   117
    }
moel@110
   118
moel@110
   119
    private void SMSCExit() {
moel@110
   120
      WinRing0.WriteIoPortByte(registerPort, 0xAA);
moel@110
   121
    }
moel@110
   122
moel@163
   123
    private void Detect() {
moel@110
   124
moel@110
   125
      for (int i = 0; i < REGISTER_PORTS.Length; i++) {
moel@110
   126
        registerPort = REGISTER_PORTS[i];
moel@110
   127
        valuePort = VALUE_PORTS[i];
moel@110
   128
moel@110
   129
        WinbondFintekEnter();
moel@110
   130
moel@110
   131
        byte logicalDeviceNumber;
moel@110
   132
        byte id = ReadByte(CHIP_ID_REGISTER);
moel@110
   133
        byte revision = ReadByte(CHIP_REVISION_REGISTER);
moel@110
   134
        chip = Chip.Unknown;
moel@110
   135
        logicalDeviceNumber = 0;
moel@110
   136
        switch (id) {
moel@110
   137
          case 0x05:
moel@110
   138
            switch (revision) {
moel@110
   139
              case 0x07:
moel@110
   140
                chip = Chip.F71858;
moel@110
   141
                logicalDeviceNumber = F71858_HARDWARE_MONITOR_LDN;
moel@110
   142
                break;
moel@110
   143
              case 0x41:
moel@110
   144
                chip = Chip.F71882;
moel@110
   145
                logicalDeviceNumber = FINTEK_HARDWARE_MONITOR_LDN;
moel@163
   146
                break;
moel@110
   147
            } break;
moel@110
   148
          case 0x06:
moel@163
   149
            switch (revision) {
moel@110
   150
              case 0x01:
moel@110
   151
                chip = Chip.F71862;
moel@110
   152
                logicalDeviceNumber = FINTEK_HARDWARE_MONITOR_LDN;
moel@163
   153
                break;
moel@110
   154
            } break;
moel@110
   155
          case 0x07:
moel@110
   156
            switch (revision) {
moel@110
   157
              case 0x23:
moel@110
   158
                chip = Chip.F71889F;
moel@110
   159
                logicalDeviceNumber = FINTEK_HARDWARE_MONITOR_LDN;
moel@163
   160
                break;
moel@110
   161
            } break;
moel@110
   162
          case 0x08:
moel@110
   163
            switch (revision) {
moel@110
   164
              case 0x14:
moel@110
   165
                chip = Chip.F71869;
moel@110
   166
                logicalDeviceNumber = FINTEK_HARDWARE_MONITOR_LDN;
moel@163
   167
                break;
moel@110
   168
            } break;
moel@110
   169
          case 0x09:
moel@110
   170
            switch (revision) {
moel@110
   171
              case 0x09:
moel@110
   172
                chip = Chip.F71889ED;
moel@110
   173
                logicalDeviceNumber = FINTEK_HARDWARE_MONITOR_LDN;
moel@163
   174
                break;
moel@110
   175
            } break;
moel@110
   176
          case 0x52:
moel@110
   177
            switch (revision) {
moel@110
   178
              case 0x17:
moel@110
   179
              case 0x3A:
moel@110
   180
              case 0x41:
moel@110
   181
                chip = Chip.W83627HF;
moel@110
   182
                logicalDeviceNumber = WINBOND_HARDWARE_MONITOR_LDN;
moel@163
   183
                break;
moel@110
   184
            } break;
moel@110
   185
          case 0x82:
moel@147
   186
            switch (revision & 0xF0) {
moel@147
   187
              case 0x80:
moel@110
   188
                chip = Chip.W83627THF;
moel@110
   189
                logicalDeviceNumber = WINBOND_HARDWARE_MONITOR_LDN;
moel@110
   190
                break;
moel@110
   191
            } break;
moel@110
   192
          case 0x85:
moel@110
   193
            switch (revision) {
moel@110
   194
              case 0x41:
moel@110
   195
                chip = Chip.W83687THF;
moel@110
   196
                logicalDeviceNumber = WINBOND_HARDWARE_MONITOR_LDN;
moel@110
   197
                break;
moel@110
   198
            } break;
moel@110
   199
          case 0x88:
moel@110
   200
            switch (revision & 0xF0) {
moel@110
   201
              case 0x50:
moel@110
   202
              case 0x60:
moel@110
   203
                chip = Chip.W83627EHF;
moel@110
   204
                logicalDeviceNumber = WINBOND_HARDWARE_MONITOR_LDN;
moel@110
   205
                break;
moel@110
   206
            } break;
moel@110
   207
          case 0xA0:
moel@110
   208
            switch (revision & 0xF0) {
moel@163
   209
              case 0x20:
moel@110
   210
                chip = Chip.W83627DHG;
moel@163
   211
                logicalDeviceNumber = WINBOND_HARDWARE_MONITOR_LDN;
moel@163
   212
                break;
moel@110
   213
            } break;
moel@110
   214
          case 0xA5:
moel@110
   215
            switch (revision & 0xF0) {
moel@110
   216
              case 0x10:
moel@110
   217
                chip = Chip.W83667HG;
moel@110
   218
                logicalDeviceNumber = WINBOND_HARDWARE_MONITOR_LDN;
moel@110
   219
                break;
moel@110
   220
            } break;
moel@110
   221
          case 0xB0:
moel@110
   222
            switch (revision & 0xF0) {
moel@110
   223
              case 0x70:
moel@110
   224
                chip = Chip.W83627DHGP;
moel@110
   225
                logicalDeviceNumber = WINBOND_HARDWARE_MONITOR_LDN;
moel@163
   226
                break;
moel@110
   227
            } break;
moel@110
   228
          case 0xB3:
moel@110
   229
            switch (revision & 0xF0) {
moel@110
   230
              case 0x50:
moel@110
   231
                chip = Chip.W83667HGB;
moel@110
   232
                logicalDeviceNumber = WINBOND_HARDWARE_MONITOR_LDN;
moel@110
   233
                break;
moel@163
   234
            } break;
moel@110
   235
        }
moel@110
   236
        if (chip == Chip.Unknown) {
moel@110
   237
          if (id != 0 && id != 0xff) {
moel@110
   238
            WinbondFintekExit();
moel@110
   239
moel@163
   240
            report.Append("Chip ID: Unknown Winbond / Fintek with ID 0x");
moel@110
   241
            report.AppendLine(((id << 8) | revision).ToString("X"));
moel@110
   242
            report.AppendLine();
moel@110
   243
          }
moel@110
   244
        } else {
moel@110
   245
moel@110
   246
          Select(logicalDeviceNumber);
moel@163
   247
          ushort address = ReadWord(BASE_ADDRESS_REGISTER);
moel@110
   248
          Thread.Sleep(1);
moel@110
   249
          ushort verify = ReadWord(BASE_ADDRESS_REGISTER);
moel@110
   250
moel@110
   251
          ushort vendorID = ReadWord(FINTEK_VENDOR_ID_REGISTER);
moel@110
   252
moel@110
   253
          WinbondFintekExit();
moel@110
   254
moel@163
   255
          if (address != verify) {
moel@163
   256
            report.Append("Chip ID: 0x");
moel@110
   257
            report.AppendLine(chip.ToString("X"));
moel@163
   258
            report.Append("Chip revision: 0x");
moel@110
   259
            report.AppendLine(revision.ToString("X"));
moel@110
   260
            report.AppendLine("Error: Address verification failed");
moel@110
   261
            report.AppendLine();
moel@110
   262
            return;
moel@110
   263
          }
moel@110
   264
moel@110
   265
          // some Fintek chips have address register offset 0x05 added already
moel@110
   266
          if ((address & 0x07) == 0x05)
moel@110
   267
            address &= 0xFFF8;
moel@110
   268
moel@163
   269
          if (address < 0x100 || (address & 0xF007) != 0) {
moel@110
   270
            report.Append("Chip ID: 0x");
moel@110
   271
            report.AppendLine(chip.ToString("X"));
moel@110
   272
            report.Append("Chip revision: 0x");
moel@110
   273
            report.AppendLine(revision.ToString("X"));
moel@110
   274
            report.Append("Error: Invalid address 0x");
moel@110
   275
            report.AppendLine(address.ToString("X"));
moel@110
   276
            report.AppendLine();
moel@110
   277
            return;
moel@110
   278
          }
moel@110
   279
moel@110
   280
          switch (chip) {
moel@110
   281
            case Chip.W83627DHG:
moel@110
   282
            case Chip.W83627DHGP:
moel@110
   283
            case Chip.W83627EHF:
moel@110
   284
            case Chip.W83627HF:
moel@110
   285
            case Chip.W83627THF:
moel@110
   286
            case Chip.W83667HG:
moel@110
   287
            case Chip.W83667HGB:
moel@110
   288
            case Chip.W83687THF:
moel@130
   289
              superIOs.Add(new W836XX(chip, revision, address));
moel@110
   290
              break;
moel@110
   291
            case Chip.F71858:
moel@110
   292
            case Chip.F71862:
moel@110
   293
            case Chip.F71869:
moel@110
   294
            case Chip.F71882:
moel@110
   295
            case Chip.F71889ED:
moel@110
   296
            case Chip.F71889F:
moel@110
   297
              if (vendorID != FINTEK_VENDOR_ID) {
moel@110
   298
                report.Append("Chip ID: 0x");
moel@110
   299
                report.AppendLine(chip.ToString("X"));
moel@110
   300
                report.Append("Chip revision: 0x");
moel@110
   301
                report.AppendLine(revision.ToString("X"));
moel@110
   302
                report.Append("Error: Invalid vendor ID 0x");
moel@110
   303
                report.AppendLine(vendorID.ToString("X"));
moel@110
   304
                report.AppendLine();
moel@110
   305
                return;
moel@110
   306
              }
moel@130
   307
              superIOs.Add(new F718XX(chip, address));
moel@110
   308
              break;
moel@110
   309
            default: break;
moel@110
   310
          }
moel@163
   311
moel@110
   312
          return;
moel@110
   313
        }
moel@110
   314
moel@110
   315
        IT87Enter();
moel@110
   316
moel@110
   317
        ushort chipID = ReadWord(CHIP_ID_REGISTER);
moel@110
   318
        switch (chipID) {
moel@110
   319
          case 0x8712: chip = Chip.IT8712F; break;
moel@110
   320
          case 0x8716: chip = Chip.IT8716F; break;
moel@110
   321
          case 0x8718: chip = Chip.IT8718F; break;
moel@110
   322
          case 0x8720: chip = Chip.IT8720F; break;
moel@163
   323
          case 0x8726: chip = Chip.IT8726F; break;
moel@110
   324
          default: chip = Chip.Unknown; break;
moel@110
   325
        }
moel@110
   326
        if (chip == Chip.Unknown) {
moel@110
   327
          if (chipID != 0 && chipID != 0xffff) {
moel@110
   328
            IT87Exit();
moel@110
   329
moel@110
   330
            report.Append("Chip ID: Unknown ITE with ID 0x");
moel@110
   331
            report.AppendLine(chipID.ToString("X"));
moel@110
   332
            report.AppendLine();
moel@110
   333
          }
moel@163
   334
        } else {
moel@110
   335
          Select(IT87_ENVIRONMENT_CONTROLLER_LDN);
moel@110
   336
          ushort address = ReadWord(BASE_ADDRESS_REGISTER);
moel@110
   337
          Thread.Sleep(1);
moel@110
   338
          ushort verify = ReadWord(BASE_ADDRESS_REGISTER);
moel@110
   339
moel@145
   340
          byte version = (byte)(ReadByte(IT87_CHIP_VERSION_REGISTER) & 0x0F);
moel@145
   341
moel@110
   342
          IT87Exit();
moel@110
   343
moel@110
   344
          if (address != verify || address < 0x100 || (address & 0xF007) != 0) {
moel@110
   345
            report.Append("Chip ID: 0x");
moel@163
   346
            report.AppendLine(chip.ToString("X"));
moel@110
   347
            report.Append("Error: Invalid address 0x");
moel@110
   348
            report.AppendLine(address.ToString("X"));
moel@110
   349
            report.AppendLine();
moel@110
   350
            return;
moel@110
   351
          }
moel@110
   352
moel@163
   353
          superIOs.Add(new IT87XX(chip, address, version));
moel@110
   354
moel@110
   355
          return;
moel@110
   356
        }
moel@110
   357
moel@110
   358
        SMSCEnter();
moel@110
   359
moel@110
   360
        chipID = ReadWord(CHIP_ID_REGISTER);
moel@110
   361
        switch (chipID) {
moel@110
   362
          default: chip = Chip.Unknown; break;
moel@110
   363
        }
moel@110
   364
        if (chip == Chip.Unknown) {
moel@110
   365
          if (chipID != 0 && chipID != 0xffff) {
moel@110
   366
            SMSCExit();
moel@110
   367
moel@110
   368
            report.Append("Chip ID: Unknown SMSC with ID 0x");
moel@110
   369
            report.AppendLine(chipID.ToString("X"));
moel@110
   370
            report.AppendLine();
moel@110
   371
          }
moel@110
   372
        } else {
moel@110
   373
          SMSCExit();
moel@110
   374
moel@110
   375
          return;
moel@110
   376
        }
moel@163
   377
      }  
moel@163
   378
    }
moel@163
   379
moel@163
   380
    public LPCIO() {
moel@163
   381
      if (!WinRing0.IsAvailable)
moel@163
   382
        return;
moel@163
   383
moel@163
   384
      if (!WinRing0.WaitIsaBusMutex(100))
moel@163
   385
        return;
moel@163
   386
moel@163
   387
      Detect();
moel@163
   388
moel@163
   389
      WinRing0.ReleaseIsaBusMutex();      
moel@110
   390
    }
moel@110
   391
moel@130
   392
    public ISuperIO[] SuperIO {
moel@110
   393
      get {
moel@130
   394
        return superIOs.ToArray();
moel@110
   395
      }
moel@110
   396
    }
moel@110
   397
moel@110
   398
    public string GetReport() {
moel@110
   399
      if (report.Length > 0) {
moel@110
   400
        report.Insert(0, "LPCIO" + Environment.NewLine +
moel@110
   401
          Environment.NewLine);        
moel@110
   402
        return report.ToString();
moel@110
   403
      } else
moel@110
   404
        return null;
moel@110
   405
    }
moel@110
   406
  }
moel@110
   407
}