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
25 #include "hw/timer/m48t59.h"
26 #include "qemu/timer.h"
27 #include "sysemu/sysemu.h"
28 #include "hw/sysbus.h"
29 #include "hw/isa/isa.h"
30 #include "exec/address-spaces.h"
34 #if defined(DEBUG_NVRAM)
35 #define NVRAM_PRINTF(fmt, ...) do { printf(fmt , ## __VA_ARGS__); } while (0)
37 #define NVRAM_PRINTF(fmt, ...) do { } while (0)
41 * The M48T02, M48T08 and M48T59 chips are very similar. The newer '59 has
42 * alarm and a watchdog timer and related control registers. In the
43 * PPC platform there is also a nvram lock function.
48 * http://www.st.com/stonline/products/literature/ds/2410/m48t02.pdf
49 * http://www.st.com/stonline/products/literature/ds/2411/m48t08.pdf
50 * http://www.st.com/stonline/products/literature/od/7001/m48t59y.pdf
54 /* Hardware parameters */
62 /* Alarm & watchdog */
64 struct QEMUTimer
*alrm_timer
;
65 struct QEMUTimer
*wd_timer
;
68 /* Model parameters */
69 uint32_t model
; /* 2 = m48t02, 8 = m48t08, 59 = m48t59 */
75 #define TYPE_ISA_M48T59 "m48t59_isa"
76 #define ISA_M48T59(obj) \
77 OBJECT_CHECK(M48t59ISAState, (obj), TYPE_ISA_M48T59)
79 typedef struct M48t59ISAState
{
86 typedef struct M48t59SysBusState
{
92 /* Fake timer functions */
94 /* Alarm management */
95 static void alarm_cb (void *opaque
)
99 M48t59State
*NVRAM
= opaque
;
101 qemu_set_irq(NVRAM
->IRQ
, 1);
102 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 a month */
107 qemu_get_timedate(&tm
, NVRAM
->time_offset
);
109 if (tm
.tm_mon
== 13) {
113 next_time
= qemu_timedate_diff(&tm
) - NVRAM
->time_offset
;
114 } else if ((NVRAM
->buffer
[0x1FF5] & 0x80) != 0 &&
115 (NVRAM
->buffer
[0x1FF4] & 0x80) == 0 &&
116 (NVRAM
->buffer
[0x1FF3] & 0x80) == 0 &&
117 (NVRAM
->buffer
[0x1FF2] & 0x80) == 0) {
118 /* Repeat once a day */
119 next_time
= 24 * 60 * 60;
120 } else if ((NVRAM
->buffer
[0x1FF5] & 0x80) != 0 &&
121 (NVRAM
->buffer
[0x1FF4] & 0x80) != 0 &&
122 (NVRAM
->buffer
[0x1FF3] & 0x80) == 0 &&
123 (NVRAM
->buffer
[0x1FF2] & 0x80) == 0) {
124 /* Repeat once an hour */
126 } else if ((NVRAM
->buffer
[0x1FF5] & 0x80) != 0 &&
127 (NVRAM
->buffer
[0x1FF4] & 0x80) != 0 &&
128 (NVRAM
->buffer
[0x1FF3] & 0x80) != 0 &&
129 (NVRAM
->buffer
[0x1FF2] & 0x80) == 0) {
130 /* Repeat once a minute */
133 /* Repeat once a second */
136 qemu_mod_timer(NVRAM
->alrm_timer
, qemu_get_clock_ns(rtc_clock
) +
138 qemu_set_irq(NVRAM
->IRQ
, 0);
141 static void set_alarm(M48t59State
*NVRAM
)
144 if (NVRAM
->alrm_timer
!= NULL
) {
145 qemu_del_timer(NVRAM
->alrm_timer
);
146 diff
= qemu_timedate_diff(&NVRAM
->alarm
) - NVRAM
->time_offset
;
148 qemu_mod_timer(NVRAM
->alrm_timer
, diff
* 1000);
152 /* RTC management helpers */
153 static inline void get_time(M48t59State
*NVRAM
, struct tm
*tm
)
155 qemu_get_timedate(tm
, NVRAM
->time_offset
);
158 static void set_time(M48t59State
*NVRAM
, struct tm
*tm
)
160 NVRAM
->time_offset
= qemu_timedate_diff(tm
);
164 /* Watchdog management */
165 static void watchdog_cb (void *opaque
)
167 M48t59State
*NVRAM
= opaque
;
169 NVRAM
->buffer
[0x1FF0] |= 0x80;
170 if (NVRAM
->buffer
[0x1FF7] & 0x80) {
171 NVRAM
->buffer
[0x1FF7] = 0x00;
172 NVRAM
->buffer
[0x1FFC] &= ~0x40;
173 /* May it be a hw CPU Reset instead ? */
174 qemu_system_reset_request();
176 qemu_set_irq(NVRAM
->IRQ
, 1);
177 qemu_set_irq(NVRAM
->IRQ
, 0);
181 static void set_up_watchdog(M48t59State
*NVRAM
, uint8_t value
)
183 uint64_t interval
; /* in 1/16 seconds */
185 NVRAM
->buffer
[0x1FF0] &= ~0x80;
186 if (NVRAM
->wd_timer
!= NULL
) {
187 qemu_del_timer(NVRAM
->wd_timer
);
189 interval
= (1 << (2 * (value
& 0x03))) * ((value
>> 2) & 0x1F);
190 qemu_mod_timer(NVRAM
->wd_timer
, ((uint64_t)time(NULL
) * 1000) +
191 ((interval
* 1000) >> 4));
196 /* Direct access to NVRAM */
197 void m48t59_write (void *opaque
, uint32_t addr
, uint32_t val
)
199 M48t59State
*NVRAM
= opaque
;
203 if (addr
> 0x1FF8 && addr
< 0x2000)
204 NVRAM_PRINTF("%s: 0x%08x => 0x%08x\n", __func__
, addr
, val
);
206 /* check for NVRAM access */
207 if ((NVRAM
->model
== 2 && addr
< 0x7f8) ||
208 (NVRAM
->model
== 8 && addr
< 0x1ff8) ||
209 (NVRAM
->model
== 59 && addr
< 0x1ff0)) {
216 /* flags register : read-only */
223 tmp
= from_bcd(val
& 0x7F);
224 if (tmp
>= 0 && tmp
<= 59) {
225 NVRAM
->alarm
.tm_sec
= tmp
;
226 NVRAM
->buffer
[0x1FF2] = val
;
232 tmp
= from_bcd(val
& 0x7F);
233 if (tmp
>= 0 && tmp
<= 59) {
234 NVRAM
->alarm
.tm_min
= tmp
;
235 NVRAM
->buffer
[0x1FF3] = val
;
241 tmp
= from_bcd(val
& 0x3F);
242 if (tmp
>= 0 && tmp
<= 23) {
243 NVRAM
->alarm
.tm_hour
= tmp
;
244 NVRAM
->buffer
[0x1FF4] = val
;
250 tmp
= from_bcd(val
& 0x3F);
252 NVRAM
->alarm
.tm_mday
= tmp
;
253 NVRAM
->buffer
[0x1FF5] = val
;
259 NVRAM
->buffer
[0x1FF6] = val
;
263 NVRAM
->buffer
[0x1FF7] = val
;
264 set_up_watchdog(NVRAM
, val
);
269 NVRAM
->buffer
[addr
] = (val
& ~0xA0) | 0x90;
274 tmp
= from_bcd(val
& 0x7F);
275 if (tmp
>= 0 && tmp
<= 59) {
276 get_time(NVRAM
, &tm
);
278 set_time(NVRAM
, &tm
);
280 if ((val
& 0x80) ^ (NVRAM
->buffer
[addr
] & 0x80)) {
282 NVRAM
->stop_time
= time(NULL
);
284 NVRAM
->time_offset
+= NVRAM
->stop_time
- time(NULL
);
285 NVRAM
->stop_time
= 0;
288 NVRAM
->buffer
[addr
] = val
& 0x80;
293 tmp
= from_bcd(val
& 0x7F);
294 if (tmp
>= 0 && tmp
<= 59) {
295 get_time(NVRAM
, &tm
);
297 set_time(NVRAM
, &tm
);
303 tmp
= from_bcd(val
& 0x3F);
304 if (tmp
>= 0 && tmp
<= 23) {
305 get_time(NVRAM
, &tm
);
307 set_time(NVRAM
, &tm
);
312 /* day of the week / century */
313 tmp
= from_bcd(val
& 0x07);
314 get_time(NVRAM
, &tm
);
316 set_time(NVRAM
, &tm
);
317 NVRAM
->buffer
[addr
] = val
& 0x40;
322 tmp
= from_bcd(val
& 0x3F);
324 get_time(NVRAM
, &tm
);
326 set_time(NVRAM
, &tm
);
332 tmp
= from_bcd(val
& 0x1F);
333 if (tmp
>= 1 && tmp
<= 12) {
334 get_time(NVRAM
, &tm
);
336 set_time(NVRAM
, &tm
);
343 if (tmp
>= 0 && tmp
<= 99) {
344 get_time(NVRAM
, &tm
);
345 if (NVRAM
->model
== 8) {
346 tm
.tm_year
= from_bcd(val
) + 68; // Base year is 1968
348 tm
.tm_year
= from_bcd(val
);
350 set_time(NVRAM
, &tm
);
354 /* Check lock registers state */
355 if (addr
>= 0x20 && addr
<= 0x2F && (NVRAM
->lock
& 1))
357 if (addr
>= 0x30 && addr
<= 0x3F && (NVRAM
->lock
& 2))
360 if (addr
< NVRAM
->size
) {
361 NVRAM
->buffer
[addr
] = val
& 0xFF;
367 uint32_t m48t59_read (void *opaque
, uint32_t addr
)
369 M48t59State
*NVRAM
= opaque
;
371 uint32_t retval
= 0xFF;
373 /* check for NVRAM access */
374 if ((NVRAM
->model
== 2 && addr
< 0x078f) ||
375 (NVRAM
->model
== 8 && addr
< 0x1ff8) ||
376 (NVRAM
->model
== 59 && addr
< 0x1ff0)) {
405 /* A read resets the watchdog */
406 set_up_watchdog(NVRAM
, NVRAM
->buffer
[0x1FF7]);
415 get_time(NVRAM
, &tm
);
416 retval
= (NVRAM
->buffer
[addr
] & 0x80) | to_bcd(tm
.tm_sec
);
421 get_time(NVRAM
, &tm
);
422 retval
= to_bcd(tm
.tm_min
);
427 get_time(NVRAM
, &tm
);
428 retval
= to_bcd(tm
.tm_hour
);
432 /* day of the week / century */
433 get_time(NVRAM
, &tm
);
434 retval
= NVRAM
->buffer
[addr
] | tm
.tm_wday
;
439 get_time(NVRAM
, &tm
);
440 retval
= to_bcd(tm
.tm_mday
);
445 get_time(NVRAM
, &tm
);
446 retval
= to_bcd(tm
.tm_mon
+ 1);
451 get_time(NVRAM
, &tm
);
452 if (NVRAM
->model
== 8) {
453 retval
= to_bcd(tm
.tm_year
- 68); // Base year is 1968
455 retval
= to_bcd(tm
.tm_year
);
459 /* Check lock registers state */
460 if (addr
>= 0x20 && addr
<= 0x2F && (NVRAM
->lock
& 1))
462 if (addr
>= 0x30 && addr
<= 0x3F && (NVRAM
->lock
& 2))
465 if (addr
< NVRAM
->size
) {
466 retval
= NVRAM
->buffer
[addr
];
470 if (addr
> 0x1FF9 && addr
< 0x2000)
471 NVRAM_PRINTF("%s: 0x%08x <= 0x%08x\n", __func__
, addr
, retval
);
476 void m48t59_toggle_lock (void *opaque
, int lock
)
478 M48t59State
*NVRAM
= opaque
;
480 NVRAM
->lock
^= 1 << lock
;
483 /* IO access to NVRAM */
484 static void NVRAM_writeb(void *opaque
, hwaddr addr
, uint64_t val
,
487 M48t59State
*NVRAM
= opaque
;
489 NVRAM_PRINTF("%s: 0x%08x => 0x%08x\n", __func__
, addr
, val
);
492 NVRAM
->addr
&= ~0x00FF;
496 NVRAM
->addr
&= ~0xFF00;
497 NVRAM
->addr
|= val
<< 8;
500 m48t59_write(NVRAM
, NVRAM
->addr
, val
);
501 NVRAM
->addr
= 0x0000;
508 static uint64_t NVRAM_readb(void *opaque
, hwaddr addr
, unsigned size
)
510 M48t59State
*NVRAM
= opaque
;
515 retval
= m48t59_read(NVRAM
, NVRAM
->addr
);
521 NVRAM_PRINTF("%s: 0x%08x <= 0x%08x\n", __func__
, addr
, retval
);
526 static void nvram_writeb (void *opaque
, hwaddr addr
, uint32_t value
)
528 M48t59State
*NVRAM
= opaque
;
530 m48t59_write(NVRAM
, addr
, value
& 0xff);
533 static void nvram_writew (void *opaque
, hwaddr addr
, uint32_t value
)
535 M48t59State
*NVRAM
= opaque
;
537 m48t59_write(NVRAM
, addr
, (value
>> 8) & 0xff);
538 m48t59_write(NVRAM
, addr
+ 1, value
& 0xff);
541 static void nvram_writel (void *opaque
, hwaddr addr
, uint32_t value
)
543 M48t59State
*NVRAM
= opaque
;
545 m48t59_write(NVRAM
, addr
, (value
>> 24) & 0xff);
546 m48t59_write(NVRAM
, addr
+ 1, (value
>> 16) & 0xff);
547 m48t59_write(NVRAM
, addr
+ 2, (value
>> 8) & 0xff);
548 m48t59_write(NVRAM
, addr
+ 3, value
& 0xff);
551 static uint32_t nvram_readb (void *opaque
, hwaddr addr
)
553 M48t59State
*NVRAM
= opaque
;
556 retval
= m48t59_read(NVRAM
, addr
);
560 static uint32_t nvram_readw (void *opaque
, hwaddr addr
)
562 M48t59State
*NVRAM
= opaque
;
565 retval
= m48t59_read(NVRAM
, addr
) << 8;
566 retval
|= m48t59_read(NVRAM
, addr
+ 1);
570 static uint32_t nvram_readl (void *opaque
, hwaddr addr
)
572 M48t59State
*NVRAM
= opaque
;
575 retval
= m48t59_read(NVRAM
, addr
) << 24;
576 retval
|= m48t59_read(NVRAM
, addr
+ 1) << 16;
577 retval
|= m48t59_read(NVRAM
, addr
+ 2) << 8;
578 retval
|= m48t59_read(NVRAM
, addr
+ 3);
582 static const MemoryRegionOps nvram_ops
= {
584 .read
= { nvram_readb
, nvram_readw
, nvram_readl
, },
585 .write
= { nvram_writeb
, nvram_writew
, nvram_writel
, },
587 .endianness
= DEVICE_NATIVE_ENDIAN
,
590 static const VMStateDescription vmstate_m48t59
= {
593 .minimum_version_id
= 1,
594 .minimum_version_id_old
= 1,
595 .fields
= (VMStateField
[]) {
596 VMSTATE_UINT8(lock
, M48t59State
),
597 VMSTATE_UINT16(addr
, M48t59State
),
598 VMSTATE_VBUFFER_UINT32(buffer
, M48t59State
, 0, NULL
, 0, size
),
599 VMSTATE_END_OF_LIST()
603 static void m48t59_reset_common(M48t59State
*NVRAM
)
607 if (NVRAM
->alrm_timer
!= NULL
)
608 qemu_del_timer(NVRAM
->alrm_timer
);
610 if (NVRAM
->wd_timer
!= NULL
)
611 qemu_del_timer(NVRAM
->wd_timer
);
614 static void m48t59_reset_isa(DeviceState
*d
)
616 M48t59ISAState
*isa
= ISA_M48T59(d
);
617 M48t59State
*NVRAM
= &isa
->state
;
619 m48t59_reset_common(NVRAM
);
622 static void m48t59_reset_sysbus(DeviceState
*d
)
624 M48t59SysBusState
*sys
= container_of(d
, M48t59SysBusState
, busdev
.qdev
);
625 M48t59State
*NVRAM
= &sys
->state
;
627 m48t59_reset_common(NVRAM
);
630 static const MemoryRegionOps m48t59_io_ops
= {
632 .write
= NVRAM_writeb
,
634 .min_access_size
= 1,
635 .max_access_size
= 1,
637 .endianness
= DEVICE_LITTLE_ENDIAN
,
640 /* Initialisation routine */
641 M48t59State
*m48t59_init(qemu_irq IRQ
, hwaddr mem_base
,
642 uint32_t io_base
, uint16_t size
, int model
)
646 M48t59SysBusState
*d
;
649 dev
= qdev_create(NULL
, "m48t59");
650 qdev_prop_set_uint32(dev
, "model", model
);
651 qdev_prop_set_uint32(dev
, "size", size
);
652 qdev_prop_set_uint32(dev
, "io_base", io_base
);
653 qdev_init_nofail(dev
);
654 s
= SYS_BUS_DEVICE(dev
);
655 d
= FROM_SYSBUS(M48t59SysBusState
, s
);
657 sysbus_connect_irq(s
, 0, IRQ
);
658 memory_region_init_io(&d
->io
, &m48t59_io_ops
, state
, "m48t59", 4);
660 memory_region_add_subregion(get_system_io(), io_base
, &d
->io
);
663 sysbus_mmio_map(s
, 0, mem_base
);
669 M48t59State
*m48t59_init_isa(ISABus
*bus
, uint32_t io_base
, uint16_t size
,
677 isadev
= isa_create(bus
, TYPE_ISA_M48T59
);
678 dev
= DEVICE(isadev
);
679 qdev_prop_set_uint32(dev
, "model", model
);
680 qdev_prop_set_uint32(dev
, "size", size
);
681 qdev_prop_set_uint32(dev
, "io_base", io_base
);
682 qdev_init_nofail(dev
);
683 d
= ISA_M48T59(isadev
);
686 memory_region_init_io(&d
->io
, &m48t59_io_ops
, s
, "m48t59", 4);
688 isa_register_ioport(isadev
, &d
->io
, io_base
);
694 static void m48t59_init_common(M48t59State
*s
)
696 s
->buffer
= g_malloc0(s
->size
);
697 if (s
->model
== 59) {
698 s
->alrm_timer
= qemu_new_timer_ns(rtc_clock
, &alarm_cb
, s
);
699 s
->wd_timer
= qemu_new_timer_ns(vm_clock
, &watchdog_cb
, s
);
701 qemu_get_timedate(&s
->alarm
, 0);
703 vmstate_register(NULL
, -1, &vmstate_m48t59
, s
);
706 static int m48t59_init_isa1(ISADevice
*dev
)
708 M48t59ISAState
*d
= ISA_M48T59(dev
);
709 M48t59State
*s
= &d
->state
;
711 isa_init_irq(dev
, &s
->IRQ
, 8);
712 m48t59_init_common(s
);
717 static int m48t59_init1(SysBusDevice
*dev
)
719 M48t59SysBusState
*d
= FROM_SYSBUS(M48t59SysBusState
, dev
);
720 M48t59State
*s
= &d
->state
;
722 sysbus_init_irq(dev
, &s
->IRQ
);
724 memory_region_init_io(&s
->iomem
, &nvram_ops
, s
, "m48t59.nvram", s
->size
);
725 sysbus_init_mmio(dev
, &s
->iomem
);
726 m48t59_init_common(s
);
731 static Property m48t59_isa_properties
[] = {
732 DEFINE_PROP_UINT32("size", M48t59ISAState
, state
.size
, -1),
733 DEFINE_PROP_UINT32("model", M48t59ISAState
, state
.model
, -1),
734 DEFINE_PROP_HEX32( "io_base", M48t59ISAState
, state
.io_base
, 0),
735 DEFINE_PROP_END_OF_LIST(),
738 static void m48t59_isa_class_init(ObjectClass
*klass
, void *data
)
740 DeviceClass
*dc
= DEVICE_CLASS(klass
);
741 ISADeviceClass
*ic
= ISA_DEVICE_CLASS(klass
);
742 ic
->init
= m48t59_init_isa1
;
744 dc
->reset
= m48t59_reset_isa
;
745 dc
->props
= m48t59_isa_properties
;
748 static const TypeInfo m48t59_isa_info
= {
749 .name
= TYPE_ISA_M48T59
,
750 .parent
= TYPE_ISA_DEVICE
,
751 .instance_size
= sizeof(M48t59ISAState
),
752 .class_init
= m48t59_isa_class_init
,
755 static Property m48t59_properties
[] = {
756 DEFINE_PROP_UINT32("size", M48t59SysBusState
, state
.size
, -1),
757 DEFINE_PROP_UINT32("model", M48t59SysBusState
, state
.model
, -1),
758 DEFINE_PROP_HEX32( "io_base", M48t59SysBusState
, state
.io_base
, 0),
759 DEFINE_PROP_END_OF_LIST(),
762 static void m48t59_class_init(ObjectClass
*klass
, void *data
)
764 DeviceClass
*dc
= DEVICE_CLASS(klass
);
765 SysBusDeviceClass
*k
= SYS_BUS_DEVICE_CLASS(klass
);
767 k
->init
= m48t59_init1
;
768 dc
->reset
= m48t59_reset_sysbus
;
769 dc
->props
= m48t59_properties
;
772 static const TypeInfo m48t59_info
= {
774 .parent
= TYPE_SYS_BUS_DEVICE
,
775 .instance_size
= sizeof(M48t59SysBusState
),
776 .class_init
= m48t59_class_init
,
779 static void m48t59_register_types(void)
781 type_register_static(&m48t59_info
);
782 type_register_static(&m48t59_isa_info
);
785 type_init(m48t59_register_types
)