Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1 | /* Copyright (c) 2009-2011, 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 | #ifndef __ARCH_ARM_MACH_MSM_CLOCK_LOCAL_H |
| 15 | #define __ARCH_ARM_MACH_MSM_CLOCK_LOCAL_H |
| 16 | |
| 17 | #include <linux/spinlock.h> |
| 18 | #include "clock.h" |
| 19 | |
| 20 | /* |
| 21 | * Bit manipulation macros |
| 22 | */ |
| 23 | #define BM(msb, lsb) (((((uint32_t)-1) << (31-msb)) >> (31-msb+lsb)) << lsb) |
| 24 | #define BVAL(msb, lsb, val) (((val) << lsb) & BM(msb, lsb)) |
| 25 | |
| 26 | /* |
| 27 | * Halt/Status Checking Mode Macros |
| 28 | */ |
| 29 | #define HALT 0 /* Bit pol: 1 = halted */ |
| 30 | #define NOCHECK 1 /* No bit to check, do nothing */ |
| 31 | #define HALT_VOTED 2 /* Bit pol: 1 = halted; delay on disable */ |
| 32 | #define ENABLE 3 /* Bit pol: 1 = running */ |
| 33 | #define ENABLE_VOTED 4 /* Bit pol: 1 = running; delay on disable */ |
| 34 | #define DELAY 5 /* No bit to check, just delay */ |
| 35 | |
| 36 | /* |
| 37 | * Clock Definition Macros |
| 38 | */ |
| 39 | #define DEFINE_CLK_MEASURE(name) \ |
| 40 | struct clk name = { \ |
| 41 | .ops = &clk_ops_measure, \ |
| 42 | .dbg_name = #name, \ |
| 43 | CLK_INIT(name), \ |
| 44 | }; \ |
| 45 | |
| 46 | /* |
| 47 | * Generic frequency-definition structs and macros |
| 48 | */ |
| 49 | struct clk_freq_tbl { |
| 50 | const uint32_t freq_hz; |
| 51 | struct clk *src_clk; |
| 52 | const uint32_t md_val; |
| 53 | const uint32_t ns_val; |
| 54 | const uint32_t ctl_val; |
| 55 | uint32_t mnd_en_mask; |
| 56 | const unsigned sys_vdd; |
| 57 | void *const extra_freq_data; |
| 58 | }; |
| 59 | |
| 60 | /* Some clocks have two banks to avoid glitches when switching frequencies. |
| 61 | * The unused bank is programmed while running on the other bank, and |
| 62 | * switched to afterwards. The following two structs describe the banks. */ |
| 63 | struct bank_mask_info { |
| 64 | void *const md_reg; |
| 65 | const uint32_t ns_mask; |
| 66 | const uint32_t rst_mask; |
| 67 | const uint32_t mnd_en_mask; |
| 68 | const uint32_t mode_mask; |
| 69 | }; |
| 70 | |
| 71 | struct bank_masks { |
| 72 | const uint32_t bank_sel_mask; |
| 73 | const struct bank_mask_info bank0_mask; |
| 74 | const struct bank_mask_info bank1_mask; |
| 75 | }; |
| 76 | |
Matt Wagantall | e18bbc8 | 2011-10-06 10:07:28 -0700 | [diff] [blame] | 77 | #define F_RAW(f, sc, m_v, n_v, c_v, m_m, e) { \ |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 78 | .freq_hz = f, \ |
| 79 | .src_clk = sc, \ |
| 80 | .md_val = m_v, \ |
| 81 | .ns_val = n_v, \ |
| 82 | .ctl_val = c_v, \ |
| 83 | .mnd_en_mask = m_m, \ |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 84 | .extra_freq_data = e, \ |
| 85 | } |
| 86 | #define FREQ_END (UINT_MAX-1) |
Matt Wagantall | e18bbc8 | 2011-10-06 10:07:28 -0700 | [diff] [blame] | 87 | #define F_END { .freq_hz = FREQ_END } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 88 | |
| 89 | /** |
| 90 | * struct branch - branch on/off |
| 91 | * @ctl_reg: clock control register |
| 92 | * @en_mask: ORed with @ctl_reg to enable the clock |
| 93 | * @halt_reg: halt register |
| 94 | * @halt_check: type of halt check to perform |
| 95 | * @halt_bit: ANDed with @halt_reg to test for clock halted |
| 96 | * @reset_reg: reset register |
| 97 | * @reset_mask: ORed with @reset_reg to reset the clock domain |
| 98 | */ |
| 99 | struct branch { |
| 100 | void __iomem *const ctl_reg; |
| 101 | const u32 en_mask; |
| 102 | |
| 103 | void __iomem *const halt_reg; |
| 104 | const u16 halt_check; |
| 105 | const u16 halt_bit; |
| 106 | |
| 107 | void __iomem *const reset_reg; |
| 108 | const u32 reset_mask; |
| 109 | }; |
| 110 | |
| 111 | int branch_reset(struct branch *clk, enum clk_reset_action action); |
Stephen Boyd | 092fd18 | 2011-10-21 15:56:30 -0700 | [diff] [blame] | 112 | void __branch_clk_enable_reg(const struct branch *clk, const char *name); |
| 113 | u32 __branch_clk_disable_reg(const struct branch *clk, const char *name); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 114 | |
| 115 | /* |
| 116 | * Generic clock-definition struct and macros |
| 117 | */ |
| 118 | struct rcg_clk { |
| 119 | bool enabled; |
| 120 | void *const ns_reg; |
| 121 | void *const md_reg; |
| 122 | |
| 123 | const uint32_t root_en_mask; |
| 124 | uint32_t ns_mask; |
| 125 | const uint32_t ctl_mask; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 126 | |
Stephen Boyd | c78d9a7 | 2011-07-20 00:46:24 -0700 | [diff] [blame] | 127 | void *bank_info; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 128 | void (*set_rate)(struct rcg_clk *, struct clk_freq_tbl *); |
Stephen Boyd | c78d9a7 | 2011-07-20 00:46:24 -0700 | [diff] [blame] | 129 | |
Tianyi Gou | baf6d34 | 2011-08-30 21:49:02 -0700 | [diff] [blame] | 130 | struct clk_freq_tbl *freq_tbl; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 131 | struct clk_freq_tbl *current_freq; |
| 132 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 133 | struct branch b; |
| 134 | struct clk c; |
| 135 | }; |
| 136 | |
| 137 | static inline struct rcg_clk *to_rcg_clk(struct clk *clk) |
| 138 | { |
| 139 | return container_of(clk, struct rcg_clk, c); |
| 140 | } |
| 141 | |
Matt Wagantall | 84f43fd | 2011-08-16 23:28:38 -0700 | [diff] [blame] | 142 | extern struct clk_freq_tbl rcg_dummy_freq; |
| 143 | |
Matt Wagantall | 0625ea0 | 2011-07-13 18:51:56 -0700 | [diff] [blame] | 144 | int rcg_clk_enable(struct clk *clk); |
| 145 | void rcg_clk_disable(struct clk *clk); |
Matt Wagantall | 9de3bfb | 2011-11-03 20:13:12 -0700 | [diff] [blame] | 146 | int rcg_clk_set_rate(struct clk *clk, unsigned long rate); |
Matt Wagantall | 9de3bfb | 2011-11-03 20:13:12 -0700 | [diff] [blame] | 147 | unsigned long rcg_clk_get_rate(struct clk *clk); |
Matt Wagantall | 0625ea0 | 2011-07-13 18:51:56 -0700 | [diff] [blame] | 148 | int rcg_clk_list_rate(struct clk *clk, unsigned n); |
| 149 | int rcg_clk_is_enabled(struct clk *clk); |
Matt Wagantall | 9de3bfb | 2011-11-03 20:13:12 -0700 | [diff] [blame] | 150 | long rcg_clk_round_rate(struct clk *clk, unsigned long rate); |
Matt Wagantall | 0625ea0 | 2011-07-13 18:51:56 -0700 | [diff] [blame] | 151 | struct clk *rcg_clk_get_parent(struct clk *c); |
Matt Wagantall | 271a6cd | 2011-09-20 16:06:31 -0700 | [diff] [blame] | 152 | int rcg_clk_handoff(struct clk *c); |
Stephen Boyd | 7bf2814 | 2011-12-07 00:30:52 -0800 | [diff] [blame^] | 153 | int rcg_clk_reset(struct clk *clk, enum clk_reset_action action); |
Matt Wagantall | 0625ea0 | 2011-07-13 18:51:56 -0700 | [diff] [blame] | 154 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 155 | /** |
Stephen Boyd | b8ad822 | 2011-11-28 12:17:58 -0800 | [diff] [blame] | 156 | * struct cdiv_clk - integer divider clock with external source selection |
| 157 | * @ns_reg: source select and divider settings register |
| 158 | * @ext_mask: bit to set to select an external source |
| 159 | * @cur_div: current divider setting (or 0 for external source) |
| 160 | * @max_div: maximum divider value supported (must be power of 2) |
| 161 | * @div_offset: number of bits to shift divider left by in @ns_reg |
| 162 | * @b: branch |
| 163 | * @c: clock |
| 164 | */ |
| 165 | struct cdiv_clk { |
| 166 | void __iomem *const ns_reg; |
| 167 | u32 ext_mask; |
| 168 | |
| 169 | unsigned long cur_div; |
| 170 | u8 div_offset; |
| 171 | u32 max_div; |
| 172 | |
| 173 | struct branch b; |
| 174 | struct clk c; |
| 175 | }; |
| 176 | |
| 177 | static inline struct cdiv_clk *to_cdiv_clk(struct clk *clk) |
| 178 | { |
| 179 | return container_of(clk, struct cdiv_clk, c); |
| 180 | } |
| 181 | |
| 182 | extern struct clk_ops clk_ops_cdiv; |
| 183 | |
| 184 | /** |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 185 | * struct fixed_clk - fixed rate clock (used for crystal oscillators) |
| 186 | * @rate: output rate |
| 187 | * @c: clk |
| 188 | */ |
| 189 | struct fixed_clk { |
| 190 | unsigned long rate; |
| 191 | struct clk c; |
| 192 | }; |
| 193 | |
| 194 | static inline struct fixed_clk *to_fixed_clk(struct clk *clk) |
| 195 | { |
| 196 | return container_of(clk, struct fixed_clk, c); |
| 197 | } |
| 198 | |
Matt Wagantall | 9de3bfb | 2011-11-03 20:13:12 -0700 | [diff] [blame] | 199 | static inline unsigned long fixed_clk_get_rate(struct clk *clk) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 200 | { |
| 201 | struct fixed_clk *f = to_fixed_clk(clk); |
| 202 | return f->rate; |
| 203 | } |
| 204 | |
| 205 | |
| 206 | /** |
| 207 | * struct pll_vote_clk - phase locked loop (HW voteable) |
| 208 | * @rate: output rate |
Vikram Mulukutla | 31680ae | 2011-11-04 14:23:55 -0700 | [diff] [blame] | 209 | * @soft_vote: soft voting variable for multiple PLL software instances |
| 210 | * @soft_vote_mask: soft voting mask for multiple PLL software instances |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 211 | * @en_reg: enable register |
| 212 | * @en_mask: ORed with @en_reg to enable the clock |
| 213 | * @status_reg: status register |
| 214 | * @parent: clock source |
| 215 | * @c: clk |
| 216 | */ |
| 217 | struct pll_vote_clk { |
| 218 | unsigned long rate; |
| 219 | |
Vikram Mulukutla | 31680ae | 2011-11-04 14:23:55 -0700 | [diff] [blame] | 220 | u32 *soft_vote; |
| 221 | const u32 soft_vote_mask; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 222 | void __iomem *const en_reg; |
| 223 | const u32 en_mask; |
| 224 | |
| 225 | void __iomem *const status_reg; |
| 226 | |
| 227 | struct clk *parent; |
| 228 | struct clk c; |
| 229 | }; |
| 230 | |
| 231 | extern struct clk_ops clk_ops_pll_vote; |
| 232 | |
| 233 | static inline struct pll_vote_clk *to_pll_vote_clk(struct clk *clk) |
| 234 | { |
| 235 | return container_of(clk, struct pll_vote_clk, c); |
| 236 | } |
| 237 | |
| 238 | /** |
| 239 | * struct pll_clk - phase locked loop |
| 240 | * @rate: output rate |
| 241 | * @mode_reg: enable register |
| 242 | * @parent: clock source |
| 243 | * @c: clk |
| 244 | */ |
| 245 | struct pll_clk { |
| 246 | unsigned long rate; |
| 247 | |
| 248 | void __iomem *const mode_reg; |
| 249 | |
| 250 | struct clk *parent; |
| 251 | struct clk c; |
| 252 | }; |
| 253 | |
| 254 | extern struct clk_ops clk_ops_pll; |
| 255 | |
| 256 | static inline struct pll_clk *to_pll_clk(struct clk *clk) |
| 257 | { |
| 258 | return container_of(clk, struct pll_clk, c); |
| 259 | } |
| 260 | |
Vikram Mulukutla | 489e39e | 2011-08-31 18:04:05 -0700 | [diff] [blame] | 261 | int sr_pll_clk_enable(struct clk *clk); |
| 262 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 263 | /** |
| 264 | * struct branch_clk - branch |
| 265 | * @enabled: true if clock is on, false otherwise |
| 266 | * @b: branch |
| 267 | * @parent: clock source |
| 268 | * @c: clk |
| 269 | * |
| 270 | * An on/off switch with a rate derived from the parent. |
| 271 | */ |
| 272 | struct branch_clk { |
| 273 | bool enabled; |
| 274 | struct branch b; |
| 275 | struct clk *parent; |
| 276 | struct clk c; |
| 277 | }; |
| 278 | |
| 279 | static inline struct branch_clk *to_branch_clk(struct clk *clk) |
| 280 | { |
| 281 | return container_of(clk, struct branch_clk, c); |
| 282 | } |
| 283 | |
| 284 | int branch_clk_enable(struct clk *clk); |
| 285 | void branch_clk_disable(struct clk *clk); |
| 286 | struct clk *branch_clk_get_parent(struct clk *clk); |
| 287 | int branch_clk_set_parent(struct clk *clk, struct clk *parent); |
| 288 | int branch_clk_is_enabled(struct clk *clk); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 289 | int branch_clk_reset(struct clk *c, enum clk_reset_action action); |
| 290 | |
| 291 | /** |
| 292 | * struct measure_clk - for rate measurement debug use |
| 293 | * @sample_ticks: sample period in reference clock ticks |
| 294 | * @multiplier: measurement scale-up factor |
| 295 | * @divider: measurement scale-down factor |
| 296 | * @c: clk |
| 297 | */ |
| 298 | struct measure_clk { |
| 299 | u64 sample_ticks; |
| 300 | u32 multiplier; |
| 301 | u32 divider; |
| 302 | struct clk c; |
| 303 | }; |
| 304 | |
| 305 | extern struct clk_ops clk_ops_measure; |
| 306 | |
| 307 | static inline struct measure_clk *to_measure_clk(struct clk *clk) |
| 308 | { |
| 309 | return container_of(clk, struct measure_clk, c); |
| 310 | } |
| 311 | |
| 312 | /* |
| 313 | * Variables from clock-local driver |
| 314 | */ |
| 315 | extern spinlock_t local_clock_reg_lock; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 316 | extern struct fixed_clk gnd_clk; |
| 317 | |
| 318 | /* |
| 319 | * Local-clock APIs |
| 320 | */ |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 321 | bool local_clk_is_local(struct clk *clk); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 322 | |
| 323 | /* |
Vikram Mulukutla | 31680ae | 2011-11-04 14:23:55 -0700 | [diff] [blame] | 324 | * PLL vote clock APIs |
| 325 | */ |
| 326 | int pll_vote_clk_enable(struct clk *clk); |
| 327 | void pll_vote_clk_disable(struct clk *clk); |
| 328 | unsigned long pll_vote_clk_get_rate(struct clk *clk); |
| 329 | struct clk *pll_vote_clk_get_parent(struct clk *clk); |
| 330 | int pll_vote_clk_is_enabled(struct clk *clk); |
| 331 | |
| 332 | /* |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 333 | * Generic set-rate implementations |
| 334 | */ |
| 335 | void set_rate_mnd(struct rcg_clk *clk, struct clk_freq_tbl *nf); |
| 336 | void set_rate_nop(struct rcg_clk *clk, struct clk_freq_tbl *nf); |
| 337 | void set_rate_mnd_8(struct rcg_clk *clk, struct clk_freq_tbl *nf); |
| 338 | void set_rate_mnd_banked(struct rcg_clk *clk, struct clk_freq_tbl *nf); |
| 339 | void set_rate_div_banked(struct rcg_clk *clk, struct clk_freq_tbl *nf); |
| 340 | |
| 341 | #endif /* __ARCH_ARM_MACH_MSM_CLOCK_LOCAL_H */ |
| 342 | |