]> git.ipfire.org Git - thirdparty/qemu.git/blob - hw/timer/m41t80.c
Include qemu/module.h where needed, drop it from qemu-common.h
[thirdparty/qemu.git] / hw / timer / m41t80.c
1 /*
2 * M41T80 serial rtc emulation
3 *
4 * Copyright (c) 2018 BALATON Zoltan
5 *
6 * This work is licensed under the GNU GPL license version 2 or later.
7 *
8 */
9
10 #include "qemu/osdep.h"
11 #include "qemu/log.h"
12 #include "qemu/module.h"
13 #include "qemu/timer.h"
14 #include "qemu/bcd.h"
15 #include "hw/i2c/i2c.h"
16
17 #define TYPE_M41T80 "m41t80"
18 #define M41T80(obj) OBJECT_CHECK(M41t80State, (obj), TYPE_M41T80)
19
20 typedef struct M41t80State {
21 I2CSlave parent_obj;
22 int8_t addr;
23 } M41t80State;
24
25 static void m41t80_realize(DeviceState *dev, Error **errp)
26 {
27 M41t80State *s = M41T80(dev);
28
29 s->addr = -1;
30 }
31
32 static int m41t80_send(I2CSlave *i2c, uint8_t data)
33 {
34 M41t80State *s = M41T80(i2c);
35
36 if (s->addr < 0) {
37 s->addr = data;
38 } else {
39 s->addr++;
40 }
41 return 0;
42 }
43
44 static uint8_t m41t80_recv(I2CSlave *i2c)
45 {
46 M41t80State *s = M41T80(i2c);
47 struct tm now;
48 qemu_timeval tv;
49
50 if (s->addr < 0) {
51 s->addr = 0;
52 }
53 if (s->addr >= 1 && s->addr <= 7) {
54 qemu_get_timedate(&now, -1);
55 }
56 switch (s->addr++) {
57 case 0:
58 qemu_gettimeofday(&tv);
59 return to_bcd(tv.tv_usec / 10000);
60 case 1:
61 return to_bcd(now.tm_sec);
62 case 2:
63 return to_bcd(now.tm_min);
64 case 3:
65 return to_bcd(now.tm_hour);
66 case 4:
67 return to_bcd(now.tm_wday);
68 case 5:
69 return to_bcd(now.tm_mday);
70 case 6:
71 return to_bcd(now.tm_mon + 1);
72 case 7:
73 return to_bcd(now.tm_year % 100);
74 case 8 ... 19:
75 qemu_log_mask(LOG_UNIMP, "%s: unimplemented register: %d\n",
76 __func__, s->addr - 1);
77 return 0;
78 default:
79 qemu_log_mask(LOG_GUEST_ERROR, "%s: invalid register: %d\n",
80 __func__, s->addr - 1);
81 return 0;
82 }
83 }
84
85 static int m41t80_event(I2CSlave *i2c, enum i2c_event event)
86 {
87 M41t80State *s = M41T80(i2c);
88
89 if (event == I2C_START_SEND) {
90 s->addr = -1;
91 }
92 return 0;
93 }
94
95 static void m41t80_class_init(ObjectClass *klass, void *data)
96 {
97 DeviceClass *dc = DEVICE_CLASS(klass);
98 I2CSlaveClass *sc = I2C_SLAVE_CLASS(klass);
99
100 dc->realize = m41t80_realize;
101 sc->send = m41t80_send;
102 sc->recv = m41t80_recv;
103 sc->event = m41t80_event;
104 }
105
106 static const TypeInfo m41t80_info = {
107 .name = TYPE_M41T80,
108 .parent = TYPE_I2C_SLAVE,
109 .instance_size = sizeof(M41t80State),
110 .class_init = m41t80_class_init,
111 };
112
113 static void m41t80_register_types(void)
114 {
115 type_register_static(&m41t80_info);
116 }
117
118 type_init(m41t80_register_types)