clock_ti814x.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. /*
  2. * clock_ti814x.c
  3. *
  4. * Clocks for TI814X based boards
  5. *
  6. * Copyright (C) 2013, Texas Instruments, Incorporated
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR /PURPOSE. See the
  16. * GNU General Public License for more details.
  17. */
  18. #include <common.h>
  19. #include <asm/arch/cpu.h>
  20. #include <asm/arch/clock.h>
  21. #include <asm/arch/hardware.h>
  22. #include <asm/io.h>
  23. /* PRCM */
  24. #define PRCM_MOD_EN 0x2
  25. /* CLK_SRC */
  26. #define OSC_SRC0 0
  27. #define OSC_SRC1 1
  28. #define L3_OSC_SRC OSC_SRC0
  29. #define OSC_0_FREQ 20
  30. #define DCO_HS2_MIN 500
  31. #define DCO_HS2_MAX 1000
  32. #define DCO_HS1_MIN 1000
  33. #define DCO_HS1_MAX 2000
  34. #define SELFREQDCO_HS2 0x00000801
  35. #define SELFREQDCO_HS1 0x00001001
  36. #define MPU_N 0x1
  37. #define MPU_M 0x3C
  38. #define MPU_M2 1
  39. #define MPU_CLKCTRL 0x1
  40. #define L3_N 19
  41. #define L3_M 880
  42. #define L3_M2 4
  43. #define L3_CLKCTRL 0x801
  44. #define DDR_N 19
  45. #define DDR_M 666
  46. #define DDR_M2 2
  47. #define DDR_CLKCTRL 0x801
  48. /* ADPLLJ register values */
  49. #define ADPLLJ_CLKCTRL_HS2 0x00000801 /* HS2 mode, TINT2 = 1 */
  50. #define ADPLLJ_CLKCTRL_HS1 0x00001001 /* HS1 mode, TINT2 = 1 */
  51. #define ADPLLJ_CLKCTRL_CLKDCOLDOEN (1 << 29)
  52. #define ADPLLJ_CLKCTRL_IDLE (1 << 23)
  53. #define ADPLLJ_CLKCTRL_CLKOUTEN (1 << 20)
  54. #define ADPLLJ_CLKCTRL_CLKOUTLDOEN (1 << 19)
  55. #define ADPLLJ_CLKCTRL_CLKDCOLDOPWDNZ (1 << 17)
  56. #define ADPLLJ_CLKCTRL_LPMODE (1 << 12)
  57. #define ADPLLJ_CLKCTRL_DRIFTGUARDIAN (1 << 11)
  58. #define ADPLLJ_CLKCTRL_REGM4XEN (1 << 10)
  59. #define ADPLLJ_CLKCTRL_TINITZ (1 << 0)
  60. #define ADPLLJ_CLKCTRL_CLKDCO (ADPLLJ_CLKCTRL_CLKDCOLDOEN | \
  61. ADPLLJ_CLKCTRL_CLKOUTEN | \
  62. ADPLLJ_CLKCTRL_CLKOUTLDOEN | \
  63. ADPLLJ_CLKCTRL_CLKDCOLDOPWDNZ)
  64. #define ADPLLJ_STATUS_PHASELOCK (1 << 10)
  65. #define ADPLLJ_STATUS_FREQLOCK (1 << 9)
  66. #define ADPLLJ_STATUS_PHSFRQLOCK (ADPLLJ_STATUS_PHASELOCK | \
  67. ADPLLJ_STATUS_FREQLOCK)
  68. #define ADPLLJ_STATUS_BYPASSACK (1 << 8)
  69. #define ADPLLJ_STATUS_BYPASS (1 << 0)
  70. #define ADPLLJ_STATUS_BYPASSANDACK (ADPLLJ_STATUS_BYPASSACK | \
  71. ADPLLJ_STATUS_BYPASS)
  72. #define ADPLLJ_TENABLE_ENB (1 << 0)
  73. #define ADPLLJ_TENABLEDIV_ENB (1 << 0)
  74. #define ADPLLJ_M2NDIV_M2SHIFT 16
  75. #define MPU_PLL_BASE (PLL_SUBSYS_BASE + 0x048)
  76. #define L3_PLL_BASE (PLL_SUBSYS_BASE + 0x110)
  77. #define DDR_PLL_BASE (PLL_SUBSYS_BASE + 0x290)
  78. struct ad_pll {
  79. unsigned int pwrctrl;
  80. unsigned int clkctrl;
  81. unsigned int tenable;
  82. unsigned int tenablediv;
  83. unsigned int m2ndiv;
  84. unsigned int mn2div;
  85. unsigned int fracdiv;
  86. unsigned int bwctrl;
  87. unsigned int fracctrl;
  88. unsigned int status;
  89. unsigned int m3div;
  90. unsigned int rampctrl;
  91. };
  92. #define OSC_SRC_CTRL (PLL_SUBSYS_BASE + 0x2C0)
  93. /* PRCM */
  94. #define CM_DEFAULT_BASE (PRCM_BASE + 0x0500)
  95. struct cm_def {
  96. unsigned int resv0[2];
  97. unsigned int l3fastclkstctrl;
  98. unsigned int resv1[1];
  99. unsigned int pciclkstctrl;
  100. unsigned int resv2[1];
  101. unsigned int ducaticlkstctrl;
  102. unsigned int resv3[1];
  103. unsigned int emif0clkctrl;
  104. unsigned int emif1clkctrl;
  105. unsigned int dmmclkctrl;
  106. unsigned int fwclkctrl;
  107. unsigned int resv4[10];
  108. unsigned int usbclkctrl;
  109. unsigned int resv5[1];
  110. unsigned int sataclkctrl;
  111. unsigned int resv6[4];
  112. unsigned int ducaticlkctrl;
  113. unsigned int pciclkctrl;
  114. };
  115. #define CM_ALWON_BASE (PRCM_BASE + 0x1400)
  116. struct cm_alwon {
  117. unsigned int l3slowclkstctrl;
  118. unsigned int ethclkstctrl;
  119. unsigned int l3medclkstctrl;
  120. unsigned int mmu_clkstctrl;
  121. unsigned int mmucfg_clkstctrl;
  122. unsigned int ocmc0clkstctrl;
  123. unsigned int vcpclkstctrl;
  124. unsigned int mpuclkstctrl;
  125. unsigned int sysclk4clkstctrl;
  126. unsigned int sysclk5clkstctrl;
  127. unsigned int sysclk6clkstctrl;
  128. unsigned int rtcclkstctrl;
  129. unsigned int l3fastclkstctrl;
  130. unsigned int resv0[67];
  131. unsigned int mcasp0clkctrl;
  132. unsigned int mcasp1clkctrl;
  133. unsigned int mcasp2clkctrl;
  134. unsigned int mcbspclkctrl;
  135. unsigned int uart0clkctrl;
  136. unsigned int uart1clkctrl;
  137. unsigned int uart2clkctrl;
  138. unsigned int gpio0clkctrl;
  139. unsigned int gpio1clkctrl;
  140. unsigned int i2c0clkctrl;
  141. unsigned int i2c1clkctrl;
  142. unsigned int mcasp345clkctrl;
  143. unsigned int atlclkctrl;
  144. unsigned int mlbclkctrl;
  145. unsigned int pataclkctrl;
  146. unsigned int resv1[1];
  147. unsigned int uart3clkctrl;
  148. unsigned int uart4clkctrl;
  149. unsigned int uart5clkctrl;
  150. unsigned int wdtimerclkctrl;
  151. unsigned int spiclkctrl;
  152. unsigned int mailboxclkctrl;
  153. unsigned int spinboxclkctrl;
  154. unsigned int mmudataclkctrl;
  155. unsigned int resv2[2];
  156. unsigned int mmucfgclkctrl;
  157. unsigned int resv3[2];
  158. unsigned int ocmc0clkctrl;
  159. unsigned int vcpclkctrl;
  160. unsigned int resv4[2];
  161. unsigned int controlclkctrl;
  162. unsigned int resv5[2];
  163. unsigned int gpmcclkctrl;
  164. unsigned int ethernet0clkctrl;
  165. unsigned int resv6[1];
  166. unsigned int mpuclkctrl;
  167. unsigned int debugssclkctrl;
  168. unsigned int l3clkctrl;
  169. unsigned int l4hsclkctrl;
  170. unsigned int l4lsclkctrl;
  171. unsigned int rtcclkctrl;
  172. unsigned int tpccclkctrl;
  173. unsigned int tptc0clkctrl;
  174. unsigned int tptc1clkctrl;
  175. unsigned int tptc2clkctrl;
  176. unsigned int tptc3clkctrl;
  177. unsigned int resv7[4];
  178. unsigned int dcan01clkctrl;
  179. unsigned int mmchs0clkctrl;
  180. unsigned int mmchs1clkctrl;
  181. unsigned int mmchs2clkctrl;
  182. unsigned int custefuseclkctrl;
  183. };
  184. const struct cm_alwon *cmalwon = (struct cm_alwon *)CM_ALWON_BASE;
  185. const struct cm_def *cmdef = (struct cm_def *)CM_DEFAULT_BASE;
  186. /*
  187. * Enable the peripheral clock for required peripherals
  188. */
  189. static void enable_per_clocks(void)
  190. {
  191. /* UART0 */
  192. writel(PRCM_MOD_EN, &cmalwon->uart0clkctrl);
  193. while (readl(&cmalwon->uart0clkctrl) != PRCM_MOD_EN)
  194. ;
  195. /* HSMMC1 */
  196. writel(PRCM_MOD_EN, &cmalwon->mmchs1clkctrl);
  197. while (readl(&cmalwon->mmchs1clkctrl) != PRCM_MOD_EN)
  198. ;
  199. }
  200. /*
  201. * select the HS1 or HS2 for DCO Freq
  202. * return : CLKCTRL
  203. */
  204. static u32 pll_dco_freq_sel(u32 clkout_dco)
  205. {
  206. if (clkout_dco >= DCO_HS2_MIN && clkout_dco < DCO_HS2_MAX)
  207. return SELFREQDCO_HS2;
  208. else if (clkout_dco >= DCO_HS1_MIN && clkout_dco < DCO_HS1_MAX)
  209. return SELFREQDCO_HS1;
  210. else
  211. return -1;
  212. }
  213. /*
  214. * select the sigma delta config
  215. * return: sigma delta val
  216. */
  217. static u32 pll_sigma_delta_val(u32 clkout_dco)
  218. {
  219. u32 sig_val = 0;
  220. float frac_div;
  221. frac_div = (float) clkout_dco / 250;
  222. frac_div = frac_div + 0.90;
  223. sig_val = (int)frac_div;
  224. sig_val = sig_val << 24;
  225. return sig_val;
  226. }
  227. /*
  228. * configure individual ADPLLJ
  229. */
  230. static void pll_config(u32 base, u32 n, u32 m, u32 m2,
  231. u32 clkctrl_val, int adpllj)
  232. {
  233. const struct ad_pll *adpll = (struct ad_pll *)base;
  234. u32 m2nval, mn2val, read_clkctrl = 0, clkout_dco = 0;
  235. u32 sig_val = 0, hs_mod = 0;
  236. m2nval = (m2 << ADPLLJ_M2NDIV_M2SHIFT) | n;
  237. mn2val = m;
  238. /* calculate clkout_dco */
  239. clkout_dco = ((OSC_0_FREQ / (n+1)) * m);
  240. /* sigma delta & Hs mode selection skip for ADPLLS*/
  241. if (adpllj) {
  242. sig_val = pll_sigma_delta_val(clkout_dco);
  243. hs_mod = pll_dco_freq_sel(clkout_dco);
  244. }
  245. /* by-pass pll */
  246. read_clkctrl = readl(&adpll->clkctrl);
  247. writel((read_clkctrl | ADPLLJ_CLKCTRL_IDLE), &adpll->clkctrl);
  248. while ((readl(&adpll->status) & ADPLLJ_STATUS_BYPASSANDACK)
  249. != ADPLLJ_STATUS_BYPASSANDACK)
  250. ;
  251. /* clear TINITZ */
  252. read_clkctrl = readl(&adpll->clkctrl);
  253. writel((read_clkctrl & ~ADPLLJ_CLKCTRL_TINITZ), &adpll->clkctrl);
  254. /*
  255. * ref_clk = 20/(n + 1);
  256. * clkout_dco = ref_clk * m;
  257. * clk_out = clkout_dco/m2;
  258. */
  259. read_clkctrl = readl(&adpll->clkctrl) &
  260. ~(ADPLLJ_CLKCTRL_LPMODE |
  261. ADPLLJ_CLKCTRL_DRIFTGUARDIAN |
  262. ADPLLJ_CLKCTRL_REGM4XEN);
  263. writel(m2nval, &adpll->m2ndiv);
  264. writel(mn2val, &adpll->mn2div);
  265. /* Skip for modena(ADPLLS) */
  266. if (adpllj) {
  267. writel(sig_val, &adpll->fracdiv);
  268. writel((read_clkctrl | hs_mod), &adpll->clkctrl);
  269. }
  270. /* Load M2, N2 dividers of ADPLL */
  271. writel(ADPLLJ_TENABLEDIV_ENB, &adpll->tenablediv);
  272. writel(~ADPLLJ_TENABLEDIV_ENB, &adpll->tenablediv);
  273. /* Load M, N dividers of ADPLL */
  274. writel(ADPLLJ_TENABLE_ENB, &adpll->tenable);
  275. writel(~ADPLLJ_TENABLE_ENB, &adpll->tenable);
  276. /* Configure CLKDCOLDOEN,CLKOUTLDOEN,CLKOUT Enable BITS */
  277. read_clkctrl = readl(&adpll->clkctrl) & ~ADPLLJ_CLKCTRL_CLKDCO;
  278. if (adpllj)
  279. writel((read_clkctrl | ADPLLJ_CLKCTRL_CLKDCO),
  280. &adpll->clkctrl);
  281. /* Enable TINTZ and disable IDLE(PLL in Active & Locked Mode */
  282. read_clkctrl = readl(&adpll->clkctrl) & ~ADPLLJ_CLKCTRL_IDLE;
  283. writel((read_clkctrl | ADPLLJ_CLKCTRL_TINITZ), &adpll->clkctrl);
  284. /* Wait for phase and freq lock */
  285. while ((readl(&adpll->status) & ADPLLJ_STATUS_PHSFRQLOCK) !=
  286. ADPLLJ_STATUS_PHSFRQLOCK)
  287. ;
  288. }
  289. static void unlock_pll_control_mmr(void)
  290. {
  291. /* TRM 2.10.1.4 and 3.2.7-3.2.11 */
  292. writel(0x1EDA4C3D, 0x481C5040);
  293. writel(0x2FF1AC2B, 0x48140060);
  294. writel(0xF757FDC0, 0x48140064);
  295. writel(0xE2BC3A6D, 0x48140068);
  296. writel(0x1EBF131D, 0x4814006c);
  297. writel(0x6F361E05, 0x48140070);
  298. }
  299. static void mpu_pll_config(void)
  300. {
  301. pll_config(MPU_PLL_BASE, MPU_N, MPU_M, MPU_M2, MPU_CLKCTRL, 0);
  302. }
  303. static void l3_pll_config(void)
  304. {
  305. u32 l3_osc_src, rd_osc_src = 0;
  306. l3_osc_src = L3_OSC_SRC;
  307. rd_osc_src = readl(OSC_SRC_CTRL);
  308. if (OSC_SRC0 == l3_osc_src)
  309. writel((rd_osc_src & 0xfffffffe)|0x0, OSC_SRC_CTRL);
  310. else
  311. writel((rd_osc_src & 0xfffffffe)|0x1, OSC_SRC_CTRL);
  312. pll_config(L3_PLL_BASE, L3_N, L3_M, L3_M2, L3_CLKCTRL, 1);
  313. }
  314. void ddr_pll_config(unsigned int ddrpll_m)
  315. {
  316. pll_config(DDR_PLL_BASE, DDR_N, DDR_M, DDR_M2, DDR_CLKCTRL, 1);
  317. }
  318. void enable_emif_clocks(void) {};
  319. void enable_dmm_clocks(void)
  320. {
  321. writel(PRCM_MOD_EN, &cmdef->fwclkctrl);
  322. writel(PRCM_MOD_EN, &cmdef->l3fastclkstctrl);
  323. writel(PRCM_MOD_EN, &cmdef->emif0clkctrl);
  324. while ((readl(&cmdef->emif0clkctrl)) != PRCM_MOD_EN)
  325. ;
  326. writel(PRCM_MOD_EN, &cmdef->emif1clkctrl);
  327. while ((readl(&cmdef->emif1clkctrl)) != PRCM_MOD_EN)
  328. ;
  329. while ((readl(&cmdef->l3fastclkstctrl) & 0x300) != 0x300)
  330. ;
  331. writel(PRCM_MOD_EN, &cmdef->dmmclkctrl);
  332. while ((readl(&cmdef->dmmclkctrl)) != PRCM_MOD_EN)
  333. ;
  334. writel(PRCM_MOD_EN, &cmalwon->l3slowclkstctrl);
  335. while ((readl(&cmalwon->l3slowclkstctrl) & 0x2100) != 0x2100)
  336. ;
  337. }
  338. /*
  339. * Configure the PLL/PRCM for necessary peripherals
  340. */
  341. void pll_init()
  342. {
  343. unlock_pll_control_mmr();
  344. /* Enable the control module */
  345. writel(PRCM_MOD_EN, &cmalwon->controlclkctrl);
  346. mpu_pll_config();
  347. l3_pll_config();
  348. /* Enable the required peripherals */
  349. enable_per_clocks();
  350. }