Peng Fan | 6a8e5f9 | 2019-03-05 02:32:33 +0000 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* |
| 3 | * Copyright 2018 NXP |
| 4 | * Peng Fan <peng.fan@nxp.com> |
| 5 | */ |
| 6 | |
Peng Fan | 6a8e5f9 | 2019-03-05 02:32:33 +0000 | [diff] [blame] | 7 | #include <clk-uclass.h> |
| 8 | #include <dm.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 9 | #include <log.h> |
Peng Fan | 2e0644a | 2023-04-28 12:08:09 +0800 | [diff] [blame] | 10 | #include <firmware/imx/sci/sci.h> |
Peng Fan | 6a8e5f9 | 2019-03-05 02:32:33 +0000 | [diff] [blame] | 11 | #include <asm/arch/clock.h> |
| 12 | #include <dt-bindings/clock/imx8qxp-clock.h> |
| 13 | #include <dt-bindings/soc/imx_rsrc.h> |
| 14 | #include <misc.h> |
| 15 | |
| 16 | #include "clk-imx8.h" |
| 17 | |
Simon Glass | 495e80f | 2023-02-05 15:36:26 -0700 | [diff] [blame] | 18 | #if IS_ENABLED(CONFIG_CMD_CLK) |
Peng Fan | 6a8e5f9 | 2019-03-05 02:32:33 +0000 | [diff] [blame] | 19 | struct imx8_clks imx8_clk_names[] = { |
| 20 | { IMX8QXP_A35_DIV, "A35_DIV" }, |
| 21 | { IMX8QXP_I2C0_CLK, "I2C0" }, |
| 22 | { IMX8QXP_I2C1_CLK, "I2C1" }, |
| 23 | { IMX8QXP_I2C2_CLK, "I2C2" }, |
| 24 | { IMX8QXP_I2C3_CLK, "I2C3" }, |
| 25 | { IMX8QXP_UART0_CLK, "UART0" }, |
| 26 | { IMX8QXP_UART1_CLK, "UART1" }, |
| 27 | { IMX8QXP_UART2_CLK, "UART2" }, |
| 28 | { IMX8QXP_UART3_CLK, "UART3" }, |
| 29 | { IMX8QXP_SDHC0_CLK, "SDHC0" }, |
| 30 | { IMX8QXP_SDHC1_CLK, "SDHC1" }, |
| 31 | { IMX8QXP_ENET0_AHB_CLK, "ENET0_AHB" }, |
| 32 | { IMX8QXP_ENET0_IPG_CLK, "ENET0_IPG" }, |
| 33 | { IMX8QXP_ENET0_REF_DIV, "ENET0_REF" }, |
| 34 | { IMX8QXP_ENET0_PTP_CLK, "ENET0_PTP" }, |
| 35 | { IMX8QXP_ENET1_AHB_CLK, "ENET1_AHB" }, |
| 36 | { IMX8QXP_ENET1_IPG_CLK, "ENET1_IPG" }, |
| 37 | { IMX8QXP_ENET1_REF_DIV, "ENET1_REF" }, |
| 38 | { IMX8QXP_ENET1_PTP_CLK, "ENET1_PTP" }, |
| 39 | }; |
| 40 | |
| 41 | int num_clks = ARRAY_SIZE(imx8_clk_names); |
| 42 | #endif |
| 43 | |
| 44 | ulong imx8_clk_get_rate(struct clk *clk) |
| 45 | { |
| 46 | sc_pm_clk_t pm_clk; |
| 47 | ulong rate; |
| 48 | u16 resource; |
| 49 | int ret; |
| 50 | |
| 51 | debug("%s(#%lu)\n", __func__, clk->id); |
| 52 | |
| 53 | switch (clk->id) { |
| 54 | case IMX8QXP_A35_DIV: |
| 55 | resource = SC_R_A35; |
| 56 | pm_clk = SC_PM_CLK_CPU; |
| 57 | break; |
| 58 | case IMX8QXP_I2C0_CLK: |
Anatolij Gustschin | b9a1e51 | 2020-01-07 14:03:03 +0100 | [diff] [blame] | 59 | case IMX8QXP_I2C0_IPG_CLK: |
Peng Fan | 6a8e5f9 | 2019-03-05 02:32:33 +0000 | [diff] [blame] | 60 | resource = SC_R_I2C_0; |
| 61 | pm_clk = SC_PM_CLK_PER; |
| 62 | break; |
| 63 | case IMX8QXP_I2C1_CLK: |
Anatolij Gustschin | b9a1e51 | 2020-01-07 14:03:03 +0100 | [diff] [blame] | 64 | case IMX8QXP_I2C1_IPG_CLK: |
Peng Fan | 6a8e5f9 | 2019-03-05 02:32:33 +0000 | [diff] [blame] | 65 | resource = SC_R_I2C_1; |
| 66 | pm_clk = SC_PM_CLK_PER; |
| 67 | break; |
| 68 | case IMX8QXP_I2C2_CLK: |
Anatolij Gustschin | b9a1e51 | 2020-01-07 14:03:03 +0100 | [diff] [blame] | 69 | case IMX8QXP_I2C2_IPG_CLK: |
Peng Fan | 6a8e5f9 | 2019-03-05 02:32:33 +0000 | [diff] [blame] | 70 | resource = SC_R_I2C_2; |
| 71 | pm_clk = SC_PM_CLK_PER; |
| 72 | break; |
| 73 | case IMX8QXP_I2C3_CLK: |
Anatolij Gustschin | b9a1e51 | 2020-01-07 14:03:03 +0100 | [diff] [blame] | 74 | case IMX8QXP_I2C3_IPG_CLK: |
Peng Fan | 6a8e5f9 | 2019-03-05 02:32:33 +0000 | [diff] [blame] | 75 | resource = SC_R_I2C_3; |
| 76 | pm_clk = SC_PM_CLK_PER; |
| 77 | break; |
| 78 | case IMX8QXP_SDHC0_IPG_CLK: |
| 79 | case IMX8QXP_SDHC0_CLK: |
| 80 | case IMX8QXP_SDHC0_DIV: |
| 81 | resource = SC_R_SDHC_0; |
| 82 | pm_clk = SC_PM_CLK_PER; |
| 83 | break; |
| 84 | case IMX8QXP_SDHC1_IPG_CLK: |
| 85 | case IMX8QXP_SDHC1_CLK: |
| 86 | case IMX8QXP_SDHC1_DIV: |
| 87 | resource = SC_R_SDHC_1; |
| 88 | pm_clk = SC_PM_CLK_PER; |
| 89 | break; |
Peng Fan | 6a8e5f9 | 2019-03-05 02:32:33 +0000 | [diff] [blame] | 90 | case IMX8QXP_UART0_CLK: |
Fabio Estevam | b08666d | 2024-03-08 17:13:15 -0300 | [diff] [blame] | 91 | case IMX8QXP_UART0_IPG_CLK: |
Peng Fan | 6a8e5f9 | 2019-03-05 02:32:33 +0000 | [diff] [blame] | 92 | resource = SC_R_UART_0; |
| 93 | pm_clk = SC_PM_CLK_PER; |
| 94 | break; |
| 95 | case IMX8QXP_UART1_CLK: |
Fabio Estevam | b08666d | 2024-03-08 17:13:15 -0300 | [diff] [blame] | 96 | case IMX8QXP_UART1_IPG_CLK: |
Peng Fan | 6a8e5f9 | 2019-03-05 02:32:33 +0000 | [diff] [blame] | 97 | resource = SC_R_UART_1; |
| 98 | pm_clk = SC_PM_CLK_PER; |
| 99 | break; |
| 100 | case IMX8QXP_UART2_CLK: |
Fabio Estevam | b08666d | 2024-03-08 17:13:15 -0300 | [diff] [blame] | 101 | case IMX8QXP_UART2_IPG_CLK: |
Peng Fan | 6a8e5f9 | 2019-03-05 02:32:33 +0000 | [diff] [blame] | 102 | resource = SC_R_UART_2; |
| 103 | pm_clk = SC_PM_CLK_PER; |
| 104 | break; |
| 105 | case IMX8QXP_UART3_CLK: |
Fabio Estevam | b08666d | 2024-03-08 17:13:15 -0300 | [diff] [blame] | 106 | case IMX8QXP_UART3_IPG_CLK: |
Peng Fan | 6a8e5f9 | 2019-03-05 02:32:33 +0000 | [diff] [blame] | 107 | resource = SC_R_UART_3; |
| 108 | pm_clk = SC_PM_CLK_PER; |
| 109 | break; |
| 110 | case IMX8QXP_ENET0_IPG_CLK: |
| 111 | case IMX8QXP_ENET0_AHB_CLK: |
| 112 | case IMX8QXP_ENET0_REF_DIV: |
| 113 | case IMX8QXP_ENET0_PTP_CLK: |
| 114 | resource = SC_R_ENET_0; |
| 115 | pm_clk = SC_PM_CLK_PER; |
| 116 | break; |
| 117 | case IMX8QXP_ENET1_IPG_CLK: |
| 118 | case IMX8QXP_ENET1_AHB_CLK: |
| 119 | case IMX8QXP_ENET1_REF_DIV: |
| 120 | case IMX8QXP_ENET1_PTP_CLK: |
| 121 | resource = SC_R_ENET_1; |
| 122 | pm_clk = SC_PM_CLK_PER; |
| 123 | break; |
| 124 | default: |
| 125 | if (clk->id < IMX8QXP_UART0_IPG_CLK || |
| 126 | clk->id >= IMX8QXP_CLK_END) { |
| 127 | printf("%s(Invalid clk ID #%lu)\n", |
| 128 | __func__, clk->id); |
| 129 | return -EINVAL; |
| 130 | } |
Simon Glass | 29ff16a | 2021-03-25 10:26:08 +1300 | [diff] [blame] | 131 | return -EINVAL; |
Peng Fan | 6a8e5f9 | 2019-03-05 02:32:33 +0000 | [diff] [blame] | 132 | }; |
| 133 | |
| 134 | ret = sc_pm_get_clock_rate(-1, resource, pm_clk, |
| 135 | (sc_pm_clock_rate_t *)&rate); |
| 136 | if (ret) { |
| 137 | printf("%s err %d\n", __func__, ret); |
| 138 | return ret; |
| 139 | } |
| 140 | |
| 141 | return rate; |
| 142 | } |
| 143 | |
| 144 | ulong imx8_clk_set_rate(struct clk *clk, unsigned long rate) |
| 145 | { |
| 146 | sc_pm_clk_t pm_clk; |
| 147 | u32 new_rate = rate; |
| 148 | u16 resource; |
| 149 | int ret; |
| 150 | |
| 151 | debug("%s(#%lu), rate: %lu\n", __func__, clk->id, rate); |
| 152 | |
| 153 | switch (clk->id) { |
| 154 | case IMX8QXP_I2C0_CLK: |
Anatolij Gustschin | b9a1e51 | 2020-01-07 14:03:03 +0100 | [diff] [blame] | 155 | case IMX8QXP_I2C0_IPG_CLK: |
Peng Fan | 6a8e5f9 | 2019-03-05 02:32:33 +0000 | [diff] [blame] | 156 | resource = SC_R_I2C_0; |
| 157 | pm_clk = SC_PM_CLK_PER; |
| 158 | break; |
| 159 | case IMX8QXP_I2C1_CLK: |
Anatolij Gustschin | b9a1e51 | 2020-01-07 14:03:03 +0100 | [diff] [blame] | 160 | case IMX8QXP_I2C1_IPG_CLK: |
Peng Fan | 6a8e5f9 | 2019-03-05 02:32:33 +0000 | [diff] [blame] | 161 | resource = SC_R_I2C_1; |
| 162 | pm_clk = SC_PM_CLK_PER; |
| 163 | break; |
| 164 | case IMX8QXP_I2C2_CLK: |
Anatolij Gustschin | b9a1e51 | 2020-01-07 14:03:03 +0100 | [diff] [blame] | 165 | case IMX8QXP_I2C2_IPG_CLK: |
Peng Fan | 6a8e5f9 | 2019-03-05 02:32:33 +0000 | [diff] [blame] | 166 | resource = SC_R_I2C_2; |
| 167 | pm_clk = SC_PM_CLK_PER; |
| 168 | break; |
| 169 | case IMX8QXP_I2C3_CLK: |
Anatolij Gustschin | b9a1e51 | 2020-01-07 14:03:03 +0100 | [diff] [blame] | 170 | case IMX8QXP_I2C3_IPG_CLK: |
Peng Fan | 6a8e5f9 | 2019-03-05 02:32:33 +0000 | [diff] [blame] | 171 | resource = SC_R_I2C_3; |
| 172 | pm_clk = SC_PM_CLK_PER; |
| 173 | break; |
| 174 | case IMX8QXP_UART0_CLK: |
Fabio Estevam | b08666d | 2024-03-08 17:13:15 -0300 | [diff] [blame] | 175 | case IMX8QXP_UART0_IPG_CLK: |
Peng Fan | 6a8e5f9 | 2019-03-05 02:32:33 +0000 | [diff] [blame] | 176 | resource = SC_R_UART_0; |
| 177 | pm_clk = SC_PM_CLK_PER; |
| 178 | break; |
| 179 | case IMX8QXP_UART1_CLK: |
Fabio Estevam | b08666d | 2024-03-08 17:13:15 -0300 | [diff] [blame] | 180 | case IMX8QXP_UART1_IPG_CLK: |
Peng Fan | 6a8e5f9 | 2019-03-05 02:32:33 +0000 | [diff] [blame] | 181 | resource = SC_R_UART_1; |
| 182 | pm_clk = SC_PM_CLK_PER; |
| 183 | break; |
| 184 | case IMX8QXP_UART2_CLK: |
Fabio Estevam | b08666d | 2024-03-08 17:13:15 -0300 | [diff] [blame] | 185 | case IMX8QXP_UART2_IPG_CLK: |
Peng Fan | 6a8e5f9 | 2019-03-05 02:32:33 +0000 | [diff] [blame] | 186 | resource = SC_R_UART_2; |
| 187 | pm_clk = SC_PM_CLK_PER; |
| 188 | break; |
| 189 | case IMX8QXP_UART3_CLK: |
Fabio Estevam | b08666d | 2024-03-08 17:13:15 -0300 | [diff] [blame] | 190 | case IMX8QXP_UART3_IPG_CLK: |
Peng Fan | 6a8e5f9 | 2019-03-05 02:32:33 +0000 | [diff] [blame] | 191 | resource = SC_R_UART_3; |
| 192 | pm_clk = SC_PM_CLK_PER; |
| 193 | break; |
| 194 | case IMX8QXP_SDHC0_IPG_CLK: |
| 195 | case IMX8QXP_SDHC0_CLK: |
| 196 | case IMX8QXP_SDHC0_DIV: |
| 197 | resource = SC_R_SDHC_0; |
| 198 | pm_clk = SC_PM_CLK_PER; |
| 199 | break; |
| 200 | case IMX8QXP_SDHC1_SEL: |
| 201 | case IMX8QXP_SDHC0_SEL: |
| 202 | return 0; |
| 203 | case IMX8QXP_SDHC1_IPG_CLK: |
| 204 | case IMX8QXP_SDHC1_CLK: |
| 205 | case IMX8QXP_SDHC1_DIV: |
| 206 | resource = SC_R_SDHC_1; |
| 207 | pm_clk = SC_PM_CLK_PER; |
| 208 | break; |
| 209 | case IMX8QXP_ENET0_IPG_CLK: |
| 210 | case IMX8QXP_ENET0_AHB_CLK: |
| 211 | case IMX8QXP_ENET0_REF_DIV: |
| 212 | case IMX8QXP_ENET0_PTP_CLK: |
| 213 | resource = SC_R_ENET_0; |
| 214 | pm_clk = SC_PM_CLK_PER; |
| 215 | break; |
| 216 | case IMX8QXP_ENET1_IPG_CLK: |
| 217 | case IMX8QXP_ENET1_AHB_CLK: |
| 218 | case IMX8QXP_ENET1_REF_DIV: |
| 219 | case IMX8QXP_ENET1_PTP_CLK: |
| 220 | resource = SC_R_ENET_1; |
| 221 | pm_clk = SC_PM_CLK_PER; |
| 222 | break; |
| 223 | default: |
| 224 | if (clk->id < IMX8QXP_UART0_IPG_CLK || |
| 225 | clk->id >= IMX8QXP_CLK_END) { |
| 226 | printf("%s(Invalid clk ID #%lu)\n", |
| 227 | __func__, clk->id); |
| 228 | return -EINVAL; |
| 229 | } |
Simon Glass | 29ff16a | 2021-03-25 10:26:08 +1300 | [diff] [blame] | 230 | return -EINVAL; |
Peng Fan | 6a8e5f9 | 2019-03-05 02:32:33 +0000 | [diff] [blame] | 231 | }; |
| 232 | |
| 233 | ret = sc_pm_set_clock_rate(-1, resource, pm_clk, &new_rate); |
| 234 | if (ret) { |
| 235 | printf("%s err %d\n", __func__, ret); |
| 236 | return ret; |
| 237 | } |
| 238 | |
| 239 | return new_rate; |
| 240 | } |
| 241 | |
| 242 | int __imx8_clk_enable(struct clk *clk, bool enable) |
| 243 | { |
| 244 | sc_pm_clk_t pm_clk; |
| 245 | u16 resource; |
| 246 | int ret; |
| 247 | |
| 248 | debug("%s(#%lu)\n", __func__, clk->id); |
| 249 | |
| 250 | switch (clk->id) { |
| 251 | case IMX8QXP_I2C0_CLK: |
Anatolij Gustschin | b9a1e51 | 2020-01-07 14:03:03 +0100 | [diff] [blame] | 252 | case IMX8QXP_I2C0_IPG_CLK: |
Peng Fan | 6a8e5f9 | 2019-03-05 02:32:33 +0000 | [diff] [blame] | 253 | resource = SC_R_I2C_0; |
| 254 | pm_clk = SC_PM_CLK_PER; |
| 255 | break; |
| 256 | case IMX8QXP_I2C1_CLK: |
Anatolij Gustschin | b9a1e51 | 2020-01-07 14:03:03 +0100 | [diff] [blame] | 257 | case IMX8QXP_I2C1_IPG_CLK: |
Peng Fan | 6a8e5f9 | 2019-03-05 02:32:33 +0000 | [diff] [blame] | 258 | resource = SC_R_I2C_1; |
| 259 | pm_clk = SC_PM_CLK_PER; |
| 260 | break; |
| 261 | case IMX8QXP_I2C2_CLK: |
Anatolij Gustschin | b9a1e51 | 2020-01-07 14:03:03 +0100 | [diff] [blame] | 262 | case IMX8QXP_I2C2_IPG_CLK: |
Peng Fan | 6a8e5f9 | 2019-03-05 02:32:33 +0000 | [diff] [blame] | 263 | resource = SC_R_I2C_2; |
| 264 | pm_clk = SC_PM_CLK_PER; |
| 265 | break; |
| 266 | case IMX8QXP_I2C3_CLK: |
Anatolij Gustschin | b9a1e51 | 2020-01-07 14:03:03 +0100 | [diff] [blame] | 267 | case IMX8QXP_I2C3_IPG_CLK: |
Peng Fan | 6a8e5f9 | 2019-03-05 02:32:33 +0000 | [diff] [blame] | 268 | resource = SC_R_I2C_3; |
| 269 | pm_clk = SC_PM_CLK_PER; |
| 270 | break; |
| 271 | case IMX8QXP_UART0_CLK: |
Fabio Estevam | b08666d | 2024-03-08 17:13:15 -0300 | [diff] [blame] | 272 | case IMX8QXP_UART0_IPG_CLK: |
Peng Fan | 6a8e5f9 | 2019-03-05 02:32:33 +0000 | [diff] [blame] | 273 | resource = SC_R_UART_0; |
| 274 | pm_clk = SC_PM_CLK_PER; |
| 275 | break; |
| 276 | case IMX8QXP_UART1_CLK: |
Fabio Estevam | b08666d | 2024-03-08 17:13:15 -0300 | [diff] [blame] | 277 | case IMX8QXP_UART1_IPG_CLK: |
Peng Fan | 6a8e5f9 | 2019-03-05 02:32:33 +0000 | [diff] [blame] | 278 | resource = SC_R_UART_1; |
| 279 | pm_clk = SC_PM_CLK_PER; |
| 280 | break; |
| 281 | case IMX8QXP_UART2_CLK: |
Fabio Estevam | b08666d | 2024-03-08 17:13:15 -0300 | [diff] [blame] | 282 | case IMX8QXP_UART2_IPG_CLK: |
Peng Fan | 6a8e5f9 | 2019-03-05 02:32:33 +0000 | [diff] [blame] | 283 | resource = SC_R_UART_2; |
| 284 | pm_clk = SC_PM_CLK_PER; |
| 285 | break; |
| 286 | case IMX8QXP_UART3_CLK: |
Fabio Estevam | b08666d | 2024-03-08 17:13:15 -0300 | [diff] [blame] | 287 | case IMX8QXP_UART3_IPG_CLK: |
Peng Fan | 6a8e5f9 | 2019-03-05 02:32:33 +0000 | [diff] [blame] | 288 | resource = SC_R_UART_3; |
| 289 | pm_clk = SC_PM_CLK_PER; |
| 290 | break; |
| 291 | case IMX8QXP_SDHC0_IPG_CLK: |
| 292 | case IMX8QXP_SDHC0_CLK: |
| 293 | case IMX8QXP_SDHC0_DIV: |
| 294 | resource = SC_R_SDHC_0; |
| 295 | pm_clk = SC_PM_CLK_PER; |
| 296 | break; |
| 297 | case IMX8QXP_SDHC1_IPG_CLK: |
| 298 | case IMX8QXP_SDHC1_CLK: |
| 299 | case IMX8QXP_SDHC1_DIV: |
| 300 | resource = SC_R_SDHC_1; |
| 301 | pm_clk = SC_PM_CLK_PER; |
| 302 | break; |
| 303 | case IMX8QXP_ENET0_IPG_CLK: |
| 304 | case IMX8QXP_ENET0_AHB_CLK: |
| 305 | case IMX8QXP_ENET0_REF_DIV: |
| 306 | case IMX8QXP_ENET0_PTP_CLK: |
| 307 | resource = SC_R_ENET_0; |
| 308 | pm_clk = SC_PM_CLK_PER; |
| 309 | break; |
| 310 | case IMX8QXP_ENET1_IPG_CLK: |
| 311 | case IMX8QXP_ENET1_AHB_CLK: |
| 312 | case IMX8QXP_ENET1_REF_DIV: |
| 313 | case IMX8QXP_ENET1_PTP_CLK: |
| 314 | resource = SC_R_ENET_1; |
| 315 | pm_clk = SC_PM_CLK_PER; |
| 316 | break; |
| 317 | default: |
| 318 | if (clk->id < IMX8QXP_UART0_IPG_CLK || |
| 319 | clk->id >= IMX8QXP_CLK_END) { |
| 320 | printf("%s(Invalid clk ID #%lu)\n", |
| 321 | __func__, clk->id); |
| 322 | return -EINVAL; |
| 323 | } |
Simon Glass | 29ff16a | 2021-03-25 10:26:08 +1300 | [diff] [blame] | 324 | return -EINVAL; |
Peng Fan | 6a8e5f9 | 2019-03-05 02:32:33 +0000 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | ret = sc_pm_clock_enable(-1, resource, pm_clk, enable, 0); |
| 328 | if (ret) { |
| 329 | printf("%s err %d\n", __func__, ret); |
| 330 | return ret; |
| 331 | } |
| 332 | |
| 333 | return 0; |
| 334 | } |