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.
47 * http://www.st.com/stonline/products/literature/ds/2410/m48t02.pdf
48 * http://www.st.com/stonline/products/literature/ds/2411/m48t08.pdf
49 * http://www.st.com/stonline/products/literature/od/7001/m48t59y.pdf
53 /* Model parameters */
54 uint32_t type
; // 2 = m48t02, 8 = m48t08, 59 = m48t59
55 /* Hardware parameters */
62 /* Alarm & watchdog */
64 struct QEMUTimer
*alrm_timer
;
65 struct QEMUTimer
*wd_timer
;
72 typedef struct M48t59ISAState
{
77 typedef struct M48t59SysBusState
{
82 /* Fake timer functions */
84 /* Alarm management */
85 static void alarm_cb (void *opaque
)
89 M48t59State
*NVRAM
= opaque
;
91 qemu_set_irq(NVRAM
->IRQ
, 1);
92 if ((NVRAM
->buffer
[0x1FF5] & 0x80) == 0 &&
93 (NVRAM
->buffer
[0x1FF4] & 0x80) == 0 &&
94 (NVRAM
->buffer
[0x1FF3] & 0x80) == 0 &&
95 (NVRAM
->buffer
[0x1FF2] & 0x80) == 0) {
96 /* Repeat once a month */
97 qemu_get_timedate(&tm
, NVRAM
->time_offset
);
99 if (tm
.tm_mon
== 13) {
103 next_time
= qemu_timedate_diff(&tm
) - NVRAM
->time_offset
;
104 } else if ((NVRAM
->buffer
[0x1FF5] & 0x80) != 0 &&
105 (NVRAM
->buffer
[0x1FF4] & 0x80) == 0 &&
106 (NVRAM
->buffer
[0x1FF3] & 0x80) == 0 &&
107 (NVRAM
->buffer
[0x1FF2] & 0x80) == 0) {
108 /* Repeat once a day */
109 next_time
= 24 * 60 * 60;
110 } else if ((NVRAM
->buffer
[0x1FF5] & 0x80) != 0 &&
111 (NVRAM
->buffer
[0x1FF4] & 0x80) != 0 &&
112 (NVRAM
->buffer
[0x1FF3] & 0x80) == 0 &&
113 (NVRAM
->buffer
[0x1FF2] & 0x80) == 0) {
114 /* Repeat once an hour */
116 } else if ((NVRAM
->buffer
[0x1FF5] & 0x80) != 0 &&
117 (NVRAM
->buffer
[0x1FF4] & 0x80) != 0 &&
118 (NVRAM
->buffer
[0x1FF3] & 0x80) != 0 &&
119 (NVRAM
->buffer
[0x1FF2] & 0x80) == 0) {
120 /* Repeat once a minute */
123 /* Repeat once a second */
126 qemu_mod_timer(NVRAM
->alrm_timer
, qemu_get_clock(vm_clock
) +
128 qemu_set_irq(NVRAM
->IRQ
, 0);
131 static void set_alarm(M48t59State
*NVRAM
)
134 if (NVRAM
->alrm_timer
!= NULL
) {
135 qemu_del_timer(NVRAM
->alrm_timer
);
136 diff
= qemu_timedate_diff(&NVRAM
->alarm
) - NVRAM
->time_offset
;
138 qemu_mod_timer(NVRAM
->alrm_timer
, diff
* 1000);
142 /* RTC management helpers */
143 static inline void get_time(M48t59State
*NVRAM
, struct tm
*tm
)
145 qemu_get_timedate(tm
, NVRAM
->time_offset
);
148 static void set_time(M48t59State
*NVRAM
, struct tm
*tm
)
150 NVRAM
->time_offset
= qemu_timedate_diff(tm
);
154 /* Watchdog management */
155 static void watchdog_cb (void *opaque
)
157 M48t59State
*NVRAM
= opaque
;
159 NVRAM
->buffer
[0x1FF0] |= 0x80;
160 if (NVRAM
->buffer
[0x1FF7] & 0x80) {
161 NVRAM
->buffer
[0x1FF7] = 0x00;
162 NVRAM
->buffer
[0x1FFC] &= ~0x40;
163 /* May it be a hw CPU Reset instead ? */
164 qemu_system_reset_request();
166 qemu_set_irq(NVRAM
->IRQ
, 1);
167 qemu_set_irq(NVRAM
->IRQ
, 0);
171 static void set_up_watchdog(M48t59State
*NVRAM
, uint8_t value
)
173 uint64_t interval
; /* in 1/16 seconds */
175 NVRAM
->buffer
[0x1FF0] &= ~0x80;
176 if (NVRAM
->wd_timer
!= NULL
) {
177 qemu_del_timer(NVRAM
->wd_timer
);
179 interval
= (1 << (2 * (value
& 0x03))) * ((value
>> 2) & 0x1F);
180 qemu_mod_timer(NVRAM
->wd_timer
, ((uint64_t)time(NULL
) * 1000) +
181 ((interval
* 1000) >> 4));
186 /* Direct access to NVRAM */
187 void m48t59_write (void *opaque
, uint32_t addr
, uint32_t val
)
189 M48t59State
*NVRAM
= opaque
;
193 if (addr
> 0x1FF8 && addr
< 0x2000)
194 NVRAM_PRINTF("%s: 0x%08x => 0x%08x\n", __func__
, addr
, val
);
196 /* check for NVRAM access */
197 if ((NVRAM
->type
== 2 && addr
< 0x7f8) ||
198 (NVRAM
->type
== 8 && addr
< 0x1ff8) ||
199 (NVRAM
->type
== 59 && addr
< 0x1ff0))
205 /* flags register : read-only */
212 tmp
= from_bcd(val
& 0x7F);
213 if (tmp
>= 0 && tmp
<= 59) {
214 NVRAM
->alarm
.tm_sec
= tmp
;
215 NVRAM
->buffer
[0x1FF2] = val
;
221 tmp
= from_bcd(val
& 0x7F);
222 if (tmp
>= 0 && tmp
<= 59) {
223 NVRAM
->alarm
.tm_min
= tmp
;
224 NVRAM
->buffer
[0x1FF3] = val
;
230 tmp
= from_bcd(val
& 0x3F);
231 if (tmp
>= 0 && tmp
<= 23) {
232 NVRAM
->alarm
.tm_hour
= tmp
;
233 NVRAM
->buffer
[0x1FF4] = val
;
239 tmp
= from_bcd(val
& 0x1F);
241 NVRAM
->alarm
.tm_mday
= tmp
;
242 NVRAM
->buffer
[0x1FF5] = val
;
248 NVRAM
->buffer
[0x1FF6] = val
;
252 NVRAM
->buffer
[0x1FF7] = val
;
253 set_up_watchdog(NVRAM
, val
);
258 NVRAM
->buffer
[addr
] = (val
& ~0xA0) | 0x90;
263 tmp
= from_bcd(val
& 0x7F);
264 if (tmp
>= 0 && tmp
<= 59) {
265 get_time(NVRAM
, &tm
);
267 set_time(NVRAM
, &tm
);
269 if ((val
& 0x80) ^ (NVRAM
->buffer
[addr
] & 0x80)) {
271 NVRAM
->stop_time
= time(NULL
);
273 NVRAM
->time_offset
+= NVRAM
->stop_time
- time(NULL
);
274 NVRAM
->stop_time
= 0;
277 NVRAM
->buffer
[addr
] = val
& 0x80;
282 tmp
= from_bcd(val
& 0x7F);
283 if (tmp
>= 0 && tmp
<= 59) {
284 get_time(NVRAM
, &tm
);
286 set_time(NVRAM
, &tm
);
292 tmp
= from_bcd(val
& 0x3F);
293 if (tmp
>= 0 && tmp
<= 23) {
294 get_time(NVRAM
, &tm
);
296 set_time(NVRAM
, &tm
);
301 /* day of the week / century */
302 tmp
= from_bcd(val
& 0x07);
303 get_time(NVRAM
, &tm
);
305 set_time(NVRAM
, &tm
);
306 NVRAM
->buffer
[addr
] = val
& 0x40;
311 tmp
= from_bcd(val
& 0x1F);
313 get_time(NVRAM
, &tm
);
315 set_time(NVRAM
, &tm
);
321 tmp
= from_bcd(val
& 0x1F);
322 if (tmp
>= 1 && tmp
<= 12) {
323 get_time(NVRAM
, &tm
);
325 set_time(NVRAM
, &tm
);
332 if (tmp
>= 0 && tmp
<= 99) {
333 get_time(NVRAM
, &tm
);
334 if (NVRAM
->type
== 8)
335 tm
.tm_year
= from_bcd(val
) + 68; // Base year is 1968
337 tm
.tm_year
= from_bcd(val
);
338 set_time(NVRAM
, &tm
);
342 /* Check lock registers state */
343 if (addr
>= 0x20 && addr
<= 0x2F && (NVRAM
->lock
& 1))
345 if (addr
>= 0x30 && addr
<= 0x3F && (NVRAM
->lock
& 2))
348 if (addr
< NVRAM
->size
) {
349 NVRAM
->buffer
[addr
] = val
& 0xFF;
355 uint32_t m48t59_read (void *opaque
, uint32_t addr
)
357 M48t59State
*NVRAM
= opaque
;
359 uint32_t retval
= 0xFF;
361 /* check for NVRAM access */
362 if ((NVRAM
->type
== 2 && addr
< 0x078f) ||
363 (NVRAM
->type
== 8 && addr
< 0x1ff8) ||
364 (NVRAM
->type
== 59 && addr
< 0x1ff0))
392 /* A read resets the watchdog */
393 set_up_watchdog(NVRAM
, NVRAM
->buffer
[0x1FF7]);
402 get_time(NVRAM
, &tm
);
403 retval
= (NVRAM
->buffer
[addr
] & 0x80) | to_bcd(tm
.tm_sec
);
408 get_time(NVRAM
, &tm
);
409 retval
= to_bcd(tm
.tm_min
);
414 get_time(NVRAM
, &tm
);
415 retval
= to_bcd(tm
.tm_hour
);
419 /* day of the week / century */
420 get_time(NVRAM
, &tm
);
421 retval
= NVRAM
->buffer
[addr
] | tm
.tm_wday
;
426 get_time(NVRAM
, &tm
);
427 retval
= to_bcd(tm
.tm_mday
);
432 get_time(NVRAM
, &tm
);
433 retval
= to_bcd(tm
.tm_mon
+ 1);
438 get_time(NVRAM
, &tm
);
439 if (NVRAM
->type
== 8)
440 retval
= to_bcd(tm
.tm_year
- 68); // Base year is 1968
442 retval
= to_bcd(tm
.tm_year
);
445 /* Check lock registers state */
446 if (addr
>= 0x20 && addr
<= 0x2F && (NVRAM
->lock
& 1))
448 if (addr
>= 0x30 && addr
<= 0x3F && (NVRAM
->lock
& 2))
451 if (addr
< NVRAM
->size
) {
452 retval
= NVRAM
->buffer
[addr
];
456 if (addr
> 0x1FF9 && addr
< 0x2000)
457 NVRAM_PRINTF("%s: 0x%08x <= 0x%08x\n", __func__
, addr
, retval
);
462 void m48t59_set_addr (void *opaque
, uint32_t addr
)
464 M48t59State
*NVRAM
= opaque
;
469 void m48t59_toggle_lock (void *opaque
, int lock
)
471 M48t59State
*NVRAM
= opaque
;
473 NVRAM
->lock
^= 1 << lock
;
476 /* IO access to NVRAM */
477 static void NVRAM_writeb (void *opaque
, uint32_t addr
, uint32_t val
)
479 M48t59State
*NVRAM
= opaque
;
481 addr
-= NVRAM
->io_base
;
482 NVRAM_PRINTF("%s: 0x%08x => 0x%08x\n", __func__
, addr
, val
);
485 NVRAM
->addr
&= ~0x00FF;
489 NVRAM
->addr
&= ~0xFF00;
490 NVRAM
->addr
|= val
<< 8;
493 m48t59_write(NVRAM
, val
, NVRAM
->addr
);
494 NVRAM
->addr
= 0x0000;
501 static uint32_t NVRAM_readb (void *opaque
, uint32_t addr
)
503 M48t59State
*NVRAM
= opaque
;
506 addr
-= NVRAM
->io_base
;
509 retval
= m48t59_read(NVRAM
, NVRAM
->addr
);
515 NVRAM_PRINTF("%s: 0x%08x <= 0x%08x\n", __func__
, addr
, retval
);
520 static void nvram_writeb (void *opaque
, target_phys_addr_t addr
, uint32_t value
)
522 M48t59State
*NVRAM
= opaque
;
524 m48t59_write(NVRAM
, addr
, value
& 0xff);
527 static void nvram_writew (void *opaque
, target_phys_addr_t addr
, uint32_t value
)
529 M48t59State
*NVRAM
= opaque
;
531 m48t59_write(NVRAM
, addr
, (value
>> 8) & 0xff);
532 m48t59_write(NVRAM
, addr
+ 1, value
& 0xff);
535 static void nvram_writel (void *opaque
, target_phys_addr_t addr
, uint32_t value
)
537 M48t59State
*NVRAM
= opaque
;
539 m48t59_write(NVRAM
, addr
, (value
>> 24) & 0xff);
540 m48t59_write(NVRAM
, addr
+ 1, (value
>> 16) & 0xff);
541 m48t59_write(NVRAM
, addr
+ 2, (value
>> 8) & 0xff);
542 m48t59_write(NVRAM
, addr
+ 3, value
& 0xff);
545 static uint32_t nvram_readb (void *opaque
, target_phys_addr_t addr
)
547 M48t59State
*NVRAM
= opaque
;
550 retval
= m48t59_read(NVRAM
, addr
);
554 static uint32_t nvram_readw (void *opaque
, target_phys_addr_t addr
)
556 M48t59State
*NVRAM
= opaque
;
559 retval
= m48t59_read(NVRAM
, addr
) << 8;
560 retval
|= m48t59_read(NVRAM
, addr
+ 1);
564 static uint32_t nvram_readl (void *opaque
, target_phys_addr_t addr
)
566 M48t59State
*NVRAM
= opaque
;
569 retval
= m48t59_read(NVRAM
, addr
) << 24;
570 retval
|= m48t59_read(NVRAM
, addr
+ 1) << 16;
571 retval
|= m48t59_read(NVRAM
, addr
+ 2) << 8;
572 retval
|= m48t59_read(NVRAM
, addr
+ 3);
576 static CPUWriteMemoryFunc
* const nvram_write
[] = {
582 static CPUReadMemoryFunc
* const nvram_read
[] = {
588 static void m48t59_save(QEMUFile
*f
, void *opaque
)
590 M48t59State
*s
= opaque
;
592 qemu_put_8s(f
, &s
->lock
);
593 qemu_put_be16s(f
, &s
->addr
);
594 qemu_put_buffer(f
, s
->buffer
, s
->size
);
597 static int m48t59_load(QEMUFile
*f
, void *opaque
, int version_id
)
599 M48t59State
*s
= opaque
;
604 qemu_get_8s(f
, &s
->lock
);
605 qemu_get_be16s(f
, &s
->addr
);
606 qemu_get_buffer(f
, s
->buffer
, s
->size
);
611 static void m48t59_reset_common(M48t59State
*NVRAM
)
615 if (NVRAM
->alrm_timer
!= NULL
)
616 qemu_del_timer(NVRAM
->alrm_timer
);
618 if (NVRAM
->wd_timer
!= NULL
)
619 qemu_del_timer(NVRAM
->wd_timer
);
622 static void m48t59_reset_isa(DeviceState
*d
)
624 M48t59ISAState
*isa
= container_of(d
, M48t59ISAState
, busdev
.qdev
);
625 M48t59State
*NVRAM
= &isa
->state
;
627 m48t59_reset_common(NVRAM
);
630 static void m48t59_reset_sysbus(DeviceState
*d
)
632 M48t59SysBusState
*sys
= container_of(d
, M48t59SysBusState
, busdev
.qdev
);
633 M48t59State
*NVRAM
= &sys
->state
;
635 m48t59_reset_common(NVRAM
);
638 /* Initialisation routine */
639 M48t59State
*m48t59_init(qemu_irq IRQ
, target_phys_addr_t mem_base
,
640 uint32_t io_base
, uint16_t size
, int type
)
644 M48t59SysBusState
*d
;
646 dev
= qdev_create(NULL
, "m48t59");
647 qdev_prop_set_uint32(dev
, "type", type
);
648 qdev_prop_set_uint32(dev
, "size", size
);
649 qdev_prop_set_uint32(dev
, "io_base", io_base
);
650 qdev_init_nofail(dev
);
651 s
= sysbus_from_qdev(dev
);
652 sysbus_connect_irq(s
, 0, IRQ
);
654 register_ioport_read(io_base
, 0x04, 1, NVRAM_readb
, s
);
655 register_ioport_write(io_base
, 0x04, 1, NVRAM_writeb
, s
);
658 sysbus_mmio_map(s
, 0, mem_base
);
661 d
= FROM_SYSBUS(M48t59SysBusState
, s
);
666 M48t59State
*m48t59_init_isa(uint32_t io_base
, uint16_t size
, int type
)
672 dev
= isa_create("m48t59_isa");
673 qdev_prop_set_uint32(&dev
->qdev
, "type", type
);
674 qdev_prop_set_uint32(&dev
->qdev
, "size", size
);
675 qdev_prop_set_uint32(&dev
->qdev
, "io_base", io_base
);
676 qdev_init_nofail(&dev
->qdev
);
677 d
= DO_UPCAST(M48t59ISAState
, busdev
, dev
);
681 register_ioport_read(io_base
, 0x04, 1, NVRAM_readb
, s
);
682 register_ioport_write(io_base
, 0x04, 1, NVRAM_writeb
, s
);
683 isa_init_ioport_range(dev
, io_base
, 4);
689 static void m48t59_init_common(M48t59State
*s
)
691 s
->buffer
= qemu_mallocz(s
->size
);
693 s
->alrm_timer
= qemu_new_timer(vm_clock
, &alarm_cb
, s
);
694 s
->wd_timer
= qemu_new_timer(vm_clock
, &watchdog_cb
, s
);
696 qemu_get_timedate(&s
->alarm
, 0);
698 register_savevm(NULL
, "m48t59", -1, 1, m48t59_save
, m48t59_load
, s
);
701 static int m48t59_init_isa1(ISADevice
*dev
)
703 M48t59ISAState
*d
= DO_UPCAST(M48t59ISAState
, busdev
, dev
);
704 M48t59State
*s
= &d
->state
;
706 isa_init_irq(dev
, &s
->IRQ
, 8);
707 m48t59_init_common(s
);
712 static int m48t59_init1(SysBusDevice
*dev
)
714 M48t59SysBusState
*d
= FROM_SYSBUS(M48t59SysBusState
, dev
);
715 M48t59State
*s
= &d
->state
;
718 sysbus_init_irq(dev
, &s
->IRQ
);
720 mem_index
= cpu_register_io_memory(nvram_read
, nvram_write
, s
,
721 DEVICE_NATIVE_ENDIAN
);
722 sysbus_init_mmio(dev
, s
->size
, mem_index
);
723 m48t59_init_common(s
);
728 static ISADeviceInfo m48t59_isa_info
= {
729 .init
= m48t59_init_isa1
,
730 .qdev
.name
= "m48t59_isa",
731 .qdev
.size
= sizeof(M48t59ISAState
),
732 .qdev
.reset
= m48t59_reset_isa
,
734 .qdev
.props
= (Property
[]) {
735 DEFINE_PROP_UINT32("size", M48t59ISAState
, state
.size
, -1),
736 DEFINE_PROP_UINT32("type", M48t59ISAState
, state
.type
, -1),
737 DEFINE_PROP_HEX32( "io_base", M48t59ISAState
, state
.io_base
, 0),
738 DEFINE_PROP_END_OF_LIST(),
742 static SysBusDeviceInfo m48t59_info
= {
743 .init
= m48t59_init1
,
744 .qdev
.name
= "m48t59",
745 .qdev
.size
= sizeof(M48t59SysBusState
),
746 .qdev
.reset
= m48t59_reset_sysbus
,
747 .qdev
.props
= (Property
[]) {
748 DEFINE_PROP_UINT32("size", M48t59SysBusState
, state
.size
, -1),
749 DEFINE_PROP_UINT32("type", M48t59SysBusState
, state
.type
, -1),
750 DEFINE_PROP_HEX32( "io_base", M48t59SysBusState
, state
.io_base
, 0),
751 DEFINE_PROP_END_OF_LIST(),
755 static void m48t59_register_devices(void)
757 sysbus_register_withprop(&m48t59_info
);
758 isa_qdev_register(&m48t59_isa_info
);
761 device_init(m48t59_register_devices
)