summaryrefslogtreecommitdiff
path: root/bsp/allwinner/libraries/sunxi-hal/hal/source/ccmu/sunxi/clk_periph.h
blob: 340c7c20c51636ef5f0c45d0eece0390d232b437 (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
/* Copyright (c) 2019-2025 Allwinner Technology Co., Ltd. ALL rights reserved.
 *
 * Allwinner is a trademark of Allwinner Technology Co.,Ltd., registered in
 *the the People's Republic of China and other countries.
 * All Allwinner Technology Co.,Ltd. trademarks are used with permission.
 *
 * DISCLAIMER
 * THIRD PARTY LICENCES MAY BE REQUIRED TO IMPLEMENT THE SOLUTION/PRODUCT.
 * IF YOU NEED TO INTEGRATE THIRD PARTY’S TECHNOLOGY (SONY, DTS, DOLBY, AVS OR MPEGLA, ETC.)
 * IN ALLWINNERS’SDK OR PRODUCTS, YOU SHALL BE SOLELY RESPONSIBLE TO OBTAIN
 * ALL APPROPRIATELY REQUIRED THIRD PARTY LICENCES.
 * ALLWINNER SHALL HAVE NO WARRANTY, INDEMNITY OR OTHER OBLIGATIONS WITH RESPECT TO MATTERS
 * COVERED UNDER ANY REQUIRED THIRD PARTY LICENSE.
 * YOU ARE SOLELY RESPONSIBLE FOR YOUR USAGE OF THIRD PARTY’S TECHNOLOGY.
 *
 *
 * THIS SOFTWARE IS PROVIDED BY ALLWINNER"AS IS" AND TO THE MAXIMUM EXTENT
 * PERMITTED BY LAW, ALLWINNER EXPRESSLY DISCLAIMS ALL WARRANTIES OF ANY KIND,
 * WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING WITHOUT LIMITATION REGARDING
 * THE TITLE, NON-INFRINGEMENT, ACCURACY, CONDITION, COMPLETENESS, PERFORMANCE
 * OR MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 * IN NO EVENT SHALL ALLWINNER BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS, OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 * OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#ifndef __CLK_PERIPH_H__
#define __CLK_PERIPH_H__

#include "clk.h"

#define ARRAY_SIZE(a) sizeof(a)/sizeof(a[0])

#define SUNXI_CLK_PERIPH_CONFIG(name, _mux_reg, _mux_shift, _mux_width,  \
                                _div_reg, _div_mshift, _div_mwidth, _div_nshift, _div_nwidth,   \
                                _gate_flags, _enable_reg, _reset_reg, _bus_gate_reg, _drm_gate_reg, \
                                _enable_shift, _reset_shift, _bus_gate_shift, _dram_gate_shift,  _com_gate, _com_gate_off) \
struct sunxi_clk_periph sunxi_clk_periph_config_##name = {      \
    .mux = {                                                \
                                                            .reg = (volatile uint32_t   *)_mux_reg,             \
                                                            .shift = _mux_shift,                                \
                                                            .width = _mux_width,                                \
           },                                                      \
           .divider = {                                            \
                                                                   .reg = (volatile uint32_t   *)_div_reg,                   \
                                                                   .mshift = _div_mshift,                              \
                                                                   .mwidth = _div_mwidth,                              \
                                                                   .nshift = _div_nshift,                              \
                                                                   .nwidth = _div_nwidth,                              \
                      },                                                      \
                      .gate = {                                               \
                                                                              .flags = _gate_flags,                               \
                                                                              .enable = (volatile uint32_t   *)_enable_reg,             \
                                                                              .reset = (volatile uint32_t   *)_reset_reg,               \
                                                                              .bus = (volatile uint32_t   *)_bus_gate_reg,              \
                                                                              .dram = (volatile uint32_t   *)_drm_gate_reg,             \
                                                                              .enb_shift = _enable_shift,                         \
                                                                              .rst_shift = _reset_shift,                          \
                                                                              .bus_shift = _bus_gate_shift,                       \
                                                                              .ddr_shift = _dram_gate_shift,                      \
                              },                                                      \
                              .com_gate = _com_gate,                                  \
                                          .com_gate_off = _com_gate_off,                          \
}

#define SUNXI_CLK_PERIPH(_name, _clk,  _parent_arry) \
    clk_periph_t sunxi_clk_periph_##_name  = {  \
                                                .clk_core = {   \
                                                                .clk = _clk,    \
                                                                .clk_type = HAL_CLK_PERIPH, \
                                                                .current_parent = HAL_CLK_UNINITIALIZED,    \
                                                                .current_parent_type = 0, \
                                                                .clk_rate = 0,     \
                                                                .parent_rate = 0,      \
                                                                .clk_enbale = HAL_CLK_STATUS_DISABLED,   \
                                                            },  \
                                                .parent_arry = _parent_arry,    \
                                                .parent_arry_size = ARRAY_SIZE(_parent_arry), \
                                                .config = &sunxi_clk_periph_config_##_name,     \
                                             }

#define SUNXI_PERIPH_INIT(_name, _clk, _parent_clk, _clk_rate) \
    clk_base_t sunxi_periph_clk_init_##_name = {    \
                                                    .clk = _clk,    \
                                                    .parent = _parent_clk,  \
                                                    .clk_rate = _clk_rate,  \
                                               }


/**
 * struct sunxi_clk_periph_gate - peripheral gate clock
 *
 * @flags:      hardware-specific flags
 * @enable:     enable register
 * @reset:      reset register
 * @bus:        bus gating resiter
 * @dram:       dram gating register
 * @enb_shift:  enable gate bit shift
 * @rst_shift:  reset gate bit shift
 * @bus_shift:  bus gate bit shift
 * @ddr_shift:  dram gate bit shift
 *
 * Flags:
 * SUNXI_PERIPH_NO_GATE - this flag indicates that module gate is not allowed for this module.
 * SUNXI_PERIPH_NO_RESET - This flag indicates that reset is not allowed for this module.
 * SUNXI_PERIPH_NO_BUS_GATE - This flag indicates that bus gate is not allowed for this module.
 * SUNXI_PERIPH_NO_DDR_GATE - This flag indicates that dram gate is not allowed for this module.
 */
struct sunxi_clk_periph_gate
{
    u32             flags;
    volatile uint32_t   *enable;
    volatile uint32_t   *reset;
    volatile uint32_t   *bus;
    volatile uint32_t   *dram;
    u8              enb_shift;
    u8              rst_shift;
    u8              bus_shift;
    u8              ddr_shift;
};

/**
 * struct sunxi_clk_periph_div - periph divider clock
 *
 * @reg:        register containing divider
 * @mshift:     shift to the divider-m bit field, div = (m+1)
 * @mwidth:     width of the divider-m bit field
 * @nshift:     shift to the divider-n bit field, div = (1<<n)
 * @nwidth:     width of the divider-n bit field
 * @lock:       register lock
 *
 * Flags:
 */
struct sunxi_clk_periph_div
{
    volatile uint32_t   *reg;
    u8              mshift;
    u8              mwidth;
    u8              nshift;
    u8              nwidth;
    //spinlock_t      *lock;
};


/**
 * struct sunxi_clk_periph_mux - multiplexer clock
 *
 * @reg:        register controlling multiplexer
 * @shift:      shift to multiplexer bit field
 * @width:      width of mutliplexer bit field
 * @lock:       register lock
 *
 * Clock with multiple selectable parents.  Implements .get_parent, .set_parent
 * and .recalc_rate
 *
 */
struct sunxi_clk_periph_mux
{
    volatile uint32_t   *reg;
    u8              shift;
    u8              width;
    //spinlock_t      *lock;
};

struct sunxi_clk_comgate
{
    const u8        *name;
    u16             val;
    u16             mask;
    u8              share;
    u8              res;
};

#define BUS_GATE_SHARE  0x01
#define RST_GATE_SHARE  0x02
#define MBUS_GATE_SHARE 0x04
#define MOD_GATE_SHARE  0x08

#define IS_SHARE_BUS_GATE(x)  (x->com_gate?((x->com_gate->share & BUS_GATE_SHARE)?1:0):0)
#define IS_SHARE_RST_GATE(x)  (x->com_gate?((x->com_gate->share & RST_GATE_SHARE)?1:0):0)
#define IS_SHARE_MBUS_GATE(x) (x->com_gate?((x->com_gate->share & MBUS_GATE_SHARE)?1:0):0)
#define IS_SHARE_MOD_GATE(x)  (x->com_gate?((x->com_gate->share & MOD_GATE_SHARE)?1:0):0)

/**
 * struct sunxi-clk-periph - peripheral clock
 *
 * @hw:         handle between common and hardware-specific interfaces
 * @flags:      flags used across common struct clk, please take refference of the clk-provider.h
 * @lock:       lock for protecting the periph clock operations
 * @mux:        mux clock
 * @gate:       gate clock
 * @divider:    divider clock
 * @com_gate:       the shared clock
 * @com_gate_off:   bit shift to mark the flag in the com_gate
 * @priv_clkops:    divider clock ops
 * @priv_regops:    gate clock ops
 */
struct sunxi_clk_periph
{
    //struct clk_hw                   hw;
    //unsigned long                   flags;
    //spinlock_t                      *lock;

    struct sunxi_clk_periph_mux     mux;
    struct sunxi_clk_periph_gate    gate;
    struct sunxi_clk_periph_div     divider;
    struct sunxi_clk_comgate       *com_gate;
    u8                              com_gate_off;
    //struct clk_ops                 *priv_clkops;
    //struct sunxi_reg_ops           *priv_regops;
};


hal_clk_status_t sunxi_clk_periph_get_parent(clk_periph_pt clk, u8 *parent_index);
hal_clk_status_t sunxi_clk_periph_set_parent(clk_periph_pt clk, u8 index);
hal_clk_status_t sunxi_clk_periph_enable(clk_periph_pt clk);
hal_clk_status_t sunxi_clk_periph_is_enabled(clk_periph_pt clk);
hal_clk_status_t sunxi_clk_periph_disable(clk_periph_pt clk);
hal_clk_status_t sunxi_clk_periph_recalc_rate(clk_periph_pt clk, u32 *rate);
u32 sunxi_clk_periph_round_rate(clk_periph_pt clk, u32 rate, u32 prate);
hal_clk_status_t sunxi_clk_periph_set_rate(clk_periph_pt clk, u32 rate);


#endif /* __MACH_SUNXI_CLK_PERIPH_H */