clock.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  1. /*
  2. * Copyright (c) 2010-2013, 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 <asm/io.h>
  19. #include <asm/arch/clock.h>
  20. #include <asm/arch/tegra.h>
  21. #include <asm/arch-tegra/clk_rst.h>
  22. #include <asm/arch-tegra/timer.h>
  23. #include <div64.h>
  24. #include <fdtdec.h>
  25. /*
  26. * This is our record of the current clock rate of each clock. We don't
  27. * fill all of these in since we are only really interested in clocks which
  28. * we use as parents.
  29. */
  30. static unsigned pll_rate[CLOCK_ID_COUNT];
  31. /*
  32. * The oscillator frequency is fixed to one of four set values. Based on this
  33. * the other clocks are set up appropriately.
  34. */
  35. static unsigned osc_freq[CLOCK_OSC_FREQ_COUNT] = {
  36. 13000000,
  37. 19200000,
  38. 12000000,
  39. 26000000,
  40. };
  41. /* return 1 if a peripheral ID is in range */
  42. #define clock_type_id_isvalid(id) ((id) >= 0 && \
  43. (id) < CLOCK_TYPE_COUNT)
  44. char pllp_valid = 1; /* PLLP is set up correctly */
  45. /* return 1 if a periphc_internal_id is in range */
  46. #define periphc_internal_id_isvalid(id) ((id) >= 0 && \
  47. (id) < PERIPHC_COUNT)
  48. /* number of clock outputs of a PLL */
  49. static const u8 pll_num_clkouts[] = {
  50. 1, /* PLLC */
  51. 1, /* PLLM */
  52. 4, /* PLLP */
  53. 1, /* PLLA */
  54. 0, /* PLLU */
  55. 0, /* PLLD */
  56. };
  57. int clock_get_osc_bypass(void)
  58. {
  59. struct clk_rst_ctlr *clkrst =
  60. (struct clk_rst_ctlr *)NV_PA_CLK_RST_BASE;
  61. u32 reg;
  62. reg = readl(&clkrst->crc_osc_ctrl);
  63. return (reg & OSC_XOBP_MASK) >> OSC_XOBP_SHIFT;
  64. }
  65. /* Returns a pointer to the registers of the given pll */
  66. static struct clk_pll *get_pll(enum clock_id clkid)
  67. {
  68. struct clk_rst_ctlr *clkrst =
  69. (struct clk_rst_ctlr *)NV_PA_CLK_RST_BASE;
  70. assert(clock_id_is_pll(clkid));
  71. return &clkrst->crc_pll[clkid];
  72. }
  73. int clock_ll_read_pll(enum clock_id clkid, u32 *divm, u32 *divn,
  74. u32 *divp, u32 *cpcon, u32 *lfcon)
  75. {
  76. struct clk_pll *pll = get_pll(clkid);
  77. u32 data;
  78. assert(clkid != CLOCK_ID_USB);
  79. /* Safety check, adds to code size but is small */
  80. if (!clock_id_is_pll(clkid) || clkid == CLOCK_ID_USB)
  81. return -1;
  82. data = readl(&pll->pll_base);
  83. *divm = (data & PLL_DIVM_MASK) >> PLL_DIVM_SHIFT;
  84. *divn = (data & PLL_DIVN_MASK) >> PLL_DIVN_SHIFT;
  85. *divp = (data & PLL_DIVP_MASK) >> PLL_DIVP_SHIFT;
  86. data = readl(&pll->pll_misc);
  87. *cpcon = (data & PLL_CPCON_MASK) >> PLL_CPCON_SHIFT;
  88. *lfcon = (data & PLL_LFCON_MASK) >> PLL_LFCON_SHIFT;
  89. return 0;
  90. }
  91. unsigned long clock_start_pll(enum clock_id clkid, u32 divm, u32 divn,
  92. u32 divp, u32 cpcon, u32 lfcon)
  93. {
  94. struct clk_pll *pll = get_pll(clkid);
  95. u32 data;
  96. /*
  97. * We cheat by treating all PLL (except PLLU) in the same fashion.
  98. * This works only because:
  99. * - same fields are always mapped at same offsets, except DCCON
  100. * - DCCON is always 0, doesn't conflict
  101. * - M,N, P of PLLP values are ignored for PLLP
  102. */
  103. data = (cpcon << PLL_CPCON_SHIFT) | (lfcon << PLL_LFCON_SHIFT);
  104. writel(data, &pll->pll_misc);
  105. data = (divm << PLL_DIVM_SHIFT) | (divn << PLL_DIVN_SHIFT) |
  106. (0 << PLL_BYPASS_SHIFT) | (1 << PLL_ENABLE_SHIFT);
  107. if (clkid == CLOCK_ID_USB)
  108. data |= divp << PLLU_VCO_FREQ_SHIFT;
  109. else
  110. data |= divp << PLL_DIVP_SHIFT;
  111. writel(data, &pll->pll_base);
  112. /* calculate the stable time */
  113. return timer_get_us() + CLOCK_PLL_STABLE_DELAY_US;
  114. }
  115. void clock_ll_set_source_divisor(enum periph_id periph_id, unsigned source,
  116. unsigned divisor)
  117. {
  118. u32 *reg = get_periph_source_reg(periph_id);
  119. u32 value;
  120. value = readl(reg);
  121. value &= ~OUT_CLK_SOURCE_MASK;
  122. value |= source << OUT_CLK_SOURCE_SHIFT;
  123. value &= ~OUT_CLK_DIVISOR_MASK;
  124. value |= divisor << OUT_CLK_DIVISOR_SHIFT;
  125. writel(value, reg);
  126. }
  127. void clock_ll_set_source(enum periph_id periph_id, unsigned source)
  128. {
  129. u32 *reg = get_periph_source_reg(periph_id);
  130. clrsetbits_le32(reg, OUT_CLK_SOURCE_MASK,
  131. source << OUT_CLK_SOURCE_SHIFT);
  132. }
  133. /**
  134. * Given the parent's rate and the required rate for the children, this works
  135. * out the peripheral clock divider to use, in 7.1 binary format.
  136. *
  137. * @param divider_bits number of divider bits (8 or 16)
  138. * @param parent_rate clock rate of parent clock in Hz
  139. * @param rate required clock rate for this clock
  140. * @return divider which should be used
  141. */
  142. static int clk_get_divider(unsigned divider_bits, unsigned long parent_rate,
  143. unsigned long rate)
  144. {
  145. u64 divider = parent_rate * 2;
  146. unsigned max_divider = 1 << divider_bits;
  147. divider += rate - 1;
  148. do_div(divider, rate);
  149. if ((s64)divider - 2 < 0)
  150. return 0;
  151. if ((s64)divider - 2 >= max_divider)
  152. return -1;
  153. return divider - 2;
  154. }
  155. int clock_set_pllout(enum clock_id clkid, enum pll_out_id pllout, unsigned rate)
  156. {
  157. struct clk_pll *pll = get_pll(clkid);
  158. int data = 0, div = 0, offset = 0;
  159. if (!clock_id_is_pll(clkid))
  160. return -1;
  161. if (pllout + 1 > pll_num_clkouts[clkid])
  162. return -1;
  163. div = clk_get_divider(8, pll_rate[clkid], rate);
  164. if (div < 0)
  165. return -1;
  166. /* out2 and out4 are in the high part of the register */
  167. if (pllout == PLL_OUT2 || pllout == PLL_OUT4)
  168. offset = 16;
  169. data = (div << PLL_OUT_RATIO_SHIFT) |
  170. PLL_OUT_OVRRIDE | PLL_OUT_CLKEN | PLL_OUT_RSTN;
  171. clrsetbits_le32(&pll->pll_out[pllout >> 1],
  172. PLL_OUT_RATIO_MASK << offset, data << offset);
  173. return 0;
  174. }
  175. /**
  176. * Given the parent's rate and the divider in 7.1 format, this works out the
  177. * resulting peripheral clock rate.
  178. *
  179. * @param parent_rate clock rate of parent clock in Hz
  180. * @param divider which should be used in 7.1 format
  181. * @return effective clock rate of peripheral
  182. */
  183. static unsigned long get_rate_from_divider(unsigned long parent_rate,
  184. int divider)
  185. {
  186. u64 rate;
  187. rate = (u64)parent_rate * 2;
  188. do_div(rate, divider + 2);
  189. return rate;
  190. }
  191. unsigned long clock_get_periph_rate(enum periph_id periph_id,
  192. enum clock_id parent)
  193. {
  194. u32 *reg = get_periph_source_reg(periph_id);
  195. return get_rate_from_divider(pll_rate[parent],
  196. (readl(reg) & OUT_CLK_DIVISOR_MASK) >> OUT_CLK_DIVISOR_SHIFT);
  197. }
  198. /**
  199. * Find the best available 7.1 format divisor given a parent clock rate and
  200. * required child clock rate. This function assumes that a second-stage
  201. * divisor is available which can divide by powers of 2 from 1 to 256.
  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. * @param extra_div value for the second-stage divisor (not set if this
  207. * function returns -1.
  208. * @return divider which should be used, or -1 if nothing is valid
  209. *
  210. */
  211. static int find_best_divider(unsigned divider_bits, unsigned long parent_rate,
  212. unsigned long rate, int *extra_div)
  213. {
  214. int shift;
  215. int best_divider = -1;
  216. int best_error = rate;
  217. /* try dividers from 1 to 256 and find closest match */
  218. for (shift = 0; shift <= 8 && best_error > 0; shift++) {
  219. unsigned divided_parent = parent_rate >> shift;
  220. int divider = clk_get_divider(divider_bits, divided_parent,
  221. rate);
  222. unsigned effective_rate = get_rate_from_divider(divided_parent,
  223. divider);
  224. int error = rate - effective_rate;
  225. /* Given a valid divider, look for the lowest error */
  226. if (divider != -1 && error < best_error) {
  227. best_error = error;
  228. *extra_div = 1 << shift;
  229. best_divider = divider;
  230. }
  231. }
  232. /* return what we found - *extra_div will already be set */
  233. return best_divider;
  234. }
  235. /**
  236. * Adjust peripheral PLL to use the given divider and source.
  237. *
  238. * @param periph_id peripheral to adjust
  239. * @param source Source number (0-3 or 0-7)
  240. * @param mux_bits Number of mux bits (2 or 4)
  241. * @param divider Required divider in 7.1 or 15.1 format
  242. * @return 0 if ok, -1 on error (requesting a parent clock which is not valid
  243. * for this peripheral)
  244. */
  245. static int adjust_periph_pll(enum periph_id periph_id, int source,
  246. int mux_bits, unsigned divider)
  247. {
  248. u32 *reg = get_periph_source_reg(periph_id);
  249. clrsetbits_le32(reg, OUT_CLK_DIVISOR_MASK,
  250. divider << OUT_CLK_DIVISOR_SHIFT);
  251. udelay(1);
  252. /* work out the source clock and set it */
  253. if (source < 0)
  254. return -1;
  255. if (mux_bits == 4) {
  256. clrsetbits_le32(reg, OUT_CLK_SOURCE4_MASK,
  257. source << OUT_CLK_SOURCE4_SHIFT);
  258. } else {
  259. clrsetbits_le32(reg, OUT_CLK_SOURCE_MASK,
  260. source << OUT_CLK_SOURCE_SHIFT);
  261. }
  262. udelay(2);
  263. return 0;
  264. }
  265. unsigned clock_adjust_periph_pll_div(enum periph_id periph_id,
  266. enum clock_id parent, unsigned rate, int *extra_div)
  267. {
  268. unsigned effective_rate;
  269. int mux_bits, divider_bits, source;
  270. int divider;
  271. int xdiv = 0;
  272. /* work out the source clock and set it */
  273. source = get_periph_clock_source(periph_id, parent, &mux_bits,
  274. &divider_bits);
  275. divider = find_best_divider(divider_bits, pll_rate[parent],
  276. rate, &xdiv);
  277. if (extra_div)
  278. *extra_div = xdiv;
  279. assert(divider >= 0);
  280. if (adjust_periph_pll(periph_id, source, mux_bits, divider))
  281. return -1U;
  282. debug("periph %d, rate=%d, reg=%p = %x\n", periph_id, rate,
  283. get_periph_source_reg(periph_id),
  284. readl(get_periph_source_reg(periph_id)));
  285. /* Check what we ended up with. This shouldn't matter though */
  286. effective_rate = clock_get_periph_rate(periph_id, parent);
  287. if (extra_div)
  288. effective_rate /= *extra_div;
  289. if (rate != effective_rate)
  290. debug("Requested clock rate %u not honored (got %u)\n",
  291. rate, effective_rate);
  292. return effective_rate;
  293. }
  294. unsigned clock_start_periph_pll(enum periph_id periph_id,
  295. enum clock_id parent, unsigned rate)
  296. {
  297. unsigned effective_rate;
  298. reset_set_enable(periph_id, 1);
  299. clock_enable(periph_id);
  300. effective_rate = clock_adjust_periph_pll_div(periph_id, parent, rate,
  301. NULL);
  302. reset_set_enable(periph_id, 0);
  303. return effective_rate;
  304. }
  305. void clock_enable(enum periph_id clkid)
  306. {
  307. clock_set_enable(clkid, 1);
  308. }
  309. void clock_disable(enum periph_id clkid)
  310. {
  311. clock_set_enable(clkid, 0);
  312. }
  313. void reset_periph(enum periph_id periph_id, int us_delay)
  314. {
  315. /* Put peripheral into reset */
  316. reset_set_enable(periph_id, 1);
  317. udelay(us_delay);
  318. /* Remove reset */
  319. reset_set_enable(periph_id, 0);
  320. udelay(us_delay);
  321. }
  322. void reset_cmplx_set_enable(int cpu, int which, int reset)
  323. {
  324. struct clk_rst_ctlr *clkrst =
  325. (struct clk_rst_ctlr *)NV_PA_CLK_RST_BASE;
  326. u32 mask;
  327. /* Form the mask, which depends on the cpu chosen (2 or 4) */
  328. assert(cpu >= 0 && cpu < MAX_NUM_CPU);
  329. mask = which << cpu;
  330. /* either enable or disable those reset for that CPU */
  331. if (reset)
  332. writel(mask, &clkrst->crc_cpu_cmplx_set);
  333. else
  334. writel(mask, &clkrst->crc_cpu_cmplx_clr);
  335. }
  336. unsigned clock_get_rate(enum clock_id clkid)
  337. {
  338. struct clk_pll *pll;
  339. u32 base;
  340. u32 divm;
  341. u64 parent_rate;
  342. u64 rate;
  343. parent_rate = osc_freq[clock_get_osc_freq()];
  344. if (clkid == CLOCK_ID_OSC)
  345. return parent_rate;
  346. pll = get_pll(clkid);
  347. base = readl(&pll->pll_base);
  348. /* Oh for bf_unpack()... */
  349. rate = parent_rate * ((base & PLL_DIVN_MASK) >> PLL_DIVN_SHIFT);
  350. divm = (base & PLL_DIVM_MASK) >> PLL_DIVM_SHIFT;
  351. if (clkid == CLOCK_ID_USB)
  352. divm <<= (base & PLLU_VCO_FREQ_MASK) >> PLLU_VCO_FREQ_SHIFT;
  353. else
  354. divm <<= (base & PLL_DIVP_MASK) >> PLL_DIVP_SHIFT;
  355. do_div(rate, divm);
  356. return rate;
  357. }
  358. /**
  359. * Set the output frequency you want for each PLL clock.
  360. * PLL output frequencies are programmed by setting their N, M and P values.
  361. * The governing equations are:
  362. * VCO = (Fi / m) * n, Fo = VCO / (2^p)
  363. * where Fo is the output frequency from the PLL.
  364. * Example: Set the output frequency to 216Mhz(Fo) with 12Mhz OSC(Fi)
  365. * 216Mhz = ((12Mhz / m) * n) / (2^p) so n=432,m=12,p=1
  366. * Please see Tegra TRM section 5.3 to get the detail for PLL Programming
  367. *
  368. * @param n PLL feedback divider(DIVN)
  369. * @param m PLL input divider(DIVN)
  370. * @param p post divider(DIVP)
  371. * @param cpcon base PLL charge pump(CPCON)
  372. * @return 0 if ok, -1 on error (the requested PLL is incorrect and cannot
  373. * be overriden), 1 if PLL is already correct
  374. */
  375. int clock_set_rate(enum clock_id clkid, u32 n, u32 m, u32 p, u32 cpcon)
  376. {
  377. u32 base_reg;
  378. u32 misc_reg;
  379. struct clk_pll *pll;
  380. pll = get_pll(clkid);
  381. base_reg = readl(&pll->pll_base);
  382. /* Set BYPASS, m, n and p to PLL_BASE */
  383. base_reg &= ~PLL_DIVM_MASK;
  384. base_reg |= m << PLL_DIVM_SHIFT;
  385. base_reg &= ~PLL_DIVN_MASK;
  386. base_reg |= n << PLL_DIVN_SHIFT;
  387. base_reg &= ~PLL_DIVP_MASK;
  388. base_reg |= p << PLL_DIVP_SHIFT;
  389. if (clkid == CLOCK_ID_PERIPH) {
  390. /*
  391. * If the PLL is already set up, check that it is correct
  392. * and record this info for clock_verify() to check.
  393. */
  394. if (base_reg & PLL_BASE_OVRRIDE_MASK) {
  395. base_reg |= PLL_ENABLE_MASK;
  396. if (base_reg != readl(&pll->pll_base))
  397. pllp_valid = 0;
  398. return pllp_valid ? 1 : -1;
  399. }
  400. base_reg |= PLL_BASE_OVRRIDE_MASK;
  401. }
  402. base_reg |= PLL_BYPASS_MASK;
  403. writel(base_reg, &pll->pll_base);
  404. /* Set cpcon to PLL_MISC */
  405. misc_reg = readl(&pll->pll_misc);
  406. misc_reg &= ~PLL_CPCON_MASK;
  407. misc_reg |= cpcon << PLL_CPCON_SHIFT;
  408. writel(misc_reg, &pll->pll_misc);
  409. /* Enable PLL */
  410. base_reg |= PLL_ENABLE_MASK;
  411. writel(base_reg, &pll->pll_base);
  412. /* Disable BYPASS */
  413. base_reg &= ~PLL_BYPASS_MASK;
  414. writel(base_reg, &pll->pll_base);
  415. return 0;
  416. }
  417. void clock_ll_start_uart(enum periph_id periph_id)
  418. {
  419. /* Assert UART reset and enable clock */
  420. reset_set_enable(periph_id, 1);
  421. clock_enable(periph_id);
  422. clock_ll_set_source(periph_id, 0); /* UARTx_CLK_SRC = 00, PLLP_OUT0 */
  423. /* wait for 2us */
  424. udelay(2);
  425. /* De-assert reset to UART */
  426. reset_set_enable(periph_id, 0);
  427. }
  428. #ifdef CONFIG_OF_CONTROL
  429. int clock_decode_periph_id(const void *blob, int node)
  430. {
  431. enum periph_id id;
  432. u32 cell[2];
  433. int err;
  434. err = fdtdec_get_int_array(blob, node, "clocks", cell,
  435. ARRAY_SIZE(cell));
  436. if (err)
  437. return -1;
  438. id = clk_id_to_periph_id(cell[1]);
  439. assert(clock_periph_id_isvalid(id));
  440. return id;
  441. }
  442. #endif /* CONFIG_OF_CONTROL */
  443. int clock_verify(void)
  444. {
  445. struct clk_pll *pll = get_pll(CLOCK_ID_PERIPH);
  446. u32 reg = readl(&pll->pll_base);
  447. if (!pllp_valid) {
  448. printf("Warning: PLLP %x is not correct\n", reg);
  449. return -1;
  450. }
  451. debug("PLLP %x is correct\n", reg);
  452. return 0;
  453. }
  454. void clock_init(void)
  455. {
  456. pll_rate[CLOCK_ID_MEMORY] = clock_get_rate(CLOCK_ID_MEMORY);
  457. pll_rate[CLOCK_ID_PERIPH] = clock_get_rate(CLOCK_ID_PERIPH);
  458. pll_rate[CLOCK_ID_CGENERAL] = clock_get_rate(CLOCK_ID_CGENERAL);
  459. pll_rate[CLOCK_ID_OSC] = clock_get_rate(CLOCK_ID_OSC);
  460. pll_rate[CLOCK_ID_SFROM32KHZ] = 32768;
  461. pll_rate[CLOCK_ID_XCPU] = clock_get_rate(CLOCK_ID_XCPU);
  462. debug("Osc = %d\n", pll_rate[CLOCK_ID_OSC]);
  463. debug("PLLM = %d\n", pll_rate[CLOCK_ID_MEMORY]);
  464. debug("PLLP = %d\n", pll_rate[CLOCK_ID_PERIPH]);
  465. debug("PLLC = %d\n", pll_rate[CLOCK_ID_CGENERAL]);
  466. debug("PLLX = %d\n", pll_rate[CLOCK_ID_XCPU]);
  467. /* Do any special system timer/TSC setup */
  468. arch_timer_init();
  469. }