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"
33 #if defined(DEBUG_NVRAM)
34 #define NVRAM_PRINTF(fmt, ...) do { printf(fmt , ## __VA_ARGS__); } while (0)
36 #define NVRAM_PRINTF(fmt, ...) do { } while (0)
40 * The M48T02, M48T08 and M48T59 chips are very similar. The newer '59 has
41 * alarm and a watchdog timer and related control registers. In the
42 * PPC platform there is also a nvram lock function.
45 /* Model parameters */
46 uint32_t type
; // 2 = m48t02, 8 = m48t08, 59 = m48t59
47 /* Hardware parameters */
54 /* Alarm & watchdog */
56 struct QEMUTimer
*alrm_timer
;
57 struct QEMUTimer
*wd_timer
;
64 typedef struct M48t59ISAState
{
69 typedef struct M48t59SysBusState
{
74 /* Fake timer functions */
75 /* Generic helpers for BCD */
76 static inline uint8_t toBCD (uint8_t value
)
78 return (((value
/ 10) % 10) << 4) | (value
% 10);
81 static inline uint8_t fromBCD (uint8_t BCD
)
83 return ((BCD
>> 4) * 10) + (BCD
& 0x0F);
86 /* Alarm management */
87 static void alarm_cb (void *opaque
)
91 m48t59_t
*NVRAM
= opaque
;
93 qemu_set_irq(NVRAM
->IRQ
, 1);
94 if ((NVRAM
->buffer
[0x1FF5] & 0x80) == 0 &&
95 (NVRAM
->buffer
[0x1FF4] & 0x80) == 0 &&
96 (NVRAM
->buffer
[0x1FF3] & 0x80) == 0 &&
97 (NVRAM
->buffer
[0x1FF2] & 0x80) == 0) {
98 /* Repeat once a month */
99 qemu_get_timedate(&tm
, NVRAM
->time_offset
);
101 if (tm
.tm_mon
== 13) {
105 next_time
= qemu_timedate_diff(&tm
) - NVRAM
->time_offset
;
106 } else if ((NVRAM
->buffer
[0x1FF5] & 0x80) != 0 &&
107 (NVRAM
->buffer
[0x1FF4] & 0x80) == 0 &&
108 (NVRAM
->buffer
[0x1FF3] & 0x80) == 0 &&
109 (NVRAM
->buffer
[0x1FF2] & 0x80) == 0) {
110 /* Repeat once a day */
111 next_time
= 24 * 60 * 60;
112 } else if ((NVRAM
->buffer
[0x1FF5] & 0x80) != 0 &&
113 (NVRAM
->buffer
[0x1FF4] & 0x80) != 0 &&
114 (NVRAM
->buffer
[0x1FF3] & 0x80) == 0 &&
115 (NVRAM
->buffer
[0x1FF2] & 0x80) == 0) {
116 /* Repeat once an hour */
118 } else if ((NVRAM
->buffer
[0x1FF5] & 0x80) != 0 &&
119 (NVRAM
->buffer
[0x1FF4] & 0x80) != 0 &&
120 (NVRAM
->buffer
[0x1FF3] & 0x80) != 0 &&
121 (NVRAM
->buffer
[0x1FF2] & 0x80) == 0) {
122 /* Repeat once a minute */
125 /* Repeat once a second */
128 qemu_mod_timer(NVRAM
->alrm_timer
, qemu_get_clock(vm_clock
) +
130 qemu_set_irq(NVRAM
->IRQ
, 0);
133 static void set_alarm (m48t59_t
*NVRAM
)
136 if (NVRAM
->alrm_timer
!= NULL
) {
137 qemu_del_timer(NVRAM
->alrm_timer
);
138 diff
= qemu_timedate_diff(&NVRAM
->alarm
) - NVRAM
->time_offset
;
140 qemu_mod_timer(NVRAM
->alrm_timer
, diff
* 1000);
144 /* RTC management helpers */
145 static inline void get_time (m48t59_t
*NVRAM
, struct tm
*tm
)
147 qemu_get_timedate(tm
, NVRAM
->time_offset
);
150 static void set_time (m48t59_t
*NVRAM
, struct tm
*tm
)
152 NVRAM
->time_offset
= qemu_timedate_diff(tm
);
156 /* Watchdog management */
157 static void watchdog_cb (void *opaque
)
159 m48t59_t
*NVRAM
= opaque
;
161 NVRAM
->buffer
[0x1FF0] |= 0x80;
162 if (NVRAM
->buffer
[0x1FF7] & 0x80) {
163 NVRAM
->buffer
[0x1FF7] = 0x00;
164 NVRAM
->buffer
[0x1FFC] &= ~0x40;
165 /* May it be a hw CPU Reset instead ? */
166 qemu_system_reset_request();
168 qemu_set_irq(NVRAM
->IRQ
, 1);
169 qemu_set_irq(NVRAM
->IRQ
, 0);
173 static void set_up_watchdog (m48t59_t
*NVRAM
, uint8_t value
)
175 uint64_t interval
; /* in 1/16 seconds */
177 NVRAM
->buffer
[0x1FF0] &= ~0x80;
178 if (NVRAM
->wd_timer
!= NULL
) {
179 qemu_del_timer(NVRAM
->wd_timer
);
181 interval
= (1 << (2 * (value
& 0x03))) * ((value
>> 2) & 0x1F);
182 qemu_mod_timer(NVRAM
->wd_timer
, ((uint64_t)time(NULL
) * 1000) +
183 ((interval
* 1000) >> 4));
188 /* Direct access to NVRAM */
189 void m48t59_write (void *opaque
, uint32_t addr
, uint32_t val
)
191 m48t59_t
*NVRAM
= opaque
;
195 if (addr
> 0x1FF8 && addr
< 0x2000)
196 NVRAM_PRINTF("%s: 0x%08x => 0x%08x\n", __func__
, addr
, val
);
198 /* check for NVRAM access */
199 if ((NVRAM
->type
== 2 && addr
< 0x7f8) ||
200 (NVRAM
->type
== 8 && addr
< 0x1ff8) ||
201 (NVRAM
->type
== 59 && addr
< 0x1ff0))
207 /* flags register : read-only */
214 tmp
= fromBCD(val
& 0x7F);
215 if (tmp
>= 0 && tmp
<= 59) {
216 NVRAM
->alarm
.tm_sec
= tmp
;
217 NVRAM
->buffer
[0x1FF2] = val
;
223 tmp
= fromBCD(val
& 0x7F);
224 if (tmp
>= 0 && tmp
<= 59) {
225 NVRAM
->alarm
.tm_min
= tmp
;
226 NVRAM
->buffer
[0x1FF3] = val
;
232 tmp
= fromBCD(val
& 0x3F);
233 if (tmp
>= 0 && tmp
<= 23) {
234 NVRAM
->alarm
.tm_hour
= tmp
;
235 NVRAM
->buffer
[0x1FF4] = val
;
241 tmp
= fromBCD(val
& 0x1F);
243 NVRAM
->alarm
.tm_mday
= tmp
;
244 NVRAM
->buffer
[0x1FF5] = val
;
250 NVRAM
->buffer
[0x1FF6] = val
;
254 NVRAM
->buffer
[0x1FF7] = val
;
255 set_up_watchdog(NVRAM
, val
);
260 NVRAM
->buffer
[addr
] = (val
& ~0xA0) | 0x90;
265 tmp
= fromBCD(val
& 0x7F);
266 if (tmp
>= 0 && tmp
<= 59) {
267 get_time(NVRAM
, &tm
);
269 set_time(NVRAM
, &tm
);
271 if ((val
& 0x80) ^ (NVRAM
->buffer
[addr
] & 0x80)) {
273 NVRAM
->stop_time
= time(NULL
);
275 NVRAM
->time_offset
+= NVRAM
->stop_time
- time(NULL
);
276 NVRAM
->stop_time
= 0;
279 NVRAM
->buffer
[addr
] = val
& 0x80;
284 tmp
= fromBCD(val
& 0x7F);
285 if (tmp
>= 0 && tmp
<= 59) {
286 get_time(NVRAM
, &tm
);
288 set_time(NVRAM
, &tm
);
294 tmp
= fromBCD(val
& 0x3F);
295 if (tmp
>= 0 && tmp
<= 23) {
296 get_time(NVRAM
, &tm
);
298 set_time(NVRAM
, &tm
);
303 /* day of the week / century */
304 tmp
= fromBCD(val
& 0x07);
305 get_time(NVRAM
, &tm
);
307 set_time(NVRAM
, &tm
);
308 NVRAM
->buffer
[addr
] = val
& 0x40;
313 tmp
= fromBCD(val
& 0x1F);
315 get_time(NVRAM
, &tm
);
317 set_time(NVRAM
, &tm
);
323 tmp
= fromBCD(val
& 0x1F);
324 if (tmp
>= 1 && tmp
<= 12) {
325 get_time(NVRAM
, &tm
);
327 set_time(NVRAM
, &tm
);
334 if (tmp
>= 0 && tmp
<= 99) {
335 get_time(NVRAM
, &tm
);
336 if (NVRAM
->type
== 8)
337 tm
.tm_year
= fromBCD(val
) + 68; // Base year is 1968
339 tm
.tm_year
= fromBCD(val
);
340 set_time(NVRAM
, &tm
);
344 /* Check lock registers state */
345 if (addr
>= 0x20 && addr
<= 0x2F && (NVRAM
->lock
& 1))
347 if (addr
>= 0x30 && addr
<= 0x3F && (NVRAM
->lock
& 2))
350 if (addr
< NVRAM
->size
) {
351 NVRAM
->buffer
[addr
] = val
& 0xFF;
357 uint32_t m48t59_read (void *opaque
, uint32_t addr
)
359 m48t59_t
*NVRAM
= opaque
;
361 uint32_t retval
= 0xFF;
363 /* check for NVRAM access */
364 if ((NVRAM
->type
== 2 && addr
< 0x078f) ||
365 (NVRAM
->type
== 8 && addr
< 0x1ff8) ||
366 (NVRAM
->type
== 59 && addr
< 0x1ff0))
394 /* A read resets the watchdog */
395 set_up_watchdog(NVRAM
, NVRAM
->buffer
[0x1FF7]);
404 get_time(NVRAM
, &tm
);
405 retval
= (NVRAM
->buffer
[addr
] & 0x80) | toBCD(tm
.tm_sec
);
410 get_time(NVRAM
, &tm
);
411 retval
= toBCD(tm
.tm_min
);
416 get_time(NVRAM
, &tm
);
417 retval
= toBCD(tm
.tm_hour
);
421 /* day of the week / century */
422 get_time(NVRAM
, &tm
);
423 retval
= NVRAM
->buffer
[addr
] | tm
.tm_wday
;
428 get_time(NVRAM
, &tm
);
429 retval
= toBCD(tm
.tm_mday
);
434 get_time(NVRAM
, &tm
);
435 retval
= toBCD(tm
.tm_mon
+ 1);
440 get_time(NVRAM
, &tm
);
441 if (NVRAM
->type
== 8)
442 retval
= toBCD(tm
.tm_year
- 68); // Base year is 1968
444 retval
= toBCD(tm
.tm_year
);
447 /* Check lock registers state */
448 if (addr
>= 0x20 && addr
<= 0x2F && (NVRAM
->lock
& 1))
450 if (addr
>= 0x30 && addr
<= 0x3F && (NVRAM
->lock
& 2))
453 if (addr
< NVRAM
->size
) {
454 retval
= NVRAM
->buffer
[addr
];
458 if (addr
> 0x1FF9 && addr
< 0x2000)
459 NVRAM_PRINTF("%s: 0x%08x <= 0x%08x\n", __func__
, addr
, retval
);
464 void m48t59_set_addr (void *opaque
, uint32_t addr
)
466 m48t59_t
*NVRAM
= opaque
;
471 void m48t59_toggle_lock (void *opaque
, int lock
)
473 m48t59_t
*NVRAM
= opaque
;
475 NVRAM
->lock
^= 1 << lock
;
478 /* IO access to NVRAM */
479 static void NVRAM_writeb (void *opaque
, uint32_t addr
, uint32_t val
)
481 m48t59_t
*NVRAM
= opaque
;
483 addr
-= NVRAM
->io_base
;
484 NVRAM_PRINTF("%s: 0x%08x => 0x%08x\n", __func__
, addr
, val
);
487 NVRAM
->addr
&= ~0x00FF;
491 NVRAM
->addr
&= ~0xFF00;
492 NVRAM
->addr
|= val
<< 8;
495 m48t59_write(NVRAM
, val
, NVRAM
->addr
);
496 NVRAM
->addr
= 0x0000;
503 static uint32_t NVRAM_readb (void *opaque
, uint32_t addr
)
505 m48t59_t
*NVRAM
= opaque
;
508 addr
-= NVRAM
->io_base
;
511 retval
= m48t59_read(NVRAM
, NVRAM
->addr
);
517 NVRAM_PRINTF("%s: 0x%08x <= 0x%08x\n", __func__
, addr
, retval
);
522 static void nvram_writeb (void *opaque
, target_phys_addr_t addr
, uint32_t value
)
524 m48t59_t
*NVRAM
= opaque
;
526 m48t59_write(NVRAM
, addr
, value
& 0xff);
529 static void nvram_writew (void *opaque
, target_phys_addr_t addr
, uint32_t value
)
531 m48t59_t
*NVRAM
= opaque
;
533 m48t59_write(NVRAM
, addr
, (value
>> 8) & 0xff);
534 m48t59_write(NVRAM
, addr
+ 1, value
& 0xff);
537 static void nvram_writel (void *opaque
, target_phys_addr_t addr
, uint32_t value
)
539 m48t59_t
*NVRAM
= opaque
;
541 m48t59_write(NVRAM
, addr
, (value
>> 24) & 0xff);
542 m48t59_write(NVRAM
, addr
+ 1, (value
>> 16) & 0xff);
543 m48t59_write(NVRAM
, addr
+ 2, (value
>> 8) & 0xff);
544 m48t59_write(NVRAM
, addr
+ 3, value
& 0xff);
547 static uint32_t nvram_readb (void *opaque
, target_phys_addr_t addr
)
549 m48t59_t
*NVRAM
= opaque
;
552 retval
= m48t59_read(NVRAM
, addr
);
556 static uint32_t nvram_readw (void *opaque
, target_phys_addr_t addr
)
558 m48t59_t
*NVRAM
= opaque
;
561 retval
= m48t59_read(NVRAM
, addr
) << 8;
562 retval
|= m48t59_read(NVRAM
, addr
+ 1);
566 static uint32_t nvram_readl (void *opaque
, target_phys_addr_t addr
)
568 m48t59_t
*NVRAM
= opaque
;
571 retval
= m48t59_read(NVRAM
, addr
) << 24;
572 retval
|= m48t59_read(NVRAM
, addr
+ 1) << 16;
573 retval
|= m48t59_read(NVRAM
, addr
+ 2) << 8;
574 retval
|= m48t59_read(NVRAM
, addr
+ 3);
578 static CPUWriteMemoryFunc
* const nvram_write
[] = {
584 static CPUReadMemoryFunc
* const nvram_read
[] = {
590 static void m48t59_save(QEMUFile
*f
, void *opaque
)
592 m48t59_t
*s
= opaque
;
594 qemu_put_8s(f
, &s
->lock
);
595 qemu_put_be16s(f
, &s
->addr
);
596 qemu_put_buffer(f
, s
->buffer
, s
->size
);
599 static int m48t59_load(QEMUFile
*f
, void *opaque
, int version_id
)
601 m48t59_t
*s
= opaque
;
606 qemu_get_8s(f
, &s
->lock
);
607 qemu_get_be16s(f
, &s
->addr
);
608 qemu_get_buffer(f
, s
->buffer
, s
->size
);
613 static void m48t59_reset(void *opaque
)
615 m48t59_t
*NVRAM
= opaque
;
619 if (NVRAM
->alrm_timer
!= NULL
)
620 qemu_del_timer(NVRAM
->alrm_timer
);
622 if (NVRAM
->wd_timer
!= NULL
)
623 qemu_del_timer(NVRAM
->wd_timer
);
626 /* Initialisation routine */
627 m48t59_t
*m48t59_init (qemu_irq IRQ
, target_phys_addr_t mem_base
,
628 uint32_t io_base
, uint16_t size
,
633 M48t59SysBusState
*d
;
635 dev
= qdev_create(NULL
, "m48t59");
636 qdev_prop_set_uint32(dev
, "type", type
);
637 qdev_prop_set_uint32(dev
, "size", size
);
638 qdev_prop_set_uint32(dev
, "io_base", io_base
);
640 s
= sysbus_from_qdev(dev
);
641 sysbus_connect_irq(s
, 0, IRQ
);
643 register_ioport_read(io_base
, 0x04, 1, NVRAM_readb
, s
);
644 register_ioport_write(io_base
, 0x04, 1, NVRAM_writeb
, s
);
647 sysbus_mmio_map(s
, 0, mem_base
);
650 d
= FROM_SYSBUS(M48t59SysBusState
, s
);
655 m48t59_t
*m48t59_init_isa(uint32_t io_base
, uint16_t size
, int type
)
661 dev
= isa_create("m48t59_isa");
662 qdev_prop_set_uint32(&dev
->qdev
, "type", type
);
663 qdev_prop_set_uint32(&dev
->qdev
, "size", size
);
664 qdev_prop_set_uint32(&dev
->qdev
, "io_base", io_base
);
665 qdev_init(&dev
->qdev
);
666 d
= DO_UPCAST(M48t59ISAState
, busdev
, dev
);
670 register_ioport_read(io_base
, 0x04, 1, NVRAM_readb
, s
);
671 register_ioport_write(io_base
, 0x04, 1, NVRAM_writeb
, s
);
677 static void m48t59_init_common(m48t59_t
*s
)
679 s
->buffer
= qemu_mallocz(s
->size
);
681 s
->alrm_timer
= qemu_new_timer(vm_clock
, &alarm_cb
, s
);
682 s
->wd_timer
= qemu_new_timer(vm_clock
, &watchdog_cb
, s
);
684 qemu_get_timedate(&s
->alarm
, 0);
686 qemu_register_reset(m48t59_reset
, s
);
687 register_savevm("m48t59", -1, 1, m48t59_save
, m48t59_load
, s
);
690 static int m48t59_init_isa1(ISADevice
*dev
)
692 M48t59ISAState
*d
= DO_UPCAST(M48t59ISAState
, busdev
, dev
);
693 m48t59_t
*s
= &d
->state
;
695 isa_init_irq(dev
, &s
->IRQ
, 8);
696 m48t59_init_common(s
);
701 static int m48t59_init1(SysBusDevice
*dev
)
703 M48t59SysBusState
*d
= FROM_SYSBUS(M48t59SysBusState
, dev
);
704 m48t59_t
*s
= &d
->state
;
707 sysbus_init_irq(dev
, &s
->IRQ
);
709 mem_index
= cpu_register_io_memory(nvram_read
, nvram_write
, s
);
710 sysbus_init_mmio(dev
, s
->size
, mem_index
);
711 m48t59_init_common(s
);
716 static ISADeviceInfo m48t59_isa_info
= {
717 .init
= m48t59_init_isa1
,
718 .qdev
.name
= "m48t59_isa",
719 .qdev
.size
= sizeof(M48t59ISAState
),
721 .qdev
.props
= (Property
[]) {
722 DEFINE_PROP_UINT32("size", M48t59ISAState
, state
.size
, -1),
723 DEFINE_PROP_UINT32("type", M48t59ISAState
, state
.type
, -1),
724 DEFINE_PROP_HEX32( "io_base", M48t59ISAState
, state
.io_base
, 0),
725 DEFINE_PROP_END_OF_LIST(),
729 static SysBusDeviceInfo m48t59_info
= {
730 .init
= m48t59_init1
,
731 .qdev
.name
= "m48t59",
732 .qdev
.size
= sizeof(M48t59SysBusState
),
733 .qdev
.props
= (Property
[]) {
734 DEFINE_PROP_UINT32("size", M48t59SysBusState
, state
.size
, -1),
735 DEFINE_PROP_UINT32("type", M48t59SysBusState
, state
.type
, -1),
736 DEFINE_PROP_HEX32( "io_base", M48t59SysBusState
, state
.io_base
, 0),
737 DEFINE_PROP_END_OF_LIST(),
741 static void m48t59_register_devices(void)
743 sysbus_register_withprop(&m48t59_info
);
744 isa_qdev_register(&m48t59_isa_info
);
747 device_init(m48t59_register_devices
)