mc146818.cc revision 5392:c3a45fac35f8
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 * Authors: Ali Saidi
29 *          Andrew Schultz
30 *          Miguel Serrano
31 */
32
33#include <sys/time.h>
34#include <time.h>
35
36#include <string>
37
38#include "base/time.hh"
39#include "base/trace.hh"
40#include "dev/mc146818.hh"
41#include "dev/rtcreg.h"
42
43using namespace std;
44
45MC146818::MC146818(const string &n, const struct tm time,
46        bool bcd, Tick frequency)
47    : _name(n), event(this, frequency)
48{
49    memset(clock_data, 0, sizeof(clock_data));
50    stat_regA = RTCA_32768HZ | RTCA_1024HZ;
51    stat_regB = RTCB_PRDC_IE |RTCB_BIN | RTCB_24HR;
52
53    year = time.tm_year;
54
55    if (bcd) {
56        // The datasheet says that the year field can be either BCD or
57        // years since 1900.  Linux seems to be happy with years since
58        // 1900.
59        year = year % 100;
60        int tens = year / 10;
61        int ones = year % 10;
62        year = (tens << 4) + ones;
63    }
64
65    // Unix is 0-11 for month, data seet says start at 1
66    mon = time.tm_mon + 1;
67    mday = time.tm_mday;
68    hour = time.tm_hour;
69    min = time.tm_min;
70    sec = time.tm_sec;
71
72    // Datasheet says 1 is sunday
73    wday = time.tm_wday + 1;
74
75    DPRINTFN("Real-time clock set to %s", asctime(&time));
76}
77
78void
79MC146818::writeData(const uint8_t addr, const uint8_t data)
80{
81    if (addr < RTC_STAT_REGA)
82        clock_data[addr] = data;
83    else {
84        switch (addr) {
85          case RTC_STAT_REGA:
86            if (data != (RTCA_32768HZ | RTCA_1024HZ))
87                panic("Unimplemented RTC register A value write!\n");
88            stat_regA = data;
89            break;
90          case RTC_STAT_REGB:
91            if ((data & ~(RTCB_PRDC_IE | RTCB_SQWE)) != (RTCB_BIN | RTCB_24HR))
92                panic("Write to RTC reg B bits that are not implemented!\n");
93
94            if (data & RTCB_PRDC_IE) {
95                if (!event.scheduled())
96                    event.scheduleIntr();
97            } else {
98                if (event.scheduled())
99                    event.deschedule();
100            }
101            stat_regB = data;
102            break;
103          case RTC_STAT_REGC:
104          case RTC_STAT_REGD:
105            panic("RTC status registers C and D are not implemented.\n");
106            break;
107        }
108    }
109}
110
111uint8_t
112MC146818::readData(uint8_t addr)
113{
114    if (addr < RTC_STAT_REGA)
115        return clock_data[addr];
116    else {
117        switch (addr) {
118          case RTC_STAT_REGA:
119            // toggle UIP bit for linux
120            stat_regA ^= RTCA_UIP;
121            return stat_regA;
122            break;
123          case RTC_STAT_REGB:
124            return stat_regB;
125            break;
126          case RTC_STAT_REGC:
127          case RTC_STAT_REGD:
128            return 0x00;
129            break;
130          default:
131            panic("Shouldn't be here");
132        }
133    }
134}
135
136void
137MC146818::serialize(const string &base, ostream &os)
138{
139    arrayParamOut(os, base + ".clock_data", clock_data, sizeof(clock_data));
140    paramOut(os, base + ".stat_regA", stat_regA);
141    paramOut(os, base + ".stat_regB", stat_regB);
142}
143
144void
145MC146818::unserialize(const string &base, Checkpoint *cp,
146                      const string &section)
147{
148    arrayParamIn(cp, section, base + ".clock_data", clock_data,
149                 sizeof(clock_data));
150    paramIn(cp, section, base + ".stat_regA", stat_regA);
151    paramIn(cp, section, base + ".stat_regB", stat_regB);
152
153    // We're not unserializing the event here, but we need to
154    // rescehedule the event since curTick was moved forward by the
155    // checkpoint
156    event.reschedule(curTick + event.interval);
157}
158
159MC146818::RTCEvent::RTCEvent(MC146818 * _parent, Tick i)
160    : Event(&mainEventQueue), parent(_parent), interval(i)
161{
162    DPRINTF(MC146818, "RTC Event Initilizing\n");
163    schedule(curTick + interval);
164}
165
166void
167MC146818::RTCEvent::scheduleIntr()
168{
169  schedule(curTick + interval);
170}
171
172void
173MC146818::RTCEvent::process()
174{
175    DPRINTF(MC146818, "RTC Timer Interrupt\n");
176    schedule(curTick + interval);
177    parent->handleEvent();
178}
179
180const char *
181MC146818::RTCEvent::description() const
182{
183    return "RTC interrupt";
184}
185