aboutsummaryrefslogtreecommitdiff
path: root/product/synquacer/module/synquacer_system/src/synquacer_pd_manage.c
blob: 35a802c54333774dd7bac88fd4fccba6b5bb6c09 (plain)
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
/*
 * Arm SCP/MCP Software
 * Copyright (c) 2018-2020, Arm Limited and Contributors. All rights reserved.
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */

#include "low_level_access.h"

#include <cmsis_os2.h>

#include <internal/pmu.h>
#include <internal/reg_PPU.h>
#include <internal/reset.h>
#include <internal/synquacer_pd.h>
#include <internal/synquacer_ppu_driver.h>
#include <internal/transaction_sw.h>

#include <mod_synquacer_system.h>

#include <fwk_log.h>
#include <fwk_macros.h>

#include <inttypes.h>
#include <stdbool.h>
#include <stdint.h>

#define SCB_DIV_ROUND_UP(NUMERATOR, DENOMINATOR) \
    (((NUMERATOR) + (DENOMINATOR)-1) / (DENOMINATOR))
#define POWER_CONV_MS_TO_PWR_ON_CYC(MS) \
    (SCB_DIV_ROUND_UP(((MS)*32768), 32000) - 1)

#define PD_TIMEOUT_COUNT ((PD_TIMEOUT_MS / PD_CHECK_CYCLE_MS) + 1)

struct {
    int32_t on_priority;
    uint32_t dev_bitmap;
    int32_t onchip_pd;
    int32_t offchip_pd;
    int32_t offchip_cycle_ms;
} sni_pmu_info[] = SNI_PMU_INFO_MP;

static const struct {
    uint32_t no;
    uint32_t dev_bitmap;
    bool booting_force_off;
    bool force_off;
} sni_ppu_info[] = SNI_PPU_INFO_MP;

static const struct {
    uint32_t dev_bitmap;
    uint32_t reg_no;
    uint32_t sw_bitmap;
} sni_transw_info[] = SNI_TRANSACTIONSW_INFO_MP;

#define FOR_EACH_PMU_INFO(index) \
    for (index = 0; index < FWK_ARRAY_SIZE(sni_pmu_info); index++)
#define FOR_EACH_PPU_INFO(index) \
    for (index = 0; index < FWK_ARRAY_SIZE(sni_ppu_info); index++)
#define FOR_EACH_TRANSW_INFO(index) \
    for (index = 0; index < FWK_ARRAY_SIZE(sni_transw_info); index++)
#define FOR_EACH_TRANSW_REG(index) \
    for (index = 0; index < TRANSW_REG_NUM; index++)

#define CONFIG_SOC_REG_ADDR_SYSOC_TRANSW_TOP UINT32_C(0x48110000 + 0x740)

#define TRANSW_ADDR(tran_tbl_num) \
    (CONFIG_SOC_REG_ADDR_SYSOC_TRANSW_TOP + tran_tbl_num * 8)

uint32_t get_domain_base_address(int domain)
{
    uint32_t address;

    switch (domain) {
    case 0:
        address = PPU0_BASE;
        break;
    case 1:
        address = PPU1_BASE;
        break;
    case 2:
        address = PPU2_BASE;
        break;
    case 3:
        address = PPU3_BASE;
        break;
    case 4:
        address = PPU4_BASE;
        break;
    case 5:
        address = PPU5_BASE;
        break;
    case 6:
        address = PPU6_BASE;
        break;
    case 7:
        address = PPU7_BASE;
        break;
    case 8:
        address = PPU8_BASE;
        break;
    default:
        address = ~0;
    }

    return address;
}

int change_power_state(
    int domain,
    int next_power_policy,
    int hwcactiveen,
    int hwcsysreqen,
    int reten)
{
    uint32_t base_address = get_domain_base_address(domain);
    uint32_t ppr = base_address + PPU_PPR_OFFSET;
    uint32_t psr = base_address + PPU_PSR_OFFSET;
    uint32_t pcr = base_address + PPU_PCR_OFFSET;
    uint32_t wdata;

    int current_power_plicy = (readl(psr) & 0x1F);

    if (current_power_plicy == next_power_policy)
        return 1;

    wdata = (reten << 4) | (hwcsysreqen << 1) | hwcactiveen;
    writel(pcr, wdata);

    writel(ppr, next_power_policy);

    return 0;
}

int read_power_status(int domain)
{
    uint32_t base_address = get_domain_base_address(domain);
    uint32_t psr = base_address + PPU_PSR_OFFSET;

    if (base_address == (uint32_t)~0)
        return 0;

    return readl(psr);
}

static void sni_power_domain_workaround_mp(void)
{
    /*
     * sni-ppu force off
     *    --- Trouble-shooting case2 by Verification-team
     *
     * "booting force off" is FALSE : active_en = 0, sysreq_en = 1
     *                        TRUE  : active_en = 0, sysreq_en = 0
     *
     * [CAUTION] Before this process need reset-clear of adb-400(pcie and dmab)
     */
    uint32_t i, j;
    int r;

    FWK_LOG_INFO("[PowerDomain] Socionext-PPU initialize .");

    /* ppu off */
    for (i = 0; i < FWK_ARRAY_SIZE(sni_ppu_info); i++) {
        uint32_t hwcsysreqen;
        hwcsysreqen = (sni_ppu_info[i].booting_force_off) ? 0 : 1;

        r = change_power_state(
            sni_ppu_info[i].no, PPU_PP_OFF, 0, hwcsysreqen, 0);
        if (r != 0) {
            FWK_LOG_ERR(
                "[PPU] powerdomain workaround error. sni-ppu%" PRIu32
                " off-process.",
                sni_ppu_info[i].no);
        }
    }

    /* ppu off-process check */
    for (i = 0; i < FWK_ARRAY_SIZE(sni_ppu_info); i++) {
        for (j = 0; j < PD_TIMEOUT_COUNT; j++) {
            if ((read_power_status(sni_ppu_info[i].no) & PPU_STATUS_MASK) ==
                PPU_PP_OFF)
                break;

            osDelay(PD_CHECK_CYCLE_MS);
        }
        if (j >= PD_TIMEOUT_COUNT) {
            FWK_LOG_ERR(
                "[PPU] powerdomain workaround error. sni-ppu%" PRIu32
                "status timeout",
                sni_ppu_info[i].no);
        }
    }

    /* reset */
    lpcm_sysoc_reset(RST_TYPE_WO_BUS, RST_PCIE0); /* PCIe#0 Link */
    lpcm_sysoc_reset(RST_TYPE_WO_BUS, RST_PCIE1); /* PCIe#1 Link */
    lpcm_sysoc_reset(RST_TYPE_ALL, RST_DDR);

    /* adb400 reset */
    lpcm_sysoc_reset(RST_TYPE_BUS, RST_PCIE0); /* PCIe#0 PowerOnReset */
    lpcm_sysoc_reset(RST_TYPE_BUS, RST_PCIE1); /* PCIe#1 PowerOnReset */
    lpcm_sysoc_reset(RST_TYPE_BUS, RST_PCIE_TOP);
    lpcm_sysoc_reset(RST_TYPE_BUS, RST_DMA);

    FWK_LOG_INFO("[PowerDomain] Socionext-PPU initialize end .");
}

static void sni_power_domain_on_mp(uint32_t dev_bitmap)
{
    uint32_t i, j, r;
    uint32_t pmu_bitmap;
    uint32_t transw_reg_bitmap[TRANSW_REG_NUM] = { 0 };

    FWK_LOG_INFO("[PowerDomain] PowerDomain All-ON start.");

    /* pmu cycle time */
    FOR_EACH_PMU_INFO(i)
    {
        if ((sni_pmu_info[i].dev_bitmap & dev_bitmap) == 0)
            continue;

        if (sni_pmu_info[i].offchip_pd == NOT_USE)
            continue;

        pmu_write_power_on_cycle(
            sni_pmu_info[i].offchip_pd,
            POWER_CONV_MS_TO_PWR_ON_CYC(sni_pmu_info[i].offchip_cycle_ms));
    }

    /* Make pmu and transaction-sw bitmap */
    pmu_bitmap = 0;
    FOR_EACH_PMU_INFO(i)
    {
        if ((sni_pmu_info[i].dev_bitmap & dev_bitmap) == 0)
            continue;

        pmu_bitmap |= (1 << sni_pmu_info[i].onchip_pd);

        if (sni_pmu_info[i].offchip_pd != NOT_USE)
            pmu_bitmap |= (1 << sni_pmu_info[i].offchip_pd);
    }
    FOR_EACH_TRANSW_INFO(i)
    {
        if ((sni_transw_info[i].dev_bitmap & dev_bitmap) == 0)
            continue;

        r = sni_transw_info[i].reg_no;
        transw_reg_bitmap[r] |= sni_transw_info[i].sw_bitmap;
    }

    /*
     * pmu all on
     * (without booting_on is 'false')
     */

    pmu_on(pmu_bitmap);

    /* waiting pmu-on */
    r = pmu_wait(pmu_bitmap, true);
    if (r != 0)
        FWK_LOG_ERR(
            "[PPU] sni-pmu timeout expected:(0x%08" PRIx32
            ") result: (0x%08" PRIx32 ").",
            pmu_bitmap,
            pmu_read_pd_power_status());

    /* adb400 reset clear */
    if ((dev_bitmap & DEV_BMAP_PCIE_BLK) != 0)
        lpcm_sysoc_reset_clear(RST_TYPE_BUS, RST_PCIE_TOP);

    if ((dev_bitmap & DEV_BMAP_PCIE0) != 0)
        lpcm_sysoc_reset_clear(
            RST_TYPE_BUS, RST_PCIE0); /* PCIe#0 PowerOnReset */

    if ((dev_bitmap & DEV_BMAP_PCIE1) != 0)
        lpcm_sysoc_reset_clear(
            RST_TYPE_BUS, RST_PCIE1); /* PCIe#1 PowerOnReset */

    if ((dev_bitmap & DEV_BMAP_DMA_BLK) != 0)
        lpcm_sysoc_reset_clear(RST_TYPE_BUS, RST_DMA);

    /* reset clear */
    if ((dev_bitmap & DEV_BMAP_PCIE0) != 0)
        lpcm_sysoc_reset_clear(RST_TYPE_WO_BUS, RST_PCIE0); /* PCIe#0 Link */

    if ((dev_bitmap & DEV_BMAP_PCIE1) != 0)
        lpcm_sysoc_reset_clear(RST_TYPE_WO_BUS, RST_PCIE1); /* PCIe#1 Link */

    if ((dev_bitmap & (DEV_BMAP_DDR0 | DEV_BMAP_DDR1)) != 0)
        lpcm_sysoc_reset_clear(RST_TYPE_ALL, RST_DDR);

    /* set TransactionSW */
    FWK_LOG_INFO("[PowerDomain] Opening transaction switch");

    FOR_EACH_TRANSW_REG(i)
    {
        FWK_LOG_INFO("[PowerDomain] Opening transaction switch + %" PRIu32, i);

        set_transactionsw_off(TRANSW_ADDR(i), transw_reg_bitmap[i]);

        FWK_LOG_INFO(
            "[PowerDomain] Finished opening transaction switch + %" PRIu32, i);
    }
    FWK_LOG_INFO("[PowerDomain] Finished transaction switch");

    FWK_LOG_INFO("[PowerDomain] PowerDomain All-ON finished.");

    /* sni-ppu all on */
    FOR_EACH_PPU_INFO(i)
    {
        if ((sni_ppu_info[i].dev_bitmap & dev_bitmap) == 0)
            continue;

        r = change_power_state(sni_ppu_info[i].no, PPU_PP_ON, 1, 1, 0);
        if (r != 0) {
            FWK_LOG_ERR(
                "[PPU] powerdomain error. sni-ppu%" PRIu32 " on-process.",
                sni_ppu_info[i].no);
        }
    }

    /* ppu on-process check */
    FOR_EACH_PPU_INFO(i)
    {
        if ((sni_ppu_info[i].dev_bitmap & dev_bitmap) == 0)
            continue;

        for (j = 0; j < PD_TIMEOUT_COUNT; j++) {
            if ((read_power_status(sni_ppu_info[i].no) & PPU_STATUS_MASK) ==
                PPU_PP_ON) {
                break;
            }
            osDelay(PD_CHECK_CYCLE_MS);
        }
        if (j >= PD_TIMEOUT_COUNT) {
            FWK_LOG_ERR(
                "[PPU] powerdomain error. sni-ppu%" PRIu32 " status timeout.",
                sni_ppu_info[i].no);
        }
    }
}

uint32_t pmu_wait(uint32_t pmu_bitmap, bool on)
{
    uint32_t i, status;

    for (i = 0; i < PD_TIMEOUT_COUNT; i++) {
        status = on ? ~pmu_read_pd_power_status() : pmu_read_pd_power_status();
        if ((status & pmu_bitmap) == 0)
            break;

        osDelay(PD_CHECK_CYCLE_MS);
    }

    return (status & pmu_bitmap);
}

static void power_domain_on(uint32_t dev_bitmap)
{
    sni_power_domain_on_mp(dev_bitmap);
}

void power_domain_coldboot(void)
{
    FWK_LOG_INFO("[SYSTEM] Initializing power domain");
    sni_power_domain_workaround_mp();
    power_domain_on(PD_PRESET_COLDBOOT);
}