moel@34: /* moel@34: moel@34: Version: MPL 1.1/GPL 2.0/LGPL 2.1 moel@34: moel@34: The contents of this file are subject to the Mozilla Public License Version moel@34: 1.1 (the "License"); you may not use this file except in compliance with moel@34: the License. You may obtain a copy of the License at moel@34: moel@34: http://www.mozilla.org/MPL/ moel@34: moel@34: Software distributed under the License is distributed on an "AS IS" basis, moel@34: WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License moel@34: for the specific language governing rights and limitations under the License. moel@34: moel@34: The Original Code is the Open Hardware Monitor code. moel@34: moel@34: The Initial Developer of the Original Code is moel@34: Michael Möller . moel@34: Portions created by the Initial Developer are Copyright (C) 2009-2010 moel@34: the Initial Developer. All Rights Reserved. moel@34: moel@34: Contributor(s): moel@34: moel@34: Alternatively, the contents of this file may be used under the terms of moel@34: either the GNU General Public License Version 2 or later (the "GPL"), or moel@34: the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), moel@34: in which case the provisions of the GPL or the LGPL are applicable instead moel@34: of those above. If you wish to allow use of your version of this file only moel@34: under the terms of either the GPL or the LGPL, and not to allow others to moel@34: use your version of this file under the terms of the MPL, indicate your moel@34: decision by deleting the provisions above and replace them with the notice moel@34: and other provisions required by the GPL or the LGPL. If you do not delete moel@34: the provisions above, a recipient may use your version of this file under moel@34: the terms of any one of the MPL, the GPL or the LGPL. moel@34: moel@34: */ moel@34: moel@34: using System; moel@34: using System.Collections.Generic; moel@34: using System.Drawing; moel@34: using System.Text; moel@34: moel@34: namespace OpenHardwareMonitor.Hardware.LPC { moel@34: public class W836XX : LPCHardware, IHardware { moel@34: moel@34: private ushort address; moel@34: private byte revision; moel@34: moel@34: private bool available; moel@34: moel@34: private Sensor[] temperatures; moel@34: private Sensor[] fans; moel@34: private Sensor[] voltages; moel@34: moel@34: private float[] voltageGains; moel@34: private string[] fanNames; moel@34: moel@34: // Consts moel@34: private const ushort WINBOND_VENDOR_ID = 0x5CA3; moel@34: private const byte HIGH_BYTE = 0x80; moel@34: moel@34: // Hardware Monitor moel@34: private const byte ADDRESS_REGISTER_OFFSET = 0x05; moel@34: private const byte DATA_REGISTER_OFFSET = 0x06; moel@34: moel@34: // Hardware Monitor Registers moel@34: private const byte VOLTAGE_BASE_REG = 0x20; moel@34: private const byte BANK_SELECT_REGISTER = 0x04E; moel@34: private const byte VENDOR_ID_REGISTER = 0x4F; moel@34: private const byte TEMPERATURE_BASE_REG = 0x50; moel@34: private const byte TEMPERATURE_SYS_REG = 0x27; moel@34: moel@34: private byte[] FAN_TACHO_REG = new byte[] { 0x28, 0x29, 0x2A, 0x3F, 0x53 }; moel@34: private byte[] FAN_TACHO_BANK = new byte[] { 0, 0, 0, 0, 5 }; moel@34: private byte[] FAN_BIT_REG = new byte[] { 0x47, 0x4B, 0x4C, 0x59, 0x5D }; moel@34: private byte[] FAN_DIV_BIT0 = new byte[] { 36, 38, 30, 8, 10 }; moel@34: private byte[] FAN_DIV_BIT1 = new byte[] { 37, 39, 31, 9, 11 }; moel@34: private byte[] FAN_DIV_BIT2 = new byte[] { 5, 6, 7, 23, 15 }; moel@34: moel@34: private byte ReadByte(byte bank, byte register) { moel@34: WinRing0.WriteIoPortByte( moel@34: (ushort)(address + ADDRESS_REGISTER_OFFSET), BANK_SELECT_REGISTER); moel@34: WinRing0.WriteIoPortByte( moel@34: (ushort)(address + DATA_REGISTER_OFFSET), bank); moel@34: WinRing0.WriteIoPortByte( moel@34: (ushort)(address + ADDRESS_REGISTER_OFFSET), register); moel@34: return WinRing0.ReadIoPortByte( moel@34: (ushort)(address + DATA_REGISTER_OFFSET)); moel@34: } moel@34: moel@34: public W836XX(Chip chip, byte revision, ushort address) moel@34: : base(chip) moel@34: { moel@34: this.address = address; moel@34: this.revision = revision; moel@34: moel@34: available = IsWinbondVendor(); moel@34: moel@34: temperatures = new Sensor[3]; moel@34: temperatures[0] = new Sensor("CPU", 0, SensorType.Temperature, this); moel@34: temperatures[1] = new Sensor("Auxiliary", 1, SensorType.Temperature, this); moel@34: temperatures[2] = new Sensor("System", 2, SensorType.Temperature, this); moel@34: moel@34: switch (chip) { moel@34: case Chip.W83627DHG: moel@34: case Chip.W83627DHGP: moel@34: case Chip.W83627EHF: moel@34: case Chip.W83667HG: moel@34: case Chip.W83667HGB: moel@34: fanNames = new string[] { "System", "CPU", "Auxiliary", moel@34: "CPU #2", "Auxiliary #2" }; moel@34: voltageGains = new float[] { 1, 1, 1, 2, 1, 1, 1, 2 }; moel@34: voltages = new Sensor[3]; moel@34: voltages[0] = new Sensor("CPU VCore", 0, SensorType.Voltage, this); moel@34: voltages[1] = new Sensor("+3.3V", 3, SensorType.Voltage, this); moel@34: voltages[2] = new Sensor("Battery", 7, SensorType.Voltage, this); moel@34: break; moel@34: case Chip.W83627HF: moel@34: fanNames = new string[] { "Fan #1", "Fan #2", "Fan #3" }; moel@34: voltageGains = new float[] { 2, 1, 2, 1, 1, 1, 1, 2 }; moel@34: voltages = new Sensor[3]; moel@34: voltages[0] = new Sensor("CPU VCore", 0, SensorType.Voltage, this); moel@34: voltages[1] = new Sensor("+3.3V", 2, SensorType.Voltage, this); moel@34: voltages[2] = new Sensor("Battery", 7, SensorType.Voltage, this); moel@34: break; moel@34: default: fanNames = new string[0]; moel@34: break; moel@34: } moel@34: moel@34: fans = new Sensor[fanNames.Length]; moel@34: for (int i = 0; i < fanNames.Length; i++) moel@34: fans[i] = new Sensor(fanNames[i], i, SensorType.Fan, this); moel@34: } moel@34: moel@34: public bool IsAvailable { moel@34: get { return available; } moel@34: } moel@34: moel@34: private bool IsWinbondVendor() { moel@34: ushort vendorId = moel@34: (ushort)((ReadByte(HIGH_BYTE, VENDOR_ID_REGISTER) << 8) | moel@34: ReadByte(0, VENDOR_ID_REGISTER)); moel@34: return vendorId == WINBOND_VENDOR_ID; moel@34: } moel@34: moel@34: public void Update() { moel@34: foreach (Sensor sensor in voltages) { moel@34: if (sensor.Index < 7) { moel@34: int value = ReadByte(0, (byte)(VOLTAGE_BASE_REG + sensor.Index)); moel@34: sensor.Value = 0.008f * voltageGains[sensor.Index] * value; moel@34: if (sensor.Value > 0) moel@34: ActivateSensor(sensor); moel@34: else moel@34: DeactivateSensor(sensor); moel@34: } else { moel@34: // Battery voltage moel@34: bool valid = (ReadByte(0, 0x5D) & 0x01) > 0; moel@34: if (valid) { moel@34: sensor.Value = moel@34: 0.008f * voltageGains[sensor.Index] * ReadByte(5, 0x51); moel@34: ActivateSensor(sensor); moel@34: } else moel@34: DeactivateSensor(sensor); moel@34: } moel@34: } moel@34: moel@34: foreach (Sensor sensor in temperatures) { moel@34: int value; moel@34: if (sensor.Index < 2) { moel@34: value = (sbyte)ReadByte((byte)(sensor.Index + 1), TEMPERATURE_BASE_REG); moel@34: value = (value << 1) | ReadByte((byte)(sensor.Index + 1), moel@34: (byte)(TEMPERATURE_BASE_REG + 1)) >> 7; moel@34: } else { moel@34: value = (sbyte)ReadByte(0, TEMPERATURE_SYS_REG) << 1; moel@34: } moel@34: sensor.Value = value / 2.0f; moel@34: if (value < 0xFE) moel@34: ActivateSensor(sensor); moel@34: else moel@34: DeactivateSensor(sensor); moel@34: } moel@34: moel@34: long bits = 0; moel@34: for (int i = 0; i < FAN_BIT_REG.Length; i++) moel@34: bits = (bits << 8) | ReadByte(0, FAN_BIT_REG[i]); moel@34: foreach (Sensor sensor in fans) { moel@34: int count = ReadByte(FAN_TACHO_BANK[sensor.Index], moel@34: FAN_TACHO_REG[sensor.Index]); moel@34: int divisorBits = (int)( moel@34: (((bits >> FAN_DIV_BIT2[sensor.Index]) & 1) << 2) | moel@34: (((bits >> FAN_DIV_BIT1[sensor.Index]) & 1) << 1) | moel@34: ((bits >> FAN_DIV_BIT0[sensor.Index]) & 1)); moel@34: int divisor = 1 << divisorBits; moel@34: float value = (count < 0xff) ? 1.35e6f / (count * divisor) : 0; moel@34: sensor.Value = value; moel@34: if (value > 0) moel@34: ActivateSensor(sensor); moel@34: } moel@34: } moel@34: moel@34: public string GetReport() { moel@34: StringBuilder r = new StringBuilder(); moel@34: moel@34: r.AppendLine("LPC " + this.GetType().Name); moel@34: r.AppendLine(); moel@34: r.Append("Chip ID: 0x"); r.AppendLine(chip.ToString("X")); moel@34: r.Append("Chip revision: 0x"); r.AppendLine(revision.ToString("X")); moel@34: r.Append("Base Adress: 0x"); r.AppendLine(address.ToString("X4")); moel@34: r.AppendLine(); moel@34: r.AppendLine("Hardware Monitor Registers"); moel@34: r.AppendLine(); moel@34: r.AppendLine(" 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F"); moel@34: r.AppendLine(); moel@34: for (int i = 0; i < 0x7; i++) { moel@34: r.Append(" "); r.Append((i << 4).ToString("X2")); r.Append(" "); moel@34: for (int j = 0; j <= 0xF; j++) { moel@34: r.Append(" "); moel@34: r.Append(ReadByte(0, (byte)((i << 4) | j)).ToString("X2")); moel@34: } moel@34: r.AppendLine(); moel@34: } moel@34: for (int k = 1; k <= 5; k++) { moel@34: r.AppendLine("Bank " + k); moel@34: for (int i = 0x5; i < 0x6; i++) { moel@34: r.Append(" "); r.Append((i << 4).ToString("X2")); r.Append(" "); moel@34: for (int j = 0; j <= 0xF; j++) { moel@34: r.Append(" "); moel@34: r.Append(ReadByte((byte)(k), moel@34: (byte)((i << 4) | j)).ToString("X2")); moel@34: } moel@34: r.AppendLine(); moel@34: } moel@34: } moel@34: r.AppendLine(); moel@34: moel@34: return r.ToString(); moel@34: } moel@34: } moel@34: }