2 * QEMU M48T59 and M48T08 NVRAM emulation for PPC PREP and Sparc platforms
4 * Copyright (c) 2003-2005, 2007 Jocelyn Mayer
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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
27 #include "qemu-timer.h"
32 #if defined(DEBUG_NVRAM)
33 #define NVRAM_PRINTF(fmt, args...) do { printf(fmt , ##args); } while (0)
35 #define NVRAM_PRINTF(fmt, args...) do { } while (0)
39 * The M48T02, M48T08 and M48T59 chips are very similar. The newer '59 has
40 * alarm and a watchdog timer and related control registers. In the
41 * PPC platform there is also a nvram lock function.
44 /* Model parameters */
45 int type
; // 2 = m48t02, 8 = m48t08, 59 = m48t59
46 /* Hardware parameters */
49 target_phys_addr_t mem_base
;
55 /* Alarm & watchdog */
57 struct QEMUTimer
*alrm_timer
;
58 struct QEMUTimer
*wd_timer
;
65 /* Fake timer functions */
66 /* Generic helpers for BCD */
67 static inline uint8_t toBCD (uint8_t value
)
69 return (((value
/ 10) % 10) << 4) | (value
% 10);
72 static inline uint8_t fromBCD (uint8_t BCD
)
74 return ((BCD
>> 4) * 10) + (BCD
& 0x0F);
77 /* Alarm management */
78 static void alarm_cb (void *opaque
)
82 m48t59_t
*NVRAM
= opaque
;
84 qemu_set_irq(NVRAM
->IRQ
, 1);
85 if ((NVRAM
->buffer
[0x1FF5] & 0x80) == 0 &&
86 (NVRAM
->buffer
[0x1FF4] & 0x80) == 0 &&
87 (NVRAM
->buffer
[0x1FF3] & 0x80) == 0 &&
88 (NVRAM
->buffer
[0x1FF2] & 0x80) == 0) {
89 /* Repeat once a month */
90 qemu_get_timedate(&tm
, NVRAM
->time_offset
);
92 if (tm
.tm_mon
== 13) {
96 next_time
= qemu_timedate_diff(&tm
) - NVRAM
->time_offset
;
97 } else if ((NVRAM
->buffer
[0x1FF5] & 0x80) != 0 &&
98 (NVRAM
->buffer
[0x1FF4] & 0x80) == 0 &&
99 (NVRAM
->buffer
[0x1FF3] & 0x80) == 0 &&
100 (NVRAM
->buffer
[0x1FF2] & 0x80) == 0) {
101 /* Repeat once a day */
102 next_time
= 24 * 60 * 60;
103 } else if ((NVRAM
->buffer
[0x1FF5] & 0x80) != 0 &&
104 (NVRAM
->buffer
[0x1FF4] & 0x80) != 0 &&
105 (NVRAM
->buffer
[0x1FF3] & 0x80) == 0 &&
106 (NVRAM
->buffer
[0x1FF2] & 0x80) == 0) {
107 /* Repeat once an hour */
109 } else if ((NVRAM
->buffer
[0x1FF5] & 0x80) != 0 &&
110 (NVRAM
->buffer
[0x1FF4] & 0x80) != 0 &&
111 (NVRAM
->buffer
[0x1FF3] & 0x80) != 0 &&
112 (NVRAM
->buffer
[0x1FF2] & 0x80) == 0) {
113 /* Repeat once a minute */
116 /* Repeat once a second */
119 qemu_mod_timer(NVRAM
->alrm_timer
, qemu_get_clock(vm_clock
) +
121 qemu_set_irq(NVRAM
->IRQ
, 0);
124 static void set_alarm (m48t59_t
*NVRAM
)
127 if (NVRAM
->alrm_timer
!= NULL
) {
128 qemu_del_timer(NVRAM
->alrm_timer
);
129 diff
= qemu_timedate_diff(&NVRAM
->alarm
) - NVRAM
->time_offset
;
131 qemu_mod_timer(NVRAM
->alrm_timer
, diff
* 1000);
135 /* RTC management helpers */
136 static inline void get_time (m48t59_t
*NVRAM
, struct tm
*tm
)
138 qemu_get_timedate(tm
, NVRAM
->time_offset
);
141 static void set_time (m48t59_t
*NVRAM
, struct tm
*tm
)
143 NVRAM
->time_offset
= qemu_timedate_diff(tm
);
147 /* Watchdog management */
148 static void watchdog_cb (void *opaque
)
150 m48t59_t
*NVRAM
= opaque
;
152 NVRAM
->buffer
[0x1FF0] |= 0x80;
153 if (NVRAM
->buffer
[0x1FF7] & 0x80) {
154 NVRAM
->buffer
[0x1FF7] = 0x00;
155 NVRAM
->buffer
[0x1FFC] &= ~0x40;
156 /* May it be a hw CPU Reset instead ? */
157 qemu_system_reset_request();
159 qemu_set_irq(NVRAM
->IRQ
, 1);
160 qemu_set_irq(NVRAM
->IRQ
, 0);
164 static void set_up_watchdog (m48t59_t
*NVRAM
, uint8_t value
)
166 uint64_t interval
; /* in 1/16 seconds */
168 NVRAM
->buffer
[0x1FF0] &= ~0x80;
169 if (NVRAM
->wd_timer
!= NULL
) {
170 qemu_del_timer(NVRAM
->wd_timer
);
172 interval
= (1 << (2 * (value
& 0x03))) * ((value
>> 2) & 0x1F);
173 qemu_mod_timer(NVRAM
->wd_timer
, ((uint64_t)time(NULL
) * 1000) +
174 ((interval
* 1000) >> 4));
179 /* Direct access to NVRAM */
180 void m48t59_write (void *opaque
, uint32_t addr
, uint32_t val
)
182 m48t59_t
*NVRAM
= opaque
;
186 if (addr
> 0x1FF8 && addr
< 0x2000)
187 NVRAM_PRINTF("%s: 0x%08x => 0x%08x\n", __func__
, addr
, val
);
189 /* check for NVRAM access */
190 if ((NVRAM
->type
== 2 && addr
< 0x7f8) ||
191 (NVRAM
->type
== 8 && addr
< 0x1ff8) ||
192 (NVRAM
->type
== 59 && addr
< 0x1ff0))
198 /* flags register : read-only */
205 tmp
= fromBCD(val
& 0x7F);
206 if (tmp
>= 0 && tmp
<= 59) {
207 NVRAM
->alarm
.tm_sec
= tmp
;
208 NVRAM
->buffer
[0x1FF2] = val
;
214 tmp
= fromBCD(val
& 0x7F);
215 if (tmp
>= 0 && tmp
<= 59) {
216 NVRAM
->alarm
.tm_min
= tmp
;
217 NVRAM
->buffer
[0x1FF3] = val
;
223 tmp
= fromBCD(val
& 0x3F);
224 if (tmp
>= 0 && tmp
<= 23) {
225 NVRAM
->alarm
.tm_hour
= tmp
;
226 NVRAM
->buffer
[0x1FF4] = val
;
232 tmp
= fromBCD(val
& 0x1F);
234 NVRAM
->alarm
.tm_mday
= tmp
;
235 NVRAM
->buffer
[0x1FF5] = val
;
241 NVRAM
->buffer
[0x1FF6] = val
;
245 NVRAM
->buffer
[0x1FF7] = val
;
246 set_up_watchdog(NVRAM
, val
);
251 NVRAM
->buffer
[addr
] = (val
& ~0xA0) | 0x90;
256 tmp
= fromBCD(val
& 0x7F);
257 if (tmp
>= 0 && tmp
<= 59) {
258 get_time(NVRAM
, &tm
);
260 set_time(NVRAM
, &tm
);
262 if ((val
& 0x80) ^ (NVRAM
->buffer
[addr
] & 0x80)) {
264 NVRAM
->stop_time
= time(NULL
);
266 NVRAM
->time_offset
+= NVRAM
->stop_time
- time(NULL
);
267 NVRAM
->stop_time
= 0;
270 NVRAM
->buffer
[addr
] = val
& 0x80;
275 tmp
= fromBCD(val
& 0x7F);
276 if (tmp
>= 0 && tmp
<= 59) {
277 get_time(NVRAM
, &tm
);
279 set_time(NVRAM
, &tm
);
285 tmp
= fromBCD(val
& 0x3F);
286 if (tmp
>= 0 && tmp
<= 23) {
287 get_time(NVRAM
, &tm
);
289 set_time(NVRAM
, &tm
);
294 /* day of the week / century */
295 tmp
= fromBCD(val
& 0x07);
296 get_time(NVRAM
, &tm
);
298 set_time(NVRAM
, &tm
);
299 NVRAM
->buffer
[addr
] = val
& 0x40;
304 tmp
= fromBCD(val
& 0x1F);
306 get_time(NVRAM
, &tm
);
308 set_time(NVRAM
, &tm
);
314 tmp
= fromBCD(val
& 0x1F);
315 if (tmp
>= 1 && tmp
<= 12) {
316 get_time(NVRAM
, &tm
);
318 set_time(NVRAM
, &tm
);
325 if (tmp
>= 0 && tmp
<= 99) {
326 get_time(NVRAM
, &tm
);
327 if (NVRAM
->type
== 8)
328 tm
.tm_year
= fromBCD(val
) + 68; // Base year is 1968
330 tm
.tm_year
= fromBCD(val
);
331 set_time(NVRAM
, &tm
);
335 /* Check lock registers state */
336 if (addr
>= 0x20 && addr
<= 0x2F && (NVRAM
->lock
& 1))
338 if (addr
>= 0x30 && addr
<= 0x3F && (NVRAM
->lock
& 2))
341 if (addr
< NVRAM
->size
) {
342 NVRAM
->buffer
[addr
] = val
& 0xFF;
348 uint32_t m48t59_read (void *opaque
, uint32_t addr
)
350 m48t59_t
*NVRAM
= opaque
;
352 uint32_t retval
= 0xFF;
354 /* check for NVRAM access */
355 if ((NVRAM
->type
== 2 && addr
< 0x078f) ||
356 (NVRAM
->type
== 8 && addr
< 0x1ff8) ||
357 (NVRAM
->type
== 59 && addr
< 0x1ff0))
385 /* A read resets the watchdog */
386 set_up_watchdog(NVRAM
, NVRAM
->buffer
[0x1FF7]);
395 get_time(NVRAM
, &tm
);
396 retval
= (NVRAM
->buffer
[addr
] & 0x80) | toBCD(tm
.tm_sec
);
401 get_time(NVRAM
, &tm
);
402 retval
= toBCD(tm
.tm_min
);
407 get_time(NVRAM
, &tm
);
408 retval
= toBCD(tm
.tm_hour
);
412 /* day of the week / century */
413 get_time(NVRAM
, &tm
);
414 retval
= NVRAM
->buffer
[addr
] | tm
.tm_wday
;
419 get_time(NVRAM
, &tm
);
420 retval
= toBCD(tm
.tm_mday
);
425 get_time(NVRAM
, &tm
);
426 retval
= toBCD(tm
.tm_mon
+ 1);
431 get_time(NVRAM
, &tm
);
432 if (NVRAM
->type
== 8)
433 retval
= toBCD(tm
.tm_year
- 68); // Base year is 1968
435 retval
= toBCD(tm
.tm_year
);
438 /* Check lock registers state */
439 if (addr
>= 0x20 && addr
<= 0x2F && (NVRAM
->lock
& 1))
441 if (addr
>= 0x30 && addr
<= 0x3F && (NVRAM
->lock
& 2))
444 if (addr
< NVRAM
->size
) {
445 retval
= NVRAM
->buffer
[addr
];
449 if (addr
> 0x1FF9 && addr
< 0x2000)
450 NVRAM_PRINTF("%s: 0x%08x <= 0x%08x\n", __func__
, addr
, retval
);
455 void m48t59_set_addr (void *opaque
, uint32_t addr
)
457 m48t59_t
*NVRAM
= opaque
;
462 void m48t59_toggle_lock (void *opaque
, int lock
)
464 m48t59_t
*NVRAM
= opaque
;
466 NVRAM
->lock
^= 1 << lock
;
469 /* IO access to NVRAM */
470 static void NVRAM_writeb (void *opaque
, uint32_t addr
, uint32_t val
)
472 m48t59_t
*NVRAM
= opaque
;
474 addr
-= NVRAM
->io_base
;
475 NVRAM_PRINTF("%s: 0x%08x => 0x%08x\n", __func__
, addr
, val
);
478 NVRAM
->addr
&= ~0x00FF;
482 NVRAM
->addr
&= ~0xFF00;
483 NVRAM
->addr
|= val
<< 8;
486 m48t59_write(NVRAM
, val
, NVRAM
->addr
);
487 NVRAM
->addr
= 0x0000;
494 static uint32_t NVRAM_readb (void *opaque
, uint32_t addr
)
496 m48t59_t
*NVRAM
= opaque
;
499 addr
-= NVRAM
->io_base
;
502 retval
= m48t59_read(NVRAM
, NVRAM
->addr
);
508 NVRAM_PRINTF("%s: 0x%08x <= 0x%08x\n", __func__
, addr
, retval
);
513 static void nvram_writeb (void *opaque
, target_phys_addr_t addr
, uint32_t value
)
515 m48t59_t
*NVRAM
= opaque
;
517 addr
-= NVRAM
->mem_base
;
518 m48t59_write(NVRAM
, addr
, value
& 0xff);
521 static void nvram_writew (void *opaque
, target_phys_addr_t addr
, uint32_t value
)
523 m48t59_t
*NVRAM
= opaque
;
525 addr
-= NVRAM
->mem_base
;
526 m48t59_write(NVRAM
, addr
, (value
>> 8) & 0xff);
527 m48t59_write(NVRAM
, addr
+ 1, value
& 0xff);
530 static void nvram_writel (void *opaque
, target_phys_addr_t addr
, uint32_t value
)
532 m48t59_t
*NVRAM
= opaque
;
534 addr
-= NVRAM
->mem_base
;
535 m48t59_write(NVRAM
, addr
, (value
>> 24) & 0xff);
536 m48t59_write(NVRAM
, addr
+ 1, (value
>> 16) & 0xff);
537 m48t59_write(NVRAM
, addr
+ 2, (value
>> 8) & 0xff);
538 m48t59_write(NVRAM
, addr
+ 3, value
& 0xff);
541 static uint32_t nvram_readb (void *opaque
, target_phys_addr_t addr
)
543 m48t59_t
*NVRAM
= opaque
;
546 addr
-= NVRAM
->mem_base
;
547 retval
= m48t59_read(NVRAM
, addr
);
551 static uint32_t nvram_readw (void *opaque
, target_phys_addr_t addr
)
553 m48t59_t
*NVRAM
= opaque
;
556 addr
-= NVRAM
->mem_base
;
557 retval
= m48t59_read(NVRAM
, addr
) << 8;
558 retval
|= m48t59_read(NVRAM
, addr
+ 1);
562 static uint32_t nvram_readl (void *opaque
, target_phys_addr_t addr
)
564 m48t59_t
*NVRAM
= opaque
;
567 addr
-= NVRAM
->mem_base
;
568 retval
= m48t59_read(NVRAM
, addr
) << 24;
569 retval
|= m48t59_read(NVRAM
, addr
+ 1) << 16;
570 retval
|= m48t59_read(NVRAM
, addr
+ 2) << 8;
571 retval
|= m48t59_read(NVRAM
, addr
+ 3);
575 static CPUWriteMemoryFunc
*nvram_write
[] = {
581 static CPUReadMemoryFunc
*nvram_read
[] = {
587 static void m48t59_save(QEMUFile
*f
, void *opaque
)
589 m48t59_t
*s
= opaque
;
591 qemu_put_8s(f
, &s
->lock
);
592 qemu_put_be16s(f
, &s
->addr
);
593 qemu_put_buffer(f
, s
->buffer
, s
->size
);
596 static int m48t59_load(QEMUFile
*f
, void *opaque
, int version_id
)
598 m48t59_t
*s
= opaque
;
603 qemu_get_8s(f
, &s
->lock
);
604 qemu_get_be16s(f
, &s
->addr
);
605 qemu_get_buffer(f
, s
->buffer
, s
->size
);
610 static void m48t59_reset(void *opaque
)
612 m48t59_t
*NVRAM
= opaque
;
614 if (NVRAM
->alrm_timer
!= NULL
)
615 qemu_del_timer(NVRAM
->alrm_timer
);
617 if (NVRAM
->wd_timer
!= NULL
)
618 qemu_del_timer(NVRAM
->wd_timer
);
621 /* Initialisation routine */
622 m48t59_t
*m48t59_init (qemu_irq IRQ
, target_phys_addr_t mem_base
,
623 uint32_t io_base
, uint16_t size
,
627 target_phys_addr_t save_base
;
629 s
= qemu_mallocz(sizeof(m48t59_t
));
632 s
->buffer
= qemu_mallocz(size
);
639 s
->mem_base
= mem_base
;
640 s
->io_base
= io_base
;
644 register_ioport_read(io_base
, 0x04, 1, NVRAM_readb
, s
);
645 register_ioport_write(io_base
, 0x04, 1, NVRAM_writeb
, s
);
648 s
->mem_index
= cpu_register_io_memory(0, nvram_read
, nvram_write
, s
);
649 cpu_register_physical_memory(mem_base
, size
, s
->mem_index
);
652 s
->alrm_timer
= qemu_new_timer(vm_clock
, &alarm_cb
, s
);
653 s
->wd_timer
= qemu_new_timer(vm_clock
, &watchdog_cb
, s
);
656 qemu_get_timedate(&s
->alarm
, 0);
658 qemu_register_reset(m48t59_reset
, s
);
659 save_base
= mem_base
? mem_base
: io_base
;
660 register_savevm("m48t59", save_base
, 1, m48t59_save
, m48t59_load
, s
);