blob: 2449674f20daccd22a4e694a5814e9f96efa65c2 [file] [log] [blame]
jinkun.hong503d1212014-07-31 14:50:49 +08001/*
2 * This file is part of the coreboot project.
3 *
4 * Copyright 2014 Rockchip Inc.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20#include <assert.h>
21#include <stdlib.h>
22#include <arch/io.h>
23#include <stdint.h>
24#include <console/console.h>
25#include <delay.h>
26#include "clock.h"
27#include "grf.h"
28#include "addressmap.h"
huang lin82ba4d02014-08-16 10:49:32 +080029#include "soc.h"
jinkun.hong503d1212014-07-31 14:50:49 +080030
31struct pll_div {
32 u32 nr;
33 u32 nf;
34 u32 no;
35};
36
37struct rk3288_cru_reg {
38 u32 cru_apll_con[4];
39 u32 cru_dpll_con[4];
40 u32 cru_cpll_con[4];
41 u32 cru_gpll_con[4];
42 u32 cru_npll_con[4];
43 u32 cru_mode_con;
44 u32 reserved0[3];
45 u32 cru_clksel_con[43];
46 u32 reserved1[21];
47 u32 cru_clkgate_con[19];
48 u32 reserved2;
49 u32 cru_glb_srst_fst_value;
50 u32 cru_glb_srst_snd_value;
51 u32 cru_softrst_con[12];
52 u32 cru_misc_con;
53 u32 cru_glb_cnt_th;
54 u32 cru_glb_rst_con;
55 u32 reserved3;
56 u32 cru_glb_rst_st;
57 u32 reserved4;
58 u32 cru_sdmmc_con[2];
59 u32 cru_sdio0_con[2];
60 u32 cru_sdio1_con[2];
61 u32 cru_emmc_con[2];
62};
63check_member(rk3288_cru_reg, cru_emmc_con[1], 0x021c);
64
65static struct rk3288_cru_reg * const cru_ptr = (void *)CRU_BASE;
66
huang lin630c86d2014-08-26 17:28:46 +080067#define PLL_DIVISORS(hz, _nr, _no) {\
68 .nr = _nr, .nf = (u32)((u64)hz * _nr * _no / 24000000), .no = _no};\
69 _Static_assert(((u64)hz * _nr * _no / 24000000) * 24000000 /\
70 (_nr * _no) == hz,\
71 #hz "Hz cannot be hit with PLL divisors in " __FILE__);
72
Jinkun Hongc33ce352014-08-28 09:37:22 -070073/* apll = 816MHz, gpll = 594MHz, cpll = 384MHz */
huang lin630c86d2014-08-26 17:28:46 +080074static const struct pll_div apll_init_cfg = PLL_DIVISORS(APLL_HZ, 1, 2);
75static const struct pll_div gpll_init_cfg = PLL_DIVISORS(GPLL_HZ, 2, 4);
76static const struct pll_div cpll_init_cfg = PLL_DIVISORS(CPLL_HZ, 2, 4);
jinkun.hong503d1212014-07-31 14:50:49 +080077
78/*******************PLL CON0 BITS***************************/
79#define PLL_OD_MSK (0x0F)
80
81#define PLL_NR_MSK (0x3F << 8)
82#define PLL_NR_SHIFT (8)
83
84/*******************PLL CON1 BITS***************************/
85#define PLL_NF_MSK (0x1FFF)
86
87/*******************PLL CON2 BITS***************************/
88#define PLL_BWADJ_MSK (0x0FFF)
89
90/*******************PLL CON3 BITS***************************/
91#define PLL_RESET_MSK (1 << 5)
92#define PLL_RESET (1 << 5)
93#define PLL_RESET_RESUME (0 << 5)
94
95/*******************CLKSEL0 BITS***************************/
96/* core clk pll sel: amr or general */
97#define CORE_SEL_PLL_MSK (1 << 15)
98#define CORE_SEL_APLL (0 << 15)
99#define CORE_SEL_GPLL (1 << 15)
100
101/* a12 core clock div: clk_core = clk_src / (div_con + 1) */
102#define A12_DIV_SHIFT (8)
103#define A12_DIV_MSK (0x1F << 8)
104
105/* mp core axi clock div: clk = clk_src / (div_con + 1) */
106#define MP_DIV_SHIFT (4)
107#define MP_DIV_MSK (0xF << 4)
108
109/* m0 core axi clock div: clk = clk_src / (div_con + 1) */
110#define M0_DIV_MSK (0xF)
111
112/*******************CLKSEL10 BITS***************************/
113/* peripheral bus clk pll sel: codec or general */
114#define PERI_SEL_PLL_MSK (1 << 15)
115#define PERI_SEL_CPLL (0 << 15)
116#define PERI_SEL_GPLL (1 << 15)
117
118/* peripheral bus pclk div:
119 * aclk_bus: pclk_bus = 1:1 or 2:1 or 4:1 or 8:1
120 */
121#define PERI_PCLK_DIV_SHIFT (12)
122#define PERI_PCLK_DIV_MSK (0x7 << 12)
123
124/* peripheral bus hclk div:
125 * aclk_bus: hclk_bus = 1:1 or 2:1 or 4:1
126 */
127#define PERI_HCLK_DIV_SHIFT (8)
128#define PERI_HCLK_DIV_MSK (0x3 << 8)
129
130/* peripheral bus aclk div:
131 * aclk_periph =
132 * periph_clk_src / (peri_aclk_div_con + 1)
133 */
huang linbbcffd92014-09-27 12:02:27 +0800134#define PERI_ACLK_DIV_SHIFT (0x0)
jinkun.hong503d1212014-07-31 14:50:49 +0800135#define PERI_ACLK_DIV_MSK (0x1F)
136
137/*******************CLKSEL37 BITS***************************/
138#define L2_DIV_MSK (0x7)
139
140#define ATCLK_DIV_MSK (0x1F << 4)
141#define ATCLK_DIV_SHIFT (4)
142
143#define PCLK_DBG_DIV_MSK (0x1F << 9)
144#define PCLK_DBG_DIV_SHIFT (9)
145
146#define APLL_MODE_MSK (0x3)
147#define APLL_MODE_SLOW (0)
148#define APLL_MODE_NORM (1)
149
150#define DPLL_MODE_MSK (0x3 << 4)
151#define DPLL_MODE_SLOW (0 << 4)
152#define DPLL_MODE_NORM (1 << 4)
153
154#define CPLL_MODE_MSK (0x3 << 8)
155#define CPLL_MODE_SLOW (0 << 8)
156#define CPLL_MODE_NORM (1 << 8)
157
158#define GPLL_MODE_MSK (0x3 << 12)
159#define GPLL_MODE_SLOW (0 << 12)
160#define GPLL_MODE_NORM (1 << 12)
161
162#define SOCSTS_DPLL_LOCK (1 << 5)
163#define SOCSTS_APLL_LOCK (1 << 6)
164#define SOCSTS_CPLL_LOCK (1 << 7)
165#define SOCSTS_GPLL_LOCK (1 << 8)
166
167static int rkclk_set_pll(u32 *pll_con, const struct pll_div *pll_div_cfg)
168{
169 /* enter rest */
huang lin630c86d2014-08-26 17:28:46 +0800170 writel(RK_SETBITS(PLL_RESET_MSK), &pll_con[3]);
jinkun.hong503d1212014-07-31 14:50:49 +0800171
huang lin630c86d2014-08-26 17:28:46 +0800172 writel(RK_CLRSETBITS(PLL_NR_MSK, (pll_div_cfg->nr - 1) << PLL_NR_SHIFT)
173 | RK_CLRSETBITS(PLL_OD_MSK, (pll_div_cfg->no - 1)), &pll_con[0]);
jinkun.hong503d1212014-07-31 14:50:49 +0800174
huang lin630c86d2014-08-26 17:28:46 +0800175 writel(RK_CLRSETBITS(PLL_NF_MSK, (pll_div_cfg->nf - 1)),
jinkun.hong503d1212014-07-31 14:50:49 +0800176 &pll_con[1]);
177
huang lin630c86d2014-08-26 17:28:46 +0800178 writel(RK_CLRSETBITS(PLL_BWADJ_MSK, ((pll_div_cfg->nf >> 1) - 1)),
jinkun.hong503d1212014-07-31 14:50:49 +0800179 &pll_con[2]);
180
181 udelay(10);
182
183 /* return form rest */
huang lin630c86d2014-08-26 17:28:46 +0800184 writel(RK_CLRBITS(PLL_RESET_MSK), &pll_con[3]);
jinkun.hong503d1212014-07-31 14:50:49 +0800185
186 return 0;
187}
188
189void rkclk_init(void)
190{
191 /* pll enter slow-mode */
huang lin630c86d2014-08-26 17:28:46 +0800192 writel(RK_CLRSETBITS(APLL_MODE_MSK, APLL_MODE_SLOW)
193 | RK_CLRSETBITS(GPLL_MODE_MSK, GPLL_MODE_SLOW)
Jinkun Hongc33ce352014-08-28 09:37:22 -0700194 | RK_CLRSETBITS(CPLL_MODE_MSK, CPLL_MODE_SLOW),
jinkun.hong503d1212014-07-31 14:50:49 +0800195 &cru_ptr->cru_mode_con);
196
197 /* init pll */
198 rkclk_set_pll(&cru_ptr->cru_apll_con[0], &apll_init_cfg);
199 rkclk_set_pll(&cru_ptr->cru_gpll_con[0], &gpll_init_cfg);
200 rkclk_set_pll(&cru_ptr->cru_cpll_con[0], &cpll_init_cfg);
jinkun.hong503d1212014-07-31 14:50:49 +0800201
202 /* waiting for pll lock */
203 while (1) {
204 if ((readl(&rk3288_grf->soc_status[1])
205 & (SOCSTS_APLL_LOCK | SOCSTS_CPLL_LOCK
Jinkun Hongc33ce352014-08-28 09:37:22 -0700206 | SOCSTS_GPLL_LOCK))
jinkun.hong503d1212014-07-31 14:50:49 +0800207 == (SOCSTS_APLL_LOCK | SOCSTS_CPLL_LOCK
Jinkun Hongc33ce352014-08-28 09:37:22 -0700208 | SOCSTS_GPLL_LOCK))
jinkun.hong503d1212014-07-31 14:50:49 +0800209 break;
210 udelay(1);
211 }
212
213 /*
214 * core clock pll source selection and
215 * set up dependent divisors for MPAXI/M0AXI and ARM clocks.
216 * core clock select apll, apll clk = 816MHz
217 * arm clk = 816MHz, mpclk = 204MHz, m0clk = 408MHz
218 */
huang lin630c86d2014-08-26 17:28:46 +0800219 writel(RK_CLRBITS(CORE_SEL_PLL_MSK)
220 | RK_CLRSETBITS(A12_DIV_MSK, 0 << A12_DIV_SHIFT)
221 | RK_CLRSETBITS(MP_DIV_MSK, 3 << MP_DIV_SHIFT)
222 | RK_CLRSETBITS(M0_DIV_MSK, 1 << 0),
jinkun.hong503d1212014-07-31 14:50:49 +0800223 &cru_ptr->cru_clksel_con[0]);
224
225 /*
226 * set up dependent divisors for L2RAM/ATCLK and PCLK clocks.
227 * l2ramclk = 408MHz, atclk = 204MHz, pclk_dbg = 204MHz
228 */
huang lin630c86d2014-08-26 17:28:46 +0800229 writel(RK_CLRSETBITS(L2_DIV_MSK, 1 << 0)
230 | RK_CLRSETBITS(ATCLK_DIV_MSK, (3 << ATCLK_DIV_SHIFT))
231 | RK_CLRSETBITS(PCLK_DBG_DIV_MSK, (3 << PCLK_DBG_DIV_SHIFT)),
jinkun.hong503d1212014-07-31 14:50:49 +0800232 &cru_ptr->cru_clksel_con[37]);
233
234 /*
235 * peri clock pll source selection and
236 * set up dependent divisors for PCLK/HCLK and ACLK clocks.
237 * peri clock select gpll, gpll clk = 594MHz
238 * aclk = 148.5MHz, hclk = 148.5Mhz, pclk = 74.25MHz
239 */
huang lin630c86d2014-08-26 17:28:46 +0800240 writel(RK_SETBITS(PERI_SEL_PLL_MSK)
241 | RK_CLRSETBITS(PERI_PCLK_DIV_MSK, 1 << PERI_PCLK_DIV_SHIFT)
242 | RK_CLRSETBITS(PERI_HCLK_DIV_MSK, 0 << PERI_HCLK_DIV_SHIFT)
243 | RK_CLRSETBITS(PERI_ACLK_DIV_MSK, 3 << 0),
jinkun.hong503d1212014-07-31 14:50:49 +0800244 &cru_ptr->cru_clksel_con[10]);
245
246 /* PLL enter normal-mode */
huang lin630c86d2014-08-26 17:28:46 +0800247 writel(RK_CLRSETBITS(APLL_MODE_MSK, APLL_MODE_NORM)
248 | RK_CLRSETBITS(GPLL_MODE_MSK, GPLL_MODE_NORM)
Jinkun Hongc33ce352014-08-28 09:37:22 -0700249 | RK_CLRSETBITS(CPLL_MODE_MSK, CPLL_MODE_NORM),
jinkun.hong503d1212014-07-31 14:50:49 +0800250 &cru_ptr->cru_mode_con);
251
252}
253
Jinkun Hongc33ce352014-08-28 09:37:22 -0700254void rkclk_configure_ddr(unsigned int hz)
255{
256 struct pll_div dpll_cfg;
257
258 if (hz <= 150000000) {
259 dpll_cfg.nr = 3;
260 dpll_cfg.no = 8;
261 } else if (hz <= 540000000) {
262 dpll_cfg.nr = 6;
263 dpll_cfg.no = 4;
264 } else {
265 dpll_cfg.nr = 1;
266 dpll_cfg.no = 1;
267 }
268
269 dpll_cfg.nf = (hz / 1000 * dpll_cfg.nr * dpll_cfg.no) / 24000;
270 assert(dpll_cfg.nf < 4096
271 && hz == dpll_cfg.nf * 24000 / (dpll_cfg.nr * dpll_cfg.no)
272 * 1000);
273 /* pll enter slow-mode */
274 writel(RK_CLRSETBITS(DPLL_MODE_MSK, DPLL_MODE_SLOW),
275 &cru_ptr->cru_mode_con);
276
277 rkclk_set_pll(&cru_ptr->cru_dpll_con[0], &dpll_cfg);
278
279 /* waiting for pll lock */
280 while (1) {
281 if (readl(&rk3288_grf->soc_status[1]) & SOCSTS_DPLL_LOCK)
282 break;
283 udelay(1);
284 }
285
286 /* PLL enter normal-mode */
287 writel(RK_CLRSETBITS(DPLL_MODE_MSK, DPLL_MODE_NORM),
288 &cru_ptr->cru_mode_con);
289}
290
291void rkclk_ddr_reset(u32 ch, u32 ctl, u32 phy)
292{
293 u32 phy_ctl_srstn_shift = 4 + 5 * ch;
294 u32 ctl_psrstn_shift = 3 + 5 * ch;
295 u32 ctl_srstn_shift = 2 + 5 * ch;
296 u32 phy_psrstn_shift = 1 + 5 * ch;
297 u32 phy_srstn_shift = 5 * ch;
298
299 writel(RK_CLRSETBITS(1 << phy_ctl_srstn_shift,
300 phy << phy_ctl_srstn_shift)
301 | RK_CLRSETBITS(1 << ctl_psrstn_shift, ctl << ctl_psrstn_shift)
302 | RK_CLRSETBITS(1 << ctl_srstn_shift, ctl << ctl_srstn_shift)
303 | RK_CLRSETBITS(1 << phy_psrstn_shift, phy << phy_psrstn_shift)
304 | RK_CLRSETBITS(1 << phy_srstn_shift, phy << phy_srstn_shift),
305 &cru_ptr->cru_softrst_con[10]);
306}
307
308void rkclk_ddr_phy_ctl_reset(u32 ch, u32 n)
309{
310 u32 phy_ctl_srstn_shift = 4 + 5 * ch;
311
312 writel(RK_CLRSETBITS(1 << phy_ctl_srstn_shift,
313 n << phy_ctl_srstn_shift),
314 &cru_ptr->cru_softrst_con[10]);
315}
316
huang lin630c86d2014-08-26 17:28:46 +0800317void rkclk_configure_spi(unsigned int bus, unsigned int hz)
jinkun.hong503d1212014-07-31 14:50:49 +0800318{
huang lin630c86d2014-08-26 17:28:46 +0800319 int src_clk_div = GPLL_HZ / hz;
jinkun.hong503d1212014-07-31 14:50:49 +0800320
huang lin630c86d2014-08-26 17:28:46 +0800321 assert((src_clk_div - 1 < 127) && (src_clk_div * hz == GPLL_HZ));
322
323 switch (bus) { /*select gpll as spi src clk, and set div*/
324 case 0:
325 writel(RK_CLRSETBITS(1 << 7 | 0x1f << 0, 1 << 7
326 | (src_clk_div - 1) << 0),
327 &cru_ptr->cru_clksel_con[25]);
328 break;
329 case 1:
330 writel(RK_CLRSETBITS(1 << 15 | 0x1f << 8, 1 << 15
331 | (src_clk_div - 1) << 8),
332 &cru_ptr->cru_clksel_con[25]);
333 break;
334 case 2:
335 writel(RK_CLRSETBITS(1 << 7 | 0x1f << 0, 1 << 7
336 | (src_clk_div - 1) << 0),
337 &cru_ptr->cru_clksel_con[39]);
338 break;
339 default:
340 printk(BIOS_ERR, "do not support this spi bus\n");
341 }
jinkun.hong503d1212014-07-31 14:50:49 +0800342}
huang lin739df1b2014-08-27 17:07:42 +0800343
344static u32 clk_gcd(u32 a, u32 b)
345{
346 while (b != 0) {
347 int r = b;
348 b = a % b;
349 a = r;
350 }
351 return a;
352}
353
354void rkclk_configure_i2s(unsigned int hz)
355{
356 int n, d;
357 int v;
358
359 /* i2s source clock: gpll
360 i2s0_outclk_sel: clk_i2s
361 i2s0_clk_sel: divider ouput from fraction
362 i2s0_pll_div_con: 0*/
363 writel(RK_CLRSETBITS(1 << 15 | 1 << 12 | 3 << 8 | 0x7f << 0 ,
364 1 << 15 | 0 << 12 | 1 << 8 | 0 << 0),
365 &cru_ptr->cru_clksel_con[4]);
366
367 /* set frac divider */
368 v = clk_gcd(GPLL_HZ, hz);
369 n = (GPLL_HZ / v) & (0xffff);
370 d = (hz / v) & (0xffff);
371 assert(hz == GPLL_HZ / n * d);
372 writel(d << 16 | n, &cru_ptr->cru_clksel_con[8]);
373}