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
26 #include "qemu-timer.h"
30 #include "exec-memory.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 typedef struct M48t59ISAState
{
81 typedef struct M48t59SysBusState
{
87 /* Fake timer functions */
89 /* Alarm management */
90 static void alarm_cb (void *opaque
)
94 M48t59State
*NVRAM
= opaque
;
96 qemu_set_irq(NVRAM
->IRQ
, 1);
97 if ((NVRAM
->buffer
[0x1FF5] & 0x80) == 0 &&
98 (NVRAM
->buffer
[0x1FF4] & 0x80) == 0 &&
99 (NVRAM
->buffer
[0x1FF3] & 0x80) == 0 &&
100 (NVRAM
->buffer
[0x1FF2] & 0x80) == 0) {
101 /* Repeat once a month */
102 qemu_get_timedate(&tm
, NVRAM
->time_offset
);
104 if (tm
.tm_mon
== 13) {
108 next_time
= qemu_timedate_diff(&tm
) - NVRAM
->time_offset
;
109 } else if ((NVRAM
->buffer
[0x1FF5] & 0x80) != 0 &&
110 (NVRAM
->buffer
[0x1FF4] & 0x80) == 0 &&
111 (NVRAM
->buffer
[0x1FF3] & 0x80) == 0 &&
112 (NVRAM
->buffer
[0x1FF2] & 0x80) == 0) {
113 /* Repeat once a day */
114 next_time
= 24 * 60 * 60;
115 } else if ((NVRAM
->buffer
[0x1FF5] & 0x80) != 0 &&
116 (NVRAM
->buffer
[0x1FF4] & 0x80) != 0 &&
117 (NVRAM
->buffer
[0x1FF3] & 0x80) == 0 &&
118 (NVRAM
->buffer
[0x1FF2] & 0x80) == 0) {
119 /* Repeat once an hour */
121 } else if ((NVRAM
->buffer
[0x1FF5] & 0x80) != 0 &&
122 (NVRAM
->buffer
[0x1FF4] & 0x80) != 0 &&
123 (NVRAM
->buffer
[0x1FF3] & 0x80) != 0 &&
124 (NVRAM
->buffer
[0x1FF2] & 0x80) == 0) {
125 /* Repeat once a minute */
128 /* Repeat once a second */
131 qemu_mod_timer(NVRAM
->alrm_timer
, qemu_get_clock_ns(rtc_clock
) +
133 qemu_set_irq(NVRAM
->IRQ
, 0);
136 static void set_alarm(M48t59State
*NVRAM
)
139 if (NVRAM
->alrm_timer
!= NULL
) {
140 qemu_del_timer(NVRAM
->alrm_timer
);
141 diff
= qemu_timedate_diff(&NVRAM
->alarm
) - NVRAM
->time_offset
;
143 qemu_mod_timer(NVRAM
->alrm_timer
, diff
* 1000);
147 /* RTC management helpers */
148 static inline void get_time(M48t59State
*NVRAM
, struct tm
*tm
)
150 qemu_get_timedate(tm
, NVRAM
->time_offset
);
153 static void set_time(M48t59State
*NVRAM
, struct tm
*tm
)
155 NVRAM
->time_offset
= qemu_timedate_diff(tm
);
159 /* Watchdog management */
160 static void watchdog_cb (void *opaque
)
162 M48t59State
*NVRAM
= opaque
;
164 NVRAM
->buffer
[0x1FF0] |= 0x80;
165 if (NVRAM
->buffer
[0x1FF7] & 0x80) {
166 NVRAM
->buffer
[0x1FF7] = 0x00;
167 NVRAM
->buffer
[0x1FFC] &= ~0x40;
168 /* May it be a hw CPU Reset instead ? */
169 qemu_system_reset_request();
171 qemu_set_irq(NVRAM
->IRQ
, 1);
172 qemu_set_irq(NVRAM
->IRQ
, 0);
176 static void set_up_watchdog(M48t59State
*NVRAM
, uint8_t value
)
178 uint64_t interval
; /* in 1/16 seconds */
180 NVRAM
->buffer
[0x1FF0] &= ~0x80;
181 if (NVRAM
->wd_timer
!= NULL
) {
182 qemu_del_timer(NVRAM
->wd_timer
);
184 interval
= (1 << (2 * (value
& 0x03))) * ((value
>> 2) & 0x1F);
185 qemu_mod_timer(NVRAM
->wd_timer
, ((uint64_t)time(NULL
) * 1000) +
186 ((interval
* 1000) >> 4));
191 /* Direct access to NVRAM */
192 void m48t59_write (void *opaque
, uint32_t addr
, uint32_t val
)
194 M48t59State
*NVRAM
= opaque
;
198 if (addr
> 0x1FF8 && addr
< 0x2000)
199 NVRAM_PRINTF("%s: 0x%08x => 0x%08x\n", __func__
, addr
, val
);
201 /* check for NVRAM access */
202 if ((NVRAM
->model
== 2 && addr
< 0x7f8) ||
203 (NVRAM
->model
== 8 && addr
< 0x1ff8) ||
204 (NVRAM
->model
== 59 && addr
< 0x1ff0)) {
211 /* flags register : read-only */
218 tmp
= from_bcd(val
& 0x7F);
219 if (tmp
>= 0 && tmp
<= 59) {
220 NVRAM
->alarm
.tm_sec
= tmp
;
221 NVRAM
->buffer
[0x1FF2] = val
;
227 tmp
= from_bcd(val
& 0x7F);
228 if (tmp
>= 0 && tmp
<= 59) {
229 NVRAM
->alarm
.tm_min
= tmp
;
230 NVRAM
->buffer
[0x1FF3] = val
;
236 tmp
= from_bcd(val
& 0x3F);
237 if (tmp
>= 0 && tmp
<= 23) {
238 NVRAM
->alarm
.tm_hour
= tmp
;
239 NVRAM
->buffer
[0x1FF4] = val
;
245 tmp
= from_bcd(val
& 0x3F);
247 NVRAM
->alarm
.tm_mday
= tmp
;
248 NVRAM
->buffer
[0x1FF5] = val
;
254 NVRAM
->buffer
[0x1FF6] = val
;
258 NVRAM
->buffer
[0x1FF7] = val
;
259 set_up_watchdog(NVRAM
, val
);
264 NVRAM
->buffer
[addr
] = (val
& ~0xA0) | 0x90;
269 tmp
= from_bcd(val
& 0x7F);
270 if (tmp
>= 0 && tmp
<= 59) {
271 get_time(NVRAM
, &tm
);
273 set_time(NVRAM
, &tm
);
275 if ((val
& 0x80) ^ (NVRAM
->buffer
[addr
] & 0x80)) {
277 NVRAM
->stop_time
= time(NULL
);
279 NVRAM
->time_offset
+= NVRAM
->stop_time
- time(NULL
);
280 NVRAM
->stop_time
= 0;
283 NVRAM
->buffer
[addr
] = val
& 0x80;
288 tmp
= from_bcd(val
& 0x7F);
289 if (tmp
>= 0 && tmp
<= 59) {
290 get_time(NVRAM
, &tm
);
292 set_time(NVRAM
, &tm
);
298 tmp
= from_bcd(val
& 0x3F);
299 if (tmp
>= 0 && tmp
<= 23) {
300 get_time(NVRAM
, &tm
);
302 set_time(NVRAM
, &tm
);
307 /* day of the week / century */
308 tmp
= from_bcd(val
& 0x07);
309 get_time(NVRAM
, &tm
);
311 set_time(NVRAM
, &tm
);
312 NVRAM
->buffer
[addr
] = val
& 0x40;
317 tmp
= from_bcd(val
& 0x3F);
319 get_time(NVRAM
, &tm
);
321 set_time(NVRAM
, &tm
);
327 tmp
= from_bcd(val
& 0x1F);
328 if (tmp
>= 1 && tmp
<= 12) {
329 get_time(NVRAM
, &tm
);
331 set_time(NVRAM
, &tm
);
338 if (tmp
>= 0 && tmp
<= 99) {
339 get_time(NVRAM
, &tm
);
340 if (NVRAM
->model
== 8) {
341 tm
.tm_year
= from_bcd(val
) + 68; // Base year is 1968
343 tm
.tm_year
= from_bcd(val
);
345 set_time(NVRAM
, &tm
);
349 /* Check lock registers state */
350 if (addr
>= 0x20 && addr
<= 0x2F && (NVRAM
->lock
& 1))
352 if (addr
>= 0x30 && addr
<= 0x3F && (NVRAM
->lock
& 2))
355 if (addr
< NVRAM
->size
) {
356 NVRAM
->buffer
[addr
] = val
& 0xFF;
362 uint32_t m48t59_read (void *opaque
, uint32_t addr
)
364 M48t59State
*NVRAM
= opaque
;
366 uint32_t retval
= 0xFF;
368 /* check for NVRAM access */
369 if ((NVRAM
->model
== 2 && addr
< 0x078f) ||
370 (NVRAM
->model
== 8 && addr
< 0x1ff8) ||
371 (NVRAM
->model
== 59 && addr
< 0x1ff0)) {
400 /* A read resets the watchdog */
401 set_up_watchdog(NVRAM
, NVRAM
->buffer
[0x1FF7]);
410 get_time(NVRAM
, &tm
);
411 retval
= (NVRAM
->buffer
[addr
] & 0x80) | to_bcd(tm
.tm_sec
);
416 get_time(NVRAM
, &tm
);
417 retval
= to_bcd(tm
.tm_min
);
422 get_time(NVRAM
, &tm
);
423 retval
= to_bcd(tm
.tm_hour
);
427 /* day of the week / century */
428 get_time(NVRAM
, &tm
);
429 retval
= NVRAM
->buffer
[addr
] | tm
.tm_wday
;
434 get_time(NVRAM
, &tm
);
435 retval
= to_bcd(tm
.tm_mday
);
440 get_time(NVRAM
, &tm
);
441 retval
= to_bcd(tm
.tm_mon
+ 1);
446 get_time(NVRAM
, &tm
);
447 if (NVRAM
->model
== 8) {
448 retval
= to_bcd(tm
.tm_year
- 68); // Base year is 1968
450 retval
= to_bcd(tm
.tm_year
);
454 /* Check lock registers state */
455 if (addr
>= 0x20 && addr
<= 0x2F && (NVRAM
->lock
& 1))
457 if (addr
>= 0x30 && addr
<= 0x3F && (NVRAM
->lock
& 2))
460 if (addr
< NVRAM
->size
) {
461 retval
= NVRAM
->buffer
[addr
];
465 if (addr
> 0x1FF9 && addr
< 0x2000)
466 NVRAM_PRINTF("%s: 0x%08x <= 0x%08x\n", __func__
, addr
, retval
);
471 void m48t59_set_addr (void *opaque
, uint32_t addr
)
473 M48t59State
*NVRAM
= opaque
;
478 void m48t59_toggle_lock (void *opaque
, int lock
)
480 M48t59State
*NVRAM
= opaque
;
482 NVRAM
->lock
^= 1 << lock
;
485 /* IO access to NVRAM */
486 static void NVRAM_writeb(void *opaque
, hwaddr addr
, uint64_t val
,
489 M48t59State
*NVRAM
= opaque
;
491 NVRAM_PRINTF("%s: 0x%08x => 0x%08x\n", __func__
, addr
, val
);
494 NVRAM
->addr
&= ~0x00FF;
498 NVRAM
->addr
&= ~0xFF00;
499 NVRAM
->addr
|= val
<< 8;
502 m48t59_write(NVRAM
, NVRAM
->addr
, val
);
503 NVRAM
->addr
= 0x0000;
510 static uint64_t NVRAM_readb(void *opaque
, hwaddr addr
, unsigned size
)
512 M48t59State
*NVRAM
= opaque
;
517 retval
= m48t59_read(NVRAM
, NVRAM
->addr
);
523 NVRAM_PRINTF("%s: 0x%08x <= 0x%08x\n", __func__
, addr
, retval
);
528 static void nvram_writeb (void *opaque
, hwaddr addr
, uint32_t value
)
530 M48t59State
*NVRAM
= opaque
;
532 m48t59_write(NVRAM
, addr
, value
& 0xff);
535 static void nvram_writew (void *opaque
, hwaddr addr
, uint32_t value
)
537 M48t59State
*NVRAM
= opaque
;
539 m48t59_write(NVRAM
, addr
, (value
>> 8) & 0xff);
540 m48t59_write(NVRAM
, addr
+ 1, value
& 0xff);
543 static void nvram_writel (void *opaque
, hwaddr addr
, uint32_t value
)
545 M48t59State
*NVRAM
= opaque
;
547 m48t59_write(NVRAM
, addr
, (value
>> 24) & 0xff);
548 m48t59_write(NVRAM
, addr
+ 1, (value
>> 16) & 0xff);
549 m48t59_write(NVRAM
, addr
+ 2, (value
>> 8) & 0xff);
550 m48t59_write(NVRAM
, addr
+ 3, value
& 0xff);
553 static uint32_t nvram_readb (void *opaque
, hwaddr addr
)
555 M48t59State
*NVRAM
= opaque
;
558 retval
= m48t59_read(NVRAM
, addr
);
562 static uint32_t nvram_readw (void *opaque
, hwaddr addr
)
564 M48t59State
*NVRAM
= opaque
;
567 retval
= m48t59_read(NVRAM
, addr
) << 8;
568 retval
|= m48t59_read(NVRAM
, addr
+ 1);
572 static uint32_t nvram_readl (void *opaque
, hwaddr addr
)
574 M48t59State
*NVRAM
= opaque
;
577 retval
= m48t59_read(NVRAM
, addr
) << 24;
578 retval
|= m48t59_read(NVRAM
, addr
+ 1) << 16;
579 retval
|= m48t59_read(NVRAM
, addr
+ 2) << 8;
580 retval
|= m48t59_read(NVRAM
, addr
+ 3);
584 static const MemoryRegionOps nvram_ops
= {
586 .read
= { nvram_readb
, nvram_readw
, nvram_readl
, },
587 .write
= { nvram_writeb
, nvram_writew
, nvram_writel
, },
589 .endianness
= DEVICE_NATIVE_ENDIAN
,
592 static const VMStateDescription vmstate_m48t59
= {
595 .minimum_version_id
= 1,
596 .minimum_version_id_old
= 1,
597 .fields
= (VMStateField
[]) {
598 VMSTATE_UINT8(lock
, M48t59State
),
599 VMSTATE_UINT16(addr
, M48t59State
),
600 VMSTATE_VBUFFER_UINT32(buffer
, M48t59State
, 0, NULL
, 0, size
),
601 VMSTATE_END_OF_LIST()
605 static void m48t59_reset_common(M48t59State
*NVRAM
)
609 if (NVRAM
->alrm_timer
!= NULL
)
610 qemu_del_timer(NVRAM
->alrm_timer
);
612 if (NVRAM
->wd_timer
!= NULL
)
613 qemu_del_timer(NVRAM
->wd_timer
);
616 static void m48t59_reset_isa(DeviceState
*d
)
618 M48t59ISAState
*isa
= container_of(d
, M48t59ISAState
, busdev
.qdev
);
619 M48t59State
*NVRAM
= &isa
->state
;
621 m48t59_reset_common(NVRAM
);
624 static void m48t59_reset_sysbus(DeviceState
*d
)
626 M48t59SysBusState
*sys
= container_of(d
, M48t59SysBusState
, busdev
.qdev
);
627 M48t59State
*NVRAM
= &sys
->state
;
629 m48t59_reset_common(NVRAM
);
632 static const MemoryRegionOps m48t59_io_ops
= {
634 .write
= NVRAM_writeb
,
636 .min_access_size
= 1,
637 .max_access_size
= 1,
639 .endianness
= DEVICE_LITTLE_ENDIAN
,
642 /* Initialisation routine */
643 M48t59State
*m48t59_init(qemu_irq IRQ
, hwaddr mem_base
,
644 uint32_t io_base
, uint16_t size
, int model
)
648 M48t59SysBusState
*d
;
651 dev
= qdev_create(NULL
, "m48t59");
652 qdev_prop_set_uint32(dev
, "model", model
);
653 qdev_prop_set_uint32(dev
, "size", size
);
654 qdev_prop_set_uint32(dev
, "io_base", io_base
);
655 qdev_init_nofail(dev
);
656 s
= sysbus_from_qdev(dev
);
657 d
= FROM_SYSBUS(M48t59SysBusState
, s
);
659 sysbus_connect_irq(s
, 0, IRQ
);
660 memory_region_init_io(&d
->io
, &m48t59_io_ops
, state
, "m48t59", 4);
662 memory_region_add_subregion(get_system_io(), io_base
, &d
->io
);
665 sysbus_mmio_map(s
, 0, mem_base
);
671 M48t59State
*m48t59_init_isa(ISABus
*bus
, uint32_t io_base
, uint16_t size
,
678 dev
= isa_create(bus
, "m48t59_isa");
679 qdev_prop_set_uint32(&dev
->qdev
, "model", model
);
680 qdev_prop_set_uint32(&dev
->qdev
, "size", size
);
681 qdev_prop_set_uint32(&dev
->qdev
, "io_base", io_base
);
682 qdev_init_nofail(&dev
->qdev
);
683 d
= DO_UPCAST(M48t59ISAState
, busdev
, dev
);
686 memory_region_init_io(&d
->io
, &m48t59_io_ops
, s
, "m48t59", 4);
688 isa_register_ioport(dev
, &d
->io
, io_base
);
694 static void m48t59_init_common(M48t59State
*s
)
696 s
->buffer
= g_malloc0(s
->size
);
697 if (s
->model
== 59) {
698 s
->alrm_timer
= qemu_new_timer_ns(rtc_clock
, &alarm_cb
, s
);
699 s
->wd_timer
= qemu_new_timer_ns(vm_clock
, &watchdog_cb
, s
);
701 qemu_get_timedate(&s
->alarm
, 0);
703 vmstate_register(NULL
, -1, &vmstate_m48t59
, s
);
706 static int m48t59_init_isa1(ISADevice
*dev
)
708 M48t59ISAState
*d
= DO_UPCAST(M48t59ISAState
, busdev
, dev
);
709 M48t59State
*s
= &d
->state
;
711 isa_init_irq(dev
, &s
->IRQ
, 8);
712 m48t59_init_common(s
);
717 static int m48t59_init1(SysBusDevice
*dev
)
719 M48t59SysBusState
*d
= FROM_SYSBUS(M48t59SysBusState
, dev
);
720 M48t59State
*s
= &d
->state
;
722 sysbus_init_irq(dev
, &s
->IRQ
);
724 memory_region_init_io(&s
->iomem
, &nvram_ops
, s
, "m48t59.nvram", s
->size
);
725 sysbus_init_mmio(dev
, &s
->iomem
);
726 m48t59_init_common(s
);
731 static Property m48t59_isa_properties
[] = {
732 DEFINE_PROP_UINT32("size", M48t59ISAState
, state
.size
, -1),
733 DEFINE_PROP_UINT32("model", M48t59ISAState
, state
.model
, -1),
734 DEFINE_PROP_HEX32( "io_base", M48t59ISAState
, state
.io_base
, 0),
735 DEFINE_PROP_END_OF_LIST(),
738 static void m48t59_init_class_isa1(ObjectClass
*klass
, void *data
)
740 DeviceClass
*dc
= DEVICE_CLASS(klass
);
741 ISADeviceClass
*ic
= ISA_DEVICE_CLASS(klass
);
742 ic
->init
= m48t59_init_isa1
;
744 dc
->reset
= m48t59_reset_isa
;
745 dc
->props
= m48t59_isa_properties
;
748 static TypeInfo m48t59_isa_info
= {
749 .name
= "m48t59_isa",
750 .parent
= TYPE_ISA_DEVICE
,
751 .instance_size
= sizeof(M48t59ISAState
),
752 .class_init
= m48t59_init_class_isa1
,
755 static Property m48t59_properties
[] = {
756 DEFINE_PROP_UINT32("size", M48t59SysBusState
, state
.size
, -1),
757 DEFINE_PROP_UINT32("model", M48t59SysBusState
, state
.model
, -1),
758 DEFINE_PROP_HEX32( "io_base", M48t59SysBusState
, state
.io_base
, 0),
759 DEFINE_PROP_END_OF_LIST(),
762 static void m48t59_class_init(ObjectClass
*klass
, void *data
)
764 DeviceClass
*dc
= DEVICE_CLASS(klass
);
765 SysBusDeviceClass
*k
= SYS_BUS_DEVICE_CLASS(klass
);
767 k
->init
= m48t59_init1
;
768 dc
->reset
= m48t59_reset_sysbus
;
769 dc
->props
= m48t59_properties
;
772 static TypeInfo m48t59_info
= {
774 .parent
= TYPE_SYS_BUS_DEVICE
,
775 .instance_size
= sizeof(M48t59SysBusState
),
776 .class_init
= m48t59_class_init
,
779 static void m48t59_register_types(void)
781 type_register_static(&m48t59_info
);
782 type_register_static(&m48t59_isa_info
);
785 type_init(m48t59_register_types
)