Thomas Abraham | 1c4c5fe | 2013-03-09 17:02:48 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2013 Samsung Electronics Co., Ltd. |
| 3 | * Copyright (c) 2013 Linaro Ltd. |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License version 2 as |
| 7 | * published by the Free Software Foundation. |
| 8 | * |
| 9 | * This file contains the utility functions to register the pll clocks. |
| 10 | */ |
| 11 | |
| 12 | #include <linux/errno.h> |
Tomasz Figa | b4054ac | 2013-08-26 19:09:05 +0200 | [diff] [blame] | 13 | #include <linux/hrtimer.h> |
Thomas Abraham | 1c4c5fe | 2013-03-09 17:02:48 +0900 | [diff] [blame] | 14 | #include "clk.h" |
| 15 | #include "clk-pll.h" |
| 16 | |
Tomasz Figa | b4054ac | 2013-08-26 19:09:05 +0200 | [diff] [blame] | 17 | #define PLL_TIMEOUT_MS 10 |
| 18 | |
Yadwinder Singh Brar | 079dbea | 2013-06-11 15:01:06 +0530 | [diff] [blame] | 19 | struct samsung_clk_pll { |
| 20 | struct clk_hw hw; |
| 21 | void __iomem *lock_reg; |
| 22 | void __iomem *con_reg; |
Yadwinder Singh Brar | 07dc76f | 2013-06-11 15:01:07 +0530 | [diff] [blame] | 23 | enum samsung_pll_type type; |
Yadwinder Singh Brar | 3ff6e0d | 2013-06-11 15:01:12 +0530 | [diff] [blame] | 24 | unsigned int rate_count; |
| 25 | const struct samsung_pll_rate_table *rate_table; |
Yadwinder Singh Brar | 079dbea | 2013-06-11 15:01:06 +0530 | [diff] [blame] | 26 | }; |
| 27 | |
| 28 | #define to_clk_pll(_hw) container_of(_hw, struct samsung_clk_pll, hw) |
| 29 | |
Yadwinder Singh Brar | dfa8931 | 2013-06-11 15:01:13 +0530 | [diff] [blame] | 30 | static const struct samsung_pll_rate_table *samsung_get_pll_settings( |
| 31 | struct samsung_clk_pll *pll, unsigned long rate) |
| 32 | { |
| 33 | const struct samsung_pll_rate_table *rate_table = pll->rate_table; |
| 34 | int i; |
| 35 | |
| 36 | for (i = 0; i < pll->rate_count; i++) { |
| 37 | if (rate == rate_table[i].rate) |
| 38 | return &rate_table[i]; |
| 39 | } |
| 40 | |
| 41 | return NULL; |
| 42 | } |
| 43 | |
| 44 | static long samsung_pll_round_rate(struct clk_hw *hw, |
| 45 | unsigned long drate, unsigned long *prate) |
| 46 | { |
| 47 | struct samsung_clk_pll *pll = to_clk_pll(hw); |
| 48 | const struct samsung_pll_rate_table *rate_table = pll->rate_table; |
| 49 | int i; |
| 50 | |
| 51 | /* Assumming rate_table is in descending order */ |
| 52 | for (i = 0; i < pll->rate_count; i++) { |
| 53 | if (drate >= rate_table[i].rate) |
| 54 | return rate_table[i].rate; |
| 55 | } |
| 56 | |
| 57 | /* return minimum supported value */ |
| 58 | return rate_table[i - 1].rate; |
| 59 | } |
| 60 | |
Thomas Abraham | 1c4c5fe | 2013-03-09 17:02:48 +0900 | [diff] [blame] | 61 | /* |
| 62 | * PLL35xx Clock Type |
| 63 | */ |
Yadwinder Singh Brar | dfa8931 | 2013-06-11 15:01:13 +0530 | [diff] [blame] | 64 | /* Maximum lock time can be 270 * PDIV cycles */ |
| 65 | #define PLL35XX_LOCK_FACTOR (270) |
Thomas Abraham | 1c4c5fe | 2013-03-09 17:02:48 +0900 | [diff] [blame] | 66 | |
| 67 | #define PLL35XX_MDIV_MASK (0x3FF) |
| 68 | #define PLL35XX_PDIV_MASK (0x3F) |
| 69 | #define PLL35XX_SDIV_MASK (0x7) |
Yadwinder Singh Brar | dfa8931 | 2013-06-11 15:01:13 +0530 | [diff] [blame] | 70 | #define PLL35XX_LOCK_STAT_MASK (0x1) |
Thomas Abraham | 1c4c5fe | 2013-03-09 17:02:48 +0900 | [diff] [blame] | 71 | #define PLL35XX_MDIV_SHIFT (16) |
| 72 | #define PLL35XX_PDIV_SHIFT (8) |
| 73 | #define PLL35XX_SDIV_SHIFT (0) |
Yadwinder Singh Brar | dfa8931 | 2013-06-11 15:01:13 +0530 | [diff] [blame] | 74 | #define PLL35XX_LOCK_STAT_SHIFT (29) |
Thomas Abraham | 1c4c5fe | 2013-03-09 17:02:48 +0900 | [diff] [blame] | 75 | |
Thomas Abraham | 1c4c5fe | 2013-03-09 17:02:48 +0900 | [diff] [blame] | 76 | static unsigned long samsung_pll35xx_recalc_rate(struct clk_hw *hw, |
| 77 | unsigned long parent_rate) |
| 78 | { |
Yadwinder Singh Brar | 079dbea | 2013-06-11 15:01:06 +0530 | [diff] [blame] | 79 | struct samsung_clk_pll *pll = to_clk_pll(hw); |
Thomas Abraham | 1c4c5fe | 2013-03-09 17:02:48 +0900 | [diff] [blame] | 80 | u32 mdiv, pdiv, sdiv, pll_con; |
| 81 | u64 fvco = parent_rate; |
| 82 | |
| 83 | pll_con = __raw_readl(pll->con_reg); |
| 84 | mdiv = (pll_con >> PLL35XX_MDIV_SHIFT) & PLL35XX_MDIV_MASK; |
| 85 | pdiv = (pll_con >> PLL35XX_PDIV_SHIFT) & PLL35XX_PDIV_MASK; |
| 86 | sdiv = (pll_con >> PLL35XX_SDIV_SHIFT) & PLL35XX_SDIV_MASK; |
| 87 | |
| 88 | fvco *= mdiv; |
| 89 | do_div(fvco, (pdiv << sdiv)); |
| 90 | |
| 91 | return (unsigned long)fvco; |
| 92 | } |
| 93 | |
Yadwinder Singh Brar | dfa8931 | 2013-06-11 15:01:13 +0530 | [diff] [blame] | 94 | static inline bool samsung_pll35xx_mp_change( |
| 95 | const struct samsung_pll_rate_table *rate, u32 pll_con) |
| 96 | { |
| 97 | u32 old_mdiv, old_pdiv; |
| 98 | |
| 99 | old_mdiv = (pll_con >> PLL35XX_MDIV_SHIFT) & PLL35XX_MDIV_MASK; |
| 100 | old_pdiv = (pll_con >> PLL35XX_PDIV_SHIFT) & PLL35XX_PDIV_MASK; |
| 101 | |
| 102 | return (rate->mdiv != old_mdiv || rate->pdiv != old_pdiv); |
| 103 | } |
| 104 | |
| 105 | static int samsung_pll35xx_set_rate(struct clk_hw *hw, unsigned long drate, |
| 106 | unsigned long prate) |
| 107 | { |
| 108 | struct samsung_clk_pll *pll = to_clk_pll(hw); |
| 109 | const struct samsung_pll_rate_table *rate; |
| 110 | u32 tmp; |
| 111 | |
| 112 | /* Get required rate settings from table */ |
| 113 | rate = samsung_get_pll_settings(pll, drate); |
| 114 | if (!rate) { |
| 115 | pr_err("%s: Invalid rate : %lu for pll clk %s\n", __func__, |
| 116 | drate, __clk_get_name(hw->clk)); |
| 117 | return -EINVAL; |
| 118 | } |
| 119 | |
| 120 | tmp = __raw_readl(pll->con_reg); |
| 121 | |
| 122 | if (!(samsung_pll35xx_mp_change(rate, tmp))) { |
| 123 | /* If only s change, change just s value only*/ |
| 124 | tmp &= ~(PLL35XX_SDIV_MASK << PLL35XX_SDIV_SHIFT); |
| 125 | tmp |= rate->sdiv << PLL35XX_SDIV_SHIFT; |
| 126 | __raw_writel(tmp, pll->con_reg); |
| 127 | |
| 128 | return 0; |
| 129 | } |
| 130 | |
| 131 | /* Set PLL lock time. */ |
| 132 | __raw_writel(rate->pdiv * PLL35XX_LOCK_FACTOR, |
| 133 | pll->lock_reg); |
| 134 | |
| 135 | /* Change PLL PMS values */ |
| 136 | tmp &= ~((PLL35XX_MDIV_MASK << PLL35XX_MDIV_SHIFT) | |
| 137 | (PLL35XX_PDIV_MASK << PLL35XX_PDIV_SHIFT) | |
| 138 | (PLL35XX_SDIV_MASK << PLL35XX_SDIV_SHIFT)); |
| 139 | tmp |= (rate->mdiv << PLL35XX_MDIV_SHIFT) | |
| 140 | (rate->pdiv << PLL35XX_PDIV_SHIFT) | |
| 141 | (rate->sdiv << PLL35XX_SDIV_SHIFT); |
| 142 | __raw_writel(tmp, pll->con_reg); |
| 143 | |
| 144 | /* wait_lock_time */ |
| 145 | do { |
| 146 | cpu_relax(); |
| 147 | tmp = __raw_readl(pll->con_reg); |
| 148 | } while (!(tmp & (PLL35XX_LOCK_STAT_MASK |
| 149 | << PLL35XX_LOCK_STAT_SHIFT))); |
| 150 | return 0; |
| 151 | } |
| 152 | |
Thomas Abraham | 1c4c5fe | 2013-03-09 17:02:48 +0900 | [diff] [blame] | 153 | static const struct clk_ops samsung_pll35xx_clk_ops = { |
| 154 | .recalc_rate = samsung_pll35xx_recalc_rate, |
Yadwinder Singh Brar | dfa8931 | 2013-06-11 15:01:13 +0530 | [diff] [blame] | 155 | .round_rate = samsung_pll_round_rate, |
| 156 | .set_rate = samsung_pll35xx_set_rate, |
| 157 | }; |
| 158 | |
| 159 | static const struct clk_ops samsung_pll35xx_clk_min_ops = { |
| 160 | .recalc_rate = samsung_pll35xx_recalc_rate, |
Thomas Abraham | 1c4c5fe | 2013-03-09 17:02:48 +0900 | [diff] [blame] | 161 | }; |
| 162 | |
Thomas Abraham | 1c4c5fe | 2013-03-09 17:02:48 +0900 | [diff] [blame] | 163 | /* |
| 164 | * PLL36xx Clock Type |
| 165 | */ |
Vikas Sajjan | bb4278d | 2013-06-11 15:01:14 +0530 | [diff] [blame] | 166 | /* Maximum lock time can be 3000 * PDIV cycles */ |
| 167 | #define PLL36XX_LOCK_FACTOR (3000) |
Thomas Abraham | 1c4c5fe | 2013-03-09 17:02:48 +0900 | [diff] [blame] | 168 | |
| 169 | #define PLL36XX_KDIV_MASK (0xFFFF) |
| 170 | #define PLL36XX_MDIV_MASK (0x1FF) |
| 171 | #define PLL36XX_PDIV_MASK (0x3F) |
| 172 | #define PLL36XX_SDIV_MASK (0x7) |
| 173 | #define PLL36XX_MDIV_SHIFT (16) |
| 174 | #define PLL36XX_PDIV_SHIFT (8) |
| 175 | #define PLL36XX_SDIV_SHIFT (0) |
Vikas Sajjan | bb4278d | 2013-06-11 15:01:14 +0530 | [diff] [blame] | 176 | #define PLL36XX_KDIV_SHIFT (0) |
| 177 | #define PLL36XX_LOCK_STAT_SHIFT (29) |
Thomas Abraham | 1c4c5fe | 2013-03-09 17:02:48 +0900 | [diff] [blame] | 178 | |
Thomas Abraham | 1c4c5fe | 2013-03-09 17:02:48 +0900 | [diff] [blame] | 179 | static unsigned long samsung_pll36xx_recalc_rate(struct clk_hw *hw, |
| 180 | unsigned long parent_rate) |
| 181 | { |
Yadwinder Singh Brar | 079dbea | 2013-06-11 15:01:06 +0530 | [diff] [blame] | 182 | struct samsung_clk_pll *pll = to_clk_pll(hw); |
Doug Anderson | 071ff9a | 2013-06-11 08:24:05 -0700 | [diff] [blame] | 183 | u32 mdiv, pdiv, sdiv, pll_con0, pll_con1; |
| 184 | s16 kdiv; |
Thomas Abraham | 1c4c5fe | 2013-03-09 17:02:48 +0900 | [diff] [blame] | 185 | u64 fvco = parent_rate; |
| 186 | |
| 187 | pll_con0 = __raw_readl(pll->con_reg); |
| 188 | pll_con1 = __raw_readl(pll->con_reg + 4); |
| 189 | mdiv = (pll_con0 >> PLL36XX_MDIV_SHIFT) & PLL36XX_MDIV_MASK; |
| 190 | pdiv = (pll_con0 >> PLL36XX_PDIV_SHIFT) & PLL36XX_PDIV_MASK; |
| 191 | sdiv = (pll_con0 >> PLL36XX_SDIV_SHIFT) & PLL36XX_SDIV_MASK; |
Doug Anderson | 071ff9a | 2013-06-11 08:24:05 -0700 | [diff] [blame] | 192 | kdiv = (s16)(pll_con1 & PLL36XX_KDIV_MASK); |
Thomas Abraham | 1c4c5fe | 2013-03-09 17:02:48 +0900 | [diff] [blame] | 193 | |
| 194 | fvco *= (mdiv << 16) + kdiv; |
| 195 | do_div(fvco, (pdiv << sdiv)); |
| 196 | fvco >>= 16; |
| 197 | |
| 198 | return (unsigned long)fvco; |
| 199 | } |
| 200 | |
Vikas Sajjan | bb4278d | 2013-06-11 15:01:14 +0530 | [diff] [blame] | 201 | static inline bool samsung_pll36xx_mpk_change( |
| 202 | const struct samsung_pll_rate_table *rate, u32 pll_con0, u32 pll_con1) |
| 203 | { |
| 204 | u32 old_mdiv, old_pdiv, old_kdiv; |
| 205 | |
| 206 | old_mdiv = (pll_con0 >> PLL36XX_MDIV_SHIFT) & PLL36XX_MDIV_MASK; |
| 207 | old_pdiv = (pll_con0 >> PLL36XX_PDIV_SHIFT) & PLL36XX_PDIV_MASK; |
| 208 | old_kdiv = (pll_con1 >> PLL36XX_KDIV_SHIFT) & PLL36XX_KDIV_MASK; |
| 209 | |
| 210 | return (rate->mdiv != old_mdiv || rate->pdiv != old_pdiv || |
| 211 | rate->kdiv != old_kdiv); |
| 212 | } |
| 213 | |
| 214 | static int samsung_pll36xx_set_rate(struct clk_hw *hw, unsigned long drate, |
| 215 | unsigned long parent_rate) |
| 216 | { |
| 217 | struct samsung_clk_pll *pll = to_clk_pll(hw); |
| 218 | u32 tmp, pll_con0, pll_con1; |
| 219 | const struct samsung_pll_rate_table *rate; |
| 220 | |
| 221 | rate = samsung_get_pll_settings(pll, drate); |
| 222 | if (!rate) { |
| 223 | pr_err("%s: Invalid rate : %lu for pll clk %s\n", __func__, |
| 224 | drate, __clk_get_name(hw->clk)); |
| 225 | return -EINVAL; |
| 226 | } |
| 227 | |
| 228 | pll_con0 = __raw_readl(pll->con_reg); |
| 229 | pll_con1 = __raw_readl(pll->con_reg + 4); |
| 230 | |
| 231 | if (!(samsung_pll36xx_mpk_change(rate, pll_con0, pll_con1))) { |
| 232 | /* If only s change, change just s value only*/ |
| 233 | pll_con0 &= ~(PLL36XX_SDIV_MASK << PLL36XX_SDIV_SHIFT); |
| 234 | pll_con0 |= (rate->sdiv << PLL36XX_SDIV_SHIFT); |
| 235 | __raw_writel(pll_con0, pll->con_reg); |
| 236 | |
| 237 | return 0; |
| 238 | } |
| 239 | |
| 240 | /* Set PLL lock time. */ |
| 241 | __raw_writel(rate->pdiv * PLL36XX_LOCK_FACTOR, pll->lock_reg); |
| 242 | |
| 243 | /* Change PLL PMS values */ |
| 244 | pll_con0 &= ~((PLL36XX_MDIV_MASK << PLL36XX_MDIV_SHIFT) | |
| 245 | (PLL36XX_PDIV_MASK << PLL36XX_PDIV_SHIFT) | |
| 246 | (PLL36XX_SDIV_MASK << PLL36XX_SDIV_SHIFT)); |
| 247 | pll_con0 |= (rate->mdiv << PLL36XX_MDIV_SHIFT) | |
| 248 | (rate->pdiv << PLL36XX_PDIV_SHIFT) | |
| 249 | (rate->sdiv << PLL36XX_SDIV_SHIFT); |
| 250 | __raw_writel(pll_con0, pll->con_reg); |
| 251 | |
| 252 | pll_con1 &= ~(PLL36XX_KDIV_MASK << PLL36XX_KDIV_SHIFT); |
| 253 | pll_con1 |= rate->kdiv << PLL36XX_KDIV_SHIFT; |
| 254 | __raw_writel(pll_con1, pll->con_reg + 4); |
| 255 | |
| 256 | /* wait_lock_time */ |
| 257 | do { |
| 258 | cpu_relax(); |
| 259 | tmp = __raw_readl(pll->con_reg); |
| 260 | } while (!(tmp & (1 << PLL36XX_LOCK_STAT_SHIFT))); |
| 261 | |
| 262 | return 0; |
| 263 | } |
| 264 | |
Thomas Abraham | 1c4c5fe | 2013-03-09 17:02:48 +0900 | [diff] [blame] | 265 | static const struct clk_ops samsung_pll36xx_clk_ops = { |
| 266 | .recalc_rate = samsung_pll36xx_recalc_rate, |
Vikas Sajjan | bb4278d | 2013-06-11 15:01:14 +0530 | [diff] [blame] | 267 | .set_rate = samsung_pll36xx_set_rate, |
| 268 | .round_rate = samsung_pll_round_rate, |
| 269 | }; |
| 270 | |
| 271 | static const struct clk_ops samsung_pll36xx_clk_min_ops = { |
| 272 | .recalc_rate = samsung_pll36xx_recalc_rate, |
Thomas Abraham | 1c4c5fe | 2013-03-09 17:02:48 +0900 | [diff] [blame] | 273 | }; |
| 274 | |
Thomas Abraham | 1c4c5fe | 2013-03-09 17:02:48 +0900 | [diff] [blame] | 275 | /* |
| 276 | * PLL45xx Clock Type |
| 277 | */ |
Tomasz Figa | b4054ac | 2013-08-26 19:09:05 +0200 | [diff] [blame] | 278 | #define PLL4502_LOCK_FACTOR 400 |
| 279 | #define PLL4508_LOCK_FACTOR 240 |
Thomas Abraham | 1c4c5fe | 2013-03-09 17:02:48 +0900 | [diff] [blame] | 280 | |
| 281 | #define PLL45XX_MDIV_MASK (0x3FF) |
| 282 | #define PLL45XX_PDIV_MASK (0x3F) |
| 283 | #define PLL45XX_SDIV_MASK (0x7) |
Tomasz Figa | b4054ac | 2013-08-26 19:09:05 +0200 | [diff] [blame] | 284 | #define PLL45XX_AFC_MASK (0x1F) |
Thomas Abraham | 1c4c5fe | 2013-03-09 17:02:48 +0900 | [diff] [blame] | 285 | #define PLL45XX_MDIV_SHIFT (16) |
| 286 | #define PLL45XX_PDIV_SHIFT (8) |
| 287 | #define PLL45XX_SDIV_SHIFT (0) |
Tomasz Figa | b4054ac | 2013-08-26 19:09:05 +0200 | [diff] [blame] | 288 | #define PLL45XX_AFC_SHIFT (0) |
| 289 | |
| 290 | #define PLL45XX_ENABLE BIT(31) |
| 291 | #define PLL45XX_LOCKED BIT(29) |
Thomas Abraham | 1c4c5fe | 2013-03-09 17:02:48 +0900 | [diff] [blame] | 292 | |
Thomas Abraham | 1c4c5fe | 2013-03-09 17:02:48 +0900 | [diff] [blame] | 293 | static unsigned long samsung_pll45xx_recalc_rate(struct clk_hw *hw, |
| 294 | unsigned long parent_rate) |
| 295 | { |
Tomasz Figa | 52b0601 | 2013-08-26 19:09:04 +0200 | [diff] [blame] | 296 | struct samsung_clk_pll *pll = to_clk_pll(hw); |
Thomas Abraham | 1c4c5fe | 2013-03-09 17:02:48 +0900 | [diff] [blame] | 297 | u32 mdiv, pdiv, sdiv, pll_con; |
| 298 | u64 fvco = parent_rate; |
| 299 | |
| 300 | pll_con = __raw_readl(pll->con_reg); |
| 301 | mdiv = (pll_con >> PLL45XX_MDIV_SHIFT) & PLL45XX_MDIV_MASK; |
| 302 | pdiv = (pll_con >> PLL45XX_PDIV_SHIFT) & PLL45XX_PDIV_MASK; |
| 303 | sdiv = (pll_con >> PLL45XX_SDIV_SHIFT) & PLL45XX_SDIV_MASK; |
| 304 | |
| 305 | if (pll->type == pll_4508) |
| 306 | sdiv = sdiv - 1; |
| 307 | |
| 308 | fvco *= mdiv; |
| 309 | do_div(fvco, (pdiv << sdiv)); |
| 310 | |
| 311 | return (unsigned long)fvco; |
| 312 | } |
| 313 | |
Tomasz Figa | b4054ac | 2013-08-26 19:09:05 +0200 | [diff] [blame] | 314 | static bool samsung_pll45xx_mp_change(u32 pll_con0, u32 pll_con1, |
| 315 | const struct samsung_pll_rate_table *rate) |
| 316 | { |
| 317 | u32 old_mdiv, old_pdiv, old_afc; |
| 318 | |
| 319 | old_mdiv = (pll_con0 >> PLL45XX_MDIV_SHIFT) & PLL45XX_MDIV_MASK; |
| 320 | old_pdiv = (pll_con0 >> PLL45XX_PDIV_SHIFT) & PLL45XX_PDIV_MASK; |
| 321 | old_afc = (pll_con1 >> PLL45XX_AFC_SHIFT) & PLL45XX_AFC_MASK; |
| 322 | |
| 323 | return (old_mdiv != rate->mdiv || old_pdiv != rate->pdiv |
| 324 | || old_afc != rate->afc); |
| 325 | } |
| 326 | |
| 327 | static int samsung_pll45xx_set_rate(struct clk_hw *hw, unsigned long drate, |
| 328 | unsigned long prate) |
| 329 | { |
| 330 | struct samsung_clk_pll *pll = to_clk_pll(hw); |
| 331 | const struct samsung_pll_rate_table *rate; |
| 332 | u32 con0, con1; |
| 333 | ktime_t start; |
| 334 | |
| 335 | /* Get required rate settings from table */ |
| 336 | rate = samsung_get_pll_settings(pll, drate); |
| 337 | if (!rate) { |
| 338 | pr_err("%s: Invalid rate : %lu for pll clk %s\n", __func__, |
| 339 | drate, __clk_get_name(hw->clk)); |
| 340 | return -EINVAL; |
| 341 | } |
| 342 | |
| 343 | con0 = __raw_readl(pll->con_reg); |
| 344 | con1 = __raw_readl(pll->con_reg + 0x4); |
| 345 | |
| 346 | if (!(samsung_pll45xx_mp_change(con0, con1, rate))) { |
| 347 | /* If only s change, change just s value only*/ |
| 348 | con0 &= ~(PLL45XX_SDIV_MASK << PLL45XX_SDIV_SHIFT); |
| 349 | con0 |= rate->sdiv << PLL45XX_SDIV_SHIFT; |
| 350 | __raw_writel(con0, pll->con_reg); |
| 351 | |
| 352 | return 0; |
| 353 | } |
| 354 | |
| 355 | /* Set PLL PMS values. */ |
| 356 | con0 &= ~((PLL45XX_MDIV_MASK << PLL45XX_MDIV_SHIFT) | |
| 357 | (PLL45XX_PDIV_MASK << PLL45XX_PDIV_SHIFT) | |
| 358 | (PLL45XX_SDIV_MASK << PLL45XX_SDIV_SHIFT)); |
| 359 | con0 |= (rate->mdiv << PLL45XX_MDIV_SHIFT) | |
| 360 | (rate->pdiv << PLL45XX_PDIV_SHIFT) | |
| 361 | (rate->sdiv << PLL45XX_SDIV_SHIFT); |
| 362 | |
| 363 | /* Set PLL AFC value. */ |
| 364 | con1 = __raw_readl(pll->con_reg + 0x4); |
| 365 | con1 &= ~(PLL45XX_AFC_MASK << PLL45XX_AFC_SHIFT); |
| 366 | con1 |= (rate->afc << PLL45XX_AFC_SHIFT); |
| 367 | |
| 368 | /* Set PLL lock time. */ |
| 369 | switch (pll->type) { |
| 370 | case pll_4502: |
| 371 | __raw_writel(rate->pdiv * PLL4502_LOCK_FACTOR, pll->lock_reg); |
| 372 | break; |
| 373 | case pll_4508: |
| 374 | __raw_writel(rate->pdiv * PLL4508_LOCK_FACTOR, pll->lock_reg); |
| 375 | break; |
| 376 | default: |
| 377 | break; |
| 378 | }; |
| 379 | |
| 380 | /* Set new configuration. */ |
| 381 | __raw_writel(con1, pll->con_reg + 0x4); |
| 382 | __raw_writel(con0, pll->con_reg); |
| 383 | |
| 384 | /* Wait for locking. */ |
| 385 | start = ktime_get(); |
| 386 | while (!(__raw_readl(pll->con_reg) & PLL45XX_LOCKED)) { |
| 387 | ktime_t delta = ktime_sub(ktime_get(), start); |
| 388 | |
| 389 | if (ktime_to_ms(delta) > PLL_TIMEOUT_MS) { |
| 390 | pr_err("%s: could not lock PLL %s\n", |
| 391 | __func__, __clk_get_name(hw->clk)); |
| 392 | return -EFAULT; |
| 393 | } |
| 394 | |
| 395 | cpu_relax(); |
| 396 | } |
| 397 | |
| 398 | return 0; |
| 399 | } |
| 400 | |
Thomas Abraham | 1c4c5fe | 2013-03-09 17:02:48 +0900 | [diff] [blame] | 401 | static const struct clk_ops samsung_pll45xx_clk_ops = { |
| 402 | .recalc_rate = samsung_pll45xx_recalc_rate, |
Tomasz Figa | b4054ac | 2013-08-26 19:09:05 +0200 | [diff] [blame] | 403 | .round_rate = samsung_pll_round_rate, |
| 404 | .set_rate = samsung_pll45xx_set_rate, |
| 405 | }; |
| 406 | |
| 407 | static const struct clk_ops samsung_pll45xx_clk_min_ops = { |
| 408 | .recalc_rate = samsung_pll45xx_recalc_rate, |
Thomas Abraham | 1c4c5fe | 2013-03-09 17:02:48 +0900 | [diff] [blame] | 409 | }; |
| 410 | |
Thomas Abraham | 1c4c5fe | 2013-03-09 17:02:48 +0900 | [diff] [blame] | 411 | /* |
| 412 | * PLL46xx Clock Type |
| 413 | */ |
| 414 | |
| 415 | #define PLL46XX_MDIV_MASK (0x1FF) |
| 416 | #define PLL46XX_PDIV_MASK (0x3F) |
| 417 | #define PLL46XX_SDIV_MASK (0x7) |
| 418 | #define PLL46XX_MDIV_SHIFT (16) |
| 419 | #define PLL46XX_PDIV_SHIFT (8) |
| 420 | #define PLL46XX_SDIV_SHIFT (0) |
| 421 | |
| 422 | #define PLL46XX_KDIV_MASK (0xFFFF) |
| 423 | #define PLL4650C_KDIV_MASK (0xFFF) |
| 424 | #define PLL46XX_KDIV_SHIFT (0) |
| 425 | |
Thomas Abraham | 1c4c5fe | 2013-03-09 17:02:48 +0900 | [diff] [blame] | 426 | static unsigned long samsung_pll46xx_recalc_rate(struct clk_hw *hw, |
| 427 | unsigned long parent_rate) |
| 428 | { |
Tomasz Figa | c50d11f | 2013-08-26 19:09:06 +0200 | [diff] [blame^] | 429 | struct samsung_clk_pll *pll = to_clk_pll(hw); |
Thomas Abraham | 1c4c5fe | 2013-03-09 17:02:48 +0900 | [diff] [blame] | 430 | u32 mdiv, pdiv, sdiv, kdiv, pll_con0, pll_con1, shift; |
| 431 | u64 fvco = parent_rate; |
| 432 | |
| 433 | pll_con0 = __raw_readl(pll->con_reg); |
| 434 | pll_con1 = __raw_readl(pll->con_reg + 4); |
| 435 | mdiv = (pll_con0 >> PLL46XX_MDIV_SHIFT) & PLL46XX_MDIV_MASK; |
| 436 | pdiv = (pll_con0 >> PLL46XX_PDIV_SHIFT) & PLL46XX_PDIV_MASK; |
| 437 | sdiv = (pll_con0 >> PLL46XX_SDIV_SHIFT) & PLL46XX_SDIV_MASK; |
| 438 | kdiv = pll->type == pll_4650c ? pll_con1 & PLL4650C_KDIV_MASK : |
| 439 | pll_con1 & PLL46XX_KDIV_MASK; |
| 440 | |
| 441 | shift = pll->type == pll_4600 ? 16 : 10; |
| 442 | fvco *= (mdiv << shift) + kdiv; |
| 443 | do_div(fvco, (pdiv << sdiv)); |
| 444 | fvco >>= shift; |
| 445 | |
| 446 | return (unsigned long)fvco; |
| 447 | } |
| 448 | |
Thomas Abraham | 1c4c5fe | 2013-03-09 17:02:48 +0900 | [diff] [blame] | 449 | static const struct clk_ops samsung_pll46xx_clk_ops = { |
| 450 | .recalc_rate = samsung_pll46xx_recalc_rate, |
Thomas Abraham | 1c4c5fe | 2013-03-09 17:02:48 +0900 | [diff] [blame] | 451 | }; |
| 452 | |
Thomas Abraham | 1c4c5fe | 2013-03-09 17:02:48 +0900 | [diff] [blame] | 453 | /* |
Tomasz Figa | eb52712 | 2013-07-23 01:49:19 +0200 | [diff] [blame] | 454 | * PLL6552 Clock Type |
| 455 | */ |
| 456 | |
Tomasz Figa | eb52712 | 2013-07-23 01:49:19 +0200 | [diff] [blame] | 457 | #define PLL6552_MDIV_MASK 0x3ff |
| 458 | #define PLL6552_PDIV_MASK 0x3f |
| 459 | #define PLL6552_SDIV_MASK 0x7 |
| 460 | #define PLL6552_MDIV_SHIFT 16 |
| 461 | #define PLL6552_PDIV_SHIFT 8 |
| 462 | #define PLL6552_SDIV_SHIFT 0 |
| 463 | |
Tomasz Figa | eb52712 | 2013-07-23 01:49:19 +0200 | [diff] [blame] | 464 | static unsigned long samsung_pll6552_recalc_rate(struct clk_hw *hw, |
| 465 | unsigned long parent_rate) |
| 466 | { |
Tomasz Figa | 40ef723 | 2013-08-21 02:33:21 +0200 | [diff] [blame] | 467 | struct samsung_clk_pll *pll = to_clk_pll(hw); |
Tomasz Figa | eb52712 | 2013-07-23 01:49:19 +0200 | [diff] [blame] | 468 | u32 mdiv, pdiv, sdiv, pll_con; |
| 469 | u64 fvco = parent_rate; |
| 470 | |
Tomasz Figa | 40ef723 | 2013-08-21 02:33:21 +0200 | [diff] [blame] | 471 | pll_con = __raw_readl(pll->con_reg); |
Tomasz Figa | eb52712 | 2013-07-23 01:49:19 +0200 | [diff] [blame] | 472 | mdiv = (pll_con >> PLL6552_MDIV_SHIFT) & PLL6552_MDIV_MASK; |
| 473 | pdiv = (pll_con >> PLL6552_PDIV_SHIFT) & PLL6552_PDIV_MASK; |
| 474 | sdiv = (pll_con >> PLL6552_SDIV_SHIFT) & PLL6552_SDIV_MASK; |
| 475 | |
| 476 | fvco *= mdiv; |
| 477 | do_div(fvco, (pdiv << sdiv)); |
| 478 | |
| 479 | return (unsigned long)fvco; |
| 480 | } |
| 481 | |
| 482 | static const struct clk_ops samsung_pll6552_clk_ops = { |
| 483 | .recalc_rate = samsung_pll6552_recalc_rate, |
| 484 | }; |
| 485 | |
Tomasz Figa | eb52712 | 2013-07-23 01:49:19 +0200 | [diff] [blame] | 486 | /* |
| 487 | * PLL6553 Clock Type |
| 488 | */ |
| 489 | |
Tomasz Figa | eb52712 | 2013-07-23 01:49:19 +0200 | [diff] [blame] | 490 | #define PLL6553_MDIV_MASK 0xff |
| 491 | #define PLL6553_PDIV_MASK 0x3f |
| 492 | #define PLL6553_SDIV_MASK 0x7 |
| 493 | #define PLL6553_KDIV_MASK 0xffff |
| 494 | #define PLL6553_MDIV_SHIFT 16 |
| 495 | #define PLL6553_PDIV_SHIFT 8 |
| 496 | #define PLL6553_SDIV_SHIFT 0 |
| 497 | #define PLL6553_KDIV_SHIFT 0 |
| 498 | |
Tomasz Figa | eb52712 | 2013-07-23 01:49:19 +0200 | [diff] [blame] | 499 | static unsigned long samsung_pll6553_recalc_rate(struct clk_hw *hw, |
| 500 | unsigned long parent_rate) |
| 501 | { |
Tomasz Figa | 40ef723 | 2013-08-21 02:33:21 +0200 | [diff] [blame] | 502 | struct samsung_clk_pll *pll = to_clk_pll(hw); |
Tomasz Figa | eb52712 | 2013-07-23 01:49:19 +0200 | [diff] [blame] | 503 | u32 mdiv, pdiv, sdiv, kdiv, pll_con0, pll_con1; |
| 504 | u64 fvco = parent_rate; |
| 505 | |
Tomasz Figa | 40ef723 | 2013-08-21 02:33:21 +0200 | [diff] [blame] | 506 | pll_con0 = __raw_readl(pll->con_reg); |
| 507 | pll_con1 = __raw_readl(pll->con_reg + 0x4); |
Tomasz Figa | eb52712 | 2013-07-23 01:49:19 +0200 | [diff] [blame] | 508 | mdiv = (pll_con0 >> PLL6553_MDIV_SHIFT) & PLL6553_MDIV_MASK; |
| 509 | pdiv = (pll_con0 >> PLL6553_PDIV_SHIFT) & PLL6553_PDIV_MASK; |
| 510 | sdiv = (pll_con0 >> PLL6553_SDIV_SHIFT) & PLL6553_SDIV_MASK; |
| 511 | kdiv = (pll_con1 >> PLL6553_KDIV_SHIFT) & PLL6553_KDIV_MASK; |
| 512 | |
| 513 | fvco *= (mdiv << 16) + kdiv; |
| 514 | do_div(fvco, (pdiv << sdiv)); |
| 515 | fvco >>= 16; |
| 516 | |
| 517 | return (unsigned long)fvco; |
| 518 | } |
| 519 | |
| 520 | static const struct clk_ops samsung_pll6553_clk_ops = { |
| 521 | .recalc_rate = samsung_pll6553_recalc_rate, |
| 522 | }; |
| 523 | |
Tomasz Figa | eb52712 | 2013-07-23 01:49:19 +0200 | [diff] [blame] | 524 | /* |
Thomas Abraham | 1c4c5fe | 2013-03-09 17:02:48 +0900 | [diff] [blame] | 525 | * PLL2550x Clock Type |
| 526 | */ |
| 527 | |
| 528 | #define PLL2550X_R_MASK (0x1) |
| 529 | #define PLL2550X_P_MASK (0x3F) |
| 530 | #define PLL2550X_M_MASK (0x3FF) |
| 531 | #define PLL2550X_S_MASK (0x7) |
| 532 | #define PLL2550X_R_SHIFT (20) |
| 533 | #define PLL2550X_P_SHIFT (14) |
| 534 | #define PLL2550X_M_SHIFT (4) |
| 535 | #define PLL2550X_S_SHIFT (0) |
| 536 | |
| 537 | struct samsung_clk_pll2550x { |
| 538 | struct clk_hw hw; |
| 539 | const void __iomem *reg_base; |
| 540 | unsigned long offset; |
| 541 | }; |
| 542 | |
| 543 | #define to_clk_pll2550x(_hw) container_of(_hw, struct samsung_clk_pll2550x, hw) |
| 544 | |
| 545 | static unsigned long samsung_pll2550x_recalc_rate(struct clk_hw *hw, |
| 546 | unsigned long parent_rate) |
| 547 | { |
| 548 | struct samsung_clk_pll2550x *pll = to_clk_pll2550x(hw); |
| 549 | u32 r, p, m, s, pll_stat; |
| 550 | u64 fvco = parent_rate; |
| 551 | |
| 552 | pll_stat = __raw_readl(pll->reg_base + pll->offset * 3); |
| 553 | r = (pll_stat >> PLL2550X_R_SHIFT) & PLL2550X_R_MASK; |
| 554 | if (!r) |
| 555 | return 0; |
| 556 | p = (pll_stat >> PLL2550X_P_SHIFT) & PLL2550X_P_MASK; |
| 557 | m = (pll_stat >> PLL2550X_M_SHIFT) & PLL2550X_M_MASK; |
| 558 | s = (pll_stat >> PLL2550X_S_SHIFT) & PLL2550X_S_MASK; |
| 559 | |
| 560 | fvco *= m; |
| 561 | do_div(fvco, (p << s)); |
| 562 | |
| 563 | return (unsigned long)fvco; |
| 564 | } |
| 565 | |
Thomas Abraham | 1c4c5fe | 2013-03-09 17:02:48 +0900 | [diff] [blame] | 566 | static const struct clk_ops samsung_pll2550x_clk_ops = { |
| 567 | .recalc_rate = samsung_pll2550x_recalc_rate, |
Thomas Abraham | 1c4c5fe | 2013-03-09 17:02:48 +0900 | [diff] [blame] | 568 | }; |
| 569 | |
| 570 | struct clk * __init samsung_clk_register_pll2550x(const char *name, |
| 571 | const char *pname, const void __iomem *reg_base, |
| 572 | const unsigned long offset) |
| 573 | { |
| 574 | struct samsung_clk_pll2550x *pll; |
| 575 | struct clk *clk; |
| 576 | struct clk_init_data init; |
| 577 | |
| 578 | pll = kzalloc(sizeof(*pll), GFP_KERNEL); |
| 579 | if (!pll) { |
| 580 | pr_err("%s: could not allocate pll clk %s\n", __func__, name); |
| 581 | return NULL; |
| 582 | } |
| 583 | |
| 584 | init.name = name; |
| 585 | init.ops = &samsung_pll2550x_clk_ops; |
| 586 | init.flags = CLK_GET_RATE_NOCACHE; |
| 587 | init.parent_names = &pname; |
| 588 | init.num_parents = 1; |
| 589 | |
| 590 | pll->hw.init = &init; |
| 591 | pll->reg_base = reg_base; |
| 592 | pll->offset = offset; |
| 593 | |
| 594 | clk = clk_register(NULL, &pll->hw); |
| 595 | if (IS_ERR(clk)) { |
| 596 | pr_err("%s: failed to register pll clock %s\n", __func__, |
| 597 | name); |
| 598 | kfree(pll); |
| 599 | } |
| 600 | |
| 601 | if (clk_register_clkdev(clk, name, NULL)) |
| 602 | pr_err("%s: failed to register lookup for %s", __func__, name); |
| 603 | |
| 604 | return clk; |
| 605 | } |
Yadwinder Singh Brar | 07dc76f | 2013-06-11 15:01:07 +0530 | [diff] [blame] | 606 | |
| 607 | static void __init _samsung_clk_register_pll(struct samsung_pll_clock *pll_clk, |
| 608 | void __iomem *base) |
| 609 | { |
| 610 | struct samsung_clk_pll *pll; |
| 611 | struct clk *clk; |
| 612 | struct clk_init_data init; |
Yadwinder Singh Brar | 3ff6e0d | 2013-06-11 15:01:12 +0530 | [diff] [blame] | 613 | int ret, len; |
Yadwinder Singh Brar | 07dc76f | 2013-06-11 15:01:07 +0530 | [diff] [blame] | 614 | |
| 615 | pll = kzalloc(sizeof(*pll), GFP_KERNEL); |
| 616 | if (!pll) { |
| 617 | pr_err("%s: could not allocate pll clk %s\n", |
| 618 | __func__, pll_clk->name); |
| 619 | return; |
| 620 | } |
| 621 | |
| 622 | init.name = pll_clk->name; |
| 623 | init.flags = pll_clk->flags; |
| 624 | init.parent_names = &pll_clk->parent_name; |
| 625 | init.num_parents = 1; |
| 626 | |
Yadwinder Singh Brar | 3ff6e0d | 2013-06-11 15:01:12 +0530 | [diff] [blame] | 627 | if (pll_clk->rate_table) { |
| 628 | /* find count of rates in rate_table */ |
| 629 | for (len = 0; pll_clk->rate_table[len].rate != 0; ) |
| 630 | len++; |
| 631 | |
| 632 | pll->rate_count = len; |
| 633 | pll->rate_table = kmemdup(pll_clk->rate_table, |
| 634 | pll->rate_count * |
| 635 | sizeof(struct samsung_pll_rate_table), |
| 636 | GFP_KERNEL); |
| 637 | WARN(!pll->rate_table, |
| 638 | "%s: could not allocate rate table for %s\n", |
| 639 | __func__, pll_clk->name); |
| 640 | } |
| 641 | |
Yadwinder Singh Brar | 07dc76f | 2013-06-11 15:01:07 +0530 | [diff] [blame] | 642 | switch (pll_clk->type) { |
| 643 | /* clk_ops for 35xx and 2550 are similar */ |
| 644 | case pll_35xx: |
| 645 | case pll_2550: |
Yadwinder Singh Brar | dfa8931 | 2013-06-11 15:01:13 +0530 | [diff] [blame] | 646 | if (!pll->rate_table) |
| 647 | init.ops = &samsung_pll35xx_clk_min_ops; |
| 648 | else |
| 649 | init.ops = &samsung_pll35xx_clk_ops; |
Yadwinder Singh Brar | 07dc76f | 2013-06-11 15:01:07 +0530 | [diff] [blame] | 650 | break; |
Tomasz Figa | 52b0601 | 2013-08-26 19:09:04 +0200 | [diff] [blame] | 651 | case pll_4500: |
Tomasz Figa | b4054ac | 2013-08-26 19:09:05 +0200 | [diff] [blame] | 652 | init.ops = &samsung_pll45xx_clk_min_ops; |
| 653 | break; |
Tomasz Figa | 52b0601 | 2013-08-26 19:09:04 +0200 | [diff] [blame] | 654 | case pll_4502: |
| 655 | case pll_4508: |
Tomasz Figa | b4054ac | 2013-08-26 19:09:05 +0200 | [diff] [blame] | 656 | if (!pll->rate_table) |
| 657 | init.ops = &samsung_pll45xx_clk_min_ops; |
| 658 | else |
| 659 | init.ops = &samsung_pll45xx_clk_ops; |
Tomasz Figa | 52b0601 | 2013-08-26 19:09:04 +0200 | [diff] [blame] | 660 | break; |
Yadwinder Singh Brar | 07dc76f | 2013-06-11 15:01:07 +0530 | [diff] [blame] | 661 | /* clk_ops for 36xx and 2650 are similar */ |
| 662 | case pll_36xx: |
| 663 | case pll_2650: |
Vikas Sajjan | bb4278d | 2013-06-11 15:01:14 +0530 | [diff] [blame] | 664 | if (!pll->rate_table) |
| 665 | init.ops = &samsung_pll36xx_clk_min_ops; |
| 666 | else |
| 667 | init.ops = &samsung_pll36xx_clk_ops; |
Yadwinder Singh Brar | 07dc76f | 2013-06-11 15:01:07 +0530 | [diff] [blame] | 668 | break; |
Tomasz Figa | 40ef723 | 2013-08-21 02:33:21 +0200 | [diff] [blame] | 669 | case pll_6552: |
| 670 | init.ops = &samsung_pll6552_clk_ops; |
| 671 | break; |
| 672 | case pll_6553: |
| 673 | init.ops = &samsung_pll6553_clk_ops; |
| 674 | break; |
Tomasz Figa | c50d11f | 2013-08-26 19:09:06 +0200 | [diff] [blame^] | 675 | case pll_4600: |
| 676 | case pll_4650: |
| 677 | case pll_4650c: |
| 678 | init.ops = &samsung_pll46xx_clk_ops; |
| 679 | break; |
Yadwinder Singh Brar | 07dc76f | 2013-06-11 15:01:07 +0530 | [diff] [blame] | 680 | default: |
| 681 | pr_warn("%s: Unknown pll type for pll clk %s\n", |
| 682 | __func__, pll_clk->name); |
| 683 | } |
| 684 | |
| 685 | pll->hw.init = &init; |
| 686 | pll->type = pll_clk->type; |
| 687 | pll->lock_reg = base + pll_clk->lock_offset; |
| 688 | pll->con_reg = base + pll_clk->con_offset; |
| 689 | |
| 690 | clk = clk_register(NULL, &pll->hw); |
| 691 | if (IS_ERR(clk)) { |
| 692 | pr_err("%s: failed to register pll clock %s : %ld\n", |
| 693 | __func__, pll_clk->name, PTR_ERR(clk)); |
| 694 | kfree(pll); |
| 695 | return; |
| 696 | } |
| 697 | |
| 698 | samsung_clk_add_lookup(clk, pll_clk->id); |
| 699 | |
| 700 | if (!pll_clk->alias) |
| 701 | return; |
| 702 | |
| 703 | ret = clk_register_clkdev(clk, pll_clk->alias, pll_clk->dev_name); |
| 704 | if (ret) |
| 705 | pr_err("%s: failed to register lookup for %s : %d", |
| 706 | __func__, pll_clk->name, ret); |
| 707 | } |
| 708 | |
| 709 | void __init samsung_clk_register_pll(struct samsung_pll_clock *pll_list, |
| 710 | unsigned int nr_pll, void __iomem *base) |
| 711 | { |
| 712 | int cnt; |
| 713 | |
| 714 | for (cnt = 0; cnt < nr_pll; cnt++) |
| 715 | _samsung_clk_register_pll(&pll_list[cnt], base); |
| 716 | } |