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