blob: 2b9033ed918df020b3a31412b300ae65bc75a4ca [file] [log] [blame]
developer65014b82015-04-13 14:47:57 +08001/*
2 * Copyright (c) 2015, ARM Limited and Contributors. All rights reserved.
3 *
dp-armfa3cf0b2017-05-03 09:38:09 +01004 * SPDX-License-Identifier: BSD-3-Clause
developer65014b82015-04-13 14:47:57 +08005 */
Antonio Nino Diaze0f90632018-12-14 00:18:21 +00006
developer65014b82015-04-13 14:47:57 +08007#include <assert.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +00008
9#include <common/debug.h>
10#include <drivers/delay_timer.h>
11
developer65014b82015-04-13 14:47:57 +080012#include <mt8173_def.h>
13#include <pmic_wrap_init.h>
14#include <rtc.h>
15
16/* RTC busy status polling interval and retry count */
17enum {
18 RTC_WRTGR_POLLING_DELAY_MS = 10,
19 RTC_WRTGR_POLLING_CNT = 100
20};
21
22static 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
30static void RTC_Write(uint32_t addr, uint16_t data)
31{
32 pwrap_write((uint32_t)addr, (uint32_t)data);
33}
34
35static 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
50static int32_t Write_trigger(void)
51{
52 RTC_Write(RTC_WRTGR, 1);
53 return rtc_busy_wait();
54}
55
56static 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
68void 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())
developere3ae6d02015-11-16 13:44:31 +080077 assert(0);
developer65014b82015-04-13 14:47:57 +080078 } else {
developere3ae6d02015-11-16 13:44:31 +080079 assert(0);
developer65014b82015-04-13 14:47:57 +080080 }
81}