Geert Uytterhoeven | f793d1e | 2015-10-16 11:41:19 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Renesas Clock Pulse Generator / Module Standby and Software Reset |
| 3 | * |
| 4 | * Copyright (C) 2015 Glider bvba |
| 5 | * |
| 6 | * Based on clk-mstp.c, clk-rcar-gen2.c, and clk-rcar-gen3.c |
| 7 | * |
| 8 | * Copyright (C) 2013 Ideas On Board SPRL |
| 9 | * Copyright (C) 2015 Renesas Electronics Corp. |
| 10 | * |
| 11 | * This program is free software; you can redistribute it and/or modify |
| 12 | * it under the terms of the GNU General Public License as published by |
| 13 | * the Free Software Foundation; version 2 of the License. |
| 14 | */ |
| 15 | |
| 16 | #include <linux/clk.h> |
| 17 | #include <linux/clk-provider.h> |
Geert Uytterhoeven | 2066390 | 2016-03-04 17:03:46 +0100 | [diff] [blame] | 18 | #include <linux/clk/renesas.h> |
Geert Uytterhoeven | 6197aa6 | 2017-01-20 11:03:03 +0100 | [diff] [blame] | 19 | #include <linux/delay.h> |
Geert Uytterhoeven | f793d1e | 2015-10-16 11:41:19 +0200 | [diff] [blame] | 20 | #include <linux/device.h> |
| 21 | #include <linux/init.h> |
| 22 | #include <linux/mod_devicetable.h> |
| 23 | #include <linux/module.h> |
| 24 | #include <linux/of_address.h> |
| 25 | #include <linux/of_device.h> |
| 26 | #include <linux/platform_device.h> |
| 27 | #include <linux/pm_clock.h> |
| 28 | #include <linux/pm_domain.h> |
Geert Uytterhoeven | 5608691 | 2017-06-07 13:20:06 +0200 | [diff] [blame] | 29 | #include <linux/psci.h> |
Geert Uytterhoeven | 6197aa6 | 2017-01-20 11:03:03 +0100 | [diff] [blame] | 30 | #include <linux/reset-controller.h> |
Geert Uytterhoeven | f793d1e | 2015-10-16 11:41:19 +0200 | [diff] [blame] | 31 | #include <linux/slab.h> |
| 32 | |
| 33 | #include <dt-bindings/clock/renesas-cpg-mssr.h> |
| 34 | |
| 35 | #include "renesas-cpg-mssr.h" |
| 36 | #include "clk-div6.h" |
| 37 | |
| 38 | #ifdef DEBUG |
Geert Uytterhoeven | f793d1e | 2015-10-16 11:41:19 +0200 | [diff] [blame] | 39 | #define WARN_DEBUG(x) WARN_ON(x) |
Geert Uytterhoeven | bc4725d | 2016-10-03 13:03:38 +0200 | [diff] [blame] | 40 | #else |
| 41 | #define WARN_DEBUG(x) do { } while (0) |
Geert Uytterhoeven | f793d1e | 2015-10-16 11:41:19 +0200 | [diff] [blame] | 42 | #endif |
| 43 | |
| 44 | |
| 45 | /* |
| 46 | * Module Standby and Software Reset register offets. |
| 47 | * |
| 48 | * If the registers exist, these are valid for SH-Mobile, R-Mobile, |
Geert Uytterhoeven | 67c995b | 2017-01-20 10:53:11 +0100 | [diff] [blame] | 49 | * R-Car Gen2, R-Car Gen3, and RZ/G1. |
Geert Uytterhoeven | f793d1e | 2015-10-16 11:41:19 +0200 | [diff] [blame] | 50 | * These are NOT valid for R-Car Gen1 and RZ/A1! |
| 51 | */ |
| 52 | |
| 53 | /* |
| 54 | * Module Stop Status Register offsets |
| 55 | */ |
| 56 | |
| 57 | static const u16 mstpsr[] = { |
| 58 | 0x030, 0x038, 0x040, 0x048, 0x04C, 0x03C, 0x1C0, 0x1C4, |
| 59 | 0x9A0, 0x9A4, 0x9A8, 0x9AC, |
| 60 | }; |
| 61 | |
| 62 | #define MSTPSR(i) mstpsr[i] |
| 63 | |
| 64 | |
| 65 | /* |
| 66 | * System Module Stop Control Register offsets |
| 67 | */ |
| 68 | |
| 69 | static const u16 smstpcr[] = { |
| 70 | 0x130, 0x134, 0x138, 0x13C, 0x140, 0x144, 0x148, 0x14C, |
| 71 | 0x990, 0x994, 0x998, 0x99C, |
| 72 | }; |
| 73 | |
| 74 | #define SMSTPCR(i) smstpcr[i] |
| 75 | |
| 76 | |
| 77 | /* |
| 78 | * Software Reset Register offsets |
| 79 | */ |
| 80 | |
| 81 | static const u16 srcr[] = { |
| 82 | 0x0A0, 0x0A8, 0x0B0, 0x0B8, 0x0BC, 0x0C4, 0x1C8, 0x1CC, |
| 83 | 0x920, 0x924, 0x928, 0x92C, |
| 84 | }; |
| 85 | |
| 86 | #define SRCR(i) srcr[i] |
| 87 | |
| 88 | |
| 89 | /* Realtime Module Stop Control Register offsets */ |
| 90 | #define RMSTPCR(i) (smstpcr[i] - 0x20) |
| 91 | |
| 92 | /* Modem Module Stop Control Register offsets (r8a73a4) */ |
| 93 | #define MMSTPCR(i) (smstpcr[i] + 0x20) |
| 94 | |
| 95 | /* Software Reset Clearing Register offsets */ |
| 96 | #define SRSTCLR(i) (0x940 + (i) * 4) |
| 97 | |
| 98 | |
| 99 | /** |
| 100 | * Clock Pulse Generator / Module Standby and Software Reset Private Data |
| 101 | * |
Geert Uytterhoeven | 6197aa6 | 2017-01-20 11:03:03 +0100 | [diff] [blame] | 102 | * @rcdev: Optional reset controller entity |
Geert Uytterhoeven | f793d1e | 2015-10-16 11:41:19 +0200 | [diff] [blame] | 103 | * @dev: CPG/MSSR device |
| 104 | * @base: CPG/MSSR register block base address |
Geert Uytterhoeven | a4ea6a0 | 2017-01-20 10:58:11 +0100 | [diff] [blame] | 105 | * @rmw_lock: protects RMW register accesses |
Geert Uytterhoeven | f793d1e | 2015-10-16 11:41:19 +0200 | [diff] [blame] | 106 | * @clks: Array containing all Core and Module Clocks |
| 107 | * @num_core_clks: Number of Core Clocks in clks[] |
| 108 | * @num_mod_clks: Number of Module Clocks in clks[] |
| 109 | * @last_dt_core_clk: ID of the last Core Clock exported to DT |
Geert Uytterhoeven | 1f4023c | 2017-06-21 22:24:15 +0200 | [diff] [blame] | 110 | * @notifiers: Notifier chain to save/restore clock state for system resume |
Geert Uytterhoeven | 5608691 | 2017-06-07 13:20:06 +0200 | [diff] [blame] | 111 | * @smstpcr_saved[].mask: Mask of SMSTPCR[] bits under our control |
| 112 | * @smstpcr_saved[].val: Saved values of SMSTPCR[] |
Geert Uytterhoeven | f793d1e | 2015-10-16 11:41:19 +0200 | [diff] [blame] | 113 | */ |
| 114 | struct cpg_mssr_priv { |
Geert Uytterhoeven | 6197aa6 | 2017-01-20 11:03:03 +0100 | [diff] [blame] | 115 | #ifdef CONFIG_RESET_CONTROLLER |
| 116 | struct reset_controller_dev rcdev; |
| 117 | #endif |
Geert Uytterhoeven | f793d1e | 2015-10-16 11:41:19 +0200 | [diff] [blame] | 118 | struct device *dev; |
| 119 | void __iomem *base; |
Geert Uytterhoeven | a4ea6a0 | 2017-01-20 10:58:11 +0100 | [diff] [blame] | 120 | spinlock_t rmw_lock; |
Geert Uytterhoeven | f793d1e | 2015-10-16 11:41:19 +0200 | [diff] [blame] | 121 | |
| 122 | struct clk **clks; |
| 123 | unsigned int num_core_clks; |
| 124 | unsigned int num_mod_clks; |
| 125 | unsigned int last_dt_core_clk; |
Geert Uytterhoeven | 5608691 | 2017-06-07 13:20:06 +0200 | [diff] [blame] | 126 | |
Geert Uytterhoeven | 1f4023c | 2017-06-21 22:24:15 +0200 | [diff] [blame] | 127 | struct raw_notifier_head notifiers; |
Geert Uytterhoeven | 5608691 | 2017-06-07 13:20:06 +0200 | [diff] [blame] | 128 | struct { |
| 129 | u32 mask; |
| 130 | u32 val; |
| 131 | } smstpcr_saved[ARRAY_SIZE(smstpcr)]; |
Geert Uytterhoeven | f793d1e | 2015-10-16 11:41:19 +0200 | [diff] [blame] | 132 | }; |
| 133 | |
| 134 | |
| 135 | /** |
| 136 | * struct mstp_clock - MSTP gating clock |
| 137 | * @hw: handle between common and hardware-specific interfaces |
| 138 | * @index: MSTP clock number |
| 139 | * @priv: CPG/MSSR private data |
| 140 | */ |
| 141 | struct mstp_clock { |
| 142 | struct clk_hw hw; |
| 143 | u32 index; |
| 144 | struct cpg_mssr_priv *priv; |
| 145 | }; |
| 146 | |
| 147 | #define to_mstp_clock(_hw) container_of(_hw, struct mstp_clock, hw) |
| 148 | |
| 149 | static int cpg_mstp_clock_endisable(struct clk_hw *hw, bool enable) |
| 150 | { |
| 151 | struct mstp_clock *clock = to_mstp_clock(hw); |
| 152 | struct cpg_mssr_priv *priv = clock->priv; |
| 153 | unsigned int reg = clock->index / 32; |
| 154 | unsigned int bit = clock->index % 32; |
| 155 | struct device *dev = priv->dev; |
| 156 | u32 bitmask = BIT(bit); |
| 157 | unsigned long flags; |
| 158 | unsigned int i; |
| 159 | u32 value; |
| 160 | |
| 161 | dev_dbg(dev, "MSTP %u%02u/%pC %s\n", reg, bit, hw->clk, |
| 162 | enable ? "ON" : "OFF"); |
Geert Uytterhoeven | a4ea6a0 | 2017-01-20 10:58:11 +0100 | [diff] [blame] | 163 | spin_lock_irqsave(&priv->rmw_lock, flags); |
Geert Uytterhoeven | f793d1e | 2015-10-16 11:41:19 +0200 | [diff] [blame] | 164 | |
Geert Uytterhoeven | c1b5371 | 2016-09-21 16:31:41 +0200 | [diff] [blame] | 165 | value = readl(priv->base + SMSTPCR(reg)); |
Geert Uytterhoeven | f793d1e | 2015-10-16 11:41:19 +0200 | [diff] [blame] | 166 | if (enable) |
| 167 | value &= ~bitmask; |
| 168 | else |
| 169 | value |= bitmask; |
Geert Uytterhoeven | c1b5371 | 2016-09-21 16:31:41 +0200 | [diff] [blame] | 170 | writel(value, priv->base + SMSTPCR(reg)); |
Geert Uytterhoeven | f793d1e | 2015-10-16 11:41:19 +0200 | [diff] [blame] | 171 | |
Geert Uytterhoeven | a4ea6a0 | 2017-01-20 10:58:11 +0100 | [diff] [blame] | 172 | spin_unlock_irqrestore(&priv->rmw_lock, flags); |
Geert Uytterhoeven | f793d1e | 2015-10-16 11:41:19 +0200 | [diff] [blame] | 173 | |
| 174 | if (!enable) |
| 175 | return 0; |
| 176 | |
| 177 | for (i = 1000; i > 0; --i) { |
Geert Uytterhoeven | c1b5371 | 2016-09-21 16:31:41 +0200 | [diff] [blame] | 178 | if (!(readl(priv->base + MSTPSR(reg)) & bitmask)) |
Geert Uytterhoeven | f793d1e | 2015-10-16 11:41:19 +0200 | [diff] [blame] | 179 | break; |
| 180 | cpu_relax(); |
| 181 | } |
| 182 | |
| 183 | if (!i) { |
| 184 | dev_err(dev, "Failed to enable SMSTP %p[%d]\n", |
| 185 | priv->base + SMSTPCR(reg), bit); |
| 186 | return -ETIMEDOUT; |
| 187 | } |
| 188 | |
| 189 | return 0; |
| 190 | } |
| 191 | |
| 192 | static int cpg_mstp_clock_enable(struct clk_hw *hw) |
| 193 | { |
| 194 | return cpg_mstp_clock_endisable(hw, true); |
| 195 | } |
| 196 | |
| 197 | static void cpg_mstp_clock_disable(struct clk_hw *hw) |
| 198 | { |
| 199 | cpg_mstp_clock_endisable(hw, false); |
| 200 | } |
| 201 | |
| 202 | static int cpg_mstp_clock_is_enabled(struct clk_hw *hw) |
| 203 | { |
| 204 | struct mstp_clock *clock = to_mstp_clock(hw); |
| 205 | struct cpg_mssr_priv *priv = clock->priv; |
| 206 | u32 value; |
| 207 | |
Geert Uytterhoeven | c1b5371 | 2016-09-21 16:31:41 +0200 | [diff] [blame] | 208 | value = readl(priv->base + MSTPSR(clock->index / 32)); |
Geert Uytterhoeven | f793d1e | 2015-10-16 11:41:19 +0200 | [diff] [blame] | 209 | |
| 210 | return !(value & BIT(clock->index % 32)); |
| 211 | } |
| 212 | |
| 213 | static const struct clk_ops cpg_mstp_clock_ops = { |
| 214 | .enable = cpg_mstp_clock_enable, |
| 215 | .disable = cpg_mstp_clock_disable, |
| 216 | .is_enabled = cpg_mstp_clock_is_enabled, |
| 217 | }; |
| 218 | |
| 219 | static |
| 220 | struct clk *cpg_mssr_clk_src_twocell_get(struct of_phandle_args *clkspec, |
| 221 | void *data) |
| 222 | { |
| 223 | unsigned int clkidx = clkspec->args[1]; |
| 224 | struct cpg_mssr_priv *priv = data; |
| 225 | struct device *dev = priv->dev; |
| 226 | unsigned int idx; |
| 227 | const char *type; |
| 228 | struct clk *clk; |
| 229 | |
| 230 | switch (clkspec->args[0]) { |
| 231 | case CPG_CORE: |
| 232 | type = "core"; |
| 233 | if (clkidx > priv->last_dt_core_clk) { |
| 234 | dev_err(dev, "Invalid %s clock index %u\n", type, |
| 235 | clkidx); |
| 236 | return ERR_PTR(-EINVAL); |
| 237 | } |
| 238 | clk = priv->clks[clkidx]; |
| 239 | break; |
| 240 | |
| 241 | case CPG_MOD: |
| 242 | type = "module"; |
| 243 | idx = MOD_CLK_PACK(clkidx); |
| 244 | if (clkidx % 100 > 31 || idx >= priv->num_mod_clks) { |
| 245 | dev_err(dev, "Invalid %s clock index %u\n", type, |
| 246 | clkidx); |
| 247 | return ERR_PTR(-EINVAL); |
| 248 | } |
| 249 | clk = priv->clks[priv->num_core_clks + idx]; |
| 250 | break; |
| 251 | |
| 252 | default: |
| 253 | dev_err(dev, "Invalid CPG clock type %u\n", clkspec->args[0]); |
| 254 | return ERR_PTR(-EINVAL); |
| 255 | } |
| 256 | |
| 257 | if (IS_ERR(clk)) |
| 258 | dev_err(dev, "Cannot get %s clock %u: %ld", type, clkidx, |
| 259 | PTR_ERR(clk)); |
| 260 | else |
| 261 | dev_dbg(dev, "clock (%u, %u) is %pC at %pCr Hz\n", |
| 262 | clkspec->args[0], clkspec->args[1], clk, clk); |
| 263 | return clk; |
| 264 | } |
| 265 | |
| 266 | static void __init cpg_mssr_register_core_clk(const struct cpg_core_clk *core, |
| 267 | const struct cpg_mssr_info *info, |
| 268 | struct cpg_mssr_priv *priv) |
| 269 | { |
Geert Uytterhoeven | 76394a36 | 2017-05-17 15:43:56 +0200 | [diff] [blame] | 270 | struct clk *clk = ERR_PTR(-ENOTSUPP), *parent; |
Geert Uytterhoeven | f793d1e | 2015-10-16 11:41:19 +0200 | [diff] [blame] | 271 | struct device *dev = priv->dev; |
Wolfram Sang | 5d3927f | 2016-03-30 16:58:18 +0200 | [diff] [blame] | 272 | unsigned int id = core->id, div = core->div; |
Geert Uytterhoeven | f793d1e | 2015-10-16 11:41:19 +0200 | [diff] [blame] | 273 | const char *parent_name; |
| 274 | |
| 275 | WARN_DEBUG(id >= priv->num_core_clks); |
| 276 | WARN_DEBUG(PTR_ERR(priv->clks[id]) != -ENOENT); |
| 277 | |
Geert Uytterhoeven | 48d0341 | 2016-09-29 14:47:58 +0200 | [diff] [blame] | 278 | if (!core->name) { |
| 279 | /* Skip NULLified clock */ |
| 280 | return; |
| 281 | } |
| 282 | |
Geert Uytterhoeven | f793d1e | 2015-10-16 11:41:19 +0200 | [diff] [blame] | 283 | switch (core->type) { |
| 284 | case CLK_TYPE_IN: |
| 285 | clk = of_clk_get_by_name(priv->dev->of_node, core->name); |
| 286 | break; |
| 287 | |
| 288 | case CLK_TYPE_FF: |
| 289 | case CLK_TYPE_DIV6P1: |
Wolfram Sang | 5d3927f | 2016-03-30 16:58:18 +0200 | [diff] [blame] | 290 | case CLK_TYPE_DIV6_RO: |
Geert Uytterhoeven | f793d1e | 2015-10-16 11:41:19 +0200 | [diff] [blame] | 291 | WARN_DEBUG(core->parent >= priv->num_core_clks); |
| 292 | parent = priv->clks[core->parent]; |
| 293 | if (IS_ERR(parent)) { |
| 294 | clk = parent; |
| 295 | goto fail; |
| 296 | } |
| 297 | |
| 298 | parent_name = __clk_get_name(parent); |
Wolfram Sang | 5d3927f | 2016-03-30 16:58:18 +0200 | [diff] [blame] | 299 | |
| 300 | if (core->type == CLK_TYPE_DIV6_RO) |
| 301 | /* Multiply with the DIV6 register value */ |
| 302 | div *= (readl(priv->base + core->offset) & 0x3f) + 1; |
| 303 | |
| 304 | if (core->type == CLK_TYPE_DIV6P1) { |
Geert Uytterhoeven | f793d1e | 2015-10-16 11:41:19 +0200 | [diff] [blame] | 305 | clk = cpg_div6_register(core->name, 1, &parent_name, |
Geert Uytterhoeven | 9f8c71e | 2017-06-21 22:34:33 +0200 | [diff] [blame] | 306 | priv->base + core->offset, |
| 307 | &priv->notifiers); |
Wolfram Sang | 5d3927f | 2016-03-30 16:58:18 +0200 | [diff] [blame] | 308 | } else { |
| 309 | clk = clk_register_fixed_factor(NULL, core->name, |
| 310 | parent_name, 0, |
| 311 | core->mult, div); |
Geert Uytterhoeven | f793d1e | 2015-10-16 11:41:19 +0200 | [diff] [blame] | 312 | } |
| 313 | break; |
| 314 | |
| 315 | default: |
| 316 | if (info->cpg_clk_register) |
| 317 | clk = info->cpg_clk_register(dev, core, info, |
Geert Uytterhoeven | 1f4023c | 2017-06-21 22:24:15 +0200 | [diff] [blame] | 318 | priv->clks, priv->base, |
| 319 | &priv->notifiers); |
Geert Uytterhoeven | f793d1e | 2015-10-16 11:41:19 +0200 | [diff] [blame] | 320 | else |
| 321 | dev_err(dev, "%s has unsupported core clock type %u\n", |
| 322 | core->name, core->type); |
| 323 | break; |
| 324 | } |
| 325 | |
| 326 | if (IS_ERR_OR_NULL(clk)) |
| 327 | goto fail; |
| 328 | |
| 329 | dev_dbg(dev, "Core clock %pC at %pCr Hz\n", clk, clk); |
| 330 | priv->clks[id] = clk; |
| 331 | return; |
| 332 | |
| 333 | fail: |
Geert Uytterhoeven | 1b9fe70 | 2016-10-18 15:59:13 +0200 | [diff] [blame] | 334 | dev_err(dev, "Failed to register %s clock %s: %ld\n", "core", |
Geert Uytterhoeven | f793d1e | 2015-10-16 11:41:19 +0200 | [diff] [blame] | 335 | core->name, PTR_ERR(clk)); |
| 336 | } |
| 337 | |
| 338 | static void __init cpg_mssr_register_mod_clk(const struct mssr_mod_clk *mod, |
| 339 | const struct cpg_mssr_info *info, |
| 340 | struct cpg_mssr_priv *priv) |
| 341 | { |
| 342 | struct mstp_clock *clock = NULL; |
| 343 | struct device *dev = priv->dev; |
| 344 | unsigned int id = mod->id; |
| 345 | struct clk_init_data init; |
| 346 | struct clk *parent, *clk; |
| 347 | const char *parent_name; |
| 348 | unsigned int i; |
| 349 | |
| 350 | WARN_DEBUG(id < priv->num_core_clks); |
| 351 | WARN_DEBUG(id >= priv->num_core_clks + priv->num_mod_clks); |
| 352 | WARN_DEBUG(mod->parent >= priv->num_core_clks + priv->num_mod_clks); |
| 353 | WARN_DEBUG(PTR_ERR(priv->clks[id]) != -ENOENT); |
| 354 | |
Geert Uytterhoeven | 48d0341 | 2016-09-29 14:47:58 +0200 | [diff] [blame] | 355 | if (!mod->name) { |
| 356 | /* Skip NULLified clock */ |
| 357 | return; |
| 358 | } |
| 359 | |
Geert Uytterhoeven | f793d1e | 2015-10-16 11:41:19 +0200 | [diff] [blame] | 360 | parent = priv->clks[mod->parent]; |
| 361 | if (IS_ERR(parent)) { |
| 362 | clk = parent; |
| 363 | goto fail; |
| 364 | } |
| 365 | |
| 366 | clock = kzalloc(sizeof(*clock), GFP_KERNEL); |
| 367 | if (!clock) { |
| 368 | clk = ERR_PTR(-ENOMEM); |
| 369 | goto fail; |
| 370 | } |
| 371 | |
| 372 | init.name = mod->name; |
| 373 | init.ops = &cpg_mstp_clock_ops; |
| 374 | init.flags = CLK_IS_BASIC | CLK_SET_RATE_PARENT; |
| 375 | for (i = 0; i < info->num_crit_mod_clks; i++) |
| 376 | if (id == info->crit_mod_clks[i]) { |
Geert Uytterhoeven | 72f5df2c | 2017-01-16 16:15:50 +0100 | [diff] [blame] | 377 | dev_dbg(dev, "MSTP %s setting CLK_IS_CRITICAL\n", |
Geert Uytterhoeven | f793d1e | 2015-10-16 11:41:19 +0200 | [diff] [blame] | 378 | mod->name); |
Geert Uytterhoeven | 72f5df2c | 2017-01-16 16:15:50 +0100 | [diff] [blame] | 379 | init.flags |= CLK_IS_CRITICAL; |
Geert Uytterhoeven | f793d1e | 2015-10-16 11:41:19 +0200 | [diff] [blame] | 380 | break; |
Geert Uytterhoeven | f793d1e | 2015-10-16 11:41:19 +0200 | [diff] [blame] | 381 | } |
| 382 | |
| 383 | parent_name = __clk_get_name(parent); |
| 384 | init.parent_names = &parent_name; |
| 385 | init.num_parents = 1; |
| 386 | |
| 387 | clock->index = id - priv->num_core_clks; |
| 388 | clock->priv = priv; |
| 389 | clock->hw.init = &init; |
| 390 | |
| 391 | clk = clk_register(NULL, &clock->hw); |
| 392 | if (IS_ERR(clk)) |
| 393 | goto fail; |
| 394 | |
| 395 | dev_dbg(dev, "Module clock %pC at %pCr Hz\n", clk, clk); |
| 396 | priv->clks[id] = clk; |
Geert Uytterhoeven | 5608691 | 2017-06-07 13:20:06 +0200 | [diff] [blame] | 397 | priv->smstpcr_saved[clock->index / 32].mask |= BIT(clock->index % 32); |
Geert Uytterhoeven | f793d1e | 2015-10-16 11:41:19 +0200 | [diff] [blame] | 398 | return; |
| 399 | |
| 400 | fail: |
Geert Uytterhoeven | 1b9fe70 | 2016-10-18 15:59:13 +0200 | [diff] [blame] | 401 | dev_err(dev, "Failed to register %s clock %s: %ld\n", "module", |
Geert Uytterhoeven | f793d1e | 2015-10-16 11:41:19 +0200 | [diff] [blame] | 402 | mod->name, PTR_ERR(clk)); |
| 403 | kfree(clock); |
| 404 | } |
| 405 | |
Geert Uytterhoeven | f793d1e | 2015-10-16 11:41:19 +0200 | [diff] [blame] | 406 | struct cpg_mssr_clk_domain { |
| 407 | struct generic_pm_domain genpd; |
| 408 | struct device_node *np; |
| 409 | unsigned int num_core_pm_clks; |
| 410 | unsigned int core_pm_clks[0]; |
| 411 | }; |
| 412 | |
Geert Uytterhoeven | 2066390 | 2016-03-04 17:03:46 +0100 | [diff] [blame] | 413 | static struct cpg_mssr_clk_domain *cpg_mssr_clk_domain; |
| 414 | |
Geert Uytterhoeven | f793d1e | 2015-10-16 11:41:19 +0200 | [diff] [blame] | 415 | static bool cpg_mssr_is_pm_clk(const struct of_phandle_args *clkspec, |
| 416 | struct cpg_mssr_clk_domain *pd) |
| 417 | { |
| 418 | unsigned int i; |
| 419 | |
| 420 | if (clkspec->np != pd->np || clkspec->args_count != 2) |
| 421 | return false; |
| 422 | |
| 423 | switch (clkspec->args[0]) { |
| 424 | case CPG_CORE: |
| 425 | for (i = 0; i < pd->num_core_pm_clks; i++) |
| 426 | if (clkspec->args[1] == pd->core_pm_clks[i]) |
| 427 | return true; |
| 428 | return false; |
| 429 | |
| 430 | case CPG_MOD: |
| 431 | return true; |
| 432 | |
| 433 | default: |
| 434 | return false; |
| 435 | } |
| 436 | } |
| 437 | |
Geert Uytterhoeven | 2066390 | 2016-03-04 17:03:46 +0100 | [diff] [blame] | 438 | int cpg_mssr_attach_dev(struct generic_pm_domain *unused, struct device *dev) |
Geert Uytterhoeven | f793d1e | 2015-10-16 11:41:19 +0200 | [diff] [blame] | 439 | { |
Geert Uytterhoeven | 2066390 | 2016-03-04 17:03:46 +0100 | [diff] [blame] | 440 | struct cpg_mssr_clk_domain *pd = cpg_mssr_clk_domain; |
Geert Uytterhoeven | f793d1e | 2015-10-16 11:41:19 +0200 | [diff] [blame] | 441 | struct device_node *np = dev->of_node; |
| 442 | struct of_phandle_args clkspec; |
| 443 | struct clk *clk; |
| 444 | int i = 0; |
| 445 | int error; |
| 446 | |
Geert Uytterhoeven | 2066390 | 2016-03-04 17:03:46 +0100 | [diff] [blame] | 447 | if (!pd) { |
| 448 | dev_dbg(dev, "CPG/MSSR clock domain not yet available\n"); |
| 449 | return -EPROBE_DEFER; |
| 450 | } |
| 451 | |
Geert Uytterhoeven | f793d1e | 2015-10-16 11:41:19 +0200 | [diff] [blame] | 452 | while (!of_parse_phandle_with_args(np, "clocks", "#clock-cells", i, |
| 453 | &clkspec)) { |
| 454 | if (cpg_mssr_is_pm_clk(&clkspec, pd)) |
| 455 | goto found; |
| 456 | |
| 457 | of_node_put(clkspec.np); |
| 458 | i++; |
| 459 | } |
| 460 | |
| 461 | return 0; |
| 462 | |
| 463 | found: |
| 464 | clk = of_clk_get_from_provider(&clkspec); |
| 465 | of_node_put(clkspec.np); |
| 466 | |
| 467 | if (IS_ERR(clk)) |
| 468 | return PTR_ERR(clk); |
| 469 | |
| 470 | error = pm_clk_create(dev); |
| 471 | if (error) { |
| 472 | dev_err(dev, "pm_clk_create failed %d\n", error); |
| 473 | goto fail_put; |
| 474 | } |
| 475 | |
| 476 | error = pm_clk_add_clk(dev, clk); |
| 477 | if (error) { |
| 478 | dev_err(dev, "pm_clk_add_clk %pC failed %d\n", clk, error); |
| 479 | goto fail_destroy; |
| 480 | } |
| 481 | |
| 482 | return 0; |
| 483 | |
| 484 | fail_destroy: |
| 485 | pm_clk_destroy(dev); |
| 486 | fail_put: |
| 487 | clk_put(clk); |
| 488 | return error; |
| 489 | } |
| 490 | |
Geert Uytterhoeven | 2066390 | 2016-03-04 17:03:46 +0100 | [diff] [blame] | 491 | void cpg_mssr_detach_dev(struct generic_pm_domain *unused, struct device *dev) |
Geert Uytterhoeven | f793d1e | 2015-10-16 11:41:19 +0200 | [diff] [blame] | 492 | { |
Geert Uytterhoeven | e05e853 | 2017-02-08 19:08:44 +0100 | [diff] [blame] | 493 | if (!pm_clk_no_clocks(dev)) |
Geert Uytterhoeven | f793d1e | 2015-10-16 11:41:19 +0200 | [diff] [blame] | 494 | pm_clk_destroy(dev); |
| 495 | } |
| 496 | |
| 497 | static int __init cpg_mssr_add_clk_domain(struct device *dev, |
| 498 | const unsigned int *core_pm_clks, |
| 499 | unsigned int num_core_pm_clks) |
| 500 | { |
| 501 | struct device_node *np = dev->of_node; |
| 502 | struct generic_pm_domain *genpd; |
| 503 | struct cpg_mssr_clk_domain *pd; |
| 504 | size_t pm_size = num_core_pm_clks * sizeof(core_pm_clks[0]); |
| 505 | |
| 506 | pd = devm_kzalloc(dev, sizeof(*pd) + pm_size, GFP_KERNEL); |
| 507 | if (!pd) |
| 508 | return -ENOMEM; |
| 509 | |
| 510 | pd->np = np; |
| 511 | pd->num_core_pm_clks = num_core_pm_clks; |
| 512 | memcpy(pd->core_pm_clks, core_pm_clks, pm_size); |
| 513 | |
| 514 | genpd = &pd->genpd; |
| 515 | genpd->name = np->name; |
Geert Uytterhoeven | 7aff266 | 2017-10-13 14:24:22 +0200 | [diff] [blame] | 516 | genpd->flags = GENPD_FLAG_PM_CLK | GENPD_FLAG_ACTIVE_WAKEUP; |
Geert Uytterhoeven | f793d1e | 2015-10-16 11:41:19 +0200 | [diff] [blame] | 517 | genpd->attach_dev = cpg_mssr_attach_dev; |
| 518 | genpd->detach_dev = cpg_mssr_detach_dev; |
Geert Uytterhoeven | d04a75a | 2016-04-22 14:59:10 +0200 | [diff] [blame] | 519 | pm_genpd_init(genpd, &pm_domain_always_on_gov, false); |
Geert Uytterhoeven | 2066390 | 2016-03-04 17:03:46 +0100 | [diff] [blame] | 520 | cpg_mssr_clk_domain = pd; |
Geert Uytterhoeven | f793d1e | 2015-10-16 11:41:19 +0200 | [diff] [blame] | 521 | |
| 522 | of_genpd_add_provider_simple(np, genpd); |
| 523 | return 0; |
| 524 | } |
Geert Uytterhoeven | f793d1e | 2015-10-16 11:41:19 +0200 | [diff] [blame] | 525 | |
Geert Uytterhoeven | 6197aa6 | 2017-01-20 11:03:03 +0100 | [diff] [blame] | 526 | #ifdef CONFIG_RESET_CONTROLLER |
| 527 | |
| 528 | #define rcdev_to_priv(x) container_of(x, struct cpg_mssr_priv, rcdev) |
| 529 | |
| 530 | static int cpg_mssr_reset(struct reset_controller_dev *rcdev, |
| 531 | unsigned long id) |
| 532 | { |
| 533 | struct cpg_mssr_priv *priv = rcdev_to_priv(rcdev); |
| 534 | unsigned int reg = id / 32; |
| 535 | unsigned int bit = id % 32; |
| 536 | u32 bitmask = BIT(bit); |
| 537 | unsigned long flags; |
| 538 | u32 value; |
| 539 | |
| 540 | dev_dbg(priv->dev, "reset %u%02u\n", reg, bit); |
| 541 | |
| 542 | /* Reset module */ |
| 543 | spin_lock_irqsave(&priv->rmw_lock, flags); |
| 544 | value = readl(priv->base + SRCR(reg)); |
| 545 | value |= bitmask; |
| 546 | writel(value, priv->base + SRCR(reg)); |
| 547 | spin_unlock_irqrestore(&priv->rmw_lock, flags); |
| 548 | |
| 549 | /* Wait for at least one cycle of the RCLK clock (@ ca. 32 kHz) */ |
| 550 | udelay(35); |
| 551 | |
| 552 | /* Release module from reset state */ |
| 553 | writel(bitmask, priv->base + SRSTCLR(reg)); |
| 554 | |
| 555 | return 0; |
| 556 | } |
| 557 | |
| 558 | static int cpg_mssr_assert(struct reset_controller_dev *rcdev, unsigned long id) |
| 559 | { |
| 560 | struct cpg_mssr_priv *priv = rcdev_to_priv(rcdev); |
| 561 | unsigned int reg = id / 32; |
| 562 | unsigned int bit = id % 32; |
| 563 | u32 bitmask = BIT(bit); |
| 564 | unsigned long flags; |
| 565 | u32 value; |
| 566 | |
| 567 | dev_dbg(priv->dev, "assert %u%02u\n", reg, bit); |
| 568 | |
| 569 | spin_lock_irqsave(&priv->rmw_lock, flags); |
| 570 | value = readl(priv->base + SRCR(reg)); |
| 571 | value |= bitmask; |
| 572 | writel(value, priv->base + SRCR(reg)); |
| 573 | spin_unlock_irqrestore(&priv->rmw_lock, flags); |
| 574 | return 0; |
| 575 | } |
| 576 | |
| 577 | static int cpg_mssr_deassert(struct reset_controller_dev *rcdev, |
| 578 | unsigned long id) |
| 579 | { |
| 580 | struct cpg_mssr_priv *priv = rcdev_to_priv(rcdev); |
| 581 | unsigned int reg = id / 32; |
| 582 | unsigned int bit = id % 32; |
| 583 | u32 bitmask = BIT(bit); |
| 584 | |
| 585 | dev_dbg(priv->dev, "deassert %u%02u\n", reg, bit); |
| 586 | |
| 587 | writel(bitmask, priv->base + SRSTCLR(reg)); |
| 588 | return 0; |
| 589 | } |
| 590 | |
| 591 | static int cpg_mssr_status(struct reset_controller_dev *rcdev, |
| 592 | unsigned long id) |
| 593 | { |
| 594 | struct cpg_mssr_priv *priv = rcdev_to_priv(rcdev); |
| 595 | unsigned int reg = id / 32; |
| 596 | unsigned int bit = id % 32; |
| 597 | u32 bitmask = BIT(bit); |
| 598 | |
| 599 | return !!(readl(priv->base + SRCR(reg)) & bitmask); |
| 600 | } |
| 601 | |
| 602 | static const struct reset_control_ops cpg_mssr_reset_ops = { |
| 603 | .reset = cpg_mssr_reset, |
| 604 | .assert = cpg_mssr_assert, |
| 605 | .deassert = cpg_mssr_deassert, |
| 606 | .status = cpg_mssr_status, |
| 607 | }; |
| 608 | |
| 609 | static int cpg_mssr_reset_xlate(struct reset_controller_dev *rcdev, |
| 610 | const struct of_phandle_args *reset_spec) |
| 611 | { |
| 612 | struct cpg_mssr_priv *priv = rcdev_to_priv(rcdev); |
| 613 | unsigned int unpacked = reset_spec->args[0]; |
| 614 | unsigned int idx = MOD_CLK_PACK(unpacked); |
| 615 | |
| 616 | if (unpacked % 100 > 31 || idx >= rcdev->nr_resets) { |
| 617 | dev_err(priv->dev, "Invalid reset index %u\n", unpacked); |
| 618 | return -EINVAL; |
| 619 | } |
| 620 | |
| 621 | return idx; |
| 622 | } |
| 623 | |
| 624 | static int cpg_mssr_reset_controller_register(struct cpg_mssr_priv *priv) |
| 625 | { |
| 626 | priv->rcdev.ops = &cpg_mssr_reset_ops; |
| 627 | priv->rcdev.of_node = priv->dev->of_node; |
| 628 | priv->rcdev.of_reset_n_cells = 1; |
| 629 | priv->rcdev.of_xlate = cpg_mssr_reset_xlate; |
| 630 | priv->rcdev.nr_resets = priv->num_mod_clks; |
| 631 | return devm_reset_controller_register(priv->dev, &priv->rcdev); |
| 632 | } |
| 633 | |
| 634 | #else /* !CONFIG_RESET_CONTROLLER */ |
| 635 | static inline int cpg_mssr_reset_controller_register(struct cpg_mssr_priv *priv) |
| 636 | { |
| 637 | return 0; |
| 638 | } |
| 639 | #endif /* !CONFIG_RESET_CONTROLLER */ |
| 640 | |
| 641 | |
Geert Uytterhoeven | f793d1e | 2015-10-16 11:41:19 +0200 | [diff] [blame] | 642 | static const struct of_device_id cpg_mssr_match[] = { |
Geert Uytterhoeven | 80978a4 | 2017-04-24 16:54:14 +0200 | [diff] [blame] | 643 | #ifdef CONFIG_CLK_R8A7743 |
Sergei Shtylyov | c0b2d75 | 2016-11-09 00:21:50 +0300 | [diff] [blame] | 644 | { |
| 645 | .compatible = "renesas,r8a7743-cpg-mssr", |
| 646 | .data = &r8a7743_cpg_mssr_info, |
| 647 | }, |
| 648 | #endif |
Geert Uytterhoeven | 80978a4 | 2017-04-24 16:54:14 +0200 | [diff] [blame] | 649 | #ifdef CONFIG_CLK_R8A7745 |
Sergei Shtylyov | 9127d54 | 2016-11-09 00:25:08 +0300 | [diff] [blame] | 650 | { |
| 651 | .compatible = "renesas,r8a7745-cpg-mssr", |
| 652 | .data = &r8a7745_cpg_mssr_info, |
| 653 | }, |
| 654 | #endif |
Geert Uytterhoeven | d4e59f1 | 2017-03-19 18:05:42 +0100 | [diff] [blame] | 655 | #ifdef CONFIG_CLK_R8A7790 |
| 656 | { |
| 657 | .compatible = "renesas,r8a7790-cpg-mssr", |
| 658 | .data = &r8a7790_cpg_mssr_info, |
| 659 | }, |
| 660 | #endif |
Geert Uytterhoeven | 6449ab8 | 2015-10-16 11:41:19 +0200 | [diff] [blame] | 661 | #ifdef CONFIG_CLK_R8A7791 |
| 662 | { |
| 663 | .compatible = "renesas,r8a7791-cpg-mssr", |
| 664 | .data = &r8a7791_cpg_mssr_info, |
| 665 | }, |
| 666 | /* R-Car M2-N is (almost) identical to R-Car M2-W w.r.t. clocks. */ |
| 667 | { |
| 668 | .compatible = "renesas,r8a7793-cpg-mssr", |
| 669 | .data = &r8a7791_cpg_mssr_info, |
| 670 | }, |
| 671 | #endif |
Geert Uytterhoeven | fd3c2f3 | 2017-03-19 18:08:59 +0100 | [diff] [blame] | 672 | #ifdef CONFIG_CLK_R8A7792 |
| 673 | { |
| 674 | .compatible = "renesas,r8a7792-cpg-mssr", |
| 675 | .data = &r8a7792_cpg_mssr_info, |
| 676 | }, |
| 677 | #endif |
Geert Uytterhoeven | 2d75588 | 2017-03-19 18:12:51 +0100 | [diff] [blame] | 678 | #ifdef CONFIG_CLK_R8A7794 |
| 679 | { |
| 680 | .compatible = "renesas,r8a7794-cpg-mssr", |
| 681 | .data = &r8a7794_cpg_mssr_info, |
| 682 | }, |
| 683 | #endif |
Geert Uytterhoeven | 80978a4 | 2017-04-24 16:54:14 +0200 | [diff] [blame] | 684 | #ifdef CONFIG_CLK_R8A7795 |
Geert Uytterhoeven | c5dae0d | 2015-10-16 11:41:19 +0200 | [diff] [blame] | 685 | { |
| 686 | .compatible = "renesas,r8a7795-cpg-mssr", |
| 687 | .data = &r8a7795_cpg_mssr_info, |
| 688 | }, |
| 689 | #endif |
Geert Uytterhoeven | 80978a4 | 2017-04-24 16:54:14 +0200 | [diff] [blame] | 690 | #ifdef CONFIG_CLK_R8A7796 |
Geert Uytterhoeven | e4e2d7c | 2016-05-03 11:06:15 +0200 | [diff] [blame] | 691 | { |
| 692 | .compatible = "renesas,r8a7796-cpg-mssr", |
| 693 | .data = &r8a7796_cpg_mssr_info, |
| 694 | }, |
| 695 | #endif |
Jacopo Mondi | 7ce36da9 | 2018-02-20 16:12:03 +0100 | [diff] [blame^] | 696 | #ifdef CONFIG_CLK_R8A77965 |
| 697 | { |
| 698 | .compatible = "renesas,r8a77965-cpg-mssr", |
| 699 | .data = &r8a77965_cpg_mssr_info, |
| 700 | }, |
| 701 | #endif |
Sergei Shtylyov | 8d46e28 | 2017-09-09 00:34:20 +0300 | [diff] [blame] | 702 | #ifdef CONFIG_CLK_R8A77970 |
| 703 | { |
| 704 | .compatible = "renesas,r8a77970-cpg-mssr", |
| 705 | .data = &r8a77970_cpg_mssr_info, |
| 706 | }, |
| 707 | #endif |
Sergei Shtylyov | ce15783 | 2018-02-15 14:58:45 +0300 | [diff] [blame] | 708 | #ifdef CONFIG_ARCH_R8A77980 |
| 709 | { |
| 710 | .compatible = "renesas,r8a77980-cpg-mssr", |
| 711 | .data = &r8a77980_cpg_mssr_info, |
| 712 | }, |
| 713 | #endif |
Geert Uytterhoeven | d71e851 | 2017-07-12 10:47:36 +0200 | [diff] [blame] | 714 | #ifdef CONFIG_CLK_R8A77995 |
| 715 | { |
| 716 | .compatible = "renesas,r8a77995-cpg-mssr", |
| 717 | .data = &r8a77995_cpg_mssr_info, |
| 718 | }, |
| 719 | #endif |
Geert Uytterhoeven | f793d1e | 2015-10-16 11:41:19 +0200 | [diff] [blame] | 720 | { /* sentinel */ } |
| 721 | }; |
| 722 | |
| 723 | static void cpg_mssr_del_clk_provider(void *data) |
| 724 | { |
| 725 | of_clk_del_provider(data); |
| 726 | } |
| 727 | |
Geert Uytterhoeven | 5608691 | 2017-06-07 13:20:06 +0200 | [diff] [blame] | 728 | #if defined(CONFIG_PM_SLEEP) && defined(CONFIG_ARM_PSCI_FW) |
| 729 | static int cpg_mssr_suspend_noirq(struct device *dev) |
| 730 | { |
| 731 | struct cpg_mssr_priv *priv = dev_get_drvdata(dev); |
| 732 | unsigned int reg; |
| 733 | |
| 734 | /* This is the best we can do to check for the presence of PSCI */ |
| 735 | if (!psci_ops.cpu_suspend) |
| 736 | return 0; |
| 737 | |
| 738 | /* Save module registers with bits under our control */ |
| 739 | for (reg = 0; reg < ARRAY_SIZE(priv->smstpcr_saved); reg++) { |
| 740 | if (priv->smstpcr_saved[reg].mask) |
| 741 | priv->smstpcr_saved[reg].val = |
| 742 | readl(priv->base + SMSTPCR(reg)); |
| 743 | } |
| 744 | |
Geert Uytterhoeven | 1f4023c | 2017-06-21 22:24:15 +0200 | [diff] [blame] | 745 | /* Save core clocks */ |
| 746 | raw_notifier_call_chain(&priv->notifiers, PM_EVENT_SUSPEND, NULL); |
| 747 | |
Geert Uytterhoeven | 5608691 | 2017-06-07 13:20:06 +0200 | [diff] [blame] | 748 | return 0; |
| 749 | } |
| 750 | |
| 751 | static int cpg_mssr_resume_noirq(struct device *dev) |
| 752 | { |
| 753 | struct cpg_mssr_priv *priv = dev_get_drvdata(dev); |
| 754 | unsigned int reg, i; |
| 755 | u32 mask, oldval, newval; |
| 756 | |
| 757 | /* This is the best we can do to check for the presence of PSCI */ |
| 758 | if (!psci_ops.cpu_suspend) |
| 759 | return 0; |
| 760 | |
Geert Uytterhoeven | 1f4023c | 2017-06-21 22:24:15 +0200 | [diff] [blame] | 761 | /* Restore core clocks */ |
| 762 | raw_notifier_call_chain(&priv->notifiers, PM_EVENT_RESUME, NULL); |
| 763 | |
Geert Uytterhoeven | 5608691 | 2017-06-07 13:20:06 +0200 | [diff] [blame] | 764 | /* Restore module clocks */ |
| 765 | for (reg = 0; reg < ARRAY_SIZE(priv->smstpcr_saved); reg++) { |
| 766 | mask = priv->smstpcr_saved[reg].mask; |
| 767 | if (!mask) |
| 768 | continue; |
| 769 | |
| 770 | oldval = readl(priv->base + SMSTPCR(reg)); |
| 771 | newval = oldval & ~mask; |
| 772 | newval |= priv->smstpcr_saved[reg].val & mask; |
| 773 | if (newval == oldval) |
| 774 | continue; |
| 775 | |
| 776 | writel(newval, priv->base + SMSTPCR(reg)); |
| 777 | |
| 778 | /* Wait until enabled clocks are really enabled */ |
| 779 | mask &= ~priv->smstpcr_saved[reg].val; |
| 780 | if (!mask) |
| 781 | continue; |
| 782 | |
| 783 | for (i = 1000; i > 0; --i) { |
| 784 | oldval = readl(priv->base + MSTPSR(reg)); |
| 785 | if (!(oldval & mask)) |
| 786 | break; |
| 787 | cpu_relax(); |
| 788 | } |
| 789 | |
| 790 | if (!i) |
| 791 | dev_warn(dev, "Failed to enable SMSTP %p[0x%x]\n", |
| 792 | priv->base + SMSTPCR(reg), oldval & mask); |
| 793 | } |
| 794 | |
| 795 | return 0; |
| 796 | } |
| 797 | |
| 798 | static const struct dev_pm_ops cpg_mssr_pm = { |
| 799 | SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(cpg_mssr_suspend_noirq, |
| 800 | cpg_mssr_resume_noirq) |
| 801 | }; |
| 802 | #define DEV_PM_OPS &cpg_mssr_pm |
| 803 | #else |
| 804 | #define DEV_PM_OPS NULL |
| 805 | #endif /* CONFIG_PM_SLEEP && CONFIG_ARM_PSCI_FW */ |
| 806 | |
Geert Uytterhoeven | f793d1e | 2015-10-16 11:41:19 +0200 | [diff] [blame] | 807 | static int __init cpg_mssr_probe(struct platform_device *pdev) |
| 808 | { |
| 809 | struct device *dev = &pdev->dev; |
| 810 | struct device_node *np = dev->of_node; |
| 811 | const struct cpg_mssr_info *info; |
| 812 | struct cpg_mssr_priv *priv; |
| 813 | unsigned int nclks, i; |
| 814 | struct resource *res; |
| 815 | struct clk **clks; |
| 816 | int error; |
| 817 | |
Geert Uytterhoeven | 34deaff | 2017-06-09 14:49:43 +0200 | [diff] [blame] | 818 | info = of_device_get_match_data(dev); |
Geert Uytterhoeven | f793d1e | 2015-10-16 11:41:19 +0200 | [diff] [blame] | 819 | if (info->init) { |
| 820 | error = info->init(dev); |
| 821 | if (error) |
| 822 | return error; |
| 823 | } |
| 824 | |
| 825 | priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL); |
| 826 | if (!priv) |
| 827 | return -ENOMEM; |
| 828 | |
| 829 | priv->dev = dev; |
Geert Uytterhoeven | a4ea6a0 | 2017-01-20 10:58:11 +0100 | [diff] [blame] | 830 | spin_lock_init(&priv->rmw_lock); |
Geert Uytterhoeven | f793d1e | 2015-10-16 11:41:19 +0200 | [diff] [blame] | 831 | |
| 832 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 833 | priv->base = devm_ioremap_resource(dev, res); |
| 834 | if (IS_ERR(priv->base)) |
| 835 | return PTR_ERR(priv->base); |
| 836 | |
| 837 | nclks = info->num_total_core_clks + info->num_hw_mod_clks; |
| 838 | clks = devm_kmalloc_array(dev, nclks, sizeof(*clks), GFP_KERNEL); |
| 839 | if (!clks) |
| 840 | return -ENOMEM; |
| 841 | |
Geert Uytterhoeven | 5608691 | 2017-06-07 13:20:06 +0200 | [diff] [blame] | 842 | dev_set_drvdata(dev, priv); |
Geert Uytterhoeven | f793d1e | 2015-10-16 11:41:19 +0200 | [diff] [blame] | 843 | priv->clks = clks; |
| 844 | priv->num_core_clks = info->num_total_core_clks; |
| 845 | priv->num_mod_clks = info->num_hw_mod_clks; |
| 846 | priv->last_dt_core_clk = info->last_dt_core_clk; |
Geert Uytterhoeven | 1f4023c | 2017-06-21 22:24:15 +0200 | [diff] [blame] | 847 | RAW_INIT_NOTIFIER_HEAD(&priv->notifiers); |
Geert Uytterhoeven | f793d1e | 2015-10-16 11:41:19 +0200 | [diff] [blame] | 848 | |
| 849 | for (i = 0; i < nclks; i++) |
| 850 | clks[i] = ERR_PTR(-ENOENT); |
| 851 | |
| 852 | for (i = 0; i < info->num_core_clks; i++) |
| 853 | cpg_mssr_register_core_clk(&info->core_clks[i], info, priv); |
| 854 | |
| 855 | for (i = 0; i < info->num_mod_clks; i++) |
| 856 | cpg_mssr_register_mod_clk(&info->mod_clks[i], info, priv); |
| 857 | |
| 858 | error = of_clk_add_provider(np, cpg_mssr_clk_src_twocell_get, priv); |
| 859 | if (error) |
| 860 | return error; |
| 861 | |
Sudip Mukherjee | c7f2318 | 2016-02-23 15:00:03 +0530 | [diff] [blame] | 862 | error = devm_add_action_or_reset(dev, |
| 863 | cpg_mssr_del_clk_provider, |
| 864 | np); |
| 865 | if (error) |
| 866 | return error; |
Geert Uytterhoeven | f793d1e | 2015-10-16 11:41:19 +0200 | [diff] [blame] | 867 | |
| 868 | error = cpg_mssr_add_clk_domain(dev, info->core_pm_clks, |
| 869 | info->num_core_pm_clks); |
| 870 | if (error) |
| 871 | return error; |
| 872 | |
Geert Uytterhoeven | 6197aa6 | 2017-01-20 11:03:03 +0100 | [diff] [blame] | 873 | error = cpg_mssr_reset_controller_register(priv); |
| 874 | if (error) |
| 875 | return error; |
| 876 | |
Geert Uytterhoeven | f793d1e | 2015-10-16 11:41:19 +0200 | [diff] [blame] | 877 | return 0; |
| 878 | } |
| 879 | |
| 880 | static struct platform_driver cpg_mssr_driver = { |
| 881 | .driver = { |
| 882 | .name = "renesas-cpg-mssr", |
| 883 | .of_match_table = cpg_mssr_match, |
Geert Uytterhoeven | 5608691 | 2017-06-07 13:20:06 +0200 | [diff] [blame] | 884 | .pm = DEV_PM_OPS, |
Geert Uytterhoeven | f793d1e | 2015-10-16 11:41:19 +0200 | [diff] [blame] | 885 | }, |
| 886 | }; |
| 887 | |
| 888 | static int __init cpg_mssr_init(void) |
| 889 | { |
| 890 | return platform_driver_probe(&cpg_mssr_driver, cpg_mssr_probe); |
| 891 | } |
| 892 | |
| 893 | subsys_initcall(cpg_mssr_init); |
| 894 | |
Geert Uytterhoeven | 48d0341 | 2016-09-29 14:47:58 +0200 | [diff] [blame] | 895 | void __init cpg_core_nullify_range(struct cpg_core_clk *core_clks, |
| 896 | unsigned int num_core_clks, |
| 897 | unsigned int first_clk, |
| 898 | unsigned int last_clk) |
| 899 | { |
| 900 | unsigned int i; |
| 901 | |
| 902 | for (i = 0; i < num_core_clks; i++) |
| 903 | if (core_clks[i].id >= first_clk && |
| 904 | core_clks[i].id <= last_clk) |
| 905 | core_clks[i].name = NULL; |
| 906 | } |
| 907 | |
| 908 | void __init mssr_mod_nullify(struct mssr_mod_clk *mod_clks, |
| 909 | unsigned int num_mod_clks, |
| 910 | const unsigned int *clks, unsigned int n) |
| 911 | { |
| 912 | unsigned int i, j; |
| 913 | |
| 914 | for (i = 0, j = 0; i < num_mod_clks && j < n; i++) |
| 915 | if (mod_clks[i].id == clks[j]) { |
| 916 | mod_clks[i].name = NULL; |
| 917 | j++; |
| 918 | } |
| 919 | } |
| 920 | |
| 921 | void __init mssr_mod_reparent(struct mssr_mod_clk *mod_clks, |
| 922 | unsigned int num_mod_clks, |
| 923 | const struct mssr_mod_reparent *clks, |
| 924 | unsigned int n) |
| 925 | { |
| 926 | unsigned int i, j; |
| 927 | |
| 928 | for (i = 0, j = 0; i < num_mod_clks && j < n; i++) |
| 929 | if (mod_clks[i].id == clks[j].clk) { |
| 930 | mod_clks[i].parent = clks[j].parent; |
| 931 | j++; |
| 932 | } |
| 933 | } |
| 934 | |
Geert Uytterhoeven | f793d1e | 2015-10-16 11:41:19 +0200 | [diff] [blame] | 935 | MODULE_DESCRIPTION("Renesas CPG/MSSR Driver"); |
| 936 | MODULE_LICENSE("GPL v2"); |