clock_sun6i.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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. const int m = 8; /* 3 MHz steps just like sun4i, sun5i and sun7i */
  131. if (clk == 0) {
  132. clrbits_le32(&ccm->pll3_cfg, CCM_PLL3_CTRL_EN);
  133. return;
  134. }
  135. /* PLL3 rate = 24000000 * n / m */
  136. writel(CCM_PLL3_CTRL_EN | CCM_PLL3_CTRL_INTEGER_MODE |
  137. CCM_PLL3_CTRL_N(clk / (24000000 / m)) | CCM_PLL3_CTRL_M(m),
  138. &ccm->pll3_cfg);
  139. }
  140. #ifdef CONFIG_SUNXI_DE2
  141. void clock_set_pll3_factors(int m, int n)
  142. {
  143. struct sunxi_ccm_reg * const ccm =
  144. (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
  145. /* PLL3 rate = 24000000 * n / m */
  146. writel(CCM_PLL3_CTRL_EN | CCM_PLL3_CTRL_INTEGER_MODE |
  147. CCM_PLL3_CTRL_N(n) | CCM_PLL3_CTRL_M(m),
  148. &ccm->pll3_cfg);
  149. while (!(readl(&ccm->pll3_cfg) & CCM_PLL3_CTRL_LOCK))
  150. ;
  151. }
  152. #endif
  153. void clock_set_pll5(unsigned int clk, bool sigma_delta_enable)
  154. {
  155. struct sunxi_ccm_reg * const ccm =
  156. (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
  157. const int max_n = 32;
  158. int k = 1, m = 2;
  159. #ifdef CONFIG_MACH_SUNXI_H3_H5
  160. clrsetbits_le32(&ccm->pll5_tuning_cfg, CCM_PLL5_TUN_LOCK_TIME_MASK |
  161. CCM_PLL5_TUN_INIT_FREQ_MASK,
  162. CCM_PLL5_TUN_LOCK_TIME(2) | CCM_PLL5_TUN_INIT_FREQ(16));
  163. #endif
  164. if (sigma_delta_enable)
  165. writel(CCM_PLL5_PATTERN, &ccm->pll5_pattern_cfg);
  166. /* PLL5 rate = 24000000 * n * k / m */
  167. if (clk > 24000000 * k * max_n / m) {
  168. m = 1;
  169. if (clk > 24000000 * k * max_n / m)
  170. k = 2;
  171. }
  172. writel(CCM_PLL5_CTRL_EN |
  173. (sigma_delta_enable ? CCM_PLL5_CTRL_SIGMA_DELTA_EN : 0) |
  174. CCM_PLL5_CTRL_UPD |
  175. CCM_PLL5_CTRL_N(clk / (24000000 * k / m)) |
  176. CCM_PLL5_CTRL_K(k) | CCM_PLL5_CTRL_M(m), &ccm->pll5_cfg);
  177. udelay(5500);
  178. }
  179. #ifdef CONFIG_MACH_SUN6I
  180. void clock_set_mipi_pll(unsigned int clk)
  181. {
  182. struct sunxi_ccm_reg * const ccm =
  183. (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
  184. unsigned int k, m, n, value, diff;
  185. unsigned best_k = 0, best_m = 0, best_n = 0, best_diff = 0xffffffff;
  186. unsigned int src = clock_get_pll3();
  187. /* All calculations are in KHz to avoid overflows */
  188. clk /= 1000;
  189. src /= 1000;
  190. /* Pick the closest lower clock */
  191. for (k = 1; k <= 4; k++) {
  192. for (m = 1; m <= 16; m++) {
  193. for (n = 1; n <= 16; n++) {
  194. value = src * n * k / m;
  195. if (value > clk)
  196. continue;
  197. diff = clk - value;
  198. if (diff < best_diff) {
  199. best_diff = diff;
  200. best_k = k;
  201. best_m = m;
  202. best_n = n;
  203. }
  204. if (diff == 0)
  205. goto done;
  206. }
  207. }
  208. }
  209. done:
  210. writel(CCM_MIPI_PLL_CTRL_EN | CCM_MIPI_PLL_CTRL_LDO_EN |
  211. CCM_MIPI_PLL_CTRL_N(best_n) | CCM_MIPI_PLL_CTRL_K(best_k) |
  212. CCM_MIPI_PLL_CTRL_M(best_m), &ccm->mipi_pll_cfg);
  213. }
  214. #endif
  215. #ifdef CONFIG_SUNXI_DE2
  216. void clock_set_pll10(unsigned int clk)
  217. {
  218. struct sunxi_ccm_reg * const ccm =
  219. (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
  220. const int m = 2; /* 12 MHz steps */
  221. if (clk == 0) {
  222. clrbits_le32(&ccm->pll10_cfg, CCM_PLL10_CTRL_EN);
  223. return;
  224. }
  225. /* PLL10 rate = 24000000 * n / m */
  226. writel(CCM_PLL10_CTRL_EN | CCM_PLL10_CTRL_INTEGER_MODE |
  227. CCM_PLL10_CTRL_N(clk / (24000000 / m)) | CCM_PLL10_CTRL_M(m),
  228. &ccm->pll10_cfg);
  229. while (!(readl(&ccm->pll10_cfg) & CCM_PLL10_CTRL_LOCK))
  230. ;
  231. }
  232. #endif
  233. #if defined(CONFIG_MACH_SUN8I_A33) || \
  234. defined(CONFIG_MACH_SUN8I_R40) || \
  235. defined(CONFIG_MACH_SUN50I)
  236. void clock_set_pll11(unsigned int clk, bool sigma_delta_enable)
  237. {
  238. struct sunxi_ccm_reg * const ccm =
  239. (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
  240. if (sigma_delta_enable)
  241. writel(CCM_PLL11_PATTERN, &ccm->pll11_pattern_cfg0);
  242. writel(CCM_PLL11_CTRL_EN | CCM_PLL11_CTRL_UPD |
  243. (sigma_delta_enable ? CCM_PLL11_CTRL_SIGMA_DELTA_EN : 0) |
  244. CCM_PLL11_CTRL_N(clk / 24000000), &ccm->pll11_cfg);
  245. while (readl(&ccm->pll11_cfg) & CCM_PLL11_CTRL_UPD)
  246. ;
  247. }
  248. #endif
  249. unsigned int clock_get_pll3(void)
  250. {
  251. struct sunxi_ccm_reg *const ccm =
  252. (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
  253. uint32_t rval = readl(&ccm->pll3_cfg);
  254. int n = ((rval & CCM_PLL3_CTRL_N_MASK) >> CCM_PLL3_CTRL_N_SHIFT) + 1;
  255. int m = ((rval & CCM_PLL3_CTRL_M_MASK) >> CCM_PLL3_CTRL_M_SHIFT) + 1;
  256. /* Multiply by 1000 after dividing by m to avoid integer overflows */
  257. return (24000 * n / m) * 1000;
  258. }
  259. unsigned int clock_get_pll6(void)
  260. {
  261. struct sunxi_ccm_reg *const ccm =
  262. (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
  263. uint32_t rval = readl(&ccm->pll6_cfg);
  264. int n = ((rval & CCM_PLL6_CTRL_N_MASK) >> CCM_PLL6_CTRL_N_SHIFT) + 1;
  265. int k = ((rval & CCM_PLL6_CTRL_K_MASK) >> CCM_PLL6_CTRL_K_SHIFT) + 1;
  266. return 24000000 * n * k / 2;
  267. }
  268. unsigned int clock_get_mipi_pll(void)
  269. {
  270. struct sunxi_ccm_reg *const ccm =
  271. (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
  272. uint32_t rval = readl(&ccm->mipi_pll_cfg);
  273. unsigned int n = ((rval & CCM_MIPI_PLL_CTRL_N_MASK) >> CCM_MIPI_PLL_CTRL_N_SHIFT) + 1;
  274. unsigned int k = ((rval & CCM_MIPI_PLL_CTRL_K_MASK) >> CCM_MIPI_PLL_CTRL_K_SHIFT) + 1;
  275. unsigned int m = ((rval & CCM_MIPI_PLL_CTRL_M_MASK) >> CCM_MIPI_PLL_CTRL_M_SHIFT) + 1;
  276. unsigned int src = clock_get_pll3();
  277. /* Multiply by 1000 after dividing by m to avoid integer overflows */
  278. return ((src / 1000) * n * k / m) * 1000;
  279. }
  280. void clock_set_de_mod_clock(u32 *clk_cfg, unsigned int hz)
  281. {
  282. int pll = clock_get_pll6() * 2;
  283. int div = 1;
  284. while ((pll / div) > hz)
  285. div++;
  286. writel(CCM_DE_CTRL_GATE | CCM_DE_CTRL_PLL6_2X | CCM_DE_CTRL_M(div),
  287. clk_cfg);
  288. }