summaryrefslogtreecommitdiff
path: root/src/soc/qualcomm/common/clock.c
blob: 09cd95c88a82de97158114df385ed73aeba854da (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/* SPDX-License-Identifier: GPL-2.0-only */

#include <assert.h>
#include <commonlib/helpers.h>
#include <delay.h>
#include <device/mmio.h>
#include <soc/clock.h>
#include <timer.h>
#include <types.h>

/* Clock Branch Operations */
static bool clock_is_off(u32 *cbcr_addr)
{
	return (read32(cbcr_addr) & CLK_CTL_OFF_BMSK);
}

enum cb_err clock_enable_vote(void *cbcr_addr, void *vote_addr,
				uint32_t vote_bit)
{
	int count = 100;

	setbits32(vote_addr, BIT(vote_bit));

	/* Ensure clock is enabled */
	while (count-- > 0) {
		if (!clock_is_off(cbcr_addr))
			return CB_SUCCESS;
		udelay(1);
	}
	printk(BIOS_ERR, "ERROR: Failed to enable clock, register val: 0x%x\n",
			read32(cbcr_addr));
	return CB_ERR;
}

enum cb_err clock_enable(void *cbcr_addr)
{
	int count = 100;

	/* Set clock enable bit */
	setbits32(cbcr_addr, BIT(CLK_CTL_EN_SHFT));

	/* Ensure clock is enabled */
	while (count-- > 0) {
		if (!clock_is_off(cbcr_addr))
			return CB_SUCCESS;
		udelay(1);
	}
	printk(BIOS_ERR, "ERROR: Failed to enable clock, register val: 0x%x\n",
			read32(cbcr_addr));
	return CB_ERR;
}

/* Clock Block Reset Operations */
void clock_reset_bcr(void *bcr_addr, bool assert)
{
	if (assert)
		setbits32(bcr_addr, BIT(CLK_CTL_BCR_BLK_SHFT));
	else
		clrbits32(bcr_addr, BIT(CLK_CTL_BCR_BLK_SHFT));
}

/* Clock GDSC Operations */
enum cb_err enable_and_poll_gdsc_status(void *gdscr_addr)
{
	if (read32(gdscr_addr) & CLK_CTL_OFF_BMSK)
		return CB_SUCCESS;

	clrbits32(gdscr_addr, BIT(GDSC_ENABLE_BIT));

	/* Ensure gdsc is enabled */
	if (!wait_us(100, (read32(gdscr_addr) & CLK_CTL_OFF_BMSK)))
		return CB_ERR;

	return CB_SUCCESS;
}

/* Clock Root clock Generator with MND Operations */
static void clock_configure_mnd(struct clock_rcg *clk, uint32_t m, uint32_t n,
				uint32_t d_2)
{
	struct clock_rcg_mnd *mnd = (struct clock_rcg_mnd *)clk;

	setbits32(&clk->rcg_cfg,
			RCG_MODE_DUAL_EDGE << CLK_CTL_CFG_MODE_SHFT);

	write32(&mnd->m, m & CLK_CTL_RCG_MND_BMSK);
	write32(&mnd->n, ~(n-m) & CLK_CTL_RCG_MND_BMSK);
	write32(&mnd->d_2, ~(d_2) & CLK_CTL_RCG_MND_BMSK);
}

/* Clock Root clock Generator Operations */
enum cb_err clock_configure(struct clock_rcg *clk,
			struct clock_freq_config *clk_cfg, uint32_t hz,
			uint32_t num_perfs)
{
	uint32_t reg_val, idx;

	for (idx = 0; idx < num_perfs; idx++)
		if (hz <= clk_cfg[idx].hz)
			break;

	reg_val = (clk_cfg[idx].src << CLK_CTL_CFG_SRC_SEL_SHFT) |
			(clk_cfg[idx].div << CLK_CTL_CFG_SRC_DIV_SHFT);

	/* Set clock config */
	write32(&clk->rcg_cfg, reg_val);

	if (clk_cfg[idx].m != 0)
		clock_configure_mnd(clk, clk_cfg[idx].m, clk_cfg[idx].n,
				clk_cfg[idx].d_2);

	/* Commit config to RCG */
	setbits32(&clk->rcg_cmd, BIT(CLK_CTL_CMD_UPDATE_SHFT));

	return CB_SUCCESS;
}

/* Clock Root clock Generator with DFS Operations */
void clock_configure_dfsr_table(int qup, struct clock_freq_config *clk_cfg,
		uint32_t num_perfs)
{
	struct qupv3_clock *qup_clk;
	unsigned int idx, s = qup % QUP_WRAP1_S0;
	uint32_t reg_val;

	qup_clk = qup < QUP_WRAP1_S0 ?
				&gcc->qup_wrap0_s[s] : &gcc->qup_wrap1_s[s];

	clrsetbits32(&qup_clk->dfsr_clk.cmd_dfsr,
			BIT(CLK_CTL_CMD_RCG_SW_CTL_SHFT),
			BIT(CLK_CTL_CMD_DFSR_SHFT));

	for (idx = 0; idx < num_perfs; idx++) {
		reg_val = (clk_cfg[idx].src << CLK_CTL_CFG_SRC_SEL_SHFT) |
			(clk_cfg[idx].div << CLK_CTL_CFG_SRC_DIV_SHFT);

		write32(&qup_clk->dfsr_clk.perf_dfsr[idx], reg_val);

		if (clk_cfg[idx].m == 0)
			continue;

		setbits32(&qup_clk->dfsr_clk.perf_dfsr[idx],
				RCG_MODE_DUAL_EDGE << CLK_CTL_CFG_MODE_SHFT);

		reg_val = clk_cfg[idx].m & CLK_CTL_RCG_MND_BMSK;
		write32(&qup_clk->dfsr_clk.perf_m_dfsr[idx], reg_val);

		reg_val = ~(clk_cfg[idx].n - clk_cfg[idx].m)
				& CLK_CTL_RCG_MND_BMSK;
		write32(&qup_clk->dfsr_clk.perf_n_dfsr[idx], reg_val);

		reg_val = ~(clk_cfg[idx].d_2) & CLK_CTL_RCG_MND_BMSK;
		write32(&qup_clk->dfsr_clk.perf_d_dfsr[idx], reg_val);
	}
}

/* General Purpose PLL configuration and enable Operations */
enum cb_err clock_configure_enable_gpll(struct alpha_pll_reg_val_config *cfg,
					bool enable, int br_enable)
{
	if (cfg->l_val)
		write32(cfg->reg_l, cfg->l_val);

	if (cfg->cal_l_val)
		write32(cfg->reg_cal_l, cfg->cal_l_val);

	if (cfg->alpha_val)
		write32(cfg->reg_alpha, cfg->alpha_val);

	if (cfg->user_ctl_val)
		write32(cfg->reg_user_ctl, cfg->user_ctl_val);

	if (cfg->user_ctl_hi_val)
		write32(cfg->reg_user_ctl_hi, cfg->user_ctl_hi_val);

	if (cfg->user_ctl_hi1_val)
		write32(cfg->reg_user_ctl_hi1, cfg->user_ctl_hi1_val);

	if (cfg->config_ctl_val)
		write32(cfg->reg_config_ctl, cfg->config_ctl_val);

	if (cfg->config_ctl_hi_val)
		write32(cfg->reg_config_ctl_hi, cfg->config_ctl_hi_val);

	if (cfg->config_ctl_hi1_val)
		write32(cfg->reg_config_ctl_hi1, cfg->config_ctl_hi1_val);

	if (cfg->fsm_enable)
		setbits32(cfg->reg_mode, BIT(PLL_FSM_EN_SHFT));

	if (enable) {
		setbits32(cfg->reg_opmode, BIT(PLL_STANDBY_MODE));

		/*
		* H/W requires a 1us delay between placing PLL in STANDBY and
		* de-asserting the reset.
		*/
		udelay(1);
		setbits32(cfg->reg_mode, BIT(PLL_RESET_N_SHFT));

		/*
		* H/W requires a 10us delay between de-asserting the reset and
		* enabling the PLL branch bit.
		*/
		udelay(10);
		setbits32(cfg->reg_apcs_pll_br_en, BIT(br_enable));

		/* Wait for Lock Detection */
		if (!wait_us(100, read32(cfg->reg_mode) & PLL_LOCK_DET_BMSK)) {
			printk(BIOS_ERR, "ERROR: PLL did not lock!\n");
			return CB_ERR;
		}
	}

	return CB_SUCCESS;
}

enum cb_err agera_pll_enable(struct alpha_pll_reg_val_config *cfg)
{
	setbits32(cfg->reg_mode, BIT(PLL_BYPASSNL_SHFT));

	/*
	* H/W requires a 5us delay between disabling the bypass and
	* de-asserting the reset.
	*/
	udelay(5);
	setbits32(cfg->reg_mode, BIT(PLL_RESET_SHFT));

	if (!wait_us(100, read32(cfg->reg_mode) & PLL_LOCK_DET_BMSK)) {
		printk(BIOS_ERR, "ERROR: CPU PLL did not lock!\n");
		return CB_ERR;
	}

	setbits32(cfg->reg_mode, BIT(PLL_OUTCTRL_SHFT));

	return CB_SUCCESS;
}

enum cb_err zonda_pll_enable(struct alpha_pll_reg_val_config *cfg)
{
	setbits32(cfg->reg_mode, BIT(PLL_BYPASSNL_SHFT));

	/*
	* H/W requires a 1us delay between disabling the bypass and
	* de-asserting the reset.
	*/
	udelay(1);
	setbits32(cfg->reg_mode, BIT(PLL_RESET_SHFT));
	setbits32(cfg->reg_opmode, PLL_RUN_MODE);

	if (!wait_us(100, read32(cfg->reg_mode) & PLL_LOCK_DET_BMSK)) {
		printk(BIOS_ERR, "ERROR: CPU PLL did not lock!\n");
		return CB_ERR;
	}

	setbits32(cfg->reg_user_ctl, PLL_USERCTL_BMSK);
	setbits32(cfg->reg_mode, BIT(PLL_OUTCTRL_SHFT));

	return CB_SUCCESS;
}

/* Bring subsystem out of RESET */
void clock_reset_subsystem(u32 *misc, u32 shft)
{
	clrbits32(misc, BIT(shft));
}