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 /* Hardware parameters */
60 /* Alarm & watchdog */
62 struct QEMUTimer
*alrm_timer
;
63 struct QEMUTimer
*wd_timer
;
66 /* Model parameters */
67 uint32_t type
; /* 2 = m48t02, 8 = m48t08, 59 = m48t59 */
73 typedef struct M48t59ISAState
{
78 typedef struct M48t59SysBusState
{
83 /* Fake timer functions */
85 /* Alarm management */
86 static void alarm_cb (void *opaque
)
90 M48t59State
*NVRAM
= opaque
;
92 qemu_set_irq(NVRAM
->IRQ
, 1);
93 if ((NVRAM
->buffer
[0x1FF5] & 0x80) == 0 &&
94 (NVRAM
->buffer
[0x1FF4] & 0x80) == 0 &&
95 (NVRAM
->buffer
[0x1FF3] & 0x80) == 0 &&
96 (NVRAM
->buffer
[0x1FF2] & 0x80) == 0) {
97 /* Repeat once a month */
98 qemu_get_timedate(&tm
, NVRAM
->time_offset
);
100 if (tm
.tm_mon
== 13) {
104 next_time
= qemu_timedate_diff(&tm
) - NVRAM
->time_offset
;
105 } else if ((NVRAM
->buffer
[0x1FF5] & 0x80) != 0 &&
106 (NVRAM
->buffer
[0x1FF4] & 0x80) == 0 &&
107 (NVRAM
->buffer
[0x1FF3] & 0x80) == 0 &&
108 (NVRAM
->buffer
[0x1FF2] & 0x80) == 0) {
109 /* Repeat once a day */
110 next_time
= 24 * 60 * 60;
111 } else if ((NVRAM
->buffer
[0x1FF5] & 0x80) != 0 &&
112 (NVRAM
->buffer
[0x1FF4] & 0x80) != 0 &&
113 (NVRAM
->buffer
[0x1FF3] & 0x80) == 0 &&
114 (NVRAM
->buffer
[0x1FF2] & 0x80) == 0) {
115 /* Repeat once an hour */
117 } else if ((NVRAM
->buffer
[0x1FF5] & 0x80) != 0 &&
118 (NVRAM
->buffer
[0x1FF4] & 0x80) != 0 &&
119 (NVRAM
->buffer
[0x1FF3] & 0x80) != 0 &&
120 (NVRAM
->buffer
[0x1FF2] & 0x80) == 0) {
121 /* Repeat once a minute */
124 /* Repeat once a second */
127 qemu_mod_timer(NVRAM
->alrm_timer
, qemu_get_clock_ns(vm_clock
) +
129 qemu_set_irq(NVRAM
->IRQ
, 0);
132 static void set_alarm(M48t59State
*NVRAM
)
135 if (NVRAM
->alrm_timer
!= NULL
) {
136 qemu_del_timer(NVRAM
->alrm_timer
);
137 diff
= qemu_timedate_diff(&NVRAM
->alarm
) - NVRAM
->time_offset
;
139 qemu_mod_timer(NVRAM
->alrm_timer
, diff
* 1000);
143 /* RTC management helpers */
144 static inline void get_time(M48t59State
*NVRAM
, struct tm
*tm
)
146 qemu_get_timedate(tm
, NVRAM
->time_offset
);
149 static void set_time(M48t59State
*NVRAM
, struct tm
*tm
)
151 NVRAM
->time_offset
= qemu_timedate_diff(tm
);
155 /* Watchdog management */
156 static void watchdog_cb (void *opaque
)
158 M48t59State
*NVRAM
= opaque
;
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 qemu_system_reset_request();
167 qemu_set_irq(NVRAM
->IRQ
, 1);
168 qemu_set_irq(NVRAM
->IRQ
, 0);
172 static void set_up_watchdog(M48t59State
*NVRAM
, uint8_t value
)
174 uint64_t interval
; /* in 1/16 seconds */
176 NVRAM
->buffer
[0x1FF0] &= ~0x80;
177 if (NVRAM
->wd_timer
!= NULL
) {
178 qemu_del_timer(NVRAM
->wd_timer
);
180 interval
= (1 << (2 * (value
& 0x03))) * ((value
>> 2) & 0x1F);
181 qemu_mod_timer(NVRAM
->wd_timer
, ((uint64_t)time(NULL
) * 1000) +
182 ((interval
* 1000) >> 4));
187 /* Direct access to NVRAM */
188 void m48t59_write (void *opaque
, uint32_t addr
, uint32_t val
)
190 M48t59State
*NVRAM
= opaque
;
194 if (addr
> 0x1FF8 && addr
< 0x2000)
195 NVRAM_PRINTF("%s: 0x%08x => 0x%08x\n", __func__
, addr
, val
);
197 /* check for NVRAM access */
198 if ((NVRAM
->type
== 2 && addr
< 0x7f8) ||
199 (NVRAM
->type
== 8 && addr
< 0x1ff8) ||
200 (NVRAM
->type
== 59 && addr
< 0x1ff0))
206 /* flags register : read-only */
213 tmp
= from_bcd(val
& 0x7F);
214 if (tmp
>= 0 && tmp
<= 59) {
215 NVRAM
->alarm
.tm_sec
= tmp
;
216 NVRAM
->buffer
[0x1FF2] = val
;
222 tmp
= from_bcd(val
& 0x7F);
223 if (tmp
>= 0 && tmp
<= 59) {
224 NVRAM
->alarm
.tm_min
= tmp
;
225 NVRAM
->buffer
[0x1FF3] = val
;
231 tmp
= from_bcd(val
& 0x3F);
232 if (tmp
>= 0 && tmp
<= 23) {
233 NVRAM
->alarm
.tm_hour
= tmp
;
234 NVRAM
->buffer
[0x1FF4] = val
;
240 tmp
= from_bcd(val
& 0x1F);
242 NVRAM
->alarm
.tm_mday
= tmp
;
243 NVRAM
->buffer
[0x1FF5] = val
;
249 NVRAM
->buffer
[0x1FF6] = val
;
253 NVRAM
->buffer
[0x1FF7] = val
;
254 set_up_watchdog(NVRAM
, val
);
259 NVRAM
->buffer
[addr
] = (val
& ~0xA0) | 0x90;
264 tmp
= from_bcd(val
& 0x7F);
265 if (tmp
>= 0 && tmp
<= 59) {
266 get_time(NVRAM
, &tm
);
268 set_time(NVRAM
, &tm
);
270 if ((val
& 0x80) ^ (NVRAM
->buffer
[addr
] & 0x80)) {
272 NVRAM
->stop_time
= time(NULL
);
274 NVRAM
->time_offset
+= NVRAM
->stop_time
- time(NULL
);
275 NVRAM
->stop_time
= 0;
278 NVRAM
->buffer
[addr
] = val
& 0x80;
283 tmp
= from_bcd(val
& 0x7F);
284 if (tmp
>= 0 && tmp
<= 59) {
285 get_time(NVRAM
, &tm
);
287 set_time(NVRAM
, &tm
);
293 tmp
= from_bcd(val
& 0x3F);
294 if (tmp
>= 0 && tmp
<= 23) {
295 get_time(NVRAM
, &tm
);
297 set_time(NVRAM
, &tm
);
302 /* day of the week / century */
303 tmp
= from_bcd(val
& 0x07);
304 get_time(NVRAM
, &tm
);
306 set_time(NVRAM
, &tm
);
307 NVRAM
->buffer
[addr
] = val
& 0x40;
312 tmp
= from_bcd(val
& 0x1F);
314 get_time(NVRAM
, &tm
);
316 set_time(NVRAM
, &tm
);
322 tmp
= from_bcd(val
& 0x1F);
323 if (tmp
>= 1 && tmp
<= 12) {
324 get_time(NVRAM
, &tm
);
326 set_time(NVRAM
, &tm
);
333 if (tmp
>= 0 && tmp
<= 99) {
334 get_time(NVRAM
, &tm
);
335 if (NVRAM
->type
== 8)
336 tm
.tm_year
= from_bcd(val
) + 68; // Base year is 1968
338 tm
.tm_year
= from_bcd(val
);
339 set_time(NVRAM
, &tm
);
343 /* Check lock registers state */
344 if (addr
>= 0x20 && addr
<= 0x2F && (NVRAM
->lock
& 1))
346 if (addr
>= 0x30 && addr
<= 0x3F && (NVRAM
->lock
& 2))
349 if (addr
< NVRAM
->size
) {
350 NVRAM
->buffer
[addr
] = val
& 0xFF;
356 uint32_t m48t59_read (void *opaque
, uint32_t addr
)
358 M48t59State
*NVRAM
= opaque
;
360 uint32_t retval
= 0xFF;
362 /* check for NVRAM access */
363 if ((NVRAM
->type
== 2 && addr
< 0x078f) ||
364 (NVRAM
->type
== 8 && addr
< 0x1ff8) ||
365 (NVRAM
->type
== 59 && addr
< 0x1ff0))
393 /* A read resets the watchdog */
394 set_up_watchdog(NVRAM
, NVRAM
->buffer
[0x1FF7]);
403 get_time(NVRAM
, &tm
);
404 retval
= (NVRAM
->buffer
[addr
] & 0x80) | to_bcd(tm
.tm_sec
);
409 get_time(NVRAM
, &tm
);
410 retval
= to_bcd(tm
.tm_min
);
415 get_time(NVRAM
, &tm
);
416 retval
= to_bcd(tm
.tm_hour
);
420 /* day of the week / century */
421 get_time(NVRAM
, &tm
);
422 retval
= NVRAM
->buffer
[addr
] | tm
.tm_wday
;
427 get_time(NVRAM
, &tm
);
428 retval
= to_bcd(tm
.tm_mday
);
433 get_time(NVRAM
, &tm
);
434 retval
= to_bcd(tm
.tm_mon
+ 1);
439 get_time(NVRAM
, &tm
);
440 if (NVRAM
->type
== 8)
441 retval
= to_bcd(tm
.tm_year
- 68); // Base year is 1968
443 retval
= to_bcd(tm
.tm_year
);
446 /* Check lock registers state */
447 if (addr
>= 0x20 && addr
<= 0x2F && (NVRAM
->lock
& 1))
449 if (addr
>= 0x30 && addr
<= 0x3F && (NVRAM
->lock
& 2))
452 if (addr
< NVRAM
->size
) {
453 retval
= NVRAM
->buffer
[addr
];
457 if (addr
> 0x1FF9 && addr
< 0x2000)
458 NVRAM_PRINTF("%s: 0x%08x <= 0x%08x\n", __func__
, addr
, retval
);
463 void m48t59_set_addr (void *opaque
, uint32_t addr
)
465 M48t59State
*NVRAM
= opaque
;
470 void m48t59_toggle_lock (void *opaque
, int lock
)
472 M48t59State
*NVRAM
= opaque
;
474 NVRAM
->lock
^= 1 << lock
;
477 /* IO access to NVRAM */
478 static void NVRAM_writeb (void *opaque
, uint32_t addr
, uint32_t val
)
480 M48t59State
*NVRAM
= opaque
;
482 addr
-= NVRAM
->io_base
;
483 NVRAM_PRINTF("%s: 0x%08x => 0x%08x\n", __func__
, addr
, val
);
486 NVRAM
->addr
&= ~0x00FF;
490 NVRAM
->addr
&= ~0xFF00;
491 NVRAM
->addr
|= val
<< 8;
494 m48t59_write(NVRAM
, val
, NVRAM
->addr
);
495 NVRAM
->addr
= 0x0000;
502 static uint32_t NVRAM_readb (void *opaque
, uint32_t addr
)
504 M48t59State
*NVRAM
= opaque
;
507 addr
-= NVRAM
->io_base
;
510 retval
= m48t59_read(NVRAM
, NVRAM
->addr
);
516 NVRAM_PRINTF("%s: 0x%08x <= 0x%08x\n", __func__
, addr
, retval
);
521 static void nvram_writeb (void *opaque
, target_phys_addr_t addr
, uint32_t value
)
523 M48t59State
*NVRAM
= opaque
;
525 m48t59_write(NVRAM
, addr
, value
& 0xff);
528 static void nvram_writew (void *opaque
, target_phys_addr_t addr
, uint32_t value
)
530 M48t59State
*NVRAM
= opaque
;
532 m48t59_write(NVRAM
, addr
, (value
>> 8) & 0xff);
533 m48t59_write(NVRAM
, addr
+ 1, value
& 0xff);
536 static void nvram_writel (void *opaque
, target_phys_addr_t addr
, uint32_t value
)
538 M48t59State
*NVRAM
= opaque
;
540 m48t59_write(NVRAM
, addr
, (value
>> 24) & 0xff);
541 m48t59_write(NVRAM
, addr
+ 1, (value
>> 16) & 0xff);
542 m48t59_write(NVRAM
, addr
+ 2, (value
>> 8) & 0xff);
543 m48t59_write(NVRAM
, addr
+ 3, value
& 0xff);
546 static uint32_t nvram_readb (void *opaque
, target_phys_addr_t addr
)
548 M48t59State
*NVRAM
= opaque
;
551 retval
= m48t59_read(NVRAM
, addr
);
555 static uint32_t nvram_readw (void *opaque
, target_phys_addr_t addr
)
557 M48t59State
*NVRAM
= opaque
;
560 retval
= m48t59_read(NVRAM
, addr
) << 8;
561 retval
|= m48t59_read(NVRAM
, addr
+ 1);
565 static uint32_t nvram_readl (void *opaque
, target_phys_addr_t addr
)
567 M48t59State
*NVRAM
= opaque
;
570 retval
= m48t59_read(NVRAM
, addr
) << 24;
571 retval
|= m48t59_read(NVRAM
, addr
+ 1) << 16;
572 retval
|= m48t59_read(NVRAM
, addr
+ 2) << 8;
573 retval
|= m48t59_read(NVRAM
, addr
+ 3);
577 static CPUWriteMemoryFunc
* const nvram_write
[] = {
583 static CPUReadMemoryFunc
* const nvram_read
[] = {
589 static const VMStateDescription vmstate_m48t59
= {
592 .minimum_version_id
= 1,
593 .minimum_version_id_old
= 1,
594 .fields
= (VMStateField
[]) {
595 VMSTATE_UINT8(lock
, M48t59State
),
596 VMSTATE_UINT16(addr
, M48t59State
),
597 VMSTATE_VBUFFER_UINT32(buffer
, M48t59State
, 0, NULL
, 0, size
),
598 VMSTATE_END_OF_LIST()
602 static void m48t59_reset_common(M48t59State
*NVRAM
)
606 if (NVRAM
->alrm_timer
!= NULL
)
607 qemu_del_timer(NVRAM
->alrm_timer
);
609 if (NVRAM
->wd_timer
!= NULL
)
610 qemu_del_timer(NVRAM
->wd_timer
);
613 static void m48t59_reset_isa(DeviceState
*d
)
615 M48t59ISAState
*isa
= container_of(d
, M48t59ISAState
, busdev
.qdev
);
616 M48t59State
*NVRAM
= &isa
->state
;
618 m48t59_reset_common(NVRAM
);
621 static void m48t59_reset_sysbus(DeviceState
*d
)
623 M48t59SysBusState
*sys
= container_of(d
, M48t59SysBusState
, busdev
.qdev
);
624 M48t59State
*NVRAM
= &sys
->state
;
626 m48t59_reset_common(NVRAM
);
629 /* Initialisation routine */
630 M48t59State
*m48t59_init(qemu_irq IRQ
, target_phys_addr_t mem_base
,
631 uint32_t io_base
, uint16_t size
, int type
)
635 M48t59SysBusState
*d
;
638 dev
= qdev_create(NULL
, "m48t59");
639 qdev_prop_set_uint32(dev
, "type", type
);
640 qdev_prop_set_uint32(dev
, "size", size
);
641 qdev_prop_set_uint32(dev
, "io_base", io_base
);
642 qdev_init_nofail(dev
);
643 s
= sysbus_from_qdev(dev
);
644 d
= FROM_SYSBUS(M48t59SysBusState
, s
);
646 sysbus_connect_irq(s
, 0, IRQ
);
648 register_ioport_read(io_base
, 0x04, 1, NVRAM_readb
, state
);
649 register_ioport_write(io_base
, 0x04, 1, NVRAM_writeb
, state
);
652 sysbus_mmio_map(s
, 0, mem_base
);
658 M48t59State
*m48t59_init_isa(uint32_t io_base
, uint16_t size
, int type
)
664 dev
= isa_create("m48t59_isa");
665 qdev_prop_set_uint32(&dev
->qdev
, "type", type
);
666 qdev_prop_set_uint32(&dev
->qdev
, "size", size
);
667 qdev_prop_set_uint32(&dev
->qdev
, "io_base", io_base
);
668 qdev_init_nofail(&dev
->qdev
);
669 d
= DO_UPCAST(M48t59ISAState
, busdev
, dev
);
673 register_ioport_read(io_base
, 0x04, 1, NVRAM_readb
, s
);
674 register_ioport_write(io_base
, 0x04, 1, NVRAM_writeb
, s
);
675 isa_init_ioport_range(dev
, io_base
, 4);
681 static void m48t59_init_common(M48t59State
*s
)
683 s
->buffer
= g_malloc0(s
->size
);
685 s
->alrm_timer
= qemu_new_timer_ns(vm_clock
, &alarm_cb
, s
);
686 s
->wd_timer
= qemu_new_timer_ns(vm_clock
, &watchdog_cb
, s
);
688 qemu_get_timedate(&s
->alarm
, 0);
690 vmstate_register(NULL
, -1, &vmstate_m48t59
, s
);
693 static int m48t59_init_isa1(ISADevice
*dev
)
695 M48t59ISAState
*d
= DO_UPCAST(M48t59ISAState
, busdev
, dev
);
696 M48t59State
*s
= &d
->state
;
698 isa_init_irq(dev
, &s
->IRQ
, 8);
699 m48t59_init_common(s
);
704 static int m48t59_init1(SysBusDevice
*dev
)
706 M48t59SysBusState
*d
= FROM_SYSBUS(M48t59SysBusState
, dev
);
707 M48t59State
*s
= &d
->state
;
710 sysbus_init_irq(dev
, &s
->IRQ
);
712 mem_index
= cpu_register_io_memory(nvram_read
, nvram_write
, s
,
713 DEVICE_NATIVE_ENDIAN
);
714 sysbus_init_mmio(dev
, s
->size
, mem_index
);
715 m48t59_init_common(s
);
720 static ISADeviceInfo m48t59_isa_info
= {
721 .init
= m48t59_init_isa1
,
722 .qdev
.name
= "m48t59_isa",
723 .qdev
.size
= sizeof(M48t59ISAState
),
724 .qdev
.reset
= m48t59_reset_isa
,
726 .qdev
.props
= (Property
[]) {
727 DEFINE_PROP_UINT32("size", M48t59ISAState
, state
.size
, -1),
728 DEFINE_PROP_UINT32("type", M48t59ISAState
, state
.type
, -1),
729 DEFINE_PROP_HEX32( "io_base", M48t59ISAState
, state
.io_base
, 0),
730 DEFINE_PROP_END_OF_LIST(),
734 static SysBusDeviceInfo m48t59_info
= {
735 .init
= m48t59_init1
,
736 .qdev
.name
= "m48t59",
737 .qdev
.size
= sizeof(M48t59SysBusState
),
738 .qdev
.reset
= m48t59_reset_sysbus
,
739 .qdev
.props
= (Property
[]) {
740 DEFINE_PROP_UINT32("size", M48t59SysBusState
, state
.size
, -1),
741 DEFINE_PROP_UINT32("type", M48t59SysBusState
, state
.type
, -1),
742 DEFINE_PROP_HEX32( "io_base", M48t59SysBusState
, state
.io_base
, 0),
743 DEFINE_PROP_END_OF_LIST(),
747 static void m48t59_register_devices(void)
749 sysbus_register_withprop(&m48t59_info
);
750 isa_qdev_register(&m48t59_isa_info
);
753 device_init(m48t59_register_devices
)