clk-core.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  1. /*
  2. * Copyright 2013 Broadcom Corporation.
  3. *
  4. * SPDX-License-Identifier: GPL-2.0+
  5. */
  6. #include <linux/stddef.h>
  7. #ifdef CONFIG_CLK_DEBUG
  8. #undef writel
  9. #undef readl
  10. static inline void writel(u32 val, void *addr)
  11. {
  12. printf("Write [0x%p] = 0x%08x\n", addr, val);
  13. *(u32 *)addr = val;
  14. }
  15. static inline u32 readl(void *addr)
  16. {
  17. u32 val = *(u32 *)addr;
  18. printf("Read [0x%p] = 0x%08x\n", addr, val);
  19. return val;
  20. }
  21. #endif
  22. struct clk;
  23. struct clk_lookup {
  24. const char *dev_id;
  25. const char *con_id;
  26. struct clk *clk;
  27. };
  28. extern struct clk_lookup arch_clk_tbl[];
  29. extern unsigned int arch_clk_tbl_array_size;
  30. /**
  31. * struct clk_ops - standard clock operations
  32. * @enable: enable/disable clock, see clk_enable() and clk_disable()
  33. * @set_rate: set the clock rate, see clk_set_rate().
  34. * @get_rate: get the clock rate, see clk_get_rate().
  35. * @round_rate: round a given clock rate, see clk_round_rate().
  36. * @set_parent: set the clock's parent, see clk_set_parent().
  37. *
  38. * Group the common clock implementations together so that we
  39. * don't have to keep setting the same fiels again. We leave
  40. * enable in struct clk.
  41. *
  42. */
  43. struct clk_ops {
  44. int (*enable) (struct clk *c, int enable);
  45. int (*set_rate) (struct clk *c, unsigned long rate);
  46. unsigned long (*get_rate) (struct clk *c);
  47. unsigned long (*round_rate) (struct clk *c, unsigned long rate);
  48. int (*set_parent) (struct clk *c, struct clk *parent);
  49. };
  50. struct clk {
  51. struct clk *parent;
  52. const char *name;
  53. int use_cnt;
  54. unsigned long rate; /* in HZ */
  55. /* programmable divider. 0 means fixed ratio to parent clock */
  56. unsigned long div;
  57. struct clk_src *src;
  58. struct clk_ops *ops;
  59. unsigned long ccu_clk_mgr_base;
  60. int sel;
  61. };
  62. struct refclk *refclk_str_to_clk(const char *name);
  63. #define U8_MAX ((u8)~0U)
  64. #define U32_MAX ((u32)~0U)
  65. #define U64_MAX ((u64)~0U)
  66. /* The common clock framework uses u8 to represent a parent index */
  67. #define PARENT_COUNT_MAX ((u32)U8_MAX)
  68. #define BAD_CLK_INDEX U8_MAX /* Can't ever be valid */
  69. #define BAD_CLK_NAME ((const char *)-1)
  70. #define BAD_SCALED_DIV_VALUE U64_MAX
  71. /*
  72. * Utility macros for object flag management. If possible, flags
  73. * should be defined such that 0 is the desired default value.
  74. */
  75. #define FLAG(type, flag) BCM_CLK_ ## type ## _FLAGS_ ## flag
  76. #define FLAG_SET(obj, type, flag) ((obj)->flags |= FLAG(type, flag))
  77. #define FLAG_CLEAR(obj, type, flag) ((obj)->flags &= ~(FLAG(type, flag)))
  78. #define FLAG_FLIP(obj, type, flag) ((obj)->flags ^= FLAG(type, flag))
  79. #define FLAG_TEST(obj, type, flag) (!!((obj)->flags & FLAG(type, flag)))
  80. /* Clock field state tests */
  81. #define gate_exists(gate) FLAG_TEST(gate, GATE, EXISTS)
  82. #define gate_is_enabled(gate) FLAG_TEST(gate, GATE, ENABLED)
  83. #define gate_is_hw_controllable(gate) FLAG_TEST(gate, GATE, HW)
  84. #define gate_is_sw_controllable(gate) FLAG_TEST(gate, GATE, SW)
  85. #define gate_is_sw_managed(gate) FLAG_TEST(gate, GATE, SW_MANAGED)
  86. #define gate_is_no_disable(gate) FLAG_TEST(gate, GATE, NO_DISABLE)
  87. #define gate_flip_enabled(gate) FLAG_FLIP(gate, GATE, ENABLED)
  88. #define divider_exists(div) FLAG_TEST(div, DIV, EXISTS)
  89. #define divider_is_fixed(div) FLAG_TEST(div, DIV, FIXED)
  90. #define divider_has_fraction(div) (!divider_is_fixed(div) && \
  91. (div)->frac_width > 0)
  92. #define selector_exists(sel) ((sel)->width != 0)
  93. #define trigger_exists(trig) FLAG_TEST(trig, TRIG, EXISTS)
  94. /* Clock type, used to tell common block what it's part of */
  95. enum bcm_clk_type {
  96. bcm_clk_none, /* undefined clock type */
  97. bcm_clk_bus,
  98. bcm_clk_core,
  99. bcm_clk_peri
  100. };
  101. /*
  102. * Gating control and status is managed by a 32-bit gate register.
  103. *
  104. * There are several types of gating available:
  105. * - (no gate)
  106. * A clock with no gate is assumed to be always enabled.
  107. * - hardware-only gating (auto-gating)
  108. * Enabling or disabling clocks with this type of gate is
  109. * managed automatically by the hardware. Such clocks can be
  110. * considered by the software to be enabled. The current status
  111. * of auto-gated clocks can be read from the gate status bit.
  112. * - software-only gating
  113. * Auto-gating is not available for this type of clock.
  114. * Instead, software manages whether it's enabled by setting or
  115. * clearing the enable bit. The current gate status of a gate
  116. * under software control can be read from the gate status bit.
  117. * To ensure a change to the gating status is complete, the
  118. * status bit can be polled to verify that the gate has entered
  119. * the desired state.
  120. * - selectable hardware or software gating
  121. * Gating for this type of clock can be configured to be either
  122. * under software or hardware control. Which type is in use is
  123. * determined by the hw_sw_sel bit of the gate register.
  124. */
  125. struct bcm_clk_gate {
  126. u32 offset; /* gate register offset */
  127. u32 status_bit; /* 0: gate is disabled; 0: gatge is enabled */
  128. u32 en_bit; /* 0: disable; 1: enable */
  129. u32 hw_sw_sel_bit; /* 0: hardware gating; 1: software gating */
  130. u32 flags; /* BCM_CLK_GATE_FLAGS_* below */
  131. };
  132. /*
  133. * Gate flags:
  134. * HW means this gate can be auto-gated
  135. * SW means the state of this gate can be software controlled
  136. * NO_DISABLE means this gate is (only) enabled if under software control
  137. * SW_MANAGED means the status of this gate is under software control
  138. * ENABLED means this software-managed gate is *supposed* to be enabled
  139. */
  140. #define BCM_CLK_GATE_FLAGS_EXISTS ((u32)1 << 0) /* Gate is valid */
  141. #define BCM_CLK_GATE_FLAGS_HW ((u32)1 << 1) /* Can auto-gate */
  142. #define BCM_CLK_GATE_FLAGS_SW ((u32)1 << 2) /* Software control */
  143. #define BCM_CLK_GATE_FLAGS_NO_DISABLE ((u32)1 << 3) /* HW or enabled */
  144. #define BCM_CLK_GATE_FLAGS_SW_MANAGED ((u32)1 << 4) /* SW now in control */
  145. #define BCM_CLK_GATE_FLAGS_ENABLED ((u32)1 << 5) /* If SW_MANAGED */
  146. /*
  147. * Gate initialization macros.
  148. *
  149. * Any gate initially under software control will be enabled.
  150. */
  151. /* A hardware/software gate initially under software control */
  152. #define HW_SW_GATE(_offset, _status_bit, _en_bit, _hw_sw_sel_bit) \
  153. { \
  154. .offset = (_offset), \
  155. .status_bit = (_status_bit), \
  156. .en_bit = (_en_bit), \
  157. .hw_sw_sel_bit = (_hw_sw_sel_bit), \
  158. .flags = FLAG(GATE, HW)|FLAG(GATE, SW)| \
  159. FLAG(GATE, SW_MANAGED)|FLAG(GATE, ENABLED)| \
  160. FLAG(GATE, EXISTS), \
  161. }
  162. /* A hardware/software gate initially under hardware control */
  163. #define HW_SW_GATE_AUTO(_offset, _status_bit, _en_bit, _hw_sw_sel_bit) \
  164. { \
  165. .offset = (_offset), \
  166. .status_bit = (_status_bit), \
  167. .en_bit = (_en_bit), \
  168. .hw_sw_sel_bit = (_hw_sw_sel_bit), \
  169. .flags = FLAG(GATE, HW)|FLAG(GATE, SW)| \
  170. FLAG(GATE, EXISTS), \
  171. }
  172. /* A hardware-or-enabled gate (enabled if not under hardware control) */
  173. #define HW_ENABLE_GATE(_offset, _status_bit, _en_bit, _hw_sw_sel_bit) \
  174. { \
  175. .offset = (_offset), \
  176. .status_bit = (_status_bit), \
  177. .en_bit = (_en_bit), \
  178. .hw_sw_sel_bit = (_hw_sw_sel_bit), \
  179. .flags = FLAG(GATE, HW)|FLAG(GATE, SW)| \
  180. FLAG(GATE, NO_DISABLE)|FLAG(GATE, EXISTS), \
  181. }
  182. /* A software-only gate */
  183. #define SW_ONLY_GATE(_offset, _status_bit, _en_bit) \
  184. { \
  185. .offset = (_offset), \
  186. .status_bit = (_status_bit), \
  187. .en_bit = (_en_bit), \
  188. .flags = FLAG(GATE, SW)|FLAG(GATE, SW_MANAGED)| \
  189. FLAG(GATE, ENABLED)|FLAG(GATE, EXISTS), \
  190. }
  191. /* A hardware-only gate */
  192. #define HW_ONLY_GATE(_offset, _status_bit) \
  193. { \
  194. .offset = (_offset), \
  195. .status_bit = (_status_bit), \
  196. .flags = FLAG(GATE, HW)|FLAG(GATE, EXISTS), \
  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. u64 scaled_div; /* scaled divider value */
  246. };
  247. u32 fixed; /* non-zero fixed divider value */
  248. };
  249. u32 flags; /* BCM_CLK_DIV_FLAGS_* below */
  250. };
  251. /*
  252. * Divider flags:
  253. * EXISTS means this divider exists
  254. * FIXED means it is a fixed-rate divider
  255. */
  256. #define BCM_CLK_DIV_FLAGS_EXISTS ((u32)1 << 0) /* Divider is valid */
  257. #define BCM_CLK_DIV_FLAGS_FIXED ((u32)1 << 1) /* Fixed-value */
  258. /* Divider initialization macros */
  259. /* A fixed (non-zero) divider */
  260. #define FIXED_DIVIDER(_value) \
  261. { \
  262. .fixed = (_value), \
  263. .flags = FLAG(DIV, EXISTS)|FLAG(DIV, FIXED), \
  264. }
  265. /* A divider with an integral divisor */
  266. #define DIVIDER(_offset, _shift, _width) \
  267. { \
  268. .offset = (_offset), \
  269. .shift = (_shift), \
  270. .width = (_width), \
  271. .scaled_div = BAD_SCALED_DIV_VALUE, \
  272. .flags = FLAG(DIV, EXISTS), \
  273. }
  274. /* A divider whose divisor has an integer and fractional part */
  275. #define FRAC_DIVIDER(_offset, _shift, _width, _frac_width) \
  276. { \
  277. .offset = (_offset), \
  278. .shift = (_shift), \
  279. .width = (_width), \
  280. .frac_width = (_frac_width), \
  281. .scaled_div = BAD_SCALED_DIV_VALUE, \
  282. .flags = FLAG(DIV, EXISTS), \
  283. }
  284. /*
  285. * Clocks may have multiple "parent" clocks. If there is more than
  286. * one, a selector must be specified to define which of the parent
  287. * clocks is currently in use. The selected clock is indicated in a
  288. * sub-field of a 32-bit selector register. The range of
  289. * representable selector values typically exceeds the number of
  290. * available parent clocks. Occasionally the reset value of a
  291. * selector field is explicitly set to a (specific) value that does
  292. * not correspond to a defined input clock.
  293. *
  294. * We register all known parent clocks with the common clock code
  295. * using a packed array (i.e., no empty slots) of (parent) clock
  296. * names, and refer to them later using indexes into that array.
  297. * We maintain an array of selector values indexed by common clock
  298. * index values in order to map between these common clock indexes
  299. * and the selector values used by the hardware.
  300. *
  301. * Like dividers, a selector can be modified, but to do so a clock
  302. * must be enabled, and a trigger must be used to commit the change.
  303. */
  304. struct bcm_clk_sel {
  305. u32 offset; /* selector register offset */
  306. u32 shift; /* field shift */
  307. u32 width; /* field width */
  308. u32 parent_count; /* number of entries in parent_sel[] */
  309. u32 *parent_sel; /* array of parent selector values */
  310. u8 clk_index; /* current selected index in parent_sel[] */
  311. };
  312. /* Selector initialization macro */
  313. #define SELECTOR(_offset, _shift, _width) \
  314. { \
  315. .offset = (_offset), \
  316. .shift = (_shift), \
  317. .width = (_width), \
  318. .clk_index = BAD_CLK_INDEX, \
  319. }
  320. /*
  321. * Making changes to a variable divider or a selector for a clock
  322. * requires the use of a trigger. A trigger is defined by a single
  323. * bit within a register. To signal a change, a 1 is written into
  324. * that bit. To determine when the change has been completed, that
  325. * trigger bit is polled; the read value will be 1 while the change
  326. * is in progress, and 0 when it is complete.
  327. *
  328. * Occasionally a clock will have more than one trigger. In this
  329. * case, the "pre-trigger" will be used when changing a clock's
  330. * selector and/or its pre-divider.
  331. */
  332. struct bcm_clk_trig {
  333. u32 offset; /* trigger register offset */
  334. u32 bit; /* trigger bit */
  335. u32 flags; /* BCM_CLK_TRIG_FLAGS_* below */
  336. };
  337. /*
  338. * Trigger flags:
  339. * EXISTS means this trigger exists
  340. */
  341. #define BCM_CLK_TRIG_FLAGS_EXISTS ((u32)1 << 0) /* Trigger is valid */
  342. /* Trigger initialization macro */
  343. #define TRIGGER(_offset, _bit) \
  344. { \
  345. .offset = (_offset), \
  346. .bit = (_bit), \
  347. .flags = FLAG(TRIG, EXISTS), \
  348. }
  349. struct bus_clk_data {
  350. struct bcm_clk_gate gate;
  351. };
  352. struct core_clk_data {
  353. struct bcm_clk_gate gate;
  354. };
  355. struct peri_clk_data {
  356. struct bcm_clk_gate gate;
  357. struct bcm_clk_trig pre_trig;
  358. struct bcm_clk_div pre_div;
  359. struct bcm_clk_trig trig;
  360. struct bcm_clk_div div;
  361. struct bcm_clk_sel sel;
  362. const char *clocks[]; /* must be last; use CLOCKS() to declare */
  363. };
  364. #define CLOCKS(...) { __VA_ARGS__, NULL, }
  365. #define NO_CLOCKS { NULL, } /* Must use of no parent clocks */
  366. struct refclk {
  367. struct clk clk;
  368. };
  369. struct peri_clock {
  370. struct clk clk;
  371. struct peri_clk_data *data;
  372. };
  373. struct ccu_clock {
  374. struct clk clk;
  375. int num_policy_masks;
  376. unsigned long policy_freq_offset;
  377. int freq_bit_shift; /* 8 for most CCUs */
  378. unsigned long policy_ctl_offset;
  379. unsigned long policy0_mask_offset;
  380. unsigned long policy1_mask_offset;
  381. unsigned long policy2_mask_offset;
  382. unsigned long policy3_mask_offset;
  383. unsigned long policy0_mask2_offset;
  384. unsigned long policy1_mask2_offset;
  385. unsigned long policy2_mask2_offset;
  386. unsigned long policy3_mask2_offset;
  387. unsigned long lvm_en_offset;
  388. int freq_id;
  389. unsigned long *freq_tbl;
  390. };
  391. struct bus_clock {
  392. struct clk clk;
  393. struct bus_clk_data *data;
  394. unsigned long *freq_tbl;
  395. };
  396. struct ref_clock {
  397. struct clk clk;
  398. };
  399. static inline int is_same_clock(struct clk *a, struct clk *b)
  400. {
  401. return (a == b);
  402. }
  403. #define to_clk(p) (&((p)->clk))
  404. #define name_to_clk(name) (&((name##_clk).clk))
  405. /* declare a struct clk_lookup */
  406. #define CLK_LK(name) \
  407. {.con_id = __stringify(name##_clk), .clk = name_to_clk(name),}
  408. static inline struct refclk *to_refclk(struct clk *clock)
  409. {
  410. return container_of(clock, struct refclk, clk);
  411. }
  412. static inline struct peri_clock *to_peri_clk(struct clk *clock)
  413. {
  414. return container_of(clock, struct peri_clock, clk);
  415. }
  416. static inline struct ccu_clock *to_ccu_clk(struct clk *clock)
  417. {
  418. return container_of(clock, struct ccu_clock, clk);
  419. }
  420. static inline struct bus_clock *to_bus_clk(struct clk *clock)
  421. {
  422. return container_of(clock, struct bus_clock, clk);
  423. }
  424. static inline struct ref_clock *to_ref_clk(struct clk *clock)
  425. {
  426. return container_of(clock, struct ref_clock, clk);
  427. }
  428. extern struct clk_ops peri_clk_ops;
  429. extern struct clk_ops ccu_clk_ops;
  430. extern struct clk_ops bus_clk_ops;
  431. extern struct clk_ops ref_clk_ops;
  432. extern int clk_get_and_enable(char *clkstr);