Dinh Nguyen | 07afb8d | 2018-03-21 09:20:12 -0500 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* |
| 3 | * Copyright (C) 2017, Intel Corporation |
| 4 | */ |
| 5 | #include <linux/slab.h> |
| 6 | #include <linux/clk-provider.h> |
Stephen Boyd | 62e59c4 | 2019-04-18 15:20:22 -0700 | [diff] [blame] | 7 | #include <linux/io.h> |
Dinh Nguyen | 07afb8d | 2018-03-21 09:20:12 -0500 | [diff] [blame] | 8 | |
| 9 | #include "stratix10-clk.h" |
| 10 | #include "clk.h" |
| 11 | |
| 12 | #define CLK_MGR_FREE_SHIFT 16 |
| 13 | #define CLK_MGR_FREE_MASK 0x7 |
| 14 | #define SWCTRLBTCLKSEN_SHIFT 8 |
| 15 | |
| 16 | #define to_periph_clk(p) container_of(p, struct socfpga_periph_clk, hw.hw) |
| 17 | |
Dinh Nguyen | a0f9819 | 2021-02-12 08:30:59 -0600 | [diff] [blame] | 18 | static unsigned long n5x_clk_peri_c_clk_recalc_rate(struct clk_hw *hwclk, |
| 19 | unsigned long parent_rate) |
| 20 | { |
| 21 | struct socfpga_periph_clk *socfpgaclk = to_periph_clk(hwclk); |
| 22 | unsigned long div; |
| 23 | unsigned long shift = socfpgaclk->shift; |
| 24 | u32 val; |
| 25 | |
| 26 | val = readl(socfpgaclk->hw.reg); |
| 27 | val &= (0x1f << shift); |
| 28 | div = (val >> shift) + 1; |
| 29 | |
| 30 | return parent_rate / div; |
| 31 | } |
| 32 | |
Dinh Nguyen | 07afb8d | 2018-03-21 09:20:12 -0500 | [diff] [blame] | 33 | static unsigned long clk_peri_c_clk_recalc_rate(struct clk_hw *hwclk, |
| 34 | unsigned long parent_rate) |
| 35 | { |
| 36 | struct socfpga_periph_clk *socfpgaclk = to_periph_clk(hwclk); |
| 37 | unsigned long div = 1; |
| 38 | u32 val; |
| 39 | |
| 40 | val = readl(socfpgaclk->hw.reg); |
| 41 | val &= GENMASK(SWCTRLBTCLKSEN_SHIFT - 1, 0); |
| 42 | parent_rate /= val; |
| 43 | |
| 44 | return parent_rate / div; |
| 45 | } |
| 46 | |
| 47 | static unsigned long clk_peri_cnt_clk_recalc_rate(struct clk_hw *hwclk, |
| 48 | unsigned long parent_rate) |
| 49 | { |
| 50 | struct socfpga_periph_clk *socfpgaclk = to_periph_clk(hwclk); |
| 51 | unsigned long div = 1; |
| 52 | |
| 53 | if (socfpgaclk->fixed_div) { |
| 54 | div = socfpgaclk->fixed_div; |
| 55 | } else { |
Dinh Nguyen | c7ec75e | 2019-08-14 10:30:14 -0500 | [diff] [blame] | 56 | if (socfpgaclk->hw.reg) |
Dinh Nguyen | 07afb8d | 2018-03-21 09:20:12 -0500 | [diff] [blame] | 57 | div = ((readl(socfpgaclk->hw.reg) & 0x7ff) + 1); |
| 58 | } |
| 59 | |
| 60 | return parent_rate / div; |
| 61 | } |
| 62 | |
| 63 | static u8 clk_periclk_get_parent(struct clk_hw *hwclk) |
| 64 | { |
| 65 | struct socfpga_periph_clk *socfpgaclk = to_periph_clk(hwclk); |
| 66 | u32 clk_src, mask; |
Dinh Nguyen | dfd1427 | 2021-06-10 21:52:01 -0500 | [diff] [blame] | 67 | u8 parent = 0; |
Dinh Nguyen | 07afb8d | 2018-03-21 09:20:12 -0500 | [diff] [blame] | 68 | |
Dinh Nguyen | dfd1427 | 2021-06-10 21:52:01 -0500 | [diff] [blame] | 69 | /* handle the bypass first */ |
Dinh Nguyen | 07afb8d | 2018-03-21 09:20:12 -0500 | [diff] [blame] | 70 | if (socfpgaclk->bypass_reg) { |
| 71 | mask = (0x1 << socfpgaclk->bypass_shift); |
| 72 | parent = ((readl(socfpgaclk->bypass_reg) & mask) >> |
| 73 | socfpgaclk->bypass_shift); |
Dinh Nguyen | dfd1427 | 2021-06-10 21:52:01 -0500 | [diff] [blame] | 74 | if (parent) |
| 75 | return parent; |
| 76 | } |
| 77 | |
| 78 | if (socfpgaclk->hw.reg) { |
Dinh Nguyen | 07afb8d | 2018-03-21 09:20:12 -0500 | [diff] [blame] | 79 | clk_src = readl(socfpgaclk->hw.reg); |
| 80 | parent = (clk_src >> CLK_MGR_FREE_SHIFT) & |
Dinh Nguyen | dfd1427 | 2021-06-10 21:52:01 -0500 | [diff] [blame] | 81 | CLK_MGR_FREE_MASK; |
Dinh Nguyen | 07afb8d | 2018-03-21 09:20:12 -0500 | [diff] [blame] | 82 | } |
| 83 | return parent; |
| 84 | } |
| 85 | |
Dinh Nguyen | a0f9819 | 2021-02-12 08:30:59 -0600 | [diff] [blame] | 86 | static const struct clk_ops n5x_peri_c_clk_ops = { |
| 87 | .recalc_rate = n5x_clk_peri_c_clk_recalc_rate, |
| 88 | .get_parent = clk_periclk_get_parent, |
| 89 | }; |
| 90 | |
Dinh Nguyen | 07afb8d | 2018-03-21 09:20:12 -0500 | [diff] [blame] | 91 | static const struct clk_ops peri_c_clk_ops = { |
| 92 | .recalc_rate = clk_peri_c_clk_recalc_rate, |
| 93 | .get_parent = clk_periclk_get_parent, |
| 94 | }; |
| 95 | |
| 96 | static const struct clk_ops peri_cnt_clk_ops = { |
| 97 | .recalc_rate = clk_peri_cnt_clk_recalc_rate, |
| 98 | .get_parent = clk_periclk_get_parent, |
| 99 | }; |
| 100 | |
Dinh Nguyen | ba7e258 | 2021-03-02 15:41:51 -0600 | [diff] [blame] | 101 | struct clk_hw *s10_register_periph(const struct stratix10_perip_c_clock *clks, |
Dinh Nguyen | 8c0e783 | 2020-01-14 10:07:26 -0600 | [diff] [blame] | 102 | void __iomem *reg) |
Dinh Nguyen | 07afb8d | 2018-03-21 09:20:12 -0500 | [diff] [blame] | 103 | { |
Dinh Nguyen | ba7e258 | 2021-03-02 15:41:51 -0600 | [diff] [blame] | 104 | struct clk_hw *hw_clk; |
Dinh Nguyen | 07afb8d | 2018-03-21 09:20:12 -0500 | [diff] [blame] | 105 | struct socfpga_periph_clk *periph_clk; |
| 106 | struct clk_init_data init; |
Dinh Nguyen | 8c0e783 | 2020-01-14 10:07:26 -0600 | [diff] [blame] | 107 | const char *name = clks->name; |
| 108 | const char *parent_name = clks->parent_name; |
Dinh Nguyen | ba7e258 | 2021-03-02 15:41:51 -0600 | [diff] [blame] | 109 | int ret; |
Dinh Nguyen | 07afb8d | 2018-03-21 09:20:12 -0500 | [diff] [blame] | 110 | |
| 111 | periph_clk = kzalloc(sizeof(*periph_clk), GFP_KERNEL); |
| 112 | if (WARN_ON(!periph_clk)) |
| 113 | return NULL; |
| 114 | |
Dinh Nguyen | 8c0e783 | 2020-01-14 10:07:26 -0600 | [diff] [blame] | 115 | periph_clk->hw.reg = reg + clks->offset; |
Dinh Nguyen | 07afb8d | 2018-03-21 09:20:12 -0500 | [diff] [blame] | 116 | |
| 117 | init.name = name; |
| 118 | init.ops = &peri_c_clk_ops; |
Dinh Nguyen | 8c0e783 | 2020-01-14 10:07:26 -0600 | [diff] [blame] | 119 | init.flags = clks->flags; |
Dinh Nguyen | 07afb8d | 2018-03-21 09:20:12 -0500 | [diff] [blame] | 120 | |
Dinh Nguyen | 8c0e783 | 2020-01-14 10:07:26 -0600 | [diff] [blame] | 121 | init.num_parents = clks->num_parents; |
Dinh Nguyen | 762d961 | 2020-05-12 13:16:43 -0500 | [diff] [blame] | 122 | init.parent_names = parent_name ? &parent_name : NULL; |
| 123 | if (init.parent_names == NULL) |
| 124 | init.parent_data = clks->parent_data; |
Dinh Nguyen | 07afb8d | 2018-03-21 09:20:12 -0500 | [diff] [blame] | 125 | |
| 126 | periph_clk->hw.hw.init = &init; |
Dinh Nguyen | ba7e258 | 2021-03-02 15:41:51 -0600 | [diff] [blame] | 127 | hw_clk = &periph_clk->hw.hw; |
Dinh Nguyen | 07afb8d | 2018-03-21 09:20:12 -0500 | [diff] [blame] | 128 | |
Dinh Nguyen | ba7e258 | 2021-03-02 15:41:51 -0600 | [diff] [blame] | 129 | ret = clk_hw_register(NULL, hw_clk); |
| 130 | if (ret) { |
Dinh Nguyen | 07afb8d | 2018-03-21 09:20:12 -0500 | [diff] [blame] | 131 | kfree(periph_clk); |
Dinh Nguyen | ba7e258 | 2021-03-02 15:41:51 -0600 | [diff] [blame] | 132 | return ERR_PTR(ret); |
Dinh Nguyen | 07afb8d | 2018-03-21 09:20:12 -0500 | [diff] [blame] | 133 | } |
Dinh Nguyen | ba7e258 | 2021-03-02 15:41:51 -0600 | [diff] [blame] | 134 | return hw_clk; |
Dinh Nguyen | 07afb8d | 2018-03-21 09:20:12 -0500 | [diff] [blame] | 135 | } |
| 136 | |
Dinh Nguyen | ba7e258 | 2021-03-02 15:41:51 -0600 | [diff] [blame] | 137 | struct clk_hw *n5x_register_periph(const struct n5x_perip_c_clock *clks, |
Dinh Nguyen | a0f9819 | 2021-02-12 08:30:59 -0600 | [diff] [blame] | 138 | void __iomem *regbase) |
| 139 | { |
Dinh Nguyen | ba7e258 | 2021-03-02 15:41:51 -0600 | [diff] [blame] | 140 | struct clk_hw *hw_clk; |
Dinh Nguyen | a0f9819 | 2021-02-12 08:30:59 -0600 | [diff] [blame] | 141 | struct socfpga_periph_clk *periph_clk; |
| 142 | struct clk_init_data init; |
| 143 | const char *name = clks->name; |
| 144 | const char *parent_name = clks->parent_name; |
Dinh Nguyen | ba7e258 | 2021-03-02 15:41:51 -0600 | [diff] [blame] | 145 | int ret; |
Dinh Nguyen | a0f9819 | 2021-02-12 08:30:59 -0600 | [diff] [blame] | 146 | |
| 147 | periph_clk = kzalloc(sizeof(*periph_clk), GFP_KERNEL); |
| 148 | if (WARN_ON(!periph_clk)) |
| 149 | return NULL; |
| 150 | |
| 151 | periph_clk->hw.reg = regbase + clks->offset; |
| 152 | periph_clk->shift = clks->shift; |
| 153 | |
| 154 | init.name = name; |
| 155 | init.ops = &n5x_peri_c_clk_ops; |
| 156 | init.flags = clks->flags; |
| 157 | |
| 158 | init.num_parents = clks->num_parents; |
| 159 | init.parent_names = parent_name ? &parent_name : NULL; |
| 160 | |
| 161 | periph_clk->hw.hw.init = &init; |
Dinh Nguyen | ba7e258 | 2021-03-02 15:41:51 -0600 | [diff] [blame] | 162 | hw_clk = &periph_clk->hw.hw; |
Dinh Nguyen | a0f9819 | 2021-02-12 08:30:59 -0600 | [diff] [blame] | 163 | |
Dinh Nguyen | ba7e258 | 2021-03-02 15:41:51 -0600 | [diff] [blame] | 164 | ret = clk_hw_register(NULL, hw_clk); |
| 165 | if (ret) { |
Dinh Nguyen | a0f9819 | 2021-02-12 08:30:59 -0600 | [diff] [blame] | 166 | kfree(periph_clk); |
Dinh Nguyen | ba7e258 | 2021-03-02 15:41:51 -0600 | [diff] [blame] | 167 | return ERR_PTR(ret); |
Dinh Nguyen | a0f9819 | 2021-02-12 08:30:59 -0600 | [diff] [blame] | 168 | } |
Dinh Nguyen | ba7e258 | 2021-03-02 15:41:51 -0600 | [diff] [blame] | 169 | return hw_clk; |
Dinh Nguyen | a0f9819 | 2021-02-12 08:30:59 -0600 | [diff] [blame] | 170 | } |
| 171 | |
Dinh Nguyen | ba7e258 | 2021-03-02 15:41:51 -0600 | [diff] [blame] | 172 | struct clk_hw *s10_register_cnt_periph(const struct stratix10_perip_cnt_clock *clks, |
Dinh Nguyen | 8c0e783 | 2020-01-14 10:07:26 -0600 | [diff] [blame] | 173 | void __iomem *regbase) |
Dinh Nguyen | 07afb8d | 2018-03-21 09:20:12 -0500 | [diff] [blame] | 174 | { |
Dinh Nguyen | ba7e258 | 2021-03-02 15:41:51 -0600 | [diff] [blame] | 175 | struct clk_hw *hw_clk; |
Dinh Nguyen | 07afb8d | 2018-03-21 09:20:12 -0500 | [diff] [blame] | 176 | struct socfpga_periph_clk *periph_clk; |
| 177 | struct clk_init_data init; |
Dinh Nguyen | 8c0e783 | 2020-01-14 10:07:26 -0600 | [diff] [blame] | 178 | const char *name = clks->name; |
| 179 | const char *parent_name = clks->parent_name; |
Dinh Nguyen | ba7e258 | 2021-03-02 15:41:51 -0600 | [diff] [blame] | 180 | int ret; |
Dinh Nguyen | 07afb8d | 2018-03-21 09:20:12 -0500 | [diff] [blame] | 181 | |
| 182 | periph_clk = kzalloc(sizeof(*periph_clk), GFP_KERNEL); |
| 183 | if (WARN_ON(!periph_clk)) |
| 184 | return NULL; |
| 185 | |
Dinh Nguyen | 8c0e783 | 2020-01-14 10:07:26 -0600 | [diff] [blame] | 186 | if (clks->offset) |
| 187 | periph_clk->hw.reg = regbase + clks->offset; |
Dinh Nguyen | 07afb8d | 2018-03-21 09:20:12 -0500 | [diff] [blame] | 188 | else |
| 189 | periph_clk->hw.reg = NULL; |
| 190 | |
Dinh Nguyen | 8c0e783 | 2020-01-14 10:07:26 -0600 | [diff] [blame] | 191 | if (clks->bypass_reg) |
| 192 | periph_clk->bypass_reg = regbase + clks->bypass_reg; |
Dinh Nguyen | 07afb8d | 2018-03-21 09:20:12 -0500 | [diff] [blame] | 193 | else |
| 194 | periph_clk->bypass_reg = NULL; |
Dinh Nguyen | 8c0e783 | 2020-01-14 10:07:26 -0600 | [diff] [blame] | 195 | periph_clk->bypass_shift = clks->bypass_shift; |
| 196 | periph_clk->fixed_div = clks->fixed_divider; |
Dinh Nguyen | 07afb8d | 2018-03-21 09:20:12 -0500 | [diff] [blame] | 197 | |
| 198 | init.name = name; |
| 199 | init.ops = &peri_cnt_clk_ops; |
Dinh Nguyen | 8c0e783 | 2020-01-14 10:07:26 -0600 | [diff] [blame] | 200 | init.flags = clks->flags; |
Dinh Nguyen | 07afb8d | 2018-03-21 09:20:12 -0500 | [diff] [blame] | 201 | |
Dinh Nguyen | 8c0e783 | 2020-01-14 10:07:26 -0600 | [diff] [blame] | 202 | init.num_parents = clks->num_parents; |
Dinh Nguyen | 762d961 | 2020-05-12 13:16:43 -0500 | [diff] [blame] | 203 | init.parent_names = parent_name ? &parent_name : NULL; |
| 204 | if (init.parent_names == NULL) |
| 205 | init.parent_data = clks->parent_data; |
Dinh Nguyen | 07afb8d | 2018-03-21 09:20:12 -0500 | [diff] [blame] | 206 | |
| 207 | periph_clk->hw.hw.init = &init; |
Dinh Nguyen | ba7e258 | 2021-03-02 15:41:51 -0600 | [diff] [blame] | 208 | hw_clk = &periph_clk->hw.hw; |
Dinh Nguyen | 07afb8d | 2018-03-21 09:20:12 -0500 | [diff] [blame] | 209 | |
Dinh Nguyen | ba7e258 | 2021-03-02 15:41:51 -0600 | [diff] [blame] | 210 | ret = clk_hw_register(NULL, hw_clk); |
| 211 | if (ret) { |
Dinh Nguyen | 07afb8d | 2018-03-21 09:20:12 -0500 | [diff] [blame] | 212 | kfree(periph_clk); |
Dinh Nguyen | ba7e258 | 2021-03-02 15:41:51 -0600 | [diff] [blame] | 213 | return ERR_PTR(ret); |
Dinh Nguyen | 07afb8d | 2018-03-21 09:20:12 -0500 | [diff] [blame] | 214 | } |
Dinh Nguyen | ba7e258 | 2021-03-02 15:41:51 -0600 | [diff] [blame] | 215 | return hw_clk; |
Dinh Nguyen | 07afb8d | 2018-03-21 09:20:12 -0500 | [diff] [blame] | 216 | } |