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/module.h"
32 #include "hw/sysbus.h"
33 #include "migration/vmstate.h"
34 #include "qemu/timer.h"
36 #include "hw/ptimer.h"
40 #include "hw/arm/exynos4210.h"
41 #include "qom/object.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 typedef struct Exynos4210RTCState Exynos4210RTCState
;
89 DECLARE_INSTANCE_CHECKER(Exynos4210RTCState
, EXYNOS4210_RTC
,
92 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 */
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
199 * Must be called within a ptimer_transaction_begin/commit block for s->ptimer.
201 static void exynos4210_rtc_update_freq(Exynos4210RTCState
*s
,
207 /* set frequncy for time generator */
208 s
->freq
= RTC_BASE_FREQ
/ (1 << TICCKSEL(reg_value
));
210 if (freq
!= s
->freq
) {
211 ptimer_set_freq(s
->ptimer
, s
->freq
);
212 DPRINTF("freq=%dHz\n", s
->freq
);
216 /* month is between 0 and 11. */
217 static int get_days_in_month(int month
, int year
)
219 static const int days_tab
[12] = {
220 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
223 if ((unsigned)month
>= 12) {
228 if ((year
% 4) == 0 && ((year
% 100) != 0 || (year
% 400) == 0)) {
235 /* update 'tm' to the next second */
236 static void rtc_next_second(struct tm
*tm
)
241 if ((unsigned)tm
->tm_sec
>= 60) {
244 if ((unsigned)tm
->tm_min
>= 60) {
247 if ((unsigned)tm
->tm_hour
>= 24) {
251 if ((unsigned)tm
->tm_wday
>= 7) {
254 days_in_month
= get_days_in_month(tm
->tm_mon
,
257 if (tm
->tm_mday
< 1) {
259 } else if (tm
->tm_mday
> days_in_month
) {
262 if (tm
->tm_mon
>= 12) {
275 static void exynos4210_rtc_tick(void *opaque
)
277 Exynos4210RTCState
*s
= (Exynos4210RTCState
*)opaque
;
279 DPRINTF("TICK IRQ\n");
281 s
->reg_intp
|= INTP_TICK_ENABLE
;
283 qemu_irq_raise(s
->tick_irq
);
286 ptimer_set_count(s
->ptimer
, s
->reg_ticcnt
);
287 ptimer_run(s
->ptimer
, 1);
293 static void exynos4210_rtc_1Hz_tick(void *opaque
)
295 Exynos4210RTCState
*s
= (Exynos4210RTCState
*)opaque
;
297 rtc_next_second(&s
->current_tm
);
298 /* DPRINTF("1Hz tick\n"); */
301 if (s
->reg_rtcalm
& ALARM_INT_ENABLE
) {
302 check_alarm_raise(s
);
305 ptimer_set_count(s
->ptimer_1Hz
, RTC_BASE_FREQ
);
306 ptimer_run(s
->ptimer_1Hz
, 1);
312 static uint64_t exynos4210_rtc_read(void *opaque
, hwaddr offset
,
316 Exynos4210RTCState
*s
= (Exynos4210RTCState
*)opaque
;
323 value
= s
->reg_rtccon
;
326 value
= s
->reg_ticcnt
;
329 value
= s
->reg_rtcalm
;
332 value
= s
->reg_almsec
;
335 value
= s
->reg_almmin
;
338 value
= s
->reg_almhour
;
341 value
= s
->reg_almday
;
344 value
= s
->reg_almmon
;
347 value
= s
->reg_almyear
;
351 value
= (uint32_t)to_bcd((uint8_t)s
->current_tm
.tm_sec
);
354 value
= (uint32_t)to_bcd((uint8_t)s
->current_tm
.tm_min
);
357 value
= (uint32_t)to_bcd((uint8_t)s
->current_tm
.tm_hour
);
360 value
= (uint32_t)to_bcd((uint8_t)s
->current_tm
.tm_wday
);
363 value
= (uint32_t)to_bcd((uint8_t)s
->current_tm
.tm_mday
);
366 value
= (uint32_t)to_bcd((uint8_t)s
->current_tm
.tm_mon
+ 1);
369 value
= BCD3DIGITS(s
->current_tm
.tm_year
);
373 s
->reg_curticcnt
= ptimer_get_count(s
->ptimer
);
374 value
= s
->reg_curticcnt
;
378 qemu_log_mask(LOG_GUEST_ERROR
,
379 "exynos4210.rtc: bad read offset " TARGET_FMT_plx
,
389 static void exynos4210_rtc_write(void *opaque
, hwaddr offset
,
390 uint64_t value
, unsigned size
)
392 Exynos4210RTCState
*s
= (Exynos4210RTCState
*)opaque
;
396 if (value
& INTP_ALM_ENABLE
) {
397 qemu_irq_lower(s
->alm_irq
);
398 s
->reg_intp
&= (~INTP_ALM_ENABLE
);
400 if (value
& INTP_TICK_ENABLE
) {
401 qemu_irq_lower(s
->tick_irq
);
402 s
->reg_intp
&= (~INTP_TICK_ENABLE
);
406 ptimer_transaction_begin(s
->ptimer_1Hz
);
407 ptimer_transaction_begin(s
->ptimer
);
408 if (value
& RTC_ENABLE
) {
409 exynos4210_rtc_update_freq(s
, value
);
411 if ((value
& RTC_ENABLE
) > (s
->reg_rtccon
& RTC_ENABLE
)) {
413 ptimer_set_count(s
->ptimer_1Hz
, RTC_BASE_FREQ
);
414 ptimer_run(s
->ptimer_1Hz
, 1);
415 DPRINTF("run clock timer\n");
417 if ((value
& RTC_ENABLE
) < (s
->reg_rtccon
& RTC_ENABLE
)) {
419 ptimer_stop(s
->ptimer
);
421 ptimer_stop(s
->ptimer_1Hz
);
422 DPRINTF("stop all timers\n");
424 if (value
& RTC_ENABLE
) {
425 if ((value
& TICK_TIMER_ENABLE
) >
426 (s
->reg_rtccon
& TICK_TIMER_ENABLE
) &&
428 ptimer_set_count(s
->ptimer
, s
->reg_ticcnt
);
429 ptimer_run(s
->ptimer
, 1);
430 DPRINTF("run tick timer\n");
432 if ((value
& TICK_TIMER_ENABLE
) <
433 (s
->reg_rtccon
& TICK_TIMER_ENABLE
)) {
434 ptimer_stop(s
->ptimer
);
437 ptimer_transaction_commit(s
->ptimer_1Hz
);
438 ptimer_transaction_commit(s
->ptimer
);
439 s
->reg_rtccon
= value
;
442 if (value
> TICNT_THRESHOLD
) {
443 s
->reg_ticcnt
= value
;
445 qemu_log_mask(LOG_GUEST_ERROR
,
446 "exynos4210.rtc: bad TICNT value %u",
452 s
->reg_rtcalm
= value
;
455 s
->reg_almsec
= (value
& 0x7f);
458 s
->reg_almmin
= (value
& 0x7f);
461 s
->reg_almhour
= (value
& 0x3f);
464 s
->reg_almday
= (value
& 0x3f);
467 s
->reg_almmon
= (value
& 0x1f);
470 s
->reg_almyear
= (value
& 0x0fff);
474 if (s
->reg_rtccon
& RTC_ENABLE
) {
475 s
->current_tm
.tm_sec
= (int)from_bcd((uint8_t)value
);
479 if (s
->reg_rtccon
& RTC_ENABLE
) {
480 s
->current_tm
.tm_min
= (int)from_bcd((uint8_t)value
);
484 if (s
->reg_rtccon
& RTC_ENABLE
) {
485 s
->current_tm
.tm_hour
= (int)from_bcd((uint8_t)value
);
489 if (s
->reg_rtccon
& RTC_ENABLE
) {
490 s
->current_tm
.tm_wday
= (int)from_bcd((uint8_t)value
);
494 if (s
->reg_rtccon
& RTC_ENABLE
) {
495 s
->current_tm
.tm_mday
= (int)from_bcd((uint8_t)value
);
499 if (s
->reg_rtccon
& RTC_ENABLE
) {
500 s
->current_tm
.tm_mon
= (int)from_bcd((uint8_t)value
) - 1;
504 if (s
->reg_rtccon
& RTC_ENABLE
) {
506 s
->current_tm
.tm_year
= (int)from_bcd((uint8_t)value
) +
507 (int)from_bcd((uint8_t)((value
>> 8) & 0x0f)) * 100;
512 qemu_log_mask(LOG_GUEST_ERROR
,
513 "exynos4210.rtc: bad write offset " TARGET_FMT_plx
,
521 * Set default values to timer fields and registers
523 static void exynos4210_rtc_reset(DeviceState
*d
)
525 Exynos4210RTCState
*s
= EXYNOS4210_RTC(d
);
527 qemu_get_timedate(&s
->current_tm
, 0);
529 DPRINTF("Get time from host: %d-%d-%d %2d:%02d:%02d\n",
530 s
->current_tm
.tm_year
, s
->current_tm
.tm_mon
, s
->current_tm
.tm_mday
,
531 s
->current_tm
.tm_hour
, s
->current_tm
.tm_min
, s
->current_tm
.tm_sec
);
544 s
->reg_curticcnt
= 0;
546 ptimer_transaction_begin(s
->ptimer
);
547 exynos4210_rtc_update_freq(s
, s
->reg_rtccon
);
548 ptimer_stop(s
->ptimer
);
549 ptimer_transaction_commit(s
->ptimer
);
550 ptimer_transaction_begin(s
->ptimer_1Hz
);
551 ptimer_stop(s
->ptimer_1Hz
);
552 ptimer_transaction_commit(s
->ptimer_1Hz
);
555 static const MemoryRegionOps exynos4210_rtc_ops
= {
556 .read
= exynos4210_rtc_read
,
557 .write
= exynos4210_rtc_write
,
558 .endianness
= DEVICE_NATIVE_ENDIAN
,
562 * RTC timer initialization
564 static void exynos4210_rtc_init(Object
*obj
)
566 Exynos4210RTCState
*s
= EXYNOS4210_RTC(obj
);
567 SysBusDevice
*dev
= SYS_BUS_DEVICE(obj
);
569 s
->ptimer
= ptimer_init(exynos4210_rtc_tick
, s
, PTIMER_POLICY_DEFAULT
);
570 ptimer_transaction_begin(s
->ptimer
);
571 ptimer_set_freq(s
->ptimer
, RTC_BASE_FREQ
);
572 exynos4210_rtc_update_freq(s
, 0);
573 ptimer_transaction_commit(s
->ptimer
);
575 s
->ptimer_1Hz
= ptimer_init(exynos4210_rtc_1Hz_tick
,
576 s
, PTIMER_POLICY_DEFAULT
);
577 ptimer_transaction_begin(s
->ptimer_1Hz
);
578 ptimer_set_freq(s
->ptimer_1Hz
, RTC_BASE_FREQ
);
579 ptimer_transaction_commit(s
->ptimer_1Hz
);
581 sysbus_init_irq(dev
, &s
->alm_irq
);
582 sysbus_init_irq(dev
, &s
->tick_irq
);
584 memory_region_init_io(&s
->iomem
, obj
, &exynos4210_rtc_ops
, s
,
585 "exynos4210-rtc", EXYNOS4210_RTC_REG_MEM_SIZE
);
586 sysbus_init_mmio(dev
, &s
->iomem
);
589 static void exynos4210_rtc_class_init(ObjectClass
*klass
, void *data
)
591 DeviceClass
*dc
= DEVICE_CLASS(klass
);
593 dc
->reset
= exynos4210_rtc_reset
;
594 dc
->vmsd
= &vmstate_exynos4210_rtc_state
;
597 static const TypeInfo exynos4210_rtc_info
= {
598 .name
= TYPE_EXYNOS4210_RTC
,
599 .parent
= TYPE_SYS_BUS_DEVICE
,
600 .instance_size
= sizeof(Exynos4210RTCState
),
601 .instance_init
= exynos4210_rtc_init
,
602 .class_init
= exynos4210_rtc_class_init
,
605 static void exynos4210_rtc_register_types(void)
607 type_register_static(&exynos4210_rtc_info
);
610 type_init(exynos4210_rtc_register_types
)