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