Statistics
| Branch: | Revision:

root / hw / m48t59.c @ 4e3e9d0b

History | View | Annotate | Download (12.6 kB)

1
/*
2
 * QEMU M48T59 NVRAM emulation for PPC PREP platform
3
 * 
4
 * Copyright (c) 2003-2004 Jocelyn Mayer
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 "vl.h"
25
#include "m48t59.h"
26

    
27
//#define DEBUG_NVRAM
28

    
29
#if defined(DEBUG_NVRAM)
30
#define NVRAM_PRINTF(fmt, args...) do { printf(fmt , ##args); } while (0)
31
#else
32
#define NVRAM_PRINTF(fmt, args...) do { } while (0)
33
#endif
34

    
35
struct m48t59_t {
36
    /* Hardware parameters */
37
    int      IRQ;
38
    uint32_t io_base;
39
    uint16_t size;
40
    /* RTC management */
41
    time_t   time_offset;
42
    time_t   stop_time;
43
    /* Alarm & watchdog */
44
    time_t   alarm;
45
    struct QEMUTimer *alrm_timer;
46
    struct QEMUTimer *wd_timer;
47
    /* NVRAM storage */
48
    uint8_t  lock;
49
    uint16_t addr;
50
    uint8_t *buffer;
51
};
52

    
53
/* Fake timer functions */
54
/* Generic helpers for BCD */
55
static inline uint8_t toBCD (uint8_t value)
56
{
57
    return (((value / 10) % 10) << 4) | (value % 10);
58
}
59

    
60
static inline uint8_t fromBCD (uint8_t BCD)
61
{
62
    return ((BCD >> 4) * 10) + (BCD & 0x0F);
63
}
64

    
65
/* RTC management helpers */
66
static void get_time (m48t59_t *NVRAM, struct tm *tm)
67
{
68
    time_t t;
69

    
70
    t = time(NULL) + NVRAM->time_offset;
71
#ifdef _WIN32
72
    memcpy(tm,localtime(&t),sizeof(*tm));
73
#else
74
    localtime_r (&t, tm) ;
75
#endif
76
}
77

    
78
static void set_time (m48t59_t *NVRAM, struct tm *tm)
79
{
80
    time_t now, new_time;
81
    
82
    new_time = mktime(tm);
83
    now = time(NULL);
84
    NVRAM->time_offset = new_time - now;
85
}
86

    
87
/* Alarm management */
88
static void alarm_cb (void *opaque)
89
{
90
    struct tm tm, tm_now;
91
    uint64_t next_time;
92
    m48t59_t *NVRAM = opaque;
93

    
94
    pic_set_irq(NVRAM->IRQ, 1);
95
    if ((NVRAM->buffer[0x1FF5] & 0x80) == 0 && 
96
        (NVRAM->buffer[0x1FF4] & 0x80) == 0 &&
97
        (NVRAM->buffer[0x1FF3] & 0x80) == 0 &&
98
        (NVRAM->buffer[0x1FF2] & 0x80) == 0) {
99
        /* Repeat once a month */
100
        get_time(NVRAM, &tm_now);
101
        memcpy(&tm, &tm_now, sizeof(struct tm));
102
        tm.tm_mon++;
103
        if (tm.tm_mon == 13) {
104
            tm.tm_mon = 1;
105
            tm.tm_year++;
106
        }
107
        next_time = mktime(&tm);
108
    } else if ((NVRAM->buffer[0x1FF5] & 0x80) != 0 &&
109
               (NVRAM->buffer[0x1FF4] & 0x80) == 0 &&
110
               (NVRAM->buffer[0x1FF3] & 0x80) == 0 &&
111
               (NVRAM->buffer[0x1FF2] & 0x80) == 0) {
112
        /* Repeat once a day */
113
        next_time = 24 * 60 * 60 + mktime(&tm_now);
114
    } else if ((NVRAM->buffer[0x1FF5] & 0x80) != 0 &&
115
               (NVRAM->buffer[0x1FF4] & 0x80) != 0 &&
116
               (NVRAM->buffer[0x1FF3] & 0x80) == 0 &&
117
               (NVRAM->buffer[0x1FF2] & 0x80) == 0) {
118
        /* Repeat once an hour */
119
        next_time = 60 * 60 + mktime(&tm_now);
120
    } else if ((NVRAM->buffer[0x1FF5] & 0x80) != 0 &&
121
               (NVRAM->buffer[0x1FF4] & 0x80) != 0 &&
122
               (NVRAM->buffer[0x1FF3] & 0x80) != 0 &&
123
               (NVRAM->buffer[0x1FF2] & 0x80) == 0) {
124
        /* Repeat once a minute */
125
        next_time = 60 + mktime(&tm_now);
126
    } else {
127
        /* Repeat once a second */
128
        next_time = 1 + mktime(&tm_now);
129
    }
130
    qemu_mod_timer(NVRAM->alrm_timer, next_time * 1000);
131
    pic_set_irq(NVRAM->IRQ, 0);
132
}
133

    
134

    
135
static void get_alarm (m48t59_t *NVRAM, struct tm *tm)
136
{
137
#ifdef _WIN32
138
    memcpy(tm,localtime(&NVRAM->alarm),sizeof(*tm));
139
#else
140
    localtime_r (&NVRAM->alarm, tm);
141
#endif
142
}
143

    
144
static void set_alarm (m48t59_t *NVRAM, struct tm *tm)
145
{
146
    NVRAM->alarm = mktime(tm);
147
    if (NVRAM->alrm_timer != NULL) {
148
        qemu_del_timer(NVRAM->alrm_timer);
149
        NVRAM->alrm_timer = NULL;
150
    }
151
    if (NVRAM->alarm - time(NULL) > 0)
152
        qemu_mod_timer(NVRAM->alrm_timer, NVRAM->alarm * 1000);
153
}
154

    
155
/* Watchdog management */
156
static void watchdog_cb (void *opaque)
157
{
158
    m48t59_t *NVRAM = opaque;
159

    
160
    NVRAM->buffer[0x1FF0] |= 0x80;
161
    if (NVRAM->buffer[0x1FF7] & 0x80) {
162
        NVRAM->buffer[0x1FF7] = 0x00;
163
        NVRAM->buffer[0x1FFC] &= ~0x40;
164
        /* May it be a hw CPU Reset instead ? */
165
        reset_requested = 1;
166
        printf("Watchdog reset...\n");
167
        cpu_interrupt(cpu_single_env, CPU_INTERRUPT_EXIT);
168
    } else {
169
        pic_set_irq(NVRAM->IRQ, 1);
170
        pic_set_irq(NVRAM->IRQ, 0);
171
    }
172
}
173

    
174
static void set_up_watchdog (m48t59_t *NVRAM, uint8_t value)
175
{
176
    uint64_t interval; /* in 1/16 seconds */
177

    
178
    if (NVRAM->wd_timer != NULL) {
179
        qemu_del_timer(NVRAM->wd_timer);
180
        NVRAM->wd_timer = NULL;
181
    }
182
    NVRAM->buffer[0x1FF0] &= ~0x80;
183
    if (value != 0) {
184
        interval = (1 << (2 * (value & 0x03))) * ((value >> 2) & 0x1F);
185
        qemu_mod_timer(NVRAM->wd_timer, ((uint64_t)time(NULL) * 1000) +
186
                       ((interval * 1000) >> 4));
187
    }
188
}
189

    
190
/* Direct access to NVRAM */
191
void m48t59_write (m48t59_t *NVRAM, uint32_t val)
192
{
193
    struct tm tm;
194
    int tmp;
195

    
196
    if (NVRAM->addr > 0x1FF8 && NVRAM->addr < 0x2000)
197
        NVRAM_PRINTF("%s: 0x%08x => 0x%08x\n", __func__, NVRAM->addr, val);
198
    switch (NVRAM->addr) {
199
    case 0x1FF0:
200
        /* flags register : read-only */
201
        break;
202
    case 0x1FF1:
203
        /* unused */
204
        break;
205
    case 0x1FF2:
206
        /* alarm seconds */
207
        tmp = fromBCD(val & 0x7F);
208
        if (tmp >= 0 && tmp <= 59) {
209
            get_alarm(NVRAM, &tm);
210
            tm.tm_sec = tmp;
211
            NVRAM->buffer[0x1FF2] = val;
212
            set_alarm(NVRAM, &tm);
213
        }
214
        break;
215
    case 0x1FF3:
216
        /* alarm minutes */
217
        tmp = fromBCD(val & 0x7F);
218
        if (tmp >= 0 && tmp <= 59) {
219
            get_alarm(NVRAM, &tm);
220
            tm.tm_min = tmp;
221
            NVRAM->buffer[0x1FF3] = val;
222
            set_alarm(NVRAM, &tm);
223
        }
224
        break;
225
    case 0x1FF4:
226
        /* alarm hours */
227
        tmp = fromBCD(val & 0x3F);
228
        if (tmp >= 0 && tmp <= 23) {
229
            get_alarm(NVRAM, &tm);
230
            tm.tm_hour = tmp;
231
            NVRAM->buffer[0x1FF4] = val;
232
            set_alarm(NVRAM, &tm);
233
        }
234
        break;
235
    case 0x1FF5:
236
        /* alarm date */
237
        tmp = fromBCD(val & 0x1F);
238
        if (tmp != 0) {
239
            get_alarm(NVRAM, &tm);
240
            tm.tm_mday = tmp;
241
            NVRAM->buffer[0x1FF5] = val;
242
            set_alarm(NVRAM, &tm);
243
        }
244
        break;
245
    case 0x1FF6:
246
        /* interrupts */
247
        NVRAM->buffer[0x1FF6] = val;
248
        break;
249
    case 0x1FF7:
250
        /* watchdog */
251
        NVRAM->buffer[0x1FF7] = val;
252
        set_up_watchdog(NVRAM, val);
253
        break;
254
    case 0x1FF8:
255
        /* control */
256
        NVRAM->buffer[0x1FF8] = (val & ~0xA0) | 0x90;
257
        break;
258
    case 0x1FF9:
259
        /* seconds (BCD) */
260
        tmp = fromBCD(val & 0x7F);
261
        if (tmp >= 0 && tmp <= 59) {
262
            get_time(NVRAM, &tm);
263
            tm.tm_sec = tmp;
264
            set_time(NVRAM, &tm);
265
        }
266
        if ((val & 0x80) ^ (NVRAM->buffer[0x1FF9] & 0x80)) {
267
            if (val & 0x80) {
268
                NVRAM->stop_time = time(NULL);
269
            } else {
270
                NVRAM->time_offset += NVRAM->stop_time - time(NULL);
271
                NVRAM->stop_time = 0;
272
            }
273
        }
274
        NVRAM->buffer[0x1FF9] = val & 0x80;
275
        break;
276
    case 0x1FFA:
277
        /* minutes (BCD) */
278
        tmp = fromBCD(val & 0x7F);
279
        if (tmp >= 0 && tmp <= 59) {
280
            get_time(NVRAM, &tm);
281
            tm.tm_min = tmp;
282
            set_time(NVRAM, &tm);
283
        }
284
        break;
285
    case 0x1FFB:
286
        /* hours (BCD) */
287
        tmp = fromBCD(val & 0x3F);
288
        if (tmp >= 0 && tmp <= 23) {
289
            get_time(NVRAM, &tm);
290
            tm.tm_hour = tmp;
291
            set_time(NVRAM, &tm);
292
        }
293
        break;
294
    case 0x1FFC:
295
        /* day of the week / century */
296
        tmp = fromBCD(val & 0x07);
297
        get_time(NVRAM, &tm);
298
        tm.tm_wday = tmp;
299
        set_time(NVRAM, &tm);
300
        NVRAM->buffer[0x1FFC] = val & 0x40;
301
        break;
302
    case 0x1FFD:
303
        /* date */
304
        tmp = fromBCD(val & 0x1F);
305
        if (tmp != 0) {
306
            get_time(NVRAM, &tm);
307
            tm.tm_mday = tmp;
308
            set_time(NVRAM, &tm);
309
        }
310
        break;
311
    case 0x1FFE:
312
        /* month */
313
        tmp = fromBCD(val & 0x1F);
314
        if (tmp >= 1 && tmp <= 12) {
315
            get_time(NVRAM, &tm);
316
            tm.tm_mon = tmp - 1;
317
            set_time(NVRAM, &tm);
318
        }
319
        break;
320
    case 0x1FFF:
321
        /* year */
322
        tmp = fromBCD(val);
323
        if (tmp >= 0 && tmp <= 99) {
324
            get_time(NVRAM, &tm);
325
            tm.tm_year = fromBCD(val);
326
            set_time(NVRAM, &tm);
327
        }
328
        break;
329
    default:
330
        /* Check lock registers state */
331
        if (NVRAM->addr >= 0x20 && NVRAM->addr <= 0x2F && (NVRAM->lock & 1))
332
            break;
333
        if (NVRAM->addr >= 0x30 && NVRAM->addr <= 0x3F && (NVRAM->lock & 2))
334
            break;
335
        if (NVRAM->addr < 0x1FF0 ||
336
            (NVRAM->addr > 0x1FFF && NVRAM->addr < NVRAM->size)) {
337
            NVRAM->buffer[NVRAM->addr] = val & 0xFF;
338
        }
339
        break;
340
    }
341
}
342

    
343
uint32_t m48t59_read (m48t59_t *NVRAM)
344
{
345
    struct tm tm;
346
    uint32_t retval = 0xFF;
347

    
348
    switch (NVRAM->addr) {
349
    case 0x1FF0:
350
        /* flags register */
351
        goto do_read;
352
    case 0x1FF1:
353
        /* unused */
354
        retval = 0;
355
        break;
356
    case 0x1FF2:
357
        /* alarm seconds */
358
        goto do_read;
359
    case 0x1FF3:
360
        /* alarm minutes */
361
        goto do_read;
362
    case 0x1FF4:
363
        /* alarm hours */
364
        goto do_read;
365
    case 0x1FF5:
366
        /* alarm date */
367
        goto do_read;
368
    case 0x1FF6:
369
        /* interrupts */
370
        goto do_read;
371
    case 0x1FF7:
372
        /* A read resets the watchdog */
373
        set_up_watchdog(NVRAM, NVRAM->buffer[0x1FF7]);
374
        goto do_read;
375
    case 0x1FF8:
376
        /* control */
377
        goto do_read;
378
    case 0x1FF9:
379
        /* seconds (BCD) */
380
        get_time(NVRAM, &tm);
381
        retval = (NVRAM->buffer[0x1FF9] & 0x80) | toBCD(tm.tm_sec);
382
        break;
383
    case 0x1FFA:
384
        /* minutes (BCD) */
385
        get_time(NVRAM, &tm);
386
        retval = toBCD(tm.tm_min);
387
        break;
388
    case 0x1FFB:
389
        /* hours (BCD) */
390
        get_time(NVRAM, &tm);
391
        retval = toBCD(tm.tm_hour);
392
        break;
393
    case 0x1FFC:
394
        /* day of the week / century */
395
        get_time(NVRAM, &tm);
396
        retval = NVRAM->buffer[0x1FFC] | tm.tm_wday;
397
        break;
398
    case 0x1FFD:
399
        /* date */
400
        get_time(NVRAM, &tm);
401
        retval = toBCD(tm.tm_mday);
402
        break;
403
    case 0x1FFE:
404
        /* month */
405
        get_time(NVRAM, &tm);
406
        retval = toBCD(tm.tm_mon + 1);
407
        break;
408
    case 0x1FFF:
409
        /* year */
410
        get_time(NVRAM, &tm);
411
        retval = toBCD(tm.tm_year);
412
        break;
413
    default:
414
        /* Check lock registers state */
415
        if (NVRAM->addr >= 0x20 && NVRAM->addr <= 0x2F && (NVRAM->lock & 1))
416
            break;
417
        if (NVRAM->addr >= 0x30 && NVRAM->addr <= 0x3F && (NVRAM->lock & 2))
418
            break;
419
        if (NVRAM->addr < 0x1FF0 ||
420
            (NVRAM->addr > 0x1FFF && NVRAM->addr < NVRAM->size)) {
421
        do_read:
422
            retval = NVRAM->buffer[NVRAM->addr];
423
        }
424
        break;
425
    }
426
    if (NVRAM->addr > 0x1FF9 && NVRAM->addr < 0x2000)
427
        NVRAM_PRINTF("0x%08x <= 0x%08x\n", NVRAM->addr, retval);
428

    
429
    return retval;
430
}
431

    
432
void m48t59_set_addr (m48t59_t *NVRAM, uint32_t addr)
433
{
434
    NVRAM->addr = addr;
435
}
436

    
437
void m48t59_toggle_lock (m48t59_t *NVRAM, int lock)
438
{
439
    NVRAM->lock ^= 1 << lock;
440
}
441

    
442
/* IO access to NVRAM */
443
static void NVRAM_writeb (void *opaque, uint32_t addr, uint32_t val)
444
{
445
    m48t59_t *NVRAM = opaque;
446

    
447
    addr -= NVRAM->io_base;
448
    NVRAM_PRINTF("0x%08x => 0x%08x\n", addr, val);
449
    switch (addr) {
450
    case 0:
451
        NVRAM->addr &= ~0x00FF;
452
        NVRAM->addr |= val;
453
        break;
454
    case 1:
455
        NVRAM->addr &= ~0xFF00;
456
        NVRAM->addr |= val << 8;
457
        break;
458
    case 3:
459
        m48t59_write(NVRAM, val);
460
        NVRAM->addr = 0x0000;
461
        break;
462
    default:
463
        break;
464
    }
465
}
466

    
467
static uint32_t NVRAM_readb (void *opaque, uint32_t addr)
468
{
469
    m48t59_t *NVRAM = opaque;
470
    uint32_t retval;
471

    
472
    addr -= NVRAM->io_base;
473
    switch (addr) {
474
    case 3:
475
        retval = m48t59_read(NVRAM);
476
        break;
477
    default:
478
        retval = -1;
479
        break;
480
    }
481
    NVRAM_PRINTF("0x%08x <= 0x%08x\n", addr, retval);
482

    
483
    return retval;
484
}
485

    
486
/* Initialisation routine */
487
m48t59_t *m48t59_init (int IRQ, uint32_t io_base, uint16_t size)
488
{
489
    m48t59_t *s;
490

    
491
    s = qemu_mallocz(sizeof(m48t59_t));
492
    if (!s)
493
        return NULL;
494
    s->buffer = qemu_mallocz(size);
495
    if (!s->buffer) {
496
        qemu_free(s);
497
        return NULL;
498
    }
499
    s->IRQ = IRQ;
500
    s->size = size;
501
    s->io_base = io_base;
502
    s->addr = 0;
503
    register_ioport_read(io_base, 0x04, 1, NVRAM_readb, s);
504
    register_ioport_write(io_base, 0x04, 1, NVRAM_writeb, s);
505
    s->alrm_timer = qemu_new_timer(vm_clock, &alarm_cb, s);
506
    s->wd_timer = qemu_new_timer(vm_clock, &watchdog_cb, s);
507
    s->lock = 0;
508

    
509
    return s;
510
}