kmi.cc revision 11793:ef606668d247
1/*
2 * Copyright (c) 2010 ARM Limited
3 * All rights reserved
4 *
5 * The license below extends only to copyright in the software and shall
6 * not be construed as granting a license to any other intellectual
7 * property including but not limited to intellectual property relating
8 * to a hardware implementation of the functionality of the software
9 * licensed hereunder.  You may use the software subject to the license
10 * terms below provided that you ensure that this notice is replicated
11 * unmodified and in its entirety in all distributions of the software,
12 * modified or unmodified, in source code or in binary form.
13 *
14 * Copyright (c) 2005 The Regents of The University of Michigan
15 * All rights reserved.
16 *
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions are
19 * met: redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer;
21 * redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the distribution;
24 * neither the name of the copyright holders nor the names of its
25 * contributors may be used to endorse or promote products derived from
26 * this software without specific prior written permission.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
31 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
32 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
33 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
34 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
35 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
36 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
38 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 *
40 * Authors: Ali Saidi
41 *          William Wang
42 */
43
44#include "dev/arm/kmi.hh"
45
46#include "base/trace.hh"
47#include "base/vnc/vncinput.hh"
48#include "debug/Pl050.hh"
49#include "dev/arm/amba_device.hh"
50#include "dev/ps2.hh"
51#include "mem/packet.hh"
52#include "mem/packet_access.hh"
53
54Pl050::Pl050(const Params *p)
55    : AmbaIntDevice(p, 0xfff), control(0), status(0x43), clkdiv(0),
56      interrupts(0), rawInterrupts(0), ackNext(false), shiftDown(false),
57      vnc(p->vnc), driverInitialized(false), intEvent(this)
58{
59    if (vnc) {
60        if (!p->is_mouse)
61            vnc->setKeyboard(this);
62        else
63            vnc->setMouse(this);
64    }
65}
66
67Tick
68Pl050::read(PacketPtr pkt)
69{
70    assert(pkt->getAddr() >= pioAddr && pkt->getAddr() < pioAddr + pioSize);
71
72    Addr daddr = pkt->getAddr() - pioAddr;
73
74    uint32_t data = 0;
75
76    switch (daddr) {
77      case kmiCr:
78        DPRINTF(Pl050, "Read Commmand: %#x\n", (uint32_t)control);
79        data = control;
80        break;
81      case kmiStat:
82        if (rxQueue.empty())
83            status.rxfull = 0;
84        else
85            status.rxfull = 1;
86
87        DPRINTF(Pl050, "Read Status: %#x\n", (uint32_t)status);
88        data = status;
89        break;
90      case kmiData:
91        if (rxQueue.empty()) {
92            data = 0;
93        } else {
94            data = rxQueue.front();
95            rxQueue.pop_front();
96        }
97        DPRINTF(Pl050, "Read Data: %#x\n", (uint32_t)data);
98        updateIntStatus();
99        break;
100      case kmiClkDiv:
101        data = clkdiv;
102        break;
103      case kmiISR:
104        data = interrupts;
105        DPRINTF(Pl050, "Read Interrupts: %#x\n", (uint32_t)interrupts);
106        break;
107      default:
108        if (readId(pkt, ambaId, pioAddr)) {
109            // Hack for variable size accesses
110            data = pkt->get<uint32_t>();
111            break;
112        }
113
114        warn("Tried to read PL050 at offset %#x that doesn't exist\n", daddr);
115        break;
116    }
117
118    switch(pkt->getSize()) {
119      case 1:
120        pkt->set<uint8_t>(data);
121        break;
122      case 2:
123        pkt->set<uint16_t>(data);
124        break;
125      case 4:
126        pkt->set<uint32_t>(data);
127        break;
128      default:
129        panic("KMI read size too big?\n");
130        break;
131    }
132
133    pkt->makeAtomicResponse();
134    return pioDelay;
135}
136
137Tick
138Pl050::write(PacketPtr pkt)
139{
140
141    assert(pkt->getAddr() >= pioAddr && pkt->getAddr() < pioAddr + pioSize);
142
143    Addr daddr = pkt->getAddr() - pioAddr;
144
145    assert(pkt->getSize() == sizeof(uint8_t));
146
147
148    switch (daddr) {
149      case kmiCr:
150        DPRINTF(Pl050, "Write Commmand: %#x\n", (uint32_t)pkt->get<uint8_t>());
151        control = pkt->get<uint8_t>();
152        updateIntStatus();
153        break;
154      case kmiData:
155        DPRINTF(Pl050, "Write Data: %#x\n", (uint32_t)pkt->get<uint8_t>());
156        processCommand(pkt->get<uint8_t>());
157        updateIntStatus();
158        break;
159      case kmiClkDiv:
160        clkdiv = pkt->get<uint8_t>();
161        break;
162      default:
163        warn("Tried to write PL050 at offset %#x that doesn't exist\n", daddr);
164        break;
165    }
166    pkt->makeAtomicResponse();
167    return pioDelay;
168}
169
170void
171Pl050::processCommand(uint8_t byte)
172{
173    using namespace Ps2;
174
175    if (ackNext) {
176        ackNext--;
177        rxQueue.push_back(Ack);
178        updateIntStatus();
179        return;
180    }
181
182    switch (byte) {
183      case Ps2Reset:
184        rxQueue.push_back(Ack);
185        rxQueue.push_back(SelfTestPass);
186        break;
187      case SetResolution:
188      case SetRate:
189      case SetStatusLed:
190      case SetScaling1_1:
191      case SetScaling1_2:
192        rxQueue.push_back(Ack);
193        ackNext = 1;
194        break;
195      case ReadId:
196        rxQueue.push_back(Ack);
197        if (params()->is_mouse)
198            rxQueue.push_back(MouseId);
199        else
200            rxQueue.push_back(KeyboardId);
201        break;
202      case TpReadId:
203        if (!params()->is_mouse)
204            break;
205        // We're not a trackpoint device, this should make the probe go away
206        rxQueue.push_back(Ack);
207        rxQueue.push_back(0);
208        rxQueue.push_back(0);
209        // fall through
210      case Disable:
211      case Enable:
212      case SetDefaults:
213        rxQueue.push_back(Ack);
214        break;
215      case StatusRequest:
216        rxQueue.push_back(Ack);
217        rxQueue.push_back(0);
218        rxQueue.push_back(2); // default resolution
219        rxQueue.push_back(100); // default sample rate
220        break;
221      case TouchKitId:
222        ackNext = 2;
223        rxQueue.push_back(Ack);
224        rxQueue.push_back(TouchKitId);
225        rxQueue.push_back(1);
226        rxQueue.push_back('A');
227
228        driverInitialized = true;
229        break;
230      default:
231        panic("Unknown byte received: %d\n", byte);
232    }
233
234    updateIntStatus();
235}
236
237
238void
239Pl050::updateIntStatus()
240{
241    if (!rxQueue.empty())
242        rawInterrupts.rx = 1;
243    else
244        rawInterrupts.rx = 0;
245
246    interrupts.tx = rawInterrupts.tx & control.txint_enable;
247    interrupts.rx = rawInterrupts.rx & control.rxint_enable;
248
249    DPRINTF(Pl050, "rawInterupts=%#x control=%#x interrupts=%#x\n",
250            (uint32_t)rawInterrupts, (uint32_t)control, (uint32_t)interrupts);
251
252    if (interrupts && !intEvent.scheduled())
253        schedule(intEvent, curTick() + intDelay);
254}
255
256void
257Pl050::generateInterrupt()
258{
259
260    if (interrupts) {
261        gic->sendInt(intNum);
262        DPRINTF(Pl050, "Generated interrupt\n");
263    }
264}
265
266void
267Pl050::mouseAt(uint16_t x, uint16_t y, uint8_t buttons)
268{
269    using namespace Ps2;
270
271    // If the driver hasn't initialized the device yet, no need to try and send
272    // it anything. Similarly we can get vnc mouse events orders of maginture
273    // faster than m5 can process them. Only queue up two sets mouse movements
274    // and don't add more until those are processed.
275    if (!driverInitialized || rxQueue.size() > 10)
276        return;
277
278    // We shouldn't be here unless a vnc server called us in which case
279    // we should have a pointer to it
280    assert(vnc);
281
282    // Convert screen coordinates to touchpad coordinates
283    uint16_t _x = (2047.0/vnc->videoWidth()) * x;
284    uint16_t _y = (2047.0/vnc->videoHeight()) * y;
285
286    rxQueue.push_back(buttons);
287    rxQueue.push_back(_x >> 7);
288    rxQueue.push_back(_x & 0x7f);
289    rxQueue.push_back(_y >> 7);
290    rxQueue.push_back(_y & 0x7f);
291
292    updateIntStatus();
293}
294
295
296void
297Pl050::keyPress(uint32_t key, bool down)
298{
299    using namespace Ps2;
300
301    std::list<uint8_t> keys;
302
303    // convert the X11 keysym into ps2 codes
304    keySymToPs2(key, down, shiftDown, keys);
305
306    // Insert into our queue of charecters
307    rxQueue.splice(rxQueue.end(), keys);
308    updateIntStatus();
309}
310
311void
312Pl050::serialize(CheckpointOut &cp) const
313{
314    uint8_t ctrlreg = control;
315    SERIALIZE_SCALAR(ctrlreg);
316
317    uint8_t stsreg = status;
318    SERIALIZE_SCALAR(stsreg);
319    SERIALIZE_SCALAR(clkdiv);
320
321    uint8_t ints = interrupts;
322    SERIALIZE_SCALAR(ints);
323
324    uint8_t raw_ints = rawInterrupts;
325    SERIALIZE_SCALAR(raw_ints);
326
327    SERIALIZE_SCALAR(ackNext);
328    SERIALIZE_SCALAR(shiftDown);
329    SERIALIZE_SCALAR(driverInitialized);
330
331    SERIALIZE_CONTAINER(rxQueue);
332}
333
334void
335Pl050::unserialize(CheckpointIn &cp)
336{
337    uint8_t ctrlreg;
338    UNSERIALIZE_SCALAR(ctrlreg);
339    control = ctrlreg;
340
341    uint8_t stsreg;
342    UNSERIALIZE_SCALAR(stsreg);
343    status = stsreg;
344
345    UNSERIALIZE_SCALAR(clkdiv);
346
347    uint8_t ints;
348    UNSERIALIZE_SCALAR(ints);
349    interrupts = ints;
350
351    uint8_t raw_ints;
352    UNSERIALIZE_SCALAR(raw_ints);
353    rawInterrupts = raw_ints;
354
355    UNSERIALIZE_SCALAR(ackNext);
356    UNSERIALIZE_SCALAR(shiftDown);
357    UNSERIALIZE_SCALAR(driverInitialized);
358
359    UNSERIALIZE_CONTAINER(rxQueue);
360}
361
362
363
364Pl050 *
365Pl050Params::create()
366{
367    return new Pl050(this);
368}
369