developer | 2186c98 | 2018-11-15 10:07:54 +0800 | [diff] [blame] | 1 | /* 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 Glass | 4dcacfc | 2020-05-10 11:40:13 -0600 | [diff] [blame] | 10 | #include <linux/bitops.h> |
developer | 2186c98 | 2018-11-15 10:07:54 +0800 | [diff] [blame] | 11 | #define CLK_XTAL 0 |
| 12 | #define MHZ (1000 * 1000) |
| 13 | |
developer | 2dc4caa | 2022-09-09 19:59:59 +0800 | [diff] [blame] | 14 | /* flags in struct mtk_clk_tree */ |
| 15 | |
Christian Marangi | a4143eb | 2024-06-28 19:40:50 +0200 | [diff] [blame] | 16 | /* clk id == 0 doesn't mean it's xtal clk |
| 17 | * This doesn't apply when CLK_PARENT_MIXED is defined. |
| 18 | * With CLK_PARENT_MIXED declare CLK_PARENT_XTAL for the |
| 19 | * relevant parent. |
| 20 | */ |
developer | 2dc4caa | 2022-09-09 19:59:59 +0800 | [diff] [blame] | 21 | #define CLK_BYPASS_XTAL BIT(0) |
| 22 | |
developer | 2186c98 | 2018-11-15 10:07:54 +0800 | [diff] [blame] | 23 | #define HAVE_RST_BAR BIT(0) |
| 24 | #define CLK_DOMAIN_SCPSYS BIT(0) |
developer | ba560c7 | 2019-12-31 11:29:21 +0800 | [diff] [blame] | 25 | #define CLK_MUX_SETCLR_UPD BIT(1) |
developer | 2186c98 | 2018-11-15 10:07:54 +0800 | [diff] [blame] | 26 | |
| 27 | #define CLK_GATE_SETCLR BIT(0) |
| 28 | #define CLK_GATE_SETCLR_INV BIT(1) |
| 29 | #define CLK_GATE_NO_SETCLR BIT(2) |
| 30 | #define CLK_GATE_NO_SETCLR_INV BIT(3) |
| 31 | #define CLK_GATE_MASK GENMASK(3, 0) |
| 32 | |
| 33 | #define CLK_PARENT_APMIXED BIT(4) |
| 34 | #define CLK_PARENT_TOPCKGEN BIT(5) |
developer | ad5b075 | 2022-09-09 20:00:04 +0800 | [diff] [blame] | 35 | #define CLK_PARENT_INFRASYS BIT(6) |
developer | f724f11 | 2022-09-09 20:00:07 +0800 | [diff] [blame] | 36 | #define CLK_PARENT_XTAL BIT(7) |
Christian Marangi | a4143eb | 2024-06-28 19:40:50 +0200 | [diff] [blame] | 37 | /* |
| 38 | * For CLK_PARENT_MIXED to correctly work, is required to |
| 39 | * define in clk_tree flags the clk type using the alias. |
| 40 | */ |
| 41 | #define CLK_PARENT_MIXED BIT(8) |
| 42 | #define CLK_PARENT_MASK GENMASK(8, 4) |
| 43 | |
| 44 | /* alias to reference clk type */ |
| 45 | #define CLK_APMIXED CLK_PARENT_APMIXED |
| 46 | #define CLK_TOPCKGEN CLK_PARENT_TOPCKGEN |
| 47 | #define CLK_INFRASYS CLK_PARENT_INFRASYS |
developer | 2186c98 | 2018-11-15 10:07:54 +0800 | [diff] [blame] | 48 | |
developer | a588d15 | 2019-07-29 22:17:48 +0800 | [diff] [blame] | 49 | #define ETHSYS_HIFSYS_RST_CTRL_OFS 0x34 |
developer | 0225945 | 2018-12-20 16:12:52 +0800 | [diff] [blame] | 50 | |
developer | 2186c98 | 2018-11-15 10:07:54 +0800 | [diff] [blame] | 51 | /* struct mtk_pll_data - hardware-specific PLLs data */ |
| 52 | struct mtk_pll_data { |
| 53 | const int id; |
| 54 | u32 reg; |
| 55 | u32 pwr_reg; |
| 56 | u32 en_mask; |
| 57 | u32 pd_reg; |
| 58 | int pd_shift; |
| 59 | u32 flags; |
| 60 | u32 rst_bar_mask; |
| 61 | u64 fmax; |
developer | 0b5e5f1 | 2019-12-31 11:29:22 +0800 | [diff] [blame] | 62 | u64 fmin; |
developer | 2186c98 | 2018-11-15 10:07:54 +0800 | [diff] [blame] | 63 | int pcwbits; |
developer | 0b5e5f1 | 2019-12-31 11:29:22 +0800 | [diff] [blame] | 64 | int pcwibits; |
developer | 2186c98 | 2018-11-15 10:07:54 +0800 | [diff] [blame] | 65 | u32 pcw_reg; |
| 66 | int pcw_shift; |
developer | 0b5e5f1 | 2019-12-31 11:29:22 +0800 | [diff] [blame] | 67 | u32 pcw_chg_reg; |
developer | 2186c98 | 2018-11-15 10:07:54 +0800 | [diff] [blame] | 68 | }; |
| 69 | |
| 70 | /** |
| 71 | * struct mtk_fixed_clk - fixed clocks |
| 72 | * |
| 73 | * @id: index of clocks |
| 74 | * @parent: index of parnet clocks |
| 75 | * @rate: fixed rate |
| 76 | */ |
| 77 | struct mtk_fixed_clk { |
| 78 | const int id; |
| 79 | const int parent; |
| 80 | unsigned long rate; |
| 81 | }; |
| 82 | |
| 83 | #define FIXED_CLK(_id, _parent, _rate) { \ |
| 84 | .id = _id, \ |
| 85 | .parent = _parent, \ |
| 86 | .rate = _rate, \ |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * struct mtk_fixed_factor - fixed multiplier and divider clocks |
| 91 | * |
| 92 | * @id: index of clocks |
| 93 | * @parent: index of parnet clocks |
| 94 | * @mult: multiplier |
| 95 | * @div: divider |
| 96 | * @flag: hardware-specific flags |
| 97 | */ |
| 98 | struct mtk_fixed_factor { |
| 99 | const int id; |
| 100 | const int parent; |
| 101 | u32 mult; |
| 102 | u32 div; |
| 103 | u32 flags; |
| 104 | }; |
| 105 | |
| 106 | #define FACTOR(_id, _parent, _mult, _div, _flags) { \ |
| 107 | .id = _id, \ |
| 108 | .parent = _parent, \ |
| 109 | .mult = _mult, \ |
| 110 | .div = _div, \ |
| 111 | .flags = _flags, \ |
| 112 | } |
| 113 | |
| 114 | /** |
Christian Marangi | a4143eb | 2024-06-28 19:40:50 +0200 | [diff] [blame] | 115 | * struct mtk_parent - clock parent with flags. Needed for MUX that |
| 116 | * parent with mixed infracfg and topckgen. |
| 117 | * |
| 118 | * @id: index of parent clocks |
| 119 | * @flags: hardware-specific flags (parent location, |
| 120 | * infracfg, topckgen, APMIXED, xtal ...) |
| 121 | */ |
| 122 | struct mtk_parent { |
| 123 | const int id; |
| 124 | u16 flags; |
| 125 | }; |
| 126 | |
| 127 | #define PARENT(_id, _flags) { \ |
| 128 | .id = _id, \ |
| 129 | .flags = _flags, \ |
| 130 | } |
| 131 | |
| 132 | /** |
developer | 2186c98 | 2018-11-15 10:07:54 +0800 | [diff] [blame] | 133 | * struct mtk_composite - aggregate clock of mux, divider and gate clocks |
| 134 | * |
| 135 | * @id: index of clocks |
| 136 | * @parent: index of parnet clocks |
Christian Marangi | a4143eb | 2024-06-28 19:40:50 +0200 | [diff] [blame] | 137 | * @parent: index of parnet clocks |
| 138 | * @parent_flags: table of parent clocks with flags |
developer | 2186c98 | 2018-11-15 10:07:54 +0800 | [diff] [blame] | 139 | * @mux_reg: hardware-specific mux register |
| 140 | * @gate_reg: hardware-specific gate register |
| 141 | * @mux_mask: mask to the mux bit field |
| 142 | * @mux_shift: shift to the mux bit field |
| 143 | * @gate_shift: shift to the gate bit field |
| 144 | * @num_parents: number of parent clocks |
| 145 | * @flags: hardware-specific flags |
| 146 | */ |
| 147 | struct mtk_composite { |
| 148 | const int id; |
Christian Marangi | a4143eb | 2024-06-28 19:40:50 +0200 | [diff] [blame] | 149 | union { |
| 150 | const int *parent; |
| 151 | const struct mtk_parent *parent_flags; |
| 152 | }; |
developer | 2186c98 | 2018-11-15 10:07:54 +0800 | [diff] [blame] | 153 | u32 mux_reg; |
developer | ba560c7 | 2019-12-31 11:29:21 +0800 | [diff] [blame] | 154 | u32 mux_set_reg; |
| 155 | u32 mux_clr_reg; |
| 156 | u32 upd_reg; |
developer | 2186c98 | 2018-11-15 10:07:54 +0800 | [diff] [blame] | 157 | u32 gate_reg; |
| 158 | u32 mux_mask; |
| 159 | signed char mux_shift; |
developer | ba560c7 | 2019-12-31 11:29:21 +0800 | [diff] [blame] | 160 | signed char upd_shift; |
developer | 2186c98 | 2018-11-15 10:07:54 +0800 | [diff] [blame] | 161 | signed char gate_shift; |
| 162 | signed char num_parents; |
| 163 | u16 flags; |
| 164 | }; |
| 165 | |
| 166 | #define MUX_GATE_FLAGS(_id, _parents, _reg, _shift, _width, _gate, \ |
| 167 | _flags) { \ |
| 168 | .id = _id, \ |
| 169 | .mux_reg = _reg, \ |
| 170 | .mux_shift = _shift, \ |
| 171 | .mux_mask = BIT(_width) - 1, \ |
| 172 | .gate_reg = _reg, \ |
| 173 | .gate_shift = _gate, \ |
| 174 | .parent = _parents, \ |
| 175 | .num_parents = ARRAY_SIZE(_parents), \ |
| 176 | .flags = _flags, \ |
| 177 | } |
| 178 | |
| 179 | #define MUX_GATE(_id, _parents, _reg, _shift, _width, _gate) \ |
| 180 | MUX_GATE_FLAGS(_id, _parents, _reg, _shift, _width, _gate, 0) |
| 181 | |
| 182 | #define MUX(_id, _parents, _reg, _shift, _width) { \ |
| 183 | .id = _id, \ |
| 184 | .mux_reg = _reg, \ |
| 185 | .mux_shift = _shift, \ |
| 186 | .mux_mask = BIT(_width) - 1, \ |
| 187 | .gate_shift = -1, \ |
| 188 | .parent = _parents, \ |
| 189 | .num_parents = ARRAY_SIZE(_parents), \ |
| 190 | .flags = 0, \ |
| 191 | } |
| 192 | |
developer | ba560c7 | 2019-12-31 11:29:21 +0800 | [diff] [blame] | 193 | #define MUX_CLR_SET_UPD_FLAGS(_id, _parents, _mux_ofs, _mux_set_ofs,\ |
| 194 | _mux_clr_ofs, _shift, _width, _gate, \ |
| 195 | _upd_ofs, _upd, _flags) { \ |
| 196 | .id = _id, \ |
| 197 | .mux_reg = _mux_ofs, \ |
| 198 | .mux_set_reg = _mux_set_ofs, \ |
| 199 | .mux_clr_reg = _mux_clr_ofs, \ |
| 200 | .upd_reg = _upd_ofs, \ |
| 201 | .upd_shift = _upd, \ |
| 202 | .mux_shift = _shift, \ |
| 203 | .mux_mask = BIT(_width) - 1, \ |
| 204 | .gate_reg = _mux_ofs, \ |
| 205 | .gate_shift = _gate, \ |
| 206 | .parent = _parents, \ |
| 207 | .num_parents = ARRAY_SIZE(_parents), \ |
| 208 | .flags = _flags, \ |
| 209 | } |
| 210 | |
developer | 2186c98 | 2018-11-15 10:07:54 +0800 | [diff] [blame] | 211 | struct mtk_gate_regs { |
| 212 | u32 sta_ofs; |
| 213 | u32 clr_ofs; |
| 214 | u32 set_ofs; |
| 215 | }; |
| 216 | |
| 217 | /** |
| 218 | * struct mtk_gate - gate clocks |
| 219 | * |
| 220 | * @id: index of gate clocks |
| 221 | * @parent: index of parnet clocks |
| 222 | * @regs: hardware-specific mux register |
| 223 | * @shift: shift to the gate bit field |
| 224 | * @flags: hardware-specific flags |
| 225 | */ |
| 226 | struct mtk_gate { |
| 227 | const int id; |
| 228 | const int parent; |
| 229 | const struct mtk_gate_regs *regs; |
| 230 | int shift; |
| 231 | u32 flags; |
| 232 | }; |
| 233 | |
| 234 | /* struct mtk_clk_tree - clock tree */ |
| 235 | struct mtk_clk_tree { |
| 236 | unsigned long xtal_rate; |
| 237 | unsigned long xtal2_rate; |
| 238 | const int fdivs_offs; |
| 239 | const int muxes_offs; |
Christian Marangi | baa244c | 2024-06-28 19:40:48 +0200 | [diff] [blame] | 240 | const int gates_offs; |
developer | 2186c98 | 2018-11-15 10:07:54 +0800 | [diff] [blame] | 241 | const struct mtk_pll_data *plls; |
| 242 | const struct mtk_fixed_clk *fclks; |
| 243 | const struct mtk_fixed_factor *fdivs; |
| 244 | const struct mtk_composite *muxes; |
Christian Marangi | baa244c | 2024-06-28 19:40:48 +0200 | [diff] [blame] | 245 | const struct mtk_gate *gates; |
developer | 2dc4caa | 2022-09-09 19:59:59 +0800 | [diff] [blame] | 246 | u32 flags; |
developer | 2186c98 | 2018-11-15 10:07:54 +0800 | [diff] [blame] | 247 | }; |
| 248 | |
| 249 | struct mtk_clk_priv { |
developer | fd47f76 | 2022-09-09 20:00:01 +0800 | [diff] [blame] | 250 | struct udevice *parent; |
developer | 2186c98 | 2018-11-15 10:07:54 +0800 | [diff] [blame] | 251 | void __iomem *base; |
| 252 | const struct mtk_clk_tree *tree; |
| 253 | }; |
| 254 | |
| 255 | struct mtk_cg_priv { |
developer | fd47f76 | 2022-09-09 20:00:01 +0800 | [diff] [blame] | 256 | struct udevice *parent; |
developer | 2186c98 | 2018-11-15 10:07:54 +0800 | [diff] [blame] | 257 | void __iomem *base; |
| 258 | const struct mtk_clk_tree *tree; |
| 259 | const struct mtk_gate *gates; |
| 260 | }; |
| 261 | |
| 262 | extern const struct clk_ops mtk_clk_apmixedsys_ops; |
| 263 | extern const struct clk_ops mtk_clk_topckgen_ops; |
developer | ad5b075 | 2022-09-09 20:00:04 +0800 | [diff] [blame] | 264 | extern const struct clk_ops mtk_clk_infrasys_ops; |
developer | 2186c98 | 2018-11-15 10:07:54 +0800 | [diff] [blame] | 265 | extern const struct clk_ops mtk_clk_gate_ops; |
| 266 | |
| 267 | int mtk_common_clk_init(struct udevice *dev, |
| 268 | const struct mtk_clk_tree *tree); |
| 269 | int mtk_common_clk_gate_init(struct udevice *dev, |
| 270 | const struct mtk_clk_tree *tree, |
| 271 | const struct mtk_gate *gates); |
| 272 | |
| 273 | #endif /* __DRV_CLK_MTK_H */ |