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