Hardware/LPC/Winbond.cs
author moel.mich
Sun, 07 Feb 2010 19:53:51 +0000
changeset 31 c4d1fb76a9e1
permissions -rw-r--r--
Added initial support for W83627HF. Some refactoring for IHardware classes.
     1 /*
     2   
     3   Version: MPL 1.1/GPL 2.0/LGPL 2.1
     4 
     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
     8  
     9   http://www.mozilla.org/MPL/
    10 
    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.
    14 
    15   The Original Code is the Open Hardware Monitor code.
    16 
    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.
    21 
    22   Contributor(s):
    23 
    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.
    35  
    36 */
    37 
    38 using System;
    39 using System.Collections.Generic;
    40 using System.Drawing;
    41 using System.Text;
    42 
    43 namespace OpenHardwareMonitor.Hardware.LPC {
    44   public abstract class Winbond : LPCHardware {
    45 
    46     private ushort address;
    47     private byte revision;
    48 
    49     private bool available;
    50 
    51     // Consts 
    52     private const ushort WINBOND_VENDOR_ID = 0x5CA3;
    53     private const byte HIGH_BYTE = 0x80;
    54 
    55     // Hardware Monitor
    56     private const byte ADDRESS_REGISTER_OFFSET = 0x05;
    57     private const byte DATA_REGISTER_OFFSET = 0x06;
    58 
    59     // Hardware Monitor Registers
    60     private const byte BANK_SELECT_REGISTER = 0x04E;
    61     private const byte VENDOR_ID_REGISTER = 0x4F;
    62 
    63     protected byte ReadByte(byte bank, byte register) {
    64       WinRing0.WriteIoPortByte(
    65          (ushort)(address + ADDRESS_REGISTER_OFFSET), BANK_SELECT_REGISTER);
    66       WinRing0.WriteIoPortByte(
    67          (ushort)(address + DATA_REGISTER_OFFSET), bank);
    68       WinRing0.WriteIoPortByte(
    69          (ushort)(address + ADDRESS_REGISTER_OFFSET), register);
    70       return WinRing0.ReadIoPortByte(
    71         (ushort)(address + DATA_REGISTER_OFFSET));
    72     } 
    73 
    74     private bool IsWinbondVendor() {
    75       ushort vendorId =
    76         (ushort)((ReadByte(HIGH_BYTE, VENDOR_ID_REGISTER) << 8) |
    77            ReadByte(0, VENDOR_ID_REGISTER));
    78       return vendorId == WINBOND_VENDOR_ID;
    79     }
    80 
    81     public Winbond(Chip chip, byte revision, ushort address)
    82       : base(chip) 
    83     {
    84       this.address = address;
    85       this.revision = revision;
    86 
    87       available = IsWinbondVendor();
    88     }
    89 
    90     public bool IsAvailable {
    91       get { return available; }
    92     }    
    93    
    94     public string GetReport() {
    95       StringBuilder r = new StringBuilder();
    96 
    97       r.AppendLine("LPC " + this.GetType().Name);
    98       r.AppendLine();
    99       r.Append("Chip ID: 0x"); r.AppendLine(chip.ToString("X"));
   100       r.Append("Chip revision: 0x"); r.AppendLine(revision.ToString("X"));
   101       r.Append("Base Adress: 0x"); r.AppendLine(address.ToString("X4"));
   102       r.AppendLine();
   103       r.AppendLine("Hardware Monitor Registers");
   104       r.AppendLine();
   105       r.AppendLine("      00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F");
   106       r.AppendLine();
   107       for (int i = 0; i < 0x7; i++) {
   108         r.Append(" "); r.Append((i << 4).ToString("X2")); r.Append("  ");
   109         for (int j = 0; j <= 0xF; j++) {
   110           r.Append(" ");
   111           r.Append(ReadByte(0, (byte)((i << 4) | j)).ToString("X2"));
   112         }
   113         r.AppendLine();
   114       }
   115       for (int k = 1; k <= 5; k++) {
   116         r.AppendLine("Bank " + k);
   117         for (int i = 0x5; i < 0x6; i++) {
   118           r.Append(" "); r.Append((i << 4).ToString("X2")); r.Append("  ");
   119           for (int j = 0; j <= 0xF; j++) {
   120             r.Append(" ");
   121             r.Append(ReadByte((byte)(k),
   122               (byte)((i << 4) | j)).ToString("X2"));
   123           }
   124           r.AppendLine();
   125         }
   126       }
   127       r.AppendLine();
   128 
   129       return r.ToString();
   130     }
   131   }
   132 }