]> git.proxmox.com Git - mirror_qemu.git/blob - hw/timer/ds1338.c
spapr_pci: Fix broken naming of PCI bus
[mirror_qemu.git] / hw / timer / ds1338.c
1 /*
2 * MAXIM DS1338 I2C RTC+NVRAM
3 *
4 * Copyright (c) 2009 CodeSourcery.
5 * Written by Paul Brook
6 *
7 * This code is licensed under the GNU GPL v2.
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.
11 */
12
13 #include "qemu/osdep.h"
14 #include "qemu-common.h"
15 #include "hw/i2c/i2c.h"
16 #include "qemu/bcd.h"
17
18 /* Size of NVRAM including both the user-accessible area and the
19 * secondary register area.
20 */
21 #define NVRAM_SIZE 64
22
23 /* Flags definitions */
24 #define SECONDS_CH 0x80
25 #define HOURS_12 0x40
26 #define HOURS_PM 0x20
27 #define CTRL_OSF 0x20
28
29 #define TYPE_DS1338 "ds1338"
30 #define DS1338(obj) OBJECT_CHECK(DS1338State, (obj), TYPE_DS1338)
31
32 typedef struct DS1338State {
33 I2CSlave parent_obj;
34
35 int64_t offset;
36 uint8_t wday_offset;
37 uint8_t nvram[NVRAM_SIZE];
38 int32_t ptr;
39 bool addr_byte;
40 } DS1338State;
41
42 static const VMStateDescription vmstate_ds1338 = {
43 .name = "ds1338",
44 .version_id = 2,
45 .minimum_version_id = 1,
46 .fields = (VMStateField[]) {
47 VMSTATE_I2C_SLAVE(parent_obj, DS1338State),
48 VMSTATE_INT64(offset, DS1338State),
49 VMSTATE_UINT8_V(wday_offset, DS1338State, 2),
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
57 static 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 */
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);
66 if (s->nvram[2] & HOURS_12) {
67 int tmp = now.tm_hour;
68 if (tmp % 12 == 0) {
69 tmp += 12;
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);
75 }
76 } else {
77 s->nvram[2] = to_bcd(now.tm_hour);
78 }
79 s->nvram[3] = (now.tm_wday + s->wday_offset) % 7 + 1;
80 s->nvram[4] = to_bcd(now.tm_mday);
81 s->nvram[5] = to_bcd(now.tm_mon + 1);
82 s->nvram[6] = to_bcd(now.tm_year - 100);
83 }
84
85 static 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
97 static int ds1338_event(I2CSlave *i2c, enum i2c_event event)
98 {
99 DS1338State *s = DS1338(i2c);
100
101 switch (event) {
102 case I2C_START_RECV:
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);
109 break;
110 case I2C_START_SEND:
111 s->addr_byte = true;
112 break;
113 default:
114 break;
115 }
116
117 return 0;
118 }
119
120 static uint8_t ds1338_recv(I2CSlave *i2c)
121 {
122 DS1338State *s = DS1338(i2c);
123 uint8_t res;
124
125 res = s->nvram[s->ptr];
126 inc_regptr(s);
127 return res;
128 }
129
130 static int ds1338_send(I2CSlave *i2c, uint8_t data)
131 {
132 DS1338State *s = DS1338(i2c);
133
134 if (s->addr_byte) {
135 s->ptr = data & (NVRAM_SIZE - 1);
136 s->addr_byte = false;
137 return 0;
138 }
139 if (s->ptr < 7) {
140 /* Time register. */
141 struct tm now;
142 qemu_get_timedate(&now, s->offset);
143 switch(s->ptr) {
144 case 0:
145 /* TODO: Implement CH (stop) bit. */
146 now.tm_sec = from_bcd(data & 0x7f);
147 break;
148 case 1:
149 now.tm_min = from_bcd(data & 0x7f);
150 break;
151 case 2:
152 if (data & HOURS_12) {
153 int tmp = from_bcd(data & (HOURS_PM - 1));
154 if (data & HOURS_PM) {
155 tmp += 12;
156 }
157 if (tmp % 12 == 0) {
158 tmp -= 12;
159 }
160 now.tm_hour = tmp;
161 } else {
162 now.tm_hour = from_bcd(data & (HOURS_12 - 1));
163 }
164 break;
165 case 3:
166 {
167 /* The day field is supposed to contain a value in
168 the range 1-7. Otherwise behavior is undefined.
169 */
170 int user_wday = (data & 7) - 1;
171 s->wday_offset = (user_wday - now.tm_wday + 7) % 7;
172 }
173 break;
174 case 4:
175 now.tm_mday = from_bcd(data & 0x3f);
176 break;
177 case 5:
178 now.tm_mon = from_bcd(data & 0x1f) - 1;
179 break;
180 case 6:
181 now.tm_year = from_bcd(data) + 100;
182 break;
183 }
184 s->offset = qemu_timedate_diff(&now);
185 } else if (s->ptr == 7) {
186 /* Control register. */
187
188 /* Ensure bits 2, 3 and 6 will read back as zero. */
189 data &= 0xB3;
190
191 /* Attempting to write the OSF flag to logic 1 leaves the
192 value unchanged. */
193 data = (data & ~CTRL_OSF) | (data & s->nvram[s->ptr] & CTRL_OSF);
194
195 s->nvram[s->ptr] = data;
196 } else {
197 s->nvram[s->ptr] = data;
198 }
199 inc_regptr(s);
200 return 0;
201 }
202
203 static void ds1338_reset(DeviceState *dev)
204 {
205 DS1338State *s = DS1338(dev);
206
207 /* The clock is running and synchronized with the host */
208 s->offset = 0;
209 s->wday_offset = 0;
210 memset(s->nvram, 0, NVRAM_SIZE);
211 s->ptr = 0;
212 s->addr_byte = false;
213 }
214
215 static void ds1338_class_init(ObjectClass *klass, void *data)
216 {
217 DeviceClass *dc = DEVICE_CLASS(klass);
218 I2CSlaveClass *k = I2C_SLAVE_CLASS(klass);
219
220 k->event = ds1338_event;
221 k->recv = ds1338_recv;
222 k->send = ds1338_send;
223 dc->reset = ds1338_reset;
224 dc->vmsd = &vmstate_ds1338;
225 }
226
227 static const TypeInfo ds1338_info = {
228 .name = TYPE_DS1338,
229 .parent = TYPE_I2C_SLAVE,
230 .instance_size = sizeof(DS1338State),
231 .class_init = ds1338_class_init,
232 };
233
234 static void ds1338_register_types(void)
235 {
236 type_register_static(&ds1338_info);
237 }
238
239 type_init(ds1338_register_types)