2 * M41T80 serial rtc emulation
4 * Copyright (c) 2018 BALATON Zoltan
6 * This work is licensed under the GNU GPL license version 2 or later.
10 #include "qemu/osdep.h"
12 #include "qemu/module.h"
13 #include "qemu/timer.h"
15 #include "hw/i2c/i2c.h"
17 #define TYPE_M41T80 "m41t80"
18 #define M41T80(obj) OBJECT_CHECK(M41t80State, (obj), TYPE_M41T80)
20 typedef struct M41t80State {
25 static void m41t80_realize(DeviceState *dev, Error **errp)
27 M41t80State *s = M41T80(dev);
32 static int m41t80_send(I2CSlave *i2c, uint8_t data)
34 M41t80State *s = M41T80(i2c);
44 static uint8_t m41t80_recv(I2CSlave *i2c)
46 M41t80State *s = M41T80(i2c);
53 if (s->addr >= 1 && s->addr <= 7) {
54 qemu_get_timedate(&now, -1);
58 qemu_gettimeofday(&tv);
59 return to_bcd(tv.tv_usec / 10000);
61 return to_bcd(now.tm_sec);
63 return to_bcd(now.tm_min);
65 return to_bcd(now.tm_hour);
67 return to_bcd(now.tm_wday);
69 return to_bcd(now.tm_mday);
71 return to_bcd(now.tm_mon + 1);
73 return to_bcd(now.tm_year % 100);
75 qemu_log_mask(LOG_UNIMP, "%s: unimplemented register: %d\n",
76 __func__, s->addr - 1);
79 qemu_log_mask(LOG_GUEST_ERROR, "%s: invalid register: %d\n",
80 __func__, s->addr - 1);
85 static int m41t80_event(I2CSlave *i2c, enum i2c_event event)
87 M41t80State *s = M41T80(i2c);
89 if (event == I2C_START_SEND) {
95 static void m41t80_class_init(ObjectClass *klass, void *data)
97 DeviceClass *dc = DEVICE_CLASS(klass);
98 I2CSlaveClass *sc = I2C_SLAVE_CLASS(klass);
100 dc->realize = m41t80_realize;
101 sc->send = m41t80_send;
102 sc->recv = m41t80_recv;
103 sc->event = m41t80_event;
106 static const TypeInfo m41t80_info = {
108 .parent = TYPE_I2C_SLAVE,
109 .instance_size = sizeof(M41t80State),
110 .class_init = m41t80_class_init,
113 static void m41t80_register_types(void)
115 type_register_static(&m41t80_info);
118 type_init(m41t80_register_types)