Prashant Gaikwad | 8f8f484 | 2013-01-11 13:16:20 +0530 | [diff] [blame] | 1 | /* |
Peter De Schrijver | dba4072 | 2013-04-03 17:40:36 +0300 | [diff] [blame] | 2 | * Copyright (c) 2012, 2013, NVIDIA CORPORATION. All rights reserved. |
Prashant Gaikwad | 8f8f484 | 2013-01-11 13:16:20 +0530 | [diff] [blame] | 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify it |
| 5 | * under the terms and conditions of the GNU General Public License, |
| 6 | * version 2, as published by the Free Software Foundation. |
| 7 | * |
| 8 | * This program is distributed in the hope it will be useful, but WITHOUT |
| 9 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 10 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 11 | * more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public License |
| 14 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 15 | */ |
| 16 | |
| 17 | #include <linux/slab.h> |
| 18 | #include <linux/io.h> |
| 19 | #include <linux/delay.h> |
| 20 | #include <linux/err.h> |
Prashant Gaikwad | 8f8f484 | 2013-01-11 13:16:20 +0530 | [diff] [blame] | 21 | #include <linux/clk.h> |
Stephen Boyd | 584ac4e | 2015-06-19 15:00:46 -0700 | [diff] [blame] | 22 | #include <linux/clk-provider.h> |
Prashant Gaikwad | 8f8f484 | 2013-01-11 13:16:20 +0530 | [diff] [blame] | 23 | |
| 24 | #include "clk.h" |
| 25 | |
| 26 | #define PLL_BASE_BYPASS BIT(31) |
| 27 | #define PLL_BASE_ENABLE BIT(30) |
| 28 | #define PLL_BASE_REF_ENABLE BIT(29) |
| 29 | #define PLL_BASE_OVERRIDE BIT(28) |
| 30 | |
| 31 | #define PLL_BASE_DIVP_SHIFT 20 |
| 32 | #define PLL_BASE_DIVP_WIDTH 3 |
| 33 | #define PLL_BASE_DIVN_SHIFT 8 |
| 34 | #define PLL_BASE_DIVN_WIDTH 10 |
| 35 | #define PLL_BASE_DIVM_SHIFT 0 |
| 36 | #define PLL_BASE_DIVM_WIDTH 5 |
| 37 | #define PLLU_POST_DIVP_MASK 0x1 |
| 38 | |
| 39 | #define PLL_MISC_DCCON_SHIFT 20 |
| 40 | #define PLL_MISC_CPCON_SHIFT 8 |
| 41 | #define PLL_MISC_CPCON_WIDTH 4 |
| 42 | #define PLL_MISC_CPCON_MASK ((1 << PLL_MISC_CPCON_WIDTH) - 1) |
| 43 | #define PLL_MISC_LFCON_SHIFT 4 |
| 44 | #define PLL_MISC_LFCON_WIDTH 4 |
| 45 | #define PLL_MISC_LFCON_MASK ((1 << PLL_MISC_LFCON_WIDTH) - 1) |
| 46 | #define PLL_MISC_VCOCON_SHIFT 0 |
| 47 | #define PLL_MISC_VCOCON_WIDTH 4 |
| 48 | #define PLL_MISC_VCOCON_MASK ((1 << PLL_MISC_VCOCON_WIDTH) - 1) |
| 49 | |
| 50 | #define OUT_OF_TABLE_CPCON 8 |
| 51 | |
| 52 | #define PMC_PLLP_WB0_OVERRIDE 0xf8 |
| 53 | #define PMC_PLLP_WB0_OVERRIDE_PLLM_ENABLE BIT(12) |
| 54 | #define PMC_PLLP_WB0_OVERRIDE_PLLM_OVERRIDE BIT(11) |
| 55 | |
| 56 | #define PLL_POST_LOCK_DELAY 50 |
| 57 | |
| 58 | #define PLLDU_LFCON_SET_DIVN 600 |
| 59 | |
| 60 | #define PLLE_BASE_DIVCML_SHIFT 24 |
Thierry Reding | d0f02ce | 2014-04-04 15:55:13 +0200 | [diff] [blame] | 61 | #define PLLE_BASE_DIVCML_MASK 0xf |
Prashant Gaikwad | 8f8f484 | 2013-01-11 13:16:20 +0530 | [diff] [blame] | 62 | #define PLLE_BASE_DIVP_SHIFT 16 |
Thierry Reding | d0f02ce | 2014-04-04 15:55:13 +0200 | [diff] [blame] | 63 | #define PLLE_BASE_DIVP_WIDTH 6 |
Prashant Gaikwad | 8f8f484 | 2013-01-11 13:16:20 +0530 | [diff] [blame] | 64 | #define PLLE_BASE_DIVN_SHIFT 8 |
| 65 | #define PLLE_BASE_DIVN_WIDTH 8 |
| 66 | #define PLLE_BASE_DIVM_SHIFT 0 |
| 67 | #define PLLE_BASE_DIVM_WIDTH 8 |
Rhyland Klein | dd322f0 | 2015-06-18 17:28:28 -0400 | [diff] [blame] | 68 | #define PLLE_BASE_ENABLE BIT(31) |
Prashant Gaikwad | 8f8f484 | 2013-01-11 13:16:20 +0530 | [diff] [blame] | 69 | |
| 70 | #define PLLE_MISC_SETUP_BASE_SHIFT 16 |
| 71 | #define PLLE_MISC_SETUP_BASE_MASK (0xffff << PLLE_MISC_SETUP_BASE_SHIFT) |
| 72 | #define PLLE_MISC_LOCK_ENABLE BIT(9) |
| 73 | #define PLLE_MISC_READY BIT(15) |
| 74 | #define PLLE_MISC_SETUP_EX_SHIFT 2 |
| 75 | #define PLLE_MISC_SETUP_EX_MASK (3 << PLLE_MISC_SETUP_EX_SHIFT) |
| 76 | #define PLLE_MISC_SETUP_MASK (PLLE_MISC_SETUP_BASE_MASK | \ |
| 77 | PLLE_MISC_SETUP_EX_MASK) |
| 78 | #define PLLE_MISC_SETUP_VALUE (7 << PLLE_MISC_SETUP_BASE_SHIFT) |
| 79 | |
| 80 | #define PLLE_SS_CTRL 0x68 |
Peter De Schrijver | 642fb0c | 2013-09-26 18:30:01 +0300 | [diff] [blame] | 81 | #define PLLE_SS_CNTL_BYPASS_SS BIT(10) |
| 82 | #define PLLE_SS_CNTL_INTERP_RESET BIT(11) |
| 83 | #define PLLE_SS_CNTL_SSC_BYP BIT(12) |
| 84 | #define PLLE_SS_CNTL_CENTER BIT(14) |
| 85 | #define PLLE_SS_CNTL_INVERT BIT(15) |
| 86 | #define PLLE_SS_DISABLE (PLLE_SS_CNTL_BYPASS_SS | PLLE_SS_CNTL_INTERP_RESET |\ |
| 87 | PLLE_SS_CNTL_SSC_BYP) |
| 88 | #define PLLE_SS_MAX_MASK 0x1ff |
| 89 | #define PLLE_SS_MAX_VAL 0x25 |
| 90 | #define PLLE_SS_INC_MASK (0xff << 16) |
| 91 | #define PLLE_SS_INC_VAL (0x1 << 16) |
| 92 | #define PLLE_SS_INCINTRV_MASK (0x3f << 24) |
| 93 | #define PLLE_SS_INCINTRV_VAL (0x20 << 24) |
| 94 | #define PLLE_SS_COEFFICIENTS_MASK \ |
| 95 | (PLLE_SS_MAX_MASK | PLLE_SS_INC_MASK | PLLE_SS_INCINTRV_MASK) |
| 96 | #define PLLE_SS_COEFFICIENTS_VAL \ |
| 97 | (PLLE_SS_MAX_VAL | PLLE_SS_INC_VAL | PLLE_SS_INCINTRV_VAL) |
Prashant Gaikwad | 8f8f484 | 2013-01-11 13:16:20 +0530 | [diff] [blame] | 98 | |
Peter De Schrijver | c1d1939 | 2013-04-03 17:40:41 +0300 | [diff] [blame] | 99 | #define PLLE_AUX_PLLP_SEL BIT(2) |
Jim Lin | 2cfe167 | 2014-05-14 17:32:57 -0700 | [diff] [blame] | 100 | #define PLLE_AUX_USE_LOCKDET BIT(3) |
Peter De Schrijver | c1d1939 | 2013-04-03 17:40:41 +0300 | [diff] [blame] | 101 | #define PLLE_AUX_ENABLE_SWCTL BIT(4) |
Jim Lin | 2cfe167 | 2014-05-14 17:32:57 -0700 | [diff] [blame] | 102 | #define PLLE_AUX_SS_SWCTL BIT(6) |
Peter De Schrijver | c1d1939 | 2013-04-03 17:40:41 +0300 | [diff] [blame] | 103 | #define PLLE_AUX_SEQ_ENABLE BIT(24) |
Jim Lin | 2cfe167 | 2014-05-14 17:32:57 -0700 | [diff] [blame] | 104 | #define PLLE_AUX_SEQ_START_STATE BIT(25) |
Peter De Schrijver | c1d1939 | 2013-04-03 17:40:41 +0300 | [diff] [blame] | 105 | #define PLLE_AUX_PLLRE_SEL BIT(28) |
Rhyland Klein | dd322f0 | 2015-06-18 17:28:28 -0400 | [diff] [blame] | 106 | #define PLLE_AUX_SS_SEQ_INCLUDE BIT(31) |
Peter De Schrijver | c1d1939 | 2013-04-03 17:40:41 +0300 | [diff] [blame] | 107 | |
Jim Lin | 2cfe167 | 2014-05-14 17:32:57 -0700 | [diff] [blame] | 108 | #define XUSBIO_PLL_CFG0 0x51c |
| 109 | #define XUSBIO_PLL_CFG0_PADPLL_RESET_SWCTL BIT(0) |
| 110 | #define XUSBIO_PLL_CFG0_CLK_ENABLE_SWCTL BIT(2) |
| 111 | #define XUSBIO_PLL_CFG0_PADPLL_USE_LOCKDET BIT(6) |
| 112 | #define XUSBIO_PLL_CFG0_SEQ_ENABLE BIT(24) |
| 113 | #define XUSBIO_PLL_CFG0_SEQ_START_STATE BIT(25) |
| 114 | |
Mikko Perttunen | 37ab366 | 2014-06-18 17:23:23 +0300 | [diff] [blame] | 115 | #define SATA_PLL_CFG0 0x490 |
| 116 | #define SATA_PLL_CFG0_PADPLL_RESET_SWCTL BIT(0) |
Mikko Perttunen | 0e548d50b | 2014-07-08 09:30:15 +0200 | [diff] [blame] | 117 | #define SATA_PLL_CFG0_PADPLL_USE_LOCKDET BIT(2) |
| 118 | #define SATA_PLL_CFG0_SEQ_ENABLE BIT(24) |
| 119 | #define SATA_PLL_CFG0_SEQ_START_STATE BIT(25) |
Mikko Perttunen | 37ab366 | 2014-06-18 17:23:23 +0300 | [diff] [blame] | 120 | |
Peter De Schrijver | c1d1939 | 2013-04-03 17:40:41 +0300 | [diff] [blame] | 121 | #define PLLE_MISC_PLLE_PTS BIT(8) |
| 122 | #define PLLE_MISC_IDDQ_SW_VALUE BIT(13) |
| 123 | #define PLLE_MISC_IDDQ_SW_CTRL BIT(14) |
| 124 | #define PLLE_MISC_VREG_BG_CTRL_SHIFT 4 |
| 125 | #define PLLE_MISC_VREG_BG_CTRL_MASK (3 << PLLE_MISC_VREG_BG_CTRL_SHIFT) |
| 126 | #define PLLE_MISC_VREG_CTRL_SHIFT 2 |
| 127 | #define PLLE_MISC_VREG_CTRL_MASK (2 << PLLE_MISC_VREG_CTRL_SHIFT) |
| 128 | |
| 129 | #define PLLCX_MISC_STROBE BIT(31) |
| 130 | #define PLLCX_MISC_RESET BIT(30) |
| 131 | #define PLLCX_MISC_SDM_DIV_SHIFT 28 |
| 132 | #define PLLCX_MISC_SDM_DIV_MASK (0x3 << PLLCX_MISC_SDM_DIV_SHIFT) |
| 133 | #define PLLCX_MISC_FILT_DIV_SHIFT 26 |
| 134 | #define PLLCX_MISC_FILT_DIV_MASK (0x3 << PLLCX_MISC_FILT_DIV_SHIFT) |
| 135 | #define PLLCX_MISC_ALPHA_SHIFT 18 |
| 136 | #define PLLCX_MISC_DIV_LOW_RANGE \ |
| 137 | ((0x1 << PLLCX_MISC_SDM_DIV_SHIFT) | \ |
| 138 | (0x1 << PLLCX_MISC_FILT_DIV_SHIFT)) |
| 139 | #define PLLCX_MISC_DIV_HIGH_RANGE \ |
| 140 | ((0x2 << PLLCX_MISC_SDM_DIV_SHIFT) | \ |
| 141 | (0x2 << PLLCX_MISC_FILT_DIV_SHIFT)) |
| 142 | #define PLLCX_MISC_COEF_LOW_RANGE \ |
| 143 | ((0x14 << PLLCX_MISC_KA_SHIFT) | (0x38 << PLLCX_MISC_KB_SHIFT)) |
| 144 | #define PLLCX_MISC_KA_SHIFT 2 |
| 145 | #define PLLCX_MISC_KB_SHIFT 9 |
| 146 | #define PLLCX_MISC_DEFAULT (PLLCX_MISC_COEF_LOW_RANGE | \ |
| 147 | (0x19 << PLLCX_MISC_ALPHA_SHIFT) | \ |
| 148 | PLLCX_MISC_DIV_LOW_RANGE | \ |
| 149 | PLLCX_MISC_RESET) |
| 150 | #define PLLCX_MISC1_DEFAULT 0x000d2308 |
| 151 | #define PLLCX_MISC2_DEFAULT 0x30211200 |
| 152 | #define PLLCX_MISC3_DEFAULT 0x200 |
| 153 | |
Prashant Gaikwad | 8f8f484 | 2013-01-11 13:16:20 +0530 | [diff] [blame] | 154 | #define PMC_SATA_PWRGT 0x1ac |
| 155 | #define PMC_SATA_PWRGT_PLLE_IDDQ_VALUE BIT(5) |
| 156 | #define PMC_SATA_PWRGT_PLLE_IDDQ_SWCTL BIT(4) |
| 157 | |
Peter De Schrijver | 798e910 | 2013-09-09 13:22:55 +0300 | [diff] [blame] | 158 | #define PLLSS_MISC_KCP 0 |
| 159 | #define PLLSS_MISC_KVCO 0 |
| 160 | #define PLLSS_MISC_SETUP 0 |
| 161 | #define PLLSS_EN_SDM 0 |
| 162 | #define PLLSS_EN_SSC 0 |
| 163 | #define PLLSS_EN_DITHER2 0 |
| 164 | #define PLLSS_EN_DITHER 1 |
| 165 | #define PLLSS_SDM_RESET 0 |
| 166 | #define PLLSS_CLAMP 0 |
| 167 | #define PLLSS_SDM_SSC_MAX 0 |
| 168 | #define PLLSS_SDM_SSC_MIN 0 |
| 169 | #define PLLSS_SDM_SSC_STEP 0 |
| 170 | #define PLLSS_SDM_DIN 0 |
| 171 | #define PLLSS_MISC_DEFAULT ((PLLSS_MISC_KCP << 25) | \ |
| 172 | (PLLSS_MISC_KVCO << 24) | \ |
| 173 | PLLSS_MISC_SETUP) |
| 174 | #define PLLSS_CFG_DEFAULT ((PLLSS_EN_SDM << 31) | \ |
| 175 | (PLLSS_EN_SSC << 30) | \ |
| 176 | (PLLSS_EN_DITHER2 << 29) | \ |
| 177 | (PLLSS_EN_DITHER << 28) | \ |
| 178 | (PLLSS_SDM_RESET) << 27 | \ |
| 179 | (PLLSS_CLAMP << 22)) |
| 180 | #define PLLSS_CTRL1_DEFAULT \ |
| 181 | ((PLLSS_SDM_SSC_MAX << 16) | PLLSS_SDM_SSC_MIN) |
| 182 | #define PLLSS_CTRL2_DEFAULT \ |
| 183 | ((PLLSS_SDM_SSC_STEP << 16) | PLLSS_SDM_DIN) |
| 184 | #define PLLSS_LOCK_OVERRIDE BIT(24) |
| 185 | #define PLLSS_REF_SRC_SEL_SHIFT 25 |
| 186 | #define PLLSS_REF_SRC_SEL_MASK (3 << PLLSS_REF_SRC_SEL_SHIFT) |
| 187 | |
Prashant Gaikwad | 8f8f484 | 2013-01-11 13:16:20 +0530 | [diff] [blame] | 188 | #define pll_readl(offset, p) readl_relaxed(p->clk_base + offset) |
| 189 | #define pll_readl_base(p) pll_readl(p->params->base_reg, p) |
| 190 | #define pll_readl_misc(p) pll_readl(p->params->misc_reg, p) |
Peter De Schrijver | 408a24f | 2013-06-06 13:47:31 +0300 | [diff] [blame] | 191 | #define pll_override_readl(offset, p) readl_relaxed(p->pmc + offset) |
Rhyland Klein | d907f4b | 2015-06-18 17:28:24 -0400 | [diff] [blame] | 192 | #define pll_readl_sdm_din(p) pll_readl(p->params->sdm_din_reg, p) |
| 193 | #define pll_readl_sdm_ctrl(p) pll_readl(p->params->sdm_ctrl_reg, p) |
Prashant Gaikwad | 8f8f484 | 2013-01-11 13:16:20 +0530 | [diff] [blame] | 194 | |
| 195 | #define pll_writel(val, offset, p) writel_relaxed(val, p->clk_base + offset) |
| 196 | #define pll_writel_base(val, p) pll_writel(val, p->params->base_reg, p) |
| 197 | #define pll_writel_misc(val, p) pll_writel(val, p->params->misc_reg, p) |
Peter De Schrijver | 408a24f | 2013-06-06 13:47:31 +0300 | [diff] [blame] | 198 | #define pll_override_writel(val, offset, p) writel(val, p->pmc + offset) |
Rhyland Klein | d907f4b | 2015-06-18 17:28:24 -0400 | [diff] [blame] | 199 | #define pll_writel_sdm_din(val, p) pll_writel(val, p->params->sdm_din_reg, p) |
| 200 | #define pll_writel_sdm_ctrl(val, p) pll_writel(val, p->params->sdm_ctrl_reg, p) |
Prashant Gaikwad | 8f8f484 | 2013-01-11 13:16:20 +0530 | [diff] [blame] | 201 | |
| 202 | #define mask(w) ((1 << (w)) - 1) |
Peter De Schrijver | aa6fefd | 2013-06-05 16:51:25 +0300 | [diff] [blame] | 203 | #define divm_mask(p) mask(p->params->div_nmp->divm_width) |
| 204 | #define divn_mask(p) mask(p->params->div_nmp->divn_width) |
Peter De Schrijver | ebe142b | 2013-10-04 17:28:34 +0300 | [diff] [blame] | 205 | #define divp_mask(p) (p->params->flags & TEGRA_PLLU ? PLLU_POST_DIVP_MASK :\ |
Peter De Schrijver | aa6fefd | 2013-06-05 16:51:25 +0300 | [diff] [blame] | 206 | mask(p->params->div_nmp->divp_width)) |
Rhyland Klein | d907f4b | 2015-06-18 17:28:24 -0400 | [diff] [blame] | 207 | #define sdm_din_mask(p) p->params->sdm_din_mask |
| 208 | #define sdm_en_mask(p) p->params->sdm_ctrl_en_mask |
Prashant Gaikwad | 8f8f484 | 2013-01-11 13:16:20 +0530 | [diff] [blame] | 209 | |
Thierry Reding | c61e4e7 | 2014-04-04 15:55:14 +0200 | [diff] [blame] | 210 | #define divm_shift(p) (p)->params->div_nmp->divm_shift |
| 211 | #define divn_shift(p) (p)->params->div_nmp->divn_shift |
| 212 | #define divp_shift(p) (p)->params->div_nmp->divp_shift |
| 213 | |
| 214 | #define divm_mask_shifted(p) (divm_mask(p) << divm_shift(p)) |
| 215 | #define divn_mask_shifted(p) (divn_mask(p) << divn_shift(p)) |
| 216 | #define divp_mask_shifted(p) (divp_mask(p) << divp_shift(p)) |
| 217 | |
Prashant Gaikwad | 8f8f484 | 2013-01-11 13:16:20 +0530 | [diff] [blame] | 218 | #define divm_max(p) (divm_mask(p)) |
| 219 | #define divn_max(p) (divn_mask(p)) |
| 220 | #define divp_max(p) (1 << (divp_mask(p))) |
| 221 | |
Rhyland Klein | d907f4b | 2015-06-18 17:28:24 -0400 | [diff] [blame] | 222 | #define sdin_din_to_data(din) ((u16)((din) ? : 0xFFFFU)) |
| 223 | #define sdin_data_to_din(dat) (((dat) == 0xFFFFU) ? 0 : (s16)dat) |
| 224 | |
Peter De Schrijver | aa6fefd | 2013-06-05 16:51:25 +0300 | [diff] [blame] | 225 | static struct div_nmp default_nmp = { |
| 226 | .divn_shift = PLL_BASE_DIVN_SHIFT, |
| 227 | .divn_width = PLL_BASE_DIVN_WIDTH, |
| 228 | .divm_shift = PLL_BASE_DIVM_SHIFT, |
| 229 | .divm_width = PLL_BASE_DIVM_WIDTH, |
| 230 | .divp_shift = PLL_BASE_DIVP_SHIFT, |
| 231 | .divp_width = PLL_BASE_DIVP_WIDTH, |
| 232 | }; |
| 233 | |
Prashant Gaikwad | 8f8f484 | 2013-01-11 13:16:20 +0530 | [diff] [blame] | 234 | static void clk_pll_enable_lock(struct tegra_clk_pll *pll) |
| 235 | { |
| 236 | u32 val; |
| 237 | |
Peter De Schrijver | ebe142b | 2013-10-04 17:28:34 +0300 | [diff] [blame] | 238 | if (!(pll->params->flags & TEGRA_PLL_USE_LOCK)) |
Prashant Gaikwad | 8f8f484 | 2013-01-11 13:16:20 +0530 | [diff] [blame] | 239 | return; |
| 240 | |
Peter De Schrijver | ebe142b | 2013-10-04 17:28:34 +0300 | [diff] [blame] | 241 | if (!(pll->params->flags & TEGRA_PLL_HAS_LOCK_ENABLE)) |
Peter De Schrijver | 7ba2881 | 2013-04-03 17:40:38 +0300 | [diff] [blame] | 242 | return; |
| 243 | |
Prashant Gaikwad | 8f8f484 | 2013-01-11 13:16:20 +0530 | [diff] [blame] | 244 | val = pll_readl_misc(pll); |
| 245 | val |= BIT(pll->params->lock_enable_bit_idx); |
| 246 | pll_writel_misc(val, pll); |
| 247 | } |
| 248 | |
Peter De Schrijver | dba4072 | 2013-04-03 17:40:36 +0300 | [diff] [blame] | 249 | static int clk_pll_wait_for_lock(struct tegra_clk_pll *pll) |
Prashant Gaikwad | 8f8f484 | 2013-01-11 13:16:20 +0530 | [diff] [blame] | 250 | { |
| 251 | int i; |
Peter De Schrijver | 3e72771 | 2013-04-03 17:40:40 +0300 | [diff] [blame] | 252 | u32 val, lock_mask; |
Peter De Schrijver | dba4072 | 2013-04-03 17:40:36 +0300 | [diff] [blame] | 253 | void __iomem *lock_addr; |
Prashant Gaikwad | 8f8f484 | 2013-01-11 13:16:20 +0530 | [diff] [blame] | 254 | |
Peter De Schrijver | ebe142b | 2013-10-04 17:28:34 +0300 | [diff] [blame] | 255 | if (!(pll->params->flags & TEGRA_PLL_USE_LOCK)) { |
Prashant Gaikwad | 8f8f484 | 2013-01-11 13:16:20 +0530 | [diff] [blame] | 256 | udelay(pll->params->lock_delay); |
| 257 | return 0; |
| 258 | } |
| 259 | |
Peter De Schrijver | dba4072 | 2013-04-03 17:40:36 +0300 | [diff] [blame] | 260 | lock_addr = pll->clk_base; |
Peter De Schrijver | ebe142b | 2013-10-04 17:28:34 +0300 | [diff] [blame] | 261 | if (pll->params->flags & TEGRA_PLL_LOCK_MISC) |
Peter De Schrijver | dba4072 | 2013-04-03 17:40:36 +0300 | [diff] [blame] | 262 | lock_addr += pll->params->misc_reg; |
| 263 | else |
| 264 | lock_addr += pll->params->base_reg; |
| 265 | |
Peter De Schrijver | 3e72771 | 2013-04-03 17:40:40 +0300 | [diff] [blame] | 266 | lock_mask = pll->params->lock_mask; |
Peter De Schrijver | dba4072 | 2013-04-03 17:40:36 +0300 | [diff] [blame] | 267 | |
Prashant Gaikwad | 8f8f484 | 2013-01-11 13:16:20 +0530 | [diff] [blame] | 268 | for (i = 0; i < pll->params->lock_delay; i++) { |
| 269 | val = readl_relaxed(lock_addr); |
Peter De Schrijver | 3e72771 | 2013-04-03 17:40:40 +0300 | [diff] [blame] | 270 | if ((val & lock_mask) == lock_mask) { |
Prashant Gaikwad | 8f8f484 | 2013-01-11 13:16:20 +0530 | [diff] [blame] | 271 | udelay(PLL_POST_LOCK_DELAY); |
| 272 | return 0; |
| 273 | } |
| 274 | udelay(2); /* timeout = 2 * lock time */ |
| 275 | } |
| 276 | |
| 277 | pr_err("%s: Timed out waiting for pll %s lock\n", __func__, |
Stephen Boyd | 836ee0f | 2015-08-12 11:42:23 -0700 | [diff] [blame] | 278 | clk_hw_get_name(&pll->hw)); |
Prashant Gaikwad | 8f8f484 | 2013-01-11 13:16:20 +0530 | [diff] [blame] | 279 | |
| 280 | return -1; |
| 281 | } |
| 282 | |
Rhyland Klein | 6583a63 | 2015-06-18 17:28:19 -0400 | [diff] [blame] | 283 | int tegra_pll_wait_for_lock(struct tegra_clk_pll *pll) |
| 284 | { |
| 285 | return clk_pll_wait_for_lock(pll); |
| 286 | } |
| 287 | |
Prashant Gaikwad | 8f8f484 | 2013-01-11 13:16:20 +0530 | [diff] [blame] | 288 | static int clk_pll_is_enabled(struct clk_hw *hw) |
| 289 | { |
| 290 | struct tegra_clk_pll *pll = to_clk_pll(hw); |
| 291 | u32 val; |
| 292 | |
Peter De Schrijver | ebe142b | 2013-10-04 17:28:34 +0300 | [diff] [blame] | 293 | if (pll->params->flags & TEGRA_PLLM) { |
Prashant Gaikwad | 8f8f484 | 2013-01-11 13:16:20 +0530 | [diff] [blame] | 294 | val = readl_relaxed(pll->pmc + PMC_PLLP_WB0_OVERRIDE); |
| 295 | if (val & PMC_PLLP_WB0_OVERRIDE_PLLM_OVERRIDE) |
| 296 | return val & PMC_PLLP_WB0_OVERRIDE_PLLM_ENABLE ? 1 : 0; |
| 297 | } |
| 298 | |
| 299 | val = pll_readl_base(pll); |
| 300 | |
| 301 | return val & PLL_BASE_ENABLE ? 1 : 0; |
| 302 | } |
| 303 | |
Peter De Schrijver | dba4072 | 2013-04-03 17:40:36 +0300 | [diff] [blame] | 304 | static void _clk_pll_enable(struct clk_hw *hw) |
Prashant Gaikwad | 8f8f484 | 2013-01-11 13:16:20 +0530 | [diff] [blame] | 305 | { |
| 306 | struct tegra_clk_pll *pll = to_clk_pll(hw); |
| 307 | u32 val; |
| 308 | |
Rhyland Klein | 7db864c | 2015-06-18 17:28:20 -0400 | [diff] [blame] | 309 | if (pll->params->iddq_reg) { |
| 310 | val = pll_readl(pll->params->iddq_reg, pll); |
| 311 | val &= ~BIT(pll->params->iddq_bit_idx); |
| 312 | pll_writel(val, pll->params->iddq_reg, pll); |
| 313 | udelay(2); |
| 314 | } |
| 315 | |
Bill Huang | fde207e | 2015-06-18 17:28:26 -0400 | [diff] [blame] | 316 | if (pll->params->reset_reg) { |
| 317 | val = pll_readl(pll->params->reset_reg, pll); |
| 318 | val &= ~BIT(pll->params->reset_bit_idx); |
| 319 | pll_writel(val, pll->params->reset_reg, pll); |
| 320 | } |
| 321 | |
Prashant Gaikwad | 8f8f484 | 2013-01-11 13:16:20 +0530 | [diff] [blame] | 322 | clk_pll_enable_lock(pll); |
| 323 | |
| 324 | val = pll_readl_base(pll); |
Peter De Schrijver | ebe142b | 2013-10-04 17:28:34 +0300 | [diff] [blame] | 325 | if (pll->params->flags & TEGRA_PLL_BYPASS) |
Peter De Schrijver | dd93587 | 2013-04-03 17:40:37 +0300 | [diff] [blame] | 326 | val &= ~PLL_BASE_BYPASS; |
Prashant Gaikwad | 8f8f484 | 2013-01-11 13:16:20 +0530 | [diff] [blame] | 327 | val |= PLL_BASE_ENABLE; |
| 328 | pll_writel_base(val, pll); |
| 329 | |
Peter De Schrijver | ebe142b | 2013-10-04 17:28:34 +0300 | [diff] [blame] | 330 | if (pll->params->flags & TEGRA_PLLM) { |
Prashant Gaikwad | 8f8f484 | 2013-01-11 13:16:20 +0530 | [diff] [blame] | 331 | val = readl_relaxed(pll->pmc + PMC_PLLP_WB0_OVERRIDE); |
| 332 | val |= PMC_PLLP_WB0_OVERRIDE_PLLM_ENABLE; |
| 333 | writel_relaxed(val, pll->pmc + PMC_PLLP_WB0_OVERRIDE); |
| 334 | } |
Prashant Gaikwad | 8f8f484 | 2013-01-11 13:16:20 +0530 | [diff] [blame] | 335 | } |
| 336 | |
| 337 | static void _clk_pll_disable(struct clk_hw *hw) |
| 338 | { |
| 339 | struct tegra_clk_pll *pll = to_clk_pll(hw); |
| 340 | u32 val; |
| 341 | |
| 342 | val = pll_readl_base(pll); |
Peter De Schrijver | ebe142b | 2013-10-04 17:28:34 +0300 | [diff] [blame] | 343 | if (pll->params->flags & TEGRA_PLL_BYPASS) |
Peter De Schrijver | dd93587 | 2013-04-03 17:40:37 +0300 | [diff] [blame] | 344 | val &= ~PLL_BASE_BYPASS; |
| 345 | val &= ~PLL_BASE_ENABLE; |
Prashant Gaikwad | 8f8f484 | 2013-01-11 13:16:20 +0530 | [diff] [blame] | 346 | pll_writel_base(val, pll); |
| 347 | |
Peter De Schrijver | ebe142b | 2013-10-04 17:28:34 +0300 | [diff] [blame] | 348 | if (pll->params->flags & TEGRA_PLLM) { |
Prashant Gaikwad | 8f8f484 | 2013-01-11 13:16:20 +0530 | [diff] [blame] | 349 | val = readl_relaxed(pll->pmc + PMC_PLLP_WB0_OVERRIDE); |
| 350 | val &= ~PMC_PLLP_WB0_OVERRIDE_PLLM_ENABLE; |
| 351 | writel_relaxed(val, pll->pmc + PMC_PLLP_WB0_OVERRIDE); |
| 352 | } |
Rhyland Klein | 7db864c | 2015-06-18 17:28:20 -0400 | [diff] [blame] | 353 | |
Bill Huang | fde207e | 2015-06-18 17:28:26 -0400 | [diff] [blame] | 354 | if (pll->params->reset_reg) { |
| 355 | val = pll_readl(pll->params->reset_reg, pll); |
| 356 | val |= BIT(pll->params->reset_bit_idx); |
| 357 | pll_writel(val, pll->params->reset_reg, pll); |
| 358 | } |
| 359 | |
Rhyland Klein | 7db864c | 2015-06-18 17:28:20 -0400 | [diff] [blame] | 360 | if (pll->params->iddq_reg) { |
| 361 | val = pll_readl(pll->params->iddq_reg, pll); |
| 362 | val |= BIT(pll->params->iddq_bit_idx); |
| 363 | pll_writel(val, pll->params->iddq_reg, pll); |
| 364 | udelay(2); |
| 365 | } |
Prashant Gaikwad | 8f8f484 | 2013-01-11 13:16:20 +0530 | [diff] [blame] | 366 | } |
| 367 | |
| 368 | static int clk_pll_enable(struct clk_hw *hw) |
| 369 | { |
| 370 | struct tegra_clk_pll *pll = to_clk_pll(hw); |
| 371 | unsigned long flags = 0; |
| 372 | int ret; |
| 373 | |
| 374 | if (pll->lock) |
| 375 | spin_lock_irqsave(pll->lock, flags); |
| 376 | |
Peter De Schrijver | dba4072 | 2013-04-03 17:40:36 +0300 | [diff] [blame] | 377 | _clk_pll_enable(hw); |
| 378 | |
| 379 | ret = clk_pll_wait_for_lock(pll); |
Prashant Gaikwad | 8f8f484 | 2013-01-11 13:16:20 +0530 | [diff] [blame] | 380 | |
| 381 | if (pll->lock) |
| 382 | spin_unlock_irqrestore(pll->lock, flags); |
| 383 | |
| 384 | return ret; |
| 385 | } |
| 386 | |
| 387 | static void clk_pll_disable(struct clk_hw *hw) |
| 388 | { |
| 389 | struct tegra_clk_pll *pll = to_clk_pll(hw); |
| 390 | unsigned long flags = 0; |
| 391 | |
| 392 | if (pll->lock) |
| 393 | spin_lock_irqsave(pll->lock, flags); |
| 394 | |
| 395 | _clk_pll_disable(hw); |
| 396 | |
| 397 | if (pll->lock) |
| 398 | spin_unlock_irqrestore(pll->lock, flags); |
| 399 | } |
| 400 | |
Peter De Schrijver | 053b525 | 2013-06-05 15:56:41 +0300 | [diff] [blame] | 401 | static int _p_div_to_hw(struct clk_hw *hw, u8 p_div) |
| 402 | { |
| 403 | struct tegra_clk_pll *pll = to_clk_pll(hw); |
Thierry Reding | 385f9ad | 2015-11-19 16:34:06 +0100 | [diff] [blame] | 404 | const struct pdiv_map *p_tohw = pll->params->pdiv_tohw; |
Peter De Schrijver | 053b525 | 2013-06-05 15:56:41 +0300 | [diff] [blame] | 405 | |
| 406 | if (p_tohw) { |
| 407 | while (p_tohw->pdiv) { |
| 408 | if (p_div <= p_tohw->pdiv) |
| 409 | return p_tohw->hw_val; |
| 410 | p_tohw++; |
| 411 | } |
| 412 | return -EINVAL; |
| 413 | } |
| 414 | return -EINVAL; |
| 415 | } |
| 416 | |
Rhyland Klein | 6b301a0 | 2015-06-18 17:28:36 -0400 | [diff] [blame] | 417 | int tegra_pll_p_div_to_hw(struct tegra_clk_pll *pll, u8 p_div) |
| 418 | { |
| 419 | return _p_div_to_hw(&pll->hw, p_div); |
| 420 | } |
| 421 | |
Peter De Schrijver | 053b525 | 2013-06-05 15:56:41 +0300 | [diff] [blame] | 422 | static int _hw_to_p_div(struct clk_hw *hw, u8 p_div_hw) |
| 423 | { |
| 424 | struct tegra_clk_pll *pll = to_clk_pll(hw); |
Thierry Reding | 385f9ad | 2015-11-19 16:34:06 +0100 | [diff] [blame] | 425 | const struct pdiv_map *p_tohw = pll->params->pdiv_tohw; |
Peter De Schrijver | 053b525 | 2013-06-05 15:56:41 +0300 | [diff] [blame] | 426 | |
| 427 | if (p_tohw) { |
| 428 | while (p_tohw->pdiv) { |
| 429 | if (p_div_hw == p_tohw->hw_val) |
| 430 | return p_tohw->pdiv; |
| 431 | p_tohw++; |
| 432 | } |
| 433 | return -EINVAL; |
| 434 | } |
| 435 | |
| 436 | return 1 << p_div_hw; |
| 437 | } |
| 438 | |
Prashant Gaikwad | 8f8f484 | 2013-01-11 13:16:20 +0530 | [diff] [blame] | 439 | static int _get_table_rate(struct clk_hw *hw, |
| 440 | struct tegra_clk_pll_freq_table *cfg, |
| 441 | unsigned long rate, unsigned long parent_rate) |
| 442 | { |
| 443 | struct tegra_clk_pll *pll = to_clk_pll(hw); |
| 444 | struct tegra_clk_pll_freq_table *sel; |
Rhyland Klein | 86c679a | 2015-06-18 17:28:34 -0400 | [diff] [blame] | 445 | int p; |
Prashant Gaikwad | 8f8f484 | 2013-01-11 13:16:20 +0530 | [diff] [blame] | 446 | |
Peter De Schrijver | ebe142b | 2013-10-04 17:28:34 +0300 | [diff] [blame] | 447 | for (sel = pll->params->freq_table; sel->input_rate != 0; sel++) |
Prashant Gaikwad | 8f8f484 | 2013-01-11 13:16:20 +0530 | [diff] [blame] | 448 | if (sel->input_rate == parent_rate && |
| 449 | sel->output_rate == rate) |
| 450 | break; |
| 451 | |
| 452 | if (sel->input_rate == 0) |
| 453 | return -EINVAL; |
| 454 | |
Rhyland Klein | 86c679a | 2015-06-18 17:28:34 -0400 | [diff] [blame] | 455 | if (pll->params->pdiv_tohw) { |
| 456 | p = _p_div_to_hw(hw, sel->p); |
| 457 | if (p < 0) |
| 458 | return p; |
| 459 | } else { |
| 460 | p = ilog2(sel->p); |
| 461 | } |
| 462 | |
Prashant Gaikwad | 8f8f484 | 2013-01-11 13:16:20 +0530 | [diff] [blame] | 463 | cfg->input_rate = sel->input_rate; |
| 464 | cfg->output_rate = sel->output_rate; |
| 465 | cfg->m = sel->m; |
| 466 | cfg->n = sel->n; |
Rhyland Klein | 86c679a | 2015-06-18 17:28:34 -0400 | [diff] [blame] | 467 | cfg->p = p; |
Prashant Gaikwad | 8f8f484 | 2013-01-11 13:16:20 +0530 | [diff] [blame] | 468 | cfg->cpcon = sel->cpcon; |
Rhyland Klein | d907f4b | 2015-06-18 17:28:24 -0400 | [diff] [blame] | 469 | cfg->sdm_data = sel->sdm_data; |
Prashant Gaikwad | 8f8f484 | 2013-01-11 13:16:20 +0530 | [diff] [blame] | 470 | |
| 471 | return 0; |
| 472 | } |
| 473 | |
| 474 | static int _calc_rate(struct clk_hw *hw, struct tegra_clk_pll_freq_table *cfg, |
| 475 | unsigned long rate, unsigned long parent_rate) |
| 476 | { |
| 477 | struct tegra_clk_pll *pll = to_clk_pll(hw); |
| 478 | unsigned long cfreq; |
| 479 | u32 p_div = 0; |
Peter De Schrijver | 053b525 | 2013-06-05 15:56:41 +0300 | [diff] [blame] | 480 | int ret; |
Prashant Gaikwad | 8f8f484 | 2013-01-11 13:16:20 +0530 | [diff] [blame] | 481 | |
| 482 | switch (parent_rate) { |
| 483 | case 12000000: |
| 484 | case 26000000: |
| 485 | cfreq = (rate <= 1000000 * 1000) ? 1000000 : 2000000; |
| 486 | break; |
| 487 | case 13000000: |
| 488 | cfreq = (rate <= 1000000 * 1000) ? 1000000 : 2600000; |
| 489 | break; |
| 490 | case 16800000: |
| 491 | case 19200000: |
| 492 | cfreq = (rate <= 1200000 * 1000) ? 1200000 : 2400000; |
| 493 | break; |
| 494 | case 9600000: |
| 495 | case 28800000: |
| 496 | /* |
| 497 | * PLL_P_OUT1 rate is not listed in PLLA table |
| 498 | */ |
Thierry Reding | e52d7c0 | 2015-11-18 14:04:20 +0100 | [diff] [blame] | 499 | cfreq = parent_rate / (parent_rate / 1000000); |
Prashant Gaikwad | 8f8f484 | 2013-01-11 13:16:20 +0530 | [diff] [blame] | 500 | break; |
| 501 | default: |
| 502 | pr_err("%s Unexpected reference rate %lu\n", |
| 503 | __func__, parent_rate); |
| 504 | BUG(); |
| 505 | } |
| 506 | |
| 507 | /* Raise VCO to guarantee 0.5% accuracy */ |
| 508 | for (cfg->output_rate = rate; cfg->output_rate < 200 * cfreq; |
| 509 | cfg->output_rate <<= 1) |
| 510 | p_div++; |
| 511 | |
Prashant Gaikwad | 8f8f484 | 2013-01-11 13:16:20 +0530 | [diff] [blame] | 512 | cfg->m = parent_rate / cfreq; |
| 513 | cfg->n = cfg->output_rate / cfreq; |
| 514 | cfg->cpcon = OUT_OF_TABLE_CPCON; |
| 515 | |
| 516 | if (cfg->m > divm_max(pll) || cfg->n > divn_max(pll) || |
Peter De Schrijver | dba4072 | 2013-04-03 17:40:36 +0300 | [diff] [blame] | 517 | (1 << p_div) > divp_max(pll) |
| 518 | || cfg->output_rate > pll->params->vco_max) { |
Prashant Gaikwad | 8f8f484 | 2013-01-11 13:16:20 +0530 | [diff] [blame] | 519 | return -EINVAL; |
| 520 | } |
| 521 | |
Thierry Reding | 00c674e | 2013-11-18 16:11:35 +0100 | [diff] [blame] | 522 | cfg->output_rate >>= p_div; |
| 523 | |
Peter De Schrijver | 053b525 | 2013-06-05 15:56:41 +0300 | [diff] [blame] | 524 | if (pll->params->pdiv_tohw) { |
| 525 | ret = _p_div_to_hw(hw, 1 << p_div); |
| 526 | if (ret < 0) |
| 527 | return ret; |
| 528 | else |
| 529 | cfg->p = ret; |
Peter De Schrijver | 0b6525a | 2013-04-03 17:40:39 +0300 | [diff] [blame] | 530 | } else |
| 531 | cfg->p = p_div; |
Peter De Schrijver | dba4072 | 2013-04-03 17:40:36 +0300 | [diff] [blame] | 532 | |
Prashant Gaikwad | 8f8f484 | 2013-01-11 13:16:20 +0530 | [diff] [blame] | 533 | return 0; |
| 534 | } |
| 535 | |
Rhyland Klein | d907f4b | 2015-06-18 17:28:24 -0400 | [diff] [blame] | 536 | /* |
| 537 | * SDM (Sigma Delta Modulator) divisor is 16-bit 2's complement signed number |
| 538 | * within (-2^12 ... 2^12-1) range. Represented in PLL data structure as |
| 539 | * unsigned 16-bit value, with "0" divisor mapped to 0xFFFF. Data "0" is used |
| 540 | * to indicate that SDM is disabled. |
| 541 | * |
| 542 | * Effective ndiv value when SDM is enabled: ndiv + 1/2 + sdm_din/2^13 |
| 543 | */ |
| 544 | static void clk_pll_set_sdm_data(struct clk_hw *hw, |
| 545 | struct tegra_clk_pll_freq_table *cfg) |
| 546 | { |
| 547 | struct tegra_clk_pll *pll = to_clk_pll(hw); |
| 548 | u32 val; |
| 549 | bool enabled; |
| 550 | |
| 551 | if (!pll->params->sdm_din_reg) |
| 552 | return; |
| 553 | |
| 554 | if (cfg->sdm_data) { |
| 555 | val = pll_readl_sdm_din(pll) & (~sdm_din_mask(pll)); |
| 556 | val |= sdin_data_to_din(cfg->sdm_data) & sdm_din_mask(pll); |
| 557 | pll_writel_sdm_din(val, pll); |
| 558 | } |
| 559 | |
| 560 | val = pll_readl_sdm_ctrl(pll); |
| 561 | enabled = (val & sdm_en_mask(pll)); |
| 562 | |
| 563 | if (cfg->sdm_data == 0 && enabled) |
| 564 | val &= ~pll->params->sdm_ctrl_en_mask; |
| 565 | |
| 566 | if (cfg->sdm_data != 0 && !enabled) |
| 567 | val |= pll->params->sdm_ctrl_en_mask; |
| 568 | |
| 569 | pll_writel_sdm_ctrl(val, pll); |
| 570 | } |
| 571 | |
Peter De Schrijver | dba4072 | 2013-04-03 17:40:36 +0300 | [diff] [blame] | 572 | static void _update_pll_mnp(struct tegra_clk_pll *pll, |
| 573 | struct tegra_clk_pll_freq_table *cfg) |
| 574 | { |
| 575 | u32 val; |
Peter De Schrijver | 408a24f | 2013-06-06 13:47:31 +0300 | [diff] [blame] | 576 | struct tegra_clk_pll_params *params = pll->params; |
| 577 | struct div_nmp *div_nmp = params->div_nmp; |
Peter De Schrijver | dba4072 | 2013-04-03 17:40:36 +0300 | [diff] [blame] | 578 | |
Rhyland Klein | 6929715 | 2015-06-18 17:28:29 -0400 | [diff] [blame] | 579 | if ((params->flags & (TEGRA_PLLM | TEGRA_PLLMB)) && |
Peter De Schrijver | 408a24f | 2013-06-06 13:47:31 +0300 | [diff] [blame] | 580 | (pll_override_readl(PMC_PLLP_WB0_OVERRIDE, pll) & |
| 581 | PMC_PLLP_WB0_OVERRIDE_PLLM_OVERRIDE)) { |
| 582 | val = pll_override_readl(params->pmc_divp_reg, pll); |
| 583 | val &= ~(divp_mask(pll) << div_nmp->override_divp_shift); |
| 584 | val |= cfg->p << div_nmp->override_divp_shift; |
| 585 | pll_override_writel(val, params->pmc_divp_reg, pll); |
Peter De Schrijver | dba4072 | 2013-04-03 17:40:36 +0300 | [diff] [blame] | 586 | |
Peter De Schrijver | 408a24f | 2013-06-06 13:47:31 +0300 | [diff] [blame] | 587 | val = pll_override_readl(params->pmc_divnm_reg, pll); |
| 588 | val &= ~(divm_mask(pll) << div_nmp->override_divm_shift) | |
| 589 | ~(divn_mask(pll) << div_nmp->override_divn_shift); |
| 590 | val |= (cfg->m << div_nmp->override_divm_shift) | |
| 591 | (cfg->n << div_nmp->override_divn_shift); |
| 592 | pll_override_writel(val, params->pmc_divnm_reg, pll); |
| 593 | } else { |
| 594 | val = pll_readl_base(pll); |
Peter De Schrijver | dba4072 | 2013-04-03 17:40:36 +0300 | [diff] [blame] | 595 | |
Thierry Reding | c61e4e7 | 2014-04-04 15:55:14 +0200 | [diff] [blame] | 596 | val &= ~(divm_mask_shifted(pll) | divn_mask_shifted(pll) | |
| 597 | divp_mask_shifted(pll)); |
Peter De Schrijver | 408a24f | 2013-06-06 13:47:31 +0300 | [diff] [blame] | 598 | |
Thierry Reding | c61e4e7 | 2014-04-04 15:55:14 +0200 | [diff] [blame] | 599 | val |= (cfg->m << divm_shift(pll)) | |
| 600 | (cfg->n << divn_shift(pll)) | |
| 601 | (cfg->p << divp_shift(pll)); |
Peter De Schrijver | 408a24f | 2013-06-06 13:47:31 +0300 | [diff] [blame] | 602 | |
| 603 | pll_writel_base(val, pll); |
Rhyland Klein | d907f4b | 2015-06-18 17:28:24 -0400 | [diff] [blame] | 604 | |
| 605 | clk_pll_set_sdm_data(&pll->hw, cfg); |
Peter De Schrijver | 408a24f | 2013-06-06 13:47:31 +0300 | [diff] [blame] | 606 | } |
Peter De Schrijver | dba4072 | 2013-04-03 17:40:36 +0300 | [diff] [blame] | 607 | } |
| 608 | |
| 609 | static void _get_pll_mnp(struct tegra_clk_pll *pll, |
| 610 | struct tegra_clk_pll_freq_table *cfg) |
| 611 | { |
| 612 | u32 val; |
Peter De Schrijver | 408a24f | 2013-06-06 13:47:31 +0300 | [diff] [blame] | 613 | struct tegra_clk_pll_params *params = pll->params; |
| 614 | struct div_nmp *div_nmp = params->div_nmp; |
Peter De Schrijver | dba4072 | 2013-04-03 17:40:36 +0300 | [diff] [blame] | 615 | |
Rhyland Klein | 6929715 | 2015-06-18 17:28:29 -0400 | [diff] [blame] | 616 | if ((params->flags & (TEGRA_PLLM | TEGRA_PLLMB)) && |
Peter De Schrijver | 408a24f | 2013-06-06 13:47:31 +0300 | [diff] [blame] | 617 | (pll_override_readl(PMC_PLLP_WB0_OVERRIDE, pll) & |
| 618 | PMC_PLLP_WB0_OVERRIDE_PLLM_OVERRIDE)) { |
| 619 | val = pll_override_readl(params->pmc_divp_reg, pll); |
| 620 | cfg->p = (val >> div_nmp->override_divp_shift) & divp_mask(pll); |
Peter De Schrijver | dba4072 | 2013-04-03 17:40:36 +0300 | [diff] [blame] | 621 | |
Peter De Schrijver | 408a24f | 2013-06-06 13:47:31 +0300 | [diff] [blame] | 622 | val = pll_override_readl(params->pmc_divnm_reg, pll); |
| 623 | cfg->m = (val >> div_nmp->override_divm_shift) & divm_mask(pll); |
| 624 | cfg->n = (val >> div_nmp->override_divn_shift) & divn_mask(pll); |
| 625 | } else { |
| 626 | val = pll_readl_base(pll); |
| 627 | |
| 628 | cfg->m = (val >> div_nmp->divm_shift) & divm_mask(pll); |
| 629 | cfg->n = (val >> div_nmp->divn_shift) & divn_mask(pll); |
| 630 | cfg->p = (val >> div_nmp->divp_shift) & divp_mask(pll); |
Rhyland Klein | d907f4b | 2015-06-18 17:28:24 -0400 | [diff] [blame] | 631 | |
| 632 | if (pll->params->sdm_din_reg) { |
| 633 | if (sdm_en_mask(pll) & pll_readl_sdm_ctrl(pll)) { |
| 634 | val = pll_readl_sdm_din(pll); |
| 635 | val &= sdm_din_mask(pll); |
| 636 | cfg->sdm_data = sdin_din_to_data(val); |
| 637 | } |
| 638 | } |
Peter De Schrijver | 408a24f | 2013-06-06 13:47:31 +0300 | [diff] [blame] | 639 | } |
Peter De Schrijver | dba4072 | 2013-04-03 17:40:36 +0300 | [diff] [blame] | 640 | } |
| 641 | |
| 642 | static void _update_pll_cpcon(struct tegra_clk_pll *pll, |
| 643 | struct tegra_clk_pll_freq_table *cfg, |
| 644 | unsigned long rate) |
| 645 | { |
| 646 | u32 val; |
| 647 | |
| 648 | val = pll_readl_misc(pll); |
| 649 | |
| 650 | val &= ~(PLL_MISC_CPCON_MASK << PLL_MISC_CPCON_SHIFT); |
| 651 | val |= cfg->cpcon << PLL_MISC_CPCON_SHIFT; |
| 652 | |
Peter De Schrijver | ebe142b | 2013-10-04 17:28:34 +0300 | [diff] [blame] | 653 | if (pll->params->flags & TEGRA_PLL_SET_LFCON) { |
Peter De Schrijver | dba4072 | 2013-04-03 17:40:36 +0300 | [diff] [blame] | 654 | val &= ~(PLL_MISC_LFCON_MASK << PLL_MISC_LFCON_SHIFT); |
| 655 | if (cfg->n >= PLLDU_LFCON_SET_DIVN) |
| 656 | val |= 1 << PLL_MISC_LFCON_SHIFT; |
Peter De Schrijver | ebe142b | 2013-10-04 17:28:34 +0300 | [diff] [blame] | 657 | } else if (pll->params->flags & TEGRA_PLL_SET_DCCON) { |
Peter De Schrijver | dba4072 | 2013-04-03 17:40:36 +0300 | [diff] [blame] | 658 | val &= ~(1 << PLL_MISC_DCCON_SHIFT); |
| 659 | if (rate >= (pll->params->vco_max >> 1)) |
| 660 | val |= 1 << PLL_MISC_DCCON_SHIFT; |
| 661 | } |
| 662 | |
| 663 | pll_writel_misc(val, pll); |
| 664 | } |
| 665 | |
Bill Huang | 0ef9db6 | 2015-06-18 17:28:33 -0400 | [diff] [blame] | 666 | static void pll_clk_start_ss(struct tegra_clk_pll *pll) |
| 667 | { |
| 668 | if (pll->params->defaults_set && pll->params->ssc_ctrl_reg) { |
| 669 | u32 val = pll_readl(pll->params->ssc_ctrl_reg, pll); |
| 670 | |
| 671 | val |= pll->params->ssc_ctrl_en_mask; |
| 672 | pll_writel(val, pll->params->ssc_ctrl_reg, pll); |
| 673 | } |
| 674 | } |
| 675 | |
| 676 | static void pll_clk_stop_ss(struct tegra_clk_pll *pll) |
| 677 | { |
| 678 | if (pll->params->defaults_set && pll->params->ssc_ctrl_reg) { |
| 679 | u32 val = pll_readl(pll->params->ssc_ctrl_reg, pll); |
| 680 | |
| 681 | val &= ~pll->params->ssc_ctrl_en_mask; |
| 682 | pll_writel(val, pll->params->ssc_ctrl_reg, pll); |
| 683 | } |
| 684 | } |
| 685 | |
Prashant Gaikwad | 8f8f484 | 2013-01-11 13:16:20 +0530 | [diff] [blame] | 686 | static int _program_pll(struct clk_hw *hw, struct tegra_clk_pll_freq_table *cfg, |
| 687 | unsigned long rate) |
| 688 | { |
| 689 | struct tegra_clk_pll *pll = to_clk_pll(hw); |
Bill Huang | b985114 | 2015-06-18 17:28:31 -0400 | [diff] [blame] | 690 | struct tegra_clk_pll_freq_table old_cfg; |
Peter De Schrijver | dba4072 | 2013-04-03 17:40:36 +0300 | [diff] [blame] | 691 | int state, ret = 0; |
Prashant Gaikwad | 8f8f484 | 2013-01-11 13:16:20 +0530 | [diff] [blame] | 692 | |
| 693 | state = clk_pll_is_enabled(hw); |
| 694 | |
Bill Huang | b985114 | 2015-06-18 17:28:31 -0400 | [diff] [blame] | 695 | _get_pll_mnp(pll, &old_cfg); |
| 696 | |
Rhyland Klein | 17e9273 | 2015-06-18 17:28:32 -0400 | [diff] [blame] | 697 | if (state && pll->params->defaults_set && pll->params->dyn_ramp && |
| 698 | (cfg->m == old_cfg.m) && (cfg->p == old_cfg.p)) { |
| 699 | ret = pll->params->dyn_ramp(pll, cfg); |
| 700 | if (!ret) |
| 701 | return 0; |
| 702 | } |
| 703 | |
Bill Huang | 0ef9db6 | 2015-06-18 17:28:33 -0400 | [diff] [blame] | 704 | if (state) { |
| 705 | pll_clk_stop_ss(pll); |
Peter De Schrijver | dba4072 | 2013-04-03 17:40:36 +0300 | [diff] [blame] | 706 | _clk_pll_disable(hw); |
Bill Huang | 0ef9db6 | 2015-06-18 17:28:33 -0400 | [diff] [blame] | 707 | } |
Prashant Gaikwad | 8f8f484 | 2013-01-11 13:16:20 +0530 | [diff] [blame] | 708 | |
Bill Huang | b985114 | 2015-06-18 17:28:31 -0400 | [diff] [blame] | 709 | if (!pll->params->defaults_set && pll->params->set_defaults) |
| 710 | pll->params->set_defaults(pll); |
| 711 | |
Peter De Schrijver | dba4072 | 2013-04-03 17:40:36 +0300 | [diff] [blame] | 712 | _update_pll_mnp(pll, cfg); |
| 713 | |
Peter De Schrijver | ebe142b | 2013-10-04 17:28:34 +0300 | [diff] [blame] | 714 | if (pll->params->flags & TEGRA_PLL_HAS_CPCON) |
Peter De Schrijver | dba4072 | 2013-04-03 17:40:36 +0300 | [diff] [blame] | 715 | _update_pll_cpcon(pll, cfg, rate); |
| 716 | |
| 717 | if (state) { |
| 718 | _clk_pll_enable(hw); |
| 719 | ret = clk_pll_wait_for_lock(pll); |
Bill Huang | 0ef9db6 | 2015-06-18 17:28:33 -0400 | [diff] [blame] | 720 | pll_clk_start_ss(pll); |
Peter De Schrijver | dba4072 | 2013-04-03 17:40:36 +0300 | [diff] [blame] | 721 | } |
| 722 | |
| 723 | return ret; |
Prashant Gaikwad | 8f8f484 | 2013-01-11 13:16:20 +0530 | [diff] [blame] | 724 | } |
| 725 | |
| 726 | static int clk_pll_set_rate(struct clk_hw *hw, unsigned long rate, |
| 727 | unsigned long parent_rate) |
| 728 | { |
| 729 | struct tegra_clk_pll *pll = to_clk_pll(hw); |
Peter De Schrijver | dba4072 | 2013-04-03 17:40:36 +0300 | [diff] [blame] | 730 | struct tegra_clk_pll_freq_table cfg, old_cfg; |
| 731 | unsigned long flags = 0; |
| 732 | int ret = 0; |
Prashant Gaikwad | 8f8f484 | 2013-01-11 13:16:20 +0530 | [diff] [blame] | 733 | |
Peter De Schrijver | ebe142b | 2013-10-04 17:28:34 +0300 | [diff] [blame] | 734 | if (pll->params->flags & TEGRA_PLL_FIXED) { |
| 735 | if (rate != pll->params->fixed_rate) { |
Prashant Gaikwad | 8f8f484 | 2013-01-11 13:16:20 +0530 | [diff] [blame] | 736 | pr_err("%s: Can not change %s fixed rate %lu to %lu\n", |
Stephen Boyd | 836ee0f | 2015-08-12 11:42:23 -0700 | [diff] [blame] | 737 | __func__, clk_hw_get_name(hw), |
Peter De Schrijver | ebe142b | 2013-10-04 17:28:34 +0300 | [diff] [blame] | 738 | pll->params->fixed_rate, rate); |
Prashant Gaikwad | 8f8f484 | 2013-01-11 13:16:20 +0530 | [diff] [blame] | 739 | return -EINVAL; |
| 740 | } |
| 741 | return 0; |
| 742 | } |
| 743 | |
| 744 | if (_get_table_rate(hw, &cfg, rate, parent_rate) && |
Rhyland Klein | 407254d | 2015-06-18 17:28:25 -0400 | [diff] [blame] | 745 | pll->params->calc_rate(hw, &cfg, rate, parent_rate)) { |
Thierry Reding | 8ba4b3b | 2013-11-27 17:26:03 +0100 | [diff] [blame] | 746 | pr_err("%s: Failed to set %s rate %lu\n", __func__, |
Stephen Boyd | 836ee0f | 2015-08-12 11:42:23 -0700 | [diff] [blame] | 747 | clk_hw_get_name(hw), rate); |
Peter De Schrijver | 053b525 | 2013-06-05 15:56:41 +0300 | [diff] [blame] | 748 | WARN_ON(1); |
Prashant Gaikwad | 8f8f484 | 2013-01-11 13:16:20 +0530 | [diff] [blame] | 749 | return -EINVAL; |
Peter De Schrijver | 053b525 | 2013-06-05 15:56:41 +0300 | [diff] [blame] | 750 | } |
Peter De Schrijver | dba4072 | 2013-04-03 17:40:36 +0300 | [diff] [blame] | 751 | if (pll->lock) |
| 752 | spin_lock_irqsave(pll->lock, flags); |
| 753 | |
| 754 | _get_pll_mnp(pll, &old_cfg); |
Andrew Bresticker | afff455 | 2015-06-18 17:28:37 -0400 | [diff] [blame^] | 755 | if (pll->params->flags & TEGRA_PLL_VCO_OUT) |
| 756 | cfg.p = old_cfg.p; |
Peter De Schrijver | dba4072 | 2013-04-03 17:40:36 +0300 | [diff] [blame] | 757 | |
Rhyland Klein | d907f4b | 2015-06-18 17:28:24 -0400 | [diff] [blame] | 758 | if (old_cfg.m != cfg.m || old_cfg.n != cfg.n || old_cfg.p != cfg.p || |
| 759 | old_cfg.sdm_data != cfg.sdm_data) |
Peter De Schrijver | dba4072 | 2013-04-03 17:40:36 +0300 | [diff] [blame] | 760 | ret = _program_pll(hw, &cfg, rate); |
| 761 | |
| 762 | if (pll->lock) |
| 763 | spin_unlock_irqrestore(pll->lock, flags); |
| 764 | |
| 765 | return ret; |
Prashant Gaikwad | 8f8f484 | 2013-01-11 13:16:20 +0530 | [diff] [blame] | 766 | } |
| 767 | |
| 768 | static long clk_pll_round_rate(struct clk_hw *hw, unsigned long rate, |
| 769 | unsigned long *prate) |
| 770 | { |
| 771 | struct tegra_clk_pll *pll = to_clk_pll(hw); |
| 772 | struct tegra_clk_pll_freq_table cfg; |
Prashant Gaikwad | 8f8f484 | 2013-01-11 13:16:20 +0530 | [diff] [blame] | 773 | |
Danny Huang | 267b62a | 2015-06-18 17:28:27 -0400 | [diff] [blame] | 774 | if (pll->params->flags & TEGRA_PLL_FIXED) { |
Rhyland Klein | 6929715 | 2015-06-18 17:28:29 -0400 | [diff] [blame] | 775 | /* PLLM/MB are used for memory; we do not change rate */ |
| 776 | if (pll->params->flags & (TEGRA_PLLM | TEGRA_PLLMB)) |
Danny Huang | 267b62a | 2015-06-18 17:28:27 -0400 | [diff] [blame] | 777 | return clk_hw_get_rate(hw); |
Peter De Schrijver | ebe142b | 2013-10-04 17:28:34 +0300 | [diff] [blame] | 778 | return pll->params->fixed_rate; |
Danny Huang | 267b62a | 2015-06-18 17:28:27 -0400 | [diff] [blame] | 779 | } |
Prashant Gaikwad | 8f8f484 | 2013-01-11 13:16:20 +0530 | [diff] [blame] | 780 | |
| 781 | if (_get_table_rate(hw, &cfg, rate, *prate) && |
Rhyland Klein | 407254d | 2015-06-18 17:28:25 -0400 | [diff] [blame] | 782 | pll->params->calc_rate(hw, &cfg, rate, *prate)) |
Prashant Gaikwad | 8f8f484 | 2013-01-11 13:16:20 +0530 | [diff] [blame] | 783 | return -EINVAL; |
| 784 | |
Peter De Schrijver | 053b525 | 2013-06-05 15:56:41 +0300 | [diff] [blame] | 785 | return cfg.output_rate; |
Prashant Gaikwad | 8f8f484 | 2013-01-11 13:16:20 +0530 | [diff] [blame] | 786 | } |
| 787 | |
| 788 | static unsigned long clk_pll_recalc_rate(struct clk_hw *hw, |
| 789 | unsigned long parent_rate) |
| 790 | { |
| 791 | struct tegra_clk_pll *pll = to_clk_pll(hw); |
Peter De Schrijver | dba4072 | 2013-04-03 17:40:36 +0300 | [diff] [blame] | 792 | struct tegra_clk_pll_freq_table cfg; |
| 793 | u32 val; |
Prashant Gaikwad | 8f8f484 | 2013-01-11 13:16:20 +0530 | [diff] [blame] | 794 | u64 rate = parent_rate; |
Peter De Schrijver | 0b6525a | 2013-04-03 17:40:39 +0300 | [diff] [blame] | 795 | int pdiv; |
Prashant Gaikwad | 8f8f484 | 2013-01-11 13:16:20 +0530 | [diff] [blame] | 796 | |
Peter De Schrijver | dba4072 | 2013-04-03 17:40:36 +0300 | [diff] [blame] | 797 | val = pll_readl_base(pll); |
| 798 | |
Peter De Schrijver | ebe142b | 2013-10-04 17:28:34 +0300 | [diff] [blame] | 799 | if ((pll->params->flags & TEGRA_PLL_BYPASS) && (val & PLL_BASE_BYPASS)) |
Prashant Gaikwad | 8f8f484 | 2013-01-11 13:16:20 +0530 | [diff] [blame] | 800 | return parent_rate; |
| 801 | |
Peter De Schrijver | ebe142b | 2013-10-04 17:28:34 +0300 | [diff] [blame] | 802 | if ((pll->params->flags & TEGRA_PLL_FIXED) && |
Rhyland Klein | 6929715 | 2015-06-18 17:28:29 -0400 | [diff] [blame] | 803 | !(pll->params->flags & (TEGRA_PLLM | TEGRA_PLLMB)) && |
Peter De Schrijver | ebe142b | 2013-10-04 17:28:34 +0300 | [diff] [blame] | 804 | !(val & PLL_BASE_OVERRIDE)) { |
Prashant Gaikwad | 8f8f484 | 2013-01-11 13:16:20 +0530 | [diff] [blame] | 805 | struct tegra_clk_pll_freq_table sel; |
Peter De Schrijver | ebe142b | 2013-10-04 17:28:34 +0300 | [diff] [blame] | 806 | if (_get_table_rate(hw, &sel, pll->params->fixed_rate, |
| 807 | parent_rate)) { |
Prashant Gaikwad | 8f8f484 | 2013-01-11 13:16:20 +0530 | [diff] [blame] | 808 | pr_err("Clock %s has unknown fixed frequency\n", |
Stephen Boyd | 836ee0f | 2015-08-12 11:42:23 -0700 | [diff] [blame] | 809 | clk_hw_get_name(hw)); |
Prashant Gaikwad | 8f8f484 | 2013-01-11 13:16:20 +0530 | [diff] [blame] | 810 | BUG(); |
| 811 | } |
Peter De Schrijver | ebe142b | 2013-10-04 17:28:34 +0300 | [diff] [blame] | 812 | return pll->params->fixed_rate; |
Prashant Gaikwad | 8f8f484 | 2013-01-11 13:16:20 +0530 | [diff] [blame] | 813 | } |
| 814 | |
Peter De Schrijver | dba4072 | 2013-04-03 17:40:36 +0300 | [diff] [blame] | 815 | _get_pll_mnp(pll, &cfg); |
| 816 | |
Andrew Bresticker | afff455 | 2015-06-18 17:28:37 -0400 | [diff] [blame^] | 817 | if (pll->params->flags & TEGRA_PLL_VCO_OUT) { |
Peter De Schrijver | 053b525 | 2013-06-05 15:56:41 +0300 | [diff] [blame] | 818 | pdiv = 1; |
Andrew Bresticker | afff455 | 2015-06-18 17:28:37 -0400 | [diff] [blame^] | 819 | } else { |
| 820 | pdiv = _hw_to_p_div(hw, cfg.p); |
| 821 | if (pdiv < 0) { |
| 822 | WARN(1, "Clock %s has invalid pdiv value : 0x%x\n", |
| 823 | clk_hw_get_name(hw), cfg.p); |
| 824 | pdiv = 1; |
| 825 | } |
Peter De Schrijver | 053b525 | 2013-06-05 15:56:41 +0300 | [diff] [blame] | 826 | } |
Peter De Schrijver | 0b6525a | 2013-04-03 17:40:39 +0300 | [diff] [blame] | 827 | |
Rhyland Klein | d907f4b | 2015-06-18 17:28:24 -0400 | [diff] [blame] | 828 | if (pll->params->set_gain) |
| 829 | pll->params->set_gain(&cfg); |
| 830 | |
Peter De Schrijver | 0b6525a | 2013-04-03 17:40:39 +0300 | [diff] [blame] | 831 | cfg.m *= pdiv; |
Prashant Gaikwad | 8f8f484 | 2013-01-11 13:16:20 +0530 | [diff] [blame] | 832 | |
Peter De Schrijver | dba4072 | 2013-04-03 17:40:36 +0300 | [diff] [blame] | 833 | rate *= cfg.n; |
| 834 | do_div(rate, cfg.m); |
| 835 | |
Prashant Gaikwad | 8f8f484 | 2013-01-11 13:16:20 +0530 | [diff] [blame] | 836 | return rate; |
| 837 | } |
| 838 | |
| 839 | static int clk_plle_training(struct tegra_clk_pll *pll) |
| 840 | { |
| 841 | u32 val; |
| 842 | unsigned long timeout; |
| 843 | |
| 844 | if (!pll->pmc) |
| 845 | return -ENOSYS; |
| 846 | |
| 847 | /* |
| 848 | * PLLE is already disabled, and setup cleared; |
| 849 | * create falling edge on PLLE IDDQ input. |
| 850 | */ |
| 851 | val = readl(pll->pmc + PMC_SATA_PWRGT); |
| 852 | val |= PMC_SATA_PWRGT_PLLE_IDDQ_VALUE; |
| 853 | writel(val, pll->pmc + PMC_SATA_PWRGT); |
| 854 | |
| 855 | val = readl(pll->pmc + PMC_SATA_PWRGT); |
| 856 | val |= PMC_SATA_PWRGT_PLLE_IDDQ_SWCTL; |
| 857 | writel(val, pll->pmc + PMC_SATA_PWRGT); |
| 858 | |
| 859 | val = readl(pll->pmc + PMC_SATA_PWRGT); |
| 860 | val &= ~PMC_SATA_PWRGT_PLLE_IDDQ_VALUE; |
| 861 | writel(val, pll->pmc + PMC_SATA_PWRGT); |
| 862 | |
| 863 | val = pll_readl_misc(pll); |
| 864 | |
| 865 | timeout = jiffies + msecs_to_jiffies(100); |
| 866 | while (1) { |
| 867 | val = pll_readl_misc(pll); |
| 868 | if (val & PLLE_MISC_READY) |
| 869 | break; |
| 870 | if (time_after(jiffies, timeout)) { |
| 871 | pr_err("%s: timeout waiting for PLLE\n", __func__); |
| 872 | return -EBUSY; |
| 873 | } |
| 874 | udelay(300); |
| 875 | } |
| 876 | |
| 877 | return 0; |
| 878 | } |
| 879 | |
| 880 | static int clk_plle_enable(struct clk_hw *hw) |
| 881 | { |
| 882 | struct tegra_clk_pll *pll = to_clk_pll(hw); |
| 883 | unsigned long input_rate = clk_get_rate(clk_get_parent(hw->clk)); |
| 884 | struct tegra_clk_pll_freq_table sel; |
| 885 | u32 val; |
| 886 | int err; |
| 887 | |
Peter De Schrijver | ebe142b | 2013-10-04 17:28:34 +0300 | [diff] [blame] | 888 | if (_get_table_rate(hw, &sel, pll->params->fixed_rate, input_rate)) |
Prashant Gaikwad | 8f8f484 | 2013-01-11 13:16:20 +0530 | [diff] [blame] | 889 | return -EINVAL; |
| 890 | |
| 891 | clk_pll_disable(hw); |
| 892 | |
| 893 | val = pll_readl_misc(pll); |
| 894 | val &= ~(PLLE_MISC_LOCK_ENABLE | PLLE_MISC_SETUP_MASK); |
| 895 | pll_writel_misc(val, pll); |
| 896 | |
| 897 | val = pll_readl_misc(pll); |
| 898 | if (!(val & PLLE_MISC_READY)) { |
| 899 | err = clk_plle_training(pll); |
| 900 | if (err) |
| 901 | return err; |
| 902 | } |
| 903 | |
Peter De Schrijver | ebe142b | 2013-10-04 17:28:34 +0300 | [diff] [blame] | 904 | if (pll->params->flags & TEGRA_PLLE_CONFIGURE) { |
Prashant Gaikwad | 8f8f484 | 2013-01-11 13:16:20 +0530 | [diff] [blame] | 905 | /* configure dividers */ |
| 906 | val = pll_readl_base(pll); |
Thierry Reding | c61e4e7 | 2014-04-04 15:55:14 +0200 | [diff] [blame] | 907 | val &= ~(divp_mask_shifted(pll) | divn_mask_shifted(pll) | |
| 908 | divm_mask_shifted(pll)); |
Thierry Reding | d0f02ce | 2014-04-04 15:55:13 +0200 | [diff] [blame] | 909 | val &= ~(PLLE_BASE_DIVCML_MASK << PLLE_BASE_DIVCML_SHIFT); |
Thierry Reding | c61e4e7 | 2014-04-04 15:55:14 +0200 | [diff] [blame] | 910 | val |= sel.m << divm_shift(pll); |
| 911 | val |= sel.n << divn_shift(pll); |
| 912 | val |= sel.p << divp_shift(pll); |
Prashant Gaikwad | 8f8f484 | 2013-01-11 13:16:20 +0530 | [diff] [blame] | 913 | val |= sel.cpcon << PLLE_BASE_DIVCML_SHIFT; |
| 914 | pll_writel_base(val, pll); |
| 915 | } |
| 916 | |
| 917 | val = pll_readl_misc(pll); |
| 918 | val |= PLLE_MISC_SETUP_VALUE; |
| 919 | val |= PLLE_MISC_LOCK_ENABLE; |
| 920 | pll_writel_misc(val, pll); |
| 921 | |
| 922 | val = readl(pll->clk_base + PLLE_SS_CTRL); |
Thierry Reding | d0f02ce | 2014-04-04 15:55:13 +0200 | [diff] [blame] | 923 | val &= ~PLLE_SS_COEFFICIENTS_MASK; |
Prashant Gaikwad | 8f8f484 | 2013-01-11 13:16:20 +0530 | [diff] [blame] | 924 | val |= PLLE_SS_DISABLE; |
| 925 | writel(val, pll->clk_base + PLLE_SS_CTRL); |
| 926 | |
Thierry Reding | 4ccc402 | 2014-04-04 15:55:15 +0200 | [diff] [blame] | 927 | val = pll_readl_base(pll); |
Prashant Gaikwad | 8f8f484 | 2013-01-11 13:16:20 +0530 | [diff] [blame] | 928 | val |= (PLL_BASE_BYPASS | PLL_BASE_ENABLE); |
| 929 | pll_writel_base(val, pll); |
| 930 | |
Peter De Schrijver | dba4072 | 2013-04-03 17:40:36 +0300 | [diff] [blame] | 931 | clk_pll_wait_for_lock(pll); |
| 932 | |
Prashant Gaikwad | 8f8f484 | 2013-01-11 13:16:20 +0530 | [diff] [blame] | 933 | return 0; |
| 934 | } |
| 935 | |
| 936 | static unsigned long clk_plle_recalc_rate(struct clk_hw *hw, |
| 937 | unsigned long parent_rate) |
| 938 | { |
| 939 | struct tegra_clk_pll *pll = to_clk_pll(hw); |
| 940 | u32 val = pll_readl_base(pll); |
| 941 | u32 divn = 0, divm = 0, divp = 0; |
| 942 | u64 rate = parent_rate; |
| 943 | |
Peter De Schrijver | aa6fefd | 2013-06-05 16:51:25 +0300 | [diff] [blame] | 944 | divp = (val >> pll->params->div_nmp->divp_shift) & (divp_mask(pll)); |
| 945 | divn = (val >> pll->params->div_nmp->divn_shift) & (divn_mask(pll)); |
| 946 | divm = (val >> pll->params->div_nmp->divm_shift) & (divm_mask(pll)); |
Prashant Gaikwad | 8f8f484 | 2013-01-11 13:16:20 +0530 | [diff] [blame] | 947 | divm *= divp; |
| 948 | |
| 949 | rate *= divn; |
| 950 | do_div(rate, divm); |
| 951 | return rate; |
| 952 | } |
| 953 | |
| 954 | const struct clk_ops tegra_clk_pll_ops = { |
| 955 | .is_enabled = clk_pll_is_enabled, |
| 956 | .enable = clk_pll_enable, |
| 957 | .disable = clk_pll_disable, |
| 958 | .recalc_rate = clk_pll_recalc_rate, |
| 959 | .round_rate = clk_pll_round_rate, |
| 960 | .set_rate = clk_pll_set_rate, |
| 961 | }; |
| 962 | |
| 963 | const struct clk_ops tegra_clk_plle_ops = { |
| 964 | .recalc_rate = clk_plle_recalc_rate, |
| 965 | .is_enabled = clk_pll_is_enabled, |
| 966 | .disable = clk_pll_disable, |
| 967 | .enable = clk_plle_enable, |
| 968 | }; |
| 969 | |
Peter De Schrijver | c1d1939 | 2013-04-03 17:40:41 +0300 | [diff] [blame] | 970 | static int _pll_fixed_mdiv(struct tegra_clk_pll_params *pll_params, |
| 971 | unsigned long parent_rate) |
| 972 | { |
Rhyland Klein | 407254d | 2015-06-18 17:28:25 -0400 | [diff] [blame] | 973 | u16 mdiv = parent_rate / pll_params->cf_min; |
| 974 | |
| 975 | if (pll_params->flags & TEGRA_MDIV_NEW) |
| 976 | return (!pll_params->mdiv_default ? mdiv : |
| 977 | min(mdiv, pll_params->mdiv_default)); |
| 978 | |
| 979 | if (pll_params->mdiv_default) |
| 980 | return pll_params->mdiv_default; |
| 981 | |
Peter De Schrijver | c1d1939 | 2013-04-03 17:40:41 +0300 | [diff] [blame] | 982 | if (parent_rate > pll_params->cf_max) |
| 983 | return 2; |
| 984 | else |
| 985 | return 1; |
| 986 | } |
| 987 | |
Rhyland Klein | 86c679a | 2015-06-18 17:28:34 -0400 | [diff] [blame] | 988 | static int _calc_dynamic_ramp_rate(struct clk_hw *hw, |
| 989 | struct tegra_clk_pll_freq_table *cfg, |
| 990 | unsigned long rate, unsigned long parent_rate) |
| 991 | { |
| 992 | struct tegra_clk_pll *pll = to_clk_pll(hw); |
| 993 | unsigned int p; |
| 994 | int p_div; |
| 995 | |
| 996 | if (!rate) |
| 997 | return -EINVAL; |
| 998 | |
| 999 | p = DIV_ROUND_UP(pll->params->vco_min, rate); |
| 1000 | cfg->m = _pll_fixed_mdiv(pll->params, parent_rate); |
| 1001 | cfg->output_rate = rate * p; |
| 1002 | cfg->n = cfg->output_rate * cfg->m / parent_rate; |
| 1003 | cfg->input_rate = parent_rate; |
| 1004 | |
| 1005 | p_div = _p_div_to_hw(hw, p); |
| 1006 | if (p_div < 0) |
| 1007 | return p_div; |
| 1008 | |
| 1009 | cfg->p = p_div; |
| 1010 | |
| 1011 | if (cfg->n > divn_max(pll) || cfg->output_rate > pll->params->vco_max) |
| 1012 | return -EINVAL; |
| 1013 | |
| 1014 | return 0; |
| 1015 | } |
| 1016 | |
| 1017 | #if defined(CONFIG_ARCH_TEGRA_114_SOC) || \ |
| 1018 | defined(CONFIG_ARCH_TEGRA_124_SOC) || \ |
Rhyland Klein | dd322f0 | 2015-06-18 17:28:28 -0400 | [diff] [blame] | 1019 | defined(CONFIG_ARCH_TEGRA_132_SOC) || \ |
| 1020 | defined(CONFIG_ARCH_TEGRA_210_SOC) |
Rhyland Klein | 86c679a | 2015-06-18 17:28:34 -0400 | [diff] [blame] | 1021 | |
Rhyland Klein | 407254d | 2015-06-18 17:28:25 -0400 | [diff] [blame] | 1022 | u16 tegra_pll_get_fixed_mdiv(struct clk_hw *hw, unsigned long input_rate) |
| 1023 | { |
| 1024 | struct tegra_clk_pll *pll = to_clk_pll(hw); |
| 1025 | |
| 1026 | return (u16)_pll_fixed_mdiv(pll->params, input_rate); |
| 1027 | } |
| 1028 | |
Peter De Schrijver | 04edb09 | 2013-09-06 14:37:37 +0300 | [diff] [blame] | 1029 | static unsigned long _clip_vco_min(unsigned long vco_min, |
| 1030 | unsigned long parent_rate) |
| 1031 | { |
| 1032 | return DIV_ROUND_UP(vco_min, parent_rate) * parent_rate; |
| 1033 | } |
| 1034 | |
| 1035 | static int _setup_dynamic_ramp(struct tegra_clk_pll_params *pll_params, |
| 1036 | void __iomem *clk_base, |
| 1037 | unsigned long parent_rate) |
| 1038 | { |
| 1039 | u32 val; |
| 1040 | u32 step_a, step_b; |
| 1041 | |
| 1042 | switch (parent_rate) { |
| 1043 | case 12000000: |
| 1044 | case 13000000: |
| 1045 | case 26000000: |
| 1046 | step_a = 0x2B; |
| 1047 | step_b = 0x0B; |
| 1048 | break; |
| 1049 | case 16800000: |
| 1050 | step_a = 0x1A; |
| 1051 | step_b = 0x09; |
| 1052 | break; |
| 1053 | case 19200000: |
| 1054 | step_a = 0x12; |
| 1055 | step_b = 0x08; |
| 1056 | break; |
| 1057 | default: |
| 1058 | pr_err("%s: Unexpected reference rate %lu\n", |
| 1059 | __func__, parent_rate); |
| 1060 | WARN_ON(1); |
| 1061 | return -EINVAL; |
| 1062 | } |
| 1063 | |
| 1064 | val = step_a << pll_params->stepa_shift; |
| 1065 | val |= step_b << pll_params->stepb_shift; |
| 1066 | writel_relaxed(val, clk_base + pll_params->dyn_ramp_reg); |
| 1067 | |
| 1068 | return 0; |
| 1069 | } |
| 1070 | |
Peter De Schrijver | c1d1939 | 2013-04-03 17:40:41 +0300 | [diff] [blame] | 1071 | static int _pll_ramp_calc_pll(struct clk_hw *hw, |
| 1072 | struct tegra_clk_pll_freq_table *cfg, |
| 1073 | unsigned long rate, unsigned long parent_rate) |
| 1074 | { |
| 1075 | struct tegra_clk_pll *pll = to_clk_pll(hw); |
Rhyland Klein | 86c679a | 2015-06-18 17:28:34 -0400 | [diff] [blame] | 1076 | int err = 0; |
Peter De Schrijver | c1d1939 | 2013-04-03 17:40:41 +0300 | [diff] [blame] | 1077 | |
| 1078 | err = _get_table_rate(hw, cfg, rate, parent_rate); |
| 1079 | if (err < 0) |
| 1080 | err = _calc_dynamic_ramp_rate(hw, cfg, rate, parent_rate); |
Peter De Schrijver | 053b525 | 2013-06-05 15:56:41 +0300 | [diff] [blame] | 1081 | else { |
| 1082 | if (cfg->m != _pll_fixed_mdiv(pll->params, parent_rate)) { |
Peter De Schrijver | c1d1939 | 2013-04-03 17:40:41 +0300 | [diff] [blame] | 1083 | WARN_ON(1); |
| 1084 | err = -EINVAL; |
| 1085 | goto out; |
Peter De Schrijver | 053b525 | 2013-06-05 15:56:41 +0300 | [diff] [blame] | 1086 | } |
Peter De Schrijver | c1d1939 | 2013-04-03 17:40:41 +0300 | [diff] [blame] | 1087 | } |
| 1088 | |
Peter De Schrijver | 053b525 | 2013-06-05 15:56:41 +0300 | [diff] [blame] | 1089 | if (cfg->p > pll->params->max_p) |
Peter De Schrijver | c1d1939 | 2013-04-03 17:40:41 +0300 | [diff] [blame] | 1090 | err = -EINVAL; |
| 1091 | |
| 1092 | out: |
| 1093 | return err; |
| 1094 | } |
| 1095 | |
| 1096 | static int clk_pllxc_set_rate(struct clk_hw *hw, unsigned long rate, |
| 1097 | unsigned long parent_rate) |
| 1098 | { |
| 1099 | struct tegra_clk_pll *pll = to_clk_pll(hw); |
| 1100 | struct tegra_clk_pll_freq_table cfg, old_cfg; |
| 1101 | unsigned long flags = 0; |
Thierry Reding | 44a6f3db | 2015-02-18 16:25:16 +0100 | [diff] [blame] | 1102 | int ret; |
Peter De Schrijver | c1d1939 | 2013-04-03 17:40:41 +0300 | [diff] [blame] | 1103 | |
| 1104 | ret = _pll_ramp_calc_pll(hw, &cfg, rate, parent_rate); |
| 1105 | if (ret < 0) |
| 1106 | return ret; |
| 1107 | |
| 1108 | if (pll->lock) |
| 1109 | spin_lock_irqsave(pll->lock, flags); |
| 1110 | |
| 1111 | _get_pll_mnp(pll, &old_cfg); |
Andrew Bresticker | afff455 | 2015-06-18 17:28:37 -0400 | [diff] [blame^] | 1112 | if (pll->params->flags & TEGRA_PLL_VCO_OUT) |
| 1113 | cfg.p = old_cfg.p; |
Peter De Schrijver | c1d1939 | 2013-04-03 17:40:41 +0300 | [diff] [blame] | 1114 | |
Peter De Schrijver | 053b525 | 2013-06-05 15:56:41 +0300 | [diff] [blame] | 1115 | if (old_cfg.m != cfg.m || old_cfg.n != cfg.n || old_cfg.p != cfg.p) |
Peter De Schrijver | c1d1939 | 2013-04-03 17:40:41 +0300 | [diff] [blame] | 1116 | ret = _program_pll(hw, &cfg, rate); |
Peter De Schrijver | c1d1939 | 2013-04-03 17:40:41 +0300 | [diff] [blame] | 1117 | |
| 1118 | if (pll->lock) |
| 1119 | spin_unlock_irqrestore(pll->lock, flags); |
| 1120 | |
| 1121 | return ret; |
| 1122 | } |
| 1123 | |
| 1124 | static long clk_pll_ramp_round_rate(struct clk_hw *hw, unsigned long rate, |
| 1125 | unsigned long *prate) |
| 1126 | { |
Rhyland Klein | d907f4b | 2015-06-18 17:28:24 -0400 | [diff] [blame] | 1127 | struct tegra_clk_pll *pll = to_clk_pll(hw); |
Peter De Schrijver | c1d1939 | 2013-04-03 17:40:41 +0300 | [diff] [blame] | 1128 | struct tegra_clk_pll_freq_table cfg; |
Thierry Reding | 44a6f3db | 2015-02-18 16:25:16 +0100 | [diff] [blame] | 1129 | int ret, p_div; |
Peter De Schrijver | c1d1939 | 2013-04-03 17:40:41 +0300 | [diff] [blame] | 1130 | u64 output_rate = *prate; |
| 1131 | |
| 1132 | ret = _pll_ramp_calc_pll(hw, &cfg, rate, *prate); |
| 1133 | if (ret < 0) |
| 1134 | return ret; |
| 1135 | |
Peter De Schrijver | 053b525 | 2013-06-05 15:56:41 +0300 | [diff] [blame] | 1136 | p_div = _hw_to_p_div(hw, cfg.p); |
| 1137 | if (p_div < 0) |
| 1138 | return p_div; |
| 1139 | |
Rhyland Klein | d907f4b | 2015-06-18 17:28:24 -0400 | [diff] [blame] | 1140 | if (pll->params->set_gain) |
| 1141 | pll->params->set_gain(&cfg); |
| 1142 | |
Peter De Schrijver | c1d1939 | 2013-04-03 17:40:41 +0300 | [diff] [blame] | 1143 | output_rate *= cfg.n; |
Peter De Schrijver | 053b525 | 2013-06-05 15:56:41 +0300 | [diff] [blame] | 1144 | do_div(output_rate, cfg.m * p_div); |
Peter De Schrijver | c1d1939 | 2013-04-03 17:40:41 +0300 | [diff] [blame] | 1145 | |
| 1146 | return output_rate; |
| 1147 | } |
| 1148 | |
Peter De Schrijver | c1d1939 | 2013-04-03 17:40:41 +0300 | [diff] [blame] | 1149 | static void _pllcx_strobe(struct tegra_clk_pll *pll) |
| 1150 | { |
| 1151 | u32 val; |
| 1152 | |
| 1153 | val = pll_readl_misc(pll); |
| 1154 | val |= PLLCX_MISC_STROBE; |
| 1155 | pll_writel_misc(val, pll); |
| 1156 | udelay(2); |
| 1157 | |
| 1158 | val &= ~PLLCX_MISC_STROBE; |
| 1159 | pll_writel_misc(val, pll); |
| 1160 | } |
| 1161 | |
| 1162 | static int clk_pllc_enable(struct clk_hw *hw) |
| 1163 | { |
| 1164 | struct tegra_clk_pll *pll = to_clk_pll(hw); |
| 1165 | u32 val; |
Thierry Reding | 44a6f3db | 2015-02-18 16:25:16 +0100 | [diff] [blame] | 1166 | int ret; |
Peter De Schrijver | c1d1939 | 2013-04-03 17:40:41 +0300 | [diff] [blame] | 1167 | unsigned long flags = 0; |
| 1168 | |
| 1169 | if (pll->lock) |
| 1170 | spin_lock_irqsave(pll->lock, flags); |
| 1171 | |
| 1172 | _clk_pll_enable(hw); |
| 1173 | udelay(2); |
| 1174 | |
| 1175 | val = pll_readl_misc(pll); |
| 1176 | val &= ~PLLCX_MISC_RESET; |
| 1177 | pll_writel_misc(val, pll); |
| 1178 | udelay(2); |
| 1179 | |
| 1180 | _pllcx_strobe(pll); |
| 1181 | |
| 1182 | ret = clk_pll_wait_for_lock(pll); |
| 1183 | |
| 1184 | if (pll->lock) |
| 1185 | spin_unlock_irqrestore(pll->lock, flags); |
| 1186 | |
| 1187 | return ret; |
| 1188 | } |
| 1189 | |
| 1190 | static void _clk_pllc_disable(struct clk_hw *hw) |
| 1191 | { |
| 1192 | struct tegra_clk_pll *pll = to_clk_pll(hw); |
| 1193 | u32 val; |
| 1194 | |
| 1195 | _clk_pll_disable(hw); |
| 1196 | |
| 1197 | val = pll_readl_misc(pll); |
| 1198 | val |= PLLCX_MISC_RESET; |
| 1199 | pll_writel_misc(val, pll); |
| 1200 | udelay(2); |
| 1201 | } |
| 1202 | |
| 1203 | static void clk_pllc_disable(struct clk_hw *hw) |
| 1204 | { |
| 1205 | struct tegra_clk_pll *pll = to_clk_pll(hw); |
| 1206 | unsigned long flags = 0; |
| 1207 | |
| 1208 | if (pll->lock) |
| 1209 | spin_lock_irqsave(pll->lock, flags); |
| 1210 | |
| 1211 | _clk_pllc_disable(hw); |
| 1212 | |
| 1213 | if (pll->lock) |
| 1214 | spin_unlock_irqrestore(pll->lock, flags); |
| 1215 | } |
| 1216 | |
| 1217 | static int _pllcx_update_dynamic_coef(struct tegra_clk_pll *pll, |
| 1218 | unsigned long input_rate, u32 n) |
| 1219 | { |
| 1220 | u32 val, n_threshold; |
| 1221 | |
| 1222 | switch (input_rate) { |
| 1223 | case 12000000: |
| 1224 | n_threshold = 70; |
| 1225 | break; |
| 1226 | case 13000000: |
| 1227 | case 26000000: |
| 1228 | n_threshold = 71; |
| 1229 | break; |
| 1230 | case 16800000: |
| 1231 | n_threshold = 55; |
| 1232 | break; |
| 1233 | case 19200000: |
| 1234 | n_threshold = 48; |
| 1235 | break; |
| 1236 | default: |
| 1237 | pr_err("%s: Unexpected reference rate %lu\n", |
| 1238 | __func__, input_rate); |
| 1239 | return -EINVAL; |
| 1240 | } |
| 1241 | |
| 1242 | val = pll_readl_misc(pll); |
| 1243 | val &= ~(PLLCX_MISC_SDM_DIV_MASK | PLLCX_MISC_FILT_DIV_MASK); |
| 1244 | val |= n <= n_threshold ? |
| 1245 | PLLCX_MISC_DIV_LOW_RANGE : PLLCX_MISC_DIV_HIGH_RANGE; |
| 1246 | pll_writel_misc(val, pll); |
| 1247 | |
| 1248 | return 0; |
| 1249 | } |
| 1250 | |
| 1251 | static int clk_pllc_set_rate(struct clk_hw *hw, unsigned long rate, |
| 1252 | unsigned long parent_rate) |
| 1253 | { |
Peter De Schrijver | 053b525 | 2013-06-05 15:56:41 +0300 | [diff] [blame] | 1254 | struct tegra_clk_pll_freq_table cfg, old_cfg; |
Peter De Schrijver | c1d1939 | 2013-04-03 17:40:41 +0300 | [diff] [blame] | 1255 | struct tegra_clk_pll *pll = to_clk_pll(hw); |
| 1256 | unsigned long flags = 0; |
| 1257 | int state, ret = 0; |
Peter De Schrijver | c1d1939 | 2013-04-03 17:40:41 +0300 | [diff] [blame] | 1258 | |
| 1259 | if (pll->lock) |
| 1260 | spin_lock_irqsave(pll->lock, flags); |
| 1261 | |
| 1262 | ret = _pll_ramp_calc_pll(hw, &cfg, rate, parent_rate); |
| 1263 | if (ret < 0) |
| 1264 | goto out; |
| 1265 | |
Peter De Schrijver | 053b525 | 2013-06-05 15:56:41 +0300 | [diff] [blame] | 1266 | _get_pll_mnp(pll, &old_cfg); |
Peter De Schrijver | c1d1939 | 2013-04-03 17:40:41 +0300 | [diff] [blame] | 1267 | |
Peter De Schrijver | 053b525 | 2013-06-05 15:56:41 +0300 | [diff] [blame] | 1268 | if (cfg.m != old_cfg.m) { |
Peter De Schrijver | c1d1939 | 2013-04-03 17:40:41 +0300 | [diff] [blame] | 1269 | WARN_ON(1); |
| 1270 | goto out; |
| 1271 | } |
| 1272 | |
Peter De Schrijver | 053b525 | 2013-06-05 15:56:41 +0300 | [diff] [blame] | 1273 | if (old_cfg.n == cfg.n && old_cfg.p == cfg.p) |
Peter De Schrijver | c1d1939 | 2013-04-03 17:40:41 +0300 | [diff] [blame] | 1274 | goto out; |
| 1275 | |
Peter De Schrijver | c1d1939 | 2013-04-03 17:40:41 +0300 | [diff] [blame] | 1276 | state = clk_pll_is_enabled(hw); |
| 1277 | if (state) |
| 1278 | _clk_pllc_disable(hw); |
| 1279 | |
| 1280 | ret = _pllcx_update_dynamic_coef(pll, parent_rate, cfg.n); |
| 1281 | if (ret < 0) |
| 1282 | goto out; |
| 1283 | |
| 1284 | _update_pll_mnp(pll, &cfg); |
| 1285 | |
| 1286 | if (state) |
| 1287 | ret = clk_pllc_enable(hw); |
| 1288 | |
| 1289 | out: |
| 1290 | if (pll->lock) |
| 1291 | spin_unlock_irqrestore(pll->lock, flags); |
| 1292 | |
| 1293 | return ret; |
| 1294 | } |
| 1295 | |
| 1296 | static long _pllre_calc_rate(struct tegra_clk_pll *pll, |
| 1297 | struct tegra_clk_pll_freq_table *cfg, |
| 1298 | unsigned long rate, unsigned long parent_rate) |
| 1299 | { |
| 1300 | u16 m, n; |
| 1301 | u64 output_rate = parent_rate; |
| 1302 | |
| 1303 | m = _pll_fixed_mdiv(pll->params, parent_rate); |
| 1304 | n = rate * m / parent_rate; |
| 1305 | |
| 1306 | output_rate *= n; |
| 1307 | do_div(output_rate, m); |
| 1308 | |
| 1309 | if (cfg) { |
| 1310 | cfg->m = m; |
| 1311 | cfg->n = n; |
| 1312 | } |
| 1313 | |
| 1314 | return output_rate; |
| 1315 | } |
Thierry Reding | 6bb18c5 | 2014-08-01 10:44:20 +0200 | [diff] [blame] | 1316 | |
Peter De Schrijver | c1d1939 | 2013-04-03 17:40:41 +0300 | [diff] [blame] | 1317 | static int clk_pllre_set_rate(struct clk_hw *hw, unsigned long rate, |
| 1318 | unsigned long parent_rate) |
| 1319 | { |
| 1320 | struct tegra_clk_pll_freq_table cfg, old_cfg; |
| 1321 | struct tegra_clk_pll *pll = to_clk_pll(hw); |
| 1322 | unsigned long flags = 0; |
| 1323 | int state, ret = 0; |
| 1324 | |
| 1325 | if (pll->lock) |
| 1326 | spin_lock_irqsave(pll->lock, flags); |
| 1327 | |
| 1328 | _pllre_calc_rate(pll, &cfg, rate, parent_rate); |
| 1329 | _get_pll_mnp(pll, &old_cfg); |
| 1330 | cfg.p = old_cfg.p; |
| 1331 | |
| 1332 | if (cfg.m != old_cfg.m || cfg.n != old_cfg.n) { |
| 1333 | state = clk_pll_is_enabled(hw); |
| 1334 | if (state) |
| 1335 | _clk_pll_disable(hw); |
| 1336 | |
| 1337 | _update_pll_mnp(pll, &cfg); |
| 1338 | |
| 1339 | if (state) { |
| 1340 | _clk_pll_enable(hw); |
| 1341 | ret = clk_pll_wait_for_lock(pll); |
| 1342 | } |
| 1343 | } |
| 1344 | |
| 1345 | if (pll->lock) |
| 1346 | spin_unlock_irqrestore(pll->lock, flags); |
| 1347 | |
| 1348 | return ret; |
| 1349 | } |
| 1350 | |
| 1351 | static unsigned long clk_pllre_recalc_rate(struct clk_hw *hw, |
| 1352 | unsigned long parent_rate) |
| 1353 | { |
| 1354 | struct tegra_clk_pll_freq_table cfg; |
| 1355 | struct tegra_clk_pll *pll = to_clk_pll(hw); |
| 1356 | u64 rate = parent_rate; |
| 1357 | |
| 1358 | _get_pll_mnp(pll, &cfg); |
| 1359 | |
| 1360 | rate *= cfg.n; |
| 1361 | do_div(rate, cfg.m); |
| 1362 | |
| 1363 | return rate; |
| 1364 | } |
| 1365 | |
| 1366 | static long clk_pllre_round_rate(struct clk_hw *hw, unsigned long rate, |
| 1367 | unsigned long *prate) |
| 1368 | { |
| 1369 | struct tegra_clk_pll *pll = to_clk_pll(hw); |
| 1370 | |
| 1371 | return _pllre_calc_rate(pll, NULL, rate, *prate); |
| 1372 | } |
| 1373 | |
| 1374 | static int clk_plle_tegra114_enable(struct clk_hw *hw) |
| 1375 | { |
| 1376 | struct tegra_clk_pll *pll = to_clk_pll(hw); |
| 1377 | struct tegra_clk_pll_freq_table sel; |
| 1378 | u32 val; |
| 1379 | int ret; |
| 1380 | unsigned long flags = 0; |
| 1381 | unsigned long input_rate = clk_get_rate(clk_get_parent(hw->clk)); |
| 1382 | |
Peter De Schrijver | ebe142b | 2013-10-04 17:28:34 +0300 | [diff] [blame] | 1383 | if (_get_table_rate(hw, &sel, pll->params->fixed_rate, input_rate)) |
Peter De Schrijver | c1d1939 | 2013-04-03 17:40:41 +0300 | [diff] [blame] | 1384 | return -EINVAL; |
| 1385 | |
| 1386 | if (pll->lock) |
| 1387 | spin_lock_irqsave(pll->lock, flags); |
| 1388 | |
| 1389 | val = pll_readl_base(pll); |
| 1390 | val &= ~BIT(29); /* Disable lock override */ |
| 1391 | pll_writel_base(val, pll); |
| 1392 | |
| 1393 | val = pll_readl(pll->params->aux_reg, pll); |
| 1394 | val |= PLLE_AUX_ENABLE_SWCTL; |
| 1395 | val &= ~PLLE_AUX_SEQ_ENABLE; |
| 1396 | pll_writel(val, pll->params->aux_reg, pll); |
| 1397 | udelay(1); |
| 1398 | |
| 1399 | val = pll_readl_misc(pll); |
| 1400 | val |= PLLE_MISC_LOCK_ENABLE; |
| 1401 | val |= PLLE_MISC_IDDQ_SW_CTRL; |
| 1402 | val &= ~PLLE_MISC_IDDQ_SW_VALUE; |
| 1403 | val |= PLLE_MISC_PLLE_PTS; |
| 1404 | val |= PLLE_MISC_VREG_BG_CTRL_MASK | PLLE_MISC_VREG_CTRL_MASK; |
| 1405 | pll_writel_misc(val, pll); |
| 1406 | udelay(5); |
| 1407 | |
| 1408 | val = pll_readl(PLLE_SS_CTRL, pll); |
| 1409 | val |= PLLE_SS_DISABLE; |
| 1410 | pll_writel(val, PLLE_SS_CTRL, pll); |
| 1411 | |
| 1412 | val = pll_readl_base(pll); |
Thierry Reding | c61e4e7 | 2014-04-04 15:55:14 +0200 | [diff] [blame] | 1413 | val &= ~(divp_mask_shifted(pll) | divn_mask_shifted(pll) | |
| 1414 | divm_mask_shifted(pll)); |
Thierry Reding | d0f02ce | 2014-04-04 15:55:13 +0200 | [diff] [blame] | 1415 | val &= ~(PLLE_BASE_DIVCML_MASK << PLLE_BASE_DIVCML_SHIFT); |
Thierry Reding | c61e4e7 | 2014-04-04 15:55:14 +0200 | [diff] [blame] | 1416 | val |= sel.m << divm_shift(pll); |
| 1417 | val |= sel.n << divn_shift(pll); |
Peter De Schrijver | c1d1939 | 2013-04-03 17:40:41 +0300 | [diff] [blame] | 1418 | val |= sel.cpcon << PLLE_BASE_DIVCML_SHIFT; |
| 1419 | pll_writel_base(val, pll); |
| 1420 | udelay(1); |
| 1421 | |
| 1422 | _clk_pll_enable(hw); |
| 1423 | ret = clk_pll_wait_for_lock(pll); |
| 1424 | |
| 1425 | if (ret < 0) |
| 1426 | goto out; |
| 1427 | |
Peter De Schrijver | 642fb0c | 2013-09-26 18:30:01 +0300 | [diff] [blame] | 1428 | val = pll_readl(PLLE_SS_CTRL, pll); |
| 1429 | val &= ~(PLLE_SS_CNTL_CENTER | PLLE_SS_CNTL_INVERT); |
| 1430 | val &= ~PLLE_SS_COEFFICIENTS_MASK; |
| 1431 | val |= PLLE_SS_COEFFICIENTS_VAL; |
| 1432 | pll_writel(val, PLLE_SS_CTRL, pll); |
| 1433 | val &= ~(PLLE_SS_CNTL_SSC_BYP | PLLE_SS_CNTL_BYPASS_SS); |
| 1434 | pll_writel(val, PLLE_SS_CTRL, pll); |
| 1435 | udelay(1); |
| 1436 | val &= ~PLLE_SS_CNTL_INTERP_RESET; |
| 1437 | pll_writel(val, PLLE_SS_CTRL, pll); |
| 1438 | udelay(1); |
| 1439 | |
Jim Lin | 2cfe167 | 2014-05-14 17:32:57 -0700 | [diff] [blame] | 1440 | /* Enable hw control of xusb brick pll */ |
| 1441 | val = pll_readl_misc(pll); |
| 1442 | val &= ~PLLE_MISC_IDDQ_SW_CTRL; |
| 1443 | pll_writel_misc(val, pll); |
| 1444 | |
| 1445 | val = pll_readl(pll->params->aux_reg, pll); |
| 1446 | val |= (PLLE_AUX_USE_LOCKDET | PLLE_AUX_SEQ_START_STATE); |
| 1447 | val &= ~(PLLE_AUX_ENABLE_SWCTL | PLLE_AUX_SS_SWCTL); |
| 1448 | pll_writel(val, pll->params->aux_reg, pll); |
| 1449 | udelay(1); |
| 1450 | val |= PLLE_AUX_SEQ_ENABLE; |
| 1451 | pll_writel(val, pll->params->aux_reg, pll); |
| 1452 | |
| 1453 | val = pll_readl(XUSBIO_PLL_CFG0, pll); |
| 1454 | val |= (XUSBIO_PLL_CFG0_PADPLL_USE_LOCKDET | |
| 1455 | XUSBIO_PLL_CFG0_SEQ_START_STATE); |
| 1456 | val &= ~(XUSBIO_PLL_CFG0_CLK_ENABLE_SWCTL | |
| 1457 | XUSBIO_PLL_CFG0_PADPLL_RESET_SWCTL); |
| 1458 | pll_writel(val, XUSBIO_PLL_CFG0, pll); |
| 1459 | udelay(1); |
| 1460 | val |= XUSBIO_PLL_CFG0_SEQ_ENABLE; |
| 1461 | pll_writel(val, XUSBIO_PLL_CFG0, pll); |
Peter De Schrijver | c1d1939 | 2013-04-03 17:40:41 +0300 | [diff] [blame] | 1462 | |
Mikko Perttunen | 37ab366 | 2014-06-18 17:23:23 +0300 | [diff] [blame] | 1463 | /* Enable hw control of SATA pll */ |
| 1464 | val = pll_readl(SATA_PLL_CFG0, pll); |
| 1465 | val &= ~SATA_PLL_CFG0_PADPLL_RESET_SWCTL; |
Mikko Perttunen | 0e548d50b | 2014-07-08 09:30:15 +0200 | [diff] [blame] | 1466 | val |= SATA_PLL_CFG0_PADPLL_USE_LOCKDET; |
| 1467 | val |= SATA_PLL_CFG0_SEQ_START_STATE; |
| 1468 | pll_writel(val, SATA_PLL_CFG0, pll); |
| 1469 | |
| 1470 | udelay(1); |
| 1471 | |
| 1472 | val = pll_readl(SATA_PLL_CFG0, pll); |
| 1473 | val |= SATA_PLL_CFG0_SEQ_ENABLE; |
Mikko Perttunen | 37ab366 | 2014-06-18 17:23:23 +0300 | [diff] [blame] | 1474 | pll_writel(val, SATA_PLL_CFG0, pll); |
| 1475 | |
Peter De Schrijver | c1d1939 | 2013-04-03 17:40:41 +0300 | [diff] [blame] | 1476 | out: |
| 1477 | if (pll->lock) |
| 1478 | spin_unlock_irqrestore(pll->lock, flags); |
| 1479 | |
| 1480 | return ret; |
| 1481 | } |
| 1482 | |
| 1483 | static void clk_plle_tegra114_disable(struct clk_hw *hw) |
| 1484 | { |
| 1485 | struct tegra_clk_pll *pll = to_clk_pll(hw); |
| 1486 | unsigned long flags = 0; |
| 1487 | u32 val; |
| 1488 | |
| 1489 | if (pll->lock) |
| 1490 | spin_lock_irqsave(pll->lock, flags); |
| 1491 | |
| 1492 | _clk_pll_disable(hw); |
| 1493 | |
| 1494 | val = pll_readl_misc(pll); |
| 1495 | val |= PLLE_MISC_IDDQ_SW_CTRL | PLLE_MISC_IDDQ_SW_VALUE; |
| 1496 | pll_writel_misc(val, pll); |
| 1497 | udelay(1); |
| 1498 | |
| 1499 | if (pll->lock) |
| 1500 | spin_unlock_irqrestore(pll->lock, flags); |
| 1501 | } |
| 1502 | #endif |
| 1503 | |
Peter De Schrijver | dba4072 | 2013-04-03 17:40:36 +0300 | [diff] [blame] | 1504 | static struct tegra_clk_pll *_tegra_init_pll(void __iomem *clk_base, |
Peter De Schrijver | ebe142b | 2013-10-04 17:28:34 +0300 | [diff] [blame] | 1505 | void __iomem *pmc, struct tegra_clk_pll_params *pll_params, |
| 1506 | spinlock_t *lock) |
Prashant Gaikwad | 8f8f484 | 2013-01-11 13:16:20 +0530 | [diff] [blame] | 1507 | { |
| 1508 | struct tegra_clk_pll *pll; |
Prashant Gaikwad | 8f8f484 | 2013-01-11 13:16:20 +0530 | [diff] [blame] | 1509 | |
| 1510 | pll = kzalloc(sizeof(*pll), GFP_KERNEL); |
| 1511 | if (!pll) |
| 1512 | return ERR_PTR(-ENOMEM); |
| 1513 | |
Prashant Gaikwad | 8f8f484 | 2013-01-11 13:16:20 +0530 | [diff] [blame] | 1514 | pll->clk_base = clk_base; |
| 1515 | pll->pmc = pmc; |
| 1516 | |
Prashant Gaikwad | 8f8f484 | 2013-01-11 13:16:20 +0530 | [diff] [blame] | 1517 | pll->params = pll_params; |
Prashant Gaikwad | 8f8f484 | 2013-01-11 13:16:20 +0530 | [diff] [blame] | 1518 | pll->lock = lock; |
| 1519 | |
Peter De Schrijver | aa6fefd | 2013-06-05 16:51:25 +0300 | [diff] [blame] | 1520 | if (!pll_params->div_nmp) |
| 1521 | pll_params->div_nmp = &default_nmp; |
Prashant Gaikwad | 8f8f484 | 2013-01-11 13:16:20 +0530 | [diff] [blame] | 1522 | |
Peter De Schrijver | dba4072 | 2013-04-03 17:40:36 +0300 | [diff] [blame] | 1523 | return pll; |
| 1524 | } |
| 1525 | |
| 1526 | static struct clk *_tegra_clk_register_pll(struct tegra_clk_pll *pll, |
| 1527 | const char *name, const char *parent_name, unsigned long flags, |
| 1528 | const struct clk_ops *ops) |
| 1529 | { |
| 1530 | struct clk_init_data init; |
| 1531 | |
| 1532 | init.name = name; |
| 1533 | init.ops = ops; |
| 1534 | init.flags = flags; |
| 1535 | init.parent_names = (parent_name ? &parent_name : NULL); |
| 1536 | init.num_parents = (parent_name ? 1 : 0); |
| 1537 | |
Rhyland Klein | 407254d | 2015-06-18 17:28:25 -0400 | [diff] [blame] | 1538 | /* Default to _calc_rate if unspecified */ |
Rhyland Klein | 86c679a | 2015-06-18 17:28:34 -0400 | [diff] [blame] | 1539 | if (!pll->params->calc_rate) { |
| 1540 | if (pll->params->flags & TEGRA_PLLM) |
| 1541 | pll->params->calc_rate = _calc_dynamic_ramp_rate; |
| 1542 | else |
| 1543 | pll->params->calc_rate = _calc_rate; |
| 1544 | } |
Rhyland Klein | 407254d | 2015-06-18 17:28:25 -0400 | [diff] [blame] | 1545 | |
Bill Huang | b985114 | 2015-06-18 17:28:31 -0400 | [diff] [blame] | 1546 | if (pll->params->set_defaults) |
| 1547 | pll->params->set_defaults(pll); |
| 1548 | |
Prashant Gaikwad | 8f8f484 | 2013-01-11 13:16:20 +0530 | [diff] [blame] | 1549 | /* Data in .init is copied by clk_register(), so stack variable OK */ |
| 1550 | pll->hw.init = &init; |
| 1551 | |
Peter De Schrijver | dba4072 | 2013-04-03 17:40:36 +0300 | [diff] [blame] | 1552 | return clk_register(NULL, &pll->hw); |
| 1553 | } |
| 1554 | |
| 1555 | struct clk *tegra_clk_register_pll(const char *name, const char *parent_name, |
| 1556 | void __iomem *clk_base, void __iomem *pmc, |
Peter De Schrijver | ebe142b | 2013-10-04 17:28:34 +0300 | [diff] [blame] | 1557 | unsigned long flags, struct tegra_clk_pll_params *pll_params, |
| 1558 | spinlock_t *lock) |
Peter De Schrijver | dba4072 | 2013-04-03 17:40:36 +0300 | [diff] [blame] | 1559 | { |
| 1560 | struct tegra_clk_pll *pll; |
| 1561 | struct clk *clk; |
| 1562 | |
Peter De Schrijver | ebe142b | 2013-10-04 17:28:34 +0300 | [diff] [blame] | 1563 | pll_params->flags |= TEGRA_PLL_BYPASS; |
Rhyland Klein | 3706b43 | 2015-06-18 17:28:23 -0400 | [diff] [blame] | 1564 | |
Peter De Schrijver | ebe142b | 2013-10-04 17:28:34 +0300 | [diff] [blame] | 1565 | pll = _tegra_init_pll(clk_base, pmc, pll_params, lock); |
Peter De Schrijver | dba4072 | 2013-04-03 17:40:36 +0300 | [diff] [blame] | 1566 | if (IS_ERR(pll)) |
| 1567 | return ERR_CAST(pll); |
| 1568 | |
| 1569 | clk = _tegra_clk_register_pll(pll, name, parent_name, flags, |
| 1570 | &tegra_clk_pll_ops); |
Prashant Gaikwad | 8f8f484 | 2013-01-11 13:16:20 +0530 | [diff] [blame] | 1571 | if (IS_ERR(clk)) |
| 1572 | kfree(pll); |
| 1573 | |
| 1574 | return clk; |
| 1575 | } |
| 1576 | |
Thierry Reding | d0f02ce | 2014-04-04 15:55:13 +0200 | [diff] [blame] | 1577 | static struct div_nmp pll_e_nmp = { |
| 1578 | .divn_shift = PLLE_BASE_DIVN_SHIFT, |
| 1579 | .divn_width = PLLE_BASE_DIVN_WIDTH, |
| 1580 | .divm_shift = PLLE_BASE_DIVM_SHIFT, |
| 1581 | .divm_width = PLLE_BASE_DIVM_WIDTH, |
| 1582 | .divp_shift = PLLE_BASE_DIVP_SHIFT, |
| 1583 | .divp_width = PLLE_BASE_DIVP_WIDTH, |
| 1584 | }; |
| 1585 | |
Prashant Gaikwad | 8f8f484 | 2013-01-11 13:16:20 +0530 | [diff] [blame] | 1586 | struct clk *tegra_clk_register_plle(const char *name, const char *parent_name, |
| 1587 | void __iomem *clk_base, void __iomem *pmc, |
Peter De Schrijver | ebe142b | 2013-10-04 17:28:34 +0300 | [diff] [blame] | 1588 | unsigned long flags, struct tegra_clk_pll_params *pll_params, |
| 1589 | spinlock_t *lock) |
Prashant Gaikwad | 8f8f484 | 2013-01-11 13:16:20 +0530 | [diff] [blame] | 1590 | { |
Peter De Schrijver | dba4072 | 2013-04-03 17:40:36 +0300 | [diff] [blame] | 1591 | struct tegra_clk_pll *pll; |
| 1592 | struct clk *clk; |
Peter De Schrijver | dba4072 | 2013-04-03 17:40:36 +0300 | [diff] [blame] | 1593 | |
Rhyland Klein | 3706b43 | 2015-06-18 17:28:23 -0400 | [diff] [blame] | 1594 | pll_params->flags |= TEGRA_PLL_BYPASS; |
Thierry Reding | d0f02ce | 2014-04-04 15:55:13 +0200 | [diff] [blame] | 1595 | |
| 1596 | if (!pll_params->div_nmp) |
| 1597 | pll_params->div_nmp = &pll_e_nmp; |
| 1598 | |
Peter De Schrijver | ebe142b | 2013-10-04 17:28:34 +0300 | [diff] [blame] | 1599 | pll = _tegra_init_pll(clk_base, pmc, pll_params, lock); |
Peter De Schrijver | dba4072 | 2013-04-03 17:40:36 +0300 | [diff] [blame] | 1600 | if (IS_ERR(pll)) |
| 1601 | return ERR_CAST(pll); |
| 1602 | |
| 1603 | clk = _tegra_clk_register_pll(pll, name, parent_name, flags, |
| 1604 | &tegra_clk_plle_ops); |
| 1605 | if (IS_ERR(clk)) |
| 1606 | kfree(pll); |
| 1607 | |
| 1608 | return clk; |
Prashant Gaikwad | 8f8f484 | 2013-01-11 13:16:20 +0530 | [diff] [blame] | 1609 | } |
Peter De Schrijver | c1d1939 | 2013-04-03 17:40:41 +0300 | [diff] [blame] | 1610 | |
Paul Walmsley | 08acae3 | 2014-12-16 12:38:29 -0800 | [diff] [blame] | 1611 | #if defined(CONFIG_ARCH_TEGRA_114_SOC) || \ |
| 1612 | defined(CONFIG_ARCH_TEGRA_124_SOC) || \ |
Rhyland Klein | dd322f0 | 2015-06-18 17:28:28 -0400 | [diff] [blame] | 1613 | defined(CONFIG_ARCH_TEGRA_132_SOC) || \ |
| 1614 | defined(CONFIG_ARCH_TEGRA_210_SOC) |
Sachin Kamat | e47e12f | 2013-10-08 16:47:41 +0530 | [diff] [blame] | 1615 | static const struct clk_ops tegra_clk_pllxc_ops = { |
Peter De Schrijver | c1d1939 | 2013-04-03 17:40:41 +0300 | [diff] [blame] | 1616 | .is_enabled = clk_pll_is_enabled, |
Rhyland Klein | 7db864c | 2015-06-18 17:28:20 -0400 | [diff] [blame] | 1617 | .enable = clk_pll_enable, |
| 1618 | .disable = clk_pll_disable, |
Peter De Schrijver | c1d1939 | 2013-04-03 17:40:41 +0300 | [diff] [blame] | 1619 | .recalc_rate = clk_pll_recalc_rate, |
| 1620 | .round_rate = clk_pll_ramp_round_rate, |
| 1621 | .set_rate = clk_pllxc_set_rate, |
| 1622 | }; |
| 1623 | |
Sachin Kamat | e47e12f | 2013-10-08 16:47:41 +0530 | [diff] [blame] | 1624 | static const struct clk_ops tegra_clk_pllc_ops = { |
Peter De Schrijver | c1d1939 | 2013-04-03 17:40:41 +0300 | [diff] [blame] | 1625 | .is_enabled = clk_pll_is_enabled, |
| 1626 | .enable = clk_pllc_enable, |
| 1627 | .disable = clk_pllc_disable, |
| 1628 | .recalc_rate = clk_pll_recalc_rate, |
| 1629 | .round_rate = clk_pll_ramp_round_rate, |
| 1630 | .set_rate = clk_pllc_set_rate, |
| 1631 | }; |
| 1632 | |
Sachin Kamat | e47e12f | 2013-10-08 16:47:41 +0530 | [diff] [blame] | 1633 | static const struct clk_ops tegra_clk_pllre_ops = { |
Peter De Schrijver | c1d1939 | 2013-04-03 17:40:41 +0300 | [diff] [blame] | 1634 | .is_enabled = clk_pll_is_enabled, |
Rhyland Klein | 7db864c | 2015-06-18 17:28:20 -0400 | [diff] [blame] | 1635 | .enable = clk_pll_enable, |
| 1636 | .disable = clk_pll_disable, |
Peter De Schrijver | c1d1939 | 2013-04-03 17:40:41 +0300 | [diff] [blame] | 1637 | .recalc_rate = clk_pllre_recalc_rate, |
| 1638 | .round_rate = clk_pllre_round_rate, |
| 1639 | .set_rate = clk_pllre_set_rate, |
| 1640 | }; |
| 1641 | |
Sachin Kamat | e47e12f | 2013-10-08 16:47:41 +0530 | [diff] [blame] | 1642 | static const struct clk_ops tegra_clk_plle_tegra114_ops = { |
Peter De Schrijver | c1d1939 | 2013-04-03 17:40:41 +0300 | [diff] [blame] | 1643 | .is_enabled = clk_pll_is_enabled, |
| 1644 | .enable = clk_plle_tegra114_enable, |
| 1645 | .disable = clk_plle_tegra114_disable, |
| 1646 | .recalc_rate = clk_pll_recalc_rate, |
| 1647 | }; |
| 1648 | |
| 1649 | |
| 1650 | struct clk *tegra_clk_register_pllxc(const char *name, const char *parent_name, |
| 1651 | void __iomem *clk_base, void __iomem *pmc, |
Peter De Schrijver | ebe142b | 2013-10-04 17:28:34 +0300 | [diff] [blame] | 1652 | unsigned long flags, |
Peter De Schrijver | c1d1939 | 2013-04-03 17:40:41 +0300 | [diff] [blame] | 1653 | struct tegra_clk_pll_params *pll_params, |
Peter De Schrijver | c1d1939 | 2013-04-03 17:40:41 +0300 | [diff] [blame] | 1654 | spinlock_t *lock) |
| 1655 | { |
| 1656 | struct tegra_clk_pll *pll; |
Peter De Schrijver | 04edb09 | 2013-09-06 14:37:37 +0300 | [diff] [blame] | 1657 | struct clk *clk, *parent; |
| 1658 | unsigned long parent_rate; |
Peter De Schrijver | 04edb09 | 2013-09-06 14:37:37 +0300 | [diff] [blame] | 1659 | u32 val, val_iddq; |
| 1660 | |
| 1661 | parent = __clk_lookup(parent_name); |
Wei Yongjun | 62ce7cd | 2013-10-29 03:07:57 +0100 | [diff] [blame] | 1662 | if (!parent) { |
Peter De Schrijver | 04edb09 | 2013-09-06 14:37:37 +0300 | [diff] [blame] | 1663 | WARN(1, "parent clk %s of %s must be registered first\n", |
Tomeu Vizoso | ca036b2 | 2014-09-30 09:22:00 +0200 | [diff] [blame] | 1664 | parent_name, name); |
Peter De Schrijver | 04edb09 | 2013-09-06 14:37:37 +0300 | [diff] [blame] | 1665 | return ERR_PTR(-EINVAL); |
| 1666 | } |
Peter De Schrijver | c1d1939 | 2013-04-03 17:40:41 +0300 | [diff] [blame] | 1667 | |
| 1668 | if (!pll_params->pdiv_tohw) |
| 1669 | return ERR_PTR(-EINVAL); |
| 1670 | |
Stephen Boyd | 5cdb1dc | 2015-07-30 17:20:57 -0700 | [diff] [blame] | 1671 | parent_rate = clk_get_rate(parent); |
Peter De Schrijver | 04edb09 | 2013-09-06 14:37:37 +0300 | [diff] [blame] | 1672 | |
| 1673 | pll_params->vco_min = _clip_vco_min(pll_params->vco_min, parent_rate); |
| 1674 | |
Bill Huang | b5512b4 | 2015-06-18 17:28:30 -0400 | [diff] [blame] | 1675 | if (pll_params->adjust_vco) |
| 1676 | pll_params->vco_min = pll_params->adjust_vco(pll_params, |
| 1677 | parent_rate); |
| 1678 | |
Bill Huang | b985114 | 2015-06-18 17:28:31 -0400 | [diff] [blame] | 1679 | /* |
| 1680 | * If the pll has a set_defaults callback, it will take care of |
| 1681 | * configuring dynamic ramping and setting IDDQ in that path. |
| 1682 | */ |
| 1683 | if (!pll_params->set_defaults) { |
| 1684 | int err; |
Peter De Schrijver | 04edb09 | 2013-09-06 14:37:37 +0300 | [diff] [blame] | 1685 | |
Bill Huang | b985114 | 2015-06-18 17:28:31 -0400 | [diff] [blame] | 1686 | err = _setup_dynamic_ramp(pll_params, clk_base, parent_rate); |
| 1687 | if (err) |
| 1688 | return ERR_PTR(err); |
Peter De Schrijver | 04edb09 | 2013-09-06 14:37:37 +0300 | [diff] [blame] | 1689 | |
Bill Huang | b985114 | 2015-06-18 17:28:31 -0400 | [diff] [blame] | 1690 | val = readl_relaxed(clk_base + pll_params->base_reg); |
| 1691 | val_iddq = readl_relaxed(clk_base + pll_params->iddq_reg); |
| 1692 | |
| 1693 | if (val & PLL_BASE_ENABLE) |
| 1694 | WARN_ON(val_iddq & BIT(pll_params->iddq_bit_idx)); |
| 1695 | else { |
| 1696 | val_iddq |= BIT(pll_params->iddq_bit_idx); |
| 1697 | writel_relaxed(val_iddq, |
| 1698 | clk_base + pll_params->iddq_reg); |
| 1699 | } |
Peter De Schrijver | 04edb09 | 2013-09-06 14:37:37 +0300 | [diff] [blame] | 1700 | } |
| 1701 | |
Peter De Schrijver | ebe142b | 2013-10-04 17:28:34 +0300 | [diff] [blame] | 1702 | pll = _tegra_init_pll(clk_base, pmc, pll_params, lock); |
Peter De Schrijver | c1d1939 | 2013-04-03 17:40:41 +0300 | [diff] [blame] | 1703 | if (IS_ERR(pll)) |
| 1704 | return ERR_CAST(pll); |
| 1705 | |
| 1706 | clk = _tegra_clk_register_pll(pll, name, parent_name, flags, |
| 1707 | &tegra_clk_pllxc_ops); |
| 1708 | if (IS_ERR(clk)) |
| 1709 | kfree(pll); |
| 1710 | |
| 1711 | return clk; |
| 1712 | } |
| 1713 | |
| 1714 | struct clk *tegra_clk_register_pllre(const char *name, const char *parent_name, |
| 1715 | void __iomem *clk_base, void __iomem *pmc, |
Peter De Schrijver | ebe142b | 2013-10-04 17:28:34 +0300 | [diff] [blame] | 1716 | unsigned long flags, |
Peter De Schrijver | c1d1939 | 2013-04-03 17:40:41 +0300 | [diff] [blame] | 1717 | struct tegra_clk_pll_params *pll_params, |
Peter De Schrijver | c1d1939 | 2013-04-03 17:40:41 +0300 | [diff] [blame] | 1718 | spinlock_t *lock, unsigned long parent_rate) |
| 1719 | { |
| 1720 | u32 val; |
| 1721 | struct tegra_clk_pll *pll; |
| 1722 | struct clk *clk; |
| 1723 | |
Peter De Schrijver | 04edb09 | 2013-09-06 14:37:37 +0300 | [diff] [blame] | 1724 | pll_params->vco_min = _clip_vco_min(pll_params->vco_min, parent_rate); |
| 1725 | |
Bill Huang | b5512b4 | 2015-06-18 17:28:30 -0400 | [diff] [blame] | 1726 | if (pll_params->adjust_vco) |
| 1727 | pll_params->vco_min = pll_params->adjust_vco(pll_params, |
| 1728 | parent_rate); |
| 1729 | |
Peter De Schrijver | ebe142b | 2013-10-04 17:28:34 +0300 | [diff] [blame] | 1730 | pll = _tegra_init_pll(clk_base, pmc, pll_params, lock); |
Peter De Schrijver | c1d1939 | 2013-04-03 17:40:41 +0300 | [diff] [blame] | 1731 | if (IS_ERR(pll)) |
| 1732 | return ERR_CAST(pll); |
| 1733 | |
| 1734 | /* program minimum rate by default */ |
| 1735 | |
| 1736 | val = pll_readl_base(pll); |
| 1737 | if (val & PLL_BASE_ENABLE) |
| 1738 | WARN_ON(val & pll_params->iddq_bit_idx); |
| 1739 | else { |
| 1740 | int m; |
| 1741 | |
| 1742 | m = _pll_fixed_mdiv(pll_params, parent_rate); |
Thierry Reding | c61e4e7 | 2014-04-04 15:55:14 +0200 | [diff] [blame] | 1743 | val = m << divm_shift(pll); |
| 1744 | val |= (pll_params->vco_min / parent_rate) << divn_shift(pll); |
Peter De Schrijver | c1d1939 | 2013-04-03 17:40:41 +0300 | [diff] [blame] | 1745 | pll_writel_base(val, pll); |
| 1746 | } |
| 1747 | |
| 1748 | /* disable lock override */ |
| 1749 | |
| 1750 | val = pll_readl_misc(pll); |
| 1751 | val &= ~BIT(29); |
| 1752 | pll_writel_misc(val, pll); |
| 1753 | |
Peter De Schrijver | c1d1939 | 2013-04-03 17:40:41 +0300 | [diff] [blame] | 1754 | clk = _tegra_clk_register_pll(pll, name, parent_name, flags, |
| 1755 | &tegra_clk_pllre_ops); |
| 1756 | if (IS_ERR(clk)) |
| 1757 | kfree(pll); |
| 1758 | |
| 1759 | return clk; |
| 1760 | } |
| 1761 | |
| 1762 | struct clk *tegra_clk_register_pllm(const char *name, const char *parent_name, |
| 1763 | void __iomem *clk_base, void __iomem *pmc, |
Peter De Schrijver | ebe142b | 2013-10-04 17:28:34 +0300 | [diff] [blame] | 1764 | unsigned long flags, |
Peter De Schrijver | c1d1939 | 2013-04-03 17:40:41 +0300 | [diff] [blame] | 1765 | struct tegra_clk_pll_params *pll_params, |
Peter De Schrijver | c1d1939 | 2013-04-03 17:40:41 +0300 | [diff] [blame] | 1766 | spinlock_t *lock) |
| 1767 | { |
| 1768 | struct tegra_clk_pll *pll; |
Peter De Schrijver | 04edb09 | 2013-09-06 14:37:37 +0300 | [diff] [blame] | 1769 | struct clk *clk, *parent; |
| 1770 | unsigned long parent_rate; |
Peter De Schrijver | c1d1939 | 2013-04-03 17:40:41 +0300 | [diff] [blame] | 1771 | |
| 1772 | if (!pll_params->pdiv_tohw) |
| 1773 | return ERR_PTR(-EINVAL); |
| 1774 | |
Peter De Schrijver | 04edb09 | 2013-09-06 14:37:37 +0300 | [diff] [blame] | 1775 | parent = __clk_lookup(parent_name); |
Wei Yongjun | 62ce7cd | 2013-10-29 03:07:57 +0100 | [diff] [blame] | 1776 | if (!parent) { |
Peter De Schrijver | 04edb09 | 2013-09-06 14:37:37 +0300 | [diff] [blame] | 1777 | WARN(1, "parent clk %s of %s must be registered first\n", |
Tomeu Vizoso | ca036b2 | 2014-09-30 09:22:00 +0200 | [diff] [blame] | 1778 | parent_name, name); |
Peter De Schrijver | 04edb09 | 2013-09-06 14:37:37 +0300 | [diff] [blame] | 1779 | return ERR_PTR(-EINVAL); |
| 1780 | } |
| 1781 | |
Stephen Boyd | 5cdb1dc | 2015-07-30 17:20:57 -0700 | [diff] [blame] | 1782 | parent_rate = clk_get_rate(parent); |
Peter De Schrijver | 04edb09 | 2013-09-06 14:37:37 +0300 | [diff] [blame] | 1783 | |
| 1784 | pll_params->vco_min = _clip_vco_min(pll_params->vco_min, parent_rate); |
| 1785 | |
Bill Huang | b5512b4 | 2015-06-18 17:28:30 -0400 | [diff] [blame] | 1786 | if (pll_params->adjust_vco) |
| 1787 | pll_params->vco_min = pll_params->adjust_vco(pll_params, |
| 1788 | parent_rate); |
| 1789 | |
Peter De Schrijver | ebe142b | 2013-10-04 17:28:34 +0300 | [diff] [blame] | 1790 | pll_params->flags |= TEGRA_PLL_BYPASS; |
Peter De Schrijver | ebe142b | 2013-10-04 17:28:34 +0300 | [diff] [blame] | 1791 | pll_params->flags |= TEGRA_PLLM; |
| 1792 | pll = _tegra_init_pll(clk_base, pmc, pll_params, lock); |
Peter De Schrijver | c1d1939 | 2013-04-03 17:40:41 +0300 | [diff] [blame] | 1793 | if (IS_ERR(pll)) |
| 1794 | return ERR_CAST(pll); |
| 1795 | |
| 1796 | clk = _tegra_clk_register_pll(pll, name, parent_name, flags, |
Danny Huang | 267b62a | 2015-06-18 17:28:27 -0400 | [diff] [blame] | 1797 | &tegra_clk_pll_ops); |
Peter De Schrijver | c1d1939 | 2013-04-03 17:40:41 +0300 | [diff] [blame] | 1798 | if (IS_ERR(clk)) |
| 1799 | kfree(pll); |
| 1800 | |
| 1801 | return clk; |
| 1802 | } |
| 1803 | |
| 1804 | struct clk *tegra_clk_register_pllc(const char *name, const char *parent_name, |
| 1805 | void __iomem *clk_base, void __iomem *pmc, |
Peter De Schrijver | ebe142b | 2013-10-04 17:28:34 +0300 | [diff] [blame] | 1806 | unsigned long flags, |
Peter De Schrijver | c1d1939 | 2013-04-03 17:40:41 +0300 | [diff] [blame] | 1807 | struct tegra_clk_pll_params *pll_params, |
Peter De Schrijver | c1d1939 | 2013-04-03 17:40:41 +0300 | [diff] [blame] | 1808 | spinlock_t *lock) |
| 1809 | { |
| 1810 | struct clk *parent, *clk; |
Thierry Reding | 385f9ad | 2015-11-19 16:34:06 +0100 | [diff] [blame] | 1811 | const struct pdiv_map *p_tohw = pll_params->pdiv_tohw; |
Peter De Schrijver | c1d1939 | 2013-04-03 17:40:41 +0300 | [diff] [blame] | 1812 | struct tegra_clk_pll *pll; |
| 1813 | struct tegra_clk_pll_freq_table cfg; |
| 1814 | unsigned long parent_rate; |
| 1815 | |
| 1816 | if (!p_tohw) |
| 1817 | return ERR_PTR(-EINVAL); |
| 1818 | |
| 1819 | parent = __clk_lookup(parent_name); |
Wei Yongjun | 62ce7cd | 2013-10-29 03:07:57 +0100 | [diff] [blame] | 1820 | if (!parent) { |
Peter De Schrijver | c1d1939 | 2013-04-03 17:40:41 +0300 | [diff] [blame] | 1821 | WARN(1, "parent clk %s of %s must be registered first\n", |
Tomeu Vizoso | ca036b2 | 2014-09-30 09:22:00 +0200 | [diff] [blame] | 1822 | parent_name, name); |
Peter De Schrijver | c1d1939 | 2013-04-03 17:40:41 +0300 | [diff] [blame] | 1823 | return ERR_PTR(-EINVAL); |
| 1824 | } |
| 1825 | |
Stephen Boyd | 5cdb1dc | 2015-07-30 17:20:57 -0700 | [diff] [blame] | 1826 | parent_rate = clk_get_rate(parent); |
Peter De Schrijver | 04edb09 | 2013-09-06 14:37:37 +0300 | [diff] [blame] | 1827 | |
| 1828 | pll_params->vco_min = _clip_vco_min(pll_params->vco_min, parent_rate); |
| 1829 | |
Peter De Schrijver | ebe142b | 2013-10-04 17:28:34 +0300 | [diff] [blame] | 1830 | pll_params->flags |= TEGRA_PLL_BYPASS; |
| 1831 | pll = _tegra_init_pll(clk_base, pmc, pll_params, lock); |
Peter De Schrijver | c1d1939 | 2013-04-03 17:40:41 +0300 | [diff] [blame] | 1832 | if (IS_ERR(pll)) |
| 1833 | return ERR_CAST(pll); |
| 1834 | |
Peter De Schrijver | c1d1939 | 2013-04-03 17:40:41 +0300 | [diff] [blame] | 1835 | /* |
| 1836 | * Most of PLLC register fields are shadowed, and can not be read |
| 1837 | * directly from PLL h/w. Hence, actual PLLC boot state is unknown. |
| 1838 | * Initialize PLL to default state: disabled, reset; shadow registers |
| 1839 | * loaded with default parameters; dividers are preset for half of |
| 1840 | * minimum VCO rate (the latter assured that shadowed divider settings |
| 1841 | * are within supported range). |
| 1842 | */ |
| 1843 | |
| 1844 | cfg.m = _pll_fixed_mdiv(pll_params, parent_rate); |
| 1845 | cfg.n = cfg.m * pll_params->vco_min / parent_rate; |
| 1846 | |
| 1847 | while (p_tohw->pdiv) { |
| 1848 | if (p_tohw->pdiv == 2) { |
| 1849 | cfg.p = p_tohw->hw_val; |
| 1850 | break; |
| 1851 | } |
| 1852 | p_tohw++; |
| 1853 | } |
| 1854 | |
| 1855 | if (!p_tohw->pdiv) { |
| 1856 | WARN_ON(1); |
| 1857 | return ERR_PTR(-EINVAL); |
| 1858 | } |
| 1859 | |
| 1860 | pll_writel_base(0, pll); |
| 1861 | _update_pll_mnp(pll, &cfg); |
| 1862 | |
| 1863 | pll_writel_misc(PLLCX_MISC_DEFAULT, pll); |
| 1864 | pll_writel(PLLCX_MISC1_DEFAULT, pll_params->ext_misc_reg[0], pll); |
| 1865 | pll_writel(PLLCX_MISC2_DEFAULT, pll_params->ext_misc_reg[1], pll); |
| 1866 | pll_writel(PLLCX_MISC3_DEFAULT, pll_params->ext_misc_reg[2], pll); |
| 1867 | |
| 1868 | _pllcx_update_dynamic_coef(pll, parent_rate, cfg.n); |
| 1869 | |
| 1870 | clk = _tegra_clk_register_pll(pll, name, parent_name, flags, |
| 1871 | &tegra_clk_pllc_ops); |
| 1872 | if (IS_ERR(clk)) |
| 1873 | kfree(pll); |
| 1874 | |
| 1875 | return clk; |
| 1876 | } |
| 1877 | |
| 1878 | struct clk *tegra_clk_register_plle_tegra114(const char *name, |
| 1879 | const char *parent_name, |
| 1880 | void __iomem *clk_base, unsigned long flags, |
Peter De Schrijver | c1d1939 | 2013-04-03 17:40:41 +0300 | [diff] [blame] | 1881 | struct tegra_clk_pll_params *pll_params, |
Peter De Schrijver | c1d1939 | 2013-04-03 17:40:41 +0300 | [diff] [blame] | 1882 | spinlock_t *lock) |
| 1883 | { |
| 1884 | struct tegra_clk_pll *pll; |
| 1885 | struct clk *clk; |
| 1886 | u32 val, val_aux; |
| 1887 | |
Peter De Schrijver | ebe142b | 2013-10-04 17:28:34 +0300 | [diff] [blame] | 1888 | pll = _tegra_init_pll(clk_base, NULL, pll_params, lock); |
Peter De Schrijver | c1d1939 | 2013-04-03 17:40:41 +0300 | [diff] [blame] | 1889 | if (IS_ERR(pll)) |
| 1890 | return ERR_CAST(pll); |
| 1891 | |
| 1892 | /* ensure parent is set to pll_re_vco */ |
| 1893 | |
| 1894 | val = pll_readl_base(pll); |
| 1895 | val_aux = pll_readl(pll_params->aux_reg, pll); |
| 1896 | |
| 1897 | if (val & PLL_BASE_ENABLE) { |
Peter De Schrijver | 8e9cc80 | 2013-11-25 14:44:13 +0200 | [diff] [blame] | 1898 | if ((val_aux & PLLE_AUX_PLLRE_SEL) || |
| 1899 | (val_aux & PLLE_AUX_PLLP_SEL)) |
Peter De Schrijver | c1d1939 | 2013-04-03 17:40:41 +0300 | [diff] [blame] | 1900 | WARN(1, "pll_e enabled with unsupported parent %s\n", |
Peter De Schrijver | 8e9cc80 | 2013-11-25 14:44:13 +0200 | [diff] [blame] | 1901 | (val_aux & PLLE_AUX_PLLP_SEL) ? "pllp_out0" : |
| 1902 | "pll_re_vco"); |
Peter De Schrijver | c1d1939 | 2013-04-03 17:40:41 +0300 | [diff] [blame] | 1903 | } else { |
Peter De Schrijver | 8e9cc80 | 2013-11-25 14:44:13 +0200 | [diff] [blame] | 1904 | val_aux &= ~(PLLE_AUX_PLLRE_SEL | PLLE_AUX_PLLP_SEL); |
Tuomas Tynkkynen | d2c834a | 2014-05-16 16:50:20 +0300 | [diff] [blame] | 1905 | pll_writel(val_aux, pll_params->aux_reg, pll); |
Peter De Schrijver | c1d1939 | 2013-04-03 17:40:41 +0300 | [diff] [blame] | 1906 | } |
| 1907 | |
| 1908 | clk = _tegra_clk_register_pll(pll, name, parent_name, flags, |
| 1909 | &tegra_clk_plle_tegra114_ops); |
| 1910 | if (IS_ERR(clk)) |
| 1911 | kfree(pll); |
| 1912 | |
| 1913 | return clk; |
| 1914 | } |
| 1915 | #endif |
Peter De Schrijver | 798e910 | 2013-09-09 13:22:55 +0300 | [diff] [blame] | 1916 | |
Paul Walmsley | 08acae3 | 2014-12-16 12:38:29 -0800 | [diff] [blame] | 1917 | #if defined(CONFIG_ARCH_TEGRA_124_SOC) || defined(CONFIG_ARCH_TEGRA_132_SOC) |
Sachin Kamat | e47e12f | 2013-10-08 16:47:41 +0530 | [diff] [blame] | 1918 | static const struct clk_ops tegra_clk_pllss_ops = { |
Peter De Schrijver | 798e910 | 2013-09-09 13:22:55 +0300 | [diff] [blame] | 1919 | .is_enabled = clk_pll_is_enabled, |
Rhyland Klein | 7db864c | 2015-06-18 17:28:20 -0400 | [diff] [blame] | 1920 | .enable = clk_pll_enable, |
| 1921 | .disable = clk_pll_disable, |
Peter De Schrijver | 798e910 | 2013-09-09 13:22:55 +0300 | [diff] [blame] | 1922 | .recalc_rate = clk_pll_recalc_rate, |
| 1923 | .round_rate = clk_pll_ramp_round_rate, |
| 1924 | .set_rate = clk_pllxc_set_rate, |
| 1925 | }; |
| 1926 | |
| 1927 | struct clk *tegra_clk_register_pllss(const char *name, const char *parent_name, |
| 1928 | void __iomem *clk_base, unsigned long flags, |
| 1929 | struct tegra_clk_pll_params *pll_params, |
| 1930 | spinlock_t *lock) |
| 1931 | { |
| 1932 | struct tegra_clk_pll *pll; |
| 1933 | struct clk *clk, *parent; |
| 1934 | struct tegra_clk_pll_freq_table cfg; |
| 1935 | unsigned long parent_rate; |
| 1936 | u32 val; |
| 1937 | int i; |
| 1938 | |
| 1939 | if (!pll_params->div_nmp) |
| 1940 | return ERR_PTR(-EINVAL); |
| 1941 | |
| 1942 | parent = __clk_lookup(parent_name); |
Wei Yongjun | 62ce7cd | 2013-10-29 03:07:57 +0100 | [diff] [blame] | 1943 | if (!parent) { |
Peter De Schrijver | 798e910 | 2013-09-09 13:22:55 +0300 | [diff] [blame] | 1944 | WARN(1, "parent clk %s of %s must be registered first\n", |
Tomeu Vizoso | ca036b2 | 2014-09-30 09:22:00 +0200 | [diff] [blame] | 1945 | parent_name, name); |
Peter De Schrijver | 798e910 | 2013-09-09 13:22:55 +0300 | [diff] [blame] | 1946 | return ERR_PTR(-EINVAL); |
| 1947 | } |
| 1948 | |
Peter De Schrijver | 798e910 | 2013-09-09 13:22:55 +0300 | [diff] [blame] | 1949 | pll = _tegra_init_pll(clk_base, NULL, pll_params, lock); |
| 1950 | if (IS_ERR(pll)) |
| 1951 | return ERR_CAST(pll); |
| 1952 | |
| 1953 | val = pll_readl_base(pll); |
| 1954 | val &= ~PLLSS_REF_SRC_SEL_MASK; |
| 1955 | pll_writel_base(val, pll); |
| 1956 | |
Stephen Boyd | 5cdb1dc | 2015-07-30 17:20:57 -0700 | [diff] [blame] | 1957 | parent_rate = clk_get_rate(parent); |
Peter De Schrijver | 798e910 | 2013-09-09 13:22:55 +0300 | [diff] [blame] | 1958 | |
| 1959 | pll_params->vco_min = _clip_vco_min(pll_params->vco_min, parent_rate); |
| 1960 | |
| 1961 | /* initialize PLL to minimum rate */ |
| 1962 | |
| 1963 | cfg.m = _pll_fixed_mdiv(pll_params, parent_rate); |
| 1964 | cfg.n = cfg.m * pll_params->vco_min / parent_rate; |
| 1965 | |
| 1966 | for (i = 0; pll_params->pdiv_tohw[i].pdiv; i++) |
| 1967 | ; |
| 1968 | if (!i) { |
| 1969 | kfree(pll); |
| 1970 | return ERR_PTR(-EINVAL); |
| 1971 | } |
| 1972 | |
| 1973 | cfg.p = pll_params->pdiv_tohw[i-1].hw_val; |
| 1974 | |
| 1975 | _update_pll_mnp(pll, &cfg); |
| 1976 | |
| 1977 | pll_writel_misc(PLLSS_MISC_DEFAULT, pll); |
| 1978 | pll_writel(PLLSS_CFG_DEFAULT, pll_params->ext_misc_reg[0], pll); |
| 1979 | pll_writel(PLLSS_CTRL1_DEFAULT, pll_params->ext_misc_reg[1], pll); |
| 1980 | pll_writel(PLLSS_CTRL1_DEFAULT, pll_params->ext_misc_reg[2], pll); |
| 1981 | |
| 1982 | val = pll_readl_base(pll); |
| 1983 | if (val & PLL_BASE_ENABLE) { |
| 1984 | if (val & BIT(pll_params->iddq_bit_idx)) { |
| 1985 | WARN(1, "%s is on but IDDQ set\n", name); |
| 1986 | kfree(pll); |
| 1987 | return ERR_PTR(-EINVAL); |
| 1988 | } |
| 1989 | } else |
| 1990 | val |= BIT(pll_params->iddq_bit_idx); |
| 1991 | |
| 1992 | val &= ~PLLSS_LOCK_OVERRIDE; |
| 1993 | pll_writel_base(val, pll); |
| 1994 | |
| 1995 | clk = _tegra_clk_register_pll(pll, name, parent_name, flags, |
| 1996 | &tegra_clk_pllss_ops); |
| 1997 | |
| 1998 | if (IS_ERR(clk)) |
| 1999 | kfree(pll); |
| 2000 | |
| 2001 | return clk; |
| 2002 | } |
| 2003 | #endif |
Rhyland Klein | dd322f0 | 2015-06-18 17:28:28 -0400 | [diff] [blame] | 2004 | |
| 2005 | #if defined(CONFIG_ARCH_TEGRA_210_SOC) |
| 2006 | static int clk_plle_tegra210_enable(struct clk_hw *hw) |
| 2007 | { |
| 2008 | struct tegra_clk_pll *pll = to_clk_pll(hw); |
| 2009 | struct tegra_clk_pll_freq_table sel; |
| 2010 | u32 val; |
| 2011 | int ret; |
| 2012 | unsigned long flags = 0; |
| 2013 | unsigned long input_rate = clk_get_rate(clk_get_parent(hw->clk)); |
| 2014 | |
| 2015 | if (_get_table_rate(hw, &sel, pll->params->fixed_rate, input_rate)) |
| 2016 | return -EINVAL; |
| 2017 | |
| 2018 | if (pll->lock) |
| 2019 | spin_lock_irqsave(pll->lock, flags); |
| 2020 | |
| 2021 | val = pll_readl_base(pll); |
| 2022 | val &= ~BIT(30); /* Disable lock override */ |
| 2023 | pll_writel_base(val, pll); |
| 2024 | |
| 2025 | val = pll_readl(pll->params->aux_reg, pll); |
| 2026 | val |= PLLE_AUX_ENABLE_SWCTL; |
| 2027 | val &= ~PLLE_AUX_SEQ_ENABLE; |
| 2028 | pll_writel(val, pll->params->aux_reg, pll); |
| 2029 | udelay(1); |
| 2030 | |
| 2031 | val = pll_readl_misc(pll); |
| 2032 | val |= PLLE_MISC_LOCK_ENABLE; |
| 2033 | val |= PLLE_MISC_IDDQ_SW_CTRL; |
| 2034 | val &= ~PLLE_MISC_IDDQ_SW_VALUE; |
| 2035 | val |= PLLE_MISC_PLLE_PTS; |
| 2036 | val |= PLLE_MISC_VREG_BG_CTRL_MASK | PLLE_MISC_VREG_CTRL_MASK; |
| 2037 | pll_writel_misc(val, pll); |
| 2038 | udelay(5); |
| 2039 | |
| 2040 | val = pll_readl(PLLE_SS_CTRL, pll); |
| 2041 | val |= PLLE_SS_DISABLE; |
| 2042 | pll_writel(val, PLLE_SS_CTRL, pll); |
| 2043 | |
| 2044 | val = pll_readl_base(pll); |
| 2045 | val &= ~(divp_mask_shifted(pll) | divn_mask_shifted(pll) | |
| 2046 | divm_mask_shifted(pll)); |
| 2047 | val &= ~(PLLE_BASE_DIVCML_MASK << PLLE_BASE_DIVCML_SHIFT); |
| 2048 | val |= sel.m << divm_shift(pll); |
| 2049 | val |= sel.n << divn_shift(pll); |
| 2050 | val |= sel.cpcon << PLLE_BASE_DIVCML_SHIFT; |
| 2051 | pll_writel_base(val, pll); |
| 2052 | udelay(1); |
| 2053 | |
| 2054 | val = pll_readl_base(pll); |
| 2055 | val |= PLLE_BASE_ENABLE; |
| 2056 | pll_writel_base(val, pll); |
| 2057 | |
| 2058 | ret = clk_pll_wait_for_lock(pll); |
| 2059 | |
| 2060 | if (ret < 0) |
| 2061 | goto out; |
| 2062 | |
| 2063 | val = pll_readl(PLLE_SS_CTRL, pll); |
| 2064 | val &= ~(PLLE_SS_CNTL_CENTER | PLLE_SS_CNTL_INVERT); |
| 2065 | val &= ~PLLE_SS_COEFFICIENTS_MASK; |
| 2066 | val |= PLLE_SS_COEFFICIENTS_VAL; |
| 2067 | pll_writel(val, PLLE_SS_CTRL, pll); |
| 2068 | val &= ~(PLLE_SS_CNTL_SSC_BYP | PLLE_SS_CNTL_BYPASS_SS); |
| 2069 | pll_writel(val, PLLE_SS_CTRL, pll); |
| 2070 | udelay(1); |
| 2071 | val &= ~PLLE_SS_CNTL_INTERP_RESET; |
| 2072 | pll_writel(val, PLLE_SS_CTRL, pll); |
| 2073 | udelay(1); |
| 2074 | |
| 2075 | val = pll_readl_misc(pll); |
| 2076 | val &= ~PLLE_MISC_IDDQ_SW_CTRL; |
| 2077 | pll_writel_misc(val, pll); |
| 2078 | |
| 2079 | val = pll_readl(pll->params->aux_reg, pll); |
| 2080 | val |= (PLLE_AUX_USE_LOCKDET | PLLE_AUX_SS_SEQ_INCLUDE); |
| 2081 | val &= ~(PLLE_AUX_ENABLE_SWCTL | PLLE_AUX_SS_SWCTL); |
| 2082 | pll_writel(val, pll->params->aux_reg, pll); |
| 2083 | udelay(1); |
| 2084 | val |= PLLE_AUX_SEQ_ENABLE; |
| 2085 | pll_writel(val, pll->params->aux_reg, pll); |
| 2086 | |
| 2087 | out: |
| 2088 | if (pll->lock) |
| 2089 | spin_unlock_irqrestore(pll->lock, flags); |
| 2090 | |
| 2091 | return ret; |
| 2092 | } |
| 2093 | |
| 2094 | static void clk_plle_tegra210_disable(struct clk_hw *hw) |
| 2095 | { |
| 2096 | struct tegra_clk_pll *pll = to_clk_pll(hw); |
| 2097 | unsigned long flags = 0; |
| 2098 | u32 val; |
| 2099 | |
| 2100 | if (pll->lock) |
| 2101 | spin_lock_irqsave(pll->lock, flags); |
| 2102 | |
| 2103 | val = pll_readl_base(pll); |
| 2104 | val &= ~PLLE_BASE_ENABLE; |
| 2105 | pll_writel_base(val, pll); |
| 2106 | |
| 2107 | val = pll_readl_misc(pll); |
| 2108 | val |= PLLE_MISC_IDDQ_SW_CTRL | PLLE_MISC_IDDQ_SW_VALUE; |
| 2109 | pll_writel_misc(val, pll); |
| 2110 | udelay(1); |
| 2111 | |
| 2112 | if (pll->lock) |
| 2113 | spin_unlock_irqrestore(pll->lock, flags); |
| 2114 | } |
| 2115 | |
| 2116 | static int clk_plle_tegra210_is_enabled(struct clk_hw *hw) |
| 2117 | { |
| 2118 | struct tegra_clk_pll *pll = to_clk_pll(hw); |
| 2119 | u32 val; |
| 2120 | |
| 2121 | val = pll_readl_base(pll); |
| 2122 | |
| 2123 | return val & PLLE_BASE_ENABLE ? 1 : 0; |
| 2124 | } |
| 2125 | |
| 2126 | static const struct clk_ops tegra_clk_plle_tegra210_ops = { |
| 2127 | .is_enabled = clk_plle_tegra210_is_enabled, |
| 2128 | .enable = clk_plle_tegra210_enable, |
| 2129 | .disable = clk_plle_tegra210_disable, |
| 2130 | .recalc_rate = clk_pll_recalc_rate, |
| 2131 | }; |
| 2132 | |
| 2133 | struct clk *tegra_clk_register_plle_tegra210(const char *name, |
| 2134 | const char *parent_name, |
| 2135 | void __iomem *clk_base, unsigned long flags, |
| 2136 | struct tegra_clk_pll_params *pll_params, |
| 2137 | spinlock_t *lock) |
| 2138 | { |
| 2139 | struct tegra_clk_pll *pll; |
| 2140 | struct clk *clk; |
| 2141 | u32 val, val_aux; |
| 2142 | |
| 2143 | pll = _tegra_init_pll(clk_base, NULL, pll_params, lock); |
| 2144 | if (IS_ERR(pll)) |
| 2145 | return ERR_CAST(pll); |
| 2146 | |
| 2147 | /* ensure parent is set to pll_re_vco */ |
| 2148 | |
| 2149 | val = pll_readl_base(pll); |
| 2150 | val_aux = pll_readl(pll_params->aux_reg, pll); |
| 2151 | |
| 2152 | if (val & PLLE_BASE_ENABLE) { |
| 2153 | if ((val_aux & PLLE_AUX_PLLRE_SEL) || |
| 2154 | (val_aux & PLLE_AUX_PLLP_SEL)) |
| 2155 | WARN(1, "pll_e enabled with unsupported parent %s\n", |
| 2156 | (val_aux & PLLE_AUX_PLLP_SEL) ? "pllp_out0" : |
| 2157 | "pll_re_vco"); |
| 2158 | } else { |
| 2159 | val_aux &= ~(PLLE_AUX_PLLRE_SEL | PLLE_AUX_PLLP_SEL); |
| 2160 | pll_writel(val_aux, pll_params->aux_reg, pll); |
| 2161 | } |
| 2162 | |
| 2163 | clk = _tegra_clk_register_pll(pll, name, parent_name, flags, |
| 2164 | &tegra_clk_plle_tegra210_ops); |
| 2165 | if (IS_ERR(clk)) |
| 2166 | kfree(pll); |
| 2167 | |
| 2168 | return clk; |
| 2169 | } |
| 2170 | |
| 2171 | struct clk *tegra_clk_register_pllc_tegra210(const char *name, |
| 2172 | const char *parent_name, void __iomem *clk_base, |
| 2173 | void __iomem *pmc, unsigned long flags, |
| 2174 | struct tegra_clk_pll_params *pll_params, |
| 2175 | spinlock_t *lock) |
| 2176 | { |
| 2177 | struct clk *parent, *clk; |
| 2178 | const struct pdiv_map *p_tohw = pll_params->pdiv_tohw; |
| 2179 | struct tegra_clk_pll *pll; |
| 2180 | unsigned long parent_rate; |
| 2181 | |
| 2182 | if (!p_tohw) |
| 2183 | return ERR_PTR(-EINVAL); |
| 2184 | |
| 2185 | parent = __clk_lookup(parent_name); |
| 2186 | if (!parent) { |
| 2187 | WARN(1, "parent clk %s of %s must be registered first\n", |
| 2188 | name, parent_name); |
| 2189 | return ERR_PTR(-EINVAL); |
| 2190 | } |
| 2191 | |
| 2192 | parent_rate = clk_get_rate(parent); |
| 2193 | |
| 2194 | pll_params->vco_min = _clip_vco_min(pll_params->vco_min, parent_rate); |
| 2195 | |
Bill Huang | b5512b4 | 2015-06-18 17:28:30 -0400 | [diff] [blame] | 2196 | if (pll_params->adjust_vco) |
| 2197 | pll_params->vco_min = pll_params->adjust_vco(pll_params, |
| 2198 | parent_rate); |
| 2199 | |
Rhyland Klein | dd322f0 | 2015-06-18 17:28:28 -0400 | [diff] [blame] | 2200 | pll_params->flags |= TEGRA_PLL_BYPASS; |
| 2201 | pll = _tegra_init_pll(clk_base, pmc, pll_params, lock); |
| 2202 | if (IS_ERR(pll)) |
| 2203 | return ERR_CAST(pll); |
| 2204 | |
| 2205 | clk = _tegra_clk_register_pll(pll, name, parent_name, flags, |
| 2206 | &tegra_clk_pll_ops); |
| 2207 | if (IS_ERR(clk)) |
| 2208 | kfree(pll); |
| 2209 | |
| 2210 | return clk; |
| 2211 | } |
| 2212 | |
| 2213 | struct clk *tegra_clk_register_pllxc_tegra210(const char *name, |
| 2214 | const char *parent_name, void __iomem *clk_base, |
| 2215 | void __iomem *pmc, unsigned long flags, |
| 2216 | struct tegra_clk_pll_params *pll_params, |
| 2217 | spinlock_t *lock) |
| 2218 | { |
| 2219 | struct tegra_clk_pll *pll; |
| 2220 | struct clk *clk, *parent; |
| 2221 | unsigned long parent_rate; |
| 2222 | |
| 2223 | parent = __clk_lookup(parent_name); |
| 2224 | if (!parent) { |
| 2225 | WARN(1, "parent clk %s of %s must be registered first\n", |
| 2226 | name, parent_name); |
| 2227 | return ERR_PTR(-EINVAL); |
| 2228 | } |
| 2229 | |
| 2230 | if (!pll_params->pdiv_tohw) |
| 2231 | return ERR_PTR(-EINVAL); |
| 2232 | |
| 2233 | parent_rate = clk_get_rate(parent); |
| 2234 | |
| 2235 | pll_params->vco_min = _clip_vco_min(pll_params->vco_min, parent_rate); |
| 2236 | |
Bill Huang | b5512b4 | 2015-06-18 17:28:30 -0400 | [diff] [blame] | 2237 | if (pll_params->adjust_vco) |
| 2238 | pll_params->vco_min = pll_params->adjust_vco(pll_params, |
| 2239 | parent_rate); |
| 2240 | |
Rhyland Klein | dd322f0 | 2015-06-18 17:28:28 -0400 | [diff] [blame] | 2241 | pll = _tegra_init_pll(clk_base, pmc, pll_params, lock); |
| 2242 | if (IS_ERR(pll)) |
| 2243 | return ERR_CAST(pll); |
| 2244 | |
| 2245 | clk = _tegra_clk_register_pll(pll, name, parent_name, flags, |
| 2246 | &tegra_clk_pll_ops); |
| 2247 | if (IS_ERR(clk)) |
| 2248 | kfree(pll); |
| 2249 | |
| 2250 | return clk; |
| 2251 | } |
| 2252 | |
| 2253 | struct clk *tegra_clk_register_pllss_tegra210(const char *name, |
| 2254 | const char *parent_name, void __iomem *clk_base, |
| 2255 | unsigned long flags, |
| 2256 | struct tegra_clk_pll_params *pll_params, |
| 2257 | spinlock_t *lock) |
| 2258 | { |
| 2259 | struct tegra_clk_pll *pll; |
| 2260 | struct clk *clk, *parent; |
| 2261 | struct tegra_clk_pll_freq_table cfg; |
| 2262 | unsigned long parent_rate; |
| 2263 | u32 val; |
| 2264 | int i; |
| 2265 | |
| 2266 | if (!pll_params->div_nmp) |
| 2267 | return ERR_PTR(-EINVAL); |
| 2268 | |
| 2269 | parent = __clk_lookup(parent_name); |
| 2270 | if (!parent) { |
| 2271 | WARN(1, "parent clk %s of %s must be registered first\n", |
| 2272 | name, parent_name); |
| 2273 | return ERR_PTR(-EINVAL); |
| 2274 | } |
| 2275 | |
| 2276 | pll = _tegra_init_pll(clk_base, NULL, pll_params, lock); |
| 2277 | if (IS_ERR(pll)) |
| 2278 | return ERR_CAST(pll); |
| 2279 | |
| 2280 | val = pll_readl_base(pll); |
| 2281 | val &= ~PLLSS_REF_SRC_SEL_MASK; |
| 2282 | pll_writel_base(val, pll); |
| 2283 | |
| 2284 | parent_rate = clk_get_rate(parent); |
| 2285 | |
| 2286 | pll_params->vco_min = _clip_vco_min(pll_params->vco_min, parent_rate); |
| 2287 | |
Bill Huang | b5512b4 | 2015-06-18 17:28:30 -0400 | [diff] [blame] | 2288 | if (pll_params->adjust_vco) |
| 2289 | pll_params->vco_min = pll_params->adjust_vco(pll_params, |
| 2290 | parent_rate); |
| 2291 | |
Rhyland Klein | dd322f0 | 2015-06-18 17:28:28 -0400 | [diff] [blame] | 2292 | /* initialize PLL to minimum rate */ |
| 2293 | |
| 2294 | cfg.m = _pll_fixed_mdiv(pll_params, parent_rate); |
| 2295 | cfg.n = cfg.m * pll_params->vco_min / parent_rate; |
| 2296 | |
| 2297 | for (i = 0; pll_params->pdiv_tohw[i].pdiv; i++) |
| 2298 | ; |
| 2299 | if (!i) { |
| 2300 | kfree(pll); |
| 2301 | return ERR_PTR(-EINVAL); |
| 2302 | } |
| 2303 | |
| 2304 | cfg.p = pll_params->pdiv_tohw[i-1].hw_val; |
| 2305 | |
| 2306 | _update_pll_mnp(pll, &cfg); |
| 2307 | |
| 2308 | pll_writel_misc(PLLSS_MISC_DEFAULT, pll); |
| 2309 | |
| 2310 | val = pll_readl_base(pll); |
| 2311 | if (val & PLL_BASE_ENABLE) { |
| 2312 | if (val & BIT(pll_params->iddq_bit_idx)) { |
| 2313 | WARN(1, "%s is on but IDDQ set\n", name); |
| 2314 | kfree(pll); |
| 2315 | return ERR_PTR(-EINVAL); |
| 2316 | } |
| 2317 | } else |
| 2318 | val |= BIT(pll_params->iddq_bit_idx); |
| 2319 | |
| 2320 | val &= ~PLLSS_LOCK_OVERRIDE; |
| 2321 | pll_writel_base(val, pll); |
| 2322 | |
| 2323 | clk = _tegra_clk_register_pll(pll, name, parent_name, flags, |
| 2324 | &tegra_clk_pll_ops); |
| 2325 | |
| 2326 | if (IS_ERR(clk)) |
| 2327 | kfree(pll); |
| 2328 | |
| 2329 | return clk; |
| 2330 | } |
Rhyland Klein | 6929715 | 2015-06-18 17:28:29 -0400 | [diff] [blame] | 2331 | |
| 2332 | struct clk *tegra_clk_register_pllmb(const char *name, const char *parent_name, |
| 2333 | void __iomem *clk_base, void __iomem *pmc, |
| 2334 | unsigned long flags, |
| 2335 | struct tegra_clk_pll_params *pll_params, |
| 2336 | spinlock_t *lock) |
| 2337 | { |
| 2338 | struct tegra_clk_pll *pll; |
| 2339 | struct clk *clk, *parent; |
| 2340 | unsigned long parent_rate; |
| 2341 | |
| 2342 | if (!pll_params->pdiv_tohw) |
| 2343 | return ERR_PTR(-EINVAL); |
| 2344 | |
| 2345 | parent = __clk_lookup(parent_name); |
| 2346 | if (!parent) { |
| 2347 | WARN(1, "parent clk %s of %s must be registered first\n", |
| 2348 | parent_name, name); |
| 2349 | return ERR_PTR(-EINVAL); |
| 2350 | } |
| 2351 | |
| 2352 | parent_rate = clk_get_rate(parent); |
| 2353 | |
| 2354 | pll_params->vco_min = _clip_vco_min(pll_params->vco_min, parent_rate); |
| 2355 | |
Bill Huang | b5512b4 | 2015-06-18 17:28:30 -0400 | [diff] [blame] | 2356 | if (pll_params->adjust_vco) |
| 2357 | pll_params->vco_min = pll_params->adjust_vco(pll_params, |
| 2358 | parent_rate); |
| 2359 | |
Rhyland Klein | 6929715 | 2015-06-18 17:28:29 -0400 | [diff] [blame] | 2360 | pll_params->flags |= TEGRA_PLL_BYPASS; |
| 2361 | pll_params->flags |= TEGRA_PLLMB; |
| 2362 | pll = _tegra_init_pll(clk_base, pmc, pll_params, lock); |
| 2363 | if (IS_ERR(pll)) |
| 2364 | return ERR_CAST(pll); |
| 2365 | |
| 2366 | clk = _tegra_clk_register_pll(pll, name, parent_name, flags, |
| 2367 | &tegra_clk_pll_ops); |
| 2368 | if (IS_ERR(clk)) |
| 2369 | kfree(pll); |
| 2370 | |
| 2371 | return clk; |
| 2372 | } |
Rhyland Klein | dd322f0 | 2015-06-18 17:28:28 -0400 | [diff] [blame] | 2373 | #endif |