diff options
Diffstat (limited to 'firmware/drivers/rtc/rtc_x1000.c')
-rw-r--r-- | firmware/drivers/rtc/rtc_x1000.c | 104 |
1 files changed, 104 insertions, 0 deletions
diff --git a/firmware/drivers/rtc/rtc_x1000.c b/firmware/drivers/rtc/rtc_x1000.c new file mode 100644 index 0000000000..1d3a5484e9 --- /dev/null +++ b/firmware/drivers/rtc/rtc_x1000.c | |||
@@ -0,0 +1,104 @@ | |||
1 | /*************************************************************************** | ||
2 | * __________ __ ___. | ||
3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ | ||
4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / | ||
5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < | ||
6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ | ||
7 | * \/ \/ \/ \/ \/ | ||
8 | * $Id$ | ||
9 | * | ||
10 | * Copyright (C) 2021 Aidan MacDonald | ||
11 | * | ||
12 | * Based mainly on rtc_jz4760.c, | ||
13 | * Copyright (C) 2016 by Roman Stolyarov | ||
14 | * | ||
15 | * This program is free software; you can redistribute it and/or | ||
16 | * modify it under the terms of the GNU General Public License | ||
17 | * as published by the Free Software Foundation; either version 2 | ||
18 | * of the License, or (at your option) any later version. | ||
19 | * | ||
20 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY | ||
21 | * KIND, either express or implied. | ||
22 | * | ||
23 | ****************************************************************************/ | ||
24 | |||
25 | #include "rtc.h" | ||
26 | #include "x1000/rtc.h" | ||
27 | #include <stdint.h> | ||
28 | |||
29 | /* 4 byte magic number 'RTCV' */ | ||
30 | #define RTCV 0x52544356 | ||
31 | |||
32 | /* expected RTC clock frequency */ | ||
33 | #define NC1HZ_EXPECTED (32768 - 1) | ||
34 | |||
35 | static void rtc_write_reg(uint32_t addr, uint32_t value) | ||
36 | { | ||
37 | while(jz_readf(RTC_CR, WRDY) == 0); | ||
38 | REG_RTC_WENR = 0xa55a; | ||
39 | while(jz_readf(RTC_WENR, WEN) == 0); | ||
40 | while(jz_readf(RTC_CR, WRDY) == 0); | ||
41 | (*(volatile uint32_t*)addr) = value; | ||
42 | while(jz_readf(RTC_CR, WRDY) == 0); | ||
43 | } | ||
44 | |||
45 | void rtc_init(void) | ||
46 | { | ||
47 | /* Check if we totally lost power and need to reset the RTC */ | ||
48 | if(REG_RTC_HSPR != RTCV || jz_readf(RTC_GR, NC1HZ) != NC1HZ_EXPECTED) { | ||
49 | rtc_write_reg(JA_RTC_GR, NC1HZ_EXPECTED); | ||
50 | rtc_write_reg(JA_RTC_HWFCR, 3200); | ||
51 | rtc_write_reg(JA_RTC_HRCR, 2048); | ||
52 | rtc_write_reg(JA_RTC_SR, 1546300800); /* 01/01/2019 */ | ||
53 | rtc_write_reg(JA_RTC_CR, 1); | ||
54 | rtc_write_reg(JA_RTC_HSPR, RTCV); | ||
55 | } | ||
56 | |||
57 | rtc_write_reg(JA_RTC_HWRSR, 0); | ||
58 | } | ||
59 | |||
60 | int rtc_read_datetime(struct tm* tm) | ||
61 | { | ||
62 | time_t time = REG_RTC_SR; | ||
63 | gmtime_r(&time, tm); | ||
64 | return 1; | ||
65 | } | ||
66 | |||
67 | int rtc_write_datetime(const struct tm* tm) | ||
68 | { | ||
69 | time_t time = mktime((struct tm*)tm); | ||
70 | rtc_write_reg(JA_RTC_SR, time); | ||
71 | return 1; | ||
72 | } | ||
73 | |||
74 | #ifdef HAVE_RTC_ALARM | ||
75 | /* TODO: implement the RTC alarm */ | ||
76 | |||
77 | void rtc_set_alarm(int h, int m) | ||
78 | { | ||
79 | (void)h; | ||
80 | (void)m; | ||
81 | } | ||
82 | |||
83 | void rtc_get_alarm(int* h, int* m) | ||
84 | { | ||
85 | (void)h; | ||
86 | (void)m; | ||
87 | } | ||
88 | |||
89 | void rtc_enable_alarm(bool enable) | ||
90 | { | ||
91 | (void)enable; | ||
92 | } | ||
93 | |||
94 | bool rtc_check_alarm_started(bool release_alarm) | ||
95 | { | ||
96 | (void)release_alarm; | ||
97 | return false; | ||
98 | } | ||
99 | |||
100 | bool rtc_check_alarm_flag(void) | ||
101 | { | ||
102 | return false; | ||
103 | } | ||
104 | #endif | ||