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
26 #include "qemu-timer.h"
32 #if defined(DEBUG_NVRAM)
33 #define NVRAM_PRINTF(fmt, ...) do { printf(fmt , ## __VA_ARGS__); } while (0)
35 #define NVRAM_PRINTF(fmt, ...) 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.
45 /* Model parameters */
46 int type
; // 2 = m48t02, 8 = m48t08, 59 = m48t59
47 /* Hardware parameters */
54 /* Alarm & watchdog */
56 struct QEMUTimer
*alrm_timer
;
57 struct QEMUTimer
*wd_timer
;
64 /* Fake timer functions */
65 /* Generic helpers for BCD */
66 static inline uint8_t toBCD (uint8_t value
)
68 return (((value
/ 10) % 10) << 4) | (value
% 10);
71 static inline uint8_t fromBCD (uint8_t BCD
)
73 return ((BCD
>> 4) * 10) + (BCD
& 0x0F);
76 /* Alarm management */
77 static void alarm_cb (void *opaque
)
81 m48t59_t
*NVRAM
= opaque
;
83 qemu_set_irq(NVRAM
->IRQ
, 1);
84 if ((NVRAM
->buffer
[0x1FF5] & 0x80) == 0 &&
85 (NVRAM
->buffer
[0x1FF4] & 0x80) == 0 &&
86 (NVRAM
->buffer
[0x1FF3] & 0x80) == 0 &&
87 (NVRAM
->buffer
[0x1FF2] & 0x80) == 0) {
88 /* Repeat once a month */
89 qemu_get_timedate(&tm
, NVRAM
->time_offset
);
91 if (tm
.tm_mon
== 13) {
95 next_time
= qemu_timedate_diff(&tm
) - NVRAM
->time_offset
;
96 } else if ((NVRAM
->buffer
[0x1FF5] & 0x80) != 0 &&
97 (NVRAM
->buffer
[0x1FF4] & 0x80) == 0 &&
98 (NVRAM
->buffer
[0x1FF3] & 0x80) == 0 &&
99 (NVRAM
->buffer
[0x1FF2] & 0x80) == 0) {
100 /* Repeat once a day */
101 next_time
= 24 * 60 * 60;
102 } else if ((NVRAM
->buffer
[0x1FF5] & 0x80) != 0 &&
103 (NVRAM
->buffer
[0x1FF4] & 0x80) != 0 &&
104 (NVRAM
->buffer
[0x1FF3] & 0x80) == 0 &&
105 (NVRAM
->buffer
[0x1FF2] & 0x80) == 0) {
106 /* Repeat once an hour */
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 minute */
115 /* Repeat once a second */
118 qemu_mod_timer(NVRAM
->alrm_timer
, qemu_get_clock(vm_clock
) +
120 qemu_set_irq(NVRAM
->IRQ
, 0);
123 static void set_alarm (m48t59_t
*NVRAM
)
126 if (NVRAM
->alrm_timer
!= NULL
) {
127 qemu_del_timer(NVRAM
->alrm_timer
);
128 diff
= qemu_timedate_diff(&NVRAM
->alarm
) - NVRAM
->time_offset
;
130 qemu_mod_timer(NVRAM
->alrm_timer
, diff
* 1000);
134 /* RTC management helpers */
135 static inline void get_time (m48t59_t
*NVRAM
, struct tm
*tm
)
137 qemu_get_timedate(tm
, NVRAM
->time_offset
);
140 static void set_time (m48t59_t
*NVRAM
, struct tm
*tm
)
142 NVRAM
->time_offset
= qemu_timedate_diff(tm
);
146 /* Watchdog management */
147 static void watchdog_cb (void *opaque
)
149 m48t59_t
*NVRAM
= opaque
;
151 NVRAM
->buffer
[0x1FF0] |= 0x80;
152 if (NVRAM
->buffer
[0x1FF7] & 0x80) {
153 NVRAM
->buffer
[0x1FF7] = 0x00;
154 NVRAM
->buffer
[0x1FFC] &= ~0x40;
155 /* May it be a hw CPU Reset instead ? */
156 qemu_system_reset_request();
158 qemu_set_irq(NVRAM
->IRQ
, 1);
159 qemu_set_irq(NVRAM
->IRQ
, 0);
163 static void set_up_watchdog (m48t59_t
*NVRAM
, uint8_t value
)
165 uint64_t interval
; /* in 1/16 seconds */
167 NVRAM
->buffer
[0x1FF0] &= ~0x80;
168 if (NVRAM
->wd_timer
!= NULL
) {
169 qemu_del_timer(NVRAM
->wd_timer
);
171 interval
= (1 << (2 * (value
& 0x03))) * ((value
>> 2) & 0x1F);
172 qemu_mod_timer(NVRAM
->wd_timer
, ((uint64_t)time(NULL
) * 1000) +
173 ((interval
* 1000) >> 4));
178 /* Direct access to NVRAM */
179 void m48t59_write (void *opaque
, uint32_t addr
, uint32_t val
)
181 m48t59_t
*NVRAM
= opaque
;
185 if (addr
> 0x1FF8 && addr
< 0x2000)
186 NVRAM_PRINTF("%s: 0x%08x => 0x%08x\n", __func__
, addr
, val
);
188 /* check for NVRAM access */
189 if ((NVRAM
->type
== 2 && addr
< 0x7f8) ||
190 (NVRAM
->type
== 8 && addr
< 0x1ff8) ||
191 (NVRAM
->type
== 59 && addr
< 0x1ff0))
197 /* flags register : read-only */
204 tmp
= fromBCD(val
& 0x7F);
205 if (tmp
>= 0 && tmp
<= 59) {
206 NVRAM
->alarm
.tm_sec
= tmp
;
207 NVRAM
->buffer
[0x1FF2] = val
;
213 tmp
= fromBCD(val
& 0x7F);
214 if (tmp
>= 0 && tmp
<= 59) {
215 NVRAM
->alarm
.tm_min
= tmp
;
216 NVRAM
->buffer
[0x1FF3] = val
;
222 tmp
= fromBCD(val
& 0x3F);
223 if (tmp
>= 0 && tmp
<= 23) {
224 NVRAM
->alarm
.tm_hour
= tmp
;
225 NVRAM
->buffer
[0x1FF4] = val
;
231 tmp
= fromBCD(val
& 0x1F);
233 NVRAM
->alarm
.tm_mday
= tmp
;
234 NVRAM
->buffer
[0x1FF5] = val
;
240 NVRAM
->buffer
[0x1FF6] = val
;
244 NVRAM
->buffer
[0x1FF7] = val
;
245 set_up_watchdog(NVRAM
, val
);
250 NVRAM
->buffer
[addr
] = (val
& ~0xA0) | 0x90;
255 tmp
= fromBCD(val
& 0x7F);
256 if (tmp
>= 0 && tmp
<= 59) {
257 get_time(NVRAM
, &tm
);
259 set_time(NVRAM
, &tm
);
261 if ((val
& 0x80) ^ (NVRAM
->buffer
[addr
] & 0x80)) {
263 NVRAM
->stop_time
= time(NULL
);
265 NVRAM
->time_offset
+= NVRAM
->stop_time
- time(NULL
);
266 NVRAM
->stop_time
= 0;
269 NVRAM
->buffer
[addr
] = val
& 0x80;
274 tmp
= fromBCD(val
& 0x7F);
275 if (tmp
>= 0 && tmp
<= 59) {
276 get_time(NVRAM
, &tm
);
278 set_time(NVRAM
, &tm
);
284 tmp
= fromBCD(val
& 0x3F);
285 if (tmp
>= 0 && tmp
<= 23) {
286 get_time(NVRAM
, &tm
);
288 set_time(NVRAM
, &tm
);
293 /* day of the week / century */
294 tmp
= fromBCD(val
& 0x07);
295 get_time(NVRAM
, &tm
);
297 set_time(NVRAM
, &tm
);
298 NVRAM
->buffer
[addr
] = val
& 0x40;
303 tmp
= fromBCD(val
& 0x1F);
305 get_time(NVRAM
, &tm
);
307 set_time(NVRAM
, &tm
);
313 tmp
= fromBCD(val
& 0x1F);
314 if (tmp
>= 1 && tmp
<= 12) {
315 get_time(NVRAM
, &tm
);
317 set_time(NVRAM
, &tm
);
324 if (tmp
>= 0 && tmp
<= 99) {
325 get_time(NVRAM
, &tm
);
326 if (NVRAM
->type
== 8)
327 tm
.tm_year
= fromBCD(val
) + 68; // Base year is 1968
329 tm
.tm_year
= fromBCD(val
);
330 set_time(NVRAM
, &tm
);
334 /* Check lock registers state */
335 if (addr
>= 0x20 && addr
<= 0x2F && (NVRAM
->lock
& 1))
337 if (addr
>= 0x30 && addr
<= 0x3F && (NVRAM
->lock
& 2))
340 if (addr
< NVRAM
->size
) {
341 NVRAM
->buffer
[addr
] = val
& 0xFF;
347 uint32_t m48t59_read (void *opaque
, uint32_t addr
)
349 m48t59_t
*NVRAM
= opaque
;
351 uint32_t retval
= 0xFF;
353 /* check for NVRAM access */
354 if ((NVRAM
->type
== 2 && addr
< 0x078f) ||
355 (NVRAM
->type
== 8 && addr
< 0x1ff8) ||
356 (NVRAM
->type
== 59 && addr
< 0x1ff0))
384 /* A read resets the watchdog */
385 set_up_watchdog(NVRAM
, NVRAM
->buffer
[0x1FF7]);
394 get_time(NVRAM
, &tm
);
395 retval
= (NVRAM
->buffer
[addr
] & 0x80) | toBCD(tm
.tm_sec
);
400 get_time(NVRAM
, &tm
);
401 retval
= toBCD(tm
.tm_min
);
406 get_time(NVRAM
, &tm
);
407 retval
= toBCD(tm
.tm_hour
);
411 /* day of the week / century */
412 get_time(NVRAM
, &tm
);
413 retval
= NVRAM
->buffer
[addr
] | tm
.tm_wday
;
418 get_time(NVRAM
, &tm
);
419 retval
= toBCD(tm
.tm_mday
);
424 get_time(NVRAM
, &tm
);
425 retval
= toBCD(tm
.tm_mon
+ 1);
430 get_time(NVRAM
, &tm
);
431 if (NVRAM
->type
== 8)
432 retval
= toBCD(tm
.tm_year
- 68); // Base year is 1968
434 retval
= toBCD(tm
.tm_year
);
437 /* Check lock registers state */
438 if (addr
>= 0x20 && addr
<= 0x2F && (NVRAM
->lock
& 1))
440 if (addr
>= 0x30 && addr
<= 0x3F && (NVRAM
->lock
& 2))
443 if (addr
< NVRAM
->size
) {
444 retval
= NVRAM
->buffer
[addr
];
448 if (addr
> 0x1FF9 && addr
< 0x2000)
449 NVRAM_PRINTF("%s: 0x%08x <= 0x%08x\n", __func__
, addr
, retval
);
454 void m48t59_set_addr (void *opaque
, uint32_t addr
)
456 m48t59_t
*NVRAM
= opaque
;
461 void m48t59_toggle_lock (void *opaque
, int lock
)
463 m48t59_t
*NVRAM
= opaque
;
465 NVRAM
->lock
^= 1 << lock
;
468 /* IO access to NVRAM */
469 static void NVRAM_writeb (void *opaque
, uint32_t addr
, uint32_t val
)
471 m48t59_t
*NVRAM
= opaque
;
473 addr
-= NVRAM
->io_base
;
474 NVRAM_PRINTF("%s: 0x%08x => 0x%08x\n", __func__
, addr
, val
);
477 NVRAM
->addr
&= ~0x00FF;
481 NVRAM
->addr
&= ~0xFF00;
482 NVRAM
->addr
|= val
<< 8;
485 m48t59_write(NVRAM
, val
, NVRAM
->addr
);
486 NVRAM
->addr
= 0x0000;
493 static uint32_t NVRAM_readb (void *opaque
, uint32_t addr
)
495 m48t59_t
*NVRAM
= opaque
;
498 addr
-= NVRAM
->io_base
;
501 retval
= m48t59_read(NVRAM
, NVRAM
->addr
);
507 NVRAM_PRINTF("%s: 0x%08x <= 0x%08x\n", __func__
, addr
, retval
);
512 static void nvram_writeb (void *opaque
, target_phys_addr_t addr
, uint32_t value
)
514 m48t59_t
*NVRAM
= opaque
;
516 m48t59_write(NVRAM
, addr
, value
& 0xff);
519 static void nvram_writew (void *opaque
, target_phys_addr_t addr
, uint32_t value
)
521 m48t59_t
*NVRAM
= opaque
;
523 m48t59_write(NVRAM
, addr
, (value
>> 8) & 0xff);
524 m48t59_write(NVRAM
, addr
+ 1, value
& 0xff);
527 static void nvram_writel (void *opaque
, target_phys_addr_t addr
, uint32_t value
)
529 m48t59_t
*NVRAM
= opaque
;
531 m48t59_write(NVRAM
, addr
, (value
>> 24) & 0xff);
532 m48t59_write(NVRAM
, addr
+ 1, (value
>> 16) & 0xff);
533 m48t59_write(NVRAM
, addr
+ 2, (value
>> 8) & 0xff);
534 m48t59_write(NVRAM
, addr
+ 3, value
& 0xff);
537 static uint32_t nvram_readb (void *opaque
, target_phys_addr_t addr
)
539 m48t59_t
*NVRAM
= opaque
;
542 retval
= m48t59_read(NVRAM
, addr
);
546 static uint32_t nvram_readw (void *opaque
, target_phys_addr_t addr
)
548 m48t59_t
*NVRAM
= opaque
;
551 retval
= m48t59_read(NVRAM
, addr
) << 8;
552 retval
|= m48t59_read(NVRAM
, addr
+ 1);
556 static uint32_t nvram_readl (void *opaque
, target_phys_addr_t addr
)
558 m48t59_t
*NVRAM
= opaque
;
561 retval
= m48t59_read(NVRAM
, addr
) << 24;
562 retval
|= m48t59_read(NVRAM
, addr
+ 1) << 16;
563 retval
|= m48t59_read(NVRAM
, addr
+ 2) << 8;
564 retval
|= m48t59_read(NVRAM
, addr
+ 3);
568 static CPUWriteMemoryFunc
*nvram_write
[] = {
574 static CPUReadMemoryFunc
*nvram_read
[] = {
580 static void m48t59_save(QEMUFile
*f
, void *opaque
)
582 m48t59_t
*s
= opaque
;
584 qemu_put_8s(f
, &s
->lock
);
585 qemu_put_be16s(f
, &s
->addr
);
586 qemu_put_buffer(f
, s
->buffer
, s
->size
);
589 static int m48t59_load(QEMUFile
*f
, void *opaque
, int version_id
)
591 m48t59_t
*s
= opaque
;
596 qemu_get_8s(f
, &s
->lock
);
597 qemu_get_be16s(f
, &s
->addr
);
598 qemu_get_buffer(f
, s
->buffer
, s
->size
);
603 static void m48t59_reset(void *opaque
)
605 m48t59_t
*NVRAM
= opaque
;
609 if (NVRAM
->alrm_timer
!= NULL
)
610 qemu_del_timer(NVRAM
->alrm_timer
);
612 if (NVRAM
->wd_timer
!= NULL
)
613 qemu_del_timer(NVRAM
->wd_timer
);
616 /* Initialisation routine */
617 m48t59_t
*m48t59_init (qemu_irq IRQ
, target_phys_addr_t mem_base
,
618 uint32_t io_base
, uint16_t size
,
625 dev
= qdev_create(NULL
, "m48t59");
626 qdev_set_prop_int(dev
, "type", type
);
627 qdev_set_prop_int(dev
, "size", size
);
628 qdev_set_prop_int(dev
, "io_base", io_base
);
630 s
= sysbus_from_qdev(dev
);
631 sysbus_connect_irq(s
, 0, IRQ
);
633 register_ioport_read(io_base
, 0x04, 1, NVRAM_readb
, s
);
634 register_ioport_write(io_base
, 0x04, 1, NVRAM_writeb
, s
);
637 sysbus_mmio_map(s
, 0, mem_base
);
640 d
= FROM_SYSBUS(m48t59_t
, s
);
645 static void m48t59_init1(SysBusDevice
*dev
)
647 m48t59_t
*s
= FROM_SYSBUS(m48t59_t
, dev
);
650 s
->size
= qdev_get_prop_int(&dev
->qdev
, "size", -1);
651 s
->buffer
= qemu_mallocz(s
->size
);
652 sysbus_init_irq(dev
, &s
->IRQ
);
653 s
->io_base
= qdev_get_prop_int(&dev
->qdev
, "io_base", 0);
654 s
->type
= qdev_get_prop_int(&dev
->qdev
, "type", -1);
656 mem_index
= cpu_register_io_memory(nvram_read
, nvram_write
, s
);
657 sysbus_init_mmio(dev
, s
->size
, mem_index
);
660 s
->alrm_timer
= qemu_new_timer(vm_clock
, &alarm_cb
, s
);
661 s
->wd_timer
= qemu_new_timer(vm_clock
, &watchdog_cb
, s
);
663 qemu_get_timedate(&s
->alarm
, 0);
665 qemu_register_reset(m48t59_reset
, s
);
666 register_savevm("m48t59", -1, 1, m48t59_save
, m48t59_load
, s
);
669 static void m48t59_register_devices(void)
671 sysbus_register_dev("m48t59", sizeof(m48t59_t
), m48t59_init1
);
674 device_init(m48t59_register_devices
)