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"
29 #include "qemu-common.h"
31 #include "qemu/main-loop.h"
32 #include "qemu/module.h"
33 #include "hw/sysbus.h"
34 #include "migration/vmstate.h"
35 #include "qemu/timer.h"
37 #include "hw/ptimer.h"
40 #include "sysemu/sysemu.h"
42 #include "hw/arm/exynos4210.h"
47 #define DPRINTF(fmt, ...) \
48 do { fprintf(stdout, "RTC: [%24s:%5d] " fmt, __func__, __LINE__, \
49 ## __VA_ARGS__); } while (0)
51 #define DPRINTF(fmt, ...) do {} while (0)
54 #define EXYNOS4210_RTC_REG_MEM_SIZE 0x0100
62 #define ALMHOUR 0x005C
65 #define ALMYEAR 0x0068
68 #define BCDHOUR 0x0078
70 #define BCDDAYWEEK 0x0080
72 #define BCDYEAR 0x0088
73 #define CURTICNT 0x0090
75 #define TICK_TIMER_ENABLE 0x0100
76 #define TICNT_THRESHOLD 2
79 #define RTC_ENABLE 0x0001
81 #define INTP_TICK_ENABLE 0x0001
82 #define INTP_ALM_ENABLE 0x0002
84 #define ALARM_INT_ENABLE 0x0040
86 #define RTC_BASE_FREQ 32768
88 #define TYPE_EXYNOS4210_RTC "exynos4210.rtc"
89 #define EXYNOS4210_RTC(obj) \
90 OBJECT_CHECK(Exynos4210RTCState, (obj), TYPE_EXYNOS4210_RTC)
92 typedef struct Exynos4210RTCState {
93 SysBusDevice parent_obj;
104 uint32_t reg_almhour;
107 uint32_t reg_almyear;
108 uint32_t reg_curticcnt;
110 ptimer_state *ptimer; /* tick timer */
111 ptimer_state *ptimer_1Hz; /* clock timer */
114 qemu_irq tick_irq; /* Time Tick Generator irq */
115 qemu_irq alm_irq; /* alarm irq */
117 struct tm current_tm; /* current time */
118 } Exynos4210RTCState;
120 #define TICCKSEL(value) ((value & (0x0F << 4)) >> 4)
123 static const VMStateDescription vmstate_exynos4210_rtc_state = {
124 .name = "exynos4210.rtc",
126 .minimum_version_id = 1,
127 .fields = (VMStateField[]) {
128 VMSTATE_UINT32(reg_intp, Exynos4210RTCState),
129 VMSTATE_UINT32(reg_rtccon, Exynos4210RTCState),
130 VMSTATE_UINT32(reg_ticcnt, Exynos4210RTCState),
131 VMSTATE_UINT32(reg_rtcalm, Exynos4210RTCState),
132 VMSTATE_UINT32(reg_almsec, Exynos4210RTCState),
133 VMSTATE_UINT32(reg_almmin, Exynos4210RTCState),
134 VMSTATE_UINT32(reg_almhour, Exynos4210RTCState),
135 VMSTATE_UINT32(reg_almday, Exynos4210RTCState),
136 VMSTATE_UINT32(reg_almmon, Exynos4210RTCState),
137 VMSTATE_UINT32(reg_almyear, Exynos4210RTCState),
138 VMSTATE_UINT32(reg_curticcnt, Exynos4210RTCState),
139 VMSTATE_PTIMER(ptimer, Exynos4210RTCState),
140 VMSTATE_PTIMER(ptimer_1Hz, Exynos4210RTCState),
141 VMSTATE_UINT32(freq, Exynos4210RTCState),
142 VMSTATE_INT32(current_tm.tm_sec, Exynos4210RTCState),
143 VMSTATE_INT32(current_tm.tm_min, Exynos4210RTCState),
144 VMSTATE_INT32(current_tm.tm_hour, Exynos4210RTCState),
145 VMSTATE_INT32(current_tm.tm_wday, Exynos4210RTCState),
146 VMSTATE_INT32(current_tm.tm_mday, Exynos4210RTCState),
147 VMSTATE_INT32(current_tm.tm_mon, Exynos4210RTCState),
148 VMSTATE_INT32(current_tm.tm_year, Exynos4210RTCState),
149 VMSTATE_END_OF_LIST()
153 #define BCD3DIGITS(x) \
154 ((uint32_t)to_bcd((uint8_t)(x % 100)) + \
155 ((uint32_t)to_bcd((uint8_t)((x % 1000) / 100)) << 8))
157 static void check_alarm_raise(Exynos4210RTCState *s)
159 unsigned int alarm_raise = 0;
160 struct tm stm = s->current_tm;
162 if ((s->reg_rtcalm & 0x01) &&
163 (to_bcd((uint8_t)stm.tm_sec) == (uint8_t)s->reg_almsec)) {
166 if ((s->reg_rtcalm & 0x02) &&
167 (to_bcd((uint8_t)stm.tm_min) == (uint8_t)s->reg_almmin)) {
170 if ((s->reg_rtcalm & 0x04) &&
171 (to_bcd((uint8_t)stm.tm_hour) == (uint8_t)s->reg_almhour)) {
174 if ((s->reg_rtcalm & 0x08) &&
175 (to_bcd((uint8_t)stm.tm_mday) == (uint8_t)s->reg_almday)) {
178 if ((s->reg_rtcalm & 0x10) &&
179 (to_bcd((uint8_t)stm.tm_mon) == (uint8_t)s->reg_almmon)) {
182 if ((s->reg_rtcalm & 0x20) &&
183 (BCD3DIGITS(stm.tm_year) == s->reg_almyear)) {
188 DPRINTF("ALARM IRQ\n");
190 s->reg_intp |= INTP_ALM_ENABLE;
191 qemu_irq_raise(s->alm_irq);
196 * RTC update frequency
198 * reg_value - current RTCCON register or his new value
200 static void exynos4210_rtc_update_freq(Exynos4210RTCState *s,
206 /* set frequncy for time generator */
207 s->freq = RTC_BASE_FREQ / (1 << TICCKSEL(reg_value));
209 if (freq != s->freq) {
210 ptimer_set_freq(s->ptimer, s->freq);
211 DPRINTF("freq=%dHz\n", s->freq);
215 /* month is between 0 and 11. */
216 static int get_days_in_month(int month, int year)
218 static const int days_tab[12] = {
219 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
222 if ((unsigned)month >= 12) {
227 if ((year % 4) == 0 && ((year % 100) != 0 || (year % 400) == 0)) {
234 /* update 'tm' to the next second */
235 static void rtc_next_second(struct tm *tm)
240 if ((unsigned)tm->tm_sec >= 60) {
243 if ((unsigned)tm->tm_min >= 60) {
246 if ((unsigned)tm->tm_hour >= 24) {
250 if ((unsigned)tm->tm_wday >= 7) {
253 days_in_month = get_days_in_month(tm->tm_mon,
256 if (tm->tm_mday < 1) {
258 } else if (tm->tm_mday > days_in_month) {
261 if (tm->tm_mon >= 12) {
274 static void exynos4210_rtc_tick(void *opaque)
276 Exynos4210RTCState *s = (Exynos4210RTCState *)opaque;
278 DPRINTF("TICK IRQ\n");
280 s->reg_intp |= INTP_TICK_ENABLE;
282 qemu_irq_raise(s->tick_irq);
285 ptimer_set_count(s->ptimer, s->reg_ticcnt);
286 ptimer_run(s->ptimer, 1);
292 static void exynos4210_rtc_1Hz_tick(void *opaque)
294 Exynos4210RTCState *s = (Exynos4210RTCState *)opaque;
296 rtc_next_second(&s->current_tm);
297 /* DPRINTF("1Hz tick\n"); */
300 if (s->reg_rtcalm & ALARM_INT_ENABLE) {
301 check_alarm_raise(s);
304 ptimer_set_count(s->ptimer_1Hz, RTC_BASE_FREQ);
305 ptimer_run(s->ptimer_1Hz, 1);
311 static uint64_t exynos4210_rtc_read(void *opaque, hwaddr offset,
315 Exynos4210RTCState *s = (Exynos4210RTCState *)opaque;
322 value = s->reg_rtccon;
325 value = s->reg_ticcnt;
328 value = s->reg_rtcalm;
331 value = s->reg_almsec;
334 value = s->reg_almmin;
337 value = s->reg_almhour;
340 value = s->reg_almday;
343 value = s->reg_almmon;
346 value = s->reg_almyear;
350 value = (uint32_t)to_bcd((uint8_t)s->current_tm.tm_sec);
353 value = (uint32_t)to_bcd((uint8_t)s->current_tm.tm_min);
356 value = (uint32_t)to_bcd((uint8_t)s->current_tm.tm_hour);
359 value = (uint32_t)to_bcd((uint8_t)s->current_tm.tm_wday);
362 value = (uint32_t)to_bcd((uint8_t)s->current_tm.tm_mday);
365 value = (uint32_t)to_bcd((uint8_t)s->current_tm.tm_mon + 1);
368 value = BCD3DIGITS(s->current_tm.tm_year);
372 s->reg_curticcnt = ptimer_get_count(s->ptimer);
373 value = s->reg_curticcnt;
377 qemu_log_mask(LOG_GUEST_ERROR,
378 "exynos4210.rtc: bad read offset " TARGET_FMT_plx,
388 static void exynos4210_rtc_write(void *opaque, hwaddr offset,
389 uint64_t value, unsigned size)
391 Exynos4210RTCState *s = (Exynos4210RTCState *)opaque;
395 if (value & INTP_ALM_ENABLE) {
396 qemu_irq_lower(s->alm_irq);
397 s->reg_intp &= (~INTP_ALM_ENABLE);
399 if (value & INTP_TICK_ENABLE) {
400 qemu_irq_lower(s->tick_irq);
401 s->reg_intp &= (~INTP_TICK_ENABLE);
405 if (value & RTC_ENABLE) {
406 exynos4210_rtc_update_freq(s, value);
408 if ((value & RTC_ENABLE) > (s->reg_rtccon & RTC_ENABLE)) {
410 ptimer_set_count(s->ptimer_1Hz, RTC_BASE_FREQ);
411 ptimer_run(s->ptimer_1Hz, 1);
412 DPRINTF("run clock timer\n");
414 if ((value & RTC_ENABLE) < (s->reg_rtccon & RTC_ENABLE)) {
416 ptimer_stop(s->ptimer);
418 ptimer_stop(s->ptimer_1Hz);
419 DPRINTF("stop all timers\n");
421 if (value & RTC_ENABLE) {
422 if ((value & TICK_TIMER_ENABLE) >
423 (s->reg_rtccon & TICK_TIMER_ENABLE) &&
425 ptimer_set_count(s->ptimer, s->reg_ticcnt);
426 ptimer_run(s->ptimer, 1);
427 DPRINTF("run tick timer\n");
429 if ((value & TICK_TIMER_ENABLE) <
430 (s->reg_rtccon & TICK_TIMER_ENABLE)) {
431 ptimer_stop(s->ptimer);
434 s->reg_rtccon = value;
437 if (value > TICNT_THRESHOLD) {
438 s->reg_ticcnt = value;
440 qemu_log_mask(LOG_GUEST_ERROR,
441 "exynos4210.rtc: bad TICNT value %u",
447 s->reg_rtcalm = value;
450 s->reg_almsec = (value & 0x7f);
453 s->reg_almmin = (value & 0x7f);
456 s->reg_almhour = (value & 0x3f);
459 s->reg_almday = (value & 0x3f);
462 s->reg_almmon = (value & 0x1f);
465 s->reg_almyear = (value & 0x0fff);
469 if (s->reg_rtccon & RTC_ENABLE) {
470 s->current_tm.tm_sec = (int)from_bcd((uint8_t)value);
474 if (s->reg_rtccon & RTC_ENABLE) {
475 s->current_tm.tm_min = (int)from_bcd((uint8_t)value);
479 if (s->reg_rtccon & RTC_ENABLE) {
480 s->current_tm.tm_hour = (int)from_bcd((uint8_t)value);
484 if (s->reg_rtccon & RTC_ENABLE) {
485 s->current_tm.tm_wday = (int)from_bcd((uint8_t)value);
489 if (s->reg_rtccon & RTC_ENABLE) {
490 s->current_tm.tm_mday = (int)from_bcd((uint8_t)value);
494 if (s->reg_rtccon & RTC_ENABLE) {
495 s->current_tm.tm_mon = (int)from_bcd((uint8_t)value) - 1;
499 if (s->reg_rtccon & RTC_ENABLE) {
501 s->current_tm.tm_year = (int)from_bcd((uint8_t)value) +
502 (int)from_bcd((uint8_t)((value >> 8) & 0x0f)) * 100;
507 qemu_log_mask(LOG_GUEST_ERROR,
508 "exynos4210.rtc: bad write offset " TARGET_FMT_plx,
516 * Set default values to timer fields and registers
518 static void exynos4210_rtc_reset(DeviceState *d)
520 Exynos4210RTCState *s = EXYNOS4210_RTC(d);
522 qemu_get_timedate(&s->current_tm, 0);
524 DPRINTF("Get time from host: %d-%d-%d %2d:%02d:%02d\n",
525 s->current_tm.tm_year, s->current_tm.tm_mon, s->current_tm.tm_mday,
526 s->current_tm.tm_hour, s->current_tm.tm_min, s->current_tm.tm_sec);
539 s->reg_curticcnt = 0;
541 exynos4210_rtc_update_freq(s, s->reg_rtccon);
542 ptimer_stop(s->ptimer);
543 ptimer_stop(s->ptimer_1Hz);
546 static const MemoryRegionOps exynos4210_rtc_ops = {
547 .read = exynos4210_rtc_read,
548 .write = exynos4210_rtc_write,
549 .endianness = DEVICE_NATIVE_ENDIAN,
553 * RTC timer initialization
555 static void exynos4210_rtc_init(Object *obj)
557 Exynos4210RTCState *s = EXYNOS4210_RTC(obj);
558 SysBusDevice *dev = SYS_BUS_DEVICE(obj);
561 bh = qemu_bh_new(exynos4210_rtc_tick, s);
562 s->ptimer = ptimer_init(bh, PTIMER_POLICY_DEFAULT);
563 ptimer_set_freq(s->ptimer, RTC_BASE_FREQ);
564 exynos4210_rtc_update_freq(s, 0);
566 bh = qemu_bh_new(exynos4210_rtc_1Hz_tick, s);
567 s->ptimer_1Hz = ptimer_init(bh, PTIMER_POLICY_DEFAULT);
568 ptimer_set_freq(s->ptimer_1Hz, RTC_BASE_FREQ);
570 sysbus_init_irq(dev, &s->alm_irq);
571 sysbus_init_irq(dev, &s->tick_irq);
573 memory_region_init_io(&s->iomem, obj, &exynos4210_rtc_ops, s,
574 "exynos4210-rtc", EXYNOS4210_RTC_REG_MEM_SIZE);
575 sysbus_init_mmio(dev, &s->iomem);
578 static void exynos4210_rtc_class_init(ObjectClass *klass, void *data)
580 DeviceClass *dc = DEVICE_CLASS(klass);
582 dc->reset = exynos4210_rtc_reset;
583 dc->vmsd = &vmstate_exynos4210_rtc_state;
586 static const TypeInfo exynos4210_rtc_info = {
587 .name = TYPE_EXYNOS4210_RTC,
588 .parent = TYPE_SYS_BUS_DEVICE,
589 .instance_size = sizeof(Exynos4210RTCState),
590 .instance_init = exynos4210_rtc_init,
591 .class_init = exynos4210_rtc_class_init,
594 static void exynos4210_rtc_register_types(void)
596 type_register_static(&exynos4210_rtc_info);
599 type_init(exynos4210_rtc_register_types)