2 * Samsung exynos4210 Real Time Clock
4 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, see <http://www.gnu.org/licenses/>.
24 * CLKSEL Bit[1] not used
25 * CLKOUTEN Bit[9] not used
28 #include "qemu/osdep.h"
30 #include "hw/sysbus.h"
31 #include "qemu/timer.h"
32 #include "qemu-common.h"
34 #include "hw/ptimer.h"
37 #include "sysemu/sysemu.h"
39 #include "hw/arm/exynos4210.h"
44 #define DPRINTF(fmt, ...) \
45 do { fprintf(stdout, "RTC: [%24s:%5d] " fmt, __func__, __LINE__, \
46 ## __VA_ARGS__); } while (0)
48 #define DPRINTF(fmt, ...) do {} while (0)
51 #define EXYNOS4210_RTC_REG_MEM_SIZE 0x0100
59 #define ALMHOUR 0x005C
62 #define ALMYEAR 0x0068
65 #define BCDHOUR 0x0078
67 #define BCDDAYWEEK 0x0080
69 #define BCDYEAR 0x0088
70 #define CURTICNT 0x0090
72 #define TICK_TIMER_ENABLE 0x0100
73 #define TICNT_THRESHOLD 2
76 #define RTC_ENABLE 0x0001
78 #define INTP_TICK_ENABLE 0x0001
79 #define INTP_ALM_ENABLE 0x0002
81 #define ALARM_INT_ENABLE 0x0040
83 #define RTC_BASE_FREQ 32768
85 #define TYPE_EXYNOS4210_RTC "exynos4210.rtc"
86 #define EXYNOS4210_RTC(obj) \
87 OBJECT_CHECK(Exynos4210RTCState, (obj), TYPE_EXYNOS4210_RTC)
89 typedef struct Exynos4210RTCState {
90 SysBusDevice parent_obj;
101 uint32_t reg_almhour;
104 uint32_t reg_almyear;
105 uint32_t reg_curticcnt;
107 ptimer_state *ptimer; /* tick timer */
108 ptimer_state *ptimer_1Hz; /* clock timer */
111 qemu_irq tick_irq; /* Time Tick Generator irq */
112 qemu_irq alm_irq; /* alarm irq */
114 struct tm current_tm; /* current time */
115 } Exynos4210RTCState;
117 #define TICCKSEL(value) ((value & (0x0F << 4)) >> 4)
120 static const VMStateDescription vmstate_exynos4210_rtc_state = {
121 .name = "exynos4210.rtc",
123 .minimum_version_id = 1,
124 .fields = (VMStateField[]) {
125 VMSTATE_UINT32(reg_intp, Exynos4210RTCState),
126 VMSTATE_UINT32(reg_rtccon, Exynos4210RTCState),
127 VMSTATE_UINT32(reg_ticcnt, Exynos4210RTCState),
128 VMSTATE_UINT32(reg_rtcalm, Exynos4210RTCState),
129 VMSTATE_UINT32(reg_almsec, Exynos4210RTCState),
130 VMSTATE_UINT32(reg_almmin, Exynos4210RTCState),
131 VMSTATE_UINT32(reg_almhour, Exynos4210RTCState),
132 VMSTATE_UINT32(reg_almday, Exynos4210RTCState),
133 VMSTATE_UINT32(reg_almmon, Exynos4210RTCState),
134 VMSTATE_UINT32(reg_almyear, Exynos4210RTCState),
135 VMSTATE_UINT32(reg_curticcnt, Exynos4210RTCState),
136 VMSTATE_PTIMER(ptimer, Exynos4210RTCState),
137 VMSTATE_PTIMER(ptimer_1Hz, Exynos4210RTCState),
138 VMSTATE_UINT32(freq, Exynos4210RTCState),
139 VMSTATE_INT32(current_tm.tm_sec, Exynos4210RTCState),
140 VMSTATE_INT32(current_tm.tm_min, Exynos4210RTCState),
141 VMSTATE_INT32(current_tm.tm_hour, Exynos4210RTCState),
142 VMSTATE_INT32(current_tm.tm_wday, Exynos4210RTCState),
143 VMSTATE_INT32(current_tm.tm_mday, Exynos4210RTCState),
144 VMSTATE_INT32(current_tm.tm_mon, Exynos4210RTCState),
145 VMSTATE_INT32(current_tm.tm_year, Exynos4210RTCState),
146 VMSTATE_END_OF_LIST()
150 #define BCD3DIGITS(x) \
151 ((uint32_t)to_bcd((uint8_t)(x % 100)) + \
152 ((uint32_t)to_bcd((uint8_t)((x % 1000) / 100)) << 8))
154 static void check_alarm_raise(Exynos4210RTCState *s)
156 unsigned int alarm_raise = 0;
157 struct tm stm = s->current_tm;
159 if ((s->reg_rtcalm & 0x01) &&
160 (to_bcd((uint8_t)stm.tm_sec) == (uint8_t)s->reg_almsec)) {
163 if ((s->reg_rtcalm & 0x02) &&
164 (to_bcd((uint8_t)stm.tm_min) == (uint8_t)s->reg_almmin)) {
167 if ((s->reg_rtcalm & 0x04) &&
168 (to_bcd((uint8_t)stm.tm_hour) == (uint8_t)s->reg_almhour)) {
171 if ((s->reg_rtcalm & 0x08) &&
172 (to_bcd((uint8_t)stm.tm_mday) == (uint8_t)s->reg_almday)) {
175 if ((s->reg_rtcalm & 0x10) &&
176 (to_bcd((uint8_t)stm.tm_mon) == (uint8_t)s->reg_almmon)) {
179 if ((s->reg_rtcalm & 0x20) &&
180 (BCD3DIGITS(stm.tm_year) == s->reg_almyear)) {
185 DPRINTF("ALARM IRQ\n");
187 s->reg_intp |= INTP_ALM_ENABLE;
188 qemu_irq_raise(s->alm_irq);
193 * RTC update frequency
195 * reg_value - current RTCCON register or his new value
197 static void exynos4210_rtc_update_freq(Exynos4210RTCState *s,
203 /* set frequncy for time generator */
204 s->freq = RTC_BASE_FREQ / (1 << TICCKSEL(reg_value));
206 if (freq != s->freq) {
207 ptimer_set_freq(s->ptimer, s->freq);
208 DPRINTF("freq=%dHz\n", s->freq);
212 /* month is between 0 and 11. */
213 static int get_days_in_month(int month, int year)
215 static const int days_tab[12] = {
216 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
219 if ((unsigned)month >= 12) {
224 if ((year % 4) == 0 && ((year % 100) != 0 || (year % 400) == 0)) {
231 /* update 'tm' to the next second */
232 static void rtc_next_second(struct tm *tm)
237 if ((unsigned)tm->tm_sec >= 60) {
240 if ((unsigned)tm->tm_min >= 60) {
243 if ((unsigned)tm->tm_hour >= 24) {
247 if ((unsigned)tm->tm_wday >= 7) {
250 days_in_month = get_days_in_month(tm->tm_mon,
253 if (tm->tm_mday < 1) {
255 } else if (tm->tm_mday > days_in_month) {
258 if (tm->tm_mon >= 12) {
271 static void exynos4210_rtc_tick(void *opaque)
273 Exynos4210RTCState *s = (Exynos4210RTCState *)opaque;
275 DPRINTF("TICK IRQ\n");
277 s->reg_intp |= INTP_TICK_ENABLE;
279 qemu_irq_raise(s->tick_irq);
282 ptimer_set_count(s->ptimer, s->reg_ticcnt);
283 ptimer_run(s->ptimer, 1);
289 static void exynos4210_rtc_1Hz_tick(void *opaque)
291 Exynos4210RTCState *s = (Exynos4210RTCState *)opaque;
293 rtc_next_second(&s->current_tm);
294 /* DPRINTF("1Hz tick\n"); */
297 if (s->reg_rtcalm & ALARM_INT_ENABLE) {
298 check_alarm_raise(s);
301 ptimer_set_count(s->ptimer_1Hz, RTC_BASE_FREQ);
302 ptimer_run(s->ptimer_1Hz, 1);
308 static uint64_t exynos4210_rtc_read(void *opaque, hwaddr offset,
312 Exynos4210RTCState *s = (Exynos4210RTCState *)opaque;
319 value = s->reg_rtccon;
322 value = s->reg_ticcnt;
325 value = s->reg_rtcalm;
328 value = s->reg_almsec;
331 value = s->reg_almmin;
334 value = s->reg_almhour;
337 value = s->reg_almday;
340 value = s->reg_almmon;
343 value = s->reg_almyear;
347 value = (uint32_t)to_bcd((uint8_t)s->current_tm.tm_sec);
350 value = (uint32_t)to_bcd((uint8_t)s->current_tm.tm_min);
353 value = (uint32_t)to_bcd((uint8_t)s->current_tm.tm_hour);
356 value = (uint32_t)to_bcd((uint8_t)s->current_tm.tm_wday);
359 value = (uint32_t)to_bcd((uint8_t)s->current_tm.tm_mday);
362 value = (uint32_t)to_bcd((uint8_t)s->current_tm.tm_mon + 1);
365 value = BCD3DIGITS(s->current_tm.tm_year);
369 s->reg_curticcnt = ptimer_get_count(s->ptimer);
370 value = s->reg_curticcnt;
374 qemu_log_mask(LOG_GUEST_ERROR,
375 "exynos4210.rtc: bad read offset " TARGET_FMT_plx,
385 static void exynos4210_rtc_write(void *opaque, hwaddr offset,
386 uint64_t value, unsigned size)
388 Exynos4210RTCState *s = (Exynos4210RTCState *)opaque;
392 if (value & INTP_ALM_ENABLE) {
393 qemu_irq_lower(s->alm_irq);
394 s->reg_intp &= (~INTP_ALM_ENABLE);
396 if (value & INTP_TICK_ENABLE) {
397 qemu_irq_lower(s->tick_irq);
398 s->reg_intp &= (~INTP_TICK_ENABLE);
402 if (value & RTC_ENABLE) {
403 exynos4210_rtc_update_freq(s, value);
405 if ((value & RTC_ENABLE) > (s->reg_rtccon & RTC_ENABLE)) {
407 ptimer_set_count(s->ptimer_1Hz, RTC_BASE_FREQ);
408 ptimer_run(s->ptimer_1Hz, 1);
409 DPRINTF("run clock timer\n");
411 if ((value & RTC_ENABLE) < (s->reg_rtccon & RTC_ENABLE)) {
413 ptimer_stop(s->ptimer);
415 ptimer_stop(s->ptimer_1Hz);
416 DPRINTF("stop all timers\n");
418 if (value & RTC_ENABLE) {
419 if ((value & TICK_TIMER_ENABLE) >
420 (s->reg_rtccon & TICK_TIMER_ENABLE) &&
422 ptimer_set_count(s->ptimer, s->reg_ticcnt);
423 ptimer_run(s->ptimer, 1);
424 DPRINTF("run tick timer\n");
426 if ((value & TICK_TIMER_ENABLE) <
427 (s->reg_rtccon & TICK_TIMER_ENABLE)) {
428 ptimer_stop(s->ptimer);
431 s->reg_rtccon = value;
434 if (value > TICNT_THRESHOLD) {
435 s->reg_ticcnt = value;
437 qemu_log_mask(LOG_GUEST_ERROR,
438 "exynos4210.rtc: bad TICNT value %u",
444 s->reg_rtcalm = value;
447 s->reg_almsec = (value & 0x7f);
450 s->reg_almmin = (value & 0x7f);
453 s->reg_almhour = (value & 0x3f);
456 s->reg_almday = (value & 0x3f);
459 s->reg_almmon = (value & 0x1f);
462 s->reg_almyear = (value & 0x0fff);
466 if (s->reg_rtccon & RTC_ENABLE) {
467 s->current_tm.tm_sec = (int)from_bcd((uint8_t)value);
471 if (s->reg_rtccon & RTC_ENABLE) {
472 s->current_tm.tm_min = (int)from_bcd((uint8_t)value);
476 if (s->reg_rtccon & RTC_ENABLE) {
477 s->current_tm.tm_hour = (int)from_bcd((uint8_t)value);
481 if (s->reg_rtccon & RTC_ENABLE) {
482 s->current_tm.tm_wday = (int)from_bcd((uint8_t)value);
486 if (s->reg_rtccon & RTC_ENABLE) {
487 s->current_tm.tm_mday = (int)from_bcd((uint8_t)value);
491 if (s->reg_rtccon & RTC_ENABLE) {
492 s->current_tm.tm_mon = (int)from_bcd((uint8_t)value) - 1;
496 if (s->reg_rtccon & RTC_ENABLE) {
498 s->current_tm.tm_year = (int)from_bcd((uint8_t)value) +
499 (int)from_bcd((uint8_t)((value >> 8) & 0x0f)) * 100;
504 qemu_log_mask(LOG_GUEST_ERROR,
505 "exynos4210.rtc: bad write offset " TARGET_FMT_plx,
513 * Set default values to timer fields and registers
515 static void exynos4210_rtc_reset(DeviceState *d)
517 Exynos4210RTCState *s = EXYNOS4210_RTC(d);
519 qemu_get_timedate(&s->current_tm, 0);
521 DPRINTF("Get time from host: %d-%d-%d %2d:%02d:%02d\n",
522 s->current_tm.tm_year, s->current_tm.tm_mon, s->current_tm.tm_mday,
523 s->current_tm.tm_hour, s->current_tm.tm_min, s->current_tm.tm_sec);
536 s->reg_curticcnt = 0;
538 exynos4210_rtc_update_freq(s, s->reg_rtccon);
539 ptimer_stop(s->ptimer);
540 ptimer_stop(s->ptimer_1Hz);
543 static const MemoryRegionOps exynos4210_rtc_ops = {
544 .read = exynos4210_rtc_read,
545 .write = exynos4210_rtc_write,
546 .endianness = DEVICE_NATIVE_ENDIAN,
550 * RTC timer initialization
552 static void exynos4210_rtc_init(Object *obj)
554 Exynos4210RTCState *s = EXYNOS4210_RTC(obj);
555 SysBusDevice *dev = SYS_BUS_DEVICE(obj);
558 bh = qemu_bh_new(exynos4210_rtc_tick, s);
559 s->ptimer = ptimer_init(bh, PTIMER_POLICY_DEFAULT);
560 ptimer_set_freq(s->ptimer, RTC_BASE_FREQ);
561 exynos4210_rtc_update_freq(s, 0);
563 bh = qemu_bh_new(exynos4210_rtc_1Hz_tick, s);
564 s->ptimer_1Hz = ptimer_init(bh, PTIMER_POLICY_DEFAULT);
565 ptimer_set_freq(s->ptimer_1Hz, RTC_BASE_FREQ);
567 sysbus_init_irq(dev, &s->alm_irq);
568 sysbus_init_irq(dev, &s->tick_irq);
570 memory_region_init_io(&s->iomem, obj, &exynos4210_rtc_ops, s,
571 "exynos4210-rtc", EXYNOS4210_RTC_REG_MEM_SIZE);
572 sysbus_init_mmio(dev, &s->iomem);
575 static void exynos4210_rtc_class_init(ObjectClass *klass, void *data)
577 DeviceClass *dc = DEVICE_CLASS(klass);
579 dc->reset = exynos4210_rtc_reset;
580 dc->vmsd = &vmstate_exynos4210_rtc_state;
583 static const TypeInfo exynos4210_rtc_info = {
584 .name = TYPE_EXYNOS4210_RTC,
585 .parent = TYPE_SYS_BUS_DEVICE,
586 .instance_size = sizeof(Exynos4210RTCState),
587 .instance_init = exynos4210_rtc_init,
588 .class_init = exynos4210_rtc_class_init,
591 static void exynos4210_rtc_register_types(void)
593 type_register_static(&exynos4210_rtc_info);
596 type_init(exynos4210_rtc_register_types)