Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Siva Durga Prasad Paladugu | 128ec1f | 2016-11-15 16:15:41 +0530 | [diff] [blame] | 2 | /* |
| 3 | * ZynqMP clock driver |
| 4 | * |
| 5 | * Copyright (C) 2016 Xilinx, Inc. |
Siva Durga Prasad Paladugu | 128ec1f | 2016-11-15 16:15:41 +0530 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include <common.h> |
Simon Glass | f7ae49f | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 9 | #include <log.h> |
Simon Glass | 336d461 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 10 | #include <malloc.h> |
| 11 | #include <dm/device_compat.h> |
Siva Durga Prasad Paladugu | 128ec1f | 2016-11-15 16:15:41 +0530 | [diff] [blame] | 12 | #include <linux/bitops.h> |
| 13 | #include <clk-uclass.h> |
Siva Durga Prasad Paladugu | 128ec1f | 2016-11-15 16:15:41 +0530 | [diff] [blame] | 14 | #include <clk.h> |
Michal Simek | 5b90412 | 2023-06-23 14:51:57 +0200 | [diff] [blame] | 15 | #include <zynqmp_firmware.h> |
Siva Durga Prasad Paladugu | ad76f8c | 2017-02-03 23:56:49 +0530 | [diff] [blame] | 16 | #include <asm/arch/sys_proto.h> |
Simon Glass | 9d92245 | 2017-05-17 17:18:03 -0600 | [diff] [blame] | 17 | #include <dm.h> |
Simon Glass | 61b29b8 | 2020-02-03 07:36:15 -0700 | [diff] [blame] | 18 | #include <linux/err.h> |
Siva Durga Prasad Paladugu | 128ec1f | 2016-11-15 16:15:41 +0530 | [diff] [blame] | 19 | |
Siva Durga Prasad Paladugu | ad76f8c | 2017-02-03 23:56:49 +0530 | [diff] [blame] | 20 | static const resource_size_t zynqmp_crf_apb_clkc_base = 0xfd1a0020; |
| 21 | static const resource_size_t zynqmp_crl_apb_clkc_base = 0xff5e0020; |
Siva Durga Prasad Paladugu | 128ec1f | 2016-11-15 16:15:41 +0530 | [diff] [blame] | 22 | |
Siva Durga Prasad Paladugu | ad76f8c | 2017-02-03 23:56:49 +0530 | [diff] [blame] | 23 | /* Full power domain clocks */ |
| 24 | #define CRF_APB_APLL_CTRL (zynqmp_crf_apb_clkc_base + 0x00) |
| 25 | #define CRF_APB_DPLL_CTRL (zynqmp_crf_apb_clkc_base + 0x0c) |
| 26 | #define CRF_APB_VPLL_CTRL (zynqmp_crf_apb_clkc_base + 0x18) |
| 27 | #define CRF_APB_PLL_STATUS (zynqmp_crf_apb_clkc_base + 0x24) |
| 28 | #define CRF_APB_APLL_TO_LPD_CTRL (zynqmp_crf_apb_clkc_base + 0x28) |
| 29 | #define CRF_APB_DPLL_TO_LPD_CTRL (zynqmp_crf_apb_clkc_base + 0x2c) |
| 30 | #define CRF_APB_VPLL_TO_LPD_CTRL (zynqmp_crf_apb_clkc_base + 0x30) |
| 31 | /* Peripheral clocks */ |
| 32 | #define CRF_APB_ACPU_CTRL (zynqmp_crf_apb_clkc_base + 0x40) |
| 33 | #define CRF_APB_DBG_TRACE_CTRL (zynqmp_crf_apb_clkc_base + 0x44) |
| 34 | #define CRF_APB_DBG_FPD_CTRL (zynqmp_crf_apb_clkc_base + 0x48) |
| 35 | #define CRF_APB_DP_VIDEO_REF_CTRL (zynqmp_crf_apb_clkc_base + 0x50) |
| 36 | #define CRF_APB_DP_AUDIO_REF_CTRL (zynqmp_crf_apb_clkc_base + 0x54) |
| 37 | #define CRF_APB_DP_STC_REF_CTRL (zynqmp_crf_apb_clkc_base + 0x5c) |
| 38 | #define CRF_APB_DDR_CTRL (zynqmp_crf_apb_clkc_base + 0x60) |
| 39 | #define CRF_APB_GPU_REF_CTRL (zynqmp_crf_apb_clkc_base + 0x64) |
| 40 | #define CRF_APB_SATA_REF_CTRL (zynqmp_crf_apb_clkc_base + 0x80) |
| 41 | #define CRF_APB_PCIE_REF_CTRL (zynqmp_crf_apb_clkc_base + 0x94) |
| 42 | #define CRF_APB_GDMA_REF_CTRL (zynqmp_crf_apb_clkc_base + 0x98) |
| 43 | #define CRF_APB_DPDMA_REF_CTRL (zynqmp_crf_apb_clkc_base + 0x9c) |
| 44 | #define CRF_APB_TOPSW_MAIN_CTRL (zynqmp_crf_apb_clkc_base + 0xa0) |
| 45 | #define CRF_APB_TOPSW_LSBUS_CTRL (zynqmp_crf_apb_clkc_base + 0xa4) |
| 46 | #define CRF_APB_GTGREF0_REF_CTRL (zynqmp_crf_apb_clkc_base + 0xa8) |
| 47 | #define CRF_APB_DBG_TSTMP_CTRL (zynqmp_crf_apb_clkc_base + 0xd8) |
| 48 | |
| 49 | /* Low power domain clocks */ |
| 50 | #define CRL_APB_IOPLL_CTRL (zynqmp_crl_apb_clkc_base + 0x00) |
| 51 | #define CRL_APB_RPLL_CTRL (zynqmp_crl_apb_clkc_base + 0x10) |
| 52 | #define CRL_APB_PLL_STATUS (zynqmp_crl_apb_clkc_base + 0x20) |
| 53 | #define CRL_APB_IOPLL_TO_FPD_CTRL (zynqmp_crl_apb_clkc_base + 0x24) |
| 54 | #define CRL_APB_RPLL_TO_FPD_CTRL (zynqmp_crl_apb_clkc_base + 0x28) |
| 55 | /* Peripheral clocks */ |
| 56 | #define CRL_APB_USB3_DUAL_REF_CTRL (zynqmp_crl_apb_clkc_base + 0x2c) |
| 57 | #define CRL_APB_GEM0_REF_CTRL (zynqmp_crl_apb_clkc_base + 0x30) |
| 58 | #define CRL_APB_GEM1_REF_CTRL (zynqmp_crl_apb_clkc_base + 0x34) |
| 59 | #define CRL_APB_GEM2_REF_CTRL (zynqmp_crl_apb_clkc_base + 0x38) |
| 60 | #define CRL_APB_GEM3_REF_CTRL (zynqmp_crl_apb_clkc_base + 0x3c) |
| 61 | #define CRL_APB_USB0_BUS_REF_CTRL (zynqmp_crl_apb_clkc_base + 0x40) |
| 62 | #define CRL_APB_USB1_BUS_REF_CTRL (zynqmp_crl_apb_clkc_base + 0x44) |
| 63 | #define CRL_APB_QSPI_REF_CTRL (zynqmp_crl_apb_clkc_base + 0x48) |
| 64 | #define CRL_APB_SDIO0_REF_CTRL (zynqmp_crl_apb_clkc_base + 0x4c) |
| 65 | #define CRL_APB_SDIO1_REF_CTRL (zynqmp_crl_apb_clkc_base + 0x50) |
| 66 | #define CRL_APB_UART0_REF_CTRL (zynqmp_crl_apb_clkc_base + 0x54) |
| 67 | #define CRL_APB_UART1_REF_CTRL (zynqmp_crl_apb_clkc_base + 0x58) |
| 68 | #define CRL_APB_SPI0_REF_CTRL (zynqmp_crl_apb_clkc_base + 0x5c) |
| 69 | #define CRL_APB_SPI1_REF_CTRL (zynqmp_crl_apb_clkc_base + 0x60) |
| 70 | #define CRL_APB_CAN0_REF_CTRL (zynqmp_crl_apb_clkc_base + 0x64) |
| 71 | #define CRL_APB_CAN1_REF_CTRL (zynqmp_crl_apb_clkc_base + 0x68) |
| 72 | #define CRL_APB_CPU_R5_CTRL (zynqmp_crl_apb_clkc_base + 0x70) |
| 73 | #define CRL_APB_IOU_SWITCH_CTRL (zynqmp_crl_apb_clkc_base + 0x7c) |
| 74 | #define CRL_APB_CSU_PLL_CTRL (zynqmp_crl_apb_clkc_base + 0x80) |
| 75 | #define CRL_APB_PCAP_CTRL (zynqmp_crl_apb_clkc_base + 0x84) |
| 76 | #define CRL_APB_LPD_SWITCH_CTRL (zynqmp_crl_apb_clkc_base + 0x88) |
| 77 | #define CRL_APB_LPD_LSBUS_CTRL (zynqmp_crl_apb_clkc_base + 0x8c) |
| 78 | #define CRL_APB_DBG_LPD_CTRL (zynqmp_crl_apb_clkc_base + 0x90) |
| 79 | #define CRL_APB_NAND_REF_CTRL (zynqmp_crl_apb_clkc_base + 0x94) |
| 80 | #define CRL_APB_ADMA_REF_CTRL (zynqmp_crl_apb_clkc_base + 0x98) |
| 81 | #define CRL_APB_PL0_REF_CTRL (zynqmp_crl_apb_clkc_base + 0xa0) |
| 82 | #define CRL_APB_PL1_REF_CTRL (zynqmp_crl_apb_clkc_base + 0xa4) |
| 83 | #define CRL_APB_PL2_REF_CTRL (zynqmp_crl_apb_clkc_base + 0xa8) |
| 84 | #define CRL_APB_PL3_REF_CTRL (zynqmp_crl_apb_clkc_base + 0xac) |
| 85 | #define CRL_APB_PL0_THR_CNT (zynqmp_crl_apb_clkc_base + 0xb4) |
| 86 | #define CRL_APB_PL1_THR_CNT (zynqmp_crl_apb_clkc_base + 0xbc) |
| 87 | #define CRL_APB_PL2_THR_CNT (zynqmp_crl_apb_clkc_base + 0xc4) |
| 88 | #define CRL_APB_PL3_THR_CNT (zynqmp_crl_apb_clkc_base + 0xdc) |
| 89 | #define CRL_APB_GEM_TSU_REF_CTRL (zynqmp_crl_apb_clkc_base + 0xe0) |
| 90 | #define CRL_APB_DLL_REF_CTRL (zynqmp_crl_apb_clkc_base + 0xe4) |
| 91 | #define CRL_APB_AMS_REF_CTRL (zynqmp_crl_apb_clkc_base + 0xe8) |
| 92 | #define CRL_APB_I2C0_REF_CTRL (zynqmp_crl_apb_clkc_base + 0x100) |
| 93 | #define CRL_APB_I2C1_REF_CTRL (zynqmp_crl_apb_clkc_base + 0x104) |
| 94 | #define CRL_APB_TIMESTAMP_REF_CTRL (zynqmp_crl_apb_clkc_base + 0x108) |
Siva Durga Prasad Paladugu | ad76f8c | 2017-02-03 23:56:49 +0530 | [diff] [blame] | 95 | |
| 96 | #define ZYNQ_CLK_MAXDIV 0x3f |
| 97 | #define CLK_CTRL_DIV1_SHIFT 16 |
| 98 | #define CLK_CTRL_DIV1_MASK (ZYNQ_CLK_MAXDIV << CLK_CTRL_DIV1_SHIFT) |
| 99 | #define CLK_CTRL_DIV0_SHIFT 8 |
| 100 | #define CLK_CTRL_DIV0_MASK (ZYNQ_CLK_MAXDIV << CLK_CTRL_DIV0_SHIFT) |
T Karthik Reddy | dd4c642 | 2021-02-24 23:44:46 -0700 | [diff] [blame] | 101 | #define CLK_CTRL_SRCSEL_MASK 0x7 |
Siva Durga Prasad Paladugu | ad76f8c | 2017-02-03 23:56:49 +0530 | [diff] [blame] | 102 | #define PLLCTRL_FBDIV_MASK 0x7f00 |
| 103 | #define PLLCTRL_FBDIV_SHIFT 8 |
| 104 | #define PLLCTRL_RESET_MASK 1 |
| 105 | #define PLLCTRL_RESET_SHIFT 0 |
| 106 | #define PLLCTRL_BYPASS_MASK 0x8 |
| 107 | #define PLLCTRL_BYPASS_SHFT 3 |
| 108 | #define PLLCTRL_POST_SRC_SHFT 24 |
| 109 | #define PLLCTRL_POST_SRC_MASK (0x7 << PLLCTRL_POST_SRC_SHFT) |
Vipul Kumar | b4f0158 | 2018-06-27 10:44:45 +0530 | [diff] [blame] | 110 | #define PLLCTRL_PRE_SRC_SHFT 20 |
| 111 | #define PLLCTRL_PRE_SRC_MASK (0x7 << PLLCTRL_PRE_SRC_SHFT) |
Siva Durga Prasad Paladugu | ad76f8c | 2017-02-03 23:56:49 +0530 | [diff] [blame] | 112 | |
| 113 | |
| 114 | #define NUM_MIO_PINS 77 |
| 115 | |
| 116 | enum zynqmp_clk { |
| 117 | iopll, rpll, |
| 118 | apll, dpll, vpll, |
| 119 | iopll_to_fpd, rpll_to_fpd, apll_to_lpd, dpll_to_lpd, vpll_to_lpd, |
| 120 | acpu, acpu_half, |
| 121 | dbg_fpd, dbg_lpd, dbg_trace, dbg_tstmp, |
| 122 | dp_video_ref, dp_audio_ref, |
| 123 | dp_stc_ref, gdma_ref, dpdma_ref, |
| 124 | ddr_ref, sata_ref, pcie_ref, |
| 125 | gpu_ref, gpu_pp0_ref, gpu_pp1_ref, |
| 126 | topsw_main, topsw_lsbus, |
| 127 | gtgref0_ref, |
| 128 | lpd_switch, lpd_lsbus, |
| 129 | usb0_bus_ref, usb1_bus_ref, usb3_dual_ref, usb0, usb1, |
| 130 | cpu_r5, cpu_r5_core, |
| 131 | csu_spb, csu_pll, pcap, |
| 132 | iou_switch, |
| 133 | gem_tsu_ref, gem_tsu, |
T Karthik Reddy | dd4c642 | 2021-02-24 23:44:46 -0700 | [diff] [blame] | 134 | gem0_tx, gem1_tx, gem2_tx, gem3_tx, |
Michal Simek | 0e789d2 | 2021-10-29 13:13:38 +0200 | [diff] [blame] | 135 | gem0_rx, gem1_rx, gem2_rx, gem3_rx, |
Siva Durga Prasad Paladugu | ad76f8c | 2017-02-03 23:56:49 +0530 | [diff] [blame] | 136 | qspi_ref, |
| 137 | sdio0_ref, sdio1_ref, |
| 138 | uart0_ref, uart1_ref, |
| 139 | spi0_ref, spi1_ref, |
| 140 | nand_ref, |
| 141 | i2c0_ref, i2c1_ref, can0_ref, can1_ref, can0, can1, |
| 142 | dll_ref, |
| 143 | adma_ref, |
| 144 | timestamp_ref, |
| 145 | ams_ref, |
| 146 | pl0, pl1, pl2, pl3, |
| 147 | wdt, |
Michal Simek | 0e789d2 | 2021-10-29 13:13:38 +0200 | [diff] [blame] | 148 | gem0_ref = 104, |
| 149 | gem1_ref, gem2_ref, gem3_ref, |
Siva Durga Prasad Paladugu | ad76f8c | 2017-02-03 23:56:49 +0530 | [diff] [blame] | 150 | clk_max, |
| 151 | }; |
| 152 | |
| 153 | static const char * const clk_names[clk_max] = { |
| 154 | "iopll", "rpll", "apll", "dpll", |
| 155 | "vpll", "iopll_to_fpd", "rpll_to_fpd", |
| 156 | "apll_to_lpd", "dpll_to_lpd", "vpll_to_lpd", |
T Karthik Reddy | dd4c642 | 2021-02-24 23:44:46 -0700 | [diff] [blame] | 157 | "acpu", "acpu_half", "dbg_fpd", "dbg_lpd", |
Siva Durga Prasad Paladugu | ad76f8c | 2017-02-03 23:56:49 +0530 | [diff] [blame] | 158 | "dbg_trace", "dbg_tstmp", "dp_video_ref", |
| 159 | "dp_audio_ref", "dp_stc_ref", "gdma_ref", |
| 160 | "dpdma_ref", "ddr_ref", "sata_ref", "pcie_ref", |
| 161 | "gpu_ref", "gpu_pp0_ref", "gpu_pp1_ref", |
| 162 | "topsw_main", "topsw_lsbus", "gtgref0_ref", |
| 163 | "lpd_switch", "lpd_lsbus", "usb0_bus_ref", |
| 164 | "usb1_bus_ref", "usb3_dual_ref", "usb0", |
| 165 | "usb1", "cpu_r5", "cpu_r5_core", "csu_spb", |
| 166 | "csu_pll", "pcap", "iou_switch", "gem_tsu_ref", |
Michal Simek | 0e789d2 | 2021-10-29 13:13:38 +0200 | [diff] [blame] | 167 | "gem_tsu", "gem0_tx", "gem1_tx", "gem2_tx", |
| 168 | "gem3_tx", "gem0_rx", "gem1_rx", "gem2_rx", |
| 169 | "gem3_rx", "qspi_ref", "sdio0_ref", "sdio1_ref", |
Siva Durga Prasad Paladugu | ad76f8c | 2017-02-03 23:56:49 +0530 | [diff] [blame] | 170 | "uart0_ref", "uart1_ref", "spi0_ref", |
| 171 | "spi1_ref", "nand_ref", "i2c0_ref", "i2c1_ref", |
| 172 | "can0_ref", "can1_ref", "can0", "can1", |
| 173 | "dll_ref", "adma_ref", "timestamp_ref", |
Michal Simek | 0e789d2 | 2021-10-29 13:13:38 +0200 | [diff] [blame] | 174 | "ams_ref", "pl0", "pl1", "pl2", "pl3", "wdt", |
| 175 | NULL, NULL, NULL, NULL, |
| 176 | NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, |
| 177 | NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, |
| 178 | NULL, NULL, NULL, NULL, "gem0_ref", "gem1_ref", "gem2_ref", "gem3_ref", |
Siva Durga Prasad Paladugu | ad76f8c | 2017-02-03 23:56:49 +0530 | [diff] [blame] | 179 | }; |
| 180 | |
T Karthik Reddy | dd4c642 | 2021-02-24 23:44:46 -0700 | [diff] [blame] | 181 | static const u32 pll_src[][4] = { |
| 182 | {apll, 0xff, dpll, vpll}, /* acpu */ |
| 183 | {dpll, vpll, 0xff, 0xff}, /* ddr_ref */ |
| 184 | {rpll, iopll, 0xff, 0xff}, /* dll_ref */ |
| 185 | {iopll, 0xff, rpll, dpll_to_lpd}, /* gem_tsu_ref */ |
| 186 | {iopll, 0xff, rpll, dpll}, /* peripheral */ |
| 187 | {apll, 0xff, iopll_to_fpd, dpll}, /* wdt */ |
| 188 | {iopll_to_fpd, 0xff, dpll, apll}, /* dbg_fpd */ |
| 189 | {iopll, 0xff, rpll, dpll_to_lpd}, /* timestamp_ref */ |
| 190 | {iopll_to_fpd, 0xff, apll, dpll}, /* sata_ref */ |
| 191 | {iopll_to_fpd, 0xff, rpll_to_fpd, dpll},/* pcie_ref */ |
| 192 | {iopll_to_fpd, 0xff, vpll, dpll}, /* gpu_ref */ |
| 193 | {apll, 0xff, vpll, dpll}, /* topsw_main_ref */ |
| 194 | {rpll, 0xff, iopll, dpll_to_lpd}, /* cpu_r5_ref */ |
| 195 | }; |
| 196 | |
| 197 | enum zynqmp_clk_pll_src { |
| 198 | ACPU_CLK_SRC = 0, |
| 199 | DDR_CLK_SRC, |
| 200 | DLL_CLK_SRC, |
| 201 | GEM_TSU_CLK_SRC, |
| 202 | PERI_CLK_SRC, |
| 203 | WDT_CLK_SRC, |
| 204 | DBG_FPD_CLK_SRC, |
| 205 | TIMESTAMP_CLK_SRC, |
| 206 | SATA_CLK_SRC, |
| 207 | PCIE_CLK_SRC, |
| 208 | GPU_CLK_SRC, |
| 209 | TOPSW_MAIN_CLK_SRC, |
| 210 | CPU_R5_CLK_SRC |
| 211 | }; |
| 212 | |
Siva Durga Prasad Paladugu | ad76f8c | 2017-02-03 23:56:49 +0530 | [diff] [blame] | 213 | struct zynqmp_clk_priv { |
| 214 | unsigned long ps_clk_freq; |
| 215 | unsigned long video_clk; |
| 216 | unsigned long pss_alt_ref_clk; |
| 217 | unsigned long gt_crx_ref_clk; |
| 218 | unsigned long aux_ref_clk; |
| 219 | }; |
| 220 | |
| 221 | static u32 zynqmp_clk_get_register(enum zynqmp_clk id) |
Siva Durga Prasad Paladugu | 128ec1f | 2016-11-15 16:15:41 +0530 | [diff] [blame] | 222 | { |
Siva Durga Prasad Paladugu | ad76f8c | 2017-02-03 23:56:49 +0530 | [diff] [blame] | 223 | switch (id) { |
| 224 | case iopll: |
| 225 | return CRL_APB_IOPLL_CTRL; |
| 226 | case rpll: |
| 227 | return CRL_APB_RPLL_CTRL; |
| 228 | case apll: |
| 229 | return CRF_APB_APLL_CTRL; |
| 230 | case dpll: |
| 231 | return CRF_APB_DPLL_CTRL; |
| 232 | case vpll: |
| 233 | return CRF_APB_VPLL_CTRL; |
| 234 | case acpu: |
| 235 | return CRF_APB_ACPU_CTRL; |
T Karthik Reddy | dd4c642 | 2021-02-24 23:44:46 -0700 | [diff] [blame] | 236 | case dbg_fpd: |
| 237 | return CRF_APB_DBG_FPD_CTRL; |
| 238 | case dbg_trace: |
| 239 | return CRF_APB_DBG_TRACE_CTRL; |
| 240 | case dbg_tstmp: |
| 241 | return CRF_APB_DBG_TSTMP_CTRL; |
Michal Simek | 6f735e4 | 2022-03-29 13:13:56 +0200 | [diff] [blame] | 242 | case dp_video_ref: |
| 243 | return CRF_APB_DP_VIDEO_REF_CTRL; |
| 244 | case dp_audio_ref: |
| 245 | return CRF_APB_DP_AUDIO_REF_CTRL; |
| 246 | case dp_stc_ref: |
| 247 | return CRF_APB_DP_STC_REF_CTRL; |
T Karthik Reddy | dd4c642 | 2021-02-24 23:44:46 -0700 | [diff] [blame] | 248 | case gpu_ref ... gpu_pp1_ref: |
| 249 | return CRF_APB_GPU_REF_CTRL; |
Siva Durga Prasad Paladugu | ad76f8c | 2017-02-03 23:56:49 +0530 | [diff] [blame] | 250 | case ddr_ref: |
| 251 | return CRF_APB_DDR_CTRL; |
T Karthik Reddy | dd4c642 | 2021-02-24 23:44:46 -0700 | [diff] [blame] | 252 | case sata_ref: |
| 253 | return CRF_APB_SATA_REF_CTRL; |
| 254 | case pcie_ref: |
| 255 | return CRF_APB_PCIE_REF_CTRL; |
| 256 | case gdma_ref: |
| 257 | return CRF_APB_GDMA_REF_CTRL; |
| 258 | case dpdma_ref: |
| 259 | return CRF_APB_DPDMA_REF_CTRL; |
| 260 | case topsw_main: |
| 261 | return CRF_APB_TOPSW_MAIN_CTRL; |
| 262 | case topsw_lsbus: |
| 263 | return CRF_APB_TOPSW_LSBUS_CTRL; |
| 264 | case lpd_switch: |
| 265 | return CRL_APB_LPD_SWITCH_CTRL; |
| 266 | case lpd_lsbus: |
| 267 | return CRL_APB_LPD_LSBUS_CTRL; |
Siva Durga Prasad Paladugu | ad76f8c | 2017-02-03 23:56:49 +0530 | [diff] [blame] | 268 | case qspi_ref: |
| 269 | return CRL_APB_QSPI_REF_CTRL; |
T Karthik Reddy | a72a6ae | 2021-02-03 03:10:45 -0700 | [diff] [blame] | 270 | case usb3_dual_ref: |
| 271 | return CRL_APB_USB3_DUAL_REF_CTRL; |
T Karthik Reddy | dd4c642 | 2021-02-24 23:44:46 -0700 | [diff] [blame] | 272 | case gem_tsu_ref: |
| 273 | return CRL_APB_GEM_TSU_REF_CTRL; |
Michal Simek | 0e789d2 | 2021-10-29 13:13:38 +0200 | [diff] [blame] | 274 | case gem0_tx: |
Siva Durga Prasad Paladugu | ad76f8c | 2017-02-03 23:56:49 +0530 | [diff] [blame] | 275 | case gem0_ref: |
| 276 | return CRL_APB_GEM0_REF_CTRL; |
Michal Simek | 0e789d2 | 2021-10-29 13:13:38 +0200 | [diff] [blame] | 277 | case gem1_tx: |
Siva Durga Prasad Paladugu | ad76f8c | 2017-02-03 23:56:49 +0530 | [diff] [blame] | 278 | case gem1_ref: |
| 279 | return CRL_APB_GEM1_REF_CTRL; |
Michal Simek | 0e789d2 | 2021-10-29 13:13:38 +0200 | [diff] [blame] | 280 | case gem2_tx: |
Siva Durga Prasad Paladugu | ad76f8c | 2017-02-03 23:56:49 +0530 | [diff] [blame] | 281 | case gem2_ref: |
| 282 | return CRL_APB_GEM2_REF_CTRL; |
Michal Simek | 0e789d2 | 2021-10-29 13:13:38 +0200 | [diff] [blame] | 283 | case gem3_tx: |
Siva Durga Prasad Paladugu | ad76f8c | 2017-02-03 23:56:49 +0530 | [diff] [blame] | 284 | case gem3_ref: |
| 285 | return CRL_APB_GEM3_REF_CTRL; |
T Karthik Reddy | a72a6ae | 2021-02-03 03:10:45 -0700 | [diff] [blame] | 286 | case usb0_bus_ref: |
| 287 | return CRL_APB_USB0_BUS_REF_CTRL; |
| 288 | case usb1_bus_ref: |
| 289 | return CRL_APB_USB1_BUS_REF_CTRL; |
T Karthik Reddy | dd4c642 | 2021-02-24 23:44:46 -0700 | [diff] [blame] | 290 | case cpu_r5: |
| 291 | return CRL_APB_CPU_R5_CTRL; |
Siva Durga Prasad Paladugu | ad76f8c | 2017-02-03 23:56:49 +0530 | [diff] [blame] | 292 | case uart0_ref: |
| 293 | return CRL_APB_UART0_REF_CTRL; |
| 294 | case uart1_ref: |
| 295 | return CRL_APB_UART1_REF_CTRL; |
| 296 | case sdio0_ref: |
| 297 | return CRL_APB_SDIO0_REF_CTRL; |
| 298 | case sdio1_ref: |
| 299 | return CRL_APB_SDIO1_REF_CTRL; |
| 300 | case spi0_ref: |
| 301 | return CRL_APB_SPI0_REF_CTRL; |
| 302 | case spi1_ref: |
| 303 | return CRL_APB_SPI1_REF_CTRL; |
| 304 | case nand_ref: |
| 305 | return CRL_APB_NAND_REF_CTRL; |
| 306 | case i2c0_ref: |
| 307 | return CRL_APB_I2C0_REF_CTRL; |
| 308 | case i2c1_ref: |
| 309 | return CRL_APB_I2C1_REF_CTRL; |
| 310 | case can0_ref: |
| 311 | return CRL_APB_CAN0_REF_CTRL; |
| 312 | case can1_ref: |
| 313 | return CRL_APB_CAN1_REF_CTRL; |
T Karthik Reddy | dd4c642 | 2021-02-24 23:44:46 -0700 | [diff] [blame] | 314 | case dll_ref: |
| 315 | return CRL_APB_DLL_REF_CTRL; |
| 316 | case adma_ref: |
| 317 | return CRL_APB_ADMA_REF_CTRL; |
| 318 | case timestamp_ref: |
| 319 | return CRL_APB_TIMESTAMP_REF_CTRL; |
| 320 | case ams_ref: |
| 321 | return CRL_APB_AMS_REF_CTRL; |
Vipul Kumar | a79b590 | 2018-03-07 14:52:44 +0530 | [diff] [blame] | 322 | case pl0: |
| 323 | return CRL_APB_PL0_REF_CTRL; |
| 324 | case pl1: |
| 325 | return CRL_APB_PL1_REF_CTRL; |
| 326 | case pl2: |
| 327 | return CRL_APB_PL2_REF_CTRL; |
| 328 | case pl3: |
| 329 | return CRL_APB_PL3_REF_CTRL; |
| 330 | case wdt: |
| 331 | return CRF_APB_TOPSW_LSBUS_CTRL; |
| 332 | case iopll_to_fpd: |
| 333 | return CRL_APB_IOPLL_TO_FPD_CTRL; |
Siva Durga Prasad Paladugu | ad76f8c | 2017-02-03 23:56:49 +0530 | [diff] [blame] | 334 | default: |
| 335 | debug("Invalid clk id%d\n", id); |
| 336 | } |
| 337 | return 0; |
| 338 | } |
Siva Durga Prasad Paladugu | 128ec1f | 2016-11-15 16:15:41 +0530 | [diff] [blame] | 339 | |
Siva Durga Prasad Paladugu | ad76f8c | 2017-02-03 23:56:49 +0530 | [diff] [blame] | 340 | static ulong zynqmp_clk_get_pll_src(ulong clk_ctrl, |
| 341 | struct zynqmp_clk_priv *priv, |
| 342 | bool is_pre_src) |
| 343 | { |
| 344 | u32 src_sel; |
| 345 | |
| 346 | if (is_pre_src) |
Vipul Kumar | b4f0158 | 2018-06-27 10:44:45 +0530 | [diff] [blame] | 347 | src_sel = (clk_ctrl & PLLCTRL_PRE_SRC_MASK) >> |
| 348 | PLLCTRL_PRE_SRC_SHFT; |
Siva Durga Prasad Paladugu | ad76f8c | 2017-02-03 23:56:49 +0530 | [diff] [blame] | 349 | else |
| 350 | src_sel = (clk_ctrl & PLLCTRL_POST_SRC_MASK) >> |
| 351 | PLLCTRL_POST_SRC_SHFT; |
| 352 | |
| 353 | switch (src_sel) { |
| 354 | case 4: |
| 355 | return priv->video_clk; |
| 356 | case 5: |
| 357 | return priv->pss_alt_ref_clk; |
| 358 | case 6: |
| 359 | return priv->aux_ref_clk; |
| 360 | case 7: |
| 361 | return priv->gt_crx_ref_clk; |
| 362 | case 0 ... 3: |
| 363 | default: |
| 364 | return priv->ps_clk_freq; |
| 365 | } |
| 366 | } |
| 367 | |
| 368 | static ulong zynqmp_clk_get_pll_rate(struct zynqmp_clk_priv *priv, |
| 369 | enum zynqmp_clk id) |
| 370 | { |
| 371 | u32 clk_ctrl, reset, mul; |
| 372 | ulong freq; |
| 373 | int ret; |
| 374 | |
| 375 | ret = zynqmp_mmio_read(zynqmp_clk_get_register(id), &clk_ctrl); |
Siva Durga Prasad Paladugu | 154799a | 2017-04-13 16:59:38 +0530 | [diff] [blame] | 376 | if (ret) { |
| 377 | printf("%s mio read fail\n", __func__); |
| 378 | return -EIO; |
| 379 | } |
Siva Durga Prasad Paladugu | ad76f8c | 2017-02-03 23:56:49 +0530 | [diff] [blame] | 380 | |
| 381 | if (clk_ctrl & PLLCTRL_BYPASS_MASK) |
| 382 | freq = zynqmp_clk_get_pll_src(clk_ctrl, priv, 0); |
| 383 | else |
| 384 | freq = zynqmp_clk_get_pll_src(clk_ctrl, priv, 1); |
| 385 | |
| 386 | reset = (clk_ctrl & PLLCTRL_RESET_MASK) >> PLLCTRL_RESET_SHIFT; |
| 387 | if (reset && !(clk_ctrl & PLLCTRL_BYPASS_MASK)) |
| 388 | return 0; |
| 389 | |
| 390 | mul = (clk_ctrl & PLLCTRL_FBDIV_MASK) >> PLLCTRL_FBDIV_SHIFT; |
| 391 | |
| 392 | freq *= mul; |
| 393 | |
| 394 | if (clk_ctrl & (1 << 16)) |
| 395 | freq /= 2; |
| 396 | |
| 397 | return freq; |
| 398 | } |
| 399 | |
| 400 | static ulong zynqmp_clk_get_cpu_rate(struct zynqmp_clk_priv *priv, |
| 401 | enum zynqmp_clk id) |
| 402 | { |
T Karthik Reddy | dd4c642 | 2021-02-24 23:44:46 -0700 | [diff] [blame] | 403 | u32 clk_ctrl, div, srcsel; |
Siva Durga Prasad Paladugu | ad76f8c | 2017-02-03 23:56:49 +0530 | [diff] [blame] | 404 | enum zynqmp_clk pll; |
| 405 | int ret; |
Siva Durga Prasad Paladugu | 154799a | 2017-04-13 16:59:38 +0530 | [diff] [blame] | 406 | unsigned long pllrate; |
Siva Durga Prasad Paladugu | ad76f8c | 2017-02-03 23:56:49 +0530 | [diff] [blame] | 407 | |
| 408 | ret = zynqmp_mmio_read(CRF_APB_ACPU_CTRL, &clk_ctrl); |
Siva Durga Prasad Paladugu | 154799a | 2017-04-13 16:59:38 +0530 | [diff] [blame] | 409 | if (ret) { |
| 410 | printf("%s mio read fail\n", __func__); |
| 411 | return -EIO; |
| 412 | } |
Siva Durga Prasad Paladugu | ad76f8c | 2017-02-03 23:56:49 +0530 | [diff] [blame] | 413 | |
| 414 | div = (clk_ctrl & CLK_CTRL_DIV0_MASK) >> CLK_CTRL_DIV0_SHIFT; |
| 415 | |
T Karthik Reddy | dd4c642 | 2021-02-24 23:44:46 -0700 | [diff] [blame] | 416 | srcsel = clk_ctrl & CLK_CTRL_SRCSEL_MASK; |
| 417 | pll = pll_src[ACPU_CLK_SRC][srcsel]; |
Siva Durga Prasad Paladugu | 154799a | 2017-04-13 16:59:38 +0530 | [diff] [blame] | 418 | pllrate = zynqmp_clk_get_pll_rate(priv, pll); |
| 419 | if (IS_ERR_VALUE(pllrate)) |
| 420 | return pllrate; |
Siva Durga Prasad Paladugu | ad76f8c | 2017-02-03 23:56:49 +0530 | [diff] [blame] | 421 | |
Siva Durga Prasad Paladugu | 154799a | 2017-04-13 16:59:38 +0530 | [diff] [blame] | 422 | return DIV_ROUND_CLOSEST(pllrate, div); |
Siva Durga Prasad Paladugu | ad76f8c | 2017-02-03 23:56:49 +0530 | [diff] [blame] | 423 | } |
| 424 | |
| 425 | static ulong zynqmp_clk_get_ddr_rate(struct zynqmp_clk_priv *priv) |
| 426 | { |
T Karthik Reddy | dd4c642 | 2021-02-24 23:44:46 -0700 | [diff] [blame] | 427 | u32 clk_ctrl, div, srcsel; |
Siva Durga Prasad Paladugu | ad76f8c | 2017-02-03 23:56:49 +0530 | [diff] [blame] | 428 | enum zynqmp_clk pll; |
| 429 | int ret; |
Siva Durga Prasad Paladugu | 154799a | 2017-04-13 16:59:38 +0530 | [diff] [blame] | 430 | ulong pllrate; |
Siva Durga Prasad Paladugu | ad76f8c | 2017-02-03 23:56:49 +0530 | [diff] [blame] | 431 | |
| 432 | ret = zynqmp_mmio_read(CRF_APB_DDR_CTRL, &clk_ctrl); |
Siva Durga Prasad Paladugu | 154799a | 2017-04-13 16:59:38 +0530 | [diff] [blame] | 433 | if (ret) { |
| 434 | printf("%s mio read fail\n", __func__); |
| 435 | return -EIO; |
| 436 | } |
Siva Durga Prasad Paladugu | ad76f8c | 2017-02-03 23:56:49 +0530 | [diff] [blame] | 437 | |
| 438 | div = (clk_ctrl & CLK_CTRL_DIV0_MASK) >> CLK_CTRL_DIV0_SHIFT; |
| 439 | |
T Karthik Reddy | dd4c642 | 2021-02-24 23:44:46 -0700 | [diff] [blame] | 440 | srcsel = clk_ctrl & CLK_CTRL_SRCSEL_MASK; |
| 441 | pll = pll_src[DDR_CLK_SRC][srcsel]; |
Siva Durga Prasad Paladugu | 154799a | 2017-04-13 16:59:38 +0530 | [diff] [blame] | 442 | pllrate = zynqmp_clk_get_pll_rate(priv, pll); |
| 443 | if (IS_ERR_VALUE(pllrate)) |
| 444 | return pllrate; |
Siva Durga Prasad Paladugu | ad76f8c | 2017-02-03 23:56:49 +0530 | [diff] [blame] | 445 | |
Siva Durga Prasad Paladugu | 154799a | 2017-04-13 16:59:38 +0530 | [diff] [blame] | 446 | return DIV_ROUND_CLOSEST(pllrate, div); |
Siva Durga Prasad Paladugu | ad76f8c | 2017-02-03 23:56:49 +0530 | [diff] [blame] | 447 | } |
| 448 | |
T Karthik Reddy | dd4c642 | 2021-02-24 23:44:46 -0700 | [diff] [blame] | 449 | static ulong zynqmp_clk_get_dll_rate(struct zynqmp_clk_priv *priv) |
| 450 | { |
| 451 | u32 clk_ctrl, srcsel; |
| 452 | enum zynqmp_clk pll; |
| 453 | ulong pllrate; |
| 454 | int ret; |
| 455 | |
| 456 | ret = zynqmp_mmio_read(CRL_APB_DLL_REF_CTRL, &clk_ctrl); |
| 457 | if (ret) { |
| 458 | printf("%s mio read fail\n", __func__); |
| 459 | return -EIO; |
| 460 | } |
| 461 | |
| 462 | srcsel = clk_ctrl & CLK_CTRL_SRCSEL_MASK; |
| 463 | pll = pll_src[DLL_CLK_SRC][srcsel]; |
| 464 | pllrate = zynqmp_clk_get_pll_rate(priv, pll); |
| 465 | if (IS_ERR_VALUE(pllrate)) |
| 466 | return pllrate; |
| 467 | |
| 468 | return pllrate; |
| 469 | } |
| 470 | |
Siva Durga Prasad Paladugu | ad76f8c | 2017-02-03 23:56:49 +0530 | [diff] [blame] | 471 | static ulong zynqmp_clk_get_peripheral_rate(struct zynqmp_clk_priv *priv, |
T Karthik Reddy | dd4c642 | 2021-02-24 23:44:46 -0700 | [diff] [blame] | 472 | enum zynqmp_clk id, bool two_divs) |
Siva Durga Prasad Paladugu | ad76f8c | 2017-02-03 23:56:49 +0530 | [diff] [blame] | 473 | { |
| 474 | enum zynqmp_clk pll; |
T Karthik Reddy | dd4c642 | 2021-02-24 23:44:46 -0700 | [diff] [blame] | 475 | u32 clk_ctrl, div0, srcsel; |
Siva Durga Prasad Paladugu | ad76f8c | 2017-02-03 23:56:49 +0530 | [diff] [blame] | 476 | u32 div1 = 1; |
| 477 | int ret; |
Siva Durga Prasad Paladugu | 154799a | 2017-04-13 16:59:38 +0530 | [diff] [blame] | 478 | ulong pllrate; |
Siva Durga Prasad Paladugu | ad76f8c | 2017-02-03 23:56:49 +0530 | [diff] [blame] | 479 | |
| 480 | ret = zynqmp_mmio_read(zynqmp_clk_get_register(id), &clk_ctrl); |
Siva Durga Prasad Paladugu | 154799a | 2017-04-13 16:59:38 +0530 | [diff] [blame] | 481 | if (ret) { |
| 482 | printf("%s mio read fail\n", __func__); |
| 483 | return -EIO; |
| 484 | } |
Siva Durga Prasad Paladugu | ad76f8c | 2017-02-03 23:56:49 +0530 | [diff] [blame] | 485 | |
| 486 | div0 = (clk_ctrl & CLK_CTRL_DIV0_MASK) >> CLK_CTRL_DIV0_SHIFT; |
| 487 | if (!div0) |
| 488 | div0 = 1; |
| 489 | |
| 490 | if (two_divs) { |
| 491 | div1 = (clk_ctrl & CLK_CTRL_DIV1_MASK) >> CLK_CTRL_DIV1_SHIFT; |
| 492 | if (!div1) |
| 493 | div1 = 1; |
| 494 | } |
T Karthik Reddy | dd4c642 | 2021-02-24 23:44:46 -0700 | [diff] [blame] | 495 | srcsel = clk_ctrl & CLK_CTRL_SRCSEL_MASK; |
Siva Durga Prasad Paladugu | ad76f8c | 2017-02-03 23:56:49 +0530 | [diff] [blame] | 496 | |
T Karthik Reddy | dd4c642 | 2021-02-24 23:44:46 -0700 | [diff] [blame] | 497 | if (id == gem_tsu_ref) |
| 498 | pll = pll_src[GEM_TSU_CLK_SRC][srcsel]; |
| 499 | else |
| 500 | pll = pll_src[PERI_CLK_SRC][srcsel]; |
| 501 | |
Siva Durga Prasad Paladugu | 154799a | 2017-04-13 16:59:38 +0530 | [diff] [blame] | 502 | pllrate = zynqmp_clk_get_pll_rate(priv, pll); |
| 503 | if (IS_ERR_VALUE(pllrate)) |
| 504 | return pllrate; |
Siva Durga Prasad Paladugu | ad76f8c | 2017-02-03 23:56:49 +0530 | [diff] [blame] | 505 | |
| 506 | return |
| 507 | DIV_ROUND_CLOSEST( |
Siva Durga Prasad Paladugu | 154799a | 2017-04-13 16:59:38 +0530 | [diff] [blame] | 508 | DIV_ROUND_CLOSEST(pllrate, div0), div1); |
Siva Durga Prasad Paladugu | ad76f8c | 2017-02-03 23:56:49 +0530 | [diff] [blame] | 509 | } |
| 510 | |
T Karthik Reddy | dd4c642 | 2021-02-24 23:44:46 -0700 | [diff] [blame] | 511 | static ulong zynqmp_clk_get_crf_crl_rate(struct zynqmp_clk_priv *priv, |
| 512 | enum zynqmp_clk id, bool two_divs) |
Vipul Kumar | a79b590 | 2018-03-07 14:52:44 +0530 | [diff] [blame] | 513 | { |
| 514 | enum zynqmp_clk pll; |
T Karthik Reddy | dd4c642 | 2021-02-24 23:44:46 -0700 | [diff] [blame] | 515 | u32 clk_ctrl, div0, srcsel; |
Vipul Kumar | a79b590 | 2018-03-07 14:52:44 +0530 | [diff] [blame] | 516 | u32 div1 = 1; |
| 517 | int ret; |
| 518 | ulong pllrate; |
| 519 | |
| 520 | ret = zynqmp_mmio_read(zynqmp_clk_get_register(id), &clk_ctrl); |
| 521 | if (ret) { |
| 522 | printf("%d %s mio read fail\n", __LINE__, __func__); |
| 523 | return -EIO; |
| 524 | } |
| 525 | |
| 526 | div0 = (clk_ctrl & CLK_CTRL_DIV0_MASK) >> CLK_CTRL_DIV0_SHIFT; |
| 527 | if (!div0) |
| 528 | div0 = 1; |
T Karthik Reddy | dd4c642 | 2021-02-24 23:44:46 -0700 | [diff] [blame] | 529 | srcsel = clk_ctrl & CLK_CTRL_SRCSEL_MASK; |
Vipul Kumar | a79b590 | 2018-03-07 14:52:44 +0530 | [diff] [blame] | 530 | |
T Karthik Reddy | dd4c642 | 2021-02-24 23:44:46 -0700 | [diff] [blame] | 531 | switch (id) { |
| 532 | case wdt: |
| 533 | case dbg_trace: |
| 534 | case topsw_lsbus: |
| 535 | pll = pll_src[WDT_CLK_SRC][srcsel]; |
| 536 | break; |
| 537 | case dbg_fpd: |
| 538 | case dbg_tstmp: |
| 539 | pll = pll_src[DBG_FPD_CLK_SRC][srcsel]; |
| 540 | break; |
| 541 | case timestamp_ref: |
| 542 | pll = pll_src[TIMESTAMP_CLK_SRC][srcsel]; |
| 543 | break; |
| 544 | case sata_ref: |
| 545 | pll = pll_src[SATA_CLK_SRC][srcsel]; |
| 546 | break; |
| 547 | case pcie_ref: |
| 548 | pll = pll_src[PCIE_CLK_SRC][srcsel]; |
| 549 | break; |
| 550 | case gpu_ref ... gpu_pp1_ref: |
| 551 | pll = pll_src[GPU_CLK_SRC][srcsel]; |
| 552 | break; |
| 553 | case gdma_ref: |
| 554 | case dpdma_ref: |
| 555 | case topsw_main: |
| 556 | pll = pll_src[TOPSW_MAIN_CLK_SRC][srcsel]; |
| 557 | break; |
| 558 | case cpu_r5: |
| 559 | case ams_ref: |
| 560 | case adma_ref: |
| 561 | case lpd_lsbus: |
| 562 | case lpd_switch: |
| 563 | pll = pll_src[CPU_R5_CLK_SRC][srcsel]; |
| 564 | break; |
| 565 | default: |
| 566 | return -ENXIO; |
| 567 | } |
Vipul Kumar | a79b590 | 2018-03-07 14:52:44 +0530 | [diff] [blame] | 568 | if (two_divs) { |
| 569 | ret = zynqmp_mmio_read(zynqmp_clk_get_register(pll), &clk_ctrl); |
| 570 | if (ret) { |
| 571 | printf("%d %s mio read fail\n", __LINE__, __func__); |
| 572 | return -EIO; |
| 573 | } |
| 574 | div1 = (clk_ctrl & CLK_CTRL_DIV0_MASK) >> CLK_CTRL_DIV0_SHIFT; |
| 575 | if (!div1) |
| 576 | div1 = 1; |
| 577 | } |
| 578 | |
| 579 | if (pll == iopll_to_fpd) |
| 580 | pll = iopll; |
| 581 | |
| 582 | pllrate = zynqmp_clk_get_pll_rate(priv, pll); |
| 583 | if (IS_ERR_VALUE(pllrate)) |
| 584 | return pllrate; |
| 585 | |
| 586 | return |
| 587 | DIV_ROUND_CLOSEST( |
| 588 | DIV_ROUND_CLOSEST(pllrate, div0), div1); |
| 589 | } |
| 590 | |
Siva Durga Prasad Paladugu | ad76f8c | 2017-02-03 23:56:49 +0530 | [diff] [blame] | 591 | static unsigned long zynqmp_clk_calc_peripheral_two_divs(ulong rate, |
| 592 | ulong pll_rate, |
| 593 | u32 *div0, u32 *div1) |
| 594 | { |
| 595 | long new_err, best_err = (long)(~0UL >> 1); |
| 596 | ulong new_rate, best_rate = 0; |
| 597 | u32 d0, d1; |
| 598 | |
| 599 | for (d0 = 1; d0 <= ZYNQ_CLK_MAXDIV; d0++) { |
| 600 | for (d1 = 1; d1 <= ZYNQ_CLK_MAXDIV >> 1; d1++) { |
| 601 | new_rate = DIV_ROUND_CLOSEST( |
| 602 | DIV_ROUND_CLOSEST(pll_rate, d0), d1); |
| 603 | new_err = abs(new_rate - rate); |
| 604 | |
| 605 | if (new_err < best_err) { |
| 606 | *div0 = d0; |
| 607 | *div1 = d1; |
| 608 | best_err = new_err; |
| 609 | best_rate = new_rate; |
| 610 | } |
Siva Durga Prasad Paladugu | 128ec1f | 2016-11-15 16:15:41 +0530 | [diff] [blame] | 611 | } |
| 612 | } |
| 613 | |
Siva Durga Prasad Paladugu | ad76f8c | 2017-02-03 23:56:49 +0530 | [diff] [blame] | 614 | return best_rate; |
Siva Durga Prasad Paladugu | 128ec1f | 2016-11-15 16:15:41 +0530 | [diff] [blame] | 615 | } |
| 616 | |
Siva Durga Prasad Paladugu | ad76f8c | 2017-02-03 23:56:49 +0530 | [diff] [blame] | 617 | static ulong zynqmp_clk_set_peripheral_rate(struct zynqmp_clk_priv *priv, |
| 618 | enum zynqmp_clk id, ulong rate, |
| 619 | bool two_divs) |
Siva Durga Prasad Paladugu | 128ec1f | 2016-11-15 16:15:41 +0530 | [diff] [blame] | 620 | { |
Siva Durga Prasad Paladugu | ad76f8c | 2017-02-03 23:56:49 +0530 | [diff] [blame] | 621 | enum zynqmp_clk pll; |
| 622 | u32 clk_ctrl, div0 = 0, div1 = 0; |
| 623 | ulong pll_rate, new_rate; |
T Karthik Reddy | dd4c642 | 2021-02-24 23:44:46 -0700 | [diff] [blame] | 624 | u32 reg, srcsel; |
Siva Durga Prasad Paladugu | ad76f8c | 2017-02-03 23:56:49 +0530 | [diff] [blame] | 625 | int ret; |
| 626 | u32 mask; |
| 627 | |
| 628 | reg = zynqmp_clk_get_register(id); |
| 629 | ret = zynqmp_mmio_read(reg, &clk_ctrl); |
Siva Durga Prasad Paladugu | 154799a | 2017-04-13 16:59:38 +0530 | [diff] [blame] | 630 | if (ret) { |
| 631 | printf("%s mio read fail\n", __func__); |
| 632 | return -EIO; |
| 633 | } |
Siva Durga Prasad Paladugu | ad76f8c | 2017-02-03 23:56:49 +0530 | [diff] [blame] | 634 | |
T Karthik Reddy | dd4c642 | 2021-02-24 23:44:46 -0700 | [diff] [blame] | 635 | srcsel = clk_ctrl & CLK_CTRL_SRCSEL_MASK; |
| 636 | pll = pll_src[PERI_CLK_SRC][srcsel]; |
Siva Durga Prasad Paladugu | ad76f8c | 2017-02-03 23:56:49 +0530 | [diff] [blame] | 637 | pll_rate = zynqmp_clk_get_pll_rate(priv, pll); |
Siva Durga Prasad Paladugu | 154799a | 2017-04-13 16:59:38 +0530 | [diff] [blame] | 638 | if (IS_ERR_VALUE(pll_rate)) |
| 639 | return pll_rate; |
| 640 | |
Siva Durga Prasad Paladugu | ad76f8c | 2017-02-03 23:56:49 +0530 | [diff] [blame] | 641 | clk_ctrl &= ~CLK_CTRL_DIV0_MASK; |
| 642 | if (two_divs) { |
| 643 | clk_ctrl &= ~CLK_CTRL_DIV1_MASK; |
| 644 | new_rate = zynqmp_clk_calc_peripheral_two_divs(rate, pll_rate, |
| 645 | &div0, &div1); |
| 646 | clk_ctrl |= div1 << CLK_CTRL_DIV1_SHIFT; |
| 647 | } else { |
| 648 | div0 = DIV_ROUND_CLOSEST(pll_rate, rate); |
| 649 | if (div0 > ZYNQ_CLK_MAXDIV) |
| 650 | div0 = ZYNQ_CLK_MAXDIV; |
| 651 | new_rate = DIV_ROUND_CLOSEST(rate, div0); |
| 652 | } |
| 653 | clk_ctrl |= div0 << CLK_CTRL_DIV0_SHIFT; |
| 654 | |
| 655 | mask = (ZYNQ_CLK_MAXDIV << CLK_CTRL_DIV0_SHIFT) | |
| 656 | (ZYNQ_CLK_MAXDIV << CLK_CTRL_DIV1_SHIFT); |
| 657 | |
| 658 | ret = zynqmp_mmio_write(reg, mask, clk_ctrl); |
Siva Durga Prasad Paladugu | 154799a | 2017-04-13 16:59:38 +0530 | [diff] [blame] | 659 | if (ret) { |
| 660 | printf("%s mio write fail\n", __func__); |
| 661 | return -EIO; |
| 662 | } |
Siva Durga Prasad Paladugu | ad76f8c | 2017-02-03 23:56:49 +0530 | [diff] [blame] | 663 | |
| 664 | return new_rate; |
| 665 | } |
| 666 | |
| 667 | static ulong zynqmp_clk_get_rate(struct clk *clk) |
| 668 | { |
| 669 | struct zynqmp_clk_priv *priv = dev_get_priv(clk->dev); |
| 670 | enum zynqmp_clk id = clk->id; |
| 671 | bool two_divs = false; |
Siva Durga Prasad Paladugu | 128ec1f | 2016-11-15 16:15:41 +0530 | [diff] [blame] | 672 | |
| 673 | switch (id) { |
Siva Durga Prasad Paladugu | ad76f8c | 2017-02-03 23:56:49 +0530 | [diff] [blame] | 674 | case iopll ... vpll: |
| 675 | return zynqmp_clk_get_pll_rate(priv, id); |
| 676 | case acpu: |
| 677 | return zynqmp_clk_get_cpu_rate(priv, id); |
| 678 | case ddr_ref: |
| 679 | return zynqmp_clk_get_ddr_rate(priv); |
T Karthik Reddy | dd4c642 | 2021-02-24 23:44:46 -0700 | [diff] [blame] | 680 | case dll_ref: |
| 681 | return zynqmp_clk_get_dll_rate(priv); |
| 682 | case gem_tsu_ref: |
Michal Simek | 6f735e4 | 2022-03-29 13:13:56 +0200 | [diff] [blame] | 683 | case dp_video_ref ... dp_stc_ref: |
T Karthik Reddy | dd4c642 | 2021-02-24 23:44:46 -0700 | [diff] [blame] | 684 | case pl0 ... pl3: |
Siva Durga Prasad Paladugu | ad76f8c | 2017-02-03 23:56:49 +0530 | [diff] [blame] | 685 | case gem0_ref ... gem3_ref: |
Michal Simek | 0e789d2 | 2021-10-29 13:13:38 +0200 | [diff] [blame] | 686 | case gem0_tx ... gem3_tx: |
Siva Durga Prasad Paladugu | ad76f8c | 2017-02-03 23:56:49 +0530 | [diff] [blame] | 687 | case qspi_ref ... can1_ref: |
T Karthik Reddy | dd4c642 | 2021-02-24 23:44:46 -0700 | [diff] [blame] | 688 | case usb0_bus_ref ... usb3_dual_ref: |
Siva Durga Prasad Paladugu | ad76f8c | 2017-02-03 23:56:49 +0530 | [diff] [blame] | 689 | two_divs = true; |
| 690 | return zynqmp_clk_get_peripheral_rate(priv, id, two_divs); |
Vipul Kumar | a79b590 | 2018-03-07 14:52:44 +0530 | [diff] [blame] | 691 | case wdt: |
T Karthik Reddy | dd4c642 | 2021-02-24 23:44:46 -0700 | [diff] [blame] | 692 | case topsw_lsbus: |
| 693 | case sata_ref ... gpu_pp1_ref: |
Vipul Kumar | a79b590 | 2018-03-07 14:52:44 +0530 | [diff] [blame] | 694 | two_divs = true; |
Algapally Santosh Sagar | 71c5fdc | 2023-05-19 17:08:15 +0530 | [diff] [blame] | 695 | fallthrough; |
T Karthik Reddy | dd4c642 | 2021-02-24 23:44:46 -0700 | [diff] [blame] | 696 | case cpu_r5: |
| 697 | case dbg_fpd: |
| 698 | case ams_ref: |
| 699 | case adma_ref: |
| 700 | case lpd_lsbus: |
| 701 | case dbg_trace: |
| 702 | case dbg_tstmp: |
| 703 | case lpd_switch: |
| 704 | case topsw_main: |
| 705 | case timestamp_ref: |
| 706 | case gdma_ref ... dpdma_ref: |
| 707 | return zynqmp_clk_get_crf_crl_rate(priv, id, two_divs); |
Siva Durga Prasad Paladugu | 128ec1f | 2016-11-15 16:15:41 +0530 | [diff] [blame] | 708 | default: |
Siva Durga Prasad Paladugu | ad76f8c | 2017-02-03 23:56:49 +0530 | [diff] [blame] | 709 | return -ENXIO; |
Siva Durga Prasad Paladugu | 128ec1f | 2016-11-15 16:15:41 +0530 | [diff] [blame] | 710 | } |
Siva Durga Prasad Paladugu | 128ec1f | 2016-11-15 16:15:41 +0530 | [diff] [blame] | 711 | } |
| 712 | |
Siva Durga Prasad Paladugu | ad76f8c | 2017-02-03 23:56:49 +0530 | [diff] [blame] | 713 | static ulong zynqmp_clk_set_rate(struct clk *clk, ulong rate) |
Siva Durga Prasad Paladugu | 128ec1f | 2016-11-15 16:15:41 +0530 | [diff] [blame] | 714 | { |
Siva Durga Prasad Paladugu | ad76f8c | 2017-02-03 23:56:49 +0530 | [diff] [blame] | 715 | struct zynqmp_clk_priv *priv = dev_get_priv(clk->dev); |
| 716 | enum zynqmp_clk id = clk->id; |
| 717 | bool two_divs = true; |
Siva Durga Prasad Paladugu | 128ec1f | 2016-11-15 16:15:41 +0530 | [diff] [blame] | 718 | |
Siva Durga Prasad Paladugu | ad76f8c | 2017-02-03 23:56:49 +0530 | [diff] [blame] | 719 | switch (id) { |
| 720 | case gem0_ref ... gem3_ref: |
Michal Simek | 0e789d2 | 2021-10-29 13:13:38 +0200 | [diff] [blame] | 721 | case gem0_tx ... gem3_tx: |
Ashok Reddy Soma | 7a480fd | 2023-07-19 02:49:12 -0600 | [diff] [blame^] | 722 | case gem0_rx ... gem3_rx: |
| 723 | case gem_tsu: |
Siva Durga Prasad Paladugu | ad76f8c | 2017-02-03 23:56:49 +0530 | [diff] [blame] | 724 | case qspi_ref ... can1_ref: |
Michal Simek | e959ade | 2021-10-29 13:13:37 +0200 | [diff] [blame] | 725 | case usb0_bus_ref ... usb3_dual_ref: |
Siva Durga Prasad Paladugu | ad76f8c | 2017-02-03 23:56:49 +0530 | [diff] [blame] | 726 | return zynqmp_clk_set_peripheral_rate(priv, id, |
| 727 | rate, two_divs); |
Siva Durga Prasad Paladugu | 128ec1f | 2016-11-15 16:15:41 +0530 | [diff] [blame] | 728 | default: |
Siva Durga Prasad Paladugu | ad76f8c | 2017-02-03 23:56:49 +0530 | [diff] [blame] | 729 | return -ENXIO; |
Siva Durga Prasad Paladugu | 128ec1f | 2016-11-15 16:15:41 +0530 | [diff] [blame] | 730 | } |
Siva Durga Prasad Paladugu | 128ec1f | 2016-11-15 16:15:41 +0530 | [diff] [blame] | 731 | } |
| 732 | |
Siva Durga Prasad Paladugu | ad76f8c | 2017-02-03 23:56:49 +0530 | [diff] [blame] | 733 | int soc_clk_dump(void) |
Siva Durga Prasad Paladugu | 128ec1f | 2016-11-15 16:15:41 +0530 | [diff] [blame] | 734 | { |
Siva Durga Prasad Paladugu | ad76f8c | 2017-02-03 23:56:49 +0530 | [diff] [blame] | 735 | struct udevice *dev; |
| 736 | int i, ret; |
Siva Durga Prasad Paladugu | 128ec1f | 2016-11-15 16:15:41 +0530 | [diff] [blame] | 737 | |
Siva Durga Prasad Paladugu | ad76f8c | 2017-02-03 23:56:49 +0530 | [diff] [blame] | 738 | ret = uclass_get_device_by_driver(UCLASS_CLK, |
Simon Glass | 65e25be | 2020-12-28 20:34:56 -0700 | [diff] [blame] | 739 | DM_DRIVER_GET(zynqmp_clk), &dev); |
Siva Durga Prasad Paladugu | ad76f8c | 2017-02-03 23:56:49 +0530 | [diff] [blame] | 740 | if (ret) |
| 741 | return ret; |
Siva Durga Prasad Paladugu | 128ec1f | 2016-11-15 16:15:41 +0530 | [diff] [blame] | 742 | |
Siva Durga Prasad Paladugu | ad76f8c | 2017-02-03 23:56:49 +0530 | [diff] [blame] | 743 | printf("clk\t\tfrequency\n"); |
| 744 | for (i = 0; i < clk_max; i++) { |
| 745 | const char *name = clk_names[i]; |
| 746 | if (name) { |
| 747 | struct clk clk; |
| 748 | unsigned long rate; |
Siva Durga Prasad Paladugu | 128ec1f | 2016-11-15 16:15:41 +0530 | [diff] [blame] | 749 | |
Siva Durga Prasad Paladugu | ad76f8c | 2017-02-03 23:56:49 +0530 | [diff] [blame] | 750 | clk.id = i; |
| 751 | ret = clk_request(dev, &clk); |
| 752 | if (ret < 0) |
| 753 | return ret; |
Siva Durga Prasad Paladugu | 128ec1f | 2016-11-15 16:15:41 +0530 | [diff] [blame] | 754 | |
Siva Durga Prasad Paladugu | ad76f8c | 2017-02-03 23:56:49 +0530 | [diff] [blame] | 755 | rate = clk_get_rate(&clk); |
Siva Durga Prasad Paladugu | 128ec1f | 2016-11-15 16:15:41 +0530 | [diff] [blame] | 756 | |
Siva Durga Prasad Paladugu | ad76f8c | 2017-02-03 23:56:49 +0530 | [diff] [blame] | 757 | clk_free(&clk); |
| 758 | |
| 759 | if ((rate == (unsigned long)-ENOSYS) || |
Siva Durga Prasad Paladugu | 154799a | 2017-04-13 16:59:38 +0530 | [diff] [blame] | 760 | (rate == (unsigned long)-ENXIO) || |
| 761 | (rate == (unsigned long)-EIO)) |
Siva Durga Prasad Paladugu | ad76f8c | 2017-02-03 23:56:49 +0530 | [diff] [blame] | 762 | printf("%10s%20s\n", name, "unknown"); |
| 763 | else |
| 764 | printf("%10s%20lu\n", name, rate); |
| 765 | } |
Siva Durga Prasad Paladugu | 128ec1f | 2016-11-15 16:15:41 +0530 | [diff] [blame] | 766 | } |
| 767 | |
| 768 | return 0; |
| 769 | } |
| 770 | |
Siva Durga Prasad Paladugu | ad76f8c | 2017-02-03 23:56:49 +0530 | [diff] [blame] | 771 | static int zynqmp_get_freq_by_name(char *name, struct udevice *dev, ulong *freq) |
Siva Durga Prasad Paladugu | 128ec1f | 2016-11-15 16:15:41 +0530 | [diff] [blame] | 772 | { |
| 773 | struct clk clk; |
| 774 | int ret; |
| 775 | |
Siva Durga Prasad Paladugu | ad76f8c | 2017-02-03 23:56:49 +0530 | [diff] [blame] | 776 | ret = clk_get_by_name(dev, name, &clk); |
Siva Durga Prasad Paladugu | 128ec1f | 2016-11-15 16:15:41 +0530 | [diff] [blame] | 777 | if (ret < 0) { |
Siva Durga Prasad Paladugu | ad76f8c | 2017-02-03 23:56:49 +0530 | [diff] [blame] | 778 | dev_err(dev, "failed to get %s\n", name); |
Siva Durga Prasad Paladugu | 128ec1f | 2016-11-15 16:15:41 +0530 | [diff] [blame] | 779 | return ret; |
| 780 | } |
| 781 | |
Siva Durga Prasad Paladugu | ad76f8c | 2017-02-03 23:56:49 +0530 | [diff] [blame] | 782 | *freq = clk_get_rate(&clk); |
| 783 | if (IS_ERR_VALUE(*freq)) { |
| 784 | dev_err(dev, "failed to get rate %s\n", name); |
Siva Durga Prasad Paladugu | 128ec1f | 2016-11-15 16:15:41 +0530 | [diff] [blame] | 785 | return -EINVAL; |
| 786 | } |
| 787 | |
| 788 | return 0; |
| 789 | } |
Siva Durga Prasad Paladugu | ad76f8c | 2017-02-03 23:56:49 +0530 | [diff] [blame] | 790 | static int zynqmp_clk_probe(struct udevice *dev) |
| 791 | { |
| 792 | int ret; |
| 793 | struct zynqmp_clk_priv *priv = dev_get_priv(dev); |
| 794 | |
| 795 | debug("%s\n", __func__); |
| 796 | ret = zynqmp_get_freq_by_name("pss_ref_clk", dev, &priv->ps_clk_freq); |
| 797 | if (ret < 0) |
| 798 | return -EINVAL; |
| 799 | |
| 800 | ret = zynqmp_get_freq_by_name("video_clk", dev, &priv->video_clk); |
| 801 | if (ret < 0) |
| 802 | return -EINVAL; |
| 803 | |
| 804 | ret = zynqmp_get_freq_by_name("pss_alt_ref_clk", dev, |
| 805 | &priv->pss_alt_ref_clk); |
| 806 | if (ret < 0) |
| 807 | return -EINVAL; |
| 808 | |
| 809 | ret = zynqmp_get_freq_by_name("aux_ref_clk", dev, &priv->aux_ref_clk); |
| 810 | if (ret < 0) |
| 811 | return -EINVAL; |
| 812 | |
| 813 | ret = zynqmp_get_freq_by_name("gt_crx_ref_clk", dev, |
| 814 | &priv->gt_crx_ref_clk); |
| 815 | if (ret < 0) |
| 816 | return -EINVAL; |
| 817 | |
| 818 | return 0; |
| 819 | } |
Siva Durga Prasad Paladugu | 128ec1f | 2016-11-15 16:15:41 +0530 | [diff] [blame] | 820 | |
T Karthik Reddy | a72a6ae | 2021-02-03 03:10:45 -0700 | [diff] [blame] | 821 | static int zynqmp_clk_enable(struct clk *clk) |
| 822 | { |
| 823 | enum zynqmp_clk id = clk->id; |
| 824 | u32 reg, clk_ctrl, clkact_shift, mask; |
| 825 | int ret; |
| 826 | |
| 827 | reg = zynqmp_clk_get_register(id); |
| 828 | debug("%s, clk_id:%x, clk_base:0x%x\n", __func__, id, reg); |
| 829 | |
| 830 | switch (id) { |
| 831 | case usb0_bus_ref ... usb1: |
| 832 | clkact_shift = 25; |
| 833 | mask = 0x1; |
| 834 | break; |
Michal Simek | 0e789d2 | 2021-10-29 13:13:38 +0200 | [diff] [blame] | 835 | case gem0_tx ... gem3_tx: |
T Karthik Reddy | a72a6ae | 2021-02-03 03:10:45 -0700 | [diff] [blame] | 836 | case gem0_ref ... gem3_ref: |
| 837 | clkact_shift = 25; |
| 838 | mask = 0x3; |
| 839 | break; |
| 840 | case qspi_ref ... can1_ref: |
Michal Simek | 0c383a7 | 2021-07-01 19:01:42 +0200 | [diff] [blame] | 841 | case lpd_lsbus: |
T Karthik Reddy | a72a6ae | 2021-02-03 03:10:45 -0700 | [diff] [blame] | 842 | clkact_shift = 24; |
| 843 | mask = 0x1; |
| 844 | break; |
| 845 | default: |
| 846 | return -ENXIO; |
| 847 | } |
| 848 | |
| 849 | ret = zynqmp_mmio_read(reg, &clk_ctrl); |
| 850 | if (ret) { |
| 851 | printf("%s mio read fail\n", __func__); |
| 852 | return -EIO; |
| 853 | } |
| 854 | |
| 855 | clk_ctrl |= (mask << clkact_shift); |
| 856 | ret = zynqmp_mmio_write(reg, mask << clkact_shift, clk_ctrl); |
| 857 | if (ret) { |
| 858 | printf("%s mio write fail\n", __func__); |
| 859 | return -EIO; |
| 860 | } |
| 861 | |
| 862 | return ret; |
| 863 | } |
| 864 | |
Siva Durga Prasad Paladugu | 128ec1f | 2016-11-15 16:15:41 +0530 | [diff] [blame] | 865 | static struct clk_ops zynqmp_clk_ops = { |
| 866 | .set_rate = zynqmp_clk_set_rate, |
| 867 | .get_rate = zynqmp_clk_get_rate, |
T Karthik Reddy | a72a6ae | 2021-02-03 03:10:45 -0700 | [diff] [blame] | 868 | .enable = zynqmp_clk_enable, |
Siva Durga Prasad Paladugu | 128ec1f | 2016-11-15 16:15:41 +0530 | [diff] [blame] | 869 | }; |
| 870 | |
| 871 | static const struct udevice_id zynqmp_clk_ids[] = { |
Michal Simek | 969dd4c | 2018-02-21 13:59:21 +0100 | [diff] [blame] | 872 | { .compatible = "xlnx,zynqmp-clk" }, |
Siva Durga Prasad Paladugu | 128ec1f | 2016-11-15 16:15:41 +0530 | [diff] [blame] | 873 | { } |
| 874 | }; |
| 875 | |
| 876 | U_BOOT_DRIVER(zynqmp_clk) = { |
Michal Simek | 6c0e59f | 2020-01-07 08:50:34 +0100 | [diff] [blame] | 877 | .name = "zynqmp_clk", |
Siva Durga Prasad Paladugu | 128ec1f | 2016-11-15 16:15:41 +0530 | [diff] [blame] | 878 | .id = UCLASS_CLK, |
| 879 | .of_match = zynqmp_clk_ids, |
| 880 | .probe = zynqmp_clk_probe, |
| 881 | .ops = &zynqmp_clk_ops, |
Simon Glass | 41575d8 | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 882 | .priv_auto = sizeof(struct zynqmp_clk_priv), |
Siva Durga Prasad Paladugu | 128ec1f | 2016-11-15 16:15:41 +0530 | [diff] [blame] | 883 | }; |