Amol Jadi | 29f9503 | 2012-06-22 12:52:54 -0700 | [diff] [blame] | 1 | /* |
Channagoud Kadabi | dd7cb38 | 2015-03-23 23:30:25 -0700 | [diff] [blame] | 2 | * Copyright (c) 2012-2015, Linux Foundation. All rights reserved. |
Amol Jadi | 29f9503 | 2012-06-22 12:52:54 -0700 | [diff] [blame] | 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions are met: |
| 6 | * * Redistributions of source code must retain the above copyright |
| 7 | * notice, this list of conditions and the following disclaimer. |
| 8 | * * Redistributions in binary form must reproduce the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer in the |
| 10 | * documentation and/or other materials provided with the distribution. |
Siddhartha Agrawal | acdaf5b | 2013-01-22 18:14:53 -0800 | [diff] [blame] | 11 | * * Neither the name of Linux Foundation nor |
Amol Jadi | 29f9503 | 2012-06-22 12:52:54 -0700 | [diff] [blame] | 12 | * the names of its contributors may be used to endorse or promote |
| 13 | * products derived from this software without specific prior written |
| 14 | * permission. |
| 15 | * |
| 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 18 | * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 19 | * NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
| 20 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 21 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 22 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; |
| 23 | * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 24 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
| 25 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
| 26 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | */ |
| 28 | |
| 29 | #ifndef __CLOCK_LIB2_H |
| 30 | #define __CLOCK_LIB2_H |
| 31 | |
Deepa Dinamani | abe2e07 | 2013-08-15 16:44:48 -0700 | [diff] [blame] | 32 | #include <bits.h> |
Amol Jadi | 29f9503 | 2012-06-22 12:52:54 -0700 | [diff] [blame] | 33 | /* |
| 34 | * Bit manipulation macros |
| 35 | */ |
Amol Jadi | 29f9503 | 2012-06-22 12:52:54 -0700 | [diff] [blame] | 36 | #define BM(msb, lsb) (((((uint32_t)-1) << (31-msb)) >> (31-msb+lsb)) << lsb) |
| 37 | #define BVAL(msb, lsb, val) (((val) << lsb) & BM(msb, lsb)) |
| 38 | |
| 39 | #define container_of(ptr, type, member) \ |
| 40 | ((type *)((addr_t)(ptr) - offsetof(type, member))) |
| 41 | |
| 42 | /* Frequency Macros */ |
| 43 | #define FREQ_END (UINT_MAX-1) |
| 44 | #define F_END \ |
| 45 | { \ |
| 46 | .freq_hz = FREQ_END, \ |
| 47 | } |
| 48 | |
| 49 | /* F(frequency, source, div, m, n) */ |
| 50 | #define F(f, s, div, m, n) \ |
| 51 | { \ |
| 52 | .freq_hz = (f), \ |
| 53 | .src_clk = &s##_clk_src.c, \ |
| 54 | .m_val = (m), \ |
Neeti Desai | ddc771b | 2012-08-28 18:17:04 -0700 | [diff] [blame] | 55 | .n_val = ~((n)-(m)) * !!(n), \ |
Amol Jadi | 29f9503 | 2012-06-22 12:52:54 -0700 | [diff] [blame] | 56 | .d_val = ~(n),\ |
| 57 | .div_src_val = BVAL(4, 0, (int)(2*(div) - 1)) \ |
| 58 | | BVAL(10, 8, s##_source_val), \ |
| 59 | } |
| 60 | |
Channagoud Kadabi | 1dd9baf | 2014-02-04 16:06:11 -0800 | [diff] [blame] | 61 | /* F(frequency, source, div, m, n) */ |
| 62 | #define F_EXT_SRC(f, s, div, m, n) \ |
| 63 | { \ |
| 64 | .freq_hz = (f), \ |
| 65 | .m_val = (m), \ |
| 66 | .n_val = ~((n)-(m)) * !!(n), \ |
| 67 | .d_val = ~(n),\ |
| 68 | .div_src_val = BVAL(4, 0, (int)(2*(div) - 1)) \ |
| 69 | | BVAL(10, 8, s##_source_val), \ |
| 70 | } |
| 71 | |
Siddhartha Agrawal | acdaf5b | 2013-01-22 18:14:53 -0800 | [diff] [blame] | 72 | /* F_MM(frequency, source, div, m, n) */ |
| 73 | #define F_MM(f, s, div, m, n) \ |
| 74 | { \ |
| 75 | .freq_hz = (f), \ |
| 76 | .src_clk = &s##_clk_src.c, \ |
| 77 | .m_val = (m), \ |
| 78 | .n_val = ~((n)-(m)) * !!(n), \ |
| 79 | .d_val = ~(n),\ |
| 80 | .div_src_val = BVAL(4, 0, (int)(2*(div) - 1)) \ |
| 81 | | BVAL(10, 8, s##_mm_source_val), \ |
| 82 | } |
Amol Jadi | 29f9503 | 2012-06-22 12:52:54 -0700 | [diff] [blame] | 83 | |
Asaf Penso | ba57262 | 2013-04-28 18:07:12 +0300 | [diff] [blame] | 84 | #define F_MDSS(f, s, div, m, n) \ |
| 85 | { \ |
| 86 | .freq_hz = (f), \ |
| 87 | .m_val = (m), \ |
| 88 | .n_val = ~((n)-(m)) * !!(n), \ |
| 89 | .d_val = ~(n),\ |
| 90 | .div_src_val = BVAL(4, 0, (int)(2*(div) - 1)) \ |
| 91 | | BVAL(10, 8, s##_mm_source_val), \ |
| 92 | } |
| 93 | |
Amol Jadi | 29f9503 | 2012-06-22 12:52:54 -0700 | [diff] [blame] | 94 | /* Branch Clock Bits */ |
| 95 | #define CBCR_BRANCH_ENABLE_BIT BIT(0) |
| 96 | #define CBCR_BRANCH_OFF_BIT BIT(31) |
Neeti Desai | ac01127 | 2012-08-29 18:24:54 -0700 | [diff] [blame] | 97 | #define BRANCH_CHECK_MASK BM(31, 28) |
| 98 | #define BRANCH_ON_VAL BVAL(31, 28, 0x0) |
| 99 | #define BRANCH_NOC_FSM_ON_VAL BVAL(31, 28, 0x2) |
Amol Jadi | 29f9503 | 2012-06-22 12:52:54 -0700 | [diff] [blame] | 100 | |
| 101 | /* Root Clock Bits */ |
| 102 | #define CMD_UPDATE_BIT BIT(0) |
| 103 | #define CMD_UPDATE_MASK 1 |
| 104 | |
| 105 | #define CFG_SRC_DIV_OFFSET 0 |
| 106 | #define CFG_SRC_DIV_MASK (0x1F << CFG_SRC_DIV_OFFSET) |
| 107 | |
| 108 | #define CFG_SRC_SEL_OFFSET 8 |
Channagoud Kadabi | b4275f1 | 2015-08-15 13:06:47 -0700 | [diff] [blame] | 109 | #define CFG_SRC_SEL_MASK (0x7 << CFG_SRC_SEL_OFFSET) |
Amol Jadi | 29f9503 | 2012-06-22 12:52:54 -0700 | [diff] [blame] | 110 | |
| 111 | #define CFG_MODE_DUAL_EDGE 0x2 |
| 112 | |
| 113 | #define CFG_MODE_OFFSET 12 |
| 114 | #define CFG_MODE_MASK (0x3 << CFG_MODE_OFFSET) |
| 115 | |
| 116 | |
| 117 | /* |
| 118 | * Generic frequency-definition structs and macros |
| 119 | */ |
| 120 | struct clk_freq_tbl { |
| 121 | |
| 122 | const uint32_t freq_hz; |
| 123 | struct clk *src_clk; |
| 124 | const uint32_t div_src_val; |
| 125 | |
| 126 | /* TODO: find out if sys_vdd is needed. */ |
| 127 | |
| 128 | const uint32_t m_val; |
| 129 | const uint32_t n_val; /* not_n_minus_m_val */ |
| 130 | const uint32_t d_val; /* not_2d_val */ |
| 131 | }; |
| 132 | |
| 133 | /* Fixed clock */ |
| 134 | struct fixed_clk { |
| 135 | struct clk c; |
| 136 | }; |
| 137 | |
| 138 | /* Branch clock */ |
| 139 | struct branch_clk { |
| 140 | |
| 141 | uint32_t *const bcr_reg; |
| 142 | uint32_t *const cbcr_reg; |
| 143 | |
| 144 | void (*set_rate)(struct branch_clk *, struct clk_freq_tbl *); |
| 145 | |
| 146 | struct clk *parent; |
| 147 | struct clk c; |
| 148 | |
| 149 | int has_sibling; |
| 150 | uint32_t cur_div; |
| 151 | uint32_t max_div; |
| 152 | uint32_t halt_check; |
Channagoud Kadabi | dd7cb38 | 2015-03-23 23:30:25 -0700 | [diff] [blame] | 153 | bool no_halt_check_on_disable; |
Amol Jadi | 29f9503 | 2012-06-22 12:52:54 -0700 | [diff] [blame] | 154 | }; |
| 155 | |
| 156 | /* Root Clock */ |
| 157 | struct rcg_clk { |
| 158 | |
| 159 | /* RCG registers for this clock */ |
| 160 | |
| 161 | uint32_t *const cmd_reg; /* Command reg */ |
| 162 | uint32_t *const cfg_reg; /* Config reg */ |
| 163 | uint32_t *const m_reg; /* m */ |
| 164 | uint32_t *const n_reg; /* not (n-m) */ |
| 165 | uint32_t *const d_reg; /* not (2d) */ |
| 166 | |
| 167 | /* set rate function for this clock */ |
| 168 | void (*set_rate)(struct rcg_clk *, struct clk_freq_tbl *); |
| 169 | |
| 170 | /* freq table */ |
Channagoud Kadabi | 9dd522f | 2013-10-10 12:30:10 -0700 | [diff] [blame] | 171 | struct clk_freq_tbl *freq_tbl; |
Amol Jadi | 29f9503 | 2012-06-22 12:52:54 -0700 | [diff] [blame] | 172 | struct clk_freq_tbl *current_freq; |
| 173 | |
| 174 | struct clk c; |
| 175 | }; |
| 176 | |
Neeti Desai | ac01127 | 2012-08-29 18:24:54 -0700 | [diff] [blame] | 177 | /* Vote Clock */ |
| 178 | struct vote_clk { |
| 179 | |
| 180 | uint32_t *const cbcr_reg; |
| 181 | uint32_t *const vote_reg; |
| 182 | uint32_t en_mask; |
| 183 | |
| 184 | struct clk c; |
| 185 | }; |
| 186 | |
Channagoud Kadabi | f32341c | 2014-04-03 15:34:04 -0700 | [diff] [blame] | 187 | struct reset_clk { |
| 188 | uint32_t bcr_reg; |
| 189 | struct clk c; |
| 190 | }; |
| 191 | |
| 192 | static inline struct reset_clk *to_reset_clk(struct clk *clk) |
| 193 | { |
| 194 | return container_of(clk, struct reset_clk, c); |
| 195 | } |
| 196 | |
Amol Jadi | 29f9503 | 2012-06-22 12:52:54 -0700 | [diff] [blame] | 197 | static inline struct rcg_clk *to_rcg_clk(struct clk *clk) |
| 198 | { |
| 199 | return container_of(clk, struct rcg_clk, c); |
| 200 | } |
| 201 | |
| 202 | static inline struct branch_clk *to_branch_clk(struct clk *clk) |
| 203 | { |
| 204 | return container_of(clk, struct branch_clk, c); |
| 205 | } |
| 206 | |
Neeti Desai | ac01127 | 2012-08-29 18:24:54 -0700 | [diff] [blame] | 207 | static inline struct vote_clk *to_local_vote_clk(struct clk *clk) |
| 208 | { |
| 209 | return container_of(clk, struct vote_clk, c); |
| 210 | } |
| 211 | |
Amol Jadi | 29f9503 | 2012-06-22 12:52:54 -0700 | [diff] [blame] | 212 | /* RCG clock functions */ |
| 213 | int clock_lib2_rcg_enable(struct clk *c); |
| 214 | int clock_lib2_rcg_set_rate(struct clk *c, unsigned rate); |
| 215 | void clock_lib2_rcg_set_rate_mnd(struct rcg_clk *rclk, struct clk_freq_tbl *freq); |
| 216 | void clock_lib2_rcg_set_rate_hid(struct rcg_clk *rclk, struct clk_freq_tbl *freq); |
| 217 | |
| 218 | /* CXO clock functions */ |
| 219 | int cxo_clk_enable(struct clk *clk); |
| 220 | void cxo_clk_disable(struct clk *clk); |
| 221 | |
| 222 | /* Branch clock functions */ |
| 223 | int clock_lib2_branch_clk_enable(struct clk *clk); |
| 224 | void clock_lib2_branch_clk_disable(struct clk *clk); |
| 225 | int clock_lib2_branch_set_rate(struct clk *c, unsigned rate); |
| 226 | |
Neeti Desai | ac01127 | 2012-08-29 18:24:54 -0700 | [diff] [blame] | 227 | /* Vote clock functions*/ |
| 228 | int clock_lib2_vote_clk_enable(struct clk *c); |
| 229 | void clock_lib2_vote_clk_disable(struct clk *c); |
Channagoud Kadabi | f32341c | 2014-04-03 15:34:04 -0700 | [diff] [blame] | 230 | /* clock reset function */ |
| 231 | int clock_lib2_reset_clk_reset(struct clk *c, enum clk_reset_action action); |
| 232 | int clock_lib2_branch_clk_reset(struct clk *c, enum clk_reset_action action); |
Amol Jadi | 29f9503 | 2012-06-22 12:52:54 -0700 | [diff] [blame] | 233 | #endif |