Matt Wagantall | e9b715a | 2012-01-04 18:16:14 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2011-2012, Code Aurora Forum. All rights reserved. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License version 2 and |
| 6 | * only version 2 as published by the Free Software Foundation. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | * GNU General Public License for more details. |
| 12 | */ |
| 13 | |
| 14 | #define pr_fmt(fmt) "%s: " fmt, __func__ |
| 15 | |
| 16 | #include <linux/kernel.h> |
| 17 | #include <linux/init.h> |
| 18 | #include <linux/io.h> |
| 19 | #include <linux/delay.h> |
| 20 | #include <linux/mutex.h> |
| 21 | #include <linux/err.h> |
| 22 | #include <linux/errno.h> |
| 23 | #include <linux/cpufreq.h> |
| 24 | #include <linux/cpu.h> |
| 25 | #include <linux/regulator/consumer.h> |
| 26 | |
| 27 | #include <asm/mach-types.h> |
| 28 | #include <asm/cpu.h> |
| 29 | |
| 30 | #include <mach/board.h> |
| 31 | #include <mach/msm_iomap.h> |
| 32 | #include <mach/socinfo.h> |
| 33 | #include <mach/msm-krait-l2-accessors.h> |
| 34 | #include <mach/rpm-regulator.h> |
Matt Wagantall | 75473eb | 2012-05-31 15:23:22 -0700 | [diff] [blame] | 35 | #include <mach/rpm-regulator-smd.h> |
Matt Wagantall | e9b715a | 2012-01-04 18:16:14 -0800 | [diff] [blame] | 36 | #include <mach/msm_bus.h> |
| 37 | |
| 38 | #include "acpuclock.h" |
| 39 | #include "acpuclock-krait.h" |
| 40 | |
| 41 | /* MUX source selects. */ |
| 42 | #define PRI_SRC_SEL_SEC_SRC 0 |
| 43 | #define PRI_SRC_SEL_HFPLL 1 |
| 44 | #define PRI_SRC_SEL_HFPLL_DIV2 2 |
| 45 | #define SEC_SRC_SEL_QSB 0 |
| 46 | #define SEC_SRC_SEL_L2PLL 1 |
| 47 | #define SEC_SRC_SEL_AUX 2 |
| 48 | |
| 49 | /* PTE EFUSE register offset. */ |
| 50 | #define PTE_EFUSE 0xC0 |
| 51 | |
| 52 | static DEFINE_MUTEX(driver_lock); |
| 53 | static DEFINE_SPINLOCK(l2_lock); |
| 54 | |
| 55 | static struct drv_data { |
Matt Wagantall | 06e4a1f | 2012-06-07 18:38:13 -0700 | [diff] [blame] | 56 | struct acpu_level *acpu_freq_tbl; |
Matt Wagantall | 754ee27 | 2012-06-18 13:40:26 -0700 | [diff] [blame] | 57 | const struct acpu_level *max_acpu_lvl; |
Matt Wagantall | e9b715a | 2012-01-04 18:16:14 -0800 | [diff] [blame] | 58 | const struct l2_level *l2_freq_tbl; |
| 59 | struct scalable *scalable; |
| 60 | u32 bus_perf_client; |
| 61 | struct device *dev; |
| 62 | } drv; |
| 63 | |
| 64 | static unsigned long acpuclk_krait_get_rate(int cpu) |
| 65 | { |
| 66 | return drv.scalable[cpu].cur_speed->khz; |
| 67 | } |
| 68 | |
| 69 | /* Select a source on the primary MUX. */ |
| 70 | static void set_pri_clk_src(struct scalable *sc, u32 pri_src_sel) |
| 71 | { |
| 72 | u32 regval; |
| 73 | |
| 74 | regval = get_l2_indirect_reg(sc->l2cpmr_iaddr); |
| 75 | regval &= ~0x3; |
| 76 | regval |= (pri_src_sel & 0x3); |
| 77 | set_l2_indirect_reg(sc->l2cpmr_iaddr, regval); |
| 78 | /* Wait for switch to complete. */ |
| 79 | mb(); |
| 80 | udelay(1); |
| 81 | } |
| 82 | |
| 83 | /* Select a source on the secondary MUX. */ |
| 84 | static void set_sec_clk_src(struct scalable *sc, u32 sec_src_sel) |
| 85 | { |
| 86 | u32 regval; |
| 87 | |
| 88 | regval = get_l2_indirect_reg(sc->l2cpmr_iaddr); |
| 89 | regval &= ~(0x3 << 2); |
| 90 | regval |= ((sec_src_sel & 0x3) << 2); |
| 91 | set_l2_indirect_reg(sc->l2cpmr_iaddr, regval); |
| 92 | /* Wait for switch to complete. */ |
| 93 | mb(); |
| 94 | udelay(1); |
| 95 | } |
| 96 | |
Matt Wagantall | 302d9a3 | 2012-07-03 13:37:29 -0700 | [diff] [blame] | 97 | static int enable_rpm_vreg(struct vreg *vreg) |
Matt Wagantall | e9b715a | 2012-01-04 18:16:14 -0800 | [diff] [blame] | 98 | { |
Matt Wagantall | 302d9a3 | 2012-07-03 13:37:29 -0700 | [diff] [blame] | 99 | int ret = 0; |
Matt Wagantall | e9b715a | 2012-01-04 18:16:14 -0800 | [diff] [blame] | 100 | |
Matt Wagantall | 75473eb | 2012-05-31 15:23:22 -0700 | [diff] [blame] | 101 | if (vreg->rpm_reg) { |
Matt Wagantall | 302d9a3 | 2012-07-03 13:37:29 -0700 | [diff] [blame] | 102 | ret = rpm_regulator_enable(vreg->rpm_reg); |
| 103 | if (ret) |
Matt Wagantall | 75473eb | 2012-05-31 15:23:22 -0700 | [diff] [blame] | 104 | dev_err(drv.dev, "%s regulator enable failed (%d)\n", |
Matt Wagantall | 302d9a3 | 2012-07-03 13:37:29 -0700 | [diff] [blame] | 105 | vreg->name, ret); |
Matt Wagantall | 75473eb | 2012-05-31 15:23:22 -0700 | [diff] [blame] | 106 | } |
Matt Wagantall | 302d9a3 | 2012-07-03 13:37:29 -0700 | [diff] [blame] | 107 | |
| 108 | return ret; |
Matt Wagantall | 75473eb | 2012-05-31 15:23:22 -0700 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | static void disable_rpm_vreg(struct vreg *vreg) |
| 112 | { |
| 113 | int rc; |
| 114 | |
| 115 | if (vreg->rpm_reg) { |
| 116 | rc = rpm_regulator_disable(vreg->rpm_reg); |
| 117 | if (rc) |
| 118 | dev_err(drv.dev, "%s regulator disable failed (%d)\n", |
| 119 | vreg->name, rc); |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | /* Enable an already-configured HFPLL. */ |
| 124 | static void hfpll_enable(struct scalable *sc, bool skip_regulators) |
| 125 | { |
Matt Wagantall | e9b715a | 2012-01-04 18:16:14 -0800 | [diff] [blame] | 126 | if (!skip_regulators) { |
| 127 | /* Enable regulators required by the HFPLL. */ |
Matt Wagantall | 75473eb | 2012-05-31 15:23:22 -0700 | [diff] [blame] | 128 | enable_rpm_vreg(&sc->vreg[VREG_HFPLL_A]); |
| 129 | enable_rpm_vreg(&sc->vreg[VREG_HFPLL_B]); |
Matt Wagantall | e9b715a | 2012-01-04 18:16:14 -0800 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | /* Disable PLL bypass mode. */ |
| 133 | writel_relaxed(0x2, sc->hfpll_base + sc->hfpll_data->mode_offset); |
| 134 | |
| 135 | /* |
| 136 | * H/W requires a 5us delay between disabling the bypass and |
| 137 | * de-asserting the reset. Delay 10us just to be safe. |
| 138 | */ |
| 139 | mb(); |
| 140 | udelay(10); |
| 141 | |
| 142 | /* De-assert active-low PLL reset. */ |
| 143 | writel_relaxed(0x6, sc->hfpll_base + sc->hfpll_data->mode_offset); |
| 144 | |
| 145 | /* Wait for PLL to lock. */ |
| 146 | mb(); |
| 147 | udelay(60); |
| 148 | |
| 149 | /* Enable PLL output. */ |
| 150 | writel_relaxed(0x7, sc->hfpll_base + sc->hfpll_data->mode_offset); |
| 151 | } |
| 152 | |
| 153 | /* Disable a HFPLL for power-savings or while it's being reprogrammed. */ |
| 154 | static void hfpll_disable(struct scalable *sc, bool skip_regulators) |
| 155 | { |
Matt Wagantall | e9b715a | 2012-01-04 18:16:14 -0800 | [diff] [blame] | 156 | /* |
| 157 | * Disable the PLL output, disable test mode, enable the bypass mode, |
| 158 | * and assert the reset. |
| 159 | */ |
| 160 | writel_relaxed(0, sc->hfpll_base + sc->hfpll_data->mode_offset); |
| 161 | |
| 162 | if (!skip_regulators) { |
| 163 | /* Remove voltage votes required by the HFPLL. */ |
Matt Wagantall | 75473eb | 2012-05-31 15:23:22 -0700 | [diff] [blame] | 164 | disable_rpm_vreg(&sc->vreg[VREG_HFPLL_B]); |
| 165 | disable_rpm_vreg(&sc->vreg[VREG_HFPLL_A]); |
Matt Wagantall | e9b715a | 2012-01-04 18:16:14 -0800 | [diff] [blame] | 166 | } |
| 167 | } |
| 168 | |
| 169 | /* Program the HFPLL rate. Assumes HFPLL is already disabled. */ |
| 170 | static void hfpll_set_rate(struct scalable *sc, const struct core_speed *tgt_s) |
| 171 | { |
| 172 | writel_relaxed(tgt_s->pll_l_val, |
| 173 | sc->hfpll_base + sc->hfpll_data->l_offset); |
| 174 | } |
| 175 | |
| 176 | /* Return the L2 speed that should be applied. */ |
Matt Wagantall | 600ea50 | 2012-06-08 18:49:53 -0700 | [diff] [blame^] | 177 | static unsigned int compute_l2_level(struct scalable *sc, unsigned int vote_l) |
Matt Wagantall | e9b715a | 2012-01-04 18:16:14 -0800 | [diff] [blame] | 178 | { |
Matt Wagantall | 600ea50 | 2012-06-08 18:49:53 -0700 | [diff] [blame^] | 179 | unsigned int new_l = 0; |
Matt Wagantall | e9b715a | 2012-01-04 18:16:14 -0800 | [diff] [blame] | 180 | int cpu; |
| 181 | |
| 182 | /* Find max L2 speed vote. */ |
| 183 | sc->l2_vote = vote_l; |
Matt Wagantall | e9b715a | 2012-01-04 18:16:14 -0800 | [diff] [blame] | 184 | for_each_present_cpu(cpu) |
| 185 | new_l = max(new_l, drv.scalable[cpu].l2_vote); |
| 186 | |
| 187 | return new_l; |
| 188 | } |
| 189 | |
| 190 | /* Update the bus bandwidth request. */ |
| 191 | static void set_bus_bw(unsigned int bw) |
| 192 | { |
| 193 | int ret; |
| 194 | |
| 195 | /* Update bandwidth if request has changed. This may sleep. */ |
| 196 | ret = msm_bus_scale_client_update_request(drv.bus_perf_client, bw); |
| 197 | if (ret) |
| 198 | dev_err(drv.dev, "bandwidth request failed (%d)\n", ret); |
| 199 | } |
| 200 | |
| 201 | /* Set the CPU or L2 clock speed. */ |
| 202 | static void set_speed(struct scalable *sc, const struct core_speed *tgt_s) |
| 203 | { |
| 204 | const struct core_speed *strt_s = sc->cur_speed; |
| 205 | |
| 206 | if (strt_s->src == HFPLL && tgt_s->src == HFPLL) { |
| 207 | /* |
| 208 | * Move to an always-on source running at a frequency |
| 209 | * that does not require an elevated CPU voltage. |
| 210 | */ |
| 211 | set_sec_clk_src(sc, SEC_SRC_SEL_AUX); |
| 212 | set_pri_clk_src(sc, PRI_SRC_SEL_SEC_SRC); |
| 213 | |
| 214 | /* Re-program HFPLL. */ |
Matt Wagantall | 75473eb | 2012-05-31 15:23:22 -0700 | [diff] [blame] | 215 | hfpll_disable(sc, true); |
Matt Wagantall | e9b715a | 2012-01-04 18:16:14 -0800 | [diff] [blame] | 216 | hfpll_set_rate(sc, tgt_s); |
Matt Wagantall | 75473eb | 2012-05-31 15:23:22 -0700 | [diff] [blame] | 217 | hfpll_enable(sc, true); |
Matt Wagantall | e9b715a | 2012-01-04 18:16:14 -0800 | [diff] [blame] | 218 | |
| 219 | /* Move to HFPLL. */ |
| 220 | set_pri_clk_src(sc, tgt_s->pri_src_sel); |
| 221 | } else if (strt_s->src == HFPLL && tgt_s->src != HFPLL) { |
| 222 | set_sec_clk_src(sc, tgt_s->sec_src_sel); |
| 223 | set_pri_clk_src(sc, tgt_s->pri_src_sel); |
Matt Wagantall | 75473eb | 2012-05-31 15:23:22 -0700 | [diff] [blame] | 224 | hfpll_disable(sc, false); |
Matt Wagantall | e9b715a | 2012-01-04 18:16:14 -0800 | [diff] [blame] | 225 | } else if (strt_s->src != HFPLL && tgt_s->src == HFPLL) { |
| 226 | hfpll_set_rate(sc, tgt_s); |
Matt Wagantall | 75473eb | 2012-05-31 15:23:22 -0700 | [diff] [blame] | 227 | hfpll_enable(sc, false); |
Matt Wagantall | e9b715a | 2012-01-04 18:16:14 -0800 | [diff] [blame] | 228 | set_pri_clk_src(sc, tgt_s->pri_src_sel); |
| 229 | } else { |
| 230 | set_sec_clk_src(sc, tgt_s->sec_src_sel); |
| 231 | } |
| 232 | |
| 233 | sc->cur_speed = tgt_s; |
| 234 | } |
| 235 | |
| 236 | /* Apply any per-cpu voltage increases. */ |
| 237 | static int increase_vdd(int cpu, int vdd_core, int vdd_mem, int vdd_dig, |
| 238 | enum setrate_reason reason) |
| 239 | { |
| 240 | struct scalable *sc = &drv.scalable[cpu]; |
| 241 | int rc = 0; |
| 242 | |
| 243 | /* |
| 244 | * Increase vdd_mem active-set before vdd_dig. |
| 245 | * vdd_mem should be >= vdd_dig. |
| 246 | */ |
| 247 | if (vdd_mem > sc->vreg[VREG_MEM].cur_vdd) { |
Matt Wagantall | 75473eb | 2012-05-31 15:23:22 -0700 | [diff] [blame] | 248 | rc = rpm_regulator_set_voltage(sc->vreg[VREG_MEM].rpm_reg, |
| 249 | vdd_mem, sc->vreg[VREG_MEM].max_vdd); |
Matt Wagantall | e9b715a | 2012-01-04 18:16:14 -0800 | [diff] [blame] | 250 | if (rc) { |
| 251 | dev_err(drv.dev, |
| 252 | "vdd_mem (cpu%d) increase failed (%d)\n", |
| 253 | cpu, rc); |
| 254 | return rc; |
| 255 | } |
| 256 | sc->vreg[VREG_MEM].cur_vdd = vdd_mem; |
| 257 | } |
| 258 | |
| 259 | /* Increase vdd_dig active-set vote. */ |
| 260 | if (vdd_dig > sc->vreg[VREG_DIG].cur_vdd) { |
Matt Wagantall | 75473eb | 2012-05-31 15:23:22 -0700 | [diff] [blame] | 261 | rc = rpm_regulator_set_voltage(sc->vreg[VREG_DIG].rpm_reg, |
| 262 | vdd_dig, sc->vreg[VREG_DIG].max_vdd); |
Matt Wagantall | e9b715a | 2012-01-04 18:16:14 -0800 | [diff] [blame] | 263 | if (rc) { |
| 264 | dev_err(drv.dev, |
| 265 | "vdd_dig (cpu%d) increase failed (%d)\n", |
| 266 | cpu, rc); |
| 267 | return rc; |
| 268 | } |
| 269 | sc->vreg[VREG_DIG].cur_vdd = vdd_dig; |
| 270 | } |
| 271 | |
| 272 | /* |
| 273 | * Update per-CPU core voltage. Don't do this for the hotplug path for |
| 274 | * which it should already be correct. Attempting to set it is bad |
| 275 | * because we don't know what CPU we are running on at this point, but |
| 276 | * the CPU regulator API requires we call it from the affected CPU. |
| 277 | */ |
| 278 | if (vdd_core > sc->vreg[VREG_CORE].cur_vdd |
| 279 | && reason != SETRATE_HOTPLUG) { |
| 280 | rc = regulator_set_voltage(sc->vreg[VREG_CORE].reg, vdd_core, |
| 281 | sc->vreg[VREG_CORE].max_vdd); |
| 282 | if (rc) { |
| 283 | dev_err(drv.dev, |
| 284 | "vdd_core (cpu%d) increase failed (%d)\n", |
| 285 | cpu, rc); |
| 286 | return rc; |
| 287 | } |
| 288 | sc->vreg[VREG_CORE].cur_vdd = vdd_core; |
| 289 | } |
| 290 | |
| 291 | return rc; |
| 292 | } |
| 293 | |
| 294 | /* Apply any per-cpu voltage decreases. */ |
| 295 | static void decrease_vdd(int cpu, int vdd_core, int vdd_mem, int vdd_dig, |
| 296 | enum setrate_reason reason) |
| 297 | { |
| 298 | struct scalable *sc = &drv.scalable[cpu]; |
| 299 | int ret; |
| 300 | |
| 301 | /* |
| 302 | * Update per-CPU core voltage. This must be called on the CPU |
| 303 | * that's being affected. Don't do this in the hotplug remove path, |
| 304 | * where the rail is off and we're executing on the other CPU. |
| 305 | */ |
| 306 | if (vdd_core < sc->vreg[VREG_CORE].cur_vdd |
| 307 | && reason != SETRATE_HOTPLUG) { |
| 308 | ret = regulator_set_voltage(sc->vreg[VREG_CORE].reg, vdd_core, |
| 309 | sc->vreg[VREG_CORE].max_vdd); |
| 310 | if (ret) { |
| 311 | dev_err(drv.dev, |
| 312 | "vdd_core (cpu%d) decrease failed (%d)\n", |
| 313 | cpu, ret); |
| 314 | return; |
| 315 | } |
| 316 | sc->vreg[VREG_CORE].cur_vdd = vdd_core; |
| 317 | } |
| 318 | |
| 319 | /* Decrease vdd_dig active-set vote. */ |
| 320 | if (vdd_dig < sc->vreg[VREG_DIG].cur_vdd) { |
Matt Wagantall | 75473eb | 2012-05-31 15:23:22 -0700 | [diff] [blame] | 321 | ret = rpm_regulator_set_voltage(sc->vreg[VREG_DIG].rpm_reg, |
| 322 | vdd_dig, sc->vreg[VREG_DIG].max_vdd); |
Matt Wagantall | e9b715a | 2012-01-04 18:16:14 -0800 | [diff] [blame] | 323 | if (ret) { |
| 324 | dev_err(drv.dev, |
| 325 | "vdd_dig (cpu%d) decrease failed (%d)\n", |
| 326 | cpu, ret); |
| 327 | return; |
| 328 | } |
| 329 | sc->vreg[VREG_DIG].cur_vdd = vdd_dig; |
| 330 | } |
| 331 | |
| 332 | /* |
| 333 | * Decrease vdd_mem active-set after vdd_dig. |
| 334 | * vdd_mem should be >= vdd_dig. |
| 335 | */ |
| 336 | if (vdd_mem < sc->vreg[VREG_MEM].cur_vdd) { |
Matt Wagantall | 75473eb | 2012-05-31 15:23:22 -0700 | [diff] [blame] | 337 | ret = rpm_regulator_set_voltage(sc->vreg[VREG_MEM].rpm_reg, |
| 338 | vdd_mem, sc->vreg[VREG_MEM].max_vdd); |
Matt Wagantall | e9b715a | 2012-01-04 18:16:14 -0800 | [diff] [blame] | 339 | if (ret) { |
| 340 | dev_err(drv.dev, |
| 341 | "vdd_mem (cpu%d) decrease failed (%d)\n", |
| 342 | cpu, ret); |
| 343 | return; |
| 344 | } |
| 345 | sc->vreg[VREG_MEM].cur_vdd = vdd_mem; |
| 346 | } |
| 347 | } |
| 348 | |
| 349 | static int calculate_vdd_mem(const struct acpu_level *tgt) |
| 350 | { |
Matt Wagantall | 600ea50 | 2012-06-08 18:49:53 -0700 | [diff] [blame^] | 351 | return drv.l2_freq_tbl[tgt->l2_level].vdd_mem; |
Matt Wagantall | e9b715a | 2012-01-04 18:16:14 -0800 | [diff] [blame] | 352 | } |
| 353 | |
| 354 | static int calculate_vdd_dig(const struct acpu_level *tgt) |
| 355 | { |
| 356 | int pll_vdd_dig; |
| 357 | const int *hfpll_vdd = drv.scalable[L2].hfpll_data->vdd; |
| 358 | const u32 low_vdd_l_max = drv.scalable[L2].hfpll_data->low_vdd_l_max; |
| 359 | |
Matt Wagantall | 600ea50 | 2012-06-08 18:49:53 -0700 | [diff] [blame^] | 360 | if (drv.l2_freq_tbl[tgt->l2_level].speed.src != HFPLL) |
Matt Wagantall | e9b715a | 2012-01-04 18:16:14 -0800 | [diff] [blame] | 361 | pll_vdd_dig = hfpll_vdd[HFPLL_VDD_NONE]; |
Matt Wagantall | 600ea50 | 2012-06-08 18:49:53 -0700 | [diff] [blame^] | 362 | else if (drv.l2_freq_tbl[tgt->l2_level].speed.pll_l_val > low_vdd_l_max) |
Matt Wagantall | e9b715a | 2012-01-04 18:16:14 -0800 | [diff] [blame] | 363 | pll_vdd_dig = hfpll_vdd[HFPLL_VDD_NOM]; |
| 364 | else |
| 365 | pll_vdd_dig = hfpll_vdd[HFPLL_VDD_LOW]; |
| 366 | |
Matt Wagantall | 600ea50 | 2012-06-08 18:49:53 -0700 | [diff] [blame^] | 367 | return max(drv.l2_freq_tbl[tgt->l2_level].vdd_dig, pll_vdd_dig); |
Matt Wagantall | e9b715a | 2012-01-04 18:16:14 -0800 | [diff] [blame] | 368 | } |
| 369 | |
| 370 | static int calculate_vdd_core(const struct acpu_level *tgt) |
| 371 | { |
| 372 | return tgt->vdd_core; |
| 373 | } |
| 374 | |
| 375 | /* Set the CPU's clock rate and adjust the L2 rate, voltage and BW requests. */ |
| 376 | static int acpuclk_krait_set_rate(int cpu, unsigned long rate, |
| 377 | enum setrate_reason reason) |
| 378 | { |
| 379 | const struct core_speed *strt_acpu_s, *tgt_acpu_s; |
Matt Wagantall | e9b715a | 2012-01-04 18:16:14 -0800 | [diff] [blame] | 380 | const struct acpu_level *tgt; |
Matt Wagantall | 600ea50 | 2012-06-08 18:49:53 -0700 | [diff] [blame^] | 381 | int tgt_l2_l; |
Matt Wagantall | e9b715a | 2012-01-04 18:16:14 -0800 | [diff] [blame] | 382 | int vdd_mem, vdd_dig, vdd_core; |
| 383 | unsigned long flags; |
| 384 | int rc = 0; |
| 385 | |
Matt Wagantall | 5941a33 | 2012-07-10 23:20:44 -0700 | [diff] [blame] | 386 | if (cpu > num_possible_cpus()) |
| 387 | return -EINVAL; |
Matt Wagantall | e9b715a | 2012-01-04 18:16:14 -0800 | [diff] [blame] | 388 | |
| 389 | if (reason == SETRATE_CPUFREQ || reason == SETRATE_HOTPLUG) |
| 390 | mutex_lock(&driver_lock); |
| 391 | |
| 392 | strt_acpu_s = drv.scalable[cpu].cur_speed; |
| 393 | |
| 394 | /* Return early if rate didn't change. */ |
| 395 | if (rate == strt_acpu_s->khz) |
| 396 | goto out; |
| 397 | |
| 398 | /* Find target frequency. */ |
| 399 | for (tgt = drv.acpu_freq_tbl; tgt->speed.khz != 0; tgt++) { |
| 400 | if (tgt->speed.khz == rate) { |
| 401 | tgt_acpu_s = &tgt->speed; |
| 402 | break; |
| 403 | } |
| 404 | } |
| 405 | if (tgt->speed.khz == 0) { |
| 406 | rc = -EINVAL; |
| 407 | goto out; |
| 408 | } |
| 409 | |
| 410 | /* Calculate voltage requirements for the current CPU. */ |
| 411 | vdd_mem = calculate_vdd_mem(tgt); |
| 412 | vdd_dig = calculate_vdd_dig(tgt); |
| 413 | vdd_core = calculate_vdd_core(tgt); |
| 414 | |
| 415 | /* Increase VDD levels if needed. */ |
| 416 | if (reason == SETRATE_CPUFREQ || reason == SETRATE_HOTPLUG) { |
| 417 | rc = increase_vdd(cpu, vdd_core, vdd_mem, vdd_dig, reason); |
| 418 | if (rc) |
| 419 | goto out; |
| 420 | } |
| 421 | |
| 422 | pr_debug("Switching from ACPU%d rate %lu KHz -> %lu KHz\n", |
| 423 | cpu, strt_acpu_s->khz, tgt_acpu_s->khz); |
| 424 | |
| 425 | /* Set the new CPU speed. */ |
| 426 | set_speed(&drv.scalable[cpu], tgt_acpu_s); |
| 427 | |
| 428 | /* |
| 429 | * Update the L2 vote and apply the rate change. A spinlock is |
| 430 | * necessary to ensure L2 rate is calculated and set atomically |
| 431 | * with the CPU frequency, even if acpuclk_krait_set_rate() is |
| 432 | * called from an atomic context and the driver_lock mutex is not |
| 433 | * acquired. |
| 434 | */ |
| 435 | spin_lock_irqsave(&l2_lock, flags); |
| 436 | tgt_l2_l = compute_l2_level(&drv.scalable[cpu], tgt->l2_level); |
Matt Wagantall | 600ea50 | 2012-06-08 18:49:53 -0700 | [diff] [blame^] | 437 | set_speed(&drv.scalable[L2], &drv.l2_freq_tbl[tgt_l2_l].speed); |
Matt Wagantall | e9b715a | 2012-01-04 18:16:14 -0800 | [diff] [blame] | 438 | spin_unlock_irqrestore(&l2_lock, flags); |
| 439 | |
| 440 | /* Nothing else to do for power collapse or SWFI. */ |
| 441 | if (reason == SETRATE_PC || reason == SETRATE_SWFI) |
| 442 | goto out; |
| 443 | |
| 444 | /* Update bus bandwith request. */ |
Matt Wagantall | 600ea50 | 2012-06-08 18:49:53 -0700 | [diff] [blame^] | 445 | set_bus_bw(drv.l2_freq_tbl[tgt_l2_l].bw_level); |
Matt Wagantall | e9b715a | 2012-01-04 18:16:14 -0800 | [diff] [blame] | 446 | |
| 447 | /* Drop VDD levels if we can. */ |
| 448 | decrease_vdd(cpu, vdd_core, vdd_mem, vdd_dig, reason); |
| 449 | |
| 450 | pr_debug("ACPU%d speed change complete\n", cpu); |
| 451 | |
| 452 | out: |
| 453 | if (reason == SETRATE_CPUFREQ || reason == SETRATE_HOTPLUG) |
| 454 | mutex_unlock(&driver_lock); |
| 455 | return rc; |
| 456 | } |
| 457 | |
| 458 | /* Initialize a HFPLL at a given rate and enable it. */ |
| 459 | static void __init hfpll_init(struct scalable *sc, |
| 460 | const struct core_speed *tgt_s) |
| 461 | { |
| 462 | pr_debug("Initializing HFPLL%d\n", sc - drv.scalable); |
| 463 | |
| 464 | /* Disable the PLL for re-programming. */ |
Matt Wagantall | 75473eb | 2012-05-31 15:23:22 -0700 | [diff] [blame] | 465 | hfpll_disable(sc, true); |
Matt Wagantall | e9b715a | 2012-01-04 18:16:14 -0800 | [diff] [blame] | 466 | |
| 467 | /* Configure PLL parameters for integer mode. */ |
| 468 | writel_relaxed(sc->hfpll_data->config_val, |
| 469 | sc->hfpll_base + sc->hfpll_data->config_offset); |
| 470 | writel_relaxed(0, sc->hfpll_base + sc->hfpll_data->m_offset); |
| 471 | writel_relaxed(1, sc->hfpll_base + sc->hfpll_data->n_offset); |
| 472 | |
Matt Wagantall | 06e4a1f | 2012-06-07 18:38:13 -0700 | [diff] [blame] | 473 | /* Program droop controller, if supported */ |
| 474 | if (sc->hfpll_data->has_droop_ctl) |
| 475 | writel_relaxed(sc->hfpll_data->droop_val, |
| 476 | sc->hfpll_base + sc->hfpll_data->droop_offset); |
| 477 | |
Matt Wagantall | e9b715a | 2012-01-04 18:16:14 -0800 | [diff] [blame] | 478 | /* Set an initial rate and enable the PLL. */ |
| 479 | hfpll_set_rate(sc, tgt_s); |
Matt Wagantall | 75473eb | 2012-05-31 15:23:22 -0700 | [diff] [blame] | 480 | hfpll_enable(sc, false); |
| 481 | } |
| 482 | |
Matt Wagantall | 302d9a3 | 2012-07-03 13:37:29 -0700 | [diff] [blame] | 483 | static int __cpuinit rpm_regulator_init(struct scalable *sc, enum vregs vreg, |
Matt Wagantall | 754ee27 | 2012-06-18 13:40:26 -0700 | [diff] [blame] | 484 | int vdd, bool enable) |
Matt Wagantall | 75473eb | 2012-05-31 15:23:22 -0700 | [diff] [blame] | 485 | { |
| 486 | int ret; |
| 487 | |
| 488 | if (!sc->vreg[vreg].name) |
Matt Wagantall | 302d9a3 | 2012-07-03 13:37:29 -0700 | [diff] [blame] | 489 | return 0; |
Matt Wagantall | 75473eb | 2012-05-31 15:23:22 -0700 | [diff] [blame] | 490 | |
| 491 | sc->vreg[vreg].rpm_reg = rpm_regulator_get(drv.dev, |
| 492 | sc->vreg[vreg].name); |
| 493 | if (IS_ERR(sc->vreg[vreg].rpm_reg)) { |
Matt Wagantall | 302d9a3 | 2012-07-03 13:37:29 -0700 | [diff] [blame] | 494 | ret = PTR_ERR(sc->vreg[vreg].rpm_reg); |
| 495 | dev_err(drv.dev, "rpm_regulator_get(%s) failed (%d)\n", |
| 496 | sc->vreg[vreg].name, ret); |
| 497 | goto err_get; |
Matt Wagantall | 75473eb | 2012-05-31 15:23:22 -0700 | [diff] [blame] | 498 | } |
| 499 | |
| 500 | ret = rpm_regulator_set_voltage(sc->vreg[vreg].rpm_reg, vdd, |
| 501 | sc->vreg[vreg].max_vdd); |
| 502 | if (ret) { |
| 503 | dev_err(drv.dev, "%s initialization failed (%d)\n", |
| 504 | sc->vreg[vreg].name, ret); |
Matt Wagantall | 302d9a3 | 2012-07-03 13:37:29 -0700 | [diff] [blame] | 505 | goto err_conf; |
Matt Wagantall | 75473eb | 2012-05-31 15:23:22 -0700 | [diff] [blame] | 506 | } |
| 507 | sc->vreg[vreg].cur_vdd = vdd; |
| 508 | |
Matt Wagantall | 302d9a3 | 2012-07-03 13:37:29 -0700 | [diff] [blame] | 509 | if (enable) { |
| 510 | ret = enable_rpm_vreg(&sc->vreg[vreg]); |
| 511 | if (ret) |
| 512 | goto err_conf; |
| 513 | } |
| 514 | |
| 515 | return 0; |
| 516 | |
| 517 | err_conf: |
| 518 | rpm_regulator_put(sc->vreg[vreg].rpm_reg); |
| 519 | err_get: |
| 520 | return ret; |
| 521 | } |
| 522 | |
| 523 | static void __cpuinit rpm_regulator_cleanup(struct scalable *sc, |
| 524 | enum vregs vreg) |
| 525 | { |
| 526 | if (!sc->vreg[vreg].rpm_reg) |
| 527 | return; |
| 528 | |
| 529 | disable_rpm_vreg(&sc->vreg[vreg]); |
| 530 | rpm_regulator_put(sc->vreg[vreg].rpm_reg); |
Matt Wagantall | e9b715a | 2012-01-04 18:16:14 -0800 | [diff] [blame] | 531 | } |
| 532 | |
| 533 | /* Voltage regulator initialization. */ |
Matt Wagantall | 302d9a3 | 2012-07-03 13:37:29 -0700 | [diff] [blame] | 534 | static int __cpuinit regulator_init(struct scalable *sc) |
Matt Wagantall | e9b715a | 2012-01-04 18:16:14 -0800 | [diff] [blame] | 535 | { |
Matt Wagantall | 754ee27 | 2012-06-18 13:40:26 -0700 | [diff] [blame] | 536 | int ret, vdd_mem, vdd_dig, vdd_core; |
Matt Wagantall | e9b715a | 2012-01-04 18:16:14 -0800 | [diff] [blame] | 537 | |
Matt Wagantall | 754ee27 | 2012-06-18 13:40:26 -0700 | [diff] [blame] | 538 | vdd_mem = calculate_vdd_mem(drv.max_acpu_lvl); |
| 539 | vdd_dig = calculate_vdd_dig(drv.max_acpu_lvl); |
Matt Wagantall | e9b715a | 2012-01-04 18:16:14 -0800 | [diff] [blame] | 540 | |
Matt Wagantall | 302d9a3 | 2012-07-03 13:37:29 -0700 | [diff] [blame] | 541 | ret = rpm_regulator_init(sc, VREG_MEM, vdd_mem, true); |
| 542 | if (ret) |
| 543 | goto err_mem; |
| 544 | ret = rpm_regulator_init(sc, VREG_DIG, vdd_dig, true); |
| 545 | if (ret) |
| 546 | goto err_dig; |
| 547 | ret = rpm_regulator_init(sc, VREG_HFPLL_A, |
Matt Wagantall | 754ee27 | 2012-06-18 13:40:26 -0700 | [diff] [blame] | 548 | sc->vreg[VREG_HFPLL_A].max_vdd, false); |
Matt Wagantall | 302d9a3 | 2012-07-03 13:37:29 -0700 | [diff] [blame] | 549 | if (ret) |
| 550 | goto err_hfpll_a; |
| 551 | ret = rpm_regulator_init(sc, VREG_HFPLL_B, |
Matt Wagantall | 754ee27 | 2012-06-18 13:40:26 -0700 | [diff] [blame] | 552 | sc->vreg[VREG_HFPLL_B].max_vdd, false); |
Matt Wagantall | 302d9a3 | 2012-07-03 13:37:29 -0700 | [diff] [blame] | 553 | if (ret) |
| 554 | goto err_hfpll_b; |
Matt Wagantall | 75473eb | 2012-05-31 15:23:22 -0700 | [diff] [blame] | 555 | |
Matt Wagantall | 754ee27 | 2012-06-18 13:40:26 -0700 | [diff] [blame] | 556 | /* Setup Krait CPU regulators and initial core voltage. */ |
| 557 | sc->vreg[VREG_CORE].reg = regulator_get(drv.dev, |
| 558 | sc->vreg[VREG_CORE].name); |
| 559 | if (IS_ERR(sc->vreg[VREG_CORE].reg)) { |
Matt Wagantall | 302d9a3 | 2012-07-03 13:37:29 -0700 | [diff] [blame] | 560 | ret = PTR_ERR(sc->vreg[VREG_CORE].reg); |
| 561 | dev_err(drv.dev, "regulator_get(%s) failed (%d)\n", |
| 562 | sc->vreg[VREG_CORE].name, ret); |
| 563 | goto err_core_get; |
Matt Wagantall | 754ee27 | 2012-06-18 13:40:26 -0700 | [diff] [blame] | 564 | } |
| 565 | vdd_core = calculate_vdd_core(drv.max_acpu_lvl); |
| 566 | ret = regulator_set_voltage(sc->vreg[VREG_CORE].reg, vdd_core, |
| 567 | sc->vreg[VREG_CORE].max_vdd); |
| 568 | if (ret) { |
| 569 | dev_err(drv.dev, "regulator_set_voltage(%s) (%d)\n", |
| 570 | sc->vreg[VREG_CORE].name, ret); |
Matt Wagantall | 302d9a3 | 2012-07-03 13:37:29 -0700 | [diff] [blame] | 571 | goto err_core_conf; |
Matt Wagantall | 754ee27 | 2012-06-18 13:40:26 -0700 | [diff] [blame] | 572 | } |
| 573 | sc->vreg[VREG_CORE].cur_vdd = vdd_core; |
| 574 | ret = regulator_set_optimum_mode(sc->vreg[VREG_CORE].reg, |
| 575 | sc->vreg[VREG_CORE].peak_ua); |
| 576 | if (ret < 0) { |
| 577 | dev_err(drv.dev, "regulator_set_optimum_mode(%s) failed (%d)\n", |
| 578 | sc->vreg[VREG_CORE].name, ret); |
Matt Wagantall | 302d9a3 | 2012-07-03 13:37:29 -0700 | [diff] [blame] | 579 | goto err_core_conf; |
Matt Wagantall | 754ee27 | 2012-06-18 13:40:26 -0700 | [diff] [blame] | 580 | } |
| 581 | ret = regulator_enable(sc->vreg[VREG_CORE].reg); |
| 582 | if (ret) { |
| 583 | dev_err(drv.dev, "regulator_enable(%s) failed (%d)\n", |
| 584 | sc->vreg[VREG_CORE].name, ret); |
Matt Wagantall | 302d9a3 | 2012-07-03 13:37:29 -0700 | [diff] [blame] | 585 | goto err_core_conf; |
Matt Wagantall | e9b715a | 2012-01-04 18:16:14 -0800 | [diff] [blame] | 586 | } |
Matt Wagantall | 302d9a3 | 2012-07-03 13:37:29 -0700 | [diff] [blame] | 587 | |
| 588 | return 0; |
| 589 | |
| 590 | err_core_conf: |
| 591 | regulator_put(sc->vreg[VREG_CORE].reg); |
| 592 | err_core_get: |
| 593 | rpm_regulator_cleanup(sc, VREG_HFPLL_B); |
| 594 | err_hfpll_b: |
| 595 | rpm_regulator_cleanup(sc, VREG_HFPLL_A); |
| 596 | err_hfpll_a: |
| 597 | rpm_regulator_cleanup(sc, VREG_DIG); |
| 598 | err_dig: |
| 599 | rpm_regulator_cleanup(sc, VREG_MEM); |
| 600 | err_mem: |
| 601 | return ret; |
| 602 | } |
| 603 | |
| 604 | static void __cpuinit regulator_cleanup(struct scalable *sc) |
| 605 | { |
| 606 | regulator_disable(sc->vreg[VREG_CORE].reg); |
| 607 | regulator_put(sc->vreg[VREG_CORE].reg); |
| 608 | rpm_regulator_cleanup(sc, VREG_HFPLL_B); |
| 609 | rpm_regulator_cleanup(sc, VREG_HFPLL_A); |
| 610 | rpm_regulator_cleanup(sc, VREG_DIG); |
| 611 | rpm_regulator_cleanup(sc, VREG_MEM); |
Matt Wagantall | e9b715a | 2012-01-04 18:16:14 -0800 | [diff] [blame] | 612 | } |
| 613 | |
| 614 | /* Set initial rate for a given core. */ |
Matt Wagantall | 302d9a3 | 2012-07-03 13:37:29 -0700 | [diff] [blame] | 615 | static int __cpuinit init_clock_sources(struct scalable *sc, |
Matt Wagantall | 754ee27 | 2012-06-18 13:40:26 -0700 | [diff] [blame] | 616 | const struct core_speed *tgt_s) |
Matt Wagantall | e9b715a | 2012-01-04 18:16:14 -0800 | [diff] [blame] | 617 | { |
| 618 | u32 regval; |
Matt Wagantall | 06e4a1f | 2012-06-07 18:38:13 -0700 | [diff] [blame] | 619 | void __iomem *aux_reg; |
Matt Wagantall | e9b715a | 2012-01-04 18:16:14 -0800 | [diff] [blame] | 620 | |
| 621 | /* Program AUX source input to the secondary MUX. */ |
Matt Wagantall | 06e4a1f | 2012-06-07 18:38:13 -0700 | [diff] [blame] | 622 | if (sc->aux_clk_sel_phys) { |
| 623 | aux_reg = ioremap(sc->aux_clk_sel_phys, 4); |
Matt Wagantall | 302d9a3 | 2012-07-03 13:37:29 -0700 | [diff] [blame] | 624 | if (!aux_reg) |
| 625 | return -ENOMEM; |
Matt Wagantall | 06e4a1f | 2012-06-07 18:38:13 -0700 | [diff] [blame] | 626 | writel_relaxed(sc->aux_clk_sel, aux_reg); |
| 627 | iounmap(aux_reg); |
| 628 | } |
Matt Wagantall | e9b715a | 2012-01-04 18:16:14 -0800 | [diff] [blame] | 629 | |
| 630 | /* Switch away from the HFPLL while it's re-initialized. */ |
| 631 | set_sec_clk_src(sc, SEC_SRC_SEL_AUX); |
| 632 | set_pri_clk_src(sc, PRI_SRC_SEL_SEC_SRC); |
| 633 | hfpll_init(sc, tgt_s); |
| 634 | |
| 635 | /* Set PRI_SRC_SEL_HFPLL_DIV2 divider to div-2. */ |
| 636 | regval = get_l2_indirect_reg(sc->l2cpmr_iaddr); |
| 637 | regval &= ~(0x3 << 6); |
| 638 | set_l2_indirect_reg(sc->l2cpmr_iaddr, regval); |
| 639 | |
| 640 | /* Switch to the target clock source. */ |
| 641 | set_sec_clk_src(sc, tgt_s->sec_src_sel); |
| 642 | set_pri_clk_src(sc, tgt_s->pri_src_sel); |
| 643 | sc->cur_speed = tgt_s; |
Matt Wagantall | 302d9a3 | 2012-07-03 13:37:29 -0700 | [diff] [blame] | 644 | |
| 645 | return 0; |
Matt Wagantall | e9b715a | 2012-01-04 18:16:14 -0800 | [diff] [blame] | 646 | } |
| 647 | |
Matt Wagantall | 302d9a3 | 2012-07-03 13:37:29 -0700 | [diff] [blame] | 648 | static int __cpuinit per_cpu_init(int cpu) |
Matt Wagantall | e9b715a | 2012-01-04 18:16:14 -0800 | [diff] [blame] | 649 | { |
Matt Wagantall | 754ee27 | 2012-06-18 13:40:26 -0700 | [diff] [blame] | 650 | struct scalable *sc = &drv.scalable[cpu]; |
Matt Wagantall | 302d9a3 | 2012-07-03 13:37:29 -0700 | [diff] [blame] | 651 | int ret; |
Matt Wagantall | e9b715a | 2012-01-04 18:16:14 -0800 | [diff] [blame] | 652 | |
Matt Wagantall | 754ee27 | 2012-06-18 13:40:26 -0700 | [diff] [blame] | 653 | sc->hfpll_base = ioremap(sc->hfpll_phys_base, SZ_32); |
Matt Wagantall | 302d9a3 | 2012-07-03 13:37:29 -0700 | [diff] [blame] | 654 | if (!sc->hfpll_base) { |
| 655 | ret = -ENOMEM; |
| 656 | goto err_ioremap; |
| 657 | } |
Matt Wagantall | 754ee27 | 2012-06-18 13:40:26 -0700 | [diff] [blame] | 658 | |
Matt Wagantall | 302d9a3 | 2012-07-03 13:37:29 -0700 | [diff] [blame] | 659 | ret = regulator_init(sc); |
| 660 | if (ret) |
| 661 | goto err_regulators; |
Matt Wagantall | 754ee27 | 2012-06-18 13:40:26 -0700 | [diff] [blame] | 662 | |
Matt Wagantall | 302d9a3 | 2012-07-03 13:37:29 -0700 | [diff] [blame] | 663 | ret = init_clock_sources(sc, &drv.max_acpu_lvl->speed); |
| 664 | if (ret) |
| 665 | goto err_clocks; |
Matt Wagantall | 754ee27 | 2012-06-18 13:40:26 -0700 | [diff] [blame] | 666 | sc->l2_vote = drv.max_acpu_lvl->l2_level; |
Matt Wagantall | 754ee27 | 2012-06-18 13:40:26 -0700 | [diff] [blame] | 667 | sc->initialized = true; |
Matt Wagantall | 302d9a3 | 2012-07-03 13:37:29 -0700 | [diff] [blame] | 668 | |
| 669 | return 0; |
| 670 | |
| 671 | err_clocks: |
| 672 | regulator_cleanup(sc); |
| 673 | err_regulators: |
| 674 | iounmap(sc->hfpll_base); |
| 675 | err_ioremap: |
| 676 | return ret; |
Matt Wagantall | e9b715a | 2012-01-04 18:16:14 -0800 | [diff] [blame] | 677 | } |
| 678 | |
| 679 | /* Register with bus driver. */ |
Matt Wagantall | 754ee27 | 2012-06-18 13:40:26 -0700 | [diff] [blame] | 680 | static void __init bus_init(struct msm_bus_scale_pdata *bus_scale_data) |
Matt Wagantall | e9b715a | 2012-01-04 18:16:14 -0800 | [diff] [blame] | 681 | { |
| 682 | int ret; |
| 683 | |
| 684 | drv.bus_perf_client = msm_bus_scale_register_client(bus_scale_data); |
| 685 | if (!drv.bus_perf_client) { |
| 686 | dev_err(drv.dev, "unable to register bus client\n"); |
| 687 | BUG(); |
| 688 | } |
| 689 | |
Matt Wagantall | 754ee27 | 2012-06-18 13:40:26 -0700 | [diff] [blame] | 690 | ret = msm_bus_scale_client_update_request(drv.bus_perf_client, |
Matt Wagantall | 600ea50 | 2012-06-08 18:49:53 -0700 | [diff] [blame^] | 691 | drv.l2_freq_tbl[drv.max_acpu_lvl->l2_level].bw_level); |
Matt Wagantall | e9b715a | 2012-01-04 18:16:14 -0800 | [diff] [blame] | 692 | if (ret) |
| 693 | dev_err(drv.dev, "initial bandwidth req failed (%d)\n", ret); |
| 694 | } |
| 695 | |
| 696 | #ifdef CONFIG_CPU_FREQ_MSM |
| 697 | static struct cpufreq_frequency_table freq_table[NR_CPUS][35]; |
| 698 | |
| 699 | static void __init cpufreq_table_init(void) |
| 700 | { |
| 701 | int cpu; |
| 702 | |
| 703 | for_each_possible_cpu(cpu) { |
| 704 | int i, freq_cnt = 0; |
| 705 | /* Construct the freq_table tables from acpu_freq_tbl. */ |
| 706 | for (i = 0; drv.acpu_freq_tbl[i].speed.khz != 0 |
| 707 | && freq_cnt < ARRAY_SIZE(*freq_table); i++) { |
| 708 | if (drv.acpu_freq_tbl[i].use_for_scaling) { |
| 709 | freq_table[cpu][freq_cnt].index = freq_cnt; |
| 710 | freq_table[cpu][freq_cnt].frequency |
| 711 | = drv.acpu_freq_tbl[i].speed.khz; |
| 712 | freq_cnt++; |
| 713 | } |
| 714 | } |
| 715 | /* freq_table not big enough to store all usable freqs. */ |
| 716 | BUG_ON(drv.acpu_freq_tbl[i].speed.khz != 0); |
| 717 | |
| 718 | freq_table[cpu][freq_cnt].index = freq_cnt; |
| 719 | freq_table[cpu][freq_cnt].frequency = CPUFREQ_TABLE_END; |
| 720 | |
| 721 | dev_info(drv.dev, "CPU%d: %d frequencies supported\n", |
| 722 | cpu, freq_cnt); |
| 723 | |
| 724 | /* Register table with CPUFreq. */ |
| 725 | cpufreq_frequency_table_get_attr(freq_table[cpu], cpu); |
| 726 | } |
| 727 | } |
| 728 | #else |
| 729 | static void __init cpufreq_table_init(void) {} |
| 730 | #endif |
| 731 | |
| 732 | #define HOT_UNPLUG_KHZ STBY_KHZ |
| 733 | static int __cpuinit acpuclk_cpu_callback(struct notifier_block *nfb, |
| 734 | unsigned long action, void *hcpu) |
| 735 | { |
| 736 | static int prev_khz[NR_CPUS]; |
| 737 | int rc, cpu = (int)hcpu; |
| 738 | struct scalable *sc = &drv.scalable[cpu]; |
| 739 | |
| 740 | switch (action & ~CPU_TASKS_FROZEN) { |
| 741 | case CPU_DEAD: |
| 742 | prev_khz[cpu] = acpuclk_krait_get_rate(cpu); |
| 743 | /* Fall through. */ |
| 744 | case CPU_UP_CANCELED: |
| 745 | acpuclk_krait_set_rate(cpu, HOT_UNPLUG_KHZ, SETRATE_HOTPLUG); |
| 746 | regulator_set_optimum_mode(sc->vreg[VREG_CORE].reg, 0); |
| 747 | break; |
| 748 | case CPU_UP_PREPARE: |
Matt Wagantall | 754ee27 | 2012-06-18 13:40:26 -0700 | [diff] [blame] | 749 | if (!sc->initialized) { |
Matt Wagantall | 302d9a3 | 2012-07-03 13:37:29 -0700 | [diff] [blame] | 750 | rc = per_cpu_init(cpu); |
| 751 | if (rc) |
| 752 | return NOTIFY_BAD; |
Matt Wagantall | 754ee27 | 2012-06-18 13:40:26 -0700 | [diff] [blame] | 753 | break; |
| 754 | } |
Matt Wagantall | e9b715a | 2012-01-04 18:16:14 -0800 | [diff] [blame] | 755 | if (WARN_ON(!prev_khz[cpu])) |
| 756 | return NOTIFY_BAD; |
| 757 | rc = regulator_set_optimum_mode(sc->vreg[VREG_CORE].reg, |
| 758 | sc->vreg[VREG_CORE].peak_ua); |
| 759 | if (rc < 0) |
| 760 | return NOTIFY_BAD; |
| 761 | acpuclk_krait_set_rate(cpu, prev_khz[cpu], SETRATE_HOTPLUG); |
| 762 | break; |
| 763 | default: |
| 764 | break; |
| 765 | } |
| 766 | |
| 767 | return NOTIFY_OK; |
| 768 | } |
| 769 | |
| 770 | static struct notifier_block __cpuinitdata acpuclk_cpu_notifier = { |
| 771 | .notifier_call = acpuclk_cpu_callback, |
| 772 | }; |
| 773 | |
Matt Wagantall | 06e4a1f | 2012-06-07 18:38:13 -0700 | [diff] [blame] | 774 | static const int krait_needs_vmin(void) |
| 775 | { |
| 776 | switch (read_cpuid_id()) { |
| 777 | case 0x511F04D0: /* KR28M2A20 */ |
| 778 | case 0x511F04D1: /* KR28M2A21 */ |
| 779 | case 0x510F06F0: /* KR28M4A10 */ |
| 780 | return 1; |
| 781 | default: |
| 782 | return 0; |
| 783 | }; |
| 784 | } |
| 785 | |
| 786 | static void krait_apply_vmin(struct acpu_level *tbl) |
| 787 | { |
| 788 | for (; tbl->speed.khz != 0; tbl++) |
| 789 | if (tbl->vdd_core < 1150000) |
| 790 | tbl->vdd_core = 1150000; |
| 791 | } |
| 792 | |
Matt Wagantall | 754ee27 | 2012-06-18 13:40:26 -0700 | [diff] [blame] | 793 | static void __init select_freq_plan(struct acpu_level *const *pvs_tbl, |
| 794 | u32 qfprom_phys) |
Matt Wagantall | e9b715a | 2012-01-04 18:16:14 -0800 | [diff] [blame] | 795 | { |
Matt Wagantall | 754ee27 | 2012-06-18 13:40:26 -0700 | [diff] [blame] | 796 | const struct acpu_level *l; |
Matt Wagantall | e9b715a | 2012-01-04 18:16:14 -0800 | [diff] [blame] | 797 | void __iomem *qfprom_base; |
| 798 | u32 pte_efuse, pvs, tbl_idx; |
Matt Wagantall | f5cc389 | 2012-06-07 19:47:02 -0700 | [diff] [blame] | 799 | char *pvs_names[] = { "Slow", "Nominal", "Fast", "Faster", "Unknown" }; |
Matt Wagantall | e9b715a | 2012-01-04 18:16:14 -0800 | [diff] [blame] | 800 | |
| 801 | qfprom_base = ioremap(qfprom_phys, SZ_256); |
| 802 | /* Select frequency tables. */ |
| 803 | if (qfprom_base) { |
| 804 | pte_efuse = readl_relaxed(qfprom_base + PTE_EFUSE); |
| 805 | pvs = (pte_efuse >> 10) & 0x7; |
| 806 | iounmap(qfprom_base); |
| 807 | if (pvs == 0x7) |
| 808 | pvs = (pte_efuse >> 13) & 0x7; |
| 809 | |
| 810 | switch (pvs) { |
| 811 | case 0x0: |
| 812 | case 0x7: |
| 813 | tbl_idx = PVS_SLOW; |
| 814 | break; |
| 815 | case 0x1: |
| 816 | tbl_idx = PVS_NOMINAL; |
| 817 | break; |
| 818 | case 0x3: |
| 819 | tbl_idx = PVS_FAST; |
| 820 | break; |
Matt Wagantall | f5cc389 | 2012-06-07 19:47:02 -0700 | [diff] [blame] | 821 | case 0x4: |
| 822 | tbl_idx = PVS_FASTER; |
| 823 | break; |
Matt Wagantall | e9b715a | 2012-01-04 18:16:14 -0800 | [diff] [blame] | 824 | default: |
| 825 | tbl_idx = PVS_UNKNOWN; |
| 826 | break; |
| 827 | } |
| 828 | } else { |
| 829 | tbl_idx = PVS_UNKNOWN; |
| 830 | dev_err(drv.dev, "Unable to map QFPROM base\n"); |
| 831 | } |
Matt Wagantall | f5cc389 | 2012-06-07 19:47:02 -0700 | [diff] [blame] | 832 | if (tbl_idx == PVS_UNKNOWN || !pvs_tbl[tbl_idx]) { |
Matt Wagantall | e9b715a | 2012-01-04 18:16:14 -0800 | [diff] [blame] | 833 | tbl_idx = PVS_SLOW; |
| 834 | dev_warn(drv.dev, "ACPU PVS: Defaulting to %s\n", |
| 835 | pvs_names[tbl_idx]); |
Matt Wagantall | f5cc389 | 2012-06-07 19:47:02 -0700 | [diff] [blame] | 836 | } else { |
| 837 | dev_info(drv.dev, "ACPU PVS: %s\n", pvs_names[tbl_idx]); |
Matt Wagantall | e9b715a | 2012-01-04 18:16:14 -0800 | [diff] [blame] | 838 | } |
| 839 | drv.acpu_freq_tbl = pvs_tbl[tbl_idx]; |
Matt Wagantall | f5cc389 | 2012-06-07 19:47:02 -0700 | [diff] [blame] | 840 | BUG_ON(!drv.acpu_freq_tbl); |
Matt Wagantall | e9b715a | 2012-01-04 18:16:14 -0800 | [diff] [blame] | 841 | |
Matt Wagantall | 06e4a1f | 2012-06-07 18:38:13 -0700 | [diff] [blame] | 842 | if (krait_needs_vmin()) |
| 843 | krait_apply_vmin(drv.acpu_freq_tbl); |
| 844 | |
Matt Wagantall | e9b715a | 2012-01-04 18:16:14 -0800 | [diff] [blame] | 845 | /* Find the max supported scaling frequency. */ |
| 846 | for (l = drv.acpu_freq_tbl; l->speed.khz != 0; l++) |
| 847 | if (l->use_for_scaling) |
Matt Wagantall | 754ee27 | 2012-06-18 13:40:26 -0700 | [diff] [blame] | 848 | drv.max_acpu_lvl = l; |
| 849 | BUG_ON(!drv.max_acpu_lvl); |
Matt Wagantall | e9b715a | 2012-01-04 18:16:14 -0800 | [diff] [blame] | 850 | dev_info(drv.dev, "Max ACPU freq: %lu KHz\n", |
Matt Wagantall | 754ee27 | 2012-06-18 13:40:26 -0700 | [diff] [blame] | 851 | drv.max_acpu_lvl->speed.khz); |
Matt Wagantall | e9b715a | 2012-01-04 18:16:14 -0800 | [diff] [blame] | 852 | } |
| 853 | |
| 854 | static struct acpuclk_data acpuclk_krait_data = { |
| 855 | .set_rate = acpuclk_krait_set_rate, |
| 856 | .get_rate = acpuclk_krait_get_rate, |
| 857 | .power_collapse_khz = STBY_KHZ, |
| 858 | .wait_for_irq_khz = STBY_KHZ, |
| 859 | }; |
| 860 | |
| 861 | int __init acpuclk_krait_init(struct device *dev, |
| 862 | const struct acpuclk_krait_params *params) |
| 863 | { |
Matt Wagantall | 754ee27 | 2012-06-18 13:40:26 -0700 | [diff] [blame] | 864 | struct scalable *l2; |
Matt Wagantall | 302d9a3 | 2012-07-03 13:37:29 -0700 | [diff] [blame] | 865 | int cpu, rc; |
Matt Wagantall | e9b715a | 2012-01-04 18:16:14 -0800 | [diff] [blame] | 866 | |
| 867 | drv.scalable = params->scalable; |
| 868 | drv.l2_freq_tbl = params->l2_freq_tbl; |
| 869 | drv.dev = dev; |
| 870 | |
Matt Wagantall | 754ee27 | 2012-06-18 13:40:26 -0700 | [diff] [blame] | 871 | select_freq_plan(params->pvs_acpu_freq_tbl, params->qfprom_phys_base); |
| 872 | bus_init(params->bus_scale_data); |
Matt Wagantall | e9b715a | 2012-01-04 18:16:14 -0800 | [diff] [blame] | 873 | |
Matt Wagantall | 754ee27 | 2012-06-18 13:40:26 -0700 | [diff] [blame] | 874 | l2 = &drv.scalable[L2]; |
| 875 | l2->hfpll_base = ioremap(l2->hfpll_phys_base, SZ_32); |
| 876 | BUG_ON(!l2->hfpll_base); |
Matt Wagantall | 754ee27 | 2012-06-18 13:40:26 -0700 | [diff] [blame] | 877 | |
Matt Wagantall | 302d9a3 | 2012-07-03 13:37:29 -0700 | [diff] [blame] | 878 | rc = rpm_regulator_init(l2, VREG_HFPLL_A, |
| 879 | l2->vreg[VREG_HFPLL_A].max_vdd, false); |
| 880 | BUG_ON(rc); |
| 881 | rc = rpm_regulator_init(l2, VREG_HFPLL_B, |
| 882 | l2->vreg[VREG_HFPLL_B].max_vdd, false); |
| 883 | BUG_ON(rc); |
Matt Wagantall | 600ea50 | 2012-06-08 18:49:53 -0700 | [diff] [blame^] | 884 | rc = init_clock_sources(l2, |
| 885 | &drv.l2_freq_tbl[drv.max_acpu_lvl->l2_level].speed); |
Matt Wagantall | 302d9a3 | 2012-07-03 13:37:29 -0700 | [diff] [blame] | 886 | BUG_ON(rc); |
| 887 | |
| 888 | for_each_online_cpu(cpu) { |
| 889 | rc = per_cpu_init(cpu); |
| 890 | BUG_ON(rc); |
| 891 | } |
Matt Wagantall | e9b715a | 2012-01-04 18:16:14 -0800 | [diff] [blame] | 892 | |
| 893 | cpufreq_table_init(); |
| 894 | |
| 895 | acpuclk_register(&acpuclk_krait_data); |
| 896 | register_hotcpu_notifier(&acpuclk_cpu_notifier); |
| 897 | |
| 898 | return 0; |
| 899 | } |