Statistics
| Branch: | Revision:

root / hw / mc146818rtc.c @ b41a2cd1

History | View | Annotate | Download (5.6 kB)

1
/*
2
 * QEMU MC146818 RTC emulation
3
 * 
4
 * Copyright (c) 2003-2004 Fabrice Bellard
5
 * 
6
 * Permission is hereby granted, free of charge, to any person obtaining a copy
7
 * of this software and associated documentation files (the "Software"), to deal
8
 * in the Software without restriction, including without limitation the rights
9
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
 * copies of the Software, and to permit persons to whom the Software is
11
 * furnished to do so, subject to the following conditions:
12
 *
13
 * The above copyright notice and this permission notice shall be included in
14
 * all copies or substantial portions of the Software.
15
 *
16
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22
 * THE SOFTWARE.
23
 */
24
#include <stdlib.h>
25
#include <stdio.h>
26
#include <stdarg.h>
27
#include <string.h>
28
#include <getopt.h>
29
#include <inttypes.h>
30
#include <unistd.h>
31
#include <sys/mman.h>
32
#include <fcntl.h>
33
#include <signal.h>
34
#include <time.h>
35
#include <sys/time.h>
36
#include <malloc.h>
37
#include <termios.h>
38
#include <sys/poll.h>
39
#include <errno.h>
40
#include <sys/wait.h>
41
#include <netinet/in.h>
42

    
43
#include "cpu.h"
44
#include "vl.h"
45

    
46
//#define DEBUG_CMOS
47

    
48
#define RTC_SECONDS             0
49
#define RTC_SECONDS_ALARM       1
50
#define RTC_MINUTES             2
51
#define RTC_MINUTES_ALARM       3
52
#define RTC_HOURS               4
53
#define RTC_HOURS_ALARM         5
54
#define RTC_ALARM_DONT_CARE    0xC0
55

    
56
#define RTC_DAY_OF_WEEK         6
57
#define RTC_DAY_OF_MONTH        7
58
#define RTC_MONTH               8
59
#define RTC_YEAR                9
60

    
61
#define RTC_REG_A               10
62
#define RTC_REG_B               11
63
#define RTC_REG_C               12
64
#define RTC_REG_D               13
65

    
66
/* PC cmos mappings */
67
#define REG_IBM_CENTURY_BYTE        0x32
68
#define REG_IBM_PS2_CENTURY_BYTE    0x37
69

    
70
RTCState rtc_state;
71

    
72
static void cmos_ioport_write(void *opaque, uint32_t addr, uint32_t data)
73
{
74
    RTCState *s = opaque;
75

    
76
    if ((addr & 1) == 0) {
77
        s->cmos_index = data & 0x7f;
78
    } else {
79
#ifdef DEBUG_CMOS
80
        printf("cmos: write index=0x%02x val=0x%02x\n",
81
               s->cmos_index, data);
82
#endif        
83
        switch(addr) {
84
        case RTC_SECONDS_ALARM:
85
        case RTC_MINUTES_ALARM:
86
        case RTC_HOURS_ALARM:
87
            /* XXX: not supported */
88
            s->cmos_data[s->cmos_index] = data;
89
            break;
90
        case RTC_SECONDS:
91
        case RTC_MINUTES:
92
        case RTC_HOURS:
93
        case RTC_DAY_OF_WEEK:
94
        case RTC_DAY_OF_MONTH:
95
        case RTC_MONTH:
96
        case RTC_YEAR:
97
            s->cmos_data[s->cmos_index] = data;
98
            break;
99
        case RTC_REG_A:
100
        case RTC_REG_B:
101
            s->cmos_data[s->cmos_index] = data;
102
            break;
103
        case RTC_REG_C:
104
        case RTC_REG_D:
105
            /* cannot write to them */
106
            break;
107
        default:
108
            s->cmos_data[s->cmos_index] = data;
109
            break;
110
        }
111
    }
112
}
113

    
114
static inline int to_bcd(int a)
115
{
116
    return ((a / 10) << 4) | (a % 10);
117
}
118

    
119
static void cmos_update_time(RTCState *s)
120
{
121
    struct tm *tm;
122
    time_t ti;
123

    
124
    ti = time(NULL);
125
    tm = gmtime(&ti);
126
    s->cmos_data[RTC_SECONDS] = to_bcd(tm->tm_sec);
127
    s->cmos_data[RTC_MINUTES] = to_bcd(tm->tm_min);
128
    s->cmos_data[RTC_HOURS] = to_bcd(tm->tm_hour);
129
    s->cmos_data[RTC_DAY_OF_WEEK] = to_bcd(tm->tm_wday);
130
    s->cmos_data[RTC_DAY_OF_MONTH] = to_bcd(tm->tm_mday);
131
    s->cmos_data[RTC_MONTH] = to_bcd(tm->tm_mon + 1);
132
    s->cmos_data[RTC_YEAR] = to_bcd(tm->tm_year % 100);
133
    s->cmos_data[REG_IBM_CENTURY_BYTE] = to_bcd((tm->tm_year / 100) + 19);
134
    s->cmos_data[REG_IBM_PS2_CENTURY_BYTE] = s->cmos_data[REG_IBM_CENTURY_BYTE];
135
}
136

    
137
static uint32_t cmos_ioport_read(void *opaque, uint32_t addr)
138
{
139
    RTCState *s = opaque;
140
    int ret;
141
    if ((addr & 1) == 0) {
142
        return 0xff;
143
    } else {
144
        switch(s->cmos_index) {
145
        case RTC_SECONDS:
146
        case RTC_MINUTES:
147
        case RTC_HOURS:
148
        case RTC_DAY_OF_WEEK:
149
        case RTC_DAY_OF_MONTH:
150
        case RTC_MONTH:
151
        case RTC_YEAR:
152
        case REG_IBM_CENTURY_BYTE:
153
        case REG_IBM_PS2_CENTURY_BYTE:
154
            cmos_update_time(s);
155
            ret = s->cmos_data[s->cmos_index];
156
            break;
157
        case RTC_REG_A:
158
            ret = s->cmos_data[s->cmos_index];
159
            /* toggle update-in-progress bit for Linux (same hack as
160
               plex86) */
161
            s->cmos_data[RTC_REG_A] ^= 0x80; 
162
            break;
163
        case RTC_REG_C:
164
            ret = s->cmos_data[s->cmos_index];
165
            pic_set_irq(s->irq, 0);
166
            s->cmos_data[RTC_REG_C] = 0x00; 
167
            break;
168
        default:
169
            ret = s->cmos_data[s->cmos_index];
170
            break;
171
        }
172
#ifdef DEBUG_CMOS
173
        printf("cmos: read index=0x%02x val=0x%02x\n",
174
               s->cmos_index, ret);
175
#endif
176
        return ret;
177
    }
178
}
179

    
180
void rtc_timer(void)
181
{
182
    RTCState *s = &rtc_state;
183
    if (s->cmos_data[RTC_REG_B] & 0x50) {
184
        pic_set_irq(s->irq, 1);
185
    }
186
}
187

    
188
void rtc_init(int base, int irq)
189
{
190
    RTCState *s = &rtc_state;
191

    
192
    cmos_update_time(s);
193

    
194
    s->irq = irq;
195
    s->cmos_data[RTC_REG_A] = 0x26;
196
    s->cmos_data[RTC_REG_B] = 0x02;
197
    s->cmos_data[RTC_REG_C] = 0x00;
198
    s->cmos_data[RTC_REG_D] = 0x80;
199

    
200
    register_ioport_write(base, 2, 1, cmos_ioport_write, s);
201
    register_ioport_read(base, 2, 1, cmos_ioport_read, s);
202
}
203