clock.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751
  1. /*
  2. * Copyright (c) 2010-2015, 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. /* Tegra SoC common clock control functions */
  17. #include <common.h>
  18. #include <errno.h>
  19. #include <asm/io.h>
  20. #include <asm/arch/clock.h>
  21. #include <asm/arch/tegra.h>
  22. #include <asm/arch-tegra/ap.h>
  23. #include <asm/arch-tegra/clk_rst.h>
  24. #include <asm/arch-tegra/pmc.h>
  25. #include <asm/arch-tegra/timer.h>
  26. #include <div64.h>
  27. #include <fdtdec.h>
  28. /*
  29. * This is our record of the current clock rate of each clock. We don't
  30. * fill all of these in since we are only really interested in clocks which
  31. * we use as parents.
  32. */
  33. static unsigned pll_rate[CLOCK_ID_COUNT];
  34. /*
  35. * The oscillator frequency is fixed to one of four set values. Based on this
  36. * the other clocks are set up appropriately.
  37. */
  38. static unsigned osc_freq[CLOCK_OSC_FREQ_COUNT] = {
  39. 13000000,
  40. 19200000,
  41. 12000000,
  42. 26000000,
  43. 38400000,
  44. 48000000,
  45. };
  46. /* return 1 if a peripheral ID is in range */
  47. #define clock_type_id_isvalid(id) ((id) >= 0 && \
  48. (id) < CLOCK_TYPE_COUNT)
  49. char pllp_valid = 1; /* PLLP is set up correctly */
  50. /* return 1 if a periphc_internal_id is in range */
  51. #define periphc_internal_id_isvalid(id) ((id) >= 0 && \
  52. (id) < PERIPHC_COUNT)
  53. /* number of clock outputs of a PLL */
  54. static const u8 pll_num_clkouts[] = {
  55. 1, /* PLLC */
  56. 1, /* PLLM */
  57. 4, /* PLLP */
  58. 1, /* PLLA */
  59. 0, /* PLLU */
  60. 0, /* PLLD */
  61. };
  62. int clock_get_osc_bypass(void)
  63. {
  64. struct clk_rst_ctlr *clkrst =
  65. (struct clk_rst_ctlr *)NV_PA_CLK_RST_BASE;
  66. u32 reg;
  67. reg = readl(&clkrst->crc_osc_ctrl);
  68. return (reg & OSC_XOBP_MASK) >> OSC_XOBP_SHIFT;
  69. }
  70. /* Returns a pointer to the registers of the given pll */
  71. static struct clk_pll *get_pll(enum clock_id clkid)
  72. {
  73. struct clk_rst_ctlr *clkrst =
  74. (struct clk_rst_ctlr *)NV_PA_CLK_RST_BASE;
  75. assert(clock_id_is_pll(clkid));
  76. if (clkid >= (enum clock_id)TEGRA_CLK_PLLS) {
  77. debug("%s: Invalid PLL %d\n", __func__, clkid);
  78. return NULL;
  79. }
  80. return &clkrst->crc_pll[clkid];
  81. }
  82. __weak struct clk_pll_simple *clock_get_simple_pll(enum clock_id clkid)
  83. {
  84. return NULL;
  85. }
  86. int clock_ll_read_pll(enum clock_id clkid, u32 *divm, u32 *divn,
  87. u32 *divp, u32 *cpcon, u32 *lfcon)
  88. {
  89. struct clk_pll *pll = get_pll(clkid);
  90. struct clk_pll_info *pllinfo = &tegra_pll_info_table[clkid];
  91. u32 data;
  92. assert(clkid != CLOCK_ID_USB);
  93. /* Safety check, adds to code size but is small */
  94. if (!clock_id_is_pll(clkid) || clkid == CLOCK_ID_USB)
  95. return -1;
  96. data = readl(&pll->pll_base);
  97. *divm = (data >> pllinfo->m_shift) & pllinfo->m_mask;
  98. *divn = (data >> pllinfo->n_shift) & pllinfo->n_mask;
  99. *divp = (data >> pllinfo->p_shift) & pllinfo->p_mask;
  100. data = readl(&pll->pll_misc);
  101. /* NOTE: On T210, cpcon/lfcon no longer exist, moved to KCP/KVCO */
  102. *cpcon = (data >> pllinfo->kcp_shift) & pllinfo->kcp_mask;
  103. *lfcon = (data >> pllinfo->kvco_shift) & pllinfo->kvco_mask;
  104. return 0;
  105. }
  106. unsigned long clock_start_pll(enum clock_id clkid, u32 divm, u32 divn,
  107. u32 divp, u32 cpcon, u32 lfcon)
  108. {
  109. struct clk_pll *pll = NULL;
  110. struct clk_pll_info *pllinfo = &tegra_pll_info_table[clkid];
  111. struct clk_pll_simple *simple_pll = NULL;
  112. u32 misc_data, data;
  113. if (clkid < (enum clock_id)TEGRA_CLK_PLLS) {
  114. pll = get_pll(clkid);
  115. } else {
  116. simple_pll = clock_get_simple_pll(clkid);
  117. if (!simple_pll) {
  118. debug("%s: Uknown simple PLL %d\n", __func__, clkid);
  119. return 0;
  120. }
  121. }
  122. /*
  123. * pllinfo has the m/n/p and kcp/kvco mask and shift
  124. * values for all of the PLLs used in U-Boot, with any
  125. * SoC differences accounted for.
  126. *
  127. * Preserve EN_LOCKDET, etc.
  128. */
  129. if (pll)
  130. misc_data = readl(&pll->pll_misc);
  131. else
  132. misc_data = readl(&simple_pll->pll_misc);
  133. misc_data &= ~(pllinfo->kcp_mask << pllinfo->kcp_shift);
  134. misc_data |= cpcon << pllinfo->kcp_shift;
  135. misc_data &= ~(pllinfo->kvco_mask << pllinfo->kvco_shift);
  136. misc_data |= lfcon << pllinfo->kvco_shift;
  137. data = (divm << pllinfo->m_shift) | (divn << pllinfo->n_shift);
  138. data |= divp << pllinfo->p_shift;
  139. data |= (1 << PLL_ENABLE_SHIFT); /* BYPASS s/b 0 already */
  140. if (pll) {
  141. writel(misc_data, &pll->pll_misc);
  142. writel(data, &pll->pll_base);
  143. } else {
  144. writel(misc_data, &simple_pll->pll_misc);
  145. writel(data, &simple_pll->pll_base);
  146. }
  147. /* calculate the stable time */
  148. return timer_get_us() + CLOCK_PLL_STABLE_DELAY_US;
  149. }
  150. void clock_ll_set_source_divisor(enum periph_id periph_id, unsigned source,
  151. unsigned divisor)
  152. {
  153. u32 *reg = get_periph_source_reg(periph_id);
  154. u32 value;
  155. value = readl(reg);
  156. value &= ~OUT_CLK_SOURCE_31_30_MASK;
  157. value |= source << OUT_CLK_SOURCE_31_30_SHIFT;
  158. value &= ~OUT_CLK_DIVISOR_MASK;
  159. value |= divisor << OUT_CLK_DIVISOR_SHIFT;
  160. writel(value, reg);
  161. }
  162. int clock_ll_set_source_bits(enum periph_id periph_id, int mux_bits,
  163. unsigned source)
  164. {
  165. u32 *reg = get_periph_source_reg(periph_id);
  166. switch (mux_bits) {
  167. case MASK_BITS_31_30:
  168. clrsetbits_le32(reg, OUT_CLK_SOURCE_31_30_MASK,
  169. source << OUT_CLK_SOURCE_31_30_SHIFT);
  170. break;
  171. case MASK_BITS_31_29:
  172. clrsetbits_le32(reg, OUT_CLK_SOURCE_31_29_MASK,
  173. source << OUT_CLK_SOURCE_31_29_SHIFT);
  174. break;
  175. case MASK_BITS_31_28:
  176. clrsetbits_le32(reg, OUT_CLK_SOURCE_31_28_MASK,
  177. source << OUT_CLK_SOURCE_31_28_SHIFT);
  178. break;
  179. default:
  180. return -1;
  181. }
  182. return 0;
  183. }
  184. void clock_ll_set_source(enum periph_id periph_id, unsigned source)
  185. {
  186. clock_ll_set_source_bits(periph_id, MASK_BITS_31_30, source);
  187. }
  188. /**
  189. * Given the parent's rate and the required rate for the children, this works
  190. * out the peripheral clock divider to use, in 7.1 binary format.
  191. *
  192. * @param divider_bits number of divider bits (8 or 16)
  193. * @param parent_rate clock rate of parent clock in Hz
  194. * @param rate required clock rate for this clock
  195. * @return divider which should be used
  196. */
  197. static int clk_get_divider(unsigned divider_bits, unsigned long parent_rate,
  198. unsigned long rate)
  199. {
  200. u64 divider = parent_rate * 2;
  201. unsigned max_divider = 1 << divider_bits;
  202. divider += rate - 1;
  203. do_div(divider, rate);
  204. if ((s64)divider - 2 < 0)
  205. return 0;
  206. if ((s64)divider - 2 >= max_divider)
  207. return -1;
  208. return divider - 2;
  209. }
  210. int clock_set_pllout(enum clock_id clkid, enum pll_out_id pllout, unsigned rate)
  211. {
  212. struct clk_pll *pll = get_pll(clkid);
  213. int data = 0, div = 0, offset = 0;
  214. if (!clock_id_is_pll(clkid))
  215. return -1;
  216. if (pllout + 1 > pll_num_clkouts[clkid])
  217. return -1;
  218. div = clk_get_divider(8, pll_rate[clkid], rate);
  219. if (div < 0)
  220. return -1;
  221. /* out2 and out4 are in the high part of the register */
  222. if (pllout == PLL_OUT2 || pllout == PLL_OUT4)
  223. offset = 16;
  224. data = (div << PLL_OUT_RATIO_SHIFT) |
  225. PLL_OUT_OVRRIDE | PLL_OUT_CLKEN | PLL_OUT_RSTN;
  226. clrsetbits_le32(&pll->pll_out[pllout >> 1],
  227. PLL_OUT_RATIO_MASK << offset, data << offset);
  228. return 0;
  229. }
  230. /**
  231. * Given the parent's rate and the divider in 7.1 format, this works out the
  232. * resulting peripheral clock rate.
  233. *
  234. * @param parent_rate clock rate of parent clock in Hz
  235. * @param divider which should be used in 7.1 format
  236. * @return effective clock rate of peripheral
  237. */
  238. static unsigned long get_rate_from_divider(unsigned long parent_rate,
  239. int divider)
  240. {
  241. u64 rate;
  242. rate = (u64)parent_rate * 2;
  243. do_div(rate, divider + 2);
  244. return rate;
  245. }
  246. unsigned long clock_get_periph_rate(enum periph_id periph_id,
  247. enum clock_id parent)
  248. {
  249. u32 *reg = get_periph_source_reg(periph_id);
  250. return get_rate_from_divider(pll_rate[parent],
  251. (readl(reg) & OUT_CLK_DIVISOR_MASK) >> OUT_CLK_DIVISOR_SHIFT);
  252. }
  253. /**
  254. * Find the best available 7.1 format divisor given a parent clock rate and
  255. * required child clock rate. This function assumes that a second-stage
  256. * divisor is available which can divide by powers of 2 from 1 to 256.
  257. *
  258. * @param divider_bits number of divider bits (8 or 16)
  259. * @param parent_rate clock rate of parent clock in Hz
  260. * @param rate required clock rate for this clock
  261. * @param extra_div value for the second-stage divisor (not set if this
  262. * function returns -1.
  263. * @return divider which should be used, or -1 if nothing is valid
  264. *
  265. */
  266. static int find_best_divider(unsigned divider_bits, unsigned long parent_rate,
  267. unsigned long rate, int *extra_div)
  268. {
  269. int shift;
  270. int best_divider = -1;
  271. int best_error = rate;
  272. /* try dividers from 1 to 256 and find closest match */
  273. for (shift = 0; shift <= 8 && best_error > 0; shift++) {
  274. unsigned divided_parent = parent_rate >> shift;
  275. int divider = clk_get_divider(divider_bits, divided_parent,
  276. rate);
  277. unsigned effective_rate = get_rate_from_divider(divided_parent,
  278. divider);
  279. int error = rate - effective_rate;
  280. /* Given a valid divider, look for the lowest error */
  281. if (divider != -1 && error < best_error) {
  282. best_error = error;
  283. *extra_div = 1 << shift;
  284. best_divider = divider;
  285. }
  286. }
  287. /* return what we found - *extra_div will already be set */
  288. return best_divider;
  289. }
  290. /**
  291. * Adjust peripheral PLL to use the given divider and source.
  292. *
  293. * @param periph_id peripheral to adjust
  294. * @param source Source number (0-3 or 0-7)
  295. * @param mux_bits Number of mux bits (2 or 4)
  296. * @param divider Required divider in 7.1 or 15.1 format
  297. * @return 0 if ok, -1 on error (requesting a parent clock which is not valid
  298. * for this peripheral)
  299. */
  300. static int adjust_periph_pll(enum periph_id periph_id, int source,
  301. int mux_bits, unsigned divider)
  302. {
  303. u32 *reg = get_periph_source_reg(periph_id);
  304. clrsetbits_le32(reg, OUT_CLK_DIVISOR_MASK,
  305. divider << OUT_CLK_DIVISOR_SHIFT);
  306. udelay(1);
  307. /* work out the source clock and set it */
  308. if (source < 0)
  309. return -1;
  310. clock_ll_set_source_bits(periph_id, mux_bits, source);
  311. udelay(2);
  312. return 0;
  313. }
  314. unsigned clock_adjust_periph_pll_div(enum periph_id periph_id,
  315. enum clock_id parent, unsigned rate, int *extra_div)
  316. {
  317. unsigned effective_rate;
  318. int mux_bits, divider_bits, source;
  319. int divider;
  320. int xdiv = 0;
  321. /* work out the source clock and set it */
  322. source = get_periph_clock_source(periph_id, parent, &mux_bits,
  323. &divider_bits);
  324. divider = find_best_divider(divider_bits, pll_rate[parent],
  325. rate, &xdiv);
  326. if (extra_div)
  327. *extra_div = xdiv;
  328. assert(divider >= 0);
  329. if (adjust_periph_pll(periph_id, source, mux_bits, divider))
  330. return -1U;
  331. debug("periph %d, rate=%d, reg=%p = %x\n", periph_id, rate,
  332. get_periph_source_reg(periph_id),
  333. readl(get_periph_source_reg(periph_id)));
  334. /* Check what we ended up with. This shouldn't matter though */
  335. effective_rate = clock_get_periph_rate(periph_id, parent);
  336. if (extra_div)
  337. effective_rate /= *extra_div;
  338. if (rate != effective_rate)
  339. debug("Requested clock rate %u not honored (got %u)\n",
  340. rate, effective_rate);
  341. return effective_rate;
  342. }
  343. unsigned clock_start_periph_pll(enum periph_id periph_id,
  344. enum clock_id parent, unsigned rate)
  345. {
  346. unsigned effective_rate;
  347. reset_set_enable(periph_id, 1);
  348. clock_enable(periph_id);
  349. effective_rate = clock_adjust_periph_pll_div(periph_id, parent, rate,
  350. NULL);
  351. reset_set_enable(periph_id, 0);
  352. return effective_rate;
  353. }
  354. void clock_enable(enum periph_id clkid)
  355. {
  356. clock_set_enable(clkid, 1);
  357. }
  358. void clock_disable(enum periph_id clkid)
  359. {
  360. clock_set_enable(clkid, 0);
  361. }
  362. void reset_periph(enum periph_id periph_id, int us_delay)
  363. {
  364. /* Put peripheral into reset */
  365. reset_set_enable(periph_id, 1);
  366. udelay(us_delay);
  367. /* Remove reset */
  368. reset_set_enable(periph_id, 0);
  369. udelay(us_delay);
  370. }
  371. void reset_cmplx_set_enable(int cpu, int which, int reset)
  372. {
  373. struct clk_rst_ctlr *clkrst =
  374. (struct clk_rst_ctlr *)NV_PA_CLK_RST_BASE;
  375. u32 mask;
  376. /* Form the mask, which depends on the cpu chosen (2 or 4) */
  377. assert(cpu >= 0 && cpu < MAX_NUM_CPU);
  378. mask = which << cpu;
  379. /* either enable or disable those reset for that CPU */
  380. if (reset)
  381. writel(mask, &clkrst->crc_cpu_cmplx_set);
  382. else
  383. writel(mask, &clkrst->crc_cpu_cmplx_clr);
  384. }
  385. unsigned int __weak clk_m_get_rate(unsigned int parent_rate)
  386. {
  387. return parent_rate;
  388. }
  389. unsigned clock_get_rate(enum clock_id clkid)
  390. {
  391. struct clk_pll *pll;
  392. u32 base, divm;
  393. u64 parent_rate, rate;
  394. struct clk_pll_info *pllinfo = &tegra_pll_info_table[clkid];
  395. parent_rate = osc_freq[clock_get_osc_freq()];
  396. if (clkid == CLOCK_ID_OSC)
  397. return parent_rate;
  398. if (clkid == CLOCK_ID_CLK_M)
  399. return clk_m_get_rate(parent_rate);
  400. pll = get_pll(clkid);
  401. if (!pll)
  402. return 0;
  403. base = readl(&pll->pll_base);
  404. rate = parent_rate * ((base >> pllinfo->n_shift) & pllinfo->n_mask);
  405. divm = (base >> pllinfo->m_shift) & pllinfo->m_mask;
  406. /*
  407. * PLLU uses p_mask/p_shift for VCO on all but T210,
  408. * T210 uses normal DIVP. Handled in pllinfo table.
  409. */
  410. #ifdef CONFIG_TEGRA210
  411. /*
  412. * PLLP's primary output (pllP_out0) on T210 is the VCO, and divp is
  413. * not applied. pllP_out2 does have divp applied. All other pllP_outN
  414. * are divided down from pllP_out0. We only support pllP_out0 in
  415. * U-Boot at the time of writing this comment.
  416. */
  417. if (clkid != CLOCK_ID_PERIPH)
  418. #endif
  419. divm <<= (base >> pllinfo->p_shift) & pllinfo->p_mask;
  420. do_div(rate, divm);
  421. return rate;
  422. }
  423. /**
  424. * Set the output frequency you want for each PLL clock.
  425. * PLL output frequencies are programmed by setting their N, M and P values.
  426. * The governing equations are:
  427. * VCO = (Fi / m) * n, Fo = VCO / (2^p)
  428. * where Fo is the output frequency from the PLL.
  429. * Example: Set the output frequency to 216Mhz(Fo) with 12Mhz OSC(Fi)
  430. * 216Mhz = ((12Mhz / m) * n) / (2^p) so n=432,m=12,p=1
  431. * Please see Tegra TRM section 5.3 to get the detail for PLL Programming
  432. *
  433. * @param n PLL feedback divider(DIVN)
  434. * @param m PLL input divider(DIVN)
  435. * @param p post divider(DIVP)
  436. * @param cpcon base PLL charge pump(CPCON)
  437. * @return 0 if ok, -1 on error (the requested PLL is incorrect and cannot
  438. * be overriden), 1 if PLL is already correct
  439. */
  440. int clock_set_rate(enum clock_id clkid, u32 n, u32 m, u32 p, u32 cpcon)
  441. {
  442. u32 base_reg, misc_reg;
  443. struct clk_pll *pll;
  444. struct clk_pll_info *pllinfo = &tegra_pll_info_table[clkid];
  445. pll = get_pll(clkid);
  446. base_reg = readl(&pll->pll_base);
  447. /* Set BYPASS, m, n and p to PLL_BASE */
  448. base_reg &= ~(pllinfo->m_mask << pllinfo->m_shift);
  449. base_reg |= m << pllinfo->m_shift;
  450. base_reg &= ~(pllinfo->n_mask << pllinfo->n_shift);
  451. base_reg |= n << pllinfo->n_shift;
  452. base_reg &= ~(pllinfo->p_mask << pllinfo->p_shift);
  453. base_reg |= p << pllinfo->p_shift;
  454. if (clkid == CLOCK_ID_PERIPH) {
  455. /*
  456. * If the PLL is already set up, check that it is correct
  457. * and record this info for clock_verify() to check.
  458. */
  459. if (base_reg & PLL_BASE_OVRRIDE_MASK) {
  460. base_reg |= PLL_ENABLE_MASK;
  461. if (base_reg != readl(&pll->pll_base))
  462. pllp_valid = 0;
  463. return pllp_valid ? 1 : -1;
  464. }
  465. base_reg |= PLL_BASE_OVRRIDE_MASK;
  466. }
  467. base_reg |= PLL_BYPASS_MASK;
  468. writel(base_reg, &pll->pll_base);
  469. /* Set cpcon (KCP) to PLL_MISC */
  470. misc_reg = readl(&pll->pll_misc);
  471. misc_reg &= ~(pllinfo->kcp_mask << pllinfo->kcp_shift);
  472. misc_reg |= cpcon << pllinfo->kcp_shift;
  473. writel(misc_reg, &pll->pll_misc);
  474. /* Enable PLL */
  475. base_reg |= PLL_ENABLE_MASK;
  476. writel(base_reg, &pll->pll_base);
  477. /* Disable BYPASS */
  478. base_reg &= ~PLL_BYPASS_MASK;
  479. writel(base_reg, &pll->pll_base);
  480. return 0;
  481. }
  482. void clock_ll_start_uart(enum periph_id periph_id)
  483. {
  484. /* Assert UART reset and enable clock */
  485. reset_set_enable(periph_id, 1);
  486. clock_enable(periph_id);
  487. clock_ll_set_source(periph_id, 0); /* UARTx_CLK_SRC = 00, PLLP_OUT0 */
  488. /* wait for 2us */
  489. udelay(2);
  490. /* De-assert reset to UART */
  491. reset_set_enable(periph_id, 0);
  492. }
  493. #if CONFIG_IS_ENABLED(OF_CONTROL)
  494. int clock_decode_periph_id(const void *blob, int node)
  495. {
  496. enum periph_id id;
  497. u32 cell[2];
  498. int err;
  499. err = fdtdec_get_int_array(blob, node, "clocks", cell,
  500. ARRAY_SIZE(cell));
  501. if (err)
  502. return -1;
  503. id = clk_id_to_periph_id(cell[1]);
  504. assert(clock_periph_id_isvalid(id));
  505. return id;
  506. }
  507. #endif /* CONFIG_IS_ENABLED(OF_CONTROL) */
  508. int clock_verify(void)
  509. {
  510. struct clk_pll *pll = get_pll(CLOCK_ID_PERIPH);
  511. u32 reg = readl(&pll->pll_base);
  512. if (!pllp_valid) {
  513. printf("Warning: PLLP %x is not correct\n", reg);
  514. return -1;
  515. }
  516. debug("PLLP %x is correct\n", reg);
  517. return 0;
  518. }
  519. void clock_init(void)
  520. {
  521. pll_rate[CLOCK_ID_CGENERAL] = clock_get_rate(CLOCK_ID_CGENERAL);
  522. pll_rate[CLOCK_ID_MEMORY] = clock_get_rate(CLOCK_ID_MEMORY);
  523. pll_rate[CLOCK_ID_PERIPH] = clock_get_rate(CLOCK_ID_PERIPH);
  524. pll_rate[CLOCK_ID_USB] = clock_get_rate(CLOCK_ID_USB);
  525. pll_rate[CLOCK_ID_DISPLAY] = clock_get_rate(CLOCK_ID_DISPLAY);
  526. pll_rate[CLOCK_ID_XCPU] = clock_get_rate(CLOCK_ID_XCPU);
  527. pll_rate[CLOCK_ID_SFROM32KHZ] = 32768;
  528. pll_rate[CLOCK_ID_OSC] = clock_get_rate(CLOCK_ID_OSC);
  529. pll_rate[CLOCK_ID_CLK_M] = clock_get_rate(CLOCK_ID_CLK_M);
  530. debug("Osc = %d\n", pll_rate[CLOCK_ID_OSC]);
  531. debug("CLKM = %d\n", pll_rate[CLOCK_ID_CLK_M]);
  532. debug("PLLC = %d\n", pll_rate[CLOCK_ID_CGENERAL]);
  533. debug("PLLM = %d\n", pll_rate[CLOCK_ID_MEMORY]);
  534. debug("PLLP = %d\n", pll_rate[CLOCK_ID_PERIPH]);
  535. debug("PLLU = %d\n", pll_rate[CLOCK_ID_USB]);
  536. debug("PLLD = %d\n", pll_rate[CLOCK_ID_DISPLAY]);
  537. debug("PLLX = %d\n", pll_rate[CLOCK_ID_XCPU]);
  538. }
  539. static void set_avp_clock_source(u32 src)
  540. {
  541. struct clk_rst_ctlr *clkrst =
  542. (struct clk_rst_ctlr *)NV_PA_CLK_RST_BASE;
  543. u32 val;
  544. val = (src << SCLK_SWAKEUP_FIQ_SOURCE_SHIFT) |
  545. (src << SCLK_SWAKEUP_IRQ_SOURCE_SHIFT) |
  546. (src << SCLK_SWAKEUP_RUN_SOURCE_SHIFT) |
  547. (src << SCLK_SWAKEUP_IDLE_SOURCE_SHIFT) |
  548. (SCLK_SYS_STATE_RUN << SCLK_SYS_STATE_SHIFT);
  549. writel(val, &clkrst->crc_sclk_brst_pol);
  550. udelay(3);
  551. }
  552. /*
  553. * This function is useful on Tegra30, and any later SoCs that have compatible
  554. * PLLP configuration registers.
  555. * NOTE: Not used on Tegra210 - see tegra210_setup_pllp in T210 clock.c
  556. */
  557. void tegra30_set_up_pllp(void)
  558. {
  559. struct clk_rst_ctlr *clkrst = (struct clk_rst_ctlr *)NV_PA_CLK_RST_BASE;
  560. u32 reg;
  561. /*
  562. * Based on the Tegra TRM, the system clock (which is the AVP clock) can
  563. * run up to 275MHz. On power on, the default sytem clock source is set
  564. * to PLLP_OUT0. This function sets PLLP's (hence PLLP_OUT0's) rate to
  565. * 408MHz which is beyond system clock's upper limit.
  566. *
  567. * The fix is to set the system clock to CLK_M before initializing PLLP,
  568. * and then switch back to PLLP_OUT4, which has an appropriate divider
  569. * configured, after PLLP has been configured
  570. */
  571. set_avp_clock_source(SCLK_SOURCE_CLKM);
  572. /*
  573. * PLLP output frequency set to 408Mhz
  574. * PLLC output frequency set to 228Mhz
  575. */
  576. switch (clock_get_osc_freq()) {
  577. case CLOCK_OSC_FREQ_12_0: /* OSC is 12Mhz */
  578. clock_set_rate(CLOCK_ID_PERIPH, 408, 12, 0, 8);
  579. clock_set_rate(CLOCK_ID_CGENERAL, 456, 12, 1, 8);
  580. break;
  581. case CLOCK_OSC_FREQ_26_0: /* OSC is 26Mhz */
  582. clock_set_rate(CLOCK_ID_PERIPH, 408, 26, 0, 8);
  583. clock_set_rate(CLOCK_ID_CGENERAL, 600, 26, 0, 8);
  584. break;
  585. case CLOCK_OSC_FREQ_13_0: /* OSC is 13Mhz */
  586. clock_set_rate(CLOCK_ID_PERIPH, 408, 13, 0, 8);
  587. clock_set_rate(CLOCK_ID_CGENERAL, 600, 13, 0, 8);
  588. break;
  589. case CLOCK_OSC_FREQ_19_2:
  590. default:
  591. /*
  592. * These are not supported. It is too early to print a
  593. * message and the UART likely won't work anyway due to the
  594. * oscillator being wrong.
  595. */
  596. break;
  597. }
  598. /* Set PLLP_OUT1, 2, 3 & 4 freqs to 9.6, 48, 102 & 204MHz */
  599. /* OUT1, 2 */
  600. /* Assert RSTN before enable */
  601. reg = PLLP_OUT2_RSTN_EN | PLLP_OUT1_RSTN_EN;
  602. writel(reg, &clkrst->crc_pll[CLOCK_ID_PERIPH].pll_out[0]);
  603. /* Set divisor and reenable */
  604. reg = (IN_408_OUT_48_DIVISOR << PLLP_OUT2_RATIO)
  605. | PLLP_OUT2_OVR | PLLP_OUT2_CLKEN | PLLP_OUT2_RSTN_DIS
  606. | (IN_408_OUT_9_6_DIVISOR << PLLP_OUT1_RATIO)
  607. | PLLP_OUT1_OVR | PLLP_OUT1_CLKEN | PLLP_OUT1_RSTN_DIS;
  608. writel(reg, &clkrst->crc_pll[CLOCK_ID_PERIPH].pll_out[0]);
  609. /* OUT3, 4 */
  610. /* Assert RSTN before enable */
  611. reg = PLLP_OUT4_RSTN_EN | PLLP_OUT3_RSTN_EN;
  612. writel(reg, &clkrst->crc_pll[CLOCK_ID_PERIPH].pll_out[1]);
  613. /* Set divisor and reenable */
  614. reg = (IN_408_OUT_204_DIVISOR << PLLP_OUT4_RATIO)
  615. | PLLP_OUT4_OVR | PLLP_OUT4_CLKEN | PLLP_OUT4_RSTN_DIS
  616. | (IN_408_OUT_102_DIVISOR << PLLP_OUT3_RATIO)
  617. | PLLP_OUT3_OVR | PLLP_OUT3_CLKEN | PLLP_OUT3_RSTN_DIS;
  618. writel(reg, &clkrst->crc_pll[CLOCK_ID_PERIPH].pll_out[1]);
  619. set_avp_clock_source(SCLK_SOURCE_PLLP_OUT4);
  620. }
  621. int clock_external_output(int clk_id)
  622. {
  623. struct pmc_ctlr *pmc = (struct pmc_ctlr *)NV_PA_PMC_BASE;
  624. if (clk_id >= 1 && clk_id <= 3) {
  625. setbits_le32(&pmc->pmc_clk_out_cntrl,
  626. 1 << (2 + (clk_id - 1) * 8));
  627. } else {
  628. printf("%s: Unknown output clock id %d\n", __func__, clk_id);
  629. return -EINVAL;
  630. }
  631. return 0;
  632. }