developer | 65014b8 | 2015-04-13 14:47:57 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2015, ARM Limited and Contributors. All rights reserved. |
| 3 | * |
dp-arm | fa3cf0b | 2017-05-03 09:38:09 +0100 | [diff] [blame] | 4 | * SPDX-License-Identifier: BSD-3-Clause |
developer | 65014b8 | 2015-04-13 14:47:57 +0800 | [diff] [blame] | 5 | */ |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 6 | |
developer | 65014b8 | 2015-04-13 14:47:57 +0800 | [diff] [blame] | 7 | #include <assert.h> |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 8 | |
| 9 | #include <common/debug.h> |
| 10 | #include <drivers/delay_timer.h> |
| 11 | |
developer | 65014b8 | 2015-04-13 14:47:57 +0800 | [diff] [blame] | 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()) |
developer | e3ae6d0 | 2015-11-16 13:44:31 +0800 | [diff] [blame] | 77 | assert(0); |
developer | 65014b8 | 2015-04-13 14:47:57 +0800 | [diff] [blame] | 78 | } else { |
developer | e3ae6d0 | 2015-11-16 13:44:31 +0800 | [diff] [blame] | 79 | assert(0); |
developer | 65014b8 | 2015-04-13 14:47:57 +0800 | [diff] [blame] | 80 | } |
| 81 | } |