xref: /openbmc/linux/drivers/gpu/drm/nouveau/nvkm/subdev/clk/gk20a.h (revision d0034a7a4ac7fae708146ac0059b9c47a1543f0d)
142d6e167SAlexandre Courbot /*
242d6e167SAlexandre Courbot  * Copyright (c) 2016, NVIDIA CORPORATION. All rights reserved.
342d6e167SAlexandre Courbot  *
442d6e167SAlexandre Courbot  * Permission is hereby granted, free of charge, to any person obtaining a
542d6e167SAlexandre Courbot  * copy of this software and associated documentation files (the "Software"),
642d6e167SAlexandre Courbot  * to deal in the Software without restriction, including without limitation
742d6e167SAlexandre Courbot  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
842d6e167SAlexandre Courbot  * and/or sell copies of the Software, and to permit persons to whom the
942d6e167SAlexandre Courbot  * Software is furnished to do so, subject to the following conditions:
1042d6e167SAlexandre Courbot  *
1142d6e167SAlexandre Courbot  * The above copyright notice and this permission notice shall be included in
1242d6e167SAlexandre Courbot  * all copies or substantial portions of the Software.
1342d6e167SAlexandre Courbot  *
1442d6e167SAlexandre Courbot  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1542d6e167SAlexandre Courbot  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1642d6e167SAlexandre Courbot  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
1742d6e167SAlexandre Courbot  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
1842d6e167SAlexandre Courbot  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
1942d6e167SAlexandre Courbot  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
2042d6e167SAlexandre Courbot  * DEALINGS IN THE SOFTWARE.
2142d6e167SAlexandre Courbot  *
2242d6e167SAlexandre Courbot  */
2342d6e167SAlexandre Courbot 
2442d6e167SAlexandre Courbot #ifndef __NVKM_CLK_GK20A_H__
2542d6e167SAlexandre Courbot #define __NVKM_CLK_GK20A_H__
2642d6e167SAlexandre Courbot 
2722b6c9e8SAlexandre Courbot #define KHZ (1000)
2822b6c9e8SAlexandre Courbot #define MHZ (KHZ * 1000)
2922b6c9e8SAlexandre Courbot 
3022b6c9e8SAlexandre Courbot #define MASK(w)	((1 << (w)) - 1)
3122b6c9e8SAlexandre Courbot 
3242d6e167SAlexandre Courbot #define GK20A_CLK_GPC_MDIV 1000
3342d6e167SAlexandre Courbot 
3442d6e167SAlexandre Courbot #define SYS_GPCPLL_CFG_BASE	0x00137000
3522b6c9e8SAlexandre Courbot #define GPCPLL_CFG		(SYS_GPCPLL_CFG_BASE + 0)
3622b6c9e8SAlexandre Courbot #define GPCPLL_CFG_ENABLE	BIT(0)
3722b6c9e8SAlexandre Courbot #define GPCPLL_CFG_IDDQ		BIT(1)
3822b6c9e8SAlexandre Courbot #define GPCPLL_CFG_LOCK_DET_OFF	BIT(4)
3922b6c9e8SAlexandre Courbot #define GPCPLL_CFG_LOCK		BIT(17)
4022b6c9e8SAlexandre Courbot 
4122b6c9e8SAlexandre Courbot #define GPCPLL_CFG2		(SYS_GPCPLL_CFG_BASE + 0xc)
4222b6c9e8SAlexandre Courbot #define GPCPLL_CFG2_SETUP2_SHIFT	16
4322b6c9e8SAlexandre Courbot #define GPCPLL_CFG2_PLL_STEPA_SHIFT	24
4422b6c9e8SAlexandre Courbot 
4522b6c9e8SAlexandre Courbot #define GPCPLL_CFG3			(SYS_GPCPLL_CFG_BASE + 0x18)
4622b6c9e8SAlexandre Courbot #define GPCPLL_CFG3_VCO_CTRL_SHIFT		0
4722b6c9e8SAlexandre Courbot #define GPCPLL_CFG3_VCO_CTRL_WIDTH		9
4822b6c9e8SAlexandre Courbot #define GPCPLL_CFG3_VCO_CTRL_MASK		\
4922b6c9e8SAlexandre Courbot 	(MASK(GPCPLL_CFG3_VCO_CTRL_WIDTH) << GPCPLL_CFG3_VCO_CTRL_SHIFT)
5022b6c9e8SAlexandre Courbot #define GPCPLL_CFG3_PLL_STEPB_SHIFT		16
5122b6c9e8SAlexandre Courbot #define GPCPLL_CFG3_PLL_STEPB_WIDTH		8
5222b6c9e8SAlexandre Courbot 
5322b6c9e8SAlexandre Courbot #define GPCPLL_COEFF		(SYS_GPCPLL_CFG_BASE + 4)
5422b6c9e8SAlexandre Courbot #define GPCPLL_COEFF_M_SHIFT	0
5522b6c9e8SAlexandre Courbot #define GPCPLL_COEFF_M_WIDTH	8
5622b6c9e8SAlexandre Courbot #define GPCPLL_COEFF_N_SHIFT	8
5722b6c9e8SAlexandre Courbot #define GPCPLL_COEFF_N_WIDTH	8
5822b6c9e8SAlexandre Courbot #define GPCPLL_COEFF_N_MASK	\
5922b6c9e8SAlexandre Courbot 	(MASK(GPCPLL_COEFF_N_WIDTH) << GPCPLL_COEFF_N_SHIFT)
6022b6c9e8SAlexandre Courbot #define GPCPLL_COEFF_P_SHIFT	16
6122b6c9e8SAlexandre Courbot #define GPCPLL_COEFF_P_WIDTH	6
6222b6c9e8SAlexandre Courbot 
6322b6c9e8SAlexandre Courbot #define GPCPLL_NDIV_SLOWDOWN			(SYS_GPCPLL_CFG_BASE + 0x1c)
6422b6c9e8SAlexandre Courbot #define GPCPLL_NDIV_SLOWDOWN_NDIV_LO_SHIFT	0
6522b6c9e8SAlexandre Courbot #define GPCPLL_NDIV_SLOWDOWN_NDIV_MID_SHIFT	8
6622b6c9e8SAlexandre Courbot #define GPCPLL_NDIV_SLOWDOWN_STEP_SIZE_LO2MID_SHIFT	16
6722b6c9e8SAlexandre Courbot #define GPCPLL_NDIV_SLOWDOWN_SLOWDOWN_USING_PLL_SHIFT	22
6822b6c9e8SAlexandre Courbot #define GPCPLL_NDIV_SLOWDOWN_EN_DYNRAMP_SHIFT	31
6922b6c9e8SAlexandre Courbot 
7022b6c9e8SAlexandre Courbot #define GPC_BCAST_GPCPLL_CFG_BASE		0x00132800
7122b6c9e8SAlexandre Courbot #define GPC_BCAST_NDIV_SLOWDOWN_DEBUG	(GPC_BCAST_GPCPLL_CFG_BASE + 0xa0)
7222b6c9e8SAlexandre Courbot #define GPC_BCAST_NDIV_SLOWDOWN_DEBUG_PLL_DYNRAMP_DONE_SYNCED_SHIFT	24
7322b6c9e8SAlexandre Courbot #define GPC_BCAST_NDIV_SLOWDOWN_DEBUG_PLL_DYNRAMP_DONE_SYNCED_MASK \
7422b6c9e8SAlexandre Courbot 	(0x1 << GPC_BCAST_NDIV_SLOWDOWN_DEBUG_PLL_DYNRAMP_DONE_SYNCED_SHIFT)
7522b6c9e8SAlexandre Courbot 
7622b6c9e8SAlexandre Courbot #define SEL_VCO				(SYS_GPCPLL_CFG_BASE + 0x100)
7722b6c9e8SAlexandre Courbot #define SEL_VCO_GPC2CLK_OUT_SHIFT	0
7822b6c9e8SAlexandre Courbot 
7922b6c9e8SAlexandre Courbot #define GPC2CLK_OUT			(SYS_GPCPLL_CFG_BASE + 0x250)
8022b6c9e8SAlexandre Courbot #define GPC2CLK_OUT_SDIV14_INDIV4_WIDTH	1
8122b6c9e8SAlexandre Courbot #define GPC2CLK_OUT_SDIV14_INDIV4_SHIFT	31
8222b6c9e8SAlexandre Courbot #define GPC2CLK_OUT_SDIV14_INDIV4_MODE	1
8322b6c9e8SAlexandre Courbot #define GPC2CLK_OUT_VCODIV_WIDTH	6
8422b6c9e8SAlexandre Courbot #define GPC2CLK_OUT_VCODIV_SHIFT	8
8522b6c9e8SAlexandre Courbot #define GPC2CLK_OUT_VCODIV1		0
8622b6c9e8SAlexandre Courbot #define GPC2CLK_OUT_VCODIV2		2
8722b6c9e8SAlexandre Courbot #define GPC2CLK_OUT_VCODIV_MASK		(MASK(GPC2CLK_OUT_VCODIV_WIDTH) << \
8822b6c9e8SAlexandre Courbot 					GPC2CLK_OUT_VCODIV_SHIFT)
8922b6c9e8SAlexandre Courbot #define GPC2CLK_OUT_BYPDIV_WIDTH	6
9022b6c9e8SAlexandre Courbot #define GPC2CLK_OUT_BYPDIV_SHIFT	0
9122b6c9e8SAlexandre Courbot #define GPC2CLK_OUT_BYPDIV31		0x3c
9222b6c9e8SAlexandre Courbot #define GPC2CLK_OUT_INIT_MASK	((MASK(GPC2CLK_OUT_SDIV14_INDIV4_WIDTH) << \
9322b6c9e8SAlexandre Courbot 		GPC2CLK_OUT_SDIV14_INDIV4_SHIFT)\
9422b6c9e8SAlexandre Courbot 		| (MASK(GPC2CLK_OUT_VCODIV_WIDTH) << GPC2CLK_OUT_VCODIV_SHIFT)\
9522b6c9e8SAlexandre Courbot 		| (MASK(GPC2CLK_OUT_BYPDIV_WIDTH) << GPC2CLK_OUT_BYPDIV_SHIFT))
9622b6c9e8SAlexandre Courbot #define GPC2CLK_OUT_INIT_VAL	((GPC2CLK_OUT_SDIV14_INDIV4_MODE << \
9722b6c9e8SAlexandre Courbot 		GPC2CLK_OUT_SDIV14_INDIV4_SHIFT) \
9822b6c9e8SAlexandre Courbot 		| (GPC2CLK_OUT_VCODIV1 << GPC2CLK_OUT_VCODIV_SHIFT) \
9922b6c9e8SAlexandre Courbot 		| (GPC2CLK_OUT_BYPDIV31 << GPC2CLK_OUT_BYPDIV_SHIFT))
10042d6e167SAlexandre Courbot 
10142d6e167SAlexandre Courbot /* All frequencies in Khz */
10242d6e167SAlexandre Courbot struct gk20a_clk_pllg_params {
10342d6e167SAlexandre Courbot 	u32 min_vco, max_vco;
10442d6e167SAlexandre Courbot 	u32 min_u, max_u;
10542d6e167SAlexandre Courbot 	u32 min_m, max_m;
10642d6e167SAlexandre Courbot 	u32 min_n, max_n;
10742d6e167SAlexandre Courbot 	u32 min_pl, max_pl;
10842d6e167SAlexandre Courbot };
10942d6e167SAlexandre Courbot 
11042d6e167SAlexandre Courbot struct gk20a_pll {
11142d6e167SAlexandre Courbot 	u32 m;
11242d6e167SAlexandre Courbot 	u32 n;
11342d6e167SAlexandre Courbot 	u32 pl;
11442d6e167SAlexandre Courbot };
11542d6e167SAlexandre Courbot 
11642d6e167SAlexandre Courbot struct gk20a_clk {
11742d6e167SAlexandre Courbot 	struct nvkm_clk base;
11842d6e167SAlexandre Courbot 	const struct gk20a_clk_pllg_params *params;
11942d6e167SAlexandre Courbot 	struct gk20a_pll pll;
12042d6e167SAlexandre Courbot 	u32 parent_rate;
12142d6e167SAlexandre Courbot 
12242d6e167SAlexandre Courbot 	u32 (*div_to_pl)(u32);
12342d6e167SAlexandre Courbot 	u32 (*pl_to_div)(u32);
12442d6e167SAlexandre Courbot };
12542d6e167SAlexandre Courbot #define gk20a_clk(p) container_of((p), struct gk20a_clk, base)
12642d6e167SAlexandre Courbot 
12722b6c9e8SAlexandre Courbot u32 gk20a_pllg_calc_rate(struct gk20a_clk *, struct gk20a_pll *);
12822b6c9e8SAlexandre Courbot int gk20a_pllg_calc_mnp(struct gk20a_clk *, unsigned long, struct gk20a_pll *);
12922b6c9e8SAlexandre Courbot void gk20a_pllg_read_mnp(struct gk20a_clk *, struct gk20a_pll *);
13022b6c9e8SAlexandre Courbot void gk20a_pllg_write_mnp(struct gk20a_clk *, const struct gk20a_pll *);
13122b6c9e8SAlexandre Courbot 
13222b6c9e8SAlexandre Courbot static inline bool
gk20a_pllg_is_enabled(struct gk20a_clk * clk)13322b6c9e8SAlexandre Courbot gk20a_pllg_is_enabled(struct gk20a_clk *clk)
13422b6c9e8SAlexandre Courbot {
13522b6c9e8SAlexandre Courbot 	struct nvkm_device *device = clk->base.subdev.device;
13622b6c9e8SAlexandre Courbot 	u32 val;
13722b6c9e8SAlexandre Courbot 
13822b6c9e8SAlexandre Courbot 	val = nvkm_rd32(device, GPCPLL_CFG);
13922b6c9e8SAlexandre Courbot 	return val & GPCPLL_CFG_ENABLE;
14022b6c9e8SAlexandre Courbot }
14122b6c9e8SAlexandre Courbot 
14222b6c9e8SAlexandre Courbot static inline u32
gk20a_pllg_n_lo(struct gk20a_clk * clk,struct gk20a_pll * pll)14322b6c9e8SAlexandre Courbot gk20a_pllg_n_lo(struct gk20a_clk *clk, struct gk20a_pll *pll)
14422b6c9e8SAlexandre Courbot {
14522b6c9e8SAlexandre Courbot 	return DIV_ROUND_UP(pll->m * clk->params->min_vco,
14622b6c9e8SAlexandre Courbot 			    clk->parent_rate / KHZ);
14722b6c9e8SAlexandre Courbot }
14822b6c9e8SAlexandre Courbot 
149*98fd7f83SBen Skeggs int gk20a_clk_ctor(struct nvkm_device *, enum nvkm_subdev_type, int, const struct nvkm_clk_func *,
15042d6e167SAlexandre Courbot 		   const struct gk20a_clk_pllg_params *, struct gk20a_clk *);
15142d6e167SAlexandre Courbot void gk20a_clk_fini(struct nvkm_clk *);
15242d6e167SAlexandre Courbot int gk20a_clk_read(struct nvkm_clk *, enum nv_clk_src);
15342d6e167SAlexandre Courbot int gk20a_clk_calc(struct nvkm_clk *, struct nvkm_cstate *);
15442d6e167SAlexandre Courbot int gk20a_clk_prog(struct nvkm_clk *);
15542d6e167SAlexandre Courbot void gk20a_clk_tidy(struct nvkm_clk *);
15642d6e167SAlexandre Courbot 
157f5f1b06eSAlexandre Courbot int gk20a_clk_setup_slide(struct gk20a_clk *);
158f5f1b06eSAlexandre Courbot 
15942d6e167SAlexandre Courbot #endif
160