Saravana Kannan | c85ecf9 | 2013-01-21 17:58:35 -0800 | [diff] [blame] | 1 | /* Copyright (c) 2009-2013, The Linux Foundation. All rights reserved. |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 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/msm_iomap.h> |
Matt Wagantall | 33d01f5 | 2012-02-23 23:27:44 -0800 | [diff] [blame] | 27 | #include <mach/clk-provider.h> |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 28 | #include <mach/clk.h> |
| 29 | #include <mach/scm-io.h> |
| 30 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 31 | #include "clock-local.h" |
| 32 | |
| 33 | #ifdef CONFIG_MSM_SECURE_IO |
| 34 | #undef readl_relaxed |
| 35 | #undef writel_relaxed |
| 36 | #define readl_relaxed secure_readl |
| 37 | #define writel_relaxed secure_writel |
| 38 | #endif |
| 39 | |
| 40 | /* |
| 41 | * When enabling/disabling a clock, check the halt bit up to this number |
| 42 | * number of times (with a 1 us delay in between) before continuing. |
| 43 | */ |
Stephen Boyd | 138da0e | 2011-08-05 13:25:57 -0700 | [diff] [blame] | 44 | #define HALT_CHECK_MAX_LOOPS 200 |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 45 | /* For clock without halt checking, wait this long after enables/disables. */ |
| 46 | #define HALT_CHECK_DELAY_US 10 |
| 47 | |
| 48 | DEFINE_SPINLOCK(local_clock_reg_lock); |
Matt Wagantall | 84f43fd | 2011-08-16 23:28:38 -0700 | [diff] [blame] | 49 | struct clk_freq_tbl rcg_dummy_freq = F_END; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 50 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 51 | /* |
| 52 | * Common Set-Rate Functions |
| 53 | */ |
| 54 | |
| 55 | /* For clocks with MND dividers. */ |
Matt Wagantall | f82f294 | 2012-01-27 13:56:13 -0800 | [diff] [blame] | 56 | void set_rate_mnd(struct rcg_clk *rcg, struct clk_freq_tbl *nf) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 57 | { |
| 58 | uint32_t ns_reg_val, ctl_reg_val; |
| 59 | |
| 60 | /* Assert MND reset. */ |
Matt Wagantall | f82f294 | 2012-01-27 13:56:13 -0800 | [diff] [blame] | 61 | ns_reg_val = readl_relaxed(rcg->ns_reg); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 62 | ns_reg_val |= BIT(7); |
Matt Wagantall | f82f294 | 2012-01-27 13:56:13 -0800 | [diff] [blame] | 63 | writel_relaxed(ns_reg_val, rcg->ns_reg); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 64 | |
| 65 | /* Program M and D values. */ |
Matt Wagantall | f82f294 | 2012-01-27 13:56:13 -0800 | [diff] [blame] | 66 | writel_relaxed(nf->md_val, rcg->md_reg); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 67 | |
| 68 | /* If the clock has a separate CC register, program it. */ |
Matt Wagantall | f82f294 | 2012-01-27 13:56:13 -0800 | [diff] [blame] | 69 | if (rcg->ns_reg != rcg->b.ctl_reg) { |
| 70 | ctl_reg_val = readl_relaxed(rcg->b.ctl_reg); |
| 71 | ctl_reg_val &= ~(rcg->ctl_mask); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 72 | ctl_reg_val |= nf->ctl_val; |
Matt Wagantall | f82f294 | 2012-01-27 13:56:13 -0800 | [diff] [blame] | 73 | writel_relaxed(ctl_reg_val, rcg->b.ctl_reg); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | /* Deassert MND reset. */ |
| 77 | ns_reg_val &= ~BIT(7); |
Matt Wagantall | f82f294 | 2012-01-27 13:56:13 -0800 | [diff] [blame] | 78 | writel_relaxed(ns_reg_val, rcg->ns_reg); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 79 | } |
| 80 | |
Matt Wagantall | f82f294 | 2012-01-27 13:56:13 -0800 | [diff] [blame] | 81 | void set_rate_nop(struct rcg_clk *rcg, struct clk_freq_tbl *nf) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 82 | { |
| 83 | /* |
| 84 | * Nothing to do for fixed-rate or integer-divider clocks. Any settings |
| 85 | * in NS registers are applied in the enable path, since power can be |
| 86 | * saved by leaving an un-clocked or slowly-clocked source selected |
| 87 | * until the clock is enabled. |
| 88 | */ |
| 89 | } |
| 90 | |
Matt Wagantall | f82f294 | 2012-01-27 13:56:13 -0800 | [diff] [blame] | 91 | void set_rate_mnd_8(struct rcg_clk *rcg, struct clk_freq_tbl *nf) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 92 | { |
| 93 | uint32_t ctl_reg_val; |
| 94 | |
| 95 | /* Assert MND reset. */ |
Matt Wagantall | f82f294 | 2012-01-27 13:56:13 -0800 | [diff] [blame] | 96 | ctl_reg_val = readl_relaxed(rcg->b.ctl_reg); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 97 | ctl_reg_val |= BIT(8); |
Matt Wagantall | f82f294 | 2012-01-27 13:56:13 -0800 | [diff] [blame] | 98 | writel_relaxed(ctl_reg_val, rcg->b.ctl_reg); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 99 | |
| 100 | /* Program M and D values. */ |
Matt Wagantall | f82f294 | 2012-01-27 13:56:13 -0800 | [diff] [blame] | 101 | writel_relaxed(nf->md_val, rcg->md_reg); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 102 | |
| 103 | /* Program MN counter Enable and Mode. */ |
Matt Wagantall | f82f294 | 2012-01-27 13:56:13 -0800 | [diff] [blame] | 104 | ctl_reg_val &= ~(rcg->ctl_mask); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 105 | ctl_reg_val |= nf->ctl_val; |
Matt Wagantall | f82f294 | 2012-01-27 13:56:13 -0800 | [diff] [blame] | 106 | writel_relaxed(ctl_reg_val, rcg->b.ctl_reg); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 107 | |
| 108 | /* Deassert MND reset. */ |
| 109 | ctl_reg_val &= ~BIT(8); |
Matt Wagantall | f82f294 | 2012-01-27 13:56:13 -0800 | [diff] [blame] | 110 | writel_relaxed(ctl_reg_val, rcg->b.ctl_reg); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 111 | } |
| 112 | |
Matt Wagantall | f82f294 | 2012-01-27 13:56:13 -0800 | [diff] [blame] | 113 | void set_rate_mnd_banked(struct rcg_clk *rcg, struct clk_freq_tbl *nf) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 114 | { |
Matt Wagantall | f82f294 | 2012-01-27 13:56:13 -0800 | [diff] [blame] | 115 | struct bank_masks *banks = rcg->bank_info; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 116 | const struct bank_mask_info *new_bank_masks; |
| 117 | const struct bank_mask_info *old_bank_masks; |
| 118 | uint32_t ns_reg_val, ctl_reg_val; |
| 119 | uint32_t bank_sel; |
| 120 | |
| 121 | /* |
| 122 | * Determine active bank and program the other one. If the clock is |
| 123 | * off, program the active bank since bank switching won't work if |
| 124 | * both banks aren't running. |
| 125 | */ |
Matt Wagantall | f82f294 | 2012-01-27 13:56:13 -0800 | [diff] [blame] | 126 | ctl_reg_val = readl_relaxed(rcg->b.ctl_reg); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 127 | bank_sel = !!(ctl_reg_val & banks->bank_sel_mask); |
| 128 | /* If clock isn't running, don't switch banks. */ |
Matt Wagantall | f82f294 | 2012-01-27 13:56:13 -0800 | [diff] [blame] | 129 | bank_sel ^= (!rcg->enabled || rcg->current_freq->freq_hz == 0); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 130 | if (bank_sel == 0) { |
| 131 | new_bank_masks = &banks->bank1_mask; |
| 132 | old_bank_masks = &banks->bank0_mask; |
| 133 | } else { |
| 134 | new_bank_masks = &banks->bank0_mask; |
| 135 | old_bank_masks = &banks->bank1_mask; |
| 136 | } |
| 137 | |
Matt Wagantall | f82f294 | 2012-01-27 13:56:13 -0800 | [diff] [blame] | 138 | ns_reg_val = readl_relaxed(rcg->ns_reg); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 139 | |
| 140 | /* Assert bank MND reset. */ |
| 141 | ns_reg_val |= new_bank_masks->rst_mask; |
Matt Wagantall | f82f294 | 2012-01-27 13:56:13 -0800 | [diff] [blame] | 142 | writel_relaxed(ns_reg_val, rcg->ns_reg); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 143 | |
| 144 | /* |
| 145 | * Program NS only if the clock is enabled, since the NS will be set |
| 146 | * as part of the enable procedure and should remain with a low-power |
| 147 | * MUX input selected until then. |
| 148 | */ |
Matt Wagantall | f82f294 | 2012-01-27 13:56:13 -0800 | [diff] [blame] | 149 | if (rcg->enabled) { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 150 | ns_reg_val &= ~(new_bank_masks->ns_mask); |
| 151 | ns_reg_val |= (nf->ns_val & new_bank_masks->ns_mask); |
Matt Wagantall | f82f294 | 2012-01-27 13:56:13 -0800 | [diff] [blame] | 152 | writel_relaxed(ns_reg_val, rcg->ns_reg); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | writel_relaxed(nf->md_val, new_bank_masks->md_reg); |
| 156 | |
| 157 | /* Enable counter only if clock is enabled. */ |
Matt Wagantall | f82f294 | 2012-01-27 13:56:13 -0800 | [diff] [blame] | 158 | if (rcg->enabled) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 159 | ctl_reg_val |= new_bank_masks->mnd_en_mask; |
| 160 | else |
| 161 | ctl_reg_val &= ~(new_bank_masks->mnd_en_mask); |
| 162 | |
| 163 | ctl_reg_val &= ~(new_bank_masks->mode_mask); |
| 164 | ctl_reg_val |= (nf->ctl_val & new_bank_masks->mode_mask); |
Matt Wagantall | f82f294 | 2012-01-27 13:56:13 -0800 | [diff] [blame] | 165 | writel_relaxed(ctl_reg_val, rcg->b.ctl_reg); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 166 | |
| 167 | /* Deassert bank MND reset. */ |
| 168 | ns_reg_val &= ~(new_bank_masks->rst_mask); |
Matt Wagantall | f82f294 | 2012-01-27 13:56:13 -0800 | [diff] [blame] | 169 | writel_relaxed(ns_reg_val, rcg->ns_reg); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 170 | |
| 171 | /* |
| 172 | * Switch to the new bank if clock is running. If it isn't, then |
| 173 | * no switch is necessary since we programmed the active bank. |
| 174 | */ |
Matt Wagantall | f82f294 | 2012-01-27 13:56:13 -0800 | [diff] [blame] | 175 | if (rcg->enabled && rcg->current_freq->freq_hz) { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 176 | ctl_reg_val ^= banks->bank_sel_mask; |
Matt Wagantall | f82f294 | 2012-01-27 13:56:13 -0800 | [diff] [blame] | 177 | writel_relaxed(ctl_reg_val, rcg->b.ctl_reg); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 178 | /* |
| 179 | * Wait at least 6 cycles of slowest bank's clock |
| 180 | * for the glitch-free MUX to fully switch sources. |
| 181 | */ |
| 182 | mb(); |
| 183 | udelay(1); |
| 184 | |
| 185 | /* Disable old bank's MN counter. */ |
| 186 | ctl_reg_val &= ~(old_bank_masks->mnd_en_mask); |
Matt Wagantall | f82f294 | 2012-01-27 13:56:13 -0800 | [diff] [blame] | 187 | writel_relaxed(ctl_reg_val, rcg->b.ctl_reg); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 188 | |
| 189 | /* Program old bank to a low-power source and divider. */ |
| 190 | ns_reg_val &= ~(old_bank_masks->ns_mask); |
Matt Wagantall | f82f294 | 2012-01-27 13:56:13 -0800 | [diff] [blame] | 191 | ns_reg_val |= (rcg->freq_tbl->ns_val & old_bank_masks->ns_mask); |
| 192 | writel_relaxed(ns_reg_val, rcg->ns_reg); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 193 | } |
| 194 | |
Matt Wagantall | 07c4547 | 2012-02-10 23:27:24 -0800 | [diff] [blame] | 195 | /* Update the MND_EN and NS masks to match the current bank. */ |
Matt Wagantall | f82f294 | 2012-01-27 13:56:13 -0800 | [diff] [blame] | 196 | rcg->mnd_en_mask = new_bank_masks->mnd_en_mask; |
| 197 | rcg->ns_mask = new_bank_masks->ns_mask; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 198 | } |
| 199 | |
Matt Wagantall | f82f294 | 2012-01-27 13:56:13 -0800 | [diff] [blame] | 200 | void set_rate_div_banked(struct rcg_clk *rcg, struct clk_freq_tbl *nf) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 201 | { |
Matt Wagantall | f82f294 | 2012-01-27 13:56:13 -0800 | [diff] [blame] | 202 | struct bank_masks *banks = rcg->bank_info; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 203 | const struct bank_mask_info *new_bank_masks; |
| 204 | const struct bank_mask_info *old_bank_masks; |
| 205 | uint32_t ns_reg_val, bank_sel; |
| 206 | |
| 207 | /* |
| 208 | * Determine active bank and program the other one. If the clock is |
| 209 | * off, program the active bank since bank switching won't work if |
| 210 | * both banks aren't running. |
| 211 | */ |
Matt Wagantall | f82f294 | 2012-01-27 13:56:13 -0800 | [diff] [blame] | 212 | ns_reg_val = readl_relaxed(rcg->ns_reg); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 213 | bank_sel = !!(ns_reg_val & banks->bank_sel_mask); |
| 214 | /* If clock isn't running, don't switch banks. */ |
Matt Wagantall | f82f294 | 2012-01-27 13:56:13 -0800 | [diff] [blame] | 215 | bank_sel ^= (!rcg->enabled || rcg->current_freq->freq_hz == 0); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 216 | if (bank_sel == 0) { |
| 217 | new_bank_masks = &banks->bank1_mask; |
| 218 | old_bank_masks = &banks->bank0_mask; |
| 219 | } else { |
| 220 | new_bank_masks = &banks->bank0_mask; |
| 221 | old_bank_masks = &banks->bank1_mask; |
| 222 | } |
| 223 | |
| 224 | /* |
| 225 | * Program NS only if the clock is enabled, since the NS will be set |
| 226 | * as part of the enable procedure and should remain with a low-power |
| 227 | * MUX input selected until then. |
| 228 | */ |
Matt Wagantall | f82f294 | 2012-01-27 13:56:13 -0800 | [diff] [blame] | 229 | if (rcg->enabled) { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 230 | ns_reg_val &= ~(new_bank_masks->ns_mask); |
| 231 | ns_reg_val |= (nf->ns_val & new_bank_masks->ns_mask); |
Matt Wagantall | f82f294 | 2012-01-27 13:56:13 -0800 | [diff] [blame] | 232 | writel_relaxed(ns_reg_val, rcg->ns_reg); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | /* |
| 236 | * Switch to the new bank if clock is running. If it isn't, then |
| 237 | * no switch is necessary since we programmed the active bank. |
| 238 | */ |
Matt Wagantall | f82f294 | 2012-01-27 13:56:13 -0800 | [diff] [blame] | 239 | if (rcg->enabled && rcg->current_freq->freq_hz) { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 240 | ns_reg_val ^= banks->bank_sel_mask; |
Matt Wagantall | f82f294 | 2012-01-27 13:56:13 -0800 | [diff] [blame] | 241 | writel_relaxed(ns_reg_val, rcg->ns_reg); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 242 | /* |
| 243 | * Wait at least 6 cycles of slowest bank's clock |
| 244 | * for the glitch-free MUX to fully switch sources. |
| 245 | */ |
| 246 | mb(); |
| 247 | udelay(1); |
| 248 | |
| 249 | /* Program old bank to a low-power source and divider. */ |
| 250 | ns_reg_val &= ~(old_bank_masks->ns_mask); |
Matt Wagantall | f82f294 | 2012-01-27 13:56:13 -0800 | [diff] [blame] | 251 | ns_reg_val |= (rcg->freq_tbl->ns_val & old_bank_masks->ns_mask); |
| 252 | writel_relaxed(ns_reg_val, rcg->ns_reg); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | /* Update the NS mask to match the current bank. */ |
Matt Wagantall | f82f294 | 2012-01-27 13:56:13 -0800 | [diff] [blame] | 256 | rcg->ns_mask = new_bank_masks->ns_mask; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 257 | } |
| 258 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 259 | /* |
| 260 | * Clock enable/disable functions |
| 261 | */ |
| 262 | |
| 263 | /* Return non-zero if a clock status registers shows the clock is halted. */ |
Matt Wagantall | f82f294 | 2012-01-27 13:56:13 -0800 | [diff] [blame] | 264 | static int branch_clk_is_halted(const struct branch *b) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 265 | { |
Matt Wagantall | f82f294 | 2012-01-27 13:56:13 -0800 | [diff] [blame] | 266 | int invert = (b->halt_check == ENABLE); |
| 267 | int status_bit = readl_relaxed(b->halt_reg) & BIT(b->halt_bit); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 268 | return invert ? !status_bit : status_bit; |
| 269 | } |
| 270 | |
Stephen Boyd | 409b8b4 | 2012-04-10 12:12:56 -0700 | [diff] [blame] | 271 | static int branch_in_hwcg_mode(const struct branch *b) |
Stephen Boyd | a52d7e3 | 2011-11-10 11:59:00 -0800 | [diff] [blame] | 272 | { |
| 273 | if (!b->hwcg_mask) |
| 274 | return 0; |
| 275 | |
| 276 | return !!(readl_relaxed(b->hwcg_reg) & b->hwcg_mask); |
| 277 | } |
| 278 | |
Matt Wagantall | 0de1b3f | 2012-06-05 19:52:43 -0700 | [diff] [blame] | 279 | void __branch_enable_reg(const struct branch *b, const char *name) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 280 | { |
| 281 | u32 reg_val; |
| 282 | |
Matt Wagantall | f82f294 | 2012-01-27 13:56:13 -0800 | [diff] [blame] | 283 | if (b->en_mask) { |
| 284 | reg_val = readl_relaxed(b->ctl_reg); |
| 285 | reg_val |= b->en_mask; |
| 286 | writel_relaxed(reg_val, b->ctl_reg); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 287 | } |
| 288 | |
| 289 | /* |
| 290 | * Use a memory barrier since some halt status registers are |
| 291 | * not within the same 1K segment as the branch/root enable |
| 292 | * registers. It's also needed in the udelay() case to ensure |
| 293 | * the delay starts after the branch enable. |
| 294 | */ |
| 295 | mb(); |
| 296 | |
Stephen Boyd | a52d7e3 | 2011-11-10 11:59:00 -0800 | [diff] [blame] | 297 | /* Skip checking halt bit if the clock is in hardware gated mode */ |
Matt Wagantall | f82f294 | 2012-01-27 13:56:13 -0800 | [diff] [blame] | 298 | if (branch_in_hwcg_mode(b)) |
Stephen Boyd | a52d7e3 | 2011-11-10 11:59:00 -0800 | [diff] [blame] | 299 | return; |
| 300 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 301 | /* Wait for clock to enable before returning. */ |
Matt Wagantall | f82f294 | 2012-01-27 13:56:13 -0800 | [diff] [blame] | 302 | if (b->halt_check == DELAY) { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 303 | udelay(HALT_CHECK_DELAY_US); |
Matt Wagantall | f82f294 | 2012-01-27 13:56:13 -0800 | [diff] [blame] | 304 | } else if (b->halt_check == ENABLE || b->halt_check == HALT |
| 305 | || b->halt_check == ENABLE_VOTED |
| 306 | || b->halt_check == HALT_VOTED) { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 307 | int count; |
| 308 | |
| 309 | /* Wait up to HALT_CHECK_MAX_LOOPS for clock to enable. */ |
Matt Wagantall | f82f294 | 2012-01-27 13:56:13 -0800 | [diff] [blame] | 310 | for (count = HALT_CHECK_MAX_LOOPS; branch_clk_is_halted(b) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 311 | && count > 0; count--) |
| 312 | udelay(1); |
| 313 | WARN(count == 0, "%s status stuck at 'off'", name); |
| 314 | } |
| 315 | } |
| 316 | |
| 317 | /* Perform any register operations required to enable the clock. */ |
Matt Wagantall | f82f294 | 2012-01-27 13:56:13 -0800 | [diff] [blame] | 318 | static void __rcg_clk_enable_reg(struct rcg_clk *rcg) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 319 | { |
| 320 | u32 reg_val; |
Matt Wagantall | f82f294 | 2012-01-27 13:56:13 -0800 | [diff] [blame] | 321 | void __iomem *const reg = rcg->b.ctl_reg; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 322 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 323 | /* |
| 324 | * Program the NS register, if applicable. NS registers are not |
| 325 | * set in the set_rate path because power can be saved by deferring |
| 326 | * the selection of a clocked source until the clock is enabled. |
| 327 | */ |
Matt Wagantall | f82f294 | 2012-01-27 13:56:13 -0800 | [diff] [blame] | 328 | if (rcg->ns_mask) { |
| 329 | reg_val = readl_relaxed(rcg->ns_reg); |
| 330 | reg_val &= ~(rcg->ns_mask); |
| 331 | reg_val |= (rcg->current_freq->ns_val & rcg->ns_mask); |
| 332 | writel_relaxed(reg_val, rcg->ns_reg); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 333 | } |
| 334 | |
| 335 | /* Enable MN counter, if applicable. */ |
| 336 | reg_val = readl_relaxed(reg); |
Matt Wagantall | f82f294 | 2012-01-27 13:56:13 -0800 | [diff] [blame] | 337 | if (rcg->current_freq->md_val) { |
| 338 | reg_val |= rcg->mnd_en_mask; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 339 | writel_relaxed(reg_val, reg); |
| 340 | } |
| 341 | /* Enable root. */ |
Matt Wagantall | f82f294 | 2012-01-27 13:56:13 -0800 | [diff] [blame] | 342 | if (rcg->root_en_mask) { |
| 343 | reg_val |= rcg->root_en_mask; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 344 | writel_relaxed(reg_val, reg); |
| 345 | } |
Matt Wagantall | 0de1b3f | 2012-06-05 19:52:43 -0700 | [diff] [blame] | 346 | __branch_enable_reg(&rcg->b, rcg->c.dbg_name); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 347 | } |
| 348 | |
| 349 | /* Perform any register operations required to disable the branch. */ |
Matt Wagantall | 0de1b3f | 2012-06-05 19:52:43 -0700 | [diff] [blame] | 350 | u32 __branch_disable_reg(const struct branch *b, const char *name) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 351 | { |
| 352 | u32 reg_val; |
| 353 | |
Matt Wagantall | e3508bb | 2012-07-23 17:18:37 -0700 | [diff] [blame] | 354 | reg_val = b->ctl_reg ? readl_relaxed(b->ctl_reg) : 0; |
Stephen Boyd | a548ca0 | 2012-10-10 10:50:11 -0700 | [diff] [blame] | 355 | if (b->ctl_reg && b->en_mask) { |
Matt Wagantall | f82f294 | 2012-01-27 13:56:13 -0800 | [diff] [blame] | 356 | reg_val &= ~(b->en_mask); |
| 357 | writel_relaxed(reg_val, b->ctl_reg); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 358 | } |
| 359 | |
| 360 | /* |
| 361 | * Use a memory barrier since some halt status registers are |
| 362 | * not within the same K segment as the branch/root enable |
| 363 | * registers. It's also needed in the udelay() case to ensure |
| 364 | * the delay starts after the branch disable. |
| 365 | */ |
| 366 | mb(); |
| 367 | |
Stephen Boyd | a52d7e3 | 2011-11-10 11:59:00 -0800 | [diff] [blame] | 368 | /* Skip checking halt bit if the clock is in hardware gated mode */ |
Matt Wagantall | f82f294 | 2012-01-27 13:56:13 -0800 | [diff] [blame] | 369 | if (branch_in_hwcg_mode(b)) |
Stephen Boyd | a52d7e3 | 2011-11-10 11:59:00 -0800 | [diff] [blame] | 370 | return reg_val; |
| 371 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 372 | /* Wait for clock to disable before continuing. */ |
Matt Wagantall | f82f294 | 2012-01-27 13:56:13 -0800 | [diff] [blame] | 373 | if (b->halt_check == DELAY || b->halt_check == ENABLE_VOTED |
| 374 | || b->halt_check == HALT_VOTED) { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 375 | udelay(HALT_CHECK_DELAY_US); |
Matt Wagantall | f82f294 | 2012-01-27 13:56:13 -0800 | [diff] [blame] | 376 | } else if (b->halt_check == ENABLE || b->halt_check == HALT) { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 377 | int count; |
| 378 | |
| 379 | /* Wait up to HALT_CHECK_MAX_LOOPS for clock to disable. */ |
Matt Wagantall | f82f294 | 2012-01-27 13:56:13 -0800 | [diff] [blame] | 380 | for (count = HALT_CHECK_MAX_LOOPS; !branch_clk_is_halted(b) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 381 | && count > 0; count--) |
| 382 | udelay(1); |
| 383 | WARN(count == 0, "%s status stuck at 'on'", name); |
| 384 | } |
| 385 | |
| 386 | return reg_val; |
| 387 | } |
| 388 | |
| 389 | /* Perform any register operations required to disable the generator. */ |
Matt Wagantall | f82f294 | 2012-01-27 13:56:13 -0800 | [diff] [blame] | 390 | static void __rcg_clk_disable_reg(struct rcg_clk *rcg) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 391 | { |
Matt Wagantall | f82f294 | 2012-01-27 13:56:13 -0800 | [diff] [blame] | 392 | void __iomem *const reg = rcg->b.ctl_reg; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 393 | uint32_t reg_val; |
| 394 | |
Matt Wagantall | 0de1b3f | 2012-06-05 19:52:43 -0700 | [diff] [blame] | 395 | reg_val = __branch_disable_reg(&rcg->b, rcg->c.dbg_name); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 396 | /* Disable root. */ |
Matt Wagantall | f82f294 | 2012-01-27 13:56:13 -0800 | [diff] [blame] | 397 | if (rcg->root_en_mask) { |
| 398 | reg_val &= ~(rcg->root_en_mask); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 399 | writel_relaxed(reg_val, reg); |
| 400 | } |
| 401 | /* Disable MN counter, if applicable. */ |
Matt Wagantall | f82f294 | 2012-01-27 13:56:13 -0800 | [diff] [blame] | 402 | if (rcg->current_freq->md_val) { |
| 403 | reg_val &= ~(rcg->mnd_en_mask); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 404 | writel_relaxed(reg_val, reg); |
| 405 | } |
| 406 | /* |
| 407 | * Program NS register to low-power value with an un-clocked or |
| 408 | * slowly-clocked source selected. |
| 409 | */ |
Matt Wagantall | f82f294 | 2012-01-27 13:56:13 -0800 | [diff] [blame] | 410 | if (rcg->ns_mask) { |
| 411 | reg_val = readl_relaxed(rcg->ns_reg); |
| 412 | reg_val &= ~(rcg->ns_mask); |
| 413 | reg_val |= (rcg->freq_tbl->ns_val & rcg->ns_mask); |
| 414 | writel_relaxed(reg_val, rcg->ns_reg); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 415 | } |
| 416 | } |
| 417 | |
Stephen Boyd | d86d1f2 | 2012-01-24 17:36:34 -0800 | [diff] [blame] | 418 | static int rcg_clk_prepare(struct clk *c) |
| 419 | { |
| 420 | struct rcg_clk *rcg = to_rcg_clk(c); |
| 421 | |
| 422 | WARN(rcg->current_freq == &rcg_dummy_freq, |
| 423 | "Attempting to prepare %s before setting its rate. " |
| 424 | "Set the rate first!\n", rcg->c.dbg_name); |
| 425 | rcg->prepared = true; |
| 426 | |
| 427 | return 0; |
| 428 | } |
| 429 | |
Matt Wagantall | e18bbc8 | 2011-10-06 10:07:28 -0700 | [diff] [blame] | 430 | /* Enable a rate-settable clock. */ |
Stephen Boyd | 409b8b4 | 2012-04-10 12:12:56 -0700 | [diff] [blame] | 431 | static int rcg_clk_enable(struct clk *c) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 432 | { |
| 433 | unsigned long flags; |
Matt Wagantall | f82f294 | 2012-01-27 13:56:13 -0800 | [diff] [blame] | 434 | struct rcg_clk *rcg = to_rcg_clk(c); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 435 | |
| 436 | spin_lock_irqsave(&local_clock_reg_lock, flags); |
Matt Wagantall | f82f294 | 2012-01-27 13:56:13 -0800 | [diff] [blame] | 437 | __rcg_clk_enable_reg(rcg); |
| 438 | rcg->enabled = true; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 439 | spin_unlock_irqrestore(&local_clock_reg_lock, flags); |
Matt Wagantall | e18bbc8 | 2011-10-06 10:07:28 -0700 | [diff] [blame] | 440 | |
| 441 | return 0; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 442 | } |
| 443 | |
Matt Wagantall | e18bbc8 | 2011-10-06 10:07:28 -0700 | [diff] [blame] | 444 | /* Disable a rate-settable clock. */ |
Stephen Boyd | 409b8b4 | 2012-04-10 12:12:56 -0700 | [diff] [blame] | 445 | static void rcg_clk_disable(struct clk *c) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 446 | { |
| 447 | unsigned long flags; |
Matt Wagantall | f82f294 | 2012-01-27 13:56:13 -0800 | [diff] [blame] | 448 | struct rcg_clk *rcg = to_rcg_clk(c); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 449 | |
| 450 | spin_lock_irqsave(&local_clock_reg_lock, flags); |
Matt Wagantall | f82f294 | 2012-01-27 13:56:13 -0800 | [diff] [blame] | 451 | __rcg_clk_disable_reg(rcg); |
| 452 | rcg->enabled = false; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 453 | spin_unlock_irqrestore(&local_clock_reg_lock, flags); |
| 454 | } |
| 455 | |
Stephen Boyd | d86d1f2 | 2012-01-24 17:36:34 -0800 | [diff] [blame] | 456 | static void rcg_clk_unprepare(struct clk *c) |
| 457 | { |
| 458 | struct rcg_clk *rcg = to_rcg_clk(c); |
| 459 | rcg->prepared = false; |
| 460 | } |
| 461 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 462 | /* |
| 463 | * Frequency-related functions |
| 464 | */ |
| 465 | |
Matt Wagantall | ab1adce | 2012-01-24 14:57:24 -0800 | [diff] [blame] | 466 | /* Set a clock to an exact rate. */ |
Stephen Boyd | 409b8b4 | 2012-04-10 12:12:56 -0700 | [diff] [blame] | 467 | static int rcg_clk_set_rate(struct clk *c, unsigned long rate) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 468 | { |
Matt Wagantall | f82f294 | 2012-01-27 13:56:13 -0800 | [diff] [blame] | 469 | struct rcg_clk *rcg = to_rcg_clk(c); |
Matt Wagantall | ab1adce | 2012-01-24 14:57:24 -0800 | [diff] [blame] | 470 | struct clk_freq_tbl *nf, *cf; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 471 | struct clk *chld; |
Matt Wagantall | ab1adce | 2012-01-24 14:57:24 -0800 | [diff] [blame] | 472 | int rc = 0; |
Stephen Boyd | d86d1f2 | 2012-01-24 17:36:34 -0800 | [diff] [blame] | 473 | unsigned long flags; |
Matt Wagantall | ab1adce | 2012-01-24 14:57:24 -0800 | [diff] [blame] | 474 | |
Matt Wagantall | f82f294 | 2012-01-27 13:56:13 -0800 | [diff] [blame] | 475 | for (nf = rcg->freq_tbl; nf->freq_hz != FREQ_END |
Matt Wagantall | ab1adce | 2012-01-24 14:57:24 -0800 | [diff] [blame] | 476 | && nf->freq_hz != rate; nf++) |
| 477 | ; |
| 478 | |
| 479 | if (nf->freq_hz == FREQ_END) |
| 480 | return -EINVAL; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 481 | |
Matt Wagantall | f82f294 | 2012-01-27 13:56:13 -0800 | [diff] [blame] | 482 | cf = rcg->current_freq; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 483 | |
Stephen Boyd | d86d1f2 | 2012-01-24 17:36:34 -0800 | [diff] [blame] | 484 | /* Enable source clock dependency for the new frequency */ |
| 485 | if (rcg->prepared) { |
| 486 | rc = clk_prepare(nf->src_clk); |
Matt Wagantall | e18bbc8 | 2011-10-06 10:07:28 -0700 | [diff] [blame] | 487 | if (rc) |
| 488 | return rc; |
Stephen Boyd | d86d1f2 | 2012-01-24 17:36:34 -0800 | [diff] [blame] | 489 | |
| 490 | } |
| 491 | |
| 492 | spin_lock_irqsave(&c->lock, flags); |
| 493 | if (rcg->enabled) { |
| 494 | rc = clk_enable(nf->src_clk); |
| 495 | if (rc) { |
| 496 | spin_unlock_irqrestore(&c->lock, flags); |
| 497 | clk_unprepare(nf->src_clk); |
| 498 | return rc; |
| 499 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 500 | } |
| 501 | |
| 502 | spin_lock(&local_clock_reg_lock); |
| 503 | |
| 504 | /* Disable branch if clock isn't dual-banked with a glitch-free MUX. */ |
Matt Wagantall | f82f294 | 2012-01-27 13:56:13 -0800 | [diff] [blame] | 505 | if (!rcg->bank_info) { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 506 | /* Disable all branches to prevent glitches. */ |
Matt Wagantall | f82f294 | 2012-01-27 13:56:13 -0800 | [diff] [blame] | 507 | list_for_each_entry(chld, &rcg->c.children, siblings) { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 508 | struct branch_clk *x = to_branch_clk(chld); |
| 509 | /* |
| 510 | * We don't need to grab the child's lock because |
| 511 | * we hold the local_clock_reg_lock and 'enabled' is |
| 512 | * only modified within lock. |
| 513 | */ |
| 514 | if (x->enabled) |
Matt Wagantall | 0de1b3f | 2012-06-05 19:52:43 -0700 | [diff] [blame] | 515 | __branch_disable_reg(&x->b, x->c.dbg_name); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 516 | } |
Matt Wagantall | f82f294 | 2012-01-27 13:56:13 -0800 | [diff] [blame] | 517 | if (rcg->enabled) |
| 518 | __rcg_clk_disable_reg(rcg); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 519 | } |
| 520 | |
| 521 | /* Perform clock-specific frequency switch operations. */ |
Matt Wagantall | f82f294 | 2012-01-27 13:56:13 -0800 | [diff] [blame] | 522 | BUG_ON(!rcg->set_rate); |
| 523 | rcg->set_rate(rcg, nf); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 524 | |
| 525 | /* |
Matt Wagantall | 0625ea0 | 2011-07-13 18:51:56 -0700 | [diff] [blame] | 526 | * Current freq must be updated before __rcg_clk_enable_reg() |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 527 | * is called to make sure the MNCNTR_EN bit is set correctly. |
| 528 | */ |
Matt Wagantall | f82f294 | 2012-01-27 13:56:13 -0800 | [diff] [blame] | 529 | rcg->current_freq = nf; |
Saravana Kannan | 7a6532e | 2012-10-18 20:51:13 -0700 | [diff] [blame] | 530 | c->parent = nf->src_clk; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 531 | |
| 532 | /* Enable any clocks that were disabled. */ |
Matt Wagantall | f82f294 | 2012-01-27 13:56:13 -0800 | [diff] [blame] | 533 | if (!rcg->bank_info) { |
| 534 | if (rcg->enabled) |
| 535 | __rcg_clk_enable_reg(rcg); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 536 | /* Enable only branches that were ON before. */ |
Matt Wagantall | f82f294 | 2012-01-27 13:56:13 -0800 | [diff] [blame] | 537 | list_for_each_entry(chld, &rcg->c.children, siblings) { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 538 | struct branch_clk *x = to_branch_clk(chld); |
| 539 | if (x->enabled) |
Matt Wagantall | 0de1b3f | 2012-06-05 19:52:43 -0700 | [diff] [blame] | 540 | __branch_enable_reg(&x->b, x->c.dbg_name); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 541 | } |
| 542 | } |
| 543 | |
| 544 | spin_unlock(&local_clock_reg_lock); |
| 545 | |
Matt Wagantall | e18bbc8 | 2011-10-06 10:07:28 -0700 | [diff] [blame] | 546 | /* Release source requirements of the old freq. */ |
Matt Wagantall | f82f294 | 2012-01-27 13:56:13 -0800 | [diff] [blame] | 547 | if (rcg->enabled) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 548 | clk_disable(cf->src_clk); |
Stephen Boyd | d86d1f2 | 2012-01-24 17:36:34 -0800 | [diff] [blame] | 549 | spin_unlock_irqrestore(&c->lock, flags); |
| 550 | |
| 551 | if (rcg->prepared) |
| 552 | clk_unprepare(cf->src_clk); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 553 | |
| 554 | return rc; |
| 555 | } |
| 556 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 557 | /* Check if a clock is currently enabled. */ |
Matt Wagantall | f82f294 | 2012-01-27 13:56:13 -0800 | [diff] [blame] | 558 | static int rcg_clk_is_enabled(struct clk *c) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 559 | { |
Matt Wagantall | f82f294 | 2012-01-27 13:56:13 -0800 | [diff] [blame] | 560 | return to_rcg_clk(c)->enabled; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 561 | } |
| 562 | |
| 563 | /* Return a supported rate that's at least the specified rate. */ |
Stephen Boyd | 409b8b4 | 2012-04-10 12:12:56 -0700 | [diff] [blame] | 564 | static long rcg_clk_round_rate(struct clk *c, unsigned long rate) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 565 | { |
Matt Wagantall | f82f294 | 2012-01-27 13:56:13 -0800 | [diff] [blame] | 566 | struct rcg_clk *rcg = to_rcg_clk(c); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 567 | struct clk_freq_tbl *f; |
| 568 | |
Matt Wagantall | f82f294 | 2012-01-27 13:56:13 -0800 | [diff] [blame] | 569 | for (f = rcg->freq_tbl; f->freq_hz != FREQ_END; f++) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 570 | if (f->freq_hz >= rate) |
| 571 | return f->freq_hz; |
| 572 | |
| 573 | return -EPERM; |
| 574 | } |
| 575 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 576 | /* Return the nth supported frequency for a given clock. */ |
Stephen Boyd | 409b8b4 | 2012-04-10 12:12:56 -0700 | [diff] [blame] | 577 | static int rcg_clk_list_rate(struct clk *c, unsigned n) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 578 | { |
Matt Wagantall | f82f294 | 2012-01-27 13:56:13 -0800 | [diff] [blame] | 579 | struct rcg_clk *rcg = to_rcg_clk(c); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 580 | |
Matt Wagantall | f82f294 | 2012-01-27 13:56:13 -0800 | [diff] [blame] | 581 | if (!rcg->freq_tbl || rcg->freq_tbl->freq_hz == FREQ_END) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 582 | return -ENXIO; |
| 583 | |
Matt Wagantall | f82f294 | 2012-01-27 13:56:13 -0800 | [diff] [blame] | 584 | return (rcg->freq_tbl + n)->freq_hz; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 585 | } |
| 586 | |
Stephen Boyd | a52d7e3 | 2011-11-10 11:59:00 -0800 | [diff] [blame] | 587 | /* Disable hw clock gating if not set at boot */ |
Matt Wagantall | f82f294 | 2012-01-27 13:56:13 -0800 | [diff] [blame] | 588 | enum handoff branch_handoff(struct branch *b, struct clk *c) |
Stephen Boyd | a52d7e3 | 2011-11-10 11:59:00 -0800 | [diff] [blame] | 589 | { |
Matt Wagantall | f82f294 | 2012-01-27 13:56:13 -0800 | [diff] [blame] | 590 | if (!branch_in_hwcg_mode(b)) { |
| 591 | b->hwcg_mask = 0; |
Matt Wagantall | e3508bb | 2012-07-23 17:18:37 -0700 | [diff] [blame] | 592 | if (b->ctl_reg && readl_relaxed(b->ctl_reg) & b->en_mask) |
Matt Wagantall | a15833b | 2012-04-03 11:00:56 -0700 | [diff] [blame] | 593 | return HANDOFF_ENABLED_CLK; |
Stephen Boyd | a52d7e3 | 2011-11-10 11:59:00 -0800 | [diff] [blame] | 594 | } |
Matt Wagantall | a15833b | 2012-04-03 11:00:56 -0700 | [diff] [blame] | 595 | return HANDOFF_DISABLED_CLK; |
Stephen Boyd | a52d7e3 | 2011-11-10 11:59:00 -0800 | [diff] [blame] | 596 | } |
| 597 | |
Stephen Boyd | 409b8b4 | 2012-04-10 12:12:56 -0700 | [diff] [blame] | 598 | static enum handoff branch_clk_handoff(struct clk *c) |
Stephen Boyd | a52d7e3 | 2011-11-10 11:59:00 -0800 | [diff] [blame] | 599 | { |
Matt Wagantall | f82f294 | 2012-01-27 13:56:13 -0800 | [diff] [blame] | 600 | struct branch_clk *br = to_branch_clk(c); |
Saravana Kannan | c85ecf9 | 2013-01-21 17:58:35 -0800 | [diff] [blame] | 601 | if (branch_handoff(&br->b, &br->c) == HANDOFF_ENABLED_CLK) { |
| 602 | br->enabled = true; |
| 603 | return HANDOFF_ENABLED_CLK; |
| 604 | } |
| 605 | |
| 606 | return HANDOFF_DISABLED_CLK; |
Stephen Boyd | a52d7e3 | 2011-11-10 11:59:00 -0800 | [diff] [blame] | 607 | } |
| 608 | |
Saravana Kannan | c85ecf9 | 2013-01-21 17:58:35 -0800 | [diff] [blame] | 609 | static struct clk *rcg_clk_get_parent(struct clk *c) |
Matt Wagantall | 14dc2af | 2011-08-12 13:16:06 -0700 | [diff] [blame] | 610 | { |
Matt Wagantall | f82f294 | 2012-01-27 13:56:13 -0800 | [diff] [blame] | 611 | struct rcg_clk *rcg = to_rcg_clk(c); |
Matt Wagantall | 14dc2af | 2011-08-12 13:16:06 -0700 | [diff] [blame] | 612 | uint32_t ctl_val, ns_val, md_val, ns_mask; |
| 613 | struct clk_freq_tbl *freq; |
Stephen Boyd | a52d7e3 | 2011-11-10 11:59:00 -0800 | [diff] [blame] | 614 | |
Matt Wagantall | f82f294 | 2012-01-27 13:56:13 -0800 | [diff] [blame] | 615 | ctl_val = readl_relaxed(rcg->b.ctl_reg); |
Matt Wagantall | 14dc2af | 2011-08-12 13:16:06 -0700 | [diff] [blame] | 616 | |
Matt Wagantall | f82f294 | 2012-01-27 13:56:13 -0800 | [diff] [blame] | 617 | if (rcg->bank_info) { |
| 618 | const struct bank_masks *bank_masks = rcg->bank_info; |
Matt Wagantall | 14dc2af | 2011-08-12 13:16:06 -0700 | [diff] [blame] | 619 | const struct bank_mask_info *bank_info; |
Stephen Boyd | c78d9a7 | 2011-07-20 00:46:24 -0700 | [diff] [blame] | 620 | if (!(ctl_val & bank_masks->bank_sel_mask)) |
| 621 | bank_info = &bank_masks->bank0_mask; |
Matt Wagantall | 14dc2af | 2011-08-12 13:16:06 -0700 | [diff] [blame] | 622 | else |
Stephen Boyd | c78d9a7 | 2011-07-20 00:46:24 -0700 | [diff] [blame] | 623 | bank_info = &bank_masks->bank1_mask; |
Matt Wagantall | 14dc2af | 2011-08-12 13:16:06 -0700 | [diff] [blame] | 624 | |
| 625 | ns_mask = bank_info->ns_mask; |
Tianyi Gou | e46938b | 2012-01-31 12:30:12 -0800 | [diff] [blame] | 626 | md_val = bank_info->md_reg ? |
| 627 | readl_relaxed(bank_info->md_reg) : 0; |
Matt Wagantall | 14dc2af | 2011-08-12 13:16:06 -0700 | [diff] [blame] | 628 | } else { |
Matt Wagantall | f82f294 | 2012-01-27 13:56:13 -0800 | [diff] [blame] | 629 | ns_mask = rcg->ns_mask; |
| 630 | md_val = rcg->md_reg ? readl_relaxed(rcg->md_reg) : 0; |
Matt Wagantall | 14dc2af | 2011-08-12 13:16:06 -0700 | [diff] [blame] | 631 | } |
Saravana Kannan | c85ecf9 | 2013-01-21 17:58:35 -0800 | [diff] [blame] | 632 | |
Matt Wagantall | a15833b | 2012-04-03 11:00:56 -0700 | [diff] [blame] | 633 | if (!ns_mask) |
Saravana Kannan | c85ecf9 | 2013-01-21 17:58:35 -0800 | [diff] [blame] | 634 | return NULL; |
| 635 | |
Matt Wagantall | f82f294 | 2012-01-27 13:56:13 -0800 | [diff] [blame] | 636 | ns_val = readl_relaxed(rcg->ns_reg) & ns_mask; |
| 637 | for (freq = rcg->freq_tbl; freq->freq_hz != FREQ_END; freq++) { |
Matt Wagantall | 14dc2af | 2011-08-12 13:16:06 -0700 | [diff] [blame] | 638 | if ((freq->ns_val & ns_mask) == ns_val && |
Matt Wagantall | 2a59b21 | 2012-06-12 19:16:01 -0700 | [diff] [blame] | 639 | (!freq->md_val || freq->md_val == md_val)) |
Matt Wagantall | 14dc2af | 2011-08-12 13:16:06 -0700 | [diff] [blame] | 640 | break; |
Matt Wagantall | 14dc2af | 2011-08-12 13:16:06 -0700 | [diff] [blame] | 641 | } |
Saravana Kannan | c85ecf9 | 2013-01-21 17:58:35 -0800 | [diff] [blame] | 642 | |
Matt Wagantall | 14dc2af | 2011-08-12 13:16:06 -0700 | [diff] [blame] | 643 | if (freq->freq_hz == FREQ_END) |
Saravana Kannan | c85ecf9 | 2013-01-21 17:58:35 -0800 | [diff] [blame] | 644 | return NULL; |
Matt Wagantall | 14dc2af | 2011-08-12 13:16:06 -0700 | [diff] [blame] | 645 | |
Saravana Kannan | c85ecf9 | 2013-01-21 17:58:35 -0800 | [diff] [blame] | 646 | /* Cache the results for the handoff code. */ |
Matt Wagantall | f82f294 | 2012-01-27 13:56:13 -0800 | [diff] [blame] | 647 | rcg->current_freq = freq; |
Matt Wagantall | 271a6cd | 2011-09-20 16:06:31 -0700 | [diff] [blame] | 648 | |
Saravana Kannan | c85ecf9 | 2013-01-21 17:58:35 -0800 | [diff] [blame] | 649 | return freq->src_clk; |
| 650 | } |
| 651 | |
| 652 | static enum handoff rcg_clk_handoff(struct clk *c) |
| 653 | { |
| 654 | struct rcg_clk *rcg = to_rcg_clk(c); |
| 655 | enum handoff ret; |
| 656 | |
| 657 | if (rcg->current_freq && rcg->current_freq->freq_hz != FREQ_END) |
| 658 | c->rate = rcg->current_freq->freq_hz; |
| 659 | |
| 660 | ret = branch_handoff(&rcg->b, &rcg->c); |
| 661 | if (ret == HANDOFF_DISABLED_CLK) |
| 662 | return HANDOFF_DISABLED_CLK; |
| 663 | |
| 664 | rcg->prepared = true; |
| 665 | rcg->enabled = true; |
Matt Wagantall | a15833b | 2012-04-03 11:00:56 -0700 | [diff] [blame] | 666 | return HANDOFF_ENABLED_CLK; |
Matt Wagantall | 14dc2af | 2011-08-12 13:16:06 -0700 | [diff] [blame] | 667 | } |
| 668 | |
Matt Wagantall | ae05322 | 2012-05-14 19:42:07 -0700 | [diff] [blame] | 669 | struct clk_ops clk_ops_empty; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 670 | |
| 671 | struct fixed_clk gnd_clk = { |
| 672 | .c = { |
| 673 | .dbg_name = "ground_clk", |
Matt Wagantall | ae05322 | 2012-05-14 19:42:07 -0700 | [diff] [blame] | 674 | .ops = &clk_ops_empty, |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 675 | CLK_INIT(gnd_clk.c), |
| 676 | }, |
| 677 | }; |
| 678 | |
Matt Wagantall | f82f294 | 2012-01-27 13:56:13 -0800 | [diff] [blame] | 679 | static int branch_clk_enable(struct clk *c) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 680 | { |
| 681 | unsigned long flags; |
Matt Wagantall | f82f294 | 2012-01-27 13:56:13 -0800 | [diff] [blame] | 682 | struct branch_clk *br = to_branch_clk(c); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 683 | |
| 684 | spin_lock_irqsave(&local_clock_reg_lock, flags); |
Matt Wagantall | 0de1b3f | 2012-06-05 19:52:43 -0700 | [diff] [blame] | 685 | __branch_enable_reg(&br->b, br->c.dbg_name); |
Matt Wagantall | f82f294 | 2012-01-27 13:56:13 -0800 | [diff] [blame] | 686 | br->enabled = true; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 687 | spin_unlock_irqrestore(&local_clock_reg_lock, flags); |
| 688 | |
| 689 | return 0; |
| 690 | } |
| 691 | |
Matt Wagantall | f82f294 | 2012-01-27 13:56:13 -0800 | [diff] [blame] | 692 | static void branch_clk_disable(struct clk *c) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 693 | { |
| 694 | unsigned long flags; |
Matt Wagantall | f82f294 | 2012-01-27 13:56:13 -0800 | [diff] [blame] | 695 | struct branch_clk *br = to_branch_clk(c); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 696 | |
| 697 | spin_lock_irqsave(&local_clock_reg_lock, flags); |
Matt Wagantall | 0de1b3f | 2012-06-05 19:52:43 -0700 | [diff] [blame] | 698 | __branch_disable_reg(&br->b, br->c.dbg_name); |
Matt Wagantall | f82f294 | 2012-01-27 13:56:13 -0800 | [diff] [blame] | 699 | br->enabled = false; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 700 | spin_unlock_irqrestore(&local_clock_reg_lock, flags); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 701 | } |
| 702 | |
Matt Wagantall | f82f294 | 2012-01-27 13:56:13 -0800 | [diff] [blame] | 703 | static int branch_clk_is_enabled(struct clk *c) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 704 | { |
Matt Wagantall | f82f294 | 2012-01-27 13:56:13 -0800 | [diff] [blame] | 705 | return to_branch_clk(c)->enabled; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 706 | } |
| 707 | |
Stephen Boyd | a52d7e3 | 2011-11-10 11:59:00 -0800 | [diff] [blame] | 708 | static void branch_enable_hwcg(struct branch *b) |
| 709 | { |
| 710 | unsigned long flags; |
| 711 | u32 reg_val; |
| 712 | |
| 713 | spin_lock_irqsave(&local_clock_reg_lock, flags); |
| 714 | reg_val = readl_relaxed(b->hwcg_reg); |
| 715 | reg_val |= b->hwcg_mask; |
| 716 | writel_relaxed(reg_val, b->hwcg_reg); |
| 717 | spin_unlock_irqrestore(&local_clock_reg_lock, flags); |
| 718 | } |
| 719 | |
| 720 | static void branch_disable_hwcg(struct branch *b) |
| 721 | { |
| 722 | unsigned long flags; |
| 723 | u32 reg_val; |
| 724 | |
| 725 | spin_lock_irqsave(&local_clock_reg_lock, flags); |
| 726 | reg_val = readl_relaxed(b->hwcg_reg); |
| 727 | reg_val &= ~b->hwcg_mask; |
| 728 | writel_relaxed(reg_val, b->hwcg_reg); |
| 729 | spin_unlock_irqrestore(&local_clock_reg_lock, flags); |
| 730 | } |
| 731 | |
Matt Wagantall | f82f294 | 2012-01-27 13:56:13 -0800 | [diff] [blame] | 732 | static void branch_clk_enable_hwcg(struct clk *c) |
Stephen Boyd | a52d7e3 | 2011-11-10 11:59:00 -0800 | [diff] [blame] | 733 | { |
Matt Wagantall | f82f294 | 2012-01-27 13:56:13 -0800 | [diff] [blame] | 734 | branch_enable_hwcg(&to_branch_clk(c)->b); |
Stephen Boyd | a52d7e3 | 2011-11-10 11:59:00 -0800 | [diff] [blame] | 735 | } |
| 736 | |
Matt Wagantall | f82f294 | 2012-01-27 13:56:13 -0800 | [diff] [blame] | 737 | static void branch_clk_disable_hwcg(struct clk *c) |
Stephen Boyd | a52d7e3 | 2011-11-10 11:59:00 -0800 | [diff] [blame] | 738 | { |
Matt Wagantall | f82f294 | 2012-01-27 13:56:13 -0800 | [diff] [blame] | 739 | branch_disable_hwcg(&to_branch_clk(c)->b); |
Stephen Boyd | a52d7e3 | 2011-11-10 11:59:00 -0800 | [diff] [blame] | 740 | } |
| 741 | |
Matt Wagantall | 7e0b6c9 | 2012-01-20 18:48:05 -0800 | [diff] [blame] | 742 | static int branch_set_flags(struct branch *b, unsigned flags) |
| 743 | { |
| 744 | unsigned long irq_flags; |
| 745 | u32 reg_val; |
| 746 | int ret = 0; |
| 747 | |
| 748 | if (!b->retain_reg) |
| 749 | return -EPERM; |
| 750 | |
| 751 | spin_lock_irqsave(&local_clock_reg_lock, irq_flags); |
| 752 | reg_val = readl_relaxed(b->retain_reg); |
| 753 | switch (flags) { |
| 754 | case CLKFLAG_RETAIN: |
| 755 | reg_val |= b->retain_mask; |
| 756 | break; |
| 757 | case CLKFLAG_NORETAIN: |
| 758 | reg_val &= ~b->retain_mask; |
| 759 | break; |
| 760 | default: |
| 761 | ret = -EINVAL; |
| 762 | } |
| 763 | writel_relaxed(reg_val, b->retain_reg); |
| 764 | spin_unlock_irqrestore(&local_clock_reg_lock, irq_flags); |
| 765 | |
| 766 | return ret; |
| 767 | } |
| 768 | |
Stephen Boyd | 409b8b4 | 2012-04-10 12:12:56 -0700 | [diff] [blame] | 769 | static int branch_clk_set_flags(struct clk *clk, unsigned flags) |
Matt Wagantall | 7e0b6c9 | 2012-01-20 18:48:05 -0800 | [diff] [blame] | 770 | { |
| 771 | return branch_set_flags(&to_branch_clk(clk)->b, flags); |
| 772 | } |
| 773 | |
Stephen Boyd | 409b8b4 | 2012-04-10 12:12:56 -0700 | [diff] [blame] | 774 | static int branch_clk_in_hwcg_mode(struct clk *c) |
Stephen Boyd | a52d7e3 | 2011-11-10 11:59:00 -0800 | [diff] [blame] | 775 | { |
Matt Wagantall | f82f294 | 2012-01-27 13:56:13 -0800 | [diff] [blame] | 776 | return branch_in_hwcg_mode(&to_branch_clk(c)->b); |
Stephen Boyd | a52d7e3 | 2011-11-10 11:59:00 -0800 | [diff] [blame] | 777 | } |
| 778 | |
Matt Wagantall | f82f294 | 2012-01-27 13:56:13 -0800 | [diff] [blame] | 779 | static void rcg_clk_enable_hwcg(struct clk *c) |
Stephen Boyd | a52d7e3 | 2011-11-10 11:59:00 -0800 | [diff] [blame] | 780 | { |
Matt Wagantall | f82f294 | 2012-01-27 13:56:13 -0800 | [diff] [blame] | 781 | branch_enable_hwcg(&to_rcg_clk(c)->b); |
Stephen Boyd | a52d7e3 | 2011-11-10 11:59:00 -0800 | [diff] [blame] | 782 | } |
| 783 | |
Matt Wagantall | f82f294 | 2012-01-27 13:56:13 -0800 | [diff] [blame] | 784 | static void rcg_clk_disable_hwcg(struct clk *c) |
Stephen Boyd | a52d7e3 | 2011-11-10 11:59:00 -0800 | [diff] [blame] | 785 | { |
Matt Wagantall | f82f294 | 2012-01-27 13:56:13 -0800 | [diff] [blame] | 786 | branch_disable_hwcg(&to_rcg_clk(c)->b); |
Stephen Boyd | a52d7e3 | 2011-11-10 11:59:00 -0800 | [diff] [blame] | 787 | } |
| 788 | |
Stephen Boyd | 409b8b4 | 2012-04-10 12:12:56 -0700 | [diff] [blame] | 789 | static int rcg_clk_in_hwcg_mode(struct clk *c) |
Stephen Boyd | a52d7e3 | 2011-11-10 11:59:00 -0800 | [diff] [blame] | 790 | { |
Matt Wagantall | f82f294 | 2012-01-27 13:56:13 -0800 | [diff] [blame] | 791 | return branch_in_hwcg_mode(&to_rcg_clk(c)->b); |
Stephen Boyd | a52d7e3 | 2011-11-10 11:59:00 -0800 | [diff] [blame] | 792 | } |
| 793 | |
Stephen Boyd | 409b8b4 | 2012-04-10 12:12:56 -0700 | [diff] [blame] | 794 | static int rcg_clk_set_flags(struct clk *clk, unsigned flags) |
Matt Wagantall | 7e0b6c9 | 2012-01-20 18:48:05 -0800 | [diff] [blame] | 795 | { |
| 796 | return branch_set_flags(&to_rcg_clk(clk)->b, flags); |
| 797 | } |
| 798 | |
Stephen Boyd | a52d7e3 | 2011-11-10 11:59:00 -0800 | [diff] [blame] | 799 | int branch_reset(struct branch *b, enum clk_reset_action action) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 800 | { |
| 801 | int ret = 0; |
| 802 | u32 reg_val; |
| 803 | unsigned long flags; |
| 804 | |
Stephen Boyd | a52d7e3 | 2011-11-10 11:59:00 -0800 | [diff] [blame] | 805 | if (!b->reset_reg) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 806 | return -EPERM; |
| 807 | |
Stephen Boyd | a52d7e3 | 2011-11-10 11:59:00 -0800 | [diff] [blame] | 808 | /* Disable hw gating when asserting a reset */ |
| 809 | if (b->hwcg_mask && action == CLK_RESET_ASSERT) |
| 810 | branch_disable_hwcg(b); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 811 | |
Stephen Boyd | a52d7e3 | 2011-11-10 11:59:00 -0800 | [diff] [blame] | 812 | spin_lock_irqsave(&local_clock_reg_lock, flags); |
| 813 | /* Assert/Deassert reset */ |
| 814 | reg_val = readl_relaxed(b->reset_reg); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 815 | switch (action) { |
| 816 | case CLK_RESET_ASSERT: |
Stephen Boyd | a52d7e3 | 2011-11-10 11:59:00 -0800 | [diff] [blame] | 817 | reg_val |= b->reset_mask; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 818 | break; |
| 819 | case CLK_RESET_DEASSERT: |
Stephen Boyd | a52d7e3 | 2011-11-10 11:59:00 -0800 | [diff] [blame] | 820 | reg_val &= ~b->reset_mask; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 821 | break; |
| 822 | default: |
| 823 | ret = -EINVAL; |
| 824 | } |
Stephen Boyd | a52d7e3 | 2011-11-10 11:59:00 -0800 | [diff] [blame] | 825 | writel_relaxed(reg_val, b->reset_reg); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 826 | spin_unlock_irqrestore(&local_clock_reg_lock, flags); |
| 827 | |
Stephen Boyd | a52d7e3 | 2011-11-10 11:59:00 -0800 | [diff] [blame] | 828 | /* Enable hw gating when deasserting a reset */ |
| 829 | if (b->hwcg_mask && action == CLK_RESET_DEASSERT) |
| 830 | branch_enable_hwcg(b); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 831 | /* Make sure write is issued before returning. */ |
| 832 | mb(); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 833 | return ret; |
| 834 | } |
| 835 | |
Matt Wagantall | f82f294 | 2012-01-27 13:56:13 -0800 | [diff] [blame] | 836 | static int branch_clk_reset(struct clk *c, enum clk_reset_action action) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 837 | { |
Matt Wagantall | f82f294 | 2012-01-27 13:56:13 -0800 | [diff] [blame] | 838 | return branch_reset(&to_branch_clk(c)->b, action); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 839 | } |
Stephen Boyd | b8ad822 | 2011-11-28 12:17:58 -0800 | [diff] [blame] | 840 | |
Stephen Boyd | 409b8b4 | 2012-04-10 12:12:56 -0700 | [diff] [blame] | 841 | struct clk_ops clk_ops_branch = { |
| 842 | .enable = branch_clk_enable, |
| 843 | .disable = branch_clk_disable, |
| 844 | .enable_hwcg = branch_clk_enable_hwcg, |
| 845 | .disable_hwcg = branch_clk_disable_hwcg, |
| 846 | .in_hwcg_mode = branch_clk_in_hwcg_mode, |
Stephen Boyd | 409b8b4 | 2012-04-10 12:12:56 -0700 | [diff] [blame] | 847 | .is_enabled = branch_clk_is_enabled, |
| 848 | .reset = branch_clk_reset, |
Stephen Boyd | 409b8b4 | 2012-04-10 12:12:56 -0700 | [diff] [blame] | 849 | .handoff = branch_clk_handoff, |
| 850 | .set_flags = branch_clk_set_flags, |
| 851 | }; |
| 852 | |
Stephen Boyd | 230a674 | 2012-09-21 14:17:11 -0700 | [diff] [blame] | 853 | struct clk_ops clk_ops_smi_2x = { |
| 854 | .prepare = branch_clk_enable, |
| 855 | .unprepare = branch_clk_disable, |
| 856 | .is_enabled = branch_clk_is_enabled, |
Stephen Boyd | 230a674 | 2012-09-21 14:17:11 -0700 | [diff] [blame] | 857 | .handoff = branch_clk_handoff, |
| 858 | }; |
| 859 | |
Stephen Boyd | 409b8b4 | 2012-04-10 12:12:56 -0700 | [diff] [blame] | 860 | struct clk_ops clk_ops_reset = { |
| 861 | .reset = branch_clk_reset, |
| 862 | }; |
| 863 | |
Matt Wagantall | f82f294 | 2012-01-27 13:56:13 -0800 | [diff] [blame] | 864 | static int rcg_clk_reset(struct clk *c, enum clk_reset_action action) |
Stephen Boyd | 7bf2814 | 2011-12-07 00:30:52 -0800 | [diff] [blame] | 865 | { |
Matt Wagantall | f82f294 | 2012-01-27 13:56:13 -0800 | [diff] [blame] | 866 | return branch_reset(&to_rcg_clk(c)->b, action); |
Stephen Boyd | 7bf2814 | 2011-12-07 00:30:52 -0800 | [diff] [blame] | 867 | } |
| 868 | |
Stephen Boyd | 409b8b4 | 2012-04-10 12:12:56 -0700 | [diff] [blame] | 869 | struct clk_ops clk_ops_rcg = { |
Stephen Boyd | d86d1f2 | 2012-01-24 17:36:34 -0800 | [diff] [blame] | 870 | .prepare = rcg_clk_prepare, |
Stephen Boyd | 409b8b4 | 2012-04-10 12:12:56 -0700 | [diff] [blame] | 871 | .enable = rcg_clk_enable, |
| 872 | .disable = rcg_clk_disable, |
Stephen Boyd | d86d1f2 | 2012-01-24 17:36:34 -0800 | [diff] [blame] | 873 | .unprepare = rcg_clk_unprepare, |
Stephen Boyd | 409b8b4 | 2012-04-10 12:12:56 -0700 | [diff] [blame] | 874 | .enable_hwcg = rcg_clk_enable_hwcg, |
| 875 | .disable_hwcg = rcg_clk_disable_hwcg, |
| 876 | .in_hwcg_mode = rcg_clk_in_hwcg_mode, |
Stephen Boyd | 409b8b4 | 2012-04-10 12:12:56 -0700 | [diff] [blame] | 877 | .handoff = rcg_clk_handoff, |
| 878 | .set_rate = rcg_clk_set_rate, |
| 879 | .list_rate = rcg_clk_list_rate, |
| 880 | .is_enabled = rcg_clk_is_enabled, |
| 881 | .round_rate = rcg_clk_round_rate, |
| 882 | .reset = rcg_clk_reset, |
Stephen Boyd | 409b8b4 | 2012-04-10 12:12:56 -0700 | [diff] [blame] | 883 | .set_flags = rcg_clk_set_flags, |
Saravana Kannan | c85ecf9 | 2013-01-21 17:58:35 -0800 | [diff] [blame] | 884 | .get_parent = rcg_clk_get_parent, |
Stephen Boyd | 409b8b4 | 2012-04-10 12:12:56 -0700 | [diff] [blame] | 885 | }; |
| 886 | |
Stephen Boyd | b8ad822 | 2011-11-28 12:17:58 -0800 | [diff] [blame] | 887 | static int cdiv_clk_enable(struct clk *c) |
| 888 | { |
| 889 | unsigned long flags; |
Matt Wagantall | f82f294 | 2012-01-27 13:56:13 -0800 | [diff] [blame] | 890 | struct cdiv_clk *cdiv = to_cdiv_clk(c); |
Stephen Boyd | b8ad822 | 2011-11-28 12:17:58 -0800 | [diff] [blame] | 891 | |
| 892 | spin_lock_irqsave(&local_clock_reg_lock, flags); |
Matt Wagantall | 0de1b3f | 2012-06-05 19:52:43 -0700 | [diff] [blame] | 893 | __branch_enable_reg(&cdiv->b, cdiv->c.dbg_name); |
Stephen Boyd | b8ad822 | 2011-11-28 12:17:58 -0800 | [diff] [blame] | 894 | spin_unlock_irqrestore(&local_clock_reg_lock, flags); |
| 895 | |
| 896 | return 0; |
| 897 | } |
| 898 | |
| 899 | static void cdiv_clk_disable(struct clk *c) |
| 900 | { |
| 901 | unsigned long flags; |
Matt Wagantall | f82f294 | 2012-01-27 13:56:13 -0800 | [diff] [blame] | 902 | struct cdiv_clk *cdiv = to_cdiv_clk(c); |
Stephen Boyd | b8ad822 | 2011-11-28 12:17:58 -0800 | [diff] [blame] | 903 | |
| 904 | spin_lock_irqsave(&local_clock_reg_lock, flags); |
Matt Wagantall | 0de1b3f | 2012-06-05 19:52:43 -0700 | [diff] [blame] | 905 | __branch_disable_reg(&cdiv->b, cdiv->c.dbg_name); |
Stephen Boyd | b8ad822 | 2011-11-28 12:17:58 -0800 | [diff] [blame] | 906 | spin_unlock_irqrestore(&local_clock_reg_lock, flags); |
| 907 | } |
| 908 | |
| 909 | static int cdiv_clk_set_rate(struct clk *c, unsigned long rate) |
| 910 | { |
Matt Wagantall | f82f294 | 2012-01-27 13:56:13 -0800 | [diff] [blame] | 911 | struct cdiv_clk *cdiv = to_cdiv_clk(c); |
Stephen Boyd | b8ad822 | 2011-11-28 12:17:58 -0800 | [diff] [blame] | 912 | u32 reg_val; |
| 913 | |
Matt Wagantall | f82f294 | 2012-01-27 13:56:13 -0800 | [diff] [blame] | 914 | if (rate > cdiv->max_div) |
Stephen Boyd | b8ad822 | 2011-11-28 12:17:58 -0800 | [diff] [blame] | 915 | return -EINVAL; |
Stephen Boyd | b8ad822 | 2011-11-28 12:17:58 -0800 | [diff] [blame] | 916 | |
| 917 | spin_lock(&local_clock_reg_lock); |
Matt Wagantall | f82f294 | 2012-01-27 13:56:13 -0800 | [diff] [blame] | 918 | reg_val = readl_relaxed(cdiv->ns_reg); |
| 919 | reg_val &= ~(cdiv->ext_mask | (cdiv->max_div - 1) << cdiv->div_offset); |
Stephen Boyd | b8ad822 | 2011-11-28 12:17:58 -0800 | [diff] [blame] | 920 | /* Non-zero rates mean set a divider, zero means use external input */ |
| 921 | if (rate) |
Matt Wagantall | f82f294 | 2012-01-27 13:56:13 -0800 | [diff] [blame] | 922 | reg_val |= (rate - 1) << cdiv->div_offset; |
Stephen Boyd | b8ad822 | 2011-11-28 12:17:58 -0800 | [diff] [blame] | 923 | else |
Matt Wagantall | f82f294 | 2012-01-27 13:56:13 -0800 | [diff] [blame] | 924 | reg_val |= cdiv->ext_mask; |
| 925 | writel_relaxed(reg_val, cdiv->ns_reg); |
Stephen Boyd | b8ad822 | 2011-11-28 12:17:58 -0800 | [diff] [blame] | 926 | spin_unlock(&local_clock_reg_lock); |
| 927 | |
Matt Wagantall | f82f294 | 2012-01-27 13:56:13 -0800 | [diff] [blame] | 928 | cdiv->cur_div = rate; |
Stephen Boyd | b8ad822 | 2011-11-28 12:17:58 -0800 | [diff] [blame] | 929 | return 0; |
| 930 | } |
| 931 | |
| 932 | static unsigned long cdiv_clk_get_rate(struct clk *c) |
| 933 | { |
Matt Wagantall | f82f294 | 2012-01-27 13:56:13 -0800 | [diff] [blame] | 934 | return to_cdiv_clk(c)->cur_div; |
Stephen Boyd | b8ad822 | 2011-11-28 12:17:58 -0800 | [diff] [blame] | 935 | } |
| 936 | |
| 937 | static long cdiv_clk_round_rate(struct clk *c, unsigned long rate) |
| 938 | { |
Matt Wagantall | f82f294 | 2012-01-27 13:56:13 -0800 | [diff] [blame] | 939 | return rate > to_cdiv_clk(c)->max_div ? -EPERM : rate; |
Stephen Boyd | b8ad822 | 2011-11-28 12:17:58 -0800 | [diff] [blame] | 940 | } |
| 941 | |
| 942 | static int cdiv_clk_list_rate(struct clk *c, unsigned n) |
| 943 | { |
Matt Wagantall | f82f294 | 2012-01-27 13:56:13 -0800 | [diff] [blame] | 944 | return n > to_cdiv_clk(c)->max_div ? -ENXIO : n; |
Stephen Boyd | b8ad822 | 2011-11-28 12:17:58 -0800 | [diff] [blame] | 945 | } |
| 946 | |
Matt Wagantall | a15833b | 2012-04-03 11:00:56 -0700 | [diff] [blame] | 947 | static enum handoff cdiv_clk_handoff(struct clk *c) |
Stephen Boyd | b8ad822 | 2011-11-28 12:17:58 -0800 | [diff] [blame] | 948 | { |
Matt Wagantall | f82f294 | 2012-01-27 13:56:13 -0800 | [diff] [blame] | 949 | struct cdiv_clk *cdiv = to_cdiv_clk(c); |
Matt Wagantall | a15833b | 2012-04-03 11:00:56 -0700 | [diff] [blame] | 950 | enum handoff ret; |
Stephen Boyd | b8ad822 | 2011-11-28 12:17:58 -0800 | [diff] [blame] | 951 | u32 reg_val; |
| 952 | |
Matt Wagantall | f82f294 | 2012-01-27 13:56:13 -0800 | [diff] [blame] | 953 | ret = branch_handoff(&cdiv->b, &cdiv->c); |
Matt Wagantall | a15833b | 2012-04-03 11:00:56 -0700 | [diff] [blame] | 954 | if (ret == HANDOFF_DISABLED_CLK) |
| 955 | return ret; |
Stephen Boyd | a52d7e3 | 2011-11-10 11:59:00 -0800 | [diff] [blame] | 956 | |
Matt Wagantall | f82f294 | 2012-01-27 13:56:13 -0800 | [diff] [blame] | 957 | reg_val = readl_relaxed(cdiv->ns_reg); |
| 958 | if (reg_val & cdiv->ext_mask) { |
| 959 | cdiv->cur_div = 0; |
Stephen Boyd | b8ad822 | 2011-11-28 12:17:58 -0800 | [diff] [blame] | 960 | } else { |
Matt Wagantall | f82f294 | 2012-01-27 13:56:13 -0800 | [diff] [blame] | 961 | reg_val >>= cdiv->div_offset; |
| 962 | cdiv->cur_div = (reg_val & (cdiv->max_div - 1)) + 1; |
Stephen Boyd | b8ad822 | 2011-11-28 12:17:58 -0800 | [diff] [blame] | 963 | } |
Saravana Kannan | c85ecf9 | 2013-01-21 17:58:35 -0800 | [diff] [blame] | 964 | c->rate = cdiv->cur_div; |
Stephen Boyd | b8ad822 | 2011-11-28 12:17:58 -0800 | [diff] [blame] | 965 | |
Matt Wagantall | a15833b | 2012-04-03 11:00:56 -0700 | [diff] [blame] | 966 | return HANDOFF_ENABLED_CLK; |
Stephen Boyd | b8ad822 | 2011-11-28 12:17:58 -0800 | [diff] [blame] | 967 | } |
| 968 | |
Stephen Boyd | a52d7e3 | 2011-11-10 11:59:00 -0800 | [diff] [blame] | 969 | static void cdiv_clk_enable_hwcg(struct clk *c) |
| 970 | { |
Matt Wagantall | f82f294 | 2012-01-27 13:56:13 -0800 | [diff] [blame] | 971 | branch_enable_hwcg(&to_cdiv_clk(c)->b); |
Stephen Boyd | a52d7e3 | 2011-11-10 11:59:00 -0800 | [diff] [blame] | 972 | } |
| 973 | |
| 974 | static void cdiv_clk_disable_hwcg(struct clk *c) |
| 975 | { |
Matt Wagantall | f82f294 | 2012-01-27 13:56:13 -0800 | [diff] [blame] | 976 | branch_disable_hwcg(&to_cdiv_clk(c)->b); |
Stephen Boyd | a52d7e3 | 2011-11-10 11:59:00 -0800 | [diff] [blame] | 977 | } |
| 978 | |
| 979 | static int cdiv_clk_in_hwcg_mode(struct clk *c) |
| 980 | { |
Matt Wagantall | f82f294 | 2012-01-27 13:56:13 -0800 | [diff] [blame] | 981 | return branch_in_hwcg_mode(&to_cdiv_clk(c)->b); |
Stephen Boyd | a52d7e3 | 2011-11-10 11:59:00 -0800 | [diff] [blame] | 982 | } |
| 983 | |
Stephen Boyd | b8ad822 | 2011-11-28 12:17:58 -0800 | [diff] [blame] | 984 | struct clk_ops clk_ops_cdiv = { |
| 985 | .enable = cdiv_clk_enable, |
| 986 | .disable = cdiv_clk_disable, |
Stephen Boyd | a52d7e3 | 2011-11-10 11:59:00 -0800 | [diff] [blame] | 987 | .in_hwcg_mode = cdiv_clk_in_hwcg_mode, |
| 988 | .enable_hwcg = cdiv_clk_enable_hwcg, |
| 989 | .disable_hwcg = cdiv_clk_disable_hwcg, |
Stephen Boyd | b8ad822 | 2011-11-28 12:17:58 -0800 | [diff] [blame] | 990 | .handoff = cdiv_clk_handoff, |
| 991 | .set_rate = cdiv_clk_set_rate, |
| 992 | .get_rate = cdiv_clk_get_rate, |
| 993 | .list_rate = cdiv_clk_list_rate, |
| 994 | .round_rate = cdiv_clk_round_rate, |
Stephen Boyd | b8ad822 | 2011-11-28 12:17:58 -0800 | [diff] [blame] | 995 | }; |