mediatek: mt8173: refactor RTC and PMIC drivers
authorkenny liang <kenny.liang@mediatek.com>
Thu, 2 May 2019 13:06:05 +0000 (21:06 +0800)
committerkenny liang <kenny.liang@mediatek.com>
Mon, 16 Sep 2019 02:27:21 +0000 (10:27 +0800)
Refactor RTC and PMIC drivers.

Signed-off-by: kenny liang <kenny.liang@mediatek.com>
Change-Id: I74fca536cd61e00c962f080f1ba3759287682ecf

plat/mediatek/mt8173/drivers/pmic/pmic_wrap_init.c [deleted file]
plat/mediatek/mt8173/drivers/pmic/pmic_wrap_init.h
plat/mediatek/mt8173/drivers/rtc/rtc.c
plat/mediatek/mt8173/drivers/rtc/rtc.h
plat/mediatek/mt8173/platform.mk

diff --git a/plat/mediatek/mt8173/drivers/pmic/pmic_wrap_init.c b/plat/mediatek/mt8173/drivers/pmic/pmic_wrap_init.c
deleted file mode 100644 (file)
index 8120d99..0000000
+++ /dev/null
@@ -1,165 +0,0 @@
-/*
- * Copyright (c) 2015, ARM Limited and Contributors. All rights reserved.
- *
- * SPDX-License-Identifier: BSD-3-Clause
- */
-
-#include <common/debug.h>
-#include <drivers/delay_timer.h>
-#include <lib/mmio.h>
-
-#include <mt8173_def.h>
-#include <pmic_wrap_init.h>
-
-/* pmic wrap module wait_idle and read polling interval (in microseconds) */
-enum {
-       WAIT_IDLE_POLLING_DELAY_US      = 1,
-       READ_POLLING_DELAY_US           = 2
-};
-
-static inline uint32_t wait_for_state_idle(uint32_t timeout_us,
-                                          void *wacs_register,
-                                          void *wacs_vldclr_register,
-                                          uint32_t *read_reg)
-{
-       uint32_t reg_rdata;
-       uint32_t retry;
-
-       retry = (timeout_us + WAIT_IDLE_POLLING_DELAY_US) /
-               WAIT_IDLE_POLLING_DELAY_US;
-
-       do {
-               udelay(WAIT_IDLE_POLLING_DELAY_US);
-               reg_rdata = mmio_read_32((uintptr_t)wacs_register);
-               /* if last read command timeout,clear vldclr bit
-                  read command state machine:FSM_REQ-->wfdle-->WFVLDCLR;
-                  write:FSM_REQ-->idle */
-               switch (((reg_rdata >> RDATA_WACS_FSM_SHIFT) &
-                       RDATA_WACS_FSM_MASK)) {
-               case WACS_FSM_WFVLDCLR:
-                       mmio_write_32((uintptr_t)wacs_vldclr_register, 1);
-                       ERROR("WACS_FSM = PMIC_WRAP_WACS_VLDCLR\n");
-                       break;
-               case WACS_FSM_WFDLE:
-                       ERROR("WACS_FSM = WACS_FSM_WFDLE\n");
-                       break;
-               case WACS_FSM_REQ:
-                       ERROR("WACS_FSM = WACS_FSM_REQ\n");
-                       break;
-               case WACS_FSM_IDLE:
-                       goto done;
-               default:
-                       break;
-               }
-
-               retry--;
-       } while (retry);
-
-done:
-       if (!retry)     /* timeout */
-               return E_PWR_WAIT_IDLE_TIMEOUT;
-
-       if (read_reg)
-               *read_reg = reg_rdata;
-       return 0;
-}
-
-static inline uint32_t wait_for_state_ready(uint32_t timeout_us,
-                                           void *wacs_register,
-                                           uint32_t *read_reg)
-{
-       uint32_t reg_rdata;
-       uint32_t retry;
-
-       retry = (timeout_us + READ_POLLING_DELAY_US) / READ_POLLING_DELAY_US;
-
-       do {
-               udelay(READ_POLLING_DELAY_US);
-               reg_rdata = mmio_read_32((uintptr_t)wacs_register);
-
-               if (((reg_rdata >> RDATA_WACS_FSM_SHIFT) & RDATA_WACS_FSM_MASK)
-                   == WACS_FSM_WFVLDCLR)
-                       break;
-
-               retry--;
-       } while (retry);
-
-       if (!retry) {   /* timeout */
-               ERROR("timeout when waiting for idle\n");
-               return E_PWR_WAIT_IDLE_TIMEOUT_READ;
-       }
-
-       if (read_reg)
-               *read_reg = reg_rdata;
-       return 0;
-}
-
-static int32_t pwrap_wacs2(uint32_t write,
-                   uint32_t adr,
-                   uint32_t wdata,
-                   uint32_t *rdata,
-                   uint32_t init_check)
-{
-       uint32_t reg_rdata = 0;
-       uint32_t wacs_write = 0;
-       uint32_t wacs_adr = 0;
-       uint32_t wacs_cmd = 0;
-       uint32_t return_value = 0;
-
-       if (init_check) {
-               reg_rdata = mmio_read_32((uintptr_t)&mt8173_pwrap->wacs2_rdata);
-               /* Prevent someone to used pwrap before pwrap init */
-               if (((reg_rdata >> RDATA_INIT_DONE_SHIFT) &
-                   RDATA_INIT_DONE_MASK) != WACS_INIT_DONE) {
-                       ERROR("initialization isn't finished\n");
-                       return E_PWR_NOT_INIT_DONE;
-               }
-       }
-       reg_rdata = 0;
-       /* Check IDLE in advance */
-       return_value = wait_for_state_idle(TIMEOUT_WAIT_IDLE,
-                               &mt8173_pwrap->wacs2_rdata,
-                               &mt8173_pwrap->wacs2_vldclr,
-                               0);
-       if (return_value != 0) {
-               ERROR("wait_for_fsm_idle fail,return_value=%d\n", return_value);
-               goto FAIL;
-       }
-       wacs_write = write << 31;
-       wacs_adr = (adr >> 1) << 16;
-       wacs_cmd = wacs_write | wacs_adr | wdata;
-
-       mmio_write_32((uintptr_t)&mt8173_pwrap->wacs2_cmd, wacs_cmd);
-       if (write == 0) {
-               if (NULL == rdata) {
-                       ERROR("rdata is a NULL pointer\n");
-                       return_value = E_PWR_INVALID_ARG;
-                       goto FAIL;
-               }
-               return_value = wait_for_state_ready(TIMEOUT_READ,
-                                       &mt8173_pwrap->wacs2_rdata,
-                                       &reg_rdata);
-               if (return_value != 0) {
-                       ERROR("wait_for_fsm_vldclr fail,return_value=%d\n",
-                                return_value);
-                       goto FAIL;
-               }
-               *rdata = ((reg_rdata >> RDATA_WACS_RDATA_SHIFT)
-                         & RDATA_WACS_RDATA_MASK);
-               mmio_write_32((uintptr_t)&mt8173_pwrap->wacs2_vldclr, 1);
-       }
-FAIL:
-       return return_value;
-}
-
-/* external API for pmic_wrap user */
-
-int32_t pwrap_read(uint32_t adr, uint32_t *rdata)
-{
-       return pwrap_wacs2(0, adr, 0, rdata, 1);
-}
-
-int32_t pwrap_write(uint32_t adr, uint32_t wdata)
-{
-       return pwrap_wacs2(1, adr, wdata, 0, 1);
-}
index 0f097713726705811d82b10e38f33f4350f94d51..0dffc23eed0347b1844f5ddd49ef3144e6209a0e 100644 (file)
@@ -7,11 +7,13 @@
 #ifndef PMIC_WRAP_INIT_H
 #define PMIC_WRAP_INIT_H
 
+#include <platform_def.h>
+
 /* external API */
 int32_t pwrap_read(uint32_t adr, uint32_t *rdata);
 int32_t pwrap_write(uint32_t adr, uint32_t wdata);
 
-static struct mt8173_pmic_wrap_regs *const mt8173_pwrap =
+static struct mt8173_pmic_wrap_regs *const mtk_pwrap =
        (void *)PMIC_WRAP_BASE;
 
 /* timeout setting */
index 2b9033ed918df020b3a31412b300ae65bc75a4ca..587886c689dd28192dd450c7be4cae9d90dd1e25 100644 (file)
@@ -5,66 +5,11 @@
  */
 
 #include <assert.h>
-
 #include <common/debug.h>
-#include <drivers/delay_timer.h>
 
 #include <mt8173_def.h>
-#include <pmic_wrap_init.h>
 #include <rtc.h>
 
-/* RTC busy status polling interval and retry count */
-enum {
-       RTC_WRTGR_POLLING_DELAY_MS      = 10,
-       RTC_WRTGR_POLLING_CNT           = 100
-};
-
-static uint16_t RTC_Read(uint32_t addr)
-{
-       uint32_t rdata = 0;
-
-       pwrap_read((uint32_t)addr, &rdata);
-       return (uint16_t)rdata;
-}
-
-static void RTC_Write(uint32_t addr, uint16_t data)
-{
-       pwrap_write((uint32_t)addr, (uint32_t)data);
-}
-
-static inline int32_t rtc_busy_wait(void)
-{
-       uint64_t retry = RTC_WRTGR_POLLING_CNT;
-
-       do {
-               mdelay(RTC_WRTGR_POLLING_DELAY_MS);
-               if (!(RTC_Read(RTC_BBPU) & RTC_BBPU_CBUSY))
-                       return 1;
-               retry--;
-       } while (retry);
-
-       ERROR("[RTC] rtc cbusy time out!\n");
-       return 0;
-}
-
-static int32_t Write_trigger(void)
-{
-       RTC_Write(RTC_WRTGR, 1);
-       return rtc_busy_wait();
-}
-
-static int32_t Writeif_unlock(void)
-{
-       RTC_Write(RTC_PROT, RTC_PROT_UNLOCK1);
-       if (!Write_trigger())
-               return 0;
-       RTC_Write(RTC_PROT, RTC_PROT_UNLOCK2);
-       if (!Write_trigger())
-               return 0;
-
-       return 1;
-}
-
 void rtc_bbpu_power_down(void)
 {
        uint16_t bbpu;
@@ -73,7 +18,7 @@ void rtc_bbpu_power_down(void)
        bbpu = RTC_BBPU_KEY | RTC_BBPU_AUTO | RTC_BBPU_PWREN;
        if (Writeif_unlock()) {
                RTC_Write(RTC_BBPU, bbpu);
-               if (!Write_trigger())
+               if (!RTC_Write_Trigger())
                        assert(0);
        } else {
                assert(0);
index 9c4ca49a90b1b37a55ac930d9c722712f2e3b78a..f60a4c18f5547846b3bbf4fd4834c0b9ec61eccc 100644 (file)
@@ -49,6 +49,12 @@ enum {
        RTC_BBPU_KEY    = 0x43 << 8
 };
 
+/* external API */
+uint16_t RTC_Read(uint32_t addr);
+void RTC_Write(uint32_t addr, uint16_t data);
+int32_t rtc_busy_wait(void);
+int32_t RTC_Write_Trigger(void);
+int32_t Writeif_unlock(void);
 void rtc_bbpu_power_down(void);
 
 #endif /* RTC_H */
index e5eca9fcc650656941e2ac7234382fcf44f39ca7..a66c49bb45c2d3be2a5e2ef3d4730a3764c6a96c 100644 (file)
@@ -35,6 +35,8 @@ BL31_SOURCES          +=      common/desc_image_load.c                        \
                                lib/cpus/aarch64/cortex_a53.S                   \
                                lib/cpus/aarch64/cortex_a57.S                   \
                                lib/cpus/aarch64/cortex_a72.S                   \
+                               ${MTK_PLAT}/common/drivers/pmic_wrap/pmic_wrap_init.c   \
+                               ${MTK_PLAT}/common/drivers/rtc/rtc_common.c     \
                                ${MTK_PLAT}/common/mtk_plat_common.c            \
                                ${MTK_PLAT}/common/mtk_sip_svc.c                \
                                ${MTK_PLAT_SOC}/aarch64/plat_helpers.S          \
@@ -42,7 +44,6 @@ BL31_SOURCES          +=      common/desc_image_load.c                        \
                                ${MTK_PLAT_SOC}/bl31_plat_setup.c               \
                                ${MTK_PLAT_SOC}/drivers/crypt/crypt.c           \
                                ${MTK_PLAT_SOC}/drivers/mtcmos/mtcmos.c         \
-                               ${MTK_PLAT_SOC}/drivers/pmic/pmic_wrap_init.c   \
                                ${MTK_PLAT_SOC}/drivers/rtc/rtc.c               \
                                ${MTK_PLAT_SOC}/drivers/spm/spm.c               \
                                ${MTK_PLAT_SOC}/drivers/spm/spm_hotplug.c       \