summaryrefslogtreecommitdiffstats
path: root/bsps/arm/imxrt/mcux-sdk/devices/MIMXRT1166/drivers/fsl_pgmc.c
blob: 03488ee4b1552d1fa10dc10ff149ce203147f971 (plain) (blame)
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
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
186
187
188
189
190
191
192
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
/*
 * Copyright 2019-2021, NXP
 * All rights reserved.
 *
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */

#include "fsl_pgmc.h"

/*******************************************************************************
 * Definitions
 ******************************************************************************/

/* Component ID definition, used by tools. */
#ifndef FSL_COMPONENT_ID
#define FSL_COMPONENT_ID "platform.drivers.pgmc"
#endif

/*!
 * @brief The structure of MIF signal.
 */
typedef struct
{
    __IO uint32_t SIGNAL; /*!< MIF MLPL control of each signal. */
    __IO uint32_t DELAY;  /*!< MIF Delay of each signal */
    uint32_t RESERVED[2];
} PGMC_MIF_SIGNAL_Type;

/*******************************************************************************
 * Variables
 ******************************************************************************/

/*******************************************************************************
 * Code
 ******************************************************************************/

/*!
 * brief Makes the BPC module controlled by the target CPU power mode(Such as Wait mode).
 *
 * This function makes the module controlled by four typical CPU power modes, It also configs the resource domain and
 * set memory low power level.
 *
 * param base PGMC basic power controller base address.
 * param mode Target CPU power mode.
 * param option The pointer of @ref pgmc_bpc_cpu_power_mode_option_t structure.
 */
void PGMC_BPC_ControlPowerDomainByCpuPowerMode(PGMC_BPC_Type *base,
                                               pgmc_cpu_mode_t mode,
                                               const pgmc_bpc_cpu_power_mode_option_t *option)
{
    assert(option != NULL);

    uint32_t tmp32 = base->BPC_SSAR_SAVE_CTRL;

    base->BPC_MODE = PGMC_BPC_BPC_MODE_DOMAIN_ASSIGN(option->assignDomain) |
                     PGMC_BPC_BPC_MODE_CTRL_MODE(kPGMC_ControlledByCpuPowerMode);
    switch (mode)
    {
        case kPGMC_RunMode:
            tmp32 |= PGMC_BPC_BPC_SSAR_SAVE_CTRL_SAVE_AT_RUN_MASK;
            break;
        case kPGMC_WaitMode:
            if (option->powerOff)
            {
                base->BPC_POWER_CTRL |= PGMC_BPC_BPC_POWER_CTRL_PWR_OFF_AT_WAIT_MASK;
            }
            tmp32 |= PGMC_BPC_BPC_SSAR_SAVE_CTRL_SAVE_AT_WAIT_MASK;
            break;
        case kPGMC_StopMode:
            if (option->powerOff)
            {
                base->BPC_POWER_CTRL |= PGMC_BPC_BPC_POWER_CTRL_PWR_OFF_AT_STOP_MASK;
            }
            tmp32 |= PGMC_BPC_BPC_SSAR_SAVE_CTRL_SAVE_AT_SUSPEND_MASK;
            break;
        case kPGMC_SuspendMode:
            if (option->powerOff)
            {
                base->BPC_POWER_CTRL |= PGMC_BPC_BPC_POWER_CTRL_PWR_OFF_AT_SUSPEND_MASK;
            }
            tmp32 |= PGMC_BPC_BPC_SSAR_SAVE_CTRL_SAVE_AT_SUSPEND_MASK;
            break;
        default:
            assert(false);
            break;
    }

    if (option->stateSave)
    {
        base->BPC_SSAR_SAVE_CTRL = tmp32;
    }
}

/*!
 * brief Makes the BPC module controlled by the target set points.
 *
 * This function makes the module controlled by specific set point, It also supports set memory lowe power level.
 *
 * note When setting more than one set point, use "|" between the map values in @ref _pgmc_setpoint_map.
 *
 * param base PGMC basic power controller base address.
 * param setPointMap Should be the OR'ed value of @ref _pgmc_setpoint_map.
 * param option The pointer of @ref pgmc_bpc_setpoint_mode_option_t structure.
 */
void PGMC_BPC_ControlPowerDomainBySetPointMode(PGMC_BPC_Type *base,
                                               uint32_t setPointMap,
                                               const pgmc_bpc_setpoint_mode_option_t *option)
{
    assert(option != NULL);

    setPointMap &= 0xFFFFU;

    base->BPC_MODE = PGMC_BPC_BPC_MODE_CTRL_MODE(kPGMC_ControlledBySetPoint);
    if (option->powerOff)
    {
        base->BPC_POWER_CTRL |= PGMC_BPC_BPC_POWER_CTRL_PWR_OFF_AT_SP(setPointMap);
    }

    if (option->stateSave)
    {
        base->BPC_SSAR_SAVE_CTRL = PGMC_BPC_BPC_SSAR_SAVE_CTRL_SAVE_AT_SP(setPointMap);
    }
}

/*!
 * brief Controls the selected power domain by software mode.
 *
 * note The function is used to control power domain when the CPU is in RUN mode.
 *
 * param base PGMC basic power controller base address.
 * param powerOff Power On/Off power domain in software mode.
 *                  - \b true Power off the power domain in software mode.
 *                  - \b false Power on the power domain in software mode.
 */
void PGMC_BPC_ControlPowerDomainBySoftwareMode(PGMC_BPC_Type *base, bool powerOff)
{
    if (powerOff)
    {
        base->BPC_POWER_CTRL |= (PGMC_BPC_BPC_POWER_CTRL_PSW_OFF_SOFT_MASK | PGMC_BPC_BPC_POWER_CTRL_ISO_ON_SOFT_MASK);
    }
    else
    {
        base->BPC_POWER_CTRL |= (PGMC_BPC_BPC_POWER_CTRL_PSW_ON_SOFT_MASK | PGMC_BPC_BPC_POWER_CTRL_ISO_OFF_SOFT_MASK);
    }
}

/*!
 * brief Powers off the CPC core module by the target CPU power mode(Such as Wait mode).
 *
 * param base CPC CORE module base address.
 * param mode Target CPU power mode.
 */
void PGMC_CPC_CORE_PowerOffByCpuPowerMode(PGMC_CPC_Type *base, pgmc_cpu_mode_t mode)
{
    base->CPC_CORE_MODE = PGMC_CPC_CPC_CORE_MODE_CTRL_MODE(kPGMC_ControlledByCpuPowerMode);
    switch (mode)
    {
        case kPGMC_RunMode:
            break;
        case kPGMC_WaitMode:
            base->CPC_CORE_POWER_CTRL |= PGMC_CPC_CPC_CORE_POWER_CTRL_PWR_OFF_AT_WAIT_MASK;
            break;
        case kPGMC_StopMode:
            base->CPC_CORE_POWER_CTRL |= PGMC_CPC_CPC_CORE_POWER_CTRL_PWR_OFF_AT_STOP_MASK;
            break;
        case kPGMC_SuspendMode:
            base->CPC_CORE_POWER_CTRL |= PGMC_CPC_CPC_CORE_POWER_CTRL_PWR_OFF_AT_SUSPEND_MASK;
            break;
        default:
            assert(false);
            break;
    }
}

/*!
 * brief Makes the CPC CACHE module controlled by the target CPU power mode(Such as Wait mode).
 *
 * This function makes the module controlled by four typical CPU power modes, it also can set memory low power level.
 *
 * param base CPC CACHE module base address.
 * param mode Target CPU power mode.
 * param memoryLowPowerLevel Memory low power level.
 */
void PGMC_CPC_CACHE_ControlByCpuPowerMode(PGMC_CPC_Type *base,
                                          pgmc_cpu_mode_t mode,
                                          pgmc_memory_low_power_level_t memoryLowPowerLevel)
{
    uint32_t temp32;

    base->CPC_CACHE_MODE = PGMC_CPC_CPC_CACHE_MODE_CTRL_MODE(kPGMC_ControlledByCpuPowerMode);
    temp32               = base->CPC_CACHE_CM_CTRL;
    switch (mode)
    {
        case kPGMC_RunMode:
            temp32 &= ~PGMC_CPC_CPC_CACHE_CM_CTRL_MLPL_AT_RUN_MASK;
            base->CPC_CACHE_CM_CTRL = temp32 | PGMC_CPC_CPC_CACHE_CM_CTRL_MLPL_AT_RUN(memoryLowPowerLevel);
            break;
        case kPGMC_WaitMode:
            temp32 &= ~PGMC_CPC_CPC_CACHE_CM_CTRL_MLPL_AT_WAIT_MASK;
            base->CPC_CACHE_CM_CTRL = temp32 | PGMC_CPC_CPC_CACHE_CM_CTRL_MLPL_AT_WAIT(memoryLowPowerLevel);
            break;
        case kPGMC_StopMode:
            temp32 &= ~PGMC_CPC_CPC_CACHE_CM_CTRL_MLPL_AT_STOP_MASK;
            base->CPC_CACHE_CM_CTRL = temp32 | PGMC_CPC_CPC_CACHE_CM_CTRL_MLPL_AT_STOP(memoryLowPowerLevel);
            break;
        case kPGMC_SuspendMode:
            temp32 &= ~PGMC_CPC_CPC_CACHE_CM_CTRL_MLPL_AT_SUSPEND_MASK;
            base->CPC_CACHE_CM_CTRL = temp32 | PGMC_CPC_CPC_CACHE_CM_CTRL_MLPL_AT_SUSPEND(memoryLowPowerLevel);
            break;
        default:
            assert(false);
            break;
    }
}

/*!
 * brief Makes the CPC CACHE module controlled by the target set points.
 *
 * This function makes the module controlled by specific set point, It also supports set memory lowe power level.
 *
 * note When setting more than one set point, use "|" between the map values in @ref _pgmc_setpoint_map.
 *
 * param base CPC CACHE module base address.
 * param setPointMap Should be the OR'ed value of @ref _pgmc_setpoint_map.
 * param memoryLowPowerLevel Memory low power level.
 */
void PGMC_CPC_CACHE_ControlBySetPointMode(PGMC_CPC_Type *base,
                                          uint32_t setPointMap,
                                          pgmc_memory_low_power_level_t memoryLowPowerLevel)
{
    uint32_t setPointIndex             = 0UL;
    uint32_t tmp32                     = 0UL;
    uint32_t regIndex                  = 0UL;
    volatile uint32_t *ptrMemSpCtrlReg = NULL;

    setPointMap &= 0xFFFFU;

    base->CPC_CACHE_MODE = PGMC_CPC_CPC_CACHE_MODE_CTRL_MODE(kPGMC_ControlledBySetPoint);

    ptrMemSpCtrlReg = &(base->CPC_CACHE_SP_CTRL_0);
    for (regIndex = 0UL; regIndex < 2UL; regIndex++)
    {
        ptrMemSpCtrlReg += regIndex;
        tmp32 = *ptrMemSpCtrlReg;
        for (setPointIndex = 0UL; setPointIndex < 8UL; setPointIndex++)
        {
            if (0UL != (setPointMap & (1UL << ((regIndex * 8UL) + setPointIndex))))
            {
                tmp32 &= ~((uint32_t)PGMC_CPC_CPC_CACHE_SP_CTRL_0_MLPL_AT_SP0_MASK << (setPointIndex * 4U));
                tmp32 |= ((uint32_t)memoryLowPowerLevel << (setPointIndex * 4U));
            }
        }
        *ptrMemSpCtrlReg = tmp32;
    }
}

/*!
 * brief Requests CPC cache module's memory low power level change by software mode.
 *
 * note If request memory low power level change, must wait the MLPL transition complete.
 *
 * param base CPC LMEM module base address.
 */
void PGMC_CPC_CACHE_TriggerMLPLSoftwareChange(PGMC_CPC_Type *base)
{
    base->CPC_CACHE_CM_CTRL |= PGMC_CPC_CPC_CACHE_CM_CTRL_MLPL_SOFT_MASK;

    /* If request software change, check the MLPL transition status. */
    while (0UL != ((base->CPC_CACHE_CM_CTRL) & PGMC_CPC_CPC_CACHE_CM_CTRL_MLPL_SOFT_MASK))
    {
    }
}

/*!
 * brief Makes the CPC LMEM module controlled by the target CPU power mode(Such as Wait mode).
 *
 * This function makes the module controlled by four typical CPU power modes, it also can set memory low power level.
 *
 * param base CPC LMEM module base address.
 * param mode Target CPU power mode.
 * param memoryLowPowerLevel Memory low power level.
 */
void PGMC_CPC_LMEM_ControlByCpuPowerMode(PGMC_CPC_Type *base,
                                         pgmc_cpu_mode_t mode,
                                         pgmc_memory_low_power_level_t memoryLowPowerLevel)
{
    uint32_t temp32;

    base->CPC_LMEM_MODE = PGMC_CPC_CPC_LMEM_MODE_CTRL_MODE(kPGMC_ControlledByCpuPowerMode);

    temp32 = base->CPC_LMEM_CM_CTRL;
    switch (mode)
    {
        case kPGMC_RunMode:
            temp32 &= ~PGMC_CPC_CPC_LMEM_CM_CTRL_MLPL_AT_RUN_MASK;
            base->CPC_LMEM_CM_CTRL = temp32 | PGMC_CPC_CPC_LMEM_CM_CTRL_MLPL_AT_RUN(memoryLowPowerLevel);
            break;
        case kPGMC_WaitMode:
            temp32 &= ~PGMC_CPC_CPC_LMEM_CM_CTRL_MLPL_AT_WAIT_MASK;
            base->CPC_LMEM_CM_CTRL = temp32 | PGMC_CPC_CPC_LMEM_CM_CTRL_MLPL_AT_WAIT(memoryLowPowerLevel);
            break;
        case kPGMC_StopMode:
            temp32 &= ~PGMC_CPC_CPC_LMEM_CM_CTRL_MLPL_AT_STOP_MASK;
            base->CPC_LMEM_CM_CTRL = temp32 | PGMC_CPC_CPC_LMEM_CM_CTRL_MLPL_AT_STOP(memoryLowPowerLevel);
            break;
        case kPGMC_SuspendMode:
            temp32 &= ~PGMC_CPC_CPC_LMEM_CM_CTRL_MLPL_AT_SUSPEND_MASK;
            base->CPC_LMEM_CM_CTRL = temp32 | PGMC_CPC_CPC_LMEM_CM_CTRL_MLPL_AT_SUSPEND(memoryLowPowerLevel);
            break;
        default:
            assert(false);
            break;
    }
}

/*!
 * brief Makes the CPC LMEM module controlled by the target set points.
 *
 * This function makes the module controlled by specific set point, It also supports set memory lowe power level.
 *
 * note When setting more than one set point, use "|" between the map values in @ref _pgmc_setpoint_map.
 *
 * param base CPC LMEM module base address.
 * param setPointMap Should be the OR'ed value of @ref _pgmc_setpoint_map.
 * param memoryLowPowerLevel Memory low power level.
 */
void PGMC_CPC_LMEM_ControlBySetPointMode(PGMC_CPC_Type *base,
                                         uint32_t setPointMap,
                                         pgmc_memory_low_power_level_t memoryLowPowerLevel)
{
    uint32_t setPointIndex             = 0UL;
    uint32_t tmp32                     = 0UL;
    uint32_t regIndex                  = 0UL;
    volatile uint32_t *ptrMemSpCtrlReg = NULL;

    setPointMap &= 0xFFFFU;

    base->CPC_LMEM_MODE = PGMC_CPC_CPC_LMEM_MODE_CTRL_MODE(kPGMC_ControlledBySetPoint);

    ptrMemSpCtrlReg = &(base->CPC_LMEM_SP_CTRL_0);
    for (regIndex = 0UL; regIndex < 2UL; regIndex++)
    {
        ptrMemSpCtrlReg += regIndex;
        tmp32 = *ptrMemSpCtrlReg;
        for (setPointIndex = 0UL; setPointIndex < 8UL; setPointIndex++)
        {
            if (0UL != (setPointMap & (1UL << ((regIndex * 8UL) + setPointIndex))))
            {
                tmp32 &= ~((uint32_t)PGMC_CPC_CPC_LMEM_SP_CTRL_0_MLPL_AT_SP0_MASK << (setPointIndex * 4U));
                tmp32 |= ((uint32_t)memoryLowPowerLevel << (setPointIndex * 4U));
            }
        }
        *ptrMemSpCtrlReg = tmp32;
    }
}

/*!
 * brief Requests CPC LMEM module's memory low power level change in software mode.
 *
 * note If request memory low power level change, must wait the MLPL transition complete.
 *
 * param base CPC LMEM module base address.
 */
void PGMC_CPC_LMEM_TriggerMLPLSoftwareChange(PGMC_CPC_Type *base)
{
    base->CPC_LMEM_CM_CTRL |= PGMC_CPC_CPC_LMEM_CM_CTRL_MLPL_SOFT_MASK;

    /* If request software change, check the MLPL transition status. */
    while (0UL != ((base->CPC_LMEM_CM_CTRL) & PGMC_CPC_CPC_LMEM_CM_CTRL_MLPL_SOFT_MASK))
    {
    }
}

/*!
 * brief Sets the behaviour of each signal(Such as Sleep signal) in MIF.
 *
 * note To control the memory low power operation, this function must be invoked after selecting the
 *       memory low power level.
 *       Use case:
 *        code
 *              PGMC_BPC_ControlPowerDomainByCpuPowerMode(PGMC_BPC0_BASE, kPGMC_WaitMode, kPGMC_CM7Core,
 *                  kPGMC_MLPLSleep, false);
 *              PGMC_MIF_SetSignalBehaviour(PGMC_BPC0_MIF_BASE, kPGMC_MLPLSleep, kPGMC_AssertSleepSignal);
 *        endcode
 *
 * param base PGMC MIF peripheral base address.
 * param memoryLevel The selected memory low power level. For details please refer to @ref
 * pgmc_memory_low_power_level_t.
 * param mask The mask of MIF signal behaviour. Should be the OR'ed value of @ref _pgmc_mif_signal_behaviour
 */
void PGMC_MIF_SetSignalBehaviour(PGMC_MIF_Type *base, pgmc_memory_low_power_level_t memoryLevel, uint32_t mask)
{
    uint8_t signalIndex           = 0U;
    uint32_t temp32               = 0U;
    PGMC_MIF_SIGNAL_Type *MIF_SIG = (PGMC_MIF_SIGNAL_Type *)(uint32_t)(&(base->MIF_MLPL_SLEEP));

    for (signalIndex = 0U; signalIndex < 11U; signalIndex++)
    {
        temp32 = MIF_SIG[signalIndex].SIGNAL;
        temp32 &= ~(1UL << (uint32_t)memoryLevel);
        temp32 |= ((uint32_t)(mask & (1UL << signalIndex)) << (uint32_t)memoryLevel);
        MIF_SIG[signalIndex].SIGNAL = temp32;
    }
}

/*!
 * brief Makes the PMIC module controlled by the target CPU power mode(Such as Wait mode).
 *
 * param base PMIC module base address.
 * param mode Target CPU power mode.
 */
void PGMC_PPC_ControlByCpuPowerMode(PGMC_PPC_Type *base, pgmc_cpu_mode_t mode)
{
    base->PPC_MODE = PGMC_PPC_PPC_MODE_CTRL_MODE(kPGMC_ControlledByCpuPowerMode);
    switch (mode)
    {
        case kPGMC_RunMode:
            break;
        case kPGMC_WaitMode:
            base->PPC_STBY_CM_CTRL |= PGMC_PPC_PPC_STBY_CM_CTRL_STBY_ON_AT_WAIT_MASK;
            break;
        case kPGMC_StopMode:
            base->PPC_STBY_CM_CTRL |= PGMC_PPC_PPC_STBY_CM_CTRL_STBY_ON_AT_STOP_MASK;
            break;
        case kPGMC_SuspendMode:
            base->PPC_STBY_CM_CTRL |= PGMC_PPC_PPC_STBY_CM_CTRL_STBY_ON_AT_SUSPEND_MASK;
            break;
        default:
            assert(false);
            break;
    }
}

/*!
 * brief Makes the PMIC module controlled by the target set points.
 *
 * This function makes the module controlled by specific set point, It also supports PMIC standby on.
 *
 * note When setting more than one set point, use "|" between the map values in @ref _pgmc_setpoint_map.
 *
 * param base PMIC module base address.
 * param setPointMap Should be the OR'ed value of @ref _pgmc_setpoint_map.
 * param enableStandby true: PMIC standby on when system enters set point number and system is in standby mode.
 *                      false: PMIC standby on when system enters set point number
 */
void PGMC_PPC_ControlBySetPointMode(PGMC_PPC_Type *base, uint32_t setPointMap, bool enableStandby)
{
    setPointMap &= 0xFFFFU;

    base->PPC_MODE = PGMC_PPC_PPC_MODE_CTRL_MODE(kPGMC_ControlledBySetPoint);

    if (enableStandby)
    {
        base->PPC_STBY_SP_CTRL = (setPointMap << PGMC_PPC_PPC_STBY_SP_CTRL_STBY_ON_AT_SP_SLEEP_SHIFT);
    }
    else
    {
        base->PPC_STBY_SP_CTRL = setPointMap;
    }
}