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