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"
30 #include "hw/timer/m48t59.h"
31 #include "qemu/timer.h"
32 #include "sysemu/sysemu.h"
33 #include "hw/sysbus.h"
34 #include "exec/address-spaces.h"
36 #include "qemu/module.h"
38 #include "m48t59-internal.h"
40 #define TYPE_M48TXX_SYS_BUS "sysbus-m48txx"
41 #define M48TXX_SYS_BUS_GET_CLASS(obj) \
42 OBJECT_GET_CLASS(M48txxSysBusDeviceClass, (obj), TYPE_M48TXX_SYS_BUS)
43 #define M48TXX_SYS_BUS_CLASS(klass) \
44 OBJECT_CLASS_CHECK(M48txxSysBusDeviceClass, (klass), TYPE_M48TXX_SYS_BUS)
45 #define M48TXX_SYS_BUS(obj) \
46 OBJECT_CHECK(M48txxSysBusState, (obj), TYPE_M48TXX_SYS_BUS)
50 * http://www.st.com/stonline/products/literature/ds/2410/m48t02.pdf
51 * http://www.st.com/stonline/products/literature/ds/2411/m48t08.pdf
52 * http://www.st.com/stonline/products/literature/od/7001/m48t59y.pdf
55 typedef struct M48txxSysBusState
{
56 SysBusDevice parent_obj
;
61 typedef struct M48txxSysBusDeviceClass
{
62 SysBusDeviceClass parent_class
;
64 } M48txxSysBusDeviceClass
;
66 static M48txxInfo m48txx_sysbus_info
[] = {
68 .bus_name
= "sysbus-m48t02",
72 .bus_name
= "sysbus-m48t08",
76 .bus_name
= "sysbus-m48t59",
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 timer_mod(NVRAM
->alrm_timer
, qemu_clock_get_ns(rtc_clock
) +
129 qemu_set_irq(NVRAM
->IRQ
, 0);
132 static void set_alarm(M48t59State
*NVRAM
)
135 if (NVRAM
->alrm_timer
!= NULL
) {
136 timer_del(NVRAM
->alrm_timer
);
137 diff
= qemu_timedate_diff(&NVRAM
->alarm
) - NVRAM
->time_offset
;
139 timer_mod(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(SHUTDOWN_CAUSE_GUEST_RESET
);
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 timer_del(NVRAM
->wd_timer
);
180 interval
= (1 << (2 * (value
& 0x03))) * ((value
>> 2) & 0x1F);
181 timer_mod(NVRAM
->wd_timer
, ((uint64_t)time(NULL
) * 1000) +
182 ((interval
* 1000) >> 4));
187 /* Direct access to NVRAM */
188 void m48t59_write(M48t59State
*NVRAM
, uint32_t addr
, uint32_t val
)
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
->model
== 2 && addr
< 0x7f8) ||
198 (NVRAM
->model
== 8 && addr
< 0x1ff8) ||
199 (NVRAM
->model
== 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
& 0x3F);
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
& 0x3F);
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 tm
.tm_year
= from_bcd(val
) + NVRAM
->base_year
- 1900;
336 set_time(NVRAM
, &tm
);
340 /* Check lock registers state */
341 if (addr
>= 0x20 && addr
<= 0x2F && (NVRAM
->lock
& 1))
343 if (addr
>= 0x30 && addr
<= 0x3F && (NVRAM
->lock
& 2))
346 if (addr
< NVRAM
->size
) {
347 NVRAM
->buffer
[addr
] = val
& 0xFF;
353 uint32_t m48t59_read(M48t59State
*NVRAM
, uint32_t addr
)
356 uint32_t retval
= 0xFF;
358 /* check for NVRAM access */
359 if ((NVRAM
->model
== 2 && addr
< 0x078f) ||
360 (NVRAM
->model
== 8 && addr
< 0x1ff8) ||
361 (NVRAM
->model
== 59 && addr
< 0x1ff0)) {
390 /* A read resets the watchdog */
391 set_up_watchdog(NVRAM
, NVRAM
->buffer
[0x1FF7]);
400 get_time(NVRAM
, &tm
);
401 retval
= (NVRAM
->buffer
[addr
] & 0x80) | to_bcd(tm
.tm_sec
);
406 get_time(NVRAM
, &tm
);
407 retval
= to_bcd(tm
.tm_min
);
412 get_time(NVRAM
, &tm
);
413 retval
= to_bcd(tm
.tm_hour
);
417 /* day of the week / century */
418 get_time(NVRAM
, &tm
);
419 retval
= NVRAM
->buffer
[addr
] | tm
.tm_wday
;
424 get_time(NVRAM
, &tm
);
425 retval
= to_bcd(tm
.tm_mday
);
430 get_time(NVRAM
, &tm
);
431 retval
= to_bcd(tm
.tm_mon
+ 1);
436 get_time(NVRAM
, &tm
);
437 retval
= to_bcd((tm
.tm_year
+ 1900 - NVRAM
->base_year
) % 100);
440 /* Check lock registers state */
441 if (addr
>= 0x20 && addr
<= 0x2F && (NVRAM
->lock
& 1))
443 if (addr
>= 0x30 && addr
<= 0x3F && (NVRAM
->lock
& 2))
446 if (addr
< NVRAM
->size
) {
447 retval
= NVRAM
->buffer
[addr
];
451 if (addr
> 0x1FF9 && addr
< 0x2000)
452 NVRAM_PRINTF("%s: 0x%08x <= 0x%08x\n", __func__
, addr
, retval
);
457 /* IO access to NVRAM */
458 static void NVRAM_writeb(void *opaque
, hwaddr addr
, uint64_t val
,
461 M48t59State
*NVRAM
= opaque
;
463 NVRAM_PRINTF("%s: 0x%"HWADDR_PRIx
" => 0x%"PRIx64
"\n", __func__
, addr
, val
);
466 NVRAM
->addr
&= ~0x00FF;
470 NVRAM
->addr
&= ~0xFF00;
471 NVRAM
->addr
|= val
<< 8;
474 m48t59_write(NVRAM
, NVRAM
->addr
, val
);
475 NVRAM
->addr
= 0x0000;
482 static uint64_t NVRAM_readb(void *opaque
, hwaddr addr
, unsigned size
)
484 M48t59State
*NVRAM
= opaque
;
489 retval
= m48t59_read(NVRAM
, NVRAM
->addr
);
495 NVRAM_PRINTF("%s: 0x%"HWADDR_PRIx
" <= 0x%08x\n", __func__
, addr
, retval
);
500 static uint64_t nvram_read(void *opaque
, hwaddr addr
, unsigned size
)
502 M48t59State
*NVRAM
= opaque
;
504 return m48t59_read(NVRAM
, addr
);
507 static void nvram_write(void *opaque
, hwaddr addr
, uint64_t value
,
510 M48t59State
*NVRAM
= opaque
;
512 return m48t59_write(NVRAM
, addr
, value
);
515 static const MemoryRegionOps nvram_ops
= {
517 .write
= nvram_write
,
518 .impl
.min_access_size
= 1,
519 .impl
.max_access_size
= 1,
520 .valid
.min_access_size
= 1,
521 .valid
.max_access_size
= 4,
522 .endianness
= DEVICE_BIG_ENDIAN
,
525 static const VMStateDescription vmstate_m48t59
= {
528 .minimum_version_id
= 1,
529 .fields
= (VMStateField
[]) {
530 VMSTATE_UINT8(lock
, M48t59State
),
531 VMSTATE_UINT16(addr
, M48t59State
),
532 VMSTATE_VBUFFER_UINT32(buffer
, M48t59State
, 0, NULL
, size
),
533 VMSTATE_END_OF_LIST()
537 void m48t59_reset_common(M48t59State
*NVRAM
)
541 if (NVRAM
->alrm_timer
!= NULL
)
542 timer_del(NVRAM
->alrm_timer
);
544 if (NVRAM
->wd_timer
!= NULL
)
545 timer_del(NVRAM
->wd_timer
);
548 static void m48t59_reset_sysbus(DeviceState
*d
)
550 M48txxSysBusState
*sys
= M48TXX_SYS_BUS(d
);
551 M48t59State
*NVRAM
= &sys
->state
;
553 m48t59_reset_common(NVRAM
);
556 const MemoryRegionOps m48t59_io_ops
= {
558 .write
= NVRAM_writeb
,
560 .min_access_size
= 1,
561 .max_access_size
= 1,
563 .endianness
= DEVICE_LITTLE_ENDIAN
,
566 /* Initialisation routine */
567 Nvram
*m48t59_init(qemu_irq IRQ
, hwaddr mem_base
,
568 uint32_t io_base
, uint16_t size
, int base_year
,
575 for (i
= 0; i
< ARRAY_SIZE(m48txx_sysbus_info
); i
++) {
576 if (m48txx_sysbus_info
[i
].size
!= size
||
577 m48txx_sysbus_info
[i
].model
!= model
) {
581 dev
= qdev_create(NULL
, m48txx_sysbus_info
[i
].bus_name
);
582 qdev_prop_set_int32(dev
, "base-year", base_year
);
583 qdev_init_nofail(dev
);
584 s
= SYS_BUS_DEVICE(dev
);
585 sysbus_connect_irq(s
, 0, IRQ
);
587 memory_region_add_subregion(get_system_io(), io_base
,
588 sysbus_mmio_get_region(s
, 1));
591 sysbus_mmio_map(s
, 0, mem_base
);
601 void m48t59_realize_common(M48t59State
*s
, Error
**errp
)
603 s
->buffer
= g_malloc0(s
->size
);
604 if (s
->model
== 59) {
605 s
->alrm_timer
= timer_new_ns(rtc_clock
, &alarm_cb
, s
);
606 s
->wd_timer
= timer_new_ns(QEMU_CLOCK_VIRTUAL
, &watchdog_cb
, s
);
608 qemu_get_timedate(&s
->alarm
, 0);
611 static void m48t59_init1(Object
*obj
)
613 M48txxSysBusDeviceClass
*u
= M48TXX_SYS_BUS_GET_CLASS(obj
);
614 M48txxSysBusState
*d
= M48TXX_SYS_BUS(obj
);
615 SysBusDevice
*dev
= SYS_BUS_DEVICE(obj
);
616 M48t59State
*s
= &d
->state
;
618 s
->model
= u
->info
.model
;
619 s
->size
= u
->info
.size
;
620 sysbus_init_irq(dev
, &s
->IRQ
);
622 memory_region_init_io(&s
->iomem
, obj
, &nvram_ops
, s
, "m48t59.nvram",
624 memory_region_init_io(&d
->io
, obj
, &m48t59_io_ops
, s
, "m48t59", 4);
627 static void m48t59_realize(DeviceState
*dev
, Error
**errp
)
629 M48txxSysBusState
*d
= M48TXX_SYS_BUS(dev
);
630 M48t59State
*s
= &d
->state
;
631 SysBusDevice
*sbd
= SYS_BUS_DEVICE(dev
);
633 sysbus_init_mmio(sbd
, &s
->iomem
);
634 sysbus_init_mmio(sbd
, &d
->io
);
635 m48t59_realize_common(s
, errp
);
638 static uint32_t m48txx_sysbus_read(Nvram
*obj
, uint32_t addr
)
640 M48txxSysBusState
*d
= M48TXX_SYS_BUS(obj
);
641 return m48t59_read(&d
->state
, addr
);
644 static void m48txx_sysbus_write(Nvram
*obj
, uint32_t addr
, uint32_t val
)
646 M48txxSysBusState
*d
= M48TXX_SYS_BUS(obj
);
647 m48t59_write(&d
->state
, addr
, val
);
650 static void m48txx_sysbus_toggle_lock(Nvram
*obj
, int lock
)
652 M48txxSysBusState
*d
= M48TXX_SYS_BUS(obj
);
653 m48t59_toggle_lock(&d
->state
, lock
);
656 static Property m48t59_sysbus_properties
[] = {
657 DEFINE_PROP_INT32("base-year", M48txxSysBusState
, state
.base_year
, 0),
658 DEFINE_PROP_END_OF_LIST(),
661 static void m48txx_sysbus_class_init(ObjectClass
*klass
, void *data
)
663 DeviceClass
*dc
= DEVICE_CLASS(klass
);
664 NvramClass
*nc
= NVRAM_CLASS(klass
);
666 dc
->realize
= m48t59_realize
;
667 dc
->reset
= m48t59_reset_sysbus
;
668 dc
->props
= m48t59_sysbus_properties
;
669 dc
->vmsd
= &vmstate_m48t59
;
670 nc
->read
= m48txx_sysbus_read
;
671 nc
->write
= m48txx_sysbus_write
;
672 nc
->toggle_lock
= m48txx_sysbus_toggle_lock
;
675 static void m48txx_sysbus_concrete_class_init(ObjectClass
*klass
, void *data
)
677 M48txxSysBusDeviceClass
*u
= M48TXX_SYS_BUS_CLASS(klass
);
678 M48txxInfo
*info
= data
;
683 static const TypeInfo nvram_info
= {
685 .parent
= TYPE_INTERFACE
,
686 .class_size
= sizeof(NvramClass
),
689 static const TypeInfo m48txx_sysbus_type_info
= {
690 .name
= TYPE_M48TXX_SYS_BUS
,
691 .parent
= TYPE_SYS_BUS_DEVICE
,
692 .instance_size
= sizeof(M48txxSysBusState
),
693 .instance_init
= m48t59_init1
,
695 .class_init
= m48txx_sysbus_class_init
,
696 .interfaces
= (InterfaceInfo
[]) {
702 static void m48t59_register_types(void)
704 TypeInfo sysbus_type_info
= {
705 .parent
= TYPE_M48TXX_SYS_BUS
,
706 .class_size
= sizeof(M48txxSysBusDeviceClass
),
707 .class_init
= m48txx_sysbus_concrete_class_init
,
711 type_register_static(&nvram_info
);
712 type_register_static(&m48txx_sysbus_type_info
);
714 for (i
= 0; i
< ARRAY_SIZE(m48txx_sysbus_info
); i
++) {
715 sysbus_type_info
.name
= m48txx_sysbus_info
[i
].bus_name
;
716 sysbus_type_info
.class_data
= &m48txx_sysbus_info
[i
];
717 type_register(&sysbus_type_info
);
721 type_init(m48t59_register_types
)