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
, OBJECT(d
), &m48t59_io_ops
, state
,
661 memory_region_add_subregion(get_system_io(), io_base
, &d
->io
);
664 sysbus_mmio_map(s
, 0, mem_base
);
670 M48t59State
*m48t59_init_isa(ISABus
*bus
, uint32_t io_base
, uint16_t size
,
678 isadev
= isa_create(bus
, TYPE_ISA_M48T59
);
679 dev
= DEVICE(isadev
);
680 qdev_prop_set_uint32(dev
, "model", model
);
681 qdev_prop_set_uint32(dev
, "size", size
);
682 qdev_prop_set_uint32(dev
, "io_base", io_base
);
683 qdev_init_nofail(dev
);
684 d
= ISA_M48T59(isadev
);
687 memory_region_init_io(&d
->io
, OBJECT(d
), &m48t59_io_ops
, s
, "m48t59", 4);
689 isa_register_ioport(isadev
, &d
->io
, io_base
);
695 static void m48t59_realize_common(M48t59State
*s
, Error
**errp
)
697 s
->buffer
= g_malloc0(s
->size
);
698 if (s
->model
== 59) {
699 s
->alrm_timer
= qemu_new_timer_ns(rtc_clock
, &alarm_cb
, s
);
700 s
->wd_timer
= qemu_new_timer_ns(vm_clock
, &watchdog_cb
, s
);
702 qemu_get_timedate(&s
->alarm
, 0);
704 vmstate_register(NULL
, -1, &vmstate_m48t59
, s
);
707 static void m48t59_isa_realize(DeviceState
*dev
, Error
**errp
)
709 ISADevice
*isadev
= ISA_DEVICE(dev
);
710 M48t59ISAState
*d
= ISA_M48T59(dev
);
711 M48t59State
*s
= &d
->state
;
713 isa_init_irq(isadev
, &s
->IRQ
, 8);
714 m48t59_realize_common(s
, errp
);
717 static int m48t59_init1(SysBusDevice
*dev
)
719 M48t59SysBusState
*d
= FROM_SYSBUS(M48t59SysBusState
, dev
);
720 M48t59State
*s
= &d
->state
;
723 sysbus_init_irq(dev
, &s
->IRQ
);
725 memory_region_init_io(&s
->iomem
, OBJECT(d
), &nvram_ops
, s
,
726 "m48t59.nvram", s
->size
);
727 sysbus_init_mmio(dev
, &s
->iomem
);
728 m48t59_realize_common(s
, &err
);
737 static Property m48t59_isa_properties
[] = {
738 DEFINE_PROP_UINT32("size", M48t59ISAState
, state
.size
, -1),
739 DEFINE_PROP_UINT32("model", M48t59ISAState
, state
.model
, -1),
740 DEFINE_PROP_HEX32( "io_base", M48t59ISAState
, state
.io_base
, 0),
741 DEFINE_PROP_END_OF_LIST(),
744 static void m48t59_isa_class_init(ObjectClass
*klass
, void *data
)
746 DeviceClass
*dc
= DEVICE_CLASS(klass
);
748 dc
->realize
= m48t59_isa_realize
;
750 dc
->reset
= m48t59_reset_isa
;
751 dc
->props
= m48t59_isa_properties
;
754 static const TypeInfo m48t59_isa_info
= {
755 .name
= TYPE_ISA_M48T59
,
756 .parent
= TYPE_ISA_DEVICE
,
757 .instance_size
= sizeof(M48t59ISAState
),
758 .class_init
= m48t59_isa_class_init
,
761 static Property m48t59_properties
[] = {
762 DEFINE_PROP_UINT32("size", M48t59SysBusState
, state
.size
, -1),
763 DEFINE_PROP_UINT32("model", M48t59SysBusState
, state
.model
, -1),
764 DEFINE_PROP_HEX32( "io_base", M48t59SysBusState
, state
.io_base
, 0),
765 DEFINE_PROP_END_OF_LIST(),
768 static void m48t59_class_init(ObjectClass
*klass
, void *data
)
770 DeviceClass
*dc
= DEVICE_CLASS(klass
);
771 SysBusDeviceClass
*k
= SYS_BUS_DEVICE_CLASS(klass
);
773 k
->init
= m48t59_init1
;
774 dc
->reset
= m48t59_reset_sysbus
;
775 dc
->props
= m48t59_properties
;
778 static const TypeInfo m48t59_info
= {
780 .parent
= TYPE_SYS_BUS_DEVICE
,
781 .instance_size
= sizeof(M48t59SysBusState
),
782 .class_init
= m48t59_class_init
,
785 static void m48t59_register_types(void)
787 type_register_static(&m48t59_info
);
788 type_register_static(&m48t59_isa_info
);
791 type_init(m48t59_register_types
)