clock.c 21 KB

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