xref: /rk3399_rockchip-uboot/arch/arm/mach-sunxi/clock_sun9i.c (revision 48d2fc47c9feac54fdc0e2e0462f13b1a8689ece)
1*7962a8d5SPhilipp Tomsich 
2e6e505b9SAlexander Graf /*
3e6e505b9SAlexander Graf  * sun9i specific clock code
4e6e505b9SAlexander Graf  *
5e6e505b9SAlexander Graf  * (C) Copyright 2015 Hans de Goede <hdegoede@redhat.com>
6e6e505b9SAlexander Graf  *
7*7962a8d5SPhilipp Tomsich  * (C) Copyright 2016 Theobroma Systems Design und Consulting GmbH
8*7962a8d5SPhilipp Tomsich  *                    Philipp Tomsich <philipp.tomsich@theobroma-systems.com>
9*7962a8d5SPhilipp Tomsich  *
10e6e505b9SAlexander Graf  * SPDX-License-Identifier:	GPL-2.0+
11e6e505b9SAlexander Graf  */
12e6e505b9SAlexander Graf 
13e6e505b9SAlexander Graf #include <common.h>
14e6e505b9SAlexander Graf #include <asm/io.h>
15e6e505b9SAlexander Graf #include <asm/arch/clock.h>
16e6e505b9SAlexander Graf #include <asm/arch/prcm.h>
17e6e505b9SAlexander Graf #include <asm/arch/sys_proto.h>
18e6e505b9SAlexander Graf 
19*7962a8d5SPhilipp Tomsich 
20*7962a8d5SPhilipp Tomsich #ifdef CONFIG_SPL_BUILD
21*7962a8d5SPhilipp Tomsich 
clock_init_safe(void)22*7962a8d5SPhilipp Tomsich void clock_init_safe(void)
23*7962a8d5SPhilipp Tomsich {
24*7962a8d5SPhilipp Tomsich 	struct sunxi_ccm_reg * const ccm =
25*7962a8d5SPhilipp Tomsich 		(struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
26*7962a8d5SPhilipp Tomsich 
27*7962a8d5SPhilipp Tomsich 	/* Set up PLL12 (peripheral 1) */
28*7962a8d5SPhilipp Tomsich 	clock_set_pll12(1200000000);
29*7962a8d5SPhilipp Tomsich 
30*7962a8d5SPhilipp Tomsich 	/* Set up PLL1 (cluster 0) and PLL2 (cluster 1) */
31*7962a8d5SPhilipp Tomsich 	clock_set_pll1(408000000);
32*7962a8d5SPhilipp Tomsich 	clock_set_pll2(408000000);
33*7962a8d5SPhilipp Tomsich 
34*7962a8d5SPhilipp Tomsich 	/* Set up PLL4 (peripheral 0) */
35*7962a8d5SPhilipp Tomsich 	clock_set_pll4(960000000);
36*7962a8d5SPhilipp Tomsich 
37*7962a8d5SPhilipp Tomsich 	/* Set up dividers for AXI0 and APB0 on cluster 0: PLL1 / 2 = 204MHz */
38*7962a8d5SPhilipp Tomsich 	writel(C0_CFG_AXI0_CLK_DIV_RATIO(2) |
39*7962a8d5SPhilipp Tomsich 	       C0_CFG_APB0_CLK_DIV_RATIO(2), &ccm->c0_cfg);
40*7962a8d5SPhilipp Tomsich 
41*7962a8d5SPhilipp Tomsich 	/* AHB0: 120 MHz (PLL_PERIPH0 / 8) */
42*7962a8d5SPhilipp Tomsich 	writel(AHBx_SRC_PLL_PERIPH0 | AHBx_CLK_DIV_RATIO(8),
43*7962a8d5SPhilipp Tomsich 	       &ccm->ahb0_cfg);
44*7962a8d5SPhilipp Tomsich 	/* AHB1: 240 MHz (PLL_PERIPH0 / 4) */
45*7962a8d5SPhilipp Tomsich 	writel(AHBx_SRC_PLL_PERIPH0 | AHBx_CLK_DIV_RATIO(4),
46*7962a8d5SPhilipp Tomsich 	       &ccm->ahb1_cfg);
47*7962a8d5SPhilipp Tomsich 	/* AHB2: 120 MHz (PLL_PERIPH0 / 8) */
48*7962a8d5SPhilipp Tomsich 	writel(AHBx_SRC_PLL_PERIPH0 | AHBx_CLK_DIV_RATIO(8),
49*7962a8d5SPhilipp Tomsich 	       &ccm->ahb2_cfg);
50*7962a8d5SPhilipp Tomsich 	/* APB0: 120 MHz (PLL_PERIPH0 / 8) */
51*7962a8d5SPhilipp Tomsich 	writel(APB0_SRC_PLL_PERIPH0 | APB0_CLK_DIV_RATIO(8),
52*7962a8d5SPhilipp Tomsich 	       &ccm->apb0_cfg);
53*7962a8d5SPhilipp Tomsich 
54*7962a8d5SPhilipp Tomsich 	/* GTBUS: 400MHz (PERIPH0 div 3) */
55*7962a8d5SPhilipp Tomsich 	writel(GTBUS_SRC_PLL_PERIPH1 | GTBUS_CLK_DIV_RATIO(3),
56*7962a8d5SPhilipp Tomsich 	       &ccm->gtbus_cfg);
57*7962a8d5SPhilipp Tomsich 	/* CCI400: 480MHz (PERIPH1 div 2) */
58*7962a8d5SPhilipp Tomsich 	writel(CCI400_SRC_PLL_PERIPH0 | CCI400_CLK_DIV_RATIO(2),
59*7962a8d5SPhilipp Tomsich 	       &ccm->cci400_cfg);
60*7962a8d5SPhilipp Tomsich 
61*7962a8d5SPhilipp Tomsich 	/* Deassert DMA reset and open clock gating for DMA */
62*7962a8d5SPhilipp Tomsich 	setbits_le32(&ccm->ahb_reset1_cfg, (1 << 24));
63*7962a8d5SPhilipp Tomsich 	setbits_le32(&ccm->apb1_gate, (1 << 24));
64*7962a8d5SPhilipp Tomsich 
65*7962a8d5SPhilipp Tomsich 	/* set enable-bit in TSTAMP_CTRL_REG */
66*7962a8d5SPhilipp Tomsich 	writel(1, 0x01720000);
67*7962a8d5SPhilipp Tomsich }
68*7962a8d5SPhilipp Tomsich #endif
69*7962a8d5SPhilipp Tomsich 
clock_init_uart(void)70e6e505b9SAlexander Graf void clock_init_uart(void)
71e6e505b9SAlexander Graf {
72e6e505b9SAlexander Graf 	struct sunxi_ccm_reg *const ccm =
73e6e505b9SAlexander Graf 		(struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
74e6e505b9SAlexander Graf 
75e6e505b9SAlexander Graf 	/* open the clock for uart */
76e6e505b9SAlexander Graf 	setbits_le32(&ccm->apb1_gate,
77e6e505b9SAlexander Graf 		     CLK_GATE_OPEN << (APB1_GATE_UART_SHIFT +
78e6e505b9SAlexander Graf 				       CONFIG_CONS_INDEX - 1));
79e6e505b9SAlexander Graf 	/* deassert uart reset */
80e6e505b9SAlexander Graf 	setbits_le32(&ccm->apb1_reset_cfg,
81e6e505b9SAlexander Graf 		     1 << (APB1_RESET_UART_SHIFT +
82e6e505b9SAlexander Graf 			   CONFIG_CONS_INDEX - 1));
83e6e505b9SAlexander Graf }
84e6e505b9SAlexander Graf 
85*7962a8d5SPhilipp Tomsich #ifdef CONFIG_SPL_BUILD
clock_set_pll1(unsigned int clk)86*7962a8d5SPhilipp Tomsich void clock_set_pll1(unsigned int clk)
87*7962a8d5SPhilipp Tomsich {
88*7962a8d5SPhilipp Tomsich 	struct sunxi_ccm_reg * const ccm =
89*7962a8d5SPhilipp Tomsich 		(struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
90*7962a8d5SPhilipp Tomsich 	const int p = 0;
91*7962a8d5SPhilipp Tomsich 
92*7962a8d5SPhilipp Tomsich 	/* Switch cluster 0 to 24MHz clock while changing PLL1 */
93*7962a8d5SPhilipp Tomsich 	clrsetbits_le32(&ccm->cpu_clk_source, C0_CPUX_CLK_SRC_MASK,
94*7962a8d5SPhilipp Tomsich 			C0_CPUX_CLK_SRC_OSC24M);
95*7962a8d5SPhilipp Tomsich 
96*7962a8d5SPhilipp Tomsich 	writel(CCM_PLL1_CTRL_EN | CCM_PLL1_CTRL_P(p) |
97*7962a8d5SPhilipp Tomsich 	       CCM_PLL1_CLOCK_TIME_2 |
98*7962a8d5SPhilipp Tomsich 	       CCM_PLL1_CTRL_N(clk / 24000000),
99*7962a8d5SPhilipp Tomsich 	       &ccm->pll1_c0_cfg);
100*7962a8d5SPhilipp Tomsich 	/*
101*7962a8d5SPhilipp Tomsich 	 * Don't bother with the stable-time registers, as it doesn't
102*7962a8d5SPhilipp Tomsich 	 * wait until the PLL is stable.  Note, that even Allwinner
103*7962a8d5SPhilipp Tomsich 	 * just uses a delay loop (or rather the AVS timer) for this
104*7962a8d5SPhilipp Tomsich 	 * instead of the PLL_STABLE_STATUS register.
105*7962a8d5SPhilipp Tomsich 	 */
106*7962a8d5SPhilipp Tomsich 	sdelay(2000);
107*7962a8d5SPhilipp Tomsich 
108*7962a8d5SPhilipp Tomsich 	/* Switch cluster 0 back to PLL1 */
109*7962a8d5SPhilipp Tomsich 	clrsetbits_le32(&ccm->cpu_clk_source, C0_CPUX_CLK_SRC_MASK,
110*7962a8d5SPhilipp Tomsich 			C0_CPUX_CLK_SRC_PLL1);
111*7962a8d5SPhilipp Tomsich }
112*7962a8d5SPhilipp Tomsich 
clock_set_pll2(unsigned int clk)113*7962a8d5SPhilipp Tomsich void clock_set_pll2(unsigned int clk)
114*7962a8d5SPhilipp Tomsich {
115*7962a8d5SPhilipp Tomsich 	struct sunxi_ccm_reg * const ccm =
116*7962a8d5SPhilipp Tomsich 		(struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
117*7962a8d5SPhilipp Tomsich 	const int p = 0;
118*7962a8d5SPhilipp Tomsich 
119*7962a8d5SPhilipp Tomsich 	/* Switch cluster 1 to 24MHz clock while changing PLL2 */
120*7962a8d5SPhilipp Tomsich 	clrsetbits_le32(&ccm->cpu_clk_source, C1_CPUX_CLK_SRC_MASK,
121*7962a8d5SPhilipp Tomsich 			C1_CPUX_CLK_SRC_OSC24M);
122*7962a8d5SPhilipp Tomsich 
123*7962a8d5SPhilipp Tomsich 	writel(CCM_PLL2_CTRL_EN | CCM_PLL2_CTRL_P(p) |
124*7962a8d5SPhilipp Tomsich 	       CCM_PLL2_CLOCK_TIME_2 | CCM_PLL2_CTRL_N(clk / 24000000),
125*7962a8d5SPhilipp Tomsich 	       &ccm->pll2_c1_cfg);
126*7962a8d5SPhilipp Tomsich 
127*7962a8d5SPhilipp Tomsich 	sdelay(2000);
128*7962a8d5SPhilipp Tomsich 
129*7962a8d5SPhilipp Tomsich 	/* Switch cluster 1 back to PLL2 */
130*7962a8d5SPhilipp Tomsich 	clrsetbits_le32(&ccm->cpu_clk_source, C1_CPUX_CLK_SRC_MASK,
131*7962a8d5SPhilipp Tomsich 			C1_CPUX_CLK_SRC_PLL2);
132*7962a8d5SPhilipp Tomsich }
133*7962a8d5SPhilipp Tomsich 
clock_set_pll6(unsigned int clk)134*7962a8d5SPhilipp Tomsich void clock_set_pll6(unsigned int clk)
135*7962a8d5SPhilipp Tomsich {
136*7962a8d5SPhilipp Tomsich 	struct sunxi_ccm_reg * const ccm =
137*7962a8d5SPhilipp Tomsich 		(struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
138*7962a8d5SPhilipp Tomsich 	const int p = 0;
139*7962a8d5SPhilipp Tomsich 
140*7962a8d5SPhilipp Tomsich 	writel(CCM_PLL6_CTRL_EN | CCM_PLL6_CFG_UPDATE | CCM_PLL6_CTRL_P(p)
141*7962a8d5SPhilipp Tomsich 	       | CCM_PLL6_CTRL_N(clk / 24000000),
142*7962a8d5SPhilipp Tomsich 	       &ccm->pll6_ddr_cfg);
143*7962a8d5SPhilipp Tomsich 	do { } while (!(readl(&ccm->pll_stable_status) & PLL_DDR_STATUS));
144*7962a8d5SPhilipp Tomsich 
145*7962a8d5SPhilipp Tomsich 	sdelay(2000);
146*7962a8d5SPhilipp Tomsich }
147*7962a8d5SPhilipp Tomsich 
clock_set_pll12(unsigned int clk)148*7962a8d5SPhilipp Tomsich void clock_set_pll12(unsigned int clk)
149*7962a8d5SPhilipp Tomsich {
150*7962a8d5SPhilipp Tomsich 	struct sunxi_ccm_reg * const ccm =
151*7962a8d5SPhilipp Tomsich 		(struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
152*7962a8d5SPhilipp Tomsich 
153*7962a8d5SPhilipp Tomsich 	if (readl(&ccm->pll12_periph1_cfg) & CCM_PLL12_CTRL_EN)
154*7962a8d5SPhilipp Tomsich 		return;
155*7962a8d5SPhilipp Tomsich 
156*7962a8d5SPhilipp Tomsich 	writel(CCM_PLL12_CTRL_EN | CCM_PLL12_CTRL_N(clk / 24000000),
157*7962a8d5SPhilipp Tomsich 	       &ccm->pll12_periph1_cfg);
158*7962a8d5SPhilipp Tomsich 
159*7962a8d5SPhilipp Tomsich 	sdelay(2000);
160*7962a8d5SPhilipp Tomsich }
161*7962a8d5SPhilipp Tomsich 
162*7962a8d5SPhilipp Tomsich 
clock_set_pll4(unsigned int clk)163*7962a8d5SPhilipp Tomsich void clock_set_pll4(unsigned int clk)
164*7962a8d5SPhilipp Tomsich {
165*7962a8d5SPhilipp Tomsich 	struct sunxi_ccm_reg * const ccm =
166*7962a8d5SPhilipp Tomsich 		(struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
167*7962a8d5SPhilipp Tomsich 
168*7962a8d5SPhilipp Tomsich 	writel(CCM_PLL4_CTRL_EN | CCM_PLL4_CTRL_N(clk / 24000000),
169*7962a8d5SPhilipp Tomsich 	       &ccm->pll4_periph0_cfg);
170*7962a8d5SPhilipp Tomsich 
171*7962a8d5SPhilipp Tomsich 	sdelay(2000);
172*7962a8d5SPhilipp Tomsich }
173*7962a8d5SPhilipp Tomsich #endif
174*7962a8d5SPhilipp Tomsich 
clock_twi_onoff(int port,int state)175e6e505b9SAlexander Graf int clock_twi_onoff(int port, int state)
176e6e505b9SAlexander Graf {
177e6e505b9SAlexander Graf 	struct sunxi_ccm_reg *const ccm =
178e6e505b9SAlexander Graf 		(struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
179e6e505b9SAlexander Graf 
180e6e505b9SAlexander Graf 	if (port > 4)
181e6e505b9SAlexander Graf 		return -1;
182e6e505b9SAlexander Graf 
183e6e505b9SAlexander Graf 	/* set the apb reset and clock gate for twi */
184e6e505b9SAlexander Graf 	if (state) {
185e6e505b9SAlexander Graf 		setbits_le32(&ccm->apb1_gate,
186e6e505b9SAlexander Graf 			     CLK_GATE_OPEN << (APB1_GATE_TWI_SHIFT + port));
187e6e505b9SAlexander Graf 		setbits_le32(&ccm->apb1_reset_cfg,
188e6e505b9SAlexander Graf 			     1 << (APB1_RESET_TWI_SHIFT + port));
189e6e505b9SAlexander Graf 	} else {
190e6e505b9SAlexander Graf 		clrbits_le32(&ccm->apb1_reset_cfg,
191e6e505b9SAlexander Graf 			     1 << (APB1_RESET_TWI_SHIFT + port));
192e6e505b9SAlexander Graf 		clrbits_le32(&ccm->apb1_gate,
193e6e505b9SAlexander Graf 			     CLK_GATE_OPEN << (APB1_GATE_TWI_SHIFT + port));
194e6e505b9SAlexander Graf 	}
195e6e505b9SAlexander Graf 
196e6e505b9SAlexander Graf 	return 0;
197e6e505b9SAlexander Graf }
198e6e505b9SAlexander Graf 
clock_get_pll4_periph0(void)199e6e505b9SAlexander Graf unsigned int clock_get_pll4_periph0(void)
200e6e505b9SAlexander Graf {
201e6e505b9SAlexander Graf 	struct sunxi_ccm_reg *const ccm =
202e6e505b9SAlexander Graf 		(struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
203e6e505b9SAlexander Graf 	uint32_t rval = readl(&ccm->pll4_periph0_cfg);
204e6e505b9SAlexander Graf 	int n = ((rval & CCM_PLL4_CTRL_N_MASK) >> CCM_PLL4_CTRL_N_SHIFT);
205e6e505b9SAlexander Graf 	int p = ((rval & CCM_PLL4_CTRL_P_MASK) >> CCM_PLL4_CTRL_P_SHIFT);
206e6e505b9SAlexander Graf 	int m = ((rval & CCM_PLL4_CTRL_M_MASK) >> CCM_PLL4_CTRL_M_SHIFT) + 1;
207e6e505b9SAlexander Graf 	const int k = 1;
208e6e505b9SAlexander Graf 
209e6e505b9SAlexander Graf 	return ((24000000 * n * k) >> p) / m;
210e6e505b9SAlexander Graf }
211