mc146818.hh revision 10905
15392Sgblack@eecs.umich.edu/*
25392Sgblack@eecs.umich.edu * Copyright (c) 2004-2005 The Regents of The University of Michigan
35392Sgblack@eecs.umich.edu * All rights reserved.
45392Sgblack@eecs.umich.edu *
55392Sgblack@eecs.umich.edu * Redistribution and use in source and binary forms, with or without
65392Sgblack@eecs.umich.edu * modification, are permitted provided that the following conditions are
75392Sgblack@eecs.umich.edu * met: redistributions of source code must retain the above copyright
85392Sgblack@eecs.umich.edu * notice, this list of conditions and the following disclaimer;
95392Sgblack@eecs.umich.edu * redistributions in binary form must reproduce the above copyright
105392Sgblack@eecs.umich.edu * notice, this list of conditions and the following disclaimer in the
115392Sgblack@eecs.umich.edu * documentation and/or other materials provided with the distribution;
125392Sgblack@eecs.umich.edu * neither the name of the copyright holders nor the names of its
135392Sgblack@eecs.umich.edu * contributors may be used to endorse or promote products derived from
145392Sgblack@eecs.umich.edu * this software without specific prior written permission.
155392Sgblack@eecs.umich.edu *
165392Sgblack@eecs.umich.edu * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
175392Sgblack@eecs.umich.edu * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
185392Sgblack@eecs.umich.edu * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
195392Sgblack@eecs.umich.edu * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
205392Sgblack@eecs.umich.edu * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
215392Sgblack@eecs.umich.edu * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
225392Sgblack@eecs.umich.edu * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
235392Sgblack@eecs.umich.edu * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
245392Sgblack@eecs.umich.edu * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
255392Sgblack@eecs.umich.edu * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
265392Sgblack@eecs.umich.edu * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
275392Sgblack@eecs.umich.edu *
285392Sgblack@eecs.umich.edu * Authors: Ali Saidi
295392Sgblack@eecs.umich.edu *          Andrew Schultz
305392Sgblack@eecs.umich.edu *          Miguel Serrano
315392Sgblack@eecs.umich.edu */
325392Sgblack@eecs.umich.edu
335392Sgblack@eecs.umich.edu#ifndef __DEV_MC146818_HH__
345392Sgblack@eecs.umich.edu#define __DEV_MC146818_HH__
355392Sgblack@eecs.umich.edu
369730Sandreas@sandberg.pp.se#include "base/bitunion.hh"
379356Snilay@cs.wisc.edu#include "sim/eventq_impl.hh"
385392Sgblack@eecs.umich.edu
395392Sgblack@eecs.umich.edu/** Real-Time Clock (MC146818) */
405606Snate@binkert.orgclass MC146818 : public EventManager
415392Sgblack@eecs.umich.edu{
425392Sgblack@eecs.umich.edu  protected:
435392Sgblack@eecs.umich.edu    virtual void handleEvent()
445392Sgblack@eecs.umich.edu    {
455392Sgblack@eecs.umich.edu        warn("No RTC event handler defined.\n");
465392Sgblack@eecs.umich.edu    }
475392Sgblack@eecs.umich.edu
485392Sgblack@eecs.umich.edu  private:
495392Sgblack@eecs.umich.edu    /** Event for RTC periodic interrupt */
505392Sgblack@eecs.umich.edu    struct RTCEvent : public Event
515392Sgblack@eecs.umich.edu    {
525392Sgblack@eecs.umich.edu        MC146818 * parent;
535392Sgblack@eecs.umich.edu        Tick interval;
5410631Scdirik@micron.com        Tick offset;
555392Sgblack@eecs.umich.edu
565392Sgblack@eecs.umich.edu        RTCEvent(MC146818 * _parent, Tick i);
575392Sgblack@eecs.umich.edu
585392Sgblack@eecs.umich.edu        /** Schedule the RTC periodic interrupt */
595392Sgblack@eecs.umich.edu        void scheduleIntr();
605392Sgblack@eecs.umich.edu
615392Sgblack@eecs.umich.edu        /** Event process to occur at interrupt*/
625392Sgblack@eecs.umich.edu        virtual void process();
635392Sgblack@eecs.umich.edu
645392Sgblack@eecs.umich.edu        /** Event description */
655392Sgblack@eecs.umich.edu        virtual const char *description() const;
665392Sgblack@eecs.umich.edu    };
675392Sgblack@eecs.umich.edu
686620Sgblack@eecs.umich.edu    /** Event for RTC periodic interrupt */
696620Sgblack@eecs.umich.edu    struct RTCTickEvent : public Event
706620Sgblack@eecs.umich.edu    {
716620Sgblack@eecs.umich.edu        MC146818 * parent;
7210631Scdirik@micron.com        Tick offset;
736620Sgblack@eecs.umich.edu
7410631Scdirik@micron.com        RTCTickEvent(MC146818 * _parent) :
7510631Scdirik@micron.com            parent(_parent), offset(SimClock::Int::s)
7610631Scdirik@micron.com        {}
776620Sgblack@eecs.umich.edu
786620Sgblack@eecs.umich.edu        /** Event process to occur at interrupt*/
796620Sgblack@eecs.umich.edu        void process();
806620Sgblack@eecs.umich.edu
816620Sgblack@eecs.umich.edu        /** Event description */
826620Sgblack@eecs.umich.edu        const char *description() const;
836620Sgblack@eecs.umich.edu    };
846620Sgblack@eecs.umich.edu
855392Sgblack@eecs.umich.edu  private:
865392Sgblack@eecs.umich.edu    std::string _name;
875392Sgblack@eecs.umich.edu    const std::string &name() const { return _name; }
885392Sgblack@eecs.umich.edu
895392Sgblack@eecs.umich.edu    /** RTC periodic interrupt event */
905392Sgblack@eecs.umich.edu    RTCEvent event;
915392Sgblack@eecs.umich.edu
926620Sgblack@eecs.umich.edu    /** RTC tick event */
936620Sgblack@eecs.umich.edu    RTCTickEvent tickEvent;
946620Sgblack@eecs.umich.edu
955392Sgblack@eecs.umich.edu    /** Data for real-time clock function */
965392Sgblack@eecs.umich.edu    union {
975392Sgblack@eecs.umich.edu        uint8_t clock_data[10];
985392Sgblack@eecs.umich.edu
995392Sgblack@eecs.umich.edu        struct {
1005392Sgblack@eecs.umich.edu            uint8_t sec;
1015392Sgblack@eecs.umich.edu            uint8_t sec_alrm;
1025392Sgblack@eecs.umich.edu            uint8_t min;
1035392Sgblack@eecs.umich.edu            uint8_t min_alrm;
1045392Sgblack@eecs.umich.edu            uint8_t hour;
1055392Sgblack@eecs.umich.edu            uint8_t hour_alrm;
1065392Sgblack@eecs.umich.edu            uint8_t wday;
1075392Sgblack@eecs.umich.edu            uint8_t mday;
1085392Sgblack@eecs.umich.edu            uint8_t mon;
1095392Sgblack@eecs.umich.edu            uint8_t year;
1105392Sgblack@eecs.umich.edu        };
1115392Sgblack@eecs.umich.edu    };
1125392Sgblack@eecs.umich.edu
1136620Sgblack@eecs.umich.edu    struct tm curTime;
1146620Sgblack@eecs.umich.edu
1156620Sgblack@eecs.umich.edu    void setTime(const struct tm time);
1166620Sgblack@eecs.umich.edu
1179730Sandreas@sandberg.pp.se    BitUnion8(RtcRegA)
1189730Sandreas@sandberg.pp.se        Bitfield<7> uip;    /// 1 = date and time update in progress
1199730Sandreas@sandberg.pp.se        Bitfield<6, 4> dv;  /// Divider configuration
1209730Sandreas@sandberg.pp.se        /** Rate selection
1219730Sandreas@sandberg.pp.se            0 = Disabled
1229730Sandreas@sandberg.pp.se            For 32768 Hz time bases:
1239730Sandreas@sandberg.pp.se              Freq = 32768Hz / 2**(n-1) for n >= 3
1249730Sandreas@sandberg.pp.se              Freq = 256Hz if n = 1
1259730Sandreas@sandberg.pp.se              Freq = 128Hz if n = 2
1269730Sandreas@sandberg.pp.se            Othwerise:
1279730Sandreas@sandberg.pp.se              Freq = 32768Hz / 2**(n-1)
1289730Sandreas@sandberg.pp.se        */
1299730Sandreas@sandberg.pp.se        Bitfield<3, 0> rs;
1309730Sandreas@sandberg.pp.se    EndBitUnion(RtcRegA)
1319730Sandreas@sandberg.pp.se
1329730Sandreas@sandberg.pp.se    /// Is the DV field in regA set to disabled?
1339730Sandreas@sandberg.pp.se    static inline bool rega_dv_disabled(const RtcRegA &reg);
1349730Sandreas@sandberg.pp.se
1359730Sandreas@sandberg.pp.se    BitUnion8(RtcRegB)
1369730Sandreas@sandberg.pp.se        Bitfield<7> set;       /// stop clock updates
1379730Sandreas@sandberg.pp.se        Bitfield<6> pie;       /// 1 = enable periodic clock interrupt
1389730Sandreas@sandberg.pp.se        Bitfield<5> aie;       /// 1 = enable alarm interrupt
1399730Sandreas@sandberg.pp.se        Bitfield<4> uie;       /// 1 = enable update-ended interrupt
1409730Sandreas@sandberg.pp.se        Bitfield<3> sqwe;      /// 1 = output sqare wave at SQW pin
1419730Sandreas@sandberg.pp.se        Bitfield<2> dm;        /// 0 = BCD, 1 = Binary coded time
1429730Sandreas@sandberg.pp.se        Bitfield<1> format24h; /// 0 = 12 hours, 1 = 24 hours
1439730Sandreas@sandberg.pp.se        Bitfield<0> dse;       /// USA Daylight Savings Time enable
1449730Sandreas@sandberg.pp.se    EndBitUnion(RtcRegB)
1459730Sandreas@sandberg.pp.se
1465392Sgblack@eecs.umich.edu    /** RTC status register A */
1479730Sandreas@sandberg.pp.se    RtcRegA stat_regA;
1485392Sgblack@eecs.umich.edu
1495392Sgblack@eecs.umich.edu    /** RTC status register B */
1509730Sandreas@sandberg.pp.se    RtcRegB stat_regB;
1515392Sgblack@eecs.umich.edu
1525392Sgblack@eecs.umich.edu  public:
1535606Snate@binkert.org    MC146818(EventManager *em, const std::string &name, const struct tm time,
1545392Sgblack@eecs.umich.edu            bool bcd, Tick frequency);
1555606Snate@binkert.org    virtual ~MC146818();
1565392Sgblack@eecs.umich.edu
15710631Scdirik@micron.com    /** Start ticking */
15810631Scdirik@micron.com    virtual void startup();
15910631Scdirik@micron.com
1605392Sgblack@eecs.umich.edu    /** RTC write data */
1615392Sgblack@eecs.umich.edu    void writeData(const uint8_t addr, const uint8_t data);
1625392Sgblack@eecs.umich.edu
1635392Sgblack@eecs.umich.edu    /** RTC read data */
1645392Sgblack@eecs.umich.edu    uint8_t readData(const uint8_t addr);
1655392Sgblack@eecs.umich.edu
1666620Sgblack@eecs.umich.edu    void tickClock();
1676620Sgblack@eecs.umich.edu
1685392Sgblack@eecs.umich.edu    /**
1695392Sgblack@eecs.umich.edu      * Serialize this object to the given output stream.
1705392Sgblack@eecs.umich.edu      * @param base The base name of the counter object.
1715392Sgblack@eecs.umich.edu      * @param os The stream to serialize to.
1725392Sgblack@eecs.umich.edu      */
17310905Sandreas.sandberg@arm.com    void serialize(const std::string &base, CheckpointOut &cp) const;
1745392Sgblack@eecs.umich.edu
1755392Sgblack@eecs.umich.edu    /**
1765392Sgblack@eecs.umich.edu     * Reconstruct the state of this object from a checkpoint.
1775392Sgblack@eecs.umich.edu     * @param base The base name of the counter object.
1785392Sgblack@eecs.umich.edu     * @param cp The checkpoint use.
1795392Sgblack@eecs.umich.edu     * @param section The section name of this object
1805392Sgblack@eecs.umich.edu     */
18110905Sandreas.sandberg@arm.com    void unserialize(const std::string &base, CheckpointIn &cp);
1825392Sgblack@eecs.umich.edu};
1835392Sgblack@eecs.umich.edu
1845392Sgblack@eecs.umich.edu#endif // __DEV_MC146818_HH__
185