blob: b576d564387f06366d51fe5a81ed2dbe44edd62e [file] [log] [blame]
Paul Brook1dfe3942009-11-20 00:21:33 +00001/*
2 * MAXIM DS1338 I2C RTC+NVRAM
3 *
4 * Copyright (c) 2009 CodeSourcery.
5 * Written by Paul Brook
6 *
Matthew Fernandez8e31bf32011-06-26 12:21:35 +10007 * This code is licensed under the GNU GPL v2.
Paolo Bonzini6b620ca2012-01-13 17:44:23 +01008 *
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.
Paul Brook1dfe3942009-11-20 00:21:33 +000011 */
12
13#include "i2c.h"
14
Peter Maydellba4906a2012-10-12 11:54:38 +010015/* Size of NVRAM including both the user-accessible area and the
16 * secondary register area.
17 */
18#define NVRAM_SIZE 64
19
Paul Brook1dfe3942009-11-20 00:21:33 +000020typedef struct {
Anthony Liguori9e07bdf2011-12-04 20:28:27 -060021 I2CSlave i2c;
Peter Maydellf4741402012-10-12 11:54:38 +010022 int64_t offset;
Peter Maydellba4906a2012-10-12 11:54:38 +010023 uint8_t nvram[NVRAM_SIZE];
Peter Maydellf4741402012-10-12 11:54:38 +010024 int32_t ptr;
25 bool addr_byte;
Paul Brook1dfe3942009-11-20 00:21:33 +000026} DS1338State;
27
Peter Maydellf4741402012-10-12 11:54:38 +010028static const VMStateDescription vmstate_ds1338 = {
29 .name = "ds1338",
30 .version_id = 1,
31 .minimum_version_id = 1,
32 .minimum_version_id_old = 1,
33 .fields = (VMStateField[]) {
34 VMSTATE_I2C_SLAVE(i2c, DS1338State),
35 VMSTATE_INT64(offset, DS1338State),
36 VMSTATE_UINT8_ARRAY(nvram, DS1338State, NVRAM_SIZE),
37 VMSTATE_INT32(ptr, DS1338State),
38 VMSTATE_BOOL(addr_byte, DS1338State),
39 VMSTATE_END_OF_LIST()
40 }
41};
42
Peter Maydell35b87a82012-10-12 11:54:38 +010043static void capture_current_time(DS1338State *s)
44{
45 /* Capture the current time into the secondary registers
46 * which will be actually read by the data transfer operation.
47 */
Peter Maydell7f7fd0f2012-10-12 11:54:38 +010048 struct tm now;
49 qemu_get_timedate(&now, s->offset);
50 s->nvram[0] = to_bcd(now.tm_sec);
51 s->nvram[1] = to_bcd(now.tm_min);
Peter Maydell35b87a82012-10-12 11:54:38 +010052 if (s->nvram[2] & 0x40) {
Peter Maydell7f7fd0f2012-10-12 11:54:38 +010053 s->nvram[2] = (to_bcd((now.tm_hour % 12)) + 1) | 0x40;
54 if (now.tm_hour >= 12) {
Peter Maydell35b87a82012-10-12 11:54:38 +010055 s->nvram[2] |= 0x20;
56 }
57 } else {
Peter Maydell7f7fd0f2012-10-12 11:54:38 +010058 s->nvram[2] = to_bcd(now.tm_hour);
Peter Maydell35b87a82012-10-12 11:54:38 +010059 }
Peter Maydell7f7fd0f2012-10-12 11:54:38 +010060 s->nvram[3] = to_bcd(now.tm_wday) + 1;
61 s->nvram[4] = to_bcd(now.tm_mday);
62 s->nvram[5] = to_bcd(now.tm_mon) + 1;
63 s->nvram[6] = to_bcd(now.tm_year - 100);
Peter Maydell35b87a82012-10-12 11:54:38 +010064}
65
66static void inc_regptr(DS1338State *s)
67{
68 /* The register pointer wraps around after 0x3F; wraparound
69 * causes the current time/date to be retransferred into
70 * the secondary registers.
71 */
72 s->ptr = (s->ptr + 1) & (NVRAM_SIZE - 1);
73 if (!s->ptr) {
74 capture_current_time(s);
75 }
76}
77
Anthony Liguori9e07bdf2011-12-04 20:28:27 -060078static void ds1338_event(I2CSlave *i2c, enum i2c_event event)
Paul Brook1dfe3942009-11-20 00:21:33 +000079{
80 DS1338State *s = FROM_I2C_SLAVE(DS1338State, i2c);
81
82 switch (event) {
83 case I2C_START_RECV:
Peter Maydell35b87a82012-10-12 11:54:38 +010084 /* In h/w, capture happens on any START condition, not just a
85 * START_RECV, but there is no need to actually capture on
86 * START_SEND, because the guest can't get at that data
87 * without going through a START_RECV which would overwrite it.
88 */
89 capture_current_time(s);
Paul Brook1dfe3942009-11-20 00:21:33 +000090 break;
91 case I2C_START_SEND:
Peter Maydellf4741402012-10-12 11:54:38 +010092 s->addr_byte = true;
Paul Brook1dfe3942009-11-20 00:21:33 +000093 break;
94 default:
95 break;
96 }
97}
98
Anthony Liguori9e07bdf2011-12-04 20:28:27 -060099static int ds1338_recv(I2CSlave *i2c)
Paul Brook1dfe3942009-11-20 00:21:33 +0000100{
101 DS1338State *s = FROM_I2C_SLAVE(DS1338State, i2c);
102 uint8_t res;
103
104 res = s->nvram[s->ptr];
Peter Maydell35b87a82012-10-12 11:54:38 +0100105 inc_regptr(s);
Paul Brook1dfe3942009-11-20 00:21:33 +0000106 return res;
107}
108
Anthony Liguori9e07bdf2011-12-04 20:28:27 -0600109static int ds1338_send(I2CSlave *i2c, uint8_t data)
Paul Brook1dfe3942009-11-20 00:21:33 +0000110{
111 DS1338State *s = FROM_I2C_SLAVE(DS1338State, i2c);
112 if (s->addr_byte) {
Peter Maydellba4906a2012-10-12 11:54:38 +0100113 s->ptr = data & (NVRAM_SIZE - 1);
Peter Maydellf4741402012-10-12 11:54:38 +0100114 s->addr_byte = false;
Paul Brook1dfe3942009-11-20 00:21:33 +0000115 return 0;
116 }
Peter Maydellba4906a2012-10-12 11:54:38 +0100117 if (s->ptr < 8) {
Peter Maydell7f7fd0f2012-10-12 11:54:38 +0100118 struct tm now;
119 qemu_get_timedate(&now, s->offset);
Peter Maydellba4906a2012-10-12 11:54:38 +0100120 switch(s->ptr) {
Paul Brook1dfe3942009-11-20 00:21:33 +0000121 case 0:
122 /* TODO: Implement CH (stop) bit. */
Peter Maydell7f7fd0f2012-10-12 11:54:38 +0100123 now.tm_sec = from_bcd(data & 0x7f);
Paul Brook1dfe3942009-11-20 00:21:33 +0000124 break;
125 case 1:
Peter Maydell7f7fd0f2012-10-12 11:54:38 +0100126 now.tm_min = from_bcd(data & 0x7f);
Paul Brook1dfe3942009-11-20 00:21:33 +0000127 break;
128 case 2:
129 if (data & 0x40) {
130 if (data & 0x20) {
131 data = from_bcd(data & 0x4f) + 11;
132 } else {
133 data = from_bcd(data & 0x1f) - 1;
134 }
135 } else {
136 data = from_bcd(data);
137 }
Peter Maydell7f7fd0f2012-10-12 11:54:38 +0100138 now.tm_hour = data;
Paul Brook1dfe3942009-11-20 00:21:33 +0000139 break;
140 case 3:
Peter Maydell7f7fd0f2012-10-12 11:54:38 +0100141 now.tm_wday = from_bcd(data & 7) - 1;
Paul Brook1dfe3942009-11-20 00:21:33 +0000142 break;
143 case 4:
Peter Maydell7f7fd0f2012-10-12 11:54:38 +0100144 now.tm_mday = from_bcd(data & 0x3f);
Paul Brook1dfe3942009-11-20 00:21:33 +0000145 break;
146 case 5:
Peter Maydell7f7fd0f2012-10-12 11:54:38 +0100147 now.tm_mon = from_bcd(data & 0x1f) - 1;
Stefan Weilfbac6a72012-02-25 14:50:25 +0100148 break;
Paul Brook1dfe3942009-11-20 00:21:33 +0000149 case 6:
Peter Maydell7f7fd0f2012-10-12 11:54:38 +0100150 now.tm_year = from_bcd(data) + 100;
Paul Brook1dfe3942009-11-20 00:21:33 +0000151 break;
152 case 7:
153 /* Control register. Currently ignored. */
154 break;
155 }
Peter Maydell7f7fd0f2012-10-12 11:54:38 +0100156 s->offset = qemu_timedate_diff(&now);
Peter Maydellba4906a2012-10-12 11:54:38 +0100157 } else {
158 s->nvram[s->ptr] = data;
Paul Brook1dfe3942009-11-20 00:21:33 +0000159 }
Peter Maydell35b87a82012-10-12 11:54:38 +0100160 inc_regptr(s);
Paul Brook1dfe3942009-11-20 00:21:33 +0000161 return 0;
162}
163
Anthony Liguori9e07bdf2011-12-04 20:28:27 -0600164static int ds1338_init(I2CSlave *i2c)
Paul Brook1dfe3942009-11-20 00:21:33 +0000165{
166 return 0;
167}
168
Anthony Liguorib5ea9322011-12-04 20:39:20 -0600169static void ds1338_class_init(ObjectClass *klass, void *data)
170{
Peter Maydellf4741402012-10-12 11:54:38 +0100171 DeviceClass *dc = DEVICE_CLASS(klass);
Anthony Liguorib5ea9322011-12-04 20:39:20 -0600172 I2CSlaveClass *k = I2C_SLAVE_CLASS(klass);
173
174 k->init = ds1338_init;
175 k->event = ds1338_event;
176 k->recv = ds1338_recv;
177 k->send = ds1338_send;
Peter Maydellf4741402012-10-12 11:54:38 +0100178 dc->vmsd = &vmstate_ds1338;
Anthony Liguorib5ea9322011-12-04 20:39:20 -0600179}
180
Anthony Liguori39bffca2011-12-07 21:34:16 -0600181static TypeInfo ds1338_info = {
182 .name = "ds1338",
183 .parent = TYPE_I2C_SLAVE,
184 .instance_size = sizeof(DS1338State),
185 .class_init = ds1338_class_init,
Paul Brook1dfe3942009-11-20 00:21:33 +0000186};
187
Andreas Färber83f7d432012-02-09 15:20:55 +0100188static void ds1338_register_types(void)
Paul Brook1dfe3942009-11-20 00:21:33 +0000189{
Anthony Liguori39bffca2011-12-07 21:34:16 -0600190 type_register_static(&ds1338_info);
Paul Brook1dfe3942009-11-20 00:21:33 +0000191}
192
Andreas Färber83f7d432012-02-09 15:20:55 +0100193type_init(ds1338_register_types)