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"
16 #include "qom/object.h"
17 #include "sysemu/rtc.h"
19 #define TYPE_M41T80 "m41t80"
20 OBJECT_DECLARE_SIMPLE_TYPE(M41t80State
, M41T80
)
27 static void m41t80_realize(DeviceState
*dev
, Error
**errp
)
29 M41t80State
*s
= M41T80(dev
);
34 static int m41t80_send(I2CSlave
*i2c
, uint8_t data
)
36 M41t80State
*s
= M41T80(i2c
);
46 static uint8_t m41t80_recv(I2CSlave
*i2c
)
48 M41t80State
*s
= M41T80(i2c
);
55 if (s
->addr
>= 1 && s
->addr
<= 7) {
56 qemu_get_timedate(&now
, -1);
60 rt
= g_get_real_time();
61 return to_bcd((rt
% G_USEC_PER_SEC
) / 10000);
63 return to_bcd(now
.tm_sec
);
65 return to_bcd(now
.tm_min
);
67 return to_bcd(now
.tm_hour
);
69 return to_bcd(now
.tm_wday
);
71 return to_bcd(now
.tm_mday
);
73 return to_bcd(now
.tm_mon
+ 1);
75 return to_bcd(now
.tm_year
% 100);
77 qemu_log_mask(LOG_UNIMP
, "%s: unimplemented register: %d\n",
78 __func__
, s
->addr
- 1);
81 qemu_log_mask(LOG_GUEST_ERROR
, "%s: invalid register: %d\n",
82 __func__
, s
->addr
- 1);
87 static int m41t80_event(I2CSlave
*i2c
, enum i2c_event event
)
89 M41t80State
*s
= M41T80(i2c
);
91 if (event
== I2C_START_SEND
) {
97 static void m41t80_class_init(ObjectClass
*klass
, void *data
)
99 DeviceClass
*dc
= DEVICE_CLASS(klass
);
100 I2CSlaveClass
*sc
= I2C_SLAVE_CLASS(klass
);
102 dc
->realize
= m41t80_realize
;
103 sc
->send
= m41t80_send
;
104 sc
->recv
= m41t80_recv
;
105 sc
->event
= m41t80_event
;
108 static const TypeInfo m41t80_info
= {
110 .parent
= TYPE_I2C_SLAVE
,
111 .instance_size
= sizeof(M41t80State
),
112 .class_init
= m41t80_class_init
,
115 static void m41t80_register_types(void)
117 type_register_static(&m41t80_info
);
120 type_init(m41t80_register_types
)