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 "hw/i2c/i2c.h"
15 #include "migration/vmstate.h"
17 #include "qemu/module.h"
18 #include "qom/object.h"
19 #include "sysemu/rtc.h"
21 /* Size of NVRAM including both the user-accessible area and the
22 * secondary register area.
26 /* Flags definitions */
27 #define SECONDS_CH 0x80
32 #define TYPE_DS1338 "ds1338"
33 OBJECT_DECLARE_SIMPLE_TYPE(DS1338State
, DS1338
)
40 uint8_t nvram
[NVRAM_SIZE
];
45 static const VMStateDescription vmstate_ds1338
= {
48 .minimum_version_id
= 1,
49 .fields
= (VMStateField
[]) {
50 VMSTATE_I2C_SLAVE(parent_obj
, DS1338State
),
51 VMSTATE_INT64(offset
, DS1338State
),
52 VMSTATE_UINT8_V(wday_offset
, DS1338State
, 2),
53 VMSTATE_UINT8_ARRAY(nvram
, DS1338State
, NVRAM_SIZE
),
54 VMSTATE_INT32(ptr
, DS1338State
),
55 VMSTATE_BOOL(addr_byte
, DS1338State
),
60 static void capture_current_time(DS1338State
*s
)
62 /* Capture the current time into the secondary registers
63 * which will be actually read by the data transfer operation.
66 qemu_get_timedate(&now
, s
->offset
);
67 s
->nvram
[0] = to_bcd(now
.tm_sec
);
68 s
->nvram
[1] = to_bcd(now
.tm_min
);
69 if (s
->nvram
[2] & HOURS_12
) {
70 int tmp
= now
.tm_hour
;
75 s
->nvram
[2] = HOURS_12
| to_bcd(tmp
);
77 s
->nvram
[2] = HOURS_12
| HOURS_PM
| to_bcd(tmp
- 12);
80 s
->nvram
[2] = to_bcd(now
.tm_hour
);
82 s
->nvram
[3] = (now
.tm_wday
+ s
->wday_offset
) % 7 + 1;
83 s
->nvram
[4] = to_bcd(now
.tm_mday
);
84 s
->nvram
[5] = to_bcd(now
.tm_mon
+ 1);
85 s
->nvram
[6] = to_bcd(now
.tm_year
- 100);
88 static void inc_regptr(DS1338State
*s
)
90 /* The register pointer wraps around after 0x3F; wraparound
91 * causes the current time/date to be retransferred into
92 * the secondary registers.
94 s
->ptr
= (s
->ptr
+ 1) & (NVRAM_SIZE
- 1);
96 capture_current_time(s
);
100 static int ds1338_event(I2CSlave
*i2c
, enum i2c_event event
)
102 DS1338State
*s
= DS1338(i2c
);
106 /* In h/w, capture happens on any START condition, not just a
107 * START_RECV, but there is no need to actually capture on
108 * START_SEND, because the guest can't get at that data
109 * without going through a START_RECV which would overwrite it.
111 capture_current_time(s
);
123 static uint8_t ds1338_recv(I2CSlave
*i2c
)
125 DS1338State
*s
= DS1338(i2c
);
128 res
= s
->nvram
[s
->ptr
];
133 static int ds1338_send(I2CSlave
*i2c
, uint8_t data
)
135 DS1338State
*s
= DS1338(i2c
);
138 s
->ptr
= data
& (NVRAM_SIZE
- 1);
139 s
->addr_byte
= false;
145 qemu_get_timedate(&now
, s
->offset
);
148 /* TODO: Implement CH (stop) bit. */
149 now
.tm_sec
= from_bcd(data
& 0x7f);
152 now
.tm_min
= from_bcd(data
& 0x7f);
155 if (data
& HOURS_12
) {
156 int tmp
= from_bcd(data
& (HOURS_PM
- 1));
157 if (data
& HOURS_PM
) {
165 now
.tm_hour
= from_bcd(data
& (HOURS_12
- 1));
170 /* The day field is supposed to contain a value in
171 the range 1-7. Otherwise behavior is undefined.
173 int user_wday
= (data
& 7) - 1;
174 s
->wday_offset
= (user_wday
- now
.tm_wday
+ 7) % 7;
178 now
.tm_mday
= from_bcd(data
& 0x3f);
181 now
.tm_mon
= from_bcd(data
& 0x1f) - 1;
184 now
.tm_year
= from_bcd(data
) + 100;
187 s
->offset
= qemu_timedate_diff(&now
);
188 } else if (s
->ptr
== 7) {
189 /* Control register. */
191 /* Ensure bits 2, 3 and 6 will read back as zero. */
194 /* Attempting to write the OSF flag to logic 1 leaves the
196 data
= (data
& ~CTRL_OSF
) | (data
& s
->nvram
[s
->ptr
] & CTRL_OSF
);
198 s
->nvram
[s
->ptr
] = data
;
200 s
->nvram
[s
->ptr
] = data
;
206 static void ds1338_reset(DeviceState
*dev
)
208 DS1338State
*s
= DS1338(dev
);
210 /* The clock is running and synchronized with the host */
213 memset(s
->nvram
, 0, NVRAM_SIZE
);
215 s
->addr_byte
= false;
218 static void ds1338_class_init(ObjectClass
*klass
, void *data
)
220 DeviceClass
*dc
= DEVICE_CLASS(klass
);
221 I2CSlaveClass
*k
= I2C_SLAVE_CLASS(klass
);
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
= {
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
)