clock.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857
  1. /*
  2. * (C) Copyright 2007
  3. * Sascha Hauer, Pengutronix
  4. *
  5. * (C) Copyright 2009 Freescale Semiconductor, Inc.
  6. *
  7. * See file CREDITS for list of people who contributed to this
  8. * project.
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License as
  12. * published by the Free Software Foundation; either version 2 of
  13. * the License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  23. * MA 02111-1307 USA
  24. */
  25. #include <common.h>
  26. #include <asm/io.h>
  27. #include <asm/errno.h>
  28. #include <asm/arch/imx-regs.h>
  29. #include <asm/arch/crm_regs.h>
  30. #include <asm/arch/clock.h>
  31. #include <div64.h>
  32. #include <asm/arch/sys_proto.h>
  33. enum pll_clocks {
  34. PLL1_CLOCK = 0,
  35. PLL2_CLOCK,
  36. PLL3_CLOCK,
  37. PLL4_CLOCK,
  38. PLL_CLOCKS,
  39. };
  40. struct mxc_pll_reg *mxc_plls[PLL_CLOCKS] = {
  41. [PLL1_CLOCK] = (struct mxc_pll_reg *)PLL1_BASE_ADDR,
  42. [PLL2_CLOCK] = (struct mxc_pll_reg *)PLL2_BASE_ADDR,
  43. [PLL3_CLOCK] = (struct mxc_pll_reg *)PLL3_BASE_ADDR,
  44. #ifdef CONFIG_MX53
  45. [PLL4_CLOCK] = (struct mxc_pll_reg *)PLL4_BASE_ADDR,
  46. #endif
  47. };
  48. #define AHB_CLK_ROOT 133333333
  49. #define SZ_DEC_1M 1000000
  50. #define PLL_PD_MAX 16 /* Actual pd+1 */
  51. #define PLL_MFI_MAX 15
  52. #define PLL_MFI_MIN 5
  53. #define ARM_DIV_MAX 8
  54. #define IPG_DIV_MAX 4
  55. #define AHB_DIV_MAX 8
  56. #define EMI_DIV_MAX 8
  57. #define NFC_DIV_MAX 8
  58. #define MX5_CBCMR 0x00015154
  59. #define MX5_CBCDR 0x02888945
  60. struct fixed_pll_mfd {
  61. u32 ref_clk_hz;
  62. u32 mfd;
  63. };
  64. const struct fixed_pll_mfd fixed_mfd[] = {
  65. {MXC_HCLK, 24 * 16},
  66. };
  67. struct pll_param {
  68. u32 pd;
  69. u32 mfi;
  70. u32 mfn;
  71. u32 mfd;
  72. };
  73. #define PLL_FREQ_MAX(ref_clk) (4 * (ref_clk) * PLL_MFI_MAX)
  74. #define PLL_FREQ_MIN(ref_clk) \
  75. ((2 * (ref_clk) * (PLL_MFI_MIN - 1)) / PLL_PD_MAX)
  76. #define MAX_DDR_CLK 420000000
  77. #define NFC_CLK_MAX 34000000
  78. struct mxc_ccm_reg *mxc_ccm = (struct mxc_ccm_reg *)MXC_CCM_BASE;
  79. void set_usboh3_clk(void)
  80. {
  81. clrsetbits_le32(&mxc_ccm->cscmr1,
  82. MXC_CCM_CSCMR1_USBOH3_CLK_SEL_MASK,
  83. MXC_CCM_CSCMR1_USBOH3_CLK_SEL(1));
  84. clrsetbits_le32(&mxc_ccm->cscdr1,
  85. MXC_CCM_CSCDR1_USBOH3_CLK_PODF_MASK |
  86. MXC_CCM_CSCDR1_USBOH3_CLK_PRED_MASK,
  87. MXC_CCM_CSCDR1_USBOH3_CLK_PRED(4) |
  88. MXC_CCM_CSCDR1_USBOH3_CLK_PODF(1));
  89. }
  90. void enable_usboh3_clk(unsigned char enable)
  91. {
  92. unsigned int cg = enable ? MXC_CCM_CCGR_CG_ON : MXC_CCM_CCGR_CG_OFF;
  93. clrsetbits_le32(&mxc_ccm->CCGR2,
  94. MXC_CCM_CCGR2_USBOH3_60M(MXC_CCM_CCGR_CG_MASK),
  95. MXC_CCM_CCGR2_USBOH3_60M(cg));
  96. }
  97. #ifdef CONFIG_I2C_MXC
  98. /* i2c_num can be from 0 - 2 */
  99. int enable_i2c_clk(unsigned char enable, unsigned i2c_num)
  100. {
  101. u32 mask;
  102. if (i2c_num > 2)
  103. return -EINVAL;
  104. mask = MXC_CCM_CCGR_CG_MASK <<
  105. (MXC_CCM_CCGR1_I2C1_OFFSET + (i2c_num << 1));
  106. if (enable)
  107. setbits_le32(&mxc_ccm->CCGR1, mask);
  108. else
  109. clrbits_le32(&mxc_ccm->CCGR1, mask);
  110. return 0;
  111. }
  112. #endif
  113. void set_usb_phy_clk(void)
  114. {
  115. clrbits_le32(&mxc_ccm->cscmr1, MXC_CCM_CSCMR1_USB_PHY_CLK_SEL);
  116. }
  117. #if defined(CONFIG_MX51)
  118. void enable_usb_phy1_clk(unsigned char enable)
  119. {
  120. unsigned int cg = enable ? MXC_CCM_CCGR_CG_ON : MXC_CCM_CCGR_CG_OFF;
  121. clrsetbits_le32(&mxc_ccm->CCGR2,
  122. MXC_CCM_CCGR2_USB_PHY(MXC_CCM_CCGR_CG_MASK),
  123. MXC_CCM_CCGR2_USB_PHY(cg));
  124. }
  125. void enable_usb_phy2_clk(unsigned char enable)
  126. {
  127. /* i.MX51 has a single USB PHY clock, so do nothing here. */
  128. }
  129. #elif defined(CONFIG_MX53)
  130. void enable_usb_phy1_clk(unsigned char enable)
  131. {
  132. unsigned int cg = enable ? MXC_CCM_CCGR_CG_ON : MXC_CCM_CCGR_CG_OFF;
  133. clrsetbits_le32(&mxc_ccm->CCGR4,
  134. MXC_CCM_CCGR4_USB_PHY1(MXC_CCM_CCGR_CG_MASK),
  135. MXC_CCM_CCGR4_USB_PHY1(cg));
  136. }
  137. void enable_usb_phy2_clk(unsigned char enable)
  138. {
  139. unsigned int cg = enable ? MXC_CCM_CCGR_CG_ON : MXC_CCM_CCGR_CG_OFF;
  140. clrsetbits_le32(&mxc_ccm->CCGR4,
  141. MXC_CCM_CCGR4_USB_PHY2(MXC_CCM_CCGR_CG_MASK),
  142. MXC_CCM_CCGR4_USB_PHY2(cg));
  143. }
  144. #endif
  145. /*
  146. * Calculate the frequency of PLLn.
  147. */
  148. static uint32_t decode_pll(struct mxc_pll_reg *pll, uint32_t infreq)
  149. {
  150. uint32_t ctrl, op, mfd, mfn, mfi, pdf, ret;
  151. uint64_t refclk, temp;
  152. int32_t mfn_abs;
  153. ctrl = readl(&pll->ctrl);
  154. if (ctrl & MXC_DPLLC_CTL_HFSM) {
  155. mfn = readl(&pll->hfs_mfn);
  156. mfd = readl(&pll->hfs_mfd);
  157. op = readl(&pll->hfs_op);
  158. } else {
  159. mfn = readl(&pll->mfn);
  160. mfd = readl(&pll->mfd);
  161. op = readl(&pll->op);
  162. }
  163. mfd &= MXC_DPLLC_MFD_MFD_MASK;
  164. mfn &= MXC_DPLLC_MFN_MFN_MASK;
  165. pdf = op & MXC_DPLLC_OP_PDF_MASK;
  166. mfi = MXC_DPLLC_OP_MFI_RD(op);
  167. /* 21.2.3 */
  168. if (mfi < 5)
  169. mfi = 5;
  170. /* Sign extend */
  171. if (mfn >= 0x04000000) {
  172. mfn |= 0xfc000000;
  173. mfn_abs = -mfn;
  174. } else
  175. mfn_abs = mfn;
  176. refclk = infreq * 2;
  177. if (ctrl & MXC_DPLLC_CTL_DPDCK0_2_EN)
  178. refclk *= 2;
  179. do_div(refclk, pdf + 1);
  180. temp = refclk * mfn_abs;
  181. do_div(temp, mfd + 1);
  182. ret = refclk * mfi;
  183. if ((int)mfn < 0)
  184. ret -= temp;
  185. else
  186. ret += temp;
  187. return ret;
  188. }
  189. /*
  190. * Get mcu main rate
  191. */
  192. u32 get_mcu_main_clk(void)
  193. {
  194. u32 reg, freq;
  195. reg = MXC_CCM_CACRR_ARM_PODF_RD(readl(&mxc_ccm->cacrr));
  196. freq = decode_pll(mxc_plls[PLL1_CLOCK], MXC_HCLK);
  197. return freq / (reg + 1);
  198. }
  199. /*
  200. * Get the rate of peripheral's root clock.
  201. */
  202. u32 get_periph_clk(void)
  203. {
  204. u32 reg;
  205. reg = readl(&mxc_ccm->cbcdr);
  206. if (!(reg & MXC_CCM_CBCDR_PERIPH_CLK_SEL))
  207. return decode_pll(mxc_plls[PLL2_CLOCK], MXC_HCLK);
  208. reg = readl(&mxc_ccm->cbcmr);
  209. switch (MXC_CCM_CBCMR_PERIPH_CLK_SEL_RD(reg)) {
  210. case 0:
  211. return decode_pll(mxc_plls[PLL1_CLOCK], MXC_HCLK);
  212. case 1:
  213. return decode_pll(mxc_plls[PLL3_CLOCK], MXC_HCLK);
  214. default:
  215. return 0;
  216. }
  217. /* NOTREACHED */
  218. }
  219. /*
  220. * Get the rate of ipg clock.
  221. */
  222. static u32 get_ipg_clk(void)
  223. {
  224. uint32_t freq, reg, div;
  225. freq = get_ahb_clk();
  226. reg = readl(&mxc_ccm->cbcdr);
  227. div = MXC_CCM_CBCDR_IPG_PODF_RD(reg) + 1;
  228. return freq / div;
  229. }
  230. /*
  231. * Get the rate of ipg_per clock.
  232. */
  233. static u32 get_ipg_per_clk(void)
  234. {
  235. u32 pred1, pred2, podf;
  236. if (readl(&mxc_ccm->cbcmr) & MXC_CCM_CBCMR_PERCLK_IPG_CLK_SEL)
  237. return get_ipg_clk();
  238. /* Fixme: not handle what about lpm*/
  239. podf = readl(&mxc_ccm->cbcdr);
  240. pred1 = MXC_CCM_CBCDR_PERCLK_PRED1_RD(podf);
  241. pred2 = MXC_CCM_CBCDR_PERCLK_PRED2_RD(podf);
  242. podf = MXC_CCM_CBCDR_PERCLK_PODF_RD(podf);
  243. return get_periph_clk() / ((pred1 + 1) * (pred2 + 1) * (podf + 1));
  244. }
  245. /*
  246. * Get the rate of uart clk.
  247. */
  248. static u32 get_uart_clk(void)
  249. {
  250. unsigned int freq, reg, pred, podf;
  251. reg = readl(&mxc_ccm->cscmr1);
  252. switch (MXC_CCM_CSCMR1_UART_CLK_SEL_RD(reg)) {
  253. case 0x0:
  254. freq = decode_pll(mxc_plls[PLL1_CLOCK], MXC_HCLK);
  255. break;
  256. case 0x1:
  257. freq = decode_pll(mxc_plls[PLL2_CLOCK], MXC_HCLK);
  258. break;
  259. case 0x2:
  260. freq = decode_pll(mxc_plls[PLL3_CLOCK], MXC_HCLK);
  261. break;
  262. default:
  263. return 66500000;
  264. }
  265. reg = readl(&mxc_ccm->cscdr1);
  266. pred = MXC_CCM_CSCDR1_UART_CLK_PRED_RD(reg);
  267. podf = MXC_CCM_CSCDR1_UART_CLK_PODF_RD(reg);
  268. freq /= (pred + 1) * (podf + 1);
  269. return freq;
  270. }
  271. /*
  272. * This function returns the low power audio clock.
  273. */
  274. static u32 get_lp_apm(void)
  275. {
  276. u32 ret_val = 0;
  277. u32 ccsr = readl(&mxc_ccm->ccsr);
  278. if (((ccsr >> 9) & 1) == 0)
  279. ret_val = MXC_HCLK;
  280. else
  281. ret_val = MXC_CLK32 * 1024;
  282. return ret_val;
  283. }
  284. /*
  285. * get cspi clock rate.
  286. */
  287. static u32 imx_get_cspiclk(void)
  288. {
  289. u32 ret_val = 0, pdf, pre_pdf, clk_sel;
  290. u32 cscmr1 = readl(&mxc_ccm->cscmr1);
  291. u32 cscdr2 = readl(&mxc_ccm->cscdr2);
  292. pre_pdf = MXC_CCM_CSCDR2_CSPI_CLK_PRED_RD(cscdr2);
  293. pdf = MXC_CCM_CSCDR2_CSPI_CLK_PODF_RD(cscdr2);
  294. clk_sel = MXC_CCM_CSCMR1_CSPI_CLK_SEL_RD(cscmr1);
  295. switch (clk_sel) {
  296. case 0:
  297. ret_val = decode_pll(mxc_plls[PLL1_CLOCK], MXC_HCLK) /
  298. ((pre_pdf + 1) * (pdf + 1));
  299. break;
  300. case 1:
  301. ret_val = decode_pll(mxc_plls[PLL2_CLOCK], MXC_HCLK) /
  302. ((pre_pdf + 1) * (pdf + 1));
  303. break;
  304. case 2:
  305. ret_val = decode_pll(mxc_plls[PLL3_CLOCK], MXC_HCLK) /
  306. ((pre_pdf + 1) * (pdf + 1));
  307. break;
  308. default:
  309. ret_val = get_lp_apm() / ((pre_pdf + 1) * (pdf + 1));
  310. break;
  311. }
  312. return ret_val;
  313. }
  314. static u32 get_axi_a_clk(void)
  315. {
  316. u32 cbcdr = readl(&mxc_ccm->cbcdr);
  317. u32 pdf = MXC_CCM_CBCDR_AXI_A_PODF_RD(cbcdr);
  318. return get_periph_clk() / (pdf + 1);
  319. }
  320. static u32 get_axi_b_clk(void)
  321. {
  322. u32 cbcdr = readl(&mxc_ccm->cbcdr);
  323. u32 pdf = MXC_CCM_CBCDR_AXI_B_PODF_RD(cbcdr);
  324. return get_periph_clk() / (pdf + 1);
  325. }
  326. static u32 get_emi_slow_clk(void)
  327. {
  328. u32 cbcdr = readl(&mxc_ccm->cbcdr);
  329. u32 emi_clk_sel = cbcdr & MXC_CCM_CBCDR_EMI_CLK_SEL;
  330. u32 pdf = MXC_CCM_CBCDR_EMI_PODF_RD(cbcdr);
  331. if (emi_clk_sel)
  332. return get_ahb_clk() / (pdf + 1);
  333. return get_periph_clk() / (pdf + 1);
  334. }
  335. static u32 get_ddr_clk(void)
  336. {
  337. u32 ret_val = 0;
  338. u32 cbcmr = readl(&mxc_ccm->cbcmr);
  339. u32 ddr_clk_sel = MXC_CCM_CBCMR_DDR_CLK_SEL_RD(cbcmr);
  340. #ifdef CONFIG_MX51
  341. u32 cbcdr = readl(&mxc_ccm->cbcdr);
  342. if (cbcdr & MXC_CCM_CBCDR_DDR_HIFREQ_SEL) {
  343. u32 ddr_clk_podf = MXC_CCM_CBCDR_DDR_PODF_RD(cbcdr);
  344. ret_val = decode_pll(mxc_plls[PLL1_CLOCK], MXC_HCLK);
  345. ret_val /= ddr_clk_podf + 1;
  346. return ret_val;
  347. }
  348. #endif
  349. switch (ddr_clk_sel) {
  350. case 0:
  351. ret_val = get_axi_a_clk();
  352. break;
  353. case 1:
  354. ret_val = get_axi_b_clk();
  355. break;
  356. case 2:
  357. ret_val = get_emi_slow_clk();
  358. break;
  359. case 3:
  360. ret_val = get_ahb_clk();
  361. break;
  362. default:
  363. break;
  364. }
  365. return ret_val;
  366. }
  367. /*
  368. * The API of get mxc clocks.
  369. */
  370. unsigned int mxc_get_clock(enum mxc_clock clk)
  371. {
  372. switch (clk) {
  373. case MXC_ARM_CLK:
  374. return get_mcu_main_clk();
  375. case MXC_AHB_CLK:
  376. return get_ahb_clk();
  377. case MXC_IPG_CLK:
  378. return get_ipg_clk();
  379. case MXC_IPG_PERCLK:
  380. case MXC_I2C_CLK:
  381. return get_ipg_per_clk();
  382. case MXC_UART_CLK:
  383. return get_uart_clk();
  384. case MXC_CSPI_CLK:
  385. return imx_get_cspiclk();
  386. case MXC_FEC_CLK:
  387. return decode_pll(mxc_plls[PLL1_CLOCK], MXC_HCLK);
  388. case MXC_SATA_CLK:
  389. return get_ahb_clk();
  390. case MXC_DDR_CLK:
  391. return get_ddr_clk();
  392. default:
  393. break;
  394. }
  395. return -EINVAL;
  396. }
  397. u32 imx_get_uartclk(void)
  398. {
  399. return get_uart_clk();
  400. }
  401. u32 imx_get_fecclk(void)
  402. {
  403. return mxc_get_clock(MXC_IPG_CLK);
  404. }
  405. static int gcd(int m, int n)
  406. {
  407. int t;
  408. while (m > 0) {
  409. if (n > m) {
  410. t = m;
  411. m = n;
  412. n = t;
  413. } /* swap */
  414. m -= n;
  415. }
  416. return n;
  417. }
  418. /*
  419. * This is to calculate various parameters based on reference clock and
  420. * targeted clock based on the equation:
  421. * t_clk = 2*ref_freq*(mfi + mfn/(mfd+1))/(pd+1)
  422. * This calculation is based on a fixed MFD value for simplicity.
  423. */
  424. static int calc_pll_params(u32 ref, u32 target, struct pll_param *pll)
  425. {
  426. u64 pd, mfi = 1, mfn, mfd, t1;
  427. u32 n_target = target;
  428. u32 n_ref = ref, i;
  429. /*
  430. * Make sure targeted freq is in the valid range.
  431. * Otherwise the following calculation might be wrong!!!
  432. */
  433. if (n_target < PLL_FREQ_MIN(ref) ||
  434. n_target > PLL_FREQ_MAX(ref)) {
  435. printf("Targeted peripheral clock should be"
  436. "within [%d - %d]\n",
  437. PLL_FREQ_MIN(ref) / SZ_DEC_1M,
  438. PLL_FREQ_MAX(ref) / SZ_DEC_1M);
  439. return -EINVAL;
  440. }
  441. for (i = 0; i < ARRAY_SIZE(fixed_mfd); i++) {
  442. if (fixed_mfd[i].ref_clk_hz == ref) {
  443. mfd = fixed_mfd[i].mfd;
  444. break;
  445. }
  446. }
  447. if (i == ARRAY_SIZE(fixed_mfd))
  448. return -EINVAL;
  449. /* Use n_target and n_ref to avoid overflow */
  450. for (pd = 1; pd <= PLL_PD_MAX; pd++) {
  451. t1 = n_target * pd;
  452. do_div(t1, (4 * n_ref));
  453. mfi = t1;
  454. if (mfi > PLL_MFI_MAX)
  455. return -EINVAL;
  456. else if (mfi < 5)
  457. continue;
  458. break;
  459. }
  460. /*
  461. * Now got pd and mfi already
  462. *
  463. * mfn = (((n_target * pd) / 4 - n_ref * mfi) * mfd) / n_ref;
  464. */
  465. t1 = n_target * pd;
  466. do_div(t1, 4);
  467. t1 -= n_ref * mfi;
  468. t1 *= mfd;
  469. do_div(t1, n_ref);
  470. mfn = t1;
  471. debug("ref=%d, target=%d, pd=%d," "mfi=%d,mfn=%d, mfd=%d\n",
  472. ref, n_target, (u32)pd, (u32)mfi, (u32)mfn, (u32)mfd);
  473. i = 1;
  474. if (mfn != 0)
  475. i = gcd(mfd, mfn);
  476. pll->pd = (u32)pd;
  477. pll->mfi = (u32)mfi;
  478. do_div(mfn, i);
  479. pll->mfn = (u32)mfn;
  480. do_div(mfd, i);
  481. pll->mfd = (u32)mfd;
  482. return 0;
  483. }
  484. #define calc_div(tgt_clk, src_clk, limit) ({ \
  485. u32 v = 0; \
  486. if (((src_clk) % (tgt_clk)) <= 100) \
  487. v = (src_clk) / (tgt_clk); \
  488. else \
  489. v = ((src_clk) / (tgt_clk)) + 1;\
  490. if (v > limit) \
  491. v = limit; \
  492. (v - 1); \
  493. })
  494. #define CHANGE_PLL_SETTINGS(pll, pd, fi, fn, fd) \
  495. { \
  496. writel(0x1232, &pll->ctrl); \
  497. writel(0x2, &pll->config); \
  498. writel((((pd) - 1) << 0) | ((fi) << 4), \
  499. &pll->op); \
  500. writel(fn, &(pll->mfn)); \
  501. writel((fd) - 1, &pll->mfd); \
  502. writel((((pd) - 1) << 0) | ((fi) << 4), \
  503. &pll->hfs_op); \
  504. writel(fn, &pll->hfs_mfn); \
  505. writel((fd) - 1, &pll->hfs_mfd); \
  506. writel(0x1232, &pll->ctrl); \
  507. while (!readl(&pll->ctrl) & 0x1) \
  508. ;\
  509. }
  510. static int config_pll_clk(enum pll_clocks index, struct pll_param *pll_param)
  511. {
  512. u32 ccsr = readl(&mxc_ccm->ccsr);
  513. struct mxc_pll_reg *pll = mxc_plls[index];
  514. switch (index) {
  515. case PLL1_CLOCK:
  516. /* Switch ARM to PLL2 clock */
  517. writel(ccsr | 0x4, &mxc_ccm->ccsr);
  518. CHANGE_PLL_SETTINGS(pll, pll_param->pd,
  519. pll_param->mfi, pll_param->mfn,
  520. pll_param->mfd);
  521. /* Switch back */
  522. writel(ccsr & ~0x4, &mxc_ccm->ccsr);
  523. break;
  524. case PLL2_CLOCK:
  525. /* Switch to pll2 bypass clock */
  526. writel(ccsr | 0x2, &mxc_ccm->ccsr);
  527. CHANGE_PLL_SETTINGS(pll, pll_param->pd,
  528. pll_param->mfi, pll_param->mfn,
  529. pll_param->mfd);
  530. /* Switch back */
  531. writel(ccsr & ~0x2, &mxc_ccm->ccsr);
  532. break;
  533. case PLL3_CLOCK:
  534. /* Switch to pll3 bypass clock */
  535. writel(ccsr | 0x1, &mxc_ccm->ccsr);
  536. CHANGE_PLL_SETTINGS(pll, pll_param->pd,
  537. pll_param->mfi, pll_param->mfn,
  538. pll_param->mfd);
  539. /* Switch back */
  540. writel(ccsr & ~0x1, &mxc_ccm->ccsr);
  541. break;
  542. case PLL4_CLOCK:
  543. /* Switch to pll4 bypass clock */
  544. writel(ccsr | 0x20, &mxc_ccm->ccsr);
  545. CHANGE_PLL_SETTINGS(pll, pll_param->pd,
  546. pll_param->mfi, pll_param->mfn,
  547. pll_param->mfd);
  548. /* Switch back */
  549. writel(ccsr & ~0x20, &mxc_ccm->ccsr);
  550. break;
  551. default:
  552. return -EINVAL;
  553. }
  554. return 0;
  555. }
  556. /* Config CPU clock */
  557. static int config_core_clk(u32 ref, u32 freq)
  558. {
  559. int ret = 0;
  560. struct pll_param pll_param;
  561. memset(&pll_param, 0, sizeof(struct pll_param));
  562. /* The case that periph uses PLL1 is not considered here */
  563. ret = calc_pll_params(ref, freq, &pll_param);
  564. if (ret != 0) {
  565. printf("Error:Can't find pll parameters: %d\n", ret);
  566. return ret;
  567. }
  568. return config_pll_clk(PLL1_CLOCK, &pll_param);
  569. }
  570. static int config_nfc_clk(u32 nfc_clk)
  571. {
  572. u32 parent_rate = get_emi_slow_clk();
  573. u32 div = parent_rate / nfc_clk;
  574. if (nfc_clk <= 0)
  575. return -EINVAL;
  576. if (div == 0)
  577. div++;
  578. if (parent_rate / div > NFC_CLK_MAX)
  579. div++;
  580. clrsetbits_le32(&mxc_ccm->cbcdr,
  581. MXC_CCM_CBCDR_NFC_PODF_MASK,
  582. MXC_CCM_CBCDR_NFC_PODF(div - 1));
  583. while (readl(&mxc_ccm->cdhipr) != 0)
  584. ;
  585. return 0;
  586. }
  587. /* Config main_bus_clock for periphs */
  588. static int config_periph_clk(u32 ref, u32 freq)
  589. {
  590. int ret = 0;
  591. struct pll_param pll_param;
  592. memset(&pll_param, 0, sizeof(struct pll_param));
  593. if (readl(&mxc_ccm->cbcdr) & MXC_CCM_CBCDR_PERIPH_CLK_SEL) {
  594. ret = calc_pll_params(ref, freq, &pll_param);
  595. if (ret != 0) {
  596. printf("Error:Can't find pll parameters: %d\n",
  597. ret);
  598. return ret;
  599. }
  600. switch (MXC_CCM_CBCMR_PERIPH_CLK_SEL_RD(
  601. readl(&mxc_ccm->cbcmr))) {
  602. case 0:
  603. return config_pll_clk(PLL1_CLOCK, &pll_param);
  604. break;
  605. case 1:
  606. return config_pll_clk(PLL3_CLOCK, &pll_param);
  607. break;
  608. default:
  609. return -EINVAL;
  610. }
  611. }
  612. return 0;
  613. }
  614. static int config_ddr_clk(u32 emi_clk)
  615. {
  616. u32 clk_src;
  617. s32 shift = 0, clk_sel, div = 1;
  618. u32 cbcmr = readl(&mxc_ccm->cbcmr);
  619. if (emi_clk > MAX_DDR_CLK) {
  620. printf("Warning:DDR clock should not exceed %d MHz\n",
  621. MAX_DDR_CLK / SZ_DEC_1M);
  622. emi_clk = MAX_DDR_CLK;
  623. }
  624. clk_src = get_periph_clk();
  625. /* Find DDR clock input */
  626. clk_sel = MXC_CCM_CBCMR_DDR_CLK_SEL_RD(cbcmr);
  627. switch (clk_sel) {
  628. case 0:
  629. shift = 16;
  630. break;
  631. case 1:
  632. shift = 19;
  633. break;
  634. case 2:
  635. shift = 22;
  636. break;
  637. case 3:
  638. shift = 10;
  639. break;
  640. default:
  641. return -EINVAL;
  642. }
  643. if ((clk_src % emi_clk) < 10000000)
  644. div = clk_src / emi_clk;
  645. else
  646. div = (clk_src / emi_clk) + 1;
  647. if (div > 8)
  648. div = 8;
  649. clrsetbits_le32(&mxc_ccm->cbcdr, 0x7 << shift, (div - 1) << shift);
  650. while (readl(&mxc_ccm->cdhipr) != 0)
  651. ;
  652. writel(0x0, &mxc_ccm->ccdr);
  653. return 0;
  654. }
  655. /*
  656. * This function assumes the expected core clock has to be changed by
  657. * modifying the PLL. This is NOT true always but for most of the times,
  658. * it is. So it assumes the PLL output freq is the same as the expected
  659. * core clock (presc=1) unless the core clock is less than PLL_FREQ_MIN.
  660. * In the latter case, it will try to increase the presc value until
  661. * (presc*core_clk) is greater than PLL_FREQ_MIN. It then makes call to
  662. * calc_pll_params() and obtains the values of PD, MFI,MFN, MFD based
  663. * on the targeted PLL and reference input clock to the PLL. Lastly,
  664. * it sets the register based on these values along with the dividers.
  665. * Note 1) There is no value checking for the passed-in divider values
  666. * so the caller has to make sure those values are sensible.
  667. * 2) Also adjust the NFC divider such that the NFC clock doesn't
  668. * exceed NFC_CLK_MAX.
  669. * 3) IPU HSP clock is independent of AHB clock. Even it can go up to
  670. * 177MHz for higher voltage, this function fixes the max to 133MHz.
  671. * 4) This function should not have allowed diag_printf() calls since
  672. * the serial driver has been stoped. But leave then here to allow
  673. * easy debugging by NOT calling the cyg_hal_plf_serial_stop().
  674. */
  675. int mxc_set_clock(u32 ref, u32 freq, enum mxc_clock clk)
  676. {
  677. freq *= SZ_DEC_1M;
  678. switch (clk) {
  679. case MXC_ARM_CLK:
  680. if (config_core_clk(ref, freq))
  681. return -EINVAL;
  682. break;
  683. case MXC_PERIPH_CLK:
  684. if (config_periph_clk(ref, freq))
  685. return -EINVAL;
  686. break;
  687. case MXC_DDR_CLK:
  688. if (config_ddr_clk(freq))
  689. return -EINVAL;
  690. break;
  691. case MXC_NFC_CLK:
  692. if (config_nfc_clk(freq))
  693. return -EINVAL;
  694. break;
  695. default:
  696. printf("Warning:Unsupported or invalid clock type\n");
  697. }
  698. return 0;
  699. }
  700. #ifdef CONFIG_MX53
  701. /*
  702. * The clock for the external interface can be set to use internal clock
  703. * if fuse bank 4, row 3, bit 2 is set.
  704. * This is an undocumented feature and it was confirmed by Freescale's support:
  705. * Fuses (but not pins) may be used to configure SATA clocks.
  706. * Particularly the i.MX53 Fuse_Map contains the next information
  707. * about configuring SATA clocks : SATA_ALT_REF_CLK[1:0] (offset 0x180C)
  708. * '00' - 100MHz (External)
  709. * '01' - 50MHz (External)
  710. * '10' - 120MHz, internal (USB PHY)
  711. * '11' - Reserved
  712. */
  713. void mxc_set_sata_internal_clock(void)
  714. {
  715. u32 *tmp_base =
  716. (u32 *)(IIM_BASE_ADDR + 0x180c);
  717. set_usb_phy_clk();
  718. clrsetbits_le32(tmp_base, 0x6, 0x4);
  719. }
  720. #endif
  721. /*
  722. * Dump some core clockes.
  723. */
  724. int do_mx5_showclocks(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  725. {
  726. u32 freq;
  727. freq = decode_pll(mxc_plls[PLL1_CLOCK], MXC_HCLK);
  728. printf("PLL1 %8d MHz\n", freq / 1000000);
  729. freq = decode_pll(mxc_plls[PLL2_CLOCK], MXC_HCLK);
  730. printf("PLL2 %8d MHz\n", freq / 1000000);
  731. freq = decode_pll(mxc_plls[PLL3_CLOCK], MXC_HCLK);
  732. printf("PLL3 %8d MHz\n", freq / 1000000);
  733. #ifdef CONFIG_MX53
  734. freq = decode_pll(mxc_plls[PLL4_CLOCK], MXC_HCLK);
  735. printf("PLL4 %8d MHz\n", freq / 1000000);
  736. #endif
  737. printf("\n");
  738. printf("AHB %8d kHz\n", mxc_get_clock(MXC_AHB_CLK) / 1000);
  739. printf("IPG %8d kHz\n", mxc_get_clock(MXC_IPG_CLK) / 1000);
  740. printf("IPG PERCLK %8d kHz\n", mxc_get_clock(MXC_IPG_PERCLK) / 1000);
  741. printf("DDR %8d kHz\n", mxc_get_clock(MXC_DDR_CLK) / 1000);
  742. return 0;
  743. }
  744. /***************************************************/
  745. U_BOOT_CMD(
  746. clocks, CONFIG_SYS_MAXARGS, 1, do_mx5_showclocks,
  747. "display clocks",
  748. ""
  749. );