blob: 0ce83e9b24375820016397c3c5491dd9d44edb4b [file] [log] [blame]
Neil Armstrong07d2f752024-04-04 18:46:39 +02001// SPDX-License-Identifier: BSD-3-Clause
2/*
3 * Clock drivers for Qualcomm sm8650
4 *
5 * (C) Copyright 2024 Linaro Ltd.
6 */
7
8#include <clk-uclass.h>
9#include <dm.h>
10#include <linux/delay.h>
11#include <errno.h>
12#include <asm/io.h>
13#include <linux/bug.h>
14#include <linux/bitops.h>
15#include <dt-bindings/clock/qcom,sm8650-gcc.h>
16#include <dt-bindings/clock/qcom,sm8650-tcsr.h>
17
18#include "clock-qcom.h"
19
20/* On-board TCXO, TOFIX get from DT */
21#define TCXO_RATE 38400000
22
23/* bi_tcxo_div2 divided after RPMh output */
24#define TCXO_DIV2_RATE (TCXO_RATE / 2)
25
26static const struct freq_tbl ftbl_gcc_qupv3_wrap1_s3_clk_src[] = {
27 F(7372800, CFG_CLK_SRC_GPLL0_EVEN, 1, 384, 15625),
28 F(14745600, CFG_CLK_SRC_GPLL0_EVEN, 1, 768, 15625),
29 F(19200000, CFG_CLK_SRC_CXO, 1, 0, 0),
30 F(29491200, CFG_CLK_SRC_GPLL0_EVEN, 1, 1536, 15625),
31 F(32000000, CFG_CLK_SRC_GPLL0_EVEN, 1, 8, 75),
32 F(48000000, CFG_CLK_SRC_GPLL0_EVEN, 1, 4, 25),
33 F(64000000, CFG_CLK_SRC_GPLL0_EVEN, 1, 16, 75),
34 F(75000000, CFG_CLK_SRC_GPLL0_EVEN, 4, 0, 0),
35 F(80000000, CFG_CLK_SRC_GPLL0_EVEN, 1, 4, 15),
36 F(96000000, CFG_CLK_SRC_GPLL0_EVEN, 1, 8, 25),
37 F(100000000, CFG_CLK_SRC_GPLL0, 6, 0, 0),
38 { }
39};
40
41static const struct freq_tbl ftbl_gcc_sdcc2_apps_clk_src[] = {
42 F(400000, CFG_CLK_SRC_CXO, 12, 1, 4),
43 F(25000000, CFG_CLK_SRC_GPLL0_EVEN, 12, 0, 0),
44 F(100000000, CFG_CLK_SRC_GPLL0_EVEN, 3, 0, 0),
45 /* TOFIX F(202000000, CFG_CLK_SRC_GPLL9, 4, 0, 0), */
46 { }
47};
48
49static const struct freq_tbl ftbl_gcc_usb30_prim_master_clk_src[] = {
50 F(66666667, CFG_CLK_SRC_GPLL0_EVEN, 4.5, 0, 0),
51 F(133333333, CFG_CLK_SRC_GPLL0, 4.5, 0, 0),
52 F(200000000, CFG_CLK_SRC_GPLL0, 3, 0, 0),
53 F(240000000, CFG_CLK_SRC_GPLL0, 2.5, 0, 0),
54 { }
55};
56
57static ulong sm8650_set_rate(struct clk *clk, ulong rate)
58{
59 struct msm_clk_priv *priv = dev_get_priv(clk->dev);
60 const struct freq_tbl *freq;
61
62 switch (clk->id) {
63 case GCC_QUPV3_WRAP2_S7_CLK: /* UART15 */
64 freq = qcom_find_freq(ftbl_gcc_qupv3_wrap1_s3_clk_src, rate);
65 clk_rcg_set_rate_mnd(priv->base, 0x1e898,
66 freq->pre_div, freq->m, freq->n, freq->src, 16);
67 return freq->freq;
68 case GCC_SDCC2_APPS_CLK:
69 freq = qcom_find_freq(ftbl_gcc_sdcc2_apps_clk_src, rate);
70 clk_rcg_set_rate_mnd(priv->base, 0x14018,
71 freq->pre_div, freq->m, freq->n, freq->src, 8);
72 return freq->freq;
73 case GCC_USB30_PRIM_MASTER_CLK:
74 freq = qcom_find_freq(ftbl_gcc_usb30_prim_master_clk_src, rate);
75 clk_rcg_set_rate_mnd(priv->base, 0x3902c,
76 freq->pre_div, freq->m, freq->n, freq->src, 8);
77 return freq->freq;
78 case GCC_USB30_PRIM_MOCK_UTMI_CLK:
79 clk_rcg_set_rate(priv->base, 0x39044, 0, 0);
80 return TCXO_DIV2_RATE;
81 case GCC_USB3_PRIM_PHY_AUX_CLK_SRC:
82 clk_rcg_set_rate(priv->base, 0x39070, 0, 0);
83 return TCXO_DIV2_RATE;
84 default:
85 return 0;
86 }
87}
88
89static const struct gate_clk sm8650_clks[] = {
90 GATE_CLK(GCC_AGGRE_NOC_PCIE_AXI_CLK, 0x52000, BIT(12)),
91 GATE_CLK(GCC_AGGRE_UFS_PHY_AXI_CLK, 0x770e4, BIT(0)),
92 GATE_CLK(GCC_AGGRE_UFS_PHY_AXI_HW_CTL_CLK, 0x770e4, BIT(1)),
93 GATE_CLK(GCC_AGGRE_USB3_PRIM_AXI_CLK, 0x3908c, BIT(0)),
94 GATE_CLK(GCC_CNOC_PCIE_SF_AXI_CLK, 0x52008, BIT(6)),
95 GATE_CLK(GCC_DDRSS_GPU_AXI_CLK, 0x71154, BIT(0)),
96 GATE_CLK(GCC_DDRSS_PCIE_SF_QTB_CLK, 0x52000, BIT(19)),
97 GATE_CLK(GCC_PCIE_0_AUX_CLK, 0x52008, BIT(3)),
98 GATE_CLK(GCC_PCIE_0_CFG_AHB_CLK, 0x52008, BIT(2)),
99 GATE_CLK(GCC_PCIE_0_MSTR_AXI_CLK, 0x52008, BIT(1)),
100 GATE_CLK(GCC_PCIE_0_PHY_RCHNG_CLK, 0x52000, BIT(22)),
101 GATE_CLK(GCC_PCIE_0_PIPE_CLK, 0x52008, BIT(4)),
102 GATE_CLK(GCC_PCIE_0_SLV_AXI_CLK, 0x52008, BIT(0)),
103 GATE_CLK(GCC_PCIE_0_SLV_Q2A_AXI_CLK, 0x52008, BIT(5)),
104 GATE_CLK(GCC_PCIE_1_AUX_CLK, 0x52000, BIT(29)),
105 GATE_CLK(GCC_PCIE_1_CFG_AHB_CLK, 0x52000, BIT(28)),
106 GATE_CLK(GCC_PCIE_1_MSTR_AXI_CLK, 0x52000, BIT(27)),
107 GATE_CLK(GCC_PCIE_1_PHY_AUX_CLK, 0x52000, BIT(24)),
108 GATE_CLK(GCC_PCIE_1_PHY_RCHNG_CLK, 0x52000, BIT(23)),
109 GATE_CLK(GCC_PCIE_1_PIPE_CLK, 0x52000, BIT(30)),
110 GATE_CLK(GCC_PCIE_1_SLV_AXI_CLK, 0x52000, BIT(26)),
111 GATE_CLK(GCC_PCIE_1_SLV_Q2A_AXI_CLK, 0x52000, BIT(25)),
112 GATE_CLK(GCC_QUPV3_I2C_CORE_CLK, 0x52008, BIT(8)),
113 GATE_CLK(GCC_QUPV3_I2C_S0_CLK, 0x52008, BIT(10)),
114 GATE_CLK(GCC_QUPV3_I2C_S1_CLK, 0x52008, BIT(11)),
115 GATE_CLK(GCC_QUPV3_I2C_S2_CLK, 0x52008, BIT(12)),
116 GATE_CLK(GCC_QUPV3_I2C_S3_CLK, 0x52008, BIT(13)),
117 GATE_CLK(GCC_QUPV3_I2C_S4_CLK, 0x52008, BIT(14)),
118 GATE_CLK(GCC_QUPV3_I2C_S5_CLK, 0x52008, BIT(15)),
119 GATE_CLK(GCC_QUPV3_I2C_S6_CLK, 0x52008, BIT(16)),
120 GATE_CLK(GCC_QUPV3_I2C_S7_CLK, 0x52008, BIT(17)),
121 GATE_CLK(GCC_QUPV3_I2C_S8_CLK, 0x52010, BIT(14)),
122 GATE_CLK(GCC_QUPV3_I2C_S9_CLK, 0x52010, BIT(15)),
123 GATE_CLK(GCC_QUPV3_I2C_S_AHB_CLK, 0x52008, BIT(7)),
124 GATE_CLK(GCC_QUPV3_WRAP1_CORE_2X_CLK, 0x52008, BIT(18)),
125 GATE_CLK(GCC_QUPV3_WRAP1_CORE_CLK, 0x52008, BIT(19)),
126 GATE_CLK(GCC_QUPV3_WRAP1_S0_CLK, 0x52008, BIT(22)),
127 GATE_CLK(GCC_QUPV3_WRAP1_S1_CLK, 0x52008, BIT(23)),
128 GATE_CLK(GCC_QUPV3_WRAP1_S2_CLK, 0x52008, BIT(24)),
129 GATE_CLK(GCC_QUPV3_WRAP1_S3_CLK, 0x52008, BIT(25)),
130 GATE_CLK(GCC_QUPV3_WRAP1_S4_CLK, 0x52008, BIT(26)),
131 GATE_CLK(GCC_QUPV3_WRAP1_S5_CLK, 0x52008, BIT(27)),
132 GATE_CLK(GCC_QUPV3_WRAP1_S6_CLK, 0x52008, BIT(28)),
133 GATE_CLK(GCC_QUPV3_WRAP1_S7_CLK, 0x52010, BIT(16)),
134 GATE_CLK(GCC_QUPV3_WRAP2_CORE_2X_CLK, 0x52010, BIT(3)),
135 GATE_CLK(GCC_QUPV3_WRAP2_CORE_CLK, 0x52010, BIT(0)),
136 GATE_CLK(GCC_QUPV3_WRAP2_S0_CLK, 0x52010, BIT(4)),
137 GATE_CLK(GCC_QUPV3_WRAP2_S1_CLK, 0x52010, BIT(5)),
138 GATE_CLK(GCC_QUPV3_WRAP2_S2_CLK, 0x52010, BIT(6)),
139 GATE_CLK(GCC_QUPV3_WRAP2_S3_CLK, 0x52010, BIT(7)),
140 GATE_CLK(GCC_QUPV3_WRAP2_S4_CLK, 0x52010, BIT(8)),
141 GATE_CLK(GCC_QUPV3_WRAP2_S5_CLK, 0x52010, BIT(9)),
142 GATE_CLK(GCC_QUPV3_WRAP2_S6_CLK, 0x52010, BIT(10)),
143 GATE_CLK(GCC_QUPV3_WRAP2_S7_CLK, 0x52010, BIT(17)),
144 GATE_CLK(GCC_QUPV3_WRAP_1_M_AHB_CLK, 0x52008, BIT(20)),
145 GATE_CLK(GCC_QUPV3_WRAP_1_S_AHB_CLK, 0x52008, BIT(21)),
146 GATE_CLK(GCC_QUPV3_WRAP_2_M_AHB_CLK, 0x52010, BIT(2)),
147 GATE_CLK(GCC_QUPV3_WRAP_2_S_AHB_CLK, 0x52010, BIT(1)),
148 GATE_CLK(GCC_SDCC2_AHB_CLK, 0x14010, BIT(0)),
149 GATE_CLK(GCC_SDCC2_APPS_CLK, 0x14004, BIT(0)),
150 GATE_CLK(GCC_UFS_PHY_AHB_CLK, 0x77024, BIT(0)),
151 GATE_CLK(GCC_UFS_PHY_AXI_CLK, 0x77018, BIT(0)),
152 GATE_CLK(GCC_UFS_PHY_AXI_HW_CTL_CLK, 0x77018, BIT(1)),
153 GATE_CLK(GCC_UFS_PHY_ICE_CORE_CLK, 0x77074, BIT(0)),
154 GATE_CLK(GCC_UFS_PHY_ICE_CORE_HW_CTL_CLK, 0x77074, BIT(1)),
155 GATE_CLK(GCC_UFS_PHY_PHY_AUX_CLK, 0x770b0, BIT(0)),
156 GATE_CLK(GCC_UFS_PHY_PHY_AUX_HW_CTL_CLK, 0x770b0, BIT(1)),
157 GATE_CLK(GCC_UFS_PHY_RX_SYMBOL_0_CLK, 0x7702c, BIT(0)),
158 GATE_CLK(GCC_UFS_PHY_RX_SYMBOL_1_CLK, 0x770cc, BIT(0)),
159 GATE_CLK(GCC_UFS_PHY_TX_SYMBOL_0_CLK, 0x77028, BIT(0)),
160 GATE_CLK(GCC_UFS_PHY_UNIPRO_CORE_CLK, 0x77068, BIT(0)),
161 GATE_CLK(GCC_UFS_PHY_UNIPRO_CORE_HW_CTL_CLK, 0x77068, BIT(1)),
162 GATE_CLK(GCC_USB30_PRIM_MASTER_CLK, 0x39018, BIT(0)),
163 GATE_CLK(GCC_USB30_PRIM_MOCK_UTMI_CLK, 0x39028, BIT(0)),
164 GATE_CLK(GCC_USB30_PRIM_SLEEP_CLK, 0x39024, BIT(0)),
165 GATE_CLK(GCC_USB3_PRIM_PHY_AUX_CLK, 0x39060, BIT(0)),
166 GATE_CLK(GCC_USB3_PRIM_PHY_COM_AUX_CLK, 0x39064, BIT(0)),
167 GATE_CLK(GCC_USB3_PRIM_PHY_PIPE_CLK, 0x39068, BIT(0)),
168};
169
170static int sm8650_enable(struct clk *clk)
171{
172 struct msm_clk_priv *priv = dev_get_priv(clk->dev);
173
174 switch (clk->id) {
175 case GCC_AGGRE_USB3_PRIM_AXI_CLK:
176 qcom_gate_clk_en(priv, GCC_USB30_PRIM_MASTER_CLK);
177 fallthrough;
178 case GCC_USB30_PRIM_MASTER_CLK:
179 qcom_gate_clk_en(priv, GCC_USB3_PRIM_PHY_AUX_CLK);
180 qcom_gate_clk_en(priv, GCC_USB3_PRIM_PHY_COM_AUX_CLK);
181 break;
182 }
183
184 qcom_gate_clk_en(priv, clk->id);
185
186 return 0;
187}
188
189static const struct qcom_reset_map sm8650_gcc_resets[] = {
190 [GCC_CAMERA_BCR] = { 0x26000 },
191 [GCC_DISPLAY_BCR] = { 0x27000 },
192 [GCC_GPU_BCR] = { 0x71000 },
193 [GCC_PCIE_0_BCR] = { 0x6b000 },
194 [GCC_PCIE_0_LINK_DOWN_BCR] = { 0x6c014 },
195 [GCC_PCIE_0_NOCSR_COM_PHY_BCR] = { 0x6c020 },
196 [GCC_PCIE_0_PHY_BCR] = { 0x6c01c },
197 [GCC_PCIE_0_PHY_NOCSR_COM_PHY_BCR] = { 0x6c028 },
198 [GCC_PCIE_1_BCR] = { 0x8d000 },
199 [GCC_PCIE_1_LINK_DOWN_BCR] = { 0x8e014 },
200 [GCC_PCIE_1_NOCSR_COM_PHY_BCR] = { 0x8e020 },
201 [GCC_PCIE_1_PHY_BCR] = { 0x8e01c },
202 [GCC_PCIE_1_PHY_NOCSR_COM_PHY_BCR] = { 0x8e024 },
203 [GCC_PCIE_PHY_BCR] = { 0x6f000 },
204 [GCC_PCIE_PHY_CFG_AHB_BCR] = { 0x6f00c },
205 [GCC_PCIE_PHY_COM_BCR] = { 0x6f010 },
206 [GCC_PDM_BCR] = { 0x33000 },
207 [GCC_QUPV3_WRAPPER_1_BCR] = { 0x18000 },
208 [GCC_QUPV3_WRAPPER_2_BCR] = { 0x1e000 },
209 [GCC_QUPV3_WRAPPER_3_BCR] = { 0x19000 },
210 [GCC_QUPV3_WRAPPER_I2C_BCR] = { 0x17000 },
211 [GCC_QUSB2PHY_PRIM_BCR] = { 0x12000 },
212 [GCC_QUSB2PHY_SEC_BCR] = { 0x12004 },
213 [GCC_SDCC2_BCR] = { 0x14000 },
214 [GCC_SDCC4_BCR] = { 0x16000 },
215 [GCC_UFS_PHY_BCR] = { 0x77000 },
216 [GCC_USB30_PRIM_BCR] = { 0x39000 },
217 [GCC_USB3_DP_PHY_PRIM_BCR] = { 0x50008 },
218 [GCC_USB3_DP_PHY_SEC_BCR] = { 0x50014 },
219 [GCC_USB3_PHY_PRIM_BCR] = { 0x50000 },
220 [GCC_USB3_PHY_SEC_BCR] = { 0x5000c },
221 [GCC_USB3PHY_PHY_PRIM_BCR] = { 0x50004 },
222 [GCC_USB3PHY_PHY_SEC_BCR] = { 0x50010 },
223 [GCC_VIDEO_AXI0_CLK_ARES] = { 0x32018, 2 },
224 [GCC_VIDEO_AXI1_CLK_ARES] = { 0x32024, 2 },
225 [GCC_VIDEO_BCR] = { 0x32000 },
226};
227
228static const struct qcom_power_map sm8650_gdscs[] = {
229 [PCIE_0_GDSC] = { 0x6b004 },
230 [PCIE_0_PHY_GDSC] = { 0x6c000 },
231 [PCIE_1_GDSC] = { 0x8d004 },
232 [PCIE_1_PHY_GDSC] = { 0x8e000 },
233 [UFS_PHY_GDSC] = { 0x77004 },
234 [UFS_MEM_PHY_GDSC] = { 0x9e000 },
235 [USB30_PRIM_GDSC] = { 0x39004 },
236 [USB3_PHY_GDSC] = { 0x50018 },
237};
238
239static struct msm_clk_data sm8650_gcc_data = {
240 .resets = sm8650_gcc_resets,
241 .num_resets = ARRAY_SIZE(sm8650_gcc_resets),
242 .clks = sm8650_clks,
243 .num_clks = ARRAY_SIZE(sm8650_clks),
244 .power_domains = sm8650_gdscs,
245 .num_power_domains = ARRAY_SIZE(sm8650_gdscs),
246
247 .enable = sm8650_enable,
248 .set_rate = sm8650_set_rate,
249};
250
251static const struct udevice_id gcc_sm8650_of_match[] = {
252 {
253 .compatible = "qcom,sm8650-gcc",
254 .data = (ulong)&sm8650_gcc_data,
255 },
256 { }
257};
258
259U_BOOT_DRIVER(gcc_sm8650) = {
260 .name = "gcc_sm8650",
261 .id = UCLASS_NOP,
262 .of_match = gcc_sm8650_of_match,
263 .bind = qcom_cc_bind,
264 .flags = DM_FLAG_PRE_RELOC | DM_FLAG_DEFAULT_PD_CTRL_OFF,
265};
266
267/* TCSRCC */
268
269static const struct gate_clk sm8650_tcsr_clks[] = {
270 GATE_CLK(TCSR_PCIE_0_CLKREF_EN, 0x31100, BIT(0)),
271 GATE_CLK(TCSR_PCIE_1_CLKREF_EN, 0x31114, BIT(0)),
272 GATE_CLK(TCSR_UFS_CLKREF_EN, 0x31110, BIT(0)),
273 GATE_CLK(TCSR_UFS_PAD_CLKREF_EN, 0x31104, BIT(0)),
274 GATE_CLK(TCSR_USB2_CLKREF_EN, 0x31118, BIT(0)),
275 GATE_CLK(TCSR_USB3_CLKREF_EN, 0x31108, BIT(0)),
276};
277
278static struct msm_clk_data sm8650_tcsrcc_data = {
279 .clks = sm8650_tcsr_clks,
280 .num_clks = ARRAY_SIZE(sm8650_tcsr_clks),
281};
282
283static int tcsrcc_sm8650_clk_enable(struct clk *clk)
284{
285 struct msm_clk_priv *priv = dev_get_priv(clk->dev);
286
287 qcom_gate_clk_en(priv, clk->id);
288
289 return 0;
290}
291
292static ulong tcsrcc_sm8650_clk_get_rate(struct clk *clk)
293{
294 return TCXO_RATE;
295}
296
297static int tcsrcc_sm8650_clk_probe(struct udevice *dev)
298{
299 struct msm_clk_data *data = (struct msm_clk_data *)dev_get_driver_data(dev);
300 struct msm_clk_priv *priv = dev_get_priv(dev);
301
302 priv->base = dev_read_addr(dev);
303 if (priv->base == FDT_ADDR_T_NONE)
304 return -EINVAL;
305
306 priv->data = data;
307
308 return 0;
309}
310
311static struct clk_ops tcsrcc_sm8650_clk_ops = {
312 .enable = tcsrcc_sm8650_clk_enable,
313 .get_rate = tcsrcc_sm8650_clk_get_rate,
314};
315
316static const struct udevice_id tcsrcc_sm8650_of_match[] = {
317 {
318 .compatible = "qcom,sm8650-tcsr",
319 .data = (ulong)&sm8650_tcsrcc_data,
320 },
321 { }
322};
323
324U_BOOT_DRIVER(tcsrcc_sm8650) = {
325 .name = "tcsrcc_sm8650",
326 .id = UCLASS_CLK,
327 .of_match = tcsrcc_sm8650_of_match,
328 .ops = &tcsrcc_sm8650_clk_ops,
329 .priv_auto = sizeof(struct msm_clk_priv),
330 .probe = tcsrcc_sm8650_clk_probe,
331 .flags = DM_FLAG_PRE_RELOC | DM_FLAG_DEFAULT_PD_CTRL_OFF,
332};