clk_ast2500.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  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. struct ast2500_clock_config {
  150. ulong input_rate;
  151. ulong rate;
  152. struct ast2500_div_config cfg;
  153. };
  154. static const struct ast2500_clock_config ast2500_clock_config_defaults[] = {
  155. { 24000000, 250000000, { .num = 124, .denum = 1, .post_div = 5 } },
  156. };
  157. static bool ast2500_get_clock_config_default(ulong input_rate,
  158. ulong requested_rate,
  159. struct ast2500_div_config *cfg)
  160. {
  161. int i;
  162. for (i = 0; i < ARRAY_SIZE(ast2500_clock_config_defaults); i++) {
  163. const struct ast2500_clock_config *default_cfg =
  164. &ast2500_clock_config_defaults[i];
  165. if (default_cfg->input_rate == input_rate &&
  166. default_cfg->rate == requested_rate) {
  167. *cfg = default_cfg->cfg;
  168. return true;
  169. }
  170. }
  171. return false;
  172. }
  173. /*
  174. * @input_rate - the rate of input clock in Hz
  175. * @requested_rate - desired output rate in Hz
  176. * @div - this is an IN/OUT parameter, at input all fields of the config
  177. * need to be set to their maximum allowed values.
  178. * The result (the best config we could find), would also be returned
  179. * in this structure.
  180. *
  181. * @return The clock rate, when the resulting div_config is used.
  182. */
  183. static ulong ast2500_calc_clock_config(ulong input_rate, ulong requested_rate,
  184. struct ast2500_div_config *cfg)
  185. {
  186. /*
  187. * The assumption is that kHz precision is good enough and
  188. * also enough to avoid overflow when multiplying.
  189. */
  190. const ulong input_rate_khz = input_rate / 1000;
  191. const ulong rate_khz = requested_rate / 1000;
  192. const struct ast2500_div_config max_vals = *cfg;
  193. struct ast2500_div_config it = { 0, 0, 0 };
  194. ulong delta = rate_khz;
  195. ulong new_rate_khz = 0;
  196. /*
  197. * Look for a well known frequency first.
  198. */
  199. if (ast2500_get_clock_config_default(input_rate, requested_rate, cfg))
  200. return requested_rate;
  201. for (; it.denum <= max_vals.denum; ++it.denum) {
  202. for (it.post_div = 0; it.post_div <= max_vals.post_div;
  203. ++it.post_div) {
  204. it.num = (rate_khz * (it.post_div + 1) / input_rate_khz)
  205. * (it.denum + 1);
  206. if (it.num > max_vals.num)
  207. continue;
  208. new_rate_khz = (input_rate_khz
  209. * ((it.num + 1) / (it.denum + 1)))
  210. / (it.post_div + 1);
  211. /* Keep the rate below requested one. */
  212. if (new_rate_khz > rate_khz)
  213. continue;
  214. if (new_rate_khz - rate_khz < delta) {
  215. delta = new_rate_khz - rate_khz;
  216. *cfg = it;
  217. if (delta == 0)
  218. return new_rate_khz * 1000;
  219. }
  220. }
  221. }
  222. return new_rate_khz * 1000;
  223. }
  224. static ulong ast2500_configure_ddr(struct ast2500_scu *scu, ulong rate)
  225. {
  226. ulong clkin = ast2500_get_clkin(scu);
  227. u32 mpll_reg;
  228. struct ast2500_div_config div_cfg = {
  229. .num = (SCU_MPLL_NUM_MASK >> SCU_MPLL_NUM_SHIFT),
  230. .denum = (SCU_MPLL_DENUM_MASK >> SCU_MPLL_DENUM_SHIFT),
  231. .post_div = (SCU_MPLL_POST_MASK >> SCU_MPLL_POST_SHIFT),
  232. };
  233. ast2500_calc_clock_config(clkin, rate, &div_cfg);
  234. mpll_reg = readl(&scu->m_pll_param);
  235. mpll_reg &= ~(SCU_MPLL_POST_MASK | SCU_MPLL_NUM_MASK
  236. | SCU_MPLL_DENUM_MASK);
  237. mpll_reg |= (div_cfg.post_div << SCU_MPLL_POST_SHIFT)
  238. | (div_cfg.num << SCU_MPLL_NUM_SHIFT)
  239. | (div_cfg.denum << SCU_MPLL_DENUM_SHIFT);
  240. ast_scu_unlock(scu);
  241. writel(mpll_reg, &scu->m_pll_param);
  242. ast_scu_lock(scu);
  243. return ast2500_get_mpll_rate(clkin, mpll_reg);
  244. }
  245. static ulong ast2500_configure_mac(struct ast2500_scu *scu, int index)
  246. {
  247. ulong clkin = ast2500_get_clkin(scu);
  248. ulong hpll_rate = ast2500_get_hpll_rate(clkin,
  249. readl(&scu->h_pll_param));
  250. ulong required_rate;
  251. u32 hwstrap;
  252. u32 divisor;
  253. u32 reset_bit;
  254. u32 clkstop_bit;
  255. /*
  256. * According to data sheet, for 10/100 mode the MAC clock frequency
  257. * should be at least 25MHz and for 1000 mode at least 100MHz
  258. */
  259. hwstrap = readl(&scu->hwstrap);
  260. if (hwstrap & (SCU_HWSTRAP_MAC1_RGMII | SCU_HWSTRAP_MAC2_RGMII))
  261. required_rate = 100 * 1000 * 1000;
  262. else
  263. required_rate = 25 * 1000 * 1000;
  264. divisor = hpll_rate / required_rate;
  265. if (divisor < 4) {
  266. /* Clock can't run fast enough, but let's try anyway */
  267. debug("MAC clock too slow\n");
  268. divisor = 4;
  269. } else if (divisor > 16) {
  270. /* Can't slow down the clock enough, but let's try anyway */
  271. debug("MAC clock too fast\n");
  272. divisor = 16;
  273. }
  274. switch (index) {
  275. case 1:
  276. reset_bit = SCU_SYSRESET_MAC1;
  277. clkstop_bit = SCU_CLKSTOP_MAC1;
  278. break;
  279. case 2:
  280. reset_bit = SCU_SYSRESET_MAC2;
  281. clkstop_bit = SCU_CLKSTOP_MAC2;
  282. break;
  283. default:
  284. return -EINVAL;
  285. }
  286. ast_scu_unlock(scu);
  287. clrsetbits_le32(&scu->clk_sel1, SCU_MACCLK_MASK,
  288. ((divisor - 2) / 2) << SCU_MACCLK_SHIFT);
  289. /*
  290. * Disable MAC, start its clock and re-enable it.
  291. * The procedure and the delays (100us & 10ms) are
  292. * specified in the datasheet.
  293. */
  294. setbits_le32(&scu->sysreset_ctrl1, reset_bit);
  295. udelay(100);
  296. clrbits_le32(&scu->clk_stop_ctrl1, clkstop_bit);
  297. mdelay(10);
  298. clrbits_le32(&scu->sysreset_ctrl1, reset_bit);
  299. writel((RGMII2_TXCK_DUTY << SCU_CLKDUTY_RGMII2TXCK_SHIFT)
  300. | (RGMII1_TXCK_DUTY << SCU_CLKDUTY_RGMII1TXCK_SHIFT),
  301. &scu->clk_duty_sel);
  302. ast_scu_lock(scu);
  303. return required_rate;
  304. }
  305. static ulong ast2500_configure_d2pll(struct ast2500_scu *scu, ulong rate)
  306. {
  307. /*
  308. * The values and the meaning of the next three
  309. * parameters are undocumented. Taken from Aspeed SDK.
  310. *
  311. * TODO(clg@kaod.org): the SIP and SIC values depend on the
  312. * Numerator value
  313. */
  314. const u32 d2_pll_ext_param = 0x2c;
  315. const u32 d2_pll_sip = 0x11;
  316. const u32 d2_pll_sic = 0x18;
  317. u32 clk_delay_settings =
  318. (RMII_RXCLK_IDLY << SCU_MICDS_MAC1RMII_RDLY_SHIFT)
  319. | (RMII_RXCLK_IDLY << SCU_MICDS_MAC2RMII_RDLY_SHIFT)
  320. | (RGMII_TXCLK_ODLY << SCU_MICDS_MAC1RGMII_TXDLY_SHIFT)
  321. | (RGMII_TXCLK_ODLY << SCU_MICDS_MAC2RGMII_TXDLY_SHIFT);
  322. struct ast2500_div_config div_cfg = {
  323. .num = SCU_D2PLL_NUM_MASK >> SCU_D2PLL_NUM_SHIFT,
  324. .denum = SCU_D2PLL_DENUM_MASK >> SCU_D2PLL_DENUM_SHIFT,
  325. .post_div = SCU_D2PLL_POST_MASK >> SCU_D2PLL_POST_SHIFT,
  326. };
  327. ulong clkin = ast2500_get_clkin(scu);
  328. ulong new_rate;
  329. ast_scu_unlock(scu);
  330. writel((d2_pll_ext_param << SCU_D2PLL_EXT1_PARAM_SHIFT)
  331. | SCU_D2PLL_EXT1_OFF
  332. | SCU_D2PLL_EXT1_RESET, &scu->d2_pll_ext_param[0]);
  333. /*
  334. * Select USB2.0 port1 PHY clock as a clock source for GCRT.
  335. * This would disconnect it from D2-PLL.
  336. */
  337. clrsetbits_le32(&scu->misc_ctrl1, SCU_MISC_D2PLL_OFF,
  338. SCU_MISC_GCRT_USB20CLK);
  339. new_rate = ast2500_calc_clock_config(clkin, rate, &div_cfg);
  340. writel((d2_pll_sip << SCU_D2PLL_SIP_SHIFT)
  341. | (d2_pll_sic << SCU_D2PLL_SIC_SHIFT)
  342. | (div_cfg.num << SCU_D2PLL_NUM_SHIFT)
  343. | (div_cfg.denum << SCU_D2PLL_DENUM_SHIFT)
  344. | (div_cfg.post_div << SCU_D2PLL_POST_SHIFT),
  345. &scu->d2_pll_param);
  346. clrbits_le32(&scu->d2_pll_ext_param[0],
  347. SCU_D2PLL_EXT1_OFF | SCU_D2PLL_EXT1_RESET);
  348. clrsetbits_le32(&scu->misc_ctrl2,
  349. SCU_MISC2_RGMII_HPLL | SCU_MISC2_RMII_MPLL
  350. | SCU_MISC2_RGMII_CLKDIV_MASK |
  351. SCU_MISC2_RMII_CLKDIV_MASK,
  352. (4 << SCU_MISC2_RMII_CLKDIV_SHIFT));
  353. writel(clk_delay_settings | SCU_MICDS_RGMIIPLL, &scu->mac_clk_delay);
  354. writel(clk_delay_settings, &scu->mac_clk_delay_100M);
  355. writel(clk_delay_settings, &scu->mac_clk_delay_10M);
  356. ast_scu_lock(scu);
  357. return new_rate;
  358. }
  359. static ulong ast2500_clk_set_rate(struct clk *clk, ulong rate)
  360. {
  361. struct ast2500_clk_priv *priv = dev_get_priv(clk->dev);
  362. ulong new_rate;
  363. switch (clk->id) {
  364. case PLL_MPLL:
  365. case MCLK_DDR:
  366. new_rate = ast2500_configure_ddr(priv->scu, rate);
  367. break;
  368. case PLL_D2PLL:
  369. new_rate = ast2500_configure_d2pll(priv->scu, rate);
  370. break;
  371. default:
  372. return -ENOENT;
  373. }
  374. return new_rate;
  375. }
  376. static int ast2500_clk_enable(struct clk *clk)
  377. {
  378. struct ast2500_clk_priv *priv = dev_get_priv(clk->dev);
  379. switch (clk->id) {
  380. /*
  381. * For MAC clocks the clock rate is
  382. * configured based on whether RGMII or RMII mode has been selected
  383. * through hardware strapping.
  384. */
  385. case PCLK_MAC1:
  386. ast2500_configure_mac(priv->scu, 1);
  387. break;
  388. case PCLK_MAC2:
  389. ast2500_configure_mac(priv->scu, 2);
  390. break;
  391. case PLL_D2PLL:
  392. ast2500_configure_d2pll(priv->scu, D2PLL_DEFAULT_RATE);
  393. break;
  394. default:
  395. return -ENOENT;
  396. }
  397. return 0;
  398. }
  399. struct clk_ops ast2500_clk_ops = {
  400. .get_rate = ast2500_clk_get_rate,
  401. .set_rate = ast2500_clk_set_rate,
  402. .enable = ast2500_clk_enable,
  403. };
  404. static int ast2500_clk_probe(struct udevice *dev)
  405. {
  406. struct ast2500_clk_priv *priv = dev_get_priv(dev);
  407. priv->scu = devfdt_get_addr_ptr(dev);
  408. if (IS_ERR(priv->scu))
  409. return PTR_ERR(priv->scu);
  410. return 0;
  411. }
  412. static int ast2500_clk_bind(struct udevice *dev)
  413. {
  414. int ret;
  415. /* The reset driver does not have a device node, so bind it here */
  416. ret = device_bind_driver(gd->dm_root, "ast_sysreset", "reset", &dev);
  417. if (ret)
  418. debug("Warning: No reset driver: ret=%d\n", ret);
  419. return 0;
  420. }
  421. static const struct udevice_id ast2500_clk_ids[] = {
  422. { .compatible = "aspeed,ast2500-scu" },
  423. { }
  424. };
  425. U_BOOT_DRIVER(aspeed_ast2500_scu) = {
  426. .name = "aspeed_ast2500_scu",
  427. .id = UCLASS_CLK,
  428. .of_match = ast2500_clk_ids,
  429. .priv_auto_alloc_size = sizeof(struct ast2500_clk_priv),
  430. .ops = &ast2500_clk_ops,
  431. .bind = ast2500_clk_bind,
  432. .probe = ast2500_clk_probe,
  433. };