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@54: private const byte BANK_SELECT_REGISTER = 0x4E; moel@34: private const byte VENDOR_ID_REGISTER = 0x4F; moel@56: private const byte TEMPERATURE_SOURCE_SELECT_REG = 0x49; moel@56: moel@56: private string[] TEMPERATURE_NAME = moel@56: new string[] {"CPU", "Auxiliary", "System"}; moel@56: private byte[] TEMPERATURE_REG = new byte[] { 0x50, 0x50, 0x27 }; moel@56: private byte[] TEMPERATURE_BANK = new byte[] { 1, 2, 0 }; 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@85: } moel@34: moel@56: private void WriteByte(byte bank, byte register, byte value) { moel@56: WinRing0.WriteIoPortByte( moel@56: (ushort)(address + ADDRESS_REGISTER_OFFSET), BANK_SELECT_REGISTER); moel@56: WinRing0.WriteIoPortByte( moel@56: (ushort)(address + DATA_REGISTER_OFFSET), bank); moel@56: WinRing0.WriteIoPortByte( moel@56: (ushort)(address + ADDRESS_REGISTER_OFFSET), register); moel@56: WinRing0.WriteIoPortByte( moel@56: (ushort)(address + DATA_REGISTER_OFFSET), value); moel@56: } moel@56: 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@63: ParameterDescription[] parameter = new ParameterDescription[] { moel@63: new ParameterDescription("Offset", "Temperature offset.", 0) moel@63: }; moel@56: List list = new List(); moel@56: switch (chip) { moel@63: case Chip.W83667HG: moel@63: case Chip.W83667HGB: moel@63: // do not add temperature sensor registers that read PECI moel@63: byte flag = ReadByte(0, TEMPERATURE_SOURCE_SELECT_REG); moel@63: if ((flag & 0x04) == 0) moel@63: list.Add(new Sensor(TEMPERATURE_NAME[0], 0, null, moel@63: SensorType.Temperature, this, parameter)); moel@63: if ((flag & 0x40) == 0) moel@63: list.Add(new Sensor(TEMPERATURE_NAME[1], 1, null, moel@63: SensorType.Temperature, this, parameter)); moel@63: list.Add(new Sensor(TEMPERATURE_NAME[2], 2, null, moel@63: SensorType.Temperature, this, parameter)); moel@63: break; moel@63: case Chip.W83627DHG: moel@63: case Chip.W83627DHGP: moel@63: // do not add temperature sensor registers that read PECI moel@56: byte sel = ReadByte(0, TEMPERATURE_SOURCE_SELECT_REG); moel@56: if ((sel & 0x07) == 0) moel@63: list.Add(new Sensor(TEMPERATURE_NAME[0], 0, null, moel@63: SensorType.Temperature, this, parameter)); moel@56: if ((sel & 0x70) == 0) moel@63: list.Add(new Sensor(TEMPERATURE_NAME[1], 1, null, moel@63: SensorType.Temperature, this, parameter)); moel@63: list.Add(new Sensor(TEMPERATURE_NAME[2], 2, null, moel@63: SensorType.Temperature, this, parameter)); moel@56: break; moel@56: default: moel@63: // no PECI support, add all sensors moel@63: for (int i = 0; i < TEMPERATURE_NAME.Length; i++) moel@63: list.Add(new Sensor(TEMPERATURE_NAME[i], i, null, moel@63: SensorType.Temperature, this, parameter)); moel@56: break; moel@56: } moel@56: temperatures = list.ToArray(); 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@54: case Chip.W83627HF: moel@54: case Chip.W83627THF: moel@67: case Chip.W83687THF: moel@54: fanNames = new string[] { "System", "CPU", "Auxiliary" }; 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@85: private ulong SetBit(ulong target, int bit, int value) { moel@85: if ((value & 1) != value) moel@85: throw new ArgumentException("Value must be one bit only."); moel@85: moel@85: if (bit < 0 || bit > 63) moel@85: throw new ArgumentException("Bit out of range."); moel@85: moel@85: ulong mask = (((ulong)1) << bit); moel@85: return value > 0 ? target | mask : target & ~mask; moel@85: } moel@85: moel@34: public void Update() { moel@56: moel@34: foreach (Sensor sensor in voltages) { moel@34: if (sensor.Index < 7) { moel@58: // two special VCore measurement modes for W83627THF moel@67: if ((chip == Chip.W83627HF || chip == Chip.W83627THF || moel@67: chip == Chip.W83687THF) && sensor.Index == 0) moel@67: { moel@58: byte vrmConfiguration = ReadByte(0, 0x18); moel@58: int value = ReadByte(0, VOLTAGE_BASE_REG); moel@58: if ((vrmConfiguration & 0x01) == 0) moel@58: sensor.Value = 0.016f * value; // VRM8 formula moel@58: else moel@58: sensor.Value = 0.00488f * value + 0.69f; // VRM9 formula moel@58: } else { moel@58: int value = ReadByte(0, (byte)(VOLTAGE_BASE_REG + sensor.Index)); moel@58: sensor.Value = 0.008f * voltageGains[sensor.Index] * value; moel@58: } moel@34: if (sensor.Value > 0) moel@34: ActivateSensor(sensor); moel@34: } else { moel@34: // Battery voltage moel@34: bool valid = (ReadByte(0, 0x5D) & 0x01) > 0; moel@34: if (valid) { moel@56: sensor.Value = moel@34: 0.008f * voltageGains[sensor.Index] * ReadByte(5, 0x51); moel@34: ActivateSensor(sensor); moel@84: } else { moel@84: sensor.Value = null; moel@84: } moel@34: } moel@34: } moel@34: moel@34: foreach (Sensor sensor in temperatures) { moel@63: int value = ((sbyte)ReadByte(TEMPERATURE_BANK[sensor.Index], moel@63: TEMPERATURE_REG[sensor.Index])) << 1; moel@63: if (TEMPERATURE_BANK[sensor.Index] > 0) moel@63: value |= ReadByte(TEMPERATURE_BANK[sensor.Index], moel@63: (byte)(TEMPERATURE_REG[sensor.Index] + 1)) >> 7; moel@63: moel@56: float temperature = value / 2.0f; moel@56: if (temperature <= 125 && temperature >= -55) { moel@63: sensor.Value = temperature + sensor.Parameters[0].Value; moel@56: ActivateSensor(sensor); moel@34: } else { moel@84: sensor.Value = null; moel@34: } moel@34: } moel@34: moel@85: ulong 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@85: ulong newBits = bits; 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@85: moel@85: // assemble fan divisor 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@85: moel@34: float value = (count < 0xff) ? 1.35e6f / (count * divisor) : 0; moel@34: sensor.Value = value; moel@34: if (value > 0) moel@85: ActivateSensor(sensor); moel@85: moel@85: // update fan divisor moel@85: if (count > 192 && divisorBits < 7) moel@85: divisorBits++; moel@85: if (count < 96 && divisorBits > 0) moel@85: divisorBits--; moel@85: moel@85: newBits = SetBit(newBits, FAN_DIV_BIT2[sensor.Index], moel@85: (divisorBits >> 2) & 1); moel@85: newBits = SetBit(newBits, FAN_DIV_BIT1[sensor.Index], moel@85: (divisorBits >> 1) & 1); moel@85: newBits = SetBit(newBits, FAN_DIV_BIT0[sensor.Index], moel@85: divisorBits & 1); moel@85: } moel@85: moel@85: // write new fan divisors moel@85: for (int i = FAN_BIT_REG.Length - 1; i >= 0; i--) { moel@85: byte oldByte = (byte)(bits & 0xFF); moel@85: byte newByte = (byte)(newBits & 0xFF); moel@85: bits = bits >> 8; moel@85: newBits = newBits >> 8; moel@85: if (oldByte != newByte) moel@85: WriteByte(0, FAN_BIT_REG[i], newByte); moel@85: } 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@56: 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@53: for (int k = 1; k <= 15; 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: }