summaryrefslogtreecommitdiff
path: root/bsp/rockchip/dm/clk/clk-rk-gate.c
blob: 7846b71ac99e60b685e523efde4b9ec49fa2603c (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
/*
 * Copyright (c) 2006-2022, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2022-3-08      GuEe-GUI     the first version
 */

#include "clk-rk-gate.h"

rt_inline rt_uint32_t clk_gate_readl(struct rockchip_clk_cell *rk_cell)
{
    void *base = rk_cell->provider->reg_base;

    if (rk_cell->gate_flags & CLK_GATE_BIG_ENDIAN)
    {
        return rt_be32_to_cpu(HWREG32(base + rk_cell->gate_offset));
    }

    return HWREG32(base + rk_cell->gate_offset);
}

rt_inline void clk_gate_writel(struct rockchip_clk_cell *rk_cell, rt_uint32_t val)
{
    void *base = rk_cell->provider->reg_base;

    if (rk_cell->gate_flags & CLK_GATE_BIG_ENDIAN)
    {
        HWREG32(base + rk_cell->gate_offset) = rt_cpu_to_be32(val);
    }
    else
    {
        HWREG32(base + rk_cell->gate_offset) = val;
    }
}

static void clk_gate_endisable(struct rt_clk_cell *cell, int enable)
{
    int set;
    rt_uint32_t reg;
    struct rockchip_clk_cell *rk_cell = cell_to_rockchip_clk_cell(cell);

    set = rk_cell->gate_flags & CLK_GATE_SET_TO_DISABLE ? 1 : 0;
    set ^= enable;

    if (rk_cell->gate_flags & CLK_GATE_HIWORD_MASK)
    {
        reg = RT_BIT(rk_cell->gate_shift + 16);

        if (set)
        {
            reg |= RT_BIT(rk_cell->gate_shift);
        }
    }
    else
    {
        reg = clk_gate_readl(rk_cell);

        if (set)
        {
            reg |= RT_BIT(rk_cell->gate_shift);
        }
        else
        {
            reg &= ~RT_BIT(rk_cell->gate_shift);
        }
    }

    clk_gate_writel(rk_cell, reg);
}

static rt_err_t rockchip_gate_clk_enable(struct rt_clk_cell *cell)
{
    clk_gate_endisable(cell, 1);

    return RT_EOK;
}

static void rockchip_gate_clk_disable(struct rt_clk_cell *cell)
{
    clk_gate_endisable(cell, 0);
}

static rt_bool_t rockchip_gate_clk_is_enabled(struct rt_clk_cell *cell)
{
    rt_uint32_t reg;
    struct rockchip_clk_cell *rk_cell = cell_to_rockchip_clk_cell(cell);

    reg = clk_gate_readl(rk_cell);

    /* If a set bit disables this clk, flip it before masking */
    if (rk_cell->gate_flags & CLK_GATE_SET_TO_DISABLE)
    {
        reg ^= RT_BIT(rk_cell->gate_shift);
    }

    reg &= RT_BIT(rk_cell->gate_shift);

    return !!reg;
}

const struct rt_clk_ops rockchip_gate_clk_ops =
{
    .enable = rockchip_gate_clk_enable,
    .disable = rockchip_gate_clk_disable,
    .is_enabled = rockchip_gate_clk_is_enabled,
};