]> git.proxmox.com Git - mirror_qemu.git/blame - hw/timer/ds1338.c
util: move declarations out of qemu-common.h
[mirror_qemu.git] / hw / timer / ds1338.c
CommitLineData
1dfe3943
PB
1/*
2 * MAXIM DS1338 I2C RTC+NVRAM
3 *
4 * Copyright (c) 2009 CodeSourcery.
5 * Written by Paul Brook
6 *
8e31bf38 7 * This code is licensed under the GNU GPL v2.
6b620ca3
PB
8 *
9 * Contributions after 2012-01-13 are licensed under the terms of the
10 * GNU GPL, version 2 or (at your option) any later version.
1dfe3943
PB
11 */
12
282bc81e 13#include "qemu/osdep.h"
4771d756 14#include "qemu-common.h"
0d09e41a 15#include "hw/i2c/i2c.h"
f348b6d1 16#include "qemu/bcd.h"
1dfe3943 17
ba4906a9
PM
18/* Size of NVRAM including both the user-accessible area and the
19 * secondary register area.
20 */
21#define NVRAM_SIZE 64
22
95c93615
AM
23/* Flags definitions */
24#define SECONDS_CH 0x80
25#define HOURS_12 0x40
26#define HOURS_PM 0x20
27#define CTRL_OSF 0x20
28
59ab56b9
AF
29#define TYPE_DS1338 "ds1338"
30#define DS1338(obj) OBJECT_CHECK(DS1338State, (obj), TYPE_DS1338)
31
32typedef struct DS1338State {
33 I2CSlave parent_obj;
34
f4741402 35 int64_t offset;
871edc5f 36 uint8_t wday_offset;
ba4906a9 37 uint8_t nvram[NVRAM_SIZE];
f4741402
PM
38 int32_t ptr;
39 bool addr_byte;
1dfe3943
PB
40} DS1338State;
41
f4741402
PM
42static const VMStateDescription vmstate_ds1338 = {
43 .name = "ds1338",
871edc5f 44 .version_id = 2,
f4741402 45 .minimum_version_id = 1,
f4741402 46 .fields = (VMStateField[]) {
59ab56b9 47 VMSTATE_I2C_SLAVE(parent_obj, DS1338State),
f4741402 48 VMSTATE_INT64(offset, DS1338State),
871edc5f 49 VMSTATE_UINT8_V(wday_offset, DS1338State, 2),
f4741402
PM
50 VMSTATE_UINT8_ARRAY(nvram, DS1338State, NVRAM_SIZE),
51 VMSTATE_INT32(ptr, DS1338State),
52 VMSTATE_BOOL(addr_byte, DS1338State),
53 VMSTATE_END_OF_LIST()
54 }
55};
56
35b87a86
PM
57static void capture_current_time(DS1338State *s)
58{
59 /* Capture the current time into the secondary registers
60 * which will be actually read by the data transfer operation.
61 */
7f7fd0f2
PM
62 struct tm now;
63 qemu_get_timedate(&now, s->offset);
64 s->nvram[0] = to_bcd(now.tm_sec);
65 s->nvram[1] = to_bcd(now.tm_min);
59dda8e0
AM
66 if (s->nvram[2] & HOURS_12) {
67 int tmp = now.tm_hour;
5c78d6a8
AM
68 if (tmp % 12 == 0) {
69 tmp += 12;
59dda8e0
AM
70 }
71 if (tmp <= 12) {
72 s->nvram[2] = HOURS_12 | to_bcd(tmp);
73 } else {
74 s->nvram[2] = HOURS_12 | HOURS_PM | to_bcd(tmp - 12);
35b87a86
PM
75 }
76 } else {
7f7fd0f2 77 s->nvram[2] = to_bcd(now.tm_hour);
35b87a86 78 }
871edc5f 79 s->nvram[3] = (now.tm_wday + s->wday_offset) % 7 + 1;
7f7fd0f2 80 s->nvram[4] = to_bcd(now.tm_mday);
580f5c00 81 s->nvram[5] = to_bcd(now.tm_mon + 1);
7f7fd0f2 82 s->nvram[6] = to_bcd(now.tm_year - 100);
35b87a86
PM
83}
84
85static void inc_regptr(DS1338State *s)
86{
87 /* The register pointer wraps around after 0x3F; wraparound
88 * causes the current time/date to be retransferred into
89 * the secondary registers.
90 */
91 s->ptr = (s->ptr + 1) & (NVRAM_SIZE - 1);
92 if (!s->ptr) {
93 capture_current_time(s);
94 }
95}
96
9e07bdf8 97static void ds1338_event(I2CSlave *i2c, enum i2c_event event)
1dfe3943 98{
59ab56b9 99 DS1338State *s = DS1338(i2c);
1dfe3943
PB
100
101 switch (event) {
102 case I2C_START_RECV:
35b87a86
PM
103 /* In h/w, capture happens on any START condition, not just a
104 * START_RECV, but there is no need to actually capture on
105 * START_SEND, because the guest can't get at that data
106 * without going through a START_RECV which would overwrite it.
107 */
108 capture_current_time(s);
1dfe3943
PB
109 break;
110 case I2C_START_SEND:
f4741402 111 s->addr_byte = true;
1dfe3943
PB
112 break;
113 default:
114 break;
115 }
116}
117
9e07bdf8 118static int ds1338_recv(I2CSlave *i2c)
1dfe3943 119{
59ab56b9 120 DS1338State *s = DS1338(i2c);
1dfe3943
PB
121 uint8_t res;
122
123 res = s->nvram[s->ptr];
35b87a86 124 inc_regptr(s);
1dfe3943
PB
125 return res;
126}
127
9e07bdf8 128static int ds1338_send(I2CSlave *i2c, uint8_t data)
1dfe3943 129{
59ab56b9
AF
130 DS1338State *s = DS1338(i2c);
131
1dfe3943 132 if (s->addr_byte) {
ba4906a9 133 s->ptr = data & (NVRAM_SIZE - 1);
f4741402 134 s->addr_byte = false;
1dfe3943
PB
135 return 0;
136 }
996e91f0
AM
137 if (s->ptr < 7) {
138 /* Time register. */
7f7fd0f2
PM
139 struct tm now;
140 qemu_get_timedate(&now, s->offset);
ba4906a9 141 switch(s->ptr) {
1dfe3943
PB
142 case 0:
143 /* TODO: Implement CH (stop) bit. */
7f7fd0f2 144 now.tm_sec = from_bcd(data & 0x7f);
1dfe3943
PB
145 break;
146 case 1:
7f7fd0f2 147 now.tm_min = from_bcd(data & 0x7f);
1dfe3943
PB
148 break;
149 case 2:
59dda8e0
AM
150 if (data & HOURS_12) {
151 int tmp = from_bcd(data & (HOURS_PM - 1));
152 if (data & HOURS_PM) {
153 tmp += 12;
154 }
5c78d6a8
AM
155 if (tmp % 12 == 0) {
156 tmp -= 12;
1dfe3943 157 }
59dda8e0 158 now.tm_hour = tmp;
1dfe3943 159 } else {
59dda8e0 160 now.tm_hour = from_bcd(data & (HOURS_12 - 1));
1dfe3943 161 }
1dfe3943
PB
162 break;
163 case 3:
871edc5f
AM
164 {
165 /* The day field is supposed to contain a value in
166 the range 1-7. Otherwise behavior is undefined.
167 */
168 int user_wday = (data & 7) - 1;
169 s->wday_offset = (user_wday - now.tm_wday + 7) % 7;
170 }
1dfe3943
PB
171 break;
172 case 4:
7f7fd0f2 173 now.tm_mday = from_bcd(data & 0x3f);
1dfe3943
PB
174 break;
175 case 5:
7f7fd0f2 176 now.tm_mon = from_bcd(data & 0x1f) - 1;
fbac6a7d 177 break;
1dfe3943 178 case 6:
7f7fd0f2 179 now.tm_year = from_bcd(data) + 100;
1dfe3943 180 break;
1dfe3943 181 }
7f7fd0f2 182 s->offset = qemu_timedate_diff(&now);
996e91f0
AM
183 } else if (s->ptr == 7) {
184 /* Control register. */
185
186 /* Ensure bits 2, 3 and 6 will read back as zero. */
187 data &= 0xB3;
188
189 /* Attempting to write the OSF flag to logic 1 leaves the
190 value unchanged. */
191 data = (data & ~CTRL_OSF) | (data & s->nvram[s->ptr] & CTRL_OSF);
192
193 s->nvram[s->ptr] = data;
ba4906a9
PM
194 } else {
195 s->nvram[s->ptr] = data;
1dfe3943 196 }
35b87a86 197 inc_regptr(s);
1dfe3943
PB
198 return 0;
199}
200
9e07bdf8 201static int ds1338_init(I2CSlave *i2c)
1dfe3943
PB
202{
203 return 0;
204}
205
ed3d37d2
AM
206static void ds1338_reset(DeviceState *dev)
207{
59ab56b9 208 DS1338State *s = DS1338(dev);
ed3d37d2
AM
209
210 /* The clock is running and synchronized with the host */
211 s->offset = 0;
871edc5f 212 s->wday_offset = 0;
ed3d37d2
AM
213 memset(s->nvram, 0, NVRAM_SIZE);
214 s->ptr = 0;
215 s->addr_byte = false;
216}
217
b5ea9327
AL
218static void ds1338_class_init(ObjectClass *klass, void *data)
219{
f4741402 220 DeviceClass *dc = DEVICE_CLASS(klass);
b5ea9327
AL
221 I2CSlaveClass *k = I2C_SLAVE_CLASS(klass);
222
223 k->init = ds1338_init;
224 k->event = ds1338_event;
225 k->recv = ds1338_recv;
226 k->send = ds1338_send;
ed3d37d2 227 dc->reset = ds1338_reset;
f4741402 228 dc->vmsd = &vmstate_ds1338;
b5ea9327
AL
229}
230
8c43a6f0 231static const TypeInfo ds1338_info = {
59ab56b9 232 .name = TYPE_DS1338,
39bffca2
AL
233 .parent = TYPE_I2C_SLAVE,
234 .instance_size = sizeof(DS1338State),
235 .class_init = ds1338_class_init,
1dfe3943
PB
236};
237
83f7d43a 238static void ds1338_register_types(void)
1dfe3943 239{
39bffca2 240 type_register_static(&ds1338_info);
1dfe3943
PB
241}
242
83f7d43a 243type_init(ds1338_register_types)