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