Small bits of refactoring.
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 readonly List<ISuperIO> superIOs = new List<ISuperIO>();
48 private readonly StringBuilder report = new StringBuilder();
51 private readonly ushort[] REGISTER_PORTS = new ushort[] { 0x2E, 0x4E };
52 private readonly 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);
79 private void ReportUnknownChip(string type, int chip) {
80 report.Append("Chip ID: Unknown ");
82 report.Append(" with ID 0x");
83 report.Append(chip.ToString("X", CultureInfo.InvariantCulture));
84 report.Append(" at 0x");
85 report.Append(registerPort.ToString("X", CultureInfo.InvariantCulture));
87 report.AppendLine(valuePort.ToString("X", CultureInfo.InvariantCulture));
91 #region Winbond, Fintek
93 private const byte FINTEK_VENDOR_ID_REGISTER = 0x23;
94 private const ushort FINTEK_VENDOR_ID = 0x1934;
96 private const byte WINBOND_HARDWARE_MONITOR_LDN = 0x0B;
98 private const byte F71858_HARDWARE_MONITOR_LDN = 0x02;
99 private const byte FINTEK_HARDWARE_MONITOR_LDN = 0x04;
101 private void WinbondFintekEnter() {
102 WinRing0.WriteIoPortByte(registerPort, 0x87);
103 WinRing0.WriteIoPortByte(registerPort, 0x87);
106 private void WinbondFintekExit() {
107 WinRing0.WriteIoPortByte(registerPort, 0xAA);
110 private bool DetectWinbondFintek() {
111 WinbondFintekEnter();
113 byte logicalDeviceNumber = 0;
114 byte id = ReadByte(CHIP_ID_REGISTER);
115 byte revision = ReadByte(CHIP_REVISION_REGISTER);
116 Chip chip = Chip.Unknown;
122 logicalDeviceNumber = F71858_HARDWARE_MONITOR_LDN;
126 logicalDeviceNumber = FINTEK_HARDWARE_MONITOR_LDN;
133 logicalDeviceNumber = FINTEK_HARDWARE_MONITOR_LDN;
140 logicalDeviceNumber = FINTEK_HARDWARE_MONITOR_LDN;
147 logicalDeviceNumber = FINTEK_HARDWARE_MONITOR_LDN;
153 chip = Chip.F71889ED;
154 logicalDeviceNumber = FINTEK_HARDWARE_MONITOR_LDN;
162 chip = Chip.W83627HF;
163 logicalDeviceNumber = WINBOND_HARDWARE_MONITOR_LDN;
167 switch (revision & 0xF0) {
169 chip = Chip.W83627THF;
170 logicalDeviceNumber = WINBOND_HARDWARE_MONITOR_LDN;
176 chip = Chip.W83687THF;
177 logicalDeviceNumber = WINBOND_HARDWARE_MONITOR_LDN;
181 switch (revision & 0xF0) {
184 chip = Chip.W83627EHF;
185 logicalDeviceNumber = WINBOND_HARDWARE_MONITOR_LDN;
189 switch (revision & 0xF0) {
191 chip = Chip.W83627DHG;
192 logicalDeviceNumber = WINBOND_HARDWARE_MONITOR_LDN;
196 switch (revision & 0xF0) {
198 chip = Chip.W83667HG;
199 logicalDeviceNumber = WINBOND_HARDWARE_MONITOR_LDN;
203 switch (revision & 0xF0) {
205 chip = Chip.W83627DHGP;
206 logicalDeviceNumber = WINBOND_HARDWARE_MONITOR_LDN;
210 switch (revision & 0xF0) {
212 chip = Chip.W83667HGB;
213 logicalDeviceNumber = WINBOND_HARDWARE_MONITOR_LDN;
217 if (chip == Chip.Unknown) {
218 if (id != 0 && id != 0xff) {
221 ReportUnknownChip("Winbond / Fintek", ((id << 8) | revision));
225 Select(logicalDeviceNumber);
226 ushort address = ReadWord(BASE_ADDRESS_REGISTER);
228 ushort verify = ReadWord(BASE_ADDRESS_REGISTER);
230 ushort vendorID = ReadWord(FINTEK_VENDOR_ID_REGISTER);
234 if (address != verify) {
235 report.Append("Chip ID: 0x");
236 report.AppendLine(chip.ToString("X"));
237 report.Append("Chip revision: 0x");
238 report.AppendLine(revision.ToString("X",
239 CultureInfo.InvariantCulture));
240 report.AppendLine("Error: Address verification failed");
245 // some Fintek chips have address register offset 0x05 added already
246 if ((address & 0x07) == 0x05)
249 if (address < 0x100 || (address & 0xF007) != 0) {
250 report.Append("Chip ID: 0x");
251 report.AppendLine(chip.ToString("X"));
252 report.Append("Chip revision: 0x");
253 report.AppendLine(revision.ToString("X",
254 CultureInfo.InvariantCulture));
255 report.Append("Error: Invalid address 0x");
256 report.AppendLine(address.ToString("X",
257 CultureInfo.InvariantCulture));
264 case Chip.W83627DHGP:
271 superIOs.Add(new W836XX(chip, revision, address));
279 if (vendorID != FINTEK_VENDOR_ID) {
280 report.Append("Chip ID: 0x");
281 report.AppendLine(chip.ToString("X"));
282 report.Append("Chip revision: 0x");
283 report.AppendLine(revision.ToString("X",
284 CultureInfo.InvariantCulture));
285 report.Append("Error: Invalid vendor ID 0x");
286 report.AppendLine(vendorID.ToString("X",
287 CultureInfo.InvariantCulture));
291 superIOs.Add(new F718XX(chip, address));
306 private const byte IT87_ENVIRONMENT_CONTROLLER_LDN = 0x04;
307 private const byte IT87_CHIP_VERSION_REGISTER = 0x22;
309 private void IT87Enter() {
310 WinRing0.WriteIoPortByte(registerPort, 0x87);
311 WinRing0.WriteIoPortByte(registerPort, 0x01);
312 WinRing0.WriteIoPortByte(registerPort, 0x55);
313 WinRing0.WriteIoPortByte(registerPort, 0x55);
316 private void IT87Exit() {
317 WinRing0.WriteIoPortByte(registerPort, CONFIGURATION_CONTROL_REGISTER);
318 WinRing0.WriteIoPortByte(valuePort, 0x02);
321 private bool DetectIT87() {
323 // IT87XX can enter only on port 0x2E
324 if (registerPort != 0x2E)
329 ushort chipID = ReadWord(CHIP_ID_REGISTER);
332 case 0x8712: chip = Chip.IT8712F; break;
333 case 0x8716: chip = Chip.IT8716F; break;
334 case 0x8718: chip = Chip.IT8718F; break;
335 case 0x8720: chip = Chip.IT8720F; break;
336 case 0x8721: chip = Chip.IT8721F; 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));
377 private void SMSCEnter() {
378 WinRing0.WriteIoPortByte(registerPort, 0x55);
381 private void SMSCExit() {
382 WinRing0.WriteIoPortByte(registerPort, 0xAA);
385 private bool DetectSMSC() {
388 ushort chipID = ReadWord(CHIP_ID_REGISTER);
391 default: chip = Chip.Unknown; break;
393 if (chip == Chip.Unknown) {
394 if (chipID != 0 && chipID != 0xffff) {
397 ReportUnknownChip("SMSC", chipID);
409 private void Detect() {
411 for (int i = 0; i < REGISTER_PORTS.Length; i++) {
412 registerPort = REGISTER_PORTS[i];
413 valuePort = VALUE_PORTS[i];
415 if (DetectWinbondFintek()) continue;
417 if (DetectIT87()) continue;
419 if (DetectSMSC()) continue;
424 if (!WinRing0.IsAvailable)
427 if (!WinRing0.WaitIsaBusMutex(100))
432 WinRing0.ReleaseIsaBusMutex();
435 public ISuperIO[] SuperIO {
437 return superIOs.ToArray();
441 public string GetReport() {
442 if (report.Length > 0) {
443 return "LPCIO" + Environment.NewLine + Environment.NewLine + report;