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