tsunami_io.hh revision 2648
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
29/** @file
30 * Tsunami I/O Space mapping including RTC/timer interrupts
31 */
32
33#ifndef __DEV_TSUNAMI_IO_HH__
34#define __DEV_TSUNAMI_IO_HH__
35
36#include "dev/io_device.hh"
37#include "base/range.hh"
38#include "dev/tsunami.hh"
39#include "sim/eventq.hh"
40
41/**
42 * Tsunami I/O device is a catch all for all the south bridge stuff we care
43 * to implement.
44 */
45class TsunamiIO : public BasicPioDevice
46{
47  private:
48    struct tm tm;
49
50  protected:
51    /** Real-Time Clock (MC146818) */
52    class RTC
53    {
54      private:
55        /** Event for RTC periodic interrupt */
56        struct RTCEvent : public Event
57        {
58            /** A pointer back to tsunami to create interrupt the processor. */
59            Tsunami* tsunami;
60            Tick interval;
61
62            RTCEvent(Tsunami* t, Tick i);
63
64            /** Schedule the RTC periodic interrupt */
65            void scheduleIntr();
66
67            /** Event process to occur at interrupt*/
68            virtual void process();
69
70            /** Event description */
71            virtual const char *description();
72        };
73
74      private:
75        std::string _name;
76        const std::string &name() const { return _name; }
77
78        /** RTC periodic interrupt event */
79        RTCEvent event;
80
81        /** Current RTC register address/index */
82        int addr;
83
84        /** Data for real-time clock function */
85        union {
86            uint8_t clock_data[10];
87
88            struct {
89                uint8_t sec;
90                uint8_t sec_alrm;
91                uint8_t min;
92                uint8_t min_alrm;
93                uint8_t hour;
94                uint8_t hour_alrm;
95                uint8_t wday;
96                uint8_t mday;
97                uint8_t mon;
98                uint8_t year;
99            };
100        };
101
102        /** RTC status register A */
103        uint8_t stat_regA;
104
105        /** RTC status register B */
106        uint8_t stat_regB;
107
108      public:
109        RTC(const std::string &name, Tsunami* t, Tick i);
110
111        /** Set the initial RTC time/date */
112        void set_time(time_t t);
113
114        /** RTC address port: write address of RTC RAM data to access */
115        void writeAddr(const uint8_t data);
116
117        /** RTC write data */
118        void writeData(const uint8_t data);
119
120        /** RTC read data */
121        uint8_t readData();
122
123        /**
124          * Serialize this object to the given output stream.
125          * @param os The stream to serialize to.
126          */
127        void serialize(const std::string &base, std::ostream &os);
128
129        /**
130         * Reconstruct the state of this object from a checkpoint.
131         * @param cp The checkpoint use.
132         * @param section The section name of this object
133         */
134        void unserialize(const std::string &base, Checkpoint *cp,
135                         const std::string &section);
136    };
137
138    /** Programmable Interval Timer (Intel 8254) */
139    class PITimer
140    {
141        /** Counter element for PIT */
142        class Counter
143        {
144            /** Event for counter interrupt */
145            class CounterEvent : public Event
146            {
147              private:
148                /** Pointer back to Counter */
149                Counter* counter;
150                Tick interval;
151
152              public:
153                CounterEvent(Counter*);
154
155                /** Event process */
156                virtual void process();
157
158                /** Event description */
159                virtual const char *description();
160
161                friend class Counter;
162            };
163
164          private:
165            std::string _name;
166            const std::string &name() const { return _name; }
167
168            CounterEvent event;
169
170            /** Current count value */
171            uint16_t count;
172
173            /** Latched count */
174            uint16_t latched_count;
175
176            /** Interrupt period */
177            uint16_t period;
178
179            /** Current mode of operation */
180            uint8_t mode;
181
182            /** Output goes high when the counter reaches zero */
183            bool output_high;
184
185            /** State of the count latch */
186            bool latch_on;
187
188            /** Set of values for read_byte and write_byte */
189            enum {LSB, MSB};
190
191            /** Determine which byte of a 16-bit count value to read/write */
192            uint8_t read_byte, write_byte;
193
194          public:
195            Counter(const std::string &name);
196
197            /** Latch the current count (if one is not already latched) */
198            void latchCount();
199
200            /** Set the read/write mode */
201            void setRW(int rw_val);
202
203            /** Set operational mode */
204            void setMode(int mode_val);
205
206            /** Set count encoding */
207            void setBCD(int bcd_val);
208
209            /** Read a count byte */
210            uint8_t read();
211
212            /** Write a count byte */
213            void write(const uint8_t data);
214
215            /** Is the output high? */
216            bool outputHigh();
217
218            /**
219             * Serialize this object to the given output stream.
220             * @param os The stream to serialize to.
221             */
222            void serialize(const std::string &base, std::ostream &os);
223
224            /**
225             * Reconstruct the state of this object from a checkpoint.
226             * @param cp The checkpoint use.
227             * @param section The section name of this object
228             */
229            void unserialize(const std::string &base, Checkpoint *cp,
230                             const std::string &section);
231        };
232
233      private:
234        std::string _name;
235        const std::string &name() const { return _name; }
236
237        /** PIT has three seperate counters */
238        Counter *counter[3];
239
240      public:
241        /** Public way to access individual counters (avoid array accesses) */
242        Counter counter0;
243        Counter counter1;
244        Counter counter2;
245
246        PITimer(const std::string &name);
247
248        /** Write control word */
249        void writeControl(const uint8_t data);
250
251        /**
252         * Serialize this object to the given output stream.
253         * @param os The stream to serialize to.
254         */
255        void serialize(const std::string &base, std::ostream &os);
256
257        /**
258         * Reconstruct the state of this object from a checkpoint.
259         * @param cp The checkpoint use.
260         * @param section The section name of this object
261         */
262        void unserialize(const std::string &base, Checkpoint *cp,
263                         const std::string &section);
264    };
265
266    /** Mask of the PIC1 */
267    uint8_t mask1;
268
269    /** Mask of the PIC2 */
270    uint8_t mask2;
271
272    /** Mode of PIC1. Not used for anything */
273    uint8_t mode1;
274
275    /** Mode of PIC2. Not used for anything */
276    uint8_t mode2;
277
278    /** Raw PIC interrupt register before masking */
279    uint8_t picr; //Raw PIC interrput register
280
281    /** Is the pic interrupting right now or not. */
282    bool picInterrupting;
283
284    /** A pointer to the Tsunami device which be belong to */
285    Tsunami *tsunami;
286
287    /** Intel 8253 Periodic Interval Timer */
288    PITimer pitimer;
289
290    RTC rtc;
291
292    /** The interval is set via two writes to the PIT.
293     * This variable contains a flag as to how many writes have happened, and
294     * the time so far.
295     */
296    uint16_t timerData;
297
298  public:
299    /**
300     * Return the freqency of the RTC
301     * @return interrupt rate of the RTC
302     */
303    Tick frequency() const;
304
305    struct Params : public BasicPioDevice::Params
306    {
307        Tick frequency;
308        Tsunami *tsunami;
309        time_t init_time;
310    };
311  protected:
312    const Params *params() const { return (const Params*)_params; }
313
314  public:
315    /**
316     * Initialize all the data for devices supported by Tsunami I/O.
317     * @param p pointer to Params struct
318     */
319    TsunamiIO(Params *p);
320
321    virtual Tick read(Packet *pkt);
322    virtual Tick write(Packet *pkt);
323
324    /**
325     * Post an PIC interrupt to the CPU via the CChip
326     * @param bitvector interrupt to post.
327     */
328    void postPIC(uint8_t bitvector);
329
330    /**
331     * Clear a posted interrupt
332     * @param bitvector interrupt to clear
333     */
334    void clearPIC(uint8_t bitvector);
335
336    /**
337     * Serialize this object to the given output stream.
338     * @param os The stream to serialize to.
339     */
340    virtual void serialize(std::ostream &os);
341
342    /**
343     * Reconstruct the state of this object from a checkpoint.
344     * @param cp The checkpoint use.
345     * @param section The section name of this object
346     */
347    virtual void unserialize(Checkpoint *cp, const std::string &section);
348
349};
350
351#endif // __DEV_TSUNAMI_IO_HH__
352