Thomas Gleixner | 2874c5f | 2019-05-27 08:55:01 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Boris BREZILLON | 38d34c3 | 2013-10-11 10:44:49 +0200 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2013 Boris BREZILLON <b.brezillon@overkiz.com> |
Boris BREZILLON | 38d34c3 | 2013-10-11 10:44:49 +0200 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #include <linux/clk-provider.h> |
| 7 | #include <linux/clkdev.h> |
| 8 | #include <linux/clk/at91_pmc.h> |
| 9 | #include <linux/delay.h> |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 10 | #include <linux/mfd/syscon.h> |
| 11 | #include <linux/regmap.h> |
Boris BREZILLON | 38d34c3 | 2013-10-11 10:44:49 +0200 | [diff] [blame] | 12 | |
| 13 | #include "pmc.h" |
| 14 | |
| 15 | #define SLOW_CLOCK_FREQ 32768 |
| 16 | #define MAINF_DIV 16 |
| 17 | #define MAINFRDY_TIMEOUT (((MAINF_DIV + 1) * USEC_PER_SEC) / \ |
| 18 | SLOW_CLOCK_FREQ) |
| 19 | #define MAINF_LOOP_MIN_WAIT (USEC_PER_SEC / SLOW_CLOCK_FREQ) |
| 20 | #define MAINF_LOOP_MAX_WAIT MAINFRDY_TIMEOUT |
| 21 | |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 22 | #define MOR_KEY_MASK (0xff << 16) |
| 23 | |
| 24 | struct clk_main_osc { |
Boris BREZILLON | 38d34c3 | 2013-10-11 10:44:49 +0200 | [diff] [blame] | 25 | struct clk_hw hw; |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 26 | struct regmap *regmap; |
Boris BREZILLON | 38d34c3 | 2013-10-11 10:44:49 +0200 | [diff] [blame] | 27 | }; |
| 28 | |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 29 | #define to_clk_main_osc(hw) container_of(hw, struct clk_main_osc, hw) |
Boris BREZILLON | 38d34c3 | 2013-10-11 10:44:49 +0200 | [diff] [blame] | 30 | |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 31 | struct clk_main_rc_osc { |
| 32 | struct clk_hw hw; |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 33 | struct regmap *regmap; |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 34 | unsigned long frequency; |
| 35 | unsigned long accuracy; |
| 36 | }; |
| 37 | |
| 38 | #define to_clk_main_rc_osc(hw) container_of(hw, struct clk_main_rc_osc, hw) |
| 39 | |
| 40 | struct clk_rm9200_main { |
| 41 | struct clk_hw hw; |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 42 | struct regmap *regmap; |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 43 | }; |
| 44 | |
| 45 | #define to_clk_rm9200_main(hw) container_of(hw, struct clk_rm9200_main, hw) |
| 46 | |
| 47 | struct clk_sam9x5_main { |
| 48 | struct clk_hw hw; |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 49 | struct regmap *regmap; |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 50 | u8 parent; |
| 51 | }; |
| 52 | |
| 53 | #define to_clk_sam9x5_main(hw) container_of(hw, struct clk_sam9x5_main, hw) |
| 54 | |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 55 | static inline bool clk_main_osc_ready(struct regmap *regmap) |
| 56 | { |
| 57 | unsigned int status; |
| 58 | |
| 59 | regmap_read(regmap, AT91_PMC_SR, &status); |
| 60 | |
| 61 | return status & AT91_PMC_MOSCS; |
| 62 | } |
| 63 | |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 64 | static int clk_main_osc_prepare(struct clk_hw *hw) |
Boris BREZILLON | 38d34c3 | 2013-10-11 10:44:49 +0200 | [diff] [blame] | 65 | { |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 66 | struct clk_main_osc *osc = to_clk_main_osc(hw); |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 67 | struct regmap *regmap = osc->regmap; |
Boris BREZILLON | 38d34c3 | 2013-10-11 10:44:49 +0200 | [diff] [blame] | 68 | u32 tmp; |
| 69 | |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 70 | regmap_read(regmap, AT91_CKGR_MOR, &tmp); |
| 71 | tmp &= ~MOR_KEY_MASK; |
| 72 | |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 73 | if (tmp & AT91_PMC_OSCBYPASS) |
Boris BREZILLON | 38d34c3 | 2013-10-11 10:44:49 +0200 | [diff] [blame] | 74 | return 0; |
| 75 | |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 76 | if (!(tmp & AT91_PMC_MOSCEN)) { |
| 77 | tmp |= AT91_PMC_MOSCEN | AT91_PMC_KEY; |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 78 | regmap_write(regmap, AT91_CKGR_MOR, tmp); |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 79 | } |
| 80 | |
Alexandre Belloni | 99a8170 | 2015-09-16 23:47:39 +0200 | [diff] [blame] | 81 | while (!clk_main_osc_ready(regmap)) |
| 82 | cpu_relax(); |
Boris BREZILLON | 38d34c3 | 2013-10-11 10:44:49 +0200 | [diff] [blame] | 83 | |
| 84 | return 0; |
| 85 | } |
| 86 | |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 87 | static void clk_main_osc_unprepare(struct clk_hw *hw) |
Boris BREZILLON | 38d34c3 | 2013-10-11 10:44:49 +0200 | [diff] [blame] | 88 | { |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 89 | struct clk_main_osc *osc = to_clk_main_osc(hw); |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 90 | struct regmap *regmap = osc->regmap; |
| 91 | u32 tmp; |
Boris BREZILLON | 38d34c3 | 2013-10-11 10:44:49 +0200 | [diff] [blame] | 92 | |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 93 | regmap_read(regmap, AT91_CKGR_MOR, &tmp); |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 94 | if (tmp & AT91_PMC_OSCBYPASS) |
| 95 | return; |
| 96 | |
| 97 | if (!(tmp & AT91_PMC_MOSCEN)) |
| 98 | return; |
| 99 | |
| 100 | tmp &= ~(AT91_PMC_KEY | AT91_PMC_MOSCEN); |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 101 | regmap_write(regmap, AT91_CKGR_MOR, tmp | AT91_PMC_KEY); |
Boris BREZILLON | 38d34c3 | 2013-10-11 10:44:49 +0200 | [diff] [blame] | 102 | } |
| 103 | |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 104 | static int clk_main_osc_is_prepared(struct clk_hw *hw) |
Boris BREZILLON | 38d34c3 | 2013-10-11 10:44:49 +0200 | [diff] [blame] | 105 | { |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 106 | struct clk_main_osc *osc = to_clk_main_osc(hw); |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 107 | struct regmap *regmap = osc->regmap; |
| 108 | u32 tmp, status; |
Boris BREZILLON | 38d34c3 | 2013-10-11 10:44:49 +0200 | [diff] [blame] | 109 | |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 110 | regmap_read(regmap, AT91_CKGR_MOR, &tmp); |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 111 | if (tmp & AT91_PMC_OSCBYPASS) |
| 112 | return 1; |
Boris BREZILLON | 38d34c3 | 2013-10-11 10:44:49 +0200 | [diff] [blame] | 113 | |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 114 | regmap_read(regmap, AT91_PMC_SR, &status); |
| 115 | |
| 116 | return (status & AT91_PMC_MOSCS) && (tmp & AT91_PMC_MOSCEN); |
Boris BREZILLON | 38d34c3 | 2013-10-11 10:44:49 +0200 | [diff] [blame] | 117 | } |
| 118 | |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 119 | static const struct clk_ops main_osc_ops = { |
| 120 | .prepare = clk_main_osc_prepare, |
| 121 | .unprepare = clk_main_osc_unprepare, |
| 122 | .is_prepared = clk_main_osc_is_prepared, |
Boris BREZILLON | 38d34c3 | 2013-10-11 10:44:49 +0200 | [diff] [blame] | 123 | }; |
| 124 | |
Alexandre Belloni | b2e39dc | 2018-10-16 16:21:44 +0200 | [diff] [blame] | 125 | struct clk_hw * __init |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 126 | at91_clk_register_main_osc(struct regmap *regmap, |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 127 | const char *name, |
| 128 | const char *parent_name, |
| 129 | bool bypass) |
Boris BREZILLON | 38d34c3 | 2013-10-11 10:44:49 +0200 | [diff] [blame] | 130 | { |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 131 | struct clk_main_osc *osc; |
Boris BREZILLON | 38d34c3 | 2013-10-11 10:44:49 +0200 | [diff] [blame] | 132 | struct clk_init_data init; |
Stephen Boyd | f5644f1 | 2016-06-01 14:31:22 -0700 | [diff] [blame] | 133 | struct clk_hw *hw; |
| 134 | int ret; |
Boris BREZILLON | 38d34c3 | 2013-10-11 10:44:49 +0200 | [diff] [blame] | 135 | |
Alexandre Belloni | 99a8170 | 2015-09-16 23:47:39 +0200 | [diff] [blame] | 136 | if (!name || !parent_name) |
Boris BREZILLON | 38d34c3 | 2013-10-11 10:44:49 +0200 | [diff] [blame] | 137 | return ERR_PTR(-EINVAL); |
| 138 | |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 139 | osc = kzalloc(sizeof(*osc), GFP_KERNEL); |
| 140 | if (!osc) |
| 141 | return ERR_PTR(-ENOMEM); |
| 142 | |
| 143 | init.name = name; |
| 144 | init.ops = &main_osc_ops; |
| 145 | init.parent_names = &parent_name; |
| 146 | init.num_parents = 1; |
| 147 | init.flags = CLK_IGNORE_UNUSED; |
| 148 | |
| 149 | osc->hw.init = &init; |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 150 | osc->regmap = regmap; |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 151 | |
| 152 | if (bypass) |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 153 | regmap_update_bits(regmap, |
| 154 | AT91_CKGR_MOR, MOR_KEY_MASK | |
| 155 | AT91_PMC_MOSCEN, |
| 156 | AT91_PMC_OSCBYPASS | AT91_PMC_KEY); |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 157 | |
Stephen Boyd | f5644f1 | 2016-06-01 14:31:22 -0700 | [diff] [blame] | 158 | hw = &osc->hw; |
| 159 | ret = clk_hw_register(NULL, &osc->hw); |
| 160 | if (ret) { |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 161 | kfree(osc); |
Stephen Boyd | f5644f1 | 2016-06-01 14:31:22 -0700 | [diff] [blame] | 162 | hw = ERR_PTR(ret); |
| 163 | } |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 164 | |
Stephen Boyd | f5644f1 | 2016-06-01 14:31:22 -0700 | [diff] [blame] | 165 | return hw; |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 166 | } |
| 167 | |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 168 | static bool clk_main_rc_osc_ready(struct regmap *regmap) |
| 169 | { |
| 170 | unsigned int status; |
| 171 | |
| 172 | regmap_read(regmap, AT91_PMC_SR, &status); |
| 173 | |
| 174 | return status & AT91_PMC_MOSCRCS; |
| 175 | } |
| 176 | |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 177 | static int clk_main_rc_osc_prepare(struct clk_hw *hw) |
| 178 | { |
| 179 | struct clk_main_rc_osc *osc = to_clk_main_rc_osc(hw); |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 180 | struct regmap *regmap = osc->regmap; |
| 181 | unsigned int mor; |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 182 | |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 183 | regmap_read(regmap, AT91_CKGR_MOR, &mor); |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 184 | |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 185 | if (!(mor & AT91_PMC_MOSCRCEN)) |
| 186 | regmap_update_bits(regmap, AT91_CKGR_MOR, |
| 187 | MOR_KEY_MASK | AT91_PMC_MOSCRCEN, |
| 188 | AT91_PMC_MOSCRCEN | AT91_PMC_KEY); |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 189 | |
Alexandre Belloni | 99a8170 | 2015-09-16 23:47:39 +0200 | [diff] [blame] | 190 | while (!clk_main_rc_osc_ready(regmap)) |
| 191 | cpu_relax(); |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 192 | |
| 193 | return 0; |
| 194 | } |
| 195 | |
| 196 | static void clk_main_rc_osc_unprepare(struct clk_hw *hw) |
| 197 | { |
| 198 | struct clk_main_rc_osc *osc = to_clk_main_rc_osc(hw); |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 199 | struct regmap *regmap = osc->regmap; |
| 200 | unsigned int mor; |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 201 | |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 202 | regmap_read(regmap, AT91_CKGR_MOR, &mor); |
| 203 | |
| 204 | if (!(mor & AT91_PMC_MOSCRCEN)) |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 205 | return; |
| 206 | |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 207 | regmap_update_bits(regmap, AT91_CKGR_MOR, |
| 208 | MOR_KEY_MASK | AT91_PMC_MOSCRCEN, AT91_PMC_KEY); |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | static int clk_main_rc_osc_is_prepared(struct clk_hw *hw) |
| 212 | { |
| 213 | struct clk_main_rc_osc *osc = to_clk_main_rc_osc(hw); |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 214 | struct regmap *regmap = osc->regmap; |
| 215 | unsigned int mor, status; |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 216 | |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 217 | regmap_read(regmap, AT91_CKGR_MOR, &mor); |
| 218 | regmap_read(regmap, AT91_PMC_SR, &status); |
| 219 | |
| 220 | return (mor & AT91_PMC_MOSCRCEN) && (status & AT91_PMC_MOSCRCS); |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | static unsigned long clk_main_rc_osc_recalc_rate(struct clk_hw *hw, |
| 224 | unsigned long parent_rate) |
| 225 | { |
| 226 | struct clk_main_rc_osc *osc = to_clk_main_rc_osc(hw); |
| 227 | |
| 228 | return osc->frequency; |
| 229 | } |
| 230 | |
| 231 | static unsigned long clk_main_rc_osc_recalc_accuracy(struct clk_hw *hw, |
| 232 | unsigned long parent_acc) |
| 233 | { |
| 234 | struct clk_main_rc_osc *osc = to_clk_main_rc_osc(hw); |
| 235 | |
| 236 | return osc->accuracy; |
| 237 | } |
| 238 | |
| 239 | static const struct clk_ops main_rc_osc_ops = { |
| 240 | .prepare = clk_main_rc_osc_prepare, |
| 241 | .unprepare = clk_main_rc_osc_unprepare, |
| 242 | .is_prepared = clk_main_rc_osc_is_prepared, |
| 243 | .recalc_rate = clk_main_rc_osc_recalc_rate, |
| 244 | .recalc_accuracy = clk_main_rc_osc_recalc_accuracy, |
| 245 | }; |
| 246 | |
Alexandre Belloni | b2e39dc | 2018-10-16 16:21:44 +0200 | [diff] [blame] | 247 | struct clk_hw * __init |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 248 | at91_clk_register_main_rc_osc(struct regmap *regmap, |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 249 | const char *name, |
| 250 | u32 frequency, u32 accuracy) |
| 251 | { |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 252 | struct clk_main_rc_osc *osc; |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 253 | struct clk_init_data init; |
Stephen Boyd | f5644f1 | 2016-06-01 14:31:22 -0700 | [diff] [blame] | 254 | struct clk_hw *hw; |
| 255 | int ret; |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 256 | |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 257 | if (!name || !frequency) |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 258 | return ERR_PTR(-EINVAL); |
| 259 | |
| 260 | osc = kzalloc(sizeof(*osc), GFP_KERNEL); |
| 261 | if (!osc) |
| 262 | return ERR_PTR(-ENOMEM); |
| 263 | |
| 264 | init.name = name; |
| 265 | init.ops = &main_rc_osc_ops; |
| 266 | init.parent_names = NULL; |
| 267 | init.num_parents = 0; |
Stephen Boyd | a9bb2ef | 2016-03-01 10:59:46 -0800 | [diff] [blame] | 268 | init.flags = CLK_IGNORE_UNUSED; |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 269 | |
| 270 | osc->hw.init = &init; |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 271 | osc->regmap = regmap; |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 272 | osc->frequency = frequency; |
| 273 | osc->accuracy = accuracy; |
| 274 | |
Stephen Boyd | f5644f1 | 2016-06-01 14:31:22 -0700 | [diff] [blame] | 275 | hw = &osc->hw; |
| 276 | ret = clk_hw_register(NULL, hw); |
| 277 | if (ret) { |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 278 | kfree(osc); |
Stephen Boyd | f5644f1 | 2016-06-01 14:31:22 -0700 | [diff] [blame] | 279 | hw = ERR_PTR(ret); |
| 280 | } |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 281 | |
Stephen Boyd | f5644f1 | 2016-06-01 14:31:22 -0700 | [diff] [blame] | 282 | return hw; |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 283 | } |
| 284 | |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 285 | static int clk_main_probe_frequency(struct regmap *regmap) |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 286 | { |
| 287 | unsigned long prep_time, timeout; |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 288 | unsigned int mcfr; |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 289 | |
| 290 | timeout = jiffies + usecs_to_jiffies(MAINFRDY_TIMEOUT); |
| 291 | do { |
| 292 | prep_time = jiffies; |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 293 | regmap_read(regmap, AT91_CKGR_MCFR, &mcfr); |
| 294 | if (mcfr & AT91_PMC_MAINRDY) |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 295 | return 0; |
| 296 | usleep_range(MAINF_LOOP_MIN_WAIT, MAINF_LOOP_MAX_WAIT); |
| 297 | } while (time_before(prep_time, timeout)); |
| 298 | |
| 299 | return -ETIMEDOUT; |
| 300 | } |
| 301 | |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 302 | static unsigned long clk_main_recalc_rate(struct regmap *regmap, |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 303 | unsigned long parent_rate) |
| 304 | { |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 305 | unsigned int mcfr; |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 306 | |
| 307 | if (parent_rate) |
| 308 | return parent_rate; |
| 309 | |
Alexandre Belloni | 4da66b6 | 2014-07-01 16:12:12 +0200 | [diff] [blame] | 310 | pr_warn("Main crystal frequency not set, using approximate value\n"); |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 311 | regmap_read(regmap, AT91_CKGR_MCFR, &mcfr); |
| 312 | if (!(mcfr & AT91_PMC_MAINRDY)) |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 313 | return 0; |
| 314 | |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 315 | return ((mcfr & AT91_PMC_MAINF) * SLOW_CLOCK_FREQ) / MAINF_DIV; |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | static int clk_rm9200_main_prepare(struct clk_hw *hw) |
| 319 | { |
| 320 | struct clk_rm9200_main *clkmain = to_clk_rm9200_main(hw); |
| 321 | |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 322 | return clk_main_probe_frequency(clkmain->regmap); |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 323 | } |
| 324 | |
| 325 | static int clk_rm9200_main_is_prepared(struct clk_hw *hw) |
| 326 | { |
| 327 | struct clk_rm9200_main *clkmain = to_clk_rm9200_main(hw); |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 328 | unsigned int status; |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 329 | |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 330 | regmap_read(clkmain->regmap, AT91_CKGR_MCFR, &status); |
| 331 | |
| 332 | return status & AT91_PMC_MAINRDY ? 1 : 0; |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 333 | } |
| 334 | |
| 335 | static unsigned long clk_rm9200_main_recalc_rate(struct clk_hw *hw, |
| 336 | unsigned long parent_rate) |
| 337 | { |
| 338 | struct clk_rm9200_main *clkmain = to_clk_rm9200_main(hw); |
| 339 | |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 340 | return clk_main_recalc_rate(clkmain->regmap, parent_rate); |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | static const struct clk_ops rm9200_main_ops = { |
| 344 | .prepare = clk_rm9200_main_prepare, |
| 345 | .is_prepared = clk_rm9200_main_is_prepared, |
| 346 | .recalc_rate = clk_rm9200_main_recalc_rate, |
| 347 | }; |
| 348 | |
Alexandre Belloni | b2e39dc | 2018-10-16 16:21:44 +0200 | [diff] [blame] | 349 | struct clk_hw * __init |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 350 | at91_clk_register_rm9200_main(struct regmap *regmap, |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 351 | const char *name, |
| 352 | const char *parent_name) |
| 353 | { |
| 354 | struct clk_rm9200_main *clkmain; |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 355 | struct clk_init_data init; |
Stephen Boyd | f5644f1 | 2016-06-01 14:31:22 -0700 | [diff] [blame] | 356 | struct clk_hw *hw; |
| 357 | int ret; |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 358 | |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 359 | if (!name) |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 360 | return ERR_PTR(-EINVAL); |
| 361 | |
| 362 | if (!parent_name) |
Boris BREZILLON | 38d34c3 | 2013-10-11 10:44:49 +0200 | [diff] [blame] | 363 | return ERR_PTR(-EINVAL); |
| 364 | |
| 365 | clkmain = kzalloc(sizeof(*clkmain), GFP_KERNEL); |
| 366 | if (!clkmain) |
| 367 | return ERR_PTR(-ENOMEM); |
| 368 | |
| 369 | init.name = name; |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 370 | init.ops = &rm9200_main_ops; |
| 371 | init.parent_names = &parent_name; |
| 372 | init.num_parents = 1; |
| 373 | init.flags = 0; |
Boris BREZILLON | 38d34c3 | 2013-10-11 10:44:49 +0200 | [diff] [blame] | 374 | |
| 375 | clkmain->hw.init = &init; |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 376 | clkmain->regmap = regmap; |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 377 | |
Stephen Boyd | f5644f1 | 2016-06-01 14:31:22 -0700 | [diff] [blame] | 378 | hw = &clkmain->hw; |
| 379 | ret = clk_hw_register(NULL, &clkmain->hw); |
| 380 | if (ret) { |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 381 | kfree(clkmain); |
Stephen Boyd | f5644f1 | 2016-06-01 14:31:22 -0700 | [diff] [blame] | 382 | hw = ERR_PTR(ret); |
| 383 | } |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 384 | |
Stephen Boyd | f5644f1 | 2016-06-01 14:31:22 -0700 | [diff] [blame] | 385 | return hw; |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 386 | } |
| 387 | |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 388 | static inline bool clk_sam9x5_main_ready(struct regmap *regmap) |
| 389 | { |
| 390 | unsigned int status; |
| 391 | |
| 392 | regmap_read(regmap, AT91_PMC_SR, &status); |
| 393 | |
| 394 | return status & AT91_PMC_MOSCSELS ? 1 : 0; |
| 395 | } |
| 396 | |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 397 | static int clk_sam9x5_main_prepare(struct clk_hw *hw) |
| 398 | { |
| 399 | struct clk_sam9x5_main *clkmain = to_clk_sam9x5_main(hw); |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 400 | struct regmap *regmap = clkmain->regmap; |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 401 | |
Alexandre Belloni | 99a8170 | 2015-09-16 23:47:39 +0200 | [diff] [blame] | 402 | while (!clk_sam9x5_main_ready(regmap)) |
| 403 | cpu_relax(); |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 404 | |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 405 | return clk_main_probe_frequency(regmap); |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 406 | } |
| 407 | |
| 408 | static int clk_sam9x5_main_is_prepared(struct clk_hw *hw) |
| 409 | { |
| 410 | struct clk_sam9x5_main *clkmain = to_clk_sam9x5_main(hw); |
| 411 | |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 412 | return clk_sam9x5_main_ready(clkmain->regmap); |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 413 | } |
| 414 | |
| 415 | static unsigned long clk_sam9x5_main_recalc_rate(struct clk_hw *hw, |
| 416 | unsigned long parent_rate) |
| 417 | { |
| 418 | struct clk_sam9x5_main *clkmain = to_clk_sam9x5_main(hw); |
| 419 | |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 420 | return clk_main_recalc_rate(clkmain->regmap, parent_rate); |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 421 | } |
| 422 | |
| 423 | static int clk_sam9x5_main_set_parent(struct clk_hw *hw, u8 index) |
| 424 | { |
| 425 | struct clk_sam9x5_main *clkmain = to_clk_sam9x5_main(hw); |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 426 | struct regmap *regmap = clkmain->regmap; |
| 427 | unsigned int tmp; |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 428 | |
| 429 | if (index > 1) |
| 430 | return -EINVAL; |
| 431 | |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 432 | regmap_read(regmap, AT91_CKGR_MOR, &tmp); |
| 433 | tmp &= ~MOR_KEY_MASK; |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 434 | |
| 435 | if (index && !(tmp & AT91_PMC_MOSCSEL)) |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 436 | regmap_write(regmap, AT91_CKGR_MOR, tmp | AT91_PMC_MOSCSEL); |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 437 | else if (!index && (tmp & AT91_PMC_MOSCSEL)) |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 438 | regmap_write(regmap, AT91_CKGR_MOR, tmp & ~AT91_PMC_MOSCSEL); |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 439 | |
Alexandre Belloni | 99a8170 | 2015-09-16 23:47:39 +0200 | [diff] [blame] | 440 | while (!clk_sam9x5_main_ready(regmap)) |
| 441 | cpu_relax(); |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 442 | |
| 443 | return 0; |
| 444 | } |
| 445 | |
| 446 | static u8 clk_sam9x5_main_get_parent(struct clk_hw *hw) |
| 447 | { |
| 448 | struct clk_sam9x5_main *clkmain = to_clk_sam9x5_main(hw); |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 449 | unsigned int status; |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 450 | |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 451 | regmap_read(clkmain->regmap, AT91_CKGR_MOR, &status); |
| 452 | |
| 453 | return status & AT91_PMC_MOSCEN ? 1 : 0; |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 454 | } |
| 455 | |
| 456 | static const struct clk_ops sam9x5_main_ops = { |
| 457 | .prepare = clk_sam9x5_main_prepare, |
| 458 | .is_prepared = clk_sam9x5_main_is_prepared, |
| 459 | .recalc_rate = clk_sam9x5_main_recalc_rate, |
| 460 | .set_parent = clk_sam9x5_main_set_parent, |
| 461 | .get_parent = clk_sam9x5_main_get_parent, |
| 462 | }; |
| 463 | |
Alexandre Belloni | b2e39dc | 2018-10-16 16:21:44 +0200 | [diff] [blame] | 464 | struct clk_hw * __init |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 465 | at91_clk_register_sam9x5_main(struct regmap *regmap, |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 466 | const char *name, |
| 467 | const char **parent_names, |
| 468 | int num_parents) |
| 469 | { |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 470 | struct clk_sam9x5_main *clkmain; |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 471 | struct clk_init_data init; |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 472 | unsigned int status; |
Stephen Boyd | f5644f1 | 2016-06-01 14:31:22 -0700 | [diff] [blame] | 473 | struct clk_hw *hw; |
| 474 | int ret; |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 475 | |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 476 | if (!name) |
Boris BREZILLON | 27cb1c2 | 2014-05-07 18:00:08 +0200 | [diff] [blame] | 477 | return ERR_PTR(-EINVAL); |
| 478 | |
| 479 | if (!parent_names || !num_parents) |
| 480 | return ERR_PTR(-EINVAL); |
| 481 | |
| 482 | clkmain = kzalloc(sizeof(*clkmain), GFP_KERNEL); |
| 483 | if (!clkmain) |
| 484 | return ERR_PTR(-ENOMEM); |
| 485 | |
| 486 | init.name = name; |
| 487 | init.ops = &sam9x5_main_ops; |
| 488 | init.parent_names = parent_names; |
| 489 | init.num_parents = num_parents; |
| 490 | init.flags = CLK_SET_PARENT_GATE; |
| 491 | |
| 492 | clkmain->hw.init = &init; |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 493 | clkmain->regmap = regmap; |
Boris Brezillon | 1bdf023 | 2014-09-07 08:14:29 +0200 | [diff] [blame] | 494 | regmap_read(clkmain->regmap, AT91_CKGR_MOR, &status); |
| 495 | clkmain->parent = status & AT91_PMC_MOSCEN ? 1 : 0; |
Boris BREZILLON | 38d34c3 | 2013-10-11 10:44:49 +0200 | [diff] [blame] | 496 | |
Stephen Boyd | f5644f1 | 2016-06-01 14:31:22 -0700 | [diff] [blame] | 497 | hw = &clkmain->hw; |
| 498 | ret = clk_hw_register(NULL, &clkmain->hw); |
| 499 | if (ret) { |
Boris BREZILLON | 38d34c3 | 2013-10-11 10:44:49 +0200 | [diff] [blame] | 500 | kfree(clkmain); |
Stephen Boyd | f5644f1 | 2016-06-01 14:31:22 -0700 | [diff] [blame] | 501 | hw = ERR_PTR(ret); |
| 502 | } |
Boris BREZILLON | 38d34c3 | 2013-10-11 10:44:49 +0200 | [diff] [blame] | 503 | |
Stephen Boyd | f5644f1 | 2016-06-01 14:31:22 -0700 | [diff] [blame] | 504 | return hw; |
Boris BREZILLON | 38d34c3 | 2013-10-11 10:44:49 +0200 | [diff] [blame] | 505 | } |