clock.c 18 KB

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