clock_sun6i.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * sun6i specific clock code
  4. *
  5. * (C) Copyright 2007-2012
  6. * Allwinner Technology Co., Ltd. <www.allwinnertech.com>
  7. * Tom Cubie <tangliang@allwinnertech.com>
  8. *
  9. * (C) Copyright 2013 Luke Kenneth Casson Leighton <lkcl@lkcl.net>
  10. */
  11. #include <common.h>
  12. #include <asm/io.h>
  13. #include <asm/arch/clock.h>
  14. #include <asm/arch/prcm.h>
  15. #include <asm/arch/sys_proto.h>
  16. #ifdef CONFIG_SPL_BUILD
  17. void clock_init_safe(void)
  18. {
  19. struct sunxi_ccm_reg * const ccm =
  20. (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
  21. #if !defined(CONFIG_MACH_SUNXI_H3_H5) && !defined(CONFIG_MACH_SUN50I)
  22. struct sunxi_prcm_reg * const prcm =
  23. (struct sunxi_prcm_reg *)SUNXI_PRCM_BASE;
  24. /* Set PLL ldo voltage without this PLL6 does not work properly */
  25. clrsetbits_le32(&prcm->pll_ctrl1, PRCM_PLL_CTRL_LDO_KEY_MASK,
  26. PRCM_PLL_CTRL_LDO_KEY);
  27. clrsetbits_le32(&prcm->pll_ctrl1, ~PRCM_PLL_CTRL_LDO_KEY_MASK,
  28. PRCM_PLL_CTRL_LDO_DIGITAL_EN | PRCM_PLL_CTRL_LDO_ANALOG_EN |
  29. PRCM_PLL_CTRL_EXT_OSC_EN | PRCM_PLL_CTRL_LDO_OUT_L(1140));
  30. clrbits_le32(&prcm->pll_ctrl1, PRCM_PLL_CTRL_LDO_KEY_MASK);
  31. #endif
  32. #if defined(CONFIG_MACH_SUN8I_R40) || defined(CONFIG_MACH_SUN50I)
  33. /* Set PLL lock enable bits and switch to old lock mode */
  34. writel(GENMASK(12, 0), &ccm->pll_lock_ctrl);
  35. #endif
  36. clock_set_pll1(408000000);
  37. writel(PLL6_CFG_DEFAULT, &ccm->pll6_cfg);
  38. while (!(readl(&ccm->pll6_cfg) & CCM_PLL6_CTRL_LOCK))
  39. ;
  40. writel(AHB1_ABP1_DIV_DEFAULT, &ccm->ahb1_apb1_div);
  41. writel(MBUS_CLK_DEFAULT, &ccm->mbus0_clk_cfg);
  42. if (IS_ENABLED(CONFIG_MACH_SUN6I))
  43. writel(MBUS_CLK_DEFAULT, &ccm->mbus1_clk_cfg);
  44. #if defined(CONFIG_MACH_SUN8I_R40) && defined(CONFIG_SUNXI_AHCI)
  45. setbits_le32(&ccm->sata_pll_cfg, CCM_SATA_PLL_DEFAULT);
  46. setbits_le32(&ccm->ahb_reset0_cfg, 0x1 << AHB_GATE_OFFSET_SATA);
  47. setbits_le32(&ccm->ahb_gate0, 0x1 << AHB_GATE_OFFSET_SATA);
  48. setbits_le32(&ccm->sata_clk_cfg, CCM_SATA_CTRL_ENABLE);
  49. #endif
  50. }
  51. #endif
  52. void clock_init_sec(void)
  53. {
  54. #ifdef CONFIG_MACH_SUNXI_H3_H5
  55. struct sunxi_ccm_reg * const ccm =
  56. (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
  57. struct sunxi_prcm_reg * const prcm =
  58. (struct sunxi_prcm_reg *)SUNXI_PRCM_BASE;
  59. setbits_le32(&ccm->ccu_sec_switch,
  60. CCM_SEC_SWITCH_MBUS_NONSEC |
  61. CCM_SEC_SWITCH_BUS_NONSEC |
  62. CCM_SEC_SWITCH_PLL_NONSEC);
  63. setbits_le32(&prcm->prcm_sec_switch,
  64. PRCM_SEC_SWITCH_APB0_CLK_NONSEC |
  65. PRCM_SEC_SWITCH_PLL_CFG_NONSEC |
  66. PRCM_SEC_SWITCH_PWR_GATE_NONSEC);
  67. #endif
  68. }
  69. void clock_init_uart(void)
  70. {
  71. #if CONFIG_CONS_INDEX < 5
  72. struct sunxi_ccm_reg *const ccm =
  73. (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
  74. /* uart clock source is apb2 */
  75. writel(APB2_CLK_SRC_OSC24M|
  76. APB2_CLK_RATE_N_1|
  77. APB2_CLK_RATE_M(1),
  78. &ccm->apb2_div);
  79. /* open the clock for uart */
  80. setbits_le32(&ccm->apb2_gate,
  81. CLK_GATE_OPEN << (APB2_GATE_UART_SHIFT +
  82. CONFIG_CONS_INDEX - 1));
  83. /* deassert uart reset */
  84. setbits_le32(&ccm->apb2_reset_cfg,
  85. 1 << (APB2_RESET_UART_SHIFT +
  86. CONFIG_CONS_INDEX - 1));
  87. #else
  88. /* enable R_PIO and R_UART clocks, and de-assert resets */
  89. prcm_apb0_enable(PRCM_APB0_GATE_PIO | PRCM_APB0_GATE_UART);
  90. #endif
  91. }
  92. #ifdef CONFIG_SPL_BUILD
  93. void clock_set_pll1(unsigned int clk)
  94. {
  95. struct sunxi_ccm_reg * const ccm =
  96. (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
  97. const int p = 0;
  98. int k = 1;
  99. int m = 1;
  100. if (clk > 1152000000) {
  101. k = 2;
  102. } else if (clk > 768000000) {
  103. k = 3;
  104. m = 2;
  105. }
  106. /* Switch to 24MHz clock while changing PLL1 */
  107. writel(AXI_DIV_3 << AXI_DIV_SHIFT |
  108. ATB_DIV_2 << ATB_DIV_SHIFT |
  109. CPU_CLK_SRC_OSC24M << CPU_CLK_SRC_SHIFT,
  110. &ccm->cpu_axi_cfg);
  111. /*
  112. * sun6i: PLL1 rate = ((24000000 * n * k) >> 0) / m (p is ignored)
  113. * sun8i: PLL1 rate = ((24000000 * n * k) >> p) / m
  114. */
  115. writel(CCM_PLL1_CTRL_EN | CCM_PLL1_CTRL_P(p) |
  116. CCM_PLL1_CTRL_N(clk / (24000000 * k / m)) |
  117. CCM_PLL1_CTRL_K(k) | CCM_PLL1_CTRL_M(m), &ccm->pll1_cfg);
  118. sdelay(200);
  119. /* Switch CPU to PLL1 */
  120. writel(AXI_DIV_3 << AXI_DIV_SHIFT |
  121. ATB_DIV_2 << ATB_DIV_SHIFT |
  122. CPU_CLK_SRC_PLL1 << CPU_CLK_SRC_SHIFT,
  123. &ccm->cpu_axi_cfg);
  124. }
  125. #endif
  126. void clock_set_pll3(unsigned int clk)
  127. {
  128. struct sunxi_ccm_reg * const ccm =
  129. (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
  130. #ifdef CONFIG_SUNXI_DE2
  131. const int m = 4; /* 6 MHz steps to allow higher frequency for DE2 */
  132. #else
  133. const int m = 8; /* 3 MHz steps just like sun4i, sun5i and sun7i */
  134. #endif
  135. if (clk == 0) {
  136. clrbits_le32(&ccm->pll3_cfg, CCM_PLL3_CTRL_EN);
  137. return;
  138. }
  139. /* PLL3 rate = 24000000 * n / m */
  140. writel(CCM_PLL3_CTRL_EN | CCM_PLL3_CTRL_INTEGER_MODE |
  141. CCM_PLL3_CTRL_N(clk / (24000000 / m)) | CCM_PLL3_CTRL_M(m),
  142. &ccm->pll3_cfg);
  143. }
  144. #ifdef CONFIG_SUNXI_DE2
  145. void clock_set_pll3_factors(int m, int n)
  146. {
  147. struct sunxi_ccm_reg * const ccm =
  148. (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
  149. /* PLL3 rate = 24000000 * n / m */
  150. writel(CCM_PLL3_CTRL_EN | CCM_PLL3_CTRL_INTEGER_MODE |
  151. CCM_PLL3_CTRL_N(n) | CCM_PLL3_CTRL_M(m),
  152. &ccm->pll3_cfg);
  153. while (!(readl(&ccm->pll3_cfg) & CCM_PLL3_CTRL_LOCK))
  154. ;
  155. }
  156. #endif
  157. void clock_set_pll5(unsigned int clk, bool sigma_delta_enable)
  158. {
  159. struct sunxi_ccm_reg * const ccm =
  160. (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
  161. const int max_n = 32;
  162. int k = 1, m = 2;
  163. #ifdef CONFIG_MACH_SUNXI_H3_H5
  164. clrsetbits_le32(&ccm->pll5_tuning_cfg, CCM_PLL5_TUN_LOCK_TIME_MASK |
  165. CCM_PLL5_TUN_INIT_FREQ_MASK,
  166. CCM_PLL5_TUN_LOCK_TIME(2) | CCM_PLL5_TUN_INIT_FREQ(16));
  167. #endif
  168. if (sigma_delta_enable)
  169. writel(CCM_PLL5_PATTERN, &ccm->pll5_pattern_cfg);
  170. /* PLL5 rate = 24000000 * n * k / m */
  171. if (clk > 24000000 * k * max_n / m) {
  172. m = 1;
  173. if (clk > 24000000 * k * max_n / m)
  174. k = 2;
  175. }
  176. writel(CCM_PLL5_CTRL_EN |
  177. (sigma_delta_enable ? CCM_PLL5_CTRL_SIGMA_DELTA_EN : 0) |
  178. CCM_PLL5_CTRL_UPD |
  179. CCM_PLL5_CTRL_N(clk / (24000000 * k / m)) |
  180. CCM_PLL5_CTRL_K(k) | CCM_PLL5_CTRL_M(m), &ccm->pll5_cfg);
  181. udelay(5500);
  182. }
  183. #ifdef CONFIG_MACH_SUN6I
  184. void clock_set_mipi_pll(unsigned int clk)
  185. {
  186. struct sunxi_ccm_reg * const ccm =
  187. (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
  188. unsigned int k, m, n, value, diff;
  189. unsigned best_k = 0, best_m = 0, best_n = 0, best_diff = 0xffffffff;
  190. unsigned int src = clock_get_pll3();
  191. /* All calculations are in KHz to avoid overflows */
  192. clk /= 1000;
  193. src /= 1000;
  194. /* Pick the closest lower clock */
  195. for (k = 1; k <= 4; k++) {
  196. for (m = 1; m <= 16; m++) {
  197. for (n = 1; n <= 16; n++) {
  198. value = src * n * k / m;
  199. if (value > clk)
  200. continue;
  201. diff = clk - value;
  202. if (diff < best_diff) {
  203. best_diff = diff;
  204. best_k = k;
  205. best_m = m;
  206. best_n = n;
  207. }
  208. if (diff == 0)
  209. goto done;
  210. }
  211. }
  212. }
  213. done:
  214. writel(CCM_MIPI_PLL_CTRL_EN | CCM_MIPI_PLL_CTRL_LDO_EN |
  215. CCM_MIPI_PLL_CTRL_N(best_n) | CCM_MIPI_PLL_CTRL_K(best_k) |
  216. CCM_MIPI_PLL_CTRL_M(best_m), &ccm->mipi_pll_cfg);
  217. }
  218. #endif
  219. #ifdef CONFIG_SUNXI_DE2
  220. void clock_set_pll10(unsigned int clk)
  221. {
  222. struct sunxi_ccm_reg * const ccm =
  223. (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
  224. const int m = 2; /* 12 MHz steps */
  225. if (clk == 0) {
  226. clrbits_le32(&ccm->pll10_cfg, CCM_PLL10_CTRL_EN);
  227. return;
  228. }
  229. /* PLL10 rate = 24000000 * n / m */
  230. writel(CCM_PLL10_CTRL_EN | CCM_PLL10_CTRL_INTEGER_MODE |
  231. CCM_PLL10_CTRL_N(clk / (24000000 / m)) | CCM_PLL10_CTRL_M(m),
  232. &ccm->pll10_cfg);
  233. while (!(readl(&ccm->pll10_cfg) & CCM_PLL10_CTRL_LOCK))
  234. ;
  235. }
  236. #endif
  237. #if defined(CONFIG_MACH_SUN8I_A33) || \
  238. defined(CONFIG_MACH_SUN8I_R40) || \
  239. defined(CONFIG_MACH_SUN50I)
  240. void clock_set_pll11(unsigned int clk, bool sigma_delta_enable)
  241. {
  242. struct sunxi_ccm_reg * const ccm =
  243. (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
  244. if (sigma_delta_enable)
  245. writel(CCM_PLL11_PATTERN, &ccm->pll11_pattern_cfg0);
  246. writel(CCM_PLL11_CTRL_EN | CCM_PLL11_CTRL_UPD |
  247. (sigma_delta_enable ? CCM_PLL11_CTRL_SIGMA_DELTA_EN : 0) |
  248. CCM_PLL11_CTRL_N(clk / 24000000), &ccm->pll11_cfg);
  249. while (readl(&ccm->pll11_cfg) & CCM_PLL11_CTRL_UPD)
  250. ;
  251. }
  252. #endif
  253. unsigned int clock_get_pll3(void)
  254. {
  255. struct sunxi_ccm_reg *const ccm =
  256. (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
  257. uint32_t rval = readl(&ccm->pll3_cfg);
  258. int n = ((rval & CCM_PLL3_CTRL_N_MASK) >> CCM_PLL3_CTRL_N_SHIFT) + 1;
  259. int m = ((rval & CCM_PLL3_CTRL_M_MASK) >> CCM_PLL3_CTRL_M_SHIFT) + 1;
  260. /* Multiply by 1000 after dividing by m to avoid integer overflows */
  261. return (24000 * n / m) * 1000;
  262. }
  263. unsigned int clock_get_pll6(void)
  264. {
  265. struct sunxi_ccm_reg *const ccm =
  266. (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
  267. uint32_t rval = readl(&ccm->pll6_cfg);
  268. int n = ((rval & CCM_PLL6_CTRL_N_MASK) >> CCM_PLL6_CTRL_N_SHIFT) + 1;
  269. int k = ((rval & CCM_PLL6_CTRL_K_MASK) >> CCM_PLL6_CTRL_K_SHIFT) + 1;
  270. return 24000000 * n * k / 2;
  271. }
  272. unsigned int clock_get_mipi_pll(void)
  273. {
  274. struct sunxi_ccm_reg *const ccm =
  275. (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
  276. uint32_t rval = readl(&ccm->mipi_pll_cfg);
  277. unsigned int n = ((rval & CCM_MIPI_PLL_CTRL_N_MASK) >> CCM_MIPI_PLL_CTRL_N_SHIFT) + 1;
  278. unsigned int k = ((rval & CCM_MIPI_PLL_CTRL_K_MASK) >> CCM_MIPI_PLL_CTRL_K_SHIFT) + 1;
  279. unsigned int m = ((rval & CCM_MIPI_PLL_CTRL_M_MASK) >> CCM_MIPI_PLL_CTRL_M_SHIFT) + 1;
  280. unsigned int src = clock_get_pll3();
  281. /* Multiply by 1000 after dividing by m to avoid integer overflows */
  282. return ((src / 1000) * n * k / m) * 1000;
  283. }
  284. void clock_set_de_mod_clock(u32 *clk_cfg, unsigned int hz)
  285. {
  286. int pll = clock_get_pll6() * 2;
  287. int div = 1;
  288. while ((pll / div) > hz)
  289. div++;
  290. writel(CCM_DE_CTRL_GATE | CCM_DE_CTRL_PLL6_2X | CCM_DE_CTRL_M(div),
  291. clk_cfg);
  292. }