clock.c 22 KB

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