clock.h 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. /*
  2. * Copyright (c) 2011 The Chromium OS Authors.
  3. * See file CREDITS for list of people who contributed to this
  4. * project.
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License as
  8. * published by the Free Software Foundation; either version 2 of
  9. * the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  19. * MA 02111-1307 USA
  20. */
  21. /* Tegra clock control functions */
  22. #ifndef _TEGRA_CLOCK_H_
  23. #define _TEGRA_CLOCK_H_
  24. /* Set of oscillator frequencies supported in the internal API. */
  25. enum clock_osc_freq {
  26. /* All in MHz, so 13_0 is 13.0MHz */
  27. CLOCK_OSC_FREQ_13_0,
  28. CLOCK_OSC_FREQ_19_2,
  29. CLOCK_OSC_FREQ_12_0,
  30. CLOCK_OSC_FREQ_26_0,
  31. CLOCK_OSC_FREQ_COUNT,
  32. };
  33. #include <asm/arch/clock-tables.h>
  34. /* PLL stabilization delay in usec */
  35. #define CLOCK_PLL_STABLE_DELAY_US 300
  36. /* return the current oscillator clock frequency */
  37. enum clock_osc_freq clock_get_osc_freq(void);
  38. /**
  39. * Start PLL using the provided configuration parameters.
  40. *
  41. * @param id clock id
  42. * @param divm input divider
  43. * @param divn feedback divider
  44. * @param divp post divider 2^n
  45. * @param cpcon charge pump setup control
  46. * @param lfcon loop filter setup control
  47. *
  48. * @returns monotonic time in us that the PLL will be stable
  49. */
  50. unsigned long clock_start_pll(enum clock_id id, u32 divm, u32 divn,
  51. u32 divp, u32 cpcon, u32 lfcon);
  52. /**
  53. * Set PLL output frequency
  54. *
  55. * @param clkid clock id
  56. * @param pllout pll output id
  57. * @param rate desired output rate
  58. *
  59. * @return 0 if ok, -1 on error (invalid clock id or no suitable divider)
  60. */
  61. int clock_set_pllout(enum clock_id clkid, enum pll_out_id pllout,
  62. unsigned rate);
  63. /**
  64. * Read low-level parameters of a PLL.
  65. *
  66. * @param id clock id to read (note: USB is not supported)
  67. * @param divm returns input divider
  68. * @param divn returns feedback divider
  69. * @param divp returns post divider 2^n
  70. * @param cpcon returns charge pump setup control
  71. * @param lfcon returns loop filter setup control
  72. *
  73. * @returns 0 if ok, -1 on error (invalid clock id)
  74. */
  75. int clock_ll_read_pll(enum clock_id clkid, u32 *divm, u32 *divn,
  76. u32 *divp, u32 *cpcon, u32 *lfcon);
  77. /*
  78. * Enable a clock
  79. *
  80. * @param id clock id
  81. */
  82. void clock_enable(enum periph_id clkid);
  83. /*
  84. * Disable a clock
  85. *
  86. * @param id clock id
  87. */
  88. void clock_disable(enum periph_id clkid);
  89. /*
  90. * Set whether a clock is enabled or disabled.
  91. *
  92. * @param id clock id
  93. * @param enable 1 to enable, 0 to disable
  94. */
  95. void clock_set_enable(enum periph_id clkid, int enable);
  96. /**
  97. * Reset a peripheral. This puts it in reset, waits for a delay, then takes
  98. * it out of reset and waits for th delay again.
  99. *
  100. * @param periph_id peripheral to reset
  101. * @param us_delay time to delay in microseconds
  102. */
  103. void reset_periph(enum periph_id periph_id, int us_delay);
  104. /**
  105. * Put a peripheral into or out of reset.
  106. *
  107. * @param periph_id peripheral to reset
  108. * @param enable 1 to put into reset, 0 to take out of reset
  109. */
  110. void reset_set_enable(enum periph_id periph_id, int enable);
  111. /* CLK_RST_CONTROLLER_RST_CPU_CMPLX_SET/CLR_0 */
  112. enum crc_reset_id {
  113. /* Things we can hold in reset for each CPU */
  114. crc_rst_cpu = 1,
  115. crc_rst_de = 1 << 2, /* What is de? */
  116. crc_rst_watchdog = 1 << 3,
  117. crc_rst_debug = 1 << 4,
  118. };
  119. /**
  120. * Put parts of the CPU complex into or out of reset.\
  121. *
  122. * @param cpu cpu number (0 or 1 on Tegra2, 0-3 on Tegra3)
  123. * @param which which parts of the complex to affect (OR of crc_reset_id)
  124. * @param reset 1 to assert reset, 0 to de-assert
  125. */
  126. void reset_cmplx_set_enable(int cpu, int which, int reset);
  127. /**
  128. * Set the source for a peripheral clock. This plus the divisor sets the
  129. * clock rate. You need to look up the datasheet to see the meaning of the
  130. * source parameter as it changes for each peripheral.
  131. *
  132. * Warning: This function is only for use pre-relocation. Please use
  133. * clock_start_periph_pll() instead.
  134. *
  135. * @param periph_id peripheral to adjust
  136. * @param source source clock (0, 1, 2 or 3)
  137. */
  138. void clock_ll_set_source(enum periph_id periph_id, unsigned source);
  139. /**
  140. * Set the source and divisor for a peripheral clock. This sets the
  141. * clock rate. You need to look up the datasheet to see the meaning of the
  142. * source parameter as it changes for each peripheral.
  143. *
  144. * Warning: This function is only for use pre-relocation. Please use
  145. * clock_start_periph_pll() instead.
  146. *
  147. * @param periph_id peripheral to adjust
  148. * @param source source clock (0, 1, 2 or 3)
  149. * @param divisor divisor value to use
  150. */
  151. void clock_ll_set_source_divisor(enum periph_id periph_id, unsigned source,
  152. unsigned divisor);
  153. /**
  154. * Start a peripheral PLL clock at the given rate. This also resets the
  155. * peripheral.
  156. *
  157. * @param periph_id peripheral to start
  158. * @param parent PLL id of required parent clock
  159. * @param rate Required clock rate in Hz
  160. * @return rate selected in Hz, or -1U if something went wrong
  161. */
  162. unsigned clock_start_periph_pll(enum periph_id periph_id,
  163. enum clock_id parent, unsigned rate);
  164. /**
  165. * Returns the rate of a peripheral clock in Hz. Since the caller almost
  166. * certainly knows the parent clock (having just set it) we require that
  167. * this be passed in so we don't need to work it out.
  168. *
  169. * @param periph_id peripheral to start
  170. * @param parent PLL id of parent clock (used to calculate rate, you
  171. * must know this!)
  172. * @return clock rate of peripheral in Hz
  173. */
  174. unsigned long clock_get_periph_rate(enum periph_id periph_id,
  175. enum clock_id parent);
  176. /**
  177. * Adjust peripheral PLL clock to the given rate. This does not reset the
  178. * peripheral. If a second stage divisor is not available, pass NULL for
  179. * extra_div. If it is available, then this parameter will return the
  180. * divisor selected (which will be a power of 2 from 1 to 256).
  181. *
  182. * @param periph_id peripheral to start
  183. * @param parent PLL id of required parent clock
  184. * @param rate Required clock rate in Hz
  185. * @param extra_div value for the second-stage divisor (NULL if one is
  186. not available)
  187. * @return rate selected in Hz, or -1U if something went wrong
  188. */
  189. unsigned clock_adjust_periph_pll_div(enum periph_id periph_id,
  190. enum clock_id parent, unsigned rate, int *extra_div);
  191. /**
  192. * Returns the clock rate of a specified clock, in Hz.
  193. *
  194. * @param parent PLL id of clock to check
  195. * @return rate of clock in Hz
  196. */
  197. unsigned clock_get_rate(enum clock_id clkid);
  198. /**
  199. * Start up a UART using low-level calls
  200. *
  201. * Prior to relocation clock_start_periph_pll() cannot be called. This
  202. * function provides a way to set up a UART using low-level calls which
  203. * do not require BSS.
  204. *
  205. * @param periph_id Peripheral ID of UART to enable (e,g, PERIPH_ID_UART1)
  206. */
  207. void clock_ll_start_uart(enum periph_id periph_id);
  208. /**
  209. * Decode a peripheral ID from a device tree node.
  210. *
  211. * This works by looking up the peripheral's 'clocks' node and reading out
  212. * the second cell, which is the clock number / peripheral ID.
  213. *
  214. * @param blob FDT blob to use
  215. * @param node Node to look at
  216. * @return peripheral ID, or PERIPH_ID_NONE if none
  217. */
  218. enum periph_id clock_decode_periph_id(const void *blob, int node);
  219. /**
  220. * Checks if the oscillator bypass is enabled (XOBP bit)
  221. *
  222. * @return 1 if bypass is enabled, 0 if not
  223. */
  224. int clock_get_osc_bypass(void);
  225. /*
  226. * Checks that clocks are valid and prints a warning if not
  227. *
  228. * @return 0 if ok, -1 on error
  229. */
  230. int clock_verify(void);
  231. /* Initialize the clocks */
  232. void clock_init(void);
  233. /* Initialize the PLLs */
  234. void clock_early_init(void);
  235. /* Returns a pointer to the clock source register for a peripheral */
  236. u32 *get_periph_source_reg(enum periph_id periph_id);
  237. /**
  238. * Given a peripheral ID and the required source clock, this returns which
  239. * value should be programmed into the source mux for that peripheral.
  240. *
  241. * There is special code here to handle the one source type with 5 sources.
  242. *
  243. * @param periph_id peripheral to start
  244. * @param source PLL id of required parent clock
  245. * @param mux_bits Set to number of bits in mux register: 2 or 4
  246. * @param divider_bits Set to number of divider bits (8 or 16)
  247. * @return mux value (0-4, or -1 if not found)
  248. */
  249. int get_periph_clock_source(enum periph_id periph_id,
  250. enum clock_id parent, int *mux_bits, int *divider_bits);
  251. /*
  252. * Convert a device tree clock ID to our peripheral ID. They are mostly
  253. * the same but we are very cautious so we check that a valid clock ID is
  254. * provided.
  255. *
  256. * @param clk_id Clock ID according to tegra30 device tree binding
  257. * @return peripheral ID, or PERIPH_ID_NONE if the clock ID is invalid
  258. */
  259. enum periph_id clk_id_to_periph_id(int clk_id);
  260. /**
  261. * Set the output frequency you want for each PLL clock.
  262. * PLL output frequencies are programmed by setting their N, M and P values.
  263. * The governing equations are:
  264. * VCO = (Fi / m) * n, Fo = VCO / (2^p)
  265. * where Fo is the output frequency from the PLL.
  266. * Example: Set the output frequency to 216Mhz(Fo) with 12Mhz OSC(Fi)
  267. * 216Mhz = ((12Mhz / m) * n) / (2^p) so n=432,m=12,p=1
  268. * Please see Tegra TRM section 5.3 to get the detail for PLL Programming
  269. *
  270. * @param n PLL feedback divider(DIVN)
  271. * @param m PLL input divider(DIVN)
  272. * @param p post divider(DIVP)
  273. * @param cpcon base PLL charge pump(CPCON)
  274. * @return 0 if ok, -1 on error (the requested PLL is incorrect and cannot
  275. * be overriden), 1 if PLL is already correct
  276. */
  277. int clock_set_rate(enum clock_id clkid, u32 n, u32 m, u32 p, u32 cpcon);
  278. /* return 1 if a peripheral ID is in range */
  279. #define clock_type_id_isvalid(id) ((id) >= 0 && \
  280. (id) < CLOCK_TYPE_COUNT)
  281. /* return 1 if a periphc_internal_id is in range */
  282. #define periphc_internal_id_isvalid(id) ((id) >= 0 && \
  283. (id) < PERIPHC_COUNT)
  284. #endif /* _TEGRA_CLOCK_H_ */