]>
Commit | Line | Data |
---|---|---|
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 | ||
13 | #include "i2c.h" | |
14 | ||
ba4906a9 PM |
15 | /* Size of NVRAM including both the user-accessible area and the |
16 | * secondary register area. | |
17 | */ | |
18 | #define NVRAM_SIZE 64 | |
19 | ||
95c93615 AM |
20 | /* Flags definitions */ |
21 | #define SECONDS_CH 0x80 | |
22 | #define HOURS_12 0x40 | |
23 | #define HOURS_PM 0x20 | |
24 | #define CTRL_OSF 0x20 | |
25 | ||
1dfe3943 | 26 | typedef struct { |
9e07bdf8 | 27 | I2CSlave i2c; |
f4741402 | 28 | int64_t offset; |
ba4906a9 | 29 | uint8_t nvram[NVRAM_SIZE]; |
f4741402 PM |
30 | int32_t ptr; |
31 | bool addr_byte; | |
1dfe3943 PB |
32 | } DS1338State; |
33 | ||
f4741402 PM |
34 | static const VMStateDescription vmstate_ds1338 = { |
35 | .name = "ds1338", | |
36 | .version_id = 1, | |
37 | .minimum_version_id = 1, | |
38 | .minimum_version_id_old = 1, | |
39 | .fields = (VMStateField[]) { | |
40 | VMSTATE_I2C_SLAVE(i2c, DS1338State), | |
41 | VMSTATE_INT64(offset, DS1338State), | |
42 | VMSTATE_UINT8_ARRAY(nvram, DS1338State, NVRAM_SIZE), | |
43 | VMSTATE_INT32(ptr, DS1338State), | |
44 | VMSTATE_BOOL(addr_byte, DS1338State), | |
45 | VMSTATE_END_OF_LIST() | |
46 | } | |
47 | }; | |
48 | ||
35b87a86 PM |
49 | static void capture_current_time(DS1338State *s) |
50 | { | |
51 | /* Capture the current time into the secondary registers | |
52 | * which will be actually read by the data transfer operation. | |
53 | */ | |
7f7fd0f2 PM |
54 | struct tm now; |
55 | qemu_get_timedate(&now, s->offset); | |
56 | s->nvram[0] = to_bcd(now.tm_sec); | |
57 | s->nvram[1] = to_bcd(now.tm_min); | |
59dda8e0 AM |
58 | if (s->nvram[2] & HOURS_12) { |
59 | int tmp = now.tm_hour; | |
60 | if (tmp == 0) { | |
61 | tmp = 24; | |
62 | } | |
63 | if (tmp <= 12) { | |
64 | s->nvram[2] = HOURS_12 | to_bcd(tmp); | |
65 | } else { | |
66 | s->nvram[2] = HOURS_12 | HOURS_PM | to_bcd(tmp - 12); | |
35b87a86 PM |
67 | } |
68 | } else { | |
7f7fd0f2 | 69 | s->nvram[2] = to_bcd(now.tm_hour); |
35b87a86 | 70 | } |
580f5c00 | 71 | s->nvram[3] = to_bcd(now.tm_wday + 1); |
7f7fd0f2 | 72 | s->nvram[4] = to_bcd(now.tm_mday); |
580f5c00 | 73 | s->nvram[5] = to_bcd(now.tm_mon + 1); |
7f7fd0f2 | 74 | s->nvram[6] = to_bcd(now.tm_year - 100); |
35b87a86 PM |
75 | } |
76 | ||
77 | static void inc_regptr(DS1338State *s) | |
78 | { | |
79 | /* The register pointer wraps around after 0x3F; wraparound | |
80 | * causes the current time/date to be retransferred into | |
81 | * the secondary registers. | |
82 | */ | |
83 | s->ptr = (s->ptr + 1) & (NVRAM_SIZE - 1); | |
84 | if (!s->ptr) { | |
85 | capture_current_time(s); | |
86 | } | |
87 | } | |
88 | ||
9e07bdf8 | 89 | static void ds1338_event(I2CSlave *i2c, enum i2c_event event) |
1dfe3943 PB |
90 | { |
91 | DS1338State *s = FROM_I2C_SLAVE(DS1338State, i2c); | |
92 | ||
93 | switch (event) { | |
94 | case I2C_START_RECV: | |
35b87a86 PM |
95 | /* In h/w, capture happens on any START condition, not just a |
96 | * START_RECV, but there is no need to actually capture on | |
97 | * START_SEND, because the guest can't get at that data | |
98 | * without going through a START_RECV which would overwrite it. | |
99 | */ | |
100 | capture_current_time(s); | |
1dfe3943 PB |
101 | break; |
102 | case I2C_START_SEND: | |
f4741402 | 103 | s->addr_byte = true; |
1dfe3943 PB |
104 | break; |
105 | default: | |
106 | break; | |
107 | } | |
108 | } | |
109 | ||
9e07bdf8 | 110 | static int ds1338_recv(I2CSlave *i2c) |
1dfe3943 PB |
111 | { |
112 | DS1338State *s = FROM_I2C_SLAVE(DS1338State, i2c); | |
113 | uint8_t res; | |
114 | ||
115 | res = s->nvram[s->ptr]; | |
35b87a86 | 116 | inc_regptr(s); |
1dfe3943 PB |
117 | return res; |
118 | } | |
119 | ||
9e07bdf8 | 120 | static int ds1338_send(I2CSlave *i2c, uint8_t data) |
1dfe3943 PB |
121 | { |
122 | DS1338State *s = FROM_I2C_SLAVE(DS1338State, i2c); | |
123 | if (s->addr_byte) { | |
ba4906a9 | 124 | s->ptr = data & (NVRAM_SIZE - 1); |
f4741402 | 125 | s->addr_byte = false; |
1dfe3943 PB |
126 | return 0; |
127 | } | |
ba4906a9 | 128 | if (s->ptr < 8) { |
7f7fd0f2 PM |
129 | struct tm now; |
130 | qemu_get_timedate(&now, s->offset); | |
ba4906a9 | 131 | switch(s->ptr) { |
1dfe3943 PB |
132 | case 0: |
133 | /* TODO: Implement CH (stop) bit. */ | |
7f7fd0f2 | 134 | now.tm_sec = from_bcd(data & 0x7f); |
1dfe3943 PB |
135 | break; |
136 | case 1: | |
7f7fd0f2 | 137 | now.tm_min = from_bcd(data & 0x7f); |
1dfe3943 PB |
138 | break; |
139 | case 2: | |
59dda8e0 AM |
140 | if (data & HOURS_12) { |
141 | int tmp = from_bcd(data & (HOURS_PM - 1)); | |
142 | if (data & HOURS_PM) { | |
143 | tmp += 12; | |
144 | } | |
145 | if (tmp == 24) { | |
146 | tmp = 0; | |
1dfe3943 | 147 | } |
59dda8e0 | 148 | now.tm_hour = tmp; |
1dfe3943 | 149 | } else { |
59dda8e0 | 150 | now.tm_hour = from_bcd(data & (HOURS_12 - 1)); |
1dfe3943 | 151 | } |
1dfe3943 PB |
152 | break; |
153 | case 3: | |
7f7fd0f2 | 154 | now.tm_wday = from_bcd(data & 7) - 1; |
1dfe3943 PB |
155 | break; |
156 | case 4: | |
7f7fd0f2 | 157 | now.tm_mday = from_bcd(data & 0x3f); |
1dfe3943 PB |
158 | break; |
159 | case 5: | |
7f7fd0f2 | 160 | now.tm_mon = from_bcd(data & 0x1f) - 1; |
fbac6a7d | 161 | break; |
1dfe3943 | 162 | case 6: |
7f7fd0f2 | 163 | now.tm_year = from_bcd(data) + 100; |
1dfe3943 PB |
164 | break; |
165 | case 7: | |
166 | /* Control register. Currently ignored. */ | |
167 | break; | |
168 | } | |
7f7fd0f2 | 169 | s->offset = qemu_timedate_diff(&now); |
ba4906a9 PM |
170 | } else { |
171 | s->nvram[s->ptr] = data; | |
1dfe3943 | 172 | } |
35b87a86 | 173 | inc_regptr(s); |
1dfe3943 PB |
174 | return 0; |
175 | } | |
176 | ||
9e07bdf8 | 177 | static int ds1338_init(I2CSlave *i2c) |
1dfe3943 PB |
178 | { |
179 | return 0; | |
180 | } | |
181 | ||
b5ea9327 AL |
182 | static void ds1338_class_init(ObjectClass *klass, void *data) |
183 | { | |
f4741402 | 184 | DeviceClass *dc = DEVICE_CLASS(klass); |
b5ea9327 AL |
185 | I2CSlaveClass *k = I2C_SLAVE_CLASS(klass); |
186 | ||
187 | k->init = ds1338_init; | |
188 | k->event = ds1338_event; | |
189 | k->recv = ds1338_recv; | |
190 | k->send = ds1338_send; | |
f4741402 | 191 | dc->vmsd = &vmstate_ds1338; |
b5ea9327 AL |
192 | } |
193 | ||
39bffca2 AL |
194 | static TypeInfo ds1338_info = { |
195 | .name = "ds1338", | |
196 | .parent = TYPE_I2C_SLAVE, | |
197 | .instance_size = sizeof(DS1338State), | |
198 | .class_init = ds1338_class_init, | |
1dfe3943 PB |
199 | }; |
200 | ||
83f7d43a | 201 | static void ds1338_register_types(void) |
1dfe3943 | 202 | { |
39bffca2 | 203 | type_register_static(&ds1338_info); |
1dfe3943 PB |
204 | } |
205 | ||
83f7d43a | 206 | type_init(ds1338_register_types) |