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"
33 #if defined(DEBUG_NVRAM)
34 #define NVRAM_PRINTF(fmt, ...) do { printf(fmt , ## __VA_ARGS__); } while (0)
36 #define NVRAM_PRINTF(fmt, ...) do { } while (0)
40 * The M48T02, M48T08 and M48T59 chips are very similar. The newer '59 has
41 * alarm and a watchdog timer and related control registers. In the
42 * PPC platform there is also a nvram lock function.
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
53 /* Hardware parameters */
61 /* Alarm & watchdog */
63 struct QEMUTimer
*alrm_timer
;
64 struct QEMUTimer
*wd_timer
;
67 /* Model parameters */
68 uint32_t type
; /* 2 = m48t02, 8 = m48t08, 59 = m48t59 */
74 typedef struct M48t59ISAState
{
80 typedef struct M48t59SysBusState
{
85 /* Fake timer functions */
87 /* Alarm management */
88 static void alarm_cb (void *opaque
)
92 M48t59State
*NVRAM
= opaque
;
94 qemu_set_irq(NVRAM
->IRQ
, 1);
95 if ((NVRAM
->buffer
[0x1FF5] & 0x80) == 0 &&
96 (NVRAM
->buffer
[0x1FF4] & 0x80) == 0 &&
97 (NVRAM
->buffer
[0x1FF3] & 0x80) == 0 &&
98 (NVRAM
->buffer
[0x1FF2] & 0x80) == 0) {
99 /* Repeat once a month */
100 qemu_get_timedate(&tm
, NVRAM
->time_offset
);
102 if (tm
.tm_mon
== 13) {
106 next_time
= qemu_timedate_diff(&tm
) - NVRAM
->time_offset
;
107 } else if ((NVRAM
->buffer
[0x1FF5] & 0x80) != 0 &&
108 (NVRAM
->buffer
[0x1FF4] & 0x80) == 0 &&
109 (NVRAM
->buffer
[0x1FF3] & 0x80) == 0 &&
110 (NVRAM
->buffer
[0x1FF2] & 0x80) == 0) {
111 /* Repeat once a day */
112 next_time
= 24 * 60 * 60;
113 } else if ((NVRAM
->buffer
[0x1FF5] & 0x80) != 0 &&
114 (NVRAM
->buffer
[0x1FF4] & 0x80) != 0 &&
115 (NVRAM
->buffer
[0x1FF3] & 0x80) == 0 &&
116 (NVRAM
->buffer
[0x1FF2] & 0x80) == 0) {
117 /* Repeat once an hour */
119 } else if ((NVRAM
->buffer
[0x1FF5] & 0x80) != 0 &&
120 (NVRAM
->buffer
[0x1FF4] & 0x80) != 0 &&
121 (NVRAM
->buffer
[0x1FF3] & 0x80) != 0 &&
122 (NVRAM
->buffer
[0x1FF2] & 0x80) == 0) {
123 /* Repeat once a minute */
126 /* Repeat once a second */
129 qemu_mod_timer(NVRAM
->alrm_timer
, qemu_get_clock_ns(vm_clock
) +
131 qemu_set_irq(NVRAM
->IRQ
, 0);
134 static void set_alarm(M48t59State
*NVRAM
)
137 if (NVRAM
->alrm_timer
!= NULL
) {
138 qemu_del_timer(NVRAM
->alrm_timer
);
139 diff
= qemu_timedate_diff(&NVRAM
->alarm
) - NVRAM
->time_offset
;
141 qemu_mod_timer(NVRAM
->alrm_timer
, diff
* 1000);
145 /* RTC management helpers */
146 static inline void get_time(M48t59State
*NVRAM
, struct tm
*tm
)
148 qemu_get_timedate(tm
, NVRAM
->time_offset
);
151 static void set_time(M48t59State
*NVRAM
, struct tm
*tm
)
153 NVRAM
->time_offset
= qemu_timedate_diff(tm
);
157 /* Watchdog management */
158 static void watchdog_cb (void *opaque
)
160 M48t59State
*NVRAM
= opaque
;
162 NVRAM
->buffer
[0x1FF0] |= 0x80;
163 if (NVRAM
->buffer
[0x1FF7] & 0x80) {
164 NVRAM
->buffer
[0x1FF7] = 0x00;
165 NVRAM
->buffer
[0x1FFC] &= ~0x40;
166 /* May it be a hw CPU Reset instead ? */
167 qemu_system_reset_request();
169 qemu_set_irq(NVRAM
->IRQ
, 1);
170 qemu_set_irq(NVRAM
->IRQ
, 0);
174 static void set_up_watchdog(M48t59State
*NVRAM
, uint8_t value
)
176 uint64_t interval
; /* in 1/16 seconds */
178 NVRAM
->buffer
[0x1FF0] &= ~0x80;
179 if (NVRAM
->wd_timer
!= NULL
) {
180 qemu_del_timer(NVRAM
->wd_timer
);
182 interval
= (1 << (2 * (value
& 0x03))) * ((value
>> 2) & 0x1F);
183 qemu_mod_timer(NVRAM
->wd_timer
, ((uint64_t)time(NULL
) * 1000) +
184 ((interval
* 1000) >> 4));
189 /* Direct access to NVRAM */
190 void m48t59_write (void *opaque
, uint32_t addr
, uint32_t val
)
192 M48t59State
*NVRAM
= opaque
;
196 if (addr
> 0x1FF8 && addr
< 0x2000)
197 NVRAM_PRINTF("%s: 0x%08x => 0x%08x\n", __func__
, addr
, val
);
199 /* check for NVRAM access */
200 if ((NVRAM
->type
== 2 && addr
< 0x7f8) ||
201 (NVRAM
->type
== 8 && addr
< 0x1ff8) ||
202 (NVRAM
->type
== 59 && addr
< 0x1ff0))
208 /* flags register : read-only */
215 tmp
= from_bcd(val
& 0x7F);
216 if (tmp
>= 0 && tmp
<= 59) {
217 NVRAM
->alarm
.tm_sec
= tmp
;
218 NVRAM
->buffer
[0x1FF2] = val
;
224 tmp
= from_bcd(val
& 0x7F);
225 if (tmp
>= 0 && tmp
<= 59) {
226 NVRAM
->alarm
.tm_min
= tmp
;
227 NVRAM
->buffer
[0x1FF3] = val
;
233 tmp
= from_bcd(val
& 0x3F);
234 if (tmp
>= 0 && tmp
<= 23) {
235 NVRAM
->alarm
.tm_hour
= tmp
;
236 NVRAM
->buffer
[0x1FF4] = val
;
242 tmp
= from_bcd(val
& 0x1F);
244 NVRAM
->alarm
.tm_mday
= tmp
;
245 NVRAM
->buffer
[0x1FF5] = val
;
251 NVRAM
->buffer
[0x1FF6] = val
;
255 NVRAM
->buffer
[0x1FF7] = val
;
256 set_up_watchdog(NVRAM
, val
);
261 NVRAM
->buffer
[addr
] = (val
& ~0xA0) | 0x90;
266 tmp
= from_bcd(val
& 0x7F);
267 if (tmp
>= 0 && tmp
<= 59) {
268 get_time(NVRAM
, &tm
);
270 set_time(NVRAM
, &tm
);
272 if ((val
& 0x80) ^ (NVRAM
->buffer
[addr
] & 0x80)) {
274 NVRAM
->stop_time
= time(NULL
);
276 NVRAM
->time_offset
+= NVRAM
->stop_time
- time(NULL
);
277 NVRAM
->stop_time
= 0;
280 NVRAM
->buffer
[addr
] = val
& 0x80;
285 tmp
= from_bcd(val
& 0x7F);
286 if (tmp
>= 0 && tmp
<= 59) {
287 get_time(NVRAM
, &tm
);
289 set_time(NVRAM
, &tm
);
295 tmp
= from_bcd(val
& 0x3F);
296 if (tmp
>= 0 && tmp
<= 23) {
297 get_time(NVRAM
, &tm
);
299 set_time(NVRAM
, &tm
);
304 /* day of the week / century */
305 tmp
= from_bcd(val
& 0x07);
306 get_time(NVRAM
, &tm
);
308 set_time(NVRAM
, &tm
);
309 NVRAM
->buffer
[addr
] = val
& 0x40;
314 tmp
= from_bcd(val
& 0x1F);
316 get_time(NVRAM
, &tm
);
318 set_time(NVRAM
, &tm
);
324 tmp
= from_bcd(val
& 0x1F);
325 if (tmp
>= 1 && tmp
<= 12) {
326 get_time(NVRAM
, &tm
);
328 set_time(NVRAM
, &tm
);
335 if (tmp
>= 0 && tmp
<= 99) {
336 get_time(NVRAM
, &tm
);
337 if (NVRAM
->type
== 8)
338 tm
.tm_year
= from_bcd(val
) + 68; // Base year is 1968
340 tm
.tm_year
= from_bcd(val
);
341 set_time(NVRAM
, &tm
);
345 /* Check lock registers state */
346 if (addr
>= 0x20 && addr
<= 0x2F && (NVRAM
->lock
& 1))
348 if (addr
>= 0x30 && addr
<= 0x3F && (NVRAM
->lock
& 2))
351 if (addr
< NVRAM
->size
) {
352 NVRAM
->buffer
[addr
] = val
& 0xFF;
358 uint32_t m48t59_read (void *opaque
, uint32_t addr
)
360 M48t59State
*NVRAM
= opaque
;
362 uint32_t retval
= 0xFF;
364 /* check for NVRAM access */
365 if ((NVRAM
->type
== 2 && addr
< 0x078f) ||
366 (NVRAM
->type
== 8 && addr
< 0x1ff8) ||
367 (NVRAM
->type
== 59 && addr
< 0x1ff0))
395 /* A read resets the watchdog */
396 set_up_watchdog(NVRAM
, NVRAM
->buffer
[0x1FF7]);
405 get_time(NVRAM
, &tm
);
406 retval
= (NVRAM
->buffer
[addr
] & 0x80) | to_bcd(tm
.tm_sec
);
411 get_time(NVRAM
, &tm
);
412 retval
= to_bcd(tm
.tm_min
);
417 get_time(NVRAM
, &tm
);
418 retval
= to_bcd(tm
.tm_hour
);
422 /* day of the week / century */
423 get_time(NVRAM
, &tm
);
424 retval
= NVRAM
->buffer
[addr
] | tm
.tm_wday
;
429 get_time(NVRAM
, &tm
);
430 retval
= to_bcd(tm
.tm_mday
);
435 get_time(NVRAM
, &tm
);
436 retval
= to_bcd(tm
.tm_mon
+ 1);
441 get_time(NVRAM
, &tm
);
442 if (NVRAM
->type
== 8)
443 retval
= to_bcd(tm
.tm_year
- 68); // Base year is 1968
445 retval
= to_bcd(tm
.tm_year
);
448 /* Check lock registers state */
449 if (addr
>= 0x20 && addr
<= 0x2F && (NVRAM
->lock
& 1))
451 if (addr
>= 0x30 && addr
<= 0x3F && (NVRAM
->lock
& 2))
454 if (addr
< NVRAM
->size
) {
455 retval
= NVRAM
->buffer
[addr
];
459 if (addr
> 0x1FF9 && addr
< 0x2000)
460 NVRAM_PRINTF("%s: 0x%08x <= 0x%08x\n", __func__
, addr
, retval
);
465 void m48t59_set_addr (void *opaque
, uint32_t addr
)
467 M48t59State
*NVRAM
= opaque
;
472 void m48t59_toggle_lock (void *opaque
, int lock
)
474 M48t59State
*NVRAM
= opaque
;
476 NVRAM
->lock
^= 1 << lock
;
479 /* IO access to NVRAM */
480 static void NVRAM_writeb (void *opaque
, uint32_t addr
, uint32_t val
)
482 M48t59State
*NVRAM
= opaque
;
484 NVRAM_PRINTF("%s: 0x%08x => 0x%08x\n", __func__
, addr
, val
);
487 NVRAM
->addr
&= ~0x00FF;
491 NVRAM
->addr
&= ~0xFF00;
492 NVRAM
->addr
|= val
<< 8;
495 m48t59_write(NVRAM
, NVRAM
->addr
, val
);
496 NVRAM
->addr
= 0x0000;
503 static uint32_t NVRAM_readb (void *opaque
, uint32_t addr
)
505 M48t59State
*NVRAM
= opaque
;
510 retval
= m48t59_read(NVRAM
, NVRAM
->addr
);
516 NVRAM_PRINTF("%s: 0x%08x <= 0x%08x\n", __func__
, addr
, retval
);
521 static void nvram_writeb (void *opaque
, target_phys_addr_t addr
, uint32_t value
)
523 M48t59State
*NVRAM
= opaque
;
525 m48t59_write(NVRAM
, addr
, value
& 0xff);
528 static void nvram_writew (void *opaque
, target_phys_addr_t addr
, uint32_t value
)
530 M48t59State
*NVRAM
= opaque
;
532 m48t59_write(NVRAM
, addr
, (value
>> 8) & 0xff);
533 m48t59_write(NVRAM
, addr
+ 1, value
& 0xff);
536 static void nvram_writel (void *opaque
, target_phys_addr_t addr
, uint32_t value
)
538 M48t59State
*NVRAM
= opaque
;
540 m48t59_write(NVRAM
, addr
, (value
>> 24) & 0xff);
541 m48t59_write(NVRAM
, addr
+ 1, (value
>> 16) & 0xff);
542 m48t59_write(NVRAM
, addr
+ 2, (value
>> 8) & 0xff);
543 m48t59_write(NVRAM
, addr
+ 3, value
& 0xff);
546 static uint32_t nvram_readb (void *opaque
, target_phys_addr_t addr
)
548 M48t59State
*NVRAM
= opaque
;
551 retval
= m48t59_read(NVRAM
, addr
);
555 static uint32_t nvram_readw (void *opaque
, target_phys_addr_t addr
)
557 M48t59State
*NVRAM
= opaque
;
560 retval
= m48t59_read(NVRAM
, addr
) << 8;
561 retval
|= m48t59_read(NVRAM
, addr
+ 1);
565 static uint32_t nvram_readl (void *opaque
, target_phys_addr_t addr
)
567 M48t59State
*NVRAM
= opaque
;
570 retval
= m48t59_read(NVRAM
, addr
) << 24;
571 retval
|= m48t59_read(NVRAM
, addr
+ 1) << 16;
572 retval
|= m48t59_read(NVRAM
, addr
+ 2) << 8;
573 retval
|= m48t59_read(NVRAM
, addr
+ 3);
577 static const MemoryRegionOps nvram_ops
= {
579 .read
= { nvram_readb
, nvram_readw
, nvram_readl
, },
580 .write
= { nvram_writeb
, nvram_writew
, nvram_writel
, },
582 .endianness
= DEVICE_NATIVE_ENDIAN
,
585 static const VMStateDescription vmstate_m48t59
= {
588 .minimum_version_id
= 1,
589 .minimum_version_id_old
= 1,
590 .fields
= (VMStateField
[]) {
591 VMSTATE_UINT8(lock
, M48t59State
),
592 VMSTATE_UINT16(addr
, M48t59State
),
593 VMSTATE_VBUFFER_UINT32(buffer
, M48t59State
, 0, NULL
, 0, size
),
594 VMSTATE_END_OF_LIST()
598 static void m48t59_reset_common(M48t59State
*NVRAM
)
602 if (NVRAM
->alrm_timer
!= NULL
)
603 qemu_del_timer(NVRAM
->alrm_timer
);
605 if (NVRAM
->wd_timer
!= NULL
)
606 qemu_del_timer(NVRAM
->wd_timer
);
609 static void m48t59_reset_isa(DeviceState
*d
)
611 M48t59ISAState
*isa
= container_of(d
, M48t59ISAState
, busdev
.qdev
);
612 M48t59State
*NVRAM
= &isa
->state
;
614 m48t59_reset_common(NVRAM
);
617 static void m48t59_reset_sysbus(DeviceState
*d
)
619 M48t59SysBusState
*sys
= container_of(d
, M48t59SysBusState
, busdev
.qdev
);
620 M48t59State
*NVRAM
= &sys
->state
;
622 m48t59_reset_common(NVRAM
);
625 static const MemoryRegionPortio m48t59_portio
[] = {
626 {0, 4, 1, .read
= NVRAM_readb
, .write
= NVRAM_writeb
},
627 PORTIO_END_OF_LIST(),
630 static const MemoryRegionOps m48t59_io_ops
= {
631 .old_portio
= m48t59_portio
,
634 /* Initialisation routine */
635 M48t59State
*m48t59_init(qemu_irq IRQ
, target_phys_addr_t mem_base
,
636 uint32_t io_base
, uint16_t size
, int type
)
640 M48t59SysBusState
*d
;
643 dev
= qdev_create(NULL
, "m48t59");
644 qdev_prop_set_uint32(dev
, "type", type
);
645 qdev_prop_set_uint32(dev
, "size", size
);
646 qdev_prop_set_uint32(dev
, "io_base", io_base
);
647 qdev_init_nofail(dev
);
648 s
= sysbus_from_qdev(dev
);
649 d
= FROM_SYSBUS(M48t59SysBusState
, s
);
651 sysbus_connect_irq(s
, 0, IRQ
);
653 register_ioport_read(io_base
, 0x04, 1, NVRAM_readb
, state
);
654 register_ioport_write(io_base
, 0x04, 1, NVRAM_writeb
, state
);
657 sysbus_mmio_map(s
, 0, mem_base
);
663 M48t59State
*m48t59_init_isa(ISABus
*bus
, uint32_t io_base
, uint16_t size
,
670 dev
= isa_create(bus
, "m48t59_isa");
671 qdev_prop_set_uint32(&dev
->qdev
, "type", type
);
672 qdev_prop_set_uint32(&dev
->qdev
, "size", size
);
673 qdev_prop_set_uint32(&dev
->qdev
, "io_base", io_base
);
674 qdev_init_nofail(&dev
->qdev
);
675 d
= DO_UPCAST(M48t59ISAState
, busdev
, dev
);
678 memory_region_init_io(&d
->io
, &m48t59_io_ops
, s
, "m48t59", 4);
680 isa_register_ioport(dev
, &d
->io
, io_base
);
686 static void m48t59_init_common(M48t59State
*s
)
688 s
->buffer
= g_malloc0(s
->size
);
690 s
->alrm_timer
= qemu_new_timer_ns(vm_clock
, &alarm_cb
, s
);
691 s
->wd_timer
= qemu_new_timer_ns(vm_clock
, &watchdog_cb
, s
);
693 qemu_get_timedate(&s
->alarm
, 0);
695 vmstate_register(NULL
, -1, &vmstate_m48t59
, s
);
698 static int m48t59_init_isa1(ISADevice
*dev
)
700 M48t59ISAState
*d
= DO_UPCAST(M48t59ISAState
, busdev
, dev
);
701 M48t59State
*s
= &d
->state
;
703 isa_init_irq(dev
, &s
->IRQ
, 8);
704 m48t59_init_common(s
);
709 static int m48t59_init1(SysBusDevice
*dev
)
711 M48t59SysBusState
*d
= FROM_SYSBUS(M48t59SysBusState
, dev
);
712 M48t59State
*s
= &d
->state
;
714 sysbus_init_irq(dev
, &s
->IRQ
);
716 memory_region_init_io(&s
->iomem
, &nvram_ops
, s
, "m48t59.nvram", s
->size
);
717 sysbus_init_mmio(dev
, &s
->iomem
);
718 m48t59_init_common(s
);
723 static ISADeviceInfo m48t59_isa_info
= {
724 .init
= m48t59_init_isa1
,
725 .qdev
.name
= "m48t59_isa",
726 .qdev
.size
= sizeof(M48t59ISAState
),
727 .qdev
.reset
= m48t59_reset_isa
,
729 .qdev
.props
= (Property
[]) {
730 DEFINE_PROP_UINT32("size", M48t59ISAState
, state
.size
, -1),
731 DEFINE_PROP_UINT32("type", M48t59ISAState
, state
.type
, -1),
732 DEFINE_PROP_HEX32( "io_base", M48t59ISAState
, state
.io_base
, 0),
733 DEFINE_PROP_END_OF_LIST(),
737 static SysBusDeviceInfo m48t59_info
= {
738 .init
= m48t59_init1
,
739 .qdev
.name
= "m48t59",
740 .qdev
.size
= sizeof(M48t59SysBusState
),
741 .qdev
.reset
= m48t59_reset_sysbus
,
742 .qdev
.props
= (Property
[]) {
743 DEFINE_PROP_UINT32("size", M48t59SysBusState
, state
.size
, -1),
744 DEFINE_PROP_UINT32("type", M48t59SysBusState
, state
.type
, -1),
745 DEFINE_PROP_HEX32( "io_base", M48t59SysBusState
, state
.io_base
, 0),
746 DEFINE_PROP_END_OF_LIST(),
750 static void m48t59_register_devices(void)
752 sysbus_register_withprop(&m48t59_info
);
753 isa_qdev_register(&m48t59_isa_info
);
756 device_init(m48t59_register_devices
)