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 QEMUTimer
*alrm_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 #define SYSBUS_M48T59(obj) \
87 OBJECT_CHECK(M48t59SysBusState, (obj), TYPE_SYSBUS_M48T59)
89 typedef struct M48t59SysBusState
{
90 SysBusDevice parent_obj
;
96 /* Fake timer functions */
98 /* Alarm management */
99 static void alarm_cb (void *opaque
)
103 M48t59State
*NVRAM
= opaque
;
105 qemu_set_irq(NVRAM
->IRQ
, 1);
106 if ((NVRAM
->buffer
[0x1FF5] & 0x80) == 0 &&
107 (NVRAM
->buffer
[0x1FF4] & 0x80) == 0 &&
108 (NVRAM
->buffer
[0x1FF3] & 0x80) == 0 &&
109 (NVRAM
->buffer
[0x1FF2] & 0x80) == 0) {
110 /* Repeat once a month */
111 qemu_get_timedate(&tm
, NVRAM
->time_offset
);
113 if (tm
.tm_mon
== 13) {
117 next_time
= qemu_timedate_diff(&tm
) - NVRAM
->time_offset
;
118 } else if ((NVRAM
->buffer
[0x1FF5] & 0x80) != 0 &&
119 (NVRAM
->buffer
[0x1FF4] & 0x80) == 0 &&
120 (NVRAM
->buffer
[0x1FF3] & 0x80) == 0 &&
121 (NVRAM
->buffer
[0x1FF2] & 0x80) == 0) {
122 /* Repeat once a day */
123 next_time
= 24 * 60 * 60;
124 } else if ((NVRAM
->buffer
[0x1FF5] & 0x80) != 0 &&
125 (NVRAM
->buffer
[0x1FF4] & 0x80) != 0 &&
126 (NVRAM
->buffer
[0x1FF3] & 0x80) == 0 &&
127 (NVRAM
->buffer
[0x1FF2] & 0x80) == 0) {
128 /* Repeat once an hour */
130 } else if ((NVRAM
->buffer
[0x1FF5] & 0x80) != 0 &&
131 (NVRAM
->buffer
[0x1FF4] & 0x80) != 0 &&
132 (NVRAM
->buffer
[0x1FF3] & 0x80) != 0 &&
133 (NVRAM
->buffer
[0x1FF2] & 0x80) == 0) {
134 /* Repeat once a minute */
137 /* Repeat once a second */
140 timer_mod(NVRAM
->alrm_timer
, qemu_clock_get_ns(rtc_clock
) +
142 qemu_set_irq(NVRAM
->IRQ
, 0);
145 static void set_alarm(M48t59State
*NVRAM
)
148 if (NVRAM
->alrm_timer
!= NULL
) {
149 timer_del(NVRAM
->alrm_timer
);
150 diff
= qemu_timedate_diff(&NVRAM
->alarm
) - NVRAM
->time_offset
;
152 timer_mod(NVRAM
->alrm_timer
, diff
* 1000);
156 /* RTC management helpers */
157 static inline void get_time(M48t59State
*NVRAM
, struct tm
*tm
)
159 qemu_get_timedate(tm
, NVRAM
->time_offset
);
162 static void set_time(M48t59State
*NVRAM
, struct tm
*tm
)
164 NVRAM
->time_offset
= qemu_timedate_diff(tm
);
168 /* Watchdog management */
169 static void watchdog_cb (void *opaque
)
171 M48t59State
*NVRAM
= opaque
;
173 NVRAM
->buffer
[0x1FF0] |= 0x80;
174 if (NVRAM
->buffer
[0x1FF7] & 0x80) {
175 NVRAM
->buffer
[0x1FF7] = 0x00;
176 NVRAM
->buffer
[0x1FFC] &= ~0x40;
177 /* May it be a hw CPU Reset instead ? */
178 qemu_system_reset_request();
180 qemu_set_irq(NVRAM
->IRQ
, 1);
181 qemu_set_irq(NVRAM
->IRQ
, 0);
185 static void set_up_watchdog(M48t59State
*NVRAM
, uint8_t value
)
187 uint64_t interval
; /* in 1/16 seconds */
189 NVRAM
->buffer
[0x1FF0] &= ~0x80;
190 if (NVRAM
->wd_timer
!= NULL
) {
191 timer_del(NVRAM
->wd_timer
);
193 interval
= (1 << (2 * (value
& 0x03))) * ((value
>> 2) & 0x1F);
194 timer_mod(NVRAM
->wd_timer
, ((uint64_t)time(NULL
) * 1000) +
195 ((interval
* 1000) >> 4));
200 /* Direct access to NVRAM */
201 void m48t59_write (void *opaque
, uint32_t addr
, uint32_t val
)
203 M48t59State
*NVRAM
= opaque
;
207 if (addr
> 0x1FF8 && addr
< 0x2000)
208 NVRAM_PRINTF("%s: 0x%08x => 0x%08x\n", __func__
, addr
, val
);
210 /* check for NVRAM access */
211 if ((NVRAM
->model
== 2 && addr
< 0x7f8) ||
212 (NVRAM
->model
== 8 && addr
< 0x1ff8) ||
213 (NVRAM
->model
== 59 && addr
< 0x1ff0)) {
220 /* flags register : read-only */
227 tmp
= from_bcd(val
& 0x7F);
228 if (tmp
>= 0 && tmp
<= 59) {
229 NVRAM
->alarm
.tm_sec
= tmp
;
230 NVRAM
->buffer
[0x1FF2] = val
;
236 tmp
= from_bcd(val
& 0x7F);
237 if (tmp
>= 0 && tmp
<= 59) {
238 NVRAM
->alarm
.tm_min
= tmp
;
239 NVRAM
->buffer
[0x1FF3] = val
;
245 tmp
= from_bcd(val
& 0x3F);
246 if (tmp
>= 0 && tmp
<= 23) {
247 NVRAM
->alarm
.tm_hour
= tmp
;
248 NVRAM
->buffer
[0x1FF4] = val
;
254 tmp
= from_bcd(val
& 0x3F);
256 NVRAM
->alarm
.tm_mday
= tmp
;
257 NVRAM
->buffer
[0x1FF5] = val
;
263 NVRAM
->buffer
[0x1FF6] = val
;
267 NVRAM
->buffer
[0x1FF7] = val
;
268 set_up_watchdog(NVRAM
, val
);
273 NVRAM
->buffer
[addr
] = (val
& ~0xA0) | 0x90;
278 tmp
= from_bcd(val
& 0x7F);
279 if (tmp
>= 0 && tmp
<= 59) {
280 get_time(NVRAM
, &tm
);
282 set_time(NVRAM
, &tm
);
284 if ((val
& 0x80) ^ (NVRAM
->buffer
[addr
] & 0x80)) {
286 NVRAM
->stop_time
= time(NULL
);
288 NVRAM
->time_offset
+= NVRAM
->stop_time
- time(NULL
);
289 NVRAM
->stop_time
= 0;
292 NVRAM
->buffer
[addr
] = val
& 0x80;
297 tmp
= from_bcd(val
& 0x7F);
298 if (tmp
>= 0 && tmp
<= 59) {
299 get_time(NVRAM
, &tm
);
301 set_time(NVRAM
, &tm
);
307 tmp
= from_bcd(val
& 0x3F);
308 if (tmp
>= 0 && tmp
<= 23) {
309 get_time(NVRAM
, &tm
);
311 set_time(NVRAM
, &tm
);
316 /* day of the week / century */
317 tmp
= from_bcd(val
& 0x07);
318 get_time(NVRAM
, &tm
);
320 set_time(NVRAM
, &tm
);
321 NVRAM
->buffer
[addr
] = val
& 0x40;
326 tmp
= from_bcd(val
& 0x3F);
328 get_time(NVRAM
, &tm
);
330 set_time(NVRAM
, &tm
);
336 tmp
= from_bcd(val
& 0x1F);
337 if (tmp
>= 1 && tmp
<= 12) {
338 get_time(NVRAM
, &tm
);
340 set_time(NVRAM
, &tm
);
347 if (tmp
>= 0 && tmp
<= 99) {
348 get_time(NVRAM
, &tm
);
349 if (NVRAM
->model
== 8) {
350 tm
.tm_year
= from_bcd(val
) + 68; // Base year is 1968
352 tm
.tm_year
= from_bcd(val
);
354 set_time(NVRAM
, &tm
);
358 /* Check lock registers state */
359 if (addr
>= 0x20 && addr
<= 0x2F && (NVRAM
->lock
& 1))
361 if (addr
>= 0x30 && addr
<= 0x3F && (NVRAM
->lock
& 2))
364 if (addr
< NVRAM
->size
) {
365 NVRAM
->buffer
[addr
] = val
& 0xFF;
371 uint32_t m48t59_read (void *opaque
, uint32_t addr
)
373 M48t59State
*NVRAM
= opaque
;
375 uint32_t retval
= 0xFF;
377 /* check for NVRAM access */
378 if ((NVRAM
->model
== 2 && addr
< 0x078f) ||
379 (NVRAM
->model
== 8 && addr
< 0x1ff8) ||
380 (NVRAM
->model
== 59 && addr
< 0x1ff0)) {
409 /* A read resets the watchdog */
410 set_up_watchdog(NVRAM
, NVRAM
->buffer
[0x1FF7]);
419 get_time(NVRAM
, &tm
);
420 retval
= (NVRAM
->buffer
[addr
] & 0x80) | to_bcd(tm
.tm_sec
);
425 get_time(NVRAM
, &tm
);
426 retval
= to_bcd(tm
.tm_min
);
431 get_time(NVRAM
, &tm
);
432 retval
= to_bcd(tm
.tm_hour
);
436 /* day of the week / century */
437 get_time(NVRAM
, &tm
);
438 retval
= NVRAM
->buffer
[addr
] | tm
.tm_wday
;
443 get_time(NVRAM
, &tm
);
444 retval
= to_bcd(tm
.tm_mday
);
449 get_time(NVRAM
, &tm
);
450 retval
= to_bcd(tm
.tm_mon
+ 1);
455 get_time(NVRAM
, &tm
);
456 if (NVRAM
->model
== 8) {
457 retval
= to_bcd(tm
.tm_year
- 68); // Base year is 1968
459 retval
= to_bcd(tm
.tm_year
);
463 /* Check lock registers state */
464 if (addr
>= 0x20 && addr
<= 0x2F && (NVRAM
->lock
& 1))
466 if (addr
>= 0x30 && addr
<= 0x3F && (NVRAM
->lock
& 2))
469 if (addr
< NVRAM
->size
) {
470 retval
= NVRAM
->buffer
[addr
];
474 if (addr
> 0x1FF9 && addr
< 0x2000)
475 NVRAM_PRINTF("%s: 0x%08x <= 0x%08x\n", __func__
, addr
, retval
);
480 void m48t59_toggle_lock (void *opaque
, int lock
)
482 M48t59State
*NVRAM
= opaque
;
484 NVRAM
->lock
^= 1 << lock
;
487 /* IO access to NVRAM */
488 static void NVRAM_writeb(void *opaque
, hwaddr addr
, uint64_t val
,
491 M48t59State
*NVRAM
= opaque
;
493 NVRAM_PRINTF("%s: 0x%08x => 0x%08x\n", __func__
, addr
, val
);
496 NVRAM
->addr
&= ~0x00FF;
500 NVRAM
->addr
&= ~0xFF00;
501 NVRAM
->addr
|= val
<< 8;
504 m48t59_write(NVRAM
, NVRAM
->addr
, val
);
505 NVRAM
->addr
= 0x0000;
512 static uint64_t NVRAM_readb(void *opaque
, hwaddr addr
, unsigned size
)
514 M48t59State
*NVRAM
= opaque
;
519 retval
= m48t59_read(NVRAM
, NVRAM
->addr
);
525 NVRAM_PRINTF("%s: 0x%08x <= 0x%08x\n", __func__
, addr
, retval
);
530 static void nvram_writeb (void *opaque
, hwaddr addr
, uint32_t value
)
532 M48t59State
*NVRAM
= opaque
;
534 m48t59_write(NVRAM
, addr
, value
& 0xff);
537 static void nvram_writew (void *opaque
, hwaddr addr
, uint32_t value
)
539 M48t59State
*NVRAM
= opaque
;
541 m48t59_write(NVRAM
, addr
, (value
>> 8) & 0xff);
542 m48t59_write(NVRAM
, addr
+ 1, value
& 0xff);
545 static void nvram_writel (void *opaque
, hwaddr addr
, uint32_t value
)
547 M48t59State
*NVRAM
= opaque
;
549 m48t59_write(NVRAM
, addr
, (value
>> 24) & 0xff);
550 m48t59_write(NVRAM
, addr
+ 1, (value
>> 16) & 0xff);
551 m48t59_write(NVRAM
, addr
+ 2, (value
>> 8) & 0xff);
552 m48t59_write(NVRAM
, addr
+ 3, value
& 0xff);
555 static uint32_t nvram_readb (void *opaque
, hwaddr addr
)
557 M48t59State
*NVRAM
= opaque
;
560 retval
= m48t59_read(NVRAM
, addr
);
564 static uint32_t nvram_readw (void *opaque
, hwaddr addr
)
566 M48t59State
*NVRAM
= opaque
;
569 retval
= m48t59_read(NVRAM
, addr
) << 8;
570 retval
|= m48t59_read(NVRAM
, addr
+ 1);
574 static uint32_t nvram_readl (void *opaque
, hwaddr addr
)
576 M48t59State
*NVRAM
= opaque
;
579 retval
= m48t59_read(NVRAM
, addr
) << 24;
580 retval
|= m48t59_read(NVRAM
, addr
+ 1) << 16;
581 retval
|= m48t59_read(NVRAM
, addr
+ 2) << 8;
582 retval
|= m48t59_read(NVRAM
, addr
+ 3);
586 static const MemoryRegionOps nvram_ops
= {
588 .read
= { nvram_readb
, nvram_readw
, nvram_readl
, },
589 .write
= { nvram_writeb
, nvram_writew
, nvram_writel
, },
591 .endianness
= DEVICE_NATIVE_ENDIAN
,
594 static const VMStateDescription vmstate_m48t59
= {
597 .minimum_version_id
= 1,
598 .minimum_version_id_old
= 1,
599 .fields
= (VMStateField
[]) {
600 VMSTATE_UINT8(lock
, M48t59State
),
601 VMSTATE_UINT16(addr
, M48t59State
),
602 VMSTATE_VBUFFER_UINT32(buffer
, M48t59State
, 0, NULL
, 0, size
),
603 VMSTATE_END_OF_LIST()
607 static void m48t59_reset_common(M48t59State
*NVRAM
)
611 if (NVRAM
->alrm_timer
!= NULL
)
612 timer_del(NVRAM
->alrm_timer
);
614 if (NVRAM
->wd_timer
!= NULL
)
615 timer_del(NVRAM
->wd_timer
);
618 static void m48t59_reset_isa(DeviceState
*d
)
620 M48t59ISAState
*isa
= ISA_M48T59(d
);
621 M48t59State
*NVRAM
= &isa
->state
;
623 m48t59_reset_common(NVRAM
);
626 static void m48t59_reset_sysbus(DeviceState
*d
)
628 M48t59SysBusState
*sys
= SYSBUS_M48T59(d
);
629 M48t59State
*NVRAM
= &sys
->state
;
631 m48t59_reset_common(NVRAM
);
634 static const MemoryRegionOps m48t59_io_ops
= {
636 .write
= NVRAM_writeb
,
638 .min_access_size
= 1,
639 .max_access_size
= 1,
641 .endianness
= DEVICE_LITTLE_ENDIAN
,
644 /* Initialisation routine */
645 M48t59State
*m48t59_init(qemu_irq IRQ
, hwaddr mem_base
,
646 uint32_t io_base
, uint16_t size
, int model
)
650 M48t59SysBusState
*d
;
653 dev
= qdev_create(NULL
, TYPE_SYSBUS_M48T59
);
654 qdev_prop_set_uint32(dev
, "model", model
);
655 qdev_prop_set_uint32(dev
, "size", size
);
656 qdev_prop_set_uint32(dev
, "io_base", io_base
);
657 qdev_init_nofail(dev
);
658 s
= SYS_BUS_DEVICE(dev
);
659 d
= SYSBUS_M48T59(dev
);
661 sysbus_connect_irq(s
, 0, IRQ
);
662 memory_region_init_io(&d
->io
, OBJECT(d
), &m48t59_io_ops
, state
,
665 memory_region_add_subregion(get_system_io(), io_base
, &d
->io
);
668 sysbus_mmio_map(s
, 0, mem_base
);
674 M48t59State
*m48t59_init_isa(ISABus
*bus
, uint32_t io_base
, uint16_t size
,
682 isadev
= isa_create(bus
, TYPE_ISA_M48T59
);
683 dev
= DEVICE(isadev
);
684 qdev_prop_set_uint32(dev
, "model", model
);
685 qdev_prop_set_uint32(dev
, "size", size
);
686 qdev_prop_set_uint32(dev
, "io_base", io_base
);
687 qdev_init_nofail(dev
);
688 d
= ISA_M48T59(isadev
);
691 memory_region_init_io(&d
->io
, OBJECT(d
), &m48t59_io_ops
, s
, "m48t59", 4);
693 isa_register_ioport(isadev
, &d
->io
, io_base
);
699 static void m48t59_realize_common(M48t59State
*s
, Error
**errp
)
701 s
->buffer
= g_malloc0(s
->size
);
702 if (s
->model
== 59) {
703 s
->alrm_timer
= timer_new_ns(rtc_clock
, &alarm_cb
, s
);
704 s
->wd_timer
= timer_new_ns(QEMU_CLOCK_VIRTUAL
, &watchdog_cb
, s
);
706 qemu_get_timedate(&s
->alarm
, 0);
708 vmstate_register(NULL
, -1, &vmstate_m48t59
, s
);
711 static void m48t59_isa_realize(DeviceState
*dev
, Error
**errp
)
713 ISADevice
*isadev
= ISA_DEVICE(dev
);
714 M48t59ISAState
*d
= ISA_M48T59(dev
);
715 M48t59State
*s
= &d
->state
;
717 isa_init_irq(isadev
, &s
->IRQ
, 8);
718 m48t59_realize_common(s
, errp
);
721 static int m48t59_init1(SysBusDevice
*dev
)
723 M48t59SysBusState
*d
= SYSBUS_M48T59(dev
);
724 M48t59State
*s
= &d
->state
;
727 sysbus_init_irq(dev
, &s
->IRQ
);
729 memory_region_init_io(&s
->iomem
, OBJECT(d
), &nvram_ops
, s
,
730 "m48t59.nvram", s
->size
);
731 sysbus_init_mmio(dev
, &s
->iomem
);
732 m48t59_realize_common(s
, &err
);
741 static Property m48t59_isa_properties
[] = {
742 DEFINE_PROP_UINT32("size", M48t59ISAState
, state
.size
, -1),
743 DEFINE_PROP_UINT32("model", M48t59ISAState
, state
.model
, -1),
744 DEFINE_PROP_UINT32("io_base", M48t59ISAState
, state
.io_base
, 0),
745 DEFINE_PROP_END_OF_LIST(),
748 static void m48t59_isa_class_init(ObjectClass
*klass
, void *data
)
750 DeviceClass
*dc
= DEVICE_CLASS(klass
);
752 dc
->realize
= m48t59_isa_realize
;
753 dc
->reset
= m48t59_reset_isa
;
754 dc
->props
= m48t59_isa_properties
;
755 /* Reason: needs to be wired up by m48t59_init_isa() */
756 dc
->cannot_instantiate_with_device_add_yet
= true;
759 static const TypeInfo m48t59_isa_info
= {
760 .name
= TYPE_ISA_M48T59
,
761 .parent
= TYPE_ISA_DEVICE
,
762 .instance_size
= sizeof(M48t59ISAState
),
763 .class_init
= m48t59_isa_class_init
,
766 static Property m48t59_properties
[] = {
767 DEFINE_PROP_UINT32("size", M48t59SysBusState
, state
.size
, -1),
768 DEFINE_PROP_UINT32("model", M48t59SysBusState
, state
.model
, -1),
769 DEFINE_PROP_UINT32("io_base", M48t59SysBusState
, state
.io_base
, 0),
770 DEFINE_PROP_END_OF_LIST(),
773 static void m48t59_class_init(ObjectClass
*klass
, void *data
)
775 DeviceClass
*dc
= DEVICE_CLASS(klass
);
776 SysBusDeviceClass
*k
= SYS_BUS_DEVICE_CLASS(klass
);
778 k
->init
= m48t59_init1
;
779 dc
->reset
= m48t59_reset_sysbus
;
780 dc
->props
= m48t59_properties
;
783 static const TypeInfo m48t59_info
= {
784 .name
= TYPE_SYSBUS_M48T59
,
785 .parent
= TYPE_SYS_BUS_DEVICE
,
786 .instance_size
= sizeof(M48t59SysBusState
),
787 .class_init
= m48t59_class_init
,
790 static void m48t59_register_types(void)
792 type_register_static(&m48t59_info
);
793 type_register_static(&m48t59_isa_info
);
796 type_init(m48t59_register_types
)