Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Andy Yan | b9909aa | 2017-05-15 17:49:56 +0800 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2017 Rockchip Electronics Co., Ltd |
| 4 | * Author: Andy Yan <andy.yan@rock-chips.com> |
Philipp Tomsich | 34b7613 | 2017-06-22 23:47:11 +0200 | [diff] [blame] | 5 | * (C) Copyright 2017 Theobroma Systems Design und Consulting GmbH |
Andy Yan | b9909aa | 2017-05-15 17:49:56 +0800 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include <common.h> |
| 9 | #include <clk-uclass.h> |
| 10 | #include <dm.h> |
Philipp Tomsich | 79aa1ab | 2017-06-22 23:51:37 +0200 | [diff] [blame] | 11 | #include <dt-structs.h> |
Andy Yan | b9909aa | 2017-05-15 17:49:56 +0800 | [diff] [blame] | 12 | #include <errno.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 13 | #include <log.h> |
Simon Glass | 9bc1564 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 14 | #include <malloc.h> |
Philipp Tomsich | 79aa1ab | 2017-06-22 23:51:37 +0200 | [diff] [blame] | 15 | #include <mapmem.h> |
Andy Yan | b9909aa | 2017-05-15 17:49:56 +0800 | [diff] [blame] | 16 | #include <syscon.h> |
David Wu | 4771ba6 | 2017-09-20 14:37:50 +0800 | [diff] [blame] | 17 | #include <bitfield.h> |
Kever Yang | 9fbe17c | 2019-03-28 11:01:23 +0800 | [diff] [blame] | 18 | #include <asm/arch-rockchip/clock.h> |
| 19 | #include <asm/arch-rockchip/cru_rk3368.h> |
| 20 | #include <asm/arch-rockchip/hardware.h> |
Andy Yan | b9909aa | 2017-05-15 17:49:56 +0800 | [diff] [blame] | 21 | #include <asm/io.h> |
Simon Glass | 9558862 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 22 | #include <dm/device-internal.h> |
Andy Yan | b9909aa | 2017-05-15 17:49:56 +0800 | [diff] [blame] | 23 | #include <dm/lists.h> |
| 24 | #include <dt-bindings/clock/rk3368-cru.h> |
Simon Glass | dbd7954 | 2020-05-10 11:40:11 -0600 | [diff] [blame] | 25 | #include <linux/delay.h> |
Simon Glass | bdd5f81 | 2023-09-14 18:21:46 -0600 | [diff] [blame] | 26 | #include <linux/printk.h> |
Simon Glass | fb64e36 | 2020-05-10 11:40:09 -0600 | [diff] [blame] | 27 | #include <linux/stringify.h> |
Andy Yan | b9909aa | 2017-05-15 17:49:56 +0800 | [diff] [blame] | 28 | |
Philipp Tomsich | 79aa1ab | 2017-06-22 23:51:37 +0200 | [diff] [blame] | 29 | #if CONFIG_IS_ENABLED(OF_PLATDATA) |
| 30 | struct rk3368_clk_plat { |
| 31 | struct dtd_rockchip_rk3368_cru dtd; |
| 32 | }; |
| 33 | #endif |
| 34 | |
Andy Yan | b9909aa | 2017-05-15 17:49:56 +0800 | [diff] [blame] | 35 | struct pll_div { |
| 36 | u32 nr; |
| 37 | u32 nf; |
| 38 | u32 no; |
| 39 | }; |
| 40 | |
| 41 | #define OSC_HZ (24 * 1000 * 1000) |
| 42 | #define APLL_L_HZ (800 * 1000 * 1000) |
| 43 | #define APLL_B_HZ (816 * 1000 * 1000) |
| 44 | #define GPLL_HZ (576 * 1000 * 1000) |
| 45 | #define CPLL_HZ (400 * 1000 * 1000) |
| 46 | |
Andy Yan | b9909aa | 2017-05-15 17:49:56 +0800 | [diff] [blame] | 47 | #define DIV_TO_RATE(input_rate, div) ((input_rate) / ((div) + 1)) |
| 48 | |
| 49 | #define PLL_DIVISORS(hz, _nr, _no) { \ |
| 50 | .nr = _nr, .nf = (u32)((u64)hz * _nr * _no / OSC_HZ), .no = _no}; \ |
| 51 | _Static_assert(((u64)hz * _nr * _no / OSC_HZ) * OSC_HZ /\ |
| 52 | (_nr * _no) == hz, #hz "Hz cannot be hit with PLL " \ |
| 53 | "divisors on line " __stringify(__LINE__)); |
| 54 | |
Philipp Tomsich | 415ff7e | 2017-06-22 23:53:44 +0200 | [diff] [blame] | 55 | #if IS_ENABLED(CONFIG_SPL_BUILD) || IS_ENABLED(CONFIG_TPL_BUILD) |
Andy Yan | b9909aa | 2017-05-15 17:49:56 +0800 | [diff] [blame] | 56 | static const struct pll_div apll_l_init_cfg = PLL_DIVISORS(APLL_L_HZ, 12, 2); |
| 57 | static const struct pll_div apll_b_init_cfg = PLL_DIVISORS(APLL_B_HZ, 1, 2); |
Philipp Tomsich | 415ff7e | 2017-06-22 23:53:44 +0200 | [diff] [blame] | 58 | #if !defined(CONFIG_TPL_BUILD) |
Andy Yan | b9909aa | 2017-05-15 17:49:56 +0800 | [diff] [blame] | 59 | static const struct pll_div gpll_init_cfg = PLL_DIVISORS(GPLL_HZ, 1, 2); |
| 60 | static const struct pll_div cpll_init_cfg = PLL_DIVISORS(CPLL_HZ, 1, 6); |
Philipp Tomsich | 415ff7e | 2017-06-22 23:53:44 +0200 | [diff] [blame] | 61 | #endif |
| 62 | #endif |
Andy Yan | b9909aa | 2017-05-15 17:49:56 +0800 | [diff] [blame] | 63 | |
Philipp Tomsich | fbf07a5 | 2017-07-04 14:49:38 +0200 | [diff] [blame] | 64 | static ulong rk3368_clk_get_rate(struct clk *clk); |
| 65 | |
Andy Yan | b9909aa | 2017-05-15 17:49:56 +0800 | [diff] [blame] | 66 | /* Get pll rate by id */ |
| 67 | static uint32_t rkclk_pll_get_rate(struct rk3368_cru *cru, |
| 68 | enum rk3368_pll_id pll_id) |
| 69 | { |
| 70 | uint32_t nr, no, nf; |
| 71 | uint32_t con; |
| 72 | struct rk3368_pll *pll = &cru->pll[pll_id]; |
| 73 | |
| 74 | con = readl(&pll->con3); |
| 75 | |
| 76 | switch ((con & PLL_MODE_MASK) >> PLL_MODE_SHIFT) { |
| 77 | case PLL_MODE_SLOW: |
| 78 | return OSC_HZ; |
| 79 | case PLL_MODE_NORMAL: |
| 80 | con = readl(&pll->con0); |
| 81 | no = ((con & PLL_OD_MASK) >> PLL_OD_SHIFT) + 1; |
| 82 | nr = ((con & PLL_NR_MASK) >> PLL_NR_SHIFT) + 1; |
| 83 | con = readl(&pll->con1); |
| 84 | nf = ((con & PLL_NF_MASK) >> PLL_NF_SHIFT) + 1; |
| 85 | |
| 86 | return (24 * nf / (nr * no)) * 1000000; |
| 87 | case PLL_MODE_DEEP_SLOW: |
| 88 | default: |
| 89 | return 32768; |
| 90 | } |
| 91 | } |
| 92 | |
Philipp Tomsich | 415ff7e | 2017-06-22 23:53:44 +0200 | [diff] [blame] | 93 | #if IS_ENABLED(CONFIG_SPL_BUILD) || IS_ENABLED(CONFIG_TPL_BUILD) |
Andy Yan | b9909aa | 2017-05-15 17:49:56 +0800 | [diff] [blame] | 94 | static int rkclk_set_pll(struct rk3368_cru *cru, enum rk3368_pll_id pll_id, |
Philipp Tomsich | 34b7613 | 2017-06-22 23:47:11 +0200 | [diff] [blame] | 95 | const struct pll_div *div) |
Andy Yan | b9909aa | 2017-05-15 17:49:56 +0800 | [diff] [blame] | 96 | { |
| 97 | struct rk3368_pll *pll = &cru->pll[pll_id]; |
| 98 | /* All PLLs have same VCO and output frequency range restrictions*/ |
| 99 | uint vco_hz = OSC_HZ / 1000 * div->nf / div->nr * 1000; |
| 100 | uint output_hz = vco_hz / div->no; |
| 101 | |
| 102 | debug("PLL at %p: nf=%d, nr=%d, no=%d, vco=%u Hz, output=%u Hz\n", |
| 103 | pll, div->nf, div->nr, div->no, vco_hz, output_hz); |
| 104 | |
| 105 | /* enter slow mode and reset pll */ |
| 106 | rk_clrsetreg(&pll->con3, PLL_MODE_MASK | PLL_RESET_MASK, |
| 107 | PLL_RESET << PLL_RESET_SHIFT); |
| 108 | |
| 109 | rk_clrsetreg(&pll->con0, PLL_NR_MASK | PLL_OD_MASK, |
| 110 | ((div->nr - 1) << PLL_NR_SHIFT) | |
| 111 | ((div->no - 1) << PLL_OD_SHIFT)); |
| 112 | writel((div->nf - 1) << PLL_NF_SHIFT, &pll->con1); |
Philipp Tomsich | 34b7613 | 2017-06-22 23:47:11 +0200 | [diff] [blame] | 113 | /* |
| 114 | * BWADJ should be set to NF / 2 to ensure the nominal bandwidth. |
| 115 | * Compare the RK3368 TRM, section "3.6.4 PLL Bandwidth Adjustment". |
| 116 | */ |
| 117 | clrsetbits_le32(&pll->con2, PLL_BWADJ_MASK, (div->nf >> 1) - 1); |
| 118 | |
Andy Yan | b9909aa | 2017-05-15 17:49:56 +0800 | [diff] [blame] | 119 | udelay(10); |
| 120 | |
| 121 | /* return from reset */ |
| 122 | rk_clrreg(&pll->con3, PLL_RESET_MASK); |
| 123 | |
| 124 | /* waiting for pll lock */ |
| 125 | while (!(readl(&pll->con1) & PLL_LOCK_STA)) |
| 126 | udelay(1); |
| 127 | |
| 128 | rk_clrsetreg(&pll->con3, PLL_MODE_MASK, |
| 129 | PLL_MODE_NORMAL << PLL_MODE_SHIFT); |
| 130 | |
| 131 | return 0; |
| 132 | } |
Philipp Tomsich | 415ff7e | 2017-06-22 23:53:44 +0200 | [diff] [blame] | 133 | #endif |
Andy Yan | b9909aa | 2017-05-15 17:49:56 +0800 | [diff] [blame] | 134 | |
Philipp Tomsich | 415ff7e | 2017-06-22 23:53:44 +0200 | [diff] [blame] | 135 | #if IS_ENABLED(CONFIG_SPL_BUILD) || IS_ENABLED(CONFIG_TPL_BUILD) |
Andy Yan | b9909aa | 2017-05-15 17:49:56 +0800 | [diff] [blame] | 136 | static void rkclk_init(struct rk3368_cru *cru) |
| 137 | { |
| 138 | u32 apllb, aplll, dpll, cpll, gpll; |
| 139 | |
Philipp Tomsich | 34b7613 | 2017-06-22 23:47:11 +0200 | [diff] [blame] | 140 | rkclk_set_pll(cru, APLLB, &apll_b_init_cfg); |
| 141 | rkclk_set_pll(cru, APLLL, &apll_l_init_cfg); |
Philipp Tomsich | 415ff7e | 2017-06-22 23:53:44 +0200 | [diff] [blame] | 142 | #if !defined(CONFIG_TPL_BUILD) |
| 143 | /* |
| 144 | * If we plan to return to the boot ROM, we can't increase the |
| 145 | * GPLL rate from the SPL stage. |
| 146 | */ |
Philipp Tomsich | 34b7613 | 2017-06-22 23:47:11 +0200 | [diff] [blame] | 147 | rkclk_set_pll(cru, GPLL, &gpll_init_cfg); |
| 148 | rkclk_set_pll(cru, CPLL, &cpll_init_cfg); |
Philipp Tomsich | 415ff7e | 2017-06-22 23:53:44 +0200 | [diff] [blame] | 149 | #endif |
Andy Yan | b9909aa | 2017-05-15 17:49:56 +0800 | [diff] [blame] | 150 | |
| 151 | apllb = rkclk_pll_get_rate(cru, APLLB); |
| 152 | aplll = rkclk_pll_get_rate(cru, APLLL); |
| 153 | dpll = rkclk_pll_get_rate(cru, DPLL); |
| 154 | cpll = rkclk_pll_get_rate(cru, CPLL); |
| 155 | gpll = rkclk_pll_get_rate(cru, GPLL); |
| 156 | |
| 157 | debug("%s apllb(%d) apll(%d) dpll(%d) cpll(%d) gpll(%d)\n", |
| 158 | __func__, apllb, aplll, dpll, cpll, gpll); |
| 159 | } |
Philipp Tomsich | 415ff7e | 2017-06-22 23:53:44 +0200 | [diff] [blame] | 160 | #endif |
Andy Yan | b9909aa | 2017-05-15 17:49:56 +0800 | [diff] [blame] | 161 | |
Simon Glass | b58bfe0 | 2021-08-08 12:20:09 -0600 | [diff] [blame] | 162 | #if !IS_ENABLED(CONFIG_SPL_BUILD) || CONFIG_IS_ENABLED(MMC) |
Andy Yan | b9909aa | 2017-05-15 17:49:56 +0800 | [diff] [blame] | 163 | static ulong rk3368_mmc_get_clk(struct rk3368_cru *cru, uint clk_id) |
| 164 | { |
| 165 | u32 div, con, con_id, rate; |
| 166 | u32 pll_rate; |
| 167 | |
| 168 | switch (clk_id) { |
Philipp Tomsich | fbf07a5 | 2017-07-04 14:49:38 +0200 | [diff] [blame] | 169 | case HCLK_SDMMC: |
Andy Yan | b9909aa | 2017-05-15 17:49:56 +0800 | [diff] [blame] | 170 | con_id = 50; |
| 171 | break; |
Philipp Tomsich | fbf07a5 | 2017-07-04 14:49:38 +0200 | [diff] [blame] | 172 | case HCLK_EMMC: |
Andy Yan | b9909aa | 2017-05-15 17:49:56 +0800 | [diff] [blame] | 173 | con_id = 51; |
| 174 | break; |
| 175 | case SCLK_SDIO0: |
| 176 | con_id = 48; |
| 177 | break; |
| 178 | default: |
| 179 | return -EINVAL; |
| 180 | } |
| 181 | |
| 182 | con = readl(&cru->clksel_con[con_id]); |
Philipp Tomsich | fbf07a5 | 2017-07-04 14:49:38 +0200 | [diff] [blame] | 183 | switch (con & MMC_PLL_SEL_MASK) { |
Andy Yan | b9909aa | 2017-05-15 17:49:56 +0800 | [diff] [blame] | 184 | case MMC_PLL_SEL_GPLL: |
| 185 | pll_rate = rkclk_pll_get_rate(cru, GPLL); |
| 186 | break; |
| 187 | case MMC_PLL_SEL_24M: |
| 188 | pll_rate = OSC_HZ; |
| 189 | break; |
| 190 | case MMC_PLL_SEL_CPLL: |
Philipp Tomsich | fbf07a5 | 2017-07-04 14:49:38 +0200 | [diff] [blame] | 191 | pll_rate = rkclk_pll_get_rate(cru, CPLL); |
| 192 | break; |
Andy Yan | b9909aa | 2017-05-15 17:49:56 +0800 | [diff] [blame] | 193 | case MMC_PLL_SEL_USBPHY_480M: |
| 194 | default: |
| 195 | return -EINVAL; |
| 196 | } |
| 197 | div = (con & MMC_CLK_DIV_MASK) >> MMC_CLK_DIV_SHIFT; |
| 198 | rate = DIV_TO_RATE(pll_rate, div); |
| 199 | |
Philipp Tomsich | fbf07a5 | 2017-07-04 14:49:38 +0200 | [diff] [blame] | 200 | debug("%s: raw rate %d (post-divide by 2)\n", __func__, rate); |
Andy Yan | b9909aa | 2017-05-15 17:49:56 +0800 | [diff] [blame] | 201 | return rate >> 1; |
| 202 | } |
| 203 | |
Philipp Tomsich | fbf07a5 | 2017-07-04 14:49:38 +0200 | [diff] [blame] | 204 | static ulong rk3368_mmc_find_best_rate_and_parent(struct clk *clk, |
| 205 | ulong rate, |
| 206 | u32 *best_mux, |
| 207 | u32 *best_div) |
Andy Yan | b9909aa | 2017-05-15 17:49:56 +0800 | [diff] [blame] | 208 | { |
Philipp Tomsich | fbf07a5 | 2017-07-04 14:49:38 +0200 | [diff] [blame] | 209 | int i; |
| 210 | ulong best_rate = 0; |
| 211 | const ulong MHz = 1000000; |
| 212 | const struct { |
| 213 | u32 mux; |
| 214 | ulong rate; |
| 215 | } parents[] = { |
| 216 | { .mux = MMC_PLL_SEL_CPLL, .rate = CPLL_HZ }, |
| 217 | { .mux = MMC_PLL_SEL_GPLL, .rate = GPLL_HZ }, |
| 218 | { .mux = MMC_PLL_SEL_24M, .rate = 24 * MHz } |
| 219 | }; |
| 220 | |
| 221 | debug("%s: target rate %ld\n", __func__, rate); |
| 222 | for (i = 0; i < ARRAY_SIZE(parents); ++i) { |
| 223 | /* |
| 224 | * Find the largest rate no larger than the target-rate for |
| 225 | * the current parent. |
| 226 | */ |
| 227 | ulong parent_rate = parents[i].rate; |
| 228 | u32 div = DIV_ROUND_UP(parent_rate, rate); |
| 229 | u32 adj_div = div; |
| 230 | ulong new_rate = parent_rate / adj_div; |
| 231 | |
| 232 | debug("%s: rate %ld, parent-mux %d, parent-rate %ld, div %d\n", |
| 233 | __func__, rate, parents[i].mux, parents[i].rate, div); |
| 234 | |
| 235 | /* Skip, if not representable */ |
| 236 | if ((div - 1) > MMC_CLK_DIV_MASK) |
| 237 | continue; |
Andy Yan | b9909aa | 2017-05-15 17:49:56 +0800 | [diff] [blame] | 238 | |
Philipp Tomsich | fbf07a5 | 2017-07-04 14:49:38 +0200 | [diff] [blame] | 239 | /* Skip, if we already have a better (or equal) solution */ |
| 240 | if (new_rate <= best_rate) |
| 241 | continue; |
| 242 | |
| 243 | /* This is our new best rate. */ |
| 244 | best_rate = new_rate; |
| 245 | *best_mux = parents[i].mux; |
| 246 | *best_div = div - 1; |
| 247 | } |
| 248 | |
| 249 | debug("%s: best_mux = %x, best_div = %d, best_rate = %ld\n", |
| 250 | __func__, *best_mux, *best_div, best_rate); |
| 251 | |
| 252 | return best_rate; |
| 253 | } |
| 254 | |
| 255 | static ulong rk3368_mmc_set_clk(struct clk *clk, ulong rate) |
| 256 | { |
| 257 | struct rk3368_clk_priv *priv = dev_get_priv(clk->dev); |
| 258 | struct rk3368_cru *cru = priv->cru; |
| 259 | ulong clk_id = clk->id; |
| 260 | u32 con_id, mux = 0, div = 0; |
| 261 | |
| 262 | /* Find the best parent and rate */ |
| 263 | rk3368_mmc_find_best_rate_and_parent(clk, rate << 1, &mux, &div); |
Andy Yan | b9909aa | 2017-05-15 17:49:56 +0800 | [diff] [blame] | 264 | |
| 265 | switch (clk_id) { |
Philipp Tomsich | fbf07a5 | 2017-07-04 14:49:38 +0200 | [diff] [blame] | 266 | case HCLK_SDMMC: |
Andy Yan | b9909aa | 2017-05-15 17:49:56 +0800 | [diff] [blame] | 267 | con_id = 50; |
| 268 | break; |
Philipp Tomsich | fbf07a5 | 2017-07-04 14:49:38 +0200 | [diff] [blame] | 269 | case HCLK_EMMC: |
Andy Yan | b9909aa | 2017-05-15 17:49:56 +0800 | [diff] [blame] | 270 | con_id = 51; |
| 271 | break; |
| 272 | case SCLK_SDIO0: |
| 273 | con_id = 48; |
| 274 | break; |
| 275 | default: |
| 276 | return -EINVAL; |
| 277 | } |
| 278 | |
Philipp Tomsich | fbf07a5 | 2017-07-04 14:49:38 +0200 | [diff] [blame] | 279 | rk_clrsetreg(&cru->clksel_con[con_id], |
| 280 | MMC_PLL_SEL_MASK | MMC_CLK_DIV_MASK, |
| 281 | mux | div); |
Andy Yan | b9909aa | 2017-05-15 17:49:56 +0800 | [diff] [blame] | 282 | |
| 283 | return rk3368_mmc_get_clk(cru, clk_id); |
| 284 | } |
Philipp Tomsich | fbf07a5 | 2017-07-04 14:49:38 +0200 | [diff] [blame] | 285 | #endif |
Andy Yan | b9909aa | 2017-05-15 17:49:56 +0800 | [diff] [blame] | 286 | |
Philipp Tomsich | c23a993 | 2017-07-05 11:55:23 +0200 | [diff] [blame] | 287 | #if IS_ENABLED(CONFIG_TPL_BUILD) |
Philipp Tomsich | 313b2da | 2017-06-23 00:01:10 +0200 | [diff] [blame] | 288 | static ulong rk3368_ddr_set_clk(struct rk3368_cru *cru, ulong set_rate) |
| 289 | { |
| 290 | const struct pll_div *dpll_cfg = NULL; |
| 291 | const ulong MHz = 1000000; |
| 292 | |
| 293 | /* Fout = ((Fin /NR) * NF )/ NO */ |
Philipp Tomsich | c23a993 | 2017-07-05 11:55:23 +0200 | [diff] [blame] | 294 | static const struct pll_div dpll_1200 = PLL_DIVISORS(1200 * MHz, 1, 1); |
| 295 | static const struct pll_div dpll_1332 = PLL_DIVISORS(1332 * MHz, 2, 1); |
| 296 | static const struct pll_div dpll_1600 = PLL_DIVISORS(1600 * MHz, 3, 2); |
Philipp Tomsich | 313b2da | 2017-06-23 00:01:10 +0200 | [diff] [blame] | 297 | |
| 298 | switch (set_rate) { |
| 299 | case 1200*MHz: |
| 300 | dpll_cfg = &dpll_1200; |
| 301 | break; |
| 302 | case 1332*MHz: |
| 303 | dpll_cfg = &dpll_1332; |
| 304 | break; |
| 305 | case 1600*MHz: |
| 306 | dpll_cfg = &dpll_1600; |
| 307 | break; |
| 308 | default: |
Masahiro Yamada | 81e1042 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 309 | pr_err("Unsupported SDRAM frequency!,%ld\n", set_rate); |
Philipp Tomsich | 313b2da | 2017-06-23 00:01:10 +0200 | [diff] [blame] | 310 | } |
| 311 | rkclk_set_pll(cru, DPLL, dpll_cfg); |
| 312 | |
| 313 | return set_rate; |
| 314 | } |
Philipp Tomsich | c23a993 | 2017-07-05 11:55:23 +0200 | [diff] [blame] | 315 | #endif |
Philipp Tomsich | 313b2da | 2017-06-23 00:01:10 +0200 | [diff] [blame] | 316 | |
Philipp Tomsich | a249f10 | 2017-07-14 19:57:39 +0200 | [diff] [blame] | 317 | #if CONFIG_IS_ENABLED(GMAC_ROCKCHIP) |
David Wu | e72793d | 2018-01-13 14:07:04 +0800 | [diff] [blame] | 318 | static ulong rk3368_gmac_set_clk(struct rk3368_cru *cru, ulong set_rate) |
Philipp Tomsich | a249f10 | 2017-07-14 19:57:39 +0200 | [diff] [blame] | 319 | { |
David Wu | e72793d | 2018-01-13 14:07:04 +0800 | [diff] [blame] | 320 | ulong ret; |
| 321 | |
Philipp Tomsich | a249f10 | 2017-07-14 19:57:39 +0200 | [diff] [blame] | 322 | /* |
David Wu | e72793d | 2018-01-13 14:07:04 +0800 | [diff] [blame] | 323 | * The gmac clock can be derived either from an external clock |
| 324 | * or can be generated from internally by a divider from SCLK_MAC. |
Philipp Tomsich | a249f10 | 2017-07-14 19:57:39 +0200 | [diff] [blame] | 325 | */ |
David Wu | e72793d | 2018-01-13 14:07:04 +0800 | [diff] [blame] | 326 | if (readl(&cru->clksel_con[43]) & GMAC_MUX_SEL_EXTCLK) { |
| 327 | /* An external clock will always generate the right rate... */ |
| 328 | ret = set_rate; |
| 329 | } else { |
| 330 | u32 con = readl(&cru->clksel_con[43]); |
| 331 | ulong pll_rate; |
| 332 | u8 div; |
| 333 | |
| 334 | if (((con >> GMAC_PLL_SHIFT) & GMAC_PLL_MASK) == |
| 335 | GMAC_PLL_SELECT_GENERAL) |
| 336 | pll_rate = GPLL_HZ; |
| 337 | else if (((con >> GMAC_PLL_SHIFT) & GMAC_PLL_MASK) == |
| 338 | GMAC_PLL_SELECT_CODEC) |
| 339 | pll_rate = CPLL_HZ; |
| 340 | else |
| 341 | /* CPLL is not set */ |
| 342 | return -EPERM; |
| 343 | |
| 344 | div = DIV_ROUND_UP(pll_rate, set_rate) - 1; |
| 345 | if (div <= 0x1f) |
| 346 | rk_clrsetreg(&cru->clksel_con[43], GMAC_DIV_CON_MASK, |
| 347 | div << GMAC_DIV_CON_SHIFT); |
| 348 | else |
| 349 | debug("Unsupported div for gmac:%d\n", div); |
| 350 | |
| 351 | return DIV_TO_RATE(pll_rate, div); |
| 352 | } |
| 353 | |
| 354 | return ret; |
Philipp Tomsich | a249f10 | 2017-07-14 19:57:39 +0200 | [diff] [blame] | 355 | } |
| 356 | #endif |
| 357 | |
Philipp Tomsich | b4fb55f | 2017-07-25 16:48:16 +0200 | [diff] [blame] | 358 | /* |
| 359 | * RK3368 SPI clocks have a common divider-width (7 bits) and a single bit |
| 360 | * to select either CPLL or GPLL as the clock-parent. The location within |
| 361 | * the enclosing CLKSEL_CON (i.e. div_shift and sel_shift) are variable. |
| 362 | */ |
| 363 | |
| 364 | struct spi_clkreg { |
| 365 | uint8_t reg; /* CLKSEL_CON[reg] register in CRU */ |
| 366 | uint8_t div_shift; |
| 367 | uint8_t sel_shift; |
| 368 | }; |
| 369 | |
| 370 | /* |
| 371 | * The entries are numbered relative to their offset from SCLK_SPI0. |
| 372 | */ |
| 373 | static const struct spi_clkreg spi_clkregs[] = { |
| 374 | [0] = { .reg = 45, .div_shift = 0, .sel_shift = 7, }, |
| 375 | [1] = { .reg = 45, .div_shift = 8, .sel_shift = 15, }, |
| 376 | [2] = { .reg = 46, .div_shift = 8, .sel_shift = 15, }, |
| 377 | }; |
| 378 | |
| 379 | static inline u32 extract_bits(u32 val, unsigned width, unsigned shift) |
| 380 | { |
| 381 | return (val >> shift) & ((1 << width) - 1); |
| 382 | } |
| 383 | |
| 384 | static ulong rk3368_spi_get_clk(struct rk3368_cru *cru, ulong clk_id) |
| 385 | { |
| 386 | const struct spi_clkreg *spiclk = NULL; |
| 387 | u32 div, val; |
| 388 | |
| 389 | switch (clk_id) { |
| 390 | case SCLK_SPI0 ... SCLK_SPI2: |
| 391 | spiclk = &spi_clkregs[clk_id - SCLK_SPI0]; |
| 392 | break; |
| 393 | |
| 394 | default: |
Masahiro Yamada | 81e1042 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 395 | pr_err("%s: SPI clk-id %ld not supported\n", __func__, clk_id); |
Philipp Tomsich | b4fb55f | 2017-07-25 16:48:16 +0200 | [diff] [blame] | 396 | return -EINVAL; |
| 397 | } |
| 398 | |
| 399 | val = readl(&cru->clksel_con[spiclk->reg]); |
| 400 | div = extract_bits(val, 7, spiclk->div_shift); |
| 401 | |
| 402 | debug("%s: div 0x%x\n", __func__, div); |
| 403 | return DIV_TO_RATE(GPLL_HZ, div); |
| 404 | } |
| 405 | |
| 406 | static ulong rk3368_spi_set_clk(struct rk3368_cru *cru, ulong clk_id, uint hz) |
| 407 | { |
| 408 | const struct spi_clkreg *spiclk = NULL; |
| 409 | int src_clk_div; |
| 410 | |
| 411 | src_clk_div = DIV_ROUND_UP(GPLL_HZ, hz); |
| 412 | assert(src_clk_div < 127); |
| 413 | |
| 414 | switch (clk_id) { |
| 415 | case SCLK_SPI0 ... SCLK_SPI2: |
| 416 | spiclk = &spi_clkregs[clk_id - SCLK_SPI0]; |
| 417 | break; |
| 418 | |
| 419 | default: |
Masahiro Yamada | 81e1042 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 420 | pr_err("%s: SPI clk-id %ld not supported\n", __func__, clk_id); |
Philipp Tomsich | b4fb55f | 2017-07-25 16:48:16 +0200 | [diff] [blame] | 421 | return -EINVAL; |
| 422 | } |
| 423 | |
| 424 | rk_clrsetreg(&cru->clksel_con[spiclk->reg], |
| 425 | ((0x7f << spiclk->div_shift) | |
| 426 | (0x1 << spiclk->sel_shift)), |
| 427 | ((src_clk_div << spiclk->div_shift) | |
| 428 | (1 << spiclk->sel_shift))); |
| 429 | |
| 430 | return rk3368_spi_get_clk(cru, clk_id); |
| 431 | } |
| 432 | |
David Wu | 4771ba6 | 2017-09-20 14:37:50 +0800 | [diff] [blame] | 433 | static ulong rk3368_saradc_get_clk(struct rk3368_cru *cru) |
| 434 | { |
| 435 | u32 div, val; |
| 436 | |
| 437 | val = readl(&cru->clksel_con[25]); |
| 438 | div = bitfield_extract(val, CLK_SARADC_DIV_CON_SHIFT, |
| 439 | CLK_SARADC_DIV_CON_WIDTH); |
| 440 | |
| 441 | return DIV_TO_RATE(OSC_HZ, div); |
| 442 | } |
| 443 | |
| 444 | static ulong rk3368_saradc_set_clk(struct rk3368_cru *cru, uint hz) |
| 445 | { |
| 446 | int src_clk_div; |
| 447 | |
| 448 | src_clk_div = DIV_ROUND_UP(OSC_HZ, hz) - 1; |
| 449 | assert(src_clk_div < 128); |
| 450 | |
| 451 | rk_clrsetreg(&cru->clksel_con[25], |
| 452 | CLK_SARADC_DIV_CON_MASK, |
| 453 | src_clk_div << CLK_SARADC_DIV_CON_SHIFT); |
| 454 | |
| 455 | return rk3368_saradc_get_clk(cru); |
| 456 | } |
| 457 | |
Philipp Tomsich | b4fb55f | 2017-07-25 16:48:16 +0200 | [diff] [blame] | 458 | static ulong rk3368_clk_get_rate(struct clk *clk) |
| 459 | { |
| 460 | struct rk3368_clk_priv *priv = dev_get_priv(clk->dev); |
| 461 | ulong rate = 0; |
| 462 | |
| 463 | debug("%s: id %ld\n", __func__, clk->id); |
| 464 | switch (clk->id) { |
| 465 | case PLL_CPLL: |
| 466 | rate = rkclk_pll_get_rate(priv->cru, CPLL); |
| 467 | break; |
| 468 | case PLL_GPLL: |
| 469 | rate = rkclk_pll_get_rate(priv->cru, GPLL); |
| 470 | break; |
| 471 | case SCLK_SPI0 ... SCLK_SPI2: |
| 472 | rate = rk3368_spi_get_clk(priv->cru, clk->id); |
| 473 | break; |
Simon Glass | b58bfe0 | 2021-08-08 12:20:09 -0600 | [diff] [blame] | 474 | #if !IS_ENABLED(CONFIG_SPL_BUILD) || CONFIG_IS_ENABLED(MMC) |
Philipp Tomsich | b4fb55f | 2017-07-25 16:48:16 +0200 | [diff] [blame] | 475 | case HCLK_SDMMC: |
| 476 | case HCLK_EMMC: |
| 477 | rate = rk3368_mmc_get_clk(priv->cru, clk->id); |
| 478 | break; |
| 479 | #endif |
David Wu | 4771ba6 | 2017-09-20 14:37:50 +0800 | [diff] [blame] | 480 | case SCLK_SARADC: |
| 481 | rate = rk3368_saradc_get_clk(priv->cru); |
| 482 | break; |
Philipp Tomsich | b4fb55f | 2017-07-25 16:48:16 +0200 | [diff] [blame] | 483 | default: |
| 484 | return -ENOENT; |
| 485 | } |
| 486 | |
| 487 | return rate; |
| 488 | } |
| 489 | |
Andy Yan | b9909aa | 2017-05-15 17:49:56 +0800 | [diff] [blame] | 490 | static ulong rk3368_clk_set_rate(struct clk *clk, ulong rate) |
| 491 | { |
Philipp Tomsich | 83a5d2c | 2017-07-05 12:11:58 +0200 | [diff] [blame] | 492 | __maybe_unused struct rk3368_clk_priv *priv = dev_get_priv(clk->dev); |
Andy Yan | b9909aa | 2017-05-15 17:49:56 +0800 | [diff] [blame] | 493 | ulong ret = 0; |
| 494 | |
| 495 | debug("%s id:%ld rate:%ld\n", __func__, clk->id, rate); |
| 496 | switch (clk->id) { |
Philipp Tomsich | b4fb55f | 2017-07-25 16:48:16 +0200 | [diff] [blame] | 497 | case SCLK_SPI0 ... SCLK_SPI2: |
| 498 | ret = rk3368_spi_set_clk(priv->cru, clk->id, rate); |
| 499 | break; |
Philipp Tomsich | c23a993 | 2017-07-05 11:55:23 +0200 | [diff] [blame] | 500 | #if IS_ENABLED(CONFIG_TPL_BUILD) |
Philipp Tomsich | 313b2da | 2017-06-23 00:01:10 +0200 | [diff] [blame] | 501 | case CLK_DDR: |
| 502 | ret = rk3368_ddr_set_clk(priv->cru, rate); |
| 503 | break; |
Philipp Tomsich | c23a993 | 2017-07-05 11:55:23 +0200 | [diff] [blame] | 504 | #endif |
Simon Glass | b58bfe0 | 2021-08-08 12:20:09 -0600 | [diff] [blame] | 505 | #if !IS_ENABLED(CONFIG_SPL_BUILD) || CONFIG_IS_ENABLED(MMC) |
Philipp Tomsich | fbf07a5 | 2017-07-04 14:49:38 +0200 | [diff] [blame] | 506 | case HCLK_SDMMC: |
| 507 | case HCLK_EMMC: |
| 508 | ret = rk3368_mmc_set_clk(clk, rate); |
| 509 | break; |
| 510 | #endif |
Philipp Tomsich | a249f10 | 2017-07-14 19:57:39 +0200 | [diff] [blame] | 511 | #if CONFIG_IS_ENABLED(GMAC_ROCKCHIP) |
Philipp Tomsich | fbf07a5 | 2017-07-04 14:49:38 +0200 | [diff] [blame] | 512 | case SCLK_MAC: |
Philipp Tomsich | a249f10 | 2017-07-14 19:57:39 +0200 | [diff] [blame] | 513 | /* select the external clock */ |
David Wu | e72793d | 2018-01-13 14:07:04 +0800 | [diff] [blame] | 514 | ret = rk3368_gmac_set_clk(priv->cru, rate); |
Andy Yan | b9909aa | 2017-05-15 17:49:56 +0800 | [diff] [blame] | 515 | break; |
Philipp Tomsich | a249f10 | 2017-07-14 19:57:39 +0200 | [diff] [blame] | 516 | #endif |
David Wu | 4771ba6 | 2017-09-20 14:37:50 +0800 | [diff] [blame] | 517 | case SCLK_SARADC: |
| 518 | ret = rk3368_saradc_set_clk(priv->cru, rate); |
| 519 | break; |
Andy Yan | b9909aa | 2017-05-15 17:49:56 +0800 | [diff] [blame] | 520 | default: |
| 521 | return -ENOENT; |
| 522 | } |
| 523 | |
| 524 | return ret; |
| 525 | } |
| 526 | |
Philipp Tomsich | 6dd2fb4 | 2018-01-25 15:27:10 +0100 | [diff] [blame] | 527 | static int __maybe_unused rk3368_gmac_set_parent(struct clk *clk, struct clk *parent) |
David Wu | e72793d | 2018-01-13 14:07:04 +0800 | [diff] [blame] | 528 | { |
| 529 | struct rk3368_clk_priv *priv = dev_get_priv(clk->dev); |
| 530 | struct rk3368_cru *cru = priv->cru; |
| 531 | const char *clock_output_name; |
| 532 | int ret; |
| 533 | |
| 534 | /* |
| 535 | * If the requested parent is in the same clock-controller and |
| 536 | * the id is SCLK_MAC ("sclk_mac"), switch to the internal |
| 537 | * clock. |
| 538 | */ |
| 539 | if ((parent->dev == clk->dev) && (parent->id == SCLK_MAC)) { |
| 540 | debug("%s: switching GAMC to SCLK_MAC\n", __func__); |
| 541 | rk_clrreg(&cru->clksel_con[43], GMAC_MUX_SEL_EXTCLK); |
| 542 | return 0; |
| 543 | } |
| 544 | |
| 545 | /* |
| 546 | * Otherwise, we need to check the clock-output-names of the |
| 547 | * requested parent to see if the requested id is "ext_gmac". |
| 548 | */ |
| 549 | ret = dev_read_string_index(parent->dev, "clock-output-names", |
| 550 | parent->id, &clock_output_name); |
| 551 | if (ret < 0) |
| 552 | return -ENODATA; |
| 553 | |
| 554 | /* If this is "ext_gmac", switch to the external clock input */ |
| 555 | if (!strcmp(clock_output_name, "ext_gmac")) { |
| 556 | debug("%s: switching GMAC to external clock\n", __func__); |
| 557 | rk_setreg(&cru->clksel_con[43], GMAC_MUX_SEL_EXTCLK); |
| 558 | return 0; |
| 559 | } |
| 560 | |
| 561 | return -EINVAL; |
| 562 | } |
| 563 | |
Philipp Tomsich | 6dd2fb4 | 2018-01-25 15:27:10 +0100 | [diff] [blame] | 564 | static int __maybe_unused rk3368_clk_set_parent(struct clk *clk, struct clk *parent) |
David Wu | e72793d | 2018-01-13 14:07:04 +0800 | [diff] [blame] | 565 | { |
| 566 | switch (clk->id) { |
| 567 | case SCLK_MAC: |
| 568 | return rk3368_gmac_set_parent(clk, parent); |
| 569 | } |
| 570 | |
| 571 | debug("%s: unsupported clk %ld\n", __func__, clk->id); |
| 572 | return -ENOENT; |
| 573 | } |
| 574 | |
Andy Yan | b9909aa | 2017-05-15 17:49:56 +0800 | [diff] [blame] | 575 | static struct clk_ops rk3368_clk_ops = { |
| 576 | .get_rate = rk3368_clk_get_rate, |
| 577 | .set_rate = rk3368_clk_set_rate, |
Simon Glass | 3580f6d | 2021-08-07 07:24:03 -0600 | [diff] [blame] | 578 | #if CONFIG_IS_ENABLED(OF_REAL) |
David Wu | e72793d | 2018-01-13 14:07:04 +0800 | [diff] [blame] | 579 | .set_parent = rk3368_clk_set_parent, |
Philipp Tomsich | 6dd2fb4 | 2018-01-25 15:27:10 +0100 | [diff] [blame] | 580 | #endif |
Andy Yan | b9909aa | 2017-05-15 17:49:56 +0800 | [diff] [blame] | 581 | }; |
| 582 | |
| 583 | static int rk3368_clk_probe(struct udevice *dev) |
| 584 | { |
Philipp Tomsich | 415ff7e | 2017-06-22 23:53:44 +0200 | [diff] [blame] | 585 | struct rk3368_clk_priv __maybe_unused *priv = dev_get_priv(dev); |
Philipp Tomsich | 79aa1ab | 2017-06-22 23:51:37 +0200 | [diff] [blame] | 586 | #if CONFIG_IS_ENABLED(OF_PLATDATA) |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 587 | struct rk3368_clk_plat *plat = dev_get_plat(dev); |
Andy Yan | b9909aa | 2017-05-15 17:49:56 +0800 | [diff] [blame] | 588 | |
Simon Glass | 1b1fe41 | 2017-08-29 14:15:50 -0600 | [diff] [blame] | 589 | priv->cru = map_sysmem(plat->dtd.reg[0], plat->dtd.reg[1]); |
Philipp Tomsich | 79aa1ab | 2017-06-22 23:51:37 +0200 | [diff] [blame] | 590 | #endif |
Philipp Tomsich | 415ff7e | 2017-06-22 23:53:44 +0200 | [diff] [blame] | 591 | #if IS_ENABLED(CONFIG_SPL_BUILD) || IS_ENABLED(CONFIG_TPL_BUILD) |
Andy Yan | b9909aa | 2017-05-15 17:49:56 +0800 | [diff] [blame] | 592 | rkclk_init(priv->cru); |
Philipp Tomsich | 415ff7e | 2017-06-22 23:53:44 +0200 | [diff] [blame] | 593 | #endif |
Andy Yan | b9909aa | 2017-05-15 17:49:56 +0800 | [diff] [blame] | 594 | |
| 595 | return 0; |
| 596 | } |
| 597 | |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 598 | static int rk3368_clk_of_to_plat(struct udevice *dev) |
Andy Yan | b9909aa | 2017-05-15 17:49:56 +0800 | [diff] [blame] | 599 | { |
Simon Glass | 6d70ba0 | 2021-08-07 07:24:06 -0600 | [diff] [blame] | 600 | if (CONFIG_IS_ENABLED(OF_REAL)) { |
| 601 | struct rk3368_clk_priv *priv = dev_get_priv(dev); |
Andy Yan | b9909aa | 2017-05-15 17:49:56 +0800 | [diff] [blame] | 602 | |
Simon Glass | 6d70ba0 | 2021-08-07 07:24:06 -0600 | [diff] [blame] | 603 | priv->cru = dev_read_addr_ptr(dev); |
| 604 | } |
Andy Yan | b9909aa | 2017-05-15 17:49:56 +0800 | [diff] [blame] | 605 | |
| 606 | return 0; |
| 607 | } |
| 608 | |
| 609 | static int rk3368_clk_bind(struct udevice *dev) |
| 610 | { |
| 611 | int ret; |
Kever Yang | 4fbb6c2 | 2017-11-03 15:16:13 +0800 | [diff] [blame] | 612 | struct udevice *sys_child; |
| 613 | struct sysreset_reg *priv; |
Andy Yan | b9909aa | 2017-05-15 17:49:56 +0800 | [diff] [blame] | 614 | |
| 615 | /* The reset driver does not have a device node, so bind it here */ |
Kever Yang | 4fbb6c2 | 2017-11-03 15:16:13 +0800 | [diff] [blame] | 616 | ret = device_bind_driver(dev, "rockchip_sysreset", "sysreset", |
| 617 | &sys_child); |
| 618 | if (ret) { |
| 619 | debug("Warning: No sysreset driver: ret=%d\n", ret); |
| 620 | } else { |
| 621 | priv = malloc(sizeof(struct sysreset_reg)); |
| 622 | priv->glb_srst_fst_value = offsetof(struct rk3368_cru, |
| 623 | glb_srst_fst_val); |
| 624 | priv->glb_srst_snd_value = offsetof(struct rk3368_cru, |
| 625 | glb_srst_snd_val); |
Simon Glass | 9558862 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 626 | dev_set_priv(sys_child, priv); |
Kever Yang | 4fbb6c2 | 2017-11-03 15:16:13 +0800 | [diff] [blame] | 627 | } |
Andy Yan | b9909aa | 2017-05-15 17:49:56 +0800 | [diff] [blame] | 628 | |
Heiko Stuebner | 416f8d3 | 2019-11-09 00:06:30 +0100 | [diff] [blame] | 629 | #if CONFIG_IS_ENABLED(RESET_ROCKCHIP) |
Elaine Zhang | 432976f | 2017-12-19 18:22:38 +0800 | [diff] [blame] | 630 | ret = offsetof(struct rk3368_cru, softrst_con[0]); |
| 631 | ret = rockchip_reset_bind(dev, ret, 15); |
| 632 | if (ret) |
Eugen Hristev | f179826 | 2023-04-11 10:17:56 +0300 | [diff] [blame] | 633 | debug("Warning: software reset driver bind failed\n"); |
Elaine Zhang | 432976f | 2017-12-19 18:22:38 +0800 | [diff] [blame] | 634 | #endif |
| 635 | |
Andy Yan | b9909aa | 2017-05-15 17:49:56 +0800 | [diff] [blame] | 636 | return ret; |
| 637 | } |
| 638 | |
| 639 | static const struct udevice_id rk3368_clk_ids[] = { |
| 640 | { .compatible = "rockchip,rk3368-cru" }, |
| 641 | { } |
| 642 | }; |
| 643 | |
| 644 | U_BOOT_DRIVER(rockchip_rk3368_cru) = { |
| 645 | .name = "rockchip_rk3368_cru", |
| 646 | .id = UCLASS_CLK, |
| 647 | .of_match = rk3368_clk_ids, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 648 | .priv_auto = sizeof(struct rk3368_clk_priv), |
Philipp Tomsich | 79aa1ab | 2017-06-22 23:51:37 +0200 | [diff] [blame] | 649 | #if CONFIG_IS_ENABLED(OF_PLATDATA) |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 650 | .plat_auto = sizeof(struct rk3368_clk_plat), |
Philipp Tomsich | 79aa1ab | 2017-06-22 23:51:37 +0200 | [diff] [blame] | 651 | #endif |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 652 | .of_to_plat = rk3368_clk_of_to_plat, |
Andy Yan | b9909aa | 2017-05-15 17:49:56 +0800 | [diff] [blame] | 653 | .ops = &rk3368_clk_ops, |
| 654 | .bind = rk3368_clk_bind, |
| 655 | .probe = rk3368_clk_probe, |
| 656 | }; |