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