Matt Wagantall | e9b715a | 2012-01-04 18:16:14 -0800 | [diff] [blame] | 1 | /* |
Vikram Mulukutla | 81796c0 | 2012-10-19 07:42:45 -0700 | [diff] [blame] | 2 | * Copyright (c) 2011-2012, The Linux Foundation. All rights reserved. |
Matt Wagantall | e9b715a | 2012-01-04 18:16:14 -0800 | [diff] [blame] | 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 | |
Matt Wagantall | e9b715a | 2012-01-04 18:16:14 -0800 | [diff] [blame] | 14 | #include <linux/kernel.h> |
Matt Wagantall | 9515bc2 | 2012-07-19 18:13:40 -0700 | [diff] [blame] | 15 | #include <linux/module.h> |
Matt Wagantall | e9b715a | 2012-01-04 18:16:14 -0800 | [diff] [blame] | 16 | #include <linux/init.h> |
| 17 | #include <linux/io.h> |
| 18 | #include <linux/delay.h> |
| 19 | #include <linux/mutex.h> |
| 20 | #include <linux/err.h> |
| 21 | #include <linux/errno.h> |
| 22 | #include <linux/cpufreq.h> |
| 23 | #include <linux/cpu.h> |
| 24 | #include <linux/regulator/consumer.h> |
| 25 | |
| 26 | #include <asm/mach-types.h> |
| 27 | #include <asm/cpu.h> |
| 28 | |
| 29 | #include <mach/board.h> |
| 30 | #include <mach/msm_iomap.h> |
| 31 | #include <mach/socinfo.h> |
| 32 | #include <mach/msm-krait-l2-accessors.h> |
| 33 | #include <mach/rpm-regulator.h> |
Matt Wagantall | 75473eb | 2012-05-31 15:23:22 -0700 | [diff] [blame] | 34 | #include <mach/rpm-regulator-smd.h> |
Matt Wagantall | e9b715a | 2012-01-04 18:16:14 -0800 | [diff] [blame] | 35 | #include <mach/msm_bus.h> |
Steve Muckle | a9aac29 | 2012-11-02 15:41:00 -0700 | [diff] [blame] | 36 | #include <mach/msm_dcvs.h> |
Matt Wagantall | e9b715a | 2012-01-04 18:16:14 -0800 | [diff] [blame] | 37 | |
| 38 | #include "acpuclock.h" |
| 39 | #include "acpuclock-krait.h" |
Stephen Boyd | c13b679 | 2012-09-14 11:25:34 -0700 | [diff] [blame] | 40 | #include "avs.h" |
Matt Wagantall | e9b715a | 2012-01-04 18:16:14 -0800 | [diff] [blame] | 41 | |
| 42 | /* MUX source selects. */ |
| 43 | #define PRI_SRC_SEL_SEC_SRC 0 |
| 44 | #define PRI_SRC_SEL_HFPLL 1 |
| 45 | #define PRI_SRC_SEL_HFPLL_DIV2 2 |
Matt Wagantall | e9b715a | 2012-01-04 18:16:14 -0800 | [diff] [blame] | 46 | |
Matt Wagantall | 7c705e7 | 2012-09-25 12:47:24 -0700 | [diff] [blame] | 47 | #define SECCLKAGD BIT(4) |
| 48 | |
Matt Wagantall | e9b715a | 2012-01-04 18:16:14 -0800 | [diff] [blame] | 49 | static DEFINE_MUTEX(driver_lock); |
| 50 | static DEFINE_SPINLOCK(l2_lock); |
| 51 | |
| 52 | static struct drv_data { |
Matt Wagantall | 06e4a1f | 2012-06-07 18:38:13 -0700 | [diff] [blame] | 53 | struct acpu_level *acpu_freq_tbl; |
Matt Wagantall | e9b715a | 2012-01-04 18:16:14 -0800 | [diff] [blame] | 54 | const struct l2_level *l2_freq_tbl; |
| 55 | struct scalable *scalable; |
Matt Wagantall | 1f3762d | 2012-06-08 19:08:48 -0700 | [diff] [blame] | 56 | struct hfpll_data *hfpll_data; |
Matt Wagantall | e9b715a | 2012-01-04 18:16:14 -0800 | [diff] [blame] | 57 | u32 bus_perf_client; |
Matt Wagantall | 1f3762d | 2012-06-08 19:08:48 -0700 | [diff] [blame] | 58 | struct msm_bus_scale_pdata *bus_scale; |
Matt Wagantall | 9515bc2 | 2012-07-19 18:13:40 -0700 | [diff] [blame] | 59 | int boost_uv; |
Matt Wagantall | e9b715a | 2012-01-04 18:16:14 -0800 | [diff] [blame] | 60 | struct device *dev; |
| 61 | } drv; |
| 62 | |
| 63 | static unsigned long acpuclk_krait_get_rate(int cpu) |
| 64 | { |
| 65 | return drv.scalable[cpu].cur_speed->khz; |
| 66 | } |
| 67 | |
| 68 | /* Select a source on the primary MUX. */ |
| 69 | static void set_pri_clk_src(struct scalable *sc, u32 pri_src_sel) |
| 70 | { |
| 71 | u32 regval; |
| 72 | |
| 73 | regval = get_l2_indirect_reg(sc->l2cpmr_iaddr); |
| 74 | regval &= ~0x3; |
| 75 | regval |= (pri_src_sel & 0x3); |
| 76 | set_l2_indirect_reg(sc->l2cpmr_iaddr, regval); |
| 77 | /* Wait for switch to complete. */ |
| 78 | mb(); |
| 79 | udelay(1); |
| 80 | } |
| 81 | |
| 82 | /* Select a source on the secondary MUX. */ |
Matt Wagantall | a133dbf | 2012-09-27 19:56:57 -0700 | [diff] [blame] | 83 | static void __cpuinit set_sec_clk_src(struct scalable *sc, u32 sec_src_sel) |
Matt Wagantall | e9b715a | 2012-01-04 18:16:14 -0800 | [diff] [blame] | 84 | { |
| 85 | u32 regval; |
| 86 | |
Matt Wagantall | 7c705e7 | 2012-09-25 12:47:24 -0700 | [diff] [blame] | 87 | /* 8064 Errata: disable sec_src clock gating during switch. */ |
Matt Wagantall | e9b715a | 2012-01-04 18:16:14 -0800 | [diff] [blame] | 88 | regval = get_l2_indirect_reg(sc->l2cpmr_iaddr); |
Matt Wagantall | 7c705e7 | 2012-09-25 12:47:24 -0700 | [diff] [blame] | 89 | regval |= SECCLKAGD; |
| 90 | set_l2_indirect_reg(sc->l2cpmr_iaddr, regval); |
| 91 | |
| 92 | /* Program the MUX */ |
Matt Wagantall | e9b715a | 2012-01-04 18:16:14 -0800 | [diff] [blame] | 93 | regval &= ~(0x3 << 2); |
| 94 | regval |= ((sec_src_sel & 0x3) << 2); |
| 95 | set_l2_indirect_reg(sc->l2cpmr_iaddr, regval); |
Matt Wagantall | 7c705e7 | 2012-09-25 12:47:24 -0700 | [diff] [blame] | 96 | |
| 97 | /* 8064 Errata: re-enabled sec_src clock gating. */ |
| 98 | regval &= ~SECCLKAGD; |
| 99 | set_l2_indirect_reg(sc->l2cpmr_iaddr, regval); |
| 100 | |
Matt Wagantall | e9b715a | 2012-01-04 18:16:14 -0800 | [diff] [blame] | 101 | /* Wait for switch to complete. */ |
| 102 | mb(); |
| 103 | udelay(1); |
| 104 | } |
| 105 | |
Matt Wagantall | 302d9a3 | 2012-07-03 13:37:29 -0700 | [diff] [blame] | 106 | static int enable_rpm_vreg(struct vreg *vreg) |
Matt Wagantall | e9b715a | 2012-01-04 18:16:14 -0800 | [diff] [blame] | 107 | { |
Matt Wagantall | 302d9a3 | 2012-07-03 13:37:29 -0700 | [diff] [blame] | 108 | int ret = 0; |
Matt Wagantall | e9b715a | 2012-01-04 18:16:14 -0800 | [diff] [blame] | 109 | |
Matt Wagantall | 75473eb | 2012-05-31 15:23:22 -0700 | [diff] [blame] | 110 | if (vreg->rpm_reg) { |
Matt Wagantall | 302d9a3 | 2012-07-03 13:37:29 -0700 | [diff] [blame] | 111 | ret = rpm_regulator_enable(vreg->rpm_reg); |
| 112 | if (ret) |
Matt Wagantall | 75473eb | 2012-05-31 15:23:22 -0700 | [diff] [blame] | 113 | dev_err(drv.dev, "%s regulator enable failed (%d)\n", |
Matt Wagantall | 302d9a3 | 2012-07-03 13:37:29 -0700 | [diff] [blame] | 114 | vreg->name, ret); |
Matt Wagantall | 75473eb | 2012-05-31 15:23:22 -0700 | [diff] [blame] | 115 | } |
Matt Wagantall | 302d9a3 | 2012-07-03 13:37:29 -0700 | [diff] [blame] | 116 | |
| 117 | return ret; |
Matt Wagantall | 75473eb | 2012-05-31 15:23:22 -0700 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | static void disable_rpm_vreg(struct vreg *vreg) |
| 121 | { |
| 122 | int rc; |
| 123 | |
| 124 | if (vreg->rpm_reg) { |
| 125 | rc = rpm_regulator_disable(vreg->rpm_reg); |
| 126 | if (rc) |
| 127 | dev_err(drv.dev, "%s regulator disable failed (%d)\n", |
| 128 | vreg->name, rc); |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | /* Enable an already-configured HFPLL. */ |
| 133 | static void hfpll_enable(struct scalable *sc, bool skip_regulators) |
| 134 | { |
Matt Wagantall | e9b715a | 2012-01-04 18:16:14 -0800 | [diff] [blame] | 135 | if (!skip_regulators) { |
| 136 | /* Enable regulators required by the HFPLL. */ |
Matt Wagantall | 75473eb | 2012-05-31 15:23:22 -0700 | [diff] [blame] | 137 | enable_rpm_vreg(&sc->vreg[VREG_HFPLL_A]); |
| 138 | enable_rpm_vreg(&sc->vreg[VREG_HFPLL_B]); |
Matt Wagantall | e9b715a | 2012-01-04 18:16:14 -0800 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | /* Disable PLL bypass mode. */ |
Matt Wagantall | 1f3762d | 2012-06-08 19:08:48 -0700 | [diff] [blame] | 142 | writel_relaxed(0x2, sc->hfpll_base + drv.hfpll_data->mode_offset); |
Matt Wagantall | e9b715a | 2012-01-04 18:16:14 -0800 | [diff] [blame] | 143 | |
| 144 | /* |
| 145 | * H/W requires a 5us delay between disabling the bypass and |
| 146 | * de-asserting the reset. Delay 10us just to be safe. |
| 147 | */ |
| 148 | mb(); |
| 149 | udelay(10); |
| 150 | |
| 151 | /* De-assert active-low PLL reset. */ |
Matt Wagantall | 1f3762d | 2012-06-08 19:08:48 -0700 | [diff] [blame] | 152 | writel_relaxed(0x6, sc->hfpll_base + drv.hfpll_data->mode_offset); |
Matt Wagantall | e9b715a | 2012-01-04 18:16:14 -0800 | [diff] [blame] | 153 | |
| 154 | /* Wait for PLL to lock. */ |
| 155 | mb(); |
| 156 | udelay(60); |
| 157 | |
| 158 | /* Enable PLL output. */ |
Matt Wagantall | 1f3762d | 2012-06-08 19:08:48 -0700 | [diff] [blame] | 159 | writel_relaxed(0x7, sc->hfpll_base + drv.hfpll_data->mode_offset); |
Matt Wagantall | e9b715a | 2012-01-04 18:16:14 -0800 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | /* Disable a HFPLL for power-savings or while it's being reprogrammed. */ |
| 163 | static void hfpll_disable(struct scalable *sc, bool skip_regulators) |
| 164 | { |
Matt Wagantall | e9b715a | 2012-01-04 18:16:14 -0800 | [diff] [blame] | 165 | /* |
| 166 | * Disable the PLL output, disable test mode, enable the bypass mode, |
| 167 | * and assert the reset. |
| 168 | */ |
Matt Wagantall | 1f3762d | 2012-06-08 19:08:48 -0700 | [diff] [blame] | 169 | writel_relaxed(0, sc->hfpll_base + drv.hfpll_data->mode_offset); |
Matt Wagantall | e9b715a | 2012-01-04 18:16:14 -0800 | [diff] [blame] | 170 | |
| 171 | if (!skip_regulators) { |
| 172 | /* Remove voltage votes required by the HFPLL. */ |
Matt Wagantall | 75473eb | 2012-05-31 15:23:22 -0700 | [diff] [blame] | 173 | disable_rpm_vreg(&sc->vreg[VREG_HFPLL_B]); |
| 174 | disable_rpm_vreg(&sc->vreg[VREG_HFPLL_A]); |
Matt Wagantall | e9b715a | 2012-01-04 18:16:14 -0800 | [diff] [blame] | 175 | } |
| 176 | } |
| 177 | |
| 178 | /* Program the HFPLL rate. Assumes HFPLL is already disabled. */ |
| 179 | static void hfpll_set_rate(struct scalable *sc, const struct core_speed *tgt_s) |
| 180 | { |
Matt Wagantall | a77b7f3 | 2012-07-18 16:32:01 -0700 | [diff] [blame] | 181 | void __iomem *base = sc->hfpll_base; |
| 182 | u32 regval; |
| 183 | |
| 184 | writel_relaxed(tgt_s->pll_l_val, base + drv.hfpll_data->l_offset); |
| 185 | |
| 186 | if (drv.hfpll_data->has_user_reg) { |
| 187 | regval = readl_relaxed(base + drv.hfpll_data->user_offset); |
| 188 | if (tgt_s->pll_l_val <= drv.hfpll_data->low_vco_l_max) |
| 189 | regval &= ~drv.hfpll_data->user_vco_mask; |
| 190 | else |
| 191 | regval |= drv.hfpll_data->user_vco_mask; |
| 192 | writel_relaxed(regval, base + drv.hfpll_data->user_offset); |
| 193 | } |
Matt Wagantall | e9b715a | 2012-01-04 18:16:14 -0800 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | /* Return the L2 speed that should be applied. */ |
Matt Wagantall | 600ea50 | 2012-06-08 18:49:53 -0700 | [diff] [blame] | 197 | 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] | 198 | { |
Matt Wagantall | 600ea50 | 2012-06-08 18:49:53 -0700 | [diff] [blame] | 199 | unsigned int new_l = 0; |
Matt Wagantall | e9b715a | 2012-01-04 18:16:14 -0800 | [diff] [blame] | 200 | int cpu; |
| 201 | |
| 202 | /* Find max L2 speed vote. */ |
| 203 | sc->l2_vote = vote_l; |
Matt Wagantall | e9b715a | 2012-01-04 18:16:14 -0800 | [diff] [blame] | 204 | for_each_present_cpu(cpu) |
| 205 | new_l = max(new_l, drv.scalable[cpu].l2_vote); |
| 206 | |
| 207 | return new_l; |
| 208 | } |
| 209 | |
| 210 | /* Update the bus bandwidth request. */ |
| 211 | static void set_bus_bw(unsigned int bw) |
| 212 | { |
| 213 | int ret; |
| 214 | |
| 215 | /* Update bandwidth if request has changed. This may sleep. */ |
| 216 | ret = msm_bus_scale_client_update_request(drv.bus_perf_client, bw); |
| 217 | if (ret) |
| 218 | dev_err(drv.dev, "bandwidth request failed (%d)\n", ret); |
| 219 | } |
| 220 | |
| 221 | /* Set the CPU or L2 clock speed. */ |
Vikram Mulukutla | 81796c0 | 2012-10-19 07:42:45 -0700 | [diff] [blame] | 222 | static void set_speed(struct scalable *sc, const struct core_speed *tgt_s, |
| 223 | bool skip_regulators) |
Matt Wagantall | e9b715a | 2012-01-04 18:16:14 -0800 | [diff] [blame] | 224 | { |
| 225 | const struct core_speed *strt_s = sc->cur_speed; |
| 226 | |
Stephen Boyd | 14a4739 | 2012-08-06 20:15:15 -0700 | [diff] [blame] | 227 | if (strt_s == tgt_s) |
| 228 | return; |
| 229 | |
Matt Wagantall | e9b715a | 2012-01-04 18:16:14 -0800 | [diff] [blame] | 230 | if (strt_s->src == HFPLL && tgt_s->src == HFPLL) { |
| 231 | /* |
| 232 | * Move to an always-on source running at a frequency |
| 233 | * that does not require an elevated CPU voltage. |
| 234 | */ |
Matt Wagantall | e9b715a | 2012-01-04 18:16:14 -0800 | [diff] [blame] | 235 | set_pri_clk_src(sc, PRI_SRC_SEL_SEC_SRC); |
| 236 | |
| 237 | /* Re-program HFPLL. */ |
Matt Wagantall | 75473eb | 2012-05-31 15:23:22 -0700 | [diff] [blame] | 238 | hfpll_disable(sc, true); |
Matt Wagantall | e9b715a | 2012-01-04 18:16:14 -0800 | [diff] [blame] | 239 | hfpll_set_rate(sc, tgt_s); |
Matt Wagantall | 75473eb | 2012-05-31 15:23:22 -0700 | [diff] [blame] | 240 | hfpll_enable(sc, true); |
Matt Wagantall | e9b715a | 2012-01-04 18:16:14 -0800 | [diff] [blame] | 241 | |
| 242 | /* Move to HFPLL. */ |
| 243 | set_pri_clk_src(sc, tgt_s->pri_src_sel); |
| 244 | } else if (strt_s->src == HFPLL && tgt_s->src != HFPLL) { |
Matt Wagantall | e9b715a | 2012-01-04 18:16:14 -0800 | [diff] [blame] | 245 | set_pri_clk_src(sc, tgt_s->pri_src_sel); |
Vikram Mulukutla | 81796c0 | 2012-10-19 07:42:45 -0700 | [diff] [blame] | 246 | hfpll_disable(sc, skip_regulators); |
Matt Wagantall | e9b715a | 2012-01-04 18:16:14 -0800 | [diff] [blame] | 247 | } else if (strt_s->src != HFPLL && tgt_s->src == HFPLL) { |
| 248 | hfpll_set_rate(sc, tgt_s); |
Vikram Mulukutla | 81796c0 | 2012-10-19 07:42:45 -0700 | [diff] [blame] | 249 | hfpll_enable(sc, skip_regulators); |
Matt Wagantall | e9b715a | 2012-01-04 18:16:14 -0800 | [diff] [blame] | 250 | set_pri_clk_src(sc, tgt_s->pri_src_sel); |
Matt Wagantall | e9b715a | 2012-01-04 18:16:14 -0800 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | sc->cur_speed = tgt_s; |
| 254 | } |
| 255 | |
Matt Wagantall | 6d9c416 | 2012-07-16 18:58:16 -0700 | [diff] [blame] | 256 | struct vdd_data { |
| 257 | int vdd_mem; |
| 258 | int vdd_dig; |
| 259 | int vdd_core; |
| 260 | int ua_core; |
| 261 | }; |
| 262 | |
Matt Wagantall | e9b715a | 2012-01-04 18:16:14 -0800 | [diff] [blame] | 263 | /* Apply any per-cpu voltage increases. */ |
Matt Wagantall | 6d9c416 | 2012-07-16 18:58:16 -0700 | [diff] [blame] | 264 | static int increase_vdd(int cpu, struct vdd_data *data, |
Matt Wagantall | e9b715a | 2012-01-04 18:16:14 -0800 | [diff] [blame] | 265 | enum setrate_reason reason) |
| 266 | { |
| 267 | struct scalable *sc = &drv.scalable[cpu]; |
Matt Wagantall | 6d9c416 | 2012-07-16 18:58:16 -0700 | [diff] [blame] | 268 | int rc; |
Matt Wagantall | e9b715a | 2012-01-04 18:16:14 -0800 | [diff] [blame] | 269 | |
| 270 | /* |
| 271 | * Increase vdd_mem active-set before vdd_dig. |
| 272 | * vdd_mem should be >= vdd_dig. |
| 273 | */ |
Matt Wagantall | 6d9c416 | 2012-07-16 18:58:16 -0700 | [diff] [blame] | 274 | if (data->vdd_mem > sc->vreg[VREG_MEM].cur_vdd) { |
Matt Wagantall | 75473eb | 2012-05-31 15:23:22 -0700 | [diff] [blame] | 275 | rc = rpm_regulator_set_voltage(sc->vreg[VREG_MEM].rpm_reg, |
Matt Wagantall | 6d9c416 | 2012-07-16 18:58:16 -0700 | [diff] [blame] | 276 | data->vdd_mem, sc->vreg[VREG_MEM].max_vdd); |
Matt Wagantall | e9b715a | 2012-01-04 18:16:14 -0800 | [diff] [blame] | 277 | if (rc) { |
| 278 | dev_err(drv.dev, |
| 279 | "vdd_mem (cpu%d) increase failed (%d)\n", |
| 280 | cpu, rc); |
| 281 | return rc; |
| 282 | } |
Matt Wagantall | 6d9c416 | 2012-07-16 18:58:16 -0700 | [diff] [blame] | 283 | sc->vreg[VREG_MEM].cur_vdd = data->vdd_mem; |
Matt Wagantall | e9b715a | 2012-01-04 18:16:14 -0800 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | /* Increase vdd_dig active-set vote. */ |
Matt Wagantall | 6d9c416 | 2012-07-16 18:58:16 -0700 | [diff] [blame] | 287 | if (data->vdd_dig > sc->vreg[VREG_DIG].cur_vdd) { |
Matt Wagantall | 75473eb | 2012-05-31 15:23:22 -0700 | [diff] [blame] | 288 | rc = rpm_regulator_set_voltage(sc->vreg[VREG_DIG].rpm_reg, |
Matt Wagantall | 6d9c416 | 2012-07-16 18:58:16 -0700 | [diff] [blame] | 289 | data->vdd_dig, sc->vreg[VREG_DIG].max_vdd); |
Matt Wagantall | e9b715a | 2012-01-04 18:16:14 -0800 | [diff] [blame] | 290 | if (rc) { |
| 291 | dev_err(drv.dev, |
| 292 | "vdd_dig (cpu%d) increase failed (%d)\n", |
| 293 | cpu, rc); |
| 294 | return rc; |
| 295 | } |
Matt Wagantall | 6d9c416 | 2012-07-16 18:58:16 -0700 | [diff] [blame] | 296 | sc->vreg[VREG_DIG].cur_vdd = data->vdd_dig; |
| 297 | } |
| 298 | |
| 299 | /* Increase current request. */ |
| 300 | if (data->ua_core > sc->vreg[VREG_CORE].cur_ua) { |
| 301 | rc = regulator_set_optimum_mode(sc->vreg[VREG_CORE].reg, |
| 302 | data->ua_core); |
| 303 | if (rc < 0) { |
| 304 | dev_err(drv.dev, "regulator_set_optimum_mode(%s) failed (%d)\n", |
| 305 | sc->vreg[VREG_CORE].name, rc); |
| 306 | return rc; |
| 307 | } |
| 308 | sc->vreg[VREG_CORE].cur_ua = data->ua_core; |
Matt Wagantall | e9b715a | 2012-01-04 18:16:14 -0800 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | /* |
| 312 | * Update per-CPU core voltage. Don't do this for the hotplug path for |
| 313 | * which it should already be correct. Attempting to set it is bad |
| 314 | * because we don't know what CPU we are running on at this point, but |
| 315 | * the CPU regulator API requires we call it from the affected CPU. |
| 316 | */ |
Matt Wagantall | 6d9c416 | 2012-07-16 18:58:16 -0700 | [diff] [blame] | 317 | if (data->vdd_core > sc->vreg[VREG_CORE].cur_vdd |
Matt Wagantall | e9b715a | 2012-01-04 18:16:14 -0800 | [diff] [blame] | 318 | && reason != SETRATE_HOTPLUG) { |
Matt Wagantall | 6d9c416 | 2012-07-16 18:58:16 -0700 | [diff] [blame] | 319 | rc = regulator_set_voltage(sc->vreg[VREG_CORE].reg, |
| 320 | data->vdd_core, sc->vreg[VREG_CORE].max_vdd); |
Matt Wagantall | e9b715a | 2012-01-04 18:16:14 -0800 | [diff] [blame] | 321 | if (rc) { |
| 322 | dev_err(drv.dev, |
| 323 | "vdd_core (cpu%d) increase failed (%d)\n", |
| 324 | cpu, rc); |
| 325 | return rc; |
| 326 | } |
Matt Wagantall | 6d9c416 | 2012-07-16 18:58:16 -0700 | [diff] [blame] | 327 | sc->vreg[VREG_CORE].cur_vdd = data->vdd_core; |
Matt Wagantall | e9b715a | 2012-01-04 18:16:14 -0800 | [diff] [blame] | 328 | } |
| 329 | |
Matt Wagantall | 6d9c416 | 2012-07-16 18:58:16 -0700 | [diff] [blame] | 330 | return 0; |
Matt Wagantall | e9b715a | 2012-01-04 18:16:14 -0800 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | /* Apply any per-cpu voltage decreases. */ |
Matt Wagantall | 6d9c416 | 2012-07-16 18:58:16 -0700 | [diff] [blame] | 334 | static void decrease_vdd(int cpu, struct vdd_data *data, |
| 335 | enum setrate_reason reason) |
Matt Wagantall | e9b715a | 2012-01-04 18:16:14 -0800 | [diff] [blame] | 336 | { |
| 337 | struct scalable *sc = &drv.scalable[cpu]; |
| 338 | int ret; |
| 339 | |
| 340 | /* |
| 341 | * Update per-CPU core voltage. This must be called on the CPU |
| 342 | * that's being affected. Don't do this in the hotplug remove path, |
| 343 | * where the rail is off and we're executing on the other CPU. |
| 344 | */ |
Matt Wagantall | 6d9c416 | 2012-07-16 18:58:16 -0700 | [diff] [blame] | 345 | if (data->vdd_core < sc->vreg[VREG_CORE].cur_vdd |
Matt Wagantall | e9b715a | 2012-01-04 18:16:14 -0800 | [diff] [blame] | 346 | && reason != SETRATE_HOTPLUG) { |
Matt Wagantall | 6d9c416 | 2012-07-16 18:58:16 -0700 | [diff] [blame] | 347 | ret = regulator_set_voltage(sc->vreg[VREG_CORE].reg, |
| 348 | data->vdd_core, sc->vreg[VREG_CORE].max_vdd); |
Matt Wagantall | e9b715a | 2012-01-04 18:16:14 -0800 | [diff] [blame] | 349 | if (ret) { |
| 350 | dev_err(drv.dev, |
| 351 | "vdd_core (cpu%d) decrease failed (%d)\n", |
| 352 | cpu, ret); |
| 353 | return; |
| 354 | } |
Matt Wagantall | 6d9c416 | 2012-07-16 18:58:16 -0700 | [diff] [blame] | 355 | sc->vreg[VREG_CORE].cur_vdd = data->vdd_core; |
| 356 | } |
| 357 | |
| 358 | /* Decrease current request. */ |
| 359 | if (data->ua_core < sc->vreg[VREG_CORE].cur_ua) { |
| 360 | ret = regulator_set_optimum_mode(sc->vreg[VREG_CORE].reg, |
| 361 | data->ua_core); |
| 362 | if (ret < 0) { |
| 363 | dev_err(drv.dev, "regulator_set_optimum_mode(%s) failed (%d)\n", |
| 364 | sc->vreg[VREG_CORE].name, ret); |
| 365 | return; |
| 366 | } |
| 367 | sc->vreg[VREG_CORE].cur_ua = data->ua_core; |
Matt Wagantall | e9b715a | 2012-01-04 18:16:14 -0800 | [diff] [blame] | 368 | } |
| 369 | |
| 370 | /* Decrease vdd_dig active-set vote. */ |
Matt Wagantall | 6d9c416 | 2012-07-16 18:58:16 -0700 | [diff] [blame] | 371 | if (data->vdd_dig < sc->vreg[VREG_DIG].cur_vdd) { |
Matt Wagantall | 75473eb | 2012-05-31 15:23:22 -0700 | [diff] [blame] | 372 | ret = rpm_regulator_set_voltage(sc->vreg[VREG_DIG].rpm_reg, |
Matt Wagantall | 6d9c416 | 2012-07-16 18:58:16 -0700 | [diff] [blame] | 373 | data->vdd_dig, sc->vreg[VREG_DIG].max_vdd); |
Matt Wagantall | e9b715a | 2012-01-04 18:16:14 -0800 | [diff] [blame] | 374 | if (ret) { |
| 375 | dev_err(drv.dev, |
| 376 | "vdd_dig (cpu%d) decrease failed (%d)\n", |
| 377 | cpu, ret); |
| 378 | return; |
| 379 | } |
Matt Wagantall | 6d9c416 | 2012-07-16 18:58:16 -0700 | [diff] [blame] | 380 | sc->vreg[VREG_DIG].cur_vdd = data->vdd_dig; |
Matt Wagantall | e9b715a | 2012-01-04 18:16:14 -0800 | [diff] [blame] | 381 | } |
| 382 | |
| 383 | /* |
| 384 | * Decrease vdd_mem active-set after vdd_dig. |
| 385 | * vdd_mem should be >= vdd_dig. |
| 386 | */ |
Matt Wagantall | 6d9c416 | 2012-07-16 18:58:16 -0700 | [diff] [blame] | 387 | if (data->vdd_mem < sc->vreg[VREG_MEM].cur_vdd) { |
Matt Wagantall | 75473eb | 2012-05-31 15:23:22 -0700 | [diff] [blame] | 388 | ret = rpm_regulator_set_voltage(sc->vreg[VREG_MEM].rpm_reg, |
Matt Wagantall | 6d9c416 | 2012-07-16 18:58:16 -0700 | [diff] [blame] | 389 | data->vdd_mem, sc->vreg[VREG_MEM].max_vdd); |
Matt Wagantall | e9b715a | 2012-01-04 18:16:14 -0800 | [diff] [blame] | 390 | if (ret) { |
| 391 | dev_err(drv.dev, |
| 392 | "vdd_mem (cpu%d) decrease failed (%d)\n", |
| 393 | cpu, ret); |
| 394 | return; |
| 395 | } |
Matt Wagantall | 6d9c416 | 2012-07-16 18:58:16 -0700 | [diff] [blame] | 396 | sc->vreg[VREG_MEM].cur_vdd = data->vdd_mem; |
Matt Wagantall | e9b715a | 2012-01-04 18:16:14 -0800 | [diff] [blame] | 397 | } |
| 398 | } |
| 399 | |
| 400 | static int calculate_vdd_mem(const struct acpu_level *tgt) |
| 401 | { |
Matt Wagantall | 600ea50 | 2012-06-08 18:49:53 -0700 | [diff] [blame] | 402 | return drv.l2_freq_tbl[tgt->l2_level].vdd_mem; |
Matt Wagantall | e9b715a | 2012-01-04 18:16:14 -0800 | [diff] [blame] | 403 | } |
| 404 | |
Matt Wagantall | 72a3800 | 2012-07-18 13:42:55 -0700 | [diff] [blame] | 405 | static int get_src_dig(const struct core_speed *s) |
Matt Wagantall | e9b715a | 2012-01-04 18:16:14 -0800 | [diff] [blame] | 406 | { |
Matt Wagantall | 1f3762d | 2012-06-08 19:08:48 -0700 | [diff] [blame] | 407 | const int *hfpll_vdd = drv.hfpll_data->vdd; |
| 408 | const u32 low_vdd_l_max = drv.hfpll_data->low_vdd_l_max; |
Matt Wagantall | 87465f5 | 2012-07-23 22:03:06 -0700 | [diff] [blame] | 409 | const u32 nom_vdd_l_max = drv.hfpll_data->nom_vdd_l_max; |
Matt Wagantall | e9b715a | 2012-01-04 18:16:14 -0800 | [diff] [blame] | 410 | |
Matt Wagantall | 72a3800 | 2012-07-18 13:42:55 -0700 | [diff] [blame] | 411 | if (s->src != HFPLL) |
| 412 | return hfpll_vdd[HFPLL_VDD_NONE]; |
Matt Wagantall | 87465f5 | 2012-07-23 22:03:06 -0700 | [diff] [blame] | 413 | else if (s->pll_l_val > nom_vdd_l_max) |
| 414 | return hfpll_vdd[HFPLL_VDD_HIGH]; |
Matt Wagantall | 72a3800 | 2012-07-18 13:42:55 -0700 | [diff] [blame] | 415 | else if (s->pll_l_val > low_vdd_l_max) |
| 416 | return hfpll_vdd[HFPLL_VDD_NOM]; |
Matt Wagantall | e9b715a | 2012-01-04 18:16:14 -0800 | [diff] [blame] | 417 | else |
Matt Wagantall | 72a3800 | 2012-07-18 13:42:55 -0700 | [diff] [blame] | 418 | return hfpll_vdd[HFPLL_VDD_LOW]; |
| 419 | } |
Matt Wagantall | e9b715a | 2012-01-04 18:16:14 -0800 | [diff] [blame] | 420 | |
Matt Wagantall | 72a3800 | 2012-07-18 13:42:55 -0700 | [diff] [blame] | 421 | static int calculate_vdd_dig(const struct acpu_level *tgt) |
| 422 | { |
| 423 | int l2_pll_vdd_dig, cpu_pll_vdd_dig; |
| 424 | |
| 425 | l2_pll_vdd_dig = get_src_dig(&drv.l2_freq_tbl[tgt->l2_level].speed); |
| 426 | cpu_pll_vdd_dig = get_src_dig(&tgt->speed); |
| 427 | |
| 428 | return max(drv.l2_freq_tbl[tgt->l2_level].vdd_dig, |
| 429 | max(l2_pll_vdd_dig, cpu_pll_vdd_dig)); |
Matt Wagantall | e9b715a | 2012-01-04 18:16:14 -0800 | [diff] [blame] | 430 | } |
| 431 | |
Matt Wagantall | 9515bc2 | 2012-07-19 18:13:40 -0700 | [diff] [blame] | 432 | static bool enable_boost = true; |
| 433 | module_param_named(boost, enable_boost, bool, S_IRUGO | S_IWUSR); |
| 434 | |
Matt Wagantall | e9b715a | 2012-01-04 18:16:14 -0800 | [diff] [blame] | 435 | static int calculate_vdd_core(const struct acpu_level *tgt) |
| 436 | { |
Matt Wagantall | 9515bc2 | 2012-07-19 18:13:40 -0700 | [diff] [blame] | 437 | return tgt->vdd_core + (enable_boost ? drv.boost_uv : 0); |
Matt Wagantall | e9b715a | 2012-01-04 18:16:14 -0800 | [diff] [blame] | 438 | } |
| 439 | |
Vikram Mulukutla | 5a623db | 2012-10-19 08:53:19 -0700 | [diff] [blame] | 440 | static DEFINE_MUTEX(l2_regulator_lock); |
| 441 | static int l2_vreg_count; |
| 442 | |
| 443 | static int enable_l2_regulators(void) |
| 444 | { |
| 445 | int ret = 0; |
| 446 | |
| 447 | mutex_lock(&l2_regulator_lock); |
| 448 | if (l2_vreg_count == 0) { |
| 449 | ret = enable_rpm_vreg(&drv.scalable[L2].vreg[VREG_HFPLL_A]); |
| 450 | if (ret) |
| 451 | goto out; |
| 452 | ret = enable_rpm_vreg(&drv.scalable[L2].vreg[VREG_HFPLL_B]); |
| 453 | if (ret) { |
| 454 | disable_rpm_vreg(&drv.scalable[L2].vreg[VREG_HFPLL_A]); |
| 455 | goto out; |
| 456 | } |
| 457 | } |
| 458 | l2_vreg_count++; |
| 459 | out: |
| 460 | mutex_unlock(&l2_regulator_lock); |
| 461 | |
| 462 | return ret; |
| 463 | } |
| 464 | |
| 465 | static void disable_l2_regulators(void) |
| 466 | { |
| 467 | mutex_lock(&l2_regulator_lock); |
| 468 | |
| 469 | if (WARN(!l2_vreg_count, "L2 regulator votes are unbalanced!")) |
| 470 | goto out; |
| 471 | |
| 472 | if (l2_vreg_count == 1) { |
| 473 | disable_rpm_vreg(&drv.scalable[L2].vreg[VREG_HFPLL_B]); |
| 474 | disable_rpm_vreg(&drv.scalable[L2].vreg[VREG_HFPLL_A]); |
| 475 | } |
| 476 | l2_vreg_count--; |
| 477 | out: |
| 478 | mutex_unlock(&l2_regulator_lock); |
| 479 | } |
| 480 | |
Matt Wagantall | e9b715a | 2012-01-04 18:16:14 -0800 | [diff] [blame] | 481 | /* Set the CPU's clock rate and adjust the L2 rate, voltage and BW requests. */ |
| 482 | static int acpuclk_krait_set_rate(int cpu, unsigned long rate, |
| 483 | enum setrate_reason reason) |
| 484 | { |
| 485 | const struct core_speed *strt_acpu_s, *tgt_acpu_s; |
Matt Wagantall | e9b715a | 2012-01-04 18:16:14 -0800 | [diff] [blame] | 486 | const struct acpu_level *tgt; |
Matt Wagantall | 600ea50 | 2012-06-08 18:49:53 -0700 | [diff] [blame] | 487 | int tgt_l2_l; |
Vikram Mulukutla | 5a623db | 2012-10-19 08:53:19 -0700 | [diff] [blame] | 488 | enum src_id prev_l2_src = NUM_SRC_ID; |
Matt Wagantall | 6d9c416 | 2012-07-16 18:58:16 -0700 | [diff] [blame] | 489 | struct vdd_data vdd_data; |
Vikram Mulukutla | 81796c0 | 2012-10-19 07:42:45 -0700 | [diff] [blame] | 490 | bool skip_regulators; |
Matt Wagantall | e9b715a | 2012-01-04 18:16:14 -0800 | [diff] [blame] | 491 | int rc = 0; |
| 492 | |
Matt Wagantall | 5941a33 | 2012-07-10 23:20:44 -0700 | [diff] [blame] | 493 | if (cpu > num_possible_cpus()) |
| 494 | return -EINVAL; |
Matt Wagantall | e9b715a | 2012-01-04 18:16:14 -0800 | [diff] [blame] | 495 | |
| 496 | if (reason == SETRATE_CPUFREQ || reason == SETRATE_HOTPLUG) |
| 497 | mutex_lock(&driver_lock); |
| 498 | |
| 499 | strt_acpu_s = drv.scalable[cpu].cur_speed; |
| 500 | |
| 501 | /* Return early if rate didn't change. */ |
| 502 | if (rate == strt_acpu_s->khz) |
| 503 | goto out; |
| 504 | |
| 505 | /* Find target frequency. */ |
| 506 | for (tgt = drv.acpu_freq_tbl; tgt->speed.khz != 0; tgt++) { |
| 507 | if (tgt->speed.khz == rate) { |
| 508 | tgt_acpu_s = &tgt->speed; |
| 509 | break; |
| 510 | } |
| 511 | } |
| 512 | if (tgt->speed.khz == 0) { |
| 513 | rc = -EINVAL; |
| 514 | goto out; |
| 515 | } |
| 516 | |
| 517 | /* Calculate voltage requirements for the current CPU. */ |
Matt Wagantall | 6d9c416 | 2012-07-16 18:58:16 -0700 | [diff] [blame] | 518 | vdd_data.vdd_mem = calculate_vdd_mem(tgt); |
| 519 | vdd_data.vdd_dig = calculate_vdd_dig(tgt); |
| 520 | vdd_data.vdd_core = calculate_vdd_core(tgt); |
| 521 | vdd_data.ua_core = tgt->ua_core; |
Matt Wagantall | e9b715a | 2012-01-04 18:16:14 -0800 | [diff] [blame] | 522 | |
Stephen Boyd | c13b679 | 2012-09-14 11:25:34 -0700 | [diff] [blame] | 523 | /* Disable AVS before voltage switch */ |
| 524 | if (reason == SETRATE_CPUFREQ && drv.scalable[cpu].avs_enabled) { |
| 525 | AVS_DISABLE(cpu); |
| 526 | drv.scalable[cpu].avs_enabled = false; |
| 527 | } |
| 528 | |
Matt Wagantall | e9b715a | 2012-01-04 18:16:14 -0800 | [diff] [blame] | 529 | /* Increase VDD levels if needed. */ |
| 530 | if (reason == SETRATE_CPUFREQ || reason == SETRATE_HOTPLUG) { |
Matt Wagantall | 6d9c416 | 2012-07-16 18:58:16 -0700 | [diff] [blame] | 531 | rc = increase_vdd(cpu, &vdd_data, reason); |
Matt Wagantall | e9b715a | 2012-01-04 18:16:14 -0800 | [diff] [blame] | 532 | if (rc) |
| 533 | goto out; |
Vikram Mulukutla | 5a623db | 2012-10-19 08:53:19 -0700 | [diff] [blame] | 534 | |
| 535 | prev_l2_src = |
| 536 | drv.l2_freq_tbl[drv.scalable[cpu].l2_vote].speed.src; |
| 537 | /* Vote for the L2 regulators here if necessary. */ |
| 538 | if (drv.l2_freq_tbl[tgt->l2_level].speed.src == HFPLL) { |
| 539 | rc = enable_l2_regulators(); |
| 540 | if (rc) |
| 541 | goto out; |
| 542 | } |
Matt Wagantall | e9b715a | 2012-01-04 18:16:14 -0800 | [diff] [blame] | 543 | } |
| 544 | |
Matt Wagantall | bd1b404 | 2012-07-24 11:20:03 -0700 | [diff] [blame] | 545 | dev_dbg(drv.dev, "Switching from ACPU%d rate %lu KHz -> %lu KHz\n", |
| 546 | cpu, strt_acpu_s->khz, tgt_acpu_s->khz); |
Matt Wagantall | e9b715a | 2012-01-04 18:16:14 -0800 | [diff] [blame] | 547 | |
Vikram Mulukutla | 81796c0 | 2012-10-19 07:42:45 -0700 | [diff] [blame] | 548 | /* |
| 549 | * If we are setting the rate as part of power collapse or in the resume |
| 550 | * path after power collapse, skip the vote for the HFPLL regulators, |
| 551 | * which are active-set-only votes that will be removed when apps enters |
| 552 | * its sleep set. This is needed to avoid voting for regulators with |
| 553 | * sleeping APIs from an atomic context. |
| 554 | */ |
| 555 | skip_regulators = (reason == SETRATE_PC); |
| 556 | |
Matt Wagantall | e9b715a | 2012-01-04 18:16:14 -0800 | [diff] [blame] | 557 | /* Set the new CPU speed. */ |
Vikram Mulukutla | 81796c0 | 2012-10-19 07:42:45 -0700 | [diff] [blame] | 558 | set_speed(&drv.scalable[cpu], tgt_acpu_s, skip_regulators); |
Matt Wagantall | e9b715a | 2012-01-04 18:16:14 -0800 | [diff] [blame] | 559 | |
| 560 | /* |
| 561 | * Update the L2 vote and apply the rate change. A spinlock is |
| 562 | * necessary to ensure L2 rate is calculated and set atomically |
| 563 | * with the CPU frequency, even if acpuclk_krait_set_rate() is |
| 564 | * called from an atomic context and the driver_lock mutex is not |
| 565 | * acquired. |
| 566 | */ |
Vikram Mulukutla | 5a623db | 2012-10-19 08:53:19 -0700 | [diff] [blame] | 567 | spin_lock(&l2_lock); |
Matt Wagantall | e9b715a | 2012-01-04 18:16:14 -0800 | [diff] [blame] | 568 | tgt_l2_l = compute_l2_level(&drv.scalable[cpu], tgt->l2_level); |
Vikram Mulukutla | 5a623db | 2012-10-19 08:53:19 -0700 | [diff] [blame] | 569 | set_speed(&drv.scalable[L2], |
| 570 | &drv.l2_freq_tbl[tgt_l2_l].speed, true); |
| 571 | spin_unlock(&l2_lock); |
Matt Wagantall | e9b715a | 2012-01-04 18:16:14 -0800 | [diff] [blame] | 572 | |
| 573 | /* Nothing else to do for power collapse or SWFI. */ |
| 574 | if (reason == SETRATE_PC || reason == SETRATE_SWFI) |
| 575 | goto out; |
| 576 | |
Vikram Mulukutla | 5a623db | 2012-10-19 08:53:19 -0700 | [diff] [blame] | 577 | /* |
| 578 | * Remove the vote for the L2 HFPLL regulators only if the L2 |
| 579 | * was already on an HFPLL source. |
| 580 | */ |
| 581 | if (prev_l2_src == HFPLL) |
| 582 | disable_l2_regulators(); |
| 583 | |
Matt Wagantall | e9b715a | 2012-01-04 18:16:14 -0800 | [diff] [blame] | 584 | /* Update bus bandwith request. */ |
Matt Wagantall | 600ea50 | 2012-06-08 18:49:53 -0700 | [diff] [blame] | 585 | set_bus_bw(drv.l2_freq_tbl[tgt_l2_l].bw_level); |
Matt Wagantall | e9b715a | 2012-01-04 18:16:14 -0800 | [diff] [blame] | 586 | |
| 587 | /* Drop VDD levels if we can. */ |
Matt Wagantall | 6d9c416 | 2012-07-16 18:58:16 -0700 | [diff] [blame] | 588 | decrease_vdd(cpu, &vdd_data, reason); |
Matt Wagantall | e9b715a | 2012-01-04 18:16:14 -0800 | [diff] [blame] | 589 | |
Stephen Boyd | c13b679 | 2012-09-14 11:25:34 -0700 | [diff] [blame] | 590 | /* Re-enable AVS */ |
| 591 | if (reason == SETRATE_CPUFREQ && tgt->avsdscr_setting) { |
| 592 | AVS_ENABLE(cpu, tgt->avsdscr_setting); |
| 593 | drv.scalable[cpu].avs_enabled = true; |
| 594 | } |
| 595 | |
Matt Wagantall | bd1b404 | 2012-07-24 11:20:03 -0700 | [diff] [blame] | 596 | dev_dbg(drv.dev, "ACPU%d speed change complete\n", cpu); |
Matt Wagantall | e9b715a | 2012-01-04 18:16:14 -0800 | [diff] [blame] | 597 | |
| 598 | out: |
| 599 | if (reason == SETRATE_CPUFREQ || reason == SETRATE_HOTPLUG) |
| 600 | mutex_unlock(&driver_lock); |
| 601 | return rc; |
| 602 | } |
| 603 | |
Matt Wagantall | b7c231b | 2012-07-24 18:40:17 -0700 | [diff] [blame] | 604 | static struct acpuclk_data acpuclk_krait_data = { |
| 605 | .set_rate = acpuclk_krait_set_rate, |
| 606 | .get_rate = acpuclk_krait_get_rate, |
| 607 | }; |
| 608 | |
Matt Wagantall | e9b715a | 2012-01-04 18:16:14 -0800 | [diff] [blame] | 609 | /* Initialize a HFPLL at a given rate and enable it. */ |
Iliyan Malchev | 16aea52 | 2012-10-16 00:35:07 -0700 | [diff] [blame] | 610 | static void __cpuinit hfpll_init(struct scalable *sc, |
Matt Wagantall | e9b715a | 2012-01-04 18:16:14 -0800 | [diff] [blame] | 611 | const struct core_speed *tgt_s) |
| 612 | { |
Matt Wagantall | bd1b404 | 2012-07-24 11:20:03 -0700 | [diff] [blame] | 613 | dev_dbg(drv.dev, "Initializing HFPLL%d\n", sc - drv.scalable); |
Matt Wagantall | e9b715a | 2012-01-04 18:16:14 -0800 | [diff] [blame] | 614 | |
| 615 | /* Disable the PLL for re-programming. */ |
Matt Wagantall | 75473eb | 2012-05-31 15:23:22 -0700 | [diff] [blame] | 616 | hfpll_disable(sc, true); |
Matt Wagantall | e9b715a | 2012-01-04 18:16:14 -0800 | [diff] [blame] | 617 | |
| 618 | /* Configure PLL parameters for integer mode. */ |
Matt Wagantall | 1f3762d | 2012-06-08 19:08:48 -0700 | [diff] [blame] | 619 | writel_relaxed(drv.hfpll_data->config_val, |
| 620 | sc->hfpll_base + drv.hfpll_data->config_offset); |
| 621 | writel_relaxed(0, sc->hfpll_base + drv.hfpll_data->m_offset); |
| 622 | writel_relaxed(1, sc->hfpll_base + drv.hfpll_data->n_offset); |
Matt Wagantall | a77b7f3 | 2012-07-18 16:32:01 -0700 | [diff] [blame] | 623 | if (drv.hfpll_data->has_user_reg) |
| 624 | writel_relaxed(drv.hfpll_data->user_val, |
| 625 | sc->hfpll_base + drv.hfpll_data->user_offset); |
Matt Wagantall | e9b715a | 2012-01-04 18:16:14 -0800 | [diff] [blame] | 626 | |
Matt Wagantall | 06e4a1f | 2012-06-07 18:38:13 -0700 | [diff] [blame] | 627 | /* Program droop controller, if supported */ |
Matt Wagantall | 1f3762d | 2012-06-08 19:08:48 -0700 | [diff] [blame] | 628 | if (drv.hfpll_data->has_droop_ctl) |
| 629 | writel_relaxed(drv.hfpll_data->droop_val, |
| 630 | sc->hfpll_base + drv.hfpll_data->droop_offset); |
Matt Wagantall | 06e4a1f | 2012-06-07 18:38:13 -0700 | [diff] [blame] | 631 | |
Matt Wagantall | e9b715a | 2012-01-04 18:16:14 -0800 | [diff] [blame] | 632 | /* Set an initial rate and enable the PLL. */ |
| 633 | hfpll_set_rate(sc, tgt_s); |
Matt Wagantall | 75473eb | 2012-05-31 15:23:22 -0700 | [diff] [blame] | 634 | hfpll_enable(sc, false); |
| 635 | } |
| 636 | |
Matt Wagantall | 302d9a3 | 2012-07-03 13:37:29 -0700 | [diff] [blame] | 637 | static int __cpuinit rpm_regulator_init(struct scalable *sc, enum vregs vreg, |
Matt Wagantall | 754ee27 | 2012-06-18 13:40:26 -0700 | [diff] [blame] | 638 | int vdd, bool enable) |
Matt Wagantall | 75473eb | 2012-05-31 15:23:22 -0700 | [diff] [blame] | 639 | { |
| 640 | int ret; |
| 641 | |
| 642 | if (!sc->vreg[vreg].name) |
Matt Wagantall | 302d9a3 | 2012-07-03 13:37:29 -0700 | [diff] [blame] | 643 | return 0; |
Matt Wagantall | 75473eb | 2012-05-31 15:23:22 -0700 | [diff] [blame] | 644 | |
| 645 | sc->vreg[vreg].rpm_reg = rpm_regulator_get(drv.dev, |
| 646 | sc->vreg[vreg].name); |
| 647 | if (IS_ERR(sc->vreg[vreg].rpm_reg)) { |
Matt Wagantall | 302d9a3 | 2012-07-03 13:37:29 -0700 | [diff] [blame] | 648 | ret = PTR_ERR(sc->vreg[vreg].rpm_reg); |
| 649 | dev_err(drv.dev, "rpm_regulator_get(%s) failed (%d)\n", |
| 650 | sc->vreg[vreg].name, ret); |
| 651 | goto err_get; |
Matt Wagantall | 75473eb | 2012-05-31 15:23:22 -0700 | [diff] [blame] | 652 | } |
| 653 | |
| 654 | ret = rpm_regulator_set_voltage(sc->vreg[vreg].rpm_reg, vdd, |
| 655 | sc->vreg[vreg].max_vdd); |
| 656 | if (ret) { |
| 657 | dev_err(drv.dev, "%s initialization failed (%d)\n", |
| 658 | sc->vreg[vreg].name, ret); |
Matt Wagantall | 302d9a3 | 2012-07-03 13:37:29 -0700 | [diff] [blame] | 659 | goto err_conf; |
Matt Wagantall | 75473eb | 2012-05-31 15:23:22 -0700 | [diff] [blame] | 660 | } |
| 661 | sc->vreg[vreg].cur_vdd = vdd; |
| 662 | |
Matt Wagantall | 302d9a3 | 2012-07-03 13:37:29 -0700 | [diff] [blame] | 663 | if (enable) { |
| 664 | ret = enable_rpm_vreg(&sc->vreg[vreg]); |
| 665 | if (ret) |
| 666 | goto err_conf; |
| 667 | } |
| 668 | |
| 669 | return 0; |
| 670 | |
| 671 | err_conf: |
| 672 | rpm_regulator_put(sc->vreg[vreg].rpm_reg); |
| 673 | err_get: |
| 674 | return ret; |
| 675 | } |
| 676 | |
| 677 | static void __cpuinit rpm_regulator_cleanup(struct scalable *sc, |
| 678 | enum vregs vreg) |
| 679 | { |
| 680 | if (!sc->vreg[vreg].rpm_reg) |
| 681 | return; |
| 682 | |
| 683 | disable_rpm_vreg(&sc->vreg[vreg]); |
| 684 | rpm_regulator_put(sc->vreg[vreg].rpm_reg); |
Matt Wagantall | e9b715a | 2012-01-04 18:16:14 -0800 | [diff] [blame] | 685 | } |
| 686 | |
| 687 | /* Voltage regulator initialization. */ |
Matt Wagantall | 9c8cb6e | 2012-07-13 19:39:15 -0700 | [diff] [blame] | 688 | static int __cpuinit regulator_init(struct scalable *sc, |
| 689 | const struct acpu_level *acpu_level) |
Matt Wagantall | e9b715a | 2012-01-04 18:16:14 -0800 | [diff] [blame] | 690 | { |
Matt Wagantall | 754ee27 | 2012-06-18 13:40:26 -0700 | [diff] [blame] | 691 | int ret, vdd_mem, vdd_dig, vdd_core; |
Matt Wagantall | e9b715a | 2012-01-04 18:16:14 -0800 | [diff] [blame] | 692 | |
Matt Wagantall | 9c8cb6e | 2012-07-13 19:39:15 -0700 | [diff] [blame] | 693 | vdd_mem = calculate_vdd_mem(acpu_level); |
Matt Wagantall | 302d9a3 | 2012-07-03 13:37:29 -0700 | [diff] [blame] | 694 | ret = rpm_regulator_init(sc, VREG_MEM, vdd_mem, true); |
| 695 | if (ret) |
| 696 | goto err_mem; |
Matt Wagantall | 9c8cb6e | 2012-07-13 19:39:15 -0700 | [diff] [blame] | 697 | |
| 698 | vdd_dig = calculate_vdd_dig(acpu_level); |
Matt Wagantall | 302d9a3 | 2012-07-03 13:37:29 -0700 | [diff] [blame] | 699 | ret = rpm_regulator_init(sc, VREG_DIG, vdd_dig, true); |
| 700 | if (ret) |
| 701 | goto err_dig; |
Matt Wagantall | 9c8cb6e | 2012-07-13 19:39:15 -0700 | [diff] [blame] | 702 | |
Matt Wagantall | 302d9a3 | 2012-07-03 13:37:29 -0700 | [diff] [blame] | 703 | ret = rpm_regulator_init(sc, VREG_HFPLL_A, |
Matt Wagantall | 754ee27 | 2012-06-18 13:40:26 -0700 | [diff] [blame] | 704 | sc->vreg[VREG_HFPLL_A].max_vdd, false); |
Matt Wagantall | 302d9a3 | 2012-07-03 13:37:29 -0700 | [diff] [blame] | 705 | if (ret) |
| 706 | goto err_hfpll_a; |
| 707 | ret = rpm_regulator_init(sc, VREG_HFPLL_B, |
Matt Wagantall | 754ee27 | 2012-06-18 13:40:26 -0700 | [diff] [blame] | 708 | sc->vreg[VREG_HFPLL_B].max_vdd, false); |
Matt Wagantall | 302d9a3 | 2012-07-03 13:37:29 -0700 | [diff] [blame] | 709 | if (ret) |
| 710 | goto err_hfpll_b; |
Matt Wagantall | 75473eb | 2012-05-31 15:23:22 -0700 | [diff] [blame] | 711 | |
Matt Wagantall | 754ee27 | 2012-06-18 13:40:26 -0700 | [diff] [blame] | 712 | /* Setup Krait CPU regulators and initial core voltage. */ |
| 713 | sc->vreg[VREG_CORE].reg = regulator_get(drv.dev, |
| 714 | sc->vreg[VREG_CORE].name); |
| 715 | if (IS_ERR(sc->vreg[VREG_CORE].reg)) { |
Matt Wagantall | 302d9a3 | 2012-07-03 13:37:29 -0700 | [diff] [blame] | 716 | ret = PTR_ERR(sc->vreg[VREG_CORE].reg); |
| 717 | dev_err(drv.dev, "regulator_get(%s) failed (%d)\n", |
| 718 | sc->vreg[VREG_CORE].name, ret); |
| 719 | goto err_core_get; |
Matt Wagantall | 754ee27 | 2012-06-18 13:40:26 -0700 | [diff] [blame] | 720 | } |
Matt Wagantall | 6d9c416 | 2012-07-16 18:58:16 -0700 | [diff] [blame] | 721 | ret = regulator_set_optimum_mode(sc->vreg[VREG_CORE].reg, |
| 722 | acpu_level->ua_core); |
| 723 | if (ret < 0) { |
| 724 | dev_err(drv.dev, "regulator_set_optimum_mode(%s) failed (%d)\n", |
| 725 | sc->vreg[VREG_CORE].name, ret); |
| 726 | goto err_core_conf; |
| 727 | } |
| 728 | sc->vreg[VREG_CORE].cur_ua = acpu_level->ua_core; |
Matt Wagantall | 9c8cb6e | 2012-07-13 19:39:15 -0700 | [diff] [blame] | 729 | vdd_core = calculate_vdd_core(acpu_level); |
Matt Wagantall | 754ee27 | 2012-06-18 13:40:26 -0700 | [diff] [blame] | 730 | ret = regulator_set_voltage(sc->vreg[VREG_CORE].reg, vdd_core, |
| 731 | sc->vreg[VREG_CORE].max_vdd); |
| 732 | if (ret) { |
| 733 | dev_err(drv.dev, "regulator_set_voltage(%s) (%d)\n", |
| 734 | sc->vreg[VREG_CORE].name, ret); |
Matt Wagantall | 302d9a3 | 2012-07-03 13:37:29 -0700 | [diff] [blame] | 735 | goto err_core_conf; |
Matt Wagantall | 754ee27 | 2012-06-18 13:40:26 -0700 | [diff] [blame] | 736 | } |
| 737 | sc->vreg[VREG_CORE].cur_vdd = vdd_core; |
Matt Wagantall | 754ee27 | 2012-06-18 13:40:26 -0700 | [diff] [blame] | 738 | ret = regulator_enable(sc->vreg[VREG_CORE].reg); |
| 739 | if (ret) { |
| 740 | dev_err(drv.dev, "regulator_enable(%s) failed (%d)\n", |
| 741 | sc->vreg[VREG_CORE].name, ret); |
Matt Wagantall | 302d9a3 | 2012-07-03 13:37:29 -0700 | [diff] [blame] | 742 | goto err_core_conf; |
Matt Wagantall | e9b715a | 2012-01-04 18:16:14 -0800 | [diff] [blame] | 743 | } |
Matt Wagantall | 302d9a3 | 2012-07-03 13:37:29 -0700 | [diff] [blame] | 744 | |
Vikram Mulukutla | 5a623db | 2012-10-19 08:53:19 -0700 | [diff] [blame] | 745 | /* |
| 746 | * Increment the L2 HFPLL regulator refcount if _this_ CPU's frequency |
| 747 | * requires a corresponding target L2 frequency that needs the L2 to |
| 748 | * run off of an HFPLL. |
| 749 | */ |
| 750 | if (drv.l2_freq_tbl[acpu_level->l2_level].speed.src == HFPLL) |
| 751 | l2_vreg_count++; |
| 752 | |
Matt Wagantall | 302d9a3 | 2012-07-03 13:37:29 -0700 | [diff] [blame] | 753 | return 0; |
| 754 | |
| 755 | err_core_conf: |
| 756 | regulator_put(sc->vreg[VREG_CORE].reg); |
| 757 | err_core_get: |
| 758 | rpm_regulator_cleanup(sc, VREG_HFPLL_B); |
| 759 | err_hfpll_b: |
| 760 | rpm_regulator_cleanup(sc, VREG_HFPLL_A); |
| 761 | err_hfpll_a: |
| 762 | rpm_regulator_cleanup(sc, VREG_DIG); |
| 763 | err_dig: |
| 764 | rpm_regulator_cleanup(sc, VREG_MEM); |
| 765 | err_mem: |
| 766 | return ret; |
| 767 | } |
| 768 | |
| 769 | static void __cpuinit regulator_cleanup(struct scalable *sc) |
| 770 | { |
| 771 | regulator_disable(sc->vreg[VREG_CORE].reg); |
| 772 | regulator_put(sc->vreg[VREG_CORE].reg); |
| 773 | rpm_regulator_cleanup(sc, VREG_HFPLL_B); |
| 774 | rpm_regulator_cleanup(sc, VREG_HFPLL_A); |
| 775 | rpm_regulator_cleanup(sc, VREG_DIG); |
| 776 | rpm_regulator_cleanup(sc, VREG_MEM); |
Matt Wagantall | e9b715a | 2012-01-04 18:16:14 -0800 | [diff] [blame] | 777 | } |
| 778 | |
| 779 | /* Set initial rate for a given core. */ |
Matt Wagantall | 302d9a3 | 2012-07-03 13:37:29 -0700 | [diff] [blame] | 780 | static int __cpuinit init_clock_sources(struct scalable *sc, |
Matt Wagantall | 754ee27 | 2012-06-18 13:40:26 -0700 | [diff] [blame] | 781 | const struct core_speed *tgt_s) |
Matt Wagantall | e9b715a | 2012-01-04 18:16:14 -0800 | [diff] [blame] | 782 | { |
| 783 | u32 regval; |
Matt Wagantall | 06e4a1f | 2012-06-07 18:38:13 -0700 | [diff] [blame] | 784 | void __iomem *aux_reg; |
Matt Wagantall | e9b715a | 2012-01-04 18:16:14 -0800 | [diff] [blame] | 785 | |
| 786 | /* Program AUX source input to the secondary MUX. */ |
Matt Wagantall | 06e4a1f | 2012-06-07 18:38:13 -0700 | [diff] [blame] | 787 | if (sc->aux_clk_sel_phys) { |
| 788 | aux_reg = ioremap(sc->aux_clk_sel_phys, 4); |
Matt Wagantall | 302d9a3 | 2012-07-03 13:37:29 -0700 | [diff] [blame] | 789 | if (!aux_reg) |
| 790 | return -ENOMEM; |
Matt Wagantall | 06e4a1f | 2012-06-07 18:38:13 -0700 | [diff] [blame] | 791 | writel_relaxed(sc->aux_clk_sel, aux_reg); |
| 792 | iounmap(aux_reg); |
| 793 | } |
Matt Wagantall | e9b715a | 2012-01-04 18:16:14 -0800 | [diff] [blame] | 794 | |
| 795 | /* Switch away from the HFPLL while it's re-initialized. */ |
Matt Wagantall | a133dbf | 2012-09-27 19:56:57 -0700 | [diff] [blame] | 796 | set_sec_clk_src(sc, sc->sec_clk_sel); |
Matt Wagantall | e9b715a | 2012-01-04 18:16:14 -0800 | [diff] [blame] | 797 | set_pri_clk_src(sc, PRI_SRC_SEL_SEC_SRC); |
| 798 | hfpll_init(sc, tgt_s); |
| 799 | |
| 800 | /* Set PRI_SRC_SEL_HFPLL_DIV2 divider to div-2. */ |
| 801 | regval = get_l2_indirect_reg(sc->l2cpmr_iaddr); |
| 802 | regval &= ~(0x3 << 6); |
| 803 | set_l2_indirect_reg(sc->l2cpmr_iaddr, regval); |
| 804 | |
| 805 | /* Switch to the target clock source. */ |
Matt Wagantall | e9b715a | 2012-01-04 18:16:14 -0800 | [diff] [blame] | 806 | set_pri_clk_src(sc, tgt_s->pri_src_sel); |
| 807 | sc->cur_speed = tgt_s; |
Matt Wagantall | 302d9a3 | 2012-07-03 13:37:29 -0700 | [diff] [blame] | 808 | |
| 809 | return 0; |
Matt Wagantall | e9b715a | 2012-01-04 18:16:14 -0800 | [diff] [blame] | 810 | } |
| 811 | |
Matt Wagantall | 9c8cb6e | 2012-07-13 19:39:15 -0700 | [diff] [blame] | 812 | static void __cpuinit fill_cur_core_speed(struct core_speed *s, |
| 813 | struct scalable *sc) |
| 814 | { |
| 815 | s->pri_src_sel = get_l2_indirect_reg(sc->l2cpmr_iaddr) & 0x3; |
Matt Wagantall | 9c8cb6e | 2012-07-13 19:39:15 -0700 | [diff] [blame] | 816 | s->pll_l_val = readl_relaxed(sc->hfpll_base + drv.hfpll_data->l_offset); |
| 817 | } |
| 818 | |
| 819 | static bool __cpuinit speed_equal(const struct core_speed *s1, |
| 820 | const struct core_speed *s2) |
| 821 | { |
| 822 | return (s1->pri_src_sel == s2->pri_src_sel && |
Matt Wagantall | 9c8cb6e | 2012-07-13 19:39:15 -0700 | [diff] [blame] | 823 | s1->pll_l_val == s2->pll_l_val); |
| 824 | } |
| 825 | |
| 826 | static const struct acpu_level __cpuinit *find_cur_acpu_level(int cpu) |
| 827 | { |
| 828 | struct scalable *sc = &drv.scalable[cpu]; |
| 829 | const struct acpu_level *l; |
| 830 | struct core_speed cur_speed; |
| 831 | |
| 832 | fill_cur_core_speed(&cur_speed, sc); |
| 833 | for (l = drv.acpu_freq_tbl; l->speed.khz != 0; l++) |
| 834 | if (speed_equal(&l->speed, &cur_speed)) |
| 835 | return l; |
| 836 | return NULL; |
| 837 | } |
| 838 | |
| 839 | static const struct l2_level __init *find_cur_l2_level(void) |
| 840 | { |
| 841 | struct scalable *sc = &drv.scalable[L2]; |
| 842 | const struct l2_level *l; |
| 843 | struct core_speed cur_speed; |
| 844 | |
| 845 | fill_cur_core_speed(&cur_speed, sc); |
| 846 | for (l = drv.l2_freq_tbl; l->speed.khz != 0; l++) |
| 847 | if (speed_equal(&l->speed, &cur_speed)) |
| 848 | return l; |
| 849 | return NULL; |
| 850 | } |
| 851 | |
| 852 | static const struct acpu_level __cpuinit *find_min_acpu_level(void) |
| 853 | { |
| 854 | struct acpu_level *l; |
| 855 | |
| 856 | for (l = drv.acpu_freq_tbl; l->speed.khz != 0; l++) |
| 857 | if (l->use_for_scaling) |
| 858 | return l; |
| 859 | |
| 860 | return NULL; |
| 861 | } |
| 862 | |
Matt Wagantall | 302d9a3 | 2012-07-03 13:37:29 -0700 | [diff] [blame] | 863 | static int __cpuinit per_cpu_init(int cpu) |
Matt Wagantall | e9b715a | 2012-01-04 18:16:14 -0800 | [diff] [blame] | 864 | { |
Matt Wagantall | 754ee27 | 2012-06-18 13:40:26 -0700 | [diff] [blame] | 865 | struct scalable *sc = &drv.scalable[cpu]; |
Matt Wagantall | 9c8cb6e | 2012-07-13 19:39:15 -0700 | [diff] [blame] | 866 | const struct acpu_level *acpu_level; |
Matt Wagantall | 302d9a3 | 2012-07-03 13:37:29 -0700 | [diff] [blame] | 867 | int ret; |
Matt Wagantall | e9b715a | 2012-01-04 18:16:14 -0800 | [diff] [blame] | 868 | |
Matt Wagantall | 754ee27 | 2012-06-18 13:40:26 -0700 | [diff] [blame] | 869 | sc->hfpll_base = ioremap(sc->hfpll_phys_base, SZ_32); |
Matt Wagantall | 302d9a3 | 2012-07-03 13:37:29 -0700 | [diff] [blame] | 870 | if (!sc->hfpll_base) { |
| 871 | ret = -ENOMEM; |
| 872 | goto err_ioremap; |
| 873 | } |
Matt Wagantall | 754ee27 | 2012-06-18 13:40:26 -0700 | [diff] [blame] | 874 | |
Matt Wagantall | 9c8cb6e | 2012-07-13 19:39:15 -0700 | [diff] [blame] | 875 | acpu_level = find_cur_acpu_level(cpu); |
Matt Wagantall | b7c231b | 2012-07-24 18:40:17 -0700 | [diff] [blame] | 876 | if (!acpu_level) { |
Matt Wagantall | 9c8cb6e | 2012-07-13 19:39:15 -0700 | [diff] [blame] | 877 | acpu_level = find_min_acpu_level(); |
| 878 | if (!acpu_level) { |
| 879 | ret = -ENODEV; |
| 880 | goto err_table; |
| 881 | } |
| 882 | dev_dbg(drv.dev, "CPU%d is running at an unknown rate. Defaulting to %lu KHz.\n", |
| 883 | cpu, acpu_level->speed.khz); |
| 884 | } else { |
| 885 | dev_dbg(drv.dev, "CPU%d is running at %lu KHz\n", cpu, |
| 886 | acpu_level->speed.khz); |
| 887 | } |
| 888 | |
| 889 | ret = regulator_init(sc, acpu_level); |
Matt Wagantall | 302d9a3 | 2012-07-03 13:37:29 -0700 | [diff] [blame] | 890 | if (ret) |
| 891 | goto err_regulators; |
Matt Wagantall | 754ee27 | 2012-06-18 13:40:26 -0700 | [diff] [blame] | 892 | |
Matt Wagantall | 9c8cb6e | 2012-07-13 19:39:15 -0700 | [diff] [blame] | 893 | ret = init_clock_sources(sc, &acpu_level->speed); |
Matt Wagantall | 302d9a3 | 2012-07-03 13:37:29 -0700 | [diff] [blame] | 894 | if (ret) |
| 895 | goto err_clocks; |
Matt Wagantall | 9c8cb6e | 2012-07-13 19:39:15 -0700 | [diff] [blame] | 896 | |
| 897 | sc->l2_vote = acpu_level->l2_level; |
Matt Wagantall | 754ee27 | 2012-06-18 13:40:26 -0700 | [diff] [blame] | 898 | sc->initialized = true; |
Matt Wagantall | 302d9a3 | 2012-07-03 13:37:29 -0700 | [diff] [blame] | 899 | |
| 900 | return 0; |
| 901 | |
| 902 | err_clocks: |
| 903 | regulator_cleanup(sc); |
| 904 | err_regulators: |
Matt Wagantall | 9c8cb6e | 2012-07-13 19:39:15 -0700 | [diff] [blame] | 905 | err_table: |
Matt Wagantall | 302d9a3 | 2012-07-03 13:37:29 -0700 | [diff] [blame] | 906 | iounmap(sc->hfpll_base); |
| 907 | err_ioremap: |
| 908 | return ret; |
Matt Wagantall | e9b715a | 2012-01-04 18:16:14 -0800 | [diff] [blame] | 909 | } |
| 910 | |
| 911 | /* Register with bus driver. */ |
Matt Wagantall | 9c8cb6e | 2012-07-13 19:39:15 -0700 | [diff] [blame] | 912 | static void __init bus_init(const struct l2_level *l2_level) |
Matt Wagantall | e9b715a | 2012-01-04 18:16:14 -0800 | [diff] [blame] | 913 | { |
| 914 | int ret; |
| 915 | |
Matt Wagantall | 1f3762d | 2012-06-08 19:08:48 -0700 | [diff] [blame] | 916 | drv.bus_perf_client = msm_bus_scale_register_client(drv.bus_scale); |
Matt Wagantall | e9b715a | 2012-01-04 18:16:14 -0800 | [diff] [blame] | 917 | if (!drv.bus_perf_client) { |
| 918 | dev_err(drv.dev, "unable to register bus client\n"); |
| 919 | BUG(); |
| 920 | } |
| 921 | |
Matt Wagantall | 754ee27 | 2012-06-18 13:40:26 -0700 | [diff] [blame] | 922 | ret = msm_bus_scale_client_update_request(drv.bus_perf_client, |
Matt Wagantall | 9c8cb6e | 2012-07-13 19:39:15 -0700 | [diff] [blame] | 923 | l2_level->bw_level); |
Matt Wagantall | e9b715a | 2012-01-04 18:16:14 -0800 | [diff] [blame] | 924 | if (ret) |
| 925 | dev_err(drv.dev, "initial bandwidth req failed (%d)\n", ret); |
| 926 | } |
| 927 | |
| 928 | #ifdef CONFIG_CPU_FREQ_MSM |
| 929 | static struct cpufreq_frequency_table freq_table[NR_CPUS][35]; |
| 930 | |
| 931 | static void __init cpufreq_table_init(void) |
| 932 | { |
| 933 | int cpu; |
| 934 | |
| 935 | for_each_possible_cpu(cpu) { |
| 936 | int i, freq_cnt = 0; |
| 937 | /* Construct the freq_table tables from acpu_freq_tbl. */ |
| 938 | for (i = 0; drv.acpu_freq_tbl[i].speed.khz != 0 |
| 939 | && freq_cnt < ARRAY_SIZE(*freq_table); i++) { |
| 940 | if (drv.acpu_freq_tbl[i].use_for_scaling) { |
| 941 | freq_table[cpu][freq_cnt].index = freq_cnt; |
| 942 | freq_table[cpu][freq_cnt].frequency |
| 943 | = drv.acpu_freq_tbl[i].speed.khz; |
| 944 | freq_cnt++; |
| 945 | } |
| 946 | } |
| 947 | /* freq_table not big enough to store all usable freqs. */ |
| 948 | BUG_ON(drv.acpu_freq_tbl[i].speed.khz != 0); |
| 949 | |
| 950 | freq_table[cpu][freq_cnt].index = freq_cnt; |
| 951 | freq_table[cpu][freq_cnt].frequency = CPUFREQ_TABLE_END; |
| 952 | |
| 953 | dev_info(drv.dev, "CPU%d: %d frequencies supported\n", |
| 954 | cpu, freq_cnt); |
| 955 | |
| 956 | /* Register table with CPUFreq. */ |
| 957 | cpufreq_frequency_table_get_attr(freq_table[cpu], cpu); |
| 958 | } |
| 959 | } |
| 960 | #else |
| 961 | static void __init cpufreq_table_init(void) {} |
| 962 | #endif |
| 963 | |
Steve Muckle | a9aac29 | 2012-11-02 15:41:00 -0700 | [diff] [blame] | 964 | static void __init dcvs_freq_init(void) |
| 965 | { |
| 966 | int i; |
| 967 | |
| 968 | for (i = 0; drv.acpu_freq_tbl[i].speed.khz != 0; i++) |
| 969 | if (drv.acpu_freq_tbl[i].use_for_scaling) |
| 970 | msm_dcvs_register_cpu_freq( |
| 971 | drv.acpu_freq_tbl[i].speed.khz, |
| 972 | drv.acpu_freq_tbl[i].vdd_core / 1000); |
| 973 | } |
| 974 | |
Matt Wagantall | e9b715a | 2012-01-04 18:16:14 -0800 | [diff] [blame] | 975 | static int __cpuinit acpuclk_cpu_callback(struct notifier_block *nfb, |
| 976 | unsigned long action, void *hcpu) |
| 977 | { |
| 978 | static int prev_khz[NR_CPUS]; |
| 979 | int rc, cpu = (int)hcpu; |
| 980 | struct scalable *sc = &drv.scalable[cpu]; |
Matt Wagantall | b7c231b | 2012-07-24 18:40:17 -0700 | [diff] [blame] | 981 | unsigned long hot_unplug_khz = acpuclk_krait_data.power_collapse_khz; |
Matt Wagantall | e9b715a | 2012-01-04 18:16:14 -0800 | [diff] [blame] | 982 | |
| 983 | switch (action & ~CPU_TASKS_FROZEN) { |
| 984 | case CPU_DEAD: |
| 985 | prev_khz[cpu] = acpuclk_krait_get_rate(cpu); |
| 986 | /* Fall through. */ |
| 987 | case CPU_UP_CANCELED: |
Matt Wagantall | b7c231b | 2012-07-24 18:40:17 -0700 | [diff] [blame] | 988 | acpuclk_krait_set_rate(cpu, hot_unplug_khz, SETRATE_HOTPLUG); |
Matt Wagantall | e9b715a | 2012-01-04 18:16:14 -0800 | [diff] [blame] | 989 | regulator_set_optimum_mode(sc->vreg[VREG_CORE].reg, 0); |
| 990 | break; |
| 991 | case CPU_UP_PREPARE: |
Matt Wagantall | 754ee27 | 2012-06-18 13:40:26 -0700 | [diff] [blame] | 992 | if (!sc->initialized) { |
Matt Wagantall | 302d9a3 | 2012-07-03 13:37:29 -0700 | [diff] [blame] | 993 | rc = per_cpu_init(cpu); |
| 994 | if (rc) |
| 995 | return NOTIFY_BAD; |
Matt Wagantall | 754ee27 | 2012-06-18 13:40:26 -0700 | [diff] [blame] | 996 | break; |
| 997 | } |
Matt Wagantall | e9b715a | 2012-01-04 18:16:14 -0800 | [diff] [blame] | 998 | if (WARN_ON(!prev_khz[cpu])) |
| 999 | return NOTIFY_BAD; |
| 1000 | rc = regulator_set_optimum_mode(sc->vreg[VREG_CORE].reg, |
Matt Wagantall | 6d9c416 | 2012-07-16 18:58:16 -0700 | [diff] [blame] | 1001 | sc->vreg[VREG_CORE].cur_ua); |
Matt Wagantall | e9b715a | 2012-01-04 18:16:14 -0800 | [diff] [blame] | 1002 | if (rc < 0) |
| 1003 | return NOTIFY_BAD; |
| 1004 | acpuclk_krait_set_rate(cpu, prev_khz[cpu], SETRATE_HOTPLUG); |
| 1005 | break; |
| 1006 | default: |
| 1007 | break; |
| 1008 | } |
| 1009 | |
| 1010 | return NOTIFY_OK; |
| 1011 | } |
| 1012 | |
| 1013 | static struct notifier_block __cpuinitdata acpuclk_cpu_notifier = { |
| 1014 | .notifier_call = acpuclk_cpu_callback, |
| 1015 | }; |
| 1016 | |
Matt Wagantall | 06e4a1f | 2012-06-07 18:38:13 -0700 | [diff] [blame] | 1017 | static const int krait_needs_vmin(void) |
| 1018 | { |
| 1019 | switch (read_cpuid_id()) { |
| 1020 | case 0x511F04D0: /* KR28M2A20 */ |
| 1021 | case 0x511F04D1: /* KR28M2A21 */ |
| 1022 | case 0x510F06F0: /* KR28M4A10 */ |
| 1023 | return 1; |
| 1024 | default: |
| 1025 | return 0; |
| 1026 | }; |
| 1027 | } |
| 1028 | |
| 1029 | static void krait_apply_vmin(struct acpu_level *tbl) |
| 1030 | { |
Stephen Boyd | c13b679 | 2012-09-14 11:25:34 -0700 | [diff] [blame] | 1031 | for (; tbl->speed.khz != 0; tbl++) { |
Matt Wagantall | 06e4a1f | 2012-06-07 18:38:13 -0700 | [diff] [blame] | 1032 | if (tbl->vdd_core < 1150000) |
| 1033 | tbl->vdd_core = 1150000; |
Stephen Boyd | c13b679 | 2012-09-14 11:25:34 -0700 | [diff] [blame] | 1034 | tbl->avsdscr_setting = 0; |
| 1035 | } |
Matt Wagantall | 06e4a1f | 2012-06-07 18:38:13 -0700 | [diff] [blame] | 1036 | } |
| 1037 | |
Patrick Daly | 02db5a8 | 2012-08-24 14:22:06 -0700 | [diff] [blame] | 1038 | static int __init get_speed_bin(u32 pte_efuse) |
| 1039 | { |
| 1040 | uint32_t speed_bin; |
| 1041 | |
| 1042 | speed_bin = pte_efuse & 0xF; |
| 1043 | if (speed_bin == 0xF) |
| 1044 | speed_bin = (pte_efuse >> 4) & 0xF; |
| 1045 | |
| 1046 | if (speed_bin == 0xF) { |
| 1047 | speed_bin = 0; |
| 1048 | dev_warn(drv.dev, "SPEED BIN: Defaulting to %d\n", speed_bin); |
| 1049 | } else { |
| 1050 | dev_info(drv.dev, "SPEED BIN: %d\n", speed_bin); |
| 1051 | } |
| 1052 | |
| 1053 | return speed_bin; |
| 1054 | } |
| 1055 | |
| 1056 | static int __init get_pvs_bin(u32 pte_efuse) |
| 1057 | { |
| 1058 | uint32_t pvs_bin; |
| 1059 | |
| 1060 | pvs_bin = (pte_efuse >> 10) & 0x7; |
| 1061 | if (pvs_bin == 0x7) |
| 1062 | pvs_bin = (pte_efuse >> 13) & 0x7; |
| 1063 | |
| 1064 | if (pvs_bin == 0x7) { |
| 1065 | pvs_bin = 0; |
| 1066 | dev_warn(drv.dev, "ACPU PVS: Defaulting to %d\n", pvs_bin); |
| 1067 | } else { |
| 1068 | dev_info(drv.dev, "ACPU PVS: %d\n", pvs_bin); |
| 1069 | } |
| 1070 | |
| 1071 | return pvs_bin; |
| 1072 | } |
| 1073 | |
| 1074 | static struct pvs_table * __init select_freq_plan(u32 pte_efuse_phys, |
| 1075 | struct pvs_table (*pvs_tables)[NUM_PVS]) |
Matt Wagantall | e9b715a | 2012-01-04 18:16:14 -0800 | [diff] [blame] | 1076 | { |
Matt Wagantall | 519e94f | 2012-09-17 17:51:06 -0700 | [diff] [blame] | 1077 | void __iomem *pte_efuse; |
Patrick Daly | 02db5a8 | 2012-08-24 14:22:06 -0700 | [diff] [blame] | 1078 | u32 pte_efuse_val, tbl_idx, bin_idx; |
Matt Wagantall | e9b715a | 2012-01-04 18:16:14 -0800 | [diff] [blame] | 1079 | |
Matt Wagantall | 519e94f | 2012-09-17 17:51:06 -0700 | [diff] [blame] | 1080 | pte_efuse = ioremap(pte_efuse_phys, 4); |
Patrick Daly | 02db5a8 | 2012-08-24 14:22:06 -0700 | [diff] [blame] | 1081 | if (!pte_efuse) { |
Matt Wagantall | e9b715a | 2012-01-04 18:16:14 -0800 | [diff] [blame] | 1082 | dev_err(drv.dev, "Unable to map QFPROM base\n"); |
Patrick Daly | 02db5a8 | 2012-08-24 14:22:06 -0700 | [diff] [blame] | 1083 | return NULL; |
Matt Wagantall | e9b715a | 2012-01-04 18:16:14 -0800 | [diff] [blame] | 1084 | } |
Matt Wagantall | e9b715a | 2012-01-04 18:16:14 -0800 | [diff] [blame] | 1085 | |
Patrick Daly | 02db5a8 | 2012-08-24 14:22:06 -0700 | [diff] [blame] | 1086 | pte_efuse_val = readl_relaxed(pte_efuse); |
| 1087 | iounmap(pte_efuse); |
| 1088 | |
| 1089 | /* Select frequency tables. */ |
| 1090 | bin_idx = get_speed_bin(pte_efuse_val); |
| 1091 | tbl_idx = get_pvs_bin(pte_efuse_val); |
| 1092 | |
| 1093 | return &pvs_tables[bin_idx][tbl_idx]; |
Matt Wagantall | 1f3762d | 2012-06-08 19:08:48 -0700 | [diff] [blame] | 1094 | } |
Matt Wagantall | 06e4a1f | 2012-06-07 18:38:13 -0700 | [diff] [blame] | 1095 | |
Matt Wagantall | 1f3762d | 2012-06-08 19:08:48 -0700 | [diff] [blame] | 1096 | static void __init drv_data_init(struct device *dev, |
| 1097 | const struct acpuclk_krait_params *params) |
Matt Wagantall | e9b715a | 2012-01-04 18:16:14 -0800 | [diff] [blame] | 1098 | { |
Patrick Daly | 02db5a8 | 2012-08-24 14:22:06 -0700 | [diff] [blame] | 1099 | struct pvs_table *pvs; |
Matt Wagantall | 1f3762d | 2012-06-08 19:08:48 -0700 | [diff] [blame] | 1100 | |
| 1101 | drv.dev = dev; |
| 1102 | drv.scalable = kmemdup(params->scalable, params->scalable_size, |
| 1103 | GFP_KERNEL); |
| 1104 | BUG_ON(!drv.scalable); |
| 1105 | |
| 1106 | drv.hfpll_data = kmemdup(params->hfpll_data, sizeof(*drv.hfpll_data), |
| 1107 | GFP_KERNEL); |
| 1108 | BUG_ON(!drv.hfpll_data); |
| 1109 | |
| 1110 | drv.l2_freq_tbl = kmemdup(params->l2_freq_tbl, params->l2_freq_tbl_size, |
| 1111 | GFP_KERNEL); |
| 1112 | BUG_ON(!drv.l2_freq_tbl); |
| 1113 | |
| 1114 | drv.bus_scale = kmemdup(params->bus_scale, sizeof(*drv.bus_scale), |
| 1115 | GFP_KERNEL); |
| 1116 | BUG_ON(!drv.bus_scale); |
| 1117 | drv.bus_scale->usecase = kmemdup(drv.bus_scale->usecase, |
| 1118 | drv.bus_scale->num_usecases * sizeof(*drv.bus_scale->usecase), |
| 1119 | GFP_KERNEL); |
| 1120 | BUG_ON(!drv.bus_scale->usecase); |
| 1121 | |
Patrick Daly | 02db5a8 | 2012-08-24 14:22:06 -0700 | [diff] [blame] | 1122 | pvs = select_freq_plan(params->pte_efuse_phys, params->pvs_tables); |
| 1123 | BUG_ON(!pvs->table); |
| 1124 | |
| 1125 | drv.acpu_freq_tbl = kmemdup(pvs->table, pvs->size, GFP_KERNEL); |
Matt Wagantall | 1f3762d | 2012-06-08 19:08:48 -0700 | [diff] [blame] | 1126 | BUG_ON(!drv.acpu_freq_tbl); |
Patrick Daly | 02db5a8 | 2012-08-24 14:22:06 -0700 | [diff] [blame] | 1127 | drv.boost_uv = pvs->boost_uv; |
Matt Wagantall | b7c231b | 2012-07-24 18:40:17 -0700 | [diff] [blame] | 1128 | |
| 1129 | acpuclk_krait_data.power_collapse_khz = params->stby_khz; |
| 1130 | acpuclk_krait_data.wait_for_irq_khz = params->stby_khz; |
Matt Wagantall | 1f3762d | 2012-06-08 19:08:48 -0700 | [diff] [blame] | 1131 | } |
| 1132 | |
| 1133 | static void __init hw_init(void) |
| 1134 | { |
| 1135 | struct scalable *l2 = &drv.scalable[L2]; |
Matt Wagantall | 9c8cb6e | 2012-07-13 19:39:15 -0700 | [diff] [blame] | 1136 | const struct l2_level *l2_level; |
Matt Wagantall | 302d9a3 | 2012-07-03 13:37:29 -0700 | [diff] [blame] | 1137 | int cpu, rc; |
Matt Wagantall | e9b715a | 2012-01-04 18:16:14 -0800 | [diff] [blame] | 1138 | |
Matt Wagantall | 1f3762d | 2012-06-08 19:08:48 -0700 | [diff] [blame] | 1139 | if (krait_needs_vmin()) |
| 1140 | krait_apply_vmin(drv.acpu_freq_tbl); |
Matt Wagantall | e9b715a | 2012-01-04 18:16:14 -0800 | [diff] [blame] | 1141 | |
Matt Wagantall | 754ee27 | 2012-06-18 13:40:26 -0700 | [diff] [blame] | 1142 | l2->hfpll_base = ioremap(l2->hfpll_phys_base, SZ_32); |
| 1143 | BUG_ON(!l2->hfpll_base); |
Matt Wagantall | 754ee27 | 2012-06-18 13:40:26 -0700 | [diff] [blame] | 1144 | |
Matt Wagantall | 302d9a3 | 2012-07-03 13:37:29 -0700 | [diff] [blame] | 1145 | rc = rpm_regulator_init(l2, VREG_HFPLL_A, |
| 1146 | l2->vreg[VREG_HFPLL_A].max_vdd, false); |
| 1147 | BUG_ON(rc); |
| 1148 | rc = rpm_regulator_init(l2, VREG_HFPLL_B, |
| 1149 | l2->vreg[VREG_HFPLL_B].max_vdd, false); |
| 1150 | BUG_ON(rc); |
Matt Wagantall | 9c8cb6e | 2012-07-13 19:39:15 -0700 | [diff] [blame] | 1151 | |
| 1152 | l2_level = find_cur_l2_level(); |
Matt Wagantall | b7c231b | 2012-07-24 18:40:17 -0700 | [diff] [blame] | 1153 | if (!l2_level) { |
Matt Wagantall | 9c8cb6e | 2012-07-13 19:39:15 -0700 | [diff] [blame] | 1154 | l2_level = drv.l2_freq_tbl; |
Matt Wagantall | b7c231b | 2012-07-24 18:40:17 -0700 | [diff] [blame] | 1155 | dev_dbg(drv.dev, "L2 is running at an unknown rate. Defaulting to %lu KHz.\n", |
| 1156 | l2_level->speed.khz); |
Matt Wagantall | 9c8cb6e | 2012-07-13 19:39:15 -0700 | [diff] [blame] | 1157 | } else { |
| 1158 | dev_dbg(drv.dev, "L2 is running at %lu KHz\n", |
| 1159 | l2_level->speed.khz); |
| 1160 | } |
| 1161 | |
| 1162 | rc = init_clock_sources(l2, &l2_level->speed); |
Matt Wagantall | 302d9a3 | 2012-07-03 13:37:29 -0700 | [diff] [blame] | 1163 | BUG_ON(rc); |
| 1164 | |
| 1165 | for_each_online_cpu(cpu) { |
| 1166 | rc = per_cpu_init(cpu); |
| 1167 | BUG_ON(rc); |
| 1168 | } |
Matt Wagantall | 9c8cb6e | 2012-07-13 19:39:15 -0700 | [diff] [blame] | 1169 | |
| 1170 | bus_init(l2_level); |
Matt Wagantall | 1f3762d | 2012-06-08 19:08:48 -0700 | [diff] [blame] | 1171 | } |
| 1172 | |
| 1173 | int __init acpuclk_krait_init(struct device *dev, |
| 1174 | const struct acpuclk_krait_params *params) |
| 1175 | { |
| 1176 | drv_data_init(dev, params); |
| 1177 | hw_init(); |
Matt Wagantall | e9b715a | 2012-01-04 18:16:14 -0800 | [diff] [blame] | 1178 | |
| 1179 | cpufreq_table_init(); |
Steve Muckle | a9aac29 | 2012-11-02 15:41:00 -0700 | [diff] [blame] | 1180 | dcvs_freq_init(); |
Matt Wagantall | e9b715a | 2012-01-04 18:16:14 -0800 | [diff] [blame] | 1181 | acpuclk_register(&acpuclk_krait_data); |
| 1182 | register_hotcpu_notifier(&acpuclk_cpu_notifier); |
| 1183 | |
| 1184 | return 0; |
| 1185 | } |