clk_rk3368.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  1. /*
  2. * (C) Copyright 2017 Rockchip Electronics Co., Ltd
  3. * Author: Andy Yan <andy.yan@rock-chips.com>
  4. * (C) Copyright 2017 Theobroma Systems Design und Consulting GmbH
  5. * SPDX-License-Identifier: GPL-2.0
  6. */
  7. #include <common.h>
  8. #include <clk-uclass.h>
  9. #include <dm.h>
  10. #include <dt-structs.h>
  11. #include <errno.h>
  12. #include <mapmem.h>
  13. #include <syscon.h>
  14. #include <asm/arch/clock.h>
  15. #include <asm/arch/cru_rk3368.h>
  16. #include <asm/arch/hardware.h>
  17. #include <asm/io.h>
  18. #include <dm/lists.h>
  19. #include <dt-bindings/clock/rk3368-cru.h>
  20. DECLARE_GLOBAL_DATA_PTR;
  21. #if CONFIG_IS_ENABLED(OF_PLATDATA)
  22. struct rk3368_clk_plat {
  23. struct dtd_rockchip_rk3368_cru dtd;
  24. };
  25. #endif
  26. struct pll_div {
  27. u32 nr;
  28. u32 nf;
  29. u32 no;
  30. };
  31. #define OSC_HZ (24 * 1000 * 1000)
  32. #define APLL_L_HZ (800 * 1000 * 1000)
  33. #define APLL_B_HZ (816 * 1000 * 1000)
  34. #define GPLL_HZ (576 * 1000 * 1000)
  35. #define CPLL_HZ (400 * 1000 * 1000)
  36. #define DIV_TO_RATE(input_rate, div) ((input_rate) / ((div) + 1))
  37. #define PLL_DIVISORS(hz, _nr, _no) { \
  38. .nr = _nr, .nf = (u32)((u64)hz * _nr * _no / OSC_HZ), .no = _no}; \
  39. _Static_assert(((u64)hz * _nr * _no / OSC_HZ) * OSC_HZ /\
  40. (_nr * _no) == hz, #hz "Hz cannot be hit with PLL " \
  41. "divisors on line " __stringify(__LINE__));
  42. #if IS_ENABLED(CONFIG_SPL_BUILD) || IS_ENABLED(CONFIG_TPL_BUILD)
  43. static const struct pll_div apll_l_init_cfg = PLL_DIVISORS(APLL_L_HZ, 12, 2);
  44. static const struct pll_div apll_b_init_cfg = PLL_DIVISORS(APLL_B_HZ, 1, 2);
  45. #if !defined(CONFIG_TPL_BUILD)
  46. static const struct pll_div gpll_init_cfg = PLL_DIVISORS(GPLL_HZ, 1, 2);
  47. static const struct pll_div cpll_init_cfg = PLL_DIVISORS(CPLL_HZ, 1, 6);
  48. #endif
  49. #endif
  50. static ulong rk3368_clk_get_rate(struct clk *clk);
  51. /* Get pll rate by id */
  52. static uint32_t rkclk_pll_get_rate(struct rk3368_cru *cru,
  53. enum rk3368_pll_id pll_id)
  54. {
  55. uint32_t nr, no, nf;
  56. uint32_t con;
  57. struct rk3368_pll *pll = &cru->pll[pll_id];
  58. con = readl(&pll->con3);
  59. switch ((con & PLL_MODE_MASK) >> PLL_MODE_SHIFT) {
  60. case PLL_MODE_SLOW:
  61. return OSC_HZ;
  62. case PLL_MODE_NORMAL:
  63. con = readl(&pll->con0);
  64. no = ((con & PLL_OD_MASK) >> PLL_OD_SHIFT) + 1;
  65. nr = ((con & PLL_NR_MASK) >> PLL_NR_SHIFT) + 1;
  66. con = readl(&pll->con1);
  67. nf = ((con & PLL_NF_MASK) >> PLL_NF_SHIFT) + 1;
  68. return (24 * nf / (nr * no)) * 1000000;
  69. case PLL_MODE_DEEP_SLOW:
  70. default:
  71. return 32768;
  72. }
  73. }
  74. #if IS_ENABLED(CONFIG_SPL_BUILD) || IS_ENABLED(CONFIG_TPL_BUILD)
  75. static int rkclk_set_pll(struct rk3368_cru *cru, enum rk3368_pll_id pll_id,
  76. const struct pll_div *div)
  77. {
  78. struct rk3368_pll *pll = &cru->pll[pll_id];
  79. /* All PLLs have same VCO and output frequency range restrictions*/
  80. uint vco_hz = OSC_HZ / 1000 * div->nf / div->nr * 1000;
  81. uint output_hz = vco_hz / div->no;
  82. debug("PLL at %p: nf=%d, nr=%d, no=%d, vco=%u Hz, output=%u Hz\n",
  83. pll, div->nf, div->nr, div->no, vco_hz, output_hz);
  84. /* enter slow mode and reset pll */
  85. rk_clrsetreg(&pll->con3, PLL_MODE_MASK | PLL_RESET_MASK,
  86. PLL_RESET << PLL_RESET_SHIFT);
  87. rk_clrsetreg(&pll->con0, PLL_NR_MASK | PLL_OD_MASK,
  88. ((div->nr - 1) << PLL_NR_SHIFT) |
  89. ((div->no - 1) << PLL_OD_SHIFT));
  90. writel((div->nf - 1) << PLL_NF_SHIFT, &pll->con1);
  91. /*
  92. * BWADJ should be set to NF / 2 to ensure the nominal bandwidth.
  93. * Compare the RK3368 TRM, section "3.6.4 PLL Bandwidth Adjustment".
  94. */
  95. clrsetbits_le32(&pll->con2, PLL_BWADJ_MASK, (div->nf >> 1) - 1);
  96. udelay(10);
  97. /* return from reset */
  98. rk_clrreg(&pll->con3, PLL_RESET_MASK);
  99. /* waiting for pll lock */
  100. while (!(readl(&pll->con1) & PLL_LOCK_STA))
  101. udelay(1);
  102. rk_clrsetreg(&pll->con3, PLL_MODE_MASK,
  103. PLL_MODE_NORMAL << PLL_MODE_SHIFT);
  104. return 0;
  105. }
  106. #endif
  107. #if IS_ENABLED(CONFIG_SPL_BUILD) || IS_ENABLED(CONFIG_TPL_BUILD)
  108. static void rkclk_init(struct rk3368_cru *cru)
  109. {
  110. u32 apllb, aplll, dpll, cpll, gpll;
  111. rkclk_set_pll(cru, APLLB, &apll_b_init_cfg);
  112. rkclk_set_pll(cru, APLLL, &apll_l_init_cfg);
  113. #if !defined(CONFIG_TPL_BUILD)
  114. /*
  115. * If we plan to return to the boot ROM, we can't increase the
  116. * GPLL rate from the SPL stage.
  117. */
  118. rkclk_set_pll(cru, GPLL, &gpll_init_cfg);
  119. rkclk_set_pll(cru, CPLL, &cpll_init_cfg);
  120. #endif
  121. apllb = rkclk_pll_get_rate(cru, APLLB);
  122. aplll = rkclk_pll_get_rate(cru, APLLL);
  123. dpll = rkclk_pll_get_rate(cru, DPLL);
  124. cpll = rkclk_pll_get_rate(cru, CPLL);
  125. gpll = rkclk_pll_get_rate(cru, GPLL);
  126. debug("%s apllb(%d) apll(%d) dpll(%d) cpll(%d) gpll(%d)\n",
  127. __func__, apllb, aplll, dpll, cpll, gpll);
  128. }
  129. #endif
  130. #if !IS_ENABLED(CONFIG_SPL_BUILD) || CONFIG_IS_ENABLED(MMC_SUPPORT)
  131. static ulong rk3368_mmc_get_clk(struct rk3368_cru *cru, uint clk_id)
  132. {
  133. u32 div, con, con_id, rate;
  134. u32 pll_rate;
  135. switch (clk_id) {
  136. case HCLK_SDMMC:
  137. con_id = 50;
  138. break;
  139. case HCLK_EMMC:
  140. con_id = 51;
  141. break;
  142. case SCLK_SDIO0:
  143. con_id = 48;
  144. break;
  145. default:
  146. return -EINVAL;
  147. }
  148. con = readl(&cru->clksel_con[con_id]);
  149. switch (con & MMC_PLL_SEL_MASK) {
  150. case MMC_PLL_SEL_GPLL:
  151. pll_rate = rkclk_pll_get_rate(cru, GPLL);
  152. break;
  153. case MMC_PLL_SEL_24M:
  154. pll_rate = OSC_HZ;
  155. break;
  156. case MMC_PLL_SEL_CPLL:
  157. pll_rate = rkclk_pll_get_rate(cru, CPLL);
  158. break;
  159. case MMC_PLL_SEL_USBPHY_480M:
  160. default:
  161. return -EINVAL;
  162. }
  163. div = (con & MMC_CLK_DIV_MASK) >> MMC_CLK_DIV_SHIFT;
  164. rate = DIV_TO_RATE(pll_rate, div);
  165. debug("%s: raw rate %d (post-divide by 2)\n", __func__, rate);
  166. return rate >> 1;
  167. }
  168. static ulong rk3368_mmc_find_best_rate_and_parent(struct clk *clk,
  169. ulong rate,
  170. u32 *best_mux,
  171. u32 *best_div)
  172. {
  173. int i;
  174. ulong best_rate = 0;
  175. const ulong MHz = 1000000;
  176. const struct {
  177. u32 mux;
  178. ulong rate;
  179. } parents[] = {
  180. { .mux = MMC_PLL_SEL_CPLL, .rate = CPLL_HZ },
  181. { .mux = MMC_PLL_SEL_GPLL, .rate = GPLL_HZ },
  182. { .mux = MMC_PLL_SEL_24M, .rate = 24 * MHz }
  183. };
  184. debug("%s: target rate %ld\n", __func__, rate);
  185. for (i = 0; i < ARRAY_SIZE(parents); ++i) {
  186. /*
  187. * Find the largest rate no larger than the target-rate for
  188. * the current parent.
  189. */
  190. ulong parent_rate = parents[i].rate;
  191. u32 div = DIV_ROUND_UP(parent_rate, rate);
  192. u32 adj_div = div;
  193. ulong new_rate = parent_rate / adj_div;
  194. debug("%s: rate %ld, parent-mux %d, parent-rate %ld, div %d\n",
  195. __func__, rate, parents[i].mux, parents[i].rate, div);
  196. /* Skip, if not representable */
  197. if ((div - 1) > MMC_CLK_DIV_MASK)
  198. continue;
  199. /* Skip, if we already have a better (or equal) solution */
  200. if (new_rate <= best_rate)
  201. continue;
  202. /* This is our new best rate. */
  203. best_rate = new_rate;
  204. *best_mux = parents[i].mux;
  205. *best_div = div - 1;
  206. }
  207. debug("%s: best_mux = %x, best_div = %d, best_rate = %ld\n",
  208. __func__, *best_mux, *best_div, best_rate);
  209. return best_rate;
  210. }
  211. static ulong rk3368_mmc_set_clk(struct clk *clk, ulong rate)
  212. {
  213. struct rk3368_clk_priv *priv = dev_get_priv(clk->dev);
  214. struct rk3368_cru *cru = priv->cru;
  215. ulong clk_id = clk->id;
  216. u32 con_id, mux = 0, div = 0;
  217. /* Find the best parent and rate */
  218. rk3368_mmc_find_best_rate_and_parent(clk, rate << 1, &mux, &div);
  219. switch (clk_id) {
  220. case HCLK_SDMMC:
  221. con_id = 50;
  222. break;
  223. case HCLK_EMMC:
  224. con_id = 51;
  225. break;
  226. case SCLK_SDIO0:
  227. con_id = 48;
  228. break;
  229. default:
  230. return -EINVAL;
  231. }
  232. rk_clrsetreg(&cru->clksel_con[con_id],
  233. MMC_PLL_SEL_MASK | MMC_CLK_DIV_MASK,
  234. mux | div);
  235. return rk3368_mmc_get_clk(cru, clk_id);
  236. }
  237. #endif
  238. #if IS_ENABLED(CONFIG_TPL_BUILD)
  239. static ulong rk3368_ddr_set_clk(struct rk3368_cru *cru, ulong set_rate)
  240. {
  241. const struct pll_div *dpll_cfg = NULL;
  242. const ulong MHz = 1000000;
  243. /* Fout = ((Fin /NR) * NF )/ NO */
  244. static const struct pll_div dpll_1200 = PLL_DIVISORS(1200 * MHz, 1, 1);
  245. static const struct pll_div dpll_1332 = PLL_DIVISORS(1332 * MHz, 2, 1);
  246. static const struct pll_div dpll_1600 = PLL_DIVISORS(1600 * MHz, 3, 2);
  247. switch (set_rate) {
  248. case 1200*MHz:
  249. dpll_cfg = &dpll_1200;
  250. break;
  251. case 1332*MHz:
  252. dpll_cfg = &dpll_1332;
  253. break;
  254. case 1600*MHz:
  255. dpll_cfg = &dpll_1600;
  256. break;
  257. default:
  258. error("Unsupported SDRAM frequency!,%ld\n", set_rate);
  259. }
  260. rkclk_set_pll(cru, DPLL, dpll_cfg);
  261. return set_rate;
  262. }
  263. #endif
  264. #if CONFIG_IS_ENABLED(GMAC_ROCKCHIP)
  265. static ulong rk3368_gmac_set_clk(struct rk3368_cru *cru,
  266. ulong clk_id, ulong set_rate)
  267. {
  268. /*
  269. * This models the 'assigned-clock-parents = <&ext_gmac>' from
  270. * the DTS and switches to the 'ext_gmac' clock parent.
  271. */
  272. rk_setreg(&cru->clksel_con[43], GMAC_MUX_SEL_EXTCLK);
  273. return set_rate;
  274. }
  275. #endif
  276. /*
  277. * RK3368 SPI clocks have a common divider-width (7 bits) and a single bit
  278. * to select either CPLL or GPLL as the clock-parent. The location within
  279. * the enclosing CLKSEL_CON (i.e. div_shift and sel_shift) are variable.
  280. */
  281. struct spi_clkreg {
  282. uint8_t reg; /* CLKSEL_CON[reg] register in CRU */
  283. uint8_t div_shift;
  284. uint8_t sel_shift;
  285. };
  286. /*
  287. * The entries are numbered relative to their offset from SCLK_SPI0.
  288. */
  289. static const struct spi_clkreg spi_clkregs[] = {
  290. [0] = { .reg = 45, .div_shift = 0, .sel_shift = 7, },
  291. [1] = { .reg = 45, .div_shift = 8, .sel_shift = 15, },
  292. [2] = { .reg = 46, .div_shift = 8, .sel_shift = 15, },
  293. };
  294. static inline u32 extract_bits(u32 val, unsigned width, unsigned shift)
  295. {
  296. return (val >> shift) & ((1 << width) - 1);
  297. }
  298. static ulong rk3368_spi_get_clk(struct rk3368_cru *cru, ulong clk_id)
  299. {
  300. const struct spi_clkreg *spiclk = NULL;
  301. u32 div, val;
  302. switch (clk_id) {
  303. case SCLK_SPI0 ... SCLK_SPI2:
  304. spiclk = &spi_clkregs[clk_id - SCLK_SPI0];
  305. break;
  306. default:
  307. error("%s: SPI clk-id %ld not supported\n", __func__, clk_id);
  308. return -EINVAL;
  309. }
  310. val = readl(&cru->clksel_con[spiclk->reg]);
  311. div = extract_bits(val, 7, spiclk->div_shift);
  312. debug("%s: div 0x%x\n", __func__, div);
  313. return DIV_TO_RATE(GPLL_HZ, div);
  314. }
  315. static ulong rk3368_spi_set_clk(struct rk3368_cru *cru, ulong clk_id, uint hz)
  316. {
  317. const struct spi_clkreg *spiclk = NULL;
  318. int src_clk_div;
  319. src_clk_div = DIV_ROUND_UP(GPLL_HZ, hz);
  320. assert(src_clk_div < 127);
  321. switch (clk_id) {
  322. case SCLK_SPI0 ... SCLK_SPI2:
  323. spiclk = &spi_clkregs[clk_id - SCLK_SPI0];
  324. break;
  325. default:
  326. error("%s: SPI clk-id %ld not supported\n", __func__, clk_id);
  327. return -EINVAL;
  328. }
  329. rk_clrsetreg(&cru->clksel_con[spiclk->reg],
  330. ((0x7f << spiclk->div_shift) |
  331. (0x1 << spiclk->sel_shift)),
  332. ((src_clk_div << spiclk->div_shift) |
  333. (1 << spiclk->sel_shift)));
  334. return rk3368_spi_get_clk(cru, clk_id);
  335. }
  336. static ulong rk3368_clk_get_rate(struct clk *clk)
  337. {
  338. struct rk3368_clk_priv *priv = dev_get_priv(clk->dev);
  339. ulong rate = 0;
  340. debug("%s: id %ld\n", __func__, clk->id);
  341. switch (clk->id) {
  342. case PLL_CPLL:
  343. rate = rkclk_pll_get_rate(priv->cru, CPLL);
  344. break;
  345. case PLL_GPLL:
  346. rate = rkclk_pll_get_rate(priv->cru, GPLL);
  347. break;
  348. case SCLK_SPI0 ... SCLK_SPI2:
  349. rate = rk3368_spi_get_clk(priv->cru, clk->id);
  350. break;
  351. #if !IS_ENABLED(CONFIG_SPL_BUILD) || CONFIG_IS_ENABLED(MMC_SUPPORT)
  352. case HCLK_SDMMC:
  353. case HCLK_EMMC:
  354. rate = rk3368_mmc_get_clk(priv->cru, clk->id);
  355. break;
  356. #endif
  357. default:
  358. return -ENOENT;
  359. }
  360. return rate;
  361. }
  362. static ulong rk3368_clk_set_rate(struct clk *clk, ulong rate)
  363. {
  364. __maybe_unused struct rk3368_clk_priv *priv = dev_get_priv(clk->dev);
  365. ulong ret = 0;
  366. debug("%s id:%ld rate:%ld\n", __func__, clk->id, rate);
  367. switch (clk->id) {
  368. case SCLK_SPI0 ... SCLK_SPI2:
  369. ret = rk3368_spi_set_clk(priv->cru, clk->id, rate);
  370. break;
  371. #if IS_ENABLED(CONFIG_TPL_BUILD)
  372. case CLK_DDR:
  373. ret = rk3368_ddr_set_clk(priv->cru, rate);
  374. break;
  375. #endif
  376. #if !IS_ENABLED(CONFIG_SPL_BUILD) || CONFIG_IS_ENABLED(MMC_SUPPORT)
  377. case HCLK_SDMMC:
  378. case HCLK_EMMC:
  379. ret = rk3368_mmc_set_clk(clk, rate);
  380. break;
  381. #endif
  382. #if CONFIG_IS_ENABLED(GMAC_ROCKCHIP)
  383. case SCLK_MAC:
  384. /* select the external clock */
  385. ret = rk3368_gmac_set_clk(priv->cru, clk->id, rate);
  386. break;
  387. #endif
  388. default:
  389. return -ENOENT;
  390. }
  391. return ret;
  392. }
  393. static struct clk_ops rk3368_clk_ops = {
  394. .get_rate = rk3368_clk_get_rate,
  395. .set_rate = rk3368_clk_set_rate,
  396. };
  397. static int rk3368_clk_probe(struct udevice *dev)
  398. {
  399. struct rk3368_clk_priv __maybe_unused *priv = dev_get_priv(dev);
  400. #if CONFIG_IS_ENABLED(OF_PLATDATA)
  401. struct rk3368_clk_plat *plat = dev_get_platdata(dev);
  402. priv->cru = map_sysmem(plat->dtd.reg[0], plat->dtd.reg[1]);
  403. #endif
  404. #if IS_ENABLED(CONFIG_SPL_BUILD) || IS_ENABLED(CONFIG_TPL_BUILD)
  405. rkclk_init(priv->cru);
  406. #endif
  407. return 0;
  408. }
  409. static int rk3368_clk_ofdata_to_platdata(struct udevice *dev)
  410. {
  411. #if !CONFIG_IS_ENABLED(OF_PLATDATA)
  412. struct rk3368_clk_priv *priv = dev_get_priv(dev);
  413. priv->cru = (struct rk3368_cru *)devfdt_get_addr(dev);
  414. #endif
  415. return 0;
  416. }
  417. static int rk3368_clk_bind(struct udevice *dev)
  418. {
  419. int ret;
  420. /* The reset driver does not have a device node, so bind it here */
  421. ret = device_bind_driver(gd->dm_root, "rk3368_sysreset", "reset", &dev);
  422. if (ret)
  423. error("bind RK3368 reset driver failed: ret=%d\n", ret);
  424. return ret;
  425. }
  426. static const struct udevice_id rk3368_clk_ids[] = {
  427. { .compatible = "rockchip,rk3368-cru" },
  428. { }
  429. };
  430. U_BOOT_DRIVER(rockchip_rk3368_cru) = {
  431. .name = "rockchip_rk3368_cru",
  432. .id = UCLASS_CLK,
  433. .of_match = rk3368_clk_ids,
  434. .priv_auto_alloc_size = sizeof(struct rk3368_clk_priv),
  435. #if CONFIG_IS_ENABLED(OF_PLATDATA)
  436. .platdata_auto_alloc_size = sizeof(struct rk3368_clk_plat),
  437. #endif
  438. .ofdata_to_platdata = rk3368_clk_ofdata_to_platdata,
  439. .ops = &rk3368_clk_ops,
  440. .bind = rk3368_clk_bind,
  441. .probe = rk3368_clk_probe,
  442. };