clock.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  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. /* Tegra2 clock control functions */
  22. #ifndef _CLOCK_H
  23. #define _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. /* The PLLs supported by the hardware */
  34. enum clock_id {
  35. CLOCK_ID_FIRST,
  36. CLOCK_ID_CGENERAL = CLOCK_ID_FIRST,
  37. CLOCK_ID_MEMORY,
  38. CLOCK_ID_PERIPH,
  39. CLOCK_ID_AUDIO,
  40. CLOCK_ID_USB,
  41. CLOCK_ID_DISPLAY,
  42. /* now the simple ones */
  43. CLOCK_ID_FIRST_SIMPLE,
  44. CLOCK_ID_XCPU = CLOCK_ID_FIRST_SIMPLE,
  45. CLOCK_ID_EPCI,
  46. CLOCK_ID_SFROM32KHZ,
  47. /* These are the base clocks (inputs to the Tegra SOC) */
  48. CLOCK_ID_32KHZ,
  49. CLOCK_ID_OSC,
  50. CLOCK_ID_COUNT, /* number of clocks */
  51. CLOCK_ID_NONE = -1,
  52. };
  53. /* The clocks supported by the hardware */
  54. enum periph_id {
  55. PERIPH_ID_FIRST,
  56. /* Low word: 31:0 */
  57. PERIPH_ID_CPU = PERIPH_ID_FIRST,
  58. PERIPH_ID_RESERVED1,
  59. PERIPH_ID_RESERVED2,
  60. PERIPH_ID_AC97,
  61. PERIPH_ID_RTC,
  62. PERIPH_ID_TMR,
  63. PERIPH_ID_UART1,
  64. PERIPH_ID_UART2,
  65. /* 8 */
  66. PERIPH_ID_GPIO,
  67. PERIPH_ID_SDMMC2,
  68. PERIPH_ID_SPDIF,
  69. PERIPH_ID_I2S1,
  70. PERIPH_ID_I2C1,
  71. PERIPH_ID_NDFLASH,
  72. PERIPH_ID_SDMMC1,
  73. PERIPH_ID_SDMMC4,
  74. /* 16 */
  75. PERIPH_ID_TWC,
  76. PERIPH_ID_PWM,
  77. PERIPH_ID_I2S2,
  78. PERIPH_ID_EPP,
  79. PERIPH_ID_VI,
  80. PERIPH_ID_2D,
  81. PERIPH_ID_USBD,
  82. PERIPH_ID_ISP,
  83. /* 24 */
  84. PERIPH_ID_3D,
  85. PERIPH_ID_IDE,
  86. PERIPH_ID_DISP2,
  87. PERIPH_ID_DISP1,
  88. PERIPH_ID_HOST1X,
  89. PERIPH_ID_VCP,
  90. PERIPH_ID_RESERVED30,
  91. PERIPH_ID_CACHE2,
  92. /* Middle word: 63:32 */
  93. PERIPH_ID_MEM,
  94. PERIPH_ID_AHBDMA,
  95. PERIPH_ID_APBDMA,
  96. PERIPH_ID_RESERVED35,
  97. PERIPH_ID_KBC,
  98. PERIPH_ID_STAT_MON,
  99. PERIPH_ID_PMC,
  100. PERIPH_ID_FUSE,
  101. /* 40 */
  102. PERIPH_ID_KFUSE,
  103. PERIPH_ID_SBC1,
  104. PERIPH_ID_SNOR,
  105. PERIPH_ID_SPI1,
  106. PERIPH_ID_SBC2,
  107. PERIPH_ID_XIO,
  108. PERIPH_ID_SBC3,
  109. PERIPH_ID_DVC_I2C,
  110. /* 48 */
  111. PERIPH_ID_DSI,
  112. PERIPH_ID_TVO,
  113. PERIPH_ID_MIPI,
  114. PERIPH_ID_HDMI,
  115. PERIPH_ID_CSI,
  116. PERIPH_ID_TVDAC,
  117. PERIPH_ID_I2C2,
  118. PERIPH_ID_UART3,
  119. /* 56 */
  120. PERIPH_ID_RESERVED56,
  121. PERIPH_ID_EMC,
  122. PERIPH_ID_USB2,
  123. PERIPH_ID_USB3,
  124. PERIPH_ID_MPE,
  125. PERIPH_ID_VDE,
  126. PERIPH_ID_BSEA,
  127. PERIPH_ID_BSEV,
  128. /* Upper word 95:64 */
  129. PERIPH_ID_SPEEDO,
  130. PERIPH_ID_UART4,
  131. PERIPH_ID_UART5,
  132. PERIPH_ID_I2C3,
  133. PERIPH_ID_SBC4,
  134. PERIPH_ID_SDMMC3,
  135. PERIPH_ID_PCIE,
  136. PERIPH_ID_OWR,
  137. /* 72 */
  138. PERIPH_ID_AFI,
  139. PERIPH_ID_CORESIGHT,
  140. PERIPH_ID_RESERVED74,
  141. PERIPH_ID_AVPUCQ,
  142. PERIPH_ID_RESERVED76,
  143. PERIPH_ID_RESERVED77,
  144. PERIPH_ID_RESERVED78,
  145. PERIPH_ID_RESERVED79,
  146. /* 80 */
  147. PERIPH_ID_RESERVED80,
  148. PERIPH_ID_RESERVED81,
  149. PERIPH_ID_RESERVED82,
  150. PERIPH_ID_RESERVED83,
  151. PERIPH_ID_IRAMA,
  152. PERIPH_ID_IRAMB,
  153. PERIPH_ID_IRAMC,
  154. PERIPH_ID_IRAMD,
  155. /* 88 */
  156. PERIPH_ID_CRAM2,
  157. PERIPH_ID_COUNT,
  158. PERIPH_ID_NONE = -1,
  159. };
  160. /* Converts a clock number to a clock register: 0=L, 1=H, 2=U */
  161. #define PERIPH_REG(id) ((id) >> 5)
  162. /* Mask value for a clock (within PERIPH_REG(id)) */
  163. #define PERIPH_MASK(id) (1 << ((id) & 0x1f))
  164. /* return 1 if a PLL ID is in range, and not a simple PLL */
  165. #define clock_id_is_pll(id) ((id) >= CLOCK_ID_FIRST && \
  166. (id) < CLOCK_ID_FIRST_SIMPLE)
  167. /* PLL stabilization delay in usec */
  168. #define CLOCK_PLL_STABLE_DELAY_US 300
  169. /* return the current oscillator clock frequency */
  170. enum clock_osc_freq clock_get_osc_freq(void);
  171. /**
  172. * Start PLL using the provided configuration parameters.
  173. *
  174. * @param id clock id
  175. * @param divm input divider
  176. * @param divn feedback divider
  177. * @param divp post divider 2^n
  178. * @param cpcon charge pump setup control
  179. * @param lfcon loop filter setup control
  180. *
  181. * @returns monotonic time in us that the PLL will be stable
  182. */
  183. unsigned long clock_start_pll(enum clock_id id, u32 divm, u32 divn,
  184. u32 divp, u32 cpcon, u32 lfcon);
  185. /**
  186. * Read low-level parameters of a PLL.
  187. *
  188. * @param id clock id to read (note: USB is not supported)
  189. * @param divm returns input divider
  190. * @param divn returns feedback divider
  191. * @param divp returns post divider 2^n
  192. * @param cpcon returns charge pump setup control
  193. * @param lfcon returns loop filter setup control
  194. *
  195. * @returns 0 if ok, -1 on error (invalid clock id)
  196. */
  197. int clock_ll_read_pll(enum clock_id clkid, u32 *divm, u32 *divn,
  198. u32 *divp, u32 *cpcon, u32 *lfcon);
  199. /*
  200. * Enable a clock
  201. *
  202. * @param id clock id
  203. */
  204. void clock_enable(enum periph_id clkid);
  205. /*
  206. * Disable a clock
  207. *
  208. * @param id clock id
  209. */
  210. void clock_disable(enum periph_id clkid);
  211. /*
  212. * Set whether a clock is enabled or disabled.
  213. *
  214. * @param id clock id
  215. * @param enable 1 to enable, 0 to disable
  216. */
  217. void clock_set_enable(enum periph_id clkid, int enable);
  218. /**
  219. * Reset a peripheral. This puts it in reset, waits for a delay, then takes
  220. * it out of reset and waits for th delay again.
  221. *
  222. * @param periph_id peripheral to reset
  223. * @param us_delay time to delay in microseconds
  224. */
  225. void reset_periph(enum periph_id periph_id, int us_delay);
  226. /**
  227. * Put a peripheral into or out of reset.
  228. *
  229. * @param periph_id peripheral to reset
  230. * @param enable 1 to put into reset, 0 to take out of reset
  231. */
  232. void reset_set_enable(enum periph_id periph_id, int enable);
  233. /* CLK_RST_CONTROLLER_RST_CPU_CMPLX_SET/CLR_0 */
  234. enum crc_reset_id {
  235. /* Things we can hold in reset for each CPU */
  236. crc_rst_cpu = 1,
  237. crc_rst_de = 1 << 2, /* What is de? */
  238. crc_rst_watchdog = 1 << 3,
  239. crc_rst_debug = 1 << 4,
  240. };
  241. /**
  242. * Put parts of the CPU complex into or out of reset.\
  243. *
  244. * @param cpu cpu number (0 or 1 on Tegra2)
  245. * @param which which parts of the complex to affect (OR of crc_reset_id)
  246. * @param reset 1 to assert reset, 0 to de-assert
  247. */
  248. void reset_cmplx_set_enable(int cpu, int which, int reset);
  249. /**
  250. * Set the source for a peripheral clock. This plus the divisor sets the
  251. * clock rate. You need to look up the datasheet to see the meaning of the
  252. * source parameter as it changes for each peripheral.
  253. *
  254. * Warning: This function is only for use pre-relocation. Please use
  255. * clock_start_periph_pll() instead.
  256. *
  257. * @param periph_id peripheral to adjust
  258. * @param source source clock (0, 1, 2 or 3)
  259. */
  260. void clock_ll_set_source(enum periph_id periph_id, unsigned source);
  261. /**
  262. * Set the source and divisor for a peripheral clock. This sets the
  263. * clock rate. You need to look up the datasheet to see the meaning of the
  264. * source parameter as it changes for each peripheral.
  265. *
  266. * Warning: This function is only for use pre-relocation. Please use
  267. * clock_start_periph_pll() instead.
  268. *
  269. * @param periph_id peripheral to adjust
  270. * @param source source clock (0, 1, 2 or 3)
  271. * @param divisor divisor value to use
  272. */
  273. void clock_ll_set_source_divisor(enum periph_id periph_id, unsigned source,
  274. unsigned divisor);
  275. /**
  276. * Start a peripheral PLL clock at the given rate. This also resets the
  277. * peripheral.
  278. *
  279. * @param periph_id peripheral to start
  280. * @param parent PLL id of required parent clock
  281. * @param rate Required clock rate in Hz
  282. * @return rate selected in Hz, or -1U if something went wrong
  283. */
  284. unsigned clock_start_periph_pll(enum periph_id periph_id,
  285. enum clock_id parent, unsigned rate);
  286. /**
  287. * Returns the rate of a peripheral clock in Hz. Since the caller almost
  288. * certainly knows the parent clock (having just set it) we require that
  289. * this be passed in so we don't need to work it out.
  290. *
  291. * @param periph_id peripheral to start
  292. * @param parent PLL id of parent clock (used to calculate rate, you
  293. * must know this!)
  294. * @return clock rate of peripheral in Hz
  295. */
  296. unsigned long clock_get_periph_rate(enum periph_id periph_id,
  297. enum clock_id parent);
  298. /**
  299. * Adjust peripheral PLL clock to the given rate. This does not reset the
  300. * peripheral. If a second stage divisor is not available, pass NULL for
  301. * extra_div. If it is available, then this parameter will return the
  302. * divisor selected (which will be a power of 2 from 1 to 256).
  303. *
  304. * @param periph_id peripheral to start
  305. * @param parent PLL id of required parent clock
  306. * @param rate Required clock rate in Hz
  307. * @param extra_div value for the second-stage divisor (NULL if one is
  308. not available)
  309. * @return rate selected in Hz, or -1U if something went wrong
  310. */
  311. unsigned clock_adjust_periph_pll_div(enum periph_id periph_id,
  312. enum clock_id parent, unsigned rate, int *extra_div);
  313. /**
  314. * Returns the clock rate of a specified clock, in Hz.
  315. *
  316. * @param parent PLL id of clock to check
  317. * @return rate of clock in Hz
  318. */
  319. unsigned clock_get_rate(enum clock_id clkid);
  320. /**
  321. * Start up a UART using low-level calls
  322. *
  323. * Prior to relocation clock_start_periph_pll() cannot be called. This
  324. * function provides a way to set up a UART using low-level calls which
  325. * do not require BSS.
  326. *
  327. * @param periph_id Peripheral ID of UART to enable (e,g, PERIPH_ID_UART1)
  328. */
  329. void clock_ll_start_uart(enum periph_id periph_id);
  330. /**
  331. * Decode a peripheral ID from a device tree node.
  332. *
  333. * This works by looking up the peripheral's 'clocks' node and reading out
  334. * the second cell, which is the clock number / peripheral ID.
  335. *
  336. * @param blob FDT blob to use
  337. * @param node Node to look at
  338. * @return peripheral ID, or PERIPH_ID_NONE if none
  339. */
  340. enum periph_id clock_decode_periph_id(const void *blob, int node);
  341. /**
  342. * Checks if the oscillator bypass is enabled (XOBP bit)
  343. *
  344. * @return 1 if bypass is enabled, 0 if not
  345. */
  346. int clock_get_osc_bypass(void);
  347. /*
  348. * Checks that clocks are valid and prints a warning if not
  349. *
  350. * @return 0 if ok, -1 on error
  351. */
  352. int clock_verify(void);
  353. /* Initialize the clocks */
  354. void clock_init(void);
  355. /* Initialize the PLLs */
  356. void clock_early_init(void);
  357. #endif