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