Fixed Issue 104.
3 Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 The contents of this file are subject to the Mozilla Public License Version
6 1.1 (the "License"); you may not use this file except in compliance with
7 the License. You may obtain a copy of the License at
9 http://www.mozilla.org/MPL/
11 Software distributed under the License is distributed on an "AS IS" basis,
12 WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 for the specific language governing rights and limitations under the License.
15 The Original Code is the Open Hardware Monitor code.
17 The Initial Developer of the Original Code is
18 Michael Möller <m.moeller@gmx.ch>.
19 Portions created by the Initial Developer are Copyright (C) 2009-2010
20 the Initial Developer. All Rights Reserved.
24 Alternatively, the contents of this file may be used under the terms of
25 either the GNU General Public License Version 2 or later (the "GPL"), or
26 the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 in which case the provisions of the GPL or the LGPL are applicable instead
28 of those above. If you wish to allow use of your version of this file only
29 under the terms of either the GPL or the LGPL, and not to allow others to
30 use your version of this file under the terms of the MPL, indicate your
31 decision by deleting the provisions above and replace them with the notice
32 and other provisions required by the GPL or the LGPL. If you do not delete
33 the provisions above, a recipient may use your version of this file under
34 the terms of any one of the MPL, the GPL or the LGPL.
39 using System.Collections.Generic;
40 using System.Globalization;
42 using System.Threading;
44 namespace OpenHardwareMonitor.Hardware.LPC {
45 internal class LPCIO {
47 private List<ISuperIO> superIOs = new List<ISuperIO>();
48 private StringBuilder report = new StringBuilder();
51 private ushort[] REGISTER_PORTS = new ushort[] { 0x2e, 0x4e };
52 private ushort[] VALUE_PORTS = new ushort[] { 0x2f, 0x4f };
54 private ushort registerPort;
55 private ushort valuePort;
58 private const byte CONFIGURATION_CONTROL_REGISTER = 0x02;
59 private const byte DEVCIE_SELECT_REGISTER = 0x07;
60 private const byte CHIP_ID_REGISTER = 0x20;
61 private const byte CHIP_REVISION_REGISTER = 0x21;
62 private const byte BASE_ADDRESS_REGISTER = 0x60;
64 private byte ReadByte(byte register) {
65 WinRing0.WriteIoPortByte(registerPort, register);
66 return WinRing0.ReadIoPortByte(valuePort);
69 private ushort ReadWord(byte register) {
70 return (ushort)((ReadByte(register) << 8) |
71 ReadByte((byte)(register + 1)));
74 private void Select(byte logicalDeviceNumber) {
75 WinRing0.WriteIoPortByte(registerPort, DEVCIE_SELECT_REGISTER);
76 WinRing0.WriteIoPortByte(valuePort, logicalDeviceNumber);
80 private const byte IT87_ENVIRONMENT_CONTROLLER_LDN = 0x04;
81 private const byte IT87_CHIP_VERSION_REGISTER = 0x22;
83 private void IT87Enter() {
84 WinRing0.WriteIoPortByte(registerPort, 0x87);
85 WinRing0.WriteIoPortByte(registerPort, 0x01);
86 WinRing0.WriteIoPortByte(registerPort, 0x55);
87 WinRing0.WriteIoPortByte(registerPort, 0x55);
90 private void IT87Exit() {
91 WinRing0.WriteIoPortByte(registerPort, CONFIGURATION_CONTROL_REGISTER);
92 WinRing0.WriteIoPortByte(valuePort, 0x02);
96 private const byte FINTEK_VENDOR_ID_REGISTER = 0x23;
97 private const ushort FINTEK_VENDOR_ID = 0x1934;
99 private const byte WINBOND_HARDWARE_MONITOR_LDN = 0x0B;
101 private const byte F71858_HARDWARE_MONITOR_LDN = 0x02;
102 private const byte FINTEK_HARDWARE_MONITOR_LDN = 0x04;
104 private void WinbondFintekEnter() {
105 WinRing0.WriteIoPortByte(registerPort, 0x87);
106 WinRing0.WriteIoPortByte(registerPort, 0x87);
109 private void WinbondFintekExit() {
110 WinRing0.WriteIoPortByte(registerPort, 0xAA);
114 private void SMSCEnter() {
115 WinRing0.WriteIoPortByte(registerPort, 0x55);
118 private void SMSCExit() {
119 WinRing0.WriteIoPortByte(registerPort, 0xAA);
122 private void ReportUnknownChip(string type, int chip) {
123 report.Append("Chip ID: Unknown ");
125 report.Append(" with ID 0x");
126 report.Append(chip.ToString("X", CultureInfo.InvariantCulture));
127 report.Append(" at 0x");
128 report.Append(registerPort.ToString("X", CultureInfo.InvariantCulture));
129 report.Append("/0x");
130 report.AppendLine(valuePort.ToString("X", CultureInfo.InvariantCulture));
134 private bool DetectWinbondFintek() {
135 WinbondFintekEnter();
137 byte logicalDeviceNumber;
138 byte id = ReadByte(CHIP_ID_REGISTER);
139 byte revision = ReadByte(CHIP_REVISION_REGISTER);
140 Chip chip = Chip.Unknown;
141 logicalDeviceNumber = 0;
147 logicalDeviceNumber = F71858_HARDWARE_MONITOR_LDN;
151 logicalDeviceNumber = FINTEK_HARDWARE_MONITOR_LDN;
158 logicalDeviceNumber = FINTEK_HARDWARE_MONITOR_LDN;
165 logicalDeviceNumber = FINTEK_HARDWARE_MONITOR_LDN;
172 logicalDeviceNumber = FINTEK_HARDWARE_MONITOR_LDN;
178 chip = Chip.F71889ED;
179 logicalDeviceNumber = FINTEK_HARDWARE_MONITOR_LDN;
187 chip = Chip.W83627HF;
188 logicalDeviceNumber = WINBOND_HARDWARE_MONITOR_LDN;
192 switch (revision & 0xF0) {
194 chip = Chip.W83627THF;
195 logicalDeviceNumber = WINBOND_HARDWARE_MONITOR_LDN;
201 chip = Chip.W83687THF;
202 logicalDeviceNumber = WINBOND_HARDWARE_MONITOR_LDN;
206 switch (revision & 0xF0) {
209 chip = Chip.W83627EHF;
210 logicalDeviceNumber = WINBOND_HARDWARE_MONITOR_LDN;
214 switch (revision & 0xF0) {
216 chip = Chip.W83627DHG;
217 logicalDeviceNumber = WINBOND_HARDWARE_MONITOR_LDN;
221 switch (revision & 0xF0) {
223 chip = Chip.W83667HG;
224 logicalDeviceNumber = WINBOND_HARDWARE_MONITOR_LDN;
228 switch (revision & 0xF0) {
230 chip = Chip.W83627DHGP;
231 logicalDeviceNumber = WINBOND_HARDWARE_MONITOR_LDN;
235 switch (revision & 0xF0) {
237 chip = Chip.W83667HGB;
238 logicalDeviceNumber = WINBOND_HARDWARE_MONITOR_LDN;
242 if (chip == Chip.Unknown) {
243 if (id != 0 && id != 0xff) {
246 ReportUnknownChip("Winbond / Fintek", ((id << 8) | revision));
250 Select(logicalDeviceNumber);
251 ushort address = ReadWord(BASE_ADDRESS_REGISTER);
253 ushort verify = ReadWord(BASE_ADDRESS_REGISTER);
255 ushort vendorID = ReadWord(FINTEK_VENDOR_ID_REGISTER);
259 if (address != verify) {
260 report.Append("Chip ID: 0x");
261 report.AppendLine(chip.ToString("X"));
262 report.Append("Chip revision: 0x");
263 report.AppendLine(revision.ToString("X",
264 CultureInfo.InvariantCulture));
265 report.AppendLine("Error: Address verification failed");
270 // some Fintek chips have address register offset 0x05 added already
271 if ((address & 0x07) == 0x05)
274 if (address < 0x100 || (address & 0xF007) != 0) {
275 report.Append("Chip ID: 0x");
276 report.AppendLine(chip.ToString("X"));
277 report.Append("Chip revision: 0x");
278 report.AppendLine(revision.ToString("X",
279 CultureInfo.InvariantCulture));
280 report.Append("Error: Invalid address 0x");
281 report.AppendLine(address.ToString("X",
282 CultureInfo.InvariantCulture));
289 case Chip.W83627DHGP:
296 superIOs.Add(new W836XX(chip, revision, address));
304 if (vendorID != FINTEK_VENDOR_ID) {
305 report.Append("Chip ID: 0x");
306 report.AppendLine(chip.ToString("X"));
307 report.Append("Chip revision: 0x");
308 report.AppendLine(revision.ToString("X",
309 CultureInfo.InvariantCulture));
310 report.Append("Error: Invalid vendor ID 0x");
311 report.AppendLine(vendorID.ToString("X",
312 CultureInfo.InvariantCulture));
316 superIOs.Add(new F718XX(chip, address));
327 private bool DetectIT87() {
330 ushort chipID = ReadWord(CHIP_ID_REGISTER);
333 case 0x8712: chip = Chip.IT8712F; break;
334 case 0x8716: chip = Chip.IT8716F; break;
335 case 0x8718: chip = Chip.IT8718F; break;
336 case 0x8720: chip = Chip.IT8720F; break;
337 case 0x8726: chip = Chip.IT8726F; break;
338 default: chip = Chip.Unknown; break;
340 if (chip == Chip.Unknown) {
341 if (chipID != 0 && chipID != 0xffff) {
344 ReportUnknownChip("ITE", chipID);
347 Select(IT87_ENVIRONMENT_CONTROLLER_LDN);
348 ushort address = ReadWord(BASE_ADDRESS_REGISTER);
350 ushort verify = ReadWord(BASE_ADDRESS_REGISTER);
352 byte version = (byte)(ReadByte(IT87_CHIP_VERSION_REGISTER) & 0x0F);
356 if (address != verify || address < 0x100 || (address & 0xF007) != 0) {
357 report.Append("Chip ID: 0x");
358 report.AppendLine(chip.ToString("X"));
359 report.Append("Error: Invalid address 0x");
360 report.AppendLine(address.ToString("X",
361 CultureInfo.InvariantCulture));
366 superIOs.Add(new IT87XX(chip, address, version));
373 private bool DetectSMSC() {
376 ushort chipID = ReadWord(CHIP_ID_REGISTER);
379 default: chip = Chip.Unknown; break;
381 if (chip == Chip.Unknown) {
382 if (chipID != 0 && chipID != 0xffff) {
385 ReportUnknownChip("SMSC", chipID);
395 private void Detect() {
397 for (int i = 0; i < REGISTER_PORTS.Length; i++) {
398 registerPort = REGISTER_PORTS[i];
399 valuePort = VALUE_PORTS[i];
401 if (DetectWinbondFintek()) continue;
403 if (DetectIT87()) continue;
405 if (DetectSMSC()) continue;
410 if (!WinRing0.IsAvailable)
413 if (!WinRing0.WaitIsaBusMutex(100))
418 WinRing0.ReleaseIsaBusMutex();
421 public ISuperIO[] SuperIO {
423 return superIOs.ToArray();
427 public string GetReport() {
428 if (report.Length > 0) {
429 report.Insert(0, "LPCIO" + Environment.NewLine +
430 Environment.NewLine);
431 return report.ToString();