hw: explicitly include qemu-common.h and cpu.h
[qemu/ar7.git] / hw / timer / ds1338.c
blob385b7d3076311786daed3e993284cbfdf590a220
1 /*
2 * MAXIM DS1338 I2C RTC+NVRAM
4 * Copyright (c) 2009 CodeSourcery.
5 * Written by Paul Brook
7 * This code is licensed under the GNU GPL v2.
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.
13 #include "qemu/osdep.h"
14 #include "qemu-common.h"
15 #include "hw/i2c/i2c.h"
17 /* Size of NVRAM including both the user-accessible area and the
18 * secondary register area.
20 #define NVRAM_SIZE 64
22 /* Flags definitions */
23 #define SECONDS_CH 0x80
24 #define HOURS_12 0x40
25 #define HOURS_PM 0x20
26 #define CTRL_OSF 0x20
28 #define TYPE_DS1338 "ds1338"
29 #define DS1338(obj) OBJECT_CHECK(DS1338State, (obj), TYPE_DS1338)
31 typedef struct DS1338State {
32 I2CSlave parent_obj;
34 int64_t offset;
35 uint8_t wday_offset;
36 uint8_t nvram[NVRAM_SIZE];
37 int32_t ptr;
38 bool addr_byte;
39 } DS1338State;
41 static const VMStateDescription vmstate_ds1338 = {
42 .name = "ds1338",
43 .version_id = 2,
44 .minimum_version_id = 1,
45 .fields = (VMStateField[]) {
46 VMSTATE_I2C_SLAVE(parent_obj, DS1338State),
47 VMSTATE_INT64(offset, DS1338State),
48 VMSTATE_UINT8_V(wday_offset, DS1338State, 2),
49 VMSTATE_UINT8_ARRAY(nvram, DS1338State, NVRAM_SIZE),
50 VMSTATE_INT32(ptr, DS1338State),
51 VMSTATE_BOOL(addr_byte, DS1338State),
52 VMSTATE_END_OF_LIST()
56 static void capture_current_time(DS1338State *s)
58 /* Capture the current time into the secondary registers
59 * which will be actually read by the data transfer operation.
61 struct tm now;
62 qemu_get_timedate(&now, s->offset);
63 s->nvram[0] = to_bcd(now.tm_sec);
64 s->nvram[1] = to_bcd(now.tm_min);
65 if (s->nvram[2] & HOURS_12) {
66 int tmp = now.tm_hour;
67 if (tmp % 12 == 0) {
68 tmp += 12;
70 if (tmp <= 12) {
71 s->nvram[2] = HOURS_12 | to_bcd(tmp);
72 } else {
73 s->nvram[2] = HOURS_12 | HOURS_PM | to_bcd(tmp - 12);
75 } else {
76 s->nvram[2] = to_bcd(now.tm_hour);
78 s->nvram[3] = (now.tm_wday + s->wday_offset) % 7 + 1;
79 s->nvram[4] = to_bcd(now.tm_mday);
80 s->nvram[5] = to_bcd(now.tm_mon + 1);
81 s->nvram[6] = to_bcd(now.tm_year - 100);
84 static void inc_regptr(DS1338State *s)
86 /* The register pointer wraps around after 0x3F; wraparound
87 * causes the current time/date to be retransferred into
88 * the secondary registers.
90 s->ptr = (s->ptr + 1) & (NVRAM_SIZE - 1);
91 if (!s->ptr) {
92 capture_current_time(s);
96 static void ds1338_event(I2CSlave *i2c, enum i2c_event event)
98 DS1338State *s = DS1338(i2c);
100 switch (event) {
101 case I2C_START_RECV:
102 /* In h/w, capture happens on any START condition, not just a
103 * START_RECV, but there is no need to actually capture on
104 * START_SEND, because the guest can't get at that data
105 * without going through a START_RECV which would overwrite it.
107 capture_current_time(s);
108 break;
109 case I2C_START_SEND:
110 s->addr_byte = true;
111 break;
112 default:
113 break;
117 static int ds1338_recv(I2CSlave *i2c)
119 DS1338State *s = DS1338(i2c);
120 uint8_t res;
122 res = s->nvram[s->ptr];
123 inc_regptr(s);
124 return res;
127 static int ds1338_send(I2CSlave *i2c, uint8_t data)
129 DS1338State *s = DS1338(i2c);
131 if (s->addr_byte) {
132 s->ptr = data & (NVRAM_SIZE - 1);
133 s->addr_byte = false;
134 return 0;
136 if (s->ptr < 7) {
137 /* Time register. */
138 struct tm now;
139 qemu_get_timedate(&now, s->offset);
140 switch(s->ptr) {
141 case 0:
142 /* TODO: Implement CH (stop) bit. */
143 now.tm_sec = from_bcd(data & 0x7f);
144 break;
145 case 1:
146 now.tm_min = from_bcd(data & 0x7f);
147 break;
148 case 2:
149 if (data & HOURS_12) {
150 int tmp = from_bcd(data & (HOURS_PM - 1));
151 if (data & HOURS_PM) {
152 tmp += 12;
154 if (tmp % 12 == 0) {
155 tmp -= 12;
157 now.tm_hour = tmp;
158 } else {
159 now.tm_hour = from_bcd(data & (HOURS_12 - 1));
161 break;
162 case 3:
164 /* The day field is supposed to contain a value in
165 the range 1-7. Otherwise behavior is undefined.
167 int user_wday = (data & 7) - 1;
168 s->wday_offset = (user_wday - now.tm_wday + 7) % 7;
170 break;
171 case 4:
172 now.tm_mday = from_bcd(data & 0x3f);
173 break;
174 case 5:
175 now.tm_mon = from_bcd(data & 0x1f) - 1;
176 break;
177 case 6:
178 now.tm_year = from_bcd(data) + 100;
179 break;
181 s->offset = qemu_timedate_diff(&now);
182 } else if (s->ptr == 7) {
183 /* Control register. */
185 /* Ensure bits 2, 3 and 6 will read back as zero. */
186 data &= 0xB3;
188 /* Attempting to write the OSF flag to logic 1 leaves the
189 value unchanged. */
190 data = (data & ~CTRL_OSF) | (data & s->nvram[s->ptr] & CTRL_OSF);
192 s->nvram[s->ptr] = data;
193 } else {
194 s->nvram[s->ptr] = data;
196 inc_regptr(s);
197 return 0;
200 static int ds1338_init(I2CSlave *i2c)
202 return 0;
205 static void ds1338_reset(DeviceState *dev)
207 DS1338State *s = DS1338(dev);
209 /* The clock is running and synchronized with the host */
210 s->offset = 0;
211 s->wday_offset = 0;
212 memset(s->nvram, 0, NVRAM_SIZE);
213 s->ptr = 0;
214 s->addr_byte = false;
217 static void ds1338_class_init(ObjectClass *klass, void *data)
219 DeviceClass *dc = DEVICE_CLASS(klass);
220 I2CSlaveClass *k = I2C_SLAVE_CLASS(klass);
222 k->init = ds1338_init;
223 k->event = ds1338_event;
224 k->recv = ds1338_recv;
225 k->send = ds1338_send;
226 dc->reset = ds1338_reset;
227 dc->vmsd = &vmstate_ds1338;
230 static const TypeInfo ds1338_info = {
231 .name = TYPE_DS1338,
232 .parent = TYPE_I2C_SLAVE,
233 .instance_size = sizeof(DS1338State),
234 .class_init = ds1338_class_init,
237 static void ds1338_register_types(void)
239 type_register_static(&ds1338_info);
242 type_init(ds1338_register_types)