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 "hw/sysbus.h"
30 #include "qemu/timer.h"
31 #include "qemu-common.h"
32 #include "hw/ptimer.h"
35 #include "sysemu/sysemu.h"
37 #include "hw/arm/exynos4210.h"
42 #define DPRINTF(fmt, ...) \
43 do { fprintf(stdout, "RTC: [%24s:%5d] " fmt, __func__, __LINE__, \
44 ## __VA_ARGS__); } while (0)
46 #define DPRINTF(fmt, ...) do {} while (0)
49 #define EXYNOS4210_RTC_REG_MEM_SIZE 0x0100
57 #define ALMHOUR 0x005C
60 #define ALMYEAR 0x0068
63 #define BCDHOUR 0x0078
65 #define BCDDAYWEEK 0x0080
67 #define BCDYEAR 0x0088
68 #define CURTICNT 0x0090
70 #define TICK_TIMER_ENABLE 0x0100
71 #define TICNT_THRESHOLD 2
74 #define RTC_ENABLE 0x0001
76 #define INTP_TICK_ENABLE 0x0001
77 #define INTP_ALM_ENABLE 0x0002
79 #define ALARM_INT_ENABLE 0x0040
81 #define RTC_BASE_FREQ 32768
83 #define TYPE_EXYNOS4210_RTC "exynos4210.rtc"
84 #define EXYNOS4210_RTC(obj) \
85 OBJECT_CHECK(Exynos4210RTCState, (obj), TYPE_EXYNOS4210_RTC)
87 typedef struct Exynos4210RTCState
{
88 SysBusDevice parent_obj
;
102 uint32_t reg_almyear
;
103 uint32_t reg_curticcnt
;
105 ptimer_state
*ptimer
; /* tick timer */
106 ptimer_state
*ptimer_1Hz
; /* clock timer */
109 qemu_irq tick_irq
; /* Time Tick Generator irq */
110 qemu_irq alm_irq
; /* alarm irq */
112 struct tm current_tm
; /* current time */
113 } Exynos4210RTCState
;
115 #define TICCKSEL(value) ((value & (0x0F << 4)) >> 4)
118 static const VMStateDescription vmstate_exynos4210_rtc_state
= {
119 .name
= "exynos4210.rtc",
121 .minimum_version_id
= 1,
122 .fields
= (VMStateField
[]) {
123 VMSTATE_UINT32(reg_intp
, Exynos4210RTCState
),
124 VMSTATE_UINT32(reg_rtccon
, Exynos4210RTCState
),
125 VMSTATE_UINT32(reg_ticcnt
, Exynos4210RTCState
),
126 VMSTATE_UINT32(reg_rtcalm
, Exynos4210RTCState
),
127 VMSTATE_UINT32(reg_almsec
, Exynos4210RTCState
),
128 VMSTATE_UINT32(reg_almmin
, Exynos4210RTCState
),
129 VMSTATE_UINT32(reg_almhour
, Exynos4210RTCState
),
130 VMSTATE_UINT32(reg_almday
, Exynos4210RTCState
),
131 VMSTATE_UINT32(reg_almmon
, Exynos4210RTCState
),
132 VMSTATE_UINT32(reg_almyear
, Exynos4210RTCState
),
133 VMSTATE_UINT32(reg_curticcnt
, Exynos4210RTCState
),
134 VMSTATE_PTIMER(ptimer
, Exynos4210RTCState
),
135 VMSTATE_PTIMER(ptimer_1Hz
, Exynos4210RTCState
),
136 VMSTATE_UINT32(freq
, Exynos4210RTCState
),
137 VMSTATE_INT32(current_tm
.tm_sec
, Exynos4210RTCState
),
138 VMSTATE_INT32(current_tm
.tm_min
, Exynos4210RTCState
),
139 VMSTATE_INT32(current_tm
.tm_hour
, Exynos4210RTCState
),
140 VMSTATE_INT32(current_tm
.tm_wday
, Exynos4210RTCState
),
141 VMSTATE_INT32(current_tm
.tm_mday
, Exynos4210RTCState
),
142 VMSTATE_INT32(current_tm
.tm_mon
, Exynos4210RTCState
),
143 VMSTATE_INT32(current_tm
.tm_year
, Exynos4210RTCState
),
144 VMSTATE_END_OF_LIST()
148 #define BCD3DIGITS(x) \
149 ((uint32_t)to_bcd((uint8_t)(x % 100)) + \
150 ((uint32_t)to_bcd((uint8_t)((x % 1000) / 100)) << 8))
152 static void check_alarm_raise(Exynos4210RTCState
*s
)
154 unsigned int alarm_raise
= 0;
155 struct tm stm
= s
->current_tm
;
157 if ((s
->reg_rtcalm
& 0x01) &&
158 (to_bcd((uint8_t)stm
.tm_sec
) == (uint8_t)s
->reg_almsec
)) {
161 if ((s
->reg_rtcalm
& 0x02) &&
162 (to_bcd((uint8_t)stm
.tm_min
) == (uint8_t)s
->reg_almmin
)) {
165 if ((s
->reg_rtcalm
& 0x04) &&
166 (to_bcd((uint8_t)stm
.tm_hour
) == (uint8_t)s
->reg_almhour
)) {
169 if ((s
->reg_rtcalm
& 0x08) &&
170 (to_bcd((uint8_t)stm
.tm_mday
) == (uint8_t)s
->reg_almday
)) {
173 if ((s
->reg_rtcalm
& 0x10) &&
174 (to_bcd((uint8_t)stm
.tm_mon
) == (uint8_t)s
->reg_almmon
)) {
177 if ((s
->reg_rtcalm
& 0x20) &&
178 (BCD3DIGITS(stm
.tm_year
) == s
->reg_almyear
)) {
183 DPRINTF("ALARM IRQ\n");
185 s
->reg_intp
|= INTP_ALM_ENABLE
;
186 qemu_irq_raise(s
->alm_irq
);
191 * RTC update frequency
193 * reg_value - current RTCCON register or his new value
195 static void exynos4210_rtc_update_freq(Exynos4210RTCState
*s
,
201 /* set frequncy for time generator */
202 s
->freq
= RTC_BASE_FREQ
/ (1 << TICCKSEL(reg_value
));
204 if (freq
!= s
->freq
) {
205 ptimer_set_freq(s
->ptimer
, s
->freq
);
206 DPRINTF("freq=%dHz\n", s
->freq
);
210 /* month is between 0 and 11. */
211 static int get_days_in_month(int month
, int year
)
213 static const int days_tab
[12] = {
214 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
217 if ((unsigned)month
>= 12) {
222 if ((year
% 4) == 0 && ((year
% 100) != 0 || (year
% 400) == 0)) {
229 /* update 'tm' to the next second */
230 static void rtc_next_second(struct tm
*tm
)
235 if ((unsigned)tm
->tm_sec
>= 60) {
238 if ((unsigned)tm
->tm_min
>= 60) {
241 if ((unsigned)tm
->tm_hour
>= 24) {
245 if ((unsigned)tm
->tm_wday
>= 7) {
248 days_in_month
= get_days_in_month(tm
->tm_mon
,
251 if (tm
->tm_mday
< 1) {
253 } else if (tm
->tm_mday
> days_in_month
) {
256 if (tm
->tm_mon
>= 12) {
269 static void exynos4210_rtc_tick(void *opaque
)
271 Exynos4210RTCState
*s
= (Exynos4210RTCState
*)opaque
;
273 DPRINTF("TICK IRQ\n");
275 s
->reg_intp
|= INTP_TICK_ENABLE
;
277 qemu_irq_raise(s
->tick_irq
);
280 ptimer_set_count(s
->ptimer
, s
->reg_ticcnt
);
281 ptimer_run(s
->ptimer
, 1);
287 static void exynos4210_rtc_1Hz_tick(void *opaque
)
289 Exynos4210RTCState
*s
= (Exynos4210RTCState
*)opaque
;
291 rtc_next_second(&s
->current_tm
);
292 /* DPRINTF("1Hz tick\n"); */
295 if (s
->reg_rtcalm
& ALARM_INT_ENABLE
) {
296 check_alarm_raise(s
);
299 ptimer_set_count(s
->ptimer_1Hz
, RTC_BASE_FREQ
);
300 ptimer_run(s
->ptimer_1Hz
, 1);
306 static uint64_t exynos4210_rtc_read(void *opaque
, hwaddr offset
,
310 Exynos4210RTCState
*s
= (Exynos4210RTCState
*)opaque
;
317 value
= s
->reg_rtccon
;
320 value
= s
->reg_ticcnt
;
323 value
= s
->reg_rtcalm
;
326 value
= s
->reg_almsec
;
329 value
= s
->reg_almmin
;
332 value
= s
->reg_almhour
;
335 value
= s
->reg_almday
;
338 value
= s
->reg_almmon
;
341 value
= s
->reg_almyear
;
345 value
= (uint32_t)to_bcd((uint8_t)s
->current_tm
.tm_sec
);
348 value
= (uint32_t)to_bcd((uint8_t)s
->current_tm
.tm_min
);
351 value
= (uint32_t)to_bcd((uint8_t)s
->current_tm
.tm_hour
);
354 value
= (uint32_t)to_bcd((uint8_t)s
->current_tm
.tm_wday
);
357 value
= (uint32_t)to_bcd((uint8_t)s
->current_tm
.tm_mday
);
360 value
= (uint32_t)to_bcd((uint8_t)s
->current_tm
.tm_mon
+ 1);
363 value
= BCD3DIGITS(s
->current_tm
.tm_year
);
367 s
->reg_curticcnt
= ptimer_get_count(s
->ptimer
);
368 value
= s
->reg_curticcnt
;
373 "[exynos4210.rtc: bad read offset " TARGET_FMT_plx
"]\n",
383 static void exynos4210_rtc_write(void *opaque
, hwaddr offset
,
384 uint64_t value
, unsigned size
)
386 Exynos4210RTCState
*s
= (Exynos4210RTCState
*)opaque
;
390 if (value
& INTP_ALM_ENABLE
) {
391 qemu_irq_lower(s
->alm_irq
);
392 s
->reg_intp
&= (~INTP_ALM_ENABLE
);
394 if (value
& INTP_TICK_ENABLE
) {
395 qemu_irq_lower(s
->tick_irq
);
396 s
->reg_intp
&= (~INTP_TICK_ENABLE
);
400 if (value
& RTC_ENABLE
) {
401 exynos4210_rtc_update_freq(s
, value
);
403 if ((value
& RTC_ENABLE
) > (s
->reg_rtccon
& RTC_ENABLE
)) {
405 ptimer_set_count(s
->ptimer_1Hz
, RTC_BASE_FREQ
);
406 ptimer_run(s
->ptimer_1Hz
, 1);
407 DPRINTF("run clock timer\n");
409 if ((value
& RTC_ENABLE
) < (s
->reg_rtccon
& RTC_ENABLE
)) {
411 ptimer_stop(s
->ptimer
);
413 ptimer_stop(s
->ptimer_1Hz
);
414 DPRINTF("stop all timers\n");
416 if (value
& RTC_ENABLE
) {
417 if ((value
& TICK_TIMER_ENABLE
) >
418 (s
->reg_rtccon
& TICK_TIMER_ENABLE
) &&
420 ptimer_set_count(s
->ptimer
, s
->reg_ticcnt
);
421 ptimer_run(s
->ptimer
, 1);
422 DPRINTF("run tick timer\n");
424 if ((value
& TICK_TIMER_ENABLE
) <
425 (s
->reg_rtccon
& TICK_TIMER_ENABLE
)) {
426 ptimer_stop(s
->ptimer
);
429 s
->reg_rtccon
= value
;
432 if (value
> TICNT_THRESHOLD
) {
433 s
->reg_ticcnt
= value
;
436 "[exynos4210.rtc: bad TICNT value %u ]\n",
442 s
->reg_rtcalm
= value
;
445 s
->reg_almsec
= (value
& 0x7f);
448 s
->reg_almmin
= (value
& 0x7f);
451 s
->reg_almhour
= (value
& 0x3f);
454 s
->reg_almday
= (value
& 0x3f);
457 s
->reg_almmon
= (value
& 0x1f);
460 s
->reg_almyear
= (value
& 0x0fff);
464 if (s
->reg_rtccon
& RTC_ENABLE
) {
465 s
->current_tm
.tm_sec
= (int)from_bcd((uint8_t)value
);
469 if (s
->reg_rtccon
& RTC_ENABLE
) {
470 s
->current_tm
.tm_min
= (int)from_bcd((uint8_t)value
);
474 if (s
->reg_rtccon
& RTC_ENABLE
) {
475 s
->current_tm
.tm_hour
= (int)from_bcd((uint8_t)value
);
479 if (s
->reg_rtccon
& RTC_ENABLE
) {
480 s
->current_tm
.tm_wday
= (int)from_bcd((uint8_t)value
);
484 if (s
->reg_rtccon
& RTC_ENABLE
) {
485 s
->current_tm
.tm_mday
= (int)from_bcd((uint8_t)value
);
489 if (s
->reg_rtccon
& RTC_ENABLE
) {
490 s
->current_tm
.tm_mon
= (int)from_bcd((uint8_t)value
) - 1;
494 if (s
->reg_rtccon
& RTC_ENABLE
) {
496 s
->current_tm
.tm_year
= (int)from_bcd((uint8_t)value
) +
497 (int)from_bcd((uint8_t)((value
>> 8) & 0x0f)) * 100;
503 "[exynos4210.rtc: bad write offset " TARGET_FMT_plx
"]\n",
511 * Set default values to timer fields and registers
513 static void exynos4210_rtc_reset(DeviceState
*d
)
515 Exynos4210RTCState
*s
= EXYNOS4210_RTC(d
);
517 qemu_get_timedate(&s
->current_tm
, 0);
519 DPRINTF("Get time from host: %d-%d-%d %2d:%02d:%02d\n",
520 s
->current_tm
.tm_year
, s
->current_tm
.tm_mon
, s
->current_tm
.tm_mday
,
521 s
->current_tm
.tm_hour
, s
->current_tm
.tm_min
, s
->current_tm
.tm_sec
);
534 s
->reg_curticcnt
= 0;
536 exynos4210_rtc_update_freq(s
, s
->reg_rtccon
);
537 ptimer_stop(s
->ptimer
);
538 ptimer_stop(s
->ptimer_1Hz
);
541 static const MemoryRegionOps exynos4210_rtc_ops
= {
542 .read
= exynos4210_rtc_read
,
543 .write
= exynos4210_rtc_write
,
544 .endianness
= DEVICE_NATIVE_ENDIAN
,
548 * RTC timer initialization
550 static int exynos4210_rtc_init(SysBusDevice
*dev
)
552 Exynos4210RTCState
*s
= EXYNOS4210_RTC(dev
);
555 bh
= qemu_bh_new(exynos4210_rtc_tick
, s
);
556 s
->ptimer
= ptimer_init(bh
);
557 ptimer_set_freq(s
->ptimer
, RTC_BASE_FREQ
);
558 exynos4210_rtc_update_freq(s
, 0);
560 bh
= qemu_bh_new(exynos4210_rtc_1Hz_tick
, s
);
561 s
->ptimer_1Hz
= ptimer_init(bh
);
562 ptimer_set_freq(s
->ptimer_1Hz
, RTC_BASE_FREQ
);
564 sysbus_init_irq(dev
, &s
->alm_irq
);
565 sysbus_init_irq(dev
, &s
->tick_irq
);
567 memory_region_init_io(&s
->iomem
, OBJECT(s
), &exynos4210_rtc_ops
, s
,
568 "exynos4210-rtc", EXYNOS4210_RTC_REG_MEM_SIZE
);
569 sysbus_init_mmio(dev
, &s
->iomem
);
574 static void exynos4210_rtc_class_init(ObjectClass
*klass
, void *data
)
576 DeviceClass
*dc
= DEVICE_CLASS(klass
);
577 SysBusDeviceClass
*k
= SYS_BUS_DEVICE_CLASS(klass
);
579 k
->init
= exynos4210_rtc_init
;
580 dc
->reset
= exynos4210_rtc_reset
;
581 dc
->vmsd
= &vmstate_exynos4210_rtc_state
;
584 static const TypeInfo exynos4210_rtc_info
= {
585 .name
= TYPE_EXYNOS4210_RTC
,
586 .parent
= TYPE_SYS_BUS_DEVICE
,
587 .instance_size
= sizeof(Exynos4210RTCState
),
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
)