clk_ast2500.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * (C) Copyright 2016 Google, Inc
  4. */
  5. #include <common.h>
  6. #include <clk-uclass.h>
  7. #include <dm.h>
  8. #include <asm/io.h>
  9. #include <asm/arch/scu_ast2500.h>
  10. #include <dm/lists.h>
  11. #include <dt-bindings/clock/ast2500-scu.h>
  12. /*
  13. * MAC Clock Delay settings, taken from Aspeed SDK
  14. */
  15. #define RGMII_TXCLK_ODLY 8
  16. #define RMII_RXCLK_IDLY 2
  17. /*
  18. * TGMII Clock Duty constants, taken from Aspeed SDK
  19. */
  20. #define RGMII2_TXCK_DUTY 0x66
  21. #define RGMII1_TXCK_DUTY 0x64
  22. #define D2PLL_DEFAULT_RATE (250 * 1000 * 1000)
  23. DECLARE_GLOBAL_DATA_PTR;
  24. /*
  25. * Clock divider/multiplier configuration struct.
  26. * For H-PLL and M-PLL the formula is
  27. * (Output Frequency) = CLKIN * ((M + 1) / (N + 1)) / (P + 1)
  28. * M - Numerator
  29. * N - Denumerator
  30. * P - Post Divider
  31. * They have the same layout in their control register.
  32. *
  33. * D-PLL and D2-PLL have extra divider (OD + 1), which is not
  34. * yet needed and ignored by clock configurations.
  35. */
  36. struct ast2500_div_config {
  37. unsigned int num;
  38. unsigned int denum;
  39. unsigned int post_div;
  40. };
  41. /*
  42. * Get the rate of the M-PLL clock from input clock frequency and
  43. * the value of the M-PLL Parameter Register.
  44. */
  45. static ulong ast2500_get_mpll_rate(ulong clkin, u32 mpll_reg)
  46. {
  47. const ulong num = (mpll_reg & SCU_MPLL_NUM_MASK) >> SCU_MPLL_NUM_SHIFT;
  48. const ulong denum = (mpll_reg & SCU_MPLL_DENUM_MASK)
  49. >> SCU_MPLL_DENUM_SHIFT;
  50. const ulong post_div = (mpll_reg & SCU_MPLL_POST_MASK)
  51. >> SCU_MPLL_POST_SHIFT;
  52. return (clkin * ((num + 1) / (denum + 1))) / (post_div + 1);
  53. }
  54. /*
  55. * Get the rate of the H-PLL clock from input clock frequency and
  56. * the value of the H-PLL Parameter Register.
  57. */
  58. static ulong ast2500_get_hpll_rate(ulong clkin, u32 hpll_reg)
  59. {
  60. const ulong num = (hpll_reg & SCU_HPLL_NUM_MASK) >> SCU_HPLL_NUM_SHIFT;
  61. const ulong denum = (hpll_reg & SCU_HPLL_DENUM_MASK)
  62. >> SCU_HPLL_DENUM_SHIFT;
  63. const ulong post_div = (hpll_reg & SCU_HPLL_POST_MASK)
  64. >> SCU_HPLL_POST_SHIFT;
  65. return (clkin * ((num + 1) / (denum + 1))) / (post_div + 1);
  66. }
  67. static ulong ast2500_get_clkin(struct ast2500_scu *scu)
  68. {
  69. return readl(&scu->hwstrap) & SCU_HWSTRAP_CLKIN_25MHZ
  70. ? 25 * 1000 * 1000 : 24 * 1000 * 1000;
  71. }
  72. /**
  73. * Get current rate or uart clock
  74. *
  75. * @scu SCU registers
  76. * @uart_index UART index, 1-5
  77. *
  78. * @return current setting for uart clock rate
  79. */
  80. static ulong ast2500_get_uart_clk_rate(struct ast2500_scu *scu, int uart_index)
  81. {
  82. /*
  83. * ast2500 datasheet is very confusing when it comes to UART clocks,
  84. * especially when CLKIN = 25 MHz. The settings are in
  85. * different registers and it is unclear how they interact.
  86. *
  87. * This has only been tested with default settings and CLKIN = 24 MHz.
  88. */
  89. ulong uart_clkin;
  90. if (readl(&scu->misc_ctrl2) &
  91. (1 << (uart_index - 1 + SCU_MISC2_UARTCLK_SHIFT)))
  92. uart_clkin = 192 * 1000 * 1000;
  93. else
  94. uart_clkin = 24 * 1000 * 1000;
  95. if (readl(&scu->misc_ctrl1) & SCU_MISC_UARTCLK_DIV13)
  96. uart_clkin /= 13;
  97. return uart_clkin;
  98. }
  99. static ulong ast2500_clk_get_rate(struct clk *clk)
  100. {
  101. struct ast2500_clk_priv *priv = dev_get_priv(clk->dev);
  102. ulong clkin = ast2500_get_clkin(priv->scu);
  103. ulong rate;
  104. switch (clk->id) {
  105. case PLL_HPLL:
  106. case ARMCLK:
  107. /*
  108. * This ignores dynamic/static slowdown of ARMCLK and may
  109. * be inaccurate.
  110. */
  111. rate = ast2500_get_hpll_rate(clkin,
  112. readl(&priv->scu->h_pll_param));
  113. break;
  114. case MCLK_DDR:
  115. rate = ast2500_get_mpll_rate(clkin,
  116. readl(&priv->scu->m_pll_param));
  117. break;
  118. case BCLK_PCLK:
  119. {
  120. ulong apb_div = 4 + 4 * ((readl(&priv->scu->clk_sel1)
  121. & SCU_PCLK_DIV_MASK)
  122. >> SCU_PCLK_DIV_SHIFT);
  123. rate = ast2500_get_hpll_rate(clkin,
  124. readl(&priv->
  125. scu->h_pll_param));
  126. rate = rate / apb_div;
  127. }
  128. break;
  129. case PCLK_UART1:
  130. rate = ast2500_get_uart_clk_rate(priv->scu, 1);
  131. break;
  132. case PCLK_UART2:
  133. rate = ast2500_get_uart_clk_rate(priv->scu, 2);
  134. break;
  135. case PCLK_UART3:
  136. rate = ast2500_get_uart_clk_rate(priv->scu, 3);
  137. break;
  138. case PCLK_UART4:
  139. rate = ast2500_get_uart_clk_rate(priv->scu, 4);
  140. break;
  141. case PCLK_UART5:
  142. rate = ast2500_get_uart_clk_rate(priv->scu, 5);
  143. break;
  144. default:
  145. return -ENOENT;
  146. }
  147. return rate;
  148. }
  149. /*
  150. * @input_rate - the rate of input clock in Hz
  151. * @requested_rate - desired output rate in Hz
  152. * @div - this is an IN/OUT parameter, at input all fields of the config
  153. * need to be set to their maximum allowed values.
  154. * The result (the best config we could find), would also be returned
  155. * in this structure.
  156. *
  157. * @return The clock rate, when the resulting div_config is used.
  158. */
  159. static ulong ast2500_calc_clock_config(ulong input_rate, ulong requested_rate,
  160. struct ast2500_div_config *cfg)
  161. {
  162. /*
  163. * The assumption is that kHz precision is good enough and
  164. * also enough to avoid overflow when multiplying.
  165. */
  166. const ulong input_rate_khz = input_rate / 1000;
  167. const ulong rate_khz = requested_rate / 1000;
  168. const struct ast2500_div_config max_vals = *cfg;
  169. struct ast2500_div_config it = { 0, 0, 0 };
  170. ulong delta = rate_khz;
  171. ulong new_rate_khz = 0;
  172. for (; it.denum <= max_vals.denum; ++it.denum) {
  173. for (it.post_div = 0; it.post_div <= max_vals.post_div;
  174. ++it.post_div) {
  175. it.num = (rate_khz * (it.post_div + 1) / input_rate_khz)
  176. * (it.denum + 1);
  177. if (it.num > max_vals.num)
  178. continue;
  179. new_rate_khz = (input_rate_khz
  180. * ((it.num + 1) / (it.denum + 1)))
  181. / (it.post_div + 1);
  182. /* Keep the rate below requested one. */
  183. if (new_rate_khz > rate_khz)
  184. continue;
  185. if (new_rate_khz - rate_khz < delta) {
  186. delta = new_rate_khz - rate_khz;
  187. *cfg = it;
  188. if (delta == 0)
  189. return new_rate_khz * 1000;
  190. }
  191. }
  192. }
  193. return new_rate_khz * 1000;
  194. }
  195. static ulong ast2500_configure_ddr(struct ast2500_scu *scu, ulong rate)
  196. {
  197. ulong clkin = ast2500_get_clkin(scu);
  198. u32 mpll_reg;
  199. struct ast2500_div_config div_cfg = {
  200. .num = (SCU_MPLL_NUM_MASK >> SCU_MPLL_NUM_SHIFT),
  201. .denum = (SCU_MPLL_DENUM_MASK >> SCU_MPLL_DENUM_SHIFT),
  202. .post_div = (SCU_MPLL_POST_MASK >> SCU_MPLL_POST_SHIFT),
  203. };
  204. ast2500_calc_clock_config(clkin, rate, &div_cfg);
  205. mpll_reg = readl(&scu->m_pll_param);
  206. mpll_reg &= ~(SCU_MPLL_POST_MASK | SCU_MPLL_NUM_MASK
  207. | SCU_MPLL_DENUM_MASK);
  208. mpll_reg |= (div_cfg.post_div << SCU_MPLL_POST_SHIFT)
  209. | (div_cfg.num << SCU_MPLL_NUM_SHIFT)
  210. | (div_cfg.denum << SCU_MPLL_DENUM_SHIFT);
  211. ast_scu_unlock(scu);
  212. writel(mpll_reg, &scu->m_pll_param);
  213. ast_scu_lock(scu);
  214. return ast2500_get_mpll_rate(clkin, mpll_reg);
  215. }
  216. static ulong ast2500_configure_mac(struct ast2500_scu *scu, int index)
  217. {
  218. ulong clkin = ast2500_get_clkin(scu);
  219. ulong hpll_rate = ast2500_get_hpll_rate(clkin,
  220. readl(&scu->h_pll_param));
  221. ulong required_rate;
  222. u32 hwstrap;
  223. u32 divisor;
  224. u32 reset_bit;
  225. u32 clkstop_bit;
  226. /*
  227. * According to data sheet, for 10/100 mode the MAC clock frequency
  228. * should be at least 25MHz and for 1000 mode at least 100MHz
  229. */
  230. hwstrap = readl(&scu->hwstrap);
  231. if (hwstrap & (SCU_HWSTRAP_MAC1_RGMII | SCU_HWSTRAP_MAC2_RGMII))
  232. required_rate = 100 * 1000 * 1000;
  233. else
  234. required_rate = 25 * 1000 * 1000;
  235. divisor = hpll_rate / required_rate;
  236. if (divisor < 4) {
  237. /* Clock can't run fast enough, but let's try anyway */
  238. debug("MAC clock too slow\n");
  239. divisor = 4;
  240. } else if (divisor > 16) {
  241. /* Can't slow down the clock enough, but let's try anyway */
  242. debug("MAC clock too fast\n");
  243. divisor = 16;
  244. }
  245. switch (index) {
  246. case 1:
  247. reset_bit = SCU_SYSRESET_MAC1;
  248. clkstop_bit = SCU_CLKSTOP_MAC1;
  249. break;
  250. case 2:
  251. reset_bit = SCU_SYSRESET_MAC2;
  252. clkstop_bit = SCU_CLKSTOP_MAC2;
  253. break;
  254. default:
  255. return -EINVAL;
  256. }
  257. ast_scu_unlock(scu);
  258. clrsetbits_le32(&scu->clk_sel1, SCU_MACCLK_MASK,
  259. ((divisor - 2) / 2) << SCU_MACCLK_SHIFT);
  260. /*
  261. * Disable MAC, start its clock and re-enable it.
  262. * The procedure and the delays (100us & 10ms) are
  263. * specified in the datasheet.
  264. */
  265. setbits_le32(&scu->sysreset_ctrl1, reset_bit);
  266. udelay(100);
  267. clrbits_le32(&scu->clk_stop_ctrl1, clkstop_bit);
  268. mdelay(10);
  269. clrbits_le32(&scu->sysreset_ctrl1, reset_bit);
  270. writel((RGMII2_TXCK_DUTY << SCU_CLKDUTY_RGMII2TXCK_SHIFT)
  271. | (RGMII1_TXCK_DUTY << SCU_CLKDUTY_RGMII1TXCK_SHIFT),
  272. &scu->clk_duty_sel);
  273. ast_scu_lock(scu);
  274. return required_rate;
  275. }
  276. static ulong ast2500_configure_d2pll(struct ast2500_scu *scu, ulong rate)
  277. {
  278. /*
  279. * The values and the meaning of the next three
  280. * parameters are undocumented. Taken from Aspeed SDK.
  281. */
  282. const u32 d2_pll_ext_param = 0x2c;
  283. const u32 d2_pll_sip = 0x11;
  284. const u32 d2_pll_sic = 0x18;
  285. u32 clk_delay_settings =
  286. (RMII_RXCLK_IDLY << SCU_MICDS_MAC1RMII_RDLY_SHIFT)
  287. | (RMII_RXCLK_IDLY << SCU_MICDS_MAC2RMII_RDLY_SHIFT)
  288. | (RGMII_TXCLK_ODLY << SCU_MICDS_MAC1RGMII_TXDLY_SHIFT)
  289. | (RGMII_TXCLK_ODLY << SCU_MICDS_MAC2RGMII_TXDLY_SHIFT);
  290. struct ast2500_div_config div_cfg = {
  291. .num = SCU_D2PLL_NUM_MASK >> SCU_D2PLL_NUM_SHIFT,
  292. .denum = SCU_D2PLL_DENUM_MASK >> SCU_D2PLL_DENUM_SHIFT,
  293. .post_div = SCU_D2PLL_POST_MASK >> SCU_D2PLL_POST_SHIFT,
  294. };
  295. ulong clkin = ast2500_get_clkin(scu);
  296. ulong new_rate;
  297. ast_scu_unlock(scu);
  298. writel((d2_pll_ext_param << SCU_D2PLL_EXT1_PARAM_SHIFT)
  299. | SCU_D2PLL_EXT1_OFF
  300. | SCU_D2PLL_EXT1_RESET, &scu->d2_pll_ext_param[0]);
  301. /*
  302. * Select USB2.0 port1 PHY clock as a clock source for GCRT.
  303. * This would disconnect it from D2-PLL.
  304. */
  305. clrsetbits_le32(&scu->misc_ctrl1, SCU_MISC_D2PLL_OFF,
  306. SCU_MISC_GCRT_USB20CLK);
  307. new_rate = ast2500_calc_clock_config(clkin, rate, &div_cfg);
  308. writel((d2_pll_sip << SCU_D2PLL_SIP_SHIFT)
  309. | (d2_pll_sic << SCU_D2PLL_SIC_SHIFT)
  310. | (div_cfg.num << SCU_D2PLL_NUM_SHIFT)
  311. | (div_cfg.denum << SCU_D2PLL_DENUM_SHIFT)
  312. | (div_cfg.post_div << SCU_D2PLL_POST_SHIFT),
  313. &scu->d2_pll_param);
  314. clrbits_le32(&scu->d2_pll_ext_param[0],
  315. SCU_D2PLL_EXT1_OFF | SCU_D2PLL_EXT1_RESET);
  316. clrsetbits_le32(&scu->misc_ctrl2,
  317. SCU_MISC2_RGMII_HPLL | SCU_MISC2_RMII_MPLL
  318. | SCU_MISC2_RGMII_CLKDIV_MASK |
  319. SCU_MISC2_RMII_CLKDIV_MASK,
  320. (4 << SCU_MISC2_RMII_CLKDIV_SHIFT));
  321. writel(clk_delay_settings | SCU_MICDS_RGMIIPLL, &scu->mac_clk_delay);
  322. writel(clk_delay_settings, &scu->mac_clk_delay_100M);
  323. writel(clk_delay_settings, &scu->mac_clk_delay_10M);
  324. ast_scu_lock(scu);
  325. return new_rate;
  326. }
  327. static ulong ast2500_clk_set_rate(struct clk *clk, ulong rate)
  328. {
  329. struct ast2500_clk_priv *priv = dev_get_priv(clk->dev);
  330. ulong new_rate;
  331. switch (clk->id) {
  332. case PLL_MPLL:
  333. case MCLK_DDR:
  334. new_rate = ast2500_configure_ddr(priv->scu, rate);
  335. break;
  336. case PLL_D2PLL:
  337. new_rate = ast2500_configure_d2pll(priv->scu, rate);
  338. break;
  339. default:
  340. return -ENOENT;
  341. }
  342. return new_rate;
  343. }
  344. static int ast2500_clk_enable(struct clk *clk)
  345. {
  346. struct ast2500_clk_priv *priv = dev_get_priv(clk->dev);
  347. switch (clk->id) {
  348. /*
  349. * For MAC clocks the clock rate is
  350. * configured based on whether RGMII or RMII mode has been selected
  351. * through hardware strapping.
  352. */
  353. case PCLK_MAC1:
  354. ast2500_configure_mac(priv->scu, 1);
  355. break;
  356. case PCLK_MAC2:
  357. ast2500_configure_mac(priv->scu, 2);
  358. break;
  359. case PLL_D2PLL:
  360. ast2500_configure_d2pll(priv->scu, D2PLL_DEFAULT_RATE);
  361. default:
  362. return -ENOENT;
  363. }
  364. return 0;
  365. }
  366. struct clk_ops ast2500_clk_ops = {
  367. .get_rate = ast2500_clk_get_rate,
  368. .set_rate = ast2500_clk_set_rate,
  369. .enable = ast2500_clk_enable,
  370. };
  371. static int ast2500_clk_probe(struct udevice *dev)
  372. {
  373. struct ast2500_clk_priv *priv = dev_get_priv(dev);
  374. priv->scu = devfdt_get_addr_ptr(dev);
  375. if (IS_ERR(priv->scu))
  376. return PTR_ERR(priv->scu);
  377. return 0;
  378. }
  379. static int ast2500_clk_bind(struct udevice *dev)
  380. {
  381. int ret;
  382. /* The reset driver does not have a device node, so bind it here */
  383. ret = device_bind_driver(gd->dm_root, "ast_sysreset", "reset", &dev);
  384. if (ret)
  385. debug("Warning: No reset driver: ret=%d\n", ret);
  386. return 0;
  387. }
  388. static const struct udevice_id ast2500_clk_ids[] = {
  389. { .compatible = "aspeed,ast2500-scu" },
  390. { }
  391. };
  392. U_BOOT_DRIVER(aspeed_ast2500_scu) = {
  393. .name = "aspeed_ast2500_scu",
  394. .id = UCLASS_CLK,
  395. .of_match = ast2500_clk_ids,
  396. .priv_auto_alloc_size = sizeof(struct ast2500_clk_priv),
  397. .ops = &ast2500_clk_ops,
  398. .bind = ast2500_clk_bind,
  399. .probe = ast2500_clk_probe,
  400. };