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 Initializing\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 TsunamiIO::RTCEvent::scheduleIntr()
96 schedule(curTick
+ interval
);
99 // Timer Event for PIT Timers
100 TsunamiIO::ClockEvent::ClockEvent()
101 : Event(&mainEventQueue
)
103 /* This is the PIT Tick Rate. A constant for the 8254 timer. The
104 * Tsunami platform has one of these cycle counters on the Cypress
105 * South Bridge and it is used by linux for estimating the cycle
106 * frequency of the machine it is running on. --Ali
108 interval
= (Tick
)(Clock::Float::s
/ 1193180.0);
110 DPRINTF(Tsunami
, "Clock Event Initilizing\n");
116 read_byte
= READ_LSB
;
120 TsunamiIO::ClockEvent::process()
122 DPRINTF(Tsunami
, "Timer Interrupt\n");
124 status
= 0x20; // set bit that linux is looking for
126 schedule(curTick
+ current_count
*interval
);
130 TsunamiIO::ClockEvent::Program(int count
)
132 DPRINTF(Tsunami
, "Timer set to curTick + %d\n", count
* interval
);
133 schedule(curTick
+ count
* interval
);
136 current_count
= (uint16_t)count
;
140 TsunamiIO::ClockEvent::description()
142 return "tsunami 8254 Interval timer";
146 TsunamiIO::ClockEvent::ChangeMode(uint8_t md
)
152 TsunamiIO::ClockEvent::Status()
158 TsunamiIO::ClockEvent::LatchCount()
160 // behave like a real latch
163 read_byte
= READ_LSB
;
164 latched_count
= current_count
;
169 TsunamiIO::ClockEvent::Read()
176 read_byte
= READ_MSB
;
177 result
= (uint8_t)latched_count
;
180 read_byte
= READ_LSB
;
182 result
= latched_count
>> 8;
188 read_byte
= READ_MSB
;
189 result
= (uint8_t)current_count
;
192 read_byte
= READ_LSB
;
193 result
= current_count
>> 8;
203 TsunamiIO::ClockEvent::serialize(std::ostream
&os
)
205 Tick time
= scheduled() ? when() : 0;
206 SERIALIZE_SCALAR(time
);
207 SERIALIZE_SCALAR(status
);
208 SERIALIZE_SCALAR(mode
);
209 SERIALIZE_SCALAR(interval
);
213 TsunamiIO::ClockEvent::unserialize(Checkpoint
*cp
, const std::string
§ion
)
216 UNSERIALIZE_SCALAR(time
);
217 UNSERIALIZE_SCALAR(status
);
218 UNSERIALIZE_SCALAR(mode
);
219 UNSERIALIZE_SCALAR(interval
);
224 TsunamiIO::TsunamiIO(const string
&name
, Tsunami
*t
, time_t init_time
,
225 Addr a
, MemoryController
*mmu
, HierParams
*hier
, Bus
*bus
,
226 Tick pio_latency
, Tick ci
)
227 : PioDevice(name
, t
), addr(a
), clockInterval(ci
), tsunami(t
), rtc(t
, ci
)
229 mmu
->add_child(this, RangeSize(addr
, size
));
232 pioInterface
= newPioInterface(name
, hier
, bus
, this,
233 &TsunamiIO::cacheAccess
);
234 pioInterface
->addAddrRange(RangeSize(addr
, size
));
235 pioLatency
= pio_latency
* bus
->clockRate
;
238 // set the back pointer from tsunami to myself
242 set_time(init_time
== 0 ? time(NULL
) : init_time
);
245 picInterrupting
= false;
249 TsunamiIO::frequency() const
251 return Clock::Frequency
/ clockInterval
;
255 TsunamiIO::set_time(time_t t
)
258 DPRINTFN("Real-time clock set to %s", asctime(&tm
));
262 TsunamiIO::read(MemReqPtr
&req
, uint8_t *data
)
264 DPRINTF(Tsunami
, "io read va=%#x size=%d IOPorrt=%#x\n",
265 req
->vaddr
, req
->size
, req
->vaddr
& 0xfff);
267 Addr daddr
= (req
->paddr
- (addr
& EV5::PAddrImplMask
));
271 case sizeof(uint8_t):
274 case TSDEV_PIC1_MASK
:
275 *(uint8_t*)data
= ~mask1
;
277 case TSDEV_PIC2_MASK
:
278 *(uint8_t*)data
= ~mask2
;
281 // !!! If this is modified 64bit case needs to be too
282 // Pal code has to do a 64 bit physical read because there is
283 // no load physical byte instruction
284 *(uint8_t*)data
= picr
;
287 // PIC2 not implemnted... just return 0
288 *(uint8_t*)data
= 0x00;
291 *(uint8_t*)data
= timer2
.Status();
293 case TSDEV_TMR0_DATA
:
294 *(uint8_t *)data
= timer0
.Read();
299 *(uint8_t*)data
= uip
<< 7 | RTCA_32768HZ
| RTCA_1024HZ
;
303 // DM and 24/12 and UIE
304 *(uint8_t*)data
= RTCB_PRDC_IE
| RTCB_BIN
| RTCB_24HR
;
307 // If we want to support RTC user access in linux
308 // This won't work, but for now it's fine
309 *(uint8_t*)data
= 0x00;
312 panic("RTC Control Register D not implemented");
316 // RTC alarm functionality is not currently implemented
317 *(uint8_t *)data
= 0x00;
320 *(uint8_t *)data
= tm
.tm_sec
;
323 *(uint8_t *)data
= tm
.tm_min
;
326 *(uint8_t *)data
= tm
.tm_hour
;
329 *(uint8_t *)data
= tm
.tm_wday
+ 1;
332 *(uint8_t *)data
= tm
.tm_mday
;
335 *(uint8_t *)data
= tm
.tm_mon
+ 1;
338 *(uint8_t *)data
= tm
.tm_year
;
341 panic("Unknown RTC Address\n");
344 panic("I/O Read - va%#x size %d\n", req
->vaddr
, req
->size
);
346 case sizeof(uint16_t):
347 case sizeof(uint32_t):
348 panic("I/O Read - invalid size - va %#x size %d\n",
349 req
->vaddr
, req
->size
);
351 case sizeof(uint64_t):
354 // !!! If this is modified 8bit case needs to be too
355 // Pal code has to do a 64 bit physical read because there is
356 // no load physical byte instruction
357 *(uint64_t*)data
= (uint64_t)picr
;
360 panic("I/O Read - invalid size - va %#x size %d\n",
361 req
->vaddr
, req
->size
);
365 panic("I/O Read - invalid size - va %#x size %d\n",
366 req
->vaddr
, req
->size
);
368 panic("I/O Read - va%#x size %d\n", req
->vaddr
, req
->size
);
374 TsunamiIO::write(MemReqPtr
&req
, const uint8_t *data
)
378 uint8_t dt
= *(uint8_t*)data
;
382 DPRINTF(Tsunami
, "io write - va=%#x size=%d IOPort=%#x Data=%#x\n",
383 req
->vaddr
, req
->size
, req
->vaddr
& 0xfff, dt64
);
385 Addr daddr
= (req
->paddr
- (addr
& EV5::PAddrImplMask
));
388 case sizeof(uint8_t):
390 case TSDEV_PIC1_MASK
:
391 mask1
= ~(*(uint8_t*)data
);
392 if ((picr
& mask1
) && !picInterrupting
) {
393 picInterrupting
= true;
394 tsunami
->cchip
->postDRIR(55);
395 DPRINTF(Tsunami
, "posting pic interrupt to cchip\n");
397 if ((!(picr
& mask1
)) && picInterrupting
) {
398 picInterrupting
= false;
399 tsunami
->cchip
->clearDRIR(55);
400 DPRINTF(Tsunami
, "clearing pic interrupt\n");
403 case TSDEV_PIC2_MASK
:
404 mask2
= *(uint8_t*)data
;
405 //PIC2 Not implemented to interrupt
408 // clear the interrupt on the PIC
409 picr
&= ~(1 << (*(uint8_t*)data
& 0xF));
411 tsunami
->cchip
->clearDRIR(55);
415 case TSDEV_DMA1_RESET
:
417 case TSDEV_DMA2_RESET
:
419 case TSDEV_DMA1_MODE
:
420 mode1
= *(uint8_t*)data
;
422 case TSDEV_DMA2_MODE
:
423 mode2
= *(uint8_t*)data
;
425 case TSDEV_DMA1_MASK
:
426 case TSDEV_DMA2_MASK
:
431 switch((*(uint8_t*)data
>> 4) & 0x3) {
433 switch(*(uint8_t*)data
>> 6) {
441 panic("Read Back Command not implemented\n");
447 panic("Only L/M write and Counter-Latch read supported\n");
450 switch(*(uint8_t*)data
>> 6) {
452 timer0
.ChangeMode((*(uint8_t*)data
& 0xF) >> 1);
455 timer2
.ChangeMode((*(uint8_t*)data
& 0xF) >> 1);
458 panic("Read Back Command not implemented\n");
461 case TSDEV_TMR2_DATA
:
462 /* two writes before we actually start the Timer
463 so I set a flag in the timerData */
464 if(timerData
& 0x1000) {
466 timerData
+= *(uint8_t*)data
<< 8;
467 timer2
.Program(timerData
);
469 timerData
= *(uint8_t*)data
;
473 case TSDEV_TMR0_DATA
:
474 /* two writes before we actually start the Timer
475 so I set a flag in the timerData */
476 if(timerData
& 0x1000) {
477 timerData
&= ~0x1000;
478 timerData
+= *(uint8_t*)data
<< 8;
479 timer0
.Program(timerData
);
482 timerData
= *(uint8_t*)data
;
487 RTCAddress
= *(uint8_t*)data
;
494 if (*data
!= (RTCA_32768HZ
| RTCA_1024HZ
))
495 panic("Unimplemented RTC register A value write!\n");
498 if ((*data
& ~(RTCB_PRDC_IE
| RTCB_SQWE
)) != (RTCB_BIN
| RTCB_24HR
))
499 panic("Write to RTC reg B bits that are not implemented!\n");
501 if (*data
& RTCB_PRDC_IE
) {
502 if (!rtc
.scheduled())
510 panic("Write to RTC reg C not implemented!\n");
513 panic("Write to RTC reg D not implemented!\n");
516 tm
.tm_sec
= *(uint8_t *)data
;
519 tm
.tm_min
= *(uint8_t *)data
;
522 tm
.tm_hour
= *(uint8_t *)data
;
525 tm
.tm_wday
= *(uint8_t *)data
;
528 tm
.tm_mday
= *(uint8_t *)data
;
531 tm
.tm_mon
= *(uint8_t *)data
;
534 tm
.tm_year
= *(uint8_t *)data
;
538 panic("I/O Write - va%#x size %d\n", req
->vaddr
, req
->size
);
540 case sizeof(uint16_t):
541 case sizeof(uint32_t):
542 case sizeof(uint64_t):
544 panic("I/O Write - invalid size - va %#x size %d\n",
545 req
->vaddr
, req
->size
);
553 TsunamiIO::postPIC(uint8_t bitvector
)
555 //PIC2 Is not implemented, because nothing of interest there
558 tsunami
->cchip
->postDRIR(55);
559 DPRINTF(Tsunami
, "posting pic interrupt to cchip\n");
564 TsunamiIO::clearPIC(uint8_t bitvector
)
566 //PIC2 Is not implemented, because nothing of interest there
568 if (!(picr
& mask1
)) {
569 tsunami
->cchip
->clearDRIR(55);
570 DPRINTF(Tsunami
, "clearing pic interrupt to cchip\n");
575 TsunamiIO::cacheAccess(MemReqPtr
&req
)
577 return curTick
+ pioLatency
;
581 TsunamiIO::serialize(std::ostream
&os
)
583 SERIALIZE_SCALAR(timerData
);
584 SERIALIZE_SCALAR(uip
);
585 SERIALIZE_SCALAR(mask1
);
586 SERIALIZE_SCALAR(mask2
);
587 SERIALIZE_SCALAR(mode1
);
588 SERIALIZE_SCALAR(mode2
);
589 SERIALIZE_SCALAR(picr
);
590 SERIALIZE_SCALAR(picInterrupting
);
591 SERIALIZE_SCALAR(RTCAddress
);
593 // Serialize the timers
594 nameOut(os
, csprintf("%s.timer0", name()));
595 timer0
.serialize(os
);
596 nameOut(os
, csprintf("%s.timer2", name()));
597 timer2
.serialize(os
);
598 nameOut(os
, csprintf("%s.rtc", name()));
603 TsunamiIO::unserialize(Checkpoint
*cp
, const std::string
§ion
)
605 UNSERIALIZE_SCALAR(timerData
);
606 UNSERIALIZE_SCALAR(uip
);
607 UNSERIALIZE_SCALAR(mask1
);
608 UNSERIALIZE_SCALAR(mask2
);
609 UNSERIALIZE_SCALAR(mode1
);
610 UNSERIALIZE_SCALAR(mode2
);
611 UNSERIALIZE_SCALAR(picr
);
612 UNSERIALIZE_SCALAR(picInterrupting
);
613 UNSERIALIZE_SCALAR(RTCAddress
);
615 // Unserialize the timers
616 timer0
.unserialize(cp
, csprintf("%s.timer0", section
));
617 timer2
.unserialize(cp
, csprintf("%s.timer2", section
));
618 rtc
.unserialize(cp
, csprintf("%s.rtc", section
));
621 BEGIN_DECLARE_SIM_OBJECT_PARAMS(TsunamiIO
)
623 SimObjectParam
<Tsunami
*> tsunami
;
625 SimObjectParam
<MemoryController
*> mmu
;
627 SimObjectParam
<Bus
*> io_bus
;
628 Param
<Tick
> pio_latency
;
629 SimObjectParam
<HierParams
*> hier
;
630 Param
<Tick
> frequency
;
632 END_DECLARE_SIM_OBJECT_PARAMS(TsunamiIO
)
634 BEGIN_INIT_SIM_OBJECT_PARAMS(TsunamiIO
)
636 INIT_PARAM(tsunami
, "Tsunami"),
637 INIT_PARAM(time
, "System time to use (0 for actual time"),
638 INIT_PARAM(mmu
, "Memory Controller"),
639 INIT_PARAM(addr
, "Device Address"),
640 INIT_PARAM_DFLT(io_bus
, "The IO Bus to attach to", NULL
),
641 INIT_PARAM_DFLT(pio_latency
, "Programmed IO latency in bus cycles", 1),
642 INIT_PARAM_DFLT(hier
, "Hierarchy global variables", &defaultHierParams
),
643 INIT_PARAM(frequency
, "clock interrupt frequency")
645 END_INIT_SIM_OBJECT_PARAMS(TsunamiIO
)
647 CREATE_SIM_OBJECT(TsunamiIO
)
649 return new TsunamiIO(getInstanceName(), tsunami
, time
, addr
, mmu
, hier
,
650 io_bus
, pio_latency
, frequency
);
653 REGISTER_SIM_OBJECT("TsunamiIO", TsunamiIO
)