Jagan Teki | 1d150b4 | 2018-12-22 21:32:49 +0530 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * Copyright (C) 2018 Amarula Solutions. |
| 4 | * Author: Jagan Teki <jagan@amarulasolutions.com> |
| 5 | */ |
| 6 | |
Jagan Teki | 1d150b4 | 2018-12-22 21:32:49 +0530 | [diff] [blame] | 7 | #include <clk-uclass.h> |
| 8 | #include <dm.h> |
| 9 | #include <errno.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 10 | #include <log.h> |
Andre Przywara | 030bab8 | 2019-01-29 15:54:08 +0000 | [diff] [blame] | 11 | #include <reset.h> |
Jagan Teki | 1d150b4 | 2018-12-22 21:32:49 +0530 | [diff] [blame] | 12 | #include <asm/io.h> |
Samuel Holland | 12e3faa | 2021-09-12 11:48:43 -0500 | [diff] [blame] | 13 | #include <clk/sunxi.h> |
Samuel Holland | 86b561c | 2022-05-09 00:29:37 -0500 | [diff] [blame] | 14 | #include <dm/device-internal.h> |
Simon Glass | 4dcacfc | 2020-05-10 11:40:13 -0600 | [diff] [blame] | 15 | #include <linux/bitops.h> |
Jagan Teki | 1d150b4 | 2018-12-22 21:32:49 +0530 | [diff] [blame] | 16 | #include <linux/log2.h> |
| 17 | |
Samuel Holland | 86b561c | 2022-05-09 00:29:37 -0500 | [diff] [blame] | 18 | extern U_BOOT_DRIVER(sunxi_reset); |
| 19 | |
Samuel Holland | 9031532 | 2022-05-09 00:29:35 -0500 | [diff] [blame] | 20 | static const struct ccu_clk_gate *plat_to_gate(struct ccu_plat *plat, |
Jagan Teki | 1d150b4 | 2018-12-22 21:32:49 +0530 | [diff] [blame] | 21 | unsigned long id) |
| 22 | { |
Samuel Holland | 9031532 | 2022-05-09 00:29:35 -0500 | [diff] [blame] | 23 | if (id >= plat->desc->num_gates) |
Samuel Holland | a496907 | 2022-05-09 00:29:32 -0500 | [diff] [blame] | 24 | return NULL; |
| 25 | |
Samuel Holland | 9031532 | 2022-05-09 00:29:35 -0500 | [diff] [blame] | 26 | return &plat->desc->gates[id]; |
Jagan Teki | 1d150b4 | 2018-12-22 21:32:49 +0530 | [diff] [blame] | 27 | } |
| 28 | |
| 29 | static int sunxi_set_gate(struct clk *clk, bool on) |
| 30 | { |
Samuel Holland | 9031532 | 2022-05-09 00:29:35 -0500 | [diff] [blame] | 31 | struct ccu_plat *plat = dev_get_plat(clk->dev); |
| 32 | const struct ccu_clk_gate *gate = plat_to_gate(plat, clk->id); |
Jagan Teki | 1d150b4 | 2018-12-22 21:32:49 +0530 | [diff] [blame] | 33 | u32 reg; |
| 34 | |
Samuel Holland | a496907 | 2022-05-09 00:29:32 -0500 | [diff] [blame] | 35 | if (gate && (gate->flags & CCU_CLK_F_DUMMY_GATE)) |
Andre Przywara | 2d1864f | 2022-05-05 01:25:43 +0100 | [diff] [blame] | 36 | return 0; |
| 37 | |
Samuel Holland | a496907 | 2022-05-09 00:29:32 -0500 | [diff] [blame] | 38 | if (!gate || !(gate->flags & CCU_CLK_F_IS_VALID)) { |
Jagan Teki | 1d150b4 | 2018-12-22 21:32:49 +0530 | [diff] [blame] | 39 | printf("%s: (CLK#%ld) unhandled\n", __func__, clk->id); |
| 40 | return 0; |
| 41 | } |
| 42 | |
| 43 | debug("%s: (CLK#%ld) off#0x%x, BIT(%d)\n", __func__, |
| 44 | clk->id, gate->off, ilog2(gate->bit)); |
| 45 | |
Samuel Holland | 9031532 | 2022-05-09 00:29:35 -0500 | [diff] [blame] | 46 | reg = readl(plat->base + gate->off); |
Jagan Teki | 1d150b4 | 2018-12-22 21:32:49 +0530 | [diff] [blame] | 47 | if (on) |
| 48 | reg |= gate->bit; |
| 49 | else |
| 50 | reg &= ~gate->bit; |
| 51 | |
Samuel Holland | 9031532 | 2022-05-09 00:29:35 -0500 | [diff] [blame] | 52 | writel(reg, plat->base + gate->off); |
Jagan Teki | 1d150b4 | 2018-12-22 21:32:49 +0530 | [diff] [blame] | 53 | |
| 54 | return 0; |
| 55 | } |
| 56 | |
| 57 | static int sunxi_clk_enable(struct clk *clk) |
| 58 | { |
| 59 | return sunxi_set_gate(clk, true); |
| 60 | } |
| 61 | |
| 62 | static int sunxi_clk_disable(struct clk *clk) |
| 63 | { |
| 64 | return sunxi_set_gate(clk, false); |
| 65 | } |
| 66 | |
| 67 | struct clk_ops sunxi_clk_ops = { |
| 68 | .enable = sunxi_clk_enable, |
| 69 | .disable = sunxi_clk_disable, |
| 70 | }; |
| 71 | |
Samuel Holland | 751c6c6 | 2022-05-09 00:29:34 -0500 | [diff] [blame] | 72 | static int sunxi_clk_bind(struct udevice *dev) |
Samuel Holland | 1567fdf | 2022-05-09 00:29:33 -0500 | [diff] [blame] | 73 | { |
Samuel Holland | 86b561c | 2022-05-09 00:29:37 -0500 | [diff] [blame] | 74 | /* Reuse the platform data for the reset driver. */ |
| 75 | return device_bind(dev, DM_DRIVER_REF(sunxi_reset), "reset", |
| 76 | dev_get_plat(dev), dev_ofnode(dev), NULL); |
Samuel Holland | 1567fdf | 2022-05-09 00:29:33 -0500 | [diff] [blame] | 77 | } |
| 78 | |
Samuel Holland | 751c6c6 | 2022-05-09 00:29:34 -0500 | [diff] [blame] | 79 | static int sunxi_clk_probe(struct udevice *dev) |
Jagan Teki | 1d150b4 | 2018-12-22 21:32:49 +0530 | [diff] [blame] | 80 | { |
Andre Przywara | 030bab8 | 2019-01-29 15:54:08 +0000 | [diff] [blame] | 81 | struct clk_bulk clk_bulk; |
| 82 | struct reset_ctl_bulk rst_bulk; |
| 83 | int ret; |
Jagan Teki | 1d150b4 | 2018-12-22 21:32:49 +0530 | [diff] [blame] | 84 | |
Andre Przywara | 030bab8 | 2019-01-29 15:54:08 +0000 | [diff] [blame] | 85 | ret = clk_get_bulk(dev, &clk_bulk); |
| 86 | if (!ret) |
| 87 | clk_enable_bulk(&clk_bulk); |
| 88 | |
| 89 | ret = reset_get_bulk(dev, &rst_bulk); |
| 90 | if (!ret) |
| 91 | reset_deassert_bulk(&rst_bulk); |
| 92 | |
Jagan Teki | 1d150b4 | 2018-12-22 21:32:49 +0530 | [diff] [blame] | 93 | return 0; |
| 94 | } |
Samuel Holland | 751c6c6 | 2022-05-09 00:29:34 -0500 | [diff] [blame] | 95 | |
Samuel Holland | 9031532 | 2022-05-09 00:29:35 -0500 | [diff] [blame] | 96 | static int sunxi_clk_of_to_plat(struct udevice *dev) |
| 97 | { |
| 98 | struct ccu_plat *plat = dev_get_plat(dev); |
| 99 | |
| 100 | plat->base = dev_read_addr_ptr(dev); |
| 101 | if (!plat->base) |
| 102 | return -ENOMEM; |
| 103 | |
| 104 | plat->desc = (const struct ccu_desc *)dev_get_driver_data(dev); |
| 105 | if (!plat->desc) |
| 106 | return -EINVAL; |
| 107 | |
| 108 | return 0; |
| 109 | } |
| 110 | |
Samuel Holland | 751c6c6 | 2022-05-09 00:29:34 -0500 | [diff] [blame] | 111 | extern const struct ccu_desc a10_ccu_desc; |
| 112 | extern const struct ccu_desc a10s_ccu_desc; |
| 113 | extern const struct ccu_desc a23_ccu_desc; |
| 114 | extern const struct ccu_desc a31_ccu_desc; |
| 115 | extern const struct ccu_desc a31_r_ccu_desc; |
| 116 | extern const struct ccu_desc a64_ccu_desc; |
| 117 | extern const struct ccu_desc a80_ccu_desc; |
| 118 | extern const struct ccu_desc a80_mmc_clk_desc; |
| 119 | extern const struct ccu_desc a83t_ccu_desc; |
Samuel Holland | 934d0f5 | 2022-04-30 22:38:37 -0500 | [diff] [blame] | 120 | extern const struct ccu_desc d1_ccu_desc; |
Samuel Holland | 751c6c6 | 2022-05-09 00:29:34 -0500 | [diff] [blame] | 121 | extern const struct ccu_desc f1c100s_ccu_desc; |
| 122 | extern const struct ccu_desc h3_ccu_desc; |
| 123 | extern const struct ccu_desc h6_ccu_desc; |
| 124 | extern const struct ccu_desc h616_ccu_desc; |
| 125 | extern const struct ccu_desc h6_r_ccu_desc; |
| 126 | extern const struct ccu_desc r40_ccu_desc; |
| 127 | extern const struct ccu_desc v3s_ccu_desc; |
| 128 | |
| 129 | static const struct udevice_id sunxi_clk_ids[] = { |
| 130 | #ifdef CONFIG_CLK_SUN4I_A10 |
| 131 | { .compatible = "allwinner,sun4i-a10-ccu", |
| 132 | .data = (ulong)&a10_ccu_desc }, |
| 133 | #endif |
| 134 | #ifdef CONFIG_CLK_SUN5I_A10S |
| 135 | { .compatible = "allwinner,sun5i-a10s-ccu", |
| 136 | .data = (ulong)&a10s_ccu_desc }, |
| 137 | { .compatible = "allwinner,sun5i-a13-ccu", |
| 138 | .data = (ulong)&a10s_ccu_desc }, |
| 139 | #endif |
| 140 | #ifdef CONFIG_CLK_SUN6I_A31 |
| 141 | { .compatible = "allwinner,sun6i-a31-ccu", |
| 142 | .data = (ulong)&a31_ccu_desc }, |
| 143 | #endif |
| 144 | #ifdef CONFIG_CLK_SUN4I_A10 |
| 145 | { .compatible = "allwinner,sun7i-a20-ccu", |
| 146 | .data = (ulong)&a10_ccu_desc }, |
| 147 | #endif |
| 148 | #ifdef CONFIG_CLK_SUN8I_A23 |
| 149 | { .compatible = "allwinner,sun8i-a23-ccu", |
| 150 | .data = (ulong)&a23_ccu_desc }, |
| 151 | { .compatible = "allwinner,sun8i-a33-ccu", |
| 152 | .data = (ulong)&a23_ccu_desc }, |
| 153 | #endif |
| 154 | #ifdef CONFIG_CLK_SUN8I_A83T |
| 155 | { .compatible = "allwinner,sun8i-a83t-ccu", |
| 156 | .data = (ulong)&a83t_ccu_desc }, |
| 157 | #endif |
| 158 | #ifdef CONFIG_CLK_SUN6I_A31_R |
| 159 | { .compatible = "allwinner,sun8i-a83t-r-ccu", |
| 160 | .data = (ulong)&a31_r_ccu_desc }, |
| 161 | #endif |
| 162 | #ifdef CONFIG_CLK_SUN8I_H3 |
| 163 | { .compatible = "allwinner,sun8i-h3-ccu", |
| 164 | .data = (ulong)&h3_ccu_desc }, |
| 165 | #endif |
| 166 | #ifdef CONFIG_CLK_SUN6I_A31_R |
| 167 | { .compatible = "allwinner,sun8i-h3-r-ccu", |
| 168 | .data = (ulong)&a31_r_ccu_desc }, |
| 169 | #endif |
| 170 | #ifdef CONFIG_CLK_SUN8I_R40 |
| 171 | { .compatible = "allwinner,sun8i-r40-ccu", |
| 172 | .data = (ulong)&r40_ccu_desc }, |
| 173 | #endif |
| 174 | #ifdef CONFIG_CLK_SUN8I_V3S |
| 175 | { .compatible = "allwinner,sun8i-v3-ccu", |
| 176 | .data = (ulong)&v3s_ccu_desc }, |
| 177 | { .compatible = "allwinner,sun8i-v3s-ccu", |
| 178 | .data = (ulong)&v3s_ccu_desc }, |
| 179 | #endif |
| 180 | #ifdef CONFIG_CLK_SUN9I_A80 |
| 181 | { .compatible = "allwinner,sun9i-a80-ccu", |
| 182 | .data = (ulong)&a80_ccu_desc }, |
| 183 | { .compatible = "allwinner,sun9i-a80-mmc-config-clk", |
| 184 | .data = (ulong)&a80_mmc_clk_desc }, |
| 185 | #endif |
| 186 | #ifdef CONFIG_CLK_SUN50I_A64 |
| 187 | { .compatible = "allwinner,sun50i-a64-ccu", |
| 188 | .data = (ulong)&a64_ccu_desc }, |
| 189 | #endif |
| 190 | #ifdef CONFIG_CLK_SUN6I_A31_R |
| 191 | { .compatible = "allwinner,sun50i-a64-r-ccu", |
| 192 | .data = (ulong)&a31_r_ccu_desc }, |
| 193 | #endif |
| 194 | #ifdef CONFIG_CLK_SUN8I_H3 |
| 195 | { .compatible = "allwinner,sun50i-h5-ccu", |
| 196 | .data = (ulong)&h3_ccu_desc }, |
| 197 | #endif |
Samuel Holland | 934d0f5 | 2022-04-30 22:38:37 -0500 | [diff] [blame] | 198 | #ifdef CONFIG_CLK_SUN20I_D1 |
| 199 | { .compatible = "allwinner,sun20i-d1-ccu", |
| 200 | .data = (ulong)&d1_ccu_desc }, |
| 201 | #endif |
Samuel Holland | 751c6c6 | 2022-05-09 00:29:34 -0500 | [diff] [blame] | 202 | #ifdef CONFIG_CLK_SUN50I_H6 |
| 203 | { .compatible = "allwinner,sun50i-h6-ccu", |
| 204 | .data = (ulong)&h6_ccu_desc }, |
| 205 | #endif |
| 206 | #ifdef CONFIG_CLK_SUN50I_H6_R |
| 207 | { .compatible = "allwinner,sun50i-h6-r-ccu", |
| 208 | .data = (ulong)&h6_r_ccu_desc }, |
| 209 | #endif |
| 210 | #ifdef CONFIG_CLK_SUN50I_H616 |
| 211 | { .compatible = "allwinner,sun50i-h616-ccu", |
| 212 | .data = (ulong)&h616_ccu_desc }, |
| 213 | #endif |
| 214 | #ifdef CONFIG_CLK_SUN50I_H6_R |
| 215 | { .compatible = "allwinner,sun50i-h616-r-ccu", |
| 216 | .data = (ulong)&h6_r_ccu_desc }, |
| 217 | #endif |
| 218 | #ifdef CONFIG_CLK_SUNIV_F1C100S |
| 219 | { .compatible = "allwinner,suniv-f1c100s-ccu", |
| 220 | .data = (ulong)&f1c100s_ccu_desc }, |
| 221 | #endif |
| 222 | { } |
| 223 | }; |
| 224 | |
| 225 | U_BOOT_DRIVER(sunxi_clk) = { |
| 226 | .name = "sunxi_clk", |
| 227 | .id = UCLASS_CLK, |
| 228 | .of_match = sunxi_clk_ids, |
| 229 | .bind = sunxi_clk_bind, |
| 230 | .probe = sunxi_clk_probe, |
Samuel Holland | 9031532 | 2022-05-09 00:29:35 -0500 | [diff] [blame] | 231 | .of_to_plat = sunxi_clk_of_to_plat, |
| 232 | .plat_auto = sizeof(struct ccu_plat), |
Samuel Holland | 751c6c6 | 2022-05-09 00:29:34 -0500 | [diff] [blame] | 233 | .ops = &sunxi_clk_ops, |
| 234 | }; |