Hardware/LPC/LPCIO.cs
author moel.mich
Mon, 07 Jun 2010 20:03:48 +0000
changeset 136 fa2957aa0699
parent 126 2354fdb91ac4
child 145 673c2ad36db2
permissions -rw-r--r--
Added (partial) SMBIOS support for Linux by reading from sysfs.
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@110
    81
    private const byte IT87_ENVIRONMENT_CONTROLLER_LDN = 0x04;    
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@130
   122
    public LPCIO() {
moel@110
   123
      if (!WinRing0.IsAvailable)
moel@110
   124
        return;
moel@110
   125
moel@110
   126
      for (int i = 0; i < REGISTER_PORTS.Length; i++) {
moel@110
   127
        registerPort = REGISTER_PORTS[i];
moel@110
   128
        valuePort = VALUE_PORTS[i];
moel@110
   129
moel@110
   130
        WinbondFintekEnter();
moel@110
   131
moel@110
   132
        byte logicalDeviceNumber;
moel@110
   133
        byte id = ReadByte(CHIP_ID_REGISTER);
moel@110
   134
        byte revision = ReadByte(CHIP_REVISION_REGISTER);
moel@110
   135
        chip = Chip.Unknown;
moel@110
   136
        logicalDeviceNumber = 0;
moel@110
   137
        switch (id) {
moel@110
   138
          case 0x05:
moel@110
   139
            switch (revision) {
moel@110
   140
              case 0x07:
moel@110
   141
                chip = Chip.F71858;
moel@110
   142
                logicalDeviceNumber = F71858_HARDWARE_MONITOR_LDN;
moel@110
   143
                break;
moel@110
   144
              case 0x41:
moel@110
   145
                chip = Chip.F71882;
moel@110
   146
                logicalDeviceNumber = FINTEK_HARDWARE_MONITOR_LDN;
moel@110
   147
                break;              
moel@110
   148
            } break;
moel@110
   149
          case 0x06:
moel@110
   150
            switch (revision) {             
moel@110
   151
              case 0x01:
moel@110
   152
                chip = Chip.F71862;
moel@110
   153
                logicalDeviceNumber = FINTEK_HARDWARE_MONITOR_LDN;
moel@110
   154
                break;              
moel@110
   155
            } break;
moel@110
   156
          case 0x07:
moel@110
   157
            switch (revision) {
moel@110
   158
              case 0x23:
moel@110
   159
                chip = Chip.F71889F;
moel@110
   160
                logicalDeviceNumber = FINTEK_HARDWARE_MONITOR_LDN;
moel@110
   161
                break;              
moel@110
   162
            } break;
moel@110
   163
          case 0x08:
moel@110
   164
            switch (revision) {
moel@110
   165
              case 0x14:
moel@110
   166
                chip = Chip.F71869;
moel@110
   167
                logicalDeviceNumber = FINTEK_HARDWARE_MONITOR_LDN;
moel@110
   168
                break;              
moel@110
   169
            } break;
moel@110
   170
          case 0x09:
moel@110
   171
            switch (revision) {
moel@110
   172
              case 0x09:
moel@110
   173
                chip = Chip.F71889ED;
moel@110
   174
                logicalDeviceNumber = FINTEK_HARDWARE_MONITOR_LDN;
moel@110
   175
                break;              
moel@110
   176
            } break;
moel@110
   177
          case 0x52:
moel@110
   178
            switch (revision) {
moel@110
   179
              case 0x17:
moel@110
   180
              case 0x3A:
moel@110
   181
              case 0x41:
moel@110
   182
                chip = Chip.W83627HF;
moel@110
   183
                logicalDeviceNumber = WINBOND_HARDWARE_MONITOR_LDN;
moel@110
   184
                break;             
moel@110
   185
            } break;
moel@110
   186
          case 0x82:
moel@110
   187
            switch (revision) {
moel@110
   188
              case 0x83:
moel@110
   189
                chip = Chip.W83627THF;
moel@110
   190
                logicalDeviceNumber = WINBOND_HARDWARE_MONITOR_LDN;
moel@110
   191
                break;
moel@110
   192
            } break;
moel@110
   193
          case 0x85:
moel@110
   194
            switch (revision) {
moel@110
   195
              case 0x41:
moel@110
   196
                chip = Chip.W83687THF;
moel@110
   197
                logicalDeviceNumber = WINBOND_HARDWARE_MONITOR_LDN;
moel@110
   198
                break;
moel@110
   199
            } break;
moel@110
   200
          case 0x88:
moel@110
   201
            switch (revision & 0xF0) {
moel@110
   202
              case 0x50:
moel@110
   203
              case 0x60:
moel@110
   204
                chip = Chip.W83627EHF;
moel@110
   205
                logicalDeviceNumber = WINBOND_HARDWARE_MONITOR_LDN;
moel@110
   206
                break;
moel@110
   207
            } break;
moel@110
   208
          case 0xA0:
moel@110
   209
            switch (revision & 0xF0) {
moel@110
   210
              case 0x20: 
moel@110
   211
                chip = Chip.W83627DHG;
moel@110
   212
                logicalDeviceNumber = WINBOND_HARDWARE_MONITOR_LDN;  
moel@110
   213
                break;             
moel@110
   214
            } break;
moel@110
   215
          case 0xA5:
moel@110
   216
            switch (revision & 0xF0) {
moel@110
   217
              case 0x10:
moel@110
   218
                chip = Chip.W83667HG;
moel@110
   219
                logicalDeviceNumber = WINBOND_HARDWARE_MONITOR_LDN;
moel@110
   220
                break;
moel@110
   221
            } break;
moel@110
   222
          case 0xB0:
moel@110
   223
            switch (revision & 0xF0) {
moel@110
   224
              case 0x70:
moel@110
   225
                chip = Chip.W83627DHGP;
moel@110
   226
                logicalDeviceNumber = WINBOND_HARDWARE_MONITOR_LDN;
moel@110
   227
                break;             
moel@110
   228
            } break;
moel@110
   229
          case 0xB3:
moel@110
   230
            switch (revision & 0xF0) {
moel@110
   231
              case 0x50:
moel@110
   232
                chip = Chip.W83667HGB;
moel@110
   233
                logicalDeviceNumber = WINBOND_HARDWARE_MONITOR_LDN;
moel@110
   234
                break;
moel@110
   235
            } break; 
moel@110
   236
        }
moel@110
   237
        if (chip == Chip.Unknown) {
moel@110
   238
          if (id != 0 && id != 0xff) {
moel@110
   239
            WinbondFintekExit();
moel@110
   240
moel@110
   241
            report.Append("Chip ID: Unknown Winbond / Fintek with ID 0x"); 
moel@110
   242
            report.AppendLine(((id << 8) | revision).ToString("X"));
moel@110
   243
            report.AppendLine();
moel@110
   244
          }
moel@110
   245
        } else {
moel@110
   246
moel@110
   247
          Select(logicalDeviceNumber);
moel@110
   248
          ushort address = ReadWord(BASE_ADDRESS_REGISTER);          
moel@110
   249
          Thread.Sleep(1);
moel@110
   250
          ushort verify = ReadWord(BASE_ADDRESS_REGISTER);
moel@110
   251
moel@110
   252
          ushort vendorID = ReadWord(FINTEK_VENDOR_ID_REGISTER);
moel@110
   253
moel@110
   254
          WinbondFintekExit();
moel@110
   255
moel@110
   256
          if (address != verify) {            
moel@110
   257
            report.Append("Chip ID: 0x"); 
moel@110
   258
            report.AppendLine(chip.ToString("X"));
moel@110
   259
            report.Append("Chip revision: 0x"); 
moel@110
   260
            report.AppendLine(revision.ToString("X"));
moel@110
   261
            report.AppendLine("Error: Address verification failed");
moel@110
   262
            report.AppendLine();
moel@110
   263
            return;
moel@110
   264
          }
moel@110
   265
moel@110
   266
          // some Fintek chips have address register offset 0x05 added already
moel@110
   267
          if ((address & 0x07) == 0x05)
moel@110
   268
            address &= 0xFFF8;
moel@110
   269
moel@110
   270
          if (address < 0x100 || (address & 0xF007) != 0) {            
moel@110
   271
            report.Append("Chip ID: 0x");
moel@110
   272
            report.AppendLine(chip.ToString("X"));
moel@110
   273
            report.Append("Chip revision: 0x");
moel@110
   274
            report.AppendLine(revision.ToString("X"));
moel@110
   275
            report.Append("Error: Invalid address 0x");
moel@110
   276
            report.AppendLine(address.ToString("X"));
moel@110
   277
            report.AppendLine();
moel@110
   278
            return;
moel@110
   279
          }
moel@110
   280
moel@110
   281
          switch (chip) {
moel@110
   282
            case Chip.W83627DHG:
moel@110
   283
            case Chip.W83627DHGP:
moel@110
   284
            case Chip.W83627EHF:
moel@110
   285
            case Chip.W83627HF:
moel@110
   286
            case Chip.W83627THF:
moel@110
   287
            case Chip.W83667HG:
moel@110
   288
            case Chip.W83667HGB:
moel@110
   289
            case Chip.W83687THF:
moel@130
   290
              superIOs.Add(new W836XX(chip, revision, address));
moel@110
   291
              break;
moel@110
   292
            case Chip.F71858:
moel@110
   293
            case Chip.F71862:
moel@110
   294
            case Chip.F71869:
moel@110
   295
            case Chip.F71882:
moel@110
   296
            case Chip.F71889ED:
moel@110
   297
            case Chip.F71889F:
moel@110
   298
              if (vendorID != FINTEK_VENDOR_ID) {
moel@110
   299
                report.Append("Chip ID: 0x");
moel@110
   300
                report.AppendLine(chip.ToString("X"));
moel@110
   301
                report.Append("Chip revision: 0x");
moel@110
   302
                report.AppendLine(revision.ToString("X"));
moel@110
   303
                report.Append("Error: Invalid vendor ID 0x");
moel@110
   304
                report.AppendLine(vendorID.ToString("X"));
moel@110
   305
                report.AppendLine();
moel@110
   306
                return;
moel@110
   307
              }
moel@130
   308
              superIOs.Add(new F718XX(chip, address));
moel@110
   309
              break;
moel@110
   310
            default: break;
moel@110
   311
          }
moel@110
   312
          
moel@110
   313
          return;
moel@110
   314
        }
moel@110
   315
moel@110
   316
        IT87Enter();
moel@110
   317
moel@110
   318
        ushort chipID = ReadWord(CHIP_ID_REGISTER);
moel@110
   319
        switch (chipID) {
moel@110
   320
          case 0x8712: chip = Chip.IT8712F; break;
moel@110
   321
          case 0x8716: chip = Chip.IT8716F; break;
moel@110
   322
          case 0x8718: chip = Chip.IT8718F; break;
moel@110
   323
          case 0x8720: chip = Chip.IT8720F; break;
moel@110
   324
          case 0x8726: chip = Chip.IT8726F; break; 
moel@110
   325
          default: chip = Chip.Unknown; break;
moel@110
   326
        }
moel@110
   327
        if (chip == Chip.Unknown) {
moel@110
   328
          if (chipID != 0 && chipID != 0xffff) {
moel@110
   329
            IT87Exit();
moel@110
   330
moel@110
   331
            report.Append("Chip ID: Unknown ITE with ID 0x");
moel@110
   332
            report.AppendLine(chipID.ToString("X"));
moel@110
   333
            report.AppendLine();
moel@110
   334
          }
moel@110
   335
        } else {
moel@110
   336
          Select(IT87_ENVIRONMENT_CONTROLLER_LDN);
moel@110
   337
          ushort address = ReadWord(BASE_ADDRESS_REGISTER);
moel@110
   338
          Thread.Sleep(1);
moel@110
   339
          ushort verify = ReadWord(BASE_ADDRESS_REGISTER);
moel@110
   340
moel@110
   341
          IT87Exit();
moel@110
   342
moel@110
   343
          if (address != verify || address < 0x100 || (address & 0xF007) != 0) {
moel@110
   344
            report.Append("Chip ID: 0x");
moel@110
   345
            report.AppendLine(chip.ToString("X"));            
moel@110
   346
            report.Append("Error: Invalid address 0x");
moel@110
   347
            report.AppendLine(address.ToString("X"));
moel@110
   348
            report.AppendLine();
moel@110
   349
            return;
moel@110
   350
          }
moel@110
   351
moel@130
   352
         superIOs.Add(new IT87XX(chip, address));
moel@110
   353
moel@110
   354
          return;
moel@110
   355
        }
moel@110
   356
moel@110
   357
        SMSCEnter();
moel@110
   358
moel@110
   359
        chipID = ReadWord(CHIP_ID_REGISTER);
moel@110
   360
        switch (chipID) {
moel@110
   361
          default: chip = Chip.Unknown; break;
moel@110
   362
        }
moel@110
   363
        if (chip == Chip.Unknown) {
moel@110
   364
          if (chipID != 0 && chipID != 0xffff) {
moel@110
   365
            SMSCExit();
moel@110
   366
moel@110
   367
            report.Append("Chip ID: Unknown SMSC with ID 0x");
moel@110
   368
            report.AppendLine(chipID.ToString("X"));
moel@110
   369
            report.AppendLine();
moel@110
   370
          }
moel@110
   371
        } else {
moel@110
   372
          SMSCExit();
moel@110
   373
moel@110
   374
          return;
moel@110
   375
        }
moel@110
   376
      }   
moel@110
   377
    }
moel@110
   378
moel@130
   379
    public ISuperIO[] SuperIO {
moel@110
   380
      get {
moel@130
   381
        return superIOs.ToArray();
moel@110
   382
      }
moel@110
   383
    }
moel@110
   384
moel@110
   385
    public string GetReport() {
moel@110
   386
      if (report.Length > 0) {
moel@110
   387
        report.Insert(0, "LPCIO" + Environment.NewLine +
moel@110
   388
          Environment.NewLine);        
moel@110
   389
        return report.ToString();
moel@110
   390
      } else
moel@110
   391
        return null;
moel@110
   392
    }
moel@110
   393
  }
moel@110
   394
}