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 "dev/pitreg.h"
43 #include "mem/bus/bus.hh"
44 #include "mem/bus/pio_interface.hh"
45 #include "mem/bus/pio_interface_impl.hh"
46 #include "sim/builder.hh"
47 #include "dev/tsunami_cchip.hh"
48 #include "dev/tsunamireg.h"
49 #include "dev/rtcreg.h"
50 #include "mem/functional/memory_control.hh"
54 TsunamiIO::RTC::RTC(const string
&name
, Tsunami
* t
, Tick i
)
55 : _name(name
), event(t
, i
), addr(0)
57 memset(clock_data
, 0, sizeof(clock_data
));
58 stat_regA
= RTCA_32768HZ
| RTCA_1024HZ
;
59 stat_regB
= RTCB_PRDC_IE
|RTCB_BIN
| RTCB_24HR
;
63 TsunamiIO::RTC::set_time(time_t t
)
71 wday
= tm
.tm_wday
+ 1;
76 DPRINTFN("Real-time clock set to %s", asctime(&tm
));
80 TsunamiIO::RTC::writeAddr(const uint8_t *data
)
82 if (*data
<= RTC_STAT_REGD
)
85 panic("RTC addresses over 0xD are not implemented.\n");
89 TsunamiIO::RTC::writeData(const uint8_t *data
)
91 if (addr
< RTC_STAT_REGA
)
92 clock_data
[addr
] = *data
;
96 if (*data
!= (RTCA_32768HZ
| RTCA_1024HZ
))
97 panic("Unimplemented RTC register A value write!\n");
101 if ((*data
& ~(RTCB_PRDC_IE
| RTCB_SQWE
)) != (RTCB_BIN
| RTCB_24HR
))
102 panic("Write to RTC reg B bits that are not implemented!\n");
104 if (*data
& RTCB_PRDC_IE
) {
105 if (!event
.scheduled())
106 event
.scheduleIntr();
108 if (event
.scheduled())
115 panic("RTC status registers C and D are not implemented.\n");
122 TsunamiIO::RTC::readData(uint8_t *data
)
124 if (addr
< RTC_STAT_REGA
)
125 *data
= clock_data
[addr
];
129 // toggle UIP bit for linux
130 stat_regA
^= RTCA_UIP
;
145 TsunamiIO::RTC::serialize(const string
&base
, ostream
&os
)
147 paramOut(os
, base
+ ".addr", addr
);
148 arrayParamOut(os
, base
+ ".clock_data", clock_data
, sizeof(clock_data
));
149 paramOut(os
, base
+ ".stat_regA", stat_regA
);
150 paramOut(os
, base
+ ".stat_regB", stat_regB
);
154 TsunamiIO::RTC::unserialize(const string
&base
, Checkpoint
*cp
,
155 const string
§ion
)
157 paramIn(cp
, section
, base
+ ".addr", addr
);
158 arrayParamIn(cp
, section
, base
+ ".clock_data", clock_data
,
160 paramIn(cp
, section
, base
+ ".stat_regA", stat_regA
);
161 paramIn(cp
, section
, base
+ ".stat_regB", stat_regB
);
163 // We're not unserializing the event here, but we need to
164 // rescehedule the event since curTick was moved forward by the
166 event
.reschedule(curTick
+ event
.interval
);
169 TsunamiIO::RTC::RTCEvent::RTCEvent(Tsunami
*t
, Tick i
)
170 : Event(&mainEventQueue
), tsunami(t
), interval(i
)
172 DPRINTF(MC146818
, "RTC Event Initilizing\n");
173 schedule(curTick
+ interval
);
177 TsunamiIO::RTC::RTCEvent::scheduleIntr()
179 schedule(curTick
+ interval
);
183 TsunamiIO::RTC::RTCEvent::process()
185 DPRINTF(MC146818
, "RTC Timer Interrupt\n");
186 schedule(curTick
+ interval
);
187 //Actually interrupt the processor here
188 tsunami
->cchip
->postRTC();
192 TsunamiIO::RTC::RTCEvent::description()
194 return "tsunami RTC interrupt";
197 TsunamiIO::PITimer::PITimer(const string
&name
)
198 : _name(name
), counter0(name
+ ".counter0"), counter1(name
+ ".counter1"),
199 counter2(name
+ ".counter2")
201 counter
[0] = &counter0
;
202 counter
[1] = &counter0
;
203 counter
[2] = &counter0
;
207 TsunamiIO::PITimer::writeControl(const uint8_t *data
)
212 sel
= GET_CTRL_SEL(*data
);
214 if (sel
== PIT_READ_BACK
)
215 panic("PITimer Read-Back Command is not implemented.\n");
217 rw
= GET_CTRL_RW(*data
);
219 if (rw
== PIT_RW_LATCH_COMMAND
)
220 counter
[sel
]->latchCount();
222 counter
[sel
]->setRW(rw
);
223 counter
[sel
]->setMode(GET_CTRL_MODE(*data
));
224 counter
[sel
]->setBCD(GET_CTRL_BCD(*data
));
229 TsunamiIO::PITimer::serialize(const string
&base
, ostream
&os
)
231 // serialize the counters
232 counter0
.serialize(base
+ ".counter0", os
);
233 counter1
.serialize(base
+ ".counter1", os
);
234 counter2
.serialize(base
+ ".counter2", os
);
238 TsunamiIO::PITimer::unserialize(const string
&base
, Checkpoint
*cp
,
239 const string
§ion
)
241 // unserialze the counters
242 counter0
.unserialize(base
+ ".counter0", cp
, section
);
243 counter1
.unserialize(base
+ ".counter1", cp
, section
);
244 counter2
.unserialize(base
+ ".counter2", cp
, section
);
247 TsunamiIO::PITimer::Counter::Counter(const string
&name
)
248 : _name(name
), event(this), count(0), latched_count(0), period(0),
249 mode(0), output_high(false), latch_on(false), read_byte(LSB
),
256 TsunamiIO::PITimer::Counter::latchCount()
258 // behave like a real latch
262 latched_count
= count
;
267 TsunamiIO::PITimer::Counter::read(uint8_t *data
)
273 *data
= (uint8_t)latched_count
;
278 *data
= latched_count
>> 8;
285 *data
= (uint8_t)count
;
296 TsunamiIO::PITimer::Counter::write(const uint8_t *data
)
298 switch (write_byte
) {
300 count
= (count
& 0xFF00) | *data
;
302 if (event
.scheduled())
309 count
= (count
& 0x00FF) | (*data
<< 8);
313 DPRINTF(Tsunami
, "Timer set to curTick + %d\n",
314 count
* event
.interval
);
315 event
.schedule(curTick
+ count
* event
.interval
);
323 TsunamiIO::PITimer::Counter::setRW(int rw_val
)
325 if (rw_val
!= PIT_RW_16BIT
)
326 panic("Only LSB/MSB read/write is implemented.\n");
330 TsunamiIO::PITimer::Counter::setMode(int mode_val
)
332 if(mode_val
!= PIT_MODE_INTTC
&& mode_val
!= PIT_MODE_RATEGEN
&&
333 mode_val
!= PIT_MODE_SQWAVE
)
334 panic("PIT mode %#x is not implemented: \n", mode_val
);
340 TsunamiIO::PITimer::Counter::setBCD(int bcd_val
)
342 if (bcd_val
!= PIT_BCD_FALSE
)
343 panic("PITimer does not implement BCD counts.\n");
347 TsunamiIO::PITimer::Counter::outputHigh()
353 TsunamiIO::PITimer::Counter::serialize(const string
&base
, ostream
&os
)
355 paramOut(os
, base
+ ".count", count
);
356 paramOut(os
, base
+ ".latched_count", latched_count
);
357 paramOut(os
, base
+ ".period", period
);
358 paramOut(os
, base
+ ".mode", mode
);
359 paramOut(os
, base
+ ".output_high", output_high
);
360 paramOut(os
, base
+ ".latch_on", latch_on
);
361 paramOut(os
, base
+ ".read_byte", read_byte
);
362 paramOut(os
, base
+ ".write_byte", write_byte
);
365 if (event
.scheduled())
366 event_tick
= event
.when();
367 paramOut(os
, base
+ ".event_tick", event_tick
);
371 TsunamiIO::PITimer::Counter::unserialize(const string
&base
, Checkpoint
*cp
,
372 const string
§ion
)
374 paramIn(cp
, section
, base
+ ".count", count
);
375 paramIn(cp
, section
, base
+ ".latched_count", latched_count
);
376 paramIn(cp
, section
, base
+ ".period", period
);
377 paramIn(cp
, section
, base
+ ".mode", mode
);
378 paramIn(cp
, section
, base
+ ".output_high", output_high
);
379 paramIn(cp
, section
, base
+ ".latch_on", latch_on
);
380 paramIn(cp
, section
, base
+ ".read_byte", read_byte
);
381 paramIn(cp
, section
, base
+ ".write_byte", write_byte
);
384 paramIn(cp
, section
, base
+ ".event_tick", event_tick
);
386 event
.schedule(event_tick
);
389 TsunamiIO::PITimer::Counter::CounterEvent::CounterEvent(Counter
* c_ptr
)
390 : Event(&mainEventQueue
)
392 interval
= (Tick
)(Clock::Float::s
/ 1193180.0);
397 TsunamiIO::PITimer::Counter::CounterEvent::process()
399 DPRINTF(Tsunami
, "Timer Interrupt\n");
400 switch (counter
->mode
) {
402 counter
->output_high
= true;
403 case PIT_MODE_RATEGEN
:
404 case PIT_MODE_SQWAVE
:
407 panic("Unimplemented PITimer mode.\n");
412 TsunamiIO::PITimer::Counter::CounterEvent::description()
414 return "tsunami 8254 Interval timer";
417 TsunamiIO::TsunamiIO(const string
&name
, Tsunami
*t
, time_t init_time
,
418 Addr a
, MemoryController
*mmu
, HierParams
*hier
, Bus
*bus
,
419 Tick pio_latency
, Tick ci
)
420 : PioDevice(name
, t
), addr(a
), clockInterval(ci
), tsunami(t
),
421 pitimer(name
+ "pitimer"), rtc(name
+ ".rtc", t
, ci
)
423 mmu
->add_child(this, RangeSize(addr
, size
));
426 pioInterface
= newPioInterface(name
+ ".pio", hier
, bus
, this,
427 &TsunamiIO::cacheAccess
);
428 pioInterface
->addAddrRange(RangeSize(addr
, size
));
429 pioLatency
= pio_latency
* bus
->clockRate
;
432 // set the back pointer from tsunami to myself
436 rtc
.set_time(init_time
== 0 ? time(NULL
) : init_time
);
438 picInterrupting
= false;
442 TsunamiIO::frequency() const
444 return Clock::Frequency
/ clockInterval
;
448 TsunamiIO::read(MemReqPtr
&req
, uint8_t *data
)
450 DPRINTF(Tsunami
, "io read va=%#x size=%d IOPorrt=%#x\n",
451 req
->vaddr
, req
->size
, req
->vaddr
& 0xfff);
453 Addr daddr
= (req
->paddr
- (addr
& EV5::PAddrImplMask
));
457 case sizeof(uint8_t):
460 case TSDEV_PIC1_MASK
:
461 *(uint8_t*)data
= ~mask1
;
463 case TSDEV_PIC2_MASK
:
464 *(uint8_t*)data
= ~mask2
;
467 // !!! If this is modified 64bit case needs to be too
468 // Pal code has to do a 64 bit physical read because there is
469 // no load physical byte instruction
470 *(uint8_t*)data
= picr
;
473 // PIC2 not implemnted... just return 0
474 *(uint8_t*)data
= 0x00;
476 case TSDEV_TMR0_DATA
:
477 pitimer
.counter0
.read(data
);
479 case TSDEV_TMR1_DATA
:
480 pitimer
.counter1
.read(data
);
482 case TSDEV_TMR2_DATA
:
483 pitimer
.counter2
.read(data
);
488 case TSDEV_CTRL_PORTB
:
489 if (pitimer
.counter2
.outputHigh())
490 *data
= PORTB_SPKR_HIGH
;
495 panic("I/O Read - va%#x size %d\n", req
->vaddr
, req
->size
);
497 case sizeof(uint16_t):
498 case sizeof(uint32_t):
499 panic("I/O Read - invalid size - va %#x size %d\n",
500 req
->vaddr
, req
->size
);
502 case sizeof(uint64_t):
505 // !!! If this is modified 8bit case needs to be too
506 // Pal code has to do a 64 bit physical read because there is
507 // no load physical byte instruction
508 *(uint64_t*)data
= (uint64_t)picr
;
511 panic("I/O Read - invalid size - va %#x size %d\n",
512 req
->vaddr
, req
->size
);
516 panic("I/O Read - invalid size - va %#x size %d\n",
517 req
->vaddr
, req
->size
);
519 panic("I/O Read - va%#x size %d\n", req
->vaddr
, req
->size
);
525 TsunamiIO::write(MemReqPtr
&req
, const uint8_t *data
)
529 uint8_t dt
= *(uint8_t*)data
;
533 DPRINTF(Tsunami
, "io write - va=%#x size=%d IOPort=%#x Data=%#x\n",
534 req
->vaddr
, req
->size
, req
->vaddr
& 0xfff, dt64
);
536 Addr daddr
= (req
->paddr
- (addr
& EV5::PAddrImplMask
));
539 case sizeof(uint8_t):
541 case TSDEV_PIC1_MASK
:
542 mask1
= ~(*(uint8_t*)data
);
543 if ((picr
& mask1
) && !picInterrupting
) {
544 picInterrupting
= true;
545 tsunami
->cchip
->postDRIR(55);
546 DPRINTF(Tsunami
, "posting pic interrupt to cchip\n");
548 if ((!(picr
& mask1
)) && picInterrupting
) {
549 picInterrupting
= false;
550 tsunami
->cchip
->clearDRIR(55);
551 DPRINTF(Tsunami
, "clearing pic interrupt\n");
554 case TSDEV_PIC2_MASK
:
555 mask2
= *(uint8_t*)data
;
556 //PIC2 Not implemented to interrupt
559 // clear the interrupt on the PIC
560 picr
&= ~(1 << (*(uint8_t*)data
& 0xF));
562 tsunami
->cchip
->clearDRIR(55);
564 case TSDEV_DMA1_CMND
:
566 case TSDEV_DMA2_CMND
:
568 case TSDEV_DMA1_MMASK
:
570 case TSDEV_DMA2_MMASK
:
574 case TSDEV_DMA1_RESET
:
576 case TSDEV_DMA2_RESET
:
578 case TSDEV_DMA1_MODE
:
579 mode1
= *(uint8_t*)data
;
581 case TSDEV_DMA2_MODE
:
582 mode2
= *(uint8_t*)data
;
584 case TSDEV_DMA1_MASK
:
585 case TSDEV_DMA2_MASK
:
587 case TSDEV_TMR0_DATA
:
588 pitimer
.counter0
.write(data
);
590 case TSDEV_TMR1_DATA
:
591 pitimer
.counter1
.write(data
);
593 case TSDEV_TMR2_DATA
:
594 pitimer
.counter2
.write(data
);
597 pitimer
.writeControl(data
);
607 case TSDEV_CTRL_PORTB
:
608 // System Control Port B not implemented
611 panic("I/O Write - va%#x size %d data %#x\n", req
->vaddr
, req
->size
, (int)*data
);
613 case sizeof(uint16_t):
614 case sizeof(uint32_t):
615 case sizeof(uint64_t):
617 panic("I/O Write - invalid size - va %#x size %d\n",
618 req
->vaddr
, req
->size
);
626 TsunamiIO::postPIC(uint8_t bitvector
)
628 //PIC2 Is not implemented, because nothing of interest there
631 tsunami
->cchip
->postDRIR(55);
632 DPRINTF(Tsunami
, "posting pic interrupt to cchip\n");
637 TsunamiIO::clearPIC(uint8_t bitvector
)
639 //PIC2 Is not implemented, because nothing of interest there
641 if (!(picr
& mask1
)) {
642 tsunami
->cchip
->clearDRIR(55);
643 DPRINTF(Tsunami
, "clearing pic interrupt to cchip\n");
648 TsunamiIO::cacheAccess(MemReqPtr
&req
)
650 return curTick
+ pioLatency
;
654 TsunamiIO::serialize(ostream
&os
)
656 SERIALIZE_SCALAR(timerData
);
657 SERIALIZE_SCALAR(mask1
);
658 SERIALIZE_SCALAR(mask2
);
659 SERIALIZE_SCALAR(mode1
);
660 SERIALIZE_SCALAR(mode2
);
661 SERIALIZE_SCALAR(picr
);
662 SERIALIZE_SCALAR(picInterrupting
);
664 // Serialize the timers
665 pitimer
.serialize("pitimer", os
);
666 rtc
.serialize("rtc", os
);
670 TsunamiIO::unserialize(Checkpoint
*cp
, const string
§ion
)
672 UNSERIALIZE_SCALAR(timerData
);
673 UNSERIALIZE_SCALAR(mask1
);
674 UNSERIALIZE_SCALAR(mask2
);
675 UNSERIALIZE_SCALAR(mode1
);
676 UNSERIALIZE_SCALAR(mode2
);
677 UNSERIALIZE_SCALAR(picr
);
678 UNSERIALIZE_SCALAR(picInterrupting
);
680 // Unserialize the timers
681 pitimer
.unserialize("pitimer", cp
, section
);
682 rtc
.unserialize("rtc", cp
, section
);
685 BEGIN_DECLARE_SIM_OBJECT_PARAMS(TsunamiIO
)
687 SimObjectParam
<Tsunami
*> tsunami
;
689 SimObjectParam
<MemoryController
*> mmu
;
691 SimObjectParam
<Bus
*> io_bus
;
692 Param
<Tick
> pio_latency
;
693 SimObjectParam
<HierParams
*> hier
;
694 Param
<Tick
> frequency
;
696 END_DECLARE_SIM_OBJECT_PARAMS(TsunamiIO
)
698 BEGIN_INIT_SIM_OBJECT_PARAMS(TsunamiIO
)
700 INIT_PARAM(tsunami
, "Tsunami"),
701 INIT_PARAM(time
, "System time to use (0 for actual time"),
702 INIT_PARAM(mmu
, "Memory Controller"),
703 INIT_PARAM(addr
, "Device Address"),
704 INIT_PARAM_DFLT(io_bus
, "The IO Bus to attach to", NULL
),
705 INIT_PARAM_DFLT(pio_latency
, "Programmed IO latency in bus cycles", 1),
706 INIT_PARAM_DFLT(hier
, "Hierarchy global variables", &defaultHierParams
),
707 INIT_PARAM(frequency
, "clock interrupt frequency")
709 END_INIT_SIM_OBJECT_PARAMS(TsunamiIO
)
711 CREATE_SIM_OBJECT(TsunamiIO
)
713 return new TsunamiIO(getInstanceName(), tsunami
, time
, addr
, mmu
, hier
,
714 io_bus
, pio_latency
, frequency
);
717 REGISTER_SIM_OBJECT("TsunamiIO", TsunamiIO
)