clock.c 20 KB

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