mc146818.cc revision 6620:ade9a088bb14
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/bitfield.hh"
39#include "base/time.hh"
40#include "base/trace.hh"
41#include "dev/mc146818.hh"
42#include "dev/rtcreg.h"
43
44using namespace std;
45
46static uint8_t
47bcdize(uint8_t val)
48{
49    uint8_t result;
50    result = val % 10;
51    result += (val / 10) << 4;
52    return result;
53}
54
55void
56MC146818::setTime(const struct tm time)
57{
58    curTime = time;
59    year = time.tm_year;
60    // Unix is 0-11 for month, data seet says start at 1
61    mon = time.tm_mon + 1;
62    mday = time.tm_mday;
63    hour = time.tm_hour;
64    min = time.tm_min;
65    sec = time.tm_sec;
66
67    // Datasheet says 1 is sunday
68    wday = time.tm_wday + 1;
69
70    if (!(stat_regB & RTCB_BIN)) {
71        // The datasheet says that the year field can be either BCD or
72        // years since 1900.  Linux seems to be happy with years since
73        // 1900.
74        year = bcdize(year % 100);
75        mon = bcdize(mon);
76        mday = bcdize(mday);
77        hour = bcdize(hour);
78        min = bcdize(min);
79        sec = bcdize(sec);
80    }
81}
82
83MC146818::MC146818(EventManager *em, const string &n, const struct tm time,
84                   bool bcd, Tick frequency)
85    : EventManager(em), _name(n), event(this, frequency), tickEvent(this)
86{
87    memset(clock_data, 0, sizeof(clock_data));
88    stat_regA = RTCA_32768HZ | RTCA_1024HZ;
89    stat_regB = RTCB_PRDC_IE | RTCB_24HR;
90    if (!bcd)
91        stat_regB |= RTCB_BIN;
92
93    setTime(time);
94    DPRINTFN("Real-time clock set to %s", asctime(&time));
95}
96
97MC146818::~MC146818()
98{
99}
100
101void
102MC146818::writeData(const uint8_t addr, const uint8_t data)
103{
104    if (addr < RTC_STAT_REGA)
105        clock_data[addr] = data;
106    else {
107        switch (addr) {
108          case RTC_STAT_REGA:
109            // The "update in progress" bit is read only.
110            if ((data & ~RTCA_UIP) != (RTCA_32768HZ | RTCA_1024HZ))
111                panic("Unimplemented RTC register A value write!\n");
112            replaceBits(stat_regA, data, 6, 0);
113            break;
114          case RTC_STAT_REGB:
115            if ((data & ~(RTCB_PRDC_IE | RTCB_SQWE)) != RTCB_24HR)
116                panic("Write to RTC reg B bits that are not implemented!\n");
117
118            if (data & RTCB_PRDC_IE) {
119                if (!event.scheduled())
120                    event.scheduleIntr();
121            } else {
122                if (event.scheduled())
123                    deschedule(event);
124            }
125            stat_regB = data;
126            break;
127          case RTC_STAT_REGC:
128          case RTC_STAT_REGD:
129            panic("RTC status registers C and D are not implemented.\n");
130            break;
131        }
132    }
133}
134
135uint8_t
136MC146818::readData(uint8_t addr)
137{
138    if (addr < RTC_STAT_REGA)
139        return clock_data[addr];
140    else {
141        switch (addr) {
142          case RTC_STAT_REGA:
143            // toggle UIP bit for linux
144            stat_regA ^= RTCA_UIP;
145            return stat_regA;
146            break;
147          case RTC_STAT_REGB:
148            return stat_regB;
149            break;
150          case RTC_STAT_REGC:
151          case RTC_STAT_REGD:
152            return 0x00;
153            break;
154          default:
155            panic("Shouldn't be here");
156        }
157    }
158}
159
160static time_t
161mkutctime(struct tm *time)
162{
163    time_t ret;
164    char *tz;
165
166    tz = getenv("TZ");
167    setenv("TZ", "", 1);
168    tzset();
169    ret = mktime(time);
170    if (tz)
171        setenv("TZ", tz, 1);
172    else
173        unsetenv("TZ");
174    tzset();
175    return ret;
176}
177
178void
179MC146818::tickClock()
180{
181    if (stat_regB & RTCB_NO_UPDT)
182        return;
183    time_t calTime = mkutctime(&curTime);
184    calTime++;
185    setTime(*gmtime(&calTime));
186}
187
188void
189MC146818::serialize(const string &base, ostream &os)
190{
191    arrayParamOut(os, base + ".clock_data", clock_data, sizeof(clock_data));
192    paramOut(os, base + ".stat_regA", stat_regA);
193    paramOut(os, base + ".stat_regB", stat_regB);
194}
195
196void
197MC146818::unserialize(const string &base, Checkpoint *cp,
198                      const string &section)
199{
200    arrayParamIn(cp, section, base + ".clock_data", clock_data,
201                 sizeof(clock_data));
202    paramIn(cp, section, base + ".stat_regA", stat_regA);
203    paramIn(cp, section, base + ".stat_regB", stat_regB);
204
205    // We're not unserializing the event here, but we need to
206    // rescehedule the event since curTick was moved forward by the
207    // checkpoint
208    reschedule(event, curTick + event.interval);
209}
210
211MC146818::RTCEvent::RTCEvent(MC146818 * _parent, Tick i)
212    : parent(_parent), interval(i)
213{
214    DPRINTF(MC146818, "RTC Event Initilizing\n");
215    parent->schedule(this, curTick + interval);
216}
217
218void
219MC146818::RTCEvent::scheduleIntr()
220{
221    parent->schedule(this, curTick + interval);
222}
223
224void
225MC146818::RTCEvent::process()
226{
227    DPRINTF(MC146818, "RTC Timer Interrupt\n");
228    parent->schedule(this, curTick + interval);
229    parent->handleEvent();
230}
231
232const char *
233MC146818::RTCEvent::description() const
234{
235    return "RTC interrupt";
236}
237
238void
239MC146818::RTCTickEvent::process()
240{
241    DPRINTF(MC146818, "RTC clock tick\n");
242    parent->schedule(this, curTick + Clock::Int::s);
243    parent->tickClock();
244}
245
246const char *
247MC146818::RTCTickEvent::description() const
248{
249    return "RTC clock tick";
250}
251