Statistics
| Branch: | Revision:

root / hw / m48t59.c @ 5105ed3b

History | View | Annotate | Download (19.6 kB)

1 a541f297 bellard
/*
2 819385c5 bellard
 * QEMU M48T59 and M48T08 NVRAM emulation for PPC PREP and Sparc platforms
3 5fafdf24 ths
 *
4 3ccacc4a blueswir1
 * Copyright (c) 2003-2005, 2007 Jocelyn Mayer
5 5fafdf24 ths
 *
6 a541f297 bellard
 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 a541f297 bellard
 * of this software and associated documentation files (the "Software"), to deal
8 a541f297 bellard
 * in the Software without restriction, including without limitation the rights
9 a541f297 bellard
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 a541f297 bellard
 * copies of the Software, and to permit persons to whom the Software is
11 a541f297 bellard
 * furnished to do so, subject to the following conditions:
12 a541f297 bellard
 *
13 a541f297 bellard
 * The above copyright notice and this permission notice shall be included in
14 a541f297 bellard
 * all copies or substantial portions of the Software.
15 a541f297 bellard
 *
16 a541f297 bellard
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 a541f297 bellard
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 a541f297 bellard
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 a541f297 bellard
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 a541f297 bellard
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 a541f297 bellard
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 a541f297 bellard
 * THE SOFTWARE.
23 a541f297 bellard
 */
24 87ecb68b pbrook
#include "hw.h"
25 87ecb68b pbrook
#include "nvram.h"
26 87ecb68b pbrook
#include "qemu-timer.h"
27 87ecb68b pbrook
#include "sysemu.h"
28 d27cf0ae Blue Swirl
#include "sysbus.h"
29 f80237d4 Blue Swirl
#include "isa.h"
30 a541f297 bellard
31 13ab5daa bellard
//#define DEBUG_NVRAM
32 a541f297 bellard
33 13ab5daa bellard
#if defined(DEBUG_NVRAM)
34 001faf32 Blue Swirl
#define NVRAM_PRINTF(fmt, ...) do { printf(fmt , ## __VA_ARGS__); } while (0)
35 a541f297 bellard
#else
36 001faf32 Blue Swirl
#define NVRAM_PRINTF(fmt, ...) do { } while (0)
37 a541f297 bellard
#endif
38 a541f297 bellard
39 819385c5 bellard
/*
40 4aed2c33 blueswir1
 * The M48T02, M48T08 and M48T59 chips are very similar. The newer '59 has
41 819385c5 bellard
 * alarm and a watchdog timer and related control registers. In the
42 819385c5 bellard
 * PPC platform there is also a nvram lock function.
43 819385c5 bellard
 */
44 930f3fe1 Blue Swirl
45 930f3fe1 Blue Swirl
/*
46 930f3fe1 Blue Swirl
 * Chipset docs:
47 930f3fe1 Blue Swirl
 * http://www.st.com/stonline/products/literature/ds/2410/m48t02.pdf
48 930f3fe1 Blue Swirl
 * http://www.st.com/stonline/products/literature/ds/2411/m48t08.pdf
49 930f3fe1 Blue Swirl
 * http://www.st.com/stonline/products/literature/od/7001/m48t59y.pdf
50 930f3fe1 Blue Swirl
 */
51 930f3fe1 Blue Swirl
52 43a34704 Blue Swirl
struct M48t59State {
53 a541f297 bellard
    /* Hardware parameters */
54 d537cf6c pbrook
    qemu_irq IRQ;
55 a541f297 bellard
    uint32_t io_base;
56 ee6847d1 Gerd Hoffmann
    uint32_t size;
57 a541f297 bellard
    /* RTC management */
58 a541f297 bellard
    time_t   time_offset;
59 a541f297 bellard
    time_t   stop_time;
60 a541f297 bellard
    /* Alarm & watchdog */
61 f6503059 balrog
    struct tm alarm;
62 a541f297 bellard
    struct QEMUTimer *alrm_timer;
63 a541f297 bellard
    struct QEMUTimer *wd_timer;
64 a541f297 bellard
    /* NVRAM storage */
65 a541f297 bellard
    uint8_t *buffer;
66 42c812b9 Blue Swirl
    /* Model parameters */
67 42c812b9 Blue Swirl
    uint32_t type; /* 2 = m48t02, 8 = m48t08, 59 = m48t59 */
68 42c812b9 Blue Swirl
    /* NVRAM storage */
69 42c812b9 Blue Swirl
    uint16_t addr;
70 42c812b9 Blue Swirl
    uint8_t  lock;
71 c5df018e bellard
};
72 a541f297 bellard
73 f80237d4 Blue Swirl
typedef struct M48t59ISAState {
74 f80237d4 Blue Swirl
    ISADevice busdev;
75 43a34704 Blue Swirl
    M48t59State state;
76 f80237d4 Blue Swirl
} M48t59ISAState;
77 f80237d4 Blue Swirl
78 f80237d4 Blue Swirl
typedef struct M48t59SysBusState {
79 f80237d4 Blue Swirl
    SysBusDevice busdev;
80 43a34704 Blue Swirl
    M48t59State state;
81 f80237d4 Blue Swirl
} M48t59SysBusState;
82 f80237d4 Blue Swirl
83 a541f297 bellard
/* Fake timer functions */
84 a541f297 bellard
85 a541f297 bellard
/* Alarm management */
86 a541f297 bellard
static void alarm_cb (void *opaque)
87 a541f297 bellard
{
88 f6503059 balrog
    struct tm tm;
89 a541f297 bellard
    uint64_t next_time;
90 43a34704 Blue Swirl
    M48t59State *NVRAM = opaque;
91 a541f297 bellard
92 d537cf6c pbrook
    qemu_set_irq(NVRAM->IRQ, 1);
93 5fafdf24 ths
    if ((NVRAM->buffer[0x1FF5] & 0x80) == 0 &&
94 a541f297 bellard
        (NVRAM->buffer[0x1FF4] & 0x80) == 0 &&
95 a541f297 bellard
        (NVRAM->buffer[0x1FF3] & 0x80) == 0 &&
96 a541f297 bellard
        (NVRAM->buffer[0x1FF2] & 0x80) == 0) {
97 f6503059 balrog
        /* Repeat once a month */
98 f6503059 balrog
        qemu_get_timedate(&tm, NVRAM->time_offset);
99 f6503059 balrog
        tm.tm_mon++;
100 f6503059 balrog
        if (tm.tm_mon == 13) {
101 f6503059 balrog
            tm.tm_mon = 1;
102 f6503059 balrog
            tm.tm_year++;
103 f6503059 balrog
        }
104 f6503059 balrog
        next_time = qemu_timedate_diff(&tm) - NVRAM->time_offset;
105 a541f297 bellard
    } else if ((NVRAM->buffer[0x1FF5] & 0x80) != 0 &&
106 a541f297 bellard
               (NVRAM->buffer[0x1FF4] & 0x80) == 0 &&
107 a541f297 bellard
               (NVRAM->buffer[0x1FF3] & 0x80) == 0 &&
108 a541f297 bellard
               (NVRAM->buffer[0x1FF2] & 0x80) == 0) {
109 f6503059 balrog
        /* Repeat once a day */
110 f6503059 balrog
        next_time = 24 * 60 * 60;
111 a541f297 bellard
    } else if ((NVRAM->buffer[0x1FF5] & 0x80) != 0 &&
112 a541f297 bellard
               (NVRAM->buffer[0x1FF4] & 0x80) != 0 &&
113 a541f297 bellard
               (NVRAM->buffer[0x1FF3] & 0x80) == 0 &&
114 a541f297 bellard
               (NVRAM->buffer[0x1FF2] & 0x80) == 0) {
115 f6503059 balrog
        /* Repeat once an hour */
116 f6503059 balrog
        next_time = 60 * 60;
117 a541f297 bellard
    } else if ((NVRAM->buffer[0x1FF5] & 0x80) != 0 &&
118 a541f297 bellard
               (NVRAM->buffer[0x1FF4] & 0x80) != 0 &&
119 a541f297 bellard
               (NVRAM->buffer[0x1FF3] & 0x80) != 0 &&
120 a541f297 bellard
               (NVRAM->buffer[0x1FF2] & 0x80) == 0) {
121 f6503059 balrog
        /* Repeat once a minute */
122 f6503059 balrog
        next_time = 60;
123 a541f297 bellard
    } else {
124 f6503059 balrog
        /* Repeat once a second */
125 f6503059 balrog
        next_time = 1;
126 a541f297 bellard
    }
127 74475455 Paolo Bonzini
    qemu_mod_timer(NVRAM->alrm_timer, qemu_get_clock_ns(vm_clock) +
128 f6503059 balrog
                    next_time * 1000);
129 d537cf6c pbrook
    qemu_set_irq(NVRAM->IRQ, 0);
130 a541f297 bellard
}
131 a541f297 bellard
132 43a34704 Blue Swirl
static void set_alarm(M48t59State *NVRAM)
133 f6503059 balrog
{
134 f6503059 balrog
    int diff;
135 f6503059 balrog
    if (NVRAM->alrm_timer != NULL) {
136 f6503059 balrog
        qemu_del_timer(NVRAM->alrm_timer);
137 f6503059 balrog
        diff = qemu_timedate_diff(&NVRAM->alarm) - NVRAM->time_offset;
138 f6503059 balrog
        if (diff > 0)
139 f6503059 balrog
            qemu_mod_timer(NVRAM->alrm_timer, diff * 1000);
140 f6503059 balrog
    }
141 f6503059 balrog
}
142 a541f297 bellard
143 f6503059 balrog
/* RTC management helpers */
144 43a34704 Blue Swirl
static inline void get_time(M48t59State *NVRAM, struct tm *tm)
145 a541f297 bellard
{
146 f6503059 balrog
    qemu_get_timedate(tm, NVRAM->time_offset);
147 a541f297 bellard
}
148 a541f297 bellard
149 43a34704 Blue Swirl
static void set_time(M48t59State *NVRAM, struct tm *tm)
150 a541f297 bellard
{
151 f6503059 balrog
    NVRAM->time_offset = qemu_timedate_diff(tm);
152 f6503059 balrog
    set_alarm(NVRAM);
153 a541f297 bellard
}
154 a541f297 bellard
155 a541f297 bellard
/* Watchdog management */
156 a541f297 bellard
static void watchdog_cb (void *opaque)
157 a541f297 bellard
{
158 43a34704 Blue Swirl
    M48t59State *NVRAM = opaque;
159 a541f297 bellard
160 a541f297 bellard
    NVRAM->buffer[0x1FF0] |= 0x80;
161 a541f297 bellard
    if (NVRAM->buffer[0x1FF7] & 0x80) {
162 a541f297 bellard
        NVRAM->buffer[0x1FF7] = 0x00;
163 a541f297 bellard
        NVRAM->buffer[0x1FFC] &= ~0x40;
164 13ab5daa bellard
        /* May it be a hw CPU Reset instead ? */
165 d7d02e3c bellard
        qemu_system_reset_request();
166 a541f297 bellard
    } else {
167 d537cf6c pbrook
        qemu_set_irq(NVRAM->IRQ, 1);
168 d537cf6c pbrook
        qemu_set_irq(NVRAM->IRQ, 0);
169 a541f297 bellard
    }
170 a541f297 bellard
}
171 a541f297 bellard
172 43a34704 Blue Swirl
static void set_up_watchdog(M48t59State *NVRAM, uint8_t value)
173 a541f297 bellard
{
174 a541f297 bellard
    uint64_t interval; /* in 1/16 seconds */
175 a541f297 bellard
176 868d585a j_mayer
    NVRAM->buffer[0x1FF0] &= ~0x80;
177 a541f297 bellard
    if (NVRAM->wd_timer != NULL) {
178 a541f297 bellard
        qemu_del_timer(NVRAM->wd_timer);
179 868d585a j_mayer
        if (value != 0) {
180 868d585a j_mayer
            interval = (1 << (2 * (value & 0x03))) * ((value >> 2) & 0x1F);
181 868d585a j_mayer
            qemu_mod_timer(NVRAM->wd_timer, ((uint64_t)time(NULL) * 1000) +
182 868d585a j_mayer
                           ((interval * 1000) >> 4));
183 868d585a j_mayer
        }
184 a541f297 bellard
    }
185 a541f297 bellard
}
186 a541f297 bellard
187 a541f297 bellard
/* Direct access to NVRAM */
188 897b4c6c j_mayer
void m48t59_write (void *opaque, uint32_t addr, uint32_t val)
189 a541f297 bellard
{
190 43a34704 Blue Swirl
    M48t59State *NVRAM = opaque;
191 a541f297 bellard
    struct tm tm;
192 a541f297 bellard
    int tmp;
193 a541f297 bellard
194 819385c5 bellard
    if (addr > 0x1FF8 && addr < 0x2000)
195 819385c5 bellard
        NVRAM_PRINTF("%s: 0x%08x => 0x%08x\n", __func__, addr, val);
196 4aed2c33 blueswir1
197 4aed2c33 blueswir1
    /* check for NVRAM access */
198 4aed2c33 blueswir1
    if ((NVRAM->type == 2 && addr < 0x7f8) ||
199 4aed2c33 blueswir1
        (NVRAM->type == 8 && addr < 0x1ff8) ||
200 4aed2c33 blueswir1
        (NVRAM->type == 59 && addr < 0x1ff0))
201 819385c5 bellard
        goto do_write;
202 4aed2c33 blueswir1
203 4aed2c33 blueswir1
    /* TOD access */
204 819385c5 bellard
    switch (addr) {
205 a541f297 bellard
    case 0x1FF0:
206 a541f297 bellard
        /* flags register : read-only */
207 a541f297 bellard
        break;
208 a541f297 bellard
    case 0x1FF1:
209 a541f297 bellard
        /* unused */
210 a541f297 bellard
        break;
211 a541f297 bellard
    case 0x1FF2:
212 a541f297 bellard
        /* alarm seconds */
213 abd0c6bd Paul Brook
        tmp = from_bcd(val & 0x7F);
214 819385c5 bellard
        if (tmp >= 0 && tmp <= 59) {
215 f6503059 balrog
            NVRAM->alarm.tm_sec = tmp;
216 819385c5 bellard
            NVRAM->buffer[0x1FF2] = val;
217 f6503059 balrog
            set_alarm(NVRAM);
218 819385c5 bellard
        }
219 a541f297 bellard
        break;
220 a541f297 bellard
    case 0x1FF3:
221 a541f297 bellard
        /* alarm minutes */
222 abd0c6bd Paul Brook
        tmp = from_bcd(val & 0x7F);
223 819385c5 bellard
        if (tmp >= 0 && tmp <= 59) {
224 f6503059 balrog
            NVRAM->alarm.tm_min = tmp;
225 819385c5 bellard
            NVRAM->buffer[0x1FF3] = val;
226 f6503059 balrog
            set_alarm(NVRAM);
227 819385c5 bellard
        }
228 a541f297 bellard
        break;
229 a541f297 bellard
    case 0x1FF4:
230 a541f297 bellard
        /* alarm hours */
231 abd0c6bd Paul Brook
        tmp = from_bcd(val & 0x3F);
232 819385c5 bellard
        if (tmp >= 0 && tmp <= 23) {
233 f6503059 balrog
            NVRAM->alarm.tm_hour = tmp;
234 819385c5 bellard
            NVRAM->buffer[0x1FF4] = val;
235 f6503059 balrog
            set_alarm(NVRAM);
236 819385c5 bellard
        }
237 a541f297 bellard
        break;
238 a541f297 bellard
    case 0x1FF5:
239 a541f297 bellard
        /* alarm date */
240 abd0c6bd Paul Brook
        tmp = from_bcd(val & 0x1F);
241 819385c5 bellard
        if (tmp != 0) {
242 f6503059 balrog
            NVRAM->alarm.tm_mday = tmp;
243 819385c5 bellard
            NVRAM->buffer[0x1FF5] = val;
244 f6503059 balrog
            set_alarm(NVRAM);
245 819385c5 bellard
        }
246 a541f297 bellard
        break;
247 a541f297 bellard
    case 0x1FF6:
248 a541f297 bellard
        /* interrupts */
249 819385c5 bellard
        NVRAM->buffer[0x1FF6] = val;
250 a541f297 bellard
        break;
251 a541f297 bellard
    case 0x1FF7:
252 a541f297 bellard
        /* watchdog */
253 819385c5 bellard
        NVRAM->buffer[0x1FF7] = val;
254 819385c5 bellard
        set_up_watchdog(NVRAM, val);
255 a541f297 bellard
        break;
256 a541f297 bellard
    case 0x1FF8:
257 4aed2c33 blueswir1
    case 0x07F8:
258 a541f297 bellard
        /* control */
259 4aed2c33 blueswir1
       NVRAM->buffer[addr] = (val & ~0xA0) | 0x90;
260 a541f297 bellard
        break;
261 a541f297 bellard
    case 0x1FF9:
262 4aed2c33 blueswir1
    case 0x07F9:
263 a541f297 bellard
        /* seconds (BCD) */
264 abd0c6bd Paul Brook
        tmp = from_bcd(val & 0x7F);
265 a541f297 bellard
        if (tmp >= 0 && tmp <= 59) {
266 a541f297 bellard
            get_time(NVRAM, &tm);
267 a541f297 bellard
            tm.tm_sec = tmp;
268 a541f297 bellard
            set_time(NVRAM, &tm);
269 a541f297 bellard
        }
270 f6503059 balrog
        if ((val & 0x80) ^ (NVRAM->buffer[addr] & 0x80)) {
271 a541f297 bellard
            if (val & 0x80) {
272 a541f297 bellard
                NVRAM->stop_time = time(NULL);
273 a541f297 bellard
            } else {
274 a541f297 bellard
                NVRAM->time_offset += NVRAM->stop_time - time(NULL);
275 a541f297 bellard
                NVRAM->stop_time = 0;
276 a541f297 bellard
            }
277 a541f297 bellard
        }
278 f6503059 balrog
        NVRAM->buffer[addr] = val & 0x80;
279 a541f297 bellard
        break;
280 a541f297 bellard
    case 0x1FFA:
281 4aed2c33 blueswir1
    case 0x07FA:
282 a541f297 bellard
        /* minutes (BCD) */
283 abd0c6bd Paul Brook
        tmp = from_bcd(val & 0x7F);
284 a541f297 bellard
        if (tmp >= 0 && tmp <= 59) {
285 a541f297 bellard
            get_time(NVRAM, &tm);
286 a541f297 bellard
            tm.tm_min = tmp;
287 a541f297 bellard
            set_time(NVRAM, &tm);
288 a541f297 bellard
        }
289 a541f297 bellard
        break;
290 a541f297 bellard
    case 0x1FFB:
291 4aed2c33 blueswir1
    case 0x07FB:
292 a541f297 bellard
        /* hours (BCD) */
293 abd0c6bd Paul Brook
        tmp = from_bcd(val & 0x3F);
294 a541f297 bellard
        if (tmp >= 0 && tmp <= 23) {
295 a541f297 bellard
            get_time(NVRAM, &tm);
296 a541f297 bellard
            tm.tm_hour = tmp;
297 a541f297 bellard
            set_time(NVRAM, &tm);
298 a541f297 bellard
        }
299 a541f297 bellard
        break;
300 a541f297 bellard
    case 0x1FFC:
301 4aed2c33 blueswir1
    case 0x07FC:
302 a541f297 bellard
        /* day of the week / century */
303 abd0c6bd Paul Brook
        tmp = from_bcd(val & 0x07);
304 a541f297 bellard
        get_time(NVRAM, &tm);
305 a541f297 bellard
        tm.tm_wday = tmp;
306 a541f297 bellard
        set_time(NVRAM, &tm);
307 4aed2c33 blueswir1
        NVRAM->buffer[addr] = val & 0x40;
308 a541f297 bellard
        break;
309 a541f297 bellard
    case 0x1FFD:
310 4aed2c33 blueswir1
    case 0x07FD:
311 a541f297 bellard
        /* date */
312 abd0c6bd Paul Brook
        tmp = from_bcd(val & 0x1F);
313 a541f297 bellard
        if (tmp != 0) {
314 a541f297 bellard
            get_time(NVRAM, &tm);
315 a541f297 bellard
            tm.tm_mday = tmp;
316 a541f297 bellard
            set_time(NVRAM, &tm);
317 a541f297 bellard
        }
318 a541f297 bellard
        break;
319 a541f297 bellard
    case 0x1FFE:
320 4aed2c33 blueswir1
    case 0x07FE:
321 a541f297 bellard
        /* month */
322 abd0c6bd Paul Brook
        tmp = from_bcd(val & 0x1F);
323 a541f297 bellard
        if (tmp >= 1 && tmp <= 12) {
324 a541f297 bellard
            get_time(NVRAM, &tm);
325 a541f297 bellard
            tm.tm_mon = tmp - 1;
326 a541f297 bellard
            set_time(NVRAM, &tm);
327 a541f297 bellard
        }
328 a541f297 bellard
        break;
329 a541f297 bellard
    case 0x1FFF:
330 4aed2c33 blueswir1
    case 0x07FF:
331 a541f297 bellard
        /* year */
332 abd0c6bd Paul Brook
        tmp = from_bcd(val);
333 a541f297 bellard
        if (tmp >= 0 && tmp <= 99) {
334 a541f297 bellard
            get_time(NVRAM, &tm);
335 180b700d bellard
            if (NVRAM->type == 8)
336 abd0c6bd Paul Brook
                tm.tm_year = from_bcd(val) + 68; // Base year is 1968
337 180b700d bellard
            else
338 abd0c6bd Paul Brook
                tm.tm_year = from_bcd(val);
339 a541f297 bellard
            set_time(NVRAM, &tm);
340 a541f297 bellard
        }
341 a541f297 bellard
        break;
342 a541f297 bellard
    default:
343 13ab5daa bellard
        /* Check lock registers state */
344 819385c5 bellard
        if (addr >= 0x20 && addr <= 0x2F && (NVRAM->lock & 1))
345 13ab5daa bellard
            break;
346 819385c5 bellard
        if (addr >= 0x30 && addr <= 0x3F && (NVRAM->lock & 2))
347 13ab5daa bellard
            break;
348 819385c5 bellard
    do_write:
349 819385c5 bellard
        if (addr < NVRAM->size) {
350 819385c5 bellard
            NVRAM->buffer[addr] = val & 0xFF;
351 a541f297 bellard
        }
352 a541f297 bellard
        break;
353 a541f297 bellard
    }
354 a541f297 bellard
}
355 a541f297 bellard
356 897b4c6c j_mayer
uint32_t m48t59_read (void *opaque, uint32_t addr)
357 a541f297 bellard
{
358 43a34704 Blue Swirl
    M48t59State *NVRAM = opaque;
359 a541f297 bellard
    struct tm tm;
360 a541f297 bellard
    uint32_t retval = 0xFF;
361 a541f297 bellard
362 4aed2c33 blueswir1
    /* check for NVRAM access */
363 4aed2c33 blueswir1
    if ((NVRAM->type == 2 && addr < 0x078f) ||
364 4aed2c33 blueswir1
        (NVRAM->type == 8 && addr < 0x1ff8) ||
365 4aed2c33 blueswir1
        (NVRAM->type == 59 && addr < 0x1ff0))
366 819385c5 bellard
        goto do_read;
367 4aed2c33 blueswir1
368 4aed2c33 blueswir1
    /* TOD access */
369 819385c5 bellard
    switch (addr) {
370 a541f297 bellard
    case 0x1FF0:
371 a541f297 bellard
        /* flags register */
372 a541f297 bellard
        goto do_read;
373 a541f297 bellard
    case 0x1FF1:
374 a541f297 bellard
        /* unused */
375 a541f297 bellard
        retval = 0;
376 a541f297 bellard
        break;
377 a541f297 bellard
    case 0x1FF2:
378 a541f297 bellard
        /* alarm seconds */
379 a541f297 bellard
        goto do_read;
380 a541f297 bellard
    case 0x1FF3:
381 a541f297 bellard
        /* alarm minutes */
382 a541f297 bellard
        goto do_read;
383 a541f297 bellard
    case 0x1FF4:
384 a541f297 bellard
        /* alarm hours */
385 a541f297 bellard
        goto do_read;
386 a541f297 bellard
    case 0x1FF5:
387 a541f297 bellard
        /* alarm date */
388 a541f297 bellard
        goto do_read;
389 a541f297 bellard
    case 0x1FF6:
390 a541f297 bellard
        /* interrupts */
391 a541f297 bellard
        goto do_read;
392 a541f297 bellard
    case 0x1FF7:
393 a541f297 bellard
        /* A read resets the watchdog */
394 a541f297 bellard
        set_up_watchdog(NVRAM, NVRAM->buffer[0x1FF7]);
395 a541f297 bellard
        goto do_read;
396 a541f297 bellard
    case 0x1FF8:
397 4aed2c33 blueswir1
    case 0x07F8:
398 a541f297 bellard
        /* control */
399 a541f297 bellard
        goto do_read;
400 a541f297 bellard
    case 0x1FF9:
401 4aed2c33 blueswir1
    case 0x07F9:
402 a541f297 bellard
        /* seconds (BCD) */
403 a541f297 bellard
        get_time(NVRAM, &tm);
404 abd0c6bd Paul Brook
        retval = (NVRAM->buffer[addr] & 0x80) | to_bcd(tm.tm_sec);
405 a541f297 bellard
        break;
406 a541f297 bellard
    case 0x1FFA:
407 4aed2c33 blueswir1
    case 0x07FA:
408 a541f297 bellard
        /* minutes (BCD) */
409 a541f297 bellard
        get_time(NVRAM, &tm);
410 abd0c6bd Paul Brook
        retval = to_bcd(tm.tm_min);
411 a541f297 bellard
        break;
412 a541f297 bellard
    case 0x1FFB:
413 4aed2c33 blueswir1
    case 0x07FB:
414 a541f297 bellard
        /* hours (BCD) */
415 a541f297 bellard
        get_time(NVRAM, &tm);
416 abd0c6bd Paul Brook
        retval = to_bcd(tm.tm_hour);
417 a541f297 bellard
        break;
418 a541f297 bellard
    case 0x1FFC:
419 4aed2c33 blueswir1
    case 0x07FC:
420 a541f297 bellard
        /* day of the week / century */
421 a541f297 bellard
        get_time(NVRAM, &tm);
422 4aed2c33 blueswir1
        retval = NVRAM->buffer[addr] | tm.tm_wday;
423 a541f297 bellard
        break;
424 a541f297 bellard
    case 0x1FFD:
425 4aed2c33 blueswir1
    case 0x07FD:
426 a541f297 bellard
        /* date */
427 a541f297 bellard
        get_time(NVRAM, &tm);
428 abd0c6bd Paul Brook
        retval = to_bcd(tm.tm_mday);
429 a541f297 bellard
        break;
430 a541f297 bellard
    case 0x1FFE:
431 4aed2c33 blueswir1
    case 0x07FE:
432 a541f297 bellard
        /* month */
433 a541f297 bellard
        get_time(NVRAM, &tm);
434 abd0c6bd Paul Brook
        retval = to_bcd(tm.tm_mon + 1);
435 a541f297 bellard
        break;
436 a541f297 bellard
    case 0x1FFF:
437 4aed2c33 blueswir1
    case 0x07FF:
438 a541f297 bellard
        /* year */
439 a541f297 bellard
        get_time(NVRAM, &tm);
440 5fafdf24 ths
        if (NVRAM->type == 8)
441 abd0c6bd Paul Brook
            retval = to_bcd(tm.tm_year - 68); // Base year is 1968
442 180b700d bellard
        else
443 abd0c6bd Paul Brook
            retval = to_bcd(tm.tm_year);
444 a541f297 bellard
        break;
445 a541f297 bellard
    default:
446 13ab5daa bellard
        /* Check lock registers state */
447 819385c5 bellard
        if (addr >= 0x20 && addr <= 0x2F && (NVRAM->lock & 1))
448 13ab5daa bellard
            break;
449 819385c5 bellard
        if (addr >= 0x30 && addr <= 0x3F && (NVRAM->lock & 2))
450 13ab5daa bellard
            break;
451 819385c5 bellard
    do_read:
452 819385c5 bellard
        if (addr < NVRAM->size) {
453 819385c5 bellard
            retval = NVRAM->buffer[addr];
454 a541f297 bellard
        }
455 a541f297 bellard
        break;
456 a541f297 bellard
    }
457 819385c5 bellard
    if (addr > 0x1FF9 && addr < 0x2000)
458 9ed1e667 blueswir1
       NVRAM_PRINTF("%s: 0x%08x <= 0x%08x\n", __func__, addr, retval);
459 a541f297 bellard
460 a541f297 bellard
    return retval;
461 a541f297 bellard
}
462 a541f297 bellard
463 897b4c6c j_mayer
void m48t59_set_addr (void *opaque, uint32_t addr)
464 a541f297 bellard
{
465 43a34704 Blue Swirl
    M48t59State *NVRAM = opaque;
466 897b4c6c j_mayer
467 a541f297 bellard
    NVRAM->addr = addr;
468 a541f297 bellard
}
469 a541f297 bellard
470 897b4c6c j_mayer
void m48t59_toggle_lock (void *opaque, int lock)
471 13ab5daa bellard
{
472 43a34704 Blue Swirl
    M48t59State *NVRAM = opaque;
473 897b4c6c j_mayer
474 13ab5daa bellard
    NVRAM->lock ^= 1 << lock;
475 13ab5daa bellard
}
476 13ab5daa bellard
477 a541f297 bellard
/* IO access to NVRAM */
478 a541f297 bellard
static void NVRAM_writeb (void *opaque, uint32_t addr, uint32_t val)
479 a541f297 bellard
{
480 43a34704 Blue Swirl
    M48t59State *NVRAM = opaque;
481 a541f297 bellard
482 a541f297 bellard
    addr -= NVRAM->io_base;
483 9ed1e667 blueswir1
    NVRAM_PRINTF("%s: 0x%08x => 0x%08x\n", __func__, addr, val);
484 a541f297 bellard
    switch (addr) {
485 a541f297 bellard
    case 0:
486 a541f297 bellard
        NVRAM->addr &= ~0x00FF;
487 a541f297 bellard
        NVRAM->addr |= val;
488 a541f297 bellard
        break;
489 a541f297 bellard
    case 1:
490 a541f297 bellard
        NVRAM->addr &= ~0xFF00;
491 a541f297 bellard
        NVRAM->addr |= val << 8;
492 a541f297 bellard
        break;
493 a541f297 bellard
    case 3:
494 819385c5 bellard
        m48t59_write(NVRAM, val, NVRAM->addr);
495 a541f297 bellard
        NVRAM->addr = 0x0000;
496 a541f297 bellard
        break;
497 a541f297 bellard
    default:
498 a541f297 bellard
        break;
499 a541f297 bellard
    }
500 a541f297 bellard
}
501 a541f297 bellard
502 a541f297 bellard
static uint32_t NVRAM_readb (void *opaque, uint32_t addr)
503 a541f297 bellard
{
504 43a34704 Blue Swirl
    M48t59State *NVRAM = opaque;
505 13ab5daa bellard
    uint32_t retval;
506 a541f297 bellard
507 13ab5daa bellard
    addr -= NVRAM->io_base;
508 13ab5daa bellard
    switch (addr) {
509 13ab5daa bellard
    case 3:
510 819385c5 bellard
        retval = m48t59_read(NVRAM, NVRAM->addr);
511 13ab5daa bellard
        break;
512 13ab5daa bellard
    default:
513 13ab5daa bellard
        retval = -1;
514 13ab5daa bellard
        break;
515 13ab5daa bellard
    }
516 9ed1e667 blueswir1
    NVRAM_PRINTF("%s: 0x%08x <= 0x%08x\n", __func__, addr, retval);
517 a541f297 bellard
518 13ab5daa bellard
    return retval;
519 a541f297 bellard
}
520 a541f297 bellard
521 c227f099 Anthony Liguori
static void nvram_writeb (void *opaque, target_phys_addr_t addr, uint32_t value)
522 e1bb04f7 bellard
{
523 43a34704 Blue Swirl
    M48t59State *NVRAM = opaque;
524 3b46e624 ths
525 819385c5 bellard
    m48t59_write(NVRAM, addr, value & 0xff);
526 e1bb04f7 bellard
}
527 e1bb04f7 bellard
528 c227f099 Anthony Liguori
static void nvram_writew (void *opaque, target_phys_addr_t addr, uint32_t value)
529 e1bb04f7 bellard
{
530 43a34704 Blue Swirl
    M48t59State *NVRAM = opaque;
531 3b46e624 ths
532 819385c5 bellard
    m48t59_write(NVRAM, addr, (value >> 8) & 0xff);
533 819385c5 bellard
    m48t59_write(NVRAM, addr + 1, value & 0xff);
534 e1bb04f7 bellard
}
535 e1bb04f7 bellard
536 c227f099 Anthony Liguori
static void nvram_writel (void *opaque, target_phys_addr_t addr, uint32_t value)
537 e1bb04f7 bellard
{
538 43a34704 Blue Swirl
    M48t59State *NVRAM = opaque;
539 3b46e624 ths
540 819385c5 bellard
    m48t59_write(NVRAM, addr, (value >> 24) & 0xff);
541 819385c5 bellard
    m48t59_write(NVRAM, addr + 1, (value >> 16) & 0xff);
542 819385c5 bellard
    m48t59_write(NVRAM, addr + 2, (value >> 8) & 0xff);
543 819385c5 bellard
    m48t59_write(NVRAM, addr + 3, value & 0xff);
544 e1bb04f7 bellard
}
545 e1bb04f7 bellard
546 c227f099 Anthony Liguori
static uint32_t nvram_readb (void *opaque, target_phys_addr_t addr)
547 e1bb04f7 bellard
{
548 43a34704 Blue Swirl
    M48t59State *NVRAM = opaque;
549 819385c5 bellard
    uint32_t retval;
550 3b46e624 ths
551 819385c5 bellard
    retval = m48t59_read(NVRAM, addr);
552 e1bb04f7 bellard
    return retval;
553 e1bb04f7 bellard
}
554 e1bb04f7 bellard
555 c227f099 Anthony Liguori
static uint32_t nvram_readw (void *opaque, target_phys_addr_t addr)
556 e1bb04f7 bellard
{
557 43a34704 Blue Swirl
    M48t59State *NVRAM = opaque;
558 819385c5 bellard
    uint32_t retval;
559 3b46e624 ths
560 819385c5 bellard
    retval = m48t59_read(NVRAM, addr) << 8;
561 819385c5 bellard
    retval |= m48t59_read(NVRAM, addr + 1);
562 e1bb04f7 bellard
    return retval;
563 e1bb04f7 bellard
}
564 e1bb04f7 bellard
565 c227f099 Anthony Liguori
static uint32_t nvram_readl (void *opaque, target_phys_addr_t addr)
566 e1bb04f7 bellard
{
567 43a34704 Blue Swirl
    M48t59State *NVRAM = opaque;
568 819385c5 bellard
    uint32_t retval;
569 e1bb04f7 bellard
570 819385c5 bellard
    retval = m48t59_read(NVRAM, addr) << 24;
571 819385c5 bellard
    retval |= m48t59_read(NVRAM, addr + 1) << 16;
572 819385c5 bellard
    retval |= m48t59_read(NVRAM, addr + 2) << 8;
573 819385c5 bellard
    retval |= m48t59_read(NVRAM, addr + 3);
574 e1bb04f7 bellard
    return retval;
575 e1bb04f7 bellard
}
576 e1bb04f7 bellard
577 d60efc6b Blue Swirl
static CPUWriteMemoryFunc * const nvram_write[] = {
578 e1bb04f7 bellard
    &nvram_writeb,
579 e1bb04f7 bellard
    &nvram_writew,
580 e1bb04f7 bellard
    &nvram_writel,
581 e1bb04f7 bellard
};
582 e1bb04f7 bellard
583 d60efc6b Blue Swirl
static CPUReadMemoryFunc * const nvram_read[] = {
584 e1bb04f7 bellard
    &nvram_readb,
585 e1bb04f7 bellard
    &nvram_readw,
586 e1bb04f7 bellard
    &nvram_readl,
587 e1bb04f7 bellard
};
588 819385c5 bellard
589 fd484ae4 Juan Quintela
static const VMStateDescription vmstate_m48t59 = {
590 fd484ae4 Juan Quintela
    .name = "m48t59",
591 fd484ae4 Juan Quintela
    .version_id = 1,
592 fd484ae4 Juan Quintela
    .minimum_version_id = 1,
593 fd484ae4 Juan Quintela
    .minimum_version_id_old = 1,
594 fd484ae4 Juan Quintela
    .fields      = (VMStateField[]) {
595 fd484ae4 Juan Quintela
        VMSTATE_UINT8(lock, M48t59State),
596 fd484ae4 Juan Quintela
        VMSTATE_UINT16(addr, M48t59State),
597 fd484ae4 Juan Quintela
        VMSTATE_VBUFFER_UINT32(buffer, M48t59State, 0, NULL, 0, size),
598 fd484ae4 Juan Quintela
        VMSTATE_END_OF_LIST()
599 fd484ae4 Juan Quintela
    }
600 fd484ae4 Juan Quintela
};
601 3ccacc4a blueswir1
602 43a34704 Blue Swirl
static void m48t59_reset_common(M48t59State *NVRAM)
603 3ccacc4a blueswir1
{
604 6e6b7363 blueswir1
    NVRAM->addr = 0;
605 6e6b7363 blueswir1
    NVRAM->lock = 0;
606 3ccacc4a blueswir1
    if (NVRAM->alrm_timer != NULL)
607 3ccacc4a blueswir1
        qemu_del_timer(NVRAM->alrm_timer);
608 3ccacc4a blueswir1
609 3ccacc4a blueswir1
    if (NVRAM->wd_timer != NULL)
610 3ccacc4a blueswir1
        qemu_del_timer(NVRAM->wd_timer);
611 3ccacc4a blueswir1
}
612 3ccacc4a blueswir1
613 285e468d Blue Swirl
static void m48t59_reset_isa(DeviceState *d)
614 285e468d Blue Swirl
{
615 285e468d Blue Swirl
    M48t59ISAState *isa = container_of(d, M48t59ISAState, busdev.qdev);
616 43a34704 Blue Swirl
    M48t59State *NVRAM = &isa->state;
617 285e468d Blue Swirl
618 285e468d Blue Swirl
    m48t59_reset_common(NVRAM);
619 285e468d Blue Swirl
}
620 285e468d Blue Swirl
621 285e468d Blue Swirl
static void m48t59_reset_sysbus(DeviceState *d)
622 285e468d Blue Swirl
{
623 285e468d Blue Swirl
    M48t59SysBusState *sys = container_of(d, M48t59SysBusState, busdev.qdev);
624 43a34704 Blue Swirl
    M48t59State *NVRAM = &sys->state;
625 285e468d Blue Swirl
626 285e468d Blue Swirl
    m48t59_reset_common(NVRAM);
627 285e468d Blue Swirl
}
628 285e468d Blue Swirl
629 a541f297 bellard
/* Initialisation routine */
630 43a34704 Blue Swirl
M48t59State *m48t59_init(qemu_irq IRQ, target_phys_addr_t mem_base,
631 43a34704 Blue Swirl
                         uint32_t io_base, uint16_t size, int type)
632 a541f297 bellard
{
633 d27cf0ae Blue Swirl
    DeviceState *dev;
634 d27cf0ae Blue Swirl
    SysBusDevice *s;
635 f80237d4 Blue Swirl
    M48t59SysBusState *d;
636 51f9b84e Hervé Poussineau
    M48t59State *state;
637 d27cf0ae Blue Swirl
638 d27cf0ae Blue Swirl
    dev = qdev_create(NULL, "m48t59");
639 ee6847d1 Gerd Hoffmann
    qdev_prop_set_uint32(dev, "type", type);
640 ee6847d1 Gerd Hoffmann
    qdev_prop_set_uint32(dev, "size", size);
641 ee6847d1 Gerd Hoffmann
    qdev_prop_set_uint32(dev, "io_base", io_base);
642 e23a1b33 Markus Armbruster
    qdev_init_nofail(dev);
643 d27cf0ae Blue Swirl
    s = sysbus_from_qdev(dev);
644 51f9b84e Hervé Poussineau
    d = FROM_SYSBUS(M48t59SysBusState, s);
645 51f9b84e Hervé Poussineau
    state = &d->state;
646 d27cf0ae Blue Swirl
    sysbus_connect_irq(s, 0, IRQ);
647 819385c5 bellard
    if (io_base != 0) {
648 51f9b84e Hervé Poussineau
        register_ioport_read(io_base, 0x04, 1, NVRAM_readb, state);
649 51f9b84e Hervé Poussineau
        register_ioport_write(io_base, 0x04, 1, NVRAM_writeb, state);
650 819385c5 bellard
    }
651 e1bb04f7 bellard
    if (mem_base != 0) {
652 d27cf0ae Blue Swirl
        sysbus_mmio_map(s, 0, mem_base);
653 e1bb04f7 bellard
    }
654 d27cf0ae Blue Swirl
655 51f9b84e Hervé Poussineau
    return state;
656 d27cf0ae Blue Swirl
}
657 d27cf0ae Blue Swirl
658 43a34704 Blue Swirl
M48t59State *m48t59_init_isa(uint32_t io_base, uint16_t size, int type)
659 d27cf0ae Blue Swirl
{
660 f80237d4 Blue Swirl
    M48t59ISAState *d;
661 f80237d4 Blue Swirl
    ISADevice *dev;
662 43a34704 Blue Swirl
    M48t59State *s;
663 f80237d4 Blue Swirl
664 f80237d4 Blue Swirl
    dev = isa_create("m48t59_isa");
665 f80237d4 Blue Swirl
    qdev_prop_set_uint32(&dev->qdev, "type", type);
666 f80237d4 Blue Swirl
    qdev_prop_set_uint32(&dev->qdev, "size", size);
667 f80237d4 Blue Swirl
    qdev_prop_set_uint32(&dev->qdev, "io_base", io_base);
668 e23a1b33 Markus Armbruster
    qdev_init_nofail(&dev->qdev);
669 f80237d4 Blue Swirl
    d = DO_UPCAST(M48t59ISAState, busdev, dev);
670 f80237d4 Blue Swirl
    s = &d->state;
671 d27cf0ae Blue Swirl
672 f80237d4 Blue Swirl
    if (io_base != 0) {
673 f80237d4 Blue Swirl
        register_ioport_read(io_base, 0x04, 1, NVRAM_readb, s);
674 f80237d4 Blue Swirl
        register_ioport_write(io_base, 0x04, 1, NVRAM_writeb, s);
675 dee41d58 Gleb Natapov
        isa_init_ioport_range(dev, io_base, 4);
676 f80237d4 Blue Swirl
    }
677 d27cf0ae Blue Swirl
678 f80237d4 Blue Swirl
    return s;
679 f80237d4 Blue Swirl
}
680 d27cf0ae Blue Swirl
681 43a34704 Blue Swirl
static void m48t59_init_common(M48t59State *s)
682 f80237d4 Blue Swirl
{
683 7267c094 Anthony Liguori
    s->buffer = g_malloc0(s->size);
684 d27cf0ae Blue Swirl
    if (s->type == 59) {
685 74475455 Paolo Bonzini
        s->alrm_timer = qemu_new_timer_ns(vm_clock, &alarm_cb, s);
686 74475455 Paolo Bonzini
        s->wd_timer = qemu_new_timer_ns(vm_clock, &watchdog_cb, s);
687 819385c5 bellard
    }
688 f6503059 balrog
    qemu_get_timedate(&s->alarm, 0);
689 13ab5daa bellard
690 fd484ae4 Juan Quintela
    vmstate_register(NULL, -1, &vmstate_m48t59, s);
691 f80237d4 Blue Swirl
}
692 f80237d4 Blue Swirl
693 f80237d4 Blue Swirl
static int m48t59_init_isa1(ISADevice *dev)
694 f80237d4 Blue Swirl
{
695 f80237d4 Blue Swirl
    M48t59ISAState *d = DO_UPCAST(M48t59ISAState, busdev, dev);
696 43a34704 Blue Swirl
    M48t59State *s = &d->state;
697 f80237d4 Blue Swirl
698 f80237d4 Blue Swirl
    isa_init_irq(dev, &s->IRQ, 8);
699 f80237d4 Blue Swirl
    m48t59_init_common(s);
700 f80237d4 Blue Swirl
701 81a322d4 Gerd Hoffmann
    return 0;
702 d27cf0ae Blue Swirl
}
703 3ccacc4a blueswir1
704 f80237d4 Blue Swirl
static int m48t59_init1(SysBusDevice *dev)
705 f80237d4 Blue Swirl
{
706 f80237d4 Blue Swirl
    M48t59SysBusState *d = FROM_SYSBUS(M48t59SysBusState, dev);
707 43a34704 Blue Swirl
    M48t59State *s = &d->state;
708 f80237d4 Blue Swirl
    int mem_index;
709 f80237d4 Blue Swirl
710 f80237d4 Blue Swirl
    sysbus_init_irq(dev, &s->IRQ);
711 f80237d4 Blue Swirl
712 2507c12a Alexander Graf
    mem_index = cpu_register_io_memory(nvram_read, nvram_write, s,
713 2507c12a Alexander Graf
                                       DEVICE_NATIVE_ENDIAN);
714 f80237d4 Blue Swirl
    sysbus_init_mmio(dev, s->size, mem_index);
715 f80237d4 Blue Swirl
    m48t59_init_common(s);
716 f80237d4 Blue Swirl
717 f80237d4 Blue Swirl
    return 0;
718 f80237d4 Blue Swirl
}
719 f80237d4 Blue Swirl
720 f80237d4 Blue Swirl
static ISADeviceInfo m48t59_isa_info = {
721 f80237d4 Blue Swirl
    .init = m48t59_init_isa1,
722 f80237d4 Blue Swirl
    .qdev.name = "m48t59_isa",
723 f80237d4 Blue Swirl
    .qdev.size = sizeof(M48t59ISAState),
724 285e468d Blue Swirl
    .qdev.reset = m48t59_reset_isa,
725 f80237d4 Blue Swirl
    .qdev.no_user = 1,
726 f80237d4 Blue Swirl
    .qdev.props = (Property[]) {
727 f80237d4 Blue Swirl
        DEFINE_PROP_UINT32("size",    M48t59ISAState, state.size,    -1),
728 f80237d4 Blue Swirl
        DEFINE_PROP_UINT32("type",    M48t59ISAState, state.type,    -1),
729 f80237d4 Blue Swirl
        DEFINE_PROP_HEX32( "io_base", M48t59ISAState, state.io_base,  0),
730 f80237d4 Blue Swirl
        DEFINE_PROP_END_OF_LIST(),
731 f80237d4 Blue Swirl
    }
732 f80237d4 Blue Swirl
};
733 f80237d4 Blue Swirl
734 ee6847d1 Gerd Hoffmann
static SysBusDeviceInfo m48t59_info = {
735 ee6847d1 Gerd Hoffmann
    .init = m48t59_init1,
736 ee6847d1 Gerd Hoffmann
    .qdev.name  = "m48t59",
737 f80237d4 Blue Swirl
    .qdev.size = sizeof(M48t59SysBusState),
738 285e468d Blue Swirl
    .qdev.reset = m48t59_reset_sysbus,
739 ee6847d1 Gerd Hoffmann
    .qdev.props = (Property[]) {
740 f80237d4 Blue Swirl
        DEFINE_PROP_UINT32("size",    M48t59SysBusState, state.size,    -1),
741 f80237d4 Blue Swirl
        DEFINE_PROP_UINT32("type",    M48t59SysBusState, state.type,    -1),
742 f80237d4 Blue Swirl
        DEFINE_PROP_HEX32( "io_base", M48t59SysBusState, state.io_base,  0),
743 01274424 Gerd Hoffmann
        DEFINE_PROP_END_OF_LIST(),
744 ee6847d1 Gerd Hoffmann
    }
745 ee6847d1 Gerd Hoffmann
};
746 ee6847d1 Gerd Hoffmann
747 d27cf0ae Blue Swirl
static void m48t59_register_devices(void)
748 d27cf0ae Blue Swirl
{
749 ee6847d1 Gerd Hoffmann
    sysbus_register_withprop(&m48t59_info);
750 f80237d4 Blue Swirl
    isa_qdev_register(&m48t59_isa_info);
751 a541f297 bellard
}
752 d27cf0ae Blue Swirl
753 d27cf0ae Blue Swirl
device_init(m48t59_register_devices)