Vikram Mulukutla | 8810e34 | 2011-10-20 20:26:53 -0700 | [diff] [blame] | 1 | /* Copyright (c) 2012, Code Aurora Forum. All rights reserved. |
| 2 | * |
| 3 | * This program is free software; you can redistribute it and/or modify |
| 4 | * it under the terms of the GNU General Public License version 2 and |
| 5 | * only version 2 as published by the Free Software Foundation. |
| 6 | * |
| 7 | * This program is distributed in the hope that it will be useful, |
| 8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 10 | * GNU General Public License for more details. |
| 11 | * |
| 12 | */ |
| 13 | |
| 14 | #define pr_fmt(fmt) "%s: " fmt, __func__ |
| 15 | |
| 16 | #include <linux/kernel.h> |
| 17 | #include <linux/init.h> |
| 18 | #include <linux/err.h> |
| 19 | #include <linux/ctype.h> |
| 20 | #include <linux/bitops.h> |
| 21 | #include <linux/io.h> |
| 22 | #include <linux/spinlock.h> |
| 23 | #include <linux/delay.h> |
| 24 | #include <linux/clk.h> |
| 25 | |
| 26 | #include <mach/clk.h> |
| 27 | |
| 28 | #include "clock.h" |
| 29 | #include "clock-local2.h" |
| 30 | |
| 31 | /* |
| 32 | * When enabling/disabling a clock, check the halt bit up to this number |
| 33 | * number of times (with a 1 us delay in between) before continuing. |
| 34 | */ |
| 35 | #define HALT_CHECK_MAX_LOOPS 200 |
| 36 | /* For clock without halt checking, wait this long after enables/disables. */ |
| 37 | #define HALT_CHECK_DELAY_US 10 |
| 38 | |
| 39 | /* |
| 40 | * When updating an RCG configuration, check the update bit up to this number |
| 41 | * number of times (with a 1 us delay in between) before continuing. |
| 42 | */ |
| 43 | #define UPDATE_CHECK_MAX_LOOPS 200 |
| 44 | |
| 45 | DEFINE_SPINLOCK(local_clock_reg_lock); |
| 46 | struct clk_freq_tbl rcg_dummy_freq = F_END; |
| 47 | |
| 48 | #define CMD_RCGR_REG(x) (*(x)->base + (x)->cmd_rcgr_reg) |
| 49 | #define CFG_RCGR_REG(x) (*(x)->base + (x)->cmd_rcgr_reg + 0x4) |
| 50 | #define M_REG(x) (*(x)->base + (x)->cmd_rcgr_reg + 0x8) |
| 51 | #define N_REG(x) (*(x)->base + (x)->cmd_rcgr_reg + 0xC) |
| 52 | #define D_REG(x) (*(x)->base + (x)->cmd_rcgr_reg + 0x10) |
| 53 | #define CBCR_REG(x) (*(x)->base + (x)->cbcr_reg) |
| 54 | #define BCR_REG(x) (*(x)->base + (x)->bcr_reg) |
| 55 | #define VOTE_REG(x) (*(x)->base + (x)->vote_reg) |
| 56 | |
| 57 | /* |
| 58 | * Important clock bit positions and masks |
| 59 | */ |
| 60 | #define CMD_RCGR_ROOT_ENABLE_BIT BIT(1) |
| 61 | #define CBCR_BRANCH_ENABLE_BIT BIT(0) |
| 62 | #define CBCR_BRANCH_OFF_BIT BIT(31) |
| 63 | #define CMD_RCGR_CONFIG_UPDATE_BIT BIT(0) |
| 64 | #define CMD_RCGR_ROOT_STATUS_BIT BIT(31) |
| 65 | #define BCR_BLK_ARES_BIT BIT(0) |
| 66 | #define CBCR_HW_CTL_BIT BIT(1) |
| 67 | #define CFG_RCGR_DIV_MASK BM(4, 0) |
| 68 | #define CFG_RCGR_SRC_SEL_MASK BM(10, 8) |
| 69 | #define MND_MODE_MASK BM(13, 12) |
| 70 | #define MND_DUAL_EDGE_MODE_BVAL BVAL(13, 12, 0x2) |
| 71 | #define CMD_RCGR_CONFIG_DIRTY_MASK BM(7, 4) |
| 72 | #define CBCR_BRANCH_CDIV_MASK BM(24, 16) |
| 73 | #define CBCR_BRANCH_CDIV_MASKED(val) BVAL(24, 16, (val)); |
| 74 | |
| 75 | enum branch_state { |
| 76 | BRANCH_ON, |
| 77 | BRANCH_OFF, |
| 78 | }; |
| 79 | |
| 80 | /* |
| 81 | * RCG functions |
| 82 | */ |
| 83 | |
| 84 | /* |
| 85 | * Update an RCG with a new configuration. This may include a new M, N, or D |
| 86 | * value, source selection or pre-divider value. |
| 87 | * |
| 88 | */ |
| 89 | static void rcg_update_config(struct rcg_clk *rcg) |
| 90 | { |
| 91 | u32 cmd_rcgr_regval, count; |
| 92 | |
| 93 | cmd_rcgr_regval = readl_relaxed(CMD_RCGR_REG(rcg)); |
| 94 | cmd_rcgr_regval |= CMD_RCGR_CONFIG_UPDATE_BIT; |
| 95 | writel_relaxed(cmd_rcgr_regval, CMD_RCGR_REG(rcg)); |
| 96 | |
| 97 | /* Wait for update to take effect */ |
| 98 | for (count = UPDATE_CHECK_MAX_LOOPS; count > 0; count--) { |
| 99 | if (!(readl_relaxed(CMD_RCGR_REG(rcg)) & |
| 100 | CMD_RCGR_CONFIG_UPDATE_BIT)) |
| 101 | return; |
| 102 | udelay(1); |
| 103 | } |
| 104 | |
| 105 | WARN(count == 0, "%s: rcg didn't update its configuration.", |
| 106 | rcg->c.dbg_name); |
| 107 | } |
| 108 | |
| 109 | /* RCG set rate function for clocks with Half Integer Dividers. */ |
| 110 | void set_rate_hid(struct rcg_clk *rcg, struct clk_freq_tbl *nf) |
| 111 | { |
| 112 | u32 cfg_regval; |
| 113 | |
| 114 | cfg_regval = readl_relaxed(CFG_RCGR_REG(rcg)); |
| 115 | cfg_regval &= ~(CFG_RCGR_DIV_MASK | CFG_RCGR_SRC_SEL_MASK); |
| 116 | cfg_regval |= nf->div_src_val; |
| 117 | writel_relaxed(cfg_regval, CFG_RCGR_REG(rcg)); |
| 118 | |
| 119 | rcg_update_config(rcg); |
| 120 | } |
| 121 | |
| 122 | /* RCG set rate function for clocks with MND & Half Integer Dividers. */ |
| 123 | void set_rate_mnd(struct rcg_clk *rcg, struct clk_freq_tbl *nf) |
| 124 | { |
| 125 | u32 cfg_regval; |
| 126 | |
| 127 | writel_relaxed(nf->m_val, M_REG(rcg)); |
| 128 | writel_relaxed(nf->n_val, N_REG(rcg)); |
| 129 | writel_relaxed(nf->d_val, D_REG(rcg)); |
| 130 | |
| 131 | cfg_regval = readl_relaxed(CFG_RCGR_REG(rcg)); |
| 132 | cfg_regval &= ~(CFG_RCGR_DIV_MASK | CFG_RCGR_SRC_SEL_MASK); |
| 133 | cfg_regval |= nf->div_src_val; |
| 134 | |
| 135 | /* Activate or disable the M/N:D divider as necessary */ |
| 136 | cfg_regval &= ~MND_MODE_MASK; |
| 137 | if (nf->n_val != 0) |
| 138 | cfg_regval |= MND_DUAL_EDGE_MODE_BVAL; |
| 139 | writel_relaxed(cfg_regval, CFG_RCGR_REG(rcg)); |
| 140 | |
| 141 | rcg_update_config(rcg); |
| 142 | } |
| 143 | |
| 144 | static int rcg_clk_enable(struct clk *c) |
| 145 | { |
| 146 | struct rcg_clk *rcg = to_rcg_clk(c); |
| 147 | |
| 148 | WARN(rcg->current_freq == &rcg_dummy_freq, |
| 149 | "Attempting to enable %s before setting its rate. " |
| 150 | "Set the rate first!\n", rcg->c.dbg_name); |
| 151 | |
| 152 | return 0; |
| 153 | } |
| 154 | |
| 155 | static int rcg_clk_set_rate(struct clk *c, unsigned long rate) |
| 156 | { |
| 157 | struct clk_freq_tbl *cf, *nf; |
| 158 | struct rcg_clk *rcg = to_rcg_clk(c); |
| 159 | int rc = 0; |
| 160 | unsigned long flags; |
| 161 | |
| 162 | for (nf = rcg->freq_tbl; nf->freq_hz != FREQ_END |
| 163 | && nf->freq_hz != rate; nf++) |
| 164 | ; |
| 165 | |
| 166 | if (nf->freq_hz == FREQ_END) |
| 167 | return -EINVAL; |
| 168 | |
Vikram Mulukutla | 8810e34 | 2011-10-20 20:26:53 -0700 | [diff] [blame] | 169 | cf = rcg->current_freq; |
Vikram Mulukutla | 8810e34 | 2011-10-20 20:26:53 -0700 | [diff] [blame] | 170 | |
| 171 | if (rcg->c.count) { |
| 172 | /* TODO: Modify to use the prepare API */ |
| 173 | /* Enable source clock dependency for the new freq. */ |
| 174 | rc = clk_enable(nf->src_clk); |
| 175 | if (rc) |
| 176 | goto out; |
| 177 | } |
| 178 | |
| 179 | BUG_ON(!rcg->set_rate); |
| 180 | |
| 181 | spin_lock_irqsave(&local_clock_reg_lock, flags); |
| 182 | |
| 183 | /* Perform clock-specific frequency switch operations. */ |
| 184 | rcg->set_rate(rcg, nf); |
| 185 | |
| 186 | spin_unlock_irqrestore(&local_clock_reg_lock, flags); |
| 187 | |
| 188 | /* Release source requirements of the old freq. */ |
| 189 | if (rcg->c.count) |
| 190 | clk_disable(cf->src_clk); |
| 191 | |
| 192 | rcg->current_freq = nf; |
| 193 | out: |
| 194 | return rc; |
| 195 | } |
| 196 | |
| 197 | /* Return a supported rate that's at least the specified rate. */ |
| 198 | static long rcg_clk_round_rate(struct clk *c, unsigned long rate) |
| 199 | { |
| 200 | struct rcg_clk *rcg = to_rcg_clk(c); |
| 201 | struct clk_freq_tbl *f; |
| 202 | |
| 203 | for (f = rcg->freq_tbl; f->freq_hz != FREQ_END; f++) |
| 204 | if (f->freq_hz >= rate) |
| 205 | return f->freq_hz; |
| 206 | |
| 207 | return -EPERM; |
| 208 | } |
| 209 | |
| 210 | /* Return the nth supported frequency for a given clock. */ |
| 211 | static int rcg_clk_list_rate(struct clk *c, unsigned n) |
| 212 | { |
| 213 | struct rcg_clk *rcg = to_rcg_clk(c); |
| 214 | |
| 215 | if (!rcg->freq_tbl || rcg->freq_tbl->freq_hz == FREQ_END) |
| 216 | return -ENXIO; |
| 217 | |
| 218 | return (rcg->freq_tbl + n)->freq_hz; |
| 219 | } |
| 220 | |
| 221 | static struct clk *rcg_clk_get_parent(struct clk *c) |
| 222 | { |
| 223 | return to_rcg_clk(c)->current_freq->src_clk; |
| 224 | } |
| 225 | |
| 226 | static enum handoff _rcg_clk_handoff(struct rcg_clk *rcg, int has_mnd) |
| 227 | { |
| 228 | u32 n_regval = 0, m_regval = 0, d_regval = 0; |
| 229 | u32 cfg_regval; |
| 230 | struct clk_freq_tbl *freq; |
| 231 | u32 cmd_rcgr_regval; |
| 232 | |
| 233 | /* Is the root enabled? */ |
| 234 | cmd_rcgr_regval = readl_relaxed(CMD_RCGR_REG(rcg)); |
| 235 | if ((cmd_rcgr_regval & CMD_RCGR_ROOT_STATUS_BIT)) |
| 236 | return HANDOFF_DISABLED_CLK; |
| 237 | |
| 238 | /* Is there a pending configuration? */ |
| 239 | if (cmd_rcgr_regval & CMD_RCGR_CONFIG_DIRTY_MASK) |
| 240 | return HANDOFF_UNKNOWN_RATE; |
| 241 | |
| 242 | /* Get values of m, n, d, div and src_sel registers. */ |
| 243 | if (has_mnd) { |
| 244 | m_regval = readl_relaxed(M_REG(rcg)); |
| 245 | n_regval = readl_relaxed(N_REG(rcg)); |
| 246 | d_regval = readl_relaxed(D_REG(rcg)); |
| 247 | |
| 248 | /* |
| 249 | * The n and d values stored in the frequency tables are sign |
| 250 | * extended to 32 bits. The n and d values in the registers are |
| 251 | * sign extended to 8 or 16 bits. Sign extend the values read |
| 252 | * from the registers so that they can be compared to the |
| 253 | * values in the frequency tables. |
| 254 | */ |
| 255 | n_regval |= (n_regval >> 8) ? BM(31, 16) : BM(31, 8); |
| 256 | d_regval |= (d_regval >> 8) ? BM(31, 16) : BM(31, 8); |
| 257 | } |
| 258 | |
| 259 | cfg_regval = readl_relaxed(CFG_RCGR_REG(rcg)); |
| 260 | cfg_regval &= CFG_RCGR_SRC_SEL_MASK | CFG_RCGR_DIV_MASK |
| 261 | | MND_MODE_MASK; |
| 262 | |
| 263 | /* If mnd counter is present, check if it's in use. */ |
| 264 | has_mnd = (has_mnd) && |
| 265 | ((cfg_regval & MND_MODE_MASK) == MND_DUAL_EDGE_MODE_BVAL); |
| 266 | |
| 267 | /* |
| 268 | * Clear out the mn counter mode bits since we now want to compare only |
| 269 | * the source mux selection and pre-divider values in the registers. |
| 270 | */ |
| 271 | cfg_regval &= ~MND_MODE_MASK; |
| 272 | |
| 273 | /* Figure out what rate the rcg is running at */ |
| 274 | for (freq = rcg->freq_tbl; freq->freq_hz != FREQ_END; freq++) { |
| 275 | if (freq->div_src_val != cfg_regval) |
| 276 | continue; |
| 277 | if (has_mnd) { |
| 278 | if (freq->m_val != m_regval) |
| 279 | continue; |
| 280 | if (freq->n_val != n_regval) |
| 281 | continue; |
| 282 | if (freq->d_val != d_regval) |
| 283 | continue; |
| 284 | } |
Vikram Mulukutla | 8810e34 | 2011-10-20 20:26:53 -0700 | [diff] [blame] | 285 | break; |
| 286 | } |
| 287 | |
| 288 | /* No known frequency found */ |
| 289 | if (freq->freq_hz == FREQ_END) |
| 290 | return HANDOFF_UNKNOWN_RATE; |
| 291 | |
| 292 | rcg->current_freq = freq; |
| 293 | rcg->c.rate = freq->freq_hz; |
| 294 | |
| 295 | return HANDOFF_ENABLED_CLK; |
| 296 | } |
| 297 | |
| 298 | static enum handoff rcg_mnd_clk_handoff(struct clk *c) |
| 299 | { |
| 300 | return _rcg_clk_handoff(to_rcg_clk(c), 1); |
| 301 | } |
| 302 | |
| 303 | static enum handoff rcg_clk_handoff(struct clk *c) |
| 304 | { |
| 305 | return _rcg_clk_handoff(to_rcg_clk(c), 0); |
| 306 | } |
| 307 | |
Vikram Mulukutla | 4be6908 | 2012-06-28 18:40:34 -0700 | [diff] [blame^] | 308 | #define BRANCH_CHECK_MASK BM(31, 28) |
| 309 | #define BRANCH_ON_VAL BVAL(31, 28, 0x0) |
| 310 | #define BRANCH_OFF_VAL BVAL(31, 28, 0x8) |
| 311 | #define BRANCH_NOC_FSM_ON_VAL BVAL(31, 28, 0x2) |
| 312 | |
Vikram Mulukutla | 8810e34 | 2011-10-20 20:26:53 -0700 | [diff] [blame] | 313 | /* |
| 314 | * Branch clock functions |
| 315 | */ |
| 316 | static void branch_clk_halt_check(u32 halt_check, const char *clk_name, |
| 317 | void __iomem *cbcr_reg, |
| 318 | enum branch_state br_status) |
| 319 | { |
Vikram Mulukutla | 86b9fa6 | 2012-05-02 16:39:14 -0700 | [diff] [blame] | 320 | char *status_str = (br_status == BRANCH_ON) ? "off" : "on"; |
Vikram Mulukutla | 8810e34 | 2011-10-20 20:26:53 -0700 | [diff] [blame] | 321 | |
| 322 | /* |
| 323 | * Use a memory barrier since some halt status registers are |
| 324 | * not within the same 1K segment as the branch/root enable |
| 325 | * registers. It's also needed in the udelay() case to ensure |
| 326 | * the delay starts after the branch disable. |
| 327 | */ |
| 328 | mb(); |
| 329 | |
| 330 | if (halt_check == DELAY || halt_check == HALT_VOTED) { |
| 331 | udelay(HALT_CHECK_DELAY_US); |
| 332 | } else if (halt_check == HALT) { |
| 333 | int count; |
Vikram Mulukutla | 4be6908 | 2012-06-28 18:40:34 -0700 | [diff] [blame^] | 334 | u32 val; |
Vikram Mulukutla | 8810e34 | 2011-10-20 20:26:53 -0700 | [diff] [blame] | 335 | for (count = HALT_CHECK_MAX_LOOPS; count > 0; count--) { |
Vikram Mulukutla | 4be6908 | 2012-06-28 18:40:34 -0700 | [diff] [blame^] | 336 | val = readl_relaxed(cbcr_reg); |
| 337 | val &= BRANCH_CHECK_MASK; |
| 338 | switch (br_status) { |
| 339 | case BRANCH_ON: |
| 340 | if (val == BRANCH_ON_VAL |
| 341 | || val == BRANCH_NOC_FSM_ON_VAL) |
| 342 | return; |
| 343 | break; |
| 344 | |
| 345 | case BRANCH_OFF: |
| 346 | if (val == BRANCH_OFF_VAL) |
| 347 | return; |
| 348 | break; |
| 349 | }; |
Vikram Mulukutla | 8810e34 | 2011-10-20 20:26:53 -0700 | [diff] [blame] | 350 | udelay(1); |
| 351 | } |
| 352 | WARN(count == 0, "%s status stuck %s", clk_name, status_str); |
| 353 | } |
| 354 | } |
| 355 | |
| 356 | static int branch_clk_enable(struct clk *c) |
| 357 | { |
| 358 | unsigned long flags; |
| 359 | u32 cbcr_val; |
| 360 | struct branch_clk *branch = to_branch_clk(c); |
| 361 | |
| 362 | spin_lock_irqsave(&local_clock_reg_lock, flags); |
| 363 | cbcr_val = readl_relaxed(CBCR_REG(branch)); |
| 364 | cbcr_val |= CBCR_BRANCH_ENABLE_BIT; |
| 365 | writel_relaxed(cbcr_val, CBCR_REG(branch)); |
| 366 | spin_unlock_irqrestore(&local_clock_reg_lock, flags); |
| 367 | |
| 368 | /* Wait for clock to enable before continuing. */ |
| 369 | branch_clk_halt_check(branch->halt_check, branch->c.dbg_name, |
| 370 | CBCR_REG(branch), BRANCH_ON); |
| 371 | |
| 372 | return 0; |
| 373 | } |
| 374 | |
| 375 | static void branch_clk_disable(struct clk *c) |
| 376 | { |
| 377 | unsigned long flags; |
| 378 | struct branch_clk *branch = to_branch_clk(c); |
| 379 | u32 reg_val; |
| 380 | |
| 381 | spin_lock_irqsave(&local_clock_reg_lock, flags); |
| 382 | reg_val = readl_relaxed(CBCR_REG(branch)); |
| 383 | reg_val &= ~CBCR_BRANCH_ENABLE_BIT; |
| 384 | writel_relaxed(reg_val, CBCR_REG(branch)); |
| 385 | spin_unlock_irqrestore(&local_clock_reg_lock, flags); |
| 386 | |
| 387 | /* Wait for clock to disable before continuing. */ |
| 388 | branch_clk_halt_check(branch->halt_check, branch->c.dbg_name, |
| 389 | CBCR_REG(branch), BRANCH_OFF); |
| 390 | } |
| 391 | |
| 392 | static int branch_cdiv_set_rate(struct branch_clk *branch, unsigned long rate) |
| 393 | { |
| 394 | unsigned long flags; |
| 395 | u32 regval; |
| 396 | |
| 397 | if (rate > branch->max_div) |
| 398 | return -EINVAL; |
| 399 | |
| 400 | spin_lock_irqsave(&local_clock_reg_lock, flags); |
| 401 | regval = readl_relaxed(CBCR_REG(branch)); |
| 402 | regval &= ~CBCR_BRANCH_CDIV_MASK; |
| 403 | regval |= CBCR_BRANCH_CDIV_MASKED(rate); |
| 404 | writel_relaxed(regval, CBCR_REG(branch)); |
| 405 | spin_unlock_irqrestore(&local_clock_reg_lock, flags); |
| 406 | |
| 407 | return 0; |
| 408 | } |
| 409 | |
| 410 | static int branch_clk_set_rate(struct clk *c, unsigned long rate) |
| 411 | { |
| 412 | struct branch_clk *branch = to_branch_clk(c); |
| 413 | |
| 414 | if (branch->max_div) |
| 415 | return branch_cdiv_set_rate(branch, rate); |
| 416 | |
| 417 | if (!branch->has_sibling) |
| 418 | return clk_set_rate(branch->parent, rate); |
| 419 | |
| 420 | return -EPERM; |
| 421 | } |
| 422 | |
Vikram Mulukutla | 5c3252d | 2012-05-25 12:28:54 -0700 | [diff] [blame] | 423 | static long branch_clk_round_rate(struct clk *c, unsigned long rate) |
| 424 | { |
| 425 | struct branch_clk *branch = to_branch_clk(c); |
| 426 | |
| 427 | if (branch->max_div) |
| 428 | return rate <= (branch->max_div) ? rate : -EPERM; |
| 429 | |
| 430 | if (!branch->has_sibling) |
| 431 | return clk_round_rate(branch->parent, rate); |
| 432 | |
| 433 | return -EPERM; |
| 434 | } |
| 435 | |
Vikram Mulukutla | 8810e34 | 2011-10-20 20:26:53 -0700 | [diff] [blame] | 436 | static unsigned long branch_clk_get_rate(struct clk *c) |
| 437 | { |
| 438 | struct branch_clk *branch = to_branch_clk(c); |
| 439 | |
| 440 | if (branch->max_div) |
| 441 | return branch->c.rate; |
| 442 | |
| 443 | if (!branch->has_sibling) |
| 444 | return clk_get_rate(branch->parent); |
| 445 | |
| 446 | return 0; |
| 447 | } |
| 448 | |
| 449 | static struct clk *branch_clk_get_parent(struct clk *c) |
| 450 | { |
| 451 | return to_branch_clk(c)->parent; |
| 452 | } |
| 453 | |
| 454 | static int branch_clk_list_rate(struct clk *c, unsigned n) |
| 455 | { |
| 456 | struct branch_clk *branch = to_branch_clk(c); |
| 457 | |
| 458 | if (branch->has_sibling == 1) |
| 459 | return -ENXIO; |
| 460 | |
| 461 | if (branch->parent) |
| 462 | return rcg_clk_list_rate(branch->parent, n); |
| 463 | else |
| 464 | return 0; |
| 465 | } |
| 466 | |
| 467 | static enum handoff branch_clk_handoff(struct clk *c) |
| 468 | { |
| 469 | struct branch_clk *branch = to_branch_clk(c); |
| 470 | u32 cbcr_regval; |
| 471 | |
| 472 | cbcr_regval = readl_relaxed(CBCR_REG(branch)); |
| 473 | if ((cbcr_regval & CBCR_BRANCH_OFF_BIT)) |
| 474 | return HANDOFF_DISABLED_CLK; |
Vikram Mulukutla | 8810e34 | 2011-10-20 20:26:53 -0700 | [diff] [blame] | 475 | |
| 476 | if (branch->parent) { |
| 477 | if (branch->parent->ops->handoff) |
| 478 | return branch->parent->ops->handoff(branch->parent); |
| 479 | } |
| 480 | |
| 481 | return HANDOFF_ENABLED_CLK; |
| 482 | } |
| 483 | |
| 484 | static int __branch_clk_reset(void __iomem *bcr_reg, |
Vikram Mulukutla | 21c6b91 | 2012-06-14 14:18:45 -0700 | [diff] [blame] | 485 | enum clk_reset_action action) |
Vikram Mulukutla | 8810e34 | 2011-10-20 20:26:53 -0700 | [diff] [blame] | 486 | { |
| 487 | int ret = 0; |
| 488 | unsigned long flags; |
| 489 | u32 reg_val; |
| 490 | |
Vikram Mulukutla | 8810e34 | 2011-10-20 20:26:53 -0700 | [diff] [blame] | 491 | spin_lock_irqsave(&local_clock_reg_lock, flags); |
| 492 | reg_val = readl_relaxed(bcr_reg); |
| 493 | switch (action) { |
| 494 | case CLK_RESET_ASSERT: |
| 495 | reg_val |= BCR_BLK_ARES_BIT; |
| 496 | break; |
| 497 | case CLK_RESET_DEASSERT: |
| 498 | reg_val &= ~BCR_BLK_ARES_BIT; |
| 499 | break; |
| 500 | default: |
| 501 | ret = -EINVAL; |
| 502 | } |
| 503 | writel_relaxed(reg_val, bcr_reg); |
| 504 | spin_unlock_irqrestore(&local_clock_reg_lock, flags); |
| 505 | |
| 506 | /* Make sure write is issued before returning. */ |
| 507 | mb(); |
| 508 | |
| 509 | return ret; |
| 510 | } |
| 511 | |
| 512 | static int branch_clk_reset(struct clk *c, enum clk_reset_action action) |
| 513 | { |
| 514 | struct branch_clk *branch = to_branch_clk(c); |
Vikram Mulukutla | 21c6b91 | 2012-06-14 14:18:45 -0700 | [diff] [blame] | 515 | |
| 516 | if (!branch->bcr_reg) { |
| 517 | WARN("clk_reset called on an unsupported clock (%s)\n", |
| 518 | c->dbg_name); |
| 519 | return -EPERM; |
| 520 | } |
| 521 | return __branch_clk_reset(BCR_REG(branch), action); |
Vikram Mulukutla | 8810e34 | 2011-10-20 20:26:53 -0700 | [diff] [blame] | 522 | } |
| 523 | |
| 524 | /* |
| 525 | * Voteable clock functions |
| 526 | */ |
| 527 | static int local_vote_clk_reset(struct clk *c, enum clk_reset_action action) |
| 528 | { |
Vikram Mulukutla | 27784c0 | 2012-06-06 13:37:36 -0700 | [diff] [blame] | 529 | struct local_vote_clk *vclk = to_local_vote_clk(c); |
Vikram Mulukutla | 21c6b91 | 2012-06-14 14:18:45 -0700 | [diff] [blame] | 530 | |
| 531 | if (!vclk->bcr_reg) { |
| 532 | WARN("clk_reset called on an unsupported clock (%s)\n", |
| 533 | c->dbg_name); |
| 534 | return -EPERM; |
| 535 | } |
| 536 | return __branch_clk_reset(BCR_REG(vclk), action); |
Vikram Mulukutla | 8810e34 | 2011-10-20 20:26:53 -0700 | [diff] [blame] | 537 | } |
| 538 | |
| 539 | static int local_vote_clk_enable(struct clk *c) |
| 540 | { |
| 541 | unsigned long flags; |
| 542 | u32 ena; |
| 543 | struct local_vote_clk *vclk = to_local_vote_clk(c); |
| 544 | |
| 545 | spin_lock_irqsave(&local_clock_reg_lock, flags); |
| 546 | ena = readl_relaxed(VOTE_REG(vclk)); |
| 547 | ena |= vclk->en_mask; |
| 548 | writel_relaxed(ena, VOTE_REG(vclk)); |
| 549 | spin_unlock_irqrestore(&local_clock_reg_lock, flags); |
| 550 | |
| 551 | branch_clk_halt_check(vclk->halt_check, c->dbg_name, CBCR_REG(vclk), |
| 552 | BRANCH_ON); |
| 553 | |
| 554 | return 0; |
| 555 | } |
| 556 | |
| 557 | static void local_vote_clk_disable(struct clk *c) |
| 558 | { |
| 559 | unsigned long flags; |
| 560 | u32 ena; |
| 561 | struct local_vote_clk *vclk = to_local_vote_clk(c); |
| 562 | |
| 563 | spin_lock_irqsave(&local_clock_reg_lock, flags); |
| 564 | ena = readl_relaxed(VOTE_REG(vclk)); |
| 565 | ena &= ~vclk->en_mask; |
| 566 | writel_relaxed(ena, VOTE_REG(vclk)); |
| 567 | spin_unlock_irqrestore(&local_clock_reg_lock, flags); |
| 568 | } |
| 569 | |
| 570 | static enum handoff local_vote_clk_handoff(struct clk *c) |
| 571 | { |
| 572 | struct local_vote_clk *vclk = to_local_vote_clk(c); |
| 573 | u32 vote_regval; |
| 574 | |
| 575 | /* Is the branch voted on by apps? */ |
| 576 | vote_regval = readl_relaxed(VOTE_REG(vclk)); |
| 577 | if (!(vote_regval & vclk->en_mask)) |
| 578 | return HANDOFF_DISABLED_CLK; |
Vikram Mulukutla | 8810e34 | 2011-10-20 20:26:53 -0700 | [diff] [blame] | 579 | |
| 580 | return HANDOFF_ENABLED_CLK; |
| 581 | } |
| 582 | |
| 583 | struct clk_ops clk_ops_rcg = { |
| 584 | .enable = rcg_clk_enable, |
| 585 | .set_rate = rcg_clk_set_rate, |
| 586 | .list_rate = rcg_clk_list_rate, |
| 587 | .round_rate = rcg_clk_round_rate, |
| 588 | .get_parent = rcg_clk_get_parent, |
| 589 | .handoff = rcg_clk_handoff, |
| 590 | }; |
| 591 | |
| 592 | struct clk_ops clk_ops_rcg_mnd = { |
| 593 | .enable = rcg_clk_enable, |
| 594 | .set_rate = rcg_clk_set_rate, |
| 595 | .list_rate = rcg_clk_list_rate, |
| 596 | .round_rate = rcg_clk_round_rate, |
| 597 | .get_parent = rcg_clk_get_parent, |
| 598 | .handoff = rcg_mnd_clk_handoff, |
| 599 | }; |
| 600 | |
| 601 | struct clk_ops clk_ops_branch = { |
| 602 | .enable = branch_clk_enable, |
| 603 | .disable = branch_clk_disable, |
Vikram Mulukutla | 8810e34 | 2011-10-20 20:26:53 -0700 | [diff] [blame] | 604 | .set_rate = branch_clk_set_rate, |
| 605 | .get_rate = branch_clk_get_rate, |
| 606 | .list_rate = branch_clk_list_rate, |
Vikram Mulukutla | 5c3252d | 2012-05-25 12:28:54 -0700 | [diff] [blame] | 607 | .round_rate = branch_clk_round_rate, |
Vikram Mulukutla | 8810e34 | 2011-10-20 20:26:53 -0700 | [diff] [blame] | 608 | .reset = branch_clk_reset, |
| 609 | .get_parent = branch_clk_get_parent, |
| 610 | .handoff = branch_clk_handoff, |
| 611 | }; |
| 612 | |
| 613 | struct clk_ops clk_ops_vote = { |
| 614 | .enable = local_vote_clk_enable, |
| 615 | .disable = local_vote_clk_disable, |
Vikram Mulukutla | 8810e34 | 2011-10-20 20:26:53 -0700 | [diff] [blame] | 616 | .reset = local_vote_clk_reset, |
| 617 | .handoff = local_vote_clk_handoff, |
| 618 | }; |