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
25 #include "qemu/osdep.h"
27 #include "hw/timer/m48t59.h"
28 #include "qapi/error.h"
29 #include "qemu/timer.h"
30 #include "sysemu/sysemu.h"
31 #include "hw/sysbus.h"
32 #include "exec/address-spaces.h"
35 #include "m48t59-internal.h"
37 #define TYPE_M48TXX_SYS_BUS "sysbus-m48txx"
38 #define M48TXX_SYS_BUS_GET_CLASS(obj) \
39 OBJECT_GET_CLASS(M48txxSysBusDeviceClass, (obj), TYPE_M48TXX_SYS_BUS)
40 #define M48TXX_SYS_BUS_CLASS(klass) \
41 OBJECT_CLASS_CHECK(M48txxSysBusDeviceClass, (klass), TYPE_M48TXX_SYS_BUS)
42 #define M48TXX_SYS_BUS(obj) \
43 OBJECT_CHECK(M48txxSysBusState, (obj), TYPE_M48TXX_SYS_BUS)
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
52 typedef struct M48txxSysBusState
{
53 SysBusDevice parent_obj
;
58 typedef struct M48txxSysBusDeviceClass
{
59 SysBusDeviceClass parent_class
;
61 } M48txxSysBusDeviceClass
;
63 static M48txxInfo m48txx_sysbus_info
[] = {
65 .bus_name
= "sysbus-m48t02",
69 .bus_name
= "sysbus-m48t08",
73 .bus_name
= "sysbus-m48t59",
80 /* Fake timer functions */
82 /* Alarm management */
83 static void alarm_cb (void *opaque
)
87 M48t59State
*NVRAM
= opaque
;
89 qemu_set_irq(NVRAM
->IRQ
, 1);
90 if ((NVRAM
->buffer
[0x1FF5] & 0x80) == 0 &&
91 (NVRAM
->buffer
[0x1FF4] & 0x80) == 0 &&
92 (NVRAM
->buffer
[0x1FF3] & 0x80) == 0 &&
93 (NVRAM
->buffer
[0x1FF2] & 0x80) == 0) {
94 /* Repeat once a month */
95 qemu_get_timedate(&tm
, NVRAM
->time_offset
);
97 if (tm
.tm_mon
== 13) {
101 next_time
= qemu_timedate_diff(&tm
) - NVRAM
->time_offset
;
102 } else 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 day */
107 next_time
= 24 * 60 * 60;
108 } else if ((NVRAM
->buffer
[0x1FF5] & 0x80) != 0 &&
109 (NVRAM
->buffer
[0x1FF4] & 0x80) != 0 &&
110 (NVRAM
->buffer
[0x1FF3] & 0x80) == 0 &&
111 (NVRAM
->buffer
[0x1FF2] & 0x80) == 0) {
112 /* Repeat once an hour */
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 minute */
121 /* Repeat once a second */
124 timer_mod(NVRAM
->alrm_timer
, qemu_clock_get_ns(rtc_clock
) +
126 qemu_set_irq(NVRAM
->IRQ
, 0);
129 static void set_alarm(M48t59State
*NVRAM
)
132 if (NVRAM
->alrm_timer
!= NULL
) {
133 timer_del(NVRAM
->alrm_timer
);
134 diff
= qemu_timedate_diff(&NVRAM
->alarm
) - NVRAM
->time_offset
;
136 timer_mod(NVRAM
->alrm_timer
, diff
* 1000);
140 /* RTC management helpers */
141 static inline void get_time(M48t59State
*NVRAM
, struct tm
*tm
)
143 qemu_get_timedate(tm
, NVRAM
->time_offset
);
146 static void set_time(M48t59State
*NVRAM
, struct tm
*tm
)
148 NVRAM
->time_offset
= qemu_timedate_diff(tm
);
152 /* Watchdog management */
153 static void watchdog_cb (void *opaque
)
155 M48t59State
*NVRAM
= opaque
;
157 NVRAM
->buffer
[0x1FF0] |= 0x80;
158 if (NVRAM
->buffer
[0x1FF7] & 0x80) {
159 NVRAM
->buffer
[0x1FF7] = 0x00;
160 NVRAM
->buffer
[0x1FFC] &= ~0x40;
161 /* May it be a hw CPU Reset instead ? */
162 qemu_system_reset_request(SHUTDOWN_CAUSE_GUEST_RESET
);
164 qemu_set_irq(NVRAM
->IRQ
, 1);
165 qemu_set_irq(NVRAM
->IRQ
, 0);
169 static void set_up_watchdog(M48t59State
*NVRAM
, uint8_t value
)
171 uint64_t interval
; /* in 1/16 seconds */
173 NVRAM
->buffer
[0x1FF0] &= ~0x80;
174 if (NVRAM
->wd_timer
!= NULL
) {
175 timer_del(NVRAM
->wd_timer
);
177 interval
= (1 << (2 * (value
& 0x03))) * ((value
>> 2) & 0x1F);
178 timer_mod(NVRAM
->wd_timer
, ((uint64_t)time(NULL
) * 1000) +
179 ((interval
* 1000) >> 4));
184 /* Direct access to NVRAM */
185 void m48t59_write(M48t59State
*NVRAM
, uint32_t addr
, uint32_t val
)
190 if (addr
> 0x1FF8 && addr
< 0x2000)
191 NVRAM_PRINTF("%s: 0x%08x => 0x%08x\n", __func__
, addr
, val
);
193 /* check for NVRAM access */
194 if ((NVRAM
->model
== 2 && addr
< 0x7f8) ||
195 (NVRAM
->model
== 8 && addr
< 0x1ff8) ||
196 (NVRAM
->model
== 59 && addr
< 0x1ff0)) {
203 /* flags register : read-only */
210 tmp
= from_bcd(val
& 0x7F);
211 if (tmp
>= 0 && tmp
<= 59) {
212 NVRAM
->alarm
.tm_sec
= tmp
;
213 NVRAM
->buffer
[0x1FF2] = val
;
219 tmp
= from_bcd(val
& 0x7F);
220 if (tmp
>= 0 && tmp
<= 59) {
221 NVRAM
->alarm
.tm_min
= tmp
;
222 NVRAM
->buffer
[0x1FF3] = val
;
228 tmp
= from_bcd(val
& 0x3F);
229 if (tmp
>= 0 && tmp
<= 23) {
230 NVRAM
->alarm
.tm_hour
= tmp
;
231 NVRAM
->buffer
[0x1FF4] = val
;
237 tmp
= from_bcd(val
& 0x3F);
239 NVRAM
->alarm
.tm_mday
= tmp
;
240 NVRAM
->buffer
[0x1FF5] = val
;
246 NVRAM
->buffer
[0x1FF6] = val
;
250 NVRAM
->buffer
[0x1FF7] = val
;
251 set_up_watchdog(NVRAM
, val
);
256 NVRAM
->buffer
[addr
] = (val
& ~0xA0) | 0x90;
261 tmp
= from_bcd(val
& 0x7F);
262 if (tmp
>= 0 && tmp
<= 59) {
263 get_time(NVRAM
, &tm
);
265 set_time(NVRAM
, &tm
);
267 if ((val
& 0x80) ^ (NVRAM
->buffer
[addr
] & 0x80)) {
269 NVRAM
->stop_time
= time(NULL
);
271 NVRAM
->time_offset
+= NVRAM
->stop_time
- time(NULL
);
272 NVRAM
->stop_time
= 0;
275 NVRAM
->buffer
[addr
] = val
& 0x80;
280 tmp
= from_bcd(val
& 0x7F);
281 if (tmp
>= 0 && tmp
<= 59) {
282 get_time(NVRAM
, &tm
);
284 set_time(NVRAM
, &tm
);
290 tmp
= from_bcd(val
& 0x3F);
291 if (tmp
>= 0 && tmp
<= 23) {
292 get_time(NVRAM
, &tm
);
294 set_time(NVRAM
, &tm
);
299 /* day of the week / century */
300 tmp
= from_bcd(val
& 0x07);
301 get_time(NVRAM
, &tm
);
303 set_time(NVRAM
, &tm
);
304 NVRAM
->buffer
[addr
] = val
& 0x40;
309 tmp
= from_bcd(val
& 0x3F);
311 get_time(NVRAM
, &tm
);
313 set_time(NVRAM
, &tm
);
319 tmp
= from_bcd(val
& 0x1F);
320 if (tmp
>= 1 && tmp
<= 12) {
321 get_time(NVRAM
, &tm
);
323 set_time(NVRAM
, &tm
);
330 if (tmp
>= 0 && tmp
<= 99) {
331 get_time(NVRAM
, &tm
);
332 tm
.tm_year
= from_bcd(val
) + NVRAM
->base_year
- 1900;
333 set_time(NVRAM
, &tm
);
337 /* Check lock registers state */
338 if (addr
>= 0x20 && addr
<= 0x2F && (NVRAM
->lock
& 1))
340 if (addr
>= 0x30 && addr
<= 0x3F && (NVRAM
->lock
& 2))
343 if (addr
< NVRAM
->size
) {
344 NVRAM
->buffer
[addr
] = val
& 0xFF;
350 uint32_t m48t59_read(M48t59State
*NVRAM
, uint32_t addr
)
353 uint32_t retval
= 0xFF;
355 /* check for NVRAM access */
356 if ((NVRAM
->model
== 2 && addr
< 0x078f) ||
357 (NVRAM
->model
== 8 && addr
< 0x1ff8) ||
358 (NVRAM
->model
== 59 && addr
< 0x1ff0)) {
387 /* A read resets the watchdog */
388 set_up_watchdog(NVRAM
, NVRAM
->buffer
[0x1FF7]);
397 get_time(NVRAM
, &tm
);
398 retval
= (NVRAM
->buffer
[addr
] & 0x80) | to_bcd(tm
.tm_sec
);
403 get_time(NVRAM
, &tm
);
404 retval
= to_bcd(tm
.tm_min
);
409 get_time(NVRAM
, &tm
);
410 retval
= to_bcd(tm
.tm_hour
);
414 /* day of the week / century */
415 get_time(NVRAM
, &tm
);
416 retval
= NVRAM
->buffer
[addr
] | tm
.tm_wday
;
421 get_time(NVRAM
, &tm
);
422 retval
= to_bcd(tm
.tm_mday
);
427 get_time(NVRAM
, &tm
);
428 retval
= to_bcd(tm
.tm_mon
+ 1);
433 get_time(NVRAM
, &tm
);
434 retval
= to_bcd((tm
.tm_year
+ 1900 - NVRAM
->base_year
) % 100);
437 /* Check lock registers state */
438 if (addr
>= 0x20 && addr
<= 0x2F && (NVRAM
->lock
& 1))
440 if (addr
>= 0x30 && addr
<= 0x3F && (NVRAM
->lock
& 2))
443 if (addr
< NVRAM
->size
) {
444 retval
= NVRAM
->buffer
[addr
];
448 if (addr
> 0x1FF9 && addr
< 0x2000)
449 NVRAM_PRINTF("%s: 0x%08x <= 0x%08x\n", __func__
, addr
, retval
);
454 /* IO access to NVRAM */
455 static void NVRAM_writeb(void *opaque
, hwaddr addr
, uint64_t val
,
458 M48t59State
*NVRAM
= opaque
;
460 NVRAM_PRINTF("%s: 0x%08x => 0x%08x\n", __func__
, addr
, val
);
463 NVRAM
->addr
&= ~0x00FF;
467 NVRAM
->addr
&= ~0xFF00;
468 NVRAM
->addr
|= val
<< 8;
471 m48t59_write(NVRAM
, NVRAM
->addr
, val
);
472 NVRAM
->addr
= 0x0000;
479 static uint64_t NVRAM_readb(void *opaque
, hwaddr addr
, unsigned size
)
481 M48t59State
*NVRAM
= opaque
;
486 retval
= m48t59_read(NVRAM
, NVRAM
->addr
);
492 NVRAM_PRINTF("%s: 0x%08x <= 0x%08x\n", __func__
, addr
, retval
);
497 static void nvram_writeb (void *opaque
, hwaddr addr
, uint32_t value
)
499 M48t59State
*NVRAM
= opaque
;
501 m48t59_write(NVRAM
, addr
, value
& 0xff);
504 static void nvram_writew (void *opaque
, hwaddr addr
, uint32_t value
)
506 M48t59State
*NVRAM
= opaque
;
508 m48t59_write(NVRAM
, addr
, (value
>> 8) & 0xff);
509 m48t59_write(NVRAM
, addr
+ 1, value
& 0xff);
512 static void nvram_writel (void *opaque
, hwaddr addr
, uint32_t value
)
514 M48t59State
*NVRAM
= opaque
;
516 m48t59_write(NVRAM
, addr
, (value
>> 24) & 0xff);
517 m48t59_write(NVRAM
, addr
+ 1, (value
>> 16) & 0xff);
518 m48t59_write(NVRAM
, addr
+ 2, (value
>> 8) & 0xff);
519 m48t59_write(NVRAM
, addr
+ 3, value
& 0xff);
522 static uint32_t nvram_readb (void *opaque
, hwaddr addr
)
524 M48t59State
*NVRAM
= opaque
;
526 return m48t59_read(NVRAM
, addr
);
529 static uint32_t nvram_readw (void *opaque
, hwaddr addr
)
531 M48t59State
*NVRAM
= opaque
;
534 retval
= m48t59_read(NVRAM
, addr
) << 8;
535 retval
|= m48t59_read(NVRAM
, addr
+ 1);
539 static uint32_t nvram_readl (void *opaque
, hwaddr addr
)
541 M48t59State
*NVRAM
= opaque
;
544 retval
= m48t59_read(NVRAM
, addr
) << 24;
545 retval
|= m48t59_read(NVRAM
, addr
+ 1) << 16;
546 retval
|= m48t59_read(NVRAM
, addr
+ 2) << 8;
547 retval
|= m48t59_read(NVRAM
, addr
+ 3);
551 static const MemoryRegionOps nvram_ops
= {
553 .read
= { nvram_readb
, nvram_readw
, nvram_readl
, },
554 .write
= { nvram_writeb
, nvram_writew
, nvram_writel
, },
556 .endianness
= DEVICE_NATIVE_ENDIAN
,
559 static const VMStateDescription vmstate_m48t59
= {
562 .minimum_version_id
= 1,
563 .fields
= (VMStateField
[]) {
564 VMSTATE_UINT8(lock
, M48t59State
),
565 VMSTATE_UINT16(addr
, M48t59State
),
566 VMSTATE_VBUFFER_UINT32(buffer
, M48t59State
, 0, NULL
, size
),
567 VMSTATE_END_OF_LIST()
571 void m48t59_reset_common(M48t59State
*NVRAM
)
575 if (NVRAM
->alrm_timer
!= NULL
)
576 timer_del(NVRAM
->alrm_timer
);
578 if (NVRAM
->wd_timer
!= NULL
)
579 timer_del(NVRAM
->wd_timer
);
582 static void m48t59_reset_sysbus(DeviceState
*d
)
584 M48txxSysBusState
*sys
= M48TXX_SYS_BUS(d
);
585 M48t59State
*NVRAM
= &sys
->state
;
587 m48t59_reset_common(NVRAM
);
590 const MemoryRegionOps m48t59_io_ops
= {
592 .write
= NVRAM_writeb
,
594 .min_access_size
= 1,
595 .max_access_size
= 1,
597 .endianness
= DEVICE_LITTLE_ENDIAN
,
600 /* Initialisation routine */
601 Nvram
*m48t59_init(qemu_irq IRQ
, hwaddr mem_base
,
602 uint32_t io_base
, uint16_t size
, int base_year
,
609 for (i
= 0; i
< ARRAY_SIZE(m48txx_sysbus_info
); i
++) {
610 if (m48txx_sysbus_info
[i
].size
!= size
||
611 m48txx_sysbus_info
[i
].model
!= model
) {
615 dev
= qdev_create(NULL
, m48txx_sysbus_info
[i
].bus_name
);
616 qdev_prop_set_int32(dev
, "base-year", base_year
);
617 qdev_init_nofail(dev
);
618 s
= SYS_BUS_DEVICE(dev
);
619 sysbus_connect_irq(s
, 0, IRQ
);
621 memory_region_add_subregion(get_system_io(), io_base
,
622 sysbus_mmio_get_region(s
, 1));
625 sysbus_mmio_map(s
, 0, mem_base
);
635 void m48t59_realize_common(M48t59State
*s
, Error
**errp
)
637 s
->buffer
= g_malloc0(s
->size
);
638 if (s
->model
== 59) {
639 s
->alrm_timer
= timer_new_ns(rtc_clock
, &alarm_cb
, s
);
640 s
->wd_timer
= timer_new_ns(QEMU_CLOCK_VIRTUAL
, &watchdog_cb
, s
);
642 qemu_get_timedate(&s
->alarm
, 0);
645 static void m48t59_init1(Object
*obj
)
647 M48txxSysBusDeviceClass
*u
= M48TXX_SYS_BUS_GET_CLASS(obj
);
648 M48txxSysBusState
*d
= M48TXX_SYS_BUS(obj
);
649 SysBusDevice
*dev
= SYS_BUS_DEVICE(obj
);
650 M48t59State
*s
= &d
->state
;
652 s
->model
= u
->info
.model
;
653 s
->size
= u
->info
.size
;
654 sysbus_init_irq(dev
, &s
->IRQ
);
656 memory_region_init_io(&s
->iomem
, obj
, &nvram_ops
, s
, "m48t59.nvram",
658 memory_region_init_io(&d
->io
, obj
, &m48t59_io_ops
, s
, "m48t59", 4);
661 static void m48t59_realize(DeviceState
*dev
, Error
**errp
)
663 M48txxSysBusState
*d
= M48TXX_SYS_BUS(dev
);
664 M48t59State
*s
= &d
->state
;
665 SysBusDevice
*sbd
= SYS_BUS_DEVICE(dev
);
667 sysbus_init_mmio(sbd
, &s
->iomem
);
668 sysbus_init_mmio(sbd
, &d
->io
);
669 m48t59_realize_common(s
, errp
);
672 static uint32_t m48txx_sysbus_read(Nvram
*obj
, uint32_t addr
)
674 M48txxSysBusState
*d
= M48TXX_SYS_BUS(obj
);
675 return m48t59_read(&d
->state
, addr
);
678 static void m48txx_sysbus_write(Nvram
*obj
, uint32_t addr
, uint32_t val
)
680 M48txxSysBusState
*d
= M48TXX_SYS_BUS(obj
);
681 m48t59_write(&d
->state
, addr
, val
);
684 static void m48txx_sysbus_toggle_lock(Nvram
*obj
, int lock
)
686 M48txxSysBusState
*d
= M48TXX_SYS_BUS(obj
);
687 m48t59_toggle_lock(&d
->state
, lock
);
690 static Property m48t59_sysbus_properties
[] = {
691 DEFINE_PROP_INT32("base-year", M48txxSysBusState
, state
.base_year
, 0),
692 DEFINE_PROP_END_OF_LIST(),
695 static void m48txx_sysbus_class_init(ObjectClass
*klass
, void *data
)
697 DeviceClass
*dc
= DEVICE_CLASS(klass
);
698 NvramClass
*nc
= NVRAM_CLASS(klass
);
700 dc
->realize
= m48t59_realize
;
701 dc
->reset
= m48t59_reset_sysbus
;
702 dc
->props
= m48t59_sysbus_properties
;
703 dc
->vmsd
= &vmstate_m48t59
;
704 nc
->read
= m48txx_sysbus_read
;
705 nc
->write
= m48txx_sysbus_write
;
706 nc
->toggle_lock
= m48txx_sysbus_toggle_lock
;
709 static void m48txx_sysbus_concrete_class_init(ObjectClass
*klass
, void *data
)
711 M48txxSysBusDeviceClass
*u
= M48TXX_SYS_BUS_CLASS(klass
);
712 M48txxInfo
*info
= data
;
717 static const TypeInfo nvram_info
= {
719 .parent
= TYPE_INTERFACE
,
720 .class_size
= sizeof(NvramClass
),
723 static const TypeInfo m48txx_sysbus_type_info
= {
724 .name
= TYPE_M48TXX_SYS_BUS
,
725 .parent
= TYPE_SYS_BUS_DEVICE
,
726 .instance_size
= sizeof(M48txxSysBusState
),
727 .instance_init
= m48t59_init1
,
729 .class_init
= m48txx_sysbus_class_init
,
730 .interfaces
= (InterfaceInfo
[]) {
736 static void m48t59_register_types(void)
738 TypeInfo sysbus_type_info
= {
739 .parent
= TYPE_M48TXX_SYS_BUS
,
740 .class_size
= sizeof(M48txxSysBusDeviceClass
),
741 .class_init
= m48txx_sysbus_concrete_class_init
,
745 type_register_static(&nvram_info
);
746 type_register_static(&m48txx_sysbus_type_info
);
748 for (i
= 0; i
< ARRAY_SIZE(m48txx_sysbus_info
); i
++) {
749 sysbus_type_info
.name
= m48txx_sysbus_info
[i
].bus_name
;
750 sysbus_type_info
.class_data
= &m48txx_sysbus_info
[i
];
751 type_register(&sysbus_type_info
);
755 type_init(m48t59_register_types
)