clock.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827
  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. unsigned parent_rate = pll_rate[parent];
  262. int div = (readl(reg) & OUT_CLK_DIVISOR_MASK) >> OUT_CLK_DIVISOR_SHIFT;
  263. switch (periph_id) {
  264. case PERIPH_ID_UART1:
  265. case PERIPH_ID_UART2:
  266. case PERIPH_ID_UART3:
  267. case PERIPH_ID_UART4:
  268. case PERIPH_ID_UART5:
  269. #ifdef CONFIG_TEGRA20
  270. /* There's no divider for these clocks in this SoC. */
  271. return parent_rate;
  272. #else
  273. /*
  274. * This undoes the +2 in get_rate_from_divider() which I
  275. * believe is incorrect. Ideally we would fix
  276. * get_rate_from_divider(), but... Removing the +2 from
  277. * get_rate_from_divider() would probably require remove the -2
  278. * from the tail of clk_get_divider() since I believe that's
  279. * only there to invert get_rate_from_divider()'s +2. Observe
  280. * how find_best_divider() uses those two functions together.
  281. * However, doing so breaks other stuff, such as Seaboard's
  282. * display, likely due to clock_set_pllout()'s call to
  283. * clk_get_divider(). Attempting to fix that by making
  284. * clock_set_pllout() subtract 2 from clk_get_divider()'s
  285. * return value doesn't help. In summary this clock driver is
  286. * quite broken but I'm afraid I have no idea how to fix it
  287. * without completely replacing it.
  288. */
  289. div -= 2;
  290. break;
  291. #endif
  292. default:
  293. break;
  294. }
  295. return get_rate_from_divider(parent_rate, div);
  296. }
  297. /**
  298. * Find the best available 7.1 format divisor given a parent clock rate and
  299. * required child clock rate. This function assumes that a second-stage
  300. * divisor is available which can divide by powers of 2 from 1 to 256.
  301. *
  302. * @param divider_bits number of divider bits (8 or 16)
  303. * @param parent_rate clock rate of parent clock in Hz
  304. * @param rate required clock rate for this clock
  305. * @param extra_div value for the second-stage divisor (not set if this
  306. * function returns -1.
  307. * @return divider which should be used, or -1 if nothing is valid
  308. *
  309. */
  310. static int find_best_divider(unsigned divider_bits, unsigned long parent_rate,
  311. unsigned long rate, int *extra_div)
  312. {
  313. int shift;
  314. int best_divider = -1;
  315. int best_error = rate;
  316. /* try dividers from 1 to 256 and find closest match */
  317. for (shift = 0; shift <= 8 && best_error > 0; shift++) {
  318. unsigned divided_parent = parent_rate >> shift;
  319. int divider = clk_get_divider(divider_bits, divided_parent,
  320. rate);
  321. unsigned effective_rate = get_rate_from_divider(divided_parent,
  322. divider);
  323. int error = rate - effective_rate;
  324. /* Given a valid divider, look for the lowest error */
  325. if (divider != -1 && error < best_error) {
  326. best_error = error;
  327. *extra_div = 1 << shift;
  328. best_divider = divider;
  329. }
  330. }
  331. /* return what we found - *extra_div will already be set */
  332. return best_divider;
  333. }
  334. /**
  335. * Adjust peripheral PLL to use the given divider and source.
  336. *
  337. * @param periph_id peripheral to adjust
  338. * @param source Source number (0-3 or 0-7)
  339. * @param mux_bits Number of mux bits (2 or 4)
  340. * @param divider Required divider in 7.1 or 15.1 format
  341. * @return 0 if ok, -1 on error (requesting a parent clock which is not valid
  342. * for this peripheral)
  343. */
  344. static int adjust_periph_pll(enum periph_id periph_id, int source,
  345. int mux_bits, unsigned divider)
  346. {
  347. u32 *reg = get_periph_source_reg(periph_id);
  348. clrsetbits_le32(reg, OUT_CLK_DIVISOR_MASK,
  349. divider << OUT_CLK_DIVISOR_SHIFT);
  350. udelay(1);
  351. /* work out the source clock and set it */
  352. if (source < 0)
  353. return -1;
  354. clock_ll_set_source_bits(periph_id, mux_bits, source);
  355. udelay(2);
  356. return 0;
  357. }
  358. enum clock_id clock_get_periph_parent(enum periph_id periph_id)
  359. {
  360. int err, mux_bits, divider_bits, type;
  361. int source;
  362. err = get_periph_clock_info(periph_id, &mux_bits, &divider_bits, &type);
  363. if (err)
  364. return CLOCK_ID_NONE;
  365. source = clock_ll_get_source_bits(periph_id, mux_bits);
  366. return get_periph_clock_id(periph_id, source);
  367. }
  368. unsigned clock_adjust_periph_pll_div(enum periph_id periph_id,
  369. enum clock_id parent, unsigned rate, int *extra_div)
  370. {
  371. unsigned effective_rate;
  372. int mux_bits, divider_bits, source;
  373. int divider;
  374. int xdiv = 0;
  375. /* work out the source clock and set it */
  376. source = get_periph_clock_source(periph_id, parent, &mux_bits,
  377. &divider_bits);
  378. divider = find_best_divider(divider_bits, pll_rate[parent],
  379. rate, &xdiv);
  380. if (extra_div)
  381. *extra_div = xdiv;
  382. assert(divider >= 0);
  383. if (adjust_periph_pll(periph_id, source, mux_bits, divider))
  384. return -1U;
  385. debug("periph %d, rate=%d, reg=%p = %x\n", periph_id, rate,
  386. get_periph_source_reg(periph_id),
  387. readl(get_periph_source_reg(periph_id)));
  388. /* Check what we ended up with. This shouldn't matter though */
  389. effective_rate = clock_get_periph_rate(periph_id, parent);
  390. if (extra_div)
  391. effective_rate /= *extra_div;
  392. if (rate != effective_rate)
  393. debug("Requested clock rate %u not honored (got %u)\n",
  394. rate, effective_rate);
  395. return effective_rate;
  396. }
  397. unsigned clock_start_periph_pll(enum periph_id periph_id,
  398. enum clock_id parent, unsigned rate)
  399. {
  400. unsigned effective_rate;
  401. reset_set_enable(periph_id, 1);
  402. clock_enable(periph_id);
  403. effective_rate = clock_adjust_periph_pll_div(periph_id, parent, rate,
  404. NULL);
  405. reset_set_enable(periph_id, 0);
  406. return effective_rate;
  407. }
  408. void clock_enable(enum periph_id clkid)
  409. {
  410. clock_set_enable(clkid, 1);
  411. }
  412. void clock_disable(enum periph_id clkid)
  413. {
  414. clock_set_enable(clkid, 0);
  415. }
  416. void reset_periph(enum periph_id periph_id, int us_delay)
  417. {
  418. /* Put peripheral into reset */
  419. reset_set_enable(periph_id, 1);
  420. udelay(us_delay);
  421. /* Remove reset */
  422. reset_set_enable(periph_id, 0);
  423. udelay(us_delay);
  424. }
  425. void reset_cmplx_set_enable(int cpu, int which, int reset)
  426. {
  427. struct clk_rst_ctlr *clkrst =
  428. (struct clk_rst_ctlr *)NV_PA_CLK_RST_BASE;
  429. u32 mask;
  430. /* Form the mask, which depends on the cpu chosen (2 or 4) */
  431. assert(cpu >= 0 && cpu < MAX_NUM_CPU);
  432. mask = which << cpu;
  433. /* either enable or disable those reset for that CPU */
  434. if (reset)
  435. writel(mask, &clkrst->crc_cpu_cmplx_set);
  436. else
  437. writel(mask, &clkrst->crc_cpu_cmplx_clr);
  438. }
  439. unsigned int __weak clk_m_get_rate(unsigned int parent_rate)
  440. {
  441. return parent_rate;
  442. }
  443. unsigned clock_get_rate(enum clock_id clkid)
  444. {
  445. struct clk_pll *pll;
  446. u32 base, divm;
  447. u64 parent_rate, rate;
  448. struct clk_pll_info *pllinfo = &tegra_pll_info_table[clkid];
  449. parent_rate = osc_freq[clock_get_osc_freq()];
  450. if (clkid == CLOCK_ID_OSC)
  451. return parent_rate;
  452. if (clkid == CLOCK_ID_CLK_M)
  453. return clk_m_get_rate(parent_rate);
  454. pll = get_pll(clkid);
  455. if (!pll)
  456. return 0;
  457. base = readl(&pll->pll_base);
  458. rate = parent_rate * ((base >> pllinfo->n_shift) & pllinfo->n_mask);
  459. divm = (base >> pllinfo->m_shift) & pllinfo->m_mask;
  460. /*
  461. * PLLU uses p_mask/p_shift for VCO on all but T210,
  462. * T210 uses normal DIVP. Handled in pllinfo table.
  463. */
  464. #ifdef CONFIG_TEGRA210
  465. /*
  466. * PLLP's primary output (pllP_out0) on T210 is the VCO, and divp is
  467. * not applied. pllP_out2 does have divp applied. All other pllP_outN
  468. * are divided down from pllP_out0. We only support pllP_out0 in
  469. * U-Boot at the time of writing this comment.
  470. */
  471. if (clkid != CLOCK_ID_PERIPH)
  472. #endif
  473. divm <<= (base >> pllinfo->p_shift) & pllinfo->p_mask;
  474. do_div(rate, divm);
  475. return rate;
  476. }
  477. /**
  478. * Set the output frequency you want for each PLL clock.
  479. * PLL output frequencies are programmed by setting their N, M and P values.
  480. * The governing equations are:
  481. * VCO = (Fi / m) * n, Fo = VCO / (2^p)
  482. * where Fo is the output frequency from the PLL.
  483. * Example: Set the output frequency to 216Mhz(Fo) with 12Mhz OSC(Fi)
  484. * 216Mhz = ((12Mhz / m) * n) / (2^p) so n=432,m=12,p=1
  485. * Please see Tegra TRM section 5.3 to get the detail for PLL Programming
  486. *
  487. * @param n PLL feedback divider(DIVN)
  488. * @param m PLL input divider(DIVN)
  489. * @param p post divider(DIVP)
  490. * @param cpcon base PLL charge pump(CPCON)
  491. * @return 0 if ok, -1 on error (the requested PLL is incorrect and cannot
  492. * be overridden), 1 if PLL is already correct
  493. */
  494. int clock_set_rate(enum clock_id clkid, u32 n, u32 m, u32 p, u32 cpcon)
  495. {
  496. u32 base_reg, misc_reg;
  497. struct clk_pll *pll;
  498. struct clk_pll_info *pllinfo = &tegra_pll_info_table[clkid];
  499. pll = get_pll(clkid);
  500. base_reg = readl(&pll->pll_base);
  501. /* Set BYPASS, m, n and p to PLL_BASE */
  502. base_reg &= ~(pllinfo->m_mask << pllinfo->m_shift);
  503. base_reg |= m << pllinfo->m_shift;
  504. base_reg &= ~(pllinfo->n_mask << pllinfo->n_shift);
  505. base_reg |= n << pllinfo->n_shift;
  506. base_reg &= ~(pllinfo->p_mask << pllinfo->p_shift);
  507. base_reg |= p << pllinfo->p_shift;
  508. if (clkid == CLOCK_ID_PERIPH) {
  509. /*
  510. * If the PLL is already set up, check that it is correct
  511. * and record this info for clock_verify() to check.
  512. */
  513. if (base_reg & PLL_BASE_OVRRIDE_MASK) {
  514. base_reg |= PLL_ENABLE_MASK;
  515. if (base_reg != readl(&pll->pll_base))
  516. pllp_valid = 0;
  517. return pllp_valid ? 1 : -1;
  518. }
  519. base_reg |= PLL_BASE_OVRRIDE_MASK;
  520. }
  521. base_reg |= PLL_BYPASS_MASK;
  522. writel(base_reg, &pll->pll_base);
  523. /* Set cpcon (KCP) to PLL_MISC */
  524. misc_reg = readl(&pll->pll_misc);
  525. misc_reg &= ~(pllinfo->kcp_mask << pllinfo->kcp_shift);
  526. misc_reg |= cpcon << pllinfo->kcp_shift;
  527. writel(misc_reg, &pll->pll_misc);
  528. /* Enable PLL */
  529. base_reg |= PLL_ENABLE_MASK;
  530. writel(base_reg, &pll->pll_base);
  531. /* Disable BYPASS */
  532. base_reg &= ~PLL_BYPASS_MASK;
  533. writel(base_reg, &pll->pll_base);
  534. return 0;
  535. }
  536. void clock_ll_start_uart(enum periph_id periph_id)
  537. {
  538. /* Assert UART reset and enable clock */
  539. reset_set_enable(periph_id, 1);
  540. clock_enable(periph_id);
  541. clock_ll_set_source(periph_id, 0); /* UARTx_CLK_SRC = 00, PLLP_OUT0 */
  542. /* wait for 2us */
  543. udelay(2);
  544. /* De-assert reset to UART */
  545. reset_set_enable(periph_id, 0);
  546. }
  547. #if CONFIG_IS_ENABLED(OF_CONTROL)
  548. int clock_decode_periph_id(const void *blob, int node)
  549. {
  550. enum periph_id id;
  551. u32 cell[2];
  552. int err;
  553. err = fdtdec_get_int_array(blob, node, "clocks", cell,
  554. ARRAY_SIZE(cell));
  555. if (err)
  556. return -1;
  557. id = clk_id_to_periph_id(cell[1]);
  558. assert(clock_periph_id_isvalid(id));
  559. return id;
  560. }
  561. #endif /* CONFIG_IS_ENABLED(OF_CONTROL) */
  562. int clock_verify(void)
  563. {
  564. struct clk_pll *pll = get_pll(CLOCK_ID_PERIPH);
  565. u32 reg = readl(&pll->pll_base);
  566. if (!pllp_valid) {
  567. printf("Warning: PLLP %x is not correct\n", reg);
  568. return -1;
  569. }
  570. debug("PLLP %x is correct\n", reg);
  571. return 0;
  572. }
  573. void clock_init(void)
  574. {
  575. int i;
  576. pll_rate[CLOCK_ID_CGENERAL] = clock_get_rate(CLOCK_ID_CGENERAL);
  577. pll_rate[CLOCK_ID_MEMORY] = clock_get_rate(CLOCK_ID_MEMORY);
  578. pll_rate[CLOCK_ID_PERIPH] = clock_get_rate(CLOCK_ID_PERIPH);
  579. pll_rate[CLOCK_ID_USB] = clock_get_rate(CLOCK_ID_USB);
  580. pll_rate[CLOCK_ID_DISPLAY] = clock_get_rate(CLOCK_ID_DISPLAY);
  581. pll_rate[CLOCK_ID_XCPU] = clock_get_rate(CLOCK_ID_XCPU);
  582. pll_rate[CLOCK_ID_SFROM32KHZ] = 32768;
  583. pll_rate[CLOCK_ID_OSC] = clock_get_rate(CLOCK_ID_OSC);
  584. pll_rate[CLOCK_ID_CLK_M] = clock_get_rate(CLOCK_ID_CLK_M);
  585. debug("Osc = %d\n", pll_rate[CLOCK_ID_OSC]);
  586. debug("CLKM = %d\n", pll_rate[CLOCK_ID_CLK_M]);
  587. debug("PLLC = %d\n", pll_rate[CLOCK_ID_CGENERAL]);
  588. debug("PLLM = %d\n", pll_rate[CLOCK_ID_MEMORY]);
  589. debug("PLLP = %d\n", pll_rate[CLOCK_ID_PERIPH]);
  590. debug("PLLU = %d\n", pll_rate[CLOCK_ID_USB]);
  591. debug("PLLD = %d\n", pll_rate[CLOCK_ID_DISPLAY]);
  592. debug("PLLX = %d\n", pll_rate[CLOCK_ID_XCPU]);
  593. for (i = 0; periph_clk_init_table[i].periph_id != -1; i++) {
  594. enum periph_id periph_id;
  595. enum clock_id parent;
  596. int source, mux_bits, divider_bits;
  597. periph_id = periph_clk_init_table[i].periph_id;
  598. parent = periph_clk_init_table[i].parent_clock_id;
  599. source = get_periph_clock_source(periph_id, parent, &mux_bits,
  600. &divider_bits);
  601. clock_ll_set_source_bits(periph_id, mux_bits, source);
  602. }
  603. }
  604. static void set_avp_clock_source(u32 src)
  605. {
  606. struct clk_rst_ctlr *clkrst =
  607. (struct clk_rst_ctlr *)NV_PA_CLK_RST_BASE;
  608. u32 val;
  609. val = (src << SCLK_SWAKEUP_FIQ_SOURCE_SHIFT) |
  610. (src << SCLK_SWAKEUP_IRQ_SOURCE_SHIFT) |
  611. (src << SCLK_SWAKEUP_RUN_SOURCE_SHIFT) |
  612. (src << SCLK_SWAKEUP_IDLE_SOURCE_SHIFT) |
  613. (SCLK_SYS_STATE_RUN << SCLK_SYS_STATE_SHIFT);
  614. writel(val, &clkrst->crc_sclk_brst_pol);
  615. udelay(3);
  616. }
  617. /*
  618. * This function is useful on Tegra30, and any later SoCs that have compatible
  619. * PLLP configuration registers.
  620. * NOTE: Not used on Tegra210 - see tegra210_setup_pllp in T210 clock.c
  621. */
  622. void tegra30_set_up_pllp(void)
  623. {
  624. struct clk_rst_ctlr *clkrst = (struct clk_rst_ctlr *)NV_PA_CLK_RST_BASE;
  625. u32 reg;
  626. /*
  627. * Based on the Tegra TRM, the system clock (which is the AVP clock) can
  628. * run up to 275MHz. On power on, the default sytem clock source is set
  629. * to PLLP_OUT0. This function sets PLLP's (hence PLLP_OUT0's) rate to
  630. * 408MHz which is beyond system clock's upper limit.
  631. *
  632. * The fix is to set the system clock to CLK_M before initializing PLLP,
  633. * and then switch back to PLLP_OUT4, which has an appropriate divider
  634. * configured, after PLLP has been configured
  635. */
  636. set_avp_clock_source(SCLK_SOURCE_CLKM);
  637. /*
  638. * PLLP output frequency set to 408Mhz
  639. * PLLC output frequency set to 228Mhz
  640. */
  641. switch (clock_get_osc_freq()) {
  642. case CLOCK_OSC_FREQ_12_0: /* OSC is 12Mhz */
  643. clock_set_rate(CLOCK_ID_PERIPH, 408, 12, 0, 8);
  644. clock_set_rate(CLOCK_ID_CGENERAL, 456, 12, 1, 8);
  645. break;
  646. case CLOCK_OSC_FREQ_26_0: /* OSC is 26Mhz */
  647. clock_set_rate(CLOCK_ID_PERIPH, 408, 26, 0, 8);
  648. clock_set_rate(CLOCK_ID_CGENERAL, 600, 26, 0, 8);
  649. break;
  650. case CLOCK_OSC_FREQ_13_0: /* OSC is 13Mhz */
  651. clock_set_rate(CLOCK_ID_PERIPH, 408, 13, 0, 8);
  652. clock_set_rate(CLOCK_ID_CGENERAL, 600, 13, 0, 8);
  653. break;
  654. case CLOCK_OSC_FREQ_19_2:
  655. default:
  656. /*
  657. * These are not supported. It is too early to print a
  658. * message and the UART likely won't work anyway due to the
  659. * oscillator being wrong.
  660. */
  661. break;
  662. }
  663. /* Set PLLP_OUT1, 2, 3 & 4 freqs to 9.6, 48, 102 & 204MHz */
  664. /* OUT1, 2 */
  665. /* Assert RSTN before enable */
  666. reg = PLLP_OUT2_RSTN_EN | PLLP_OUT1_RSTN_EN;
  667. writel(reg, &clkrst->crc_pll[CLOCK_ID_PERIPH].pll_out[0]);
  668. /* Set divisor and reenable */
  669. reg = (IN_408_OUT_48_DIVISOR << PLLP_OUT2_RATIO)
  670. | PLLP_OUT2_OVR | PLLP_OUT2_CLKEN | PLLP_OUT2_RSTN_DIS
  671. | (IN_408_OUT_9_6_DIVISOR << PLLP_OUT1_RATIO)
  672. | PLLP_OUT1_OVR | PLLP_OUT1_CLKEN | PLLP_OUT1_RSTN_DIS;
  673. writel(reg, &clkrst->crc_pll[CLOCK_ID_PERIPH].pll_out[0]);
  674. /* OUT3, 4 */
  675. /* Assert RSTN before enable */
  676. reg = PLLP_OUT4_RSTN_EN | PLLP_OUT3_RSTN_EN;
  677. writel(reg, &clkrst->crc_pll[CLOCK_ID_PERIPH].pll_out[1]);
  678. /* Set divisor and reenable */
  679. reg = (IN_408_OUT_204_DIVISOR << PLLP_OUT4_RATIO)
  680. | PLLP_OUT4_OVR | PLLP_OUT4_CLKEN | PLLP_OUT4_RSTN_DIS
  681. | (IN_408_OUT_102_DIVISOR << PLLP_OUT3_RATIO)
  682. | PLLP_OUT3_OVR | PLLP_OUT3_CLKEN | PLLP_OUT3_RSTN_DIS;
  683. writel(reg, &clkrst->crc_pll[CLOCK_ID_PERIPH].pll_out[1]);
  684. set_avp_clock_source(SCLK_SOURCE_PLLP_OUT4);
  685. }
  686. int clock_external_output(int clk_id)
  687. {
  688. struct pmc_ctlr *pmc = (struct pmc_ctlr *)NV_PA_PMC_BASE;
  689. if (clk_id >= 1 && clk_id <= 3) {
  690. setbits_le32(&pmc->pmc_clk_out_cntrl,
  691. 1 << (2 + (clk_id - 1) * 8));
  692. } else {
  693. printf("%s: Unknown output clock id %d\n", __func__, clk_id);
  694. return -EINVAL;
  695. }
  696. return 0;
  697. }