2 * QEMU M48T59 and M48T08 NVRAM emulation for PPC PREP and Sparc platforms
4 * Copyright (c) 2003-2005, 2007, 2017 Jocelyn Mayer
5 * Copyright (c) 2013 Hervé Poussineau
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26 #include "qemu/osdep.h"
27 #include "qemu-common.h"
29 #include "hw/qdev-properties.h"
30 #include "hw/timer/m48t59.h"
31 #include "qemu/timer.h"
32 #include "sysemu/runstate.h"
33 #include "sysemu/sysemu.h"
34 #include "hw/sysbus.h"
35 #include "exec/address-spaces.h"
37 #include "qemu/module.h"
39 #include "m48t59-internal.h"
40 #include "migration/vmstate.h"
42 #define TYPE_M48TXX_SYS_BUS "sysbus-m48txx"
43 #define M48TXX_SYS_BUS_GET_CLASS(obj) \
44 OBJECT_GET_CLASS(M48txxSysBusDeviceClass, (obj), TYPE_M48TXX_SYS_BUS)
45 #define M48TXX_SYS_BUS_CLASS(klass) \
46 OBJECT_CLASS_CHECK(M48txxSysBusDeviceClass, (klass), TYPE_M48TXX_SYS_BUS)
47 #define M48TXX_SYS_BUS(obj) \
48 OBJECT_CHECK(M48txxSysBusState, (obj), TYPE_M48TXX_SYS_BUS)
52 * http://www.st.com/stonline/products/literature/ds/2410/m48t02.pdf
53 * http://www.st.com/stonline/products/literature/ds/2411/m48t08.pdf
54 * http://www.st.com/stonline/products/literature/od/7001/m48t59y.pdf
57 typedef struct M48txxSysBusState
{
58 SysBusDevice parent_obj
;
63 typedef struct M48txxSysBusDeviceClass
{
64 SysBusDeviceClass parent_class
;
66 } M48txxSysBusDeviceClass
;
68 static M48txxInfo m48txx_sysbus_info
[] = {
70 .bus_name
= "sysbus-m48t02",
74 .bus_name
= "sysbus-m48t08",
78 .bus_name
= "sysbus-m48t59",
85 /* Fake timer functions */
87 /* Alarm management */
88 static void alarm_cb (void *opaque
)
92 M48t59State
*NVRAM
= opaque
;
94 qemu_set_irq(NVRAM
->IRQ
, 1);
95 if ((NVRAM
->buffer
[0x1FF5] & 0x80) == 0 &&
96 (NVRAM
->buffer
[0x1FF4] & 0x80) == 0 &&
97 (NVRAM
->buffer
[0x1FF3] & 0x80) == 0 &&
98 (NVRAM
->buffer
[0x1FF2] & 0x80) == 0) {
99 /* Repeat once a month */
100 qemu_get_timedate(&tm
, NVRAM
->time_offset
);
102 if (tm
.tm_mon
== 13) {
106 next_time
= qemu_timedate_diff(&tm
) - NVRAM
->time_offset
;
107 } else if ((NVRAM
->buffer
[0x1FF5] & 0x80) != 0 &&
108 (NVRAM
->buffer
[0x1FF4] & 0x80) == 0 &&
109 (NVRAM
->buffer
[0x1FF3] & 0x80) == 0 &&
110 (NVRAM
->buffer
[0x1FF2] & 0x80) == 0) {
111 /* Repeat once a day */
112 next_time
= 24 * 60 * 60;
113 } else if ((NVRAM
->buffer
[0x1FF5] & 0x80) != 0 &&
114 (NVRAM
->buffer
[0x1FF4] & 0x80) != 0 &&
115 (NVRAM
->buffer
[0x1FF3] & 0x80) == 0 &&
116 (NVRAM
->buffer
[0x1FF2] & 0x80) == 0) {
117 /* Repeat once an hour */
119 } else if ((NVRAM
->buffer
[0x1FF5] & 0x80) != 0 &&
120 (NVRAM
->buffer
[0x1FF4] & 0x80) != 0 &&
121 (NVRAM
->buffer
[0x1FF3] & 0x80) != 0 &&
122 (NVRAM
->buffer
[0x1FF2] & 0x80) == 0) {
123 /* Repeat once a minute */
126 /* Repeat once a second */
129 timer_mod(NVRAM
->alrm_timer
, qemu_clock_get_ns(rtc_clock
) +
131 qemu_set_irq(NVRAM
->IRQ
, 0);
134 static void set_alarm(M48t59State
*NVRAM
)
137 if (NVRAM
->alrm_timer
!= NULL
) {
138 timer_del(NVRAM
->alrm_timer
);
139 diff
= qemu_timedate_diff(&NVRAM
->alarm
) - NVRAM
->time_offset
;
141 timer_mod(NVRAM
->alrm_timer
, diff
* 1000);
145 /* RTC management helpers */
146 static inline void get_time(M48t59State
*NVRAM
, struct tm
*tm
)
148 qemu_get_timedate(tm
, NVRAM
->time_offset
);
151 static void set_time(M48t59State
*NVRAM
, struct tm
*tm
)
153 NVRAM
->time_offset
= qemu_timedate_diff(tm
);
157 /* Watchdog management */
158 static void watchdog_cb (void *opaque
)
160 M48t59State
*NVRAM
= opaque
;
162 NVRAM
->buffer
[0x1FF0] |= 0x80;
163 if (NVRAM
->buffer
[0x1FF7] & 0x80) {
164 NVRAM
->buffer
[0x1FF7] = 0x00;
165 NVRAM
->buffer
[0x1FFC] &= ~0x40;
166 /* May it be a hw CPU Reset instead ? */
167 qemu_system_reset_request(SHUTDOWN_CAUSE_GUEST_RESET
);
169 qemu_set_irq(NVRAM
->IRQ
, 1);
170 qemu_set_irq(NVRAM
->IRQ
, 0);
174 static void set_up_watchdog(M48t59State
*NVRAM
, uint8_t value
)
176 uint64_t interval
; /* in 1/16 seconds */
178 NVRAM
->buffer
[0x1FF0] &= ~0x80;
179 if (NVRAM
->wd_timer
!= NULL
) {
180 timer_del(NVRAM
->wd_timer
);
182 interval
= (1 << (2 * (value
& 0x03))) * ((value
>> 2) & 0x1F);
183 timer_mod(NVRAM
->wd_timer
, ((uint64_t)time(NULL
) * 1000) +
184 ((interval
* 1000) >> 4));
189 /* Direct access to NVRAM */
190 void m48t59_write(M48t59State
*NVRAM
, uint32_t addr
, uint32_t val
)
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
->model
== 2 && addr
< 0x7f8) ||
200 (NVRAM
->model
== 8 && addr
< 0x1ff8) ||
201 (NVRAM
->model
== 59 && addr
< 0x1ff0)) {
208 /* flags register : read-only */
215 tmp
= from_bcd(val
& 0x7F);
216 if (tmp
>= 0 && tmp
<= 59) {
217 NVRAM
->alarm
.tm_sec
= tmp
;
218 NVRAM
->buffer
[0x1FF2] = val
;
224 tmp
= from_bcd(val
& 0x7F);
225 if (tmp
>= 0 && tmp
<= 59) {
226 NVRAM
->alarm
.tm_min
= tmp
;
227 NVRAM
->buffer
[0x1FF3] = val
;
233 tmp
= from_bcd(val
& 0x3F);
234 if (tmp
>= 0 && tmp
<= 23) {
235 NVRAM
->alarm
.tm_hour
= tmp
;
236 NVRAM
->buffer
[0x1FF4] = val
;
242 tmp
= from_bcd(val
& 0x3F);
244 NVRAM
->alarm
.tm_mday
= tmp
;
245 NVRAM
->buffer
[0x1FF5] = val
;
251 NVRAM
->buffer
[0x1FF6] = val
;
255 NVRAM
->buffer
[0x1FF7] = val
;
256 set_up_watchdog(NVRAM
, val
);
261 NVRAM
->buffer
[addr
] = (val
& ~0xA0) | 0x90;
266 tmp
= from_bcd(val
& 0x7F);
267 if (tmp
>= 0 && tmp
<= 59) {
268 get_time(NVRAM
, &tm
);
270 set_time(NVRAM
, &tm
);
272 if ((val
& 0x80) ^ (NVRAM
->buffer
[addr
] & 0x80)) {
274 NVRAM
->stop_time
= time(NULL
);
276 NVRAM
->time_offset
+= NVRAM
->stop_time
- time(NULL
);
277 NVRAM
->stop_time
= 0;
280 NVRAM
->buffer
[addr
] = val
& 0x80;
285 tmp
= from_bcd(val
& 0x7F);
286 if (tmp
>= 0 && tmp
<= 59) {
287 get_time(NVRAM
, &tm
);
289 set_time(NVRAM
, &tm
);
295 tmp
= from_bcd(val
& 0x3F);
296 if (tmp
>= 0 && tmp
<= 23) {
297 get_time(NVRAM
, &tm
);
299 set_time(NVRAM
, &tm
);
304 /* day of the week / century */
305 tmp
= from_bcd(val
& 0x07);
306 get_time(NVRAM
, &tm
);
308 set_time(NVRAM
, &tm
);
309 NVRAM
->buffer
[addr
] = val
& 0x40;
314 tmp
= from_bcd(val
& 0x3F);
316 get_time(NVRAM
, &tm
);
318 set_time(NVRAM
, &tm
);
324 tmp
= from_bcd(val
& 0x1F);
325 if (tmp
>= 1 && tmp
<= 12) {
326 get_time(NVRAM
, &tm
);
328 set_time(NVRAM
, &tm
);
335 if (tmp
>= 0 && tmp
<= 99) {
336 get_time(NVRAM
, &tm
);
337 tm
.tm_year
= from_bcd(val
) + NVRAM
->base_year
- 1900;
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(M48t59State
*NVRAM
, uint32_t addr
)
358 uint32_t retval
= 0xFF;
360 /* check for NVRAM access */
361 if ((NVRAM
->model
== 2 && addr
< 0x078f) ||
362 (NVRAM
->model
== 8 && addr
< 0x1ff8) ||
363 (NVRAM
->model
== 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 retval
= to_bcd((tm
.tm_year
+ 1900 - NVRAM
->base_year
) % 100);
442 /* Check lock registers state */
443 if (addr
>= 0x20 && addr
<= 0x2F && (NVRAM
->lock
& 1))
445 if (addr
>= 0x30 && addr
<= 0x3F && (NVRAM
->lock
& 2))
448 if (addr
< NVRAM
->size
) {
449 retval
= NVRAM
->buffer
[addr
];
453 if (addr
> 0x1FF9 && addr
< 0x2000)
454 NVRAM_PRINTF("%s: 0x%08x <= 0x%08x\n", __func__
, addr
, retval
);
459 /* IO access to NVRAM */
460 static void NVRAM_writeb(void *opaque
, hwaddr addr
, uint64_t val
,
463 M48t59State
*NVRAM
= opaque
;
465 NVRAM_PRINTF("%s: 0x%"HWADDR_PRIx
" => 0x%"PRIx64
"\n", __func__
, addr
, val
);
468 NVRAM
->addr
&= ~0x00FF;
472 NVRAM
->addr
&= ~0xFF00;
473 NVRAM
->addr
|= val
<< 8;
476 m48t59_write(NVRAM
, NVRAM
->addr
, val
);
477 NVRAM
->addr
= 0x0000;
484 static uint64_t NVRAM_readb(void *opaque
, hwaddr addr
, unsigned size
)
486 M48t59State
*NVRAM
= opaque
;
491 retval
= m48t59_read(NVRAM
, NVRAM
->addr
);
497 NVRAM_PRINTF("%s: 0x%"HWADDR_PRIx
" <= 0x%08x\n", __func__
, addr
, retval
);
502 static uint64_t nvram_read(void *opaque
, hwaddr addr
, unsigned size
)
504 M48t59State
*NVRAM
= opaque
;
506 return m48t59_read(NVRAM
, addr
);
509 static void nvram_write(void *opaque
, hwaddr addr
, uint64_t value
,
512 M48t59State
*NVRAM
= opaque
;
514 return m48t59_write(NVRAM
, addr
, value
);
517 static const MemoryRegionOps nvram_ops
= {
519 .write
= nvram_write
,
520 .impl
.min_access_size
= 1,
521 .impl
.max_access_size
= 1,
522 .valid
.min_access_size
= 1,
523 .valid
.max_access_size
= 4,
524 .endianness
= DEVICE_BIG_ENDIAN
,
527 static const VMStateDescription vmstate_m48t59
= {
530 .minimum_version_id
= 1,
531 .fields
= (VMStateField
[]) {
532 VMSTATE_UINT8(lock
, M48t59State
),
533 VMSTATE_UINT16(addr
, M48t59State
),
534 VMSTATE_VBUFFER_UINT32(buffer
, M48t59State
, 0, NULL
, size
),
535 VMSTATE_END_OF_LIST()
539 void m48t59_reset_common(M48t59State
*NVRAM
)
543 if (NVRAM
->alrm_timer
!= NULL
)
544 timer_del(NVRAM
->alrm_timer
);
546 if (NVRAM
->wd_timer
!= NULL
)
547 timer_del(NVRAM
->wd_timer
);
550 static void m48t59_reset_sysbus(DeviceState
*d
)
552 M48txxSysBusState
*sys
= M48TXX_SYS_BUS(d
);
553 M48t59State
*NVRAM
= &sys
->state
;
555 m48t59_reset_common(NVRAM
);
558 const MemoryRegionOps m48t59_io_ops
= {
560 .write
= NVRAM_writeb
,
562 .min_access_size
= 1,
563 .max_access_size
= 1,
565 .endianness
= DEVICE_LITTLE_ENDIAN
,
568 /* Initialisation routine */
569 Nvram
*m48t59_init(qemu_irq IRQ
, hwaddr mem_base
,
570 uint32_t io_base
, uint16_t size
, int base_year
,
577 for (i
= 0; i
< ARRAY_SIZE(m48txx_sysbus_info
); i
++) {
578 if (m48txx_sysbus_info
[i
].size
!= size
||
579 m48txx_sysbus_info
[i
].model
!= model
) {
583 dev
= qdev_create(NULL
, m48txx_sysbus_info
[i
].bus_name
);
584 qdev_prop_set_int32(dev
, "base-year", base_year
);
585 qdev_init_nofail(dev
);
586 s
= SYS_BUS_DEVICE(dev
);
587 sysbus_connect_irq(s
, 0, IRQ
);
589 memory_region_add_subregion(get_system_io(), io_base
,
590 sysbus_mmio_get_region(s
, 1));
593 sysbus_mmio_map(s
, 0, mem_base
);
603 void m48t59_realize_common(M48t59State
*s
, Error
**errp
)
605 s
->buffer
= g_malloc0(s
->size
);
606 if (s
->model
== 59) {
607 s
->alrm_timer
= timer_new_ns(rtc_clock
, &alarm_cb
, s
);
608 s
->wd_timer
= timer_new_ns(QEMU_CLOCK_VIRTUAL
, &watchdog_cb
, s
);
610 qemu_get_timedate(&s
->alarm
, 0);
613 static void m48t59_init1(Object
*obj
)
615 M48txxSysBusDeviceClass
*u
= M48TXX_SYS_BUS_GET_CLASS(obj
);
616 M48txxSysBusState
*d
= M48TXX_SYS_BUS(obj
);
617 SysBusDevice
*dev
= SYS_BUS_DEVICE(obj
);
618 M48t59State
*s
= &d
->state
;
620 s
->model
= u
->info
.model
;
621 s
->size
= u
->info
.size
;
622 sysbus_init_irq(dev
, &s
->IRQ
);
624 memory_region_init_io(&s
->iomem
, obj
, &nvram_ops
, s
, "m48t59.nvram",
626 memory_region_init_io(&d
->io
, obj
, &m48t59_io_ops
, s
, "m48t59", 4);
629 static void m48t59_realize(DeviceState
*dev
, Error
**errp
)
631 M48txxSysBusState
*d
= M48TXX_SYS_BUS(dev
);
632 M48t59State
*s
= &d
->state
;
633 SysBusDevice
*sbd
= SYS_BUS_DEVICE(dev
);
635 sysbus_init_mmio(sbd
, &s
->iomem
);
636 sysbus_init_mmio(sbd
, &d
->io
);
637 m48t59_realize_common(s
, errp
);
640 static uint32_t m48txx_sysbus_read(Nvram
*obj
, uint32_t addr
)
642 M48txxSysBusState
*d
= M48TXX_SYS_BUS(obj
);
643 return m48t59_read(&d
->state
, addr
);
646 static void m48txx_sysbus_write(Nvram
*obj
, uint32_t addr
, uint32_t val
)
648 M48txxSysBusState
*d
= M48TXX_SYS_BUS(obj
);
649 m48t59_write(&d
->state
, addr
, val
);
652 static void m48txx_sysbus_toggle_lock(Nvram
*obj
, int lock
)
654 M48txxSysBusState
*d
= M48TXX_SYS_BUS(obj
);
655 m48t59_toggle_lock(&d
->state
, lock
);
658 static Property m48t59_sysbus_properties
[] = {
659 DEFINE_PROP_INT32("base-year", M48txxSysBusState
, state
.base_year
, 0),
660 DEFINE_PROP_END_OF_LIST(),
663 static void m48txx_sysbus_class_init(ObjectClass
*klass
, void *data
)
665 DeviceClass
*dc
= DEVICE_CLASS(klass
);
666 NvramClass
*nc
= NVRAM_CLASS(klass
);
668 dc
->realize
= m48t59_realize
;
669 dc
->reset
= m48t59_reset_sysbus
;
670 dc
->props
= m48t59_sysbus_properties
;
671 dc
->vmsd
= &vmstate_m48t59
;
672 nc
->read
= m48txx_sysbus_read
;
673 nc
->write
= m48txx_sysbus_write
;
674 nc
->toggle_lock
= m48txx_sysbus_toggle_lock
;
677 static void m48txx_sysbus_concrete_class_init(ObjectClass
*klass
, void *data
)
679 M48txxSysBusDeviceClass
*u
= M48TXX_SYS_BUS_CLASS(klass
);
680 M48txxInfo
*info
= data
;
685 static const TypeInfo nvram_info
= {
687 .parent
= TYPE_INTERFACE
,
688 .class_size
= sizeof(NvramClass
),
691 static const TypeInfo m48txx_sysbus_type_info
= {
692 .name
= TYPE_M48TXX_SYS_BUS
,
693 .parent
= TYPE_SYS_BUS_DEVICE
,
694 .instance_size
= sizeof(M48txxSysBusState
),
695 .instance_init
= m48t59_init1
,
697 .class_init
= m48txx_sysbus_class_init
,
698 .interfaces
= (InterfaceInfo
[]) {
704 static void m48t59_register_types(void)
706 TypeInfo sysbus_type_info
= {
707 .parent
= TYPE_M48TXX_SYS_BUS
,
708 .class_size
= sizeof(M48txxSysBusDeviceClass
),
709 .class_init
= m48txx_sysbus_concrete_class_init
,
713 type_register_static(&nvram_info
);
714 type_register_static(&m48txx_sysbus_type_info
);
716 for (i
= 0; i
< ARRAY_SIZE(m48txx_sysbus_info
); i
++) {
717 sysbus_type_info
.name
= m48txx_sysbus_info
[i
].bus_name
;
718 sysbus_type_info
.class_data
= &m48txx_sysbus_info
[i
];
719 type_register(&sysbus_type_info
);
723 type_init(m48t59_register_types
)