blob: 41854879c6af585d91d476657e7bd54e6a9d5351 [file] [log] [blame]
developer2186c982018-11-15 10:07:54 +08001/* SPDX-License-Identifier: GPL-2.0 */
2/*
3 * Copyright (C) 2018 MediaTek Inc.
4 * Author: Ryder Lee <ryder.lee@mediatek.com>
5 */
6
7#ifndef __DRV_CLK_MTK_H
8#define __DRV_CLK_MTK_H
9
Simon Glass4dcacfc2020-05-10 11:40:13 -060010#include <linux/bitops.h>
developer2186c982018-11-15 10:07:54 +080011#define CLK_XTAL 0
12#define MHZ (1000 * 1000)
13
developer2dc4caa2022-09-09 19:59:59 +080014/* flags in struct mtk_clk_tree */
15
16/* clk id == 0 doesn't mean it's xtal clk */
17#define CLK_BYPASS_XTAL BIT(0)
18
developer2186c982018-11-15 10:07:54 +080019#define HAVE_RST_BAR BIT(0)
20#define CLK_DOMAIN_SCPSYS BIT(0)
developerba560c72019-12-31 11:29:21 +080021#define CLK_MUX_SETCLR_UPD BIT(1)
developer2186c982018-11-15 10:07:54 +080022
23#define CLK_GATE_SETCLR BIT(0)
24#define CLK_GATE_SETCLR_INV BIT(1)
25#define CLK_GATE_NO_SETCLR BIT(2)
26#define CLK_GATE_NO_SETCLR_INV BIT(3)
27#define CLK_GATE_MASK GENMASK(3, 0)
28
29#define CLK_PARENT_APMIXED BIT(4)
30#define CLK_PARENT_TOPCKGEN BIT(5)
31#define CLK_PARENT_MASK GENMASK(5, 4)
32
developera588d152019-07-29 22:17:48 +080033#define ETHSYS_HIFSYS_RST_CTRL_OFS 0x34
developer02259452018-12-20 16:12:52 +080034
developer2186c982018-11-15 10:07:54 +080035/* struct mtk_pll_data - hardware-specific PLLs data */
36struct mtk_pll_data {
37 const int id;
38 u32 reg;
39 u32 pwr_reg;
40 u32 en_mask;
41 u32 pd_reg;
42 int pd_shift;
43 u32 flags;
44 u32 rst_bar_mask;
45 u64 fmax;
developer0b5e5f12019-12-31 11:29:22 +080046 u64 fmin;
developer2186c982018-11-15 10:07:54 +080047 int pcwbits;
developer0b5e5f12019-12-31 11:29:22 +080048 int pcwibits;
developer2186c982018-11-15 10:07:54 +080049 u32 pcw_reg;
50 int pcw_shift;
developer0b5e5f12019-12-31 11:29:22 +080051 u32 pcw_chg_reg;
developer2186c982018-11-15 10:07:54 +080052};
53
54/**
55 * struct mtk_fixed_clk - fixed clocks
56 *
57 * @id: index of clocks
58 * @parent: index of parnet clocks
59 * @rate: fixed rate
60 */
61struct mtk_fixed_clk {
62 const int id;
63 const int parent;
64 unsigned long rate;
65};
66
67#define FIXED_CLK(_id, _parent, _rate) { \
68 .id = _id, \
69 .parent = _parent, \
70 .rate = _rate, \
71 }
72
73/**
74 * struct mtk_fixed_factor - fixed multiplier and divider clocks
75 *
76 * @id: index of clocks
77 * @parent: index of parnet clocks
78 * @mult: multiplier
79 * @div: divider
80 * @flag: hardware-specific flags
81 */
82struct mtk_fixed_factor {
83 const int id;
84 const int parent;
85 u32 mult;
86 u32 div;
87 u32 flags;
88};
89
90#define FACTOR(_id, _parent, _mult, _div, _flags) { \
91 .id = _id, \
92 .parent = _parent, \
93 .mult = _mult, \
94 .div = _div, \
95 .flags = _flags, \
96 }
97
98/**
99 * struct mtk_composite - aggregate clock of mux, divider and gate clocks
100 *
101 * @id: index of clocks
102 * @parent: index of parnet clocks
103 * @mux_reg: hardware-specific mux register
104 * @gate_reg: hardware-specific gate register
105 * @mux_mask: mask to the mux bit field
106 * @mux_shift: shift to the mux bit field
107 * @gate_shift: shift to the gate bit field
108 * @num_parents: number of parent clocks
109 * @flags: hardware-specific flags
110 */
111struct mtk_composite {
112 const int id;
113 const int *parent;
114 u32 mux_reg;
developerba560c72019-12-31 11:29:21 +0800115 u32 mux_set_reg;
116 u32 mux_clr_reg;
117 u32 upd_reg;
developer2186c982018-11-15 10:07:54 +0800118 u32 gate_reg;
119 u32 mux_mask;
120 signed char mux_shift;
developerba560c72019-12-31 11:29:21 +0800121 signed char upd_shift;
developer2186c982018-11-15 10:07:54 +0800122 signed char gate_shift;
123 signed char num_parents;
124 u16 flags;
125};
126
127#define MUX_GATE_FLAGS(_id, _parents, _reg, _shift, _width, _gate, \
128 _flags) { \
129 .id = _id, \
130 .mux_reg = _reg, \
131 .mux_shift = _shift, \
132 .mux_mask = BIT(_width) - 1, \
133 .gate_reg = _reg, \
134 .gate_shift = _gate, \
135 .parent = _parents, \
136 .num_parents = ARRAY_SIZE(_parents), \
137 .flags = _flags, \
138 }
139
140#define MUX_GATE(_id, _parents, _reg, _shift, _width, _gate) \
141 MUX_GATE_FLAGS(_id, _parents, _reg, _shift, _width, _gate, 0)
142
143#define MUX(_id, _parents, _reg, _shift, _width) { \
144 .id = _id, \
145 .mux_reg = _reg, \
146 .mux_shift = _shift, \
147 .mux_mask = BIT(_width) - 1, \
148 .gate_shift = -1, \
149 .parent = _parents, \
150 .num_parents = ARRAY_SIZE(_parents), \
151 .flags = 0, \
152 }
153
developerba560c72019-12-31 11:29:21 +0800154#define MUX_CLR_SET_UPD_FLAGS(_id, _parents, _mux_ofs, _mux_set_ofs,\
155 _mux_clr_ofs, _shift, _width, _gate, \
156 _upd_ofs, _upd, _flags) { \
157 .id = _id, \
158 .mux_reg = _mux_ofs, \
159 .mux_set_reg = _mux_set_ofs, \
160 .mux_clr_reg = _mux_clr_ofs, \
161 .upd_reg = _upd_ofs, \
162 .upd_shift = _upd, \
163 .mux_shift = _shift, \
164 .mux_mask = BIT(_width) - 1, \
165 .gate_reg = _mux_ofs, \
166 .gate_shift = _gate, \
167 .parent = _parents, \
168 .num_parents = ARRAY_SIZE(_parents), \
169 .flags = _flags, \
170 }
171
developer2186c982018-11-15 10:07:54 +0800172struct mtk_gate_regs {
173 u32 sta_ofs;
174 u32 clr_ofs;
175 u32 set_ofs;
176};
177
178/**
179 * struct mtk_gate - gate clocks
180 *
181 * @id: index of gate clocks
182 * @parent: index of parnet clocks
183 * @regs: hardware-specific mux register
184 * @shift: shift to the gate bit field
185 * @flags: hardware-specific flags
186 */
187struct mtk_gate {
188 const int id;
189 const int parent;
190 const struct mtk_gate_regs *regs;
191 int shift;
192 u32 flags;
193};
194
195/* struct mtk_clk_tree - clock tree */
196struct mtk_clk_tree {
197 unsigned long xtal_rate;
198 unsigned long xtal2_rate;
199 const int fdivs_offs;
200 const int muxes_offs;
201 const struct mtk_pll_data *plls;
202 const struct mtk_fixed_clk *fclks;
203 const struct mtk_fixed_factor *fdivs;
204 const struct mtk_composite *muxes;
developer2dc4caa2022-09-09 19:59:59 +0800205 u32 flags;
developer2186c982018-11-15 10:07:54 +0800206};
207
208struct mtk_clk_priv {
developerfd47f762022-09-09 20:00:01 +0800209 struct udevice *parent;
developer2186c982018-11-15 10:07:54 +0800210 void __iomem *base;
211 const struct mtk_clk_tree *tree;
212};
213
214struct mtk_cg_priv {
developerfd47f762022-09-09 20:00:01 +0800215 struct udevice *parent;
developer2186c982018-11-15 10:07:54 +0800216 void __iomem *base;
217 const struct mtk_clk_tree *tree;
218 const struct mtk_gate *gates;
219};
220
221extern const struct clk_ops mtk_clk_apmixedsys_ops;
222extern const struct clk_ops mtk_clk_topckgen_ops;
223extern const struct clk_ops mtk_clk_gate_ops;
224
225int mtk_common_clk_init(struct udevice *dev,
226 const struct mtk_clk_tree *tree);
227int mtk_common_clk_gate_init(struct udevice *dev,
228 const struct mtk_clk_tree *tree,
229 const struct mtk_gate *gates);
230
231#endif /* __DRV_CLK_MTK_H */