xref: /openbmc/linux/drivers/clk/qcom/gpucc-sc7180.c (revision 15e3ae36)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (c) 2019, The Linux Foundation. All rights reserved.
4  */
5 
6 #include <linux/clk-provider.h>
7 #include <linux/module.h>
8 #include <linux/platform_device.h>
9 #include <linux/regmap.h>
10 
11 #include <dt-bindings/clock/qcom,gpucc-sc7180.h>
12 
13 #include "clk-alpha-pll.h"
14 #include "clk-branch.h"
15 #include "clk-rcg.h"
16 #include "clk-regmap.h"
17 #include "common.h"
18 #include "gdsc.h"
19 
20 #define CX_GMU_CBCR_SLEEP_MASK		0xF
21 #define CX_GMU_CBCR_SLEEP_SHIFT		4
22 #define CX_GMU_CBCR_WAKE_MASK		0xF
23 #define CX_GMU_CBCR_WAKE_SHIFT		8
24 #define CLK_DIS_WAIT_SHIFT		12
25 #define CLK_DIS_WAIT_MASK		(0xf << CLK_DIS_WAIT_SHIFT)
26 
27 enum {
28 	P_BI_TCXO,
29 	P_CORE_BI_PLL_TEST_SE,
30 	P_GPLL0_OUT_MAIN,
31 	P_GPLL0_OUT_MAIN_DIV,
32 	P_GPU_CC_PLL1_OUT_EVEN,
33 	P_GPU_CC_PLL1_OUT_MAIN,
34 	P_GPU_CC_PLL1_OUT_ODD,
35 };
36 
37 static const struct pll_vco fabia_vco[] = {
38 	{ 249600000, 2000000000, 0 },
39 };
40 
41 static struct clk_alpha_pll gpu_cc_pll1 = {
42 	.offset = 0x100,
43 	.vco_table = fabia_vco,
44 	.num_vco = ARRAY_SIZE(fabia_vco),
45 	.regs = clk_alpha_pll_regs[CLK_ALPHA_PLL_TYPE_FABIA],
46 	.clkr = {
47 		.hw.init = &(struct clk_init_data){
48 			.name = "gpu_cc_pll1",
49 			.parent_data =  &(const struct clk_parent_data){
50 				.fw_name = "bi_tcxo",
51 			},
52 			.num_parents = 1,
53 			.ops = &clk_alpha_pll_fabia_ops,
54 		},
55 	},
56 };
57 
58 static const struct parent_map gpu_cc_parent_map_0[] = {
59 	{ P_BI_TCXO, 0 },
60 	{ P_GPU_CC_PLL1_OUT_MAIN, 3 },
61 	{ P_GPLL0_OUT_MAIN, 5 },
62 	{ P_GPLL0_OUT_MAIN_DIV, 6 },
63 };
64 
65 static const struct clk_parent_data gpu_cc_parent_data_0[] = {
66 	{ .fw_name = "bi_tcxo" },
67 	{ .hw = &gpu_cc_pll1.clkr.hw },
68 	{ .fw_name = "gcc_gpu_gpll0_clk_src" },
69 	{ .fw_name = "gcc_gpu_gpll0_div_clk_src" },
70 };
71 
72 static const struct freq_tbl ftbl_gpu_cc_gmu_clk_src[] = {
73 	F(19200000, P_BI_TCXO, 1, 0, 0),
74 	F(200000000, P_GPLL0_OUT_MAIN_DIV, 1.5, 0, 0),
75 	{ }
76 };
77 
78 static struct clk_rcg2 gpu_cc_gmu_clk_src = {
79 	.cmd_rcgr = 0x1120,
80 	.mnd_width = 0,
81 	.hid_width = 5,
82 	.parent_map = gpu_cc_parent_map_0,
83 	.freq_tbl = ftbl_gpu_cc_gmu_clk_src,
84 	.clkr.hw.init = &(struct clk_init_data){
85 		.name = "gpu_cc_gmu_clk_src",
86 		.parent_data = gpu_cc_parent_data_0,
87 		.num_parents = ARRAY_SIZE(gpu_cc_parent_data_0),
88 		.flags = CLK_SET_RATE_PARENT,
89 		.ops = &clk_rcg2_shared_ops,
90 	},
91 };
92 
93 static struct clk_branch gpu_cc_crc_ahb_clk = {
94 	.halt_reg = 0x107c,
95 	.halt_check = BRANCH_HALT_DELAY,
96 	.clkr = {
97 		.enable_reg = 0x107c,
98 		.enable_mask = BIT(0),
99 		.hw.init = &(struct clk_init_data){
100 			.name = "gpu_cc_crc_ahb_clk",
101 			.ops = &clk_branch2_ops,
102 		},
103 	},
104 };
105 
106 static struct clk_branch gpu_cc_cx_gmu_clk = {
107 	.halt_reg = 0x1098,
108 	.halt_check = BRANCH_HALT,
109 	.clkr = {
110 		.enable_reg = 0x1098,
111 		.enable_mask = BIT(0),
112 		.hw.init = &(struct clk_init_data){
113 			.name = "gpu_cc_cx_gmu_clk",
114 			.parent_data =  &(const struct clk_parent_data){
115 				.hw = &gpu_cc_gmu_clk_src.clkr.hw,
116 			},
117 			.num_parents = 1,
118 			.flags = CLK_SET_RATE_PARENT,
119 			.ops = &clk_branch2_ops,
120 		},
121 	},
122 };
123 
124 static struct clk_branch gpu_cc_cx_snoc_dvm_clk = {
125 	.halt_reg = 0x108c,
126 	.halt_check = BRANCH_HALT_DELAY,
127 	.clkr = {
128 		.enable_reg = 0x108c,
129 		.enable_mask = BIT(0),
130 		.hw.init = &(struct clk_init_data){
131 			.name = "gpu_cc_cx_snoc_dvm_clk",
132 			.ops = &clk_branch2_ops,
133 		},
134 	},
135 };
136 
137 static struct clk_branch gpu_cc_cxo_aon_clk = {
138 	.halt_reg = 0x1004,
139 	.halt_check = BRANCH_HALT_DELAY,
140 	.clkr = {
141 		.enable_reg = 0x1004,
142 		.enable_mask = BIT(0),
143 		.hw.init = &(struct clk_init_data){
144 			.name = "gpu_cc_cxo_aon_clk",
145 			.ops = &clk_branch2_ops,
146 		},
147 	},
148 };
149 
150 static struct clk_branch gpu_cc_cxo_clk = {
151 	.halt_reg = 0x109c,
152 	.halt_check = BRANCH_HALT,
153 	.clkr = {
154 		.enable_reg = 0x109c,
155 		.enable_mask = BIT(0),
156 		.hw.init = &(struct clk_init_data){
157 			.name = "gpu_cc_cxo_clk",
158 			.ops = &clk_branch2_ops,
159 		},
160 	},
161 };
162 
163 static struct gdsc cx_gdsc = {
164 	.gdscr = 0x106c,
165 	.gds_hw_ctrl = 0x1540,
166 	.pd = {
167 		.name = "cx_gdsc",
168 	},
169 	.pwrsts = PWRSTS_OFF_ON,
170 	.flags = VOTABLE,
171 };
172 
173 /*
174  * On SC7180 the GPU GX domain is *almost* entirely controlled by the GMU
175  * running in the CX domain so the CPU doesn't need to know anything about the
176  * GX domain EXCEPT....
177  *
178  * Hardware constraints dictate that the GX be powered down before the CX. If
179  * the GMU crashes it could leave the GX on. In order to successfully bring back
180  * the device the CPU needs to disable the GX headswitch. There being no sane
181  * way to reach in and touch that register from deep inside the GPU driver we
182  * need to set up the infrastructure to be able to ensure that the GPU can
183  * ensure that the GX is off during this super special case. We do this by
184  * defining a GX gdsc with a dummy enable function and a "default" disable
185  * function.
186  *
187  * This allows us to attach with genpd_dev_pm_attach_by_name() in the GPU
188  * driver. During power up, nothing will happen from the CPU (and the GMU will
189  * power up normally but during power down this will ensure that the GX domain
190  * is *really* off - this gives us a semi standard way of doing what we need.
191  */
192 static int gx_gdsc_enable(struct generic_pm_domain *domain)
193 {
194 	/* Do nothing but give genpd the impression that we were successful */
195 	return 0;
196 }
197 
198 static struct gdsc gx_gdsc = {
199 	.gdscr = 0x100c,
200 	.clamp_io_ctrl = 0x1508,
201 	.pd = {
202 		.name = "gx_gdsc",
203 		.power_on = gx_gdsc_enable,
204 	},
205 	.pwrsts = PWRSTS_OFF_ON,
206 	.flags = CLAMP_IO,
207 };
208 
209 static struct gdsc *gpu_cc_sc7180_gdscs[] = {
210 	[CX_GDSC] = &cx_gdsc,
211 	[GX_GDSC] = &gx_gdsc,
212 };
213 
214 static struct clk_regmap *gpu_cc_sc7180_clocks[] = {
215 	[GPU_CC_CXO_CLK] = &gpu_cc_cxo_clk.clkr,
216 	[GPU_CC_CRC_AHB_CLK] = &gpu_cc_crc_ahb_clk.clkr,
217 	[GPU_CC_CX_GMU_CLK] = &gpu_cc_cx_gmu_clk.clkr,
218 	[GPU_CC_CX_SNOC_DVM_CLK] = &gpu_cc_cx_snoc_dvm_clk.clkr,
219 	[GPU_CC_CXO_AON_CLK] = &gpu_cc_cxo_aon_clk.clkr,
220 	[GPU_CC_GMU_CLK_SRC] = &gpu_cc_gmu_clk_src.clkr,
221 	[GPU_CC_PLL1] = &gpu_cc_pll1.clkr,
222 };
223 
224 static const struct regmap_config gpu_cc_sc7180_regmap_config = {
225 	.reg_bits =	32,
226 	.reg_stride =	4,
227 	.val_bits =	32,
228 	.max_register =	0x8008,
229 	.fast_io =	true,
230 };
231 
232 static const struct qcom_cc_desc gpu_cc_sc7180_desc = {
233 	.config = &gpu_cc_sc7180_regmap_config,
234 	.clks = gpu_cc_sc7180_clocks,
235 	.num_clks = ARRAY_SIZE(gpu_cc_sc7180_clocks),
236 	.gdscs = gpu_cc_sc7180_gdscs,
237 	.num_gdscs = ARRAY_SIZE(gpu_cc_sc7180_gdscs),
238 };
239 
240 static const struct of_device_id gpu_cc_sc7180_match_table[] = {
241 	{ .compatible = "qcom,sc7180-gpucc" },
242 	{ }
243 };
244 MODULE_DEVICE_TABLE(of, gpu_cc_sc7180_match_table);
245 
246 static int gpu_cc_sc7180_probe(struct platform_device *pdev)
247 {
248 	struct regmap *regmap;
249 	struct alpha_pll_config gpu_cc_pll_config = {};
250 	unsigned int value, mask;
251 
252 	regmap = qcom_cc_map(pdev, &gpu_cc_sc7180_desc);
253 	if (IS_ERR(regmap))
254 		return PTR_ERR(regmap);
255 
256 	/* 360MHz Configuration */
257 	gpu_cc_pll_config.l = 0x12;
258 	gpu_cc_pll_config.alpha = 0xc000;
259 	gpu_cc_pll_config.config_ctl_val = 0x20485699;
260 	gpu_cc_pll_config.config_ctl_hi_val = 0x00002067;
261 	gpu_cc_pll_config.user_ctl_val = 0x00000001;
262 	gpu_cc_pll_config.user_ctl_hi_val = 0x00004805;
263 	gpu_cc_pll_config.test_ctl_hi_val = 0x40000000;
264 
265 	clk_fabia_pll_configure(&gpu_cc_pll1, regmap, &gpu_cc_pll_config);
266 
267 	/* Recommended WAKEUP/SLEEP settings for the gpu_cc_cx_gmu_clk */
268 	mask = CX_GMU_CBCR_WAKE_MASK << CX_GMU_CBCR_WAKE_SHIFT;
269 	mask |= CX_GMU_CBCR_SLEEP_MASK << CX_GMU_CBCR_SLEEP_SHIFT;
270 	value = 0xF << CX_GMU_CBCR_WAKE_SHIFT | 0xF << CX_GMU_CBCR_SLEEP_SHIFT;
271 	regmap_update_bits(regmap, 0x1098, mask, value);
272 
273 	/* Configure clk_dis_wait for gpu_cx_gdsc */
274 	regmap_update_bits(regmap, 0x106c, CLK_DIS_WAIT_MASK,
275 						8 << CLK_DIS_WAIT_SHIFT);
276 
277 	return qcom_cc_really_probe(pdev, &gpu_cc_sc7180_desc, regmap);
278 }
279 
280 static struct platform_driver gpu_cc_sc7180_driver = {
281 	.probe = gpu_cc_sc7180_probe,
282 	.driver = {
283 		.name = "sc7180-gpucc",
284 		.of_match_table = gpu_cc_sc7180_match_table,
285 	},
286 };
287 
288 static int __init gpu_cc_sc7180_init(void)
289 {
290 	return platform_driver_register(&gpu_cc_sc7180_driver);
291 }
292 subsys_initcall(gpu_cc_sc7180_init);
293 
294 static void __exit gpu_cc_sc7180_exit(void)
295 {
296 	platform_driver_unregister(&gpu_cc_sc7180_driver);
297 }
298 module_exit(gpu_cc_sc7180_exit);
299 
300 MODULE_DESCRIPTION("QTI GPU_CC SC7180 Driver");
301 MODULE_LICENSE("GPL v2");
302