clk-kona.h 17 KB

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