clk-kona.h 17 KB

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