1 /* 2 * Copyright (C) 2013 Broadcom Corporation 3 * Copyright 2013 Linaro Limited 4 * 5 * This program is free software; you can redistribute it and/or 6 * modify it under the terms of the GNU General Public License as 7 * published by the Free Software Foundation version 2. 8 * 9 * This program is distributed "as is" WITHOUT ANY WARRANTY of any 10 * kind, whether express or implied; without even the implied warranty 11 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 */ 14 15 #ifndef _CLK_KONA_H 16 #define _CLK_KONA_H 17 18 #include <linux/kernel.h> 19 #include <linux/list.h> 20 #include <linux/spinlock.h> 21 #include <linux/slab.h> 22 #include <linux/device.h> 23 #include <linux/of.h> 24 #include <linux/clk-provider.h> 25 26 #define BILLION 1000000000 27 28 /* The common clock framework uses u8 to represent a parent index */ 29 #define PARENT_COUNT_MAX ((u32)U8_MAX) 30 31 #define BAD_CLK_INDEX U8_MAX /* Can't ever be valid */ 32 #define BAD_CLK_NAME ((const char *)-1) 33 34 #define BAD_SCALED_DIV_VALUE U64_MAX 35 36 /* 37 * Utility macros for object flag management. If possible, flags 38 * should be defined such that 0 is the desired default value. 39 */ 40 #define FLAG(type, flag) BCM_CLK_ ## type ## _FLAGS_ ## flag 41 #define FLAG_SET(obj, type, flag) ((obj)->flags |= FLAG(type, flag)) 42 #define FLAG_CLEAR(obj, type, flag) ((obj)->flags &= ~(FLAG(type, flag))) 43 #define FLAG_FLIP(obj, type, flag) ((obj)->flags ^= FLAG(type, flag)) 44 #define FLAG_TEST(obj, type, flag) (!!((obj)->flags & FLAG(type, flag))) 45 46 /* Clock field state tests */ 47 48 #define gate_exists(gate) FLAG_TEST(gate, GATE, EXISTS) 49 #define gate_is_enabled(gate) FLAG_TEST(gate, GATE, ENABLED) 50 #define gate_is_hw_controllable(gate) FLAG_TEST(gate, GATE, HW) 51 #define gate_is_sw_controllable(gate) FLAG_TEST(gate, GATE, SW) 52 #define gate_is_sw_managed(gate) FLAG_TEST(gate, GATE, SW_MANAGED) 53 #define gate_is_no_disable(gate) FLAG_TEST(gate, GATE, NO_DISABLE) 54 55 #define gate_flip_enabled(gate) FLAG_FLIP(gate, GATE, ENABLED) 56 57 #define divider_exists(div) FLAG_TEST(div, DIV, EXISTS) 58 #define divider_is_fixed(div) FLAG_TEST(div, DIV, FIXED) 59 #define divider_has_fraction(div) (!divider_is_fixed(div) && \ 60 (div)->u.s.frac_width > 0) 61 62 #define selector_exists(sel) ((sel)->width != 0) 63 #define trigger_exists(trig) FLAG_TEST(trig, TRIG, EXISTS) 64 65 /* Clock type, used to tell common block what it's part of */ 66 enum bcm_clk_type { 67 bcm_clk_none, /* undefined clock type */ 68 bcm_clk_bus, 69 bcm_clk_core, 70 bcm_clk_peri 71 }; 72 73 /* 74 * Each CCU defines a mapped area of memory containing registers 75 * used to manage clocks implemented by the CCU. Access to memory 76 * within the CCU's space is serialized by a spinlock. Before any 77 * (other) address can be written, a special access "password" value 78 * must be written to its WR_ACCESS register (located at the base 79 * address of the range). We keep track of the name of each CCU as 80 * it is set up, and maintain them in a list. 81 */ 82 struct ccu_data { 83 void __iomem *base; /* base of mapped address space */ 84 spinlock_t lock; /* serialization lock */ 85 bool write_enabled; /* write access is currently enabled */ 86 struct list_head links; /* for ccu_list */ 87 struct device_node *node; 88 struct clk_onecell_data data; 89 const char *name; 90 u32 range; /* byte range of address space */ 91 }; 92 93 /* 94 * Gating control and status is managed by a 32-bit gate register. 95 * 96 * There are several types of gating available: 97 * - (no gate) 98 * A clock with no gate is assumed to be always enabled. 99 * - hardware-only gating (auto-gating) 100 * Enabling or disabling clocks with this type of gate is 101 * managed automatically by the hardware. Such clocks can be 102 * considered by the software to be enabled. The current status 103 * of auto-gated clocks can be read from the gate status bit. 104 * - software-only gating 105 * Auto-gating is not available for this type of clock. 106 * Instead, software manages whether it's enabled by setting or 107 * clearing the enable bit. The current gate status of a gate 108 * under software control can be read from the gate status bit. 109 * To ensure a change to the gating status is complete, the 110 * status bit can be polled to verify that the gate has entered 111 * the desired state. 112 * - selectable hardware or software gating 113 * Gating for this type of clock can be configured to be either 114 * under software or hardware control. Which type is in use is 115 * determined by the hw_sw_sel bit of the gate register. 116 */ 117 struct bcm_clk_gate { 118 u32 offset; /* gate register offset */ 119 u32 status_bit; /* 0: gate is disabled; 0: gatge is enabled */ 120 u32 en_bit; /* 0: disable; 1: enable */ 121 u32 hw_sw_sel_bit; /* 0: hardware gating; 1: software gating */ 122 u32 flags; /* BCM_CLK_GATE_FLAGS_* below */ 123 }; 124 125 /* 126 * Gate flags: 127 * HW means this gate can be auto-gated 128 * SW means the state of this gate can be software controlled 129 * NO_DISABLE means this gate is (only) enabled if under software control 130 * SW_MANAGED means the status of this gate is under software control 131 * ENABLED means this software-managed gate is *supposed* to be enabled 132 */ 133 #define BCM_CLK_GATE_FLAGS_EXISTS ((u32)1 << 0) /* Gate is valid */ 134 #define BCM_CLK_GATE_FLAGS_HW ((u32)1 << 1) /* Can auto-gate */ 135 #define BCM_CLK_GATE_FLAGS_SW ((u32)1 << 2) /* Software control */ 136 #define BCM_CLK_GATE_FLAGS_NO_DISABLE ((u32)1 << 3) /* HW or enabled */ 137 #define BCM_CLK_GATE_FLAGS_SW_MANAGED ((u32)1 << 4) /* SW now in control */ 138 #define BCM_CLK_GATE_FLAGS_ENABLED ((u32)1 << 5) /* If SW_MANAGED */ 139 140 /* 141 * Gate initialization macros. 142 * 143 * Any gate initially under software control will be enabled. 144 */ 145 146 /* A hardware/software gate initially under software control */ 147 #define HW_SW_GATE(_offset, _status_bit, _en_bit, _hw_sw_sel_bit) \ 148 { \ 149 .offset = (_offset), \ 150 .status_bit = (_status_bit), \ 151 .en_bit = (_en_bit), \ 152 .hw_sw_sel_bit = (_hw_sw_sel_bit), \ 153 .flags = FLAG(GATE, HW)|FLAG(GATE, SW)| \ 154 FLAG(GATE, SW_MANAGED)|FLAG(GATE, ENABLED)| \ 155 FLAG(GATE, EXISTS), \ 156 } 157 158 /* A hardware/software gate initially under hardware control */ 159 #define HW_SW_GATE_AUTO(_offset, _status_bit, _en_bit, _hw_sw_sel_bit) \ 160 { \ 161 .offset = (_offset), \ 162 .status_bit = (_status_bit), \ 163 .en_bit = (_en_bit), \ 164 .hw_sw_sel_bit = (_hw_sw_sel_bit), \ 165 .flags = FLAG(GATE, HW)|FLAG(GATE, SW)| \ 166 FLAG(GATE, EXISTS), \ 167 } 168 169 /* A hardware-or-enabled gate (enabled if not under hardware control) */ 170 #define HW_ENABLE_GATE(_offset, _status_bit, _en_bit, _hw_sw_sel_bit) \ 171 { \ 172 .offset = (_offset), \ 173 .status_bit = (_status_bit), \ 174 .en_bit = (_en_bit), \ 175 .hw_sw_sel_bit = (_hw_sw_sel_bit), \ 176 .flags = FLAG(GATE, HW)|FLAG(GATE, SW)| \ 177 FLAG(GATE, NO_DISABLE)|FLAG(GATE, EXISTS), \ 178 } 179 180 /* A software-only gate */ 181 #define SW_ONLY_GATE(_offset, _status_bit, _en_bit) \ 182 { \ 183 .offset = (_offset), \ 184 .status_bit = (_status_bit), \ 185 .en_bit = (_en_bit), \ 186 .flags = FLAG(GATE, SW)|FLAG(GATE, SW_MANAGED)| \ 187 FLAG(GATE, ENABLED)|FLAG(GATE, EXISTS), \ 188 } 189 190 /* A hardware-only gate */ 191 #define HW_ONLY_GATE(_offset, _status_bit) \ 192 { \ 193 .offset = (_offset), \ 194 .status_bit = (_status_bit), \ 195 .flags = FLAG(GATE, HW)|FLAG(GATE, EXISTS), \ 196 } 197 198 /* 199 * Each clock can have zero, one, or two dividers which change the 200 * output rate of the clock. Each divider can be either fixed or 201 * variable. If there are two dividers, they are the "pre-divider" 202 * and the "regular" or "downstream" divider. If there is only one, 203 * there is no pre-divider. 204 * 205 * A fixed divider is any non-zero (positive) value, and it 206 * indicates how the input rate is affected by the divider. 207 * 208 * The value of a variable divider is maintained in a sub-field of a 209 * 32-bit divider register. The position of the field in the 210 * register is defined by its offset and width. The value recorded 211 * in this field is always 1 less than the value it represents. 212 * 213 * In addition, a variable divider can indicate that some subset 214 * of its bits represent a "fractional" part of the divider. Such 215 * bits comprise the low-order portion of the divider field, and can 216 * be viewed as representing the portion of the divider that lies to 217 * the right of the decimal point. Most variable dividers have zero 218 * fractional bits. Variable dividers with non-zero fraction width 219 * still record a value 1 less than the value they represent; the 220 * added 1 does *not* affect the low-order bit in this case, it 221 * affects the bits above the fractional part only. (Often in this 222 * code a divider field value is distinguished from the value it 223 * represents by referring to the latter as a "divisor".) 224 * 225 * In order to avoid dealing with fractions, divider arithmetic is 226 * performed using "scaled" values. A scaled value is one that's 227 * been left-shifted by the fractional width of a divider. Dividing 228 * a scaled value by a scaled divisor produces the desired quotient 229 * without loss of precision and without any other special handling 230 * for fractions. 231 * 232 * The recorded value of a variable divider can be modified. To 233 * modify either divider (or both), a clock must be enabled (i.e., 234 * using its gate). In addition, a trigger register (described 235 * below) must be used to commit the change, and polled to verify 236 * the change is complete. 237 */ 238 struct bcm_clk_div { 239 union { 240 struct { /* variable divider */ 241 u32 offset; /* divider register offset */ 242 u32 shift; /* field shift */ 243 u32 width; /* field width */ 244 u32 frac_width; /* field fraction width */ 245 246 u64 scaled_div; /* scaled divider value */ 247 } s; 248 u32 fixed; /* non-zero fixed divider value */ 249 } u; 250 u32 flags; /* BCM_CLK_DIV_FLAGS_* below */ 251 }; 252 253 /* 254 * Divider flags: 255 * EXISTS means this divider exists 256 * FIXED means it is a fixed-rate divider 257 */ 258 #define BCM_CLK_DIV_FLAGS_EXISTS ((u32)1 << 0) /* Divider is valid */ 259 #define BCM_CLK_DIV_FLAGS_FIXED ((u32)1 << 1) /* Fixed-value */ 260 261 /* Divider initialization macros */ 262 263 /* A fixed (non-zero) divider */ 264 #define FIXED_DIVIDER(_value) \ 265 { \ 266 .u.fixed = (_value), \ 267 .flags = FLAG(DIV, EXISTS)|FLAG(DIV, FIXED), \ 268 } 269 270 /* A divider with an integral divisor */ 271 #define DIVIDER(_offset, _shift, _width) \ 272 { \ 273 .u.s.offset = (_offset), \ 274 .u.s.shift = (_shift), \ 275 .u.s.width = (_width), \ 276 .u.s.scaled_div = BAD_SCALED_DIV_VALUE, \ 277 .flags = FLAG(DIV, EXISTS), \ 278 } 279 280 /* A divider whose divisor has an integer and fractional part */ 281 #define FRAC_DIVIDER(_offset, _shift, _width, _frac_width) \ 282 { \ 283 .u.s.offset = (_offset), \ 284 .u.s.shift = (_shift), \ 285 .u.s.width = (_width), \ 286 .u.s.frac_width = (_frac_width), \ 287 .u.s.scaled_div = BAD_SCALED_DIV_VALUE, \ 288 .flags = FLAG(DIV, EXISTS), \ 289 } 290 291 /* 292 * Clocks may have multiple "parent" clocks. If there is more than 293 * one, a selector must be specified to define which of the parent 294 * clocks is currently in use. The selected clock is indicated in a 295 * sub-field of a 32-bit selector register. The range of 296 * representable selector values typically exceeds the number of 297 * available parent clocks. Occasionally the reset value of a 298 * selector field is explicitly set to a (specific) value that does 299 * not correspond to a defined input clock. 300 * 301 * We register all known parent clocks with the common clock code 302 * using a packed array (i.e., no empty slots) of (parent) clock 303 * names, and refer to them later using indexes into that array. 304 * We maintain an array of selector values indexed by common clock 305 * index values in order to map between these common clock indexes 306 * and the selector values used by the hardware. 307 * 308 * Like dividers, a selector can be modified, but to do so a clock 309 * must be enabled, and a trigger must be used to commit the change. 310 */ 311 struct bcm_clk_sel { 312 u32 offset; /* selector register offset */ 313 u32 shift; /* field shift */ 314 u32 width; /* field width */ 315 316 u32 parent_count; /* number of entries in parent_sel[] */ 317 u32 *parent_sel; /* array of parent selector values */ 318 u8 clk_index; /* current selected index in parent_sel[] */ 319 }; 320 321 /* Selector initialization macro */ 322 #define SELECTOR(_offset, _shift, _width) \ 323 { \ 324 .offset = (_offset), \ 325 .shift = (_shift), \ 326 .width = (_width), \ 327 .clk_index = BAD_CLK_INDEX, \ 328 } 329 330 /* 331 * Making changes to a variable divider or a selector for a clock 332 * requires the use of a trigger. A trigger is defined by a single 333 * bit within a register. To signal a change, a 1 is written into 334 * that bit. To determine when the change has been completed, that 335 * trigger bit is polled; the read value will be 1 while the change 336 * is in progress, and 0 when it is complete. 337 * 338 * Occasionally a clock will have more than one trigger. In this 339 * case, the "pre-trigger" will be used when changing a clock's 340 * selector and/or its pre-divider. 341 */ 342 struct bcm_clk_trig { 343 u32 offset; /* trigger register offset */ 344 u32 bit; /* trigger bit */ 345 u32 flags; /* BCM_CLK_TRIG_FLAGS_* below */ 346 }; 347 348 /* 349 * Trigger flags: 350 * EXISTS means this trigger exists 351 */ 352 #define BCM_CLK_TRIG_FLAGS_EXISTS ((u32)1 << 0) /* Trigger is valid */ 353 354 /* Trigger initialization macro */ 355 #define TRIGGER(_offset, _bit) \ 356 { \ 357 .offset = (_offset), \ 358 .bit = (_bit), \ 359 .flags = FLAG(TRIG, EXISTS), \ 360 } 361 362 struct peri_clk_data { 363 struct bcm_clk_gate gate; 364 struct bcm_clk_trig pre_trig; 365 struct bcm_clk_div pre_div; 366 struct bcm_clk_trig trig; 367 struct bcm_clk_div div; 368 struct bcm_clk_sel sel; 369 const char *clocks[]; /* must be last; use CLOCKS() to declare */ 370 }; 371 #define CLOCKS(...) { __VA_ARGS__, NULL, } 372 #define NO_CLOCKS { NULL, } /* Must use of no parent clocks */ 373 374 struct kona_clk { 375 struct clk_hw hw; 376 struct clk_init_data init_data; 377 const char *name; /* name of this clock */ 378 struct ccu_data *ccu; /* ccu this clock is associated with */ 379 enum bcm_clk_type type; 380 union { 381 void *data; 382 struct peri_clk_data *peri; 383 } u; 384 }; 385 #define to_kona_clk(_hw) \ 386 container_of(_hw, struct kona_clk, hw) 387 388 /* Exported globals */ 389 390 extern struct clk_ops kona_peri_clk_ops; 391 392 /* Help functions */ 393 394 #define PERI_CLK_SETUP(clks, ccu, id, name) \ 395 clks[id] = kona_clk_setup(ccu, #name, bcm_clk_peri, &name ## _data) 396 397 /* Externally visible functions */ 398 399 extern u64 do_div_round_closest(u64 dividend, unsigned long divisor); 400 extern u64 scaled_div_max(struct bcm_clk_div *div); 401 extern u64 scaled_div_build(struct bcm_clk_div *div, u32 div_value, 402 u32 billionths); 403 404 extern struct clk *kona_clk_setup(struct ccu_data *ccu, const char *name, 405 enum bcm_clk_type type, void *data); 406 extern void __init kona_dt_ccu_setup(struct device_node *node, 407 int (*ccu_clks_setup)(struct ccu_data *)); 408 extern bool __init kona_ccu_init(struct ccu_data *ccu); 409 410 #endif /* _CLK_KONA_H */ 411