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 model; /* 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(rtc_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->model == 2 && addr < 0x7f8) ||
201 (NVRAM->model == 8 && addr < 0x1ff8) ||
202 (NVRAM->model == 59 && addr < 0x1ff0)) {
209 /* flags register : read-only */
216 tmp = from_bcd(val & 0x7F);
217 if (tmp >= 0 && tmp <= 59) {
218 NVRAM->alarm.tm_sec = tmp;
219 NVRAM->buffer[0x1FF2] = val;
225 tmp = from_bcd(val & 0x7F);
226 if (tmp >= 0 && tmp <= 59) {
227 NVRAM->alarm.tm_min = tmp;
228 NVRAM->buffer[0x1FF3] = val;
234 tmp = from_bcd(val & 0x3F);
235 if (tmp >= 0 && tmp <= 23) {
236 NVRAM->alarm.tm_hour = tmp;
237 NVRAM->buffer[0x1FF4] = val;
243 tmp = from_bcd(val & 0x3F);
245 NVRAM->alarm.tm_mday = tmp;
246 NVRAM->buffer[0x1FF5] = val;
252 NVRAM->buffer[0x1FF6] = val;
256 NVRAM->buffer[0x1FF7] = val;
257 set_up_watchdog(NVRAM, val);
262 NVRAM->buffer[addr] = (val & ~0xA0) | 0x90;
267 tmp = from_bcd(val & 0x7F);
268 if (tmp >= 0 && tmp <= 59) {
269 get_time(NVRAM, &tm);
271 set_time(NVRAM, &tm);
273 if ((val & 0x80) ^ (NVRAM->buffer[addr] & 0x80)) {
275 NVRAM->stop_time = time(NULL);
277 NVRAM->time_offset += NVRAM->stop_time - time(NULL);
278 NVRAM->stop_time = 0;
281 NVRAM->buffer[addr] = val & 0x80;
286 tmp = from_bcd(val & 0x7F);
287 if (tmp >= 0 && tmp <= 59) {
288 get_time(NVRAM, &tm);
290 set_time(NVRAM, &tm);
296 tmp = from_bcd(val & 0x3F);
297 if (tmp >= 0 && tmp <= 23) {
298 get_time(NVRAM, &tm);
300 set_time(NVRAM, &tm);
305 /* day of the week / century */
306 tmp = from_bcd(val & 0x07);
307 get_time(NVRAM, &tm);
309 set_time(NVRAM, &tm);
310 NVRAM->buffer[addr] = val & 0x40;
315 tmp = from_bcd(val & 0x3F);
317 get_time(NVRAM, &tm);
319 set_time(NVRAM, &tm);
325 tmp = from_bcd(val & 0x1F);
326 if (tmp >= 1 && tmp <= 12) {
327 get_time(NVRAM, &tm);
329 set_time(NVRAM, &tm);
336 if (tmp >= 0 && tmp <= 99) {
337 get_time(NVRAM, &tm);
338 if (NVRAM->model == 8) {
339 tm.tm_year = from_bcd(val) + 68; // Base year is 1968
341 tm.tm_year = from_bcd(val);
343 set_time(NVRAM, &tm);
347 /* Check lock registers state */
348 if (addr >= 0x20 && addr <= 0x2F && (NVRAM->lock & 1))
350 if (addr >= 0x30 && addr <= 0x3F && (NVRAM->lock & 2))
353 if (addr < NVRAM->size) {
354 NVRAM->buffer[addr] = val & 0xFF;
360 uint32_t m48t59_read (void *opaque, uint32_t addr)
362 M48t59State *NVRAM = opaque;
364 uint32_t retval = 0xFF;
366 /* check for NVRAM access */
367 if ((NVRAM->model == 2 && addr < 0x078f) ||
368 (NVRAM->model == 8 && addr < 0x1ff8) ||
369 (NVRAM->model == 59 && addr < 0x1ff0)) {
398 /* A read resets the watchdog */
399 set_up_watchdog(NVRAM, NVRAM->buffer[0x1FF7]);
408 get_time(NVRAM, &tm);
409 retval = (NVRAM->buffer[addr] & 0x80) | to_bcd(tm.tm_sec);
414 get_time(NVRAM, &tm);
415 retval = to_bcd(tm.tm_min);
420 get_time(NVRAM, &tm);
421 retval = to_bcd(tm.tm_hour);
425 /* day of the week / century */
426 get_time(NVRAM, &tm);
427 retval = NVRAM->buffer[addr] | tm.tm_wday;
432 get_time(NVRAM, &tm);
433 retval = to_bcd(tm.tm_mday);
438 get_time(NVRAM, &tm);
439 retval = to_bcd(tm.tm_mon + 1);
444 get_time(NVRAM, &tm);
445 if (NVRAM->model == 8) {
446 retval = to_bcd(tm.tm_year - 68); // Base year is 1968
448 retval = to_bcd(tm.tm_year);
452 /* Check lock registers state */
453 if (addr >= 0x20 && addr <= 0x2F && (NVRAM->lock & 1))
455 if (addr >= 0x30 && addr <= 0x3F && (NVRAM->lock & 2))
458 if (addr < NVRAM->size) {
459 retval = NVRAM->buffer[addr];
463 if (addr > 0x1FF9 && addr < 0x2000)
464 NVRAM_PRINTF("%s: 0x%08x <= 0x%08x\n", __func__, addr, retval);
469 void m48t59_set_addr (void *opaque, uint32_t addr)
471 M48t59State *NVRAM = opaque;
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, uint32_t addr, uint32_t val)
486 M48t59State *NVRAM = opaque;
488 NVRAM_PRINTF("%s: 0x%08x => 0x%08x\n", __func__, addr, val);
491 NVRAM->addr &= ~0x00FF;
495 NVRAM->addr &= ~0xFF00;
496 NVRAM->addr |= val << 8;
499 m48t59_write(NVRAM, NVRAM->addr, val);
500 NVRAM->addr = 0x0000;
507 static uint32_t NVRAM_readb (void *opaque, uint32_t addr)
509 M48t59State *NVRAM = opaque;
514 retval = m48t59_read(NVRAM, NVRAM->addr);
520 NVRAM_PRINTF("%s: 0x%08x <= 0x%08x\n", __func__, addr, retval);
525 static void nvram_writeb (void *opaque, hwaddr addr, uint32_t value)
527 M48t59State *NVRAM = opaque;
529 m48t59_write(NVRAM, addr, value & 0xff);
532 static void nvram_writew (void *opaque, hwaddr addr, uint32_t value)
534 M48t59State *NVRAM = opaque;
536 m48t59_write(NVRAM, addr, (value >> 8) & 0xff);
537 m48t59_write(NVRAM, addr + 1, value & 0xff);
540 static void nvram_writel (void *opaque, hwaddr addr, uint32_t value)
542 M48t59State *NVRAM = opaque;
544 m48t59_write(NVRAM, addr, (value >> 24) & 0xff);
545 m48t59_write(NVRAM, addr + 1, (value >> 16) & 0xff);
546 m48t59_write(NVRAM, addr + 2, (value >> 8) & 0xff);
547 m48t59_write(NVRAM, addr + 3, value & 0xff);
550 static uint32_t nvram_readb (void *opaque, hwaddr addr)
552 M48t59State *NVRAM = opaque;
555 retval = m48t59_read(NVRAM, addr);
559 static uint32_t nvram_readw (void *opaque, hwaddr addr)
561 M48t59State *NVRAM = opaque;
564 retval = m48t59_read(NVRAM, addr) << 8;
565 retval |= m48t59_read(NVRAM, addr + 1);
569 static uint32_t nvram_readl (void *opaque, hwaddr addr)
571 M48t59State *NVRAM = opaque;
574 retval = m48t59_read(NVRAM, addr) << 24;
575 retval |= m48t59_read(NVRAM, addr + 1) << 16;
576 retval |= m48t59_read(NVRAM, addr + 2) << 8;
577 retval |= m48t59_read(NVRAM, addr + 3);
581 static const MemoryRegionOps nvram_ops = {
583 .read = { nvram_readb, nvram_readw, nvram_readl, },
584 .write = { nvram_writeb, nvram_writew, nvram_writel, },
586 .endianness = DEVICE_NATIVE_ENDIAN,
589 static const VMStateDescription vmstate_m48t59 = {
592 .minimum_version_id = 1,
593 .minimum_version_id_old = 1,
594 .fields = (VMStateField[]) {
595 VMSTATE_UINT8(lock, M48t59State),
596 VMSTATE_UINT16(addr, M48t59State),
597 VMSTATE_VBUFFER_UINT32(buffer, M48t59State, 0, NULL, 0, size),
598 VMSTATE_END_OF_LIST()
602 static void m48t59_reset_common(M48t59State *NVRAM)
606 if (NVRAM->alrm_timer != NULL)
607 qemu_del_timer(NVRAM->alrm_timer);
609 if (NVRAM->wd_timer != NULL)
610 qemu_del_timer(NVRAM->wd_timer);
613 static void m48t59_reset_isa(DeviceState *d)
615 M48t59ISAState *isa = container_of(d, M48t59ISAState, busdev.qdev);
616 M48t59State *NVRAM = &isa->state;
618 m48t59_reset_common(NVRAM);
621 static void m48t59_reset_sysbus(DeviceState *d)
623 M48t59SysBusState *sys = container_of(d, M48t59SysBusState, busdev.qdev);
624 M48t59State *NVRAM = &sys->state;
626 m48t59_reset_common(NVRAM);
629 static const MemoryRegionPortio m48t59_portio[] = {
630 {0, 4, 1, .read = NVRAM_readb, .write = NVRAM_writeb },
631 PORTIO_END_OF_LIST(),
634 static const MemoryRegionOps m48t59_io_ops = {
635 .old_portio = m48t59_portio,
638 /* Initialisation routine */
639 M48t59State *m48t59_init(qemu_irq IRQ, hwaddr mem_base,
640 uint32_t io_base, uint16_t size, int model)
644 M48t59SysBusState *d;
647 dev = qdev_create(NULL, "m48t59");
648 qdev_prop_set_uint32(dev, "model", model);
649 qdev_prop_set_uint32(dev, "size", size);
650 qdev_prop_set_uint32(dev, "io_base", io_base);
651 qdev_init_nofail(dev);
652 s = sysbus_from_qdev(dev);
653 d = FROM_SYSBUS(M48t59SysBusState, s);
655 sysbus_connect_irq(s, 0, IRQ);
657 register_ioport_read(io_base, 0x04, 1, NVRAM_readb, state);
658 register_ioport_write(io_base, 0x04, 1, NVRAM_writeb, state);
661 sysbus_mmio_map(s, 0, mem_base);
667 M48t59State *m48t59_init_isa(ISABus *bus, uint32_t io_base, uint16_t size,
674 dev = isa_create(bus, "m48t59_isa");
675 qdev_prop_set_uint32(&dev->qdev, "model", model);
676 qdev_prop_set_uint32(&dev->qdev, "size", size);
677 qdev_prop_set_uint32(&dev->qdev, "io_base", io_base);
678 qdev_init_nofail(&dev->qdev);
679 d = DO_UPCAST(M48t59ISAState, busdev, dev);
682 memory_region_init_io(&d->io, &m48t59_io_ops, s, "m48t59", 4);
684 isa_register_ioport(dev, &d->io, io_base);
690 static void m48t59_init_common(M48t59State *s)
692 s->buffer = g_malloc0(s->size);
693 if (s->model == 59) {
694 s->alrm_timer = qemu_new_timer_ns(rtc_clock, &alarm_cb, s);
695 s->wd_timer = qemu_new_timer_ns(vm_clock, &watchdog_cb, s);
697 qemu_get_timedate(&s->alarm, 0);
699 vmstate_register(NULL, -1, &vmstate_m48t59, s);
702 static int m48t59_init_isa1(ISADevice *dev)
704 M48t59ISAState *d = DO_UPCAST(M48t59ISAState, busdev, dev);
705 M48t59State *s = &d->state;
707 isa_init_irq(dev, &s->IRQ, 8);
708 m48t59_init_common(s);
713 static int m48t59_init1(SysBusDevice *dev)
715 M48t59SysBusState *d = FROM_SYSBUS(M48t59SysBusState, dev);
716 M48t59State *s = &d->state;
718 sysbus_init_irq(dev, &s->IRQ);
720 memory_region_init_io(&s->iomem, &nvram_ops, s, "m48t59.nvram", s->size);
721 sysbus_init_mmio(dev, &s->iomem);
722 m48t59_init_common(s);
727 static Property m48t59_isa_properties[] = {
728 DEFINE_PROP_UINT32("size", M48t59ISAState, state.size, -1),
729 DEFINE_PROP_UINT32("model", M48t59ISAState, state.model, -1),
730 DEFINE_PROP_HEX32( "io_base", M48t59ISAState, state.io_base, 0),
731 DEFINE_PROP_END_OF_LIST(),
734 static void m48t59_init_class_isa1(ObjectClass *klass, void *data)
736 DeviceClass *dc = DEVICE_CLASS(klass);
737 ISADeviceClass *ic = ISA_DEVICE_CLASS(klass);
738 ic->init = m48t59_init_isa1;
740 dc->reset = m48t59_reset_isa;
741 dc->props = m48t59_isa_properties;
744 static TypeInfo m48t59_isa_info = {
745 .name = "m48t59_isa",
746 .parent = TYPE_ISA_DEVICE,
747 .instance_size = sizeof(M48t59ISAState),
748 .class_init = m48t59_init_class_isa1,
751 static Property m48t59_properties[] = {
752 DEFINE_PROP_UINT32("size", M48t59SysBusState, state.size, -1),
753 DEFINE_PROP_UINT32("model", M48t59SysBusState, state.model, -1),
754 DEFINE_PROP_HEX32( "io_base", M48t59SysBusState, state.io_base, 0),
755 DEFINE_PROP_END_OF_LIST(),
758 static void m48t59_class_init(ObjectClass *klass, void *data)
760 DeviceClass *dc = DEVICE_CLASS(klass);
761 SysBusDeviceClass *k = SYS_BUS_DEVICE_CLASS(klass);
763 k->init = m48t59_init1;
764 dc->reset = m48t59_reset_sysbus;
765 dc->props = m48t59_properties;
768 static TypeInfo m48t59_info = {
770 .parent = TYPE_SYS_BUS_DEVICE,
771 .instance_size = sizeof(M48t59SysBusState),
772 .class_init = m48t59_class_init,
775 static void m48t59_register_types(void)
777 type_register_static(&m48t59_info);
778 type_register_static(&m48t59_isa_info);
781 type_init(m48t59_register_types)