Xiaohu.Huang | 6095045 | 2022-03-12 22:51:01 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2021-2022 Amlogic, Inc. All rights reserved. |
| 3 | * |
| 4 | * SPDX-License-Identifier: MIT |
| 5 | */ |
| 6 | |
| 7 | #include <stdio.h> |
| 8 | #include <string.h> |
| 9 | #include <unistd.h> |
| 10 | #include "common.h" |
| 11 | #include "rtc.h" |
| 12 | #include "register.h" |
| 13 | #include "FreeRTOS.h" |
| 14 | #include "mailbox-api.h" |
| 15 | #include "soc.h" |
| 16 | #include "string.h" |
| 17 | #include "interrupt.h" |
| 18 | #include "suspend.h" |
yiting.deng | 21d1aa4 | 2023-02-08 17:46:11 +0800 | [diff] [blame] | 19 | #include "rtc_register.h" |
yiting.deng | ac5b18d | 2023-04-25 11:13:26 +0800 | [diff] [blame] | 20 | #include "timers.h" |
| 21 | #include "interrupt_control_eclic.h" |
Xiaohu.Huang | 6095045 | 2022-03-12 22:51:01 +0800 | [diff] [blame] | 22 | |
| 23 | #undef TAG |
| 24 | #define TAG "AOCPU RTC" |
| 25 | |
yiting.deng | ac5b18d | 2023-04-25 11:13:26 +0800 | [diff] [blame] | 26 | static TimerHandle_t xAlarmTimer; |
| 27 | static int alarm_flags; |
| 28 | |
yiting.deng | 21d1aa4 | 2023-02-08 17:46:11 +0800 | [diff] [blame] | 29 | static uint32_t gray_to_binary(uint32_t gray) |
| 30 | { |
| 31 | uint32_t bcd = gray; |
| 32 | int size = sizeof(bcd) * 8; |
| 33 | int i; |
| 34 | |
| 35 | for (i = 0; (1 << i) < size; i++) |
| 36 | bcd ^= bcd >> (1 << i); |
| 37 | |
| 38 | return bcd; |
| 39 | } |
| 40 | |
Xiaohu.Huang | 6095045 | 2022-03-12 22:51:01 +0800 | [diff] [blame] | 41 | static void vRTCInterruptHandler(void) |
| 42 | { |
xiaohu.huang | 2beac51 | 2022-05-07 15:10:04 +0800 | [diff] [blame] | 43 | uint32_t buf[4] = { 0 }; |
yiting.deng | 4f79f4f | 2023-09-07 14:32:14 +0800 | [diff] [blame^] | 44 | uint32_t reg_val; |
Xiaohu.Huang | 6095045 | 2022-03-12 22:51:01 +0800 | [diff] [blame] | 45 | |
yiting.deng | ac5b18d | 2023-04-25 11:13:26 +0800 | [diff] [blame] | 46 | alarm_flags = 1; |
yiting.deng | 4f79f4f | 2023-09-07 14:32:14 +0800 | [diff] [blame^] | 47 | reg_val = REG32(RTC_DIG_INT_STATUS) & 0x1; |
| 48 | /* Clear alarm0 int status */ |
| 49 | if (reg_val) |
| 50 | REG32(RTC_DIG_INT_CLR) = 0x1; |
| 51 | |
Xiaohu.Huang | 6095045 | 2022-03-12 22:51:01 +0800 | [diff] [blame] | 52 | printf("[%s]: rtc alarm fired\n", TAG); |
| 53 | |
| 54 | buf[0] = RTC_WAKEUP; |
| 55 | STR_Wakeup_src_Queue_Send_FromISR(buf); |
| 56 | } |
| 57 | |
| 58 | static uint32_t get_reboot_mode(void) |
| 59 | { |
| 60 | uint32_t reg_val; |
| 61 | uint32_t reboot_mode; |
| 62 | |
| 63 | reg_val = REG32(SYSCTRL_SEC_STATUS_REG31); |
| 64 | reboot_mode = ((reg_val >> 12) & 0xf); |
| 65 | |
| 66 | return reboot_mode; |
| 67 | } |
| 68 | |
| 69 | static void reset_rtc(void) |
| 70 | { |
| 71 | uint32_t reg_val; |
| 72 | |
| 73 | printf("[%s]: reset rtc\n", TAG); |
| 74 | /* Reset RTC */ |
| 75 | reg_val = (1 << 0); |
| 76 | REG32(RESETCTRL_RESET4) = reg_val; |
| 77 | /* Mask RTC reset to prevent RTC being reset in the next reboot */ |
| 78 | reg_val = REG32(RESETCTRL_RESET4_MASK); |
| 79 | reg_val |= (1 << 0); |
| 80 | REG32(RESETCTRL_RESET4_MASK) = reg_val; |
| 81 | } |
| 82 | |
| 83 | static int get_rtc(uint32_t *val) |
| 84 | { |
| 85 | if (!REG32(VRTC_STICKY_REG)) |
| 86 | return -1; |
Xiaohu.Huang | 6095045 | 2022-03-12 22:51:01 +0800 | [diff] [blame] | 87 | |
xiaohu.huang | 2beac51 | 2022-05-07 15:10:04 +0800 | [diff] [blame] | 88 | *(val) = REG32(VRTC_STICKY_REG); |
Xiaohu.Huang | 6095045 | 2022-03-12 22:51:01 +0800 | [diff] [blame] | 89 | return 0; |
| 90 | } |
| 91 | |
| 92 | static void set_rtc(uint32_t val) |
| 93 | { |
| 94 | REG32(VRTC_STICKY_REG) = val; |
| 95 | } |
| 96 | |
| 97 | void store_rtc(void) |
| 98 | { |
| 99 | uint32_t reg_val; |
| 100 | |
yiting.deng | 21d1aa4 | 2023-02-08 17:46:11 +0800 | [diff] [blame] | 101 | reg_val = REG32(RTC_DIG_REAL_TIME); |
| 102 | #ifdef CONFIG_RTC_STORAGE_FORMAT_GRAY |
| 103 | reg_val = gray_to_binary(reg_val); |
| 104 | #endif |
Xiaohu.Huang | 6095045 | 2022-03-12 22:51:01 +0800 | [diff] [blame] | 105 | REG32(VRTC_STICKY_REG) = reg_val; |
| 106 | } |
| 107 | |
| 108 | void *MboxSetRTC(void *msg) |
| 109 | { |
| 110 | unsigned int val = *(uint32_t *)msg; |
xiaohu.huang | 2beac51 | 2022-05-07 15:10:04 +0800 | [diff] [blame] | 111 | |
| 112 | printf("[%s]: %s val=0x%x\n", TAG, __func__, val); |
Xiaohu.Huang | 6095045 | 2022-03-12 22:51:01 +0800 | [diff] [blame] | 113 | set_rtc(val); |
| 114 | |
| 115 | return NULL; |
| 116 | } |
| 117 | |
| 118 | void *MboxGetRTC(void *msg) |
| 119 | { |
| 120 | uint32_t val = 0; |
| 121 | |
| 122 | get_rtc(&val); |
| 123 | memset(msg, 0, MBOX_BUF_LEN); |
| 124 | *(uint32_t *)msg = val; |
| 125 | |
xiaohu.huang | 2beac51 | 2022-05-07 15:10:04 +0800 | [diff] [blame] | 126 | printf("[%s]: %s val=0x%x\n", TAG, __func__, val); |
Xiaohu.Huang | 6095045 | 2022-03-12 22:51:01 +0800 | [diff] [blame] | 127 | |
| 128 | return NULL; |
| 129 | } |
| 130 | |
yiting.deng | 6ec1d64 | 2023-03-19 10:10:35 +0800 | [diff] [blame] | 131 | void rtc_enable_irq(void) |
| 132 | { |
yiting.deng | bfc3f45 | 2023-05-18 16:22:40 +0800 | [diff] [blame] | 133 | int ret, val; |
yiting.deng | ac5b18d | 2023-04-25 11:13:26 +0800 | [diff] [blame] | 134 | u32 alarm, time; |
yiting.deng | 4f79f4f | 2023-09-07 14:32:14 +0800 | [diff] [blame^] | 135 | int irq_num; |
| 136 | uint32_t reg_val; |
yiting.deng | 6ec1d64 | 2023-03-19 10:10:35 +0800 | [diff] [blame] | 137 | |
yiting.deng | 4f79f4f | 2023-09-07 14:32:14 +0800 | [diff] [blame^] | 138 | irq_num = RTC_IRQ % 32; |
| 139 | reg_val = REG32(IRQCTRL_IRQ_LATCH4) >> irq_num & 0x1; |
| 140 | if (reg_val) |
| 141 | REG32(IRQCTRL_IRQ_LATCH_CLR4) |= (0x1 << irq_num); |
yiting.deng | ac5b18d | 2023-04-25 11:13:26 +0800 | [diff] [blame] | 142 | alarm = REG32(RTC_DIG_ALARM0_REG); |
yiting.deng | bfc3f45 | 2023-05-18 16:22:40 +0800 | [diff] [blame] | 143 | time = REG32(RTC_DIG_REAL_TIME); |
| 144 | #ifdef CONFIG_RTC_STORAGE_FORMAT_GRAY |
| 145 | alarm = gray_to_binary(alarm); |
| 146 | time = gray_to_binary(time); |
| 147 | #endif |
| 148 | val = alarm - time; |
| 149 | if (val > 0) { |
yiting.deng | ac5b18d | 2023-04-25 11:13:26 +0800 | [diff] [blame] | 150 | ret = RegisterIrq(RTC_IRQ, 6, vRTCInterruptHandler); |
| 151 | if (ret) |
| 152 | printf("RTC_irq RegisterIrq error, ret = %d\n", ret); |
| 153 | EnableIrq(RTC_IRQ); |
yiting.deng | bfc3f45 | 2023-05-18 16:22:40 +0800 | [diff] [blame] | 154 | printf("[%s]: alarm val=%d S\n", TAG, val); |
| 155 | if (xAlarmTimer != NULL) { |
| 156 | alarm = (val + 1) * 1000; |
yiting.deng | ac5b18d | 2023-04-25 11:13:26 +0800 | [diff] [blame] | 157 | xTimerChangePeriod(xAlarmTimer, pdMS_TO_TICKS(alarm), 0); |
| 158 | } |
| 159 | } |
yiting.deng | 6ec1d64 | 2023-03-19 10:10:35 +0800 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | void rtc_disable_irq(void) |
| 163 | { |
yiting.deng | bfc3f45 | 2023-05-18 16:22:40 +0800 | [diff] [blame] | 164 | int ret, val; |
yiting.deng | 6ec1d64 | 2023-03-19 10:10:35 +0800 | [diff] [blame] | 165 | |
yiting.deng | 4c21b17 | 2023-08-29 15:44:23 +0800 | [diff] [blame] | 166 | val = GetIrqInner(RTC_IRQ); |
yiting.deng | bfc3f45 | 2023-05-18 16:22:40 +0800 | [diff] [blame] | 167 | if (val > 0) { |
yiting.deng | ac5b18d | 2023-04-25 11:13:26 +0800 | [diff] [blame] | 168 | DisableIrq(RTC_IRQ); |
| 169 | ret = UnRegisterIrq(RTC_IRQ); |
| 170 | if (ret) |
| 171 | printf("RTC_irq UnRegisterIrq error, ret = %d\n", ret); |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | static void rtc_alarm_timer_handler(TimerHandle_t xAlarmTimer) |
| 176 | { |
| 177 | static int rtc_irq, status; |
yiting.deng | 4f79f4f | 2023-09-07 14:32:14 +0800 | [diff] [blame^] | 178 | int irq_num; |
| 179 | uint32_t reg_val; |
yiting.deng | ac5b18d | 2023-04-25 11:13:26 +0800 | [diff] [blame] | 180 | |
| 181 | status = REG32(RTC_DIG_INT_STATUS) & 0x1; |
| 182 | if (status && !alarm_flags) { |
yiting.deng | 4f79f4f | 2023-09-07 14:32:14 +0800 | [diff] [blame^] | 183 | printf("warning: rtc interrupt lost!trigger rtc interrupt manually!\n"); |
| 184 | irq_num = RTC_IRQ % 32; |
| 185 | reg_val = REG32(IRQCTRL_IRQ_LATCH4) >> irq_num & 0x1; |
| 186 | printf("[%s]: timer read rtc irqctrl status: 0x%x !!\n", TAG, reg_val); |
yiting.deng | ac5b18d | 2023-04-25 11:13:26 +0800 | [diff] [blame] | 187 | rtc_irq = GetIrqInner(RTC_IRQ); |
| 188 | if (rtc_irq) |
| 189 | eclic_set_pending(rtc_irq); |
| 190 | } |
| 191 | alarm_flags = 0; |
| 192 | } |
| 193 | |
| 194 | void alarm_clr(void) |
| 195 | { |
yiting.deng | 4f79f4f | 2023-09-07 14:32:14 +0800 | [diff] [blame^] | 196 | int irq_num; |
| 197 | uint32_t reg_val; |
| 198 | |
yiting.deng | ac5b18d | 2023-04-25 11:13:26 +0800 | [diff] [blame] | 199 | if (xAlarmTimer != NULL) |
| 200 | xTimerStop(xAlarmTimer, 0); |
yiting.deng | 4f79f4f | 2023-09-07 14:32:14 +0800 | [diff] [blame^] | 201 | |
| 202 | irq_num = RTC_IRQ % 32; |
| 203 | reg_val = REG32(IRQCTRL_IRQ_LATCH4) >> irq_num & 0x1; |
| 204 | printf("[%s]: rtc irqctrl status: 0x%x !!\n", TAG, reg_val); |
| 205 | if (reg_val) |
| 206 | REG32(IRQCTRL_IRQ_LATCH_CLR4) |= (0x1 << irq_num); |
| 207 | reg_val = REG32(IRQCTRL_IRQ_LATCH4) >> irq_num & 0x1; |
yiting.deng | ac5b18d | 2023-04-25 11:13:26 +0800 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | static void rtc_alarm_timer_init(void) |
| 211 | { |
| 212 | xAlarmTimer = xTimerCreate("RtcAlarmTimer", pdMS_TO_TICKS(1000), |
| 213 | pdFALSE, NULL, rtc_alarm_timer_handler); |
yiting.deng | 6ec1d64 | 2023-03-19 10:10:35 +0800 | [diff] [blame] | 214 | } |
| 215 | |
Xiaohu.Huang | 6095045 | 2022-03-12 22:51:01 +0800 | [diff] [blame] | 216 | void rtc_init(void) |
| 217 | { |
| 218 | int ret; |
| 219 | uint32_t reboot_mode; |
| 220 | |
| 221 | printf("[%s]: init rtc\n", TAG); |
Xiaohu.Huang | 6095045 | 2022-03-12 22:51:01 +0800 | [diff] [blame] | 222 | |
xiaohu.huang | 2beac51 | 2022-05-07 15:10:04 +0800 | [diff] [blame] | 223 | ret = xInstallRemoteMessageCallbackFeedBack(AOREE_CHANNEL, MBX_CMD_SET_RTC, MboxSetRTC, 0); |
Xiaohu.Huang | 6095045 | 2022-03-12 22:51:01 +0800 | [diff] [blame] | 224 | if (ret == MBOX_CALL_MAX) |
| 225 | printf("[%s]: mbox cmd 0x%x register fail\n", TAG, MBX_CMD_SET_RTC); |
| 226 | |
xiaohu.huang | 2beac51 | 2022-05-07 15:10:04 +0800 | [diff] [blame] | 227 | ret = xInstallRemoteMessageCallbackFeedBack(AOREE_CHANNEL, MBX_CMD_GET_RTC, MboxGetRTC, 1); |
Xiaohu.Huang | 6095045 | 2022-03-12 22:51:01 +0800 | [diff] [blame] | 228 | if (ret == MBOX_CALL_MAX) |
| 229 | printf("[%s]: mbox cmd 0x%x register fail\n", TAG, MBX_CMD_GET_RTC); |
| 230 | |
| 231 | reboot_mode = get_reboot_mode(); |
| 232 | if (reboot_mode == COLD_REBOOT) |
| 233 | reset_rtc(); |
yiting.deng | ac5b18d | 2023-04-25 11:13:26 +0800 | [diff] [blame] | 234 | |
| 235 | rtc_alarm_timer_init(); |
Xiaohu.Huang | 6095045 | 2022-03-12 22:51:01 +0800 | [diff] [blame] | 236 | } |