Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Ley Foon Tan | ca40f29 | 2017-04-26 02:44:39 +0800 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2016-2017 Intel Corporation |
Ley Foon Tan | ca40f29 | 2017-04-26 02:44:39 +0800 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #include <common.h> |
| 7 | #include <fdtdec.h> |
Simon Glass | 9bc1564 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 8 | #include <malloc.h> |
Ley Foon Tan | ca40f29 | 2017-04-26 02:44:39 +0800 | [diff] [blame] | 9 | #include <asm/io.h> |
Eugeniy Paltsev | 7473932 | 2017-12-28 15:09:02 +0300 | [diff] [blame] | 10 | #include <dm.h> |
Marek Vasut | e1dcd62 | 2018-07-30 15:56:19 +0200 | [diff] [blame] | 11 | #include <clk.h> |
| 12 | #include <dm/device-internal.h> |
Ley Foon Tan | ca40f29 | 2017-04-26 02:44:39 +0800 | [diff] [blame] | 13 | #include <asm/arch/clock_manager.h> |
Simon Glass | dbd7954 | 2020-05-10 11:40:11 -0600 | [diff] [blame] | 14 | #include <linux/delay.h> |
Ley Foon Tan | ca40f29 | 2017-04-26 02:44:39 +0800 | [diff] [blame] | 15 | |
Marek Vasut | 8fdb419 | 2018-08-18 19:11:52 +0200 | [diff] [blame] | 16 | #ifdef CONFIG_SPL_BUILD |
Marek Vasut | ec472e0 | 2018-05-12 00:09:21 +0200 | [diff] [blame] | 17 | |
Ley Foon Tan | ca40f29 | 2017-04-26 02:44:39 +0800 | [diff] [blame] | 18 | static u32 eosc1_hz; |
| 19 | static u32 cb_intosc_hz; |
| 20 | static u32 f2s_free_hz; |
Ley Foon Tan | ca40f29 | 2017-04-26 02:44:39 +0800 | [diff] [blame] | 21 | |
| 22 | struct mainpll_cfg { |
| 23 | u32 vco0_psrc; |
| 24 | u32 vco1_denom; |
| 25 | u32 vco1_numer; |
| 26 | u32 mpuclk; |
| 27 | u32 mpuclk_cnt; |
| 28 | u32 mpuclk_src; |
| 29 | u32 nocclk; |
| 30 | u32 nocclk_cnt; |
| 31 | u32 nocclk_src; |
| 32 | u32 cntr2clk_cnt; |
| 33 | u32 cntr3clk_cnt; |
| 34 | u32 cntr4clk_cnt; |
| 35 | u32 cntr5clk_cnt; |
| 36 | u32 cntr6clk_cnt; |
| 37 | u32 cntr7clk_cnt; |
| 38 | u32 cntr7clk_src; |
| 39 | u32 cntr8clk_cnt; |
| 40 | u32 cntr9clk_cnt; |
| 41 | u32 cntr9clk_src; |
| 42 | u32 cntr15clk_cnt; |
| 43 | u32 nocdiv_l4mainclk; |
| 44 | u32 nocdiv_l4mpclk; |
| 45 | u32 nocdiv_l4spclk; |
| 46 | u32 nocdiv_csatclk; |
| 47 | u32 nocdiv_cstraceclk; |
| 48 | u32 nocdiv_cspdbclk; |
| 49 | }; |
| 50 | |
| 51 | struct perpll_cfg { |
| 52 | u32 vco0_psrc; |
| 53 | u32 vco1_denom; |
| 54 | u32 vco1_numer; |
| 55 | u32 cntr2clk_cnt; |
| 56 | u32 cntr2clk_src; |
| 57 | u32 cntr3clk_cnt; |
| 58 | u32 cntr3clk_src; |
| 59 | u32 cntr4clk_cnt; |
| 60 | u32 cntr4clk_src; |
| 61 | u32 cntr5clk_cnt; |
| 62 | u32 cntr5clk_src; |
| 63 | u32 cntr6clk_cnt; |
| 64 | u32 cntr6clk_src; |
| 65 | u32 cntr7clk_cnt; |
| 66 | u32 cntr8clk_cnt; |
| 67 | u32 cntr8clk_src; |
| 68 | u32 cntr9clk_cnt; |
Marek Vasut | ec472e0 | 2018-05-12 00:09:21 +0200 | [diff] [blame] | 69 | u32 cntr9clk_src; |
Ley Foon Tan | ca40f29 | 2017-04-26 02:44:39 +0800 | [diff] [blame] | 70 | u32 emacctl_emac0sel; |
| 71 | u32 emacctl_emac1sel; |
| 72 | u32 emacctl_emac2sel; |
| 73 | u32 gpiodiv_gpiodbclk; |
| 74 | }; |
| 75 | |
Marek Vasut | ec472e0 | 2018-05-12 00:09:21 +0200 | [diff] [blame] | 76 | struct strtou32 { |
| 77 | const char *str; |
| 78 | const u32 val; |
Ley Foon Tan | ca40f29 | 2017-04-26 02:44:39 +0800 | [diff] [blame] | 79 | }; |
| 80 | |
Marek Vasut | ec472e0 | 2018-05-12 00:09:21 +0200 | [diff] [blame] | 81 | static const struct strtou32 mainpll_cfg_tab[] = { |
| 82 | { "vco0-psrc", offsetof(struct mainpll_cfg, vco0_psrc) }, |
| 83 | { "vco1-denom", offsetof(struct mainpll_cfg, vco1_denom) }, |
| 84 | { "vco1-numer", offsetof(struct mainpll_cfg, vco1_numer) }, |
| 85 | { "mpuclk-cnt", offsetof(struct mainpll_cfg, mpuclk_cnt) }, |
| 86 | { "mpuclk-src", offsetof(struct mainpll_cfg, mpuclk_src) }, |
| 87 | { "nocclk-cnt", offsetof(struct mainpll_cfg, nocclk_cnt) }, |
| 88 | { "nocclk-src", offsetof(struct mainpll_cfg, nocclk_src) }, |
| 89 | { "cntr2clk-cnt", offsetof(struct mainpll_cfg, cntr2clk_cnt) }, |
| 90 | { "cntr3clk-cnt", offsetof(struct mainpll_cfg, cntr3clk_cnt) }, |
| 91 | { "cntr4clk-cnt", offsetof(struct mainpll_cfg, cntr4clk_cnt) }, |
| 92 | { "cntr5clk-cnt", offsetof(struct mainpll_cfg, cntr5clk_cnt) }, |
| 93 | { "cntr6clk-cnt", offsetof(struct mainpll_cfg, cntr6clk_cnt) }, |
| 94 | { "cntr7clk-cnt", offsetof(struct mainpll_cfg, cntr7clk_cnt) }, |
| 95 | { "cntr7clk-src", offsetof(struct mainpll_cfg, cntr7clk_src) }, |
| 96 | { "cntr8clk-cnt", offsetof(struct mainpll_cfg, cntr8clk_cnt) }, |
| 97 | { "cntr9clk-cnt", offsetof(struct mainpll_cfg, cntr9clk_cnt) }, |
| 98 | { "cntr9clk-src", offsetof(struct mainpll_cfg, cntr9clk_src) }, |
| 99 | { "cntr15clk-cnt", offsetof(struct mainpll_cfg, cntr15clk_cnt) }, |
| 100 | { "nocdiv-l4mainclk", offsetof(struct mainpll_cfg, nocdiv_l4mainclk) }, |
| 101 | { "nocdiv-l4mpclk", offsetof(struct mainpll_cfg, nocdiv_l4mpclk) }, |
| 102 | { "nocdiv-l4spclk", offsetof(struct mainpll_cfg, nocdiv_l4spclk) }, |
| 103 | { "nocdiv-csatclk", offsetof(struct mainpll_cfg, nocdiv_csatclk) }, |
| 104 | { "nocdiv-cstraceclk", offsetof(struct mainpll_cfg, nocdiv_cstraceclk) }, |
| 105 | { "nocdiv-cspdbgclk", offsetof(struct mainpll_cfg, nocdiv_cspdbclk) }, |
| 106 | }; |
Ley Foon Tan | ca40f29 | 2017-04-26 02:44:39 +0800 | [diff] [blame] | 107 | |
Marek Vasut | ec472e0 | 2018-05-12 00:09:21 +0200 | [diff] [blame] | 108 | static const struct strtou32 perpll_cfg_tab[] = { |
| 109 | { "vco0-psrc", offsetof(struct perpll_cfg, vco0_psrc) }, |
| 110 | { "vco1-denom", offsetof(struct perpll_cfg, vco1_denom) }, |
| 111 | { "vco1-numer", offsetof(struct perpll_cfg, vco1_numer) }, |
| 112 | { "cntr2clk-cnt", offsetof(struct perpll_cfg, cntr2clk_cnt) }, |
| 113 | { "cntr2clk-src", offsetof(struct perpll_cfg, cntr2clk_src) }, |
| 114 | { "cntr3clk-cnt", offsetof(struct perpll_cfg, cntr3clk_cnt) }, |
| 115 | { "cntr3clk-src", offsetof(struct perpll_cfg, cntr3clk_src) }, |
| 116 | { "cntr4clk-cnt", offsetof(struct perpll_cfg, cntr4clk_cnt) }, |
| 117 | { "cntr4clk-src", offsetof(struct perpll_cfg, cntr4clk_src) }, |
| 118 | { "cntr5clk-cnt", offsetof(struct perpll_cfg, cntr5clk_cnt) }, |
| 119 | { "cntr5clk-src", offsetof(struct perpll_cfg, cntr5clk_src) }, |
| 120 | { "cntr6clk-cnt", offsetof(struct perpll_cfg, cntr6clk_cnt) }, |
| 121 | { "cntr6clk-src", offsetof(struct perpll_cfg, cntr6clk_src) }, |
| 122 | { "cntr7clk-cnt", offsetof(struct perpll_cfg, cntr7clk_cnt) }, |
| 123 | { "cntr8clk-cnt", offsetof(struct perpll_cfg, cntr8clk_cnt) }, |
| 124 | { "cntr8clk-src", offsetof(struct perpll_cfg, cntr8clk_src) }, |
| 125 | { "cntr9clk-cnt", offsetof(struct perpll_cfg, cntr9clk_cnt) }, |
| 126 | { "emacctl-emac0sel", offsetof(struct perpll_cfg, emacctl_emac0sel) }, |
| 127 | { "emacctl-emac1sel", offsetof(struct perpll_cfg, emacctl_emac1sel) }, |
| 128 | { "emacctl-emac2sel", offsetof(struct perpll_cfg, emacctl_emac2sel) }, |
| 129 | { "gpiodiv-gpiodbclk", offsetof(struct perpll_cfg, gpiodiv_gpiodbclk) }, |
| 130 | }; |
| 131 | |
| 132 | static const struct strtou32 alteragrp_cfg_tab[] = { |
| 133 | { "nocclk", offsetof(struct mainpll_cfg, nocclk) }, |
| 134 | { "mpuclk", offsetof(struct mainpll_cfg, mpuclk) }, |
| 135 | }; |
| 136 | |
| 137 | struct strtopu32 { |
| 138 | const char *str; |
| 139 | u32 *p; |
| 140 | }; |
| 141 | |
| 142 | const struct strtopu32 dt_to_val[] = { |
Marek Vasut | e1dcd62 | 2018-07-30 15:56:19 +0200 | [diff] [blame] | 143 | { "altera_arria10_hps_eosc1", &eosc1_hz }, |
| 144 | { "altera_arria10_hps_cb_intosc_ls", &cb_intosc_hz }, |
| 145 | { "altera_arria10_hps_f2h_free", &f2s_free_hz }, |
Marek Vasut | ec472e0 | 2018-05-12 00:09:21 +0200 | [diff] [blame] | 146 | }; |
| 147 | |
| 148 | static int of_to_struct(const void *blob, int node, const struct strtou32 *cfg_tab, |
| 149 | int cfg_tab_len, void *cfg) |
Ley Foon Tan | ca40f29 | 2017-04-26 02:44:39 +0800 | [diff] [blame] | 150 | { |
Marek Vasut | ec472e0 | 2018-05-12 00:09:21 +0200 | [diff] [blame] | 151 | int i; |
| 152 | u32 val; |
| 153 | |
| 154 | for (i = 0; i < cfg_tab_len; i++) { |
| 155 | if (fdtdec_get_int_array(blob, node, cfg_tab[i].str, &val, 1)) { |
| 156 | /* could not find required property */ |
| 157 | return -EINVAL; |
| 158 | } |
| 159 | *(u32 *)(cfg + cfg_tab[i].val) = val; |
Ley Foon Tan | ca40f29 | 2017-04-26 02:44:39 +0800 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | return 0; |
| 163 | } |
| 164 | |
Marek Vasut | e1dcd62 | 2018-07-30 15:56:19 +0200 | [diff] [blame] | 165 | static int of_get_input_clks(const void *blob) |
Ley Foon Tan | ca40f29 | 2017-04-26 02:44:39 +0800 | [diff] [blame] | 166 | { |
Marek Vasut | e1dcd62 | 2018-07-30 15:56:19 +0200 | [diff] [blame] | 167 | struct udevice *dev; |
| 168 | struct clk clk; |
| 169 | int i, ret; |
Ley Foon Tan | ca40f29 | 2017-04-26 02:44:39 +0800 | [diff] [blame] | 170 | |
Marek Vasut | ec472e0 | 2018-05-12 00:09:21 +0200 | [diff] [blame] | 171 | for (i = 0; i < ARRAY_SIZE(dt_to_val); i++) { |
Marek Vasut | e1dcd62 | 2018-07-30 15:56:19 +0200 | [diff] [blame] | 172 | memset(&clk, 0, sizeof(clk)); |
Marek Vasut | ec472e0 | 2018-05-12 00:09:21 +0200 | [diff] [blame] | 173 | |
Marek Vasut | e1dcd62 | 2018-07-30 15:56:19 +0200 | [diff] [blame] | 174 | ret = uclass_get_device_by_name(UCLASS_CLK, dt_to_val[i].str, |
| 175 | &dev); |
| 176 | if (ret) |
| 177 | return ret; |
Marek Vasut | ec472e0 | 2018-05-12 00:09:21 +0200 | [diff] [blame] | 178 | |
Marek Vasut | e1dcd62 | 2018-07-30 15:56:19 +0200 | [diff] [blame] | 179 | ret = clk_request(dev, &clk); |
| 180 | if (ret) |
| 181 | return ret; |
| 182 | |
| 183 | *dt_to_val[i].p = clk_get_rate(&clk); |
Marek Vasut | ec472e0 | 2018-05-12 00:09:21 +0200 | [diff] [blame] | 184 | } |
Marek Vasut | e1dcd62 | 2018-07-30 15:56:19 +0200 | [diff] [blame] | 185 | |
| 186 | return 0; |
Ley Foon Tan | ca40f29 | 2017-04-26 02:44:39 +0800 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | static int of_get_clk_cfg(const void *blob, struct mainpll_cfg *main_cfg, |
Marek Vasut | ec472e0 | 2018-05-12 00:09:21 +0200 | [diff] [blame] | 190 | struct perpll_cfg *per_cfg) |
Ley Foon Tan | ca40f29 | 2017-04-26 02:44:39 +0800 | [diff] [blame] | 191 | { |
Marek Vasut | e1dcd62 | 2018-07-30 15:56:19 +0200 | [diff] [blame] | 192 | int ret, node, child, len; |
Ley Foon Tan | ca40f29 | 2017-04-26 02:44:39 +0800 | [diff] [blame] | 193 | const char *node_name; |
| 194 | |
Marek Vasut | e1dcd62 | 2018-07-30 15:56:19 +0200 | [diff] [blame] | 195 | ret = of_get_input_clks(blob); |
| 196 | if (ret) |
| 197 | return ret; |
Marek Vasut | ec472e0 | 2018-05-12 00:09:21 +0200 | [diff] [blame] | 198 | |
| 199 | node = fdtdec_next_compatible(blob, 0, COMPAT_ALTERA_SOCFPGA_CLK_INIT); |
| 200 | |
Ley Foon Tan | ca40f29 | 2017-04-26 02:44:39 +0800 | [diff] [blame] | 201 | if (node < 0) |
| 202 | return -EINVAL; |
| 203 | |
| 204 | child = fdt_first_subnode(blob, node); |
Ley Foon Tan | ca40f29 | 2017-04-26 02:44:39 +0800 | [diff] [blame] | 205 | |
Ley Foon Tan | ca40f29 | 2017-04-26 02:44:39 +0800 | [diff] [blame] | 206 | if (child < 0) |
| 207 | return -EINVAL; |
| 208 | |
| 209 | node_name = fdt_get_name(blob, child, &len); |
| 210 | |
| 211 | while (node_name) { |
Marek Vasut | ec472e0 | 2018-05-12 00:09:21 +0200 | [diff] [blame] | 212 | if (!strcmp(node_name, "mainpll")) { |
| 213 | if (of_to_struct(blob, child, mainpll_cfg_tab, |
| 214 | ARRAY_SIZE(mainpll_cfg_tab), main_cfg)) |
Ley Foon Tan | ca40f29 | 2017-04-26 02:44:39 +0800 | [diff] [blame] | 215 | return -EINVAL; |
Marek Vasut | ec472e0 | 2018-05-12 00:09:21 +0200 | [diff] [blame] | 216 | } else if (!strcmp(node_name, "perpll")) { |
| 217 | if (of_to_struct(blob, child, perpll_cfg_tab, |
| 218 | ARRAY_SIZE(perpll_cfg_tab), per_cfg)) |
Ley Foon Tan | ca40f29 | 2017-04-26 02:44:39 +0800 | [diff] [blame] | 219 | return -EINVAL; |
Marek Vasut | ec472e0 | 2018-05-12 00:09:21 +0200 | [diff] [blame] | 220 | } else if (!strcmp(node_name, "alteragrp")) { |
| 221 | if (of_to_struct(blob, child, alteragrp_cfg_tab, |
| 222 | ARRAY_SIZE(alteragrp_cfg_tab), main_cfg)) |
Ley Foon Tan | ca40f29 | 2017-04-26 02:44:39 +0800 | [diff] [blame] | 223 | return -EINVAL; |
Ley Foon Tan | ca40f29 | 2017-04-26 02:44:39 +0800 | [diff] [blame] | 224 | } |
| 225 | child = fdt_next_subnode(blob, child); |
| 226 | |
| 227 | if (child < 0) |
| 228 | break; |
| 229 | |
| 230 | node_name = fdt_get_name(blob, child, &len); |
| 231 | } |
| 232 | |
| 233 | return 0; |
| 234 | } |
| 235 | |
| 236 | /* calculate the intended main VCO frequency based on handoff */ |
| 237 | static unsigned int cm_calc_handoff_main_vco_clk_hz |
| 238 | (struct mainpll_cfg *main_cfg) |
| 239 | { |
| 240 | unsigned int clk_hz; |
| 241 | |
| 242 | /* Check main VCO clock source: eosc, intosc or f2s? */ |
| 243 | switch (main_cfg->vco0_psrc) { |
| 244 | case CLKMGR_MAINPLL_VCO0_PSRC_EOSC: |
| 245 | clk_hz = eosc1_hz; |
| 246 | break; |
| 247 | case CLKMGR_MAINPLL_VCO0_PSRC_E_INTOSC: |
| 248 | clk_hz = cb_intosc_hz; |
| 249 | break; |
| 250 | case CLKMGR_MAINPLL_VCO0_PSRC_F2S: |
| 251 | clk_hz = f2s_free_hz; |
| 252 | break; |
| 253 | default: |
| 254 | return 0; |
| 255 | } |
| 256 | |
| 257 | /* calculate the VCO frequency */ |
| 258 | clk_hz /= 1 + main_cfg->vco1_denom; |
| 259 | clk_hz *= 1 + main_cfg->vco1_numer; |
| 260 | |
| 261 | return clk_hz; |
| 262 | } |
| 263 | |
| 264 | /* calculate the intended periph VCO frequency based on handoff */ |
| 265 | static unsigned int cm_calc_handoff_periph_vco_clk_hz( |
| 266 | struct mainpll_cfg *main_cfg, struct perpll_cfg *per_cfg) |
| 267 | { |
| 268 | unsigned int clk_hz; |
| 269 | |
| 270 | /* Check periph VCO clock source: eosc, intosc, f2s or mainpll? */ |
| 271 | switch (per_cfg->vco0_psrc) { |
| 272 | case CLKMGR_PERPLL_VCO0_PSRC_EOSC: |
| 273 | clk_hz = eosc1_hz; |
| 274 | break; |
| 275 | case CLKMGR_PERPLL_VCO0_PSRC_E_INTOSC: |
| 276 | clk_hz = cb_intosc_hz; |
| 277 | break; |
| 278 | case CLKMGR_PERPLL_VCO0_PSRC_F2S: |
| 279 | clk_hz = f2s_free_hz; |
| 280 | break; |
| 281 | case CLKMGR_PERPLL_VCO0_PSRC_MAIN: |
| 282 | clk_hz = cm_calc_handoff_main_vco_clk_hz(main_cfg); |
| 283 | clk_hz /= main_cfg->cntr15clk_cnt; |
| 284 | break; |
| 285 | default: |
| 286 | return 0; |
| 287 | } |
| 288 | |
| 289 | /* calculate the VCO frequency */ |
| 290 | clk_hz /= 1 + per_cfg->vco1_denom; |
| 291 | clk_hz *= 1 + per_cfg->vco1_numer; |
| 292 | |
| 293 | return clk_hz; |
| 294 | } |
| 295 | |
| 296 | /* calculate the intended MPU clock frequency based on handoff */ |
| 297 | static unsigned int cm_calc_handoff_mpu_clk_hz(struct mainpll_cfg *main_cfg, |
| 298 | struct perpll_cfg *per_cfg) |
| 299 | { |
| 300 | unsigned int clk_hz; |
| 301 | |
| 302 | /* Check MPU clock source: main, periph, osc1, intosc or f2s? */ |
| 303 | switch (main_cfg->mpuclk_src) { |
| 304 | case CLKMGR_MAINPLL_MPUCLK_SRC_MAIN: |
| 305 | clk_hz = cm_calc_handoff_main_vco_clk_hz(main_cfg); |
| 306 | clk_hz /= (main_cfg->mpuclk & CLKMGR_MAINPLL_MPUCLK_CNT_MSK) |
| 307 | + 1; |
| 308 | break; |
| 309 | case CLKMGR_MAINPLL_MPUCLK_SRC_PERI: |
| 310 | clk_hz = cm_calc_handoff_periph_vco_clk_hz(main_cfg, per_cfg); |
| 311 | clk_hz /= ((main_cfg->mpuclk >> |
| 312 | CLKMGR_MAINPLL_MPUCLK_PERICNT_LSB) & |
| 313 | CLKMGR_MAINPLL_MPUCLK_CNT_MSK) + 1; |
| 314 | break; |
| 315 | case CLKMGR_MAINPLL_MPUCLK_SRC_OSC1: |
| 316 | clk_hz = eosc1_hz; |
| 317 | break; |
| 318 | case CLKMGR_MAINPLL_MPUCLK_SRC_INTOSC: |
| 319 | clk_hz = cb_intosc_hz; |
| 320 | break; |
| 321 | case CLKMGR_MAINPLL_MPUCLK_SRC_FPGA: |
| 322 | clk_hz = f2s_free_hz; |
| 323 | break; |
| 324 | default: |
| 325 | return 0; |
| 326 | } |
| 327 | |
| 328 | clk_hz /= main_cfg->mpuclk_cnt + 1; |
| 329 | return clk_hz; |
| 330 | } |
| 331 | |
| 332 | /* calculate the intended NOC clock frequency based on handoff */ |
| 333 | static unsigned int cm_calc_handoff_noc_clk_hz(struct mainpll_cfg *main_cfg, |
| 334 | struct perpll_cfg *per_cfg) |
| 335 | { |
| 336 | unsigned int clk_hz; |
| 337 | |
| 338 | /* Check MPU clock source: main, periph, osc1, intosc or f2s? */ |
| 339 | switch (main_cfg->nocclk_src) { |
| 340 | case CLKMGR_MAINPLL_NOCCLK_SRC_MAIN: |
| 341 | clk_hz = cm_calc_handoff_main_vco_clk_hz(main_cfg); |
| 342 | clk_hz /= (main_cfg->nocclk & CLKMGR_MAINPLL_NOCCLK_CNT_MSK) |
| 343 | + 1; |
| 344 | break; |
| 345 | case CLKMGR_MAINPLL_NOCCLK_SRC_PERI: |
| 346 | clk_hz = cm_calc_handoff_periph_vco_clk_hz(main_cfg, per_cfg); |
| 347 | clk_hz /= ((main_cfg->nocclk >> |
| 348 | CLKMGR_MAINPLL_NOCCLK_PERICNT_LSB) & |
| 349 | CLKMGR_MAINPLL_NOCCLK_CNT_MSK) + 1; |
| 350 | break; |
| 351 | case CLKMGR_MAINPLL_NOCCLK_SRC_OSC1: |
| 352 | clk_hz = eosc1_hz; |
| 353 | break; |
| 354 | case CLKMGR_MAINPLL_NOCCLK_SRC_INTOSC: |
| 355 | clk_hz = cb_intosc_hz; |
| 356 | break; |
| 357 | case CLKMGR_MAINPLL_NOCCLK_SRC_FPGA: |
| 358 | clk_hz = f2s_free_hz; |
| 359 | break; |
| 360 | default: |
| 361 | return 0; |
| 362 | } |
| 363 | |
| 364 | clk_hz /= main_cfg->nocclk_cnt + 1; |
| 365 | return clk_hz; |
| 366 | } |
| 367 | |
| 368 | /* return 1 if PLL ramp is required */ |
| 369 | static int cm_is_pll_ramp_required(int main0periph1, |
| 370 | struct mainpll_cfg *main_cfg, |
| 371 | struct perpll_cfg *per_cfg) |
| 372 | { |
| 373 | /* Check for main PLL */ |
| 374 | if (main0periph1 == 0) { |
| 375 | /* |
| 376 | * PLL ramp is not required if both MPU clock and NOC clock are |
| 377 | * not sourced from main PLL |
| 378 | */ |
| 379 | if (main_cfg->mpuclk_src != CLKMGR_MAINPLL_MPUCLK_SRC_MAIN && |
| 380 | main_cfg->nocclk_src != CLKMGR_MAINPLL_NOCCLK_SRC_MAIN) |
| 381 | return 0; |
| 382 | |
| 383 | /* |
| 384 | * PLL ramp is required if MPU clock is sourced from main PLL |
| 385 | * and MPU clock is over 900MHz (as advised by HW team) |
| 386 | */ |
| 387 | if (main_cfg->mpuclk_src == CLKMGR_MAINPLL_MPUCLK_SRC_MAIN && |
| 388 | (cm_calc_handoff_mpu_clk_hz(main_cfg, per_cfg) > |
| 389 | CLKMGR_PLL_RAMP_MPUCLK_THRESHOLD_HZ)) |
| 390 | return 1; |
| 391 | |
| 392 | /* |
| 393 | * PLL ramp is required if NOC clock is sourced from main PLL |
| 394 | * and NOC clock is over 300MHz (as advised by HW team) |
| 395 | */ |
| 396 | if (main_cfg->nocclk_src == CLKMGR_MAINPLL_NOCCLK_SRC_MAIN && |
| 397 | (cm_calc_handoff_noc_clk_hz(main_cfg, per_cfg) > |
| 398 | CLKMGR_PLL_RAMP_NOCCLK_THRESHOLD_HZ)) |
| 399 | return 2; |
| 400 | |
| 401 | } else if (main0periph1 == 1) { |
| 402 | /* |
| 403 | * PLL ramp is not required if both MPU clock and NOC clock are |
| 404 | * not sourced from periph PLL |
| 405 | */ |
| 406 | if (main_cfg->mpuclk_src != CLKMGR_MAINPLL_MPUCLK_SRC_PERI && |
| 407 | main_cfg->nocclk_src != CLKMGR_MAINPLL_NOCCLK_SRC_PERI) |
| 408 | return 0; |
| 409 | |
| 410 | /* |
| 411 | * PLL ramp is required if MPU clock are source from periph PLL |
| 412 | * and MPU clock is over 900MHz (as advised by HW team) |
| 413 | */ |
| 414 | if (main_cfg->mpuclk_src == CLKMGR_MAINPLL_MPUCLK_SRC_PERI && |
| 415 | (cm_calc_handoff_mpu_clk_hz(main_cfg, per_cfg) > |
| 416 | CLKMGR_PLL_RAMP_MPUCLK_THRESHOLD_HZ)) |
| 417 | return 1; |
| 418 | |
| 419 | /* |
| 420 | * PLL ramp is required if NOC clock are source from periph PLL |
| 421 | * and NOC clock is over 300MHz (as advised by HW team) |
| 422 | */ |
| 423 | if (main_cfg->nocclk_src == CLKMGR_MAINPLL_NOCCLK_SRC_PERI && |
| 424 | (cm_calc_handoff_noc_clk_hz(main_cfg, per_cfg) > |
| 425 | CLKMGR_PLL_RAMP_NOCCLK_THRESHOLD_HZ)) |
| 426 | return 2; |
| 427 | } |
| 428 | |
| 429 | return 0; |
| 430 | } |
| 431 | |
| 432 | static u32 cm_calculate_numer(struct mainpll_cfg *main_cfg, |
| 433 | struct perpll_cfg *per_cfg, |
| 434 | u32 safe_hz, u32 clk_hz) |
| 435 | { |
| 436 | u32 cnt; |
| 437 | u32 clk; |
| 438 | u32 shift; |
| 439 | u32 mask; |
| 440 | u32 denom; |
| 441 | |
| 442 | if (main_cfg->mpuclk_src == CLKMGR_MAINPLL_MPUCLK_SRC_MAIN) { |
| 443 | cnt = main_cfg->mpuclk_cnt; |
| 444 | clk = main_cfg->mpuclk; |
| 445 | shift = 0; |
| 446 | mask = CLKMGR_MAINPLL_MPUCLK_CNT_MSK; |
| 447 | denom = main_cfg->vco1_denom; |
| 448 | } else if (main_cfg->nocclk_src == CLKMGR_MAINPLL_NOCCLK_SRC_MAIN) { |
| 449 | cnt = main_cfg->nocclk_cnt; |
| 450 | clk = main_cfg->nocclk; |
| 451 | shift = 0; |
| 452 | mask = CLKMGR_MAINPLL_NOCCLK_CNT_MSK; |
| 453 | denom = main_cfg->vco1_denom; |
| 454 | } else if (main_cfg->mpuclk_src == CLKMGR_MAINPLL_MPUCLK_SRC_PERI) { |
| 455 | cnt = main_cfg->mpuclk_cnt; |
| 456 | clk = main_cfg->mpuclk; |
| 457 | shift = CLKMGR_MAINPLL_MPUCLK_PERICNT_LSB; |
| 458 | mask = CLKMGR_MAINPLL_MPUCLK_CNT_MSK; |
| 459 | denom = per_cfg->vco1_denom; |
| 460 | } else if (main_cfg->nocclk_src == CLKMGR_MAINPLL_NOCCLK_SRC_PERI) { |
| 461 | cnt = main_cfg->nocclk_cnt; |
| 462 | clk = main_cfg->nocclk; |
| 463 | shift = CLKMGR_MAINPLL_NOCCLK_PERICNT_LSB; |
| 464 | mask = CLKMGR_MAINPLL_NOCCLK_CNT_MSK; |
| 465 | denom = per_cfg->vco1_denom; |
| 466 | } else { |
| 467 | return 0; |
| 468 | } |
| 469 | |
| 470 | return (safe_hz / clk_hz) * (cnt + 1) * (((clk >> shift) & mask) + 1) * |
| 471 | (1 + denom) - 1; |
| 472 | } |
| 473 | |
| 474 | /* |
| 475 | * Calculate the new PLL numerator which is based on existing DTS hand off and |
| 476 | * intended safe frequency (safe_hz). Note that PLL ramp is only modifying the |
| 477 | * numerator while maintaining denominator as denominator will influence the |
| 478 | * jitter condition. Please refer A10 HPS TRM for the jitter guide. Note final |
| 479 | * value for numerator is minus with 1 to cater our register value |
| 480 | * representation. |
| 481 | */ |
| 482 | static unsigned int cm_calc_safe_pll_numer(int main0periph1, |
| 483 | struct mainpll_cfg *main_cfg, |
| 484 | struct perpll_cfg *per_cfg, |
| 485 | unsigned int safe_hz) |
| 486 | { |
| 487 | unsigned int clk_hz = 0; |
| 488 | |
| 489 | /* Check for main PLL */ |
| 490 | if (main0periph1 == 0) { |
| 491 | /* Check main VCO clock source: eosc, intosc or f2s? */ |
| 492 | switch (main_cfg->vco0_psrc) { |
| 493 | case CLKMGR_MAINPLL_VCO0_PSRC_EOSC: |
| 494 | clk_hz = eosc1_hz; |
| 495 | break; |
| 496 | case CLKMGR_MAINPLL_VCO0_PSRC_E_INTOSC: |
| 497 | clk_hz = cb_intosc_hz; |
| 498 | break; |
| 499 | case CLKMGR_MAINPLL_VCO0_PSRC_F2S: |
| 500 | clk_hz = f2s_free_hz; |
| 501 | break; |
| 502 | default: |
| 503 | return 0; |
| 504 | } |
| 505 | } else if (main0periph1 == 1) { |
| 506 | /* Check periph VCO clock source: eosc, intosc, f2s, mainpll */ |
| 507 | switch (per_cfg->vco0_psrc) { |
| 508 | case CLKMGR_PERPLL_VCO0_PSRC_EOSC: |
| 509 | clk_hz = eosc1_hz; |
| 510 | break; |
| 511 | case CLKMGR_PERPLL_VCO0_PSRC_E_INTOSC: |
| 512 | clk_hz = cb_intosc_hz; |
| 513 | break; |
| 514 | case CLKMGR_PERPLL_VCO0_PSRC_F2S: |
| 515 | clk_hz = f2s_free_hz; |
| 516 | break; |
| 517 | case CLKMGR_PERPLL_VCO0_PSRC_MAIN: |
| 518 | clk_hz = cm_calc_handoff_main_vco_clk_hz(main_cfg); |
| 519 | clk_hz /= main_cfg->cntr15clk_cnt; |
| 520 | break; |
| 521 | default: |
| 522 | return 0; |
| 523 | } |
| 524 | } else { |
| 525 | return 0; |
| 526 | } |
| 527 | |
| 528 | return cm_calculate_numer(main_cfg, per_cfg, safe_hz, clk_hz); |
| 529 | } |
| 530 | |
| 531 | /* ramping the main PLL to final value */ |
| 532 | static void cm_pll_ramp_main(struct mainpll_cfg *main_cfg, |
| 533 | struct perpll_cfg *per_cfg, |
| 534 | unsigned int pll_ramp_main_hz) |
| 535 | { |
| 536 | unsigned int clk_hz = 0, clk_incr_hz = 0, clk_final_hz = 0; |
| 537 | |
| 538 | /* find out the increment value */ |
| 539 | if (main_cfg->mpuclk_src == CLKMGR_MAINPLL_MPUCLK_SRC_MAIN) { |
| 540 | clk_incr_hz = CLKMGR_PLL_RAMP_MPUCLK_INCREMENT_HZ; |
| 541 | clk_final_hz = cm_calc_handoff_mpu_clk_hz(main_cfg, per_cfg); |
| 542 | } else if (main_cfg->nocclk_src == CLKMGR_MAINPLL_NOCCLK_SRC_MAIN) { |
| 543 | clk_incr_hz = CLKMGR_PLL_RAMP_NOCCLK_INCREMENT_HZ; |
| 544 | clk_final_hz = cm_calc_handoff_noc_clk_hz(main_cfg, per_cfg); |
| 545 | } |
| 546 | |
| 547 | /* execute the ramping here */ |
| 548 | for (clk_hz = pll_ramp_main_hz + clk_incr_hz; |
| 549 | clk_hz < clk_final_hz; clk_hz += clk_incr_hz) { |
| 550 | writel((main_cfg->vco1_denom << |
| 551 | CLKMGR_MAINPLL_VCO1_DENOM_LSB) | |
| 552 | cm_calc_safe_pll_numer(0, main_cfg, per_cfg, clk_hz), |
Ley Foon Tan | 2669591 | 2019-11-08 10:38:21 +0800 | [diff] [blame] | 553 | socfpga_get_clkmgr_addr() + CLKMGR_A10_MAINPLL_VCO1); |
Ley Foon Tan | ca40f29 | 2017-04-26 02:44:39 +0800 | [diff] [blame] | 554 | mdelay(1); |
| 555 | cm_wait_for_lock(LOCKED_MASK); |
| 556 | } |
| 557 | writel((main_cfg->vco1_denom << CLKMGR_MAINPLL_VCO1_DENOM_LSB) | |
Ley Foon Tan | 2669591 | 2019-11-08 10:38:21 +0800 | [diff] [blame] | 558 | main_cfg->vco1_numer, |
| 559 | socfpga_get_clkmgr_addr() + CLKMGR_A10_MAINPLL_VCO1); |
Ley Foon Tan | ca40f29 | 2017-04-26 02:44:39 +0800 | [diff] [blame] | 560 | mdelay(1); |
| 561 | cm_wait_for_lock(LOCKED_MASK); |
| 562 | } |
| 563 | |
| 564 | /* ramping the periph PLL to final value */ |
| 565 | static void cm_pll_ramp_periph(struct mainpll_cfg *main_cfg, |
| 566 | struct perpll_cfg *per_cfg, |
| 567 | unsigned int pll_ramp_periph_hz) |
| 568 | { |
| 569 | unsigned int clk_hz = 0, clk_incr_hz = 0, clk_final_hz = 0; |
| 570 | |
| 571 | /* find out the increment value */ |
| 572 | if (main_cfg->mpuclk_src == CLKMGR_MAINPLL_MPUCLK_SRC_PERI) { |
| 573 | clk_incr_hz = CLKMGR_PLL_RAMP_MPUCLK_INCREMENT_HZ; |
| 574 | clk_final_hz = cm_calc_handoff_mpu_clk_hz(main_cfg, per_cfg); |
| 575 | } else if (main_cfg->nocclk_src == CLKMGR_MAINPLL_NOCCLK_SRC_PERI) { |
| 576 | clk_incr_hz = CLKMGR_PLL_RAMP_NOCCLK_INCREMENT_HZ; |
| 577 | clk_final_hz = cm_calc_handoff_noc_clk_hz(main_cfg, per_cfg); |
| 578 | } |
| 579 | /* execute the ramping here */ |
| 580 | for (clk_hz = pll_ramp_periph_hz + clk_incr_hz; |
| 581 | clk_hz < clk_final_hz; clk_hz += clk_incr_hz) { |
Ley Foon Tan | 2669591 | 2019-11-08 10:38:21 +0800 | [diff] [blame] | 582 | writel((per_cfg->vco1_denom << |
| 583 | CLKMGR_PERPLL_VCO1_DENOM_LSB) | |
| 584 | cm_calc_safe_pll_numer(1, main_cfg, per_cfg, |
| 585 | clk_hz), |
| 586 | socfpga_get_clkmgr_addr() + |
| 587 | CLKMGR_A10_PERPLL_VCO1); |
Ley Foon Tan | ca40f29 | 2017-04-26 02:44:39 +0800 | [diff] [blame] | 588 | mdelay(1); |
| 589 | cm_wait_for_lock(LOCKED_MASK); |
| 590 | } |
| 591 | writel((per_cfg->vco1_denom << CLKMGR_PERPLL_VCO1_DENOM_LSB) | |
Ley Foon Tan | 2669591 | 2019-11-08 10:38:21 +0800 | [diff] [blame] | 592 | per_cfg->vco1_numer, |
| 593 | socfpga_get_clkmgr_addr() + CLKMGR_A10_PERPLL_VCO1); |
Ley Foon Tan | ca40f29 | 2017-04-26 02:44:39 +0800 | [diff] [blame] | 594 | mdelay(1); |
| 595 | cm_wait_for_lock(LOCKED_MASK); |
| 596 | } |
| 597 | |
| 598 | /* |
| 599 | * Setup clocks while making no assumptions of the |
| 600 | * previous state of the clocks. |
| 601 | * |
| 602 | * Start by being paranoid and gate all sw managed clocks |
| 603 | * |
| 604 | * Put all plls in bypass |
| 605 | * |
| 606 | * Put all plls VCO registers back to reset value (bgpwr dwn). |
| 607 | * |
| 608 | * Put peripheral and main pll src to reset value to avoid glitch. |
| 609 | * |
| 610 | * Delay 5 us. |
| 611 | * |
| 612 | * Deassert bg pwr dn and set numerator and denominator |
| 613 | * |
| 614 | * Start 7 us timer. |
| 615 | * |
| 616 | * set internal dividers |
| 617 | * |
| 618 | * Wait for 7 us timer. |
| 619 | * |
| 620 | * Enable plls |
| 621 | * |
| 622 | * Set external dividers while plls are locking |
| 623 | * |
| 624 | * Wait for pll lock |
| 625 | * |
| 626 | * Assert/deassert outreset all. |
| 627 | * |
| 628 | * Take all pll's out of bypass |
| 629 | * |
| 630 | * Clear safe mode |
| 631 | * |
| 632 | * set source main and peripheral clocks |
| 633 | * |
| 634 | * Ungate clocks |
| 635 | */ |
| 636 | |
| 637 | static int cm_full_cfg(struct mainpll_cfg *main_cfg, struct perpll_cfg *per_cfg) |
| 638 | { |
| 639 | unsigned int pll_ramp_main_hz = 0, pll_ramp_periph_hz = 0, |
| 640 | ramp_required; |
| 641 | |
| 642 | /* gate off all mainpll clock excpet HW managed clock */ |
| 643 | writel(CLKMGR_MAINPLL_EN_S2FUSER0CLKEN_SET_MSK | |
| 644 | CLKMGR_MAINPLL_EN_HMCPLLREFCLKEN_SET_MSK, |
Ley Foon Tan | 2669591 | 2019-11-08 10:38:21 +0800 | [diff] [blame] | 645 | socfpga_get_clkmgr_addr() + CLKMGR_A10_MAINPLL_ENR); |
Ley Foon Tan | ca40f29 | 2017-04-26 02:44:39 +0800 | [diff] [blame] | 646 | |
| 647 | /* now we can gate off the rest of the peripheral clocks */ |
Ley Foon Tan | 2669591 | 2019-11-08 10:38:21 +0800 | [diff] [blame] | 648 | writel(0, socfpga_get_clkmgr_addr() + CLKMGR_A10_PERPLL_EN); |
Ley Foon Tan | ca40f29 | 2017-04-26 02:44:39 +0800 | [diff] [blame] | 649 | |
| 650 | /* Put all plls in external bypass */ |
| 651 | writel(CLKMGR_MAINPLL_BYPASS_RESET, |
Ley Foon Tan | 2669591 | 2019-11-08 10:38:21 +0800 | [diff] [blame] | 652 | socfpga_get_clkmgr_addr() + CLKMGR_A10_MAINPLL_BYPASSS); |
Ley Foon Tan | ca40f29 | 2017-04-26 02:44:39 +0800 | [diff] [blame] | 653 | writel(CLKMGR_PERPLL_BYPASS_RESET, |
Ley Foon Tan | 2669591 | 2019-11-08 10:38:21 +0800 | [diff] [blame] | 654 | socfpga_get_clkmgr_addr() + CLKMGR_A10_PERPLL_BYPASSS); |
Ley Foon Tan | ca40f29 | 2017-04-26 02:44:39 +0800 | [diff] [blame] | 655 | |
| 656 | /* |
| 657 | * Put all plls VCO registers back to reset value. |
| 658 | * Some code might have messed with them. At same time set the |
| 659 | * desired clock source |
| 660 | */ |
| 661 | writel(CLKMGR_MAINPLL_VCO0_RESET | |
| 662 | CLKMGR_MAINPLL_VCO0_REGEXTSEL_SET_MSK | |
| 663 | (main_cfg->vco0_psrc << CLKMGR_MAINPLL_VCO0_PSRC_LSB), |
Ley Foon Tan | 2669591 | 2019-11-08 10:38:21 +0800 | [diff] [blame] | 664 | socfpga_get_clkmgr_addr() + CLKMGR_A10_MAINPLL_VCO0); |
Ley Foon Tan | ca40f29 | 2017-04-26 02:44:39 +0800 | [diff] [blame] | 665 | |
| 666 | writel(CLKMGR_PERPLL_VCO0_RESET | |
| 667 | CLKMGR_PERPLL_VCO0_REGEXTSEL_SET_MSK | |
| 668 | (per_cfg->vco0_psrc << CLKMGR_PERPLL_VCO0_PSRC_LSB), |
Ley Foon Tan | 2669591 | 2019-11-08 10:38:21 +0800 | [diff] [blame] | 669 | socfpga_get_clkmgr_addr() + CLKMGR_A10_PERPLL_VCO0); |
Ley Foon Tan | ca40f29 | 2017-04-26 02:44:39 +0800 | [diff] [blame] | 670 | |
Ley Foon Tan | 2669591 | 2019-11-08 10:38:21 +0800 | [diff] [blame] | 671 | writel(CLKMGR_MAINPLL_VCO1_RESET, |
| 672 | socfpga_get_clkmgr_addr() + CLKMGR_A10_MAINPLL_VCO1); |
| 673 | writel(CLKMGR_PERPLL_VCO1_RESET, |
| 674 | socfpga_get_clkmgr_addr() + CLKMGR_A10_PERPLL_VCO1); |
Ley Foon Tan | ca40f29 | 2017-04-26 02:44:39 +0800 | [diff] [blame] | 675 | |
| 676 | /* clear the interrupt register status register */ |
| 677 | writel(CLKMGR_CLKMGR_INTR_MAINPLLLOST_SET_MSK | |
| 678 | CLKMGR_CLKMGR_INTR_PERPLLLOST_SET_MSK | |
| 679 | CLKMGR_CLKMGR_INTR_MAINPLLRFSLIP_SET_MSK | |
| 680 | CLKMGR_CLKMGR_INTR_PERPLLRFSLIP_SET_MSK | |
| 681 | CLKMGR_CLKMGR_INTR_MAINPLLFBSLIP_SET_MSK | |
| 682 | CLKMGR_CLKMGR_INTR_PERPLLFBSLIP_SET_MSK | |
| 683 | CLKMGR_CLKMGR_INTR_MAINPLLACHIEVED_SET_MSK | |
| 684 | CLKMGR_CLKMGR_INTR_PERPLLACHIEVED_SET_MSK, |
Ley Foon Tan | 2669591 | 2019-11-08 10:38:21 +0800 | [diff] [blame] | 685 | socfpga_get_clkmgr_addr() + CLKMGR_A10_INTR); |
Ley Foon Tan | ca40f29 | 2017-04-26 02:44:39 +0800 | [diff] [blame] | 686 | |
| 687 | /* Program VCO Numerator and Denominator for main PLL */ |
| 688 | ramp_required = cm_is_pll_ramp_required(0, main_cfg, per_cfg); |
| 689 | if (ramp_required) { |
| 690 | /* set main PLL to safe starting threshold frequency */ |
| 691 | if (ramp_required == 1) |
| 692 | pll_ramp_main_hz = CLKMGR_PLL_RAMP_MPUCLK_THRESHOLD_HZ; |
| 693 | else if (ramp_required == 2) |
| 694 | pll_ramp_main_hz = CLKMGR_PLL_RAMP_NOCCLK_THRESHOLD_HZ; |
| 695 | |
Ley Foon Tan | 2669591 | 2019-11-08 10:38:21 +0800 | [diff] [blame] | 696 | writel((main_cfg->vco1_denom << |
| 697 | CLKMGR_MAINPLL_VCO1_DENOM_LSB) | |
Ley Foon Tan | ca40f29 | 2017-04-26 02:44:39 +0800 | [diff] [blame] | 698 | cm_calc_safe_pll_numer(0, main_cfg, per_cfg, |
| 699 | pll_ramp_main_hz), |
Ley Foon Tan | 2669591 | 2019-11-08 10:38:21 +0800 | [diff] [blame] | 700 | socfpga_get_clkmgr_addr() + CLKMGR_A10_MAINPLL_VCO1); |
Ley Foon Tan | ca40f29 | 2017-04-26 02:44:39 +0800 | [diff] [blame] | 701 | } else |
Ley Foon Tan | 2669591 | 2019-11-08 10:38:21 +0800 | [diff] [blame] | 702 | writel((main_cfg->vco1_denom << |
| 703 | CLKMGR_MAINPLL_VCO1_DENOM_LSB) | |
| 704 | main_cfg->vco1_numer, |
| 705 | socfpga_get_clkmgr_addr() + CLKMGR_A10_MAINPLL_VCO1); |
Ley Foon Tan | ca40f29 | 2017-04-26 02:44:39 +0800 | [diff] [blame] | 706 | |
| 707 | /* Program VCO Numerator and Denominator for periph PLL */ |
| 708 | ramp_required = cm_is_pll_ramp_required(1, main_cfg, per_cfg); |
| 709 | if (ramp_required) { |
| 710 | /* set periph PLL to safe starting threshold frequency */ |
| 711 | if (ramp_required == 1) |
| 712 | pll_ramp_periph_hz = |
| 713 | CLKMGR_PLL_RAMP_MPUCLK_THRESHOLD_HZ; |
| 714 | else if (ramp_required == 2) |
| 715 | pll_ramp_periph_hz = |
| 716 | CLKMGR_PLL_RAMP_NOCCLK_THRESHOLD_HZ; |
| 717 | |
Ley Foon Tan | 2669591 | 2019-11-08 10:38:21 +0800 | [diff] [blame] | 718 | writel((per_cfg->vco1_denom << |
| 719 | CLKMGR_PERPLL_VCO1_DENOM_LSB) | |
Ley Foon Tan | ca40f29 | 2017-04-26 02:44:39 +0800 | [diff] [blame] | 720 | cm_calc_safe_pll_numer(1, main_cfg, per_cfg, |
| 721 | pll_ramp_periph_hz), |
Ley Foon Tan | 2669591 | 2019-11-08 10:38:21 +0800 | [diff] [blame] | 722 | socfpga_get_clkmgr_addr() + CLKMGR_A10_PERPLL_VCO1); |
Ley Foon Tan | ca40f29 | 2017-04-26 02:44:39 +0800 | [diff] [blame] | 723 | } else |
Ley Foon Tan | 2669591 | 2019-11-08 10:38:21 +0800 | [diff] [blame] | 724 | writel((per_cfg->vco1_denom << |
| 725 | CLKMGR_PERPLL_VCO1_DENOM_LSB) | |
Ley Foon Tan | ca40f29 | 2017-04-26 02:44:39 +0800 | [diff] [blame] | 726 | per_cfg->vco1_numer, |
Ley Foon Tan | 2669591 | 2019-11-08 10:38:21 +0800 | [diff] [blame] | 727 | socfpga_get_clkmgr_addr() + CLKMGR_A10_PERPLL_VCO1); |
Ley Foon Tan | ca40f29 | 2017-04-26 02:44:39 +0800 | [diff] [blame] | 728 | |
| 729 | /* Wait for at least 5 us */ |
| 730 | udelay(5); |
| 731 | |
| 732 | /* Now deassert BGPWRDN and PWRDN */ |
Ley Foon Tan | 2669591 | 2019-11-08 10:38:21 +0800 | [diff] [blame] | 733 | clrbits_le32(socfpga_get_clkmgr_addr() + CLKMGR_A10_MAINPLL_VCO0, |
Ley Foon Tan | ca40f29 | 2017-04-26 02:44:39 +0800 | [diff] [blame] | 734 | CLKMGR_MAINPLL_VCO0_BGPWRDN_SET_MSK | |
| 735 | CLKMGR_MAINPLL_VCO0_PWRDN_SET_MSK); |
Ley Foon Tan | 2669591 | 2019-11-08 10:38:21 +0800 | [diff] [blame] | 736 | clrbits_le32(socfpga_get_clkmgr_addr() + CLKMGR_A10_PERPLL_VCO0, |
Ley Foon Tan | ca40f29 | 2017-04-26 02:44:39 +0800 | [diff] [blame] | 737 | CLKMGR_PERPLL_VCO0_BGPWRDN_SET_MSK | |
| 738 | CLKMGR_PERPLL_VCO0_PWRDN_SET_MSK); |
| 739 | |
| 740 | /* Wait for at least 7 us */ |
| 741 | udelay(7); |
| 742 | |
| 743 | /* enable the VCO and disable the external regulator to PLL */ |
Ley Foon Tan | 2669591 | 2019-11-08 10:38:21 +0800 | [diff] [blame] | 744 | writel((readl(socfpga_get_clkmgr_addr() + CLKMGR_A10_MAINPLL_VCO0) & |
Ley Foon Tan | ca40f29 | 2017-04-26 02:44:39 +0800 | [diff] [blame] | 745 | ~CLKMGR_MAINPLL_VCO0_REGEXTSEL_SET_MSK) | |
| 746 | CLKMGR_MAINPLL_VCO0_EN_SET_MSK, |
Ley Foon Tan | 2669591 | 2019-11-08 10:38:21 +0800 | [diff] [blame] | 747 | socfpga_get_clkmgr_addr() + CLKMGR_A10_MAINPLL_VCO0); |
| 748 | writel((readl(socfpga_get_clkmgr_addr() + CLKMGR_A10_PERPLL_VCO0) & |
Ley Foon Tan | ca40f29 | 2017-04-26 02:44:39 +0800 | [diff] [blame] | 749 | ~CLKMGR_PERPLL_VCO0_REGEXTSEL_SET_MSK) | |
| 750 | CLKMGR_PERPLL_VCO0_EN_SET_MSK, |
Ley Foon Tan | 2669591 | 2019-11-08 10:38:21 +0800 | [diff] [blame] | 751 | socfpga_get_clkmgr_addr() + CLKMGR_A10_PERPLL_VCO0); |
Ley Foon Tan | ca40f29 | 2017-04-26 02:44:39 +0800 | [diff] [blame] | 752 | |
| 753 | /* setup all the main PLL counter and clock source */ |
| 754 | writel(main_cfg->nocclk, |
Ley Foon Tan | 2669591 | 2019-11-08 10:38:21 +0800 | [diff] [blame] | 755 | socfpga_get_clkmgr_addr() + CLKMGR_A10_ALTR_NOCCLK); |
Ley Foon Tan | ca40f29 | 2017-04-26 02:44:39 +0800 | [diff] [blame] | 756 | writel(main_cfg->mpuclk, |
Ley Foon Tan | 2669591 | 2019-11-08 10:38:21 +0800 | [diff] [blame] | 757 | socfpga_get_clkmgr_addr() + CLKMGR_A10_ALTR_MPUCLK); |
Ley Foon Tan | ca40f29 | 2017-04-26 02:44:39 +0800 | [diff] [blame] | 758 | |
| 759 | /* main_emaca_clk divider */ |
Ley Foon Tan | 2669591 | 2019-11-08 10:38:21 +0800 | [diff] [blame] | 760 | writel(main_cfg->cntr2clk_cnt, |
| 761 | socfpga_get_clkmgr_addr() + CLKMGR_A10_MAINPLL_CNTR2CLK); |
Ley Foon Tan | ca40f29 | 2017-04-26 02:44:39 +0800 | [diff] [blame] | 762 | /* main_emacb_clk divider */ |
Ley Foon Tan | 2669591 | 2019-11-08 10:38:21 +0800 | [diff] [blame] | 763 | writel(main_cfg->cntr3clk_cnt, |
| 764 | socfpga_get_clkmgr_addr() + CLKMGR_A10_MAINPLL_CNTR3CLK); |
Ley Foon Tan | ca40f29 | 2017-04-26 02:44:39 +0800 | [diff] [blame] | 765 | /* main_emac_ptp_clk divider */ |
Ley Foon Tan | 2669591 | 2019-11-08 10:38:21 +0800 | [diff] [blame] | 766 | writel(main_cfg->cntr4clk_cnt, |
| 767 | socfpga_get_clkmgr_addr() + CLKMGR_A10_MAINPLL_CNTR4CLK); |
Ley Foon Tan | ca40f29 | 2017-04-26 02:44:39 +0800 | [diff] [blame] | 768 | /* main_gpio_db_clk divider */ |
Ley Foon Tan | 2669591 | 2019-11-08 10:38:21 +0800 | [diff] [blame] | 769 | writel(main_cfg->cntr5clk_cnt, |
| 770 | socfpga_get_clkmgr_addr() + CLKMGR_A10_MAINPLL_CNTR5CLK); |
Ley Foon Tan | ca40f29 | 2017-04-26 02:44:39 +0800 | [diff] [blame] | 771 | /* main_sdmmc_clk divider */ |
Ley Foon Tan | 2669591 | 2019-11-08 10:38:21 +0800 | [diff] [blame] | 772 | writel(main_cfg->cntr6clk_cnt, |
| 773 | socfpga_get_clkmgr_addr() + CLKMGR_A10_MAINPLL_CNTR6CLK); |
Ley Foon Tan | ca40f29 | 2017-04-26 02:44:39 +0800 | [diff] [blame] | 774 | /* main_s2f_user0_clk divider */ |
| 775 | writel(main_cfg->cntr7clk_cnt | |
| 776 | (main_cfg->cntr7clk_src << CLKMGR_MAINPLL_CNTR7CLK_SRC_LSB), |
Ley Foon Tan | 2669591 | 2019-11-08 10:38:21 +0800 | [diff] [blame] | 777 | socfpga_get_clkmgr_addr() + CLKMGR_A10_MAINPLL_CNTR7CLK); |
Ley Foon Tan | ca40f29 | 2017-04-26 02:44:39 +0800 | [diff] [blame] | 778 | /* main_s2f_user1_clk divider */ |
Ley Foon Tan | 2669591 | 2019-11-08 10:38:21 +0800 | [diff] [blame] | 779 | writel(main_cfg->cntr8clk_cnt, |
| 780 | socfpga_get_clkmgr_addr() + CLKMGR_A10_MAINPLL_CNTR8CLK); |
Ley Foon Tan | ca40f29 | 2017-04-26 02:44:39 +0800 | [diff] [blame] | 781 | /* main_hmc_pll_clk divider */ |
| 782 | writel(main_cfg->cntr9clk_cnt | |
| 783 | (main_cfg->cntr9clk_src << CLKMGR_MAINPLL_CNTR9CLK_SRC_LSB), |
Ley Foon Tan | 2669591 | 2019-11-08 10:38:21 +0800 | [diff] [blame] | 784 | socfpga_get_clkmgr_addr() + CLKMGR_A10_MAINPLL_CNTR9CLK); |
Ley Foon Tan | ca40f29 | 2017-04-26 02:44:39 +0800 | [diff] [blame] | 785 | /* main_periph_ref_clk divider */ |
| 786 | writel(main_cfg->cntr15clk_cnt, |
Ley Foon Tan | 2669591 | 2019-11-08 10:38:21 +0800 | [diff] [blame] | 787 | socfpga_get_clkmgr_addr() + CLKMGR_A10_MAINPLL_CNTR15CLK); |
Ley Foon Tan | ca40f29 | 2017-04-26 02:44:39 +0800 | [diff] [blame] | 788 | |
| 789 | /* setup all the peripheral PLL counter and clock source */ |
| 790 | /* peri_emaca_clk divider */ |
| 791 | writel(per_cfg->cntr2clk_cnt | |
| 792 | (per_cfg->cntr2clk_src << CLKMGR_PERPLL_CNTR2CLK_SRC_LSB), |
Ley Foon Tan | 2669591 | 2019-11-08 10:38:21 +0800 | [diff] [blame] | 793 | socfpga_get_clkmgr_addr() + CLKMGR_A10_PERPLL_CNTR2CLK); |
Ley Foon Tan | ca40f29 | 2017-04-26 02:44:39 +0800 | [diff] [blame] | 794 | /* peri_emacb_clk divider */ |
| 795 | writel(per_cfg->cntr3clk_cnt | |
| 796 | (per_cfg->cntr3clk_src << CLKMGR_PERPLL_CNTR3CLK_SRC_LSB), |
Ley Foon Tan | 2669591 | 2019-11-08 10:38:21 +0800 | [diff] [blame] | 797 | socfpga_get_clkmgr_addr() + CLKMGR_A10_PERPLL_CNTR3CLK); |
Ley Foon Tan | ca40f29 | 2017-04-26 02:44:39 +0800 | [diff] [blame] | 798 | /* peri_emac_ptp_clk divider */ |
| 799 | writel(per_cfg->cntr4clk_cnt | |
| 800 | (per_cfg->cntr4clk_src << CLKMGR_PERPLL_CNTR4CLK_SRC_LSB), |
Ley Foon Tan | 2669591 | 2019-11-08 10:38:21 +0800 | [diff] [blame] | 801 | socfpga_get_clkmgr_addr() + CLKMGR_A10_PERPLL_CNTR4CLK); |
Ley Foon Tan | ca40f29 | 2017-04-26 02:44:39 +0800 | [diff] [blame] | 802 | /* peri_gpio_db_clk divider */ |
| 803 | writel(per_cfg->cntr5clk_cnt | |
| 804 | (per_cfg->cntr5clk_src << CLKMGR_PERPLL_CNTR5CLK_SRC_LSB), |
Ley Foon Tan | 2669591 | 2019-11-08 10:38:21 +0800 | [diff] [blame] | 805 | socfpga_get_clkmgr_addr() + CLKMGR_A10_PERPLL_CNTR5CLK); |
Ley Foon Tan | ca40f29 | 2017-04-26 02:44:39 +0800 | [diff] [blame] | 806 | /* peri_sdmmc_clk divider */ |
| 807 | writel(per_cfg->cntr6clk_cnt | |
| 808 | (per_cfg->cntr6clk_src << CLKMGR_PERPLL_CNTR6CLK_SRC_LSB), |
Ley Foon Tan | 2669591 | 2019-11-08 10:38:21 +0800 | [diff] [blame] | 809 | socfpga_get_clkmgr_addr() + CLKMGR_A10_PERPLL_CNTR6CLK); |
Ley Foon Tan | ca40f29 | 2017-04-26 02:44:39 +0800 | [diff] [blame] | 810 | /* peri_s2f_user0_clk divider */ |
Ley Foon Tan | 2669591 | 2019-11-08 10:38:21 +0800 | [diff] [blame] | 811 | writel(per_cfg->cntr7clk_cnt, |
| 812 | socfpga_get_clkmgr_addr() + CLKMGR_A10_PERPLL_CNTR7CLK); |
Ley Foon Tan | ca40f29 | 2017-04-26 02:44:39 +0800 | [diff] [blame] | 813 | /* peri_s2f_user1_clk divider */ |
| 814 | writel(per_cfg->cntr8clk_cnt | |
| 815 | (per_cfg->cntr8clk_src << CLKMGR_PERPLL_CNTR8CLK_SRC_LSB), |
Ley Foon Tan | 2669591 | 2019-11-08 10:38:21 +0800 | [diff] [blame] | 816 | socfpga_get_clkmgr_addr() + CLKMGR_A10_PERPLL_CNTR8CLK); |
Ley Foon Tan | ca40f29 | 2017-04-26 02:44:39 +0800 | [diff] [blame] | 817 | /* peri_hmc_pll_clk divider */ |
Ley Foon Tan | 2669591 | 2019-11-08 10:38:21 +0800 | [diff] [blame] | 818 | writel(per_cfg->cntr9clk_cnt, |
| 819 | socfpga_get_clkmgr_addr() + CLKMGR_A10_PERPLL_CNTR9CLK); |
Ley Foon Tan | ca40f29 | 2017-04-26 02:44:39 +0800 | [diff] [blame] | 820 | |
| 821 | /* setup all the external PLL counter */ |
| 822 | /* mpu wrapper / external divider */ |
| 823 | writel(main_cfg->mpuclk_cnt | |
| 824 | (main_cfg->mpuclk_src << CLKMGR_MAINPLL_MPUCLK_SRC_LSB), |
Ley Foon Tan | 2669591 | 2019-11-08 10:38:21 +0800 | [diff] [blame] | 825 | socfpga_get_clkmgr_addr() + CLKMGR_A10_MAINPLL_MPUCLK); |
Ley Foon Tan | ca40f29 | 2017-04-26 02:44:39 +0800 | [diff] [blame] | 826 | /* NOC wrapper / external divider */ |
| 827 | writel(main_cfg->nocclk_cnt | |
| 828 | (main_cfg->nocclk_src << CLKMGR_MAINPLL_NOCCLK_SRC_LSB), |
Ley Foon Tan | 2669591 | 2019-11-08 10:38:21 +0800 | [diff] [blame] | 829 | socfpga_get_clkmgr_addr() + CLKMGR_A10_MAINPLL_NOCCLK); |
Ley Foon Tan | ca40f29 | 2017-04-26 02:44:39 +0800 | [diff] [blame] | 830 | /* NOC subclock divider such as l4 */ |
| 831 | writel(main_cfg->nocdiv_l4mainclk | |
| 832 | (main_cfg->nocdiv_l4mpclk << |
| 833 | CLKMGR_MAINPLL_NOCDIV_L4MPCLK_LSB) | |
| 834 | (main_cfg->nocdiv_l4spclk << |
| 835 | CLKMGR_MAINPLL_NOCDIV_L4SPCLK_LSB) | |
| 836 | (main_cfg->nocdiv_csatclk << |
| 837 | CLKMGR_MAINPLL_NOCDIV_CSATCLK_LSB) | |
| 838 | (main_cfg->nocdiv_cstraceclk << |
| 839 | CLKMGR_MAINPLL_NOCDIV_CSTRACECLK_LSB) | |
| 840 | (main_cfg->nocdiv_cspdbclk << |
| 841 | CLKMGR_MAINPLL_NOCDIV_CSPDBGCLK_LSB), |
Ley Foon Tan | 2669591 | 2019-11-08 10:38:21 +0800 | [diff] [blame] | 842 | socfpga_get_clkmgr_addr() + CLKMGR_A10_MAINPLL_NOCDIV); |
Ley Foon Tan | ca40f29 | 2017-04-26 02:44:39 +0800 | [diff] [blame] | 843 | /* gpio_db external divider */ |
| 844 | writel(per_cfg->gpiodiv_gpiodbclk, |
Ley Foon Tan | 2669591 | 2019-11-08 10:38:21 +0800 | [diff] [blame] | 845 | socfpga_get_clkmgr_addr() + CLKMGR_A10_PERPLL_GPIOFIV); |
Ley Foon Tan | ca40f29 | 2017-04-26 02:44:39 +0800 | [diff] [blame] | 846 | |
| 847 | /* setup the EMAC clock mux select */ |
| 848 | writel((per_cfg->emacctl_emac0sel << |
| 849 | CLKMGR_PERPLL_EMACCTL_EMAC0SEL_LSB) | |
| 850 | (per_cfg->emacctl_emac1sel << |
| 851 | CLKMGR_PERPLL_EMACCTL_EMAC1SEL_LSB) | |
| 852 | (per_cfg->emacctl_emac2sel << |
| 853 | CLKMGR_PERPLL_EMACCTL_EMAC2SEL_LSB), |
Ley Foon Tan | 2669591 | 2019-11-08 10:38:21 +0800 | [diff] [blame] | 854 | socfpga_get_clkmgr_addr() + CLKMGR_A10_PERPLL_EMACCTL); |
Ley Foon Tan | ca40f29 | 2017-04-26 02:44:39 +0800 | [diff] [blame] | 855 | |
| 856 | /* at this stage, check for PLL lock status */ |
| 857 | cm_wait_for_lock(LOCKED_MASK); |
| 858 | |
| 859 | /* |
| 860 | * after locking, but before taking out of bypass, |
| 861 | * assert/deassert outresetall |
| 862 | */ |
| 863 | /* assert mainpll outresetall */ |
Ley Foon Tan | 2669591 | 2019-11-08 10:38:21 +0800 | [diff] [blame] | 864 | setbits_le32(socfpga_get_clkmgr_addr() + CLKMGR_A10_MAINPLL_VCO0, |
Ley Foon Tan | ca40f29 | 2017-04-26 02:44:39 +0800 | [diff] [blame] | 865 | CLKMGR_MAINPLL_VCO0_OUTRSTALL_SET_MSK); |
| 866 | /* assert perpll outresetall */ |
Ley Foon Tan | 2669591 | 2019-11-08 10:38:21 +0800 | [diff] [blame] | 867 | setbits_le32(socfpga_get_clkmgr_addr() + CLKMGR_A10_PERPLL_VCO0, |
Ley Foon Tan | ca40f29 | 2017-04-26 02:44:39 +0800 | [diff] [blame] | 868 | CLKMGR_PERPLL_VCO0_OUTRSTALL_SET_MSK); |
| 869 | /* de-assert mainpll outresetall */ |
Ley Foon Tan | 2669591 | 2019-11-08 10:38:21 +0800 | [diff] [blame] | 870 | clrbits_le32(socfpga_get_clkmgr_addr() + CLKMGR_A10_MAINPLL_VCO0, |
Ley Foon Tan | ca40f29 | 2017-04-26 02:44:39 +0800 | [diff] [blame] | 871 | CLKMGR_MAINPLL_VCO0_OUTRSTALL_SET_MSK); |
| 872 | /* de-assert perpll outresetall */ |
Ley Foon Tan | 2669591 | 2019-11-08 10:38:21 +0800 | [diff] [blame] | 873 | clrbits_le32(socfpga_get_clkmgr_addr() + CLKMGR_A10_PERPLL_VCO0, |
Ley Foon Tan | ca40f29 | 2017-04-26 02:44:39 +0800 | [diff] [blame] | 874 | CLKMGR_PERPLL_VCO0_OUTRSTALL_SET_MSK); |
| 875 | |
| 876 | /* Take all PLLs out of bypass when boot mode is cleared. */ |
| 877 | /* release mainpll from bypass */ |
| 878 | writel(CLKMGR_MAINPLL_BYPASS_RESET, |
Ley Foon Tan | 2669591 | 2019-11-08 10:38:21 +0800 | [diff] [blame] | 879 | socfpga_get_clkmgr_addr() + CLKMGR_A10_MAINPLL_BYPASSR); |
Ley Foon Tan | ca40f29 | 2017-04-26 02:44:39 +0800 | [diff] [blame] | 880 | /* wait till Clock Manager is not busy */ |
| 881 | cm_wait_for_fsm(); |
| 882 | |
| 883 | /* release perpll from bypass */ |
| 884 | writel(CLKMGR_PERPLL_BYPASS_RESET, |
Ley Foon Tan | 2669591 | 2019-11-08 10:38:21 +0800 | [diff] [blame] | 885 | socfpga_get_clkmgr_addr() + CLKMGR_A10_PERPLL_BYPASSR); |
Ley Foon Tan | ca40f29 | 2017-04-26 02:44:39 +0800 | [diff] [blame] | 886 | /* wait till Clock Manager is not busy */ |
| 887 | cm_wait_for_fsm(); |
| 888 | |
| 889 | /* clear boot mode */ |
Ley Foon Tan | 2669591 | 2019-11-08 10:38:21 +0800 | [diff] [blame] | 890 | clrbits_le32(socfpga_get_clkmgr_addr() + CLKMGR_A10_CTRL, |
Ley Foon Tan | ca40f29 | 2017-04-26 02:44:39 +0800 | [diff] [blame] | 891 | CLKMGR_CLKMGR_CTL_BOOTMOD_SET_MSK); |
| 892 | /* wait till Clock Manager is not busy */ |
| 893 | cm_wait_for_fsm(); |
| 894 | |
| 895 | /* At here, we need to ramp to final value if needed */ |
| 896 | if (pll_ramp_main_hz != 0) |
| 897 | cm_pll_ramp_main(main_cfg, per_cfg, pll_ramp_main_hz); |
| 898 | if (pll_ramp_periph_hz != 0) |
| 899 | cm_pll_ramp_periph(main_cfg, per_cfg, pll_ramp_periph_hz); |
| 900 | |
| 901 | /* Now ungate non-hw-managed clocks */ |
| 902 | writel(CLKMGR_MAINPLL_EN_S2FUSER0CLKEN_SET_MSK | |
Ley Foon Tan | 2669591 | 2019-11-08 10:38:21 +0800 | [diff] [blame] | 903 | CLKMGR_MAINPLL_EN_HMCPLLREFCLKEN_SET_MSK, |
| 904 | socfpga_get_clkmgr_addr() + CLKMGR_A10_MAINPLL_ENS); |
| 905 | writel(CLKMGR_PERPLL_EN_RESET, |
| 906 | socfpga_get_clkmgr_addr() + CLKMGR_A10_PERPLL_ENS); |
Ley Foon Tan | ca40f29 | 2017-04-26 02:44:39 +0800 | [diff] [blame] | 907 | |
| 908 | /* Clear the loss lock and slip bits as they might set during |
| 909 | clock reconfiguration */ |
| 910 | writel(CLKMGR_CLKMGR_INTR_MAINPLLLOST_SET_MSK | |
| 911 | CLKMGR_CLKMGR_INTR_PERPLLLOST_SET_MSK | |
| 912 | CLKMGR_CLKMGR_INTR_MAINPLLRFSLIP_SET_MSK | |
| 913 | CLKMGR_CLKMGR_INTR_PERPLLRFSLIP_SET_MSK | |
| 914 | CLKMGR_CLKMGR_INTR_MAINPLLFBSLIP_SET_MSK | |
| 915 | CLKMGR_CLKMGR_INTR_PERPLLFBSLIP_SET_MSK, |
Ley Foon Tan | 2669591 | 2019-11-08 10:38:21 +0800 | [diff] [blame] | 916 | socfpga_get_clkmgr_addr() + CLKMGR_A10_INTR); |
Ley Foon Tan | ca40f29 | 2017-04-26 02:44:39 +0800 | [diff] [blame] | 917 | |
| 918 | return 0; |
| 919 | } |
| 920 | |
Marek Vasut | 8fdb419 | 2018-08-18 19:11:52 +0200 | [diff] [blame] | 921 | static void cm_use_intosc(void) |
Ley Foon Tan | ca40f29 | 2017-04-26 02:44:39 +0800 | [diff] [blame] | 922 | { |
Ley Foon Tan | 2669591 | 2019-11-08 10:38:21 +0800 | [diff] [blame] | 923 | setbits_le32(socfpga_get_clkmgr_addr() + CLKMGR_A10_CTRL, |
Ley Foon Tan | ca40f29 | 2017-04-26 02:44:39 +0800 | [diff] [blame] | 924 | CLKMGR_CLKMGR_CTL_BOOTCLK_INTOSC_SET_MSK); |
| 925 | } |
| 926 | |
Ley Foon Tan | ca40f29 | 2017-04-26 02:44:39 +0800 | [diff] [blame] | 927 | int cm_basic_init(const void *blob) |
| 928 | { |
| 929 | struct mainpll_cfg main_cfg; |
| 930 | struct perpll_cfg per_cfg; |
Ley Foon Tan | ca40f29 | 2017-04-26 02:44:39 +0800 | [diff] [blame] | 931 | int rval; |
| 932 | |
| 933 | /* initialize to zero for use case of optional node */ |
| 934 | memset(&main_cfg, 0, sizeof(main_cfg)); |
| 935 | memset(&per_cfg, 0, sizeof(per_cfg)); |
Ley Foon Tan | ca40f29 | 2017-04-26 02:44:39 +0800 | [diff] [blame] | 936 | |
Marek Vasut | ec472e0 | 2018-05-12 00:09:21 +0200 | [diff] [blame] | 937 | rval = of_get_clk_cfg(blob, &main_cfg, &per_cfg); |
Ley Foon Tan | ca40f29 | 2017-04-26 02:44:39 +0800 | [diff] [blame] | 938 | if (rval) |
| 939 | return rval; |
| 940 | |
Marek Vasut | 8fdb419 | 2018-08-18 19:11:52 +0200 | [diff] [blame] | 941 | cm_use_intosc(); |
| 942 | |
Marek Vasut | fd6bcb5 | 2018-07-31 17:33:42 +0200 | [diff] [blame] | 943 | return cm_full_cfg(&main_cfg, &per_cfg); |
Ley Foon Tan | ca40f29 | 2017-04-26 02:44:39 +0800 | [diff] [blame] | 944 | } |
Marek Vasut | 8fdb419 | 2018-08-18 19:11:52 +0200 | [diff] [blame] | 945 | #endif |
Ley Foon Tan | ca40f29 | 2017-04-26 02:44:39 +0800 | [diff] [blame] | 946 | |
Marek Vasut | 71b1637 | 2018-08-06 21:42:05 +0200 | [diff] [blame] | 947 | static u32 cm_get_rate_dm(char *name) |
Ley Foon Tan | ca40f29 | 2017-04-26 02:44:39 +0800 | [diff] [blame] | 948 | { |
Marek Vasut | 71b1637 | 2018-08-06 21:42:05 +0200 | [diff] [blame] | 949 | struct uclass *uc; |
| 950 | struct udevice *dev = NULL; |
| 951 | struct clk clk = { 0 }; |
| 952 | ulong rate; |
| 953 | int ret; |
Ley Foon Tan | ca40f29 | 2017-04-26 02:44:39 +0800 | [diff] [blame] | 954 | |
Marek Vasut | 71b1637 | 2018-08-06 21:42:05 +0200 | [diff] [blame] | 955 | /* Device addresses start at 1 */ |
| 956 | ret = uclass_get(UCLASS_CLK, &uc); |
| 957 | if (ret) |
Ley Foon Tan | ca40f29 | 2017-04-26 02:44:39 +0800 | [diff] [blame] | 958 | return 0; |
Ley Foon Tan | ca40f29 | 2017-04-26 02:44:39 +0800 | [diff] [blame] | 959 | |
Marek Vasut | 71b1637 | 2018-08-06 21:42:05 +0200 | [diff] [blame] | 960 | ret = uclass_get_device_by_name(UCLASS_CLK, name, &dev); |
| 961 | if (ret) |
Ley Foon Tan | ca40f29 | 2017-04-26 02:44:39 +0800 | [diff] [blame] | 962 | return 0; |
Ley Foon Tan | ca40f29 | 2017-04-26 02:44:39 +0800 | [diff] [blame] | 963 | |
Marek Vasut | 71b1637 | 2018-08-06 21:42:05 +0200 | [diff] [blame] | 964 | ret = device_probe(dev); |
| 965 | if (ret) |
Ley Foon Tan | ca40f29 | 2017-04-26 02:44:39 +0800 | [diff] [blame] | 966 | return 0; |
Ley Foon Tan | ca40f29 | 2017-04-26 02:44:39 +0800 | [diff] [blame] | 967 | |
Marek Vasut | 71b1637 | 2018-08-06 21:42:05 +0200 | [diff] [blame] | 968 | ret = clk_request(dev, &clk); |
| 969 | if (ret) |
| 970 | return 0; |
Ley Foon Tan | ca40f29 | 2017-04-26 02:44:39 +0800 | [diff] [blame] | 971 | |
Marek Vasut | 71b1637 | 2018-08-06 21:42:05 +0200 | [diff] [blame] | 972 | rate = clk_get_rate(&clk); |
Ley Foon Tan | ca40f29 | 2017-04-26 02:44:39 +0800 | [diff] [blame] | 973 | |
Marek Vasut | 71b1637 | 2018-08-06 21:42:05 +0200 | [diff] [blame] | 974 | clk_free(&clk); |
Ley Foon Tan | ca40f29 | 2017-04-26 02:44:39 +0800 | [diff] [blame] | 975 | |
Marek Vasut | 71b1637 | 2018-08-06 21:42:05 +0200 | [diff] [blame] | 976 | return rate; |
Ley Foon Tan | ca40f29 | 2017-04-26 02:44:39 +0800 | [diff] [blame] | 977 | } |
| 978 | |
Marek Vasut | 71b1637 | 2018-08-06 21:42:05 +0200 | [diff] [blame] | 979 | static u32 cm_get_rate_dm_khz(char *name) |
Ley Foon Tan | ca40f29 | 2017-04-26 02:44:39 +0800 | [diff] [blame] | 980 | { |
Marek Vasut | 71b1637 | 2018-08-06 21:42:05 +0200 | [diff] [blame] | 981 | return cm_get_rate_dm(name) / 1000; |
Ley Foon Tan | ca40f29 | 2017-04-26 02:44:39 +0800 | [diff] [blame] | 982 | } |
| 983 | |
Marek Vasut | 71b1637 | 2018-08-06 21:42:05 +0200 | [diff] [blame] | 984 | unsigned long cm_get_mpu_clk_hz(void) |
Ley Foon Tan | ca40f29 | 2017-04-26 02:44:39 +0800 | [diff] [blame] | 985 | { |
Marek Vasut | 71b1637 | 2018-08-06 21:42:05 +0200 | [diff] [blame] | 986 | return cm_get_rate_dm("main_mpu_base_clk"); |
Ley Foon Tan | ca40f29 | 2017-04-26 02:44:39 +0800 | [diff] [blame] | 987 | } |
| 988 | |
| 989 | unsigned int cm_get_qspi_controller_clk_hz(void) |
| 990 | { |
Marek Vasut | 71b1637 | 2018-08-06 21:42:05 +0200 | [diff] [blame] | 991 | return cm_get_rate_dm("qspi_clk"); |
Ley Foon Tan | ca40f29 | 2017-04-26 02:44:39 +0800 | [diff] [blame] | 992 | } |
| 993 | |
Marek Vasut | 71b1637 | 2018-08-06 21:42:05 +0200 | [diff] [blame] | 994 | unsigned int cm_get_l4_sp_clk_hz(void) |
Eugeniy Paltsev | 7473932 | 2017-12-28 15:09:02 +0300 | [diff] [blame] | 995 | { |
Marek Vasut | 71b1637 | 2018-08-06 21:42:05 +0200 | [diff] [blame] | 996 | return cm_get_rate_dm("l4_sp_clk"); |
Eugeniy Paltsev | 7473932 | 2017-12-28 15:09:02 +0300 | [diff] [blame] | 997 | } |
| 998 | |
Ley Foon Tan | ca40f29 | 2017-04-26 02:44:39 +0800 | [diff] [blame] | 999 | void cm_print_clock_quick_summary(void) |
| 1000 | { |
Marek Vasut | 71b1637 | 2018-08-06 21:42:05 +0200 | [diff] [blame] | 1001 | printf("MPU %10d kHz\n", cm_get_rate_dm_khz("main_mpu_base_clk")); |
| 1002 | printf("MMC %8d kHz\n", cm_get_rate_dm_khz("sdmmc_clk")); |
| 1003 | printf("QSPI %8d kHz\n", cm_get_rate_dm_khz("qspi_clk")); |
| 1004 | printf("SPI %8d kHz\n", cm_get_rate_dm_khz("spi_m_clk")); |
| 1005 | printf("EOSC1 %8d kHz\n", cm_get_rate_dm_khz("osc1")); |
| 1006 | printf("cb_intosc %8d kHz\n", cm_get_rate_dm_khz("cb_intosc_ls_clk")); |
| 1007 | printf("f2s_free %8d kHz\n", cm_get_rate_dm_khz("f2s_free_clk")); |
| 1008 | printf("Main VCO %8d kHz\n", cm_get_rate_dm_khz("main_pll@40")); |
| 1009 | printf("NOC %8d kHz\n", cm_get_rate_dm_khz("main_noc_base_clk")); |
| 1010 | printf("L4 Main %8d kHz\n", cm_get_rate_dm_khz("l4_main_clk")); |
| 1011 | printf("L4 MP %8d kHz\n", cm_get_rate_dm_khz("l4_mp_clk")); |
| 1012 | printf("L4 SP %8d kHz\n", cm_get_rate_dm_khz("l4_sp_clk")); |
| 1013 | printf("L4 sys free %8d kHz\n", cm_get_rate_dm_khz("l4_sys_free_clk")); |
Ley Foon Tan | ca40f29 | 2017-04-26 02:44:39 +0800 | [diff] [blame] | 1014 | } |