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