intel_8254_timer.cc revision 11793:ef606668d247
112855Sgabeblack@google.com/*
212855Sgabeblack@google.com * Copyright (c) 2004, 2005
312855Sgabeblack@google.com * The Regents of The University of Michigan
412855Sgabeblack@google.com * All Rights Reserved
512855Sgabeblack@google.com *
612855Sgabeblack@google.com * This code is part of the M5 simulator.
712855Sgabeblack@google.com *
812855Sgabeblack@google.com * Permission is granted to use, copy, create derivative works and
912855Sgabeblack@google.com * redistribute this software and such derivative works for any
1012855Sgabeblack@google.com * purpose, so long as the copyright notice above, this grant of
1112855Sgabeblack@google.com * permission, and the disclaimer below appear in all copies made; and
1212855Sgabeblack@google.com * so long as the name of The University of Michigan is not used in
1312855Sgabeblack@google.com * any advertising or publicity pertaining to the use or distribution
1412855Sgabeblack@google.com * of this software without specific, written prior authorization.
1512855Sgabeblack@google.com *
1612855Sgabeblack@google.com * THIS SOFTWARE IS PROVIDED AS IS, WITHOUT REPRESENTATION FROM THE
1712855Sgabeblack@google.com * UNIVERSITY OF MICHIGAN AS TO ITS FITNESS FOR ANY PURPOSE, AND
1812855Sgabeblack@google.com * WITHOUT WARRANTY BY THE UNIVERSITY OF MICHIGAN OF ANY KIND, EITHER
1912855Sgabeblack@google.com * EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED
2012855Sgabeblack@google.com * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2112855Sgabeblack@google.com * PURPOSE. THE REGENTS OF THE UNIVERSITY OF MICHIGAN SHALL NOT BE
2212855Sgabeblack@google.com * LIABLE FOR ANY DAMAGES, INCLUDING DIRECT, SPECIAL, INDIRECT,
2312855Sgabeblack@google.com * INCIDENTAL, OR CONSEQUENTIAL DAMAGES, WITH RESPECT TO ANY CLAIM
2412855Sgabeblack@google.com * ARISING OUT OF OR IN CONNECTION WITH THE USE OF THE SOFTWARE, EVEN
2512855Sgabeblack@google.com * IF IT HAS BEEN OR IS HEREAFTER ADVISED OF THE POSSIBILITY OF SUCH
2612855Sgabeblack@google.com * DAMAGES.
2712855Sgabeblack@google.com *
2812855Sgabeblack@google.com * Authors: Ali G. Saidi
2912855Sgabeblack@google.com *          Andrew L. Schultz
3012855Sgabeblack@google.com *          Miguel J. Serrano
3112855Sgabeblack@google.com */
3212855Sgabeblack@google.com
3312855Sgabeblack@google.com#include "dev/intel_8254_timer.hh"
3412855Sgabeblack@google.com
3512855Sgabeblack@google.com#include "base/misc.hh"
3612855Sgabeblack@google.com#include "debug/Intel8254Timer.hh"
3712855Sgabeblack@google.com
3812855Sgabeblack@google.comusing namespace std;
3912855Sgabeblack@google.com
4012855Sgabeblack@google.comIntel8254Timer::Intel8254Timer(EventManager *em, const string &name,
4112855Sgabeblack@google.com    Counter *counter0, Counter *counter1, Counter *counter2) :
4212855Sgabeblack@google.com    EventManager(em), _name(name)
4312855Sgabeblack@google.com{
4412855Sgabeblack@google.com    counter[0] = counter0;
4512855Sgabeblack@google.com    counter[1] = counter1;
4612855Sgabeblack@google.com    counter[2] = counter2;
4712855Sgabeblack@google.com}
4812855Sgabeblack@google.com
4912855Sgabeblack@google.comIntel8254Timer::Intel8254Timer(EventManager *em, const string &name) :
5012855Sgabeblack@google.com    EventManager(em), _name(name)
5112855Sgabeblack@google.com{
5212855Sgabeblack@google.com    counter[0] = new Counter(this, name + ".counter0", 0);
5312855Sgabeblack@google.com    counter[1] = new Counter(this, name + ".counter1", 1);
5412855Sgabeblack@google.com    counter[2] = new Counter(this, name + ".counter2", 2);
5512855Sgabeblack@google.com}
5612855Sgabeblack@google.com
5712855Sgabeblack@google.comvoid
5812855Sgabeblack@google.comIntel8254Timer::writeControl(const CtrlReg data)
5912855Sgabeblack@google.com{
6012855Sgabeblack@google.com    int sel = data.sel;
6112855Sgabeblack@google.com
6212855Sgabeblack@google.com    if (sel == ReadBackCommand)
6312855Sgabeblack@google.com       panic("PITimer Read-Back Command is not implemented.\n");
6412855Sgabeblack@google.com
6512855Sgabeblack@google.com    if (data.rw == LatchCommand)
6612855Sgabeblack@google.com        counter[sel]->latchCount();
6712855Sgabeblack@google.com    else {
6812855Sgabeblack@google.com        counter[sel]->setRW(data.rw);
6912855Sgabeblack@google.com        counter[sel]->setMode(data.mode);
7012855Sgabeblack@google.com        counter[sel]->setBCD(data.bcd);
7112855Sgabeblack@google.com    }
7212855Sgabeblack@google.com}
7312855Sgabeblack@google.com
7412855Sgabeblack@google.comvoid
7512855Sgabeblack@google.comIntel8254Timer::serialize(const string &base, CheckpointOut &cp) const
7612855Sgabeblack@google.com{
7712855Sgabeblack@google.com    // serialize the counters
7812855Sgabeblack@google.com    counter[0]->serialize(base + ".counter0", cp);
7912855Sgabeblack@google.com    counter[1]->serialize(base + ".counter1", cp);
8012855Sgabeblack@google.com    counter[2]->serialize(base + ".counter2", cp);
8112855Sgabeblack@google.com}
8212855Sgabeblack@google.com
8312855Sgabeblack@google.comvoid
8412855Sgabeblack@google.comIntel8254Timer::unserialize(const string &base, CheckpointIn &cp)
8512855Sgabeblack@google.com{
8612855Sgabeblack@google.com    // unserialze the counters
8712855Sgabeblack@google.com    counter[0]->unserialize(base + ".counter0", cp);
8812855Sgabeblack@google.com    counter[1]->unserialize(base + ".counter1", cp);
8912855Sgabeblack@google.com    counter[2]->unserialize(base + ".counter2", cp);
9012855Sgabeblack@google.com}
9112855Sgabeblack@google.com
9212855Sgabeblack@google.comvoid
9312855Sgabeblack@google.comIntel8254Timer::startup()
9412855Sgabeblack@google.com{
9512855Sgabeblack@google.com    counter[0]->startup();
9612855Sgabeblack@google.com    counter[1]->startup();
9712855Sgabeblack@google.com    counter[2]->startup();
9812855Sgabeblack@google.com}
9912855Sgabeblack@google.com
10012855Sgabeblack@google.comIntel8254Timer::Counter::Counter(Intel8254Timer *p,
10112855Sgabeblack@google.com        const string &name, unsigned int _num)
10212855Sgabeblack@google.com    : _name(name), num(_num), event(this), running(false),
10312855Sgabeblack@google.com      initial_count(0), latched_count(0), period(0), mode(0),
10412855Sgabeblack@google.com      output_high(false), latch_on(false), read_byte(LSB),
10512855Sgabeblack@google.com      write_byte(LSB), parent(p)
10612855Sgabeblack@google.com{
10712855Sgabeblack@google.com    offset = period * event.getInterval();
10812855Sgabeblack@google.com}
10912855Sgabeblack@google.com
11012855Sgabeblack@google.comvoid
11112855Sgabeblack@google.comIntel8254Timer::Counter::latchCount()
11212855Sgabeblack@google.com{
11312855Sgabeblack@google.com    // behave like a real latch
114    if (!latch_on) {
115        latch_on = true;
116        read_byte = LSB;
117        latched_count = currentCount();
118    }
119}
120
121int
122Intel8254Timer::Counter::currentCount()
123{
124    int clocks = event.clocksLeft();
125    if (clocks == -1) {
126        warn_once("Reading current count from inactive timer.\n");
127        return 0;
128    }
129    if (mode == RateGen || mode == SquareWave)
130        return clocks + 1;
131    else
132        return clocks;
133}
134
135uint8_t
136Intel8254Timer::Counter::read()
137{
138    if (latch_on) {
139        switch (read_byte) {
140          case LSB:
141            read_byte = MSB;
142            return (uint8_t)latched_count;
143            break;
144          case MSB:
145            read_byte = LSB;
146            latch_on = false;
147            return latched_count >> 8;
148            break;
149          default:
150            panic("Shouldn't be here");
151        }
152    } else {
153        uint16_t count = currentCount();
154        switch (read_byte) {
155          case LSB:
156            read_byte = MSB;
157            return (uint8_t)count;
158            break;
159          case MSB:
160            read_byte = LSB;
161            return count >> 8;
162            break;
163          default:
164            panic("Shouldn't be here");
165        }
166    }
167}
168
169void
170Intel8254Timer::Counter::write(const uint8_t data)
171{
172    switch (write_byte) {
173      case LSB:
174        initial_count = (initial_count & 0xFF00) | data;
175
176        if (event.scheduled())
177            parent->deschedule(event);
178        output_high = false;
179        write_byte = MSB;
180        break;
181
182      case MSB:
183        initial_count = (initial_count & 0x00FF) | (data << 8);
184        // In the RateGen or SquareWave modes, the timer wraps around and
185        // triggers on a value of 1, not 0.
186        if (mode == RateGen || mode == SquareWave)
187            period = initial_count - 1;
188        else
189            period = initial_count;
190
191        offset = period * event.getInterval();
192
193        if (running && (period > 0))
194            event.setTo(period);
195
196        write_byte = LSB;
197        break;
198    }
199}
200
201void
202Intel8254Timer::Counter::setRW(int rw_val)
203{
204    if (rw_val != TwoPhase)
205        panic("Only LSB/MSB read/write is implemented.\n");
206}
207
208void
209Intel8254Timer::Counter::setMode(int mode_val)
210{
211    if (mode_val != InitTc && mode_val != RateGen &&
212       mode_val != SquareWave)
213        panic("PIT mode %#x is not implemented: \n", mode_val);
214
215    mode = mode_val;
216}
217
218void
219Intel8254Timer::Counter::setBCD(int bcd_val)
220{
221    if (bcd_val)
222        panic("PITimer does not implement BCD counts.\n");
223}
224
225bool
226Intel8254Timer::Counter::outputHigh()
227{
228    return output_high;
229}
230
231void
232Intel8254Timer::Counter::serialize(const string &base, CheckpointOut &cp) const
233{
234    paramOut(cp, base + ".initial_count", initial_count);
235    paramOut(cp, base + ".latched_count", latched_count);
236    paramOut(cp, base + ".period", period);
237    paramOut(cp, base + ".mode", mode);
238    paramOut(cp, base + ".output_high", output_high);
239    paramOut(cp, base + ".latch_on", latch_on);
240    paramOut(cp, base + ".read_byte", read_byte);
241    paramOut(cp, base + ".write_byte", write_byte);
242
243    Tick event_tick_offset = 0;
244    if (event.scheduled())
245        event_tick_offset = event.when() - curTick();
246    paramOut(cp, base + ".event_tick_offset", event_tick_offset);
247}
248
249void
250Intel8254Timer::Counter::unserialize(const string &base, CheckpointIn &cp)
251{
252    paramIn(cp, base + ".initial_count", initial_count);
253    paramIn(cp, base + ".latched_count", latched_count);
254    paramIn(cp, base + ".period", period);
255    paramIn(cp, base + ".mode", mode);
256    paramIn(cp, base + ".output_high", output_high);
257    paramIn(cp, base + ".latch_on", latch_on);
258    paramIn(cp, base + ".read_byte", read_byte);
259    paramIn(cp, base + ".write_byte", write_byte);
260
261    Tick event_tick_offset = 0;
262    assert(!event.scheduled());
263    paramIn(cp, base + ".event_tick_offset", event_tick_offset);
264    offset = event_tick_offset;
265}
266
267void
268Intel8254Timer::Counter::startup()
269{
270    running = true;
271    if ((period > 0) && (offset > 0))
272    {
273        parent->schedule(event, curTick() + offset);
274    }
275}
276
277Intel8254Timer::Counter::CounterEvent::CounterEvent(Counter* c_ptr)
278{
279    interval = (Tick)(SimClock::Float::s / 1193180.0);
280    counter = c_ptr;
281}
282
283void
284Intel8254Timer::Counter::CounterEvent::process()
285{
286    switch (counter->mode) {
287      case InitTc:
288        counter->output_high = true;
289        break;
290      case RateGen:
291      case SquareWave:
292        setTo(counter->period);
293        break;
294      default:
295        panic("Unimplemented PITimer mode.\n");
296    }
297    counter->parent->counterInterrupt(counter->num);
298}
299
300void
301Intel8254Timer::Counter::CounterEvent::setTo(int clocks)
302{
303    if (clocks == 0)
304        panic("Timer can't be set to go off instantly.\n");
305    DPRINTF(Intel8254Timer, "Timer set to curTick() + %d\n",
306            clocks * interval);
307    counter->parent->schedule(this, curTick() + clocks * interval);
308}
309
310int
311Intel8254Timer::Counter::CounterEvent::clocksLeft()
312{
313    if (!scheduled())
314        return -1;
315    return (when() - curTick() + interval - 1) / interval;
316}
317
318const char *
319Intel8254Timer::Counter::CounterEvent::description() const
320{
321    return "Intel 8254 Interval timer";
322}
323
324Tick
325Intel8254Timer::Counter::CounterEvent::getInterval()
326{
327    return interval;
328}
329
330