clock.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. /* SPDX-License-Identifier: GPL-2.0+ */
  2. /*
  3. * Copyright (c) 2011 The Chromium OS Authors.
  4. */
  5. /* Tegra clock control functions */
  6. #ifndef _TEGRA_CLOCK_H_
  7. #define _TEGRA_CLOCK_H_
  8. struct udevice;
  9. /* Set of oscillator frequencies supported in the internal API. */
  10. enum clock_osc_freq {
  11. /* All in MHz, so 13_0 is 13.0MHz */
  12. CLOCK_OSC_FREQ_13_0,
  13. CLOCK_OSC_FREQ_19_2,
  14. CLOCK_OSC_FREQ_12_0,
  15. CLOCK_OSC_FREQ_26_0,
  16. CLOCK_OSC_FREQ_38_4,
  17. CLOCK_OSC_FREQ_48_0,
  18. CLOCK_OSC_FREQ_COUNT,
  19. };
  20. /*
  21. * Note that no Tegra clock register actually uses all of bits 31:28 as
  22. * the mux field. Rather, bits 30:28, 29:28, or 28 are used. However, in
  23. * those cases, nothing is stored in the bits about the mux field, so it's
  24. * safe to pretend that the mux field extends all the way to the end of the
  25. * register. As such, the U-Boot clock driver is currently a bit lazy, and
  26. * doesn't distinguish between 31:28, 30:28, 29:28 and 28; it just lumps
  27. * them all together and pretends they're all 31:28.
  28. */
  29. enum {
  30. MASK_BITS_31_30,
  31. MASK_BITS_31_29,
  32. MASK_BITS_31_28,
  33. };
  34. #include <asm/arch/clock-tables.h>
  35. /* PLL stabilization delay in usec */
  36. #define CLOCK_PLL_STABLE_DELAY_US 300
  37. /* return the current oscillator clock frequency */
  38. enum clock_osc_freq clock_get_osc_freq(void);
  39. /* return the clk_m frequency */
  40. unsigned int clk_m_get_rate(unsigned int parent_rate);
  41. /**
  42. * Start PLL using the provided configuration parameters.
  43. *
  44. * @param id clock id
  45. * @param divm input divider
  46. * @param divn feedback divider
  47. * @param divp post divider 2^n
  48. * @param cpcon charge pump setup control
  49. * @param lfcon loop filter setup control
  50. *
  51. * @returns monotonic time in us that the PLL will be stable
  52. */
  53. unsigned long clock_start_pll(enum clock_id id, u32 divm, u32 divn,
  54. u32 divp, u32 cpcon, u32 lfcon);
  55. /**
  56. * Set PLL output frequency
  57. *
  58. * @param clkid clock id
  59. * @param pllout pll output id
  60. * @param rate desired output rate
  61. *
  62. * @return 0 if ok, -1 on error (invalid clock id or no suitable divider)
  63. */
  64. int clock_set_pllout(enum clock_id clkid, enum pll_out_id pllout,
  65. unsigned rate);
  66. /**
  67. * Read low-level parameters of a PLL.
  68. *
  69. * @param id clock id to read (note: USB is not supported)
  70. * @param divm returns input divider
  71. * @param divn returns feedback divider
  72. * @param divp returns post divider 2^n
  73. * @param cpcon returns charge pump setup control
  74. * @param lfcon returns loop filter setup control
  75. *
  76. * @returns 0 if ok, -1 on error (invalid clock id)
  77. */
  78. int clock_ll_read_pll(enum clock_id clkid, u32 *divm, u32 *divn,
  79. u32 *divp, u32 *cpcon, u32 *lfcon);
  80. /*
  81. * Enable a clock
  82. *
  83. * @param id clock id
  84. */
  85. void clock_enable(enum periph_id clkid);
  86. /*
  87. * Disable a clock
  88. *
  89. * @param id clock id
  90. */
  91. void clock_disable(enum periph_id clkid);
  92. /*
  93. * Set whether a clock is enabled or disabled.
  94. *
  95. * @param id clock id
  96. * @param enable 1 to enable, 0 to disable
  97. */
  98. void clock_set_enable(enum periph_id clkid, int enable);
  99. /**
  100. * Reset a peripheral. This puts it in reset, waits for a delay, then takes
  101. * it out of reset and waits for th delay again.
  102. *
  103. * @param periph_id peripheral to reset
  104. * @param us_delay time to delay in microseconds
  105. */
  106. void reset_periph(enum periph_id periph_id, int us_delay);
  107. /**
  108. * Put a peripheral into or out of reset.
  109. *
  110. * @param periph_id peripheral to reset
  111. * @param enable 1 to put into reset, 0 to take out of reset
  112. */
  113. void reset_set_enable(enum periph_id periph_id, int enable);
  114. /* CLK_RST_CONTROLLER_RST_CPU_CMPLX_SET/CLR_0 */
  115. enum crc_reset_id {
  116. /* Things we can hold in reset for each CPU */
  117. crc_rst_cpu = 1,
  118. crc_rst_de = 1 << 4, /* What is de? */
  119. crc_rst_watchdog = 1 << 8,
  120. crc_rst_debug = 1 << 12,
  121. };
  122. /**
  123. * Put parts of the CPU complex into or out of reset.\
  124. *
  125. * @param cpu cpu number (0 or 1 on Tegra2, 0-3 on Tegra3)
  126. * @param which which parts of the complex to affect (OR of crc_reset_id)
  127. * @param reset 1 to assert reset, 0 to de-assert
  128. */
  129. void reset_cmplx_set_enable(int cpu, int which, int reset);
  130. /**
  131. * Set the source for a peripheral clock. This plus the divisor sets the
  132. * clock rate. You need to look up the datasheet to see the meaning of the
  133. * source parameter as it changes for each peripheral.
  134. *
  135. * Warning: This function is only for use pre-relocation. Please use
  136. * clock_start_periph_pll() instead.
  137. *
  138. * @param periph_id peripheral to adjust
  139. * @param source source clock (0, 1, 2 or 3)
  140. */
  141. void clock_ll_set_source(enum periph_id periph_id, unsigned source);
  142. /**
  143. * This function is similar to clock_ll_set_source() except that it can be
  144. * used for clocks with more than 2 mux bits.
  145. *
  146. * @param periph_id peripheral to adjust
  147. * @param mux_bits number of mux bits for the clock
  148. * @param source source clock (0-15 depending on mux_bits)
  149. */
  150. int clock_ll_set_source_bits(enum periph_id periph_id, int mux_bits,
  151. unsigned source);
  152. /**
  153. * Set the source and divisor for a peripheral clock. This sets the
  154. * clock rate. You need to look up the datasheet to see the meaning of the
  155. * source parameter as it changes for each peripheral.
  156. *
  157. * Warning: This function is only for use pre-relocation. Please use
  158. * clock_start_periph_pll() instead.
  159. *
  160. * @param periph_id peripheral to adjust
  161. * @param source source clock (0, 1, 2 or 3)
  162. * @param divisor divisor value to use
  163. */
  164. void clock_ll_set_source_divisor(enum periph_id periph_id, unsigned source,
  165. unsigned divisor);
  166. /**
  167. * Returns the current parent clock ID of a given peripheral. This can be
  168. * useful in order to call clock_*_periph_*() from generic code that has no
  169. * specific knowledge of system-level clock tree structure.
  170. *
  171. * @param periph_id peripheral to query
  172. * @return clock ID of the peripheral's current parent clock
  173. */
  174. enum clock_id clock_get_periph_parent(enum periph_id periph_id);
  175. /**
  176. * Start a peripheral PLL clock at the given rate. This also resets the
  177. * peripheral.
  178. *
  179. * @param periph_id peripheral to start
  180. * @param parent PLL id of required parent clock
  181. * @param rate Required clock rate in Hz
  182. * @return rate selected in Hz, or -1U if something went wrong
  183. */
  184. unsigned clock_start_periph_pll(enum periph_id periph_id,
  185. enum clock_id parent, unsigned rate);
  186. /**
  187. * Returns the rate of a peripheral clock in Hz. Since the caller almost
  188. * certainly knows the parent clock (having just set it) we require that
  189. * this be passed in so we don't need to work it out.
  190. *
  191. * @param periph_id peripheral to start
  192. * @param parent PLL id of parent clock (used to calculate rate, you
  193. * must know this!)
  194. * @return clock rate of peripheral in Hz
  195. */
  196. unsigned long clock_get_periph_rate(enum periph_id periph_id,
  197. enum clock_id parent);
  198. /**
  199. * Adjust peripheral PLL clock to the given rate. This does not reset the
  200. * peripheral. If a second stage divisor is not available, pass NULL for
  201. * extra_div. If it is available, then this parameter will return the
  202. * divisor selected (which will be a power of 2 from 1 to 256).
  203. *
  204. * @param periph_id peripheral to start
  205. * @param parent PLL id of required parent clock
  206. * @param rate Required clock rate in Hz
  207. * @param extra_div value for the second-stage divisor (NULL if one is
  208. not available)
  209. * @return rate selected in Hz, or -1U if something went wrong
  210. */
  211. unsigned clock_adjust_periph_pll_div(enum periph_id periph_id,
  212. enum clock_id parent, unsigned rate, int *extra_div);
  213. /**
  214. * Returns the clock rate of a specified clock, in Hz.
  215. *
  216. * @param parent PLL id of clock to check
  217. * @return rate of clock in Hz
  218. */
  219. unsigned clock_get_rate(enum clock_id clkid);
  220. /**
  221. * Start up a UART using low-level calls
  222. *
  223. * Prior to relocation clock_start_periph_pll() cannot be called. This
  224. * function provides a way to set up a UART using low-level calls which
  225. * do not require BSS.
  226. *
  227. * @param periph_id Peripheral ID of UART to enable (e,g, PERIPH_ID_UART1)
  228. */
  229. void clock_ll_start_uart(enum periph_id periph_id);
  230. /**
  231. * Decode a peripheral ID from a device tree node.
  232. *
  233. * This works by looking up the peripheral's 'clocks' node and reading out
  234. * the second cell, which is the clock number / peripheral ID.
  235. *
  236. * @param blob FDT blob to use
  237. * @param node Node to look at
  238. * @return peripheral ID, or PERIPH_ID_NONE if none
  239. */
  240. int clock_decode_periph_id(struct udevice *dev);
  241. /**
  242. * Checks if the oscillator bypass is enabled (XOBP bit)
  243. *
  244. * @return 1 if bypass is enabled, 0 if not
  245. */
  246. int clock_get_osc_bypass(void);
  247. /*
  248. * Checks that clocks are valid and prints a warning if not
  249. *
  250. * @return 0 if ok, -1 on error
  251. */
  252. int clock_verify(void);
  253. /* Initialize the clocks */
  254. void clock_init(void);
  255. /* Initialize the PLLs */
  256. void clock_early_init(void);
  257. /* @return true if hardware indicates that clock_early_init() was called */
  258. bool clock_early_init_done(void);
  259. /* Returns a pointer to the clock source register for a peripheral */
  260. u32 *get_periph_source_reg(enum periph_id periph_id);
  261. /* Returns a pointer to the given 'simple' PLL */
  262. struct clk_pll_simple *clock_get_simple_pll(enum clock_id clkid);
  263. /*
  264. * Given a peripheral ID, determine where the mux bits are in the peripheral
  265. * clock's register, the number of divider bits the clock has, and the SoC-
  266. * specific clock type.
  267. *
  268. * This is an internal API between the core Tegra clock code and the SoC-
  269. * specific clock code.
  270. *
  271. * @param periph_id peripheral to query
  272. * @param mux_bits Set to number of bits in mux register
  273. * @param divider_bits Set to the relevant MASK_BITS_* value
  274. * @param type Set to the SoC-specific clock type
  275. * @return 0 on success, -1 on error
  276. */
  277. int get_periph_clock_info(enum periph_id periph_id, int *mux_bits,
  278. int *divider_bits, int *type);
  279. /*
  280. * Given a peripheral ID and clock source mux value, determine the clock_id
  281. * of that peripheral's parent.
  282. *
  283. * This is an internal API between the core Tegra clock code and the SoC-
  284. * specific clock code.
  285. *
  286. * @param periph_id peripheral to query
  287. * @param source raw clock source mux value
  288. * @return the CLOCK_ID_* value @source represents
  289. */
  290. enum clock_id get_periph_clock_id(enum periph_id periph_id, int source);
  291. /**
  292. * Given a peripheral ID and the required source clock, this returns which
  293. * value should be programmed into the source mux for that peripheral.
  294. *
  295. * There is special code here to handle the one source type with 5 sources.
  296. *
  297. * @param periph_id peripheral to start
  298. * @param source PLL id of required parent clock
  299. * @param mux_bits Set to number of bits in mux register: 2 or 4
  300. * @param divider_bits Set to number of divider bits (8 or 16)
  301. * @return mux value (0-4, or -1 if not found)
  302. */
  303. int get_periph_clock_source(enum periph_id periph_id,
  304. enum clock_id parent, int *mux_bits, int *divider_bits);
  305. /*
  306. * Convert a device tree clock ID to our peripheral ID. They are mostly
  307. * the same but we are very cautious so we check that a valid clock ID is
  308. * provided.
  309. *
  310. * @param clk_id Clock ID according to tegra30 device tree binding
  311. * @return peripheral ID, or PERIPH_ID_NONE if the clock ID is invalid
  312. */
  313. enum periph_id clk_id_to_periph_id(int clk_id);
  314. /**
  315. * Set the output frequency you want for each PLL clock.
  316. * PLL output frequencies are programmed by setting their N, M and P values.
  317. * The governing equations are:
  318. * VCO = (Fi / m) * n, Fo = VCO / (2^p)
  319. * where Fo is the output frequency from the PLL.
  320. * Example: Set the output frequency to 216Mhz(Fo) with 12Mhz OSC(Fi)
  321. * 216Mhz = ((12Mhz / m) * n) / (2^p) so n=432,m=12,p=1
  322. * Please see Tegra TRM section 5.3 to get the detail for PLL Programming
  323. *
  324. * @param n PLL feedback divider(DIVN)
  325. * @param m PLL input divider(DIVN)
  326. * @param p post divider(DIVP)
  327. * @param cpcon base PLL charge pump(CPCON)
  328. * @return 0 if ok, -1 on error (the requested PLL is incorrect and cannot
  329. * be overridden), 1 if PLL is already correct
  330. */
  331. int clock_set_rate(enum clock_id clkid, u32 n, u32 m, u32 p, u32 cpcon);
  332. /* return 1 if a peripheral ID is in range */
  333. #define clock_type_id_isvalid(id) ((id) >= 0 && \
  334. (id) < CLOCK_TYPE_COUNT)
  335. /* return 1 if a periphc_internal_id is in range */
  336. #define periphc_internal_id_isvalid(id) ((id) >= 0 && \
  337. (id) < PERIPHC_COUNT)
  338. /* SoC-specific TSC init */
  339. void arch_timer_init(void);
  340. void tegra30_set_up_pllp(void);
  341. /* Number of PLL-based clocks (i.e. not OSC, MCLK or 32KHz) */
  342. #define CLOCK_ID_PLL_COUNT (CLOCK_ID_COUNT - 3)
  343. struct clk_pll_info {
  344. u32 m_shift:5; /* DIVM_SHIFT */
  345. u32 n_shift:5; /* DIVN_SHIFT */
  346. u32 p_shift:5; /* DIVP_SHIFT */
  347. u32 kcp_shift:5; /* KCP/cpcon SHIFT */
  348. u32 kvco_shift:5; /* KVCO/lfcon SHIFT */
  349. u32 lock_ena:6; /* LOCK_ENABLE/EN_LOCKDET shift */
  350. u32 rsvd:1;
  351. u32 m_mask:10; /* DIVM_MASK */
  352. u32 n_mask:12; /* DIVN_MASK */
  353. u32 p_mask:10; /* DIVP_MASK or VCO_MASK */
  354. u32 kcp_mask:10; /* KCP/CPCON MASK */
  355. u32 kvco_mask:10; /* KVCO/LFCON MASK */
  356. u32 lock_det:6; /* LOCK_DETECT/LOCKED shift */
  357. u32 rsvd2:6;
  358. };
  359. extern struct clk_pll_info tegra_pll_info_table[CLOCK_ID_PLL_COUNT];
  360. struct periph_clk_init {
  361. enum periph_id periph_id;
  362. enum clock_id parent_clock_id;
  363. };
  364. extern struct periph_clk_init periph_clk_init_table[];
  365. /**
  366. * Enable output clock for external peripherals
  367. *
  368. * @param clk_id Clock ID to output (1, 2 or 3)
  369. * @return 0 if OK. -ve on error
  370. */
  371. int clock_external_output(int clk_id);
  372. #endif /* _TEGRA_CLOCK_H_ */