Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
huang lin | 01aa702 | 2015-11-17 14:20:16 +0800 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2015 Google, Inc |
huang lin | 01aa702 | 2015-11-17 14:20:16 +0800 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #include <common.h> |
Stephen Warren | a962243 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 7 | #include <clk-uclass.h> |
huang lin | 01aa702 | 2015-11-17 14:20:16 +0800 | [diff] [blame] | 8 | #include <dm.h> |
| 9 | #include <errno.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 10 | #include <log.h> |
Simon Glass | 9bc1564 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 11 | #include <malloc.h> |
huang lin | 01aa702 | 2015-11-17 14:20:16 +0800 | [diff] [blame] | 12 | #include <syscon.h> |
Kever Yang | 9fbe17c | 2019-03-28 11:01:23 +0800 | [diff] [blame] | 13 | #include <asm/arch-rockchip/clock.h> |
| 14 | #include <asm/arch-rockchip/cru_rk3036.h> |
| 15 | #include <asm/arch-rockchip/hardware.h> |
Simon Glass | 9558862 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 16 | #include <dm/device-internal.h> |
huang lin | 01aa702 | 2015-11-17 14:20:16 +0800 | [diff] [blame] | 17 | #include <dm/lists.h> |
Simon Glass | 8d32f4b | 2016-01-21 19:43:38 -0700 | [diff] [blame] | 18 | #include <dt-bindings/clock/rk3036-cru.h> |
Simon Glass | dbd7954 | 2020-05-10 11:40:11 -0600 | [diff] [blame] | 19 | #include <linux/delay.h> |
Heiko Stübner | 1b7dcc3 | 2016-07-22 23:51:06 +0200 | [diff] [blame] | 20 | #include <linux/log2.h> |
Simon Glass | fb64e36 | 2020-05-10 11:40:09 -0600 | [diff] [blame] | 21 | #include <linux/stringify.h> |
huang lin | 01aa702 | 2015-11-17 14:20:16 +0800 | [diff] [blame] | 22 | |
huang lin | 01aa702 | 2015-11-17 14:20:16 +0800 | [diff] [blame] | 23 | enum { |
| 24 | VCO_MAX_HZ = 2400U * 1000000, |
| 25 | VCO_MIN_HZ = 600 * 1000000, |
| 26 | OUTPUT_MAX_HZ = 2400U * 1000000, |
| 27 | OUTPUT_MIN_HZ = 24 * 1000000, |
| 28 | }; |
| 29 | |
| 30 | #define RATE_TO_DIV(input_rate, output_rate) \ |
| 31 | ((input_rate) / (output_rate) - 1); |
| 32 | |
| 33 | #define DIV_TO_RATE(input_rate, div) ((input_rate) / ((div) + 1)) |
| 34 | |
| 35 | #define PLL_DIVISORS(hz, _refdiv, _postdiv1, _postdiv2) {\ |
| 36 | .refdiv = _refdiv,\ |
| 37 | .fbdiv = (u32)((u64)hz * _refdiv * _postdiv1 * _postdiv2 / OSC_HZ),\ |
| 38 | .postdiv1 = _postdiv1, .postdiv2 = _postdiv2};\ |
| 39 | _Static_assert(((u64)hz * _refdiv * _postdiv1 * _postdiv2 / OSC_HZ) *\ |
| 40 | OSC_HZ / (_refdiv * _postdiv1 * _postdiv2) == hz,\ |
| 41 | #hz "Hz cannot be hit with PLL "\ |
| 42 | "divisors on line " __stringify(__LINE__)); |
| 43 | |
Kever Yang | ef5eb17 | 2017-06-13 10:03:11 +0800 | [diff] [blame] | 44 | /* use integer mode*/ |
huang lin | 01aa702 | 2015-11-17 14:20:16 +0800 | [diff] [blame] | 45 | static const struct pll_div apll_init_cfg = PLL_DIVISORS(APLL_HZ, 1, 3, 1); |
| 46 | static const struct pll_div gpll_init_cfg = PLL_DIVISORS(GPLL_HZ, 2, 2, 1); |
| 47 | |
huang lin | 01aa702 | 2015-11-17 14:20:16 +0800 | [diff] [blame] | 48 | static int rkclk_set_pll(struct rk3036_cru *cru, enum rk_clk_id clk_id, |
| 49 | const struct pll_div *div) |
| 50 | { |
| 51 | int pll_id = rk_pll_id(clk_id); |
| 52 | struct rk3036_pll *pll = &cru->pll[pll_id]; |
| 53 | |
| 54 | /* All PLLs have same VCO and output frequency range restrictions. */ |
| 55 | uint vco_hz = OSC_HZ / 1000 * div->fbdiv / div->refdiv * 1000; |
| 56 | uint output_hz = vco_hz / div->postdiv1 / div->postdiv2; |
| 57 | |
| 58 | debug("PLL at %p: fbdiv=%d, refdiv=%d, postdiv1=%d, postdiv2=%d,\ |
| 59 | vco=%u Hz, output=%u Hz\n", |
| 60 | pll, div->fbdiv, div->refdiv, div->postdiv1, |
| 61 | div->postdiv2, vco_hz, output_hz); |
| 62 | assert(vco_hz >= VCO_MIN_HZ && vco_hz <= VCO_MAX_HZ && |
| 63 | output_hz >= OUTPUT_MIN_HZ && output_hz <= OUTPUT_MAX_HZ); |
| 64 | |
Kever Yang | ef5eb17 | 2017-06-13 10:03:11 +0800 | [diff] [blame] | 65 | /* use integer mode */ |
| 66 | rk_setreg(&pll->con1, 1 << PLL_DSMPD_SHIFT); |
huang lin | 01aa702 | 2015-11-17 14:20:16 +0800 | [diff] [blame] | 67 | |
| 68 | rk_clrsetreg(&pll->con0, |
Kever Yang | cb04ad2 | 2017-05-15 20:52:15 +0800 | [diff] [blame] | 69 | PLL_POSTDIV1_MASK | PLL_FBDIV_MASK, |
huang lin | 01aa702 | 2015-11-17 14:20:16 +0800 | [diff] [blame] | 70 | (div->postdiv1 << PLL_POSTDIV1_SHIFT) | div->fbdiv); |
Kever Yang | cb04ad2 | 2017-05-15 20:52:15 +0800 | [diff] [blame] | 71 | rk_clrsetreg(&pll->con1, PLL_POSTDIV2_MASK | PLL_REFDIV_MASK, |
| 72 | (div->postdiv2 << PLL_POSTDIV2_SHIFT | |
| 73 | div->refdiv << PLL_REFDIV_SHIFT)); |
huang lin | 01aa702 | 2015-11-17 14:20:16 +0800 | [diff] [blame] | 74 | |
| 75 | /* waiting for pll lock */ |
| 76 | while (readl(&pll->con1) & (1 << PLL_LOCK_STATUS_SHIFT)) |
| 77 | udelay(1); |
| 78 | |
| 79 | return 0; |
| 80 | } |
| 81 | |
| 82 | static void rkclk_init(struct rk3036_cru *cru) |
| 83 | { |
| 84 | u32 aclk_div; |
| 85 | u32 hclk_div; |
| 86 | u32 pclk_div; |
| 87 | |
| 88 | /* pll enter slow-mode */ |
| 89 | rk_clrsetreg(&cru->cru_mode_con, |
Kever Yang | cb04ad2 | 2017-05-15 20:52:15 +0800 | [diff] [blame] | 90 | GPLL_MODE_MASK | APLL_MODE_MASK, |
huang lin | 01aa702 | 2015-11-17 14:20:16 +0800 | [diff] [blame] | 91 | GPLL_MODE_SLOW << GPLL_MODE_SHIFT | |
| 92 | APLL_MODE_SLOW << APLL_MODE_SHIFT); |
| 93 | |
| 94 | /* init pll */ |
| 95 | rkclk_set_pll(cru, CLK_ARM, &apll_init_cfg); |
| 96 | rkclk_set_pll(cru, CLK_GENERAL, &gpll_init_cfg); |
| 97 | |
| 98 | /* |
Kever Yang | cb04ad2 | 2017-05-15 20:52:15 +0800 | [diff] [blame] | 99 | * select apll as cpu/core clock pll source and |
| 100 | * set up dependent divisors for PERI and ACLK clocks. |
huang lin | 01aa702 | 2015-11-17 14:20:16 +0800 | [diff] [blame] | 101 | * core hz : apll = 1:1 |
| 102 | */ |
| 103 | aclk_div = APLL_HZ / CORE_ACLK_HZ - 1; |
| 104 | assert((aclk_div + 1) * CORE_ACLK_HZ == APLL_HZ && aclk_div < 0x7); |
| 105 | |
| 106 | pclk_div = APLL_HZ / CORE_PERI_HZ - 1; |
| 107 | assert((pclk_div + 1) * CORE_PERI_HZ == APLL_HZ && pclk_div < 0xf); |
| 108 | |
| 109 | rk_clrsetreg(&cru->cru_clksel_con[0], |
Kever Yang | cb04ad2 | 2017-05-15 20:52:15 +0800 | [diff] [blame] | 110 | CORE_CLK_PLL_SEL_MASK | CORE_DIV_CON_MASK, |
huang lin | 01aa702 | 2015-11-17 14:20:16 +0800 | [diff] [blame] | 111 | CORE_CLK_PLL_SEL_APLL << CORE_CLK_PLL_SEL_SHIFT | |
| 112 | 0 << CORE_DIV_CON_SHIFT); |
| 113 | |
| 114 | rk_clrsetreg(&cru->cru_clksel_con[1], |
Kever Yang | cb04ad2 | 2017-05-15 20:52:15 +0800 | [diff] [blame] | 115 | CORE_ACLK_DIV_MASK | CORE_PERI_DIV_MASK, |
huang lin | 01aa702 | 2015-11-17 14:20:16 +0800 | [diff] [blame] | 116 | aclk_div << CORE_ACLK_DIV_SHIFT | |
| 117 | pclk_div << CORE_PERI_DIV_SHIFT); |
| 118 | |
| 119 | /* |
Kever Yang | b45fc40 | 2017-05-15 20:52:16 +0800 | [diff] [blame] | 120 | * select apll as pd_bus bus clock source and |
huang lin | 01aa702 | 2015-11-17 14:20:16 +0800 | [diff] [blame] | 121 | * set up dependent divisors for PCLK/HCLK and ACLK clocks. |
| 122 | */ |
Kever Yang | b45fc40 | 2017-05-15 20:52:16 +0800 | [diff] [blame] | 123 | aclk_div = GPLL_HZ / BUS_ACLK_HZ - 1; |
| 124 | assert((aclk_div + 1) * BUS_ACLK_HZ == GPLL_HZ && aclk_div <= 0x1f); |
huang lin | 01aa702 | 2015-11-17 14:20:16 +0800 | [diff] [blame] | 125 | |
Kever Yang | b45fc40 | 2017-05-15 20:52:16 +0800 | [diff] [blame] | 126 | pclk_div = GPLL_HZ / BUS_PCLK_HZ - 1; |
| 127 | assert((pclk_div + 1) * BUS_PCLK_HZ == GPLL_HZ && pclk_div <= 0x7); |
huang lin | 01aa702 | 2015-11-17 14:20:16 +0800 | [diff] [blame] | 128 | |
Kever Yang | b45fc40 | 2017-05-15 20:52:16 +0800 | [diff] [blame] | 129 | hclk_div = GPLL_HZ / BUS_HCLK_HZ - 1; |
| 130 | assert((hclk_div + 1) * BUS_HCLK_HZ == GPLL_HZ && hclk_div <= 0x3); |
huang lin | 01aa702 | 2015-11-17 14:20:16 +0800 | [diff] [blame] | 131 | |
| 132 | rk_clrsetreg(&cru->cru_clksel_con[0], |
Kever Yang | cb04ad2 | 2017-05-15 20:52:15 +0800 | [diff] [blame] | 133 | BUS_ACLK_PLL_SEL_MASK | BUS_ACLK_DIV_MASK, |
Kever Yang | b45fc40 | 2017-05-15 20:52:16 +0800 | [diff] [blame] | 134 | BUS_ACLK_PLL_SEL_GPLL << BUS_ACLK_PLL_SEL_SHIFT | |
Kever Yang | cb04ad2 | 2017-05-15 20:52:15 +0800 | [diff] [blame] | 135 | aclk_div << BUS_ACLK_DIV_SHIFT); |
huang lin | 01aa702 | 2015-11-17 14:20:16 +0800 | [diff] [blame] | 136 | |
| 137 | rk_clrsetreg(&cru->cru_clksel_con[1], |
Kever Yang | cb04ad2 | 2017-05-15 20:52:15 +0800 | [diff] [blame] | 138 | BUS_PCLK_DIV_MASK | BUS_HCLK_DIV_MASK, |
| 139 | pclk_div << BUS_PCLK_DIV_SHIFT | |
| 140 | hclk_div << BUS_HCLK_DIV_SHIFT); |
huang lin | 01aa702 | 2015-11-17 14:20:16 +0800 | [diff] [blame] | 141 | |
| 142 | /* |
Kever Yang | cb04ad2 | 2017-05-15 20:52:15 +0800 | [diff] [blame] | 143 | * select gpll as pd_peri bus clock source and |
huang lin | 01aa702 | 2015-11-17 14:20:16 +0800 | [diff] [blame] | 144 | * set up dependent divisors for PCLK/HCLK and ACLK clocks. |
| 145 | */ |
| 146 | aclk_div = GPLL_HZ / PERI_ACLK_HZ - 1; |
| 147 | assert((aclk_div + 1) * PERI_ACLK_HZ == GPLL_HZ && aclk_div < 0x1f); |
| 148 | |
Heiko Stübner | 1b7dcc3 | 2016-07-22 23:51:06 +0200 | [diff] [blame] | 149 | hclk_div = ilog2(PERI_ACLK_HZ / PERI_HCLK_HZ); |
huang lin | 01aa702 | 2015-11-17 14:20:16 +0800 | [diff] [blame] | 150 | assert((1 << hclk_div) * PERI_HCLK_HZ == |
Kever Yang | b45fc40 | 2017-05-15 20:52:16 +0800 | [diff] [blame] | 151 | PERI_ACLK_HZ && (hclk_div < 0x4)); |
huang lin | 01aa702 | 2015-11-17 14:20:16 +0800 | [diff] [blame] | 152 | |
Heiko Stübner | 1b7dcc3 | 2016-07-22 23:51:06 +0200 | [diff] [blame] | 153 | pclk_div = ilog2(PERI_ACLK_HZ / PERI_PCLK_HZ); |
huang lin | 01aa702 | 2015-11-17 14:20:16 +0800 | [diff] [blame] | 154 | assert((1 << pclk_div) * PERI_PCLK_HZ == |
| 155 | PERI_ACLK_HZ && pclk_div < 0x8); |
| 156 | |
| 157 | rk_clrsetreg(&cru->cru_clksel_con[10], |
Kever Yang | cb04ad2 | 2017-05-15 20:52:15 +0800 | [diff] [blame] | 158 | PERI_PLL_SEL_MASK | PERI_PCLK_DIV_MASK | |
| 159 | PERI_HCLK_DIV_MASK | PERI_ACLK_DIV_MASK, |
huang lin | 01aa702 | 2015-11-17 14:20:16 +0800 | [diff] [blame] | 160 | PERI_PLL_GPLL << PERI_PLL_SEL_SHIFT | |
| 161 | pclk_div << PERI_PCLK_DIV_SHIFT | |
| 162 | hclk_div << PERI_HCLK_DIV_SHIFT | |
| 163 | aclk_div << PERI_ACLK_DIV_SHIFT); |
| 164 | |
| 165 | /* PLL enter normal-mode */ |
| 166 | rk_clrsetreg(&cru->cru_mode_con, |
Kever Yang | cb04ad2 | 2017-05-15 20:52:15 +0800 | [diff] [blame] | 167 | GPLL_MODE_MASK | APLL_MODE_MASK, |
huang lin | 01aa702 | 2015-11-17 14:20:16 +0800 | [diff] [blame] | 168 | GPLL_MODE_NORM << GPLL_MODE_SHIFT | |
| 169 | APLL_MODE_NORM << APLL_MODE_SHIFT); |
| 170 | } |
| 171 | |
| 172 | /* Get pll rate by id */ |
| 173 | static uint32_t rkclk_pll_get_rate(struct rk3036_cru *cru, |
| 174 | enum rk_clk_id clk_id) |
| 175 | { |
| 176 | uint32_t refdiv, fbdiv, postdiv1, postdiv2; |
| 177 | uint32_t con; |
| 178 | int pll_id = rk_pll_id(clk_id); |
| 179 | struct rk3036_pll *pll = &cru->pll[pll_id]; |
| 180 | static u8 clk_shift[CLK_COUNT] = { |
| 181 | 0xff, APLL_MODE_SHIFT, DPLL_MODE_SHIFT, 0xff, |
| 182 | GPLL_MODE_SHIFT, 0xff |
| 183 | }; |
Kever Yang | cb04ad2 | 2017-05-15 20:52:15 +0800 | [diff] [blame] | 184 | static u32 clk_mask[CLK_COUNT] = { |
| 185 | 0xffffffff, APLL_MODE_MASK, DPLL_MODE_MASK, 0xffffffff, |
| 186 | GPLL_MODE_MASK, 0xffffffff |
huang lin | 01aa702 | 2015-11-17 14:20:16 +0800 | [diff] [blame] | 187 | }; |
| 188 | uint shift; |
| 189 | uint mask; |
| 190 | |
| 191 | con = readl(&cru->cru_mode_con); |
| 192 | shift = clk_shift[clk_id]; |
| 193 | mask = clk_mask[clk_id]; |
| 194 | |
Kever Yang | cb04ad2 | 2017-05-15 20:52:15 +0800 | [diff] [blame] | 195 | switch ((con & mask) >> shift) { |
huang lin | 01aa702 | 2015-11-17 14:20:16 +0800 | [diff] [blame] | 196 | case GPLL_MODE_SLOW: |
| 197 | return OSC_HZ; |
| 198 | case GPLL_MODE_NORM: |
| 199 | |
| 200 | /* normal mode */ |
| 201 | con = readl(&pll->con0); |
Kever Yang | cb04ad2 | 2017-05-15 20:52:15 +0800 | [diff] [blame] | 202 | postdiv1 = (con & PLL_POSTDIV1_MASK) >> PLL_POSTDIV1_SHIFT; |
| 203 | fbdiv = (con & PLL_FBDIV_MASK) >> PLL_FBDIV_SHIFT; |
huang lin | 01aa702 | 2015-11-17 14:20:16 +0800 | [diff] [blame] | 204 | con = readl(&pll->con1); |
Kever Yang | cb04ad2 | 2017-05-15 20:52:15 +0800 | [diff] [blame] | 205 | postdiv2 = (con & PLL_POSTDIV2_MASK) >> PLL_POSTDIV2_SHIFT; |
| 206 | refdiv = (con & PLL_REFDIV_MASK) >> PLL_REFDIV_SHIFT; |
huang lin | 01aa702 | 2015-11-17 14:20:16 +0800 | [diff] [blame] | 207 | return (24 * fbdiv / (refdiv * postdiv1 * postdiv2)) * 1000000; |
| 208 | case GPLL_MODE_DEEP: |
| 209 | default: |
| 210 | return 32768; |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | static ulong rockchip_mmc_get_clk(struct rk3036_cru *cru, uint clk_general_rate, |
Simon Glass | 8d32f4b | 2016-01-21 19:43:38 -0700 | [diff] [blame] | 215 | int periph) |
huang lin | 01aa702 | 2015-11-17 14:20:16 +0800 | [diff] [blame] | 216 | { |
| 217 | uint src_rate; |
| 218 | uint div, mux; |
| 219 | u32 con; |
| 220 | |
| 221 | switch (periph) { |
Simon Glass | 8d32f4b | 2016-01-21 19:43:38 -0700 | [diff] [blame] | 222 | case HCLK_EMMC: |
Xu Ziyuan | 6682b9b | 2017-04-16 17:44:43 +0800 | [diff] [blame] | 223 | case SCLK_EMMC: |
huang lin | 01aa702 | 2015-11-17 14:20:16 +0800 | [diff] [blame] | 224 | con = readl(&cru->cru_clksel_con[12]); |
Kever Yang | cb04ad2 | 2017-05-15 20:52:15 +0800 | [diff] [blame] | 225 | mux = (con & EMMC_PLL_MASK) >> EMMC_PLL_SHIFT; |
| 226 | div = (con & EMMC_DIV_MASK) >> EMMC_DIV_SHIFT; |
huang lin | 01aa702 | 2015-11-17 14:20:16 +0800 | [diff] [blame] | 227 | break; |
Simon Glass | 8d32f4b | 2016-01-21 19:43:38 -0700 | [diff] [blame] | 228 | case HCLK_SDIO: |
Xu Ziyuan | 6682b9b | 2017-04-16 17:44:43 +0800 | [diff] [blame] | 229 | case SCLK_SDIO: |
huang lin | 01aa702 | 2015-11-17 14:20:16 +0800 | [diff] [blame] | 230 | con = readl(&cru->cru_clksel_con[12]); |
Kever Yang | cb04ad2 | 2017-05-15 20:52:15 +0800 | [diff] [blame] | 231 | mux = (con & MMC0_PLL_MASK) >> MMC0_PLL_SHIFT; |
| 232 | div = (con & MMC0_DIV_MASK) >> MMC0_DIV_SHIFT; |
huang lin | 01aa702 | 2015-11-17 14:20:16 +0800 | [diff] [blame] | 233 | break; |
| 234 | default: |
| 235 | return -EINVAL; |
| 236 | } |
| 237 | |
| 238 | src_rate = mux == EMMC_SEL_24M ? OSC_HZ : clk_general_rate; |
Kever Yang | 99b546d | 2017-07-27 12:54:01 +0800 | [diff] [blame] | 239 | return DIV_TO_RATE(src_rate, div) / 2; |
huang lin | 01aa702 | 2015-11-17 14:20:16 +0800 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | static ulong rockchip_mmc_set_clk(struct rk3036_cru *cru, uint clk_general_rate, |
Simon Glass | 8d32f4b | 2016-01-21 19:43:38 -0700 | [diff] [blame] | 243 | int periph, uint freq) |
huang lin | 01aa702 | 2015-11-17 14:20:16 +0800 | [diff] [blame] | 244 | { |
| 245 | int src_clk_div; |
| 246 | int mux; |
| 247 | |
| 248 | debug("%s: clk_general_rate=%u\n", __func__, clk_general_rate); |
| 249 | |
| 250 | /* mmc clock auto divide 2 in internal */ |
Kever Yang | 99b546d | 2017-07-27 12:54:01 +0800 | [diff] [blame] | 251 | src_clk_div = DIV_ROUND_UP(clk_general_rate / 2, freq); |
huang lin | 01aa702 | 2015-11-17 14:20:16 +0800 | [diff] [blame] | 252 | |
Kever Yang | f20995b | 2017-07-27 12:54:02 +0800 | [diff] [blame] | 253 | if (src_clk_div > 128) { |
Kever Yang | 99b546d | 2017-07-27 12:54:01 +0800 | [diff] [blame] | 254 | src_clk_div = DIV_ROUND_UP(OSC_HZ / 2, freq); |
Kever Yang | f20995b | 2017-07-27 12:54:02 +0800 | [diff] [blame] | 255 | assert(src_clk_div - 1 < 128); |
huang lin | 01aa702 | 2015-11-17 14:20:16 +0800 | [diff] [blame] | 256 | mux = EMMC_SEL_24M; |
| 257 | } else { |
| 258 | mux = EMMC_SEL_GPLL; |
| 259 | } |
| 260 | |
| 261 | switch (periph) { |
Simon Glass | 8d32f4b | 2016-01-21 19:43:38 -0700 | [diff] [blame] | 262 | case HCLK_EMMC: |
Xu Ziyuan | 6682b9b | 2017-04-16 17:44:43 +0800 | [diff] [blame] | 263 | case SCLK_EMMC: |
huang lin | 01aa702 | 2015-11-17 14:20:16 +0800 | [diff] [blame] | 264 | rk_clrsetreg(&cru->cru_clksel_con[12], |
Kever Yang | cb04ad2 | 2017-05-15 20:52:15 +0800 | [diff] [blame] | 265 | EMMC_PLL_MASK | EMMC_DIV_MASK, |
huang lin | 01aa702 | 2015-11-17 14:20:16 +0800 | [diff] [blame] | 266 | mux << EMMC_PLL_SHIFT | |
| 267 | (src_clk_div - 1) << EMMC_DIV_SHIFT); |
| 268 | break; |
Simon Glass | 8d32f4b | 2016-01-21 19:43:38 -0700 | [diff] [blame] | 269 | case HCLK_SDIO: |
Xu Ziyuan | 6682b9b | 2017-04-16 17:44:43 +0800 | [diff] [blame] | 270 | case SCLK_SDIO: |
huang lin | 01aa702 | 2015-11-17 14:20:16 +0800 | [diff] [blame] | 271 | rk_clrsetreg(&cru->cru_clksel_con[11], |
Kever Yang | cb04ad2 | 2017-05-15 20:52:15 +0800 | [diff] [blame] | 272 | MMC0_PLL_MASK | MMC0_DIV_MASK, |
huang lin | 01aa702 | 2015-11-17 14:20:16 +0800 | [diff] [blame] | 273 | mux << MMC0_PLL_SHIFT | |
| 274 | (src_clk_div - 1) << MMC0_DIV_SHIFT); |
| 275 | break; |
| 276 | default: |
| 277 | return -EINVAL; |
| 278 | } |
| 279 | |
| 280 | return rockchip_mmc_get_clk(cru, clk_general_rate, periph); |
| 281 | } |
| 282 | |
Stephen Warren | a962243 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 283 | static ulong rk3036_clk_get_rate(struct clk *clk) |
huang lin | 01aa702 | 2015-11-17 14:20:16 +0800 | [diff] [blame] | 284 | { |
Stephen Warren | a962243 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 285 | struct rk3036_clk_priv *priv = dev_get_priv(clk->dev); |
huang lin | 01aa702 | 2015-11-17 14:20:16 +0800 | [diff] [blame] | 286 | |
Stephen Warren | a962243 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 287 | switch (clk->id) { |
| 288 | case 0 ... 63: |
| 289 | return rkclk_pll_get_rate(priv->cru, clk->id); |
| 290 | default: |
| 291 | return -ENOENT; |
| 292 | } |
huang lin | 01aa702 | 2015-11-17 14:20:16 +0800 | [diff] [blame] | 293 | } |
| 294 | |
Stephen Warren | a962243 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 295 | static ulong rk3036_clk_set_rate(struct clk *clk, ulong rate) |
huang lin | 01aa702 | 2015-11-17 14:20:16 +0800 | [diff] [blame] | 296 | { |
Stephen Warren | a962243 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 297 | struct rk3036_clk_priv *priv = dev_get_priv(clk->dev); |
| 298 | ulong new_rate, gclk_rate; |
huang lin | 01aa702 | 2015-11-17 14:20:16 +0800 | [diff] [blame] | 299 | |
Stephen Warren | a962243 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 300 | gclk_rate = rkclk_pll_get_rate(priv->cru, CLK_GENERAL); |
| 301 | switch (clk->id) { |
| 302 | case 0 ... 63: |
| 303 | return 0; |
Simon Glass | 8d32f4b | 2016-01-21 19:43:38 -0700 | [diff] [blame] | 304 | case HCLK_EMMC: |
Xu Ziyuan | 6682b9b | 2017-04-16 17:44:43 +0800 | [diff] [blame] | 305 | case SCLK_EMMC: |
Stephen Warren | a962243 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 306 | new_rate = rockchip_mmc_set_clk(priv->cru, gclk_rate, |
| 307 | clk->id, rate); |
huang lin | 01aa702 | 2015-11-17 14:20:16 +0800 | [diff] [blame] | 308 | break; |
| 309 | default: |
| 310 | return -ENOENT; |
| 311 | } |
| 312 | |
| 313 | return new_rate; |
| 314 | } |
| 315 | |
| 316 | static struct clk_ops rk3036_clk_ops = { |
| 317 | .get_rate = rk3036_clk_get_rate, |
| 318 | .set_rate = rk3036_clk_set_rate, |
huang lin | 01aa702 | 2015-11-17 14:20:16 +0800 | [diff] [blame] | 319 | }; |
| 320 | |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 321 | static int rk3036_clk_of_to_plat(struct udevice *dev) |
huang lin | 01aa702 | 2015-11-17 14:20:16 +0800 | [diff] [blame] | 322 | { |
huang lin | 01aa702 | 2015-11-17 14:20:16 +0800 | [diff] [blame] | 323 | struct rk3036_clk_priv *priv = dev_get_priv(dev); |
| 324 | |
Kever Yang | d7d162c | 2018-02-11 11:53:05 +0800 | [diff] [blame] | 325 | priv->cru = dev_read_addr_ptr(dev); |
Kever Yang | 1e05ff1 | 2018-04-24 11:27:06 +0800 | [diff] [blame] | 326 | |
| 327 | return 0; |
| 328 | } |
| 329 | |
| 330 | static int rk3036_clk_probe(struct udevice *dev) |
| 331 | { |
| 332 | struct rk3036_clk_priv *priv = dev_get_priv(dev); |
| 333 | |
huang lin | 01aa702 | 2015-11-17 14:20:16 +0800 | [diff] [blame] | 334 | rkclk_init(priv->cru); |
| 335 | |
| 336 | return 0; |
| 337 | } |
| 338 | |
huang lin | 01aa702 | 2015-11-17 14:20:16 +0800 | [diff] [blame] | 339 | static int rk3036_clk_bind(struct udevice *dev) |
| 340 | { |
Stephen Warren | a962243 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 341 | int ret; |
Kever Yang | 4fbb6c2 | 2017-11-03 15:16:13 +0800 | [diff] [blame] | 342 | struct udevice *sys_child; |
| 343 | struct sysreset_reg *priv; |
huang lin | 01aa702 | 2015-11-17 14:20:16 +0800 | [diff] [blame] | 344 | |
| 345 | /* 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] | 346 | ret = device_bind_driver(dev, "rockchip_sysreset", "sysreset", |
| 347 | &sys_child); |
| 348 | if (ret) { |
| 349 | debug("Warning: No sysreset driver: ret=%d\n", ret); |
| 350 | } else { |
| 351 | priv = malloc(sizeof(struct sysreset_reg)); |
| 352 | priv->glb_srst_fst_value = offsetof(struct rk3036_cru, |
| 353 | cru_glb_srst_fst_value); |
| 354 | priv->glb_srst_snd_value = offsetof(struct rk3036_cru, |
| 355 | cru_glb_srst_snd_value); |
Simon Glass | 9558862 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 356 | dev_set_priv(sys_child, priv); |
Kever Yang | 4fbb6c2 | 2017-11-03 15:16:13 +0800 | [diff] [blame] | 357 | } |
huang lin | 01aa702 | 2015-11-17 14:20:16 +0800 | [diff] [blame] | 358 | |
Heiko Stuebner | 416f8d3 | 2019-11-09 00:06:30 +0100 | [diff] [blame] | 359 | #if CONFIG_IS_ENABLED(RESET_ROCKCHIP) |
Elaine Zhang | 432976f | 2017-12-19 18:22:38 +0800 | [diff] [blame] | 360 | ret = offsetof(struct rk3036_cru, cru_softrst_con[0]); |
| 361 | ret = rockchip_reset_bind(dev, ret, 9); |
| 362 | if (ret) |
Eugen Hristev | f179826 | 2023-04-11 10:17:56 +0300 | [diff] [blame] | 363 | debug("Warning: software reset driver bind failed\n"); |
Elaine Zhang | 432976f | 2017-12-19 18:22:38 +0800 | [diff] [blame] | 364 | #endif |
| 365 | |
huang lin | 01aa702 | 2015-11-17 14:20:16 +0800 | [diff] [blame] | 366 | return 0; |
| 367 | } |
| 368 | |
| 369 | static const struct udevice_id rk3036_clk_ids[] = { |
| 370 | { .compatible = "rockchip,rk3036-cru" }, |
| 371 | { } |
| 372 | }; |
| 373 | |
Simon Glass | 3814f0e | 2016-10-01 20:04:50 -0600 | [diff] [blame] | 374 | U_BOOT_DRIVER(rockchip_rk3036_cru) = { |
huang lin | 01aa702 | 2015-11-17 14:20:16 +0800 | [diff] [blame] | 375 | .name = "clk_rk3036", |
| 376 | .id = UCLASS_CLK, |
| 377 | .of_match = rk3036_clk_ids, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 378 | .priv_auto = sizeof(struct rk3036_clk_priv), |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 379 | .of_to_plat = rk3036_clk_of_to_plat, |
huang lin | 01aa702 | 2015-11-17 14:20:16 +0800 | [diff] [blame] | 380 | .ops = &rk3036_clk_ops, |
| 381 | .bind = rk3036_clk_bind, |
| 382 | .probe = rk3036_clk_probe, |
| 383 | }; |