1 /* 2 * Copyright (c) 2015, ARM Limited and Contributors. All rights reserved. 3 * 4 * SPDX-License-Identifier: BSD-3-Clause 5 */ 6 7 #include <assert.h> 8 9 #include <common/debug.h> 10 #include <drivers/delay_timer.h> 11 12 #include <mt8173_def.h> 13 #include <pmic_wrap_init.h> 14 #include <rtc.h> 15 16 /* RTC busy status polling interval and retry count */ 17 enum { 18 RTC_WRTGR_POLLING_DELAY_MS = 10, 19 RTC_WRTGR_POLLING_CNT = 100 20 }; 21 22 static uint16_t RTC_Read(uint32_t addr) 23 { 24 uint32_t rdata = 0; 25 26 pwrap_read((uint32_t)addr, &rdata); 27 return (uint16_t)rdata; 28 } 29 30 static void RTC_Write(uint32_t addr, uint16_t data) 31 { 32 pwrap_write((uint32_t)addr, (uint32_t)data); 33 } 34 35 static inline int32_t rtc_busy_wait(void) 36 { 37 uint64_t retry = RTC_WRTGR_POLLING_CNT; 38 39 do { 40 mdelay(RTC_WRTGR_POLLING_DELAY_MS); 41 if (!(RTC_Read(RTC_BBPU) & RTC_BBPU_CBUSY)) 42 return 1; 43 retry--; 44 } while (retry); 45 46 ERROR("[RTC] rtc cbusy time out!\n"); 47 return 0; 48 } 49 50 static int32_t Write_trigger(void) 51 { 52 RTC_Write(RTC_WRTGR, 1); 53 return rtc_busy_wait(); 54 } 55 56 static int32_t Writeif_unlock(void) 57 { 58 RTC_Write(RTC_PROT, RTC_PROT_UNLOCK1); 59 if (!Write_trigger()) 60 return 0; 61 RTC_Write(RTC_PROT, RTC_PROT_UNLOCK2); 62 if (!Write_trigger()) 63 return 0; 64 65 return 1; 66 } 67 68 void rtc_bbpu_power_down(void) 69 { 70 uint16_t bbpu; 71 72 /* pull PWRBB low */ 73 bbpu = RTC_BBPU_KEY | RTC_BBPU_AUTO | RTC_BBPU_PWREN; 74 if (Writeif_unlock()) { 75 RTC_Write(RTC_BBPU, bbpu); 76 if (!Write_trigger()) 77 assert(0); 78 } else { 79 assert(0); 80 } 81 } 82