drv_clk.c 7.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
/**************************************************************************//**
*
* @copyright (C) 2020 Nuvoton Technology Corp. All rights reserved.
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date            Author       Notes
* 2020-03-25      klcheng      First version
*
******************************************************************************/

#include <rtconfig.h>

#if defined(BSP_USING_CLK)

#include <rtdevice.h>
#include <rtdbg.h>
#include <stdint.h>
#include <string.h>
#include <NuMicro.h>


/* Private define ---------------------------------------------------------------*/

/* pm run mode speed mapping */
#define CONFIG_HIGH_SPEED_FREQ      (192000000ul)
#define CONFIG_NORMAL_SPEED_FREQ    (192000000ul)
#define CONFIG_MEDIMUM_SPEED_FREQ   (144000000ul)
#define CONFIG_LOW_SPEED_FREQ       (72000000ul)

/* pm sleep mode mapping */
#define CONFIG_MODE_LIGHT           (CLK_PMUCTL_PDMSEL_FWPD)
#define CONFIG_MODE_DEEP            (CLK_PMUCTL_PDMSEL_PD)
#define CONFIG_MODE_STANDBY         (CLK_PMUCTL_PDMSEL_SPD1)
#define CONFIG_MODE_SHUTDOWN        (CLK_PMUCTL_PDMSEL_DPD)


#if defined (NU_CLK_INVOKE_WKTMR)
    /* Wake-up timer clock source is OSC10K */
    #define WKTMR_INTERVAL              (CLK_PMUCTL_WKTMRIS_65536)
#endif


/* Timer module assigned for pm device usage. */
/* e.g. If TIMERn is reserved for pm, then define the PM_TIMER_USE_INSTANCE
        macro to n value (without parentheses). */
#define PM_TIMER_USE_INSTANCE       3


/* Concatenate */
#define _CONCAT2_(x, y)             x##y
#define _CONCAT3_(x, y, z)          x##y##z
#define CONCAT2(x, y)               _CONCAT2_(x, y)
#define CONCAT3(x, y, z)            _CONCAT3_(x,y,z)

/* Concatenate the macros of timer instance for driver usage. */
#define PM_TIMER                    CONCAT2(TIMER, PM_TIMER_USE_INSTANCE)
#define PM_TMR                      CONCAT2(TMR, PM_TIMER_USE_INSTANCE)
#define PM_TIMER_MODULE             CONCAT2(PM_TMR, _MODULE)
#define PM_TIMER_IRQn               CONCAT2(PM_TMR, _IRQn)
#define PM_TIMER_IRQHandler         CONCAT2(PM_TMR, _IRQHandler)
#define PM_TIMER_SEL_LXT            CONCAT3(CLK_CLKSEL1_, PM_TMR, SEL_LXT)

/* Private typedef --------------------------------------------------------------*/


/* Private functions ------------------------------------------------------------*/
static void pm_sleep(struct rt_pm *pm, rt_uint8_t mode);
static void pm_run(struct rt_pm *pm, rt_uint8_t mode);
static void pm_timer_start(struct rt_pm *pm, rt_uint32_t timeout);
static void pm_timer_stop(struct rt_pm *pm);
static rt_tick_t pm_timer_get_tick(struct rt_pm *pm);
static rt_tick_t pm_tick_from_os_tick(rt_tick_t os_tick);
static rt_tick_t os_tick_from_pm_tick(rt_tick_t pm_tick);

/* Public functions -------------------------------------------------------------*/
int rt_hw_pm_init(void);

/* Private variables ------------------------------------------------------------*/
static struct rt_pm_ops ops =
{
    .sleep = pm_sleep,
    .run = pm_run,
    .timer_start = pm_timer_start,
    .timer_stop = pm_timer_stop,
    .timer_get_tick = pm_timer_get_tick,
};

struct rt_device pm;

W
Wayne Lin 已提交
92 93 94 95 96 97 98 99 100 101
/* Sleep and power-down mapping */
const static uint32_t g_au32SleepingMode[PM_SLEEP_MODE_MAX] =
{
    0,
    0,
    CONFIG_MODE_LIGHT,
    CONFIG_MODE_DEEP,
    CONFIG_MODE_STANDBY,
    CONFIG_MODE_SHUTDOWN
};
102 103 104 105

/* pm sleep() entry */
static void pm_sleep(struct rt_pm *pm, rt_uint8_t mode)
{
W
Wayne Lin 已提交
106 107 108 109
    RT_ASSERT(mode < PM_SLEEP_MODE_MAX);

    if ((mode == PM_SLEEP_MODE_NONE) || (mode == PM_SLEEP_MODE_IDLE))
        return;
110 111 112 113 114 115 116

    /*  wake-up source:                                                    */
    /*      PM_SLEEP_MODE_LIGHT : TIMERn                                   */
    /*      PM_SLEEP_MODE_DEEP : TIMERn                                    */
    /*      PM_SLEEP_MODE_STANDBY : wake-up timer  (optional)              */
    /*      PM_SLEEP_MODE_SHUTDOWN : wake-up timer  (optional)             */

W
Wayne Lin 已提交
117
    SYS_UnlockReg();
118 119

#if defined (NU_CLK_INVOKE_WKTMR)
W
Wayne Lin 已提交
120 121
    if ((mode == PM_SLEEP_MODE_SHUTDOWN) || (mode == PM_SLEEP_MODE_STANDBY))
    {
122 123 124
        /* Enable wake-up timer with pre-defined interval if it is invoked */
        CLK_SET_WKTMR_INTERVAL(WKTMR_INTERVAL);
        CLK_ENABLE_WKTMR();
W
Wayne Lin 已提交
125
    }
126 127
#endif

W
Wayne Lin 已提交
128 129
    CLK_SetPowerDownMode(g_au32SleepingMode[mode]);
    CLK_PowerDown();
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147

    SYS_LockReg();
}


/* pm run() entry */
static void pm_run(struct rt_pm *pm, rt_uint8_t mode)
{
    static uint8_t prev_mode = RT_PM_DEFAULT_RUN_MODE;

    /* ignore it if power mode is the same. */
    if (mode == prev_mode)
        return;

    prev_mode = mode;

    SYS_UnlockReg();

W
Wayne Lin 已提交
148 149
    /* Switch run mode frequency using PLL + HXT if HXT is enabled.
       Otherwise, the system clock will use PLL + HIRC. */
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
    switch (mode)
    {
    case PM_RUN_MODE_HIGH_SPEED:

        CLK_SetCoreClock(CONFIG_HIGH_SPEED_FREQ);
        break;

    case PM_RUN_MODE_NORMAL_SPEED:

        CLK_SetCoreClock(CONFIG_NORMAL_SPEED_FREQ);
        break;

    case PM_RUN_MODE_MEDIUM_SPEED:

        CLK_SetCoreClock(CONFIG_MEDIMUM_SPEED_FREQ);
        break;

    case PM_RUN_MODE_LOW_SPEED:

        CLK_SetCoreClock(CONFIG_LOW_SPEED_FREQ);
        break;

    default:
        RT_ASSERT(0);
        break;
    }

    SystemCoreClockUpdate();
    SysTick_Config(SystemCoreClock / RT_TICK_PER_SECOND);

    SYS_LockReg();
}


static void hw_timer_init(void)
{
186 187 188 189 190 191
    /* Assign a hardware timer for pm usage. */
    SYS_UnlockReg();
    CLK_SetModuleClock(PM_TIMER_MODULE, PM_TIMER_SEL_LXT, MODULE_NoMsk);
    CLK_EnableModuleClock(PM_TIMER_MODULE);
    SYS_LockReg();

W
Wayne Lin 已提交
192
    /* Initialize timer and enable wakeup function. */
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
    TIMER_Open(PM_TIMER, TIMER_CONTINUOUS_MODE, 1);
    TIMER_SET_PRESCALE_VALUE(PM_TIMER, 0);
    TIMER_EnableInt(PM_TIMER);
    TIMER_EnableWakeup(PM_TIMER);
    NVIC_EnableIRQ(PM_TIMER_IRQn);
}


/* convert os tick to pm timer tick */
static rt_tick_t pm_tick_from_os_tick(rt_tick_t os_tick)
{
    rt_uint32_t hz = TIMER_GetModuleClock(PM_TIMER);

    return (rt_tick_t)(hz * os_tick / RT_TICK_PER_SECOND);
}


/* convert pm timer tick to os tick */
static rt_tick_t os_tick_from_pm_tick(rt_tick_t pm_tick)
{
    static rt_uint32_t os_tick_remain = 0;
    rt_uint32_t ret, hz;

    hz = TIMER_GetModuleClock(PM_TIMER);
    ret = (pm_tick * RT_TICK_PER_SECOND + os_tick_remain) / hz;

    os_tick_remain += (pm_tick * RT_TICK_PER_SECOND);
    os_tick_remain %= hz;

    return ret;
}


/* pm_ops timer_get_tick() entry */
static rt_tick_t pm_timer_get_tick(struct rt_pm *pm)
{
    rt_tick_t tick;

    tick = TIMER_GetCounter(PM_TIMER);

    return os_tick_from_pm_tick(tick);
}


/* pm timer_start() entry */
static void pm_timer_start(struct rt_pm *pm, rt_uint32_t timeout)
{
    int tick;

    if (timeout == RT_TICK_MAX)
        return;

W
Wayne Lin 已提交
245
    /* start pm timer to compensate the os tick in power down mode */
246 247 248 249 250 251 252 253 254 255 256 257 258 259
    tick = pm_tick_from_os_tick(timeout);
    TIMER_SET_CMP_VALUE(PM_TIMER, tick);
    TIMER_Start(PM_TIMER);
}


/* pm timer_stop() entry */
static void pm_timer_stop(struct rt_pm *pm)
{
    TIMER_Stop(PM_TIMER);
    TIMER_ResetCounter(PM_TIMER);
}


W
Wayne Lin 已提交
260
/* pm device driver initialize. */
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
int rt_hw_pm_init(void)
{
    rt_uint8_t timer_mask;

    if (CLK_GetPMUWKSrc())
    {
        /* Release I/O hold status after wake-up from Standby Power-down Mode (SPD) */
        CLK->IOPDCTL = 1;

        /* Clear Power Manager Status register */
        CLK->PMUSTS = CLK_PMUSTS_CLRWK_Msk;
    }

    hw_timer_init();

    /* initialize timer mask */
    timer_mask = (1UL << PM_SLEEP_MODE_LIGHT) |
                 (1UL << PM_SLEEP_MODE_DEEP);

    /* initialize system pm module */
    rt_system_pm_init(&ops, timer_mask, RT_NULL);

    return RT_EOK;
}
INIT_BOARD_EXPORT(rt_hw_pm_init);


/* pm timer interrupt entry */
void PM_TIMER_IRQHandler(void)
{
    rt_interrupt_enter();

    if (TIMER_GetIntFlag(PM_TIMER))
    {
        TIMER_ClearIntFlag(PM_TIMER);
    }

    if (TIMER_GetWakeupFlag(PM_TIMER))
    {
        TIMER_ClearWakeupFlag(PM_TIMER);
    }

    rt_interrupt_leave();
}

#endif /* BSP_USING_CLK */