hw/sd/sdcard: Simplify cmd_valid_while_locked()
[qemu/ar7.git] / hw / rtc / m41t80.c
blob396d110ba2d266c5e6bd45892f6ecdea168cd19e
1 /*
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.
8 */
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 #include "qom/object.h"
19 #define TYPE_M41T80 "m41t80"
20 OBJECT_DECLARE_SIMPLE_TYPE(M41t80State, M41T80)
22 struct M41t80State {
23 I2CSlave parent_obj;
24 int8_t addr;
27 static void m41t80_realize(DeviceState *dev, Error **errp)
29 M41t80State *s = M41T80(dev);
31 s->addr = -1;
34 static int m41t80_send(I2CSlave *i2c, uint8_t data)
36 M41t80State *s = M41T80(i2c);
38 if (s->addr < 0) {
39 s->addr = data;
40 } else {
41 s->addr++;
43 return 0;
46 static uint8_t m41t80_recv(I2CSlave *i2c)
48 M41t80State *s = M41T80(i2c);
49 struct tm now;
50 qemu_timeval tv;
52 if (s->addr < 0) {
53 s->addr = 0;
55 if (s->addr >= 1 && s->addr <= 7) {
56 qemu_get_timedate(&now, -1);
58 switch (s->addr++) {
59 case 0:
60 qemu_gettimeofday(&tv);
61 return to_bcd(tv.tv_usec / 10000);
62 case 1:
63 return to_bcd(now.tm_sec);
64 case 2:
65 return to_bcd(now.tm_min);
66 case 3:
67 return to_bcd(now.tm_hour);
68 case 4:
69 return to_bcd(now.tm_wday);
70 case 5:
71 return to_bcd(now.tm_mday);
72 case 6:
73 return to_bcd(now.tm_mon + 1);
74 case 7:
75 return to_bcd(now.tm_year % 100);
76 case 8 ... 19:
77 qemu_log_mask(LOG_UNIMP, "%s: unimplemented register: %d\n",
78 __func__, s->addr - 1);
79 return 0;
80 default:
81 qemu_log_mask(LOG_GUEST_ERROR, "%s: invalid register: %d\n",
82 __func__, s->addr - 1);
83 return 0;
87 static int m41t80_event(I2CSlave *i2c, enum i2c_event event)
89 M41t80State *s = M41T80(i2c);
91 if (event == I2C_START_SEND) {
92 s->addr = -1;
94 return 0;
97 static void m41t80_class_init(ObjectClass *klass, void *data)
99 DeviceClass *dc = DEVICE_CLASS(klass);
100 I2CSlaveClass *sc = I2C_SLAVE_CLASS(klass);
102 dc->realize = m41t80_realize;
103 sc->send = m41t80_send;
104 sc->recv = m41t80_recv;
105 sc->event = m41t80_event;
108 static const TypeInfo m41t80_info = {
109 .name = TYPE_M41T80,
110 .parent = TYPE_I2C_SLAVE,
111 .instance_size = sizeof(M41t80State),
112 .class_init = m41t80_class_init,
115 static void m41t80_register_types(void)
117 type_register_static(&m41t80_info);
120 type_init(m41t80_register_types)