2 * Copyright (c) 2004-2005 The Regents of The University of Michigan
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.
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.
30 * Tsunami I/O including PIC, PIT, RTC, DMA
39 #include "base/trace.hh"
40 #include "dev/tsunami_io.hh"
41 #include "dev/tsunami.hh"
42 #include "mem/bus/bus.hh"
43 #include "mem/bus/pio_interface.hh"
44 #include "mem/bus/pio_interface_impl.hh"
45 #include "sim/builder.hh"
46 #include "dev/tsunami_cchip.hh"
47 #include "dev/tsunamireg.h"
48 #include "dev/rtcreg.h"
49 #include "mem/functional/memory_control.hh"
53 #define UNIX_YEAR_OFFSET 52
55 // Timer Event for Periodic interrupt of RTC
56 TsunamiIO::RTCEvent::RTCEvent(Tsunami
* t
, Tick i
)
57 : Event(&mainEventQueue
), tsunami(t
), interval(i
)
59 DPRINTF(MC146818
, "RTC Event Initilizing\n");
60 schedule(curTick
+ interval
);
64 TsunamiIO::RTCEvent::process()
66 DPRINTF(MC146818
, "RTC Timer Interrupt\n");
67 schedule(curTick
+ interval
);
68 //Actually interrupt the processor here
69 tsunami
->cchip
->postRTC();
73 TsunamiIO::RTCEvent::description()
75 return "tsunami RTC interrupt";
79 TsunamiIO::RTCEvent::serialize(std::ostream
&os
)
82 SERIALIZE_SCALAR(time
);
86 TsunamiIO::RTCEvent::unserialize(Checkpoint
*cp
, const std::string
§ion
)
89 UNSERIALIZE_SCALAR(time
);
94 // Timer Event for PIT Timers
95 TsunamiIO::ClockEvent::ClockEvent()
96 : Event(&mainEventQueue
)
98 /* This is the PIT Tick Rate. A constant for the 8254 timer. The
99 * Tsunami platform has one of these cycle counters on the Cypress
100 * South Bridge and it is used by linux for estimating the cycle
101 * frequency of the machine it is running on. --Ali
103 interval
= (Tick
)(Clock::Float::s
/ 1193180.0);
105 DPRINTF(Tsunami
, "Clock Event Initilizing\n");
110 TsunamiIO::ClockEvent::process()
112 DPRINTF(Tsunami
, "Timer Interrupt\n");
114 status
= 0x20; // set bit that linux is looking for
116 schedule(curTick
+ interval
);
120 TsunamiIO::ClockEvent::Program(int count
)
122 DPRINTF(Tsunami
, "Timer set to curTick + %d\n", count
* interval
);
123 schedule(curTick
+ count
* interval
);
128 TsunamiIO::ClockEvent::description()
130 return "tsunami 8254 Interval timer";
134 TsunamiIO::ClockEvent::ChangeMode(uint8_t md
)
140 TsunamiIO::ClockEvent::Status()
146 TsunamiIO::ClockEvent::serialize(std::ostream
&os
)
148 Tick time
= scheduled() ? when() : 0;
149 SERIALIZE_SCALAR(time
);
150 SERIALIZE_SCALAR(status
);
151 SERIALIZE_SCALAR(mode
);
152 SERIALIZE_SCALAR(interval
);
156 TsunamiIO::ClockEvent::unserialize(Checkpoint
*cp
, const std::string
§ion
)
159 UNSERIALIZE_SCALAR(time
);
160 UNSERIALIZE_SCALAR(status
);
161 UNSERIALIZE_SCALAR(mode
);
162 UNSERIALIZE_SCALAR(interval
);
167 TsunamiIO::TsunamiIO(const string
&name
, Tsunami
*t
, time_t init_time
,
168 Addr a
, MemoryController
*mmu
, HierParams
*hier
, Bus
*bus
,
169 Tick pio_latency
, Tick ci
)
170 : PioDevice(name
, t
), addr(a
), clockInterval(ci
), tsunami(t
), rtc(t
, ci
)
172 mmu
->add_child(this, RangeSize(addr
, size
));
175 pioInterface
= newPioInterface(name
, hier
, bus
, this,
176 &TsunamiIO::cacheAccess
);
177 pioInterface
->addAddrRange(RangeSize(addr
, size
));
178 pioLatency
= pio_latency
* bus
->clockRate
;
181 // set the back pointer from tsunami to myself
185 set_time(init_time
== 0 ? time(NULL
) : init_time
);
188 picInterrupting
= false;
192 TsunamiIO::frequency() const
194 return Clock::Frequency
/ clockInterval
;
198 TsunamiIO::set_time(time_t t
)
201 DPRINTFN("Real-time clock set to %s", asctime(&tm
));
205 TsunamiIO::read(MemReqPtr
&req
, uint8_t *data
)
207 DPRINTF(Tsunami
, "io read va=%#x size=%d IOPorrt=%#x\n",
208 req
->vaddr
, req
->size
, req
->vaddr
& 0xfff);
210 Addr daddr
= (req
->paddr
- (addr
& EV5::PAddrImplMask
)) + 0x20;
214 case sizeof(uint8_t):
217 case TSDEV_PIC1_MASK
:
218 *(uint8_t*)data
= ~mask1
;
221 // !!! If this is modified 64bit case needs to be too
222 // Pal code has to do a 64 bit physical read because there is
223 // no load physical byte instruction
224 *(uint8_t*)data
= picr
;
227 // PIC2 not implemnted... just return 0
228 *(uint8_t*)data
= 0x00;
231 *(uint8_t*)data
= timer2
.Status();
236 *(uint8_t*)data
= uip
<< 7 | 0x26;
240 // DM and 24/12 and UIE
241 *(uint8_t*)data
= 0x46;
244 // If we want to support RTC user access in linux
245 // This won't work, but for now it's fine
246 *(uint8_t*)data
= 0x00;
249 panic("RTC Control Register D not implemented");
251 *(uint8_t *)data
= tm
.tm_sec
;
254 *(uint8_t *)data
= tm
.tm_min
;
257 *(uint8_t *)data
= tm
.tm_hour
;
260 *(uint8_t *)data
= tm
.tm_wday
;
263 *(uint8_t *)data
= tm
.tm_mday
;
265 *(uint8_t *)data
= tm
.tm_mon
+ 1;
268 *(uint8_t *)data
= tm
.tm_year
- UNIX_YEAR_OFFSET
;
271 panic("Unknown RTC Address\n");
274 /* Added for keyboard reads */
276 *(uint8_t *)data
= 0x00;
278 /* Added for ATA PCI DMA */
280 *(uint8_t *)data
= 0x00;
283 panic("I/O Read - va%#x size %d\n", req
->vaddr
, req
->size
);
285 case sizeof(uint16_t):
286 case sizeof(uint32_t):
287 panic("I/O Read - invalid size - va %#x size %d\n",
288 req
->vaddr
, req
->size
);
290 case sizeof(uint64_t):
293 // !!! If this is modified 8bit case needs to be too
294 // Pal code has to do a 64 bit physical read because there is
295 // no load physical byte instruction
296 *(uint64_t*)data
= (uint64_t)picr
;
299 panic("I/O Read - invalid size - va %#x size %d\n",
300 req
->vaddr
, req
->size
);
304 panic("I/O Read - invalid size - va %#x size %d\n",
305 req
->vaddr
, req
->size
);
307 panic("I/O Read - va%#x size %d\n", req
->vaddr
, req
->size
);
313 TsunamiIO::write(MemReqPtr
&req
, const uint8_t *data
)
317 uint8_t dt
= *(uint8_t*)data
;
321 DPRINTF(Tsunami
, "io write - va=%#x size=%d IOPort=%#x Data=%#x\n",
322 req
->vaddr
, req
->size
, req
->vaddr
& 0xfff, dt64
);
324 Addr daddr
= (req
->paddr
- (addr
& EV5::PAddrImplMask
)) + 0x20;
327 case sizeof(uint8_t):
329 case TSDEV_PIC1_MASK
:
330 mask1
= ~(*(uint8_t*)data
);
331 if ((picr
& mask1
) && !picInterrupting
) {
332 picInterrupting
= true;
333 tsunami
->cchip
->postDRIR(55);
334 DPRINTF(Tsunami
, "posting pic interrupt to cchip\n");
336 if ((!(picr
& mask1
)) && picInterrupting
) {
337 picInterrupting
= false;
338 tsunami
->cchip
->clearDRIR(55);
339 DPRINTF(Tsunami
, "clearing pic interrupt\n");
342 case TSDEV_PIC2_MASK
:
343 mask2
= *(uint8_t*)data
;
344 //PIC2 Not implemented to interrupt
347 // clear the interrupt on the PIC
348 picr
&= ~(1 << (*(uint8_t*)data
& 0xF));
350 tsunami
->cchip
->clearDRIR(55);
354 case TSDEV_DMA1_RESET
:
356 case TSDEV_DMA2_RESET
:
358 case TSDEV_DMA1_MODE
:
359 mode1
= *(uint8_t*)data
;
361 case TSDEV_DMA2_MODE
:
362 mode2
= *(uint8_t*)data
;
364 case TSDEV_DMA1_MASK
:
365 case TSDEV_DMA2_MASK
:
370 if ((*(uint8_t*)data
& 0x30) != 0x30)
371 panic("Only L/M write supported\n");
373 switch(*(uint8_t*)data
>> 6) {
375 timer0
.ChangeMode((*(uint8_t*)data
& 0xF) >> 1);
378 timer2
.ChangeMode((*(uint8_t*)data
& 0xF) >> 1);
381 panic("Read Back Command not implemented\n");
384 case TSDEV_TMR2_DATA
:
385 /* two writes before we actually start the Timer
386 so I set a flag in the timerData */
387 if(timerData
& 0x1000) {
389 timerData
+= *(uint8_t*)data
<< 8;
390 timer2
.Program(timerData
);
392 timerData
= *(uint8_t*)data
;
396 case TSDEV_TMR0_DATA
:
397 /* two writes before we actually start the Timer
398 so I set a flag in the timerData */
399 if(timerData
& 0x1000) {
401 timerData
+= *(uint8_t*)data
<< 8;
402 timer0
.Program(timerData
);
404 timerData
= *(uint8_t*)data
;
409 RTCAddress
= *(uint8_t*)data
;
414 panic("RTC Write not implmented (rtc.o won't work)\n");
416 panic("I/O Write - va%#x size %d\n", req
->vaddr
, req
->size
);
418 case sizeof(uint16_t):
419 case sizeof(uint32_t):
420 case sizeof(uint64_t):
422 panic("I/O Write - invalid size - va %#x size %d\n",
423 req
->vaddr
, req
->size
);
431 TsunamiIO::postPIC(uint8_t bitvector
)
433 //PIC2 Is not implemented, because nothing of interest there
436 tsunami
->cchip
->postDRIR(55);
437 DPRINTF(Tsunami
, "posting pic interrupt to cchip\n");
442 TsunamiIO::clearPIC(uint8_t bitvector
)
444 //PIC2 Is not implemented, because nothing of interest there
446 if (!(picr
& mask1
)) {
447 tsunami
->cchip
->clearDRIR(55);
448 DPRINTF(Tsunami
, "clearing pic interrupt to cchip\n");
453 TsunamiIO::cacheAccess(MemReqPtr
&req
)
455 return curTick
+ pioLatency
;
459 TsunamiIO::serialize(std::ostream
&os
)
461 SERIALIZE_SCALAR(timerData
);
462 SERIALIZE_SCALAR(uip
);
463 SERIALIZE_SCALAR(mask1
);
464 SERIALIZE_SCALAR(mask2
);
465 SERIALIZE_SCALAR(mode1
);
466 SERIALIZE_SCALAR(mode2
);
467 SERIALIZE_SCALAR(picr
);
468 SERIALIZE_SCALAR(picInterrupting
);
469 SERIALIZE_SCALAR(RTCAddress
);
471 // Serialize the timers
472 nameOut(os
, csprintf("%s.timer0", name()));
473 timer0
.serialize(os
);
474 nameOut(os
, csprintf("%s.timer2", name()));
475 timer2
.serialize(os
);
476 nameOut(os
, csprintf("%s.rtc", name()));
481 TsunamiIO::unserialize(Checkpoint
*cp
, const std::string
§ion
)
483 UNSERIALIZE_SCALAR(timerData
);
484 UNSERIALIZE_SCALAR(uip
);
485 UNSERIALIZE_SCALAR(mask1
);
486 UNSERIALIZE_SCALAR(mask2
);
487 UNSERIALIZE_SCALAR(mode1
);
488 UNSERIALIZE_SCALAR(mode2
);
489 UNSERIALIZE_SCALAR(picr
);
490 UNSERIALIZE_SCALAR(picInterrupting
);
491 UNSERIALIZE_SCALAR(RTCAddress
);
493 // Unserialize the timers
494 timer0
.unserialize(cp
, csprintf("%s.timer0", section
));
495 timer2
.unserialize(cp
, csprintf("%s.timer2", section
));
496 rtc
.unserialize(cp
, csprintf("%s.rtc", section
));
499 BEGIN_DECLARE_SIM_OBJECT_PARAMS(TsunamiIO
)
501 SimObjectParam
<Tsunami
*> tsunami
;
503 SimObjectParam
<MemoryController
*> mmu
;
505 SimObjectParam
<Bus
*> io_bus
;
506 Param
<Tick
> pio_latency
;
507 SimObjectParam
<HierParams
*> hier
;
508 Param
<Tick
> frequency
;
510 END_DECLARE_SIM_OBJECT_PARAMS(TsunamiIO
)
512 BEGIN_INIT_SIM_OBJECT_PARAMS(TsunamiIO
)
514 INIT_PARAM(tsunami
, "Tsunami"),
515 INIT_PARAM(time
, "System time to use (0 for actual time"),
516 INIT_PARAM(mmu
, "Memory Controller"),
517 INIT_PARAM(addr
, "Device Address"),
518 INIT_PARAM_DFLT(io_bus
, "The IO Bus to attach to", NULL
),
519 INIT_PARAM_DFLT(pio_latency
, "Programmed IO latency in bus cycles", 1),
520 INIT_PARAM_DFLT(hier
, "Hierarchy global variables", &defaultHierParams
),
521 INIT_PARAM(frequency
, "clock interrupt frequency")
523 END_INIT_SIM_OBJECT_PARAMS(TsunamiIO
)
525 CREATE_SIM_OBJECT(TsunamiIO
)
527 return new TsunamiIO(getInstanceName(), tsunami
, time
, addr
, mmu
, hier
,
528 io_bus
, pio_latency
, frequency
);
531 REGISTER_SIM_OBJECT("TsunamiIO", TsunamiIO
)