2 * Samsung exynos4210 Real Time Clock
4 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 * Ogurtsov Oleg <o.ogurtsov@samsung.com>
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"
41 #include "hw/arm/exynos4210.h"
46 #define DPRINTF(fmt, ...) \
47 do { fprintf(stdout, "RTC: [%24s:%5d] " fmt, __func__, __LINE__, \
48 ## __VA_ARGS__); } while (0)
50 #define DPRINTF(fmt, ...) do {} while (0)
53 #define EXYNOS4210_RTC_REG_MEM_SIZE 0x0100
61 #define ALMHOUR 0x005C
64 #define ALMYEAR 0x0068
67 #define BCDHOUR 0x0078
69 #define BCDDAYWEEK 0x0080
71 #define BCDYEAR 0x0088
72 #define CURTICNT 0x0090
74 #define TICK_TIMER_ENABLE 0x0100
75 #define TICNT_THRESHOLD 2
78 #define RTC_ENABLE 0x0001
80 #define INTP_TICK_ENABLE 0x0001
81 #define INTP_ALM_ENABLE 0x0002
83 #define ALARM_INT_ENABLE 0x0040
85 #define RTC_BASE_FREQ 32768
87 #define TYPE_EXYNOS4210_RTC "exynos4210.rtc"
88 #define EXYNOS4210_RTC(obj) \
89 OBJECT_CHECK(Exynos4210RTCState, (obj), TYPE_EXYNOS4210_RTC)
91 typedef struct Exynos4210RTCState
{
92 SysBusDevice parent_obj
;
103 uint32_t reg_almhour
;
106 uint32_t reg_almyear
;
107 uint32_t reg_curticcnt
;
109 ptimer_state
*ptimer
; /* tick timer */
110 ptimer_state
*ptimer_1Hz
; /* clock timer */
113 qemu_irq tick_irq
; /* Time Tick Generator irq */
114 qemu_irq alm_irq
; /* alarm irq */
116 struct tm current_tm
; /* current time */
117 } Exynos4210RTCState
;
119 #define TICCKSEL(value) ((value & (0x0F << 4)) >> 4)
122 static const VMStateDescription vmstate_exynos4210_rtc_state
= {
123 .name
= "exynos4210.rtc",
125 .minimum_version_id
= 1,
126 .fields
= (VMStateField
[]) {
127 VMSTATE_UINT32(reg_intp
, Exynos4210RTCState
),
128 VMSTATE_UINT32(reg_rtccon
, Exynos4210RTCState
),
129 VMSTATE_UINT32(reg_ticcnt
, Exynos4210RTCState
),
130 VMSTATE_UINT32(reg_rtcalm
, Exynos4210RTCState
),
131 VMSTATE_UINT32(reg_almsec
, Exynos4210RTCState
),
132 VMSTATE_UINT32(reg_almmin
, Exynos4210RTCState
),
133 VMSTATE_UINT32(reg_almhour
, Exynos4210RTCState
),
134 VMSTATE_UINT32(reg_almday
, Exynos4210RTCState
),
135 VMSTATE_UINT32(reg_almmon
, Exynos4210RTCState
),
136 VMSTATE_UINT32(reg_almyear
, Exynos4210RTCState
),
137 VMSTATE_UINT32(reg_curticcnt
, Exynos4210RTCState
),
138 VMSTATE_PTIMER(ptimer
, Exynos4210RTCState
),
139 VMSTATE_PTIMER(ptimer_1Hz
, Exynos4210RTCState
),
140 VMSTATE_UINT32(freq
, Exynos4210RTCState
),
141 VMSTATE_INT32(current_tm
.tm_sec
, Exynos4210RTCState
),
142 VMSTATE_INT32(current_tm
.tm_min
, Exynos4210RTCState
),
143 VMSTATE_INT32(current_tm
.tm_hour
, Exynos4210RTCState
),
144 VMSTATE_INT32(current_tm
.tm_wday
, Exynos4210RTCState
),
145 VMSTATE_INT32(current_tm
.tm_mday
, Exynos4210RTCState
),
146 VMSTATE_INT32(current_tm
.tm_mon
, Exynos4210RTCState
),
147 VMSTATE_INT32(current_tm
.tm_year
, Exynos4210RTCState
),
148 VMSTATE_END_OF_LIST()
152 #define BCD3DIGITS(x) \
153 ((uint32_t)to_bcd((uint8_t)(x % 100)) + \
154 ((uint32_t)to_bcd((uint8_t)((x % 1000) / 100)) << 8))
156 static void check_alarm_raise(Exynos4210RTCState
*s
)
158 unsigned int alarm_raise
= 0;
159 struct tm stm
= s
->current_tm
;
161 if ((s
->reg_rtcalm
& 0x01) &&
162 (to_bcd((uint8_t)stm
.tm_sec
) == (uint8_t)s
->reg_almsec
)) {
165 if ((s
->reg_rtcalm
& 0x02) &&
166 (to_bcd((uint8_t)stm
.tm_min
) == (uint8_t)s
->reg_almmin
)) {
169 if ((s
->reg_rtcalm
& 0x04) &&
170 (to_bcd((uint8_t)stm
.tm_hour
) == (uint8_t)s
->reg_almhour
)) {
173 if ((s
->reg_rtcalm
& 0x08) &&
174 (to_bcd((uint8_t)stm
.tm_mday
) == (uint8_t)s
->reg_almday
)) {
177 if ((s
->reg_rtcalm
& 0x10) &&
178 (to_bcd((uint8_t)stm
.tm_mon
) == (uint8_t)s
->reg_almmon
)) {
181 if ((s
->reg_rtcalm
& 0x20) &&
182 (BCD3DIGITS(stm
.tm_year
) == s
->reg_almyear
)) {
187 DPRINTF("ALARM IRQ\n");
189 s
->reg_intp
|= INTP_ALM_ENABLE
;
190 qemu_irq_raise(s
->alm_irq
);
195 * RTC update frequency
197 * reg_value - current RTCCON register or his new value
199 static void exynos4210_rtc_update_freq(Exynos4210RTCState
*s
,
205 /* set frequncy for time generator */
206 s
->freq
= RTC_BASE_FREQ
/ (1 << TICCKSEL(reg_value
));
208 if (freq
!= s
->freq
) {
209 ptimer_set_freq(s
->ptimer
, s
->freq
);
210 DPRINTF("freq=%dHz\n", s
->freq
);
214 /* month is between 0 and 11. */
215 static int get_days_in_month(int month
, int year
)
217 static const int days_tab
[12] = {
218 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
221 if ((unsigned)month
>= 12) {
226 if ((year
% 4) == 0 && ((year
% 100) != 0 || (year
% 400) == 0)) {
233 /* update 'tm' to the next second */
234 static void rtc_next_second(struct tm
*tm
)
239 if ((unsigned)tm
->tm_sec
>= 60) {
242 if ((unsigned)tm
->tm_min
>= 60) {
245 if ((unsigned)tm
->tm_hour
>= 24) {
249 if ((unsigned)tm
->tm_wday
>= 7) {
252 days_in_month
= get_days_in_month(tm
->tm_mon
,
255 if (tm
->tm_mday
< 1) {
257 } else if (tm
->tm_mday
> days_in_month
) {
260 if (tm
->tm_mon
>= 12) {
273 static void exynos4210_rtc_tick(void *opaque
)
275 Exynos4210RTCState
*s
= (Exynos4210RTCState
*)opaque
;
277 DPRINTF("TICK IRQ\n");
279 s
->reg_intp
|= INTP_TICK_ENABLE
;
281 qemu_irq_raise(s
->tick_irq
);
284 ptimer_set_count(s
->ptimer
, s
->reg_ticcnt
);
285 ptimer_run(s
->ptimer
, 1);
291 static void exynos4210_rtc_1Hz_tick(void *opaque
)
293 Exynos4210RTCState
*s
= (Exynos4210RTCState
*)opaque
;
295 rtc_next_second(&s
->current_tm
);
296 /* DPRINTF("1Hz tick\n"); */
299 if (s
->reg_rtcalm
& ALARM_INT_ENABLE
) {
300 check_alarm_raise(s
);
303 ptimer_set_count(s
->ptimer_1Hz
, RTC_BASE_FREQ
);
304 ptimer_run(s
->ptimer_1Hz
, 1);
310 static uint64_t exynos4210_rtc_read(void *opaque
, hwaddr offset
,
314 Exynos4210RTCState
*s
= (Exynos4210RTCState
*)opaque
;
321 value
= s
->reg_rtccon
;
324 value
= s
->reg_ticcnt
;
327 value
= s
->reg_rtcalm
;
330 value
= s
->reg_almsec
;
333 value
= s
->reg_almmin
;
336 value
= s
->reg_almhour
;
339 value
= s
->reg_almday
;
342 value
= s
->reg_almmon
;
345 value
= s
->reg_almyear
;
349 value
= (uint32_t)to_bcd((uint8_t)s
->current_tm
.tm_sec
);
352 value
= (uint32_t)to_bcd((uint8_t)s
->current_tm
.tm_min
);
355 value
= (uint32_t)to_bcd((uint8_t)s
->current_tm
.tm_hour
);
358 value
= (uint32_t)to_bcd((uint8_t)s
->current_tm
.tm_wday
);
361 value
= (uint32_t)to_bcd((uint8_t)s
->current_tm
.tm_mday
);
364 value
= (uint32_t)to_bcd((uint8_t)s
->current_tm
.tm_mon
+ 1);
367 value
= BCD3DIGITS(s
->current_tm
.tm_year
);
371 s
->reg_curticcnt
= ptimer_get_count(s
->ptimer
);
372 value
= s
->reg_curticcnt
;
376 qemu_log_mask(LOG_GUEST_ERROR
,
377 "exynos4210.rtc: bad read offset " TARGET_FMT_plx
,
387 static void exynos4210_rtc_write(void *opaque
, hwaddr offset
,
388 uint64_t value
, unsigned size
)
390 Exynos4210RTCState
*s
= (Exynos4210RTCState
*)opaque
;
394 if (value
& INTP_ALM_ENABLE
) {
395 qemu_irq_lower(s
->alm_irq
);
396 s
->reg_intp
&= (~INTP_ALM_ENABLE
);
398 if (value
& INTP_TICK_ENABLE
) {
399 qemu_irq_lower(s
->tick_irq
);
400 s
->reg_intp
&= (~INTP_TICK_ENABLE
);
404 if (value
& RTC_ENABLE
) {
405 exynos4210_rtc_update_freq(s
, value
);
407 if ((value
& RTC_ENABLE
) > (s
->reg_rtccon
& RTC_ENABLE
)) {
409 ptimer_set_count(s
->ptimer_1Hz
, RTC_BASE_FREQ
);
410 ptimer_run(s
->ptimer_1Hz
, 1);
411 DPRINTF("run clock timer\n");
413 if ((value
& RTC_ENABLE
) < (s
->reg_rtccon
& RTC_ENABLE
)) {
415 ptimer_stop(s
->ptimer
);
417 ptimer_stop(s
->ptimer_1Hz
);
418 DPRINTF("stop all timers\n");
420 if (value
& RTC_ENABLE
) {
421 if ((value
& TICK_TIMER_ENABLE
) >
422 (s
->reg_rtccon
& TICK_TIMER_ENABLE
) &&
424 ptimer_set_count(s
->ptimer
, s
->reg_ticcnt
);
425 ptimer_run(s
->ptimer
, 1);
426 DPRINTF("run tick timer\n");
428 if ((value
& TICK_TIMER_ENABLE
) <
429 (s
->reg_rtccon
& TICK_TIMER_ENABLE
)) {
430 ptimer_stop(s
->ptimer
);
433 s
->reg_rtccon
= value
;
436 if (value
> TICNT_THRESHOLD
) {
437 s
->reg_ticcnt
= value
;
439 qemu_log_mask(LOG_GUEST_ERROR
,
440 "exynos4210.rtc: bad TICNT value %u",
446 s
->reg_rtcalm
= value
;
449 s
->reg_almsec
= (value
& 0x7f);
452 s
->reg_almmin
= (value
& 0x7f);
455 s
->reg_almhour
= (value
& 0x3f);
458 s
->reg_almday
= (value
& 0x3f);
461 s
->reg_almmon
= (value
& 0x1f);
464 s
->reg_almyear
= (value
& 0x0fff);
468 if (s
->reg_rtccon
& RTC_ENABLE
) {
469 s
->current_tm
.tm_sec
= (int)from_bcd((uint8_t)value
);
473 if (s
->reg_rtccon
& RTC_ENABLE
) {
474 s
->current_tm
.tm_min
= (int)from_bcd((uint8_t)value
);
478 if (s
->reg_rtccon
& RTC_ENABLE
) {
479 s
->current_tm
.tm_hour
= (int)from_bcd((uint8_t)value
);
483 if (s
->reg_rtccon
& RTC_ENABLE
) {
484 s
->current_tm
.tm_wday
= (int)from_bcd((uint8_t)value
);
488 if (s
->reg_rtccon
& RTC_ENABLE
) {
489 s
->current_tm
.tm_mday
= (int)from_bcd((uint8_t)value
);
493 if (s
->reg_rtccon
& RTC_ENABLE
) {
494 s
->current_tm
.tm_mon
= (int)from_bcd((uint8_t)value
) - 1;
498 if (s
->reg_rtccon
& RTC_ENABLE
) {
500 s
->current_tm
.tm_year
= (int)from_bcd((uint8_t)value
) +
501 (int)from_bcd((uint8_t)((value
>> 8) & 0x0f)) * 100;
506 qemu_log_mask(LOG_GUEST_ERROR
,
507 "exynos4210.rtc: bad write offset " TARGET_FMT_plx
,
515 * Set default values to timer fields and registers
517 static void exynos4210_rtc_reset(DeviceState
*d
)
519 Exynos4210RTCState
*s
= EXYNOS4210_RTC(d
);
521 qemu_get_timedate(&s
->current_tm
, 0);
523 DPRINTF("Get time from host: %d-%d-%d %2d:%02d:%02d\n",
524 s
->current_tm
.tm_year
, s
->current_tm
.tm_mon
, s
->current_tm
.tm_mday
,
525 s
->current_tm
.tm_hour
, s
->current_tm
.tm_min
, s
->current_tm
.tm_sec
);
538 s
->reg_curticcnt
= 0;
540 exynos4210_rtc_update_freq(s
, s
->reg_rtccon
);
541 ptimer_stop(s
->ptimer
);
542 ptimer_stop(s
->ptimer_1Hz
);
545 static const MemoryRegionOps exynos4210_rtc_ops
= {
546 .read
= exynos4210_rtc_read
,
547 .write
= exynos4210_rtc_write
,
548 .endianness
= DEVICE_NATIVE_ENDIAN
,
552 * RTC timer initialization
554 static void exynos4210_rtc_init(Object
*obj
)
556 Exynos4210RTCState
*s
= EXYNOS4210_RTC(obj
);
557 SysBusDevice
*dev
= SYS_BUS_DEVICE(obj
);
560 bh
= qemu_bh_new(exynos4210_rtc_tick
, s
);
561 s
->ptimer
= ptimer_init(bh
, PTIMER_POLICY_DEFAULT
);
562 ptimer_set_freq(s
->ptimer
, RTC_BASE_FREQ
);
563 exynos4210_rtc_update_freq(s
, 0);
565 bh
= qemu_bh_new(exynos4210_rtc_1Hz_tick
, s
);
566 s
->ptimer_1Hz
= ptimer_init(bh
, PTIMER_POLICY_DEFAULT
);
567 ptimer_set_freq(s
->ptimer_1Hz
, RTC_BASE_FREQ
);
569 sysbus_init_irq(dev
, &s
->alm_irq
);
570 sysbus_init_irq(dev
, &s
->tick_irq
);
572 memory_region_init_io(&s
->iomem
, obj
, &exynos4210_rtc_ops
, s
,
573 "exynos4210-rtc", EXYNOS4210_RTC_REG_MEM_SIZE
);
574 sysbus_init_mmio(dev
, &s
->iomem
);
577 static void exynos4210_rtc_class_init(ObjectClass
*klass
, void *data
)
579 DeviceClass
*dc
= DEVICE_CLASS(klass
);
581 dc
->reset
= exynos4210_rtc_reset
;
582 dc
->vmsd
= &vmstate_exynos4210_rtc_state
;
585 static const TypeInfo exynos4210_rtc_info
= {
586 .name
= TYPE_EXYNOS4210_RTC
,
587 .parent
= TYPE_SYS_BUS_DEVICE
,
588 .instance_size
= sizeof(Exynos4210RTCState
),
589 .instance_init
= exynos4210_rtc_init
,
590 .class_init
= exynos4210_rtc_class_init
,
593 static void exynos4210_rtc_register_types(void)
595 type_register_static(&exynos4210_rtc_info
);
598 type_init(exynos4210_rtc_register_types
)