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/timer.h"
14 #include "hw/i2c/i2c.h"
16 #define TYPE_M41T80 "m41t80"
17 #define M41T80(obj) OBJECT_CHECK(M41t80State, (obj), TYPE_M41T80)
19 typedef struct M41t80State
{
24 static void m41t80_realize(DeviceState
*dev
, Error
**errp
)
26 M41t80State
*s
= M41T80(dev
);
31 static int m41t80_send(I2CSlave
*i2c
, uint8_t data
)
33 M41t80State
*s
= M41T80(i2c
);
43 static int m41t80_recv(I2CSlave
*i2c
)
45 M41t80State
*s
= M41T80(i2c
);
52 if (s
->addr
>= 1 && s
->addr
<= 7) {
53 qemu_get_timedate(&now
, -1);
57 qemu_gettimeofday(&tv
);
58 return to_bcd(tv
.tv_usec
/ 10000);
60 return to_bcd(now
.tm_sec
);
62 return to_bcd(now
.tm_min
);
64 return to_bcd(now
.tm_hour
);
66 return to_bcd(now
.tm_wday
);
68 return to_bcd(now
.tm_mday
);
70 return to_bcd(now
.tm_mon
+ 1);
72 return to_bcd(now
.tm_year
% 100);
74 qemu_log_mask(LOG_UNIMP
, "%s: unimplemented register: %d\n",
75 __func__
, s
->addr
- 1);
78 qemu_log_mask(LOG_GUEST_ERROR
, "%s: invalid register: %d\n",
79 __func__
, s
->addr
- 1);
84 static int m41t80_event(I2CSlave
*i2c
, enum i2c_event event
)
86 M41t80State
*s
= M41T80(i2c
);
88 if (event
== I2C_START_SEND
) {
94 static void m41t80_class_init(ObjectClass
*klass
, void *data
)
96 DeviceClass
*dc
= DEVICE_CLASS(klass
);
97 I2CSlaveClass
*sc
= I2C_SLAVE_CLASS(klass
);
99 dc
->realize
= m41t80_realize
;
100 sc
->send
= m41t80_send
;
101 sc
->recv
= m41t80_recv
;
102 sc
->event
= m41t80_event
;
105 static const TypeInfo m41t80_info
= {
107 .parent
= TYPE_I2C_SLAVE
,
108 .instance_size
= sizeof(M41t80State
),
109 .class_init
= m41t80_class_init
,
112 static void m41t80_register_types(void)
114 type_register_static(&m41t80_info
);
117 type_init(m41t80_register_types
)