1/*
2 * Copyright (c) 2004-2005 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: Ali Saidi
29 *          Andrew Schultz
30 *          Miguel Serrano
31 */
32
33/** @file
34 * Tsunami I/O including PIC, PIT, RTC, DMA
35 */
36
37#include "dev/alpha/tsunami_io.hh"
38
39#include <sys/time.h>
40
41#include <deque>
42#include <string>
43#include <vector>
44
45#include "base/time.hh"
46#include "base/trace.hh"
47#include "config/the_isa.hh"
48#include "debug/Tsunami.hh"
49#include "dev/alpha/tsunami.hh"
50#include "dev/alpha/tsunami_cchip.hh"
51#include "dev/alpha/tsunamireg.h"
52#include "dev/rtcreg.h"
53#include "mem/packet.hh"
54#include "mem/packet_access.hh"
55#include "mem/port.hh"
56#include "sim/system.hh"
57
58// clang complains about std::set being overloaded with Packet::set if
59// we open up the entire namespace std
60using std::string;
61using std::ostream;
62
63//Should this be AlphaISA?
64using namespace TheISA;
65
66TsunamiIO::RTC::RTC(const string &n, const TsunamiIOParams *p)
67    : MC146818(p->tsunami, n, p->time, p->year_is_bcd, p->frequency),
68      tsunami(p->tsunami)
69{
70}
71
72TsunamiIO::TsunamiIO(const Params *p)
73    : BasicPioDevice(p, 0x100), tsunami(p->tsunami),
74      pitimer(this, p->name + "pitimer"), rtc(p->name + ".rtc", p)
75{
76    // set the back pointer from tsunami to myself
77    tsunami->io = this;
78
79    timerData = 0;
80    picr = 0;
81    picInterrupting = false;
82}
83
84Tick
85TsunamiIO::frequency() const
86{
87    return SimClock::Frequency / params()->frequency;
88}
89
90Tick
91TsunamiIO::read(PacketPtr pkt)
92{
93    assert(pkt->getAddr() >= pioAddr && pkt->getAddr() < pioAddr + pioSize);
94
95    Addr daddr = pkt->getAddr() - pioAddr;
96
97    DPRINTF(Tsunami, "io read  va=%#x size=%d IOPorrt=%#x\n", pkt->getAddr(),
98            pkt->getSize(), daddr);
99
100    if (pkt->getSize() == sizeof(uint8_t)) {
101        switch(daddr) {
102          // PIC1 mask read
103          case TSDEV_PIC1_MASK:
104            pkt->setLE(~mask1);
105            break;
106          case TSDEV_PIC2_MASK:
107            pkt->setLE(~mask2);
108            break;
109          case TSDEV_PIC1_ISR:
110              // !!! If this is modified 64bit case needs to be too
111              // Pal code has to do a 64 bit physical read because there is
112              // no load physical byte instruction
113              pkt->setLE(picr);
114              break;
115          case TSDEV_PIC2_ISR:
116              // PIC2 not implemnted... just return 0
117              pkt->setLE(0x00);
118              break;
119          case TSDEV_TMR0_DATA:
120            pkt->setLE(pitimer.readCounter(0));
121            break;
122          case TSDEV_TMR1_DATA:
123            pkt->setLE(pitimer.readCounter(1));
124            break;
125          case TSDEV_TMR2_DATA:
126            pkt->setLE(pitimer.readCounter(2));
127            break;
128          case TSDEV_RTC_DATA:
129            pkt->setLE(rtc.readData(rtcAddr));
130            break;
131          case TSDEV_CTRL_PORTB:
132            if (pitimer.outputHigh(2))
133                pkt->setLE(PORTB_SPKR_HIGH);
134            else
135                pkt->setLE(0x00);
136            break;
137          default:
138            panic("I/O Read - va%#x size %d\n", pkt->getAddr(), pkt->getSize());
139        }
140    } else if (pkt->getSize() == sizeof(uint64_t)) {
141        if (daddr == TSDEV_PIC1_ISR)
142            pkt->setLE<uint64_t>(picr);
143        else
144           panic("I/O Read - invalid addr - va %#x size %d\n",
145                   pkt->getAddr(), pkt->getSize());
146    } else {
147       panic("I/O Read - invalid size - va %#x size %d\n", pkt->getAddr(), pkt->getSize());
148    }
149    pkt->makeAtomicResponse();
150    return pioDelay;
151}
152
153Tick
154TsunamiIO::write(PacketPtr pkt)
155{
156    assert(pkt->getAddr() >= pioAddr && pkt->getAddr() < pioAddr + pioSize);
157    Addr daddr = pkt->getAddr() - pioAddr;
158
159    DPRINTF(Tsunami, "io write - va=%#x size=%d IOPort=%#x Data=%#x\n",
160            pkt->getAddr(), pkt->getSize(), pkt->getAddr() & 0xfff,
161            (uint32_t)pkt->getLE<uint8_t>());
162
163    assert(pkt->getSize() == sizeof(uint8_t));
164
165    switch(daddr) {
166      case TSDEV_PIC1_MASK:
167        mask1 = ~(pkt->getLE<uint8_t>());
168        if ((picr & mask1) && !picInterrupting) {
169            picInterrupting = true;
170            tsunami->cchip->postDRIR(55);
171            DPRINTF(Tsunami, "posting pic interrupt to cchip\n");
172        }
173        if ((!(picr & mask1)) && picInterrupting) {
174            picInterrupting = false;
175            tsunami->cchip->clearDRIR(55);
176            DPRINTF(Tsunami, "clearing pic interrupt\n");
177        }
178        break;
179      case TSDEV_PIC2_MASK:
180        mask2 = pkt->getLE<uint8_t>();
181        //PIC2 Not implemented to interrupt
182        break;
183      case TSDEV_PIC1_ACK:
184        // clear the interrupt on the PIC
185        picr &= ~(1 << (pkt->getLE<uint8_t>() & 0xF));
186        if (!(picr & mask1))
187            tsunami->cchip->clearDRIR(55);
188        break;
189      case TSDEV_DMA1_MODE:
190        mode1 = pkt->getLE<uint8_t>();
191        break;
192      case TSDEV_DMA2_MODE:
193        mode2 = pkt->getLE<uint8_t>();
194        break;
195      case TSDEV_TMR0_DATA:
196        pitimer.writeCounter(0, pkt->getLE<uint8_t>());
197        break;
198      case TSDEV_TMR1_DATA:
199        pitimer.writeCounter(1, pkt->getLE<uint8_t>());
200        break;
201      case TSDEV_TMR2_DATA:
202        pitimer.writeCounter(2, pkt->getLE<uint8_t>());
203        break;
204      case TSDEV_TMR_CTRL:
205        pitimer.writeControl(pkt->getLE<uint8_t>());
206        break;
207      case TSDEV_RTC_ADDR:
208        rtcAddr = pkt->getLE<uint8_t>();
209        break;
210      case TSDEV_RTC_DATA:
211        rtc.writeData(rtcAddr, pkt->getLE<uint8_t>());
212        break;
213      case TSDEV_KBD:
214      case TSDEV_DMA1_CMND:
215      case TSDEV_DMA2_CMND:
216      case TSDEV_DMA1_MMASK:
217      case TSDEV_DMA2_MMASK:
218      case TSDEV_PIC2_ACK:
219      case TSDEV_DMA1_RESET:
220      case TSDEV_DMA2_RESET:
221      case TSDEV_DMA1_MASK:
222      case TSDEV_DMA2_MASK:
223      case TSDEV_CTRL_PORTB:
224        break;
225      default:
226        panic("I/O Write - va%#x size %d data %#x\n",
227                pkt->getAddr(), pkt->getSize(), pkt->getLE<uint8_t>());
228    }
229
230    pkt->makeAtomicResponse();
231    return pioDelay;
232}
233
234void
235TsunamiIO::postPIC(uint8_t bitvector)
236{
237    //PIC2 Is not implemented, because nothing of interest there
238    picr |= bitvector;
239    if (picr & mask1) {
240        tsunami->cchip->postDRIR(55);
241        DPRINTF(Tsunami, "posting pic interrupt to cchip\n");
242    }
243}
244
245void
246TsunamiIO::clearPIC(uint8_t bitvector)
247{
248    //PIC2 Is not implemented, because nothing of interest there
249    picr &= ~bitvector;
250    if (!(picr & mask1)) {
251        tsunami->cchip->clearDRIR(55);
252        DPRINTF(Tsunami, "clearing pic interrupt to cchip\n");
253    }
254}
255
256void
257TsunamiIO::serialize(CheckpointOut &cp) const
258{
259    SERIALIZE_SCALAR(rtcAddr);
260    SERIALIZE_SCALAR(timerData);
261    SERIALIZE_SCALAR(mask1);
262    SERIALIZE_SCALAR(mask2);
263    SERIALIZE_SCALAR(mode1);
264    SERIALIZE_SCALAR(mode2);
265    SERIALIZE_SCALAR(picr);
266    SERIALIZE_SCALAR(picInterrupting);
267
268    // Serialize the timers
269    pitimer.serialize("pitimer", cp);
270    rtc.serialize("rtc", cp);
271}
272
273void
274TsunamiIO::unserialize(CheckpointIn &cp)
275{
276    UNSERIALIZE_SCALAR(rtcAddr);
277    UNSERIALIZE_SCALAR(timerData);
278    UNSERIALIZE_SCALAR(mask1);
279    UNSERIALIZE_SCALAR(mask2);
280    UNSERIALIZE_SCALAR(mode1);
281    UNSERIALIZE_SCALAR(mode2);
282    UNSERIALIZE_SCALAR(picr);
283    UNSERIALIZE_SCALAR(picInterrupting);
284
285    // Unserialize the timers
286    pitimer.unserialize("pitimer", cp);
287    rtc.unserialize("rtc", cp);
288}
289
290void
291TsunamiIO::startup()
292{
293    rtc.startup();
294    pitimer.startup();
295}
296
297TsunamiIO *
298TsunamiIOParams::create()
299{
300    return new TsunamiIO(this);
301}
302