clk.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  1. /*
  2. * Copyright (c) 2012, NVIDIA CORPORATION. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms and conditions of the GNU General Public License,
  6. * version 2, as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope it will be useful, but WITHOUT
  9. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. * more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #ifndef __TEGRA_CLK_H
  17. #define __TEGRA_CLK_H
  18. #include <linux/clk-provider.h>
  19. #include <linux/clkdev.h>
  20. /**
  21. * struct tegra_clk_sync_source - external clock source from codec
  22. *
  23. * @hw: handle between common and hardware-specific interfaces
  24. * @rate: input frequency from source
  25. * @max_rate: max rate allowed
  26. */
  27. struct tegra_clk_sync_source {
  28. struct clk_hw hw;
  29. unsigned long rate;
  30. unsigned long max_rate;
  31. };
  32. #define to_clk_sync_source(_hw) \
  33. container_of(_hw, struct tegra_clk_sync_source, hw)
  34. extern const struct clk_ops tegra_clk_sync_source_ops;
  35. struct clk *tegra_clk_register_sync_source(const char *name,
  36. unsigned long fixed_rate, unsigned long max_rate);
  37. /**
  38. * struct tegra_clk_frac_div - fractional divider clock
  39. *
  40. * @hw: handle between common and hardware-specific interfaces
  41. * @reg: register containing divider
  42. * @flags: hardware-specific flags
  43. * @shift: shift to the divider bit field
  44. * @width: width of the divider bit field
  45. * @frac_width: width of the fractional bit field
  46. * @lock: register lock
  47. *
  48. * Flags:
  49. * TEGRA_DIVIDER_ROUND_UP - This flags indicates to round up the divider value.
  50. * TEGRA_DIVIDER_FIXED - Fixed rate PLL dividers has addition override bit, this
  51. * flag indicates that this divider is for fixed rate PLL.
  52. * TEGRA_DIVIDER_INT - Some modules can not cope with the duty cycle when
  53. * fraction bit is set. This flags indicates to calculate divider for which
  54. * fracton bit will be zero.
  55. * TEGRA_DIVIDER_UART - UART module divider has additional enable bit which is
  56. * set when divider value is not 0. This flags indicates that the divider
  57. * is for UART module.
  58. */
  59. struct tegra_clk_frac_div {
  60. struct clk_hw hw;
  61. void __iomem *reg;
  62. u8 flags;
  63. u8 shift;
  64. u8 width;
  65. u8 frac_width;
  66. spinlock_t *lock;
  67. };
  68. #define to_clk_frac_div(_hw) container_of(_hw, struct tegra_clk_frac_div, hw)
  69. #define TEGRA_DIVIDER_ROUND_UP BIT(0)
  70. #define TEGRA_DIVIDER_FIXED BIT(1)
  71. #define TEGRA_DIVIDER_INT BIT(2)
  72. #define TEGRA_DIVIDER_UART BIT(3)
  73. extern const struct clk_ops tegra_clk_frac_div_ops;
  74. struct clk *tegra_clk_register_divider(const char *name,
  75. const char *parent_name, void __iomem *reg,
  76. unsigned long flags, u8 clk_divider_flags, u8 shift, u8 width,
  77. u8 frac_width, spinlock_t *lock);
  78. /*
  79. * Tegra PLL:
  80. *
  81. * In general, there are 3 requirements for each PLL
  82. * that SW needs to be comply with.
  83. * (1) Input frequency range (REF).
  84. * (2) Comparison frequency range (CF). CF = REF/DIVM.
  85. * (3) VCO frequency range (VCO). VCO = CF * DIVN.
  86. *
  87. * The final PLL output frequency (FO) = VCO >> DIVP.
  88. */
  89. /**
  90. * struct tegra_clk_pll_freq_table - PLL frequecy table
  91. *
  92. * @input_rate: input rate from source
  93. * @output_rate: output rate from PLL for the input rate
  94. * @n: feedback divider
  95. * @m: input divider
  96. * @p: post divider
  97. * @cpcon: charge pump current
  98. */
  99. struct tegra_clk_pll_freq_table {
  100. unsigned long input_rate;
  101. unsigned long output_rate;
  102. u16 n;
  103. u16 m;
  104. u8 p;
  105. u8 cpcon;
  106. };
  107. /**
  108. * struct pdiv_map - map post divider to hw value
  109. *
  110. * @pdiv: post divider
  111. * @hw_val: value to be written to the PLL hw
  112. */
  113. struct pdiv_map {
  114. u8 pdiv;
  115. u8 hw_val;
  116. };
  117. /**
  118. * struct clk_pll_params - PLL parameters
  119. *
  120. * @input_min: Minimum input frequency
  121. * @input_max: Maximum input frequency
  122. * @cf_min: Minimum comparison frequency
  123. * @cf_max: Maximum comparison frequency
  124. * @vco_min: Minimum VCO frequency
  125. * @vco_max: Maximum VCO frequency
  126. * @base_reg: PLL base reg offset
  127. * @misc_reg: PLL misc reg offset
  128. * @lock_reg: PLL lock reg offset
  129. * @lock_bit_idx: Bit index for PLL lock status
  130. * @lock_enable_bit_idx: Bit index to enable PLL lock
  131. * @lock_delay: Delay in us if PLL lock is not used
  132. */
  133. struct tegra_clk_pll_params {
  134. unsigned long input_min;
  135. unsigned long input_max;
  136. unsigned long cf_min;
  137. unsigned long cf_max;
  138. unsigned long vco_min;
  139. unsigned long vco_max;
  140. u32 base_reg;
  141. u32 misc_reg;
  142. u32 lock_reg;
  143. u32 lock_mask;
  144. u32 lock_enable_bit_idx;
  145. int lock_delay;
  146. int max_p;
  147. struct pdiv_map *pdiv_tohw;
  148. };
  149. /**
  150. * struct tegra_clk_pll - Tegra PLL clock
  151. *
  152. * @hw: handle between common and hardware-specifix interfaces
  153. * @clk_base: address of CAR controller
  154. * @pmc: address of PMC, required to read override bits
  155. * @freq_table: array of frequencies supported by PLL
  156. * @params: PLL parameters
  157. * @flags: PLL flags
  158. * @fixed_rate: PLL rate if it is fixed
  159. * @lock: register lock
  160. * @divn_shift: shift to the feedback divider bit field
  161. * @divn_width: width of the feedback divider bit field
  162. * @divm_shift: shift to the input divider bit field
  163. * @divm_width: width of the input divider bit field
  164. * @divp_shift: shift to the post divider bit field
  165. * @divp_width: width of the post divider bit field
  166. *
  167. * Flags:
  168. * TEGRA_PLL_USE_LOCK - This flag indicated to use lock bits for
  169. * PLL locking. If not set it will use lock_delay value to wait.
  170. * TEGRA_PLL_HAS_CPCON - This flag indicates that CPCON value needs
  171. * to be programmed to change output frequency of the PLL.
  172. * TEGRA_PLL_SET_LFCON - This flag indicates that LFCON value needs
  173. * to be programmed to change output frequency of the PLL.
  174. * TEGRA_PLL_SET_DCCON - This flag indicates that DCCON value needs
  175. * to be programmed to change output frequency of the PLL.
  176. * TEGRA_PLLU - PLLU has inverted post divider. This flags indicated
  177. * that it is PLLU and invert post divider value.
  178. * TEGRA_PLLM - PLLM has additional override settings in PMC. This
  179. * flag indicates that it is PLLM and use override settings.
  180. * TEGRA_PLL_FIXED - We are not supposed to change output frequency
  181. * of some plls.
  182. * TEGRA_PLLE_CONFIGURE - Configure PLLE when enabling.
  183. * TEGRA_PLL_LOCK_MISC - Lock bit is in the misc register instead of the
  184. * base register.
  185. * TEGRA_PLL_BYPASS - PLL has bypass bit
  186. * TEGRA_PLL_HAS_LOCK_ENABLE - PLL has bit to enable lock monitoring
  187. */
  188. struct tegra_clk_pll {
  189. struct clk_hw hw;
  190. void __iomem *clk_base;
  191. void __iomem *pmc;
  192. u32 flags;
  193. unsigned long fixed_rate;
  194. spinlock_t *lock;
  195. u8 divn_shift;
  196. u8 divn_width;
  197. u8 divm_shift;
  198. u8 divm_width;
  199. u8 divp_shift;
  200. u8 divp_width;
  201. struct tegra_clk_pll_freq_table *freq_table;
  202. struct tegra_clk_pll_params *params;
  203. };
  204. #define to_clk_pll(_hw) container_of(_hw, struct tegra_clk_pll, hw)
  205. #define TEGRA_PLL_USE_LOCK BIT(0)
  206. #define TEGRA_PLL_HAS_CPCON BIT(1)
  207. #define TEGRA_PLL_SET_LFCON BIT(2)
  208. #define TEGRA_PLL_SET_DCCON BIT(3)
  209. #define TEGRA_PLLU BIT(4)
  210. #define TEGRA_PLLM BIT(5)
  211. #define TEGRA_PLL_FIXED BIT(6)
  212. #define TEGRA_PLLE_CONFIGURE BIT(7)
  213. #define TEGRA_PLL_LOCK_MISC BIT(8)
  214. #define TEGRA_PLL_BYPASS BIT(9)
  215. #define TEGRA_PLL_HAS_LOCK_ENABLE BIT(10)
  216. extern const struct clk_ops tegra_clk_pll_ops;
  217. extern const struct clk_ops tegra_clk_plle_ops;
  218. struct clk *tegra_clk_register_pll(const char *name, const char *parent_name,
  219. void __iomem *clk_base, void __iomem *pmc,
  220. unsigned long flags, unsigned long fixed_rate,
  221. struct tegra_clk_pll_params *pll_params, u32 pll_flags,
  222. struct tegra_clk_pll_freq_table *freq_table, spinlock_t *lock);
  223. struct clk *tegra_clk_register_plle(const char *name, const char *parent_name,
  224. void __iomem *clk_base, void __iomem *pmc,
  225. unsigned long flags, unsigned long fixed_rate,
  226. struct tegra_clk_pll_params *pll_params, u32 pll_flags,
  227. struct tegra_clk_pll_freq_table *freq_table, spinlock_t *lock);
  228. /**
  229. * struct tegra_clk_pll_out - PLL divider down clock
  230. *
  231. * @hw: handle between common and hardware-specific interfaces
  232. * @reg: register containing the PLL divider
  233. * @enb_bit_idx: bit to enable/disable PLL divider
  234. * @rst_bit_idx: bit to reset PLL divider
  235. * @lock: register lock
  236. * @flags: hardware-specific flags
  237. */
  238. struct tegra_clk_pll_out {
  239. struct clk_hw hw;
  240. void __iomem *reg;
  241. u8 enb_bit_idx;
  242. u8 rst_bit_idx;
  243. spinlock_t *lock;
  244. u8 flags;
  245. };
  246. #define to_clk_pll_out(_hw) container_of(_hw, struct tegra_clk_pll_out, hw)
  247. extern const struct clk_ops tegra_clk_pll_out_ops;
  248. struct clk *tegra_clk_register_pll_out(const char *name,
  249. const char *parent_name, void __iomem *reg, u8 enb_bit_idx,
  250. u8 rst_bit_idx, unsigned long flags, u8 pll_div_flags,
  251. spinlock_t *lock);
  252. /**
  253. * struct tegra_clk_periph_regs - Registers controlling peripheral clock
  254. *
  255. * @enb_reg: read the enable status
  256. * @enb_set_reg: write 1 to enable clock
  257. * @enb_clr_reg: write 1 to disable clock
  258. * @rst_reg: read the reset status
  259. * @rst_set_reg: write 1 to assert the reset of peripheral
  260. * @rst_clr_reg: write 1 to deassert the reset of peripheral
  261. */
  262. struct tegra_clk_periph_regs {
  263. u32 enb_reg;
  264. u32 enb_set_reg;
  265. u32 enb_clr_reg;
  266. u32 rst_reg;
  267. u32 rst_set_reg;
  268. u32 rst_clr_reg;
  269. };
  270. /**
  271. * struct tegra_clk_periph_gate - peripheral gate clock
  272. *
  273. * @magic: magic number to validate type
  274. * @hw: handle between common and hardware-specific interfaces
  275. * @clk_base: address of CAR controller
  276. * @regs: Registers to control the peripheral
  277. * @flags: hardware-specific flags
  278. * @clk_num: Clock number
  279. * @enable_refcnt: array to maintain reference count of the clock
  280. *
  281. * Flags:
  282. * TEGRA_PERIPH_NO_RESET - This flag indicates that reset is not allowed
  283. * for this module.
  284. * TEGRA_PERIPH_MANUAL_RESET - This flag indicates not to reset module
  285. * after clock enable and driver for the module is responsible for
  286. * doing reset.
  287. * TEGRA_PERIPH_ON_APB - If peripheral is in the APB bus then read the
  288. * bus to flush the write operation in apb bus. This flag indicates
  289. * that this peripheral is in apb bus.
  290. */
  291. struct tegra_clk_periph_gate {
  292. u32 magic;
  293. struct clk_hw hw;
  294. void __iomem *clk_base;
  295. u8 flags;
  296. int clk_num;
  297. int *enable_refcnt;
  298. struct tegra_clk_periph_regs *regs;
  299. };
  300. #define to_clk_periph_gate(_hw) \
  301. container_of(_hw, struct tegra_clk_periph_gate, hw)
  302. #define TEGRA_CLK_PERIPH_GATE_MAGIC 0x17760309
  303. #define TEGRA_PERIPH_NO_RESET BIT(0)
  304. #define TEGRA_PERIPH_MANUAL_RESET BIT(1)
  305. #define TEGRA_PERIPH_ON_APB BIT(2)
  306. void tegra_periph_reset(struct tegra_clk_periph_gate *gate, bool assert);
  307. extern const struct clk_ops tegra_clk_periph_gate_ops;
  308. struct clk *tegra_clk_register_periph_gate(const char *name,
  309. const char *parent_name, u8 gate_flags, void __iomem *clk_base,
  310. unsigned long flags, int clk_num,
  311. struct tegra_clk_periph_regs *pregs, int *enable_refcnt);
  312. /**
  313. * struct clk-periph - peripheral clock
  314. *
  315. * @magic: magic number to validate type
  316. * @hw: handle between common and hardware-specific interfaces
  317. * @mux: mux clock
  318. * @divider: divider clock
  319. * @gate: gate clock
  320. * @mux_ops: mux clock ops
  321. * @div_ops: divider clock ops
  322. * @gate_ops: gate clock ops
  323. */
  324. struct tegra_clk_periph {
  325. u32 magic;
  326. struct clk_hw hw;
  327. struct clk_mux mux;
  328. struct tegra_clk_frac_div divider;
  329. struct tegra_clk_periph_gate gate;
  330. const struct clk_ops *mux_ops;
  331. const struct clk_ops *div_ops;
  332. const struct clk_ops *gate_ops;
  333. };
  334. #define to_clk_periph(_hw) container_of(_hw, struct tegra_clk_periph, hw)
  335. #define TEGRA_CLK_PERIPH_MAGIC 0x18221223
  336. extern const struct clk_ops tegra_clk_periph_ops;
  337. struct clk *tegra_clk_register_periph(const char *name,
  338. const char **parent_names, int num_parents,
  339. struct tegra_clk_periph *periph, void __iomem *clk_base,
  340. u32 offset);
  341. struct clk *tegra_clk_register_periph_nodiv(const char *name,
  342. const char **parent_names, int num_parents,
  343. struct tegra_clk_periph *periph, void __iomem *clk_base,
  344. u32 offset);
  345. #define TEGRA_CLK_PERIPH(_mux_shift, _mux_mask, _mux_flags, \
  346. _div_shift, _div_width, _div_frac_width, \
  347. _div_flags, _clk_num, _enb_refcnt, _regs, \
  348. _gate_flags, _table) \
  349. { \
  350. .mux = { \
  351. .flags = _mux_flags, \
  352. .shift = _mux_shift, \
  353. .mask = _mux_mask, \
  354. .table = _table, \
  355. }, \
  356. .divider = { \
  357. .flags = _div_flags, \
  358. .shift = _div_shift, \
  359. .width = _div_width, \
  360. .frac_width = _div_frac_width, \
  361. }, \
  362. .gate = { \
  363. .flags = _gate_flags, \
  364. .clk_num = _clk_num, \
  365. .enable_refcnt = _enb_refcnt, \
  366. .regs = _regs, \
  367. }, \
  368. .mux_ops = &clk_mux_ops, \
  369. .div_ops = &tegra_clk_frac_div_ops, \
  370. .gate_ops = &tegra_clk_periph_gate_ops, \
  371. }
  372. struct tegra_periph_init_data {
  373. const char *name;
  374. int clk_id;
  375. const char **parent_names;
  376. int num_parents;
  377. struct tegra_clk_periph periph;
  378. u32 offset;
  379. const char *con_id;
  380. const char *dev_id;
  381. };
  382. #define TEGRA_INIT_DATA_TABLE(_name, _con_id, _dev_id, _parent_names, _offset,\
  383. _mux_shift, _mux_mask, _mux_flags, _div_shift, \
  384. _div_width, _div_frac_width, _div_flags, _regs, \
  385. _clk_num, _enb_refcnt, _gate_flags, _clk_id, _table) \
  386. { \
  387. .name = _name, \
  388. .clk_id = _clk_id, \
  389. .parent_names = _parent_names, \
  390. .num_parents = ARRAY_SIZE(_parent_names), \
  391. .periph = TEGRA_CLK_PERIPH(_mux_shift, _mux_mask, \
  392. _mux_flags, _div_shift, \
  393. _div_width, _div_frac_width, \
  394. _div_flags, _clk_num, \
  395. _enb_refcnt, _regs, \
  396. _gate_flags, _table), \
  397. .offset = _offset, \
  398. .con_id = _con_id, \
  399. .dev_id = _dev_id, \
  400. }
  401. #define TEGRA_INIT_DATA(_name, _con_id, _dev_id, _parent_names, _offset,\
  402. _mux_shift, _mux_width, _mux_flags, _div_shift, \
  403. _div_width, _div_frac_width, _div_flags, _regs, \
  404. _clk_num, _enb_refcnt, _gate_flags, _clk_id) \
  405. TEGRA_INIT_DATA_TABLE(_name, _con_id, _dev_id, _parent_names, _offset,\
  406. _mux_shift, BIT(_mux_width) - 1, _mux_flags, \
  407. _div_shift, _div_width, _div_frac_width, _div_flags, \
  408. _regs, _clk_num, _enb_refcnt, _gate_flags, _clk_id,\
  409. NULL)
  410. /**
  411. * struct clk_super_mux - super clock
  412. *
  413. * @hw: handle between common and hardware-specific interfaces
  414. * @reg: register controlling multiplexer
  415. * @width: width of the multiplexer bit field
  416. * @flags: hardware-specific flags
  417. * @div2_index: bit controlling divide-by-2
  418. * @pllx_index: PLLX index in the parent list
  419. * @lock: register lock
  420. *
  421. * Flags:
  422. * TEGRA_DIVIDER_2 - LP cluster has additional divider. This flag indicates
  423. * that this is LP cluster clock.
  424. */
  425. struct tegra_clk_super_mux {
  426. struct clk_hw hw;
  427. void __iomem *reg;
  428. u8 width;
  429. u8 flags;
  430. u8 div2_index;
  431. u8 pllx_index;
  432. spinlock_t *lock;
  433. };
  434. #define to_clk_super_mux(_hw) container_of(_hw, struct tegra_clk_super_mux, hw)
  435. #define TEGRA_DIVIDER_2 BIT(0)
  436. extern const struct clk_ops tegra_clk_super_ops;
  437. struct clk *tegra_clk_register_super_mux(const char *name,
  438. const char **parent_names, u8 num_parents,
  439. unsigned long flags, void __iomem *reg, u8 clk_super_flags,
  440. u8 width, u8 pllx_index, u8 div2_index, spinlock_t *lock);
  441. /**
  442. * struct clk_init_tabel - clock initialization table
  443. * @clk_id: clock id as mentioned in device tree bindings
  444. * @parent_id: parent clock id as mentioned in device tree bindings
  445. * @rate: rate to set
  446. * @state: enable/disable
  447. */
  448. struct tegra_clk_init_table {
  449. unsigned int clk_id;
  450. unsigned int parent_id;
  451. unsigned long rate;
  452. int state;
  453. };
  454. /**
  455. * struct clk_duplicate - duplicate clocks
  456. * @clk_id: clock id as mentioned in device tree bindings
  457. * @lookup: duplicate lookup entry for the clock
  458. */
  459. struct tegra_clk_duplicate {
  460. int clk_id;
  461. struct clk_lookup lookup;
  462. };
  463. #define TEGRA_CLK_DUPLICATE(_clk_id, _dev, _con) \
  464. { \
  465. .clk_id = _clk_id, \
  466. .lookup = { \
  467. .dev_id = _dev, \
  468. .con_id = _con, \
  469. }, \
  470. }
  471. void tegra_init_from_table(struct tegra_clk_init_table *tbl,
  472. struct clk *clks[], int clk_max);
  473. void tegra_init_dup_clks(struct tegra_clk_duplicate *dup_list,
  474. struct clk *clks[], int clk_max);
  475. #ifdef CONFIG_ARCH_TEGRA_2x_SOC
  476. void tegra20_clock_init(struct device_node *np);
  477. #else
  478. static inline void tegra20_clock_init(struct device_node *np) {}
  479. #endif /* CONFIG_ARCH_TEGRA_2x_SOC */
  480. #ifdef CONFIG_ARCH_TEGRA_3x_SOC
  481. void tegra30_clock_init(struct device_node *np);
  482. #else
  483. static inline void tegra30_clock_init(struct device_node *np) {}
  484. #endif /* CONFIG_ARCH_TEGRA_3x_SOC */
  485. typedef void (*tegra_clk_apply_init_table_func)(void);
  486. extern tegra_clk_apply_init_table_func tegra_clk_apply_init_table;
  487. #endif /* TEGRA_CLK_H */