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"
3712334Sgabeblack@google.com#include "base/logging.hh"
389356Snilay@cs.wisc.edu#include "sim/eventq_impl.hh"
395392Sgblack@eecs.umich.edu
405392Sgblack@eecs.umich.edu/** Real-Time Clock (MC146818) */
415606Snate@binkert.orgclass MC146818 : public EventManager
425392Sgblack@eecs.umich.edu{
435392Sgblack@eecs.umich.edu  protected:
445392Sgblack@eecs.umich.edu    virtual void handleEvent()
455392Sgblack@eecs.umich.edu    {
465392Sgblack@eecs.umich.edu        warn("No RTC event handler defined.\n");
475392Sgblack@eecs.umich.edu    }
485392Sgblack@eecs.umich.edu
495392Sgblack@eecs.umich.edu  private:
505392Sgblack@eecs.umich.edu    /** Event for RTC periodic interrupt */
515392Sgblack@eecs.umich.edu    struct RTCEvent : public Event
525392Sgblack@eecs.umich.edu    {
535392Sgblack@eecs.umich.edu        MC146818 * parent;
545392Sgblack@eecs.umich.edu        Tick interval;
5510631Scdirik@micron.com        Tick offset;
565392Sgblack@eecs.umich.edu
575392Sgblack@eecs.umich.edu        RTCEvent(MC146818 * _parent, Tick i);
585392Sgblack@eecs.umich.edu
595392Sgblack@eecs.umich.edu        /** Schedule the RTC periodic interrupt */
605392Sgblack@eecs.umich.edu        void scheduleIntr();
615392Sgblack@eecs.umich.edu
625392Sgblack@eecs.umich.edu        /** Event process to occur at interrupt*/
635392Sgblack@eecs.umich.edu        virtual void process();
645392Sgblack@eecs.umich.edu
655392Sgblack@eecs.umich.edu        /** Event description */
665392Sgblack@eecs.umich.edu        virtual const char *description() const;
675392Sgblack@eecs.umich.edu    };
685392Sgblack@eecs.umich.edu
696620Sgblack@eecs.umich.edu    /** Event for RTC periodic interrupt */
706620Sgblack@eecs.umich.edu    struct RTCTickEvent : public Event
716620Sgblack@eecs.umich.edu    {
726620Sgblack@eecs.umich.edu        MC146818 * parent;
7310631Scdirik@micron.com        Tick offset;
746620Sgblack@eecs.umich.edu
7510631Scdirik@micron.com        RTCTickEvent(MC146818 * _parent) :
7610631Scdirik@micron.com            parent(_parent), offset(SimClock::Int::s)
7710631Scdirik@micron.com        {}
786620Sgblack@eecs.umich.edu
796620Sgblack@eecs.umich.edu        /** Event process to occur at interrupt*/
806620Sgblack@eecs.umich.edu        void process();
816620Sgblack@eecs.umich.edu
826620Sgblack@eecs.umich.edu        /** Event description */
836620Sgblack@eecs.umich.edu        const char *description() const;
846620Sgblack@eecs.umich.edu    };
856620Sgblack@eecs.umich.edu
865392Sgblack@eecs.umich.edu  private:
875392Sgblack@eecs.umich.edu    std::string _name;
885392Sgblack@eecs.umich.edu    const std::string &name() const { return _name; }
895392Sgblack@eecs.umich.edu
905392Sgblack@eecs.umich.edu    /** RTC periodic interrupt event */
915392Sgblack@eecs.umich.edu    RTCEvent event;
925392Sgblack@eecs.umich.edu
936620Sgblack@eecs.umich.edu    /** RTC tick event */
946620Sgblack@eecs.umich.edu    RTCTickEvent tickEvent;
956620Sgblack@eecs.umich.edu
965392Sgblack@eecs.umich.edu    /** Data for real-time clock function */
975392Sgblack@eecs.umich.edu    union {
985392Sgblack@eecs.umich.edu        uint8_t clock_data[10];
995392Sgblack@eecs.umich.edu
1005392Sgblack@eecs.umich.edu        struct {
1015392Sgblack@eecs.umich.edu            uint8_t sec;
1025392Sgblack@eecs.umich.edu            uint8_t sec_alrm;
1035392Sgblack@eecs.umich.edu            uint8_t min;
1045392Sgblack@eecs.umich.edu            uint8_t min_alrm;
1055392Sgblack@eecs.umich.edu            uint8_t hour;
1065392Sgblack@eecs.umich.edu            uint8_t hour_alrm;
1075392Sgblack@eecs.umich.edu            uint8_t wday;
1085392Sgblack@eecs.umich.edu            uint8_t mday;
1095392Sgblack@eecs.umich.edu            uint8_t mon;
1105392Sgblack@eecs.umich.edu            uint8_t year;
1115392Sgblack@eecs.umich.edu        };
1125392Sgblack@eecs.umich.edu    };
1135392Sgblack@eecs.umich.edu
1146620Sgblack@eecs.umich.edu    struct tm curTime;
1156620Sgblack@eecs.umich.edu
1166620Sgblack@eecs.umich.edu    void setTime(const struct tm time);
1176620Sgblack@eecs.umich.edu
1189730Sandreas@sandberg.pp.se    BitUnion8(RtcRegA)
1199730Sandreas@sandberg.pp.se        Bitfield<7> uip;    /// 1 = date and time update in progress
1209730Sandreas@sandberg.pp.se        Bitfield<6, 4> dv;  /// Divider configuration
1219730Sandreas@sandberg.pp.se        /** Rate selection
1229730Sandreas@sandberg.pp.se            0 = Disabled
1239730Sandreas@sandberg.pp.se            For 32768 Hz time bases:
1249730Sandreas@sandberg.pp.se              Freq = 32768Hz / 2**(n-1) for n >= 3
1259730Sandreas@sandberg.pp.se              Freq = 256Hz if n = 1
1269730Sandreas@sandberg.pp.se              Freq = 128Hz if n = 2
1279730Sandreas@sandberg.pp.se            Othwerise:
1289730Sandreas@sandberg.pp.se              Freq = 32768Hz / 2**(n-1)
1299730Sandreas@sandberg.pp.se        */
1309730Sandreas@sandberg.pp.se        Bitfield<3, 0> rs;
1319730Sandreas@sandberg.pp.se    EndBitUnion(RtcRegA)
1329730Sandreas@sandberg.pp.se
1339730Sandreas@sandberg.pp.se    /// Is the DV field in regA set to disabled?
1349730Sandreas@sandberg.pp.se    static inline bool rega_dv_disabled(const RtcRegA &reg);
1359730Sandreas@sandberg.pp.se
1369730Sandreas@sandberg.pp.se    BitUnion8(RtcRegB)
1379730Sandreas@sandberg.pp.se        Bitfield<7> set;       /// stop clock updates
1389730Sandreas@sandberg.pp.se        Bitfield<6> pie;       /// 1 = enable periodic clock interrupt
1399730Sandreas@sandberg.pp.se        Bitfield<5> aie;       /// 1 = enable alarm interrupt
1409730Sandreas@sandberg.pp.se        Bitfield<4> uie;       /// 1 = enable update-ended interrupt
1419730Sandreas@sandberg.pp.se        Bitfield<3> sqwe;      /// 1 = output sqare wave at SQW pin
1429730Sandreas@sandberg.pp.se        Bitfield<2> dm;        /// 0 = BCD, 1 = Binary coded time
1439730Sandreas@sandberg.pp.se        Bitfield<1> format24h; /// 0 = 12 hours, 1 = 24 hours
1449730Sandreas@sandberg.pp.se        Bitfield<0> dse;       /// USA Daylight Savings Time enable
1459730Sandreas@sandberg.pp.se    EndBitUnion(RtcRegB)
1469730Sandreas@sandberg.pp.se
1475392Sgblack@eecs.umich.edu    /** RTC status register A */
1489730Sandreas@sandberg.pp.se    RtcRegA stat_regA;
1495392Sgblack@eecs.umich.edu
1505392Sgblack@eecs.umich.edu    /** RTC status register B */
1519730Sandreas@sandberg.pp.se    RtcRegB stat_regB;
1525392Sgblack@eecs.umich.edu
1535392Sgblack@eecs.umich.edu  public:
1545606Snate@binkert.org    MC146818(EventManager *em, const std::string &name, const struct tm time,
1555392Sgblack@eecs.umich.edu            bool bcd, Tick frequency);
1565606Snate@binkert.org    virtual ~MC146818();
1575392Sgblack@eecs.umich.edu
15810631Scdirik@micron.com    /** Start ticking */
15910631Scdirik@micron.com    virtual void startup();
16010631Scdirik@micron.com
1615392Sgblack@eecs.umich.edu    /** RTC write data */
1625392Sgblack@eecs.umich.edu    void writeData(const uint8_t addr, const uint8_t data);
1635392Sgblack@eecs.umich.edu
1645392Sgblack@eecs.umich.edu    /** RTC read data */
1655392Sgblack@eecs.umich.edu    uint8_t readData(const uint8_t addr);
1665392Sgblack@eecs.umich.edu
1676620Sgblack@eecs.umich.edu    void tickClock();
1686620Sgblack@eecs.umich.edu
1695392Sgblack@eecs.umich.edu    /**
1705392Sgblack@eecs.umich.edu      * Serialize this object to the given output stream.
1715392Sgblack@eecs.umich.edu      * @param base The base name of the counter object.
1725392Sgblack@eecs.umich.edu      * @param os The stream to serialize to.
1735392Sgblack@eecs.umich.edu      */
17410905Sandreas.sandberg@arm.com    void serialize(const std::string &base, CheckpointOut &cp) const;
1755392Sgblack@eecs.umich.edu
1765392Sgblack@eecs.umich.edu    /**
1775392Sgblack@eecs.umich.edu     * Reconstruct the state of this object from a checkpoint.
1785392Sgblack@eecs.umich.edu     * @param base The base name of the counter object.
1795392Sgblack@eecs.umich.edu     * @param cp The checkpoint use.
1805392Sgblack@eecs.umich.edu     * @param section The section name of this object
1815392Sgblack@eecs.umich.edu     */
18210905Sandreas.sandberg@arm.com    void unserialize(const std::string &base, CheckpointIn &cp);
1835392Sgblack@eecs.umich.edu};
1845392Sgblack@eecs.umich.edu
1855392Sgblack@eecs.umich.edu#endif // __DEV_MC146818_HH__
186