Peng Fan | 0f08515 | 2019-07-31 07:01:34 +0000 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* |
| 3 | * Copyright (C) 2010-2011 Canonical Ltd <jeremy.kerr@canonical.com> |
| 4 | * Copyright (C) 2011-2012 Mike Turquette, Linaro Ltd <mturquette@linaro.org> |
| 5 | * Copyright 2019 NXP |
| 6 | * |
| 7 | * Gated clock implementation |
| 8 | */ |
| 9 | |
Patrick Delaunay | 8767e79 | 2021-11-19 15:12:07 +0100 | [diff] [blame] | 10 | #define LOG_CATEGORY UCLASS_CLK |
| 11 | |
Peng Fan | 0f08515 | 2019-07-31 07:01:34 +0000 | [diff] [blame] | 12 | #include <common.h> |
Patrick Delaunay | 283dadf | 2021-11-19 15:12:06 +0100 | [diff] [blame] | 13 | #include <clk.h> |
Patrick Delaunay | 8767e79 | 2021-11-19 15:12:07 +0100 | [diff] [blame] | 14 | #include <log.h> |
Peng Fan | 0f08515 | 2019-07-31 07:01:34 +0000 | [diff] [blame] | 15 | #include <clk-uclass.h> |
Patrick Delaunay | 283dadf | 2021-11-19 15:12:06 +0100 | [diff] [blame] | 16 | #include <malloc.h> |
| 17 | #include <asm/io.h> |
Peng Fan | 0f08515 | 2019-07-31 07:01:34 +0000 | [diff] [blame] | 18 | #include <dm/device.h> |
Patrick Delaunay | 8767e79 | 2021-11-19 15:12:07 +0100 | [diff] [blame] | 19 | #include <dm/device_compat.h> |
Simon Glass | d66c5f7 | 2020-02-03 07:36:15 -0700 | [diff] [blame] | 20 | #include <dm/devres.h> |
Simon Glass | 4dcacfc | 2020-05-10 11:40:13 -0600 | [diff] [blame] | 21 | #include <linux/bitops.h> |
Peng Fan | 0f08515 | 2019-07-31 07:01:34 +0000 | [diff] [blame] | 22 | #include <linux/clk-provider.h> |
Simon Glass | d66c5f7 | 2020-02-03 07:36:15 -0700 | [diff] [blame] | 23 | #include <linux/err.h> |
Peng Fan | 0f08515 | 2019-07-31 07:01:34 +0000 | [diff] [blame] | 24 | |
Patrick Delaunay | 283dadf | 2021-11-19 15:12:06 +0100 | [diff] [blame] | 25 | #include "clk.h" |
| 26 | |
Peng Fan | 0f08515 | 2019-07-31 07:01:34 +0000 | [diff] [blame] | 27 | #define UBOOT_DM_CLK_GATE "clk_gate" |
| 28 | |
| 29 | /** |
| 30 | * DOC: basic gatable clock which can gate and ungate it's output |
| 31 | * |
| 32 | * Traits of this clock: |
| 33 | * prepare - clk_(un)prepare only ensures parent is (un)prepared |
| 34 | * enable - clk_enable and clk_disable are functional & control gating |
| 35 | * rate - inherits rate from parent. No clk_set_rate support |
| 36 | * parent - fixed parent. No clk_set_parent support |
| 37 | */ |
| 38 | |
| 39 | /* |
| 40 | * It works on following logic: |
| 41 | * |
| 42 | * For enabling clock, enable = 1 |
| 43 | * set2dis = 1 -> clear bit -> set = 0 |
| 44 | * set2dis = 0 -> set bit -> set = 1 |
| 45 | * |
| 46 | * For disabling clock, enable = 0 |
| 47 | * set2dis = 1 -> set bit -> set = 1 |
| 48 | * set2dis = 0 -> clear bit -> set = 0 |
| 49 | * |
| 50 | * So, result is always: enable xor set2dis. |
| 51 | */ |
| 52 | static void clk_gate_endisable(struct clk *clk, int enable) |
| 53 | { |
Sean Anderson | cfc2f02 | 2020-06-24 06:41:06 -0400 | [diff] [blame] | 54 | struct clk_gate *gate = to_clk_gate(clk); |
Peng Fan | 0f08515 | 2019-07-31 07:01:34 +0000 | [diff] [blame] | 55 | int set = gate->flags & CLK_GATE_SET_TO_DISABLE ? 1 : 0; |
| 56 | u32 reg; |
| 57 | |
| 58 | set ^= enable; |
| 59 | |
| 60 | if (gate->flags & CLK_GATE_HIWORD_MASK) { |
| 61 | reg = BIT(gate->bit_idx + 16); |
| 62 | if (set) |
| 63 | reg |= BIT(gate->bit_idx); |
| 64 | } else { |
Simon Glass | 0a6a0c4 | 2023-02-05 15:40:43 -0700 | [diff] [blame] | 65 | #if IS_ENABLED(CONFIG_SANDBOX_CLK_CCF) |
Peng Fan | 3b7f3ae | 2019-07-31 07:01:57 +0000 | [diff] [blame] | 66 | reg = gate->io_gate_val; |
| 67 | #else |
Peng Fan | 0f08515 | 2019-07-31 07:01:34 +0000 | [diff] [blame] | 68 | reg = readl(gate->reg); |
Peng Fan | 3b7f3ae | 2019-07-31 07:01:57 +0000 | [diff] [blame] | 69 | #endif |
Peng Fan | 0f08515 | 2019-07-31 07:01:34 +0000 | [diff] [blame] | 70 | |
| 71 | if (set) |
| 72 | reg |= BIT(gate->bit_idx); |
| 73 | else |
| 74 | reg &= ~BIT(gate->bit_idx); |
| 75 | } |
| 76 | |
| 77 | writel(reg, gate->reg); |
| 78 | } |
| 79 | |
| 80 | static int clk_gate_enable(struct clk *clk) |
| 81 | { |
| 82 | clk_gate_endisable(clk, 1); |
| 83 | |
| 84 | return 0; |
| 85 | } |
| 86 | |
| 87 | static int clk_gate_disable(struct clk *clk) |
| 88 | { |
| 89 | clk_gate_endisable(clk, 0); |
| 90 | |
| 91 | return 0; |
| 92 | } |
| 93 | |
| 94 | int clk_gate_is_enabled(struct clk *clk) |
| 95 | { |
Sean Anderson | cfc2f02 | 2020-06-24 06:41:06 -0400 | [diff] [blame] | 96 | struct clk_gate *gate = to_clk_gate(clk); |
Peng Fan | 0f08515 | 2019-07-31 07:01:34 +0000 | [diff] [blame] | 97 | u32 reg; |
| 98 | |
Simon Glass | 0a6a0c4 | 2023-02-05 15:40:43 -0700 | [diff] [blame] | 99 | #if IS_ENABLED(CONFIG_SANDBOX_CLK_CCF) |
Peng Fan | 3b7f3ae | 2019-07-31 07:01:57 +0000 | [diff] [blame] | 100 | reg = gate->io_gate_val; |
| 101 | #else |
Peng Fan | 0f08515 | 2019-07-31 07:01:34 +0000 | [diff] [blame] | 102 | reg = readl(gate->reg); |
Peng Fan | 3b7f3ae | 2019-07-31 07:01:57 +0000 | [diff] [blame] | 103 | #endif |
Peng Fan | 0f08515 | 2019-07-31 07:01:34 +0000 | [diff] [blame] | 104 | |
| 105 | /* if a set bit disables this clk, flip it before masking */ |
| 106 | if (gate->flags & CLK_GATE_SET_TO_DISABLE) |
| 107 | reg ^= BIT(gate->bit_idx); |
| 108 | |
| 109 | reg &= BIT(gate->bit_idx); |
| 110 | |
| 111 | return reg ? 1 : 0; |
| 112 | } |
| 113 | |
| 114 | const struct clk_ops clk_gate_ops = { |
| 115 | .enable = clk_gate_enable, |
| 116 | .disable = clk_gate_disable, |
| 117 | .get_rate = clk_generic_get_rate, |
| 118 | }; |
| 119 | |
| 120 | struct clk *clk_register_gate(struct device *dev, const char *name, |
| 121 | const char *parent_name, unsigned long flags, |
| 122 | void __iomem *reg, u8 bit_idx, |
| 123 | u8 clk_gate_flags, spinlock_t *lock) |
| 124 | { |
| 125 | struct clk_gate *gate; |
| 126 | struct clk *clk; |
| 127 | int ret; |
| 128 | |
| 129 | if (clk_gate_flags & CLK_GATE_HIWORD_MASK) { |
| 130 | if (bit_idx > 15) { |
Patrick Delaunay | 8767e79 | 2021-11-19 15:12:07 +0100 | [diff] [blame] | 131 | dev_err(dev, "gate bit exceeds LOWORD field\n"); |
Peng Fan | 0f08515 | 2019-07-31 07:01:34 +0000 | [diff] [blame] | 132 | return ERR_PTR(-EINVAL); |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | /* allocate the gate */ |
| 137 | gate = kzalloc(sizeof(*gate), GFP_KERNEL); |
| 138 | if (!gate) |
| 139 | return ERR_PTR(-ENOMEM); |
| 140 | |
| 141 | /* struct clk_gate assignments */ |
| 142 | gate->reg = reg; |
| 143 | gate->bit_idx = bit_idx; |
| 144 | gate->flags = clk_gate_flags; |
Simon Glass | 0a6a0c4 | 2023-02-05 15:40:43 -0700 | [diff] [blame] | 145 | #if IS_ENABLED(CONFIG_SANDBOX_CLK_CCF) |
Peng Fan | 3b7f3ae | 2019-07-31 07:01:57 +0000 | [diff] [blame] | 146 | gate->io_gate_val = *(u32 *)reg; |
| 147 | #endif |
Peng Fan | 0f08515 | 2019-07-31 07:01:34 +0000 | [diff] [blame] | 148 | |
| 149 | clk = &gate->clk; |
Dario Binacchi | 1a62dc1 | 2020-04-13 14:36:27 +0200 | [diff] [blame] | 150 | clk->flags = flags; |
Peng Fan | 0f08515 | 2019-07-31 07:01:34 +0000 | [diff] [blame] | 151 | |
| 152 | ret = clk_register(clk, UBOOT_DM_CLK_GATE, name, parent_name); |
| 153 | if (ret) { |
| 154 | kfree(gate); |
| 155 | return ERR_PTR(ret); |
| 156 | } |
| 157 | |
| 158 | return clk; |
| 159 | } |
| 160 | |
| 161 | U_BOOT_DRIVER(clk_gate) = { |
| 162 | .name = UBOOT_DM_CLK_GATE, |
| 163 | .id = UCLASS_CLK, |
| 164 | .ops = &clk_gate_ops, |
| 165 | .flags = DM_FLAG_PRE_RELOC, |
| 166 | }; |