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