Eugeniy Paltsev | 7e1fb09 | 2017-12-10 21:20:08 +0300 | [diff] [blame] | 1 | /* |
| 2 | * Synopsys HSDK SDP CGU clock driver |
| 3 | * |
| 4 | * Copyright (C) 2017 Synopsys |
| 5 | * Author: Eugeniy Paltsev <Eugeniy.Paltsev@synopsys.com> |
| 6 | * |
| 7 | * This file is licensed under the terms of the GNU General Public |
| 8 | * License version 2. This program is licensed "as is" without any |
| 9 | * warranty of any kind, whether express or implied. |
| 10 | */ |
| 11 | |
| 12 | #include <common.h> |
| 13 | #include <clk-uclass.h> |
| 14 | #include <div64.h> |
| 15 | #include <dm.h> |
| 16 | #include <linux/io.h> |
| 17 | |
| 18 | /* |
| 19 | * Synopsys ARC HSDK clock tree. |
| 20 | * |
| 21 | * ------------------ |
| 22 | * | 33.33 MHz xtal | |
| 23 | * ------------------ |
| 24 | * | |
| 25 | * | ----------- |
| 26 | * |-->| ARC PLL | |
| 27 | * | ----------- |
| 28 | * | | |
| 29 | * | |-->|CGU_ARC_IDIV|-----------> |
| 30 | * | |-->|CREG_CORE_IF_DIV|-------> |
| 31 | * | |
| 32 | * | -------------- |
| 33 | * |-->| SYSTEM PLL | |
| 34 | * | -------------- |
| 35 | * | | |
| 36 | * | |-->|CGU_SYS_IDIV_APB|-------> |
| 37 | * | |-->|CGU_SYS_IDIV_AXI|-------> |
| 38 | * | |-->|CGU_SYS_IDIV_*|---------> |
| 39 | * | |-->|CGU_SYS_IDIV_EBI_REF|---> |
| 40 | * | |
| 41 | * | -------------- |
| 42 | * |-->| TUNNEL PLL | |
| 43 | * | -------------- |
| 44 | * | | |
Eugeniy Paltsev | 7451424 | 2018-01-16 20:44:25 +0300 | [diff] [blame] | 45 | * | |-->|CGU_TUN_IDIV_TUN|-----------> |
| 46 | * | |-->|CGU_TUN_IDIV_ROM|-----------> |
| 47 | * | |-->|CGU_TUN_IDIV_PWM|-----------> |
Eugeniy Paltsev | 7e1fb09 | 2017-12-10 21:20:08 +0300 | [diff] [blame] | 48 | * | |
Eugeniy Paltsev | 7e1fb09 | 2017-12-10 21:20:08 +0300 | [diff] [blame] | 49 | * | ----------- |
| 50 | * |-->| DDR PLL | |
| 51 | * ----------- |
| 52 | * | |
| 53 | * |----------------------------> |
Eugeniy Paltsev | 5872fc6 | 2020-01-29 14:08:30 +0300 | [diff] [blame] | 54 | * |
| 55 | * ------------------ |
| 56 | * | 27.00 MHz xtal | |
| 57 | * ------------------ |
| 58 | * | |
| 59 | * | ------------ |
| 60 | * |-->| HDMI PLL | |
| 61 | * ------------ |
| 62 | * | |
| 63 | * |-->|CGU_HDMI_IDIV_APB|------> |
Eugeniy Paltsev | 7e1fb09 | 2017-12-10 21:20:08 +0300 | [diff] [blame] | 64 | */ |
| 65 | |
Eugeniy Paltsev | 7e1fb09 | 2017-12-10 21:20:08 +0300 | [diff] [blame] | 66 | #define CGU_ARC_IDIV 0x080 |
Eugeniy Paltsev | 7451424 | 2018-01-16 20:44:25 +0300 | [diff] [blame] | 67 | #define CGU_TUN_IDIV_TUN 0x380 |
| 68 | #define CGU_TUN_IDIV_ROM 0x390 |
| 69 | #define CGU_TUN_IDIV_PWM 0x3A0 |
Eugeniy Paltsev | 6575713 | 2020-04-23 14:50:50 +0300 | [diff] [blame] | 70 | #define CGU_TUN_IDIV_TIMER 0x3B0 |
Eugeniy Paltsev | 7e1fb09 | 2017-12-10 21:20:08 +0300 | [diff] [blame] | 71 | #define CGU_HDMI_IDIV_APB 0x480 |
| 72 | #define CGU_SYS_IDIV_APB 0x180 |
| 73 | #define CGU_SYS_IDIV_AXI 0x190 |
| 74 | #define CGU_SYS_IDIV_ETH 0x1A0 |
| 75 | #define CGU_SYS_IDIV_USB 0x1B0 |
| 76 | #define CGU_SYS_IDIV_SDIO 0x1C0 |
| 77 | #define CGU_SYS_IDIV_HDMI 0x1D0 |
| 78 | #define CGU_SYS_IDIV_GFX_CORE 0x1E0 |
| 79 | #define CGU_SYS_IDIV_GFX_DMA 0x1F0 |
| 80 | #define CGU_SYS_IDIV_GFX_CFG 0x200 |
| 81 | #define CGU_SYS_IDIV_DMAC_CORE 0x210 |
| 82 | #define CGU_SYS_IDIV_DMAC_CFG 0x220 |
| 83 | #define CGU_SYS_IDIV_SDIO_REF 0x230 |
| 84 | #define CGU_SYS_IDIV_SPI_REF 0x240 |
| 85 | #define CGU_SYS_IDIV_I2C_REF 0x250 |
| 86 | #define CGU_SYS_IDIV_UART_REF 0x260 |
| 87 | #define CGU_SYS_IDIV_EBI_REF 0x270 |
| 88 | |
| 89 | #define CGU_IDIV_MASK 0xFF /* All idiv have 8 significant bits */ |
| 90 | |
| 91 | #define CGU_ARC_PLL 0x0 |
| 92 | #define CGU_SYS_PLL 0x10 |
| 93 | #define CGU_DDR_PLL 0x20 |
| 94 | #define CGU_TUN_PLL 0x30 |
| 95 | #define CGU_HDMI_PLL 0x40 |
| 96 | |
| 97 | #define CGU_PLL_CTRL 0x000 /* ARC PLL control register */ |
| 98 | #define CGU_PLL_STATUS 0x004 /* ARC PLL status register */ |
| 99 | #define CGU_PLL_FMEAS 0x008 /* ARC PLL frequency measurement register */ |
| 100 | #define CGU_PLL_MON 0x00C /* ARC PLL monitor register */ |
| 101 | |
| 102 | #define CGU_PLL_CTRL_ODIV_SHIFT 2 |
| 103 | #define CGU_PLL_CTRL_IDIV_SHIFT 4 |
| 104 | #define CGU_PLL_CTRL_FBDIV_SHIFT 9 |
| 105 | #define CGU_PLL_CTRL_BAND_SHIFT 20 |
| 106 | |
| 107 | #define CGU_PLL_CTRL_ODIV_MASK GENMASK(3, CGU_PLL_CTRL_ODIV_SHIFT) |
| 108 | #define CGU_PLL_CTRL_IDIV_MASK GENMASK(8, CGU_PLL_CTRL_IDIV_SHIFT) |
| 109 | #define CGU_PLL_CTRL_FBDIV_MASK GENMASK(15, CGU_PLL_CTRL_FBDIV_SHIFT) |
| 110 | |
| 111 | #define CGU_PLL_CTRL_PD BIT(0) |
| 112 | #define CGU_PLL_CTRL_BYPASS BIT(1) |
| 113 | |
| 114 | #define CGU_PLL_STATUS_LOCK BIT(0) |
| 115 | #define CGU_PLL_STATUS_ERR BIT(1) |
| 116 | |
| 117 | #define HSDK_PLL_MAX_LOCK_TIME 100 /* 100 us */ |
| 118 | |
| 119 | #define CREG_CORE_IF_DIV 0x000 /* ARC CORE interface divider */ |
| 120 | #define CORE_IF_CLK_THRESHOLD_HZ 500000000 |
| 121 | #define CREG_CORE_IF_CLK_DIV_1 0x0 |
| 122 | #define CREG_CORE_IF_CLK_DIV_2 0x1 |
| 123 | |
Eugeniy Paltsev | 7451424 | 2018-01-16 20:44:25 +0300 | [diff] [blame] | 124 | #define MIN_PLL_RATE 100000000 /* 100 MHz */ |
Eugeniy Paltsev | 5872fc6 | 2020-01-29 14:08:30 +0300 | [diff] [blame] | 125 | #define PARENT_RATE_33 33333333 /* fixed clock - xtal */ |
| 126 | #define PARENT_RATE_27 27000000 /* fixed clock - xtal */ |
Eugeniy Paltsev | 6575713 | 2020-04-23 14:50:50 +0300 | [diff] [blame] | 127 | #define CGU_MAX_CLOCKS 27 |
Eugeniy Paltsev | 7451424 | 2018-01-16 20:44:25 +0300 | [diff] [blame] | 128 | |
Eugeniy Paltsev | 42da394 | 2020-05-07 16:59:54 +0300 | [diff] [blame] | 129 | #define MAX_FREQ_VARIATIONS 6 |
Eugeniy Paltsev | 7451424 | 2018-01-16 20:44:25 +0300 | [diff] [blame] | 130 | |
Eugeniy Paltsev | 42da394 | 2020-05-07 16:59:54 +0300 | [diff] [blame] | 131 | struct hsdk_idiv_cfg { |
Eugeniy Paltsev | 7451424 | 2018-01-16 20:44:25 +0300 | [diff] [blame] | 132 | u32 oft; |
Eugeniy Paltsev | 42da394 | 2020-05-07 16:59:54 +0300 | [diff] [blame] | 133 | u8 val[MAX_FREQ_VARIATIONS]; |
Eugeniy Paltsev | 7451424 | 2018-01-16 20:44:25 +0300 | [diff] [blame] | 134 | }; |
| 135 | |
Eugeniy Paltsev | 42da394 | 2020-05-07 16:59:54 +0300 | [diff] [blame] | 136 | struct hsdk_div_full_cfg { |
| 137 | const u32 clk_rate[MAX_FREQ_VARIATIONS]; |
| 138 | const u32 pll_rate[MAX_FREQ_VARIATIONS]; |
| 139 | const struct hsdk_idiv_cfg idiv[]; |
Eugeniy Paltsev | 7451424 | 2018-01-16 20:44:25 +0300 | [diff] [blame] | 140 | }; |
| 141 | |
Eugeniy Paltsev | 42da394 | 2020-05-07 16:59:54 +0300 | [diff] [blame] | 142 | static const struct hsdk_div_full_cfg tun_clk_cfg = { |
Eugeniy Paltsev | 7451424 | 2018-01-16 20:44:25 +0300 | [diff] [blame] | 143 | { 25000000, 50000000, 75000000, 100000000, 125000000, 150000000 }, |
Eugeniy Paltsev | c6f15de | 2020-04-16 22:35:11 +0300 | [diff] [blame] | 144 | { 600000000, 600000000, 600000000, 600000000, 750000000, 600000000 }, { |
Eugeniy Paltsev | 7451424 | 2018-01-16 20:44:25 +0300 | [diff] [blame] | 145 | { CGU_TUN_IDIV_TUN, { 24, 12, 8, 6, 6, 4 } }, |
| 146 | { CGU_TUN_IDIV_ROM, { 4, 4, 4, 4, 5, 4 } }, |
Eugeniy Paltsev | 6575713 | 2020-04-23 14:50:50 +0300 | [diff] [blame] | 147 | { CGU_TUN_IDIV_PWM, { 8, 8, 8, 8, 10, 8 } }, |
Eugeniy Paltsev | 42da394 | 2020-05-07 16:59:54 +0300 | [diff] [blame] | 148 | { CGU_TUN_IDIV_TIMER, { 12, 12, 12, 12, 15, 12 } }, |
| 149 | { /* last one */ } |
Eugeniy Paltsev | 7451424 | 2018-01-16 20:44:25 +0300 | [diff] [blame] | 150 | } |
| 151 | }; |
| 152 | |
Eugeniy Paltsev | 42da394 | 2020-05-07 16:59:54 +0300 | [diff] [blame] | 153 | static const struct hsdk_div_full_cfg axi_clk_cfg = { |
Eugeniy Paltsev | 7451424 | 2018-01-16 20:44:25 +0300 | [diff] [blame] | 154 | { 200000000, 400000000, 600000000, 800000000 }, |
| 155 | { 800000000, 800000000, 600000000, 800000000 }, { |
| 156 | { CGU_SYS_IDIV_APB, { 4, 4, 3, 4 } }, /* APB */ |
| 157 | { CGU_SYS_IDIV_AXI, { 4, 2, 1, 1 } }, /* AXI */ |
| 158 | { CGU_SYS_IDIV_ETH, { 2, 2, 2, 2 } }, /* ETH */ |
| 159 | { CGU_SYS_IDIV_USB, { 2, 2, 2, 2 } }, /* USB */ |
| 160 | { CGU_SYS_IDIV_SDIO, { 2, 2, 2, 2 } }, /* SDIO */ |
| 161 | { CGU_SYS_IDIV_HDMI, { 2, 2, 2, 2 } }, /* HDMI */ |
| 162 | { CGU_SYS_IDIV_GFX_CORE, { 1, 1, 1, 1 } }, /* GPU-CORE */ |
| 163 | { CGU_SYS_IDIV_GFX_DMA, { 2, 2, 2, 2 } }, /* GPU-DMA */ |
| 164 | { CGU_SYS_IDIV_GFX_CFG, { 4, 4, 3, 4 } }, /* GPU-CFG */ |
| 165 | { CGU_SYS_IDIV_DMAC_CORE,{ 2, 2, 2, 2 } }, /* DMAC-CORE */ |
| 166 | { CGU_SYS_IDIV_DMAC_CFG, { 4, 4, 3, 4 } }, /* DMAC-CFG */ |
| 167 | { CGU_SYS_IDIV_SDIO_REF, { 8, 8, 6, 8 } }, /* SDIO-REF */ |
| 168 | { CGU_SYS_IDIV_SPI_REF, { 24, 24, 18, 24 } }, /* SPI-REF */ |
| 169 | { CGU_SYS_IDIV_I2C_REF, { 4, 4, 3, 4 } }, /* I2C-REF */ |
| 170 | { CGU_SYS_IDIV_UART_REF, { 24, 24, 18, 24 } }, /* UART-REF */ |
Eugeniy Paltsev | 42da394 | 2020-05-07 16:59:54 +0300 | [diff] [blame] | 171 | { CGU_SYS_IDIV_EBI_REF, { 16, 16, 12, 16 } }, /* EBI-REF */ |
| 172 | { /* last one */ } |
Eugeniy Paltsev | 7451424 | 2018-01-16 20:44:25 +0300 | [diff] [blame] | 173 | } |
| 174 | }; |
Eugeniy Paltsev | 7e1fb09 | 2017-12-10 21:20:08 +0300 | [diff] [blame] | 175 | |
| 176 | struct hsdk_pll_cfg { |
| 177 | u32 rate; |
| 178 | u32 idiv; |
| 179 | u32 fbdiv; |
| 180 | u32 odiv; |
| 181 | u32 band; |
| 182 | }; |
| 183 | |
| 184 | static const struct hsdk_pll_cfg asdt_pll_cfg[] = { |
| 185 | { 100000000, 0, 11, 3, 0 }, |
| 186 | { 125000000, 0, 14, 3, 0 }, |
| 187 | { 133000000, 0, 15, 3, 0 }, |
| 188 | { 150000000, 0, 17, 3, 0 }, |
| 189 | { 200000000, 1, 47, 3, 0 }, |
| 190 | { 233000000, 1, 27, 2, 0 }, |
| 191 | { 300000000, 1, 35, 2, 0 }, |
| 192 | { 333000000, 1, 39, 2, 0 }, |
| 193 | { 400000000, 1, 47, 2, 0 }, |
| 194 | { 500000000, 0, 14, 1, 0 }, |
| 195 | { 600000000, 0, 17, 1, 0 }, |
| 196 | { 700000000, 0, 20, 1, 0 }, |
Eugeniy Paltsev | c6f15de | 2020-04-16 22:35:11 +0300 | [diff] [blame] | 197 | { 750000000, 1, 44, 1, 0 }, |
Eugeniy Paltsev | 7e1fb09 | 2017-12-10 21:20:08 +0300 | [diff] [blame] | 198 | { 800000000, 0, 23, 1, 0 }, |
| 199 | { 900000000, 1, 26, 0, 0 }, |
| 200 | { 1000000000, 1, 29, 0, 0 }, |
| 201 | { 1100000000, 1, 32, 0, 0 }, |
| 202 | { 1200000000, 1, 35, 0, 0 }, |
| 203 | { 1300000000, 1, 38, 0, 0 }, |
| 204 | { 1400000000, 1, 41, 0, 0 }, |
| 205 | { 1500000000, 1, 44, 0, 0 }, |
| 206 | { 1600000000, 1, 47, 0, 0 }, |
| 207 | {} |
| 208 | }; |
| 209 | |
| 210 | static const struct hsdk_pll_cfg hdmi_pll_cfg[] = { |
| 211 | { 297000000, 0, 21, 2, 0 }, |
| 212 | { 540000000, 0, 19, 1, 0 }, |
| 213 | { 594000000, 0, 21, 1, 0 }, |
| 214 | {} |
| 215 | }; |
| 216 | |
| 217 | struct hsdk_cgu_clk { |
| 218 | /* CGU block register */ |
| 219 | void __iomem *cgu_regs; |
| 220 | /* CREG block register */ |
| 221 | void __iomem *creg_regs; |
| 222 | |
| 223 | /* PLLs registers */ |
| 224 | void __iomem *regs; |
| 225 | /* PLLs special registers */ |
| 226 | void __iomem *spec_regs; |
| 227 | /* PLLs devdata */ |
| 228 | const struct hsdk_pll_devdata *pll_devdata; |
| 229 | |
| 230 | /* Dividers registers */ |
| 231 | void __iomem *idiv_regs; |
| 232 | }; |
| 233 | |
| 234 | struct hsdk_pll_devdata { |
Eugeniy Paltsev | 5872fc6 | 2020-01-29 14:08:30 +0300 | [diff] [blame] | 235 | const u32 parent_rate; |
Eugeniy Paltsev | 7e1fb09 | 2017-12-10 21:20:08 +0300 | [diff] [blame] | 236 | const struct hsdk_pll_cfg *pll_cfg; |
| 237 | int (*update_rate)(struct hsdk_cgu_clk *clk, unsigned long rate, |
| 238 | const struct hsdk_pll_cfg *cfg); |
| 239 | }; |
| 240 | |
| 241 | static int hsdk_pll_core_update_rate(struct hsdk_cgu_clk *, unsigned long, |
| 242 | const struct hsdk_pll_cfg *); |
| 243 | static int hsdk_pll_comm_update_rate(struct hsdk_cgu_clk *, unsigned long, |
| 244 | const struct hsdk_pll_cfg *); |
| 245 | |
| 246 | static const struct hsdk_pll_devdata core_pll_dat = { |
Eugeniy Paltsev | 5872fc6 | 2020-01-29 14:08:30 +0300 | [diff] [blame] | 247 | .parent_rate = PARENT_RATE_33, |
Eugeniy Paltsev | 7e1fb09 | 2017-12-10 21:20:08 +0300 | [diff] [blame] | 248 | .pll_cfg = asdt_pll_cfg, |
| 249 | .update_rate = hsdk_pll_core_update_rate, |
| 250 | }; |
| 251 | |
| 252 | static const struct hsdk_pll_devdata sdt_pll_dat = { |
Eugeniy Paltsev | 5872fc6 | 2020-01-29 14:08:30 +0300 | [diff] [blame] | 253 | .parent_rate = PARENT_RATE_33, |
Eugeniy Paltsev | 7e1fb09 | 2017-12-10 21:20:08 +0300 | [diff] [blame] | 254 | .pll_cfg = asdt_pll_cfg, |
| 255 | .update_rate = hsdk_pll_comm_update_rate, |
| 256 | }; |
| 257 | |
| 258 | static const struct hsdk_pll_devdata hdmi_pll_dat = { |
Eugeniy Paltsev | 5872fc6 | 2020-01-29 14:08:30 +0300 | [diff] [blame] | 259 | .parent_rate = PARENT_RATE_27, |
Eugeniy Paltsev | 7e1fb09 | 2017-12-10 21:20:08 +0300 | [diff] [blame] | 260 | .pll_cfg = hdmi_pll_cfg, |
| 261 | .update_rate = hsdk_pll_comm_update_rate, |
| 262 | }; |
| 263 | |
| 264 | static ulong idiv_set(struct clk *, ulong); |
Eugeniy Paltsev | 7451424 | 2018-01-16 20:44:25 +0300 | [diff] [blame] | 265 | static ulong cpu_clk_set(struct clk *, ulong); |
| 266 | static ulong axi_clk_set(struct clk *, ulong); |
| 267 | static ulong tun_clk_set(struct clk *, ulong); |
Eugeniy Paltsev | 7e1fb09 | 2017-12-10 21:20:08 +0300 | [diff] [blame] | 268 | static ulong idiv_get(struct clk *); |
| 269 | static int idiv_off(struct clk *); |
| 270 | static ulong pll_set(struct clk *, ulong); |
| 271 | static ulong pll_get(struct clk *); |
| 272 | |
| 273 | struct hsdk_cgu_clock_map { |
| 274 | u32 cgu_pll_oft; |
Eugeniy Paltsev | 7e1fb09 | 2017-12-10 21:20:08 +0300 | [diff] [blame] | 275 | u32 cgu_div_oft; |
| 276 | const struct hsdk_pll_devdata *pll_devdata; |
| 277 | ulong (*get_rate)(struct clk *clk); |
| 278 | ulong (*set_rate)(struct clk *clk, ulong rate); |
| 279 | int (*disable)(struct clk *clk); |
| 280 | }; |
| 281 | |
| 282 | static const struct hsdk_cgu_clock_map clock_map[] = { |
Eugeniy Paltsev | f29fb45 | 2020-05-07 17:37:37 +0300 | [diff] [blame^] | 283 | { CGU_ARC_PLL, 0, &core_pll_dat, pll_get, pll_set, NULL }, |
| 284 | { CGU_ARC_PLL, CGU_ARC_IDIV, &core_pll_dat, idiv_get, cpu_clk_set, idiv_off }, |
| 285 | { CGU_DDR_PLL, 0, &sdt_pll_dat, pll_get, pll_set, NULL }, |
| 286 | { CGU_SYS_PLL, 0, &sdt_pll_dat, pll_get, pll_set, NULL }, |
| 287 | { CGU_SYS_PLL, CGU_SYS_IDIV_APB, &sdt_pll_dat, idiv_get, idiv_set, idiv_off }, |
| 288 | { CGU_SYS_PLL, CGU_SYS_IDIV_AXI, &sdt_pll_dat, idiv_get, axi_clk_set, idiv_off }, |
| 289 | { CGU_SYS_PLL, CGU_SYS_IDIV_ETH, &sdt_pll_dat, idiv_get, idiv_set, idiv_off }, |
| 290 | { CGU_SYS_PLL, CGU_SYS_IDIV_USB, &sdt_pll_dat, idiv_get, idiv_set, idiv_off }, |
| 291 | { CGU_SYS_PLL, CGU_SYS_IDIV_SDIO, &sdt_pll_dat, idiv_get, idiv_set, idiv_off }, |
| 292 | { CGU_SYS_PLL, CGU_SYS_IDIV_HDMI, &sdt_pll_dat, idiv_get, idiv_set, idiv_off }, |
| 293 | { CGU_SYS_PLL, CGU_SYS_IDIV_GFX_CORE, &sdt_pll_dat, idiv_get, idiv_set, idiv_off }, |
| 294 | { CGU_SYS_PLL, CGU_SYS_IDIV_GFX_DMA, &sdt_pll_dat, idiv_get, idiv_set, idiv_off }, |
| 295 | { CGU_SYS_PLL, CGU_SYS_IDIV_GFX_CFG, &sdt_pll_dat, idiv_get, idiv_set, idiv_off }, |
| 296 | { CGU_SYS_PLL, CGU_SYS_IDIV_DMAC_CORE, &sdt_pll_dat, idiv_get, idiv_set, idiv_off }, |
| 297 | { CGU_SYS_PLL, CGU_SYS_IDIV_DMAC_CFG, &sdt_pll_dat, idiv_get, idiv_set, idiv_off }, |
| 298 | { CGU_SYS_PLL, CGU_SYS_IDIV_SDIO_REF, &sdt_pll_dat, idiv_get, idiv_set, idiv_off }, |
| 299 | { CGU_SYS_PLL, CGU_SYS_IDIV_SPI_REF, &sdt_pll_dat, idiv_get, idiv_set, idiv_off }, |
| 300 | { CGU_SYS_PLL, CGU_SYS_IDIV_I2C_REF, &sdt_pll_dat, idiv_get, idiv_set, idiv_off }, |
| 301 | { CGU_SYS_PLL, CGU_SYS_IDIV_UART_REF, &sdt_pll_dat, idiv_get, idiv_set, idiv_off }, |
| 302 | { CGU_SYS_PLL, CGU_SYS_IDIV_EBI_REF, &sdt_pll_dat, idiv_get, idiv_set, idiv_off }, |
| 303 | { CGU_TUN_PLL, 0, &sdt_pll_dat, pll_get, pll_set, NULL }, |
| 304 | { CGU_TUN_PLL, CGU_TUN_IDIV_TUN, &sdt_pll_dat, idiv_get, tun_clk_set, idiv_off }, |
| 305 | { CGU_TUN_PLL, CGU_TUN_IDIV_ROM, &sdt_pll_dat, idiv_get, idiv_set, idiv_off }, |
| 306 | { CGU_TUN_PLL, CGU_TUN_IDIV_PWM, &sdt_pll_dat, idiv_get, idiv_set, idiv_off }, |
| 307 | { CGU_TUN_PLL, CGU_TUN_IDIV_TIMER, &sdt_pll_dat, idiv_get, idiv_set, idiv_off }, |
| 308 | { CGU_HDMI_PLL, 0, &hdmi_pll_dat, pll_get, pll_set, NULL }, |
| 309 | { CGU_HDMI_PLL, CGU_HDMI_IDIV_APB, &hdmi_pll_dat, idiv_get, idiv_set, idiv_off } |
Eugeniy Paltsev | 7e1fb09 | 2017-12-10 21:20:08 +0300 | [diff] [blame] | 310 | }; |
| 311 | |
| 312 | static inline void hsdk_idiv_write(struct hsdk_cgu_clk *clk, u32 val) |
| 313 | { |
| 314 | iowrite32(val, clk->idiv_regs); |
| 315 | } |
| 316 | |
| 317 | static inline u32 hsdk_idiv_read(struct hsdk_cgu_clk *clk) |
| 318 | { |
| 319 | return ioread32(clk->idiv_regs); |
| 320 | } |
| 321 | |
| 322 | static inline void hsdk_pll_write(struct hsdk_cgu_clk *clk, u32 reg, u32 val) |
| 323 | { |
| 324 | iowrite32(val, clk->regs + reg); |
| 325 | } |
| 326 | |
| 327 | static inline u32 hsdk_pll_read(struct hsdk_cgu_clk *clk, u32 reg) |
| 328 | { |
| 329 | return ioread32(clk->regs + reg); |
| 330 | } |
| 331 | |
| 332 | static inline void hsdk_pll_spcwrite(struct hsdk_cgu_clk *clk, u32 reg, u32 val) |
| 333 | { |
| 334 | iowrite32(val, clk->spec_regs + reg); |
| 335 | } |
| 336 | |
| 337 | static inline u32 hsdk_pll_spcread(struct hsdk_cgu_clk *clk, u32 reg) |
| 338 | { |
| 339 | return ioread32(clk->spec_regs + reg); |
| 340 | } |
| 341 | |
| 342 | static inline void hsdk_pll_set_cfg(struct hsdk_cgu_clk *clk, |
| 343 | const struct hsdk_pll_cfg *cfg) |
| 344 | { |
| 345 | u32 val = 0; |
| 346 | |
| 347 | /* Powerdown and Bypass bits should be cleared */ |
| 348 | val |= cfg->idiv << CGU_PLL_CTRL_IDIV_SHIFT; |
| 349 | val |= cfg->fbdiv << CGU_PLL_CTRL_FBDIV_SHIFT; |
| 350 | val |= cfg->odiv << CGU_PLL_CTRL_ODIV_SHIFT; |
| 351 | val |= cfg->band << CGU_PLL_CTRL_BAND_SHIFT; |
| 352 | |
| 353 | pr_debug("write configurarion: %#x\n", val); |
| 354 | |
| 355 | hsdk_pll_write(clk, CGU_PLL_CTRL, val); |
| 356 | } |
| 357 | |
| 358 | static inline bool hsdk_pll_is_locked(struct hsdk_cgu_clk *clk) |
| 359 | { |
| 360 | return !!(hsdk_pll_read(clk, CGU_PLL_STATUS) & CGU_PLL_STATUS_LOCK); |
| 361 | } |
| 362 | |
| 363 | static inline bool hsdk_pll_is_err(struct hsdk_cgu_clk *clk) |
| 364 | { |
| 365 | return !!(hsdk_pll_read(clk, CGU_PLL_STATUS) & CGU_PLL_STATUS_ERR); |
| 366 | } |
| 367 | |
| 368 | static ulong pll_get(struct clk *sclk) |
| 369 | { |
| 370 | u32 val; |
| 371 | u64 rate; |
| 372 | u32 idiv, fbdiv, odiv; |
| 373 | struct hsdk_cgu_clk *clk = dev_get_priv(sclk->dev); |
Eugeniy Paltsev | 5872fc6 | 2020-01-29 14:08:30 +0300 | [diff] [blame] | 374 | u32 parent_rate = clk->pll_devdata->parent_rate; |
Eugeniy Paltsev | 7e1fb09 | 2017-12-10 21:20:08 +0300 | [diff] [blame] | 375 | |
| 376 | val = hsdk_pll_read(clk, CGU_PLL_CTRL); |
| 377 | |
| 378 | pr_debug("current configurarion: %#x\n", val); |
| 379 | |
Eugeniy Paltsev | 7e1fb09 | 2017-12-10 21:20:08 +0300 | [diff] [blame] | 380 | /* Check if PLL is bypassed */ |
| 381 | if (val & CGU_PLL_CTRL_BYPASS) |
Eugeniy Paltsev | 5872fc6 | 2020-01-29 14:08:30 +0300 | [diff] [blame] | 382 | return parent_rate; |
Eugeniy Paltsev | 7e1fb09 | 2017-12-10 21:20:08 +0300 | [diff] [blame] | 383 | |
Eugeniy Paltsev | 9d70c9c | 2020-01-29 14:08:29 +0300 | [diff] [blame] | 384 | /* Check if PLL is disabled */ |
| 385 | if (val & CGU_PLL_CTRL_PD) |
| 386 | return 0; |
| 387 | |
Eugeniy Paltsev | 7e1fb09 | 2017-12-10 21:20:08 +0300 | [diff] [blame] | 388 | /* input divider = reg.idiv + 1 */ |
| 389 | idiv = 1 + ((val & CGU_PLL_CTRL_IDIV_MASK) >> CGU_PLL_CTRL_IDIV_SHIFT); |
| 390 | /* fb divider = 2*(reg.fbdiv + 1) */ |
| 391 | fbdiv = 2 * (1 + ((val & CGU_PLL_CTRL_FBDIV_MASK) >> CGU_PLL_CTRL_FBDIV_SHIFT)); |
| 392 | /* output divider = 2^(reg.odiv) */ |
| 393 | odiv = 1 << ((val & CGU_PLL_CTRL_ODIV_MASK) >> CGU_PLL_CTRL_ODIV_SHIFT); |
| 394 | |
Eugeniy Paltsev | 5872fc6 | 2020-01-29 14:08:30 +0300 | [diff] [blame] | 395 | rate = (u64)parent_rate * fbdiv; |
Eugeniy Paltsev | 7e1fb09 | 2017-12-10 21:20:08 +0300 | [diff] [blame] | 396 | do_div(rate, idiv * odiv); |
| 397 | |
| 398 | return rate; |
| 399 | } |
| 400 | |
| 401 | static unsigned long hsdk_pll_round_rate(struct clk *sclk, unsigned long rate) |
| 402 | { |
| 403 | int i; |
| 404 | unsigned long best_rate; |
| 405 | struct hsdk_cgu_clk *clk = dev_get_priv(sclk->dev); |
| 406 | const struct hsdk_pll_cfg *pll_cfg = clk->pll_devdata->pll_cfg; |
| 407 | |
| 408 | if (pll_cfg[0].rate == 0) |
| 409 | return -EINVAL; |
| 410 | |
| 411 | best_rate = pll_cfg[0].rate; |
| 412 | |
| 413 | for (i = 1; pll_cfg[i].rate != 0; i++) { |
| 414 | if (abs(rate - pll_cfg[i].rate) < abs(rate - best_rate)) |
| 415 | best_rate = pll_cfg[i].rate; |
| 416 | } |
| 417 | |
| 418 | pr_debug("chosen best rate: %lu\n", best_rate); |
| 419 | |
| 420 | return best_rate; |
| 421 | } |
| 422 | |
| 423 | static int hsdk_pll_comm_update_rate(struct hsdk_cgu_clk *clk, |
| 424 | unsigned long rate, |
| 425 | const struct hsdk_pll_cfg *cfg) |
| 426 | { |
| 427 | hsdk_pll_set_cfg(clk, cfg); |
| 428 | |
| 429 | /* |
| 430 | * Wait until CGU relocks and check error status. |
| 431 | * If after timeout CGU is unlocked yet return error. |
| 432 | */ |
| 433 | udelay(HSDK_PLL_MAX_LOCK_TIME); |
| 434 | if (!hsdk_pll_is_locked(clk)) |
| 435 | return -ETIMEDOUT; |
| 436 | |
| 437 | if (hsdk_pll_is_err(clk)) |
| 438 | return -EINVAL; |
| 439 | |
| 440 | return 0; |
| 441 | } |
| 442 | |
| 443 | static int hsdk_pll_core_update_rate(struct hsdk_cgu_clk *clk, |
| 444 | unsigned long rate, |
| 445 | const struct hsdk_pll_cfg *cfg) |
| 446 | { |
| 447 | /* |
| 448 | * When core clock exceeds 500MHz, the divider for the interface |
| 449 | * clock must be programmed to div-by-2. |
| 450 | */ |
| 451 | if (rate > CORE_IF_CLK_THRESHOLD_HZ) |
| 452 | hsdk_pll_spcwrite(clk, CREG_CORE_IF_DIV, CREG_CORE_IF_CLK_DIV_2); |
| 453 | |
| 454 | hsdk_pll_set_cfg(clk, cfg); |
| 455 | |
| 456 | /* |
| 457 | * Wait until CGU relocks and check error status. |
| 458 | * If after timeout CGU is unlocked yet return error. |
| 459 | */ |
| 460 | udelay(HSDK_PLL_MAX_LOCK_TIME); |
| 461 | if (!hsdk_pll_is_locked(clk)) |
| 462 | return -ETIMEDOUT; |
| 463 | |
| 464 | if (hsdk_pll_is_err(clk)) |
| 465 | return -EINVAL; |
| 466 | |
| 467 | /* |
| 468 | * Program divider to div-by-1 if we succesfuly set core clock below |
| 469 | * 500MHz threshold. |
| 470 | */ |
| 471 | if (rate <= CORE_IF_CLK_THRESHOLD_HZ) |
| 472 | hsdk_pll_spcwrite(clk, CREG_CORE_IF_DIV, CREG_CORE_IF_CLK_DIV_1); |
| 473 | |
| 474 | return 0; |
| 475 | } |
| 476 | |
| 477 | static ulong pll_set(struct clk *sclk, ulong rate) |
| 478 | { |
| 479 | int i; |
| 480 | unsigned long best_rate; |
| 481 | struct hsdk_cgu_clk *clk = dev_get_priv(sclk->dev); |
| 482 | const struct hsdk_pll_cfg *pll_cfg = clk->pll_devdata->pll_cfg; |
| 483 | |
| 484 | best_rate = hsdk_pll_round_rate(sclk, rate); |
| 485 | |
| 486 | for (i = 0; pll_cfg[i].rate != 0; i++) { |
| 487 | if (pll_cfg[i].rate == best_rate) { |
| 488 | return clk->pll_devdata->update_rate(clk, best_rate, |
| 489 | &pll_cfg[i]); |
| 490 | } |
| 491 | } |
| 492 | |
Eugeniy Paltsev | 5872fc6 | 2020-01-29 14:08:30 +0300 | [diff] [blame] | 493 | pr_err("invalid rate=%ld Hz, parent_rate=%d Hz\n", best_rate, |
| 494 | clk->pll_devdata->parent_rate); |
Eugeniy Paltsev | 7e1fb09 | 2017-12-10 21:20:08 +0300 | [diff] [blame] | 495 | |
| 496 | return -EINVAL; |
| 497 | } |
| 498 | |
| 499 | static int idiv_off(struct clk *sclk) |
| 500 | { |
| 501 | struct hsdk_cgu_clk *clk = dev_get_priv(sclk->dev); |
| 502 | |
| 503 | hsdk_idiv_write(clk, 0); |
| 504 | |
| 505 | return 0; |
| 506 | } |
| 507 | |
| 508 | static ulong idiv_get(struct clk *sclk) |
| 509 | { |
| 510 | struct hsdk_cgu_clk *clk = dev_get_priv(sclk->dev); |
| 511 | ulong parent_rate = pll_get(sclk); |
| 512 | u32 div_factor = hsdk_idiv_read(clk); |
| 513 | |
| 514 | div_factor &= CGU_IDIV_MASK; |
| 515 | |
| 516 | pr_debug("current configurarion: %#x (%d)\n", div_factor, div_factor); |
| 517 | |
| 518 | if (div_factor == 0) |
| 519 | return 0; |
| 520 | |
| 521 | return parent_rate / div_factor; |
| 522 | } |
| 523 | |
Eugeniy Paltsev | 7451424 | 2018-01-16 20:44:25 +0300 | [diff] [blame] | 524 | /* Special behavior: wen we set this clock we set both idiv and pll */ |
| 525 | static ulong cpu_clk_set(struct clk *sclk, ulong rate) |
| 526 | { |
| 527 | ulong ret; |
| 528 | |
| 529 | ret = pll_set(sclk, rate); |
| 530 | idiv_set(sclk, rate); |
| 531 | |
| 532 | return ret; |
| 533 | } |
| 534 | |
Eugeniy Paltsev | 42da394 | 2020-05-07 16:59:54 +0300 | [diff] [blame] | 535 | /* |
| 536 | * Special behavior: |
| 537 | * when we set these clocks we set both PLL and all idiv dividers related to |
| 538 | * this PLL domain. |
| 539 | */ |
| 540 | static ulong common_div_clk_set(struct clk *sclk, ulong rate, |
| 541 | const struct hsdk_div_full_cfg *cfg) |
Eugeniy Paltsev | 7451424 | 2018-01-16 20:44:25 +0300 | [diff] [blame] | 542 | { |
| 543 | struct hsdk_cgu_clk *clk = dev_get_priv(sclk->dev); |
| 544 | ulong pll_rate; |
| 545 | int i, freq_idx = -1; |
| 546 | ulong ret = 0; |
| 547 | |
| 548 | pll_rate = pll_get(sclk); |
| 549 | |
Eugeniy Paltsev | 42da394 | 2020-05-07 16:59:54 +0300 | [diff] [blame] | 550 | for (i = 0; i < MAX_FREQ_VARIATIONS; i++) { |
| 551 | /* unused freq variations are filled with 0 */ |
| 552 | if (!cfg->clk_rate[i]) |
| 553 | break; |
| 554 | |
| 555 | if (cfg->clk_rate[i] == rate) { |
Eugeniy Paltsev | 7451424 | 2018-01-16 20:44:25 +0300 | [diff] [blame] | 556 | freq_idx = i; |
| 557 | break; |
| 558 | } |
| 559 | } |
| 560 | |
| 561 | if (freq_idx < 0) { |
Eugeniy Paltsev | 42da394 | 2020-05-07 16:59:54 +0300 | [diff] [blame] | 562 | pr_err("clk: invalid rate=%ld Hz\n", rate); |
Eugeniy Paltsev | 7451424 | 2018-01-16 20:44:25 +0300 | [diff] [blame] | 563 | return -EINVAL; |
| 564 | } |
| 565 | |
| 566 | /* configure PLL before dividers */ |
Eugeniy Paltsev | 42da394 | 2020-05-07 16:59:54 +0300 | [diff] [blame] | 567 | if (cfg->pll_rate[freq_idx] < pll_rate) |
| 568 | ret = pll_set(sclk, cfg->pll_rate[freq_idx]); |
Eugeniy Paltsev | 7451424 | 2018-01-16 20:44:25 +0300 | [diff] [blame] | 569 | |
| 570 | /* configure SYS dividers */ |
Eugeniy Paltsev | 42da394 | 2020-05-07 16:59:54 +0300 | [diff] [blame] | 571 | for (i = 0; cfg->idiv[i].oft != 0; i++) { |
| 572 | clk->idiv_regs = clk->cgu_regs + cfg->idiv[i].oft; |
| 573 | hsdk_idiv_write(clk, cfg->idiv[i].val[freq_idx]); |
Eugeniy Paltsev | 7451424 | 2018-01-16 20:44:25 +0300 | [diff] [blame] | 574 | } |
| 575 | |
| 576 | /* configure PLL after dividers */ |
Eugeniy Paltsev | 42da394 | 2020-05-07 16:59:54 +0300 | [diff] [blame] | 577 | if (cfg->pll_rate[freq_idx] >= pll_rate) |
| 578 | ret = pll_set(sclk, cfg->pll_rate[freq_idx]); |
Eugeniy Paltsev | 7451424 | 2018-01-16 20:44:25 +0300 | [diff] [blame] | 579 | |
| 580 | return ret; |
| 581 | } |
| 582 | |
Eugeniy Paltsev | 42da394 | 2020-05-07 16:59:54 +0300 | [diff] [blame] | 583 | static ulong axi_clk_set(struct clk *sclk, ulong rate) |
Eugeniy Paltsev | 7451424 | 2018-01-16 20:44:25 +0300 | [diff] [blame] | 584 | { |
Eugeniy Paltsev | 42da394 | 2020-05-07 16:59:54 +0300 | [diff] [blame] | 585 | return common_div_clk_set(sclk, rate, &axi_clk_cfg); |
| 586 | } |
Eugeniy Paltsev | 7451424 | 2018-01-16 20:44:25 +0300 | [diff] [blame] | 587 | |
Eugeniy Paltsev | 42da394 | 2020-05-07 16:59:54 +0300 | [diff] [blame] | 588 | static ulong tun_clk_set(struct clk *sclk, ulong rate) |
| 589 | { |
| 590 | return common_div_clk_set(sclk, rate, &tun_clk_cfg); |
Eugeniy Paltsev | 7451424 | 2018-01-16 20:44:25 +0300 | [diff] [blame] | 591 | } |
| 592 | |
Eugeniy Paltsev | 7e1fb09 | 2017-12-10 21:20:08 +0300 | [diff] [blame] | 593 | static ulong idiv_set(struct clk *sclk, ulong rate) |
| 594 | { |
| 595 | struct hsdk_cgu_clk *clk = dev_get_priv(sclk->dev); |
| 596 | ulong parent_rate = pll_get(sclk); |
| 597 | u32 div_factor; |
| 598 | |
| 599 | div_factor = parent_rate / rate; |
| 600 | if (abs(rate - parent_rate / (div_factor + 1)) <= |
| 601 | abs(rate - parent_rate / div_factor)) { |
| 602 | div_factor += 1; |
| 603 | } |
| 604 | |
| 605 | if (div_factor & ~CGU_IDIV_MASK) { |
Eugeniy Paltsev | 7d7d9f2 | 2018-01-16 20:44:27 +0300 | [diff] [blame] | 606 | pr_err("invalid rate=%ld Hz, parent_rate=%ld Hz, div=%d: max divider valie is%d\n", |
Eugeniy Paltsev | 7e1fb09 | 2017-12-10 21:20:08 +0300 | [diff] [blame] | 607 | rate, parent_rate, div_factor, CGU_IDIV_MASK); |
| 608 | |
| 609 | div_factor = CGU_IDIV_MASK; |
| 610 | } |
| 611 | |
| 612 | if (div_factor == 0) { |
Eugeniy Paltsev | 7d7d9f2 | 2018-01-16 20:44:27 +0300 | [diff] [blame] | 613 | pr_err("invalid rate=%ld Hz, parent_rate=%ld Hz, div=%d: min divider valie is 1\n", |
Eugeniy Paltsev | 7e1fb09 | 2017-12-10 21:20:08 +0300 | [diff] [blame] | 614 | rate, parent_rate, div_factor); |
| 615 | |
| 616 | div_factor = 1; |
| 617 | } |
| 618 | |
| 619 | hsdk_idiv_write(clk, div_factor); |
| 620 | |
| 621 | return 0; |
| 622 | } |
| 623 | |
| 624 | static int hsdk_prepare_clock_tree_branch(struct clk *sclk) |
| 625 | { |
| 626 | struct hsdk_cgu_clk *clk = dev_get_priv(sclk->dev); |
| 627 | |
| 628 | if (sclk->id >= CGU_MAX_CLOCKS) |
| 629 | return -EINVAL; |
| 630 | |
| 631 | clk->pll_devdata = clock_map[sclk->id].pll_devdata; |
| 632 | clk->regs = clk->cgu_regs + clock_map[sclk->id].cgu_pll_oft; |
Eugeniy Paltsev | f29fb45 | 2020-05-07 17:37:37 +0300 | [diff] [blame^] | 633 | clk->spec_regs = clk->creg_regs; |
Eugeniy Paltsev | 7e1fb09 | 2017-12-10 21:20:08 +0300 | [diff] [blame] | 634 | clk->idiv_regs = clk->cgu_regs + clock_map[sclk->id].cgu_div_oft; |
| 635 | |
| 636 | return 0; |
| 637 | } |
| 638 | |
| 639 | static ulong hsdk_cgu_get_rate(struct clk *sclk) |
| 640 | { |
| 641 | if (hsdk_prepare_clock_tree_branch(sclk)) |
| 642 | return -EINVAL; |
| 643 | |
| 644 | return clock_map[sclk->id].get_rate(sclk); |
| 645 | } |
| 646 | |
| 647 | static ulong hsdk_cgu_set_rate(struct clk *sclk, ulong rate) |
| 648 | { |
| 649 | if (hsdk_prepare_clock_tree_branch(sclk)) |
| 650 | return -EINVAL; |
| 651 | |
| 652 | return clock_map[sclk->id].set_rate(sclk, rate); |
| 653 | } |
| 654 | |
| 655 | static int hsdk_cgu_disable(struct clk *sclk) |
| 656 | { |
| 657 | if (hsdk_prepare_clock_tree_branch(sclk)) |
| 658 | return -EINVAL; |
| 659 | |
| 660 | if (clock_map[sclk->id].disable) |
| 661 | return clock_map[sclk->id].disable(sclk); |
| 662 | |
| 663 | return -ENOTSUPP; |
| 664 | } |
| 665 | |
| 666 | static const struct clk_ops hsdk_cgu_ops = { |
| 667 | .set_rate = hsdk_cgu_set_rate, |
| 668 | .get_rate = hsdk_cgu_get_rate, |
| 669 | .disable = hsdk_cgu_disable, |
| 670 | }; |
| 671 | |
| 672 | static int hsdk_cgu_clk_probe(struct udevice *dev) |
| 673 | { |
| 674 | struct hsdk_cgu_clk *pll_clk = dev_get_priv(dev); |
| 675 | |
| 676 | BUILD_BUG_ON(ARRAY_SIZE(clock_map) != CGU_MAX_CLOCKS); |
| 677 | |
| 678 | pll_clk->cgu_regs = (void __iomem *)devfdt_get_addr_index(dev, 0); |
| 679 | if (!pll_clk->cgu_regs) |
| 680 | return -EINVAL; |
| 681 | |
| 682 | pll_clk->creg_regs = (void __iomem *)devfdt_get_addr_index(dev, 1); |
| 683 | if (!pll_clk->creg_regs) |
| 684 | return -EINVAL; |
| 685 | |
| 686 | return 0; |
| 687 | } |
| 688 | |
| 689 | static const struct udevice_id hsdk_cgu_clk_id[] = { |
| 690 | { .compatible = "snps,hsdk-cgu-clock" }, |
| 691 | { } |
| 692 | }; |
| 693 | |
| 694 | U_BOOT_DRIVER(hsdk_cgu_clk) = { |
| 695 | .name = "hsdk-cgu-clk", |
| 696 | .id = UCLASS_CLK, |
| 697 | .of_match = hsdk_cgu_clk_id, |
| 698 | .probe = hsdk_cgu_clk_probe, |
Eugeniy Paltsev | a5a238f | 2018-01-16 20:44:26 +0300 | [diff] [blame] | 699 | .priv_auto_alloc_size = sizeof(struct hsdk_cgu_clk), |
Eugeniy Paltsev | 7e1fb09 | 2017-12-10 21:20:08 +0300 | [diff] [blame] | 700 | .ops = &hsdk_cgu_ops, |
| 701 | }; |