1/*
2 * Copyright (c) 2008 The Regents of The University of Michigan
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 *
28 * Authors: Gabe Black
29 */
30
31#ifndef __DEV_X86_I8042_HH__
32#define __DEV_X86_I8042_HH__
33
34#include <deque>
35
36#include "dev/intpin.hh"
37#include "dev/io_device.hh"
38#include "dev/ps2/device.hh"
39#include "params/I8042.hh"
40
41namespace X86ISA
42{
43
44class I8042 : public BasicPioDevice
45{
46  protected:
47    enum Command
48    {
49        GetCommandByte = 0x20,
50        ReadControllerRamBase = 0x20,
51        WriteCommandByte = 0x60,
52        WriteControllerRamBase = 0x60,
53        CheckForPassword = 0xA4,
54        LoadPassword = 0xA5,
55        CheckPassword = 0xA6,
56        DisableMouse = 0xA7,
57        EnableMouse = 0xA8,
58        TestMouse = 0xA9,
59        SelfTest = 0xAA,
60        InterfaceTest = 0xAB,
61        DiagnosticDump = 0xAC,
62        DisableKeyboard = 0xAD,
63        EnableKeyboard = 0xAE,
64        ReadInputPort = 0xC0,
65        ContinuousPollLow = 0xC1,
66        ContinuousPollHigh = 0xC2,
67        ReadOutputPort = 0xD0,
68        WriteOutputPort = 0xD1,
69        WriteKeyboardOutputBuff = 0xD2,
70        WriteMouseOutputBuff = 0xD3,
71        WriteToMouse = 0xD4,
72        DisableA20 = 0xDD,
73        EnableA20 = 0xDF,
74        ReadTestInputs = 0xE0,
75        PulseOutputBitBase = 0xF0,
76        SystemReset = 0xFE
77    };
78
79    BitUnion8(StatusReg)
80        Bitfield<7> parityError;
81        Bitfield<6> timeout;
82        Bitfield<5> mouseOutputFull;
83        Bitfield<4> keyboardUnlocked;
84        Bitfield<3> commandLast;
85        Bitfield<2> passedSelfTest;
86        Bitfield<1> inputFull;
87        Bitfield<0> outputFull;
88    EndBitUnion(StatusReg)
89
90    BitUnion8(CommandByte)
91        Bitfield<6> convertScanCodes;
92        Bitfield<5> disableMouse;
93        Bitfield<4> disableKeyboard;
94        Bitfield<2> passedSelfTest;
95        Bitfield<1> mouseFullInt;
96        Bitfield<0> keyboardFullInt;
97    EndBitUnion(CommandByte)
98
99    Tick latency;
100    Addr dataPort;
101    Addr commandPort;
102
103    StatusReg statusReg;
104    CommandByte commandByte;
105
106    uint8_t dataReg;
107
108    static const uint16_t NoCommand = (uint16_t)(-1);
109    uint16_t lastCommand;
110
111    std::vector<IntSourcePin<I8042> *> mouseIntPin;
112    std::vector<IntSourcePin<I8042> *> keyboardIntPin;
113
114    PS2Device *mouse;
115    PS2Device *keyboard;
116
117    void writeData(uint8_t newData, bool mouse = false);
118    uint8_t readDataOut();
119
120  public:
121    typedef I8042Params Params;
122
123    const Params *
124    params() const
125    {
126        return dynamic_cast<const Params *>(_params);
127    }
128
129    I8042(Params *p);
130
131    Port &
132    getPort(const std::string &if_name, PortID idx=InvalidPortID) override
133    {
134        if (if_name == "mouse_int_pin")
135            return *mouseIntPin.at(idx);
136        else if (if_name == "keyboard_int_pin")
137            return *keyboardIntPin.at(idx);
138        else
139            return BasicPioDevice::getPort(if_name, idx);
140    }
141
142    AddrRangeList getAddrRanges() const override;
143
144    Tick read(PacketPtr pkt) override;
145
146    Tick write(PacketPtr pkt) override;
147
148    void serialize(CheckpointOut &cp) const override;
149    void unserialize(CheckpointIn &cp) override;
150};
151
152} // namespace X86ISA
153
154#endif //__DEV_X86_I8042_HH__
155