clock.c 19 KB

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